2 * Linux driver for System z and s390 unit record devices
3 * (z/VM virtual punch, reader, printer)
5 * Copyright IBM Corp. 2001, 2007
6 * Authors: Malcolm Beattie <beattiem@uk.ibm.com>
7 * Michael Holzheu <holzheu@de.ibm.com>
8 * Frank Munzert <munzert@de.ibm.com>
11 #include <linux/cdev.h>
13 #include <asm/uaccess.h>
15 #include <asm/ccwdev.h>
16 #include <asm/debug.h>
23 * Unit record device support is implemented as a character device driver.
24 * We can fit at least 16 bits into a device minor number and use the
25 * simple method of mapping a character device number with minor abcd
26 * to the unit record device with devno abcd.
27 * I/O to virtual unit record devices is handled as follows:
28 * Reads: Diagnose code 0x14 (input spool file manipulation)
29 * is used to read spool data page-wise.
30 * Writes: The CCW used is WRITE_CCW_CMD (0x01). The device's record length
31 * is available by reading sysfs attr reclen. Each write() to the device
32 * must specify an integral multiple (maximal 511) of reclen.
35 static char ur_banner[] = "z/VM virtual unit record device driver";
37 MODULE_AUTHOR("IBM Corporation");
38 MODULE_DESCRIPTION("s390 z/VM virtual unit record device driver");
39 MODULE_LICENSE("GPL");
41 #define PRINTK_HEADER "vmur: "
43 static dev_t ur_first_dev_maj_min;
44 static struct class *vmur_class;
45 static struct debug_info *vmur_dbf;
47 /* We put the device's record length (for writes) in the driver_info field */
48 static struct ccw_device_id ur_ids[] = {
49 { CCWDEV_CU_DI(READER_PUNCH_DEVTYPE, 80) },
50 { CCWDEV_CU_DI(PRINTER_DEVTYPE, 132) },
54 MODULE_DEVICE_TABLE(ccw, ur_ids);
56 static int ur_probe(struct ccw_device *cdev);
57 static void ur_remove(struct ccw_device *cdev);
58 static int ur_set_online(struct ccw_device *cdev);
59 static int ur_set_offline(struct ccw_device *cdev);
61 static struct ccw_driver ur_driver = {
67 .set_online = ur_set_online,
68 .set_offline = ur_set_offline,
72 * Allocation, freeing, getting and putting of urdev structures
74 static struct urdev *urdev_alloc(struct ccw_device *cdev)
78 urd = kzalloc(sizeof(struct urdev), GFP_KERNEL);
82 urd->reclen = cdev->id.driver_info;
83 ccw_device_get_id(cdev, &urd->dev_id);
84 mutex_init(&urd->io_mutex);
85 mutex_init(&urd->open_mutex);
89 static void urdev_free(struct urdev *urd)
95 * This is how the character device driver gets a reference to a
96 * ur device. When this call returns successfully, a reference has
97 * been taken (by get_device) on the underlying kobject. The recipient
98 * of this urdev pointer must eventually drop it with urdev_put(urd)
99 * which does the corresponding put_device().
101 static struct urdev *urdev_get_from_devno(u16 devno)
104 struct ccw_device *cdev;
106 sprintf(bus_id, "0.0.%04x", devno);
107 cdev = get_ccwdev_by_busid(&ur_driver, bus_id);
111 return cdev->dev.driver_data;
114 static void urdev_put(struct urdev *urd)
116 put_device(&urd->cdev->dev);
120 * Low-level functions to do I/O to a ur device.
125 * alloc_chan_prog allocates and builds the channel program
127 * do_ur_io issues the channel program to the device and blocks waiting
128 * on a completion event it publishes at urd->io_done. The function
129 * serialises itself on the device's mutex so that only one I/O
130 * is issued at a time (and that I/O is synchronous).
132 * ur_int_handler catches the "I/O done" interrupt, writes the
133 * subchannel status word into the scsw member of the urdev structure
134 * and complete()s the io_done to wake the waiting do_ur_io.
136 * The caller of do_ur_io is responsible for kfree()ing the channel program
137 * address pointer that alloc_chan_prog returned.
143 * The channel program we use is write commands chained together
144 * with a final NOP CCW command-chained on (which ensures that CE and DE
145 * are presented together in a single interrupt instead of as separate
146 * interrupts unless an incorrect length indication kicks in first). The
147 * data length in each CCW is reclen. The caller must ensure that count
148 * is an integral multiple of reclen.
149 * The channel program pointer returned by this function must be freed
150 * with kfree. The caller is responsible for checking that
151 * count/reclen is not ridiculously large.
153 static struct ccw1 *alloc_chan_prog(char *buf, size_t count, size_t reclen)
159 TRACE("alloc_chan_prog(%p, %zu, %zu)\n", buf, count, reclen);
162 * We chain a NOP onto the writes to force CE+DE together.
163 * That means we allocate room for CCWs to cover count/reclen
164 * records plus a NOP.
166 num_ccws = count / reclen + 1;
167 cpa = kmalloc(num_ccws * sizeof(struct ccw1), GFP_KERNEL | GFP_DMA);
171 for (i = 0; count; i++) {
172 cpa[i].cmd_code = WRITE_CCW_CMD;
173 cpa[i].flags = CCW_FLAG_CC | CCW_FLAG_SLI;
174 cpa[i].count = reclen;
175 cpa[i].cda = __pa(buf);
179 /* The following NOP CCW forces CE+DE to be presented together */
180 cpa[i].cmd_code = CCW_CMD_NOOP;
188 static int do_ur_io(struct urdev *urd, struct ccw1 *cpa)
191 struct ccw_device *cdev = urd->cdev;
192 DECLARE_COMPLETION(event);
194 TRACE("do_ur_io: cpa=%p\n", cpa);
196 rc = mutex_lock_interruptible(&urd->io_mutex);
200 urd->io_done = &event;
202 spin_lock_irq(get_ccwdev_lock(cdev));
203 rc = ccw_device_start(cdev, cpa, 1, 0, 0);
204 spin_unlock_irq(get_ccwdev_lock(cdev));
206 TRACE("do_ur_io: ccw_device_start returned %d\n", rc);
210 wait_for_completion(&event);
211 TRACE("do_ur_io: I/O complete\n");
215 mutex_unlock(&urd->io_mutex);
220 * ur interrupt handler, called from the ccw_device layer
222 static void ur_int_handler(struct ccw_device *cdev, unsigned long intparm,
227 TRACE("ur_int_handler: intparm=0x%lx cstat=%02x dstat=%02x res=%u\n",
228 intparm, irb->scsw.cstat, irb->scsw.dstat, irb->scsw.count);
231 TRACE("ur_int_handler: unsolicited interrupt\n");
234 urd = cdev->dev.driver_data;
235 /* On special conditions irb is an error pointer */
237 urd->io_request_rc = PTR_ERR(irb);
238 else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END))
239 urd->io_request_rc = 0;
241 urd->io_request_rc = -EIO;
243 complete(urd->io_done);
247 * reclen sysfs attribute - The record length to be used for write CCWs
249 static ssize_t ur_attr_reclen_show(struct device *dev,
250 struct device_attribute *attr, char *buf)
252 struct urdev *urd = dev->driver_data;
254 return sprintf(buf, "%zu\n", urd->reclen);
257 static DEVICE_ATTR(reclen, 0444, ur_attr_reclen_show, NULL);
259 static int ur_create_attributes(struct device *dev)
261 return device_create_file(dev, &dev_attr_reclen);
264 static void ur_remove_attributes(struct device *dev)
266 device_remove_file(dev, &dev_attr_reclen);
270 * diagnose code 0x210 - retrieve device information
271 * cc=0 normal completion, we have a real device
272 * cc=1 CP paging error
273 * cc=2 The virtual device exists, but is not associated with a real device
274 * cc=3 Invalid device address, or the virtual device does not exist
276 static int get_urd_class(struct urdev *urd)
278 static struct diag210 ur_diag210;
281 ur_diag210.vrdcdvno = urd->dev_id.devno;
282 ur_diag210.vrdclen = sizeof(struct diag210);
284 cc = diag210(&ur_diag210);
289 return ur_diag210.vrdcvcla; /* virtual device class */
298 * Allocation and freeing of urfile structures
300 static struct urfile *urfile_alloc(struct urdev *urd)
304 urf = kzalloc(sizeof(struct urfile), GFP_KERNEL);
309 TRACE("urfile_alloc: urd=%p urf=%p rl=%zu\n", urd, urf,
315 static void urfile_free(struct urfile *urf)
317 TRACE("urfile_free: urf=%p urd=%p\n", urf, urf->urd);
322 * The fops implementation of the character device driver
324 static ssize_t do_write(struct urdev *urd, const char __user *udata,
325 size_t count, size_t reclen, loff_t *ppos)
331 /* Data buffer must be under 2GB line for fmt1 CCWs: hence GFP_DMA */
332 buf = kmalloc(count, GFP_KERNEL | GFP_DMA);
336 if (copy_from_user(buf, udata, count)) {
341 cpa = alloc_chan_prog(buf, count, reclen);
347 rc = do_ur_io(urd, cpa);
351 if (urd->io_request_rc) {
352 rc = urd->io_request_rc;
364 static ssize_t ur_write(struct file *file, const char __user *udata,
365 size_t count, loff_t *ppos)
367 struct urfile *urf = file->private_data;
369 TRACE("ur_write: count=%zu\n", count);
374 if (count % urf->dev_reclen)
375 return -EINVAL; /* count must be a multiple of reclen */
377 if (count > urf->dev_reclen * MAX_RECS_PER_IO)
378 count = urf->dev_reclen * MAX_RECS_PER_IO;
380 return do_write(urf->urd, udata, count, urf->dev_reclen, ppos);
383 static int do_diag_14(unsigned long rx, unsigned long ry1,
384 unsigned long subcode)
386 register unsigned long _ry1 asm("2") = ry1;
387 register unsigned long _ry2 asm("3") = subcode;
400 : "=d" (rc), "+d" (_ry2)
401 : "d" (rx), "d" (_ry1)
404 TRACE("diag 14: subcode=0x%lx, cc=%i\n", subcode, rc);
409 * diagnose code 0x14 subcode 0x0028 - position spool file to designated
411 * cc=0 normal completion
412 * cc=2 no file active on the virtual reader or device not ready
413 * cc=3 record specified is beyond EOF
415 static int diag_position_to_record(int devno, int record)
419 cc = do_diag_14(record, devno, 0x28);
426 return -ENODATA; /* position beyond end of file */
433 * diagnose code 0x14 subcode 0x0000 - read next spool file buffer
434 * cc=0 normal completion
436 * cc=2 no file active on the virtual reader, and no file eligible
437 * cc=3 file already active on the virtual reader or specified virtual
438 * reader does not exist or is not a reader
440 static int diag_read_file(int devno, char *buf)
444 cc = do_diag_14((unsigned long) buf, devno, 0x00);
457 static ssize_t diag14_read(struct file *file, char __user *ubuf, size_t count,
460 size_t len, copied, res;
466 urd = ((struct urfile *) file->private_data)->urd;
467 reclen = ((struct urfile *) file->private_data)->file_reclen;
469 rc = diag_position_to_record(urd->dev_id.devno, *offs / PAGE_SIZE + 1);
475 len = min((size_t) PAGE_SIZE, count);
476 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
481 res = (size_t) (*offs % PAGE_SIZE);
483 rc = diag_read_file(urd->dev_id.devno, buf);
484 if (rc == -ENODATA) {
490 *((u16 *) &buf[FILE_RECLEN_OFFSET]) = reclen;
491 len = min(count - copied, PAGE_SIZE - res);
492 if (copy_to_user(ubuf + copied, buf + res, len)) {
498 } while (copied != count);
507 static ssize_t ur_read(struct file *file, char __user *ubuf, size_t count,
513 TRACE("ur_read: count=%zu ppos=%li\n", count, (unsigned long) *offs);
518 urd = ((struct urfile *) file->private_data)->urd;
519 rc = mutex_lock_interruptible(&urd->io_mutex);
522 rc = diag14_read(file, ubuf, count, offs);
523 mutex_unlock(&urd->io_mutex);
528 * diagnose code 0x14 subcode 0x0fff - retrieve next file descriptor
529 * cc=0 normal completion
530 * cc=1 no files on reader queue or no subsequent file
531 * cc=2 spid specified is invalid
533 static int diag_read_next_file_info(struct file_control_block *buf, int spid)
537 cc = do_diag_14((unsigned long) buf, spid, 0xfff);
546 static int verify_device(struct urdev *urd)
548 struct file_control_block fcb;
552 switch (urd->class) {
554 return 0; /* no check needed here */
556 /* check for empty reader device (beginning of chain) */
557 rc = diag_read_next_file_info(&fcb, 0);
561 /* open file on virtual reader */
562 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
565 rc = diag_read_file(urd->dev_id.devno, buf);
568 if ((rc != 0) && (rc != -ENODATA)) /* EOF does not hurt */
576 static int get_file_reclen(struct urdev *urd)
578 struct file_control_block fcb;
581 switch (urd->class) {
585 rc = diag_read_next_file_info(&fcb, 0);
592 if (fcb.file_stat & FLG_CP_DUMP)
598 static int ur_open(struct inode *inode, struct file *file)
603 unsigned short accmode;
606 accmode = file->f_flags & O_ACCMODE;
608 if (accmode == O_RDWR)
612 * We treat the minor number as the devno of the ur device
613 * to find in the driver tree.
615 devno = MINOR(file->f_dentry->d_inode->i_rdev);
617 urd = urdev_get_from_devno(devno);
621 if (file->f_flags & O_NONBLOCK) {
622 if (!mutex_trylock(&urd->open_mutex)) {
627 if (mutex_lock_interruptible(&urd->open_mutex)) {
635 if (((accmode == O_RDONLY) && (urd->class != DEV_CLASS_UR_I)) ||
636 ((accmode == O_WRONLY) && (urd->class != DEV_CLASS_UR_O))) {
637 TRACE("ur_open: unsupported dev class (%d)\n", urd->class);
642 rc = verify_device(urd);
646 urf = urfile_alloc(urd);
652 urf->dev_reclen = urd->reclen;
653 rc = get_file_reclen(urd);
655 goto fail_urfile_free;
656 urf->file_reclen = rc;
657 file->private_data = urf;
663 mutex_unlock(&urd->open_mutex);
669 static int ur_release(struct inode *inode, struct file *file)
671 struct urfile *urf = file->private_data;
673 TRACE("ur_release\n");
674 mutex_unlock(&urf->urd->open_mutex);
680 static loff_t ur_llseek(struct file *file, loff_t offset, int whence)
684 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
685 return -ESPIPE; /* seek allowed only for reader */
686 if (offset % PAGE_SIZE)
687 return -ESPIPE; /* only multiples of 4K allowed */
689 case 0: /* SEEK_SET */
692 case 1: /* SEEK_CUR */
693 newpos = file->f_pos + offset;
698 file->f_pos = newpos;
702 static struct file_operations ur_fops = {
703 .owner = THIS_MODULE,
705 .release = ur_release,
712 * ccw_device infrastructure:
713 * ur_probe gets its own ref to the device (i.e. get_device),
714 * creates the struct urdev, the device attributes, sets up
715 * the interrupt handler and validates the virtual unit record device.
716 * ur_remove removes the device attributes, frees the struct urdev
717 * and drops (put_device) the ref to the device we got in ur_probe.
719 static int ur_probe(struct ccw_device *cdev)
724 TRACE("ur_probe: cdev=%p state=%d\n", cdev, *(int *) cdev->private);
726 if (!get_device(&cdev->dev))
729 urd = urdev_alloc(cdev);
734 rc = ur_create_attributes(&cdev->dev);
739 cdev->dev.driver_data = urd;
740 cdev->handler = ur_int_handler;
742 /* validate virtual unit record device */
743 urd->class = get_urd_class(urd);
744 if (urd->class < 0) {
748 if ((urd->class != DEV_CLASS_UR_I) && (urd->class != DEV_CLASS_UR_O)) {
757 put_device(&cdev->dev);
761 static void ur_remove(struct ccw_device *cdev)
763 struct urdev *urd = cdev->dev.driver_data;
765 TRACE("ur_remove\n");
767 ur_set_offline(cdev);
768 ur_remove_attributes(&cdev->dev);
770 put_device(&cdev->dev);
773 static int ur_set_online(struct ccw_device *cdev)
776 int minor, major, rc;
779 TRACE("ur_set_online: cdev=%p state=%d\n", cdev,
780 *(int *) cdev->private);
782 if (!try_module_get(ur_driver.owner))
785 urd = (struct urdev *) cdev->dev.driver_data;
786 minor = urd->dev_id.devno;
787 major = MAJOR(ur_first_dev_maj_min);
789 urd->char_device = cdev_alloc();
790 if (!urd->char_device) {
792 goto fail_module_put;
795 cdev_init(urd->char_device, &ur_fops);
796 urd->char_device->dev = MKDEV(major, minor);
797 urd->char_device->owner = ur_fops.owner;
799 rc = cdev_add(urd->char_device, urd->char_device->dev, 1);
802 if (urd->cdev->id.cu_type == READER_PUNCH_DEVTYPE) {
803 if (urd->class == DEV_CLASS_UR_I)
804 sprintf(node_id, "vmrdr-%s", cdev->dev.bus_id);
805 if (urd->class == DEV_CLASS_UR_O)
806 sprintf(node_id, "vmpun-%s", cdev->dev.bus_id);
807 } else if (urd->cdev->id.cu_type == PRINTER_DEVTYPE) {
808 sprintf(node_id, "vmprt-%s", cdev->dev.bus_id);
814 urd->device = device_create(vmur_class, NULL, urd->char_device->dev,
816 if (IS_ERR(urd->device)) {
817 rc = PTR_ERR(urd->device);
818 TRACE("ur_set_online: device_create rc=%d\n", rc);
825 cdev_del(urd->char_device);
827 module_put(ur_driver.owner);
832 static int ur_set_offline(struct ccw_device *cdev)
836 TRACE("ur_set_offline: cdev=%p cdev->private=%p state=%d\n",
837 cdev, cdev->private, *(int *) cdev->private);
838 urd = (struct urdev *) cdev->dev.driver_data;
839 device_destroy(vmur_class, urd->char_device->dev);
840 cdev_del(urd->char_device);
841 module_put(ur_driver.owner);
847 * Module initialisation and cleanup
849 static int __init ur_init(void)
854 if (!MACHINE_IS_VM) {
855 PRINT_ERR("%s is only available under z/VM.\n", ur_banner);
859 vmur_dbf = debug_register("vmur", 4, 1, 4 * sizeof(long));
862 rc = debug_register_view(vmur_dbf, &debug_sprintf_view);
866 debug_set_level(vmur_dbf, 6);
868 rc = ccw_driver_register(&ur_driver);
872 rc = alloc_chrdev_region(&dev, 0, NUM_MINORS, "vmur");
874 PRINT_ERR("alloc_chrdev_region failed: err = %d\n", rc);
875 goto fail_unregister_driver;
877 ur_first_dev_maj_min = MKDEV(MAJOR(dev), 0);
879 vmur_class = class_create(THIS_MODULE, "vmur");
880 if (IS_ERR(vmur_class)) {
881 rc = PTR_ERR(vmur_class);
882 goto fail_unregister_region;
884 PRINT_INFO("%s loaded.\n", ur_banner);
887 fail_unregister_region:
888 unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS);
889 fail_unregister_driver:
890 ccw_driver_unregister(&ur_driver);
892 debug_unregister(vmur_dbf);
896 static void __exit ur_exit(void)
898 class_destroy(vmur_class);
899 unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS);
900 ccw_driver_unregister(&ur_driver);
901 debug_unregister(vmur_dbf);
902 PRINT_INFO("%s unloaded.\n", ur_banner);
905 module_init(ur_init);
906 module_exit(ur_exit);