blob: f651e51a3319e0311f6a7bcc5e3d7e12d0e7d0e4 [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 Bergmann6e9624b2010-08-07 18:25:34 +020044#include <linux/smp_lock.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
76static DEFINE_SPINLOCK(viodasd_spinlock);
77
78#define VIOMAXREQ 16
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#define DEVICE_NO(cell) ((struct viodasd_device *)(cell) - &viodasd_devices[0])
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082struct viodasd_waitevent {
83 struct completion com;
84 int rc;
85 u16 sub_result;
86 int max_disk; /* open */
87};
88
89static const struct vio_error_entry viodasd_err_table[] = {
90 { 0x0201, EINVAL, "Invalid Range" },
91 { 0x0202, EINVAL, "Invalid Token" },
92 { 0x0203, EIO, "DMA Error" },
93 { 0x0204, EIO, "Use Error" },
94 { 0x0205, EIO, "Release Error" },
95 { 0x0206, EINVAL, "Invalid Disk" },
96 { 0x0207, EBUSY, "Cant Lock" },
97 { 0x0208, EIO, "Already Locked" },
98 { 0x0209, EIO, "Already Unlocked" },
99 { 0x020A, EIO, "Invalid Arg" },
100 { 0x020B, EIO, "Bad IFS File" },
101 { 0x020C, EROFS, "Read Only Device" },
102 { 0x02FF, EIO, "Internal Error" },
103 { 0x0000, 0, NULL },
104};
105
106/*
107 * Figure out the biggest I/O request (in sectors) we can accept
108 */
109#define VIODASD_MAXSECTORS (4096 / 512 * VIOMAXBLOCKDMA)
110
111/*
112 * Number of disk I/O requests we've sent to OS/400
113 */
114static int num_req_outstanding;
115
116/*
117 * This is our internal structure for keeping track of disk devices
118 */
119struct viodasd_device {
120 u16 cylinders;
121 u16 tracks;
122 u16 sectors;
123 u16 bytes_per_sector;
124 u64 size;
125 int read_only;
126 spinlock_t q_lock;
127 struct gendisk *disk;
128 struct device *dev;
129} viodasd_devices[MAX_DISKNO];
130
131/*
132 * External open entry point.
133 */
Al Virof115a142008-03-02 10:22:07 -0500134static int viodasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Al Virof115a142008-03-02 10:22:07 -0500136 struct viodasd_device *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 HvLpEvent_Rc hvrc;
138 struct viodasd_waitevent we;
139 u16 flags = 0;
140
141 if (d->read_only) {
Al Virof115a142008-03-02 10:22:07 -0500142 if (mode & FMODE_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return -EROFS;
144 flags = vioblockflags_ro;
145 }
146
147 init_completion(&we.com);
148
149 /* Send the open event to OS/400 */
150 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
151 HvLpEvent_Type_VirtualIo,
152 viomajorsubtype_blockio | vioblockopen,
153 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
154 viopath_sourceinst(viopath_hostLp),
155 viopath_targetinst(viopath_hostLp),
156 (u64)(unsigned long)&we, VIOVERSION << 16,
157 ((u64)DEVICE_NO(d) << 48) | ((u64)flags << 32),
158 0, 0, 0);
159 if (hvrc != 0) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000160 pr_warning("HV open failed %d\n", (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return -EIO;
162 }
163
164 wait_for_completion(&we.com);
165
166 /* Check the return code */
167 if (we.rc != 0) {
168 const struct vio_error_entry *err =
169 vio_lookup_rc(viodasd_err_table, we.sub_result);
170
Joe Perchesdc942ce2009-12-22 21:22:59 +0000171 pr_warning("bad rc opening disk: %d:0x%04x (%s)\n",
172 (int)we.rc, we.sub_result, err->msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return -EIO;
174 }
175
176 return 0;
177}
178
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200179static int viodasd_unlocked_open(struct block_device *bdev, fmode_t mode)
180{
181 int ret;
182
183 lock_kernel();
184 ret = viodasd_open(bdev, mode);
185 unlock_kernel();
186
187 return ret;
188}
189
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
192 * External release entry point.
193 */
Al Virof115a142008-03-02 10:22:07 -0500194static int viodasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Al Virof115a142008-03-02 10:22:07 -0500196 struct viodasd_device *d = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 HvLpEvent_Rc hvrc;
198
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200199 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* Send the event to OS/400. We DON'T expect a response */
201 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
202 HvLpEvent_Type_VirtualIo,
203 viomajorsubtype_blockio | vioblockclose,
204 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
205 viopath_sourceinst(viopath_hostLp),
206 viopath_targetinst(viopath_hostLp),
207 0, VIOVERSION << 16,
208 ((u64)DEVICE_NO(d) << 48) /* | ((u64)flags << 32) */,
209 0, 0, 0);
210 if (hvrc != 0)
Joe Perchesdc942ce2009-12-22 21:22:59 +0000211 pr_warning("HV close call failed %d\n", (int)hvrc);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200212
213 unlock_kernel();
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return 0;
216}
217
218
219/* External ioctl entry point.
220 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800221static int viodasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800223 struct gendisk *disk = bdev->bd_disk;
224 struct viodasd_device *d = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Stephen Rothwell36a70032007-05-29 16:06:54 +1000226 geo->sectors = d->sectors ? d->sectors : 32;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800227 geo->heads = d->tracks ? d->tracks : 64;
228 geo->cylinders = d->cylinders ? d->cylinders :
Stephen Rothwell36a70032007-05-29 16:06:54 +1000229 get_capacity(disk) / (geo->sectors * geo->heads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800231 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234/*
235 * Our file operations table
236 */
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700237static const struct block_device_operations viodasd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .owner = THIS_MODULE,
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200239 .open = viodasd_unlocked_open,
Al Virof115a142008-03-02 10:22:07 -0500240 .release = viodasd_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800241 .getgeo = viodasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242};
243
244/*
245 * End a request
246 */
Kiyoshi Uedab2aec242007-12-11 17:47:14 -0500247static void viodasd_end_request(struct request *req, int error,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 int num_sectors)
249{
Kiyoshi Uedab2aec242007-12-11 17:47:14 -0500250 __blk_end_request(req, error, num_sectors << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253/*
254 * Send an actual I/O request to OS/400
255 */
256static int send_request(struct request *req)
257{
258 u64 start;
259 int direction;
260 int nsg;
261 u16 viocmd;
262 HvLpEvent_Rc hvrc;
263 struct vioblocklpevent *bevent;
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100264 struct HvLpEvent *hev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 struct scatterlist sg[VIOMAXBLOCKDMA];
266 int sgindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct viodasd_device *d;
268 unsigned long flags;
269
Tejun Heo83096eb2009-05-07 22:24:39 +0900270 start = (u64)blk_rq_pos(req) << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if (rq_data_dir(req) == READ) {
273 direction = DMA_FROM_DEVICE;
274 viocmd = viomajorsubtype_blockio | vioblockread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 } else {
276 direction = DMA_TO_DEVICE;
277 viocmd = viomajorsubtype_blockio | vioblockwrite;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279
280 d = req->rq_disk->private_data;
281
282 /* Now build the scatter-gather list */
Jens Axboe45711f12007-10-22 21:19:53 +0200283 sg_init_table(sg, VIOMAXBLOCKDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 nsg = blk_rq_map_sg(req->q, req, sg);
285 nsg = dma_map_sg(d->dev, sg, nsg, direction);
286
287 spin_lock_irqsave(&viodasd_spinlock, flags);
288 num_req_outstanding++;
289
290 /* This optimization handles a single DMA block */
291 if (nsg == 1)
292 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
293 HvLpEvent_Type_VirtualIo, viocmd,
294 HvLpEvent_AckInd_DoAck,
295 HvLpEvent_AckType_ImmediateAck,
296 viopath_sourceinst(viopath_hostLp),
297 viopath_targetinst(viopath_hostLp),
298 (u64)(unsigned long)req, VIOVERSION << 16,
299 ((u64)DEVICE_NO(d) << 48), start,
300 ((u64)sg_dma_address(&sg[0])) << 32,
301 sg_dma_len(&sg[0]));
302 else {
303 bevent = (struct vioblocklpevent *)
304 vio_get_event_buffer(viomajorsubtype_blockio);
305 if (bevent == NULL) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000306 pr_warning("error allocating disk event buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 goto error_ret;
308 }
309
310 /*
311 * Now build up the actual request. Note that we store
312 * the pointer to the request in the correlation
313 * token so we can match the response up later
314 */
315 memset(bevent, 0, sizeof(struct vioblocklpevent));
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100316 hev = &bevent->event;
317 hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DO_ACK |
318 HV_LP_EVENT_INT;
319 hev->xType = HvLpEvent_Type_VirtualIo;
320 hev->xSubtype = viocmd;
321 hev->xSourceLp = HvLpConfig_getLpIndex();
322 hev->xTargetLp = viopath_hostLp;
323 hev->xSizeMinus1 =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 offsetof(struct vioblocklpevent, u.rw_data.dma_info) +
325 (sizeof(bevent->u.rw_data.dma_info[0]) * nsg) - 1;
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100326 hev->xSourceInstanceId = viopath_sourceinst(viopath_hostLp);
327 hev->xTargetInstanceId = viopath_targetinst(viopath_hostLp);
328 hev->xCorrelationToken = (u64)req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 bevent->version = VIOVERSION;
330 bevent->disk = DEVICE_NO(d);
331 bevent->u.rw_data.offset = start;
332
333 /*
334 * Copy just the dma information from the sg list
335 * into the request
336 */
337 for (sgindex = 0; sgindex < nsg; sgindex++) {
338 bevent->u.rw_data.dma_info[sgindex].token =
339 sg_dma_address(&sg[sgindex]);
340 bevent->u.rw_data.dma_info[sgindex].len =
341 sg_dma_len(&sg[sgindex]);
342 }
343
344 /* Send the request */
345 hvrc = HvCallEvent_signalLpEvent(&bevent->event);
346 vio_free_event_buffer(viomajorsubtype_blockio, bevent);
347 }
348
349 if (hvrc != HvLpEvent_Rc_Good) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000350 pr_warning("error sending disk event to OS/400 (rc %d)\n",
351 (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 goto error_ret;
353 }
354 spin_unlock_irqrestore(&viodasd_spinlock, flags);
355 return 0;
356
357error_ret:
358 num_req_outstanding--;
359 spin_unlock_irqrestore(&viodasd_spinlock, flags);
360 dma_unmap_sg(d->dev, sg, nsg, direction);
361 return -1;
362}
363
364/*
365 * This is the external request processing routine
366 */
Jens Axboe165125e2007-07-24 09:28:11 +0200367static void do_viodasd_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 struct request *req;
370
371 /*
372 * If we already have the maximum number of requests
373 * outstanding to OS/400 just bail out. We'll come
374 * back later.
375 */
376 while (num_req_outstanding < VIOMAXREQ) {
Tejun Heo9934c8c2009-05-08 11:54:16 +0900377 req = blk_fetch_request(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (req == NULL)
379 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* check that request contains a valid command */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200381 if (req->cmd_type != REQ_TYPE_FS) {
Tejun Heo5b936292009-05-07 22:24:38 +0900382 viodasd_end_request(req, -EIO, blk_rq_sectors(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 continue;
384 }
385 /* Try sending the request */
386 if (send_request(req) != 0)
Tejun Heo5b936292009-05-07 22:24:38 +0900387 viodasd_end_request(req, -EIO, blk_rq_sectors(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389}
390
391/*
392 * Probe a single disk and fill in the viodasd_device structure
393 * for it.
394 */
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000395static int probe_disk(struct viodasd_device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 HvLpEvent_Rc hvrc;
398 struct viodasd_waitevent we;
399 int dev_no = DEVICE_NO(d);
400 struct gendisk *g;
401 struct request_queue *q;
402 u16 flags = 0;
403
404retry:
405 init_completion(&we.com);
406
407 /* Send the open event to OS/400 */
408 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
409 HvLpEvent_Type_VirtualIo,
410 viomajorsubtype_blockio | vioblockopen,
411 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
412 viopath_sourceinst(viopath_hostLp),
413 viopath_targetinst(viopath_hostLp),
414 (u64)(unsigned long)&we, VIOVERSION << 16,
415 ((u64)dev_no << 48) | ((u64)flags<< 32),
416 0, 0, 0);
417 if (hvrc != 0) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000418 pr_warning("bad rc on HV open %d\n", (int)hvrc);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000419 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
422 wait_for_completion(&we.com);
423
424 if (we.rc != 0) {
425 if (flags != 0)
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /* try again with read only flag set */
428 flags = vioblockflags_ro;
429 goto retry;
430 }
431 if (we.max_disk > (MAX_DISKNO - 1)) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000432 printk_once(KERN_INFO pr_fmt("Only examining the first %d of %d disks connected\n"),
433 MAX_DISKNO, we.max_disk + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
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) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000447 pr_warning("bad rc sending event to OS/400 %d\n", (int)hvrc);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000450
451 if (d->dev == NULL) {
452 /* this is when we reprobe for new disks */
453 if (vio_create_viodasd(dev_no) == NULL) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000454 pr_warning("cannot allocate virtual device for disk %d\n",
455 dev_no);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000456 return 0;
457 }
458 /*
459 * The vio_create_viodasd will have recursed into this
460 * routine with d->dev set to the new vio device and
461 * will finish the setup of the disk below.
462 */
463 return 1;
464 }
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* create the request queue for the disk */
467 spin_lock_init(&d->q_lock);
468 q = blk_init_queue(do_viodasd_request, &d->q_lock);
469 if (q == NULL) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000470 pr_warning("cannot allocate queue for disk %d\n", dev_no);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473 g = alloc_disk(1 << PARTITION_SHIFT);
474 if (g == NULL) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000475 pr_warning("cannot allocate disk structure for disk %d\n",
476 dev_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 blk_cleanup_queue(q);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480
481 d->disk = g;
Martin K. Petersen8a783622010-02-26 00:20:39 -0500482 blk_queue_max_segments(q, VIOMAXBLOCKDMA);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500483 blk_queue_max_hw_sectors(q, VIODASD_MAXSECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 g->major = VIODASD_MAJOR;
485 g->first_minor = dev_no << PARTITION_SHIFT;
486 if (dev_no >= 26)
487 snprintf(g->disk_name, sizeof(g->disk_name),
488 VIOD_GENHD_NAME "%c%c",
489 'a' + (dev_no / 26) - 1, 'a' + (dev_no % 26));
490 else
491 snprintf(g->disk_name, sizeof(g->disk_name),
492 VIOD_GENHD_NAME "%c", 'a' + (dev_no % 26));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 g->fops = &viodasd_fops;
494 g->queue = q;
495 g->private_data = d;
496 g->driverfs_dev = d->dev;
497 set_capacity(g, d->size >> 9);
498
Joe Perchesdc942ce2009-12-22 21:22:59 +0000499 pr_info("disk %d: %lu sectors (%lu MB) CHS=%d/%d/%d sector size %d%s\n",
500 dev_no, (unsigned long)(d->size >> 9),
501 (unsigned long)(d->size >> 20),
502 (int)d->cylinders, (int)d->tracks,
503 (int)d->sectors, (int)d->bytes_per_sector,
504 d->read_only ? " (RO)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /* register us in the global list */
507 add_disk(g);
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000508 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511/* returns the total number of scatterlist elements converted */
512static int block_event_to_scatterlist(const struct vioblocklpevent *bevent,
513 struct scatterlist *sg, int *total_len)
514{
515 int i, numsg;
516 const struct rw_data *rw_data = &bevent->u.rw_data;
517 static const int offset =
518 offsetof(struct vioblocklpevent, u.rw_data.dma_info);
519 static const int element_size = sizeof(rw_data->dma_info[0]);
520
521 numsg = ((bevent->event.xSizeMinus1 + 1) - offset) / element_size;
522 if (numsg > VIOMAXBLOCKDMA)
523 numsg = VIOMAXBLOCKDMA;
524
525 *total_len = 0;
Benjamin Herrenschmidt25c0a7b2008-03-12 17:23:56 +1100526 sg_init_table(sg, VIOMAXBLOCKDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 for (i = 0; (i < numsg) && (rw_data->dma_info[i].len > 0); ++i) {
528 sg_dma_address(&sg[i]) = rw_data->dma_info[i].token;
529 sg_dma_len(&sg[i]) = rw_data->dma_info[i].len;
530 *total_len += rw_data->dma_info[i].len;
531 }
532 return i;
533}
534
535/*
536 * Restart all queues, starting with the one _after_ the disk given,
537 * thus reducing the chance of starvation of higher numbered disks.
538 */
539static void viodasd_restart_all_queues_starting_from(int first_index)
540{
541 int i;
542
543 for (i = first_index + 1; i < MAX_DISKNO; ++i)
544 if (viodasd_devices[i].disk)
545 blk_run_queue(viodasd_devices[i].disk->queue);
546 for (i = 0; i <= first_index; ++i)
547 if (viodasd_devices[i].disk)
548 blk_run_queue(viodasd_devices[i].disk->queue);
549}
550
551/*
552 * For read and write requests, decrement the number of outstanding requests,
553 * Free the DMA buffers we allocated.
554 */
555static int viodasd_handle_read_write(struct vioblocklpevent *bevent)
556{
557 int num_sg, num_sect, pci_direction, total_len;
558 struct request *req;
559 struct scatterlist sg[VIOMAXBLOCKDMA];
560 struct HvLpEvent *event = &bevent->event;
561 unsigned long irq_flags;
562 struct viodasd_device *d;
563 int error;
564 spinlock_t *qlock;
565
566 num_sg = block_event_to_scatterlist(bevent, sg, &total_len);
567 num_sect = total_len >> 9;
568 if (event->xSubtype == (viomajorsubtype_blockio | vioblockread))
569 pci_direction = DMA_FROM_DEVICE;
570 else
571 pci_direction = DMA_TO_DEVICE;
572 req = (struct request *)bevent->event.xCorrelationToken;
573 d = req->rq_disk->private_data;
574
575 dma_unmap_sg(d->dev, sg, num_sg, pci_direction);
576
577 /*
578 * Since this is running in interrupt mode, we need to make sure
579 * we're not stepping on any global I/O operations
580 */
581 spin_lock_irqsave(&viodasd_spinlock, irq_flags);
582 num_req_outstanding--;
583 spin_unlock_irqrestore(&viodasd_spinlock, irq_flags);
584
Kiyoshi Uedab2aec242007-12-11 17:47:14 -0500585 error = (event->xRc == HvLpEvent_Rc_Good) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (error) {
587 const struct vio_error_entry *err;
588 err = vio_lookup_rc(viodasd_err_table, bevent->sub_result);
Joe Perchesdc942ce2009-12-22 21:22:59 +0000589 pr_warning("read/write error %d:0x%04x (%s)\n",
590 event->xRc, bevent->sub_result, err->msg);
Tejun Heo5b936292009-05-07 22:24:38 +0900591 num_sect = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593 qlock = req->q->queue_lock;
594 spin_lock_irqsave(qlock, irq_flags);
Kiyoshi Uedab2aec242007-12-11 17:47:14 -0500595 viodasd_end_request(req, error, num_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 spin_unlock_irqrestore(qlock, irq_flags);
597
598 /* Finally, try to get more requests off of this device's queue */
599 viodasd_restart_all_queues_starting_from(DEVICE_NO(d));
600
601 return 0;
602}
603
604/* This routine handles incoming block LP events */
605static void handle_block_event(struct HvLpEvent *event)
606{
607 struct vioblocklpevent *bevent = (struct vioblocklpevent *)event;
608 struct viodasd_waitevent *pwe;
609
610 if (event == NULL)
611 /* Notification that a partition went away! */
612 return;
613 /* First, we should NEVER get an int here...only acks */
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100614 if (hvlpevent_is_int(event)) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000615 pr_warning("Yikes! got an int in viodasd event handler!\n");
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100616 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 event->xRc = HvLpEvent_Rc_InvalidSubtype;
618 HvCallEvent_ackLpEvent(event);
619 }
620 }
621
622 switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
623 case vioblockopen:
624 /*
625 * Handle a response to an open request. We get all the
626 * disk information in the response, so update it. The
627 * correlation token contains a pointer to a waitevent
628 * structure that has a completion in it. update the
629 * return code in the waitevent structure and post the
630 * completion to wake up the guy who sent the request
631 */
632 pwe = (struct viodasd_waitevent *)event->xCorrelationToken;
633 pwe->rc = event->xRc;
634 pwe->sub_result = bevent->sub_result;
635 if (event->xRc == HvLpEvent_Rc_Good) {
636 const struct open_data *data = &bevent->u.open_data;
637 struct viodasd_device *device =
638 &viodasd_devices[bevent->disk];
639 device->read_only =
640 bevent->flags & vioblockflags_ro;
641 device->size = data->disk_size;
642 device->cylinders = data->cylinders;
643 device->tracks = data->tracks;
644 device->sectors = data->sectors;
645 device->bytes_per_sector = data->bytes_per_sector;
646 pwe->max_disk = data->max_disk;
647 }
648 complete(&pwe->com);
649 break;
650 case vioblockclose:
651 break;
652 case vioblockread:
653 case vioblockwrite:
654 viodasd_handle_read_write(bevent);
655 break;
656
657 default:
Joe Perchesdc942ce2009-12-22 21:22:59 +0000658 pr_warning("invalid subtype!");
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100659 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 event->xRc = HvLpEvent_Rc_InvalidSubtype;
661 HvCallEvent_ackLpEvent(event);
662 }
663 }
664}
665
666/*
667 * Get the driver to reprobe for more disks.
668 */
669static ssize_t probe_disks(struct device_driver *drv, const char *buf,
670 size_t count)
671{
672 struct viodasd_device *d;
673
674 for (d = viodasd_devices; d < &viodasd_devices[MAX_DISKNO]; d++) {
675 if (d->disk == NULL)
676 probe_disk(d);
677 }
678 return count;
679}
680static DRIVER_ATTR(probe, S_IWUSR, NULL, probe_disks);
681
682static int viodasd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
683{
684 struct viodasd_device *d = &viodasd_devices[vdev->unit_address];
685
686 d->dev = &vdev->dev;
Stephen Rothwell8251b4c2007-10-11 14:59:54 +1000687 if (!probe_disk(d))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return -ENODEV;
689 return 0;
690}
691
692static int viodasd_remove(struct vio_dev *vdev)
693{
694 struct viodasd_device *d;
695
696 d = &viodasd_devices[vdev->unit_address];
697 if (d->disk) {
698 del_gendisk(d->disk);
699 blk_cleanup_queue(d->disk->queue);
700 put_disk(d->disk);
701 d->disk = NULL;
702 }
703 d->dev = NULL;
704 return 0;
705}
706
707/**
708 * viodasd_device_table: Used by vio.c to match devices that we
709 * support.
710 */
711static struct vio_device_id viodasd_device_table[] __devinitdata = {
Stephen Rothwellfbabeb62006-05-15 13:41:22 +1000712 { "block", "IBM,iSeries-viodasd" },
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000713 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715MODULE_DEVICE_TABLE(vio, viodasd_device_table);
Stephen Rothwell915124d2005-10-24 15:12:22 +1000716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717static struct vio_driver viodasd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 .id_table = viodasd_device_table,
719 .probe = viodasd_probe,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000720 .remove = viodasd_remove,
721 .driver = {
722 .name = "viodasd",
Stephen Rothwell915124d2005-10-24 15:12:22 +1000723 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725};
726
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100727static int need_delete_probe;
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729/*
730 * Initialize the whole device driver. Handle module and non-module
731 * versions
732 */
733static int __init viodasd_init(void)
734{
735 int rc;
736
Stephen Rothwellfb8b5002006-12-15 15:40:08 +1100737 if (!firmware_has_feature(FW_FEATURE_ISERIES)) {
738 rc = -ENODEV;
739 goto early_fail;
740 }
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /* Try to open to our host lp */
743 if (viopath_hostLp == HvLpIndexInvalid)
744 vio_set_hostlp();
745
746 if (viopath_hostLp == HvLpIndexInvalid) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000747 pr_warning("invalid hosting partition\n");
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100748 rc = -EIO;
749 goto early_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751
Joe Perchesdc942ce2009-12-22 21:22:59 +0000752 pr_info("vers " VIOD_VERS ", hosting partition %d\n", viopath_hostLp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /* register the block device */
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100755 rc = register_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
756 if (rc) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000757 pr_warning("Unable to get major number %d for %s\n",
758 VIODASD_MAJOR, VIOD_GENHD_NAME);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100759 goto early_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761 /* Actually open the path to the hosting partition */
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100762 rc = viopath_open(viopath_hostLp, viomajorsubtype_blockio,
763 VIOMAXREQ + 2);
764 if (rc) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000765 pr_warning("error opening path to host partition %d\n",
766 viopath_hostLp);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100767 goto unregister_blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769
770 /* Initialize our request handler */
771 vio_setHandler(viomajorsubtype_blockio, handle_block_event);
772
773 rc = vio_register_driver(&viodasd_driver);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100774 if (rc) {
Joe Perchesdc942ce2009-12-22 21:22:59 +0000775 pr_warning("vio_register_driver failed\n");
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100776 goto unset_handler;
777 }
778
779 /*
780 * If this call fails, it just means that we cannot dynamically
781 * add virtual disks, but the driver will still work fine for
782 * all existing disk, so ignore the failure.
783 */
784 if (!driver_create_file(&viodasd_driver.driver, &driver_attr_probe))
785 need_delete_probe = 1;
786
787 return 0;
788
789unset_handler:
790 vio_clearHandler(viomajorsubtype_blockio);
791 viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
792unregister_blk:
793 unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
794early_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return rc;
796}
797module_init(viodasd_init);
798
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100799void __exit viodasd_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100801 if (need_delete_probe)
802 driver_remove_file(&viodasd_driver.driver, &driver_attr_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 vio_unregister_driver(&viodasd_driver);
804 vio_clearHandler(viomajorsubtype_blockio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
Stephen Rothwellf9df68e2006-11-13 14:43:17 +1100806 unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808module_exit(viodasd_exit);