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
);
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
= stor_vsc_on_device_add
;
117 stor_driver
->base
.dev_rm
= stor_vsc_on_device_remove
;
118 stor_driver
->base
.cleanup
= stor_vsc_on_cleanup
;
120 stor_driver
->on_io_request
= stor_vsc_on_io_request
;
126 static int storvsc_probe(struct 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 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_object 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_object
*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 drv
->priv
= storvsc_drv_obj
;
202 DPRINT_INFO(STORVSC_DRV
,
203 "max outstanding reqs %u",
204 storvsc_drv_obj
->max_outstanding_req_per_channel
);
206 if (storvsc_drv_obj
->max_outstanding_req_per_channel
<
207 STORVSC_MAX_IO_REQUESTS
) {
208 DPRINT_ERR(STORVSC_DRV
,
209 "The number of outstanding io requests (%d) "
210 "is larger than that supported (%d) internally.",
211 STORVSC_MAX_IO_REQUESTS
,
212 storvsc_drv_obj
->max_outstanding_req_per_channel
);
216 drv
->driver
.name
= storvsc_drv_obj
->base
.name
;
218 drv
->driver
.probe
= storvsc_probe
;
219 drv
->driver
.remove
= storvsc_remove
;
221 /* The driver belongs to vmbus */
222 ret
= vmbus_child_driver_register(&drv
->driver
);
228 static int stor_vsc_on_host_reset(struct hv_device
*device
)
230 struct storvsc_device
*stor_device
;
231 struct hv_storvsc_request
*request
;
232 struct vstor_packet
*vstor_packet
;
235 DPRINT_INFO(STORVSC
, "resetting host adapter...");
237 stor_device
= get_stor_device(device
);
239 DPRINT_ERR(STORVSC
, "unable to get stor device..."
240 "device being destroyed?");
244 request
= &stor_device
->reset_request
;
245 vstor_packet
= &request
->vstor_packet
;
247 init_completion(&request
->wait_event
);
249 vstor_packet
->operation
= VSTOR_OPERATION_RESET_BUS
;
250 vstor_packet
->flags
= REQUEST_COMPLETION_FLAG
;
251 vstor_packet
->vm_srb
.path_id
= stor_device
->path_id
;
253 ret
= vmbus_sendpacket(device
->channel
, vstor_packet
,
254 sizeof(struct vstor_packet
),
255 (unsigned long)&stor_device
->reset_request
,
257 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
259 DPRINT_ERR(STORVSC
, "Unable to send reset packet %p ret %d",
264 t
= wait_for_completion_timeout(&request
->wait_event
, HZ
);
270 DPRINT_INFO(STORVSC
, "host adapter reset completed");
273 * At this point, all outstanding requests in the adapter
274 * should have been flushed out and return to us
278 put_stor_device(device
);
282 static int storvsc_drv_exit_cb(struct device
*dev
, void *data
)
284 struct device
**curr
= (struct device
**)data
;
286 return 1; /* stop iterating */
289 static void storvsc_drv_exit(void)
291 struct storvsc_driver_object
*storvsc_drv_obj
= &g_storvsc_drv
;
292 struct hv_driver
*drv
= &g_storvsc_drv
.base
;
293 struct device
*current_dev
= NULL
;
300 ret
= driver_for_each_device(&drv
->driver
, NULL
,
301 (void *) ¤t_dev
,
302 storvsc_drv_exit_cb
);
305 DPRINT_WARN(STORVSC_DRV
,
306 "driver_for_each_device returned %d", ret
);
308 if (current_dev
== NULL
)
311 /* Initiate removal from the top-down */
312 device_unregister(current_dev
);
315 if (storvsc_drv_obj
->base
.cleanup
)
316 storvsc_drv_obj
->base
.cleanup(&storvsc_drv_obj
->base
);
318 vmbus_child_driver_unregister(&drv
->driver
);
323 * storvsc_probe - Add a new device for this driver
325 static int storvsc_probe(struct device
*device
)
328 struct hv_driver
*drv
=
329 drv_to_hv_drv(device
->driver
);
330 struct storvsc_driver_object
*storvsc_drv_obj
= drv
->priv
;
331 struct hv_device
*device_obj
= device_to_hv_device(device
);
332 struct Scsi_Host
*host
;
333 struct host_device_context
*host_device_ctx
;
334 struct storvsc_device_info device_info
;
336 if (!storvsc_drv_obj
->base
.dev_add
)
339 host
= scsi_host_alloc(&scsi_driver
,
340 sizeof(struct host_device_context
));
342 DPRINT_ERR(STORVSC_DRV
, "unable to allocate scsi host object");
346 dev_set_drvdata(device
, host
);
348 host_device_ctx
= (struct host_device_context
*)host
->hostdata
;
349 memset(host_device_ctx
, 0, sizeof(struct host_device_context
));
351 host_device_ctx
->port
= host
->host_no
;
352 host_device_ctx
->device_ctx
= device_obj
;
354 host_device_ctx
->request_pool
=
355 kmem_cache_create(dev_name(&device_obj
->device
),
356 sizeof(struct storvsc_cmd_request
), 0,
357 SLAB_HWCACHE_ALIGN
, NULL
);
359 if (!host_device_ctx
->request_pool
) {
364 device_info
.port_number
= host
->host_no
;
365 /* Call to the vsc driver to add the device */
366 ret
= storvsc_drv_obj
->base
.dev_add(device_obj
,
367 (void *)&device_info
);
369 DPRINT_ERR(STORVSC_DRV
, "unable to add scsi vsc device");
370 kmem_cache_destroy(host_device_ctx
->request_pool
);
375 /* host_device_ctx->port = device_info.PortNumber; */
376 host_device_ctx
->path
= device_info
.path_id
;
377 host_device_ctx
->target
= device_info
.target_id
;
379 /* max # of devices per target */
380 host
->max_lun
= STORVSC_MAX_LUNS_PER_TARGET
;
381 /* max # of targets per channel */
382 host
->max_id
= STORVSC_MAX_TARGETS
;
383 /* max # of channels */
384 host
->max_channel
= STORVSC_MAX_CHANNELS
- 1;
386 /* Register the HBA and start the scsi bus scan */
387 ret
= scsi_add_host(host
, device
);
389 DPRINT_ERR(STORVSC_DRV
, "unable to add scsi host device");
391 storvsc_drv_obj
->base
.dev_rm(device_obj
);
393 kmem_cache_destroy(host_device_ctx
->request_pool
);
398 scsi_scan_host(host
);
403 * storvsc_remove - Callback when our device is removed
405 static int storvsc_remove(struct device
*device
)
407 struct hv_driver
*drv
=
408 drv_to_hv_drv(device
->driver
);
409 struct storvsc_driver_object
*storvsc_drv_obj
= drv
->priv
;
410 struct hv_device
*device_obj
= device_to_hv_device(device
);
411 struct Scsi_Host
*host
= dev_get_drvdata(device
);
412 struct host_device_context
*host_device_ctx
=
413 (struct host_device_context
*)host
->hostdata
;
416 * Call to the vsc driver to let it know that the device is being
419 storvsc_drv_obj
->base
.dev_rm(device_obj
);
421 if (host_device_ctx
->request_pool
) {
422 kmem_cache_destroy(host_device_ctx
->request_pool
);
423 host_device_ctx
->request_pool
= NULL
;
426 DPRINT_INFO(STORVSC
, "removing host adapter (%p)...", host
);
427 scsi_remove_host(host
);
429 DPRINT_INFO(STORVSC
, "releasing host adapter (%p)...", host
);
435 * storvsc_commmand_completion - Command completion processing
437 static void storvsc_commmand_completion(struct hv_storvsc_request
*request
)
439 struct storvsc_cmd_request
*cmd_request
=
440 (struct storvsc_cmd_request
*)request
->context
;
441 struct scsi_cmnd
*scmnd
= cmd_request
->cmd
;
442 struct host_device_context
*host_device_ctx
=
443 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
444 void (*scsi_done_fn
)(struct scsi_cmnd
*);
445 struct scsi_sense_hdr sense_hdr
;
446 struct vmscsi_request
*vm_srb
;
448 /* ASSERT(request == &cmd_request->request); */
450 /* ASSERT((unsigned long)scmnd->host_scribble == */
451 /* (unsigned long)cmd_request); */
452 /* ASSERT(scmnd->scsi_done); */
454 if (cmd_request
->bounce_sgl_count
) {
455 /* using bounce buffer */
456 /* printk("copy_from_bounce_buffer\n"); */
458 /* FIXME: We can optimize on writes by just skipping this */
459 copy_from_bounce_buffer(scsi_sglist(scmnd
),
460 cmd_request
->bounce_sgl
,
461 scsi_sg_count(scmnd
));
462 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
463 cmd_request
->bounce_sgl_count
);
466 vm_srb
= &request
->vstor_packet
.vm_srb
;
467 scmnd
->result
= vm_srb
->scsi_status
;
470 if (scsi_normalize_sense(scmnd
->sense_buffer
,
471 SCSI_SENSE_BUFFERSIZE
, &sense_hdr
))
472 scsi_print_sense_hdr("storvsc", &sense_hdr
);
475 /* ASSERT(request->BytesXfer <= request->data_buffer.Length); */
476 scsi_set_resid(scmnd
,
477 request
->data_buffer
.len
-
478 vm_srb
->data_transfer_length
);
480 scsi_done_fn
= scmnd
->scsi_done
;
482 scmnd
->host_scribble
= NULL
;
483 scmnd
->scsi_done
= NULL
;
485 /* !!DO NOT MODIFY the scmnd after this call */
488 kmem_cache_free(host_device_ctx
->request_pool
, cmd_request
);
491 static int do_bounce_buffer(struct scatterlist
*sgl
, unsigned int sg_count
)
495 /* No need to check */
499 /* We have at least 2 sg entries */
500 for (i
= 0; i
< sg_count
; i
++) {
502 /* make sure 1st one does not have hole */
503 if (sgl
[i
].offset
+ sgl
[i
].length
!= PAGE_SIZE
)
505 } else if (i
== sg_count
- 1) {
506 /* make sure last one does not have hole */
507 if (sgl
[i
].offset
!= 0)
510 /* make sure no hole in the middle */
511 if (sgl
[i
].length
!= PAGE_SIZE
|| sgl
[i
].offset
!= 0)
518 static struct scatterlist
*create_bounce_buffer(struct scatterlist
*sgl
,
519 unsigned int sg_count
,
524 struct scatterlist
*bounce_sgl
;
525 struct page
*page_buf
;
527 num_pages
= ALIGN(len
, PAGE_SIZE
) >> PAGE_SHIFT
;
529 bounce_sgl
= kcalloc(num_pages
, sizeof(struct scatterlist
), GFP_ATOMIC
);
533 for (i
= 0; i
< num_pages
; i
++) {
534 page_buf
= alloc_page(GFP_ATOMIC
);
537 sg_set_page(&bounce_sgl
[i
], page_buf
, 0, 0);
543 destroy_bounce_buffer(bounce_sgl
, num_pages
);
547 static void destroy_bounce_buffer(struct scatterlist
*sgl
,
548 unsigned int sg_count
)
551 struct page
*page_buf
;
553 for (i
= 0; i
< sg_count
; i
++) {
554 page_buf
= sg_page((&sgl
[i
]));
555 if (page_buf
!= NULL
)
556 __free_page(page_buf
);
562 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
563 static unsigned int copy_to_bounce_buffer(struct scatterlist
*orig_sgl
,
564 struct scatterlist
*bounce_sgl
,
565 unsigned int orig_sgl_count
)
569 unsigned long src
, dest
;
570 unsigned int srclen
, destlen
, copylen
;
571 unsigned int total_copied
= 0;
572 unsigned long bounce_addr
= 0;
573 unsigned long src_addr
= 0;
576 local_irq_save(flags
);
578 for (i
= 0; i
< orig_sgl_count
; i
++) {
579 src_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
580 KM_IRQ0
) + orig_sgl
[i
].offset
;
582 srclen
= orig_sgl
[i
].length
;
584 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
586 if (bounce_addr
== 0)
587 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
590 /* assume bounce offset always == 0 */
591 dest
= bounce_addr
+ bounce_sgl
[j
].length
;
592 destlen
= PAGE_SIZE
- bounce_sgl
[j
].length
;
594 copylen
= min(srclen
, destlen
);
595 memcpy((void *)dest
, (void *)src
, copylen
);
597 total_copied
+= copylen
;
598 bounce_sgl
[j
].length
+= copylen
;
602 if (bounce_sgl
[j
].length
== PAGE_SIZE
) {
603 /* full..move to next entry */
604 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
607 /* if we need to use another bounce buffer */
608 if (srclen
|| i
!= orig_sgl_count
- 1)
609 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
610 } else if (srclen
== 0 && i
== orig_sgl_count
- 1) {
611 /* unmap the last bounce that is < PAGE_SIZE */
612 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
616 kunmap_atomic((void *)(src_addr
- orig_sgl
[i
].offset
), KM_IRQ0
);
619 local_irq_restore(flags
);
624 /* Assume the original sgl has enough room */
625 static unsigned int copy_from_bounce_buffer(struct scatterlist
*orig_sgl
,
626 struct scatterlist
*bounce_sgl
,
627 unsigned int orig_sgl_count
)
631 unsigned long src
, dest
;
632 unsigned int srclen
, destlen
, copylen
;
633 unsigned int total_copied
= 0;
634 unsigned long bounce_addr
= 0;
635 unsigned long dest_addr
= 0;
638 local_irq_save(flags
);
640 for (i
= 0; i
< orig_sgl_count
; i
++) {
641 dest_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
642 KM_IRQ0
) + orig_sgl
[i
].offset
;
644 destlen
= orig_sgl
[i
].length
;
645 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
647 if (bounce_addr
== 0)
648 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
651 src
= bounce_addr
+ bounce_sgl
[j
].offset
;
652 srclen
= bounce_sgl
[j
].length
- bounce_sgl
[j
].offset
;
654 copylen
= min(srclen
, destlen
);
655 memcpy((void *)dest
, (void *)src
, copylen
);
657 total_copied
+= copylen
;
658 bounce_sgl
[j
].offset
+= copylen
;
662 if (bounce_sgl
[j
].offset
== bounce_sgl
[j
].length
) {
664 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
667 /* if we need to use another bounce buffer */
668 if (destlen
|| i
!= orig_sgl_count
- 1)
669 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
670 } else if (destlen
== 0 && i
== orig_sgl_count
- 1) {
671 /* unmap the last bounce that is < PAGE_SIZE */
672 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
676 kunmap_atomic((void *)(dest_addr
- orig_sgl
[i
].offset
),
680 local_irq_restore(flags
);
686 * storvsc_queuecommand - Initiate command processing
688 static int storvsc_queuecommand_lck(struct scsi_cmnd
*scmnd
,
689 void (*done
)(struct scsi_cmnd
*))
692 struct host_device_context
*host_device_ctx
=
693 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
694 struct hv_device
*device_ctx
= host_device_ctx
->device_ctx
;
695 struct hv_driver
*drv
=
696 drv_to_hv_drv(device_ctx
->device
.driver
);
697 struct storvsc_driver_object
*storvsc_drv_obj
= drv
->priv
;
698 struct hv_storvsc_request
*request
;
699 struct storvsc_cmd_request
*cmd_request
;
700 unsigned int request_size
= 0;
702 struct scatterlist
*sgl
;
703 unsigned int sg_count
= 0;
704 struct vmscsi_request
*vm_srb
;
706 DPRINT_DBG(STORVSC_DRV
, "scmnd %p dir %d, use_sg %d buf %p len %d "
707 "queue depth %d tagged %d", scmnd
, scmnd
->sc_data_direction
,
708 scsi_sg_count(scmnd
), scsi_sglist(scmnd
),
709 scsi_bufflen(scmnd
), scmnd
->device
->queue_depth
,
710 scmnd
->device
->tagged_supported
);
712 /* If retrying, no need to prep the cmd */
713 if (scmnd
->host_scribble
) {
714 /* ASSERT(scmnd->scsi_done != NULL); */
717 (struct storvsc_cmd_request
*)scmnd
->host_scribble
;
718 DPRINT_INFO(STORVSC_DRV
, "retrying scmnd %p cmd_request %p",
724 /* ASSERT(scmnd->scsi_done == NULL); */
725 /* ASSERT(scmnd->host_scribble == NULL); */
727 scmnd
->scsi_done
= done
;
729 request_size
= sizeof(struct storvsc_cmd_request
);
731 cmd_request
= kmem_cache_zalloc(host_device_ctx
->request_pool
,
734 DPRINT_ERR(STORVSC_DRV
, "scmnd (%p) - unable to allocate "
735 "storvsc_cmd_request...marking queue busy", scmnd
);
736 scmnd
->scsi_done
= NULL
;
737 return SCSI_MLQUEUE_DEVICE_BUSY
;
740 /* Setup the cmd request */
741 cmd_request
->bounce_sgl_count
= 0;
742 cmd_request
->bounce_sgl
= NULL
;
743 cmd_request
->cmd
= scmnd
;
745 scmnd
->host_scribble
= (unsigned char *)cmd_request
;
747 request
= &cmd_request
->request
;
748 vm_srb
= &request
->vstor_packet
.vm_srb
;
750 DPRINT_DBG(STORVSC_DRV
, "req %p size %d", request
, request_size
);
753 switch (scmnd
->sc_data_direction
) {
755 vm_srb
->data_in
= WRITE_TYPE
;
757 case DMA_FROM_DEVICE
:
758 vm_srb
->data_in
= READ_TYPE
;
761 vm_srb
->data_in
= UNKNOWN_TYPE
;
765 request
->on_io_completion
= storvsc_commmand_completion
;
766 request
->context
= cmd_request
;/* scmnd; */
768 /* request->PortId = scmnd->device->channel; */
769 vm_srb
->port_number
= host_device_ctx
->port
;
770 vm_srb
->path_id
= scmnd
->device
->channel
;
771 vm_srb
->target_id
= scmnd
->device
->id
;
772 vm_srb
->lun
= scmnd
->device
->lun
;
774 /* ASSERT(scmnd->cmd_len <= 16); */
775 vm_srb
->cdb_length
= scmnd
->cmd_len
;
777 memcpy(vm_srb
->cdb
, scmnd
->cmnd
, vm_srb
->cdb_length
);
779 request
->sense_buffer
= scmnd
->sense_buffer
;
782 request
->data_buffer
.len
= scsi_bufflen(scmnd
);
783 if (scsi_sg_count(scmnd
)) {
784 sgl
= (struct scatterlist
*)scsi_sglist(scmnd
);
785 sg_count
= scsi_sg_count(scmnd
);
787 /* check if we need to bounce the sgl */
788 if (do_bounce_buffer(sgl
, scsi_sg_count(scmnd
)) != -1) {
789 DPRINT_INFO(STORVSC_DRV
,
790 "need to bounce buffer for this scmnd %p",
792 cmd_request
->bounce_sgl
=
793 create_bounce_buffer(sgl
, scsi_sg_count(scmnd
),
794 scsi_bufflen(scmnd
));
795 if (!cmd_request
->bounce_sgl
) {
796 DPRINT_ERR(STORVSC_DRV
,
797 "unable to create bounce buffer for "
798 "this scmnd %p", scmnd
);
800 scmnd
->scsi_done
= NULL
;
801 scmnd
->host_scribble
= NULL
;
802 kmem_cache_free(host_device_ctx
->request_pool
,
805 return SCSI_MLQUEUE_HOST_BUSY
;
808 cmd_request
->bounce_sgl_count
=
809 ALIGN(scsi_bufflen(scmnd
), PAGE_SIZE
) >>
813 * FIXME: We can optimize on reads by just skipping
816 copy_to_bounce_buffer(sgl
, cmd_request
->bounce_sgl
,
817 scsi_sg_count(scmnd
));
819 sgl
= cmd_request
->bounce_sgl
;
820 sg_count
= cmd_request
->bounce_sgl_count
;
823 request
->data_buffer
.offset
= sgl
[0].offset
;
825 for (i
= 0; i
< sg_count
; i
++) {
826 DPRINT_DBG(STORVSC_DRV
, "sgl[%d] len %d offset %d\n",
827 i
, sgl
[i
].length
, sgl
[i
].offset
);
828 request
->data_buffer
.pfn_array
[i
] =
829 page_to_pfn(sg_page((&sgl
[i
])));
831 } else if (scsi_sglist(scmnd
)) {
832 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
833 request
->data_buffer
.offset
=
834 virt_to_phys(scsi_sglist(scmnd
)) & (PAGE_SIZE
-1);
835 request
->data_buffer
.pfn_array
[0] =
836 virt_to_phys(scsi_sglist(scmnd
)) >> PAGE_SHIFT
;
840 /* Invokes the vsc to start an IO */
841 ret
= storvsc_drv_obj
->on_io_request(device_ctx
,
842 &cmd_request
->request
);
845 DPRINT_ERR(STORVSC_DRV
,
846 "scmnd (%p) - queue FULL...marking queue busy",
849 if (cmd_request
->bounce_sgl_count
) {
851 * FIXME: We can optimize on writes by just skipping
854 copy_from_bounce_buffer(scsi_sglist(scmnd
),
855 cmd_request
->bounce_sgl
,
856 scsi_sg_count(scmnd
));
857 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
858 cmd_request
->bounce_sgl_count
);
861 kmem_cache_free(host_device_ctx
->request_pool
, cmd_request
);
863 scmnd
->scsi_done
= NULL
;
864 scmnd
->host_scribble
= NULL
;
866 ret
= SCSI_MLQUEUE_DEVICE_BUSY
;
872 static DEF_SCSI_QCMD(storvsc_queuecommand
)
874 static int storvsc_merge_bvec(struct request_queue
*q
,
875 struct bvec_merge_data
*bmd
, struct bio_vec
*bvec
)
877 /* checking done by caller. */
882 * storvsc_device_configure - Configure the specified scsi device
884 static int storvsc_device_alloc(struct scsi_device
*sdevice
)
886 DPRINT_DBG(STORVSC_DRV
, "sdev (%p) - setting device flag to %d",
887 sdevice
, BLIST_SPARSELUN
);
889 * This enables luns to be located sparsely. Otherwise, we may not
892 sdevice
->sdev_bflags
|= BLIST_SPARSELUN
| BLIST_LARGELUN
;
896 static int storvsc_device_configure(struct scsi_device
*sdevice
)
898 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - curr queue depth %d", sdevice
,
899 sdevice
->queue_depth
);
901 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - setting queue depth to %d",
902 sdevice
, STORVSC_MAX_IO_REQUESTS
);
903 scsi_adjust_queue_depth(sdevice
, MSG_SIMPLE_TAG
,
904 STORVSC_MAX_IO_REQUESTS
);
906 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - setting max segment size to %ld",
908 blk_queue_max_segment_size(sdevice
->request_queue
, PAGE_SIZE
);
910 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - adding merge bio vec routine",
912 blk_queue_merge_bvec(sdevice
->request_queue
, storvsc_merge_bvec
);
914 blk_queue_bounce_limit(sdevice
->request_queue
, BLK_BOUNCE_ANY
);
915 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
921 * storvsc_host_reset_handler - Reset the scsi HBA
923 static int storvsc_host_reset_handler(struct scsi_cmnd
*scmnd
)
926 struct host_device_context
*host_device_ctx
=
927 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
928 struct hv_device
*device_ctx
= host_device_ctx
->device_ctx
;
930 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) dev obj (%p) - host resetting...",
931 scmnd
->device
, device_ctx
);
933 /* Invokes the vsc to reset the host/bus */
934 ret
= stor_vsc_on_host_reset(device_ctx
);
938 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) dev obj (%p) - host reseted",
939 scmnd
->device
, device_ctx
);
944 static int storvsc_get_chs(struct scsi_device
*sdev
, struct block_device
* bdev
,
945 sector_t capacity
, int *info
)
947 sector_t total_sectors
= capacity
;
948 sector_t cylinder_times_heads
= 0;
951 int sectors_per_track
= 0;
956 if (total_sectors
> (65535 * 16 * 255))
957 total_sectors
= (65535 * 16 * 255);
959 if (total_sectors
>= (65535 * 16 * 63)) {
960 sectors_per_track
= 255;
963 cylinder_times_heads
= total_sectors
;
964 /* sector_div stores the quotient in cylinder_times_heads */
965 rem
= sector_div(cylinder_times_heads
, sectors_per_track
);
967 sectors_per_track
= 17;
969 cylinder_times_heads
= total_sectors
;
970 /* sector_div stores the quotient in cylinder_times_heads */
971 rem
= sector_div(cylinder_times_heads
, sectors_per_track
);
973 temp
= cylinder_times_heads
+ 1023;
974 /* sector_div stores the quotient in temp */
975 rem
= sector_div(temp
, 1024);
982 if (cylinder_times_heads
>= (heads
* 1024) || (heads
> 16)) {
983 sectors_per_track
= 31;
986 cylinder_times_heads
= total_sectors
;
988 * sector_div stores the quotient in
989 * cylinder_times_heads
991 rem
= sector_div(cylinder_times_heads
,
995 if (cylinder_times_heads
>= (heads
* 1024)) {
996 sectors_per_track
= 63;
999 cylinder_times_heads
= total_sectors
;
1001 * sector_div stores the quotient in
1002 * cylinder_times_heads
1004 rem
= sector_div(cylinder_times_heads
,
1009 temp
= cylinder_times_heads
;
1010 /* sector_div stores the quotient in temp */
1011 rem
= sector_div(temp
, heads
);
1015 info
[1] = sectors_per_track
;
1016 info
[2] = cylinders
;
1018 DPRINT_INFO(STORVSC_DRV
, "CHS (%d, %d, %d)", cylinders
, heads
,
1024 static int __init
storvsc_init(void)
1028 DPRINT_INFO(STORVSC_DRV
, "Storvsc initializing....");
1029 ret
= storvsc_drv_init();
1033 static void __exit
storvsc_exit(void)
1038 MODULE_LICENSE("GPL");
1039 MODULE_VERSION(HV_DRV_VERSION
);
1040 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
1041 module_init(storvsc_init
);
1042 module_exit(storvsc_exit
);