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_object
*stor_driver
;
82 stor_driver
= hvdr_to_stordr(driver
);
84 DPRINT_DBG(STORVSC
, "sizeof(STORVSC_REQUEST)=%zd "
85 "sizeof(struct storvsc_request_extension)=%zd "
86 "sizeof(struct vstor_packet)=%zd, "
87 "sizeof(struct vmscsi_request)=%zd",
88 sizeof(struct hv_storvsc_request
),
89 sizeof(struct storvsc_request_extension
),
90 sizeof(struct vstor_packet
),
91 sizeof(struct vmscsi_request
));
93 /* Make sure we are at least 2 pages since 1 page is used for control */
95 driver
->name
= g_driver_name
;
96 memcpy(&driver
->dev_type
, &gStorVscDeviceType
,
97 sizeof(struct hv_guid
));
101 * Divide the ring buffer data size (which is 1 page less
102 * than the ring buffer size since that page is reserved for
103 * the ring buffer indices) by the max request size (which is
104 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
106 stor_driver
->max_outstanding_req_per_channel
=
107 ((stor_driver
->ring_buffer_size
- PAGE_SIZE
) /
108 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET
+
109 sizeof(struct vstor_packet
) + sizeof(u64
),
112 DPRINT_INFO(STORVSC
, "max io %u, currently %u\n",
113 stor_driver
->max_outstanding_req_per_channel
,
114 STORVSC_MAX_IO_REQUESTS
);
116 /* Setup the dispatch table */
117 stor_driver
->base
.dev_add
= stor_vsc_on_device_add
;
118 stor_driver
->base
.dev_rm
= stor_vsc_on_device_remove
;
119 stor_driver
->base
.cleanup
= stor_vsc_on_cleanup
;
121 stor_driver
->on_io_request
= stor_vsc_on_io_request
;
127 static int storvsc_probe(struct device
*dev
);
128 static int storvsc_queuecommand(struct Scsi_Host
*shost
, struct scsi_cmnd
*scmnd
);
129 static int storvsc_device_alloc(struct scsi_device
*);
130 static int storvsc_device_configure(struct scsi_device
*);
131 static int storvsc_host_reset_handler(struct scsi_cmnd
*scmnd
);
132 static int storvsc_remove(struct device
*dev
);
134 static struct scatterlist
*create_bounce_buffer(struct scatterlist
*sgl
,
135 unsigned int sg_count
,
137 static void destroy_bounce_buffer(struct scatterlist
*sgl
,
138 unsigned int sg_count
);
139 static int do_bounce_buffer(struct scatterlist
*sgl
, unsigned int sg_count
);
140 static unsigned int copy_from_bounce_buffer(struct scatterlist
*orig_sgl
,
141 struct scatterlist
*bounce_sgl
,
142 unsigned int orig_sgl_count
);
143 static unsigned int copy_to_bounce_buffer(struct scatterlist
*orig_sgl
,
144 struct scatterlist
*bounce_sgl
,
145 unsigned int orig_sgl_count
);
147 static int storvsc_get_chs(struct scsi_device
*sdev
, struct block_device
*bdev
,
148 sector_t capacity
, int *info
);
151 static int storvsc_ringbuffer_size
= STORVSC_RING_BUFFER_SIZE
;
152 module_param(storvsc_ringbuffer_size
, int, S_IRUGO
);
153 MODULE_PARM_DESC(storvsc_ringbuffer_size
, "Ring buffer size (bytes)");
155 /* The one and only one */
156 static struct storvsc_driver_object g_storvsc_drv
;
159 static struct scsi_host_template scsi_driver
= {
160 .module
= THIS_MODULE
,
161 .name
= "storvsc_host_t",
162 .bios_param
= storvsc_get_chs
,
163 .queuecommand
= storvsc_queuecommand
,
164 .eh_host_reset_handler
= storvsc_host_reset_handler
,
165 .slave_alloc
= storvsc_device_alloc
,
166 .slave_configure
= storvsc_device_configure
,
168 /* 64 max_queue * 1 target */
169 .can_queue
= STORVSC_MAX_IO_REQUESTS
*STORVSC_MAX_TARGETS
,
171 /* no use setting to 0 since ll_blk_rw reset it to 1 */
173 .sg_tablesize
= MAX_MULTIPAGE_BUFFER_COUNT
,
175 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
176 * into 1 sg element. If set, we must limit the max_segment_size to
177 * PAGE_SIZE, otherwise we may get 1 sg element that represents
180 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
181 .use_clustering
= ENABLE_CLUSTERING
,
182 /* Make sure we dont get a sg segment crosses a page boundary */
183 .dma_boundary
= PAGE_SIZE
-1,
188 * storvsc_drv_init - StorVsc driver initialization.
190 static int storvsc_drv_init(void)
193 struct storvsc_driver_object
*storvsc_drv_obj
= &g_storvsc_drv
;
194 struct hv_driver
*drv
= &g_storvsc_drv
.base
;
196 storvsc_drv_obj
->ring_buffer_size
= storvsc_ringbuffer_size
;
198 /* Callback to client driver to complete the initialization */
199 stor_vsc_initialize(&storvsc_drv_obj
->base
);
201 drv
->priv
= storvsc_drv_obj
;
203 DPRINT_INFO(STORVSC_DRV
,
204 "max outstanding reqs %u",
205 storvsc_drv_obj
->max_outstanding_req_per_channel
);
207 if (storvsc_drv_obj
->max_outstanding_req_per_channel
<
208 STORVSC_MAX_IO_REQUESTS
) {
209 DPRINT_ERR(STORVSC_DRV
,
210 "The number of outstanding io requests (%d) "
211 "is larger than that supported (%d) internally.",
212 STORVSC_MAX_IO_REQUESTS
,
213 storvsc_drv_obj
->max_outstanding_req_per_channel
);
217 drv
->driver
.name
= storvsc_drv_obj
->base
.name
;
219 drv
->driver
.probe
= storvsc_probe
;
220 drv
->driver
.remove
= storvsc_remove
;
222 /* The driver belongs to vmbus */
223 ret
= vmbus_child_driver_register(&drv
->driver
);
229 static int stor_vsc_on_host_reset(struct hv_device
*device
)
231 struct storvsc_device
*stor_device
;
232 struct storvsc_request_extension
*request
;
233 struct vstor_packet
*vstor_packet
;
236 DPRINT_INFO(STORVSC
, "resetting host adapter...");
238 stor_device
= get_stor_device(device
);
240 DPRINT_ERR(STORVSC
, "unable to get stor device..."
241 "device being destroyed?");
245 request
= &stor_device
->reset_request
;
246 vstor_packet
= &request
->vstor_packet
;
248 init_completion(&request
->wait_event
);
250 vstor_packet
->operation
= VSTOR_OPERATION_RESET_BUS
;
251 vstor_packet
->flags
= REQUEST_COMPLETION_FLAG
;
252 vstor_packet
->vm_srb
.path_id
= stor_device
->path_id
;
254 ret
= vmbus_sendpacket(device
->channel
, vstor_packet
,
255 sizeof(struct vstor_packet
),
256 (unsigned long)&stor_device
->reset_request
,
258 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
260 DPRINT_ERR(STORVSC
, "Unable to send reset packet %p ret %d",
265 t
= wait_for_completion_timeout(&request
->wait_event
, HZ
);
271 DPRINT_INFO(STORVSC
, "host adapter reset completed");
274 * At this point, all outstanding requests in the adapter
275 * should have been flushed out and return to us
279 put_stor_device(device
);
283 static int storvsc_drv_exit_cb(struct device
*dev
, void *data
)
285 struct device
**curr
= (struct device
**)data
;
287 return 1; /* stop iterating */
290 static void storvsc_drv_exit(void)
292 struct storvsc_driver_object
*storvsc_drv_obj
= &g_storvsc_drv
;
293 struct hv_driver
*drv
= &g_storvsc_drv
.base
;
294 struct device
*current_dev
= NULL
;
301 ret
= driver_for_each_device(&drv
->driver
, NULL
,
302 (void *) ¤t_dev
,
303 storvsc_drv_exit_cb
);
306 DPRINT_WARN(STORVSC_DRV
,
307 "driver_for_each_device returned %d", ret
);
309 if (current_dev
== NULL
)
312 /* Initiate removal from the top-down */
313 device_unregister(current_dev
);
316 if (storvsc_drv_obj
->base
.cleanup
)
317 storvsc_drv_obj
->base
.cleanup(&storvsc_drv_obj
->base
);
319 vmbus_child_driver_unregister(&drv
->driver
);
324 * storvsc_probe - Add a new device for this driver
326 static int storvsc_probe(struct device
*device
)
329 struct hv_driver
*drv
=
330 drv_to_hv_drv(device
->driver
);
331 struct storvsc_driver_object
*storvsc_drv_obj
= drv
->priv
;
332 struct hv_device
*device_obj
= device_to_hv_device(device
);
333 struct Scsi_Host
*host
;
334 struct host_device_context
*host_device_ctx
;
335 struct storvsc_device_info device_info
;
337 if (!storvsc_drv_obj
->base
.dev_add
)
340 host
= scsi_host_alloc(&scsi_driver
,
341 sizeof(struct host_device_context
));
343 DPRINT_ERR(STORVSC_DRV
, "unable to allocate scsi host object");
347 dev_set_drvdata(device
, host
);
349 host_device_ctx
= (struct host_device_context
*)host
->hostdata
;
350 memset(host_device_ctx
, 0, sizeof(struct host_device_context
));
352 host_device_ctx
->port
= host
->host_no
;
353 host_device_ctx
->device_ctx
= device_obj
;
355 host_device_ctx
->request_pool
=
356 kmem_cache_create(dev_name(&device_obj
->device
),
357 sizeof(struct storvsc_cmd_request
), 0,
358 SLAB_HWCACHE_ALIGN
, NULL
);
360 if (!host_device_ctx
->request_pool
) {
365 device_info
.port_number
= host
->host_no
;
366 /* Call to the vsc driver to add the device */
367 ret
= storvsc_drv_obj
->base
.dev_add(device_obj
,
368 (void *)&device_info
);
370 DPRINT_ERR(STORVSC_DRV
, "unable to add scsi vsc device");
371 kmem_cache_destroy(host_device_ctx
->request_pool
);
376 /* host_device_ctx->port = device_info.PortNumber; */
377 host_device_ctx
->path
= device_info
.path_id
;
378 host_device_ctx
->target
= device_info
.target_id
;
380 /* max # of devices per target */
381 host
->max_lun
= STORVSC_MAX_LUNS_PER_TARGET
;
382 /* max # of targets per channel */
383 host
->max_id
= STORVSC_MAX_TARGETS
;
384 /* max # of channels */
385 host
->max_channel
= STORVSC_MAX_CHANNELS
- 1;
387 /* Register the HBA and start the scsi bus scan */
388 ret
= scsi_add_host(host
, device
);
390 DPRINT_ERR(STORVSC_DRV
, "unable to add scsi host device");
392 storvsc_drv_obj
->base
.dev_rm(device_obj
);
394 kmem_cache_destroy(host_device_ctx
->request_pool
);
399 scsi_scan_host(host
);
404 * storvsc_remove - Callback when our device is removed
406 static int storvsc_remove(struct device
*device
)
409 struct hv_driver
*drv
=
410 drv_to_hv_drv(device
->driver
);
411 struct storvsc_driver_object
*storvsc_drv_obj
= drv
->priv
;
412 struct hv_device
*device_obj
= device_to_hv_device(device
);
413 struct Scsi_Host
*host
= dev_get_drvdata(device
);
414 struct host_device_context
*host_device_ctx
=
415 (struct host_device_context
*)host
->hostdata
;
418 if (!storvsc_drv_obj
->base
.dev_rm
)
422 * Call to the vsc driver to let it know that the device is being
425 ret
= storvsc_drv_obj
->base
.dev_rm(device_obj
);
428 DPRINT_ERR(STORVSC
, "unable to remove vsc device (ret %d)",
432 if (host_device_ctx
->request_pool
) {
433 kmem_cache_destroy(host_device_ctx
->request_pool
);
434 host_device_ctx
->request_pool
= NULL
;
437 DPRINT_INFO(STORVSC
, "removing host adapter (%p)...", host
);
438 scsi_remove_host(host
);
440 DPRINT_INFO(STORVSC
, "releasing host adapter (%p)...", host
);
446 * storvsc_commmand_completion - Command completion processing
448 static void storvsc_commmand_completion(struct hv_storvsc_request
*request
)
450 struct storvsc_cmd_request
*cmd_request
=
451 (struct storvsc_cmd_request
*)request
->extension
.context
;
452 struct scsi_cmnd
*scmnd
= cmd_request
->cmd
;
453 struct host_device_context
*host_device_ctx
=
454 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
455 void (*scsi_done_fn
)(struct scsi_cmnd
*);
456 struct scsi_sense_hdr sense_hdr
;
457 struct vmscsi_request
*vm_srb
;
459 /* ASSERT(request == &cmd_request->request); */
461 /* ASSERT((unsigned long)scmnd->host_scribble == */
462 /* (unsigned long)cmd_request); */
463 /* ASSERT(scmnd->scsi_done); */
465 if (cmd_request
->bounce_sgl_count
) {
466 /* using bounce buffer */
467 /* printk("copy_from_bounce_buffer\n"); */
469 /* FIXME: We can optimize on writes by just skipping this */
470 copy_from_bounce_buffer(scsi_sglist(scmnd
),
471 cmd_request
->bounce_sgl
,
472 scsi_sg_count(scmnd
));
473 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
474 cmd_request
->bounce_sgl_count
);
477 vm_srb
= &request
->extension
.vstor_packet
.vm_srb
;
478 scmnd
->result
= vm_srb
->scsi_status
;
481 if (scsi_normalize_sense(scmnd
->sense_buffer
,
482 SCSI_SENSE_BUFFERSIZE
, &sense_hdr
))
483 scsi_print_sense_hdr("storvsc", &sense_hdr
);
486 /* ASSERT(request->BytesXfer <= request->data_buffer.Length); */
487 scsi_set_resid(scmnd
,
488 request
->extension
.data_buffer
.len
-
489 vm_srb
->data_transfer_length
);
491 scsi_done_fn
= scmnd
->scsi_done
;
493 scmnd
->host_scribble
= NULL
;
494 scmnd
->scsi_done
= NULL
;
496 /* !!DO NOT MODIFY the scmnd after this call */
499 kmem_cache_free(host_device_ctx
->request_pool
, cmd_request
);
502 static int do_bounce_buffer(struct scatterlist
*sgl
, unsigned int sg_count
)
506 /* No need to check */
510 /* We have at least 2 sg entries */
511 for (i
= 0; i
< sg_count
; i
++) {
513 /* make sure 1st one does not have hole */
514 if (sgl
[i
].offset
+ sgl
[i
].length
!= PAGE_SIZE
)
516 } else if (i
== sg_count
- 1) {
517 /* make sure last one does not have hole */
518 if (sgl
[i
].offset
!= 0)
521 /* make sure no hole in the middle */
522 if (sgl
[i
].length
!= PAGE_SIZE
|| sgl
[i
].offset
!= 0)
529 static struct scatterlist
*create_bounce_buffer(struct scatterlist
*sgl
,
530 unsigned int sg_count
,
535 struct scatterlist
*bounce_sgl
;
536 struct page
*page_buf
;
538 num_pages
= ALIGN(len
, PAGE_SIZE
) >> PAGE_SHIFT
;
540 bounce_sgl
= kcalloc(num_pages
, sizeof(struct scatterlist
), GFP_ATOMIC
);
544 for (i
= 0; i
< num_pages
; i
++) {
545 page_buf
= alloc_page(GFP_ATOMIC
);
548 sg_set_page(&bounce_sgl
[i
], page_buf
, 0, 0);
554 destroy_bounce_buffer(bounce_sgl
, num_pages
);
558 static void destroy_bounce_buffer(struct scatterlist
*sgl
,
559 unsigned int sg_count
)
562 struct page
*page_buf
;
564 for (i
= 0; i
< sg_count
; i
++) {
565 page_buf
= sg_page((&sgl
[i
]));
566 if (page_buf
!= NULL
)
567 __free_page(page_buf
);
573 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
574 static unsigned int copy_to_bounce_buffer(struct scatterlist
*orig_sgl
,
575 struct scatterlist
*bounce_sgl
,
576 unsigned int orig_sgl_count
)
580 unsigned long src
, dest
;
581 unsigned int srclen
, destlen
, copylen
;
582 unsigned int total_copied
= 0;
583 unsigned long bounce_addr
= 0;
584 unsigned long src_addr
= 0;
587 local_irq_save(flags
);
589 for (i
= 0; i
< orig_sgl_count
; i
++) {
590 src_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
591 KM_IRQ0
) + orig_sgl
[i
].offset
;
593 srclen
= orig_sgl
[i
].length
;
595 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
597 if (bounce_addr
== 0)
598 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
601 /* assume bounce offset always == 0 */
602 dest
= bounce_addr
+ bounce_sgl
[j
].length
;
603 destlen
= PAGE_SIZE
- bounce_sgl
[j
].length
;
605 copylen
= min(srclen
, destlen
);
606 memcpy((void *)dest
, (void *)src
, copylen
);
608 total_copied
+= copylen
;
609 bounce_sgl
[j
].length
+= copylen
;
613 if (bounce_sgl
[j
].length
== PAGE_SIZE
) {
614 /* full..move to next entry */
615 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
618 /* if we need to use another bounce buffer */
619 if (srclen
|| i
!= orig_sgl_count
- 1)
620 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
621 } else if (srclen
== 0 && i
== orig_sgl_count
- 1) {
622 /* unmap the last bounce that is < PAGE_SIZE */
623 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
627 kunmap_atomic((void *)(src_addr
- orig_sgl
[i
].offset
), KM_IRQ0
);
630 local_irq_restore(flags
);
635 /* Assume the original sgl has enough room */
636 static unsigned int copy_from_bounce_buffer(struct scatterlist
*orig_sgl
,
637 struct scatterlist
*bounce_sgl
,
638 unsigned int orig_sgl_count
)
642 unsigned long src
, dest
;
643 unsigned int srclen
, destlen
, copylen
;
644 unsigned int total_copied
= 0;
645 unsigned long bounce_addr
= 0;
646 unsigned long dest_addr
= 0;
649 local_irq_save(flags
);
651 for (i
= 0; i
< orig_sgl_count
; i
++) {
652 dest_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
653 KM_IRQ0
) + orig_sgl
[i
].offset
;
655 destlen
= orig_sgl
[i
].length
;
656 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
658 if (bounce_addr
== 0)
659 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
662 src
= bounce_addr
+ bounce_sgl
[j
].offset
;
663 srclen
= bounce_sgl
[j
].length
- bounce_sgl
[j
].offset
;
665 copylen
= min(srclen
, destlen
);
666 memcpy((void *)dest
, (void *)src
, copylen
);
668 total_copied
+= copylen
;
669 bounce_sgl
[j
].offset
+= copylen
;
673 if (bounce_sgl
[j
].offset
== bounce_sgl
[j
].length
) {
675 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
678 /* if we need to use another bounce buffer */
679 if (destlen
|| i
!= orig_sgl_count
- 1)
680 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
681 } else if (destlen
== 0 && i
== orig_sgl_count
- 1) {
682 /* unmap the last bounce that is < PAGE_SIZE */
683 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
687 kunmap_atomic((void *)(dest_addr
- orig_sgl
[i
].offset
),
691 local_irq_restore(flags
);
697 * storvsc_queuecommand - Initiate command processing
699 static int storvsc_queuecommand_lck(struct scsi_cmnd
*scmnd
,
700 void (*done
)(struct scsi_cmnd
*))
703 struct host_device_context
*host_device_ctx
=
704 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
705 struct hv_device
*device_ctx
= host_device_ctx
->device_ctx
;
706 struct hv_driver
*drv
=
707 drv_to_hv_drv(device_ctx
->device
.driver
);
708 struct storvsc_driver_object
*storvsc_drv_obj
= drv
->priv
;
709 struct hv_storvsc_request
*request
;
710 struct storvsc_cmd_request
*cmd_request
;
711 unsigned int request_size
= 0;
713 struct scatterlist
*sgl
;
714 unsigned int sg_count
= 0;
715 struct vmscsi_request
*vm_srb
;
717 DPRINT_DBG(STORVSC_DRV
, "scmnd %p dir %d, use_sg %d buf %p len %d "
718 "queue depth %d tagged %d", scmnd
, scmnd
->sc_data_direction
,
719 scsi_sg_count(scmnd
), scsi_sglist(scmnd
),
720 scsi_bufflen(scmnd
), scmnd
->device
->queue_depth
,
721 scmnd
->device
->tagged_supported
);
723 /* If retrying, no need to prep the cmd */
724 if (scmnd
->host_scribble
) {
725 /* ASSERT(scmnd->scsi_done != NULL); */
728 (struct storvsc_cmd_request
*)scmnd
->host_scribble
;
729 DPRINT_INFO(STORVSC_DRV
, "retrying scmnd %p cmd_request %p",
735 /* ASSERT(scmnd->scsi_done == NULL); */
736 /* ASSERT(scmnd->host_scribble == NULL); */
738 scmnd
->scsi_done
= done
;
740 request_size
= sizeof(struct storvsc_cmd_request
);
742 cmd_request
= kmem_cache_alloc(host_device_ctx
->request_pool
,
745 DPRINT_ERR(STORVSC_DRV
, "scmnd (%p) - unable to allocate "
746 "storvsc_cmd_request...marking queue busy", scmnd
);
747 scmnd
->scsi_done
= NULL
;
748 return SCSI_MLQUEUE_DEVICE_BUSY
;
751 /* Setup the cmd request */
752 cmd_request
->bounce_sgl_count
= 0;
753 cmd_request
->bounce_sgl
= NULL
;
754 cmd_request
->cmd
= scmnd
;
756 scmnd
->host_scribble
= (unsigned char *)cmd_request
;
758 request
= &cmd_request
->request
;
759 vm_srb
= &request
->extension
.vstor_packet
.vm_srb
;
761 DPRINT_DBG(STORVSC_DRV
, "req %p size %d", request
, request_size
);
764 switch (scmnd
->sc_data_direction
) {
766 vm_srb
->data_in
= WRITE_TYPE
;
768 case DMA_FROM_DEVICE
:
769 vm_srb
->data_in
= READ_TYPE
;
772 vm_srb
->data_in
= UNKNOWN_TYPE
;
776 request
->extension
.on_io_completion
= storvsc_commmand_completion
;
777 request
->extension
.context
= cmd_request
;/* scmnd; */
779 /* request->PortId = scmnd->device->channel; */
780 vm_srb
->port_number
= host_device_ctx
->port
;
781 vm_srb
->path_id
= scmnd
->device
->channel
;
782 vm_srb
->target_id
= scmnd
->device
->id
;
783 vm_srb
->lun
= scmnd
->device
->lun
;
785 /* ASSERT(scmnd->cmd_len <= 16); */
786 vm_srb
->cdb_length
= scmnd
->cmd_len
;
788 memcpy(vm_srb
->cdb
, scmnd
->cmnd
, vm_srb
->cdb_length
);
790 request
->extension
.sense_buffer
= scmnd
->sense_buffer
;
793 request
->extension
.data_buffer
.len
= scsi_bufflen(scmnd
);
794 if (scsi_sg_count(scmnd
)) {
795 sgl
= (struct scatterlist
*)scsi_sglist(scmnd
);
796 sg_count
= scsi_sg_count(scmnd
);
798 /* check if we need to bounce the sgl */
799 if (do_bounce_buffer(sgl
, scsi_sg_count(scmnd
)) != -1) {
800 DPRINT_INFO(STORVSC_DRV
,
801 "need to bounce buffer for this scmnd %p",
803 cmd_request
->bounce_sgl
=
804 create_bounce_buffer(sgl
, scsi_sg_count(scmnd
),
805 scsi_bufflen(scmnd
));
806 if (!cmd_request
->bounce_sgl
) {
807 DPRINT_ERR(STORVSC_DRV
,
808 "unable to create bounce buffer for "
809 "this scmnd %p", scmnd
);
811 scmnd
->scsi_done
= NULL
;
812 scmnd
->host_scribble
= NULL
;
813 kmem_cache_free(host_device_ctx
->request_pool
,
816 return SCSI_MLQUEUE_HOST_BUSY
;
819 cmd_request
->bounce_sgl_count
=
820 ALIGN(scsi_bufflen(scmnd
), PAGE_SIZE
) >>
824 * FIXME: We can optimize on reads by just skipping
827 copy_to_bounce_buffer(sgl
, cmd_request
->bounce_sgl
,
828 scsi_sg_count(scmnd
));
830 sgl
= cmd_request
->bounce_sgl
;
831 sg_count
= cmd_request
->bounce_sgl_count
;
834 request
->extension
.data_buffer
.offset
= sgl
[0].offset
;
836 for (i
= 0; i
< sg_count
; i
++) {
837 DPRINT_DBG(STORVSC_DRV
, "sgl[%d] len %d offset %d\n",
838 i
, sgl
[i
].length
, sgl
[i
].offset
);
839 request
->extension
.data_buffer
.pfn_array
[i
] =
840 page_to_pfn(sg_page((&sgl
[i
])));
842 } else if (scsi_sglist(scmnd
)) {
843 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
844 request
->extension
.data_buffer
.offset
=
845 virt_to_phys(scsi_sglist(scmnd
)) & (PAGE_SIZE
-1);
846 request
->extension
.data_buffer
.pfn_array
[0] =
847 virt_to_phys(scsi_sglist(scmnd
)) >> PAGE_SHIFT
;
851 /* Invokes the vsc to start an IO */
852 ret
= storvsc_drv_obj
->on_io_request(device_ctx
,
853 &cmd_request
->request
);
856 DPRINT_ERR(STORVSC_DRV
,
857 "scmnd (%p) - queue FULL...marking queue busy",
860 if (cmd_request
->bounce_sgl_count
) {
862 * FIXME: We can optimize on writes by just skipping
865 copy_from_bounce_buffer(scsi_sglist(scmnd
),
866 cmd_request
->bounce_sgl
,
867 scsi_sg_count(scmnd
));
868 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
869 cmd_request
->bounce_sgl_count
);
872 kmem_cache_free(host_device_ctx
->request_pool
, cmd_request
);
874 scmnd
->scsi_done
= NULL
;
875 scmnd
->host_scribble
= NULL
;
877 ret
= SCSI_MLQUEUE_DEVICE_BUSY
;
883 static DEF_SCSI_QCMD(storvsc_queuecommand
)
885 static int storvsc_merge_bvec(struct request_queue
*q
,
886 struct bvec_merge_data
*bmd
, struct bio_vec
*bvec
)
888 /* checking done by caller. */
893 * storvsc_device_configure - Configure the specified scsi device
895 static int storvsc_device_alloc(struct scsi_device
*sdevice
)
897 DPRINT_DBG(STORVSC_DRV
, "sdev (%p) - setting device flag to %d",
898 sdevice
, BLIST_SPARSELUN
);
900 * This enables luns to be located sparsely. Otherwise, we may not
903 sdevice
->sdev_bflags
|= BLIST_SPARSELUN
| BLIST_LARGELUN
;
907 static int storvsc_device_configure(struct scsi_device
*sdevice
)
909 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - curr queue depth %d", sdevice
,
910 sdevice
->queue_depth
);
912 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - setting queue depth to %d",
913 sdevice
, STORVSC_MAX_IO_REQUESTS
);
914 scsi_adjust_queue_depth(sdevice
, MSG_SIMPLE_TAG
,
915 STORVSC_MAX_IO_REQUESTS
);
917 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - setting max segment size to %ld",
919 blk_queue_max_segment_size(sdevice
->request_queue
, PAGE_SIZE
);
921 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - adding merge bio vec routine",
923 blk_queue_merge_bvec(sdevice
->request_queue
, storvsc_merge_bvec
);
925 blk_queue_bounce_limit(sdevice
->request_queue
, BLK_BOUNCE_ANY
);
926 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
932 * storvsc_host_reset_handler - Reset the scsi HBA
934 static int storvsc_host_reset_handler(struct scsi_cmnd
*scmnd
)
937 struct host_device_context
*host_device_ctx
=
938 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
939 struct hv_device
*device_ctx
= host_device_ctx
->device_ctx
;
941 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) dev obj (%p) - host resetting...",
942 scmnd
->device
, device_ctx
);
944 /* Invokes the vsc to reset the host/bus */
945 ret
= stor_vsc_on_host_reset(device_ctx
);
949 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) dev obj (%p) - host reseted",
950 scmnd
->device
, device_ctx
);
955 static int storvsc_get_chs(struct scsi_device
*sdev
, struct block_device
* bdev
,
956 sector_t capacity
, int *info
)
958 sector_t total_sectors
= capacity
;
959 sector_t cylinder_times_heads
= 0;
962 int sectors_per_track
= 0;
967 if (total_sectors
> (65535 * 16 * 255))
968 total_sectors
= (65535 * 16 * 255);
970 if (total_sectors
>= (65535 * 16 * 63)) {
971 sectors_per_track
= 255;
974 cylinder_times_heads
= total_sectors
;
975 /* sector_div stores the quotient in cylinder_times_heads */
976 rem
= sector_div(cylinder_times_heads
, sectors_per_track
);
978 sectors_per_track
= 17;
980 cylinder_times_heads
= total_sectors
;
981 /* sector_div stores the quotient in cylinder_times_heads */
982 rem
= sector_div(cylinder_times_heads
, sectors_per_track
);
984 temp
= cylinder_times_heads
+ 1023;
985 /* sector_div stores the quotient in temp */
986 rem
= sector_div(temp
, 1024);
993 if (cylinder_times_heads
>= (heads
* 1024) || (heads
> 16)) {
994 sectors_per_track
= 31;
997 cylinder_times_heads
= total_sectors
;
999 * sector_div stores the quotient in
1000 * cylinder_times_heads
1002 rem
= sector_div(cylinder_times_heads
,
1006 if (cylinder_times_heads
>= (heads
* 1024)) {
1007 sectors_per_track
= 63;
1010 cylinder_times_heads
= total_sectors
;
1012 * sector_div stores the quotient in
1013 * cylinder_times_heads
1015 rem
= sector_div(cylinder_times_heads
,
1020 temp
= cylinder_times_heads
;
1021 /* sector_div stores the quotient in temp */
1022 rem
= sector_div(temp
, heads
);
1026 info
[1] = sectors_per_track
;
1027 info
[2] = cylinders
;
1029 DPRINT_INFO(STORVSC_DRV
, "CHS (%d, %d, %d)", cylinders
, heads
,
1035 static int __init
storvsc_init(void)
1039 DPRINT_INFO(STORVSC_DRV
, "Storvsc initializing....");
1040 ret
= storvsc_drv_init();
1044 static void __exit
storvsc_exit(void)
1049 MODULE_LICENSE("GPL");
1050 MODULE_VERSION(HV_DRV_VERSION
);
1051 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
1052 module_init(storvsc_init
);
1053 module_exit(storvsc_exit
);