xref: /openbmc/linux/drivers/s390/char/vmur.c (revision f3e59ff348c077a6afd4edb23d7e69e9cba62fdc)
16f05e69eSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2810cb5b3SFrank Munzert /*
3810cb5b3SFrank Munzert  * Linux driver for System z and s390 unit record devices
4810cb5b3SFrank Munzert  * (z/VM virtual punch, reader, printer)
5810cb5b3SFrank Munzert  *
6b241f7bcSFrank Munzert  * Copyright IBM Corp. 2001, 2009
7810cb5b3SFrank Munzert  * Authors: Malcolm Beattie <beattiem@uk.ibm.com>
8810cb5b3SFrank Munzert  *	    Michael Holzheu <holzheu@de.ibm.com>
9810cb5b3SFrank Munzert  *	    Frank Munzert <munzert@de.ibm.com>
10810cb5b3SFrank Munzert  */
11810cb5b3SFrank Munzert 
1280a5c36dSFrank Munzert #define KMSG_COMPONENT "vmur"
1380a5c36dSFrank Munzert #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
1480a5c36dSFrank Munzert 
15810cb5b3SFrank Munzert #include <linux/cdev.h>
165a0e3ad6STejun Heo #include <linux/slab.h>
173a4c5d59SHeiko Carstens #include <linux/module.h>
18810cb5b3SFrank Munzert 
197c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
20810cb5b3SFrank Munzert #include <asm/cio.h>
21810cb5b3SFrank Munzert #include <asm/ccwdev.h>
22810cb5b3SFrank Munzert #include <asm/debug.h>
230a87c5cfSMichael Holzheu #include <asm/diag.h>
24810cb5b3SFrank Munzert 
25810cb5b3SFrank Munzert #include "vmur.h"
26810cb5b3SFrank Munzert 
27810cb5b3SFrank Munzert /*
28810cb5b3SFrank Munzert  * Driver overview
29810cb5b3SFrank Munzert  *
30810cb5b3SFrank Munzert  * Unit record device support is implemented as a character device driver.
31810cb5b3SFrank Munzert  * We can fit at least 16 bits into a device minor number and use the
32810cb5b3SFrank Munzert  * simple method of mapping a character device number with minor abcd
33810cb5b3SFrank Munzert  * to the unit record device with devno abcd.
34810cb5b3SFrank Munzert  * I/O to virtual unit record devices is handled as follows:
35810cb5b3SFrank Munzert  * Reads: Diagnose code 0x14 (input spool file manipulation)
36810cb5b3SFrank Munzert  * is used to read spool data page-wise.
37810cb5b3SFrank Munzert  * Writes: The CCW used is WRITE_CCW_CMD (0x01). The device's record length
38810cb5b3SFrank Munzert  * is available by reading sysfs attr reclen. Each write() to the device
39810cb5b3SFrank Munzert  * must specify an integral multiple (maximal 511) of reclen.
40810cb5b3SFrank Munzert  */
41810cb5b3SFrank Munzert 
42810cb5b3SFrank Munzert static char ur_banner[] = "z/VM virtual unit record device driver";
43810cb5b3SFrank Munzert 
44810cb5b3SFrank Munzert MODULE_AUTHOR("IBM Corporation");
45810cb5b3SFrank Munzert MODULE_DESCRIPTION("s390 z/VM virtual unit record device driver");
46810cb5b3SFrank Munzert MODULE_LICENSE("GPL");
47810cb5b3SFrank Munzert 
48810cb5b3SFrank Munzert static dev_t ur_first_dev_maj_min;
49810cb5b3SFrank Munzert static struct class *vmur_class;
50810cb5b3SFrank Munzert static struct debug_info *vmur_dbf;
51810cb5b3SFrank Munzert 
52810cb5b3SFrank Munzert /* We put the device's record length (for writes) in the driver_info field */
53810cb5b3SFrank Munzert static struct ccw_device_id ur_ids[] = {
54810cb5b3SFrank Munzert 	{ CCWDEV_CU_DI(READER_PUNCH_DEVTYPE, 80) },
55810cb5b3SFrank Munzert 	{ CCWDEV_CU_DI(PRINTER_DEVTYPE, 132) },
56810cb5b3SFrank Munzert 	{ /* end of list */ }
57810cb5b3SFrank Munzert };
58810cb5b3SFrank Munzert 
59810cb5b3SFrank Munzert MODULE_DEVICE_TABLE(ccw, ur_ids);
60810cb5b3SFrank Munzert 
61810cb5b3SFrank Munzert static int ur_probe(struct ccw_device *cdev);
62810cb5b3SFrank Munzert static void ur_remove(struct ccw_device *cdev);
63810cb5b3SFrank Munzert static int ur_set_online(struct ccw_device *cdev);
64810cb5b3SFrank Munzert static int ur_set_offline(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,
76420f42ecSHeiko Carstens 	.int_class	= IRQIO_VMR,
77810cb5b3SFrank Munzert };
78810cb5b3SFrank Munzert 
798127a1f8SMichael Holzheu static DEFINE_MUTEX(vmur_mutex);
808127a1f8SMichael Holzheu 
81810cb5b3SFrank Munzert /*
82810cb5b3SFrank Munzert  * Allocation, freeing, getting and putting of urdev structures
838127a1f8SMichael Holzheu  *
848127a1f8SMichael Holzheu  * Each ur device (urd) contains a reference to its corresponding ccw device
858127a1f8SMichael Holzheu  * (cdev) using the urd->cdev pointer. Each ccw device has a reference to the
86dff59b64SGreg Kroah-Hartman  * ur device using dev_get_drvdata(&cdev->dev) pointer.
878127a1f8SMichael Holzheu  *
888127a1f8SMichael Holzheu  * urd references:
898127a1f8SMichael Holzheu  * - ur_probe gets a urd reference, ur_remove drops the reference
90dff59b64SGreg Kroah-Hartman  *   dev_get_drvdata(&cdev->dev)
9183a35e36SGeert Uytterhoeven  * - ur_open gets a urd reference, ur_release drops the reference
928127a1f8SMichael Holzheu  *   (urf->urd)
938127a1f8SMichael Holzheu  *
948127a1f8SMichael Holzheu  * cdev references:
958127a1f8SMichael Holzheu  * - urdev_alloc get a cdev reference (urd->cdev)
968127a1f8SMichael Holzheu  * - urdev_free drops the cdev reference (urd->cdev)
978127a1f8SMichael Holzheu  *
98dff59b64SGreg Kroah-Hartman  * Setting and clearing of dev_get_drvdata(&cdev->dev) is protected by the ccwdev lock
99810cb5b3SFrank Munzert  */
100810cb5b3SFrank Munzert static struct urdev *urdev_alloc(struct ccw_device *cdev)
101810cb5b3SFrank Munzert {
102810cb5b3SFrank Munzert 	struct urdev *urd;
103810cb5b3SFrank Munzert 
104810cb5b3SFrank Munzert 	urd = kzalloc(sizeof(struct urdev), GFP_KERNEL);
105810cb5b3SFrank Munzert 	if (!urd)
106810cb5b3SFrank Munzert 		return NULL;
107810cb5b3SFrank Munzert 	urd->reclen = cdev->id.driver_info;
108810cb5b3SFrank Munzert 	ccw_device_get_id(cdev, &urd->dev_id);
109810cb5b3SFrank Munzert 	mutex_init(&urd->io_mutex);
110a695f167SFrank Munzert 	init_waitqueue_head(&urd->wait);
111a695f167SFrank Munzert 	spin_lock_init(&urd->open_lock);
1120462d992SElena Reshetova 	refcount_set(&urd->ref_count,  1);
1138127a1f8SMichael Holzheu 	urd->cdev = cdev;
1148127a1f8SMichael Holzheu 	get_device(&cdev->dev);
115810cb5b3SFrank Munzert 	return urd;
116810cb5b3SFrank Munzert }
117810cb5b3SFrank Munzert 
118810cb5b3SFrank Munzert static void urdev_free(struct urdev *urd)
119810cb5b3SFrank Munzert {
1208127a1f8SMichael Holzheu 	TRACE("urdev_free: %p\n", urd);
1218127a1f8SMichael Holzheu 	if (urd->cdev)
1228127a1f8SMichael Holzheu 		put_device(&urd->cdev->dev);
123810cb5b3SFrank Munzert 	kfree(urd);
124810cb5b3SFrank Munzert }
125810cb5b3SFrank Munzert 
1268127a1f8SMichael Holzheu static void urdev_get(struct urdev *urd)
1278127a1f8SMichael Holzheu {
1280462d992SElena Reshetova 	refcount_inc(&urd->ref_count);
1298127a1f8SMichael Holzheu }
1308127a1f8SMichael Holzheu 
1318127a1f8SMichael Holzheu static struct urdev *urdev_get_from_cdev(struct ccw_device *cdev)
1328127a1f8SMichael Holzheu {
1338127a1f8SMichael Holzheu 	struct urdev *urd;
1348127a1f8SMichael Holzheu 	unsigned long flags;
1358127a1f8SMichael Holzheu 
1368127a1f8SMichael Holzheu 	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
137dff59b64SGreg Kroah-Hartman 	urd = dev_get_drvdata(&cdev->dev);
1388127a1f8SMichael Holzheu 	if (urd)
1398127a1f8SMichael Holzheu 		urdev_get(urd);
1408127a1f8SMichael Holzheu 	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1418127a1f8SMichael Holzheu 	return urd;
1428127a1f8SMichael Holzheu }
1438127a1f8SMichael Holzheu 
144810cb5b3SFrank Munzert static struct urdev *urdev_get_from_devno(u16 devno)
145810cb5b3SFrank Munzert {
146810cb5b3SFrank Munzert 	char bus_id[16];
147810cb5b3SFrank Munzert 	struct ccw_device *cdev;
1488127a1f8SMichael Holzheu 	struct urdev *urd;
149810cb5b3SFrank Munzert 
150810cb5b3SFrank Munzert 	sprintf(bus_id, "0.0.%04x", devno);
151810cb5b3SFrank Munzert 	cdev = get_ccwdev_by_busid(&ur_driver, bus_id);
152810cb5b3SFrank Munzert 	if (!cdev)
153810cb5b3SFrank Munzert 		return NULL;
1548127a1f8SMichael Holzheu 	urd = urdev_get_from_cdev(cdev);
1558127a1f8SMichael Holzheu 	put_device(&cdev->dev);
1568127a1f8SMichael Holzheu 	return urd;
157810cb5b3SFrank Munzert }
158810cb5b3SFrank Munzert 
159810cb5b3SFrank Munzert static void urdev_put(struct urdev *urd)
160810cb5b3SFrank Munzert {
1610462d992SElena Reshetova 	if (refcount_dec_and_test(&urd->ref_count))
1628127a1f8SMichael Holzheu 		urdev_free(urd);
163810cb5b3SFrank Munzert }
164810cb5b3SFrank Munzert 
165810cb5b3SFrank Munzert /*
166810cb5b3SFrank Munzert  * Low-level functions to do I/O to a ur device.
167810cb5b3SFrank Munzert  *     alloc_chan_prog
1681eade380SMichael Holzheu  *     free_chan_prog
169810cb5b3SFrank Munzert  *     do_ur_io
170810cb5b3SFrank Munzert  *     ur_int_handler
171810cb5b3SFrank Munzert  *
172810cb5b3SFrank Munzert  * alloc_chan_prog allocates and builds the channel program
1731eade380SMichael Holzheu  * free_chan_prog frees memory of the channel program
174810cb5b3SFrank Munzert  *
175810cb5b3SFrank Munzert  * do_ur_io issues the channel program to the device and blocks waiting
176810cb5b3SFrank Munzert  * on a completion event it publishes at urd->io_done. The function
177810cb5b3SFrank Munzert  * serialises itself on the device's mutex so that only one I/O
178810cb5b3SFrank Munzert  * is issued at a time (and that I/O is synchronous).
179810cb5b3SFrank Munzert  *
180810cb5b3SFrank Munzert  * ur_int_handler catches the "I/O done" interrupt, writes the
181810cb5b3SFrank Munzert  * subchannel status word into the scsw member of the urdev structure
182810cb5b3SFrank Munzert  * and complete()s the io_done to wake the waiting do_ur_io.
183810cb5b3SFrank Munzert  *
184810cb5b3SFrank Munzert  * The caller of do_ur_io is responsible for kfree()ing the channel program
185810cb5b3SFrank Munzert  * address pointer that alloc_chan_prog returned.
186810cb5b3SFrank Munzert  */
187810cb5b3SFrank Munzert 
1881eade380SMichael Holzheu static void free_chan_prog(struct ccw1 *cpa)
1891eade380SMichael Holzheu {
1901eade380SMichael Holzheu 	struct ccw1 *ptr = cpa;
1911eade380SMichael Holzheu 
1921eade380SMichael Holzheu 	while (ptr->cda) {
1931eade380SMichael Holzheu 		kfree((void *)(addr_t) ptr->cda);
1941eade380SMichael Holzheu 		ptr++;
1951eade380SMichael Holzheu 	}
1961eade380SMichael Holzheu 	kfree(cpa);
1971eade380SMichael Holzheu }
198810cb5b3SFrank Munzert 
199810cb5b3SFrank Munzert /*
200810cb5b3SFrank Munzert  * alloc_chan_prog
201810cb5b3SFrank Munzert  * The channel program we use is write commands chained together
202810cb5b3SFrank Munzert  * with a final NOP CCW command-chained on (which ensures that CE and DE
203810cb5b3SFrank Munzert  * are presented together in a single interrupt instead of as separate
204810cb5b3SFrank Munzert  * interrupts unless an incorrect length indication kicks in first). The
2051eade380SMichael Holzheu  * data length in each CCW is reclen.
206810cb5b3SFrank Munzert  */
2071eade380SMichael Holzheu static struct ccw1 *alloc_chan_prog(const char __user *ubuf, int rec_count,
2081eade380SMichael Holzheu 				    int reclen)
209810cb5b3SFrank Munzert {
210810cb5b3SFrank Munzert 	struct ccw1 *cpa;
2111eade380SMichael Holzheu 	void *kbuf;
212810cb5b3SFrank Munzert 	int i;
213810cb5b3SFrank Munzert 
2141eade380SMichael Holzheu 	TRACE("alloc_chan_prog(%p, %i, %i)\n", ubuf, rec_count, reclen);
215810cb5b3SFrank Munzert 
216810cb5b3SFrank Munzert 	/*
217810cb5b3SFrank Munzert 	 * We chain a NOP onto the writes to force CE+DE together.
218810cb5b3SFrank Munzert 	 * That means we allocate room for CCWs to cover count/reclen
219810cb5b3SFrank Munzert 	 * records plus a NOP.
220810cb5b3SFrank Munzert 	 */
2216396bb22SKees Cook 	cpa = kcalloc(rec_count + 1, sizeof(struct ccw1),
2221eade380SMichael Holzheu 		      GFP_KERNEL | GFP_DMA);
223810cb5b3SFrank Munzert 	if (!cpa)
2241eade380SMichael Holzheu 		return ERR_PTR(-ENOMEM);
225810cb5b3SFrank Munzert 
2261eade380SMichael Holzheu 	for (i = 0; i < rec_count; i++) {
227810cb5b3SFrank Munzert 		cpa[i].cmd_code = WRITE_CCW_CMD;
228810cb5b3SFrank Munzert 		cpa[i].flags = CCW_FLAG_CC | CCW_FLAG_SLI;
229810cb5b3SFrank Munzert 		cpa[i].count = reclen;
2301eade380SMichael Holzheu 		kbuf = kmalloc(reclen, GFP_KERNEL | GFP_DMA);
2311eade380SMichael Holzheu 		if (!kbuf) {
2321eade380SMichael Holzheu 			free_chan_prog(cpa);
2331eade380SMichael Holzheu 			return ERR_PTR(-ENOMEM);
2341eade380SMichael Holzheu 		}
2351eade380SMichael Holzheu 		cpa[i].cda = (u32)(addr_t) kbuf;
2361eade380SMichael Holzheu 		if (copy_from_user(kbuf, ubuf, reclen)) {
2371eade380SMichael Holzheu 			free_chan_prog(cpa);
2381eade380SMichael Holzheu 			return ERR_PTR(-EFAULT);
2391eade380SMichael Holzheu 		}
2401eade380SMichael Holzheu 		ubuf += reclen;
241810cb5b3SFrank Munzert 	}
242810cb5b3SFrank Munzert 	/* The following NOP CCW forces CE+DE to be presented together */
243810cb5b3SFrank Munzert 	cpa[i].cmd_code = CCW_CMD_NOOP;
244810cb5b3SFrank Munzert 	return cpa;
245810cb5b3SFrank Munzert }
246810cb5b3SFrank Munzert 
247810cb5b3SFrank Munzert static int do_ur_io(struct urdev *urd, struct ccw1 *cpa)
248810cb5b3SFrank Munzert {
249810cb5b3SFrank Munzert 	int rc;
250810cb5b3SFrank Munzert 	struct ccw_device *cdev = urd->cdev;
251278bc68cSHeiko Carstens 	DECLARE_COMPLETION_ONSTACK(event);
252810cb5b3SFrank Munzert 
253810cb5b3SFrank Munzert 	TRACE("do_ur_io: cpa=%p\n", cpa);
254810cb5b3SFrank Munzert 
255810cb5b3SFrank Munzert 	rc = mutex_lock_interruptible(&urd->io_mutex);
256810cb5b3SFrank Munzert 	if (rc)
257810cb5b3SFrank Munzert 		return rc;
258810cb5b3SFrank Munzert 
259810cb5b3SFrank Munzert 	urd->io_done = &event;
260810cb5b3SFrank Munzert 
261810cb5b3SFrank Munzert 	spin_lock_irq(get_ccwdev_lock(cdev));
262810cb5b3SFrank Munzert 	rc = ccw_device_start(cdev, cpa, 1, 0, 0);
263810cb5b3SFrank Munzert 	spin_unlock_irq(get_ccwdev_lock(cdev));
264810cb5b3SFrank Munzert 
265810cb5b3SFrank Munzert 	TRACE("do_ur_io: ccw_device_start returned %d\n", rc);
266810cb5b3SFrank Munzert 	if (rc)
267810cb5b3SFrank Munzert 		goto out;
268810cb5b3SFrank Munzert 
269810cb5b3SFrank Munzert 	wait_for_completion(&event);
270810cb5b3SFrank Munzert 	TRACE("do_ur_io: I/O complete\n");
271810cb5b3SFrank Munzert 	rc = 0;
272810cb5b3SFrank Munzert 
273810cb5b3SFrank Munzert out:
274810cb5b3SFrank Munzert 	mutex_unlock(&urd->io_mutex);
275810cb5b3SFrank Munzert 	return rc;
276810cb5b3SFrank Munzert }
277810cb5b3SFrank Munzert 
278810cb5b3SFrank Munzert /*
279810cb5b3SFrank Munzert  * ur interrupt handler, called from the ccw_device layer
280810cb5b3SFrank Munzert  */
281810cb5b3SFrank Munzert static void ur_int_handler(struct ccw_device *cdev, unsigned long intparm,
282810cb5b3SFrank Munzert 			   struct irb *irb)
283810cb5b3SFrank Munzert {
284810cb5b3SFrank Munzert 	struct urdev *urd;
285810cb5b3SFrank Munzert 
286f622b517SSebastian Ott 	if (!IS_ERR(irb)) {
287810cb5b3SFrank Munzert 		TRACE("ur_int_handler: intparm=0x%lx cstat=%02x dstat=%02x res=%u\n",
28823d805b6SPeter Oberparleiter 		      intparm, irb->scsw.cmd.cstat, irb->scsw.cmd.dstat,
28923d805b6SPeter Oberparleiter 		      irb->scsw.cmd.count);
290f622b517SSebastian Ott 	}
291810cb5b3SFrank Munzert 	if (!intparm) {
292810cb5b3SFrank Munzert 		TRACE("ur_int_handler: unsolicited interrupt\n");
293810cb5b3SFrank Munzert 		return;
294810cb5b3SFrank Munzert 	}
295dff59b64SGreg Kroah-Hartman 	urd = dev_get_drvdata(&cdev->dev);
296810cb5b3SFrank Munzert 	/* On special conditions irb is an error pointer */
297810cb5b3SFrank Munzert 	if (IS_ERR(irb))
298810cb5b3SFrank Munzert 		urd->io_request_rc = PTR_ERR(irb);
29923d805b6SPeter Oberparleiter 	else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END))
300810cb5b3SFrank Munzert 		urd->io_request_rc = 0;
301810cb5b3SFrank Munzert 	else
302810cb5b3SFrank Munzert 		urd->io_request_rc = -EIO;
303810cb5b3SFrank Munzert 
304810cb5b3SFrank Munzert 	complete(urd->io_done);
305810cb5b3SFrank Munzert }
306810cb5b3SFrank Munzert 
307810cb5b3SFrank Munzert /*
308810cb5b3SFrank Munzert  * reclen sysfs attribute - The record length to be used for write CCWs
309810cb5b3SFrank Munzert  */
310810cb5b3SFrank Munzert static ssize_t ur_attr_reclen_show(struct device *dev,
311810cb5b3SFrank Munzert 				   struct device_attribute *attr, char *buf)
312810cb5b3SFrank Munzert {
3138127a1f8SMichael Holzheu 	struct urdev *urd;
3148127a1f8SMichael Holzheu 	int rc;
315810cb5b3SFrank Munzert 
3168127a1f8SMichael Holzheu 	urd = urdev_get_from_cdev(to_ccwdev(dev));
3178127a1f8SMichael Holzheu 	if (!urd)
3188127a1f8SMichael Holzheu 		return -ENODEV;
3198127a1f8SMichael Holzheu 	rc = sprintf(buf, "%zu\n", urd->reclen);
3208127a1f8SMichael Holzheu 	urdev_put(urd);
3218127a1f8SMichael Holzheu 	return rc;
322810cb5b3SFrank Munzert }
323810cb5b3SFrank Munzert 
324810cb5b3SFrank Munzert static DEVICE_ATTR(reclen, 0444, ur_attr_reclen_show, NULL);
325810cb5b3SFrank Munzert 
326810cb5b3SFrank Munzert static int ur_create_attributes(struct device *dev)
327810cb5b3SFrank Munzert {
328810cb5b3SFrank Munzert 	return device_create_file(dev, &dev_attr_reclen);
329810cb5b3SFrank Munzert }
330810cb5b3SFrank Munzert 
331810cb5b3SFrank Munzert static void ur_remove_attributes(struct device *dev)
332810cb5b3SFrank Munzert {
333810cb5b3SFrank Munzert 	device_remove_file(dev, &dev_attr_reclen);
334810cb5b3SFrank Munzert }
335810cb5b3SFrank Munzert 
336810cb5b3SFrank Munzert /*
337810cb5b3SFrank Munzert  * diagnose code 0x210 - retrieve device information
338810cb5b3SFrank Munzert  * cc=0  normal completion, we have a real device
339810cb5b3SFrank Munzert  * cc=1  CP paging error
340810cb5b3SFrank Munzert  * cc=2  The virtual device exists, but is not associated with a real device
341810cb5b3SFrank Munzert  * cc=3  Invalid device address, or the virtual device does not exist
342810cb5b3SFrank Munzert  */
343810cb5b3SFrank Munzert static int get_urd_class(struct urdev *urd)
344810cb5b3SFrank Munzert {
345810cb5b3SFrank Munzert 	static struct diag210 ur_diag210;
346810cb5b3SFrank Munzert 	int cc;
347810cb5b3SFrank Munzert 
348810cb5b3SFrank Munzert 	ur_diag210.vrdcdvno = urd->dev_id.devno;
349810cb5b3SFrank Munzert 	ur_diag210.vrdclen = sizeof(struct diag210);
350810cb5b3SFrank Munzert 
351810cb5b3SFrank Munzert 	cc = diag210(&ur_diag210);
352810cb5b3SFrank Munzert 	switch (cc) {
353810cb5b3SFrank Munzert 	case 0:
354b9993a38SFrank Munzert 		return -EOPNOTSUPP;
355810cb5b3SFrank Munzert 	case 2:
356810cb5b3SFrank Munzert 		return ur_diag210.vrdcvcla; /* virtual device class */
357810cb5b3SFrank Munzert 	case 3:
358810cb5b3SFrank Munzert 		return -ENODEV;
359810cb5b3SFrank Munzert 	default:
360810cb5b3SFrank Munzert 		return -EIO;
361810cb5b3SFrank Munzert 	}
362810cb5b3SFrank Munzert }
363810cb5b3SFrank Munzert 
364810cb5b3SFrank Munzert /*
365810cb5b3SFrank Munzert  * Allocation and freeing of urfile structures
366810cb5b3SFrank Munzert  */
367810cb5b3SFrank Munzert static struct urfile *urfile_alloc(struct urdev *urd)
368810cb5b3SFrank Munzert {
369810cb5b3SFrank Munzert 	struct urfile *urf;
370810cb5b3SFrank Munzert 
371810cb5b3SFrank Munzert 	urf = kzalloc(sizeof(struct urfile), GFP_KERNEL);
372810cb5b3SFrank Munzert 	if (!urf)
373810cb5b3SFrank Munzert 		return NULL;
374810cb5b3SFrank Munzert 	urf->urd = urd;
375810cb5b3SFrank Munzert 
376810cb5b3SFrank Munzert 	TRACE("urfile_alloc: urd=%p urf=%p rl=%zu\n", urd, urf,
377810cb5b3SFrank Munzert 	      urf->dev_reclen);
378810cb5b3SFrank Munzert 
379810cb5b3SFrank Munzert 	return urf;
380810cb5b3SFrank Munzert }
381810cb5b3SFrank Munzert 
382810cb5b3SFrank Munzert static void urfile_free(struct urfile *urf)
383810cb5b3SFrank Munzert {
384810cb5b3SFrank Munzert 	TRACE("urfile_free: urf=%p urd=%p\n", urf, urf->urd);
385810cb5b3SFrank Munzert 	kfree(urf);
386810cb5b3SFrank Munzert }
387810cb5b3SFrank Munzert 
388810cb5b3SFrank Munzert /*
389810cb5b3SFrank Munzert  * The fops implementation of the character device driver
390810cb5b3SFrank Munzert  */
391810cb5b3SFrank Munzert static ssize_t do_write(struct urdev *urd, const char __user *udata,
392810cb5b3SFrank Munzert 			size_t count, size_t reclen, loff_t *ppos)
393810cb5b3SFrank Munzert {
394810cb5b3SFrank Munzert 	struct ccw1 *cpa;
395810cb5b3SFrank Munzert 	int rc;
396810cb5b3SFrank Munzert 
3971eade380SMichael Holzheu 	cpa = alloc_chan_prog(udata, count / reclen, reclen);
3981eade380SMichael Holzheu 	if (IS_ERR(cpa))
3991eade380SMichael Holzheu 		return PTR_ERR(cpa);
400810cb5b3SFrank Munzert 
401810cb5b3SFrank Munzert 	rc = do_ur_io(urd, cpa);
402810cb5b3SFrank Munzert 	if (rc)
403810cb5b3SFrank Munzert 		goto fail_kfree_cpa;
404810cb5b3SFrank Munzert 
405810cb5b3SFrank Munzert 	if (urd->io_request_rc) {
406810cb5b3SFrank Munzert 		rc = urd->io_request_rc;
407810cb5b3SFrank Munzert 		goto fail_kfree_cpa;
408810cb5b3SFrank Munzert 	}
409810cb5b3SFrank Munzert 	*ppos += count;
410810cb5b3SFrank Munzert 	rc = count;
4111eade380SMichael Holzheu 
412810cb5b3SFrank Munzert fail_kfree_cpa:
4131eade380SMichael Holzheu 	free_chan_prog(cpa);
414810cb5b3SFrank Munzert 	return rc;
415810cb5b3SFrank Munzert }
416810cb5b3SFrank Munzert 
417810cb5b3SFrank Munzert static ssize_t ur_write(struct file *file, const char __user *udata,
418810cb5b3SFrank Munzert 			size_t count, loff_t *ppos)
419810cb5b3SFrank Munzert {
420810cb5b3SFrank Munzert 	struct urfile *urf = file->private_data;
421810cb5b3SFrank Munzert 
422810cb5b3SFrank Munzert 	TRACE("ur_write: count=%zu\n", count);
423810cb5b3SFrank Munzert 
424810cb5b3SFrank Munzert 	if (count == 0)
425810cb5b3SFrank Munzert 		return 0;
426810cb5b3SFrank Munzert 
427810cb5b3SFrank Munzert 	if (count % urf->dev_reclen)
428810cb5b3SFrank Munzert 		return -EINVAL;	/* count must be a multiple of reclen */
429810cb5b3SFrank Munzert 
430810cb5b3SFrank Munzert 	if (count > urf->dev_reclen * MAX_RECS_PER_IO)
431810cb5b3SFrank Munzert 		count = urf->dev_reclen * MAX_RECS_PER_IO;
432810cb5b3SFrank Munzert 
433810cb5b3SFrank Munzert 	return do_write(urf->urd, udata, count, urf->dev_reclen, ppos);
434810cb5b3SFrank Munzert }
435810cb5b3SFrank Munzert 
436810cb5b3SFrank Munzert /*
437810cb5b3SFrank Munzert  * diagnose code 0x14 subcode 0x0028 - position spool file to designated
438810cb5b3SFrank Munzert  *				       record
439810cb5b3SFrank Munzert  * cc=0  normal completion
440810cb5b3SFrank Munzert  * cc=2  no file active on the virtual reader or device not ready
441810cb5b3SFrank Munzert  * cc=3  record specified is beyond EOF
442810cb5b3SFrank Munzert  */
443810cb5b3SFrank Munzert static int diag_position_to_record(int devno, int record)
444810cb5b3SFrank Munzert {
445810cb5b3SFrank Munzert 	int cc;
446810cb5b3SFrank Munzert 
4470a87c5cfSMichael Holzheu 	cc = diag14(record, devno, 0x28);
448810cb5b3SFrank Munzert 	switch (cc) {
449810cb5b3SFrank Munzert 	case 0:
450810cb5b3SFrank Munzert 		return 0;
451810cb5b3SFrank Munzert 	case 2:
452810cb5b3SFrank Munzert 		return -ENOMEDIUM;
453810cb5b3SFrank Munzert 	case 3:
454810cb5b3SFrank Munzert 		return -ENODATA; /* position beyond end of file */
455810cb5b3SFrank Munzert 	default:
456810cb5b3SFrank Munzert 		return -EIO;
457810cb5b3SFrank Munzert 	}
458810cb5b3SFrank Munzert }
459810cb5b3SFrank Munzert 
460810cb5b3SFrank Munzert /*
461810cb5b3SFrank Munzert  * diagnose code 0x14 subcode 0x0000 - read next spool file buffer
462810cb5b3SFrank Munzert  * cc=0  normal completion
463810cb5b3SFrank Munzert  * cc=1  EOF reached
464810cb5b3SFrank Munzert  * cc=2  no file active on the virtual reader, and no file eligible
465810cb5b3SFrank Munzert  * cc=3  file already active on the virtual reader or specified virtual
466810cb5b3SFrank Munzert  *	 reader does not exist or is not a reader
467810cb5b3SFrank Munzert  */
468810cb5b3SFrank Munzert static int diag_read_file(int devno, char *buf)
469810cb5b3SFrank Munzert {
470810cb5b3SFrank Munzert 	int cc;
471810cb5b3SFrank Munzert 
4720a87c5cfSMichael Holzheu 	cc = diag14((unsigned long) buf, devno, 0x00);
473810cb5b3SFrank Munzert 	switch (cc) {
474810cb5b3SFrank Munzert 	case 0:
475810cb5b3SFrank Munzert 		return 0;
476810cb5b3SFrank Munzert 	case 1:
477810cb5b3SFrank Munzert 		return -ENODATA;
478810cb5b3SFrank Munzert 	case 2:
479810cb5b3SFrank Munzert 		return -ENOMEDIUM;
480810cb5b3SFrank Munzert 	default:
481810cb5b3SFrank Munzert 		return -EIO;
482810cb5b3SFrank Munzert 	}
483810cb5b3SFrank Munzert }
484810cb5b3SFrank Munzert 
485810cb5b3SFrank Munzert static ssize_t diag14_read(struct file *file, char __user *ubuf, size_t count,
486810cb5b3SFrank Munzert 			   loff_t *offs)
487810cb5b3SFrank Munzert {
488810cb5b3SFrank Munzert 	size_t len, copied, res;
489810cb5b3SFrank Munzert 	char *buf;
490810cb5b3SFrank Munzert 	int rc;
491810cb5b3SFrank Munzert 	u16 reclen;
492810cb5b3SFrank Munzert 	struct urdev *urd;
493810cb5b3SFrank Munzert 
494810cb5b3SFrank Munzert 	urd = ((struct urfile *) file->private_data)->urd;
495810cb5b3SFrank Munzert 	reclen = ((struct urfile *) file->private_data)->file_reclen;
496810cb5b3SFrank Munzert 
497810cb5b3SFrank Munzert 	rc = diag_position_to_record(urd->dev_id.devno, *offs / PAGE_SIZE + 1);
498810cb5b3SFrank Munzert 	if (rc == -ENODATA)
499810cb5b3SFrank Munzert 		return 0;
500810cb5b3SFrank Munzert 	if (rc)
501810cb5b3SFrank Munzert 		return rc;
502810cb5b3SFrank Munzert 
503810cb5b3SFrank Munzert 	len = min((size_t) PAGE_SIZE, count);
5043eed13ccSMichael Holzheu 	buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA);
505810cb5b3SFrank Munzert 	if (!buf)
506810cb5b3SFrank Munzert 		return -ENOMEM;
507810cb5b3SFrank Munzert 
508810cb5b3SFrank Munzert 	copied = 0;
509810cb5b3SFrank Munzert 	res = (size_t) (*offs % PAGE_SIZE);
510810cb5b3SFrank Munzert 	do {
511810cb5b3SFrank Munzert 		rc = diag_read_file(urd->dev_id.devno, buf);
512810cb5b3SFrank Munzert 		if (rc == -ENODATA) {
513810cb5b3SFrank Munzert 			break;
514810cb5b3SFrank Munzert 		}
515810cb5b3SFrank Munzert 		if (rc)
516810cb5b3SFrank Munzert 			goto fail;
5172b3d8c9eSFrank Munzert 		if (reclen && (copied == 0) && (*offs < PAGE_SIZE))
518810cb5b3SFrank Munzert 			*((u16 *) &buf[FILE_RECLEN_OFFSET]) = reclen;
519810cb5b3SFrank Munzert 		len = min(count - copied, PAGE_SIZE - res);
520810cb5b3SFrank Munzert 		if (copy_to_user(ubuf + copied, buf + res, len)) {
521810cb5b3SFrank Munzert 			rc = -EFAULT;
522810cb5b3SFrank Munzert 			goto fail;
523810cb5b3SFrank Munzert 		}
524810cb5b3SFrank Munzert 		res = 0;
525810cb5b3SFrank Munzert 		copied += len;
526810cb5b3SFrank Munzert 	} while (copied != count);
527810cb5b3SFrank Munzert 
528810cb5b3SFrank Munzert 	*offs += copied;
529810cb5b3SFrank Munzert 	rc = copied;
530810cb5b3SFrank Munzert fail:
5313eed13ccSMichael Holzheu 	free_page((unsigned long) buf);
532810cb5b3SFrank Munzert 	return rc;
533810cb5b3SFrank Munzert }
534810cb5b3SFrank Munzert 
535810cb5b3SFrank Munzert static ssize_t ur_read(struct file *file, char __user *ubuf, size_t count,
536810cb5b3SFrank Munzert 		       loff_t *offs)
537810cb5b3SFrank Munzert {
538810cb5b3SFrank Munzert 	struct urdev *urd;
539810cb5b3SFrank Munzert 	int rc;
540810cb5b3SFrank Munzert 
541810cb5b3SFrank Munzert 	TRACE("ur_read: count=%zu ppos=%li\n", count, (unsigned long) *offs);
542810cb5b3SFrank Munzert 
543810cb5b3SFrank Munzert 	if (count == 0)
544810cb5b3SFrank Munzert 		return 0;
545810cb5b3SFrank Munzert 
546810cb5b3SFrank Munzert 	urd = ((struct urfile *) file->private_data)->urd;
547810cb5b3SFrank Munzert 	rc = mutex_lock_interruptible(&urd->io_mutex);
548810cb5b3SFrank Munzert 	if (rc)
549810cb5b3SFrank Munzert 		return rc;
550810cb5b3SFrank Munzert 	rc = diag14_read(file, ubuf, count, offs);
551810cb5b3SFrank Munzert 	mutex_unlock(&urd->io_mutex);
552810cb5b3SFrank Munzert 	return rc;
553810cb5b3SFrank Munzert }
554810cb5b3SFrank Munzert 
555810cb5b3SFrank Munzert /*
556810cb5b3SFrank Munzert  * diagnose code 0x14 subcode 0x0fff - retrieve next file descriptor
557810cb5b3SFrank Munzert  * cc=0  normal completion
558810cb5b3SFrank Munzert  * cc=1  no files on reader queue or no subsequent file
559810cb5b3SFrank Munzert  * cc=2  spid specified is invalid
560810cb5b3SFrank Munzert  */
561810cb5b3SFrank Munzert static int diag_read_next_file_info(struct file_control_block *buf, int spid)
562810cb5b3SFrank Munzert {
563810cb5b3SFrank Munzert 	int cc;
564810cb5b3SFrank Munzert 
5650a87c5cfSMichael Holzheu 	cc = diag14((unsigned long) buf, spid, 0xfff);
566810cb5b3SFrank Munzert 	switch (cc) {
567810cb5b3SFrank Munzert 	case 0:
568810cb5b3SFrank Munzert 		return 0;
569810cb5b3SFrank Munzert 	default:
570810cb5b3SFrank Munzert 		return -ENODATA;
571810cb5b3SFrank Munzert 	}
572810cb5b3SFrank Munzert }
573810cb5b3SFrank Munzert 
5743eed13ccSMichael Holzheu static int verify_uri_device(struct urdev *urd)
575810cb5b3SFrank Munzert {
5763eed13ccSMichael Holzheu 	struct file_control_block *fcb;
577810cb5b3SFrank Munzert 	char *buf;
578810cb5b3SFrank Munzert 	int rc;
579810cb5b3SFrank Munzert 
5803eed13ccSMichael Holzheu 	fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA);
5813eed13ccSMichael Holzheu 	if (!fcb)
5823eed13ccSMichael Holzheu 		return -ENOMEM;
5833eed13ccSMichael Holzheu 
5843eed13ccSMichael Holzheu 	/* check for empty reader device (beginning of chain) */
5853eed13ccSMichael Holzheu 	rc = diag_read_next_file_info(fcb, 0);
5863eed13ccSMichael Holzheu 	if (rc)
5873eed13ccSMichael Holzheu 		goto fail_free_fcb;
5883eed13ccSMichael Holzheu 
5893eed13ccSMichael Holzheu 	/* if file is in hold status, we do not read it */
5903eed13ccSMichael Holzheu 	if (fcb->file_stat & (FLG_SYSTEM_HOLD | FLG_USER_HOLD)) {
5913eed13ccSMichael Holzheu 		rc = -EPERM;
5923eed13ccSMichael Holzheu 		goto fail_free_fcb;
5933eed13ccSMichael Holzheu 	}
5943eed13ccSMichael Holzheu 
5953eed13ccSMichael Holzheu 	/* open file on virtual reader	*/
5963eed13ccSMichael Holzheu 	buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA);
5973eed13ccSMichael Holzheu 	if (!buf) {
5983eed13ccSMichael Holzheu 		rc = -ENOMEM;
5993eed13ccSMichael Holzheu 		goto fail_free_fcb;
6003eed13ccSMichael Holzheu 	}
6013eed13ccSMichael Holzheu 	rc = diag_read_file(urd->dev_id.devno, buf);
6023eed13ccSMichael Holzheu 	if ((rc != 0) && (rc != -ENODATA)) /* EOF does not hurt */
6033eed13ccSMichael Holzheu 		goto fail_free_buf;
6043eed13ccSMichael Holzheu 
6053eed13ccSMichael Holzheu 	/* check if the file on top of the queue is open now */
6063eed13ccSMichael Holzheu 	rc = diag_read_next_file_info(fcb, 0);
6073eed13ccSMichael Holzheu 	if (rc)
6083eed13ccSMichael Holzheu 		goto fail_free_buf;
6093eed13ccSMichael Holzheu 	if (!(fcb->file_stat & FLG_IN_USE)) {
6103eed13ccSMichael Holzheu 		rc = -EMFILE;
6113eed13ccSMichael Holzheu 		goto fail_free_buf;
6123eed13ccSMichael Holzheu 	}
6133eed13ccSMichael Holzheu 	rc = 0;
6143eed13ccSMichael Holzheu 
6153eed13ccSMichael Holzheu fail_free_buf:
6163eed13ccSMichael Holzheu 	free_page((unsigned long) buf);
6173eed13ccSMichael Holzheu fail_free_fcb:
6183eed13ccSMichael Holzheu 	kfree(fcb);
6193eed13ccSMichael Holzheu 	return rc;
6203eed13ccSMichael Holzheu }
6213eed13ccSMichael Holzheu 
6223eed13ccSMichael Holzheu static int verify_device(struct urdev *urd)
6233eed13ccSMichael Holzheu {
624810cb5b3SFrank Munzert 	switch (urd->class) {
625810cb5b3SFrank Munzert 	case DEV_CLASS_UR_O:
626810cb5b3SFrank Munzert 		return 0; /* no check needed here */
627810cb5b3SFrank Munzert 	case DEV_CLASS_UR_I:
6283eed13ccSMichael Holzheu 		return verify_uri_device(urd);
629810cb5b3SFrank Munzert 	default:
630b9993a38SFrank Munzert 		return -EOPNOTSUPP;
631810cb5b3SFrank Munzert 	}
632810cb5b3SFrank Munzert }
633810cb5b3SFrank Munzert 
6343eed13ccSMichael Holzheu static int get_uri_file_reclen(struct urdev *urd)
6353eed13ccSMichael Holzheu {
6363eed13ccSMichael Holzheu 	struct file_control_block *fcb;
6373eed13ccSMichael Holzheu 	int rc;
6383eed13ccSMichael Holzheu 
6393eed13ccSMichael Holzheu 	fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA);
6403eed13ccSMichael Holzheu 	if (!fcb)
6413eed13ccSMichael Holzheu 		return -ENOMEM;
6423eed13ccSMichael Holzheu 	rc = diag_read_next_file_info(fcb, 0);
6433eed13ccSMichael Holzheu 	if (rc)
6443eed13ccSMichael Holzheu 		goto fail_free;
6453eed13ccSMichael Holzheu 	if (fcb->file_stat & FLG_CP_DUMP)
6463eed13ccSMichael Holzheu 		rc = 0;
6473eed13ccSMichael Holzheu 	else
6483eed13ccSMichael Holzheu 		rc = fcb->rec_len;
6493eed13ccSMichael Holzheu 
6503eed13ccSMichael Holzheu fail_free:
6513eed13ccSMichael Holzheu 	kfree(fcb);
6523eed13ccSMichael Holzheu 	return rc;
6533eed13ccSMichael Holzheu }
6543eed13ccSMichael Holzheu 
655810cb5b3SFrank Munzert static int get_file_reclen(struct urdev *urd)
656810cb5b3SFrank Munzert {
657810cb5b3SFrank Munzert 	switch (urd->class) {
658810cb5b3SFrank Munzert 	case DEV_CLASS_UR_O:
659810cb5b3SFrank Munzert 		return 0;
660810cb5b3SFrank Munzert 	case DEV_CLASS_UR_I:
6613eed13ccSMichael Holzheu 		return get_uri_file_reclen(urd);
662810cb5b3SFrank Munzert 	default:
663b9993a38SFrank Munzert 		return -EOPNOTSUPP;
664810cb5b3SFrank Munzert 	}
665810cb5b3SFrank Munzert }
666810cb5b3SFrank Munzert 
667810cb5b3SFrank Munzert static int ur_open(struct inode *inode, struct file *file)
668810cb5b3SFrank Munzert {
669810cb5b3SFrank Munzert 	u16 devno;
670810cb5b3SFrank Munzert 	struct urdev *urd;
671810cb5b3SFrank Munzert 	struct urfile *urf;
672810cb5b3SFrank Munzert 	unsigned short accmode;
673810cb5b3SFrank Munzert 	int rc;
674810cb5b3SFrank Munzert 
675810cb5b3SFrank Munzert 	accmode = file->f_flags & O_ACCMODE;
676810cb5b3SFrank Munzert 
677810cb5b3SFrank Munzert 	if (accmode == O_RDWR)
678810cb5b3SFrank Munzert 		return -EACCES;
679810cb5b3SFrank Munzert 	/*
680810cb5b3SFrank Munzert 	 * We treat the minor number as the devno of the ur device
681810cb5b3SFrank Munzert 	 * to find in the driver tree.
682810cb5b3SFrank Munzert 	 */
6836f24784fSAl Viro 	devno = iminor(file_inode(file));
684810cb5b3SFrank Munzert 
685810cb5b3SFrank Munzert 	urd = urdev_get_from_devno(devno);
686764a4a8eSJonathan Corbet 	if (!urd) {
687764a4a8eSJonathan Corbet 		rc = -ENXIO;
688764a4a8eSJonathan Corbet 		goto out;
689764a4a8eSJonathan Corbet 	}
690810cb5b3SFrank Munzert 
691a695f167SFrank Munzert 	spin_lock(&urd->open_lock);
692a695f167SFrank Munzert 	while (urd->open_flag) {
693a695f167SFrank Munzert 		spin_unlock(&urd->open_lock);
694810cb5b3SFrank Munzert 		if (file->f_flags & O_NONBLOCK) {
695810cb5b3SFrank Munzert 			rc = -EBUSY;
696810cb5b3SFrank Munzert 			goto fail_put;
697810cb5b3SFrank Munzert 		}
698a695f167SFrank Munzert 		if (wait_event_interruptible(urd->wait, urd->open_flag == 0)) {
699810cb5b3SFrank Munzert 			rc = -ERESTARTSYS;
700810cb5b3SFrank Munzert 			goto fail_put;
701810cb5b3SFrank Munzert 		}
702a695f167SFrank Munzert 		spin_lock(&urd->open_lock);
703810cb5b3SFrank Munzert 	}
704a695f167SFrank Munzert 	urd->open_flag++;
705a695f167SFrank Munzert 	spin_unlock(&urd->open_lock);
706810cb5b3SFrank Munzert 
707810cb5b3SFrank Munzert 	TRACE("ur_open\n");
708810cb5b3SFrank Munzert 
709810cb5b3SFrank Munzert 	if (((accmode == O_RDONLY) && (urd->class != DEV_CLASS_UR_I)) ||
710810cb5b3SFrank Munzert 	    ((accmode == O_WRONLY) && (urd->class != DEV_CLASS_UR_O))) {
711810cb5b3SFrank Munzert 		TRACE("ur_open: unsupported dev class (%d)\n", urd->class);
712810cb5b3SFrank Munzert 		rc = -EACCES;
713810cb5b3SFrank Munzert 		goto fail_unlock;
714810cb5b3SFrank Munzert 	}
715810cb5b3SFrank Munzert 
716810cb5b3SFrank Munzert 	rc = verify_device(urd);
717810cb5b3SFrank Munzert 	if (rc)
718810cb5b3SFrank Munzert 		goto fail_unlock;
719810cb5b3SFrank Munzert 
720810cb5b3SFrank Munzert 	urf = urfile_alloc(urd);
721810cb5b3SFrank Munzert 	if (!urf) {
722810cb5b3SFrank Munzert 		rc = -ENOMEM;
723810cb5b3SFrank Munzert 		goto fail_unlock;
724810cb5b3SFrank Munzert 	}
725810cb5b3SFrank Munzert 
726810cb5b3SFrank Munzert 	urf->dev_reclen = urd->reclen;
727810cb5b3SFrank Munzert 	rc = get_file_reclen(urd);
728810cb5b3SFrank Munzert 	if (rc < 0)
729810cb5b3SFrank Munzert 		goto fail_urfile_free;
730810cb5b3SFrank Munzert 	urf->file_reclen = rc;
731810cb5b3SFrank Munzert 	file->private_data = urf;
732810cb5b3SFrank Munzert 	return 0;
733810cb5b3SFrank Munzert 
734810cb5b3SFrank Munzert fail_urfile_free:
735810cb5b3SFrank Munzert 	urfile_free(urf);
736810cb5b3SFrank Munzert fail_unlock:
737a695f167SFrank Munzert 	spin_lock(&urd->open_lock);
738a695f167SFrank Munzert 	urd->open_flag--;
739a695f167SFrank Munzert 	spin_unlock(&urd->open_lock);
740810cb5b3SFrank Munzert fail_put:
741810cb5b3SFrank Munzert 	urdev_put(urd);
742764a4a8eSJonathan Corbet out:
743810cb5b3SFrank Munzert 	return rc;
744810cb5b3SFrank Munzert }
745810cb5b3SFrank Munzert 
746810cb5b3SFrank Munzert static int ur_release(struct inode *inode, struct file *file)
747810cb5b3SFrank Munzert {
748810cb5b3SFrank Munzert 	struct urfile *urf = file->private_data;
749810cb5b3SFrank Munzert 
750810cb5b3SFrank Munzert 	TRACE("ur_release\n");
751a695f167SFrank Munzert 	spin_lock(&urf->urd->open_lock);
752a695f167SFrank Munzert 	urf->urd->open_flag--;
753a695f167SFrank Munzert 	spin_unlock(&urf->urd->open_lock);
754a695f167SFrank Munzert 	wake_up_interruptible(&urf->urd->wait);
755810cb5b3SFrank Munzert 	urdev_put(urf->urd);
756810cb5b3SFrank Munzert 	urfile_free(urf);
757810cb5b3SFrank Munzert 	return 0;
758810cb5b3SFrank Munzert }
759810cb5b3SFrank Munzert 
760810cb5b3SFrank Munzert static loff_t ur_llseek(struct file *file, loff_t offset, int whence)
761810cb5b3SFrank Munzert {
762810cb5b3SFrank Munzert 	if ((file->f_flags & O_ACCMODE) != O_RDONLY)
763810cb5b3SFrank Munzert 		return -ESPIPE; /* seek allowed only for reader */
764810cb5b3SFrank Munzert 	if (offset % PAGE_SIZE)
765810cb5b3SFrank Munzert 		return -ESPIPE; /* only multiples of 4K allowed */
766b25472f9SAl Viro 	return no_seek_end_llseek(file, offset, whence);
767810cb5b3SFrank Munzert }
768810cb5b3SFrank Munzert 
7695c81cdbeSJan Engelhardt static const struct file_operations ur_fops = {
770810cb5b3SFrank Munzert 	.owner	 = THIS_MODULE,
771810cb5b3SFrank Munzert 	.open	 = ur_open,
772810cb5b3SFrank Munzert 	.release = ur_release,
773810cb5b3SFrank Munzert 	.read	 = ur_read,
774810cb5b3SFrank Munzert 	.write	 = ur_write,
775810cb5b3SFrank Munzert 	.llseek  = ur_llseek,
776810cb5b3SFrank Munzert };
777810cb5b3SFrank Munzert 
778810cb5b3SFrank Munzert /*
779810cb5b3SFrank Munzert  * ccw_device infrastructure:
7808127a1f8SMichael Holzheu  *     ur_probe creates the struct urdev (with refcount = 1), the device
7818127a1f8SMichael Holzheu  *     attributes, sets up the interrupt handler and validates the virtual
7828127a1f8SMichael Holzheu  *     unit record device.
7838127a1f8SMichael Holzheu  *     ur_remove removes the device attributes and drops the reference to
7848127a1f8SMichael Holzheu  *     struct urdev.
7858127a1f8SMichael Holzheu  *
7868127a1f8SMichael Holzheu  *     ur_probe, ur_remove, ur_set_online and ur_set_offline are serialized
7878127a1f8SMichael Holzheu  *     by the vmur_mutex lock.
7888127a1f8SMichael Holzheu  *
7898127a1f8SMichael Holzheu  *     urd->char_device is used as indication that the online function has
7908127a1f8SMichael Holzheu  *     been completed successfully.
791810cb5b3SFrank Munzert  */
792810cb5b3SFrank Munzert static int ur_probe(struct ccw_device *cdev)
793810cb5b3SFrank Munzert {
794810cb5b3SFrank Munzert 	struct urdev *urd;
795810cb5b3SFrank Munzert 	int rc;
796810cb5b3SFrank Munzert 
7978127a1f8SMichael Holzheu 	TRACE("ur_probe: cdev=%p\n", cdev);
798810cb5b3SFrank Munzert 
7998127a1f8SMichael Holzheu 	mutex_lock(&vmur_mutex);
800810cb5b3SFrank Munzert 	urd = urdev_alloc(cdev);
801810cb5b3SFrank Munzert 	if (!urd) {
802810cb5b3SFrank Munzert 		rc = -ENOMEM;
8038127a1f8SMichael Holzheu 		goto fail_unlock;
804810cb5b3SFrank Munzert 	}
8058127a1f8SMichael Holzheu 
806810cb5b3SFrank Munzert 	rc = ur_create_attributes(&cdev->dev);
807810cb5b3SFrank Munzert 	if (rc) {
808810cb5b3SFrank Munzert 		rc = -ENOMEM;
8098127a1f8SMichael Holzheu 		goto fail_urdev_put;
810810cb5b3SFrank Munzert 	}
811810cb5b3SFrank Munzert 
812810cb5b3SFrank Munzert 	/* validate virtual unit record device */
813810cb5b3SFrank Munzert 	urd->class = get_urd_class(urd);
814810cb5b3SFrank Munzert 	if (urd->class < 0) {
815810cb5b3SFrank Munzert 		rc = urd->class;
8168127a1f8SMichael Holzheu 		goto fail_remove_attr;
817810cb5b3SFrank Munzert 	}
818810cb5b3SFrank Munzert 	if ((urd->class != DEV_CLASS_UR_I) && (urd->class != DEV_CLASS_UR_O)) {
819b9993a38SFrank Munzert 		rc = -EOPNOTSUPP;
8208127a1f8SMichael Holzheu 		goto fail_remove_attr;
821810cb5b3SFrank Munzert 	}
8228127a1f8SMichael Holzheu 	spin_lock_irq(get_ccwdev_lock(cdev));
823dff59b64SGreg Kroah-Hartman 	dev_set_drvdata(&cdev->dev, urd);
824*f3e59ff3SPeter Oberparleiter 	cdev->handler = ur_int_handler;
8258127a1f8SMichael Holzheu 	spin_unlock_irq(get_ccwdev_lock(cdev));
826810cb5b3SFrank Munzert 
8278127a1f8SMichael Holzheu 	mutex_unlock(&vmur_mutex);
828810cb5b3SFrank Munzert 	return 0;
829810cb5b3SFrank Munzert 
8308127a1f8SMichael Holzheu fail_remove_attr:
831810cb5b3SFrank Munzert 	ur_remove_attributes(&cdev->dev);
8328127a1f8SMichael Holzheu fail_urdev_put:
8338127a1f8SMichael Holzheu 	urdev_put(urd);
8348127a1f8SMichael Holzheu fail_unlock:
8358127a1f8SMichael Holzheu 	mutex_unlock(&vmur_mutex);
8368127a1f8SMichael Holzheu 	return rc;
837810cb5b3SFrank Munzert }
838810cb5b3SFrank Munzert 
839810cb5b3SFrank Munzert static int ur_set_online(struct ccw_device *cdev)
840810cb5b3SFrank Munzert {
841810cb5b3SFrank Munzert 	struct urdev *urd;
842810cb5b3SFrank Munzert 	int minor, major, rc;
843810cb5b3SFrank Munzert 	char node_id[16];
844810cb5b3SFrank Munzert 
8458127a1f8SMichael Holzheu 	TRACE("ur_set_online: cdev=%p\n", cdev);
846810cb5b3SFrank Munzert 
8478127a1f8SMichael Holzheu 	mutex_lock(&vmur_mutex);
8488127a1f8SMichael Holzheu 	urd = urdev_get_from_cdev(cdev);
8498127a1f8SMichael Holzheu 	if (!urd) {
8508127a1f8SMichael Holzheu 		/* ur_remove already deleted our urd */
8518127a1f8SMichael Holzheu 		rc = -ENODEV;
8528127a1f8SMichael Holzheu 		goto fail_unlock;
8538127a1f8SMichael Holzheu 	}
854810cb5b3SFrank Munzert 
8558127a1f8SMichael Holzheu 	if (urd->char_device) {
8568127a1f8SMichael Holzheu 		/* Another ur_set_online was faster */
8578127a1f8SMichael Holzheu 		rc = -EBUSY;
8588127a1f8SMichael Holzheu 		goto fail_urdev_put;
8598127a1f8SMichael Holzheu 	}
8608127a1f8SMichael Holzheu 
861810cb5b3SFrank Munzert 	minor = urd->dev_id.devno;
862810cb5b3SFrank Munzert 	major = MAJOR(ur_first_dev_maj_min);
863810cb5b3SFrank Munzert 
864810cb5b3SFrank Munzert 	urd->char_device = cdev_alloc();
865810cb5b3SFrank Munzert 	if (!urd->char_device) {
866810cb5b3SFrank Munzert 		rc = -ENOMEM;
8678127a1f8SMichael Holzheu 		goto fail_urdev_put;
868810cb5b3SFrank Munzert 	}
869810cb5b3SFrank Munzert 
870dae1df42SDennis Chen 	urd->char_device->ops = &ur_fops;
871810cb5b3SFrank Munzert 	urd->char_device->owner = ur_fops.owner;
872810cb5b3SFrank Munzert 
873b08e19deSJean Delvare 	rc = cdev_add(urd->char_device, MKDEV(major, minor), 1);
874810cb5b3SFrank Munzert 	if (rc)
875810cb5b3SFrank Munzert 		goto fail_free_cdev;
876810cb5b3SFrank Munzert 	if (urd->cdev->id.cu_type == READER_PUNCH_DEVTYPE) {
877810cb5b3SFrank Munzert 		if (urd->class == DEV_CLASS_UR_I)
8782a0217d5SKay Sievers 			sprintf(node_id, "vmrdr-%s", dev_name(&cdev->dev));
879810cb5b3SFrank Munzert 		if (urd->class == DEV_CLASS_UR_O)
8802a0217d5SKay Sievers 			sprintf(node_id, "vmpun-%s", dev_name(&cdev->dev));
881810cb5b3SFrank Munzert 	} else if (urd->cdev->id.cu_type == PRINTER_DEVTYPE) {
8822a0217d5SKay Sievers 		sprintf(node_id, "vmprt-%s", dev_name(&cdev->dev));
883810cb5b3SFrank Munzert 	} else {
884b9993a38SFrank Munzert 		rc = -EOPNOTSUPP;
885810cb5b3SFrank Munzert 		goto fail_free_cdev;
886810cb5b3SFrank Munzert 	}
887810cb5b3SFrank Munzert 
888964c150eSHendrik Brueckner 	urd->device = device_create(vmur_class, &cdev->dev,
889964c150eSHendrik Brueckner 				    urd->char_device->dev, NULL, "%s", node_id);
890810cb5b3SFrank Munzert 	if (IS_ERR(urd->device)) {
891810cb5b3SFrank Munzert 		rc = PTR_ERR(urd->device);
892810cb5b3SFrank Munzert 		TRACE("ur_set_online: device_create rc=%d\n", rc);
893810cb5b3SFrank Munzert 		goto fail_free_cdev;
894810cb5b3SFrank Munzert 	}
8958127a1f8SMichael Holzheu 	urdev_put(urd);
8968127a1f8SMichael Holzheu 	mutex_unlock(&vmur_mutex);
897810cb5b3SFrank Munzert 	return 0;
898810cb5b3SFrank Munzert 
899810cb5b3SFrank Munzert fail_free_cdev:
900810cb5b3SFrank Munzert 	cdev_del(urd->char_device);
9018127a1f8SMichael Holzheu 	urd->char_device = NULL;
9028127a1f8SMichael Holzheu fail_urdev_put:
9038127a1f8SMichael Holzheu 	urdev_put(urd);
9048127a1f8SMichael Holzheu fail_unlock:
9058127a1f8SMichael Holzheu 	mutex_unlock(&vmur_mutex);
9068127a1f8SMichael Holzheu 	return rc;
9078127a1f8SMichael Holzheu }
908810cb5b3SFrank Munzert 
9098127a1f8SMichael Holzheu static int ur_set_offline_force(struct ccw_device *cdev, int force)
9108127a1f8SMichael Holzheu {
9118127a1f8SMichael Holzheu 	struct urdev *urd;
9128127a1f8SMichael Holzheu 	int rc;
9138127a1f8SMichael Holzheu 
9148127a1f8SMichael Holzheu 	TRACE("ur_set_offline: cdev=%p\n", cdev);
9158127a1f8SMichael Holzheu 	urd = urdev_get_from_cdev(cdev);
9168127a1f8SMichael Holzheu 	if (!urd)
9178127a1f8SMichael Holzheu 		/* ur_remove already deleted our urd */
9188127a1f8SMichael Holzheu 		return -ENODEV;
9198127a1f8SMichael Holzheu 	if (!urd->char_device) {
9208127a1f8SMichael Holzheu 		/* Another ur_set_offline was faster */
9218127a1f8SMichael Holzheu 		rc = -EBUSY;
9228127a1f8SMichael Holzheu 		goto fail_urdev_put;
9238127a1f8SMichael Holzheu 	}
9240462d992SElena Reshetova 	if (!force && (refcount_read(&urd->ref_count) > 2)) {
9258127a1f8SMichael Holzheu 		/* There is still a user of urd (e.g. ur_open) */
9268127a1f8SMichael Holzheu 		TRACE("ur_set_offline: BUSY\n");
9278127a1f8SMichael Holzheu 		rc = -EBUSY;
9288127a1f8SMichael Holzheu 		goto fail_urdev_put;
9298127a1f8SMichael Holzheu 	}
9308127a1f8SMichael Holzheu 	device_destroy(vmur_class, urd->char_device->dev);
9318127a1f8SMichael Holzheu 	cdev_del(urd->char_device);
9328127a1f8SMichael Holzheu 	urd->char_device = NULL;
9338127a1f8SMichael Holzheu 	rc = 0;
9348127a1f8SMichael Holzheu 
9358127a1f8SMichael Holzheu fail_urdev_put:
9368127a1f8SMichael Holzheu 	urdev_put(urd);
937810cb5b3SFrank Munzert 	return rc;
938810cb5b3SFrank Munzert }
939810cb5b3SFrank Munzert 
940810cb5b3SFrank Munzert static int ur_set_offline(struct ccw_device *cdev)
941810cb5b3SFrank Munzert {
9428127a1f8SMichael Holzheu 	int rc;
943810cb5b3SFrank Munzert 
9448127a1f8SMichael Holzheu 	mutex_lock(&vmur_mutex);
9458127a1f8SMichael Holzheu 	rc = ur_set_offline_force(cdev, 0);
9468127a1f8SMichael Holzheu 	mutex_unlock(&vmur_mutex);
9478127a1f8SMichael Holzheu 	return rc;
9488127a1f8SMichael Holzheu }
949810cb5b3SFrank Munzert 
9508127a1f8SMichael Holzheu static void ur_remove(struct ccw_device *cdev)
9518127a1f8SMichael Holzheu {
9528127a1f8SMichael Holzheu 	unsigned long flags;
9538127a1f8SMichael Holzheu 
9548127a1f8SMichael Holzheu 	TRACE("ur_remove\n");
9558127a1f8SMichael Holzheu 
9568127a1f8SMichael Holzheu 	mutex_lock(&vmur_mutex);
9578127a1f8SMichael Holzheu 
9588127a1f8SMichael Holzheu 	if (cdev->online)
9598127a1f8SMichael Holzheu 		ur_set_offline_force(cdev, 1);
9608127a1f8SMichael Holzheu 	ur_remove_attributes(&cdev->dev);
9618127a1f8SMichael Holzheu 
9628127a1f8SMichael Holzheu 	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
963dff59b64SGreg Kroah-Hartman 	urdev_put(dev_get_drvdata(&cdev->dev));
964dff59b64SGreg Kroah-Hartman 	dev_set_drvdata(&cdev->dev, NULL);
965*f3e59ff3SPeter Oberparleiter 	cdev->handler = NULL;
9668127a1f8SMichael Holzheu 	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
9678127a1f8SMichael Holzheu 
9688127a1f8SMichael Holzheu 	mutex_unlock(&vmur_mutex);
969810cb5b3SFrank Munzert }
970810cb5b3SFrank Munzert 
971810cb5b3SFrank Munzert /*
972810cb5b3SFrank Munzert  * Module initialisation and cleanup
973810cb5b3SFrank Munzert  */
974810cb5b3SFrank Munzert static int __init ur_init(void)
975810cb5b3SFrank Munzert {
976810cb5b3SFrank Munzert 	int rc;
977810cb5b3SFrank Munzert 	dev_t dev;
978810cb5b3SFrank Munzert 
979810cb5b3SFrank Munzert 	if (!MACHINE_IS_VM) {
98080a5c36dSFrank Munzert 		pr_err("The %s cannot be loaded without z/VM\n",
98180a5c36dSFrank Munzert 		       ur_banner);
982810cb5b3SFrank Munzert 		return -ENODEV;
983810cb5b3SFrank Munzert 	}
984810cb5b3SFrank Munzert 
985810cb5b3SFrank Munzert 	vmur_dbf = debug_register("vmur", 4, 1, 4 * sizeof(long));
986810cb5b3SFrank Munzert 	if (!vmur_dbf)
987810cb5b3SFrank Munzert 		return -ENOMEM;
988810cb5b3SFrank Munzert 	rc = debug_register_view(vmur_dbf, &debug_sprintf_view);
989810cb5b3SFrank Munzert 	if (rc)
990810cb5b3SFrank Munzert 		goto fail_free_dbf;
991810cb5b3SFrank Munzert 
992810cb5b3SFrank Munzert 	debug_set_level(vmur_dbf, 6);
993810cb5b3SFrank Munzert 
9943f7cb51fSMichael Holzheu 	vmur_class = class_create(THIS_MODULE, "vmur");
9953f7cb51fSMichael Holzheu 	if (IS_ERR(vmur_class)) {
9963f7cb51fSMichael Holzheu 		rc = PTR_ERR(vmur_class);
9973f7cb51fSMichael Holzheu 		goto fail_free_dbf;
9983f7cb51fSMichael Holzheu 	}
9993f7cb51fSMichael Holzheu 
1000810cb5b3SFrank Munzert 	rc = ccw_driver_register(&ur_driver);
1001810cb5b3SFrank Munzert 	if (rc)
10023f7cb51fSMichael Holzheu 		goto fail_class_destroy;
1003810cb5b3SFrank Munzert 
1004810cb5b3SFrank Munzert 	rc = alloc_chrdev_region(&dev, 0, NUM_MINORS, "vmur");
1005810cb5b3SFrank Munzert 	if (rc) {
100680a5c36dSFrank Munzert 		pr_err("Kernel function alloc_chrdev_region failed with "
100780a5c36dSFrank Munzert 		       "error code %d\n", rc);
1008810cb5b3SFrank Munzert 		goto fail_unregister_driver;
1009810cb5b3SFrank Munzert 	}
1010810cb5b3SFrank Munzert 	ur_first_dev_maj_min = MKDEV(MAJOR(dev), 0);
1011810cb5b3SFrank Munzert 
101280a5c36dSFrank Munzert 	pr_info("%s loaded.\n", ur_banner);
1013810cb5b3SFrank Munzert 	return 0;
1014810cb5b3SFrank Munzert 
1015810cb5b3SFrank Munzert fail_unregister_driver:
1016810cb5b3SFrank Munzert 	ccw_driver_unregister(&ur_driver);
10173f7cb51fSMichael Holzheu fail_class_destroy:
10183f7cb51fSMichael Holzheu 	class_destroy(vmur_class);
1019810cb5b3SFrank Munzert fail_free_dbf:
1020810cb5b3SFrank Munzert 	debug_unregister(vmur_dbf);
1021810cb5b3SFrank Munzert 	return rc;
1022810cb5b3SFrank Munzert }
1023810cb5b3SFrank Munzert 
1024810cb5b3SFrank Munzert static void __exit ur_exit(void)
1025810cb5b3SFrank Munzert {
1026810cb5b3SFrank Munzert 	unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS);
1027810cb5b3SFrank Munzert 	ccw_driver_unregister(&ur_driver);
10283f7cb51fSMichael Holzheu 	class_destroy(vmur_class);
1029810cb5b3SFrank Munzert 	debug_unregister(vmur_dbf);
103080a5c36dSFrank Munzert 	pr_info("%s unloaded.\n", ur_banner);
1031810cb5b3SFrank Munzert }
1032810cb5b3SFrank Munzert 
1033810cb5b3SFrank Munzert module_init(ur_init);
1034810cb5b3SFrank Munzert module_exit(ur_exit);
1035