viodasd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /* -*- linux-c -*-
  2. * viodasd.c
  3. * Authors: Dave Boutcher <boutcher@us.ibm.com>
  4. * Ryan Arnold <ryanarn@us.ibm.com>
  5. * Colin Devilbiss <devilbis@us.ibm.com>
  6. * Stephen Rothwell <sfr@au1.ibm.com>
  7. *
  8. * (C) Copyright 2000-2004 IBM Corporation
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * This routine provides access to disk space (termed "DASD" in historical
  25. * IBM terms) owned and managed by an OS/400 partition running on the
  26. * same box as this Linux partition.
  27. *
  28. * All disk operations are performed by sending messages back and forth to
  29. * the OS/400 partition.
  30. */
  31. #include <linux/major.h>
  32. #include <linux/fs.h>
  33. #include <linux/module.h>
  34. #include <linux/kernel.h>
  35. #include <linux/blkdev.h>
  36. #include <linux/genhd.h>
  37. #include <linux/hdreg.h>
  38. #include <linux/errno.h>
  39. #include <linux/init.h>
  40. #include <linux/string.h>
  41. #include <linux/dma-mapping.h>
  42. #include <linux/completion.h>
  43. #include <linux/device.h>
  44. #include <linux/scatterlist.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/vio.h>
  47. #include <asm/iseries/hv_types.h>
  48. #include <asm/iseries/hv_lp_event.h>
  49. #include <asm/iseries/hv_lp_config.h>
  50. #include <asm/iseries/vio.h>
  51. #include <asm/firmware.h>
  52. MODULE_DESCRIPTION("iSeries Virtual DASD");
  53. MODULE_AUTHOR("Dave Boutcher");
  54. MODULE_LICENSE("GPL");
  55. /*
  56. * We only support 7 partitions per physical disk....so with minor
  57. * numbers 0-255 we get a maximum of 32 disks.
  58. */
  59. #define VIOD_GENHD_NAME "iseries/vd"
  60. #define VIOD_VERS "1.64"
  61. #define VIOD_KERN_WARNING KERN_WARNING "viod: "
  62. #define VIOD_KERN_INFO KERN_INFO "viod: "
  63. enum {
  64. PARTITION_SHIFT = 3,
  65. MAX_DISKNO = HVMAXARCHITECTEDVIRTUALDISKS,
  66. MAX_DISK_NAME = sizeof(((struct gendisk *)0)->disk_name)
  67. };
  68. static DEFINE_SPINLOCK(viodasd_spinlock);
  69. #define VIOMAXREQ 16
  70. #define DEVICE_NO(cell) ((struct viodasd_device *)(cell) - &viodasd_devices[0])
  71. struct viodasd_waitevent {
  72. struct completion com;
  73. int rc;
  74. u16 sub_result;
  75. int max_disk; /* open */
  76. };
  77. static const struct vio_error_entry viodasd_err_table[] = {
  78. { 0x0201, EINVAL, "Invalid Range" },
  79. { 0x0202, EINVAL, "Invalid Token" },
  80. { 0x0203, EIO, "DMA Error" },
  81. { 0x0204, EIO, "Use Error" },
  82. { 0x0205, EIO, "Release Error" },
  83. { 0x0206, EINVAL, "Invalid Disk" },
  84. { 0x0207, EBUSY, "Cant Lock" },
  85. { 0x0208, EIO, "Already Locked" },
  86. { 0x0209, EIO, "Already Unlocked" },
  87. { 0x020A, EIO, "Invalid Arg" },
  88. { 0x020B, EIO, "Bad IFS File" },
  89. { 0x020C, EROFS, "Read Only Device" },
  90. { 0x02FF, EIO, "Internal Error" },
  91. { 0x0000, 0, NULL },
  92. };
  93. /*
  94. * Figure out the biggest I/O request (in sectors) we can accept
  95. */
  96. #define VIODASD_MAXSECTORS (4096 / 512 * VIOMAXBLOCKDMA)
  97. /*
  98. * Number of disk I/O requests we've sent to OS/400
  99. */
  100. static int num_req_outstanding;
  101. /*
  102. * This is our internal structure for keeping track of disk devices
  103. */
  104. struct viodasd_device {
  105. u16 cylinders;
  106. u16 tracks;
  107. u16 sectors;
  108. u16 bytes_per_sector;
  109. u64 size;
  110. int read_only;
  111. spinlock_t q_lock;
  112. struct gendisk *disk;
  113. struct device *dev;
  114. } viodasd_devices[MAX_DISKNO];
  115. /*
  116. * External open entry point.
  117. */
  118. static int viodasd_open(struct inode *ino, struct file *fil)
  119. {
  120. struct viodasd_device *d = ino->i_bdev->bd_disk->private_data;
  121. HvLpEvent_Rc hvrc;
  122. struct viodasd_waitevent we;
  123. u16 flags = 0;
  124. if (d->read_only) {
  125. if ((fil != NULL) && (fil->f_mode & FMODE_WRITE))
  126. return -EROFS;
  127. flags = vioblockflags_ro;
  128. }
  129. init_completion(&we.com);
  130. /* Send the open event to OS/400 */
  131. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  132. HvLpEvent_Type_VirtualIo,
  133. viomajorsubtype_blockio | vioblockopen,
  134. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  135. viopath_sourceinst(viopath_hostLp),
  136. viopath_targetinst(viopath_hostLp),
  137. (u64)(unsigned long)&we, VIOVERSION << 16,
  138. ((u64)DEVICE_NO(d) << 48) | ((u64)flags << 32),
  139. 0, 0, 0);
  140. if (hvrc != 0) {
  141. printk(VIOD_KERN_WARNING "HV open failed %d\n", (int)hvrc);
  142. return -EIO;
  143. }
  144. wait_for_completion(&we.com);
  145. /* Check the return code */
  146. if (we.rc != 0) {
  147. const struct vio_error_entry *err =
  148. vio_lookup_rc(viodasd_err_table, we.sub_result);
  149. printk(VIOD_KERN_WARNING
  150. "bad rc opening disk: %d:0x%04x (%s)\n",
  151. (int)we.rc, we.sub_result, err->msg);
  152. return -EIO;
  153. }
  154. return 0;
  155. }
  156. /*
  157. * External release entry point.
  158. */
  159. static int viodasd_release(struct inode *ino, struct file *fil)
  160. {
  161. struct viodasd_device *d = ino->i_bdev->bd_disk->private_data;
  162. HvLpEvent_Rc hvrc;
  163. /* Send the event to OS/400. We DON'T expect a response */
  164. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  165. HvLpEvent_Type_VirtualIo,
  166. viomajorsubtype_blockio | vioblockclose,
  167. HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
  168. viopath_sourceinst(viopath_hostLp),
  169. viopath_targetinst(viopath_hostLp),
  170. 0, VIOVERSION << 16,
  171. ((u64)DEVICE_NO(d) << 48) /* | ((u64)flags << 32) */,
  172. 0, 0, 0);
  173. if (hvrc != 0)
  174. printk(VIOD_KERN_WARNING "HV close call failed %d\n",
  175. (int)hvrc);
  176. return 0;
  177. }
  178. /* External ioctl entry point.
  179. */
  180. static int viodasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  181. {
  182. struct gendisk *disk = bdev->bd_disk;
  183. struct viodasd_device *d = disk->private_data;
  184. geo->sectors = d->sectors ? d->sectors : 32;
  185. geo->heads = d->tracks ? d->tracks : 64;
  186. geo->cylinders = d->cylinders ? d->cylinders :
  187. get_capacity(disk) / (geo->sectors * geo->heads);
  188. return 0;
  189. }
  190. /*
  191. * Our file operations table
  192. */
  193. static struct block_device_operations viodasd_fops = {
  194. .owner = THIS_MODULE,
  195. .open = viodasd_open,
  196. .release = viodasd_release,
  197. .getgeo = viodasd_getgeo,
  198. };
  199. /*
  200. * End a request
  201. */
  202. static void viodasd_end_request(struct request *req, int uptodate,
  203. int num_sectors)
  204. {
  205. if (end_that_request_first(req, uptodate, num_sectors))
  206. return;
  207. add_disk_randomness(req->rq_disk);
  208. end_that_request_last(req, uptodate);
  209. }
  210. /*
  211. * Send an actual I/O request to OS/400
  212. */
  213. static int send_request(struct request *req)
  214. {
  215. u64 start;
  216. int direction;
  217. int nsg;
  218. u16 viocmd;
  219. HvLpEvent_Rc hvrc;
  220. struct vioblocklpevent *bevent;
  221. struct HvLpEvent *hev;
  222. struct scatterlist sg[VIOMAXBLOCKDMA];
  223. int sgindex;
  224. int statindex;
  225. struct viodasd_device *d;
  226. unsigned long flags;
  227. start = (u64)req->sector << 9;
  228. if (rq_data_dir(req) == READ) {
  229. direction = DMA_FROM_DEVICE;
  230. viocmd = viomajorsubtype_blockio | vioblockread;
  231. statindex = 0;
  232. } else {
  233. direction = DMA_TO_DEVICE;
  234. viocmd = viomajorsubtype_blockio | vioblockwrite;
  235. statindex = 1;
  236. }
  237. d = req->rq_disk->private_data;
  238. /* Now build the scatter-gather list */
  239. sg_init_table(sg, VIOMAXBLOCKDMA);
  240. nsg = blk_rq_map_sg(req->q, req, sg);
  241. nsg = dma_map_sg(d->dev, sg, nsg, direction);
  242. spin_lock_irqsave(&viodasd_spinlock, flags);
  243. num_req_outstanding++;
  244. /* This optimization handles a single DMA block */
  245. if (nsg == 1)
  246. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  247. HvLpEvent_Type_VirtualIo, viocmd,
  248. HvLpEvent_AckInd_DoAck,
  249. HvLpEvent_AckType_ImmediateAck,
  250. viopath_sourceinst(viopath_hostLp),
  251. viopath_targetinst(viopath_hostLp),
  252. (u64)(unsigned long)req, VIOVERSION << 16,
  253. ((u64)DEVICE_NO(d) << 48), start,
  254. ((u64)sg_dma_address(&sg[0])) << 32,
  255. sg_dma_len(&sg[0]));
  256. else {
  257. bevent = (struct vioblocklpevent *)
  258. vio_get_event_buffer(viomajorsubtype_blockio);
  259. if (bevent == NULL) {
  260. printk(VIOD_KERN_WARNING
  261. "error allocating disk event buffer\n");
  262. goto error_ret;
  263. }
  264. /*
  265. * Now build up the actual request. Note that we store
  266. * the pointer to the request in the correlation
  267. * token so we can match the response up later
  268. */
  269. memset(bevent, 0, sizeof(struct vioblocklpevent));
  270. hev = &bevent->event;
  271. hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DO_ACK |
  272. HV_LP_EVENT_INT;
  273. hev->xType = HvLpEvent_Type_VirtualIo;
  274. hev->xSubtype = viocmd;
  275. hev->xSourceLp = HvLpConfig_getLpIndex();
  276. hev->xTargetLp = viopath_hostLp;
  277. hev->xSizeMinus1 =
  278. offsetof(struct vioblocklpevent, u.rw_data.dma_info) +
  279. (sizeof(bevent->u.rw_data.dma_info[0]) * nsg) - 1;
  280. hev->xSourceInstanceId = viopath_sourceinst(viopath_hostLp);
  281. hev->xTargetInstanceId = viopath_targetinst(viopath_hostLp);
  282. hev->xCorrelationToken = (u64)req;
  283. bevent->version = VIOVERSION;
  284. bevent->disk = DEVICE_NO(d);
  285. bevent->u.rw_data.offset = start;
  286. /*
  287. * Copy just the dma information from the sg list
  288. * into the request
  289. */
  290. for (sgindex = 0; sgindex < nsg; sgindex++) {
  291. bevent->u.rw_data.dma_info[sgindex].token =
  292. sg_dma_address(&sg[sgindex]);
  293. bevent->u.rw_data.dma_info[sgindex].len =
  294. sg_dma_len(&sg[sgindex]);
  295. }
  296. /* Send the request */
  297. hvrc = HvCallEvent_signalLpEvent(&bevent->event);
  298. vio_free_event_buffer(viomajorsubtype_blockio, bevent);
  299. }
  300. if (hvrc != HvLpEvent_Rc_Good) {
  301. printk(VIOD_KERN_WARNING
  302. "error sending disk event to OS/400 (rc %d)\n",
  303. (int)hvrc);
  304. goto error_ret;
  305. }
  306. spin_unlock_irqrestore(&viodasd_spinlock, flags);
  307. return 0;
  308. error_ret:
  309. num_req_outstanding--;
  310. spin_unlock_irqrestore(&viodasd_spinlock, flags);
  311. dma_unmap_sg(d->dev, sg, nsg, direction);
  312. return -1;
  313. }
  314. /*
  315. * This is the external request processing routine
  316. */
  317. static void do_viodasd_request(struct request_queue *q)
  318. {
  319. struct request *req;
  320. /*
  321. * If we already have the maximum number of requests
  322. * outstanding to OS/400 just bail out. We'll come
  323. * back later.
  324. */
  325. while (num_req_outstanding < VIOMAXREQ) {
  326. req = elv_next_request(q);
  327. if (req == NULL)
  328. return;
  329. /* dequeue the current request from the queue */
  330. blkdev_dequeue_request(req);
  331. /* check that request contains a valid command */
  332. if (!blk_fs_request(req)) {
  333. viodasd_end_request(req, 0, req->hard_nr_sectors);
  334. continue;
  335. }
  336. /* Try sending the request */
  337. if (send_request(req) != 0)
  338. viodasd_end_request(req, 0, req->hard_nr_sectors);
  339. }
  340. }
  341. /*
  342. * Probe a single disk and fill in the viodasd_device structure
  343. * for it.
  344. */
  345. static int probe_disk(struct viodasd_device *d)
  346. {
  347. HvLpEvent_Rc hvrc;
  348. struct viodasd_waitevent we;
  349. int dev_no = DEVICE_NO(d);
  350. struct gendisk *g;
  351. struct request_queue *q;
  352. u16 flags = 0;
  353. retry:
  354. init_completion(&we.com);
  355. /* Send the open event to OS/400 */
  356. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  357. HvLpEvent_Type_VirtualIo,
  358. viomajorsubtype_blockio | vioblockopen,
  359. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  360. viopath_sourceinst(viopath_hostLp),
  361. viopath_targetinst(viopath_hostLp),
  362. (u64)(unsigned long)&we, VIOVERSION << 16,
  363. ((u64)dev_no << 48) | ((u64)flags<< 32),
  364. 0, 0, 0);
  365. if (hvrc != 0) {
  366. printk(VIOD_KERN_WARNING "bad rc on HV open %d\n", (int)hvrc);
  367. return 0;
  368. }
  369. wait_for_completion(&we.com);
  370. if (we.rc != 0) {
  371. if (flags != 0)
  372. return 0;
  373. /* try again with read only flag set */
  374. flags = vioblockflags_ro;
  375. goto retry;
  376. }
  377. if (we.max_disk > (MAX_DISKNO - 1)) {
  378. static int warned;
  379. if (warned == 0) {
  380. warned++;
  381. printk(VIOD_KERN_INFO
  382. "Only examining the first %d "
  383. "of %d disks connected\n",
  384. MAX_DISKNO, we.max_disk + 1);
  385. }
  386. }
  387. /* Send the close event to OS/400. We DON'T expect a response */
  388. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  389. HvLpEvent_Type_VirtualIo,
  390. viomajorsubtype_blockio | vioblockclose,
  391. HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
  392. viopath_sourceinst(viopath_hostLp),
  393. viopath_targetinst(viopath_hostLp),
  394. 0, VIOVERSION << 16,
  395. ((u64)dev_no << 48) | ((u64)flags << 32),
  396. 0, 0, 0);
  397. if (hvrc != 0) {
  398. printk(VIOD_KERN_WARNING
  399. "bad rc sending event to OS/400 %d\n", (int)hvrc);
  400. return 0;
  401. }
  402. if (d->dev == NULL) {
  403. /* this is when we reprobe for new disks */
  404. if (vio_create_viodasd(dev_no) == NULL) {
  405. printk(VIOD_KERN_WARNING
  406. "cannot allocate virtual device for disk %d\n",
  407. dev_no);
  408. return 0;
  409. }
  410. /*
  411. * The vio_create_viodasd will have recursed into this
  412. * routine with d->dev set to the new vio device and
  413. * will finish the setup of the disk below.
  414. */
  415. return 1;
  416. }
  417. /* create the request queue for the disk */
  418. spin_lock_init(&d->q_lock);
  419. q = blk_init_queue(do_viodasd_request, &d->q_lock);
  420. if (q == NULL) {
  421. printk(VIOD_KERN_WARNING "cannot allocate queue for disk %d\n",
  422. dev_no);
  423. return 0;
  424. }
  425. g = alloc_disk(1 << PARTITION_SHIFT);
  426. if (g == NULL) {
  427. printk(VIOD_KERN_WARNING
  428. "cannot allocate disk structure for disk %d\n",
  429. dev_no);
  430. blk_cleanup_queue(q);
  431. return 0;
  432. }
  433. d->disk = g;
  434. blk_queue_max_hw_segments(q, VIOMAXBLOCKDMA);
  435. blk_queue_max_phys_segments(q, VIOMAXBLOCKDMA);
  436. blk_queue_max_sectors(q, VIODASD_MAXSECTORS);
  437. g->major = VIODASD_MAJOR;
  438. g->first_minor = dev_no << PARTITION_SHIFT;
  439. if (dev_no >= 26)
  440. snprintf(g->disk_name, sizeof(g->disk_name),
  441. VIOD_GENHD_NAME "%c%c",
  442. 'a' + (dev_no / 26) - 1, 'a' + (dev_no % 26));
  443. else
  444. snprintf(g->disk_name, sizeof(g->disk_name),
  445. VIOD_GENHD_NAME "%c", 'a' + (dev_no % 26));
  446. g->fops = &viodasd_fops;
  447. g->queue = q;
  448. g->private_data = d;
  449. g->driverfs_dev = d->dev;
  450. set_capacity(g, d->size >> 9);
  451. printk(VIOD_KERN_INFO "disk %d: %lu sectors (%lu MB) "
  452. "CHS=%d/%d/%d sector size %d%s\n",
  453. dev_no, (unsigned long)(d->size >> 9),
  454. (unsigned long)(d->size >> 20),
  455. (int)d->cylinders, (int)d->tracks,
  456. (int)d->sectors, (int)d->bytes_per_sector,
  457. d->read_only ? " (RO)" : "");
  458. /* register us in the global list */
  459. add_disk(g);
  460. return 1;
  461. }
  462. /* returns the total number of scatterlist elements converted */
  463. static int block_event_to_scatterlist(const struct vioblocklpevent *bevent,
  464. struct scatterlist *sg, int *total_len)
  465. {
  466. int i, numsg;
  467. const struct rw_data *rw_data = &bevent->u.rw_data;
  468. static const int offset =
  469. offsetof(struct vioblocklpevent, u.rw_data.dma_info);
  470. static const int element_size = sizeof(rw_data->dma_info[0]);
  471. numsg = ((bevent->event.xSizeMinus1 + 1) - offset) / element_size;
  472. if (numsg > VIOMAXBLOCKDMA)
  473. numsg = VIOMAXBLOCKDMA;
  474. *total_len = 0;
  475. memset(sg, 0, sizeof(sg[0]) * VIOMAXBLOCKDMA);
  476. for (i = 0; (i < numsg) && (rw_data->dma_info[i].len > 0); ++i) {
  477. sg_dma_address(&sg[i]) = rw_data->dma_info[i].token;
  478. sg_dma_len(&sg[i]) = rw_data->dma_info[i].len;
  479. *total_len += rw_data->dma_info[i].len;
  480. }
  481. return i;
  482. }
  483. /*
  484. * Restart all queues, starting with the one _after_ the disk given,
  485. * thus reducing the chance of starvation of higher numbered disks.
  486. */
  487. static void viodasd_restart_all_queues_starting_from(int first_index)
  488. {
  489. int i;
  490. for (i = first_index + 1; i < MAX_DISKNO; ++i)
  491. if (viodasd_devices[i].disk)
  492. blk_run_queue(viodasd_devices[i].disk->queue);
  493. for (i = 0; i <= first_index; ++i)
  494. if (viodasd_devices[i].disk)
  495. blk_run_queue(viodasd_devices[i].disk->queue);
  496. }
  497. /*
  498. * For read and write requests, decrement the number of outstanding requests,
  499. * Free the DMA buffers we allocated.
  500. */
  501. static int viodasd_handle_read_write(struct vioblocklpevent *bevent)
  502. {
  503. int num_sg, num_sect, pci_direction, total_len;
  504. struct request *req;
  505. struct scatterlist sg[VIOMAXBLOCKDMA];
  506. struct HvLpEvent *event = &bevent->event;
  507. unsigned long irq_flags;
  508. struct viodasd_device *d;
  509. int error;
  510. spinlock_t *qlock;
  511. num_sg = block_event_to_scatterlist(bevent, sg, &total_len);
  512. num_sect = total_len >> 9;
  513. if (event->xSubtype == (viomajorsubtype_blockio | vioblockread))
  514. pci_direction = DMA_FROM_DEVICE;
  515. else
  516. pci_direction = DMA_TO_DEVICE;
  517. req = (struct request *)bevent->event.xCorrelationToken;
  518. d = req->rq_disk->private_data;
  519. dma_unmap_sg(d->dev, sg, num_sg, pci_direction);
  520. /*
  521. * Since this is running in interrupt mode, we need to make sure
  522. * we're not stepping on any global I/O operations
  523. */
  524. spin_lock_irqsave(&viodasd_spinlock, irq_flags);
  525. num_req_outstanding--;
  526. spin_unlock_irqrestore(&viodasd_spinlock, irq_flags);
  527. error = event->xRc != HvLpEvent_Rc_Good;
  528. if (error) {
  529. const struct vio_error_entry *err;
  530. err = vio_lookup_rc(viodasd_err_table, bevent->sub_result);
  531. printk(VIOD_KERN_WARNING "read/write error %d:0x%04x (%s)\n",
  532. event->xRc, bevent->sub_result, err->msg);
  533. num_sect = req->hard_nr_sectors;
  534. }
  535. qlock = req->q->queue_lock;
  536. spin_lock_irqsave(qlock, irq_flags);
  537. viodasd_end_request(req, !error, num_sect);
  538. spin_unlock_irqrestore(qlock, irq_flags);
  539. /* Finally, try to get more requests off of this device's queue */
  540. viodasd_restart_all_queues_starting_from(DEVICE_NO(d));
  541. return 0;
  542. }
  543. /* This routine handles incoming block LP events */
  544. static void handle_block_event(struct HvLpEvent *event)
  545. {
  546. struct vioblocklpevent *bevent = (struct vioblocklpevent *)event;
  547. struct viodasd_waitevent *pwe;
  548. if (event == NULL)
  549. /* Notification that a partition went away! */
  550. return;
  551. /* First, we should NEVER get an int here...only acks */
  552. if (hvlpevent_is_int(event)) {
  553. printk(VIOD_KERN_WARNING
  554. "Yikes! got an int in viodasd event handler!\n");
  555. if (hvlpevent_need_ack(event)) {
  556. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  557. HvCallEvent_ackLpEvent(event);
  558. }
  559. }
  560. switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
  561. case vioblockopen:
  562. /*
  563. * Handle a response to an open request. We get all the
  564. * disk information in the response, so update it. The
  565. * correlation token contains a pointer to a waitevent
  566. * structure that has a completion in it. update the
  567. * return code in the waitevent structure and post the
  568. * completion to wake up the guy who sent the request
  569. */
  570. pwe = (struct viodasd_waitevent *)event->xCorrelationToken;
  571. pwe->rc = event->xRc;
  572. pwe->sub_result = bevent->sub_result;
  573. if (event->xRc == HvLpEvent_Rc_Good) {
  574. const struct open_data *data = &bevent->u.open_data;
  575. struct viodasd_device *device =
  576. &viodasd_devices[bevent->disk];
  577. device->read_only =
  578. bevent->flags & vioblockflags_ro;
  579. device->size = data->disk_size;
  580. device->cylinders = data->cylinders;
  581. device->tracks = data->tracks;
  582. device->sectors = data->sectors;
  583. device->bytes_per_sector = data->bytes_per_sector;
  584. pwe->max_disk = data->max_disk;
  585. }
  586. complete(&pwe->com);
  587. break;
  588. case vioblockclose:
  589. break;
  590. case vioblockread:
  591. case vioblockwrite:
  592. viodasd_handle_read_write(bevent);
  593. break;
  594. default:
  595. printk(VIOD_KERN_WARNING "invalid subtype!");
  596. if (hvlpevent_need_ack(event)) {
  597. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  598. HvCallEvent_ackLpEvent(event);
  599. }
  600. }
  601. }
  602. /*
  603. * Get the driver to reprobe for more disks.
  604. */
  605. static ssize_t probe_disks(struct device_driver *drv, const char *buf,
  606. size_t count)
  607. {
  608. struct viodasd_device *d;
  609. for (d = viodasd_devices; d < &viodasd_devices[MAX_DISKNO]; d++) {
  610. if (d->disk == NULL)
  611. probe_disk(d);
  612. }
  613. return count;
  614. }
  615. static DRIVER_ATTR(probe, S_IWUSR, NULL, probe_disks);
  616. static int viodasd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  617. {
  618. struct viodasd_device *d = &viodasd_devices[vdev->unit_address];
  619. d->dev = &vdev->dev;
  620. if (!probe_disk(d))
  621. return -ENODEV;
  622. return 0;
  623. }
  624. static int viodasd_remove(struct vio_dev *vdev)
  625. {
  626. struct viodasd_device *d;
  627. d = &viodasd_devices[vdev->unit_address];
  628. if (d->disk) {
  629. del_gendisk(d->disk);
  630. blk_cleanup_queue(d->disk->queue);
  631. put_disk(d->disk);
  632. d->disk = NULL;
  633. }
  634. d->dev = NULL;
  635. return 0;
  636. }
  637. /**
  638. * viodasd_device_table: Used by vio.c to match devices that we
  639. * support.
  640. */
  641. static struct vio_device_id viodasd_device_table[] __devinitdata = {
  642. { "block", "IBM,iSeries-viodasd" },
  643. { "", "" }
  644. };
  645. MODULE_DEVICE_TABLE(vio, viodasd_device_table);
  646. static struct vio_driver viodasd_driver = {
  647. .id_table = viodasd_device_table,
  648. .probe = viodasd_probe,
  649. .remove = viodasd_remove,
  650. .driver = {
  651. .name = "viodasd",
  652. .owner = THIS_MODULE,
  653. }
  654. };
  655. static int need_delete_probe;
  656. /*
  657. * Initialize the whole device driver. Handle module and non-module
  658. * versions
  659. */
  660. static int __init viodasd_init(void)
  661. {
  662. int rc;
  663. if (!firmware_has_feature(FW_FEATURE_ISERIES)) {
  664. rc = -ENODEV;
  665. goto early_fail;
  666. }
  667. /* Try to open to our host lp */
  668. if (viopath_hostLp == HvLpIndexInvalid)
  669. vio_set_hostlp();
  670. if (viopath_hostLp == HvLpIndexInvalid) {
  671. printk(VIOD_KERN_WARNING "invalid hosting partition\n");
  672. rc = -EIO;
  673. goto early_fail;
  674. }
  675. printk(VIOD_KERN_INFO "vers " VIOD_VERS ", hosting partition %d\n",
  676. viopath_hostLp);
  677. /* register the block device */
  678. rc = register_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
  679. if (rc) {
  680. printk(VIOD_KERN_WARNING
  681. "Unable to get major number %d for %s\n",
  682. VIODASD_MAJOR, VIOD_GENHD_NAME);
  683. goto early_fail;
  684. }
  685. /* Actually open the path to the hosting partition */
  686. rc = viopath_open(viopath_hostLp, viomajorsubtype_blockio,
  687. VIOMAXREQ + 2);
  688. if (rc) {
  689. printk(VIOD_KERN_WARNING
  690. "error opening path to host partition %d\n",
  691. viopath_hostLp);
  692. goto unregister_blk;
  693. }
  694. /* Initialize our request handler */
  695. vio_setHandler(viomajorsubtype_blockio, handle_block_event);
  696. rc = vio_register_driver(&viodasd_driver);
  697. if (rc) {
  698. printk(VIOD_KERN_WARNING "vio_register_driver failed\n");
  699. goto unset_handler;
  700. }
  701. /*
  702. * If this call fails, it just means that we cannot dynamically
  703. * add virtual disks, but the driver will still work fine for
  704. * all existing disk, so ignore the failure.
  705. */
  706. if (!driver_create_file(&viodasd_driver.driver, &driver_attr_probe))
  707. need_delete_probe = 1;
  708. return 0;
  709. unset_handler:
  710. vio_clearHandler(viomajorsubtype_blockio);
  711. viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
  712. unregister_blk:
  713. unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
  714. early_fail:
  715. return rc;
  716. }
  717. module_init(viodasd_init);
  718. void __exit viodasd_exit(void)
  719. {
  720. if (need_delete_probe)
  721. driver_remove_file(&viodasd_driver.driver, &driver_attr_probe);
  722. vio_unregister_driver(&viodasd_driver);
  723. vio_clearHandler(viomajorsubtype_blockio);
  724. viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
  725. unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
  726. }
  727. module_exit(viodasd_exit);