vmur.c 24 KB

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