Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Linux driver for System z and s390 unit record devices |
| 3 | * (z/VM virtual punch, reader, printer) |
| 4 | * |
| 5 | * Copyright IBM Corp. 2001, 2007 |
| 6 | * Authors: Malcolm Beattie <beattiem@uk.ibm.com> |
| 7 | * Michael Holzheu <holzheu@de.ibm.com> |
| 8 | * Frank Munzert <munzert@de.ibm.com> |
| 9 | */ |
| 10 | |
| 11 | #include <linux/cdev.h> |
Jonathan Corbet | 764a4a8 | 2008-05-15 10:01:17 -0600 | [diff] [blame] | 12 | #include <linux/smp_lock.h> |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 13 | |
| 14 | #include <asm/uaccess.h> |
| 15 | #include <asm/cio.h> |
| 16 | #include <asm/ccwdev.h> |
| 17 | #include <asm/debug.h> |
Michael Holzheu | 0a87c5c | 2007-08-22 13:51:40 +0200 | [diff] [blame] | 18 | #include <asm/diag.h> |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 19 | |
| 20 | #include "vmur.h" |
| 21 | |
| 22 | /* |
| 23 | * Driver overview |
| 24 | * |
| 25 | * Unit record device support is implemented as a character device driver. |
| 26 | * We can fit at least 16 bits into a device minor number and use the |
| 27 | * simple method of mapping a character device number with minor abcd |
| 28 | * to the unit record device with devno abcd. |
| 29 | * I/O to virtual unit record devices is handled as follows: |
| 30 | * Reads: Diagnose code 0x14 (input spool file manipulation) |
| 31 | * is used to read spool data page-wise. |
| 32 | * Writes: The CCW used is WRITE_CCW_CMD (0x01). The device's record length |
| 33 | * is available by reading sysfs attr reclen. Each write() to the device |
| 34 | * must specify an integral multiple (maximal 511) of reclen. |
| 35 | */ |
| 36 | |
| 37 | static char ur_banner[] = "z/VM virtual unit record device driver"; |
| 38 | |
| 39 | MODULE_AUTHOR("IBM Corporation"); |
| 40 | MODULE_DESCRIPTION("s390 z/VM virtual unit record device driver"); |
| 41 | MODULE_LICENSE("GPL"); |
| 42 | |
| 43 | #define PRINTK_HEADER "vmur: " |
| 44 | |
| 45 | static dev_t ur_first_dev_maj_min; |
| 46 | static struct class *vmur_class; |
| 47 | static struct debug_info *vmur_dbf; |
| 48 | |
| 49 | /* We put the device's record length (for writes) in the driver_info field */ |
| 50 | static struct ccw_device_id ur_ids[] = { |
| 51 | { CCWDEV_CU_DI(READER_PUNCH_DEVTYPE, 80) }, |
| 52 | { CCWDEV_CU_DI(PRINTER_DEVTYPE, 132) }, |
| 53 | { /* end of list */ } |
| 54 | }; |
| 55 | |
| 56 | MODULE_DEVICE_TABLE(ccw, ur_ids); |
| 57 | |
| 58 | static int ur_probe(struct ccw_device *cdev); |
| 59 | static void ur_remove(struct ccw_device *cdev); |
| 60 | static int ur_set_online(struct ccw_device *cdev); |
| 61 | static int ur_set_offline(struct ccw_device *cdev); |
| 62 | |
| 63 | static struct ccw_driver ur_driver = { |
| 64 | .name = "vmur", |
| 65 | .owner = THIS_MODULE, |
| 66 | .ids = ur_ids, |
| 67 | .probe = ur_probe, |
| 68 | .remove = ur_remove, |
| 69 | .set_online = ur_set_online, |
| 70 | .set_offline = ur_set_offline, |
| 71 | }; |
| 72 | |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 73 | static DEFINE_MUTEX(vmur_mutex); |
| 74 | |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 75 | /* |
| 76 | * Allocation, freeing, getting and putting of urdev structures |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 77 | * |
| 78 | * Each ur device (urd) contains a reference to its corresponding ccw device |
| 79 | * (cdev) using the urd->cdev pointer. Each ccw device has a reference to the |
| 80 | * ur device using the cdev->dev.driver_data pointer. |
| 81 | * |
| 82 | * urd references: |
| 83 | * - ur_probe gets a urd reference, ur_remove drops the reference |
| 84 | * (cdev->dev.driver_data) |
| 85 | * - ur_open gets a urd reference, ur_relase drops the reference |
| 86 | * (urf->urd) |
| 87 | * |
| 88 | * cdev references: |
| 89 | * - urdev_alloc get a cdev reference (urd->cdev) |
| 90 | * - urdev_free drops the cdev reference (urd->cdev) |
| 91 | * |
| 92 | * Setting and clearing of cdev->dev.driver_data is protected by the ccwdev lock |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 93 | */ |
| 94 | static struct urdev *urdev_alloc(struct ccw_device *cdev) |
| 95 | { |
| 96 | struct urdev *urd; |
| 97 | |
| 98 | urd = kzalloc(sizeof(struct urdev), GFP_KERNEL); |
| 99 | if (!urd) |
| 100 | return NULL; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 101 | urd->reclen = cdev->id.driver_info; |
| 102 | ccw_device_get_id(cdev, &urd->dev_id); |
| 103 | mutex_init(&urd->io_mutex); |
Frank Munzert | a695f16 | 2008-04-17 07:46:06 +0200 | [diff] [blame] | 104 | init_waitqueue_head(&urd->wait); |
| 105 | spin_lock_init(&urd->open_lock); |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 106 | atomic_set(&urd->ref_count, 1); |
| 107 | urd->cdev = cdev; |
| 108 | get_device(&cdev->dev); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 109 | return urd; |
| 110 | } |
| 111 | |
| 112 | static void urdev_free(struct urdev *urd) |
| 113 | { |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 114 | TRACE("urdev_free: %p\n", urd); |
| 115 | if (urd->cdev) |
| 116 | put_device(&urd->cdev->dev); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 117 | kfree(urd); |
| 118 | } |
| 119 | |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 120 | static void urdev_get(struct urdev *urd) |
| 121 | { |
| 122 | atomic_inc(&urd->ref_count); |
| 123 | } |
| 124 | |
| 125 | static struct urdev *urdev_get_from_cdev(struct ccw_device *cdev) |
| 126 | { |
| 127 | struct urdev *urd; |
| 128 | unsigned long flags; |
| 129 | |
| 130 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); |
| 131 | urd = cdev->dev.driver_data; |
| 132 | if (urd) |
| 133 | urdev_get(urd); |
| 134 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
| 135 | return urd; |
| 136 | } |
| 137 | |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 138 | static struct urdev *urdev_get_from_devno(u16 devno) |
| 139 | { |
| 140 | char bus_id[16]; |
| 141 | struct ccw_device *cdev; |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 142 | struct urdev *urd; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 143 | |
| 144 | sprintf(bus_id, "0.0.%04x", devno); |
| 145 | cdev = get_ccwdev_by_busid(&ur_driver, bus_id); |
| 146 | if (!cdev) |
| 147 | return NULL; |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 148 | urd = urdev_get_from_cdev(cdev); |
| 149 | put_device(&cdev->dev); |
| 150 | return urd; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void urdev_put(struct urdev *urd) |
| 154 | { |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 155 | if (atomic_dec_and_test(&urd->ref_count)) |
| 156 | urdev_free(urd); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Low-level functions to do I/O to a ur device. |
| 161 | * alloc_chan_prog |
Michael Holzheu | 1eade38 | 2007-08-10 14:32:30 +0200 | [diff] [blame] | 162 | * free_chan_prog |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 163 | * do_ur_io |
| 164 | * ur_int_handler |
| 165 | * |
| 166 | * alloc_chan_prog allocates and builds the channel program |
Michael Holzheu | 1eade38 | 2007-08-10 14:32:30 +0200 | [diff] [blame] | 167 | * free_chan_prog frees memory of the channel program |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 168 | * |
| 169 | * do_ur_io issues the channel program to the device and blocks waiting |
| 170 | * on a completion event it publishes at urd->io_done. The function |
| 171 | * serialises itself on the device's mutex so that only one I/O |
| 172 | * is issued at a time (and that I/O is synchronous). |
| 173 | * |
| 174 | * ur_int_handler catches the "I/O done" interrupt, writes the |
| 175 | * subchannel status word into the scsw member of the urdev structure |
| 176 | * and complete()s the io_done to wake the waiting do_ur_io. |
| 177 | * |
| 178 | * The caller of do_ur_io is responsible for kfree()ing the channel program |
| 179 | * address pointer that alloc_chan_prog returned. |
| 180 | */ |
| 181 | |
Michael Holzheu | 1eade38 | 2007-08-10 14:32:30 +0200 | [diff] [blame] | 182 | static void free_chan_prog(struct ccw1 *cpa) |
| 183 | { |
| 184 | struct ccw1 *ptr = cpa; |
| 185 | |
| 186 | while (ptr->cda) { |
| 187 | kfree((void *)(addr_t) ptr->cda); |
| 188 | ptr++; |
| 189 | } |
| 190 | kfree(cpa); |
| 191 | } |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 192 | |
| 193 | /* |
| 194 | * alloc_chan_prog |
| 195 | * The channel program we use is write commands chained together |
| 196 | * with a final NOP CCW command-chained on (which ensures that CE and DE |
| 197 | * are presented together in a single interrupt instead of as separate |
| 198 | * interrupts unless an incorrect length indication kicks in first). The |
Michael Holzheu | 1eade38 | 2007-08-10 14:32:30 +0200 | [diff] [blame] | 199 | * data length in each CCW is reclen. |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 200 | */ |
Michael Holzheu | 1eade38 | 2007-08-10 14:32:30 +0200 | [diff] [blame] | 201 | static struct ccw1 *alloc_chan_prog(const char __user *ubuf, int rec_count, |
| 202 | int reclen) |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 203 | { |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 204 | struct ccw1 *cpa; |
Michael Holzheu | 1eade38 | 2007-08-10 14:32:30 +0200 | [diff] [blame] | 205 | void *kbuf; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 206 | int i; |
| 207 | |
Michael Holzheu | 1eade38 | 2007-08-10 14:32:30 +0200 | [diff] [blame] | 208 | TRACE("alloc_chan_prog(%p, %i, %i)\n", ubuf, rec_count, reclen); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * We chain a NOP onto the writes to force CE+DE together. |
| 212 | * That means we allocate room for CCWs to cover count/reclen |
| 213 | * records plus a NOP. |
| 214 | */ |
Michael Holzheu | 1eade38 | 2007-08-10 14:32:30 +0200 | [diff] [blame] | 215 | cpa = kzalloc((rec_count + 1) * sizeof(struct ccw1), |
| 216 | GFP_KERNEL | GFP_DMA); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 217 | if (!cpa) |
Michael Holzheu | 1eade38 | 2007-08-10 14:32:30 +0200 | [diff] [blame] | 218 | return ERR_PTR(-ENOMEM); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 219 | |
Michael Holzheu | 1eade38 | 2007-08-10 14:32:30 +0200 | [diff] [blame] | 220 | for (i = 0; i < rec_count; i++) { |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 221 | cpa[i].cmd_code = WRITE_CCW_CMD; |
| 222 | cpa[i].flags = CCW_FLAG_CC | CCW_FLAG_SLI; |
| 223 | cpa[i].count = reclen; |
Michael Holzheu | 1eade38 | 2007-08-10 14:32:30 +0200 | [diff] [blame] | 224 | kbuf = kmalloc(reclen, GFP_KERNEL | GFP_DMA); |
| 225 | if (!kbuf) { |
| 226 | free_chan_prog(cpa); |
| 227 | return ERR_PTR(-ENOMEM); |
| 228 | } |
| 229 | cpa[i].cda = (u32)(addr_t) kbuf; |
| 230 | if (copy_from_user(kbuf, ubuf, reclen)) { |
| 231 | free_chan_prog(cpa); |
| 232 | return ERR_PTR(-EFAULT); |
| 233 | } |
| 234 | ubuf += reclen; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 235 | } |
| 236 | /* The following NOP CCW forces CE+DE to be presented together */ |
| 237 | cpa[i].cmd_code = CCW_CMD_NOOP; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 238 | return cpa; |
| 239 | } |
| 240 | |
| 241 | static int do_ur_io(struct urdev *urd, struct ccw1 *cpa) |
| 242 | { |
| 243 | int rc; |
| 244 | struct ccw_device *cdev = urd->cdev; |
Heiko Carstens | 278bc68 | 2007-08-10 14:32:31 +0200 | [diff] [blame] | 245 | DECLARE_COMPLETION_ONSTACK(event); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 246 | |
| 247 | TRACE("do_ur_io: cpa=%p\n", cpa); |
| 248 | |
| 249 | rc = mutex_lock_interruptible(&urd->io_mutex); |
| 250 | if (rc) |
| 251 | return rc; |
| 252 | |
| 253 | urd->io_done = &event; |
| 254 | |
| 255 | spin_lock_irq(get_ccwdev_lock(cdev)); |
| 256 | rc = ccw_device_start(cdev, cpa, 1, 0, 0); |
| 257 | spin_unlock_irq(get_ccwdev_lock(cdev)); |
| 258 | |
| 259 | TRACE("do_ur_io: ccw_device_start returned %d\n", rc); |
| 260 | if (rc) |
| 261 | goto out; |
| 262 | |
| 263 | wait_for_completion(&event); |
| 264 | TRACE("do_ur_io: I/O complete\n"); |
| 265 | rc = 0; |
| 266 | |
| 267 | out: |
| 268 | mutex_unlock(&urd->io_mutex); |
| 269 | return rc; |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * ur interrupt handler, called from the ccw_device layer |
| 274 | */ |
| 275 | static void ur_int_handler(struct ccw_device *cdev, unsigned long intparm, |
| 276 | struct irb *irb) |
| 277 | { |
| 278 | struct urdev *urd; |
| 279 | |
| 280 | TRACE("ur_int_handler: intparm=0x%lx cstat=%02x dstat=%02x res=%u\n", |
Peter Oberparleiter | 23d805b | 2008-07-14 09:58:50 +0200 | [diff] [blame] | 281 | intparm, irb->scsw.cmd.cstat, irb->scsw.cmd.dstat, |
| 282 | irb->scsw.cmd.count); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 283 | |
| 284 | if (!intparm) { |
| 285 | TRACE("ur_int_handler: unsolicited interrupt\n"); |
| 286 | return; |
| 287 | } |
| 288 | urd = cdev->dev.driver_data; |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 289 | BUG_ON(!urd); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 290 | /* On special conditions irb is an error pointer */ |
| 291 | if (IS_ERR(irb)) |
| 292 | urd->io_request_rc = PTR_ERR(irb); |
Peter Oberparleiter | 23d805b | 2008-07-14 09:58:50 +0200 | [diff] [blame] | 293 | else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END)) |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 294 | urd->io_request_rc = 0; |
| 295 | else |
| 296 | urd->io_request_rc = -EIO; |
| 297 | |
| 298 | complete(urd->io_done); |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * reclen sysfs attribute - The record length to be used for write CCWs |
| 303 | */ |
| 304 | static ssize_t ur_attr_reclen_show(struct device *dev, |
| 305 | struct device_attribute *attr, char *buf) |
| 306 | { |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 307 | struct urdev *urd; |
| 308 | int rc; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 309 | |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 310 | urd = urdev_get_from_cdev(to_ccwdev(dev)); |
| 311 | if (!urd) |
| 312 | return -ENODEV; |
| 313 | rc = sprintf(buf, "%zu\n", urd->reclen); |
| 314 | urdev_put(urd); |
| 315 | return rc; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static DEVICE_ATTR(reclen, 0444, ur_attr_reclen_show, NULL); |
| 319 | |
| 320 | static int ur_create_attributes(struct device *dev) |
| 321 | { |
| 322 | return device_create_file(dev, &dev_attr_reclen); |
| 323 | } |
| 324 | |
| 325 | static void ur_remove_attributes(struct device *dev) |
| 326 | { |
| 327 | device_remove_file(dev, &dev_attr_reclen); |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * diagnose code 0x210 - retrieve device information |
| 332 | * cc=0 normal completion, we have a real device |
| 333 | * cc=1 CP paging error |
| 334 | * cc=2 The virtual device exists, but is not associated with a real device |
| 335 | * cc=3 Invalid device address, or the virtual device does not exist |
| 336 | */ |
| 337 | static int get_urd_class(struct urdev *urd) |
| 338 | { |
| 339 | static struct diag210 ur_diag210; |
| 340 | int cc; |
| 341 | |
| 342 | ur_diag210.vrdcdvno = urd->dev_id.devno; |
| 343 | ur_diag210.vrdclen = sizeof(struct diag210); |
| 344 | |
| 345 | cc = diag210(&ur_diag210); |
| 346 | switch (cc) { |
| 347 | case 0: |
| 348 | return -ENOTSUPP; |
| 349 | case 2: |
| 350 | return ur_diag210.vrdcvcla; /* virtual device class */ |
| 351 | case 3: |
| 352 | return -ENODEV; |
| 353 | default: |
| 354 | return -EIO; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Allocation and freeing of urfile structures |
| 360 | */ |
| 361 | static struct urfile *urfile_alloc(struct urdev *urd) |
| 362 | { |
| 363 | struct urfile *urf; |
| 364 | |
| 365 | urf = kzalloc(sizeof(struct urfile), GFP_KERNEL); |
| 366 | if (!urf) |
| 367 | return NULL; |
| 368 | urf->urd = urd; |
| 369 | |
| 370 | TRACE("urfile_alloc: urd=%p urf=%p rl=%zu\n", urd, urf, |
| 371 | urf->dev_reclen); |
| 372 | |
| 373 | return urf; |
| 374 | } |
| 375 | |
| 376 | static void urfile_free(struct urfile *urf) |
| 377 | { |
| 378 | TRACE("urfile_free: urf=%p urd=%p\n", urf, urf->urd); |
| 379 | kfree(urf); |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * The fops implementation of the character device driver |
| 384 | */ |
| 385 | static ssize_t do_write(struct urdev *urd, const char __user *udata, |
| 386 | size_t count, size_t reclen, loff_t *ppos) |
| 387 | { |
| 388 | struct ccw1 *cpa; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 389 | int rc; |
| 390 | |
Michael Holzheu | 1eade38 | 2007-08-10 14:32:30 +0200 | [diff] [blame] | 391 | cpa = alloc_chan_prog(udata, count / reclen, reclen); |
| 392 | if (IS_ERR(cpa)) |
| 393 | return PTR_ERR(cpa); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 394 | |
| 395 | rc = do_ur_io(urd, cpa); |
| 396 | if (rc) |
| 397 | goto fail_kfree_cpa; |
| 398 | |
| 399 | if (urd->io_request_rc) { |
| 400 | rc = urd->io_request_rc; |
| 401 | goto fail_kfree_cpa; |
| 402 | } |
| 403 | *ppos += count; |
| 404 | rc = count; |
Michael Holzheu | 1eade38 | 2007-08-10 14:32:30 +0200 | [diff] [blame] | 405 | |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 406 | fail_kfree_cpa: |
Michael Holzheu | 1eade38 | 2007-08-10 14:32:30 +0200 | [diff] [blame] | 407 | free_chan_prog(cpa); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 408 | return rc; |
| 409 | } |
| 410 | |
| 411 | static ssize_t ur_write(struct file *file, const char __user *udata, |
| 412 | size_t count, loff_t *ppos) |
| 413 | { |
| 414 | struct urfile *urf = file->private_data; |
| 415 | |
| 416 | TRACE("ur_write: count=%zu\n", count); |
| 417 | |
| 418 | if (count == 0) |
| 419 | return 0; |
| 420 | |
| 421 | if (count % urf->dev_reclen) |
| 422 | return -EINVAL; /* count must be a multiple of reclen */ |
| 423 | |
| 424 | if (count > urf->dev_reclen * MAX_RECS_PER_IO) |
| 425 | count = urf->dev_reclen * MAX_RECS_PER_IO; |
| 426 | |
| 427 | return do_write(urf->urd, udata, count, urf->dev_reclen, ppos); |
| 428 | } |
| 429 | |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 430 | /* |
| 431 | * diagnose code 0x14 subcode 0x0028 - position spool file to designated |
| 432 | * record |
| 433 | * cc=0 normal completion |
| 434 | * cc=2 no file active on the virtual reader or device not ready |
| 435 | * cc=3 record specified is beyond EOF |
| 436 | */ |
| 437 | static int diag_position_to_record(int devno, int record) |
| 438 | { |
| 439 | int cc; |
| 440 | |
Michael Holzheu | 0a87c5c | 2007-08-22 13:51:40 +0200 | [diff] [blame] | 441 | cc = diag14(record, devno, 0x28); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 442 | switch (cc) { |
| 443 | case 0: |
| 444 | return 0; |
| 445 | case 2: |
| 446 | return -ENOMEDIUM; |
| 447 | case 3: |
| 448 | return -ENODATA; /* position beyond end of file */ |
| 449 | default: |
| 450 | return -EIO; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * diagnose code 0x14 subcode 0x0000 - read next spool file buffer |
| 456 | * cc=0 normal completion |
| 457 | * cc=1 EOF reached |
| 458 | * cc=2 no file active on the virtual reader, and no file eligible |
| 459 | * cc=3 file already active on the virtual reader or specified virtual |
| 460 | * reader does not exist or is not a reader |
| 461 | */ |
| 462 | static int diag_read_file(int devno, char *buf) |
| 463 | { |
| 464 | int cc; |
| 465 | |
Michael Holzheu | 0a87c5c | 2007-08-22 13:51:40 +0200 | [diff] [blame] | 466 | cc = diag14((unsigned long) buf, devno, 0x00); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 467 | switch (cc) { |
| 468 | case 0: |
| 469 | return 0; |
| 470 | case 1: |
| 471 | return -ENODATA; |
| 472 | case 2: |
| 473 | return -ENOMEDIUM; |
| 474 | default: |
| 475 | return -EIO; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | static ssize_t diag14_read(struct file *file, char __user *ubuf, size_t count, |
| 480 | loff_t *offs) |
| 481 | { |
| 482 | size_t len, copied, res; |
| 483 | char *buf; |
| 484 | int rc; |
| 485 | u16 reclen; |
| 486 | struct urdev *urd; |
| 487 | |
| 488 | urd = ((struct urfile *) file->private_data)->urd; |
| 489 | reclen = ((struct urfile *) file->private_data)->file_reclen; |
| 490 | |
| 491 | rc = diag_position_to_record(urd->dev_id.devno, *offs / PAGE_SIZE + 1); |
| 492 | if (rc == -ENODATA) |
| 493 | return 0; |
| 494 | if (rc) |
| 495 | return rc; |
| 496 | |
| 497 | len = min((size_t) PAGE_SIZE, count); |
Michael Holzheu | 3eed13c | 2007-08-10 14:32:34 +0200 | [diff] [blame] | 498 | buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 499 | if (!buf) |
| 500 | return -ENOMEM; |
| 501 | |
| 502 | copied = 0; |
| 503 | res = (size_t) (*offs % PAGE_SIZE); |
| 504 | do { |
| 505 | rc = diag_read_file(urd->dev_id.devno, buf); |
| 506 | if (rc == -ENODATA) { |
| 507 | break; |
| 508 | } |
| 509 | if (rc) |
| 510 | goto fail; |
Frank Munzert | 2b3d8c9 | 2007-07-27 12:29:17 +0200 | [diff] [blame] | 511 | if (reclen && (copied == 0) && (*offs < PAGE_SIZE)) |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 512 | *((u16 *) &buf[FILE_RECLEN_OFFSET]) = reclen; |
| 513 | len = min(count - copied, PAGE_SIZE - res); |
| 514 | if (copy_to_user(ubuf + copied, buf + res, len)) { |
| 515 | rc = -EFAULT; |
| 516 | goto fail; |
| 517 | } |
| 518 | res = 0; |
| 519 | copied += len; |
| 520 | } while (copied != count); |
| 521 | |
| 522 | *offs += copied; |
| 523 | rc = copied; |
| 524 | fail: |
Michael Holzheu | 3eed13c | 2007-08-10 14:32:34 +0200 | [diff] [blame] | 525 | free_page((unsigned long) buf); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 526 | return rc; |
| 527 | } |
| 528 | |
| 529 | static ssize_t ur_read(struct file *file, char __user *ubuf, size_t count, |
| 530 | loff_t *offs) |
| 531 | { |
| 532 | struct urdev *urd; |
| 533 | int rc; |
| 534 | |
| 535 | TRACE("ur_read: count=%zu ppos=%li\n", count, (unsigned long) *offs); |
| 536 | |
| 537 | if (count == 0) |
| 538 | return 0; |
| 539 | |
| 540 | urd = ((struct urfile *) file->private_data)->urd; |
| 541 | rc = mutex_lock_interruptible(&urd->io_mutex); |
| 542 | if (rc) |
| 543 | return rc; |
| 544 | rc = diag14_read(file, ubuf, count, offs); |
| 545 | mutex_unlock(&urd->io_mutex); |
| 546 | return rc; |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * diagnose code 0x14 subcode 0x0fff - retrieve next file descriptor |
| 551 | * cc=0 normal completion |
| 552 | * cc=1 no files on reader queue or no subsequent file |
| 553 | * cc=2 spid specified is invalid |
| 554 | */ |
| 555 | static int diag_read_next_file_info(struct file_control_block *buf, int spid) |
| 556 | { |
| 557 | int cc; |
| 558 | |
Michael Holzheu | 0a87c5c | 2007-08-22 13:51:40 +0200 | [diff] [blame] | 559 | cc = diag14((unsigned long) buf, spid, 0xfff); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 560 | switch (cc) { |
| 561 | case 0: |
| 562 | return 0; |
| 563 | default: |
| 564 | return -ENODATA; |
| 565 | } |
| 566 | } |
| 567 | |
Michael Holzheu | 3eed13c | 2007-08-10 14:32:34 +0200 | [diff] [blame] | 568 | static int verify_uri_device(struct urdev *urd) |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 569 | { |
Michael Holzheu | 3eed13c | 2007-08-10 14:32:34 +0200 | [diff] [blame] | 570 | struct file_control_block *fcb; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 571 | char *buf; |
| 572 | int rc; |
| 573 | |
Michael Holzheu | 3eed13c | 2007-08-10 14:32:34 +0200 | [diff] [blame] | 574 | fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA); |
| 575 | if (!fcb) |
| 576 | return -ENOMEM; |
| 577 | |
| 578 | /* check for empty reader device (beginning of chain) */ |
| 579 | rc = diag_read_next_file_info(fcb, 0); |
| 580 | if (rc) |
| 581 | goto fail_free_fcb; |
| 582 | |
| 583 | /* if file is in hold status, we do not read it */ |
| 584 | if (fcb->file_stat & (FLG_SYSTEM_HOLD | FLG_USER_HOLD)) { |
| 585 | rc = -EPERM; |
| 586 | goto fail_free_fcb; |
| 587 | } |
| 588 | |
| 589 | /* open file on virtual reader */ |
| 590 | buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA); |
| 591 | if (!buf) { |
| 592 | rc = -ENOMEM; |
| 593 | goto fail_free_fcb; |
| 594 | } |
| 595 | rc = diag_read_file(urd->dev_id.devno, buf); |
| 596 | if ((rc != 0) && (rc != -ENODATA)) /* EOF does not hurt */ |
| 597 | goto fail_free_buf; |
| 598 | |
| 599 | /* check if the file on top of the queue is open now */ |
| 600 | rc = diag_read_next_file_info(fcb, 0); |
| 601 | if (rc) |
| 602 | goto fail_free_buf; |
| 603 | if (!(fcb->file_stat & FLG_IN_USE)) { |
| 604 | rc = -EMFILE; |
| 605 | goto fail_free_buf; |
| 606 | } |
| 607 | rc = 0; |
| 608 | |
| 609 | fail_free_buf: |
| 610 | free_page((unsigned long) buf); |
| 611 | fail_free_fcb: |
| 612 | kfree(fcb); |
| 613 | return rc; |
| 614 | } |
| 615 | |
| 616 | static int verify_device(struct urdev *urd) |
| 617 | { |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 618 | switch (urd->class) { |
| 619 | case DEV_CLASS_UR_O: |
| 620 | return 0; /* no check needed here */ |
| 621 | case DEV_CLASS_UR_I: |
Michael Holzheu | 3eed13c | 2007-08-10 14:32:34 +0200 | [diff] [blame] | 622 | return verify_uri_device(urd); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 623 | default: |
| 624 | return -ENOTSUPP; |
| 625 | } |
| 626 | } |
| 627 | |
Michael Holzheu | 3eed13c | 2007-08-10 14:32:34 +0200 | [diff] [blame] | 628 | static int get_uri_file_reclen(struct urdev *urd) |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 629 | { |
Michael Holzheu | 3eed13c | 2007-08-10 14:32:34 +0200 | [diff] [blame] | 630 | struct file_control_block *fcb; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 631 | int rc; |
| 632 | |
Michael Holzheu | 3eed13c | 2007-08-10 14:32:34 +0200 | [diff] [blame] | 633 | fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA); |
| 634 | if (!fcb) |
| 635 | return -ENOMEM; |
| 636 | rc = diag_read_next_file_info(fcb, 0); |
| 637 | if (rc) |
| 638 | goto fail_free; |
| 639 | if (fcb->file_stat & FLG_CP_DUMP) |
| 640 | rc = 0; |
| 641 | else |
| 642 | rc = fcb->rec_len; |
| 643 | |
| 644 | fail_free: |
| 645 | kfree(fcb); |
| 646 | return rc; |
| 647 | } |
| 648 | |
| 649 | static int get_file_reclen(struct urdev *urd) |
| 650 | { |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 651 | switch (urd->class) { |
| 652 | case DEV_CLASS_UR_O: |
| 653 | return 0; |
| 654 | case DEV_CLASS_UR_I: |
Michael Holzheu | 3eed13c | 2007-08-10 14:32:34 +0200 | [diff] [blame] | 655 | return get_uri_file_reclen(urd); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 656 | default: |
| 657 | return -ENOTSUPP; |
| 658 | } |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | static int ur_open(struct inode *inode, struct file *file) |
| 662 | { |
| 663 | u16 devno; |
| 664 | struct urdev *urd; |
| 665 | struct urfile *urf; |
| 666 | unsigned short accmode; |
| 667 | int rc; |
| 668 | |
| 669 | accmode = file->f_flags & O_ACCMODE; |
| 670 | |
| 671 | if (accmode == O_RDWR) |
| 672 | return -EACCES; |
Jonathan Corbet | 764a4a8 | 2008-05-15 10:01:17 -0600 | [diff] [blame] | 673 | lock_kernel(); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 674 | /* |
| 675 | * We treat the minor number as the devno of the ur device |
| 676 | * to find in the driver tree. |
| 677 | */ |
| 678 | devno = MINOR(file->f_dentry->d_inode->i_rdev); |
| 679 | |
| 680 | urd = urdev_get_from_devno(devno); |
Jonathan Corbet | 764a4a8 | 2008-05-15 10:01:17 -0600 | [diff] [blame] | 681 | if (!urd) { |
| 682 | rc = -ENXIO; |
| 683 | goto out; |
| 684 | } |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 685 | |
Frank Munzert | a695f16 | 2008-04-17 07:46:06 +0200 | [diff] [blame] | 686 | spin_lock(&urd->open_lock); |
| 687 | while (urd->open_flag) { |
| 688 | spin_unlock(&urd->open_lock); |
| 689 | if (file->f_flags & O_NONBLOCK) { |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 690 | rc = -EBUSY; |
| 691 | goto fail_put; |
| 692 | } |
Frank Munzert | a695f16 | 2008-04-17 07:46:06 +0200 | [diff] [blame] | 693 | if (wait_event_interruptible(urd->wait, urd->open_flag == 0)) { |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 694 | rc = -ERESTARTSYS; |
| 695 | goto fail_put; |
| 696 | } |
Frank Munzert | a695f16 | 2008-04-17 07:46:06 +0200 | [diff] [blame] | 697 | spin_lock(&urd->open_lock); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 698 | } |
Frank Munzert | a695f16 | 2008-04-17 07:46:06 +0200 | [diff] [blame] | 699 | urd->open_flag++; |
| 700 | spin_unlock(&urd->open_lock); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 701 | |
| 702 | TRACE("ur_open\n"); |
| 703 | |
| 704 | if (((accmode == O_RDONLY) && (urd->class != DEV_CLASS_UR_I)) || |
| 705 | ((accmode == O_WRONLY) && (urd->class != DEV_CLASS_UR_O))) { |
| 706 | TRACE("ur_open: unsupported dev class (%d)\n", urd->class); |
| 707 | rc = -EACCES; |
| 708 | goto fail_unlock; |
| 709 | } |
| 710 | |
| 711 | rc = verify_device(urd); |
| 712 | if (rc) |
| 713 | goto fail_unlock; |
| 714 | |
| 715 | urf = urfile_alloc(urd); |
| 716 | if (!urf) { |
| 717 | rc = -ENOMEM; |
| 718 | goto fail_unlock; |
| 719 | } |
| 720 | |
| 721 | urf->dev_reclen = urd->reclen; |
| 722 | rc = get_file_reclen(urd); |
| 723 | if (rc < 0) |
| 724 | goto fail_urfile_free; |
| 725 | urf->file_reclen = rc; |
| 726 | file->private_data = urf; |
Jonathan Corbet | 764a4a8 | 2008-05-15 10:01:17 -0600 | [diff] [blame] | 727 | unlock_kernel(); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 728 | return 0; |
| 729 | |
| 730 | fail_urfile_free: |
| 731 | urfile_free(urf); |
| 732 | fail_unlock: |
Frank Munzert | a695f16 | 2008-04-17 07:46:06 +0200 | [diff] [blame] | 733 | spin_lock(&urd->open_lock); |
| 734 | urd->open_flag--; |
| 735 | spin_unlock(&urd->open_lock); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 736 | fail_put: |
| 737 | urdev_put(urd); |
Jonathan Corbet | 764a4a8 | 2008-05-15 10:01:17 -0600 | [diff] [blame] | 738 | out: |
| 739 | unlock_kernel(); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 740 | return rc; |
| 741 | } |
| 742 | |
| 743 | static int ur_release(struct inode *inode, struct file *file) |
| 744 | { |
| 745 | struct urfile *urf = file->private_data; |
| 746 | |
| 747 | TRACE("ur_release\n"); |
Frank Munzert | a695f16 | 2008-04-17 07:46:06 +0200 | [diff] [blame] | 748 | spin_lock(&urf->urd->open_lock); |
| 749 | urf->urd->open_flag--; |
| 750 | spin_unlock(&urf->urd->open_lock); |
| 751 | wake_up_interruptible(&urf->urd->wait); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 752 | urdev_put(urf->urd); |
| 753 | urfile_free(urf); |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | static loff_t ur_llseek(struct file *file, loff_t offset, int whence) |
| 758 | { |
| 759 | loff_t newpos; |
| 760 | |
| 761 | if ((file->f_flags & O_ACCMODE) != O_RDONLY) |
| 762 | return -ESPIPE; /* seek allowed only for reader */ |
| 763 | if (offset % PAGE_SIZE) |
| 764 | return -ESPIPE; /* only multiples of 4K allowed */ |
| 765 | switch (whence) { |
| 766 | case 0: /* SEEK_SET */ |
| 767 | newpos = offset; |
| 768 | break; |
| 769 | case 1: /* SEEK_CUR */ |
| 770 | newpos = file->f_pos + offset; |
| 771 | break; |
| 772 | default: |
| 773 | return -EINVAL; |
| 774 | } |
| 775 | file->f_pos = newpos; |
| 776 | return newpos; |
| 777 | } |
| 778 | |
Jan Engelhardt | 5c81cdb | 2008-01-26 14:11:29 +0100 | [diff] [blame] | 779 | static const struct file_operations ur_fops = { |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 780 | .owner = THIS_MODULE, |
| 781 | .open = ur_open, |
| 782 | .release = ur_release, |
| 783 | .read = ur_read, |
| 784 | .write = ur_write, |
| 785 | .llseek = ur_llseek, |
| 786 | }; |
| 787 | |
| 788 | /* |
| 789 | * ccw_device infrastructure: |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 790 | * ur_probe creates the struct urdev (with refcount = 1), the device |
| 791 | * attributes, sets up the interrupt handler and validates the virtual |
| 792 | * unit record device. |
| 793 | * ur_remove removes the device attributes and drops the reference to |
| 794 | * struct urdev. |
| 795 | * |
| 796 | * ur_probe, ur_remove, ur_set_online and ur_set_offline are serialized |
| 797 | * by the vmur_mutex lock. |
| 798 | * |
| 799 | * urd->char_device is used as indication that the online function has |
| 800 | * been completed successfully. |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 801 | */ |
| 802 | static int ur_probe(struct ccw_device *cdev) |
| 803 | { |
| 804 | struct urdev *urd; |
| 805 | int rc; |
| 806 | |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 807 | TRACE("ur_probe: cdev=%p\n", cdev); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 808 | |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 809 | mutex_lock(&vmur_mutex); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 810 | urd = urdev_alloc(cdev); |
| 811 | if (!urd) { |
| 812 | rc = -ENOMEM; |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 813 | goto fail_unlock; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 814 | } |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 815 | |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 816 | rc = ur_create_attributes(&cdev->dev); |
| 817 | if (rc) { |
| 818 | rc = -ENOMEM; |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 819 | goto fail_urdev_put; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 820 | } |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 821 | cdev->handler = ur_int_handler; |
| 822 | |
| 823 | /* validate virtual unit record device */ |
| 824 | urd->class = get_urd_class(urd); |
| 825 | if (urd->class < 0) { |
| 826 | rc = urd->class; |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 827 | goto fail_remove_attr; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 828 | } |
| 829 | if ((urd->class != DEV_CLASS_UR_I) && (urd->class != DEV_CLASS_UR_O)) { |
| 830 | rc = -ENOTSUPP; |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 831 | goto fail_remove_attr; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 832 | } |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 833 | spin_lock_irq(get_ccwdev_lock(cdev)); |
| 834 | cdev->dev.driver_data = urd; |
| 835 | spin_unlock_irq(get_ccwdev_lock(cdev)); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 836 | |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 837 | mutex_unlock(&vmur_mutex); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 838 | return 0; |
| 839 | |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 840 | fail_remove_attr: |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 841 | ur_remove_attributes(&cdev->dev); |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 842 | fail_urdev_put: |
| 843 | urdev_put(urd); |
| 844 | fail_unlock: |
| 845 | mutex_unlock(&vmur_mutex); |
| 846 | return rc; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | static int ur_set_online(struct ccw_device *cdev) |
| 850 | { |
| 851 | struct urdev *urd; |
| 852 | int minor, major, rc; |
| 853 | char node_id[16]; |
| 854 | |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 855 | TRACE("ur_set_online: cdev=%p\n", cdev); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 856 | |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 857 | mutex_lock(&vmur_mutex); |
| 858 | urd = urdev_get_from_cdev(cdev); |
| 859 | if (!urd) { |
| 860 | /* ur_remove already deleted our urd */ |
| 861 | rc = -ENODEV; |
| 862 | goto fail_unlock; |
| 863 | } |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 864 | |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 865 | if (urd->char_device) { |
| 866 | /* Another ur_set_online was faster */ |
| 867 | rc = -EBUSY; |
| 868 | goto fail_urdev_put; |
| 869 | } |
| 870 | |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 871 | minor = urd->dev_id.devno; |
| 872 | major = MAJOR(ur_first_dev_maj_min); |
| 873 | |
| 874 | urd->char_device = cdev_alloc(); |
| 875 | if (!urd->char_device) { |
| 876 | rc = -ENOMEM; |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 877 | goto fail_urdev_put; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | cdev_init(urd->char_device, &ur_fops); |
| 881 | urd->char_device->dev = MKDEV(major, minor); |
| 882 | urd->char_device->owner = ur_fops.owner; |
| 883 | |
| 884 | rc = cdev_add(urd->char_device, urd->char_device->dev, 1); |
| 885 | if (rc) |
| 886 | goto fail_free_cdev; |
| 887 | if (urd->cdev->id.cu_type == READER_PUNCH_DEVTYPE) { |
| 888 | if (urd->class == DEV_CLASS_UR_I) |
| 889 | sprintf(node_id, "vmrdr-%s", cdev->dev.bus_id); |
| 890 | if (urd->class == DEV_CLASS_UR_O) |
| 891 | sprintf(node_id, "vmpun-%s", cdev->dev.bus_id); |
| 892 | } else if (urd->cdev->id.cu_type == PRINTER_DEVTYPE) { |
| 893 | sprintf(node_id, "vmprt-%s", cdev->dev.bus_id); |
| 894 | } else { |
| 895 | rc = -ENOTSUPP; |
| 896 | goto fail_free_cdev; |
| 897 | } |
| 898 | |
| 899 | urd->device = device_create(vmur_class, NULL, urd->char_device->dev, |
| 900 | "%s", node_id); |
| 901 | if (IS_ERR(urd->device)) { |
| 902 | rc = PTR_ERR(urd->device); |
| 903 | TRACE("ur_set_online: device_create rc=%d\n", rc); |
| 904 | goto fail_free_cdev; |
| 905 | } |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 906 | urdev_put(urd); |
| 907 | mutex_unlock(&vmur_mutex); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 908 | return 0; |
| 909 | |
| 910 | fail_free_cdev: |
| 911 | cdev_del(urd->char_device); |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 912 | urd->char_device = NULL; |
| 913 | fail_urdev_put: |
| 914 | urdev_put(urd); |
| 915 | fail_unlock: |
| 916 | mutex_unlock(&vmur_mutex); |
| 917 | return rc; |
| 918 | } |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 919 | |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 920 | static int ur_set_offline_force(struct ccw_device *cdev, int force) |
| 921 | { |
| 922 | struct urdev *urd; |
| 923 | int rc; |
| 924 | |
| 925 | TRACE("ur_set_offline: cdev=%p\n", cdev); |
| 926 | urd = urdev_get_from_cdev(cdev); |
| 927 | if (!urd) |
| 928 | /* ur_remove already deleted our urd */ |
| 929 | return -ENODEV; |
| 930 | if (!urd->char_device) { |
| 931 | /* Another ur_set_offline was faster */ |
| 932 | rc = -EBUSY; |
| 933 | goto fail_urdev_put; |
| 934 | } |
| 935 | if (!force && (atomic_read(&urd->ref_count) > 2)) { |
| 936 | /* There is still a user of urd (e.g. ur_open) */ |
| 937 | TRACE("ur_set_offline: BUSY\n"); |
| 938 | rc = -EBUSY; |
| 939 | goto fail_urdev_put; |
| 940 | } |
| 941 | device_destroy(vmur_class, urd->char_device->dev); |
| 942 | cdev_del(urd->char_device); |
| 943 | urd->char_device = NULL; |
| 944 | rc = 0; |
| 945 | |
| 946 | fail_urdev_put: |
| 947 | urdev_put(urd); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 948 | return rc; |
| 949 | } |
| 950 | |
| 951 | static int ur_set_offline(struct ccw_device *cdev) |
| 952 | { |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 953 | int rc; |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 954 | |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 955 | mutex_lock(&vmur_mutex); |
| 956 | rc = ur_set_offline_force(cdev, 0); |
| 957 | mutex_unlock(&vmur_mutex); |
| 958 | return rc; |
| 959 | } |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 960 | |
Michael Holzheu | 8127a1f | 2007-08-22 13:51:41 +0200 | [diff] [blame] | 961 | static void ur_remove(struct ccw_device *cdev) |
| 962 | { |
| 963 | unsigned long flags; |
| 964 | |
| 965 | TRACE("ur_remove\n"); |
| 966 | |
| 967 | mutex_lock(&vmur_mutex); |
| 968 | |
| 969 | if (cdev->online) |
| 970 | ur_set_offline_force(cdev, 1); |
| 971 | ur_remove_attributes(&cdev->dev); |
| 972 | |
| 973 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); |
| 974 | urdev_put(cdev->dev.driver_data); |
| 975 | cdev->dev.driver_data = NULL; |
| 976 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
| 977 | |
| 978 | mutex_unlock(&vmur_mutex); |
Frank Munzert | 810cb5b | 2007-07-17 13:36:06 +0200 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | /* |
| 982 | * Module initialisation and cleanup |
| 983 | */ |
| 984 | static int __init ur_init(void) |
| 985 | { |
| 986 | int rc; |
| 987 | dev_t dev; |
| 988 | |
| 989 | if (!MACHINE_IS_VM) { |
| 990 | PRINT_ERR("%s is only available under z/VM.\n", ur_banner); |
| 991 | return -ENODEV; |
| 992 | } |
| 993 | |
| 994 | vmur_dbf = debug_register("vmur", 4, 1, 4 * sizeof(long)); |
| 995 | if (!vmur_dbf) |
| 996 | return -ENOMEM; |
| 997 | rc = debug_register_view(vmur_dbf, &debug_sprintf_view); |
| 998 | if (rc) |
| 999 | goto fail_free_dbf; |
| 1000 | |
| 1001 | debug_set_level(vmur_dbf, 6); |
| 1002 | |
| 1003 | rc = ccw_driver_register(&ur_driver); |
| 1004 | if (rc) |
| 1005 | goto fail_free_dbf; |
| 1006 | |
| 1007 | rc = alloc_chrdev_region(&dev, 0, NUM_MINORS, "vmur"); |
| 1008 | if (rc) { |
| 1009 | PRINT_ERR("alloc_chrdev_region failed: err = %d\n", rc); |
| 1010 | goto fail_unregister_driver; |
| 1011 | } |
| 1012 | ur_first_dev_maj_min = MKDEV(MAJOR(dev), 0); |
| 1013 | |
| 1014 | vmur_class = class_create(THIS_MODULE, "vmur"); |
| 1015 | if (IS_ERR(vmur_class)) { |
| 1016 | rc = PTR_ERR(vmur_class); |
| 1017 | goto fail_unregister_region; |
| 1018 | } |
| 1019 | PRINT_INFO("%s loaded.\n", ur_banner); |
| 1020 | return 0; |
| 1021 | |
| 1022 | fail_unregister_region: |
| 1023 | unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS); |
| 1024 | fail_unregister_driver: |
| 1025 | ccw_driver_unregister(&ur_driver); |
| 1026 | fail_free_dbf: |
| 1027 | debug_unregister(vmur_dbf); |
| 1028 | return rc; |
| 1029 | } |
| 1030 | |
| 1031 | static void __exit ur_exit(void) |
| 1032 | { |
| 1033 | class_destroy(vmur_class); |
| 1034 | unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS); |
| 1035 | ccw_driver_unregister(&ur_driver); |
| 1036 | debug_unregister(vmur_dbf); |
| 1037 | PRINT_INFO("%s unloaded.\n", ur_banner); |
| 1038 | } |
| 1039 | |
| 1040 | module_init(ur_init); |
| 1041 | module_exit(ur_exit); |