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.
126 * alloc_chan_prog allocates and builds the channel program
127 * free_chan_prog frees memory of the channel program
129 * do_ur_io issues the channel program to the device and blocks waiting
130 * on a completion event it publishes at urd->io_done. The function
131 * serialises itself on the device's mutex so that only one I/O
132 * is issued at a time (and that I/O is synchronous).
134 * ur_int_handler catches the "I/O done" interrupt, writes the
135 * subchannel status word into the scsw member of the urdev structure
136 * and complete()s the io_done to wake the waiting do_ur_io.
138 * The caller of do_ur_io is responsible for kfree()ing the channel program
139 * address pointer that alloc_chan_prog returned.
142 static void free_chan_prog(struct ccw1
*cpa
)
144 struct ccw1
*ptr
= cpa
;
147 kfree((void *)(addr_t
) ptr
->cda
);
155 * The channel program we use is write commands chained together
156 * with a final NOP CCW command-chained on (which ensures that CE and DE
157 * are presented together in a single interrupt instead of as separate
158 * interrupts unless an incorrect length indication kicks in first). The
159 * data length in each CCW is reclen.
161 static struct ccw1
*alloc_chan_prog(const char __user
*ubuf
, int rec_count
,
168 TRACE("alloc_chan_prog(%p, %i, %i)\n", ubuf
, rec_count
, reclen
);
171 * We chain a NOP onto the writes to force CE+DE together.
172 * That means we allocate room for CCWs to cover count/reclen
173 * records plus a NOP.
175 cpa
= kzalloc((rec_count
+ 1) * sizeof(struct ccw1
),
176 GFP_KERNEL
| GFP_DMA
);
178 return ERR_PTR(-ENOMEM
);
180 for (i
= 0; i
< rec_count
; i
++) {
181 cpa
[i
].cmd_code
= WRITE_CCW_CMD
;
182 cpa
[i
].flags
= CCW_FLAG_CC
| CCW_FLAG_SLI
;
183 cpa
[i
].count
= reclen
;
184 kbuf
= kmalloc(reclen
, GFP_KERNEL
| GFP_DMA
);
187 return ERR_PTR(-ENOMEM
);
189 cpa
[i
].cda
= (u32
)(addr_t
) kbuf
;
190 if (copy_from_user(kbuf
, ubuf
, reclen
)) {
192 return ERR_PTR(-EFAULT
);
196 /* The following NOP CCW forces CE+DE to be presented together */
197 cpa
[i
].cmd_code
= CCW_CMD_NOOP
;
201 static int do_ur_io(struct urdev
*urd
, struct ccw1
*cpa
)
204 struct ccw_device
*cdev
= urd
->cdev
;
205 DECLARE_COMPLETION_ONSTACK(event
);
207 TRACE("do_ur_io: cpa=%p\n", cpa
);
209 rc
= mutex_lock_interruptible(&urd
->io_mutex
);
213 urd
->io_done
= &event
;
215 spin_lock_irq(get_ccwdev_lock(cdev
));
216 rc
= ccw_device_start(cdev
, cpa
, 1, 0, 0);
217 spin_unlock_irq(get_ccwdev_lock(cdev
));
219 TRACE("do_ur_io: ccw_device_start returned %d\n", rc
);
223 wait_for_completion(&event
);
224 TRACE("do_ur_io: I/O complete\n");
228 mutex_unlock(&urd
->io_mutex
);
233 * ur interrupt handler, called from the ccw_device layer
235 static void ur_int_handler(struct ccw_device
*cdev
, unsigned long intparm
,
240 TRACE("ur_int_handler: intparm=0x%lx cstat=%02x dstat=%02x res=%u\n",
241 intparm
, irb
->scsw
.cstat
, irb
->scsw
.dstat
, irb
->scsw
.count
);
244 TRACE("ur_int_handler: unsolicited interrupt\n");
247 urd
= cdev
->dev
.driver_data
;
248 /* On special conditions irb is an error pointer */
250 urd
->io_request_rc
= PTR_ERR(irb
);
251 else if (irb
->scsw
.dstat
== (DEV_STAT_CHN_END
| DEV_STAT_DEV_END
))
252 urd
->io_request_rc
= 0;
254 urd
->io_request_rc
= -EIO
;
256 complete(urd
->io_done
);
260 * reclen sysfs attribute - The record length to be used for write CCWs
262 static ssize_t
ur_attr_reclen_show(struct device
*dev
,
263 struct device_attribute
*attr
, char *buf
)
265 struct urdev
*urd
= dev
->driver_data
;
267 return sprintf(buf
, "%zu\n", urd
->reclen
);
270 static DEVICE_ATTR(reclen
, 0444, ur_attr_reclen_show
, NULL
);
272 static int ur_create_attributes(struct device
*dev
)
274 return device_create_file(dev
, &dev_attr_reclen
);
277 static void ur_remove_attributes(struct device
*dev
)
279 device_remove_file(dev
, &dev_attr_reclen
);
283 * diagnose code 0x210 - retrieve device information
284 * cc=0 normal completion, we have a real device
285 * cc=1 CP paging error
286 * cc=2 The virtual device exists, but is not associated with a real device
287 * cc=3 Invalid device address, or the virtual device does not exist
289 static int get_urd_class(struct urdev
*urd
)
291 static struct diag210 ur_diag210
;
294 ur_diag210
.vrdcdvno
= urd
->dev_id
.devno
;
295 ur_diag210
.vrdclen
= sizeof(struct diag210
);
297 cc
= diag210(&ur_diag210
);
302 return ur_diag210
.vrdcvcla
; /* virtual device class */
311 * Allocation and freeing of urfile structures
313 static struct urfile
*urfile_alloc(struct urdev
*urd
)
317 urf
= kzalloc(sizeof(struct urfile
), GFP_KERNEL
);
322 TRACE("urfile_alloc: urd=%p urf=%p rl=%zu\n", urd
, urf
,
328 static void urfile_free(struct urfile
*urf
)
330 TRACE("urfile_free: urf=%p urd=%p\n", urf
, urf
->urd
);
335 * The fops implementation of the character device driver
337 static ssize_t
do_write(struct urdev
*urd
, const char __user
*udata
,
338 size_t count
, size_t reclen
, loff_t
*ppos
)
343 cpa
= alloc_chan_prog(udata
, count
/ reclen
, reclen
);
347 rc
= do_ur_io(urd
, cpa
);
351 if (urd
->io_request_rc
) {
352 rc
= urd
->io_request_rc
;
363 static ssize_t
ur_write(struct file
*file
, const char __user
*udata
,
364 size_t count
, loff_t
*ppos
)
366 struct urfile
*urf
= file
->private_data
;
368 TRACE("ur_write: count=%zu\n", count
);
373 if (count
% urf
->dev_reclen
)
374 return -EINVAL
; /* count must be a multiple of reclen */
376 if (count
> urf
->dev_reclen
* MAX_RECS_PER_IO
)
377 count
= urf
->dev_reclen
* MAX_RECS_PER_IO
;
379 return do_write(urf
->urd
, udata
, count
, urf
->dev_reclen
, ppos
);
382 static int do_diag_14(unsigned long rx
, unsigned long ry1
,
383 unsigned long subcode
)
385 register unsigned long _ry1
asm("2") = ry1
;
386 register unsigned long _ry2
asm("3") = subcode
;
399 : "=d" (rc
), "+d" (_ry2
)
400 : "d" (rx
), "d" (_ry1
)
403 TRACE("diag 14: subcode=0x%lx, cc=%i\n", subcode
, rc
);
408 * diagnose code 0x14 subcode 0x0028 - position spool file to designated
410 * cc=0 normal completion
411 * cc=2 no file active on the virtual reader or device not ready
412 * cc=3 record specified is beyond EOF
414 static int diag_position_to_record(int devno
, int record
)
418 cc
= do_diag_14(record
, devno
, 0x28);
425 return -ENODATA
; /* position beyond end of file */
432 * diagnose code 0x14 subcode 0x0000 - read next spool file buffer
433 * cc=0 normal completion
435 * cc=2 no file active on the virtual reader, and no file eligible
436 * cc=3 file already active on the virtual reader or specified virtual
437 * reader does not exist or is not a reader
439 static int diag_read_file(int devno
, char *buf
)
443 cc
= do_diag_14((unsigned long) buf
, devno
, 0x00);
456 static ssize_t
diag14_read(struct file
*file
, char __user
*ubuf
, size_t count
,
459 size_t len
, copied
, res
;
465 urd
= ((struct urfile
*) file
->private_data
)->urd
;
466 reclen
= ((struct urfile
*) file
->private_data
)->file_reclen
;
468 rc
= diag_position_to_record(urd
->dev_id
.devno
, *offs
/ PAGE_SIZE
+ 1);
474 len
= min((size_t) PAGE_SIZE
, count
);
475 buf
= (char *) __get_free_page(GFP_KERNEL
| GFP_DMA
);
480 res
= (size_t) (*offs
% PAGE_SIZE
);
482 rc
= diag_read_file(urd
->dev_id
.devno
, buf
);
483 if (rc
== -ENODATA
) {
488 if (reclen
&& (copied
== 0) && (*offs
< PAGE_SIZE
))
489 *((u16
*) &buf
[FILE_RECLEN_OFFSET
]) = reclen
;
490 len
= min(count
- copied
, PAGE_SIZE
- res
);
491 if (copy_to_user(ubuf
+ copied
, buf
+ res
, len
)) {
497 } while (copied
!= count
);
502 free_page((unsigned long) buf
);
506 static ssize_t
ur_read(struct file
*file
, char __user
*ubuf
, size_t count
,
512 TRACE("ur_read: count=%zu ppos=%li\n", count
, (unsigned long) *offs
);
517 urd
= ((struct urfile
*) file
->private_data
)->urd
;
518 rc
= mutex_lock_interruptible(&urd
->io_mutex
);
521 rc
= diag14_read(file
, ubuf
, count
, offs
);
522 mutex_unlock(&urd
->io_mutex
);
527 * diagnose code 0x14 subcode 0x0fff - retrieve next file descriptor
528 * cc=0 normal completion
529 * cc=1 no files on reader queue or no subsequent file
530 * cc=2 spid specified is invalid
532 static int diag_read_next_file_info(struct file_control_block
*buf
, int spid
)
536 cc
= do_diag_14((unsigned long) buf
, spid
, 0xfff);
545 static int verify_uri_device(struct urdev
*urd
)
547 struct file_control_block
*fcb
;
551 fcb
= kmalloc(sizeof(*fcb
), GFP_KERNEL
| GFP_DMA
);
555 /* check for empty reader device (beginning of chain) */
556 rc
= diag_read_next_file_info(fcb
, 0);
560 /* if file is in hold status, we do not read it */
561 if (fcb
->file_stat
& (FLG_SYSTEM_HOLD
| FLG_USER_HOLD
)) {
566 /* open file on virtual reader */
567 buf
= (char *) __get_free_page(GFP_KERNEL
| GFP_DMA
);
572 rc
= diag_read_file(urd
->dev_id
.devno
, buf
);
573 if ((rc
!= 0) && (rc
!= -ENODATA
)) /* EOF does not hurt */
576 /* check if the file on top of the queue is open now */
577 rc
= diag_read_next_file_info(fcb
, 0);
580 if (!(fcb
->file_stat
& FLG_IN_USE
)) {
587 free_page((unsigned long) buf
);
593 static int verify_device(struct urdev
*urd
)
595 switch (urd
->class) {
597 return 0; /* no check needed here */
599 return verify_uri_device(urd
);
605 static int get_uri_file_reclen(struct urdev
*urd
)
607 struct file_control_block
*fcb
;
610 fcb
= kmalloc(sizeof(*fcb
), GFP_KERNEL
| GFP_DMA
);
613 rc
= diag_read_next_file_info(fcb
, 0);
616 if (fcb
->file_stat
& FLG_CP_DUMP
)
626 static int get_file_reclen(struct urdev
*urd
)
628 switch (urd
->class) {
632 return get_uri_file_reclen(urd
);
638 static int ur_open(struct inode
*inode
, struct file
*file
)
643 unsigned short accmode
;
646 accmode
= file
->f_flags
& O_ACCMODE
;
648 if (accmode
== O_RDWR
)
652 * We treat the minor number as the devno of the ur device
653 * to find in the driver tree.
655 devno
= MINOR(file
->f_dentry
->d_inode
->i_rdev
);
657 urd
= urdev_get_from_devno(devno
);
661 if (file
->f_flags
& O_NONBLOCK
) {
662 if (!mutex_trylock(&urd
->open_mutex
)) {
667 if (mutex_lock_interruptible(&urd
->open_mutex
)) {
675 if (((accmode
== O_RDONLY
) && (urd
->class != DEV_CLASS_UR_I
)) ||
676 ((accmode
== O_WRONLY
) && (urd
->class != DEV_CLASS_UR_O
))) {
677 TRACE("ur_open: unsupported dev class (%d)\n", urd
->class);
682 rc
= verify_device(urd
);
686 urf
= urfile_alloc(urd
);
692 urf
->dev_reclen
= urd
->reclen
;
693 rc
= get_file_reclen(urd
);
695 goto fail_urfile_free
;
696 urf
->file_reclen
= rc
;
697 file
->private_data
= urf
;
703 mutex_unlock(&urd
->open_mutex
);
709 static int ur_release(struct inode
*inode
, struct file
*file
)
711 struct urfile
*urf
= file
->private_data
;
713 TRACE("ur_release\n");
714 mutex_unlock(&urf
->urd
->open_mutex
);
720 static loff_t
ur_llseek(struct file
*file
, loff_t offset
, int whence
)
724 if ((file
->f_flags
& O_ACCMODE
) != O_RDONLY
)
725 return -ESPIPE
; /* seek allowed only for reader */
726 if (offset
% PAGE_SIZE
)
727 return -ESPIPE
; /* only multiples of 4K allowed */
729 case 0: /* SEEK_SET */
732 case 1: /* SEEK_CUR */
733 newpos
= file
->f_pos
+ offset
;
738 file
->f_pos
= newpos
;
742 static struct file_operations ur_fops
= {
743 .owner
= THIS_MODULE
,
745 .release
= ur_release
,
752 * ccw_device infrastructure:
753 * ur_probe gets its own ref to the device (i.e. get_device),
754 * creates the struct urdev, the device attributes, sets up
755 * the interrupt handler and validates the virtual unit record device.
756 * ur_remove removes the device attributes, frees the struct urdev
757 * and drops (put_device) the ref to the device we got in ur_probe.
759 static int ur_probe(struct ccw_device
*cdev
)
764 TRACE("ur_probe: cdev=%p state=%d\n", cdev
, *(int *) cdev
->private);
766 if (!get_device(&cdev
->dev
))
769 urd
= urdev_alloc(cdev
);
774 rc
= ur_create_attributes(&cdev
->dev
);
779 cdev
->dev
.driver_data
= urd
;
780 cdev
->handler
= ur_int_handler
;
782 /* validate virtual unit record device */
783 urd
->class = get_urd_class(urd
);
784 if (urd
->class < 0) {
788 if ((urd
->class != DEV_CLASS_UR_I
) && (urd
->class != DEV_CLASS_UR_O
)) {
797 put_device(&cdev
->dev
);
801 static void ur_remove(struct ccw_device
*cdev
)
803 struct urdev
*urd
= cdev
->dev
.driver_data
;
805 TRACE("ur_remove\n");
807 ur_set_offline(cdev
);
808 ur_remove_attributes(&cdev
->dev
);
810 put_device(&cdev
->dev
);
813 static int ur_set_online(struct ccw_device
*cdev
)
816 int minor
, major
, rc
;
819 TRACE("ur_set_online: cdev=%p state=%d\n", cdev
,
820 *(int *) cdev
->private);
822 if (!try_module_get(ur_driver
.owner
))
825 urd
= (struct urdev
*) cdev
->dev
.driver_data
;
826 minor
= urd
->dev_id
.devno
;
827 major
= MAJOR(ur_first_dev_maj_min
);
829 urd
->char_device
= cdev_alloc();
830 if (!urd
->char_device
) {
832 goto fail_module_put
;
835 cdev_init(urd
->char_device
, &ur_fops
);
836 urd
->char_device
->dev
= MKDEV(major
, minor
);
837 urd
->char_device
->owner
= ur_fops
.owner
;
839 rc
= cdev_add(urd
->char_device
, urd
->char_device
->dev
, 1);
842 if (urd
->cdev
->id
.cu_type
== READER_PUNCH_DEVTYPE
) {
843 if (urd
->class == DEV_CLASS_UR_I
)
844 sprintf(node_id
, "vmrdr-%s", cdev
->dev
.bus_id
);
845 if (urd
->class == DEV_CLASS_UR_O
)
846 sprintf(node_id
, "vmpun-%s", cdev
->dev
.bus_id
);
847 } else if (urd
->cdev
->id
.cu_type
== PRINTER_DEVTYPE
) {
848 sprintf(node_id
, "vmprt-%s", cdev
->dev
.bus_id
);
854 urd
->device
= device_create(vmur_class
, NULL
, urd
->char_device
->dev
,
856 if (IS_ERR(urd
->device
)) {
857 rc
= PTR_ERR(urd
->device
);
858 TRACE("ur_set_online: device_create rc=%d\n", rc
);
865 cdev_del(urd
->char_device
);
867 module_put(ur_driver
.owner
);
872 static int ur_set_offline(struct ccw_device
*cdev
)
876 TRACE("ur_set_offline: cdev=%p cdev->private=%p state=%d\n",
877 cdev
, cdev
->private, *(int *) cdev
->private);
878 urd
= (struct urdev
*) cdev
->dev
.driver_data
;
879 device_destroy(vmur_class
, urd
->char_device
->dev
);
880 cdev_del(urd
->char_device
);
881 module_put(ur_driver
.owner
);
887 * Module initialisation and cleanup
889 static int __init
ur_init(void)
894 if (!MACHINE_IS_VM
) {
895 PRINT_ERR("%s is only available under z/VM.\n", ur_banner
);
899 vmur_dbf
= debug_register("vmur", 4, 1, 4 * sizeof(long));
902 rc
= debug_register_view(vmur_dbf
, &debug_sprintf_view
);
906 debug_set_level(vmur_dbf
, 6);
908 rc
= ccw_driver_register(&ur_driver
);
912 rc
= alloc_chrdev_region(&dev
, 0, NUM_MINORS
, "vmur");
914 PRINT_ERR("alloc_chrdev_region failed: err = %d\n", rc
);
915 goto fail_unregister_driver
;
917 ur_first_dev_maj_min
= MKDEV(MAJOR(dev
), 0);
919 vmur_class
= class_create(THIS_MODULE
, "vmur");
920 if (IS_ERR(vmur_class
)) {
921 rc
= PTR_ERR(vmur_class
);
922 goto fail_unregister_region
;
924 PRINT_INFO("%s loaded.\n", ur_banner
);
927 fail_unregister_region
:
928 unregister_chrdev_region(ur_first_dev_maj_min
, NUM_MINORS
);
929 fail_unregister_driver
:
930 ccw_driver_unregister(&ur_driver
);
932 debug_unregister(vmur_dbf
);
936 static void __exit
ur_exit(void)
938 class_destroy(vmur_class
);
939 unregister_chrdev_region(ur_first_dev_maj_min
, NUM_MINORS
);
940 ccw_driver_unregister(&ur_driver
);
941 debug_unregister(vmur_dbf
);
942 PRINT_INFO("%s unloaded.\n", ur_banner
);
945 module_init(ur_init
);
946 module_exit(ur_exit
);