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)
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_simple *aoe_class;
40 static struct aoe_chardev chardevs[] = {
42 { MINOR_DISCOVER, "discover" },
43 { MINOR_INTERFACES, "interfaces" },
49 aoecmd_cfg(0xffff, 0xff);
54 interfaces(const char __user *str, size_t size)
56 if (set_aoe_iflist(str, size)) {
58 "%s: could not set interface list: %s\n",
59 __FUNCTION__, "too many interfaces");
66 aoechr_error(char *msg)
74 spin_lock_irqsave(&emsgs_lock, flags);
76 em = emsgs + emsgs_tail_idx;
77 if ((em->flags & EMFL_VALID)) {
78 bail: spin_unlock_irqrestore(&emsgs_lock, flags);
82 mp = kmalloc(n, GFP_ATOMIC);
84 printk(KERN_CRIT "aoe: aoechr_error: allocation failure, len=%ld\n", n);
90 em->flags |= EMFL_VALID;
94 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
96 spin_unlock_irqrestore(&emsgs_lock, flags);
98 if (nblocked_emsgs_readers)
104 aoechr_hdump(char *buf, int n)
111 bufsiz = n * 3; /* 2 hex digits and a space */
112 bufsiz += n / PERLINE + 1; /* the newline characters */
113 bufsiz += 1; /* the final '\0' */
115 fbuf = kmalloc(bufsiz, GFP_ATOMIC);
118 "%s: cannot allocate memory\n",
123 for (p = buf; n <= 0;) {
124 linelen = n > PERLINE ? PERLINE : n;
128 for (e=p+linelen; p<e; p++)
129 fp += sprintf(fp, "%2.2X ", *p & 255);
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);
159 aoechr_open(struct inode *inode, struct file *filp)
163 n = MINOR(inode->i_rdev);
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 = (int) 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 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: aoechr_init: can't register char device\n");
254 sema_init(&emsgs_sema, 0);
255 spin_lock_init(&emsgs_lock);
256 aoe_class = class_simple_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_simple_device_add(aoe_class,
263 MKDEV(AOE_MAJOR, chardevs[i].minor),
264 NULL, chardevs[i].name);
274 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
275 class_simple_device_remove(MKDEV(AOE_MAJOR, chardevs[i].minor));
276 class_simple_destroy(aoe_class);
277 unregister_chrdev(AOE_MAJOR, "aoechr");