aoe: revert printk macros
[linux-2.6.22.y-op.git] / drivers / block / aoe / aoechr.c
blobe22b4c9520a9fdecee3317636bb532ea6404cda6
1 /* Copyright (c) 2006 Coraid, Inc. See COPYING for GPL terms. */
2 /*
3 * aoechr.c
4 * AoE character device driver
5 */
7 #include <linux/hdreg.h>
8 #include <linux/blkdev.h>
9 #include "aoe.h"
11 enum {
12 //MINOR_STAT = 1, (moved to sysfs)
13 MINOR_ERR = 2,
14 MINOR_DISCOVER,
15 MINOR_INTERFACES,
16 MINOR_REVALIDATE,
17 MSGSZ = 2048,
18 NMSG = 100, /* message backlog to retain */
21 struct aoe_chardev {
22 ulong minor;
23 char name[32];
26 enum { EMFL_VALID = 1 };
28 struct ErrMsg {
29 short flags;
30 short len;
31 char *msg;
34 static struct ErrMsg emsgs[NMSG];
35 static int emsgs_head_idx, emsgs_tail_idx;
36 static struct semaphore emsgs_sema;
37 static spinlock_t emsgs_lock;
38 static int nblocked_emsgs_readers;
39 static struct class *aoe_class;
40 static struct aoe_chardev chardevs[] = {
41 { MINOR_ERR, "err" },
42 { MINOR_DISCOVER, "discover" },
43 { MINOR_INTERFACES, "interfaces" },
44 { MINOR_REVALIDATE, "revalidate" },
47 static int
48 discover(void)
50 aoecmd_cfg(0xffff, 0xff);
51 return 0;
54 static int
55 interfaces(const char __user *str, size_t size)
57 if (set_aoe_iflist(str, size)) {
58 printk(KERN_ERR
59 "aoe: could not set interface list: too many interfaces\n");
60 return -EINVAL;
62 return 0;
65 static int
66 revalidate(const char __user *str, size_t size)
68 int major, minor, n;
69 ulong flags;
70 struct aoedev *d;
71 char buf[16];
73 if (size >= sizeof buf)
74 return -EINVAL;
75 buf[sizeof buf - 1] = '\0';
76 if (copy_from_user(buf, str, size))
77 return -EFAULT;
79 /* should be e%d.%d format */
80 n = sscanf(buf, "e%d.%d", &major, &minor);
81 if (n != 2) {
82 printk(KERN_ERR "aoe: invalid device specification\n");
83 return -EINVAL;
85 d = aoedev_by_aoeaddr(major, minor);
86 if (!d)
87 return -EINVAL;
89 spin_lock_irqsave(&d->lock, flags);
90 d->flags &= ~DEVFL_MAXBCNT;
91 d->flags |= DEVFL_PAUSE;
92 spin_unlock_irqrestore(&d->lock, flags);
93 aoecmd_cfg(major, minor);
95 return 0;
98 void
99 aoechr_error(char *msg)
101 struct ErrMsg *em;
102 char *mp;
103 ulong flags, n;
105 n = strlen(msg);
107 spin_lock_irqsave(&emsgs_lock, flags);
109 em = emsgs + emsgs_tail_idx;
110 if ((em->flags & EMFL_VALID)) {
111 bail: spin_unlock_irqrestore(&emsgs_lock, flags);
112 return;
115 mp = kmalloc(n, GFP_ATOMIC);
116 if (mp == NULL) {
117 printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
118 goto bail;
121 memcpy(mp, msg, n);
122 em->msg = mp;
123 em->flags |= EMFL_VALID;
124 em->len = n;
126 emsgs_tail_idx++;
127 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
129 spin_unlock_irqrestore(&emsgs_lock, flags);
131 if (nblocked_emsgs_readers)
132 up(&emsgs_sema);
135 static ssize_t
136 aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
138 int ret = -EINVAL;
140 switch ((unsigned long) filp->private_data) {
141 default:
142 printk(KERN_INFO "aoe: can't write to that file.\n");
143 break;
144 case MINOR_DISCOVER:
145 ret = discover();
146 break;
147 case MINOR_INTERFACES:
148 ret = interfaces(buf, cnt);
149 break;
150 case MINOR_REVALIDATE:
151 ret = revalidate(buf, cnt);
153 if (ret == 0)
154 ret = cnt;
155 return ret;
158 static int
159 aoechr_open(struct inode *inode, struct file *filp)
161 int n, i;
163 n = iminor(inode);
164 filp->private_data = (void *) (unsigned long) n;
166 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
167 if (chardevs[i].minor == n)
168 return 0;
169 return -EINVAL;
172 static int
173 aoechr_rel(struct inode *inode, struct file *filp)
175 return 0;
178 static ssize_t
179 aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
181 unsigned long n;
182 char *mp;
183 struct ErrMsg *em;
184 ssize_t len;
185 ulong flags;
187 n = (unsigned long) filp->private_data;
188 switch (n) {
189 case MINOR_ERR:
190 spin_lock_irqsave(&emsgs_lock, flags);
191 loop:
192 em = emsgs + emsgs_head_idx;
193 if ((em->flags & EMFL_VALID) == 0) {
194 if (filp->f_flags & O_NDELAY) {
195 spin_unlock_irqrestore(&emsgs_lock, flags);
196 return -EAGAIN;
198 nblocked_emsgs_readers++;
200 spin_unlock_irqrestore(&emsgs_lock, flags);
202 n = down_interruptible(&emsgs_sema);
204 spin_lock_irqsave(&emsgs_lock, flags);
206 nblocked_emsgs_readers--;
208 if (n) {
209 spin_unlock_irqrestore(&emsgs_lock, flags);
210 return -ERESTARTSYS;
212 goto loop;
214 if (em->len > cnt) {
215 spin_unlock_irqrestore(&emsgs_lock, flags);
216 return -EAGAIN;
218 mp = em->msg;
219 len = em->len;
220 em->msg = NULL;
221 em->flags &= ~EMFL_VALID;
223 emsgs_head_idx++;
224 emsgs_head_idx %= ARRAY_SIZE(emsgs);
226 spin_unlock_irqrestore(&emsgs_lock, flags);
228 n = copy_to_user(buf, mp, len);
229 kfree(mp);
230 return n == 0 ? len : -EFAULT;
231 default:
232 return -EFAULT;
236 static struct file_operations aoe_fops = {
237 .write = aoechr_write,
238 .read = aoechr_read,
239 .open = aoechr_open,
240 .release = aoechr_rel,
241 .owner = THIS_MODULE,
244 int __init
245 aoechr_init(void)
247 int n, i;
249 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
250 if (n < 0) {
251 printk(KERN_ERR "aoe: can't register char device\n");
252 return n;
254 sema_init(&emsgs_sema, 0);
255 spin_lock_init(&emsgs_lock);
256 aoe_class = class_create(THIS_MODULE, "aoe");
257 if (IS_ERR(aoe_class)) {
258 unregister_chrdev(AOE_MAJOR, "aoechr");
259 return PTR_ERR(aoe_class);
261 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
262 class_device_create(aoe_class, NULL,
263 MKDEV(AOE_MAJOR, chardevs[i].minor),
264 NULL, chardevs[i].name);
266 return 0;
269 void
270 aoechr_exit(void)
272 int i;
274 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
275 class_device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
276 class_destroy(aoe_class);
277 unregister_chrdev(AOE_MAJOR, "aoechr");