blob: 1b3def1e8591d330bc3aa9b4730086b7fc6844c4 [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 */
Joe Perchesdc942ce2009-12-22 21:22:59 +000031
32#define pr_fmt(fmt) "viod: " fmt
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/major.h>
35#include <linux/fs.h>
36#include <linux/module.h>
37#include <linux/kernel.h>
38#include <linux/blkdev.h>
39#include <linux/genhd.h>
40#include <linux/hdreg.h>
41#include <linux/errno.h>
42#include <linux/init.h>
43#include <linux/string.h>
44#include <linux/dma-mapping.h>
45#include <linux/completion.h>
46#include <linux/device.h>
Jens Axboe45711f12007-10-22 21:19:53 +020047#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <asm/uaccess.h>
50#include <asm/vio.h>
Kelly Daly1ec65d72005-11-02 13:46:07 +110051#include <asm/iseries/hv_types.h>
Kelly Dalye45423e2005-11-02 12:08:31 +110052#include <asm/iseries/hv_lp_event.h>
Kelly Daly15b17182005-11-02 11:55:28 +110053#include <asm/iseries/hv_lp_config.h>
Kelly Dalyb4206772005-11-02 15:13:57 +110054#include <asm/iseries/vio.h>
Stephen Rothwellfb8b5002006-12-15 15:40:08 +110055#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57MODULE_DESCRIPTION("iSeries Virtual DASD");
58MODULE_AUTHOR("Dave Boutcher");
59MODULE_LICENSE("GPL");
60
61/*
62 * We only support 7 partitions per physical disk....so with minor
63 * numbers 0-255 we get a maximum of 32 disks.
64 */
65#define VIOD_GENHD_NAME "iseries/vd"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67#define VIOD_VERS "1.64"
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069enum {
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) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000159 pr_warning("HV open failed %d\n", (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 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
Joe Perchesdc942ce2009-12-22 21:22:59 +0000170 pr_warning("bad rc opening disk: %d:0x%04x (%s)\n",
171 (int)we.rc, we.sub_result, err->msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return -EIO;
173 }
174
175 return 0;
176}
177
178/*
179 * External release entry point.
180 */
Al Virof115a142008-03-02 10:22:07 -0500181static int viodasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Al Virof115a142008-03-02 10:22:07 -0500183 struct viodasd_device *d = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 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)
Joe Perchesdc942ce2009-12-22 21:22:59 +0000197 pr_warning("HV close call failed %d\n", (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return 0;
199}
200
201
202/* External ioctl entry point.
203 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800204static int viodasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800206 struct gendisk *disk = bdev->bd_disk;
207 struct viodasd_device *d = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Stephen Rothwell36a70032007-05-29 16:06:54 +1000209 geo->sectors = d->sectors ? d->sectors : 32;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800210 geo->heads = d->tracks ? d->tracks : 64;
211 geo->cylinders = d->cylinders ? d->cylinders :
Stephen Rothwell36a70032007-05-29 16:06:54 +1000212 get_capacity(disk) / (geo->sectors * geo->heads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/*
218 * Our file operations table
219 */
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700220static const struct block_device_operations viodasd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 .owner = THIS_MODULE,
Al Virof115a142008-03-02 10:22:07 -0500222 .open = viodasd_open,
223 .release = viodasd_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800224 .getgeo = viodasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225};
226
227/*
228 * End a request
229 */
Kiyoshi Uedab2aec242007-12-11 17:47:14 -0500230static void viodasd_end_request(struct request *req, int error,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 int num_sectors)
232{
Kiyoshi Uedab2aec242007-12-11 17:47:14 -0500233 __blk_end_request(req, error, num_sectors << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236/*
237 * Send an actual I/O request to OS/400
238 */
239static int send_request(struct request *req)
240{
241 u64 start;
242 int direction;
243 int nsg;
244 u16 viocmd;
245 HvLpEvent_Rc hvrc;
246 struct vioblocklpevent *bevent;
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100247 struct HvLpEvent *hev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 struct scatterlist sg[VIOMAXBLOCKDMA];
249 int sgindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 struct viodasd_device *d;
251 unsigned long flags;
252
Tejun Heo83096eb2009-05-07 22:24:39 +0900253 start = (u64)blk_rq_pos(req) << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 if (rq_data_dir(req) == READ) {
256 direction = DMA_FROM_DEVICE;
257 viocmd = viomajorsubtype_blockio | vioblockread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 } else {
259 direction = DMA_TO_DEVICE;
260 viocmd = viomajorsubtype_blockio | vioblockwrite;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262
263 d = req->rq_disk->private_data;
264
265 /* Now build the scatter-gather list */
Jens Axboe45711f12007-10-22 21:19:53 +0200266 sg_init_table(sg, VIOMAXBLOCKDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 nsg = blk_rq_map_sg(req->q, req, sg);
268 nsg = dma_map_sg(d->dev, sg, nsg, direction);
269
270 spin_lock_irqsave(&viodasd_spinlock, flags);
271 num_req_outstanding++;
272
273 /* This optimization handles a single DMA block */
274 if (nsg == 1)
275 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
276 HvLpEvent_Type_VirtualIo, viocmd,
277 HvLpEvent_AckInd_DoAck,
278 HvLpEvent_AckType_ImmediateAck,
279 viopath_sourceinst(viopath_hostLp),
280 viopath_targetinst(viopath_hostLp),
281 (u64)(unsigned long)req, VIOVERSION << 16,
282 ((u64)DEVICE_NO(d) << 48), start,
283 ((u64)sg_dma_address(&sg[0])) << 32,
284 sg_dma_len(&sg[0]));
285 else {
286 bevent = (struct vioblocklpevent *)
287 vio_get_event_buffer(viomajorsubtype_blockio);
288 if (bevent == NULL) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000289 pr_warning("error allocating disk event buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 goto error_ret;
291 }
292
293 /*
294 * Now build up the actual request. Note that we store
295 * the pointer to the request in the correlation
296 * token so we can match the response up later
297 */
298 memset(bevent, 0, sizeof(struct vioblocklpevent));
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100299 hev = &bevent->event;
300 hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DO_ACK |
301 HV_LP_EVENT_INT;
302 hev->xType = HvLpEvent_Type_VirtualIo;
303 hev->xSubtype = viocmd;
304 hev->xSourceLp = HvLpConfig_getLpIndex();
305 hev->xTargetLp = viopath_hostLp;
306 hev->xSizeMinus1 =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 offsetof(struct vioblocklpevent, u.rw_data.dma_info) +
308 (sizeof(bevent->u.rw_data.dma_info[0]) * nsg) - 1;
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100309 hev->xSourceInstanceId = viopath_sourceinst(viopath_hostLp);
310 hev->xTargetInstanceId = viopath_targetinst(viopath_hostLp);
311 hev->xCorrelationToken = (u64)req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 bevent->version = VIOVERSION;
313 bevent->disk = DEVICE_NO(d);
314 bevent->u.rw_data.offset = start;
315
316 /*
317 * Copy just the dma information from the sg list
318 * into the request
319 */
320 for (sgindex = 0; sgindex < nsg; sgindex++) {
321 bevent->u.rw_data.dma_info[sgindex].token =
322 sg_dma_address(&sg[sgindex]);
323 bevent->u.rw_data.dma_info[sgindex].len =
324 sg_dma_len(&sg[sgindex]);
325 }
326
327 /* Send the request */
328 hvrc = HvCallEvent_signalLpEvent(&bevent->event);
329 vio_free_event_buffer(viomajorsubtype_blockio, bevent);
330 }
331
332 if (hvrc != HvLpEvent_Rc_Good) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000333 pr_warning("error sending disk event to OS/400 (rc %d)\n",
334 (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 goto error_ret;
336 }
337 spin_unlock_irqrestore(&viodasd_spinlock, flags);
338 return 0;
339
340error_ret:
341 num_req_outstanding--;
342 spin_unlock_irqrestore(&viodasd_spinlock, flags);
343 dma_unmap_sg(d->dev, sg, nsg, direction);
344 return -1;
345}
346
347/*
348 * This is the external request processing routine
349 */
Jens Axboe165125e2007-07-24 09:28:11 +0200350static void do_viodasd_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 struct request *req;
353
354 /*
355 * If we already have the maximum number of requests
356 * outstanding to OS/400 just bail out. We'll come
357 * back later.
358 */
359 while (num_req_outstanding < VIOMAXREQ) {
Tejun Heo9934c8c2009-05-08 11:54:16 +0900360 req = blk_fetch_request(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (req == NULL)
362 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 /* check that request contains a valid command */
364 if (!blk_fs_request(req)) {
Tejun Heo5b936292009-05-07 22:24:38 +0900365 viodasd_end_request(req, -EIO, blk_rq_sectors(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 continue;
367 }
368 /* Try sending the request */
369 if (send_request(req) != 0)
Tejun Heo5b936292009-05-07 22:24:38 +0900370 viodasd_end_request(req, -EIO, blk_rq_sectors(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372}
373
374/*
375 * Probe a single disk and fill in the viodasd_device structure
376 * for it.
377 */
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000378static int probe_disk(struct viodasd_device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 HvLpEvent_Rc hvrc;
381 struct viodasd_waitevent we;
382 int dev_no = DEVICE_NO(d);
383 struct gendisk *g;
384 struct request_queue *q;
385 u16 flags = 0;
386
387retry:
388 init_completion(&we.com);
389
390 /* Send the open event to OS/400 */
391 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
392 HvLpEvent_Type_VirtualIo,
393 viomajorsubtype_blockio | vioblockopen,
394 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
395 viopath_sourceinst(viopath_hostLp),
396 viopath_targetinst(viopath_hostLp),
397 (u64)(unsigned long)&we, VIOVERSION << 16,
398 ((u64)dev_no << 48) | ((u64)flags<< 32),
399 0, 0, 0);
400 if (hvrc != 0) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000401 pr_warning("bad rc on HV open %d\n", (int)hvrc);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404
405 wait_for_completion(&we.com);
406
407 if (we.rc != 0) {
408 if (flags != 0)
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* try again with read only flag set */
411 flags = vioblockflags_ro;
412 goto retry;
413 }
414 if (we.max_disk > (MAX_DISKNO - 1)) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000415 printk_once(KERN_INFO pr_fmt("Only examining the first %d of %d disks connected\n"),
416 MAX_DISKNO, we.max_disk + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
419 /* Send the close event to OS/400. We DON'T expect a response */
420 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
421 HvLpEvent_Type_VirtualIo,
422 viomajorsubtype_blockio | vioblockclose,
423 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
424 viopath_sourceinst(viopath_hostLp),
425 viopath_targetinst(viopath_hostLp),
426 0, VIOVERSION << 16,
427 ((u64)dev_no << 48) | ((u64)flags << 32),
428 0, 0, 0);
429 if (hvrc != 0) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000430 pr_warning("bad rc sending event to OS/400 %d\n", (int)hvrc);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000431 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000433
434 if (d->dev == NULL) {
435 /* this is when we reprobe for new disks */
436 if (vio_create_viodasd(dev_no) == NULL) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000437 pr_warning("cannot allocate virtual device for disk %d\n",
438 dev_no);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000439 return 0;
440 }
441 /*
442 * The vio_create_viodasd will have recursed into this
443 * routine with d->dev set to the new vio device and
444 * will finish the setup of the disk below.
445 */
446 return 1;
447 }
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /* create the request queue for the disk */
450 spin_lock_init(&d->q_lock);
451 q = blk_init_queue(do_viodasd_request, &d->q_lock);
452 if (q == NULL) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000453 pr_warning("cannot allocate queue for disk %d\n", dev_no);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456 g = alloc_disk(1 << PARTITION_SHIFT);
457 if (g == NULL) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000458 pr_warning("cannot allocate disk structure for disk %d\n",
459 dev_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 blk_cleanup_queue(q);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000461 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463
464 d->disk = g;
465 blk_queue_max_hw_segments(q, VIOMAXBLOCKDMA);
466 blk_queue_max_phys_segments(q, VIOMAXBLOCKDMA);
467 blk_queue_max_sectors(q, VIODASD_MAXSECTORS);
468 g->major = VIODASD_MAJOR;
469 g->first_minor = dev_no << PARTITION_SHIFT;
470 if (dev_no >= 26)
471 snprintf(g->disk_name, sizeof(g->disk_name),
472 VIOD_GENHD_NAME "%c%c",
473 'a' + (dev_no / 26) - 1, 'a' + (dev_no % 26));
474 else
475 snprintf(g->disk_name, sizeof(g->disk_name),
476 VIOD_GENHD_NAME "%c", 'a' + (dev_no % 26));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 g->fops = &viodasd_fops;
478 g->queue = q;
479 g->private_data = d;
480 g->driverfs_dev = d->dev;
481 set_capacity(g, d->size >> 9);
482
Joe Perchesdc942ce2009-12-22 21:22:59 +0000483 pr_info("disk %d: %lu sectors (%lu MB) CHS=%d/%d/%d sector size %d%s\n",
484 dev_no, (unsigned long)(d->size >> 9),
485 (unsigned long)(d->size >> 20),
486 (int)d->cylinders, (int)d->tracks,
487 (int)d->sectors, (int)d->bytes_per_sector,
488 d->read_only ? " (RO)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 /* register us in the global list */
491 add_disk(g);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000492 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
495/* returns the total number of scatterlist elements converted */
496static int block_event_to_scatterlist(const struct vioblocklpevent *bevent,
497 struct scatterlist *sg, int *total_len)
498{
499 int i, numsg;
500 const struct rw_data *rw_data = &bevent->u.rw_data;
501 static const int offset =
502 offsetof(struct vioblocklpevent, u.rw_data.dma_info);
503 static const int element_size = sizeof(rw_data->dma_info[0]);
504
505 numsg = ((bevent->event.xSizeMinus1 + 1) - offset) / element_size;
506 if (numsg > VIOMAXBLOCKDMA)
507 numsg = VIOMAXBLOCKDMA;
508
509 *total_len = 0;
Benjamin Herrenschmidt25c0a7b2008-03-12 17:23:56 +1100510 sg_init_table(sg, VIOMAXBLOCKDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 for (i = 0; (i < numsg) && (rw_data->dma_info[i].len > 0); ++i) {
512 sg_dma_address(&sg[i]) = rw_data->dma_info[i].token;
513 sg_dma_len(&sg[i]) = rw_data->dma_info[i].len;
514 *total_len += rw_data->dma_info[i].len;
515 }
516 return i;
517}
518
519/*
520 * Restart all queues, starting with the one _after_ the disk given,
521 * thus reducing the chance of starvation of higher numbered disks.
522 */
523static void viodasd_restart_all_queues_starting_from(int first_index)
524{
525 int i;
526
527 for (i = first_index + 1; i < MAX_DISKNO; ++i)
528 if (viodasd_devices[i].disk)
529 blk_run_queue(viodasd_devices[i].disk->queue);
530 for (i = 0; i <= first_index; ++i)
531 if (viodasd_devices[i].disk)
532 blk_run_queue(viodasd_devices[i].disk->queue);
533}
534
535/*
536 * For read and write requests, decrement the number of outstanding requests,
537 * Free the DMA buffers we allocated.
538 */
539static int viodasd_handle_read_write(struct vioblocklpevent *bevent)
540{
541 int num_sg, num_sect, pci_direction, total_len;
542 struct request *req;
543 struct scatterlist sg[VIOMAXBLOCKDMA];
544 struct HvLpEvent *event = &bevent->event;
545 unsigned long irq_flags;
546 struct viodasd_device *d;
547 int error;
548 spinlock_t *qlock;
549
550 num_sg = block_event_to_scatterlist(bevent, sg, &total_len);
551 num_sect = total_len >> 9;
552 if (event->xSubtype == (viomajorsubtype_blockio | vioblockread))
553 pci_direction = DMA_FROM_DEVICE;
554 else
555 pci_direction = DMA_TO_DEVICE;
556 req = (struct request *)bevent->event.xCorrelationToken;
557 d = req->rq_disk->private_data;
558
559 dma_unmap_sg(d->dev, sg, num_sg, pci_direction);
560
561 /*
562 * Since this is running in interrupt mode, we need to make sure
563 * we're not stepping on any global I/O operations
564 */
565 spin_lock_irqsave(&viodasd_spinlock, irq_flags);
566 num_req_outstanding--;
567 spin_unlock_irqrestore(&viodasd_spinlock, irq_flags);
568
Kiyoshi Uedab2aec242007-12-11 17:47:14 -0500569 error = (event->xRc == HvLpEvent_Rc_Good) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (error) {
571 const struct vio_error_entry *err;
572 err = vio_lookup_rc(viodasd_err_table, bevent->sub_result);
Joe Perchesdc942ce2009-12-22 21:22:59 +0000573 pr_warning("read/write error %d:0x%04x (%s)\n",
574 event->xRc, bevent->sub_result, err->msg);
Tejun Heo5b936292009-05-07 22:24:38 +0900575 num_sect = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577 qlock = req->q->queue_lock;
578 spin_lock_irqsave(qlock, irq_flags);
Kiyoshi Uedab2aec242007-12-11 17:47:14 -0500579 viodasd_end_request(req, error, num_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 spin_unlock_irqrestore(qlock, irq_flags);
581
582 /* Finally, try to get more requests off of this device's queue */
583 viodasd_restart_all_queues_starting_from(DEVICE_NO(d));
584
585 return 0;
586}
587
588/* This routine handles incoming block LP events */
589static void handle_block_event(struct HvLpEvent *event)
590{
591 struct vioblocklpevent *bevent = (struct vioblocklpevent *)event;
592 struct viodasd_waitevent *pwe;
593
594 if (event == NULL)
595 /* Notification that a partition went away! */
596 return;
597 /* First, we should NEVER get an int here...only acks */
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100598 if (hvlpevent_is_int(event)) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000599 pr_warning("Yikes! got an int in viodasd event handler!\n");
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100600 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 event->xRc = HvLpEvent_Rc_InvalidSubtype;
602 HvCallEvent_ackLpEvent(event);
603 }
604 }
605
606 switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
607 case vioblockopen:
608 /*
609 * Handle a response to an open request. We get all the
610 * disk information in the response, so update it. The
611 * correlation token contains a pointer to a waitevent
612 * structure that has a completion in it. update the
613 * return code in the waitevent structure and post the
614 * completion to wake up the guy who sent the request
615 */
616 pwe = (struct viodasd_waitevent *)event->xCorrelationToken;
617 pwe->rc = event->xRc;
618 pwe->sub_result = bevent->sub_result;
619 if (event->xRc == HvLpEvent_Rc_Good) {
620 const struct open_data *data = &bevent->u.open_data;
621 struct viodasd_device *device =
622 &viodasd_devices[bevent->disk];
623 device->read_only =
624 bevent->flags & vioblockflags_ro;
625 device->size = data->disk_size;
626 device->cylinders = data->cylinders;
627 device->tracks = data->tracks;
628 device->sectors = data->sectors;
629 device->bytes_per_sector = data->bytes_per_sector;
630 pwe->max_disk = data->max_disk;
631 }
632 complete(&pwe->com);
633 break;
634 case vioblockclose:
635 break;
636 case vioblockread:
637 case vioblockwrite:
638 viodasd_handle_read_write(bevent);
639 break;
640
641 default:
Joe Perchesdc942ce2009-12-22 21:22:59 +0000642 pr_warning("invalid subtype!");
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100643 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 event->xRc = HvLpEvent_Rc_InvalidSubtype;
645 HvCallEvent_ackLpEvent(event);
646 }
647 }
648}
649
650/*
651 * Get the driver to reprobe for more disks.
652 */
653static ssize_t probe_disks(struct device_driver *drv, const char *buf,
654 size_t count)
655{
656 struct viodasd_device *d;
657
658 for (d = viodasd_devices; d < &viodasd_devices[MAX_DISKNO]; d++) {
659 if (d->disk == NULL)
660 probe_disk(d);
661 }
662 return count;
663}
664static DRIVER_ATTR(probe, S_IWUSR, NULL, probe_disks);
665
666static int viodasd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
667{
668 struct viodasd_device *d = &viodasd_devices[vdev->unit_address];
669
670 d->dev = &vdev->dev;
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000671 if (!probe_disk(d))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return -ENODEV;
673 return 0;
674}
675
676static int viodasd_remove(struct vio_dev *vdev)
677{
678 struct viodasd_device *d;
679
680 d = &viodasd_devices[vdev->unit_address];
681 if (d->disk) {
682 del_gendisk(d->disk);
683 blk_cleanup_queue(d->disk->queue);
684 put_disk(d->disk);
685 d->disk = NULL;
686 }
687 d->dev = NULL;
688 return 0;
689}
690
691/**
692 * viodasd_device_table: Used by vio.c to match devices that we
693 * support.
694 */
695static struct vio_device_id viodasd_device_table[] __devinitdata = {
Stephen Rothwellfbabeb62006-05-15 13:41:22 +1000696 { "block", "IBM,iSeries-viodasd" },
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000697 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699MODULE_DEVICE_TABLE(vio, viodasd_device_table);
Stephen Rothwell915124d2005-10-24 15:12:22 +1000700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701static struct vio_driver viodasd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 .id_table = viodasd_device_table,
703 .probe = viodasd_probe,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000704 .remove = viodasd_remove,
705 .driver = {
706 .name = "viodasd",
Stephen Rothwell915124d2005-10-24 15:12:22 +1000707 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709};
710
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100711static int need_delete_probe;
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713/*
714 * Initialize the whole device driver. Handle module and non-module
715 * versions
716 */
717static int __init viodasd_init(void)
718{
719 int rc;
720
Stephen Rothwellfb8b5002006-12-15 15:40:08 +1100721 if (!firmware_has_feature(FW_FEATURE_ISERIES)) {
722 rc = -ENODEV;
723 goto early_fail;
724 }
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* Try to open to our host lp */
727 if (viopath_hostLp == HvLpIndexInvalid)
728 vio_set_hostlp();
729
730 if (viopath_hostLp == HvLpIndexInvalid) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000731 pr_warning("invalid hosting partition\n");
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100732 rc = -EIO;
733 goto early_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735
Joe Perchesdc942ce2009-12-22 21:22:59 +0000736 pr_info("vers " VIOD_VERS ", hosting partition %d\n", viopath_hostLp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 /* register the block device */
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100739 rc = register_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
740 if (rc) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000741 pr_warning("Unable to get major number %d for %s\n",
742 VIODASD_MAJOR, VIOD_GENHD_NAME);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100743 goto early_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745 /* Actually open the path to the hosting partition */
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100746 rc = viopath_open(viopath_hostLp, viomajorsubtype_blockio,
747 VIOMAXREQ + 2);
748 if (rc) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000749 pr_warning("error opening path to host partition %d\n",
750 viopath_hostLp);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100751 goto unregister_blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
754 /* Initialize our request handler */
755 vio_setHandler(viomajorsubtype_blockio, handle_block_event);
756
757 rc = vio_register_driver(&viodasd_driver);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100758 if (rc) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000759 pr_warning("vio_register_driver failed\n");
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100760 goto unset_handler;
761 }
762
763 /*
764 * If this call fails, it just means that we cannot dynamically
765 * add virtual disks, but the driver will still work fine for
766 * all existing disk, so ignore the failure.
767 */
768 if (!driver_create_file(&viodasd_driver.driver, &driver_attr_probe))
769 need_delete_probe = 1;
770
771 return 0;
772
773unset_handler:
774 vio_clearHandler(viomajorsubtype_blockio);
775 viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
776unregister_blk:
777 unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
778early_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return rc;
780}
781module_init(viodasd_init);
782
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100783void __exit viodasd_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100785 if (need_delete_probe)
786 driver_remove_file(&viodasd_driver.driver, &driver_attr_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 vio_unregister_driver(&viodasd_driver);
788 vio_clearHandler(viomajorsubtype_blockio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100790 unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792module_exit(viodasd_exit);