1 /* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */
4 * AoE character device driver
7 #include <linux/hdreg.h>
8 #include <linux/blkdev.h>
9 #include <linux/completion.h>
10 #include <linux/delay.h>
11 #include <linux/slab.h>
12 #include <linux/smp_lock.h>
13 #include <linux/skbuff.h>
17 //MINOR_STAT = 1, (moved to sysfs)
24 NMSG = 100, /* message backlog to retain */
32 enum { EMFL_VALID = 1 };
40 static struct ErrMsg emsgs[NMSG];
41 static int emsgs_head_idx, emsgs_tail_idx;
42 static struct completion emsgs_comp;
43 static spinlock_t emsgs_lock;
44 static int nblocked_emsgs_readers;
45 static struct class *aoe_class;
46 static struct aoe_chardev chardevs[] = {
48 { MINOR_DISCOVER, "discover" },
49 { MINOR_INTERFACES, "interfaces" },
50 { MINOR_REVALIDATE, "revalidate" },
51 { MINOR_FLUSH, "flush" },
57 aoecmd_cfg(0xffff, 0xff);
62 interfaces(const char __user *str, size_t size)
64 if (set_aoe_iflist(str, size)) {
66 "aoe: could not set interface list: too many interfaces\n");
73 revalidate(const char __user *str, size_t size)
81 if (size >= sizeof buf)
83 buf[sizeof buf - 1] = '\0';
84 if (copy_from_user(buf, str, size))
87 /* should be e%d.%d format */
88 n = sscanf(buf, "e%d.%d", &major, &minor);
90 printk(KERN_ERR "aoe: invalid device specification\n");
93 d = aoedev_by_aoeaddr(major, minor);
96 spin_lock_irqsave(&d->lock, flags);
99 skb = aoecmd_ata_id(d);
100 spin_unlock_irqrestore(&d->lock, flags);
101 /* try again if we are able to sleep a bit,
102 * otherwise give up this revalidation
104 if (!skb && !msleep_interruptible(200)) {
105 spin_lock_irqsave(&d->lock, flags);
109 struct sk_buff_head queue;
110 __skb_queue_head_init(&queue);
111 __skb_queue_tail(&queue, skb);
114 aoecmd_cfg(major, minor);
119 aoechr_error(char *msg)
127 spin_lock_irqsave(&emsgs_lock, flags);
129 em = emsgs + emsgs_tail_idx;
130 if ((em->flags & EMFL_VALID)) {
131 bail: spin_unlock_irqrestore(&emsgs_lock, flags);
135 mp = kmalloc(n, GFP_ATOMIC);
137 printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
143 em->flags |= EMFL_VALID;
147 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
149 spin_unlock_irqrestore(&emsgs_lock, flags);
151 if (nblocked_emsgs_readers)
152 complete(&emsgs_comp);
156 aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
160 switch ((unsigned long) filp->private_data) {
162 printk(KERN_INFO "aoe: can't write to that file.\n");
167 case MINOR_INTERFACES:
168 ret = interfaces(buf, cnt);
170 case MINOR_REVALIDATE:
171 ret = revalidate(buf, cnt);
174 ret = aoedev_flush(buf, cnt);
182 aoechr_open(struct inode *inode, struct file *filp)
188 filp->private_data = (void *) (unsigned long) n;
190 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
191 if (chardevs[i].minor == n) {
200 aoechr_rel(struct inode *inode, struct file *filp)
206 aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
214 n = (unsigned long) filp->private_data;
218 spin_lock_irqsave(&emsgs_lock, flags);
221 em = emsgs + emsgs_head_idx;
222 if ((em->flags & EMFL_VALID) != 0)
224 if (filp->f_flags & O_NDELAY) {
225 spin_unlock_irqrestore(&emsgs_lock, flags);
228 nblocked_emsgs_readers++;
230 spin_unlock_irqrestore(&emsgs_lock, flags);
232 n = wait_for_completion_interruptible(&emsgs_comp);
234 spin_lock_irqsave(&emsgs_lock, flags);
236 nblocked_emsgs_readers--;
239 spin_unlock_irqrestore(&emsgs_lock, flags);
244 spin_unlock_irqrestore(&emsgs_lock, flags);
250 em->flags &= ~EMFL_VALID;
253 emsgs_head_idx %= ARRAY_SIZE(emsgs);
255 spin_unlock_irqrestore(&emsgs_lock, flags);
257 n = copy_to_user(buf, mp, len);
259 return n == 0 ? len : -EFAULT;
262 static const struct file_operations aoe_fops = {
263 .write = aoechr_write,
266 .release = aoechr_rel,
267 .owner = THIS_MODULE,
270 static char *aoe_devnode(struct device *dev, mode_t *mode)
272 return kasprintf(GFP_KERNEL, "etherd/%s", dev_name(dev));
280 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
282 printk(KERN_ERR "aoe: can't register char device\n");
285 init_completion(&emsgs_comp);
286 spin_lock_init(&emsgs_lock);
287 aoe_class = class_create(THIS_MODULE, "aoe");
288 if (IS_ERR(aoe_class)) {
289 unregister_chrdev(AOE_MAJOR, "aoechr");
290 return PTR_ERR(aoe_class);
292 aoe_class->devnode = aoe_devnode;
294 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
295 device_create(aoe_class, NULL,
296 MKDEV(AOE_MAJOR, chardevs[i].minor), NULL,
307 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
308 device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
309 class_destroy(aoe_class);
310 unregister_chrdev(AOE_MAJOR, "aoechr");