1 /* Copyright (c) 2006 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)
19 NMSG = 100, /* message backlog to retain */
27 enum { EMFL_VALID = 1 };
35 static struct ErrMsg emsgs[NMSG];
36 static int emsgs_head_idx, emsgs_tail_idx;
37 static struct semaphore emsgs_sema;
38 static spinlock_t emsgs_lock;
39 static int nblocked_emsgs_readers;
40 static struct class *aoe_class;
41 static struct aoe_chardev chardevs[] = {
43 { MINOR_DISCOVER, "discover" },
44 { MINOR_INTERFACES, "interfaces" },
45 { MINOR_REVALIDATE, "revalidate" },
51 aoecmd_cfg(0xffff, 0xff);
56 interfaces(const char __user *str, size_t size)
58 if (set_aoe_iflist(str, size)) {
60 "aoe: could not set interface list: too many interfaces\n");
67 revalidate(const char __user *str, size_t size)
75 if (size >= sizeof buf)
77 buf[sizeof buf - 1] = '\0';
78 if (copy_from_user(buf, str, size))
81 /* should be e%d.%d format */
82 n = sscanf(buf, "e%d.%d", &major, &minor);
84 printk(KERN_ERR "aoe: invalid device specification\n");
87 d = aoedev_by_aoeaddr(major, minor);
90 spin_lock_irqsave(&d->lock, flags);
93 skb = aoecmd_ata_id(d);
94 spin_unlock_irqrestore(&d->lock, flags);
95 /* try again if we are able to sleep a bit,
96 * otherwise give up this revalidation
98 if (!skb && !msleep_interruptible(200)) {
99 spin_lock_irqsave(&d->lock, flags);
103 aoecmd_cfg(major, minor);
108 aoechr_error(char *msg)
116 spin_lock_irqsave(&emsgs_lock, flags);
118 em = emsgs + emsgs_tail_idx;
119 if ((em->flags & EMFL_VALID)) {
120 bail: spin_unlock_irqrestore(&emsgs_lock, flags);
124 mp = kmalloc(n, GFP_ATOMIC);
126 printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
132 em->flags |= EMFL_VALID;
136 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
138 spin_unlock_irqrestore(&emsgs_lock, flags);
140 if (nblocked_emsgs_readers)
145 aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
149 switch ((unsigned long) filp->private_data) {
151 printk(KERN_INFO "aoe: can't write to that file.\n");
156 case MINOR_INTERFACES:
157 ret = interfaces(buf, cnt);
159 case MINOR_REVALIDATE:
160 ret = revalidate(buf, cnt);
168 aoechr_open(struct inode *inode, struct file *filp)
173 filp->private_data = (void *) (unsigned long) n;
175 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
176 if (chardevs[i].minor == n)
182 aoechr_rel(struct inode *inode, struct file *filp)
188 aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
196 n = (unsigned long) filp->private_data;
200 spin_lock_irqsave(&emsgs_lock, flags);
203 em = emsgs + emsgs_head_idx;
204 if ((em->flags & EMFL_VALID) != 0)
206 if (filp->f_flags & O_NDELAY) {
207 spin_unlock_irqrestore(&emsgs_lock, flags);
210 nblocked_emsgs_readers++;
212 spin_unlock_irqrestore(&emsgs_lock, flags);
214 n = down_interruptible(&emsgs_sema);
216 spin_lock_irqsave(&emsgs_lock, flags);
218 nblocked_emsgs_readers--;
221 spin_unlock_irqrestore(&emsgs_lock, flags);
226 spin_unlock_irqrestore(&emsgs_lock, flags);
232 em->flags &= ~EMFL_VALID;
235 emsgs_head_idx %= ARRAY_SIZE(emsgs);
237 spin_unlock_irqrestore(&emsgs_lock, flags);
239 n = copy_to_user(buf, mp, len);
241 return n == 0 ? len : -EFAULT;
244 static const struct file_operations aoe_fops = {
245 .write = aoechr_write,
248 .release = aoechr_rel,
249 .owner = THIS_MODULE,
257 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
259 printk(KERN_ERR "aoe: can't register char device\n");
262 sema_init(&emsgs_sema, 0);
263 spin_lock_init(&emsgs_lock);
264 aoe_class = class_create(THIS_MODULE, "aoe");
265 if (IS_ERR(aoe_class)) {
266 unregister_chrdev(AOE_MAJOR, "aoechr");
267 return PTR_ERR(aoe_class);
269 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
270 device_create(aoe_class, NULL,
271 MKDEV(AOE_MAJOR, chardevs[i].minor), chardevs[i].name);
281 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
282 device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
283 class_destroy(aoe_class);
284 unregister_chrdev(AOE_MAJOR, "aoechr");