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/slab.h>
23 #include <linux/module.h>
24 #include <linux/device.h>
25 #include <linux/blkdev.h>
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>
33 #include <scsi/scsi_dbg.h>
36 #include "version_info.h"
38 #include "storvsc_api.h"
41 struct host_device_context {
43 * FIXME this is a bug */
44 /* point back to our device context */
45 struct vm_device *device_ctx;
46 struct kmem_cache *request_pool;
52 struct storvsc_cmd_request {
53 struct list_head entry;
54 struct scsi_cmnd *cmd;
56 unsigned int bounce_sgl_count;
57 struct scatterlist *bounce_sgl;
59 struct hv_storvsc_request request;
60 /* !!!DO NOT ADD ANYTHING BELOW HERE!!! */
61 /* The extension buffer falls right here and is pointed to by
63 * Which sounds like a very bad design... */
66 struct storvsc_driver_context {
67 /* !! These must be the first 2 fields !! */
68 /* FIXME this is a bug... */
69 struct driver_context drv_ctx;
70 struct storvsc_driver_object drv_obj;
74 static int storvsc_probe(struct device *dev);
75 static int storvsc_queuecommand(struct scsi_cmnd *scmnd,
76 void (*done)(struct scsi_cmnd *));
77 static int storvsc_device_alloc(struct scsi_device *);
78 static int storvsc_device_configure(struct scsi_device *);
79 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd);
80 static int storvsc_remove(struct device *dev);
82 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
83 unsigned int sg_count,
85 static void destroy_bounce_buffer(struct scatterlist *sgl,
86 unsigned int sg_count);
87 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
88 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
89 struct scatterlist *bounce_sgl,
90 unsigned int orig_sgl_count);
91 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
92 struct scatterlist *bounce_sgl,
93 unsigned int orig_sgl_count);
95 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device *bdev,
96 sector_t capacity, int *info);
99 static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
100 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
101 MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
103 /* The one and only one */
104 static struct storvsc_driver_context g_storvsc_drv;
107 static struct scsi_host_template scsi_driver = {
108 .module = THIS_MODULE,
109 .name = "storvsc_host_t",
110 .bios_param = storvsc_get_chs,
111 .queuecommand = storvsc_queuecommand,
112 .eh_host_reset_handler = storvsc_host_reset_handler,
113 .slave_alloc = storvsc_device_alloc,
114 .slave_configure = storvsc_device_configure,
116 /* 64 max_queue * 1 target */
117 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
119 /* no use setting to 0 since ll_blk_rw reset it to 1 */
121 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
123 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
124 * into 1 sg element. If set, we must limit the max_segment_size to
125 * PAGE_SIZE, otherwise we may get 1 sg element that represents
128 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
129 .use_clustering = ENABLE_CLUSTERING,
130 /* Make sure we dont get a sg segment crosses a page boundary */
131 .dma_boundary = PAGE_SIZE-1,
136 * storvsc_drv_init - StorVsc driver initialization.
138 static int storvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
141 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
142 struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx;
144 vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
146 storvsc_drv_obj->RingBufferSize = storvsc_ringbuffer_size;
148 /* Callback to client driver to complete the initialization */
149 drv_init(&storvsc_drv_obj->Base);
151 DPRINT_INFO(STORVSC_DRV,
152 "request extension size %u, max outstanding reqs %u",
153 storvsc_drv_obj->RequestExtSize,
154 storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
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);
166 drv_ctx->driver.name = storvsc_drv_obj->Base.name;
167 memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType,
168 sizeof(struct hv_guid));
170 drv_ctx->probe = storvsc_probe;
171 drv_ctx->remove = storvsc_remove;
173 /* The driver belongs to vmbus */
174 ret = vmbus_child_driver_register(drv_ctx);
176 DPRINT_EXIT(STORVSC_DRV);
181 static int storvsc_drv_exit_cb(struct device *dev, void *data)
183 struct device **curr = (struct device **)data;
185 return 1; /* stop iterating */
188 static void storvsc_drv_exit(void)
190 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
191 struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx;
192 struct device *current_dev = NULL;
199 ret = driver_for_each_device(&drv_ctx->driver, NULL,
200 (void *) ¤t_dev,
201 storvsc_drv_exit_cb);
204 DPRINT_WARN(STORVSC_DRV,
205 "driver_for_each_device returned %d", ret);
207 if (current_dev == NULL)
210 /* Initiate removal from the top-down */
211 device_unregister(current_dev);
214 if (storvsc_drv_obj->Base.OnCleanup)
215 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
217 vmbus_child_driver_unregister(drv_ctx);
219 DPRINT_EXIT(STORVSC_DRV);
225 * storvsc_probe - Add a new device for this driver
227 static int storvsc_probe(struct device *device)
230 struct driver_context *driver_ctx =
231 driver_to_driver_context(device->driver);
232 struct storvsc_driver_context *storvsc_drv_ctx =
233 (struct storvsc_driver_context *)driver_ctx;
234 struct storvsc_driver_object *storvsc_drv_obj =
235 &storvsc_drv_ctx->drv_obj;
236 struct vm_device *device_ctx = device_to_vm_device(device);
237 struct hv_device *device_obj = &device_ctx->device_obj;
238 struct Scsi_Host *host;
239 struct host_device_context *host_device_ctx;
240 struct storvsc_device_info device_info;
242 if (!storvsc_drv_obj->Base.OnDeviceAdd)
245 host = scsi_host_alloc(&scsi_driver,
246 sizeof(struct host_device_context));
248 DPRINT_ERR(STORVSC_DRV, "unable to allocate scsi host object");
252 dev_set_drvdata(device, host);
254 host_device_ctx = (struct host_device_context *)host->hostdata;
255 memset(host_device_ctx, 0, sizeof(struct host_device_context));
257 host_device_ctx->port = host->host_no;
258 host_device_ctx->device_ctx = device_ctx;
260 host_device_ctx->request_pool =
261 kmem_cache_create(dev_name(&device_ctx->device),
262 sizeof(struct storvsc_cmd_request) +
263 storvsc_drv_obj->RequestExtSize, 0,
264 SLAB_HWCACHE_ALIGN, NULL);
266 if (!host_device_ctx->request_pool) {
268 DPRINT_EXIT(STORVSC_DRV);
273 device_info.PortNumber = host->host_no;
274 /* Call to the vsc driver to add the device */
275 ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj,
276 (void *)&device_info);
278 DPRINT_ERR(STORVSC_DRV, "unable to add scsi vsc device");
279 kmem_cache_destroy(host_device_ctx->request_pool);
281 DPRINT_EXIT(STORVSC_DRV);
286 /* host_device_ctx->port = device_info.PortNumber; */
287 host_device_ctx->path = device_info.PathId;
288 host_device_ctx->target = device_info.TargetId;
290 /* max # of devices per target */
291 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
292 /* max # of targets per channel */
293 host->max_id = STORVSC_MAX_TARGETS;
294 /* max # of channels */
295 host->max_channel = STORVSC_MAX_CHANNELS - 1;
297 /* Register the HBA and start the scsi bus scan */
298 ret = scsi_add_host(host, device);
300 DPRINT_ERR(STORVSC_DRV, "unable to add scsi host device");
302 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
304 kmem_cache_destroy(host_device_ctx->request_pool);
306 DPRINT_EXIT(STORVSC_DRV);
311 scsi_scan_host(host);
313 DPRINT_EXIT(STORVSC_DRV);
319 * storvsc_remove - Callback when our device is removed
321 static int storvsc_remove(struct device *device)
324 struct driver_context *driver_ctx =
325 driver_to_driver_context(device->driver);
326 struct storvsc_driver_context *storvsc_drv_ctx =
327 (struct storvsc_driver_context *)driver_ctx;
328 struct storvsc_driver_object *storvsc_drv_obj =
329 &storvsc_drv_ctx->drv_obj;
330 struct vm_device *device_ctx = device_to_vm_device(device);
331 struct hv_device *device_obj = &device_ctx->device_obj;
332 struct Scsi_Host *host = dev_get_drvdata(device);
333 struct host_device_context *host_device_ctx =
334 (struct host_device_context *)host->hostdata;
337 if (!storvsc_drv_obj->Base.OnDeviceRemove) {
338 DPRINT_EXIT(STORVSC_DRV);
343 * Call to the vsc driver to let it know that the device is being
346 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
349 DPRINT_ERR(STORVSC, "unable to remove vsc device (ret %d)",
353 if (host_device_ctx->request_pool) {
354 kmem_cache_destroy(host_device_ctx->request_pool);
355 host_device_ctx->request_pool = NULL;
358 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
359 scsi_remove_host(host);
361 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
364 DPRINT_EXIT(STORVSC_DRV);
370 * storvsc_commmand_completion - Command completion processing
372 static void storvsc_commmand_completion(struct hv_storvsc_request *request)
374 struct storvsc_cmd_request *cmd_request =
375 (struct storvsc_cmd_request *)request->Context;
376 struct scsi_cmnd *scmnd = cmd_request->cmd;
377 struct host_device_context *host_device_ctx =
378 (struct host_device_context *)scmnd->device->host->hostdata;
379 void (*scsi_done_fn)(struct scsi_cmnd *);
380 struct scsi_sense_hdr sense_hdr;
382 /* ASSERT(request == &cmd_request->request); */
384 /* ASSERT((unsigned long)scmnd->host_scribble == */
385 /* (unsigned long)cmd_request); */
386 /* ASSERT(scmnd->scsi_done); */
388 if (cmd_request->bounce_sgl_count) {
389 /* using bounce buffer */
390 /* printk("copy_from_bounce_buffer\n"); */
392 /* FIXME: We can optimize on writes by just skipping this */
393 copy_from_bounce_buffer(scsi_sglist(scmnd),
394 cmd_request->bounce_sgl,
395 scsi_sg_count(scmnd));
396 destroy_bounce_buffer(cmd_request->bounce_sgl,
397 cmd_request->bounce_sgl_count);
400 scmnd->result = request->Status;
403 if (scsi_normalize_sense(scmnd->sense_buffer,
404 request->SenseBufferSize, &sense_hdr))
405 scsi_print_sense_hdr("storvsc", &sense_hdr);
408 /* ASSERT(request->BytesXfer <= request->DataBuffer.Length); */
409 scsi_set_resid(scmnd, request->DataBuffer.Length - request->BytesXfer);
411 scsi_done_fn = scmnd->scsi_done;
413 scmnd->host_scribble = NULL;
414 scmnd->scsi_done = NULL;
416 /* !!DO NOT MODIFY the scmnd after this call */
419 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
421 DPRINT_EXIT(STORVSC_DRV);
424 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
428 /* No need to check */
432 /* We have at least 2 sg entries */
433 for (i = 0; i < sg_count; i++) {
435 /* make sure 1st one does not have hole */
436 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
438 } else if (i == sg_count - 1) {
439 /* make sure last one does not have hole */
440 if (sgl[i].offset != 0)
443 /* make sure no hole in the middle */
444 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
451 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
452 unsigned int sg_count,
457 struct scatterlist *bounce_sgl;
458 struct page *page_buf;
460 num_pages = ALIGN_UP(len, PAGE_SIZE) >> PAGE_SHIFT;
462 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
466 for (i = 0; i < num_pages; i++) {
467 page_buf = alloc_page(GFP_ATOMIC);
470 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
476 destroy_bounce_buffer(bounce_sgl, num_pages);
480 static void destroy_bounce_buffer(struct scatterlist *sgl,
481 unsigned int sg_count)
484 struct page *page_buf;
486 for (i = 0; i < sg_count; i++) {
487 page_buf = sg_page((&sgl[i]));
488 if (page_buf != NULL)
489 __free_page(page_buf);
495 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
496 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
497 struct scatterlist *bounce_sgl,
498 unsigned int orig_sgl_count)
502 unsigned long src, dest;
503 unsigned int srclen, destlen, copylen;
504 unsigned int total_copied = 0;
505 unsigned long bounce_addr = 0;
506 unsigned long src_addr = 0;
509 local_irq_save(flags);
511 for (i = 0; i < orig_sgl_count; i++) {
512 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
513 KM_IRQ0) + orig_sgl[i].offset;
515 srclen = orig_sgl[i].length;
517 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
520 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
523 /* assume bounce offset always == 0 */
524 dest = bounce_addr + bounce_sgl[j].length;
525 destlen = PAGE_SIZE - bounce_sgl[j].length;
527 copylen = min(srclen, destlen);
528 memcpy((void *)dest, (void *)src, copylen);
530 total_copied += copylen;
531 bounce_sgl[j].length += copylen;
535 if (bounce_sgl[j].length == PAGE_SIZE) {
536 /* full..move to next entry */
537 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
540 /* if we need to use another bounce buffer */
541 if (srclen || i != orig_sgl_count - 1)
542 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
543 } else if (srclen == 0 && i == orig_sgl_count - 1) {
544 /* unmap the last bounce that is < PAGE_SIZE */
545 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
549 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
552 local_irq_restore(flags);
557 /* Assume the original sgl has enough room */
558 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
559 struct scatterlist *bounce_sgl,
560 unsigned int orig_sgl_count)
564 unsigned long src, dest;
565 unsigned int srclen, destlen, copylen;
566 unsigned int total_copied = 0;
567 unsigned long bounce_addr = 0;
568 unsigned long dest_addr = 0;
571 local_irq_save(flags);
573 for (i = 0; i < orig_sgl_count; i++) {
574 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
575 KM_IRQ0) + orig_sgl[i].offset;
577 destlen = orig_sgl[i].length;
578 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
581 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
584 src = bounce_addr + bounce_sgl[j].offset;
585 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
587 copylen = min(srclen, destlen);
588 memcpy((void *)dest, (void *)src, copylen);
590 total_copied += copylen;
591 bounce_sgl[j].offset += copylen;
595 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
597 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
600 /* if we need to use another bounce buffer */
601 if (destlen || i != orig_sgl_count - 1)
602 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
603 } else if (destlen == 0 && i == orig_sgl_count - 1) {
604 /* unmap the last bounce that is < PAGE_SIZE */
605 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
609 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
613 local_irq_restore(flags);
619 * storvsc_queuecommand - Initiate command processing
621 static int storvsc_queuecommand(struct scsi_cmnd *scmnd,
622 void (*done)(struct scsi_cmnd *))
625 struct host_device_context *host_device_ctx =
626 (struct host_device_context *)scmnd->device->host->hostdata;
627 struct vm_device *device_ctx = host_device_ctx->device_ctx;
628 struct driver_context *driver_ctx =
629 driver_to_driver_context(device_ctx->device.driver);
630 struct storvsc_driver_context *storvsc_drv_ctx =
631 (struct storvsc_driver_context *)driver_ctx;
632 struct storvsc_driver_object *storvsc_drv_obj =
633 &storvsc_drv_ctx->drv_obj;
634 struct hv_storvsc_request *request;
635 struct storvsc_cmd_request *cmd_request;
636 unsigned int request_size = 0;
638 struct scatterlist *sgl;
640 DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d "
641 "queue depth %d tagged %d", scmnd, scmnd->sc_data_direction,
642 scsi_sg_count(scmnd), scsi_sglist(scmnd),
643 scsi_bufflen(scmnd), scmnd->device->queue_depth,
644 scmnd->device->tagged_supported);
646 /* If retrying, no need to prep the cmd */
647 if (scmnd->host_scribble) {
648 /* ASSERT(scmnd->scsi_done != NULL); */
651 (struct storvsc_cmd_request *)scmnd->host_scribble;
652 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
658 /* ASSERT(scmnd->scsi_done == NULL); */
659 /* ASSERT(scmnd->host_scribble == NULL); */
661 scmnd->scsi_done = done;
663 request_size = sizeof(struct storvsc_cmd_request);
665 cmd_request = kmem_cache_alloc(host_device_ctx->request_pool,
668 DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate "
669 "storvsc_cmd_request...marking queue busy", scmnd);
670 scmnd->scsi_done = NULL;
671 return SCSI_MLQUEUE_DEVICE_BUSY;
674 /* Setup the cmd request */
675 cmd_request->bounce_sgl_count = 0;
676 cmd_request->bounce_sgl = NULL;
677 cmd_request->cmd = scmnd;
679 scmnd->host_scribble = (unsigned char *)cmd_request;
681 request = &cmd_request->request;
684 (void *)((unsigned long)cmd_request + request_size);
685 DPRINT_DBG(STORVSC_DRV, "req %p size %d ext %d", request, request_size,
686 storvsc_drv_obj->RequestExtSize);
689 switch (scmnd->sc_data_direction) {
691 request->Type = WRITE_TYPE;
693 case DMA_FROM_DEVICE:
694 request->Type = READ_TYPE;
697 request->Type = UNKNOWN_TYPE;
701 request->OnIOCompletion = storvsc_commmand_completion;
702 request->Context = cmd_request;/* scmnd; */
704 /* request->PortId = scmnd->device->channel; */
705 request->Host = host_device_ctx->port;
706 request->Bus = scmnd->device->channel;
707 request->TargetId = scmnd->device->id;
708 request->LunId = scmnd->device->lun;
710 /* ASSERT(scmnd->cmd_len <= 16); */
711 request->CdbLen = scmnd->cmd_len;
712 request->Cdb = scmnd->cmnd;
714 request->SenseBuffer = scmnd->sense_buffer;
715 request->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
718 request->DataBuffer.Length = scsi_bufflen(scmnd);
719 if (scsi_sg_count(scmnd)) {
720 sgl = (struct scatterlist *)scsi_sglist(scmnd);
722 /* check if we need to bounce the sgl */
723 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
724 DPRINT_INFO(STORVSC_DRV,
725 "need to bounce buffer for this scmnd %p",
727 cmd_request->bounce_sgl =
728 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
729 scsi_bufflen(scmnd));
730 if (!cmd_request->bounce_sgl) {
731 DPRINT_ERR(STORVSC_DRV,
732 "unable to create bounce buffer for "
733 "this scmnd %p", scmnd);
735 scmnd->scsi_done = NULL;
736 scmnd->host_scribble = NULL;
737 kmem_cache_free(host_device_ctx->request_pool,
740 return SCSI_MLQUEUE_HOST_BUSY;
743 cmd_request->bounce_sgl_count =
744 ALIGN_UP(scsi_bufflen(scmnd), PAGE_SIZE) >>
748 * FIXME: We can optimize on reads by just skipping
751 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
752 scsi_sg_count(scmnd));
754 sgl = cmd_request->bounce_sgl;
757 request->DataBuffer.Offset = sgl[0].offset;
759 for (i = 0; i < scsi_sg_count(scmnd); i++) {
760 DPRINT_DBG(STORVSC_DRV, "sgl[%d] len %d offset %d\n",
761 i, sgl[i].length, sgl[i].offset);
762 request->DataBuffer.PfnArray[i] =
763 page_to_pfn(sg_page((&sgl[i])));
765 } else if (scsi_sglist(scmnd)) {
766 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
767 request->DataBuffer.Offset =
768 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
769 request->DataBuffer.PfnArray[0] =
770 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
774 /* Invokes the vsc to start an IO */
775 ret = storvsc_drv_obj->OnIORequest(&device_ctx->device_obj,
776 &cmd_request->request);
779 DPRINT_ERR(STORVSC_DRV,
780 "scmnd (%p) - queue FULL...marking queue busy",
783 if (cmd_request->bounce_sgl_count) {
785 * FIXME: We can optimize on writes by just skipping
788 copy_from_bounce_buffer(scsi_sglist(scmnd),
789 cmd_request->bounce_sgl,
790 scsi_sg_count(scmnd));
791 destroy_bounce_buffer(cmd_request->bounce_sgl,
792 cmd_request->bounce_sgl_count);
795 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
797 scmnd->scsi_done = NULL;
798 scmnd->host_scribble = NULL;
800 ret = SCSI_MLQUEUE_DEVICE_BUSY;
803 DPRINT_EXIT(STORVSC_DRV);
808 static int storvsc_merge_bvec(struct request_queue *q,
809 struct bvec_merge_data *bmd, struct bio_vec *bvec)
811 /* checking done by caller. */
816 * storvsc_device_configure - Configure the specified scsi device
818 static int storvsc_device_alloc(struct scsi_device *sdevice)
820 DPRINT_DBG(STORVSC_DRV, "sdev (%p) - setting device flag to %d",
821 sdevice, BLIST_SPARSELUN);
823 * This enables luns to be located sparsely. Otherwise, we may not
826 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
830 static int storvsc_device_configure(struct scsi_device *sdevice)
832 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice,
833 sdevice->queue_depth);
835 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting queue depth to %d",
836 sdevice, STORVSC_MAX_IO_REQUESTS);
837 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
838 STORVSC_MAX_IO_REQUESTS);
840 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
842 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
844 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
846 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
848 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
849 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
855 * storvsc_host_reset_handler - Reset the scsi HBA
857 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
860 struct host_device_context *host_device_ctx =
861 (struct host_device_context *)scmnd->device->host->hostdata;
862 struct vm_device *device_ctx = host_device_ctx->device_ctx;
864 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
865 scmnd->device, &device_ctx->device_obj);
867 /* Invokes the vsc to reset the host/bus */
868 ret = StorVscOnHostReset(&device_ctx->device_obj);
870 DPRINT_EXIT(STORVSC_DRV);
874 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
875 scmnd->device, &device_ctx->device_obj);
877 DPRINT_EXIT(STORVSC_DRV);
882 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
883 sector_t capacity, int *info)
885 sector_t total_sectors = capacity;
886 sector_t cylinder_times_heads = 0;
889 int sectors_per_track = 0;
894 if (total_sectors > (65535 * 16 * 255))
895 total_sectors = (65535 * 16 * 255);
897 if (total_sectors >= (65535 * 16 * 63)) {
898 sectors_per_track = 255;
901 cylinder_times_heads = total_sectors;
902 /* sector_div stores the quotient in cylinder_times_heads */
903 rem = sector_div(cylinder_times_heads, sectors_per_track);
905 sectors_per_track = 17;
907 cylinder_times_heads = total_sectors;
908 /* sector_div stores the quotient in cylinder_times_heads */
909 rem = sector_div(cylinder_times_heads, sectors_per_track);
911 temp = cylinder_times_heads + 1023;
912 /* sector_div stores the quotient in temp */
913 rem = sector_div(temp, 1024);
920 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
921 sectors_per_track = 31;
924 cylinder_times_heads = total_sectors;
926 * sector_div stores the quotient in
927 * cylinder_times_heads
929 rem = sector_div(cylinder_times_heads,
933 if (cylinder_times_heads >= (heads * 1024)) {
934 sectors_per_track = 63;
937 cylinder_times_heads = total_sectors;
939 * sector_div stores the quotient in
940 * cylinder_times_heads
942 rem = sector_div(cylinder_times_heads,
947 temp = cylinder_times_heads;
948 /* sector_div stores the quotient in temp */
949 rem = sector_div(temp, heads);
953 info[1] = sectors_per_track;
956 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
962 static int __init storvsc_init(void)
966 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
967 ret = storvsc_drv_init(StorVscInitialize);
968 DPRINT_EXIT(STORVSC_DRV);
972 static void __exit storvsc_exit(void)
977 MODULE_LICENSE("GPL");
978 MODULE_VERSION(HV_DRV_VERSION);
979 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
980 module_init(storvsc_init);
981 module_exit(storvsc_exit);