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/delay.h>
13 //MINOR_STAT = 1, (moved to sysfs)
20 NMSG = 100, /* message backlog to retain */
28 enum { EMFL_VALID = 1 };
36 static struct ErrMsg emsgs[NMSG];
37 static int emsgs_head_idx, emsgs_tail_idx;
38 static struct semaphore emsgs_sema;
39 static spinlock_t emsgs_lock;
40 static int nblocked_emsgs_readers;
41 static struct class *aoe_class;
42 static struct aoe_chardev chardevs[] = {
44 { MINOR_DISCOVER, "discover" },
45 { MINOR_INTERFACES, "interfaces" },
46 { MINOR_REVALIDATE, "revalidate" },
47 { MINOR_FLUSH, "flush" },
53 aoecmd_cfg(0xffff, 0xff);
58 interfaces(const char __user *str, size_t size)
60 if (set_aoe_iflist(str, size)) {
62 "aoe: could not set interface list: too many interfaces\n");
69 revalidate(const char __user *str, size_t size)
77 if (size >= sizeof buf)
79 buf[sizeof buf - 1] = '\0';
80 if (copy_from_user(buf, str, size))
83 /* should be e%d.%d format */
84 n = sscanf(buf, "e%d.%d", &major, &minor);
86 printk(KERN_ERR "aoe: invalid device specification\n");
89 d = aoedev_by_aoeaddr(major, minor);
92 spin_lock_irqsave(&d->lock, flags);
95 skb = aoecmd_ata_id(d);
96 spin_unlock_irqrestore(&d->lock, flags);
97 /* try again if we are able to sleep a bit,
98 * otherwise give up this revalidation
100 if (!skb && !msleep_interruptible(200)) {
101 spin_lock_irqsave(&d->lock, flags);
105 aoecmd_cfg(major, minor);
110 aoechr_error(char *msg)
118 spin_lock_irqsave(&emsgs_lock, flags);
120 em = emsgs + emsgs_tail_idx;
121 if ((em->flags & EMFL_VALID)) {
122 bail: spin_unlock_irqrestore(&emsgs_lock, flags);
126 mp = kmalloc(n, GFP_ATOMIC);
128 printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
134 em->flags |= EMFL_VALID;
138 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
140 spin_unlock_irqrestore(&emsgs_lock, flags);
142 if (nblocked_emsgs_readers)
147 aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
151 switch ((unsigned long) filp->private_data) {
153 printk(KERN_INFO "aoe: can't write to that file.\n");
158 case MINOR_INTERFACES:
159 ret = interfaces(buf, cnt);
161 case MINOR_REVALIDATE:
162 ret = revalidate(buf, cnt);
165 ret = aoedev_flush(buf, cnt);
173 aoechr_open(struct inode *inode, struct file *filp)
178 filp->private_data = (void *) (unsigned long) n;
180 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
181 if (chardevs[i].minor == n)
187 aoechr_rel(struct inode *inode, struct file *filp)
193 aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
201 n = (unsigned long) filp->private_data;
205 spin_lock_irqsave(&emsgs_lock, flags);
208 em = emsgs + emsgs_head_idx;
209 if ((em->flags & EMFL_VALID) != 0)
211 if (filp->f_flags & O_NDELAY) {
212 spin_unlock_irqrestore(&emsgs_lock, flags);
215 nblocked_emsgs_readers++;
217 spin_unlock_irqrestore(&emsgs_lock, flags);
219 n = down_interruptible(&emsgs_sema);
221 spin_lock_irqsave(&emsgs_lock, flags);
223 nblocked_emsgs_readers--;
226 spin_unlock_irqrestore(&emsgs_lock, flags);
231 spin_unlock_irqrestore(&emsgs_lock, flags);
237 em->flags &= ~EMFL_VALID;
240 emsgs_head_idx %= ARRAY_SIZE(emsgs);
242 spin_unlock_irqrestore(&emsgs_lock, flags);
244 n = copy_to_user(buf, mp, len);
246 return n == 0 ? len : -EFAULT;
249 static const struct file_operations aoe_fops = {
250 .write = aoechr_write,
253 .release = aoechr_rel,
254 .owner = THIS_MODULE,
262 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
264 printk(KERN_ERR "aoe: can't register char device\n");
267 sema_init(&emsgs_sema, 0);
268 spin_lock_init(&emsgs_lock);
269 aoe_class = class_create(THIS_MODULE, "aoe");
270 if (IS_ERR(aoe_class)) {
271 unregister_chrdev(AOE_MAJOR, "aoechr");
272 return PTR_ERR(aoe_class);
274 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
275 device_create(aoe_class, NULL,
276 MKDEV(AOE_MAJOR, chardevs[i].minor), chardevs[i].name);
286 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
287 device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
288 class_destroy(aoe_class);
289 unregister_chrdev(AOE_MAJOR, "aoechr");