viodasd.c 21 KB

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