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>
12 //MINOR_STAT = 1, (moved to sysfs)
18 NMSG = 100, /* message backlog to retain */
26 enum { EMFL_VALID = 1 };
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[] = {
42 { MINOR_DISCOVER, "discover" },
43 { MINOR_INTERFACES, "interfaces" },
44 { MINOR_REVALIDATE, "revalidate" },
50 aoecmd_cfg(0xffff, 0xff);
55 interfaces(const char __user *str, size_t size)
57 if (set_aoe_iflist(str, size)) {
59 "aoe: could not set interface list: too many interfaces\n");
66 revalidate(const char __user *str, size_t size)
73 if (size >= sizeof buf)
75 buf[sizeof buf - 1] = '\0';
76 if (copy_from_user(buf, str, size))
79 /* should be e%d.%d format */
80 n = sscanf(buf, "e%d.%d", &major, &minor);
82 printk(KERN_ERR "aoe: invalid device specification\n");
85 d = aoedev_by_aoeaddr(major, minor);
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);
99 aoechr_error(char *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);
115 mp = kmalloc(n, GFP_ATOMIC);
117 printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
123 em->flags |= EMFL_VALID;
127 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
129 spin_unlock_irqrestore(&emsgs_lock, flags);
131 if (nblocked_emsgs_readers)
136 aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
140 switch ((unsigned long) filp->private_data) {
142 printk(KERN_INFO "aoe: can't write to that file.\n");
147 case MINOR_INTERFACES:
148 ret = interfaces(buf, cnt);
150 case MINOR_REVALIDATE:
151 ret = revalidate(buf, cnt);
159 aoechr_open(struct inode *inode, struct file *filp)
164 filp->private_data = (void *) (unsigned long) n;
166 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
167 if (chardevs[i].minor == n)
173 aoechr_rel(struct inode *inode, struct file *filp)
179 aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
187 n = (unsigned long) filp->private_data;
190 spin_lock_irqsave(&emsgs_lock, flags);
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);
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--;
209 spin_unlock_irqrestore(&emsgs_lock, flags);
215 spin_unlock_irqrestore(&emsgs_lock, flags);
221 em->flags &= ~EMFL_VALID;
224 emsgs_head_idx %= ARRAY_SIZE(emsgs);
226 spin_unlock_irqrestore(&emsgs_lock, flags);
228 n = copy_to_user(buf, mp, len);
230 return n == 0 ? len : -EFAULT;
236 static const struct file_operations aoe_fops = {
237 .write = aoechr_write,
240 .release = aoechr_rel,
241 .owner = THIS_MODULE,
249 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
251 printk(KERN_ERR "aoe: can't register char device\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);
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");