16f05e69eSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2810cb5b3SFrank Munzert /* 3810cb5b3SFrank Munzert * Linux driver for System z and s390 unit record devices 4810cb5b3SFrank Munzert * (z/VM virtual punch, reader, printer) 5810cb5b3SFrank Munzert * 6b241f7bcSFrank Munzert * Copyright IBM Corp. 2001, 2009 7810cb5b3SFrank Munzert * Authors: Malcolm Beattie <beattiem@uk.ibm.com> 8810cb5b3SFrank Munzert * Michael Holzheu <holzheu@de.ibm.com> 9810cb5b3SFrank Munzert * Frank Munzert <munzert@de.ibm.com> 10810cb5b3SFrank Munzert */ 11810cb5b3SFrank Munzert 1280a5c36dSFrank Munzert #define KMSG_COMPONENT "vmur" 1380a5c36dSFrank Munzert #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 1480a5c36dSFrank Munzert 15810cb5b3SFrank Munzert #include <linux/cdev.h> 165a0e3ad6STejun Heo #include <linux/slab.h> 173a4c5d59SHeiko Carstens #include <linux/module.h> 18bf18140dSPeter Oberparleiter #include <linux/kobject.h> 19810cb5b3SFrank Munzert 207c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 21810cb5b3SFrank Munzert #include <asm/cio.h> 22810cb5b3SFrank Munzert #include <asm/ccwdev.h> 23810cb5b3SFrank Munzert #include <asm/debug.h> 240a87c5cfSMichael Holzheu #include <asm/diag.h> 25bf18140dSPeter Oberparleiter #include <asm/scsw.h> 26810cb5b3SFrank Munzert 27810cb5b3SFrank Munzert #include "vmur.h" 28810cb5b3SFrank Munzert 29810cb5b3SFrank Munzert /* 30810cb5b3SFrank Munzert * Driver overview 31810cb5b3SFrank Munzert * 32810cb5b3SFrank Munzert * Unit record device support is implemented as a character device driver. 33810cb5b3SFrank Munzert * We can fit at least 16 bits into a device minor number and use the 34810cb5b3SFrank Munzert * simple method of mapping a character device number with minor abcd 35810cb5b3SFrank Munzert * to the unit record device with devno abcd. 36810cb5b3SFrank Munzert * I/O to virtual unit record devices is handled as follows: 37810cb5b3SFrank Munzert * Reads: Diagnose code 0x14 (input spool file manipulation) 38810cb5b3SFrank Munzert * is used to read spool data page-wise. 39810cb5b3SFrank Munzert * Writes: The CCW used is WRITE_CCW_CMD (0x01). The device's record length 40810cb5b3SFrank Munzert * is available by reading sysfs attr reclen. Each write() to the device 41810cb5b3SFrank Munzert * must specify an integral multiple (maximal 511) of reclen. 42810cb5b3SFrank Munzert */ 43810cb5b3SFrank Munzert 44810cb5b3SFrank Munzert static char ur_banner[] = "z/VM virtual unit record device driver"; 45810cb5b3SFrank Munzert 46810cb5b3SFrank Munzert MODULE_AUTHOR("IBM Corporation"); 47810cb5b3SFrank Munzert MODULE_DESCRIPTION("s390 z/VM virtual unit record device driver"); 48810cb5b3SFrank Munzert MODULE_LICENSE("GPL"); 49810cb5b3SFrank Munzert 50810cb5b3SFrank Munzert static dev_t ur_first_dev_maj_min; 51810cb5b3SFrank Munzert static struct class *vmur_class; 52810cb5b3SFrank Munzert static struct debug_info *vmur_dbf; 53810cb5b3SFrank Munzert 54810cb5b3SFrank Munzert /* We put the device's record length (for writes) in the driver_info field */ 55810cb5b3SFrank Munzert static struct ccw_device_id ur_ids[] = { 56810cb5b3SFrank Munzert { CCWDEV_CU_DI(READER_PUNCH_DEVTYPE, 80) }, 57810cb5b3SFrank Munzert { CCWDEV_CU_DI(PRINTER_DEVTYPE, 132) }, 58810cb5b3SFrank Munzert { /* end of list */ } 59810cb5b3SFrank Munzert }; 60810cb5b3SFrank Munzert 61810cb5b3SFrank Munzert MODULE_DEVICE_TABLE(ccw, ur_ids); 62810cb5b3SFrank Munzert 63810cb5b3SFrank Munzert static int ur_probe(struct ccw_device *cdev); 64810cb5b3SFrank Munzert static void ur_remove(struct ccw_device *cdev); 65810cb5b3SFrank Munzert static int ur_set_online(struct ccw_device *cdev); 66810cb5b3SFrank Munzert static int ur_set_offline(struct ccw_device *cdev); 67810cb5b3SFrank Munzert 68810cb5b3SFrank Munzert static struct ccw_driver ur_driver = { 693bda058bSSebastian Ott .driver = { 70810cb5b3SFrank Munzert .name = "vmur", 71810cb5b3SFrank Munzert .owner = THIS_MODULE, 723bda058bSSebastian Ott }, 73810cb5b3SFrank Munzert .ids = ur_ids, 74810cb5b3SFrank Munzert .probe = ur_probe, 75810cb5b3SFrank Munzert .remove = ur_remove, 76810cb5b3SFrank Munzert .set_online = ur_set_online, 77810cb5b3SFrank Munzert .set_offline = ur_set_offline, 78420f42ecSHeiko Carstens .int_class = IRQIO_VMR, 79810cb5b3SFrank Munzert }; 80810cb5b3SFrank Munzert 818127a1f8SMichael Holzheu static DEFINE_MUTEX(vmur_mutex); 828127a1f8SMichael Holzheu 83bf18140dSPeter Oberparleiter static void ur_uevent(struct work_struct *ws); 84bf18140dSPeter Oberparleiter 85810cb5b3SFrank Munzert /* 86810cb5b3SFrank Munzert * Allocation, freeing, getting and putting of urdev structures 878127a1f8SMichael Holzheu * 888127a1f8SMichael Holzheu * Each ur device (urd) contains a reference to its corresponding ccw device 898127a1f8SMichael Holzheu * (cdev) using the urd->cdev pointer. Each ccw device has a reference to the 90dff59b64SGreg Kroah-Hartman * ur device using dev_get_drvdata(&cdev->dev) pointer. 918127a1f8SMichael Holzheu * 928127a1f8SMichael Holzheu * urd references: 938127a1f8SMichael Holzheu * - ur_probe gets a urd reference, ur_remove drops the reference 94dff59b64SGreg Kroah-Hartman * dev_get_drvdata(&cdev->dev) 9583a35e36SGeert Uytterhoeven * - ur_open gets a urd reference, ur_release drops the reference 968127a1f8SMichael Holzheu * (urf->urd) 978127a1f8SMichael Holzheu * 988127a1f8SMichael Holzheu * cdev references: 998127a1f8SMichael Holzheu * - urdev_alloc get a cdev reference (urd->cdev) 1008127a1f8SMichael Holzheu * - urdev_free drops the cdev reference (urd->cdev) 1018127a1f8SMichael Holzheu * 102dff59b64SGreg Kroah-Hartman * Setting and clearing of dev_get_drvdata(&cdev->dev) is protected by the ccwdev lock 103810cb5b3SFrank Munzert */ 104810cb5b3SFrank Munzert static struct urdev *urdev_alloc(struct ccw_device *cdev) 105810cb5b3SFrank Munzert { 106810cb5b3SFrank Munzert struct urdev *urd; 107810cb5b3SFrank Munzert 108810cb5b3SFrank Munzert urd = kzalloc(sizeof(struct urdev), GFP_KERNEL); 109810cb5b3SFrank Munzert if (!urd) 110810cb5b3SFrank Munzert return NULL; 111810cb5b3SFrank Munzert urd->reclen = cdev->id.driver_info; 112810cb5b3SFrank Munzert ccw_device_get_id(cdev, &urd->dev_id); 113810cb5b3SFrank Munzert mutex_init(&urd->io_mutex); 114a695f167SFrank Munzert init_waitqueue_head(&urd->wait); 115bf18140dSPeter Oberparleiter INIT_WORK(&urd->uevent_work, ur_uevent); 116a695f167SFrank Munzert spin_lock_init(&urd->open_lock); 1170462d992SElena Reshetova refcount_set(&urd->ref_count, 1); 1188127a1f8SMichael Holzheu urd->cdev = cdev; 1198127a1f8SMichael Holzheu get_device(&cdev->dev); 120810cb5b3SFrank Munzert return urd; 121810cb5b3SFrank Munzert } 122810cb5b3SFrank Munzert 123810cb5b3SFrank Munzert static void urdev_free(struct urdev *urd) 124810cb5b3SFrank Munzert { 1258127a1f8SMichael Holzheu TRACE("urdev_free: %p\n", urd); 1268127a1f8SMichael Holzheu if (urd->cdev) 1278127a1f8SMichael Holzheu put_device(&urd->cdev->dev); 128810cb5b3SFrank Munzert kfree(urd); 129810cb5b3SFrank Munzert } 130810cb5b3SFrank Munzert 1318127a1f8SMichael Holzheu static void urdev_get(struct urdev *urd) 1328127a1f8SMichael Holzheu { 1330462d992SElena Reshetova refcount_inc(&urd->ref_count); 1348127a1f8SMichael Holzheu } 1358127a1f8SMichael Holzheu 1368127a1f8SMichael Holzheu static struct urdev *urdev_get_from_cdev(struct ccw_device *cdev) 1378127a1f8SMichael Holzheu { 1388127a1f8SMichael Holzheu struct urdev *urd; 1398127a1f8SMichael Holzheu unsigned long flags; 1408127a1f8SMichael Holzheu 1418127a1f8SMichael Holzheu spin_lock_irqsave(get_ccwdev_lock(cdev), flags); 142dff59b64SGreg Kroah-Hartman urd = dev_get_drvdata(&cdev->dev); 1438127a1f8SMichael Holzheu if (urd) 1448127a1f8SMichael Holzheu urdev_get(urd); 1458127a1f8SMichael Holzheu spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); 1468127a1f8SMichael Holzheu return urd; 1478127a1f8SMichael Holzheu } 1488127a1f8SMichael Holzheu 149810cb5b3SFrank Munzert static struct urdev *urdev_get_from_devno(u16 devno) 150810cb5b3SFrank Munzert { 151810cb5b3SFrank Munzert char bus_id[16]; 152810cb5b3SFrank Munzert struct ccw_device *cdev; 1538127a1f8SMichael Holzheu struct urdev *urd; 154810cb5b3SFrank Munzert 155810cb5b3SFrank Munzert sprintf(bus_id, "0.0.%04x", devno); 156810cb5b3SFrank Munzert cdev = get_ccwdev_by_busid(&ur_driver, bus_id); 157810cb5b3SFrank Munzert if (!cdev) 158810cb5b3SFrank Munzert return NULL; 1598127a1f8SMichael Holzheu urd = urdev_get_from_cdev(cdev); 1608127a1f8SMichael Holzheu put_device(&cdev->dev); 1618127a1f8SMichael Holzheu return urd; 162810cb5b3SFrank Munzert } 163810cb5b3SFrank Munzert 164810cb5b3SFrank Munzert static void urdev_put(struct urdev *urd) 165810cb5b3SFrank Munzert { 1660462d992SElena Reshetova if (refcount_dec_and_test(&urd->ref_count)) 1678127a1f8SMichael Holzheu urdev_free(urd); 168810cb5b3SFrank Munzert } 169810cb5b3SFrank Munzert 170810cb5b3SFrank Munzert /* 171810cb5b3SFrank Munzert * Low-level functions to do I/O to a ur device. 172810cb5b3SFrank Munzert * alloc_chan_prog 1731eade380SMichael Holzheu * free_chan_prog 174810cb5b3SFrank Munzert * do_ur_io 175810cb5b3SFrank Munzert * ur_int_handler 176810cb5b3SFrank Munzert * 177810cb5b3SFrank Munzert * alloc_chan_prog allocates and builds the channel program 1781eade380SMichael Holzheu * free_chan_prog frees memory of the channel program 179810cb5b3SFrank Munzert * 180810cb5b3SFrank Munzert * do_ur_io issues the channel program to the device and blocks waiting 181810cb5b3SFrank Munzert * on a completion event it publishes at urd->io_done. The function 182810cb5b3SFrank Munzert * serialises itself on the device's mutex so that only one I/O 183810cb5b3SFrank Munzert * is issued at a time (and that I/O is synchronous). 184810cb5b3SFrank Munzert * 185810cb5b3SFrank Munzert * ur_int_handler catches the "I/O done" interrupt, writes the 186810cb5b3SFrank Munzert * subchannel status word into the scsw member of the urdev structure 187810cb5b3SFrank Munzert * and complete()s the io_done to wake the waiting do_ur_io. 188810cb5b3SFrank Munzert * 189810cb5b3SFrank Munzert * The caller of do_ur_io is responsible for kfree()ing the channel program 190810cb5b3SFrank Munzert * address pointer that alloc_chan_prog returned. 191810cb5b3SFrank Munzert */ 192810cb5b3SFrank Munzert 1931eade380SMichael Holzheu static void free_chan_prog(struct ccw1 *cpa) 1941eade380SMichael Holzheu { 1951eade380SMichael Holzheu struct ccw1 *ptr = cpa; 1961eade380SMichael Holzheu 1971eade380SMichael Holzheu while (ptr->cda) { 1981eade380SMichael Holzheu kfree((void *)(addr_t) ptr->cda); 1991eade380SMichael Holzheu ptr++; 2001eade380SMichael Holzheu } 2011eade380SMichael Holzheu kfree(cpa); 2021eade380SMichael Holzheu } 203810cb5b3SFrank Munzert 204810cb5b3SFrank Munzert /* 205810cb5b3SFrank Munzert * alloc_chan_prog 206810cb5b3SFrank Munzert * The channel program we use is write commands chained together 207810cb5b3SFrank Munzert * with a final NOP CCW command-chained on (which ensures that CE and DE 208810cb5b3SFrank Munzert * are presented together in a single interrupt instead of as separate 209810cb5b3SFrank Munzert * interrupts unless an incorrect length indication kicks in first). The 2101eade380SMichael Holzheu * data length in each CCW is reclen. 211810cb5b3SFrank Munzert */ 2121eade380SMichael Holzheu static struct ccw1 *alloc_chan_prog(const char __user *ubuf, int rec_count, 2131eade380SMichael Holzheu int reclen) 214810cb5b3SFrank Munzert { 215810cb5b3SFrank Munzert struct ccw1 *cpa; 2161eade380SMichael Holzheu void *kbuf; 217810cb5b3SFrank Munzert int i; 218810cb5b3SFrank Munzert 2191eade380SMichael Holzheu TRACE("alloc_chan_prog(%p, %i, %i)\n", ubuf, rec_count, reclen); 220810cb5b3SFrank Munzert 221810cb5b3SFrank Munzert /* 222810cb5b3SFrank Munzert * We chain a NOP onto the writes to force CE+DE together. 223810cb5b3SFrank Munzert * That means we allocate room for CCWs to cover count/reclen 224810cb5b3SFrank Munzert * records plus a NOP. 225810cb5b3SFrank Munzert */ 2266396bb22SKees Cook cpa = kcalloc(rec_count + 1, sizeof(struct ccw1), 2271eade380SMichael Holzheu GFP_KERNEL | GFP_DMA); 228810cb5b3SFrank Munzert if (!cpa) 2291eade380SMichael Holzheu return ERR_PTR(-ENOMEM); 230810cb5b3SFrank Munzert 2311eade380SMichael Holzheu for (i = 0; i < rec_count; i++) { 232810cb5b3SFrank Munzert cpa[i].cmd_code = WRITE_CCW_CMD; 233810cb5b3SFrank Munzert cpa[i].flags = CCW_FLAG_CC | CCW_FLAG_SLI; 234810cb5b3SFrank Munzert cpa[i].count = reclen; 2351eade380SMichael Holzheu kbuf = kmalloc(reclen, GFP_KERNEL | GFP_DMA); 2361eade380SMichael Holzheu if (!kbuf) { 2371eade380SMichael Holzheu free_chan_prog(cpa); 2381eade380SMichael Holzheu return ERR_PTR(-ENOMEM); 2391eade380SMichael Holzheu } 2401eade380SMichael Holzheu cpa[i].cda = (u32)(addr_t) kbuf; 2411eade380SMichael Holzheu if (copy_from_user(kbuf, ubuf, reclen)) { 2421eade380SMichael Holzheu free_chan_prog(cpa); 2431eade380SMichael Holzheu return ERR_PTR(-EFAULT); 2441eade380SMichael Holzheu } 2451eade380SMichael Holzheu ubuf += reclen; 246810cb5b3SFrank Munzert } 247810cb5b3SFrank Munzert /* The following NOP CCW forces CE+DE to be presented together */ 248810cb5b3SFrank Munzert cpa[i].cmd_code = CCW_CMD_NOOP; 249810cb5b3SFrank Munzert return cpa; 250810cb5b3SFrank Munzert } 251810cb5b3SFrank Munzert 252810cb5b3SFrank Munzert static int do_ur_io(struct urdev *urd, struct ccw1 *cpa) 253810cb5b3SFrank Munzert { 254810cb5b3SFrank Munzert int rc; 255810cb5b3SFrank Munzert struct ccw_device *cdev = urd->cdev; 256278bc68cSHeiko Carstens DECLARE_COMPLETION_ONSTACK(event); 257810cb5b3SFrank Munzert 258810cb5b3SFrank Munzert TRACE("do_ur_io: cpa=%p\n", cpa); 259810cb5b3SFrank Munzert 260810cb5b3SFrank Munzert rc = mutex_lock_interruptible(&urd->io_mutex); 261810cb5b3SFrank Munzert if (rc) 262810cb5b3SFrank Munzert return rc; 263810cb5b3SFrank Munzert 264810cb5b3SFrank Munzert urd->io_done = &event; 265810cb5b3SFrank Munzert 266810cb5b3SFrank Munzert spin_lock_irq(get_ccwdev_lock(cdev)); 267810cb5b3SFrank Munzert rc = ccw_device_start(cdev, cpa, 1, 0, 0); 268810cb5b3SFrank Munzert spin_unlock_irq(get_ccwdev_lock(cdev)); 269810cb5b3SFrank Munzert 270810cb5b3SFrank Munzert TRACE("do_ur_io: ccw_device_start returned %d\n", rc); 271810cb5b3SFrank Munzert if (rc) 272810cb5b3SFrank Munzert goto out; 273810cb5b3SFrank Munzert 274810cb5b3SFrank Munzert wait_for_completion(&event); 275810cb5b3SFrank Munzert TRACE("do_ur_io: I/O complete\n"); 276810cb5b3SFrank Munzert rc = 0; 277810cb5b3SFrank Munzert 278810cb5b3SFrank Munzert out: 279810cb5b3SFrank Munzert mutex_unlock(&urd->io_mutex); 280810cb5b3SFrank Munzert return rc; 281810cb5b3SFrank Munzert } 282810cb5b3SFrank Munzert 283bf18140dSPeter Oberparleiter static void ur_uevent(struct work_struct *ws) 284bf18140dSPeter Oberparleiter { 285bf18140dSPeter Oberparleiter struct urdev *urd = container_of(ws, struct urdev, uevent_work); 286bf18140dSPeter Oberparleiter char *envp[] = { 287bf18140dSPeter Oberparleiter "EVENT=unsol_de", /* Unsolicited device-end interrupt */ 288bf18140dSPeter Oberparleiter NULL 289bf18140dSPeter Oberparleiter }; 290bf18140dSPeter Oberparleiter 291bf18140dSPeter Oberparleiter kobject_uevent_env(&urd->cdev->dev.kobj, KOBJ_CHANGE, envp); 292bf18140dSPeter Oberparleiter urdev_put(urd); 293bf18140dSPeter Oberparleiter } 294bf18140dSPeter Oberparleiter 295810cb5b3SFrank Munzert /* 296810cb5b3SFrank Munzert * ur interrupt handler, called from the ccw_device layer 297810cb5b3SFrank Munzert */ 298810cb5b3SFrank Munzert static void ur_int_handler(struct ccw_device *cdev, unsigned long intparm, 299810cb5b3SFrank Munzert struct irb *irb) 300810cb5b3SFrank Munzert { 301810cb5b3SFrank Munzert struct urdev *urd; 302810cb5b3SFrank Munzert 303f622b517SSebastian Ott if (!IS_ERR(irb)) { 304810cb5b3SFrank Munzert TRACE("ur_int_handler: intparm=0x%lx cstat=%02x dstat=%02x res=%u\n", 30523d805b6SPeter Oberparleiter intparm, irb->scsw.cmd.cstat, irb->scsw.cmd.dstat, 30623d805b6SPeter Oberparleiter irb->scsw.cmd.count); 307f622b517SSebastian Ott } 308bf18140dSPeter Oberparleiter urd = dev_get_drvdata(&cdev->dev); 309810cb5b3SFrank Munzert if (!intparm) { 310810cb5b3SFrank Munzert TRACE("ur_int_handler: unsolicited interrupt\n"); 311bf18140dSPeter Oberparleiter 312bf18140dSPeter Oberparleiter if (scsw_dstat(&irb->scsw) & DEV_STAT_DEV_END) { 313bf18140dSPeter Oberparleiter /* 314bf18140dSPeter Oberparleiter * Userspace might be interested in a transition to 315bf18140dSPeter Oberparleiter * device-ready state. 316bf18140dSPeter Oberparleiter */ 317bf18140dSPeter Oberparleiter urdev_get(urd); 318bf18140dSPeter Oberparleiter schedule_work(&urd->uevent_work); 319bf18140dSPeter Oberparleiter } 320bf18140dSPeter Oberparleiter 321810cb5b3SFrank Munzert return; 322810cb5b3SFrank Munzert } 323810cb5b3SFrank Munzert /* On special conditions irb is an error pointer */ 324810cb5b3SFrank Munzert if (IS_ERR(irb)) 325810cb5b3SFrank Munzert urd->io_request_rc = PTR_ERR(irb); 32623d805b6SPeter Oberparleiter else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END)) 327810cb5b3SFrank Munzert urd->io_request_rc = 0; 328810cb5b3SFrank Munzert else 329810cb5b3SFrank Munzert urd->io_request_rc = -EIO; 330810cb5b3SFrank Munzert 331810cb5b3SFrank Munzert complete(urd->io_done); 332810cb5b3SFrank Munzert } 333810cb5b3SFrank Munzert 334810cb5b3SFrank Munzert /* 335810cb5b3SFrank Munzert * reclen sysfs attribute - The record length to be used for write CCWs 336810cb5b3SFrank Munzert */ 337810cb5b3SFrank Munzert static ssize_t ur_attr_reclen_show(struct device *dev, 338810cb5b3SFrank Munzert struct device_attribute *attr, char *buf) 339810cb5b3SFrank Munzert { 3408127a1f8SMichael Holzheu struct urdev *urd; 3418127a1f8SMichael Holzheu int rc; 342810cb5b3SFrank Munzert 3438127a1f8SMichael Holzheu urd = urdev_get_from_cdev(to_ccwdev(dev)); 3448127a1f8SMichael Holzheu if (!urd) 3458127a1f8SMichael Holzheu return -ENODEV; 3468127a1f8SMichael Holzheu rc = sprintf(buf, "%zu\n", urd->reclen); 3478127a1f8SMichael Holzheu urdev_put(urd); 3488127a1f8SMichael Holzheu return rc; 349810cb5b3SFrank Munzert } 350810cb5b3SFrank Munzert 351810cb5b3SFrank Munzert static DEVICE_ATTR(reclen, 0444, ur_attr_reclen_show, NULL); 352810cb5b3SFrank Munzert 353810cb5b3SFrank Munzert static int ur_create_attributes(struct device *dev) 354810cb5b3SFrank Munzert { 355810cb5b3SFrank Munzert return device_create_file(dev, &dev_attr_reclen); 356810cb5b3SFrank Munzert } 357810cb5b3SFrank Munzert 358810cb5b3SFrank Munzert static void ur_remove_attributes(struct device *dev) 359810cb5b3SFrank Munzert { 360810cb5b3SFrank Munzert device_remove_file(dev, &dev_attr_reclen); 361810cb5b3SFrank Munzert } 362810cb5b3SFrank Munzert 363810cb5b3SFrank Munzert /* 364810cb5b3SFrank Munzert * diagnose code 0x210 - retrieve device information 365810cb5b3SFrank Munzert * cc=0 normal completion, we have a real device 366810cb5b3SFrank Munzert * cc=1 CP paging error 367810cb5b3SFrank Munzert * cc=2 The virtual device exists, but is not associated with a real device 368810cb5b3SFrank Munzert * cc=3 Invalid device address, or the virtual device does not exist 369810cb5b3SFrank Munzert */ 370810cb5b3SFrank Munzert static int get_urd_class(struct urdev *urd) 371810cb5b3SFrank Munzert { 372810cb5b3SFrank Munzert static struct diag210 ur_diag210; 373810cb5b3SFrank Munzert int cc; 374810cb5b3SFrank Munzert 375810cb5b3SFrank Munzert ur_diag210.vrdcdvno = urd->dev_id.devno; 376810cb5b3SFrank Munzert ur_diag210.vrdclen = sizeof(struct diag210); 377810cb5b3SFrank Munzert 378810cb5b3SFrank Munzert cc = diag210(&ur_diag210); 379810cb5b3SFrank Munzert switch (cc) { 380810cb5b3SFrank Munzert case 0: 381b9993a38SFrank Munzert return -EOPNOTSUPP; 382810cb5b3SFrank Munzert case 2: 383810cb5b3SFrank Munzert return ur_diag210.vrdcvcla; /* virtual device class */ 384810cb5b3SFrank Munzert case 3: 385810cb5b3SFrank Munzert return -ENODEV; 386810cb5b3SFrank Munzert default: 387810cb5b3SFrank Munzert return -EIO; 388810cb5b3SFrank Munzert } 389810cb5b3SFrank Munzert } 390810cb5b3SFrank Munzert 391810cb5b3SFrank Munzert /* 392810cb5b3SFrank Munzert * Allocation and freeing of urfile structures 393810cb5b3SFrank Munzert */ 394810cb5b3SFrank Munzert static struct urfile *urfile_alloc(struct urdev *urd) 395810cb5b3SFrank Munzert { 396810cb5b3SFrank Munzert struct urfile *urf; 397810cb5b3SFrank Munzert 398810cb5b3SFrank Munzert urf = kzalloc(sizeof(struct urfile), GFP_KERNEL); 399810cb5b3SFrank Munzert if (!urf) 400810cb5b3SFrank Munzert return NULL; 401810cb5b3SFrank Munzert urf->urd = urd; 402810cb5b3SFrank Munzert 403810cb5b3SFrank Munzert TRACE("urfile_alloc: urd=%p urf=%p rl=%zu\n", urd, urf, 404810cb5b3SFrank Munzert urf->dev_reclen); 405810cb5b3SFrank Munzert 406810cb5b3SFrank Munzert return urf; 407810cb5b3SFrank Munzert } 408810cb5b3SFrank Munzert 409810cb5b3SFrank Munzert static void urfile_free(struct urfile *urf) 410810cb5b3SFrank Munzert { 411810cb5b3SFrank Munzert TRACE("urfile_free: urf=%p urd=%p\n", urf, urf->urd); 412810cb5b3SFrank Munzert kfree(urf); 413810cb5b3SFrank Munzert } 414810cb5b3SFrank Munzert 415810cb5b3SFrank Munzert /* 416810cb5b3SFrank Munzert * The fops implementation of the character device driver 417810cb5b3SFrank Munzert */ 418810cb5b3SFrank Munzert static ssize_t do_write(struct urdev *urd, const char __user *udata, 419810cb5b3SFrank Munzert size_t count, size_t reclen, loff_t *ppos) 420810cb5b3SFrank Munzert { 421810cb5b3SFrank Munzert struct ccw1 *cpa; 422810cb5b3SFrank Munzert int rc; 423810cb5b3SFrank Munzert 4241eade380SMichael Holzheu cpa = alloc_chan_prog(udata, count / reclen, reclen); 4251eade380SMichael Holzheu if (IS_ERR(cpa)) 4261eade380SMichael Holzheu return PTR_ERR(cpa); 427810cb5b3SFrank Munzert 428810cb5b3SFrank Munzert rc = do_ur_io(urd, cpa); 429810cb5b3SFrank Munzert if (rc) 430810cb5b3SFrank Munzert goto fail_kfree_cpa; 431810cb5b3SFrank Munzert 432810cb5b3SFrank Munzert if (urd->io_request_rc) { 433810cb5b3SFrank Munzert rc = urd->io_request_rc; 434810cb5b3SFrank Munzert goto fail_kfree_cpa; 435810cb5b3SFrank Munzert } 436810cb5b3SFrank Munzert *ppos += count; 437810cb5b3SFrank Munzert rc = count; 4381eade380SMichael Holzheu 439810cb5b3SFrank Munzert fail_kfree_cpa: 4401eade380SMichael Holzheu free_chan_prog(cpa); 441810cb5b3SFrank Munzert return rc; 442810cb5b3SFrank Munzert } 443810cb5b3SFrank Munzert 444810cb5b3SFrank Munzert static ssize_t ur_write(struct file *file, const char __user *udata, 445810cb5b3SFrank Munzert size_t count, loff_t *ppos) 446810cb5b3SFrank Munzert { 447810cb5b3SFrank Munzert struct urfile *urf = file->private_data; 448810cb5b3SFrank Munzert 449810cb5b3SFrank Munzert TRACE("ur_write: count=%zu\n", count); 450810cb5b3SFrank Munzert 451810cb5b3SFrank Munzert if (count == 0) 452810cb5b3SFrank Munzert return 0; 453810cb5b3SFrank Munzert 454810cb5b3SFrank Munzert if (count % urf->dev_reclen) 455810cb5b3SFrank Munzert return -EINVAL; /* count must be a multiple of reclen */ 456810cb5b3SFrank Munzert 457810cb5b3SFrank Munzert if (count > urf->dev_reclen * MAX_RECS_PER_IO) 458810cb5b3SFrank Munzert count = urf->dev_reclen * MAX_RECS_PER_IO; 459810cb5b3SFrank Munzert 460810cb5b3SFrank Munzert return do_write(urf->urd, udata, count, urf->dev_reclen, ppos); 461810cb5b3SFrank Munzert } 462810cb5b3SFrank Munzert 463810cb5b3SFrank Munzert /* 464810cb5b3SFrank Munzert * diagnose code 0x14 subcode 0x0028 - position spool file to designated 465810cb5b3SFrank Munzert * record 466810cb5b3SFrank Munzert * cc=0 normal completion 467810cb5b3SFrank Munzert * cc=2 no file active on the virtual reader or device not ready 468810cb5b3SFrank Munzert * cc=3 record specified is beyond EOF 469810cb5b3SFrank Munzert */ 470810cb5b3SFrank Munzert static int diag_position_to_record(int devno, int record) 471810cb5b3SFrank Munzert { 472810cb5b3SFrank Munzert int cc; 473810cb5b3SFrank Munzert 4740a87c5cfSMichael Holzheu cc = diag14(record, devno, 0x28); 475810cb5b3SFrank Munzert switch (cc) { 476810cb5b3SFrank Munzert case 0: 477810cb5b3SFrank Munzert return 0; 478810cb5b3SFrank Munzert case 2: 479810cb5b3SFrank Munzert return -ENOMEDIUM; 480810cb5b3SFrank Munzert case 3: 481810cb5b3SFrank Munzert return -ENODATA; /* position beyond end of file */ 482810cb5b3SFrank Munzert default: 483810cb5b3SFrank Munzert return -EIO; 484810cb5b3SFrank Munzert } 485810cb5b3SFrank Munzert } 486810cb5b3SFrank Munzert 487810cb5b3SFrank Munzert /* 488810cb5b3SFrank Munzert * diagnose code 0x14 subcode 0x0000 - read next spool file buffer 489810cb5b3SFrank Munzert * cc=0 normal completion 490810cb5b3SFrank Munzert * cc=1 EOF reached 491810cb5b3SFrank Munzert * cc=2 no file active on the virtual reader, and no file eligible 492810cb5b3SFrank Munzert * cc=3 file already active on the virtual reader or specified virtual 493810cb5b3SFrank Munzert * reader does not exist or is not a reader 494810cb5b3SFrank Munzert */ 495810cb5b3SFrank Munzert static int diag_read_file(int devno, char *buf) 496810cb5b3SFrank Munzert { 497810cb5b3SFrank Munzert int cc; 498810cb5b3SFrank Munzert 4990a87c5cfSMichael Holzheu cc = diag14((unsigned long) buf, devno, 0x00); 500810cb5b3SFrank Munzert switch (cc) { 501810cb5b3SFrank Munzert case 0: 502810cb5b3SFrank Munzert return 0; 503810cb5b3SFrank Munzert case 1: 504810cb5b3SFrank Munzert return -ENODATA; 505810cb5b3SFrank Munzert case 2: 506810cb5b3SFrank Munzert return -ENOMEDIUM; 507810cb5b3SFrank Munzert default: 508810cb5b3SFrank Munzert return -EIO; 509810cb5b3SFrank Munzert } 510810cb5b3SFrank Munzert } 511810cb5b3SFrank Munzert 512810cb5b3SFrank Munzert static ssize_t diag14_read(struct file *file, char __user *ubuf, size_t count, 513810cb5b3SFrank Munzert loff_t *offs) 514810cb5b3SFrank Munzert { 515810cb5b3SFrank Munzert size_t len, copied, res; 516810cb5b3SFrank Munzert char *buf; 517810cb5b3SFrank Munzert int rc; 518810cb5b3SFrank Munzert u16 reclen; 519810cb5b3SFrank Munzert struct urdev *urd; 520810cb5b3SFrank Munzert 521810cb5b3SFrank Munzert urd = ((struct urfile *) file->private_data)->urd; 522810cb5b3SFrank Munzert reclen = ((struct urfile *) file->private_data)->file_reclen; 523810cb5b3SFrank Munzert 524810cb5b3SFrank Munzert rc = diag_position_to_record(urd->dev_id.devno, *offs / PAGE_SIZE + 1); 525810cb5b3SFrank Munzert if (rc == -ENODATA) 526810cb5b3SFrank Munzert return 0; 527810cb5b3SFrank Munzert if (rc) 528810cb5b3SFrank Munzert return rc; 529810cb5b3SFrank Munzert 530810cb5b3SFrank Munzert len = min((size_t) PAGE_SIZE, count); 5313eed13ccSMichael Holzheu buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA); 532810cb5b3SFrank Munzert if (!buf) 533810cb5b3SFrank Munzert return -ENOMEM; 534810cb5b3SFrank Munzert 535810cb5b3SFrank Munzert copied = 0; 536810cb5b3SFrank Munzert res = (size_t) (*offs % PAGE_SIZE); 537810cb5b3SFrank Munzert do { 538810cb5b3SFrank Munzert rc = diag_read_file(urd->dev_id.devno, buf); 539810cb5b3SFrank Munzert if (rc == -ENODATA) { 540810cb5b3SFrank Munzert break; 541810cb5b3SFrank Munzert } 542810cb5b3SFrank Munzert if (rc) 543810cb5b3SFrank Munzert goto fail; 5442b3d8c9eSFrank Munzert if (reclen && (copied == 0) && (*offs < PAGE_SIZE)) 545810cb5b3SFrank Munzert *((u16 *) &buf[FILE_RECLEN_OFFSET]) = reclen; 546810cb5b3SFrank Munzert len = min(count - copied, PAGE_SIZE - res); 547810cb5b3SFrank Munzert if (copy_to_user(ubuf + copied, buf + res, len)) { 548810cb5b3SFrank Munzert rc = -EFAULT; 549810cb5b3SFrank Munzert goto fail; 550810cb5b3SFrank Munzert } 551810cb5b3SFrank Munzert res = 0; 552810cb5b3SFrank Munzert copied += len; 553810cb5b3SFrank Munzert } while (copied != count); 554810cb5b3SFrank Munzert 555810cb5b3SFrank Munzert *offs += copied; 556810cb5b3SFrank Munzert rc = copied; 557810cb5b3SFrank Munzert fail: 5583eed13ccSMichael Holzheu free_page((unsigned long) buf); 559810cb5b3SFrank Munzert return rc; 560810cb5b3SFrank Munzert } 561810cb5b3SFrank Munzert 562810cb5b3SFrank Munzert static ssize_t ur_read(struct file *file, char __user *ubuf, size_t count, 563810cb5b3SFrank Munzert loff_t *offs) 564810cb5b3SFrank Munzert { 565810cb5b3SFrank Munzert struct urdev *urd; 566810cb5b3SFrank Munzert int rc; 567810cb5b3SFrank Munzert 568810cb5b3SFrank Munzert TRACE("ur_read: count=%zu ppos=%li\n", count, (unsigned long) *offs); 569810cb5b3SFrank Munzert 570810cb5b3SFrank Munzert if (count == 0) 571810cb5b3SFrank Munzert return 0; 572810cb5b3SFrank Munzert 573810cb5b3SFrank Munzert urd = ((struct urfile *) file->private_data)->urd; 574810cb5b3SFrank Munzert rc = mutex_lock_interruptible(&urd->io_mutex); 575810cb5b3SFrank Munzert if (rc) 576810cb5b3SFrank Munzert return rc; 577810cb5b3SFrank Munzert rc = diag14_read(file, ubuf, count, offs); 578810cb5b3SFrank Munzert mutex_unlock(&urd->io_mutex); 579810cb5b3SFrank Munzert return rc; 580810cb5b3SFrank Munzert } 581810cb5b3SFrank Munzert 582810cb5b3SFrank Munzert /* 583810cb5b3SFrank Munzert * diagnose code 0x14 subcode 0x0fff - retrieve next file descriptor 584810cb5b3SFrank Munzert * cc=0 normal completion 585810cb5b3SFrank Munzert * cc=1 no files on reader queue or no subsequent file 586810cb5b3SFrank Munzert * cc=2 spid specified is invalid 587810cb5b3SFrank Munzert */ 588810cb5b3SFrank Munzert static int diag_read_next_file_info(struct file_control_block *buf, int spid) 589810cb5b3SFrank Munzert { 590810cb5b3SFrank Munzert int cc; 591810cb5b3SFrank Munzert 5920a87c5cfSMichael Holzheu cc = diag14((unsigned long) buf, spid, 0xfff); 593810cb5b3SFrank Munzert switch (cc) { 594810cb5b3SFrank Munzert case 0: 595810cb5b3SFrank Munzert return 0; 596810cb5b3SFrank Munzert default: 597810cb5b3SFrank Munzert return -ENODATA; 598810cb5b3SFrank Munzert } 599810cb5b3SFrank Munzert } 600810cb5b3SFrank Munzert 6013eed13ccSMichael Holzheu static int verify_uri_device(struct urdev *urd) 602810cb5b3SFrank Munzert { 6033eed13ccSMichael Holzheu struct file_control_block *fcb; 604810cb5b3SFrank Munzert char *buf; 605810cb5b3SFrank Munzert int rc; 606810cb5b3SFrank Munzert 6073eed13ccSMichael Holzheu fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA); 6083eed13ccSMichael Holzheu if (!fcb) 6093eed13ccSMichael Holzheu return -ENOMEM; 6103eed13ccSMichael Holzheu 6113eed13ccSMichael Holzheu /* check for empty reader device (beginning of chain) */ 6123eed13ccSMichael Holzheu rc = diag_read_next_file_info(fcb, 0); 6133eed13ccSMichael Holzheu if (rc) 6143eed13ccSMichael Holzheu goto fail_free_fcb; 6153eed13ccSMichael Holzheu 6163eed13ccSMichael Holzheu /* if file is in hold status, we do not read it */ 6173eed13ccSMichael Holzheu if (fcb->file_stat & (FLG_SYSTEM_HOLD | FLG_USER_HOLD)) { 6183eed13ccSMichael Holzheu rc = -EPERM; 6193eed13ccSMichael Holzheu goto fail_free_fcb; 6203eed13ccSMichael Holzheu } 6213eed13ccSMichael Holzheu 6223eed13ccSMichael Holzheu /* open file on virtual reader */ 6233eed13ccSMichael Holzheu buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA); 6243eed13ccSMichael Holzheu if (!buf) { 6253eed13ccSMichael Holzheu rc = -ENOMEM; 6263eed13ccSMichael Holzheu goto fail_free_fcb; 6273eed13ccSMichael Holzheu } 6283eed13ccSMichael Holzheu rc = diag_read_file(urd->dev_id.devno, buf); 6293eed13ccSMichael Holzheu if ((rc != 0) && (rc != -ENODATA)) /* EOF does not hurt */ 6303eed13ccSMichael Holzheu goto fail_free_buf; 6313eed13ccSMichael Holzheu 6323eed13ccSMichael Holzheu /* check if the file on top of the queue is open now */ 6333eed13ccSMichael Holzheu rc = diag_read_next_file_info(fcb, 0); 6343eed13ccSMichael Holzheu if (rc) 6353eed13ccSMichael Holzheu goto fail_free_buf; 6363eed13ccSMichael Holzheu if (!(fcb->file_stat & FLG_IN_USE)) { 6373eed13ccSMichael Holzheu rc = -EMFILE; 6383eed13ccSMichael Holzheu goto fail_free_buf; 6393eed13ccSMichael Holzheu } 6403eed13ccSMichael Holzheu rc = 0; 6413eed13ccSMichael Holzheu 6423eed13ccSMichael Holzheu fail_free_buf: 6433eed13ccSMichael Holzheu free_page((unsigned long) buf); 6443eed13ccSMichael Holzheu fail_free_fcb: 6453eed13ccSMichael Holzheu kfree(fcb); 6463eed13ccSMichael Holzheu return rc; 6473eed13ccSMichael Holzheu } 6483eed13ccSMichael Holzheu 6493eed13ccSMichael Holzheu static int verify_device(struct urdev *urd) 6503eed13ccSMichael Holzheu { 651810cb5b3SFrank Munzert switch (urd->class) { 652810cb5b3SFrank Munzert case DEV_CLASS_UR_O: 653810cb5b3SFrank Munzert return 0; /* no check needed here */ 654810cb5b3SFrank Munzert case DEV_CLASS_UR_I: 6553eed13ccSMichael Holzheu return verify_uri_device(urd); 656810cb5b3SFrank Munzert default: 657b9993a38SFrank Munzert return -EOPNOTSUPP; 658810cb5b3SFrank Munzert } 659810cb5b3SFrank Munzert } 660810cb5b3SFrank Munzert 6613eed13ccSMichael Holzheu static int get_uri_file_reclen(struct urdev *urd) 6623eed13ccSMichael Holzheu { 6633eed13ccSMichael Holzheu struct file_control_block *fcb; 6643eed13ccSMichael Holzheu int rc; 6653eed13ccSMichael Holzheu 6663eed13ccSMichael Holzheu fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA); 6673eed13ccSMichael Holzheu if (!fcb) 6683eed13ccSMichael Holzheu return -ENOMEM; 6693eed13ccSMichael Holzheu rc = diag_read_next_file_info(fcb, 0); 6703eed13ccSMichael Holzheu if (rc) 6713eed13ccSMichael Holzheu goto fail_free; 6723eed13ccSMichael Holzheu if (fcb->file_stat & FLG_CP_DUMP) 6733eed13ccSMichael Holzheu rc = 0; 6743eed13ccSMichael Holzheu else 6753eed13ccSMichael Holzheu rc = fcb->rec_len; 6763eed13ccSMichael Holzheu 6773eed13ccSMichael Holzheu fail_free: 6783eed13ccSMichael Holzheu kfree(fcb); 6793eed13ccSMichael Holzheu return rc; 6803eed13ccSMichael Holzheu } 6813eed13ccSMichael Holzheu 682810cb5b3SFrank Munzert static int get_file_reclen(struct urdev *urd) 683810cb5b3SFrank Munzert { 684810cb5b3SFrank Munzert switch (urd->class) { 685810cb5b3SFrank Munzert case DEV_CLASS_UR_O: 686810cb5b3SFrank Munzert return 0; 687810cb5b3SFrank Munzert case DEV_CLASS_UR_I: 6883eed13ccSMichael Holzheu return get_uri_file_reclen(urd); 689810cb5b3SFrank Munzert default: 690b9993a38SFrank Munzert return -EOPNOTSUPP; 691810cb5b3SFrank Munzert } 692810cb5b3SFrank Munzert } 693810cb5b3SFrank Munzert 694810cb5b3SFrank Munzert static int ur_open(struct inode *inode, struct file *file) 695810cb5b3SFrank Munzert { 696810cb5b3SFrank Munzert u16 devno; 697810cb5b3SFrank Munzert struct urdev *urd; 698810cb5b3SFrank Munzert struct urfile *urf; 699810cb5b3SFrank Munzert unsigned short accmode; 700810cb5b3SFrank Munzert int rc; 701810cb5b3SFrank Munzert 702810cb5b3SFrank Munzert accmode = file->f_flags & O_ACCMODE; 703810cb5b3SFrank Munzert 704810cb5b3SFrank Munzert if (accmode == O_RDWR) 705810cb5b3SFrank Munzert return -EACCES; 706810cb5b3SFrank Munzert /* 707810cb5b3SFrank Munzert * We treat the minor number as the devno of the ur device 708810cb5b3SFrank Munzert * to find in the driver tree. 709810cb5b3SFrank Munzert */ 7106f24784fSAl Viro devno = iminor(file_inode(file)); 711810cb5b3SFrank Munzert 712810cb5b3SFrank Munzert urd = urdev_get_from_devno(devno); 713764a4a8eSJonathan Corbet if (!urd) { 714764a4a8eSJonathan Corbet rc = -ENXIO; 715764a4a8eSJonathan Corbet goto out; 716764a4a8eSJonathan Corbet } 717810cb5b3SFrank Munzert 718a695f167SFrank Munzert spin_lock(&urd->open_lock); 719a695f167SFrank Munzert while (urd->open_flag) { 720a695f167SFrank Munzert spin_unlock(&urd->open_lock); 721810cb5b3SFrank Munzert if (file->f_flags & O_NONBLOCK) { 722810cb5b3SFrank Munzert rc = -EBUSY; 723810cb5b3SFrank Munzert goto fail_put; 724810cb5b3SFrank Munzert } 725a695f167SFrank Munzert if (wait_event_interruptible(urd->wait, urd->open_flag == 0)) { 726810cb5b3SFrank Munzert rc = -ERESTARTSYS; 727810cb5b3SFrank Munzert goto fail_put; 728810cb5b3SFrank Munzert } 729a695f167SFrank Munzert spin_lock(&urd->open_lock); 730810cb5b3SFrank Munzert } 731a695f167SFrank Munzert urd->open_flag++; 732a695f167SFrank Munzert spin_unlock(&urd->open_lock); 733810cb5b3SFrank Munzert 734810cb5b3SFrank Munzert TRACE("ur_open\n"); 735810cb5b3SFrank Munzert 736810cb5b3SFrank Munzert if (((accmode == O_RDONLY) && (urd->class != DEV_CLASS_UR_I)) || 737810cb5b3SFrank Munzert ((accmode == O_WRONLY) && (urd->class != DEV_CLASS_UR_O))) { 738810cb5b3SFrank Munzert TRACE("ur_open: unsupported dev class (%d)\n", urd->class); 739810cb5b3SFrank Munzert rc = -EACCES; 740810cb5b3SFrank Munzert goto fail_unlock; 741810cb5b3SFrank Munzert } 742810cb5b3SFrank Munzert 743810cb5b3SFrank Munzert rc = verify_device(urd); 744810cb5b3SFrank Munzert if (rc) 745810cb5b3SFrank Munzert goto fail_unlock; 746810cb5b3SFrank Munzert 747810cb5b3SFrank Munzert urf = urfile_alloc(urd); 748810cb5b3SFrank Munzert if (!urf) { 749810cb5b3SFrank Munzert rc = -ENOMEM; 750810cb5b3SFrank Munzert goto fail_unlock; 751810cb5b3SFrank Munzert } 752810cb5b3SFrank Munzert 753810cb5b3SFrank Munzert urf->dev_reclen = urd->reclen; 754810cb5b3SFrank Munzert rc = get_file_reclen(urd); 755810cb5b3SFrank Munzert if (rc < 0) 756810cb5b3SFrank Munzert goto fail_urfile_free; 757810cb5b3SFrank Munzert urf->file_reclen = rc; 758810cb5b3SFrank Munzert file->private_data = urf; 759810cb5b3SFrank Munzert return 0; 760810cb5b3SFrank Munzert 761810cb5b3SFrank Munzert fail_urfile_free: 762810cb5b3SFrank Munzert urfile_free(urf); 763810cb5b3SFrank Munzert fail_unlock: 764a695f167SFrank Munzert spin_lock(&urd->open_lock); 765a695f167SFrank Munzert urd->open_flag--; 766a695f167SFrank Munzert spin_unlock(&urd->open_lock); 767810cb5b3SFrank Munzert fail_put: 768810cb5b3SFrank Munzert urdev_put(urd); 769764a4a8eSJonathan Corbet out: 770810cb5b3SFrank Munzert return rc; 771810cb5b3SFrank Munzert } 772810cb5b3SFrank Munzert 773810cb5b3SFrank Munzert static int ur_release(struct inode *inode, struct file *file) 774810cb5b3SFrank Munzert { 775810cb5b3SFrank Munzert struct urfile *urf = file->private_data; 776810cb5b3SFrank Munzert 777810cb5b3SFrank Munzert TRACE("ur_release\n"); 778a695f167SFrank Munzert spin_lock(&urf->urd->open_lock); 779a695f167SFrank Munzert urf->urd->open_flag--; 780a695f167SFrank Munzert spin_unlock(&urf->urd->open_lock); 781a695f167SFrank Munzert wake_up_interruptible(&urf->urd->wait); 782810cb5b3SFrank Munzert urdev_put(urf->urd); 783810cb5b3SFrank Munzert urfile_free(urf); 784810cb5b3SFrank Munzert return 0; 785810cb5b3SFrank Munzert } 786810cb5b3SFrank Munzert 787810cb5b3SFrank Munzert static loff_t ur_llseek(struct file *file, loff_t offset, int whence) 788810cb5b3SFrank Munzert { 789810cb5b3SFrank Munzert if ((file->f_flags & O_ACCMODE) != O_RDONLY) 790810cb5b3SFrank Munzert return -ESPIPE; /* seek allowed only for reader */ 791810cb5b3SFrank Munzert if (offset % PAGE_SIZE) 792810cb5b3SFrank Munzert return -ESPIPE; /* only multiples of 4K allowed */ 793b25472f9SAl Viro return no_seek_end_llseek(file, offset, whence); 794810cb5b3SFrank Munzert } 795810cb5b3SFrank Munzert 7965c81cdbeSJan Engelhardt static const struct file_operations ur_fops = { 797810cb5b3SFrank Munzert .owner = THIS_MODULE, 798810cb5b3SFrank Munzert .open = ur_open, 799810cb5b3SFrank Munzert .release = ur_release, 800810cb5b3SFrank Munzert .read = ur_read, 801810cb5b3SFrank Munzert .write = ur_write, 802810cb5b3SFrank Munzert .llseek = ur_llseek, 803810cb5b3SFrank Munzert }; 804810cb5b3SFrank Munzert 805810cb5b3SFrank Munzert /* 806810cb5b3SFrank Munzert * ccw_device infrastructure: 8078127a1f8SMichael Holzheu * ur_probe creates the struct urdev (with refcount = 1), the device 8088127a1f8SMichael Holzheu * attributes, sets up the interrupt handler and validates the virtual 8098127a1f8SMichael Holzheu * unit record device. 8108127a1f8SMichael Holzheu * ur_remove removes the device attributes and drops the reference to 8118127a1f8SMichael Holzheu * struct urdev. 8128127a1f8SMichael Holzheu * 8138127a1f8SMichael Holzheu * ur_probe, ur_remove, ur_set_online and ur_set_offline are serialized 8148127a1f8SMichael Holzheu * by the vmur_mutex lock. 8158127a1f8SMichael Holzheu * 8168127a1f8SMichael Holzheu * urd->char_device is used as indication that the online function has 8178127a1f8SMichael Holzheu * been completed successfully. 818810cb5b3SFrank Munzert */ 819810cb5b3SFrank Munzert static int ur_probe(struct ccw_device *cdev) 820810cb5b3SFrank Munzert { 821810cb5b3SFrank Munzert struct urdev *urd; 822810cb5b3SFrank Munzert int rc; 823810cb5b3SFrank Munzert 8248127a1f8SMichael Holzheu TRACE("ur_probe: cdev=%p\n", cdev); 825810cb5b3SFrank Munzert 8268127a1f8SMichael Holzheu mutex_lock(&vmur_mutex); 827810cb5b3SFrank Munzert urd = urdev_alloc(cdev); 828810cb5b3SFrank Munzert if (!urd) { 829810cb5b3SFrank Munzert rc = -ENOMEM; 8308127a1f8SMichael Holzheu goto fail_unlock; 831810cb5b3SFrank Munzert } 8328127a1f8SMichael Holzheu 833810cb5b3SFrank Munzert rc = ur_create_attributes(&cdev->dev); 834810cb5b3SFrank Munzert if (rc) { 835810cb5b3SFrank Munzert rc = -ENOMEM; 8368127a1f8SMichael Holzheu goto fail_urdev_put; 837810cb5b3SFrank Munzert } 838810cb5b3SFrank Munzert 839810cb5b3SFrank Munzert /* validate virtual unit record device */ 840810cb5b3SFrank Munzert urd->class = get_urd_class(urd); 841810cb5b3SFrank Munzert if (urd->class < 0) { 842810cb5b3SFrank Munzert rc = urd->class; 8438127a1f8SMichael Holzheu goto fail_remove_attr; 844810cb5b3SFrank Munzert } 845810cb5b3SFrank Munzert if ((urd->class != DEV_CLASS_UR_I) && (urd->class != DEV_CLASS_UR_O)) { 846b9993a38SFrank Munzert rc = -EOPNOTSUPP; 8478127a1f8SMichael Holzheu goto fail_remove_attr; 848810cb5b3SFrank Munzert } 8498127a1f8SMichael Holzheu spin_lock_irq(get_ccwdev_lock(cdev)); 850dff59b64SGreg Kroah-Hartman dev_set_drvdata(&cdev->dev, urd); 851f3e59ff3SPeter Oberparleiter cdev->handler = ur_int_handler; 8528127a1f8SMichael Holzheu spin_unlock_irq(get_ccwdev_lock(cdev)); 853810cb5b3SFrank Munzert 8548127a1f8SMichael Holzheu mutex_unlock(&vmur_mutex); 855810cb5b3SFrank Munzert return 0; 856810cb5b3SFrank Munzert 8578127a1f8SMichael Holzheu fail_remove_attr: 858810cb5b3SFrank Munzert ur_remove_attributes(&cdev->dev); 8598127a1f8SMichael Holzheu fail_urdev_put: 8608127a1f8SMichael Holzheu urdev_put(urd); 8618127a1f8SMichael Holzheu fail_unlock: 8628127a1f8SMichael Holzheu mutex_unlock(&vmur_mutex); 8638127a1f8SMichael Holzheu return rc; 864810cb5b3SFrank Munzert } 865810cb5b3SFrank Munzert 866810cb5b3SFrank Munzert static int ur_set_online(struct ccw_device *cdev) 867810cb5b3SFrank Munzert { 868810cb5b3SFrank Munzert struct urdev *urd; 869810cb5b3SFrank Munzert int minor, major, rc; 870810cb5b3SFrank Munzert char node_id[16]; 871810cb5b3SFrank Munzert 8728127a1f8SMichael Holzheu TRACE("ur_set_online: cdev=%p\n", cdev); 873810cb5b3SFrank Munzert 8748127a1f8SMichael Holzheu mutex_lock(&vmur_mutex); 8758127a1f8SMichael Holzheu urd = urdev_get_from_cdev(cdev); 8768127a1f8SMichael Holzheu if (!urd) { 8778127a1f8SMichael Holzheu /* ur_remove already deleted our urd */ 8788127a1f8SMichael Holzheu rc = -ENODEV; 8798127a1f8SMichael Holzheu goto fail_unlock; 8808127a1f8SMichael Holzheu } 881810cb5b3SFrank Munzert 8828127a1f8SMichael Holzheu if (urd->char_device) { 8838127a1f8SMichael Holzheu /* Another ur_set_online was faster */ 8848127a1f8SMichael Holzheu rc = -EBUSY; 8858127a1f8SMichael Holzheu goto fail_urdev_put; 8868127a1f8SMichael Holzheu } 8878127a1f8SMichael Holzheu 888810cb5b3SFrank Munzert minor = urd->dev_id.devno; 889810cb5b3SFrank Munzert major = MAJOR(ur_first_dev_maj_min); 890810cb5b3SFrank Munzert 891810cb5b3SFrank Munzert urd->char_device = cdev_alloc(); 892810cb5b3SFrank Munzert if (!urd->char_device) { 893810cb5b3SFrank Munzert rc = -ENOMEM; 8948127a1f8SMichael Holzheu goto fail_urdev_put; 895810cb5b3SFrank Munzert } 896810cb5b3SFrank Munzert 897dae1df42SDennis Chen urd->char_device->ops = &ur_fops; 898810cb5b3SFrank Munzert urd->char_device->owner = ur_fops.owner; 899810cb5b3SFrank Munzert 900b08e19deSJean Delvare rc = cdev_add(urd->char_device, MKDEV(major, minor), 1); 901810cb5b3SFrank Munzert if (rc) 902810cb5b3SFrank Munzert goto fail_free_cdev; 903810cb5b3SFrank Munzert if (urd->cdev->id.cu_type == READER_PUNCH_DEVTYPE) { 904810cb5b3SFrank Munzert if (urd->class == DEV_CLASS_UR_I) 9052a0217d5SKay Sievers sprintf(node_id, "vmrdr-%s", dev_name(&cdev->dev)); 906810cb5b3SFrank Munzert if (urd->class == DEV_CLASS_UR_O) 9072a0217d5SKay Sievers sprintf(node_id, "vmpun-%s", dev_name(&cdev->dev)); 908810cb5b3SFrank Munzert } else if (urd->cdev->id.cu_type == PRINTER_DEVTYPE) { 9092a0217d5SKay Sievers sprintf(node_id, "vmprt-%s", dev_name(&cdev->dev)); 910810cb5b3SFrank Munzert } else { 911b9993a38SFrank Munzert rc = -EOPNOTSUPP; 912810cb5b3SFrank Munzert goto fail_free_cdev; 913810cb5b3SFrank Munzert } 914810cb5b3SFrank Munzert 915964c150eSHendrik Brueckner urd->device = device_create(vmur_class, &cdev->dev, 916964c150eSHendrik Brueckner urd->char_device->dev, NULL, "%s", node_id); 917810cb5b3SFrank Munzert if (IS_ERR(urd->device)) { 918810cb5b3SFrank Munzert rc = PTR_ERR(urd->device); 919810cb5b3SFrank Munzert TRACE("ur_set_online: device_create rc=%d\n", rc); 920810cb5b3SFrank Munzert goto fail_free_cdev; 921810cb5b3SFrank Munzert } 9228127a1f8SMichael Holzheu urdev_put(urd); 9238127a1f8SMichael Holzheu mutex_unlock(&vmur_mutex); 924810cb5b3SFrank Munzert return 0; 925810cb5b3SFrank Munzert 926810cb5b3SFrank Munzert fail_free_cdev: 927810cb5b3SFrank Munzert cdev_del(urd->char_device); 9288127a1f8SMichael Holzheu urd->char_device = NULL; 9298127a1f8SMichael Holzheu fail_urdev_put: 9308127a1f8SMichael Holzheu urdev_put(urd); 9318127a1f8SMichael Holzheu fail_unlock: 9328127a1f8SMichael Holzheu mutex_unlock(&vmur_mutex); 9338127a1f8SMichael Holzheu return rc; 9348127a1f8SMichael Holzheu } 935810cb5b3SFrank Munzert 9368127a1f8SMichael Holzheu static int ur_set_offline_force(struct ccw_device *cdev, int force) 9378127a1f8SMichael Holzheu { 9388127a1f8SMichael Holzheu struct urdev *urd; 9398127a1f8SMichael Holzheu int rc; 9408127a1f8SMichael Holzheu 9418127a1f8SMichael Holzheu TRACE("ur_set_offline: cdev=%p\n", cdev); 9428127a1f8SMichael Holzheu urd = urdev_get_from_cdev(cdev); 9438127a1f8SMichael Holzheu if (!urd) 9448127a1f8SMichael Holzheu /* ur_remove already deleted our urd */ 9458127a1f8SMichael Holzheu return -ENODEV; 9468127a1f8SMichael Holzheu if (!urd->char_device) { 9478127a1f8SMichael Holzheu /* Another ur_set_offline was faster */ 9488127a1f8SMichael Holzheu rc = -EBUSY; 9498127a1f8SMichael Holzheu goto fail_urdev_put; 9508127a1f8SMichael Holzheu } 9510462d992SElena Reshetova if (!force && (refcount_read(&urd->ref_count) > 2)) { 9528127a1f8SMichael Holzheu /* There is still a user of urd (e.g. ur_open) */ 9538127a1f8SMichael Holzheu TRACE("ur_set_offline: BUSY\n"); 9548127a1f8SMichael Holzheu rc = -EBUSY; 9558127a1f8SMichael Holzheu goto fail_urdev_put; 9568127a1f8SMichael Holzheu } 957bf18140dSPeter Oberparleiter if (cancel_work_sync(&urd->uevent_work)) { 958bf18140dSPeter Oberparleiter /* Work not run yet - need to release reference here */ 959bf18140dSPeter Oberparleiter urdev_put(urd); 960bf18140dSPeter Oberparleiter } 9618127a1f8SMichael Holzheu device_destroy(vmur_class, urd->char_device->dev); 9628127a1f8SMichael Holzheu cdev_del(urd->char_device); 9638127a1f8SMichael Holzheu urd->char_device = NULL; 9648127a1f8SMichael Holzheu rc = 0; 9658127a1f8SMichael Holzheu 9668127a1f8SMichael Holzheu fail_urdev_put: 9678127a1f8SMichael Holzheu urdev_put(urd); 968810cb5b3SFrank Munzert return rc; 969810cb5b3SFrank Munzert } 970810cb5b3SFrank Munzert 971810cb5b3SFrank Munzert static int ur_set_offline(struct ccw_device *cdev) 972810cb5b3SFrank Munzert { 9738127a1f8SMichael Holzheu int rc; 974810cb5b3SFrank Munzert 9758127a1f8SMichael Holzheu mutex_lock(&vmur_mutex); 9768127a1f8SMichael Holzheu rc = ur_set_offline_force(cdev, 0); 9778127a1f8SMichael Holzheu mutex_unlock(&vmur_mutex); 9788127a1f8SMichael Holzheu return rc; 9798127a1f8SMichael Holzheu } 980810cb5b3SFrank Munzert 9818127a1f8SMichael Holzheu static void ur_remove(struct ccw_device *cdev) 9828127a1f8SMichael Holzheu { 9838127a1f8SMichael Holzheu unsigned long flags; 9848127a1f8SMichael Holzheu 9858127a1f8SMichael Holzheu TRACE("ur_remove\n"); 9868127a1f8SMichael Holzheu 9878127a1f8SMichael Holzheu mutex_lock(&vmur_mutex); 9888127a1f8SMichael Holzheu 9898127a1f8SMichael Holzheu if (cdev->online) 9908127a1f8SMichael Holzheu ur_set_offline_force(cdev, 1); 9918127a1f8SMichael Holzheu ur_remove_attributes(&cdev->dev); 9928127a1f8SMichael Holzheu 9938127a1f8SMichael Holzheu spin_lock_irqsave(get_ccwdev_lock(cdev), flags); 994dff59b64SGreg Kroah-Hartman urdev_put(dev_get_drvdata(&cdev->dev)); 995dff59b64SGreg Kroah-Hartman dev_set_drvdata(&cdev->dev, NULL); 996f3e59ff3SPeter Oberparleiter cdev->handler = NULL; 9978127a1f8SMichael Holzheu spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); 9988127a1f8SMichael Holzheu 9998127a1f8SMichael Holzheu mutex_unlock(&vmur_mutex); 1000810cb5b3SFrank Munzert } 1001810cb5b3SFrank Munzert 1002810cb5b3SFrank Munzert /* 1003810cb5b3SFrank Munzert * Module initialisation and cleanup 1004810cb5b3SFrank Munzert */ 1005810cb5b3SFrank Munzert static int __init ur_init(void) 1006810cb5b3SFrank Munzert { 1007810cb5b3SFrank Munzert int rc; 1008810cb5b3SFrank Munzert dev_t dev; 1009810cb5b3SFrank Munzert 1010810cb5b3SFrank Munzert if (!MACHINE_IS_VM) { 101180a5c36dSFrank Munzert pr_err("The %s cannot be loaded without z/VM\n", 101280a5c36dSFrank Munzert ur_banner); 1013810cb5b3SFrank Munzert return -ENODEV; 1014810cb5b3SFrank Munzert } 1015810cb5b3SFrank Munzert 1016810cb5b3SFrank Munzert vmur_dbf = debug_register("vmur", 4, 1, 4 * sizeof(long)); 1017810cb5b3SFrank Munzert if (!vmur_dbf) 1018810cb5b3SFrank Munzert return -ENOMEM; 1019810cb5b3SFrank Munzert rc = debug_register_view(vmur_dbf, &debug_sprintf_view); 1020810cb5b3SFrank Munzert if (rc) 1021810cb5b3SFrank Munzert goto fail_free_dbf; 1022810cb5b3SFrank Munzert 1023810cb5b3SFrank Munzert debug_set_level(vmur_dbf, 6); 1024810cb5b3SFrank Munzert 1025*1aaba11dSGreg Kroah-Hartman vmur_class = class_create("vmur"); 10263f7cb51fSMichael Holzheu if (IS_ERR(vmur_class)) { 10273f7cb51fSMichael Holzheu rc = PTR_ERR(vmur_class); 10283f7cb51fSMichael Holzheu goto fail_free_dbf; 10293f7cb51fSMichael Holzheu } 10303f7cb51fSMichael Holzheu 1031810cb5b3SFrank Munzert rc = ccw_driver_register(&ur_driver); 1032810cb5b3SFrank Munzert if (rc) 10333f7cb51fSMichael Holzheu goto fail_class_destroy; 1034810cb5b3SFrank Munzert 1035810cb5b3SFrank Munzert rc = alloc_chrdev_region(&dev, 0, NUM_MINORS, "vmur"); 1036810cb5b3SFrank Munzert if (rc) { 103780a5c36dSFrank Munzert pr_err("Kernel function alloc_chrdev_region failed with " 103880a5c36dSFrank Munzert "error code %d\n", rc); 1039810cb5b3SFrank Munzert goto fail_unregister_driver; 1040810cb5b3SFrank Munzert } 1041810cb5b3SFrank Munzert ur_first_dev_maj_min = MKDEV(MAJOR(dev), 0); 1042810cb5b3SFrank Munzert 104380a5c36dSFrank Munzert pr_info("%s loaded.\n", ur_banner); 1044810cb5b3SFrank Munzert return 0; 1045810cb5b3SFrank Munzert 1046810cb5b3SFrank Munzert fail_unregister_driver: 1047810cb5b3SFrank Munzert ccw_driver_unregister(&ur_driver); 10483f7cb51fSMichael Holzheu fail_class_destroy: 10493f7cb51fSMichael Holzheu class_destroy(vmur_class); 1050810cb5b3SFrank Munzert fail_free_dbf: 1051810cb5b3SFrank Munzert debug_unregister(vmur_dbf); 1052810cb5b3SFrank Munzert return rc; 1053810cb5b3SFrank Munzert } 1054810cb5b3SFrank Munzert 1055810cb5b3SFrank Munzert static void __exit ur_exit(void) 1056810cb5b3SFrank Munzert { 1057810cb5b3SFrank Munzert unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS); 1058810cb5b3SFrank Munzert ccw_driver_unregister(&ur_driver); 10593f7cb51fSMichael Holzheu class_destroy(vmur_class); 1060810cb5b3SFrank Munzert debug_unregister(vmur_dbf); 106180a5c36dSFrank Munzert pr_info("%s unloaded.\n", ur_banner); 1062810cb5b3SFrank Munzert } 1063810cb5b3SFrank Munzert 1064810cb5b3SFrank Munzert module_init(ur_init); 1065810cb5b3SFrank Munzert module_exit(ur_exit); 1066