1 /* Copyright (c) 2007 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/smp_lock.h>
12 #include <linux/skbuff.h>
16 //MINOR_STAT = 1, (moved to sysfs)
23 NMSG
= 100, /* message backlog to retain */
31 enum { EMFL_VALID
= 1 };
39 static struct ErrMsg emsgs
[NMSG
];
40 static int emsgs_head_idx
, emsgs_tail_idx
;
41 static struct completion emsgs_comp
;
42 static spinlock_t emsgs_lock
;
43 static int nblocked_emsgs_readers
;
44 static struct class *aoe_class
;
45 static struct aoe_chardev chardevs
[] = {
47 { MINOR_DISCOVER
, "discover" },
48 { MINOR_INTERFACES
, "interfaces" },
49 { MINOR_REVALIDATE
, "revalidate" },
50 { MINOR_FLUSH
, "flush" },
56 aoecmd_cfg(0xffff, 0xff);
61 interfaces(const char __user
*str
, size_t size
)
63 if (set_aoe_iflist(str
, size
)) {
65 "aoe: could not set interface list: too many interfaces\n");
72 revalidate(const char __user
*str
, size_t size
)
80 if (size
>= sizeof buf
)
82 buf
[sizeof buf
- 1] = '\0';
83 if (copy_from_user(buf
, str
, size
))
86 /* should be e%d.%d format */
87 n
= sscanf(buf
, "e%d.%d", &major
, &minor
);
89 printk(KERN_ERR
"aoe: invalid device specification\n");
92 d
= aoedev_by_aoeaddr(major
, minor
);
95 spin_lock_irqsave(&d
->lock
, flags
);
98 skb
= aoecmd_ata_id(d
);
99 spin_unlock_irqrestore(&d
->lock
, flags
);
100 /* try again if we are able to sleep a bit,
101 * otherwise give up this revalidation
103 if (!skb
&& !msleep_interruptible(200)) {
104 spin_lock_irqsave(&d
->lock
, flags
);
108 struct sk_buff_head queue
;
109 __skb_queue_head_init(&queue
);
110 __skb_queue_tail(&queue
, skb
);
113 aoecmd_cfg(major
, minor
);
118 aoechr_error(char *msg
)
126 spin_lock_irqsave(&emsgs_lock
, flags
);
128 em
= emsgs
+ emsgs_tail_idx
;
129 if ((em
->flags
& EMFL_VALID
)) {
130 bail
: spin_unlock_irqrestore(&emsgs_lock
, flags
);
134 mp
= kmalloc(n
, GFP_ATOMIC
);
136 printk(KERN_ERR
"aoe: allocation failure, len=%ld\n", n
);
142 em
->flags
|= EMFL_VALID
;
146 emsgs_tail_idx
%= ARRAY_SIZE(emsgs
);
148 spin_unlock_irqrestore(&emsgs_lock
, flags
);
150 if (nblocked_emsgs_readers
)
151 complete(&emsgs_comp
);
155 aoechr_write(struct file
*filp
, const char __user
*buf
, size_t cnt
, loff_t
*offp
)
159 switch ((unsigned long) filp
->private_data
) {
161 printk(KERN_INFO
"aoe: can't write to that file.\n");
166 case MINOR_INTERFACES
:
167 ret
= interfaces(buf
, cnt
);
169 case MINOR_REVALIDATE
:
170 ret
= revalidate(buf
, cnt
);
173 ret
= aoedev_flush(buf
, cnt
);
181 aoechr_open(struct inode
*inode
, struct file
*filp
)
187 filp
->private_data
= (void *) (unsigned long) n
;
189 for (i
= 0; i
< ARRAY_SIZE(chardevs
); ++i
)
190 if (chardevs
[i
].minor
== n
) {
199 aoechr_rel(struct inode
*inode
, struct file
*filp
)
205 aoechr_read(struct file
*filp
, char __user
*buf
, size_t cnt
, loff_t
*off
)
213 n
= (unsigned long) filp
->private_data
;
217 spin_lock_irqsave(&emsgs_lock
, flags
);
220 em
= emsgs
+ emsgs_head_idx
;
221 if ((em
->flags
& EMFL_VALID
) != 0)
223 if (filp
->f_flags
& O_NDELAY
) {
224 spin_unlock_irqrestore(&emsgs_lock
, flags
);
227 nblocked_emsgs_readers
++;
229 spin_unlock_irqrestore(&emsgs_lock
, flags
);
231 n
= wait_for_completion_interruptible(&emsgs_comp
);
233 spin_lock_irqsave(&emsgs_lock
, flags
);
235 nblocked_emsgs_readers
--;
238 spin_unlock_irqrestore(&emsgs_lock
, flags
);
243 spin_unlock_irqrestore(&emsgs_lock
, flags
);
249 em
->flags
&= ~EMFL_VALID
;
252 emsgs_head_idx
%= ARRAY_SIZE(emsgs
);
254 spin_unlock_irqrestore(&emsgs_lock
, flags
);
256 n
= copy_to_user(buf
, mp
, len
);
258 return n
== 0 ? len
: -EFAULT
;
261 static const struct file_operations aoe_fops
= {
262 .write
= aoechr_write
,
265 .release
= aoechr_rel
,
266 .owner
= THIS_MODULE
,
269 static char *aoe_devnode(struct device
*dev
, mode_t
*mode
)
271 return kasprintf(GFP_KERNEL
, "etherd/%s", dev_name(dev
));
279 n
= register_chrdev(AOE_MAJOR
, "aoechr", &aoe_fops
);
281 printk(KERN_ERR
"aoe: can't register char device\n");
284 init_completion(&emsgs_comp
);
285 spin_lock_init(&emsgs_lock
);
286 aoe_class
= class_create(THIS_MODULE
, "aoe");
287 if (IS_ERR(aoe_class
)) {
288 unregister_chrdev(AOE_MAJOR
, "aoechr");
289 return PTR_ERR(aoe_class
);
291 aoe_class
->devnode
= aoe_devnode
;
293 for (i
= 0; i
< ARRAY_SIZE(chardevs
); ++i
)
294 device_create(aoe_class
, NULL
,
295 MKDEV(AOE_MAJOR
, chardevs
[i
].minor
), NULL
,
306 for (i
= 0; i
< ARRAY_SIZE(chardevs
); ++i
)
307 device_destroy(aoe_class
, MKDEV(AOE_MAJOR
, chardevs
[i
].minor
));
308 class_destroy(aoe_class
);
309 unregister_chrdev(AOE_MAJOR
, "aoechr");