Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1 | /* |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 2 | * Copyright (c) 2009, Microsoft Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 15 | * Place - Suite 330, Boston, MA 02111-1307 USA. |
| 16 | * |
| 17 | * Authors: |
| 18 | * Haiyang Zhang <haiyangz@microsoft.com> |
| 19 | * Hank Janssen <hjanssen@microsoft.com> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 20 | */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 21 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/device.h> |
| 25 | #include <linux/blkdev.h> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 26 | #include <scsi/scsi.h> |
| 27 | #include <scsi/scsi_cmnd.h> |
| 28 | #include <scsi/scsi_host.h> |
| 29 | #include <scsi/scsi_device.h> |
| 30 | #include <scsi/scsi_tcq.h> |
| 31 | #include <scsi/scsi_eh.h> |
| 32 | #include <scsi/scsi_devinfo.h> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 33 | #include <scsi/scsi_dbg.h> |
Greg Kroah-Hartman | 4983b39 | 2009-08-19 16:14:47 -0700 | [diff] [blame] | 34 | #include "osd.h" |
Greg Kroah-Hartman | 645954c | 2009-08-28 16:22:59 -0700 | [diff] [blame] | 35 | #include "logging.h" |
Hank Janssen | 26c14cc | 2010-02-11 23:02:42 +0000 | [diff] [blame] | 36 | #include "VersionInfo.h" |
Greg Kroah-Hartman | 870cde8 | 2009-08-19 16:21:28 -0700 | [diff] [blame] | 37 | #include "vmbus.h" |
Greg Kroah-Hartman | 731a788 | 2009-08-28 16:23:49 -0700 | [diff] [blame] | 38 | #include "StorVscApi.h" |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 39 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 40 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 41 | struct host_device_context { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 42 | /* must be 1st field |
| 43 | * FIXME this is a bug */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 44 | /* point back to our device context */ |
Haiyang Zhang | f916a34 | 2010-02-17 20:58:47 +0000 | [diff] [blame] | 45 | struct vm_device *device_ctx; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 46 | struct kmem_cache *request_pool; |
| 47 | unsigned int port; |
| 48 | unsigned char path; |
| 49 | unsigned char target; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | struct storvsc_cmd_request { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 53 | struct list_head entry; |
| 54 | struct scsi_cmnd *cmd; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 55 | |
| 56 | unsigned int bounce_sgl_count; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 57 | struct scatterlist *bounce_sgl; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 58 | |
Nicolas Palix | 0b3f683 | 2009-07-29 14:10:19 +0200 | [diff] [blame] | 59 | struct hv_storvsc_request request; |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 60 | /* !!!DO NOT ADD ANYTHING BELOW HERE!!! */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 61 | /* The extension buffer falls right here and is pointed to by |
| 62 | * request.Extension; |
| 63 | * Which sounds like a very bad design... */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | struct storvsc_driver_context { |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 67 | /* !! These must be the first 2 fields !! */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 68 | /* FIXME this is a bug... */ |
| 69 | struct driver_context drv_ctx; |
Greg Kroah-Hartman | 9f0c7d2 | 2009-08-28 16:20:07 -0700 | [diff] [blame] | 70 | struct storvsc_driver_object drv_obj; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 73 | /* Static decl */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 74 | static int storvsc_probe(struct device *dev); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 75 | static int storvsc_queuecommand(struct scsi_cmnd *scmnd, |
| 76 | void (*done)(struct scsi_cmnd *)); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 77 | static int storvsc_device_alloc(struct scsi_device *); |
| 78 | static int storvsc_device_configure(struct scsi_device *); |
| 79 | static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 80 | static int storvsc_remove(struct device *dev); |
| 81 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 82 | static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl, |
| 83 | unsigned int sg_count, |
| 84 | unsigned int len); |
| 85 | static void destroy_bounce_buffer(struct scatterlist *sgl, |
| 86 | unsigned int sg_count); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 87 | static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 88 | static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl, |
| 89 | struct scatterlist *bounce_sgl, |
| 90 | unsigned int orig_sgl_count); |
| 91 | static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl, |
| 92 | struct scatterlist *bounce_sgl, |
| 93 | unsigned int orig_sgl_count); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 94 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 95 | static int storvsc_get_chs(struct scsi_device *sdev, struct block_device *bdev, |
| 96 | sector_t capacity, int *info); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 97 | |
| 98 | |
| 99 | static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE; |
| 100 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 101 | /* The one and only one */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 102 | static struct storvsc_driver_context g_storvsc_drv; |
| 103 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 104 | /* Scsi driver */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 105 | static struct scsi_host_template scsi_driver = { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 106 | .module = THIS_MODULE, |
| 107 | .name = "storvsc_host_t", |
| 108 | .bios_param = storvsc_get_chs, |
| 109 | .queuecommand = storvsc_queuecommand, |
| 110 | .eh_host_reset_handler = storvsc_host_reset_handler, |
| 111 | .slave_alloc = storvsc_device_alloc, |
| 112 | .slave_configure = storvsc_device_configure, |
| 113 | .cmd_per_lun = 1, |
| 114 | /* 64 max_queue * 1 target */ |
Lars Lindley | 0686e4f | 2010-03-11 23:51:23 +0100 | [diff] [blame^] | 115 | .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS, |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 116 | .this_id = -1, |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 117 | /* no use setting to 0 since ll_blk_rw reset it to 1 */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 118 | /* currently 32 */ |
| 119 | .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT, |
| 120 | /* |
| 121 | * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge |
| 122 | * into 1 sg element. If set, we must limit the max_segment_size to |
| 123 | * PAGE_SIZE, otherwise we may get 1 sg element that represents |
| 124 | * multiple |
| 125 | */ |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 126 | /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 127 | .use_clustering = ENABLE_CLUSTERING, |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 128 | /* Make sure we dont get a sg segment crosses a page boundary */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 129 | .dma_boundary = PAGE_SIZE-1, |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 133 | /* |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 134 | * storvsc_drv_init - StorVsc driver initialization. |
| 135 | */ |
Greg Kroah-Hartman | 21707be | 2009-09-02 11:53:59 -0700 | [diff] [blame] | 136 | static int storvsc_drv_init(int (*drv_init)(struct hv_driver *drv)) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 137 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 138 | int ret; |
Greg Kroah-Hartman | 9f0c7d2 | 2009-08-28 16:20:07 -0700 | [diff] [blame] | 139 | struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 140 | struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 141 | |
| 142 | DPRINT_ENTER(STORVSC_DRV); |
| 143 | |
| 144 | vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface); |
| 145 | |
| 146 | storvsc_drv_obj->RingBufferSize = storvsc_ringbuffer_size; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 147 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 148 | /* Callback to client driver to complete the initialization */ |
Greg Kroah-Hartman | 21707be | 2009-09-02 11:53:59 -0700 | [diff] [blame] | 149 | drv_init(&storvsc_drv_obj->Base); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 150 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 151 | DPRINT_INFO(STORVSC_DRV, |
| 152 | "request extension size %u, max outstanding reqs %u", |
| 153 | storvsc_drv_obj->RequestExtSize, |
| 154 | storvsc_drv_obj->MaxOutstandingRequestsPerChannel); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 155 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 156 | if (storvsc_drv_obj->MaxOutstandingRequestsPerChannel < |
| 157 | STORVSC_MAX_IO_REQUESTS) { |
| 158 | DPRINT_ERR(STORVSC_DRV, |
| 159 | "The number of outstanding io requests (%d) " |
| 160 | "is larger than that supported (%d) internally.", |
| 161 | STORVSC_MAX_IO_REQUESTS, |
| 162 | storvsc_drv_obj->MaxOutstandingRequestsPerChannel); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | drv_ctx->driver.name = storvsc_drv_obj->Base.name; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 167 | memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType, |
| 168 | sizeof(struct hv_guid)); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 169 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 170 | drv_ctx->probe = storvsc_probe; |
| 171 | drv_ctx->remove = storvsc_remove; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 172 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 173 | /* The driver belongs to vmbus */ |
Bill Pemberton | 5d48a1c | 2009-07-27 16:47:36 -0400 | [diff] [blame] | 174 | ret = vmbus_child_driver_register(drv_ctx); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 175 | |
| 176 | DPRINT_EXIT(STORVSC_DRV); |
| 177 | |
| 178 | return ret; |
| 179 | } |
| 180 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 181 | static int storvsc_drv_exit_cb(struct device *dev, void *data) |
| 182 | { |
| 183 | struct device **curr = (struct device **)data; |
| 184 | *curr = dev; |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 185 | return 1; /* stop iterating */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Greg Kroah-Hartman | bd1de70 | 2009-07-29 09:04:51 -0700 | [diff] [blame] | 188 | static void storvsc_drv_exit(void) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 189 | { |
Greg Kroah-Hartman | 9f0c7d2 | 2009-08-28 16:20:07 -0700 | [diff] [blame] | 190 | struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 191 | struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx; |
| 192 | struct device *current_dev = NULL; |
Bill Pemberton | 2295ba2 | 2009-07-28 13:46:22 -0400 | [diff] [blame] | 193 | int ret; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 194 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 195 | DPRINT_ENTER(STORVSC_DRV); |
| 196 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 197 | while (1) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 198 | current_dev = NULL; |
| 199 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 200 | /* Get the device */ |
Bill Pemberton | 2295ba2 | 2009-07-28 13:46:22 -0400 | [diff] [blame] | 201 | ret = driver_for_each_device(&drv_ctx->driver, NULL, |
| 202 | (void *) ¤t_dev, |
| 203 | storvsc_drv_exit_cb); |
| 204 | |
| 205 | if (ret) |
| 206 | DPRINT_WARN(STORVSC_DRV, |
| 207 | "driver_for_each_device returned %d", ret); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 208 | |
| 209 | if (current_dev == NULL) |
| 210 | break; |
| 211 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 212 | /* Initiate removal from the top-down */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 213 | device_unregister(current_dev); |
| 214 | } |
| 215 | |
| 216 | if (storvsc_drv_obj->Base.OnCleanup) |
| 217 | storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base); |
| 218 | |
| 219 | vmbus_child_driver_unregister(drv_ctx); |
| 220 | |
| 221 | DPRINT_EXIT(STORVSC_DRV); |
| 222 | |
| 223 | return; |
| 224 | } |
| 225 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 226 | /* |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 227 | * storvsc_probe - Add a new device for this driver |
| 228 | */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 229 | static int storvsc_probe(struct device *device) |
| 230 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 231 | int ret; |
| 232 | struct driver_context *driver_ctx = |
| 233 | driver_to_driver_context(device->driver); |
| 234 | struct storvsc_driver_context *storvsc_drv_ctx = |
| 235 | (struct storvsc_driver_context *)driver_ctx; |
| 236 | struct storvsc_driver_object *storvsc_drv_obj = |
| 237 | &storvsc_drv_ctx->drv_obj; |
Haiyang Zhang | f916a34 | 2010-02-17 20:58:47 +0000 | [diff] [blame] | 238 | struct vm_device *device_ctx = device_to_vm_device(device); |
Nicolas Palix | 3d3b551 | 2009-07-28 17:32:53 +0200 | [diff] [blame] | 239 | struct hv_device *device_obj = &device_ctx->device_obj; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 240 | struct Scsi_Host *host; |
| 241 | struct host_device_context *host_device_ctx; |
Greg Kroah-Hartman | 9f0c7d2 | 2009-08-28 16:20:07 -0700 | [diff] [blame] | 242 | struct storvsc_device_info device_info; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 243 | |
| 244 | DPRINT_ENTER(STORVSC_DRV); |
| 245 | |
| 246 | if (!storvsc_drv_obj->Base.OnDeviceAdd) |
| 247 | return -1; |
| 248 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 249 | host = scsi_host_alloc(&scsi_driver, |
| 250 | sizeof(struct host_device_context)); |
| 251 | if (!host) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 252 | DPRINT_ERR(STORVSC_DRV, "unable to allocate scsi host object"); |
| 253 | return -ENOMEM; |
| 254 | } |
| 255 | |
Greg Kroah-Hartman | 0883c52 | 2009-07-24 10:58:22 -0700 | [diff] [blame] | 256 | dev_set_drvdata(device, host); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 257 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 258 | host_device_ctx = (struct host_device_context *)host->hostdata; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 259 | memset(host_device_ctx, 0, sizeof(struct host_device_context)); |
| 260 | |
| 261 | host_device_ctx->port = host->host_no; |
| 262 | host_device_ctx->device_ctx = device_ctx; |
| 263 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 264 | host_device_ctx->request_pool = |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 265 | kmem_cache_create(dev_name(&device_ctx->device), |
| 266 | sizeof(struct storvsc_cmd_request) + |
| 267 | storvsc_drv_obj->RequestExtSize, 0, |
| 268 | SLAB_HWCACHE_ALIGN, NULL); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 269 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 270 | if (!host_device_ctx->request_pool) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 271 | scsi_host_put(host); |
| 272 | DPRINT_EXIT(STORVSC_DRV); |
| 273 | |
| 274 | return -ENOMEM; |
| 275 | } |
| 276 | |
| 277 | device_info.PortNumber = host->host_no; |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 278 | /* Call to the vsc driver to add the device */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 279 | ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj, |
| 280 | (void *)&device_info); |
| 281 | if (ret != 0) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 282 | DPRINT_ERR(STORVSC_DRV, "unable to add scsi vsc device"); |
| 283 | kmem_cache_destroy(host_device_ctx->request_pool); |
| 284 | scsi_host_put(host); |
| 285 | DPRINT_EXIT(STORVSC_DRV); |
| 286 | |
| 287 | return -1; |
| 288 | } |
| 289 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 290 | /* host_device_ctx->port = device_info.PortNumber; */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 291 | host_device_ctx->path = device_info.PathId; |
| 292 | host_device_ctx->target = device_info.TargetId; |
| 293 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 294 | /* max # of devices per target */ |
| 295 | host->max_lun = STORVSC_MAX_LUNS_PER_TARGET; |
| 296 | /* max # of targets per channel */ |
| 297 | host->max_id = STORVSC_MAX_TARGETS; |
| 298 | /* max # of channels */ |
| 299 | host->max_channel = STORVSC_MAX_CHANNELS - 1; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 300 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 301 | /* Register the HBA and start the scsi bus scan */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 302 | ret = scsi_add_host(host, device); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 303 | if (ret != 0) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 304 | DPRINT_ERR(STORVSC_DRV, "unable to add scsi host device"); |
| 305 | |
| 306 | storvsc_drv_obj->Base.OnDeviceRemove(device_obj); |
| 307 | |
| 308 | kmem_cache_destroy(host_device_ctx->request_pool); |
| 309 | scsi_host_put(host); |
| 310 | DPRINT_EXIT(STORVSC_DRV); |
| 311 | |
| 312 | return -1; |
| 313 | } |
| 314 | |
| 315 | scsi_scan_host(host); |
| 316 | |
| 317 | DPRINT_EXIT(STORVSC_DRV); |
| 318 | |
| 319 | return ret; |
| 320 | } |
| 321 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 322 | /* |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 323 | * storvsc_remove - Callback when our device is removed |
| 324 | */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 325 | static int storvsc_remove(struct device *device) |
| 326 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 327 | int ret; |
| 328 | struct driver_context *driver_ctx = |
| 329 | driver_to_driver_context(device->driver); |
| 330 | struct storvsc_driver_context *storvsc_drv_ctx = |
| 331 | (struct storvsc_driver_context *)driver_ctx; |
| 332 | struct storvsc_driver_object *storvsc_drv_obj = |
| 333 | &storvsc_drv_ctx->drv_obj; |
Haiyang Zhang | f916a34 | 2010-02-17 20:58:47 +0000 | [diff] [blame] | 334 | struct vm_device *device_ctx = device_to_vm_device(device); |
Nicolas Palix | 3d3b551 | 2009-07-28 17:32:53 +0200 | [diff] [blame] | 335 | struct hv_device *device_obj = &device_ctx->device_obj; |
Greg Kroah-Hartman | 0883c52 | 2009-07-24 10:58:22 -0700 | [diff] [blame] | 336 | struct Scsi_Host *host = dev_get_drvdata(device); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 337 | struct host_device_context *host_device_ctx = |
| 338 | (struct host_device_context *)host->hostdata; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 339 | |
| 340 | |
| 341 | DPRINT_ENTER(STORVSC_DRV); |
| 342 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 343 | if (!storvsc_drv_obj->Base.OnDeviceRemove) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 344 | DPRINT_EXIT(STORVSC_DRV); |
| 345 | return -1; |
| 346 | } |
| 347 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 348 | /* |
| 349 | * Call to the vsc driver to let it know that the device is being |
| 350 | * removed |
| 351 | */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 352 | ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 353 | if (ret != 0) { |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 354 | /* TODO: */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 355 | DPRINT_ERR(STORVSC, "unable to remove vsc device (ret %d)", |
| 356 | ret); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 359 | if (host_device_ctx->request_pool) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 360 | kmem_cache_destroy(host_device_ctx->request_pool); |
| 361 | host_device_ctx->request_pool = NULL; |
| 362 | } |
| 363 | |
| 364 | DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host); |
| 365 | scsi_remove_host(host); |
| 366 | |
| 367 | DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host); |
| 368 | scsi_host_put(host); |
| 369 | |
| 370 | DPRINT_EXIT(STORVSC_DRV); |
| 371 | |
| 372 | return ret; |
| 373 | } |
| 374 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 375 | /* |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 376 | * storvsc_commmand_completion - Command completion processing |
| 377 | */ |
Nicolas Palix | 0b3f683 | 2009-07-29 14:10:19 +0200 | [diff] [blame] | 378 | static void storvsc_commmand_completion(struct hv_storvsc_request *request) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 379 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 380 | struct storvsc_cmd_request *cmd_request = |
| 381 | (struct storvsc_cmd_request *)request->Context; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 382 | struct scsi_cmnd *scmnd = cmd_request->cmd; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 383 | struct host_device_context *host_device_ctx = |
| 384 | (struct host_device_context *)scmnd->device->host->hostdata; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 385 | void (*scsi_done_fn)(struct scsi_cmnd *); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 386 | struct scsi_sense_hdr sense_hdr; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 387 | |
| 388 | ASSERT(request == &cmd_request->request); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 389 | ASSERT((unsigned long)scmnd->host_scribble == |
| 390 | (unsigned long)cmd_request); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 391 | ASSERT(scmnd); |
| 392 | ASSERT(scmnd->scsi_done); |
| 393 | |
| 394 | DPRINT_ENTER(STORVSC_DRV); |
| 395 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 396 | if (cmd_request->bounce_sgl_count) { |
| 397 | /* using bounce buffer */ |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 398 | /* printk("copy_from_bounce_buffer\n"); */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 399 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 400 | /* FIXME: We can optimize on writes by just skipping this */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 401 | copy_from_bounce_buffer(scsi_sglist(scmnd), |
| 402 | cmd_request->bounce_sgl, |
| 403 | scsi_sg_count(scmnd)); |
| 404 | destroy_bounce_buffer(cmd_request->bounce_sgl, |
| 405 | cmd_request->bounce_sgl_count); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | scmnd->result = request->Status; |
| 409 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 410 | if (scmnd->result) { |
| 411 | if (scsi_normalize_sense(scmnd->sense_buffer, |
| 412 | request->SenseBufferSize, &sense_hdr)) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 413 | scsi_print_sense_hdr("storvsc", &sense_hdr); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | ASSERT(request->BytesXfer <= request->DataBuffer.Length); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 417 | scsi_set_resid(scmnd, request->DataBuffer.Length - request->BytesXfer); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 418 | |
| 419 | scsi_done_fn = scmnd->scsi_done; |
| 420 | |
| 421 | scmnd->host_scribble = NULL; |
| 422 | scmnd->scsi_done = NULL; |
| 423 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 424 | /* !!DO NOT MODIFY the scmnd after this call */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 425 | scsi_done_fn(scmnd); |
| 426 | |
| 427 | kmem_cache_free(host_device_ctx->request_pool, cmd_request); |
| 428 | |
| 429 | DPRINT_EXIT(STORVSC_DRV); |
| 430 | } |
| 431 | |
| 432 | static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count) |
| 433 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 434 | int i; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 435 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 436 | /* No need to check */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 437 | if (sg_count < 2) |
| 438 | return -1; |
| 439 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 440 | /* We have at least 2 sg entries */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 441 | for (i = 0; i < sg_count; i++) { |
| 442 | if (i == 0) { |
| 443 | /* make sure 1st one does not have hole */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 444 | if (sgl[i].offset + sgl[i].length != PAGE_SIZE) |
| 445 | return i; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 446 | } else if (i == sg_count - 1) { |
| 447 | /* make sure last one does not have hole */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 448 | if (sgl[i].offset != 0) |
| 449 | return i; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 450 | } else { |
| 451 | /* make sure no hole in the middle */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 452 | if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 453 | return i; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | return -1; |
| 457 | } |
| 458 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 459 | static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl, |
| 460 | unsigned int sg_count, |
| 461 | unsigned int len) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 462 | { |
| 463 | int i; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 464 | int num_pages; |
| 465 | struct scatterlist *bounce_sgl; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 466 | struct page *page_buf; |
| 467 | |
| 468 | num_pages = ALIGN_UP(len, PAGE_SIZE) >> PAGE_SHIFT; |
| 469 | |
Nicolas Palix | 06da0bc | 2009-07-22 15:39:13 +0200 | [diff] [blame] | 470 | bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 471 | if (!bounce_sgl) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 472 | return NULL; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 473 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 474 | for (i = 0; i < num_pages; i++) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 475 | page_buf = alloc_page(GFP_ATOMIC); |
| 476 | if (!page_buf) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 477 | goto cleanup; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 478 | sg_set_page(&bounce_sgl[i], page_buf, 0, 0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | return bounce_sgl; |
| 482 | |
| 483 | cleanup: |
| 484 | destroy_bounce_buffer(bounce_sgl, num_pages); |
| 485 | return NULL; |
| 486 | } |
| 487 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 488 | static void destroy_bounce_buffer(struct scatterlist *sgl, |
| 489 | unsigned int sg_count) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 490 | { |
| 491 | int i; |
| 492 | struct page *page_buf; |
| 493 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 494 | for (i = 0; i < sg_count; i++) { |
| 495 | page_buf = sg_page((&sgl[i])); |
| 496 | if (page_buf != NULL) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 497 | __free_page(page_buf); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | kfree(sgl); |
| 501 | } |
| 502 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 503 | /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 504 | static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl, |
| 505 | struct scatterlist *bounce_sgl, |
| 506 | unsigned int orig_sgl_count) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 507 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 508 | int i; |
| 509 | int j = 0; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 510 | unsigned long src, dest; |
| 511 | unsigned int srclen, destlen, copylen; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 512 | unsigned int total_copied = 0; |
| 513 | unsigned long bounce_addr = 0; |
| 514 | unsigned long src_addr = 0; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 515 | unsigned long flags; |
| 516 | |
| 517 | local_irq_save(flags); |
| 518 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 519 | for (i = 0; i < orig_sgl_count; i++) { |
| 520 | src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), |
| 521 | KM_IRQ0) + orig_sgl[i].offset; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 522 | src = src_addr; |
| 523 | srclen = orig_sgl[i].length; |
| 524 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 525 | ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); |
| 526 | |
| 527 | if (j == 0) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 528 | bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 529 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 530 | while (srclen) { |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 531 | /* assume bounce offset always == 0 */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 532 | dest = bounce_addr + bounce_sgl[j].length; |
| 533 | destlen = PAGE_SIZE - bounce_sgl[j].length; |
| 534 | |
Greg Kroah-Hartman | fc6a4b2 | 2009-07-15 11:05:14 -0700 | [diff] [blame] | 535 | copylen = min(srclen, destlen); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 536 | memcpy((void *)dest, (void *)src, copylen); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 537 | |
| 538 | total_copied += copylen; |
| 539 | bounce_sgl[j].length += copylen; |
| 540 | srclen -= copylen; |
| 541 | src += copylen; |
| 542 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 543 | if (bounce_sgl[j].length == PAGE_SIZE) { |
| 544 | /* full..move to next entry */ |
| 545 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 546 | j++; |
| 547 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 548 | /* if we need to use another bounce buffer */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 549 | if (srclen || i != orig_sgl_count - 1) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 550 | bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 551 | } else if (srclen == 0 && i == orig_sgl_count - 1) { |
| 552 | /* unmap the last bounce that is < PAGE_SIZE */ |
| 553 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 557 | kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | local_irq_restore(flags); |
| 561 | |
| 562 | return total_copied; |
| 563 | } |
| 564 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 565 | /* Assume the original sgl has enough room */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 566 | static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl, |
| 567 | struct scatterlist *bounce_sgl, |
| 568 | unsigned int orig_sgl_count) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 569 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 570 | int i; |
| 571 | int j = 0; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 572 | unsigned long src, dest; |
| 573 | unsigned int srclen, destlen, copylen; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 574 | unsigned int total_copied = 0; |
| 575 | unsigned long bounce_addr = 0; |
| 576 | unsigned long dest_addr = 0; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 577 | unsigned long flags; |
| 578 | |
| 579 | local_irq_save(flags); |
| 580 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 581 | for (i = 0; i < orig_sgl_count; i++) { |
| 582 | dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), |
| 583 | KM_IRQ0) + orig_sgl[i].offset; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 584 | dest = dest_addr; |
| 585 | destlen = orig_sgl[i].length; |
| 586 | ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); |
| 587 | |
| 588 | if (j == 0) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 589 | bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 590 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 591 | while (destlen) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 592 | src = bounce_addr + bounce_sgl[j].offset; |
| 593 | srclen = bounce_sgl[j].length - bounce_sgl[j].offset; |
| 594 | |
Greg Kroah-Hartman | fc6a4b2 | 2009-07-15 11:05:14 -0700 | [diff] [blame] | 595 | copylen = min(srclen, destlen); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 596 | memcpy((void *)dest, (void *)src, copylen); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 597 | |
| 598 | total_copied += copylen; |
| 599 | bounce_sgl[j].offset += copylen; |
| 600 | destlen -= copylen; |
| 601 | dest += copylen; |
| 602 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 603 | if (bounce_sgl[j].offset == bounce_sgl[j].length) { |
| 604 | /* full */ |
| 605 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 606 | j++; |
| 607 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 608 | /* if we need to use another bounce buffer */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 609 | if (destlen || i != orig_sgl_count - 1) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 610 | bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 611 | } else if (destlen == 0 && i == orig_sgl_count - 1) { |
| 612 | /* unmap the last bounce that is < PAGE_SIZE */ |
| 613 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 617 | kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset), |
| 618 | KM_IRQ0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | local_irq_restore(flags); |
| 622 | |
| 623 | return total_copied; |
| 624 | } |
| 625 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 626 | /* |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 627 | * storvsc_queuecommand - Initiate command processing |
| 628 | */ |
| 629 | static int storvsc_queuecommand(struct scsi_cmnd *scmnd, |
| 630 | void (*done)(struct scsi_cmnd *)) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 631 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 632 | int ret; |
| 633 | struct host_device_context *host_device_ctx = |
| 634 | (struct host_device_context *)scmnd->device->host->hostdata; |
Haiyang Zhang | f916a34 | 2010-02-17 20:58:47 +0000 | [diff] [blame] | 635 | struct vm_device *device_ctx = host_device_ctx->device_ctx; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 636 | struct driver_context *driver_ctx = |
| 637 | driver_to_driver_context(device_ctx->device.driver); |
| 638 | struct storvsc_driver_context *storvsc_drv_ctx = |
| 639 | (struct storvsc_driver_context *)driver_ctx; |
| 640 | struct storvsc_driver_object *storvsc_drv_obj = |
| 641 | &storvsc_drv_ctx->drv_obj; |
Nicolas Palix | 0b3f683 | 2009-07-29 14:10:19 +0200 | [diff] [blame] | 642 | struct hv_storvsc_request *request; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 643 | struct storvsc_cmd_request *cmd_request; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 644 | unsigned int request_size = 0; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 645 | int i; |
| 646 | struct scatterlist *sgl; |
| 647 | |
| 648 | DPRINT_ENTER(STORVSC_DRV); |
| 649 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 650 | DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d " |
| 651 | "queue depth %d tagged %d", scmnd, scmnd->sc_data_direction, |
| 652 | scsi_sg_count(scmnd), scsi_sglist(scmnd), |
| 653 | scsi_bufflen(scmnd), scmnd->device->queue_depth, |
| 654 | scmnd->device->tagged_supported); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 655 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 656 | /* If retrying, no need to prep the cmd */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 657 | if (scmnd->host_scribble) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 658 | ASSERT(scmnd->scsi_done != NULL); |
| 659 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 660 | cmd_request = |
| 661 | (struct storvsc_cmd_request *)scmnd->host_scribble; |
| 662 | DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p", |
| 663 | scmnd, cmd_request); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 664 | |
| 665 | goto retry_request; |
| 666 | } |
| 667 | |
| 668 | ASSERT(scmnd->scsi_done == NULL); |
| 669 | ASSERT(scmnd->host_scribble == NULL); |
| 670 | |
| 671 | scmnd->scsi_done = done; |
| 672 | |
| 673 | request_size = sizeof(struct storvsc_cmd_request); |
| 674 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 675 | cmd_request = kmem_cache_alloc(host_device_ctx->request_pool, |
| 676 | GFP_ATOMIC); |
| 677 | if (!cmd_request) { |
| 678 | DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate " |
| 679 | "storvsc_cmd_request...marking queue busy", scmnd); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 680 | scmnd->scsi_done = NULL; |
| 681 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 682 | } |
| 683 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 684 | /* Setup the cmd request */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 685 | cmd_request->bounce_sgl_count = 0; |
| 686 | cmd_request->bounce_sgl = NULL; |
| 687 | cmd_request->cmd = scmnd; |
| 688 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 689 | scmnd->host_scribble = (unsigned char *)cmd_request; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 690 | |
| 691 | request = &cmd_request->request; |
| 692 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 693 | request->Extension = |
| 694 | (void *)((unsigned long)cmd_request + request_size); |
| 695 | DPRINT_DBG(STORVSC_DRV, "req %p size %d ext %d", request, request_size, |
| 696 | storvsc_drv_obj->RequestExtSize); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 697 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 698 | /* Build the SRB */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 699 | switch (scmnd->sc_data_direction) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 700 | case DMA_TO_DEVICE: |
| 701 | request->Type = WRITE_TYPE; |
| 702 | break; |
| 703 | case DMA_FROM_DEVICE: |
| 704 | request->Type = READ_TYPE; |
| 705 | break; |
| 706 | default: |
| 707 | request->Type = UNKNOWN_TYPE; |
| 708 | break; |
| 709 | } |
| 710 | |
| 711 | request->OnIOCompletion = storvsc_commmand_completion; |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 712 | request->Context = cmd_request;/* scmnd; */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 713 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 714 | /* request->PortId = scmnd->device->channel; */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 715 | request->Host = host_device_ctx->port; |
| 716 | request->Bus = scmnd->device->channel; |
| 717 | request->TargetId = scmnd->device->id; |
| 718 | request->LunId = scmnd->device->lun; |
| 719 | |
| 720 | ASSERT(scmnd->cmd_len <= 16); |
| 721 | request->CdbLen = scmnd->cmd_len; |
| 722 | request->Cdb = scmnd->cmnd; |
| 723 | |
| 724 | request->SenseBuffer = scmnd->sense_buffer; |
| 725 | request->SenseBufferSize = SCSI_SENSE_BUFFERSIZE; |
| 726 | |
| 727 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 728 | request->DataBuffer.Length = scsi_bufflen(scmnd); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 729 | if (scsi_sg_count(scmnd)) { |
| 730 | sgl = (struct scatterlist *)scsi_sglist(scmnd); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 731 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 732 | /* check if we need to bounce the sgl */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 733 | if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) { |
| 734 | DPRINT_INFO(STORVSC_DRV, |
| 735 | "need to bounce buffer for this scmnd %p", |
| 736 | scmnd); |
| 737 | cmd_request->bounce_sgl = |
| 738 | create_bounce_buffer(sgl, scsi_sg_count(scmnd), |
| 739 | scsi_bufflen(scmnd)); |
| 740 | if (!cmd_request->bounce_sgl) { |
| 741 | DPRINT_ERR(STORVSC_DRV, |
| 742 | "unable to create bounce buffer for " |
| 743 | "this scmnd %p", scmnd); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 744 | |
| 745 | scmnd->scsi_done = NULL; |
| 746 | scmnd->host_scribble = NULL; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 747 | kmem_cache_free(host_device_ctx->request_pool, |
| 748 | cmd_request); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 749 | |
| 750 | return SCSI_MLQUEUE_HOST_BUSY; |
| 751 | } |
| 752 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 753 | cmd_request->bounce_sgl_count = |
| 754 | ALIGN_UP(scsi_bufflen(scmnd), PAGE_SIZE) >> |
| 755 | PAGE_SHIFT; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 756 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 757 | /* |
| 758 | * FIXME: We can optimize on reads by just skipping |
| 759 | * this |
| 760 | */ |
| 761 | copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl, |
| 762 | scsi_sg_count(scmnd)); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 763 | |
| 764 | sgl = cmd_request->bounce_sgl; |
| 765 | } |
| 766 | |
| 767 | request->DataBuffer.Offset = sgl[0].offset; |
| 768 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 769 | for (i = 0; i < scsi_sg_count(scmnd); i++) { |
Lars Lindley | 0686e4f | 2010-03-11 23:51:23 +0100 | [diff] [blame^] | 770 | DPRINT_DBG(STORVSC_DRV, "sgl[%d] len %d offset %d\n", |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 771 | i, sgl[i].length, sgl[i].offset); |
| 772 | request->DataBuffer.PfnArray[i] = |
| 773 | page_to_pfn(sg_page((&sgl[i]))); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 774 | } |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 775 | } else if (scsi_sglist(scmnd)) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 776 | ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 777 | request->DataBuffer.Offset = |
| 778 | virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1); |
| 779 | request->DataBuffer.PfnArray[0] = |
| 780 | virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT; |
| 781 | } else { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 782 | ASSERT(scsi_bufflen(scmnd) == 0); |
| 783 | } |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 784 | |
| 785 | retry_request: |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 786 | /* Invokes the vsc to start an IO */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 787 | ret = storvsc_drv_obj->OnIORequest(&device_ctx->device_obj, |
| 788 | &cmd_request->request); |
| 789 | if (ret == -1) { |
| 790 | /* no more space */ |
| 791 | DPRINT_ERR(STORVSC_DRV, |
| 792 | "scmnd (%p) - queue FULL...marking queue busy", |
| 793 | scmnd); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 794 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 795 | if (cmd_request->bounce_sgl_count) { |
| 796 | /* |
| 797 | * FIXME: We can optimize on writes by just skipping |
| 798 | * this |
| 799 | */ |
| 800 | copy_from_bounce_buffer(scsi_sglist(scmnd), |
| 801 | cmd_request->bounce_sgl, |
| 802 | scsi_sg_count(scmnd)); |
| 803 | destroy_bounce_buffer(cmd_request->bounce_sgl, |
| 804 | cmd_request->bounce_sgl_count); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | kmem_cache_free(host_device_ctx->request_pool, cmd_request); |
| 808 | |
| 809 | scmnd->scsi_done = NULL; |
| 810 | scmnd->host_scribble = NULL; |
| 811 | |
| 812 | ret = SCSI_MLQUEUE_DEVICE_BUSY; |
| 813 | } |
| 814 | |
| 815 | DPRINT_EXIT(STORVSC_DRV); |
| 816 | |
| 817 | return ret; |
| 818 | } |
| 819 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 820 | static int storvsc_merge_bvec(struct request_queue *q, |
| 821 | struct bvec_merge_data *bmd, struct bio_vec *bvec) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 822 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 823 | /* checking done by caller. */ |
| 824 | return bvec->bv_len; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 825 | } |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 826 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 827 | /* |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 828 | * storvsc_device_configure - Configure the specified scsi device |
| 829 | */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 830 | static int storvsc_device_alloc(struct scsi_device *sdevice) |
| 831 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 832 | DPRINT_DBG(STORVSC_DRV, "sdev (%p) - setting device flag to %d", |
| 833 | sdevice, BLIST_SPARSELUN); |
| 834 | /* |
| 835 | * This enables luns to be located sparsely. Otherwise, we may not |
| 836 | * discovered them. |
| 837 | */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 838 | sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | static int storvsc_device_configure(struct scsi_device *sdevice) |
| 843 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 844 | DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice, |
| 845 | sdevice->queue_depth); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 846 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 847 | DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting queue depth to %d", |
| 848 | sdevice, STORVSC_MAX_IO_REQUESTS); |
| 849 | scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG, |
| 850 | STORVSC_MAX_IO_REQUESTS); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 851 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 852 | DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld", |
| 853 | sdevice, PAGE_SIZE); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 854 | blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE); |
| 855 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 856 | DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine", |
| 857 | sdevice); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 858 | blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec); |
| 859 | |
| 860 | blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY); |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 861 | /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 862 | |
| 863 | return 0; |
| 864 | } |
| 865 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 866 | /* |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 867 | * storvsc_host_reset_handler - Reset the scsi HBA |
| 868 | */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 869 | static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd) |
| 870 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 871 | int ret; |
| 872 | struct host_device_context *host_device_ctx = |
| 873 | (struct host_device_context *)scmnd->device->host->hostdata; |
Haiyang Zhang | f916a34 | 2010-02-17 20:58:47 +0000 | [diff] [blame] | 874 | struct vm_device *device_ctx = host_device_ctx->device_ctx; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 875 | |
| 876 | DPRINT_ENTER(STORVSC_DRV); |
| 877 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 878 | DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...", |
| 879 | scmnd->device, &device_ctx->device_obj); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 880 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 881 | /* Invokes the vsc to reset the host/bus */ |
Greg Kroah-Hartman | 354b0a6 | 2010-01-08 10:24:26 -0800 | [diff] [blame] | 882 | ret = StorVscOnHostReset(&device_ctx->device_obj); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 883 | if (ret != 0) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 884 | DPRINT_EXIT(STORVSC_DRV); |
| 885 | return ret; |
| 886 | } |
| 887 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 888 | DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted", |
| 889 | scmnd->device, &device_ctx->device_obj); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 890 | |
| 891 | DPRINT_EXIT(STORVSC_DRV); |
| 892 | |
| 893 | return ret; |
| 894 | } |
| 895 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 896 | static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, |
| 897 | sector_t capacity, int *info) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 898 | { |
| 899 | sector_t total_sectors = capacity; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 900 | sector_t cylinder_times_heads = 0; |
| 901 | sector_t temp = 0; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 902 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 903 | int sectors_per_track = 0; |
| 904 | int heads = 0; |
| 905 | int cylinders = 0; |
| 906 | int rem = 0; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 907 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 908 | if (total_sectors > (65535 * 16 * 255)) |
| 909 | total_sectors = (65535 * 16 * 255); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 910 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 911 | if (total_sectors >= (65535 * 16 * 63)) { |
| 912 | sectors_per_track = 255; |
| 913 | heads = 16; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 914 | |
| 915 | cylinder_times_heads = total_sectors; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 916 | /* sector_div stores the quotient in cylinder_times_heads */ |
| 917 | rem = sector_div(cylinder_times_heads, sectors_per_track); |
| 918 | } else { |
| 919 | sectors_per_track = 17; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 920 | |
| 921 | cylinder_times_heads = total_sectors; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 922 | /* sector_div stores the quotient in cylinder_times_heads */ |
| 923 | rem = sector_div(cylinder_times_heads, sectors_per_track); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 924 | |
| 925 | temp = cylinder_times_heads + 1023; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 926 | /* sector_div stores the quotient in temp */ |
| 927 | rem = sector_div(temp, 1024); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 928 | |
| 929 | heads = temp; |
| 930 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 931 | if (heads < 4) |
| 932 | heads = 4; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 933 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 934 | if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) { |
| 935 | sectors_per_track = 31; |
| 936 | heads = 16; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 937 | |
| 938 | cylinder_times_heads = total_sectors; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 939 | /* |
| 940 | * sector_div stores the quotient in |
| 941 | * cylinder_times_heads |
| 942 | */ |
| 943 | rem = sector_div(cylinder_times_heads, |
| 944 | sectors_per_track); |
| 945 | } |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 946 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 947 | if (cylinder_times_heads >= (heads * 1024)) { |
| 948 | sectors_per_track = 63; |
| 949 | heads = 16; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 950 | |
| 951 | cylinder_times_heads = total_sectors; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 952 | /* |
| 953 | * sector_div stores the quotient in |
| 954 | * cylinder_times_heads |
| 955 | */ |
| 956 | rem = sector_div(cylinder_times_heads, |
| 957 | sectors_per_track); |
| 958 | } |
| 959 | } |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 960 | |
| 961 | temp = cylinder_times_heads; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 962 | /* sector_div stores the quotient in temp */ |
| 963 | rem = sector_div(temp, heads); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 964 | cylinders = temp; |
| 965 | |
| 966 | info[0] = heads; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 967 | info[1] = sectors_per_track; |
| 968 | info[2] = cylinders; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 969 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 970 | DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads, |
| 971 | sectors_per_track); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 972 | |
| 973 | return 0; |
| 974 | } |
| 975 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 976 | static int __init storvsc_init(void) |
| 977 | { |
| 978 | int ret; |
| 979 | |
| 980 | DPRINT_ENTER(STORVSC_DRV); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 981 | DPRINT_INFO(STORVSC_DRV, "Storvsc initializing...."); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 982 | ret = storvsc_drv_init(StorVscInitialize); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 983 | DPRINT_EXIT(STORVSC_DRV); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 984 | return ret; |
| 985 | } |
| 986 | |
| 987 | static void __exit storvsc_exit(void) |
| 988 | { |
| 989 | DPRINT_ENTER(STORVSC_DRV); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 990 | storvsc_drv_exit(); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 991 | DPRINT_ENTER(STORVSC_DRV); |
| 992 | } |
| 993 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 994 | MODULE_LICENSE("GPL"); |
Hank Janssen | 26c14cc | 2010-02-11 23:02:42 +0000 | [diff] [blame] | 995 | MODULE_VERSION(HV_DRV_VERSION); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 996 | module_param(storvsc_ringbuffer_size, int, S_IRUGO); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 997 | module_init(storvsc_init); |
| 998 | module_exit(storvsc_exit); |