vmur.c 24 KB

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