vmur.c 24 KB

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