vmur.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. #include <linux/cdev.h>
  11. #include <linux/smp_lock.h>
  12. #include <asm/uaccess.h>
  13. #include <asm/cio.h>
  14. #include <asm/ccwdev.h>
  15. #include <asm/debug.h>
  16. #include <asm/diag.h>
  17. #include "vmur.h"
  18. /*
  19. * Driver overview
  20. *
  21. * Unit record device support is implemented as a character device driver.
  22. * We can fit at least 16 bits into a device minor number and use the
  23. * simple method of mapping a character device number with minor abcd
  24. * to the unit record device with devno abcd.
  25. * I/O to virtual unit record devices is handled as follows:
  26. * Reads: Diagnose code 0x14 (input spool file manipulation)
  27. * is used to read spool data page-wise.
  28. * Writes: The CCW used is WRITE_CCW_CMD (0x01). The device's record length
  29. * is available by reading sysfs attr reclen. Each write() to the device
  30. * must specify an integral multiple (maximal 511) of reclen.
  31. */
  32. static char ur_banner[] = "z/VM virtual unit record device driver";
  33. MODULE_AUTHOR("IBM Corporation");
  34. MODULE_DESCRIPTION("s390 z/VM virtual unit record device driver");
  35. MODULE_LICENSE("GPL");
  36. #define PRINTK_HEADER "vmur: "
  37. static dev_t ur_first_dev_maj_min;
  38. static struct class *vmur_class;
  39. static struct debug_info *vmur_dbf;
  40. /* We put the device's record length (for writes) in the driver_info field */
  41. static struct ccw_device_id ur_ids[] = {
  42. { CCWDEV_CU_DI(READER_PUNCH_DEVTYPE, 80) },
  43. { CCWDEV_CU_DI(PRINTER_DEVTYPE, 132) },
  44. { /* end of list */ }
  45. };
  46. MODULE_DEVICE_TABLE(ccw, ur_ids);
  47. static int ur_probe(struct ccw_device *cdev);
  48. static void ur_remove(struct ccw_device *cdev);
  49. static int ur_set_online(struct ccw_device *cdev);
  50. static int ur_set_offline(struct ccw_device *cdev);
  51. static struct ccw_driver ur_driver = {
  52. .name = "vmur",
  53. .owner = THIS_MODULE,
  54. .ids = ur_ids,
  55. .probe = ur_probe,
  56. .remove = ur_remove,
  57. .set_online = ur_set_online,
  58. .set_offline = ur_set_offline,
  59. };
  60. static DEFINE_MUTEX(vmur_mutex);
  61. /*
  62. * Allocation, freeing, getting and putting of urdev structures
  63. *
  64. * Each ur device (urd) contains a reference to its corresponding ccw device
  65. * (cdev) using the urd->cdev pointer. Each ccw device has a reference to the
  66. * ur device using the cdev->dev.driver_data pointer.
  67. *
  68. * urd references:
  69. * - ur_probe gets a urd reference, ur_remove drops the reference
  70. * (cdev->dev.driver_data)
  71. * - ur_open gets a urd reference, ur_relase drops the reference
  72. * (urf->urd)
  73. *
  74. * cdev references:
  75. * - urdev_alloc get a cdev reference (urd->cdev)
  76. * - urdev_free drops the cdev reference (urd->cdev)
  77. *
  78. * Setting and clearing of cdev->dev.driver_data is protected by the ccwdev lock
  79. */
  80. static struct urdev *urdev_alloc(struct ccw_device *cdev)
  81. {
  82. struct urdev *urd;
  83. urd = kzalloc(sizeof(struct urdev), GFP_KERNEL);
  84. if (!urd)
  85. return NULL;
  86. urd->reclen = cdev->id.driver_info;
  87. ccw_device_get_id(cdev, &urd->dev_id);
  88. mutex_init(&urd->io_mutex);
  89. init_waitqueue_head(&urd->wait);
  90. spin_lock_init(&urd->open_lock);
  91. atomic_set(&urd->ref_count, 1);
  92. urd->cdev = cdev;
  93. get_device(&cdev->dev);
  94. return urd;
  95. }
  96. static void urdev_free(struct urdev *urd)
  97. {
  98. TRACE("urdev_free: %p\n", urd);
  99. if (urd->cdev)
  100. put_device(&urd->cdev->dev);
  101. kfree(urd);
  102. }
  103. static void urdev_get(struct urdev *urd)
  104. {
  105. atomic_inc(&urd->ref_count);
  106. }
  107. static struct urdev *urdev_get_from_cdev(struct ccw_device *cdev)
  108. {
  109. struct urdev *urd;
  110. unsigned long flags;
  111. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  112. urd = cdev->dev.driver_data;
  113. if (urd)
  114. urdev_get(urd);
  115. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  116. return urd;
  117. }
  118. static struct urdev *urdev_get_from_devno(u16 devno)
  119. {
  120. char bus_id[16];
  121. struct ccw_device *cdev;
  122. struct urdev *urd;
  123. sprintf(bus_id, "0.0.%04x", devno);
  124. cdev = get_ccwdev_by_busid(&ur_driver, bus_id);
  125. if (!cdev)
  126. return NULL;
  127. urd = urdev_get_from_cdev(cdev);
  128. put_device(&cdev->dev);
  129. return urd;
  130. }
  131. static void urdev_put(struct urdev *urd)
  132. {
  133. if (atomic_dec_and_test(&urd->ref_count))
  134. urdev_free(urd);
  135. }
  136. /*
  137. * Low-level functions to do I/O to a ur device.
  138. * alloc_chan_prog
  139. * free_chan_prog
  140. * do_ur_io
  141. * ur_int_handler
  142. *
  143. * alloc_chan_prog allocates and builds the channel program
  144. * free_chan_prog frees memory of the channel program
  145. *
  146. * do_ur_io issues the channel program to the device and blocks waiting
  147. * on a completion event it publishes at urd->io_done. The function
  148. * serialises itself on the device's mutex so that only one I/O
  149. * is issued at a time (and that I/O is synchronous).
  150. *
  151. * ur_int_handler catches the "I/O done" interrupt, writes the
  152. * subchannel status word into the scsw member of the urdev structure
  153. * and complete()s the io_done to wake the waiting do_ur_io.
  154. *
  155. * The caller of do_ur_io is responsible for kfree()ing the channel program
  156. * address pointer that alloc_chan_prog returned.
  157. */
  158. static void free_chan_prog(struct ccw1 *cpa)
  159. {
  160. struct ccw1 *ptr = cpa;
  161. while (ptr->cda) {
  162. kfree((void *)(addr_t) ptr->cda);
  163. ptr++;
  164. }
  165. kfree(cpa);
  166. }
  167. /*
  168. * alloc_chan_prog
  169. * The channel program we use is write commands chained together
  170. * with a final NOP CCW command-chained on (which ensures that CE and DE
  171. * are presented together in a single interrupt instead of as separate
  172. * interrupts unless an incorrect length indication kicks in first). The
  173. * data length in each CCW is reclen.
  174. */
  175. static struct ccw1 *alloc_chan_prog(const char __user *ubuf, int rec_count,
  176. int reclen)
  177. {
  178. struct ccw1 *cpa;
  179. void *kbuf;
  180. int i;
  181. TRACE("alloc_chan_prog(%p, %i, %i)\n", ubuf, rec_count, reclen);
  182. /*
  183. * We chain a NOP onto the writes to force CE+DE together.
  184. * That means we allocate room for CCWs to cover count/reclen
  185. * records plus a NOP.
  186. */
  187. cpa = kzalloc((rec_count + 1) * sizeof(struct ccw1),
  188. GFP_KERNEL | GFP_DMA);
  189. if (!cpa)
  190. return ERR_PTR(-ENOMEM);
  191. for (i = 0; i < rec_count; i++) {
  192. cpa[i].cmd_code = WRITE_CCW_CMD;
  193. cpa[i].flags = CCW_FLAG_CC | CCW_FLAG_SLI;
  194. cpa[i].count = reclen;
  195. kbuf = kmalloc(reclen, GFP_KERNEL | GFP_DMA);
  196. if (!kbuf) {
  197. free_chan_prog(cpa);
  198. return ERR_PTR(-ENOMEM);
  199. }
  200. cpa[i].cda = (u32)(addr_t) kbuf;
  201. if (copy_from_user(kbuf, ubuf, reclen)) {
  202. free_chan_prog(cpa);
  203. return ERR_PTR(-EFAULT);
  204. }
  205. ubuf += reclen;
  206. }
  207. /* The following NOP CCW forces CE+DE to be presented together */
  208. cpa[i].cmd_code = CCW_CMD_NOOP;
  209. return cpa;
  210. }
  211. static int do_ur_io(struct urdev *urd, struct ccw1 *cpa)
  212. {
  213. int rc;
  214. struct ccw_device *cdev = urd->cdev;
  215. DECLARE_COMPLETION_ONSTACK(event);
  216. TRACE("do_ur_io: cpa=%p\n", cpa);
  217. rc = mutex_lock_interruptible(&urd->io_mutex);
  218. if (rc)
  219. return rc;
  220. urd->io_done = &event;
  221. spin_lock_irq(get_ccwdev_lock(cdev));
  222. rc = ccw_device_start(cdev, cpa, 1, 0, 0);
  223. spin_unlock_irq(get_ccwdev_lock(cdev));
  224. TRACE("do_ur_io: ccw_device_start returned %d\n", rc);
  225. if (rc)
  226. goto out;
  227. wait_for_completion(&event);
  228. TRACE("do_ur_io: I/O complete\n");
  229. rc = 0;
  230. out:
  231. mutex_unlock(&urd->io_mutex);
  232. return rc;
  233. }
  234. /*
  235. * ur interrupt handler, called from the ccw_device layer
  236. */
  237. static void ur_int_handler(struct ccw_device *cdev, unsigned long intparm,
  238. struct irb *irb)
  239. {
  240. struct urdev *urd;
  241. TRACE("ur_int_handler: intparm=0x%lx cstat=%02x dstat=%02x res=%u\n",
  242. intparm, irb->scsw.cmd.cstat, irb->scsw.cmd.dstat,
  243. irb->scsw.cmd.count);
  244. if (!intparm) {
  245. TRACE("ur_int_handler: unsolicited interrupt\n");
  246. return;
  247. }
  248. urd = cdev->dev.driver_data;
  249. BUG_ON(!urd);
  250. /* On special conditions irb is an error pointer */
  251. if (IS_ERR(irb))
  252. urd->io_request_rc = PTR_ERR(irb);
  253. else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END))
  254. urd->io_request_rc = 0;
  255. else
  256. urd->io_request_rc = -EIO;
  257. complete(urd->io_done);
  258. }
  259. /*
  260. * reclen sysfs attribute - The record length to be used for write CCWs
  261. */
  262. static ssize_t ur_attr_reclen_show(struct device *dev,
  263. struct device_attribute *attr, char *buf)
  264. {
  265. struct urdev *urd;
  266. int rc;
  267. urd = urdev_get_from_cdev(to_ccwdev(dev));
  268. if (!urd)
  269. return -ENODEV;
  270. rc = sprintf(buf, "%zu\n", urd->reclen);
  271. urdev_put(urd);
  272. return rc;
  273. }
  274. static DEVICE_ATTR(reclen, 0444, ur_attr_reclen_show, NULL);
  275. static int ur_create_attributes(struct device *dev)
  276. {
  277. return device_create_file(dev, &dev_attr_reclen);
  278. }
  279. static void ur_remove_attributes(struct device *dev)
  280. {
  281. device_remove_file(dev, &dev_attr_reclen);
  282. }
  283. /*
  284. * diagnose code 0x210 - retrieve device information
  285. * cc=0 normal completion, we have a real device
  286. * cc=1 CP paging error
  287. * cc=2 The virtual device exists, but is not associated with a real device
  288. * cc=3 Invalid device address, or the virtual device does not exist
  289. */
  290. static int get_urd_class(struct urdev *urd)
  291. {
  292. static struct diag210 ur_diag210;
  293. int cc;
  294. ur_diag210.vrdcdvno = urd->dev_id.devno;
  295. ur_diag210.vrdclen = sizeof(struct diag210);
  296. cc = diag210(&ur_diag210);
  297. switch (cc) {
  298. case 0:
  299. return -EOPNOTSUPP;
  300. case 2:
  301. return ur_diag210.vrdcvcla; /* virtual device class */
  302. case 3:
  303. return -ENODEV;
  304. default:
  305. return -EIO;
  306. }
  307. }
  308. /*
  309. * Allocation and freeing of urfile structures
  310. */
  311. static struct urfile *urfile_alloc(struct urdev *urd)
  312. {
  313. struct urfile *urf;
  314. urf = kzalloc(sizeof(struct urfile), GFP_KERNEL);
  315. if (!urf)
  316. return NULL;
  317. urf->urd = urd;
  318. TRACE("urfile_alloc: urd=%p urf=%p rl=%zu\n", urd, urf,
  319. urf->dev_reclen);
  320. return urf;
  321. }
  322. static void urfile_free(struct urfile *urf)
  323. {
  324. TRACE("urfile_free: urf=%p urd=%p\n", urf, urf->urd);
  325. kfree(urf);
  326. }
  327. /*
  328. * The fops implementation of the character device driver
  329. */
  330. static ssize_t do_write(struct urdev *urd, const char __user *udata,
  331. size_t count, size_t reclen, loff_t *ppos)
  332. {
  333. struct ccw1 *cpa;
  334. int rc;
  335. cpa = alloc_chan_prog(udata, count / reclen, reclen);
  336. if (IS_ERR(cpa))
  337. return PTR_ERR(cpa);
  338. rc = do_ur_io(urd, cpa);
  339. if (rc)
  340. goto fail_kfree_cpa;
  341. if (urd->io_request_rc) {
  342. rc = urd->io_request_rc;
  343. goto fail_kfree_cpa;
  344. }
  345. *ppos += count;
  346. rc = count;
  347. fail_kfree_cpa:
  348. free_chan_prog(cpa);
  349. return rc;
  350. }
  351. static ssize_t ur_write(struct file *file, const char __user *udata,
  352. size_t count, loff_t *ppos)
  353. {
  354. struct urfile *urf = file->private_data;
  355. TRACE("ur_write: count=%zu\n", count);
  356. if (count == 0)
  357. return 0;
  358. if (count % urf->dev_reclen)
  359. return -EINVAL; /* count must be a multiple of reclen */
  360. if (count > urf->dev_reclen * MAX_RECS_PER_IO)
  361. count = urf->dev_reclen * MAX_RECS_PER_IO;
  362. return do_write(urf->urd, udata, count, urf->dev_reclen, ppos);
  363. }
  364. /*
  365. * diagnose code 0x14 subcode 0x0028 - position spool file to designated
  366. * record
  367. * cc=0 normal completion
  368. * cc=2 no file active on the virtual reader or device not ready
  369. * cc=3 record specified is beyond EOF
  370. */
  371. static int diag_position_to_record(int devno, int record)
  372. {
  373. int cc;
  374. cc = diag14(record, devno, 0x28);
  375. switch (cc) {
  376. case 0:
  377. return 0;
  378. case 2:
  379. return -ENOMEDIUM;
  380. case 3:
  381. return -ENODATA; /* position beyond end of file */
  382. default:
  383. return -EIO;
  384. }
  385. }
  386. /*
  387. * diagnose code 0x14 subcode 0x0000 - read next spool file buffer
  388. * cc=0 normal completion
  389. * cc=1 EOF reached
  390. * cc=2 no file active on the virtual reader, and no file eligible
  391. * cc=3 file already active on the virtual reader or specified virtual
  392. * reader does not exist or is not a reader
  393. */
  394. static int diag_read_file(int devno, char *buf)
  395. {
  396. int cc;
  397. cc = diag14((unsigned long) buf, devno, 0x00);
  398. switch (cc) {
  399. case 0:
  400. return 0;
  401. case 1:
  402. return -ENODATA;
  403. case 2:
  404. return -ENOMEDIUM;
  405. default:
  406. return -EIO;
  407. }
  408. }
  409. static ssize_t diag14_read(struct file *file, char __user *ubuf, size_t count,
  410. loff_t *offs)
  411. {
  412. size_t len, copied, res;
  413. char *buf;
  414. int rc;
  415. u16 reclen;
  416. struct urdev *urd;
  417. urd = ((struct urfile *) file->private_data)->urd;
  418. reclen = ((struct urfile *) file->private_data)->file_reclen;
  419. rc = diag_position_to_record(urd->dev_id.devno, *offs / PAGE_SIZE + 1);
  420. if (rc == -ENODATA)
  421. return 0;
  422. if (rc)
  423. return rc;
  424. len = min((size_t) PAGE_SIZE, count);
  425. buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA);
  426. if (!buf)
  427. return -ENOMEM;
  428. copied = 0;
  429. res = (size_t) (*offs % PAGE_SIZE);
  430. do {
  431. rc = diag_read_file(urd->dev_id.devno, buf);
  432. if (rc == -ENODATA) {
  433. break;
  434. }
  435. if (rc)
  436. goto fail;
  437. if (reclen && (copied == 0) && (*offs < PAGE_SIZE))
  438. *((u16 *) &buf[FILE_RECLEN_OFFSET]) = reclen;
  439. len = min(count - copied, PAGE_SIZE - res);
  440. if (copy_to_user(ubuf + copied, buf + res, len)) {
  441. rc = -EFAULT;
  442. goto fail;
  443. }
  444. res = 0;
  445. copied += len;
  446. } while (copied != count);
  447. *offs += copied;
  448. rc = copied;
  449. fail:
  450. free_page((unsigned long) buf);
  451. return rc;
  452. }
  453. static ssize_t ur_read(struct file *file, char __user *ubuf, size_t count,
  454. loff_t *offs)
  455. {
  456. struct urdev *urd;
  457. int rc;
  458. TRACE("ur_read: count=%zu ppos=%li\n", count, (unsigned long) *offs);
  459. if (count == 0)
  460. return 0;
  461. urd = ((struct urfile *) file->private_data)->urd;
  462. rc = mutex_lock_interruptible(&urd->io_mutex);
  463. if (rc)
  464. return rc;
  465. rc = diag14_read(file, ubuf, count, offs);
  466. mutex_unlock(&urd->io_mutex);
  467. return rc;
  468. }
  469. /*
  470. * diagnose code 0x14 subcode 0x0fff - retrieve next file descriptor
  471. * cc=0 normal completion
  472. * cc=1 no files on reader queue or no subsequent file
  473. * cc=2 spid specified is invalid
  474. */
  475. static int diag_read_next_file_info(struct file_control_block *buf, int spid)
  476. {
  477. int cc;
  478. cc = diag14((unsigned long) buf, spid, 0xfff);
  479. switch (cc) {
  480. case 0:
  481. return 0;
  482. default:
  483. return -ENODATA;
  484. }
  485. }
  486. static int verify_uri_device(struct urdev *urd)
  487. {
  488. struct file_control_block *fcb;
  489. char *buf;
  490. int rc;
  491. fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA);
  492. if (!fcb)
  493. return -ENOMEM;
  494. /* check for empty reader device (beginning of chain) */
  495. rc = diag_read_next_file_info(fcb, 0);
  496. if (rc)
  497. goto fail_free_fcb;
  498. /* if file is in hold status, we do not read it */
  499. if (fcb->file_stat & (FLG_SYSTEM_HOLD | FLG_USER_HOLD)) {
  500. rc = -EPERM;
  501. goto fail_free_fcb;
  502. }
  503. /* open file on virtual reader */
  504. buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA);
  505. if (!buf) {
  506. rc = -ENOMEM;
  507. goto fail_free_fcb;
  508. }
  509. rc = diag_read_file(urd->dev_id.devno, buf);
  510. if ((rc != 0) && (rc != -ENODATA)) /* EOF does not hurt */
  511. goto fail_free_buf;
  512. /* check if the file on top of the queue is open now */
  513. rc = diag_read_next_file_info(fcb, 0);
  514. if (rc)
  515. goto fail_free_buf;
  516. if (!(fcb->file_stat & FLG_IN_USE)) {
  517. rc = -EMFILE;
  518. goto fail_free_buf;
  519. }
  520. rc = 0;
  521. fail_free_buf:
  522. free_page((unsigned long) buf);
  523. fail_free_fcb:
  524. kfree(fcb);
  525. return rc;
  526. }
  527. static int verify_device(struct urdev *urd)
  528. {
  529. switch (urd->class) {
  530. case DEV_CLASS_UR_O:
  531. return 0; /* no check needed here */
  532. case DEV_CLASS_UR_I:
  533. return verify_uri_device(urd);
  534. default:
  535. return -EOPNOTSUPP;
  536. }
  537. }
  538. static int get_uri_file_reclen(struct urdev *urd)
  539. {
  540. struct file_control_block *fcb;
  541. int rc;
  542. fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA);
  543. if (!fcb)
  544. return -ENOMEM;
  545. rc = diag_read_next_file_info(fcb, 0);
  546. if (rc)
  547. goto fail_free;
  548. if (fcb->file_stat & FLG_CP_DUMP)
  549. rc = 0;
  550. else
  551. rc = fcb->rec_len;
  552. fail_free:
  553. kfree(fcb);
  554. return rc;
  555. }
  556. static int get_file_reclen(struct urdev *urd)
  557. {
  558. switch (urd->class) {
  559. case DEV_CLASS_UR_O:
  560. return 0;
  561. case DEV_CLASS_UR_I:
  562. return get_uri_file_reclen(urd);
  563. default:
  564. return -EOPNOTSUPP;
  565. }
  566. }
  567. static int ur_open(struct inode *inode, struct file *file)
  568. {
  569. u16 devno;
  570. struct urdev *urd;
  571. struct urfile *urf;
  572. unsigned short accmode;
  573. int rc;
  574. accmode = file->f_flags & O_ACCMODE;
  575. if (accmode == O_RDWR)
  576. return -EACCES;
  577. lock_kernel();
  578. /*
  579. * We treat the minor number as the devno of the ur device
  580. * to find in the driver tree.
  581. */
  582. devno = MINOR(file->f_dentry->d_inode->i_rdev);
  583. urd = urdev_get_from_devno(devno);
  584. if (!urd) {
  585. rc = -ENXIO;
  586. goto out;
  587. }
  588. spin_lock(&urd->open_lock);
  589. while (urd->open_flag) {
  590. spin_unlock(&urd->open_lock);
  591. if (file->f_flags & O_NONBLOCK) {
  592. rc = -EBUSY;
  593. goto fail_put;
  594. }
  595. if (wait_event_interruptible(urd->wait, urd->open_flag == 0)) {
  596. rc = -ERESTARTSYS;
  597. goto fail_put;
  598. }
  599. spin_lock(&urd->open_lock);
  600. }
  601. urd->open_flag++;
  602. spin_unlock(&urd->open_lock);
  603. TRACE("ur_open\n");
  604. if (((accmode == O_RDONLY) && (urd->class != DEV_CLASS_UR_I)) ||
  605. ((accmode == O_WRONLY) && (urd->class != DEV_CLASS_UR_O))) {
  606. TRACE("ur_open: unsupported dev class (%d)\n", urd->class);
  607. rc = -EACCES;
  608. goto fail_unlock;
  609. }
  610. rc = verify_device(urd);
  611. if (rc)
  612. goto fail_unlock;
  613. urf = urfile_alloc(urd);
  614. if (!urf) {
  615. rc = -ENOMEM;
  616. goto fail_unlock;
  617. }
  618. urf->dev_reclen = urd->reclen;
  619. rc = get_file_reclen(urd);
  620. if (rc < 0)
  621. goto fail_urfile_free;
  622. urf->file_reclen = rc;
  623. file->private_data = urf;
  624. unlock_kernel();
  625. return 0;
  626. fail_urfile_free:
  627. urfile_free(urf);
  628. fail_unlock:
  629. spin_lock(&urd->open_lock);
  630. urd->open_flag--;
  631. spin_unlock(&urd->open_lock);
  632. fail_put:
  633. urdev_put(urd);
  634. out:
  635. unlock_kernel();
  636. return rc;
  637. }
  638. static int ur_release(struct inode *inode, struct file *file)
  639. {
  640. struct urfile *urf = file->private_data;
  641. TRACE("ur_release\n");
  642. spin_lock(&urf->urd->open_lock);
  643. urf->urd->open_flag--;
  644. spin_unlock(&urf->urd->open_lock);
  645. wake_up_interruptible(&urf->urd->wait);
  646. urdev_put(urf->urd);
  647. urfile_free(urf);
  648. return 0;
  649. }
  650. static loff_t ur_llseek(struct file *file, loff_t offset, int whence)
  651. {
  652. loff_t newpos;
  653. if ((file->f_flags & O_ACCMODE) != O_RDONLY)
  654. return -ESPIPE; /* seek allowed only for reader */
  655. if (offset % PAGE_SIZE)
  656. return -ESPIPE; /* only multiples of 4K allowed */
  657. switch (whence) {
  658. case 0: /* SEEK_SET */
  659. newpos = offset;
  660. break;
  661. case 1: /* SEEK_CUR */
  662. newpos = file->f_pos + offset;
  663. break;
  664. default:
  665. return -EINVAL;
  666. }
  667. file->f_pos = newpos;
  668. return newpos;
  669. }
  670. static const struct file_operations ur_fops = {
  671. .owner = THIS_MODULE,
  672. .open = ur_open,
  673. .release = ur_release,
  674. .read = ur_read,
  675. .write = ur_write,
  676. .llseek = ur_llseek,
  677. };
  678. /*
  679. * ccw_device infrastructure:
  680. * ur_probe creates the struct urdev (with refcount = 1), the device
  681. * attributes, sets up the interrupt handler and validates the virtual
  682. * unit record device.
  683. * ur_remove removes the device attributes and drops the reference to
  684. * struct urdev.
  685. *
  686. * ur_probe, ur_remove, ur_set_online and ur_set_offline are serialized
  687. * by the vmur_mutex lock.
  688. *
  689. * urd->char_device is used as indication that the online function has
  690. * been completed successfully.
  691. */
  692. static int ur_probe(struct ccw_device *cdev)
  693. {
  694. struct urdev *urd;
  695. int rc;
  696. TRACE("ur_probe: cdev=%p\n", cdev);
  697. mutex_lock(&vmur_mutex);
  698. urd = urdev_alloc(cdev);
  699. if (!urd) {
  700. rc = -ENOMEM;
  701. goto fail_unlock;
  702. }
  703. rc = ur_create_attributes(&cdev->dev);
  704. if (rc) {
  705. rc = -ENOMEM;
  706. goto fail_urdev_put;
  707. }
  708. cdev->handler = ur_int_handler;
  709. /* validate virtual unit record device */
  710. urd->class = get_urd_class(urd);
  711. if (urd->class < 0) {
  712. rc = urd->class;
  713. goto fail_remove_attr;
  714. }
  715. if ((urd->class != DEV_CLASS_UR_I) && (urd->class != DEV_CLASS_UR_O)) {
  716. rc = -EOPNOTSUPP;
  717. goto fail_remove_attr;
  718. }
  719. spin_lock_irq(get_ccwdev_lock(cdev));
  720. cdev->dev.driver_data = urd;
  721. spin_unlock_irq(get_ccwdev_lock(cdev));
  722. mutex_unlock(&vmur_mutex);
  723. return 0;
  724. fail_remove_attr:
  725. ur_remove_attributes(&cdev->dev);
  726. fail_urdev_put:
  727. urdev_put(urd);
  728. fail_unlock:
  729. mutex_unlock(&vmur_mutex);
  730. return rc;
  731. }
  732. static int ur_set_online(struct ccw_device *cdev)
  733. {
  734. struct urdev *urd;
  735. int minor, major, rc;
  736. char node_id[16];
  737. TRACE("ur_set_online: cdev=%p\n", cdev);
  738. mutex_lock(&vmur_mutex);
  739. urd = urdev_get_from_cdev(cdev);
  740. if (!urd) {
  741. /* ur_remove already deleted our urd */
  742. rc = -ENODEV;
  743. goto fail_unlock;
  744. }
  745. if (urd->char_device) {
  746. /* Another ur_set_online was faster */
  747. rc = -EBUSY;
  748. goto fail_urdev_put;
  749. }
  750. minor = urd->dev_id.devno;
  751. major = MAJOR(ur_first_dev_maj_min);
  752. urd->char_device = cdev_alloc();
  753. if (!urd->char_device) {
  754. rc = -ENOMEM;
  755. goto fail_urdev_put;
  756. }
  757. cdev_init(urd->char_device, &ur_fops);
  758. urd->char_device->dev = MKDEV(major, minor);
  759. urd->char_device->owner = ur_fops.owner;
  760. rc = cdev_add(urd->char_device, urd->char_device->dev, 1);
  761. if (rc)
  762. goto fail_free_cdev;
  763. if (urd->cdev->id.cu_type == READER_PUNCH_DEVTYPE) {
  764. if (urd->class == DEV_CLASS_UR_I)
  765. sprintf(node_id, "vmrdr-%s", dev_name(&cdev->dev));
  766. if (urd->class == DEV_CLASS_UR_O)
  767. sprintf(node_id, "vmpun-%s", dev_name(&cdev->dev));
  768. } else if (urd->cdev->id.cu_type == PRINTER_DEVTYPE) {
  769. sprintf(node_id, "vmprt-%s", dev_name(&cdev->dev));
  770. } else {
  771. rc = -EOPNOTSUPP;
  772. goto fail_free_cdev;
  773. }
  774. urd->device = device_create_drvdata(vmur_class, NULL,
  775. urd->char_device->dev, NULL,
  776. "%s", node_id);
  777. if (IS_ERR(urd->device)) {
  778. rc = PTR_ERR(urd->device);
  779. TRACE("ur_set_online: device_create rc=%d\n", rc);
  780. goto fail_free_cdev;
  781. }
  782. urdev_put(urd);
  783. mutex_unlock(&vmur_mutex);
  784. return 0;
  785. fail_free_cdev:
  786. cdev_del(urd->char_device);
  787. urd->char_device = NULL;
  788. fail_urdev_put:
  789. urdev_put(urd);
  790. fail_unlock:
  791. mutex_unlock(&vmur_mutex);
  792. return rc;
  793. }
  794. static int ur_set_offline_force(struct ccw_device *cdev, int force)
  795. {
  796. struct urdev *urd;
  797. int rc;
  798. TRACE("ur_set_offline: cdev=%p\n", cdev);
  799. urd = urdev_get_from_cdev(cdev);
  800. if (!urd)
  801. /* ur_remove already deleted our urd */
  802. return -ENODEV;
  803. if (!urd->char_device) {
  804. /* Another ur_set_offline was faster */
  805. rc = -EBUSY;
  806. goto fail_urdev_put;
  807. }
  808. if (!force && (atomic_read(&urd->ref_count) > 2)) {
  809. /* There is still a user of urd (e.g. ur_open) */
  810. TRACE("ur_set_offline: BUSY\n");
  811. rc = -EBUSY;
  812. goto fail_urdev_put;
  813. }
  814. device_destroy(vmur_class, urd->char_device->dev);
  815. cdev_del(urd->char_device);
  816. urd->char_device = NULL;
  817. rc = 0;
  818. fail_urdev_put:
  819. urdev_put(urd);
  820. return rc;
  821. }
  822. static int ur_set_offline(struct ccw_device *cdev)
  823. {
  824. int rc;
  825. mutex_lock(&vmur_mutex);
  826. rc = ur_set_offline_force(cdev, 0);
  827. mutex_unlock(&vmur_mutex);
  828. return rc;
  829. }
  830. static void ur_remove(struct ccw_device *cdev)
  831. {
  832. unsigned long flags;
  833. TRACE("ur_remove\n");
  834. mutex_lock(&vmur_mutex);
  835. if (cdev->online)
  836. ur_set_offline_force(cdev, 1);
  837. ur_remove_attributes(&cdev->dev);
  838. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  839. urdev_put(cdev->dev.driver_data);
  840. cdev->dev.driver_data = NULL;
  841. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  842. mutex_unlock(&vmur_mutex);
  843. }
  844. /*
  845. * Module initialisation and cleanup
  846. */
  847. static int __init ur_init(void)
  848. {
  849. int rc;
  850. dev_t dev;
  851. if (!MACHINE_IS_VM) {
  852. PRINT_ERR("%s is only available under z/VM.\n", ur_banner);
  853. return -ENODEV;
  854. }
  855. vmur_dbf = debug_register("vmur", 4, 1, 4 * sizeof(long));
  856. if (!vmur_dbf)
  857. return -ENOMEM;
  858. rc = debug_register_view(vmur_dbf, &debug_sprintf_view);
  859. if (rc)
  860. goto fail_free_dbf;
  861. debug_set_level(vmur_dbf, 6);
  862. rc = ccw_driver_register(&ur_driver);
  863. if (rc)
  864. goto fail_free_dbf;
  865. rc = alloc_chrdev_region(&dev, 0, NUM_MINORS, "vmur");
  866. if (rc) {
  867. PRINT_ERR("alloc_chrdev_region failed: err = %d\n", rc);
  868. goto fail_unregister_driver;
  869. }
  870. ur_first_dev_maj_min = MKDEV(MAJOR(dev), 0);
  871. vmur_class = class_create(THIS_MODULE, "vmur");
  872. if (IS_ERR(vmur_class)) {
  873. rc = PTR_ERR(vmur_class);
  874. goto fail_unregister_region;
  875. }
  876. PRINT_INFO("%s loaded.\n", ur_banner);
  877. return 0;
  878. fail_unregister_region:
  879. unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS);
  880. fail_unregister_driver:
  881. ccw_driver_unregister(&ur_driver);
  882. fail_free_dbf:
  883. debug_unregister(vmur_dbf);
  884. return rc;
  885. }
  886. static void __exit ur_exit(void)
  887. {
  888. class_destroy(vmur_class);
  889. unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS);
  890. ccw_driver_unregister(&ur_driver);
  891. debug_unregister(vmur_dbf);
  892. PRINT_INFO("%s unloaded.\n", ur_banner);
  893. }
  894. module_init(ur_init);
  895. module_exit(ur_exit);