1810cb5b3SFrank Munzert /* 2810cb5b3SFrank Munzert * Linux driver for System z and s390 unit record devices 3810cb5b3SFrank Munzert * (z/VM virtual punch, reader, printer) 4810cb5b3SFrank Munzert * 5b241f7bcSFrank Munzert * Copyright IBM Corp. 2001, 2009 6810cb5b3SFrank Munzert * Authors: Malcolm Beattie <beattiem@uk.ibm.com> 7810cb5b3SFrank Munzert * Michael Holzheu <holzheu@de.ibm.com> 8810cb5b3SFrank Munzert * Frank Munzert <munzert@de.ibm.com> 9810cb5b3SFrank Munzert */ 10810cb5b3SFrank Munzert 1180a5c36dSFrank Munzert #define KMSG_COMPONENT "vmur" 1280a5c36dSFrank Munzert #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 1380a5c36dSFrank Munzert 14810cb5b3SFrank Munzert #include <linux/cdev.h> 15764a4a8eSJonathan Corbet #include <linux/smp_lock.h> 16810cb5b3SFrank Munzert 17810cb5b3SFrank Munzert #include <asm/uaccess.h> 18810cb5b3SFrank Munzert #include <asm/cio.h> 19810cb5b3SFrank Munzert #include <asm/ccwdev.h> 20810cb5b3SFrank Munzert #include <asm/debug.h> 210a87c5cfSMichael Holzheu #include <asm/diag.h> 22810cb5b3SFrank Munzert 23810cb5b3SFrank Munzert #include "vmur.h" 24810cb5b3SFrank Munzert 25810cb5b3SFrank Munzert /* 26810cb5b3SFrank Munzert * Driver overview 27810cb5b3SFrank Munzert * 28810cb5b3SFrank Munzert * Unit record device support is implemented as a character device driver. 29810cb5b3SFrank Munzert * We can fit at least 16 bits into a device minor number and use the 30810cb5b3SFrank Munzert * simple method of mapping a character device number with minor abcd 31810cb5b3SFrank Munzert * to the unit record device with devno abcd. 32810cb5b3SFrank Munzert * I/O to virtual unit record devices is handled as follows: 33810cb5b3SFrank Munzert * Reads: Diagnose code 0x14 (input spool file manipulation) 34810cb5b3SFrank Munzert * is used to read spool data page-wise. 35810cb5b3SFrank Munzert * Writes: The CCW used is WRITE_CCW_CMD (0x01). The device's record length 36810cb5b3SFrank Munzert * is available by reading sysfs attr reclen. Each write() to the device 37810cb5b3SFrank Munzert * must specify an integral multiple (maximal 511) of reclen. 38810cb5b3SFrank Munzert */ 39810cb5b3SFrank Munzert 40810cb5b3SFrank Munzert static char ur_banner[] = "z/VM virtual unit record device driver"; 41810cb5b3SFrank Munzert 42810cb5b3SFrank Munzert MODULE_AUTHOR("IBM Corporation"); 43810cb5b3SFrank Munzert MODULE_DESCRIPTION("s390 z/VM virtual unit record device driver"); 44810cb5b3SFrank Munzert MODULE_LICENSE("GPL"); 45810cb5b3SFrank Munzert 46810cb5b3SFrank Munzert static dev_t ur_first_dev_maj_min; 47810cb5b3SFrank Munzert static struct class *vmur_class; 48810cb5b3SFrank Munzert static struct debug_info *vmur_dbf; 49810cb5b3SFrank Munzert 50810cb5b3SFrank Munzert /* We put the device's record length (for writes) in the driver_info field */ 51810cb5b3SFrank Munzert static struct ccw_device_id ur_ids[] = { 52810cb5b3SFrank Munzert { CCWDEV_CU_DI(READER_PUNCH_DEVTYPE, 80) }, 53810cb5b3SFrank Munzert { CCWDEV_CU_DI(PRINTER_DEVTYPE, 132) }, 54810cb5b3SFrank Munzert { /* end of list */ } 55810cb5b3SFrank Munzert }; 56810cb5b3SFrank Munzert 57810cb5b3SFrank Munzert MODULE_DEVICE_TABLE(ccw, ur_ids); 58810cb5b3SFrank Munzert 59810cb5b3SFrank Munzert static int ur_probe(struct ccw_device *cdev); 60810cb5b3SFrank Munzert static void ur_remove(struct ccw_device *cdev); 61810cb5b3SFrank Munzert static int ur_set_online(struct ccw_device *cdev); 62810cb5b3SFrank Munzert static int ur_set_offline(struct ccw_device *cdev); 63b241f7bcSFrank Munzert static int ur_pm_suspend(struct ccw_device *cdev); 64810cb5b3SFrank Munzert 65810cb5b3SFrank Munzert static struct ccw_driver ur_driver = { 66810cb5b3SFrank Munzert .name = "vmur", 67810cb5b3SFrank Munzert .owner = THIS_MODULE, 68810cb5b3SFrank Munzert .ids = ur_ids, 69810cb5b3SFrank Munzert .probe = ur_probe, 70810cb5b3SFrank Munzert .remove = ur_remove, 71810cb5b3SFrank Munzert .set_online = ur_set_online, 72810cb5b3SFrank Munzert .set_offline = ur_set_offline, 73b241f7bcSFrank Munzert .freeze = ur_pm_suspend, 74810cb5b3SFrank Munzert }; 75810cb5b3SFrank Munzert 768127a1f8SMichael Holzheu static DEFINE_MUTEX(vmur_mutex); 778127a1f8SMichael Holzheu 78810cb5b3SFrank Munzert /* 79810cb5b3SFrank Munzert * Allocation, freeing, getting and putting of urdev structures 808127a1f8SMichael Holzheu * 818127a1f8SMichael Holzheu * Each ur device (urd) contains a reference to its corresponding ccw device 828127a1f8SMichael Holzheu * (cdev) using the urd->cdev pointer. Each ccw device has a reference to the 83dff59b64SGreg Kroah-Hartman * ur device using dev_get_drvdata(&cdev->dev) pointer. 848127a1f8SMichael Holzheu * 858127a1f8SMichael Holzheu * urd references: 868127a1f8SMichael Holzheu * - ur_probe gets a urd reference, ur_remove drops the reference 87dff59b64SGreg Kroah-Hartman * dev_get_drvdata(&cdev->dev) 888127a1f8SMichael Holzheu * - ur_open gets a urd reference, ur_relase drops the reference 898127a1f8SMichael Holzheu * (urf->urd) 908127a1f8SMichael Holzheu * 918127a1f8SMichael Holzheu * cdev references: 928127a1f8SMichael Holzheu * - urdev_alloc get a cdev reference (urd->cdev) 938127a1f8SMichael Holzheu * - urdev_free drops the cdev reference (urd->cdev) 948127a1f8SMichael Holzheu * 95dff59b64SGreg Kroah-Hartman * Setting and clearing of dev_get_drvdata(&cdev->dev) is protected by the ccwdev lock 96810cb5b3SFrank Munzert */ 97810cb5b3SFrank Munzert static struct urdev *urdev_alloc(struct ccw_device *cdev) 98810cb5b3SFrank Munzert { 99810cb5b3SFrank Munzert struct urdev *urd; 100810cb5b3SFrank Munzert 101810cb5b3SFrank Munzert urd = kzalloc(sizeof(struct urdev), GFP_KERNEL); 102810cb5b3SFrank Munzert if (!urd) 103810cb5b3SFrank Munzert return NULL; 104810cb5b3SFrank Munzert urd->reclen = cdev->id.driver_info; 105810cb5b3SFrank Munzert ccw_device_get_id(cdev, &urd->dev_id); 106810cb5b3SFrank Munzert mutex_init(&urd->io_mutex); 107a695f167SFrank Munzert init_waitqueue_head(&urd->wait); 108a695f167SFrank Munzert spin_lock_init(&urd->open_lock); 1098127a1f8SMichael Holzheu atomic_set(&urd->ref_count, 1); 1108127a1f8SMichael Holzheu urd->cdev = cdev; 1118127a1f8SMichael Holzheu get_device(&cdev->dev); 112810cb5b3SFrank Munzert return urd; 113810cb5b3SFrank Munzert } 114810cb5b3SFrank Munzert 115810cb5b3SFrank Munzert static void urdev_free(struct urdev *urd) 116810cb5b3SFrank Munzert { 1178127a1f8SMichael Holzheu TRACE("urdev_free: %p\n", urd); 1188127a1f8SMichael Holzheu if (urd->cdev) 1198127a1f8SMichael Holzheu put_device(&urd->cdev->dev); 120810cb5b3SFrank Munzert kfree(urd); 121810cb5b3SFrank Munzert } 122810cb5b3SFrank Munzert 1238127a1f8SMichael Holzheu static void urdev_get(struct urdev *urd) 1248127a1f8SMichael Holzheu { 1258127a1f8SMichael Holzheu atomic_inc(&urd->ref_count); 1268127a1f8SMichael Holzheu } 1278127a1f8SMichael Holzheu 1288127a1f8SMichael Holzheu static struct urdev *urdev_get_from_cdev(struct ccw_device *cdev) 1298127a1f8SMichael Holzheu { 1308127a1f8SMichael Holzheu struct urdev *urd; 1318127a1f8SMichael Holzheu unsigned long flags; 1328127a1f8SMichael Holzheu 1338127a1f8SMichael Holzheu spin_lock_irqsave(get_ccwdev_lock(cdev), flags); 134dff59b64SGreg Kroah-Hartman urd = dev_get_drvdata(&cdev->dev); 1358127a1f8SMichael Holzheu if (urd) 1368127a1f8SMichael Holzheu urdev_get(urd); 1378127a1f8SMichael Holzheu spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); 1388127a1f8SMichael Holzheu return urd; 1398127a1f8SMichael Holzheu } 1408127a1f8SMichael Holzheu 141810cb5b3SFrank Munzert static struct urdev *urdev_get_from_devno(u16 devno) 142810cb5b3SFrank Munzert { 143810cb5b3SFrank Munzert char bus_id[16]; 144810cb5b3SFrank Munzert struct ccw_device *cdev; 1458127a1f8SMichael Holzheu struct urdev *urd; 146810cb5b3SFrank Munzert 147810cb5b3SFrank Munzert sprintf(bus_id, "0.0.%04x", devno); 148810cb5b3SFrank Munzert cdev = get_ccwdev_by_busid(&ur_driver, bus_id); 149810cb5b3SFrank Munzert if (!cdev) 150810cb5b3SFrank Munzert return NULL; 1518127a1f8SMichael Holzheu urd = urdev_get_from_cdev(cdev); 1528127a1f8SMichael Holzheu put_device(&cdev->dev); 1538127a1f8SMichael Holzheu return urd; 154810cb5b3SFrank Munzert } 155810cb5b3SFrank Munzert 156810cb5b3SFrank Munzert static void urdev_put(struct urdev *urd) 157810cb5b3SFrank Munzert { 1588127a1f8SMichael Holzheu if (atomic_dec_and_test(&urd->ref_count)) 1598127a1f8SMichael Holzheu urdev_free(urd); 160810cb5b3SFrank Munzert } 161810cb5b3SFrank Munzert 162810cb5b3SFrank Munzert /* 163b241f7bcSFrank Munzert * State and contents of ur devices can be changed by class D users issuing 164b241f7bcSFrank Munzert * CP commands such as PURGE or TRANSFER, while the Linux guest is suspended. 165b241f7bcSFrank Munzert * Also the Linux guest might be logged off, which causes all active spool 166b241f7bcSFrank Munzert * files to be closed. 167b241f7bcSFrank Munzert * So we cannot guarantee that spool files are still the same when the Linux 168b241f7bcSFrank Munzert * guest is resumed. In order to avoid unpredictable results at resume time 169b241f7bcSFrank Munzert * we simply refuse to suspend if a ur device node is open. 170b241f7bcSFrank Munzert */ 171b241f7bcSFrank Munzert static int ur_pm_suspend(struct ccw_device *cdev) 172b241f7bcSFrank Munzert { 1734f0076f7SMartin Schwidefsky struct urdev *urd = dev_get_drvdata(&cdev->dev); 174b241f7bcSFrank Munzert 175b241f7bcSFrank Munzert TRACE("ur_pm_suspend: cdev=%p\n", cdev); 176b241f7bcSFrank Munzert if (urd->open_flag) { 177b241f7bcSFrank Munzert pr_err("Unit record device %s is busy, %s refusing to " 178b241f7bcSFrank Munzert "suspend.\n", dev_name(&cdev->dev), ur_banner); 179b241f7bcSFrank Munzert return -EBUSY; 180b241f7bcSFrank Munzert } 181b241f7bcSFrank Munzert return 0; 182b241f7bcSFrank Munzert } 183b241f7bcSFrank Munzert 184b241f7bcSFrank Munzert /* 185810cb5b3SFrank Munzert * Low-level functions to do I/O to a ur device. 186810cb5b3SFrank Munzert * alloc_chan_prog 1871eade380SMichael Holzheu * free_chan_prog 188810cb5b3SFrank Munzert * do_ur_io 189810cb5b3SFrank Munzert * ur_int_handler 190810cb5b3SFrank Munzert * 191810cb5b3SFrank Munzert * alloc_chan_prog allocates and builds the channel program 1921eade380SMichael Holzheu * free_chan_prog frees memory of the channel program 193810cb5b3SFrank Munzert * 194810cb5b3SFrank Munzert * do_ur_io issues the channel program to the device and blocks waiting 195810cb5b3SFrank Munzert * on a completion event it publishes at urd->io_done. The function 196810cb5b3SFrank Munzert * serialises itself on the device's mutex so that only one I/O 197810cb5b3SFrank Munzert * is issued at a time (and that I/O is synchronous). 198810cb5b3SFrank Munzert * 199810cb5b3SFrank Munzert * ur_int_handler catches the "I/O done" interrupt, writes the 200810cb5b3SFrank Munzert * subchannel status word into the scsw member of the urdev structure 201810cb5b3SFrank Munzert * and complete()s the io_done to wake the waiting do_ur_io. 202810cb5b3SFrank Munzert * 203810cb5b3SFrank Munzert * The caller of do_ur_io is responsible for kfree()ing the channel program 204810cb5b3SFrank Munzert * address pointer that alloc_chan_prog returned. 205810cb5b3SFrank Munzert */ 206810cb5b3SFrank Munzert 2071eade380SMichael Holzheu static void free_chan_prog(struct ccw1 *cpa) 2081eade380SMichael Holzheu { 2091eade380SMichael Holzheu struct ccw1 *ptr = cpa; 2101eade380SMichael Holzheu 2111eade380SMichael Holzheu while (ptr->cda) { 2121eade380SMichael Holzheu kfree((void *)(addr_t) ptr->cda); 2131eade380SMichael Holzheu ptr++; 2141eade380SMichael Holzheu } 2151eade380SMichael Holzheu kfree(cpa); 2161eade380SMichael Holzheu } 217810cb5b3SFrank Munzert 218810cb5b3SFrank Munzert /* 219810cb5b3SFrank Munzert * alloc_chan_prog 220810cb5b3SFrank Munzert * The channel program we use is write commands chained together 221810cb5b3SFrank Munzert * with a final NOP CCW command-chained on (which ensures that CE and DE 222810cb5b3SFrank Munzert * are presented together in a single interrupt instead of as separate 223810cb5b3SFrank Munzert * interrupts unless an incorrect length indication kicks in first). The 2241eade380SMichael Holzheu * data length in each CCW is reclen. 225810cb5b3SFrank Munzert */ 2261eade380SMichael Holzheu static struct ccw1 *alloc_chan_prog(const char __user *ubuf, int rec_count, 2271eade380SMichael Holzheu int reclen) 228810cb5b3SFrank Munzert { 229810cb5b3SFrank Munzert struct ccw1 *cpa; 2301eade380SMichael Holzheu void *kbuf; 231810cb5b3SFrank Munzert int i; 232810cb5b3SFrank Munzert 2331eade380SMichael Holzheu TRACE("alloc_chan_prog(%p, %i, %i)\n", ubuf, rec_count, reclen); 234810cb5b3SFrank Munzert 235810cb5b3SFrank Munzert /* 236810cb5b3SFrank Munzert * We chain a NOP onto the writes to force CE+DE together. 237810cb5b3SFrank Munzert * That means we allocate room for CCWs to cover count/reclen 238810cb5b3SFrank Munzert * records plus a NOP. 239810cb5b3SFrank Munzert */ 2401eade380SMichael Holzheu cpa = kzalloc((rec_count + 1) * sizeof(struct ccw1), 2411eade380SMichael Holzheu GFP_KERNEL | GFP_DMA); 242810cb5b3SFrank Munzert if (!cpa) 2431eade380SMichael Holzheu return ERR_PTR(-ENOMEM); 244810cb5b3SFrank Munzert 2451eade380SMichael Holzheu for (i = 0; i < rec_count; i++) { 246810cb5b3SFrank Munzert cpa[i].cmd_code = WRITE_CCW_CMD; 247810cb5b3SFrank Munzert cpa[i].flags = CCW_FLAG_CC | CCW_FLAG_SLI; 248810cb5b3SFrank Munzert cpa[i].count = reclen; 2491eade380SMichael Holzheu kbuf = kmalloc(reclen, GFP_KERNEL | GFP_DMA); 2501eade380SMichael Holzheu if (!kbuf) { 2511eade380SMichael Holzheu free_chan_prog(cpa); 2521eade380SMichael Holzheu return ERR_PTR(-ENOMEM); 2531eade380SMichael Holzheu } 2541eade380SMichael Holzheu cpa[i].cda = (u32)(addr_t) kbuf; 2551eade380SMichael Holzheu if (copy_from_user(kbuf, ubuf, reclen)) { 2561eade380SMichael Holzheu free_chan_prog(cpa); 2571eade380SMichael Holzheu return ERR_PTR(-EFAULT); 2581eade380SMichael Holzheu } 2591eade380SMichael Holzheu ubuf += reclen; 260810cb5b3SFrank Munzert } 261810cb5b3SFrank Munzert /* The following NOP CCW forces CE+DE to be presented together */ 262810cb5b3SFrank Munzert cpa[i].cmd_code = CCW_CMD_NOOP; 263810cb5b3SFrank Munzert return cpa; 264810cb5b3SFrank Munzert } 265810cb5b3SFrank Munzert 266810cb5b3SFrank Munzert static int do_ur_io(struct urdev *urd, struct ccw1 *cpa) 267810cb5b3SFrank Munzert { 268810cb5b3SFrank Munzert int rc; 269810cb5b3SFrank Munzert struct ccw_device *cdev = urd->cdev; 270278bc68cSHeiko Carstens DECLARE_COMPLETION_ONSTACK(event); 271810cb5b3SFrank Munzert 272810cb5b3SFrank Munzert TRACE("do_ur_io: cpa=%p\n", cpa); 273810cb5b3SFrank Munzert 274810cb5b3SFrank Munzert rc = mutex_lock_interruptible(&urd->io_mutex); 275810cb5b3SFrank Munzert if (rc) 276810cb5b3SFrank Munzert return rc; 277810cb5b3SFrank Munzert 278810cb5b3SFrank Munzert urd->io_done = &event; 279810cb5b3SFrank Munzert 280810cb5b3SFrank Munzert spin_lock_irq(get_ccwdev_lock(cdev)); 281810cb5b3SFrank Munzert rc = ccw_device_start(cdev, cpa, 1, 0, 0); 282810cb5b3SFrank Munzert spin_unlock_irq(get_ccwdev_lock(cdev)); 283810cb5b3SFrank Munzert 284810cb5b3SFrank Munzert TRACE("do_ur_io: ccw_device_start returned %d\n", rc); 285810cb5b3SFrank Munzert if (rc) 286810cb5b3SFrank Munzert goto out; 287810cb5b3SFrank Munzert 288810cb5b3SFrank Munzert wait_for_completion(&event); 289810cb5b3SFrank Munzert TRACE("do_ur_io: I/O complete\n"); 290810cb5b3SFrank Munzert rc = 0; 291810cb5b3SFrank Munzert 292810cb5b3SFrank Munzert out: 293810cb5b3SFrank Munzert mutex_unlock(&urd->io_mutex); 294810cb5b3SFrank Munzert return rc; 295810cb5b3SFrank Munzert } 296810cb5b3SFrank Munzert 297810cb5b3SFrank Munzert /* 298810cb5b3SFrank Munzert * ur interrupt handler, called from the ccw_device layer 299810cb5b3SFrank Munzert */ 300810cb5b3SFrank Munzert static void ur_int_handler(struct ccw_device *cdev, unsigned long intparm, 301810cb5b3SFrank Munzert struct irb *irb) 302810cb5b3SFrank Munzert { 303810cb5b3SFrank Munzert struct urdev *urd; 304810cb5b3SFrank Munzert 305810cb5b3SFrank Munzert TRACE("ur_int_handler: intparm=0x%lx cstat=%02x dstat=%02x res=%u\n", 30623d805b6SPeter Oberparleiter intparm, irb->scsw.cmd.cstat, irb->scsw.cmd.dstat, 30723d805b6SPeter Oberparleiter irb->scsw.cmd.count); 308810cb5b3SFrank Munzert 309810cb5b3SFrank Munzert if (!intparm) { 310810cb5b3SFrank Munzert TRACE("ur_int_handler: unsolicited interrupt\n"); 311810cb5b3SFrank Munzert return; 312810cb5b3SFrank Munzert } 313dff59b64SGreg Kroah-Hartman urd = dev_get_drvdata(&cdev->dev); 3148127a1f8SMichael Holzheu BUG_ON(!urd); 315810cb5b3SFrank Munzert /* On special conditions irb is an error pointer */ 316810cb5b3SFrank Munzert if (IS_ERR(irb)) 317810cb5b3SFrank Munzert urd->io_request_rc = PTR_ERR(irb); 31823d805b6SPeter Oberparleiter else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END)) 319810cb5b3SFrank Munzert urd->io_request_rc = 0; 320810cb5b3SFrank Munzert else 321810cb5b3SFrank Munzert urd->io_request_rc = -EIO; 322810cb5b3SFrank Munzert 323810cb5b3SFrank Munzert complete(urd->io_done); 324810cb5b3SFrank Munzert } 325810cb5b3SFrank Munzert 326810cb5b3SFrank Munzert /* 327810cb5b3SFrank Munzert * reclen sysfs attribute - The record length to be used for write CCWs 328810cb5b3SFrank Munzert */ 329810cb5b3SFrank Munzert static ssize_t ur_attr_reclen_show(struct device *dev, 330810cb5b3SFrank Munzert struct device_attribute *attr, char *buf) 331810cb5b3SFrank Munzert { 3328127a1f8SMichael Holzheu struct urdev *urd; 3338127a1f8SMichael Holzheu int rc; 334810cb5b3SFrank Munzert 3358127a1f8SMichael Holzheu urd = urdev_get_from_cdev(to_ccwdev(dev)); 3368127a1f8SMichael Holzheu if (!urd) 3378127a1f8SMichael Holzheu return -ENODEV; 3388127a1f8SMichael Holzheu rc = sprintf(buf, "%zu\n", urd->reclen); 3398127a1f8SMichael Holzheu urdev_put(urd); 3408127a1f8SMichael Holzheu return rc; 341810cb5b3SFrank Munzert } 342810cb5b3SFrank Munzert 343810cb5b3SFrank Munzert static DEVICE_ATTR(reclen, 0444, ur_attr_reclen_show, NULL); 344810cb5b3SFrank Munzert 345810cb5b3SFrank Munzert static int ur_create_attributes(struct device *dev) 346810cb5b3SFrank Munzert { 347810cb5b3SFrank Munzert return device_create_file(dev, &dev_attr_reclen); 348810cb5b3SFrank Munzert } 349810cb5b3SFrank Munzert 350810cb5b3SFrank Munzert static void ur_remove_attributes(struct device *dev) 351810cb5b3SFrank Munzert { 352810cb5b3SFrank Munzert device_remove_file(dev, &dev_attr_reclen); 353810cb5b3SFrank Munzert } 354810cb5b3SFrank Munzert 355810cb5b3SFrank Munzert /* 356810cb5b3SFrank Munzert * diagnose code 0x210 - retrieve device information 357810cb5b3SFrank Munzert * cc=0 normal completion, we have a real device 358810cb5b3SFrank Munzert * cc=1 CP paging error 359810cb5b3SFrank Munzert * cc=2 The virtual device exists, but is not associated with a real device 360810cb5b3SFrank Munzert * cc=3 Invalid device address, or the virtual device does not exist 361810cb5b3SFrank Munzert */ 362810cb5b3SFrank Munzert static int get_urd_class(struct urdev *urd) 363810cb5b3SFrank Munzert { 364810cb5b3SFrank Munzert static struct diag210 ur_diag210; 365810cb5b3SFrank Munzert int cc; 366810cb5b3SFrank Munzert 367810cb5b3SFrank Munzert ur_diag210.vrdcdvno = urd->dev_id.devno; 368810cb5b3SFrank Munzert ur_diag210.vrdclen = sizeof(struct diag210); 369810cb5b3SFrank Munzert 370810cb5b3SFrank Munzert cc = diag210(&ur_diag210); 371810cb5b3SFrank Munzert switch (cc) { 372810cb5b3SFrank Munzert case 0: 373b9993a38SFrank Munzert return -EOPNOTSUPP; 374810cb5b3SFrank Munzert case 2: 375810cb5b3SFrank Munzert return ur_diag210.vrdcvcla; /* virtual device class */ 376810cb5b3SFrank Munzert case 3: 377810cb5b3SFrank Munzert return -ENODEV; 378810cb5b3SFrank Munzert default: 379810cb5b3SFrank Munzert return -EIO; 380810cb5b3SFrank Munzert } 381810cb5b3SFrank Munzert } 382810cb5b3SFrank Munzert 383810cb5b3SFrank Munzert /* 384810cb5b3SFrank Munzert * Allocation and freeing of urfile structures 385810cb5b3SFrank Munzert */ 386810cb5b3SFrank Munzert static struct urfile *urfile_alloc(struct urdev *urd) 387810cb5b3SFrank Munzert { 388810cb5b3SFrank Munzert struct urfile *urf; 389810cb5b3SFrank Munzert 390810cb5b3SFrank Munzert urf = kzalloc(sizeof(struct urfile), GFP_KERNEL); 391810cb5b3SFrank Munzert if (!urf) 392810cb5b3SFrank Munzert return NULL; 393810cb5b3SFrank Munzert urf->urd = urd; 394810cb5b3SFrank Munzert 395810cb5b3SFrank Munzert TRACE("urfile_alloc: urd=%p urf=%p rl=%zu\n", urd, urf, 396810cb5b3SFrank Munzert urf->dev_reclen); 397810cb5b3SFrank Munzert 398810cb5b3SFrank Munzert return urf; 399810cb5b3SFrank Munzert } 400810cb5b3SFrank Munzert 401810cb5b3SFrank Munzert static void urfile_free(struct urfile *urf) 402810cb5b3SFrank Munzert { 403810cb5b3SFrank Munzert TRACE("urfile_free: urf=%p urd=%p\n", urf, urf->urd); 404810cb5b3SFrank Munzert kfree(urf); 405810cb5b3SFrank Munzert } 406810cb5b3SFrank Munzert 407810cb5b3SFrank Munzert /* 408810cb5b3SFrank Munzert * The fops implementation of the character device driver 409810cb5b3SFrank Munzert */ 410810cb5b3SFrank Munzert static ssize_t do_write(struct urdev *urd, const char __user *udata, 411810cb5b3SFrank Munzert size_t count, size_t reclen, loff_t *ppos) 412810cb5b3SFrank Munzert { 413810cb5b3SFrank Munzert struct ccw1 *cpa; 414810cb5b3SFrank Munzert int rc; 415810cb5b3SFrank Munzert 4161eade380SMichael Holzheu cpa = alloc_chan_prog(udata, count / reclen, reclen); 4171eade380SMichael Holzheu if (IS_ERR(cpa)) 4181eade380SMichael Holzheu return PTR_ERR(cpa); 419810cb5b3SFrank Munzert 420810cb5b3SFrank Munzert rc = do_ur_io(urd, cpa); 421810cb5b3SFrank Munzert if (rc) 422810cb5b3SFrank Munzert goto fail_kfree_cpa; 423810cb5b3SFrank Munzert 424810cb5b3SFrank Munzert if (urd->io_request_rc) { 425810cb5b3SFrank Munzert rc = urd->io_request_rc; 426810cb5b3SFrank Munzert goto fail_kfree_cpa; 427810cb5b3SFrank Munzert } 428810cb5b3SFrank Munzert *ppos += count; 429810cb5b3SFrank Munzert rc = count; 4301eade380SMichael Holzheu 431810cb5b3SFrank Munzert fail_kfree_cpa: 4321eade380SMichael Holzheu free_chan_prog(cpa); 433810cb5b3SFrank Munzert return rc; 434810cb5b3SFrank Munzert } 435810cb5b3SFrank Munzert 436810cb5b3SFrank Munzert static ssize_t ur_write(struct file *file, const char __user *udata, 437810cb5b3SFrank Munzert size_t count, loff_t *ppos) 438810cb5b3SFrank Munzert { 439810cb5b3SFrank Munzert struct urfile *urf = file->private_data; 440810cb5b3SFrank Munzert 441810cb5b3SFrank Munzert TRACE("ur_write: count=%zu\n", count); 442810cb5b3SFrank Munzert 443810cb5b3SFrank Munzert if (count == 0) 444810cb5b3SFrank Munzert return 0; 445810cb5b3SFrank Munzert 446810cb5b3SFrank Munzert if (count % urf->dev_reclen) 447810cb5b3SFrank Munzert return -EINVAL; /* count must be a multiple of reclen */ 448810cb5b3SFrank Munzert 449810cb5b3SFrank Munzert if (count > urf->dev_reclen * MAX_RECS_PER_IO) 450810cb5b3SFrank Munzert count = urf->dev_reclen * MAX_RECS_PER_IO; 451810cb5b3SFrank Munzert 452810cb5b3SFrank Munzert return do_write(urf->urd, udata, count, urf->dev_reclen, ppos); 453810cb5b3SFrank Munzert } 454810cb5b3SFrank Munzert 455810cb5b3SFrank Munzert /* 456810cb5b3SFrank Munzert * diagnose code 0x14 subcode 0x0028 - position spool file to designated 457810cb5b3SFrank Munzert * record 458810cb5b3SFrank Munzert * cc=0 normal completion 459810cb5b3SFrank Munzert * cc=2 no file active on the virtual reader or device not ready 460810cb5b3SFrank Munzert * cc=3 record specified is beyond EOF 461810cb5b3SFrank Munzert */ 462810cb5b3SFrank Munzert static int diag_position_to_record(int devno, int record) 463810cb5b3SFrank Munzert { 464810cb5b3SFrank Munzert int cc; 465810cb5b3SFrank Munzert 4660a87c5cfSMichael Holzheu cc = diag14(record, devno, 0x28); 467810cb5b3SFrank Munzert switch (cc) { 468810cb5b3SFrank Munzert case 0: 469810cb5b3SFrank Munzert return 0; 470810cb5b3SFrank Munzert case 2: 471810cb5b3SFrank Munzert return -ENOMEDIUM; 472810cb5b3SFrank Munzert case 3: 473810cb5b3SFrank Munzert return -ENODATA; /* position beyond end of file */ 474810cb5b3SFrank Munzert default: 475810cb5b3SFrank Munzert return -EIO; 476810cb5b3SFrank Munzert } 477810cb5b3SFrank Munzert } 478810cb5b3SFrank Munzert 479810cb5b3SFrank Munzert /* 480810cb5b3SFrank Munzert * diagnose code 0x14 subcode 0x0000 - read next spool file buffer 481810cb5b3SFrank Munzert * cc=0 normal completion 482810cb5b3SFrank Munzert * cc=1 EOF reached 483810cb5b3SFrank Munzert * cc=2 no file active on the virtual reader, and no file eligible 484810cb5b3SFrank Munzert * cc=3 file already active on the virtual reader or specified virtual 485810cb5b3SFrank Munzert * reader does not exist or is not a reader 486810cb5b3SFrank Munzert */ 487810cb5b3SFrank Munzert static int diag_read_file(int devno, char *buf) 488810cb5b3SFrank Munzert { 489810cb5b3SFrank Munzert int cc; 490810cb5b3SFrank Munzert 4910a87c5cfSMichael Holzheu cc = diag14((unsigned long) buf, devno, 0x00); 492810cb5b3SFrank Munzert switch (cc) { 493810cb5b3SFrank Munzert case 0: 494810cb5b3SFrank Munzert return 0; 495810cb5b3SFrank Munzert case 1: 496810cb5b3SFrank Munzert return -ENODATA; 497810cb5b3SFrank Munzert case 2: 498810cb5b3SFrank Munzert return -ENOMEDIUM; 499810cb5b3SFrank Munzert default: 500810cb5b3SFrank Munzert return -EIO; 501810cb5b3SFrank Munzert } 502810cb5b3SFrank Munzert } 503810cb5b3SFrank Munzert 504810cb5b3SFrank Munzert static ssize_t diag14_read(struct file *file, char __user *ubuf, size_t count, 505810cb5b3SFrank Munzert loff_t *offs) 506810cb5b3SFrank Munzert { 507810cb5b3SFrank Munzert size_t len, copied, res; 508810cb5b3SFrank Munzert char *buf; 509810cb5b3SFrank Munzert int rc; 510810cb5b3SFrank Munzert u16 reclen; 511810cb5b3SFrank Munzert struct urdev *urd; 512810cb5b3SFrank Munzert 513810cb5b3SFrank Munzert urd = ((struct urfile *) file->private_data)->urd; 514810cb5b3SFrank Munzert reclen = ((struct urfile *) file->private_data)->file_reclen; 515810cb5b3SFrank Munzert 516810cb5b3SFrank Munzert rc = diag_position_to_record(urd->dev_id.devno, *offs / PAGE_SIZE + 1); 517810cb5b3SFrank Munzert if (rc == -ENODATA) 518810cb5b3SFrank Munzert return 0; 519810cb5b3SFrank Munzert if (rc) 520810cb5b3SFrank Munzert return rc; 521810cb5b3SFrank Munzert 522810cb5b3SFrank Munzert len = min((size_t) PAGE_SIZE, count); 5233eed13ccSMichael Holzheu buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA); 524810cb5b3SFrank Munzert if (!buf) 525810cb5b3SFrank Munzert return -ENOMEM; 526810cb5b3SFrank Munzert 527810cb5b3SFrank Munzert copied = 0; 528810cb5b3SFrank Munzert res = (size_t) (*offs % PAGE_SIZE); 529810cb5b3SFrank Munzert do { 530810cb5b3SFrank Munzert rc = diag_read_file(urd->dev_id.devno, buf); 531810cb5b3SFrank Munzert if (rc == -ENODATA) { 532810cb5b3SFrank Munzert break; 533810cb5b3SFrank Munzert } 534810cb5b3SFrank Munzert if (rc) 535810cb5b3SFrank Munzert goto fail; 5362b3d8c9eSFrank Munzert if (reclen && (copied == 0) && (*offs < PAGE_SIZE)) 537810cb5b3SFrank Munzert *((u16 *) &buf[FILE_RECLEN_OFFSET]) = reclen; 538810cb5b3SFrank Munzert len = min(count - copied, PAGE_SIZE - res); 539810cb5b3SFrank Munzert if (copy_to_user(ubuf + copied, buf + res, len)) { 540810cb5b3SFrank Munzert rc = -EFAULT; 541810cb5b3SFrank Munzert goto fail; 542810cb5b3SFrank Munzert } 543810cb5b3SFrank Munzert res = 0; 544810cb5b3SFrank Munzert copied += len; 545810cb5b3SFrank Munzert } while (copied != count); 546810cb5b3SFrank Munzert 547810cb5b3SFrank Munzert *offs += copied; 548810cb5b3SFrank Munzert rc = copied; 549810cb5b3SFrank Munzert fail: 5503eed13ccSMichael Holzheu free_page((unsigned long) buf); 551810cb5b3SFrank Munzert return rc; 552810cb5b3SFrank Munzert } 553810cb5b3SFrank Munzert 554810cb5b3SFrank Munzert static ssize_t ur_read(struct file *file, char __user *ubuf, size_t count, 555810cb5b3SFrank Munzert loff_t *offs) 556810cb5b3SFrank Munzert { 557810cb5b3SFrank Munzert struct urdev *urd; 558810cb5b3SFrank Munzert int rc; 559810cb5b3SFrank Munzert 560810cb5b3SFrank Munzert TRACE("ur_read: count=%zu ppos=%li\n", count, (unsigned long) *offs); 561810cb5b3SFrank Munzert 562810cb5b3SFrank Munzert if (count == 0) 563810cb5b3SFrank Munzert return 0; 564810cb5b3SFrank Munzert 565810cb5b3SFrank Munzert urd = ((struct urfile *) file->private_data)->urd; 566810cb5b3SFrank Munzert rc = mutex_lock_interruptible(&urd->io_mutex); 567810cb5b3SFrank Munzert if (rc) 568810cb5b3SFrank Munzert return rc; 569810cb5b3SFrank Munzert rc = diag14_read(file, ubuf, count, offs); 570810cb5b3SFrank Munzert mutex_unlock(&urd->io_mutex); 571810cb5b3SFrank Munzert return rc; 572810cb5b3SFrank Munzert } 573810cb5b3SFrank Munzert 574810cb5b3SFrank Munzert /* 575810cb5b3SFrank Munzert * diagnose code 0x14 subcode 0x0fff - retrieve next file descriptor 576810cb5b3SFrank Munzert * cc=0 normal completion 577810cb5b3SFrank Munzert * cc=1 no files on reader queue or no subsequent file 578810cb5b3SFrank Munzert * cc=2 spid specified is invalid 579810cb5b3SFrank Munzert */ 580810cb5b3SFrank Munzert static int diag_read_next_file_info(struct file_control_block *buf, int spid) 581810cb5b3SFrank Munzert { 582810cb5b3SFrank Munzert int cc; 583810cb5b3SFrank Munzert 5840a87c5cfSMichael Holzheu cc = diag14((unsigned long) buf, spid, 0xfff); 585810cb5b3SFrank Munzert switch (cc) { 586810cb5b3SFrank Munzert case 0: 587810cb5b3SFrank Munzert return 0; 588810cb5b3SFrank Munzert default: 589810cb5b3SFrank Munzert return -ENODATA; 590810cb5b3SFrank Munzert } 591810cb5b3SFrank Munzert } 592810cb5b3SFrank Munzert 5933eed13ccSMichael Holzheu static int verify_uri_device(struct urdev *urd) 594810cb5b3SFrank Munzert { 5953eed13ccSMichael Holzheu struct file_control_block *fcb; 596810cb5b3SFrank Munzert char *buf; 597810cb5b3SFrank Munzert int rc; 598810cb5b3SFrank Munzert 5993eed13ccSMichael Holzheu fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA); 6003eed13ccSMichael Holzheu if (!fcb) 6013eed13ccSMichael Holzheu return -ENOMEM; 6023eed13ccSMichael Holzheu 6033eed13ccSMichael Holzheu /* check for empty reader device (beginning of chain) */ 6043eed13ccSMichael Holzheu rc = diag_read_next_file_info(fcb, 0); 6053eed13ccSMichael Holzheu if (rc) 6063eed13ccSMichael Holzheu goto fail_free_fcb; 6073eed13ccSMichael Holzheu 6083eed13ccSMichael Holzheu /* if file is in hold status, we do not read it */ 6093eed13ccSMichael Holzheu if (fcb->file_stat & (FLG_SYSTEM_HOLD | FLG_USER_HOLD)) { 6103eed13ccSMichael Holzheu rc = -EPERM; 6113eed13ccSMichael Holzheu goto fail_free_fcb; 6123eed13ccSMichael Holzheu } 6133eed13ccSMichael Holzheu 6143eed13ccSMichael Holzheu /* open file on virtual reader */ 6153eed13ccSMichael Holzheu buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA); 6163eed13ccSMichael Holzheu if (!buf) { 6173eed13ccSMichael Holzheu rc = -ENOMEM; 6183eed13ccSMichael Holzheu goto fail_free_fcb; 6193eed13ccSMichael Holzheu } 6203eed13ccSMichael Holzheu rc = diag_read_file(urd->dev_id.devno, buf); 6213eed13ccSMichael Holzheu if ((rc != 0) && (rc != -ENODATA)) /* EOF does not hurt */ 6223eed13ccSMichael Holzheu goto fail_free_buf; 6233eed13ccSMichael Holzheu 6243eed13ccSMichael Holzheu /* check if the file on top of the queue is open now */ 6253eed13ccSMichael Holzheu rc = diag_read_next_file_info(fcb, 0); 6263eed13ccSMichael Holzheu if (rc) 6273eed13ccSMichael Holzheu goto fail_free_buf; 6283eed13ccSMichael Holzheu if (!(fcb->file_stat & FLG_IN_USE)) { 6293eed13ccSMichael Holzheu rc = -EMFILE; 6303eed13ccSMichael Holzheu goto fail_free_buf; 6313eed13ccSMichael Holzheu } 6323eed13ccSMichael Holzheu rc = 0; 6333eed13ccSMichael Holzheu 6343eed13ccSMichael Holzheu fail_free_buf: 6353eed13ccSMichael Holzheu free_page((unsigned long) buf); 6363eed13ccSMichael Holzheu fail_free_fcb: 6373eed13ccSMichael Holzheu kfree(fcb); 6383eed13ccSMichael Holzheu return rc; 6393eed13ccSMichael Holzheu } 6403eed13ccSMichael Holzheu 6413eed13ccSMichael Holzheu static int verify_device(struct urdev *urd) 6423eed13ccSMichael Holzheu { 643810cb5b3SFrank Munzert switch (urd->class) { 644810cb5b3SFrank Munzert case DEV_CLASS_UR_O: 645810cb5b3SFrank Munzert return 0; /* no check needed here */ 646810cb5b3SFrank Munzert case DEV_CLASS_UR_I: 6473eed13ccSMichael Holzheu return verify_uri_device(urd); 648810cb5b3SFrank Munzert default: 649b9993a38SFrank Munzert return -EOPNOTSUPP; 650810cb5b3SFrank Munzert } 651810cb5b3SFrank Munzert } 652810cb5b3SFrank Munzert 6533eed13ccSMichael Holzheu static int get_uri_file_reclen(struct urdev *urd) 6543eed13ccSMichael Holzheu { 6553eed13ccSMichael Holzheu struct file_control_block *fcb; 6563eed13ccSMichael Holzheu int rc; 6573eed13ccSMichael Holzheu 6583eed13ccSMichael Holzheu fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA); 6593eed13ccSMichael Holzheu if (!fcb) 6603eed13ccSMichael Holzheu return -ENOMEM; 6613eed13ccSMichael Holzheu rc = diag_read_next_file_info(fcb, 0); 6623eed13ccSMichael Holzheu if (rc) 6633eed13ccSMichael Holzheu goto fail_free; 6643eed13ccSMichael Holzheu if (fcb->file_stat & FLG_CP_DUMP) 6653eed13ccSMichael Holzheu rc = 0; 6663eed13ccSMichael Holzheu else 6673eed13ccSMichael Holzheu rc = fcb->rec_len; 6683eed13ccSMichael Holzheu 6693eed13ccSMichael Holzheu fail_free: 6703eed13ccSMichael Holzheu kfree(fcb); 6713eed13ccSMichael Holzheu return rc; 6723eed13ccSMichael Holzheu } 6733eed13ccSMichael Holzheu 674810cb5b3SFrank Munzert static int get_file_reclen(struct urdev *urd) 675810cb5b3SFrank Munzert { 676810cb5b3SFrank Munzert switch (urd->class) { 677810cb5b3SFrank Munzert case DEV_CLASS_UR_O: 678810cb5b3SFrank Munzert return 0; 679810cb5b3SFrank Munzert case DEV_CLASS_UR_I: 6803eed13ccSMichael Holzheu return get_uri_file_reclen(urd); 681810cb5b3SFrank Munzert default: 682b9993a38SFrank Munzert return -EOPNOTSUPP; 683810cb5b3SFrank Munzert } 684810cb5b3SFrank Munzert } 685810cb5b3SFrank Munzert 686810cb5b3SFrank Munzert static int ur_open(struct inode *inode, struct file *file) 687810cb5b3SFrank Munzert { 688810cb5b3SFrank Munzert u16 devno; 689810cb5b3SFrank Munzert struct urdev *urd; 690810cb5b3SFrank Munzert struct urfile *urf; 691810cb5b3SFrank Munzert unsigned short accmode; 692810cb5b3SFrank Munzert int rc; 693810cb5b3SFrank Munzert 694810cb5b3SFrank Munzert accmode = file->f_flags & O_ACCMODE; 695810cb5b3SFrank Munzert 696810cb5b3SFrank Munzert if (accmode == O_RDWR) 697810cb5b3SFrank Munzert return -EACCES; 698764a4a8eSJonathan Corbet lock_kernel(); 699810cb5b3SFrank Munzert /* 700810cb5b3SFrank Munzert * We treat the minor number as the devno of the ur device 701810cb5b3SFrank Munzert * to find in the driver tree. 702810cb5b3SFrank Munzert */ 703810cb5b3SFrank Munzert devno = MINOR(file->f_dentry->d_inode->i_rdev); 704810cb5b3SFrank Munzert 705810cb5b3SFrank Munzert urd = urdev_get_from_devno(devno); 706764a4a8eSJonathan Corbet if (!urd) { 707764a4a8eSJonathan Corbet rc = -ENXIO; 708764a4a8eSJonathan Corbet goto out; 709764a4a8eSJonathan Corbet } 710810cb5b3SFrank Munzert 711a695f167SFrank Munzert spin_lock(&urd->open_lock); 712a695f167SFrank Munzert while (urd->open_flag) { 713a695f167SFrank Munzert spin_unlock(&urd->open_lock); 714810cb5b3SFrank Munzert if (file->f_flags & O_NONBLOCK) { 715810cb5b3SFrank Munzert rc = -EBUSY; 716810cb5b3SFrank Munzert goto fail_put; 717810cb5b3SFrank Munzert } 718a695f167SFrank Munzert if (wait_event_interruptible(urd->wait, urd->open_flag == 0)) { 719810cb5b3SFrank Munzert rc = -ERESTARTSYS; 720810cb5b3SFrank Munzert goto fail_put; 721810cb5b3SFrank Munzert } 722a695f167SFrank Munzert spin_lock(&urd->open_lock); 723810cb5b3SFrank Munzert } 724a695f167SFrank Munzert urd->open_flag++; 725a695f167SFrank Munzert spin_unlock(&urd->open_lock); 726810cb5b3SFrank Munzert 727810cb5b3SFrank Munzert TRACE("ur_open\n"); 728810cb5b3SFrank Munzert 729810cb5b3SFrank Munzert if (((accmode == O_RDONLY) && (urd->class != DEV_CLASS_UR_I)) || 730810cb5b3SFrank Munzert ((accmode == O_WRONLY) && (urd->class != DEV_CLASS_UR_O))) { 731810cb5b3SFrank Munzert TRACE("ur_open: unsupported dev class (%d)\n", urd->class); 732810cb5b3SFrank Munzert rc = -EACCES; 733810cb5b3SFrank Munzert goto fail_unlock; 734810cb5b3SFrank Munzert } 735810cb5b3SFrank Munzert 736810cb5b3SFrank Munzert rc = verify_device(urd); 737810cb5b3SFrank Munzert if (rc) 738810cb5b3SFrank Munzert goto fail_unlock; 739810cb5b3SFrank Munzert 740810cb5b3SFrank Munzert urf = urfile_alloc(urd); 741810cb5b3SFrank Munzert if (!urf) { 742810cb5b3SFrank Munzert rc = -ENOMEM; 743810cb5b3SFrank Munzert goto fail_unlock; 744810cb5b3SFrank Munzert } 745810cb5b3SFrank Munzert 746810cb5b3SFrank Munzert urf->dev_reclen = urd->reclen; 747810cb5b3SFrank Munzert rc = get_file_reclen(urd); 748810cb5b3SFrank Munzert if (rc < 0) 749810cb5b3SFrank Munzert goto fail_urfile_free; 750810cb5b3SFrank Munzert urf->file_reclen = rc; 751810cb5b3SFrank Munzert file->private_data = urf; 752764a4a8eSJonathan Corbet unlock_kernel(); 753810cb5b3SFrank Munzert return 0; 754810cb5b3SFrank Munzert 755810cb5b3SFrank Munzert fail_urfile_free: 756810cb5b3SFrank Munzert urfile_free(urf); 757810cb5b3SFrank Munzert fail_unlock: 758a695f167SFrank Munzert spin_lock(&urd->open_lock); 759a695f167SFrank Munzert urd->open_flag--; 760a695f167SFrank Munzert spin_unlock(&urd->open_lock); 761810cb5b3SFrank Munzert fail_put: 762810cb5b3SFrank Munzert urdev_put(urd); 763764a4a8eSJonathan Corbet out: 764764a4a8eSJonathan Corbet unlock_kernel(); 765810cb5b3SFrank Munzert return rc; 766810cb5b3SFrank Munzert } 767810cb5b3SFrank Munzert 768810cb5b3SFrank Munzert static int ur_release(struct inode *inode, struct file *file) 769810cb5b3SFrank Munzert { 770810cb5b3SFrank Munzert struct urfile *urf = file->private_data; 771810cb5b3SFrank Munzert 772810cb5b3SFrank Munzert TRACE("ur_release\n"); 773a695f167SFrank Munzert spin_lock(&urf->urd->open_lock); 774a695f167SFrank Munzert urf->urd->open_flag--; 775a695f167SFrank Munzert spin_unlock(&urf->urd->open_lock); 776a695f167SFrank Munzert wake_up_interruptible(&urf->urd->wait); 777810cb5b3SFrank Munzert urdev_put(urf->urd); 778810cb5b3SFrank Munzert urfile_free(urf); 779810cb5b3SFrank Munzert return 0; 780810cb5b3SFrank Munzert } 781810cb5b3SFrank Munzert 782810cb5b3SFrank Munzert static loff_t ur_llseek(struct file *file, loff_t offset, int whence) 783810cb5b3SFrank Munzert { 784810cb5b3SFrank Munzert loff_t newpos; 785810cb5b3SFrank Munzert 786810cb5b3SFrank Munzert if ((file->f_flags & O_ACCMODE) != O_RDONLY) 787810cb5b3SFrank Munzert return -ESPIPE; /* seek allowed only for reader */ 788810cb5b3SFrank Munzert if (offset % PAGE_SIZE) 789810cb5b3SFrank Munzert return -ESPIPE; /* only multiples of 4K allowed */ 790810cb5b3SFrank Munzert switch (whence) { 791810cb5b3SFrank Munzert case 0: /* SEEK_SET */ 792810cb5b3SFrank Munzert newpos = offset; 793810cb5b3SFrank Munzert break; 794810cb5b3SFrank Munzert case 1: /* SEEK_CUR */ 795810cb5b3SFrank Munzert newpos = file->f_pos + offset; 796810cb5b3SFrank Munzert break; 797810cb5b3SFrank Munzert default: 798810cb5b3SFrank Munzert return -EINVAL; 799810cb5b3SFrank Munzert } 800810cb5b3SFrank Munzert file->f_pos = newpos; 801810cb5b3SFrank Munzert return newpos; 802810cb5b3SFrank Munzert } 803810cb5b3SFrank Munzert 8045c81cdbeSJan Engelhardt static const struct file_operations ur_fops = { 805810cb5b3SFrank Munzert .owner = THIS_MODULE, 806810cb5b3SFrank Munzert .open = ur_open, 807810cb5b3SFrank Munzert .release = ur_release, 808810cb5b3SFrank Munzert .read = ur_read, 809810cb5b3SFrank Munzert .write = ur_write, 810810cb5b3SFrank Munzert .llseek = ur_llseek, 811810cb5b3SFrank Munzert }; 812810cb5b3SFrank Munzert 813810cb5b3SFrank Munzert /* 814810cb5b3SFrank Munzert * ccw_device infrastructure: 8158127a1f8SMichael Holzheu * ur_probe creates the struct urdev (with refcount = 1), the device 8168127a1f8SMichael Holzheu * attributes, sets up the interrupt handler and validates the virtual 8178127a1f8SMichael Holzheu * unit record device. 8188127a1f8SMichael Holzheu * ur_remove removes the device attributes and drops the reference to 8198127a1f8SMichael Holzheu * struct urdev. 8208127a1f8SMichael Holzheu * 8218127a1f8SMichael Holzheu * ur_probe, ur_remove, ur_set_online and ur_set_offline are serialized 8228127a1f8SMichael Holzheu * by the vmur_mutex lock. 8238127a1f8SMichael Holzheu * 8248127a1f8SMichael Holzheu * urd->char_device is used as indication that the online function has 8258127a1f8SMichael Holzheu * been completed successfully. 826810cb5b3SFrank Munzert */ 827810cb5b3SFrank Munzert static int ur_probe(struct ccw_device *cdev) 828810cb5b3SFrank Munzert { 829810cb5b3SFrank Munzert struct urdev *urd; 830810cb5b3SFrank Munzert int rc; 831810cb5b3SFrank Munzert 8328127a1f8SMichael Holzheu TRACE("ur_probe: cdev=%p\n", cdev); 833810cb5b3SFrank Munzert 8348127a1f8SMichael Holzheu mutex_lock(&vmur_mutex); 835810cb5b3SFrank Munzert urd = urdev_alloc(cdev); 836810cb5b3SFrank Munzert if (!urd) { 837810cb5b3SFrank Munzert rc = -ENOMEM; 8388127a1f8SMichael Holzheu goto fail_unlock; 839810cb5b3SFrank Munzert } 8408127a1f8SMichael Holzheu 841810cb5b3SFrank Munzert rc = ur_create_attributes(&cdev->dev); 842810cb5b3SFrank Munzert if (rc) { 843810cb5b3SFrank Munzert rc = -ENOMEM; 8448127a1f8SMichael Holzheu goto fail_urdev_put; 845810cb5b3SFrank Munzert } 846810cb5b3SFrank Munzert cdev->handler = ur_int_handler; 847810cb5b3SFrank Munzert 848810cb5b3SFrank Munzert /* validate virtual unit record device */ 849810cb5b3SFrank Munzert urd->class = get_urd_class(urd); 850810cb5b3SFrank Munzert if (urd->class < 0) { 851810cb5b3SFrank Munzert rc = urd->class; 8528127a1f8SMichael Holzheu goto fail_remove_attr; 853810cb5b3SFrank Munzert } 854810cb5b3SFrank Munzert if ((urd->class != DEV_CLASS_UR_I) && (urd->class != DEV_CLASS_UR_O)) { 855b9993a38SFrank Munzert rc = -EOPNOTSUPP; 8568127a1f8SMichael Holzheu goto fail_remove_attr; 857810cb5b3SFrank Munzert } 8588127a1f8SMichael Holzheu spin_lock_irq(get_ccwdev_lock(cdev)); 859dff59b64SGreg Kroah-Hartman dev_set_drvdata(&cdev->dev, urd); 8608127a1f8SMichael Holzheu spin_unlock_irq(get_ccwdev_lock(cdev)); 861810cb5b3SFrank Munzert 8628127a1f8SMichael Holzheu mutex_unlock(&vmur_mutex); 863810cb5b3SFrank Munzert return 0; 864810cb5b3SFrank Munzert 8658127a1f8SMichael Holzheu fail_remove_attr: 866810cb5b3SFrank Munzert ur_remove_attributes(&cdev->dev); 8678127a1f8SMichael Holzheu fail_urdev_put: 8688127a1f8SMichael Holzheu urdev_put(urd); 8698127a1f8SMichael Holzheu fail_unlock: 8708127a1f8SMichael Holzheu mutex_unlock(&vmur_mutex); 8718127a1f8SMichael Holzheu return rc; 872810cb5b3SFrank Munzert } 873810cb5b3SFrank Munzert 874810cb5b3SFrank Munzert static int ur_set_online(struct ccw_device *cdev) 875810cb5b3SFrank Munzert { 876810cb5b3SFrank Munzert struct urdev *urd; 877810cb5b3SFrank Munzert int minor, major, rc; 878810cb5b3SFrank Munzert char node_id[16]; 879810cb5b3SFrank Munzert 8808127a1f8SMichael Holzheu TRACE("ur_set_online: cdev=%p\n", cdev); 881810cb5b3SFrank Munzert 8828127a1f8SMichael Holzheu mutex_lock(&vmur_mutex); 8838127a1f8SMichael Holzheu urd = urdev_get_from_cdev(cdev); 8848127a1f8SMichael Holzheu if (!urd) { 8858127a1f8SMichael Holzheu /* ur_remove already deleted our urd */ 8868127a1f8SMichael Holzheu rc = -ENODEV; 8878127a1f8SMichael Holzheu goto fail_unlock; 8888127a1f8SMichael Holzheu } 889810cb5b3SFrank Munzert 8908127a1f8SMichael Holzheu if (urd->char_device) { 8918127a1f8SMichael Holzheu /* Another ur_set_online was faster */ 8928127a1f8SMichael Holzheu rc = -EBUSY; 8938127a1f8SMichael Holzheu goto fail_urdev_put; 8948127a1f8SMichael Holzheu } 8958127a1f8SMichael Holzheu 896810cb5b3SFrank Munzert minor = urd->dev_id.devno; 897810cb5b3SFrank Munzert major = MAJOR(ur_first_dev_maj_min); 898810cb5b3SFrank Munzert 899810cb5b3SFrank Munzert urd->char_device = cdev_alloc(); 900810cb5b3SFrank Munzert if (!urd->char_device) { 901810cb5b3SFrank Munzert rc = -ENOMEM; 9028127a1f8SMichael Holzheu goto fail_urdev_put; 903810cb5b3SFrank Munzert } 904810cb5b3SFrank Munzert 905810cb5b3SFrank Munzert cdev_init(urd->char_device, &ur_fops); 906810cb5b3SFrank Munzert urd->char_device->dev = MKDEV(major, minor); 907810cb5b3SFrank Munzert urd->char_device->owner = ur_fops.owner; 908810cb5b3SFrank Munzert 909810cb5b3SFrank Munzert rc = cdev_add(urd->char_device, urd->char_device->dev, 1); 910810cb5b3SFrank Munzert if (rc) 911810cb5b3SFrank Munzert goto fail_free_cdev; 912810cb5b3SFrank Munzert if (urd->cdev->id.cu_type == READER_PUNCH_DEVTYPE) { 913810cb5b3SFrank Munzert if (urd->class == DEV_CLASS_UR_I) 9142a0217d5SKay Sievers sprintf(node_id, "vmrdr-%s", dev_name(&cdev->dev)); 915810cb5b3SFrank Munzert if (urd->class == DEV_CLASS_UR_O) 9162a0217d5SKay Sievers sprintf(node_id, "vmpun-%s", dev_name(&cdev->dev)); 917810cb5b3SFrank Munzert } else if (urd->cdev->id.cu_type == PRINTER_DEVTYPE) { 9182a0217d5SKay Sievers sprintf(node_id, "vmprt-%s", dev_name(&cdev->dev)); 919810cb5b3SFrank Munzert } else { 920b9993a38SFrank Munzert rc = -EOPNOTSUPP; 921810cb5b3SFrank Munzert goto fail_free_cdev; 922810cb5b3SFrank Munzert } 923810cb5b3SFrank Munzert 924ea9e42f6SGreg Kroah-Hartman urd->device = device_create(vmur_class, NULL, urd->char_device->dev, 925ea9e42f6SGreg Kroah-Hartman NULL, "%s", node_id); 926810cb5b3SFrank Munzert if (IS_ERR(urd->device)) { 927810cb5b3SFrank Munzert rc = PTR_ERR(urd->device); 928810cb5b3SFrank Munzert TRACE("ur_set_online: device_create rc=%d\n", rc); 929810cb5b3SFrank Munzert goto fail_free_cdev; 930810cb5b3SFrank Munzert } 9318127a1f8SMichael Holzheu urdev_put(urd); 9328127a1f8SMichael Holzheu mutex_unlock(&vmur_mutex); 933810cb5b3SFrank Munzert return 0; 934810cb5b3SFrank Munzert 935810cb5b3SFrank Munzert fail_free_cdev: 936810cb5b3SFrank Munzert cdev_del(urd->char_device); 9378127a1f8SMichael Holzheu urd->char_device = NULL; 9388127a1f8SMichael Holzheu fail_urdev_put: 9398127a1f8SMichael Holzheu urdev_put(urd); 9408127a1f8SMichael Holzheu fail_unlock: 9418127a1f8SMichael Holzheu mutex_unlock(&vmur_mutex); 9428127a1f8SMichael Holzheu return rc; 9438127a1f8SMichael Holzheu } 944810cb5b3SFrank Munzert 9458127a1f8SMichael Holzheu static int ur_set_offline_force(struct ccw_device *cdev, int force) 9468127a1f8SMichael Holzheu { 9478127a1f8SMichael Holzheu struct urdev *urd; 9488127a1f8SMichael Holzheu int rc; 9498127a1f8SMichael Holzheu 9508127a1f8SMichael Holzheu TRACE("ur_set_offline: cdev=%p\n", cdev); 9518127a1f8SMichael Holzheu urd = urdev_get_from_cdev(cdev); 9528127a1f8SMichael Holzheu if (!urd) 9538127a1f8SMichael Holzheu /* ur_remove already deleted our urd */ 9548127a1f8SMichael Holzheu return -ENODEV; 9558127a1f8SMichael Holzheu if (!urd->char_device) { 9568127a1f8SMichael Holzheu /* Another ur_set_offline was faster */ 9578127a1f8SMichael Holzheu rc = -EBUSY; 9588127a1f8SMichael Holzheu goto fail_urdev_put; 9598127a1f8SMichael Holzheu } 9608127a1f8SMichael Holzheu if (!force && (atomic_read(&urd->ref_count) > 2)) { 9618127a1f8SMichael Holzheu /* There is still a user of urd (e.g. ur_open) */ 9628127a1f8SMichael Holzheu TRACE("ur_set_offline: BUSY\n"); 9638127a1f8SMichael Holzheu rc = -EBUSY; 9648127a1f8SMichael Holzheu goto fail_urdev_put; 9658127a1f8SMichael Holzheu } 9668127a1f8SMichael Holzheu device_destroy(vmur_class, urd->char_device->dev); 9678127a1f8SMichael Holzheu cdev_del(urd->char_device); 9688127a1f8SMichael Holzheu urd->char_device = NULL; 9698127a1f8SMichael Holzheu rc = 0; 9708127a1f8SMichael Holzheu 9718127a1f8SMichael Holzheu fail_urdev_put: 9728127a1f8SMichael Holzheu urdev_put(urd); 973810cb5b3SFrank Munzert return rc; 974810cb5b3SFrank Munzert } 975810cb5b3SFrank Munzert 976810cb5b3SFrank Munzert static int ur_set_offline(struct ccw_device *cdev) 977810cb5b3SFrank Munzert { 9788127a1f8SMichael Holzheu int rc; 979810cb5b3SFrank Munzert 9808127a1f8SMichael Holzheu mutex_lock(&vmur_mutex); 9818127a1f8SMichael Holzheu rc = ur_set_offline_force(cdev, 0); 9828127a1f8SMichael Holzheu mutex_unlock(&vmur_mutex); 9838127a1f8SMichael Holzheu return rc; 9848127a1f8SMichael Holzheu } 985810cb5b3SFrank Munzert 9868127a1f8SMichael Holzheu static void ur_remove(struct ccw_device *cdev) 9878127a1f8SMichael Holzheu { 9888127a1f8SMichael Holzheu unsigned long flags; 9898127a1f8SMichael Holzheu 9908127a1f8SMichael Holzheu TRACE("ur_remove\n"); 9918127a1f8SMichael Holzheu 9928127a1f8SMichael Holzheu mutex_lock(&vmur_mutex); 9938127a1f8SMichael Holzheu 9948127a1f8SMichael Holzheu if (cdev->online) 9958127a1f8SMichael Holzheu ur_set_offline_force(cdev, 1); 9968127a1f8SMichael Holzheu ur_remove_attributes(&cdev->dev); 9978127a1f8SMichael Holzheu 9988127a1f8SMichael Holzheu spin_lock_irqsave(get_ccwdev_lock(cdev), flags); 999dff59b64SGreg Kroah-Hartman urdev_put(dev_get_drvdata(&cdev->dev)); 1000dff59b64SGreg Kroah-Hartman dev_set_drvdata(&cdev->dev, NULL); 10018127a1f8SMichael Holzheu spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); 10028127a1f8SMichael Holzheu 10038127a1f8SMichael Holzheu mutex_unlock(&vmur_mutex); 1004810cb5b3SFrank Munzert } 1005810cb5b3SFrank Munzert 1006810cb5b3SFrank Munzert /* 1007810cb5b3SFrank Munzert * Module initialisation and cleanup 1008810cb5b3SFrank Munzert */ 1009810cb5b3SFrank Munzert static int __init ur_init(void) 1010810cb5b3SFrank Munzert { 1011810cb5b3SFrank Munzert int rc; 1012810cb5b3SFrank Munzert dev_t dev; 1013810cb5b3SFrank Munzert 1014810cb5b3SFrank Munzert if (!MACHINE_IS_VM) { 101580a5c36dSFrank Munzert pr_err("The %s cannot be loaded without z/VM\n", 101680a5c36dSFrank Munzert ur_banner); 1017810cb5b3SFrank Munzert return -ENODEV; 1018810cb5b3SFrank Munzert } 1019810cb5b3SFrank Munzert 1020810cb5b3SFrank Munzert vmur_dbf = debug_register("vmur", 4, 1, 4 * sizeof(long)); 1021810cb5b3SFrank Munzert if (!vmur_dbf) 1022810cb5b3SFrank Munzert return -ENOMEM; 1023810cb5b3SFrank Munzert rc = debug_register_view(vmur_dbf, &debug_sprintf_view); 1024810cb5b3SFrank Munzert if (rc) 1025810cb5b3SFrank Munzert goto fail_free_dbf; 1026810cb5b3SFrank Munzert 1027810cb5b3SFrank Munzert debug_set_level(vmur_dbf, 6); 1028810cb5b3SFrank Munzert 1029*3f7cb51fSMichael Holzheu vmur_class = class_create(THIS_MODULE, "vmur"); 1030*3f7cb51fSMichael Holzheu if (IS_ERR(vmur_class)) { 1031*3f7cb51fSMichael Holzheu rc = PTR_ERR(vmur_class); 1032*3f7cb51fSMichael Holzheu goto fail_free_dbf; 1033*3f7cb51fSMichael Holzheu } 1034*3f7cb51fSMichael Holzheu 1035810cb5b3SFrank Munzert rc = ccw_driver_register(&ur_driver); 1036810cb5b3SFrank Munzert if (rc) 1037*3f7cb51fSMichael Holzheu goto fail_class_destroy; 1038810cb5b3SFrank Munzert 1039810cb5b3SFrank Munzert rc = alloc_chrdev_region(&dev, 0, NUM_MINORS, "vmur"); 1040810cb5b3SFrank Munzert if (rc) { 104180a5c36dSFrank Munzert pr_err("Kernel function alloc_chrdev_region failed with " 104280a5c36dSFrank Munzert "error code %d\n", rc); 1043810cb5b3SFrank Munzert goto fail_unregister_driver; 1044810cb5b3SFrank Munzert } 1045810cb5b3SFrank Munzert ur_first_dev_maj_min = MKDEV(MAJOR(dev), 0); 1046810cb5b3SFrank Munzert 104780a5c36dSFrank Munzert pr_info("%s loaded.\n", ur_banner); 1048810cb5b3SFrank Munzert return 0; 1049810cb5b3SFrank Munzert 1050810cb5b3SFrank Munzert fail_unregister_driver: 1051810cb5b3SFrank Munzert ccw_driver_unregister(&ur_driver); 1052*3f7cb51fSMichael Holzheu fail_class_destroy: 1053*3f7cb51fSMichael Holzheu class_destroy(vmur_class); 1054810cb5b3SFrank Munzert fail_free_dbf: 1055810cb5b3SFrank Munzert debug_unregister(vmur_dbf); 1056810cb5b3SFrank Munzert return rc; 1057810cb5b3SFrank Munzert } 1058810cb5b3SFrank Munzert 1059810cb5b3SFrank Munzert static void __exit ur_exit(void) 1060810cb5b3SFrank Munzert { 1061810cb5b3SFrank Munzert unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS); 1062810cb5b3SFrank Munzert ccw_driver_unregister(&ur_driver); 1063*3f7cb51fSMichael Holzheu class_destroy(vmur_class); 1064810cb5b3SFrank Munzert debug_unregister(vmur_dbf); 106580a5c36dSFrank Munzert pr_info("%s unloaded.\n", ur_banner); 1066810cb5b3SFrank Munzert } 1067810cb5b3SFrank Munzert 1068810cb5b3SFrank Munzert module_init(ur_init); 1069810cb5b3SFrank Munzert module_exit(ur_exit); 1070