vmur.c 23 KB

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