// SPDX-License-Identifier: GPL-2.0 /* * Linux driver for System z and s390 unit record devices * (z/VM virtual punch, reader, printer) * * Copyright IBM Corp. 2001, 2009 * Authors: Malcolm Beattie <beattiem@uk.ibm.com> * Michael Holzheu <holzheu@de.ibm.com> * Frank Munzert <munzert@de.ibm.com>
*/
/* * 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.
*/
staticchar 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");
/* We put the device's record length (for writes) in the driver_info field */ staticstruct ccw_device_id ur_ids[] = {
{ CCWDEV_CU_DI(READER_PUNCH_DEVTYPE, 80) },
{ CCWDEV_CU_DI(PRINTER_DEVTYPE, 132) },
{ /* end of list */ }
};
/* * 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 dev_get_drvdata(&cdev->dev) pointer. * * urd references: * - ur_probe gets a urd reference, ur_remove drops the reference * dev_get_drvdata(&cdev->dev) * - ur_open gets a urd reference, ur_release 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 dev_get_drvdata(&cdev->dev) is protected by the ccwdev lock
*/ staticstruct urdev *urdev_alloc(struct ccw_device *cdev)
{ struct urdev *urd;
staticvoid urdev_put(struct urdev *urd)
{ if (refcount_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.
*/
while (ptr->cda) {
kfree(dma32_to_virt(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.
*/ staticstruct ccw1 *alloc_chan_prog(constchar __user *ubuf, int rec_count, int reclen)
{ struct ccw1 *cpa; void *kbuf; int i;
/* * 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 = kcalloc(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 = virt_to_dma32(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;
}
if (scsw_dstat(&irb->scsw) & DEV_STAT_DEV_END) { /* * Userspace might be interested in a transition to * device-ready state.
*/
urdev_get(urd);
schedule_work(&urd->uevent_work);
}
return;
} /* On special conditions irb is an error pointer */ if (IS_ERR(irb))
urd->io_request_rc = PTR_ERR(irb); elseif (irb->scsw.cmd.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;
/* * 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
*/ staticint get_urd_class(struct urdev *urd)
{ staticstruct diag210 ur_diag210; int cc;
/* * 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
*/ staticint 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
*/ staticint diag_read_file(int devno, char *buf)
{ int cc;
cc = diag14((unsignedlong) buf, devno, 0x00); switch (cc) { case 0: return 0; case 1: return -ENODATA; case 2: return -ENOMEDIUM; default: return -EIO;
}
}
/* * diagnose code 0x14 subcode 0x0fff - retrieve next file descriptor * cc=0 normal completion * cc=1 no files on reader queue or no subsequent file * cc=2 spid specified is invalid
*/ staticint diag_read_next_file_info(struct file_control_block *buf, int spid)
{ int cc;
cc = diag14((unsignedlong) buf, spid, 0xfff); switch (cc) { case 0: return 0; default: return -ENODATA;
}
}
fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA); if (!fcb) return -ENOMEM;
/* check for empty reader device (beginning of chain) */
rc = diag_read_next_file_info(fcb, 0); if (rc) goto fail_free_fcb;
/* if file is in hold status, we do not read it */ if (fcb->file_stat & (FLG_SYSTEM_HOLD | FLG_USER_HOLD)) {
rc = -EPERM; goto fail_free_fcb;
}
/* open file on virtual reader */
buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA); if (!buf) {
rc = -ENOMEM; goto fail_free_fcb;
}
rc = diag_read_file(urd->dev_id.devno, buf); if ((rc != 0) && (rc != -ENODATA)) /* EOF does not hurt */ goto fail_free_buf;
/* check if the file on top of the queue is open now */
rc = diag_read_next_file_info(fcb, 0); if (rc) goto fail_free_buf; if (!(fcb->file_stat & FLG_IN_USE)) {
rc = -EMFILE; goto fail_free_buf;
}
rc = 0;
if (accmode == O_RDWR) return -EACCES; /* * We treat the minor number as the devno of the ur device * to find in the driver tree.
*/
devno = iminor(file_inode(file));
/* * ccw_device infrastructure: * ur_probe creates the struct urdev (with refcount = 1), the device * attributes, sets up the interrupt handler and validates the virtual * unit record device. * ur_remove removes the device attributes and drops the reference to * struct urdev. * * ur_probe, ur_remove, ur_set_online and ur_set_offline are serialized * by the vmur_mutex lock. * * urd->char_device is used as indication that the online function has * been completed successfully.
*/ staticint ur_probe(struct ccw_device *cdev)
{ struct urdev *urd; int rc;
staticint ur_set_offline_force(struct ccw_device *cdev, int force)
{ struct urdev *urd; int rc;
TRACE("ur_set_offline: cdev=%p\n", cdev);
urd = urdev_get_from_cdev(cdev); if (!urd) /* ur_remove already deleted our urd */ return -ENODEV; if (!urd->char_device) { /* Another ur_set_offline was faster */
rc = -EBUSY; goto fail_urdev_put;
} if (!force && (refcount_read(&urd->ref_count) > 2)) { /* There is still a user of urd (e.g. ur_open) */
TRACE("ur_set_offline: BUSY\n");
rc = -EBUSY; goto fail_urdev_put;
} if (cancel_work_sync(&urd->uevent_work)) { /* Work not run yet - need to release reference here */
urdev_put(urd);
}
device_destroy(&vmur_class, urd->char_device->dev);
cdev_del(urd->char_device);
urd->char_device = NULL;
rc = 0;
fail_urdev_put:
urdev_put(urd); return rc;
}
staticint ur_set_offline(struct ccw_device *cdev)
{ int rc;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.