aoe: update internal version number to 49
[linux-2.6-block.git] / drivers / block / aoe / aoechr.c
CommitLineData
52e112b3 1/* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */
1da177e4
LT
2/*
3 * aoechr.c
4 * AoE character device driver
5 */
6
7#include <linux/hdreg.h>
8#include <linux/blkdev.h>
24879a8e 9#include <linux/completion.h>
68e0d42f 10#include <linux/delay.h>
5a0e3ad6 11#include <linux/slab.h>
2a48fc0a 12#include <linux/mutex.h>
e9bb8fb0 13#include <linux/skbuff.h>
d5decd3b 14#include <linux/export.h>
1da177e4
LT
15#include "aoe.h"
16
17enum {
18 //MINOR_STAT = 1, (moved to sysfs)
19 MINOR_ERR = 2,
20 MINOR_DISCOVER,
21 MINOR_INTERFACES,
3ae1c24e 22 MINOR_REVALIDATE,
262bf541 23 MINOR_FLUSH,
1da177e4 24 MSGSZ = 2048,
1da177e4
LT
25 NMSG = 100, /* message backlog to retain */
26};
27
28struct aoe_chardev {
29 ulong minor;
30 char name[32];
31};
32
33enum { EMFL_VALID = 1 };
34
35struct ErrMsg {
36 short flags;
37 short len;
38 char *msg;
39};
40
2a48fc0a 41static DEFINE_MUTEX(aoechr_mutex);
1da177e4
LT
42static struct ErrMsg emsgs[NMSG];
43static int emsgs_head_idx, emsgs_tail_idx;
24879a8e 44static struct completion emsgs_comp;
1da177e4
LT
45static spinlock_t emsgs_lock;
46static int nblocked_emsgs_readers;
deb36970 47static struct class *aoe_class;
1da177e4
LT
48static struct aoe_chardev chardevs[] = {
49 { MINOR_ERR, "err" },
50 { MINOR_DISCOVER, "discover" },
51 { MINOR_INTERFACES, "interfaces" },
3ae1c24e 52 { MINOR_REVALIDATE, "revalidate" },
262bf541 53 { MINOR_FLUSH, "flush" },
1da177e4
LT
54};
55
56static int
57discover(void)
58{
59 aoecmd_cfg(0xffff, 0xff);
60 return 0;
61}
62
63static int
64interfaces(const char __user *str, size_t size)
65{
66 if (set_aoe_iflist(str, size)) {
a12c93f0
EC
67 printk(KERN_ERR
68 "aoe: could not set interface list: too many interfaces\n");
1da177e4
LT
69 return -EINVAL;
70 }
71 return 0;
72}
73
3ae1c24e
EC
74static int
75revalidate(const char __user *str, size_t size)
76{
77 int major, minor, n;
78 ulong flags;
79 struct aoedev *d;
68e0d42f 80 struct sk_buff *skb;
3ae1c24e
EC
81 char buf[16];
82
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;
88
3ae1c24e
EC
89 n = sscanf(buf, "e%d.%d", &major, &minor);
90 if (n != 2) {
896831f5 91 pr_err("aoe: invalid device specification %s\n", buf);
3ae1c24e
EC
92 return -EINVAL;
93 }
94 d = aoedev_by_aoeaddr(major, minor);
95 if (!d)
96 return -EINVAL;
3ae1c24e 97 spin_lock_irqsave(&d->lock, flags);
68e0d42f 98 aoecmd_cleanslate(d);
25f4d75e 99 aoecmd_cfg(major, minor);
68e0d42f
EC
100loop:
101 skb = aoecmd_ata_id(d);
3ae1c24e 102 spin_unlock_irqrestore(&d->lock, flags);
68e0d42f
EC
103 /* try again if we are able to sleep a bit,
104 * otherwise give up this revalidation
105 */
25f4d75e 106 if (!skb && !msleep_interruptible(250)) {
68e0d42f
EC
107 spin_lock_irqsave(&d->lock, flags);
108 goto loop;
109 }
69cf2d85 110 aoedev_put(d);
e9bb8fb0
DM
111 if (skb) {
112 struct sk_buff_head queue;
113 __skb_queue_head_init(&queue);
114 __skb_queue_tail(&queue, skb);
115 aoenet_xmit(&queue);
116 }
3ae1c24e
EC
117 return 0;
118}
119
1da177e4
LT
120void
121aoechr_error(char *msg)
122{
123 struct ErrMsg *em;
124 char *mp;
125 ulong flags, n;
126
127 n = strlen(msg);
128
129 spin_lock_irqsave(&emsgs_lock, flags);
130
131 em = emsgs + emsgs_tail_idx;
132 if ((em->flags & EMFL_VALID)) {
133bail: spin_unlock_irqrestore(&emsgs_lock, flags);
134 return;
135 }
136
137 mp = kmalloc(n, GFP_ATOMIC);
138 if (mp == NULL) {
a12c93f0 139 printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
1da177e4
LT
140 goto bail;
141 }
142
143 memcpy(mp, msg, n);
144 em->msg = mp;
145 em->flags |= EMFL_VALID;
146 em->len = n;
147
148 emsgs_tail_idx++;
149 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
150
151 spin_unlock_irqrestore(&emsgs_lock, flags);
152
153 if (nblocked_emsgs_readers)
24879a8e 154 complete(&emsgs_comp);
1da177e4
LT
155}
156
157static ssize_t
158aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
159{
160 int ret = -EINVAL;
161
162 switch ((unsigned long) filp->private_data) {
163 default:
a12c93f0 164 printk(KERN_INFO "aoe: can't write to that file.\n");
1da177e4
LT
165 break;
166 case MINOR_DISCOVER:
167 ret = discover();
168 break;
169 case MINOR_INTERFACES:
170 ret = interfaces(buf, cnt);
171 break;
3ae1c24e
EC
172 case MINOR_REVALIDATE:
173 ret = revalidate(buf, cnt);
262bf541
EC
174 break;
175 case MINOR_FLUSH:
176 ret = aoedev_flush(buf, cnt);
b21faa25 177 break;
1da177e4
LT
178 }
179 if (ret == 0)
180 ret = cnt;
181 return ret;
182}
183
184static int
185aoechr_open(struct inode *inode, struct file *filp)
186{
187 int n, i;
188
2a48fc0a 189 mutex_lock(&aoechr_mutex);
2017b376 190 n = iminor(inode);
1da177e4
LT
191 filp->private_data = (void *) (unsigned long) n;
192
193 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
579174a5 194 if (chardevs[i].minor == n) {
2a48fc0a 195 mutex_unlock(&aoechr_mutex);
1da177e4 196 return 0;
579174a5 197 }
2a48fc0a 198 mutex_unlock(&aoechr_mutex);
1da177e4
LT
199 return -EINVAL;
200}
201
202static int
203aoechr_rel(struct inode *inode, struct file *filp)
204{
205 return 0;
206}
207
208static ssize_t
209aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
210{
211 unsigned long n;
212 char *mp;
213 struct ErrMsg *em;
214 ssize_t len;
215 ulong flags;
216
217 n = (unsigned long) filp->private_data;
cf446f0d
EC
218 if (n != MINOR_ERR)
219 return -EFAULT;
220
221 spin_lock_irqsave(&emsgs_lock, flags);
1da177e4 222
cf446f0d
EC
223 for (;;) {
224 em = emsgs + emsgs_head_idx;
225 if ((em->flags & EMFL_VALID) != 0)
226 break;
227 if (filp->f_flags & O_NDELAY) {
1da177e4 228 spin_unlock_irqrestore(&emsgs_lock, flags);
cf446f0d
EC
229 return -EAGAIN;
230 }
231 nblocked_emsgs_readers++;
232
233 spin_unlock_irqrestore(&emsgs_lock, flags);
1da177e4 234
24879a8e 235 n = wait_for_completion_interruptible(&emsgs_comp);
1da177e4 236
cf446f0d 237 spin_lock_irqsave(&emsgs_lock, flags);
1da177e4 238
cf446f0d 239 nblocked_emsgs_readers--;
1da177e4 240
cf446f0d 241 if (n) {
1da177e4 242 spin_unlock_irqrestore(&emsgs_lock, flags);
cf446f0d 243 return -ERESTARTSYS;
1da177e4 244 }
cf446f0d
EC
245 }
246 if (em->len > cnt) {
247 spin_unlock_irqrestore(&emsgs_lock, flags);
248 return -EAGAIN;
249 }
250 mp = em->msg;
251 len = em->len;
252 em->msg = NULL;
253 em->flags &= ~EMFL_VALID;
1da177e4 254
cf446f0d
EC
255 emsgs_head_idx++;
256 emsgs_head_idx %= ARRAY_SIZE(emsgs);
1da177e4 257
cf446f0d 258 spin_unlock_irqrestore(&emsgs_lock, flags);
1da177e4 259
cf446f0d
EC
260 n = copy_to_user(buf, mp, len);
261 kfree(mp);
262 return n == 0 ? len : -EFAULT;
1da177e4
LT
263}
264
2b8693c0 265static const struct file_operations aoe_fops = {
1da177e4
LT
266 .write = aoechr_write,
267 .read = aoechr_read,
268 .open = aoechr_open,
269 .release = aoechr_rel,
270 .owner = THIS_MODULE,
6038f373 271 .llseek = noop_llseek,
1da177e4
LT
272};
273
2c9ede55 274static char *aoe_devnode(struct device *dev, umode_t *mode)
1ce8a0d3
KS
275{
276 return kasprintf(GFP_KERNEL, "etherd/%s", dev_name(dev));
277}
278
1da177e4
LT
279int __init
280aoechr_init(void)
281{
282 int n, i;
283
284 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
285 if (n < 0) {
a12c93f0 286 printk(KERN_ERR "aoe: can't register char device\n");
1da177e4
LT
287 return n;
288 }
24879a8e 289 init_completion(&emsgs_comp);
1da177e4 290 spin_lock_init(&emsgs_lock);
deb36970 291 aoe_class = class_create(THIS_MODULE, "aoe");
1da177e4
LT
292 if (IS_ERR(aoe_class)) {
293 unregister_chrdev(AOE_MAJOR, "aoechr");
294 return PTR_ERR(aoe_class);
295 }
e454cea2 296 aoe_class->devnode = aoe_devnode;
1ce8a0d3 297
1da177e4 298 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
1ff9f542
GKH
299 device_create(aoe_class, NULL,
300 MKDEV(AOE_MAJOR, chardevs[i].minor), NULL,
301 chardevs[i].name);
1da177e4
LT
302
303 return 0;
304}
305
306void
307aoechr_exit(void)
308{
309 int i;
310
311 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
7ea7ed01 312 device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
deb36970 313 class_destroy(aoe_class);
1da177e4
LT
314 unregister_chrdev(AOE_MAJOR, "aoechr");
315}
316