blob: c04440cd6a32d5bc5031aed5ec4fe50c4d50dd67 [file] [log] [blame]
Ed L. Cashin52e112b2008-02-08 04:20:09 -08001/* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * aoechr.c
4 * AoE character device driver
5 */
6
7#include <linux/hdreg.h>
8#include <linux/blkdev.h>
Ed L. Cashin68e0d422008-02-08 04:20:00 -08009#include <linux/delay.h>
Jonathan Corbet579174a2008-05-15 10:03:09 -060010#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "aoe.h"
12
13enum {
14 //MINOR_STAT = 1, (moved to sysfs)
15 MINOR_ERR = 2,
16 MINOR_DISCOVER,
17 MINOR_INTERFACES,
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050018 MINOR_REVALIDATE,
Ed L. Cashin262bf542008-02-08 04:20:03 -080019 MINOR_FLUSH,
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 MSGSZ = 2048,
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 NMSG = 100, /* message backlog to retain */
22};
23
24struct aoe_chardev {
25 ulong minor;
26 char name[32];
27};
28
29enum { EMFL_VALID = 1 };
30
31struct ErrMsg {
32 short flags;
33 short len;
34 char *msg;
35};
36
37static struct ErrMsg emsgs[NMSG];
38static int emsgs_head_idx, emsgs_tail_idx;
39static struct semaphore emsgs_sema;
40static spinlock_t emsgs_lock;
41static int nblocked_emsgs_readers;
gregkh@suse.dedeb36972005-03-23 09:52:10 -080042static struct class *aoe_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static struct aoe_chardev chardevs[] = {
44 { MINOR_ERR, "err" },
45 { MINOR_DISCOVER, "discover" },
46 { MINOR_INTERFACES, "interfaces" },
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050047 { MINOR_REVALIDATE, "revalidate" },
Ed L. Cashin262bf542008-02-08 04:20:03 -080048 { MINOR_FLUSH, "flush" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
51static int
52discover(void)
53{
54 aoecmd_cfg(0xffff, 0xff);
55 return 0;
56}
57
58static int
59interfaces(const char __user *str, size_t size)
60{
61 if (set_aoe_iflist(str, size)) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -040062 printk(KERN_ERR
63 "aoe: could not set interface list: too many interfaces\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return -EINVAL;
65 }
66 return 0;
67}
68
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050069static int
70revalidate(const char __user *str, size_t size)
71{
72 int major, minor, n;
73 ulong flags;
74 struct aoedev *d;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080075 struct sk_buff *skb;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050076 char buf[16];
77
78 if (size >= sizeof buf)
79 return -EINVAL;
80 buf[sizeof buf - 1] = '\0';
81 if (copy_from_user(buf, str, size))
82 return -EFAULT;
83
84 /* should be e%d.%d format */
85 n = sscanf(buf, "e%d.%d", &major, &minor);
86 if (n != 2) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -040087 printk(KERN_ERR "aoe: invalid device specification\n");
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050088 return -EINVAL;
89 }
90 d = aoedev_by_aoeaddr(major, minor);
91 if (!d)
92 return -EINVAL;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050093 spin_lock_irqsave(&d->lock, flags);
Ed L. Cashin68e0d422008-02-08 04:20:00 -080094 aoecmd_cleanslate(d);
95loop:
96 skb = aoecmd_ata_id(d);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050097 spin_unlock_irqrestore(&d->lock, flags);
Ed L. Cashin68e0d422008-02-08 04:20:00 -080098 /* try again if we are able to sleep a bit,
99 * otherwise give up this revalidation
100 */
101 if (!skb && !msleep_interruptible(200)) {
102 spin_lock_irqsave(&d->lock, flags);
103 goto loop;
104 }
105 aoenet_xmit(skb);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500106 aoecmd_cfg(major, minor);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500107 return 0;
108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110void
111aoechr_error(char *msg)
112{
113 struct ErrMsg *em;
114 char *mp;
115 ulong flags, n;
116
117 n = strlen(msg);
118
119 spin_lock_irqsave(&emsgs_lock, flags);
120
121 em = emsgs + emsgs_tail_idx;
122 if ((em->flags & EMFL_VALID)) {
123bail: spin_unlock_irqrestore(&emsgs_lock, flags);
124 return;
125 }
126
127 mp = kmalloc(n, GFP_ATOMIC);
128 if (mp == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400129 printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 goto bail;
131 }
132
133 memcpy(mp, msg, n);
134 em->msg = mp;
135 em->flags |= EMFL_VALID;
136 em->len = n;
137
138 emsgs_tail_idx++;
139 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
140
141 spin_unlock_irqrestore(&emsgs_lock, flags);
142
143 if (nblocked_emsgs_readers)
144 up(&emsgs_sema);
145}
146
147static ssize_t
148aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
149{
150 int ret = -EINVAL;
151
152 switch ((unsigned long) filp->private_data) {
153 default:
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400154 printk(KERN_INFO "aoe: can't write to that file.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 break;
156 case MINOR_DISCOVER:
157 ret = discover();
158 break;
159 case MINOR_INTERFACES:
160 ret = interfaces(buf, cnt);
161 break;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500162 case MINOR_REVALIDATE:
163 ret = revalidate(buf, cnt);
Ed L. Cashin262bf542008-02-08 04:20:03 -0800164 break;
165 case MINOR_FLUSH:
166 ret = aoedev_flush(buf, cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168 if (ret == 0)
169 ret = cnt;
170 return ret;
171}
172
173static int
174aoechr_open(struct inode *inode, struct file *filp)
175{
176 int n, i;
177
Jonathan Corbet579174a2008-05-15 10:03:09 -0600178 lock_kernel();
Eric Sesterhenn2017b372006-07-10 04:45:32 -0700179 n = iminor(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 filp->private_data = (void *) (unsigned long) n;
181
182 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Jonathan Corbet579174a2008-05-15 10:03:09 -0600183 if (chardevs[i].minor == n) {
184 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return 0;
Jonathan Corbet579174a2008-05-15 10:03:09 -0600186 }
187 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -EINVAL;
189}
190
191static int
192aoechr_rel(struct inode *inode, struct file *filp)
193{
194 return 0;
195}
196
197static ssize_t
198aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
199{
200 unsigned long n;
201 char *mp;
202 struct ErrMsg *em;
203 ssize_t len;
204 ulong flags;
205
206 n = (unsigned long) filp->private_data;
Ed L. Cashincf446f02008-02-08 04:20:03 -0800207 if (n != MINOR_ERR)
208 return -EFAULT;
209
210 spin_lock_irqsave(&emsgs_lock, flags);
211
212 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 em = emsgs + emsgs_head_idx;
Ed L. Cashincf446f02008-02-08 04:20:03 -0800214 if ((em->flags & EMFL_VALID) != 0)
215 break;
216 if (filp->f_flags & O_NDELAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 spin_unlock_irqrestore(&emsgs_lock, flags);
218 return -EAGAIN;
219 }
Ed L. Cashincf446f02008-02-08 04:20:03 -0800220 nblocked_emsgs_readers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 spin_unlock_irqrestore(&emsgs_lock, flags);
223
Ed L. Cashincf446f02008-02-08 04:20:03 -0800224 n = down_interruptible(&emsgs_sema);
225
226 spin_lock_irqsave(&emsgs_lock, flags);
227
228 nblocked_emsgs_readers--;
229
230 if (n) {
231 spin_unlock_irqrestore(&emsgs_lock, flags);
232 return -ERESTARTSYS;
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
Ed L. Cashincf446f02008-02-08 04:20:03 -0800235 if (em->len > cnt) {
236 spin_unlock_irqrestore(&emsgs_lock, flags);
237 return -EAGAIN;
238 }
239 mp = em->msg;
240 len = em->len;
241 em->msg = NULL;
242 em->flags &= ~EMFL_VALID;
243
244 emsgs_head_idx++;
245 emsgs_head_idx %= ARRAY_SIZE(emsgs);
246
247 spin_unlock_irqrestore(&emsgs_lock, flags);
248
249 n = copy_to_user(buf, mp, len);
250 kfree(mp);
251 return n == 0 ? len : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800254static const struct file_operations aoe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 .write = aoechr_write,
256 .read = aoechr_read,
257 .open = aoechr_open,
258 .release = aoechr_rel,
259 .owner = THIS_MODULE,
260};
261
262int __init
263aoechr_init(void)
264{
265 int n, i;
266
267 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
268 if (n < 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400269 printk(KERN_ERR "aoe: can't register char device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return n;
271 }
272 sema_init(&emsgs_sema, 0);
273 spin_lock_init(&emsgs_lock);
gregkh@suse.dedeb36972005-03-23 09:52:10 -0800274 aoe_class = class_create(THIS_MODULE, "aoe");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (IS_ERR(aoe_class)) {
276 unregister_chrdev(AOE_MAJOR, "aoechr");
277 return PTR_ERR(aoe_class);
278 }
279 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Greg Kroah-Hartmanf79f0602008-05-21 12:52:33 -0700280 device_create_drvdata(aoe_class, NULL,
281 MKDEV(AOE_MAJOR, chardevs[i].minor),
282 NULL, chardevs[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 return 0;
285}
286
287void
288aoechr_exit(void)
289{
290 int i;
291
292 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Tony Jones7ea7ed02007-09-25 02:03:03 +0200293 device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
gregkh@suse.dedeb36972005-03-23 09:52:10 -0800294 class_destroy(aoe_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 unregister_chrdev(AOE_MAJOR, "aoechr");
296}
297