viodasd.c 22 KB

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