vmur.c 23 KB

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