blob: d22e35f598ba43a0925281a7df7403da3e7029c2 [file] [log] [blame]
Hank Janssenbef4a342009-07-13 16:01:31 -07001/*
Hank Janssenbef4a342009-07-13 16:01:31 -07002 * 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 Janssenbef4a342009-07-13 16:01:31 -070020 */
Hank Janssenbef4a342009-07-13 16:01:31 -070021#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070023#include <linux/module.h>
24#include <linux/device.h>
25#include <linux/blkdev.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070026#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 Janssenbef4a342009-07-13 16:01:31 -070033#include <scsi/scsi_dbg.h>
Greg Kroah-Hartman4983b392009-08-19 16:14:47 -070034#include "osd.h"
Greg Kroah-Hartman645954c2009-08-28 16:22:59 -070035#include "logging.h"
Greg Kroah-Hartman2d82f6c2010-05-05 22:52:28 -070036#include "version_info.h"
Greg Kroah-Hartman870cde82009-08-19 16:21:28 -070037#include "vmbus.h"
Greg Kroah-Hartmanbb969792010-05-05 22:40:43 -070038#include "storvsc_api.h"
Hank Janssenbef4a342009-07-13 16:01:31 -070039
Hank Janssenbef4a342009-07-13 16:01:31 -070040
Hank Janssenbef4a342009-07-13 16:01:31 -070041struct host_device_context {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070042 /* must be 1st field
43 * FIXME this is a bug */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070044 /* point back to our device context */
Haiyang Zhangf916a342010-02-17 20:58:47 +000045 struct vm_device *device_ctx;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070046 struct kmem_cache *request_pool;
47 unsigned int port;
48 unsigned char path;
49 unsigned char target;
Hank Janssenbef4a342009-07-13 16:01:31 -070050};
51
52struct storvsc_cmd_request {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070053 struct list_head entry;
54 struct scsi_cmnd *cmd;
Hank Janssenbef4a342009-07-13 16:01:31 -070055
56 unsigned int bounce_sgl_count;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070057 struct scatterlist *bounce_sgl;
Hank Janssenbef4a342009-07-13 16:01:31 -070058
Nicolas Palix0b3f6832009-07-29 14:10:19 +020059 struct hv_storvsc_request request;
Bill Pemberton454f18a2009-07-27 16:47:24 -040060 /* !!!DO NOT ADD ANYTHING BELOW HERE!!! */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070061 /* The extension buffer falls right here and is pointed to by
62 * request.Extension;
63 * Which sounds like a very bad design... */
Hank Janssenbef4a342009-07-13 16:01:31 -070064};
65
66struct storvsc_driver_context {
Bill Pemberton454f18a2009-07-27 16:47:24 -040067 /* !! These must be the first 2 fields !! */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070068 /* FIXME this is a bug... */
69 struct driver_context drv_ctx;
Greg Kroah-Hartman9f0c7d22009-08-28 16:20:07 -070070 struct storvsc_driver_object drv_obj;
Hank Janssenbef4a342009-07-13 16:01:31 -070071};
72
Bill Pemberton454f18a2009-07-27 16:47:24 -040073/* Static decl */
Hank Janssenbef4a342009-07-13 16:01:31 -070074static int storvsc_probe(struct device *dev);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070075static int storvsc_queuecommand(struct scsi_cmnd *scmnd,
76 void (*done)(struct scsi_cmnd *));
Hank Janssenbef4a342009-07-13 16:01:31 -070077static int storvsc_device_alloc(struct scsi_device *);
78static int storvsc_device_configure(struct scsi_device *);
79static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd);
Hank Janssenbef4a342009-07-13 16:01:31 -070080static int storvsc_remove(struct device *dev);
81
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070082static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
83 unsigned int sg_count,
84 unsigned int len);
85static void destroy_bounce_buffer(struct scatterlist *sgl,
86 unsigned int sg_count);
Hank Janssenbef4a342009-07-13 16:01:31 -070087static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070088static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
89 struct scatterlist *bounce_sgl,
90 unsigned int orig_sgl_count);
91static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
92 struct scatterlist *bounce_sgl,
93 unsigned int orig_sgl_count);
Hank Janssenbef4a342009-07-13 16:01:31 -070094
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070095static int storvsc_get_chs(struct scsi_device *sdev, struct block_device *bdev,
96 sector_t capacity, int *info);
Hank Janssenbef4a342009-07-13 16:01:31 -070097
98
99static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -0700100module_param(storvsc_ringbuffer_size, int, S_IRUGO);
101MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
Hank Janssenbef4a342009-07-13 16:01:31 -0700102
Bill Pemberton454f18a2009-07-27 16:47:24 -0400103/* The one and only one */
Hank Janssenbef4a342009-07-13 16:01:31 -0700104static struct storvsc_driver_context g_storvsc_drv;
105
Bill Pemberton454f18a2009-07-27 16:47:24 -0400106/* Scsi driver */
Hank Janssenbef4a342009-07-13 16:01:31 -0700107static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700108 .module = THIS_MODULE,
109 .name = "storvsc_host_t",
110 .bios_param = storvsc_get_chs,
111 .queuecommand = storvsc_queuecommand,
112 .eh_host_reset_handler = storvsc_host_reset_handler,
113 .slave_alloc = storvsc_device_alloc,
114 .slave_configure = storvsc_device_configure,
115 .cmd_per_lun = 1,
116 /* 64 max_queue * 1 target */
Lars Lindley0686e4f2010-03-11 23:51:23 +0100117 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700118 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400119 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700120 /* currently 32 */
121 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
122 /*
123 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
124 * into 1 sg element. If set, we must limit the max_segment_size to
125 * PAGE_SIZE, otherwise we may get 1 sg element that represents
126 * multiple
127 */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400128 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700129 .use_clustering = ENABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400130 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700131 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -0700132};
133
134
Hank Janssen3e189512010-03-04 22:11:00 +0000135/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700136 * storvsc_drv_init - StorVsc driver initialization.
137 */
Greg Kroah-Hartman21707be2009-09-02 11:53:59 -0700138static int storvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
Hank Janssenbef4a342009-07-13 16:01:31 -0700139{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700140 int ret;
Greg Kroah-Hartman9f0c7d22009-08-28 16:20:07 -0700141 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700142 struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx;
Hank Janssenbef4a342009-07-13 16:01:31 -0700143
144 DPRINT_ENTER(STORVSC_DRV);
145
146 vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
147
148 storvsc_drv_obj->RingBufferSize = storvsc_ringbuffer_size;
Hank Janssenbef4a342009-07-13 16:01:31 -0700149
Bill Pemberton454f18a2009-07-27 16:47:24 -0400150 /* Callback to client driver to complete the initialization */
Greg Kroah-Hartman21707be2009-09-02 11:53:59 -0700151 drv_init(&storvsc_drv_obj->Base);
Hank Janssenbef4a342009-07-13 16:01:31 -0700152
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700153 DPRINT_INFO(STORVSC_DRV,
154 "request extension size %u, max outstanding reqs %u",
155 storvsc_drv_obj->RequestExtSize,
156 storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
Hank Janssenbef4a342009-07-13 16:01:31 -0700157
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700158 if (storvsc_drv_obj->MaxOutstandingRequestsPerChannel <
159 STORVSC_MAX_IO_REQUESTS) {
160 DPRINT_ERR(STORVSC_DRV,
161 "The number of outstanding io requests (%d) "
162 "is larger than that supported (%d) internally.",
163 STORVSC_MAX_IO_REQUESTS,
164 storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
Hank Janssenbef4a342009-07-13 16:01:31 -0700165 return -1;
166 }
167
168 drv_ctx->driver.name = storvsc_drv_obj->Base.name;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700169 memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType,
170 sizeof(struct hv_guid));
Hank Janssenbef4a342009-07-13 16:01:31 -0700171
Hank Janssenbef4a342009-07-13 16:01:31 -0700172 drv_ctx->probe = storvsc_probe;
173 drv_ctx->remove = storvsc_remove;
Hank Janssenbef4a342009-07-13 16:01:31 -0700174
Bill Pemberton454f18a2009-07-27 16:47:24 -0400175 /* The driver belongs to vmbus */
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400176 ret = vmbus_child_driver_register(drv_ctx);
Hank Janssenbef4a342009-07-13 16:01:31 -0700177
178 DPRINT_EXIT(STORVSC_DRV);
179
180 return ret;
181}
182
Hank Janssenbef4a342009-07-13 16:01:31 -0700183static int storvsc_drv_exit_cb(struct device *dev, void *data)
184{
185 struct device **curr = (struct device **)data;
186 *curr = dev;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400187 return 1; /* stop iterating */
Hank Janssenbef4a342009-07-13 16:01:31 -0700188}
189
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700190static void storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -0700191{
Greg Kroah-Hartman9f0c7d22009-08-28 16:20:07 -0700192 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700193 struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx;
194 struct device *current_dev = NULL;
Bill Pemberton2295ba22009-07-28 13:46:22 -0400195 int ret;
Hank Janssenbef4a342009-07-13 16:01:31 -0700196
Hank Janssenbef4a342009-07-13 16:01:31 -0700197 DPRINT_ENTER(STORVSC_DRV);
198
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700199 while (1) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700200 current_dev = NULL;
201
Bill Pemberton454f18a2009-07-27 16:47:24 -0400202 /* Get the device */
Bill Pemberton2295ba22009-07-28 13:46:22 -0400203 ret = driver_for_each_device(&drv_ctx->driver, NULL,
204 (void *) &current_dev,
205 storvsc_drv_exit_cb);
206
207 if (ret)
208 DPRINT_WARN(STORVSC_DRV,
209 "driver_for_each_device returned %d", ret);
Hank Janssenbef4a342009-07-13 16:01:31 -0700210
211 if (current_dev == NULL)
212 break;
213
Bill Pemberton454f18a2009-07-27 16:47:24 -0400214 /* Initiate removal from the top-down */
Hank Janssenbef4a342009-07-13 16:01:31 -0700215 device_unregister(current_dev);
216 }
217
218 if (storvsc_drv_obj->Base.OnCleanup)
219 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
220
221 vmbus_child_driver_unregister(drv_ctx);
222
223 DPRINT_EXIT(STORVSC_DRV);
224
225 return;
226}
227
Hank Janssen3e189512010-03-04 22:11:00 +0000228/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700229 * storvsc_probe - Add a new device for this driver
230 */
Hank Janssenbef4a342009-07-13 16:01:31 -0700231static int storvsc_probe(struct device *device)
232{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700233 int ret;
234 struct driver_context *driver_ctx =
235 driver_to_driver_context(device->driver);
236 struct storvsc_driver_context *storvsc_drv_ctx =
237 (struct storvsc_driver_context *)driver_ctx;
238 struct storvsc_driver_object *storvsc_drv_obj =
239 &storvsc_drv_ctx->drv_obj;
Haiyang Zhangf916a342010-02-17 20:58:47 +0000240 struct vm_device *device_ctx = device_to_vm_device(device);
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200241 struct hv_device *device_obj = &device_ctx->device_obj;
Hank Janssenbef4a342009-07-13 16:01:31 -0700242 struct Scsi_Host *host;
243 struct host_device_context *host_device_ctx;
Greg Kroah-Hartman9f0c7d22009-08-28 16:20:07 -0700244 struct storvsc_device_info device_info;
Hank Janssenbef4a342009-07-13 16:01:31 -0700245
246 DPRINT_ENTER(STORVSC_DRV);
247
248 if (!storvsc_drv_obj->Base.OnDeviceAdd)
249 return -1;
250
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700251 host = scsi_host_alloc(&scsi_driver,
252 sizeof(struct host_device_context));
253 if (!host) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700254 DPRINT_ERR(STORVSC_DRV, "unable to allocate scsi host object");
255 return -ENOMEM;
256 }
257
Greg Kroah-Hartman0883c522009-07-24 10:58:22 -0700258 dev_set_drvdata(device, host);
Hank Janssenbef4a342009-07-13 16:01:31 -0700259
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700260 host_device_ctx = (struct host_device_context *)host->hostdata;
Hank Janssenbef4a342009-07-13 16:01:31 -0700261 memset(host_device_ctx, 0, sizeof(struct host_device_context));
262
263 host_device_ctx->port = host->host_no;
264 host_device_ctx->device_ctx = device_ctx;
265
Hank Janssenbef4a342009-07-13 16:01:31 -0700266 host_device_ctx->request_pool =
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700267 kmem_cache_create(dev_name(&device_ctx->device),
268 sizeof(struct storvsc_cmd_request) +
269 storvsc_drv_obj->RequestExtSize, 0,
270 SLAB_HWCACHE_ALIGN, NULL);
Hank Janssenbef4a342009-07-13 16:01:31 -0700271
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700272 if (!host_device_ctx->request_pool) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700273 scsi_host_put(host);
274 DPRINT_EXIT(STORVSC_DRV);
275
276 return -ENOMEM;
277 }
278
279 device_info.PortNumber = host->host_no;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400280 /* Call to the vsc driver to add the device */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700281 ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj,
282 (void *)&device_info);
283 if (ret != 0) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700284 DPRINT_ERR(STORVSC_DRV, "unable to add scsi vsc device");
285 kmem_cache_destroy(host_device_ctx->request_pool);
286 scsi_host_put(host);
287 DPRINT_EXIT(STORVSC_DRV);
288
289 return -1;
290 }
291
Bill Pemberton454f18a2009-07-27 16:47:24 -0400292 /* host_device_ctx->port = device_info.PortNumber; */
Hank Janssenbef4a342009-07-13 16:01:31 -0700293 host_device_ctx->path = device_info.PathId;
294 host_device_ctx->target = device_info.TargetId;
295
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700296 /* max # of devices per target */
297 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
298 /* max # of targets per channel */
299 host->max_id = STORVSC_MAX_TARGETS;
300 /* max # of channels */
301 host->max_channel = STORVSC_MAX_CHANNELS - 1;
Hank Janssenbef4a342009-07-13 16:01:31 -0700302
Bill Pemberton454f18a2009-07-27 16:47:24 -0400303 /* Register the HBA and start the scsi bus scan */
Hank Janssenbef4a342009-07-13 16:01:31 -0700304 ret = scsi_add_host(host, device);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700305 if (ret != 0) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700306 DPRINT_ERR(STORVSC_DRV, "unable to add scsi host device");
307
308 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
309
310 kmem_cache_destroy(host_device_ctx->request_pool);
311 scsi_host_put(host);
312 DPRINT_EXIT(STORVSC_DRV);
313
314 return -1;
315 }
316
317 scsi_scan_host(host);
318
319 DPRINT_EXIT(STORVSC_DRV);
320
321 return ret;
322}
323
Hank Janssen3e189512010-03-04 22:11:00 +0000324/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700325 * storvsc_remove - Callback when our device is removed
326 */
Hank Janssenbef4a342009-07-13 16:01:31 -0700327static int storvsc_remove(struct device *device)
328{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700329 int ret;
330 struct driver_context *driver_ctx =
331 driver_to_driver_context(device->driver);
332 struct storvsc_driver_context *storvsc_drv_ctx =
333 (struct storvsc_driver_context *)driver_ctx;
334 struct storvsc_driver_object *storvsc_drv_obj =
335 &storvsc_drv_ctx->drv_obj;
Haiyang Zhangf916a342010-02-17 20:58:47 +0000336 struct vm_device *device_ctx = device_to_vm_device(device);
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200337 struct hv_device *device_obj = &device_ctx->device_obj;
Greg Kroah-Hartman0883c522009-07-24 10:58:22 -0700338 struct Scsi_Host *host = dev_get_drvdata(device);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700339 struct host_device_context *host_device_ctx =
340 (struct host_device_context *)host->hostdata;
Hank Janssenbef4a342009-07-13 16:01:31 -0700341
342
343 DPRINT_ENTER(STORVSC_DRV);
344
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700345 if (!storvsc_drv_obj->Base.OnDeviceRemove) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700346 DPRINT_EXIT(STORVSC_DRV);
347 return -1;
348 }
349
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700350 /*
351 * Call to the vsc driver to let it know that the device is being
352 * removed
353 */
Hank Janssenbef4a342009-07-13 16:01:31 -0700354 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700355 if (ret != 0) {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400356 /* TODO: */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700357 DPRINT_ERR(STORVSC, "unable to remove vsc device (ret %d)",
358 ret);
Hank Janssenbef4a342009-07-13 16:01:31 -0700359 }
360
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700361 if (host_device_ctx->request_pool) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700362 kmem_cache_destroy(host_device_ctx->request_pool);
363 host_device_ctx->request_pool = NULL;
364 }
365
366 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
367 scsi_remove_host(host);
368
369 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
370 scsi_host_put(host);
371
372 DPRINT_EXIT(STORVSC_DRV);
373
374 return ret;
375}
376
Hank Janssen3e189512010-03-04 22:11:00 +0000377/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700378 * storvsc_commmand_completion - Command completion processing
379 */
Nicolas Palix0b3f6832009-07-29 14:10:19 +0200380static void storvsc_commmand_completion(struct hv_storvsc_request *request)
Hank Janssenbef4a342009-07-13 16:01:31 -0700381{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700382 struct storvsc_cmd_request *cmd_request =
383 (struct storvsc_cmd_request *)request->Context;
Hank Janssenbef4a342009-07-13 16:01:31 -0700384 struct scsi_cmnd *scmnd = cmd_request->cmd;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700385 struct host_device_context *host_device_ctx =
386 (struct host_device_context *)scmnd->device->host->hostdata;
Hank Janssenbef4a342009-07-13 16:01:31 -0700387 void (*scsi_done_fn)(struct scsi_cmnd *);
Hank Janssenbef4a342009-07-13 16:01:31 -0700388 struct scsi_sense_hdr sense_hdr;
Hank Janssenbef4a342009-07-13 16:01:31 -0700389
Bill Pembertonb856e732010-05-05 15:27:41 -0400390 /* ASSERT(request == &cmd_request->request); */
391 /* ASSERT(scmnd); */
392 /* ASSERT((unsigned long)scmnd->host_scribble == */
393 /* (unsigned long)cmd_request); */
394 /* ASSERT(scmnd->scsi_done); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700395
396 DPRINT_ENTER(STORVSC_DRV);
397
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700398 if (cmd_request->bounce_sgl_count) {
399 /* using bounce buffer */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400400 /* printk("copy_from_bounce_buffer\n"); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700401
Bill Pemberton454f18a2009-07-27 16:47:24 -0400402 /* FIXME: We can optimize on writes by just skipping this */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700403 copy_from_bounce_buffer(scsi_sglist(scmnd),
404 cmd_request->bounce_sgl,
405 scsi_sg_count(scmnd));
406 destroy_bounce_buffer(cmd_request->bounce_sgl,
407 cmd_request->bounce_sgl_count);
Hank Janssenbef4a342009-07-13 16:01:31 -0700408 }
409
410 scmnd->result = request->Status;
411
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700412 if (scmnd->result) {
413 if (scsi_normalize_sense(scmnd->sense_buffer,
414 request->SenseBufferSize, &sense_hdr))
Hank Janssenbef4a342009-07-13 16:01:31 -0700415 scsi_print_sense_hdr("storvsc", &sense_hdr);
Hank Janssenbef4a342009-07-13 16:01:31 -0700416 }
417
Bill Pembertonb856e732010-05-05 15:27:41 -0400418 /* ASSERT(request->BytesXfer <= request->DataBuffer.Length); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700419 scsi_set_resid(scmnd, request->DataBuffer.Length - request->BytesXfer);
Hank Janssenbef4a342009-07-13 16:01:31 -0700420
421 scsi_done_fn = scmnd->scsi_done;
422
423 scmnd->host_scribble = NULL;
424 scmnd->scsi_done = NULL;
425
Bill Pemberton454f18a2009-07-27 16:47:24 -0400426 /* !!DO NOT MODIFY the scmnd after this call */
Hank Janssenbef4a342009-07-13 16:01:31 -0700427 scsi_done_fn(scmnd);
428
429 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
430
431 DPRINT_EXIT(STORVSC_DRV);
432}
433
434static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
435{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700436 int i;
Hank Janssenbef4a342009-07-13 16:01:31 -0700437
Bill Pemberton454f18a2009-07-27 16:47:24 -0400438 /* No need to check */
Hank Janssenbef4a342009-07-13 16:01:31 -0700439 if (sg_count < 2)
440 return -1;
441
Bill Pemberton454f18a2009-07-27 16:47:24 -0400442 /* We have at least 2 sg entries */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700443 for (i = 0; i < sg_count; i++) {
444 if (i == 0) {
445 /* make sure 1st one does not have hole */
Hank Janssenbef4a342009-07-13 16:01:31 -0700446 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
447 return i;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700448 } else if (i == sg_count - 1) {
449 /* make sure last one does not have hole */
Hank Janssenbef4a342009-07-13 16:01:31 -0700450 if (sgl[i].offset != 0)
451 return i;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700452 } else {
453 /* make sure no hole in the middle */
Hank Janssenbef4a342009-07-13 16:01:31 -0700454 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
Hank Janssenbef4a342009-07-13 16:01:31 -0700455 return i;
Hank Janssenbef4a342009-07-13 16:01:31 -0700456 }
457 }
458 return -1;
459}
460
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700461static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
462 unsigned int sg_count,
463 unsigned int len)
Hank Janssenbef4a342009-07-13 16:01:31 -0700464{
465 int i;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700466 int num_pages;
467 struct scatterlist *bounce_sgl;
Hank Janssenbef4a342009-07-13 16:01:31 -0700468 struct page *page_buf;
469
470 num_pages = ALIGN_UP(len, PAGE_SIZE) >> PAGE_SHIFT;
471
Nicolas Palix06da0bc2009-07-22 15:39:13 +0200472 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
Hank Janssenbef4a342009-07-13 16:01:31 -0700473 if (!bounce_sgl)
Hank Janssenbef4a342009-07-13 16:01:31 -0700474 return NULL;
Hank Janssenbef4a342009-07-13 16:01:31 -0700475
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700476 for (i = 0; i < num_pages; i++) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700477 page_buf = alloc_page(GFP_ATOMIC);
478 if (!page_buf)
Hank Janssenbef4a342009-07-13 16:01:31 -0700479 goto cleanup;
Hank Janssenbef4a342009-07-13 16:01:31 -0700480 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700481 }
482
483 return bounce_sgl;
484
485cleanup:
486 destroy_bounce_buffer(bounce_sgl, num_pages);
487 return NULL;
488}
489
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700490static void destroy_bounce_buffer(struct scatterlist *sgl,
491 unsigned int sg_count)
Hank Janssenbef4a342009-07-13 16:01:31 -0700492{
493 int i;
494 struct page *page_buf;
495
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700496 for (i = 0; i < sg_count; i++) {
497 page_buf = sg_page((&sgl[i]));
498 if (page_buf != NULL)
Hank Janssenbef4a342009-07-13 16:01:31 -0700499 __free_page(page_buf);
Hank Janssenbef4a342009-07-13 16:01:31 -0700500 }
501
502 kfree(sgl);
503}
504
Bill Pemberton454f18a2009-07-27 16:47:24 -0400505/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700506static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
507 struct scatterlist *bounce_sgl,
508 unsigned int orig_sgl_count)
Hank Janssenbef4a342009-07-13 16:01:31 -0700509{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700510 int i;
511 int j = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700512 unsigned long src, dest;
513 unsigned int srclen, destlen, copylen;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700514 unsigned int total_copied = 0;
515 unsigned long bounce_addr = 0;
516 unsigned long src_addr = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700517 unsigned long flags;
518
519 local_irq_save(flags);
520
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700521 for (i = 0; i < orig_sgl_count; i++) {
522 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
523 KM_IRQ0) + orig_sgl[i].offset;
Hank Janssenbef4a342009-07-13 16:01:31 -0700524 src = src_addr;
525 srclen = orig_sgl[i].length;
526
Bill Pembertonb856e732010-05-05 15:27:41 -0400527 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700528
529 if (j == 0)
Hank Janssenbef4a342009-07-13 16:01:31 -0700530 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700531
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700532 while (srclen) {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400533 /* assume bounce offset always == 0 */
Hank Janssenbef4a342009-07-13 16:01:31 -0700534 dest = bounce_addr + bounce_sgl[j].length;
535 destlen = PAGE_SIZE - bounce_sgl[j].length;
536
Greg Kroah-Hartmanfc6a4b22009-07-15 11:05:14 -0700537 copylen = min(srclen, destlen);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700538 memcpy((void *)dest, (void *)src, copylen);
Hank Janssenbef4a342009-07-13 16:01:31 -0700539
540 total_copied += copylen;
541 bounce_sgl[j].length += copylen;
542 srclen -= copylen;
543 src += copylen;
544
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700545 if (bounce_sgl[j].length == PAGE_SIZE) {
546 /* full..move to next entry */
547 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700548 j++;
549
Bill Pemberton454f18a2009-07-27 16:47:24 -0400550 /* if we need to use another bounce buffer */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700551 if (srclen || i != orig_sgl_count - 1)
Hank Janssenbef4a342009-07-13 16:01:31 -0700552 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700553 } else if (srclen == 0 && i == orig_sgl_count - 1) {
554 /* unmap the last bounce that is < PAGE_SIZE */
555 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700556 }
557 }
558
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700559 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700560 }
561
562 local_irq_restore(flags);
563
564 return total_copied;
565}
566
Bill Pemberton454f18a2009-07-27 16:47:24 -0400567/* Assume the original sgl has enough room */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700568static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
569 struct scatterlist *bounce_sgl,
570 unsigned int orig_sgl_count)
Hank Janssenbef4a342009-07-13 16:01:31 -0700571{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700572 int i;
573 int j = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700574 unsigned long src, dest;
575 unsigned int srclen, destlen, copylen;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700576 unsigned int total_copied = 0;
577 unsigned long bounce_addr = 0;
578 unsigned long dest_addr = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700579 unsigned long flags;
580
581 local_irq_save(flags);
582
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700583 for (i = 0; i < orig_sgl_count; i++) {
584 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
585 KM_IRQ0) + orig_sgl[i].offset;
Hank Janssenbef4a342009-07-13 16:01:31 -0700586 dest = dest_addr;
587 destlen = orig_sgl[i].length;
Bill Pembertonb856e732010-05-05 15:27:41 -0400588 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700589
590 if (j == 0)
Hank Janssenbef4a342009-07-13 16:01:31 -0700591 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700592
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700593 while (destlen) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700594 src = bounce_addr + bounce_sgl[j].offset;
595 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
596
Greg Kroah-Hartmanfc6a4b22009-07-15 11:05:14 -0700597 copylen = min(srclen, destlen);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700598 memcpy((void *)dest, (void *)src, copylen);
Hank Janssenbef4a342009-07-13 16:01:31 -0700599
600 total_copied += copylen;
601 bounce_sgl[j].offset += copylen;
602 destlen -= copylen;
603 dest += copylen;
604
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700605 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
606 /* full */
607 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700608 j++;
609
Bill Pemberton454f18a2009-07-27 16:47:24 -0400610 /* if we need to use another bounce buffer */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700611 if (destlen || i != orig_sgl_count - 1)
Hank Janssenbef4a342009-07-13 16:01:31 -0700612 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700613 } else if (destlen == 0 && i == orig_sgl_count - 1) {
614 /* unmap the last bounce that is < PAGE_SIZE */
615 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700616 }
617 }
618
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700619 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
620 KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700621 }
622
623 local_irq_restore(flags);
624
625 return total_copied;
626}
627
Hank Janssen3e189512010-03-04 22:11:00 +0000628/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700629 * storvsc_queuecommand - Initiate command processing
630 */
631static int storvsc_queuecommand(struct scsi_cmnd *scmnd,
632 void (*done)(struct scsi_cmnd *))
Hank Janssenbef4a342009-07-13 16:01:31 -0700633{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700634 int ret;
635 struct host_device_context *host_device_ctx =
636 (struct host_device_context *)scmnd->device->host->hostdata;
Haiyang Zhangf916a342010-02-17 20:58:47 +0000637 struct vm_device *device_ctx = host_device_ctx->device_ctx;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700638 struct driver_context *driver_ctx =
639 driver_to_driver_context(device_ctx->device.driver);
640 struct storvsc_driver_context *storvsc_drv_ctx =
641 (struct storvsc_driver_context *)driver_ctx;
642 struct storvsc_driver_object *storvsc_drv_obj =
643 &storvsc_drv_ctx->drv_obj;
Nicolas Palix0b3f6832009-07-29 14:10:19 +0200644 struct hv_storvsc_request *request;
Hank Janssenbef4a342009-07-13 16:01:31 -0700645 struct storvsc_cmd_request *cmd_request;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700646 unsigned int request_size = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700647 int i;
648 struct scatterlist *sgl;
649
650 DPRINT_ENTER(STORVSC_DRV);
651
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700652 DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d "
653 "queue depth %d tagged %d", scmnd, scmnd->sc_data_direction,
654 scsi_sg_count(scmnd), scsi_sglist(scmnd),
655 scsi_bufflen(scmnd), scmnd->device->queue_depth,
656 scmnd->device->tagged_supported);
Hank Janssenbef4a342009-07-13 16:01:31 -0700657
Bill Pemberton454f18a2009-07-27 16:47:24 -0400658 /* If retrying, no need to prep the cmd */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700659 if (scmnd->host_scribble) {
Bill Pembertonb856e732010-05-05 15:27:41 -0400660 /* ASSERT(scmnd->scsi_done != NULL); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700661
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700662 cmd_request =
663 (struct storvsc_cmd_request *)scmnd->host_scribble;
664 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
665 scmnd, cmd_request);
Hank Janssenbef4a342009-07-13 16:01:31 -0700666
667 goto retry_request;
668 }
669
Bill Pembertonb856e732010-05-05 15:27:41 -0400670 /* ASSERT(scmnd->scsi_done == NULL); */
671 /* ASSERT(scmnd->host_scribble == NULL); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700672
673 scmnd->scsi_done = done;
674
675 request_size = sizeof(struct storvsc_cmd_request);
676
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700677 cmd_request = kmem_cache_alloc(host_device_ctx->request_pool,
678 GFP_ATOMIC);
679 if (!cmd_request) {
680 DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate "
681 "storvsc_cmd_request...marking queue busy", scmnd);
Hank Janssenbef4a342009-07-13 16:01:31 -0700682 scmnd->scsi_done = NULL;
683 return SCSI_MLQUEUE_DEVICE_BUSY;
684 }
685
Bill Pemberton454f18a2009-07-27 16:47:24 -0400686 /* Setup the cmd request */
Hank Janssenbef4a342009-07-13 16:01:31 -0700687 cmd_request->bounce_sgl_count = 0;
688 cmd_request->bounce_sgl = NULL;
689 cmd_request->cmd = scmnd;
690
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700691 scmnd->host_scribble = (unsigned char *)cmd_request;
Hank Janssenbef4a342009-07-13 16:01:31 -0700692
693 request = &cmd_request->request;
694
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700695 request->Extension =
696 (void *)((unsigned long)cmd_request + request_size);
697 DPRINT_DBG(STORVSC_DRV, "req %p size %d ext %d", request, request_size,
698 storvsc_drv_obj->RequestExtSize);
Hank Janssenbef4a342009-07-13 16:01:31 -0700699
Bill Pemberton454f18a2009-07-27 16:47:24 -0400700 /* Build the SRB */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700701 switch (scmnd->sc_data_direction) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700702 case DMA_TO_DEVICE:
703 request->Type = WRITE_TYPE;
704 break;
705 case DMA_FROM_DEVICE:
706 request->Type = READ_TYPE;
707 break;
708 default:
709 request->Type = UNKNOWN_TYPE;
710 break;
711 }
712
713 request->OnIOCompletion = storvsc_commmand_completion;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400714 request->Context = cmd_request;/* scmnd; */
Hank Janssenbef4a342009-07-13 16:01:31 -0700715
Bill Pemberton454f18a2009-07-27 16:47:24 -0400716 /* request->PortId = scmnd->device->channel; */
Hank Janssenbef4a342009-07-13 16:01:31 -0700717 request->Host = host_device_ctx->port;
718 request->Bus = scmnd->device->channel;
719 request->TargetId = scmnd->device->id;
720 request->LunId = scmnd->device->lun;
721
Bill Pembertonb856e732010-05-05 15:27:41 -0400722 /* ASSERT(scmnd->cmd_len <= 16); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700723 request->CdbLen = scmnd->cmd_len;
724 request->Cdb = scmnd->cmnd;
725
726 request->SenseBuffer = scmnd->sense_buffer;
727 request->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
728
729
Hank Janssenbef4a342009-07-13 16:01:31 -0700730 request->DataBuffer.Length = scsi_bufflen(scmnd);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700731 if (scsi_sg_count(scmnd)) {
732 sgl = (struct scatterlist *)scsi_sglist(scmnd);
Hank Janssenbef4a342009-07-13 16:01:31 -0700733
Bill Pemberton454f18a2009-07-27 16:47:24 -0400734 /* check if we need to bounce the sgl */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700735 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
736 DPRINT_INFO(STORVSC_DRV,
737 "need to bounce buffer for this scmnd %p",
738 scmnd);
739 cmd_request->bounce_sgl =
740 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
741 scsi_bufflen(scmnd));
742 if (!cmd_request->bounce_sgl) {
743 DPRINT_ERR(STORVSC_DRV,
744 "unable to create bounce buffer for "
745 "this scmnd %p", scmnd);
Hank Janssenbef4a342009-07-13 16:01:31 -0700746
747 scmnd->scsi_done = NULL;
748 scmnd->host_scribble = NULL;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700749 kmem_cache_free(host_device_ctx->request_pool,
750 cmd_request);
Hank Janssenbef4a342009-07-13 16:01:31 -0700751
752 return SCSI_MLQUEUE_HOST_BUSY;
753 }
754
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700755 cmd_request->bounce_sgl_count =
756 ALIGN_UP(scsi_bufflen(scmnd), PAGE_SIZE) >>
757 PAGE_SHIFT;
Hank Janssenbef4a342009-07-13 16:01:31 -0700758
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700759 /*
760 * FIXME: We can optimize on reads by just skipping
761 * this
762 */
763 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
764 scsi_sg_count(scmnd));
Hank Janssenbef4a342009-07-13 16:01:31 -0700765
766 sgl = cmd_request->bounce_sgl;
767 }
768
769 request->DataBuffer.Offset = sgl[0].offset;
770
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700771 for (i = 0; i < scsi_sg_count(scmnd); i++) {
Lars Lindley0686e4f2010-03-11 23:51:23 +0100772 DPRINT_DBG(STORVSC_DRV, "sgl[%d] len %d offset %d\n",
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700773 i, sgl[i].length, sgl[i].offset);
774 request->DataBuffer.PfnArray[i] =
775 page_to_pfn(sg_page((&sgl[i])));
Hank Janssenbef4a342009-07-13 16:01:31 -0700776 }
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700777 } else if (scsi_sglist(scmnd)) {
Bill Pembertonb856e732010-05-05 15:27:41 -0400778 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700779 request->DataBuffer.Offset =
780 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
781 request->DataBuffer.PfnArray[0] =
782 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
Hank Janssenbef4a342009-07-13 16:01:31 -0700783 }
Hank Janssenbef4a342009-07-13 16:01:31 -0700784
785retry_request:
Bill Pemberton454f18a2009-07-27 16:47:24 -0400786 /* Invokes the vsc to start an IO */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700787 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 Janssenbef4a342009-07-13 16:01:31 -0700794
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700795 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 Janssenbef4a342009-07-13 16:01:31 -0700805 }
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-Hartmanff568d32009-09-02 08:37:47 -0700820static int storvsc_merge_bvec(struct request_queue *q,
821 struct bvec_merge_data *bmd, struct bio_vec *bvec)
Hank Janssenbef4a342009-07-13 16:01:31 -0700822{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700823 /* checking done by caller. */
824 return bvec->bv_len;
Hank Janssenbef4a342009-07-13 16:01:31 -0700825}
Hank Janssenbef4a342009-07-13 16:01:31 -0700826
Hank Janssen3e189512010-03-04 22:11:00 +0000827/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700828 * storvsc_device_configure - Configure the specified scsi device
829 */
Hank Janssenbef4a342009-07-13 16:01:31 -0700830static int storvsc_device_alloc(struct scsi_device *sdevice)
831{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700832 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 Janssenbef4a342009-07-13 16:01:31 -0700838 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
Hank Janssenbef4a342009-07-13 16:01:31 -0700839 return 0;
840}
841
842static int storvsc_device_configure(struct scsi_device *sdevice)
843{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700844 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice,
845 sdevice->queue_depth);
Hank Janssenbef4a342009-07-13 16:01:31 -0700846
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700847 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 Janssenbef4a342009-07-13 16:01:31 -0700851
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700852 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
853 sdevice, PAGE_SIZE);
Hank Janssenbef4a342009-07-13 16:01:31 -0700854 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
855
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700856 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
857 sdevice);
Hank Janssenbef4a342009-07-13 16:01:31 -0700858 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
859
860 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
Bill Pemberton454f18a2009-07-27 16:47:24 -0400861 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700862
863 return 0;
864}
865
Hank Janssen3e189512010-03-04 22:11:00 +0000866/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700867 * storvsc_host_reset_handler - Reset the scsi HBA
868 */
Hank Janssenbef4a342009-07-13 16:01:31 -0700869static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
870{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700871 int ret;
872 struct host_device_context *host_device_ctx =
873 (struct host_device_context *)scmnd->device->host->hostdata;
Haiyang Zhangf916a342010-02-17 20:58:47 +0000874 struct vm_device *device_ctx = host_device_ctx->device_ctx;
Hank Janssenbef4a342009-07-13 16:01:31 -0700875
876 DPRINT_ENTER(STORVSC_DRV);
877
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700878 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
879 scmnd->device, &device_ctx->device_obj);
Hank Janssenbef4a342009-07-13 16:01:31 -0700880
Bill Pemberton454f18a2009-07-27 16:47:24 -0400881 /* Invokes the vsc to reset the host/bus */
Greg Kroah-Hartman354b0a62010-01-08 10:24:26 -0800882 ret = StorVscOnHostReset(&device_ctx->device_obj);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700883 if (ret != 0) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700884 DPRINT_EXIT(STORVSC_DRV);
885 return ret;
886 }
887
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700888 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
889 scmnd->device, &device_ctx->device_obj);
Hank Janssenbef4a342009-07-13 16:01:31 -0700890
891 DPRINT_EXIT(STORVSC_DRV);
892
893 return ret;
894}
895
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700896static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
897 sector_t capacity, int *info)
Hank Janssenbef4a342009-07-13 16:01:31 -0700898{
899 sector_t total_sectors = capacity;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700900 sector_t cylinder_times_heads = 0;
901 sector_t temp = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700902
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700903 int sectors_per_track = 0;
904 int heads = 0;
905 int cylinders = 0;
906 int rem = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700907
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700908 if (total_sectors > (65535 * 16 * 255))
909 total_sectors = (65535 * 16 * 255);
Hank Janssenbef4a342009-07-13 16:01:31 -0700910
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700911 if (total_sectors >= (65535 * 16 * 63)) {
912 sectors_per_track = 255;
913 heads = 16;
Hank Janssenbef4a342009-07-13 16:01:31 -0700914
915 cylinder_times_heads = total_sectors;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700916 /* 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 Janssenbef4a342009-07-13 16:01:31 -0700920
921 cylinder_times_heads = total_sectors;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700922 /* sector_div stores the quotient in cylinder_times_heads */
923 rem = sector_div(cylinder_times_heads, sectors_per_track);
Hank Janssenbef4a342009-07-13 16:01:31 -0700924
925 temp = cylinder_times_heads + 1023;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700926 /* sector_div stores the quotient in temp */
927 rem = sector_div(temp, 1024);
Hank Janssenbef4a342009-07-13 16:01:31 -0700928
929 heads = temp;
930
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700931 if (heads < 4)
932 heads = 4;
Hank Janssenbef4a342009-07-13 16:01:31 -0700933
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700934 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
935 sectors_per_track = 31;
936 heads = 16;
Hank Janssenbef4a342009-07-13 16:01:31 -0700937
938 cylinder_times_heads = total_sectors;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700939 /*
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 Janssenbef4a342009-07-13 16:01:31 -0700946
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700947 if (cylinder_times_heads >= (heads * 1024)) {
948 sectors_per_track = 63;
949 heads = 16;
Hank Janssenbef4a342009-07-13 16:01:31 -0700950
951 cylinder_times_heads = total_sectors;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700952 /*
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 Janssenbef4a342009-07-13 16:01:31 -0700960
961 temp = cylinder_times_heads;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700962 /* sector_div stores the quotient in temp */
963 rem = sector_div(temp, heads);
Hank Janssenbef4a342009-07-13 16:01:31 -0700964 cylinders = temp;
965
966 info[0] = heads;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700967 info[1] = sectors_per_track;
968 info[2] = cylinders;
Hank Janssenbef4a342009-07-13 16:01:31 -0700969
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700970 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
971 sectors_per_track);
Hank Janssenbef4a342009-07-13 16:01:31 -0700972
973 return 0;
974}
975
Hank Janssenbef4a342009-07-13 16:01:31 -0700976static int __init storvsc_init(void)
977{
978 int ret;
979
980 DPRINT_ENTER(STORVSC_DRV);
Hank Janssenbef4a342009-07-13 16:01:31 -0700981 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
Hank Janssenbef4a342009-07-13 16:01:31 -0700982 ret = storvsc_drv_init(StorVscInitialize);
Hank Janssenbef4a342009-07-13 16:01:31 -0700983 DPRINT_EXIT(STORVSC_DRV);
Hank Janssenbef4a342009-07-13 16:01:31 -0700984 return ret;
985}
986
987static void __exit storvsc_exit(void)
988{
989 DPRINT_ENTER(STORVSC_DRV);
Hank Janssenbef4a342009-07-13 16:01:31 -0700990 storvsc_drv_exit();
Hank Janssenbef4a342009-07-13 16:01:31 -0700991 DPRINT_ENTER(STORVSC_DRV);
992}
993
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700994MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +0000995MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -0700996MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
Hank Janssenbef4a342009-07-13 16:01:31 -0700997module_init(storvsc_init);
998module_exit(storvsc_exit);