vmur.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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 <linux/module.h>
  16. #include <asm/uaccess.h>
  17. #include <asm/cio.h>
  18. #include <asm/ccwdev.h>
  19. #include <asm/debug.h>
  20. #include <asm/diag.h>
  21. #include "vmur.h"
  22. /*
  23. * Driver overview
  24. *
  25. * Unit record device support is implemented as a character device driver.
  26. * We can fit at least 16 bits into a device minor number and use the
  27. * simple method of mapping a character device number with minor abcd
  28. * to the unit record device with devno abcd.
  29. * I/O to virtual unit record devices is handled as follows:
  30. * Reads: Diagnose code 0x14 (input spool file manipulation)
  31. * is used to read spool data page-wise.
  32. * Writes: The CCW used is WRITE_CCW_CMD (0x01). The device's record length
  33. * is available by reading sysfs attr reclen. Each write() to the device
  34. * must specify an integral multiple (maximal 511) of reclen.
  35. */
  36. static char ur_banner[] = "z/VM virtual unit record device driver";
  37. MODULE_AUTHOR("IBM Corporation");
  38. MODULE_DESCRIPTION("s390 z/VM virtual unit record device driver");
  39. MODULE_LICENSE("GPL");
  40. static dev_t ur_first_dev_maj_min;
  41. static struct class *vmur_class;
  42. static struct debug_info *vmur_dbf;
  43. /* We put the device's record length (for writes) in the driver_info field */
  44. static struct ccw_device_id ur_ids[] = {
  45. { CCWDEV_CU_DI(READER_PUNCH_DEVTYPE, 80) },
  46. { CCWDEV_CU_DI(PRINTER_DEVTYPE, 132) },
  47. { /* end of list */ }
  48. };
  49. MODULE_DEVICE_TABLE(ccw, ur_ids);
  50. static int ur_probe(struct ccw_device *cdev);
  51. static void ur_remove(struct ccw_device *cdev);
  52. static int ur_set_online(struct ccw_device *cdev);
  53. static int ur_set_offline(struct ccw_device *cdev);
  54. static int ur_pm_suspend(struct ccw_device *cdev);
  55. static struct ccw_driver ur_driver = {
  56. .driver = {
  57. .name = "vmur",
  58. .owner = THIS_MODULE,
  59. },
  60. .ids = ur_ids,
  61. .probe = ur_probe,
  62. .remove = ur_remove,
  63. .set_online = ur_set_online,
  64. .set_offline = ur_set_offline,
  65. .freeze = ur_pm_suspend,
  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_relase 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. kstat_cpu(smp_processor_id()).irqs[IOINT_VMR]++;
  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. if (!intparm) {
  273. TRACE("ur_int_handler: unsolicited interrupt\n");
  274. return;
  275. }
  276. urd = dev_get_drvdata(&cdev->dev);
  277. BUG_ON(!urd);
  278. /* On special conditions irb is an error pointer */
  279. if (IS_ERR(irb))
  280. urd->io_request_rc = PTR_ERR(irb);
  281. else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END))
  282. urd->io_request_rc = 0;
  283. else
  284. urd->io_request_rc = -EIO;
  285. complete(urd->io_done);
  286. }
  287. /*
  288. * reclen sysfs attribute - The record length to be used for write CCWs
  289. */
  290. static ssize_t ur_attr_reclen_show(struct device *dev,
  291. struct device_attribute *attr, char *buf)
  292. {
  293. struct urdev *urd;
  294. int rc;
  295. urd = urdev_get_from_cdev(to_ccwdev(dev));
  296. if (!urd)
  297. return -ENODEV;
  298. rc = sprintf(buf, "%zu\n", urd->reclen);
  299. urdev_put(urd);
  300. return rc;
  301. }
  302. static DEVICE_ATTR(reclen, 0444, ur_attr_reclen_show, NULL);
  303. static int ur_create_attributes(struct device *dev)
  304. {
  305. return device_create_file(dev, &dev_attr_reclen);
  306. }
  307. static void ur_remove_attributes(struct device *dev)
  308. {
  309. device_remove_file(dev, &dev_attr_reclen);
  310. }
  311. /*
  312. * diagnose code 0x210 - retrieve device information
  313. * cc=0 normal completion, we have a real device
  314. * cc=1 CP paging error
  315. * cc=2 The virtual device exists, but is not associated with a real device
  316. * cc=3 Invalid device address, or the virtual device does not exist
  317. */
  318. static int get_urd_class(struct urdev *urd)
  319. {
  320. static struct diag210 ur_diag210;
  321. int cc;
  322. ur_diag210.vrdcdvno = urd->dev_id.devno;
  323. ur_diag210.vrdclen = sizeof(struct diag210);
  324. cc = diag210(&ur_diag210);
  325. switch (cc) {
  326. case 0:
  327. return -EOPNOTSUPP;
  328. case 2:
  329. return ur_diag210.vrdcvcla; /* virtual device class */
  330. case 3:
  331. return -ENODEV;
  332. default:
  333. return -EIO;
  334. }
  335. }
  336. /*
  337. * Allocation and freeing of urfile structures
  338. */
  339. static struct urfile *urfile_alloc(struct urdev *urd)
  340. {
  341. struct urfile *urf;
  342. urf = kzalloc(sizeof(struct urfile), GFP_KERNEL);
  343. if (!urf)
  344. return NULL;
  345. urf->urd = urd;
  346. TRACE("urfile_alloc: urd=%p urf=%p rl=%zu\n", urd, urf,
  347. urf->dev_reclen);
  348. return urf;
  349. }
  350. static void urfile_free(struct urfile *urf)
  351. {
  352. TRACE("urfile_free: urf=%p urd=%p\n", urf, urf->urd);
  353. kfree(urf);
  354. }
  355. /*
  356. * The fops implementation of the character device driver
  357. */
  358. static ssize_t do_write(struct urdev *urd, const char __user *udata,
  359. size_t count, size_t reclen, loff_t *ppos)
  360. {
  361. struct ccw1 *cpa;
  362. int rc;
  363. cpa = alloc_chan_prog(udata, count / reclen, reclen);
  364. if (IS_ERR(cpa))
  365. return PTR_ERR(cpa);
  366. rc = do_ur_io(urd, cpa);
  367. if (rc)
  368. goto fail_kfree_cpa;
  369. if (urd->io_request_rc) {
  370. rc = urd->io_request_rc;
  371. goto fail_kfree_cpa;
  372. }
  373. *ppos += count;
  374. rc = count;
  375. fail_kfree_cpa:
  376. free_chan_prog(cpa);
  377. return rc;
  378. }
  379. static ssize_t ur_write(struct file *file, const char __user *udata,
  380. size_t count, loff_t *ppos)
  381. {
  382. struct urfile *urf = file->private_data;
  383. TRACE("ur_write: count=%zu\n", count);
  384. if (count == 0)
  385. return 0;
  386. if (count % urf->dev_reclen)
  387. return -EINVAL; /* count must be a multiple of reclen */
  388. if (count > urf->dev_reclen * MAX_RECS_PER_IO)
  389. count = urf->dev_reclen * MAX_RECS_PER_IO;
  390. return do_write(urf->urd, udata, count, urf->dev_reclen, ppos);
  391. }
  392. /*
  393. * diagnose code 0x14 subcode 0x0028 - position spool file to designated
  394. * record
  395. * cc=0 normal completion
  396. * cc=2 no file active on the virtual reader or device not ready
  397. * cc=3 record specified is beyond EOF
  398. */
  399. static int diag_position_to_record(int devno, int record)
  400. {
  401. int cc;
  402. cc = diag14(record, devno, 0x28);
  403. switch (cc) {
  404. case 0:
  405. return 0;
  406. case 2:
  407. return -ENOMEDIUM;
  408. case 3:
  409. return -ENODATA; /* position beyond end of file */
  410. default:
  411. return -EIO;
  412. }
  413. }
  414. /*
  415. * diagnose code 0x14 subcode 0x0000 - read next spool file buffer
  416. * cc=0 normal completion
  417. * cc=1 EOF reached
  418. * cc=2 no file active on the virtual reader, and no file eligible
  419. * cc=3 file already active on the virtual reader or specified virtual
  420. * reader does not exist or is not a reader
  421. */
  422. static int diag_read_file(int devno, char *buf)
  423. {
  424. int cc;
  425. cc = diag14((unsigned long) buf, devno, 0x00);
  426. switch (cc) {
  427. case 0:
  428. return 0;
  429. case 1:
  430. return -ENODATA;
  431. case 2:
  432. return -ENOMEDIUM;
  433. default:
  434. return -EIO;
  435. }
  436. }
  437. static ssize_t diag14_read(struct file *file, char __user *ubuf, size_t count,
  438. loff_t *offs)
  439. {
  440. size_t len, copied, res;
  441. char *buf;
  442. int rc;
  443. u16 reclen;
  444. struct urdev *urd;
  445. urd = ((struct urfile *) file->private_data)->urd;
  446. reclen = ((struct urfile *) file->private_data)->file_reclen;
  447. rc = diag_position_to_record(urd->dev_id.devno, *offs / PAGE_SIZE + 1);
  448. if (rc == -ENODATA)
  449. return 0;
  450. if (rc)
  451. return rc;
  452. len = min((size_t) PAGE_SIZE, count);
  453. buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA);
  454. if (!buf)
  455. return -ENOMEM;
  456. copied = 0;
  457. res = (size_t) (*offs % PAGE_SIZE);
  458. do {
  459. rc = diag_read_file(urd->dev_id.devno, buf);
  460. if (rc == -ENODATA) {
  461. break;
  462. }
  463. if (rc)
  464. goto fail;
  465. if (reclen && (copied == 0) && (*offs < PAGE_SIZE))
  466. *((u16 *) &buf[FILE_RECLEN_OFFSET]) = reclen;
  467. len = min(count - copied, PAGE_SIZE - res);
  468. if (copy_to_user(ubuf + copied, buf + res, len)) {
  469. rc = -EFAULT;
  470. goto fail;
  471. }
  472. res = 0;
  473. copied += len;
  474. } while (copied != count);
  475. *offs += copied;
  476. rc = copied;
  477. fail:
  478. free_page((unsigned long) buf);
  479. return rc;
  480. }
  481. static ssize_t ur_read(struct file *file, char __user *ubuf, size_t count,
  482. loff_t *offs)
  483. {
  484. struct urdev *urd;
  485. int rc;
  486. TRACE("ur_read: count=%zu ppos=%li\n", count, (unsigned long) *offs);
  487. if (count == 0)
  488. return 0;
  489. urd = ((struct urfile *) file->private_data)->urd;
  490. rc = mutex_lock_interruptible(&urd->io_mutex);
  491. if (rc)
  492. return rc;
  493. rc = diag14_read(file, ubuf, count, offs);
  494. mutex_unlock(&urd->io_mutex);
  495. return rc;
  496. }
  497. /*
  498. * diagnose code 0x14 subcode 0x0fff - retrieve next file descriptor
  499. * cc=0 normal completion
  500. * cc=1 no files on reader queue or no subsequent file
  501. * cc=2 spid specified is invalid
  502. */
  503. static int diag_read_next_file_info(struct file_control_block *buf, int spid)
  504. {
  505. int cc;
  506. cc = diag14((unsigned long) buf, spid, 0xfff);
  507. switch (cc) {
  508. case 0:
  509. return 0;
  510. default:
  511. return -ENODATA;
  512. }
  513. }
  514. static int verify_uri_device(struct urdev *urd)
  515. {
  516. struct file_control_block *fcb;
  517. char *buf;
  518. int rc;
  519. fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA);
  520. if (!fcb)
  521. return -ENOMEM;
  522. /* check for empty reader device (beginning of chain) */
  523. rc = diag_read_next_file_info(fcb, 0);
  524. if (rc)
  525. goto fail_free_fcb;
  526. /* if file is in hold status, we do not read it */
  527. if (fcb->file_stat & (FLG_SYSTEM_HOLD | FLG_USER_HOLD)) {
  528. rc = -EPERM;
  529. goto fail_free_fcb;
  530. }
  531. /* open file on virtual reader */
  532. buf = (char *) __get_free_page(GFP_KERNEL | GFP_DMA);
  533. if (!buf) {
  534. rc = -ENOMEM;
  535. goto fail_free_fcb;
  536. }
  537. rc = diag_read_file(urd->dev_id.devno, buf);
  538. if ((rc != 0) && (rc != -ENODATA)) /* EOF does not hurt */
  539. goto fail_free_buf;
  540. /* check if the file on top of the queue is open now */
  541. rc = diag_read_next_file_info(fcb, 0);
  542. if (rc)
  543. goto fail_free_buf;
  544. if (!(fcb->file_stat & FLG_IN_USE)) {
  545. rc = -EMFILE;
  546. goto fail_free_buf;
  547. }
  548. rc = 0;
  549. fail_free_buf:
  550. free_page((unsigned long) buf);
  551. fail_free_fcb:
  552. kfree(fcb);
  553. return rc;
  554. }
  555. static int verify_device(struct urdev *urd)
  556. {
  557. switch (urd->class) {
  558. case DEV_CLASS_UR_O:
  559. return 0; /* no check needed here */
  560. case DEV_CLASS_UR_I:
  561. return verify_uri_device(urd);
  562. default:
  563. return -EOPNOTSUPP;
  564. }
  565. }
  566. static int get_uri_file_reclen(struct urdev *urd)
  567. {
  568. struct file_control_block *fcb;
  569. int rc;
  570. fcb = kmalloc(sizeof(*fcb), GFP_KERNEL | GFP_DMA);
  571. if (!fcb)
  572. return -ENOMEM;
  573. rc = diag_read_next_file_info(fcb, 0);
  574. if (rc)
  575. goto fail_free;
  576. if (fcb->file_stat & FLG_CP_DUMP)
  577. rc = 0;
  578. else
  579. rc = fcb->rec_len;
  580. fail_free:
  581. kfree(fcb);
  582. return rc;
  583. }
  584. static int get_file_reclen(struct urdev *urd)
  585. {
  586. switch (urd->class) {
  587. case DEV_CLASS_UR_O:
  588. return 0;
  589. case DEV_CLASS_UR_I:
  590. return get_uri_file_reclen(urd);
  591. default:
  592. return -EOPNOTSUPP;
  593. }
  594. }
  595. static int ur_open(struct inode *inode, struct file *file)
  596. {
  597. u16 devno;
  598. struct urdev *urd;
  599. struct urfile *urf;
  600. unsigned short accmode;
  601. int rc;
  602. accmode = file->f_flags & O_ACCMODE;
  603. if (accmode == O_RDWR)
  604. return -EACCES;
  605. /*
  606. * We treat the minor number as the devno of the ur device
  607. * to find in the driver tree.
  608. */
  609. devno = MINOR(file->f_dentry->d_inode->i_rdev);
  610. urd = urdev_get_from_devno(devno);
  611. if (!urd) {
  612. rc = -ENXIO;
  613. goto out;
  614. }
  615. spin_lock(&urd->open_lock);
  616. while (urd->open_flag) {
  617. spin_unlock(&urd->open_lock);
  618. if (file->f_flags & O_NONBLOCK) {
  619. rc = -EBUSY;
  620. goto fail_put;
  621. }
  622. if (wait_event_interruptible(urd->wait, urd->open_flag == 0)) {
  623. rc = -ERESTARTSYS;
  624. goto fail_put;
  625. }
  626. spin_lock(&urd->open_lock);
  627. }
  628. urd->open_flag++;
  629. spin_unlock(&urd->open_lock);
  630. TRACE("ur_open\n");
  631. if (((accmode == O_RDONLY) && (urd->class != DEV_CLASS_UR_I)) ||
  632. ((accmode == O_WRONLY) && (urd->class != DEV_CLASS_UR_O))) {
  633. TRACE("ur_open: unsupported dev class (%d)\n", urd->class);
  634. rc = -EACCES;
  635. goto fail_unlock;
  636. }
  637. rc = verify_device(urd);
  638. if (rc)
  639. goto fail_unlock;
  640. urf = urfile_alloc(urd);
  641. if (!urf) {
  642. rc = -ENOMEM;
  643. goto fail_unlock;
  644. }
  645. urf->dev_reclen = urd->reclen;
  646. rc = get_file_reclen(urd);
  647. if (rc < 0)
  648. goto fail_urfile_free;
  649. urf->file_reclen = rc;
  650. file->private_data = urf;
  651. return 0;
  652. fail_urfile_free:
  653. urfile_free(urf);
  654. fail_unlock:
  655. spin_lock(&urd->open_lock);
  656. urd->open_flag--;
  657. spin_unlock(&urd->open_lock);
  658. fail_put:
  659. urdev_put(urd);
  660. out:
  661. return rc;
  662. }
  663. static int ur_release(struct inode *inode, struct file *file)
  664. {
  665. struct urfile *urf = file->private_data;
  666. TRACE("ur_release\n");
  667. spin_lock(&urf->urd->open_lock);
  668. urf->urd->open_flag--;
  669. spin_unlock(&urf->urd->open_lock);
  670. wake_up_interruptible(&urf->urd->wait);
  671. urdev_put(urf->urd);
  672. urfile_free(urf);
  673. return 0;
  674. }
  675. static loff_t ur_llseek(struct file *file, loff_t offset, int whence)
  676. {
  677. loff_t newpos;
  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. switch (whence) {
  683. case 0: /* SEEK_SET */
  684. newpos = offset;
  685. break;
  686. case 1: /* SEEK_CUR */
  687. newpos = file->f_pos + offset;
  688. break;
  689. default:
  690. return -EINVAL;
  691. }
  692. file->f_pos = newpos;
  693. return newpos;
  694. }
  695. static const struct file_operations ur_fops = {
  696. .owner = THIS_MODULE,
  697. .open = ur_open,
  698. .release = ur_release,
  699. .read = ur_read,
  700. .write = ur_write,
  701. .llseek = ur_llseek,
  702. };
  703. /*
  704. * ccw_device infrastructure:
  705. * ur_probe creates the struct urdev (with refcount = 1), the device
  706. * attributes, sets up the interrupt handler and validates the virtual
  707. * unit record device.
  708. * ur_remove removes the device attributes and drops the reference to
  709. * struct urdev.
  710. *
  711. * ur_probe, ur_remove, ur_set_online and ur_set_offline are serialized
  712. * by the vmur_mutex lock.
  713. *
  714. * urd->char_device is used as indication that the online function has
  715. * been completed successfully.
  716. */
  717. static int ur_probe(struct ccw_device *cdev)
  718. {
  719. struct urdev *urd;
  720. int rc;
  721. TRACE("ur_probe: cdev=%p\n", cdev);
  722. mutex_lock(&vmur_mutex);
  723. urd = urdev_alloc(cdev);
  724. if (!urd) {
  725. rc = -ENOMEM;
  726. goto fail_unlock;
  727. }
  728. rc = ur_create_attributes(&cdev->dev);
  729. if (rc) {
  730. rc = -ENOMEM;
  731. goto fail_urdev_put;
  732. }
  733. cdev->handler = ur_int_handler;
  734. /* validate virtual unit record device */
  735. urd->class = get_urd_class(urd);
  736. if (urd->class < 0) {
  737. rc = urd->class;
  738. goto fail_remove_attr;
  739. }
  740. if ((urd->class != DEV_CLASS_UR_I) && (urd->class != DEV_CLASS_UR_O)) {
  741. rc = -EOPNOTSUPP;
  742. goto fail_remove_attr;
  743. }
  744. spin_lock_irq(get_ccwdev_lock(cdev));
  745. dev_set_drvdata(&cdev->dev, urd);
  746. spin_unlock_irq(get_ccwdev_lock(cdev));
  747. mutex_unlock(&vmur_mutex);
  748. return 0;
  749. fail_remove_attr:
  750. ur_remove_attributes(&cdev->dev);
  751. fail_urdev_put:
  752. urdev_put(urd);
  753. fail_unlock:
  754. mutex_unlock(&vmur_mutex);
  755. return rc;
  756. }
  757. static int ur_set_online(struct ccw_device *cdev)
  758. {
  759. struct urdev *urd;
  760. int minor, major, rc;
  761. char node_id[16];
  762. TRACE("ur_set_online: cdev=%p\n", cdev);
  763. mutex_lock(&vmur_mutex);
  764. urd = urdev_get_from_cdev(cdev);
  765. if (!urd) {
  766. /* ur_remove already deleted our urd */
  767. rc = -ENODEV;
  768. goto fail_unlock;
  769. }
  770. if (urd->char_device) {
  771. /* Another ur_set_online was faster */
  772. rc = -EBUSY;
  773. goto fail_urdev_put;
  774. }
  775. minor = urd->dev_id.devno;
  776. major = MAJOR(ur_first_dev_maj_min);
  777. urd->char_device = cdev_alloc();
  778. if (!urd->char_device) {
  779. rc = -ENOMEM;
  780. goto fail_urdev_put;
  781. }
  782. cdev_init(urd->char_device, &ur_fops);
  783. urd->char_device->dev = MKDEV(major, minor);
  784. urd->char_device->owner = ur_fops.owner;
  785. rc = cdev_add(urd->char_device, urd->char_device->dev, 1);
  786. if (rc)
  787. goto fail_free_cdev;
  788. if (urd->cdev->id.cu_type == READER_PUNCH_DEVTYPE) {
  789. if (urd->class == DEV_CLASS_UR_I)
  790. sprintf(node_id, "vmrdr-%s", dev_name(&cdev->dev));
  791. if (urd->class == DEV_CLASS_UR_O)
  792. sprintf(node_id, "vmpun-%s", dev_name(&cdev->dev));
  793. } else if (urd->cdev->id.cu_type == PRINTER_DEVTYPE) {
  794. sprintf(node_id, "vmprt-%s", dev_name(&cdev->dev));
  795. } else {
  796. rc = -EOPNOTSUPP;
  797. goto fail_free_cdev;
  798. }
  799. urd->device = device_create(vmur_class, NULL, urd->char_device->dev,
  800. NULL, "%s", node_id);
  801. if (IS_ERR(urd->device)) {
  802. rc = PTR_ERR(urd->device);
  803. TRACE("ur_set_online: device_create rc=%d\n", rc);
  804. goto fail_free_cdev;
  805. }
  806. urdev_put(urd);
  807. mutex_unlock(&vmur_mutex);
  808. return 0;
  809. fail_free_cdev:
  810. cdev_del(urd->char_device);
  811. urd->char_device = NULL;
  812. fail_urdev_put:
  813. urdev_put(urd);
  814. fail_unlock:
  815. mutex_unlock(&vmur_mutex);
  816. return rc;
  817. }
  818. static int ur_set_offline_force(struct ccw_device *cdev, int force)
  819. {
  820. struct urdev *urd;
  821. int rc;
  822. TRACE("ur_set_offline: cdev=%p\n", cdev);
  823. urd = urdev_get_from_cdev(cdev);
  824. if (!urd)
  825. /* ur_remove already deleted our urd */
  826. return -ENODEV;
  827. if (!urd->char_device) {
  828. /* Another ur_set_offline was faster */
  829. rc = -EBUSY;
  830. goto fail_urdev_put;
  831. }
  832. if (!force && (atomic_read(&urd->ref_count) > 2)) {
  833. /* There is still a user of urd (e.g. ur_open) */
  834. TRACE("ur_set_offline: BUSY\n");
  835. rc = -EBUSY;
  836. goto fail_urdev_put;
  837. }
  838. device_destroy(vmur_class, urd->char_device->dev);
  839. cdev_del(urd->char_device);
  840. urd->char_device = NULL;
  841. rc = 0;
  842. fail_urdev_put:
  843. urdev_put(urd);
  844. return rc;
  845. }
  846. static int ur_set_offline(struct ccw_device *cdev)
  847. {
  848. int rc;
  849. mutex_lock(&vmur_mutex);
  850. rc = ur_set_offline_force(cdev, 0);
  851. mutex_unlock(&vmur_mutex);
  852. return rc;
  853. }
  854. static void ur_remove(struct ccw_device *cdev)
  855. {
  856. unsigned long flags;
  857. TRACE("ur_remove\n");
  858. mutex_lock(&vmur_mutex);
  859. if (cdev->online)
  860. ur_set_offline_force(cdev, 1);
  861. ur_remove_attributes(&cdev->dev);
  862. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  863. urdev_put(dev_get_drvdata(&cdev->dev));
  864. dev_set_drvdata(&cdev->dev, NULL);
  865. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  866. mutex_unlock(&vmur_mutex);
  867. }
  868. /*
  869. * Module initialisation and cleanup
  870. */
  871. static int __init ur_init(void)
  872. {
  873. int rc;
  874. dev_t dev;
  875. if (!MACHINE_IS_VM) {
  876. pr_err("The %s cannot be loaded without z/VM\n",
  877. ur_banner);
  878. return -ENODEV;
  879. }
  880. vmur_dbf = debug_register("vmur", 4, 1, 4 * sizeof(long));
  881. if (!vmur_dbf)
  882. return -ENOMEM;
  883. rc = debug_register_view(vmur_dbf, &debug_sprintf_view);
  884. if (rc)
  885. goto fail_free_dbf;
  886. debug_set_level(vmur_dbf, 6);
  887. vmur_class = class_create(THIS_MODULE, "vmur");
  888. if (IS_ERR(vmur_class)) {
  889. rc = PTR_ERR(vmur_class);
  890. goto fail_free_dbf;
  891. }
  892. rc = ccw_driver_register(&ur_driver);
  893. if (rc)
  894. goto fail_class_destroy;
  895. rc = alloc_chrdev_region(&dev, 0, NUM_MINORS, "vmur");
  896. if (rc) {
  897. pr_err("Kernel function alloc_chrdev_region failed with "
  898. "error code %d\n", rc);
  899. goto fail_unregister_driver;
  900. }
  901. ur_first_dev_maj_min = MKDEV(MAJOR(dev), 0);
  902. pr_info("%s loaded.\n", ur_banner);
  903. return 0;
  904. fail_unregister_driver:
  905. ccw_driver_unregister(&ur_driver);
  906. fail_class_destroy:
  907. class_destroy(vmur_class);
  908. fail_free_dbf:
  909. debug_unregister(vmur_dbf);
  910. return rc;
  911. }
  912. static void __exit ur_exit(void)
  913. {
  914. unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS);
  915. ccw_driver_unregister(&ur_driver);
  916. class_destroy(vmur_class);
  917. debug_unregister(vmur_dbf);
  918. pr_info("%s unloaded.\n", ur_banner);
  919. }
  920. module_init(ur_init);
  921. module_exit(ur_exit);