xref: /openbmc/linux/drivers/scsi/storvsc_drv.c (revision 360823a09426347ea8f232b0b0b5156d0aed0302)
13b20eb23SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
289ae7d70SK. Y. Srinivasan /*
389ae7d70SK. Y. Srinivasan  * Copyright (c) 2009, Microsoft Corporation.
489ae7d70SK. Y. Srinivasan  *
589ae7d70SK. Y. Srinivasan  * Authors:
689ae7d70SK. Y. Srinivasan  *   Haiyang Zhang <haiyangz@microsoft.com>
789ae7d70SK. Y. Srinivasan  *   Hank Janssen  <hjanssen@microsoft.com>
889ae7d70SK. Y. Srinivasan  *   K. Y. Srinivasan <kys@microsoft.com>
989ae7d70SK. Y. Srinivasan  */
1089ae7d70SK. Y. Srinivasan 
1189ae7d70SK. Y. Srinivasan #include <linux/kernel.h>
1289ae7d70SK. Y. Srinivasan #include <linux/wait.h>
1389ae7d70SK. Y. Srinivasan #include <linux/sched.h>
1489ae7d70SK. Y. Srinivasan #include <linux/completion.h>
1589ae7d70SK. Y. Srinivasan #include <linux/string.h>
1689ae7d70SK. Y. Srinivasan #include <linux/mm.h>
1789ae7d70SK. Y. Srinivasan #include <linux/delay.h>
1889ae7d70SK. Y. Srinivasan #include <linux/init.h>
1989ae7d70SK. Y. Srinivasan #include <linux/slab.h>
2089ae7d70SK. Y. Srinivasan #include <linux/module.h>
2189ae7d70SK. Y. Srinivasan #include <linux/device.h>
2289ae7d70SK. Y. Srinivasan #include <linux/hyperv.h>
2356b26e69SK. Y. Srinivasan #include <linux/blkdev.h>
24743b237cSTianyu Lan #include <linux/dma-mapping.h>
25743b237cSTianyu Lan 
2689ae7d70SK. Y. Srinivasan #include <scsi/scsi.h>
2789ae7d70SK. Y. Srinivasan #include <scsi/scsi_cmnd.h>
2889ae7d70SK. Y. Srinivasan #include <scsi/scsi_host.h>
2989ae7d70SK. Y. Srinivasan #include <scsi/scsi_device.h>
3089ae7d70SK. Y. Srinivasan #include <scsi/scsi_tcq.h>
3189ae7d70SK. Y. Srinivasan #include <scsi/scsi_eh.h>
3289ae7d70SK. Y. Srinivasan #include <scsi/scsi_devinfo.h>
3389ae7d70SK. Y. Srinivasan #include <scsi/scsi_dbg.h>
34dac58241SK. Y. Srinivasan #include <scsi/scsi_transport_fc.h>
35d791a8c6SK. Y. Srinivasan #include <scsi/scsi_transport.h>
3689ae7d70SK. Y. Srinivasan 
3789ae7d70SK. Y. Srinivasan /*
3889ae7d70SK. Y. Srinivasan  * All wire protocol details (storage protocol between the guest and the host)
3989ae7d70SK. Y. Srinivasan  * are consolidated here.
4089ae7d70SK. Y. Srinivasan  *
4189ae7d70SK. Y. Srinivasan  * Begin protocol definitions.
4289ae7d70SK. Y. Srinivasan  */
4389ae7d70SK. Y. Srinivasan 
4489ae7d70SK. Y. Srinivasan /*
4589ae7d70SK. Y. Srinivasan  * Version history:
4689ae7d70SK. Y. Srinivasan  * V1 Beta: 0.1
4789ae7d70SK. Y. Srinivasan  * V1 RC < 2008/1/31: 1.0
4889ae7d70SK. Y. Srinivasan  * V1 RC > 2008/1/31:  2.0
4989ae7d70SK. Y. Srinivasan  * Win7: 4.2
508b612fa2SK. Y. Srinivasan  * Win8: 5.1
511a363108SKeith Mange  * Win8.1: 6.0
521a363108SKeith Mange  * Win10: 6.2
5389ae7d70SK. Y. Srinivasan  */
5489ae7d70SK. Y. Srinivasan 
552492fd7aSKeith Mange #define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_)	((((MAJOR_) & 0xff) << 8) | \
562492fd7aSKeith Mange 						(((MINOR_) & 0xff)))
571a363108SKeith Mange #define VMSTOR_PROTO_VERSION_WIN6	VMSTOR_PROTO_VERSION(2, 0)
582492fd7aSKeith Mange #define VMSTOR_PROTO_VERSION_WIN7	VMSTOR_PROTO_VERSION(4, 2)
592492fd7aSKeith Mange #define VMSTOR_PROTO_VERSION_WIN8	VMSTOR_PROTO_VERSION(5, 1)
601a363108SKeith Mange #define VMSTOR_PROTO_VERSION_WIN8_1	VMSTOR_PROTO_VERSION(6, 0)
611a363108SKeith Mange #define VMSTOR_PROTO_VERSION_WIN10	VMSTOR_PROTO_VERSION(6, 2)
6289ae7d70SK. Y. Srinivasan 
6378c65f0fSSaurabh Sengar /* channel callback timeout in ms */
6478c65f0fSSaurabh Sengar #define CALLBACK_TIMEOUT               2
6578c65f0fSSaurabh Sengar 
6689ae7d70SK. Y. Srinivasan /*  Packet structure describing virtual storage requests. */
6789ae7d70SK. Y. Srinivasan enum vstor_packet_operation {
6889ae7d70SK. Y. Srinivasan 	VSTOR_OPERATION_COMPLETE_IO		= 1,
6989ae7d70SK. Y. Srinivasan 	VSTOR_OPERATION_REMOVE_DEVICE		= 2,
7089ae7d70SK. Y. Srinivasan 	VSTOR_OPERATION_EXECUTE_SRB		= 3,
7189ae7d70SK. Y. Srinivasan 	VSTOR_OPERATION_RESET_LUN		= 4,
7289ae7d70SK. Y. Srinivasan 	VSTOR_OPERATION_RESET_ADAPTER		= 5,
7389ae7d70SK. Y. Srinivasan 	VSTOR_OPERATION_RESET_BUS		= 6,
7489ae7d70SK. Y. Srinivasan 	VSTOR_OPERATION_BEGIN_INITIALIZATION	= 7,
7589ae7d70SK. Y. Srinivasan 	VSTOR_OPERATION_END_INITIALIZATION	= 8,
7689ae7d70SK. Y. Srinivasan 	VSTOR_OPERATION_QUERY_PROTOCOL_VERSION	= 9,
7789ae7d70SK. Y. Srinivasan 	VSTOR_OPERATION_QUERY_PROPERTIES	= 10,
7889ae7d70SK. Y. Srinivasan 	VSTOR_OPERATION_ENUMERATE_BUS		= 11,
798b612fa2SK. Y. Srinivasan 	VSTOR_OPERATION_FCHBA_DATA              = 12,
808b612fa2SK. Y. Srinivasan 	VSTOR_OPERATION_CREATE_SUB_CHANNELS     = 13,
818b612fa2SK. Y. Srinivasan 	VSTOR_OPERATION_MAXIMUM                 = 13
8289ae7d70SK. Y. Srinivasan };
8389ae7d70SK. Y. Srinivasan 
8489ae7d70SK. Y. Srinivasan /*
858b612fa2SK. Y. Srinivasan  * WWN packet for Fibre Channel HBA
868b612fa2SK. Y. Srinivasan  */
878b612fa2SK. Y. Srinivasan 
888b612fa2SK. Y. Srinivasan struct hv_fc_wwn_packet {
8983d1e8b9SK. Y. Srinivasan 	u8	primary_active;
9083d1e8b9SK. Y. Srinivasan 	u8	reserved1[3];
918b612fa2SK. Y. Srinivasan 	u8	primary_port_wwn[8];
928b612fa2SK. Y. Srinivasan 	u8	primary_node_wwn[8];
938b612fa2SK. Y. Srinivasan 	u8	secondary_port_wwn[8];
948b612fa2SK. Y. Srinivasan 	u8	secondary_node_wwn[8];
958b612fa2SK. Y. Srinivasan };
968b612fa2SK. Y. Srinivasan 
978b612fa2SK. Y. Srinivasan 
988b612fa2SK. Y. Srinivasan 
998b612fa2SK. Y. Srinivasan /*
1008b612fa2SK. Y. Srinivasan  * SRB Flag Bits
1018b612fa2SK. Y. Srinivasan  */
1028b612fa2SK. Y. Srinivasan 
1038b612fa2SK. Y. Srinivasan #define SRB_FLAGS_QUEUE_ACTION_ENABLE		0x00000002
1048b612fa2SK. Y. Srinivasan #define SRB_FLAGS_DISABLE_DISCONNECT		0x00000004
1058b612fa2SK. Y. Srinivasan #define SRB_FLAGS_DISABLE_SYNCH_TRANSFER	0x00000008
1068b612fa2SK. Y. Srinivasan #define SRB_FLAGS_BYPASS_FROZEN_QUEUE		0x00000010
1078b612fa2SK. Y. Srinivasan #define SRB_FLAGS_DISABLE_AUTOSENSE		0x00000020
1088b612fa2SK. Y. Srinivasan #define SRB_FLAGS_DATA_IN			0x00000040
1098b612fa2SK. Y. Srinivasan #define SRB_FLAGS_DATA_OUT			0x00000080
1108b612fa2SK. Y. Srinivasan #define SRB_FLAGS_NO_DATA_TRANSFER		0x00000000
1118b612fa2SK. Y. Srinivasan #define SRB_FLAGS_UNSPECIFIED_DIRECTION	(SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT)
1128b612fa2SK. Y. Srinivasan #define SRB_FLAGS_NO_QUEUE_FREEZE		0x00000100
1138b612fa2SK. Y. Srinivasan #define SRB_FLAGS_ADAPTER_CACHE_ENABLE		0x00000200
1148b612fa2SK. Y. Srinivasan #define SRB_FLAGS_FREE_SENSE_BUFFER		0x00000400
1158b612fa2SK. Y. Srinivasan 
1168b612fa2SK. Y. Srinivasan /*
1178b612fa2SK. Y. Srinivasan  * This flag indicates the request is part of the workflow for processing a D3.
1188b612fa2SK. Y. Srinivasan  */
1198b612fa2SK. Y. Srinivasan #define SRB_FLAGS_D3_PROCESSING			0x00000800
1208b612fa2SK. Y. Srinivasan #define SRB_FLAGS_IS_ACTIVE			0x00010000
1218b612fa2SK. Y. Srinivasan #define SRB_FLAGS_ALLOCATED_FROM_ZONE		0x00020000
1228b612fa2SK. Y. Srinivasan #define SRB_FLAGS_SGLIST_FROM_POOL		0x00040000
1238b612fa2SK. Y. Srinivasan #define SRB_FLAGS_BYPASS_LOCKED_QUEUE		0x00080000
1248b612fa2SK. Y. Srinivasan #define SRB_FLAGS_NO_KEEP_AWAKE			0x00100000
1258b612fa2SK. Y. Srinivasan #define SRB_FLAGS_PORT_DRIVER_ALLOCSENSE	0x00200000
1268b612fa2SK. Y. Srinivasan #define SRB_FLAGS_PORT_DRIVER_SENSEHASPORT	0x00400000
1278b612fa2SK. Y. Srinivasan #define SRB_FLAGS_DONT_START_NEXT_PACKET	0x00800000
1288b612fa2SK. Y. Srinivasan #define SRB_FLAGS_PORT_DRIVER_RESERVED		0x0F000000
1298b612fa2SK. Y. Srinivasan #define SRB_FLAGS_CLASS_DRIVER_RESERVED		0xF0000000
1308b612fa2SK. Y. Srinivasan 
1313cd6d3d9SLong Li #define SP_UNTAGGED			((unsigned char) ~0)
1323cd6d3d9SLong Li #define SRB_SIMPLE_TAG_REQUEST		0x20
1338b612fa2SK. Y. Srinivasan 
1348b612fa2SK. Y. Srinivasan /*
13589ae7d70SK. Y. Srinivasan  * Platform neutral description of a scsi request -
13689ae7d70SK. Y. Srinivasan  * this remains the same across the write regardless of 32/64 bit
13789ae7d70SK. Y. Srinivasan  * note: it's patterned off the SCSI_PASS_THROUGH structure
13889ae7d70SK. Y. Srinivasan  */
13989ae7d70SK. Y. Srinivasan #define STORVSC_MAX_CMD_LEN			0x10
1408b612fa2SK. Y. Srinivasan 
14186c8fb4dSSaurabh Sengar /* Sense buffer size is the same for all versions since Windows 8 */
1428b612fa2SK. Y. Srinivasan #define STORVSC_SENSE_BUFFER_SIZE		0x14
14389ae7d70SK. Y. Srinivasan #define STORVSC_MAX_BUF_LEN_WITH_PADDING	0x14
14489ae7d70SK. Y. Srinivasan 
1458b612fa2SK. Y. Srinivasan /*
146cb11feadSKeith Mange  * The storage protocol version is determined during the
147cb11feadSKeith Mange  * initial exchange with the host.  It will indicate which
148cb11feadSKeith Mange  * storage functionality is available in the host.
1498b612fa2SK. Y. Srinivasan */
1502492fd7aSKeith Mange static int vmstor_proto_version;
1518b612fa2SK. Y. Srinivasan 
1523556af9aSCathy Avery static bool hv_dev_is_fc(struct hv_device *hv_dev);
1533556af9aSCathy Avery 
154f8aea701SLong Li #define STORVSC_LOGGING_NONE	0
155f8aea701SLong Li #define STORVSC_LOGGING_ERROR	1
156f8aea701SLong Li #define STORVSC_LOGGING_WARN	2
157f8aea701SLong Li 
158f8aea701SLong Li static int logging_level = STORVSC_LOGGING_ERROR;
159f8aea701SLong Li module_param(logging_level, int, S_IRUGO|S_IWUSR);
160f8aea701SLong Li MODULE_PARM_DESC(logging_level,
161f8aea701SLong Li 	"Logging level, 0 - None, 1 - Error (default), 2 - Warning.");
162f8aea701SLong Li 
do_logging(int level)163f8aea701SLong Li static inline bool do_logging(int level)
164f8aea701SLong Li {
165f8aea701SLong Li 	return logging_level >= level;
166f8aea701SLong Li }
167f8aea701SLong Li 
168f8aea701SLong Li #define storvsc_log(dev, level, fmt, ...)			\
169f8aea701SLong Li do {								\
170f8aea701SLong Li 	if (do_logging(level))					\
171f8aea701SLong Li 		dev_warn(&(dev)->device, fmt, ##__VA_ARGS__);	\
172f8aea701SLong Li } while (0)
173f8aea701SLong Li 
174088bde86SEaswar Hariharan #define storvsc_log_ratelimited(dev, level, fmt, ...)				\
175088bde86SEaswar Hariharan do {										\
176088bde86SEaswar Hariharan 	if (do_logging(level))							\
177088bde86SEaswar Hariharan 		dev_warn_ratelimited(&(dev)->device, fmt, ##__VA_ARGS__);	\
178088bde86SEaswar Hariharan } while (0)
179088bde86SEaswar Hariharan 
18089ae7d70SK. Y. Srinivasan struct vmscsi_request {
18189ae7d70SK. Y. Srinivasan 	u16 length;
18289ae7d70SK. Y. Srinivasan 	u8 srb_status;
18389ae7d70SK. Y. Srinivasan 	u8 scsi_status;
18489ae7d70SK. Y. Srinivasan 
18589ae7d70SK. Y. Srinivasan 	u8  port_number;
18689ae7d70SK. Y. Srinivasan 	u8  path_id;
18789ae7d70SK. Y. Srinivasan 	u8  target_id;
18889ae7d70SK. Y. Srinivasan 	u8  lun;
18989ae7d70SK. Y. Srinivasan 
19089ae7d70SK. Y. Srinivasan 	u8  cdb_length;
19189ae7d70SK. Y. Srinivasan 	u8  sense_info_length;
19289ae7d70SK. Y. Srinivasan 	u8  data_in;
19389ae7d70SK. Y. Srinivasan 	u8  reserved;
19489ae7d70SK. Y. Srinivasan 
19589ae7d70SK. Y. Srinivasan 	u32 data_transfer_length;
19689ae7d70SK. Y. Srinivasan 
19789ae7d70SK. Y. Srinivasan 	union {
19889ae7d70SK. Y. Srinivasan 		u8 cdb[STORVSC_MAX_CMD_LEN];
19989ae7d70SK. Y. Srinivasan 		u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
20089ae7d70SK. Y. Srinivasan 		u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
20189ae7d70SK. Y. Srinivasan 	};
2028b612fa2SK. Y. Srinivasan 	/*
2038b612fa2SK. Y. Srinivasan 	 * The following was added in win8.
2048b612fa2SK. Y. Srinivasan 	 */
20586c8fb4dSSaurabh Sengar 	u16 reserve;
20686c8fb4dSSaurabh Sengar 	u8  queue_tag;
20786c8fb4dSSaurabh Sengar 	u8  queue_action;
20886c8fb4dSSaurabh Sengar 	u32 srb_flags;
20986c8fb4dSSaurabh Sengar 	u32 time_out_value;
21086c8fb4dSSaurabh Sengar 	u32 queue_sort_ey;
2118b612fa2SK. Y. Srinivasan 
21289ae7d70SK. Y. Srinivasan } __attribute((packed));
21389ae7d70SK. Y. Srinivasan 
214cb11feadSKeith Mange /*
21586c8fb4dSSaurabh Sengar  * The list of windows version in order of preference.
2161a363108SKeith Mange  */
2171a363108SKeith Mange 
21886c8fb4dSSaurabh Sengar static const int protocol_version[] = {
2191a363108SKeith Mange 		VMSTOR_PROTO_VERSION_WIN10,
2201a363108SKeith Mange 		VMSTOR_PROTO_VERSION_WIN8_1,
2211a363108SKeith Mange 		VMSTOR_PROTO_VERSION_WIN8,
2221a363108SKeith Mange };
2231a363108SKeith Mange 
2241a363108SKeith Mange 
2251a363108SKeith Mange /*
226183b8021SMasahiro Yamada  * This structure is sent during the initialization phase to get the different
22789ae7d70SK. Y. Srinivasan  * properties of the channel.
22889ae7d70SK. Y. Srinivasan  */
22989ae7d70SK. Y. Srinivasan 
2308b612fa2SK. Y. Srinivasan #define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL		0x1
2318b612fa2SK. Y. Srinivasan 
2328b612fa2SK. Y. Srinivasan struct vmstorage_channel_properties {
2338b612fa2SK. Y. Srinivasan 	u32 reserved;
2348b612fa2SK. Y. Srinivasan 	u16 max_channel_cnt;
2358b612fa2SK. Y. Srinivasan 	u16 reserved1;
2368b612fa2SK. Y. Srinivasan 
23789ae7d70SK. Y. Srinivasan 	u32 flags;
23889ae7d70SK. Y. Srinivasan 	u32   max_transfer_bytes;
23989ae7d70SK. Y. Srinivasan 
2408b612fa2SK. Y. Srinivasan 	u64  reserved2;
24189ae7d70SK. Y. Srinivasan } __packed;
24289ae7d70SK. Y. Srinivasan 
24389ae7d70SK. Y. Srinivasan /*  This structure is sent during the storage protocol negotiations. */
24489ae7d70SK. Y. Srinivasan struct vmstorage_protocol_version {
24589ae7d70SK. Y. Srinivasan 	/* Major (MSW) and minor (LSW) version numbers. */
24689ae7d70SK. Y. Srinivasan 	u16 major_minor;
24789ae7d70SK. Y. Srinivasan 
24889ae7d70SK. Y. Srinivasan 	/*
24989ae7d70SK. Y. Srinivasan 	 * Revision number is auto-incremented whenever this file is changed
25089ae7d70SK. Y. Srinivasan 	 * (See FILL_VMSTOR_REVISION macro above).  Mismatch does not
25189ae7d70SK. Y. Srinivasan 	 * definitely indicate incompatibility--but it does indicate mismatched
25289ae7d70SK. Y. Srinivasan 	 * builds.
25389ae7d70SK. Y. Srinivasan 	 * This is only used on the windows side. Just set it to 0.
25489ae7d70SK. Y. Srinivasan 	 */
25589ae7d70SK. Y. Srinivasan 	u16 revision;
25689ae7d70SK. Y. Srinivasan } __packed;
25789ae7d70SK. Y. Srinivasan 
25889ae7d70SK. Y. Srinivasan /* Channel Property Flags */
25989ae7d70SK. Y. Srinivasan #define STORAGE_CHANNEL_REMOVABLE_FLAG		0x1
26089ae7d70SK. Y. Srinivasan #define STORAGE_CHANNEL_EMULATED_IDE_FLAG	0x2
26189ae7d70SK. Y. Srinivasan 
26289ae7d70SK. Y. Srinivasan struct vstor_packet {
26389ae7d70SK. Y. Srinivasan 	/* Requested operation type */
26489ae7d70SK. Y. Srinivasan 	enum vstor_packet_operation operation;
26589ae7d70SK. Y. Srinivasan 
26689ae7d70SK. Y. Srinivasan 	/*  Flags - see below for values */
26789ae7d70SK. Y. Srinivasan 	u32 flags;
26889ae7d70SK. Y. Srinivasan 
26989ae7d70SK. Y. Srinivasan 	/* Status of the request returned from the server side. */
27089ae7d70SK. Y. Srinivasan 	u32 status;
27189ae7d70SK. Y. Srinivasan 
27289ae7d70SK. Y. Srinivasan 	/* Data payload area */
27389ae7d70SK. Y. Srinivasan 	union {
27489ae7d70SK. Y. Srinivasan 		/*
27589ae7d70SK. Y. Srinivasan 		 * Structure used to forward SCSI commands from the
27689ae7d70SK. Y. Srinivasan 		 * client to the server.
27789ae7d70SK. Y. Srinivasan 		 */
27889ae7d70SK. Y. Srinivasan 		struct vmscsi_request vm_srb;
27989ae7d70SK. Y. Srinivasan 
28089ae7d70SK. Y. Srinivasan 		/* Structure used to query channel properties. */
28189ae7d70SK. Y. Srinivasan 		struct vmstorage_channel_properties storage_channel_properties;
28289ae7d70SK. Y. Srinivasan 
28389ae7d70SK. Y. Srinivasan 		/* Used during version negotiations. */
28489ae7d70SK. Y. Srinivasan 		struct vmstorage_protocol_version version;
2858b612fa2SK. Y. Srinivasan 
2868b612fa2SK. Y. Srinivasan 		/* Fibre channel address packet */
2878b612fa2SK. Y. Srinivasan 		struct hv_fc_wwn_packet wwn_packet;
2888b612fa2SK. Y. Srinivasan 
2898b612fa2SK. Y. Srinivasan 		/* Number of sub-channels to create */
2908b612fa2SK. Y. Srinivasan 		u16 sub_channel_count;
2918b612fa2SK. Y. Srinivasan 
2928b612fa2SK. Y. Srinivasan 		/* This will be the maximum of the union members */
2938b612fa2SK. Y. Srinivasan 		u8  buffer[0x34];
29489ae7d70SK. Y. Srinivasan 	};
29589ae7d70SK. Y. Srinivasan } __packed;
29689ae7d70SK. Y. Srinivasan 
29789ae7d70SK. Y. Srinivasan /*
29889ae7d70SK. Y. Srinivasan  * Packet Flags:
29989ae7d70SK. Y. Srinivasan  *
30089ae7d70SK. Y. Srinivasan  * This flag indicates that the server should send back a completion for this
30189ae7d70SK. Y. Srinivasan  * packet.
30289ae7d70SK. Y. Srinivasan  */
30389ae7d70SK. Y. Srinivasan 
30489ae7d70SK. Y. Srinivasan #define REQUEST_COMPLETION_FLAG	0x1
30589ae7d70SK. Y. Srinivasan 
30689ae7d70SK. Y. Srinivasan /* Matches Windows-end */
30789ae7d70SK. Y. Srinivasan enum storvsc_request_type {
30889ae7d70SK. Y. Srinivasan 	WRITE_TYPE = 0,
30989ae7d70SK. Y. Srinivasan 	READ_TYPE,
31089ae7d70SK. Y. Srinivasan 	UNKNOWN_TYPE,
31189ae7d70SK. Y. Srinivasan };
31289ae7d70SK. Y. Srinivasan 
31389ae7d70SK. Y. Srinivasan /*
314b8a5376cSMichael Kelley  * SRB status codes and masks. In the 8-bit field, the two high order bits
315b8a5376cSMichael Kelley  * are flags, while the remaining 6 bits are an integer status code.  The
316b8a5376cSMichael Kelley  * definitions here include only the subset of the integer status codes that
317b8a5376cSMichael Kelley  * are tested for in this driver.
31889ae7d70SK. Y. Srinivasan  */
31989ae7d70SK. Y. Srinivasan #define SRB_STATUS_AUTOSENSE_VALID	0x80
3203209f9d7SK. Y. Srinivasan #define SRB_STATUS_QUEUE_FROZEN		0x40
321b8a5376cSMichael Kelley 
322b8a5376cSMichael Kelley /* SRB status integer codes */
32389ae7d70SK. Y. Srinivasan #define SRB_STATUS_SUCCESS		0x01
3246781209eSK. Y. Srinivasan #define SRB_STATUS_ABORTED		0x02
32589ae7d70SK. Y. Srinivasan #define SRB_STATUS_ERROR		0x04
326b8a5376cSMichael Kelley #define SRB_STATUS_INVALID_REQUEST	0x06
327812fe642SMichael Kelley #define SRB_STATUS_TIMEOUT		0x09
328812fe642SMichael Kelley #define SRB_STATUS_SELECTION_TIMEOUT	0x0A
329812fe642SMichael Kelley #define SRB_STATUS_BUS_RESET		0x0E
33040630f46SLong Li #define SRB_STATUS_DATA_OVERRUN		0x12
331b8a5376cSMichael Kelley #define SRB_STATUS_INVALID_LUN		0x20
332f4d1a8e0SMichael Kelley #define SRB_STATUS_INTERNAL_ERROR	0x30
33389ae7d70SK. Y. Srinivasan 
3343209f9d7SK. Y. Srinivasan #define SRB_STATUS(status) \
3353209f9d7SK. Y. Srinivasan 	(status & ~(SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_QUEUE_FROZEN))
33689ae7d70SK. Y. Srinivasan /*
33789ae7d70SK. Y. Srinivasan  * This is the end of Protocol specific defines.
33889ae7d70SK. Y. Srinivasan  */
33989ae7d70SK. Y. Srinivasan 
340ed2e63aaSMichael Kelley static int storvsc_ringbuffer_size = (128 * 1024);
341acd2eda2SMichael Kelley static int aligned_ringbuffer_size;
342f458aadaSK. Y. Srinivasan static u32 max_outstanding_req_per_channel;
343adfbd028SBranden Bonaby static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth);
344f458aadaSK. Y. Srinivasan 
345f458aadaSK. Y. Srinivasan static int storvsc_vcpus_per_sub_channel = 4;
346a81a38ccSMelanie Plageman (Microsoft) static unsigned int storvsc_max_hw_queues;
34789ae7d70SK. Y. Srinivasan 
34889ae7d70SK. Y. Srinivasan module_param(storvsc_ringbuffer_size, int, S_IRUGO);
34989ae7d70SK. Y. Srinivasan MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
35089ae7d70SK. Y. Srinivasan 
351a81a38ccSMelanie Plageman (Microsoft) module_param(storvsc_max_hw_queues, uint, 0644);
352a81a38ccSMelanie Plageman (Microsoft) MODULE_PARM_DESC(storvsc_max_hw_queues, "Maximum number of hardware queues");
353a81a38ccSMelanie Plageman (Microsoft) 
354f458aadaSK. Y. Srinivasan module_param(storvsc_vcpus_per_sub_channel, int, S_IRUGO);
35574e26784SDan Carpenter MODULE_PARM_DESC(storvsc_vcpus_per_sub_channel, "Ratio of VCPUs to subchannels");
3562217a47dSLong Li 
3572217a47dSLong Li static int ring_avail_percent_lowater = 10;
3582217a47dSLong Li module_param(ring_avail_percent_lowater, int, S_IRUGO);
3592217a47dSLong Li MODULE_PARM_DESC(ring_avail_percent_lowater,
3602217a47dSLong Li 		"Select a channel if available ring size > this in percent");
3612217a47dSLong Li 
362893def38SK. Y. Srinivasan /*
363893def38SK. Y. Srinivasan  * Timeout in seconds for all devices managed by this driver.
364893def38SK. Y. Srinivasan  */
365893def38SK. Y. Srinivasan static int storvsc_timeout = 180;
366893def38SK. Y. Srinivasan 
367dac58241SK. Y. Srinivasan #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
368dac58241SK. Y. Srinivasan static struct scsi_transport_template *fc_transport_template;
369dac58241SK. Y. Srinivasan #endif
37089ae7d70SK. Y. Srinivasan 
371453de21cSAndres Beltran static struct scsi_host_template scsi_driver;
3726f94d5deSK. Y. Srinivasan static void storvsc_on_channel_callback(void *context);
3736f94d5deSK. Y. Srinivasan 
3744cd83ecdSK. Y. Srinivasan #define STORVSC_MAX_LUNS_PER_TARGET			255
3754cd83ecdSK. Y. Srinivasan #define STORVSC_MAX_TARGETS				2
3764cd83ecdSK. Y. Srinivasan #define STORVSC_MAX_CHANNELS				8
37789ae7d70SK. Y. Srinivasan 
3784cd83ecdSK. Y. Srinivasan #define STORVSC_FC_MAX_LUNS_PER_TARGET			255
3794cd83ecdSK. Y. Srinivasan #define STORVSC_FC_MAX_TARGETS				128
3804cd83ecdSK. Y. Srinivasan #define STORVSC_FC_MAX_CHANNELS				8
381010c1e1cSMichael Kelley #define STORVSC_FC_MAX_XFER_SIZE			((u32)(512 * 1024))
38289ae7d70SK. Y. Srinivasan 
3834cd83ecdSK. Y. Srinivasan #define STORVSC_IDE_MAX_LUNS_PER_TARGET			64
3844cd83ecdSK. Y. Srinivasan #define STORVSC_IDE_MAX_TARGETS				1
3854cd83ecdSK. Y. Srinivasan #define STORVSC_IDE_MAX_CHANNELS			1
38689ae7d70SK. Y. Srinivasan 
387adae1e93SAndres Beltran /*
38886c8fb4dSSaurabh Sengar  * Upper bound on the size of a storvsc packet.
389adae1e93SAndres Beltran  */
390adae1e93SAndres Beltran #define STORVSC_MAX_PKT_SIZE (sizeof(struct vmpacket_descriptor) +\
391adae1e93SAndres Beltran 			      sizeof(struct vstor_packet))
392adae1e93SAndres Beltran 
39389ae7d70SK. Y. Srinivasan struct storvsc_cmd_request {
39489ae7d70SK. Y. Srinivasan 	struct scsi_cmnd *cmd;
39589ae7d70SK. Y. Srinivasan 
39689ae7d70SK. Y. Srinivasan 	struct hv_device *device;
39789ae7d70SK. Y. Srinivasan 
39889ae7d70SK. Y. Srinivasan 	/* Synchronize the request/response if needed */
39989ae7d70SK. Y. Srinivasan 	struct completion wait_event;
40089ae7d70SK. Y. Srinivasan 
401be0cf6caSK. Y. Srinivasan 	struct vmbus_channel_packet_multipage_buffer mpb;
402be0cf6caSK. Y. Srinivasan 	struct vmbus_packet_mpb_array *payload;
403be0cf6caSK. Y. Srinivasan 	u32 payload_sz;
404be0cf6caSK. Y. Srinivasan 
40589ae7d70SK. Y. Srinivasan 	struct vstor_packet vstor_packet;
40689ae7d70SK. Y. Srinivasan };
40789ae7d70SK. Y. Srinivasan 
40889ae7d70SK. Y. Srinivasan 
40989ae7d70SK. Y. Srinivasan /* A storvsc device is a device object that contains a vmbus channel */
41089ae7d70SK. Y. Srinivasan struct storvsc_device {
41189ae7d70SK. Y. Srinivasan 	struct hv_device *device;
41289ae7d70SK. Y. Srinivasan 
41389ae7d70SK. Y. Srinivasan 	bool	 destroy;
41489ae7d70SK. Y. Srinivasan 	bool	 drain_notify;
41589ae7d70SK. Y. Srinivasan 	atomic_t num_outstanding_req;
41689ae7d70SK. Y. Srinivasan 	struct Scsi_Host *host;
41789ae7d70SK. Y. Srinivasan 
41889ae7d70SK. Y. Srinivasan 	wait_queue_head_t waiting_to_drain;
41989ae7d70SK. Y. Srinivasan 
42089ae7d70SK. Y. Srinivasan 	/*
42189ae7d70SK. Y. Srinivasan 	 * Each unique Port/Path/Target represents 1 channel ie scsi
42289ae7d70SK. Y. Srinivasan 	 * controller. In reality, the pathid, targetid is always 0
42389ae7d70SK. Y. Srinivasan 	 * and the port is set by us
42489ae7d70SK. Y. Srinivasan 	 */
42589ae7d70SK. Y. Srinivasan 	unsigned int port_number;
42689ae7d70SK. Y. Srinivasan 	unsigned char path_id;
42789ae7d70SK. Y. Srinivasan 	unsigned char target_id;
42889ae7d70SK. Y. Srinivasan 
4295117b936SK. Y. Srinivasan 	/*
4305117b936SK. Y. Srinivasan 	 * Max I/O, the device can support.
4315117b936SK. Y. Srinivasan 	 */
4325117b936SK. Y. Srinivasan 	u32   max_transfer_bytes;
433d86adf48SK. Y. Srinivasan 	/*
434d86adf48SK. Y. Srinivasan 	 * Number of sub-channels we will open.
435d86adf48SK. Y. Srinivasan 	 */
436d86adf48SK. Y. Srinivasan 	u16 num_sc;
437d86adf48SK. Y. Srinivasan 	struct vmbus_channel **stor_chns;
438d86adf48SK. Y. Srinivasan 	/*
439d86adf48SK. Y. Srinivasan 	 * Mask of CPUs bound to subchannels.
440d86adf48SK. Y. Srinivasan 	 */
441d86adf48SK. Y. Srinivasan 	struct cpumask alloced_cpus;
44221d2052cSAndrea Parri (Microsoft) 	/*
44321d2052cSAndrea Parri (Microsoft) 	 * Serializes modifications of stor_chns[] from storvsc_do_io()
44421d2052cSAndrea Parri (Microsoft) 	 * and storvsc_change_target_cpu().
44521d2052cSAndrea Parri (Microsoft) 	 */
44621d2052cSAndrea Parri (Microsoft) 	spinlock_t lock;
44789ae7d70SK. Y. Srinivasan 	/* Used for vsc/vsp channel reset process */
44889ae7d70SK. Y. Srinivasan 	struct storvsc_cmd_request init_request;
44989ae7d70SK. Y. Srinivasan 	struct storvsc_cmd_request reset_request;
450dac58241SK. Y. Srinivasan 	/*
451dac58241SK. Y. Srinivasan 	 * Currently active port and node names for FC devices.
452dac58241SK. Y. Srinivasan 	 */
453dac58241SK. Y. Srinivasan 	u64 node_name;
454dac58241SK. Y. Srinivasan 	u64 port_name;
455daf0cd44SCathy Avery #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
456daf0cd44SCathy Avery 	struct fc_rport *rport;
457daf0cd44SCathy Avery #endif
45889ae7d70SK. Y. Srinivasan };
45989ae7d70SK. Y. Srinivasan 
46089ae7d70SK. Y. Srinivasan struct hv_host_device {
46189ae7d70SK. Y. Srinivasan 	struct hv_device *dev;
46289ae7d70SK. Y. Srinivasan 	unsigned int port;
46389ae7d70SK. Y. Srinivasan 	unsigned char path;
46489ae7d70SK. Y. Srinivasan 	unsigned char target;
465436ad941SCathy Avery 	struct workqueue_struct *handle_error_wq;
466c58cc70fSLong Li 	struct work_struct host_scan_work;
467c58cc70fSLong Li 	struct Scsi_Host *host;
46889ae7d70SK. Y. Srinivasan };
46989ae7d70SK. Y. Srinivasan 
47089ae7d70SK. Y. Srinivasan struct storvsc_scan_work {
47189ae7d70SK. Y. Srinivasan 	struct work_struct work;
47289ae7d70SK. Y. Srinivasan 	struct Scsi_Host *host;
47398441221SK. Y. Srinivasan 	u8 lun;
47498441221SK. Y. Srinivasan 	u8 tgt_id;
47589ae7d70SK. Y. Srinivasan };
47689ae7d70SK. Y. Srinivasan 
storvsc_device_scan(struct work_struct * work)4776781209eSK. Y. Srinivasan static void storvsc_device_scan(struct work_struct *work)
4786781209eSK. Y. Srinivasan {
4796781209eSK. Y. Srinivasan 	struct storvsc_scan_work *wrk;
4806781209eSK. Y. Srinivasan 	struct scsi_device *sdev;
4816781209eSK. Y. Srinivasan 
4826781209eSK. Y. Srinivasan 	wrk = container_of(work, struct storvsc_scan_work, work);
4836781209eSK. Y. Srinivasan 
48498441221SK. Y. Srinivasan 	sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
4856781209eSK. Y. Srinivasan 	if (!sdev)
4866781209eSK. Y. Srinivasan 		goto done;
48779519528SBart Van Assche 	scsi_rescan_device(sdev);
4886781209eSK. Y. Srinivasan 	scsi_device_put(sdev);
4896781209eSK. Y. Srinivasan 
4906781209eSK. Y. Srinivasan done:
4916781209eSK. Y. Srinivasan 	kfree(wrk);
4926781209eSK. Y. Srinivasan }
4936781209eSK. Y. Srinivasan 
storvsc_host_scan(struct work_struct * work)4942a09ed3dSK. Y. Srinivasan static void storvsc_host_scan(struct work_struct *work)
49589ae7d70SK. Y. Srinivasan {
4962a09ed3dSK. Y. Srinivasan 	struct Scsi_Host *host;
49734a716bcSK. Y. Srinivasan 	struct scsi_device *sdev;
498c58cc70fSLong Li 	struct hv_host_device *host_device =
499c58cc70fSLong Li 		container_of(work, struct hv_host_device, host_scan_work);
50089ae7d70SK. Y. Srinivasan 
501c58cc70fSLong Li 	host = host_device->host;
50234a716bcSK. Y. Srinivasan 	/*
50334a716bcSK. Y. Srinivasan 	 * Before scanning the host, first check to see if any of the
5045445e08eSJulia Lawall 	 * currently known devices have been hot removed. We issue a
50534a716bcSK. Y. Srinivasan 	 * "unit ready" command against all currently known devices.
50634a716bcSK. Y. Srinivasan 	 * This I/O will result in an error for devices that have been
50734a716bcSK. Y. Srinivasan 	 * removed. As part of handling the I/O error, we remove the device.
50834a716bcSK. Y. Srinivasan 	 *
50934a716bcSK. Y. Srinivasan 	 * When a LUN is added or removed, the host sends us a signal to
51034a716bcSK. Y. Srinivasan 	 * scan the host. Thus we are forced to discover the LUNs that
51134a716bcSK. Y. Srinivasan 	 * may have been removed this way.
51234a716bcSK. Y. Srinivasan 	 */
51334a716bcSK. Y. Srinivasan 	mutex_lock(&host->scan_mutex);
5148d6a9f56SVitaly Kuznetsov 	shost_for_each_device(sdev, host)
51534a716bcSK. Y. Srinivasan 		scsi_test_unit_ready(sdev, 1, 1, NULL);
51634a716bcSK. Y. Srinivasan 	mutex_unlock(&host->scan_mutex);
51734a716bcSK. Y. Srinivasan 	/*
51834a716bcSK. Y. Srinivasan 	 * Now scan the host to discover LUNs that may have been added.
51934a716bcSK. Y. Srinivasan 	 */
5202a09ed3dSK. Y. Srinivasan 	scsi_scan_host(host);
52189ae7d70SK. Y. Srinivasan }
52289ae7d70SK. Y. Srinivasan 
storvsc_remove_lun(struct work_struct * work)52389ae7d70SK. Y. Srinivasan static void storvsc_remove_lun(struct work_struct *work)
52489ae7d70SK. Y. Srinivasan {
52589ae7d70SK. Y. Srinivasan 	struct storvsc_scan_work *wrk;
52689ae7d70SK. Y. Srinivasan 	struct scsi_device *sdev;
52789ae7d70SK. Y. Srinivasan 
52889ae7d70SK. Y. Srinivasan 	wrk = container_of(work, struct storvsc_scan_work, work);
52989ae7d70SK. Y. Srinivasan 	if (!scsi_host_get(wrk->host))
53089ae7d70SK. Y. Srinivasan 		goto done;
53189ae7d70SK. Y. Srinivasan 
53298441221SK. Y. Srinivasan 	sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
53389ae7d70SK. Y. Srinivasan 
53489ae7d70SK. Y. Srinivasan 	if (sdev) {
53589ae7d70SK. Y. Srinivasan 		scsi_remove_device(sdev);
53689ae7d70SK. Y. Srinivasan 		scsi_device_put(sdev);
53789ae7d70SK. Y. Srinivasan 	}
53889ae7d70SK. Y. Srinivasan 	scsi_host_put(wrk->host);
53989ae7d70SK. Y. Srinivasan 
54089ae7d70SK. Y. Srinivasan done:
54189ae7d70SK. Y. Srinivasan 	kfree(wrk);
54289ae7d70SK. Y. Srinivasan }
54389ae7d70SK. Y. Srinivasan 
54489ae7d70SK. Y. Srinivasan 
54589ae7d70SK. Y. Srinivasan /*
54689ae7d70SK. Y. Srinivasan  * We can get incoming messages from the host that are not in response to
54789ae7d70SK. Y. Srinivasan  * messages that we have sent out. An example of this would be messages
54889ae7d70SK. Y. Srinivasan  * received by the guest to notify dynamic addition/removal of LUNs. To
54989ae7d70SK. Y. Srinivasan  * deal with potential race conditions where the driver may be in the
55089ae7d70SK. Y. Srinivasan  * midst of being unloaded when we might receive an unsolicited message
55189ae7d70SK. Y. Srinivasan  * from the host, we have implemented a mechanism to gurantee sequential
55289ae7d70SK. Y. Srinivasan  * consistency:
55389ae7d70SK. Y. Srinivasan  *
55489ae7d70SK. Y. Srinivasan  * 1) Once the device is marked as being destroyed, we will fail all
55589ae7d70SK. Y. Srinivasan  *    outgoing messages.
55689ae7d70SK. Y. Srinivasan  * 2) We permit incoming messages when the device is being destroyed,
55789ae7d70SK. Y. Srinivasan  *    only to properly account for messages already sent out.
55889ae7d70SK. Y. Srinivasan  */
55989ae7d70SK. Y. Srinivasan 
get_out_stor_device(struct hv_device * device)56089ae7d70SK. Y. Srinivasan static inline struct storvsc_device *get_out_stor_device(
56189ae7d70SK. Y. Srinivasan 					struct hv_device *device)
56289ae7d70SK. Y. Srinivasan {
56389ae7d70SK. Y. Srinivasan 	struct storvsc_device *stor_device;
56489ae7d70SK. Y. Srinivasan 
56589ae7d70SK. Y. Srinivasan 	stor_device = hv_get_drvdata(device);
56689ae7d70SK. Y. Srinivasan 
56789ae7d70SK. Y. Srinivasan 	if (stor_device && stor_device->destroy)
56889ae7d70SK. Y. Srinivasan 		stor_device = NULL;
56989ae7d70SK. Y. Srinivasan 
57089ae7d70SK. Y. Srinivasan 	return stor_device;
57189ae7d70SK. Y. Srinivasan }
57289ae7d70SK. Y. Srinivasan 
57389ae7d70SK. Y. Srinivasan 
storvsc_wait_to_drain(struct storvsc_device * dev)57489ae7d70SK. Y. Srinivasan static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
57589ae7d70SK. Y. Srinivasan {
57689ae7d70SK. Y. Srinivasan 	dev->drain_notify = true;
57789ae7d70SK. Y. Srinivasan 	wait_event(dev->waiting_to_drain,
57889ae7d70SK. Y. Srinivasan 		   atomic_read(&dev->num_outstanding_req) == 0);
57989ae7d70SK. Y. Srinivasan 	dev->drain_notify = false;
58089ae7d70SK. Y. Srinivasan }
58189ae7d70SK. Y. Srinivasan 
get_in_stor_device(struct hv_device * device)58289ae7d70SK. Y. Srinivasan static inline struct storvsc_device *get_in_stor_device(
58389ae7d70SK. Y. Srinivasan 					struct hv_device *device)
58489ae7d70SK. Y. Srinivasan {
58589ae7d70SK. Y. Srinivasan 	struct storvsc_device *stor_device;
58689ae7d70SK. Y. Srinivasan 
58789ae7d70SK. Y. Srinivasan 	stor_device = hv_get_drvdata(device);
58889ae7d70SK. Y. Srinivasan 
58989ae7d70SK. Y. Srinivasan 	if (!stor_device)
59089ae7d70SK. Y. Srinivasan 		goto get_in_err;
59189ae7d70SK. Y. Srinivasan 
59289ae7d70SK. Y. Srinivasan 	/*
59389ae7d70SK. Y. Srinivasan 	 * If the device is being destroyed; allow incoming
59489ae7d70SK. Y. Srinivasan 	 * traffic only to cleanup outstanding requests.
59589ae7d70SK. Y. Srinivasan 	 */
59689ae7d70SK. Y. Srinivasan 
59789ae7d70SK. Y. Srinivasan 	if (stor_device->destroy  &&
59889ae7d70SK. Y. Srinivasan 		(atomic_read(&stor_device->num_outstanding_req) == 0))
59989ae7d70SK. Y. Srinivasan 		stor_device = NULL;
60089ae7d70SK. Y. Srinivasan 
60189ae7d70SK. Y. Srinivasan get_in_err:
60289ae7d70SK. Y. Srinivasan 	return stor_device;
60389ae7d70SK. Y. Srinivasan 
60489ae7d70SK. Y. Srinivasan }
60589ae7d70SK. Y. Srinivasan 
storvsc_change_target_cpu(struct vmbus_channel * channel,u32 old,u32 new)6067769e18cSAndrea Parri (Microsoft) static void storvsc_change_target_cpu(struct vmbus_channel *channel, u32 old,
6077769e18cSAndrea Parri (Microsoft) 				      u32 new)
6087769e18cSAndrea Parri (Microsoft) {
6097769e18cSAndrea Parri (Microsoft) 	struct storvsc_device *stor_device;
6107769e18cSAndrea Parri (Microsoft) 	struct vmbus_channel *cur_chn;
6117769e18cSAndrea Parri (Microsoft) 	bool old_is_alloced = false;
6127769e18cSAndrea Parri (Microsoft) 	struct hv_device *device;
6137769e18cSAndrea Parri (Microsoft) 	unsigned long flags;
6147769e18cSAndrea Parri (Microsoft) 	int cpu;
6157769e18cSAndrea Parri (Microsoft) 
6167769e18cSAndrea Parri (Microsoft) 	device = channel->primary_channel ?
6177769e18cSAndrea Parri (Microsoft) 			channel->primary_channel->device_obj
6187769e18cSAndrea Parri (Microsoft) 				: channel->device_obj;
6197769e18cSAndrea Parri (Microsoft) 	stor_device = get_out_stor_device(device);
6207769e18cSAndrea Parri (Microsoft) 	if (!stor_device)
6217769e18cSAndrea Parri (Microsoft) 		return;
6227769e18cSAndrea Parri (Microsoft) 
6237769e18cSAndrea Parri (Microsoft) 	/* See storvsc_do_io() -> get_og_chn(). */
62421d2052cSAndrea Parri (Microsoft) 	spin_lock_irqsave(&stor_device->lock, flags);
6257769e18cSAndrea Parri (Microsoft) 
6267769e18cSAndrea Parri (Microsoft) 	/*
6277769e18cSAndrea Parri (Microsoft) 	 * Determines if the storvsc device has other channels assigned to
6287769e18cSAndrea Parri (Microsoft) 	 * the "old" CPU to update the alloced_cpus mask and the stor_chns
6297769e18cSAndrea Parri (Microsoft) 	 * array.
6307769e18cSAndrea Parri (Microsoft) 	 */
6317769e18cSAndrea Parri (Microsoft) 	if (device->channel != channel && device->channel->target_cpu == old) {
6327769e18cSAndrea Parri (Microsoft) 		cur_chn = device->channel;
6337769e18cSAndrea Parri (Microsoft) 		old_is_alloced = true;
6347769e18cSAndrea Parri (Microsoft) 		goto old_is_alloced;
6357769e18cSAndrea Parri (Microsoft) 	}
6367769e18cSAndrea Parri (Microsoft) 	list_for_each_entry(cur_chn, &device->channel->sc_list, sc_list) {
6377769e18cSAndrea Parri (Microsoft) 		if (cur_chn == channel)
6387769e18cSAndrea Parri (Microsoft) 			continue;
6397769e18cSAndrea Parri (Microsoft) 		if (cur_chn->target_cpu == old) {
6407769e18cSAndrea Parri (Microsoft) 			old_is_alloced = true;
6417769e18cSAndrea Parri (Microsoft) 			goto old_is_alloced;
6427769e18cSAndrea Parri (Microsoft) 		}
6437769e18cSAndrea Parri (Microsoft) 	}
6447769e18cSAndrea Parri (Microsoft) 
6457769e18cSAndrea Parri (Microsoft) old_is_alloced:
6467769e18cSAndrea Parri (Microsoft) 	if (old_is_alloced)
6477769e18cSAndrea Parri (Microsoft) 		WRITE_ONCE(stor_device->stor_chns[old], cur_chn);
6487769e18cSAndrea Parri (Microsoft) 	else
6497769e18cSAndrea Parri (Microsoft) 		cpumask_clear_cpu(old, &stor_device->alloced_cpus);
6507769e18cSAndrea Parri (Microsoft) 
6517769e18cSAndrea Parri (Microsoft) 	/* "Flush" the stor_chns array. */
6527769e18cSAndrea Parri (Microsoft) 	for_each_possible_cpu(cpu) {
6537769e18cSAndrea Parri (Microsoft) 		if (stor_device->stor_chns[cpu] && !cpumask_test_cpu(
6547769e18cSAndrea Parri (Microsoft) 					cpu, &stor_device->alloced_cpus))
6557769e18cSAndrea Parri (Microsoft) 			WRITE_ONCE(stor_device->stor_chns[cpu], NULL);
6567769e18cSAndrea Parri (Microsoft) 	}
6577769e18cSAndrea Parri (Microsoft) 
6587769e18cSAndrea Parri (Microsoft) 	WRITE_ONCE(stor_device->stor_chns[new], channel);
6597769e18cSAndrea Parri (Microsoft) 	cpumask_set_cpu(new, &stor_device->alloced_cpus);
6607769e18cSAndrea Parri (Microsoft) 
66121d2052cSAndrea Parri (Microsoft) 	spin_unlock_irqrestore(&stor_device->lock, flags);
6627769e18cSAndrea Parri (Microsoft) }
6637769e18cSAndrea Parri (Microsoft) 
storvsc_next_request_id(struct vmbus_channel * channel,u64 rqst_addr)664bf5fd8caSAndrea Parri (Microsoft) static u64 storvsc_next_request_id(struct vmbus_channel *channel, u64 rqst_addr)
665bf5fd8caSAndrea Parri (Microsoft) {
666bf5fd8caSAndrea Parri (Microsoft) 	struct storvsc_cmd_request *request =
667bf5fd8caSAndrea Parri (Microsoft) 		(struct storvsc_cmd_request *)(unsigned long)rqst_addr;
668bf5fd8caSAndrea Parri (Microsoft) 
669bf5fd8caSAndrea Parri (Microsoft) 	if (rqst_addr == VMBUS_RQST_INIT)
670bf5fd8caSAndrea Parri (Microsoft) 		return VMBUS_RQST_INIT;
671bf5fd8caSAndrea Parri (Microsoft) 	if (rqst_addr == VMBUS_RQST_RESET)
672bf5fd8caSAndrea Parri (Microsoft) 		return VMBUS_RQST_RESET;
673bf5fd8caSAndrea Parri (Microsoft) 
674bf5fd8caSAndrea Parri (Microsoft) 	/*
675bf5fd8caSAndrea Parri (Microsoft) 	 * Cannot return an ID of 0, which is reserved for an unsolicited
676bf5fd8caSAndrea Parri (Microsoft) 	 * message from Hyper-V.
677bf5fd8caSAndrea Parri (Microsoft) 	 */
678c5bf198cSMartin K. Petersen 	return (u64)blk_mq_unique_tag(scsi_cmd_to_rq(request->cmd)) + 1;
679bf5fd8caSAndrea Parri (Microsoft) }
680bf5fd8caSAndrea Parri (Microsoft) 
handle_sc_creation(struct vmbus_channel * new_sc)6816f94d5deSK. Y. Srinivasan static void handle_sc_creation(struct vmbus_channel *new_sc)
6826f94d5deSK. Y. Srinivasan {
6836f94d5deSK. Y. Srinivasan 	struct hv_device *device = new_sc->primary_channel->device_obj;
684c9675904SDexuan Cui 	struct device *dev = &device->device;
6856f94d5deSK. Y. Srinivasan 	struct storvsc_device *stor_device;
6866f94d5deSK. Y. Srinivasan 	struct vmstorage_channel_properties props;
687c9675904SDexuan Cui 	int ret;
6886f94d5deSK. Y. Srinivasan 
6896f94d5deSK. Y. Srinivasan 	stor_device = get_out_stor_device(device);
6906f94d5deSK. Y. Srinivasan 	if (!stor_device)
6916f94d5deSK. Y. Srinivasan 		return;
6926f94d5deSK. Y. Srinivasan 
6936f94d5deSK. Y. Srinivasan 	memset(&props, 0, sizeof(struct vmstorage_channel_properties));
694adae1e93SAndres Beltran 	new_sc->max_pkt_size = STORVSC_MAX_PKT_SIZE;
6956f94d5deSK. Y. Srinivasan 
696bf5fd8caSAndrea Parri (Microsoft) 	new_sc->next_request_id_callback = storvsc_next_request_id;
697453de21cSAndres Beltran 
698c9675904SDexuan Cui 	ret = vmbus_open(new_sc,
699acd2eda2SMichael Kelley 			 aligned_ringbuffer_size,
700acd2eda2SMichael Kelley 			 aligned_ringbuffer_size,
7016f94d5deSK. Y. Srinivasan 			 (void *)&props,
7026f94d5deSK. Y. Srinivasan 			 sizeof(struct vmstorage_channel_properties),
7036f94d5deSK. Y. Srinivasan 			 storvsc_on_channel_callback, new_sc);
704d86adf48SK. Y. Srinivasan 
705c9675904SDexuan Cui 	/* In case vmbus_open() fails, we don't use the sub-channel. */
706c9675904SDexuan Cui 	if (ret != 0) {
707c9675904SDexuan Cui 		dev_err(dev, "Failed to open sub-channel: err=%d\n", ret);
708c9675904SDexuan Cui 		return;
709c9675904SDexuan Cui 	}
710c9675904SDexuan Cui 
7117769e18cSAndrea Parri (Microsoft) 	new_sc->change_target_cpu_callback = storvsc_change_target_cpu;
7127769e18cSAndrea Parri (Microsoft) 
713c9675904SDexuan Cui 	/* Add the sub-channel to the array of available channels. */
714d86adf48SK. Y. Srinivasan 	stor_device->stor_chns[new_sc->target_cpu] = new_sc;
715d86adf48SK. Y. Srinivasan 	cpumask_set_cpu(new_sc->target_cpu, &stor_device->alloced_cpus);
716d86adf48SK. Y. Srinivasan }
7176f94d5deSK. Y. Srinivasan 
handle_multichannel_storage(struct hv_device * device,int max_chns)7186f94d5deSK. Y. Srinivasan static void  handle_multichannel_storage(struct hv_device *device, int max_chns)
7196f94d5deSK. Y. Srinivasan {
720c9675904SDexuan Cui 	struct device *dev = &device->device;
7216f94d5deSK. Y. Srinivasan 	struct storvsc_device *stor_device;
7226f94d5deSK. Y. Srinivasan 	int num_sc;
7236f94d5deSK. Y. Srinivasan 	struct storvsc_cmd_request *request;
7246f94d5deSK. Y. Srinivasan 	struct vstor_packet *vstor_packet;
7256f94d5deSK. Y. Srinivasan 	int ret, t;
7266f94d5deSK. Y. Srinivasan 
727382e06d1SMichael Kelley 	/*
728382e06d1SMichael Kelley 	 * If the number of CPUs is artificially restricted, such as
729382e06d1SMichael Kelley 	 * with maxcpus=1 on the kernel boot line, Hyper-V could offer
730382e06d1SMichael Kelley 	 * sub-channels >= the number of CPUs. These sub-channels
731382e06d1SMichael Kelley 	 * should not be created. The primary channel is already created
732382e06d1SMichael Kelley 	 * and assigned to one CPU, so check against # CPUs - 1.
733382e06d1SMichael Kelley 	 */
734382e06d1SMichael Kelley 	num_sc = min((int)(num_online_cpus() - 1), max_chns);
735382e06d1SMichael Kelley 	if (!num_sc)
736382e06d1SMichael Kelley 		return;
737382e06d1SMichael Kelley 
7386f94d5deSK. Y. Srinivasan 	stor_device = get_out_stor_device(device);
7396f94d5deSK. Y. Srinivasan 	if (!stor_device)
7406f94d5deSK. Y. Srinivasan 		return;
7416f94d5deSK. Y. Srinivasan 
742d86adf48SK. Y. Srinivasan 	stor_device->num_sc = num_sc;
7436f94d5deSK. Y. Srinivasan 	request = &stor_device->init_request;
7446f94d5deSK. Y. Srinivasan 	vstor_packet = &request->vstor_packet;
7456f94d5deSK. Y. Srinivasan 
7466f94d5deSK. Y. Srinivasan 	/*
7476f94d5deSK. Y. Srinivasan 	 * Establish a handler for dealing with subchannels.
7486f94d5deSK. Y. Srinivasan 	 */
7496f94d5deSK. Y. Srinivasan 	vmbus_set_sc_create_callback(device->channel, handle_sc_creation);
7506f94d5deSK. Y. Srinivasan 
7516f94d5deSK. Y. Srinivasan 	/*
7526f94d5deSK. Y. Srinivasan 	 * Request the host to create sub-channels.
7536f94d5deSK. Y. Srinivasan 	 */
7546f94d5deSK. Y. Srinivasan 	memset(request, 0, sizeof(struct storvsc_cmd_request));
7556f94d5deSK. Y. Srinivasan 	init_completion(&request->wait_event);
7566f94d5deSK. Y. Srinivasan 	vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
7576f94d5deSK. Y. Srinivasan 	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
7586f94d5deSK. Y. Srinivasan 	vstor_packet->sub_channel_count = num_sc;
7596f94d5deSK. Y. Srinivasan 
7606f94d5deSK. Y. Srinivasan 	ret = vmbus_sendpacket(device->channel, vstor_packet,
76186c8fb4dSSaurabh Sengar 			       sizeof(struct vstor_packet),
762bf5fd8caSAndrea Parri (Microsoft) 			       VMBUS_RQST_INIT,
7636f94d5deSK. Y. Srinivasan 			       VM_PKT_DATA_INBAND,
7646f94d5deSK. Y. Srinivasan 			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
7656f94d5deSK. Y. Srinivasan 
766c9675904SDexuan Cui 	if (ret != 0) {
767c9675904SDexuan Cui 		dev_err(dev, "Failed to create sub-channel: err=%d\n", ret);
7686f94d5deSK. Y. Srinivasan 		return;
769c9675904SDexuan Cui 	}
7706f94d5deSK. Y. Srinivasan 
7716f94d5deSK. Y. Srinivasan 	t = wait_for_completion_timeout(&request->wait_event, 10*HZ);
772c9675904SDexuan Cui 	if (t == 0) {
773c9675904SDexuan Cui 		dev_err(dev, "Failed to create sub-channel: timed out\n");
7746f94d5deSK. Y. Srinivasan 		return;
775c9675904SDexuan Cui 	}
7766f94d5deSK. Y. Srinivasan 
7776f94d5deSK. Y. Srinivasan 	if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
778c9675904SDexuan Cui 	    vstor_packet->status != 0) {
779c9675904SDexuan Cui 		dev_err(dev, "Failed to create sub-channel: op=%d, sts=%d\n",
780c9675904SDexuan Cui 			vstor_packet->operation, vstor_packet->status);
7816f94d5deSK. Y. Srinivasan 		return;
782c9675904SDexuan Cui 	}
7836f94d5deSK. Y. Srinivasan 
7846f94d5deSK. Y. Srinivasan 	/*
785c9675904SDexuan Cui 	 * We need to do nothing here, because vmbus_process_offer()
786c9675904SDexuan Cui 	 * invokes channel->sc_creation_callback, which will open and use
787c9675904SDexuan Cui 	 * the sub-channel(s).
7886f94d5deSK. Y. Srinivasan 	 */
7896f94d5deSK. Y. Srinivasan }
7906f94d5deSK. Y. Srinivasan 
cache_wwn(struct storvsc_device * stor_device,struct vstor_packet * vstor_packet)791dac58241SK. Y. Srinivasan static void cache_wwn(struct storvsc_device *stor_device,
792dac58241SK. Y. Srinivasan 		      struct vstor_packet *vstor_packet)
793dac58241SK. Y. Srinivasan {
794dac58241SK. Y. Srinivasan 	/*
795dac58241SK. Y. Srinivasan 	 * Cache the currently active port and node ww names.
796dac58241SK. Y. Srinivasan 	 */
797dac58241SK. Y. Srinivasan 	if (vstor_packet->wwn_packet.primary_active) {
798dac58241SK. Y. Srinivasan 		stor_device->node_name =
799dac58241SK. Y. Srinivasan 			wwn_to_u64(vstor_packet->wwn_packet.primary_node_wwn);
800dac58241SK. Y. Srinivasan 		stor_device->port_name =
801dac58241SK. Y. Srinivasan 			wwn_to_u64(vstor_packet->wwn_packet.primary_port_wwn);
802dac58241SK. Y. Srinivasan 	} else {
803dac58241SK. Y. Srinivasan 		stor_device->node_name =
804dac58241SK. Y. Srinivasan 			wwn_to_u64(vstor_packet->wwn_packet.secondary_node_wwn);
805dac58241SK. Y. Srinivasan 		stor_device->port_name =
806dac58241SK. Y. Srinivasan 			wwn_to_u64(vstor_packet->wwn_packet.secondary_port_wwn);
807dac58241SK. Y. Srinivasan 	}
808dac58241SK. Y. Srinivasan }
809dac58241SK. Y. Srinivasan 
81059635018SK. Y. Srinivasan 
storvsc_execute_vstor_op(struct hv_device * device,struct storvsc_cmd_request * request,bool status_check)81159635018SK. Y. Srinivasan static int storvsc_execute_vstor_op(struct hv_device *device,
81259635018SK. Y. Srinivasan 				    struct storvsc_cmd_request *request,
81359635018SK. Y. Srinivasan 				    bool status_check)
81459635018SK. Y. Srinivasan {
815244808e0SAndrea Parri (Microsoft) 	struct storvsc_device *stor_device;
81659635018SK. Y. Srinivasan 	struct vstor_packet *vstor_packet;
81759635018SK. Y. Srinivasan 	int ret, t;
81859635018SK. Y. Srinivasan 
819244808e0SAndrea Parri (Microsoft) 	stor_device = get_out_stor_device(device);
820244808e0SAndrea Parri (Microsoft) 	if (!stor_device)
821244808e0SAndrea Parri (Microsoft) 		return -ENODEV;
822244808e0SAndrea Parri (Microsoft) 
82359635018SK. Y. Srinivasan 	vstor_packet = &request->vstor_packet;
82459635018SK. Y. Srinivasan 
82559635018SK. Y. Srinivasan 	init_completion(&request->wait_event);
82659635018SK. Y. Srinivasan 	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
82759635018SK. Y. Srinivasan 
82859635018SK. Y. Srinivasan 	ret = vmbus_sendpacket(device->channel, vstor_packet,
82986c8fb4dSSaurabh Sengar 			       sizeof(struct vstor_packet),
830bf5fd8caSAndrea Parri (Microsoft) 			       VMBUS_RQST_INIT,
83159635018SK. Y. Srinivasan 			       VM_PKT_DATA_INBAND,
83259635018SK. Y. Srinivasan 			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
83359635018SK. Y. Srinivasan 	if (ret != 0)
83459635018SK. Y. Srinivasan 		return ret;
83559635018SK. Y. Srinivasan 
83659635018SK. Y. Srinivasan 	t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
83759635018SK. Y. Srinivasan 	if (t == 0)
83859635018SK. Y. Srinivasan 		return -ETIMEDOUT;
83959635018SK. Y. Srinivasan 
84059635018SK. Y. Srinivasan 	if (!status_check)
84159635018SK. Y. Srinivasan 		return ret;
84259635018SK. Y. Srinivasan 
84359635018SK. Y. Srinivasan 	if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
84459635018SK. Y. Srinivasan 	    vstor_packet->status != 0)
84559635018SK. Y. Srinivasan 		return -EINVAL;
84659635018SK. Y. Srinivasan 
84759635018SK. Y. Srinivasan 	return ret;
84859635018SK. Y. Srinivasan }
84959635018SK. Y. Srinivasan 
storvsc_channel_init(struct hv_device * device,bool is_fc)850dac58241SK. Y. Srinivasan static int storvsc_channel_init(struct hv_device *device, bool is_fc)
85189ae7d70SK. Y. Srinivasan {
85289ae7d70SK. Y. Srinivasan 	struct storvsc_device *stor_device;
85389ae7d70SK. Y. Srinivasan 	struct storvsc_cmd_request *request;
85489ae7d70SK. Y. Srinivasan 	struct vstor_packet *vstor_packet;
85559635018SK. Y. Srinivasan 	int ret, i;
8566f94d5deSK. Y. Srinivasan 	int max_chns;
8576f94d5deSK. Y. Srinivasan 	bool process_sub_channels = false;
85889ae7d70SK. Y. Srinivasan 
85989ae7d70SK. Y. Srinivasan 	stor_device = get_out_stor_device(device);
86089ae7d70SK. Y. Srinivasan 	if (!stor_device)
86189ae7d70SK. Y. Srinivasan 		return -ENODEV;
86289ae7d70SK. Y. Srinivasan 
86389ae7d70SK. Y. Srinivasan 	request = &stor_device->init_request;
86489ae7d70SK. Y. Srinivasan 	vstor_packet = &request->vstor_packet;
86589ae7d70SK. Y. Srinivasan 
86689ae7d70SK. Y. Srinivasan 	/*
86789ae7d70SK. Y. Srinivasan 	 * Now, initiate the vsc/vsp initialization protocol on the open
86889ae7d70SK. Y. Srinivasan 	 * channel
86989ae7d70SK. Y. Srinivasan 	 */
87089ae7d70SK. Y. Srinivasan 	memset(request, 0, sizeof(struct storvsc_cmd_request));
87189ae7d70SK. Y. Srinivasan 	vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
87259635018SK. Y. Srinivasan 	ret = storvsc_execute_vstor_op(device, request, true);
87359635018SK. Y. Srinivasan 	if (ret)
874dac58241SK. Y. Srinivasan 		return ret;
87559635018SK. Y. Srinivasan 	/*
87659635018SK. Y. Srinivasan 	 * Query host supported protocol version.
87759635018SK. Y. Srinivasan 	 */
87889ae7d70SK. Y. Srinivasan 
87986c8fb4dSSaurabh Sengar 	for (i = 0; i < ARRAY_SIZE(protocol_version); i++) {
88089ae7d70SK. Y. Srinivasan 		/* reuse the packet for version range supported */
88189ae7d70SK. Y. Srinivasan 		memset(vstor_packet, 0, sizeof(struct vstor_packet));
8821a363108SKeith Mange 		vstor_packet->operation =
8831a363108SKeith Mange 			VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
88489ae7d70SK. Y. Srinivasan 
88586c8fb4dSSaurabh Sengar 		vstor_packet->version.major_minor = protocol_version[i];
88689ae7d70SK. Y. Srinivasan 
88789ae7d70SK. Y. Srinivasan 		/*
88889ae7d70SK. Y. Srinivasan 		 * The revision number is only used in Windows; set it to 0.
88989ae7d70SK. Y. Srinivasan 		 */
89089ae7d70SK. Y. Srinivasan 		vstor_packet->version.revision = 0;
89159635018SK. Y. Srinivasan 		ret = storvsc_execute_vstor_op(device, request, false);
89289ae7d70SK. Y. Srinivasan 		if (ret != 0)
893dac58241SK. Y. Srinivasan 			return ret;
89489ae7d70SK. Y. Srinivasan 
895dac58241SK. Y. Srinivasan 		if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO)
896dac58241SK. Y. Srinivasan 			return -EINVAL;
8971a363108SKeith Mange 
8981a363108SKeith Mange 		if (vstor_packet->status == 0) {
89986c8fb4dSSaurabh Sengar 			vmstor_proto_version = protocol_version[i];
9001a363108SKeith Mange 
9011a363108SKeith Mange 			break;
9021a363108SKeith Mange 		}
9031a363108SKeith Mange 	}
9041a363108SKeith Mange 
90586c8fb4dSSaurabh Sengar 	if (vstor_packet->status != 0) {
90686c8fb4dSSaurabh Sengar 		dev_err(&device->device, "Obsolete Hyper-V version\n");
907dac58241SK. Y. Srinivasan 		return -EINVAL;
90886c8fb4dSSaurabh Sengar 	}
90989ae7d70SK. Y. Srinivasan 
91089ae7d70SK. Y. Srinivasan 
91189ae7d70SK. Y. Srinivasan 	memset(vstor_packet, 0, sizeof(struct vstor_packet));
91289ae7d70SK. Y. Srinivasan 	vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
91359635018SK. Y. Srinivasan 	ret = storvsc_execute_vstor_op(device, request, true);
91489ae7d70SK. Y. Srinivasan 	if (ret != 0)
915dac58241SK. Y. Srinivasan 		return ret;
91689ae7d70SK. Y. Srinivasan 
9176f94d5deSK. Y. Srinivasan 	/*
9186f94d5deSK. Y. Srinivasan 	 * Check to see if multi-channel support is there.
9196f94d5deSK. Y. Srinivasan 	 * Hosts that implement protocol version of 5.1 and above
9206f94d5deSK. Y. Srinivasan 	 * support multi-channel.
9216f94d5deSK. Y. Srinivasan 	 */
9226f94d5deSK. Y. Srinivasan 	max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
923d86adf48SK. Y. Srinivasan 
924d86adf48SK. Y. Srinivasan 	/*
925d86adf48SK. Y. Srinivasan 	 * Allocate state to manage the sub-channels.
926d86adf48SK. Y. Srinivasan 	 * We allocate an array based on the numbers of possible CPUs
927d86adf48SK. Y. Srinivasan 	 * (Hyper-V does not support cpu online/offline).
928d86adf48SK. Y. Srinivasan 	 * This Array will be sparseley populated with unique
929d86adf48SK. Y. Srinivasan 	 * channels - primary + sub-channels.
930d86adf48SK. Y. Srinivasan 	 * We will however populate all the slots to evenly distribute
931d86adf48SK. Y. Srinivasan 	 * the load.
932d86adf48SK. Y. Srinivasan 	 */
933e0408528SMiguel Bernal Marin 	stor_device->stor_chns = kcalloc(num_possible_cpus(), sizeof(void *),
934d86adf48SK. Y. Srinivasan 					 GFP_KERNEL);
935d86adf48SK. Y. Srinivasan 	if (stor_device->stor_chns == NULL)
936d86adf48SK. Y. Srinivasan 		return -ENOMEM;
937d86adf48SK. Y. Srinivasan 
9387769e18cSAndrea Parri (Microsoft) 	device->channel->change_target_cpu_callback = storvsc_change_target_cpu;
9397769e18cSAndrea Parri (Microsoft) 
940d86adf48SK. Y. Srinivasan 	stor_device->stor_chns[device->channel->target_cpu] = device->channel;
941d86adf48SK. Y. Srinivasan 	cpumask_set_cpu(device->channel->target_cpu,
942d86adf48SK. Y. Srinivasan 			&stor_device->alloced_cpus);
943d86adf48SK. Y. Srinivasan 
9446f94d5deSK. Y. Srinivasan 	if (vstor_packet->storage_channel_properties.flags &
9456f94d5deSK. Y. Srinivasan 	    STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
9466f94d5deSK. Y. Srinivasan 		process_sub_channels = true;
94786c8fb4dSSaurabh Sengar 
9485117b936SK. Y. Srinivasan 	stor_device->max_transfer_bytes =
9495117b936SK. Y. Srinivasan 		vstor_packet->storage_channel_properties.max_transfer_bytes;
9506f94d5deSK. Y. Srinivasan 
951dac58241SK. Y. Srinivasan 	if (!is_fc)
952dac58241SK. Y. Srinivasan 		goto done;
953dac58241SK. Y. Srinivasan 
95459635018SK. Y. Srinivasan 	/*
95559635018SK. Y. Srinivasan 	 * For FC devices retrieve FC HBA data.
95659635018SK. Y. Srinivasan 	 */
957dac58241SK. Y. Srinivasan 	memset(vstor_packet, 0, sizeof(struct vstor_packet));
958dac58241SK. Y. Srinivasan 	vstor_packet->operation = VSTOR_OPERATION_FCHBA_DATA;
95959635018SK. Y. Srinivasan 	ret = storvsc_execute_vstor_op(device, request, true);
960dac58241SK. Y. Srinivasan 	if (ret != 0)
961dac58241SK. Y. Srinivasan 		return ret;
962dac58241SK. Y. Srinivasan 
963dac58241SK. Y. Srinivasan 	/*
964dac58241SK. Y. Srinivasan 	 * Cache the currently active port and node ww names.
965dac58241SK. Y. Srinivasan 	 */
966dac58241SK. Y. Srinivasan 	cache_wwn(stor_device, vstor_packet);
967dac58241SK. Y. Srinivasan 
968dac58241SK. Y. Srinivasan done:
969dac58241SK. Y. Srinivasan 
97089ae7d70SK. Y. Srinivasan 	memset(vstor_packet, 0, sizeof(struct vstor_packet));
97189ae7d70SK. Y. Srinivasan 	vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
97259635018SK. Y. Srinivasan 	ret = storvsc_execute_vstor_op(device, request, true);
97389ae7d70SK. Y. Srinivasan 	if (ret != 0)
974dac58241SK. Y. Srinivasan 		return ret;
97589ae7d70SK. Y. Srinivasan 
9766f94d5deSK. Y. Srinivasan 	if (process_sub_channels)
9776f94d5deSK. Y. Srinivasan 		handle_multichannel_storage(device, max_chns);
9786f94d5deSK. Y. Srinivasan 
97989ae7d70SK. Y. Srinivasan 	return ret;
98089ae7d70SK. Y. Srinivasan }
98189ae7d70SK. Y. Srinivasan 
storvsc_handle_error(struct vmscsi_request * vm_srb,struct scsi_cmnd * scmnd,struct Scsi_Host * host,u8 asc,u8 ascq)982c50bd448SK. Y. Srinivasan static void storvsc_handle_error(struct vmscsi_request *vm_srb,
983c50bd448SK. Y. Srinivasan 				struct scsi_cmnd *scmnd,
984c50bd448SK. Y. Srinivasan 				struct Scsi_Host *host,
985c50bd448SK. Y. Srinivasan 				u8 asc, u8 ascq)
986c50bd448SK. Y. Srinivasan {
987c50bd448SK. Y. Srinivasan 	struct storvsc_scan_work *wrk;
988c50bd448SK. Y. Srinivasan 	void (*process_err_fn)(struct work_struct *work);
989436ad941SCathy Avery 	struct hv_host_device *host_dev = shost_priv(host);
990c50bd448SK. Y. Srinivasan 
991b8a5376cSMichael Kelley 	switch (SRB_STATUS(vm_srb->srb_status)) {
992b8a5376cSMichael Kelley 	case SRB_STATUS_ERROR:
993b8a5376cSMichael Kelley 	case SRB_STATUS_ABORTED:
994b8a5376cSMichael Kelley 	case SRB_STATUS_INVALID_REQUEST:
995f4d1a8e0SMichael Kelley 	case SRB_STATUS_INTERNAL_ERROR:
996812fe642SMichael Kelley 	case SRB_STATUS_TIMEOUT:
997812fe642SMichael Kelley 	case SRB_STATUS_SELECTION_TIMEOUT:
998812fe642SMichael Kelley 	case SRB_STATUS_BUS_RESET:
999812fe642SMichael Kelley 	case SRB_STATUS_DATA_OVERRUN:
1000b8a5376cSMichael Kelley 		if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID) {
1001b8a5376cSMichael Kelley 			/* Check for capacity change */
1002b8a5376cSMichael Kelley 			if ((asc == 0x2a) && (ascq == 0x9)) {
100352e1b3b3SMichael Kelley 				process_err_fn = storvsc_device_scan;
1004b8a5376cSMichael Kelley 				/* Retry the I/O that triggered this. */
100552e1b3b3SMichael Kelley 				set_host_byte(scmnd, DID_REQUEUE);
100652e1b3b3SMichael Kelley 				goto do_work;
100752e1b3b3SMichael Kelley 			}
100852e1b3b3SMichael Kelley 
1009c50bd448SK. Y. Srinivasan 			/*
101011d9874cSMichael Kelley 			 * Check for "Operating parameters have changed"
101111d9874cSMichael Kelley 			 * due to Hyper-V changing the VHD/VHDX BlockSize
101211d9874cSMichael Kelley 			 * when adding/removing a differencing disk. This
101311d9874cSMichael Kelley 			 * causes discard_granularity to change, so do a
101411d9874cSMichael Kelley 			 * rescan to pick up the new granularity. We don't
101511d9874cSMichael Kelley 			 * want scsi_report_sense() to output a message
101611d9874cSMichael Kelley 			 * that a sysadmin wouldn't know what to do with.
101711d9874cSMichael Kelley 			 */
101811d9874cSMichael Kelley 			if ((asc == 0x3f) && (ascq != 0x03) &&
101911d9874cSMichael Kelley 					(ascq != 0x0e)) {
102011d9874cSMichael Kelley 				process_err_fn = storvsc_device_scan;
102111d9874cSMichael Kelley 				set_host_byte(scmnd, DID_REQUEUE);
102211d9874cSMichael Kelley 				goto do_work;
102311d9874cSMichael Kelley 			}
102411d9874cSMichael Kelley 
102511d9874cSMichael Kelley 			/*
1026b8a5376cSMichael Kelley 			 * Otherwise, let upper layer deal with the
1027b8a5376cSMichael Kelley 			 * error when sense message is present
1028bba5dc33SLong Li 			 */
102952e1b3b3SMichael Kelley 			return;
1030b8a5376cSMichael Kelley 		}
103152e1b3b3SMichael Kelley 
1032bba5dc33SLong Li 		/*
1033c50bd448SK. Y. Srinivasan 		 * If there is an error; offline the device since all
1034c50bd448SK. Y. Srinivasan 		 * error recovery strategies would have already been
1035c50bd448SK. Y. Srinivasan 		 * deployed on the host side. However, if the command
1036c50bd448SK. Y. Srinivasan 		 * were a pass-through command deal with it appropriately.
1037c50bd448SK. Y. Srinivasan 		 */
1038c50bd448SK. Y. Srinivasan 		switch (scmnd->cmnd[0]) {
1039c50bd448SK. Y. Srinivasan 		case ATA_16:
1040c50bd448SK. Y. Srinivasan 		case ATA_12:
1041c50bd448SK. Y. Srinivasan 			set_host_byte(scmnd, DID_PASSTHROUGH);
1042c50bd448SK. Y. Srinivasan 			break;
10433533f860SK. Y. Srinivasan 		/*
104452e1b3b3SMichael Kelley 		 * On some Hyper-V hosts TEST_UNIT_READY command can
104552e1b3b3SMichael Kelley 		 * return SRB_STATUS_ERROR. Let the upper level code
104652e1b3b3SMichael Kelley 		 * deal with it based on the sense information.
10473533f860SK. Y. Srinivasan 		 */
10483533f860SK. Y. Srinivasan 		case TEST_UNIT_READY:
10493533f860SK. Y. Srinivasan 			break;
1050c50bd448SK. Y. Srinivasan 		default:
1051d1b8b239SCathy Avery 			set_host_byte(scmnd, DID_ERROR);
1052c50bd448SK. Y. Srinivasan 		}
1053b8a5376cSMichael Kelley 		return;
1054b8a5376cSMichael Kelley 
1055b8a5376cSMichael Kelley 	case SRB_STATUS_INVALID_LUN:
1056b8a5376cSMichael Kelley 		set_host_byte(scmnd, DID_NO_CONNECT);
1057b8a5376cSMichael Kelley 		process_err_fn = storvsc_remove_lun;
1058b8a5376cSMichael Kelley 		goto do_work;
1059b8a5376cSMichael Kelley 
1060c50bd448SK. Y. Srinivasan 	}
1061c50bd448SK. Y. Srinivasan 	return;
1062c50bd448SK. Y. Srinivasan 
106352e1b3b3SMichael Kelley do_work:
1064c50bd448SK. Y. Srinivasan 	/*
1065c50bd448SK. Y. Srinivasan 	 * We need to schedule work to process this error; schedule it.
1066c50bd448SK. Y. Srinivasan 	 */
1067c50bd448SK. Y. Srinivasan 	wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
1068c50bd448SK. Y. Srinivasan 	if (!wrk) {
1069a6cb5462SMike Christie 		set_host_byte(scmnd, DID_BAD_TARGET);
1070c50bd448SK. Y. Srinivasan 		return;
1071c50bd448SK. Y. Srinivasan 	}
1072c50bd448SK. Y. Srinivasan 
1073c50bd448SK. Y. Srinivasan 	wrk->host = host;
1074c50bd448SK. Y. Srinivasan 	wrk->lun = vm_srb->lun;
107598441221SK. Y. Srinivasan 	wrk->tgt_id = vm_srb->target_id;
1076c50bd448SK. Y. Srinivasan 	INIT_WORK(&wrk->work, process_err_fn);
1077436ad941SCathy Avery 	queue_work(host_dev->handle_error_wq, &wrk->work);
1078c50bd448SK. Y. Srinivasan }
1079c50bd448SK. Y. Srinivasan 
108089ae7d70SK. Y. Srinivasan 
storvsc_command_completion(struct storvsc_cmd_request * cmd_request,struct storvsc_device * stor_dev)108103996f20SK. Y. Srinivasan static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request,
108203996f20SK. Y. Srinivasan 				       struct storvsc_device *stor_dev)
108389ae7d70SK. Y. Srinivasan {
108489ae7d70SK. Y. Srinivasan 	struct scsi_cmnd *scmnd = cmd_request->cmd;
108589ae7d70SK. Y. Srinivasan 	struct scsi_sense_hdr sense_hdr;
108689ae7d70SK. Y. Srinivasan 	struct vmscsi_request *vm_srb;
108740630f46SLong Li 	u32 data_transfer_length;
1088c50bd448SK. Y. Srinivasan 	struct Scsi_Host *host;
1089be0cf6caSK. Y. Srinivasan 	u32 payload_sz = cmd_request->payload_sz;
1090be0cf6caSK. Y. Srinivasan 	void *payload = cmd_request->payload;
109108f76547SMichael Kelley 	bool sense_ok;
1092c50bd448SK. Y. Srinivasan 
1093c50bd448SK. Y. Srinivasan 	host = stor_dev->host;
109489ae7d70SK. Y. Srinivasan 
109589ae7d70SK. Y. Srinivasan 	vm_srb = &cmd_request->vstor_packet.vm_srb;
109640630f46SLong Li 	data_transfer_length = vm_srb->data_transfer_length;
109789ae7d70SK. Y. Srinivasan 
109889ae7d70SK. Y. Srinivasan 	scmnd->result = vm_srb->scsi_status;
109989ae7d70SK. Y. Srinivasan 
110089ae7d70SK. Y. Srinivasan 	if (scmnd->result) {
110108f76547SMichael Kelley 		sense_ok = scsi_normalize_sense(scmnd->sense_buffer,
110208f76547SMichael Kelley 				SCSI_SENSE_BUFFERSIZE, &sense_hdr);
110308f76547SMichael Kelley 
110408f76547SMichael Kelley 		if (sense_ok && do_logging(STORVSC_LOGGING_WARN))
1105d811b848SHannes Reinecke 			scsi_print_sense_hdr(scmnd->device, "storvsc",
1106d811b848SHannes Reinecke 					     &sense_hdr);
110789ae7d70SK. Y. Srinivasan 	}
110889ae7d70SK. Y. Srinivasan 
110940630f46SLong Li 	if (vm_srb->srb_status != SRB_STATUS_SUCCESS) {
1110c50bd448SK. Y. Srinivasan 		storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
1111c50bd448SK. Y. Srinivasan 					 sense_hdr.ascq);
111240630f46SLong Li 		/*
111340630f46SLong Li 		 * The Windows driver set data_transfer_length on
111440630f46SLong Li 		 * SRB_STATUS_DATA_OVERRUN. On other errors, this value
111540630f46SLong Li 		 * is untouched.  In these cases we set it to 0.
111640630f46SLong Li 		 */
111740630f46SLong Li 		if (vm_srb->srb_status != SRB_STATUS_DATA_OVERRUN)
111840630f46SLong Li 			data_transfer_length = 0;
111940630f46SLong Li 	}
1120c50bd448SK. Y. Srinivasan 
11210a765665SAndres Beltran 	/* Validate data_transfer_length (from Hyper-V) */
11220a765665SAndres Beltran 	if (data_transfer_length > cmd_request->payload->range.len)
11230a765665SAndres Beltran 		data_transfer_length = cmd_request->payload->range.len;
11240a765665SAndres Beltran 
112589ae7d70SK. Y. Srinivasan 	scsi_set_resid(scmnd,
112640630f46SLong Li 		cmd_request->payload->range.len - data_transfer_length);
112789ae7d70SK. Y. Srinivasan 
11280c31fa0eSBart Van Assche 	scsi_done(scmnd);
1129be0cf6caSK. Y. Srinivasan 
1130be0cf6caSK. Y. Srinivasan 	if (payload_sz >
1131be0cf6caSK. Y. Srinivasan 		sizeof(struct vmbus_channel_packet_multipage_buffer))
1132be0cf6caSK. Y. Srinivasan 		kfree(payload);
113389ae7d70SK. Y. Srinivasan }
113489ae7d70SK. Y. Srinivasan 
storvsc_on_io_completion(struct storvsc_device * stor_device,struct vstor_packet * vstor_packet,struct storvsc_cmd_request * request)113503996f20SK. Y. Srinivasan static void storvsc_on_io_completion(struct storvsc_device *stor_device,
113689ae7d70SK. Y. Srinivasan 				  struct vstor_packet *vstor_packet,
113789ae7d70SK. Y. Srinivasan 				  struct storvsc_cmd_request *request)
113889ae7d70SK. Y. Srinivasan {
113989ae7d70SK. Y. Srinivasan 	struct vstor_packet *stor_pkt;
114003996f20SK. Y. Srinivasan 	struct hv_device *device = stor_device->device;
114189ae7d70SK. Y. Srinivasan 
114289ae7d70SK. Y. Srinivasan 	stor_pkt = &request->vstor_packet;
114389ae7d70SK. Y. Srinivasan 
114489ae7d70SK. Y. Srinivasan 	/*
114589ae7d70SK. Y. Srinivasan 	 * The current SCSI handling on the host side does
114689ae7d70SK. Y. Srinivasan 	 * not correctly handle:
114789ae7d70SK. Y. Srinivasan 	 * INQUIRY command with page code parameter set to 0x80
114889ae7d70SK. Y. Srinivasan 	 * MODE_SENSE command with cmd[2] == 0x1c
11493556af9aSCathy Avery 	 * MAINTENANCE_IN is not supported by HyperV FC passthrough
115089ae7d70SK. Y. Srinivasan 	 *
115189ae7d70SK. Y. Srinivasan 	 * Setup srb and scsi status so this won't be fatal.
115289ae7d70SK. Y. Srinivasan 	 * We do this so we can distinguish truly fatal failues
115389ae7d70SK. Y. Srinivasan 	 * (srb status == 0x4) and off-line the device in that case.
115489ae7d70SK. Y. Srinivasan 	 */
115589ae7d70SK. Y. Srinivasan 
115689ae7d70SK. Y. Srinivasan 	if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
11573556af9aSCathy Avery 	   (stor_pkt->vm_srb.cdb[0] == MODE_SENSE) ||
11583556af9aSCathy Avery 	   (stor_pkt->vm_srb.cdb[0] == MAINTENANCE_IN &&
11593556af9aSCathy Avery 	   hv_dev_is_fc(device))) {
116089ae7d70SK. Y. Srinivasan 		vstor_packet->vm_srb.scsi_status = 0;
116189ae7d70SK. Y. Srinivasan 		vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
116289ae7d70SK. Y. Srinivasan 	}
116389ae7d70SK. Y. Srinivasan 
116489ae7d70SK. Y. Srinivasan 	/* Copy over the status...etc */
116589ae7d70SK. Y. Srinivasan 	stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
116689ae7d70SK. Y. Srinivasan 	stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
11670a765665SAndres Beltran 
1168d4674859SMichael Kelley 	/*
1169d4674859SMichael Kelley 	 * Copy over the sense_info_length, but limit to the known max
1170d4674859SMichael Kelley 	 * size if Hyper-V returns a bad value.
1171d4674859SMichael Kelley 	 */
117286c8fb4dSSaurabh Sengar 	stor_pkt->vm_srb.sense_info_length = min_t(u8, STORVSC_SENSE_BUFFER_SIZE,
1173d4674859SMichael Kelley 		vstor_packet->vm_srb.sense_info_length);
117489ae7d70SK. Y. Srinivasan 
1175f8aea701SLong Li 	if (vstor_packet->vm_srb.scsi_status != 0 ||
1176dbe7633cSMichael Kelley 	    vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS) {
1177dbe7633cSMichael Kelley 
1178dbe7633cSMichael Kelley 		/*
1179dbe7633cSMichael Kelley 		 * Log TEST_UNIT_READY errors only as warnings. Hyper-V can
1180dbe7633cSMichael Kelley 		 * return errors when detecting devices using TEST_UNIT_READY,
1181dbe7633cSMichael Kelley 		 * and logging these as errors produces unhelpful noise.
1182dbe7633cSMichael Kelley 		 */
1183dbe7633cSMichael Kelley 		int loglevel = (stor_pkt->vm_srb.cdb[0] == TEST_UNIT_READY) ?
1184dbe7633cSMichael Kelley 			STORVSC_LOGGING_WARN : STORVSC_LOGGING_ERROR;
1185dbe7633cSMichael Kelley 
1186088bde86SEaswar Hariharan 		storvsc_log_ratelimited(device, loglevel,
118708f76547SMichael Kelley 			"tag#%d cmd 0x%x status: scsi 0x%x srb 0x%x hv 0x%x\n",
1188c5bf198cSMartin K. Petersen 			scsi_cmd_to_rq(request->cmd)->tag,
1189f8aea701SLong Li 			stor_pkt->vm_srb.cdb[0],
1190f8aea701SLong Li 			vstor_packet->vm_srb.scsi_status,
119108f76547SMichael Kelley 			vstor_packet->vm_srb.srb_status,
119208f76547SMichael Kelley 			vstor_packet->status);
1193dbe7633cSMichael Kelley 	}
119489ae7d70SK. Y. Srinivasan 
1195d4674859SMichael Kelley 	if (vstor_packet->vm_srb.scsi_status == SAM_STAT_CHECK_CONDITION &&
1196d4674859SMichael Kelley 	    (vstor_packet->vm_srb.srb_status & SRB_STATUS_AUTOSENSE_VALID))
1197ead3700dSChristoph Hellwig 		memcpy(request->cmd->sense_buffer,
119889ae7d70SK. Y. Srinivasan 		       vstor_packet->vm_srb.sense_data,
1199d4674859SMichael Kelley 		       stor_pkt->vm_srb.sense_info_length);
120089ae7d70SK. Y. Srinivasan 
120189ae7d70SK. Y. Srinivasan 	stor_pkt->vm_srb.data_transfer_length =
120289ae7d70SK. Y. Srinivasan 		vstor_packet->vm_srb.data_transfer_length;
120389ae7d70SK. Y. Srinivasan 
120403996f20SK. Y. Srinivasan 	storvsc_command_completion(request, stor_device);
120589ae7d70SK. Y. Srinivasan 
120689ae7d70SK. Y. Srinivasan 	if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
120789ae7d70SK. Y. Srinivasan 		stor_device->drain_notify)
120889ae7d70SK. Y. Srinivasan 		wake_up(&stor_device->waiting_to_drain);
120989ae7d70SK. Y. Srinivasan }
121089ae7d70SK. Y. Srinivasan 
storvsc_on_receive(struct storvsc_device * stor_device,struct vstor_packet * vstor_packet,struct storvsc_cmd_request * request)121103996f20SK. Y. Srinivasan static void storvsc_on_receive(struct storvsc_device *stor_device,
121289ae7d70SK. Y. Srinivasan 			     struct vstor_packet *vstor_packet,
121389ae7d70SK. Y. Srinivasan 			     struct storvsc_cmd_request *request)
121489ae7d70SK. Y. Srinivasan {
1215c58cc70fSLong Li 	struct hv_host_device *host_dev;
121689ae7d70SK. Y. Srinivasan 	switch (vstor_packet->operation) {
121789ae7d70SK. Y. Srinivasan 	case VSTOR_OPERATION_COMPLETE_IO:
121803996f20SK. Y. Srinivasan 		storvsc_on_io_completion(stor_device, vstor_packet, request);
121989ae7d70SK. Y. Srinivasan 		break;
122089ae7d70SK. Y. Srinivasan 
122189ae7d70SK. Y. Srinivasan 	case VSTOR_OPERATION_REMOVE_DEVICE:
122289ae7d70SK. Y. Srinivasan 	case VSTOR_OPERATION_ENUMERATE_BUS:
1223c58cc70fSLong Li 		host_dev = shost_priv(stor_device->host);
1224c58cc70fSLong Li 		queue_work(
1225c58cc70fSLong Li 			host_dev->handle_error_wq, &host_dev->host_scan_work);
122689ae7d70SK. Y. Srinivasan 		break;
122789ae7d70SK. Y. Srinivasan 
1228dac58241SK. Y. Srinivasan 	case VSTOR_OPERATION_FCHBA_DATA:
1229dac58241SK. Y. Srinivasan 		cache_wwn(stor_device, vstor_packet);
1230dac58241SK. Y. Srinivasan #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1231dac58241SK. Y. Srinivasan 		fc_host_node_name(stor_device->host) = stor_device->node_name;
1232dac58241SK. Y. Srinivasan 		fc_host_port_name(stor_device->host) = stor_device->port_name;
1233dac58241SK. Y. Srinivasan #endif
1234dac58241SK. Y. Srinivasan 		break;
123589ae7d70SK. Y. Srinivasan 	default:
123689ae7d70SK. Y. Srinivasan 		break;
123789ae7d70SK. Y. Srinivasan 	}
123889ae7d70SK. Y. Srinivasan }
123989ae7d70SK. Y. Srinivasan 
storvsc_on_channel_callback(void * context)124089ae7d70SK. Y. Srinivasan static void storvsc_on_channel_callback(void *context)
124189ae7d70SK. Y. Srinivasan {
12426f94d5deSK. Y. Srinivasan 	struct vmbus_channel *channel = (struct vmbus_channel *)context;
1243ddccd952SStephen Hemminger 	const struct vmpacket_descriptor *desc;
12446f94d5deSK. Y. Srinivasan 	struct hv_device *device;
124589ae7d70SK. Y. Srinivasan 	struct storvsc_device *stor_device;
1246bf5fd8caSAndrea Parri (Microsoft) 	struct Scsi_Host *shost;
124778c65f0fSSaurabh Sengar 	unsigned long time_limit = jiffies + msecs_to_jiffies(CALLBACK_TIMEOUT);
124889ae7d70SK. Y. Srinivasan 
12496f94d5deSK. Y. Srinivasan 	if (channel->primary_channel != NULL)
12506f94d5deSK. Y. Srinivasan 		device = channel->primary_channel->device_obj;
12516f94d5deSK. Y. Srinivasan 	else
12526f94d5deSK. Y. Srinivasan 		device = channel->device_obj;
125389ae7d70SK. Y. Srinivasan 
125489ae7d70SK. Y. Srinivasan 	stor_device = get_in_stor_device(device);
125589ae7d70SK. Y. Srinivasan 	if (!stor_device)
125689ae7d70SK. Y. Srinivasan 		return;
125789ae7d70SK. Y. Srinivasan 
1258bf5fd8caSAndrea Parri (Microsoft) 	shost = stor_device->host;
1259bf5fd8caSAndrea Parri (Microsoft) 
1260ddccd952SStephen Hemminger 	foreach_vmbus_pkt(desc, channel) {
1261bf5fd8caSAndrea Parri (Microsoft) 		struct vstor_packet *packet = hv_pkt_data(desc);
1262bf5fd8caSAndrea Parri (Microsoft) 		struct storvsc_cmd_request *request = NULL;
12636fd13d69SAndrea Parri (Microsoft) 		u32 pktlen = hv_pkt_datalen(desc);
1264bf5fd8caSAndrea Parri (Microsoft) 		u64 rqst_id = desc->trans_id;
126586c8fb4dSSaurabh Sengar 		u32 minlen = rqst_id ? sizeof(struct vstor_packet) :
126686c8fb4dSSaurabh Sengar 			sizeof(enum vstor_packet_operation);
126789ae7d70SK. Y. Srinivasan 
126878c65f0fSSaurabh Sengar 		if (unlikely(time_after(jiffies, time_limit))) {
126978c65f0fSSaurabh Sengar 			hv_pkt_iter_close(channel);
127078c65f0fSSaurabh Sengar 			return;
127178c65f0fSSaurabh Sengar 		}
127278c65f0fSSaurabh Sengar 
12736fd13d69SAndrea Parri (Microsoft) 		if (pktlen < minlen) {
12746fd13d69SAndrea Parri (Microsoft) 			dev_err(&device->device,
12756fd13d69SAndrea Parri (Microsoft) 				"Invalid pkt: id=%llu, len=%u, minlen=%u\n",
12766fd13d69SAndrea Parri (Microsoft) 				rqst_id, pktlen, minlen);
127791b1b640SAndrea Parri (Microsoft) 			continue;
127891b1b640SAndrea Parri (Microsoft) 		}
127991b1b640SAndrea Parri (Microsoft) 
1280bf5fd8caSAndrea Parri (Microsoft) 		if (rqst_id == VMBUS_RQST_INIT) {
1281bf5fd8caSAndrea Parri (Microsoft) 			request = &stor_device->init_request;
1282bf5fd8caSAndrea Parri (Microsoft) 		} else if (rqst_id == VMBUS_RQST_RESET) {
1283bf5fd8caSAndrea Parri (Microsoft) 			request = &stor_device->reset_request;
1284bf5fd8caSAndrea Parri (Microsoft) 		} else {
1285bf5fd8caSAndrea Parri (Microsoft) 			/* Hyper-V can send an unsolicited message with ID of 0 */
1286bf5fd8caSAndrea Parri (Microsoft) 			if (rqst_id == 0) {
1287bf5fd8caSAndrea Parri (Microsoft) 				/*
1288bf5fd8caSAndrea Parri (Microsoft) 				 * storvsc_on_receive() looks at the vstor_packet in the message
12896fd13d69SAndrea Parri (Microsoft) 				 * from the ring buffer.
12906fd13d69SAndrea Parri (Microsoft) 				 *
12916fd13d69SAndrea Parri (Microsoft) 				 * - If the operation in the vstor_packet is COMPLETE_IO, then
12926fd13d69SAndrea Parri (Microsoft) 				 *   we call storvsc_on_io_completion(), and dereference the
12936fd13d69SAndrea Parri (Microsoft) 				 *   guest memory address.  Make sure we don't call
12946fd13d69SAndrea Parri (Microsoft) 				 *   storvsc_on_io_completion() with a guest memory address
12956fd13d69SAndrea Parri (Microsoft) 				 *   that is zero if Hyper-V were to construct and send such
12966fd13d69SAndrea Parri (Microsoft) 				 *   a bogus packet.
12976fd13d69SAndrea Parri (Microsoft) 				 *
12986fd13d69SAndrea Parri (Microsoft) 				 * - If the operation in the vstor_packet is FCHBA_DATA, then
12996fd13d69SAndrea Parri (Microsoft) 				 *   we call cache_wwn(), and access the data payload area of
13006fd13d69SAndrea Parri (Microsoft) 				 *   the packet (wwn_packet); however, there is no guarantee
13016fd13d69SAndrea Parri (Microsoft) 				 *   that the packet is big enough to contain such area.
13026fd13d69SAndrea Parri (Microsoft) 				 *   Future-proof the code by rejecting such a bogus packet.
1303bf5fd8caSAndrea Parri (Microsoft) 				 */
13046fd13d69SAndrea Parri (Microsoft) 				if (packet->operation == VSTOR_OPERATION_COMPLETE_IO ||
13056fd13d69SAndrea Parri (Microsoft) 				    packet->operation == VSTOR_OPERATION_FCHBA_DATA) {
1306bf5fd8caSAndrea Parri (Microsoft) 					dev_err(&device->device, "Invalid packet with ID of 0\n");
1307bf5fd8caSAndrea Parri (Microsoft) 					continue;
1308bf5fd8caSAndrea Parri (Microsoft) 				}
1309bf5fd8caSAndrea Parri (Microsoft) 			} else {
1310bf5fd8caSAndrea Parri (Microsoft) 				struct scsi_cmnd *scmnd;
1311bf5fd8caSAndrea Parri (Microsoft) 
1312bf5fd8caSAndrea Parri (Microsoft) 				/* Transaction 'rqst_id' corresponds to tag 'rqst_id - 1' */
1313bf5fd8caSAndrea Parri (Microsoft) 				scmnd = scsi_host_find_tag(shost, rqst_id - 1);
1314bf5fd8caSAndrea Parri (Microsoft) 				if (scmnd == NULL) {
1315bf5fd8caSAndrea Parri (Microsoft) 					dev_err(&device->device, "Incorrect transaction ID\n");
1316bf5fd8caSAndrea Parri (Microsoft) 					continue;
1317bf5fd8caSAndrea Parri (Microsoft) 				}
1318bf5fd8caSAndrea Parri (Microsoft) 				request = (struct storvsc_cmd_request *)scsi_cmd_priv(scmnd);
1319743b237cSTianyu Lan 				scsi_dma_unmap(scmnd);
1320bf5fd8caSAndrea Parri (Microsoft) 			}
1321bf5fd8caSAndrea Parri (Microsoft) 
1322bf5fd8caSAndrea Parri (Microsoft) 			storvsc_on_receive(stor_device, packet, request);
1323bf5fd8caSAndrea Parri (Microsoft) 			continue;
1324bf5fd8caSAndrea Parri (Microsoft) 		}
1325bf5fd8caSAndrea Parri (Microsoft) 
132689ae7d70SK. Y. Srinivasan 		memcpy(&request->vstor_packet, packet,
132786c8fb4dSSaurabh Sengar 		       sizeof(struct vstor_packet));
132889ae7d70SK. Y. Srinivasan 		complete(&request->wait_event);
132989ae7d70SK. Y. Srinivasan 	}
133089ae7d70SK. Y. Srinivasan }
133189ae7d70SK. Y. Srinivasan 
storvsc_connect_to_vsp(struct hv_device * device,u32 ring_size,bool is_fc)1332dac58241SK. Y. Srinivasan static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
1333dac58241SK. Y. Srinivasan 				  bool is_fc)
133489ae7d70SK. Y. Srinivasan {
133589ae7d70SK. Y. Srinivasan 	struct vmstorage_channel_properties props;
133689ae7d70SK. Y. Srinivasan 	int ret;
133789ae7d70SK. Y. Srinivasan 
133889ae7d70SK. Y. Srinivasan 	memset(&props, 0, sizeof(struct vmstorage_channel_properties));
133989ae7d70SK. Y. Srinivasan 
1340adae1e93SAndres Beltran 	device->channel->max_pkt_size = STORVSC_MAX_PKT_SIZE;
1341bf5fd8caSAndrea Parri (Microsoft) 	device->channel->next_request_id_callback = storvsc_next_request_id;
1342453de21cSAndres Beltran 
134389ae7d70SK. Y. Srinivasan 	ret = vmbus_open(device->channel,
134489ae7d70SK. Y. Srinivasan 			 ring_size,
134589ae7d70SK. Y. Srinivasan 			 ring_size,
134689ae7d70SK. Y. Srinivasan 			 (void *)&props,
134789ae7d70SK. Y. Srinivasan 			 sizeof(struct vmstorage_channel_properties),
13486f94d5deSK. Y. Srinivasan 			 storvsc_on_channel_callback, device->channel);
134989ae7d70SK. Y. Srinivasan 
135089ae7d70SK. Y. Srinivasan 	if (ret != 0)
135189ae7d70SK. Y. Srinivasan 		return ret;
135289ae7d70SK. Y. Srinivasan 
1353dac58241SK. Y. Srinivasan 	ret = storvsc_channel_init(device, is_fc);
135489ae7d70SK. Y. Srinivasan 
135589ae7d70SK. Y. Srinivasan 	return ret;
135689ae7d70SK. Y. Srinivasan }
135789ae7d70SK. Y. Srinivasan 
storvsc_dev_remove(struct hv_device * device)135889ae7d70SK. Y. Srinivasan static int storvsc_dev_remove(struct hv_device *device)
135989ae7d70SK. Y. Srinivasan {
136089ae7d70SK. Y. Srinivasan 	struct storvsc_device *stor_device;
136189ae7d70SK. Y. Srinivasan 
136289ae7d70SK. Y. Srinivasan 	stor_device = hv_get_drvdata(device);
136389ae7d70SK. Y. Srinivasan 
136489ae7d70SK. Y. Srinivasan 	stor_device->destroy = true;
13652371cd90SStephen Hemminger 
13662371cd90SStephen Hemminger 	/* Make sure flag is set before waiting */
13672371cd90SStephen Hemminger 	wmb();
136889ae7d70SK. Y. Srinivasan 
136989ae7d70SK. Y. Srinivasan 	/*
137089ae7d70SK. Y. Srinivasan 	 * At this point, all outbound traffic should be disable. We
137189ae7d70SK. Y. Srinivasan 	 * only allow inbound traffic (responses) to proceed so that
137289ae7d70SK. Y. Srinivasan 	 * outstanding requests can be completed.
137389ae7d70SK. Y. Srinivasan 	 */
137489ae7d70SK. Y. Srinivasan 
137589ae7d70SK. Y. Srinivasan 	storvsc_wait_to_drain(stor_device);
137689ae7d70SK. Y. Srinivasan 
137789ae7d70SK. Y. Srinivasan 	/*
137889ae7d70SK. Y. Srinivasan 	 * Since we have already drained, we don't need to busy wait
137989ae7d70SK. Y. Srinivasan 	 * as was done in final_release_stor_device()
138089ae7d70SK. Y. Srinivasan 	 * Note that we cannot set the ext pointer to NULL until
138189ae7d70SK. Y. Srinivasan 	 * we have drained - to drain the outgoing packets, we need to
138289ae7d70SK. Y. Srinivasan 	 * allow incoming packets.
138389ae7d70SK. Y. Srinivasan 	 */
138489ae7d70SK. Y. Srinivasan 	hv_set_drvdata(device, NULL);
138589ae7d70SK. Y. Srinivasan 
138689ae7d70SK. Y. Srinivasan 	/* Close the channel */
138789ae7d70SK. Y. Srinivasan 	vmbus_close(device->channel);
138889ae7d70SK. Y. Srinivasan 
1389d86adf48SK. Y. Srinivasan 	kfree(stor_device->stor_chns);
139089ae7d70SK. Y. Srinivasan 	kfree(stor_device);
139189ae7d70SK. Y. Srinivasan 	return 0;
139289ae7d70SK. Y. Srinivasan }
139389ae7d70SK. Y. Srinivasan 
get_og_chn(struct storvsc_device * stor_device,u16 q_num)1394d86adf48SK. Y. Srinivasan static struct vmbus_channel *get_og_chn(struct storvsc_device *stor_device,
1395d86adf48SK. Y. Srinivasan 					u16 q_num)
1396d86adf48SK. Y. Srinivasan {
1397d86adf48SK. Y. Srinivasan 	u16 slot = 0;
1398d86adf48SK. Y. Srinivasan 	u16 hash_qnum;
13991b25a8c4SMichael Kelley 	const struct cpumask *node_mask;
1400d86adf48SK. Y. Srinivasan 	int num_channels, tgt_cpu;
1401d86adf48SK. Y. Srinivasan 
14027769e18cSAndrea Parri (Microsoft) 	if (stor_device->num_sc == 0) {
14037769e18cSAndrea Parri (Microsoft) 		stor_device->stor_chns[q_num] = stor_device->device->channel;
1404d86adf48SK. Y. Srinivasan 		return stor_device->device->channel;
14057769e18cSAndrea Parri (Microsoft) 	}
1406d86adf48SK. Y. Srinivasan 
1407d86adf48SK. Y. Srinivasan 	/*
1408d86adf48SK. Y. Srinivasan 	 * Our channel array is sparsley populated and we
1409d86adf48SK. Y. Srinivasan 	 * initiated I/O on a processor/hw-q that does not
1410d86adf48SK. Y. Srinivasan 	 * currently have a designated channel. Fix this.
1411d86adf48SK. Y. Srinivasan 	 * The strategy is simple:
1412d86adf48SK. Y. Srinivasan 	 * I. Ensure NUMA locality
1413d86adf48SK. Y. Srinivasan 	 * II. Distribute evenly (best effort)
1414d86adf48SK. Y. Srinivasan 	 */
1415d86adf48SK. Y. Srinivasan 
14161b25a8c4SMichael Kelley 	node_mask = cpumask_of_node(cpu_to_node(q_num));
1417d86adf48SK. Y. Srinivasan 
14181b25a8c4SMichael Kelley 	num_channels = 0;
14191b25a8c4SMichael Kelley 	for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
14201b25a8c4SMichael Kelley 		if (cpumask_test_cpu(tgt_cpu, node_mask))
14211b25a8c4SMichael Kelley 			num_channels++;
14221b25a8c4SMichael Kelley 	}
14237769e18cSAndrea Parri (Microsoft) 	if (num_channels == 0) {
14247769e18cSAndrea Parri (Microsoft) 		stor_device->stor_chns[q_num] = stor_device->device->channel;
1425d86adf48SK. Y. Srinivasan 		return stor_device->device->channel;
14267769e18cSAndrea Parri (Microsoft) 	}
1427d86adf48SK. Y. Srinivasan 
1428d86adf48SK. Y. Srinivasan 	hash_qnum = q_num;
1429d86adf48SK. Y. Srinivasan 	while (hash_qnum >= num_channels)
1430d86adf48SK. Y. Srinivasan 		hash_qnum -= num_channels;
1431d86adf48SK. Y. Srinivasan 
14321b25a8c4SMichael Kelley 	for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
14331b25a8c4SMichael Kelley 		if (!cpumask_test_cpu(tgt_cpu, node_mask))
14341b25a8c4SMichael Kelley 			continue;
1435d86adf48SK. Y. Srinivasan 		if (slot == hash_qnum)
1436d86adf48SK. Y. Srinivasan 			break;
1437d86adf48SK. Y. Srinivasan 		slot++;
1438d86adf48SK. Y. Srinivasan 	}
1439d86adf48SK. Y. Srinivasan 
1440d86adf48SK. Y. Srinivasan 	stor_device->stor_chns[q_num] = stor_device->stor_chns[tgt_cpu];
1441d86adf48SK. Y. Srinivasan 
1442d86adf48SK. Y. Srinivasan 	return stor_device->stor_chns[q_num];
1443d86adf48SK. Y. Srinivasan }
1444d86adf48SK. Y. Srinivasan 
1445d86adf48SK. Y. Srinivasan 
storvsc_do_io(struct hv_device * device,struct storvsc_cmd_request * request,u16 q_num)144689ae7d70SK. Y. Srinivasan static int storvsc_do_io(struct hv_device *device,
1447d86adf48SK. Y. Srinivasan 			 struct storvsc_cmd_request *request, u16 q_num)
144889ae7d70SK. Y. Srinivasan {
144989ae7d70SK. Y. Srinivasan 	struct storvsc_device *stor_device;
145089ae7d70SK. Y. Srinivasan 	struct vstor_packet *vstor_packet;
14512217a47dSLong Li 	struct vmbus_channel *outgoing_channel, *channel;
14527769e18cSAndrea Parri (Microsoft) 	unsigned long flags;
145389ae7d70SK. Y. Srinivasan 	int ret = 0;
14541b25a8c4SMichael Kelley 	const struct cpumask *node_mask;
1455d86adf48SK. Y. Srinivasan 	int tgt_cpu;
145689ae7d70SK. Y. Srinivasan 
145789ae7d70SK. Y. Srinivasan 	vstor_packet = &request->vstor_packet;
145889ae7d70SK. Y. Srinivasan 	stor_device = get_out_stor_device(device);
145989ae7d70SK. Y. Srinivasan 
146089ae7d70SK. Y. Srinivasan 	if (!stor_device)
146189ae7d70SK. Y. Srinivasan 		return -ENODEV;
146289ae7d70SK. Y. Srinivasan 
146389ae7d70SK. Y. Srinivasan 
146489ae7d70SK. Y. Srinivasan 	request->device  = device;
14656f94d5deSK. Y. Srinivasan 	/*
14667769e18cSAndrea Parri (Microsoft) 	 * Select an appropriate channel to send the request out.
14676f94d5deSK. Y. Srinivasan 	 */
14687769e18cSAndrea Parri (Microsoft) 	/* See storvsc_change_target_cpu(). */
14697769e18cSAndrea Parri (Microsoft) 	outgoing_channel = READ_ONCE(stor_device->stor_chns[q_num]);
14707769e18cSAndrea Parri (Microsoft) 	if (outgoing_channel != NULL) {
14712217a47dSLong Li 		if (outgoing_channel->target_cpu == q_num) {
1472d86adf48SK. Y. Srinivasan 			/*
1473d86adf48SK. Y. Srinivasan 			 * Ideally, we want to pick a different channel if
1474d86adf48SK. Y. Srinivasan 			 * available on the same NUMA node.
1475d86adf48SK. Y. Srinivasan 			 */
14761b25a8c4SMichael Kelley 			node_mask = cpumask_of_node(cpu_to_node(q_num));
14771b25a8c4SMichael Kelley 			for_each_cpu_wrap(tgt_cpu,
14781b25a8c4SMichael Kelley 				 &stor_device->alloced_cpus, q_num + 1) {
14791b25a8c4SMichael Kelley 				if (!cpumask_test_cpu(tgt_cpu, node_mask))
14801b25a8c4SMichael Kelley 					continue;
14812217a47dSLong Li 				if (tgt_cpu == q_num)
14822217a47dSLong Li 					continue;
14837769e18cSAndrea Parri (Microsoft) 				channel = READ_ONCE(
14847769e18cSAndrea Parri (Microsoft) 					stor_device->stor_chns[tgt_cpu]);
14857769e18cSAndrea Parri (Microsoft) 				if (channel == NULL)
14867769e18cSAndrea Parri (Microsoft) 					continue;
14872217a47dSLong Li 				if (hv_get_avail_to_write_percent(
14882217a47dSLong Li 							&channel->outbound)
14892217a47dSLong Li 						> ring_avail_percent_lowater) {
14902217a47dSLong Li 					outgoing_channel = channel;
14912217a47dSLong Li 					goto found_channel;
14922217a47dSLong Li 				}
14932217a47dSLong Li 			}
14942217a47dSLong Li 
14952217a47dSLong Li 			/*
14962217a47dSLong Li 			 * All the other channels on the same NUMA node are
14972217a47dSLong Li 			 * busy. Try to use the channel on the current CPU
14982217a47dSLong Li 			 */
14992217a47dSLong Li 			if (hv_get_avail_to_write_percent(
15002217a47dSLong Li 						&outgoing_channel->outbound)
15012217a47dSLong Li 					> ring_avail_percent_lowater)
15022217a47dSLong Li 				goto found_channel;
15032217a47dSLong Li 
15042217a47dSLong Li 			/*
15052217a47dSLong Li 			 * If we reach here, all the channels on the current
15062217a47dSLong Li 			 * NUMA node are busy. Try to find a channel in
15072217a47dSLong Li 			 * other NUMA nodes
15082217a47dSLong Li 			 */
15091b25a8c4SMichael Kelley 			for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
15101b25a8c4SMichael Kelley 				if (cpumask_test_cpu(tgt_cpu, node_mask))
15111b25a8c4SMichael Kelley 					continue;
15127769e18cSAndrea Parri (Microsoft) 				channel = READ_ONCE(
15137769e18cSAndrea Parri (Microsoft) 					stor_device->stor_chns[tgt_cpu]);
15147769e18cSAndrea Parri (Microsoft) 				if (channel == NULL)
15157769e18cSAndrea Parri (Microsoft) 					continue;
15162217a47dSLong Li 				if (hv_get_avail_to_write_percent(
15172217a47dSLong Li 							&channel->outbound)
15182217a47dSLong Li 						> ring_avail_percent_lowater) {
15192217a47dSLong Li 					outgoing_channel = channel;
15202217a47dSLong Li 					goto found_channel;
1521d86adf48SK. Y. Srinivasan 				}
1522d86adf48SK. Y. Srinivasan 			}
1523d86adf48SK. Y. Srinivasan 		}
1524d86adf48SK. Y. Srinivasan 	} else {
152521d2052cSAndrea Parri (Microsoft) 		spin_lock_irqsave(&stor_device->lock, flags);
15267769e18cSAndrea Parri (Microsoft) 		outgoing_channel = stor_device->stor_chns[q_num];
15277769e18cSAndrea Parri (Microsoft) 		if (outgoing_channel != NULL) {
152821d2052cSAndrea Parri (Microsoft) 			spin_unlock_irqrestore(&stor_device->lock, flags);
15297769e18cSAndrea Parri (Microsoft) 			goto found_channel;
15307769e18cSAndrea Parri (Microsoft) 		}
1531d86adf48SK. Y. Srinivasan 		outgoing_channel = get_og_chn(stor_device, q_num);
153221d2052cSAndrea Parri (Microsoft) 		spin_unlock_irqrestore(&stor_device->lock, flags);
1533d86adf48SK. Y. Srinivasan 	}
153489ae7d70SK. Y. Srinivasan 
15352217a47dSLong Li found_channel:
153689ae7d70SK. Y. Srinivasan 	vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
153789ae7d70SK. Y. Srinivasan 
153886c8fb4dSSaurabh Sengar 	vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
153989ae7d70SK. Y. Srinivasan 
154089ae7d70SK. Y. Srinivasan 
154186c8fb4dSSaurabh Sengar 	vstor_packet->vm_srb.sense_info_length = STORVSC_SENSE_BUFFER_SIZE;
154289ae7d70SK. Y. Srinivasan 
154389ae7d70SK. Y. Srinivasan 
154489ae7d70SK. Y. Srinivasan 	vstor_packet->vm_srb.data_transfer_length =
1545be0cf6caSK. Y. Srinivasan 	request->payload->range.len;
154689ae7d70SK. Y. Srinivasan 
154789ae7d70SK. Y. Srinivasan 	vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
154889ae7d70SK. Y. Srinivasan 
1549be0cf6caSK. Y. Srinivasan 	if (request->payload->range.len) {
1550be0cf6caSK. Y. Srinivasan 
1551be0cf6caSK. Y. Srinivasan 		ret = vmbus_sendpacket_mpb_desc(outgoing_channel,
1552be0cf6caSK. Y. Srinivasan 				request->payload, request->payload_sz,
155389ae7d70SK. Y. Srinivasan 				vstor_packet,
155486c8fb4dSSaurabh Sengar 				sizeof(struct vstor_packet),
155589ae7d70SK. Y. Srinivasan 				(unsigned long)request);
155689ae7d70SK. Y. Srinivasan 	} else {
15570147dabcSK. Y. Srinivasan 		ret = vmbus_sendpacket(outgoing_channel, vstor_packet,
155886c8fb4dSSaurabh Sengar 			       sizeof(struct vstor_packet),
155989ae7d70SK. Y. Srinivasan 			       (unsigned long)request,
156089ae7d70SK. Y. Srinivasan 			       VM_PKT_DATA_INBAND,
156189ae7d70SK. Y. Srinivasan 			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
156289ae7d70SK. Y. Srinivasan 	}
156389ae7d70SK. Y. Srinivasan 
156489ae7d70SK. Y. Srinivasan 	if (ret != 0)
156589ae7d70SK. Y. Srinivasan 		return ret;
156689ae7d70SK. Y. Srinivasan 
156789ae7d70SK. Y. Srinivasan 	atomic_inc(&stor_device->num_outstanding_req);
156889ae7d70SK. Y. Srinivasan 
156989ae7d70SK. Y. Srinivasan 	return ret;
157089ae7d70SK. Y. Srinivasan }
157189ae7d70SK. Y. Srinivasan 
storvsc_device_alloc(struct scsi_device * sdevice)1572f1c635b4SStephen Hemminger static int storvsc_device_alloc(struct scsi_device *sdevice)
1573f1c635b4SStephen Hemminger {
1574f1c635b4SStephen Hemminger 	/*
1575f1c635b4SStephen Hemminger 	 * Set blist flag to permit the reading of the VPD pages even when
1576f1c635b4SStephen Hemminger 	 * the target may claim SPC-2 compliance. MSFT targets currently
1577f1c635b4SStephen Hemminger 	 * claim SPC-2 compliance while they implement post SPC-2 features.
1578f1c635b4SStephen Hemminger 	 * With this flag we can correctly handle WRITE_SAME_16 issues.
1579f1c635b4SStephen Hemminger 	 *
1580f1c635b4SStephen Hemminger 	 * Hypervisor reports SCSI_UNKNOWN type for DVD ROM device but
1581f1c635b4SStephen Hemminger 	 * still supports REPORT LUN.
1582f1c635b4SStephen Hemminger 	 */
1583f1c635b4SStephen Hemminger 	sdevice->sdev_bflags = BLIST_REPORTLUN2 | BLIST_TRY_VPD_PAGES;
1584f1c635b4SStephen Hemminger 
1585f1c635b4SStephen Hemminger 	return 0;
1586f1c635b4SStephen Hemminger }
1587f1c635b4SStephen Hemminger 
storvsc_device_configure(struct scsi_device * sdevice)158889ae7d70SK. Y. Srinivasan static int storvsc_device_configure(struct scsi_device *sdevice)
158989ae7d70SK. Y. Srinivasan {
1590893def38SK. Y. Srinivasan 	blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
1591893def38SK. Y. Srinivasan 
159231d16e71SMichael Kelley 	/* storvsc devices don't support MAINTENANCE_IN SCSI cmd */
159331d16e71SMichael Kelley 	sdevice->no_report_opcodes = 1;
15943e8f4f40SOlaf Hering 	sdevice->no_write_same = 1;
15953e8f4f40SOlaf Hering 
1596f3cfabceSK. Y. Srinivasan 	/*
1597b0a93d96SK. Y. Srinivasan 	 * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3
1598b95f5be0SKeith Mange 	 * if the device is a MSFT virtual device.  If the host is
1599b95f5be0SKeith Mange 	 * WIN10 or newer, allow write_same.
1600b0a93d96SK. Y. Srinivasan 	 */
1601b0a93d96SK. Y. Srinivasan 	if (!strncmp(sdevice->vendor, "Msft", 4)) {
1602e6c4bc66SKeith Mange 		switch (vmstor_proto_version) {
1603e6c4bc66SKeith Mange 		case VMSTOR_PROTO_VERSION_WIN8:
1604e6c4bc66SKeith Mange 		case VMSTOR_PROTO_VERSION_WIN8_1:
1605b0a93d96SK. Y. Srinivasan 			sdevice->scsi_level = SCSI_SPC_3;
1606b0a93d96SK. Y. Srinivasan 			break;
1607b0a93d96SK. Y. Srinivasan 		}
1608b95f5be0SKeith Mange 
1609b95f5be0SKeith Mange 		if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN10)
1610b95f5be0SKeith Mange 			sdevice->no_write_same = 0;
1611b0a93d96SK. Y. Srinivasan 	}
1612b0a93d96SK. Y. Srinivasan 
161389ae7d70SK. Y. Srinivasan 	return 0;
161489ae7d70SK. Y. Srinivasan }
161589ae7d70SK. Y. Srinivasan 
storvsc_get_chs(struct scsi_device * sdev,struct block_device * bdev,sector_t capacity,int * info)161689ae7d70SK. Y. Srinivasan static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
161789ae7d70SK. Y. Srinivasan 			   sector_t capacity, int *info)
161889ae7d70SK. Y. Srinivasan {
161989ae7d70SK. Y. Srinivasan 	sector_t nsect = capacity;
162089ae7d70SK. Y. Srinivasan 	sector_t cylinders = nsect;
162189ae7d70SK. Y. Srinivasan 	int heads, sectors_pt;
162289ae7d70SK. Y. Srinivasan 
162389ae7d70SK. Y. Srinivasan 	/*
162489ae7d70SK. Y. Srinivasan 	 * We are making up these values; let us keep it simple.
162589ae7d70SK. Y. Srinivasan 	 */
162689ae7d70SK. Y. Srinivasan 	heads = 0xff;
162789ae7d70SK. Y. Srinivasan 	sectors_pt = 0x3f;      /* Sectors per track */
162889ae7d70SK. Y. Srinivasan 	sector_div(cylinders, heads * sectors_pt);
162989ae7d70SK. Y. Srinivasan 	if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
163089ae7d70SK. Y. Srinivasan 		cylinders = 0xffff;
163189ae7d70SK. Y. Srinivasan 
163289ae7d70SK. Y. Srinivasan 	info[0] = heads;
163389ae7d70SK. Y. Srinivasan 	info[1] = sectors_pt;
163489ae7d70SK. Y. Srinivasan 	info[2] = (int)cylinders;
163589ae7d70SK. Y. Srinivasan 
163689ae7d70SK. Y. Srinivasan 	return 0;
163789ae7d70SK. Y. Srinivasan }
163889ae7d70SK. Y. Srinivasan 
storvsc_host_reset_handler(struct scsi_cmnd * scmnd)163989ae7d70SK. Y. Srinivasan static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
164089ae7d70SK. Y. Srinivasan {
164189ae7d70SK. Y. Srinivasan 	struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
164289ae7d70SK. Y. Srinivasan 	struct hv_device *device = host_dev->dev;
164389ae7d70SK. Y. Srinivasan 
164489ae7d70SK. Y. Srinivasan 	struct storvsc_device *stor_device;
164589ae7d70SK. Y. Srinivasan 	struct storvsc_cmd_request *request;
164689ae7d70SK. Y. Srinivasan 	struct vstor_packet *vstor_packet;
164789ae7d70SK. Y. Srinivasan 	int ret, t;
164889ae7d70SK. Y. Srinivasan 
164989ae7d70SK. Y. Srinivasan 	stor_device = get_out_stor_device(device);
165089ae7d70SK. Y. Srinivasan 	if (!stor_device)
165189ae7d70SK. Y. Srinivasan 		return FAILED;
165289ae7d70SK. Y. Srinivasan 
165389ae7d70SK. Y. Srinivasan 	request = &stor_device->reset_request;
165489ae7d70SK. Y. Srinivasan 	vstor_packet = &request->vstor_packet;
16550a765665SAndres Beltran 	memset(vstor_packet, 0, sizeof(struct vstor_packet));
165689ae7d70SK. Y. Srinivasan 
165789ae7d70SK. Y. Srinivasan 	init_completion(&request->wait_event);
165889ae7d70SK. Y. Srinivasan 
165989ae7d70SK. Y. Srinivasan 	vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
166089ae7d70SK. Y. Srinivasan 	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
166189ae7d70SK. Y. Srinivasan 	vstor_packet->vm_srb.path_id = stor_device->path_id;
166289ae7d70SK. Y. Srinivasan 
166389ae7d70SK. Y. Srinivasan 	ret = vmbus_sendpacket(device->channel, vstor_packet,
166486c8fb4dSSaurabh Sengar 			       sizeof(struct vstor_packet),
1665bf5fd8caSAndrea Parri (Microsoft) 			       VMBUS_RQST_RESET,
166689ae7d70SK. Y. Srinivasan 			       VM_PKT_DATA_INBAND,
166789ae7d70SK. Y. Srinivasan 			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
166889ae7d70SK. Y. Srinivasan 	if (ret != 0)
166989ae7d70SK. Y. Srinivasan 		return FAILED;
167089ae7d70SK. Y. Srinivasan 
167189ae7d70SK. Y. Srinivasan 	t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
167289ae7d70SK. Y. Srinivasan 	if (t == 0)
167389ae7d70SK. Y. Srinivasan 		return TIMEOUT_ERROR;
167489ae7d70SK. Y. Srinivasan 
167589ae7d70SK. Y. Srinivasan 
167689ae7d70SK. Y. Srinivasan 	/*
167789ae7d70SK. Y. Srinivasan 	 * At this point, all outstanding requests in the adapter
167889ae7d70SK. Y. Srinivasan 	 * should have been flushed out and return to us
16795c1b10abSK. Y. Srinivasan 	 * There is a potential race here where the host may be in
16805c1b10abSK. Y. Srinivasan 	 * the process of responding when we return from here.
16815c1b10abSK. Y. Srinivasan 	 * Just wait for all in-transit packets to be accounted for
16825c1b10abSK. Y. Srinivasan 	 * before we return from here.
168389ae7d70SK. Y. Srinivasan 	 */
16845c1b10abSK. Y. Srinivasan 	storvsc_wait_to_drain(stor_device);
168589ae7d70SK. Y. Srinivasan 
168689ae7d70SK. Y. Srinivasan 	return SUCCESS;
168789ae7d70SK. Y. Srinivasan }
168889ae7d70SK. Y. Srinivasan 
168956b26e69SK. Y. Srinivasan /*
169056b26e69SK. Y. Srinivasan  * The host guarantees to respond to each command, although I/O latencies might
169156b26e69SK. Y. Srinivasan  * be unbounded on Azure.  Reset the timer unconditionally to give the host a
169256b26e69SK. Y. Srinivasan  * chance to perform EH.
169356b26e69SK. Y. Srinivasan  */
storvsc_eh_timed_out(struct scsi_cmnd * scmnd)1694dee7121eSBart Van Assche static enum scsi_timeout_action storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
169556b26e69SK. Y. Srinivasan {
1696dee7121eSBart Van Assche 	return SCSI_EH_RESET_TIMER;
169756b26e69SK. Y. Srinivasan }
169856b26e69SK. Y. Srinivasan 
storvsc_scsi_cmd_ok(struct scsi_cmnd * scmnd)169989ae7d70SK. Y. Srinivasan static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
170089ae7d70SK. Y. Srinivasan {
170189ae7d70SK. Y. Srinivasan 	bool allowed = true;
170289ae7d70SK. Y. Srinivasan 	u8 scsi_op = scmnd->cmnd[0];
170389ae7d70SK. Y. Srinivasan 
170489ae7d70SK. Y. Srinivasan 	switch (scsi_op) {
17053e8f4f40SOlaf Hering 	/* the host does not handle WRITE_SAME, log accident usage */
17063e8f4f40SOlaf Hering 	case WRITE_SAME:
170789ae7d70SK. Y. Srinivasan 	/*
170889ae7d70SK. Y. Srinivasan 	 * smartd sends this command and the host does not handle
170989ae7d70SK. Y. Srinivasan 	 * this. So, don't send it.
171089ae7d70SK. Y. Srinivasan 	 */
171189ae7d70SK. Y. Srinivasan 	case SET_WINDOW:
1712d4674859SMichael Kelley 		set_host_byte(scmnd, DID_ERROR);
171389ae7d70SK. Y. Srinivasan 		allowed = false;
171489ae7d70SK. Y. Srinivasan 		break;
171589ae7d70SK. Y. Srinivasan 	default:
171689ae7d70SK. Y. Srinivasan 		break;
171789ae7d70SK. Y. Srinivasan 	}
171889ae7d70SK. Y. Srinivasan 	return allowed;
171989ae7d70SK. Y. Srinivasan }
172089ae7d70SK. Y. Srinivasan 
storvsc_queuecommand(struct Scsi_Host * host,struct scsi_cmnd * scmnd)172189ae7d70SK. Y. Srinivasan static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
172289ae7d70SK. Y. Srinivasan {
172389ae7d70SK. Y. Srinivasan 	int ret;
172489ae7d70SK. Y. Srinivasan 	struct hv_host_device *host_dev = shost_priv(host);
172589ae7d70SK. Y. Srinivasan 	struct hv_device *dev = host_dev->dev;
1726ead3700dSChristoph Hellwig 	struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd);
172789ae7d70SK. Y. Srinivasan 	struct scatterlist *sgl;
172889ae7d70SK. Y. Srinivasan 	struct vmscsi_request *vm_srb;
1729be0cf6caSK. Y. Srinivasan 	struct vmbus_packet_mpb_array  *payload;
1730be0cf6caSK. Y. Srinivasan 	u32 payload_sz;
1731be0cf6caSK. Y. Srinivasan 	u32 length;
173289ae7d70SK. Y. Srinivasan 
17332492fd7aSKeith Mange 	if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) {
17348caf92d8SK. Y. Srinivasan 		/*
17358caf92d8SK. Y. Srinivasan 		 * On legacy hosts filter unimplemented commands.
17368caf92d8SK. Y. Srinivasan 		 * Future hosts are expected to correctly handle
17378caf92d8SK. Y. Srinivasan 		 * unsupported commands. Furthermore, it is
17388caf92d8SK. Y. Srinivasan 		 * possible that some of the currently
17398caf92d8SK. Y. Srinivasan 		 * unsupported commands maybe supported in
17408caf92d8SK. Y. Srinivasan 		 * future versions of the host.
17418caf92d8SK. Y. Srinivasan 		 */
174289ae7d70SK. Y. Srinivasan 		if (!storvsc_scsi_cmd_ok(scmnd)) {
17430c31fa0eSBart Van Assche 			scsi_done(scmnd);
174489ae7d70SK. Y. Srinivasan 			return 0;
174589ae7d70SK. Y. Srinivasan 		}
17468caf92d8SK. Y. Srinivasan 	}
174789ae7d70SK. Y. Srinivasan 
174889ae7d70SK. Y. Srinivasan 	/* Setup the cmd request */
174989ae7d70SK. Y. Srinivasan 	cmd_request->cmd = scmnd;
175089ae7d70SK. Y. Srinivasan 
17510a765665SAndres Beltran 	memset(&cmd_request->vstor_packet, 0, sizeof(struct vstor_packet));
175289ae7d70SK. Y. Srinivasan 	vm_srb = &cmd_request->vstor_packet.vm_srb;
175386c8fb4dSSaurabh Sengar 	vm_srb->time_out_value = 60;
175489ae7d70SK. Y. Srinivasan 
175586c8fb4dSSaurabh Sengar 	vm_srb->srb_flags |=
17568cf308e1SK. Y. Srinivasan 		SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
175789ae7d70SK. Y. Srinivasan 
17583cd6d3d9SLong Li 	if (scmnd->device->tagged_supported) {
175986c8fb4dSSaurabh Sengar 		vm_srb->srb_flags |=
17603cd6d3d9SLong Li 		(SRB_FLAGS_QUEUE_ACTION_ENABLE | SRB_FLAGS_NO_QUEUE_FREEZE);
176186c8fb4dSSaurabh Sengar 		vm_srb->queue_tag = SP_UNTAGGED;
176286c8fb4dSSaurabh Sengar 		vm_srb->queue_action = SRB_SIMPLE_TAG_REQUEST;
17633cd6d3d9SLong Li 	}
17643cd6d3d9SLong Li 
176589ae7d70SK. Y. Srinivasan 	/* Build the SRB */
176689ae7d70SK. Y. Srinivasan 	switch (scmnd->sc_data_direction) {
176789ae7d70SK. Y. Srinivasan 	case DMA_TO_DEVICE:
176889ae7d70SK. Y. Srinivasan 		vm_srb->data_in = WRITE_TYPE;
176986c8fb4dSSaurabh Sengar 		vm_srb->srb_flags |= SRB_FLAGS_DATA_OUT;
177089ae7d70SK. Y. Srinivasan 		break;
177189ae7d70SK. Y. Srinivasan 	case DMA_FROM_DEVICE:
177289ae7d70SK. Y. Srinivasan 		vm_srb->data_in = READ_TYPE;
177386c8fb4dSSaurabh Sengar 		vm_srb->srb_flags |= SRB_FLAGS_DATA_IN;
177489ae7d70SK. Y. Srinivasan 		break;
1775cb1cf080SVitaly Kuznetsov 	case DMA_NONE:
177689ae7d70SK. Y. Srinivasan 		vm_srb->data_in = UNKNOWN_TYPE;
177786c8fb4dSSaurabh Sengar 		vm_srb->srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER;
177889ae7d70SK. Y. Srinivasan 		break;
1779cb1cf080SVitaly Kuznetsov 	default:
1780cb1cf080SVitaly Kuznetsov 		/*
1781cb1cf080SVitaly Kuznetsov 		 * This is DMA_BIDIRECTIONAL or something else we are never
1782cb1cf080SVitaly Kuznetsov 		 * supposed to see here.
1783cb1cf080SVitaly Kuznetsov 		 */
1784cb1cf080SVitaly Kuznetsov 		WARN(1, "Unexpected data direction: %d\n",
1785cb1cf080SVitaly Kuznetsov 		     scmnd->sc_data_direction);
1786cb1cf080SVitaly Kuznetsov 		return -EINVAL;
178789ae7d70SK. Y. Srinivasan 	}
178889ae7d70SK. Y. Srinivasan 
178989ae7d70SK. Y. Srinivasan 
179089ae7d70SK. Y. Srinivasan 	vm_srb->port_number = host_dev->port;
179189ae7d70SK. Y. Srinivasan 	vm_srb->path_id = scmnd->device->channel;
179289ae7d70SK. Y. Srinivasan 	vm_srb->target_id = scmnd->device->id;
179389ae7d70SK. Y. Srinivasan 	vm_srb->lun = scmnd->device->lun;
179489ae7d70SK. Y. Srinivasan 
179589ae7d70SK. Y. Srinivasan 	vm_srb->cdb_length = scmnd->cmd_len;
179689ae7d70SK. Y. Srinivasan 
179789ae7d70SK. Y. Srinivasan 	memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
179889ae7d70SK. Y. Srinivasan 
179989ae7d70SK. Y. Srinivasan 	sgl = (struct scatterlist *)scsi_sglist(scmnd);
180089ae7d70SK. Y. Srinivasan 
1801be0cf6caSK. Y. Srinivasan 	length = scsi_bufflen(scmnd);
1802be0cf6caSK. Y. Srinivasan 	payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
1803*7df68980SLong Li 	payload->range.len = 0;
18044e81a6cbSMichael Kelley 	payload_sz = 0;
1805be0cf6caSK. Y. Srinivasan 
18060bd2fbeeSYueHaibing 	if (scsi_sg_count(scmnd)) {
18073d9c3dccSMichael Kelley 		unsigned long offset_in_hvpg = offset_in_hvpage(sgl->offset);
18088f437105SBoqun Feng 		unsigned int hvpg_count = HVPFN_UP(offset_in_hvpg + length);
1809743b237cSTianyu Lan 		struct scatterlist *sg;
1810743b237cSTianyu Lan 		unsigned long hvpfn, hvpfns_to_add;
18110bd2fbeeSYueHaibing 		int j, i = 0, sg_count;
1812be0cf6caSK. Y. Srinivasan 
18138f437105SBoqun Feng 		payload_sz = (hvpg_count * sizeof(u64) +
1814be0cf6caSK. Y. Srinivasan 			      sizeof(struct vmbus_packet_mpb_array));
18154e81a6cbSMichael Kelley 
18164e81a6cbSMichael Kelley 		if (hvpg_count > MAX_PAGE_BUFFER_COUNT) {
1817b0120d99SCathy Avery 			payload = kzalloc(payload_sz, GFP_ATOMIC);
181881988a0eSVitaly Kuznetsov 			if (!payload)
1819be0cf6caSK. Y. Srinivasan 				return SCSI_MLQUEUE_DEVICE_BUSY;
1820be0cf6caSK. Y. Srinivasan 		}
1821be0cf6caSK. Y. Srinivasan 
1822be0cf6caSK. Y. Srinivasan 		payload->range.len = length;
18238f437105SBoqun Feng 		payload->range.offset = offset_in_hvpg;
1824be0cf6caSK. Y. Srinivasan 
1825743b237cSTianyu Lan 		sg_count = scsi_dma_map(scmnd);
18264eea5332SJuan Vazquez 		if (sg_count < 0) {
18274eea5332SJuan Vazquez 			ret = SCSI_MLQUEUE_DEVICE_BUSY;
18284eea5332SJuan Vazquez 			goto err_free_payload;
18294eea5332SJuan Vazquez 		}
18303d9c3dccSMichael Kelley 
1831743b237cSTianyu Lan 		for_each_sg(sgl, sg, sg_count, j) {
18328f437105SBoqun Feng 			/*
1833743b237cSTianyu Lan 			 * Init values for the current sgl entry. hvpfns_to_add
1834743b237cSTianyu Lan 			 * is in units of Hyper-V size pages. Handling the
1835743b237cSTianyu Lan 			 * PAGE_SIZE != HV_HYP_PAGE_SIZE case also handles
1836743b237cSTianyu Lan 			 * values of sgl->offset that are larger than PAGE_SIZE.
1837743b237cSTianyu Lan 			 * Such offsets are handled even on other than the first
1838743b237cSTianyu Lan 			 * sgl entry, provided they are a multiple of PAGE_SIZE.
18398f437105SBoqun Feng 			 */
1840743b237cSTianyu Lan 			hvpfn = HVPFN_DOWN(sg_dma_address(sg));
1841743b237cSTianyu Lan 			hvpfns_to_add = HVPFN_UP(sg_dma_address(sg) +
1842743b237cSTianyu Lan 						 sg_dma_len(sg)) - hvpfn;
18438f437105SBoqun Feng 
18448f437105SBoqun Feng 			/*
18453d9c3dccSMichael Kelley 			 * Fill the next portion of the PFN array with
18463d9c3dccSMichael Kelley 			 * sequential Hyper-V PFNs for the continguous physical
18473d9c3dccSMichael Kelley 			 * memory described by the sgl entry. The end of the
18483d9c3dccSMichael Kelley 			 * last sgl should be reached at the same time that
18493d9c3dccSMichael Kelley 			 * the PFN array is filled.
18508f437105SBoqun Feng 			 */
18513d9c3dccSMichael Kelley 			while (hvpfns_to_add--)
18523d9c3dccSMichael Kelley 				payload->range.pfn_array[i++] = hvpfn++;
18538f437105SBoqun Feng 		}
185489ae7d70SK. Y. Srinivasan 	}
185589ae7d70SK. Y. Srinivasan 
1856be0cf6caSK. Y. Srinivasan 	cmd_request->payload = payload;
1857be0cf6caSK. Y. Srinivasan 	cmd_request->payload_sz = payload_sz;
1858be0cf6caSK. Y. Srinivasan 
185989ae7d70SK. Y. Srinivasan 	/* Invokes the vsc to start an IO */
1860d86adf48SK. Y. Srinivasan 	ret = storvsc_do_io(dev, cmd_request, get_cpu());
1861d86adf48SK. Y. Srinivasan 	put_cpu();
186289ae7d70SK. Y. Srinivasan 
186367ff3d0aSMichael Kelley 	if (ret)
186467ff3d0aSMichael Kelley 		scsi_dma_unmap(scmnd);
186567ff3d0aSMichael Kelley 
186689ae7d70SK. Y. Srinivasan 	if (ret == -EAGAIN) {
186789ae7d70SK. Y. Srinivasan 		/* no more space */
18684eea5332SJuan Vazquez 		ret = SCSI_MLQUEUE_DEVICE_BUSY;
18694eea5332SJuan Vazquez 		goto err_free_payload;
187089ae7d70SK. Y. Srinivasan 	}
187189ae7d70SK. Y. Srinivasan 
187289ae7d70SK. Y. Srinivasan 	return 0;
18734eea5332SJuan Vazquez 
18744eea5332SJuan Vazquez err_free_payload:
18754eea5332SJuan Vazquez 	if (payload_sz > sizeof(cmd_request->mpb))
18764eea5332SJuan Vazquez 		kfree(payload);
18774eea5332SJuan Vazquez 
18784eea5332SJuan Vazquez 	return ret;
187989ae7d70SK. Y. Srinivasan }
188089ae7d70SK. Y. Srinivasan 
188189ae7d70SK. Y. Srinivasan static struct scsi_host_template scsi_driver = {
188289ae7d70SK. Y. Srinivasan 	.module	=		THIS_MODULE,
188389ae7d70SK. Y. Srinivasan 	.name =			"storvsc_host_t",
1884ead3700dSChristoph Hellwig 	.cmd_size =             sizeof(struct storvsc_cmd_request),
188589ae7d70SK. Y. Srinivasan 	.bios_param =		storvsc_get_chs,
188689ae7d70SK. Y. Srinivasan 	.queuecommand =		storvsc_queuecommand,
188789ae7d70SK. Y. Srinivasan 	.eh_host_reset_handler =	storvsc_host_reset_handler,
1888ead3700dSChristoph Hellwig 	.proc_name =		"storvsc_host",
188956b26e69SK. Y. Srinivasan 	.eh_timed_out =		storvsc_eh_timed_out,
1890f1c635b4SStephen Hemminger 	.slave_alloc =		storvsc_device_alloc,
189189ae7d70SK. Y. Srinivasan 	.slave_configure =	storvsc_device_configure,
1892cabe92a5SMichael Kelley (EOSG) 	.cmd_per_lun =		2048,
189389ae7d70SK. Y. Srinivasan 	.this_id =		-1,
189483eed459SChristoph Hellwig 	/* Ensure there are no gaps in presented sgls */
18951d3e0980SSaurabh Sengar 	.virt_boundary_mask =	HV_HYP_PAGE_SIZE - 1,
189654b2b50cSMartin K. Petersen 	.no_write_same =	1,
1897f64dad26SK. Y. Srinivasan 	.track_queue_depth =	1,
1898adfbd028SBranden Bonaby 	.change_queue_depth =	storvsc_change_queue_depth,
189989ae7d70SK. Y. Srinivasan };
190089ae7d70SK. Y. Srinivasan 
190189ae7d70SK. Y. Srinivasan enum {
190289ae7d70SK. Y. Srinivasan 	SCSI_GUID,
190389ae7d70SK. Y. Srinivasan 	IDE_GUID,
1904bde6d0f9SK. Y. Srinivasan 	SFC_GUID,
190589ae7d70SK. Y. Srinivasan };
190689ae7d70SK. Y. Srinivasan 
190789ae7d70SK. Y. Srinivasan static const struct hv_vmbus_device_id id_table[] = {
190889ae7d70SK. Y. Srinivasan 	/* SCSI guid */
190935c3bc20SK. Y. Srinivasan 	{ HV_SCSI_GUID,
191035c3bc20SK. Y. Srinivasan 	  .driver_data = SCSI_GUID
191135c3bc20SK. Y. Srinivasan 	},
191289ae7d70SK. Y. Srinivasan 	/* IDE guid */
191335c3bc20SK. Y. Srinivasan 	{ HV_IDE_GUID,
191435c3bc20SK. Y. Srinivasan 	  .driver_data = IDE_GUID
191535c3bc20SK. Y. Srinivasan 	},
1916bde6d0f9SK. Y. Srinivasan 	/* Fibre Channel GUID */
1917bde6d0f9SK. Y. Srinivasan 	{
1918bde6d0f9SK. Y. Srinivasan 	  HV_SYNTHFC_GUID,
1919bde6d0f9SK. Y. Srinivasan 	  .driver_data = SFC_GUID
1920bde6d0f9SK. Y. Srinivasan 	},
192189ae7d70SK. Y. Srinivasan 	{ },
192289ae7d70SK. Y. Srinivasan };
192389ae7d70SK. Y. Srinivasan 
192489ae7d70SK. Y. Srinivasan MODULE_DEVICE_TABLE(vmbus, id_table);
192589ae7d70SK. Y. Srinivasan 
192656fb1058SDexuan Cui static const struct { guid_t guid; } fc_guid = { HV_SYNTHFC_GUID };
192756fb1058SDexuan Cui 
hv_dev_is_fc(struct hv_device * hv_dev)192856fb1058SDexuan Cui static bool hv_dev_is_fc(struct hv_device *hv_dev)
192956fb1058SDexuan Cui {
193056fb1058SDexuan Cui 	return guid_equal(&fc_guid.guid, &hv_dev->dev_type);
193156fb1058SDexuan Cui }
193256fb1058SDexuan Cui 
storvsc_probe(struct hv_device * device,const struct hv_vmbus_device_id * dev_id)193389ae7d70SK. Y. Srinivasan static int storvsc_probe(struct hv_device *device,
193489ae7d70SK. Y. Srinivasan 			const struct hv_vmbus_device_id *dev_id)
193589ae7d70SK. Y. Srinivasan {
193689ae7d70SK. Y. Srinivasan 	int ret;
1937f458aadaSK. Y. Srinivasan 	int num_cpus = num_online_cpus();
1938a81a38ccSMelanie Plageman (Microsoft) 	int num_present_cpus = num_present_cpus();
193989ae7d70SK. Y. Srinivasan 	struct Scsi_Host *host;
194089ae7d70SK. Y. Srinivasan 	struct hv_host_device *host_dev;
194189ae7d70SK. Y. Srinivasan 	bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
1942dac58241SK. Y. Srinivasan 	bool is_fc = ((dev_id->driver_data == SFC_GUID) ? true : false);
194389ae7d70SK. Y. Srinivasan 	int target = 0;
194489ae7d70SK. Y. Srinivasan 	struct storvsc_device *stor_device;
1945f458aadaSK. Y. Srinivasan 	int max_sub_channels = 0;
19461d3e0980SSaurabh Sengar 	u32 max_xfer_bytes;
194789ae7d70SK. Y. Srinivasan 
19488b612fa2SK. Y. Srinivasan 	/*
1949106b98a5SMichael Kelley 	 * We support sub-channels for storage on SCSI and FC controllers.
1950f458aadaSK. Y. Srinivasan 	 * The number of sub-channels offerred is based on the number of
1951f458aadaSK. Y. Srinivasan 	 * VCPUs in the guest.
1952f458aadaSK. Y. Srinivasan 	 */
1953f286299cSLong Li 	if (!dev_is_ide)
1954f286299cSLong Li 		max_sub_channels =
1955f286299cSLong Li 			(num_cpus - 1) / storvsc_vcpus_per_sub_channel;
19568b612fa2SK. Y. Srinivasan 
19572217a47dSLong Li 	scsi_driver.can_queue = max_outstanding_req_per_channel *
19582217a47dSLong Li 				(max_sub_channels + 1) *
19592217a47dSLong Li 				(100 - ring_avail_percent_lowater) / 100;
1960f458aadaSK. Y. Srinivasan 
196189ae7d70SK. Y. Srinivasan 	host = scsi_host_alloc(&scsi_driver,
196289ae7d70SK. Y. Srinivasan 			       sizeof(struct hv_host_device));
196389ae7d70SK. Y. Srinivasan 	if (!host)
196489ae7d70SK. Y. Srinivasan 		return -ENOMEM;
196589ae7d70SK. Y. Srinivasan 
196689ae7d70SK. Y. Srinivasan 	host_dev = shost_priv(host);
196789ae7d70SK. Y. Srinivasan 	memset(host_dev, 0, sizeof(struct hv_host_device));
196889ae7d70SK. Y. Srinivasan 
196989ae7d70SK. Y. Srinivasan 	host_dev->port = host->host_no;
197089ae7d70SK. Y. Srinivasan 	host_dev->dev = device;
1971c58cc70fSLong Li 	host_dev->host = host;
197289ae7d70SK. Y. Srinivasan 
197389ae7d70SK. Y. Srinivasan 
197489ae7d70SK. Y. Srinivasan 	stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
197589ae7d70SK. Y. Srinivasan 	if (!stor_device) {
197689ae7d70SK. Y. Srinivasan 		ret = -ENOMEM;
197789ae7d70SK. Y. Srinivasan 		goto err_out0;
197889ae7d70SK. Y. Srinivasan 	}
197989ae7d70SK. Y. Srinivasan 
198089ae7d70SK. Y. Srinivasan 	stor_device->destroy = false;
198189ae7d70SK. Y. Srinivasan 	init_waitqueue_head(&stor_device->waiting_to_drain);
198289ae7d70SK. Y. Srinivasan 	stor_device->device = device;
198389ae7d70SK. Y. Srinivasan 	stor_device->host = host;
198421d2052cSAndrea Parri (Microsoft) 	spin_lock_init(&stor_device->lock);
198589ae7d70SK. Y. Srinivasan 	hv_set_drvdata(device, stor_device);
1986743b237cSTianyu Lan 	dma_set_min_align_mask(&device->device, HV_HYP_PAGE_SIZE - 1);
198789ae7d70SK. Y. Srinivasan 
198889ae7d70SK. Y. Srinivasan 	stor_device->port_number = host->host_no;
1989acd2eda2SMichael Kelley 	ret = storvsc_connect_to_vsp(device, aligned_ringbuffer_size, is_fc);
199089ae7d70SK. Y. Srinivasan 	if (ret)
199189ae7d70SK. Y. Srinivasan 		goto err_out1;
199289ae7d70SK. Y. Srinivasan 
199389ae7d70SK. Y. Srinivasan 	host_dev->path = stor_device->path_id;
199489ae7d70SK. Y. Srinivasan 	host_dev->target = stor_device->target_id;
199589ae7d70SK. Y. Srinivasan 
19964cd83ecdSK. Y. Srinivasan 	switch (dev_id->driver_data) {
19974cd83ecdSK. Y. Srinivasan 	case SFC_GUID:
19984cd83ecdSK. Y. Srinivasan 		host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
19994cd83ecdSK. Y. Srinivasan 		host->max_id = STORVSC_FC_MAX_TARGETS;
20004cd83ecdSK. Y. Srinivasan 		host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
2001dac58241SK. Y. Srinivasan #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2002dac58241SK. Y. Srinivasan 		host->transportt = fc_transport_template;
2003dac58241SK. Y. Srinivasan #endif
20044cd83ecdSK. Y. Srinivasan 		break;
20054cd83ecdSK. Y. Srinivasan 
20064cd83ecdSK. Y. Srinivasan 	case SCSI_GUID:
2007106b98a5SMichael Kelley 		host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
2008106b98a5SMichael Kelley 		host->max_id = STORVSC_MAX_TARGETS;
2009106b98a5SMichael Kelley 		host->max_channel = STORVSC_MAX_CHANNELS - 1;
20104cd83ecdSK. Y. Srinivasan 		break;
20114cd83ecdSK. Y. Srinivasan 
20124cd83ecdSK. Y. Srinivasan 	default:
20134cd83ecdSK. Y. Srinivasan 		host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
20144cd83ecdSK. Y. Srinivasan 		host->max_id = STORVSC_IDE_MAX_TARGETS;
20154cd83ecdSK. Y. Srinivasan 		host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
20164cd83ecdSK. Y. Srinivasan 		break;
20174cd83ecdSK. Y. Srinivasan 	}
201889ae7d70SK. Y. Srinivasan 	/* max cmd length */
201989ae7d70SK. Y. Srinivasan 	host->max_cmd_len = STORVSC_MAX_CMD_LEN;
2020be0cf6caSK. Y. Srinivasan 	/*
20211d3e0980SSaurabh Sengar 	 * Any reasonable Hyper-V configuration should provide
20221d3e0980SSaurabh Sengar 	 * max_transfer_bytes value aligning to HV_HYP_PAGE_SIZE,
20231d3e0980SSaurabh Sengar 	 * protecting it from any weird value.
2024be0cf6caSK. Y. Srinivasan 	 */
20251d3e0980SSaurabh Sengar 	max_xfer_bytes = round_down(stor_device->max_transfer_bytes, HV_HYP_PAGE_SIZE);
2026010c1e1cSMichael Kelley 	if (is_fc)
2027010c1e1cSMichael Kelley 		max_xfer_bytes = min(max_xfer_bytes, STORVSC_FC_MAX_XFER_SIZE);
2028010c1e1cSMichael Kelley 
20291d3e0980SSaurabh Sengar 	/* max_hw_sectors_kb */
20301d3e0980SSaurabh Sengar 	host->max_sectors = max_xfer_bytes >> 9;
20311d3e0980SSaurabh Sengar 	/*
20321d3e0980SSaurabh Sengar 	 * There are 2 requirements for Hyper-V storvsc sgl segments,
20331d3e0980SSaurabh Sengar 	 * based on which the below calculation for max segments is
20341d3e0980SSaurabh Sengar 	 * done:
20351d3e0980SSaurabh Sengar 	 *
20361d3e0980SSaurabh Sengar 	 * 1. Except for the first and last sgl segment, all sgl segments
20371d3e0980SSaurabh Sengar 	 *    should be align to HV_HYP_PAGE_SIZE, that also means the
20381d3e0980SSaurabh Sengar 	 *    maximum number of segments in a sgl can be calculated by
20391d3e0980SSaurabh Sengar 	 *    dividing the total max transfer length by HV_HYP_PAGE_SIZE.
20401d3e0980SSaurabh Sengar 	 *
20411d3e0980SSaurabh Sengar 	 * 2. Except for the first and last, each entry in the SGL must
20421d3e0980SSaurabh Sengar 	 *    have an offset that is a multiple of HV_HYP_PAGE_SIZE.
20431d3e0980SSaurabh Sengar 	 */
20441d3e0980SSaurabh Sengar 	host->sg_tablesize = (max_xfer_bytes >> HV_HYP_PAGE_SHIFT) + 1;
2045d86adf48SK. Y. Srinivasan 	/*
20467b571c19SLong Li 	 * For non-IDE disks, the host supports multiple channels.
2047d86adf48SK. Y. Srinivasan 	 * Set the number of HW queues we are supporting.
2048d86adf48SK. Y. Srinivasan 	 */
2049a81a38ccSMelanie Plageman (Microsoft) 	if (!dev_is_ide) {
2050a81a38ccSMelanie Plageman (Microsoft) 		if (storvsc_max_hw_queues > num_present_cpus) {
2051a81a38ccSMelanie Plageman (Microsoft) 			storvsc_max_hw_queues = 0;
2052a81a38ccSMelanie Plageman (Microsoft) 			storvsc_log(device, STORVSC_LOGGING_WARN,
2053a81a38ccSMelanie Plageman (Microsoft) 				"Resetting invalid storvsc_max_hw_queues value to default.\n");
2054a81a38ccSMelanie Plageman (Microsoft) 		}
2055a81a38ccSMelanie Plageman (Microsoft) 		if (storvsc_max_hw_queues)
2056a81a38ccSMelanie Plageman (Microsoft) 			host->nr_hw_queues = storvsc_max_hw_queues;
2057a81a38ccSMelanie Plageman (Microsoft) 		else
2058a81a38ccSMelanie Plageman (Microsoft) 			host->nr_hw_queues = num_present_cpus;
2059a81a38ccSMelanie Plageman (Microsoft) 	}
2060be0cf6caSK. Y. Srinivasan 
2061436ad941SCathy Avery 	/*
2062436ad941SCathy Avery 	 * Set the error handler work queue.
2063436ad941SCathy Avery 	 */
2064436ad941SCathy Avery 	host_dev->handle_error_wq =
2065436ad941SCathy Avery 			alloc_ordered_workqueue("storvsc_error_wq_%d",
2066d957e7ffSSaurabh Sengar 						0,
2067436ad941SCathy Avery 						host->host_no);
20686112ff4eSJing Xiangfeng 	if (!host_dev->handle_error_wq) {
20696112ff4eSJing Xiangfeng 		ret = -ENOMEM;
2070436ad941SCathy Avery 		goto err_out2;
20716112ff4eSJing Xiangfeng 	}
2072c58cc70fSLong Li 	INIT_WORK(&host_dev->host_scan_work, storvsc_host_scan);
207389ae7d70SK. Y. Srinivasan 	/* Register the HBA and start the scsi bus scan */
207489ae7d70SK. Y. Srinivasan 	ret = scsi_add_host(host, &device->device);
207589ae7d70SK. Y. Srinivasan 	if (ret != 0)
2076436ad941SCathy Avery 		goto err_out3;
207789ae7d70SK. Y. Srinivasan 
207889ae7d70SK. Y. Srinivasan 	if (!dev_is_ide) {
207989ae7d70SK. Y. Srinivasan 		scsi_scan_host(host);
208089ae7d70SK. Y. Srinivasan 	} else {
208189ae7d70SK. Y. Srinivasan 		target = (device->dev_instance.b[5] << 8 |
208289ae7d70SK. Y. Srinivasan 			 device->dev_instance.b[4]);
208389ae7d70SK. Y. Srinivasan 		ret = scsi_add_device(host, 0, target, 0);
2084daf0cd44SCathy Avery 		if (ret)
2085436ad941SCathy Avery 			goto err_out4;
208689ae7d70SK. Y. Srinivasan 	}
2087dac58241SK. Y. Srinivasan #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2088dac58241SK. Y. Srinivasan 	if (host->transportt == fc_transport_template) {
2089daf0cd44SCathy Avery 		struct fc_rport_identifiers ids = {
2090daf0cd44SCathy Avery 			.roles = FC_PORT_ROLE_FCP_DUMMY_INITIATOR,
2091daf0cd44SCathy Avery 		};
2092daf0cd44SCathy Avery 
2093dac58241SK. Y. Srinivasan 		fc_host_node_name(host) = stor_device->node_name;
2094dac58241SK. Y. Srinivasan 		fc_host_port_name(host) = stor_device->port_name;
2095daf0cd44SCathy Avery 		stor_device->rport = fc_remote_port_add(host, 0, &ids);
2096ca8dc694SDan Carpenter 		if (!stor_device->rport) {
2097ca8dc694SDan Carpenter 			ret = -ENOMEM;
2098436ad941SCathy Avery 			goto err_out4;
2099dac58241SK. Y. Srinivasan 		}
2100ca8dc694SDan Carpenter 	}
2101dac58241SK. Y. Srinivasan #endif
210289ae7d70SK. Y. Srinivasan 	return 0;
210389ae7d70SK. Y. Srinivasan 
2104436ad941SCathy Avery err_out4:
2105daf0cd44SCathy Avery 	scsi_remove_host(host);
2106daf0cd44SCathy Avery 
2107436ad941SCathy Avery err_out3:
2108436ad941SCathy Avery 	destroy_workqueue(host_dev->handle_error_wq);
2109436ad941SCathy Avery 
211089ae7d70SK. Y. Srinivasan err_out2:
211189ae7d70SK. Y. Srinivasan 	/*
211289ae7d70SK. Y. Srinivasan 	 * Once we have connected with the host, we would need to
2113635b241dSwangjianli 	 * invoke storvsc_dev_remove() to rollback this state and
211489ae7d70SK. Y. Srinivasan 	 * this call also frees up the stor_device; hence the jump around
211589ae7d70SK. Y. Srinivasan 	 * err_out1 label.
211689ae7d70SK. Y. Srinivasan 	 */
211789ae7d70SK. Y. Srinivasan 	storvsc_dev_remove(device);
211889ae7d70SK. Y. Srinivasan 	goto err_out0;
211989ae7d70SK. Y. Srinivasan 
212089ae7d70SK. Y. Srinivasan err_out1:
2121d86adf48SK. Y. Srinivasan 	kfree(stor_device->stor_chns);
212289ae7d70SK. Y. Srinivasan 	kfree(stor_device);
212389ae7d70SK. Y. Srinivasan 
212489ae7d70SK. Y. Srinivasan err_out0:
212589ae7d70SK. Y. Srinivasan 	scsi_host_put(host);
212689ae7d70SK. Y. Srinivasan 	return ret;
212789ae7d70SK. Y. Srinivasan }
212889ae7d70SK. Y. Srinivasan 
2129adfbd028SBranden Bonaby /* Change a scsi target's queue depth */
storvsc_change_queue_depth(struct scsi_device * sdev,int queue_depth)2130adfbd028SBranden Bonaby static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth)
2131adfbd028SBranden Bonaby {
2132adfbd028SBranden Bonaby 	if (queue_depth > scsi_driver.can_queue)
2133adfbd028SBranden Bonaby 		queue_depth = scsi_driver.can_queue;
2134adfbd028SBranden Bonaby 
2135adfbd028SBranden Bonaby 	return scsi_change_queue_depth(sdev, queue_depth);
2136adfbd028SBranden Bonaby }
2137adfbd028SBranden Bonaby 
storvsc_remove(struct hv_device * dev)213896ec2939SDawei Li static void storvsc_remove(struct hv_device *dev)
213989ae7d70SK. Y. Srinivasan {
214089ae7d70SK. Y. Srinivasan 	struct storvsc_device *stor_device = hv_get_drvdata(dev);
214189ae7d70SK. Y. Srinivasan 	struct Scsi_Host *host = stor_device->host;
2142436ad941SCathy Avery 	struct hv_host_device *host_dev = shost_priv(host);
214389ae7d70SK. Y. Srinivasan 
2144dac58241SK. Y. Srinivasan #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2145daf0cd44SCathy Avery 	if (host->transportt == fc_transport_template) {
2146daf0cd44SCathy Avery 		fc_remote_port_delete(stor_device->rport);
2147dac58241SK. Y. Srinivasan 		fc_remove_host(host);
2148daf0cd44SCathy Avery 	}
2149dac58241SK. Y. Srinivasan #endif
2150436ad941SCathy Avery 	destroy_workqueue(host_dev->handle_error_wq);
215189ae7d70SK. Y. Srinivasan 	scsi_remove_host(host);
215289ae7d70SK. Y. Srinivasan 	storvsc_dev_remove(dev);
215389ae7d70SK. Y. Srinivasan 	scsi_host_put(host);
215489ae7d70SK. Y. Srinivasan }
215589ae7d70SK. Y. Srinivasan 
storvsc_suspend(struct hv_device * hv_dev)215656fb1058SDexuan Cui static int storvsc_suspend(struct hv_device *hv_dev)
215756fb1058SDexuan Cui {
215856fb1058SDexuan Cui 	struct storvsc_device *stor_device = hv_get_drvdata(hv_dev);
215956fb1058SDexuan Cui 	struct Scsi_Host *host = stor_device->host;
216056fb1058SDexuan Cui 	struct hv_host_device *host_dev = shost_priv(host);
216156fb1058SDexuan Cui 
216256fb1058SDexuan Cui 	storvsc_wait_to_drain(stor_device);
216356fb1058SDexuan Cui 
216456fb1058SDexuan Cui 	drain_workqueue(host_dev->handle_error_wq);
216556fb1058SDexuan Cui 
216656fb1058SDexuan Cui 	vmbus_close(hv_dev->channel);
216756fb1058SDexuan Cui 
216856fb1058SDexuan Cui 	kfree(stor_device->stor_chns);
216956fb1058SDexuan Cui 	stor_device->stor_chns = NULL;
217056fb1058SDexuan Cui 
217156fb1058SDexuan Cui 	cpumask_clear(&stor_device->alloced_cpus);
217256fb1058SDexuan Cui 
217356fb1058SDexuan Cui 	return 0;
217456fb1058SDexuan Cui }
217556fb1058SDexuan Cui 
storvsc_resume(struct hv_device * hv_dev)217656fb1058SDexuan Cui static int storvsc_resume(struct hv_device *hv_dev)
217756fb1058SDexuan Cui {
217856fb1058SDexuan Cui 	int ret;
217956fb1058SDexuan Cui 
2180acd2eda2SMichael Kelley 	ret = storvsc_connect_to_vsp(hv_dev, aligned_ringbuffer_size,
218156fb1058SDexuan Cui 				     hv_dev_is_fc(hv_dev));
218256fb1058SDexuan Cui 	return ret;
218356fb1058SDexuan Cui }
218456fb1058SDexuan Cui 
218589ae7d70SK. Y. Srinivasan static struct hv_driver storvsc_drv = {
218689ae7d70SK. Y. Srinivasan 	.name = KBUILD_MODNAME,
218789ae7d70SK. Y. Srinivasan 	.id_table = id_table,
218889ae7d70SK. Y. Srinivasan 	.probe = storvsc_probe,
218989ae7d70SK. Y. Srinivasan 	.remove = storvsc_remove,
219056fb1058SDexuan Cui 	.suspend = storvsc_suspend,
219156fb1058SDexuan Cui 	.resume = storvsc_resume,
2192af0a5646SArjan van de Ven 	.driver = {
2193af0a5646SArjan van de Ven 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
2194af0a5646SArjan van de Ven 	},
219589ae7d70SK. Y. Srinivasan };
219689ae7d70SK. Y. Srinivasan 
2197dac58241SK. Y. Srinivasan #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2198dac58241SK. Y. Srinivasan static struct fc_function_template fc_transport_functions = {
2199dac58241SK. Y. Srinivasan 	.show_host_node_name = 1,
2200dac58241SK. Y. Srinivasan 	.show_host_port_name = 1,
2201dac58241SK. Y. Srinivasan };
2202dac58241SK. Y. Srinivasan #endif
2203dac58241SK. Y. Srinivasan 
storvsc_drv_init(void)220489ae7d70SK. Y. Srinivasan static int __init storvsc_drv_init(void)
220589ae7d70SK. Y. Srinivasan {
2206dac58241SK. Y. Srinivasan 	int ret;
220789ae7d70SK. Y. Srinivasan 
220889ae7d70SK. Y. Srinivasan 	/*
220989ae7d70SK. Y. Srinivasan 	 * Divide the ring buffer data size (which is 1 page less
221089ae7d70SK. Y. Srinivasan 	 * than the ring buffer size since that page is reserved for
221189ae7d70SK. Y. Srinivasan 	 * the ring buffer indices) by the max request size (which is
221289ae7d70SK. Y. Srinivasan 	 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
221389ae7d70SK. Y. Srinivasan 	 */
2214acd2eda2SMichael Kelley 	aligned_ringbuffer_size = VMBUS_RING_SIZE(storvsc_ringbuffer_size);
221589ae7d70SK. Y. Srinivasan 	max_outstanding_req_per_channel =
2216acd2eda2SMichael Kelley 		((aligned_ringbuffer_size - PAGE_SIZE) /
221789ae7d70SK. Y. Srinivasan 		ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
2218ab548fd2SAndrea Parri (Microsoft) 		sizeof(struct vstor_packet) + sizeof(u64),
221989ae7d70SK. Y. Srinivasan 		sizeof(u64)));
222089ae7d70SK. Y. Srinivasan 
2221dac58241SK. Y. Srinivasan #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2222dac58241SK. Y. Srinivasan 	fc_transport_template = fc_attach_transport(&fc_transport_functions);
2223dac58241SK. Y. Srinivasan 	if (!fc_transport_template)
2224dac58241SK. Y. Srinivasan 		return -ENODEV;
2225dac58241SK. Y. Srinivasan #endif
2226dac58241SK. Y. Srinivasan 
2227dac58241SK. Y. Srinivasan 	ret = vmbus_driver_register(&storvsc_drv);
2228dac58241SK. Y. Srinivasan 
2229dac58241SK. Y. Srinivasan #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2230dac58241SK. Y. Srinivasan 	if (ret)
2231dac58241SK. Y. Srinivasan 		fc_release_transport(fc_transport_template);
2232dac58241SK. Y. Srinivasan #endif
2233dac58241SK. Y. Srinivasan 
2234dac58241SK. Y. Srinivasan 	return ret;
223589ae7d70SK. Y. Srinivasan }
223689ae7d70SK. Y. Srinivasan 
storvsc_drv_exit(void)223789ae7d70SK. Y. Srinivasan static void __exit storvsc_drv_exit(void)
223889ae7d70SK. Y. Srinivasan {
223989ae7d70SK. Y. Srinivasan 	vmbus_driver_unregister(&storvsc_drv);
2240dac58241SK. Y. Srinivasan #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2241dac58241SK. Y. Srinivasan 	fc_release_transport(fc_transport_template);
2242dac58241SK. Y. Srinivasan #endif
224389ae7d70SK. Y. Srinivasan }
224489ae7d70SK. Y. Srinivasan 
224589ae7d70SK. Y. Srinivasan MODULE_LICENSE("GPL");
224689ae7d70SK. Y. Srinivasan MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
224789ae7d70SK. Y. Srinivasan module_init(storvsc_drv_init);
224889ae7d70SK. Y. Srinivasan module_exit(storvsc_drv_exit);
2249