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