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>
12 #include <linux/smp_lock.h>
14 #include <asm/uaccess.h>
16 #include <asm/ccwdev.h>
17 #include <asm/debug.h>
25 * Unit record device support is implemented as a character device driver.
26 * We can fit at least 16 bits into a device minor number and use the
27 * simple method of mapping a character device number with minor abcd
28 * to the unit record device with devno abcd.
29 * I/O to virtual unit record devices is handled as follows:
30 * Reads: Diagnose code 0x14 (input spool file manipulation)
31 * is used to read spool data page-wise.
32 * Writes: The CCW used is WRITE_CCW_CMD (0x01). The device's record length
33 * is available by reading sysfs attr reclen. Each write() to the device
34 * must specify an integral multiple (maximal 511) of reclen.
37 static char ur_banner[] = "z/VM virtual unit record device driver";
39 MODULE_AUTHOR("IBM Corporation");
40 MODULE_DESCRIPTION("s390 z/VM virtual unit record device driver");
41 MODULE_LICENSE("GPL");
43 #define PRINTK_HEADER "vmur: "
45 static dev_t ur_first_dev_maj_min;
46 static struct class *vmur_class;
47 static struct debug_info *vmur_dbf;
49 /* We put the device's record length (for writes) in the driver_info field */
50 static struct ccw_device_id ur_ids[] = {
51 { CCWDEV_CU_DI(READER_PUNCH_DEVTYPE, 80) },
52 { CCWDEV_CU_DI(PRINTER_DEVTYPE, 132) },
56 MODULE_DEVICE_TABLE(ccw, ur_ids);
58 static int ur_probe(struct ccw_device *cdev);
59 static void ur_remove(struct ccw_device *cdev);
60 static int ur_set_online(struct ccw_device *cdev);
61 static int ur_set_offline(struct ccw_device *cdev);
63 static struct ccw_driver ur_driver = {
69 .set_online = ur_set_online,
70 .set_offline = ur_set_offline,
73 static DEFINE_MUTEX(vmur_mutex);
76 * Allocation, freeing, getting and putting of urdev structures
78 * Each ur device (urd) contains a reference to its corresponding ccw device
79 * (cdev) using the urd->cdev pointer. Each ccw device has a reference to the
80 * ur device using the cdev->dev.driver_data pointer.
83 * - ur_probe gets a urd reference, ur_remove drops the reference
84 * (cdev->dev.driver_data)
85 * - ur_open gets a urd reference, ur_relase drops the reference
89 * - urdev_alloc get a cdev reference (urd->cdev)
90 * - urdev_free drops the cdev reference (urd->cdev)
92 * Setting and clearing of cdev->dev.driver_data is protected by the ccwdev lock
94 static struct urdev *urdev_alloc(struct ccw_device *cdev)
98 urd = kzalloc(sizeof(struct urdev), GFP_KERNEL);
101 urd->reclen = cdev->id.driver_info;
102 ccw_device_get_id(cdev, &urd->dev_id);
103 mutex_init(&urd->io_mutex);
104 init_waitqueue_head(&urd->wait);
105 spin_lock_init(&urd->open_lock);
106 atomic_set(&urd->ref_count, 1);
108 get_device(&cdev->dev);
112 static void urdev_free(struct urdev *urd)
114 TRACE("urdev_free: %p\n", urd);
116 put_device(&urd->cdev->dev);
120 static void urdev_get(struct urdev *urd)
122 atomic_inc(&urd->ref_count);
125 static struct urdev *urdev_get_from_cdev(struct ccw_device *cdev)
130 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
131 urd = cdev->dev.driver_data;
134 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
138 static struct urdev *urdev_get_from_devno(u16 devno)
141 struct ccw_device *cdev;
144 sprintf(bus_id, "0.0.%04x", devno);
145 cdev = get_ccwdev_by_busid(&ur_driver, bus_id);
148 urd = urdev_get_from_cdev(cdev);
149 put_device(&cdev->dev);
153 static void urdev_put(struct urdev *urd)
155 if (atomic_dec_and_test(&urd->ref_count))
160 * Low-level functions to do I/O to a ur device.
166 * alloc_chan_prog allocates and builds the channel program
167 * free_chan_prog frees memory of the channel program
169 * do_ur_io issues the channel program to the device and blocks waiting
170 * on a completion event it publishes at urd->io_done. The function
171 * serialises itself on the device's mutex so that only one I/O
172 * is issued at a time (and that I/O is synchronous).
174 * ur_int_handler catches the "I/O done" interrupt, writes the
175 * subchannel status word into the scsw member of the urdev structure
176 * and complete()s the io_done to wake the waiting do_ur_io.
178 * The caller of do_ur_io is responsible for kfree()ing the channel program
179 * address pointer that alloc_chan_prog returned.
182 static void free_chan_prog(struct ccw1 *cpa)
184 struct ccw1 *ptr = cpa;
187 kfree((void *)(addr_t) ptr->cda);
195 * The channel program we use is write commands chained together
196 * with a final NOP CCW command-chained on (which ensures that CE and DE
197 * are presented together in a single interrupt instead of as separate
198 * interrupts unless an incorrect length indication kicks in first). The
199 * data length in each CCW is reclen.
201 static struct ccw1 *alloc_chan_prog(const char __user *ubuf, int rec_count,
208 TRACE("alloc_chan_prog(%p, %i, %i)\n", ubuf, rec_count, reclen);
211 * We chain a NOP onto the writes to force CE+DE together.
212 * That means we allocate room for CCWs to cover count/reclen
213 * records plus a NOP.
215 cpa = kzalloc((rec_count + 1) * sizeof(struct ccw1),
216 GFP_KERNEL | GFP_DMA);
218 return ERR_PTR(-ENOMEM);
220 for (i = 0; i < rec_count; i++) {
221 cpa[i].cmd_code = WRITE_CCW_CMD;
222 cpa[i].flags = CCW_FLAG_CC | CCW_FLAG_SLI;
223 cpa[i].count = reclen;
224 kbuf = kmalloc(reclen, GFP_KERNEL | GFP_DMA);
227 return ERR_PTR(-ENOMEM);
229 cpa[i].cda = (u32)(addr_t) kbuf;
230 if (copy_from_user(kbuf, ubuf, reclen)) {
232 return ERR_PTR(-EFAULT);
236 /* The following NOP CCW forces CE+DE to be presented together */
237 cpa[i].cmd_code = CCW_CMD_NOOP;
241 static int do_ur_io(struct urdev *urd, struct ccw1 *cpa)
244 struct ccw_device *cdev = urd->cdev;
245 DECLARE_COMPLETION_ONSTACK(event);
247 TRACE("do_ur_io: cpa=%p\n", cpa);
249 rc = mutex_lock_interruptible(&urd->io_mutex);
253 urd->io_done = &event;
255 spin_lock_irq(get_ccwdev_lock(cdev));
256 rc = ccw_device_start(cdev, cpa, 1, 0, 0);
257 spin_unlock_irq(get_ccwdev_lock(cdev));
259 TRACE("do_ur_io: ccw_device_start returned %d\n", rc);
263 wait_for_completion(&event);
264 TRACE("do_ur_io: I/O complete\n");
268 mutex_unlock(&urd->io_mutex);
273 * ur interrupt handler, called from the ccw_device layer
275 static void ur_int_handler(struct ccw_device *cdev, unsigned long intparm,
280 TRACE("ur_int_handler: intparm=0x%lx cstat=%02x dstat=%02x res=%u\n",
281 intparm, irb->scsw.cmd.cstat, irb->scsw.cmd.dstat,
282 irb->scsw.cmd.count);
285 TRACE("ur_int_handler: unsolicited interrupt\n");
288 urd = cdev->dev.driver_data;
290 /* On special conditions irb is an error pointer */
292 urd->io_request_rc = PTR_ERR(irb);
293 else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END))
294 urd->io_request_rc = 0;
296 urd->io_request_rc = -EIO;
298 complete(urd->io_done);
302 * reclen sysfs attribute - The record length to be used for write CCWs
304 static ssize_t ur_attr_reclen_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
310 urd = urdev_get_from_cdev(to_ccwdev(dev));
313 rc = sprintf(buf, "%zu\n", urd->reclen);
318 static DEVICE_ATTR(reclen, 0444, ur_attr_reclen_show, NULL);
320 static int ur_create_attributes(struct device *dev)
322 return device_create_file(dev, &dev_attr_reclen);
325 static void ur_remove_attributes(struct device *dev)
327 device_remove_file(dev, &dev_attr_reclen);
331 * diagnose code 0x210 - retrieve device information
332 * cc=0 normal completion, we have a real device
333 * cc=1 CP paging error
334 * cc=2 The virtual device exists, but is not associated with a real device
335 * cc=3 Invalid device address, or the virtual device does not exist
337 static int get_urd_class(struct urdev *urd)
339 static struct diag210 ur_diag210;
342 ur_diag210.vrdcdvno = urd->dev_id.devno;
343 ur_diag210.vrdclen = sizeof(struct diag210);
345 cc = diag210(&ur_diag210);
350 return ur_diag210.vrdcvcla; /* virtual device class */
359 * Allocation and freeing of urfile structures
361 static struct urfile *urfile_alloc(struct urdev *urd)
365 urf = kzalloc(sizeof(struct urfile), GFP_KERNEL);
370 TRACE("urfile_alloc: urd=%p urf=%p rl=%zu\n", urd, urf,
376 static void urfile_free(struct urfile *urf)
378 TRACE("urfile_free: urf=%p urd=%p\n", urf, urf->urd);
383 * The fops implementation of the character device driver
385 static ssize_t do_write(struct urdev *urd, const char __user *udata,
386 size_t count, size_t reclen, loff_t *ppos)
391 cpa = alloc_chan_prog(udata, count / reclen, reclen);
395 rc = do_ur_io(urd, cpa);
399 if (urd->io_request_rc) {
400 rc = urd->io_request_rc;
411 static ssize_t ur_write(struct file *file, const char __user *udata,
412 size_t count, loff_t *ppos)
414 struct urfile *urf = file->private_data;
416 TRACE("ur_write: count=%zu\n", count);
421 if (count % urf->dev_reclen)
422 return -EINVAL; /* count must be a multiple of reclen */
424 if (count > urf->dev_reclen * MAX_RECS_PER_IO)
425 count = urf->dev_reclen * MAX_RECS_PER_IO;
427 return do_write(urf->urd, udata, count, urf->dev_reclen, ppos);
431 * diagnose code 0x14 subcode 0x0028 - position spool file to designated
433 * cc=0 normal completion
434 * cc=2 no file active on the virtual reader or device not ready
435 * cc=3 record specified is beyond EOF
437 static int diag_position_to_record(int devno, int record)
441 cc = diag14(record, devno, 0x28);
448 return -ENODATA; /* position beyond end of file */
455 * diagnose code 0x14 subcode 0x0000 - read next spool file buffer
456 * cc=0 normal completion
458 * cc=2 no file active on the virtual reader, and no file eligible
459 * cc=3 file already active on the virtual reader or specified virtual
460 * reader does not exist or is not a reader
462 static int diag_read_file(int devno, char *buf)
466 cc = diag14((unsigned long) buf, devno, 0x00);
479 static ssize_t diag14_read(struct file *file, char __user *ubuf, size_t count,
482 size_t len, copied, res;
488 urd = ((struct urfile *) file->private_data)->urd;
489 reclen = ((struct urfile *) file->private_data)->file_reclen;
491 rc = diag_position_to_record(urd->dev_id.devno, *offs / PAGE_SIZE + 1);
497 len = min((size_t) PAGE_SIZE, count);
498 buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA);
503 res = (size_t) (*offs % PAGE_SIZE);
505 rc = diag_read_file(urd->dev_id.devno, buf);
506 if (rc == -ENODATA) {
511 if (reclen && (copied == 0) && (*offs < PAGE_SIZE))
512 *((u16 *) &buf[FILE_RECLEN_OFFSET]) = reclen;
513 len = min(count - copied, PAGE_SIZE - res);
514 if (copy_to_user(ubuf + copied, buf + res, len)) {
520 } while (copied != count);
525 free_page((unsigned long) buf);
529 static ssize_t ur_read(struct file *file, char __user *ubuf, size_t count,
535 TRACE("ur_read: count=%zu ppos=%li\n", count, (unsigned long) *offs);
540 urd = ((struct urfile *) file->private_data)->urd;
541 rc = mutex_lock_interruptible(&urd->io_mutex);
544 rc = diag14_read(file, ubuf, count, offs);
545 mutex_unlock(&urd->io_mutex);
550 * diagnose code 0x14 subcode 0x0fff - retrieve next file descriptor
551 * cc=0 normal completion
552 * cc=1 no files on reader queue or no subsequent file
553 * cc=2 spid specified is invalid
555 static int diag_read_next_file_info(struct file_control_block *buf, int spid)
559 cc = diag14((unsigned long) buf, spid, 0xfff);
568 static int verify_uri_device(struct urdev *urd)
570 struct file_control_block *fcb;
574 fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA);
578 /* check for empty reader device (beginning of chain) */
579 rc = diag_read_next_file_info(fcb, 0);
583 /* if file is in hold status, we do not read it */
584 if (fcb->file_stat & (FLG_SYSTEM_HOLD | FLG_USER_HOLD)) {
589 /* open file on virtual reader */
590 buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA);
595 rc = diag_read_file(urd->dev_id.devno, buf);
596 if ((rc != 0) && (rc != -ENODATA)) /* EOF does not hurt */
599 /* check if the file on top of the queue is open now */
600 rc = diag_read_next_file_info(fcb, 0);
603 if (!(fcb->file_stat & FLG_IN_USE)) {
610 free_page((unsigned long) buf);
616 static int verify_device(struct urdev *urd)
618 switch (urd->class) {
620 return 0; /* no check needed here */
622 return verify_uri_device(urd);
628 static int get_uri_file_reclen(struct urdev *urd)
630 struct file_control_block *fcb;
633 fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA);
636 rc = diag_read_next_file_info(fcb, 0);
639 if (fcb->file_stat & FLG_CP_DUMP)
649 static int get_file_reclen(struct urdev *urd)
651 switch (urd->class) {
655 return get_uri_file_reclen(urd);
661 static int ur_open(struct inode *inode, struct file *file)
666 unsigned short accmode;
669 accmode = file->f_flags & O_ACCMODE;
671 if (accmode == O_RDWR)
675 * We treat the minor number as the devno of the ur device
676 * to find in the driver tree.
678 devno = MINOR(file->f_dentry->d_inode->i_rdev);
680 urd = urdev_get_from_devno(devno);
686 spin_lock(&urd->open_lock);
687 while (urd->open_flag) {
688 spin_unlock(&urd->open_lock);
689 if (file->f_flags & O_NONBLOCK) {
693 if (wait_event_interruptible(urd->wait, urd->open_flag == 0)) {
697 spin_lock(&urd->open_lock);
700 spin_unlock(&urd->open_lock);
704 if (((accmode == O_RDONLY) && (urd->class != DEV_CLASS_UR_I)) ||
705 ((accmode == O_WRONLY) && (urd->class != DEV_CLASS_UR_O))) {
706 TRACE("ur_open: unsupported dev class (%d)\n", urd->class);
711 rc = verify_device(urd);
715 urf = urfile_alloc(urd);
721 urf->dev_reclen = urd->reclen;
722 rc = get_file_reclen(urd);
724 goto fail_urfile_free;
725 urf->file_reclen = rc;
726 file->private_data = urf;
733 spin_lock(&urd->open_lock);
735 spin_unlock(&urd->open_lock);
743 static int ur_release(struct inode *inode, struct file *file)
745 struct urfile *urf = file->private_data;
747 TRACE("ur_release\n");
748 spin_lock(&urf->urd->open_lock);
749 urf->urd->open_flag--;
750 spin_unlock(&urf->urd->open_lock);
751 wake_up_interruptible(&urf->urd->wait);
757 static loff_t ur_llseek(struct file *file, loff_t offset, int whence)
761 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
762 return -ESPIPE; /* seek allowed only for reader */
763 if (offset % PAGE_SIZE)
764 return -ESPIPE; /* only multiples of 4K allowed */
766 case 0: /* SEEK_SET */
769 case 1: /* SEEK_CUR */
770 newpos = file->f_pos + offset;
775 file->f_pos = newpos;
779 static const struct file_operations ur_fops = {
780 .owner = THIS_MODULE,
782 .release = ur_release,
789 * ccw_device infrastructure:
790 * ur_probe creates the struct urdev (with refcount = 1), the device
791 * attributes, sets up the interrupt handler and validates the virtual
792 * unit record device.
793 * ur_remove removes the device attributes and drops the reference to
796 * ur_probe, ur_remove, ur_set_online and ur_set_offline are serialized
797 * by the vmur_mutex lock.
799 * urd->char_device is used as indication that the online function has
800 * been completed successfully.
802 static int ur_probe(struct ccw_device *cdev)
807 TRACE("ur_probe: cdev=%p\n", cdev);
809 mutex_lock(&vmur_mutex);
810 urd = urdev_alloc(cdev);
816 rc = ur_create_attributes(&cdev->dev);
821 cdev->handler = ur_int_handler;
823 /* validate virtual unit record device */
824 urd->class = get_urd_class(urd);
825 if (urd->class < 0) {
827 goto fail_remove_attr;
829 if ((urd->class != DEV_CLASS_UR_I) && (urd->class != DEV_CLASS_UR_O)) {
831 goto fail_remove_attr;
833 spin_lock_irq(get_ccwdev_lock(cdev));
834 cdev->dev.driver_data = urd;
835 spin_unlock_irq(get_ccwdev_lock(cdev));
837 mutex_unlock(&vmur_mutex);
841 ur_remove_attributes(&cdev->dev);
845 mutex_unlock(&vmur_mutex);
849 static int ur_set_online(struct ccw_device *cdev)
852 int minor, major, rc;
855 TRACE("ur_set_online: cdev=%p\n", cdev);
857 mutex_lock(&vmur_mutex);
858 urd = urdev_get_from_cdev(cdev);
860 /* ur_remove already deleted our urd */
865 if (urd->char_device) {
866 /* Another ur_set_online was faster */
871 minor = urd->dev_id.devno;
872 major = MAJOR(ur_first_dev_maj_min);
874 urd->char_device = cdev_alloc();
875 if (!urd->char_device) {
880 cdev_init(urd->char_device, &ur_fops);
881 urd->char_device->dev = MKDEV(major, minor);
882 urd->char_device->owner = ur_fops.owner;
884 rc = cdev_add(urd->char_device, urd->char_device->dev, 1);
887 if (urd->cdev->id.cu_type == READER_PUNCH_DEVTYPE) {
888 if (urd->class == DEV_CLASS_UR_I)
889 sprintf(node_id, "vmrdr-%s", dev_name(&cdev->dev));
890 if (urd->class == DEV_CLASS_UR_O)
891 sprintf(node_id, "vmpun-%s", dev_name(&cdev->dev));
892 } else if (urd->cdev->id.cu_type == PRINTER_DEVTYPE) {
893 sprintf(node_id, "vmprt-%s", dev_name(&cdev->dev));
899 urd->device = device_create_drvdata(vmur_class, NULL,
900 urd->char_device->dev, NULL,
902 if (IS_ERR(urd->device)) {
903 rc = PTR_ERR(urd->device);
904 TRACE("ur_set_online: device_create rc=%d\n", rc);
908 mutex_unlock(&vmur_mutex);
912 cdev_del(urd->char_device);
913 urd->char_device = NULL;
917 mutex_unlock(&vmur_mutex);
921 static int ur_set_offline_force(struct ccw_device *cdev, int force)
926 TRACE("ur_set_offline: cdev=%p\n", cdev);
927 urd = urdev_get_from_cdev(cdev);
929 /* ur_remove already deleted our urd */
931 if (!urd->char_device) {
932 /* Another ur_set_offline was faster */
936 if (!force && (atomic_read(&urd->ref_count) > 2)) {
937 /* There is still a user of urd (e.g. ur_open) */
938 TRACE("ur_set_offline: BUSY\n");
942 device_destroy(vmur_class, urd->char_device->dev);
943 cdev_del(urd->char_device);
944 urd->char_device = NULL;
952 static int ur_set_offline(struct ccw_device *cdev)
956 mutex_lock(&vmur_mutex);
957 rc = ur_set_offline_force(cdev, 0);
958 mutex_unlock(&vmur_mutex);
962 static void ur_remove(struct ccw_device *cdev)
966 TRACE("ur_remove\n");
968 mutex_lock(&vmur_mutex);
971 ur_set_offline_force(cdev, 1);
972 ur_remove_attributes(&cdev->dev);
974 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
975 urdev_put(cdev->dev.driver_data);
976 cdev->dev.driver_data = NULL;
977 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
979 mutex_unlock(&vmur_mutex);
983 * Module initialisation and cleanup
985 static int __init ur_init(void)
990 if (!MACHINE_IS_VM) {
991 PRINT_ERR("%s is only available under z/VM.\n", ur_banner);
995 vmur_dbf = debug_register("vmur", 4, 1, 4 * sizeof(long));
998 rc = debug_register_view(vmur_dbf, &debug_sprintf_view);
1002 debug_set_level(vmur_dbf, 6);
1004 rc = ccw_driver_register(&ur_driver);
1008 rc = alloc_chrdev_region(&dev, 0, NUM_MINORS, "vmur");
1010 PRINT_ERR("alloc_chrdev_region failed: err = %d\n", rc);
1011 goto fail_unregister_driver;
1013 ur_first_dev_maj_min = MKDEV(MAJOR(dev), 0);
1015 vmur_class = class_create(THIS_MODULE, "vmur");
1016 if (IS_ERR(vmur_class)) {
1017 rc = PTR_ERR(vmur_class);
1018 goto fail_unregister_region;
1020 PRINT_INFO("%s loaded.\n", ur_banner);
1023 fail_unregister_region:
1024 unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS);
1025 fail_unregister_driver:
1026 ccw_driver_unregister(&ur_driver);
1028 debug_unregister(vmur_dbf);
1032 static void __exit ur_exit(void)
1034 class_destroy(vmur_class);
1035 unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS);
1036 ccw_driver_unregister(&ur_driver);
1037 debug_unregister(vmur_dbf);
1038 PRINT_INFO("%s unloaded.\n", ur_banner);
1041 module_init(ur_init);
1042 module_exit(ur_exit);