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 | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame^] | 44 | #include <linux/smp_lock.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 | |
| 76 | static DEFINE_SPINLOCK(viodasd_spinlock); |
| 77 | |
| 78 | #define VIOMAXREQ 16 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | #define DEVICE_NO(cell) ((struct viodasd_device *)(cell) - &viodasd_devices[0]) |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | struct viodasd_waitevent { |
| 83 | struct completion com; |
| 84 | int rc; |
| 85 | u16 sub_result; |
| 86 | int max_disk; /* open */ |
| 87 | }; |
| 88 | |
| 89 | static 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 | */ |
| 114 | static int num_req_outstanding; |
| 115 | |
| 116 | /* |
| 117 | * This is our internal structure for keeping track of disk devices |
| 118 | */ |
| 119 | struct 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 Viro | f115a14 | 2008-03-02 10:22:07 -0500 | [diff] [blame] | 134 | static int viodasd_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
Al Viro | f115a14 | 2008-03-02 10:22:07 -0500 | [diff] [blame] | 136 | struct viodasd_device *d = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | HvLpEvent_Rc hvrc; |
| 138 | struct viodasd_waitevent we; |
| 139 | u16 flags = 0; |
| 140 | |
| 141 | if (d->read_only) { |
Al Viro | f115a14 | 2008-03-02 10:22:07 -0500 | [diff] [blame] | 142 | if (mode & FMODE_WRITE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 160 | pr_warning("HV open failed %d\n", (int)hvrc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 171 | pr_warning("bad rc opening disk: %d:0x%04x (%s)\n", |
| 172 | (int)we.rc, we.sub_result, err->msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | return -EIO; |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame^] | 179 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | /* |
| 192 | * External release entry point. |
| 193 | */ |
Al Viro | f115a14 | 2008-03-02 10:22:07 -0500 | [diff] [blame] | 194 | static int viodasd_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
Al Viro | f115a14 | 2008-03-02 10:22:07 -0500 | [diff] [blame] | 196 | struct viodasd_device *d = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | HvLpEvent_Rc hvrc; |
| 198 | |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame^] | 199 | lock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | /* 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 211 | pr_warning("HV close call failed %d\n", (int)hvrc); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame^] | 212 | |
| 213 | unlock_kernel(); |
| 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /* External ioctl entry point. |
| 220 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 221 | static int viodasd_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 223 | struct gendisk *disk = bdev->bd_disk; |
| 224 | struct viodasd_device *d = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Stephen Rothwell | 36a7003 | 2007-05-29 16:06:54 +1000 | [diff] [blame] | 226 | geo->sectors = d->sectors ? d->sectors : 32; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 227 | geo->heads = d->tracks ? d->tracks : 64; |
| 228 | geo->cylinders = d->cylinders ? d->cylinders : |
Stephen Rothwell | 36a7003 | 2007-05-29 16:06:54 +1000 | [diff] [blame] | 229 | get_capacity(disk) / (geo->sectors * geo->heads); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 231 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Our file operations table |
| 236 | */ |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 237 | static const struct block_device_operations viodasd_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | .owner = THIS_MODULE, |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame^] | 239 | .open = viodasd_unlocked_open, |
Al Viro | f115a14 | 2008-03-02 10:22:07 -0500 | [diff] [blame] | 240 | .release = viodasd_release, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 241 | .getgeo = viodasd_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | /* |
| 245 | * End a request |
| 246 | */ |
Kiyoshi Ueda | b2aec24 | 2007-12-11 17:47:14 -0500 | [diff] [blame] | 247 | static void viodasd_end_request(struct request *req, int error, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | int num_sectors) |
| 249 | { |
Kiyoshi Ueda | b2aec24 | 2007-12-11 17:47:14 -0500 | [diff] [blame] | 250 | __blk_end_request(req, error, num_sectors << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Send an actual I/O request to OS/400 |
| 255 | */ |
| 256 | static 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 Rothwell | 677f8c0 | 2006-01-12 13:47:43 +1100 | [diff] [blame] | 264 | struct HvLpEvent *hev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | struct scatterlist sg[VIOMAXBLOCKDMA]; |
| 266 | int sgindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | struct viodasd_device *d; |
| 268 | unsigned long flags; |
| 269 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 270 | start = (u64)blk_rq_pos(req) << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | if (rq_data_dir(req) == READ) { |
| 273 | direction = DMA_FROM_DEVICE; |
| 274 | viocmd = viomajorsubtype_blockio | vioblockread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | } else { |
| 276 | direction = DMA_TO_DEVICE; |
| 277 | viocmd = viomajorsubtype_blockio | vioblockwrite; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | d = req->rq_disk->private_data; |
| 281 | |
| 282 | /* Now build the scatter-gather list */ |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 283 | sg_init_table(sg, VIOMAXBLOCKDMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 306 | pr_warning("error allocating disk event buffer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | 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 Rothwell | 677f8c0 | 2006-01-12 13:47:43 +1100 | [diff] [blame] | 316 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | offsetof(struct vioblocklpevent, u.rw_data.dma_info) + |
| 325 | (sizeof(bevent->u.rw_data.dma_info[0]) * nsg) - 1; |
Stephen Rothwell | 677f8c0 | 2006-01-12 13:47:43 +1100 | [diff] [blame] | 326 | hev->xSourceInstanceId = viopath_sourceinst(viopath_hostLp); |
| 327 | hev->xTargetInstanceId = viopath_targetinst(viopath_hostLp); |
| 328 | hev->xCorrelationToken = (u64)req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 350 | pr_warning("error sending disk event to OS/400 (rc %d)\n", |
| 351 | (int)hvrc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | goto error_ret; |
| 353 | } |
| 354 | spin_unlock_irqrestore(&viodasd_spinlock, flags); |
| 355 | return 0; |
| 356 | |
| 357 | error_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 Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 367 | static void do_viodasd_request(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
| 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 Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 377 | req = blk_fetch_request(q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | if (req == NULL) |
| 379 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | /* check that request contains a valid command */ |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 381 | if (req->cmd_type != REQ_TYPE_FS) { |
Tejun Heo | 5b93629 | 2009-05-07 22:24:38 +0900 | [diff] [blame] | 382 | viodasd_end_request(req, -EIO, blk_rq_sectors(req)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | continue; |
| 384 | } |
| 385 | /* Try sending the request */ |
| 386 | if (send_request(req) != 0) |
Tejun Heo | 5b93629 | 2009-05-07 22:24:38 +0900 | [diff] [blame] | 387 | viodasd_end_request(req, -EIO, blk_rq_sectors(req)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Probe a single disk and fill in the viodasd_device structure |
| 393 | * for it. |
| 394 | */ |
Stephen Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 395 | static int probe_disk(struct viodasd_device *d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | { |
| 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 | |
| 404 | retry: |
| 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 418 | pr_warning("bad rc on HV open %d\n", (int)hvrc); |
Stephen Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 419 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | wait_for_completion(&we.com); |
| 423 | |
| 424 | if (we.rc != 0) { |
| 425 | if (flags != 0) |
Stephen Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 426 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | /* try again with read only flag set */ |
| 428 | flags = vioblockflags_ro; |
| 429 | goto retry; |
| 430 | } |
| 431 | if (we.max_disk > (MAX_DISKNO - 1)) { |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 432 | printk_once(KERN_INFO pr_fmt("Only examining the first %d of %d disks connected\n"), |
| 433 | MAX_DISKNO, we.max_disk + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* Send the close event to OS/400. We DON'T expect a response */ |
| 437 | hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp, |
| 438 | HvLpEvent_Type_VirtualIo, |
| 439 | viomajorsubtype_blockio | vioblockclose, |
| 440 | HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck, |
| 441 | viopath_sourceinst(viopath_hostLp), |
| 442 | viopath_targetinst(viopath_hostLp), |
| 443 | 0, VIOVERSION << 16, |
| 444 | ((u64)dev_no << 48) | ((u64)flags << 32), |
| 445 | 0, 0, 0); |
| 446 | if (hvrc != 0) { |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 447 | 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] | 448 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | } |
Stephen Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 450 | |
| 451 | if (d->dev == NULL) { |
| 452 | /* this is when we reprobe for new disks */ |
| 453 | if (vio_create_viodasd(dev_no) == NULL) { |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 454 | pr_warning("cannot allocate virtual device for disk %d\n", |
| 455 | dev_no); |
Stephen Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 456 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | /* 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 470 | pr_warning("cannot allocate queue for disk %d\n", dev_no); |
Stephen Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 471 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | g = alloc_disk(1 << PARTITION_SHIFT); |
| 474 | if (g == NULL) { |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 475 | pr_warning("cannot allocate disk structure for disk %d\n", |
| 476 | dev_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | blk_cleanup_queue(q); |
Stephen Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 478 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | d->disk = g; |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 482 | blk_queue_max_segments(q, VIOMAXBLOCKDMA); |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 483 | blk_queue_max_hw_sectors(q, VIODASD_MAXSECTORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 499 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
| 506 | /* register us in the global list */ |
| 507 | add_disk(g); |
Stephen Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 508 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /* returns the total number of scatterlist elements converted */ |
| 512 | static 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 Herrenschmidt | 25c0a7b | 2008-03-12 17:23:56 +1100 | [diff] [blame] | 526 | sg_init_table(sg, VIOMAXBLOCKDMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | 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 | */ |
| 539 | static 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 | */ |
| 555 | static 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 Ueda | b2aec24 | 2007-12-11 17:47:14 -0500 | [diff] [blame] | 585 | error = (event->xRc == HvLpEvent_Rc_Good) ? 0 : -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | if (error) { |
| 587 | const struct vio_error_entry *err; |
| 588 | err = vio_lookup_rc(viodasd_err_table, bevent->sub_result); |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 589 | pr_warning("read/write error %d:0x%04x (%s)\n", |
| 590 | event->xRc, bevent->sub_result, err->msg); |
Tejun Heo | 5b93629 | 2009-05-07 22:24:38 +0900 | [diff] [blame] | 591 | num_sect = blk_rq_sectors(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | } |
| 593 | qlock = req->q->queue_lock; |
| 594 | spin_lock_irqsave(qlock, irq_flags); |
Kiyoshi Ueda | b2aec24 | 2007-12-11 17:47:14 -0500 | [diff] [blame] | 595 | viodasd_end_request(req, error, num_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | 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 */ |
| 605 | static 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 Rothwell | 677f8c0 | 2006-01-12 13:47:43 +1100 | [diff] [blame] | 614 | if (hvlpevent_is_int(event)) { |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 615 | pr_warning("Yikes! got an int in viodasd event handler!\n"); |
Stephen Rothwell | 677f8c0 | 2006-01-12 13:47:43 +1100 | [diff] [blame] | 616 | if (hvlpevent_need_ack(event)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | 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 Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 658 | pr_warning("invalid subtype!"); |
Stephen Rothwell | 677f8c0 | 2006-01-12 13:47:43 +1100 | [diff] [blame] | 659 | if (hvlpevent_need_ack(event)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | event->xRc = HvLpEvent_Rc_InvalidSubtype; |
| 661 | HvCallEvent_ackLpEvent(event); |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | /* |
| 667 | * Get the driver to reprobe for more disks. |
| 668 | */ |
| 669 | static 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 | } |
| 680 | static DRIVER_ATTR(probe, S_IWUSR, NULL, probe_disks); |
| 681 | |
| 682 | static 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 Rothwell | 8251b4c | 2007-10-11 14:59:54 +1000 | [diff] [blame] | 687 | if (!probe_disk(d)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | return -ENODEV; |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | static 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 | */ |
| 711 | static struct vio_device_id viodasd_device_table[] __devinitdata = { |
Stephen Rothwell | fbabeb6 | 2006-05-15 13:41:22 +1000 | [diff] [blame] | 712 | { "block", "IBM,iSeries-viodasd" }, |
Stephen Rothwell | fb120da | 2005-08-17 16:42:59 +1000 | [diff] [blame] | 713 | { "", "" } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | MODULE_DEVICE_TABLE(vio, viodasd_device_table); |
Stephen Rothwell | 915124d | 2005-10-24 15:12:22 +1000 | [diff] [blame] | 716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | static struct vio_driver viodasd_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | .id_table = viodasd_device_table, |
| 719 | .probe = viodasd_probe, |
Stephen Rothwell | 6fdf539 | 2005-10-24 14:53:21 +1000 | [diff] [blame] | 720 | .remove = viodasd_remove, |
| 721 | .driver = { |
| 722 | .name = "viodasd", |
Stephen Rothwell | 915124d | 2005-10-24 15:12:22 +1000 | [diff] [blame] | 723 | .owner = THIS_MODULE, |
Stephen Rothwell | 6fdf539 | 2005-10-24 14:53:21 +1000 | [diff] [blame] | 724 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | }; |
| 726 | |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 727 | static int need_delete_probe; |
| 728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | /* |
| 730 | * Initialize the whole device driver. Handle module and non-module |
| 731 | * versions |
| 732 | */ |
| 733 | static int __init viodasd_init(void) |
| 734 | { |
| 735 | int rc; |
| 736 | |
Stephen Rothwell | fb8b500 | 2006-12-15 15:40:08 +1100 | [diff] [blame] | 737 | if (!firmware_has_feature(FW_FEATURE_ISERIES)) { |
| 738 | rc = -ENODEV; |
| 739 | goto early_fail; |
| 740 | } |
| 741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | /* Try to open to our host lp */ |
| 743 | if (viopath_hostLp == HvLpIndexInvalid) |
| 744 | vio_set_hostlp(); |
| 745 | |
| 746 | if (viopath_hostLp == HvLpIndexInvalid) { |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 747 | pr_warning("invalid hosting partition\n"); |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 748 | rc = -EIO; |
| 749 | goto early_fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 752 | pr_info("vers " VIOD_VERS ", hosting partition %d\n", viopath_hostLp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
| 754 | /* register the block device */ |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 755 | rc = register_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME); |
| 756 | if (rc) { |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 757 | pr_warning("Unable to get major number %d for %s\n", |
| 758 | VIODASD_MAJOR, VIOD_GENHD_NAME); |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 759 | goto early_fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | } |
| 761 | /* Actually open the path to the hosting partition */ |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 762 | rc = viopath_open(viopath_hostLp, viomajorsubtype_blockio, |
| 763 | VIOMAXREQ + 2); |
| 764 | if (rc) { |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 765 | pr_warning("error opening path to host partition %d\n", |
| 766 | viopath_hostLp); |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 767 | goto unregister_blk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /* Initialize our request handler */ |
| 771 | vio_setHandler(viomajorsubtype_blockio, handle_block_event); |
| 772 | |
| 773 | rc = vio_register_driver(&viodasd_driver); |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 774 | if (rc) { |
Joe Perches | dc942ce | 2009-12-22 21:22:59 +0000 | [diff] [blame] | 775 | pr_warning("vio_register_driver failed\n"); |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 776 | 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 | |
| 789 | unset_handler: |
| 790 | vio_clearHandler(viomajorsubtype_blockio); |
| 791 | viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2); |
| 792 | unregister_blk: |
| 793 | unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME); |
| 794 | early_fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | return rc; |
| 796 | } |
| 797 | module_init(viodasd_init); |
| 798 | |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 799 | void __exit viodasd_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | { |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 801 | if (need_delete_probe) |
| 802 | driver_remove_file(&viodasd_driver.driver, &driver_attr_probe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | vio_unregister_driver(&viodasd_driver); |
| 804 | vio_clearHandler(viomajorsubtype_blockio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2); |
Stephen Rothwell | f9df68e | 2006-11-13 14:43:17 +1100 | [diff] [blame] | 806 | unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | module_exit(viodasd_exit); |