]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/s390/char/vmur.c
[S390] cio: Exorcise cio_msg= from documentation.
[mirror_ubuntu-bionic-kernel.git] / drivers / s390 / char / vmur.c
CommitLineData
810cb5b3
FM
1/*
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>
764a4a8e 12#include <linux/smp_lock.h>
810cb5b3
FM
13
14#include <asm/uaccess.h>
15#include <asm/cio.h>
16#include <asm/ccwdev.h>
17#include <asm/debug.h>
0a87c5cf 18#include <asm/diag.h>
810cb5b3
FM
19
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
8127a1f8
MH
73static DEFINE_MUTEX(vmur_mutex);
74
810cb5b3
FM
75/*
76 * Allocation, freeing, getting and putting of urdev structures
8127a1f8
MH
77 *
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
810cb5b3
FM
93 */
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;
810cb5b3
FM
101 urd->reclen = cdev->id.driver_info;
102 ccw_device_get_id(cdev, &urd->dev_id);
103 mutex_init(&urd->io_mutex);
a695f167
FM
104 init_waitqueue_head(&urd->wait);
105 spin_lock_init(&urd->open_lock);
8127a1f8
MH
106 atomic_set(&urd->ref_count, 1);
107 urd->cdev = cdev;
108 get_device(&cdev->dev);
810cb5b3
FM
109 return urd;
110}
111
112static void urdev_free(struct urdev *urd)
113{
8127a1f8
MH
114 TRACE("urdev_free: %p\n", urd);
115 if (urd->cdev)
116 put_device(&urd->cdev->dev);
810cb5b3
FM
117 kfree(urd);
118}
119
8127a1f8
MH
120static 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
810cb5b3
FM
138static struct urdev *urdev_get_from_devno(u16 devno)
139{
140 char bus_id[16];
141 struct ccw_device *cdev;
8127a1f8 142 struct urdev *urd;
810cb5b3
FM
143
144 sprintf(bus_id, "0.0.%04x", devno);
145 cdev = get_ccwdev_by_busid(&ur_driver, bus_id);
146 if (!cdev)
147 return NULL;
8127a1f8
MH
148 urd = urdev_get_from_cdev(cdev);
149 put_device(&cdev->dev);
150 return urd;
810cb5b3
FM
151}
152
153static void urdev_put(struct urdev *urd)
154{
8127a1f8
MH
155 if (atomic_dec_and_test(&urd->ref_count))
156 urdev_free(urd);
810cb5b3
FM
157}
158
159/*
160 * Low-level functions to do I/O to a ur device.
161 * alloc_chan_prog
1eade380 162 * free_chan_prog
810cb5b3
FM
163 * do_ur_io
164 * ur_int_handler
165 *
166 * alloc_chan_prog allocates and builds the channel program
1eade380 167 * free_chan_prog frees memory of the channel program
810cb5b3
FM
168 *
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
1eade380
MH
182static 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}
810cb5b3
FM
192
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
1eade380 199 * data length in each CCW is reclen.
810cb5b3 200 */
1eade380
MH
201static struct ccw1 *alloc_chan_prog(const char __user *ubuf, int rec_count,
202 int reclen)
810cb5b3 203{
810cb5b3 204 struct ccw1 *cpa;
1eade380 205 void *kbuf;
810cb5b3
FM
206 int i;
207
1eade380 208 TRACE("alloc_chan_prog(%p, %i, %i)\n", ubuf, rec_count, reclen);
810cb5b3
FM
209
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 */
1eade380
MH
215 cpa = kzalloc((rec_count + 1) * sizeof(struct ccw1),
216 GFP_KERNEL | GFP_DMA);
810cb5b3 217 if (!cpa)
1eade380 218 return ERR_PTR(-ENOMEM);
810cb5b3 219
1eade380 220 for (i = 0; i < rec_count; i++) {
810cb5b3
FM
221 cpa[i].cmd_code = WRITE_CCW_CMD;
222 cpa[i].flags = CCW_FLAG_CC | CCW_FLAG_SLI;
223 cpa[i].count = reclen;
1eade380
MH
224 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;
810cb5b3
FM
235 }
236 /* The following NOP CCW forces CE+DE to be presented together */
237 cpa[i].cmd_code = CCW_CMD_NOOP;
810cb5b3
FM
238 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;
278bc68c 245 DECLARE_COMPLETION_ONSTACK(event);
810cb5b3
FM
246
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",
23d805b6
PO
281 intparm, irb->scsw.cmd.cstat, irb->scsw.cmd.dstat,
282 irb->scsw.cmd.count);
810cb5b3
FM
283
284 if (!intparm) {
285 TRACE("ur_int_handler: unsolicited interrupt\n");
286 return;
287 }
288 urd = cdev->dev.driver_data;
8127a1f8 289 BUG_ON(!urd);
810cb5b3
FM
290 /* On special conditions irb is an error pointer */
291 if (IS_ERR(irb))
292 urd->io_request_rc = PTR_ERR(irb);
23d805b6 293 else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END))
810cb5b3
FM
294 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{
8127a1f8
MH
307 struct urdev *urd;
308 int rc;
810cb5b3 309
8127a1f8
MH
310 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;
810cb5b3
FM
316}
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:
b9993a38 348 return -EOPNOTSUPP;
810cb5b3
FM
349 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;
810cb5b3
FM
389 int rc;
390
1eade380
MH
391 cpa = alloc_chan_prog(udata, count / reclen, reclen);
392 if (IS_ERR(cpa))
393 return PTR_ERR(cpa);
810cb5b3
FM
394
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;
1eade380 405
810cb5b3 406fail_kfree_cpa:
1eade380 407 free_chan_prog(cpa);
810cb5b3
FM
408 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
810cb5b3
FM
430/*
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
0a87c5cf 441 cc = diag14(record, devno, 0x28);
810cb5b3
FM
442 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
0a87c5cf 466 cc = diag14((unsigned long) buf, devno, 0x00);
810cb5b3
FM
467 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);
3eed13cc 498 buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA);
810cb5b3
FM
499 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;
2b3d8c9e 511 if (reclen && (copied == 0) && (*offs < PAGE_SIZE))
810cb5b3
FM
512 *((u16 *) &buf[FILE_RECLEN_OFFSET]) = reclen;
513 len = min(count - copied, PAGE_SIZE - res);
514 if (copy_to_user(ubuf + copied, buf + res, len)) {
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:
3eed13cc 525 free_page((unsigned long) buf);
810cb5b3
FM
526 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
0a87c5cf 559 cc = diag14((unsigned long) buf, spid, 0xfff);
810cb5b3
FM
560 switch (cc) {
561 case 0:
562 return 0;
563 default:
564 return -ENODATA;
565 }
566}
567
3eed13cc 568static int verify_uri_device(struct urdev *urd)
810cb5b3 569{
3eed13cc 570 struct file_control_block *fcb;
810cb5b3
FM
571 char *buf;
572 int rc;
573
3eed13cc
MH
574 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{
810cb5b3
FM
618 switch (urd->class) {
619 case DEV_CLASS_UR_O:
620 return 0; /* no check needed here */
621 case DEV_CLASS_UR_I:
3eed13cc 622 return verify_uri_device(urd);
810cb5b3 623 default:
b9993a38 624 return -EOPNOTSUPP;
810cb5b3
FM
625 }
626}
627
3eed13cc 628static int get_uri_file_reclen(struct urdev *urd)
810cb5b3 629{
3eed13cc 630 struct file_control_block *fcb;
810cb5b3
FM
631 int rc;
632
3eed13cc
MH
633 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{
810cb5b3
FM
651 switch (urd->class) {
652 case DEV_CLASS_UR_O:
653 return 0;
654 case DEV_CLASS_UR_I:
3eed13cc 655 return get_uri_file_reclen(urd);
810cb5b3 656 default:
b9993a38 657 return -EOPNOTSUPP;
810cb5b3 658 }
810cb5b3
FM
659}
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;
764a4a8e 673 lock_kernel();
810cb5b3
FM
674 /*
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);
764a4a8e
JC
681 if (!urd) {
682 rc = -ENXIO;
683 goto out;
684 }
810cb5b3 685
a695f167
FM
686 spin_lock(&urd->open_lock);
687 while (urd->open_flag) {
688 spin_unlock(&urd->open_lock);
689 if (file->f_flags & O_NONBLOCK) {
810cb5b3
FM
690 rc = -EBUSY;
691 goto fail_put;
692 }
a695f167 693 if (wait_event_interruptible(urd->wait, urd->open_flag == 0)) {
810cb5b3
FM
694 rc = -ERESTARTSYS;
695 goto fail_put;
696 }
a695f167 697 spin_lock(&urd->open_lock);
810cb5b3 698 }
a695f167
FM
699 urd->open_flag++;
700 spin_unlock(&urd->open_lock);
810cb5b3
FM
701
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;
764a4a8e 727 unlock_kernel();
810cb5b3
FM
728 return 0;
729
730fail_urfile_free:
731 urfile_free(urf);
732fail_unlock:
a695f167
FM
733 spin_lock(&urd->open_lock);
734 urd->open_flag--;
735 spin_unlock(&urd->open_lock);
810cb5b3
FM
736fail_put:
737 urdev_put(urd);
764a4a8e
JC
738out:
739 unlock_kernel();
810cb5b3
FM
740 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");
a695f167
FM
748 spin_lock(&urf->urd->open_lock);
749 urf->urd->open_flag--;
750 spin_unlock(&urf->urd->open_lock);
751 wake_up_interruptible(&urf->urd->wait);
810cb5b3
FM
752 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
5c81cdbe 779static const struct file_operations ur_fops = {
810cb5b3
FM
780 .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:
8127a1f8
MH
790 * ur_probe creates the struct urdev (with refcount = 1), the device
791 * attributes, sets up the interrupt handler and validates the virtual
792 * unit record device.
793 * ur_remove removes the device attributes and drops the reference to
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.
810cb5b3
FM
801 */
802static int ur_probe(struct ccw_device *cdev)
803{
804 struct urdev *urd;
805 int rc;
806
8127a1f8 807 TRACE("ur_probe: cdev=%p\n", cdev);
810cb5b3 808
8127a1f8 809 mutex_lock(&vmur_mutex);
810cb5b3
FM
810 urd = urdev_alloc(cdev);
811 if (!urd) {
812 rc = -ENOMEM;
8127a1f8 813 goto fail_unlock;
810cb5b3 814 }
8127a1f8 815
810cb5b3
FM
816 rc = ur_create_attributes(&cdev->dev);
817 if (rc) {
818 rc = -ENOMEM;
8127a1f8 819 goto fail_urdev_put;
810cb5b3 820 }
810cb5b3
FM
821 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;
8127a1f8 827 goto fail_remove_attr;
810cb5b3
FM
828 }
829 if ((urd->class != DEV_CLASS_UR_I) && (urd->class != DEV_CLASS_UR_O)) {
b9993a38 830 rc = -EOPNOTSUPP;
8127a1f8 831 goto fail_remove_attr;
810cb5b3 832 }
8127a1f8
MH
833 spin_lock_irq(get_ccwdev_lock(cdev));
834 cdev->dev.driver_data = urd;
835 spin_unlock_irq(get_ccwdev_lock(cdev));
810cb5b3 836
8127a1f8 837 mutex_unlock(&vmur_mutex);
810cb5b3
FM
838 return 0;
839
8127a1f8 840fail_remove_attr:
810cb5b3 841 ur_remove_attributes(&cdev->dev);
8127a1f8
MH
842fail_urdev_put:
843 urdev_put(urd);
844fail_unlock:
845 mutex_unlock(&vmur_mutex);
846 return rc;
810cb5b3
FM
847}
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
8127a1f8 855 TRACE("ur_set_online: cdev=%p\n", cdev);
810cb5b3 856
8127a1f8
MH
857 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 }
864
865 if (urd->char_device) {
866 /* Another ur_set_online was faster */
867 rc = -EBUSY;
868 goto fail_urdev_put;
869 }
810cb5b3 870
810cb5b3
FM
871 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;
8127a1f8 877 goto fail_urdev_put;
810cb5b3
FM
878 }
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 {
b9993a38 895 rc = -EOPNOTSUPP;
810cb5b3
FM
896 goto fail_free_cdev;
897 }
898
05675de2
GKH
899 urd->device = device_create_drvdata(vmur_class, NULL,
900 urd->char_device->dev, NULL,
901 "%s", node_id);
810cb5b3
FM
902 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 }
8127a1f8
MH
907 urdev_put(urd);
908 mutex_unlock(&vmur_mutex);
810cb5b3
FM
909 return 0;
910
911fail_free_cdev:
912 cdev_del(urd->char_device);
8127a1f8
MH
913 urd->char_device = NULL;
914fail_urdev_put:
915 urdev_put(urd);
916fail_unlock:
917 mutex_unlock(&vmur_mutex);
810cb5b3
FM
918 return rc;
919}
920
8127a1f8 921static int ur_set_offline_force(struct ccw_device *cdev, int force)
810cb5b3
FM
922{
923 struct urdev *urd;
8127a1f8 924 int rc;
810cb5b3 925
8127a1f8
MH
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 }
810cb5b3
FM
942 device_destroy(vmur_class, urd->char_device->dev);
943 cdev_del(urd->char_device);
8127a1f8
MH
944 urd->char_device = NULL;
945 rc = 0;
810cb5b3 946
8127a1f8
MH
947fail_urdev_put:
948 urdev_put(urd);
949 return rc;
950}
951
952static int ur_set_offline(struct ccw_device *cdev)
953{
954 int rc;
955
956 mutex_lock(&vmur_mutex);
957 rc = ur_set_offline_force(cdev, 0);
958 mutex_unlock(&vmur_mutex);
959 return rc;
960}
961
962static 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);
810cb5b3
FM
980}
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);