vmur.c 24 KB

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