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
;
52 static struct class *aoe_class
;
53 static struct aoe_chardev chardevs
[] = {
55 { MINOR_DISCOVER
, "discover" },
56 { MINOR_INTERFACES
, "interfaces" },
57 { MINOR_REVALIDATE
, "revalidate" },
58 { MINOR_FLUSH
, "flush" },
64 aoecmd_cfg(0xffff, 0xff);
69 interfaces(const char __user
*str
, size_t size
)
71 if (set_aoe_iflist(str
, size
)) {
73 "aoe: could not set interface list: too many interfaces\n");
80 revalidate(const char __user
*str
, size_t size
)
88 if (size
>= sizeof buf
)
90 buf
[sizeof buf
- 1] = '\0';
91 if (copy_from_user(buf
, str
, size
))
94 n
= sscanf(buf
, "e%d.%d", &major
, &minor
);
96 pr_err("aoe: invalid device specification %s\n", buf
);
99 d
= aoedev_by_aoeaddr(major
, minor
, 0);
102 spin_lock_irqsave(&d
->lock
, flags
);
103 aoecmd_cleanslate(d
);
104 aoecmd_cfg(major
, minor
);
106 skb
= aoecmd_ata_id(d
);
107 spin_unlock_irqrestore(&d
->lock
, flags
);
108 /* try again if we are able to sleep a bit,
109 * otherwise give up this revalidation
111 if (!skb
&& !msleep_interruptible(250)) {
112 spin_lock_irqsave(&d
->lock
, flags
);
117 struct sk_buff_head queue
;
118 __skb_queue_head_init(&queue
);
119 __skb_queue_tail(&queue
, skb
);
126 aoechr_error(char *msg
)
134 spin_lock_irqsave(&emsgs_lock
, flags
);
136 em
= emsgs
+ emsgs_tail_idx
;
137 if ((em
->flags
& EMFL_VALID
)) {
138 bail
: spin_unlock_irqrestore(&emsgs_lock
, flags
);
142 mp
= kmalloc(n
, GFP_ATOMIC
);
144 printk(KERN_ERR
"aoe: allocation failure, len=%ld\n", n
);
150 em
->flags
|= EMFL_VALID
;
154 emsgs_tail_idx
%= ARRAY_SIZE(emsgs
);
156 spin_unlock_irqrestore(&emsgs_lock
, flags
);
158 if (nblocked_emsgs_readers
)
159 complete(&emsgs_comp
);
163 aoechr_write(struct file
*filp
, const char __user
*buf
, size_t cnt
, loff_t
*offp
)
167 switch ((unsigned long) filp
->private_data
) {
169 printk(KERN_INFO
"aoe: can't write to that file.\n");
174 case MINOR_INTERFACES
:
175 ret
= interfaces(buf
, cnt
);
177 case MINOR_REVALIDATE
:
178 ret
= revalidate(buf
, cnt
);
181 ret
= aoedev_flush(buf
, cnt
);
190 aoechr_open(struct inode
*inode
, struct file
*filp
)
194 mutex_lock(&aoechr_mutex
);
196 filp
->private_data
= (void *) (unsigned long) n
;
198 for (i
= 0; i
< ARRAY_SIZE(chardevs
); ++i
)
199 if (chardevs
[i
].minor
== n
) {
200 mutex_unlock(&aoechr_mutex
);
203 mutex_unlock(&aoechr_mutex
);
208 aoechr_rel(struct inode
*inode
, struct file
*filp
)
214 aoechr_read(struct file
*filp
, char __user
*buf
, size_t cnt
, loff_t
*off
)
222 n
= (unsigned long) filp
->private_data
;
226 spin_lock_irqsave(&emsgs_lock
, flags
);
229 em
= emsgs
+ emsgs_head_idx
;
230 if ((em
->flags
& EMFL_VALID
) != 0)
232 if (filp
->f_flags
& O_NDELAY
) {
233 spin_unlock_irqrestore(&emsgs_lock
, flags
);
236 nblocked_emsgs_readers
++;
238 spin_unlock_irqrestore(&emsgs_lock
, flags
);
240 n
= wait_for_completion_interruptible(&emsgs_comp
);
242 spin_lock_irqsave(&emsgs_lock
, flags
);
244 nblocked_emsgs_readers
--;
247 spin_unlock_irqrestore(&emsgs_lock
, flags
);
252 spin_unlock_irqrestore(&emsgs_lock
, flags
);
258 em
->flags
&= ~EMFL_VALID
;
261 emsgs_head_idx
%= ARRAY_SIZE(emsgs
);
263 spin_unlock_irqrestore(&emsgs_lock
, flags
);
265 n
= copy_to_user(buf
, mp
, len
);
267 return n
== 0 ? len
: -EFAULT
;
270 static const struct file_operations aoe_fops
= {
271 .write
= aoechr_write
,
274 .release
= aoechr_rel
,
275 .owner
= THIS_MODULE
,
276 .llseek
= noop_llseek
,
279 static char *aoe_devnode(struct device
*dev
, umode_t
*mode
)
281 return kasprintf(GFP_KERNEL
, "etherd/%s", dev_name(dev
));
289 n
= register_chrdev(AOE_MAJOR
, "aoechr", &aoe_fops
);
291 printk(KERN_ERR
"aoe: can't register char device\n");
294 init_completion(&emsgs_comp
);
295 spin_lock_init(&emsgs_lock
);
296 aoe_class
= class_create(THIS_MODULE
, "aoe");
297 if (IS_ERR(aoe_class
)) {
298 unregister_chrdev(AOE_MAJOR
, "aoechr");
299 return PTR_ERR(aoe_class
);
301 aoe_class
->devnode
= aoe_devnode
;
303 for (i
= 0; i
< ARRAY_SIZE(chardevs
); ++i
)
304 device_create(aoe_class
, NULL
,
305 MKDEV(AOE_MAJOR
, chardevs
[i
].minor
), NULL
,
316 for (i
= 0; i
< ARRAY_SIZE(chardevs
); ++i
)
317 device_destroy(aoe_class
, MKDEV(AOE_MAJOR
, chardevs
[i
].minor
));
318 class_destroy(aoe_class
);
319 unregister_chrdev(AOE_MAJOR
, "aoechr");