vmur.c 23 KB

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