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