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"
43 static const char *g_driver_name
= "storvsc";
45 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
46 static const struct hv_guid gStorVscDeviceType
= {
48 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
49 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
53 struct host_device_context
{
55 * FIXME this is a bug */
56 /* point back to our device context */
57 struct hv_device
*device_ctx
;
58 struct kmem_cache
*request_pool
;
64 struct storvsc_cmd_request
{
65 struct list_head entry
;
66 struct scsi_cmnd
*cmd
;
68 unsigned int bounce_sgl_count
;
69 struct scatterlist
*bounce_sgl
;
71 struct hv_storvsc_request request
;
76 * stor_vsc_initialize - Main entry point
78 static int stor_vsc_initialize(struct hv_driver
*driver
)
80 struct storvsc_driver
*stor_driver
;
82 stor_driver
= hvdr_to_stordr(driver
);
85 "sizeof(struct hv_storvsc_request)=%zd "
86 "sizeof(struct vstor_packet)=%zd, "
87 "sizeof(struct vmscsi_request)=%zd",
88 sizeof(struct hv_storvsc_request
),
89 sizeof(struct vstor_packet
),
90 sizeof(struct vmscsi_request
));
92 /* Make sure we are at least 2 pages since 1 page is used for control */
94 driver
->name
= g_driver_name
;
95 memcpy(&driver
->dev_type
, &gStorVscDeviceType
,
96 sizeof(struct hv_guid
));
100 * Divide the ring buffer data size (which is 1 page less
101 * than the ring buffer size since that page is reserved for
102 * the ring buffer indices) by the max request size (which is
103 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
105 stor_driver
->max_outstanding_req_per_channel
=
106 ((stor_driver
->ring_buffer_size
- PAGE_SIZE
) /
107 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET
+
108 sizeof(struct vstor_packet
) + sizeof(u64
),
111 DPRINT_INFO(STORVSC
, "max io %u, currently %u\n",
112 stor_driver
->max_outstanding_req_per_channel
,
113 STORVSC_MAX_IO_REQUESTS
);
115 /* Setup the dispatch table */
116 stor_driver
->base
.dev_add
= storvsc_dev_add
;
117 stor_driver
->base
.dev_rm
= storvsc_dev_remove
;
118 stor_driver
->base
.cleanup
= storvsc_cleanup
;
120 stor_driver
->on_io_request
= storvsc_do_io
;
126 static int storvsc_probe(struct hv_device
*dev
);
127 static int storvsc_queuecommand(struct Scsi_Host
*shost
, struct scsi_cmnd
*scmnd
);
128 static int storvsc_device_alloc(struct scsi_device
*);
129 static int storvsc_device_configure(struct scsi_device
*);
130 static int storvsc_host_reset_handler(struct scsi_cmnd
*scmnd
);
131 static int storvsc_remove(struct hv_device
*dev
);
133 static struct scatterlist
*create_bounce_buffer(struct scatterlist
*sgl
,
134 unsigned int sg_count
,
136 static void destroy_bounce_buffer(struct scatterlist
*sgl
,
137 unsigned int sg_count
);
138 static int do_bounce_buffer(struct scatterlist
*sgl
, unsigned int sg_count
);
139 static unsigned int copy_from_bounce_buffer(struct scatterlist
*orig_sgl
,
140 struct scatterlist
*bounce_sgl
,
141 unsigned int orig_sgl_count
);
142 static unsigned int copy_to_bounce_buffer(struct scatterlist
*orig_sgl
,
143 struct scatterlist
*bounce_sgl
,
144 unsigned int orig_sgl_count
);
146 static int storvsc_get_chs(struct scsi_device
*sdev
, struct block_device
*bdev
,
147 sector_t capacity
, int *info
);
150 static int storvsc_ringbuffer_size
= STORVSC_RING_BUFFER_SIZE
;
151 module_param(storvsc_ringbuffer_size
, int, S_IRUGO
);
152 MODULE_PARM_DESC(storvsc_ringbuffer_size
, "Ring buffer size (bytes)");
154 /* The one and only one */
155 static struct storvsc_driver g_storvsc_drv
;
158 static struct scsi_host_template scsi_driver
= {
159 .module
= THIS_MODULE
,
160 .name
= "storvsc_host_t",
161 .bios_param
= storvsc_get_chs
,
162 .queuecommand
= storvsc_queuecommand
,
163 .eh_host_reset_handler
= storvsc_host_reset_handler
,
164 .slave_alloc
= storvsc_device_alloc
,
165 .slave_configure
= storvsc_device_configure
,
167 /* 64 max_queue * 1 target */
168 .can_queue
= STORVSC_MAX_IO_REQUESTS
*STORVSC_MAX_TARGETS
,
170 /* no use setting to 0 since ll_blk_rw reset it to 1 */
172 .sg_tablesize
= MAX_MULTIPAGE_BUFFER_COUNT
,
174 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
175 * into 1 sg element. If set, we must limit the max_segment_size to
176 * PAGE_SIZE, otherwise we may get 1 sg element that represents
179 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
180 .use_clustering
= ENABLE_CLUSTERING
,
181 /* Make sure we dont get a sg segment crosses a page boundary */
182 .dma_boundary
= PAGE_SIZE
-1,
187 * storvsc_drv_init - StorVsc driver initialization.
189 static int storvsc_drv_init(void)
192 struct storvsc_driver
*storvsc_drv_obj
= &g_storvsc_drv
;
193 struct hv_driver
*drv
= &g_storvsc_drv
.base
;
195 storvsc_drv_obj
->ring_buffer_size
= storvsc_ringbuffer_size
;
197 /* Callback to client driver to complete the initialization */
198 stor_vsc_initialize(&storvsc_drv_obj
->base
);
200 DPRINT_INFO(STORVSC_DRV
,
201 "max outstanding reqs %u",
202 storvsc_drv_obj
->max_outstanding_req_per_channel
);
204 if (storvsc_drv_obj
->max_outstanding_req_per_channel
<
205 STORVSC_MAX_IO_REQUESTS
) {
206 DPRINT_ERR(STORVSC_DRV
,
207 "The number of outstanding io requests (%d) "
208 "is larger than that supported (%d) internally.",
209 STORVSC_MAX_IO_REQUESTS
,
210 storvsc_drv_obj
->max_outstanding_req_per_channel
);
214 drv
->driver
.name
= storvsc_drv_obj
->base
.name
;
216 drv
->probe
= storvsc_probe
;
217 drv
->remove
= storvsc_remove
;
219 /* The driver belongs to vmbus */
220 ret
= vmbus_child_driver_register(&drv
->driver
);
226 static int stor_vsc_on_host_reset(struct hv_device
*device
)
228 struct storvsc_device
*stor_device
;
229 struct hv_storvsc_request
*request
;
230 struct vstor_packet
*vstor_packet
;
233 DPRINT_INFO(STORVSC
, "resetting host adapter...");
235 stor_device
= get_stor_device(device
);
237 DPRINT_ERR(STORVSC
, "unable to get stor device..."
238 "device being destroyed?");
242 request
= &stor_device
->reset_request
;
243 vstor_packet
= &request
->vstor_packet
;
245 init_completion(&request
->wait_event
);
247 vstor_packet
->operation
= VSTOR_OPERATION_RESET_BUS
;
248 vstor_packet
->flags
= REQUEST_COMPLETION_FLAG
;
249 vstor_packet
->vm_srb
.path_id
= stor_device
->path_id
;
251 ret
= vmbus_sendpacket(device
->channel
, vstor_packet
,
252 sizeof(struct vstor_packet
),
253 (unsigned long)&stor_device
->reset_request
,
255 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
257 DPRINT_ERR(STORVSC
, "Unable to send reset packet %p ret %d",
262 t
= wait_for_completion_timeout(&request
->wait_event
, HZ
);
268 DPRINT_INFO(STORVSC
, "host adapter reset completed");
271 * At this point, all outstanding requests in the adapter
272 * should have been flushed out and return to us
276 put_stor_device(device
);
280 static int storvsc_drv_exit_cb(struct device
*dev
, void *data
)
282 struct device
**curr
= (struct device
**)data
;
284 return 1; /* stop iterating */
287 static void storvsc_drv_exit(void)
289 struct storvsc_driver
*storvsc_drv_obj
= &g_storvsc_drv
;
290 struct hv_driver
*drv
= &g_storvsc_drv
.base
;
291 struct device
*current_dev
= NULL
;
298 ret
= driver_for_each_device(&drv
->driver
, NULL
,
299 (void *) ¤t_dev
,
300 storvsc_drv_exit_cb
);
303 DPRINT_WARN(STORVSC_DRV
,
304 "driver_for_each_device returned %d", ret
);
306 if (current_dev
== NULL
)
309 /* Initiate removal from the top-down */
310 device_unregister(current_dev
);
313 if (storvsc_drv_obj
->base
.cleanup
)
314 storvsc_drv_obj
->base
.cleanup(&storvsc_drv_obj
->base
);
316 vmbus_child_driver_unregister(&drv
->driver
);
321 * storvsc_probe - Add a new device for this driver
323 static int storvsc_probe(struct hv_device
*device
)
326 struct storvsc_driver
*storvsc_drv_obj
=
327 drv_to_stordrv(device
->device
.driver
);
328 struct Scsi_Host
*host
;
329 struct host_device_context
*host_device_ctx
;
330 struct storvsc_device_info device_info
;
332 if (!storvsc_drv_obj
->base
.dev_add
)
335 host
= scsi_host_alloc(&scsi_driver
,
336 sizeof(struct host_device_context
));
338 DPRINT_ERR(STORVSC_DRV
, "unable to allocate scsi host object");
342 dev_set_drvdata(&device
->device
, host
);
344 host_device_ctx
= (struct host_device_context
*)host
->hostdata
;
345 memset(host_device_ctx
, 0, sizeof(struct host_device_context
));
347 host_device_ctx
->port
= host
->host_no
;
348 host_device_ctx
->device_ctx
= device
;
350 host_device_ctx
->request_pool
=
351 kmem_cache_create(dev_name(&device
->device
),
352 sizeof(struct storvsc_cmd_request
), 0,
353 SLAB_HWCACHE_ALIGN
, NULL
);
355 if (!host_device_ctx
->request_pool
) {
360 device_info
.port_number
= host
->host_no
;
361 /* Call to the vsc driver to add the device */
362 ret
= storvsc_drv_obj
->base
.dev_add(device
, (void *)&device_info
);
365 DPRINT_ERR(STORVSC_DRV
, "unable to add scsi vsc device");
366 kmem_cache_destroy(host_device_ctx
->request_pool
);
371 /* host_device_ctx->port = device_info.PortNumber; */
372 host_device_ctx
->path
= device_info
.path_id
;
373 host_device_ctx
->target
= device_info
.target_id
;
375 /* max # of devices per target */
376 host
->max_lun
= STORVSC_MAX_LUNS_PER_TARGET
;
377 /* max # of targets per channel */
378 host
->max_id
= STORVSC_MAX_TARGETS
;
379 /* max # of channels */
380 host
->max_channel
= STORVSC_MAX_CHANNELS
- 1;
382 /* Register the HBA and start the scsi bus scan */
383 ret
= scsi_add_host(host
, &device
->device
);
385 DPRINT_ERR(STORVSC_DRV
, "unable to add scsi host device");
387 storvsc_drv_obj
->base
.dev_rm(device
);
389 kmem_cache_destroy(host_device_ctx
->request_pool
);
394 scsi_scan_host(host
);
399 * storvsc_remove - Callback when our device is removed
401 static int storvsc_remove(struct hv_device
*dev
)
403 struct storvsc_driver
*storvsc_drv_obj
=
404 drv_to_stordrv(dev
->device
.driver
);
405 struct Scsi_Host
*host
= dev_get_drvdata(&dev
->device
);
406 struct host_device_context
*host_device_ctx
=
407 (struct host_device_context
*)host
->hostdata
;
410 * Call to the vsc driver to let it know that the device is being
413 storvsc_drv_obj
->base
.dev_rm(dev
);
415 if (host_device_ctx
->request_pool
) {
416 kmem_cache_destroy(host_device_ctx
->request_pool
);
417 host_device_ctx
->request_pool
= NULL
;
420 DPRINT_INFO(STORVSC
, "removing host adapter (%p)...", host
);
421 scsi_remove_host(host
);
423 DPRINT_INFO(STORVSC
, "releasing host adapter (%p)...", host
);
429 * storvsc_commmand_completion - Command completion processing
431 static void storvsc_commmand_completion(struct hv_storvsc_request
*request
)
433 struct storvsc_cmd_request
*cmd_request
=
434 (struct storvsc_cmd_request
*)request
->context
;
435 struct scsi_cmnd
*scmnd
= cmd_request
->cmd
;
436 struct host_device_context
*host_device_ctx
=
437 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
438 void (*scsi_done_fn
)(struct scsi_cmnd
*);
439 struct scsi_sense_hdr sense_hdr
;
440 struct vmscsi_request
*vm_srb
;
442 /* ASSERT(request == &cmd_request->request); */
444 /* ASSERT((unsigned long)scmnd->host_scribble == */
445 /* (unsigned long)cmd_request); */
446 /* ASSERT(scmnd->scsi_done); */
448 if (cmd_request
->bounce_sgl_count
) {
449 /* using bounce buffer */
450 /* printk("copy_from_bounce_buffer\n"); */
452 /* FIXME: We can optimize on writes by just skipping this */
453 copy_from_bounce_buffer(scsi_sglist(scmnd
),
454 cmd_request
->bounce_sgl
,
455 scsi_sg_count(scmnd
));
456 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
457 cmd_request
->bounce_sgl_count
);
460 vm_srb
= &request
->vstor_packet
.vm_srb
;
461 scmnd
->result
= vm_srb
->scsi_status
;
464 if (scsi_normalize_sense(scmnd
->sense_buffer
,
465 SCSI_SENSE_BUFFERSIZE
, &sense_hdr
))
466 scsi_print_sense_hdr("storvsc", &sense_hdr
);
469 /* ASSERT(request->BytesXfer <= request->data_buffer.Length); */
470 scsi_set_resid(scmnd
,
471 request
->data_buffer
.len
-
472 vm_srb
->data_transfer_length
);
474 scsi_done_fn
= scmnd
->scsi_done
;
476 scmnd
->host_scribble
= NULL
;
477 scmnd
->scsi_done
= NULL
;
479 /* !!DO NOT MODIFY the scmnd after this call */
482 kmem_cache_free(host_device_ctx
->request_pool
, cmd_request
);
485 static int do_bounce_buffer(struct scatterlist
*sgl
, unsigned int sg_count
)
489 /* No need to check */
493 /* We have at least 2 sg entries */
494 for (i
= 0; i
< sg_count
; i
++) {
496 /* make sure 1st one does not have hole */
497 if (sgl
[i
].offset
+ sgl
[i
].length
!= PAGE_SIZE
)
499 } else if (i
== sg_count
- 1) {
500 /* make sure last one does not have hole */
501 if (sgl
[i
].offset
!= 0)
504 /* make sure no hole in the middle */
505 if (sgl
[i
].length
!= PAGE_SIZE
|| sgl
[i
].offset
!= 0)
512 static struct scatterlist
*create_bounce_buffer(struct scatterlist
*sgl
,
513 unsigned int sg_count
,
518 struct scatterlist
*bounce_sgl
;
519 struct page
*page_buf
;
521 num_pages
= ALIGN(len
, PAGE_SIZE
) >> PAGE_SHIFT
;
523 bounce_sgl
= kcalloc(num_pages
, sizeof(struct scatterlist
), GFP_ATOMIC
);
527 for (i
= 0; i
< num_pages
; i
++) {
528 page_buf
= alloc_page(GFP_ATOMIC
);
531 sg_set_page(&bounce_sgl
[i
], page_buf
, 0, 0);
537 destroy_bounce_buffer(bounce_sgl
, num_pages
);
541 static void destroy_bounce_buffer(struct scatterlist
*sgl
,
542 unsigned int sg_count
)
545 struct page
*page_buf
;
547 for (i
= 0; i
< sg_count
; i
++) {
548 page_buf
= sg_page((&sgl
[i
]));
549 if (page_buf
!= NULL
)
550 __free_page(page_buf
);
556 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
557 static unsigned int copy_to_bounce_buffer(struct scatterlist
*orig_sgl
,
558 struct scatterlist
*bounce_sgl
,
559 unsigned int orig_sgl_count
)
563 unsigned long src
, dest
;
564 unsigned int srclen
, destlen
, copylen
;
565 unsigned int total_copied
= 0;
566 unsigned long bounce_addr
= 0;
567 unsigned long src_addr
= 0;
570 local_irq_save(flags
);
572 for (i
= 0; i
< orig_sgl_count
; i
++) {
573 src_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
574 KM_IRQ0
) + orig_sgl
[i
].offset
;
576 srclen
= orig_sgl
[i
].length
;
578 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
580 if (bounce_addr
== 0)
581 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
584 /* assume bounce offset always == 0 */
585 dest
= bounce_addr
+ bounce_sgl
[j
].length
;
586 destlen
= PAGE_SIZE
- bounce_sgl
[j
].length
;
588 copylen
= min(srclen
, destlen
);
589 memcpy((void *)dest
, (void *)src
, copylen
);
591 total_copied
+= copylen
;
592 bounce_sgl
[j
].length
+= copylen
;
596 if (bounce_sgl
[j
].length
== PAGE_SIZE
) {
597 /* full..move to next entry */
598 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
601 /* if we need to use another bounce buffer */
602 if (srclen
|| i
!= orig_sgl_count
- 1)
603 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
604 } else if (srclen
== 0 && i
== orig_sgl_count
- 1) {
605 /* unmap the last bounce that is < PAGE_SIZE */
606 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
610 kunmap_atomic((void *)(src_addr
- orig_sgl
[i
].offset
), KM_IRQ0
);
613 local_irq_restore(flags
);
618 /* Assume the original sgl has enough room */
619 static unsigned int copy_from_bounce_buffer(struct scatterlist
*orig_sgl
,
620 struct scatterlist
*bounce_sgl
,
621 unsigned int orig_sgl_count
)
625 unsigned long src
, dest
;
626 unsigned int srclen
, destlen
, copylen
;
627 unsigned int total_copied
= 0;
628 unsigned long bounce_addr
= 0;
629 unsigned long dest_addr
= 0;
632 local_irq_save(flags
);
634 for (i
= 0; i
< orig_sgl_count
; i
++) {
635 dest_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
636 KM_IRQ0
) + orig_sgl
[i
].offset
;
638 destlen
= orig_sgl
[i
].length
;
639 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
641 if (bounce_addr
== 0)
642 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
645 src
= bounce_addr
+ bounce_sgl
[j
].offset
;
646 srclen
= bounce_sgl
[j
].length
- bounce_sgl
[j
].offset
;
648 copylen
= min(srclen
, destlen
);
649 memcpy((void *)dest
, (void *)src
, copylen
);
651 total_copied
+= copylen
;
652 bounce_sgl
[j
].offset
+= copylen
;
656 if (bounce_sgl
[j
].offset
== bounce_sgl
[j
].length
) {
658 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
661 /* if we need to use another bounce buffer */
662 if (destlen
|| i
!= orig_sgl_count
- 1)
663 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
664 } else if (destlen
== 0 && i
== orig_sgl_count
- 1) {
665 /* unmap the last bounce that is < PAGE_SIZE */
666 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
670 kunmap_atomic((void *)(dest_addr
- orig_sgl
[i
].offset
),
674 local_irq_restore(flags
);
680 * storvsc_queuecommand - Initiate command processing
682 static int storvsc_queuecommand_lck(struct scsi_cmnd
*scmnd
,
683 void (*done
)(struct scsi_cmnd
*))
686 struct host_device_context
*host_device_ctx
=
687 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
688 struct hv_device
*device_ctx
= host_device_ctx
->device_ctx
;
689 struct storvsc_driver
*storvsc_drv_obj
=
690 drv_to_stordrv(device_ctx
->device
.driver
);
691 struct hv_storvsc_request
*request
;
692 struct storvsc_cmd_request
*cmd_request
;
693 unsigned int request_size
= 0;
695 struct scatterlist
*sgl
;
696 unsigned int sg_count
= 0;
697 struct vmscsi_request
*vm_srb
;
699 DPRINT_DBG(STORVSC_DRV
, "scmnd %p dir %d, use_sg %d buf %p len %d "
700 "queue depth %d tagged %d", scmnd
, scmnd
->sc_data_direction
,
701 scsi_sg_count(scmnd
), scsi_sglist(scmnd
),
702 scsi_bufflen(scmnd
), scmnd
->device
->queue_depth
,
703 scmnd
->device
->tagged_supported
);
705 /* If retrying, no need to prep the cmd */
706 if (scmnd
->host_scribble
) {
707 /* ASSERT(scmnd->scsi_done != NULL); */
710 (struct storvsc_cmd_request
*)scmnd
->host_scribble
;
711 DPRINT_INFO(STORVSC_DRV
, "retrying scmnd %p cmd_request %p",
717 /* ASSERT(scmnd->scsi_done == NULL); */
718 /* ASSERT(scmnd->host_scribble == NULL); */
720 scmnd
->scsi_done
= done
;
722 request_size
= sizeof(struct storvsc_cmd_request
);
724 cmd_request
= kmem_cache_zalloc(host_device_ctx
->request_pool
,
727 DPRINT_ERR(STORVSC_DRV
, "scmnd (%p) - unable to allocate "
728 "storvsc_cmd_request...marking queue busy", scmnd
);
729 scmnd
->scsi_done
= NULL
;
730 return SCSI_MLQUEUE_DEVICE_BUSY
;
733 /* Setup the cmd request */
734 cmd_request
->bounce_sgl_count
= 0;
735 cmd_request
->bounce_sgl
= NULL
;
736 cmd_request
->cmd
= scmnd
;
738 scmnd
->host_scribble
= (unsigned char *)cmd_request
;
740 request
= &cmd_request
->request
;
741 vm_srb
= &request
->vstor_packet
.vm_srb
;
743 DPRINT_DBG(STORVSC_DRV
, "req %p size %d", request
, request_size
);
746 switch (scmnd
->sc_data_direction
) {
748 vm_srb
->data_in
= WRITE_TYPE
;
750 case DMA_FROM_DEVICE
:
751 vm_srb
->data_in
= READ_TYPE
;
754 vm_srb
->data_in
= UNKNOWN_TYPE
;
758 request
->on_io_completion
= storvsc_commmand_completion
;
759 request
->context
= cmd_request
;/* scmnd; */
761 /* request->PortId = scmnd->device->channel; */
762 vm_srb
->port_number
= host_device_ctx
->port
;
763 vm_srb
->path_id
= scmnd
->device
->channel
;
764 vm_srb
->target_id
= scmnd
->device
->id
;
765 vm_srb
->lun
= scmnd
->device
->lun
;
767 /* ASSERT(scmnd->cmd_len <= 16); */
768 vm_srb
->cdb_length
= scmnd
->cmd_len
;
770 memcpy(vm_srb
->cdb
, scmnd
->cmnd
, vm_srb
->cdb_length
);
772 request
->sense_buffer
= scmnd
->sense_buffer
;
775 request
->data_buffer
.len
= scsi_bufflen(scmnd
);
776 if (scsi_sg_count(scmnd
)) {
777 sgl
= (struct scatterlist
*)scsi_sglist(scmnd
);
778 sg_count
= scsi_sg_count(scmnd
);
780 /* check if we need to bounce the sgl */
781 if (do_bounce_buffer(sgl
, scsi_sg_count(scmnd
)) != -1) {
782 DPRINT_INFO(STORVSC_DRV
,
783 "need to bounce buffer for this scmnd %p",
785 cmd_request
->bounce_sgl
=
786 create_bounce_buffer(sgl
, scsi_sg_count(scmnd
),
787 scsi_bufflen(scmnd
));
788 if (!cmd_request
->bounce_sgl
) {
789 DPRINT_ERR(STORVSC_DRV
,
790 "unable to create bounce buffer for "
791 "this scmnd %p", scmnd
);
793 scmnd
->scsi_done
= NULL
;
794 scmnd
->host_scribble
= NULL
;
795 kmem_cache_free(host_device_ctx
->request_pool
,
798 return SCSI_MLQUEUE_HOST_BUSY
;
801 cmd_request
->bounce_sgl_count
=
802 ALIGN(scsi_bufflen(scmnd
), PAGE_SIZE
) >>
806 * FIXME: We can optimize on reads by just skipping
809 copy_to_bounce_buffer(sgl
, cmd_request
->bounce_sgl
,
810 scsi_sg_count(scmnd
));
812 sgl
= cmd_request
->bounce_sgl
;
813 sg_count
= cmd_request
->bounce_sgl_count
;
816 request
->data_buffer
.offset
= sgl
[0].offset
;
818 for (i
= 0; i
< sg_count
; i
++) {
819 DPRINT_DBG(STORVSC_DRV
, "sgl[%d] len %d offset %d\n",
820 i
, sgl
[i
].length
, sgl
[i
].offset
);
821 request
->data_buffer
.pfn_array
[i
] =
822 page_to_pfn(sg_page((&sgl
[i
])));
824 } else if (scsi_sglist(scmnd
)) {
825 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
826 request
->data_buffer
.offset
=
827 virt_to_phys(scsi_sglist(scmnd
)) & (PAGE_SIZE
-1);
828 request
->data_buffer
.pfn_array
[0] =
829 virt_to_phys(scsi_sglist(scmnd
)) >> PAGE_SHIFT
;
833 /* Invokes the vsc to start an IO */
834 ret
= storvsc_drv_obj
->on_io_request(device_ctx
,
835 &cmd_request
->request
);
838 DPRINT_ERR(STORVSC_DRV
,
839 "scmnd (%p) - queue FULL...marking queue busy",
842 if (cmd_request
->bounce_sgl_count
) {
844 * FIXME: We can optimize on writes by just skipping
847 copy_from_bounce_buffer(scsi_sglist(scmnd
),
848 cmd_request
->bounce_sgl
,
849 scsi_sg_count(scmnd
));
850 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
851 cmd_request
->bounce_sgl_count
);
854 kmem_cache_free(host_device_ctx
->request_pool
, cmd_request
);
856 scmnd
->scsi_done
= NULL
;
857 scmnd
->host_scribble
= NULL
;
859 ret
= SCSI_MLQUEUE_DEVICE_BUSY
;
865 static DEF_SCSI_QCMD(storvsc_queuecommand
)
867 static int storvsc_merge_bvec(struct request_queue
*q
,
868 struct bvec_merge_data
*bmd
, struct bio_vec
*bvec
)
870 /* checking done by caller. */
875 * storvsc_device_configure - Configure the specified scsi device
877 static int storvsc_device_alloc(struct scsi_device
*sdevice
)
879 DPRINT_DBG(STORVSC_DRV
, "sdev (%p) - setting device flag to %d",
880 sdevice
, BLIST_SPARSELUN
);
882 * This enables luns to be located sparsely. Otherwise, we may not
885 sdevice
->sdev_bflags
|= BLIST_SPARSELUN
| BLIST_LARGELUN
;
889 static int storvsc_device_configure(struct scsi_device
*sdevice
)
891 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - curr queue depth %d", sdevice
,
892 sdevice
->queue_depth
);
894 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - setting queue depth to %d",
895 sdevice
, STORVSC_MAX_IO_REQUESTS
);
896 scsi_adjust_queue_depth(sdevice
, MSG_SIMPLE_TAG
,
897 STORVSC_MAX_IO_REQUESTS
);
899 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - setting max segment size to %ld",
901 blk_queue_max_segment_size(sdevice
->request_queue
, PAGE_SIZE
);
903 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - adding merge bio vec routine",
905 blk_queue_merge_bvec(sdevice
->request_queue
, storvsc_merge_bvec
);
907 blk_queue_bounce_limit(sdevice
->request_queue
, BLK_BOUNCE_ANY
);
908 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
914 * storvsc_host_reset_handler - Reset the scsi HBA
916 static int storvsc_host_reset_handler(struct scsi_cmnd
*scmnd
)
919 struct host_device_context
*host_device_ctx
=
920 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
921 struct hv_device
*device_ctx
= host_device_ctx
->device_ctx
;
923 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) dev obj (%p) - host resetting...",
924 scmnd
->device
, device_ctx
);
926 /* Invokes the vsc to reset the host/bus */
927 ret
= stor_vsc_on_host_reset(device_ctx
);
931 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) dev obj (%p) - host reseted",
932 scmnd
->device
, device_ctx
);
937 static int storvsc_get_chs(struct scsi_device
*sdev
, struct block_device
* bdev
,
938 sector_t capacity
, int *info
)
940 sector_t total_sectors
= capacity
;
941 sector_t cylinder_times_heads
= 0;
944 int sectors_per_track
= 0;
949 if (total_sectors
> (65535 * 16 * 255))
950 total_sectors
= (65535 * 16 * 255);
952 if (total_sectors
>= (65535 * 16 * 63)) {
953 sectors_per_track
= 255;
956 cylinder_times_heads
= total_sectors
;
957 /* sector_div stores the quotient in cylinder_times_heads */
958 rem
= sector_div(cylinder_times_heads
, sectors_per_track
);
960 sectors_per_track
= 17;
962 cylinder_times_heads
= total_sectors
;
963 /* sector_div stores the quotient in cylinder_times_heads */
964 rem
= sector_div(cylinder_times_heads
, sectors_per_track
);
966 temp
= cylinder_times_heads
+ 1023;
967 /* sector_div stores the quotient in temp */
968 rem
= sector_div(temp
, 1024);
975 if (cylinder_times_heads
>= (heads
* 1024) || (heads
> 16)) {
976 sectors_per_track
= 31;
979 cylinder_times_heads
= total_sectors
;
981 * sector_div stores the quotient in
982 * cylinder_times_heads
984 rem
= sector_div(cylinder_times_heads
,
988 if (cylinder_times_heads
>= (heads
* 1024)) {
989 sectors_per_track
= 63;
992 cylinder_times_heads
= total_sectors
;
994 * sector_div stores the quotient in
995 * cylinder_times_heads
997 rem
= sector_div(cylinder_times_heads
,
1002 temp
= cylinder_times_heads
;
1003 /* sector_div stores the quotient in temp */
1004 rem
= sector_div(temp
, heads
);
1008 info
[1] = sectors_per_track
;
1009 info
[2] = cylinders
;
1011 DPRINT_INFO(STORVSC_DRV
, "CHS (%d, %d, %d)", cylinders
, heads
,
1017 static int __init
storvsc_init(void)
1021 DPRINT_INFO(STORVSC_DRV
, "Storvsc initializing....");
1022 ret
= storvsc_drv_init();
1026 static void __exit
storvsc_exit(void)
1031 MODULE_LICENSE("GPL");
1032 MODULE_VERSION(HV_DRV_VERSION
);
1033 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
1034 module_init(storvsc_init
);
1035 module_exit(storvsc_exit
);