vmur.c 23 KB

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