xref: /OK3568_Linux_fs/kernel/drivers/scsi/storvsc_drv.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2009, Microsoft Corporation.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Authors:
6*4882a593Smuzhiyun  *   Haiyang Zhang <haiyangz@microsoft.com>
7*4882a593Smuzhiyun  *   Hank Janssen  <hjanssen@microsoft.com>
8*4882a593Smuzhiyun  *   K. Y. Srinivasan <kys@microsoft.com>
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/wait.h>
13*4882a593Smuzhiyun #include <linux/sched.h>
14*4882a593Smuzhiyun #include <linux/completion.h>
15*4882a593Smuzhiyun #include <linux/string.h>
16*4882a593Smuzhiyun #include <linux/mm.h>
17*4882a593Smuzhiyun #include <linux/delay.h>
18*4882a593Smuzhiyun #include <linux/init.h>
19*4882a593Smuzhiyun #include <linux/slab.h>
20*4882a593Smuzhiyun #include <linux/module.h>
21*4882a593Smuzhiyun #include <linux/device.h>
22*4882a593Smuzhiyun #include <linux/hyperv.h>
23*4882a593Smuzhiyun #include <linux/blkdev.h>
24*4882a593Smuzhiyun #include <scsi/scsi.h>
25*4882a593Smuzhiyun #include <scsi/scsi_cmnd.h>
26*4882a593Smuzhiyun #include <scsi/scsi_host.h>
27*4882a593Smuzhiyun #include <scsi/scsi_device.h>
28*4882a593Smuzhiyun #include <scsi/scsi_tcq.h>
29*4882a593Smuzhiyun #include <scsi/scsi_eh.h>
30*4882a593Smuzhiyun #include <scsi/scsi_devinfo.h>
31*4882a593Smuzhiyun #include <scsi/scsi_dbg.h>
32*4882a593Smuzhiyun #include <scsi/scsi_transport_fc.h>
33*4882a593Smuzhiyun #include <scsi/scsi_transport.h>
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun  * All wire protocol details (storage protocol between the guest and the host)
37*4882a593Smuzhiyun  * are consolidated here.
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  * Begin protocol definitions.
40*4882a593Smuzhiyun  */
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /*
43*4882a593Smuzhiyun  * Version history:
44*4882a593Smuzhiyun  * V1 Beta: 0.1
45*4882a593Smuzhiyun  * V1 RC < 2008/1/31: 1.0
46*4882a593Smuzhiyun  * V1 RC > 2008/1/31:  2.0
47*4882a593Smuzhiyun  * Win7: 4.2
48*4882a593Smuzhiyun  * Win8: 5.1
49*4882a593Smuzhiyun  * Win8.1: 6.0
50*4882a593Smuzhiyun  * Win10: 6.2
51*4882a593Smuzhiyun  */
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_)	((((MAJOR_) & 0xff) << 8) | \
54*4882a593Smuzhiyun 						(((MINOR_) & 0xff)))
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #define VMSTOR_PROTO_VERSION_WIN6	VMSTOR_PROTO_VERSION(2, 0)
57*4882a593Smuzhiyun #define VMSTOR_PROTO_VERSION_WIN7	VMSTOR_PROTO_VERSION(4, 2)
58*4882a593Smuzhiyun #define VMSTOR_PROTO_VERSION_WIN8	VMSTOR_PROTO_VERSION(5, 1)
59*4882a593Smuzhiyun #define VMSTOR_PROTO_VERSION_WIN8_1	VMSTOR_PROTO_VERSION(6, 0)
60*4882a593Smuzhiyun #define VMSTOR_PROTO_VERSION_WIN10	VMSTOR_PROTO_VERSION(6, 2)
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun /*  Packet structure describing virtual storage requests. */
63*4882a593Smuzhiyun enum vstor_packet_operation {
64*4882a593Smuzhiyun 	VSTOR_OPERATION_COMPLETE_IO		= 1,
65*4882a593Smuzhiyun 	VSTOR_OPERATION_REMOVE_DEVICE		= 2,
66*4882a593Smuzhiyun 	VSTOR_OPERATION_EXECUTE_SRB		= 3,
67*4882a593Smuzhiyun 	VSTOR_OPERATION_RESET_LUN		= 4,
68*4882a593Smuzhiyun 	VSTOR_OPERATION_RESET_ADAPTER		= 5,
69*4882a593Smuzhiyun 	VSTOR_OPERATION_RESET_BUS		= 6,
70*4882a593Smuzhiyun 	VSTOR_OPERATION_BEGIN_INITIALIZATION	= 7,
71*4882a593Smuzhiyun 	VSTOR_OPERATION_END_INITIALIZATION	= 8,
72*4882a593Smuzhiyun 	VSTOR_OPERATION_QUERY_PROTOCOL_VERSION	= 9,
73*4882a593Smuzhiyun 	VSTOR_OPERATION_QUERY_PROPERTIES	= 10,
74*4882a593Smuzhiyun 	VSTOR_OPERATION_ENUMERATE_BUS		= 11,
75*4882a593Smuzhiyun 	VSTOR_OPERATION_FCHBA_DATA              = 12,
76*4882a593Smuzhiyun 	VSTOR_OPERATION_CREATE_SUB_CHANNELS     = 13,
77*4882a593Smuzhiyun 	VSTOR_OPERATION_MAXIMUM                 = 13
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun /*
81*4882a593Smuzhiyun  * WWN packet for Fibre Channel HBA
82*4882a593Smuzhiyun  */
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun struct hv_fc_wwn_packet {
85*4882a593Smuzhiyun 	u8	primary_active;
86*4882a593Smuzhiyun 	u8	reserved1[3];
87*4882a593Smuzhiyun 	u8	primary_port_wwn[8];
88*4882a593Smuzhiyun 	u8	primary_node_wwn[8];
89*4882a593Smuzhiyun 	u8	secondary_port_wwn[8];
90*4882a593Smuzhiyun 	u8	secondary_node_wwn[8];
91*4882a593Smuzhiyun };
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun /*
96*4882a593Smuzhiyun  * SRB Flag Bits
97*4882a593Smuzhiyun  */
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun #define SRB_FLAGS_QUEUE_ACTION_ENABLE		0x00000002
100*4882a593Smuzhiyun #define SRB_FLAGS_DISABLE_DISCONNECT		0x00000004
101*4882a593Smuzhiyun #define SRB_FLAGS_DISABLE_SYNCH_TRANSFER	0x00000008
102*4882a593Smuzhiyun #define SRB_FLAGS_BYPASS_FROZEN_QUEUE		0x00000010
103*4882a593Smuzhiyun #define SRB_FLAGS_DISABLE_AUTOSENSE		0x00000020
104*4882a593Smuzhiyun #define SRB_FLAGS_DATA_IN			0x00000040
105*4882a593Smuzhiyun #define SRB_FLAGS_DATA_OUT			0x00000080
106*4882a593Smuzhiyun #define SRB_FLAGS_NO_DATA_TRANSFER		0x00000000
107*4882a593Smuzhiyun #define SRB_FLAGS_UNSPECIFIED_DIRECTION	(SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT)
108*4882a593Smuzhiyun #define SRB_FLAGS_NO_QUEUE_FREEZE		0x00000100
109*4882a593Smuzhiyun #define SRB_FLAGS_ADAPTER_CACHE_ENABLE		0x00000200
110*4882a593Smuzhiyun #define SRB_FLAGS_FREE_SENSE_BUFFER		0x00000400
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun /*
113*4882a593Smuzhiyun  * This flag indicates the request is part of the workflow for processing a D3.
114*4882a593Smuzhiyun  */
115*4882a593Smuzhiyun #define SRB_FLAGS_D3_PROCESSING			0x00000800
116*4882a593Smuzhiyun #define SRB_FLAGS_IS_ACTIVE			0x00010000
117*4882a593Smuzhiyun #define SRB_FLAGS_ALLOCATED_FROM_ZONE		0x00020000
118*4882a593Smuzhiyun #define SRB_FLAGS_SGLIST_FROM_POOL		0x00040000
119*4882a593Smuzhiyun #define SRB_FLAGS_BYPASS_LOCKED_QUEUE		0x00080000
120*4882a593Smuzhiyun #define SRB_FLAGS_NO_KEEP_AWAKE			0x00100000
121*4882a593Smuzhiyun #define SRB_FLAGS_PORT_DRIVER_ALLOCSENSE	0x00200000
122*4882a593Smuzhiyun #define SRB_FLAGS_PORT_DRIVER_SENSEHASPORT	0x00400000
123*4882a593Smuzhiyun #define SRB_FLAGS_DONT_START_NEXT_PACKET	0x00800000
124*4882a593Smuzhiyun #define SRB_FLAGS_PORT_DRIVER_RESERVED		0x0F000000
125*4882a593Smuzhiyun #define SRB_FLAGS_CLASS_DRIVER_RESERVED		0xF0000000
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun #define SP_UNTAGGED			((unsigned char) ~0)
128*4882a593Smuzhiyun #define SRB_SIMPLE_TAG_REQUEST		0x20
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun /*
131*4882a593Smuzhiyun  * Platform neutral description of a scsi request -
132*4882a593Smuzhiyun  * this remains the same across the write regardless of 32/64 bit
133*4882a593Smuzhiyun  * note: it's patterned off the SCSI_PASS_THROUGH structure
134*4882a593Smuzhiyun  */
135*4882a593Smuzhiyun #define STORVSC_MAX_CMD_LEN			0x10
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun #define POST_WIN7_STORVSC_SENSE_BUFFER_SIZE	0x14
138*4882a593Smuzhiyun #define PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE	0x12
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun #define STORVSC_SENSE_BUFFER_SIZE		0x14
141*4882a593Smuzhiyun #define STORVSC_MAX_BUF_LEN_WITH_PADDING	0x14
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun /*
144*4882a593Smuzhiyun  * Sense buffer size changed in win8; have a run-time
145*4882a593Smuzhiyun  * variable to track the size we should use.  This value will
146*4882a593Smuzhiyun  * likely change during protocol negotiation but it is valid
147*4882a593Smuzhiyun  * to start by assuming pre-Win8.
148*4882a593Smuzhiyun  */
149*4882a593Smuzhiyun static int sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun /*
152*4882a593Smuzhiyun  * The storage protocol version is determined during the
153*4882a593Smuzhiyun  * initial exchange with the host.  It will indicate which
154*4882a593Smuzhiyun  * storage functionality is available in the host.
155*4882a593Smuzhiyun */
156*4882a593Smuzhiyun static int vmstor_proto_version;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun #define STORVSC_LOGGING_NONE	0
159*4882a593Smuzhiyun #define STORVSC_LOGGING_ERROR	1
160*4882a593Smuzhiyun #define STORVSC_LOGGING_WARN	2
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun static int logging_level = STORVSC_LOGGING_ERROR;
163*4882a593Smuzhiyun module_param(logging_level, int, S_IRUGO|S_IWUSR);
164*4882a593Smuzhiyun MODULE_PARM_DESC(logging_level,
165*4882a593Smuzhiyun 	"Logging level, 0 - None, 1 - Error (default), 2 - Warning.");
166*4882a593Smuzhiyun 
do_logging(int level)167*4882a593Smuzhiyun static inline bool do_logging(int level)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun 	return logging_level >= level;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun #define storvsc_log(dev, level, fmt, ...)			\
173*4882a593Smuzhiyun do {								\
174*4882a593Smuzhiyun 	if (do_logging(level))					\
175*4882a593Smuzhiyun 		dev_warn(&(dev)->device, fmt, ##__VA_ARGS__);	\
176*4882a593Smuzhiyun } while (0)
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun struct vmscsi_win8_extension {
179*4882a593Smuzhiyun 	/*
180*4882a593Smuzhiyun 	 * The following were added in Windows 8
181*4882a593Smuzhiyun 	 */
182*4882a593Smuzhiyun 	u16 reserve;
183*4882a593Smuzhiyun 	u8  queue_tag;
184*4882a593Smuzhiyun 	u8  queue_action;
185*4882a593Smuzhiyun 	u32 srb_flags;
186*4882a593Smuzhiyun 	u32 time_out_value;
187*4882a593Smuzhiyun 	u32 queue_sort_ey;
188*4882a593Smuzhiyun } __packed;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun struct vmscsi_request {
191*4882a593Smuzhiyun 	u16 length;
192*4882a593Smuzhiyun 	u8 srb_status;
193*4882a593Smuzhiyun 	u8 scsi_status;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	u8  port_number;
196*4882a593Smuzhiyun 	u8  path_id;
197*4882a593Smuzhiyun 	u8  target_id;
198*4882a593Smuzhiyun 	u8  lun;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	u8  cdb_length;
201*4882a593Smuzhiyun 	u8  sense_info_length;
202*4882a593Smuzhiyun 	u8  data_in;
203*4882a593Smuzhiyun 	u8  reserved;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	u32 data_transfer_length;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	union {
208*4882a593Smuzhiyun 		u8 cdb[STORVSC_MAX_CMD_LEN];
209*4882a593Smuzhiyun 		u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
210*4882a593Smuzhiyun 		u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
211*4882a593Smuzhiyun 	};
212*4882a593Smuzhiyun 	/*
213*4882a593Smuzhiyun 	 * The following was added in win8.
214*4882a593Smuzhiyun 	 */
215*4882a593Smuzhiyun 	struct vmscsi_win8_extension win8_extension;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun } __attribute((packed));
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun /*
221*4882a593Smuzhiyun  * The size of the vmscsi_request has changed in win8. The
222*4882a593Smuzhiyun  * additional size is because of new elements added to the
223*4882a593Smuzhiyun  * structure. These elements are valid only when we are talking
224*4882a593Smuzhiyun  * to a win8 host.
225*4882a593Smuzhiyun  * Track the correction to size we need to apply. This value
226*4882a593Smuzhiyun  * will likely change during protocol negotiation but it is
227*4882a593Smuzhiyun  * valid to start by assuming pre-Win8.
228*4882a593Smuzhiyun  */
229*4882a593Smuzhiyun static int vmscsi_size_delta = sizeof(struct vmscsi_win8_extension);
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun /*
232*4882a593Smuzhiyun  * The list of storage protocols in order of preference.
233*4882a593Smuzhiyun  */
234*4882a593Smuzhiyun struct vmstor_protocol {
235*4882a593Smuzhiyun 	int protocol_version;
236*4882a593Smuzhiyun 	int sense_buffer_size;
237*4882a593Smuzhiyun 	int vmscsi_size_delta;
238*4882a593Smuzhiyun };
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun static const struct vmstor_protocol vmstor_protocols[] = {
242*4882a593Smuzhiyun 	{
243*4882a593Smuzhiyun 		VMSTOR_PROTO_VERSION_WIN10,
244*4882a593Smuzhiyun 		POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
245*4882a593Smuzhiyun 		0
246*4882a593Smuzhiyun 	},
247*4882a593Smuzhiyun 	{
248*4882a593Smuzhiyun 		VMSTOR_PROTO_VERSION_WIN8_1,
249*4882a593Smuzhiyun 		POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
250*4882a593Smuzhiyun 		0
251*4882a593Smuzhiyun 	},
252*4882a593Smuzhiyun 	{
253*4882a593Smuzhiyun 		VMSTOR_PROTO_VERSION_WIN8,
254*4882a593Smuzhiyun 		POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
255*4882a593Smuzhiyun 		0
256*4882a593Smuzhiyun 	},
257*4882a593Smuzhiyun 	{
258*4882a593Smuzhiyun 		VMSTOR_PROTO_VERSION_WIN7,
259*4882a593Smuzhiyun 		PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
260*4882a593Smuzhiyun 		sizeof(struct vmscsi_win8_extension),
261*4882a593Smuzhiyun 	},
262*4882a593Smuzhiyun 	{
263*4882a593Smuzhiyun 		VMSTOR_PROTO_VERSION_WIN6,
264*4882a593Smuzhiyun 		PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
265*4882a593Smuzhiyun 		sizeof(struct vmscsi_win8_extension),
266*4882a593Smuzhiyun 	}
267*4882a593Smuzhiyun };
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun /*
271*4882a593Smuzhiyun  * This structure is sent during the initialization phase to get the different
272*4882a593Smuzhiyun  * properties of the channel.
273*4882a593Smuzhiyun  */
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun #define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL		0x1
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun struct vmstorage_channel_properties {
278*4882a593Smuzhiyun 	u32 reserved;
279*4882a593Smuzhiyun 	u16 max_channel_cnt;
280*4882a593Smuzhiyun 	u16 reserved1;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	u32 flags;
283*4882a593Smuzhiyun 	u32   max_transfer_bytes;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	u64  reserved2;
286*4882a593Smuzhiyun } __packed;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun /*  This structure is sent during the storage protocol negotiations. */
289*4882a593Smuzhiyun struct vmstorage_protocol_version {
290*4882a593Smuzhiyun 	/* Major (MSW) and minor (LSW) version numbers. */
291*4882a593Smuzhiyun 	u16 major_minor;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	/*
294*4882a593Smuzhiyun 	 * Revision number is auto-incremented whenever this file is changed
295*4882a593Smuzhiyun 	 * (See FILL_VMSTOR_REVISION macro above).  Mismatch does not
296*4882a593Smuzhiyun 	 * definitely indicate incompatibility--but it does indicate mismatched
297*4882a593Smuzhiyun 	 * builds.
298*4882a593Smuzhiyun 	 * This is only used on the windows side. Just set it to 0.
299*4882a593Smuzhiyun 	 */
300*4882a593Smuzhiyun 	u16 revision;
301*4882a593Smuzhiyun } __packed;
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun /* Channel Property Flags */
304*4882a593Smuzhiyun #define STORAGE_CHANNEL_REMOVABLE_FLAG		0x1
305*4882a593Smuzhiyun #define STORAGE_CHANNEL_EMULATED_IDE_FLAG	0x2
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun struct vstor_packet {
308*4882a593Smuzhiyun 	/* Requested operation type */
309*4882a593Smuzhiyun 	enum vstor_packet_operation operation;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	/*  Flags - see below for values */
312*4882a593Smuzhiyun 	u32 flags;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	/* Status of the request returned from the server side. */
315*4882a593Smuzhiyun 	u32 status;
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	/* Data payload area */
318*4882a593Smuzhiyun 	union {
319*4882a593Smuzhiyun 		/*
320*4882a593Smuzhiyun 		 * Structure used to forward SCSI commands from the
321*4882a593Smuzhiyun 		 * client to the server.
322*4882a593Smuzhiyun 		 */
323*4882a593Smuzhiyun 		struct vmscsi_request vm_srb;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 		/* Structure used to query channel properties. */
326*4882a593Smuzhiyun 		struct vmstorage_channel_properties storage_channel_properties;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 		/* Used during version negotiations. */
329*4882a593Smuzhiyun 		struct vmstorage_protocol_version version;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 		/* Fibre channel address packet */
332*4882a593Smuzhiyun 		struct hv_fc_wwn_packet wwn_packet;
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 		/* Number of sub-channels to create */
335*4882a593Smuzhiyun 		u16 sub_channel_count;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 		/* This will be the maximum of the union members */
338*4882a593Smuzhiyun 		u8  buffer[0x34];
339*4882a593Smuzhiyun 	};
340*4882a593Smuzhiyun } __packed;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun /*
343*4882a593Smuzhiyun  * Packet Flags:
344*4882a593Smuzhiyun  *
345*4882a593Smuzhiyun  * This flag indicates that the server should send back a completion for this
346*4882a593Smuzhiyun  * packet.
347*4882a593Smuzhiyun  */
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun #define REQUEST_COMPLETION_FLAG	0x1
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun /* Matches Windows-end */
352*4882a593Smuzhiyun enum storvsc_request_type {
353*4882a593Smuzhiyun 	WRITE_TYPE = 0,
354*4882a593Smuzhiyun 	READ_TYPE,
355*4882a593Smuzhiyun 	UNKNOWN_TYPE,
356*4882a593Smuzhiyun };
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun /*
359*4882a593Smuzhiyun  * SRB status codes and masks. In the 8-bit field, the two high order bits
360*4882a593Smuzhiyun  * are flags, while the remaining 6 bits are an integer status code.  The
361*4882a593Smuzhiyun  * definitions here include only the subset of the integer status codes that
362*4882a593Smuzhiyun  * are tested for in this driver.
363*4882a593Smuzhiyun  */
364*4882a593Smuzhiyun #define SRB_STATUS_AUTOSENSE_VALID	0x80
365*4882a593Smuzhiyun #define SRB_STATUS_QUEUE_FROZEN		0x40
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun /* SRB status integer codes */
368*4882a593Smuzhiyun #define SRB_STATUS_SUCCESS		0x01
369*4882a593Smuzhiyun #define SRB_STATUS_ABORTED		0x02
370*4882a593Smuzhiyun #define SRB_STATUS_ERROR		0x04
371*4882a593Smuzhiyun #define SRB_STATUS_INVALID_REQUEST	0x06
372*4882a593Smuzhiyun #define SRB_STATUS_DATA_OVERRUN		0x12
373*4882a593Smuzhiyun #define SRB_STATUS_INVALID_LUN		0x20
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun #define SRB_STATUS(status) \
376*4882a593Smuzhiyun 	(status & ~(SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_QUEUE_FROZEN))
377*4882a593Smuzhiyun /*
378*4882a593Smuzhiyun  * This is the end of Protocol specific defines.
379*4882a593Smuzhiyun  */
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun static int storvsc_ringbuffer_size = (128 * 1024);
382*4882a593Smuzhiyun static u32 max_outstanding_req_per_channel;
383*4882a593Smuzhiyun static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth);
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun static int storvsc_vcpus_per_sub_channel = 4;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun module_param(storvsc_ringbuffer_size, int, S_IRUGO);
388*4882a593Smuzhiyun MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun module_param(storvsc_vcpus_per_sub_channel, int, S_IRUGO);
391*4882a593Smuzhiyun MODULE_PARM_DESC(storvsc_vcpus_per_sub_channel, "Ratio of VCPUs to subchannels");
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun static int ring_avail_percent_lowater = 10;
394*4882a593Smuzhiyun module_param(ring_avail_percent_lowater, int, S_IRUGO);
395*4882a593Smuzhiyun MODULE_PARM_DESC(ring_avail_percent_lowater,
396*4882a593Smuzhiyun 		"Select a channel if available ring size > this in percent");
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun /*
399*4882a593Smuzhiyun  * Timeout in seconds for all devices managed by this driver.
400*4882a593Smuzhiyun  */
401*4882a593Smuzhiyun static int storvsc_timeout = 180;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
404*4882a593Smuzhiyun static struct scsi_transport_template *fc_transport_template;
405*4882a593Smuzhiyun #endif
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun static void storvsc_on_channel_callback(void *context);
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun #define STORVSC_MAX_LUNS_PER_TARGET			255
410*4882a593Smuzhiyun #define STORVSC_MAX_TARGETS				2
411*4882a593Smuzhiyun #define STORVSC_MAX_CHANNELS				8
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun #define STORVSC_FC_MAX_LUNS_PER_TARGET			255
414*4882a593Smuzhiyun #define STORVSC_FC_MAX_TARGETS				128
415*4882a593Smuzhiyun #define STORVSC_FC_MAX_CHANNELS				8
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun #define STORVSC_IDE_MAX_LUNS_PER_TARGET			64
418*4882a593Smuzhiyun #define STORVSC_IDE_MAX_TARGETS				1
419*4882a593Smuzhiyun #define STORVSC_IDE_MAX_CHANNELS			1
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun struct storvsc_cmd_request {
422*4882a593Smuzhiyun 	struct scsi_cmnd *cmd;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	struct hv_device *device;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	/* Synchronize the request/response if needed */
427*4882a593Smuzhiyun 	struct completion wait_event;
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	struct vmbus_channel_packet_multipage_buffer mpb;
430*4882a593Smuzhiyun 	struct vmbus_packet_mpb_array *payload;
431*4882a593Smuzhiyun 	u32 payload_sz;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	struct vstor_packet vstor_packet;
434*4882a593Smuzhiyun };
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun /* A storvsc device is a device object that contains a vmbus channel */
438*4882a593Smuzhiyun struct storvsc_device {
439*4882a593Smuzhiyun 	struct hv_device *device;
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	bool	 destroy;
442*4882a593Smuzhiyun 	bool	 drain_notify;
443*4882a593Smuzhiyun 	atomic_t num_outstanding_req;
444*4882a593Smuzhiyun 	struct Scsi_Host *host;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	wait_queue_head_t waiting_to_drain;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	/*
449*4882a593Smuzhiyun 	 * Each unique Port/Path/Target represents 1 channel ie scsi
450*4882a593Smuzhiyun 	 * controller. In reality, the pathid, targetid is always 0
451*4882a593Smuzhiyun 	 * and the port is set by us
452*4882a593Smuzhiyun 	 */
453*4882a593Smuzhiyun 	unsigned int port_number;
454*4882a593Smuzhiyun 	unsigned char path_id;
455*4882a593Smuzhiyun 	unsigned char target_id;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	/*
458*4882a593Smuzhiyun 	 * Max I/O, the device can support.
459*4882a593Smuzhiyun 	 */
460*4882a593Smuzhiyun 	u32   max_transfer_bytes;
461*4882a593Smuzhiyun 	/*
462*4882a593Smuzhiyun 	 * Number of sub-channels we will open.
463*4882a593Smuzhiyun 	 */
464*4882a593Smuzhiyun 	u16 num_sc;
465*4882a593Smuzhiyun 	struct vmbus_channel **stor_chns;
466*4882a593Smuzhiyun 	/*
467*4882a593Smuzhiyun 	 * Mask of CPUs bound to subchannels.
468*4882a593Smuzhiyun 	 */
469*4882a593Smuzhiyun 	struct cpumask alloced_cpus;
470*4882a593Smuzhiyun 	/*
471*4882a593Smuzhiyun 	 * Serializes modifications of stor_chns[] from storvsc_do_io()
472*4882a593Smuzhiyun 	 * and storvsc_change_target_cpu().
473*4882a593Smuzhiyun 	 */
474*4882a593Smuzhiyun 	spinlock_t lock;
475*4882a593Smuzhiyun 	/* Used for vsc/vsp channel reset process */
476*4882a593Smuzhiyun 	struct storvsc_cmd_request init_request;
477*4882a593Smuzhiyun 	struct storvsc_cmd_request reset_request;
478*4882a593Smuzhiyun 	/*
479*4882a593Smuzhiyun 	 * Currently active port and node names for FC devices.
480*4882a593Smuzhiyun 	 */
481*4882a593Smuzhiyun 	u64 node_name;
482*4882a593Smuzhiyun 	u64 port_name;
483*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
484*4882a593Smuzhiyun 	struct fc_rport *rport;
485*4882a593Smuzhiyun #endif
486*4882a593Smuzhiyun };
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun struct hv_host_device {
489*4882a593Smuzhiyun 	struct hv_device *dev;
490*4882a593Smuzhiyun 	unsigned int port;
491*4882a593Smuzhiyun 	unsigned char path;
492*4882a593Smuzhiyun 	unsigned char target;
493*4882a593Smuzhiyun 	struct workqueue_struct *handle_error_wq;
494*4882a593Smuzhiyun 	struct work_struct host_scan_work;
495*4882a593Smuzhiyun 	struct Scsi_Host *host;
496*4882a593Smuzhiyun };
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun struct storvsc_scan_work {
499*4882a593Smuzhiyun 	struct work_struct work;
500*4882a593Smuzhiyun 	struct Scsi_Host *host;
501*4882a593Smuzhiyun 	u8 lun;
502*4882a593Smuzhiyun 	u8 tgt_id;
503*4882a593Smuzhiyun };
504*4882a593Smuzhiyun 
storvsc_device_scan(struct work_struct * work)505*4882a593Smuzhiyun static void storvsc_device_scan(struct work_struct *work)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun 	struct storvsc_scan_work *wrk;
508*4882a593Smuzhiyun 	struct scsi_device *sdev;
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	wrk = container_of(work, struct storvsc_scan_work, work);
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 	sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
513*4882a593Smuzhiyun 	if (!sdev)
514*4882a593Smuzhiyun 		goto done;
515*4882a593Smuzhiyun 	scsi_rescan_device(&sdev->sdev_gendev);
516*4882a593Smuzhiyun 	scsi_device_put(sdev);
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun done:
519*4882a593Smuzhiyun 	kfree(wrk);
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun 
storvsc_host_scan(struct work_struct * work)522*4882a593Smuzhiyun static void storvsc_host_scan(struct work_struct *work)
523*4882a593Smuzhiyun {
524*4882a593Smuzhiyun 	struct Scsi_Host *host;
525*4882a593Smuzhiyun 	struct scsi_device *sdev;
526*4882a593Smuzhiyun 	struct hv_host_device *host_device =
527*4882a593Smuzhiyun 		container_of(work, struct hv_host_device, host_scan_work);
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 	host = host_device->host;
530*4882a593Smuzhiyun 	/*
531*4882a593Smuzhiyun 	 * Before scanning the host, first check to see if any of the
532*4882a593Smuzhiyun 	 * currrently known devices have been hot removed. We issue a
533*4882a593Smuzhiyun 	 * "unit ready" command against all currently known devices.
534*4882a593Smuzhiyun 	 * This I/O will result in an error for devices that have been
535*4882a593Smuzhiyun 	 * removed. As part of handling the I/O error, we remove the device.
536*4882a593Smuzhiyun 	 *
537*4882a593Smuzhiyun 	 * When a LUN is added or removed, the host sends us a signal to
538*4882a593Smuzhiyun 	 * scan the host. Thus we are forced to discover the LUNs that
539*4882a593Smuzhiyun 	 * may have been removed this way.
540*4882a593Smuzhiyun 	 */
541*4882a593Smuzhiyun 	mutex_lock(&host->scan_mutex);
542*4882a593Smuzhiyun 	shost_for_each_device(sdev, host)
543*4882a593Smuzhiyun 		scsi_test_unit_ready(sdev, 1, 1, NULL);
544*4882a593Smuzhiyun 	mutex_unlock(&host->scan_mutex);
545*4882a593Smuzhiyun 	/*
546*4882a593Smuzhiyun 	 * Now scan the host to discover LUNs that may have been added.
547*4882a593Smuzhiyun 	 */
548*4882a593Smuzhiyun 	scsi_scan_host(host);
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun 
storvsc_remove_lun(struct work_struct * work)551*4882a593Smuzhiyun static void storvsc_remove_lun(struct work_struct *work)
552*4882a593Smuzhiyun {
553*4882a593Smuzhiyun 	struct storvsc_scan_work *wrk;
554*4882a593Smuzhiyun 	struct scsi_device *sdev;
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 	wrk = container_of(work, struct storvsc_scan_work, work);
557*4882a593Smuzhiyun 	if (!scsi_host_get(wrk->host))
558*4882a593Smuzhiyun 		goto done;
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	if (sdev) {
563*4882a593Smuzhiyun 		scsi_remove_device(sdev);
564*4882a593Smuzhiyun 		scsi_device_put(sdev);
565*4882a593Smuzhiyun 	}
566*4882a593Smuzhiyun 	scsi_host_put(wrk->host);
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun done:
569*4882a593Smuzhiyun 	kfree(wrk);
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun /*
574*4882a593Smuzhiyun  * We can get incoming messages from the host that are not in response to
575*4882a593Smuzhiyun  * messages that we have sent out. An example of this would be messages
576*4882a593Smuzhiyun  * received by the guest to notify dynamic addition/removal of LUNs. To
577*4882a593Smuzhiyun  * deal with potential race conditions where the driver may be in the
578*4882a593Smuzhiyun  * midst of being unloaded when we might receive an unsolicited message
579*4882a593Smuzhiyun  * from the host, we have implemented a mechanism to gurantee sequential
580*4882a593Smuzhiyun  * consistency:
581*4882a593Smuzhiyun  *
582*4882a593Smuzhiyun  * 1) Once the device is marked as being destroyed, we will fail all
583*4882a593Smuzhiyun  *    outgoing messages.
584*4882a593Smuzhiyun  * 2) We permit incoming messages when the device is being destroyed,
585*4882a593Smuzhiyun  *    only to properly account for messages already sent out.
586*4882a593Smuzhiyun  */
587*4882a593Smuzhiyun 
get_out_stor_device(struct hv_device * device)588*4882a593Smuzhiyun static inline struct storvsc_device *get_out_stor_device(
589*4882a593Smuzhiyun 					struct hv_device *device)
590*4882a593Smuzhiyun {
591*4882a593Smuzhiyun 	struct storvsc_device *stor_device;
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 	stor_device = hv_get_drvdata(device);
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	if (stor_device && stor_device->destroy)
596*4882a593Smuzhiyun 		stor_device = NULL;
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 	return stor_device;
599*4882a593Smuzhiyun }
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 
storvsc_wait_to_drain(struct storvsc_device * dev)602*4882a593Smuzhiyun static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
603*4882a593Smuzhiyun {
604*4882a593Smuzhiyun 	dev->drain_notify = true;
605*4882a593Smuzhiyun 	wait_event(dev->waiting_to_drain,
606*4882a593Smuzhiyun 		   atomic_read(&dev->num_outstanding_req) == 0);
607*4882a593Smuzhiyun 	dev->drain_notify = false;
608*4882a593Smuzhiyun }
609*4882a593Smuzhiyun 
get_in_stor_device(struct hv_device * device)610*4882a593Smuzhiyun static inline struct storvsc_device *get_in_stor_device(
611*4882a593Smuzhiyun 					struct hv_device *device)
612*4882a593Smuzhiyun {
613*4882a593Smuzhiyun 	struct storvsc_device *stor_device;
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	stor_device = hv_get_drvdata(device);
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 	if (!stor_device)
618*4882a593Smuzhiyun 		goto get_in_err;
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	/*
621*4882a593Smuzhiyun 	 * If the device is being destroyed; allow incoming
622*4882a593Smuzhiyun 	 * traffic only to cleanup outstanding requests.
623*4882a593Smuzhiyun 	 */
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun 	if (stor_device->destroy  &&
626*4882a593Smuzhiyun 		(atomic_read(&stor_device->num_outstanding_req) == 0))
627*4882a593Smuzhiyun 		stor_device = NULL;
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun get_in_err:
630*4882a593Smuzhiyun 	return stor_device;
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun }
633*4882a593Smuzhiyun 
storvsc_change_target_cpu(struct vmbus_channel * channel,u32 old,u32 new)634*4882a593Smuzhiyun static void storvsc_change_target_cpu(struct vmbus_channel *channel, u32 old,
635*4882a593Smuzhiyun 				      u32 new)
636*4882a593Smuzhiyun {
637*4882a593Smuzhiyun 	struct storvsc_device *stor_device;
638*4882a593Smuzhiyun 	struct vmbus_channel *cur_chn;
639*4882a593Smuzhiyun 	bool old_is_alloced = false;
640*4882a593Smuzhiyun 	struct hv_device *device;
641*4882a593Smuzhiyun 	unsigned long flags;
642*4882a593Smuzhiyun 	int cpu;
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	device = channel->primary_channel ?
645*4882a593Smuzhiyun 			channel->primary_channel->device_obj
646*4882a593Smuzhiyun 				: channel->device_obj;
647*4882a593Smuzhiyun 	stor_device = get_out_stor_device(device);
648*4882a593Smuzhiyun 	if (!stor_device)
649*4882a593Smuzhiyun 		return;
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	/* See storvsc_do_io() -> get_og_chn(). */
652*4882a593Smuzhiyun 	spin_lock_irqsave(&stor_device->lock, flags);
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	/*
655*4882a593Smuzhiyun 	 * Determines if the storvsc device has other channels assigned to
656*4882a593Smuzhiyun 	 * the "old" CPU to update the alloced_cpus mask and the stor_chns
657*4882a593Smuzhiyun 	 * array.
658*4882a593Smuzhiyun 	 */
659*4882a593Smuzhiyun 	if (device->channel != channel && device->channel->target_cpu == old) {
660*4882a593Smuzhiyun 		cur_chn = device->channel;
661*4882a593Smuzhiyun 		old_is_alloced = true;
662*4882a593Smuzhiyun 		goto old_is_alloced;
663*4882a593Smuzhiyun 	}
664*4882a593Smuzhiyun 	list_for_each_entry(cur_chn, &device->channel->sc_list, sc_list) {
665*4882a593Smuzhiyun 		if (cur_chn == channel)
666*4882a593Smuzhiyun 			continue;
667*4882a593Smuzhiyun 		if (cur_chn->target_cpu == old) {
668*4882a593Smuzhiyun 			old_is_alloced = true;
669*4882a593Smuzhiyun 			goto old_is_alloced;
670*4882a593Smuzhiyun 		}
671*4882a593Smuzhiyun 	}
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun old_is_alloced:
674*4882a593Smuzhiyun 	if (old_is_alloced)
675*4882a593Smuzhiyun 		WRITE_ONCE(stor_device->stor_chns[old], cur_chn);
676*4882a593Smuzhiyun 	else
677*4882a593Smuzhiyun 		cpumask_clear_cpu(old, &stor_device->alloced_cpus);
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 	/* "Flush" the stor_chns array. */
680*4882a593Smuzhiyun 	for_each_possible_cpu(cpu) {
681*4882a593Smuzhiyun 		if (stor_device->stor_chns[cpu] && !cpumask_test_cpu(
682*4882a593Smuzhiyun 					cpu, &stor_device->alloced_cpus))
683*4882a593Smuzhiyun 			WRITE_ONCE(stor_device->stor_chns[cpu], NULL);
684*4882a593Smuzhiyun 	}
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 	WRITE_ONCE(stor_device->stor_chns[new], channel);
687*4882a593Smuzhiyun 	cpumask_set_cpu(new, &stor_device->alloced_cpus);
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	spin_unlock_irqrestore(&stor_device->lock, flags);
690*4882a593Smuzhiyun }
691*4882a593Smuzhiyun 
handle_sc_creation(struct vmbus_channel * new_sc)692*4882a593Smuzhiyun static void handle_sc_creation(struct vmbus_channel *new_sc)
693*4882a593Smuzhiyun {
694*4882a593Smuzhiyun 	struct hv_device *device = new_sc->primary_channel->device_obj;
695*4882a593Smuzhiyun 	struct device *dev = &device->device;
696*4882a593Smuzhiyun 	struct storvsc_device *stor_device;
697*4882a593Smuzhiyun 	struct vmstorage_channel_properties props;
698*4882a593Smuzhiyun 	int ret;
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	stor_device = get_out_stor_device(device);
701*4882a593Smuzhiyun 	if (!stor_device)
702*4882a593Smuzhiyun 		return;
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 	memset(&props, 0, sizeof(struct vmstorage_channel_properties));
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	ret = vmbus_open(new_sc,
707*4882a593Smuzhiyun 			 storvsc_ringbuffer_size,
708*4882a593Smuzhiyun 			 storvsc_ringbuffer_size,
709*4882a593Smuzhiyun 			 (void *)&props,
710*4882a593Smuzhiyun 			 sizeof(struct vmstorage_channel_properties),
711*4882a593Smuzhiyun 			 storvsc_on_channel_callback, new_sc);
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	/* In case vmbus_open() fails, we don't use the sub-channel. */
714*4882a593Smuzhiyun 	if (ret != 0) {
715*4882a593Smuzhiyun 		dev_err(dev, "Failed to open sub-channel: err=%d\n", ret);
716*4882a593Smuzhiyun 		return;
717*4882a593Smuzhiyun 	}
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 	new_sc->change_target_cpu_callback = storvsc_change_target_cpu;
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun 	/* Add the sub-channel to the array of available channels. */
722*4882a593Smuzhiyun 	stor_device->stor_chns[new_sc->target_cpu] = new_sc;
723*4882a593Smuzhiyun 	cpumask_set_cpu(new_sc->target_cpu, &stor_device->alloced_cpus);
724*4882a593Smuzhiyun }
725*4882a593Smuzhiyun 
handle_multichannel_storage(struct hv_device * device,int max_chns)726*4882a593Smuzhiyun static void  handle_multichannel_storage(struct hv_device *device, int max_chns)
727*4882a593Smuzhiyun {
728*4882a593Smuzhiyun 	struct device *dev = &device->device;
729*4882a593Smuzhiyun 	struct storvsc_device *stor_device;
730*4882a593Smuzhiyun 	int num_sc;
731*4882a593Smuzhiyun 	struct storvsc_cmd_request *request;
732*4882a593Smuzhiyun 	struct vstor_packet *vstor_packet;
733*4882a593Smuzhiyun 	int ret, t;
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	/*
736*4882a593Smuzhiyun 	 * If the number of CPUs is artificially restricted, such as
737*4882a593Smuzhiyun 	 * with maxcpus=1 on the kernel boot line, Hyper-V could offer
738*4882a593Smuzhiyun 	 * sub-channels >= the number of CPUs. These sub-channels
739*4882a593Smuzhiyun 	 * should not be created. The primary channel is already created
740*4882a593Smuzhiyun 	 * and assigned to one CPU, so check against # CPUs - 1.
741*4882a593Smuzhiyun 	 */
742*4882a593Smuzhiyun 	num_sc = min((int)(num_online_cpus() - 1), max_chns);
743*4882a593Smuzhiyun 	if (!num_sc)
744*4882a593Smuzhiyun 		return;
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	stor_device = get_out_stor_device(device);
747*4882a593Smuzhiyun 	if (!stor_device)
748*4882a593Smuzhiyun 		return;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	stor_device->num_sc = num_sc;
751*4882a593Smuzhiyun 	request = &stor_device->init_request;
752*4882a593Smuzhiyun 	vstor_packet = &request->vstor_packet;
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	/*
755*4882a593Smuzhiyun 	 * Establish a handler for dealing with subchannels.
756*4882a593Smuzhiyun 	 */
757*4882a593Smuzhiyun 	vmbus_set_sc_create_callback(device->channel, handle_sc_creation);
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun 	/*
760*4882a593Smuzhiyun 	 * Request the host to create sub-channels.
761*4882a593Smuzhiyun 	 */
762*4882a593Smuzhiyun 	memset(request, 0, sizeof(struct storvsc_cmd_request));
763*4882a593Smuzhiyun 	init_completion(&request->wait_event);
764*4882a593Smuzhiyun 	vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
765*4882a593Smuzhiyun 	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
766*4882a593Smuzhiyun 	vstor_packet->sub_channel_count = num_sc;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	ret = vmbus_sendpacket(device->channel, vstor_packet,
769*4882a593Smuzhiyun 			       (sizeof(struct vstor_packet) -
770*4882a593Smuzhiyun 			       vmscsi_size_delta),
771*4882a593Smuzhiyun 			       (unsigned long)request,
772*4882a593Smuzhiyun 			       VM_PKT_DATA_INBAND,
773*4882a593Smuzhiyun 			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun 	if (ret != 0) {
776*4882a593Smuzhiyun 		dev_err(dev, "Failed to create sub-channel: err=%d\n", ret);
777*4882a593Smuzhiyun 		return;
778*4882a593Smuzhiyun 	}
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 	t = wait_for_completion_timeout(&request->wait_event, 10*HZ);
781*4882a593Smuzhiyun 	if (t == 0) {
782*4882a593Smuzhiyun 		dev_err(dev, "Failed to create sub-channel: timed out\n");
783*4882a593Smuzhiyun 		return;
784*4882a593Smuzhiyun 	}
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
787*4882a593Smuzhiyun 	    vstor_packet->status != 0) {
788*4882a593Smuzhiyun 		dev_err(dev, "Failed to create sub-channel: op=%d, sts=%d\n",
789*4882a593Smuzhiyun 			vstor_packet->operation, vstor_packet->status);
790*4882a593Smuzhiyun 		return;
791*4882a593Smuzhiyun 	}
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 	/*
794*4882a593Smuzhiyun 	 * We need to do nothing here, because vmbus_process_offer()
795*4882a593Smuzhiyun 	 * invokes channel->sc_creation_callback, which will open and use
796*4882a593Smuzhiyun 	 * the sub-channel(s).
797*4882a593Smuzhiyun 	 */
798*4882a593Smuzhiyun }
799*4882a593Smuzhiyun 
cache_wwn(struct storvsc_device * stor_device,struct vstor_packet * vstor_packet)800*4882a593Smuzhiyun static void cache_wwn(struct storvsc_device *stor_device,
801*4882a593Smuzhiyun 		      struct vstor_packet *vstor_packet)
802*4882a593Smuzhiyun {
803*4882a593Smuzhiyun 	/*
804*4882a593Smuzhiyun 	 * Cache the currently active port and node ww names.
805*4882a593Smuzhiyun 	 */
806*4882a593Smuzhiyun 	if (vstor_packet->wwn_packet.primary_active) {
807*4882a593Smuzhiyun 		stor_device->node_name =
808*4882a593Smuzhiyun 			wwn_to_u64(vstor_packet->wwn_packet.primary_node_wwn);
809*4882a593Smuzhiyun 		stor_device->port_name =
810*4882a593Smuzhiyun 			wwn_to_u64(vstor_packet->wwn_packet.primary_port_wwn);
811*4882a593Smuzhiyun 	} else {
812*4882a593Smuzhiyun 		stor_device->node_name =
813*4882a593Smuzhiyun 			wwn_to_u64(vstor_packet->wwn_packet.secondary_node_wwn);
814*4882a593Smuzhiyun 		stor_device->port_name =
815*4882a593Smuzhiyun 			wwn_to_u64(vstor_packet->wwn_packet.secondary_port_wwn);
816*4882a593Smuzhiyun 	}
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 
storvsc_execute_vstor_op(struct hv_device * device,struct storvsc_cmd_request * request,bool status_check)820*4882a593Smuzhiyun static int storvsc_execute_vstor_op(struct hv_device *device,
821*4882a593Smuzhiyun 				    struct storvsc_cmd_request *request,
822*4882a593Smuzhiyun 				    bool status_check)
823*4882a593Smuzhiyun {
824*4882a593Smuzhiyun 	struct vstor_packet *vstor_packet;
825*4882a593Smuzhiyun 	int ret, t;
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 	vstor_packet = &request->vstor_packet;
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 	init_completion(&request->wait_event);
830*4882a593Smuzhiyun 	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	ret = vmbus_sendpacket(device->channel, vstor_packet,
833*4882a593Smuzhiyun 			       (sizeof(struct vstor_packet) -
834*4882a593Smuzhiyun 			       vmscsi_size_delta),
835*4882a593Smuzhiyun 			       (unsigned long)request,
836*4882a593Smuzhiyun 			       VM_PKT_DATA_INBAND,
837*4882a593Smuzhiyun 			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
838*4882a593Smuzhiyun 	if (ret != 0)
839*4882a593Smuzhiyun 		return ret;
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
842*4882a593Smuzhiyun 	if (t == 0)
843*4882a593Smuzhiyun 		return -ETIMEDOUT;
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun 	if (!status_check)
846*4882a593Smuzhiyun 		return ret;
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 	if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
849*4882a593Smuzhiyun 	    vstor_packet->status != 0)
850*4882a593Smuzhiyun 		return -EINVAL;
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun 	return ret;
853*4882a593Smuzhiyun }
854*4882a593Smuzhiyun 
storvsc_channel_init(struct hv_device * device,bool is_fc)855*4882a593Smuzhiyun static int storvsc_channel_init(struct hv_device *device, bool is_fc)
856*4882a593Smuzhiyun {
857*4882a593Smuzhiyun 	struct storvsc_device *stor_device;
858*4882a593Smuzhiyun 	struct storvsc_cmd_request *request;
859*4882a593Smuzhiyun 	struct vstor_packet *vstor_packet;
860*4882a593Smuzhiyun 	int ret, i;
861*4882a593Smuzhiyun 	int max_chns;
862*4882a593Smuzhiyun 	bool process_sub_channels = false;
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 	stor_device = get_out_stor_device(device);
865*4882a593Smuzhiyun 	if (!stor_device)
866*4882a593Smuzhiyun 		return -ENODEV;
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun 	request = &stor_device->init_request;
869*4882a593Smuzhiyun 	vstor_packet = &request->vstor_packet;
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	/*
872*4882a593Smuzhiyun 	 * Now, initiate the vsc/vsp initialization protocol on the open
873*4882a593Smuzhiyun 	 * channel
874*4882a593Smuzhiyun 	 */
875*4882a593Smuzhiyun 	memset(request, 0, sizeof(struct storvsc_cmd_request));
876*4882a593Smuzhiyun 	vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
877*4882a593Smuzhiyun 	ret = storvsc_execute_vstor_op(device, request, true);
878*4882a593Smuzhiyun 	if (ret)
879*4882a593Smuzhiyun 		return ret;
880*4882a593Smuzhiyun 	/*
881*4882a593Smuzhiyun 	 * Query host supported protocol version.
882*4882a593Smuzhiyun 	 */
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(vmstor_protocols); i++) {
885*4882a593Smuzhiyun 		/* reuse the packet for version range supported */
886*4882a593Smuzhiyun 		memset(vstor_packet, 0, sizeof(struct vstor_packet));
887*4882a593Smuzhiyun 		vstor_packet->operation =
888*4882a593Smuzhiyun 			VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 		vstor_packet->version.major_minor =
891*4882a593Smuzhiyun 			vmstor_protocols[i].protocol_version;
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 		/*
894*4882a593Smuzhiyun 		 * The revision number is only used in Windows; set it to 0.
895*4882a593Smuzhiyun 		 */
896*4882a593Smuzhiyun 		vstor_packet->version.revision = 0;
897*4882a593Smuzhiyun 		ret = storvsc_execute_vstor_op(device, request, false);
898*4882a593Smuzhiyun 		if (ret != 0)
899*4882a593Smuzhiyun 			return ret;
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 		if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO)
902*4882a593Smuzhiyun 			return -EINVAL;
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun 		if (vstor_packet->status == 0) {
905*4882a593Smuzhiyun 			vmstor_proto_version =
906*4882a593Smuzhiyun 				vmstor_protocols[i].protocol_version;
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 			sense_buffer_size =
909*4882a593Smuzhiyun 				vmstor_protocols[i].sense_buffer_size;
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 			vmscsi_size_delta =
912*4882a593Smuzhiyun 				vmstor_protocols[i].vmscsi_size_delta;
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 			break;
915*4882a593Smuzhiyun 		}
916*4882a593Smuzhiyun 	}
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 	if (vstor_packet->status != 0)
919*4882a593Smuzhiyun 		return -EINVAL;
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	memset(vstor_packet, 0, sizeof(struct vstor_packet));
923*4882a593Smuzhiyun 	vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
924*4882a593Smuzhiyun 	ret = storvsc_execute_vstor_op(device, request, true);
925*4882a593Smuzhiyun 	if (ret != 0)
926*4882a593Smuzhiyun 		return ret;
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 	/*
929*4882a593Smuzhiyun 	 * Check to see if multi-channel support is there.
930*4882a593Smuzhiyun 	 * Hosts that implement protocol version of 5.1 and above
931*4882a593Smuzhiyun 	 * support multi-channel.
932*4882a593Smuzhiyun 	 */
933*4882a593Smuzhiyun 	max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	/*
936*4882a593Smuzhiyun 	 * Allocate state to manage the sub-channels.
937*4882a593Smuzhiyun 	 * We allocate an array based on the numbers of possible CPUs
938*4882a593Smuzhiyun 	 * (Hyper-V does not support cpu online/offline).
939*4882a593Smuzhiyun 	 * This Array will be sparseley populated with unique
940*4882a593Smuzhiyun 	 * channels - primary + sub-channels.
941*4882a593Smuzhiyun 	 * We will however populate all the slots to evenly distribute
942*4882a593Smuzhiyun 	 * the load.
943*4882a593Smuzhiyun 	 */
944*4882a593Smuzhiyun 	stor_device->stor_chns = kcalloc(num_possible_cpus(), sizeof(void *),
945*4882a593Smuzhiyun 					 GFP_KERNEL);
946*4882a593Smuzhiyun 	if (stor_device->stor_chns == NULL)
947*4882a593Smuzhiyun 		return -ENOMEM;
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 	device->channel->change_target_cpu_callback = storvsc_change_target_cpu;
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 	stor_device->stor_chns[device->channel->target_cpu] = device->channel;
952*4882a593Smuzhiyun 	cpumask_set_cpu(device->channel->target_cpu,
953*4882a593Smuzhiyun 			&stor_device->alloced_cpus);
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun 	if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN8) {
956*4882a593Smuzhiyun 		if (vstor_packet->storage_channel_properties.flags &
957*4882a593Smuzhiyun 		    STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
958*4882a593Smuzhiyun 			process_sub_channels = true;
959*4882a593Smuzhiyun 	}
960*4882a593Smuzhiyun 	stor_device->max_transfer_bytes =
961*4882a593Smuzhiyun 		vstor_packet->storage_channel_properties.max_transfer_bytes;
962*4882a593Smuzhiyun 
963*4882a593Smuzhiyun 	if (!is_fc)
964*4882a593Smuzhiyun 		goto done;
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun 	/*
967*4882a593Smuzhiyun 	 * For FC devices retrieve FC HBA data.
968*4882a593Smuzhiyun 	 */
969*4882a593Smuzhiyun 	memset(vstor_packet, 0, sizeof(struct vstor_packet));
970*4882a593Smuzhiyun 	vstor_packet->operation = VSTOR_OPERATION_FCHBA_DATA;
971*4882a593Smuzhiyun 	ret = storvsc_execute_vstor_op(device, request, true);
972*4882a593Smuzhiyun 	if (ret != 0)
973*4882a593Smuzhiyun 		return ret;
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun 	/*
976*4882a593Smuzhiyun 	 * Cache the currently active port and node ww names.
977*4882a593Smuzhiyun 	 */
978*4882a593Smuzhiyun 	cache_wwn(stor_device, vstor_packet);
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun done:
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun 	memset(vstor_packet, 0, sizeof(struct vstor_packet));
983*4882a593Smuzhiyun 	vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
984*4882a593Smuzhiyun 	ret = storvsc_execute_vstor_op(device, request, true);
985*4882a593Smuzhiyun 	if (ret != 0)
986*4882a593Smuzhiyun 		return ret;
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 	if (process_sub_channels)
989*4882a593Smuzhiyun 		handle_multichannel_storage(device, max_chns);
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun 	return ret;
992*4882a593Smuzhiyun }
993*4882a593Smuzhiyun 
storvsc_handle_error(struct vmscsi_request * vm_srb,struct scsi_cmnd * scmnd,struct Scsi_Host * host,u8 asc,u8 ascq)994*4882a593Smuzhiyun static void storvsc_handle_error(struct vmscsi_request *vm_srb,
995*4882a593Smuzhiyun 				struct scsi_cmnd *scmnd,
996*4882a593Smuzhiyun 				struct Scsi_Host *host,
997*4882a593Smuzhiyun 				u8 asc, u8 ascq)
998*4882a593Smuzhiyun {
999*4882a593Smuzhiyun 	struct storvsc_scan_work *wrk;
1000*4882a593Smuzhiyun 	void (*process_err_fn)(struct work_struct *work);
1001*4882a593Smuzhiyun 	struct hv_host_device *host_dev = shost_priv(host);
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun 	switch (SRB_STATUS(vm_srb->srb_status)) {
1004*4882a593Smuzhiyun 	case SRB_STATUS_ERROR:
1005*4882a593Smuzhiyun 	case SRB_STATUS_ABORTED:
1006*4882a593Smuzhiyun 	case SRB_STATUS_INVALID_REQUEST:
1007*4882a593Smuzhiyun 		if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID) {
1008*4882a593Smuzhiyun 			/* Check for capacity change */
1009*4882a593Smuzhiyun 			if ((asc == 0x2a) && (ascq == 0x9)) {
1010*4882a593Smuzhiyun 				process_err_fn = storvsc_device_scan;
1011*4882a593Smuzhiyun 				/* Retry the I/O that triggered this. */
1012*4882a593Smuzhiyun 				set_host_byte(scmnd, DID_REQUEUE);
1013*4882a593Smuzhiyun 				goto do_work;
1014*4882a593Smuzhiyun 			}
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun 			/*
1017*4882a593Smuzhiyun 			 * Otherwise, let upper layer deal with the
1018*4882a593Smuzhiyun 			 * error when sense message is present
1019*4882a593Smuzhiyun 			 */
1020*4882a593Smuzhiyun 			return;
1021*4882a593Smuzhiyun 		}
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 		/*
1024*4882a593Smuzhiyun 		 * If there is an error; offline the device since all
1025*4882a593Smuzhiyun 		 * error recovery strategies would have already been
1026*4882a593Smuzhiyun 		 * deployed on the host side. However, if the command
1027*4882a593Smuzhiyun 		 * were a pass-through command deal with it appropriately.
1028*4882a593Smuzhiyun 		 */
1029*4882a593Smuzhiyun 		switch (scmnd->cmnd[0]) {
1030*4882a593Smuzhiyun 		case ATA_16:
1031*4882a593Smuzhiyun 		case ATA_12:
1032*4882a593Smuzhiyun 			set_host_byte(scmnd, DID_PASSTHROUGH);
1033*4882a593Smuzhiyun 			break;
1034*4882a593Smuzhiyun 		/*
1035*4882a593Smuzhiyun 		 * On some Hyper-V hosts TEST_UNIT_READY command can
1036*4882a593Smuzhiyun 		 * return SRB_STATUS_ERROR. Let the upper level code
1037*4882a593Smuzhiyun 		 * deal with it based on the sense information.
1038*4882a593Smuzhiyun 		 */
1039*4882a593Smuzhiyun 		case TEST_UNIT_READY:
1040*4882a593Smuzhiyun 			break;
1041*4882a593Smuzhiyun 		default:
1042*4882a593Smuzhiyun 			set_host_byte(scmnd, DID_ERROR);
1043*4882a593Smuzhiyun 		}
1044*4882a593Smuzhiyun 		return;
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun 	case SRB_STATUS_INVALID_LUN:
1047*4882a593Smuzhiyun 		set_host_byte(scmnd, DID_NO_CONNECT);
1048*4882a593Smuzhiyun 		process_err_fn = storvsc_remove_lun;
1049*4882a593Smuzhiyun 		goto do_work;
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 	}
1052*4882a593Smuzhiyun 	return;
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun do_work:
1055*4882a593Smuzhiyun 	/*
1056*4882a593Smuzhiyun 	 * We need to schedule work to process this error; schedule it.
1057*4882a593Smuzhiyun 	 */
1058*4882a593Smuzhiyun 	wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
1059*4882a593Smuzhiyun 	if (!wrk) {
1060*4882a593Smuzhiyun 		set_host_byte(scmnd, DID_TARGET_FAILURE);
1061*4882a593Smuzhiyun 		return;
1062*4882a593Smuzhiyun 	}
1063*4882a593Smuzhiyun 
1064*4882a593Smuzhiyun 	wrk->host = host;
1065*4882a593Smuzhiyun 	wrk->lun = vm_srb->lun;
1066*4882a593Smuzhiyun 	wrk->tgt_id = vm_srb->target_id;
1067*4882a593Smuzhiyun 	INIT_WORK(&wrk->work, process_err_fn);
1068*4882a593Smuzhiyun 	queue_work(host_dev->handle_error_wq, &wrk->work);
1069*4882a593Smuzhiyun }
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun 
storvsc_command_completion(struct storvsc_cmd_request * cmd_request,struct storvsc_device * stor_dev)1072*4882a593Smuzhiyun static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request,
1073*4882a593Smuzhiyun 				       struct storvsc_device *stor_dev)
1074*4882a593Smuzhiyun {
1075*4882a593Smuzhiyun 	struct scsi_cmnd *scmnd = cmd_request->cmd;
1076*4882a593Smuzhiyun 	struct scsi_sense_hdr sense_hdr;
1077*4882a593Smuzhiyun 	struct vmscsi_request *vm_srb;
1078*4882a593Smuzhiyun 	u32 data_transfer_length;
1079*4882a593Smuzhiyun 	struct Scsi_Host *host;
1080*4882a593Smuzhiyun 	u32 payload_sz = cmd_request->payload_sz;
1081*4882a593Smuzhiyun 	void *payload = cmd_request->payload;
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun 	host = stor_dev->host;
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun 	vm_srb = &cmd_request->vstor_packet.vm_srb;
1086*4882a593Smuzhiyun 	data_transfer_length = vm_srb->data_transfer_length;
1087*4882a593Smuzhiyun 
1088*4882a593Smuzhiyun 	scmnd->result = vm_srb->scsi_status;
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun 	if (scmnd->result) {
1091*4882a593Smuzhiyun 		if (scsi_normalize_sense(scmnd->sense_buffer,
1092*4882a593Smuzhiyun 				SCSI_SENSE_BUFFERSIZE, &sense_hdr) &&
1093*4882a593Smuzhiyun 		    !(sense_hdr.sense_key == NOT_READY &&
1094*4882a593Smuzhiyun 				 sense_hdr.asc == 0x03A) &&
1095*4882a593Smuzhiyun 		    do_logging(STORVSC_LOGGING_ERROR))
1096*4882a593Smuzhiyun 			scsi_print_sense_hdr(scmnd->device, "storvsc",
1097*4882a593Smuzhiyun 					     &sense_hdr);
1098*4882a593Smuzhiyun 	}
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 	if (vm_srb->srb_status != SRB_STATUS_SUCCESS) {
1101*4882a593Smuzhiyun 		storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
1102*4882a593Smuzhiyun 					 sense_hdr.ascq);
1103*4882a593Smuzhiyun 		/*
1104*4882a593Smuzhiyun 		 * The Windows driver set data_transfer_length on
1105*4882a593Smuzhiyun 		 * SRB_STATUS_DATA_OVERRUN. On other errors, this value
1106*4882a593Smuzhiyun 		 * is untouched.  In these cases we set it to 0.
1107*4882a593Smuzhiyun 		 */
1108*4882a593Smuzhiyun 		if (vm_srb->srb_status != SRB_STATUS_DATA_OVERRUN)
1109*4882a593Smuzhiyun 			data_transfer_length = 0;
1110*4882a593Smuzhiyun 	}
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun 	/* Validate data_transfer_length (from Hyper-V) */
1113*4882a593Smuzhiyun 	if (data_transfer_length > cmd_request->payload->range.len)
1114*4882a593Smuzhiyun 		data_transfer_length = cmd_request->payload->range.len;
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun 	scsi_set_resid(scmnd,
1117*4882a593Smuzhiyun 		cmd_request->payload->range.len - data_transfer_length);
1118*4882a593Smuzhiyun 
1119*4882a593Smuzhiyun 	scmnd->scsi_done(scmnd);
1120*4882a593Smuzhiyun 
1121*4882a593Smuzhiyun 	if (payload_sz >
1122*4882a593Smuzhiyun 		sizeof(struct vmbus_channel_packet_multipage_buffer))
1123*4882a593Smuzhiyun 		kfree(payload);
1124*4882a593Smuzhiyun }
1125*4882a593Smuzhiyun 
storvsc_on_io_completion(struct storvsc_device * stor_device,struct vstor_packet * vstor_packet,struct storvsc_cmd_request * request)1126*4882a593Smuzhiyun static void storvsc_on_io_completion(struct storvsc_device *stor_device,
1127*4882a593Smuzhiyun 				  struct vstor_packet *vstor_packet,
1128*4882a593Smuzhiyun 				  struct storvsc_cmd_request *request)
1129*4882a593Smuzhiyun {
1130*4882a593Smuzhiyun 	struct vstor_packet *stor_pkt;
1131*4882a593Smuzhiyun 	struct hv_device *device = stor_device->device;
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 	stor_pkt = &request->vstor_packet;
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun 	/*
1136*4882a593Smuzhiyun 	 * The current SCSI handling on the host side does
1137*4882a593Smuzhiyun 	 * not correctly handle:
1138*4882a593Smuzhiyun 	 * INQUIRY command with page code parameter set to 0x80
1139*4882a593Smuzhiyun 	 * MODE_SENSE command with cmd[2] == 0x1c
1140*4882a593Smuzhiyun 	 *
1141*4882a593Smuzhiyun 	 * Setup srb and scsi status so this won't be fatal.
1142*4882a593Smuzhiyun 	 * We do this so we can distinguish truly fatal failues
1143*4882a593Smuzhiyun 	 * (srb status == 0x4) and off-line the device in that case.
1144*4882a593Smuzhiyun 	 */
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 	if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
1147*4882a593Smuzhiyun 	   (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
1148*4882a593Smuzhiyun 		vstor_packet->vm_srb.scsi_status = 0;
1149*4882a593Smuzhiyun 		vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
1150*4882a593Smuzhiyun 	}
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 
1153*4882a593Smuzhiyun 	/* Copy over the status...etc */
1154*4882a593Smuzhiyun 	stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
1155*4882a593Smuzhiyun 	stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun 	/* Validate sense_info_length (from Hyper-V) */
1158*4882a593Smuzhiyun 	if (vstor_packet->vm_srb.sense_info_length > sense_buffer_size)
1159*4882a593Smuzhiyun 		vstor_packet->vm_srb.sense_info_length = sense_buffer_size;
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun 	stor_pkt->vm_srb.sense_info_length =
1162*4882a593Smuzhiyun 	vstor_packet->vm_srb.sense_info_length;
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 	if (vstor_packet->vm_srb.scsi_status != 0 ||
1165*4882a593Smuzhiyun 	    vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS)
1166*4882a593Smuzhiyun 		storvsc_log(device, STORVSC_LOGGING_WARN,
1167*4882a593Smuzhiyun 			"cmd 0x%x scsi status 0x%x srb status 0x%x\n",
1168*4882a593Smuzhiyun 			stor_pkt->vm_srb.cdb[0],
1169*4882a593Smuzhiyun 			vstor_packet->vm_srb.scsi_status,
1170*4882a593Smuzhiyun 			vstor_packet->vm_srb.srb_status);
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 	if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
1173*4882a593Smuzhiyun 		/* CHECK_CONDITION */
1174*4882a593Smuzhiyun 		if (vstor_packet->vm_srb.srb_status &
1175*4882a593Smuzhiyun 			SRB_STATUS_AUTOSENSE_VALID) {
1176*4882a593Smuzhiyun 			/* autosense data available */
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 			storvsc_log(device, STORVSC_LOGGING_WARN,
1179*4882a593Smuzhiyun 				"stor pkt %p autosense data valid - len %d\n",
1180*4882a593Smuzhiyun 				request, vstor_packet->vm_srb.sense_info_length);
1181*4882a593Smuzhiyun 
1182*4882a593Smuzhiyun 			memcpy(request->cmd->sense_buffer,
1183*4882a593Smuzhiyun 			       vstor_packet->vm_srb.sense_data,
1184*4882a593Smuzhiyun 			       vstor_packet->vm_srb.sense_info_length);
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 		}
1187*4882a593Smuzhiyun 	}
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun 	stor_pkt->vm_srb.data_transfer_length =
1190*4882a593Smuzhiyun 	vstor_packet->vm_srb.data_transfer_length;
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun 	storvsc_command_completion(request, stor_device);
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun 	if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
1195*4882a593Smuzhiyun 		stor_device->drain_notify)
1196*4882a593Smuzhiyun 		wake_up(&stor_device->waiting_to_drain);
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun }
1200*4882a593Smuzhiyun 
storvsc_on_receive(struct storvsc_device * stor_device,struct vstor_packet * vstor_packet,struct storvsc_cmd_request * request)1201*4882a593Smuzhiyun static void storvsc_on_receive(struct storvsc_device *stor_device,
1202*4882a593Smuzhiyun 			     struct vstor_packet *vstor_packet,
1203*4882a593Smuzhiyun 			     struct storvsc_cmd_request *request)
1204*4882a593Smuzhiyun {
1205*4882a593Smuzhiyun 	struct hv_host_device *host_dev;
1206*4882a593Smuzhiyun 	switch (vstor_packet->operation) {
1207*4882a593Smuzhiyun 	case VSTOR_OPERATION_COMPLETE_IO:
1208*4882a593Smuzhiyun 		storvsc_on_io_completion(stor_device, vstor_packet, request);
1209*4882a593Smuzhiyun 		break;
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun 	case VSTOR_OPERATION_REMOVE_DEVICE:
1212*4882a593Smuzhiyun 	case VSTOR_OPERATION_ENUMERATE_BUS:
1213*4882a593Smuzhiyun 		host_dev = shost_priv(stor_device->host);
1214*4882a593Smuzhiyun 		queue_work(
1215*4882a593Smuzhiyun 			host_dev->handle_error_wq, &host_dev->host_scan_work);
1216*4882a593Smuzhiyun 		break;
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 	case VSTOR_OPERATION_FCHBA_DATA:
1219*4882a593Smuzhiyun 		cache_wwn(stor_device, vstor_packet);
1220*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1221*4882a593Smuzhiyun 		fc_host_node_name(stor_device->host) = stor_device->node_name;
1222*4882a593Smuzhiyun 		fc_host_port_name(stor_device->host) = stor_device->port_name;
1223*4882a593Smuzhiyun #endif
1224*4882a593Smuzhiyun 		break;
1225*4882a593Smuzhiyun 	default:
1226*4882a593Smuzhiyun 		break;
1227*4882a593Smuzhiyun 	}
1228*4882a593Smuzhiyun }
1229*4882a593Smuzhiyun 
storvsc_on_channel_callback(void * context)1230*4882a593Smuzhiyun static void storvsc_on_channel_callback(void *context)
1231*4882a593Smuzhiyun {
1232*4882a593Smuzhiyun 	struct vmbus_channel *channel = (struct vmbus_channel *)context;
1233*4882a593Smuzhiyun 	const struct vmpacket_descriptor *desc;
1234*4882a593Smuzhiyun 	struct hv_device *device;
1235*4882a593Smuzhiyun 	struct storvsc_device *stor_device;
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 	if (channel->primary_channel != NULL)
1238*4882a593Smuzhiyun 		device = channel->primary_channel->device_obj;
1239*4882a593Smuzhiyun 	else
1240*4882a593Smuzhiyun 		device = channel->device_obj;
1241*4882a593Smuzhiyun 
1242*4882a593Smuzhiyun 	stor_device = get_in_stor_device(device);
1243*4882a593Smuzhiyun 	if (!stor_device)
1244*4882a593Smuzhiyun 		return;
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 	foreach_vmbus_pkt(desc, channel) {
1247*4882a593Smuzhiyun 		void *packet = hv_pkt_data(desc);
1248*4882a593Smuzhiyun 		struct storvsc_cmd_request *request;
1249*4882a593Smuzhiyun 
1250*4882a593Smuzhiyun 		request = (struct storvsc_cmd_request *)
1251*4882a593Smuzhiyun 			((unsigned long)desc->trans_id);
1252*4882a593Smuzhiyun 
1253*4882a593Smuzhiyun 		if (request == &stor_device->init_request ||
1254*4882a593Smuzhiyun 		    request == &stor_device->reset_request) {
1255*4882a593Smuzhiyun 			memcpy(&request->vstor_packet, packet,
1256*4882a593Smuzhiyun 			       (sizeof(struct vstor_packet) - vmscsi_size_delta));
1257*4882a593Smuzhiyun 			complete(&request->wait_event);
1258*4882a593Smuzhiyun 		} else {
1259*4882a593Smuzhiyun 			storvsc_on_receive(stor_device, packet, request);
1260*4882a593Smuzhiyun 		}
1261*4882a593Smuzhiyun 	}
1262*4882a593Smuzhiyun }
1263*4882a593Smuzhiyun 
storvsc_connect_to_vsp(struct hv_device * device,u32 ring_size,bool is_fc)1264*4882a593Smuzhiyun static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
1265*4882a593Smuzhiyun 				  bool is_fc)
1266*4882a593Smuzhiyun {
1267*4882a593Smuzhiyun 	struct vmstorage_channel_properties props;
1268*4882a593Smuzhiyun 	int ret;
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun 	memset(&props, 0, sizeof(struct vmstorage_channel_properties));
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun 	ret = vmbus_open(device->channel,
1273*4882a593Smuzhiyun 			 ring_size,
1274*4882a593Smuzhiyun 			 ring_size,
1275*4882a593Smuzhiyun 			 (void *)&props,
1276*4882a593Smuzhiyun 			 sizeof(struct vmstorage_channel_properties),
1277*4882a593Smuzhiyun 			 storvsc_on_channel_callback, device->channel);
1278*4882a593Smuzhiyun 
1279*4882a593Smuzhiyun 	if (ret != 0)
1280*4882a593Smuzhiyun 		return ret;
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun 	ret = storvsc_channel_init(device, is_fc);
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun 	return ret;
1285*4882a593Smuzhiyun }
1286*4882a593Smuzhiyun 
storvsc_dev_remove(struct hv_device * device)1287*4882a593Smuzhiyun static int storvsc_dev_remove(struct hv_device *device)
1288*4882a593Smuzhiyun {
1289*4882a593Smuzhiyun 	struct storvsc_device *stor_device;
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun 	stor_device = hv_get_drvdata(device);
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun 	stor_device->destroy = true;
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun 	/* Make sure flag is set before waiting */
1296*4882a593Smuzhiyun 	wmb();
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun 	/*
1299*4882a593Smuzhiyun 	 * At this point, all outbound traffic should be disable. We
1300*4882a593Smuzhiyun 	 * only allow inbound traffic (responses) to proceed so that
1301*4882a593Smuzhiyun 	 * outstanding requests can be completed.
1302*4882a593Smuzhiyun 	 */
1303*4882a593Smuzhiyun 
1304*4882a593Smuzhiyun 	storvsc_wait_to_drain(stor_device);
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun 	/*
1307*4882a593Smuzhiyun 	 * Since we have already drained, we don't need to busy wait
1308*4882a593Smuzhiyun 	 * as was done in final_release_stor_device()
1309*4882a593Smuzhiyun 	 * Note that we cannot set the ext pointer to NULL until
1310*4882a593Smuzhiyun 	 * we have drained - to drain the outgoing packets, we need to
1311*4882a593Smuzhiyun 	 * allow incoming packets.
1312*4882a593Smuzhiyun 	 */
1313*4882a593Smuzhiyun 	hv_set_drvdata(device, NULL);
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun 	/* Close the channel */
1316*4882a593Smuzhiyun 	vmbus_close(device->channel);
1317*4882a593Smuzhiyun 
1318*4882a593Smuzhiyun 	kfree(stor_device->stor_chns);
1319*4882a593Smuzhiyun 	kfree(stor_device);
1320*4882a593Smuzhiyun 	return 0;
1321*4882a593Smuzhiyun }
1322*4882a593Smuzhiyun 
get_og_chn(struct storvsc_device * stor_device,u16 q_num)1323*4882a593Smuzhiyun static struct vmbus_channel *get_og_chn(struct storvsc_device *stor_device,
1324*4882a593Smuzhiyun 					u16 q_num)
1325*4882a593Smuzhiyun {
1326*4882a593Smuzhiyun 	u16 slot = 0;
1327*4882a593Smuzhiyun 	u16 hash_qnum;
1328*4882a593Smuzhiyun 	const struct cpumask *node_mask;
1329*4882a593Smuzhiyun 	int num_channels, tgt_cpu;
1330*4882a593Smuzhiyun 
1331*4882a593Smuzhiyun 	if (stor_device->num_sc == 0) {
1332*4882a593Smuzhiyun 		stor_device->stor_chns[q_num] = stor_device->device->channel;
1333*4882a593Smuzhiyun 		return stor_device->device->channel;
1334*4882a593Smuzhiyun 	}
1335*4882a593Smuzhiyun 
1336*4882a593Smuzhiyun 	/*
1337*4882a593Smuzhiyun 	 * Our channel array is sparsley populated and we
1338*4882a593Smuzhiyun 	 * initiated I/O on a processor/hw-q that does not
1339*4882a593Smuzhiyun 	 * currently have a designated channel. Fix this.
1340*4882a593Smuzhiyun 	 * The strategy is simple:
1341*4882a593Smuzhiyun 	 * I. Ensure NUMA locality
1342*4882a593Smuzhiyun 	 * II. Distribute evenly (best effort)
1343*4882a593Smuzhiyun 	 */
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 	node_mask = cpumask_of_node(cpu_to_node(q_num));
1346*4882a593Smuzhiyun 
1347*4882a593Smuzhiyun 	num_channels = 0;
1348*4882a593Smuzhiyun 	for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
1349*4882a593Smuzhiyun 		if (cpumask_test_cpu(tgt_cpu, node_mask))
1350*4882a593Smuzhiyun 			num_channels++;
1351*4882a593Smuzhiyun 	}
1352*4882a593Smuzhiyun 	if (num_channels == 0) {
1353*4882a593Smuzhiyun 		stor_device->stor_chns[q_num] = stor_device->device->channel;
1354*4882a593Smuzhiyun 		return stor_device->device->channel;
1355*4882a593Smuzhiyun 	}
1356*4882a593Smuzhiyun 
1357*4882a593Smuzhiyun 	hash_qnum = q_num;
1358*4882a593Smuzhiyun 	while (hash_qnum >= num_channels)
1359*4882a593Smuzhiyun 		hash_qnum -= num_channels;
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun 	for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
1362*4882a593Smuzhiyun 		if (!cpumask_test_cpu(tgt_cpu, node_mask))
1363*4882a593Smuzhiyun 			continue;
1364*4882a593Smuzhiyun 		if (slot == hash_qnum)
1365*4882a593Smuzhiyun 			break;
1366*4882a593Smuzhiyun 		slot++;
1367*4882a593Smuzhiyun 	}
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun 	stor_device->stor_chns[q_num] = stor_device->stor_chns[tgt_cpu];
1370*4882a593Smuzhiyun 
1371*4882a593Smuzhiyun 	return stor_device->stor_chns[q_num];
1372*4882a593Smuzhiyun }
1373*4882a593Smuzhiyun 
1374*4882a593Smuzhiyun 
storvsc_do_io(struct hv_device * device,struct storvsc_cmd_request * request,u16 q_num)1375*4882a593Smuzhiyun static int storvsc_do_io(struct hv_device *device,
1376*4882a593Smuzhiyun 			 struct storvsc_cmd_request *request, u16 q_num)
1377*4882a593Smuzhiyun {
1378*4882a593Smuzhiyun 	struct storvsc_device *stor_device;
1379*4882a593Smuzhiyun 	struct vstor_packet *vstor_packet;
1380*4882a593Smuzhiyun 	struct vmbus_channel *outgoing_channel, *channel;
1381*4882a593Smuzhiyun 	unsigned long flags;
1382*4882a593Smuzhiyun 	int ret = 0;
1383*4882a593Smuzhiyun 	const struct cpumask *node_mask;
1384*4882a593Smuzhiyun 	int tgt_cpu;
1385*4882a593Smuzhiyun 
1386*4882a593Smuzhiyun 	vstor_packet = &request->vstor_packet;
1387*4882a593Smuzhiyun 	stor_device = get_out_stor_device(device);
1388*4882a593Smuzhiyun 
1389*4882a593Smuzhiyun 	if (!stor_device)
1390*4882a593Smuzhiyun 		return -ENODEV;
1391*4882a593Smuzhiyun 
1392*4882a593Smuzhiyun 
1393*4882a593Smuzhiyun 	request->device  = device;
1394*4882a593Smuzhiyun 	/*
1395*4882a593Smuzhiyun 	 * Select an appropriate channel to send the request out.
1396*4882a593Smuzhiyun 	 */
1397*4882a593Smuzhiyun 	/* See storvsc_change_target_cpu(). */
1398*4882a593Smuzhiyun 	outgoing_channel = READ_ONCE(stor_device->stor_chns[q_num]);
1399*4882a593Smuzhiyun 	if (outgoing_channel != NULL) {
1400*4882a593Smuzhiyun 		if (outgoing_channel->target_cpu == q_num) {
1401*4882a593Smuzhiyun 			/*
1402*4882a593Smuzhiyun 			 * Ideally, we want to pick a different channel if
1403*4882a593Smuzhiyun 			 * available on the same NUMA node.
1404*4882a593Smuzhiyun 			 */
1405*4882a593Smuzhiyun 			node_mask = cpumask_of_node(cpu_to_node(q_num));
1406*4882a593Smuzhiyun 			for_each_cpu_wrap(tgt_cpu,
1407*4882a593Smuzhiyun 				 &stor_device->alloced_cpus, q_num + 1) {
1408*4882a593Smuzhiyun 				if (!cpumask_test_cpu(tgt_cpu, node_mask))
1409*4882a593Smuzhiyun 					continue;
1410*4882a593Smuzhiyun 				if (tgt_cpu == q_num)
1411*4882a593Smuzhiyun 					continue;
1412*4882a593Smuzhiyun 				channel = READ_ONCE(
1413*4882a593Smuzhiyun 					stor_device->stor_chns[tgt_cpu]);
1414*4882a593Smuzhiyun 				if (channel == NULL)
1415*4882a593Smuzhiyun 					continue;
1416*4882a593Smuzhiyun 				if (hv_get_avail_to_write_percent(
1417*4882a593Smuzhiyun 							&channel->outbound)
1418*4882a593Smuzhiyun 						> ring_avail_percent_lowater) {
1419*4882a593Smuzhiyun 					outgoing_channel = channel;
1420*4882a593Smuzhiyun 					goto found_channel;
1421*4882a593Smuzhiyun 				}
1422*4882a593Smuzhiyun 			}
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun 			/*
1425*4882a593Smuzhiyun 			 * All the other channels on the same NUMA node are
1426*4882a593Smuzhiyun 			 * busy. Try to use the channel on the current CPU
1427*4882a593Smuzhiyun 			 */
1428*4882a593Smuzhiyun 			if (hv_get_avail_to_write_percent(
1429*4882a593Smuzhiyun 						&outgoing_channel->outbound)
1430*4882a593Smuzhiyun 					> ring_avail_percent_lowater)
1431*4882a593Smuzhiyun 				goto found_channel;
1432*4882a593Smuzhiyun 
1433*4882a593Smuzhiyun 			/*
1434*4882a593Smuzhiyun 			 * If we reach here, all the channels on the current
1435*4882a593Smuzhiyun 			 * NUMA node are busy. Try to find a channel in
1436*4882a593Smuzhiyun 			 * other NUMA nodes
1437*4882a593Smuzhiyun 			 */
1438*4882a593Smuzhiyun 			for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
1439*4882a593Smuzhiyun 				if (cpumask_test_cpu(tgt_cpu, node_mask))
1440*4882a593Smuzhiyun 					continue;
1441*4882a593Smuzhiyun 				channel = READ_ONCE(
1442*4882a593Smuzhiyun 					stor_device->stor_chns[tgt_cpu]);
1443*4882a593Smuzhiyun 				if (channel == NULL)
1444*4882a593Smuzhiyun 					continue;
1445*4882a593Smuzhiyun 				if (hv_get_avail_to_write_percent(
1446*4882a593Smuzhiyun 							&channel->outbound)
1447*4882a593Smuzhiyun 						> ring_avail_percent_lowater) {
1448*4882a593Smuzhiyun 					outgoing_channel = channel;
1449*4882a593Smuzhiyun 					goto found_channel;
1450*4882a593Smuzhiyun 				}
1451*4882a593Smuzhiyun 			}
1452*4882a593Smuzhiyun 		}
1453*4882a593Smuzhiyun 	} else {
1454*4882a593Smuzhiyun 		spin_lock_irqsave(&stor_device->lock, flags);
1455*4882a593Smuzhiyun 		outgoing_channel = stor_device->stor_chns[q_num];
1456*4882a593Smuzhiyun 		if (outgoing_channel != NULL) {
1457*4882a593Smuzhiyun 			spin_unlock_irqrestore(&stor_device->lock, flags);
1458*4882a593Smuzhiyun 			goto found_channel;
1459*4882a593Smuzhiyun 		}
1460*4882a593Smuzhiyun 		outgoing_channel = get_og_chn(stor_device, q_num);
1461*4882a593Smuzhiyun 		spin_unlock_irqrestore(&stor_device->lock, flags);
1462*4882a593Smuzhiyun 	}
1463*4882a593Smuzhiyun 
1464*4882a593Smuzhiyun found_channel:
1465*4882a593Smuzhiyun 	vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
1466*4882a593Smuzhiyun 
1467*4882a593Smuzhiyun 	vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) -
1468*4882a593Smuzhiyun 					vmscsi_size_delta);
1469*4882a593Smuzhiyun 
1470*4882a593Smuzhiyun 
1471*4882a593Smuzhiyun 	vstor_packet->vm_srb.sense_info_length = sense_buffer_size;
1472*4882a593Smuzhiyun 
1473*4882a593Smuzhiyun 
1474*4882a593Smuzhiyun 	vstor_packet->vm_srb.data_transfer_length =
1475*4882a593Smuzhiyun 	request->payload->range.len;
1476*4882a593Smuzhiyun 
1477*4882a593Smuzhiyun 	vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
1478*4882a593Smuzhiyun 
1479*4882a593Smuzhiyun 	if (request->payload->range.len) {
1480*4882a593Smuzhiyun 
1481*4882a593Smuzhiyun 		ret = vmbus_sendpacket_mpb_desc(outgoing_channel,
1482*4882a593Smuzhiyun 				request->payload, request->payload_sz,
1483*4882a593Smuzhiyun 				vstor_packet,
1484*4882a593Smuzhiyun 				(sizeof(struct vstor_packet) -
1485*4882a593Smuzhiyun 				vmscsi_size_delta),
1486*4882a593Smuzhiyun 				(unsigned long)request);
1487*4882a593Smuzhiyun 	} else {
1488*4882a593Smuzhiyun 		ret = vmbus_sendpacket(outgoing_channel, vstor_packet,
1489*4882a593Smuzhiyun 			       (sizeof(struct vstor_packet) -
1490*4882a593Smuzhiyun 				vmscsi_size_delta),
1491*4882a593Smuzhiyun 			       (unsigned long)request,
1492*4882a593Smuzhiyun 			       VM_PKT_DATA_INBAND,
1493*4882a593Smuzhiyun 			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1494*4882a593Smuzhiyun 	}
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun 	if (ret != 0)
1497*4882a593Smuzhiyun 		return ret;
1498*4882a593Smuzhiyun 
1499*4882a593Smuzhiyun 	atomic_inc(&stor_device->num_outstanding_req);
1500*4882a593Smuzhiyun 
1501*4882a593Smuzhiyun 	return ret;
1502*4882a593Smuzhiyun }
1503*4882a593Smuzhiyun 
storvsc_device_alloc(struct scsi_device * sdevice)1504*4882a593Smuzhiyun static int storvsc_device_alloc(struct scsi_device *sdevice)
1505*4882a593Smuzhiyun {
1506*4882a593Smuzhiyun 	/*
1507*4882a593Smuzhiyun 	 * Set blist flag to permit the reading of the VPD pages even when
1508*4882a593Smuzhiyun 	 * the target may claim SPC-2 compliance. MSFT targets currently
1509*4882a593Smuzhiyun 	 * claim SPC-2 compliance while they implement post SPC-2 features.
1510*4882a593Smuzhiyun 	 * With this flag we can correctly handle WRITE_SAME_16 issues.
1511*4882a593Smuzhiyun 	 *
1512*4882a593Smuzhiyun 	 * Hypervisor reports SCSI_UNKNOWN type for DVD ROM device but
1513*4882a593Smuzhiyun 	 * still supports REPORT LUN.
1514*4882a593Smuzhiyun 	 */
1515*4882a593Smuzhiyun 	sdevice->sdev_bflags = BLIST_REPORTLUN2 | BLIST_TRY_VPD_PAGES;
1516*4882a593Smuzhiyun 
1517*4882a593Smuzhiyun 	return 0;
1518*4882a593Smuzhiyun }
1519*4882a593Smuzhiyun 
storvsc_device_configure(struct scsi_device * sdevice)1520*4882a593Smuzhiyun static int storvsc_device_configure(struct scsi_device *sdevice)
1521*4882a593Smuzhiyun {
1522*4882a593Smuzhiyun 	blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
1523*4882a593Smuzhiyun 
1524*4882a593Smuzhiyun 	sdevice->no_write_same = 1;
1525*4882a593Smuzhiyun 
1526*4882a593Smuzhiyun 	/*
1527*4882a593Smuzhiyun 	 * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3
1528*4882a593Smuzhiyun 	 * if the device is a MSFT virtual device.  If the host is
1529*4882a593Smuzhiyun 	 * WIN10 or newer, allow write_same.
1530*4882a593Smuzhiyun 	 */
1531*4882a593Smuzhiyun 	if (!strncmp(sdevice->vendor, "Msft", 4)) {
1532*4882a593Smuzhiyun 		switch (vmstor_proto_version) {
1533*4882a593Smuzhiyun 		case VMSTOR_PROTO_VERSION_WIN8:
1534*4882a593Smuzhiyun 		case VMSTOR_PROTO_VERSION_WIN8_1:
1535*4882a593Smuzhiyun 			sdevice->scsi_level = SCSI_SPC_3;
1536*4882a593Smuzhiyun 			break;
1537*4882a593Smuzhiyun 		}
1538*4882a593Smuzhiyun 
1539*4882a593Smuzhiyun 		if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN10)
1540*4882a593Smuzhiyun 			sdevice->no_write_same = 0;
1541*4882a593Smuzhiyun 	}
1542*4882a593Smuzhiyun 
1543*4882a593Smuzhiyun 	return 0;
1544*4882a593Smuzhiyun }
1545*4882a593Smuzhiyun 
storvsc_get_chs(struct scsi_device * sdev,struct block_device * bdev,sector_t capacity,int * info)1546*4882a593Smuzhiyun static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1547*4882a593Smuzhiyun 			   sector_t capacity, int *info)
1548*4882a593Smuzhiyun {
1549*4882a593Smuzhiyun 	sector_t nsect = capacity;
1550*4882a593Smuzhiyun 	sector_t cylinders = nsect;
1551*4882a593Smuzhiyun 	int heads, sectors_pt;
1552*4882a593Smuzhiyun 
1553*4882a593Smuzhiyun 	/*
1554*4882a593Smuzhiyun 	 * We are making up these values; let us keep it simple.
1555*4882a593Smuzhiyun 	 */
1556*4882a593Smuzhiyun 	heads = 0xff;
1557*4882a593Smuzhiyun 	sectors_pt = 0x3f;      /* Sectors per track */
1558*4882a593Smuzhiyun 	sector_div(cylinders, heads * sectors_pt);
1559*4882a593Smuzhiyun 	if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1560*4882a593Smuzhiyun 		cylinders = 0xffff;
1561*4882a593Smuzhiyun 
1562*4882a593Smuzhiyun 	info[0] = heads;
1563*4882a593Smuzhiyun 	info[1] = sectors_pt;
1564*4882a593Smuzhiyun 	info[2] = (int)cylinders;
1565*4882a593Smuzhiyun 
1566*4882a593Smuzhiyun 	return 0;
1567*4882a593Smuzhiyun }
1568*4882a593Smuzhiyun 
storvsc_host_reset_handler(struct scsi_cmnd * scmnd)1569*4882a593Smuzhiyun static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
1570*4882a593Smuzhiyun {
1571*4882a593Smuzhiyun 	struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1572*4882a593Smuzhiyun 	struct hv_device *device = host_dev->dev;
1573*4882a593Smuzhiyun 
1574*4882a593Smuzhiyun 	struct storvsc_device *stor_device;
1575*4882a593Smuzhiyun 	struct storvsc_cmd_request *request;
1576*4882a593Smuzhiyun 	struct vstor_packet *vstor_packet;
1577*4882a593Smuzhiyun 	int ret, t;
1578*4882a593Smuzhiyun 
1579*4882a593Smuzhiyun 
1580*4882a593Smuzhiyun 	stor_device = get_out_stor_device(device);
1581*4882a593Smuzhiyun 	if (!stor_device)
1582*4882a593Smuzhiyun 		return FAILED;
1583*4882a593Smuzhiyun 
1584*4882a593Smuzhiyun 	request = &stor_device->reset_request;
1585*4882a593Smuzhiyun 	vstor_packet = &request->vstor_packet;
1586*4882a593Smuzhiyun 	memset(vstor_packet, 0, sizeof(struct vstor_packet));
1587*4882a593Smuzhiyun 
1588*4882a593Smuzhiyun 	init_completion(&request->wait_event);
1589*4882a593Smuzhiyun 
1590*4882a593Smuzhiyun 	vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1591*4882a593Smuzhiyun 	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1592*4882a593Smuzhiyun 	vstor_packet->vm_srb.path_id = stor_device->path_id;
1593*4882a593Smuzhiyun 
1594*4882a593Smuzhiyun 	ret = vmbus_sendpacket(device->channel, vstor_packet,
1595*4882a593Smuzhiyun 			       (sizeof(struct vstor_packet) -
1596*4882a593Smuzhiyun 				vmscsi_size_delta),
1597*4882a593Smuzhiyun 			       (unsigned long)&stor_device->reset_request,
1598*4882a593Smuzhiyun 			       VM_PKT_DATA_INBAND,
1599*4882a593Smuzhiyun 			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1600*4882a593Smuzhiyun 	if (ret != 0)
1601*4882a593Smuzhiyun 		return FAILED;
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun 	t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
1604*4882a593Smuzhiyun 	if (t == 0)
1605*4882a593Smuzhiyun 		return TIMEOUT_ERROR;
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun 
1608*4882a593Smuzhiyun 	/*
1609*4882a593Smuzhiyun 	 * At this point, all outstanding requests in the adapter
1610*4882a593Smuzhiyun 	 * should have been flushed out and return to us
1611*4882a593Smuzhiyun 	 * There is a potential race here where the host may be in
1612*4882a593Smuzhiyun 	 * the process of responding when we return from here.
1613*4882a593Smuzhiyun 	 * Just wait for all in-transit packets to be accounted for
1614*4882a593Smuzhiyun 	 * before we return from here.
1615*4882a593Smuzhiyun 	 */
1616*4882a593Smuzhiyun 	storvsc_wait_to_drain(stor_device);
1617*4882a593Smuzhiyun 
1618*4882a593Smuzhiyun 	return SUCCESS;
1619*4882a593Smuzhiyun }
1620*4882a593Smuzhiyun 
1621*4882a593Smuzhiyun /*
1622*4882a593Smuzhiyun  * The host guarantees to respond to each command, although I/O latencies might
1623*4882a593Smuzhiyun  * be unbounded on Azure.  Reset the timer unconditionally to give the host a
1624*4882a593Smuzhiyun  * chance to perform EH.
1625*4882a593Smuzhiyun  */
storvsc_eh_timed_out(struct scsi_cmnd * scmnd)1626*4882a593Smuzhiyun static enum blk_eh_timer_return storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
1627*4882a593Smuzhiyun {
1628*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1629*4882a593Smuzhiyun 	if (scmnd->device->host->transportt == fc_transport_template)
1630*4882a593Smuzhiyun 		return fc_eh_timed_out(scmnd);
1631*4882a593Smuzhiyun #endif
1632*4882a593Smuzhiyun 	return BLK_EH_RESET_TIMER;
1633*4882a593Smuzhiyun }
1634*4882a593Smuzhiyun 
storvsc_scsi_cmd_ok(struct scsi_cmnd * scmnd)1635*4882a593Smuzhiyun static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
1636*4882a593Smuzhiyun {
1637*4882a593Smuzhiyun 	bool allowed = true;
1638*4882a593Smuzhiyun 	u8 scsi_op = scmnd->cmnd[0];
1639*4882a593Smuzhiyun 
1640*4882a593Smuzhiyun 	switch (scsi_op) {
1641*4882a593Smuzhiyun 	/* the host does not handle WRITE_SAME, log accident usage */
1642*4882a593Smuzhiyun 	case WRITE_SAME:
1643*4882a593Smuzhiyun 	/*
1644*4882a593Smuzhiyun 	 * smartd sends this command and the host does not handle
1645*4882a593Smuzhiyun 	 * this. So, don't send it.
1646*4882a593Smuzhiyun 	 */
1647*4882a593Smuzhiyun 	case SET_WINDOW:
1648*4882a593Smuzhiyun 		scmnd->result = ILLEGAL_REQUEST << 16;
1649*4882a593Smuzhiyun 		allowed = false;
1650*4882a593Smuzhiyun 		break;
1651*4882a593Smuzhiyun 	default:
1652*4882a593Smuzhiyun 		break;
1653*4882a593Smuzhiyun 	}
1654*4882a593Smuzhiyun 	return allowed;
1655*4882a593Smuzhiyun }
1656*4882a593Smuzhiyun 
storvsc_queuecommand(struct Scsi_Host * host,struct scsi_cmnd * scmnd)1657*4882a593Smuzhiyun static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
1658*4882a593Smuzhiyun {
1659*4882a593Smuzhiyun 	int ret;
1660*4882a593Smuzhiyun 	struct hv_host_device *host_dev = shost_priv(host);
1661*4882a593Smuzhiyun 	struct hv_device *dev = host_dev->dev;
1662*4882a593Smuzhiyun 	struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd);
1663*4882a593Smuzhiyun 	int i;
1664*4882a593Smuzhiyun 	struct scatterlist *sgl;
1665*4882a593Smuzhiyun 	unsigned int sg_count = 0;
1666*4882a593Smuzhiyun 	struct vmscsi_request *vm_srb;
1667*4882a593Smuzhiyun 	struct scatterlist *cur_sgl;
1668*4882a593Smuzhiyun 	struct vmbus_packet_mpb_array  *payload;
1669*4882a593Smuzhiyun 	u32 payload_sz;
1670*4882a593Smuzhiyun 	u32 length;
1671*4882a593Smuzhiyun 
1672*4882a593Smuzhiyun 	if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) {
1673*4882a593Smuzhiyun 		/*
1674*4882a593Smuzhiyun 		 * On legacy hosts filter unimplemented commands.
1675*4882a593Smuzhiyun 		 * Future hosts are expected to correctly handle
1676*4882a593Smuzhiyun 		 * unsupported commands. Furthermore, it is
1677*4882a593Smuzhiyun 		 * possible that some of the currently
1678*4882a593Smuzhiyun 		 * unsupported commands maybe supported in
1679*4882a593Smuzhiyun 		 * future versions of the host.
1680*4882a593Smuzhiyun 		 */
1681*4882a593Smuzhiyun 		if (!storvsc_scsi_cmd_ok(scmnd)) {
1682*4882a593Smuzhiyun 			scmnd->scsi_done(scmnd);
1683*4882a593Smuzhiyun 			return 0;
1684*4882a593Smuzhiyun 		}
1685*4882a593Smuzhiyun 	}
1686*4882a593Smuzhiyun 
1687*4882a593Smuzhiyun 	/* Setup the cmd request */
1688*4882a593Smuzhiyun 	cmd_request->cmd = scmnd;
1689*4882a593Smuzhiyun 
1690*4882a593Smuzhiyun 	memset(&cmd_request->vstor_packet, 0, sizeof(struct vstor_packet));
1691*4882a593Smuzhiyun 	vm_srb = &cmd_request->vstor_packet.vm_srb;
1692*4882a593Smuzhiyun 	vm_srb->win8_extension.time_out_value = 60;
1693*4882a593Smuzhiyun 
1694*4882a593Smuzhiyun 	vm_srb->win8_extension.srb_flags |=
1695*4882a593Smuzhiyun 		SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
1696*4882a593Smuzhiyun 
1697*4882a593Smuzhiyun 	if (scmnd->device->tagged_supported) {
1698*4882a593Smuzhiyun 		vm_srb->win8_extension.srb_flags |=
1699*4882a593Smuzhiyun 		(SRB_FLAGS_QUEUE_ACTION_ENABLE | SRB_FLAGS_NO_QUEUE_FREEZE);
1700*4882a593Smuzhiyun 		vm_srb->win8_extension.queue_tag = SP_UNTAGGED;
1701*4882a593Smuzhiyun 		vm_srb->win8_extension.queue_action = SRB_SIMPLE_TAG_REQUEST;
1702*4882a593Smuzhiyun 	}
1703*4882a593Smuzhiyun 
1704*4882a593Smuzhiyun 	/* Build the SRB */
1705*4882a593Smuzhiyun 	switch (scmnd->sc_data_direction) {
1706*4882a593Smuzhiyun 	case DMA_TO_DEVICE:
1707*4882a593Smuzhiyun 		vm_srb->data_in = WRITE_TYPE;
1708*4882a593Smuzhiyun 		vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT;
1709*4882a593Smuzhiyun 		break;
1710*4882a593Smuzhiyun 	case DMA_FROM_DEVICE:
1711*4882a593Smuzhiyun 		vm_srb->data_in = READ_TYPE;
1712*4882a593Smuzhiyun 		vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN;
1713*4882a593Smuzhiyun 		break;
1714*4882a593Smuzhiyun 	case DMA_NONE:
1715*4882a593Smuzhiyun 		vm_srb->data_in = UNKNOWN_TYPE;
1716*4882a593Smuzhiyun 		vm_srb->win8_extension.srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER;
1717*4882a593Smuzhiyun 		break;
1718*4882a593Smuzhiyun 	default:
1719*4882a593Smuzhiyun 		/*
1720*4882a593Smuzhiyun 		 * This is DMA_BIDIRECTIONAL or something else we are never
1721*4882a593Smuzhiyun 		 * supposed to see here.
1722*4882a593Smuzhiyun 		 */
1723*4882a593Smuzhiyun 		WARN(1, "Unexpected data direction: %d\n",
1724*4882a593Smuzhiyun 		     scmnd->sc_data_direction);
1725*4882a593Smuzhiyun 		return -EINVAL;
1726*4882a593Smuzhiyun 	}
1727*4882a593Smuzhiyun 
1728*4882a593Smuzhiyun 
1729*4882a593Smuzhiyun 	vm_srb->port_number = host_dev->port;
1730*4882a593Smuzhiyun 	vm_srb->path_id = scmnd->device->channel;
1731*4882a593Smuzhiyun 	vm_srb->target_id = scmnd->device->id;
1732*4882a593Smuzhiyun 	vm_srb->lun = scmnd->device->lun;
1733*4882a593Smuzhiyun 
1734*4882a593Smuzhiyun 	vm_srb->cdb_length = scmnd->cmd_len;
1735*4882a593Smuzhiyun 
1736*4882a593Smuzhiyun 	memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1737*4882a593Smuzhiyun 
1738*4882a593Smuzhiyun 	sgl = (struct scatterlist *)scsi_sglist(scmnd);
1739*4882a593Smuzhiyun 	sg_count = scsi_sg_count(scmnd);
1740*4882a593Smuzhiyun 
1741*4882a593Smuzhiyun 	length = scsi_bufflen(scmnd);
1742*4882a593Smuzhiyun 	payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
1743*4882a593Smuzhiyun 	payload_sz = sizeof(cmd_request->mpb);
1744*4882a593Smuzhiyun 
1745*4882a593Smuzhiyun 	if (sg_count) {
1746*4882a593Smuzhiyun 		unsigned int hvpgoff = 0;
1747*4882a593Smuzhiyun 		unsigned long offset_in_hvpg = sgl->offset & ~HV_HYP_PAGE_MASK;
1748*4882a593Smuzhiyun 		unsigned int hvpg_count = HVPFN_UP(offset_in_hvpg + length);
1749*4882a593Smuzhiyun 		u64 hvpfn;
1750*4882a593Smuzhiyun 
1751*4882a593Smuzhiyun 		if (hvpg_count > MAX_PAGE_BUFFER_COUNT) {
1752*4882a593Smuzhiyun 
1753*4882a593Smuzhiyun 			payload_sz = (hvpg_count * sizeof(u64) +
1754*4882a593Smuzhiyun 				      sizeof(struct vmbus_packet_mpb_array));
1755*4882a593Smuzhiyun 			payload = kzalloc(payload_sz, GFP_ATOMIC);
1756*4882a593Smuzhiyun 			if (!payload)
1757*4882a593Smuzhiyun 				return SCSI_MLQUEUE_DEVICE_BUSY;
1758*4882a593Smuzhiyun 		}
1759*4882a593Smuzhiyun 
1760*4882a593Smuzhiyun 		/*
1761*4882a593Smuzhiyun 		 * sgl is a list of PAGEs, and payload->range.pfn_array
1762*4882a593Smuzhiyun 		 * expects the page number in the unit of HV_HYP_PAGE_SIZE (the
1763*4882a593Smuzhiyun 		 * page size that Hyper-V uses, so here we need to divide PAGEs
1764*4882a593Smuzhiyun 		 * into HV_HYP_PAGE in case that PAGE_SIZE > HV_HYP_PAGE_SIZE.
1765*4882a593Smuzhiyun 		 * Besides, payload->range.offset should be the offset in one
1766*4882a593Smuzhiyun 		 * HV_HYP_PAGE.
1767*4882a593Smuzhiyun 		 */
1768*4882a593Smuzhiyun 		payload->range.len = length;
1769*4882a593Smuzhiyun 		payload->range.offset = offset_in_hvpg;
1770*4882a593Smuzhiyun 		hvpgoff = sgl->offset >> HV_HYP_PAGE_SHIFT;
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun 		cur_sgl = sgl;
1773*4882a593Smuzhiyun 		for (i = 0; i < hvpg_count; i++) {
1774*4882a593Smuzhiyun 			/*
1775*4882a593Smuzhiyun 			 * 'i' is the index of hv pages in the payload and
1776*4882a593Smuzhiyun 			 * 'hvpgoff' is the offset (in hv pages) of the first
1777*4882a593Smuzhiyun 			 * hv page in the the first page. The relationship
1778*4882a593Smuzhiyun 			 * between the sum of 'i' and 'hvpgoff' and the offset
1779*4882a593Smuzhiyun 			 * (in hv pages) in a payload page ('hvpgoff_in_page')
1780*4882a593Smuzhiyun 			 * is as follow:
1781*4882a593Smuzhiyun 			 *
1782*4882a593Smuzhiyun 			 * |------------------ PAGE -------------------|
1783*4882a593Smuzhiyun 			 * |   NR_HV_HYP_PAGES_IN_PAGE hvpgs in total  |
1784*4882a593Smuzhiyun 			 * |hvpg|hvpg| ...              |hvpg|... |hvpg|
1785*4882a593Smuzhiyun 			 * ^         ^                                 ^                 ^
1786*4882a593Smuzhiyun 			 * +-hvpgoff-+                                 +-hvpgoff_in_page-+
1787*4882a593Smuzhiyun 			 *           ^                                                   |
1788*4882a593Smuzhiyun 			 *           +--------------------- i ---------------------------+
1789*4882a593Smuzhiyun 			 */
1790*4882a593Smuzhiyun 			unsigned int hvpgoff_in_page =
1791*4882a593Smuzhiyun 				(i + hvpgoff) % NR_HV_HYP_PAGES_IN_PAGE;
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun 			/*
1794*4882a593Smuzhiyun 			 * Two cases that we need to fetch a page:
1795*4882a593Smuzhiyun 			 * 1) i == 0, the first step or
1796*4882a593Smuzhiyun 			 * 2) hvpgoff_in_page == 0, when we reach the boundary
1797*4882a593Smuzhiyun 			 *    of a page.
1798*4882a593Smuzhiyun 			 */
1799*4882a593Smuzhiyun 			if (hvpgoff_in_page == 0 || i == 0) {
1800*4882a593Smuzhiyun 				hvpfn = page_to_hvpfn(sg_page(cur_sgl));
1801*4882a593Smuzhiyun 				cur_sgl = sg_next(cur_sgl);
1802*4882a593Smuzhiyun 			}
1803*4882a593Smuzhiyun 
1804*4882a593Smuzhiyun 			payload->range.pfn_array[i] = hvpfn + hvpgoff_in_page;
1805*4882a593Smuzhiyun 		}
1806*4882a593Smuzhiyun 	}
1807*4882a593Smuzhiyun 
1808*4882a593Smuzhiyun 	cmd_request->payload = payload;
1809*4882a593Smuzhiyun 	cmd_request->payload_sz = payload_sz;
1810*4882a593Smuzhiyun 
1811*4882a593Smuzhiyun 	/* Invokes the vsc to start an IO */
1812*4882a593Smuzhiyun 	ret = storvsc_do_io(dev, cmd_request, get_cpu());
1813*4882a593Smuzhiyun 	put_cpu();
1814*4882a593Smuzhiyun 
1815*4882a593Smuzhiyun 	if (ret == -EAGAIN) {
1816*4882a593Smuzhiyun 		if (payload_sz > sizeof(cmd_request->mpb))
1817*4882a593Smuzhiyun 			kfree(payload);
1818*4882a593Smuzhiyun 		/* no more space */
1819*4882a593Smuzhiyun 		return SCSI_MLQUEUE_DEVICE_BUSY;
1820*4882a593Smuzhiyun 	}
1821*4882a593Smuzhiyun 
1822*4882a593Smuzhiyun 	return 0;
1823*4882a593Smuzhiyun }
1824*4882a593Smuzhiyun 
1825*4882a593Smuzhiyun static struct scsi_host_template scsi_driver = {
1826*4882a593Smuzhiyun 	.module	=		THIS_MODULE,
1827*4882a593Smuzhiyun 	.name =			"storvsc_host_t",
1828*4882a593Smuzhiyun 	.cmd_size =             sizeof(struct storvsc_cmd_request),
1829*4882a593Smuzhiyun 	.bios_param =		storvsc_get_chs,
1830*4882a593Smuzhiyun 	.queuecommand =		storvsc_queuecommand,
1831*4882a593Smuzhiyun 	.eh_host_reset_handler =	storvsc_host_reset_handler,
1832*4882a593Smuzhiyun 	.proc_name =		"storvsc_host",
1833*4882a593Smuzhiyun 	.eh_timed_out =		storvsc_eh_timed_out,
1834*4882a593Smuzhiyun 	.slave_alloc =		storvsc_device_alloc,
1835*4882a593Smuzhiyun 	.slave_configure =	storvsc_device_configure,
1836*4882a593Smuzhiyun 	.cmd_per_lun =		2048,
1837*4882a593Smuzhiyun 	.this_id =		-1,
1838*4882a593Smuzhiyun 	/* Make sure we dont get a sg segment crosses a page boundary */
1839*4882a593Smuzhiyun 	.dma_boundary =		PAGE_SIZE-1,
1840*4882a593Smuzhiyun 	/* Ensure there are no gaps in presented sgls */
1841*4882a593Smuzhiyun 	.virt_boundary_mask =	PAGE_SIZE-1,
1842*4882a593Smuzhiyun 	.no_write_same =	1,
1843*4882a593Smuzhiyun 	.track_queue_depth =	1,
1844*4882a593Smuzhiyun 	.change_queue_depth =	storvsc_change_queue_depth,
1845*4882a593Smuzhiyun };
1846*4882a593Smuzhiyun 
1847*4882a593Smuzhiyun enum {
1848*4882a593Smuzhiyun 	SCSI_GUID,
1849*4882a593Smuzhiyun 	IDE_GUID,
1850*4882a593Smuzhiyun 	SFC_GUID,
1851*4882a593Smuzhiyun };
1852*4882a593Smuzhiyun 
1853*4882a593Smuzhiyun static const struct hv_vmbus_device_id id_table[] = {
1854*4882a593Smuzhiyun 	/* SCSI guid */
1855*4882a593Smuzhiyun 	{ HV_SCSI_GUID,
1856*4882a593Smuzhiyun 	  .driver_data = SCSI_GUID
1857*4882a593Smuzhiyun 	},
1858*4882a593Smuzhiyun 	/* IDE guid */
1859*4882a593Smuzhiyun 	{ HV_IDE_GUID,
1860*4882a593Smuzhiyun 	  .driver_data = IDE_GUID
1861*4882a593Smuzhiyun 	},
1862*4882a593Smuzhiyun 	/* Fibre Channel GUID */
1863*4882a593Smuzhiyun 	{
1864*4882a593Smuzhiyun 	  HV_SYNTHFC_GUID,
1865*4882a593Smuzhiyun 	  .driver_data = SFC_GUID
1866*4882a593Smuzhiyun 	},
1867*4882a593Smuzhiyun 	{ },
1868*4882a593Smuzhiyun };
1869*4882a593Smuzhiyun 
1870*4882a593Smuzhiyun MODULE_DEVICE_TABLE(vmbus, id_table);
1871*4882a593Smuzhiyun 
1872*4882a593Smuzhiyun static const struct { guid_t guid; } fc_guid = { HV_SYNTHFC_GUID };
1873*4882a593Smuzhiyun 
hv_dev_is_fc(struct hv_device * hv_dev)1874*4882a593Smuzhiyun static bool hv_dev_is_fc(struct hv_device *hv_dev)
1875*4882a593Smuzhiyun {
1876*4882a593Smuzhiyun 	return guid_equal(&fc_guid.guid, &hv_dev->dev_type);
1877*4882a593Smuzhiyun }
1878*4882a593Smuzhiyun 
storvsc_probe(struct hv_device * device,const struct hv_vmbus_device_id * dev_id)1879*4882a593Smuzhiyun static int storvsc_probe(struct hv_device *device,
1880*4882a593Smuzhiyun 			const struct hv_vmbus_device_id *dev_id)
1881*4882a593Smuzhiyun {
1882*4882a593Smuzhiyun 	int ret;
1883*4882a593Smuzhiyun 	int num_cpus = num_online_cpus();
1884*4882a593Smuzhiyun 	struct Scsi_Host *host;
1885*4882a593Smuzhiyun 	struct hv_host_device *host_dev;
1886*4882a593Smuzhiyun 	bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
1887*4882a593Smuzhiyun 	bool is_fc = ((dev_id->driver_data == SFC_GUID) ? true : false);
1888*4882a593Smuzhiyun 	int target = 0;
1889*4882a593Smuzhiyun 	struct storvsc_device *stor_device;
1890*4882a593Smuzhiyun 	int max_luns_per_target;
1891*4882a593Smuzhiyun 	int max_targets;
1892*4882a593Smuzhiyun 	int max_channels;
1893*4882a593Smuzhiyun 	int max_sub_channels = 0;
1894*4882a593Smuzhiyun 
1895*4882a593Smuzhiyun 	/*
1896*4882a593Smuzhiyun 	 * Based on the windows host we are running on,
1897*4882a593Smuzhiyun 	 * set state to properly communicate with the host.
1898*4882a593Smuzhiyun 	 */
1899*4882a593Smuzhiyun 
1900*4882a593Smuzhiyun 	if (vmbus_proto_version < VERSION_WIN8) {
1901*4882a593Smuzhiyun 		max_luns_per_target = STORVSC_IDE_MAX_LUNS_PER_TARGET;
1902*4882a593Smuzhiyun 		max_targets = STORVSC_IDE_MAX_TARGETS;
1903*4882a593Smuzhiyun 		max_channels = STORVSC_IDE_MAX_CHANNELS;
1904*4882a593Smuzhiyun 	} else {
1905*4882a593Smuzhiyun 		max_luns_per_target = STORVSC_MAX_LUNS_PER_TARGET;
1906*4882a593Smuzhiyun 		max_targets = STORVSC_MAX_TARGETS;
1907*4882a593Smuzhiyun 		max_channels = STORVSC_MAX_CHANNELS;
1908*4882a593Smuzhiyun 		/*
1909*4882a593Smuzhiyun 		 * On Windows8 and above, we support sub-channels for storage
1910*4882a593Smuzhiyun 		 * on SCSI and FC controllers.
1911*4882a593Smuzhiyun 		 * The number of sub-channels offerred is based on the number of
1912*4882a593Smuzhiyun 		 * VCPUs in the guest.
1913*4882a593Smuzhiyun 		 */
1914*4882a593Smuzhiyun 		if (!dev_is_ide)
1915*4882a593Smuzhiyun 			max_sub_channels =
1916*4882a593Smuzhiyun 				(num_cpus - 1) / storvsc_vcpus_per_sub_channel;
1917*4882a593Smuzhiyun 	}
1918*4882a593Smuzhiyun 
1919*4882a593Smuzhiyun 	scsi_driver.can_queue = max_outstanding_req_per_channel *
1920*4882a593Smuzhiyun 				(max_sub_channels + 1) *
1921*4882a593Smuzhiyun 				(100 - ring_avail_percent_lowater) / 100;
1922*4882a593Smuzhiyun 
1923*4882a593Smuzhiyun 	host = scsi_host_alloc(&scsi_driver,
1924*4882a593Smuzhiyun 			       sizeof(struct hv_host_device));
1925*4882a593Smuzhiyun 	if (!host)
1926*4882a593Smuzhiyun 		return -ENOMEM;
1927*4882a593Smuzhiyun 
1928*4882a593Smuzhiyun 	host_dev = shost_priv(host);
1929*4882a593Smuzhiyun 	memset(host_dev, 0, sizeof(struct hv_host_device));
1930*4882a593Smuzhiyun 
1931*4882a593Smuzhiyun 	host_dev->port = host->host_no;
1932*4882a593Smuzhiyun 	host_dev->dev = device;
1933*4882a593Smuzhiyun 	host_dev->host = host;
1934*4882a593Smuzhiyun 
1935*4882a593Smuzhiyun 
1936*4882a593Smuzhiyun 	stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
1937*4882a593Smuzhiyun 	if (!stor_device) {
1938*4882a593Smuzhiyun 		ret = -ENOMEM;
1939*4882a593Smuzhiyun 		goto err_out0;
1940*4882a593Smuzhiyun 	}
1941*4882a593Smuzhiyun 
1942*4882a593Smuzhiyun 	stor_device->destroy = false;
1943*4882a593Smuzhiyun 	init_waitqueue_head(&stor_device->waiting_to_drain);
1944*4882a593Smuzhiyun 	stor_device->device = device;
1945*4882a593Smuzhiyun 	stor_device->host = host;
1946*4882a593Smuzhiyun 	spin_lock_init(&stor_device->lock);
1947*4882a593Smuzhiyun 	hv_set_drvdata(device, stor_device);
1948*4882a593Smuzhiyun 
1949*4882a593Smuzhiyun 	stor_device->port_number = host->host_no;
1950*4882a593Smuzhiyun 	ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size, is_fc);
1951*4882a593Smuzhiyun 	if (ret)
1952*4882a593Smuzhiyun 		goto err_out1;
1953*4882a593Smuzhiyun 
1954*4882a593Smuzhiyun 	host_dev->path = stor_device->path_id;
1955*4882a593Smuzhiyun 	host_dev->target = stor_device->target_id;
1956*4882a593Smuzhiyun 
1957*4882a593Smuzhiyun 	switch (dev_id->driver_data) {
1958*4882a593Smuzhiyun 	case SFC_GUID:
1959*4882a593Smuzhiyun 		host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
1960*4882a593Smuzhiyun 		host->max_id = STORVSC_FC_MAX_TARGETS;
1961*4882a593Smuzhiyun 		host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
1962*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1963*4882a593Smuzhiyun 		host->transportt = fc_transport_template;
1964*4882a593Smuzhiyun #endif
1965*4882a593Smuzhiyun 		break;
1966*4882a593Smuzhiyun 
1967*4882a593Smuzhiyun 	case SCSI_GUID:
1968*4882a593Smuzhiyun 		host->max_lun = max_luns_per_target;
1969*4882a593Smuzhiyun 		host->max_id = max_targets;
1970*4882a593Smuzhiyun 		host->max_channel = max_channels - 1;
1971*4882a593Smuzhiyun 		break;
1972*4882a593Smuzhiyun 
1973*4882a593Smuzhiyun 	default:
1974*4882a593Smuzhiyun 		host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
1975*4882a593Smuzhiyun 		host->max_id = STORVSC_IDE_MAX_TARGETS;
1976*4882a593Smuzhiyun 		host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
1977*4882a593Smuzhiyun 		break;
1978*4882a593Smuzhiyun 	}
1979*4882a593Smuzhiyun 	/* max cmd length */
1980*4882a593Smuzhiyun 	host->max_cmd_len = STORVSC_MAX_CMD_LEN;
1981*4882a593Smuzhiyun 
1982*4882a593Smuzhiyun 	/*
1983*4882a593Smuzhiyun 	 * set the table size based on the info we got
1984*4882a593Smuzhiyun 	 * from the host.
1985*4882a593Smuzhiyun 	 */
1986*4882a593Smuzhiyun 	host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
1987*4882a593Smuzhiyun 	/*
1988*4882a593Smuzhiyun 	 * For non-IDE disks, the host supports multiple channels.
1989*4882a593Smuzhiyun 	 * Set the number of HW queues we are supporting.
1990*4882a593Smuzhiyun 	 */
1991*4882a593Smuzhiyun 	if (!dev_is_ide)
1992*4882a593Smuzhiyun 		host->nr_hw_queues = num_present_cpus();
1993*4882a593Smuzhiyun 
1994*4882a593Smuzhiyun 	/*
1995*4882a593Smuzhiyun 	 * Set the error handler work queue.
1996*4882a593Smuzhiyun 	 */
1997*4882a593Smuzhiyun 	host_dev->handle_error_wq =
1998*4882a593Smuzhiyun 			alloc_ordered_workqueue("storvsc_error_wq_%d",
1999*4882a593Smuzhiyun 						0,
2000*4882a593Smuzhiyun 						host->host_no);
2001*4882a593Smuzhiyun 	if (!host_dev->handle_error_wq) {
2002*4882a593Smuzhiyun 		ret = -ENOMEM;
2003*4882a593Smuzhiyun 		goto err_out2;
2004*4882a593Smuzhiyun 	}
2005*4882a593Smuzhiyun 	INIT_WORK(&host_dev->host_scan_work, storvsc_host_scan);
2006*4882a593Smuzhiyun 	/* Register the HBA and start the scsi bus scan */
2007*4882a593Smuzhiyun 	ret = scsi_add_host(host, &device->device);
2008*4882a593Smuzhiyun 	if (ret != 0)
2009*4882a593Smuzhiyun 		goto err_out3;
2010*4882a593Smuzhiyun 
2011*4882a593Smuzhiyun 	if (!dev_is_ide) {
2012*4882a593Smuzhiyun 		scsi_scan_host(host);
2013*4882a593Smuzhiyun 	} else {
2014*4882a593Smuzhiyun 		target = (device->dev_instance.b[5] << 8 |
2015*4882a593Smuzhiyun 			 device->dev_instance.b[4]);
2016*4882a593Smuzhiyun 		ret = scsi_add_device(host, 0, target, 0);
2017*4882a593Smuzhiyun 		if (ret)
2018*4882a593Smuzhiyun 			goto err_out4;
2019*4882a593Smuzhiyun 	}
2020*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2021*4882a593Smuzhiyun 	if (host->transportt == fc_transport_template) {
2022*4882a593Smuzhiyun 		struct fc_rport_identifiers ids = {
2023*4882a593Smuzhiyun 			.roles = FC_PORT_ROLE_FCP_DUMMY_INITIATOR,
2024*4882a593Smuzhiyun 		};
2025*4882a593Smuzhiyun 
2026*4882a593Smuzhiyun 		fc_host_node_name(host) = stor_device->node_name;
2027*4882a593Smuzhiyun 		fc_host_port_name(host) = stor_device->port_name;
2028*4882a593Smuzhiyun 		stor_device->rport = fc_remote_port_add(host, 0, &ids);
2029*4882a593Smuzhiyun 		if (!stor_device->rport) {
2030*4882a593Smuzhiyun 			ret = -ENOMEM;
2031*4882a593Smuzhiyun 			goto err_out4;
2032*4882a593Smuzhiyun 		}
2033*4882a593Smuzhiyun 	}
2034*4882a593Smuzhiyun #endif
2035*4882a593Smuzhiyun 	return 0;
2036*4882a593Smuzhiyun 
2037*4882a593Smuzhiyun err_out4:
2038*4882a593Smuzhiyun 	scsi_remove_host(host);
2039*4882a593Smuzhiyun 
2040*4882a593Smuzhiyun err_out3:
2041*4882a593Smuzhiyun 	destroy_workqueue(host_dev->handle_error_wq);
2042*4882a593Smuzhiyun 
2043*4882a593Smuzhiyun err_out2:
2044*4882a593Smuzhiyun 	/*
2045*4882a593Smuzhiyun 	 * Once we have connected with the host, we would need to
2046*4882a593Smuzhiyun 	 * to invoke storvsc_dev_remove() to rollback this state and
2047*4882a593Smuzhiyun 	 * this call also frees up the stor_device; hence the jump around
2048*4882a593Smuzhiyun 	 * err_out1 label.
2049*4882a593Smuzhiyun 	 */
2050*4882a593Smuzhiyun 	storvsc_dev_remove(device);
2051*4882a593Smuzhiyun 	goto err_out0;
2052*4882a593Smuzhiyun 
2053*4882a593Smuzhiyun err_out1:
2054*4882a593Smuzhiyun 	kfree(stor_device->stor_chns);
2055*4882a593Smuzhiyun 	kfree(stor_device);
2056*4882a593Smuzhiyun 
2057*4882a593Smuzhiyun err_out0:
2058*4882a593Smuzhiyun 	scsi_host_put(host);
2059*4882a593Smuzhiyun 	return ret;
2060*4882a593Smuzhiyun }
2061*4882a593Smuzhiyun 
2062*4882a593Smuzhiyun /* Change a scsi target's queue depth */
storvsc_change_queue_depth(struct scsi_device * sdev,int queue_depth)2063*4882a593Smuzhiyun static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth)
2064*4882a593Smuzhiyun {
2065*4882a593Smuzhiyun 	if (queue_depth > scsi_driver.can_queue)
2066*4882a593Smuzhiyun 		queue_depth = scsi_driver.can_queue;
2067*4882a593Smuzhiyun 
2068*4882a593Smuzhiyun 	return scsi_change_queue_depth(sdev, queue_depth);
2069*4882a593Smuzhiyun }
2070*4882a593Smuzhiyun 
storvsc_remove(struct hv_device * dev)2071*4882a593Smuzhiyun static int storvsc_remove(struct hv_device *dev)
2072*4882a593Smuzhiyun {
2073*4882a593Smuzhiyun 	struct storvsc_device *stor_device = hv_get_drvdata(dev);
2074*4882a593Smuzhiyun 	struct Scsi_Host *host = stor_device->host;
2075*4882a593Smuzhiyun 	struct hv_host_device *host_dev = shost_priv(host);
2076*4882a593Smuzhiyun 
2077*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2078*4882a593Smuzhiyun 	if (host->transportt == fc_transport_template) {
2079*4882a593Smuzhiyun 		fc_remote_port_delete(stor_device->rport);
2080*4882a593Smuzhiyun 		fc_remove_host(host);
2081*4882a593Smuzhiyun 	}
2082*4882a593Smuzhiyun #endif
2083*4882a593Smuzhiyun 	destroy_workqueue(host_dev->handle_error_wq);
2084*4882a593Smuzhiyun 	scsi_remove_host(host);
2085*4882a593Smuzhiyun 	storvsc_dev_remove(dev);
2086*4882a593Smuzhiyun 	scsi_host_put(host);
2087*4882a593Smuzhiyun 
2088*4882a593Smuzhiyun 	return 0;
2089*4882a593Smuzhiyun }
2090*4882a593Smuzhiyun 
storvsc_suspend(struct hv_device * hv_dev)2091*4882a593Smuzhiyun static int storvsc_suspend(struct hv_device *hv_dev)
2092*4882a593Smuzhiyun {
2093*4882a593Smuzhiyun 	struct storvsc_device *stor_device = hv_get_drvdata(hv_dev);
2094*4882a593Smuzhiyun 	struct Scsi_Host *host = stor_device->host;
2095*4882a593Smuzhiyun 	struct hv_host_device *host_dev = shost_priv(host);
2096*4882a593Smuzhiyun 
2097*4882a593Smuzhiyun 	storvsc_wait_to_drain(stor_device);
2098*4882a593Smuzhiyun 
2099*4882a593Smuzhiyun 	drain_workqueue(host_dev->handle_error_wq);
2100*4882a593Smuzhiyun 
2101*4882a593Smuzhiyun 	vmbus_close(hv_dev->channel);
2102*4882a593Smuzhiyun 
2103*4882a593Smuzhiyun 	kfree(stor_device->stor_chns);
2104*4882a593Smuzhiyun 	stor_device->stor_chns = NULL;
2105*4882a593Smuzhiyun 
2106*4882a593Smuzhiyun 	cpumask_clear(&stor_device->alloced_cpus);
2107*4882a593Smuzhiyun 
2108*4882a593Smuzhiyun 	return 0;
2109*4882a593Smuzhiyun }
2110*4882a593Smuzhiyun 
storvsc_resume(struct hv_device * hv_dev)2111*4882a593Smuzhiyun static int storvsc_resume(struct hv_device *hv_dev)
2112*4882a593Smuzhiyun {
2113*4882a593Smuzhiyun 	int ret;
2114*4882a593Smuzhiyun 
2115*4882a593Smuzhiyun 	ret = storvsc_connect_to_vsp(hv_dev, storvsc_ringbuffer_size,
2116*4882a593Smuzhiyun 				     hv_dev_is_fc(hv_dev));
2117*4882a593Smuzhiyun 	return ret;
2118*4882a593Smuzhiyun }
2119*4882a593Smuzhiyun 
2120*4882a593Smuzhiyun static struct hv_driver storvsc_drv = {
2121*4882a593Smuzhiyun 	.name = KBUILD_MODNAME,
2122*4882a593Smuzhiyun 	.id_table = id_table,
2123*4882a593Smuzhiyun 	.probe = storvsc_probe,
2124*4882a593Smuzhiyun 	.remove = storvsc_remove,
2125*4882a593Smuzhiyun 	.suspend = storvsc_suspend,
2126*4882a593Smuzhiyun 	.resume = storvsc_resume,
2127*4882a593Smuzhiyun 	.driver = {
2128*4882a593Smuzhiyun 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
2129*4882a593Smuzhiyun 	},
2130*4882a593Smuzhiyun };
2131*4882a593Smuzhiyun 
2132*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2133*4882a593Smuzhiyun static struct fc_function_template fc_transport_functions = {
2134*4882a593Smuzhiyun 	.show_host_node_name = 1,
2135*4882a593Smuzhiyun 	.show_host_port_name = 1,
2136*4882a593Smuzhiyun };
2137*4882a593Smuzhiyun #endif
2138*4882a593Smuzhiyun 
storvsc_drv_init(void)2139*4882a593Smuzhiyun static int __init storvsc_drv_init(void)
2140*4882a593Smuzhiyun {
2141*4882a593Smuzhiyun 	int ret;
2142*4882a593Smuzhiyun 
2143*4882a593Smuzhiyun 	/*
2144*4882a593Smuzhiyun 	 * Divide the ring buffer data size (which is 1 page less
2145*4882a593Smuzhiyun 	 * than the ring buffer size since that page is reserved for
2146*4882a593Smuzhiyun 	 * the ring buffer indices) by the max request size (which is
2147*4882a593Smuzhiyun 	 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
2148*4882a593Smuzhiyun 	 */
2149*4882a593Smuzhiyun 	max_outstanding_req_per_channel =
2150*4882a593Smuzhiyun 		((storvsc_ringbuffer_size - PAGE_SIZE) /
2151*4882a593Smuzhiyun 		ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
2152*4882a593Smuzhiyun 		sizeof(struct vstor_packet) + sizeof(u64) -
2153*4882a593Smuzhiyun 		vmscsi_size_delta,
2154*4882a593Smuzhiyun 		sizeof(u64)));
2155*4882a593Smuzhiyun 
2156*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2157*4882a593Smuzhiyun 	fc_transport_template = fc_attach_transport(&fc_transport_functions);
2158*4882a593Smuzhiyun 	if (!fc_transport_template)
2159*4882a593Smuzhiyun 		return -ENODEV;
2160*4882a593Smuzhiyun #endif
2161*4882a593Smuzhiyun 
2162*4882a593Smuzhiyun 	ret = vmbus_driver_register(&storvsc_drv);
2163*4882a593Smuzhiyun 
2164*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2165*4882a593Smuzhiyun 	if (ret)
2166*4882a593Smuzhiyun 		fc_release_transport(fc_transport_template);
2167*4882a593Smuzhiyun #endif
2168*4882a593Smuzhiyun 
2169*4882a593Smuzhiyun 	return ret;
2170*4882a593Smuzhiyun }
2171*4882a593Smuzhiyun 
storvsc_drv_exit(void)2172*4882a593Smuzhiyun static void __exit storvsc_drv_exit(void)
2173*4882a593Smuzhiyun {
2174*4882a593Smuzhiyun 	vmbus_driver_unregister(&storvsc_drv);
2175*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2176*4882a593Smuzhiyun 	fc_release_transport(fc_transport_template);
2177*4882a593Smuzhiyun #endif
2178*4882a593Smuzhiyun }
2179*4882a593Smuzhiyun 
2180*4882a593Smuzhiyun MODULE_LICENSE("GPL");
2181*4882a593Smuzhiyun MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
2182*4882a593Smuzhiyun module_init(storvsc_drv_init);
2183*4882a593Smuzhiyun module_exit(storvsc_drv_exit);
2184