blob: b441ce3832e9ff7a403fed425d79ca354aace97b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- 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>
Stephen Rothwell8962cad2008-05-23 11:41:46 +10006 * Stephen Rothwell
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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>
Jens Axboe45711f12007-10-22 21:19:53 +020044#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <asm/uaccess.h>
47#include <asm/vio.h>
Kelly Daly1ec65d72005-11-02 13:46:07 +110048#include <asm/iseries/hv_types.h>
Kelly Dalye45423e2005-11-02 12:08:31 +110049#include <asm/iseries/hv_lp_event.h>
Kelly Daly15b17182005-11-02 11:55:28 +110050#include <asm/iseries/hv_lp_config.h>
Kelly Dalyb4206772005-11-02 15:13:57 +110051#include <asm/iseries/vio.h>
Stephen Rothwellfb8b5002006-12-15 15:40:08 +110052#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54MODULE_DESCRIPTION("iSeries Virtual DASD");
55MODULE_AUTHOR("Dave Boutcher");
56MODULE_LICENSE("GPL");
57
58/*
59 * We only support 7 partitions per physical disk....so with minor
60 * numbers 0-255 we get a maximum of 32 disks.
61 */
62#define VIOD_GENHD_NAME "iseries/vd"
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#define VIOD_VERS "1.64"
65
66#define VIOD_KERN_WARNING KERN_WARNING "viod: "
67#define VIOD_KERN_INFO KERN_INFO "viod: "
68
69enum {
70 PARTITION_SHIFT = 3,
71 MAX_DISKNO = HVMAXARCHITECTEDVIRTUALDISKS,
Julia Lawallea6728c2008-03-29 08:21:08 +110072 MAX_DISK_NAME = FIELD_SIZEOF(struct gendisk, disk_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
75static DEFINE_SPINLOCK(viodasd_spinlock);
76
77#define VIOMAXREQ 16
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79#define DEVICE_NO(cell) ((struct viodasd_device *)(cell) - &viodasd_devices[0])
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081struct viodasd_waitevent {
82 struct completion com;
83 int rc;
84 u16 sub_result;
85 int max_disk; /* open */
86};
87
88static const struct vio_error_entry viodasd_err_table[] = {
89 { 0x0201, EINVAL, "Invalid Range" },
90 { 0x0202, EINVAL, "Invalid Token" },
91 { 0x0203, EIO, "DMA Error" },
92 { 0x0204, EIO, "Use Error" },
93 { 0x0205, EIO, "Release Error" },
94 { 0x0206, EINVAL, "Invalid Disk" },
95 { 0x0207, EBUSY, "Cant Lock" },
96 { 0x0208, EIO, "Already Locked" },
97 { 0x0209, EIO, "Already Unlocked" },
98 { 0x020A, EIO, "Invalid Arg" },
99 { 0x020B, EIO, "Bad IFS File" },
100 { 0x020C, EROFS, "Read Only Device" },
101 { 0x02FF, EIO, "Internal Error" },
102 { 0x0000, 0, NULL },
103};
104
105/*
106 * Figure out the biggest I/O request (in sectors) we can accept
107 */
108#define VIODASD_MAXSECTORS (4096 / 512 * VIOMAXBLOCKDMA)
109
110/*
111 * Number of disk I/O requests we've sent to OS/400
112 */
113static int num_req_outstanding;
114
115/*
116 * This is our internal structure for keeping track of disk devices
117 */
118struct viodasd_device {
119 u16 cylinders;
120 u16 tracks;
121 u16 sectors;
122 u16 bytes_per_sector;
123 u64 size;
124 int read_only;
125 spinlock_t q_lock;
126 struct gendisk *disk;
127 struct device *dev;
128} viodasd_devices[MAX_DISKNO];
129
130/*
131 * External open entry point.
132 */
Al Virof115a142008-03-02 10:22:07 -0500133static int viodasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Al Virof115a142008-03-02 10:22:07 -0500135 struct viodasd_device *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 HvLpEvent_Rc hvrc;
137 struct viodasd_waitevent we;
138 u16 flags = 0;
139
140 if (d->read_only) {
Al Virof115a142008-03-02 10:22:07 -0500141 if (mode & FMODE_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return -EROFS;
143 flags = vioblockflags_ro;
144 }
145
146 init_completion(&we.com);
147
148 /* Send the open event to OS/400 */
149 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
150 HvLpEvent_Type_VirtualIo,
151 viomajorsubtype_blockio | vioblockopen,
152 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
153 viopath_sourceinst(viopath_hostLp),
154 viopath_targetinst(viopath_hostLp),
155 (u64)(unsigned long)&we, VIOVERSION << 16,
156 ((u64)DEVICE_NO(d) << 48) | ((u64)flags << 32),
157 0, 0, 0);
158 if (hvrc != 0) {
159 printk(VIOD_KERN_WARNING "HV open failed %d\n", (int)hvrc);
160 return -EIO;
161 }
162
163 wait_for_completion(&we.com);
164
165 /* Check the return code */
166 if (we.rc != 0) {
167 const struct vio_error_entry *err =
168 vio_lookup_rc(viodasd_err_table, we.sub_result);
169
170 printk(VIOD_KERN_WARNING
171 "bad rc opening disk: %d:0x%04x (%s)\n",
172 (int)we.rc, we.sub_result, err->msg);
173 return -EIO;
174 }
175
176 return 0;
177}
178
179/*
180 * External release entry point.
181 */
Al Virof115a142008-03-02 10:22:07 -0500182static int viodasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Al Virof115a142008-03-02 10:22:07 -0500184 struct viodasd_device *d = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 HvLpEvent_Rc hvrc;
186
187 /* Send the event to OS/400. We DON'T expect a response */
188 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
189 HvLpEvent_Type_VirtualIo,
190 viomajorsubtype_blockio | vioblockclose,
191 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
192 viopath_sourceinst(viopath_hostLp),
193 viopath_targetinst(viopath_hostLp),
194 0, VIOVERSION << 16,
195 ((u64)DEVICE_NO(d) << 48) /* | ((u64)flags << 32) */,
196 0, 0, 0);
197 if (hvrc != 0)
198 printk(VIOD_KERN_WARNING "HV close call failed %d\n",
199 (int)hvrc);
200 return 0;
201}
202
203
204/* External ioctl entry point.
205 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800206static int viodasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800208 struct gendisk *disk = bdev->bd_disk;
209 struct viodasd_device *d = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Stephen Rothwell36a70032007-05-29 16:06:54 +1000211 geo->sectors = d->sectors ? d->sectors : 32;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800212 geo->heads = d->tracks ? d->tracks : 64;
213 geo->cylinders = d->cylinders ? d->cylinders :
Stephen Rothwell36a70032007-05-29 16:06:54 +1000214 get_capacity(disk) / (geo->sectors * geo->heads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219/*
220 * Our file operations table
221 */
222static struct block_device_operations viodasd_fops = {
223 .owner = THIS_MODULE,
Al Virof115a142008-03-02 10:22:07 -0500224 .open = viodasd_open,
225 .release = viodasd_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800226 .getgeo = viodasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227};
228
229/*
230 * End a request
231 */
Kiyoshi Uedab2aec242007-12-11 17:47:14 -0500232static void viodasd_end_request(struct request *req, int error,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 int num_sectors)
234{
Kiyoshi Uedab2aec242007-12-11 17:47:14 -0500235 __blk_end_request(req, error, num_sectors << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238/*
239 * Send an actual I/O request to OS/400
240 */
241static int send_request(struct request *req)
242{
243 u64 start;
244 int direction;
245 int nsg;
246 u16 viocmd;
247 HvLpEvent_Rc hvrc;
248 struct vioblocklpevent *bevent;
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100249 struct HvLpEvent *hev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 struct scatterlist sg[VIOMAXBLOCKDMA];
251 int sgindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 struct viodasd_device *d;
253 unsigned long flags;
254
Tejun Heo83096eb2009-05-07 22:24:39 +0900255 start = (u64)blk_rq_pos(req) << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 if (rq_data_dir(req) == READ) {
258 direction = DMA_FROM_DEVICE;
259 viocmd = viomajorsubtype_blockio | vioblockread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 } else {
261 direction = DMA_TO_DEVICE;
262 viocmd = viomajorsubtype_blockio | vioblockwrite;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264
265 d = req->rq_disk->private_data;
266
267 /* Now build the scatter-gather list */
Jens Axboe45711f12007-10-22 21:19:53 +0200268 sg_init_table(sg, VIOMAXBLOCKDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 nsg = blk_rq_map_sg(req->q, req, sg);
270 nsg = dma_map_sg(d->dev, sg, nsg, direction);
271
272 spin_lock_irqsave(&viodasd_spinlock, flags);
273 num_req_outstanding++;
274
275 /* This optimization handles a single DMA block */
276 if (nsg == 1)
277 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
278 HvLpEvent_Type_VirtualIo, viocmd,
279 HvLpEvent_AckInd_DoAck,
280 HvLpEvent_AckType_ImmediateAck,
281 viopath_sourceinst(viopath_hostLp),
282 viopath_targetinst(viopath_hostLp),
283 (u64)(unsigned long)req, VIOVERSION << 16,
284 ((u64)DEVICE_NO(d) << 48), start,
285 ((u64)sg_dma_address(&sg[0])) << 32,
286 sg_dma_len(&sg[0]));
287 else {
288 bevent = (struct vioblocklpevent *)
289 vio_get_event_buffer(viomajorsubtype_blockio);
290 if (bevent == NULL) {
291 printk(VIOD_KERN_WARNING
292 "error allocating disk event buffer\n");
293 goto error_ret;
294 }
295
296 /*
297 * Now build up the actual request. Note that we store
298 * the pointer to the request in the correlation
299 * token so we can match the response up later
300 */
301 memset(bevent, 0, sizeof(struct vioblocklpevent));
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100302 hev = &bevent->event;
303 hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DO_ACK |
304 HV_LP_EVENT_INT;
305 hev->xType = HvLpEvent_Type_VirtualIo;
306 hev->xSubtype = viocmd;
307 hev->xSourceLp = HvLpConfig_getLpIndex();
308 hev->xTargetLp = viopath_hostLp;
309 hev->xSizeMinus1 =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 offsetof(struct vioblocklpevent, u.rw_data.dma_info) +
311 (sizeof(bevent->u.rw_data.dma_info[0]) * nsg) - 1;
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100312 hev->xSourceInstanceId = viopath_sourceinst(viopath_hostLp);
313 hev->xTargetInstanceId = viopath_targetinst(viopath_hostLp);
314 hev->xCorrelationToken = (u64)req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 bevent->version = VIOVERSION;
316 bevent->disk = DEVICE_NO(d);
317 bevent->u.rw_data.offset = start;
318
319 /*
320 * Copy just the dma information from the sg list
321 * into the request
322 */
323 for (sgindex = 0; sgindex < nsg; sgindex++) {
324 bevent->u.rw_data.dma_info[sgindex].token =
325 sg_dma_address(&sg[sgindex]);
326 bevent->u.rw_data.dma_info[sgindex].len =
327 sg_dma_len(&sg[sgindex]);
328 }
329
330 /* Send the request */
331 hvrc = HvCallEvent_signalLpEvent(&bevent->event);
332 vio_free_event_buffer(viomajorsubtype_blockio, bevent);
333 }
334
335 if (hvrc != HvLpEvent_Rc_Good) {
336 printk(VIOD_KERN_WARNING
337 "error sending disk event to OS/400 (rc %d)\n",
338 (int)hvrc);
339 goto error_ret;
340 }
341 spin_unlock_irqrestore(&viodasd_spinlock, flags);
342 return 0;
343
344error_ret:
345 num_req_outstanding--;
346 spin_unlock_irqrestore(&viodasd_spinlock, flags);
347 dma_unmap_sg(d->dev, sg, nsg, direction);
348 return -1;
349}
350
351/*
352 * This is the external request processing routine
353 */
Jens Axboe165125e2007-07-24 09:28:11 +0200354static void do_viodasd_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct request *req;
357
358 /*
359 * If we already have the maximum number of requests
360 * outstanding to OS/400 just bail out. We'll come
361 * back later.
362 */
363 while (num_req_outstanding < VIOMAXREQ) {
Tejun Heo9934c8c2009-05-08 11:54:16 +0900364 req = blk_fetch_request(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (req == NULL)
366 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /* check that request contains a valid command */
368 if (!blk_fs_request(req)) {
Tejun Heo5b936292009-05-07 22:24:38 +0900369 viodasd_end_request(req, -EIO, blk_rq_sectors(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 continue;
371 }
372 /* Try sending the request */
373 if (send_request(req) != 0)
Tejun Heo5b936292009-05-07 22:24:38 +0900374 viodasd_end_request(req, -EIO, blk_rq_sectors(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376}
377
378/*
379 * Probe a single disk and fill in the viodasd_device structure
380 * for it.
381 */
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000382static int probe_disk(struct viodasd_device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 HvLpEvent_Rc hvrc;
385 struct viodasd_waitevent we;
386 int dev_no = DEVICE_NO(d);
387 struct gendisk *g;
388 struct request_queue *q;
389 u16 flags = 0;
390
391retry:
392 init_completion(&we.com);
393
394 /* Send the open event to OS/400 */
395 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
396 HvLpEvent_Type_VirtualIo,
397 viomajorsubtype_blockio | vioblockopen,
398 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
399 viopath_sourceinst(viopath_hostLp),
400 viopath_targetinst(viopath_hostLp),
401 (u64)(unsigned long)&we, VIOVERSION << 16,
402 ((u64)dev_no << 48) | ((u64)flags<< 32),
403 0, 0, 0);
404 if (hvrc != 0) {
405 printk(VIOD_KERN_WARNING "bad rc on HV open %d\n", (int)hvrc);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
409 wait_for_completion(&we.com);
410
411 if (we.rc != 0) {
412 if (flags != 0)
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /* try again with read only flag set */
415 flags = vioblockflags_ro;
416 goto retry;
417 }
418 if (we.max_disk > (MAX_DISKNO - 1)) {
Marcin Slusarz49b3a3c2009-08-24 10:56:38 +0200419 printk_once(VIOD_KERN_INFO
420 "Only examining the first %d of %d disks connected\n",
421 MAX_DISKNO, we.max_disk + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
424 /* Send the close event to OS/400. We DON'T expect a response */
425 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
426 HvLpEvent_Type_VirtualIo,
427 viomajorsubtype_blockio | vioblockclose,
428 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
429 viopath_sourceinst(viopath_hostLp),
430 viopath_targetinst(viopath_hostLp),
431 0, VIOVERSION << 16,
432 ((u64)dev_no << 48) | ((u64)flags << 32),
433 0, 0, 0);
434 if (hvrc != 0) {
435 printk(VIOD_KERN_WARNING
436 "bad rc sending event to OS/400 %d\n", (int)hvrc);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000439
440 if (d->dev == NULL) {
441 /* this is when we reprobe for new disks */
442 if (vio_create_viodasd(dev_no) == NULL) {
443 printk(VIOD_KERN_WARNING
444 "cannot allocate virtual device for disk %d\n",
445 dev_no);
446 return 0;
447 }
448 /*
449 * The vio_create_viodasd will have recursed into this
450 * routine with d->dev set to the new vio device and
451 * will finish the setup of the disk below.
452 */
453 return 1;
454 }
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* create the request queue for the disk */
457 spin_lock_init(&d->q_lock);
458 q = blk_init_queue(do_viodasd_request, &d->q_lock);
459 if (q == NULL) {
460 printk(VIOD_KERN_WARNING "cannot allocate queue for disk %d\n",
461 dev_no);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000462 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464 g = alloc_disk(1 << PARTITION_SHIFT);
465 if (g == NULL) {
466 printk(VIOD_KERN_WARNING
467 "cannot allocate disk structure for disk %d\n",
468 dev_no);
469 blk_cleanup_queue(q);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
473 d->disk = g;
474 blk_queue_max_hw_segments(q, VIOMAXBLOCKDMA);
475 blk_queue_max_phys_segments(q, VIOMAXBLOCKDMA);
476 blk_queue_max_sectors(q, VIODASD_MAXSECTORS);
477 g->major = VIODASD_MAJOR;
478 g->first_minor = dev_no << PARTITION_SHIFT;
479 if (dev_no >= 26)
480 snprintf(g->disk_name, sizeof(g->disk_name),
481 VIOD_GENHD_NAME "%c%c",
482 'a' + (dev_no / 26) - 1, 'a' + (dev_no % 26));
483 else
484 snprintf(g->disk_name, sizeof(g->disk_name),
485 VIOD_GENHD_NAME "%c", 'a' + (dev_no % 26));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 g->fops = &viodasd_fops;
487 g->queue = q;
488 g->private_data = d;
489 g->driverfs_dev = d->dev;
490 set_capacity(g, d->size >> 9);
491
492 printk(VIOD_KERN_INFO "disk %d: %lu sectors (%lu MB) "
493 "CHS=%d/%d/%d sector size %d%s\n",
494 dev_no, (unsigned long)(d->size >> 9),
495 (unsigned long)(d->size >> 20),
496 (int)d->cylinders, (int)d->tracks,
497 (int)d->sectors, (int)d->bytes_per_sector,
498 d->read_only ? " (RO)" : "");
499
500 /* register us in the global list */
501 add_disk(g);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000502 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/* returns the total number of scatterlist elements converted */
506static int block_event_to_scatterlist(const struct vioblocklpevent *bevent,
507 struct scatterlist *sg, int *total_len)
508{
509 int i, numsg;
510 const struct rw_data *rw_data = &bevent->u.rw_data;
511 static const int offset =
512 offsetof(struct vioblocklpevent, u.rw_data.dma_info);
513 static const int element_size = sizeof(rw_data->dma_info[0]);
514
515 numsg = ((bevent->event.xSizeMinus1 + 1) - offset) / element_size;
516 if (numsg > VIOMAXBLOCKDMA)
517 numsg = VIOMAXBLOCKDMA;
518
519 *total_len = 0;
Benjamin Herrenschmidt25c0a7b2008-03-12 17:23:56 +1100520 sg_init_table(sg, VIOMAXBLOCKDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 for (i = 0; (i < numsg) && (rw_data->dma_info[i].len > 0); ++i) {
522 sg_dma_address(&sg[i]) = rw_data->dma_info[i].token;
523 sg_dma_len(&sg[i]) = rw_data->dma_info[i].len;
524 *total_len += rw_data->dma_info[i].len;
525 }
526 return i;
527}
528
529/*
530 * Restart all queues, starting with the one _after_ the disk given,
531 * thus reducing the chance of starvation of higher numbered disks.
532 */
533static void viodasd_restart_all_queues_starting_from(int first_index)
534{
535 int i;
536
537 for (i = first_index + 1; i < MAX_DISKNO; ++i)
538 if (viodasd_devices[i].disk)
539 blk_run_queue(viodasd_devices[i].disk->queue);
540 for (i = 0; i <= first_index; ++i)
541 if (viodasd_devices[i].disk)
542 blk_run_queue(viodasd_devices[i].disk->queue);
543}
544
545/*
546 * For read and write requests, decrement the number of outstanding requests,
547 * Free the DMA buffers we allocated.
548 */
549static int viodasd_handle_read_write(struct vioblocklpevent *bevent)
550{
551 int num_sg, num_sect, pci_direction, total_len;
552 struct request *req;
553 struct scatterlist sg[VIOMAXBLOCKDMA];
554 struct HvLpEvent *event = &bevent->event;
555 unsigned long irq_flags;
556 struct viodasd_device *d;
557 int error;
558 spinlock_t *qlock;
559
560 num_sg = block_event_to_scatterlist(bevent, sg, &total_len);
561 num_sect = total_len >> 9;
562 if (event->xSubtype == (viomajorsubtype_blockio | vioblockread))
563 pci_direction = DMA_FROM_DEVICE;
564 else
565 pci_direction = DMA_TO_DEVICE;
566 req = (struct request *)bevent->event.xCorrelationToken;
567 d = req->rq_disk->private_data;
568
569 dma_unmap_sg(d->dev, sg, num_sg, pci_direction);
570
571 /*
572 * Since this is running in interrupt mode, we need to make sure
573 * we're not stepping on any global I/O operations
574 */
575 spin_lock_irqsave(&viodasd_spinlock, irq_flags);
576 num_req_outstanding--;
577 spin_unlock_irqrestore(&viodasd_spinlock, irq_flags);
578
Kiyoshi Uedab2aec242007-12-11 17:47:14 -0500579 error = (event->xRc == HvLpEvent_Rc_Good) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (error) {
581 const struct vio_error_entry *err;
582 err = vio_lookup_rc(viodasd_err_table, bevent->sub_result);
583 printk(VIOD_KERN_WARNING "read/write error %d:0x%04x (%s)\n",
584 event->xRc, bevent->sub_result, err->msg);
Tejun Heo5b936292009-05-07 22:24:38 +0900585 num_sect = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587 qlock = req->q->queue_lock;
588 spin_lock_irqsave(qlock, irq_flags);
Kiyoshi Uedab2aec242007-12-11 17:47:14 -0500589 viodasd_end_request(req, error, num_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 spin_unlock_irqrestore(qlock, irq_flags);
591
592 /* Finally, try to get more requests off of this device's queue */
593 viodasd_restart_all_queues_starting_from(DEVICE_NO(d));
594
595 return 0;
596}
597
598/* This routine handles incoming block LP events */
599static void handle_block_event(struct HvLpEvent *event)
600{
601 struct vioblocklpevent *bevent = (struct vioblocklpevent *)event;
602 struct viodasd_waitevent *pwe;
603
604 if (event == NULL)
605 /* Notification that a partition went away! */
606 return;
607 /* First, we should NEVER get an int here...only acks */
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100608 if (hvlpevent_is_int(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 printk(VIOD_KERN_WARNING
610 "Yikes! got an int in viodasd event handler!\n");
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100611 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 event->xRc = HvLpEvent_Rc_InvalidSubtype;
613 HvCallEvent_ackLpEvent(event);
614 }
615 }
616
617 switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
618 case vioblockopen:
619 /*
620 * Handle a response to an open request. We get all the
621 * disk information in the response, so update it. The
622 * correlation token contains a pointer to a waitevent
623 * structure that has a completion in it. update the
624 * return code in the waitevent structure and post the
625 * completion to wake up the guy who sent the request
626 */
627 pwe = (struct viodasd_waitevent *)event->xCorrelationToken;
628 pwe->rc = event->xRc;
629 pwe->sub_result = bevent->sub_result;
630 if (event->xRc == HvLpEvent_Rc_Good) {
631 const struct open_data *data = &bevent->u.open_data;
632 struct viodasd_device *device =
633 &viodasd_devices[bevent->disk];
634 device->read_only =
635 bevent->flags & vioblockflags_ro;
636 device->size = data->disk_size;
637 device->cylinders = data->cylinders;
638 device->tracks = data->tracks;
639 device->sectors = data->sectors;
640 device->bytes_per_sector = data->bytes_per_sector;
641 pwe->max_disk = data->max_disk;
642 }
643 complete(&pwe->com);
644 break;
645 case vioblockclose:
646 break;
647 case vioblockread:
648 case vioblockwrite:
649 viodasd_handle_read_write(bevent);
650 break;
651
652 default:
653 printk(VIOD_KERN_WARNING "invalid subtype!");
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100654 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 event->xRc = HvLpEvent_Rc_InvalidSubtype;
656 HvCallEvent_ackLpEvent(event);
657 }
658 }
659}
660
661/*
662 * Get the driver to reprobe for more disks.
663 */
664static ssize_t probe_disks(struct device_driver *drv, const char *buf,
665 size_t count)
666{
667 struct viodasd_device *d;
668
669 for (d = viodasd_devices; d < &viodasd_devices[MAX_DISKNO]; d++) {
670 if (d->disk == NULL)
671 probe_disk(d);
672 }
673 return count;
674}
675static DRIVER_ATTR(probe, S_IWUSR, NULL, probe_disks);
676
677static int viodasd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
678{
679 struct viodasd_device *d = &viodasd_devices[vdev->unit_address];
680
681 d->dev = &vdev->dev;
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000682 if (!probe_disk(d))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return -ENODEV;
684 return 0;
685}
686
687static int viodasd_remove(struct vio_dev *vdev)
688{
689 struct viodasd_device *d;
690
691 d = &viodasd_devices[vdev->unit_address];
692 if (d->disk) {
693 del_gendisk(d->disk);
694 blk_cleanup_queue(d->disk->queue);
695 put_disk(d->disk);
696 d->disk = NULL;
697 }
698 d->dev = NULL;
699 return 0;
700}
701
702/**
703 * viodasd_device_table: Used by vio.c to match devices that we
704 * support.
705 */
706static struct vio_device_id viodasd_device_table[] __devinitdata = {
Stephen Rothwellfbabeb62006-05-15 13:41:22 +1000707 { "block", "IBM,iSeries-viodasd" },
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000708 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710MODULE_DEVICE_TABLE(vio, viodasd_device_table);
Stephen Rothwell915124d2005-10-24 15:12:22 +1000711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712static struct vio_driver viodasd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 .id_table = viodasd_device_table,
714 .probe = viodasd_probe,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000715 .remove = viodasd_remove,
716 .driver = {
717 .name = "viodasd",
Stephen Rothwell915124d2005-10-24 15:12:22 +1000718 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720};
721
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100722static int need_delete_probe;
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724/*
725 * Initialize the whole device driver. Handle module and non-module
726 * versions
727 */
728static int __init viodasd_init(void)
729{
730 int rc;
731
Stephen Rothwellfb8b5002006-12-15 15:40:08 +1100732 if (!firmware_has_feature(FW_FEATURE_ISERIES)) {
733 rc = -ENODEV;
734 goto early_fail;
735 }
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 /* Try to open to our host lp */
738 if (viopath_hostLp == HvLpIndexInvalid)
739 vio_set_hostlp();
740
741 if (viopath_hostLp == HvLpIndexInvalid) {
742 printk(VIOD_KERN_WARNING "invalid hosting partition\n");
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100743 rc = -EIO;
744 goto early_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
747 printk(VIOD_KERN_INFO "vers " VIOD_VERS ", hosting partition %d\n",
748 viopath_hostLp);
749
750 /* register the block device */
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100751 rc = register_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
752 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 printk(VIOD_KERN_WARNING
754 "Unable to get major number %d for %s\n",
755 VIODASD_MAJOR, VIOD_GENHD_NAME);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100756 goto early_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758 /* Actually open the path to the hosting partition */
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100759 rc = viopath_open(viopath_hostLp, viomajorsubtype_blockio,
760 VIOMAXREQ + 2);
761 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 printk(VIOD_KERN_WARNING
763 "error opening path to host partition %d\n",
764 viopath_hostLp);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100765 goto unregister_blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767
768 /* Initialize our request handler */
769 vio_setHandler(viomajorsubtype_blockio, handle_block_event);
770
771 rc = vio_register_driver(&viodasd_driver);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100772 if (rc) {
773 printk(VIOD_KERN_WARNING "vio_register_driver failed\n");
774 goto unset_handler;
775 }
776
777 /*
778 * If this call fails, it just means that we cannot dynamically
779 * add virtual disks, but the driver will still work fine for
780 * all existing disk, so ignore the failure.
781 */
782 if (!driver_create_file(&viodasd_driver.driver, &driver_attr_probe))
783 need_delete_probe = 1;
784
785 return 0;
786
787unset_handler:
788 vio_clearHandler(viomajorsubtype_blockio);
789 viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
790unregister_blk:
791 unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
792early_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return rc;
794}
795module_init(viodasd_init);
796
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100797void __exit viodasd_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100799 if (need_delete_probe)
800 driver_remove_file(&viodasd_driver.driver, &driver_attr_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 vio_unregister_driver(&viodasd_driver);
802 vio_clearHandler(viomajorsubtype_blockio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100804 unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806module_exit(viodasd_exit);