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