blob: 32b484ba21bd440006ae6112932ea92c248c6a33 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Jonathan Corbet579174a2008-05-15 10:03:09 -060012#include <linux/smp_lock.h>
David S. Millere9bb8fb2008-09-21 22:36:49 -070013#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "aoe.h"
15
16enum {
17 //MINOR_STAT = 1, (moved to sysfs)
18 MINOR_ERR = 2,
19 MINOR_DISCOVER,
20 MINOR_INTERFACES,
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050021 MINOR_REVALIDATE,
Ed L. Cashin262bf542008-02-08 04:20:03 -080022 MINOR_FLUSH,
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 MSGSZ = 2048,
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 NMSG = 100, /* message backlog to retain */
25};
26
27struct aoe_chardev {
28 ulong minor;
29 char name[32];
30};
31
32enum { EMFL_VALID = 1 };
33
34struct ErrMsg {
35 short flags;
36 short len;
37 char *msg;
38};
39
40static struct ErrMsg emsgs[NMSG];
41static int emsgs_head_idx, emsgs_tail_idx;
Matthias Kaehlcke24879a82008-07-25 01:48:38 -070042static struct completion emsgs_comp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static spinlock_t emsgs_lock;
44static int nblocked_emsgs_readers;
gregkh@suse.dedeb36972005-03-23 09:52:10 -080045static struct class *aoe_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static struct aoe_chardev chardevs[] = {
47 { MINOR_ERR, "err" },
48 { MINOR_DISCOVER, "discover" },
49 { MINOR_INTERFACES, "interfaces" },
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050050 { MINOR_REVALIDATE, "revalidate" },
Ed L. Cashin262bf542008-02-08 04:20:03 -080051 { MINOR_FLUSH, "flush" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
54static int
55discover(void)
56{
57 aoecmd_cfg(0xffff, 0xff);
58 return 0;
59}
60
61static int
62interfaces(const char __user *str, size_t size)
63{
64 if (set_aoe_iflist(str, size)) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -040065 printk(KERN_ERR
66 "aoe: could not set interface list: too many interfaces\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return -EINVAL;
68 }
69 return 0;
70}
71
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050072static int
73revalidate(const char __user *str, size_t size)
74{
75 int major, minor, n;
76 ulong flags;
77 struct aoedev *d;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080078 struct sk_buff *skb;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050079 char buf[16];
80
81 if (size >= sizeof buf)
82 return -EINVAL;
83 buf[sizeof buf - 1] = '\0';
84 if (copy_from_user(buf, str, size))
85 return -EFAULT;
86
87 /* should be e%d.%d format */
88 n = sscanf(buf, "e%d.%d", &major, &minor);
89 if (n != 2) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -040090 printk(KERN_ERR "aoe: invalid device specification\n");
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050091 return -EINVAL;
92 }
93 d = aoedev_by_aoeaddr(major, minor);
94 if (!d)
95 return -EINVAL;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050096 spin_lock_irqsave(&d->lock, flags);
Ed L. Cashin68e0d422008-02-08 04:20:00 -080097 aoecmd_cleanslate(d);
98loop:
99 skb = aoecmd_ata_id(d);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500100 spin_unlock_irqrestore(&d->lock, flags);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800101 /* try again if we are able to sleep a bit,
102 * otherwise give up this revalidation
103 */
104 if (!skb && !msleep_interruptible(200)) {
105 spin_lock_irqsave(&d->lock, flags);
106 goto loop;
107 }
David S. Millere9bb8fb2008-09-21 22:36:49 -0700108 if (skb) {
109 struct sk_buff_head queue;
110 __skb_queue_head_init(&queue);
111 __skb_queue_tail(&queue, skb);
112 aoenet_xmit(&queue);
113 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500114 aoecmd_cfg(major, minor);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500115 return 0;
116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118void
119aoechr_error(char *msg)
120{
121 struct ErrMsg *em;
122 char *mp;
123 ulong flags, n;
124
125 n = strlen(msg);
126
127 spin_lock_irqsave(&emsgs_lock, flags);
128
129 em = emsgs + emsgs_tail_idx;
130 if ((em->flags & EMFL_VALID)) {
131bail: spin_unlock_irqrestore(&emsgs_lock, flags);
132 return;
133 }
134
135 mp = kmalloc(n, GFP_ATOMIC);
136 if (mp == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400137 printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 goto bail;
139 }
140
141 memcpy(mp, msg, n);
142 em->msg = mp;
143 em->flags |= EMFL_VALID;
144 em->len = n;
145
146 emsgs_tail_idx++;
147 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
148
149 spin_unlock_irqrestore(&emsgs_lock, flags);
150
151 if (nblocked_emsgs_readers)
Matthias Kaehlcke24879a82008-07-25 01:48:38 -0700152 complete(&emsgs_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155static ssize_t
156aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
157{
158 int ret = -EINVAL;
159
160 switch ((unsigned long) filp->private_data) {
161 default:
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400162 printk(KERN_INFO "aoe: can't write to that file.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 break;
164 case MINOR_DISCOVER:
165 ret = discover();
166 break;
167 case MINOR_INTERFACES:
168 ret = interfaces(buf, cnt);
169 break;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500170 case MINOR_REVALIDATE:
171 ret = revalidate(buf, cnt);
Ed L. Cashin262bf542008-02-08 04:20:03 -0800172 break;
173 case MINOR_FLUSH:
174 ret = aoedev_flush(buf, cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176 if (ret == 0)
177 ret = cnt;
178 return ret;
179}
180
181static int
182aoechr_open(struct inode *inode, struct file *filp)
183{
184 int n, i;
185
Jonathan Corbet579174a2008-05-15 10:03:09 -0600186 lock_kernel();
Eric Sesterhenn2017b372006-07-10 04:45:32 -0700187 n = iminor(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 filp->private_data = (void *) (unsigned long) n;
189
190 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Jonathan Corbet579174a2008-05-15 10:03:09 -0600191 if (chardevs[i].minor == n) {
192 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return 0;
Jonathan Corbet579174a2008-05-15 10:03:09 -0600194 }
195 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return -EINVAL;
197}
198
199static int
200aoechr_rel(struct inode *inode, struct file *filp)
201{
202 return 0;
203}
204
205static ssize_t
206aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
207{
208 unsigned long n;
209 char *mp;
210 struct ErrMsg *em;
211 ssize_t len;
212 ulong flags;
213
214 n = (unsigned long) filp->private_data;
Ed L. Cashincf446f02008-02-08 04:20:03 -0800215 if (n != MINOR_ERR)
216 return -EFAULT;
217
218 spin_lock_irqsave(&emsgs_lock, flags);
219
220 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 em = emsgs + emsgs_head_idx;
Ed L. Cashincf446f02008-02-08 04:20:03 -0800222 if ((em->flags & EMFL_VALID) != 0)
223 break;
224 if (filp->f_flags & O_NDELAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 spin_unlock_irqrestore(&emsgs_lock, flags);
226 return -EAGAIN;
227 }
Ed L. Cashincf446f02008-02-08 04:20:03 -0800228 nblocked_emsgs_readers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 spin_unlock_irqrestore(&emsgs_lock, flags);
231
Matthias Kaehlcke24879a82008-07-25 01:48:38 -0700232 n = wait_for_completion_interruptible(&emsgs_comp);
Ed L. Cashincf446f02008-02-08 04:20:03 -0800233
234 spin_lock_irqsave(&emsgs_lock, flags);
235
236 nblocked_emsgs_readers--;
237
238 if (n) {
239 spin_unlock_irqrestore(&emsgs_lock, flags);
240 return -ERESTARTSYS;
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Ed L. Cashincf446f02008-02-08 04:20:03 -0800243 if (em->len > cnt) {
244 spin_unlock_irqrestore(&emsgs_lock, flags);
245 return -EAGAIN;
246 }
247 mp = em->msg;
248 len = em->len;
249 em->msg = NULL;
250 em->flags &= ~EMFL_VALID;
251
252 emsgs_head_idx++;
253 emsgs_head_idx %= ARRAY_SIZE(emsgs);
254
255 spin_unlock_irqrestore(&emsgs_lock, flags);
256
257 n = copy_to_user(buf, mp, len);
258 kfree(mp);
259 return n == 0 ? len : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800262static const struct file_operations aoe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 .write = aoechr_write,
264 .read = aoechr_read,
265 .open = aoechr_open,
266 .release = aoechr_rel,
267 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200268 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
Kay Sieverse454cea2009-09-18 23:01:12 +0200271static char *aoe_devnode(struct device *dev, mode_t *mode)
Kay Sievers1ce8a0d2009-04-30 15:23:42 +0200272{
273 return kasprintf(GFP_KERNEL, "etherd/%s", dev_name(dev));
274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276int __init
277aoechr_init(void)
278{
279 int n, i;
280
281 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
282 if (n < 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400283 printk(KERN_ERR "aoe: can't register char device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return n;
285 }
Matthias Kaehlcke24879a82008-07-25 01:48:38 -0700286 init_completion(&emsgs_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 spin_lock_init(&emsgs_lock);
gregkh@suse.dedeb36972005-03-23 09:52:10 -0800288 aoe_class = class_create(THIS_MODULE, "aoe");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (IS_ERR(aoe_class)) {
290 unregister_chrdev(AOE_MAJOR, "aoechr");
291 return PTR_ERR(aoe_class);
292 }
Kay Sieverse454cea2009-09-18 23:01:12 +0200293 aoe_class->devnode = aoe_devnode;
Kay Sievers1ce8a0d2009-04-30 15:23:42 +0200294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Greg Kroah-Hartman1ff9f542008-07-21 20:03:34 -0700296 device_create(aoe_class, NULL,
297 MKDEV(AOE_MAJOR, chardevs[i].minor), NULL,
298 chardevs[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 return 0;
301}
302
303void
304aoechr_exit(void)
305{
306 int i;
307
308 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Tony Jones7ea7ed02007-09-25 02:03:03 +0200309 device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
gregkh@suse.dedeb36972005-03-23 09:52:10 -0800310 class_destroy(aoe_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 unregister_chrdev(AOE_MAJOR, "aoechr");
312}
313