blob: c1f352b84868c16affd93a7ff7af1f162527eb2b [file] [log] [blame]
Frank Munzert810cb5b2007-07-17 13:36:06 +02001/*
2 * Linux driver for System z and s390 unit record devices
3 * (z/VM virtual punch, reader, printer)
4 *
5 * Copyright IBM Corp. 2001, 2007
6 * Authors: Malcolm Beattie <beattiem@uk.ibm.com>
7 * Michael Holzheu <holzheu@de.ibm.com>
8 * Frank Munzert <munzert@de.ibm.com>
9 */
10
11#include <linux/cdev.h>
Jonathan Corbet764a4a82008-05-15 10:01:17 -060012#include <linux/smp_lock.h>
Frank Munzert810cb5b2007-07-17 13:36:06 +020013
14#include <asm/uaccess.h>
15#include <asm/cio.h>
16#include <asm/ccwdev.h>
17#include <asm/debug.h>
Michael Holzheu0a87c5c2007-08-22 13:51:40 +020018#include <asm/diag.h>
Frank Munzert810cb5b2007-07-17 13:36:06 +020019
20#include "vmur.h"
21
22/*
23 * Driver overview
24 *
25 * Unit record device support is implemented as a character device driver.
26 * We can fit at least 16 bits into a device minor number and use the
27 * simple method of mapping a character device number with minor abcd
28 * to the unit record device with devno abcd.
29 * I/O to virtual unit record devices is handled as follows:
30 * Reads: Diagnose code 0x14 (input spool file manipulation)
31 * is used to read spool data page-wise.
32 * Writes: The CCW used is WRITE_CCW_CMD (0x01). The device's record length
33 * is available by reading sysfs attr reclen. Each write() to the device
34 * must specify an integral multiple (maximal 511) of reclen.
35 */
36
37static char ur_banner[] = "z/VM virtual unit record device driver";
38
39MODULE_AUTHOR("IBM Corporation");
40MODULE_DESCRIPTION("s390 z/VM virtual unit record device driver");
41MODULE_LICENSE("GPL");
42
43#define PRINTK_HEADER "vmur: "
44
45static dev_t ur_first_dev_maj_min;
46static struct class *vmur_class;
47static struct debug_info *vmur_dbf;
48
49/* We put the device's record length (for writes) in the driver_info field */
50static struct ccw_device_id ur_ids[] = {
51 { CCWDEV_CU_DI(READER_PUNCH_DEVTYPE, 80) },
52 { CCWDEV_CU_DI(PRINTER_DEVTYPE, 132) },
53 { /* end of list */ }
54};
55
56MODULE_DEVICE_TABLE(ccw, ur_ids);
57
58static int ur_probe(struct ccw_device *cdev);
59static void ur_remove(struct ccw_device *cdev);
60static int ur_set_online(struct ccw_device *cdev);
61static int ur_set_offline(struct ccw_device *cdev);
62
63static struct ccw_driver ur_driver = {
64 .name = "vmur",
65 .owner = THIS_MODULE,
66 .ids = ur_ids,
67 .probe = ur_probe,
68 .remove = ur_remove,
69 .set_online = ur_set_online,
70 .set_offline = ur_set_offline,
71};
72
Michael Holzheu8127a1f2007-08-22 13:51:41 +020073static DEFINE_MUTEX(vmur_mutex);
74
Frank Munzert810cb5b2007-07-17 13:36:06 +020075/*
76 * Allocation, freeing, getting and putting of urdev structures
Michael Holzheu8127a1f2007-08-22 13:51:41 +020077 *
78 * Each ur device (urd) contains a reference to its corresponding ccw device
79 * (cdev) using the urd->cdev pointer. Each ccw device has a reference to the
80 * ur device using the cdev->dev.driver_data pointer.
81 *
82 * urd references:
83 * - ur_probe gets a urd reference, ur_remove drops the reference
84 * (cdev->dev.driver_data)
85 * - ur_open gets a urd reference, ur_relase drops the reference
86 * (urf->urd)
87 *
88 * cdev references:
89 * - urdev_alloc get a cdev reference (urd->cdev)
90 * - urdev_free drops the cdev reference (urd->cdev)
91 *
92 * Setting and clearing of cdev->dev.driver_data is protected by the ccwdev lock
Frank Munzert810cb5b2007-07-17 13:36:06 +020093 */
94static struct urdev *urdev_alloc(struct ccw_device *cdev)
95{
96 struct urdev *urd;
97
98 urd = kzalloc(sizeof(struct urdev), GFP_KERNEL);
99 if (!urd)
100 return NULL;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200101 urd->reclen = cdev->id.driver_info;
102 ccw_device_get_id(cdev, &urd->dev_id);
103 mutex_init(&urd->io_mutex);
Frank Munzerta695f162008-04-17 07:46:06 +0200104 init_waitqueue_head(&urd->wait);
105 spin_lock_init(&urd->open_lock);
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200106 atomic_set(&urd->ref_count, 1);
107 urd->cdev = cdev;
108 get_device(&cdev->dev);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200109 return urd;
110}
111
112static void urdev_free(struct urdev *urd)
113{
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200114 TRACE("urdev_free: %p\n", urd);
115 if (urd->cdev)
116 put_device(&urd->cdev->dev);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200117 kfree(urd);
118}
119
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200120static void urdev_get(struct urdev *urd)
121{
122 atomic_inc(&urd->ref_count);
123}
124
125static struct urdev *urdev_get_from_cdev(struct ccw_device *cdev)
126{
127 struct urdev *urd;
128 unsigned long flags;
129
130 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
131 urd = cdev->dev.driver_data;
132 if (urd)
133 urdev_get(urd);
134 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
135 return urd;
136}
137
Frank Munzert810cb5b2007-07-17 13:36:06 +0200138static struct urdev *urdev_get_from_devno(u16 devno)
139{
140 char bus_id[16];
141 struct ccw_device *cdev;
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200142 struct urdev *urd;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200143
144 sprintf(bus_id, "0.0.%04x", devno);
145 cdev = get_ccwdev_by_busid(&ur_driver, bus_id);
146 if (!cdev)
147 return NULL;
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200148 urd = urdev_get_from_cdev(cdev);
149 put_device(&cdev->dev);
150 return urd;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200151}
152
153static void urdev_put(struct urdev *urd)
154{
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200155 if (atomic_dec_and_test(&urd->ref_count))
156 urdev_free(urd);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200157}
158
159/*
160 * Low-level functions to do I/O to a ur device.
161 * alloc_chan_prog
Michael Holzheu1eade382007-08-10 14:32:30 +0200162 * free_chan_prog
Frank Munzert810cb5b2007-07-17 13:36:06 +0200163 * do_ur_io
164 * ur_int_handler
165 *
166 * alloc_chan_prog allocates and builds the channel program
Michael Holzheu1eade382007-08-10 14:32:30 +0200167 * free_chan_prog frees memory of the channel program
Frank Munzert810cb5b2007-07-17 13:36:06 +0200168 *
169 * do_ur_io issues the channel program to the device and blocks waiting
170 * on a completion event it publishes at urd->io_done. The function
171 * serialises itself on the device's mutex so that only one I/O
172 * is issued at a time (and that I/O is synchronous).
173 *
174 * ur_int_handler catches the "I/O done" interrupt, writes the
175 * subchannel status word into the scsw member of the urdev structure
176 * and complete()s the io_done to wake the waiting do_ur_io.
177 *
178 * The caller of do_ur_io is responsible for kfree()ing the channel program
179 * address pointer that alloc_chan_prog returned.
180 */
181
Michael Holzheu1eade382007-08-10 14:32:30 +0200182static void free_chan_prog(struct ccw1 *cpa)
183{
184 struct ccw1 *ptr = cpa;
185
186 while (ptr->cda) {
187 kfree((void *)(addr_t) ptr->cda);
188 ptr++;
189 }
190 kfree(cpa);
191}
Frank Munzert810cb5b2007-07-17 13:36:06 +0200192
193/*
194 * alloc_chan_prog
195 * The channel program we use is write commands chained together
196 * with a final NOP CCW command-chained on (which ensures that CE and DE
197 * are presented together in a single interrupt instead of as separate
198 * interrupts unless an incorrect length indication kicks in first). The
Michael Holzheu1eade382007-08-10 14:32:30 +0200199 * data length in each CCW is reclen.
Frank Munzert810cb5b2007-07-17 13:36:06 +0200200 */
Michael Holzheu1eade382007-08-10 14:32:30 +0200201static struct ccw1 *alloc_chan_prog(const char __user *ubuf, int rec_count,
202 int reclen)
Frank Munzert810cb5b2007-07-17 13:36:06 +0200203{
Frank Munzert810cb5b2007-07-17 13:36:06 +0200204 struct ccw1 *cpa;
Michael Holzheu1eade382007-08-10 14:32:30 +0200205 void *kbuf;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200206 int i;
207
Michael Holzheu1eade382007-08-10 14:32:30 +0200208 TRACE("alloc_chan_prog(%p, %i, %i)\n", ubuf, rec_count, reclen);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200209
210 /*
211 * We chain a NOP onto the writes to force CE+DE together.
212 * That means we allocate room for CCWs to cover count/reclen
213 * records plus a NOP.
214 */
Michael Holzheu1eade382007-08-10 14:32:30 +0200215 cpa = kzalloc((rec_count + 1) * sizeof(struct ccw1),
216 GFP_KERNEL | GFP_DMA);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200217 if (!cpa)
Michael Holzheu1eade382007-08-10 14:32:30 +0200218 return ERR_PTR(-ENOMEM);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200219
Michael Holzheu1eade382007-08-10 14:32:30 +0200220 for (i = 0; i < rec_count; i++) {
Frank Munzert810cb5b2007-07-17 13:36:06 +0200221 cpa[i].cmd_code = WRITE_CCW_CMD;
222 cpa[i].flags = CCW_FLAG_CC | CCW_FLAG_SLI;
223 cpa[i].count = reclen;
Michael Holzheu1eade382007-08-10 14:32:30 +0200224 kbuf = kmalloc(reclen, GFP_KERNEL | GFP_DMA);
225 if (!kbuf) {
226 free_chan_prog(cpa);
227 return ERR_PTR(-ENOMEM);
228 }
229 cpa[i].cda = (u32)(addr_t) kbuf;
230 if (copy_from_user(kbuf, ubuf, reclen)) {
231 free_chan_prog(cpa);
232 return ERR_PTR(-EFAULT);
233 }
234 ubuf += reclen;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200235 }
236 /* The following NOP CCW forces CE+DE to be presented together */
237 cpa[i].cmd_code = CCW_CMD_NOOP;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200238 return cpa;
239}
240
241static int do_ur_io(struct urdev *urd, struct ccw1 *cpa)
242{
243 int rc;
244 struct ccw_device *cdev = urd->cdev;
Heiko Carstens278bc682007-08-10 14:32:31 +0200245 DECLARE_COMPLETION_ONSTACK(event);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200246
247 TRACE("do_ur_io: cpa=%p\n", cpa);
248
249 rc = mutex_lock_interruptible(&urd->io_mutex);
250 if (rc)
251 return rc;
252
253 urd->io_done = &event;
254
255 spin_lock_irq(get_ccwdev_lock(cdev));
256 rc = ccw_device_start(cdev, cpa, 1, 0, 0);
257 spin_unlock_irq(get_ccwdev_lock(cdev));
258
259 TRACE("do_ur_io: ccw_device_start returned %d\n", rc);
260 if (rc)
261 goto out;
262
263 wait_for_completion(&event);
264 TRACE("do_ur_io: I/O complete\n");
265 rc = 0;
266
267out:
268 mutex_unlock(&urd->io_mutex);
269 return rc;
270}
271
272/*
273 * ur interrupt handler, called from the ccw_device layer
274 */
275static void ur_int_handler(struct ccw_device *cdev, unsigned long intparm,
276 struct irb *irb)
277{
278 struct urdev *urd;
279
280 TRACE("ur_int_handler: intparm=0x%lx cstat=%02x dstat=%02x res=%u\n",
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200281 intparm, irb->scsw.cmd.cstat, irb->scsw.cmd.dstat,
282 irb->scsw.cmd.count);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200283
284 if (!intparm) {
285 TRACE("ur_int_handler: unsolicited interrupt\n");
286 return;
287 }
288 urd = cdev->dev.driver_data;
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200289 BUG_ON(!urd);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200290 /* On special conditions irb is an error pointer */
291 if (IS_ERR(irb))
292 urd->io_request_rc = PTR_ERR(irb);
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200293 else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END))
Frank Munzert810cb5b2007-07-17 13:36:06 +0200294 urd->io_request_rc = 0;
295 else
296 urd->io_request_rc = -EIO;
297
298 complete(urd->io_done);
299}
300
301/*
302 * reclen sysfs attribute - The record length to be used for write CCWs
303 */
304static ssize_t ur_attr_reclen_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
306{
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200307 struct urdev *urd;
308 int rc;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200309
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200310 urd = urdev_get_from_cdev(to_ccwdev(dev));
311 if (!urd)
312 return -ENODEV;
313 rc = sprintf(buf, "%zu\n", urd->reclen);
314 urdev_put(urd);
315 return rc;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200316}
317
318static DEVICE_ATTR(reclen, 0444, ur_attr_reclen_show, NULL);
319
320static int ur_create_attributes(struct device *dev)
321{
322 return device_create_file(dev, &dev_attr_reclen);
323}
324
325static void ur_remove_attributes(struct device *dev)
326{
327 device_remove_file(dev, &dev_attr_reclen);
328}
329
330/*
331 * diagnose code 0x210 - retrieve device information
332 * cc=0 normal completion, we have a real device
333 * cc=1 CP paging error
334 * cc=2 The virtual device exists, but is not associated with a real device
335 * cc=3 Invalid device address, or the virtual device does not exist
336 */
337static int get_urd_class(struct urdev *urd)
338{
339 static struct diag210 ur_diag210;
340 int cc;
341
342 ur_diag210.vrdcdvno = urd->dev_id.devno;
343 ur_diag210.vrdclen = sizeof(struct diag210);
344
345 cc = diag210(&ur_diag210);
346 switch (cc) {
347 case 0:
Frank Munzertb9993a32008-07-17 17:16:46 +0200348 return -EOPNOTSUPP;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200349 case 2:
350 return ur_diag210.vrdcvcla; /* virtual device class */
351 case 3:
352 return -ENODEV;
353 default:
354 return -EIO;
355 }
356}
357
358/*
359 * Allocation and freeing of urfile structures
360 */
361static struct urfile *urfile_alloc(struct urdev *urd)
362{
363 struct urfile *urf;
364
365 urf = kzalloc(sizeof(struct urfile), GFP_KERNEL);
366 if (!urf)
367 return NULL;
368 urf->urd = urd;
369
370 TRACE("urfile_alloc: urd=%p urf=%p rl=%zu\n", urd, urf,
371 urf->dev_reclen);
372
373 return urf;
374}
375
376static void urfile_free(struct urfile *urf)
377{
378 TRACE("urfile_free: urf=%p urd=%p\n", urf, urf->urd);
379 kfree(urf);
380}
381
382/*
383 * The fops implementation of the character device driver
384 */
385static ssize_t do_write(struct urdev *urd, const char __user *udata,
386 size_t count, size_t reclen, loff_t *ppos)
387{
388 struct ccw1 *cpa;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200389 int rc;
390
Michael Holzheu1eade382007-08-10 14:32:30 +0200391 cpa = alloc_chan_prog(udata, count / reclen, reclen);
392 if (IS_ERR(cpa))
393 return PTR_ERR(cpa);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200394
395 rc = do_ur_io(urd, cpa);
396 if (rc)
397 goto fail_kfree_cpa;
398
399 if (urd->io_request_rc) {
400 rc = urd->io_request_rc;
401 goto fail_kfree_cpa;
402 }
403 *ppos += count;
404 rc = count;
Michael Holzheu1eade382007-08-10 14:32:30 +0200405
Frank Munzert810cb5b2007-07-17 13:36:06 +0200406fail_kfree_cpa:
Michael Holzheu1eade382007-08-10 14:32:30 +0200407 free_chan_prog(cpa);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200408 return rc;
409}
410
411static ssize_t ur_write(struct file *file, const char __user *udata,
412 size_t count, loff_t *ppos)
413{
414 struct urfile *urf = file->private_data;
415
416 TRACE("ur_write: count=%zu\n", count);
417
418 if (count == 0)
419 return 0;
420
421 if (count % urf->dev_reclen)
422 return -EINVAL; /* count must be a multiple of reclen */
423
424 if (count > urf->dev_reclen * MAX_RECS_PER_IO)
425 count = urf->dev_reclen * MAX_RECS_PER_IO;
426
427 return do_write(urf->urd, udata, count, urf->dev_reclen, ppos);
428}
429
Frank Munzert810cb5b2007-07-17 13:36:06 +0200430/*
431 * diagnose code 0x14 subcode 0x0028 - position spool file to designated
432 * record
433 * cc=0 normal completion
434 * cc=2 no file active on the virtual reader or device not ready
435 * cc=3 record specified is beyond EOF
436 */
437static int diag_position_to_record(int devno, int record)
438{
439 int cc;
440
Michael Holzheu0a87c5c2007-08-22 13:51:40 +0200441 cc = diag14(record, devno, 0x28);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200442 switch (cc) {
443 case 0:
444 return 0;
445 case 2:
446 return -ENOMEDIUM;
447 case 3:
448 return -ENODATA; /* position beyond end of file */
449 default:
450 return -EIO;
451 }
452}
453
454/*
455 * diagnose code 0x14 subcode 0x0000 - read next spool file buffer
456 * cc=0 normal completion
457 * cc=1 EOF reached
458 * cc=2 no file active on the virtual reader, and no file eligible
459 * cc=3 file already active on the virtual reader or specified virtual
460 * reader does not exist or is not a reader
461 */
462static int diag_read_file(int devno, char *buf)
463{
464 int cc;
465
Michael Holzheu0a87c5c2007-08-22 13:51:40 +0200466 cc = diag14((unsigned long) buf, devno, 0x00);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200467 switch (cc) {
468 case 0:
469 return 0;
470 case 1:
471 return -ENODATA;
472 case 2:
473 return -ENOMEDIUM;
474 default:
475 return -EIO;
476 }
477}
478
479static ssize_t diag14_read(struct file *file, char __user *ubuf, size_t count,
480 loff_t *offs)
481{
482 size_t len, copied, res;
483 char *buf;
484 int rc;
485 u16 reclen;
486 struct urdev *urd;
487
488 urd = ((struct urfile *) file->private_data)->urd;
489 reclen = ((struct urfile *) file->private_data)->file_reclen;
490
491 rc = diag_position_to_record(urd->dev_id.devno, *offs / PAGE_SIZE + 1);
492 if (rc == -ENODATA)
493 return 0;
494 if (rc)
495 return rc;
496
497 len = min((size_t) PAGE_SIZE, count);
Michael Holzheu3eed13c2007-08-10 14:32:34 +0200498 buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200499 if (!buf)
500 return -ENOMEM;
501
502 copied = 0;
503 res = (size_t) (*offs % PAGE_SIZE);
504 do {
505 rc = diag_read_file(urd->dev_id.devno, buf);
506 if (rc == -ENODATA) {
507 break;
508 }
509 if (rc)
510 goto fail;
Frank Munzert2b3d8c92007-07-27 12:29:17 +0200511 if (reclen && (copied == 0) && (*offs < PAGE_SIZE))
Frank Munzert810cb5b2007-07-17 13:36:06 +0200512 *((u16 *) &buf[FILE_RECLEN_OFFSET]) = reclen;
513 len = min(count - copied, PAGE_SIZE - res);
514 if (copy_to_user(ubuf + copied, buf + res, len)) {
515 rc = -EFAULT;
516 goto fail;
517 }
518 res = 0;
519 copied += len;
520 } while (copied != count);
521
522 *offs += copied;
523 rc = copied;
524fail:
Michael Holzheu3eed13c2007-08-10 14:32:34 +0200525 free_page((unsigned long) buf);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200526 return rc;
527}
528
529static ssize_t ur_read(struct file *file, char __user *ubuf, size_t count,
530 loff_t *offs)
531{
532 struct urdev *urd;
533 int rc;
534
535 TRACE("ur_read: count=%zu ppos=%li\n", count, (unsigned long) *offs);
536
537 if (count == 0)
538 return 0;
539
540 urd = ((struct urfile *) file->private_data)->urd;
541 rc = mutex_lock_interruptible(&urd->io_mutex);
542 if (rc)
543 return rc;
544 rc = diag14_read(file, ubuf, count, offs);
545 mutex_unlock(&urd->io_mutex);
546 return rc;
547}
548
549/*
550 * diagnose code 0x14 subcode 0x0fff - retrieve next file descriptor
551 * cc=0 normal completion
552 * cc=1 no files on reader queue or no subsequent file
553 * cc=2 spid specified is invalid
554 */
555static int diag_read_next_file_info(struct file_control_block *buf, int spid)
556{
557 int cc;
558
Michael Holzheu0a87c5c2007-08-22 13:51:40 +0200559 cc = diag14((unsigned long) buf, spid, 0xfff);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200560 switch (cc) {
561 case 0:
562 return 0;
563 default:
564 return -ENODATA;
565 }
566}
567
Michael Holzheu3eed13c2007-08-10 14:32:34 +0200568static int verify_uri_device(struct urdev *urd)
Frank Munzert810cb5b2007-07-17 13:36:06 +0200569{
Michael Holzheu3eed13c2007-08-10 14:32:34 +0200570 struct file_control_block *fcb;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200571 char *buf;
572 int rc;
573
Michael Holzheu3eed13c2007-08-10 14:32:34 +0200574 fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA);
575 if (!fcb)
576 return -ENOMEM;
577
578 /* check for empty reader device (beginning of chain) */
579 rc = diag_read_next_file_info(fcb, 0);
580 if (rc)
581 goto fail_free_fcb;
582
583 /* if file is in hold status, we do not read it */
584 if (fcb->file_stat & (FLG_SYSTEM_HOLD | FLG_USER_HOLD)) {
585 rc = -EPERM;
586 goto fail_free_fcb;
587 }
588
589 /* open file on virtual reader */
590 buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA);
591 if (!buf) {
592 rc = -ENOMEM;
593 goto fail_free_fcb;
594 }
595 rc = diag_read_file(urd->dev_id.devno, buf);
596 if ((rc != 0) && (rc != -ENODATA)) /* EOF does not hurt */
597 goto fail_free_buf;
598
599 /* check if the file on top of the queue is open now */
600 rc = diag_read_next_file_info(fcb, 0);
601 if (rc)
602 goto fail_free_buf;
603 if (!(fcb->file_stat & FLG_IN_USE)) {
604 rc = -EMFILE;
605 goto fail_free_buf;
606 }
607 rc = 0;
608
609fail_free_buf:
610 free_page((unsigned long) buf);
611fail_free_fcb:
612 kfree(fcb);
613 return rc;
614}
615
616static int verify_device(struct urdev *urd)
617{
Frank Munzert810cb5b2007-07-17 13:36:06 +0200618 switch (urd->class) {
619 case DEV_CLASS_UR_O:
620 return 0; /* no check needed here */
621 case DEV_CLASS_UR_I:
Michael Holzheu3eed13c2007-08-10 14:32:34 +0200622 return verify_uri_device(urd);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200623 default:
Frank Munzertb9993a32008-07-17 17:16:46 +0200624 return -EOPNOTSUPP;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200625 }
626}
627
Michael Holzheu3eed13c2007-08-10 14:32:34 +0200628static int get_uri_file_reclen(struct urdev *urd)
Frank Munzert810cb5b2007-07-17 13:36:06 +0200629{
Michael Holzheu3eed13c2007-08-10 14:32:34 +0200630 struct file_control_block *fcb;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200631 int rc;
632
Michael Holzheu3eed13c2007-08-10 14:32:34 +0200633 fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA);
634 if (!fcb)
635 return -ENOMEM;
636 rc = diag_read_next_file_info(fcb, 0);
637 if (rc)
638 goto fail_free;
639 if (fcb->file_stat & FLG_CP_DUMP)
640 rc = 0;
641 else
642 rc = fcb->rec_len;
643
644fail_free:
645 kfree(fcb);
646 return rc;
647}
648
649static int get_file_reclen(struct urdev *urd)
650{
Frank Munzert810cb5b2007-07-17 13:36:06 +0200651 switch (urd->class) {
652 case DEV_CLASS_UR_O:
653 return 0;
654 case DEV_CLASS_UR_I:
Michael Holzheu3eed13c2007-08-10 14:32:34 +0200655 return get_uri_file_reclen(urd);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200656 default:
Frank Munzertb9993a32008-07-17 17:16:46 +0200657 return -EOPNOTSUPP;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200658 }
Frank Munzert810cb5b2007-07-17 13:36:06 +0200659}
660
661static int ur_open(struct inode *inode, struct file *file)
662{
663 u16 devno;
664 struct urdev *urd;
665 struct urfile *urf;
666 unsigned short accmode;
667 int rc;
668
669 accmode = file->f_flags & O_ACCMODE;
670
671 if (accmode == O_RDWR)
672 return -EACCES;
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600673 lock_kernel();
Frank Munzert810cb5b2007-07-17 13:36:06 +0200674 /*
675 * We treat the minor number as the devno of the ur device
676 * to find in the driver tree.
677 */
678 devno = MINOR(file->f_dentry->d_inode->i_rdev);
679
680 urd = urdev_get_from_devno(devno);
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600681 if (!urd) {
682 rc = -ENXIO;
683 goto out;
684 }
Frank Munzert810cb5b2007-07-17 13:36:06 +0200685
Frank Munzerta695f162008-04-17 07:46:06 +0200686 spin_lock(&urd->open_lock);
687 while (urd->open_flag) {
688 spin_unlock(&urd->open_lock);
689 if (file->f_flags & O_NONBLOCK) {
Frank Munzert810cb5b2007-07-17 13:36:06 +0200690 rc = -EBUSY;
691 goto fail_put;
692 }
Frank Munzerta695f162008-04-17 07:46:06 +0200693 if (wait_event_interruptible(urd->wait, urd->open_flag == 0)) {
Frank Munzert810cb5b2007-07-17 13:36:06 +0200694 rc = -ERESTARTSYS;
695 goto fail_put;
696 }
Frank Munzerta695f162008-04-17 07:46:06 +0200697 spin_lock(&urd->open_lock);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200698 }
Frank Munzerta695f162008-04-17 07:46:06 +0200699 urd->open_flag++;
700 spin_unlock(&urd->open_lock);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200701
702 TRACE("ur_open\n");
703
704 if (((accmode == O_RDONLY) && (urd->class != DEV_CLASS_UR_I)) ||
705 ((accmode == O_WRONLY) && (urd->class != DEV_CLASS_UR_O))) {
706 TRACE("ur_open: unsupported dev class (%d)\n", urd->class);
707 rc = -EACCES;
708 goto fail_unlock;
709 }
710
711 rc = verify_device(urd);
712 if (rc)
713 goto fail_unlock;
714
715 urf = urfile_alloc(urd);
716 if (!urf) {
717 rc = -ENOMEM;
718 goto fail_unlock;
719 }
720
721 urf->dev_reclen = urd->reclen;
722 rc = get_file_reclen(urd);
723 if (rc < 0)
724 goto fail_urfile_free;
725 urf->file_reclen = rc;
726 file->private_data = urf;
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600727 unlock_kernel();
Frank Munzert810cb5b2007-07-17 13:36:06 +0200728 return 0;
729
730fail_urfile_free:
731 urfile_free(urf);
732fail_unlock:
Frank Munzerta695f162008-04-17 07:46:06 +0200733 spin_lock(&urd->open_lock);
734 urd->open_flag--;
735 spin_unlock(&urd->open_lock);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200736fail_put:
737 urdev_put(urd);
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600738out:
739 unlock_kernel();
Frank Munzert810cb5b2007-07-17 13:36:06 +0200740 return rc;
741}
742
743static int ur_release(struct inode *inode, struct file *file)
744{
745 struct urfile *urf = file->private_data;
746
747 TRACE("ur_release\n");
Frank Munzerta695f162008-04-17 07:46:06 +0200748 spin_lock(&urf->urd->open_lock);
749 urf->urd->open_flag--;
750 spin_unlock(&urf->urd->open_lock);
751 wake_up_interruptible(&urf->urd->wait);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200752 urdev_put(urf->urd);
753 urfile_free(urf);
754 return 0;
755}
756
757static loff_t ur_llseek(struct file *file, loff_t offset, int whence)
758{
759 loff_t newpos;
760
761 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
762 return -ESPIPE; /* seek allowed only for reader */
763 if (offset % PAGE_SIZE)
764 return -ESPIPE; /* only multiples of 4K allowed */
765 switch (whence) {
766 case 0: /* SEEK_SET */
767 newpos = offset;
768 break;
769 case 1: /* SEEK_CUR */
770 newpos = file->f_pos + offset;
771 break;
772 default:
773 return -EINVAL;
774 }
775 file->f_pos = newpos;
776 return newpos;
777}
778
Jan Engelhardt5c81cdb2008-01-26 14:11:29 +0100779static const struct file_operations ur_fops = {
Frank Munzert810cb5b2007-07-17 13:36:06 +0200780 .owner = THIS_MODULE,
781 .open = ur_open,
782 .release = ur_release,
783 .read = ur_read,
784 .write = ur_write,
785 .llseek = ur_llseek,
786};
787
788/*
789 * ccw_device infrastructure:
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200790 * ur_probe creates the struct urdev (with refcount = 1), the device
791 * attributes, sets up the interrupt handler and validates the virtual
792 * unit record device.
793 * ur_remove removes the device attributes and drops the reference to
794 * struct urdev.
795 *
796 * ur_probe, ur_remove, ur_set_online and ur_set_offline are serialized
797 * by the vmur_mutex lock.
798 *
799 * urd->char_device is used as indication that the online function has
800 * been completed successfully.
Frank Munzert810cb5b2007-07-17 13:36:06 +0200801 */
802static int ur_probe(struct ccw_device *cdev)
803{
804 struct urdev *urd;
805 int rc;
806
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200807 TRACE("ur_probe: cdev=%p\n", cdev);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200808
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200809 mutex_lock(&vmur_mutex);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200810 urd = urdev_alloc(cdev);
811 if (!urd) {
812 rc = -ENOMEM;
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200813 goto fail_unlock;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200814 }
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200815
Frank Munzert810cb5b2007-07-17 13:36:06 +0200816 rc = ur_create_attributes(&cdev->dev);
817 if (rc) {
818 rc = -ENOMEM;
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200819 goto fail_urdev_put;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200820 }
Frank Munzert810cb5b2007-07-17 13:36:06 +0200821 cdev->handler = ur_int_handler;
822
823 /* validate virtual unit record device */
824 urd->class = get_urd_class(urd);
825 if (urd->class < 0) {
826 rc = urd->class;
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200827 goto fail_remove_attr;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200828 }
829 if ((urd->class != DEV_CLASS_UR_I) && (urd->class != DEV_CLASS_UR_O)) {
Frank Munzertb9993a32008-07-17 17:16:46 +0200830 rc = -EOPNOTSUPP;
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200831 goto fail_remove_attr;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200832 }
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200833 spin_lock_irq(get_ccwdev_lock(cdev));
834 cdev->dev.driver_data = urd;
835 spin_unlock_irq(get_ccwdev_lock(cdev));
Frank Munzert810cb5b2007-07-17 13:36:06 +0200836
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200837 mutex_unlock(&vmur_mutex);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200838 return 0;
839
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200840fail_remove_attr:
Frank Munzert810cb5b2007-07-17 13:36:06 +0200841 ur_remove_attributes(&cdev->dev);
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200842fail_urdev_put:
843 urdev_put(urd);
844fail_unlock:
845 mutex_unlock(&vmur_mutex);
846 return rc;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200847}
848
849static int ur_set_online(struct ccw_device *cdev)
850{
851 struct urdev *urd;
852 int minor, major, rc;
853 char node_id[16];
854
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200855 TRACE("ur_set_online: cdev=%p\n", cdev);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200856
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200857 mutex_lock(&vmur_mutex);
858 urd = urdev_get_from_cdev(cdev);
859 if (!urd) {
860 /* ur_remove already deleted our urd */
861 rc = -ENODEV;
862 goto fail_unlock;
863 }
Frank Munzert810cb5b2007-07-17 13:36:06 +0200864
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200865 if (urd->char_device) {
866 /* Another ur_set_online was faster */
867 rc = -EBUSY;
868 goto fail_urdev_put;
869 }
870
Frank Munzert810cb5b2007-07-17 13:36:06 +0200871 minor = urd->dev_id.devno;
872 major = MAJOR(ur_first_dev_maj_min);
873
874 urd->char_device = cdev_alloc();
875 if (!urd->char_device) {
876 rc = -ENOMEM;
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200877 goto fail_urdev_put;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200878 }
879
880 cdev_init(urd->char_device, &ur_fops);
881 urd->char_device->dev = MKDEV(major, minor);
882 urd->char_device->owner = ur_fops.owner;
883
884 rc = cdev_add(urd->char_device, urd->char_device->dev, 1);
885 if (rc)
886 goto fail_free_cdev;
887 if (urd->cdev->id.cu_type == READER_PUNCH_DEVTYPE) {
888 if (urd->class == DEV_CLASS_UR_I)
889 sprintf(node_id, "vmrdr-%s", cdev->dev.bus_id);
890 if (urd->class == DEV_CLASS_UR_O)
891 sprintf(node_id, "vmpun-%s", cdev->dev.bus_id);
892 } else if (urd->cdev->id.cu_type == PRINTER_DEVTYPE) {
893 sprintf(node_id, "vmprt-%s", cdev->dev.bus_id);
894 } else {
Frank Munzertb9993a32008-07-17 17:16:46 +0200895 rc = -EOPNOTSUPP;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200896 goto fail_free_cdev;
897 }
898
Greg Kroah-Hartman05675de22008-05-21 12:52:33 -0700899 urd->device = device_create_drvdata(vmur_class, NULL,
900 urd->char_device->dev, NULL,
901 "%s", node_id);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200902 if (IS_ERR(urd->device)) {
903 rc = PTR_ERR(urd->device);
904 TRACE("ur_set_online: device_create rc=%d\n", rc);
905 goto fail_free_cdev;
906 }
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200907 urdev_put(urd);
908 mutex_unlock(&vmur_mutex);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200909 return 0;
910
911fail_free_cdev:
912 cdev_del(urd->char_device);
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200913 urd->char_device = NULL;
914fail_urdev_put:
915 urdev_put(urd);
916fail_unlock:
917 mutex_unlock(&vmur_mutex);
918 return rc;
919}
Frank Munzert810cb5b2007-07-17 13:36:06 +0200920
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200921static int ur_set_offline_force(struct ccw_device *cdev, int force)
922{
923 struct urdev *urd;
924 int rc;
925
926 TRACE("ur_set_offline: cdev=%p\n", cdev);
927 urd = urdev_get_from_cdev(cdev);
928 if (!urd)
929 /* ur_remove already deleted our urd */
930 return -ENODEV;
931 if (!urd->char_device) {
932 /* Another ur_set_offline was faster */
933 rc = -EBUSY;
934 goto fail_urdev_put;
935 }
936 if (!force && (atomic_read(&urd->ref_count) > 2)) {
937 /* There is still a user of urd (e.g. ur_open) */
938 TRACE("ur_set_offline: BUSY\n");
939 rc = -EBUSY;
940 goto fail_urdev_put;
941 }
942 device_destroy(vmur_class, urd->char_device->dev);
943 cdev_del(urd->char_device);
944 urd->char_device = NULL;
945 rc = 0;
946
947fail_urdev_put:
948 urdev_put(urd);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200949 return rc;
950}
951
952static int ur_set_offline(struct ccw_device *cdev)
953{
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200954 int rc;
Frank Munzert810cb5b2007-07-17 13:36:06 +0200955
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200956 mutex_lock(&vmur_mutex);
957 rc = ur_set_offline_force(cdev, 0);
958 mutex_unlock(&vmur_mutex);
959 return rc;
960}
Frank Munzert810cb5b2007-07-17 13:36:06 +0200961
Michael Holzheu8127a1f2007-08-22 13:51:41 +0200962static void ur_remove(struct ccw_device *cdev)
963{
964 unsigned long flags;
965
966 TRACE("ur_remove\n");
967
968 mutex_lock(&vmur_mutex);
969
970 if (cdev->online)
971 ur_set_offline_force(cdev, 1);
972 ur_remove_attributes(&cdev->dev);
973
974 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
975 urdev_put(cdev->dev.driver_data);
976 cdev->dev.driver_data = NULL;
977 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
978
979 mutex_unlock(&vmur_mutex);
Frank Munzert810cb5b2007-07-17 13:36:06 +0200980}
981
982/*
983 * Module initialisation and cleanup
984 */
985static int __init ur_init(void)
986{
987 int rc;
988 dev_t dev;
989
990 if (!MACHINE_IS_VM) {
991 PRINT_ERR("%s is only available under z/VM.\n", ur_banner);
992 return -ENODEV;
993 }
994
995 vmur_dbf = debug_register("vmur", 4, 1, 4 * sizeof(long));
996 if (!vmur_dbf)
997 return -ENOMEM;
998 rc = debug_register_view(vmur_dbf, &debug_sprintf_view);
999 if (rc)
1000 goto fail_free_dbf;
1001
1002 debug_set_level(vmur_dbf, 6);
1003
1004 rc = ccw_driver_register(&ur_driver);
1005 if (rc)
1006 goto fail_free_dbf;
1007
1008 rc = alloc_chrdev_region(&dev, 0, NUM_MINORS, "vmur");
1009 if (rc) {
1010 PRINT_ERR("alloc_chrdev_region failed: err = %d\n", rc);
1011 goto fail_unregister_driver;
1012 }
1013 ur_first_dev_maj_min = MKDEV(MAJOR(dev), 0);
1014
1015 vmur_class = class_create(THIS_MODULE, "vmur");
1016 if (IS_ERR(vmur_class)) {
1017 rc = PTR_ERR(vmur_class);
1018 goto fail_unregister_region;
1019 }
1020 PRINT_INFO("%s loaded.\n", ur_banner);
1021 return 0;
1022
1023fail_unregister_region:
1024 unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS);
1025fail_unregister_driver:
1026 ccw_driver_unregister(&ur_driver);
1027fail_free_dbf:
1028 debug_unregister(vmur_dbf);
1029 return rc;
1030}
1031
1032static void __exit ur_exit(void)
1033{
1034 class_destroy(vmur_class);
1035 unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS);
1036 ccw_driver_unregister(&ur_driver);
1037 debug_unregister(vmur_dbf);
1038 PRINT_INFO("%s unloaded.\n", ur_banner);
1039}
1040
1041module_init(ur_init);
1042module_exit(ur_exit);