blob: 181ebb85f0be582e95206aa144c04ee26ff016bc [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "aoe.h"
13
14enum {
15 //MINOR_STAT = 1, (moved to sysfs)
16 MINOR_ERR = 2,
17 MINOR_DISCOVER,
18 MINOR_INTERFACES,
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050019 MINOR_REVALIDATE,
Ed L. Cashin262bf542008-02-08 04:20:03 -080020 MINOR_FLUSH,
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 MSGSZ = 2048,
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 NMSG = 100, /* message backlog to retain */
23};
24
25struct aoe_chardev {
26 ulong minor;
27 char name[32];
28};
29
30enum { EMFL_VALID = 1 };
31
32struct ErrMsg {
33 short flags;
34 short len;
35 char *msg;
36};
37
38static struct ErrMsg emsgs[NMSG];
39static int emsgs_head_idx, emsgs_tail_idx;
Matthias Kaehlcke24879a82008-07-25 01:48:38 -070040static struct completion emsgs_comp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static spinlock_t emsgs_lock;
42static int nblocked_emsgs_readers;
gregkh@suse.dedeb36972005-03-23 09:52:10 -080043static struct class *aoe_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static struct aoe_chardev chardevs[] = {
45 { MINOR_ERR, "err" },
46 { MINOR_DISCOVER, "discover" },
47 { MINOR_INTERFACES, "interfaces" },
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050048 { MINOR_REVALIDATE, "revalidate" },
Ed L. Cashin262bf542008-02-08 04:20:03 -080049 { MINOR_FLUSH, "flush" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
52static int
53discover(void)
54{
55 aoecmd_cfg(0xffff, 0xff);
56 return 0;
57}
58
59static int
60interfaces(const char __user *str, size_t size)
61{
62 if (set_aoe_iflist(str, size)) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -040063 printk(KERN_ERR
64 "aoe: could not set interface list: too many interfaces\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return -EINVAL;
66 }
67 return 0;
68}
69
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050070static int
71revalidate(const char __user *str, size_t size)
72{
73 int major, minor, n;
74 ulong flags;
75 struct aoedev *d;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080076 struct sk_buff *skb;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050077 char buf[16];
78
79 if (size >= sizeof buf)
80 return -EINVAL;
81 buf[sizeof buf - 1] = '\0';
82 if (copy_from_user(buf, str, size))
83 return -EFAULT;
84
85 /* should be e%d.%d format */
86 n = sscanf(buf, "e%d.%d", &major, &minor);
87 if (n != 2) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -040088 printk(KERN_ERR "aoe: invalid device specification\n");
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050089 return -EINVAL;
90 }
91 d = aoedev_by_aoeaddr(major, minor);
92 if (!d)
93 return -EINVAL;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050094 spin_lock_irqsave(&d->lock, flags);
Ed L. Cashin68e0d422008-02-08 04:20:00 -080095 aoecmd_cleanslate(d);
96loop:
97 skb = aoecmd_ata_id(d);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050098 spin_unlock_irqrestore(&d->lock, flags);
Ed L. Cashin68e0d422008-02-08 04:20:00 -080099 /* try again if we are able to sleep a bit,
100 * otherwise give up this revalidation
101 */
102 if (!skb && !msleep_interruptible(200)) {
103 spin_lock_irqsave(&d->lock, flags);
104 goto loop;
105 }
106 aoenet_xmit(skb);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500107 aoecmd_cfg(major, minor);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500108 return 0;
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111void
112aoechr_error(char *msg)
113{
114 struct ErrMsg *em;
115 char *mp;
116 ulong flags, n;
117
118 n = strlen(msg);
119
120 spin_lock_irqsave(&emsgs_lock, flags);
121
122 em = emsgs + emsgs_tail_idx;
123 if ((em->flags & EMFL_VALID)) {
124bail: spin_unlock_irqrestore(&emsgs_lock, flags);
125 return;
126 }
127
128 mp = kmalloc(n, GFP_ATOMIC);
129 if (mp == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400130 printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 goto bail;
132 }
133
134 memcpy(mp, msg, n);
135 em->msg = mp;
136 em->flags |= EMFL_VALID;
137 em->len = n;
138
139 emsgs_tail_idx++;
140 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
141
142 spin_unlock_irqrestore(&emsgs_lock, flags);
143
144 if (nblocked_emsgs_readers)
Matthias Kaehlcke24879a82008-07-25 01:48:38 -0700145 complete(&emsgs_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148static ssize_t
149aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
150{
151 int ret = -EINVAL;
152
153 switch ((unsigned long) filp->private_data) {
154 default:
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400155 printk(KERN_INFO "aoe: can't write to that file.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 break;
157 case MINOR_DISCOVER:
158 ret = discover();
159 break;
160 case MINOR_INTERFACES:
161 ret = interfaces(buf, cnt);
162 break;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500163 case MINOR_REVALIDATE:
164 ret = revalidate(buf, cnt);
Ed L. Cashin262bf542008-02-08 04:20:03 -0800165 break;
166 case MINOR_FLUSH:
167 ret = aoedev_flush(buf, cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169 if (ret == 0)
170 ret = cnt;
171 return ret;
172}
173
174static int
175aoechr_open(struct inode *inode, struct file *filp)
176{
177 int n, i;
178
Jonathan Corbet579174a2008-05-15 10:03:09 -0600179 lock_kernel();
Eric Sesterhenn2017b372006-07-10 04:45:32 -0700180 n = iminor(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 filp->private_data = (void *) (unsigned long) n;
182
183 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Jonathan Corbet579174a2008-05-15 10:03:09 -0600184 if (chardevs[i].minor == n) {
185 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return 0;
Jonathan Corbet579174a2008-05-15 10:03:09 -0600187 }
188 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return -EINVAL;
190}
191
192static int
193aoechr_rel(struct inode *inode, struct file *filp)
194{
195 return 0;
196}
197
198static ssize_t
199aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
200{
201 unsigned long n;
202 char *mp;
203 struct ErrMsg *em;
204 ssize_t len;
205 ulong flags;
206
207 n = (unsigned long) filp->private_data;
Ed L. Cashincf446f02008-02-08 04:20:03 -0800208 if (n != MINOR_ERR)
209 return -EFAULT;
210
211 spin_lock_irqsave(&emsgs_lock, flags);
212
213 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 em = emsgs + emsgs_head_idx;
Ed L. Cashincf446f02008-02-08 04:20:03 -0800215 if ((em->flags & EMFL_VALID) != 0)
216 break;
217 if (filp->f_flags & O_NDELAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 spin_unlock_irqrestore(&emsgs_lock, flags);
219 return -EAGAIN;
220 }
Ed L. Cashincf446f02008-02-08 04:20:03 -0800221 nblocked_emsgs_readers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 spin_unlock_irqrestore(&emsgs_lock, flags);
224
Matthias Kaehlcke24879a82008-07-25 01:48:38 -0700225 n = wait_for_completion_interruptible(&emsgs_comp);
Ed L. Cashincf446f02008-02-08 04:20:03 -0800226
227 spin_lock_irqsave(&emsgs_lock, flags);
228
229 nblocked_emsgs_readers--;
230
231 if (n) {
232 spin_unlock_irqrestore(&emsgs_lock, flags);
233 return -ERESTARTSYS;
234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Ed L. Cashincf446f02008-02-08 04:20:03 -0800236 if (em->len > cnt) {
237 spin_unlock_irqrestore(&emsgs_lock, flags);
238 return -EAGAIN;
239 }
240 mp = em->msg;
241 len = em->len;
242 em->msg = NULL;
243 em->flags &= ~EMFL_VALID;
244
245 emsgs_head_idx++;
246 emsgs_head_idx %= ARRAY_SIZE(emsgs);
247
248 spin_unlock_irqrestore(&emsgs_lock, flags);
249
250 n = copy_to_user(buf, mp, len);
251 kfree(mp);
252 return n == 0 ? len : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800255static const struct file_operations aoe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 .write = aoechr_write,
257 .read = aoechr_read,
258 .open = aoechr_open,
259 .release = aoechr_rel,
260 .owner = THIS_MODULE,
261};
262
263int __init
264aoechr_init(void)
265{
266 int n, i;
267
268 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
269 if (n < 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400270 printk(KERN_ERR "aoe: can't register char device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return n;
272 }
Matthias Kaehlcke24879a82008-07-25 01:48:38 -0700273 init_completion(&emsgs_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 spin_lock_init(&emsgs_lock);
gregkh@suse.dedeb36972005-03-23 09:52:10 -0800275 aoe_class = class_create(THIS_MODULE, "aoe");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (IS_ERR(aoe_class)) {
277 unregister_chrdev(AOE_MAJOR, "aoechr");
278 return PTR_ERR(aoe_class);
279 }
280 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Greg Kroah-Hartmanf79f0602008-05-21 12:52:33 -0700281 device_create_drvdata(aoe_class, NULL,
282 MKDEV(AOE_MAJOR, chardevs[i].minor),
283 NULL, chardevs[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 return 0;
286}
287
288void
289aoechr_exit(void)
290{
291 int i;
292
293 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
Tony Jones7ea7ed02007-09-25 02:03:03 +0200294 device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
gregkh@suse.dedeb36972005-03-23 09:52:10 -0800295 class_destroy(aoe_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 unregister_chrdev(AOE_MAJOR, "aoechr");
297}
298