vmur.c 24 KB

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