Ed L. Cashin | 52e112b | 2008-02-08 04:20:09 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * aoechr.c |
| 4 | * AoE character device driver |
| 5 | */ |
| 6 | |
| 7 | #include <linux/hdreg.h> |
| 8 | #include <linux/blkdev.h> |
Matthias Kaehlcke | 24879a8 | 2008-07-25 01:48:38 -0700 | [diff] [blame] | 9 | #include <linux/completion.h> |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 10 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Jonathan Corbet | 579174a | 2008-05-15 10:03:09 -0600 | [diff] [blame] | 12 | #include <linux/smp_lock.h> |
David S. Miller | e9bb8fb | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 13 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "aoe.h" |
| 15 | |
| 16 | enum { |
| 17 | //MINOR_STAT = 1, (moved to sysfs) |
| 18 | MINOR_ERR = 2, |
| 19 | MINOR_DISCOVER, |
| 20 | MINOR_INTERFACES, |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 21 | MINOR_REVALIDATE, |
Ed L. Cashin | 262bf54 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 22 | MINOR_FLUSH, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | MSGSZ = 2048, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | NMSG = 100, /* message backlog to retain */ |
| 25 | }; |
| 26 | |
| 27 | struct aoe_chardev { |
| 28 | ulong minor; |
| 29 | char name[32]; |
| 30 | }; |
| 31 | |
| 32 | enum { EMFL_VALID = 1 }; |
| 33 | |
| 34 | struct ErrMsg { |
| 35 | short flags; |
| 36 | short len; |
| 37 | char *msg; |
| 38 | }; |
| 39 | |
| 40 | static struct ErrMsg emsgs[NMSG]; |
| 41 | static int emsgs_head_idx, emsgs_tail_idx; |
Matthias Kaehlcke | 24879a8 | 2008-07-25 01:48:38 -0700 | [diff] [blame] | 42 | static struct completion emsgs_comp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | static spinlock_t emsgs_lock; |
| 44 | static int nblocked_emsgs_readers; |
gregkh@suse.de | deb3697 | 2005-03-23 09:52:10 -0800 | [diff] [blame] | 45 | static struct class *aoe_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | static struct aoe_chardev chardevs[] = { |
| 47 | { MINOR_ERR, "err" }, |
| 48 | { MINOR_DISCOVER, "discover" }, |
| 49 | { MINOR_INTERFACES, "interfaces" }, |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 50 | { MINOR_REVALIDATE, "revalidate" }, |
Ed L. Cashin | 262bf54 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 51 | { MINOR_FLUSH, "flush" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | static int |
| 55 | discover(void) |
| 56 | { |
| 57 | aoecmd_cfg(0xffff, 0xff); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static int |
| 62 | interfaces(const char __user *str, size_t size) |
| 63 | { |
| 64 | if (set_aoe_iflist(str, size)) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 65 | printk(KERN_ERR |
| 66 | "aoe: could not set interface list: too many interfaces\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | return -EINVAL; |
| 68 | } |
| 69 | return 0; |
| 70 | } |
| 71 | |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 72 | static int |
| 73 | revalidate(const char __user *str, size_t size) |
| 74 | { |
| 75 | int major, minor, n; |
| 76 | ulong flags; |
| 77 | struct aoedev *d; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 78 | struct sk_buff *skb; |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 79 | 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. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 90 | printk(KERN_ERR "aoe: invalid device specification\n"); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 91 | return -EINVAL; |
| 92 | } |
| 93 | d = aoedev_by_aoeaddr(major, minor); |
| 94 | if (!d) |
| 95 | return -EINVAL; |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 96 | spin_lock_irqsave(&d->lock, flags); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 97 | aoecmd_cleanslate(d); |
| 98 | loop: |
| 99 | skb = aoecmd_ata_id(d); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 100 | spin_unlock_irqrestore(&d->lock, flags); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 101 | /* 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. Miller | e9bb8fb | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 108 | 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. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 114 | aoecmd_cfg(major, minor); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | void |
| 119 | aoechr_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)) { |
| 131 | bail: spin_unlock_irqrestore(&emsgs_lock, flags); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | mp = kmalloc(n, GFP_ATOMIC); |
| 136 | if (mp == NULL) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 137 | printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | 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 Kaehlcke | 24879a8 | 2008-07-25 01:48:38 -0700 | [diff] [blame] | 152 | complete(&emsgs_comp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static ssize_t |
| 156 | aoechr_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. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 162 | printk(KERN_INFO "aoe: can't write to that file.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | break; |
| 164 | case MINOR_DISCOVER: |
| 165 | ret = discover(); |
| 166 | break; |
| 167 | case MINOR_INTERFACES: |
| 168 | ret = interfaces(buf, cnt); |
| 169 | break; |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 170 | case MINOR_REVALIDATE: |
| 171 | ret = revalidate(buf, cnt); |
Ed L. Cashin | 262bf54 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 172 | break; |
| 173 | case MINOR_FLUSH: |
| 174 | ret = aoedev_flush(buf, cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | if (ret == 0) |
| 177 | ret = cnt; |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | static int |
| 182 | aoechr_open(struct inode *inode, struct file *filp) |
| 183 | { |
| 184 | int n, i; |
| 185 | |
Jonathan Corbet | 579174a | 2008-05-15 10:03:09 -0600 | [diff] [blame] | 186 | lock_kernel(); |
Eric Sesterhenn | 2017b37 | 2006-07-10 04:45:32 -0700 | [diff] [blame] | 187 | n = iminor(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | filp->private_data = (void *) (unsigned long) n; |
| 189 | |
| 190 | for (i = 0; i < ARRAY_SIZE(chardevs); ++i) |
Jonathan Corbet | 579174a | 2008-05-15 10:03:09 -0600 | [diff] [blame] | 191 | if (chardevs[i].minor == n) { |
| 192 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | return 0; |
Jonathan Corbet | 579174a | 2008-05-15 10:03:09 -0600 | [diff] [blame] | 194 | } |
| 195 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | return -EINVAL; |
| 197 | } |
| 198 | |
| 199 | static int |
| 200 | aoechr_rel(struct inode *inode, struct file *filp) |
| 201 | { |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static ssize_t |
| 206 | aoechr_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. Cashin | cf446f0 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 215 | if (n != MINOR_ERR) |
| 216 | return -EFAULT; |
| 217 | |
| 218 | spin_lock_irqsave(&emsgs_lock, flags); |
| 219 | |
| 220 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | em = emsgs + emsgs_head_idx; |
Ed L. Cashin | cf446f0 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 222 | if ((em->flags & EMFL_VALID) != 0) |
| 223 | break; |
| 224 | if (filp->f_flags & O_NDELAY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | spin_unlock_irqrestore(&emsgs_lock, flags); |
| 226 | return -EAGAIN; |
| 227 | } |
Ed L. Cashin | cf446f0 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 228 | nblocked_emsgs_readers++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | spin_unlock_irqrestore(&emsgs_lock, flags); |
| 231 | |
Matthias Kaehlcke | 24879a8 | 2008-07-25 01:48:38 -0700 | [diff] [blame] | 232 | n = wait_for_completion_interruptible(&emsgs_comp); |
Ed L. Cashin | cf446f0 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 233 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
Ed L. Cashin | cf446f0 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 243 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 262 | static const struct file_operations aoe_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | .write = aoechr_write, |
| 264 | .read = aoechr_read, |
| 265 | .open = aoechr_open, |
| 266 | .release = aoechr_rel, |
| 267 | .owner = THIS_MODULE, |
| 268 | }; |
| 269 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 270 | static char *aoe_devnode(struct device *dev, mode_t *mode) |
Kay Sievers | 1ce8a0d | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 271 | { |
| 272 | return kasprintf(GFP_KERNEL, "etherd/%s", dev_name(dev)); |
| 273 | } |
| 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | int __init |
| 276 | aoechr_init(void) |
| 277 | { |
| 278 | int n, i; |
| 279 | |
| 280 | n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops); |
| 281 | if (n < 0) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 282 | printk(KERN_ERR "aoe: can't register char device\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | return n; |
| 284 | } |
Matthias Kaehlcke | 24879a8 | 2008-07-25 01:48:38 -0700 | [diff] [blame] | 285 | init_completion(&emsgs_comp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | spin_lock_init(&emsgs_lock); |
gregkh@suse.de | deb3697 | 2005-03-23 09:52:10 -0800 | [diff] [blame] | 287 | aoe_class = class_create(THIS_MODULE, "aoe"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | if (IS_ERR(aoe_class)) { |
| 289 | unregister_chrdev(AOE_MAJOR, "aoechr"); |
| 290 | return PTR_ERR(aoe_class); |
| 291 | } |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 292 | aoe_class->devnode = aoe_devnode; |
Kay Sievers | 1ce8a0d | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | for (i = 0; i < ARRAY_SIZE(chardevs); ++i) |
Greg Kroah-Hartman | 1ff9f54 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 295 | device_create(aoe_class, NULL, |
| 296 | MKDEV(AOE_MAJOR, chardevs[i].minor), NULL, |
| 297 | chardevs[i].name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | void |
| 303 | aoechr_exit(void) |
| 304 | { |
| 305 | int i; |
| 306 | |
| 307 | for (i = 0; i < ARRAY_SIZE(chardevs); ++i) |
Tony Jones | 7ea7ed0 | 2007-09-25 02:03:03 +0200 | [diff] [blame] | 308 | device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor)); |
gregkh@suse.de | deb3697 | 2005-03-23 09:52:10 -0800 | [diff] [blame] | 309 | class_destroy(aoe_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | unregister_chrdev(AOE_MAJOR, "aoechr"); |
| 311 | } |
| 312 | |