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> |
Jonathan Corbet | 579174a | 2008-05-15 10:03:09 -0600 | [diff] [blame] | 11 | #include <linux/smp_lock.h> |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 12 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "aoe.h" |
| 14 | |
| 15 | enum { |
| 16 | //MINOR_STAT = 1, (moved to sysfs) |
| 17 | MINOR_ERR = 2, |
| 18 | MINOR_DISCOVER, |
| 19 | MINOR_INTERFACES, |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 20 | MINOR_REVALIDATE, |
Ed L. Cashin | 262bf54 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 21 | MINOR_FLUSH, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | MSGSZ = 2048, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | NMSG = 100, /* message backlog to retain */ |
| 24 | }; |
| 25 | |
| 26 | struct aoe_chardev { |
| 27 | ulong minor; |
| 28 | char name[32]; |
| 29 | }; |
| 30 | |
| 31 | enum { EMFL_VALID = 1 }; |
| 32 | |
| 33 | struct ErrMsg { |
| 34 | short flags; |
| 35 | short len; |
| 36 | char *msg; |
| 37 | }; |
| 38 | |
| 39 | static struct ErrMsg emsgs[NMSG]; |
| 40 | static int emsgs_head_idx, emsgs_tail_idx; |
Matthias Kaehlcke | 24879a8 | 2008-07-25 01:48:38 -0700 | [diff] [blame] | 41 | static struct completion emsgs_comp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | static spinlock_t emsgs_lock; |
| 43 | static int nblocked_emsgs_readers; |
gregkh@suse.de | deb3697 | 2005-03-23 09:52:10 -0800 | [diff] [blame] | 44 | static struct class *aoe_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | static struct aoe_chardev chardevs[] = { |
| 46 | { MINOR_ERR, "err" }, |
| 47 | { MINOR_DISCOVER, "discover" }, |
| 48 | { MINOR_INTERFACES, "interfaces" }, |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 49 | { MINOR_REVALIDATE, "revalidate" }, |
Ed L. Cashin | 262bf54 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 50 | { MINOR_FLUSH, "flush" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | static int |
| 54 | discover(void) |
| 55 | { |
| 56 | aoecmd_cfg(0xffff, 0xff); |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int |
| 61 | interfaces(const char __user *str, size_t size) |
| 62 | { |
| 63 | if (set_aoe_iflist(str, size)) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 64 | printk(KERN_ERR |
| 65 | "aoe: could not set interface list: too many interfaces\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | return -EINVAL; |
| 67 | } |
| 68 | return 0; |
| 69 | } |
| 70 | |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 71 | static int |
| 72 | revalidate(const char __user *str, size_t size) |
| 73 | { |
| 74 | int major, minor, n; |
| 75 | ulong flags; |
| 76 | struct aoedev *d; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 77 | struct sk_buff *skb; |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 78 | char buf[16]; |
| 79 | |
| 80 | if (size >= sizeof buf) |
| 81 | return -EINVAL; |
| 82 | buf[sizeof buf - 1] = '\0'; |
| 83 | if (copy_from_user(buf, str, size)) |
| 84 | return -EFAULT; |
| 85 | |
| 86 | /* should be e%d.%d format */ |
| 87 | n = sscanf(buf, "e%d.%d", &major, &minor); |
| 88 | if (n != 2) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 89 | printk(KERN_ERR "aoe: invalid device specification\n"); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 90 | return -EINVAL; |
| 91 | } |
| 92 | d = aoedev_by_aoeaddr(major, minor); |
| 93 | if (!d) |
| 94 | return -EINVAL; |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 95 | spin_lock_irqsave(&d->lock, flags); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 96 | aoecmd_cleanslate(d); |
| 97 | loop: |
| 98 | skb = aoecmd_ata_id(d); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 99 | spin_unlock_irqrestore(&d->lock, flags); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 100 | /* try again if we are able to sleep a bit, |
| 101 | * otherwise give up this revalidation |
| 102 | */ |
| 103 | if (!skb && !msleep_interruptible(200)) { |
| 104 | spin_lock_irqsave(&d->lock, flags); |
| 105 | goto loop; |
| 106 | } |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 107 | if (skb) { |
| 108 | struct sk_buff_head queue; |
| 109 | __skb_queue_head_init(&queue); |
| 110 | __skb_queue_tail(&queue, skb); |
| 111 | aoenet_xmit(&queue); |
| 112 | } |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 113 | aoecmd_cfg(major, minor); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | void |
| 118 | aoechr_error(char *msg) |
| 119 | { |
| 120 | struct ErrMsg *em; |
| 121 | char *mp; |
| 122 | ulong flags, n; |
| 123 | |
| 124 | n = strlen(msg); |
| 125 | |
| 126 | spin_lock_irqsave(&emsgs_lock, flags); |
| 127 | |
| 128 | em = emsgs + emsgs_tail_idx; |
| 129 | if ((em->flags & EMFL_VALID)) { |
| 130 | bail: spin_unlock_irqrestore(&emsgs_lock, flags); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | mp = kmalloc(n, GFP_ATOMIC); |
| 135 | if (mp == NULL) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 136 | printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | goto bail; |
| 138 | } |
| 139 | |
| 140 | memcpy(mp, msg, n); |
| 141 | em->msg = mp; |
| 142 | em->flags |= EMFL_VALID; |
| 143 | em->len = n; |
| 144 | |
| 145 | emsgs_tail_idx++; |
| 146 | emsgs_tail_idx %= ARRAY_SIZE(emsgs); |
| 147 | |
| 148 | spin_unlock_irqrestore(&emsgs_lock, flags); |
| 149 | |
| 150 | if (nblocked_emsgs_readers) |
Matthias Kaehlcke | 24879a8 | 2008-07-25 01:48:38 -0700 | [diff] [blame] | 151 | complete(&emsgs_comp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static ssize_t |
| 155 | aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp) |
| 156 | { |
| 157 | int ret = -EINVAL; |
| 158 | |
| 159 | switch ((unsigned long) filp->private_data) { |
| 160 | default: |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 161 | printk(KERN_INFO "aoe: can't write to that file.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | break; |
| 163 | case MINOR_DISCOVER: |
| 164 | ret = discover(); |
| 165 | break; |
| 166 | case MINOR_INTERFACES: |
| 167 | ret = interfaces(buf, cnt); |
| 168 | break; |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 169 | case MINOR_REVALIDATE: |
| 170 | ret = revalidate(buf, cnt); |
Ed L. Cashin | 262bf54 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 171 | break; |
| 172 | case MINOR_FLUSH: |
| 173 | ret = aoedev_flush(buf, cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | if (ret == 0) |
| 176 | ret = cnt; |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | static int |
| 181 | aoechr_open(struct inode *inode, struct file *filp) |
| 182 | { |
| 183 | int n, i; |
| 184 | |
Jonathan Corbet | 579174a | 2008-05-15 10:03:09 -0600 | [diff] [blame] | 185 | lock_kernel(); |
Eric Sesterhenn | 2017b37 | 2006-07-10 04:45:32 -0700 | [diff] [blame] | 186 | n = iminor(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | filp->private_data = (void *) (unsigned long) n; |
| 188 | |
| 189 | for (i = 0; i < ARRAY_SIZE(chardevs); ++i) |
Jonathan Corbet | 579174a | 2008-05-15 10:03:09 -0600 | [diff] [blame] | 190 | if (chardevs[i].minor == n) { |
| 191 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | return 0; |
Jonathan Corbet | 579174a | 2008-05-15 10:03:09 -0600 | [diff] [blame] | 193 | } |
| 194 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | return -EINVAL; |
| 196 | } |
| 197 | |
| 198 | static int |
| 199 | aoechr_rel(struct inode *inode, struct file *filp) |
| 200 | { |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static ssize_t |
| 205 | aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off) |
| 206 | { |
| 207 | unsigned long n; |
| 208 | char *mp; |
| 209 | struct ErrMsg *em; |
| 210 | ssize_t len; |
| 211 | ulong flags; |
| 212 | |
| 213 | n = (unsigned long) filp->private_data; |
Ed L. Cashin | cf446f0 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 214 | if (n != MINOR_ERR) |
| 215 | return -EFAULT; |
| 216 | |
| 217 | spin_lock_irqsave(&emsgs_lock, flags); |
| 218 | |
| 219 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | em = emsgs + emsgs_head_idx; |
Ed L. Cashin | cf446f0 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 221 | if ((em->flags & EMFL_VALID) != 0) |
| 222 | break; |
| 223 | if (filp->f_flags & O_NDELAY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | spin_unlock_irqrestore(&emsgs_lock, flags); |
| 225 | return -EAGAIN; |
| 226 | } |
Ed L. Cashin | cf446f0 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 227 | nblocked_emsgs_readers++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
| 229 | spin_unlock_irqrestore(&emsgs_lock, flags); |
| 230 | |
Matthias Kaehlcke | 24879a8 | 2008-07-25 01:48:38 -0700 | [diff] [blame] | 231 | n = wait_for_completion_interruptible(&emsgs_comp); |
Ed L. Cashin | cf446f0 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 232 | |
| 233 | spin_lock_irqsave(&emsgs_lock, flags); |
| 234 | |
| 235 | nblocked_emsgs_readers--; |
| 236 | |
| 237 | if (n) { |
| 238 | spin_unlock_irqrestore(&emsgs_lock, flags); |
| 239 | return -ERESTARTSYS; |
| 240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
Ed L. Cashin | cf446f0 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 242 | if (em->len > cnt) { |
| 243 | spin_unlock_irqrestore(&emsgs_lock, flags); |
| 244 | return -EAGAIN; |
| 245 | } |
| 246 | mp = em->msg; |
| 247 | len = em->len; |
| 248 | em->msg = NULL; |
| 249 | em->flags &= ~EMFL_VALID; |
| 250 | |
| 251 | emsgs_head_idx++; |
| 252 | emsgs_head_idx %= ARRAY_SIZE(emsgs); |
| 253 | |
| 254 | spin_unlock_irqrestore(&emsgs_lock, flags); |
| 255 | |
| 256 | n = copy_to_user(buf, mp, len); |
| 257 | kfree(mp); |
| 258 | return n == 0 ? len : -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 261 | static const struct file_operations aoe_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | .write = aoechr_write, |
| 263 | .read = aoechr_read, |
| 264 | .open = aoechr_open, |
| 265 | .release = aoechr_rel, |
| 266 | .owner = THIS_MODULE, |
| 267 | }; |
| 268 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame^] | 269 | static char *aoe_devnode(struct device *dev, mode_t *mode) |
Kay Sievers | 1ce8a0d | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 270 | { |
| 271 | return kasprintf(GFP_KERNEL, "etherd/%s", dev_name(dev)); |
| 272 | } |
| 273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | int __init |
| 275 | aoechr_init(void) |
| 276 | { |
| 277 | int n, i; |
| 278 | |
| 279 | n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops); |
| 280 | if (n < 0) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 281 | printk(KERN_ERR "aoe: can't register char device\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | return n; |
| 283 | } |
Matthias Kaehlcke | 24879a8 | 2008-07-25 01:48:38 -0700 | [diff] [blame] | 284 | init_completion(&emsgs_comp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | spin_lock_init(&emsgs_lock); |
gregkh@suse.de | deb3697 | 2005-03-23 09:52:10 -0800 | [diff] [blame] | 286 | aoe_class = class_create(THIS_MODULE, "aoe"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | if (IS_ERR(aoe_class)) { |
| 288 | unregister_chrdev(AOE_MAJOR, "aoechr"); |
| 289 | return PTR_ERR(aoe_class); |
| 290 | } |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame^] | 291 | aoe_class->devnode = aoe_devnode; |
Kay Sievers | 1ce8a0d | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | for (i = 0; i < ARRAY_SIZE(chardevs); ++i) |
Greg Kroah-Hartman | 1ff9f54 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 294 | device_create(aoe_class, NULL, |
| 295 | MKDEV(AOE_MAJOR, chardevs[i].minor), NULL, |
| 296 | chardevs[i].name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | void |
| 302 | aoechr_exit(void) |
| 303 | { |
| 304 | int i; |
| 305 | |
| 306 | for (i = 0; i < ARRAY_SIZE(chardevs); ++i) |
Tony Jones | 7ea7ed0 | 2007-09-25 02:03:03 +0200 | [diff] [blame] | 307 | device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor)); |
gregkh@suse.de | deb3697 | 2005-03-23 09:52:10 -0800 | [diff] [blame] | 308 | class_destroy(aoe_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | unregister_chrdev(AOE_MAJOR, "aoechr"); |
| 310 | } |
| 311 | |