viodasd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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_segments(q, VIOMAXBLOCKDMA);
  412. blk_queue_max_hw_sectors(q, VIODASD_MAXSECTORS);
  413. g->major = VIODASD_MAJOR;
  414. g->first_minor = dev_no << PARTITION_SHIFT;
  415. if (dev_no >= 26)
  416. snprintf(g->disk_name, sizeof(g->disk_name),
  417. VIOD_GENHD_NAME "%c%c",
  418. 'a' + (dev_no / 26) - 1, 'a' + (dev_no % 26));
  419. else
  420. snprintf(g->disk_name, sizeof(g->disk_name),
  421. VIOD_GENHD_NAME "%c", 'a' + (dev_no % 26));
  422. g->fops = &viodasd_fops;
  423. g->queue = q;
  424. g->private_data = d;
  425. g->driverfs_dev = d->dev;
  426. set_capacity(g, d->size >> 9);
  427. pr_info("disk %d: %lu sectors (%lu MB) CHS=%d/%d/%d sector size %d%s\n",
  428. dev_no, (unsigned long)(d->size >> 9),
  429. (unsigned long)(d->size >> 20),
  430. (int)d->cylinders, (int)d->tracks,
  431. (int)d->sectors, (int)d->bytes_per_sector,
  432. d->read_only ? " (RO)" : "");
  433. /* register us in the global list */
  434. add_disk(g);
  435. return 1;
  436. }
  437. /* returns the total number of scatterlist elements converted */
  438. static int block_event_to_scatterlist(const struct vioblocklpevent *bevent,
  439. struct scatterlist *sg, int *total_len)
  440. {
  441. int i, numsg;
  442. const struct rw_data *rw_data = &bevent->u.rw_data;
  443. static const int offset =
  444. offsetof(struct vioblocklpevent, u.rw_data.dma_info);
  445. static const int element_size = sizeof(rw_data->dma_info[0]);
  446. numsg = ((bevent->event.xSizeMinus1 + 1) - offset) / element_size;
  447. if (numsg > VIOMAXBLOCKDMA)
  448. numsg = VIOMAXBLOCKDMA;
  449. *total_len = 0;
  450. sg_init_table(sg, VIOMAXBLOCKDMA);
  451. for (i = 0; (i < numsg) && (rw_data->dma_info[i].len > 0); ++i) {
  452. sg_dma_address(&sg[i]) = rw_data->dma_info[i].token;
  453. sg_dma_len(&sg[i]) = rw_data->dma_info[i].len;
  454. *total_len += rw_data->dma_info[i].len;
  455. }
  456. return i;
  457. }
  458. /*
  459. * Restart all queues, starting with the one _after_ the disk given,
  460. * thus reducing the chance of starvation of higher numbered disks.
  461. */
  462. static void viodasd_restart_all_queues_starting_from(int first_index)
  463. {
  464. int i;
  465. for (i = first_index + 1; i < MAX_DISKNO; ++i)
  466. if (viodasd_devices[i].disk)
  467. blk_run_queue(viodasd_devices[i].disk->queue);
  468. for (i = 0; i <= first_index; ++i)
  469. if (viodasd_devices[i].disk)
  470. blk_run_queue(viodasd_devices[i].disk->queue);
  471. }
  472. /*
  473. * For read and write requests, decrement the number of outstanding requests,
  474. * Free the DMA buffers we allocated.
  475. */
  476. static int viodasd_handle_read_write(struct vioblocklpevent *bevent)
  477. {
  478. int num_sg, num_sect, pci_direction, total_len;
  479. struct request *req;
  480. struct scatterlist sg[VIOMAXBLOCKDMA];
  481. struct HvLpEvent *event = &bevent->event;
  482. unsigned long irq_flags;
  483. struct viodasd_device *d;
  484. int error;
  485. spinlock_t *qlock;
  486. num_sg = block_event_to_scatterlist(bevent, sg, &total_len);
  487. num_sect = total_len >> 9;
  488. if (event->xSubtype == (viomajorsubtype_blockio | vioblockread))
  489. pci_direction = DMA_FROM_DEVICE;
  490. else
  491. pci_direction = DMA_TO_DEVICE;
  492. req = (struct request *)bevent->event.xCorrelationToken;
  493. d = req->rq_disk->private_data;
  494. dma_unmap_sg(d->dev, sg, num_sg, pci_direction);
  495. /*
  496. * Since this is running in interrupt mode, we need to make sure
  497. * we're not stepping on any global I/O operations
  498. */
  499. spin_lock_irqsave(&viodasd_spinlock, irq_flags);
  500. num_req_outstanding--;
  501. spin_unlock_irqrestore(&viodasd_spinlock, irq_flags);
  502. error = (event->xRc == HvLpEvent_Rc_Good) ? 0 : -EIO;
  503. if (error) {
  504. const struct vio_error_entry *err;
  505. err = vio_lookup_rc(viodasd_err_table, bevent->sub_result);
  506. pr_warning("read/write error %d:0x%04x (%s)\n",
  507. event->xRc, bevent->sub_result, err->msg);
  508. num_sect = blk_rq_sectors(req);
  509. }
  510. qlock = req->q->queue_lock;
  511. spin_lock_irqsave(qlock, irq_flags);
  512. viodasd_end_request(req, error, num_sect);
  513. spin_unlock_irqrestore(qlock, irq_flags);
  514. /* Finally, try to get more requests off of this device's queue */
  515. viodasd_restart_all_queues_starting_from(DEVICE_NO(d));
  516. return 0;
  517. }
  518. /* This routine handles incoming block LP events */
  519. static void handle_block_event(struct HvLpEvent *event)
  520. {
  521. struct vioblocklpevent *bevent = (struct vioblocklpevent *)event;
  522. struct viodasd_waitevent *pwe;
  523. if (event == NULL)
  524. /* Notification that a partition went away! */
  525. return;
  526. /* First, we should NEVER get an int here...only acks */
  527. if (hvlpevent_is_int(event)) {
  528. pr_warning("Yikes! got an int in viodasd event handler!\n");
  529. if (hvlpevent_need_ack(event)) {
  530. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  531. HvCallEvent_ackLpEvent(event);
  532. }
  533. }
  534. switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
  535. case vioblockopen:
  536. /*
  537. * Handle a response to an open request. We get all the
  538. * disk information in the response, so update it. The
  539. * correlation token contains a pointer to a waitevent
  540. * structure that has a completion in it. update the
  541. * return code in the waitevent structure and post the
  542. * completion to wake up the guy who sent the request
  543. */
  544. pwe = (struct viodasd_waitevent *)event->xCorrelationToken;
  545. pwe->rc = event->xRc;
  546. pwe->sub_result = bevent->sub_result;
  547. if (event->xRc == HvLpEvent_Rc_Good) {
  548. const struct open_data *data = &bevent->u.open_data;
  549. struct viodasd_device *device =
  550. &viodasd_devices[bevent->disk];
  551. device->read_only =
  552. bevent->flags & vioblockflags_ro;
  553. device->size = data->disk_size;
  554. device->cylinders = data->cylinders;
  555. device->tracks = data->tracks;
  556. device->sectors = data->sectors;
  557. device->bytes_per_sector = data->bytes_per_sector;
  558. pwe->max_disk = data->max_disk;
  559. }
  560. complete(&pwe->com);
  561. break;
  562. case vioblockclose:
  563. break;
  564. case vioblockread:
  565. case vioblockwrite:
  566. viodasd_handle_read_write(bevent);
  567. break;
  568. default:
  569. pr_warning("invalid subtype!");
  570. if (hvlpevent_need_ack(event)) {
  571. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  572. HvCallEvent_ackLpEvent(event);
  573. }
  574. }
  575. }
  576. /*
  577. * Get the driver to reprobe for more disks.
  578. */
  579. static ssize_t probe_disks(struct device_driver *drv, const char *buf,
  580. size_t count)
  581. {
  582. struct viodasd_device *d;
  583. for (d = viodasd_devices; d < &viodasd_devices[MAX_DISKNO]; d++) {
  584. if (d->disk == NULL)
  585. probe_disk(d);
  586. }
  587. return count;
  588. }
  589. static DRIVER_ATTR(probe, S_IWUSR, NULL, probe_disks);
  590. static int viodasd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  591. {
  592. struct viodasd_device *d = &viodasd_devices[vdev->unit_address];
  593. d->dev = &vdev->dev;
  594. if (!probe_disk(d))
  595. return -ENODEV;
  596. return 0;
  597. }
  598. static int viodasd_remove(struct vio_dev *vdev)
  599. {
  600. struct viodasd_device *d;
  601. d = &viodasd_devices[vdev->unit_address];
  602. if (d->disk) {
  603. del_gendisk(d->disk);
  604. blk_cleanup_queue(d->disk->queue);
  605. put_disk(d->disk);
  606. d->disk = NULL;
  607. }
  608. d->dev = NULL;
  609. return 0;
  610. }
  611. /**
  612. * viodasd_device_table: Used by vio.c to match devices that we
  613. * support.
  614. */
  615. static struct vio_device_id viodasd_device_table[] __devinitdata = {
  616. { "block", "IBM,iSeries-viodasd" },
  617. { "", "" }
  618. };
  619. MODULE_DEVICE_TABLE(vio, viodasd_device_table);
  620. static struct vio_driver viodasd_driver = {
  621. .id_table = viodasd_device_table,
  622. .probe = viodasd_probe,
  623. .remove = viodasd_remove,
  624. .driver = {
  625. .name = "viodasd",
  626. .owner = THIS_MODULE,
  627. }
  628. };
  629. static int need_delete_probe;
  630. /*
  631. * Initialize the whole device driver. Handle module and non-module
  632. * versions
  633. */
  634. static int __init viodasd_init(void)
  635. {
  636. int rc;
  637. if (!firmware_has_feature(FW_FEATURE_ISERIES)) {
  638. rc = -ENODEV;
  639. goto early_fail;
  640. }
  641. /* Try to open to our host lp */
  642. if (viopath_hostLp == HvLpIndexInvalid)
  643. vio_set_hostlp();
  644. if (viopath_hostLp == HvLpIndexInvalid) {
  645. pr_warning("invalid hosting partition\n");
  646. rc = -EIO;
  647. goto early_fail;
  648. }
  649. pr_info("vers " VIOD_VERS ", hosting partition %d\n", viopath_hostLp);
  650. /* register the block device */
  651. rc = register_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
  652. if (rc) {
  653. pr_warning("Unable to get major number %d for %s\n",
  654. VIODASD_MAJOR, VIOD_GENHD_NAME);
  655. goto early_fail;
  656. }
  657. /* Actually open the path to the hosting partition */
  658. rc = viopath_open(viopath_hostLp, viomajorsubtype_blockio,
  659. VIOMAXREQ + 2);
  660. if (rc) {
  661. pr_warning("error opening path to host partition %d\n",
  662. viopath_hostLp);
  663. goto unregister_blk;
  664. }
  665. /* Initialize our request handler */
  666. vio_setHandler(viomajorsubtype_blockio, handle_block_event);
  667. rc = vio_register_driver(&viodasd_driver);
  668. if (rc) {
  669. pr_warning("vio_register_driver failed\n");
  670. goto unset_handler;
  671. }
  672. /*
  673. * If this call fails, it just means that we cannot dynamically
  674. * add virtual disks, but the driver will still work fine for
  675. * all existing disk, so ignore the failure.
  676. */
  677. if (!driver_create_file(&viodasd_driver.driver, &driver_attr_probe))
  678. need_delete_probe = 1;
  679. return 0;
  680. unset_handler:
  681. vio_clearHandler(viomajorsubtype_blockio);
  682. viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
  683. unregister_blk:
  684. unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
  685. early_fail:
  686. return rc;
  687. }
  688. module_init(viodasd_init);
  689. void __exit viodasd_exit(void)
  690. {
  691. if (need_delete_probe)
  692. driver_remove_file(&viodasd_driver.driver, &driver_attr_probe);
  693. vio_unregister_driver(&viodasd_driver);
  694. vio_clearHandler(viomajorsubtype_blockio);
  695. viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
  696. unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
  697. }
  698. module_exit(viodasd_exit);