Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / block / aoe / aoechr.c
blobe86d2062a1641f36ff2bc0200d286f433e56fb27
1 /* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */
2 /*
3 * aoechr.c
4 * AoE character device driver
5 */
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>
15 #include "aoe.h"
17 enum {
18 //MINOR_STAT = 1, (moved to sysfs)
19 MINOR_ERR = 2,
20 MINOR_DISCOVER,
21 MINOR_INTERFACES,
22 MINOR_REVALIDATE,
23 MINOR_FLUSH,
24 MSGSZ = 2048,
25 NMSG = 100, /* message backlog to retain */
28 struct aoe_chardev {
29 ulong minor;
30 char name[32];
33 enum { EMFL_VALID = 1 };
35 struct ErrMsg {
36 short flags;
37 short len;
38 char *msg;
41 static DEFINE_MUTEX(aoechr_mutex);
42 static struct ErrMsg emsgs[NMSG];
43 static int emsgs_head_idx, emsgs_tail_idx;
44 static struct completion emsgs_comp;
45 static spinlock_t emsgs_lock;
46 static int nblocked_emsgs_readers;
47 static struct class *aoe_class;
48 static struct aoe_chardev chardevs[] = {
49 { MINOR_ERR, "err" },
50 { MINOR_DISCOVER, "discover" },
51 { MINOR_INTERFACES, "interfaces" },
52 { MINOR_REVALIDATE, "revalidate" },
53 { MINOR_FLUSH, "flush" },
56 static int
57 discover(void)
59 aoecmd_cfg(0xffff, 0xff);
60 return 0;
63 static int
64 interfaces(const char __user *str, size_t size)
66 if (set_aoe_iflist(str, size)) {
67 printk(KERN_ERR
68 "aoe: could not set interface list: too many interfaces\n");
69 return -EINVAL;
71 return 0;
74 static int
75 revalidate(const char __user *str, size_t size)
77 int major, minor, n;
78 ulong flags;
79 struct aoedev *d;
80 struct sk_buff *skb;
81 char buf[16];
83 if (size >= sizeof buf)
84 return -EINVAL;
85 buf[sizeof buf - 1] = '\0';
86 if (copy_from_user(buf, str, size))
87 return -EFAULT;
89 /* should be e%d.%d format */
90 n = sscanf(buf, "e%d.%d", &major, &minor);
91 if (n != 2) {
92 printk(KERN_ERR "aoe: invalid device specification\n");
93 return -EINVAL;
95 d = aoedev_by_aoeaddr(major, minor);
96 if (!d)
97 return -EINVAL;
98 spin_lock_irqsave(&d->lock, flags);
99 aoecmd_cleanslate(d);
100 loop:
101 skb = aoecmd_ata_id(d);
102 spin_unlock_irqrestore(&d->lock, flags);
103 /* try again if we are able to sleep a bit,
104 * otherwise give up this revalidation
106 if (!skb && !msleep_interruptible(200)) {
107 spin_lock_irqsave(&d->lock, flags);
108 goto loop;
110 if (skb) {
111 struct sk_buff_head queue;
112 __skb_queue_head_init(&queue);
113 __skb_queue_tail(&queue, skb);
114 aoenet_xmit(&queue);
116 aoecmd_cfg(major, minor);
117 return 0;
120 void
121 aoechr_error(char *msg)
123 struct ErrMsg *em;
124 char *mp;
125 ulong flags, n;
127 n = strlen(msg);
129 spin_lock_irqsave(&emsgs_lock, flags);
131 em = emsgs + emsgs_tail_idx;
132 if ((em->flags & EMFL_VALID)) {
133 bail: spin_unlock_irqrestore(&emsgs_lock, flags);
134 return;
137 mp = kmalloc(n, GFP_ATOMIC);
138 if (mp == NULL) {
139 printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
140 goto bail;
143 memcpy(mp, msg, n);
144 em->msg = mp;
145 em->flags |= EMFL_VALID;
146 em->len = n;
148 emsgs_tail_idx++;
149 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
151 spin_unlock_irqrestore(&emsgs_lock, flags);
153 if (nblocked_emsgs_readers)
154 complete(&emsgs_comp);
157 static ssize_t
158 aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
160 int ret = -EINVAL;
162 switch ((unsigned long) filp->private_data) {
163 default:
164 printk(KERN_INFO "aoe: can't write to that file.\n");
165 break;
166 case MINOR_DISCOVER:
167 ret = discover();
168 break;
169 case MINOR_INTERFACES:
170 ret = interfaces(buf, cnt);
171 break;
172 case MINOR_REVALIDATE:
173 ret = revalidate(buf, cnt);
174 break;
175 case MINOR_FLUSH:
176 ret = aoedev_flush(buf, cnt);
178 if (ret == 0)
179 ret = cnt;
180 return ret;
183 static int
184 aoechr_open(struct inode *inode, struct file *filp)
186 int n, i;
188 mutex_lock(&aoechr_mutex);
189 n = iminor(inode);
190 filp->private_data = (void *) (unsigned long) n;
192 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
193 if (chardevs[i].minor == n) {
194 mutex_unlock(&aoechr_mutex);
195 return 0;
197 mutex_unlock(&aoechr_mutex);
198 return -EINVAL;
201 static int
202 aoechr_rel(struct inode *inode, struct file *filp)
204 return 0;
207 static ssize_t
208 aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
210 unsigned long n;
211 char *mp;
212 struct ErrMsg *em;
213 ssize_t len;
214 ulong flags;
216 n = (unsigned long) filp->private_data;
217 if (n != MINOR_ERR)
218 return -EFAULT;
220 spin_lock_irqsave(&emsgs_lock, flags);
222 for (;;) {
223 em = emsgs + emsgs_head_idx;
224 if ((em->flags & EMFL_VALID) != 0)
225 break;
226 if (filp->f_flags & O_NDELAY) {
227 spin_unlock_irqrestore(&emsgs_lock, flags);
228 return -EAGAIN;
230 nblocked_emsgs_readers++;
232 spin_unlock_irqrestore(&emsgs_lock, flags);
234 n = wait_for_completion_interruptible(&emsgs_comp);
236 spin_lock_irqsave(&emsgs_lock, flags);
238 nblocked_emsgs_readers--;
240 if (n) {
241 spin_unlock_irqrestore(&emsgs_lock, flags);
242 return -ERESTARTSYS;
245 if (em->len > cnt) {
246 spin_unlock_irqrestore(&emsgs_lock, flags);
247 return -EAGAIN;
249 mp = em->msg;
250 len = em->len;
251 em->msg = NULL;
252 em->flags &= ~EMFL_VALID;
254 emsgs_head_idx++;
255 emsgs_head_idx %= ARRAY_SIZE(emsgs);
257 spin_unlock_irqrestore(&emsgs_lock, flags);
259 n = copy_to_user(buf, mp, len);
260 kfree(mp);
261 return n == 0 ? len : -EFAULT;
264 static const struct file_operations aoe_fops = {
265 .write = aoechr_write,
266 .read = aoechr_read,
267 .open = aoechr_open,
268 .release = aoechr_rel,
269 .owner = THIS_MODULE,
270 .llseek = noop_llseek,
273 static char *aoe_devnode(struct device *dev, umode_t *mode)
275 return kasprintf(GFP_KERNEL, "etherd/%s", dev_name(dev));
278 int __init
279 aoechr_init(void)
281 int n, i;
283 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
284 if (n < 0) {
285 printk(KERN_ERR "aoe: can't register char device\n");
286 return n;
288 init_completion(&emsgs_comp);
289 spin_lock_init(&emsgs_lock);
290 aoe_class = class_create(THIS_MODULE, "aoe");
291 if (IS_ERR(aoe_class)) {
292 unregister_chrdev(AOE_MAJOR, "aoechr");
293 return PTR_ERR(aoe_class);
295 aoe_class->devnode = aoe_devnode;
297 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
298 device_create(aoe_class, NULL,
299 MKDEV(AOE_MAJOR, chardevs[i].minor), NULL,
300 chardevs[i].name);
302 return 0;
305 void
306 aoechr_exit(void)
308 int i;
310 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
311 device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
312 class_destroy(aoe_class);
313 unregister_chrdev(AOE_MAJOR, "aoechr");