vmur.c 23 KB

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