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/blkdev.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_cmnd.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_device.h>
40 #include <scsi/scsi_tcq.h>
41 #include <scsi/scsi_eh.h>
42 #include <scsi/scsi_devinfo.h>
43 #include <scsi/scsi_dbg.h>
44 #include <scsi/scsi_transport_fc.h>
45 #include <scsi/scsi_transport.h>
48 * All wire protocol details (storage protocol between the guest and the host)
49 * are consolidated here.
51 * Begin protocol definitions.
57 * V1 RC < 2008/1/31: 1.0
58 * V1 RC > 2008/1/31: 2.0
65 #define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
68 #define VMSTOR_PROTO_VERSION_WIN6 VMSTOR_PROTO_VERSION(2, 0)
69 #define VMSTOR_PROTO_VERSION_WIN7 VMSTOR_PROTO_VERSION(4, 2)
70 #define VMSTOR_PROTO_VERSION_WIN8 VMSTOR_PROTO_VERSION(5, 1)
71 #define VMSTOR_PROTO_VERSION_WIN8_1 VMSTOR_PROTO_VERSION(6, 0)
72 #define VMSTOR_PROTO_VERSION_WIN10 VMSTOR_PROTO_VERSION(6, 2)
74 /* Packet structure describing virtual storage requests. */
75 enum vstor_packet_operation
{
76 VSTOR_OPERATION_COMPLETE_IO
= 1,
77 VSTOR_OPERATION_REMOVE_DEVICE
= 2,
78 VSTOR_OPERATION_EXECUTE_SRB
= 3,
79 VSTOR_OPERATION_RESET_LUN
= 4,
80 VSTOR_OPERATION_RESET_ADAPTER
= 5,
81 VSTOR_OPERATION_RESET_BUS
= 6,
82 VSTOR_OPERATION_BEGIN_INITIALIZATION
= 7,
83 VSTOR_OPERATION_END_INITIALIZATION
= 8,
84 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION
= 9,
85 VSTOR_OPERATION_QUERY_PROPERTIES
= 10,
86 VSTOR_OPERATION_ENUMERATE_BUS
= 11,
87 VSTOR_OPERATION_FCHBA_DATA
= 12,
88 VSTOR_OPERATION_CREATE_SUB_CHANNELS
= 13,
89 VSTOR_OPERATION_MAXIMUM
= 13
93 * WWN packet for Fibre Channel HBA
96 struct hv_fc_wwn_packet
{
99 u8 primary_port_wwn
[8];
100 u8 primary_node_wwn
[8];
101 u8 secondary_port_wwn
[8];
102 u8 secondary_node_wwn
[8];
111 #define SRB_FLAGS_QUEUE_ACTION_ENABLE 0x00000002
112 #define SRB_FLAGS_DISABLE_DISCONNECT 0x00000004
113 #define SRB_FLAGS_DISABLE_SYNCH_TRANSFER 0x00000008
114 #define SRB_FLAGS_BYPASS_FROZEN_QUEUE 0x00000010
115 #define SRB_FLAGS_DISABLE_AUTOSENSE 0x00000020
116 #define SRB_FLAGS_DATA_IN 0x00000040
117 #define SRB_FLAGS_DATA_OUT 0x00000080
118 #define SRB_FLAGS_NO_DATA_TRANSFER 0x00000000
119 #define SRB_FLAGS_UNSPECIFIED_DIRECTION (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT)
120 #define SRB_FLAGS_NO_QUEUE_FREEZE 0x00000100
121 #define SRB_FLAGS_ADAPTER_CACHE_ENABLE 0x00000200
122 #define SRB_FLAGS_FREE_SENSE_BUFFER 0x00000400
125 * This flag indicates the request is part of the workflow for processing a D3.
127 #define SRB_FLAGS_D3_PROCESSING 0x00000800
128 #define SRB_FLAGS_IS_ACTIVE 0x00010000
129 #define SRB_FLAGS_ALLOCATED_FROM_ZONE 0x00020000
130 #define SRB_FLAGS_SGLIST_FROM_POOL 0x00040000
131 #define SRB_FLAGS_BYPASS_LOCKED_QUEUE 0x00080000
132 #define SRB_FLAGS_NO_KEEP_AWAKE 0x00100000
133 #define SRB_FLAGS_PORT_DRIVER_ALLOCSENSE 0x00200000
134 #define SRB_FLAGS_PORT_DRIVER_SENSEHASPORT 0x00400000
135 #define SRB_FLAGS_DONT_START_NEXT_PACKET 0x00800000
136 #define SRB_FLAGS_PORT_DRIVER_RESERVED 0x0F000000
137 #define SRB_FLAGS_CLASS_DRIVER_RESERVED 0xF0000000
139 #define SP_UNTAGGED ((unsigned char) ~0)
140 #define SRB_SIMPLE_TAG_REQUEST 0x20
143 * Platform neutral description of a scsi request -
144 * this remains the same across the write regardless of 32/64 bit
145 * note: it's patterned off the SCSI_PASS_THROUGH structure
147 #define STORVSC_MAX_CMD_LEN 0x10
149 #define POST_WIN7_STORVSC_SENSE_BUFFER_SIZE 0x14
150 #define PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE 0x12
152 #define STORVSC_SENSE_BUFFER_SIZE 0x14
153 #define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
156 * Sense buffer size changed in win8; have a run-time
157 * variable to track the size we should use. This value will
158 * likely change during protocol negotiation but it is valid
159 * to start by assuming pre-Win8.
161 static int sense_buffer_size
= PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE
;
164 * The storage protocol version is determined during the
165 * initial exchange with the host. It will indicate which
166 * storage functionality is available in the host.
168 static int vmstor_proto_version
;
170 #define STORVSC_LOGGING_NONE 0
171 #define STORVSC_LOGGING_ERROR 1
172 #define STORVSC_LOGGING_WARN 2
174 static int logging_level
= STORVSC_LOGGING_ERROR
;
175 module_param(logging_level
, int, S_IRUGO
|S_IWUSR
);
176 MODULE_PARM_DESC(logging_level
,
177 "Logging level, 0 - None, 1 - Error (default), 2 - Warning.");
179 static inline bool do_logging(int level
)
181 return logging_level
>= level
;
184 #define storvsc_log(dev, level, fmt, ...) \
186 if (do_logging(level)) \
187 dev_warn(&(dev)->device, fmt, ##__VA_ARGS__); \
190 struct vmscsi_win8_extension
{
192 * The following were added in Windows 8
202 struct vmscsi_request
{
213 u8 sense_info_length
;
217 u32 data_transfer_length
;
220 u8 cdb
[STORVSC_MAX_CMD_LEN
];
221 u8 sense_data
[STORVSC_SENSE_BUFFER_SIZE
];
222 u8 reserved_array
[STORVSC_MAX_BUF_LEN_WITH_PADDING
];
225 * The following was added in win8.
227 struct vmscsi_win8_extension win8_extension
;
229 } __attribute((packed
));
233 * The size of the vmscsi_request has changed in win8. The
234 * additional size is because of new elements added to the
235 * structure. These elements are valid only when we are talking
237 * Track the correction to size we need to apply. This value
238 * will likely change during protocol negotiation but it is
239 * valid to start by assuming pre-Win8.
241 static int vmscsi_size_delta
= sizeof(struct vmscsi_win8_extension
);
244 * The list of storage protocols in order of preference.
246 struct vmstor_protocol
{
247 int protocol_version
;
248 int sense_buffer_size
;
249 int vmscsi_size_delta
;
253 static const struct vmstor_protocol vmstor_protocols
[] = {
255 VMSTOR_PROTO_VERSION_WIN10
,
256 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE
,
260 VMSTOR_PROTO_VERSION_WIN8_1
,
261 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE
,
265 VMSTOR_PROTO_VERSION_WIN8
,
266 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE
,
270 VMSTOR_PROTO_VERSION_WIN7
,
271 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE
,
272 sizeof(struct vmscsi_win8_extension
),
275 VMSTOR_PROTO_VERSION_WIN6
,
276 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE
,
277 sizeof(struct vmscsi_win8_extension
),
283 * This structure is sent during the intialization phase to get the different
284 * properties of the channel.
287 #define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1
289 struct vmstorage_channel_properties
{
295 u32 max_transfer_bytes
;
300 /* This structure is sent during the storage protocol negotiations. */
301 struct vmstorage_protocol_version
{
302 /* Major (MSW) and minor (LSW) version numbers. */
306 * Revision number is auto-incremented whenever this file is changed
307 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
308 * definitely indicate incompatibility--but it does indicate mismatched
310 * This is only used on the windows side. Just set it to 0.
315 /* Channel Property Flags */
316 #define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
317 #define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
319 struct vstor_packet
{
320 /* Requested operation type */
321 enum vstor_packet_operation operation
;
323 /* Flags - see below for values */
326 /* Status of the request returned from the server side. */
329 /* Data payload area */
332 * Structure used to forward SCSI commands from the
333 * client to the server.
335 struct vmscsi_request vm_srb
;
337 /* Structure used to query channel properties. */
338 struct vmstorage_channel_properties storage_channel_properties
;
340 /* Used during version negotiations. */
341 struct vmstorage_protocol_version version
;
343 /* Fibre channel address packet */
344 struct hv_fc_wwn_packet wwn_packet
;
346 /* Number of sub-channels to create */
347 u16 sub_channel_count
;
349 /* This will be the maximum of the union members */
357 * This flag indicates that the server should send back a completion for this
361 #define REQUEST_COMPLETION_FLAG 0x1
363 /* Matches Windows-end */
364 enum storvsc_request_type
{
371 * SRB status codes and masks; a subset of the codes used here.
374 #define SRB_STATUS_AUTOSENSE_VALID 0x80
375 #define SRB_STATUS_QUEUE_FROZEN 0x40
376 #define SRB_STATUS_INVALID_LUN 0x20
377 #define SRB_STATUS_SUCCESS 0x01
378 #define SRB_STATUS_ABORTED 0x02
379 #define SRB_STATUS_ERROR 0x04
381 #define SRB_STATUS(status) \
382 (status & ~(SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_QUEUE_FROZEN))
384 * This is the end of Protocol specific defines.
387 static int storvsc_ringbuffer_size
= (256 * PAGE_SIZE
);
388 static u32 max_outstanding_req_per_channel
;
390 static int storvsc_vcpus_per_sub_channel
= 4;
392 module_param(storvsc_ringbuffer_size
, int, S_IRUGO
);
393 MODULE_PARM_DESC(storvsc_ringbuffer_size
, "Ring buffer size (bytes)");
395 module_param(storvsc_vcpus_per_sub_channel
, int, S_IRUGO
);
396 MODULE_PARM_DESC(storvsc_vcpus_per_sub_channel
, "Ratio of VCPUs to subchannels");
398 * Timeout in seconds for all devices managed by this driver.
400 static int storvsc_timeout
= 180;
402 static int msft_blist_flags
= BLIST_TRY_VPD_PAGES
;
404 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
405 static struct scsi_transport_template
*fc_transport_template
;
408 static void storvsc_on_channel_callback(void *context
);
410 #define STORVSC_MAX_LUNS_PER_TARGET 255
411 #define STORVSC_MAX_TARGETS 2
412 #define STORVSC_MAX_CHANNELS 8
414 #define STORVSC_FC_MAX_LUNS_PER_TARGET 255
415 #define STORVSC_FC_MAX_TARGETS 128
416 #define STORVSC_FC_MAX_CHANNELS 8
418 #define STORVSC_IDE_MAX_LUNS_PER_TARGET 64
419 #define STORVSC_IDE_MAX_TARGETS 1
420 #define STORVSC_IDE_MAX_CHANNELS 1
422 struct storvsc_cmd_request
{
423 struct scsi_cmnd
*cmd
;
425 struct hv_device
*device
;
427 /* Synchronize the request/response if needed */
428 struct completion wait_event
;
430 struct vmbus_channel_packet_multipage_buffer mpb
;
431 struct vmbus_packet_mpb_array
*payload
;
434 struct vstor_packet vstor_packet
;
438 /* A storvsc device is a device object that contains a vmbus channel */
439 struct storvsc_device
{
440 struct hv_device
*device
;
444 bool open_sub_channel
;
445 atomic_t num_outstanding_req
;
446 struct Scsi_Host
*host
;
448 wait_queue_head_t waiting_to_drain
;
451 * Each unique Port/Path/Target represents 1 channel ie scsi
452 * controller. In reality, the pathid, targetid is always 0
453 * and the port is set by us
455 unsigned int port_number
;
456 unsigned char path_id
;
457 unsigned char target_id
;
460 * Max I/O, the device can support.
462 u32 max_transfer_bytes
;
464 * Number of sub-channels we will open.
467 struct vmbus_channel
**stor_chns
;
469 * Mask of CPUs bound to subchannels.
471 struct cpumask alloced_cpus
;
472 /* Used for vsc/vsp channel reset process */
473 struct storvsc_cmd_request init_request
;
474 struct storvsc_cmd_request reset_request
;
476 * Currently active port and node names for FC devices.
482 struct hv_host_device
{
483 struct hv_device
*dev
;
486 unsigned char target
;
489 struct storvsc_scan_work
{
490 struct work_struct work
;
491 struct Scsi_Host
*host
;
496 static void storvsc_device_scan(struct work_struct
*work
)
498 struct storvsc_scan_work
*wrk
;
499 struct scsi_device
*sdev
;
501 wrk
= container_of(work
, struct storvsc_scan_work
, work
);
503 sdev
= scsi_device_lookup(wrk
->host
, 0, wrk
->tgt_id
, wrk
->lun
);
506 scsi_rescan_device(&sdev
->sdev_gendev
);
507 scsi_device_put(sdev
);
513 static void storvsc_host_scan(struct work_struct
*work
)
515 struct storvsc_scan_work
*wrk
;
516 struct Scsi_Host
*host
;
517 struct scsi_device
*sdev
;
519 wrk
= container_of(work
, struct storvsc_scan_work
, work
);
523 * Before scanning the host, first check to see if any of the
524 * currrently known devices have been hot removed. We issue a
525 * "unit ready" command against all currently known devices.
526 * This I/O will result in an error for devices that have been
527 * removed. As part of handling the I/O error, we remove the device.
529 * When a LUN is added or removed, the host sends us a signal to
530 * scan the host. Thus we are forced to discover the LUNs that
531 * may have been removed this way.
533 mutex_lock(&host
->scan_mutex
);
534 shost_for_each_device(sdev
, host
)
535 scsi_test_unit_ready(sdev
, 1, 1, NULL
);
536 mutex_unlock(&host
->scan_mutex
);
538 * Now scan the host to discover LUNs that may have been added.
540 scsi_scan_host(host
);
545 static void storvsc_remove_lun(struct work_struct
*work
)
547 struct storvsc_scan_work
*wrk
;
548 struct scsi_device
*sdev
;
550 wrk
= container_of(work
, struct storvsc_scan_work
, work
);
551 if (!scsi_host_get(wrk
->host
))
554 sdev
= scsi_device_lookup(wrk
->host
, 0, wrk
->tgt_id
, wrk
->lun
);
557 scsi_remove_device(sdev
);
558 scsi_device_put(sdev
);
560 scsi_host_put(wrk
->host
);
568 * We can get incoming messages from the host that are not in response to
569 * messages that we have sent out. An example of this would be messages
570 * received by the guest to notify dynamic addition/removal of LUNs. To
571 * deal with potential race conditions where the driver may be in the
572 * midst of being unloaded when we might receive an unsolicited message
573 * from the host, we have implemented a mechanism to gurantee sequential
576 * 1) Once the device is marked as being destroyed, we will fail all
578 * 2) We permit incoming messages when the device is being destroyed,
579 * only to properly account for messages already sent out.
582 static inline struct storvsc_device
*get_out_stor_device(
583 struct hv_device
*device
)
585 struct storvsc_device
*stor_device
;
587 stor_device
= hv_get_drvdata(device
);
589 if (stor_device
&& stor_device
->destroy
)
596 static inline void storvsc_wait_to_drain(struct storvsc_device
*dev
)
598 dev
->drain_notify
= true;
599 wait_event(dev
->waiting_to_drain
,
600 atomic_read(&dev
->num_outstanding_req
) == 0);
601 dev
->drain_notify
= false;
604 static inline struct storvsc_device
*get_in_stor_device(
605 struct hv_device
*device
)
607 struct storvsc_device
*stor_device
;
609 stor_device
= hv_get_drvdata(device
);
615 * If the device is being destroyed; allow incoming
616 * traffic only to cleanup outstanding requests.
619 if (stor_device
->destroy
&&
620 (atomic_read(&stor_device
->num_outstanding_req
) == 0))
628 static void handle_sc_creation(struct vmbus_channel
*new_sc
)
630 struct hv_device
*device
= new_sc
->primary_channel
->device_obj
;
631 struct storvsc_device
*stor_device
;
632 struct vmstorage_channel_properties props
;
634 stor_device
= get_out_stor_device(device
);
638 if (stor_device
->open_sub_channel
== false)
641 memset(&props
, 0, sizeof(struct vmstorage_channel_properties
));
644 storvsc_ringbuffer_size
,
645 storvsc_ringbuffer_size
,
647 sizeof(struct vmstorage_channel_properties
),
648 storvsc_on_channel_callback
, new_sc
);
650 if (new_sc
->state
== CHANNEL_OPENED_STATE
) {
651 stor_device
->stor_chns
[new_sc
->target_cpu
] = new_sc
;
652 cpumask_set_cpu(new_sc
->target_cpu
, &stor_device
->alloced_cpus
);
656 static void handle_multichannel_storage(struct hv_device
*device
, int max_chns
)
658 struct storvsc_device
*stor_device
;
659 int num_cpus
= num_online_cpus();
661 struct storvsc_cmd_request
*request
;
662 struct vstor_packet
*vstor_packet
;
665 num_sc
= ((max_chns
> num_cpus
) ? num_cpus
: max_chns
);
666 stor_device
= get_out_stor_device(device
);
670 stor_device
->num_sc
= num_sc
;
671 request
= &stor_device
->init_request
;
672 vstor_packet
= &request
->vstor_packet
;
674 stor_device
->open_sub_channel
= true;
676 * Establish a handler for dealing with subchannels.
678 vmbus_set_sc_create_callback(device
->channel
, handle_sc_creation
);
681 * Check to see if sub-channels have already been created. This
682 * can happen when this driver is re-loaded after unloading.
685 if (vmbus_are_subchannels_present(device
->channel
))
688 stor_device
->open_sub_channel
= false;
690 * Request the host to create sub-channels.
692 memset(request
, 0, sizeof(struct storvsc_cmd_request
));
693 init_completion(&request
->wait_event
);
694 vstor_packet
->operation
= VSTOR_OPERATION_CREATE_SUB_CHANNELS
;
695 vstor_packet
->flags
= REQUEST_COMPLETION_FLAG
;
696 vstor_packet
->sub_channel_count
= num_sc
;
698 ret
= vmbus_sendpacket(device
->channel
, vstor_packet
,
699 (sizeof(struct vstor_packet
) -
701 (unsigned long)request
,
703 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
708 t
= wait_for_completion_timeout(&request
->wait_event
, 10*HZ
);
712 if (vstor_packet
->operation
!= VSTOR_OPERATION_COMPLETE_IO
||
713 vstor_packet
->status
!= 0)
717 * Now that we created the sub-channels, invoke the check; this
718 * may trigger the callback.
720 stor_device
->open_sub_channel
= true;
721 vmbus_are_subchannels_present(device
->channel
);
724 static void cache_wwn(struct storvsc_device
*stor_device
,
725 struct vstor_packet
*vstor_packet
)
728 * Cache the currently active port and node ww names.
730 if (vstor_packet
->wwn_packet
.primary_active
) {
731 stor_device
->node_name
=
732 wwn_to_u64(vstor_packet
->wwn_packet
.primary_node_wwn
);
733 stor_device
->port_name
=
734 wwn_to_u64(vstor_packet
->wwn_packet
.primary_port_wwn
);
736 stor_device
->node_name
=
737 wwn_to_u64(vstor_packet
->wwn_packet
.secondary_node_wwn
);
738 stor_device
->port_name
=
739 wwn_to_u64(vstor_packet
->wwn_packet
.secondary_port_wwn
);
744 static int storvsc_execute_vstor_op(struct hv_device
*device
,
745 struct storvsc_cmd_request
*request
,
748 struct vstor_packet
*vstor_packet
;
751 vstor_packet
= &request
->vstor_packet
;
753 init_completion(&request
->wait_event
);
754 vstor_packet
->flags
= REQUEST_COMPLETION_FLAG
;
756 ret
= vmbus_sendpacket(device
->channel
, vstor_packet
,
757 (sizeof(struct vstor_packet
) -
759 (unsigned long)request
,
761 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
765 t
= wait_for_completion_timeout(&request
->wait_event
, 5*HZ
);
772 if (vstor_packet
->operation
!= VSTOR_OPERATION_COMPLETE_IO
||
773 vstor_packet
->status
!= 0)
779 static int storvsc_channel_init(struct hv_device
*device
, bool is_fc
)
781 struct storvsc_device
*stor_device
;
782 struct storvsc_cmd_request
*request
;
783 struct vstor_packet
*vstor_packet
;
786 bool process_sub_channels
= false;
788 stor_device
= get_out_stor_device(device
);
792 request
= &stor_device
->init_request
;
793 vstor_packet
= &request
->vstor_packet
;
796 * Now, initiate the vsc/vsp initialization protocol on the open
799 memset(request
, 0, sizeof(struct storvsc_cmd_request
));
800 vstor_packet
->operation
= VSTOR_OPERATION_BEGIN_INITIALIZATION
;
801 ret
= storvsc_execute_vstor_op(device
, request
, true);
805 * Query host supported protocol version.
808 for (i
= 0; i
< ARRAY_SIZE(vmstor_protocols
); i
++) {
809 /* reuse the packet for version range supported */
810 memset(vstor_packet
, 0, sizeof(struct vstor_packet
));
811 vstor_packet
->operation
=
812 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION
;
814 vstor_packet
->version
.major_minor
=
815 vmstor_protocols
[i
].protocol_version
;
818 * The revision number is only used in Windows; set it to 0.
820 vstor_packet
->version
.revision
= 0;
821 ret
= storvsc_execute_vstor_op(device
, request
, false);
825 if (vstor_packet
->operation
!= VSTOR_OPERATION_COMPLETE_IO
)
828 if (vstor_packet
->status
== 0) {
829 vmstor_proto_version
=
830 vmstor_protocols
[i
].protocol_version
;
833 vmstor_protocols
[i
].sense_buffer_size
;
836 vmstor_protocols
[i
].vmscsi_size_delta
;
842 if (vstor_packet
->status
!= 0)
846 memset(vstor_packet
, 0, sizeof(struct vstor_packet
));
847 vstor_packet
->operation
= VSTOR_OPERATION_QUERY_PROPERTIES
;
848 ret
= storvsc_execute_vstor_op(device
, request
, true);
853 * Check to see if multi-channel support is there.
854 * Hosts that implement protocol version of 5.1 and above
855 * support multi-channel.
857 max_chns
= vstor_packet
->storage_channel_properties
.max_channel_cnt
;
860 * Allocate state to manage the sub-channels.
861 * We allocate an array based on the numbers of possible CPUs
862 * (Hyper-V does not support cpu online/offline).
863 * This Array will be sparseley populated with unique
864 * channels - primary + sub-channels.
865 * We will however populate all the slots to evenly distribute
868 stor_device
->stor_chns
= kzalloc(sizeof(void *) * num_possible_cpus(),
870 if (stor_device
->stor_chns
== NULL
)
873 stor_device
->stor_chns
[device
->channel
->target_cpu
] = device
->channel
;
874 cpumask_set_cpu(device
->channel
->target_cpu
,
875 &stor_device
->alloced_cpus
);
877 if (vmstor_proto_version
>= VMSTOR_PROTO_VERSION_WIN8
) {
878 if (vstor_packet
->storage_channel_properties
.flags
&
879 STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL
)
880 process_sub_channels
= true;
882 stor_device
->max_transfer_bytes
=
883 vstor_packet
->storage_channel_properties
.max_transfer_bytes
;
889 * For FC devices retrieve FC HBA data.
891 memset(vstor_packet
, 0, sizeof(struct vstor_packet
));
892 vstor_packet
->operation
= VSTOR_OPERATION_FCHBA_DATA
;
893 ret
= storvsc_execute_vstor_op(device
, request
, true);
898 * Cache the currently active port and node ww names.
900 cache_wwn(stor_device
, vstor_packet
);
904 memset(vstor_packet
, 0, sizeof(struct vstor_packet
));
905 vstor_packet
->operation
= VSTOR_OPERATION_END_INITIALIZATION
;
906 ret
= storvsc_execute_vstor_op(device
, request
, true);
910 if (process_sub_channels
)
911 handle_multichannel_storage(device
, max_chns
);
916 static void storvsc_handle_error(struct vmscsi_request
*vm_srb
,
917 struct scsi_cmnd
*scmnd
,
918 struct Scsi_Host
*host
,
921 struct storvsc_scan_work
*wrk
;
922 void (*process_err_fn
)(struct work_struct
*work
);
923 bool do_work
= false;
925 switch (SRB_STATUS(vm_srb
->srb_status
)) {
926 case SRB_STATUS_ERROR
:
928 * If there is an error; offline the device since all
929 * error recovery strategies would have already been
930 * deployed on the host side. However, if the command
931 * were a pass-through command deal with it appropriately.
933 switch (scmnd
->cmnd
[0]) {
936 set_host_byte(scmnd
, DID_PASSTHROUGH
);
939 * On Some Windows hosts TEST_UNIT_READY command can return
940 * SRB_STATUS_ERROR, let the upper level code deal with it
941 * based on the sense information.
943 case TEST_UNIT_READY
:
946 set_host_byte(scmnd
, DID_TARGET_FAILURE
);
949 case SRB_STATUS_INVALID_LUN
:
951 process_err_fn
= storvsc_remove_lun
;
953 case SRB_STATUS_ABORTED
:
954 if (vm_srb
->srb_status
& SRB_STATUS_AUTOSENSE_VALID
&&
955 (asc
== 0x2a) && (ascq
== 0x9)) {
957 process_err_fn
= storvsc_device_scan
;
959 * Retry the I/O that trigerred this.
961 set_host_byte(scmnd
, DID_REQUEUE
);
970 * We need to schedule work to process this error; schedule it.
972 wrk
= kmalloc(sizeof(struct storvsc_scan_work
), GFP_ATOMIC
);
974 set_host_byte(scmnd
, DID_TARGET_FAILURE
);
979 wrk
->lun
= vm_srb
->lun
;
980 wrk
->tgt_id
= vm_srb
->target_id
;
981 INIT_WORK(&wrk
->work
, process_err_fn
);
982 schedule_work(&wrk
->work
);
986 static void storvsc_command_completion(struct storvsc_cmd_request
*cmd_request
,
987 struct storvsc_device
*stor_dev
)
989 struct scsi_cmnd
*scmnd
= cmd_request
->cmd
;
990 struct scsi_sense_hdr sense_hdr
;
991 struct vmscsi_request
*vm_srb
;
992 struct Scsi_Host
*host
;
993 u32 payload_sz
= cmd_request
->payload_sz
;
994 void *payload
= cmd_request
->payload
;
996 host
= stor_dev
->host
;
998 vm_srb
= &cmd_request
->vstor_packet
.vm_srb
;
1000 scmnd
->result
= vm_srb
->scsi_status
;
1002 if (scmnd
->result
) {
1003 if (scsi_normalize_sense(scmnd
->sense_buffer
,
1004 SCSI_SENSE_BUFFERSIZE
, &sense_hdr
) &&
1005 !(sense_hdr
.sense_key
== NOT_READY
&&
1006 sense_hdr
.asc
== 0x03A) &&
1007 do_logging(STORVSC_LOGGING_ERROR
))
1008 scsi_print_sense_hdr(scmnd
->device
, "storvsc",
1012 if (vm_srb
->srb_status
!= SRB_STATUS_SUCCESS
)
1013 storvsc_handle_error(vm_srb
, scmnd
, host
, sense_hdr
.asc
,
1016 scsi_set_resid(scmnd
,
1017 cmd_request
->payload
->range
.len
-
1018 vm_srb
->data_transfer_length
);
1020 scmnd
->scsi_done(scmnd
);
1023 sizeof(struct vmbus_channel_packet_multipage_buffer
))
1027 static void storvsc_on_io_completion(struct storvsc_device
*stor_device
,
1028 struct vstor_packet
*vstor_packet
,
1029 struct storvsc_cmd_request
*request
)
1031 struct vstor_packet
*stor_pkt
;
1032 struct hv_device
*device
= stor_device
->device
;
1034 stor_pkt
= &request
->vstor_packet
;
1037 * The current SCSI handling on the host side does
1038 * not correctly handle:
1039 * INQUIRY command with page code parameter set to 0x80
1040 * MODE_SENSE command with cmd[2] == 0x1c
1042 * Setup srb and scsi status so this won't be fatal.
1043 * We do this so we can distinguish truly fatal failues
1044 * (srb status == 0x4) and off-line the device in that case.
1047 if ((stor_pkt
->vm_srb
.cdb
[0] == INQUIRY
) ||
1048 (stor_pkt
->vm_srb
.cdb
[0] == MODE_SENSE
)) {
1049 vstor_packet
->vm_srb
.scsi_status
= 0;
1050 vstor_packet
->vm_srb
.srb_status
= SRB_STATUS_SUCCESS
;
1054 /* Copy over the status...etc */
1055 stor_pkt
->vm_srb
.scsi_status
= vstor_packet
->vm_srb
.scsi_status
;
1056 stor_pkt
->vm_srb
.srb_status
= vstor_packet
->vm_srb
.srb_status
;
1057 stor_pkt
->vm_srb
.sense_info_length
=
1058 vstor_packet
->vm_srb
.sense_info_length
;
1060 if (vstor_packet
->vm_srb
.scsi_status
!= 0 ||
1061 vstor_packet
->vm_srb
.srb_status
!= SRB_STATUS_SUCCESS
)
1062 storvsc_log(device
, STORVSC_LOGGING_WARN
,
1063 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
1064 stor_pkt
->vm_srb
.cdb
[0],
1065 vstor_packet
->vm_srb
.scsi_status
,
1066 vstor_packet
->vm_srb
.srb_status
);
1068 if ((vstor_packet
->vm_srb
.scsi_status
& 0xFF) == 0x02) {
1069 /* CHECK_CONDITION */
1070 if (vstor_packet
->vm_srb
.srb_status
&
1071 SRB_STATUS_AUTOSENSE_VALID
) {
1072 /* autosense data available */
1074 storvsc_log(device
, STORVSC_LOGGING_WARN
,
1075 "stor pkt %p autosense data valid - len %d\n",
1076 request
, vstor_packet
->vm_srb
.sense_info_length
);
1078 memcpy(request
->cmd
->sense_buffer
,
1079 vstor_packet
->vm_srb
.sense_data
,
1080 vstor_packet
->vm_srb
.sense_info_length
);
1085 stor_pkt
->vm_srb
.data_transfer_length
=
1086 vstor_packet
->vm_srb
.data_transfer_length
;
1088 storvsc_command_completion(request
, stor_device
);
1090 if (atomic_dec_and_test(&stor_device
->num_outstanding_req
) &&
1091 stor_device
->drain_notify
)
1092 wake_up(&stor_device
->waiting_to_drain
);
1097 static void storvsc_on_receive(struct storvsc_device
*stor_device
,
1098 struct vstor_packet
*vstor_packet
,
1099 struct storvsc_cmd_request
*request
)
1101 struct storvsc_scan_work
*work
;
1103 switch (vstor_packet
->operation
) {
1104 case VSTOR_OPERATION_COMPLETE_IO
:
1105 storvsc_on_io_completion(stor_device
, vstor_packet
, request
);
1108 case VSTOR_OPERATION_REMOVE_DEVICE
:
1109 case VSTOR_OPERATION_ENUMERATE_BUS
:
1110 work
= kmalloc(sizeof(struct storvsc_scan_work
), GFP_ATOMIC
);
1114 INIT_WORK(&work
->work
, storvsc_host_scan
);
1115 work
->host
= stor_device
->host
;
1116 schedule_work(&work
->work
);
1119 case VSTOR_OPERATION_FCHBA_DATA
:
1120 cache_wwn(stor_device
, vstor_packet
);
1121 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1122 fc_host_node_name(stor_device
->host
) = stor_device
->node_name
;
1123 fc_host_port_name(stor_device
->host
) = stor_device
->port_name
;
1131 static void storvsc_on_channel_callback(void *context
)
1133 struct vmbus_channel
*channel
= (struct vmbus_channel
*)context
;
1134 struct hv_device
*device
;
1135 struct storvsc_device
*stor_device
;
1138 unsigned char packet
[ALIGN(sizeof(struct vstor_packet
), 8)];
1139 struct storvsc_cmd_request
*request
;
1142 if (channel
->primary_channel
!= NULL
)
1143 device
= channel
->primary_channel
->device_obj
;
1145 device
= channel
->device_obj
;
1147 stor_device
= get_in_stor_device(device
);
1152 ret
= vmbus_recvpacket(channel
, packet
,
1153 ALIGN((sizeof(struct vstor_packet
) -
1154 vmscsi_size_delta
), 8),
1155 &bytes_recvd
, &request_id
);
1156 if (ret
== 0 && bytes_recvd
> 0) {
1158 request
= (struct storvsc_cmd_request
*)
1159 (unsigned long)request_id
;
1161 if ((request
== &stor_device
->init_request
) ||
1162 (request
== &stor_device
->reset_request
)) {
1164 memcpy(&request
->vstor_packet
, packet
,
1165 (sizeof(struct vstor_packet
) -
1166 vmscsi_size_delta
));
1167 complete(&request
->wait_event
);
1169 storvsc_on_receive(stor_device
,
1170 (struct vstor_packet
*)packet
,
1181 static int storvsc_connect_to_vsp(struct hv_device
*device
, u32 ring_size
,
1184 struct vmstorage_channel_properties props
;
1187 memset(&props
, 0, sizeof(struct vmstorage_channel_properties
));
1189 ret
= vmbus_open(device
->channel
,
1193 sizeof(struct vmstorage_channel_properties
),
1194 storvsc_on_channel_callback
, device
->channel
);
1199 ret
= storvsc_channel_init(device
, is_fc
);
1204 static int storvsc_dev_remove(struct hv_device
*device
)
1206 struct storvsc_device
*stor_device
;
1207 unsigned long flags
;
1209 stor_device
= hv_get_drvdata(device
);
1211 spin_lock_irqsave(&device
->channel
->inbound_lock
, flags
);
1212 stor_device
->destroy
= true;
1213 spin_unlock_irqrestore(&device
->channel
->inbound_lock
, flags
);
1216 * At this point, all outbound traffic should be disable. We
1217 * only allow inbound traffic (responses) to proceed so that
1218 * outstanding requests can be completed.
1221 storvsc_wait_to_drain(stor_device
);
1224 * Since we have already drained, we don't need to busy wait
1225 * as was done in final_release_stor_device()
1226 * Note that we cannot set the ext pointer to NULL until
1227 * we have drained - to drain the outgoing packets, we need to
1228 * allow incoming packets.
1230 spin_lock_irqsave(&device
->channel
->inbound_lock
, flags
);
1231 hv_set_drvdata(device
, NULL
);
1232 spin_unlock_irqrestore(&device
->channel
->inbound_lock
, flags
);
1234 /* Close the channel */
1235 vmbus_close(device
->channel
);
1237 kfree(stor_device
->stor_chns
);
1242 static struct vmbus_channel
*get_og_chn(struct storvsc_device
*stor_device
,
1247 struct cpumask alloced_mask
;
1248 int num_channels
, tgt_cpu
;
1250 if (stor_device
->num_sc
== 0)
1251 return stor_device
->device
->channel
;
1254 * Our channel array is sparsley populated and we
1255 * initiated I/O on a processor/hw-q that does not
1256 * currently have a designated channel. Fix this.
1257 * The strategy is simple:
1258 * I. Ensure NUMA locality
1259 * II. Distribute evenly (best effort)
1260 * III. Mapping is persistent.
1263 cpumask_and(&alloced_mask
, &stor_device
->alloced_cpus
,
1264 cpumask_of_node(cpu_to_node(q_num
)));
1266 num_channels
= cpumask_weight(&alloced_mask
);
1267 if (num_channels
== 0)
1268 return stor_device
->device
->channel
;
1271 while (hash_qnum
>= num_channels
)
1272 hash_qnum
-= num_channels
;
1274 for_each_cpu(tgt_cpu
, &alloced_mask
) {
1275 if (slot
== hash_qnum
)
1280 stor_device
->stor_chns
[q_num
] = stor_device
->stor_chns
[tgt_cpu
];
1282 return stor_device
->stor_chns
[q_num
];
1286 static int storvsc_do_io(struct hv_device
*device
,
1287 struct storvsc_cmd_request
*request
, u16 q_num
)
1289 struct storvsc_device
*stor_device
;
1290 struct vstor_packet
*vstor_packet
;
1291 struct vmbus_channel
*outgoing_channel
;
1293 struct cpumask alloced_mask
;
1296 vstor_packet
= &request
->vstor_packet
;
1297 stor_device
= get_out_stor_device(device
);
1303 request
->device
= device
;
1305 * Select an an appropriate channel to send the request out.
1308 if (stor_device
->stor_chns
[q_num
] != NULL
) {
1309 outgoing_channel
= stor_device
->stor_chns
[q_num
];
1310 if (outgoing_channel
->target_cpu
== smp_processor_id()) {
1312 * Ideally, we want to pick a different channel if
1313 * available on the same NUMA node.
1315 cpumask_and(&alloced_mask
, &stor_device
->alloced_cpus
,
1316 cpumask_of_node(cpu_to_node(q_num
)));
1317 for_each_cpu(tgt_cpu
, &alloced_mask
) {
1318 if (tgt_cpu
!= outgoing_channel
->target_cpu
) {
1320 stor_device
->stor_chns
[tgt_cpu
];
1326 outgoing_channel
= get_og_chn(stor_device
, q_num
);
1330 vstor_packet
->flags
|= REQUEST_COMPLETION_FLAG
;
1332 vstor_packet
->vm_srb
.length
= (sizeof(struct vmscsi_request
) -
1336 vstor_packet
->vm_srb
.sense_info_length
= sense_buffer_size
;
1339 vstor_packet
->vm_srb
.data_transfer_length
=
1340 request
->payload
->range
.len
;
1342 vstor_packet
->operation
= VSTOR_OPERATION_EXECUTE_SRB
;
1344 if (request
->payload
->range
.len
) {
1346 ret
= vmbus_sendpacket_mpb_desc(outgoing_channel
,
1347 request
->payload
, request
->payload_sz
,
1349 (sizeof(struct vstor_packet
) -
1351 (unsigned long)request
);
1353 ret
= vmbus_sendpacket(outgoing_channel
, vstor_packet
,
1354 (sizeof(struct vstor_packet
) -
1356 (unsigned long)request
,
1358 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
1364 atomic_inc(&stor_device
->num_outstanding_req
);
1369 static int storvsc_device_configure(struct scsi_device
*sdevice
)
1372 blk_queue_bounce_limit(sdevice
->request_queue
, BLK_BOUNCE_ANY
);
1374 blk_queue_rq_timeout(sdevice
->request_queue
, (storvsc_timeout
* HZ
));
1376 /* Ensure there are no gaps in presented sgls */
1377 blk_queue_virt_boundary(sdevice
->request_queue
, PAGE_SIZE
- 1);
1379 sdevice
->no_write_same
= 1;
1382 * Add blist flags to permit the reading of the VPD pages even when
1383 * the target may claim SPC-2 compliance. MSFT targets currently
1384 * claim SPC-2 compliance while they implement post SPC-2 features.
1385 * With this patch we can correctly handle WRITE_SAME_16 issues.
1387 sdevice
->sdev_bflags
|= msft_blist_flags
;
1390 * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3
1391 * if the device is a MSFT virtual device. If the host is
1392 * WIN10 or newer, allow write_same.
1394 if (!strncmp(sdevice
->vendor
, "Msft", 4)) {
1395 switch (vmstor_proto_version
) {
1396 case VMSTOR_PROTO_VERSION_WIN8
:
1397 case VMSTOR_PROTO_VERSION_WIN8_1
:
1398 sdevice
->scsi_level
= SCSI_SPC_3
;
1402 if (vmstor_proto_version
>= VMSTOR_PROTO_VERSION_WIN10
)
1403 sdevice
->no_write_same
= 0;
1409 static int storvsc_get_chs(struct scsi_device
*sdev
, struct block_device
* bdev
,
1410 sector_t capacity
, int *info
)
1412 sector_t nsect
= capacity
;
1413 sector_t cylinders
= nsect
;
1414 int heads
, sectors_pt
;
1417 * We are making up these values; let us keep it simple.
1420 sectors_pt
= 0x3f; /* Sectors per track */
1421 sector_div(cylinders
, heads
* sectors_pt
);
1422 if ((sector_t
)(cylinders
+ 1) * heads
* sectors_pt
< nsect
)
1426 info
[1] = sectors_pt
;
1427 info
[2] = (int)cylinders
;
1432 static int storvsc_host_reset_handler(struct scsi_cmnd
*scmnd
)
1434 struct hv_host_device
*host_dev
= shost_priv(scmnd
->device
->host
);
1435 struct hv_device
*device
= host_dev
->dev
;
1437 struct storvsc_device
*stor_device
;
1438 struct storvsc_cmd_request
*request
;
1439 struct vstor_packet
*vstor_packet
;
1443 stor_device
= get_out_stor_device(device
);
1447 request
= &stor_device
->reset_request
;
1448 vstor_packet
= &request
->vstor_packet
;
1450 init_completion(&request
->wait_event
);
1452 vstor_packet
->operation
= VSTOR_OPERATION_RESET_BUS
;
1453 vstor_packet
->flags
= REQUEST_COMPLETION_FLAG
;
1454 vstor_packet
->vm_srb
.path_id
= stor_device
->path_id
;
1456 ret
= vmbus_sendpacket(device
->channel
, vstor_packet
,
1457 (sizeof(struct vstor_packet
) -
1459 (unsigned long)&stor_device
->reset_request
,
1461 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
1465 t
= wait_for_completion_timeout(&request
->wait_event
, 5*HZ
);
1467 return TIMEOUT_ERROR
;
1471 * At this point, all outstanding requests in the adapter
1472 * should have been flushed out and return to us
1473 * There is a potential race here where the host may be in
1474 * the process of responding when we return from here.
1475 * Just wait for all in-transit packets to be accounted for
1476 * before we return from here.
1478 storvsc_wait_to_drain(stor_device
);
1484 * The host guarantees to respond to each command, although I/O latencies might
1485 * be unbounded on Azure. Reset the timer unconditionally to give the host a
1486 * chance to perform EH.
1488 static enum blk_eh_timer_return
storvsc_eh_timed_out(struct scsi_cmnd
*scmnd
)
1490 return BLK_EH_RESET_TIMER
;
1493 static bool storvsc_scsi_cmd_ok(struct scsi_cmnd
*scmnd
)
1495 bool allowed
= true;
1496 u8 scsi_op
= scmnd
->cmnd
[0];
1499 /* the host does not handle WRITE_SAME, log accident usage */
1502 * smartd sends this command and the host does not handle
1503 * this. So, don't send it.
1506 scmnd
->result
= ILLEGAL_REQUEST
<< 16;
1515 static int storvsc_queuecommand(struct Scsi_Host
*host
, struct scsi_cmnd
*scmnd
)
1518 struct hv_host_device
*host_dev
= shost_priv(host
);
1519 struct hv_device
*dev
= host_dev
->dev
;
1520 struct storvsc_cmd_request
*cmd_request
= scsi_cmd_priv(scmnd
);
1522 struct scatterlist
*sgl
;
1523 unsigned int sg_count
= 0;
1524 struct vmscsi_request
*vm_srb
;
1525 struct scatterlist
*cur_sgl
;
1526 struct vmbus_packet_mpb_array
*payload
;
1530 if (vmstor_proto_version
<= VMSTOR_PROTO_VERSION_WIN8
) {
1532 * On legacy hosts filter unimplemented commands.
1533 * Future hosts are expected to correctly handle
1534 * unsupported commands. Furthermore, it is
1535 * possible that some of the currently
1536 * unsupported commands maybe supported in
1537 * future versions of the host.
1539 if (!storvsc_scsi_cmd_ok(scmnd
)) {
1540 scmnd
->scsi_done(scmnd
);
1545 /* Setup the cmd request */
1546 cmd_request
->cmd
= scmnd
;
1548 vm_srb
= &cmd_request
->vstor_packet
.vm_srb
;
1549 vm_srb
->win8_extension
.time_out_value
= 60;
1551 vm_srb
->win8_extension
.srb_flags
|=
1552 SRB_FLAGS_DISABLE_SYNCH_TRANSFER
;
1554 if (scmnd
->device
->tagged_supported
) {
1555 vm_srb
->win8_extension
.srb_flags
|=
1556 (SRB_FLAGS_QUEUE_ACTION_ENABLE
| SRB_FLAGS_NO_QUEUE_FREEZE
);
1557 vm_srb
->win8_extension
.queue_tag
= SP_UNTAGGED
;
1558 vm_srb
->win8_extension
.queue_action
= SRB_SIMPLE_TAG_REQUEST
;
1562 switch (scmnd
->sc_data_direction
) {
1564 vm_srb
->data_in
= WRITE_TYPE
;
1565 vm_srb
->win8_extension
.srb_flags
|= SRB_FLAGS_DATA_OUT
;
1567 case DMA_FROM_DEVICE
:
1568 vm_srb
->data_in
= READ_TYPE
;
1569 vm_srb
->win8_extension
.srb_flags
|= SRB_FLAGS_DATA_IN
;
1572 vm_srb
->data_in
= UNKNOWN_TYPE
;
1573 vm_srb
->win8_extension
.srb_flags
|= SRB_FLAGS_NO_DATA_TRANSFER
;
1577 * This is DMA_BIDIRECTIONAL or something else we are never
1578 * supposed to see here.
1580 WARN(1, "Unexpected data direction: %d\n",
1581 scmnd
->sc_data_direction
);
1586 vm_srb
->port_number
= host_dev
->port
;
1587 vm_srb
->path_id
= scmnd
->device
->channel
;
1588 vm_srb
->target_id
= scmnd
->device
->id
;
1589 vm_srb
->lun
= scmnd
->device
->lun
;
1591 vm_srb
->cdb_length
= scmnd
->cmd_len
;
1593 memcpy(vm_srb
->cdb
, scmnd
->cmnd
, vm_srb
->cdb_length
);
1595 sgl
= (struct scatterlist
*)scsi_sglist(scmnd
);
1596 sg_count
= scsi_sg_count(scmnd
);
1598 length
= scsi_bufflen(scmnd
);
1599 payload
= (struct vmbus_packet_mpb_array
*)&cmd_request
->mpb
;
1600 payload_sz
= sizeof(cmd_request
->mpb
);
1603 if (sg_count
> MAX_PAGE_BUFFER_COUNT
) {
1605 payload_sz
= (sg_count
* sizeof(u64
) +
1606 sizeof(struct vmbus_packet_mpb_array
));
1607 payload
= kzalloc(payload_sz
, GFP_ATOMIC
);
1609 return SCSI_MLQUEUE_DEVICE_BUSY
;
1612 payload
->range
.len
= length
;
1613 payload
->range
.offset
= sgl
[0].offset
;
1616 for (i
= 0; i
< sg_count
; i
++) {
1617 payload
->range
.pfn_array
[i
] =
1618 page_to_pfn(sg_page((cur_sgl
)));
1619 cur_sgl
= sg_next(cur_sgl
);
1622 } else if (scsi_sglist(scmnd
)) {
1623 payload
->range
.len
= length
;
1624 payload
->range
.offset
=
1625 virt_to_phys(scsi_sglist(scmnd
)) & (PAGE_SIZE
-1);
1626 payload
->range
.pfn_array
[0] =
1627 virt_to_phys(scsi_sglist(scmnd
)) >> PAGE_SHIFT
;
1630 cmd_request
->payload
= payload
;
1631 cmd_request
->payload_sz
= payload_sz
;
1633 /* Invokes the vsc to start an IO */
1634 ret
= storvsc_do_io(dev
, cmd_request
, get_cpu());
1637 if (ret
== -EAGAIN
) {
1639 return SCSI_MLQUEUE_DEVICE_BUSY
;
1645 static struct scsi_host_template scsi_driver
= {
1646 .module
= THIS_MODULE
,
1647 .name
= "storvsc_host_t",
1648 .cmd_size
= sizeof(struct storvsc_cmd_request
),
1649 .bios_param
= storvsc_get_chs
,
1650 .queuecommand
= storvsc_queuecommand
,
1651 .eh_host_reset_handler
= storvsc_host_reset_handler
,
1652 .proc_name
= "storvsc_host",
1653 .eh_timed_out
= storvsc_eh_timed_out
,
1654 .slave_configure
= storvsc_device_configure
,
1657 .use_clustering
= ENABLE_CLUSTERING
,
1658 /* Make sure we dont get a sg segment crosses a page boundary */
1659 .dma_boundary
= PAGE_SIZE
-1,
1661 .track_queue_depth
= 1,
1670 static const struct hv_vmbus_device_id id_table
[] = {
1673 .driver_data
= SCSI_GUID
1677 .driver_data
= IDE_GUID
1679 /* Fibre Channel GUID */
1682 .driver_data
= SFC_GUID
1687 MODULE_DEVICE_TABLE(vmbus
, id_table
);
1689 static int storvsc_probe(struct hv_device
*device
,
1690 const struct hv_vmbus_device_id
*dev_id
)
1693 int num_cpus
= num_online_cpus();
1694 struct Scsi_Host
*host
;
1695 struct hv_host_device
*host_dev
;
1696 bool dev_is_ide
= ((dev_id
->driver_data
== IDE_GUID
) ? true : false);
1697 bool is_fc
= ((dev_id
->driver_data
== SFC_GUID
) ? true : false);
1699 struct storvsc_device
*stor_device
;
1700 int max_luns_per_target
;
1703 int max_sub_channels
= 0;
1706 * Based on the windows host we are running on,
1707 * set state to properly communicate with the host.
1710 if (vmbus_proto_version
< VERSION_WIN8
) {
1711 max_luns_per_target
= STORVSC_IDE_MAX_LUNS_PER_TARGET
;
1712 max_targets
= STORVSC_IDE_MAX_TARGETS
;
1713 max_channels
= STORVSC_IDE_MAX_CHANNELS
;
1715 max_luns_per_target
= STORVSC_MAX_LUNS_PER_TARGET
;
1716 max_targets
= STORVSC_MAX_TARGETS
;
1717 max_channels
= STORVSC_MAX_CHANNELS
;
1719 * On Windows8 and above, we support sub-channels for storage.
1720 * The number of sub-channels offerred is based on the number of
1721 * VCPUs in the guest.
1723 max_sub_channels
= (num_cpus
/ storvsc_vcpus_per_sub_channel
);
1726 scsi_driver
.can_queue
= (max_outstanding_req_per_channel
*
1727 (max_sub_channels
+ 1));
1729 host
= scsi_host_alloc(&scsi_driver
,
1730 sizeof(struct hv_host_device
));
1734 host_dev
= shost_priv(host
);
1735 memset(host_dev
, 0, sizeof(struct hv_host_device
));
1737 host_dev
->port
= host
->host_no
;
1738 host_dev
->dev
= device
;
1741 stor_device
= kzalloc(sizeof(struct storvsc_device
), GFP_KERNEL
);
1747 stor_device
->destroy
= false;
1748 stor_device
->open_sub_channel
= false;
1749 init_waitqueue_head(&stor_device
->waiting_to_drain
);
1750 stor_device
->device
= device
;
1751 stor_device
->host
= host
;
1752 hv_set_drvdata(device
, stor_device
);
1754 stor_device
->port_number
= host
->host_no
;
1755 ret
= storvsc_connect_to_vsp(device
, storvsc_ringbuffer_size
, is_fc
);
1759 host_dev
->path
= stor_device
->path_id
;
1760 host_dev
->target
= stor_device
->target_id
;
1762 switch (dev_id
->driver_data
) {
1764 host
->max_lun
= STORVSC_FC_MAX_LUNS_PER_TARGET
;
1765 host
->max_id
= STORVSC_FC_MAX_TARGETS
;
1766 host
->max_channel
= STORVSC_FC_MAX_CHANNELS
- 1;
1767 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1768 host
->transportt
= fc_transport_template
;
1773 host
->max_lun
= max_luns_per_target
;
1774 host
->max_id
= max_targets
;
1775 host
->max_channel
= max_channels
- 1;
1779 host
->max_lun
= STORVSC_IDE_MAX_LUNS_PER_TARGET
;
1780 host
->max_id
= STORVSC_IDE_MAX_TARGETS
;
1781 host
->max_channel
= STORVSC_IDE_MAX_CHANNELS
- 1;
1784 /* max cmd length */
1785 host
->max_cmd_len
= STORVSC_MAX_CMD_LEN
;
1788 * set the table size based on the info we got
1791 host
->sg_tablesize
= (stor_device
->max_transfer_bytes
>> PAGE_SHIFT
);
1793 * Set the number of HW queues we are supporting.
1795 if (stor_device
->num_sc
!= 0)
1796 host
->nr_hw_queues
= stor_device
->num_sc
+ 1;
1798 /* Register the HBA and start the scsi bus scan */
1799 ret
= scsi_add_host(host
, &device
->device
);
1804 scsi_scan_host(host
);
1806 target
= (device
->dev_instance
.b
[5] << 8 |
1807 device
->dev_instance
.b
[4]);
1808 ret
= scsi_add_device(host
, 0, target
, 0);
1810 scsi_remove_host(host
);
1814 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1815 if (host
->transportt
== fc_transport_template
) {
1816 fc_host_node_name(host
) = stor_device
->node_name
;
1817 fc_host_port_name(host
) = stor_device
->port_name
;
1824 * Once we have connected with the host, we would need to
1825 * to invoke storvsc_dev_remove() to rollback this state and
1826 * this call also frees up the stor_device; hence the jump around
1829 storvsc_dev_remove(device
);
1833 kfree(stor_device
->stor_chns
);
1837 scsi_host_put(host
);
1841 static int storvsc_remove(struct hv_device
*dev
)
1843 struct storvsc_device
*stor_device
= hv_get_drvdata(dev
);
1844 struct Scsi_Host
*host
= stor_device
->host
;
1846 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1847 if (host
->transportt
== fc_transport_template
)
1848 fc_remove_host(host
);
1850 scsi_remove_host(host
);
1851 storvsc_dev_remove(dev
);
1852 scsi_host_put(host
);
1857 static struct hv_driver storvsc_drv
= {
1858 .name
= KBUILD_MODNAME
,
1859 .id_table
= id_table
,
1860 .probe
= storvsc_probe
,
1861 .remove
= storvsc_remove
,
1864 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1865 static struct fc_function_template fc_transport_functions
= {
1866 .show_host_node_name
= 1,
1867 .show_host_port_name
= 1,
1871 static int __init
storvsc_drv_init(void)
1876 * Divide the ring buffer data size (which is 1 page less
1877 * than the ring buffer size since that page is reserved for
1878 * the ring buffer indices) by the max request size (which is
1879 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1881 max_outstanding_req_per_channel
=
1882 ((storvsc_ringbuffer_size
- PAGE_SIZE
) /
1883 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET
+
1884 sizeof(struct vstor_packet
) + sizeof(u64
) -
1888 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1889 fc_transport_template
= fc_attach_transport(&fc_transport_functions
);
1890 if (!fc_transport_template
)
1894 * Install Hyper-V specific timeout handler.
1896 fc_transport_template
->eh_timed_out
= storvsc_eh_timed_out
;
1899 ret
= vmbus_driver_register(&storvsc_drv
);
1901 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1903 fc_release_transport(fc_transport_template
);
1909 static void __exit
storvsc_drv_exit(void)
1911 vmbus_driver_unregister(&storvsc_drv
);
1912 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1913 fc_release_transport(fc_transport_template
);
1917 MODULE_LICENSE("GPL");
1918 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
1919 module_init(storvsc_drv_init
);
1920 module_exit(storvsc_drv_exit
);