Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[GitHub/mt8127/android_kernel_alcatel_ttab.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
HJ
143
144 DPRINT_ENTER(STORVSC_DRV);
145
146 vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
147
148 storvsc_drv_obj->RingBufferSize = storvsc_ringbuffer_size;
bef4a34a 149
454f18a9 150 /* Callback to client driver to complete the initialization */
21707bed 151 drv_init(&storvsc_drv_obj->Base);
bef4a34a 152
ff568d3a
GKH
153 DPRINT_INFO(STORVSC_DRV,
154 "request extension size %u, max outstanding reqs %u",
155 storvsc_drv_obj->RequestExtSize,
156 storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
157
158 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);
bef4a34a
HJ
165 return -1;
166 }
167
168 drv_ctx->driver.name = storvsc_drv_obj->Base.name;
ff568d3a
GKH
169 memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType,
170 sizeof(struct hv_guid));
bef4a34a 171
bef4a34a
HJ
172 drv_ctx->probe = storvsc_probe;
173 drv_ctx->remove = storvsc_remove;
bef4a34a 174
454f18a9 175 /* The driver belongs to vmbus */
5d48a1c2 176 ret = vmbus_child_driver_register(drv_ctx);
bef4a34a
HJ
177
178 DPRINT_EXIT(STORVSC_DRV);
179
180 return ret;
181}
182
bef4a34a
HJ
183static int storvsc_drv_exit_cb(struct device *dev, void *data)
184{
185 struct device **curr = (struct device **)data;
186 *curr = dev;
454f18a9 187 return 1; /* stop iterating */
bef4a34a
HJ
188}
189
bd1de709 190static void storvsc_drv_exit(void)
bef4a34a 191{
9f0c7d2c 192 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
ff568d3a
GKH
193 struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx;
194 struct device *current_dev = NULL;
2295ba2e 195 int ret;
bef4a34a 196
bef4a34a
HJ
197 DPRINT_ENTER(STORVSC_DRV);
198
ff568d3a 199 while (1) {
bef4a34a
HJ
200 current_dev = NULL;
201
454f18a9 202 /* Get the device */
2295ba2e
BP
203 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);
bef4a34a
HJ
210
211 if (current_dev == NULL)
212 break;
213
454f18a9 214 /* Initiate removal from the top-down */
bef4a34a
HJ
215 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
3e189519 228/*
ff568d3a
GKH
229 * storvsc_probe - Add a new device for this driver
230 */
bef4a34a
HJ
231static int storvsc_probe(struct device *device)
232{
ff568d3a
GKH
233 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;
f916a34d 240 struct vm_device *device_ctx = device_to_vm_device(device);
3d3b5518 241 struct hv_device *device_obj = &device_ctx->device_obj;
bef4a34a
HJ
242 struct Scsi_Host *host;
243 struct host_device_context *host_device_ctx;
9f0c7d2c 244 struct storvsc_device_info device_info;
bef4a34a
HJ
245
246 DPRINT_ENTER(STORVSC_DRV);
247
248 if (!storvsc_drv_obj->Base.OnDeviceAdd)
249 return -1;
250
ff568d3a
GKH
251 host = scsi_host_alloc(&scsi_driver,
252 sizeof(struct host_device_context));
253 if (!host) {
bef4a34a
HJ
254 DPRINT_ERR(STORVSC_DRV, "unable to allocate scsi host object");
255 return -ENOMEM;
256 }
257
0883c52b 258 dev_set_drvdata(device, host);
bef4a34a 259
ff568d3a 260 host_device_ctx = (struct host_device_context *)host->hostdata;
bef4a34a
HJ
261 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
bef4a34a 266 host_device_ctx->request_pool =
ff568d3a
GKH
267 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);
271
272 if (!host_device_ctx->request_pool) {
bef4a34a
HJ
273 scsi_host_put(host);
274 DPRINT_EXIT(STORVSC_DRV);
275
276 return -ENOMEM;
277 }
278
279 device_info.PortNumber = host->host_no;
454f18a9 280 /* Call to the vsc driver to add the device */
ff568d3a
GKH
281 ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj,
282 (void *)&device_info);
283 if (ret != 0) {
bef4a34a
HJ
284 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
454f18a9 292 /* host_device_ctx->port = device_info.PortNumber; */
bef4a34a
HJ
293 host_device_ctx->path = device_info.PathId;
294 host_device_ctx->target = device_info.TargetId;
295
ff568d3a
GKH
296 /* 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;
bef4a34a 302
454f18a9 303 /* Register the HBA and start the scsi bus scan */
bef4a34a 304 ret = scsi_add_host(host, device);
ff568d3a 305 if (ret != 0) {
bef4a34a
HJ
306 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
3e189519 324/*
ff568d3a
GKH
325 * storvsc_remove - Callback when our device is removed
326 */
bef4a34a
HJ
327static int storvsc_remove(struct device *device)
328{
ff568d3a
GKH
329 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;
f916a34d 336 struct vm_device *device_ctx = device_to_vm_device(device);
3d3b5518 337 struct hv_device *device_obj = &device_ctx->device_obj;
0883c52b 338 struct Scsi_Host *host = dev_get_drvdata(device);
ff568d3a
GKH
339 struct host_device_context *host_device_ctx =
340 (struct host_device_context *)host->hostdata;
bef4a34a
HJ
341
342
343 DPRINT_ENTER(STORVSC_DRV);
344
ff568d3a 345 if (!storvsc_drv_obj->Base.OnDeviceRemove) {
bef4a34a
HJ
346 DPRINT_EXIT(STORVSC_DRV);
347 return -1;
348 }
349
ff568d3a
GKH
350 /*
351 * Call to the vsc driver to let it know that the device is being
352 * removed
353 */
bef4a34a 354 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
ff568d3a 355 if (ret != 0) {
454f18a9 356 /* TODO: */
ff568d3a
GKH
357 DPRINT_ERR(STORVSC, "unable to remove vsc device (ret %d)",
358 ret);
bef4a34a
HJ
359 }
360
ff568d3a 361 if (host_device_ctx->request_pool) {
bef4a34a
HJ
362 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
3e189519 377/*
ff568d3a
GKH
378 * storvsc_commmand_completion - Command completion processing
379 */
0b3f6834 380static void storvsc_commmand_completion(struct hv_storvsc_request *request)
bef4a34a 381{
ff568d3a
GKH
382 struct storvsc_cmd_request *cmd_request =
383 (struct storvsc_cmd_request *)request->Context;
bef4a34a 384 struct scsi_cmnd *scmnd = cmd_request->cmd;
ff568d3a
GKH
385 struct host_device_context *host_device_ctx =
386 (struct host_device_context *)scmnd->device->host->hostdata;
bef4a34a 387 void (*scsi_done_fn)(struct scsi_cmnd *);
bef4a34a 388 struct scsi_sense_hdr sense_hdr;
bef4a34a 389
b856e738
BP
390 /* 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); */
bef4a34a
HJ
395
396 DPRINT_ENTER(STORVSC_DRV);
397
ff568d3a
GKH
398 if (cmd_request->bounce_sgl_count) {
399 /* using bounce buffer */
454f18a9 400 /* printk("copy_from_bounce_buffer\n"); */
bef4a34a 401
454f18a9 402 /* FIXME: We can optimize on writes by just skipping this */
ff568d3a
GKH
403 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);
bef4a34a
HJ
408 }
409
410 scmnd->result = request->Status;
411
ff568d3a
GKH
412 if (scmnd->result) {
413 if (scsi_normalize_sense(scmnd->sense_buffer,
414 request->SenseBufferSize, &sense_hdr))
bef4a34a 415 scsi_print_sense_hdr("storvsc", &sense_hdr);
bef4a34a
HJ
416 }
417
b856e738 418 /* ASSERT(request->BytesXfer <= request->DataBuffer.Length); */
bef4a34a 419 scsi_set_resid(scmnd, request->DataBuffer.Length - request->BytesXfer);
bef4a34a
HJ
420
421 scsi_done_fn = scmnd->scsi_done;
422
423 scmnd->host_scribble = NULL;
424 scmnd->scsi_done = NULL;
425
454f18a9 426 /* !!DO NOT MODIFY the scmnd after this call */
bef4a34a
HJ
427 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{
ff568d3a 436 int i;
bef4a34a 437
454f18a9 438 /* No need to check */
bef4a34a
HJ
439 if (sg_count < 2)
440 return -1;
441
454f18a9 442 /* We have at least 2 sg entries */
ff568d3a
GKH
443 for (i = 0; i < sg_count; i++) {
444 if (i == 0) {
445 /* make sure 1st one does not have hole */
bef4a34a
HJ
446 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
447 return i;
ff568d3a
GKH
448 } else if (i == sg_count - 1) {
449 /* make sure last one does not have hole */
bef4a34a
HJ
450 if (sgl[i].offset != 0)
451 return i;
ff568d3a
GKH
452 } else {
453 /* make sure no hole in the middle */
bef4a34a 454 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
bef4a34a 455 return i;
bef4a34a
HJ
456 }
457 }
458 return -1;
459}
460
ff568d3a
GKH
461static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
462 unsigned int sg_count,
463 unsigned int len)
bef4a34a
HJ
464{
465 int i;
ff568d3a
GKH
466 int num_pages;
467 struct scatterlist *bounce_sgl;
bef4a34a
HJ
468 struct page *page_buf;
469
470 num_pages = ALIGN_UP(len, PAGE_SIZE) >> PAGE_SHIFT;
471
06da0bc8 472 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
bef4a34a 473 if (!bounce_sgl)
bef4a34a 474 return NULL;
bef4a34a 475
ff568d3a 476 for (i = 0; i < num_pages; i++) {
bef4a34a
HJ
477 page_buf = alloc_page(GFP_ATOMIC);
478 if (!page_buf)
bef4a34a 479 goto cleanup;
bef4a34a 480 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
bef4a34a
HJ
481 }
482
483 return bounce_sgl;
484
485cleanup:
486 destroy_bounce_buffer(bounce_sgl, num_pages);
487 return NULL;
488}
489
ff568d3a
GKH
490static void destroy_bounce_buffer(struct scatterlist *sgl,
491 unsigned int sg_count)
bef4a34a
HJ
492{
493 int i;
494 struct page *page_buf;
495
ff568d3a
GKH
496 for (i = 0; i < sg_count; i++) {
497 page_buf = sg_page((&sgl[i]));
498 if (page_buf != NULL)
bef4a34a 499 __free_page(page_buf);
bef4a34a
HJ
500 }
501
502 kfree(sgl);
503}
504
454f18a9 505/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
ff568d3a
GKH
506static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
507 struct scatterlist *bounce_sgl,
508 unsigned int orig_sgl_count)
bef4a34a 509{
ff568d3a
GKH
510 int i;
511 int j = 0;
bef4a34a
HJ
512 unsigned long src, dest;
513 unsigned int srclen, destlen, copylen;
ff568d3a
GKH
514 unsigned int total_copied = 0;
515 unsigned long bounce_addr = 0;
516 unsigned long src_addr = 0;
bef4a34a
HJ
517 unsigned long flags;
518
519 local_irq_save(flags);
520
ff568d3a
GKH
521 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;
bef4a34a
HJ
524 src = src_addr;
525 srclen = orig_sgl[i].length;
526
b856e738 527 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
bef4a34a
HJ
528
529 if (j == 0)
bef4a34a 530 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
bef4a34a 531
ff568d3a 532 while (srclen) {
454f18a9 533 /* assume bounce offset always == 0 */
bef4a34a
HJ
534 dest = bounce_addr + bounce_sgl[j].length;
535 destlen = PAGE_SIZE - bounce_sgl[j].length;
536
fc6a4b26 537 copylen = min(srclen, destlen);
ff568d3a 538 memcpy((void *)dest, (void *)src, copylen);
bef4a34a
HJ
539
540 total_copied += copylen;
541 bounce_sgl[j].length += copylen;
542 srclen -= copylen;
543 src += copylen;
544
ff568d3a
GKH
545 if (bounce_sgl[j].length == PAGE_SIZE) {
546 /* full..move to next entry */
547 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
bef4a34a
HJ
548 j++;
549
454f18a9 550 /* if we need to use another bounce buffer */
ff568d3a 551 if (srclen || i != orig_sgl_count - 1)
bef4a34a 552 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
ff568d3a
GKH
553 } 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);
bef4a34a
HJ
556 }
557 }
558
ff568d3a 559 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
bef4a34a
HJ
560 }
561
562 local_irq_restore(flags);
563
564 return total_copied;
565}
566
454f18a9 567/* Assume the original sgl has enough room */
ff568d3a
GKH
568static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
569 struct scatterlist *bounce_sgl,
570 unsigned int orig_sgl_count)
bef4a34a 571{
ff568d3a
GKH
572 int i;
573 int j = 0;
bef4a34a
HJ
574 unsigned long src, dest;
575 unsigned int srclen, destlen, copylen;
ff568d3a
GKH
576 unsigned int total_copied = 0;
577 unsigned long bounce_addr = 0;
578 unsigned long dest_addr = 0;
bef4a34a
HJ
579 unsigned long flags;
580
581 local_irq_save(flags);
582
ff568d3a
GKH
583 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;
bef4a34a
HJ
586 dest = dest_addr;
587 destlen = orig_sgl[i].length;
b856e738 588 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
bef4a34a
HJ
589
590 if (j == 0)
bef4a34a 591 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
bef4a34a 592
ff568d3a 593 while (destlen) {
bef4a34a
HJ
594 src = bounce_addr + bounce_sgl[j].offset;
595 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
596
fc6a4b26 597 copylen = min(srclen, destlen);
ff568d3a 598 memcpy((void *)dest, (void *)src, copylen);
bef4a34a
HJ
599
600 total_copied += copylen;
601 bounce_sgl[j].offset += copylen;
602 destlen -= copylen;
603 dest += copylen;
604
ff568d3a
GKH
605 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
606 /* full */
607 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
bef4a34a
HJ
608 j++;
609
454f18a9 610 /* if we need to use another bounce buffer */
ff568d3a 611 if (destlen || i != orig_sgl_count - 1)
bef4a34a 612 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
ff568d3a
GKH
613 } 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);
bef4a34a
HJ
616 }
617 }
618
ff568d3a
GKH
619 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
620 KM_IRQ0);
bef4a34a
HJ
621 }
622
623 local_irq_restore(flags);
624
625 return total_copied;
626}
627
3e189519 628/*
ff568d3a
GKH
629 * storvsc_queuecommand - Initiate command processing
630 */
631static int storvsc_queuecommand(struct scsi_cmnd *scmnd,
632 void (*done)(struct scsi_cmnd *))
bef4a34a 633{
ff568d3a
GKH
634 int ret;
635 struct host_device_context *host_device_ctx =
636 (struct host_device_context *)scmnd->device->host->hostdata;
f916a34d 637 struct vm_device *device_ctx = host_device_ctx->device_ctx;
ff568d3a
GKH
638 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;
0b3f6834 644 struct hv_storvsc_request *request;
bef4a34a 645 struct storvsc_cmd_request *cmd_request;
ff568d3a 646 unsigned int request_size = 0;
bef4a34a
HJ
647 int i;
648 struct scatterlist *sgl;
649
650 DPRINT_ENTER(STORVSC_DRV);
651
ff568d3a
GKH
652 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);
bef4a34a 657
454f18a9 658 /* If retrying, no need to prep the cmd */
ff568d3a 659 if (scmnd->host_scribble) {
b856e738 660 /* ASSERT(scmnd->scsi_done != NULL); */
bef4a34a 661
ff568d3a
GKH
662 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);
bef4a34a
HJ
666
667 goto retry_request;
668 }
669
b856e738
BP
670 /* ASSERT(scmnd->scsi_done == NULL); */
671 /* ASSERT(scmnd->host_scribble == NULL); */
bef4a34a
HJ
672
673 scmnd->scsi_done = done;
674
675 request_size = sizeof(struct storvsc_cmd_request);
676
ff568d3a
GKH
677 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);
bef4a34a
HJ
682 scmnd->scsi_done = NULL;
683 return SCSI_MLQUEUE_DEVICE_BUSY;
684 }
685
454f18a9 686 /* Setup the cmd request */
bef4a34a
HJ
687 cmd_request->bounce_sgl_count = 0;
688 cmd_request->bounce_sgl = NULL;
689 cmd_request->cmd = scmnd;
690
ff568d3a 691 scmnd->host_scribble = (unsigned char *)cmd_request;
bef4a34a
HJ
692
693 request = &cmd_request->request;
694
ff568d3a
GKH
695 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);
bef4a34a 699
454f18a9 700 /* Build the SRB */
ff568d3a 701 switch (scmnd->sc_data_direction) {
bef4a34a
HJ
702 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;
454f18a9 714 request->Context = cmd_request;/* scmnd; */
bef4a34a 715
454f18a9 716 /* request->PortId = scmnd->device->channel; */
bef4a34a
HJ
717 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
b856e738 722 /* ASSERT(scmnd->cmd_len <= 16); */
bef4a34a
HJ
723 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
bef4a34a 730 request->DataBuffer.Length = scsi_bufflen(scmnd);
ff568d3a
GKH
731 if (scsi_sg_count(scmnd)) {
732 sgl = (struct scatterlist *)scsi_sglist(scmnd);
bef4a34a 733
454f18a9 734 /* check if we need to bounce the sgl */
ff568d3a
GKH
735 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);
bef4a34a
HJ
746
747 scmnd->scsi_done = NULL;
748 scmnd->host_scribble = NULL;
ff568d3a
GKH
749 kmem_cache_free(host_device_ctx->request_pool,
750 cmd_request);
bef4a34a
HJ
751
752 return SCSI_MLQUEUE_HOST_BUSY;
753 }
754
ff568d3a
GKH
755 cmd_request->bounce_sgl_count =
756 ALIGN_UP(scsi_bufflen(scmnd), PAGE_SIZE) >>
757 PAGE_SHIFT;
bef4a34a 758
ff568d3a
GKH
759 /*
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));
bef4a34a
HJ
765
766 sgl = cmd_request->bounce_sgl;
767 }
768
769 request->DataBuffer.Offset = sgl[0].offset;
770
ff568d3a 771 for (i = 0; i < scsi_sg_count(scmnd); i++) {
0686e4f4 772 DPRINT_DBG(STORVSC_DRV, "sgl[%d] len %d offset %d\n",
ff568d3a
GKH
773 i, sgl[i].length, sgl[i].offset);
774 request->DataBuffer.PfnArray[i] =
775 page_to_pfn(sg_page((&sgl[i])));
bef4a34a 776 }
ff568d3a 777 } else if (scsi_sglist(scmnd)) {
b856e738 778 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
ff568d3a
GKH
779 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;
bef4a34a 783 }
bef4a34a
HJ
784
785retry_request:
454f18a9 786 /* Invokes the vsc to start an IO */
ff568d3a
GKH
787 ret = storvsc_drv_obj->OnIORequest(&device_ctx->device_obj,
788 &cmd_request->request);
789 if (ret == -1) {
790 /* no more space */
791 DPRINT_ERR(STORVSC_DRV,
792 "scmnd (%p) - queue FULL...marking queue busy",
793 scmnd);
794
795 if (cmd_request->bounce_sgl_count) {
796 /*
797 * FIXME: We can optimize on writes by just skipping
798 * this
799 */
800 copy_from_bounce_buffer(scsi_sglist(scmnd),
801 cmd_request->bounce_sgl,
802 scsi_sg_count(scmnd));
803 destroy_bounce_buffer(cmd_request->bounce_sgl,
804 cmd_request->bounce_sgl_count);
bef4a34a
HJ
805 }
806
807 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
808
809 scmnd->scsi_done = NULL;
810 scmnd->host_scribble = NULL;
811
812 ret = SCSI_MLQUEUE_DEVICE_BUSY;
813 }
814
815 DPRINT_EXIT(STORVSC_DRV);
816
817 return ret;
818}
819
ff568d3a
GKH
820static int storvsc_merge_bvec(struct request_queue *q,
821 struct bvec_merge_data *bmd, struct bio_vec *bvec)
bef4a34a 822{
ff568d3a
GKH
823 /* checking done by caller. */
824 return bvec->bv_len;
bef4a34a 825}
bef4a34a 826
3e189519 827/*
ff568d3a
GKH
828 * storvsc_device_configure - Configure the specified scsi device
829 */
bef4a34a
HJ
830static int storvsc_device_alloc(struct scsi_device *sdevice)
831{
ff568d3a
GKH
832 DPRINT_DBG(STORVSC_DRV, "sdev (%p) - setting device flag to %d",
833 sdevice, BLIST_SPARSELUN);
834 /*
835 * This enables luns to be located sparsely. Otherwise, we may not
836 * discovered them.
837 */
bef4a34a 838 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
bef4a34a
HJ
839 return 0;
840}
841
842static int storvsc_device_configure(struct scsi_device *sdevice)
843{
ff568d3a
GKH
844 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice,
845 sdevice->queue_depth);
bef4a34a 846
ff568d3a
GKH
847 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting queue depth to %d",
848 sdevice, STORVSC_MAX_IO_REQUESTS);
849 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
850 STORVSC_MAX_IO_REQUESTS);
bef4a34a 851
ff568d3a
GKH
852 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
853 sdevice, PAGE_SIZE);
bef4a34a
HJ
854 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
855
ff568d3a
GKH
856 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
857 sdevice);
bef4a34a
HJ
858 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
859
860 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
454f18a9 861 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
bef4a34a
HJ
862
863 return 0;
864}
865
3e189519 866/*
ff568d3a
GKH
867 * storvsc_host_reset_handler - Reset the scsi HBA
868 */
bef4a34a
HJ
869static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
870{
ff568d3a
GKH
871 int ret;
872 struct host_device_context *host_device_ctx =
873 (struct host_device_context *)scmnd->device->host->hostdata;
f916a34d 874 struct vm_device *device_ctx = host_device_ctx->device_ctx;
bef4a34a
HJ
875
876 DPRINT_ENTER(STORVSC_DRV);
877
ff568d3a
GKH
878 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
879 scmnd->device, &device_ctx->device_obj);
bef4a34a 880
454f18a9 881 /* Invokes the vsc to reset the host/bus */
354b0a64 882 ret = StorVscOnHostReset(&device_ctx->device_obj);
ff568d3a 883 if (ret != 0) {
bef4a34a
HJ
884 DPRINT_EXIT(STORVSC_DRV);
885 return ret;
886 }
887
ff568d3a
GKH
888 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
889 scmnd->device, &device_ctx->device_obj);
bef4a34a
HJ
890
891 DPRINT_EXIT(STORVSC_DRV);
892
893 return ret;
894}
895
ff568d3a
GKH
896static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
897 sector_t capacity, int *info)
bef4a34a
HJ
898{
899 sector_t total_sectors = capacity;
ff568d3a
GKH
900 sector_t cylinder_times_heads = 0;
901 sector_t temp = 0;
bef4a34a 902
ff568d3a
GKH
903 int sectors_per_track = 0;
904 int heads = 0;
905 int cylinders = 0;
906 int rem = 0;
bef4a34a 907
ff568d3a
GKH
908 if (total_sectors > (65535 * 16 * 255))
909 total_sectors = (65535 * 16 * 255);
bef4a34a 910
ff568d3a
GKH
911 if (total_sectors >= (65535 * 16 * 63)) {
912 sectors_per_track = 255;
913 heads = 16;
bef4a34a
HJ
914
915 cylinder_times_heads = total_sectors;
ff568d3a
GKH
916 /* sector_div stores the quotient in cylinder_times_heads */
917 rem = sector_div(cylinder_times_heads, sectors_per_track);
918 } else {
919 sectors_per_track = 17;
bef4a34a
HJ
920
921 cylinder_times_heads = total_sectors;
ff568d3a
GKH
922 /* sector_div stores the quotient in cylinder_times_heads */
923 rem = sector_div(cylinder_times_heads, sectors_per_track);
bef4a34a
HJ
924
925 temp = cylinder_times_heads + 1023;
ff568d3a
GKH
926 /* sector_div stores the quotient in temp */
927 rem = sector_div(temp, 1024);
bef4a34a
HJ
928
929 heads = temp;
930
ff568d3a
GKH
931 if (heads < 4)
932 heads = 4;
bef4a34a 933
ff568d3a
GKH
934 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
935 sectors_per_track = 31;
936 heads = 16;
bef4a34a
HJ
937
938 cylinder_times_heads = total_sectors;
ff568d3a
GKH
939 /*
940 * sector_div stores the quotient in
941 * cylinder_times_heads
942 */
943 rem = sector_div(cylinder_times_heads,
944 sectors_per_track);
945 }
bef4a34a 946
ff568d3a
GKH
947 if (cylinder_times_heads >= (heads * 1024)) {
948 sectors_per_track = 63;
949 heads = 16;
bef4a34a
HJ
950
951 cylinder_times_heads = total_sectors;
ff568d3a
GKH
952 /*
953 * sector_div stores the quotient in
954 * cylinder_times_heads
955 */
956 rem = sector_div(cylinder_times_heads,
957 sectors_per_track);
958 }
959 }
bef4a34a
HJ
960
961 temp = cylinder_times_heads;
ff568d3a
GKH
962 /* sector_div stores the quotient in temp */
963 rem = sector_div(temp, heads);
bef4a34a
HJ
964 cylinders = temp;
965
966 info[0] = heads;
ff568d3a
GKH
967 info[1] = sectors_per_track;
968 info[2] = cylinders;
bef4a34a 969
ff568d3a
GKH
970 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
971 sectors_per_track);
bef4a34a
HJ
972
973 return 0;
974}
975
bef4a34a
HJ
976static int __init storvsc_init(void)
977{
978 int ret;
979
980 DPRINT_ENTER(STORVSC_DRV);
bef4a34a 981 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
bef4a34a 982 ret = storvsc_drv_init(StorVscInitialize);
bef4a34a 983 DPRINT_EXIT(STORVSC_DRV);
bef4a34a
HJ
984 return ret;
985}
986
987static void __exit storvsc_exit(void)
988{
989 DPRINT_ENTER(STORVSC_DRV);
bef4a34a 990 storvsc_drv_exit();
bef4a34a
HJ
991 DPRINT_ENTER(STORVSC_DRV);
992}
993
ff568d3a 994MODULE_LICENSE("GPL");
26c14cc1 995MODULE_VERSION(HV_DRV_VERSION);
3afc7cc3 996MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
bef4a34a
HJ
997module_init(storvsc_init);
998module_exit(storvsc_exit);