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