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