📄 vmur.c
字号:
/* * Linux driver for System z and s390 unit record devices * (z/VM virtual punch, reader, printer) * * Copyright IBM Corp. 2001, 2007 * Authors: Malcolm Beattie <beattiem@uk.ibm.com> * Michael Holzheu <holzheu@de.ibm.com> * Frank Munzert <munzert@de.ibm.com> */#include <linux/cdev.h>#include <asm/uaccess.h>#include <asm/cio.h>#include <asm/ccwdev.h>#include <asm/debug.h>#include <asm/diag.h>#include "vmur.h"/* * Driver overview * * Unit record device support is implemented as a character device driver. * We can fit at least 16 bits into a device minor number and use the * simple method of mapping a character device number with minor abcd * to the unit record device with devno abcd. * I/O to virtual unit record devices is handled as follows: * Reads: Diagnose code 0x14 (input spool file manipulation) * is used to read spool data page-wise. * Writes: The CCW used is WRITE_CCW_CMD (0x01). The device's record length * is available by reading sysfs attr reclen. Each write() to the device * must specify an integral multiple (maximal 511) of reclen. */static char ur_banner[] = "z/VM virtual unit record device driver";MODULE_AUTHOR("IBM Corporation");MODULE_DESCRIPTION("s390 z/VM virtual unit record device driver");MODULE_LICENSE("GPL");#define PRINTK_HEADER "vmur: "static dev_t ur_first_dev_maj_min;static struct class *vmur_class;static struct debug_info *vmur_dbf;/* We put the device's record length (for writes) in the driver_info field */static struct ccw_device_id ur_ids[] = { { CCWDEV_CU_DI(READER_PUNCH_DEVTYPE, 80) }, { CCWDEV_CU_DI(PRINTER_DEVTYPE, 132) }, { /* end of list */ }};MODULE_DEVICE_TABLE(ccw, ur_ids);static int ur_probe(struct ccw_device *cdev);static void ur_remove(struct ccw_device *cdev);static int ur_set_online(struct ccw_device *cdev);static int ur_set_offline(struct ccw_device *cdev);static struct ccw_driver ur_driver = { .name = "vmur", .owner = THIS_MODULE, .ids = ur_ids, .probe = ur_probe, .remove = ur_remove, .set_online = ur_set_online, .set_offline = ur_set_offline,};static DEFINE_MUTEX(vmur_mutex);/* * Allocation, freeing, getting and putting of urdev structures * * Each ur device (urd) contains a reference to its corresponding ccw device * (cdev) using the urd->cdev pointer. Each ccw device has a reference to the * ur device using the cdev->dev.driver_data pointer. * * urd references: * - ur_probe gets a urd reference, ur_remove drops the reference * (cdev->dev.driver_data) * - ur_open gets a urd reference, ur_relase drops the reference * (urf->urd) * * cdev references: * - urdev_alloc get a cdev reference (urd->cdev) * - urdev_free drops the cdev reference (urd->cdev) * * Setting and clearing of cdev->dev.driver_data is protected by the ccwdev lock */static struct urdev *urdev_alloc(struct ccw_device *cdev){ struct urdev *urd; urd = kzalloc(sizeof(struct urdev), GFP_KERNEL); if (!urd) return NULL; urd->reclen = cdev->id.driver_info; ccw_device_get_id(cdev, &urd->dev_id); mutex_init(&urd->io_mutex); mutex_init(&urd->open_mutex); atomic_set(&urd->ref_count, 1); urd->cdev = cdev; get_device(&cdev->dev); return urd;}static void urdev_free(struct urdev *urd){ TRACE("urdev_free: %p\n", urd); if (urd->cdev) put_device(&urd->cdev->dev); kfree(urd);}static void urdev_get(struct urdev *urd){ atomic_inc(&urd->ref_count);}static struct urdev *urdev_get_from_cdev(struct ccw_device *cdev){ struct urdev *urd; unsigned long flags; spin_lock_irqsave(get_ccwdev_lock(cdev), flags); urd = cdev->dev.driver_data; if (urd) urdev_get(urd); spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); return urd;}static struct urdev *urdev_get_from_devno(u16 devno){ char bus_id[16]; struct ccw_device *cdev; struct urdev *urd; sprintf(bus_id, "0.0.%04x", devno); cdev = get_ccwdev_by_busid(&ur_driver, bus_id); if (!cdev) return NULL; urd = urdev_get_from_cdev(cdev); put_device(&cdev->dev); return urd;}static void urdev_put(struct urdev *urd){ if (atomic_dec_and_test(&urd->ref_count)) urdev_free(urd);}/* * Low-level functions to do I/O to a ur device. * alloc_chan_prog * free_chan_prog * do_ur_io * ur_int_handler * * alloc_chan_prog allocates and builds the channel program * free_chan_prog frees memory of the channel program * * do_ur_io issues the channel program to the device and blocks waiting * on a completion event it publishes at urd->io_done. The function * serialises itself on the device's mutex so that only one I/O * is issued at a time (and that I/O is synchronous). * * ur_int_handler catches the "I/O done" interrupt, writes the * subchannel status word into the scsw member of the urdev structure * and complete()s the io_done to wake the waiting do_ur_io. * * The caller of do_ur_io is responsible for kfree()ing the channel program * address pointer that alloc_chan_prog returned. */static void free_chan_prog(struct ccw1 *cpa){ struct ccw1 *ptr = cpa; while (ptr->cda) { kfree((void *)(addr_t) ptr->cda); ptr++; } kfree(cpa);}/* * alloc_chan_prog * The channel program we use is write commands chained together * with a final NOP CCW command-chained on (which ensures that CE and DE * are presented together in a single interrupt instead of as separate * interrupts unless an incorrect length indication kicks in first). The * data length in each CCW is reclen. */static struct ccw1 *alloc_chan_prog(const char __user *ubuf, int rec_count, int reclen){ struct ccw1 *cpa; void *kbuf; int i; TRACE("alloc_chan_prog(%p, %i, %i)\n", ubuf, rec_count, reclen); /* * We chain a NOP onto the writes to force CE+DE together. * That means we allocate room for CCWs to cover count/reclen * records plus a NOP. */ cpa = kzalloc((rec_count + 1) * sizeof(struct ccw1), GFP_KERNEL | GFP_DMA); if (!cpa) return ERR_PTR(-ENOMEM); for (i = 0; i < rec_count; i++) { cpa[i].cmd_code = WRITE_CCW_CMD; cpa[i].flags = CCW_FLAG_CC | CCW_FLAG_SLI; cpa[i].count = reclen; kbuf = kmalloc(reclen, GFP_KERNEL | GFP_DMA); if (!kbuf) { free_chan_prog(cpa); return ERR_PTR(-ENOMEM); } cpa[i].cda = (u32)(addr_t) kbuf; if (copy_from_user(kbuf, ubuf, reclen)) { free_chan_prog(cpa); return ERR_PTR(-EFAULT); } ubuf += reclen; } /* The following NOP CCW forces CE+DE to be presented together */ cpa[i].cmd_code = CCW_CMD_NOOP; return cpa;}static int do_ur_io(struct urdev *urd, struct ccw1 *cpa){ int rc; struct ccw_device *cdev = urd->cdev; DECLARE_COMPLETION_ONSTACK(event); TRACE("do_ur_io: cpa=%p\n", cpa); rc = mutex_lock_interruptible(&urd->io_mutex); if (rc) return rc; urd->io_done = &event; spin_lock_irq(get_ccwdev_lock(cdev)); rc = ccw_device_start(cdev, cpa, 1, 0, 0); spin_unlock_irq(get_ccwdev_lock(cdev)); TRACE("do_ur_io: ccw_device_start returned %d\n", rc); if (rc) goto out; wait_for_completion(&event); TRACE("do_ur_io: I/O complete\n"); rc = 0;out: mutex_unlock(&urd->io_mutex); return rc;}/* * ur interrupt handler, called from the ccw_device layer */static void ur_int_handler(struct ccw_device *cdev, unsigned long intparm, struct irb *irb){ struct urdev *urd; TRACE("ur_int_handler: intparm=0x%lx cstat=%02x dstat=%02x res=%u\n", intparm, irb->scsw.cstat, irb->scsw.dstat, irb->scsw.count); if (!intparm) { TRACE("ur_int_handler: unsolicited interrupt\n"); return; } urd = cdev->dev.driver_data; BUG_ON(!urd); /* On special conditions irb is an error pointer */ if (IS_ERR(irb)) urd->io_request_rc = PTR_ERR(irb); else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END)) urd->io_request_rc = 0; else urd->io_request_rc = -EIO; complete(urd->io_done);}/* * reclen sysfs attribute - The record length to be used for write CCWs */static ssize_t ur_attr_reclen_show(struct device *dev, struct device_attribute *attr, char *buf){ struct urdev *urd; int rc; urd = urdev_get_from_cdev(to_ccwdev(dev)); if (!urd) return -ENODEV; rc = sprintf(buf, "%zu\n", urd->reclen); urdev_put(urd); return rc;}static DEVICE_ATTR(reclen, 0444, ur_attr_reclen_show, NULL);static int ur_create_attributes(struct device *dev){ return device_create_file(dev, &dev_attr_reclen);}static void ur_remove_attributes(struct device *dev){ device_remove_file(dev, &dev_attr_reclen);}/* * diagnose code 0x210 - retrieve device information * cc=0 normal completion, we have a real device * cc=1 CP paging error * cc=2 The virtual device exists, but is not associated with a real device * cc=3 Invalid device address, or the virtual device does not exist */static int get_urd_class(struct urdev *urd){ static struct diag210 ur_diag210; int cc; ur_diag210.vrdcdvno = urd->dev_id.devno; ur_diag210.vrdclen = sizeof(struct diag210); cc = diag210(&ur_diag210); switch (cc) { case 0: return -ENOTSUPP; case 2: return ur_diag210.vrdcvcla; /* virtual device class */ case 3: return -ENODEV; default: return -EIO; }}/* * Allocation and freeing of urfile structures */static struct urfile *urfile_alloc(struct urdev *urd){ struct urfile *urf; urf = kzalloc(sizeof(struct urfile), GFP_KERNEL); if (!urf) return NULL; urf->urd = urd; TRACE("urfile_alloc: urd=%p urf=%p rl=%zu\n", urd, urf, urf->dev_reclen); return urf;}static void urfile_free(struct urfile *urf){ TRACE("urfile_free: urf=%p urd=%p\n", urf, urf->urd); kfree(urf);}/* * The fops implementation of the character device driver */static ssize_t do_write(struct urdev *urd, const char __user *udata, size_t count, size_t reclen, loff_t *ppos){ struct ccw1 *cpa; int rc; cpa = alloc_chan_prog(udata, count / reclen, reclen); if (IS_ERR(cpa)) return PTR_ERR(cpa); rc = do_ur_io(urd, cpa); if (rc) goto fail_kfree_cpa; if (urd->io_request_rc) { rc = urd->io_request_rc; goto fail_kfree_cpa; } *ppos += count; rc = count;fail_kfree_cpa: free_chan_prog(cpa); return rc;}static ssize_t ur_write(struct file *file, const char __user *udata, size_t count, loff_t *ppos){ struct urfile *urf = file->private_data; TRACE("ur_write: count=%zu\n", count); if (count == 0) return 0; if (count % urf->dev_reclen) return -EINVAL; /* count must be a multiple of reclen */ if (count > urf->dev_reclen * MAX_RECS_PER_IO) count = urf->dev_reclen * MAX_RECS_PER_IO; return do_write(urf->urd, udata, count, urf->dev_reclen, ppos);}/* * diagnose code 0x14 subcode 0x0028 - position spool file to designated * record * cc=0 normal completion * cc=2 no file active on the virtual reader or device not ready * cc=3 record specified is beyond EOF */static int diag_position_to_record(int devno, int record){ int cc; cc = diag14(record, devno, 0x28); switch (cc) { case 0: return 0; case 2: return -ENOMEDIUM; case 3: return -ENODATA; /* position beyond end of file */ default: return -EIO; }}/* * diagnose code 0x14 subcode 0x0000 - read next spool file buffer * cc=0 normal completion * cc=1 EOF reached * cc=2 no file active on the virtual reader, and no file eligible * cc=3 file already active on the virtual reader or specified virtual * reader does not exist or is not a reader */static int diag_read_file(int devno, char *buf){ int cc; cc = diag14((unsigned long) buf, devno, 0x00); switch (cc) { case 0: return 0; case 1: return -ENODATA; case 2: return -ENOMEDIUM; default: return -EIO; }}static ssize_t diag14_read(struct file *file, char __user *ubuf, size_t count, loff_t *offs){ size_t len, copied, res; char *buf; int rc; u16 reclen; struct urdev *urd; urd = ((struct urfile *) file->private_data)->urd; reclen = ((struct urfile *) file->private_data)->file_reclen; rc = diag_position_to_record(urd->dev_id.devno, *offs / PAGE_SIZE + 1); if (rc == -ENODATA) return 0; if (rc) return rc; len = min((size_t) PAGE_SIZE, count); buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA); if (!buf) return -ENOMEM; copied = 0; res = (size_t) (*offs % PAGE_SIZE); do { rc = diag_read_file(urd->dev_id.devno, buf); if (rc == -ENODATA) { break; } if (rc) goto fail; if (reclen && (copied == 0) && (*offs < PAGE_SIZE)) *((u16 *) &buf[FILE_RECLEN_OFFSET]) = reclen; len = min(count - copied, PAGE_SIZE - res); if (copy_to_user(ubuf + copied, buf + res, len)) { rc = -EFAULT; goto fail;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -