1 /* Copyright (c) 2012 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/completion.h>
10 #include <linux/delay.h>
11 #include <linux/slab.h>
12 #include <linux/mutex.h>
13 #include <linux/skbuff.h>
14 #include <linux/export.h>
18 //MINOR_STAT = 1, (moved to sysfs)
25 NMSG
= 100, /* message backlog to retain */
33 enum { EMFL_VALID
= 1 };
41 static DEFINE_MUTEX(aoechr_mutex
);
43 /* A ring buffer of error messages, to be read through
44 * "/dev/etherd/err". When no messages are present,
45 * readers will block waiting for messages to appear.
47 static struct ErrMsg emsgs
[NMSG
];
48 static int emsgs_head_idx
, emsgs_tail_idx
;
49 static struct completion emsgs_comp
;
50 static spinlock_t emsgs_lock
;
51 static int nblocked_emsgs_readers
;
53 static struct aoe_chardev chardevs
[] = {
55 { MINOR_DISCOVER
, "discover" },
56 { MINOR_INTERFACES
, "interfaces" },
57 { MINOR_REVALIDATE
, "revalidate" },
58 { MINOR_FLUSH
, "flush" },
61 static char *aoe_devnode(const struct device
*dev
, umode_t
*mode
)
63 return kasprintf(GFP_KERNEL
, "etherd/%s", dev_name(dev
));
66 static const struct class aoe_class
= {
68 .devnode
= aoe_devnode
,
74 aoecmd_cfg(0xffff, 0xff);
79 interfaces(const char __user
*str
, size_t size
)
81 if (set_aoe_iflist(str
, size
)) {
83 "aoe: could not set interface list: too many interfaces\n");
90 revalidate(const char __user
*str
, size_t size
)
98 if (size
>= sizeof buf
)
100 buf
[sizeof buf
- 1] = '\0';
101 if (copy_from_user(buf
, str
, size
))
104 n
= sscanf(buf
, "e%d.%d", &major
, &minor
);
106 pr_err("aoe: invalid device specification %s\n", buf
);
109 d
= aoedev_by_aoeaddr(major
, minor
, 0);
112 spin_lock_irqsave(&d
->lock
, flags
);
113 aoecmd_cleanslate(d
);
114 aoecmd_cfg(major
, minor
);
116 skb
= aoecmd_ata_id(d
);
117 spin_unlock_irqrestore(&d
->lock
, flags
);
118 /* try again if we are able to sleep a bit,
119 * otherwise give up this revalidation
121 if (!skb
&& !msleep_interruptible(250)) {
122 spin_lock_irqsave(&d
->lock
, flags
);
127 struct sk_buff_head queue
;
128 __skb_queue_head_init(&queue
);
129 __skb_queue_tail(&queue
, skb
);
136 aoechr_error(char *msg
)
144 spin_lock_irqsave(&emsgs_lock
, flags
);
146 em
= emsgs
+ emsgs_tail_idx
;
147 if ((em
->flags
& EMFL_VALID
)) {
148 bail
: spin_unlock_irqrestore(&emsgs_lock
, flags
);
152 mp
= kmemdup(msg
, n
, GFP_ATOMIC
);
157 em
->flags
|= EMFL_VALID
;
161 emsgs_tail_idx
%= ARRAY_SIZE(emsgs
);
163 spin_unlock_irqrestore(&emsgs_lock
, flags
);
165 if (nblocked_emsgs_readers
)
166 complete(&emsgs_comp
);
170 aoechr_write(struct file
*filp
, const char __user
*buf
, size_t cnt
, loff_t
*offp
)
174 switch ((unsigned long) filp
->private_data
) {
176 printk(KERN_INFO
"aoe: can't write to that file.\n");
181 case MINOR_INTERFACES
:
182 ret
= interfaces(buf
, cnt
);
184 case MINOR_REVALIDATE
:
185 ret
= revalidate(buf
, cnt
);
188 ret
= aoedev_flush(buf
, cnt
);
197 aoechr_open(struct inode
*inode
, struct file
*filp
)
201 mutex_lock(&aoechr_mutex
);
203 filp
->private_data
= (void *) (unsigned long) n
;
205 for (i
= 0; i
< ARRAY_SIZE(chardevs
); ++i
)
206 if (chardevs
[i
].minor
== n
) {
207 mutex_unlock(&aoechr_mutex
);
210 mutex_unlock(&aoechr_mutex
);
215 aoechr_rel(struct inode
*inode
, struct file
*filp
)
221 aoechr_read(struct file
*filp
, char __user
*buf
, size_t cnt
, loff_t
*off
)
229 n
= (unsigned long) filp
->private_data
;
233 spin_lock_irqsave(&emsgs_lock
, flags
);
236 em
= emsgs
+ emsgs_head_idx
;
237 if ((em
->flags
& EMFL_VALID
) != 0)
239 if (filp
->f_flags
& O_NDELAY
) {
240 spin_unlock_irqrestore(&emsgs_lock
, flags
);
243 nblocked_emsgs_readers
++;
245 spin_unlock_irqrestore(&emsgs_lock
, flags
);
247 n
= wait_for_completion_interruptible(&emsgs_comp
);
249 spin_lock_irqsave(&emsgs_lock
, flags
);
251 nblocked_emsgs_readers
--;
254 spin_unlock_irqrestore(&emsgs_lock
, flags
);
259 spin_unlock_irqrestore(&emsgs_lock
, flags
);
265 em
->flags
&= ~EMFL_VALID
;
268 emsgs_head_idx
%= ARRAY_SIZE(emsgs
);
270 spin_unlock_irqrestore(&emsgs_lock
, flags
);
272 n
= copy_to_user(buf
, mp
, len
);
274 return n
== 0 ? len
: -EFAULT
;
277 static const struct file_operations aoe_fops
= {
278 .write
= aoechr_write
,
281 .release
= aoechr_rel
,
282 .owner
= THIS_MODULE
,
283 .llseek
= noop_llseek
,
291 n
= register_chrdev(AOE_MAJOR
, "aoechr", &aoe_fops
);
293 printk(KERN_ERR
"aoe: can't register char device\n");
296 init_completion(&emsgs_comp
);
297 spin_lock_init(&emsgs_lock
);
298 n
= class_register(&aoe_class
);
300 unregister_chrdev(AOE_MAJOR
, "aoechr");
304 for (i
= 0; i
< ARRAY_SIZE(chardevs
); ++i
)
305 device_create(&aoe_class
, NULL
,
306 MKDEV(AOE_MAJOR
, chardevs
[i
].minor
), NULL
,
317 for (i
= 0; i
< ARRAY_SIZE(chardevs
); ++i
)
318 device_destroy(&aoe_class
, MKDEV(AOE_MAJOR
, chardevs
[i
].minor
));
319 class_unregister(&aoe_class
);
320 unregister_chrdev(AOE_MAJOR
, "aoechr");