1 /* Copyright (c) 2004 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)
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 "%s: could not set interface list: %s\n",
61 __FUNCTION__, "too many interfaces");
68 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: %s: invalid device specification\n",
88 d = aoedev_by_aoeaddr(major, minor);
92 spin_lock_irqsave(&d->lock, flags);
93 d->flags |= DEVFL_PAUSE;
94 spin_unlock_irqrestore(&d->lock, flags);
95 aoecmd_cfg(major, minor);
101 aoechr_error(char *msg)
109 spin_lock_irqsave(&emsgs_lock, flags);
111 em = emsgs + emsgs_tail_idx;
112 if ((em->flags & EMFL_VALID)) {
113 bail: spin_unlock_irqrestore(&emsgs_lock, flags);
117 mp = kmalloc(n, GFP_ATOMIC);
119 printk(KERN_CRIT "aoe: aoechr_error: allocation failure, len=%ld\n", n);
125 em->flags |= EMFL_VALID;
129 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
131 spin_unlock_irqrestore(&emsgs_lock, flags);
133 if (nblocked_emsgs_readers)
138 aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
142 switch ((unsigned long) filp->private_data) {
144 printk(KERN_INFO "aoe: aoechr_write: can't write to that file.\n");
149 case MINOR_INTERFACES:
150 ret = interfaces(buf, cnt);
152 case MINOR_REVALIDATE:
153 ret = revalidate(buf, cnt);
161 aoechr_open(struct inode *inode, struct file *filp)
166 filp->private_data = (void *) (unsigned long) n;
168 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
169 if (chardevs[i].minor == n)
175 aoechr_rel(struct inode *inode, struct file *filp)
181 aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
189 n = (unsigned long) filp->private_data;
192 spin_lock_irqsave(&emsgs_lock, flags);
194 em = emsgs + emsgs_head_idx;
195 if ((em->flags & EMFL_VALID) == 0) {
196 if (filp->f_flags & O_NDELAY) {
197 spin_unlock_irqrestore(&emsgs_lock, flags);
200 nblocked_emsgs_readers++;
202 spin_unlock_irqrestore(&emsgs_lock, flags);
204 n = down_interruptible(&emsgs_sema);
206 spin_lock_irqsave(&emsgs_lock, flags);
208 nblocked_emsgs_readers--;
211 spin_unlock_irqrestore(&emsgs_lock, flags);
217 spin_unlock_irqrestore(&emsgs_lock, flags);
223 em->flags &= ~EMFL_VALID;
226 emsgs_head_idx %= ARRAY_SIZE(emsgs);
228 spin_unlock_irqrestore(&emsgs_lock, flags);
230 n = copy_to_user(buf, mp, len);
232 return n == 0 ? len : -EFAULT;
238 static struct file_operations aoe_fops = {
239 .write = aoechr_write,
242 .release = aoechr_rel,
243 .owner = THIS_MODULE,
251 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
253 printk(KERN_ERR "aoe: aoechr_init: can't register char device\n");
256 sema_init(&emsgs_sema, 0);
257 spin_lock_init(&emsgs_lock);
258 aoe_class = class_create(THIS_MODULE, "aoe");
259 if (IS_ERR(aoe_class)) {
260 unregister_chrdev(AOE_MAJOR, "aoechr");
261 return PTR_ERR(aoe_class);
263 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
264 class_device_create(aoe_class, NULL,
265 MKDEV(AOE_MAJOR, chardevs[i].minor),
266 NULL, chardevs[i].name);
276 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
277 class_device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
278 class_destroy(aoe_class);
279 unregister_chrdev(AOE_MAJOR, "aoechr");