blob: ab41be625a5316e30ae2f30d1329e657196e98e2 [file] [log] [blame]
Ed Cashinfea05a22012-10-04 17:16:38 -07001/* Copyright (c) 2012 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. Millere9bb8fb02008-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);
Ed Cashin662a8892012-12-17 16:03:26 -080042
43/* A ring buffer of error messages, to be read through
44 * "/dev/etherd/err". When no messages are present,
45 * readers will block waiting for messages to appear.
46 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static struct ErrMsg emsgs[NMSG];
48static int emsgs_head_idx, emsgs_tail_idx;
Matthias Kaehlcke24879a82008-07-25 01:48:38 -070049static struct completion emsgs_comp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static spinlock_t emsgs_lock;
51static int nblocked_emsgs_readers;
gregkh@suse.dedeb36972005-03-23 09:52:10 -080052static struct class *aoe_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static struct aoe_chardev chardevs[] = {
54 { MINOR_ERR, "err" },
55 { MINOR_DISCOVER, "discover" },
56 { MINOR_INTERFACES, "interfaces" },
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050057 { MINOR_REVALIDATE, "revalidate" },
Ed L. Cashin262bf542008-02-08 04:20:03 -080058 { MINOR_FLUSH, "flush" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
61static int
62discover(void)
63{
64 aoecmd_cfg(0xffff, 0xff);
65 return 0;
66}
67
68static int
69interfaces(const char __user *str, size_t size)
70{
71 if (set_aoe_iflist(str, size)) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -040072 printk(KERN_ERR
73 "aoe: could not set interface list: too many interfaces\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return -EINVAL;
75 }
76 return 0;
77}
78
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050079static int
80revalidate(const char __user *str, size_t size)
81{
82 int major, minor, n;
83 ulong flags;
84 struct aoedev *d;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080085 struct sk_buff *skb;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050086 char buf[16];
87
88 if (size >= sizeof buf)
89 return -EINVAL;
90 buf[sizeof buf - 1] = '\0';
91 if (copy_from_user(buf, str, size))
92 return -EFAULT;
93
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050094 n = sscanf(buf, "e%d.%d", &major, &minor);
95 if (n != 2) {
Ed Cashin896831f2012-10-04 17:16:21 -070096 pr_err("aoe: invalid device specification %s\n", buf);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050097 return -EINVAL;
98 }
Ed Cashin0c966212012-10-04 17:16:40 -070099 d = aoedev_by_aoeaddr(major, minor, 0);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500100 if (!d)
101 return -EINVAL;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500102 spin_lock_irqsave(&d->lock, flags);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800103 aoecmd_cleanslate(d);
Ed Cashin25f4d752012-10-04 17:16:31 -0700104 aoecmd_cfg(major, minor);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800105loop:
106 skb = aoecmd_ata_id(d);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500107 spin_unlock_irqrestore(&d->lock, flags);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800108 /* try again if we are able to sleep a bit,
109 * otherwise give up this revalidation
110 */
Ed Cashin25f4d752012-10-04 17:16:31 -0700111 if (!skb && !msleep_interruptible(250)) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800112 spin_lock_irqsave(&d->lock, flags);
113 goto loop;
114 }
Ed Cashin69cf2d852012-10-04 17:16:23 -0700115 aoedev_put(d);
David S. Millere9bb8fb02008-09-21 22:36:49 -0700116 if (skb) {
117 struct sk_buff_head queue;
118 __skb_queue_head_init(&queue);
119 __skb_queue_tail(&queue, skb);
120 aoenet_xmit(&queue);
121 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500122 return 0;
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125void
126aoechr_error(char *msg)
127{
128 struct ErrMsg *em;
129 char *mp;
130 ulong flags, n;
131
132 n = strlen(msg);
133
134 spin_lock_irqsave(&emsgs_lock, flags);
135
136 em = emsgs + emsgs_tail_idx;
137 if ((em->flags & EMFL_VALID)) {
138bail: spin_unlock_irqrestore(&emsgs_lock, flags);
139 return;
140 }
141
Mihnea Dobrescu-Balaur60abc782013-04-30 15:28:29 -0700142 mp = kmemdup(msg, n, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (mp == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400144 printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 goto bail;
146 }
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 em->msg = mp;
149 em->flags |= EMFL_VALID;
150 em->len = n;
151
152 emsgs_tail_idx++;
153 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
154
155 spin_unlock_irqrestore(&emsgs_lock, flags);
156
157 if (nblocked_emsgs_readers)
Matthias Kaehlcke24879a82008-07-25 01:48:38 -0700158 complete(&emsgs_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161static ssize_t
162aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
163{
164 int ret = -EINVAL;
165
166 switch ((unsigned long) filp->private_data) {
167 default:
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400168 printk(KERN_INFO "aoe: can't write to that file.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 break;
170 case MINOR_DISCOVER:
171 ret = discover();
172 break;
173 case MINOR_INTERFACES:
174 ret = interfaces(buf, cnt);
175 break;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500176 case MINOR_REVALIDATE:
177 ret = revalidate(buf, cnt);
Ed L. Cashin262bf542008-02-08 04:20:03 -0800178 break;
179 case MINOR_FLUSH:
180 ret = aoedev_flush(buf, cnt);
Ed Cashinb21faa22012-10-04 17:16:35 -0700181 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183 if (ret == 0)
184 ret = cnt;
185 return ret;
186}
187
188static int
189aoechr_open(struct inode *inode, struct file *filp)
190{
191 int n, i;
192
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200193 mutex_lock(&aoechr_mutex);
Eric Sesterhenn2017b372006-07-10 04:45:32 -0700194 n = iminor(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 filp->private_data = (void *) (unsigned long) n;
196
197 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Jonathan Corbet579174a2008-05-15 10:03:09 -0600198 if (chardevs[i].minor == n) {
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200199 mutex_unlock(&aoechr_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return 0;
Jonathan Corbet579174a2008-05-15 10:03:09 -0600201 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200202 mutex_unlock(&aoechr_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return -EINVAL;
204}
205
206static int
207aoechr_rel(struct inode *inode, struct file *filp)
208{
209 return 0;
210}
211
212static ssize_t
213aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
214{
215 unsigned long n;
216 char *mp;
217 struct ErrMsg *em;
218 ssize_t len;
219 ulong flags;
220
221 n = (unsigned long) filp->private_data;
Ed L. Cashincf446f02008-02-08 04:20:03 -0800222 if (n != MINOR_ERR)
223 return -EFAULT;
224
225 spin_lock_irqsave(&emsgs_lock, flags);
226
227 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 em = emsgs + emsgs_head_idx;
Ed L. Cashincf446f02008-02-08 04:20:03 -0800229 if ((em->flags & EMFL_VALID) != 0)
230 break;
231 if (filp->f_flags & O_NDELAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 spin_unlock_irqrestore(&emsgs_lock, flags);
233 return -EAGAIN;
234 }
Ed L. Cashincf446f02008-02-08 04:20:03 -0800235 nblocked_emsgs_readers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 spin_unlock_irqrestore(&emsgs_lock, flags);
238
Matthias Kaehlcke24879a82008-07-25 01:48:38 -0700239 n = wait_for_completion_interruptible(&emsgs_comp);
Ed L. Cashincf446f02008-02-08 04:20:03 -0800240
241 spin_lock_irqsave(&emsgs_lock, flags);
242
243 nblocked_emsgs_readers--;
244
245 if (n) {
246 spin_unlock_irqrestore(&emsgs_lock, flags);
247 return -ERESTARTSYS;
248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Ed L. Cashincf446f02008-02-08 04:20:03 -0800250 if (em->len > cnt) {
251 spin_unlock_irqrestore(&emsgs_lock, flags);
252 return -EAGAIN;
253 }
254 mp = em->msg;
255 len = em->len;
256 em->msg = NULL;
257 em->flags &= ~EMFL_VALID;
258
259 emsgs_head_idx++;
260 emsgs_head_idx %= ARRAY_SIZE(emsgs);
261
262 spin_unlock_irqrestore(&emsgs_lock, flags);
263
264 n = copy_to_user(buf, mp, len);
265 kfree(mp);
266 return n == 0 ? len : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800269static const struct file_operations aoe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 .write = aoechr_write,
271 .read = aoechr_read,
272 .open = aoechr_open,
273 .release = aoechr_rel,
274 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200275 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276};
277
Al Viro2c9ede52011-07-23 20:24:48 -0400278static char *aoe_devnode(struct device *dev, umode_t *mode)
Kay Sievers1ce8a0d2009-04-30 15:23:42 +0200279{
280 return kasprintf(GFP_KERNEL, "etherd/%s", dev_name(dev));
281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283int __init
284aoechr_init(void)
285{
286 int n, i;
287
288 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
Ed Cashina04b41c2012-12-17 16:03:39 -0800289 if (n < 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400290 printk(KERN_ERR "aoe: can't register char device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return n;
292 }
Matthias Kaehlcke24879a82008-07-25 01:48:38 -0700293 init_completion(&emsgs_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 spin_lock_init(&emsgs_lock);
gregkh@suse.dedeb36972005-03-23 09:52:10 -0800295 aoe_class = class_create(THIS_MODULE, "aoe");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (IS_ERR(aoe_class)) {
297 unregister_chrdev(AOE_MAJOR, "aoechr");
298 return PTR_ERR(aoe_class);
299 }
Kay Sieverse454cea2009-09-18 23:01:12 +0200300 aoe_class->devnode = aoe_devnode;
Kay Sievers1ce8a0d2009-04-30 15:23:42 +0200301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Greg Kroah-Hartman1ff9f542008-07-21 20:03:34 -0700303 device_create(aoe_class, NULL,
304 MKDEV(AOE_MAJOR, chardevs[i].minor), NULL,
305 chardevs[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 return 0;
308}
309
310void
311aoechr_exit(void)
312{
313 int i;
314
315 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Tony Jones7ea7ed02007-09-25 02:03:03 +0200316 device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
gregkh@suse.dedeb36972005-03-23 09:52:10 -0800317 class_destroy(aoe_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 unregister_chrdev(AOE_MAJOR, "aoechr");
319}
320