viodasd.c 21 KB

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