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