Merge branch 'timers/urgent' of git://git.kernel.org/pub/scm/linux/kernel/git/tip...
[linux-2.6/mini2440.git] / drivers / block / aoe / aoechr.c
blob200efc4d2c1e756cd9a7182709bbfb2328a57140
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/smp_lock.h>
12 #include <linux/skbuff.h>
13 #include "aoe.h"
15 enum {
16 //MINOR_STAT = 1, (moved to sysfs)
17 MINOR_ERR = 2,
18 MINOR_DISCOVER,
19 MINOR_INTERFACES,
20 MINOR_REVALIDATE,
21 MINOR_FLUSH,
22 MSGSZ = 2048,
23 NMSG = 100, /* message backlog to retain */
26 struct aoe_chardev {
27 ulong minor;
28 char name[32];
31 enum { EMFL_VALID = 1 };
33 struct ErrMsg {
34 short flags;
35 short len;
36 char *msg;
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[] = {
46 { MINOR_ERR, "err" },
47 { MINOR_DISCOVER, "discover" },
48 { MINOR_INTERFACES, "interfaces" },
49 { MINOR_REVALIDATE, "revalidate" },
50 { MINOR_FLUSH, "flush" },
53 static int
54 discover(void)
56 aoecmd_cfg(0xffff, 0xff);
57 return 0;
60 static int
61 interfaces(const char __user *str, size_t size)
63 if (set_aoe_iflist(str, size)) {
64 printk(KERN_ERR
65 "aoe: could not set interface list: too many interfaces\n");
66 return -EINVAL;
68 return 0;
71 static int
72 revalidate(const char __user *str, size_t size)
74 int major, minor, n;
75 ulong flags;
76 struct aoedev *d;
77 struct sk_buff *skb;
78 char buf[16];
80 if (size >= sizeof buf)
81 return -EINVAL;
82 buf[sizeof buf - 1] = '\0';
83 if (copy_from_user(buf, str, size))
84 return -EFAULT;
86 /* should be e%d.%d format */
87 n = sscanf(buf, "e%d.%d", &major, &minor);
88 if (n != 2) {
89 printk(KERN_ERR "aoe: invalid device specification\n");
90 return -EINVAL;
92 d = aoedev_by_aoeaddr(major, minor);
93 if (!d)
94 return -EINVAL;
95 spin_lock_irqsave(&d->lock, flags);
96 aoecmd_cleanslate(d);
97 loop:
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);
105 goto loop;
107 if (skb) {
108 struct sk_buff_head queue;
109 __skb_queue_head_init(&queue);
110 __skb_queue_tail(&queue, skb);
111 aoenet_xmit(&queue);
113 aoecmd_cfg(major, minor);
114 return 0;
117 void
118 aoechr_error(char *msg)
120 struct ErrMsg *em;
121 char *mp;
122 ulong flags, n;
124 n = strlen(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);
131 return;
134 mp = kmalloc(n, GFP_ATOMIC);
135 if (mp == NULL) {
136 printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
137 goto bail;
140 memcpy(mp, msg, n);
141 em->msg = mp;
142 em->flags |= EMFL_VALID;
143 em->len = n;
145 emsgs_tail_idx++;
146 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
148 spin_unlock_irqrestore(&emsgs_lock, flags);
150 if (nblocked_emsgs_readers)
151 complete(&emsgs_comp);
154 static ssize_t
155 aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
157 int ret = -EINVAL;
159 switch ((unsigned long) filp->private_data) {
160 default:
161 printk(KERN_INFO "aoe: can't write to that file.\n");
162 break;
163 case MINOR_DISCOVER:
164 ret = discover();
165 break;
166 case MINOR_INTERFACES:
167 ret = interfaces(buf, cnt);
168 break;
169 case MINOR_REVALIDATE:
170 ret = revalidate(buf, cnt);
171 break;
172 case MINOR_FLUSH:
173 ret = aoedev_flush(buf, cnt);
175 if (ret == 0)
176 ret = cnt;
177 return ret;
180 static int
181 aoechr_open(struct inode *inode, struct file *filp)
183 int n, i;
185 lock_kernel();
186 n = iminor(inode);
187 filp->private_data = (void *) (unsigned long) n;
189 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
190 if (chardevs[i].minor == n) {
191 unlock_kernel();
192 return 0;
194 unlock_kernel();
195 return -EINVAL;
198 static int
199 aoechr_rel(struct inode *inode, struct file *filp)
201 return 0;
204 static ssize_t
205 aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
207 unsigned long n;
208 char *mp;
209 struct ErrMsg *em;
210 ssize_t len;
211 ulong flags;
213 n = (unsigned long) filp->private_data;
214 if (n != MINOR_ERR)
215 return -EFAULT;
217 spin_lock_irqsave(&emsgs_lock, flags);
219 for (;;) {
220 em = emsgs + emsgs_head_idx;
221 if ((em->flags & EMFL_VALID) != 0)
222 break;
223 if (filp->f_flags & O_NDELAY) {
224 spin_unlock_irqrestore(&emsgs_lock, flags);
225 return -EAGAIN;
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--;
237 if (n) {
238 spin_unlock_irqrestore(&emsgs_lock, flags);
239 return -ERESTARTSYS;
242 if (em->len > cnt) {
243 spin_unlock_irqrestore(&emsgs_lock, flags);
244 return -EAGAIN;
246 mp = em->msg;
247 len = em->len;
248 em->msg = NULL;
249 em->flags &= ~EMFL_VALID;
251 emsgs_head_idx++;
252 emsgs_head_idx %= ARRAY_SIZE(emsgs);
254 spin_unlock_irqrestore(&emsgs_lock, flags);
256 n = copy_to_user(buf, mp, len);
257 kfree(mp);
258 return n == 0 ? len : -EFAULT;
261 static const struct file_operations aoe_fops = {
262 .write = aoechr_write,
263 .read = aoechr_read,
264 .open = aoechr_open,
265 .release = aoechr_rel,
266 .owner = THIS_MODULE,
269 int __init
270 aoechr_init(void)
272 int n, i;
274 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
275 if (n < 0) {
276 printk(KERN_ERR "aoe: can't register char device\n");
277 return n;
279 init_completion(&emsgs_comp);
280 spin_lock_init(&emsgs_lock);
281 aoe_class = class_create(THIS_MODULE, "aoe");
282 if (IS_ERR(aoe_class)) {
283 unregister_chrdev(AOE_MAJOR, "aoechr");
284 return PTR_ERR(aoe_class);
286 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
287 device_create(aoe_class, NULL,
288 MKDEV(AOE_MAJOR, chardevs[i].minor), NULL,
289 chardevs[i].name);
291 return 0;
294 void
295 aoechr_exit(void)
297 int i;
299 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
300 device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
301 class_destroy(aoe_class);
302 unregister_chrdev(AOE_MAJOR, "aoechr");