blob: ba15f54a315c3bb50b2dc0b4c35d100602bc16ec [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>
22#include <linux/module.h>
23#include <linux/device.h>
24#include <linux/blkdev.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070025#include <scsi/scsi.h>
26#include <scsi/scsi_cmnd.h>
27#include <scsi/scsi_host.h>
28#include <scsi/scsi_device.h>
29#include <scsi/scsi_tcq.h>
30#include <scsi/scsi_eh.h>
31#include <scsi/scsi_devinfo.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070032#include <scsi/scsi_dbg.h>
Greg Kroah-Hartman4983b392009-08-19 16:14:47 -070033#include "osd.h"
Greg Kroah-Hartman645954c2009-08-28 16:22:59 -070034#include "logging.h"
Greg Kroah-Hartman870cde82009-08-19 16:21:28 -070035#include "vmbus.h"
Greg Kroah-Hartman731a7882009-08-28 16:23:49 -070036#include "StorVscApi.h"
Hank Janssenbef4a342009-07-13 16:01:31 -070037
Hank Janssenbef4a342009-07-13 16:01:31 -070038
Hank Janssenbef4a342009-07-13 16:01:31 -070039struct host_device_context {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070040 /* must be 1st field
41 * FIXME this is a bug */
42 struct work_struct host_rescan_work;
43
44 /* point back to our device context */
45 struct device_context *device_ctx;
46 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 void storvsc_host_rescan_callback(struct work_struct *work);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070081static void storvsc_host_rescan(struct hv_device *device_obj);
Hank Janssenbef4a342009-07-13 16:01:31 -070082static int storvsc_remove(struct device *dev);
83
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070084static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
85 unsigned int sg_count,
86 unsigned int len);
87static void destroy_bounce_buffer(struct scatterlist *sgl,
88 unsigned int sg_count);
Hank Janssenbef4a342009-07-13 16:01:31 -070089static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070090static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
91 struct scatterlist *bounce_sgl,
92 unsigned int orig_sgl_count);
93static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
94 struct scatterlist *bounce_sgl,
95 unsigned int orig_sgl_count);
Hank Janssenbef4a342009-07-13 16:01:31 -070096
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070097static int storvsc_report_luns(struct scsi_device *sdev, unsigned int luns[],
98 unsigned int *lun_count);
99static int storvsc_get_chs(struct scsi_device *sdev, struct block_device *bdev,
100 sector_t capacity, int *info);
Hank Janssenbef4a342009-07-13 16:01:31 -0700101
102
103static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
104
Bill Pemberton454f18a2009-07-27 16:47:24 -0400105/* The one and only one */
Hank Janssenbef4a342009-07-13 16:01:31 -0700106static struct storvsc_driver_context g_storvsc_drv;
107
Bill Pemberton454f18a2009-07-27 16:47:24 -0400108/* Scsi driver */
Hank Janssenbef4a342009-07-13 16:01:31 -0700109static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700110 .module = THIS_MODULE,
111 .name = "storvsc_host_t",
112 .bios_param = storvsc_get_chs,
113 .queuecommand = storvsc_queuecommand,
114 .eh_host_reset_handler = storvsc_host_reset_handler,
115 .slave_alloc = storvsc_device_alloc,
116 .slave_configure = storvsc_device_configure,
117 .cmd_per_lun = 1,
118 /* 64 max_queue * 1 target */
119 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
120 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400121 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700122 /* currently 32 */
123 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
124 /*
125 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
126 * into 1 sg element. If set, we must limit the max_segment_size to
127 * PAGE_SIZE, otherwise we may get 1 sg element that represents
128 * multiple
129 */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400130 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700131 .use_clustering = ENABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400132 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700133 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -0700134};
135
136
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700137/**
138 * storvsc_drv_init - StorVsc driver initialization.
139 */
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700140static int storvsc_drv_init(PFN_DRIVERINITIALIZE pfn_drv_init)
Hank Janssenbef4a342009-07-13 16:01:31 -0700141{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700142 int ret;
Greg Kroah-Hartman9f0c7d22009-08-28 16:20:07 -0700143 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700144 struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx;
Hank Janssenbef4a342009-07-13 16:01:31 -0700145
146 DPRINT_ENTER(STORVSC_DRV);
147
148 vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
149
150 storvsc_drv_obj->RingBufferSize = storvsc_ringbuffer_size;
151 storvsc_drv_obj->OnHostRescan = storvsc_host_rescan;
152
Bill Pemberton454f18a2009-07-27 16:47:24 -0400153 /* Callback to client driver to complete the initialization */
Hank Janssenbef4a342009-07-13 16:01:31 -0700154 pfn_drv_init(&storvsc_drv_obj->Base);
155
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700156 DPRINT_INFO(STORVSC_DRV,
157 "request extension size %u, max outstanding reqs %u",
158 storvsc_drv_obj->RequestExtSize,
159 storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
Hank Janssenbef4a342009-07-13 16:01:31 -0700160
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700161 if (storvsc_drv_obj->MaxOutstandingRequestsPerChannel <
162 STORVSC_MAX_IO_REQUESTS) {
163 DPRINT_ERR(STORVSC_DRV,
164 "The number of outstanding io requests (%d) "
165 "is larger than that supported (%d) internally.",
166 STORVSC_MAX_IO_REQUESTS,
167 storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
Hank Janssenbef4a342009-07-13 16:01:31 -0700168 return -1;
169 }
170
171 drv_ctx->driver.name = storvsc_drv_obj->Base.name;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700172 memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType,
173 sizeof(struct hv_guid));
Hank Janssenbef4a342009-07-13 16:01:31 -0700174
Hank Janssenbef4a342009-07-13 16:01:31 -0700175 drv_ctx->probe = storvsc_probe;
176 drv_ctx->remove = storvsc_remove;
Hank Janssenbef4a342009-07-13 16:01:31 -0700177
Bill Pemberton454f18a2009-07-27 16:47:24 -0400178 /* The driver belongs to vmbus */
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400179 ret = vmbus_child_driver_register(drv_ctx);
Hank Janssenbef4a342009-07-13 16:01:31 -0700180
181 DPRINT_EXIT(STORVSC_DRV);
182
183 return ret;
184}
185
Hank Janssenbef4a342009-07-13 16:01:31 -0700186static int storvsc_drv_exit_cb(struct device *dev, void *data)
187{
188 struct device **curr = (struct device **)data;
189 *curr = dev;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400190 return 1; /* stop iterating */
Hank Janssenbef4a342009-07-13 16:01:31 -0700191}
192
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700193static void storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -0700194{
Greg Kroah-Hartman9f0c7d22009-08-28 16:20:07 -0700195 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700196 struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx;
197 struct device *current_dev = NULL;
Bill Pemberton2295ba22009-07-28 13:46:22 -0400198 int ret;
Hank Janssenbef4a342009-07-13 16:01:31 -0700199
Hank Janssenbef4a342009-07-13 16:01:31 -0700200 DPRINT_ENTER(STORVSC_DRV);
201
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700202 while (1) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700203 current_dev = NULL;
204
Bill Pemberton454f18a2009-07-27 16:47:24 -0400205 /* Get the device */
Bill Pemberton2295ba22009-07-28 13:46:22 -0400206 ret = driver_for_each_device(&drv_ctx->driver, NULL,
207 (void *) &current_dev,
208 storvsc_drv_exit_cb);
209
210 if (ret)
211 DPRINT_WARN(STORVSC_DRV,
212 "driver_for_each_device returned %d", ret);
Hank Janssenbef4a342009-07-13 16:01:31 -0700213
214 if (current_dev == NULL)
215 break;
216
Bill Pemberton454f18a2009-07-27 16:47:24 -0400217 /* Initiate removal from the top-down */
Hank Janssenbef4a342009-07-13 16:01:31 -0700218 device_unregister(current_dev);
219 }
220
221 if (storvsc_drv_obj->Base.OnCleanup)
222 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
223
224 vmbus_child_driver_unregister(drv_ctx);
225
226 DPRINT_EXIT(STORVSC_DRV);
227
228 return;
229}
230
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700231/**
232 * storvsc_probe - Add a new device for this driver
233 */
Hank Janssenbef4a342009-07-13 16:01:31 -0700234static int storvsc_probe(struct device *device)
235{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700236 int ret;
237 struct driver_context *driver_ctx =
238 driver_to_driver_context(device->driver);
239 struct storvsc_driver_context *storvsc_drv_ctx =
240 (struct storvsc_driver_context *)driver_ctx;
241 struct storvsc_driver_object *storvsc_drv_obj =
242 &storvsc_drv_ctx->drv_obj;
Hank Janssenbef4a342009-07-13 16:01:31 -0700243 struct device_context *device_ctx = device_to_device_context(device);
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200244 struct hv_device *device_obj = &device_ctx->device_obj;
Hank Janssenbef4a342009-07-13 16:01:31 -0700245 struct Scsi_Host *host;
246 struct host_device_context *host_device_ctx;
Greg Kroah-Hartman9f0c7d22009-08-28 16:20:07 -0700247 struct storvsc_device_info device_info;
Hank Janssenbef4a342009-07-13 16:01:31 -0700248
249 DPRINT_ENTER(STORVSC_DRV);
250
251 if (!storvsc_drv_obj->Base.OnDeviceAdd)
252 return -1;
253
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700254 host = scsi_host_alloc(&scsi_driver,
255 sizeof(struct host_device_context));
256 if (!host) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700257 DPRINT_ERR(STORVSC_DRV, "unable to allocate scsi host object");
258 return -ENOMEM;
259 }
260
Greg Kroah-Hartman0883c522009-07-24 10:58:22 -0700261 dev_set_drvdata(device, host);
Hank Janssenbef4a342009-07-13 16:01:31 -0700262
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700263 host_device_ctx = (struct host_device_context *)host->hostdata;
Hank Janssenbef4a342009-07-13 16:01:31 -0700264 memset(host_device_ctx, 0, sizeof(struct host_device_context));
265
266 host_device_ctx->port = host->host_no;
267 host_device_ctx->device_ctx = device_ctx;
268
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700269 INIT_WORK(&host_device_ctx->host_rescan_work,
270 storvsc_host_rescan_callback);
Hank Janssenbef4a342009-07-13 16:01:31 -0700271
Hank Janssenbef4a342009-07-13 16:01:31 -0700272 host_device_ctx->request_pool =
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700273 kmem_cache_create(dev_name(&device_ctx->device),
274 sizeof(struct storvsc_cmd_request) +
275 storvsc_drv_obj->RequestExtSize, 0,
276 SLAB_HWCACHE_ALIGN, NULL);
Hank Janssenbef4a342009-07-13 16:01:31 -0700277
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700278 if (!host_device_ctx->request_pool) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700279 scsi_host_put(host);
280 DPRINT_EXIT(STORVSC_DRV);
281
282 return -ENOMEM;
283 }
284
285 device_info.PortNumber = host->host_no;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400286 /* Call to the vsc driver to add the device */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700287 ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj,
288 (void *)&device_info);
289 if (ret != 0) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700290 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 Pemberton454f18a2009-07-27 16:47:24 -0400298 /* host_device_ctx->port = device_info.PortNumber; */
Hank Janssenbef4a342009-07-13 16:01:31 -0700299 host_device_ctx->path = device_info.PathId;
300 host_device_ctx->target = device_info.TargetId;
301
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700302 /* max # of devices per target */
303 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
304 /* max # of targets per channel */
305 host->max_id = STORVSC_MAX_TARGETS;
306 /* max # of channels */
307 host->max_channel = STORVSC_MAX_CHANNELS - 1;
Hank Janssenbef4a342009-07-13 16:01:31 -0700308
Bill Pemberton454f18a2009-07-27 16:47:24 -0400309 /* Register the HBA and start the scsi bus scan */
Hank Janssenbef4a342009-07-13 16:01:31 -0700310 ret = scsi_add_host(host, device);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700311 if (ret != 0) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700312 DPRINT_ERR(STORVSC_DRV, "unable to add scsi host device");
313
314 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
315
316 kmem_cache_destroy(host_device_ctx->request_pool);
317 scsi_host_put(host);
318 DPRINT_EXIT(STORVSC_DRV);
319
320 return -1;
321 }
322
323 scsi_scan_host(host);
324
325 DPRINT_EXIT(STORVSC_DRV);
326
327 return ret;
328}
329
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700330/**
331 * storvsc_remove - Callback when our device is removed
332 */
Hank Janssenbef4a342009-07-13 16:01:31 -0700333static int storvsc_remove(struct device *device)
334{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700335 int ret;
336 struct driver_context *driver_ctx =
337 driver_to_driver_context(device->driver);
338 struct storvsc_driver_context *storvsc_drv_ctx =
339 (struct storvsc_driver_context *)driver_ctx;
340 struct storvsc_driver_object *storvsc_drv_obj =
341 &storvsc_drv_ctx->drv_obj;
Hank Janssenbef4a342009-07-13 16:01:31 -0700342 struct device_context *device_ctx = device_to_device_context(device);
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200343 struct hv_device *device_obj = &device_ctx->device_obj;
Greg Kroah-Hartman0883c522009-07-24 10:58:22 -0700344 struct Scsi_Host *host = dev_get_drvdata(device);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700345 struct host_device_context *host_device_ctx =
346 (struct host_device_context *)host->hostdata;
Hank Janssenbef4a342009-07-13 16:01:31 -0700347
348
349 DPRINT_ENTER(STORVSC_DRV);
350
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700351 if (!storvsc_drv_obj->Base.OnDeviceRemove) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700352 DPRINT_EXIT(STORVSC_DRV);
353 return -1;
354 }
355
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700356 /*
357 * Call to the vsc driver to let it know that the device is being
358 * removed
359 */
Hank Janssenbef4a342009-07-13 16:01:31 -0700360 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700361 if (ret != 0) {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400362 /* TODO: */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700363 DPRINT_ERR(STORVSC, "unable to remove vsc device (ret %d)",
364 ret);
Hank Janssenbef4a342009-07-13 16:01:31 -0700365 }
366
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700367 if (host_device_ctx->request_pool) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700368 kmem_cache_destroy(host_device_ctx->request_pool);
369 host_device_ctx->request_pool = NULL;
370 }
371
372 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
373 scsi_remove_host(host);
374
375 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
376 scsi_host_put(host);
377
378 DPRINT_EXIT(STORVSC_DRV);
379
380 return ret;
381}
382
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700383/**
384 * storvsc_commmand_completion - Command completion processing
385 */
Nicolas Palix0b3f6832009-07-29 14:10:19 +0200386static void storvsc_commmand_completion(struct hv_storvsc_request *request)
Hank Janssenbef4a342009-07-13 16:01:31 -0700387{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700388 struct storvsc_cmd_request *cmd_request =
389 (struct storvsc_cmd_request *)request->Context;
Hank Janssenbef4a342009-07-13 16:01:31 -0700390 struct scsi_cmnd *scmnd = cmd_request->cmd;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700391 struct host_device_context *host_device_ctx =
392 (struct host_device_context *)scmnd->device->host->hostdata;
Hank Janssenbef4a342009-07-13 16:01:31 -0700393 void (*scsi_done_fn)(struct scsi_cmnd *);
Hank Janssenbef4a342009-07-13 16:01:31 -0700394 struct scsi_sense_hdr sense_hdr;
Hank Janssenbef4a342009-07-13 16:01:31 -0700395
396 ASSERT(request == &cmd_request->request);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700397 ASSERT((unsigned long)scmnd->host_scribble ==
398 (unsigned long)cmd_request);
Hank Janssenbef4a342009-07-13 16:01:31 -0700399 ASSERT(scmnd);
400 ASSERT(scmnd->scsi_done);
401
402 DPRINT_ENTER(STORVSC_DRV);
403
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700404 if (cmd_request->bounce_sgl_count) {
405 /* using bounce buffer */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400406 /* printk("copy_from_bounce_buffer\n"); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700407
Bill Pemberton454f18a2009-07-27 16:47:24 -0400408 /* FIXME: We can optimize on writes by just skipping this */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700409 copy_from_bounce_buffer(scsi_sglist(scmnd),
410 cmd_request->bounce_sgl,
411 scsi_sg_count(scmnd));
412 destroy_bounce_buffer(cmd_request->bounce_sgl,
413 cmd_request->bounce_sgl_count);
Hank Janssenbef4a342009-07-13 16:01:31 -0700414 }
415
416 scmnd->result = request->Status;
417
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700418 if (scmnd->result) {
419 if (scsi_normalize_sense(scmnd->sense_buffer,
420 request->SenseBufferSize, &sense_hdr))
Hank Janssenbef4a342009-07-13 16:01:31 -0700421 scsi_print_sense_hdr("storvsc", &sense_hdr);
Hank Janssenbef4a342009-07-13 16:01:31 -0700422 }
423
424 ASSERT(request->BytesXfer <= request->DataBuffer.Length);
Hank Janssenbef4a342009-07-13 16:01:31 -0700425 scsi_set_resid(scmnd, request->DataBuffer.Length - request->BytesXfer);
Hank Janssenbef4a342009-07-13 16:01:31 -0700426
427 scsi_done_fn = scmnd->scsi_done;
428
429 scmnd->host_scribble = NULL;
430 scmnd->scsi_done = NULL;
431
Bill Pemberton454f18a2009-07-27 16:47:24 -0400432 /* !!DO NOT MODIFY the scmnd after this call */
Hank Janssenbef4a342009-07-13 16:01:31 -0700433 scsi_done_fn(scmnd);
434
435 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
436
437 DPRINT_EXIT(STORVSC_DRV);
438}
439
440static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
441{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700442 int i;
Hank Janssenbef4a342009-07-13 16:01:31 -0700443
Bill Pemberton454f18a2009-07-27 16:47:24 -0400444 /* No need to check */
Hank Janssenbef4a342009-07-13 16:01:31 -0700445 if (sg_count < 2)
446 return -1;
447
Bill Pemberton454f18a2009-07-27 16:47:24 -0400448 /* We have at least 2 sg entries */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700449 for (i = 0; i < sg_count; i++) {
450 if (i == 0) {
451 /* make sure 1st one does not have hole */
Hank Janssenbef4a342009-07-13 16:01:31 -0700452 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
453 return i;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700454 } else if (i == sg_count - 1) {
455 /* make sure last one does not have hole */
Hank Janssenbef4a342009-07-13 16:01:31 -0700456 if (sgl[i].offset != 0)
457 return i;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700458 } else {
459 /* make sure no hole in the middle */
Hank Janssenbef4a342009-07-13 16:01:31 -0700460 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
Hank Janssenbef4a342009-07-13 16:01:31 -0700461 return i;
Hank Janssenbef4a342009-07-13 16:01:31 -0700462 }
463 }
464 return -1;
465}
466
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700467static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
468 unsigned int sg_count,
469 unsigned int len)
Hank Janssenbef4a342009-07-13 16:01:31 -0700470{
471 int i;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700472 int num_pages;
473 struct scatterlist *bounce_sgl;
Hank Janssenbef4a342009-07-13 16:01:31 -0700474 struct page *page_buf;
475
476 num_pages = ALIGN_UP(len, PAGE_SIZE) >> PAGE_SHIFT;
477
Nicolas Palix06da0bc2009-07-22 15:39:13 +0200478 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
Hank Janssenbef4a342009-07-13 16:01:31 -0700479 if (!bounce_sgl)
Hank Janssenbef4a342009-07-13 16:01:31 -0700480 return NULL;
Hank Janssenbef4a342009-07-13 16:01:31 -0700481
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700482 for (i = 0; i < num_pages; i++) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700483 page_buf = alloc_page(GFP_ATOMIC);
484 if (!page_buf)
Hank Janssenbef4a342009-07-13 16:01:31 -0700485 goto cleanup;
Hank Janssenbef4a342009-07-13 16:01:31 -0700486 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700487 }
488
489 return bounce_sgl;
490
491cleanup:
492 destroy_bounce_buffer(bounce_sgl, num_pages);
493 return NULL;
494}
495
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700496static void destroy_bounce_buffer(struct scatterlist *sgl,
497 unsigned int sg_count)
Hank Janssenbef4a342009-07-13 16:01:31 -0700498{
499 int i;
500 struct page *page_buf;
501
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700502 for (i = 0; i < sg_count; i++) {
503 page_buf = sg_page((&sgl[i]));
504 if (page_buf != NULL)
Hank Janssenbef4a342009-07-13 16:01:31 -0700505 __free_page(page_buf);
Hank Janssenbef4a342009-07-13 16:01:31 -0700506 }
507
508 kfree(sgl);
509}
510
Bill Pemberton454f18a2009-07-27 16:47:24 -0400511/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700512static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
513 struct scatterlist *bounce_sgl,
514 unsigned int orig_sgl_count)
Hank Janssenbef4a342009-07-13 16:01:31 -0700515{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700516 int i;
517 int j = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700518 unsigned long src, dest;
519 unsigned int srclen, destlen, copylen;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700520 unsigned int total_copied = 0;
521 unsigned long bounce_addr = 0;
522 unsigned long src_addr = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700523 unsigned long flags;
524
525 local_irq_save(flags);
526
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700527 for (i = 0; i < orig_sgl_count; i++) {
528 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
529 KM_IRQ0) + orig_sgl[i].offset;
Hank Janssenbef4a342009-07-13 16:01:31 -0700530 src = src_addr;
531 srclen = orig_sgl[i].length;
532
Hank Janssenbef4a342009-07-13 16:01:31 -0700533 ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
534
535 if (j == 0)
Hank Janssenbef4a342009-07-13 16:01:31 -0700536 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700537
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700538 while (srclen) {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400539 /* assume bounce offset always == 0 */
Hank Janssenbef4a342009-07-13 16:01:31 -0700540 dest = bounce_addr + bounce_sgl[j].length;
541 destlen = PAGE_SIZE - bounce_sgl[j].length;
542
Greg Kroah-Hartmanfc6a4b22009-07-15 11:05:14 -0700543 copylen = min(srclen, destlen);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700544 memcpy((void *)dest, (void *)src, copylen);
Hank Janssenbef4a342009-07-13 16:01:31 -0700545
546 total_copied += copylen;
547 bounce_sgl[j].length += copylen;
548 srclen -= copylen;
549 src += copylen;
550
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700551 if (bounce_sgl[j].length == PAGE_SIZE) {
552 /* full..move to next entry */
553 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700554 j++;
555
Bill Pemberton454f18a2009-07-27 16:47:24 -0400556 /* if we need to use another bounce buffer */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700557 if (srclen || i != orig_sgl_count - 1)
Hank Janssenbef4a342009-07-13 16:01:31 -0700558 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700559 } else if (srclen == 0 && i == orig_sgl_count - 1) {
560 /* unmap the last bounce that is < PAGE_SIZE */
561 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700562 }
563 }
564
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700565 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700566 }
567
568 local_irq_restore(flags);
569
570 return total_copied;
571}
572
Bill Pemberton454f18a2009-07-27 16:47:24 -0400573/* Assume the original sgl has enough room */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700574static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
575 struct scatterlist *bounce_sgl,
576 unsigned int orig_sgl_count)
Hank Janssenbef4a342009-07-13 16:01:31 -0700577{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700578 int i;
579 int j = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700580 unsigned long src, dest;
581 unsigned int srclen, destlen, copylen;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700582 unsigned int total_copied = 0;
583 unsigned long bounce_addr = 0;
584 unsigned long dest_addr = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700585 unsigned long flags;
586
587 local_irq_save(flags);
588
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700589 for (i = 0; i < orig_sgl_count; i++) {
590 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
591 KM_IRQ0) + orig_sgl[i].offset;
Hank Janssenbef4a342009-07-13 16:01:31 -0700592 dest = dest_addr;
593 destlen = orig_sgl[i].length;
594 ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
595
596 if (j == 0)
Hank Janssenbef4a342009-07-13 16:01:31 -0700597 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700598
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700599 while (destlen) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700600 src = bounce_addr + bounce_sgl[j].offset;
601 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
602
Greg Kroah-Hartmanfc6a4b22009-07-15 11:05:14 -0700603 copylen = min(srclen, destlen);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700604 memcpy((void *)dest, (void *)src, copylen);
Hank Janssenbef4a342009-07-13 16:01:31 -0700605
606 total_copied += copylen;
607 bounce_sgl[j].offset += copylen;
608 destlen -= copylen;
609 dest += copylen;
610
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700611 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
612 /* full */
613 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700614 j++;
615
Bill Pemberton454f18a2009-07-27 16:47:24 -0400616 /* if we need to use another bounce buffer */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700617 if (destlen || i != orig_sgl_count - 1)
Hank Janssenbef4a342009-07-13 16:01:31 -0700618 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700619 } else if (destlen == 0 && i == orig_sgl_count - 1) {
620 /* unmap the last bounce that is < PAGE_SIZE */
621 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700622 }
623 }
624
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700625 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
626 KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700627 }
628
629 local_irq_restore(flags);
630
631 return total_copied;
632}
633
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700634/**
635 * storvsc_queuecommand - Initiate command processing
636 */
637static int storvsc_queuecommand(struct scsi_cmnd *scmnd,
638 void (*done)(struct scsi_cmnd *))
Hank Janssenbef4a342009-07-13 16:01:31 -0700639{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700640 int ret;
641 struct host_device_context *host_device_ctx =
642 (struct host_device_context *)scmnd->device->host->hostdata;
643 struct device_context *device_ctx = host_device_ctx->device_ctx;
644 struct driver_context *driver_ctx =
645 driver_to_driver_context(device_ctx->device.driver);
646 struct storvsc_driver_context *storvsc_drv_ctx =
647 (struct storvsc_driver_context *)driver_ctx;
648 struct storvsc_driver_object *storvsc_drv_obj =
649 &storvsc_drv_ctx->drv_obj;
Nicolas Palix0b3f6832009-07-29 14:10:19 +0200650 struct hv_storvsc_request *request;
Hank Janssenbef4a342009-07-13 16:01:31 -0700651 struct storvsc_cmd_request *cmd_request;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700652 unsigned int request_size = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700653 int i;
654 struct scatterlist *sgl;
655
656 DPRINT_ENTER(STORVSC_DRV);
657
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700658 DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d "
659 "queue depth %d tagged %d", scmnd, scmnd->sc_data_direction,
660 scsi_sg_count(scmnd), scsi_sglist(scmnd),
661 scsi_bufflen(scmnd), scmnd->device->queue_depth,
662 scmnd->device->tagged_supported);
Hank Janssenbef4a342009-07-13 16:01:31 -0700663
Bill Pemberton454f18a2009-07-27 16:47:24 -0400664 /* If retrying, no need to prep the cmd */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700665 if (scmnd->host_scribble) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700666 ASSERT(scmnd->scsi_done != NULL);
667
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700668 cmd_request =
669 (struct storvsc_cmd_request *)scmnd->host_scribble;
670 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
671 scmnd, cmd_request);
Hank Janssenbef4a342009-07-13 16:01:31 -0700672
673 goto retry_request;
674 }
675
676 ASSERT(scmnd->scsi_done == NULL);
677 ASSERT(scmnd->host_scribble == NULL);
678
679 scmnd->scsi_done = done;
680
681 request_size = sizeof(struct storvsc_cmd_request);
682
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700683 cmd_request = kmem_cache_alloc(host_device_ctx->request_pool,
684 GFP_ATOMIC);
685 if (!cmd_request) {
686 DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate "
687 "storvsc_cmd_request...marking queue busy", scmnd);
Hank Janssenbef4a342009-07-13 16:01:31 -0700688 scmnd->scsi_done = NULL;
689 return SCSI_MLQUEUE_DEVICE_BUSY;
690 }
691
Bill Pemberton454f18a2009-07-27 16:47:24 -0400692 /* Setup the cmd request */
Hank Janssenbef4a342009-07-13 16:01:31 -0700693 cmd_request->bounce_sgl_count = 0;
694 cmd_request->bounce_sgl = NULL;
695 cmd_request->cmd = scmnd;
696
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700697 scmnd->host_scribble = (unsigned char *)cmd_request;
Hank Janssenbef4a342009-07-13 16:01:31 -0700698
699 request = &cmd_request->request;
700
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700701 request->Extension =
702 (void *)((unsigned long)cmd_request + request_size);
703 DPRINT_DBG(STORVSC_DRV, "req %p size %d ext %d", request, request_size,
704 storvsc_drv_obj->RequestExtSize);
Hank Janssenbef4a342009-07-13 16:01:31 -0700705
Bill Pemberton454f18a2009-07-27 16:47:24 -0400706 /* Build the SRB */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700707 switch (scmnd->sc_data_direction) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700708 case DMA_TO_DEVICE:
709 request->Type = WRITE_TYPE;
710 break;
711 case DMA_FROM_DEVICE:
712 request->Type = READ_TYPE;
713 break;
714 default:
715 request->Type = UNKNOWN_TYPE;
716 break;
717 }
718
719 request->OnIOCompletion = storvsc_commmand_completion;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400720 request->Context = cmd_request;/* scmnd; */
Hank Janssenbef4a342009-07-13 16:01:31 -0700721
Bill Pemberton454f18a2009-07-27 16:47:24 -0400722 /* request->PortId = scmnd->device->channel; */
Hank Janssenbef4a342009-07-13 16:01:31 -0700723 request->Host = host_device_ctx->port;
724 request->Bus = scmnd->device->channel;
725 request->TargetId = scmnd->device->id;
726 request->LunId = scmnd->device->lun;
727
728 ASSERT(scmnd->cmd_len <= 16);
729 request->CdbLen = scmnd->cmd_len;
730 request->Cdb = scmnd->cmnd;
731
732 request->SenseBuffer = scmnd->sense_buffer;
733 request->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
734
735
Hank Janssenbef4a342009-07-13 16:01:31 -0700736 request->DataBuffer.Length = scsi_bufflen(scmnd);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700737 if (scsi_sg_count(scmnd)) {
738 sgl = (struct scatterlist *)scsi_sglist(scmnd);
Hank Janssenbef4a342009-07-13 16:01:31 -0700739
Bill Pemberton454f18a2009-07-27 16:47:24 -0400740 /* check if we need to bounce the sgl */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700741 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
742 DPRINT_INFO(STORVSC_DRV,
743 "need to bounce buffer for this scmnd %p",
744 scmnd);
745 cmd_request->bounce_sgl =
746 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
747 scsi_bufflen(scmnd));
748 if (!cmd_request->bounce_sgl) {
749 DPRINT_ERR(STORVSC_DRV,
750 "unable to create bounce buffer for "
751 "this scmnd %p", scmnd);
Hank Janssenbef4a342009-07-13 16:01:31 -0700752
753 scmnd->scsi_done = NULL;
754 scmnd->host_scribble = NULL;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700755 kmem_cache_free(host_device_ctx->request_pool,
756 cmd_request);
Hank Janssenbef4a342009-07-13 16:01:31 -0700757
758 return SCSI_MLQUEUE_HOST_BUSY;
759 }
760
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700761 cmd_request->bounce_sgl_count =
762 ALIGN_UP(scsi_bufflen(scmnd), PAGE_SIZE) >>
763 PAGE_SHIFT;
Hank Janssenbef4a342009-07-13 16:01:31 -0700764
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700765 /*
766 * FIXME: We can optimize on reads by just skipping
767 * this
768 */
769 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
770 scsi_sg_count(scmnd));
Hank Janssenbef4a342009-07-13 16:01:31 -0700771
772 sgl = cmd_request->bounce_sgl;
773 }
774
775 request->DataBuffer.Offset = sgl[0].offset;
776
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700777 for (i = 0; i < scsi_sg_count(scmnd); i++) {
778 DPRINT_DBG(STORVSC_DRV, "sgl[%d] len %d offset %d \n",
779 i, sgl[i].length, sgl[i].offset);
780 request->DataBuffer.PfnArray[i] =
781 page_to_pfn(sg_page((&sgl[i])));
Hank Janssenbef4a342009-07-13 16:01:31 -0700782 }
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700783 } else if (scsi_sglist(scmnd)) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700784 ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700785 request->DataBuffer.Offset =
786 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
787 request->DataBuffer.PfnArray[0] =
788 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
789 } else {
Hank Janssenbef4a342009-07-13 16:01:31 -0700790 ASSERT(scsi_bufflen(scmnd) == 0);
791 }
Hank Janssenbef4a342009-07-13 16:01:31 -0700792
793retry_request:
Bill Pemberton454f18a2009-07-27 16:47:24 -0400794 /* Invokes the vsc to start an IO */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700795 ret = storvsc_drv_obj->OnIORequest(&device_ctx->device_obj,
796 &cmd_request->request);
797 if (ret == -1) {
798 /* no more space */
799 DPRINT_ERR(STORVSC_DRV,
800 "scmnd (%p) - queue FULL...marking queue busy",
801 scmnd);
Hank Janssenbef4a342009-07-13 16:01:31 -0700802
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700803 if (cmd_request->bounce_sgl_count) {
804 /*
805 * FIXME: We can optimize on writes by just skipping
806 * this
807 */
808 copy_from_bounce_buffer(scsi_sglist(scmnd),
809 cmd_request->bounce_sgl,
810 scsi_sg_count(scmnd));
811 destroy_bounce_buffer(cmd_request->bounce_sgl,
812 cmd_request->bounce_sgl_count);
Hank Janssenbef4a342009-07-13 16:01:31 -0700813 }
814
815 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
816
817 scmnd->scsi_done = NULL;
818 scmnd->host_scribble = NULL;
819
820 ret = SCSI_MLQUEUE_DEVICE_BUSY;
821 }
822
823 DPRINT_EXIT(STORVSC_DRV);
824
825 return ret;
826}
827
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700828static int storvsc_merge_bvec(struct request_queue *q,
829 struct bvec_merge_data *bmd, struct bio_vec *bvec)
Hank Janssenbef4a342009-07-13 16:01:31 -0700830{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700831 /* checking done by caller. */
832 return bvec->bv_len;
Hank Janssenbef4a342009-07-13 16:01:31 -0700833}
Hank Janssenbef4a342009-07-13 16:01:31 -0700834
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700835/**
836 * storvsc_device_configure - Configure the specified scsi device
837 */
Hank Janssenbef4a342009-07-13 16:01:31 -0700838static int storvsc_device_alloc(struct scsi_device *sdevice)
839{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700840 DPRINT_DBG(STORVSC_DRV, "sdev (%p) - setting device flag to %d",
841 sdevice, BLIST_SPARSELUN);
842 /*
843 * This enables luns to be located sparsely. Otherwise, we may not
844 * discovered them.
845 */
Hank Janssenbef4a342009-07-13 16:01:31 -0700846 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
Hank Janssenbef4a342009-07-13 16:01:31 -0700847 return 0;
848}
849
850static int storvsc_device_configure(struct scsi_device *sdevice)
851{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700852 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice,
853 sdevice->queue_depth);
Hank Janssenbef4a342009-07-13 16:01:31 -0700854
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700855 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting queue depth to %d",
856 sdevice, STORVSC_MAX_IO_REQUESTS);
857 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
858 STORVSC_MAX_IO_REQUESTS);
Hank Janssenbef4a342009-07-13 16:01:31 -0700859
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700860 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
861 sdevice, PAGE_SIZE);
Hank Janssenbef4a342009-07-13 16:01:31 -0700862 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
863
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700864 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
865 sdevice);
Hank Janssenbef4a342009-07-13 16:01:31 -0700866 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
867
868 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
Bill Pemberton454f18a2009-07-27 16:47:24 -0400869 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700870
871 return 0;
872}
873
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700874/**
875 * storvsc_host_reset_handler - Reset the scsi HBA
876 */
Hank Janssenbef4a342009-07-13 16:01:31 -0700877static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
878{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700879 int ret;
880 struct host_device_context *host_device_ctx =
881 (struct host_device_context *)scmnd->device->host->hostdata;
Hank Janssenbef4a342009-07-13 16:01:31 -0700882 struct device_context *device_ctx = host_device_ctx->device_ctx;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700883 struct driver_context *driver_ctx =
884 driver_to_driver_context(device_ctx->device.driver);
885 struct storvsc_driver_context *storvsc_drv_ctx =
886 (struct storvsc_driver_context *)driver_ctx;
Hank Janssenbef4a342009-07-13 16:01:31 -0700887
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700888 struct storvsc_driver_object *storvsc_drv_obj =
889 &storvsc_drv_ctx->drv_obj;
Hank Janssenbef4a342009-07-13 16:01:31 -0700890
891 DPRINT_ENTER(STORVSC_DRV);
892
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700893 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
894 scmnd->device, &device_ctx->device_obj);
Hank Janssenbef4a342009-07-13 16:01:31 -0700895
Bill Pemberton454f18a2009-07-27 16:47:24 -0400896 /* Invokes the vsc to reset the host/bus */
Hank Janssenbef4a342009-07-13 16:01:31 -0700897 ASSERT(storvsc_drv_obj->OnHostReset);
898 ret = storvsc_drv_obj->OnHostReset(&device_ctx->device_obj);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700899 if (ret != 0) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700900 DPRINT_EXIT(STORVSC_DRV);
901 return ret;
902 }
903
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700904 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
905 scmnd->device, &device_ctx->device_obj);
Hank Janssenbef4a342009-07-13 16:01:31 -0700906
907 DPRINT_EXIT(STORVSC_DRV);
908
909 return ret;
910}
911
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700912/**
913 * storvsc_host_rescan - Rescan the scsi HBA
914 */
Hank Janssenbef4a342009-07-13 16:01:31 -0700915static void storvsc_host_rescan_callback(struct work_struct *work)
916{
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200917 struct hv_device *device_obj =
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700918 &((struct host_device_context *)work)->device_ctx->device_obj;
919 struct device_context *device_ctx = to_device_context(device_obj);
Greg Kroah-Hartman0883c522009-07-24 10:58:22 -0700920 struct Scsi_Host *host = dev_get_drvdata(&device_ctx->device);
Hank Janssenbef4a342009-07-13 16:01:31 -0700921 struct scsi_device *sdev;
922 struct host_device_context *host_device_ctx;
923 struct scsi_device **sdevs_remove_list;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700924 unsigned int sdevs_count = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700925 unsigned int found;
926 unsigned int i;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700927 unsigned int lun_count = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700928 unsigned int *lun_list;
929
930 DPRINT_ENTER(STORVSC_DRV);
931
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700932 host_device_ctx = (struct host_device_context *)host->hostdata;
933 lun_list = kcalloc(STORVSC_MAX_LUNS_PER_TARGET, sizeof(unsigned int),
934 GFP_ATOMIC);
935 if (!lun_list) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700936 DPRINT_ERR(STORVSC_DRV, "unable to allocate lun list");
937 return;
938 }
939
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700940 sdevs_remove_list = kcalloc(STORVSC_MAX_LUNS_PER_TARGET,
941 sizeof(void *), GFP_ATOMIC);
942 if (!sdevs_remove_list) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700943 kfree(lun_list);
944 DPRINT_ERR(STORVSC_DRV, "unable to allocate lun remove list");
945 return;
946 }
947
Bill Pemberton7f6f27d2009-07-27 16:47:33 -0400948 DPRINT_INFO(STORVSC_DRV, "rescanning host for new scsi devices...");
Hank Janssenbef4a342009-07-13 16:01:31 -0700949
Bill Pemberton454f18a2009-07-27 16:47:24 -0400950 /* Rescan for new device */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700951 scsi_scan_target(&host->shost_gendev, host_device_ctx->path,
952 host_device_ctx->target, SCAN_WILD_CARD, 1);
Hank Janssenbef4a342009-07-13 16:01:31 -0700953
954 DPRINT_INFO(STORVSC_DRV, "rescanning host for removed scsi device...");
955
Bill Pemberton454f18a2009-07-27 16:47:24 -0400956 /* Use the 1st device to send the report luns cmd */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700957 shost_for_each_device(sdev, host) {
958 lun_count = STORVSC_MAX_LUNS_PER_TARGET;
Hank Janssenbef4a342009-07-13 16:01:31 -0700959 storvsc_report_luns(sdev, lun_list, &lun_count);
960
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700961 DPRINT_INFO(STORVSC_DRV,
962 "report luns on scsi device (%p) found %u luns ",
963 sdev, lun_count);
964 DPRINT_INFO(STORVSC_DRV,
965 "existing luns on scsi device (%p) host (%d)",
966 sdev, host->host_no);
Hank Janssenbef4a342009-07-13 16:01:31 -0700967
968 scsi_device_put(sdev);
969 break;
970 }
971
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700972 for (i = 0; i < lun_count; i++)
Hank Janssenbef4a342009-07-13 16:01:31 -0700973 DPRINT_INFO(STORVSC_DRV, "%d) lun %u", i, lun_list[i]);
Hank Janssenbef4a342009-07-13 16:01:31 -0700974
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700975 /* Rescan for devices that may have been removed.
976 * We do not have to worry that new devices may have been added since
977 * this callback is serialized by the workqueue ie add/remove are done
978 * here.
979 */
980 shost_for_each_device(sdev, host) {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400981 /* See if this device is still here */
Hank Janssenbef4a342009-07-13 16:01:31 -0700982 found = 0;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700983 for (i = 0; i < lun_count; i++) {
984 if (sdev->lun == lun_list[i]) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700985 found = 1;
986 break;
987 }
988 }
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700989 if (!found) {
990 DPRINT_INFO(STORVSC_DRV, "lun (%u) does not exists",
991 sdev->lun);
Hank Janssenbef4a342009-07-13 16:01:31 -0700992 sdevs_remove_list[sdevs_count++] = sdev;
993 }
994 }
995
Bill Pemberton454f18a2009-07-27 16:47:24 -0400996 /* Now remove the devices */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700997 for (i = 0; i < sdevs_count; i++) {
998 DPRINT_INFO(STORVSC_DRV,
999 "removing scsi device (%p) lun (%u)...",
1000 sdevs_remove_list[i], sdevs_remove_list[i]->lun);
Hank Janssenbef4a342009-07-13 16:01:31 -07001001
Bill Pemberton454f18a2009-07-27 16:47:24 -04001002 /* make sure it is not removed from underneath us */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001003 if (!scsi_device_get(sdevs_remove_list[i])) {
Hank Janssenbef4a342009-07-13 16:01:31 -07001004 scsi_remove_device(sdevs_remove_list[i]);
1005 scsi_device_put(sdevs_remove_list[i]);
1006 }
1007 }
1008
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001009 DPRINT_INFO(STORVSC_DRV, "rescan completed on dev obj (%p) "
1010 "target (%u) bus (%u)", device_obj,
1011 host_device_ctx->target, host_device_ctx->path);
Hank Janssenbef4a342009-07-13 16:01:31 -07001012
1013 kfree(lun_list);
1014 kfree(sdevs_remove_list);
1015
1016 DPRINT_EXIT(STORVSC_DRV);
1017}
1018
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001019static int storvsc_report_luns(struct scsi_device *sdev, unsigned int luns[],
1020 unsigned int *lun_count)
Hank Janssenbef4a342009-07-13 16:01:31 -07001021{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001022 int i, j;
1023 unsigned int lun = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -07001024 unsigned int num_luns;
1025 int result;
1026 unsigned char *data;
Hank Janssenbef4a342009-07-13 16:01:31 -07001027 struct scsi_sense_hdr sshdr;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001028 unsigned char cmd[16] = {0};
1029 /* Add 1 to cover the report_lun header */
1030 unsigned int report_len = 8 * (STORVSC_MAX_LUNS_PER_TARGET+1);
Hank Janssenbef4a342009-07-13 16:01:31 -07001031 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)
Hank Janssenbef4a342009-07-13 16:01:31 -07001038 return -ENOMEM;
Hank Janssenbef4a342009-07-13 16:01:31 -07001039
1040 cmd[0] = REPORT_LUNS;
1041
Bill Pemberton454f18a2009-07-27 16:47:24 -04001042 /* cmd length */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001043 *(unsigned int *)&cmd[6] = cpu_to_be32(report_len);
Hank Janssenbef4a342009-07-13 16:01:31 -07001044
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001045 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE,
1046 (unsigned char *)report_luns, report_len,
1047 &sshdr, 30 * HZ, 3, NULL);
1048 if (result != 0) {
Hank Janssenbef4a342009-07-13 16:01:31 -07001049 kfree(report_luns);
1050 return -EBUSY;
1051 }
1052
Bill Pemberton454f18a2009-07-27 16:47:24 -04001053 /* get the length from the first four bytes */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001054 report_len = be32_to_cpu(*(unsigned int *)&report_luns[0]);
Hank Janssenbef4a342009-07-13 16:01:31 -07001055
1056 num_luns = (report_len / sizeof(unsigned long long));
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001057 if (num_luns > in_lun_count) {
Hank Janssenbef4a342009-07-13 16:01:31 -07001058 kfree(report_luns);
1059 return -EINVAL;
1060 }
1061
1062 *lun_count = num_luns;
1063
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001064 DPRINT_DBG(STORVSC_DRV,
1065 "report luns on scsi device (%p) found %u luns ",
1066 sdev, num_luns);
Hank Janssenbef4a342009-07-13 16:01:31 -07001067
Bill Pemberton454f18a2009-07-27 16:47:24 -04001068 /* lun id starts at 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001069 for (i = 1; i < num_luns + 1; i++) {
Hank Janssenbef4a342009-07-13 16:01:31 -07001070 lun = 0;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001071 data = (unsigned char *)&report_luns[i];
1072 for (j = 0; j < sizeof(lun); j += 2) {
1073 lun = lun | (((data[j] << 8) | data[j + 1]) <<
1074 (j * 8));
1075 }
Hank Janssenbef4a342009-07-13 16:01:31 -07001076
1077 luns[i-1] = lun;
1078 }
1079
1080 kfree(report_luns);
1081 return 0;
1082}
Hank Janssenbef4a342009-07-13 16:01:31 -07001083
Nicolas Palix3d3b5512009-07-28 17:32:53 +02001084static void storvsc_host_rescan(struct hv_device *device_obj)
Hank Janssenbef4a342009-07-13 16:01:31 -07001085{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001086 struct device_context *device_ctx = to_device_context(device_obj);
Greg Kroah-Hartman0883c522009-07-24 10:58:22 -07001087 struct Scsi_Host *host = dev_get_drvdata(&device_ctx->device);
Hank Janssenbef4a342009-07-13 16:01:31 -07001088 struct host_device_context *host_device_ctx;
1089
1090 DPRINT_ENTER(STORVSC_DRV);
Hank Janssenbef4a342009-07-13 16:01:31 -07001091
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001092 host_device_ctx = (struct host_device_context *)host->hostdata;
Hank Janssenbef4a342009-07-13 16:01:31 -07001093
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001094 DPRINT_INFO(STORVSC_DRV, "initiating rescan on dev obj (%p) "
1095 "target (%u) bus (%u)...", device_obj,
1096 host_device_ctx->target, host_device_ctx->path);
Hank Janssenbef4a342009-07-13 16:01:31 -07001097
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001098 /*
1099 * We need to queue this since the scanning may block and the caller
1100 * may be in an intr context
1101 */
Bill Pemberton454f18a2009-07-27 16:47:24 -04001102 /* scsi_queue_work(host, &host_device_ctx->host_rescan_work); */
Hank Janssenbef4a342009-07-13 16:01:31 -07001103 schedule_work(&host_device_ctx->host_rescan_work);
Hank Janssenbef4a342009-07-13 16:01:31 -07001104 DPRINT_EXIT(STORVSC_DRV);
1105}
1106
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001107static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1108 sector_t capacity, int *info)
Hank Janssenbef4a342009-07-13 16:01:31 -07001109{
1110 sector_t total_sectors = capacity;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001111 sector_t cylinder_times_heads = 0;
1112 sector_t temp = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -07001113
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001114 int sectors_per_track = 0;
1115 int heads = 0;
1116 int cylinders = 0;
1117 int rem = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -07001118
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001119 if (total_sectors > (65535 * 16 * 255))
1120 total_sectors = (65535 * 16 * 255);
Hank Janssenbef4a342009-07-13 16:01:31 -07001121
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001122 if (total_sectors >= (65535 * 16 * 63)) {
1123 sectors_per_track = 255;
1124 heads = 16;
Hank Janssenbef4a342009-07-13 16:01:31 -07001125
1126 cylinder_times_heads = total_sectors;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001127 /* sector_div stores the quotient in cylinder_times_heads */
1128 rem = sector_div(cylinder_times_heads, sectors_per_track);
1129 } else {
1130 sectors_per_track = 17;
Hank Janssenbef4a342009-07-13 16:01:31 -07001131
1132 cylinder_times_heads = total_sectors;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001133 /* sector_div stores the quotient in cylinder_times_heads */
1134 rem = sector_div(cylinder_times_heads, sectors_per_track);
Hank Janssenbef4a342009-07-13 16:01:31 -07001135
1136 temp = cylinder_times_heads + 1023;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001137 /* sector_div stores the quotient in temp */
1138 rem = sector_div(temp, 1024);
Hank Janssenbef4a342009-07-13 16:01:31 -07001139
1140 heads = temp;
1141
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001142 if (heads < 4)
1143 heads = 4;
Hank Janssenbef4a342009-07-13 16:01:31 -07001144
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001145 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
1146 sectors_per_track = 31;
1147 heads = 16;
Hank Janssenbef4a342009-07-13 16:01:31 -07001148
1149 cylinder_times_heads = total_sectors;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001150 /*
1151 * sector_div stores the quotient in
1152 * cylinder_times_heads
1153 */
1154 rem = sector_div(cylinder_times_heads,
1155 sectors_per_track);
1156 }
Hank Janssenbef4a342009-07-13 16:01:31 -07001157
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001158 if (cylinder_times_heads >= (heads * 1024)) {
1159 sectors_per_track = 63;
1160 heads = 16;
Hank Janssenbef4a342009-07-13 16:01:31 -07001161
1162 cylinder_times_heads = total_sectors;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001163 /*
1164 * sector_div stores the quotient in
1165 * cylinder_times_heads
1166 */
1167 rem = sector_div(cylinder_times_heads,
1168 sectors_per_track);
1169 }
1170 }
Hank Janssenbef4a342009-07-13 16:01:31 -07001171
1172 temp = cylinder_times_heads;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001173 /* sector_div stores the quotient in temp */
1174 rem = sector_div(temp, heads);
Hank Janssenbef4a342009-07-13 16:01:31 -07001175 cylinders = temp;
1176
1177 info[0] = heads;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001178 info[1] = sectors_per_track;
1179 info[2] = cylinders;
Hank Janssenbef4a342009-07-13 16:01:31 -07001180
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001181 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
1182 sectors_per_track);
Hank Janssenbef4a342009-07-13 16:01:31 -07001183
1184 return 0;
1185}
1186
Hank Janssenbef4a342009-07-13 16:01:31 -07001187static int __init storvsc_init(void)
1188{
1189 int ret;
1190
1191 DPRINT_ENTER(STORVSC_DRV);
Hank Janssenbef4a342009-07-13 16:01:31 -07001192 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
Hank Janssenbef4a342009-07-13 16:01:31 -07001193 ret = storvsc_drv_init(StorVscInitialize);
Hank Janssenbef4a342009-07-13 16:01:31 -07001194 DPRINT_EXIT(STORVSC_DRV);
Hank Janssenbef4a342009-07-13 16:01:31 -07001195 return ret;
1196}
1197
1198static void __exit storvsc_exit(void)
1199{
1200 DPRINT_ENTER(STORVSC_DRV);
Hank Janssenbef4a342009-07-13 16:01:31 -07001201 storvsc_drv_exit();
Hank Janssenbef4a342009-07-13 16:01:31 -07001202 DPRINT_ENTER(STORVSC_DRV);
1203}
1204
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001205MODULE_LICENSE("GPL");
Hank Janssenbef4a342009-07-13 16:01:31 -07001206module_param(storvsc_ringbuffer_size, int, S_IRUGO);
Hank Janssenbef4a342009-07-13 16:01:31 -07001207module_init(storvsc_init);
1208module_exit(storvsc_exit);