blob: 19888354188f7f4c4afb0f1c37050cc1d06805fc [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>
Matthias Kaehlcke24879a82008-07-25 01:48:38 -07009#include <linux/completion.h>
Ed L. Cashin68e0d422008-02-08 04:20:00 -080010#include <linux/delay.h>
Jonathan Corbet579174a2008-05-15 10:03:09 -060011#include <linux/smp_lock.h>
David S. Millere9bb8fb2008-09-21 22:36:49 -070012#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "aoe.h"
14
15enum {
16 //MINOR_STAT = 1, (moved to sysfs)
17 MINOR_ERR = 2,
18 MINOR_DISCOVER,
19 MINOR_INTERFACES,
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050020 MINOR_REVALIDATE,
Ed L. Cashin262bf542008-02-08 04:20:03 -080021 MINOR_FLUSH,
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 MSGSZ = 2048,
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 NMSG = 100, /* message backlog to retain */
24};
25
26struct aoe_chardev {
27 ulong minor;
28 char name[32];
29};
30
31enum { EMFL_VALID = 1 };
32
33struct ErrMsg {
34 short flags;
35 short len;
36 char *msg;
37};
38
39static struct ErrMsg emsgs[NMSG];
40static int emsgs_head_idx, emsgs_tail_idx;
Matthias Kaehlcke24879a82008-07-25 01:48:38 -070041static struct completion emsgs_comp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static spinlock_t emsgs_lock;
43static int nblocked_emsgs_readers;
gregkh@suse.dedeb36972005-03-23 09:52:10 -080044static struct class *aoe_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static struct aoe_chardev chardevs[] = {
46 { MINOR_ERR, "err" },
47 { MINOR_DISCOVER, "discover" },
48 { MINOR_INTERFACES, "interfaces" },
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050049 { MINOR_REVALIDATE, "revalidate" },
Ed L. Cashin262bf542008-02-08 04:20:03 -080050 { MINOR_FLUSH, "flush" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
53static int
54discover(void)
55{
56 aoecmd_cfg(0xffff, 0xff);
57 return 0;
58}
59
60static int
61interfaces(const char __user *str, size_t size)
62{
63 if (set_aoe_iflist(str, size)) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -040064 printk(KERN_ERR
65 "aoe: could not set interface list: too many interfaces\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return -EINVAL;
67 }
68 return 0;
69}
70
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050071static int
72revalidate(const char __user *str, size_t size)
73{
74 int major, minor, n;
75 ulong flags;
76 struct aoedev *d;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080077 struct sk_buff *skb;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050078 char buf[16];
79
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;
85
86 /* should be e%d.%d format */
87 n = sscanf(buf, "e%d.%d", &major, &minor);
88 if (n != 2) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -040089 printk(KERN_ERR "aoe: invalid device specification\n");
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050090 return -EINVAL;
91 }
92 d = aoedev_by_aoeaddr(major, minor);
93 if (!d)
94 return -EINVAL;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050095 spin_lock_irqsave(&d->lock, flags);
Ed L. Cashin68e0d422008-02-08 04:20:00 -080096 aoecmd_cleanslate(d);
97loop:
98 skb = aoecmd_ata_id(d);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050099 spin_unlock_irqrestore(&d->lock, flags);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800100 /* try again if we are able to sleep a bit,
101 * otherwise give up this revalidation
102 */
103 if (!skb && !msleep_interruptible(200)) {
104 spin_lock_irqsave(&d->lock, flags);
105 goto loop;
106 }
David S. Millere9bb8fb2008-09-21 22:36:49 -0700107 if (skb) {
108 struct sk_buff_head queue;
109 __skb_queue_head_init(&queue);
110 __skb_queue_tail(&queue, skb);
111 aoenet_xmit(&queue);
112 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500113 aoecmd_cfg(major, minor);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500114 return 0;
115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117void
118aoechr_error(char *msg)
119{
120 struct ErrMsg *em;
121 char *mp;
122 ulong flags, n;
123
124 n = strlen(msg);
125
126 spin_lock_irqsave(&emsgs_lock, flags);
127
128 em = emsgs + emsgs_tail_idx;
129 if ((em->flags & EMFL_VALID)) {
130bail: spin_unlock_irqrestore(&emsgs_lock, flags);
131 return;
132 }
133
134 mp = kmalloc(n, GFP_ATOMIC);
135 if (mp == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400136 printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 goto bail;
138 }
139
140 memcpy(mp, msg, n);
141 em->msg = mp;
142 em->flags |= EMFL_VALID;
143 em->len = n;
144
145 emsgs_tail_idx++;
146 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
147
148 spin_unlock_irqrestore(&emsgs_lock, flags);
149
150 if (nblocked_emsgs_readers)
Matthias Kaehlcke24879a82008-07-25 01:48:38 -0700151 complete(&emsgs_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
154static ssize_t
155aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
156{
157 int ret = -EINVAL;
158
159 switch ((unsigned long) filp->private_data) {
160 default:
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400161 printk(KERN_INFO "aoe: can't write to that file.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 break;
163 case MINOR_DISCOVER:
164 ret = discover();
165 break;
166 case MINOR_INTERFACES:
167 ret = interfaces(buf, cnt);
168 break;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500169 case MINOR_REVALIDATE:
170 ret = revalidate(buf, cnt);
Ed L. Cashin262bf542008-02-08 04:20:03 -0800171 break;
172 case MINOR_FLUSH:
173 ret = aoedev_flush(buf, cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175 if (ret == 0)
176 ret = cnt;
177 return ret;
178}
179
180static int
181aoechr_open(struct inode *inode, struct file *filp)
182{
183 int n, i;
184
Jonathan Corbet579174a2008-05-15 10:03:09 -0600185 lock_kernel();
Eric Sesterhenn2017b372006-07-10 04:45:32 -0700186 n = iminor(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 filp->private_data = (void *) (unsigned long) n;
188
189 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Jonathan Corbet579174a2008-05-15 10:03:09 -0600190 if (chardevs[i].minor == n) {
191 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return 0;
Jonathan Corbet579174a2008-05-15 10:03:09 -0600193 }
194 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -EINVAL;
196}
197
198static int
199aoechr_rel(struct inode *inode, struct file *filp)
200{
201 return 0;
202}
203
204static ssize_t
205aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
206{
207 unsigned long n;
208 char *mp;
209 struct ErrMsg *em;
210 ssize_t len;
211 ulong flags;
212
213 n = (unsigned long) filp->private_data;
Ed L. Cashincf446f02008-02-08 04:20:03 -0800214 if (n != MINOR_ERR)
215 return -EFAULT;
216
217 spin_lock_irqsave(&emsgs_lock, flags);
218
219 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 em = emsgs + emsgs_head_idx;
Ed L. Cashincf446f02008-02-08 04:20:03 -0800221 if ((em->flags & EMFL_VALID) != 0)
222 break;
223 if (filp->f_flags & O_NDELAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 spin_unlock_irqrestore(&emsgs_lock, flags);
225 return -EAGAIN;
226 }
Ed L. Cashincf446f02008-02-08 04:20:03 -0800227 nblocked_emsgs_readers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 spin_unlock_irqrestore(&emsgs_lock, flags);
230
Matthias Kaehlcke24879a82008-07-25 01:48:38 -0700231 n = wait_for_completion_interruptible(&emsgs_comp);
Ed L. Cashincf446f02008-02-08 04:20:03 -0800232
233 spin_lock_irqsave(&emsgs_lock, flags);
234
235 nblocked_emsgs_readers--;
236
237 if (n) {
238 spin_unlock_irqrestore(&emsgs_lock, flags);
239 return -ERESTARTSYS;
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
Ed L. Cashincf446f02008-02-08 04:20:03 -0800242 if (em->len > cnt) {
243 spin_unlock_irqrestore(&emsgs_lock, flags);
244 return -EAGAIN;
245 }
246 mp = em->msg;
247 len = em->len;
248 em->msg = NULL;
249 em->flags &= ~EMFL_VALID;
250
251 emsgs_head_idx++;
252 emsgs_head_idx %= ARRAY_SIZE(emsgs);
253
254 spin_unlock_irqrestore(&emsgs_lock, flags);
255
256 n = copy_to_user(buf, mp, len);
257 kfree(mp);
258 return n == 0 ? len : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800261static const struct file_operations aoe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 .write = aoechr_write,
263 .read = aoechr_read,
264 .open = aoechr_open,
265 .release = aoechr_rel,
266 .owner = THIS_MODULE,
267};
268
Kay Sievers1ce8a0d2009-04-30 15:23:42 +0200269static char *aoe_nodename(struct device *dev)
270{
271 return kasprintf(GFP_KERNEL, "etherd/%s", dev_name(dev));
272}
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274int __init
275aoechr_init(void)
276{
277 int n, i;
278
279 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
280 if (n < 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400281 printk(KERN_ERR "aoe: can't register char device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return n;
283 }
Matthias Kaehlcke24879a82008-07-25 01:48:38 -0700284 init_completion(&emsgs_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 spin_lock_init(&emsgs_lock);
gregkh@suse.dedeb36972005-03-23 09:52:10 -0800286 aoe_class = class_create(THIS_MODULE, "aoe");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (IS_ERR(aoe_class)) {
288 unregister_chrdev(AOE_MAJOR, "aoechr");
289 return PTR_ERR(aoe_class);
290 }
Kay Sievers1ce8a0d2009-04-30 15:23:42 +0200291 aoe_class->nodename = aoe_nodename;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Greg Kroah-Hartman1ff9f542008-07-21 20:03:34 -0700294 device_create(aoe_class, NULL,
295 MKDEV(AOE_MAJOR, chardevs[i].minor), NULL,
296 chardevs[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 return 0;
299}
300
301void
302aoechr_exit(void)
303{
304 int i;
305
306 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Tony Jones7ea7ed02007-09-25 02:03:03 +0200307 device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
gregkh@suse.dedeb36972005-03-23 09:52:10 -0800308 class_destroy(aoe_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 unregister_chrdev(AOE_MAJOR, "aoechr");
310}
311