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