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