vmur.c 23 KB

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