2 * Copyright (c) 2009, Microsoft Corporation.
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.
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
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.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/device.h>
24 #include <linux/blkdev.h>
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>
32 #include <scsi/scsi_dbg.h>
35 #include "VersionInfo.h"
37 #include "StorVscApi.h"
40 struct host_device_context {
42 * FIXME this is a bug */
43 /* point back to our device context */
44 struct vm_device *device_ctx;
45 struct kmem_cache *request_pool;
51 struct storvsc_cmd_request {
52 struct list_head entry;
53 struct scsi_cmnd *cmd;
55 unsigned int bounce_sgl_count;
56 struct scatterlist *bounce_sgl;
58 struct hv_storvsc_request request;
59 /* !!!DO NOT ADD ANYTHING BELOW HERE!!! */
60 /* The extension buffer falls right here and is pointed to by
62 * Which sounds like a very bad design... */
65 struct storvsc_driver_context {
66 /* !! These must be the first 2 fields !! */
67 /* FIXME this is a bug... */
68 struct driver_context drv_ctx;
69 struct storvsc_driver_object drv_obj;
73 static int storvsc_probe(struct device *dev);
74 static int storvsc_queuecommand(struct scsi_cmnd *scmnd,
75 void (*done)(struct scsi_cmnd *));
76 static int storvsc_device_alloc(struct scsi_device *);
77 static int storvsc_device_configure(struct scsi_device *);
78 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd);
79 static int storvsc_remove(struct device *dev);
81 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
82 unsigned int sg_count,
84 static void destroy_bounce_buffer(struct scatterlist *sgl,
85 unsigned int sg_count);
86 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
87 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
88 struct scatterlist *bounce_sgl,
89 unsigned int orig_sgl_count);
90 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
91 struct scatterlist *bounce_sgl,
92 unsigned int orig_sgl_count);
94 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device *bdev,
95 sector_t capacity, int *info);
98 static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
100 /* The one and only one */
101 static struct storvsc_driver_context g_storvsc_drv;
104 static struct scsi_host_template scsi_driver = {
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,
113 /* 64 max_queue * 1 target */
114 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
116 /* no use setting to 0 since ll_blk_rw reset it to 1 */
118 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
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
125 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
126 .use_clustering = ENABLE_CLUSTERING,
127 /* Make sure we dont get a sg segment crosses a page boundary */
128 .dma_boundary = PAGE_SIZE-1,
133 * storvsc_drv_init - StorVsc driver initialization.
135 static int storvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
138 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
139 struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx;
141 DPRINT_ENTER(STORVSC_DRV);
143 vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
145 storvsc_drv_obj->RingBufferSize = storvsc_ringbuffer_size;
147 /* Callback to client driver to complete the initialization */
148 drv_init(&storvsc_drv_obj->Base);
150 DPRINT_INFO(STORVSC_DRV,
151 "request extension size %u, max outstanding reqs %u",
152 storvsc_drv_obj->RequestExtSize,
153 storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
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);
165 drv_ctx->driver.name = storvsc_drv_obj->Base.name;
166 memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType,
167 sizeof(struct hv_guid));
169 drv_ctx->probe = storvsc_probe;
170 drv_ctx->remove = storvsc_remove;
172 /* The driver belongs to vmbus */
173 ret = vmbus_child_driver_register(drv_ctx);
175 DPRINT_EXIT(STORVSC_DRV);
180 static int storvsc_drv_exit_cb(struct device *dev, void *data)
182 struct device **curr = (struct device **)data;
184 return 1; /* stop iterating */
187 static void storvsc_drv_exit(void)
189 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
190 struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx;
191 struct device *current_dev = NULL;
194 DPRINT_ENTER(STORVSC_DRV);
200 ret = driver_for_each_device(&drv_ctx->driver, NULL,
201 (void *) ¤t_dev,
202 storvsc_drv_exit_cb);
205 DPRINT_WARN(STORVSC_DRV,
206 "driver_for_each_device returned %d", ret);
208 if (current_dev == NULL)
211 /* Initiate removal from the top-down */
212 device_unregister(current_dev);
215 if (storvsc_drv_obj->Base.OnCleanup)
216 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
218 vmbus_child_driver_unregister(drv_ctx);
220 DPRINT_EXIT(STORVSC_DRV);
226 * storvsc_probe - Add a new device for this driver
228 static int storvsc_probe(struct device *device)
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;
237 struct vm_device *device_ctx = device_to_vm_device(device);
238 struct hv_device *device_obj = &device_ctx->device_obj;
239 struct Scsi_Host *host;
240 struct host_device_context *host_device_ctx;
241 struct storvsc_device_info device_info;
243 DPRINT_ENTER(STORVSC_DRV);
245 if (!storvsc_drv_obj->Base.OnDeviceAdd)
248 host = scsi_host_alloc(&scsi_driver,
249 sizeof(struct host_device_context));
251 DPRINT_ERR(STORVSC_DRV, "unable to allocate scsi host object");
255 dev_set_drvdata(device, host);
257 host_device_ctx = (struct host_device_context *)host->hostdata;
258 memset(host_device_ctx, 0, sizeof(struct host_device_context));
260 host_device_ctx->port = host->host_no;
261 host_device_ctx->device_ctx = device_ctx;
263 host_device_ctx->request_pool =
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);
269 if (!host_device_ctx->request_pool) {
271 DPRINT_EXIT(STORVSC_DRV);
276 device_info.PortNumber = host->host_no;
277 /* Call to the vsc driver to add the device */
278 ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj,
279 (void *)&device_info);
281 DPRINT_ERR(STORVSC_DRV, "unable to add scsi vsc device");
282 kmem_cache_destroy(host_device_ctx->request_pool);
284 DPRINT_EXIT(STORVSC_DRV);
289 /* host_device_ctx->port = device_info.PortNumber; */
290 host_device_ctx->path = device_info.PathId;
291 host_device_ctx->target = device_info.TargetId;
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;
300 /* Register the HBA and start the scsi bus scan */
301 ret = scsi_add_host(host, device);
303 DPRINT_ERR(STORVSC_DRV, "unable to add scsi host device");
305 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
307 kmem_cache_destroy(host_device_ctx->request_pool);
309 DPRINT_EXIT(STORVSC_DRV);
314 scsi_scan_host(host);
316 DPRINT_EXIT(STORVSC_DRV);
322 * storvsc_remove - Callback when our device is removed
324 static int storvsc_remove(struct device *device)
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;
333 struct vm_device *device_ctx = device_to_vm_device(device);
334 struct hv_device *device_obj = &device_ctx->device_obj;
335 struct Scsi_Host *host = dev_get_drvdata(device);
336 struct host_device_context *host_device_ctx =
337 (struct host_device_context *)host->hostdata;
340 DPRINT_ENTER(STORVSC_DRV);
342 if (!storvsc_drv_obj->Base.OnDeviceRemove) {
343 DPRINT_EXIT(STORVSC_DRV);
348 * Call to the vsc driver to let it know that the device is being
351 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
354 DPRINT_ERR(STORVSC, "unable to remove vsc device (ret %d)",
358 if (host_device_ctx->request_pool) {
359 kmem_cache_destroy(host_device_ctx->request_pool);
360 host_device_ctx->request_pool = NULL;
363 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
364 scsi_remove_host(host);
366 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
369 DPRINT_EXIT(STORVSC_DRV);
375 * storvsc_commmand_completion - Command completion processing
377 static void storvsc_commmand_completion(struct hv_storvsc_request *request)
379 struct storvsc_cmd_request *cmd_request =
380 (struct storvsc_cmd_request *)request->Context;
381 struct scsi_cmnd *scmnd = cmd_request->cmd;
382 struct host_device_context *host_device_ctx =
383 (struct host_device_context *)scmnd->device->host->hostdata;
384 void (*scsi_done_fn)(struct scsi_cmnd *);
385 struct scsi_sense_hdr sense_hdr;
387 ASSERT(request == &cmd_request->request);
388 ASSERT((unsigned long)scmnd->host_scribble ==
389 (unsigned long)cmd_request);
391 ASSERT(scmnd->scsi_done);
393 DPRINT_ENTER(STORVSC_DRV);
395 if (cmd_request->bounce_sgl_count) {
396 /* using bounce buffer */
397 /* printk("copy_from_bounce_buffer\n"); */
399 /* FIXME: We can optimize on writes by just skipping this */
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);
407 scmnd->result = request->Status;
410 if (scsi_normalize_sense(scmnd->sense_buffer,
411 request->SenseBufferSize, &sense_hdr))
412 scsi_print_sense_hdr("storvsc", &sense_hdr);
415 ASSERT(request->BytesXfer <= request->DataBuffer.Length);
416 scsi_set_resid(scmnd, request->DataBuffer.Length - request->BytesXfer);
418 scsi_done_fn = scmnd->scsi_done;
420 scmnd->host_scribble = NULL;
421 scmnd->scsi_done = NULL;
423 /* !!DO NOT MODIFY the scmnd after this call */
426 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
428 DPRINT_EXIT(STORVSC_DRV);
431 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
435 /* No need to check */
439 /* We have at least 2 sg entries */
440 for (i = 0; i < sg_count; i++) {
442 /* make sure 1st one does not have hole */
443 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
445 } else if (i == sg_count - 1) {
446 /* make sure last one does not have hole */
447 if (sgl[i].offset != 0)
450 /* make sure no hole in the middle */
451 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
458 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
459 unsigned int sg_count,
464 struct scatterlist *bounce_sgl;
465 struct page *page_buf;
467 num_pages = ALIGN_UP(len, PAGE_SIZE) >> PAGE_SHIFT;
469 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
473 for (i = 0; i < num_pages; i++) {
474 page_buf = alloc_page(GFP_ATOMIC);
477 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
483 destroy_bounce_buffer(bounce_sgl, num_pages);
487 static void destroy_bounce_buffer(struct scatterlist *sgl,
488 unsigned int sg_count)
491 struct page *page_buf;
493 for (i = 0; i < sg_count; i++) {
494 page_buf = sg_page((&sgl[i]));
495 if (page_buf != NULL)
496 __free_page(page_buf);
502 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
503 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
504 struct scatterlist *bounce_sgl,
505 unsigned int orig_sgl_count)
509 unsigned long src, dest;
510 unsigned int srclen, destlen, copylen;
511 unsigned int total_copied = 0;
512 unsigned long bounce_addr = 0;
513 unsigned long src_addr = 0;
516 local_irq_save(flags);
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;
522 srclen = orig_sgl[i].length;
524 ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
527 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
530 /* assume bounce offset always == 0 */
531 dest = bounce_addr + bounce_sgl[j].length;
532 destlen = PAGE_SIZE - bounce_sgl[j].length;
534 copylen = min(srclen, destlen);
535 memcpy((void *)dest, (void *)src, copylen);
537 total_copied += copylen;
538 bounce_sgl[j].length += copylen;
542 if (bounce_sgl[j].length == PAGE_SIZE) {
543 /* full..move to next entry */
544 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
547 /* if we need to use another bounce buffer */
548 if (srclen || i != orig_sgl_count - 1)
549 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
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);
556 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
559 local_irq_restore(flags);
564 /* Assume the original sgl has enough room */
565 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
566 struct scatterlist *bounce_sgl,
567 unsigned int orig_sgl_count)
571 unsigned long src, dest;
572 unsigned int srclen, destlen, copylen;
573 unsigned int total_copied = 0;
574 unsigned long bounce_addr = 0;
575 unsigned long dest_addr = 0;
578 local_irq_save(flags);
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;
584 destlen = orig_sgl[i].length;
585 ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
588 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
591 src = bounce_addr + bounce_sgl[j].offset;
592 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
594 copylen = min(srclen, destlen);
595 memcpy((void *)dest, (void *)src, copylen);
597 total_copied += copylen;
598 bounce_sgl[j].offset += copylen;
602 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
604 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
607 /* if we need to use another bounce buffer */
608 if (destlen || i != orig_sgl_count - 1)
609 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
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);
616 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
620 local_irq_restore(flags);
626 * storvsc_queuecommand - Initiate command processing
628 static int storvsc_queuecommand(struct scsi_cmnd *scmnd,
629 void (*done)(struct scsi_cmnd *))
632 struct host_device_context *host_device_ctx =
633 (struct host_device_context *)scmnd->device->host->hostdata;
634 struct vm_device *device_ctx = host_device_ctx->device_ctx;
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;
641 struct hv_storvsc_request *request;
642 struct storvsc_cmd_request *cmd_request;
643 unsigned int request_size = 0;
645 struct scatterlist *sgl;
647 DPRINT_ENTER(STORVSC_DRV);
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);
655 /* If retrying, no need to prep the cmd */
656 if (scmnd->host_scribble) {
657 ASSERT(scmnd->scsi_done != NULL);
660 (struct storvsc_cmd_request *)scmnd->host_scribble;
661 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
667 ASSERT(scmnd->scsi_done == NULL);
668 ASSERT(scmnd->host_scribble == NULL);
670 scmnd->scsi_done = done;
672 request_size = sizeof(struct storvsc_cmd_request);
674 cmd_request = kmem_cache_alloc(host_device_ctx->request_pool,
677 DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate "
678 "storvsc_cmd_request...marking queue busy", scmnd);
679 scmnd->scsi_done = NULL;
680 return SCSI_MLQUEUE_DEVICE_BUSY;
683 /* Setup the cmd request */
684 cmd_request->bounce_sgl_count = 0;
685 cmd_request->bounce_sgl = NULL;
686 cmd_request->cmd = scmnd;
688 scmnd->host_scribble = (unsigned char *)cmd_request;
690 request = &cmd_request->request;
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);
698 switch (scmnd->sc_data_direction) {
700 request->Type = WRITE_TYPE;
702 case DMA_FROM_DEVICE:
703 request->Type = READ_TYPE;
706 request->Type = UNKNOWN_TYPE;
710 request->OnIOCompletion = storvsc_commmand_completion;
711 request->Context = cmd_request;/* scmnd; */
713 /* request->PortId = scmnd->device->channel; */
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;
719 ASSERT(scmnd->cmd_len <= 16);
720 request->CdbLen = scmnd->cmd_len;
721 request->Cdb = scmnd->cmnd;
723 request->SenseBuffer = scmnd->sense_buffer;
724 request->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
727 request->DataBuffer.Length = scsi_bufflen(scmnd);
728 if (scsi_sg_count(scmnd)) {
729 sgl = (struct scatterlist *)scsi_sglist(scmnd);
731 /* check if we need to bounce the sgl */
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",
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);
744 scmnd->scsi_done = NULL;
745 scmnd->host_scribble = NULL;
746 kmem_cache_free(host_device_ctx->request_pool,
749 return SCSI_MLQUEUE_HOST_BUSY;
752 cmd_request->bounce_sgl_count =
753 ALIGN_UP(scsi_bufflen(scmnd), PAGE_SIZE) >>
757 * FIXME: We can optimize on reads by just skipping
760 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
761 scsi_sg_count(scmnd));
763 sgl = cmd_request->bounce_sgl;
766 request->DataBuffer.Offset = sgl[0].offset;
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])));
774 } else if (scsi_sglist(scmnd)) {
775 ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE);
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;
781 ASSERT(scsi_bufflen(scmnd) == 0);
785 /* Invokes the vsc to start an IO */
786 ret = storvsc_drv_obj->OnIORequest(&device_ctx->device_obj,
787 &cmd_request->request);
790 DPRINT_ERR(STORVSC_DRV,
791 "scmnd (%p) - queue FULL...marking queue busy",
794 if (cmd_request->bounce_sgl_count) {
796 * FIXME: We can optimize on writes by just skipping
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);
806 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
808 scmnd->scsi_done = NULL;
809 scmnd->host_scribble = NULL;
811 ret = SCSI_MLQUEUE_DEVICE_BUSY;
814 DPRINT_EXIT(STORVSC_DRV);
819 static int storvsc_merge_bvec(struct request_queue *q,
820 struct bvec_merge_data *bmd, struct bio_vec *bvec)
822 /* checking done by caller. */
827 * storvsc_device_configure - Configure the specified scsi device
829 static int storvsc_device_alloc(struct scsi_device *sdevice)
831 DPRINT_DBG(STORVSC_DRV, "sdev (%p) - setting device flag to %d",
832 sdevice, BLIST_SPARSELUN);
834 * This enables luns to be located sparsely. Otherwise, we may not
837 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
841 static int storvsc_device_configure(struct scsi_device *sdevice)
843 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice,
844 sdevice->queue_depth);
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);
851 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
853 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
855 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
857 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
859 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
860 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
866 * storvsc_host_reset_handler - Reset the scsi HBA
868 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
871 struct host_device_context *host_device_ctx =
872 (struct host_device_context *)scmnd->device->host->hostdata;
873 struct vm_device *device_ctx = host_device_ctx->device_ctx;
875 DPRINT_ENTER(STORVSC_DRV);
877 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
878 scmnd->device, &device_ctx->device_obj);
880 /* Invokes the vsc to reset the host/bus */
881 ret = StorVscOnHostReset(&device_ctx->device_obj);
883 DPRINT_EXIT(STORVSC_DRV);
887 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
888 scmnd->device, &device_ctx->device_obj);
890 DPRINT_EXIT(STORVSC_DRV);
895 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
896 sector_t capacity, int *info)
898 sector_t total_sectors = capacity;
899 sector_t cylinder_times_heads = 0;
902 int sectors_per_track = 0;
907 if (total_sectors > (65535 * 16 * 255))
908 total_sectors = (65535 * 16 * 255);
910 if (total_sectors >= (65535 * 16 * 63)) {
911 sectors_per_track = 255;
914 cylinder_times_heads = total_sectors;
915 /* sector_div stores the quotient in cylinder_times_heads */
916 rem = sector_div(cylinder_times_heads, sectors_per_track);
918 sectors_per_track = 17;
920 cylinder_times_heads = total_sectors;
921 /* sector_div stores the quotient in cylinder_times_heads */
922 rem = sector_div(cylinder_times_heads, sectors_per_track);
924 temp = cylinder_times_heads + 1023;
925 /* sector_div stores the quotient in temp */
926 rem = sector_div(temp, 1024);
933 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
934 sectors_per_track = 31;
937 cylinder_times_heads = total_sectors;
939 * sector_div stores the quotient in
940 * cylinder_times_heads
942 rem = sector_div(cylinder_times_heads,
946 if (cylinder_times_heads >= (heads * 1024)) {
947 sectors_per_track = 63;
950 cylinder_times_heads = total_sectors;
952 * sector_div stores the quotient in
953 * cylinder_times_heads
955 rem = sector_div(cylinder_times_heads,
960 temp = cylinder_times_heads;
961 /* sector_div stores the quotient in temp */
962 rem = sector_div(temp, heads);
966 info[1] = sectors_per_track;
969 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
975 static int __init storvsc_init(void)
979 DPRINT_ENTER(STORVSC_DRV);
980 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
981 ret = storvsc_drv_init(StorVscInitialize);
982 DPRINT_EXIT(STORVSC_DRV);
986 static void __exit storvsc_exit(void)
988 DPRINT_ENTER(STORVSC_DRV);
990 DPRINT_ENTER(STORVSC_DRV);
993 MODULE_LICENSE("GPL");
994 MODULE_VERSION(HV_DRV_VERSION);
995 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
996 module_init(storvsc_init);
997 module_exit(storvsc_exit);