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>
20 * K. Y. Srinivasan <kys@microsoft.com>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/device.h>
26 #include <linux/blkdev.h>
27 #include <linux/dmi.h>
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_tcq.h>
33 #include <scsi/scsi_eh.h>
34 #include <scsi/scsi_devinfo.h>
35 #include <scsi/scsi_dbg.h>
38 #include "hyperv_storage.h"
40 static int storvsc_ringbuffer_size
= STORVSC_RING_BUFFER_SIZE
;
42 module_param(storvsc_ringbuffer_size
, int, S_IRUGO
);
43 MODULE_PARM_DESC(storvsc_ringbuffer_size
, "Ring buffer size (bytes)");
45 static const char *driver_name
= "storvsc";
47 struct hv_host_device
{
48 struct hv_device
*dev
;
49 struct kmem_cache
*request_pool
;
55 struct storvsc_cmd_request
{
56 struct list_head entry
;
57 struct scsi_cmnd
*cmd
;
59 unsigned int bounce_sgl_count
;
60 struct scatterlist
*bounce_sgl
;
62 struct hv_storvsc_request request
;
66 static int storvsc_device_alloc(struct scsi_device
*sdevice
)
69 * This enables luns to be located sparsely. Otherwise, we may not
72 sdevice
->sdev_bflags
|= BLIST_SPARSELUN
| BLIST_LARGELUN
;
76 static int storvsc_merge_bvec(struct request_queue
*q
,
77 struct bvec_merge_data
*bmd
, struct bio_vec
*bvec
)
79 /* checking done by caller. */
83 static int storvsc_device_configure(struct scsi_device
*sdevice
)
85 scsi_adjust_queue_depth(sdevice
, MSG_SIMPLE_TAG
,
86 STORVSC_MAX_IO_REQUESTS
);
88 blk_queue_max_segment_size(sdevice
->request_queue
, PAGE_SIZE
);
90 blk_queue_merge_bvec(sdevice
->request_queue
, storvsc_merge_bvec
);
92 blk_queue_bounce_limit(sdevice
->request_queue
, BLK_BOUNCE_ANY
);
97 static void destroy_bounce_buffer(struct scatterlist
*sgl
,
98 unsigned int sg_count
)
101 struct page
*page_buf
;
103 for (i
= 0; i
< sg_count
; i
++) {
104 page_buf
= sg_page((&sgl
[i
]));
105 if (page_buf
!= NULL
)
106 __free_page(page_buf
);
112 static int do_bounce_buffer(struct scatterlist
*sgl
, unsigned int sg_count
)
116 /* No need to check */
120 /* We have at least 2 sg entries */
121 for (i
= 0; i
< sg_count
; i
++) {
123 /* make sure 1st one does not have hole */
124 if (sgl
[i
].offset
+ sgl
[i
].length
!= PAGE_SIZE
)
126 } else if (i
== sg_count
- 1) {
127 /* make sure last one does not have hole */
128 if (sgl
[i
].offset
!= 0)
131 /* make sure no hole in the middle */
132 if (sgl
[i
].length
!= PAGE_SIZE
|| sgl
[i
].offset
!= 0)
139 static struct scatterlist
*create_bounce_buffer(struct scatterlist
*sgl
,
140 unsigned int sg_count
,
145 struct scatterlist
*bounce_sgl
;
146 struct page
*page_buf
;
148 num_pages
= ALIGN(len
, PAGE_SIZE
) >> PAGE_SHIFT
;
150 bounce_sgl
= kcalloc(num_pages
, sizeof(struct scatterlist
), GFP_ATOMIC
);
154 for (i
= 0; i
< num_pages
; i
++) {
155 page_buf
= alloc_page(GFP_ATOMIC
);
158 sg_set_page(&bounce_sgl
[i
], page_buf
, 0, 0);
164 destroy_bounce_buffer(bounce_sgl
, num_pages
);
169 /* Assume the original sgl has enough room */
170 static unsigned int copy_from_bounce_buffer(struct scatterlist
*orig_sgl
,
171 struct scatterlist
*bounce_sgl
,
172 unsigned int orig_sgl_count
)
176 unsigned long src
, dest
;
177 unsigned int srclen
, destlen
, copylen
;
178 unsigned int total_copied
= 0;
179 unsigned long bounce_addr
= 0;
180 unsigned long dest_addr
= 0;
183 local_irq_save(flags
);
185 for (i
= 0; i
< orig_sgl_count
; i
++) {
186 dest_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
187 KM_IRQ0
) + orig_sgl
[i
].offset
;
189 destlen
= orig_sgl
[i
].length
;
191 if (bounce_addr
== 0)
193 (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])),
197 src
= bounce_addr
+ bounce_sgl
[j
].offset
;
198 srclen
= bounce_sgl
[j
].length
- bounce_sgl
[j
].offset
;
200 copylen
= min(srclen
, destlen
);
201 memcpy((void *)dest
, (void *)src
, copylen
);
203 total_copied
+= copylen
;
204 bounce_sgl
[j
].offset
+= copylen
;
208 if (bounce_sgl
[j
].offset
== bounce_sgl
[j
].length
) {
210 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
213 /* if we need to use another bounce buffer */
214 if (destlen
|| i
!= orig_sgl_count
- 1)
216 (unsigned long)kmap_atomic(
217 sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
218 } else if (destlen
== 0 && i
== orig_sgl_count
- 1) {
219 /* unmap the last bounce that is < PAGE_SIZE */
220 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
224 kunmap_atomic((void *)(dest_addr
- orig_sgl
[i
].offset
),
228 local_irq_restore(flags
);
234 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
235 static unsigned int copy_to_bounce_buffer(struct scatterlist
*orig_sgl
,
236 struct scatterlist
*bounce_sgl
,
237 unsigned int orig_sgl_count
)
241 unsigned long src
, dest
;
242 unsigned int srclen
, destlen
, copylen
;
243 unsigned int total_copied
= 0;
244 unsigned long bounce_addr
= 0;
245 unsigned long src_addr
= 0;
248 local_irq_save(flags
);
250 for (i
= 0; i
< orig_sgl_count
; i
++) {
251 src_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
252 KM_IRQ0
) + orig_sgl
[i
].offset
;
254 srclen
= orig_sgl
[i
].length
;
256 if (bounce_addr
== 0)
258 (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])),
262 /* assume bounce offset always == 0 */
263 dest
= bounce_addr
+ bounce_sgl
[j
].length
;
264 destlen
= PAGE_SIZE
- bounce_sgl
[j
].length
;
266 copylen
= min(srclen
, destlen
);
267 memcpy((void *)dest
, (void *)src
, copylen
);
269 total_copied
+= copylen
;
270 bounce_sgl
[j
].length
+= copylen
;
274 if (bounce_sgl
[j
].length
== PAGE_SIZE
) {
275 /* full..move to next entry */
276 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
279 /* if we need to use another bounce buffer */
280 if (srclen
|| i
!= orig_sgl_count
- 1)
282 (unsigned long)kmap_atomic(
283 sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
285 } else if (srclen
== 0 && i
== orig_sgl_count
- 1) {
286 /* unmap the last bounce that is < PAGE_SIZE */
287 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
291 kunmap_atomic((void *)(src_addr
- orig_sgl
[i
].offset
), KM_IRQ0
);
294 local_irq_restore(flags
);
300 static int storvsc_remove(struct hv_device
*dev
)
302 struct Scsi_Host
*host
= dev_get_drvdata(&dev
->device
);
303 struct hv_host_device
*host_dev
=
304 (struct hv_host_device
*)host
->hostdata
;
306 scsi_remove_host(host
);
310 storvsc_dev_remove(dev
);
311 if (host_dev
->request_pool
) {
312 kmem_cache_destroy(host_dev
->request_pool
);
313 host_dev
->request_pool
= NULL
;
319 static int storvsc_get_chs(struct scsi_device
*sdev
, struct block_device
* bdev
,
320 sector_t capacity
, int *info
)
322 sector_t nsect
= capacity
;
323 sector_t cylinders
= nsect
;
324 int heads
, sectors_pt
;
327 * We are making up these values; let us keep it simple.
330 sectors_pt
= 0x3f; /* Sectors per track */
331 sector_div(cylinders
, heads
* sectors_pt
);
332 if ((sector_t
)(cylinders
+ 1) * heads
* sectors_pt
< nsect
)
336 info
[1] = sectors_pt
;
337 info
[2] = (int)cylinders
;
342 static int storvsc_host_reset(struct hv_device
*device
)
344 struct storvsc_device
*stor_device
;
345 struct hv_storvsc_request
*request
;
346 struct vstor_packet
*vstor_packet
;
350 stor_device
= get_stor_device(device
);
354 request
= &stor_device
->reset_request
;
355 vstor_packet
= &request
->vstor_packet
;
357 init_completion(&request
->wait_event
);
359 vstor_packet
->operation
= VSTOR_OPERATION_RESET_BUS
;
360 vstor_packet
->flags
= REQUEST_COMPLETION_FLAG
;
361 vstor_packet
->vm_srb
.path_id
= stor_device
->path_id
;
363 ret
= vmbus_sendpacket(device
->channel
, vstor_packet
,
364 sizeof(struct vstor_packet
),
365 (unsigned long)&stor_device
->reset_request
,
367 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
371 t
= wait_for_completion_timeout(&request
->wait_event
, 5*HZ
);
379 * At this point, all outstanding requests in the adapter
380 * should have been flushed out and return to us
384 put_stor_device(device
);
390 * storvsc_host_reset_handler - Reset the scsi HBA
392 static int storvsc_host_reset_handler(struct scsi_cmnd
*scmnd
)
395 struct hv_host_device
*host_dev
=
396 (struct hv_host_device
*)scmnd
->device
->host
->hostdata
;
397 struct hv_device
*dev
= host_dev
->dev
;
399 ret
= storvsc_host_reset(dev
);
408 * storvsc_commmand_completion - Command completion processing
410 static void storvsc_commmand_completion(struct hv_storvsc_request
*request
)
412 struct storvsc_cmd_request
*cmd_request
=
413 (struct storvsc_cmd_request
*)request
->context
;
414 struct scsi_cmnd
*scmnd
= cmd_request
->cmd
;
415 struct hv_host_device
*host_dev
=
416 (struct hv_host_device
*)scmnd
->device
->host
->hostdata
;
417 void (*scsi_done_fn
)(struct scsi_cmnd
*);
418 struct scsi_sense_hdr sense_hdr
;
419 struct vmscsi_request
*vm_srb
;
421 if (cmd_request
->bounce_sgl_count
) {
423 /* FIXME: We can optimize on writes by just skipping this */
424 copy_from_bounce_buffer(scsi_sglist(scmnd
),
425 cmd_request
->bounce_sgl
,
426 scsi_sg_count(scmnd
));
427 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
428 cmd_request
->bounce_sgl_count
);
431 vm_srb
= &request
->vstor_packet
.vm_srb
;
432 scmnd
->result
= vm_srb
->scsi_status
;
435 if (scsi_normalize_sense(scmnd
->sense_buffer
,
436 SCSI_SENSE_BUFFERSIZE
, &sense_hdr
))
437 scsi_print_sense_hdr("storvsc", &sense_hdr
);
440 scsi_set_resid(scmnd
,
441 request
->data_buffer
.len
-
442 vm_srb
->data_transfer_length
);
444 scsi_done_fn
= scmnd
->scsi_done
;
446 scmnd
->host_scribble
= NULL
;
447 scmnd
->scsi_done
= NULL
;
451 kmem_cache_free(host_dev
->request_pool
, cmd_request
);
456 * storvsc_queuecommand - Initiate command processing
458 static int storvsc_queuecommand_lck(struct scsi_cmnd
*scmnd
,
459 void (*done
)(struct scsi_cmnd
*))
462 struct hv_host_device
*host_dev
=
463 (struct hv_host_device
*)scmnd
->device
->host
->hostdata
;
464 struct hv_device
*dev
= host_dev
->dev
;
465 struct hv_storvsc_request
*request
;
466 struct storvsc_cmd_request
*cmd_request
;
467 unsigned int request_size
= 0;
469 struct scatterlist
*sgl
;
470 unsigned int sg_count
= 0;
471 struct vmscsi_request
*vm_srb
;
474 /* If retrying, no need to prep the cmd */
475 if (scmnd
->host_scribble
) {
478 (struct storvsc_cmd_request
*)scmnd
->host_scribble
;
483 scmnd
->scsi_done
= done
;
485 request_size
= sizeof(struct storvsc_cmd_request
);
487 cmd_request
= kmem_cache_zalloc(host_dev
->request_pool
,
490 scmnd
->scsi_done
= NULL
;
491 return SCSI_MLQUEUE_DEVICE_BUSY
;
494 /* Setup the cmd request */
495 cmd_request
->bounce_sgl_count
= 0;
496 cmd_request
->bounce_sgl
= NULL
;
497 cmd_request
->cmd
= scmnd
;
499 scmnd
->host_scribble
= (unsigned char *)cmd_request
;
501 request
= &cmd_request
->request
;
502 vm_srb
= &request
->vstor_packet
.vm_srb
;
506 switch (scmnd
->sc_data_direction
) {
508 vm_srb
->data_in
= WRITE_TYPE
;
510 case DMA_FROM_DEVICE
:
511 vm_srb
->data_in
= READ_TYPE
;
514 vm_srb
->data_in
= UNKNOWN_TYPE
;
518 request
->on_io_completion
= storvsc_commmand_completion
;
519 request
->context
= cmd_request
;/* scmnd; */
521 vm_srb
->port_number
= host_dev
->port
;
522 vm_srb
->path_id
= scmnd
->device
->channel
;
523 vm_srb
->target_id
= scmnd
->device
->id
;
524 vm_srb
->lun
= scmnd
->device
->lun
;
526 vm_srb
->cdb_length
= scmnd
->cmd_len
;
528 memcpy(vm_srb
->cdb
, scmnd
->cmnd
, vm_srb
->cdb_length
);
530 request
->sense_buffer
= scmnd
->sense_buffer
;
533 request
->data_buffer
.len
= scsi_bufflen(scmnd
);
534 if (scsi_sg_count(scmnd
)) {
535 sgl
= (struct scatterlist
*)scsi_sglist(scmnd
);
536 sg_count
= scsi_sg_count(scmnd
);
538 /* check if we need to bounce the sgl */
539 if (do_bounce_buffer(sgl
, scsi_sg_count(scmnd
)) != -1) {
540 cmd_request
->bounce_sgl
=
541 create_bounce_buffer(sgl
, scsi_sg_count(scmnd
),
542 scsi_bufflen(scmnd
));
543 if (!cmd_request
->bounce_sgl
) {
544 scmnd
->scsi_done
= NULL
;
545 scmnd
->host_scribble
= NULL
;
546 kmem_cache_free(host_dev
->request_pool
,
549 return SCSI_MLQUEUE_HOST_BUSY
;
552 cmd_request
->bounce_sgl_count
=
553 ALIGN(scsi_bufflen(scmnd
), PAGE_SIZE
) >>
557 * FIXME: We can optimize on reads by just skipping
560 copy_to_bounce_buffer(sgl
, cmd_request
->bounce_sgl
,
561 scsi_sg_count(scmnd
));
563 sgl
= cmd_request
->bounce_sgl
;
564 sg_count
= cmd_request
->bounce_sgl_count
;
567 request
->data_buffer
.offset
= sgl
[0].offset
;
569 for (i
= 0; i
< sg_count
; i
++)
570 request
->data_buffer
.pfn_array
[i
] =
571 page_to_pfn(sg_page((&sgl
[i
])));
573 } else if (scsi_sglist(scmnd
)) {
574 request
->data_buffer
.offset
=
575 virt_to_phys(scsi_sglist(scmnd
)) & (PAGE_SIZE
-1);
576 request
->data_buffer
.pfn_array
[0] =
577 virt_to_phys(scsi_sglist(scmnd
)) >> PAGE_SHIFT
;
581 /* Invokes the vsc to start an IO */
582 ret
= storvsc_do_io(dev
, &cmd_request
->request
);
587 if (cmd_request
->bounce_sgl_count
) {
589 * FIXME: We can optimize on writes by just skipping
592 copy_from_bounce_buffer(scsi_sglist(scmnd
),
593 cmd_request
->bounce_sgl
,
594 scsi_sg_count(scmnd
));
595 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
596 cmd_request
->bounce_sgl_count
);
599 kmem_cache_free(host_dev
->request_pool
, cmd_request
);
601 scmnd
->scsi_done
= NULL
;
602 scmnd
->host_scribble
= NULL
;
604 ret
= SCSI_MLQUEUE_DEVICE_BUSY
;
610 static DEF_SCSI_QCMD(storvsc_queuecommand
)
614 static struct scsi_host_template scsi_driver
= {
615 .module
= THIS_MODULE
,
616 .name
= "storvsc_host_t",
617 .bios_param
= storvsc_get_chs
,
618 .queuecommand
= storvsc_queuecommand
,
619 .eh_host_reset_handler
= storvsc_host_reset_handler
,
620 .slave_alloc
= storvsc_device_alloc
,
621 .slave_configure
= storvsc_device_configure
,
623 /* 64 max_queue * 1 target */
624 .can_queue
= STORVSC_MAX_IO_REQUESTS
*STORVSC_MAX_TARGETS
,
626 /* no use setting to 0 since ll_blk_rw reset it to 1 */
628 .sg_tablesize
= MAX_MULTIPAGE_BUFFER_COUNT
,
630 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
631 * into 1 sg element. If set, we must limit the max_segment_size to
632 * PAGE_SIZE, otherwise we may get 1 sg element that represents
635 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
636 .use_clustering
= ENABLE_CLUSTERING
,
637 /* Make sure we dont get a sg segment crosses a page boundary */
638 .dma_boundary
= PAGE_SIZE
-1,
641 static const struct hv_vmbus_device_id id_table
[] = {
643 { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
644 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f) },
648 MODULE_DEVICE_TABLE(vmbus
, id_table
);
650 * storvsc_probe - Add a new device for this driver
653 static int storvsc_probe(struct hv_device
*device
)
656 struct Scsi_Host
*host
;
657 struct hv_host_device
*host_dev
;
658 struct storvsc_device_info device_info
;
660 host
= scsi_host_alloc(&scsi_driver
,
661 sizeof(struct hv_host_device
));
665 dev_set_drvdata(&device
->device
, host
);
667 host_dev
= (struct hv_host_device
*)host
->hostdata
;
668 memset(host_dev
, 0, sizeof(struct hv_host_device
));
670 host_dev
->port
= host
->host_no
;
671 host_dev
->dev
= device
;
673 host_dev
->request_pool
=
674 kmem_cache_create(dev_name(&device
->device
),
675 sizeof(struct storvsc_cmd_request
), 0,
676 SLAB_HWCACHE_ALIGN
, NULL
);
678 if (!host_dev
->request_pool
) {
683 device_info
.port_number
= host
->host_no
;
684 device_info
.ring_buffer_size
= storvsc_ringbuffer_size
;
685 /* Call to the vsc driver to add the device */
686 ret
= storvsc_dev_add(device
, (void *)&device_info
);
689 kmem_cache_destroy(host_dev
->request_pool
);
694 host_dev
->path
= device_info
.path_id
;
695 host_dev
->target
= device_info
.target_id
;
697 /* max # of devices per target */
698 host
->max_lun
= STORVSC_MAX_LUNS_PER_TARGET
;
699 /* max # of targets per channel */
700 host
->max_id
= STORVSC_MAX_TARGETS
;
701 /* max # of channels */
702 host
->max_channel
= STORVSC_MAX_CHANNELS
- 1;
704 /* Register the HBA and start the scsi bus scan */
705 ret
= scsi_add_host(host
, &device
->device
);
708 storvsc_dev_remove(device
);
710 kmem_cache_destroy(host_dev
->request_pool
);
715 scsi_scan_host(host
);
719 /* The one and only one */
721 static struct hv_driver storvsc_drv
= {
722 .id_table
= id_table
,
723 .probe
= storvsc_probe
,
724 .remove
= storvsc_remove
,
728 * We use a DMI table to determine if we should autoload this driver This is
729 * needed by distro tools to determine if the hyperv drivers should be
730 * installed and/or configured. We don't do anything else with the table, but
731 * it needs to be present.
734 static const struct dmi_system_id __initconst
735 hv_stor_dmi_table
[] __maybe_unused
= {
739 DMI_MATCH(DMI_SYS_VENDOR
, "Microsoft Corporation"),
740 DMI_MATCH(DMI_PRODUCT_NAME
, "Virtual Machine"),
741 DMI_MATCH(DMI_BOARD_NAME
, "Virtual Machine"),
746 MODULE_DEVICE_TABLE(dmi
, hv_stor_dmi_table
);
748 static int __init
storvsc_drv_init(void)
751 struct hv_driver
*drv
= &storvsc_drv
;
752 u32 max_outstanding_req_per_channel
;
755 * Divide the ring buffer data size (which is 1 page less
756 * than the ring buffer size since that page is reserved for
757 * the ring buffer indices) by the max request size (which is
758 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
761 max_outstanding_req_per_channel
=
762 ((storvsc_ringbuffer_size
- PAGE_SIZE
) /
763 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET
+
764 sizeof(struct vstor_packet
) + sizeof(u64
),
767 if (max_outstanding_req_per_channel
<
768 STORVSC_MAX_IO_REQUESTS
)
771 drv
->driver
.name
= driver_name
;
774 /* The driver belongs to vmbus */
775 ret
= vmbus_child_driver_register(&drv
->driver
);
780 static void __exit
storvsc_drv_exit(void)
782 vmbus_child_driver_unregister(&storvsc_drv
.driver
);
785 MODULE_LICENSE("GPL");
786 MODULE_VERSION(HV_DRV_VERSION
);
787 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
788 module_init(storvsc_drv_init
);
789 module_exit(storvsc_drv_exit
);