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