vmur.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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. /*
  601. * We treat the minor number as the devno of the ur device
  602. * to find in the driver tree.
  603. */
  604. devno = MINOR(file->f_dentry->d_inode->i_rdev);
  605. urd = urdev_get_from_devno(devno);
  606. if (!urd) {
  607. rc = -ENXIO;
  608. goto out;
  609. }
  610. spin_lock(&urd->open_lock);
  611. while (urd->open_flag) {
  612. spin_unlock(&urd->open_lock);
  613. if (file->f_flags & O_NONBLOCK) {
  614. rc = -EBUSY;
  615. goto fail_put;
  616. }
  617. if (wait_event_interruptible(urd->wait, urd->open_flag == 0)) {
  618. rc = -ERESTARTSYS;
  619. goto fail_put;
  620. }
  621. spin_lock(&urd->open_lock);
  622. }
  623. urd->open_flag++;
  624. spin_unlock(&urd->open_lock);
  625. TRACE("ur_open\n");
  626. if (((accmode == O_RDONLY) && (urd->class != DEV_CLASS_UR_I)) ||
  627. ((accmode == O_WRONLY) && (urd->class != DEV_CLASS_UR_O))) {
  628. TRACE("ur_open: unsupported dev class (%d)\n", urd->class);
  629. rc = -EACCES;
  630. goto fail_unlock;
  631. }
  632. rc = verify_device(urd);
  633. if (rc)
  634. goto fail_unlock;
  635. urf = urfile_alloc(urd);
  636. if (!urf) {
  637. rc = -ENOMEM;
  638. goto fail_unlock;
  639. }
  640. urf->dev_reclen = urd->reclen;
  641. rc = get_file_reclen(urd);
  642. if (rc < 0)
  643. goto fail_urfile_free;
  644. urf->file_reclen = rc;
  645. file->private_data = urf;
  646. return 0;
  647. fail_urfile_free:
  648. urfile_free(urf);
  649. fail_unlock:
  650. spin_lock(&urd->open_lock);
  651. urd->open_flag--;
  652. spin_unlock(&urd->open_lock);
  653. fail_put:
  654. urdev_put(urd);
  655. out:
  656. return rc;
  657. }
  658. static int ur_release(struct inode *inode, struct file *file)
  659. {
  660. struct urfile *urf = file->private_data;
  661. TRACE("ur_release\n");
  662. spin_lock(&urf->urd->open_lock);
  663. urf->urd->open_flag--;
  664. spin_unlock(&urf->urd->open_lock);
  665. wake_up_interruptible(&urf->urd->wait);
  666. urdev_put(urf->urd);
  667. urfile_free(urf);
  668. return 0;
  669. }
  670. static loff_t ur_llseek(struct file *file, loff_t offset, int whence)
  671. {
  672. loff_t newpos;
  673. if ((file->f_flags & O_ACCMODE) != O_RDONLY)
  674. return -ESPIPE; /* seek allowed only for reader */
  675. if (offset % PAGE_SIZE)
  676. return -ESPIPE; /* only multiples of 4K allowed */
  677. switch (whence) {
  678. case 0: /* SEEK_SET */
  679. newpos = offset;
  680. break;
  681. case 1: /* SEEK_CUR */
  682. newpos = file->f_pos + offset;
  683. break;
  684. default:
  685. return -EINVAL;
  686. }
  687. file->f_pos = newpos;
  688. return newpos;
  689. }
  690. static const struct file_operations ur_fops = {
  691. .owner = THIS_MODULE,
  692. .open = ur_open,
  693. .release = ur_release,
  694. .read = ur_read,
  695. .write = ur_write,
  696. .llseek = ur_llseek,
  697. };
  698. /*
  699. * ccw_device infrastructure:
  700. * ur_probe creates the struct urdev (with refcount = 1), the device
  701. * attributes, sets up the interrupt handler and validates the virtual
  702. * unit record device.
  703. * ur_remove removes the device attributes and drops the reference to
  704. * struct urdev.
  705. *
  706. * ur_probe, ur_remove, ur_set_online and ur_set_offline are serialized
  707. * by the vmur_mutex lock.
  708. *
  709. * urd->char_device is used as indication that the online function has
  710. * been completed successfully.
  711. */
  712. static int ur_probe(struct ccw_device *cdev)
  713. {
  714. struct urdev *urd;
  715. int rc;
  716. TRACE("ur_probe: cdev=%p\n", cdev);
  717. mutex_lock(&vmur_mutex);
  718. urd = urdev_alloc(cdev);
  719. if (!urd) {
  720. rc = -ENOMEM;
  721. goto fail_unlock;
  722. }
  723. rc = ur_create_attributes(&cdev->dev);
  724. if (rc) {
  725. rc = -ENOMEM;
  726. goto fail_urdev_put;
  727. }
  728. cdev->handler = ur_int_handler;
  729. /* validate virtual unit record device */
  730. urd->class = get_urd_class(urd);
  731. if (urd->class < 0) {
  732. rc = urd->class;
  733. goto fail_remove_attr;
  734. }
  735. if ((urd->class != DEV_CLASS_UR_I) && (urd->class != DEV_CLASS_UR_O)) {
  736. rc = -EOPNOTSUPP;
  737. goto fail_remove_attr;
  738. }
  739. spin_lock_irq(get_ccwdev_lock(cdev));
  740. dev_set_drvdata(&cdev->dev, urd);
  741. spin_unlock_irq(get_ccwdev_lock(cdev));
  742. mutex_unlock(&vmur_mutex);
  743. return 0;
  744. fail_remove_attr:
  745. ur_remove_attributes(&cdev->dev);
  746. fail_urdev_put:
  747. urdev_put(urd);
  748. fail_unlock:
  749. mutex_unlock(&vmur_mutex);
  750. return rc;
  751. }
  752. static int ur_set_online(struct ccw_device *cdev)
  753. {
  754. struct urdev *urd;
  755. int minor, major, rc;
  756. char node_id[16];
  757. TRACE("ur_set_online: cdev=%p\n", cdev);
  758. mutex_lock(&vmur_mutex);
  759. urd = urdev_get_from_cdev(cdev);
  760. if (!urd) {
  761. /* ur_remove already deleted our urd */
  762. rc = -ENODEV;
  763. goto fail_unlock;
  764. }
  765. if (urd->char_device) {
  766. /* Another ur_set_online was faster */
  767. rc = -EBUSY;
  768. goto fail_urdev_put;
  769. }
  770. minor = urd->dev_id.devno;
  771. major = MAJOR(ur_first_dev_maj_min);
  772. urd->char_device = cdev_alloc();
  773. if (!urd->char_device) {
  774. rc = -ENOMEM;
  775. goto fail_urdev_put;
  776. }
  777. cdev_init(urd->char_device, &ur_fops);
  778. urd->char_device->dev = MKDEV(major, minor);
  779. urd->char_device->owner = ur_fops.owner;
  780. rc = cdev_add(urd->char_device, urd->char_device->dev, 1);
  781. if (rc)
  782. goto fail_free_cdev;
  783. if (urd->cdev->id.cu_type == READER_PUNCH_DEVTYPE) {
  784. if (urd->class == DEV_CLASS_UR_I)
  785. sprintf(node_id, "vmrdr-%s", dev_name(&cdev->dev));
  786. if (urd->class == DEV_CLASS_UR_O)
  787. sprintf(node_id, "vmpun-%s", dev_name(&cdev->dev));
  788. } else if (urd->cdev->id.cu_type == PRINTER_DEVTYPE) {
  789. sprintf(node_id, "vmprt-%s", dev_name(&cdev->dev));
  790. } else {
  791. rc = -EOPNOTSUPP;
  792. goto fail_free_cdev;
  793. }
  794. urd->device = device_create(vmur_class, NULL, urd->char_device->dev,
  795. NULL, "%s", node_id);
  796. if (IS_ERR(urd->device)) {
  797. rc = PTR_ERR(urd->device);
  798. TRACE("ur_set_online: device_create rc=%d\n", rc);
  799. goto fail_free_cdev;
  800. }
  801. urdev_put(urd);
  802. mutex_unlock(&vmur_mutex);
  803. return 0;
  804. fail_free_cdev:
  805. cdev_del(urd->char_device);
  806. urd->char_device = NULL;
  807. fail_urdev_put:
  808. urdev_put(urd);
  809. fail_unlock:
  810. mutex_unlock(&vmur_mutex);
  811. return rc;
  812. }
  813. static int ur_set_offline_force(struct ccw_device *cdev, int force)
  814. {
  815. struct urdev *urd;
  816. int rc;
  817. TRACE("ur_set_offline: cdev=%p\n", cdev);
  818. urd = urdev_get_from_cdev(cdev);
  819. if (!urd)
  820. /* ur_remove already deleted our urd */
  821. return -ENODEV;
  822. if (!urd->char_device) {
  823. /* Another ur_set_offline was faster */
  824. rc = -EBUSY;
  825. goto fail_urdev_put;
  826. }
  827. if (!force && (atomic_read(&urd->ref_count) > 2)) {
  828. /* There is still a user of urd (e.g. ur_open) */
  829. TRACE("ur_set_offline: BUSY\n");
  830. rc = -EBUSY;
  831. goto fail_urdev_put;
  832. }
  833. device_destroy(vmur_class, urd->char_device->dev);
  834. cdev_del(urd->char_device);
  835. urd->char_device = NULL;
  836. rc = 0;
  837. fail_urdev_put:
  838. urdev_put(urd);
  839. return rc;
  840. }
  841. static int ur_set_offline(struct ccw_device *cdev)
  842. {
  843. int rc;
  844. mutex_lock(&vmur_mutex);
  845. rc = ur_set_offline_force(cdev, 0);
  846. mutex_unlock(&vmur_mutex);
  847. return rc;
  848. }
  849. static void ur_remove(struct ccw_device *cdev)
  850. {
  851. unsigned long flags;
  852. TRACE("ur_remove\n");
  853. mutex_lock(&vmur_mutex);
  854. if (cdev->online)
  855. ur_set_offline_force(cdev, 1);
  856. ur_remove_attributes(&cdev->dev);
  857. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  858. urdev_put(dev_get_drvdata(&cdev->dev));
  859. dev_set_drvdata(&cdev->dev, NULL);
  860. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  861. mutex_unlock(&vmur_mutex);
  862. }
  863. /*
  864. * Module initialisation and cleanup
  865. */
  866. static int __init ur_init(void)
  867. {
  868. int rc;
  869. dev_t dev;
  870. if (!MACHINE_IS_VM) {
  871. pr_err("The %s cannot be loaded without z/VM\n",
  872. ur_banner);
  873. return -ENODEV;
  874. }
  875. vmur_dbf = debug_register("vmur", 4, 1, 4 * sizeof(long));
  876. if (!vmur_dbf)
  877. return -ENOMEM;
  878. rc = debug_register_view(vmur_dbf, &debug_sprintf_view);
  879. if (rc)
  880. goto fail_free_dbf;
  881. debug_set_level(vmur_dbf, 6);
  882. vmur_class = class_create(THIS_MODULE, "vmur");
  883. if (IS_ERR(vmur_class)) {
  884. rc = PTR_ERR(vmur_class);
  885. goto fail_free_dbf;
  886. }
  887. rc = ccw_driver_register(&ur_driver);
  888. if (rc)
  889. goto fail_class_destroy;
  890. rc = alloc_chrdev_region(&dev, 0, NUM_MINORS, "vmur");
  891. if (rc) {
  892. pr_err("Kernel function alloc_chrdev_region failed with "
  893. "error code %d\n", rc);
  894. goto fail_unregister_driver;
  895. }
  896. ur_first_dev_maj_min = MKDEV(MAJOR(dev), 0);
  897. pr_info("%s loaded.\n", ur_banner);
  898. return 0;
  899. fail_unregister_driver:
  900. ccw_driver_unregister(&ur_driver);
  901. fail_class_destroy:
  902. class_destroy(vmur_class);
  903. fail_free_dbf:
  904. debug_unregister(vmur_dbf);
  905. return rc;
  906. }
  907. static void __exit ur_exit(void)
  908. {
  909. unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS);
  910. ccw_driver_unregister(&ur_driver);
  911. class_destroy(vmur_class);
  912. debug_unregister(vmur_dbf);
  913. pr_info("%s unloaded.\n", ur_banner);
  914. }
  915. module_init(ur_init);
  916. module_exit(ur_exit);