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 DPRINT_ENTER(STORVSC_DRV
);
146 vmbus_get_interface(&storvsc_drv_obj
->Base
.VmbusChannelInterface
);
148 storvsc_drv_obj
->RingBufferSize
= storvsc_ringbuffer_size
;
150 /* Callback to client driver to complete the initialization */
151 drv_init(&storvsc_drv_obj
->Base
);
153 DPRINT_INFO(STORVSC_DRV
,
154 "request extension size %u, max outstanding reqs %u",
155 storvsc_drv_obj
->RequestExtSize
,
156 storvsc_drv_obj
->MaxOutstandingRequestsPerChannel
);
158 if (storvsc_drv_obj
->MaxOutstandingRequestsPerChannel
<
159 STORVSC_MAX_IO_REQUESTS
) {
160 DPRINT_ERR(STORVSC_DRV
,
161 "The number of outstanding io requests (%d) "
162 "is larger than that supported (%d) internally.",
163 STORVSC_MAX_IO_REQUESTS
,
164 storvsc_drv_obj
->MaxOutstandingRequestsPerChannel
);
168 drv_ctx
->driver
.name
= storvsc_drv_obj
->Base
.name
;
169 memcpy(&drv_ctx
->class_id
, &storvsc_drv_obj
->Base
.deviceType
,
170 sizeof(struct hv_guid
));
172 drv_ctx
->probe
= storvsc_probe
;
173 drv_ctx
->remove
= storvsc_remove
;
175 /* The driver belongs to vmbus */
176 ret
= vmbus_child_driver_register(drv_ctx
);
178 DPRINT_EXIT(STORVSC_DRV
);
183 static int storvsc_drv_exit_cb(struct device
*dev
, void *data
)
185 struct device
**curr
= (struct device
**)data
;
187 return 1; /* stop iterating */
190 static void storvsc_drv_exit(void)
192 struct storvsc_driver_object
*storvsc_drv_obj
= &g_storvsc_drv
.drv_obj
;
193 struct driver_context
*drv_ctx
= &g_storvsc_drv
.drv_ctx
;
194 struct device
*current_dev
= NULL
;
197 DPRINT_ENTER(STORVSC_DRV
);
203 ret
= driver_for_each_device(&drv_ctx
->driver
, NULL
,
204 (void *) ¤t_dev
,
205 storvsc_drv_exit_cb
);
208 DPRINT_WARN(STORVSC_DRV
,
209 "driver_for_each_device returned %d", ret
);
211 if (current_dev
== NULL
)
214 /* Initiate removal from the top-down */
215 device_unregister(current_dev
);
218 if (storvsc_drv_obj
->Base
.OnCleanup
)
219 storvsc_drv_obj
->Base
.OnCleanup(&storvsc_drv_obj
->Base
);
221 vmbus_child_driver_unregister(drv_ctx
);
223 DPRINT_EXIT(STORVSC_DRV
);
229 * storvsc_probe - Add a new device for this driver
231 static int storvsc_probe(struct device
*device
)
234 struct driver_context
*driver_ctx
=
235 driver_to_driver_context(device
->driver
);
236 struct storvsc_driver_context
*storvsc_drv_ctx
=
237 (struct storvsc_driver_context
*)driver_ctx
;
238 struct storvsc_driver_object
*storvsc_drv_obj
=
239 &storvsc_drv_ctx
->drv_obj
;
240 struct vm_device
*device_ctx
= device_to_vm_device(device
);
241 struct hv_device
*device_obj
= &device_ctx
->device_obj
;
242 struct Scsi_Host
*host
;
243 struct host_device_context
*host_device_ctx
;
244 struct storvsc_device_info device_info
;
246 DPRINT_ENTER(STORVSC_DRV
);
248 if (!storvsc_drv_obj
->Base
.OnDeviceAdd
)
251 host
= scsi_host_alloc(&scsi_driver
,
252 sizeof(struct host_device_context
));
254 DPRINT_ERR(STORVSC_DRV
, "unable to allocate scsi host object");
258 dev_set_drvdata(device
, host
);
260 host_device_ctx
= (struct host_device_context
*)host
->hostdata
;
261 memset(host_device_ctx
, 0, sizeof(struct host_device_context
));
263 host_device_ctx
->port
= host
->host_no
;
264 host_device_ctx
->device_ctx
= device_ctx
;
266 host_device_ctx
->request_pool
=
267 kmem_cache_create(dev_name(&device_ctx
->device
),
268 sizeof(struct storvsc_cmd_request
) +
269 storvsc_drv_obj
->RequestExtSize
, 0,
270 SLAB_HWCACHE_ALIGN
, NULL
);
272 if (!host_device_ctx
->request_pool
) {
274 DPRINT_EXIT(STORVSC_DRV
);
279 device_info
.PortNumber
= host
->host_no
;
280 /* Call to the vsc driver to add the device */
281 ret
= storvsc_drv_obj
->Base
.OnDeviceAdd(device_obj
,
282 (void *)&device_info
);
284 DPRINT_ERR(STORVSC_DRV
, "unable to add scsi vsc device");
285 kmem_cache_destroy(host_device_ctx
->request_pool
);
287 DPRINT_EXIT(STORVSC_DRV
);
292 /* host_device_ctx->port = device_info.PortNumber; */
293 host_device_ctx
->path
= device_info
.PathId
;
294 host_device_ctx
->target
= device_info
.TargetId
;
296 /* max # of devices per target */
297 host
->max_lun
= STORVSC_MAX_LUNS_PER_TARGET
;
298 /* max # of targets per channel */
299 host
->max_id
= STORVSC_MAX_TARGETS
;
300 /* max # of channels */
301 host
->max_channel
= STORVSC_MAX_CHANNELS
- 1;
303 /* Register the HBA and start the scsi bus scan */
304 ret
= scsi_add_host(host
, device
);
306 DPRINT_ERR(STORVSC_DRV
, "unable to add scsi host device");
308 storvsc_drv_obj
->Base
.OnDeviceRemove(device_obj
);
310 kmem_cache_destroy(host_device_ctx
->request_pool
);
312 DPRINT_EXIT(STORVSC_DRV
);
317 scsi_scan_host(host
);
319 DPRINT_EXIT(STORVSC_DRV
);
325 * storvsc_remove - Callback when our device is removed
327 static int storvsc_remove(struct device
*device
)
330 struct driver_context
*driver_ctx
=
331 driver_to_driver_context(device
->driver
);
332 struct storvsc_driver_context
*storvsc_drv_ctx
=
333 (struct storvsc_driver_context
*)driver_ctx
;
334 struct storvsc_driver_object
*storvsc_drv_obj
=
335 &storvsc_drv_ctx
->drv_obj
;
336 struct vm_device
*device_ctx
= device_to_vm_device(device
);
337 struct hv_device
*device_obj
= &device_ctx
->device_obj
;
338 struct Scsi_Host
*host
= dev_get_drvdata(device
);
339 struct host_device_context
*host_device_ctx
=
340 (struct host_device_context
*)host
->hostdata
;
343 DPRINT_ENTER(STORVSC_DRV
);
345 if (!storvsc_drv_obj
->Base
.OnDeviceRemove
) {
346 DPRINT_EXIT(STORVSC_DRV
);
351 * Call to the vsc driver to let it know that the device is being
354 ret
= storvsc_drv_obj
->Base
.OnDeviceRemove(device_obj
);
357 DPRINT_ERR(STORVSC
, "unable to remove vsc device (ret %d)",
361 if (host_device_ctx
->request_pool
) {
362 kmem_cache_destroy(host_device_ctx
->request_pool
);
363 host_device_ctx
->request_pool
= NULL
;
366 DPRINT_INFO(STORVSC
, "removing host adapter (%p)...", host
);
367 scsi_remove_host(host
);
369 DPRINT_INFO(STORVSC
, "releasing host adapter (%p)...", host
);
372 DPRINT_EXIT(STORVSC_DRV
);
378 * storvsc_commmand_completion - Command completion processing
380 static void storvsc_commmand_completion(struct hv_storvsc_request
*request
)
382 struct storvsc_cmd_request
*cmd_request
=
383 (struct storvsc_cmd_request
*)request
->Context
;
384 struct scsi_cmnd
*scmnd
= cmd_request
->cmd
;
385 struct host_device_context
*host_device_ctx
=
386 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
387 void (*scsi_done_fn
)(struct scsi_cmnd
*);
388 struct scsi_sense_hdr sense_hdr
;
390 /* ASSERT(request == &cmd_request->request); */
392 /* ASSERT((unsigned long)scmnd->host_scribble == */
393 /* (unsigned long)cmd_request); */
394 /* ASSERT(scmnd->scsi_done); */
396 DPRINT_ENTER(STORVSC_DRV
);
398 if (cmd_request
->bounce_sgl_count
) {
399 /* using bounce buffer */
400 /* printk("copy_from_bounce_buffer\n"); */
402 /* FIXME: We can optimize on writes by just skipping this */
403 copy_from_bounce_buffer(scsi_sglist(scmnd
),
404 cmd_request
->bounce_sgl
,
405 scsi_sg_count(scmnd
));
406 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
407 cmd_request
->bounce_sgl_count
);
410 scmnd
->result
= request
->Status
;
413 if (scsi_normalize_sense(scmnd
->sense_buffer
,
414 request
->SenseBufferSize
, &sense_hdr
))
415 scsi_print_sense_hdr("storvsc", &sense_hdr
);
418 /* ASSERT(request->BytesXfer <= request->DataBuffer.Length); */
419 scsi_set_resid(scmnd
, request
->DataBuffer
.Length
- request
->BytesXfer
);
421 scsi_done_fn
= scmnd
->scsi_done
;
423 scmnd
->host_scribble
= NULL
;
424 scmnd
->scsi_done
= NULL
;
426 /* !!DO NOT MODIFY the scmnd after this call */
429 kmem_cache_free(host_device_ctx
->request_pool
, cmd_request
);
431 DPRINT_EXIT(STORVSC_DRV
);
434 static int do_bounce_buffer(struct scatterlist
*sgl
, unsigned int sg_count
)
438 /* No need to check */
442 /* We have at least 2 sg entries */
443 for (i
= 0; i
< sg_count
; i
++) {
445 /* make sure 1st one does not have hole */
446 if (sgl
[i
].offset
+ sgl
[i
].length
!= PAGE_SIZE
)
448 } else if (i
== sg_count
- 1) {
449 /* make sure last one does not have hole */
450 if (sgl
[i
].offset
!= 0)
453 /* make sure no hole in the middle */
454 if (sgl
[i
].length
!= PAGE_SIZE
|| sgl
[i
].offset
!= 0)
461 static struct scatterlist
*create_bounce_buffer(struct scatterlist
*sgl
,
462 unsigned int sg_count
,
467 struct scatterlist
*bounce_sgl
;
468 struct page
*page_buf
;
470 num_pages
= ALIGN_UP(len
, PAGE_SIZE
) >> PAGE_SHIFT
;
472 bounce_sgl
= kcalloc(num_pages
, sizeof(struct scatterlist
), GFP_ATOMIC
);
476 for (i
= 0; i
< num_pages
; i
++) {
477 page_buf
= alloc_page(GFP_ATOMIC
);
480 sg_set_page(&bounce_sgl
[i
], page_buf
, 0, 0);
486 destroy_bounce_buffer(bounce_sgl
, num_pages
);
490 static void destroy_bounce_buffer(struct scatterlist
*sgl
,
491 unsigned int sg_count
)
494 struct page
*page_buf
;
496 for (i
= 0; i
< sg_count
; i
++) {
497 page_buf
= sg_page((&sgl
[i
]));
498 if (page_buf
!= NULL
)
499 __free_page(page_buf
);
505 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
506 static unsigned int copy_to_bounce_buffer(struct scatterlist
*orig_sgl
,
507 struct scatterlist
*bounce_sgl
,
508 unsigned int orig_sgl_count
)
512 unsigned long src
, dest
;
513 unsigned int srclen
, destlen
, copylen
;
514 unsigned int total_copied
= 0;
515 unsigned long bounce_addr
= 0;
516 unsigned long src_addr
= 0;
519 local_irq_save(flags
);
521 for (i
= 0; i
< orig_sgl_count
; i
++) {
522 src_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
523 KM_IRQ0
) + orig_sgl
[i
].offset
;
525 srclen
= orig_sgl
[i
].length
;
527 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
530 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
533 /* assume bounce offset always == 0 */
534 dest
= bounce_addr
+ bounce_sgl
[j
].length
;
535 destlen
= PAGE_SIZE
- bounce_sgl
[j
].length
;
537 copylen
= min(srclen
, destlen
);
538 memcpy((void *)dest
, (void *)src
, copylen
);
540 total_copied
+= copylen
;
541 bounce_sgl
[j
].length
+= copylen
;
545 if (bounce_sgl
[j
].length
== PAGE_SIZE
) {
546 /* full..move to next entry */
547 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
550 /* if we need to use another bounce buffer */
551 if (srclen
|| i
!= orig_sgl_count
- 1)
552 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
553 } else if (srclen
== 0 && i
== orig_sgl_count
- 1) {
554 /* unmap the last bounce that is < PAGE_SIZE */
555 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
559 kunmap_atomic((void *)(src_addr
- orig_sgl
[i
].offset
), KM_IRQ0
);
562 local_irq_restore(flags
);
567 /* Assume the original sgl has enough room */
568 static unsigned int copy_from_bounce_buffer(struct scatterlist
*orig_sgl
,
569 struct scatterlist
*bounce_sgl
,
570 unsigned int orig_sgl_count
)
574 unsigned long src
, dest
;
575 unsigned int srclen
, destlen
, copylen
;
576 unsigned int total_copied
= 0;
577 unsigned long bounce_addr
= 0;
578 unsigned long dest_addr
= 0;
581 local_irq_save(flags
);
583 for (i
= 0; i
< orig_sgl_count
; i
++) {
584 dest_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
585 KM_IRQ0
) + orig_sgl
[i
].offset
;
587 destlen
= orig_sgl
[i
].length
;
588 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
591 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
594 src
= bounce_addr
+ bounce_sgl
[j
].offset
;
595 srclen
= bounce_sgl
[j
].length
- bounce_sgl
[j
].offset
;
597 copylen
= min(srclen
, destlen
);
598 memcpy((void *)dest
, (void *)src
, copylen
);
600 total_copied
+= copylen
;
601 bounce_sgl
[j
].offset
+= copylen
;
605 if (bounce_sgl
[j
].offset
== bounce_sgl
[j
].length
) {
607 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
610 /* if we need to use another bounce buffer */
611 if (destlen
|| i
!= orig_sgl_count
- 1)
612 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
613 } else if (destlen
== 0 && i
== orig_sgl_count
- 1) {
614 /* unmap the last bounce that is < PAGE_SIZE */
615 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
619 kunmap_atomic((void *)(dest_addr
- orig_sgl
[i
].offset
),
623 local_irq_restore(flags
);
629 * storvsc_queuecommand - Initiate command processing
631 static int storvsc_queuecommand(struct scsi_cmnd
*scmnd
,
632 void (*done
)(struct scsi_cmnd
*))
635 struct host_device_context
*host_device_ctx
=
636 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
637 struct vm_device
*device_ctx
= host_device_ctx
->device_ctx
;
638 struct driver_context
*driver_ctx
=
639 driver_to_driver_context(device_ctx
->device
.driver
);
640 struct storvsc_driver_context
*storvsc_drv_ctx
=
641 (struct storvsc_driver_context
*)driver_ctx
;
642 struct storvsc_driver_object
*storvsc_drv_obj
=
643 &storvsc_drv_ctx
->drv_obj
;
644 struct hv_storvsc_request
*request
;
645 struct storvsc_cmd_request
*cmd_request
;
646 unsigned int request_size
= 0;
648 struct scatterlist
*sgl
;
650 DPRINT_ENTER(STORVSC_DRV
);
652 DPRINT_DBG(STORVSC_DRV
, "scmnd %p dir %d, use_sg %d buf %p len %d "
653 "queue depth %d tagged %d", scmnd
, scmnd
->sc_data_direction
,
654 scsi_sg_count(scmnd
), scsi_sglist(scmnd
),
655 scsi_bufflen(scmnd
), scmnd
->device
->queue_depth
,
656 scmnd
->device
->tagged_supported
);
658 /* If retrying, no need to prep the cmd */
659 if (scmnd
->host_scribble
) {
660 /* ASSERT(scmnd->scsi_done != NULL); */
663 (struct storvsc_cmd_request
*)scmnd
->host_scribble
;
664 DPRINT_INFO(STORVSC_DRV
, "retrying scmnd %p cmd_request %p",
670 /* ASSERT(scmnd->scsi_done == NULL); */
671 /* ASSERT(scmnd->host_scribble == NULL); */
673 scmnd
->scsi_done
= done
;
675 request_size
= sizeof(struct storvsc_cmd_request
);
677 cmd_request
= kmem_cache_alloc(host_device_ctx
->request_pool
,
680 DPRINT_ERR(STORVSC_DRV
, "scmnd (%p) - unable to allocate "
681 "storvsc_cmd_request...marking queue busy", scmnd
);
682 scmnd
->scsi_done
= NULL
;
683 return SCSI_MLQUEUE_DEVICE_BUSY
;
686 /* Setup the cmd request */
687 cmd_request
->bounce_sgl_count
= 0;
688 cmd_request
->bounce_sgl
= NULL
;
689 cmd_request
->cmd
= scmnd
;
691 scmnd
->host_scribble
= (unsigned char *)cmd_request
;
693 request
= &cmd_request
->request
;
696 (void *)((unsigned long)cmd_request
+ request_size
);
697 DPRINT_DBG(STORVSC_DRV
, "req %p size %d ext %d", request
, request_size
,
698 storvsc_drv_obj
->RequestExtSize
);
701 switch (scmnd
->sc_data_direction
) {
703 request
->Type
= WRITE_TYPE
;
705 case DMA_FROM_DEVICE
:
706 request
->Type
= READ_TYPE
;
709 request
->Type
= UNKNOWN_TYPE
;
713 request
->OnIOCompletion
= storvsc_commmand_completion
;
714 request
->Context
= cmd_request
;/* scmnd; */
716 /* request->PortId = scmnd->device->channel; */
717 request
->Host
= host_device_ctx
->port
;
718 request
->Bus
= scmnd
->device
->channel
;
719 request
->TargetId
= scmnd
->device
->id
;
720 request
->LunId
= scmnd
->device
->lun
;
722 /* ASSERT(scmnd->cmd_len <= 16); */
723 request
->CdbLen
= scmnd
->cmd_len
;
724 request
->Cdb
= scmnd
->cmnd
;
726 request
->SenseBuffer
= scmnd
->sense_buffer
;
727 request
->SenseBufferSize
= SCSI_SENSE_BUFFERSIZE
;
730 request
->DataBuffer
.Length
= scsi_bufflen(scmnd
);
731 if (scsi_sg_count(scmnd
)) {
732 sgl
= (struct scatterlist
*)scsi_sglist(scmnd
);
734 /* check if we need to bounce the sgl */
735 if (do_bounce_buffer(sgl
, scsi_sg_count(scmnd
)) != -1) {
736 DPRINT_INFO(STORVSC_DRV
,
737 "need to bounce buffer for this scmnd %p",
739 cmd_request
->bounce_sgl
=
740 create_bounce_buffer(sgl
, scsi_sg_count(scmnd
),
741 scsi_bufflen(scmnd
));
742 if (!cmd_request
->bounce_sgl
) {
743 DPRINT_ERR(STORVSC_DRV
,
744 "unable to create bounce buffer for "
745 "this scmnd %p", scmnd
);
747 scmnd
->scsi_done
= NULL
;
748 scmnd
->host_scribble
= NULL
;
749 kmem_cache_free(host_device_ctx
->request_pool
,
752 return SCSI_MLQUEUE_HOST_BUSY
;
755 cmd_request
->bounce_sgl_count
=
756 ALIGN_UP(scsi_bufflen(scmnd
), PAGE_SIZE
) >>
760 * FIXME: We can optimize on reads by just skipping
763 copy_to_bounce_buffer(sgl
, cmd_request
->bounce_sgl
,
764 scsi_sg_count(scmnd
));
766 sgl
= cmd_request
->bounce_sgl
;
769 request
->DataBuffer
.Offset
= sgl
[0].offset
;
771 for (i
= 0; i
< scsi_sg_count(scmnd
); i
++) {
772 DPRINT_DBG(STORVSC_DRV
, "sgl[%d] len %d offset %d\n",
773 i
, sgl
[i
].length
, sgl
[i
].offset
);
774 request
->DataBuffer
.PfnArray
[i
] =
775 page_to_pfn(sg_page((&sgl
[i
])));
777 } else if (scsi_sglist(scmnd
)) {
778 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
779 request
->DataBuffer
.Offset
=
780 virt_to_phys(scsi_sglist(scmnd
)) & (PAGE_SIZE
-1);
781 request
->DataBuffer
.PfnArray
[0] =
782 virt_to_phys(scsi_sglist(scmnd
)) >> PAGE_SHIFT
;
786 /* Invokes the vsc to start an IO */
787 ret
= storvsc_drv_obj
->OnIORequest(&device_ctx
->device_obj
,
788 &cmd_request
->request
);
791 DPRINT_ERR(STORVSC_DRV
,
792 "scmnd (%p) - queue FULL...marking queue busy",
795 if (cmd_request
->bounce_sgl_count
) {
797 * FIXME: We can optimize on writes by just skipping
800 copy_from_bounce_buffer(scsi_sglist(scmnd
),
801 cmd_request
->bounce_sgl
,
802 scsi_sg_count(scmnd
));
803 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
804 cmd_request
->bounce_sgl_count
);
807 kmem_cache_free(host_device_ctx
->request_pool
, cmd_request
);
809 scmnd
->scsi_done
= NULL
;
810 scmnd
->host_scribble
= NULL
;
812 ret
= SCSI_MLQUEUE_DEVICE_BUSY
;
815 DPRINT_EXIT(STORVSC_DRV
);
820 static int storvsc_merge_bvec(struct request_queue
*q
,
821 struct bvec_merge_data
*bmd
, struct bio_vec
*bvec
)
823 /* checking done by caller. */
828 * storvsc_device_configure - Configure the specified scsi device
830 static int storvsc_device_alloc(struct scsi_device
*sdevice
)
832 DPRINT_DBG(STORVSC_DRV
, "sdev (%p) - setting device flag to %d",
833 sdevice
, BLIST_SPARSELUN
);
835 * This enables luns to be located sparsely. Otherwise, we may not
838 sdevice
->sdev_bflags
|= BLIST_SPARSELUN
| BLIST_LARGELUN
;
842 static int storvsc_device_configure(struct scsi_device
*sdevice
)
844 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - curr queue depth %d", sdevice
,
845 sdevice
->queue_depth
);
847 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - setting queue depth to %d",
848 sdevice
, STORVSC_MAX_IO_REQUESTS
);
849 scsi_adjust_queue_depth(sdevice
, MSG_SIMPLE_TAG
,
850 STORVSC_MAX_IO_REQUESTS
);
852 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - setting max segment size to %ld",
854 blk_queue_max_segment_size(sdevice
->request_queue
, PAGE_SIZE
);
856 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - adding merge bio vec routine",
858 blk_queue_merge_bvec(sdevice
->request_queue
, storvsc_merge_bvec
);
860 blk_queue_bounce_limit(sdevice
->request_queue
, BLK_BOUNCE_ANY
);
861 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
867 * storvsc_host_reset_handler - Reset the scsi HBA
869 static int storvsc_host_reset_handler(struct scsi_cmnd
*scmnd
)
872 struct host_device_context
*host_device_ctx
=
873 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
874 struct vm_device
*device_ctx
= host_device_ctx
->device_ctx
;
876 DPRINT_ENTER(STORVSC_DRV
);
878 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) dev obj (%p) - host resetting...",
879 scmnd
->device
, &device_ctx
->device_obj
);
881 /* Invokes the vsc to reset the host/bus */
882 ret
= StorVscOnHostReset(&device_ctx
->device_obj
);
884 DPRINT_EXIT(STORVSC_DRV
);
888 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) dev obj (%p) - host reseted",
889 scmnd
->device
, &device_ctx
->device_obj
);
891 DPRINT_EXIT(STORVSC_DRV
);
896 static int storvsc_get_chs(struct scsi_device
*sdev
, struct block_device
* bdev
,
897 sector_t capacity
, int *info
)
899 sector_t total_sectors
= capacity
;
900 sector_t cylinder_times_heads
= 0;
903 int sectors_per_track
= 0;
908 if (total_sectors
> (65535 * 16 * 255))
909 total_sectors
= (65535 * 16 * 255);
911 if (total_sectors
>= (65535 * 16 * 63)) {
912 sectors_per_track
= 255;
915 cylinder_times_heads
= total_sectors
;
916 /* sector_div stores the quotient in cylinder_times_heads */
917 rem
= sector_div(cylinder_times_heads
, sectors_per_track
);
919 sectors_per_track
= 17;
921 cylinder_times_heads
= total_sectors
;
922 /* sector_div stores the quotient in cylinder_times_heads */
923 rem
= sector_div(cylinder_times_heads
, sectors_per_track
);
925 temp
= cylinder_times_heads
+ 1023;
926 /* sector_div stores the quotient in temp */
927 rem
= sector_div(temp
, 1024);
934 if (cylinder_times_heads
>= (heads
* 1024) || (heads
> 16)) {
935 sectors_per_track
= 31;
938 cylinder_times_heads
= total_sectors
;
940 * sector_div stores the quotient in
941 * cylinder_times_heads
943 rem
= sector_div(cylinder_times_heads
,
947 if (cylinder_times_heads
>= (heads
* 1024)) {
948 sectors_per_track
= 63;
951 cylinder_times_heads
= total_sectors
;
953 * sector_div stores the quotient in
954 * cylinder_times_heads
956 rem
= sector_div(cylinder_times_heads
,
961 temp
= cylinder_times_heads
;
962 /* sector_div stores the quotient in temp */
963 rem
= sector_div(temp
, heads
);
967 info
[1] = sectors_per_track
;
970 DPRINT_INFO(STORVSC_DRV
, "CHS (%d, %d, %d)", cylinders
, heads
,
976 static int __init
storvsc_init(void)
980 DPRINT_ENTER(STORVSC_DRV
);
981 DPRINT_INFO(STORVSC_DRV
, "Storvsc initializing....");
982 ret
= storvsc_drv_init(StorVscInitialize
);
983 DPRINT_EXIT(STORVSC_DRV
);
987 static void __exit
storvsc_exit(void)
989 DPRINT_ENTER(STORVSC_DRV
);
991 DPRINT_ENTER(STORVSC_DRV
);
994 MODULE_LICENSE("GPL");
995 MODULE_VERSION(HV_DRV_VERSION
);
996 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
997 module_init(storvsc_init
);
998 module_exit(storvsc_exit
);