xref: /openbmc/linux/drivers/s390/char/vmur.c (revision dae1df42f544ce3969d437dfa835169210593240)
1810cb5b3SFrank Munzert /*
2810cb5b3SFrank Munzert  * Linux driver for System z and s390 unit record devices
3810cb5b3SFrank Munzert  * (z/VM virtual punch, reader, printer)
4810cb5b3SFrank Munzert  *
5b241f7bcSFrank Munzert  * Copyright IBM Corp. 2001, 2009
6810cb5b3SFrank Munzert  * Authors: Malcolm Beattie <beattiem@uk.ibm.com>
7810cb5b3SFrank Munzert  *	    Michael Holzheu <holzheu@de.ibm.com>
8810cb5b3SFrank Munzert  *	    Frank Munzert <munzert@de.ibm.com>
9810cb5b3SFrank Munzert  */
10810cb5b3SFrank Munzert 
1180a5c36dSFrank Munzert #define KMSG_COMPONENT "vmur"
1280a5c36dSFrank Munzert #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
1380a5c36dSFrank Munzert 
14810cb5b3SFrank Munzert #include <linux/cdev.h>
155a0e3ad6STejun Heo #include <linux/slab.h>
163a4c5d59SHeiko Carstens #include <linux/module.h>
17810cb5b3SFrank Munzert 
18810cb5b3SFrank Munzert #include <asm/uaccess.h>
19810cb5b3SFrank Munzert #include <asm/cio.h>
20810cb5b3SFrank Munzert #include <asm/ccwdev.h>
21810cb5b3SFrank Munzert #include <asm/debug.h>
220a87c5cfSMichael Holzheu #include <asm/diag.h>
23810cb5b3SFrank Munzert 
24810cb5b3SFrank Munzert #include "vmur.h"
25810cb5b3SFrank Munzert 
26810cb5b3SFrank Munzert /*
27810cb5b3SFrank Munzert  * Driver overview
28810cb5b3SFrank Munzert  *
29810cb5b3SFrank Munzert  * Unit record device support is implemented as a character device driver.
30810cb5b3SFrank Munzert  * We can fit at least 16 bits into a device minor number and use the
31810cb5b3SFrank Munzert  * simple method of mapping a character device number with minor abcd
32810cb5b3SFrank Munzert  * to the unit record device with devno abcd.
33810cb5b3SFrank Munzert  * I/O to virtual unit record devices is handled as follows:
34810cb5b3SFrank Munzert  * Reads: Diagnose code 0x14 (input spool file manipulation)
35810cb5b3SFrank Munzert  * is used to read spool data page-wise.
36810cb5b3SFrank Munzert  * Writes: The CCW used is WRITE_CCW_CMD (0x01). The device's record length
37810cb5b3SFrank Munzert  * is available by reading sysfs attr reclen. Each write() to the device
38810cb5b3SFrank Munzert  * must specify an integral multiple (maximal 511) of reclen.
39810cb5b3SFrank Munzert  */
40810cb5b3SFrank Munzert 
41810cb5b3SFrank Munzert static char ur_banner[] = "z/VM virtual unit record device driver";
42810cb5b3SFrank Munzert 
43810cb5b3SFrank Munzert MODULE_AUTHOR("IBM Corporation");
44810cb5b3SFrank Munzert MODULE_DESCRIPTION("s390 z/VM virtual unit record device driver");
45810cb5b3SFrank Munzert MODULE_LICENSE("GPL");
46810cb5b3SFrank Munzert 
47810cb5b3SFrank Munzert static dev_t ur_first_dev_maj_min;
48810cb5b3SFrank Munzert static struct class *vmur_class;
49810cb5b3SFrank Munzert static struct debug_info *vmur_dbf;
50810cb5b3SFrank Munzert 
51810cb5b3SFrank Munzert /* We put the device's record length (for writes) in the driver_info field */
52810cb5b3SFrank Munzert static struct ccw_device_id ur_ids[] = {
53810cb5b3SFrank Munzert 	{ CCWDEV_CU_DI(READER_PUNCH_DEVTYPE, 80) },
54810cb5b3SFrank Munzert 	{ CCWDEV_CU_DI(PRINTER_DEVTYPE, 132) },
55810cb5b3SFrank Munzert 	{ /* end of list */ }
56810cb5b3SFrank Munzert };
57810cb5b3SFrank Munzert 
58810cb5b3SFrank Munzert MODULE_DEVICE_TABLE(ccw, ur_ids);
59810cb5b3SFrank Munzert 
60810cb5b3SFrank Munzert static int ur_probe(struct ccw_device *cdev);
61810cb5b3SFrank Munzert static void ur_remove(struct ccw_device *cdev);
62810cb5b3SFrank Munzert static int ur_set_online(struct ccw_device *cdev);
63810cb5b3SFrank Munzert static int ur_set_offline(struct ccw_device *cdev);
64b241f7bcSFrank Munzert static int ur_pm_suspend(struct ccw_device *cdev);
65810cb5b3SFrank Munzert 
66810cb5b3SFrank Munzert static struct ccw_driver ur_driver = {
673bda058bSSebastian Ott 	.driver = {
68810cb5b3SFrank Munzert 		.name	= "vmur",
69810cb5b3SFrank Munzert 		.owner	= THIS_MODULE,
703bda058bSSebastian Ott 	},
71810cb5b3SFrank Munzert 	.ids		= ur_ids,
72810cb5b3SFrank Munzert 	.probe		= ur_probe,
73810cb5b3SFrank Munzert 	.remove		= ur_remove,
74810cb5b3SFrank Munzert 	.set_online	= ur_set_online,
75810cb5b3SFrank Munzert 	.set_offline	= ur_set_offline,
76b241f7bcSFrank Munzert 	.freeze		= ur_pm_suspend,
77de400d6bSPeter Oberparleiter 	.int_class	= IOINT_VMR,
78810cb5b3SFrank Munzert };
79810cb5b3SFrank Munzert 
808127a1f8SMichael Holzheu static DEFINE_MUTEX(vmur_mutex);
818127a1f8SMichael Holzheu 
82810cb5b3SFrank Munzert /*
83810cb5b3SFrank Munzert  * Allocation, freeing, getting and putting of urdev structures
848127a1f8SMichael Holzheu  *
858127a1f8SMichael Holzheu  * Each ur device (urd) contains a reference to its corresponding ccw device
868127a1f8SMichael Holzheu  * (cdev) using the urd->cdev pointer. Each ccw device has a reference to the
87dff59b64SGreg Kroah-Hartman  * ur device using dev_get_drvdata(&cdev->dev) pointer.
888127a1f8SMichael Holzheu  *
898127a1f8SMichael Holzheu  * urd references:
908127a1f8SMichael Holzheu  * - ur_probe gets a urd reference, ur_remove drops the reference
91dff59b64SGreg Kroah-Hartman  *   dev_get_drvdata(&cdev->dev)
928127a1f8SMichael Holzheu  * - ur_open gets a urd reference, ur_relase drops the reference
938127a1f8SMichael Holzheu  *   (urf->urd)
948127a1f8SMichael Holzheu  *
958127a1f8SMichael Holzheu  * cdev references:
968127a1f8SMichael Holzheu  * - urdev_alloc get a cdev reference (urd->cdev)
978127a1f8SMichael Holzheu  * - urdev_free drops the cdev reference (urd->cdev)
988127a1f8SMichael Holzheu  *
99dff59b64SGreg Kroah-Hartman  * Setting and clearing of dev_get_drvdata(&cdev->dev) is protected by the ccwdev lock
100810cb5b3SFrank Munzert  */
101810cb5b3SFrank Munzert static struct urdev *urdev_alloc(struct ccw_device *cdev)
102810cb5b3SFrank Munzert {
103810cb5b3SFrank Munzert 	struct urdev *urd;
104810cb5b3SFrank Munzert 
105810cb5b3SFrank Munzert 	urd = kzalloc(sizeof(struct urdev), GFP_KERNEL);
106810cb5b3SFrank Munzert 	if (!urd)
107810cb5b3SFrank Munzert 		return NULL;
108810cb5b3SFrank Munzert 	urd->reclen = cdev->id.driver_info;
109810cb5b3SFrank Munzert 	ccw_device_get_id(cdev, &urd->dev_id);
110810cb5b3SFrank Munzert 	mutex_init(&urd->io_mutex);
111a695f167SFrank Munzert 	init_waitqueue_head(&urd->wait);
112a695f167SFrank Munzert 	spin_lock_init(&urd->open_lock);
1138127a1f8SMichael Holzheu 	atomic_set(&urd->ref_count,  1);
1148127a1f8SMichael Holzheu 	urd->cdev = cdev;
1158127a1f8SMichael Holzheu 	get_device(&cdev->dev);
116810cb5b3SFrank Munzert 	return urd;
117810cb5b3SFrank Munzert }
118810cb5b3SFrank Munzert 
119810cb5b3SFrank Munzert static void urdev_free(struct urdev *urd)
120810cb5b3SFrank Munzert {
1218127a1f8SMichael Holzheu 	TRACE("urdev_free: %p\n", urd);
1228127a1f8SMichael Holzheu 	if (urd->cdev)
1238127a1f8SMichael Holzheu 		put_device(&urd->cdev->dev);
124810cb5b3SFrank Munzert 	kfree(urd);
125810cb5b3SFrank Munzert }
126810cb5b3SFrank Munzert 
1278127a1f8SMichael Holzheu static void urdev_get(struct urdev *urd)
1288127a1f8SMichael Holzheu {
1298127a1f8SMichael Holzheu 	atomic_inc(&urd->ref_count);
1308127a1f8SMichael Holzheu }
1318127a1f8SMichael Holzheu 
1328127a1f8SMichael Holzheu static struct urdev *urdev_get_from_cdev(struct ccw_device *cdev)
1338127a1f8SMichael Holzheu {
1348127a1f8SMichael Holzheu 	struct urdev *urd;
1358127a1f8SMichael Holzheu 	unsigned long flags;
1368127a1f8SMichael Holzheu 
1378127a1f8SMichael Holzheu 	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
138dff59b64SGreg Kroah-Hartman 	urd = dev_get_drvdata(&cdev->dev);
1398127a1f8SMichael Holzheu 	if (urd)
1408127a1f8SMichael Holzheu 		urdev_get(urd);
1418127a1f8SMichael Holzheu 	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1428127a1f8SMichael Holzheu 	return urd;
1438127a1f8SMichael Holzheu }
1448127a1f8SMichael Holzheu 
145810cb5b3SFrank Munzert static struct urdev *urdev_get_from_devno(u16 devno)
146810cb5b3SFrank Munzert {
147810cb5b3SFrank Munzert 	char bus_id[16];
148810cb5b3SFrank Munzert 	struct ccw_device *cdev;
1498127a1f8SMichael Holzheu 	struct urdev *urd;
150810cb5b3SFrank Munzert 
151810cb5b3SFrank Munzert 	sprintf(bus_id, "0.0.%04x", devno);
152810cb5b3SFrank Munzert 	cdev = get_ccwdev_by_busid(&ur_driver, bus_id);
153810cb5b3SFrank Munzert 	if (!cdev)
154810cb5b3SFrank Munzert 		return NULL;
1558127a1f8SMichael Holzheu 	urd = urdev_get_from_cdev(cdev);
1568127a1f8SMichael Holzheu 	put_device(&cdev->dev);
1578127a1f8SMichael Holzheu 	return urd;
158810cb5b3SFrank Munzert }
159810cb5b3SFrank Munzert 
160810cb5b3SFrank Munzert static void urdev_put(struct urdev *urd)
161810cb5b3SFrank Munzert {
1628127a1f8SMichael Holzheu 	if (atomic_dec_and_test(&urd->ref_count))
1638127a1f8SMichael Holzheu 		urdev_free(urd);
164810cb5b3SFrank Munzert }
165810cb5b3SFrank Munzert 
166810cb5b3SFrank Munzert /*
167b241f7bcSFrank Munzert  * State and contents of ur devices can be changed by class D users issuing
168b241f7bcSFrank Munzert  * CP commands such as PURGE or TRANSFER, while the Linux guest is suspended.
169b241f7bcSFrank Munzert  * Also the Linux guest might be logged off, which causes all active spool
170b241f7bcSFrank Munzert  * files to be closed.
171b241f7bcSFrank Munzert  * So we cannot guarantee that spool files are still the same when the Linux
172b241f7bcSFrank Munzert  * guest is resumed. In order to avoid unpredictable results at resume time
173b241f7bcSFrank Munzert  * we simply refuse to suspend if a ur device node is open.
174b241f7bcSFrank Munzert  */
175b241f7bcSFrank Munzert static int ur_pm_suspend(struct ccw_device *cdev)
176b241f7bcSFrank Munzert {
1774f0076f7SMartin Schwidefsky 	struct urdev *urd = dev_get_drvdata(&cdev->dev);
178b241f7bcSFrank Munzert 
179b241f7bcSFrank Munzert 	TRACE("ur_pm_suspend: cdev=%p\n", cdev);
180b241f7bcSFrank Munzert 	if (urd->open_flag) {
181b241f7bcSFrank Munzert 		pr_err("Unit record device %s is busy, %s refusing to "
182b241f7bcSFrank Munzert 		       "suspend.\n", dev_name(&cdev->dev), ur_banner);
183b241f7bcSFrank Munzert 		return -EBUSY;
184b241f7bcSFrank Munzert 	}
185b241f7bcSFrank Munzert 	return 0;
186b241f7bcSFrank Munzert }
187b241f7bcSFrank Munzert 
188b241f7bcSFrank Munzert /*
189810cb5b3SFrank Munzert  * Low-level functions to do I/O to a ur device.
190810cb5b3SFrank Munzert  *     alloc_chan_prog
1911eade380SMichael Holzheu  *     free_chan_prog
192810cb5b3SFrank Munzert  *     do_ur_io
193810cb5b3SFrank Munzert  *     ur_int_handler
194810cb5b3SFrank Munzert  *
195810cb5b3SFrank Munzert  * alloc_chan_prog allocates and builds the channel program
1961eade380SMichael Holzheu  * free_chan_prog frees memory of the channel program
197810cb5b3SFrank Munzert  *
198810cb5b3SFrank Munzert  * do_ur_io issues the channel program to the device and blocks waiting
199810cb5b3SFrank Munzert  * on a completion event it publishes at urd->io_done. The function
200810cb5b3SFrank Munzert  * serialises itself on the device's mutex so that only one I/O
201810cb5b3SFrank Munzert  * is issued at a time (and that I/O is synchronous).
202810cb5b3SFrank Munzert  *
203810cb5b3SFrank Munzert  * ur_int_handler catches the "I/O done" interrupt, writes the
204810cb5b3SFrank Munzert  * subchannel status word into the scsw member of the urdev structure
205810cb5b3SFrank Munzert  * and complete()s the io_done to wake the waiting do_ur_io.
206810cb5b3SFrank Munzert  *
207810cb5b3SFrank Munzert  * The caller of do_ur_io is responsible for kfree()ing the channel program
208810cb5b3SFrank Munzert  * address pointer that alloc_chan_prog returned.
209810cb5b3SFrank Munzert  */
210810cb5b3SFrank Munzert 
2111eade380SMichael Holzheu static void free_chan_prog(struct ccw1 *cpa)
2121eade380SMichael Holzheu {
2131eade380SMichael Holzheu 	struct ccw1 *ptr = cpa;
2141eade380SMichael Holzheu 
2151eade380SMichael Holzheu 	while (ptr->cda) {
2161eade380SMichael Holzheu 		kfree((void *)(addr_t) ptr->cda);
2171eade380SMichael Holzheu 		ptr++;
2181eade380SMichael Holzheu 	}
2191eade380SMichael Holzheu 	kfree(cpa);
2201eade380SMichael Holzheu }
221810cb5b3SFrank Munzert 
222810cb5b3SFrank Munzert /*
223810cb5b3SFrank Munzert  * alloc_chan_prog
224810cb5b3SFrank Munzert  * The channel program we use is write commands chained together
225810cb5b3SFrank Munzert  * with a final NOP CCW command-chained on (which ensures that CE and DE
226810cb5b3SFrank Munzert  * are presented together in a single interrupt instead of as separate
227810cb5b3SFrank Munzert  * interrupts unless an incorrect length indication kicks in first). The
2281eade380SMichael Holzheu  * data length in each CCW is reclen.
229810cb5b3SFrank Munzert  */
2301eade380SMichael Holzheu static struct ccw1 *alloc_chan_prog(const char __user *ubuf, int rec_count,
2311eade380SMichael Holzheu 				    int reclen)
232810cb5b3SFrank Munzert {
233810cb5b3SFrank Munzert 	struct ccw1 *cpa;
2341eade380SMichael Holzheu 	void *kbuf;
235810cb5b3SFrank Munzert 	int i;
236810cb5b3SFrank Munzert 
2371eade380SMichael Holzheu 	TRACE("alloc_chan_prog(%p, %i, %i)\n", ubuf, rec_count, reclen);
238810cb5b3SFrank Munzert 
239810cb5b3SFrank Munzert 	/*
240810cb5b3SFrank Munzert 	 * We chain a NOP onto the writes to force CE+DE together.
241810cb5b3SFrank Munzert 	 * That means we allocate room for CCWs to cover count/reclen
242810cb5b3SFrank Munzert 	 * records plus a NOP.
243810cb5b3SFrank Munzert 	 */
2441eade380SMichael Holzheu 	cpa = kzalloc((rec_count + 1) * sizeof(struct ccw1),
2451eade380SMichael Holzheu 		      GFP_KERNEL | GFP_DMA);
246810cb5b3SFrank Munzert 	if (!cpa)
2471eade380SMichael Holzheu 		return ERR_PTR(-ENOMEM);
248810cb5b3SFrank Munzert 
2491eade380SMichael Holzheu 	for (i = 0; i < rec_count; i++) {
250810cb5b3SFrank Munzert 		cpa[i].cmd_code = WRITE_CCW_CMD;
251810cb5b3SFrank Munzert 		cpa[i].flags = CCW_FLAG_CC | CCW_FLAG_SLI;
252810cb5b3SFrank Munzert 		cpa[i].count = reclen;
2531eade380SMichael Holzheu 		kbuf = kmalloc(reclen, GFP_KERNEL | GFP_DMA);
2541eade380SMichael Holzheu 		if (!kbuf) {
2551eade380SMichael Holzheu 			free_chan_prog(cpa);
2561eade380SMichael Holzheu 			return ERR_PTR(-ENOMEM);
2571eade380SMichael Holzheu 		}
2581eade380SMichael Holzheu 		cpa[i].cda = (u32)(addr_t) kbuf;
2591eade380SMichael Holzheu 		if (copy_from_user(kbuf, ubuf, reclen)) {
2601eade380SMichael Holzheu 			free_chan_prog(cpa);
2611eade380SMichael Holzheu 			return ERR_PTR(-EFAULT);
2621eade380SMichael Holzheu 		}
2631eade380SMichael Holzheu 		ubuf += reclen;
264810cb5b3SFrank Munzert 	}
265810cb5b3SFrank Munzert 	/* The following NOP CCW forces CE+DE to be presented together */
266810cb5b3SFrank Munzert 	cpa[i].cmd_code = CCW_CMD_NOOP;
267810cb5b3SFrank Munzert 	return cpa;
268810cb5b3SFrank Munzert }
269810cb5b3SFrank Munzert 
270810cb5b3SFrank Munzert static int do_ur_io(struct urdev *urd, struct ccw1 *cpa)
271810cb5b3SFrank Munzert {
272810cb5b3SFrank Munzert 	int rc;
273810cb5b3SFrank Munzert 	struct ccw_device *cdev = urd->cdev;
274278bc68cSHeiko Carstens 	DECLARE_COMPLETION_ONSTACK(event);
275810cb5b3SFrank Munzert 
276810cb5b3SFrank Munzert 	TRACE("do_ur_io: cpa=%p\n", cpa);
277810cb5b3SFrank Munzert 
278810cb5b3SFrank Munzert 	rc = mutex_lock_interruptible(&urd->io_mutex);
279810cb5b3SFrank Munzert 	if (rc)
280810cb5b3SFrank Munzert 		return rc;
281810cb5b3SFrank Munzert 
282810cb5b3SFrank Munzert 	urd->io_done = &event;
283810cb5b3SFrank Munzert 
284810cb5b3SFrank Munzert 	spin_lock_irq(get_ccwdev_lock(cdev));
285810cb5b3SFrank Munzert 	rc = ccw_device_start(cdev, cpa, 1, 0, 0);
286810cb5b3SFrank Munzert 	spin_unlock_irq(get_ccwdev_lock(cdev));
287810cb5b3SFrank Munzert 
288810cb5b3SFrank Munzert 	TRACE("do_ur_io: ccw_device_start returned %d\n", rc);
289810cb5b3SFrank Munzert 	if (rc)
290810cb5b3SFrank Munzert 		goto out;
291810cb5b3SFrank Munzert 
292810cb5b3SFrank Munzert 	wait_for_completion(&event);
293810cb5b3SFrank Munzert 	TRACE("do_ur_io: I/O complete\n");
294810cb5b3SFrank Munzert 	rc = 0;
295810cb5b3SFrank Munzert 
296810cb5b3SFrank Munzert out:
297810cb5b3SFrank Munzert 	mutex_unlock(&urd->io_mutex);
298810cb5b3SFrank Munzert 	return rc;
299810cb5b3SFrank Munzert }
300810cb5b3SFrank Munzert 
301810cb5b3SFrank Munzert /*
302810cb5b3SFrank Munzert  * ur interrupt handler, called from the ccw_device layer
303810cb5b3SFrank Munzert  */
304810cb5b3SFrank Munzert static void ur_int_handler(struct ccw_device *cdev, unsigned long intparm,
305810cb5b3SFrank Munzert 			   struct irb *irb)
306810cb5b3SFrank Munzert {
307810cb5b3SFrank Munzert 	struct urdev *urd;
308810cb5b3SFrank Munzert 
309810cb5b3SFrank Munzert 	TRACE("ur_int_handler: intparm=0x%lx cstat=%02x dstat=%02x res=%u\n",
31023d805b6SPeter Oberparleiter 	      intparm, irb->scsw.cmd.cstat, irb->scsw.cmd.dstat,
31123d805b6SPeter Oberparleiter 	      irb->scsw.cmd.count);
312810cb5b3SFrank Munzert 
313810cb5b3SFrank Munzert 	if (!intparm) {
314810cb5b3SFrank Munzert 		TRACE("ur_int_handler: unsolicited interrupt\n");
315810cb5b3SFrank Munzert 		return;
316810cb5b3SFrank Munzert 	}
317dff59b64SGreg Kroah-Hartman 	urd = dev_get_drvdata(&cdev->dev);
3188127a1f8SMichael Holzheu 	BUG_ON(!urd);
319810cb5b3SFrank Munzert 	/* On special conditions irb is an error pointer */
320810cb5b3SFrank Munzert 	if (IS_ERR(irb))
321810cb5b3SFrank Munzert 		urd->io_request_rc = PTR_ERR(irb);
32223d805b6SPeter Oberparleiter 	else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END))
323810cb5b3SFrank Munzert 		urd->io_request_rc = 0;
324810cb5b3SFrank Munzert 	else
325810cb5b3SFrank Munzert 		urd->io_request_rc = -EIO;
326810cb5b3SFrank Munzert 
327810cb5b3SFrank Munzert 	complete(urd->io_done);
328810cb5b3SFrank Munzert }
329810cb5b3SFrank Munzert 
330810cb5b3SFrank Munzert /*
331810cb5b3SFrank Munzert  * reclen sysfs attribute - The record length to be used for write CCWs
332810cb5b3SFrank Munzert  */
333810cb5b3SFrank Munzert static ssize_t ur_attr_reclen_show(struct device *dev,
334810cb5b3SFrank Munzert 				   struct device_attribute *attr, char *buf)
335810cb5b3SFrank Munzert {
3368127a1f8SMichael Holzheu 	struct urdev *urd;
3378127a1f8SMichael Holzheu 	int rc;
338810cb5b3SFrank Munzert 
3398127a1f8SMichael Holzheu 	urd = urdev_get_from_cdev(to_ccwdev(dev));
3408127a1f8SMichael Holzheu 	if (!urd)
3418127a1f8SMichael Holzheu 		return -ENODEV;
3428127a1f8SMichael Holzheu 	rc = sprintf(buf, "%zu\n", urd->reclen);
3438127a1f8SMichael Holzheu 	urdev_put(urd);
3448127a1f8SMichael Holzheu 	return rc;
345810cb5b3SFrank Munzert }
346810cb5b3SFrank Munzert 
347810cb5b3SFrank Munzert static DEVICE_ATTR(reclen, 0444, ur_attr_reclen_show, NULL);
348810cb5b3SFrank Munzert 
349810cb5b3SFrank Munzert static int ur_create_attributes(struct device *dev)
350810cb5b3SFrank Munzert {
351810cb5b3SFrank Munzert 	return device_create_file(dev, &dev_attr_reclen);
352810cb5b3SFrank Munzert }
353810cb5b3SFrank Munzert 
354810cb5b3SFrank Munzert static void ur_remove_attributes(struct device *dev)
355810cb5b3SFrank Munzert {
356810cb5b3SFrank Munzert 	device_remove_file(dev, &dev_attr_reclen);
357810cb5b3SFrank Munzert }
358810cb5b3SFrank Munzert 
359810cb5b3SFrank Munzert /*
360810cb5b3SFrank Munzert  * diagnose code 0x210 - retrieve device information
361810cb5b3SFrank Munzert  * cc=0  normal completion, we have a real device
362810cb5b3SFrank Munzert  * cc=1  CP paging error
363810cb5b3SFrank Munzert  * cc=2  The virtual device exists, but is not associated with a real device
364810cb5b3SFrank Munzert  * cc=3  Invalid device address, or the virtual device does not exist
365810cb5b3SFrank Munzert  */
366810cb5b3SFrank Munzert static int get_urd_class(struct urdev *urd)
367810cb5b3SFrank Munzert {
368810cb5b3SFrank Munzert 	static struct diag210 ur_diag210;
369810cb5b3SFrank Munzert 	int cc;
370810cb5b3SFrank Munzert 
371810cb5b3SFrank Munzert 	ur_diag210.vrdcdvno = urd->dev_id.devno;
372810cb5b3SFrank Munzert 	ur_diag210.vrdclen = sizeof(struct diag210);
373810cb5b3SFrank Munzert 
374810cb5b3SFrank Munzert 	cc = diag210(&ur_diag210);
375810cb5b3SFrank Munzert 	switch (cc) {
376810cb5b3SFrank Munzert 	case 0:
377b9993a38SFrank Munzert 		return -EOPNOTSUPP;
378810cb5b3SFrank Munzert 	case 2:
379810cb5b3SFrank Munzert 		return ur_diag210.vrdcvcla; /* virtual device class */
380810cb5b3SFrank Munzert 	case 3:
381810cb5b3SFrank Munzert 		return -ENODEV;
382810cb5b3SFrank Munzert 	default:
383810cb5b3SFrank Munzert 		return -EIO;
384810cb5b3SFrank Munzert 	}
385810cb5b3SFrank Munzert }
386810cb5b3SFrank Munzert 
387810cb5b3SFrank Munzert /*
388810cb5b3SFrank Munzert  * Allocation and freeing of urfile structures
389810cb5b3SFrank Munzert  */
390810cb5b3SFrank Munzert static struct urfile *urfile_alloc(struct urdev *urd)
391810cb5b3SFrank Munzert {
392810cb5b3SFrank Munzert 	struct urfile *urf;
393810cb5b3SFrank Munzert 
394810cb5b3SFrank Munzert 	urf = kzalloc(sizeof(struct urfile), GFP_KERNEL);
395810cb5b3SFrank Munzert 	if (!urf)
396810cb5b3SFrank Munzert 		return NULL;
397810cb5b3SFrank Munzert 	urf->urd = urd;
398810cb5b3SFrank Munzert 
399810cb5b3SFrank Munzert 	TRACE("urfile_alloc: urd=%p urf=%p rl=%zu\n", urd, urf,
400810cb5b3SFrank Munzert 	      urf->dev_reclen);
401810cb5b3SFrank Munzert 
402810cb5b3SFrank Munzert 	return urf;
403810cb5b3SFrank Munzert }
404810cb5b3SFrank Munzert 
405810cb5b3SFrank Munzert static void urfile_free(struct urfile *urf)
406810cb5b3SFrank Munzert {
407810cb5b3SFrank Munzert 	TRACE("urfile_free: urf=%p urd=%p\n", urf, urf->urd);
408810cb5b3SFrank Munzert 	kfree(urf);
409810cb5b3SFrank Munzert }
410810cb5b3SFrank Munzert 
411810cb5b3SFrank Munzert /*
412810cb5b3SFrank Munzert  * The fops implementation of the character device driver
413810cb5b3SFrank Munzert  */
414810cb5b3SFrank Munzert static ssize_t do_write(struct urdev *urd, const char __user *udata,
415810cb5b3SFrank Munzert 			size_t count, size_t reclen, loff_t *ppos)
416810cb5b3SFrank Munzert {
417810cb5b3SFrank Munzert 	struct ccw1 *cpa;
418810cb5b3SFrank Munzert 	int rc;
419810cb5b3SFrank Munzert 
4201eade380SMichael Holzheu 	cpa = alloc_chan_prog(udata, count / reclen, reclen);
4211eade380SMichael Holzheu 	if (IS_ERR(cpa))
4221eade380SMichael Holzheu 		return PTR_ERR(cpa);
423810cb5b3SFrank Munzert 
424810cb5b3SFrank Munzert 	rc = do_ur_io(urd, cpa);
425810cb5b3SFrank Munzert 	if (rc)
426810cb5b3SFrank Munzert 		goto fail_kfree_cpa;
427810cb5b3SFrank Munzert 
428810cb5b3SFrank Munzert 	if (urd->io_request_rc) {
429810cb5b3SFrank Munzert 		rc = urd->io_request_rc;
430810cb5b3SFrank Munzert 		goto fail_kfree_cpa;
431810cb5b3SFrank Munzert 	}
432810cb5b3SFrank Munzert 	*ppos += count;
433810cb5b3SFrank Munzert 	rc = count;
4341eade380SMichael Holzheu 
435810cb5b3SFrank Munzert fail_kfree_cpa:
4361eade380SMichael Holzheu 	free_chan_prog(cpa);
437810cb5b3SFrank Munzert 	return rc;
438810cb5b3SFrank Munzert }
439810cb5b3SFrank Munzert 
440810cb5b3SFrank Munzert static ssize_t ur_write(struct file *file, const char __user *udata,
441810cb5b3SFrank Munzert 			size_t count, loff_t *ppos)
442810cb5b3SFrank Munzert {
443810cb5b3SFrank Munzert 	struct urfile *urf = file->private_data;
444810cb5b3SFrank Munzert 
445810cb5b3SFrank Munzert 	TRACE("ur_write: count=%zu\n", count);
446810cb5b3SFrank Munzert 
447810cb5b3SFrank Munzert 	if (count == 0)
448810cb5b3SFrank Munzert 		return 0;
449810cb5b3SFrank Munzert 
450810cb5b3SFrank Munzert 	if (count % urf->dev_reclen)
451810cb5b3SFrank Munzert 		return -EINVAL;	/* count must be a multiple of reclen */
452810cb5b3SFrank Munzert 
453810cb5b3SFrank Munzert 	if (count > urf->dev_reclen * MAX_RECS_PER_IO)
454810cb5b3SFrank Munzert 		count = urf->dev_reclen * MAX_RECS_PER_IO;
455810cb5b3SFrank Munzert 
456810cb5b3SFrank Munzert 	return do_write(urf->urd, udata, count, urf->dev_reclen, ppos);
457810cb5b3SFrank Munzert }
458810cb5b3SFrank Munzert 
459810cb5b3SFrank Munzert /*
460810cb5b3SFrank Munzert  * diagnose code 0x14 subcode 0x0028 - position spool file to designated
461810cb5b3SFrank Munzert  *				       record
462810cb5b3SFrank Munzert  * cc=0  normal completion
463810cb5b3SFrank Munzert  * cc=2  no file active on the virtual reader or device not ready
464810cb5b3SFrank Munzert  * cc=3  record specified is beyond EOF
465810cb5b3SFrank Munzert  */
466810cb5b3SFrank Munzert static int diag_position_to_record(int devno, int record)
467810cb5b3SFrank Munzert {
468810cb5b3SFrank Munzert 	int cc;
469810cb5b3SFrank Munzert 
4700a87c5cfSMichael Holzheu 	cc = diag14(record, devno, 0x28);
471810cb5b3SFrank Munzert 	switch (cc) {
472810cb5b3SFrank Munzert 	case 0:
473810cb5b3SFrank Munzert 		return 0;
474810cb5b3SFrank Munzert 	case 2:
475810cb5b3SFrank Munzert 		return -ENOMEDIUM;
476810cb5b3SFrank Munzert 	case 3:
477810cb5b3SFrank Munzert 		return -ENODATA; /* position beyond end of file */
478810cb5b3SFrank Munzert 	default:
479810cb5b3SFrank Munzert 		return -EIO;
480810cb5b3SFrank Munzert 	}
481810cb5b3SFrank Munzert }
482810cb5b3SFrank Munzert 
483810cb5b3SFrank Munzert /*
484810cb5b3SFrank Munzert  * diagnose code 0x14 subcode 0x0000 - read next spool file buffer
485810cb5b3SFrank Munzert  * cc=0  normal completion
486810cb5b3SFrank Munzert  * cc=1  EOF reached
487810cb5b3SFrank Munzert  * cc=2  no file active on the virtual reader, and no file eligible
488810cb5b3SFrank Munzert  * cc=3  file already active on the virtual reader or specified virtual
489810cb5b3SFrank Munzert  *	 reader does not exist or is not a reader
490810cb5b3SFrank Munzert  */
491810cb5b3SFrank Munzert static int diag_read_file(int devno, char *buf)
492810cb5b3SFrank Munzert {
493810cb5b3SFrank Munzert 	int cc;
494810cb5b3SFrank Munzert 
4950a87c5cfSMichael Holzheu 	cc = diag14((unsigned long) buf, devno, 0x00);
496810cb5b3SFrank Munzert 	switch (cc) {
497810cb5b3SFrank Munzert 	case 0:
498810cb5b3SFrank Munzert 		return 0;
499810cb5b3SFrank Munzert 	case 1:
500810cb5b3SFrank Munzert 		return -ENODATA;
501810cb5b3SFrank Munzert 	case 2:
502810cb5b3SFrank Munzert 		return -ENOMEDIUM;
503810cb5b3SFrank Munzert 	default:
504810cb5b3SFrank Munzert 		return -EIO;
505810cb5b3SFrank Munzert 	}
506810cb5b3SFrank Munzert }
507810cb5b3SFrank Munzert 
508810cb5b3SFrank Munzert static ssize_t diag14_read(struct file *file, char __user *ubuf, size_t count,
509810cb5b3SFrank Munzert 			   loff_t *offs)
510810cb5b3SFrank Munzert {
511810cb5b3SFrank Munzert 	size_t len, copied, res;
512810cb5b3SFrank Munzert 	char *buf;
513810cb5b3SFrank Munzert 	int rc;
514810cb5b3SFrank Munzert 	u16 reclen;
515810cb5b3SFrank Munzert 	struct urdev *urd;
516810cb5b3SFrank Munzert 
517810cb5b3SFrank Munzert 	urd = ((struct urfile *) file->private_data)->urd;
518810cb5b3SFrank Munzert 	reclen = ((struct urfile *) file->private_data)->file_reclen;
519810cb5b3SFrank Munzert 
520810cb5b3SFrank Munzert 	rc = diag_position_to_record(urd->dev_id.devno, *offs / PAGE_SIZE + 1);
521810cb5b3SFrank Munzert 	if (rc == -ENODATA)
522810cb5b3SFrank Munzert 		return 0;
523810cb5b3SFrank Munzert 	if (rc)
524810cb5b3SFrank Munzert 		return rc;
525810cb5b3SFrank Munzert 
526810cb5b3SFrank Munzert 	len = min((size_t) PAGE_SIZE, count);
5273eed13ccSMichael Holzheu 	buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA);
528810cb5b3SFrank Munzert 	if (!buf)
529810cb5b3SFrank Munzert 		return -ENOMEM;
530810cb5b3SFrank Munzert 
531810cb5b3SFrank Munzert 	copied = 0;
532810cb5b3SFrank Munzert 	res = (size_t) (*offs % PAGE_SIZE);
533810cb5b3SFrank Munzert 	do {
534810cb5b3SFrank Munzert 		rc = diag_read_file(urd->dev_id.devno, buf);
535810cb5b3SFrank Munzert 		if (rc == -ENODATA) {
536810cb5b3SFrank Munzert 			break;
537810cb5b3SFrank Munzert 		}
538810cb5b3SFrank Munzert 		if (rc)
539810cb5b3SFrank Munzert 			goto fail;
5402b3d8c9eSFrank Munzert 		if (reclen && (copied == 0) && (*offs < PAGE_SIZE))
541810cb5b3SFrank Munzert 			*((u16 *) &buf[FILE_RECLEN_OFFSET]) = reclen;
542810cb5b3SFrank Munzert 		len = min(count - copied, PAGE_SIZE - res);
543810cb5b3SFrank Munzert 		if (copy_to_user(ubuf + copied, buf + res, len)) {
544810cb5b3SFrank Munzert 			rc = -EFAULT;
545810cb5b3SFrank Munzert 			goto fail;
546810cb5b3SFrank Munzert 		}
547810cb5b3SFrank Munzert 		res = 0;
548810cb5b3SFrank Munzert 		copied += len;
549810cb5b3SFrank Munzert 	} while (copied != count);
550810cb5b3SFrank Munzert 
551810cb5b3SFrank Munzert 	*offs += copied;
552810cb5b3SFrank Munzert 	rc = copied;
553810cb5b3SFrank Munzert fail:
5543eed13ccSMichael Holzheu 	free_page((unsigned long) buf);
555810cb5b3SFrank Munzert 	return rc;
556810cb5b3SFrank Munzert }
557810cb5b3SFrank Munzert 
558810cb5b3SFrank Munzert static ssize_t ur_read(struct file *file, char __user *ubuf, size_t count,
559810cb5b3SFrank Munzert 		       loff_t *offs)
560810cb5b3SFrank Munzert {
561810cb5b3SFrank Munzert 	struct urdev *urd;
562810cb5b3SFrank Munzert 	int rc;
563810cb5b3SFrank Munzert 
564810cb5b3SFrank Munzert 	TRACE("ur_read: count=%zu ppos=%li\n", count, (unsigned long) *offs);
565810cb5b3SFrank Munzert 
566810cb5b3SFrank Munzert 	if (count == 0)
567810cb5b3SFrank Munzert 		return 0;
568810cb5b3SFrank Munzert 
569810cb5b3SFrank Munzert 	urd = ((struct urfile *) file->private_data)->urd;
570810cb5b3SFrank Munzert 	rc = mutex_lock_interruptible(&urd->io_mutex);
571810cb5b3SFrank Munzert 	if (rc)
572810cb5b3SFrank Munzert 		return rc;
573810cb5b3SFrank Munzert 	rc = diag14_read(file, ubuf, count, offs);
574810cb5b3SFrank Munzert 	mutex_unlock(&urd->io_mutex);
575810cb5b3SFrank Munzert 	return rc;
576810cb5b3SFrank Munzert }
577810cb5b3SFrank Munzert 
578810cb5b3SFrank Munzert /*
579810cb5b3SFrank Munzert  * diagnose code 0x14 subcode 0x0fff - retrieve next file descriptor
580810cb5b3SFrank Munzert  * cc=0  normal completion
581810cb5b3SFrank Munzert  * cc=1  no files on reader queue or no subsequent file
582810cb5b3SFrank Munzert  * cc=2  spid specified is invalid
583810cb5b3SFrank Munzert  */
584810cb5b3SFrank Munzert static int diag_read_next_file_info(struct file_control_block *buf, int spid)
585810cb5b3SFrank Munzert {
586810cb5b3SFrank Munzert 	int cc;
587810cb5b3SFrank Munzert 
5880a87c5cfSMichael Holzheu 	cc = diag14((unsigned long) buf, spid, 0xfff);
589810cb5b3SFrank Munzert 	switch (cc) {
590810cb5b3SFrank Munzert 	case 0:
591810cb5b3SFrank Munzert 		return 0;
592810cb5b3SFrank Munzert 	default:
593810cb5b3SFrank Munzert 		return -ENODATA;
594810cb5b3SFrank Munzert 	}
595810cb5b3SFrank Munzert }
596810cb5b3SFrank Munzert 
5973eed13ccSMichael Holzheu static int verify_uri_device(struct urdev *urd)
598810cb5b3SFrank Munzert {
5993eed13ccSMichael Holzheu 	struct file_control_block *fcb;
600810cb5b3SFrank Munzert 	char *buf;
601810cb5b3SFrank Munzert 	int rc;
602810cb5b3SFrank Munzert 
6033eed13ccSMichael Holzheu 	fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA);
6043eed13ccSMichael Holzheu 	if (!fcb)
6053eed13ccSMichael Holzheu 		return -ENOMEM;
6063eed13ccSMichael Holzheu 
6073eed13ccSMichael Holzheu 	/* check for empty reader device (beginning of chain) */
6083eed13ccSMichael Holzheu 	rc = diag_read_next_file_info(fcb, 0);
6093eed13ccSMichael Holzheu 	if (rc)
6103eed13ccSMichael Holzheu 		goto fail_free_fcb;
6113eed13ccSMichael Holzheu 
6123eed13ccSMichael Holzheu 	/* if file is in hold status, we do not read it */
6133eed13ccSMichael Holzheu 	if (fcb->file_stat & (FLG_SYSTEM_HOLD | FLG_USER_HOLD)) {
6143eed13ccSMichael Holzheu 		rc = -EPERM;
6153eed13ccSMichael Holzheu 		goto fail_free_fcb;
6163eed13ccSMichael Holzheu 	}
6173eed13ccSMichael Holzheu 
6183eed13ccSMichael Holzheu 	/* open file on virtual reader	*/
6193eed13ccSMichael Holzheu 	buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA);
6203eed13ccSMichael Holzheu 	if (!buf) {
6213eed13ccSMichael Holzheu 		rc = -ENOMEM;
6223eed13ccSMichael Holzheu 		goto fail_free_fcb;
6233eed13ccSMichael Holzheu 	}
6243eed13ccSMichael Holzheu 	rc = diag_read_file(urd->dev_id.devno, buf);
6253eed13ccSMichael Holzheu 	if ((rc != 0) && (rc != -ENODATA)) /* EOF does not hurt */
6263eed13ccSMichael Holzheu 		goto fail_free_buf;
6273eed13ccSMichael Holzheu 
6283eed13ccSMichael Holzheu 	/* check if the file on top of the queue is open now */
6293eed13ccSMichael Holzheu 	rc = diag_read_next_file_info(fcb, 0);
6303eed13ccSMichael Holzheu 	if (rc)
6313eed13ccSMichael Holzheu 		goto fail_free_buf;
6323eed13ccSMichael Holzheu 	if (!(fcb->file_stat & FLG_IN_USE)) {
6333eed13ccSMichael Holzheu 		rc = -EMFILE;
6343eed13ccSMichael Holzheu 		goto fail_free_buf;
6353eed13ccSMichael Holzheu 	}
6363eed13ccSMichael Holzheu 	rc = 0;
6373eed13ccSMichael Holzheu 
6383eed13ccSMichael Holzheu fail_free_buf:
6393eed13ccSMichael Holzheu 	free_page((unsigned long) buf);
6403eed13ccSMichael Holzheu fail_free_fcb:
6413eed13ccSMichael Holzheu 	kfree(fcb);
6423eed13ccSMichael Holzheu 	return rc;
6433eed13ccSMichael Holzheu }
6443eed13ccSMichael Holzheu 
6453eed13ccSMichael Holzheu static int verify_device(struct urdev *urd)
6463eed13ccSMichael Holzheu {
647810cb5b3SFrank Munzert 	switch (urd->class) {
648810cb5b3SFrank Munzert 	case DEV_CLASS_UR_O:
649810cb5b3SFrank Munzert 		return 0; /* no check needed here */
650810cb5b3SFrank Munzert 	case DEV_CLASS_UR_I:
6513eed13ccSMichael Holzheu 		return verify_uri_device(urd);
652810cb5b3SFrank Munzert 	default:
653b9993a38SFrank Munzert 		return -EOPNOTSUPP;
654810cb5b3SFrank Munzert 	}
655810cb5b3SFrank Munzert }
656810cb5b3SFrank Munzert 
6573eed13ccSMichael Holzheu static int get_uri_file_reclen(struct urdev *urd)
6583eed13ccSMichael Holzheu {
6593eed13ccSMichael Holzheu 	struct file_control_block *fcb;
6603eed13ccSMichael Holzheu 	int rc;
6613eed13ccSMichael Holzheu 
6623eed13ccSMichael Holzheu 	fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA);
6633eed13ccSMichael Holzheu 	if (!fcb)
6643eed13ccSMichael Holzheu 		return -ENOMEM;
6653eed13ccSMichael Holzheu 	rc = diag_read_next_file_info(fcb, 0);
6663eed13ccSMichael Holzheu 	if (rc)
6673eed13ccSMichael Holzheu 		goto fail_free;
6683eed13ccSMichael Holzheu 	if (fcb->file_stat & FLG_CP_DUMP)
6693eed13ccSMichael Holzheu 		rc = 0;
6703eed13ccSMichael Holzheu 	else
6713eed13ccSMichael Holzheu 		rc = fcb->rec_len;
6723eed13ccSMichael Holzheu 
6733eed13ccSMichael Holzheu fail_free:
6743eed13ccSMichael Holzheu 	kfree(fcb);
6753eed13ccSMichael Holzheu 	return rc;
6763eed13ccSMichael Holzheu }
6773eed13ccSMichael Holzheu 
678810cb5b3SFrank Munzert static int get_file_reclen(struct urdev *urd)
679810cb5b3SFrank Munzert {
680810cb5b3SFrank Munzert 	switch (urd->class) {
681810cb5b3SFrank Munzert 	case DEV_CLASS_UR_O:
682810cb5b3SFrank Munzert 		return 0;
683810cb5b3SFrank Munzert 	case DEV_CLASS_UR_I:
6843eed13ccSMichael Holzheu 		return get_uri_file_reclen(urd);
685810cb5b3SFrank Munzert 	default:
686b9993a38SFrank Munzert 		return -EOPNOTSUPP;
687810cb5b3SFrank Munzert 	}
688810cb5b3SFrank Munzert }
689810cb5b3SFrank Munzert 
690810cb5b3SFrank Munzert static int ur_open(struct inode *inode, struct file *file)
691810cb5b3SFrank Munzert {
692810cb5b3SFrank Munzert 	u16 devno;
693810cb5b3SFrank Munzert 	struct urdev *urd;
694810cb5b3SFrank Munzert 	struct urfile *urf;
695810cb5b3SFrank Munzert 	unsigned short accmode;
696810cb5b3SFrank Munzert 	int rc;
697810cb5b3SFrank Munzert 
698810cb5b3SFrank Munzert 	accmode = file->f_flags & O_ACCMODE;
699810cb5b3SFrank Munzert 
700810cb5b3SFrank Munzert 	if (accmode == O_RDWR)
701810cb5b3SFrank Munzert 		return -EACCES;
702810cb5b3SFrank Munzert 	/*
703810cb5b3SFrank Munzert 	 * We treat the minor number as the devno of the ur device
704810cb5b3SFrank Munzert 	 * to find in the driver tree.
705810cb5b3SFrank Munzert 	 */
706810cb5b3SFrank Munzert 	devno = MINOR(file->f_dentry->d_inode->i_rdev);
707810cb5b3SFrank Munzert 
708810cb5b3SFrank Munzert 	urd = urdev_get_from_devno(devno);
709764a4a8eSJonathan Corbet 	if (!urd) {
710764a4a8eSJonathan Corbet 		rc = -ENXIO;
711764a4a8eSJonathan Corbet 		goto out;
712764a4a8eSJonathan Corbet 	}
713810cb5b3SFrank Munzert 
714a695f167SFrank Munzert 	spin_lock(&urd->open_lock);
715a695f167SFrank Munzert 	while (urd->open_flag) {
716a695f167SFrank Munzert 		spin_unlock(&urd->open_lock);
717810cb5b3SFrank Munzert 		if (file->f_flags & O_NONBLOCK) {
718810cb5b3SFrank Munzert 			rc = -EBUSY;
719810cb5b3SFrank Munzert 			goto fail_put;
720810cb5b3SFrank Munzert 		}
721a695f167SFrank Munzert 		if (wait_event_interruptible(urd->wait, urd->open_flag == 0)) {
722810cb5b3SFrank Munzert 			rc = -ERESTARTSYS;
723810cb5b3SFrank Munzert 			goto fail_put;
724810cb5b3SFrank Munzert 		}
725a695f167SFrank Munzert 		spin_lock(&urd->open_lock);
726810cb5b3SFrank Munzert 	}
727a695f167SFrank Munzert 	urd->open_flag++;
728a695f167SFrank Munzert 	spin_unlock(&urd->open_lock);
729810cb5b3SFrank Munzert 
730810cb5b3SFrank Munzert 	TRACE("ur_open\n");
731810cb5b3SFrank Munzert 
732810cb5b3SFrank Munzert 	if (((accmode == O_RDONLY) && (urd->class != DEV_CLASS_UR_I)) ||
733810cb5b3SFrank Munzert 	    ((accmode == O_WRONLY) && (urd->class != DEV_CLASS_UR_O))) {
734810cb5b3SFrank Munzert 		TRACE("ur_open: unsupported dev class (%d)\n", urd->class);
735810cb5b3SFrank Munzert 		rc = -EACCES;
736810cb5b3SFrank Munzert 		goto fail_unlock;
737810cb5b3SFrank Munzert 	}
738810cb5b3SFrank Munzert 
739810cb5b3SFrank Munzert 	rc = verify_device(urd);
740810cb5b3SFrank Munzert 	if (rc)
741810cb5b3SFrank Munzert 		goto fail_unlock;
742810cb5b3SFrank Munzert 
743810cb5b3SFrank Munzert 	urf = urfile_alloc(urd);
744810cb5b3SFrank Munzert 	if (!urf) {
745810cb5b3SFrank Munzert 		rc = -ENOMEM;
746810cb5b3SFrank Munzert 		goto fail_unlock;
747810cb5b3SFrank Munzert 	}
748810cb5b3SFrank Munzert 
749810cb5b3SFrank Munzert 	urf->dev_reclen = urd->reclen;
750810cb5b3SFrank Munzert 	rc = get_file_reclen(urd);
751810cb5b3SFrank Munzert 	if (rc < 0)
752810cb5b3SFrank Munzert 		goto fail_urfile_free;
753810cb5b3SFrank Munzert 	urf->file_reclen = rc;
754810cb5b3SFrank Munzert 	file->private_data = urf;
755810cb5b3SFrank Munzert 	return 0;
756810cb5b3SFrank Munzert 
757810cb5b3SFrank Munzert fail_urfile_free:
758810cb5b3SFrank Munzert 	urfile_free(urf);
759810cb5b3SFrank Munzert fail_unlock:
760a695f167SFrank Munzert 	spin_lock(&urd->open_lock);
761a695f167SFrank Munzert 	urd->open_flag--;
762a695f167SFrank Munzert 	spin_unlock(&urd->open_lock);
763810cb5b3SFrank Munzert fail_put:
764810cb5b3SFrank Munzert 	urdev_put(urd);
765764a4a8eSJonathan Corbet out:
766810cb5b3SFrank Munzert 	return rc;
767810cb5b3SFrank Munzert }
768810cb5b3SFrank Munzert 
769810cb5b3SFrank Munzert static int ur_release(struct inode *inode, struct file *file)
770810cb5b3SFrank Munzert {
771810cb5b3SFrank Munzert 	struct urfile *urf = file->private_data;
772810cb5b3SFrank Munzert 
773810cb5b3SFrank Munzert 	TRACE("ur_release\n");
774a695f167SFrank Munzert 	spin_lock(&urf->urd->open_lock);
775a695f167SFrank Munzert 	urf->urd->open_flag--;
776a695f167SFrank Munzert 	spin_unlock(&urf->urd->open_lock);
777a695f167SFrank Munzert 	wake_up_interruptible(&urf->urd->wait);
778810cb5b3SFrank Munzert 	urdev_put(urf->urd);
779810cb5b3SFrank Munzert 	urfile_free(urf);
780810cb5b3SFrank Munzert 	return 0;
781810cb5b3SFrank Munzert }
782810cb5b3SFrank Munzert 
783810cb5b3SFrank Munzert static loff_t ur_llseek(struct file *file, loff_t offset, int whence)
784810cb5b3SFrank Munzert {
785810cb5b3SFrank Munzert 	loff_t newpos;
786810cb5b3SFrank Munzert 
787810cb5b3SFrank Munzert 	if ((file->f_flags & O_ACCMODE) != O_RDONLY)
788810cb5b3SFrank Munzert 		return -ESPIPE; /* seek allowed only for reader */
789810cb5b3SFrank Munzert 	if (offset % PAGE_SIZE)
790810cb5b3SFrank Munzert 		return -ESPIPE; /* only multiples of 4K allowed */
791810cb5b3SFrank Munzert 	switch (whence) {
792810cb5b3SFrank Munzert 	case 0: /* SEEK_SET */
793810cb5b3SFrank Munzert 		newpos = offset;
794810cb5b3SFrank Munzert 		break;
795810cb5b3SFrank Munzert 	case 1: /* SEEK_CUR */
796810cb5b3SFrank Munzert 		newpos = file->f_pos + offset;
797810cb5b3SFrank Munzert 		break;
798810cb5b3SFrank Munzert 	default:
799810cb5b3SFrank Munzert 		return -EINVAL;
800810cb5b3SFrank Munzert 	}
801810cb5b3SFrank Munzert 	file->f_pos = newpos;
802810cb5b3SFrank Munzert 	return newpos;
803810cb5b3SFrank Munzert }
804810cb5b3SFrank Munzert 
8055c81cdbeSJan Engelhardt static const struct file_operations ur_fops = {
806810cb5b3SFrank Munzert 	.owner	 = THIS_MODULE,
807810cb5b3SFrank Munzert 	.open	 = ur_open,
808810cb5b3SFrank Munzert 	.release = ur_release,
809810cb5b3SFrank Munzert 	.read	 = ur_read,
810810cb5b3SFrank Munzert 	.write	 = ur_write,
811810cb5b3SFrank Munzert 	.llseek  = ur_llseek,
812810cb5b3SFrank Munzert };
813810cb5b3SFrank Munzert 
814810cb5b3SFrank Munzert /*
815810cb5b3SFrank Munzert  * ccw_device infrastructure:
8168127a1f8SMichael Holzheu  *     ur_probe creates the struct urdev (with refcount = 1), the device
8178127a1f8SMichael Holzheu  *     attributes, sets up the interrupt handler and validates the virtual
8188127a1f8SMichael Holzheu  *     unit record device.
8198127a1f8SMichael Holzheu  *     ur_remove removes the device attributes and drops the reference to
8208127a1f8SMichael Holzheu  *     struct urdev.
8218127a1f8SMichael Holzheu  *
8228127a1f8SMichael Holzheu  *     ur_probe, ur_remove, ur_set_online and ur_set_offline are serialized
8238127a1f8SMichael Holzheu  *     by the vmur_mutex lock.
8248127a1f8SMichael Holzheu  *
8258127a1f8SMichael Holzheu  *     urd->char_device is used as indication that the online function has
8268127a1f8SMichael Holzheu  *     been completed successfully.
827810cb5b3SFrank Munzert  */
828810cb5b3SFrank Munzert static int ur_probe(struct ccw_device *cdev)
829810cb5b3SFrank Munzert {
830810cb5b3SFrank Munzert 	struct urdev *urd;
831810cb5b3SFrank Munzert 	int rc;
832810cb5b3SFrank Munzert 
8338127a1f8SMichael Holzheu 	TRACE("ur_probe: cdev=%p\n", cdev);
834810cb5b3SFrank Munzert 
8358127a1f8SMichael Holzheu 	mutex_lock(&vmur_mutex);
836810cb5b3SFrank Munzert 	urd = urdev_alloc(cdev);
837810cb5b3SFrank Munzert 	if (!urd) {
838810cb5b3SFrank Munzert 		rc = -ENOMEM;
8398127a1f8SMichael Holzheu 		goto fail_unlock;
840810cb5b3SFrank Munzert 	}
8418127a1f8SMichael Holzheu 
842810cb5b3SFrank Munzert 	rc = ur_create_attributes(&cdev->dev);
843810cb5b3SFrank Munzert 	if (rc) {
844810cb5b3SFrank Munzert 		rc = -ENOMEM;
8458127a1f8SMichael Holzheu 		goto fail_urdev_put;
846810cb5b3SFrank Munzert 	}
847810cb5b3SFrank Munzert 	cdev->handler = ur_int_handler;
848810cb5b3SFrank Munzert 
849810cb5b3SFrank Munzert 	/* validate virtual unit record device */
850810cb5b3SFrank Munzert 	urd->class = get_urd_class(urd);
851810cb5b3SFrank Munzert 	if (urd->class < 0) {
852810cb5b3SFrank Munzert 		rc = urd->class;
8538127a1f8SMichael Holzheu 		goto fail_remove_attr;
854810cb5b3SFrank Munzert 	}
855810cb5b3SFrank Munzert 	if ((urd->class != DEV_CLASS_UR_I) && (urd->class != DEV_CLASS_UR_O)) {
856b9993a38SFrank Munzert 		rc = -EOPNOTSUPP;
8578127a1f8SMichael Holzheu 		goto fail_remove_attr;
858810cb5b3SFrank Munzert 	}
8598127a1f8SMichael Holzheu 	spin_lock_irq(get_ccwdev_lock(cdev));
860dff59b64SGreg Kroah-Hartman 	dev_set_drvdata(&cdev->dev, urd);
8618127a1f8SMichael Holzheu 	spin_unlock_irq(get_ccwdev_lock(cdev));
862810cb5b3SFrank Munzert 
8638127a1f8SMichael Holzheu 	mutex_unlock(&vmur_mutex);
864810cb5b3SFrank Munzert 	return 0;
865810cb5b3SFrank Munzert 
8668127a1f8SMichael Holzheu fail_remove_attr:
867810cb5b3SFrank Munzert 	ur_remove_attributes(&cdev->dev);
8688127a1f8SMichael Holzheu fail_urdev_put:
8698127a1f8SMichael Holzheu 	urdev_put(urd);
8708127a1f8SMichael Holzheu fail_unlock:
8718127a1f8SMichael Holzheu 	mutex_unlock(&vmur_mutex);
8728127a1f8SMichael Holzheu 	return rc;
873810cb5b3SFrank Munzert }
874810cb5b3SFrank Munzert 
875810cb5b3SFrank Munzert static int ur_set_online(struct ccw_device *cdev)
876810cb5b3SFrank Munzert {
877810cb5b3SFrank Munzert 	struct urdev *urd;
878810cb5b3SFrank Munzert 	int minor, major, rc;
879810cb5b3SFrank Munzert 	char node_id[16];
880810cb5b3SFrank Munzert 
8818127a1f8SMichael Holzheu 	TRACE("ur_set_online: cdev=%p\n", cdev);
882810cb5b3SFrank Munzert 
8838127a1f8SMichael Holzheu 	mutex_lock(&vmur_mutex);
8848127a1f8SMichael Holzheu 	urd = urdev_get_from_cdev(cdev);
8858127a1f8SMichael Holzheu 	if (!urd) {
8868127a1f8SMichael Holzheu 		/* ur_remove already deleted our urd */
8878127a1f8SMichael Holzheu 		rc = -ENODEV;
8888127a1f8SMichael Holzheu 		goto fail_unlock;
8898127a1f8SMichael Holzheu 	}
890810cb5b3SFrank Munzert 
8918127a1f8SMichael Holzheu 	if (urd->char_device) {
8928127a1f8SMichael Holzheu 		/* Another ur_set_online was faster */
8938127a1f8SMichael Holzheu 		rc = -EBUSY;
8948127a1f8SMichael Holzheu 		goto fail_urdev_put;
8958127a1f8SMichael Holzheu 	}
8968127a1f8SMichael Holzheu 
897810cb5b3SFrank Munzert 	minor = urd->dev_id.devno;
898810cb5b3SFrank Munzert 	major = MAJOR(ur_first_dev_maj_min);
899810cb5b3SFrank Munzert 
900810cb5b3SFrank Munzert 	urd->char_device = cdev_alloc();
901810cb5b3SFrank Munzert 	if (!urd->char_device) {
902810cb5b3SFrank Munzert 		rc = -ENOMEM;
9038127a1f8SMichael Holzheu 		goto fail_urdev_put;
904810cb5b3SFrank Munzert 	}
905810cb5b3SFrank Munzert 
906*dae1df42SDennis Chen 	urd->char_device->ops = &ur_fops;
907810cb5b3SFrank Munzert 	urd->char_device->dev = MKDEV(major, minor);
908810cb5b3SFrank Munzert 	urd->char_device->owner = ur_fops.owner;
909810cb5b3SFrank Munzert 
910810cb5b3SFrank Munzert 	rc = cdev_add(urd->char_device, urd->char_device->dev, 1);
911810cb5b3SFrank Munzert 	if (rc)
912810cb5b3SFrank Munzert 		goto fail_free_cdev;
913810cb5b3SFrank Munzert 	if (urd->cdev->id.cu_type == READER_PUNCH_DEVTYPE) {
914810cb5b3SFrank Munzert 		if (urd->class == DEV_CLASS_UR_I)
9152a0217d5SKay Sievers 			sprintf(node_id, "vmrdr-%s", dev_name(&cdev->dev));
916810cb5b3SFrank Munzert 		if (urd->class == DEV_CLASS_UR_O)
9172a0217d5SKay Sievers 			sprintf(node_id, "vmpun-%s", dev_name(&cdev->dev));
918810cb5b3SFrank Munzert 	} else if (urd->cdev->id.cu_type == PRINTER_DEVTYPE) {
9192a0217d5SKay Sievers 		sprintf(node_id, "vmprt-%s", dev_name(&cdev->dev));
920810cb5b3SFrank Munzert 	} else {
921b9993a38SFrank Munzert 		rc = -EOPNOTSUPP;
922810cb5b3SFrank Munzert 		goto fail_free_cdev;
923810cb5b3SFrank Munzert 	}
924810cb5b3SFrank Munzert 
925ea9e42f6SGreg Kroah-Hartman 	urd->device = device_create(vmur_class, NULL, urd->char_device->dev,
926ea9e42f6SGreg Kroah-Hartman 				    NULL, "%s", node_id);
927810cb5b3SFrank Munzert 	if (IS_ERR(urd->device)) {
928810cb5b3SFrank Munzert 		rc = PTR_ERR(urd->device);
929810cb5b3SFrank Munzert 		TRACE("ur_set_online: device_create rc=%d\n", rc);
930810cb5b3SFrank Munzert 		goto fail_free_cdev;
931810cb5b3SFrank Munzert 	}
9328127a1f8SMichael Holzheu 	urdev_put(urd);
9338127a1f8SMichael Holzheu 	mutex_unlock(&vmur_mutex);
934810cb5b3SFrank Munzert 	return 0;
935810cb5b3SFrank Munzert 
936810cb5b3SFrank Munzert fail_free_cdev:
937810cb5b3SFrank Munzert 	cdev_del(urd->char_device);
9388127a1f8SMichael Holzheu 	urd->char_device = NULL;
9398127a1f8SMichael Holzheu fail_urdev_put:
9408127a1f8SMichael Holzheu 	urdev_put(urd);
9418127a1f8SMichael Holzheu fail_unlock:
9428127a1f8SMichael Holzheu 	mutex_unlock(&vmur_mutex);
9438127a1f8SMichael Holzheu 	return rc;
9448127a1f8SMichael Holzheu }
945810cb5b3SFrank Munzert 
9468127a1f8SMichael Holzheu static int ur_set_offline_force(struct ccw_device *cdev, int force)
9478127a1f8SMichael Holzheu {
9488127a1f8SMichael Holzheu 	struct urdev *urd;
9498127a1f8SMichael Holzheu 	int rc;
9508127a1f8SMichael Holzheu 
9518127a1f8SMichael Holzheu 	TRACE("ur_set_offline: cdev=%p\n", cdev);
9528127a1f8SMichael Holzheu 	urd = urdev_get_from_cdev(cdev);
9538127a1f8SMichael Holzheu 	if (!urd)
9548127a1f8SMichael Holzheu 		/* ur_remove already deleted our urd */
9558127a1f8SMichael Holzheu 		return -ENODEV;
9568127a1f8SMichael Holzheu 	if (!urd->char_device) {
9578127a1f8SMichael Holzheu 		/* Another ur_set_offline was faster */
9588127a1f8SMichael Holzheu 		rc = -EBUSY;
9598127a1f8SMichael Holzheu 		goto fail_urdev_put;
9608127a1f8SMichael Holzheu 	}
9618127a1f8SMichael Holzheu 	if (!force && (atomic_read(&urd->ref_count) > 2)) {
9628127a1f8SMichael Holzheu 		/* There is still a user of urd (e.g. ur_open) */
9638127a1f8SMichael Holzheu 		TRACE("ur_set_offline: BUSY\n");
9648127a1f8SMichael Holzheu 		rc = -EBUSY;
9658127a1f8SMichael Holzheu 		goto fail_urdev_put;
9668127a1f8SMichael Holzheu 	}
9678127a1f8SMichael Holzheu 	device_destroy(vmur_class, urd->char_device->dev);
9688127a1f8SMichael Holzheu 	cdev_del(urd->char_device);
9698127a1f8SMichael Holzheu 	urd->char_device = NULL;
9708127a1f8SMichael Holzheu 	rc = 0;
9718127a1f8SMichael Holzheu 
9728127a1f8SMichael Holzheu fail_urdev_put:
9738127a1f8SMichael Holzheu 	urdev_put(urd);
974810cb5b3SFrank Munzert 	return rc;
975810cb5b3SFrank Munzert }
976810cb5b3SFrank Munzert 
977810cb5b3SFrank Munzert static int ur_set_offline(struct ccw_device *cdev)
978810cb5b3SFrank Munzert {
9798127a1f8SMichael Holzheu 	int rc;
980810cb5b3SFrank Munzert 
9818127a1f8SMichael Holzheu 	mutex_lock(&vmur_mutex);
9828127a1f8SMichael Holzheu 	rc = ur_set_offline_force(cdev, 0);
9838127a1f8SMichael Holzheu 	mutex_unlock(&vmur_mutex);
9848127a1f8SMichael Holzheu 	return rc;
9858127a1f8SMichael Holzheu }
986810cb5b3SFrank Munzert 
9878127a1f8SMichael Holzheu static void ur_remove(struct ccw_device *cdev)
9888127a1f8SMichael Holzheu {
9898127a1f8SMichael Holzheu 	unsigned long flags;
9908127a1f8SMichael Holzheu 
9918127a1f8SMichael Holzheu 	TRACE("ur_remove\n");
9928127a1f8SMichael Holzheu 
9938127a1f8SMichael Holzheu 	mutex_lock(&vmur_mutex);
9948127a1f8SMichael Holzheu 
9958127a1f8SMichael Holzheu 	if (cdev->online)
9968127a1f8SMichael Holzheu 		ur_set_offline_force(cdev, 1);
9978127a1f8SMichael Holzheu 	ur_remove_attributes(&cdev->dev);
9988127a1f8SMichael Holzheu 
9998127a1f8SMichael Holzheu 	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1000dff59b64SGreg Kroah-Hartman 	urdev_put(dev_get_drvdata(&cdev->dev));
1001dff59b64SGreg Kroah-Hartman 	dev_set_drvdata(&cdev->dev, NULL);
10028127a1f8SMichael Holzheu 	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
10038127a1f8SMichael Holzheu 
10048127a1f8SMichael Holzheu 	mutex_unlock(&vmur_mutex);
1005810cb5b3SFrank Munzert }
1006810cb5b3SFrank Munzert 
1007810cb5b3SFrank Munzert /*
1008810cb5b3SFrank Munzert  * Module initialisation and cleanup
1009810cb5b3SFrank Munzert  */
1010810cb5b3SFrank Munzert static int __init ur_init(void)
1011810cb5b3SFrank Munzert {
1012810cb5b3SFrank Munzert 	int rc;
1013810cb5b3SFrank Munzert 	dev_t dev;
1014810cb5b3SFrank Munzert 
1015810cb5b3SFrank Munzert 	if (!MACHINE_IS_VM) {
101680a5c36dSFrank Munzert 		pr_err("The %s cannot be loaded without z/VM\n",
101780a5c36dSFrank Munzert 		       ur_banner);
1018810cb5b3SFrank Munzert 		return -ENODEV;
1019810cb5b3SFrank Munzert 	}
1020810cb5b3SFrank Munzert 
1021810cb5b3SFrank Munzert 	vmur_dbf = debug_register("vmur", 4, 1, 4 * sizeof(long));
1022810cb5b3SFrank Munzert 	if (!vmur_dbf)
1023810cb5b3SFrank Munzert 		return -ENOMEM;
1024810cb5b3SFrank Munzert 	rc = debug_register_view(vmur_dbf, &debug_sprintf_view);
1025810cb5b3SFrank Munzert 	if (rc)
1026810cb5b3SFrank Munzert 		goto fail_free_dbf;
1027810cb5b3SFrank Munzert 
1028810cb5b3SFrank Munzert 	debug_set_level(vmur_dbf, 6);
1029810cb5b3SFrank Munzert 
10303f7cb51fSMichael Holzheu 	vmur_class = class_create(THIS_MODULE, "vmur");
10313f7cb51fSMichael Holzheu 	if (IS_ERR(vmur_class)) {
10323f7cb51fSMichael Holzheu 		rc = PTR_ERR(vmur_class);
10333f7cb51fSMichael Holzheu 		goto fail_free_dbf;
10343f7cb51fSMichael Holzheu 	}
10353f7cb51fSMichael Holzheu 
1036810cb5b3SFrank Munzert 	rc = ccw_driver_register(&ur_driver);
1037810cb5b3SFrank Munzert 	if (rc)
10383f7cb51fSMichael Holzheu 		goto fail_class_destroy;
1039810cb5b3SFrank Munzert 
1040810cb5b3SFrank Munzert 	rc = alloc_chrdev_region(&dev, 0, NUM_MINORS, "vmur");
1041810cb5b3SFrank Munzert 	if (rc) {
104280a5c36dSFrank Munzert 		pr_err("Kernel function alloc_chrdev_region failed with "
104380a5c36dSFrank Munzert 		       "error code %d\n", rc);
1044810cb5b3SFrank Munzert 		goto fail_unregister_driver;
1045810cb5b3SFrank Munzert 	}
1046810cb5b3SFrank Munzert 	ur_first_dev_maj_min = MKDEV(MAJOR(dev), 0);
1047810cb5b3SFrank Munzert 
104880a5c36dSFrank Munzert 	pr_info("%s loaded.\n", ur_banner);
1049810cb5b3SFrank Munzert 	return 0;
1050810cb5b3SFrank Munzert 
1051810cb5b3SFrank Munzert fail_unregister_driver:
1052810cb5b3SFrank Munzert 	ccw_driver_unregister(&ur_driver);
10533f7cb51fSMichael Holzheu fail_class_destroy:
10543f7cb51fSMichael Holzheu 	class_destroy(vmur_class);
1055810cb5b3SFrank Munzert fail_free_dbf:
1056810cb5b3SFrank Munzert 	debug_unregister(vmur_dbf);
1057810cb5b3SFrank Munzert 	return rc;
1058810cb5b3SFrank Munzert }
1059810cb5b3SFrank Munzert 
1060810cb5b3SFrank Munzert static void __exit ur_exit(void)
1061810cb5b3SFrank Munzert {
1062810cb5b3SFrank Munzert 	unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS);
1063810cb5b3SFrank Munzert 	ccw_driver_unregister(&ur_driver);
10643f7cb51fSMichael Holzheu 	class_destroy(vmur_class);
1065810cb5b3SFrank Munzert 	debug_unregister(vmur_dbf);
106680a5c36dSFrank Munzert 	pr_info("%s unloaded.\n", ur_banner);
1067810cb5b3SFrank Munzert }
1068810cb5b3SFrank Munzert 
1069810cb5b3SFrank Munzert module_init(ur_init);
1070810cb5b3SFrank Munzert module_exit(ur_exit);
1071