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