blob: e86d2062a1641f36ff2bc0200d286f433e56fb27 [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>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020012#include <linux/mutex.h>
David S. Millere9bb8fb2008-09-21 22:36:49 -070013#include <linux/skbuff.h>
Paul Gortmakerd5decd32011-05-26 16:00:52 -040014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "aoe.h"
16
17enum {
18 //MINOR_STAT = 1, (moved to sysfs)
19 MINOR_ERR = 2,
20 MINOR_DISCOVER,
21 MINOR_INTERFACES,
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050022 MINOR_REVALIDATE,
Ed L. Cashin262bf542008-02-08 04:20:03 -080023 MINOR_FLUSH,
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 MSGSZ = 2048,
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 NMSG = 100, /* message backlog to retain */
26};
27
28struct aoe_chardev {
29 ulong minor;
30 char name[32];
31};
32
33enum { EMFL_VALID = 1 };
34
35struct ErrMsg {
36 short flags;
37 short len;
38 char *msg;
39};
40
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020041static DEFINE_MUTEX(aoechr_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static struct ErrMsg emsgs[NMSG];
43static int emsgs_head_idx, emsgs_tail_idx;
Matthias Kaehlcke24879a82008-07-25 01:48:38 -070044static struct completion emsgs_comp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static spinlock_t emsgs_lock;
46static int nblocked_emsgs_readers;
gregkh@suse.dedeb36972005-03-23 09:52:10 -080047static struct class *aoe_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static struct aoe_chardev chardevs[] = {
49 { MINOR_ERR, "err" },
50 { MINOR_DISCOVER, "discover" },
51 { MINOR_INTERFACES, "interfaces" },
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050052 { MINOR_REVALIDATE, "revalidate" },
Ed L. Cashin262bf542008-02-08 04:20:03 -080053 { MINOR_FLUSH, "flush" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
55
56static int
57discover(void)
58{
59 aoecmd_cfg(0xffff, 0xff);
60 return 0;
61}
62
63static int
64interfaces(const char __user *str, size_t size)
65{
66 if (set_aoe_iflist(str, size)) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -040067 printk(KERN_ERR
68 "aoe: could not set interface list: too many interfaces\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return -EINVAL;
70 }
71 return 0;
72}
73
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050074static int
75revalidate(const char __user *str, size_t size)
76{
77 int major, minor, n;
78 ulong flags;
79 struct aoedev *d;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080080 struct sk_buff *skb;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050081 char buf[16];
82
83 if (size >= sizeof buf)
84 return -EINVAL;
85 buf[sizeof buf - 1] = '\0';
86 if (copy_from_user(buf, str, size))
87 return -EFAULT;
88
89 /* should be e%d.%d format */
90 n = sscanf(buf, "e%d.%d", &major, &minor);
91 if (n != 2) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -040092 printk(KERN_ERR "aoe: invalid device specification\n");
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050093 return -EINVAL;
94 }
95 d = aoedev_by_aoeaddr(major, minor);
96 if (!d)
97 return -EINVAL;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050098 spin_lock_irqsave(&d->lock, flags);
Ed L. Cashin68e0d422008-02-08 04:20:00 -080099 aoecmd_cleanslate(d);
100loop:
101 skb = aoecmd_ata_id(d);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500102 spin_unlock_irqrestore(&d->lock, flags);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800103 /* try again if we are able to sleep a bit,
104 * otherwise give up this revalidation
105 */
106 if (!skb && !msleep_interruptible(200)) {
107 spin_lock_irqsave(&d->lock, flags);
108 goto loop;
109 }
David S. Millere9bb8fb2008-09-21 22:36:49 -0700110 if (skb) {
111 struct sk_buff_head queue;
112 __skb_queue_head_init(&queue);
113 __skb_queue_tail(&queue, skb);
114 aoenet_xmit(&queue);
115 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500116 aoecmd_cfg(major, minor);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500117 return 0;
118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120void
121aoechr_error(char *msg)
122{
123 struct ErrMsg *em;
124 char *mp;
125 ulong flags, n;
126
127 n = strlen(msg);
128
129 spin_lock_irqsave(&emsgs_lock, flags);
130
131 em = emsgs + emsgs_tail_idx;
132 if ((em->flags & EMFL_VALID)) {
133bail: spin_unlock_irqrestore(&emsgs_lock, flags);
134 return;
135 }
136
137 mp = kmalloc(n, GFP_ATOMIC);
138 if (mp == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400139 printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 goto bail;
141 }
142
143 memcpy(mp, msg, n);
144 em->msg = mp;
145 em->flags |= EMFL_VALID;
146 em->len = n;
147
148 emsgs_tail_idx++;
149 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
150
151 spin_unlock_irqrestore(&emsgs_lock, flags);
152
153 if (nblocked_emsgs_readers)
Matthias Kaehlcke24879a82008-07-25 01:48:38 -0700154 complete(&emsgs_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157static ssize_t
158aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
159{
160 int ret = -EINVAL;
161
162 switch ((unsigned long) filp->private_data) {
163 default:
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400164 printk(KERN_INFO "aoe: can't write to that file.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 break;
166 case MINOR_DISCOVER:
167 ret = discover();
168 break;
169 case MINOR_INTERFACES:
170 ret = interfaces(buf, cnt);
171 break;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500172 case MINOR_REVALIDATE:
173 ret = revalidate(buf, cnt);
Ed L. Cashin262bf542008-02-08 04:20:03 -0800174 break;
175 case MINOR_FLUSH:
176 ret = aoedev_flush(buf, cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178 if (ret == 0)
179 ret = cnt;
180 return ret;
181}
182
183static int
184aoechr_open(struct inode *inode, struct file *filp)
185{
186 int n, i;
187
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200188 mutex_lock(&aoechr_mutex);
Eric Sesterhenn2017b372006-07-10 04:45:32 -0700189 n = iminor(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 filp->private_data = (void *) (unsigned long) n;
191
192 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Jonathan Corbet579174a2008-05-15 10:03:09 -0600193 if (chardevs[i].minor == n) {
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200194 mutex_unlock(&aoechr_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return 0;
Jonathan Corbet579174a2008-05-15 10:03:09 -0600196 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200197 mutex_unlock(&aoechr_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return -EINVAL;
199}
200
201static int
202aoechr_rel(struct inode *inode, struct file *filp)
203{
204 return 0;
205}
206
207static ssize_t
208aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
209{
210 unsigned long n;
211 char *mp;
212 struct ErrMsg *em;
213 ssize_t len;
214 ulong flags;
215
216 n = (unsigned long) filp->private_data;
Ed L. Cashincf446f02008-02-08 04:20:03 -0800217 if (n != MINOR_ERR)
218 return -EFAULT;
219
220 spin_lock_irqsave(&emsgs_lock, flags);
221
222 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 em = emsgs + emsgs_head_idx;
Ed L. Cashincf446f02008-02-08 04:20:03 -0800224 if ((em->flags & EMFL_VALID) != 0)
225 break;
226 if (filp->f_flags & O_NDELAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 spin_unlock_irqrestore(&emsgs_lock, flags);
228 return -EAGAIN;
229 }
Ed L. Cashincf446f02008-02-08 04:20:03 -0800230 nblocked_emsgs_readers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 spin_unlock_irqrestore(&emsgs_lock, flags);
233
Matthias Kaehlcke24879a82008-07-25 01:48:38 -0700234 n = wait_for_completion_interruptible(&emsgs_comp);
Ed L. Cashincf446f02008-02-08 04:20:03 -0800235
236 spin_lock_irqsave(&emsgs_lock, flags);
237
238 nblocked_emsgs_readers--;
239
240 if (n) {
241 spin_unlock_irqrestore(&emsgs_lock, flags);
242 return -ERESTARTSYS;
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
Ed L. Cashincf446f02008-02-08 04:20:03 -0800245 if (em->len > cnt) {
246 spin_unlock_irqrestore(&emsgs_lock, flags);
247 return -EAGAIN;
248 }
249 mp = em->msg;
250 len = em->len;
251 em->msg = NULL;
252 em->flags &= ~EMFL_VALID;
253
254 emsgs_head_idx++;
255 emsgs_head_idx %= ARRAY_SIZE(emsgs);
256
257 spin_unlock_irqrestore(&emsgs_lock, flags);
258
259 n = copy_to_user(buf, mp, len);
260 kfree(mp);
261 return n == 0 ? len : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800264static const struct file_operations aoe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 .write = aoechr_write,
266 .read = aoechr_read,
267 .open = aoechr_open,
268 .release = aoechr_rel,
269 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200270 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271};
272
Al Viro2c9ede52011-07-23 20:24:48 -0400273static char *aoe_devnode(struct device *dev, umode_t *mode)
Kay Sievers1ce8a0d2009-04-30 15:23:42 +0200274{
275 return kasprintf(GFP_KERNEL, "etherd/%s", dev_name(dev));
276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278int __init
279aoechr_init(void)
280{
281 int n, i;
282
283 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
284 if (n < 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400285 printk(KERN_ERR "aoe: can't register char device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return n;
287 }
Matthias Kaehlcke24879a82008-07-25 01:48:38 -0700288 init_completion(&emsgs_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 spin_lock_init(&emsgs_lock);
gregkh@suse.dedeb36972005-03-23 09:52:10 -0800290 aoe_class = class_create(THIS_MODULE, "aoe");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (IS_ERR(aoe_class)) {
292 unregister_chrdev(AOE_MAJOR, "aoechr");
293 return PTR_ERR(aoe_class);
294 }
Kay Sieverse454cea2009-09-18 23:01:12 +0200295 aoe_class->devnode = aoe_devnode;
Kay Sievers1ce8a0d2009-04-30 15:23:42 +0200296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Greg Kroah-Hartman1ff9f542008-07-21 20:03:34 -0700298 device_create(aoe_class, NULL,
299 MKDEV(AOE_MAJOR, chardevs[i].minor), NULL,
300 chardevs[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 return 0;
303}
304
305void
306aoechr_exit(void)
307{
308 int i;
309
310 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Tony Jones7ea7ed02007-09-25 02:03:03 +0200311 device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
gregkh@suse.dedeb36972005-03-23 09:52:10 -0800312 class_destroy(aoe_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unregister_chrdev(AOE_MAJOR, "aoechr");
314}
315