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 static const char *g_driver_name
= "storvsc";
43 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
44 static const struct hv_guid gStorVscDeviceType
= {
46 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
47 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
51 struct host_device_context
{
53 * FIXME this is a bug */
54 /* point back to our device context */
55 struct hv_device
*device_ctx
;
56 struct kmem_cache
*request_pool
;
62 struct storvsc_cmd_request
{
63 struct list_head entry
;
64 struct scsi_cmnd
*cmd
;
66 unsigned int bounce_sgl_count
;
67 struct scatterlist
*bounce_sgl
;
69 struct hv_storvsc_request request
;
70 /* !!!DO NOT ADD ANYTHING BELOW HERE!!! */
71 /* The extension buffer falls right here and is pointed to by
73 * Which sounds like a very bad design... */
78 * stor_vsc_initialize - Main entry point
80 int stor_vsc_initialize(struct hv_driver
*driver
)
82 struct storvsc_driver_object
*stor_driver
;
84 stor_driver
= (struct storvsc_driver_object
*)driver
;
86 DPRINT_DBG(STORVSC
, "sizeof(STORVSC_REQUEST)=%zd "
87 "sizeof(struct storvsc_request_extension)=%zd "
88 "sizeof(struct vstor_packet)=%zd, "
89 "sizeof(struct vmscsi_request)=%zd",
90 sizeof(struct hv_storvsc_request
),
91 sizeof(struct storvsc_request_extension
),
92 sizeof(struct vstor_packet
),
93 sizeof(struct vmscsi_request
));
95 /* Make sure we are at least 2 pages since 1 page is used for control */
97 driver
->name
= g_driver_name
;
98 memcpy(&driver
->dev_type
, &gStorVscDeviceType
,
99 sizeof(struct hv_guid
));
101 stor_driver
->request_ext_size
=
102 sizeof(struct storvsc_request_extension
);
105 * Divide the ring buffer data size (which is 1 page less
106 * than the ring buffer size since that page is reserved for
107 * the ring buffer indices) by the max request size (which is
108 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
110 stor_driver
->max_outstanding_req_per_channel
=
111 ((stor_driver
->ring_buffer_size
- PAGE_SIZE
) /
112 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET
+
113 sizeof(struct vstor_packet
) + sizeof(u64
),
116 DPRINT_INFO(STORVSC
, "max io %u, currently %u\n",
117 stor_driver
->max_outstanding_req_per_channel
,
118 STORVSC_MAX_IO_REQUESTS
);
120 /* Setup the dispatch table */
121 stor_driver
->base
.dev_add
= stor_vsc_on_device_add
;
122 stor_driver
->base
.dev_rm
= stor_vsc_on_device_remove
;
123 stor_driver
->base
.cleanup
= stor_vsc_on_cleanup
;
125 stor_driver
->on_io_request
= stor_vsc_on_io_request
;
131 static int storvsc_probe(struct device
*dev
);
132 static int storvsc_queuecommand(struct Scsi_Host
*shost
, struct scsi_cmnd
*scmnd
);
133 static int storvsc_device_alloc(struct scsi_device
*);
134 static int storvsc_device_configure(struct scsi_device
*);
135 static int storvsc_host_reset_handler(struct scsi_cmnd
*scmnd
);
136 static int storvsc_remove(struct device
*dev
);
138 static struct scatterlist
*create_bounce_buffer(struct scatterlist
*sgl
,
139 unsigned int sg_count
,
141 static void destroy_bounce_buffer(struct scatterlist
*sgl
,
142 unsigned int sg_count
);
143 static int do_bounce_buffer(struct scatterlist
*sgl
, unsigned int sg_count
);
144 static unsigned int copy_from_bounce_buffer(struct scatterlist
*orig_sgl
,
145 struct scatterlist
*bounce_sgl
,
146 unsigned int orig_sgl_count
);
147 static unsigned int copy_to_bounce_buffer(struct scatterlist
*orig_sgl
,
148 struct scatterlist
*bounce_sgl
,
149 unsigned int orig_sgl_count
);
151 static int storvsc_get_chs(struct scsi_device
*sdev
, struct block_device
*bdev
,
152 sector_t capacity
, int *info
);
155 static int storvsc_ringbuffer_size
= STORVSC_RING_BUFFER_SIZE
;
156 module_param(storvsc_ringbuffer_size
, int, S_IRUGO
);
157 MODULE_PARM_DESC(storvsc_ringbuffer_size
, "Ring buffer size (bytes)");
159 /* The one and only one */
160 static struct storvsc_driver_object g_storvsc_drv
;
163 static struct scsi_host_template scsi_driver
= {
164 .module
= THIS_MODULE
,
165 .name
= "storvsc_host_t",
166 .bios_param
= storvsc_get_chs
,
167 .queuecommand
= storvsc_queuecommand
,
168 .eh_host_reset_handler
= storvsc_host_reset_handler
,
169 .slave_alloc
= storvsc_device_alloc
,
170 .slave_configure
= storvsc_device_configure
,
172 /* 64 max_queue * 1 target */
173 .can_queue
= STORVSC_MAX_IO_REQUESTS
*STORVSC_MAX_TARGETS
,
175 /* no use setting to 0 since ll_blk_rw reset it to 1 */
177 .sg_tablesize
= MAX_MULTIPAGE_BUFFER_COUNT
,
179 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
180 * into 1 sg element. If set, we must limit the max_segment_size to
181 * PAGE_SIZE, otherwise we may get 1 sg element that represents
184 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
185 .use_clustering
= ENABLE_CLUSTERING
,
186 /* Make sure we dont get a sg segment crosses a page boundary */
187 .dma_boundary
= PAGE_SIZE
-1,
192 * storvsc_drv_init - StorVsc driver initialization.
194 static int storvsc_drv_init(int (*drv_init
)(struct hv_driver
*drv
))
197 struct storvsc_driver_object
*storvsc_drv_obj
= &g_storvsc_drv
;
198 struct hv_driver
*drv
= &g_storvsc_drv
.base
;
200 storvsc_drv_obj
->ring_buffer_size
= storvsc_ringbuffer_size
;
202 /* Callback to client driver to complete the initialization */
203 drv_init(&storvsc_drv_obj
->base
);
205 drv
->priv
= storvsc_drv_obj
;
207 DPRINT_INFO(STORVSC_DRV
,
208 "request extension size %u, max outstanding reqs %u",
209 storvsc_drv_obj
->request_ext_size
,
210 storvsc_drv_obj
->max_outstanding_req_per_channel
);
212 if (storvsc_drv_obj
->max_outstanding_req_per_channel
<
213 STORVSC_MAX_IO_REQUESTS
) {
214 DPRINT_ERR(STORVSC_DRV
,
215 "The number of outstanding io requests (%d) "
216 "is larger than that supported (%d) internally.",
217 STORVSC_MAX_IO_REQUESTS
,
218 storvsc_drv_obj
->max_outstanding_req_per_channel
);
222 drv
->driver
.name
= storvsc_drv_obj
->base
.name
;
224 drv
->driver
.probe
= storvsc_probe
;
225 drv
->driver
.remove
= storvsc_remove
;
227 /* The driver belongs to vmbus */
228 ret
= vmbus_child_driver_register(&drv
->driver
);
233 static int storvsc_drv_exit_cb(struct device
*dev
, void *data
)
235 struct device
**curr
= (struct device
**)data
;
237 return 1; /* stop iterating */
240 static void storvsc_drv_exit(void)
242 struct storvsc_driver_object
*storvsc_drv_obj
= &g_storvsc_drv
;
243 struct hv_driver
*drv
= &g_storvsc_drv
.base
;
244 struct device
*current_dev
= NULL
;
251 ret
= driver_for_each_device(&drv
->driver
, NULL
,
252 (void *) ¤t_dev
,
253 storvsc_drv_exit_cb
);
256 DPRINT_WARN(STORVSC_DRV
,
257 "driver_for_each_device returned %d", ret
);
259 if (current_dev
== NULL
)
262 /* Initiate removal from the top-down */
263 device_unregister(current_dev
);
266 if (storvsc_drv_obj
->base
.cleanup
)
267 storvsc_drv_obj
->base
.cleanup(&storvsc_drv_obj
->base
);
269 vmbus_child_driver_unregister(&drv
->driver
);
274 * storvsc_probe - Add a new device for this driver
276 static int storvsc_probe(struct device
*device
)
279 struct hv_driver
*drv
=
280 drv_to_hv_drv(device
->driver
);
281 struct storvsc_driver_object
*storvsc_drv_obj
= drv
->priv
;
282 struct hv_device
*device_obj
= device_to_hv_device(device
);
283 struct Scsi_Host
*host
;
284 struct host_device_context
*host_device_ctx
;
285 struct storvsc_device_info device_info
;
287 if (!storvsc_drv_obj
->base
.dev_add
)
290 host
= scsi_host_alloc(&scsi_driver
,
291 sizeof(struct host_device_context
));
293 DPRINT_ERR(STORVSC_DRV
, "unable to allocate scsi host object");
297 dev_set_drvdata(device
, host
);
299 host_device_ctx
= (struct host_device_context
*)host
->hostdata
;
300 memset(host_device_ctx
, 0, sizeof(struct host_device_context
));
302 host_device_ctx
->port
= host
->host_no
;
303 host_device_ctx
->device_ctx
= device_obj
;
305 host_device_ctx
->request_pool
=
306 kmem_cache_create(dev_name(&device_obj
->device
),
307 sizeof(struct storvsc_cmd_request
) +
308 storvsc_drv_obj
->request_ext_size
, 0,
309 SLAB_HWCACHE_ALIGN
, NULL
);
311 if (!host_device_ctx
->request_pool
) {
316 device_info
.port_number
= host
->host_no
;
317 /* Call to the vsc driver to add the device */
318 ret
= storvsc_drv_obj
->base
.dev_add(device_obj
,
319 (void *)&device_info
);
321 DPRINT_ERR(STORVSC_DRV
, "unable to add scsi vsc device");
322 kmem_cache_destroy(host_device_ctx
->request_pool
);
327 /* host_device_ctx->port = device_info.PortNumber; */
328 host_device_ctx
->path
= device_info
.path_id
;
329 host_device_ctx
->target
= device_info
.target_id
;
331 /* max # of devices per target */
332 host
->max_lun
= STORVSC_MAX_LUNS_PER_TARGET
;
333 /* max # of targets per channel */
334 host
->max_id
= STORVSC_MAX_TARGETS
;
335 /* max # of channels */
336 host
->max_channel
= STORVSC_MAX_CHANNELS
- 1;
338 /* Register the HBA and start the scsi bus scan */
339 ret
= scsi_add_host(host
, device
);
341 DPRINT_ERR(STORVSC_DRV
, "unable to add scsi host device");
343 storvsc_drv_obj
->base
.dev_rm(device_obj
);
345 kmem_cache_destroy(host_device_ctx
->request_pool
);
350 scsi_scan_host(host
);
355 * storvsc_remove - Callback when our device is removed
357 static int storvsc_remove(struct device
*device
)
360 struct hv_driver
*drv
=
361 drv_to_hv_drv(device
->driver
);
362 struct storvsc_driver_object
*storvsc_drv_obj
= drv
->priv
;
363 struct hv_device
*device_obj
= device_to_hv_device(device
);
364 struct Scsi_Host
*host
= dev_get_drvdata(device
);
365 struct host_device_context
*host_device_ctx
=
366 (struct host_device_context
*)host
->hostdata
;
369 if (!storvsc_drv_obj
->base
.dev_rm
)
373 * Call to the vsc driver to let it know that the device is being
376 ret
= storvsc_drv_obj
->base
.dev_rm(device_obj
);
379 DPRINT_ERR(STORVSC
, "unable to remove vsc device (ret %d)",
383 if (host_device_ctx
->request_pool
) {
384 kmem_cache_destroy(host_device_ctx
->request_pool
);
385 host_device_ctx
->request_pool
= NULL
;
388 DPRINT_INFO(STORVSC
, "removing host adapter (%p)...", host
);
389 scsi_remove_host(host
);
391 DPRINT_INFO(STORVSC
, "releasing host adapter (%p)...", host
);
397 * storvsc_commmand_completion - Command completion processing
399 static void storvsc_commmand_completion(struct hv_storvsc_request
*request
)
401 struct storvsc_cmd_request
*cmd_request
=
402 (struct storvsc_cmd_request
*)request
->context
;
403 struct scsi_cmnd
*scmnd
= cmd_request
->cmd
;
404 struct host_device_context
*host_device_ctx
=
405 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
406 void (*scsi_done_fn
)(struct scsi_cmnd
*);
407 struct scsi_sense_hdr sense_hdr
;
409 /* ASSERT(request == &cmd_request->request); */
411 /* ASSERT((unsigned long)scmnd->host_scribble == */
412 /* (unsigned long)cmd_request); */
413 /* ASSERT(scmnd->scsi_done); */
415 if (cmd_request
->bounce_sgl_count
) {
416 /* using bounce buffer */
417 /* printk("copy_from_bounce_buffer\n"); */
419 /* FIXME: We can optimize on writes by just skipping this */
420 copy_from_bounce_buffer(scsi_sglist(scmnd
),
421 cmd_request
->bounce_sgl
,
422 scsi_sg_count(scmnd
));
423 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
424 cmd_request
->bounce_sgl_count
);
427 scmnd
->result
= request
->status
;
430 if (scsi_normalize_sense(scmnd
->sense_buffer
,
431 request
->sense_buffer_size
, &sense_hdr
))
432 scsi_print_sense_hdr("storvsc", &sense_hdr
);
435 /* ASSERT(request->BytesXfer <= request->data_buffer.Length); */
436 scsi_set_resid(scmnd
,
437 request
->data_buffer
.len
- request
->bytes_xfer
);
439 scsi_done_fn
= scmnd
->scsi_done
;
441 scmnd
->host_scribble
= NULL
;
442 scmnd
->scsi_done
= NULL
;
444 /* !!DO NOT MODIFY the scmnd after this call */
447 kmem_cache_free(host_device_ctx
->request_pool
, cmd_request
);
450 static int do_bounce_buffer(struct scatterlist
*sgl
, unsigned int sg_count
)
454 /* No need to check */
458 /* We have at least 2 sg entries */
459 for (i
= 0; i
< sg_count
; i
++) {
461 /* make sure 1st one does not have hole */
462 if (sgl
[i
].offset
+ sgl
[i
].length
!= PAGE_SIZE
)
464 } else if (i
== sg_count
- 1) {
465 /* make sure last one does not have hole */
466 if (sgl
[i
].offset
!= 0)
469 /* make sure no hole in the middle */
470 if (sgl
[i
].length
!= PAGE_SIZE
|| sgl
[i
].offset
!= 0)
477 static struct scatterlist
*create_bounce_buffer(struct scatterlist
*sgl
,
478 unsigned int sg_count
,
483 struct scatterlist
*bounce_sgl
;
484 struct page
*page_buf
;
486 num_pages
= ALIGN(len
, PAGE_SIZE
) >> PAGE_SHIFT
;
488 bounce_sgl
= kcalloc(num_pages
, sizeof(struct scatterlist
), GFP_ATOMIC
);
492 for (i
= 0; i
< num_pages
; i
++) {
493 page_buf
= alloc_page(GFP_ATOMIC
);
496 sg_set_page(&bounce_sgl
[i
], page_buf
, 0, 0);
502 destroy_bounce_buffer(bounce_sgl
, num_pages
);
506 static void destroy_bounce_buffer(struct scatterlist
*sgl
,
507 unsigned int sg_count
)
510 struct page
*page_buf
;
512 for (i
= 0; i
< sg_count
; i
++) {
513 page_buf
= sg_page((&sgl
[i
]));
514 if (page_buf
!= NULL
)
515 __free_page(page_buf
);
521 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
522 static unsigned int copy_to_bounce_buffer(struct scatterlist
*orig_sgl
,
523 struct scatterlist
*bounce_sgl
,
524 unsigned int orig_sgl_count
)
528 unsigned long src
, dest
;
529 unsigned int srclen
, destlen
, copylen
;
530 unsigned int total_copied
= 0;
531 unsigned long bounce_addr
= 0;
532 unsigned long src_addr
= 0;
535 local_irq_save(flags
);
537 for (i
= 0; i
< orig_sgl_count
; i
++) {
538 src_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
539 KM_IRQ0
) + orig_sgl
[i
].offset
;
541 srclen
= orig_sgl
[i
].length
;
543 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
545 if (bounce_addr
== 0)
546 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
549 /* assume bounce offset always == 0 */
550 dest
= bounce_addr
+ bounce_sgl
[j
].length
;
551 destlen
= PAGE_SIZE
- bounce_sgl
[j
].length
;
553 copylen
= min(srclen
, destlen
);
554 memcpy((void *)dest
, (void *)src
, copylen
);
556 total_copied
+= copylen
;
557 bounce_sgl
[j
].length
+= copylen
;
561 if (bounce_sgl
[j
].length
== PAGE_SIZE
) {
562 /* full..move to next entry */
563 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
566 /* if we need to use another bounce buffer */
567 if (srclen
|| i
!= orig_sgl_count
- 1)
568 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
569 } else if (srclen
== 0 && i
== orig_sgl_count
- 1) {
570 /* unmap the last bounce that is < PAGE_SIZE */
571 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
575 kunmap_atomic((void *)(src_addr
- orig_sgl
[i
].offset
), KM_IRQ0
);
578 local_irq_restore(flags
);
583 /* Assume the original sgl has enough room */
584 static unsigned int copy_from_bounce_buffer(struct scatterlist
*orig_sgl
,
585 struct scatterlist
*bounce_sgl
,
586 unsigned int orig_sgl_count
)
590 unsigned long src
, dest
;
591 unsigned int srclen
, destlen
, copylen
;
592 unsigned int total_copied
= 0;
593 unsigned long bounce_addr
= 0;
594 unsigned long dest_addr
= 0;
597 local_irq_save(flags
);
599 for (i
= 0; i
< orig_sgl_count
; i
++) {
600 dest_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
601 KM_IRQ0
) + orig_sgl
[i
].offset
;
603 destlen
= orig_sgl
[i
].length
;
604 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
606 if (bounce_addr
== 0)
607 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
610 src
= bounce_addr
+ bounce_sgl
[j
].offset
;
611 srclen
= bounce_sgl
[j
].length
- bounce_sgl
[j
].offset
;
613 copylen
= min(srclen
, destlen
);
614 memcpy((void *)dest
, (void *)src
, copylen
);
616 total_copied
+= copylen
;
617 bounce_sgl
[j
].offset
+= copylen
;
621 if (bounce_sgl
[j
].offset
== bounce_sgl
[j
].length
) {
623 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
626 /* if we need to use another bounce buffer */
627 if (destlen
|| i
!= orig_sgl_count
- 1)
628 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
629 } else if (destlen
== 0 && i
== orig_sgl_count
- 1) {
630 /* unmap the last bounce that is < PAGE_SIZE */
631 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
635 kunmap_atomic((void *)(dest_addr
- orig_sgl
[i
].offset
),
639 local_irq_restore(flags
);
645 * storvsc_queuecommand - Initiate command processing
647 static int storvsc_queuecommand_lck(struct scsi_cmnd
*scmnd
,
648 void (*done
)(struct scsi_cmnd
*))
651 struct host_device_context
*host_device_ctx
=
652 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
653 struct hv_device
*device_ctx
= host_device_ctx
->device_ctx
;
654 struct hv_driver
*drv
=
655 drv_to_hv_drv(device_ctx
->device
.driver
);
656 struct storvsc_driver_object
*storvsc_drv_obj
= drv
->priv
;
657 struct hv_storvsc_request
*request
;
658 struct storvsc_cmd_request
*cmd_request
;
659 unsigned int request_size
= 0;
661 struct scatterlist
*sgl
;
662 unsigned int sg_count
= 0;
664 DPRINT_DBG(STORVSC_DRV
, "scmnd %p dir %d, use_sg %d buf %p len %d "
665 "queue depth %d tagged %d", scmnd
, scmnd
->sc_data_direction
,
666 scsi_sg_count(scmnd
), scsi_sglist(scmnd
),
667 scsi_bufflen(scmnd
), scmnd
->device
->queue_depth
,
668 scmnd
->device
->tagged_supported
);
670 /* If retrying, no need to prep the cmd */
671 if (scmnd
->host_scribble
) {
672 /* ASSERT(scmnd->scsi_done != NULL); */
675 (struct storvsc_cmd_request
*)scmnd
->host_scribble
;
676 DPRINT_INFO(STORVSC_DRV
, "retrying scmnd %p cmd_request %p",
682 /* ASSERT(scmnd->scsi_done == NULL); */
683 /* ASSERT(scmnd->host_scribble == NULL); */
685 scmnd
->scsi_done
= done
;
687 request_size
= sizeof(struct storvsc_cmd_request
);
689 cmd_request
= kmem_cache_alloc(host_device_ctx
->request_pool
,
692 DPRINT_ERR(STORVSC_DRV
, "scmnd (%p) - unable to allocate "
693 "storvsc_cmd_request...marking queue busy", scmnd
);
694 scmnd
->scsi_done
= NULL
;
695 return SCSI_MLQUEUE_DEVICE_BUSY
;
698 /* Setup the cmd request */
699 cmd_request
->bounce_sgl_count
= 0;
700 cmd_request
->bounce_sgl
= NULL
;
701 cmd_request
->cmd
= scmnd
;
703 scmnd
->host_scribble
= (unsigned char *)cmd_request
;
705 request
= &cmd_request
->request
;
708 (void *)((unsigned long)cmd_request
+ request_size
);
709 DPRINT_DBG(STORVSC_DRV
, "req %p size %d ext %d", request
, request_size
,
710 storvsc_drv_obj
->request_ext_size
);
713 switch (scmnd
->sc_data_direction
) {
715 request
->type
= WRITE_TYPE
;
717 case DMA_FROM_DEVICE
:
718 request
->type
= READ_TYPE
;
721 request
->type
= UNKNOWN_TYPE
;
725 request
->on_io_completion
= storvsc_commmand_completion
;
726 request
->context
= cmd_request
;/* scmnd; */
728 /* request->PortId = scmnd->device->channel; */
729 request
->host
= host_device_ctx
->port
;
730 request
->bus
= scmnd
->device
->channel
;
731 request
->target_id
= scmnd
->device
->id
;
732 request
->lun_id
= scmnd
->device
->lun
;
734 /* ASSERT(scmnd->cmd_len <= 16); */
735 request
->cdb_len
= scmnd
->cmd_len
;
736 request
->cdb
= scmnd
->cmnd
;
738 request
->sense_buffer
= scmnd
->sense_buffer
;
739 request
->sense_buffer_size
= SCSI_SENSE_BUFFERSIZE
;
742 request
->data_buffer
.len
= scsi_bufflen(scmnd
);
743 if (scsi_sg_count(scmnd
)) {
744 sgl
= (struct scatterlist
*)scsi_sglist(scmnd
);
745 sg_count
= scsi_sg_count(scmnd
);
747 /* check if we need to bounce the sgl */
748 if (do_bounce_buffer(sgl
, scsi_sg_count(scmnd
)) != -1) {
749 DPRINT_INFO(STORVSC_DRV
,
750 "need to bounce buffer for this scmnd %p",
752 cmd_request
->bounce_sgl
=
753 create_bounce_buffer(sgl
, scsi_sg_count(scmnd
),
754 scsi_bufflen(scmnd
));
755 if (!cmd_request
->bounce_sgl
) {
756 DPRINT_ERR(STORVSC_DRV
,
757 "unable to create bounce buffer for "
758 "this scmnd %p", scmnd
);
760 scmnd
->scsi_done
= NULL
;
761 scmnd
->host_scribble
= NULL
;
762 kmem_cache_free(host_device_ctx
->request_pool
,
765 return SCSI_MLQUEUE_HOST_BUSY
;
768 cmd_request
->bounce_sgl_count
=
769 ALIGN(scsi_bufflen(scmnd
), PAGE_SIZE
) >>
773 * FIXME: We can optimize on reads by just skipping
776 copy_to_bounce_buffer(sgl
, cmd_request
->bounce_sgl
,
777 scsi_sg_count(scmnd
));
779 sgl
= cmd_request
->bounce_sgl
;
780 sg_count
= cmd_request
->bounce_sgl_count
;
783 request
->data_buffer
.offset
= sgl
[0].offset
;
785 for (i
= 0; i
< sg_count
; i
++) {
786 DPRINT_DBG(STORVSC_DRV
, "sgl[%d] len %d offset %d\n",
787 i
, sgl
[i
].length
, sgl
[i
].offset
);
788 request
->data_buffer
.pfn_array
[i
] =
789 page_to_pfn(sg_page((&sgl
[i
])));
791 } else if (scsi_sglist(scmnd
)) {
792 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
793 request
->data_buffer
.offset
=
794 virt_to_phys(scsi_sglist(scmnd
)) & (PAGE_SIZE
-1);
795 request
->data_buffer
.pfn_array
[0] =
796 virt_to_phys(scsi_sglist(scmnd
)) >> PAGE_SHIFT
;
800 /* Invokes the vsc to start an IO */
801 ret
= storvsc_drv_obj
->on_io_request(device_ctx
,
802 &cmd_request
->request
);
805 DPRINT_ERR(STORVSC_DRV
,
806 "scmnd (%p) - queue FULL...marking queue busy",
809 if (cmd_request
->bounce_sgl_count
) {
811 * FIXME: We can optimize on writes by just skipping
814 copy_from_bounce_buffer(scsi_sglist(scmnd
),
815 cmd_request
->bounce_sgl
,
816 scsi_sg_count(scmnd
));
817 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
818 cmd_request
->bounce_sgl_count
);
821 kmem_cache_free(host_device_ctx
->request_pool
, cmd_request
);
823 scmnd
->scsi_done
= NULL
;
824 scmnd
->host_scribble
= NULL
;
826 ret
= SCSI_MLQUEUE_DEVICE_BUSY
;
832 static DEF_SCSI_QCMD(storvsc_queuecommand
)
834 static int storvsc_merge_bvec(struct request_queue
*q
,
835 struct bvec_merge_data
*bmd
, struct bio_vec
*bvec
)
837 /* checking done by caller. */
842 * storvsc_device_configure - Configure the specified scsi device
844 static int storvsc_device_alloc(struct scsi_device
*sdevice
)
846 DPRINT_DBG(STORVSC_DRV
, "sdev (%p) - setting device flag to %d",
847 sdevice
, BLIST_SPARSELUN
);
849 * This enables luns to be located sparsely. Otherwise, we may not
852 sdevice
->sdev_bflags
|= BLIST_SPARSELUN
| BLIST_LARGELUN
;
856 static int storvsc_device_configure(struct scsi_device
*sdevice
)
858 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - curr queue depth %d", sdevice
,
859 sdevice
->queue_depth
);
861 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - setting queue depth to %d",
862 sdevice
, STORVSC_MAX_IO_REQUESTS
);
863 scsi_adjust_queue_depth(sdevice
, MSG_SIMPLE_TAG
,
864 STORVSC_MAX_IO_REQUESTS
);
866 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - setting max segment size to %ld",
868 blk_queue_max_segment_size(sdevice
->request_queue
, PAGE_SIZE
);
870 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - adding merge bio vec routine",
872 blk_queue_merge_bvec(sdevice
->request_queue
, storvsc_merge_bvec
);
874 blk_queue_bounce_limit(sdevice
->request_queue
, BLK_BOUNCE_ANY
);
875 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
881 * storvsc_host_reset_handler - Reset the scsi HBA
883 static int storvsc_host_reset_handler(struct scsi_cmnd
*scmnd
)
886 struct host_device_context
*host_device_ctx
=
887 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
888 struct hv_device
*device_ctx
= host_device_ctx
->device_ctx
;
890 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) dev obj (%p) - host resetting...",
891 scmnd
->device
, device_ctx
);
893 /* Invokes the vsc to reset the host/bus */
894 ret
= stor_vsc_on_host_reset(device_ctx
);
898 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) dev obj (%p) - host reseted",
899 scmnd
->device
, device_ctx
);
904 static int storvsc_get_chs(struct scsi_device
*sdev
, struct block_device
* bdev
,
905 sector_t capacity
, int *info
)
907 sector_t total_sectors
= capacity
;
908 sector_t cylinder_times_heads
= 0;
911 int sectors_per_track
= 0;
916 if (total_sectors
> (65535 * 16 * 255))
917 total_sectors
= (65535 * 16 * 255);
919 if (total_sectors
>= (65535 * 16 * 63)) {
920 sectors_per_track
= 255;
923 cylinder_times_heads
= total_sectors
;
924 /* sector_div stores the quotient in cylinder_times_heads */
925 rem
= sector_div(cylinder_times_heads
, sectors_per_track
);
927 sectors_per_track
= 17;
929 cylinder_times_heads
= total_sectors
;
930 /* sector_div stores the quotient in cylinder_times_heads */
931 rem
= sector_div(cylinder_times_heads
, sectors_per_track
);
933 temp
= cylinder_times_heads
+ 1023;
934 /* sector_div stores the quotient in temp */
935 rem
= sector_div(temp
, 1024);
942 if (cylinder_times_heads
>= (heads
* 1024) || (heads
> 16)) {
943 sectors_per_track
= 31;
946 cylinder_times_heads
= total_sectors
;
948 * sector_div stores the quotient in
949 * cylinder_times_heads
951 rem
= sector_div(cylinder_times_heads
,
955 if (cylinder_times_heads
>= (heads
* 1024)) {
956 sectors_per_track
= 63;
959 cylinder_times_heads
= total_sectors
;
961 * sector_div stores the quotient in
962 * cylinder_times_heads
964 rem
= sector_div(cylinder_times_heads
,
969 temp
= cylinder_times_heads
;
970 /* sector_div stores the quotient in temp */
971 rem
= sector_div(temp
, heads
);
975 info
[1] = sectors_per_track
;
978 DPRINT_INFO(STORVSC_DRV
, "CHS (%d, %d, %d)", cylinders
, heads
,
984 static int __init
storvsc_init(void)
988 DPRINT_INFO(STORVSC_DRV
, "Storvsc initializing....");
989 ret
= storvsc_drv_init(stor_vsc_initialize
);
993 static void __exit
storvsc_exit(void)
998 MODULE_LICENSE("GPL");
999 MODULE_VERSION(HV_DRV_VERSION
);
1000 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
1001 module_init(storvsc_init
);
1002 module_exit(storvsc_exit
);