Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* -*- 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 Rothwell | 8962cad | 2008-05-23 11:41:46 +1000 | [diff] [blame] | 6 | * Stephen Rothwell |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * (C) Copyright 2000-2004 IBM Corporation |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of the |
| 13 | * License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | * |
| 24 | * This routine provides access to disk space (termed "DASD" in historical |
| 25 | * IBM terms) owned and managed by an OS/400 partition running on the |
| 26 | * same box as this Linux partition. |
| 27 | * |
| 28 | * All disk operations are performed by sending messages back and forth to |
| 29 | * the OS/400 partition. |
| 30 | */ |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 31 | |
| 32 | #define pr_fmt(fmt) "viod: " fmt |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #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 Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 44 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/dma-mapping.h> |
| 46 | #include <linux/completion.h> |
| 47 | #include <linux/device.h> |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 48 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | #include <asm/uaccess.h> |
| 51 | #include <asm/vio.h> |
Kelly Daly | 1ec65d7 | 2005-11-02 13:46:07 +1100 | [diff] [blame] | 52 | #include <asm/iseries/hv_types.h> |
Kelly Daly | e45423e | 2005-11-02 12:08:31 +1100 | [diff] [blame] | 53 | #include <asm/iseries/hv_lp_event.h> |
Kelly Daly | 15b1718 | 2005-11-02 11:55:28 +1100 | [diff] [blame] | 54 | #include <asm/iseries/hv_lp_config.h> |
Kelly Daly | b420677 | 2005-11-02 15:13:57 +1100 | [diff] [blame] | 55 | #include <asm/iseries/vio.h> |
Stephen Rothwell | fb8b500 | 2006-12-15 15:40:08 +1100 | [diff] [blame] | 56 | #include <asm/firmware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | MODULE_DESCRIPTION("iSeries Virtual DASD"); |
| 59 | MODULE_AUTHOR("Dave Boutcher"); |
| 60 | MODULE_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | #define VIOD_VERS "1.64" |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | enum { |
| 71 | PARTITION_SHIFT = 3, |
| 72 | MAX_DISKNO = HVMAXARCHITECTEDVIRTUALDISKS, |
Julia Lawall | ea6728c | 2008-03-29 08:21:08 +1100 | [diff] [blame] | 73 | MAX_DISK_NAME = FIELD_SIZEOF(struct gendisk, disk_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 76 | static DEFINE_MUTEX(viodasd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | static DEFINE_SPINLOCK(viodasd_spinlock); |
| 78 | |
| 79 | #define VIOMAXREQ 16 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | #define DEVICE_NO(cell) ((struct viodasd_device *)(cell) - &viodasd_devices[0]) |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | struct viodasd_waitevent { |
| 84 | struct completion com; |
| 85 | int rc; |
| 86 | u16 sub_result; |
| 87 | int max_disk; /* open */ |
| 88 | }; |
| 89 | |
| 90 | static 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 | */ |
| 115 | static int num_req_outstanding; |
| 116 | |
| 117 | /* |
| 118 | * This is our internal structure for keeping track of disk devices |
| 119 | */ |
| 120 | struct 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 Viro | f115a14 | 2008-03-02 10:22:07 -0500 | [diff] [blame] | 135 | static int viodasd_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
Al Viro | f115a14 | 2008-03-02 10:22:07 -0500 | [diff] [blame] | 137 | struct viodasd_device *d = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | HvLpEvent_Rc hvrc; |
| 139 | struct viodasd_waitevent we; |
| 140 | u16 flags = 0; |
| 141 | |
| 142 | if (d->read_only) { |
Al Viro | f115a14 | 2008-03-02 10:22:07 -0500 | [diff] [blame] | 143 | if (mode & FMODE_WRITE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 161 | pr_warning("HV open failed %d\n", (int)hvrc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 172 | pr_warning("bad rc opening disk: %d:0x%04x (%s)\n", |
| 173 | (int)we.rc, we.sub_result, err->msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | return -EIO; |
| 175 | } |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 180 | static int viodasd_unlocked_open(struct block_device *bdev, fmode_t mode) |
| 181 | { |
| 182 | int ret; |
| 183 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 184 | mutex_lock(&viodasd_mutex); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 185 | ret = viodasd_open(bdev, mode); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 186 | mutex_unlock(&viodasd_mutex); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 187 | |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | /* |
| 193 | * External release entry point. |
| 194 | */ |
Al Viro | f115a14 | 2008-03-02 10:22:07 -0500 | [diff] [blame] | 195 | static int viodasd_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
Al Viro | f115a14 | 2008-03-02 10:22:07 -0500 | [diff] [blame] | 197 | struct viodasd_device *d = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | HvLpEvent_Rc hvrc; |
| 199 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 200 | mutex_lock(&viodasd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | /* 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 212 | pr_warning("HV close call failed %d\n", (int)hvrc); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 213 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 214 | mutex_unlock(&viodasd_mutex); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | /* External ioctl entry point. |
| 221 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 222 | static int viodasd_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 224 | struct gendisk *disk = bdev->bd_disk; |
| 225 | struct viodasd_device *d = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Stephen Rothwell | 36a7003 | 2007-05-29 16:06:54 +1000 | [diff] [blame] | 227 | geo->sectors = d->sectors ? d->sectors : 32; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 228 | geo->heads = d->tracks ? d->tracks : 64; |
| 229 | geo->cylinders = d->cylinders ? d->cylinders : |
Stephen Rothwell | 36a7003 | 2007-05-29 16:06:54 +1000 | [diff] [blame] | 230 | get_capacity(disk) / (geo->sectors * geo->heads); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 232 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /* |
| 236 | * Our file operations table |
| 237 | */ |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 238 | static const struct block_device_operations viodasd_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | .owner = THIS_MODULE, |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 240 | .open = viodasd_unlocked_open, |
Al Viro | f115a14 | 2008-03-02 10:22:07 -0500 | [diff] [blame] | 241 | .release = viodasd_release, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 242 | .getgeo = viodasd_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | /* |
| 246 | * End a request |
| 247 | */ |
Kiyoshi Ueda | b2aec24 | 2007-12-11 17:47:14 -0500 | [diff] [blame] | 248 | static void viodasd_end_request(struct request *req, int error, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | int num_sectors) |
| 250 | { |
Kiyoshi Ueda | b2aec24 | 2007-12-11 17:47:14 -0500 | [diff] [blame] | 251 | __blk_end_request(req, error, num_sectors << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Send an actual I/O request to OS/400 |
| 256 | */ |
| 257 | static 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 Rothwell | 677f8c0 | 2006-01-12 13:47:43 +1100 | [diff] [blame] | 265 | struct HvLpEvent *hev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | struct scatterlist sg[VIOMAXBLOCKDMA]; |
| 267 | int sgindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | struct viodasd_device *d; |
| 269 | unsigned long flags; |
| 270 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 271 | start = (u64)blk_rq_pos(req) << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
| 273 | if (rq_data_dir(req) == READ) { |
| 274 | direction = DMA_FROM_DEVICE; |
| 275 | viocmd = viomajorsubtype_blockio | vioblockread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } else { |
| 277 | direction = DMA_TO_DEVICE; |
| 278 | viocmd = viomajorsubtype_blockio | vioblockwrite; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | d = req->rq_disk->private_data; |
| 282 | |
| 283 | /* Now build the scatter-gather list */ |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 284 | sg_init_table(sg, VIOMAXBLOCKDMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 307 | pr_warning("error allocating disk event buffer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | 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 Rothwell | 677f8c0 | 2006-01-12 13:47:43 +1100 | [diff] [blame] | 317 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | offsetof(struct vioblocklpevent, u.rw_data.dma_info) + |
| 326 | (sizeof(bevent->u.rw_data.dma_info[0]) * nsg) - 1; |
Stephen Rothwell | 677f8c0 | 2006-01-12 13:47:43 +1100 | [diff] [blame] | 327 | hev->xSourceInstanceId = viopath_sourceinst(viopath_hostLp); |
| 328 | hev->xTargetInstanceId = viopath_targetinst(viopath_hostLp); |
| 329 | hev->xCorrelationToken = (u64)req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 351 | pr_warning("error sending disk event to OS/400 (rc %d)\n", |
| 352 | (int)hvrc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | goto error_ret; |
| 354 | } |
| 355 | spin_unlock_irqrestore(&viodasd_spinlock, flags); |
| 356 | return 0; |
| 357 | |
| 358 | error_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 Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 368 | static void do_viodasd_request(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | { |
| 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 Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 378 | req = blk_fetch_request(q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | if (req == NULL) |
| 380 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | /* check that request contains a valid command */ |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 382 | if (req->cmd_type != REQ_TYPE_FS) { |
Tejun Heo | 5b93629 | 2009-05-07 22:24:38 +0900 | [diff] [blame] | 383 | viodasd_end_request(req, -EIO, blk_rq_sectors(req)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | continue; |
| 385 | } |
| 386 | /* Try sending the request */ |
| 387 | if (send_request(req) != 0) |
Tejun Heo | 5b93629 | 2009-05-07 22:24:38 +0900 | [diff] [blame] | 388 | viodasd_end_request(req, -EIO, blk_rq_sectors(req)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Probe a single disk and fill in the viodasd_device structure |
| 394 | * for it. |
| 395 | */ |
Stephen Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 396 | static int probe_disk(struct viodasd_device *d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | { |
| 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 | |
| 405 | retry: |
| 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 419 | pr_warning("bad rc on HV open %d\n", (int)hvrc); |
Stephen Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 420 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | wait_for_completion(&we.com); |
| 424 | |
| 425 | if (we.rc != 0) { |
| 426 | if (flags != 0) |
Stephen Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 427 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | /* try again with read only flag set */ |
| 429 | flags = vioblockflags_ro; |
| 430 | goto retry; |
| 431 | } |
| 432 | if (we.max_disk > (MAX_DISKNO - 1)) { |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 433 | printk_once(KERN_INFO pr_fmt("Only examining the first %d of %d disks connected\n"), |
| 434 | MAX_DISKNO, we.max_disk + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 448 | pr_warning("bad rc sending event to OS/400 %d\n", (int)hvrc); |
Stephen Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 449 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | } |
Stephen Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 451 | |
| 452 | if (d->dev == NULL) { |
| 453 | /* this is when we reprobe for new disks */ |
| 454 | if (vio_create_viodasd(dev_no) == NULL) { |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 455 | pr_warning("cannot allocate virtual device for disk %d\n", |
| 456 | dev_no); |
Stephen Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 457 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | /* 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 471 | pr_warning("cannot allocate queue for disk %d\n", dev_no); |
Stephen Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 472 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |
| 474 | g = alloc_disk(1 << PARTITION_SHIFT); |
| 475 | if (g == NULL) { |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 476 | pr_warning("cannot allocate disk structure for disk %d\n", |
| 477 | dev_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | blk_cleanup_queue(q); |
Stephen Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 479 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | d->disk = g; |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 483 | blk_queue_max_segments(q, VIOMAXBLOCKDMA); |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 484 | blk_queue_max_hw_sectors(q, VIODASD_MAXSECTORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 500 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
| 507 | /* register us in the global list */ |
| 508 | add_disk(g); |
Stephen Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 509 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /* returns the total number of scatterlist elements converted */ |
| 513 | static 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 Herrenschmidt | 25c0a7b | 2008-03-12 17:23:56 +1100 | [diff] [blame] | 527 | sg_init_table(sg, VIOMAXBLOCKDMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | 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 | */ |
| 540 | static 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 | */ |
| 556 | static 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 Ueda | b2aec24 | 2007-12-11 17:47:14 -0500 | [diff] [blame] | 586 | error = (event->xRc == HvLpEvent_Rc_Good) ? 0 : -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | if (error) { |
| 588 | const struct vio_error_entry *err; |
| 589 | err = vio_lookup_rc(viodasd_err_table, bevent->sub_result); |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 590 | pr_warning("read/write error %d:0x%04x (%s)\n", |
| 591 | event->xRc, bevent->sub_result, err->msg); |
Tejun Heo | 5b93629 | 2009-05-07 22:24:38 +0900 | [diff] [blame] | 592 | num_sect = blk_rq_sectors(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
| 594 | qlock = req->q->queue_lock; |
| 595 | spin_lock_irqsave(qlock, irq_flags); |
Kiyoshi Ueda | b2aec24 | 2007-12-11 17:47:14 -0500 | [diff] [blame] | 596 | viodasd_end_request(req, error, num_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | 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 */ |
| 606 | static 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 Rothwell | 677f8c0 | 2006-01-12 13:47:43 +1100 | [diff] [blame] | 615 | if (hvlpevent_is_int(event)) { |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 616 | pr_warning("Yikes! got an int in viodasd event handler!\n"); |
Stephen Rothwell | 677f8c0 | 2006-01-12 13:47:43 +1100 | [diff] [blame] | 617 | if (hvlpevent_need_ack(event)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 659 | pr_warning("invalid subtype!"); |
Stephen Rothwell | 677f8c0 | 2006-01-12 13:47:43 +1100 | [diff] [blame] | 660 | if (hvlpevent_need_ack(event)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | event->xRc = HvLpEvent_Rc_InvalidSubtype; |
| 662 | HvCallEvent_ackLpEvent(event); |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | /* |
| 668 | * Get the driver to reprobe for more disks. |
| 669 | */ |
| 670 | static 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 | } |
| 681 | static DRIVER_ATTR(probe, S_IWUSR, NULL, probe_disks); |
| 682 | |
| 683 | static 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 Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 688 | if (!probe_disk(d)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | return -ENODEV; |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | static 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 | */ |
| 712 | static struct vio_device_id viodasd_device_table[] __devinitdata = { |
Stephen Rothwell | fbabeb6 | 2006-05-15 13:41:22 +1000 | [diff] [blame] | 713 | { "block", "IBM,iSeries-viodasd" }, |
Stephen Rothwell | fb120da | 2005-08-17 16:42:59 +1000 | [diff] [blame] | 714 | { "", "" } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | MODULE_DEVICE_TABLE(vio, viodasd_device_table); |
Stephen Rothwell | 915124d | 2005-10-24 15:12:22 +1000 | [diff] [blame] | 717 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | static struct vio_driver viodasd_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | .id_table = viodasd_device_table, |
| 720 | .probe = viodasd_probe, |
Stephen Rothwell | 6fdf539 | 2005-10-24 14:53:21 +1000 | [diff] [blame] | 721 | .remove = viodasd_remove, |
| 722 | .driver = { |
| 723 | .name = "viodasd", |
Stephen Rothwell | 915124d | 2005-10-24 15:12:22 +1000 | [diff] [blame] | 724 | .owner = THIS_MODULE, |
Stephen Rothwell | 6fdf539 | 2005-10-24 14:53:21 +1000 | [diff] [blame] | 725 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | }; |
| 727 | |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 728 | static int need_delete_probe; |
| 729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | /* |
| 731 | * Initialize the whole device driver. Handle module and non-module |
| 732 | * versions |
| 733 | */ |
| 734 | static int __init viodasd_init(void) |
| 735 | { |
| 736 | int rc; |
| 737 | |
Stephen Rothwell | fb8b500 | 2006-12-15 15:40:08 +1100 | [diff] [blame] | 738 | if (!firmware_has_feature(FW_FEATURE_ISERIES)) { |
| 739 | rc = -ENODEV; |
| 740 | goto early_fail; |
| 741 | } |
| 742 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | /* Try to open to our host lp */ |
| 744 | if (viopath_hostLp == HvLpIndexInvalid) |
| 745 | vio_set_hostlp(); |
| 746 | |
| 747 | if (viopath_hostLp == HvLpIndexInvalid) { |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 748 | pr_warning("invalid hosting partition\n"); |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 749 | rc = -EIO; |
| 750 | goto early_fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 753 | pr_info("vers " VIOD_VERS ", hosting partition %d\n", viopath_hostLp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
| 755 | /* register the block device */ |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 756 | rc = register_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME); |
| 757 | if (rc) { |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 758 | pr_warning("Unable to get major number %d for %s\n", |
| 759 | VIODASD_MAJOR, VIOD_GENHD_NAME); |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 760 | goto early_fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | } |
| 762 | /* Actually open the path to the hosting partition */ |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 763 | rc = viopath_open(viopath_hostLp, viomajorsubtype_blockio, |
| 764 | VIOMAXREQ + 2); |
| 765 | if (rc) { |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 766 | pr_warning("error opening path to host partition %d\n", |
| 767 | viopath_hostLp); |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 768 | goto unregister_blk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | /* Initialize our request handler */ |
| 772 | vio_setHandler(viomajorsubtype_blockio, handle_block_event); |
| 773 | |
| 774 | rc = vio_register_driver(&viodasd_driver); |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 775 | if (rc) { |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 776 | pr_warning("vio_register_driver failed\n"); |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 777 | 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 | |
| 790 | unset_handler: |
| 791 | vio_clearHandler(viomajorsubtype_blockio); |
| 792 | viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2); |
| 793 | unregister_blk: |
| 794 | unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME); |
| 795 | early_fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | return rc; |
| 797 | } |
| 798 | module_init(viodasd_init); |
| 799 | |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 800 | void __exit viodasd_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | { |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 802 | if (need_delete_probe) |
| 803 | driver_remove_file(&viodasd_driver.driver, &driver_attr_probe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | vio_unregister_driver(&viodasd_driver); |
| 805 | vio_clearHandler(viomajorsubtype_blockio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2); |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 807 | unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | module_exit(viodasd_exit); |