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/smp_lock.h>
15 //MINOR_STAT = 1, (moved to sysfs)
22 NMSG = 100, /* message backlog to retain */
30 enum { EMFL_VALID = 1 };
38 static struct ErrMsg emsgs[NMSG];
39 static int emsgs_head_idx, emsgs_tail_idx;
40 static struct completion emsgs_comp;
41 static spinlock_t emsgs_lock;
42 static int nblocked_emsgs_readers;
43 static struct class *aoe_class;
44 static struct aoe_chardev chardevs[] = {
46 { MINOR_DISCOVER, "discover" },
47 { MINOR_INTERFACES, "interfaces" },
48 { MINOR_REVALIDATE, "revalidate" },
49 { MINOR_FLUSH, "flush" },
55 aoecmd_cfg(0xffff, 0xff);
60 interfaces(const char __user *str, size_t size)
62 if (set_aoe_iflist(str, size)) {
64 "aoe: could not set interface list: too many interfaces\n");
71 revalidate(const char __user *str, size_t size)
79 if (size >= sizeof buf)
81 buf[sizeof buf - 1] = '\0';
82 if (copy_from_user(buf, str, size))
85 /* should be e%d.%d format */
86 n = sscanf(buf, "e%d.%d", &major, &minor);
88 printk(KERN_ERR "aoe: invalid device specification\n");
91 d = aoedev_by_aoeaddr(major, minor);
94 spin_lock_irqsave(&d->lock, flags);
97 skb = aoecmd_ata_id(d);
98 spin_unlock_irqrestore(&d->lock, flags);
99 /* try again if we are able to sleep a bit,
100 * otherwise give up this revalidation
102 if (!skb && !msleep_interruptible(200)) {
103 spin_lock_irqsave(&d->lock, flags);
107 aoecmd_cfg(major, minor);
112 aoechr_error(char *msg)
120 spin_lock_irqsave(&emsgs_lock, flags);
122 em = emsgs + emsgs_tail_idx;
123 if ((em->flags & EMFL_VALID)) {
124 bail: spin_unlock_irqrestore(&emsgs_lock, flags);
128 mp = kmalloc(n, GFP_ATOMIC);
130 printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
136 em->flags |= EMFL_VALID;
140 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
142 spin_unlock_irqrestore(&emsgs_lock, flags);
144 if (nblocked_emsgs_readers)
145 complete(&emsgs_comp);
149 aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
153 switch ((unsigned long) filp->private_data) {
155 printk(KERN_INFO "aoe: can't write to that file.\n");
160 case MINOR_INTERFACES:
161 ret = interfaces(buf, cnt);
163 case MINOR_REVALIDATE:
164 ret = revalidate(buf, cnt);
167 ret = aoedev_flush(buf, cnt);
175 aoechr_open(struct inode *inode, struct file *filp)
181 filp->private_data = (void *) (unsigned long) n;
183 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
184 if (chardevs[i].minor == n) {
193 aoechr_rel(struct inode *inode, struct file *filp)
199 aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
207 n = (unsigned long) filp->private_data;
211 spin_lock_irqsave(&emsgs_lock, flags);
214 em = emsgs + emsgs_head_idx;
215 if ((em->flags & EMFL_VALID) != 0)
217 if (filp->f_flags & O_NDELAY) {
218 spin_unlock_irqrestore(&emsgs_lock, flags);
221 nblocked_emsgs_readers++;
223 spin_unlock_irqrestore(&emsgs_lock, flags);
225 n = wait_for_completion_interruptible(&emsgs_comp);
227 spin_lock_irqsave(&emsgs_lock, flags);
229 nblocked_emsgs_readers--;
232 spin_unlock_irqrestore(&emsgs_lock, flags);
237 spin_unlock_irqrestore(&emsgs_lock, flags);
243 em->flags &= ~EMFL_VALID;
246 emsgs_head_idx %= ARRAY_SIZE(emsgs);
248 spin_unlock_irqrestore(&emsgs_lock, flags);
250 n = copy_to_user(buf, mp, len);
252 return n == 0 ? len : -EFAULT;
255 static const struct file_operations aoe_fops = {
256 .write = aoechr_write,
259 .release = aoechr_rel,
260 .owner = THIS_MODULE,
268 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
270 printk(KERN_ERR "aoe: can't register char device\n");
273 init_completion(&emsgs_comp);
274 spin_lock_init(&emsgs_lock);
275 aoe_class = class_create(THIS_MODULE, "aoe");
276 if (IS_ERR(aoe_class)) {
277 unregister_chrdev(AOE_MAJOR, "aoechr");
278 return PTR_ERR(aoe_class);
280 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
281 device_create_drvdata(aoe_class, NULL,
282 MKDEV(AOE_MAJOR, chardevs[i].minor),
283 NULL, chardevs[i].name);
293 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
294 device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
295 class_destroy(aoe_class);
296 unregister_chrdev(AOE_MAJOR, "aoechr");