blob: e824b672e05a23c75473d6d2a04b4462eb709bec [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>
6 * Stephen Rothwell <sfr@au1.ibm.com>
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <asm/uaccess.h>
46#include <asm/vio.h>
Kelly Daly1ec65d72005-11-02 13:46:07 +110047#include <asm/iseries/hv_types.h>
Kelly Dalye45423e2005-11-02 12:08:31 +110048#include <asm/iseries/hv_lp_event.h>
Kelly Daly15b17182005-11-02 11:55:28 +110049#include <asm/iseries/hv_lp_config.h>
Kelly Dalyb4206772005-11-02 15:13:57 +110050#include <asm/iseries/vio.h>
Stephen Rothwellfb8b5002006-12-15 15:40:08 +110051#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53MODULE_DESCRIPTION("iSeries Virtual DASD");
54MODULE_AUTHOR("Dave Boutcher");
55MODULE_LICENSE("GPL");
56
57/*
58 * We only support 7 partitions per physical disk....so with minor
59 * numbers 0-255 we get a maximum of 32 disks.
60 */
61#define VIOD_GENHD_NAME "iseries/vd"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#define VIOD_VERS "1.64"
64
65#define VIOD_KERN_WARNING KERN_WARNING "viod: "
66#define VIOD_KERN_INFO KERN_INFO "viod: "
67
68enum {
69 PARTITION_SHIFT = 3,
70 MAX_DISKNO = HVMAXARCHITECTEDVIRTUALDISKS,
71 MAX_DISK_NAME = sizeof(((struct gendisk *)0)->disk_name)
72};
73
74static DEFINE_SPINLOCK(viodasd_spinlock);
75
76#define VIOMAXREQ 16
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78#define DEVICE_NO(cell) ((struct viodasd_device *)(cell) - &viodasd_devices[0])
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080struct viodasd_waitevent {
81 struct completion com;
82 int rc;
83 u16 sub_result;
84 int max_disk; /* open */
85};
86
87static const struct vio_error_entry viodasd_err_table[] = {
88 { 0x0201, EINVAL, "Invalid Range" },
89 { 0x0202, EINVAL, "Invalid Token" },
90 { 0x0203, EIO, "DMA Error" },
91 { 0x0204, EIO, "Use Error" },
92 { 0x0205, EIO, "Release Error" },
93 { 0x0206, EINVAL, "Invalid Disk" },
94 { 0x0207, EBUSY, "Cant Lock" },
95 { 0x0208, EIO, "Already Locked" },
96 { 0x0209, EIO, "Already Unlocked" },
97 { 0x020A, EIO, "Invalid Arg" },
98 { 0x020B, EIO, "Bad IFS File" },
99 { 0x020C, EROFS, "Read Only Device" },
100 { 0x02FF, EIO, "Internal Error" },
101 { 0x0000, 0, NULL },
102};
103
104/*
105 * Figure out the biggest I/O request (in sectors) we can accept
106 */
107#define VIODASD_MAXSECTORS (4096 / 512 * VIOMAXBLOCKDMA)
108
109/*
110 * Number of disk I/O requests we've sent to OS/400
111 */
112static int num_req_outstanding;
113
114/*
115 * This is our internal structure for keeping track of disk devices
116 */
117struct viodasd_device {
118 u16 cylinders;
119 u16 tracks;
120 u16 sectors;
121 u16 bytes_per_sector;
122 u64 size;
123 int read_only;
124 spinlock_t q_lock;
125 struct gendisk *disk;
126 struct device *dev;
127} viodasd_devices[MAX_DISKNO];
128
129/*
130 * External open entry point.
131 */
132static int viodasd_open(struct inode *ino, struct file *fil)
133{
134 struct viodasd_device *d = ino->i_bdev->bd_disk->private_data;
135 HvLpEvent_Rc hvrc;
136 struct viodasd_waitevent we;
137 u16 flags = 0;
138
139 if (d->read_only) {
140 if ((fil != NULL) && (fil->f_mode & FMODE_WRITE))
141 return -EROFS;
142 flags = vioblockflags_ro;
143 }
144
145 init_completion(&we.com);
146
147 /* Send the open event to OS/400 */
148 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
149 HvLpEvent_Type_VirtualIo,
150 viomajorsubtype_blockio | vioblockopen,
151 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
152 viopath_sourceinst(viopath_hostLp),
153 viopath_targetinst(viopath_hostLp),
154 (u64)(unsigned long)&we, VIOVERSION << 16,
155 ((u64)DEVICE_NO(d) << 48) | ((u64)flags << 32),
156 0, 0, 0);
157 if (hvrc != 0) {
158 printk(VIOD_KERN_WARNING "HV open failed %d\n", (int)hvrc);
159 return -EIO;
160 }
161
162 wait_for_completion(&we.com);
163
164 /* Check the return code */
165 if (we.rc != 0) {
166 const struct vio_error_entry *err =
167 vio_lookup_rc(viodasd_err_table, we.sub_result);
168
169 printk(VIOD_KERN_WARNING
170 "bad rc opening disk: %d:0x%04x (%s)\n",
171 (int)we.rc, we.sub_result, err->msg);
172 return -EIO;
173 }
174
175 return 0;
176}
177
178/*
179 * External release entry point.
180 */
181static int viodasd_release(struct inode *ino, struct file *fil)
182{
183 struct viodasd_device *d = ino->i_bdev->bd_disk->private_data;
184 HvLpEvent_Rc hvrc;
185
186 /* Send the event to OS/400. We DON'T expect a response */
187 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
188 HvLpEvent_Type_VirtualIo,
189 viomajorsubtype_blockio | vioblockclose,
190 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
191 viopath_sourceinst(viopath_hostLp),
192 viopath_targetinst(viopath_hostLp),
193 0, VIOVERSION << 16,
194 ((u64)DEVICE_NO(d) << 48) /* | ((u64)flags << 32) */,
195 0, 0, 0);
196 if (hvrc != 0)
197 printk(VIOD_KERN_WARNING "HV close call failed %d\n",
198 (int)hvrc);
199 return 0;
200}
201
202
203/* External ioctl entry point.
204 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800205static int viodasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800207 struct gendisk *disk = bdev->bd_disk;
208 struct viodasd_device *d = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Stephen Rothwell36a70032007-05-29 16:06:54 +1000210 geo->sectors = d->sectors ? d->sectors : 32;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800211 geo->heads = d->tracks ? d->tracks : 64;
212 geo->cylinders = d->cylinders ? d->cylinders :
Stephen Rothwell36a70032007-05-29 16:06:54 +1000213 get_capacity(disk) / (geo->sectors * geo->heads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218/*
219 * Our file operations table
220 */
221static struct block_device_operations viodasd_fops = {
222 .owner = THIS_MODULE,
223 .open = viodasd_open,
224 .release = viodasd_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800225 .getgeo = viodasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226};
227
228/*
229 * End a request
230 */
231static void viodasd_end_request(struct request *req, int uptodate,
232 int num_sectors)
233{
234 if (end_that_request_first(req, uptodate, num_sectors))
235 return;
236 add_disk_randomness(req->rq_disk);
Tejun Heo8ffdc652006-01-06 09:49:03 +0100237 end_that_request_last(req, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240/*
241 * Send an actual I/O request to OS/400
242 */
243static int send_request(struct request *req)
244{
245 u64 start;
246 int direction;
247 int nsg;
248 u16 viocmd;
249 HvLpEvent_Rc hvrc;
250 struct vioblocklpevent *bevent;
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100251 struct HvLpEvent *hev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 struct scatterlist sg[VIOMAXBLOCKDMA];
253 int sgindex;
254 int statindex;
255 struct viodasd_device *d;
256 unsigned long flags;
257
258 start = (u64)req->sector << 9;
259
260 if (rq_data_dir(req) == READ) {
261 direction = DMA_FROM_DEVICE;
262 viocmd = viomajorsubtype_blockio | vioblockread;
263 statindex = 0;
264 } else {
265 direction = DMA_TO_DEVICE;
266 viocmd = viomajorsubtype_blockio | vioblockwrite;
267 statindex = 1;
268 }
269
270 d = req->rq_disk->private_data;
271
272 /* Now build the scatter-gather list */
273 nsg = blk_rq_map_sg(req->q, req, sg);
274 nsg = dma_map_sg(d->dev, sg, nsg, direction);
275
276 spin_lock_irqsave(&viodasd_spinlock, flags);
277 num_req_outstanding++;
278
279 /* This optimization handles a single DMA block */
280 if (nsg == 1)
281 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
282 HvLpEvent_Type_VirtualIo, viocmd,
283 HvLpEvent_AckInd_DoAck,
284 HvLpEvent_AckType_ImmediateAck,
285 viopath_sourceinst(viopath_hostLp),
286 viopath_targetinst(viopath_hostLp),
287 (u64)(unsigned long)req, VIOVERSION << 16,
288 ((u64)DEVICE_NO(d) << 48), start,
289 ((u64)sg_dma_address(&sg[0])) << 32,
290 sg_dma_len(&sg[0]));
291 else {
292 bevent = (struct vioblocklpevent *)
293 vio_get_event_buffer(viomajorsubtype_blockio);
294 if (bevent == NULL) {
295 printk(VIOD_KERN_WARNING
296 "error allocating disk event buffer\n");
297 goto error_ret;
298 }
299
300 /*
301 * Now build up the actual request. Note that we store
302 * the pointer to the request in the correlation
303 * token so we can match the response up later
304 */
305 memset(bevent, 0, sizeof(struct vioblocklpevent));
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100306 hev = &bevent->event;
307 hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DO_ACK |
308 HV_LP_EVENT_INT;
309 hev->xType = HvLpEvent_Type_VirtualIo;
310 hev->xSubtype = viocmd;
311 hev->xSourceLp = HvLpConfig_getLpIndex();
312 hev->xTargetLp = viopath_hostLp;
313 hev->xSizeMinus1 =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 offsetof(struct vioblocklpevent, u.rw_data.dma_info) +
315 (sizeof(bevent->u.rw_data.dma_info[0]) * nsg) - 1;
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100316 hev->xSourceInstanceId = viopath_sourceinst(viopath_hostLp);
317 hev->xTargetInstanceId = viopath_targetinst(viopath_hostLp);
318 hev->xCorrelationToken = (u64)req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 bevent->version = VIOVERSION;
320 bevent->disk = DEVICE_NO(d);
321 bevent->u.rw_data.offset = start;
322
323 /*
324 * Copy just the dma information from the sg list
325 * into the request
326 */
327 for (sgindex = 0; sgindex < nsg; sgindex++) {
328 bevent->u.rw_data.dma_info[sgindex].token =
329 sg_dma_address(&sg[sgindex]);
330 bevent->u.rw_data.dma_info[sgindex].len =
331 sg_dma_len(&sg[sgindex]);
332 }
333
334 /* Send the request */
335 hvrc = HvCallEvent_signalLpEvent(&bevent->event);
336 vio_free_event_buffer(viomajorsubtype_blockio, bevent);
337 }
338
339 if (hvrc != HvLpEvent_Rc_Good) {
340 printk(VIOD_KERN_WARNING
341 "error sending disk event to OS/400 (rc %d)\n",
342 (int)hvrc);
343 goto error_ret;
344 }
345 spin_unlock_irqrestore(&viodasd_spinlock, flags);
346 return 0;
347
348error_ret:
349 num_req_outstanding--;
350 spin_unlock_irqrestore(&viodasd_spinlock, flags);
351 dma_unmap_sg(d->dev, sg, nsg, direction);
352 return -1;
353}
354
355/*
356 * This is the external request processing routine
357 */
Jens Axboe165125e2007-07-24 09:28:11 +0200358static void do_viodasd_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 struct request *req;
361
362 /*
363 * If we already have the maximum number of requests
364 * outstanding to OS/400 just bail out. We'll come
365 * back later.
366 */
367 while (num_req_outstanding < VIOMAXREQ) {
368 req = elv_next_request(q);
369 if (req == NULL)
370 return;
371 /* dequeue the current request from the queue */
372 blkdev_dequeue_request(req);
373 /* check that request contains a valid command */
374 if (!blk_fs_request(req)) {
375 viodasd_end_request(req, 0, req->hard_nr_sectors);
376 continue;
377 }
378 /* Try sending the request */
379 if (send_request(req) != 0)
380 viodasd_end_request(req, 0, req->hard_nr_sectors);
381 }
382}
383
384/*
385 * Probe a single disk and fill in the viodasd_device structure
386 * for it.
387 */
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000388static int probe_disk(struct viodasd_device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 HvLpEvent_Rc hvrc;
391 struct viodasd_waitevent we;
392 int dev_no = DEVICE_NO(d);
393 struct gendisk *g;
394 struct request_queue *q;
395 u16 flags = 0;
396
397retry:
398 init_completion(&we.com);
399
400 /* Send the open event to OS/400 */
401 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
402 HvLpEvent_Type_VirtualIo,
403 viomajorsubtype_blockio | vioblockopen,
404 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
405 viopath_sourceinst(viopath_hostLp),
406 viopath_targetinst(viopath_hostLp),
407 (u64)(unsigned long)&we, VIOVERSION << 16,
408 ((u64)dev_no << 48) | ((u64)flags<< 32),
409 0, 0, 0);
410 if (hvrc != 0) {
411 printk(VIOD_KERN_WARNING "bad rc on HV open %d\n", (int)hvrc);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000412 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
415 wait_for_completion(&we.com);
416
417 if (we.rc != 0) {
418 if (flags != 0)
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000419 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /* try again with read only flag set */
421 flags = vioblockflags_ro;
422 goto retry;
423 }
424 if (we.max_disk > (MAX_DISKNO - 1)) {
425 static int warned;
426
427 if (warned == 0) {
428 warned++;
429 printk(VIOD_KERN_INFO
430 "Only examining the first %d "
431 "of %d disks connected\n",
432 MAX_DISKNO, we.max_disk + 1);
433 }
434 }
435
436 /* Send the close event to OS/400. We DON'T expect a response */
437 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
438 HvLpEvent_Type_VirtualIo,
439 viomajorsubtype_blockio | vioblockclose,
440 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
441 viopath_sourceinst(viopath_hostLp),
442 viopath_targetinst(viopath_hostLp),
443 0, VIOVERSION << 16,
444 ((u64)dev_no << 48) | ((u64)flags << 32),
445 0, 0, 0);
446 if (hvrc != 0) {
447 printk(VIOD_KERN_WARNING
448 "bad rc sending event to OS/400 %d\n", (int)hvrc);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000451
452 if (d->dev == NULL) {
453 /* this is when we reprobe for new disks */
454 if (vio_create_viodasd(dev_no) == NULL) {
455 printk(VIOD_KERN_WARNING
456 "cannot allocate virtual device for disk %d\n",
457 dev_no);
458 return 0;
459 }
460 /*
461 * The vio_create_viodasd will have recursed into this
462 * routine with d->dev set to the new vio device and
463 * will finish the setup of the disk below.
464 */
465 return 1;
466 }
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* create the request queue for the disk */
469 spin_lock_init(&d->q_lock);
470 q = blk_init_queue(do_viodasd_request, &d->q_lock);
471 if (q == NULL) {
472 printk(VIOD_KERN_WARNING "cannot allocate queue for disk %d\n",
473 dev_no);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 g = alloc_disk(1 << PARTITION_SHIFT);
477 if (g == NULL) {
478 printk(VIOD_KERN_WARNING
479 "cannot allocate disk structure for disk %d\n",
480 dev_no);
481 blk_cleanup_queue(q);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000482 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484
485 d->disk = g;
486 blk_queue_max_hw_segments(q, VIOMAXBLOCKDMA);
487 blk_queue_max_phys_segments(q, VIOMAXBLOCKDMA);
488 blk_queue_max_sectors(q, VIODASD_MAXSECTORS);
489 g->major = VIODASD_MAJOR;
490 g->first_minor = dev_no << PARTITION_SHIFT;
491 if (dev_no >= 26)
492 snprintf(g->disk_name, sizeof(g->disk_name),
493 VIOD_GENHD_NAME "%c%c",
494 'a' + (dev_no / 26) - 1, 'a' + (dev_no % 26));
495 else
496 snprintf(g->disk_name, sizeof(g->disk_name),
497 VIOD_GENHD_NAME "%c", 'a' + (dev_no % 26));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 g->fops = &viodasd_fops;
499 g->queue = q;
500 g->private_data = d;
501 g->driverfs_dev = d->dev;
502 set_capacity(g, d->size >> 9);
503
504 printk(VIOD_KERN_INFO "disk %d: %lu sectors (%lu MB) "
505 "CHS=%d/%d/%d sector size %d%s\n",
506 dev_no, (unsigned long)(d->size >> 9),
507 (unsigned long)(d->size >> 20),
508 (int)d->cylinders, (int)d->tracks,
509 (int)d->sectors, (int)d->bytes_per_sector,
510 d->read_only ? " (RO)" : "");
511
512 /* register us in the global list */
513 add_disk(g);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000514 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
517/* returns the total number of scatterlist elements converted */
518static int block_event_to_scatterlist(const struct vioblocklpevent *bevent,
519 struct scatterlist *sg, int *total_len)
520{
521 int i, numsg;
522 const struct rw_data *rw_data = &bevent->u.rw_data;
523 static const int offset =
524 offsetof(struct vioblocklpevent, u.rw_data.dma_info);
525 static const int element_size = sizeof(rw_data->dma_info[0]);
526
527 numsg = ((bevent->event.xSizeMinus1 + 1) - offset) / element_size;
528 if (numsg > VIOMAXBLOCKDMA)
529 numsg = VIOMAXBLOCKDMA;
530
531 *total_len = 0;
532 memset(sg, 0, sizeof(sg[0]) * VIOMAXBLOCKDMA);
533
534 for (i = 0; (i < numsg) && (rw_data->dma_info[i].len > 0); ++i) {
535 sg_dma_address(&sg[i]) = rw_data->dma_info[i].token;
536 sg_dma_len(&sg[i]) = rw_data->dma_info[i].len;
537 *total_len += rw_data->dma_info[i].len;
538 }
539 return i;
540}
541
542/*
543 * Restart all queues, starting with the one _after_ the disk given,
544 * thus reducing the chance of starvation of higher numbered disks.
545 */
546static void viodasd_restart_all_queues_starting_from(int first_index)
547{
548 int i;
549
550 for (i = first_index + 1; i < MAX_DISKNO; ++i)
551 if (viodasd_devices[i].disk)
552 blk_run_queue(viodasd_devices[i].disk->queue);
553 for (i = 0; i <= first_index; ++i)
554 if (viodasd_devices[i].disk)
555 blk_run_queue(viodasd_devices[i].disk->queue);
556}
557
558/*
559 * For read and write requests, decrement the number of outstanding requests,
560 * Free the DMA buffers we allocated.
561 */
562static int viodasd_handle_read_write(struct vioblocklpevent *bevent)
563{
564 int num_sg, num_sect, pci_direction, total_len;
565 struct request *req;
566 struct scatterlist sg[VIOMAXBLOCKDMA];
567 struct HvLpEvent *event = &bevent->event;
568 unsigned long irq_flags;
569 struct viodasd_device *d;
570 int error;
571 spinlock_t *qlock;
572
573 num_sg = block_event_to_scatterlist(bevent, sg, &total_len);
574 num_sect = total_len >> 9;
575 if (event->xSubtype == (viomajorsubtype_blockio | vioblockread))
576 pci_direction = DMA_FROM_DEVICE;
577 else
578 pci_direction = DMA_TO_DEVICE;
579 req = (struct request *)bevent->event.xCorrelationToken;
580 d = req->rq_disk->private_data;
581
582 dma_unmap_sg(d->dev, sg, num_sg, pci_direction);
583
584 /*
585 * Since this is running in interrupt mode, we need to make sure
586 * we're not stepping on any global I/O operations
587 */
588 spin_lock_irqsave(&viodasd_spinlock, irq_flags);
589 num_req_outstanding--;
590 spin_unlock_irqrestore(&viodasd_spinlock, irq_flags);
591
592 error = event->xRc != HvLpEvent_Rc_Good;
593 if (error) {
594 const struct vio_error_entry *err;
595 err = vio_lookup_rc(viodasd_err_table, bevent->sub_result);
596 printk(VIOD_KERN_WARNING "read/write error %d:0x%04x (%s)\n",
597 event->xRc, bevent->sub_result, err->msg);
598 num_sect = req->hard_nr_sectors;
599 }
600 qlock = req->q->queue_lock;
601 spin_lock_irqsave(qlock, irq_flags);
602 viodasd_end_request(req, !error, num_sect);
603 spin_unlock_irqrestore(qlock, irq_flags);
604
605 /* Finally, try to get more requests off of this device's queue */
606 viodasd_restart_all_queues_starting_from(DEVICE_NO(d));
607
608 return 0;
609}
610
611/* This routine handles incoming block LP events */
612static void handle_block_event(struct HvLpEvent *event)
613{
614 struct vioblocklpevent *bevent = (struct vioblocklpevent *)event;
615 struct viodasd_waitevent *pwe;
616
617 if (event == NULL)
618 /* Notification that a partition went away! */
619 return;
620 /* First, we should NEVER get an int here...only acks */
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100621 if (hvlpevent_is_int(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 printk(VIOD_KERN_WARNING
623 "Yikes! got an int in viodasd event handler!\n");
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100624 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 event->xRc = HvLpEvent_Rc_InvalidSubtype;
626 HvCallEvent_ackLpEvent(event);
627 }
628 }
629
630 switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
631 case vioblockopen:
632 /*
633 * Handle a response to an open request. We get all the
634 * disk information in the response, so update it. The
635 * correlation token contains a pointer to a waitevent
636 * structure that has a completion in it. update the
637 * return code in the waitevent structure and post the
638 * completion to wake up the guy who sent the request
639 */
640 pwe = (struct viodasd_waitevent *)event->xCorrelationToken;
641 pwe->rc = event->xRc;
642 pwe->sub_result = bevent->sub_result;
643 if (event->xRc == HvLpEvent_Rc_Good) {
644 const struct open_data *data = &bevent->u.open_data;
645 struct viodasd_device *device =
646 &viodasd_devices[bevent->disk];
647 device->read_only =
648 bevent->flags & vioblockflags_ro;
649 device->size = data->disk_size;
650 device->cylinders = data->cylinders;
651 device->tracks = data->tracks;
652 device->sectors = data->sectors;
653 device->bytes_per_sector = data->bytes_per_sector;
654 pwe->max_disk = data->max_disk;
655 }
656 complete(&pwe->com);
657 break;
658 case vioblockclose:
659 break;
660 case vioblockread:
661 case vioblockwrite:
662 viodasd_handle_read_write(bevent);
663 break;
664
665 default:
666 printk(VIOD_KERN_WARNING "invalid subtype!");
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100667 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 event->xRc = HvLpEvent_Rc_InvalidSubtype;
669 HvCallEvent_ackLpEvent(event);
670 }
671 }
672}
673
674/*
675 * Get the driver to reprobe for more disks.
676 */
677static ssize_t probe_disks(struct device_driver *drv, const char *buf,
678 size_t count)
679{
680 struct viodasd_device *d;
681
682 for (d = viodasd_devices; d < &viodasd_devices[MAX_DISKNO]; d++) {
683 if (d->disk == NULL)
684 probe_disk(d);
685 }
686 return count;
687}
688static DRIVER_ATTR(probe, S_IWUSR, NULL, probe_disks);
689
690static int viodasd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
691{
692 struct viodasd_device *d = &viodasd_devices[vdev->unit_address];
693
694 d->dev = &vdev->dev;
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000695 if (!probe_disk(d))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return -ENODEV;
697 return 0;
698}
699
700static int viodasd_remove(struct vio_dev *vdev)
701{
702 struct viodasd_device *d;
703
704 d = &viodasd_devices[vdev->unit_address];
705 if (d->disk) {
706 del_gendisk(d->disk);
707 blk_cleanup_queue(d->disk->queue);
708 put_disk(d->disk);
709 d->disk = NULL;
710 }
711 d->dev = NULL;
712 return 0;
713}
714
715/**
716 * viodasd_device_table: Used by vio.c to match devices that we
717 * support.
718 */
719static struct vio_device_id viodasd_device_table[] __devinitdata = {
Stephen Rothwellfbabeb62006-05-15 13:41:22 +1000720 { "block", "IBM,iSeries-viodasd" },
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000721 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723MODULE_DEVICE_TABLE(vio, viodasd_device_table);
Stephen Rothwell915124d2005-10-24 15:12:22 +1000724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725static struct vio_driver viodasd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 .id_table = viodasd_device_table,
727 .probe = viodasd_probe,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000728 .remove = viodasd_remove,
729 .driver = {
730 .name = "viodasd",
Stephen Rothwell915124d2005-10-24 15:12:22 +1000731 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733};
734
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100735static int need_delete_probe;
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737/*
738 * Initialize the whole device driver. Handle module and non-module
739 * versions
740 */
741static int __init viodasd_init(void)
742{
743 int rc;
744
Stephen Rothwellfb8b5002006-12-15 15:40:08 +1100745 if (!firmware_has_feature(FW_FEATURE_ISERIES)) {
746 rc = -ENODEV;
747 goto early_fail;
748 }
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* Try to open to our host lp */
751 if (viopath_hostLp == HvLpIndexInvalid)
752 vio_set_hostlp();
753
754 if (viopath_hostLp == HvLpIndexInvalid) {
755 printk(VIOD_KERN_WARNING "invalid hosting partition\n");
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100756 rc = -EIO;
757 goto early_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759
760 printk(VIOD_KERN_INFO "vers " VIOD_VERS ", hosting partition %d\n",
761 viopath_hostLp);
762
763 /* register the block device */
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100764 rc = register_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
765 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 printk(VIOD_KERN_WARNING
767 "Unable to get major number %d for %s\n",
768 VIODASD_MAJOR, VIOD_GENHD_NAME);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100769 goto early_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771 /* Actually open the path to the hosting partition */
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100772 rc = viopath_open(viopath_hostLp, viomajorsubtype_blockio,
773 VIOMAXREQ + 2);
774 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 printk(VIOD_KERN_WARNING
776 "error opening path to host partition %d\n",
777 viopath_hostLp);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100778 goto unregister_blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
781 /* Initialize our request handler */
782 vio_setHandler(viomajorsubtype_blockio, handle_block_event);
783
784 rc = vio_register_driver(&viodasd_driver);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100785 if (rc) {
786 printk(VIOD_KERN_WARNING "vio_register_driver failed\n");
787 goto unset_handler;
788 }
789
790 /*
791 * If this call fails, it just means that we cannot dynamically
792 * add virtual disks, but the driver will still work fine for
793 * all existing disk, so ignore the failure.
794 */
795 if (!driver_create_file(&viodasd_driver.driver, &driver_attr_probe))
796 need_delete_probe = 1;
797
798 return 0;
799
800unset_handler:
801 vio_clearHandler(viomajorsubtype_blockio);
802 viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
803unregister_blk:
804 unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
805early_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return rc;
807}
808module_init(viodasd_init);
809
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100810void __exit viodasd_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100812 if (need_delete_probe)
813 driver_remove_file(&viodasd_driver.driver, &driver_attr_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 vio_unregister_driver(&viodasd_driver);
815 vio_clearHandler(viomajorsubtype_blockio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100817 unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819module_exit(viodasd_exit);