blob: e2ff697697c27c7a26d7f66d24bafd94f10e1031 [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>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020044#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/dma-mapping.h>
46#include <linux/completion.h>
47#include <linux/device.h>
Jens Axboe45711f12007-10-22 21:19:53 +020048#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <asm/uaccess.h>
51#include <asm/vio.h>
Kelly Daly1ec65d72005-11-02 13:46:07 +110052#include <asm/iseries/hv_types.h>
Kelly Dalye45423e2005-11-02 12:08:31 +110053#include <asm/iseries/hv_lp_event.h>
Kelly Daly15b17182005-11-02 11:55:28 +110054#include <asm/iseries/hv_lp_config.h>
Kelly Dalyb4206772005-11-02 15:13:57 +110055#include <asm/iseries/vio.h>
Stephen Rothwellfb8b5002006-12-15 15:40:08 +110056#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58MODULE_DESCRIPTION("iSeries Virtual DASD");
59MODULE_AUTHOR("Dave Boutcher");
60MODULE_LICENSE("GPL");
61
62/*
63 * We only support 7 partitions per physical disk....so with minor
64 * numbers 0-255 we get a maximum of 32 disks.
65 */
66#define VIOD_GENHD_NAME "iseries/vd"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define VIOD_VERS "1.64"
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070enum {
71 PARTITION_SHIFT = 3,
72 MAX_DISKNO = HVMAXARCHITECTEDVIRTUALDISKS,
Julia Lawallea6728c2008-03-29 08:21:08 +110073 MAX_DISK_NAME = FIELD_SIZEOF(struct gendisk, disk_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020076static DEFINE_MUTEX(viodasd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static DEFINE_SPINLOCK(viodasd_spinlock);
78
79#define VIOMAXREQ 16
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81#define DEVICE_NO(cell) ((struct viodasd_device *)(cell) - &viodasd_devices[0])
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083struct viodasd_waitevent {
84 struct completion com;
85 int rc;
86 u16 sub_result;
87 int max_disk; /* open */
88};
89
90static const struct vio_error_entry viodasd_err_table[] = {
91 { 0x0201, EINVAL, "Invalid Range" },
92 { 0x0202, EINVAL, "Invalid Token" },
93 { 0x0203, EIO, "DMA Error" },
94 { 0x0204, EIO, "Use Error" },
95 { 0x0205, EIO, "Release Error" },
96 { 0x0206, EINVAL, "Invalid Disk" },
97 { 0x0207, EBUSY, "Cant Lock" },
98 { 0x0208, EIO, "Already Locked" },
99 { 0x0209, EIO, "Already Unlocked" },
100 { 0x020A, EIO, "Invalid Arg" },
101 { 0x020B, EIO, "Bad IFS File" },
102 { 0x020C, EROFS, "Read Only Device" },
103 { 0x02FF, EIO, "Internal Error" },
104 { 0x0000, 0, NULL },
105};
106
107/*
108 * Figure out the biggest I/O request (in sectors) we can accept
109 */
110#define VIODASD_MAXSECTORS (4096 / 512 * VIOMAXBLOCKDMA)
111
112/*
113 * Number of disk I/O requests we've sent to OS/400
114 */
115static int num_req_outstanding;
116
117/*
118 * This is our internal structure for keeping track of disk devices
119 */
120struct viodasd_device {
121 u16 cylinders;
122 u16 tracks;
123 u16 sectors;
124 u16 bytes_per_sector;
125 u64 size;
126 int read_only;
127 spinlock_t q_lock;
128 struct gendisk *disk;
129 struct device *dev;
130} viodasd_devices[MAX_DISKNO];
131
132/*
133 * External open entry point.
134 */
Al Virof115a142008-03-02 10:22:07 -0500135static int viodasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Al Virof115a142008-03-02 10:22:07 -0500137 struct viodasd_device *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 HvLpEvent_Rc hvrc;
139 struct viodasd_waitevent we;
140 u16 flags = 0;
141
142 if (d->read_only) {
Al Virof115a142008-03-02 10:22:07 -0500143 if (mode & FMODE_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return -EROFS;
145 flags = vioblockflags_ro;
146 }
147
148 init_completion(&we.com);
149
150 /* Send the open event to OS/400 */
151 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
152 HvLpEvent_Type_VirtualIo,
153 viomajorsubtype_blockio | vioblockopen,
154 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
155 viopath_sourceinst(viopath_hostLp),
156 viopath_targetinst(viopath_hostLp),
157 (u64)(unsigned long)&we, VIOVERSION << 16,
158 ((u64)DEVICE_NO(d) << 48) | ((u64)flags << 32),
159 0, 0, 0);
160 if (hvrc != 0) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000161 pr_warning("HV open failed %d\n", (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return -EIO;
163 }
164
165 wait_for_completion(&we.com);
166
167 /* Check the return code */
168 if (we.rc != 0) {
169 const struct vio_error_entry *err =
170 vio_lookup_rc(viodasd_err_table, we.sub_result);
171
Joe Perchesdc942ce2009-12-22 21:22:59 +0000172 pr_warning("bad rc opening disk: %d:0x%04x (%s)\n",
173 (int)we.rc, we.sub_result, err->msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return -EIO;
175 }
176
177 return 0;
178}
179
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200180static int viodasd_unlocked_open(struct block_device *bdev, fmode_t mode)
181{
182 int ret;
183
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200184 mutex_lock(&viodasd_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200185 ret = viodasd_open(bdev, mode);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200186 mutex_unlock(&viodasd_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200187
188 return ret;
189}
190
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
193 * External release entry point.
194 */
Al Virof115a142008-03-02 10:22:07 -0500195static int viodasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Al Virof115a142008-03-02 10:22:07 -0500197 struct viodasd_device *d = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 HvLpEvent_Rc hvrc;
199
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200200 mutex_lock(&viodasd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* Send the event to OS/400. We DON'T expect a response */
202 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
203 HvLpEvent_Type_VirtualIo,
204 viomajorsubtype_blockio | vioblockclose,
205 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
206 viopath_sourceinst(viopath_hostLp),
207 viopath_targetinst(viopath_hostLp),
208 0, VIOVERSION << 16,
209 ((u64)DEVICE_NO(d) << 48) /* | ((u64)flags << 32) */,
210 0, 0, 0);
211 if (hvrc != 0)
Joe Perchesdc942ce2009-12-22 21:22:59 +0000212 pr_warning("HV close call failed %d\n", (int)hvrc);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200213
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200214 mutex_unlock(&viodasd_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return 0;
217}
218
219
220/* External ioctl entry point.
221 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800222static int viodasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800224 struct gendisk *disk = bdev->bd_disk;
225 struct viodasd_device *d = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Stephen Rothwell36a70032007-05-29 16:06:54 +1000227 geo->sectors = d->sectors ? d->sectors : 32;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800228 geo->heads = d->tracks ? d->tracks : 64;
229 geo->cylinders = d->cylinders ? d->cylinders :
Stephen Rothwell36a70032007-05-29 16:06:54 +1000230 get_capacity(disk) / (geo->sectors * geo->heads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235/*
236 * Our file operations table
237 */
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700238static const struct block_device_operations viodasd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 .owner = THIS_MODULE,
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200240 .open = viodasd_unlocked_open,
Al Virof115a142008-03-02 10:22:07 -0500241 .release = viodasd_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800242 .getgeo = viodasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243};
244
245/*
246 * End a request
247 */
Kiyoshi Uedab2aec242007-12-11 17:47:14 -0500248static void viodasd_end_request(struct request *req, int error,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 int num_sectors)
250{
Kiyoshi Uedab2aec242007-12-11 17:47:14 -0500251 __blk_end_request(req, error, num_sectors << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254/*
255 * Send an actual I/O request to OS/400
256 */
257static int send_request(struct request *req)
258{
259 u64 start;
260 int direction;
261 int nsg;
262 u16 viocmd;
263 HvLpEvent_Rc hvrc;
264 struct vioblocklpevent *bevent;
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100265 struct HvLpEvent *hev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct scatterlist sg[VIOMAXBLOCKDMA];
267 int sgindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct viodasd_device *d;
269 unsigned long flags;
270
Tejun Heo83096eb2009-05-07 22:24:39 +0900271 start = (u64)blk_rq_pos(req) << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 if (rq_data_dir(req) == READ) {
274 direction = DMA_FROM_DEVICE;
275 viocmd = viomajorsubtype_blockio | vioblockread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 } else {
277 direction = DMA_TO_DEVICE;
278 viocmd = viomajorsubtype_blockio | vioblockwrite;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280
281 d = req->rq_disk->private_data;
282
283 /* Now build the scatter-gather list */
Jens Axboe45711f12007-10-22 21:19:53 +0200284 sg_init_table(sg, VIOMAXBLOCKDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 nsg = blk_rq_map_sg(req->q, req, sg);
286 nsg = dma_map_sg(d->dev, sg, nsg, direction);
287
288 spin_lock_irqsave(&viodasd_spinlock, flags);
289 num_req_outstanding++;
290
291 /* This optimization handles a single DMA block */
292 if (nsg == 1)
293 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
294 HvLpEvent_Type_VirtualIo, viocmd,
295 HvLpEvent_AckInd_DoAck,
296 HvLpEvent_AckType_ImmediateAck,
297 viopath_sourceinst(viopath_hostLp),
298 viopath_targetinst(viopath_hostLp),
299 (u64)(unsigned long)req, VIOVERSION << 16,
300 ((u64)DEVICE_NO(d) << 48), start,
301 ((u64)sg_dma_address(&sg[0])) << 32,
302 sg_dma_len(&sg[0]));
303 else {
304 bevent = (struct vioblocklpevent *)
305 vio_get_event_buffer(viomajorsubtype_blockio);
306 if (bevent == NULL) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000307 pr_warning("error allocating disk event buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 goto error_ret;
309 }
310
311 /*
312 * Now build up the actual request. Note that we store
313 * the pointer to the request in the correlation
314 * token so we can match the response up later
315 */
316 memset(bevent, 0, sizeof(struct vioblocklpevent));
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100317 hev = &bevent->event;
318 hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DO_ACK |
319 HV_LP_EVENT_INT;
320 hev->xType = HvLpEvent_Type_VirtualIo;
321 hev->xSubtype = viocmd;
322 hev->xSourceLp = HvLpConfig_getLpIndex();
323 hev->xTargetLp = viopath_hostLp;
324 hev->xSizeMinus1 =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 offsetof(struct vioblocklpevent, u.rw_data.dma_info) +
326 (sizeof(bevent->u.rw_data.dma_info[0]) * nsg) - 1;
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100327 hev->xSourceInstanceId = viopath_sourceinst(viopath_hostLp);
328 hev->xTargetInstanceId = viopath_targetinst(viopath_hostLp);
329 hev->xCorrelationToken = (u64)req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 bevent->version = VIOVERSION;
331 bevent->disk = DEVICE_NO(d);
332 bevent->u.rw_data.offset = start;
333
334 /*
335 * Copy just the dma information from the sg list
336 * into the request
337 */
338 for (sgindex = 0; sgindex < nsg; sgindex++) {
339 bevent->u.rw_data.dma_info[sgindex].token =
340 sg_dma_address(&sg[sgindex]);
341 bevent->u.rw_data.dma_info[sgindex].len =
342 sg_dma_len(&sg[sgindex]);
343 }
344
345 /* Send the request */
346 hvrc = HvCallEvent_signalLpEvent(&bevent->event);
347 vio_free_event_buffer(viomajorsubtype_blockio, bevent);
348 }
349
350 if (hvrc != HvLpEvent_Rc_Good) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000351 pr_warning("error sending disk event to OS/400 (rc %d)\n",
352 (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 goto error_ret;
354 }
355 spin_unlock_irqrestore(&viodasd_spinlock, flags);
356 return 0;
357
358error_ret:
359 num_req_outstanding--;
360 spin_unlock_irqrestore(&viodasd_spinlock, flags);
361 dma_unmap_sg(d->dev, sg, nsg, direction);
362 return -1;
363}
364
365/*
366 * This is the external request processing routine
367 */
Jens Axboe165125e2007-07-24 09:28:11 +0200368static void do_viodasd_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 struct request *req;
371
372 /*
373 * If we already have the maximum number of requests
374 * outstanding to OS/400 just bail out. We'll come
375 * back later.
376 */
377 while (num_req_outstanding < VIOMAXREQ) {
Tejun Heo9934c8c2009-05-08 11:54:16 +0900378 req = blk_fetch_request(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (req == NULL)
380 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /* check that request contains a valid command */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200382 if (req->cmd_type != REQ_TYPE_FS) {
Tejun Heo5b936292009-05-07 22:24:38 +0900383 viodasd_end_request(req, -EIO, blk_rq_sectors(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 continue;
385 }
386 /* Try sending the request */
387 if (send_request(req) != 0)
Tejun Heo5b936292009-05-07 22:24:38 +0900388 viodasd_end_request(req, -EIO, blk_rq_sectors(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390}
391
392/*
393 * Probe a single disk and fill in the viodasd_device structure
394 * for it.
395 */
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000396static int probe_disk(struct viodasd_device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 HvLpEvent_Rc hvrc;
399 struct viodasd_waitevent we;
400 int dev_no = DEVICE_NO(d);
401 struct gendisk *g;
402 struct request_queue *q;
403 u16 flags = 0;
404
405retry:
406 init_completion(&we.com);
407
408 /* Send the open event to OS/400 */
409 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
410 HvLpEvent_Type_VirtualIo,
411 viomajorsubtype_blockio | vioblockopen,
412 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
413 viopath_sourceinst(viopath_hostLp),
414 viopath_targetinst(viopath_hostLp),
415 (u64)(unsigned long)&we, VIOVERSION << 16,
416 ((u64)dev_no << 48) | ((u64)flags<< 32),
417 0, 0, 0);
418 if (hvrc != 0) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000419 pr_warning("bad rc on HV open %d\n", (int)hvrc);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
423 wait_for_completion(&we.com);
424
425 if (we.rc != 0) {
426 if (flags != 0)
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* try again with read only flag set */
429 flags = vioblockflags_ro;
430 goto retry;
431 }
432 if (we.max_disk > (MAX_DISKNO - 1)) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000433 printk_once(KERN_INFO pr_fmt("Only examining the first %d of %d disks connected\n"),
434 MAX_DISKNO, we.max_disk + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
437 /* Send the close event to OS/400. We DON'T expect a response */
438 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
439 HvLpEvent_Type_VirtualIo,
440 viomajorsubtype_blockio | vioblockclose,
441 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
442 viopath_sourceinst(viopath_hostLp),
443 viopath_targetinst(viopath_hostLp),
444 0, VIOVERSION << 16,
445 ((u64)dev_no << 48) | ((u64)flags << 32),
446 0, 0, 0);
447 if (hvrc != 0) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000448 pr_warning("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) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000455 pr_warning("cannot allocate virtual device for disk %d\n",
456 dev_no);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000457 return 0;
458 }
459 /*
460 * The vio_create_viodasd will have recursed into this
461 * routine with d->dev set to the new vio device and
462 * will finish the setup of the disk below.
463 */
464 return 1;
465 }
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /* create the request queue for the disk */
468 spin_lock_init(&d->q_lock);
469 q = blk_init_queue(do_viodasd_request, &d->q_lock);
470 if (q == NULL) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000471 pr_warning("cannot allocate queue for disk %d\n", dev_no);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474 g = alloc_disk(1 << PARTITION_SHIFT);
475 if (g == NULL) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000476 pr_warning("cannot allocate disk structure for disk %d\n",
477 dev_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 blk_cleanup_queue(q);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
482 d->disk = g;
Martin K. Petersen8a783622010-02-26 00:20:39 -0500483 blk_queue_max_segments(q, VIOMAXBLOCKDMA);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500484 blk_queue_max_hw_sectors(q, VIODASD_MAXSECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 g->major = VIODASD_MAJOR;
486 g->first_minor = dev_no << PARTITION_SHIFT;
487 if (dev_no >= 26)
488 snprintf(g->disk_name, sizeof(g->disk_name),
489 VIOD_GENHD_NAME "%c%c",
490 'a' + (dev_no / 26) - 1, 'a' + (dev_no % 26));
491 else
492 snprintf(g->disk_name, sizeof(g->disk_name),
493 VIOD_GENHD_NAME "%c", 'a' + (dev_no % 26));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 g->fops = &viodasd_fops;
495 g->queue = q;
496 g->private_data = d;
497 g->driverfs_dev = d->dev;
498 set_capacity(g, d->size >> 9);
499
Joe Perchesdc942ce2009-12-22 21:22:59 +0000500 pr_info("disk %d: %lu sectors (%lu MB) CHS=%d/%d/%d sector size %d%s\n",
501 dev_no, (unsigned long)(d->size >> 9),
502 (unsigned long)(d->size >> 20),
503 (int)d->cylinders, (int)d->tracks,
504 (int)d->sectors, (int)d->bytes_per_sector,
505 d->read_only ? " (RO)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 /* register us in the global list */
508 add_disk(g);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000509 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
512/* returns the total number of scatterlist elements converted */
513static int block_event_to_scatterlist(const struct vioblocklpevent *bevent,
514 struct scatterlist *sg, int *total_len)
515{
516 int i, numsg;
517 const struct rw_data *rw_data = &bevent->u.rw_data;
518 static const int offset =
519 offsetof(struct vioblocklpevent, u.rw_data.dma_info);
520 static const int element_size = sizeof(rw_data->dma_info[0]);
521
522 numsg = ((bevent->event.xSizeMinus1 + 1) - offset) / element_size;
523 if (numsg > VIOMAXBLOCKDMA)
524 numsg = VIOMAXBLOCKDMA;
525
526 *total_len = 0;
Benjamin Herrenschmidt25c0a7b2008-03-12 17:23:56 +1100527 sg_init_table(sg, VIOMAXBLOCKDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 for (i = 0; (i < numsg) && (rw_data->dma_info[i].len > 0); ++i) {
529 sg_dma_address(&sg[i]) = rw_data->dma_info[i].token;
530 sg_dma_len(&sg[i]) = rw_data->dma_info[i].len;
531 *total_len += rw_data->dma_info[i].len;
532 }
533 return i;
534}
535
536/*
537 * Restart all queues, starting with the one _after_ the disk given,
538 * thus reducing the chance of starvation of higher numbered disks.
539 */
540static void viodasd_restart_all_queues_starting_from(int first_index)
541{
542 int i;
543
544 for (i = first_index + 1; i < MAX_DISKNO; ++i)
545 if (viodasd_devices[i].disk)
546 blk_run_queue(viodasd_devices[i].disk->queue);
547 for (i = 0; i <= first_index; ++i)
548 if (viodasd_devices[i].disk)
549 blk_run_queue(viodasd_devices[i].disk->queue);
550}
551
552/*
553 * For read and write requests, decrement the number of outstanding requests,
554 * Free the DMA buffers we allocated.
555 */
556static int viodasd_handle_read_write(struct vioblocklpevent *bevent)
557{
558 int num_sg, num_sect, pci_direction, total_len;
559 struct request *req;
560 struct scatterlist sg[VIOMAXBLOCKDMA];
561 struct HvLpEvent *event = &bevent->event;
562 unsigned long irq_flags;
563 struct viodasd_device *d;
564 int error;
565 spinlock_t *qlock;
566
567 num_sg = block_event_to_scatterlist(bevent, sg, &total_len);
568 num_sect = total_len >> 9;
569 if (event->xSubtype == (viomajorsubtype_blockio | vioblockread))
570 pci_direction = DMA_FROM_DEVICE;
571 else
572 pci_direction = DMA_TO_DEVICE;
573 req = (struct request *)bevent->event.xCorrelationToken;
574 d = req->rq_disk->private_data;
575
576 dma_unmap_sg(d->dev, sg, num_sg, pci_direction);
577
578 /*
579 * Since this is running in interrupt mode, we need to make sure
580 * we're not stepping on any global I/O operations
581 */
582 spin_lock_irqsave(&viodasd_spinlock, irq_flags);
583 num_req_outstanding--;
584 spin_unlock_irqrestore(&viodasd_spinlock, irq_flags);
585
Kiyoshi Uedab2aec242007-12-11 17:47:14 -0500586 error = (event->xRc == HvLpEvent_Rc_Good) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (error) {
588 const struct vio_error_entry *err;
589 err = vio_lookup_rc(viodasd_err_table, bevent->sub_result);
Joe Perchesdc942ce2009-12-22 21:22:59 +0000590 pr_warning("read/write error %d:0x%04x (%s)\n",
591 event->xRc, bevent->sub_result, err->msg);
Tejun Heo5b936292009-05-07 22:24:38 +0900592 num_sect = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 qlock = req->q->queue_lock;
595 spin_lock_irqsave(qlock, irq_flags);
Kiyoshi Uedab2aec242007-12-11 17:47:14 -0500596 viodasd_end_request(req, error, num_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 spin_unlock_irqrestore(qlock, irq_flags);
598
599 /* Finally, try to get more requests off of this device's queue */
600 viodasd_restart_all_queues_starting_from(DEVICE_NO(d));
601
602 return 0;
603}
604
605/* This routine handles incoming block LP events */
606static void handle_block_event(struct HvLpEvent *event)
607{
608 struct vioblocklpevent *bevent = (struct vioblocklpevent *)event;
609 struct viodasd_waitevent *pwe;
610
611 if (event == NULL)
612 /* Notification that a partition went away! */
613 return;
614 /* First, we should NEVER get an int here...only acks */
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100615 if (hvlpevent_is_int(event)) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000616 pr_warning("Yikes! got an int in viodasd event handler!\n");
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100617 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 event->xRc = HvLpEvent_Rc_InvalidSubtype;
619 HvCallEvent_ackLpEvent(event);
620 }
621 }
622
623 switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
624 case vioblockopen:
625 /*
626 * Handle a response to an open request. We get all the
627 * disk information in the response, so update it. The
628 * correlation token contains a pointer to a waitevent
629 * structure that has a completion in it. update the
630 * return code in the waitevent structure and post the
631 * completion to wake up the guy who sent the request
632 */
633 pwe = (struct viodasd_waitevent *)event->xCorrelationToken;
634 pwe->rc = event->xRc;
635 pwe->sub_result = bevent->sub_result;
636 if (event->xRc == HvLpEvent_Rc_Good) {
637 const struct open_data *data = &bevent->u.open_data;
638 struct viodasd_device *device =
639 &viodasd_devices[bevent->disk];
640 device->read_only =
641 bevent->flags & vioblockflags_ro;
642 device->size = data->disk_size;
643 device->cylinders = data->cylinders;
644 device->tracks = data->tracks;
645 device->sectors = data->sectors;
646 device->bytes_per_sector = data->bytes_per_sector;
647 pwe->max_disk = data->max_disk;
648 }
649 complete(&pwe->com);
650 break;
651 case vioblockclose:
652 break;
653 case vioblockread:
654 case vioblockwrite:
655 viodasd_handle_read_write(bevent);
656 break;
657
658 default:
Joe Perchesdc942ce2009-12-22 21:22:59 +0000659 pr_warning("invalid subtype!");
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100660 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 event->xRc = HvLpEvent_Rc_InvalidSubtype;
662 HvCallEvent_ackLpEvent(event);
663 }
664 }
665}
666
667/*
668 * Get the driver to reprobe for more disks.
669 */
670static ssize_t probe_disks(struct device_driver *drv, const char *buf,
671 size_t count)
672{
673 struct viodasd_device *d;
674
675 for (d = viodasd_devices; d < &viodasd_devices[MAX_DISKNO]; d++) {
676 if (d->disk == NULL)
677 probe_disk(d);
678 }
679 return count;
680}
681static DRIVER_ATTR(probe, S_IWUSR, NULL, probe_disks);
682
683static int viodasd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
684{
685 struct viodasd_device *d = &viodasd_devices[vdev->unit_address];
686
687 d->dev = &vdev->dev;
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000688 if (!probe_disk(d))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return -ENODEV;
690 return 0;
691}
692
693static int viodasd_remove(struct vio_dev *vdev)
694{
695 struct viodasd_device *d;
696
697 d = &viodasd_devices[vdev->unit_address];
698 if (d->disk) {
699 del_gendisk(d->disk);
700 blk_cleanup_queue(d->disk->queue);
701 put_disk(d->disk);
702 d->disk = NULL;
703 }
704 d->dev = NULL;
705 return 0;
706}
707
708/**
709 * viodasd_device_table: Used by vio.c to match devices that we
710 * support.
711 */
712static struct vio_device_id viodasd_device_table[] __devinitdata = {
Stephen Rothwellfbabeb62006-05-15 13:41:22 +1000713 { "block", "IBM,iSeries-viodasd" },
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000714 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716MODULE_DEVICE_TABLE(vio, viodasd_device_table);
Stephen Rothwell915124d2005-10-24 15:12:22 +1000717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718static struct vio_driver viodasd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 .id_table = viodasd_device_table,
720 .probe = viodasd_probe,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000721 .remove = viodasd_remove,
722 .driver = {
723 .name = "viodasd",
Stephen Rothwell915124d2005-10-24 15:12:22 +1000724 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726};
727
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100728static int need_delete_probe;
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730/*
731 * Initialize the whole device driver. Handle module and non-module
732 * versions
733 */
734static int __init viodasd_init(void)
735{
736 int rc;
737
Stephen Rothwellfb8b5002006-12-15 15:40:08 +1100738 if (!firmware_has_feature(FW_FEATURE_ISERIES)) {
739 rc = -ENODEV;
740 goto early_fail;
741 }
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 /* Try to open to our host lp */
744 if (viopath_hostLp == HvLpIndexInvalid)
745 vio_set_hostlp();
746
747 if (viopath_hostLp == HvLpIndexInvalid) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000748 pr_warning("invalid hosting partition\n");
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100749 rc = -EIO;
750 goto early_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752
Joe Perchesdc942ce2009-12-22 21:22:59 +0000753 pr_info("vers " VIOD_VERS ", hosting partition %d\n", viopath_hostLp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /* register the block device */
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100756 rc = register_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
757 if (rc) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000758 pr_warning("Unable to get major number %d for %s\n",
759 VIODASD_MAJOR, VIOD_GENHD_NAME);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100760 goto early_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762 /* Actually open the path to the hosting partition */
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100763 rc = viopath_open(viopath_hostLp, viomajorsubtype_blockio,
764 VIOMAXREQ + 2);
765 if (rc) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000766 pr_warning("error opening path to host partition %d\n",
767 viopath_hostLp);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100768 goto unregister_blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770
771 /* Initialize our request handler */
772 vio_setHandler(viomajorsubtype_blockio, handle_block_event);
773
774 rc = vio_register_driver(&viodasd_driver);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100775 if (rc) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000776 pr_warning("vio_register_driver failed\n");
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100777 goto unset_handler;
778 }
779
780 /*
781 * If this call fails, it just means that we cannot dynamically
782 * add virtual disks, but the driver will still work fine for
783 * all existing disk, so ignore the failure.
784 */
785 if (!driver_create_file(&viodasd_driver.driver, &driver_attr_probe))
786 need_delete_probe = 1;
787
788 return 0;
789
790unset_handler:
791 vio_clearHandler(viomajorsubtype_blockio);
792 viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
793unregister_blk:
794 unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
795early_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return rc;
797}
798module_init(viodasd_init);
799
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100800void __exit viodasd_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100802 if (need_delete_probe)
803 driver_remove_file(&viodasd_driver.driver, &driver_attr_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 vio_unregister_driver(&viodasd_driver);
805 vio_clearHandler(viomajorsubtype_blockio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100807 unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809module_exit(viodasd_exit);