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>
23 #include <linux/kernel.h>
24 #include <linux/wait.h>
25 #include <linux/sched.h>
26 #include <linux/completion.h>
27 #include <linux/string.h>
29 #include <linux/delay.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/module.h>
33 #include <linux/device.h>
34 #include <linux/hyperv.h>
35 #include <linux/mempool.h>
36 #include <linux/blkdev.h>
37 #include <scsi/scsi.h>
38 #include <scsi/scsi_cmnd.h>
39 #include <scsi/scsi_host.h>
40 #include <scsi/scsi_device.h>
41 #include <scsi/scsi_tcq.h>
42 #include <scsi/scsi_eh.h>
43 #include <scsi/scsi_devinfo.h>
44 #include <scsi/scsi_dbg.h>
47 * All wire protocol details (storage protocol between the guest and the host)
48 * are consolidated here.
50 * Begin protocol definitions.
56 * V1 RC < 2008/1/31: 1.0
57 * V1 RC > 2008/1/31: 2.0
61 #define VMSTOR_CURRENT_MAJOR 4
62 #define VMSTOR_CURRENT_MINOR 2
65 /* Packet structure describing virtual storage requests. */
66 enum vstor_packet_operation
{
67 VSTOR_OPERATION_COMPLETE_IO
= 1,
68 VSTOR_OPERATION_REMOVE_DEVICE
= 2,
69 VSTOR_OPERATION_EXECUTE_SRB
= 3,
70 VSTOR_OPERATION_RESET_LUN
= 4,
71 VSTOR_OPERATION_RESET_ADAPTER
= 5,
72 VSTOR_OPERATION_RESET_BUS
= 6,
73 VSTOR_OPERATION_BEGIN_INITIALIZATION
= 7,
74 VSTOR_OPERATION_END_INITIALIZATION
= 8,
75 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION
= 9,
76 VSTOR_OPERATION_QUERY_PROPERTIES
= 10,
77 VSTOR_OPERATION_ENUMERATE_BUS
= 11,
78 VSTOR_OPERATION_MAXIMUM
= 11
82 * Platform neutral description of a scsi request -
83 * this remains the same across the write regardless of 32/64 bit
84 * note: it's patterned off the SCSI_PASS_THROUGH structure
86 #define STORVSC_MAX_CMD_LEN 0x10
87 #define STORVSC_SENSE_BUFFER_SIZE 0x12
88 #define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
90 struct vmscsi_request
{
101 u8 sense_info_length
;
105 u32 data_transfer_length
;
108 u8 cdb
[STORVSC_MAX_CMD_LEN
];
109 u8 sense_data
[STORVSC_SENSE_BUFFER_SIZE
];
110 u8 reserved_array
[STORVSC_MAX_BUF_LEN_WITH_PADDING
];
112 } __attribute((packed
));
116 * This structure is sent during the intialization phase to get the different
117 * properties of the channel.
119 struct vmstorage_channel_properties
{
120 u16 protocol_version
;
124 /* Note: port number is only really known on the client side */
127 u32 max_transfer_bytes
;
130 * This id is unique for each channel and will correspond with
131 * vendor specific data in the inquiry data.
137 /* This structure is sent during the storage protocol negotiations. */
138 struct vmstorage_protocol_version
{
139 /* Major (MSW) and minor (LSW) version numbers. */
143 * Revision number is auto-incremented whenever this file is changed
144 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
145 * definitely indicate incompatibility--but it does indicate mismatched
147 * This is only used on the windows side. Just set it to 0.
152 /* Channel Property Flags */
153 #define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
154 #define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
156 struct vstor_packet
{
157 /* Requested operation type */
158 enum vstor_packet_operation operation
;
160 /* Flags - see below for values */
163 /* Status of the request returned from the server side. */
166 /* Data payload area */
169 * Structure used to forward SCSI commands from the
170 * client to the server.
172 struct vmscsi_request vm_srb
;
174 /* Structure used to query channel properties. */
175 struct vmstorage_channel_properties storage_channel_properties
;
177 /* Used during version negotiations. */
178 struct vmstorage_protocol_version version
;
185 * This flag indicates that the server should send back a completion for this
189 #define REQUEST_COMPLETION_FLAG 0x1
191 /* Matches Windows-end */
192 enum storvsc_request_type
{
199 * SRB status codes and masks; a subset of the codes used here.
202 #define SRB_STATUS_AUTOSENSE_VALID 0x80
203 #define SRB_STATUS_INVALID_LUN 0x20
204 #define SRB_STATUS_SUCCESS 0x01
205 #define SRB_STATUS_ABORTED 0x02
206 #define SRB_STATUS_ERROR 0x04
207 #define SRB_STATUS_DATA_OVERRUN 0x12
210 * This is the end of Protocol specific defines.
215 * We setup a mempool to allocate request structures for this driver
216 * on a per-lun basis. The following define specifies the number of
217 * elements in the pool.
220 #define STORVSC_MIN_BUF_NR 64
221 static int storvsc_ringbuffer_size
= (20 * PAGE_SIZE
);
223 module_param(storvsc_ringbuffer_size
, int, S_IRUGO
);
224 MODULE_PARM_DESC(storvsc_ringbuffer_size
, "Ring buffer size (bytes)");
226 #define STORVSC_MAX_IO_REQUESTS 128
229 * In Hyper-V, each port/path/target maps to 1 scsi host adapter. In
230 * reality, the path/target is not used (ie always set to 0) so our
231 * scsi host adapter essentially has 1 bus with 1 target that contains
234 #define STORVSC_MAX_LUNS_PER_TARGET 64
235 #define STORVSC_MAX_TARGETS 1
236 #define STORVSC_MAX_CHANNELS 1
240 struct storvsc_cmd_request
{
241 struct list_head entry
;
242 struct scsi_cmnd
*cmd
;
244 unsigned int bounce_sgl_count
;
245 struct scatterlist
*bounce_sgl
;
247 struct hv_device
*device
;
249 /* Synchronize the request/response if needed */
250 struct completion wait_event
;
252 unsigned char *sense_buffer
;
253 struct hv_multipage_buffer data_buffer
;
254 struct vstor_packet vstor_packet
;
258 /* A storvsc device is a device object that contains a vmbus channel */
259 struct storvsc_device
{
260 struct hv_device
*device
;
264 atomic_t num_outstanding_req
;
265 struct Scsi_Host
*host
;
267 wait_queue_head_t waiting_to_drain
;
270 * Each unique Port/Path/Target represents 1 channel ie scsi
271 * controller. In reality, the pathid, targetid is always 0
272 * and the port is set by us
274 unsigned int port_number
;
275 unsigned char path_id
;
276 unsigned char target_id
;
278 /* Used for vsc/vsp channel reset process */
279 struct storvsc_cmd_request init_request
;
280 struct storvsc_cmd_request reset_request
;
283 struct stor_mem_pools
{
284 struct kmem_cache
*request_pool
;
285 mempool_t
*request_mempool
;
288 struct hv_host_device
{
289 struct hv_device
*dev
;
292 unsigned char target
;
295 struct storvsc_scan_work
{
296 struct work_struct work
;
297 struct Scsi_Host
*host
;
301 static void storvsc_device_scan(struct work_struct
*work
)
303 struct storvsc_scan_work
*wrk
;
305 struct scsi_device
*sdev
;
307 wrk
= container_of(work
, struct storvsc_scan_work
, work
);
310 sdev
= scsi_device_lookup(wrk
->host
, 0, 0, lun
);
313 scsi_rescan_device(&sdev
->sdev_gendev
);
314 scsi_device_put(sdev
);
320 static void storvsc_bus_scan(struct work_struct
*work
)
322 struct storvsc_scan_work
*wrk
;
325 wrk
= container_of(work
, struct storvsc_scan_work
, work
);
326 for (id
= 0; id
< wrk
->host
->max_id
; ++id
) {
327 if (wrk
->host
->reverse_ordering
)
328 order_id
= wrk
->host
->max_id
- id
- 1;
332 scsi_scan_target(&wrk
->host
->shost_gendev
, 0,
333 order_id
, SCAN_WILD_CARD
, 1);
338 static void storvsc_remove_lun(struct work_struct
*work
)
340 struct storvsc_scan_work
*wrk
;
341 struct scsi_device
*sdev
;
343 wrk
= container_of(work
, struct storvsc_scan_work
, work
);
344 if (!scsi_host_get(wrk
->host
))
347 sdev
= scsi_device_lookup(wrk
->host
, 0, 0, wrk
->lun
);
350 scsi_remove_device(sdev
);
351 scsi_device_put(sdev
);
353 scsi_host_put(wrk
->host
);
360 * Major/minor macros. Minor version is in LSB, meaning that earlier flat
361 * version numbers will be interpreted as "0.x" (i.e., 1 becomes 0.1).
364 static inline u16
storvsc_get_version(u8 major
, u8 minor
)
368 version
= ((major
<< 8) | minor
);
373 * We can get incoming messages from the host that are not in response to
374 * messages that we have sent out. An example of this would be messages
375 * received by the guest to notify dynamic addition/removal of LUNs. To
376 * deal with potential race conditions where the driver may be in the
377 * midst of being unloaded when we might receive an unsolicited message
378 * from the host, we have implemented a mechanism to gurantee sequential
381 * 1) Once the device is marked as being destroyed, we will fail all
383 * 2) We permit incoming messages when the device is being destroyed,
384 * only to properly account for messages already sent out.
387 static inline struct storvsc_device
*get_out_stor_device(
388 struct hv_device
*device
)
390 struct storvsc_device
*stor_device
;
392 stor_device
= hv_get_drvdata(device
);
394 if (stor_device
&& stor_device
->destroy
)
401 static inline void storvsc_wait_to_drain(struct storvsc_device
*dev
)
403 dev
->drain_notify
= true;
404 wait_event(dev
->waiting_to_drain
,
405 atomic_read(&dev
->num_outstanding_req
) == 0);
406 dev
->drain_notify
= false;
409 static inline struct storvsc_device
*get_in_stor_device(
410 struct hv_device
*device
)
412 struct storvsc_device
*stor_device
;
414 stor_device
= hv_get_drvdata(device
);
420 * If the device is being destroyed; allow incoming
421 * traffic only to cleanup outstanding requests.
424 if (stor_device
->destroy
&&
425 (atomic_read(&stor_device
->num_outstanding_req
) == 0))
433 static void destroy_bounce_buffer(struct scatterlist
*sgl
,
434 unsigned int sg_count
)
437 struct page
*page_buf
;
439 for (i
= 0; i
< sg_count
; i
++) {
440 page_buf
= sg_page((&sgl
[i
]));
441 if (page_buf
!= NULL
)
442 __free_page(page_buf
);
448 static int do_bounce_buffer(struct scatterlist
*sgl
, unsigned int sg_count
)
452 /* No need to check */
456 /* We have at least 2 sg entries */
457 for (i
= 0; i
< sg_count
; i
++) {
459 /* make sure 1st one does not have hole */
460 if (sgl
[i
].offset
+ sgl
[i
].length
!= PAGE_SIZE
)
462 } else if (i
== sg_count
- 1) {
463 /* make sure last one does not have hole */
464 if (sgl
[i
].offset
!= 0)
467 /* make sure no hole in the middle */
468 if (sgl
[i
].length
!= PAGE_SIZE
|| sgl
[i
].offset
!= 0)
475 static struct scatterlist
*create_bounce_buffer(struct scatterlist
*sgl
,
476 unsigned int sg_count
,
482 struct scatterlist
*bounce_sgl
;
483 struct page
*page_buf
;
484 unsigned int buf_len
= ((write
== WRITE_TYPE
) ? 0 : PAGE_SIZE
);
486 num_pages
= ALIGN(len
, PAGE_SIZE
) >> PAGE_SHIFT
;
488 bounce_sgl
= kcalloc(num_pages
, sizeof(struct scatterlist
), GFP_ATOMIC
);
492 sg_init_table(bounce_sgl
, num_pages
);
493 for (i
= 0; i
< num_pages
; i
++) {
494 page_buf
= alloc_page(GFP_ATOMIC
);
497 sg_set_page(&bounce_sgl
[i
], page_buf
, buf_len
, 0);
503 destroy_bounce_buffer(bounce_sgl
, num_pages
);
507 /* Disgusting wrapper functions */
508 static inline unsigned long sg_kmap_atomic(struct scatterlist
*sgl
, int idx
)
510 void *addr
= kmap_atomic(sg_page(sgl
+ idx
));
511 return (unsigned long)addr
;
514 static inline void sg_kunmap_atomic(unsigned long addr
)
516 kunmap_atomic((void *)addr
);
520 /* Assume the original sgl has enough room */
521 static unsigned int copy_from_bounce_buffer(struct scatterlist
*orig_sgl
,
522 struct scatterlist
*bounce_sgl
,
523 unsigned int orig_sgl_count
,
524 unsigned int bounce_sgl_count
)
528 unsigned long src
, dest
;
529 unsigned int srclen
, destlen
, copylen
;
530 unsigned int total_copied
= 0;
531 unsigned long bounce_addr
= 0;
532 unsigned long dest_addr
= 0;
535 local_irq_save(flags
);
537 for (i
= 0; i
< orig_sgl_count
; i
++) {
538 dest_addr
= sg_kmap_atomic(orig_sgl
,i
) + orig_sgl
[i
].offset
;
540 destlen
= orig_sgl
[i
].length
;
542 if (bounce_addr
== 0)
543 bounce_addr
= sg_kmap_atomic(bounce_sgl
,j
);
546 src
= bounce_addr
+ bounce_sgl
[j
].offset
;
547 srclen
= bounce_sgl
[j
].length
- bounce_sgl
[j
].offset
;
549 copylen
= min(srclen
, destlen
);
550 memcpy((void *)dest
, (void *)src
, copylen
);
552 total_copied
+= copylen
;
553 bounce_sgl
[j
].offset
+= copylen
;
557 if (bounce_sgl
[j
].offset
== bounce_sgl
[j
].length
) {
559 sg_kunmap_atomic(bounce_addr
);
563 * It is possible that the number of elements
564 * in the bounce buffer may not be equal to
565 * the number of elements in the original
566 * scatter list. Handle this correctly.
569 if (j
== bounce_sgl_count
) {
571 * We are done; cleanup and return.
573 sg_kunmap_atomic(dest_addr
- orig_sgl
[i
].offset
);
574 local_irq_restore(flags
);
578 /* if we need to use another bounce buffer */
579 if (destlen
|| i
!= orig_sgl_count
- 1)
580 bounce_addr
= sg_kmap_atomic(bounce_sgl
,j
);
581 } else if (destlen
== 0 && i
== orig_sgl_count
- 1) {
582 /* unmap the last bounce that is < PAGE_SIZE */
583 sg_kunmap_atomic(bounce_addr
);
587 sg_kunmap_atomic(dest_addr
- orig_sgl
[i
].offset
);
590 local_irq_restore(flags
);
595 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
596 static unsigned int copy_to_bounce_buffer(struct scatterlist
*orig_sgl
,
597 struct scatterlist
*bounce_sgl
,
598 unsigned int orig_sgl_count
)
602 unsigned long src
, dest
;
603 unsigned int srclen
, destlen
, copylen
;
604 unsigned int total_copied
= 0;
605 unsigned long bounce_addr
= 0;
606 unsigned long src_addr
= 0;
609 local_irq_save(flags
);
611 for (i
= 0; i
< orig_sgl_count
; i
++) {
612 src_addr
= sg_kmap_atomic(orig_sgl
,i
) + orig_sgl
[i
].offset
;
614 srclen
= orig_sgl
[i
].length
;
616 if (bounce_addr
== 0)
617 bounce_addr
= sg_kmap_atomic(bounce_sgl
,j
);
620 /* assume bounce offset always == 0 */
621 dest
= bounce_addr
+ bounce_sgl
[j
].length
;
622 destlen
= PAGE_SIZE
- bounce_sgl
[j
].length
;
624 copylen
= min(srclen
, destlen
);
625 memcpy((void *)dest
, (void *)src
, copylen
);
627 total_copied
+= copylen
;
628 bounce_sgl
[j
].length
+= copylen
;
632 if (bounce_sgl
[j
].length
== PAGE_SIZE
) {
633 /* full..move to next entry */
634 sg_kunmap_atomic(bounce_addr
);
639 /* if we need to use another bounce buffer */
640 if (srclen
&& bounce_addr
== 0)
641 bounce_addr
= sg_kmap_atomic(bounce_sgl
, j
);
645 sg_kunmap_atomic(src_addr
- orig_sgl
[i
].offset
);
649 sg_kunmap_atomic(bounce_addr
);
651 local_irq_restore(flags
);
656 static int storvsc_channel_init(struct hv_device
*device
)
658 struct storvsc_device
*stor_device
;
659 struct storvsc_cmd_request
*request
;
660 struct vstor_packet
*vstor_packet
;
663 stor_device
= get_out_stor_device(device
);
667 request
= &stor_device
->init_request
;
668 vstor_packet
= &request
->vstor_packet
;
671 * Now, initiate the vsc/vsp initialization protocol on the open
674 memset(request
, 0, sizeof(struct storvsc_cmd_request
));
675 init_completion(&request
->wait_event
);
676 vstor_packet
->operation
= VSTOR_OPERATION_BEGIN_INITIALIZATION
;
677 vstor_packet
->flags
= REQUEST_COMPLETION_FLAG
;
679 ret
= vmbus_sendpacket(device
->channel
, vstor_packet
,
680 sizeof(struct vstor_packet
),
681 (unsigned long)request
,
683 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
687 t
= wait_for_completion_timeout(&request
->wait_event
, 5*HZ
);
693 if (vstor_packet
->operation
!= VSTOR_OPERATION_COMPLETE_IO
||
694 vstor_packet
->status
!= 0)
698 /* reuse the packet for version range supported */
699 memset(vstor_packet
, 0, sizeof(struct vstor_packet
));
700 vstor_packet
->operation
= VSTOR_OPERATION_QUERY_PROTOCOL_VERSION
;
701 vstor_packet
->flags
= REQUEST_COMPLETION_FLAG
;
703 vstor_packet
->version
.major_minor
=
704 storvsc_get_version(VMSTOR_CURRENT_MAJOR
, VMSTOR_CURRENT_MINOR
);
707 * The revision number is only used in Windows; set it to 0.
709 vstor_packet
->version
.revision
= 0;
711 ret
= vmbus_sendpacket(device
->channel
, vstor_packet
,
712 sizeof(struct vstor_packet
),
713 (unsigned long)request
,
715 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
719 t
= wait_for_completion_timeout(&request
->wait_event
, 5*HZ
);
725 if (vstor_packet
->operation
!= VSTOR_OPERATION_COMPLETE_IO
||
726 vstor_packet
->status
!= 0)
730 memset(vstor_packet
, 0, sizeof(struct vstor_packet
));
731 vstor_packet
->operation
= VSTOR_OPERATION_QUERY_PROPERTIES
;
732 vstor_packet
->flags
= REQUEST_COMPLETION_FLAG
;
733 vstor_packet
->storage_channel_properties
.port_number
=
734 stor_device
->port_number
;
736 ret
= vmbus_sendpacket(device
->channel
, vstor_packet
,
737 sizeof(struct vstor_packet
),
738 (unsigned long)request
,
740 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
745 t
= wait_for_completion_timeout(&request
->wait_event
, 5*HZ
);
751 if (vstor_packet
->operation
!= VSTOR_OPERATION_COMPLETE_IO
||
752 vstor_packet
->status
!= 0)
755 stor_device
->path_id
= vstor_packet
->storage_channel_properties
.path_id
;
756 stor_device
->target_id
757 = vstor_packet
->storage_channel_properties
.target_id
;
759 memset(vstor_packet
, 0, sizeof(struct vstor_packet
));
760 vstor_packet
->operation
= VSTOR_OPERATION_END_INITIALIZATION
;
761 vstor_packet
->flags
= REQUEST_COMPLETION_FLAG
;
763 ret
= vmbus_sendpacket(device
->channel
, vstor_packet
,
764 sizeof(struct vstor_packet
),
765 (unsigned long)request
,
767 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
772 t
= wait_for_completion_timeout(&request
->wait_event
, 5*HZ
);
778 if (vstor_packet
->operation
!= VSTOR_OPERATION_COMPLETE_IO
||
779 vstor_packet
->status
!= 0)
787 static void storvsc_handle_error(struct vmscsi_request
*vm_srb
,
788 struct scsi_cmnd
*scmnd
,
789 struct Scsi_Host
*host
,
792 struct storvsc_scan_work
*wrk
;
793 void (*process_err_fn
)(struct work_struct
*work
);
794 bool do_work
= false;
796 switch (vm_srb
->srb_status
) {
797 case SRB_STATUS_ERROR
:
799 * Let upper layer deal with error when
800 * sense message is present.
803 if (vm_srb
->srb_status
& SRB_STATUS_AUTOSENSE_VALID
)
806 * If there is an error; offline the device since all
807 * error recovery strategies would have already been
808 * deployed on the host side. However, if the command
809 * were a pass-through command deal with it appropriately.
811 switch (scmnd
->cmnd
[0]) {
814 set_host_byte(scmnd
, DID_PASSTHROUGH
);
817 * On Some Windows hosts TEST_UNIT_READY command can return
818 * SRB_STATUS_ERROR, let the upper level code deal with it
819 * based on the sense information.
821 case TEST_UNIT_READY
:
824 set_host_byte(scmnd
, DID_TARGET_FAILURE
);
827 case SRB_STATUS_INVALID_LUN
:
829 process_err_fn
= storvsc_remove_lun
;
831 case (SRB_STATUS_ABORTED
| SRB_STATUS_AUTOSENSE_VALID
):
832 if ((asc
== 0x2a) && (ascq
== 0x9)) {
834 process_err_fn
= storvsc_device_scan
;
836 * Retry the I/O that trigerred this.
838 set_host_byte(scmnd
, DID_REQUEUE
);
847 * We need to schedule work to process this error; schedule it.
849 wrk
= kmalloc(sizeof(struct storvsc_scan_work
), GFP_ATOMIC
);
851 set_host_byte(scmnd
, DID_TARGET_FAILURE
);
856 wrk
->lun
= vm_srb
->lun
;
857 INIT_WORK(&wrk
->work
, process_err_fn
);
858 schedule_work(&wrk
->work
);
862 static void storvsc_command_completion(struct storvsc_cmd_request
*cmd_request
)
864 struct scsi_cmnd
*scmnd
= cmd_request
->cmd
;
865 struct hv_host_device
*host_dev
= shost_priv(scmnd
->device
->host
);
866 void (*scsi_done_fn
)(struct scsi_cmnd
*);
867 struct scsi_sense_hdr sense_hdr
;
868 struct vmscsi_request
*vm_srb
;
869 struct stor_mem_pools
*memp
= scmnd
->device
->hostdata
;
870 u32 data_transfer_length
;
871 struct Scsi_Host
*host
;
872 struct storvsc_device
*stor_dev
;
873 struct hv_device
*dev
= host_dev
->dev
;
875 stor_dev
= get_in_stor_device(dev
);
876 host
= stor_dev
->host
;
878 vm_srb
= &cmd_request
->vstor_packet
.vm_srb
;
879 data_transfer_length
= vm_srb
->data_transfer_length
;
880 if (cmd_request
->bounce_sgl_count
) {
881 if (vm_srb
->data_in
== READ_TYPE
)
882 copy_from_bounce_buffer(scsi_sglist(scmnd
),
883 cmd_request
->bounce_sgl
,
884 scsi_sg_count(scmnd
),
885 cmd_request
->bounce_sgl_count
);
886 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
887 cmd_request
->bounce_sgl_count
);
890 scmnd
->result
= vm_srb
->scsi_status
;
893 if (scsi_normalize_sense(scmnd
->sense_buffer
,
894 SCSI_SENSE_BUFFERSIZE
, &sense_hdr
))
895 scsi_print_sense_hdr("storvsc", &sense_hdr
);
898 if (vm_srb
->srb_status
!= SRB_STATUS_SUCCESS
) {
899 storvsc_handle_error(vm_srb
, scmnd
, host
, sense_hdr
.asc
,
902 * The Windows driver set data_transfer_length on
903 * SRB_STATUS_DATA_OVERRUN. On other errors, this value
904 * is untouched. In these cases we set it to 0.
906 if (vm_srb
->srb_status
!= SRB_STATUS_DATA_OVERRUN
)
907 data_transfer_length
= 0;
910 scsi_set_resid(scmnd
,
911 cmd_request
->data_buffer
.len
- data_transfer_length
);
913 scsi_done_fn
= scmnd
->scsi_done
;
915 scmnd
->host_scribble
= NULL
;
916 scmnd
->scsi_done
= NULL
;
920 mempool_free(cmd_request
, memp
->request_mempool
);
923 static void storvsc_on_io_completion(struct hv_device
*device
,
924 struct vstor_packet
*vstor_packet
,
925 struct storvsc_cmd_request
*request
)
927 struct storvsc_device
*stor_device
;
928 struct vstor_packet
*stor_pkt
;
930 stor_device
= hv_get_drvdata(device
);
931 stor_pkt
= &request
->vstor_packet
;
934 * The current SCSI handling on the host side does
935 * not correctly handle:
936 * INQUIRY command with page code parameter set to 0x80
937 * MODE_SENSE command with cmd[2] == 0x1c
939 * Setup srb and scsi status so this won't be fatal.
940 * We do this so we can distinguish truly fatal failues
941 * (srb status == 0x4) and off-line the device in that case.
944 if ((stor_pkt
->vm_srb
.cdb
[0] == INQUIRY
) ||
945 (stor_pkt
->vm_srb
.cdb
[0] == MODE_SENSE
)) {
946 vstor_packet
->vm_srb
.scsi_status
= 0;
947 vstor_packet
->vm_srb
.srb_status
= SRB_STATUS_SUCCESS
;
951 /* Copy over the status...etc */
952 stor_pkt
->vm_srb
.scsi_status
= vstor_packet
->vm_srb
.scsi_status
;
953 stor_pkt
->vm_srb
.srb_status
= vstor_packet
->vm_srb
.srb_status
;
954 stor_pkt
->vm_srb
.sense_info_length
=
955 vstor_packet
->vm_srb
.sense_info_length
;
957 if (vstor_packet
->vm_srb
.scsi_status
!= 0 ||
958 vstor_packet
->vm_srb
.srb_status
!= SRB_STATUS_SUCCESS
){
959 dev_warn(&device
->device
,
960 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
961 stor_pkt
->vm_srb
.cdb
[0],
962 vstor_packet
->vm_srb
.scsi_status
,
963 vstor_packet
->vm_srb
.srb_status
);
966 if ((vstor_packet
->vm_srb
.scsi_status
& 0xFF) == 0x02) {
967 /* CHECK_CONDITION */
968 if (vstor_packet
->vm_srb
.srb_status
&
969 SRB_STATUS_AUTOSENSE_VALID
) {
970 /* autosense data available */
971 dev_warn(&device
->device
,
972 "stor pkt %p autosense data valid - len %d\n",
974 vstor_packet
->vm_srb
.sense_info_length
);
976 memcpy(request
->sense_buffer
,
977 vstor_packet
->vm_srb
.sense_data
,
978 vstor_packet
->vm_srb
.sense_info_length
);
983 stor_pkt
->vm_srb
.data_transfer_length
=
984 vstor_packet
->vm_srb
.data_transfer_length
;
986 storvsc_command_completion(request
);
988 if (atomic_dec_and_test(&stor_device
->num_outstanding_req
) &&
989 stor_device
->drain_notify
)
990 wake_up(&stor_device
->waiting_to_drain
);
995 static void storvsc_on_receive(struct hv_device
*device
,
996 struct vstor_packet
*vstor_packet
,
997 struct storvsc_cmd_request
*request
)
999 struct storvsc_scan_work
*work
;
1000 struct storvsc_device
*stor_device
;
1002 switch (vstor_packet
->operation
) {
1003 case VSTOR_OPERATION_COMPLETE_IO
:
1004 storvsc_on_io_completion(device
, vstor_packet
, request
);
1007 case VSTOR_OPERATION_REMOVE_DEVICE
:
1008 case VSTOR_OPERATION_ENUMERATE_BUS
:
1009 stor_device
= get_in_stor_device(device
);
1010 work
= kmalloc(sizeof(struct storvsc_scan_work
), GFP_ATOMIC
);
1014 INIT_WORK(&work
->work
, storvsc_bus_scan
);
1015 work
->host
= stor_device
->host
;
1016 schedule_work(&work
->work
);
1024 static void storvsc_on_channel_callback(void *context
)
1026 struct hv_device
*device
= (struct hv_device
*)context
;
1027 struct storvsc_device
*stor_device
;
1030 unsigned char packet
[ALIGN(sizeof(struct vstor_packet
), 8)];
1031 struct storvsc_cmd_request
*request
;
1035 stor_device
= get_in_stor_device(device
);
1040 ret
= vmbus_recvpacket(device
->channel
, packet
,
1041 ALIGN(sizeof(struct vstor_packet
), 8),
1042 &bytes_recvd
, &request_id
);
1043 if (ret
== 0 && bytes_recvd
> 0) {
1045 request
= (struct storvsc_cmd_request
*)
1046 (unsigned long)request_id
;
1048 if ((request
== &stor_device
->init_request
) ||
1049 (request
== &stor_device
->reset_request
)) {
1051 memcpy(&request
->vstor_packet
, packet
,
1052 sizeof(struct vstor_packet
));
1053 complete(&request
->wait_event
);
1055 storvsc_on_receive(device
,
1056 (struct vstor_packet
*)packet
,
1067 static int storvsc_connect_to_vsp(struct hv_device
*device
, u32 ring_size
)
1069 struct vmstorage_channel_properties props
;
1072 memset(&props
, 0, sizeof(struct vmstorage_channel_properties
));
1074 ret
= vmbus_open(device
->channel
,
1078 sizeof(struct vmstorage_channel_properties
),
1079 storvsc_on_channel_callback
, device
);
1084 ret
= storvsc_channel_init(device
);
1089 static int storvsc_dev_remove(struct hv_device
*device
)
1091 struct storvsc_device
*stor_device
;
1092 unsigned long flags
;
1094 stor_device
= hv_get_drvdata(device
);
1096 spin_lock_irqsave(&device
->channel
->inbound_lock
, flags
);
1097 stor_device
->destroy
= true;
1098 spin_unlock_irqrestore(&device
->channel
->inbound_lock
, flags
);
1101 * At this point, all outbound traffic should be disable. We
1102 * only allow inbound traffic (responses) to proceed so that
1103 * outstanding requests can be completed.
1106 storvsc_wait_to_drain(stor_device
);
1109 * Since we have already drained, we don't need to busy wait
1110 * as was done in final_release_stor_device()
1111 * Note that we cannot set the ext pointer to NULL until
1112 * we have drained - to drain the outgoing packets, we need to
1113 * allow incoming packets.
1115 spin_lock_irqsave(&device
->channel
->inbound_lock
, flags
);
1116 hv_set_drvdata(device
, NULL
);
1117 spin_unlock_irqrestore(&device
->channel
->inbound_lock
, flags
);
1119 /* Close the channel */
1120 vmbus_close(device
->channel
);
1126 static int storvsc_do_io(struct hv_device
*device
,
1127 struct storvsc_cmd_request
*request
)
1129 struct storvsc_device
*stor_device
;
1130 struct vstor_packet
*vstor_packet
;
1133 vstor_packet
= &request
->vstor_packet
;
1134 stor_device
= get_out_stor_device(device
);
1140 request
->device
= device
;
1143 vstor_packet
->flags
|= REQUEST_COMPLETION_FLAG
;
1145 vstor_packet
->vm_srb
.length
= sizeof(struct vmscsi_request
);
1148 vstor_packet
->vm_srb
.sense_info_length
= STORVSC_SENSE_BUFFER_SIZE
;
1151 vstor_packet
->vm_srb
.data_transfer_length
=
1152 request
->data_buffer
.len
;
1154 vstor_packet
->operation
= VSTOR_OPERATION_EXECUTE_SRB
;
1156 if (request
->data_buffer
.len
) {
1157 ret
= vmbus_sendpacket_multipagebuffer(device
->channel
,
1158 &request
->data_buffer
,
1160 sizeof(struct vstor_packet
),
1161 (unsigned long)request
);
1163 ret
= vmbus_sendpacket(device
->channel
, vstor_packet
,
1164 sizeof(struct vstor_packet
),
1165 (unsigned long)request
,
1167 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
1173 atomic_inc(&stor_device
->num_outstanding_req
);
1178 static int storvsc_device_alloc(struct scsi_device
*sdevice
)
1180 struct stor_mem_pools
*memp
;
1181 int number
= STORVSC_MIN_BUF_NR
;
1183 memp
= kzalloc(sizeof(struct stor_mem_pools
), GFP_KERNEL
);
1187 memp
->request_pool
=
1188 kmem_cache_create(dev_name(&sdevice
->sdev_dev
),
1189 sizeof(struct storvsc_cmd_request
), 0,
1190 SLAB_HWCACHE_ALIGN
, NULL
);
1192 if (!memp
->request_pool
)
1195 memp
->request_mempool
= mempool_create(number
, mempool_alloc_slab
,
1197 memp
->request_pool
);
1199 if (!memp
->request_mempool
)
1202 sdevice
->hostdata
= memp
;
1207 kmem_cache_destroy(memp
->request_pool
);
1214 static void storvsc_device_destroy(struct scsi_device
*sdevice
)
1216 struct stor_mem_pools
*memp
= sdevice
->hostdata
;
1221 mempool_destroy(memp
->request_mempool
);
1222 kmem_cache_destroy(memp
->request_pool
);
1224 sdevice
->hostdata
= NULL
;
1227 static int storvsc_device_configure(struct scsi_device
*sdevice
)
1229 scsi_adjust_queue_depth(sdevice
, MSG_SIMPLE_TAG
,
1230 STORVSC_MAX_IO_REQUESTS
);
1232 blk_queue_max_segment_size(sdevice
->request_queue
, PAGE_SIZE
);
1234 blk_queue_bounce_limit(sdevice
->request_queue
, BLK_BOUNCE_ANY
);
1236 sdevice
->no_write_same
= 1;
1241 static int storvsc_get_chs(struct scsi_device
*sdev
, struct block_device
* bdev
,
1242 sector_t capacity
, int *info
)
1244 sector_t nsect
= capacity
;
1245 sector_t cylinders
= nsect
;
1246 int heads
, sectors_pt
;
1249 * We are making up these values; let us keep it simple.
1252 sectors_pt
= 0x3f; /* Sectors per track */
1253 sector_div(cylinders
, heads
* sectors_pt
);
1254 if ((sector_t
)(cylinders
+ 1) * heads
* sectors_pt
< nsect
)
1258 info
[1] = sectors_pt
;
1259 info
[2] = (int)cylinders
;
1264 static int storvsc_host_reset_handler(struct scsi_cmnd
*scmnd
)
1266 struct hv_host_device
*host_dev
= shost_priv(scmnd
->device
->host
);
1267 struct hv_device
*device
= host_dev
->dev
;
1269 struct storvsc_device
*stor_device
;
1270 struct storvsc_cmd_request
*request
;
1271 struct vstor_packet
*vstor_packet
;
1275 stor_device
= get_out_stor_device(device
);
1279 request
= &stor_device
->reset_request
;
1280 vstor_packet
= &request
->vstor_packet
;
1282 init_completion(&request
->wait_event
);
1284 vstor_packet
->operation
= VSTOR_OPERATION_RESET_BUS
;
1285 vstor_packet
->flags
= REQUEST_COMPLETION_FLAG
;
1286 vstor_packet
->vm_srb
.path_id
= stor_device
->path_id
;
1288 ret
= vmbus_sendpacket(device
->channel
, vstor_packet
,
1289 sizeof(struct vstor_packet
),
1290 (unsigned long)&stor_device
->reset_request
,
1292 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
1296 t
= wait_for_completion_timeout(&request
->wait_event
, 5*HZ
);
1298 return TIMEOUT_ERROR
;
1302 * At this point, all outstanding requests in the adapter
1303 * should have been flushed out and return to us
1304 * There is a potential race here where the host may be in
1305 * the process of responding when we return from here.
1306 * Just wait for all in-transit packets to be accounted for
1307 * before we return from here.
1309 storvsc_wait_to_drain(stor_device
);
1315 * The host guarantees to respond to each command, although I/O latencies might
1316 * be unbounded on Azure. Reset the timer unconditionally to give the host a
1317 * chance to perform EH.
1319 static enum blk_eh_timer_return
storvsc_eh_timed_out(struct scsi_cmnd
*scmnd
)
1321 return BLK_EH_RESET_TIMER
;
1324 static bool storvsc_scsi_cmd_ok(struct scsi_cmnd
*scmnd
)
1326 bool allowed
= true;
1327 u8 scsi_op
= scmnd
->cmnd
[0];
1330 /* the host does not handle WRITE_SAME, log accident usage */
1333 * smartd sends this command and the host does not handle
1334 * this. So, don't send it.
1337 scmnd
->result
= ILLEGAL_REQUEST
<< 16;
1346 static int storvsc_queuecommand(struct Scsi_Host
*host
, struct scsi_cmnd
*scmnd
)
1349 struct hv_host_device
*host_dev
= shost_priv(host
);
1350 struct hv_device
*dev
= host_dev
->dev
;
1351 struct storvsc_cmd_request
*cmd_request
;
1352 unsigned int request_size
= 0;
1354 struct scatterlist
*sgl
;
1355 unsigned int sg_count
= 0;
1356 struct vmscsi_request
*vm_srb
;
1357 struct stor_mem_pools
*memp
= scmnd
->device
->hostdata
;
1359 if (!storvsc_scsi_cmd_ok(scmnd
)) {
1360 scmnd
->scsi_done(scmnd
);
1364 request_size
= sizeof(struct storvsc_cmd_request
);
1366 cmd_request
= mempool_alloc(memp
->request_mempool
,
1370 * We might be invoked in an interrupt context; hence
1371 * mempool_alloc() can fail.
1374 return SCSI_MLQUEUE_DEVICE_BUSY
;
1376 memset(cmd_request
, 0, sizeof(struct storvsc_cmd_request
));
1378 /* Setup the cmd request */
1379 cmd_request
->cmd
= scmnd
;
1381 scmnd
->host_scribble
= (unsigned char *)cmd_request
;
1383 vm_srb
= &cmd_request
->vstor_packet
.vm_srb
;
1387 switch (scmnd
->sc_data_direction
) {
1389 vm_srb
->data_in
= WRITE_TYPE
;
1391 case DMA_FROM_DEVICE
:
1392 vm_srb
->data_in
= READ_TYPE
;
1395 vm_srb
->data_in
= UNKNOWN_TYPE
;
1400 vm_srb
->port_number
= host_dev
->port
;
1401 vm_srb
->path_id
= scmnd
->device
->channel
;
1402 vm_srb
->target_id
= scmnd
->device
->id
;
1403 vm_srb
->lun
= scmnd
->device
->lun
;
1405 vm_srb
->cdb_length
= scmnd
->cmd_len
;
1407 memcpy(vm_srb
->cdb
, scmnd
->cmnd
, vm_srb
->cdb_length
);
1409 cmd_request
->sense_buffer
= scmnd
->sense_buffer
;
1412 cmd_request
->data_buffer
.len
= scsi_bufflen(scmnd
);
1413 if (scsi_sg_count(scmnd
)) {
1414 sgl
= (struct scatterlist
*)scsi_sglist(scmnd
);
1415 sg_count
= scsi_sg_count(scmnd
);
1417 /* check if we need to bounce the sgl */
1418 if (do_bounce_buffer(sgl
, scsi_sg_count(scmnd
)) != -1) {
1419 cmd_request
->bounce_sgl
=
1420 create_bounce_buffer(sgl
, scsi_sg_count(scmnd
),
1421 scsi_bufflen(scmnd
),
1423 if (!cmd_request
->bounce_sgl
) {
1424 ret
= SCSI_MLQUEUE_HOST_BUSY
;
1428 cmd_request
->bounce_sgl_count
=
1429 ALIGN(scsi_bufflen(scmnd
), PAGE_SIZE
) >>
1432 if (vm_srb
->data_in
== WRITE_TYPE
)
1433 copy_to_bounce_buffer(sgl
,
1434 cmd_request
->bounce_sgl
,
1435 scsi_sg_count(scmnd
));
1437 sgl
= cmd_request
->bounce_sgl
;
1438 sg_count
= cmd_request
->bounce_sgl_count
;
1441 cmd_request
->data_buffer
.offset
= sgl
[0].offset
;
1443 for (i
= 0; i
< sg_count
; i
++)
1444 cmd_request
->data_buffer
.pfn_array
[i
] =
1445 page_to_pfn(sg_page((&sgl
[i
])));
1447 } else if (scsi_sglist(scmnd
)) {
1448 cmd_request
->data_buffer
.offset
=
1449 virt_to_phys(scsi_sglist(scmnd
)) & (PAGE_SIZE
-1);
1450 cmd_request
->data_buffer
.pfn_array
[0] =
1451 virt_to_phys(scsi_sglist(scmnd
)) >> PAGE_SHIFT
;
1454 /* Invokes the vsc to start an IO */
1455 ret
= storvsc_do_io(dev
, cmd_request
);
1457 if (ret
== -EAGAIN
) {
1460 if (cmd_request
->bounce_sgl_count
)
1461 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
1462 cmd_request
->bounce_sgl_count
);
1464 ret
= SCSI_MLQUEUE_DEVICE_BUSY
;
1471 mempool_free(cmd_request
, memp
->request_mempool
);
1472 scmnd
->host_scribble
= NULL
;
1476 static struct scsi_host_template scsi_driver
= {
1477 .module
= THIS_MODULE
,
1478 .name
= "storvsc_host_t",
1479 .bios_param
= storvsc_get_chs
,
1480 .queuecommand
= storvsc_queuecommand
,
1481 .eh_host_reset_handler
= storvsc_host_reset_handler
,
1482 .eh_timed_out
= storvsc_eh_timed_out
,
1483 .slave_alloc
= storvsc_device_alloc
,
1484 .slave_destroy
= storvsc_device_destroy
,
1485 .slave_configure
= storvsc_device_configure
,
1487 /* 64 max_queue * 1 target */
1488 .can_queue
= STORVSC_MAX_IO_REQUESTS
*STORVSC_MAX_TARGETS
,
1490 /* no use setting to 0 since ll_blk_rw reset it to 1 */
1492 .sg_tablesize
= MAX_MULTIPAGE_BUFFER_COUNT
,
1493 .use_clustering
= DISABLE_CLUSTERING
,
1494 /* Make sure we dont get a sg segment crosses a page boundary */
1495 .dma_boundary
= PAGE_SIZE
-1,
1504 static const struct hv_vmbus_device_id id_table
[] = {
1507 .driver_data
= SCSI_GUID
1511 .driver_data
= IDE_GUID
1516 MODULE_DEVICE_TABLE(vmbus
, id_table
);
1518 static int storvsc_probe(struct hv_device
*device
,
1519 const struct hv_vmbus_device_id
*dev_id
)
1522 struct Scsi_Host
*host
;
1523 struct hv_host_device
*host_dev
;
1524 bool dev_is_ide
= ((dev_id
->driver_data
== IDE_GUID
) ? true : false);
1526 struct storvsc_device
*stor_device
;
1528 host
= scsi_host_alloc(&scsi_driver
,
1529 sizeof(struct hv_host_device
));
1533 host_dev
= shost_priv(host
);
1534 memset(host_dev
, 0, sizeof(struct hv_host_device
));
1536 host_dev
->port
= host
->host_no
;
1537 host_dev
->dev
= device
;
1540 stor_device
= kzalloc(sizeof(struct storvsc_device
), GFP_KERNEL
);
1546 stor_device
->destroy
= false;
1547 init_waitqueue_head(&stor_device
->waiting_to_drain
);
1548 stor_device
->device
= device
;
1549 stor_device
->host
= host
;
1550 hv_set_drvdata(device
, stor_device
);
1552 stor_device
->port_number
= host
->host_no
;
1553 ret
= storvsc_connect_to_vsp(device
, storvsc_ringbuffer_size
);
1557 host_dev
->path
= stor_device
->path_id
;
1558 host_dev
->target
= stor_device
->target_id
;
1560 /* max # of devices per target */
1561 host
->max_lun
= STORVSC_MAX_LUNS_PER_TARGET
;
1562 /* max # of targets per channel */
1563 host
->max_id
= STORVSC_MAX_TARGETS
;
1564 /* max # of channels */
1565 host
->max_channel
= STORVSC_MAX_CHANNELS
- 1;
1566 /* max cmd length */
1567 host
->max_cmd_len
= STORVSC_MAX_CMD_LEN
;
1569 /* Register the HBA and start the scsi bus scan */
1570 ret
= scsi_add_host(host
, &device
->device
);
1575 scsi_scan_host(host
);
1577 target
= (device
->dev_instance
.b
[5] << 8 |
1578 device
->dev_instance
.b
[4]);
1579 ret
= scsi_add_device(host
, 0, target
, 0);
1581 scsi_remove_host(host
);
1589 * Once we have connected with the host, we would need to
1590 * to invoke storvsc_dev_remove() to rollback this state and
1591 * this call also frees up the stor_device; hence the jump around
1594 storvsc_dev_remove(device
);
1601 scsi_host_put(host
);
1605 static int storvsc_remove(struct hv_device
*dev
)
1607 struct storvsc_device
*stor_device
= hv_get_drvdata(dev
);
1608 struct Scsi_Host
*host
= stor_device
->host
;
1610 scsi_remove_host(host
);
1611 storvsc_dev_remove(dev
);
1612 scsi_host_put(host
);
1617 static struct hv_driver storvsc_drv
= {
1618 .name
= KBUILD_MODNAME
,
1619 .id_table
= id_table
,
1620 .probe
= storvsc_probe
,
1621 .remove
= storvsc_remove
,
1624 static int __init
storvsc_drv_init(void)
1626 u32 max_outstanding_req_per_channel
;
1629 * Divide the ring buffer data size (which is 1 page less
1630 * than the ring buffer size since that page is reserved for
1631 * the ring buffer indices) by the max request size (which is
1632 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1634 max_outstanding_req_per_channel
=
1635 ((storvsc_ringbuffer_size
- PAGE_SIZE
) /
1636 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET
+
1637 sizeof(struct vstor_packet
) + sizeof(u64
),
1640 if (max_outstanding_req_per_channel
<
1641 STORVSC_MAX_IO_REQUESTS
)
1644 return vmbus_driver_register(&storvsc_drv
);
1647 static void __exit
storvsc_drv_exit(void)
1649 vmbus_driver_unregister(&storvsc_drv
);
1652 MODULE_LICENSE("GPL");
1653 MODULE_VERSION(HV_DRV_VERSION
);
1654 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
1655 module_init(storvsc_drv_init
);
1656 module_exit(storvsc_drv_exit
);