viodasd.c 22 KB

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