viodasd.c 21 KB

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