blob: 9a0bba5a51a717d2aef47f3a737f56fcc59db08a [file] [log] [blame]
Thomas Gleixner3b20eb22019-05-29 16:57:35 -07001// SPDX-License-Identifier: GPL-2.0-only
Hank Janssenbef4a342009-07-13 16:01:31 -07002/*
Hank Janssenbef4a342009-07-13 16:01:31 -07003 * Copyright (c) 2009, Microsoft Corporation.
4 *
Hank Janssenbef4a342009-07-13 16:01:31 -07005 * Authors:
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
K. Y. Srinivasan972621c2011-05-10 07:54:19 -07008 * K. Y. Srinivasan <kys@microsoft.com>
Hank Janssenbef4a342009-07-13 16:01:31 -07009 */
K. Y. Srinivasana1be1702011-08-27 11:31:23 -070010
11#include <linux/kernel.h>
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070012#include <linux/wait.h>
K. Y. Srinivasana1be1702011-08-27 11:31:23 -070013#include <linux/sched.h>
14#include <linux/completion.h>
15#include <linux/string.h>
16#include <linux/mm.h>
17#include <linux/delay.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070018#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070020#include <linux/module.h>
21#include <linux/device.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070022#include <linux/hyperv.h>
K. Y. Srinivasan56b26e62014-07-12 09:48:30 -070023#include <linux/blkdev.h>
Tianyu Lan743b2372021-12-13 02:14:05 -050024#include <linux/dma-mapping.h>
25
Hank Janssenbef4a342009-07-13 16:01:31 -070026#include <scsi/scsi.h>
27#include <scsi/scsi_cmnd.h>
28#include <scsi/scsi_host.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_tcq.h>
31#include <scsi/scsi_eh.h>
32#include <scsi/scsi_devinfo.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070033#include <scsi/scsi_dbg.h>
K. Y. Srinivasandac58242015-12-23 13:15:49 -080034#include <scsi/scsi_transport_fc.h>
K. Y. Srinivasand791a8c2016-01-27 18:22:44 -080035#include <scsi/scsi_transport.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070036
K. Y. Srinivasanc6491142012-01-12 12:38:03 -080037/*
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -080038 * All wire protocol details (storage protocol between the guest and the host)
39 * are consolidated here.
40 *
41 * Begin protocol definitions.
K. Y. Srinivasanc6491142012-01-12 12:38:03 -080042 */
43
K. Y. Srinivasan09f03552012-01-12 12:37:54 -080044/*
45 * Version history:
46 * V1 Beta: 0.1
47 * V1 RC < 2008/1/31: 1.0
48 * V1 RC > 2008/1/31: 2.0
49 * Win7: 4.2
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -070050 * Win8: 5.1
Keith Mange1a363102015-08-13 08:43:48 -070051 * Win8.1: 6.0
52 * Win10: 6.2
K. Y. Srinivasan09f03552012-01-12 12:37:54 -080053 */
54
Keith Mange2492fd72015-08-13 08:43:47 -070055#define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
56 (((MINOR_) & 0xff)))
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -070057
Keith Mange1a363102015-08-13 08:43:48 -070058#define VMSTOR_PROTO_VERSION_WIN6 VMSTOR_PROTO_VERSION(2, 0)
Keith Mange2492fd72015-08-13 08:43:47 -070059#define VMSTOR_PROTO_VERSION_WIN7 VMSTOR_PROTO_VERSION(4, 2)
60#define VMSTOR_PROTO_VERSION_WIN8 VMSTOR_PROTO_VERSION(5, 1)
Keith Mange1a363102015-08-13 08:43:48 -070061#define VMSTOR_PROTO_VERSION_WIN8_1 VMSTOR_PROTO_VERSION(6, 0)
62#define VMSTOR_PROTO_VERSION_WIN10 VMSTOR_PROTO_VERSION(6, 2)
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070063
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070064/* Packet structure describing virtual storage requests. */
65enum vstor_packet_operation {
66 VSTOR_OPERATION_COMPLETE_IO = 1,
67 VSTOR_OPERATION_REMOVE_DEVICE = 2,
68 VSTOR_OPERATION_EXECUTE_SRB = 3,
69 VSTOR_OPERATION_RESET_LUN = 4,
70 VSTOR_OPERATION_RESET_ADAPTER = 5,
71 VSTOR_OPERATION_RESET_BUS = 6,
72 VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
73 VSTOR_OPERATION_END_INITIALIZATION = 8,
74 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
75 VSTOR_OPERATION_QUERY_PROPERTIES = 10,
K. Y. Srinivasan2b9525f2011-11-08 09:01:48 -080076 VSTOR_OPERATION_ENUMERATE_BUS = 11,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -070077 VSTOR_OPERATION_FCHBA_DATA = 12,
78 VSTOR_OPERATION_CREATE_SUB_CHANNELS = 13,
79 VSTOR_OPERATION_MAXIMUM = 13
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070080};
81
82/*
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -070083 * WWN packet for Fibre Channel HBA
84 */
85
86struct hv_fc_wwn_packet {
K. Y. Srinivasan83d1e8b2015-12-23 13:15:48 -080087 u8 primary_active;
88 u8 reserved1[3];
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -070089 u8 primary_port_wwn[8];
90 u8 primary_node_wwn[8];
91 u8 secondary_port_wwn[8];
92 u8 secondary_node_wwn[8];
93};
94
95
96
97/*
98 * SRB Flag Bits
99 */
100
101#define SRB_FLAGS_QUEUE_ACTION_ENABLE 0x00000002
102#define SRB_FLAGS_DISABLE_DISCONNECT 0x00000004
103#define SRB_FLAGS_DISABLE_SYNCH_TRANSFER 0x00000008
104#define SRB_FLAGS_BYPASS_FROZEN_QUEUE 0x00000010
105#define SRB_FLAGS_DISABLE_AUTOSENSE 0x00000020
106#define SRB_FLAGS_DATA_IN 0x00000040
107#define SRB_FLAGS_DATA_OUT 0x00000080
108#define SRB_FLAGS_NO_DATA_TRANSFER 0x00000000
109#define SRB_FLAGS_UNSPECIFIED_DIRECTION (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT)
110#define SRB_FLAGS_NO_QUEUE_FREEZE 0x00000100
111#define SRB_FLAGS_ADAPTER_CACHE_ENABLE 0x00000200
112#define SRB_FLAGS_FREE_SENSE_BUFFER 0x00000400
113
114/*
115 * This flag indicates the request is part of the workflow for processing a D3.
116 */
117#define SRB_FLAGS_D3_PROCESSING 0x00000800
118#define SRB_FLAGS_IS_ACTIVE 0x00010000
119#define SRB_FLAGS_ALLOCATED_FROM_ZONE 0x00020000
120#define SRB_FLAGS_SGLIST_FROM_POOL 0x00040000
121#define SRB_FLAGS_BYPASS_LOCKED_QUEUE 0x00080000
122#define SRB_FLAGS_NO_KEEP_AWAKE 0x00100000
123#define SRB_FLAGS_PORT_DRIVER_ALLOCSENSE 0x00200000
124#define SRB_FLAGS_PORT_DRIVER_SENSEHASPORT 0x00400000
125#define SRB_FLAGS_DONT_START_NEXT_PACKET 0x00800000
126#define SRB_FLAGS_PORT_DRIVER_RESERVED 0x0F000000
127#define SRB_FLAGS_CLASS_DRIVER_RESERVED 0xF0000000
128
Long Li3cd6d3d2016-12-14 18:46:01 -0800129#define SP_UNTAGGED ((unsigned char) ~0)
130#define SRB_SIMPLE_TAG_REQUEST 0x20
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700131
132/*
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700133 * Platform neutral description of a scsi request -
134 * this remains the same across the write regardless of 32/64 bit
135 * note: it's patterned off the SCSI_PASS_THROUGH structure
136 */
K. Y. Srinivasan6b2f9492012-01-12 12:38:05 -0800137#define STORVSC_MAX_CMD_LEN 0x10
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700138
139#define POST_WIN7_STORVSC_SENSE_BUFFER_SIZE 0x14
140#define PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE 0x12
141
142#define STORVSC_SENSE_BUFFER_SIZE 0x14
K. Y. Srinivasan6b2f9492012-01-12 12:38:05 -0800143#define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700144
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700145/*
146 * Sense buffer size changed in win8; have a run-time
Keith Mangecb11fead2015-08-13 08:43:49 -0700147 * variable to track the size we should use. This value will
148 * likely change during protocol negotiation but it is valid
149 * to start by assuming pre-Win8.
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700150 */
Keith Mangecb11fead2015-08-13 08:43:49 -0700151static int sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700152
153/*
Keith Mangecb11fead2015-08-13 08:43:49 -0700154 * The storage protocol version is determined during the
155 * initial exchange with the host. It will indicate which
156 * storage functionality is available in the host.
157*/
Keith Mange2492fd72015-08-13 08:43:47 -0700158static int vmstor_proto_version;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700159
Long Lif8aea702015-12-04 00:07:24 -0800160#define STORVSC_LOGGING_NONE 0
161#define STORVSC_LOGGING_ERROR 1
162#define STORVSC_LOGGING_WARN 2
163
164static int logging_level = STORVSC_LOGGING_ERROR;
165module_param(logging_level, int, S_IRUGO|S_IWUSR);
166MODULE_PARM_DESC(logging_level,
167 "Logging level, 0 - None, 1 - Error (default), 2 - Warning.");
168
169static inline bool do_logging(int level)
170{
171 return logging_level >= level;
172}
173
174#define storvsc_log(dev, level, fmt, ...) \
175do { \
176 if (do_logging(level)) \
177 dev_warn(&(dev)->device, fmt, ##__VA_ARGS__); \
178} while (0)
179
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700180struct vmscsi_win8_extension {
181 /*
182 * The following were added in Windows 8
183 */
184 u16 reserve;
185 u8 queue_tag;
186 u8 queue_action;
187 u32 srb_flags;
188 u32 time_out_value;
189 u32 queue_sort_ey;
190} __packed;
191
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700192struct vmscsi_request {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800193 u16 length;
194 u8 srb_status;
195 u8 scsi_status;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700196
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800197 u8 port_number;
198 u8 path_id;
199 u8 target_id;
200 u8 lun;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700201
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800202 u8 cdb_length;
203 u8 sense_info_length;
204 u8 data_in;
205 u8 reserved;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700206
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800207 u32 data_transfer_length;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700208
209 union {
K. Y. Srinivasan6b2f9492012-01-12 12:38:05 -0800210 u8 cdb[STORVSC_MAX_CMD_LEN];
211 u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
212 u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700213 };
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700214 /*
215 * The following was added in win8.
216 */
217 struct vmscsi_win8_extension win8_extension;
218
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700219} __attribute((packed));
220
Keith Mangecb11fead2015-08-13 08:43:49 -0700221/*
Keith Mange1a363102015-08-13 08:43:48 -0700222 * The list of storage protocols in order of preference.
223 */
224struct vmstor_protocol {
225 int protocol_version;
226 int sense_buffer_size;
227 int vmscsi_size_delta;
228};
229
230
231static const struct vmstor_protocol vmstor_protocols[] = {
232 {
233 VMSTOR_PROTO_VERSION_WIN10,
234 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
235 0
236 },
237 {
238 VMSTOR_PROTO_VERSION_WIN8_1,
239 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
240 0
241 },
242 {
243 VMSTOR_PROTO_VERSION_WIN8,
244 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
245 0
246 },
247 {
248 VMSTOR_PROTO_VERSION_WIN7,
249 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
250 sizeof(struct vmscsi_win8_extension),
251 },
252 {
253 VMSTOR_PROTO_VERSION_WIN6,
254 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
255 sizeof(struct vmscsi_win8_extension),
256 }
257};
258
259
260/*
Masahiro Yamada183b8022017-02-27 14:29:20 -0800261 * This structure is sent during the initialization phase to get the different
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700262 * properties of the channel.
263 */
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700264
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700265#define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1
266
267struct vmstorage_channel_properties {
268 u32 reserved;
269 u16 max_channel_cnt;
270 u16 reserved1;
271
272 u32 flags;
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800273 u32 max_transfer_bytes;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700274
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700275 u64 reserved2;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700276} __packed;
277
278/* This structure is sent during the storage protocol negotiations. */
279struct vmstorage_protocol_version {
280 /* Major (MSW) and minor (LSW) version numbers. */
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800281 u16 major_minor;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700282
283 /*
284 * Revision number is auto-incremented whenever this file is changed
285 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
286 * definitely indicate incompatibility--but it does indicate mismatched
287 * builds.
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800288 * This is only used on the windows side. Just set it to 0.
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700289 */
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800290 u16 revision;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700291} __packed;
292
293/* Channel Property Flags */
294#define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
295#define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
296
297struct vstor_packet {
298 /* Requested operation type */
299 enum vstor_packet_operation operation;
300
301 /* Flags - see below for values */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800302 u32 flags;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700303
304 /* Status of the request returned from the server side. */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800305 u32 status;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700306
307 /* Data payload area */
308 union {
309 /*
310 * Structure used to forward SCSI commands from the
311 * client to the server.
312 */
313 struct vmscsi_request vm_srb;
314
315 /* Structure used to query channel properties. */
316 struct vmstorage_channel_properties storage_channel_properties;
317
318 /* Used during version negotiations. */
319 struct vmstorage_protocol_version version;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700320
321 /* Fibre channel address packet */
322 struct hv_fc_wwn_packet wwn_packet;
323
324 /* Number of sub-channels to create */
325 u16 sub_channel_count;
326
327 /* This will be the maximum of the union members */
328 u8 buffer[0x34];
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700329 };
330} __packed;
331
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700332/*
K. Y. Srinivasan09f03552012-01-12 12:37:54 -0800333 * Packet Flags:
334 *
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700335 * This flag indicates that the server should send back a completion for this
336 * packet.
337 */
K. Y. Srinivasan09f03552012-01-12 12:37:54 -0800338
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700339#define REQUEST_COMPLETION_FLAG 0x1
340
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700341/* Matches Windows-end */
342enum storvsc_request_type {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800343 WRITE_TYPE = 0,
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700344 READ_TYPE,
345 UNKNOWN_TYPE,
346};
347
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800348/*
349 * SRB status codes and masks; a subset of the codes used here.
350 */
351
352#define SRB_STATUS_AUTOSENSE_VALID 0x80
K. Y. Srinivasan3209f9d2015-10-06 18:04:01 -0700353#define SRB_STATUS_QUEUE_FROZEN 0x40
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800354#define SRB_STATUS_INVALID_LUN 0x20
355#define SRB_STATUS_SUCCESS 0x01
K. Y. Srinivasan67812092013-02-21 12:04:53 -0800356#define SRB_STATUS_ABORTED 0x02
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800357#define SRB_STATUS_ERROR 0x04
Long Li40630f42016-12-14 18:46:03 -0800358#define SRB_STATUS_DATA_OVERRUN 0x12
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800359
K. Y. Srinivasan3209f9d2015-10-06 18:04:01 -0700360#define SRB_STATUS(status) \
361 (status & ~(SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_QUEUE_FROZEN))
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800362/*
363 * This is the end of Protocol specific defines.
364 */
365
Michael Kelleyed2e63a2019-04-01 21:42:06 +0000366static int storvsc_ringbuffer_size = (128 * 1024);
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -0700367static u32 max_outstanding_req_per_channel;
Branden Bonabyadfbd022019-06-14 19:48:22 -0400368static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth);
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -0700369
370static int storvsc_vcpus_per_sub_channel = 4;
Melanie Plageman (Microsoft)a81a38c2021-02-24 23:29:48 +0000371static unsigned int storvsc_max_hw_queues;
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800372
373module_param(storvsc_ringbuffer_size, int, S_IRUGO);
374MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
375
Melanie Plageman (Microsoft)a81a38c2021-02-24 23:29:48 +0000376module_param(storvsc_max_hw_queues, uint, 0644);
377MODULE_PARM_DESC(storvsc_max_hw_queues, "Maximum number of hardware queues");
378
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -0700379module_param(storvsc_vcpus_per_sub_channel, int, S_IRUGO);
Dan Carpenter74e26782016-01-08 14:02:04 +0300380MODULE_PARM_DESC(storvsc_vcpus_per_sub_channel, "Ratio of VCPUs to subchannels");
Long Li2217a472018-04-19 14:54:24 -0700381
382static int ring_avail_percent_lowater = 10;
383module_param(ring_avail_percent_lowater, int, S_IRUGO);
384MODULE_PARM_DESC(ring_avail_percent_lowater,
385 "Select a channel if available ring size > this in percent");
386
K. Y. Srinivasan893def32013-06-04 12:05:05 -0700387/*
388 * Timeout in seconds for all devices managed by this driver.
389 */
390static int storvsc_timeout = 180;
391
K. Y. Srinivasandac58242015-12-23 13:15:49 -0800392#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
393static struct scsi_transport_template *fc_transport_template;
394#endif
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800395
Andres Beltran453de212020-11-09 11:04:01 +0100396static struct scsi_host_template scsi_driver;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700397static void storvsc_on_channel_callback(void *context);
398
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -0700399#define STORVSC_MAX_LUNS_PER_TARGET 255
400#define STORVSC_MAX_TARGETS 2
401#define STORVSC_MAX_CHANNELS 8
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800402
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -0700403#define STORVSC_FC_MAX_LUNS_PER_TARGET 255
404#define STORVSC_FC_MAX_TARGETS 128
405#define STORVSC_FC_MAX_CHANNELS 8
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800406
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -0700407#define STORVSC_IDE_MAX_LUNS_PER_TARGET 64
408#define STORVSC_IDE_MAX_TARGETS 1
409#define STORVSC_IDE_MAX_CHANNELS 1
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800410
Andres Beltranadae1e92021-04-08 18:14:39 +0200411/*
412 * Upper bound on the size of a storvsc packet. vmscsi_size_delta is not
413 * included in the calculation because it is set after STORVSC_MAX_PKT_SIZE
414 * is used in storvsc_connect_to_vsp
415 */
416#define STORVSC_MAX_PKT_SIZE (sizeof(struct vmpacket_descriptor) +\
417 sizeof(struct vstor_packet))
418
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800419struct storvsc_cmd_request {
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800420 struct scsi_cmnd *cmd;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700421
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700422 struct hv_device *device;
423
424 /* Synchronize the request/response if needed */
425 struct completion wait_event;
426
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -0700427 struct vmbus_channel_packet_multipage_buffer mpb;
428 struct vmbus_packet_mpb_array *payload;
429 u32 payload_sz;
430
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700431 struct vstor_packet vstor_packet;
432};
433
434
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700435/* A storvsc device is a device object that contains a vmbus channel */
436struct storvsc_device {
437 struct hv_device *device;
438
439 bool destroy;
440 bool drain_notify;
441 atomic_t num_outstanding_req;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700442 struct Scsi_Host *host;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700443
444 wait_queue_head_t waiting_to_drain;
445
446 /*
447 * Each unique Port/Path/Target represents 1 channel ie scsi
448 * controller. In reality, the pathid, targetid is always 0
449 * and the port is set by us
450 */
451 unsigned int port_number;
452 unsigned char path_id;
453 unsigned char target_id;
454
K. Y. Srinivasan5117b932015-03-27 00:27:17 -0700455 /*
Andrea Parri (Microsoft)244808e2020-12-17 21:33:20 +0100456 * The size of the vmscsi_request has changed in win8. The
457 * additional size is because of new elements added to the
458 * structure. These elements are valid only when we are talking
459 * to a win8 host.
460 * Track the correction to size we need to apply. This value
461 * will likely change during protocol negotiation but it is
462 * valid to start by assuming pre-Win8.
463 */
464 int vmscsi_size_delta;
465
466 /*
K. Y. Srinivasan5117b932015-03-27 00:27:17 -0700467 * Max I/O, the device can support.
468 */
469 u32 max_transfer_bytes;
K. Y. Srinivasand86adf42016-12-14 18:46:00 -0800470 /*
471 * Number of sub-channels we will open.
472 */
473 u16 num_sc;
474 struct vmbus_channel **stor_chns;
475 /*
476 * Mask of CPUs bound to subchannels.
477 */
478 struct cpumask alloced_cpus;
Andrea Parri (Microsoft)21d20522020-06-17 18:46:41 +0200479 /*
480 * Serializes modifications of stor_chns[] from storvsc_do_io()
481 * and storvsc_change_target_cpu().
482 */
483 spinlock_t lock;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700484 /* Used for vsc/vsp channel reset process */
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800485 struct storvsc_cmd_request init_request;
486 struct storvsc_cmd_request reset_request;
K. Y. Srinivasandac58242015-12-23 13:15:49 -0800487 /*
488 * Currently active port and node names for FC devices.
489 */
490 u64 node_name;
491 u64 port_name;
Cathy Averydaf0cd42017-04-17 14:37:46 -0400492#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
493 struct fc_rport *rport;
494#endif
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700495};
496
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800497struct hv_host_device {
498 struct hv_device *dev;
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700499 unsigned int port;
500 unsigned char path;
501 unsigned char target;
Cathy Avery436ad942017-10-31 08:52:06 -0400502 struct workqueue_struct *handle_error_wq;
Long Lic58cc702017-10-31 14:58:08 -0700503 struct work_struct host_scan_work;
504 struct Scsi_Host *host;
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700505};
506
K. Y. Srinivasan126757992011-11-08 09:01:49 -0800507struct storvsc_scan_work {
508 struct work_struct work;
509 struct Scsi_Host *host;
K. Y. Srinivasan98441222016-01-27 18:22:45 -0800510 u8 lun;
511 u8 tgt_id;
K. Y. Srinivasan126757992011-11-08 09:01:49 -0800512};
513
K. Y. Srinivasan67812092013-02-21 12:04:53 -0800514static void storvsc_device_scan(struct work_struct *work)
515{
516 struct storvsc_scan_work *wrk;
K. Y. Srinivasan67812092013-02-21 12:04:53 -0800517 struct scsi_device *sdev;
518
519 wrk = container_of(work, struct storvsc_scan_work, work);
K. Y. Srinivasan67812092013-02-21 12:04:53 -0800520
K. Y. Srinivasan98441222016-01-27 18:22:45 -0800521 sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
K. Y. Srinivasan67812092013-02-21 12:04:53 -0800522 if (!sdev)
523 goto done;
524 scsi_rescan_device(&sdev->sdev_gendev);
525 scsi_device_put(sdev);
526
527done:
528 kfree(wrk);
529}
530
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -0800531static void storvsc_host_scan(struct work_struct *work)
K. Y. Srinivasan126757992011-11-08 09:01:49 -0800532{
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -0800533 struct Scsi_Host *host;
K. Y. Srinivasan34a716b2014-12-16 13:21:43 -0800534 struct scsi_device *sdev;
Long Lic58cc702017-10-31 14:58:08 -0700535 struct hv_host_device *host_device =
536 container_of(work, struct hv_host_device, host_scan_work);
K. Y. Srinivasan126757992011-11-08 09:01:49 -0800537
Long Lic58cc702017-10-31 14:58:08 -0700538 host = host_device->host;
K. Y. Srinivasan34a716b2014-12-16 13:21:43 -0800539 /*
540 * Before scanning the host, first check to see if any of the
541 * currrently known devices have been hot removed. We issue a
542 * "unit ready" command against all currently known devices.
543 * This I/O will result in an error for devices that have been
544 * removed. As part of handling the I/O error, we remove the device.
545 *
546 * When a LUN is added or removed, the host sends us a signal to
547 * scan the host. Thus we are forced to discover the LUNs that
548 * may have been removed this way.
549 */
550 mutex_lock(&host->scan_mutex);
Vitaly Kuznetsov8d6a9f52015-07-01 11:31:27 +0200551 shost_for_each_device(sdev, host)
K. Y. Srinivasan34a716b2014-12-16 13:21:43 -0800552 scsi_test_unit_ready(sdev, 1, 1, NULL);
K. Y. Srinivasan34a716b2014-12-16 13:21:43 -0800553 mutex_unlock(&host->scan_mutex);
554 /*
555 * Now scan the host to discover LUNs that may have been added.
556 */
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -0800557 scsi_scan_host(host);
K. Y. Srinivasan126757992011-11-08 09:01:49 -0800558}
559
K. Y. Srinivasanb4017312011-11-08 09:01:50 -0800560static void storvsc_remove_lun(struct work_struct *work)
561{
562 struct storvsc_scan_work *wrk;
563 struct scsi_device *sdev;
564
565 wrk = container_of(work, struct storvsc_scan_work, work);
566 if (!scsi_host_get(wrk->host))
567 goto done;
568
K. Y. Srinivasan98441222016-01-27 18:22:45 -0800569 sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
K. Y. Srinivasanb4017312011-11-08 09:01:50 -0800570
571 if (sdev) {
572 scsi_remove_device(sdev);
573 scsi_device_put(sdev);
574 }
575 scsi_host_put(wrk->host);
576
577done:
578 kfree(wrk);
579}
580
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800581
582/*
K. Y. Srinivasana8c18c52012-01-12 12:38:00 -0800583 * We can get incoming messages from the host that are not in response to
584 * messages that we have sent out. An example of this would be messages
585 * received by the guest to notify dynamic addition/removal of LUNs. To
586 * deal with potential race conditions where the driver may be in the
587 * midst of being unloaded when we might receive an unsolicited message
588 * from the host, we have implemented a mechanism to gurantee sequential
589 * consistency:
590 *
591 * 1) Once the device is marked as being destroyed, we will fail all
592 * outgoing messages.
593 * 2) We permit incoming messages when the device is being destroyed,
594 * only to properly account for messages already sent out.
595 */
596
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700597static inline struct storvsc_device *get_out_stor_device(
598 struct hv_device *device)
599{
600 struct storvsc_device *stor_device;
601
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700602 stor_device = hv_get_drvdata(device);
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700603
604 if (stor_device && stor_device->destroy)
605 stor_device = NULL;
606
607 return stor_device;
608}
609
610
611static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
612{
613 dev->drain_notify = true;
614 wait_event(dev->waiting_to_drain,
615 atomic_read(&dev->num_outstanding_req) == 0);
616 dev->drain_notify = false;
617}
Hank Janssenbef4a342009-07-13 16:01:31 -0700618
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700619static inline struct storvsc_device *get_in_stor_device(
620 struct hv_device *device)
621{
622 struct storvsc_device *stor_device;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700623
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700624 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700625
626 if (!stor_device)
627 goto get_in_err;
628
629 /*
630 * If the device is being destroyed; allow incoming
631 * traffic only to cleanup outstanding requests.
632 */
633
634 if (stor_device->destroy &&
635 (atomic_read(&stor_device->num_outstanding_req) == 0))
636 stor_device = NULL;
637
638get_in_err:
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700639 return stor_device;
640
641}
642
Andrea Parri (Microsoft)7769e182020-04-06 02:15:14 +0200643static void storvsc_change_target_cpu(struct vmbus_channel *channel, u32 old,
644 u32 new)
645{
646 struct storvsc_device *stor_device;
647 struct vmbus_channel *cur_chn;
648 bool old_is_alloced = false;
649 struct hv_device *device;
650 unsigned long flags;
651 int cpu;
652
653 device = channel->primary_channel ?
654 channel->primary_channel->device_obj
655 : channel->device_obj;
656 stor_device = get_out_stor_device(device);
657 if (!stor_device)
658 return;
659
660 /* See storvsc_do_io() -> get_og_chn(). */
Andrea Parri (Microsoft)21d20522020-06-17 18:46:41 +0200661 spin_lock_irqsave(&stor_device->lock, flags);
Andrea Parri (Microsoft)7769e182020-04-06 02:15:14 +0200662
663 /*
664 * Determines if the storvsc device has other channels assigned to
665 * the "old" CPU to update the alloced_cpus mask and the stor_chns
666 * array.
667 */
668 if (device->channel != channel && device->channel->target_cpu == old) {
669 cur_chn = device->channel;
670 old_is_alloced = true;
671 goto old_is_alloced;
672 }
673 list_for_each_entry(cur_chn, &device->channel->sc_list, sc_list) {
674 if (cur_chn == channel)
675 continue;
676 if (cur_chn->target_cpu == old) {
677 old_is_alloced = true;
678 goto old_is_alloced;
679 }
680 }
681
682old_is_alloced:
683 if (old_is_alloced)
684 WRITE_ONCE(stor_device->stor_chns[old], cur_chn);
685 else
686 cpumask_clear_cpu(old, &stor_device->alloced_cpus);
687
688 /* "Flush" the stor_chns array. */
689 for_each_possible_cpu(cpu) {
690 if (stor_device->stor_chns[cpu] && !cpumask_test_cpu(
691 cpu, &stor_device->alloced_cpus))
692 WRITE_ONCE(stor_device->stor_chns[cpu], NULL);
693 }
694
695 WRITE_ONCE(stor_device->stor_chns[new], channel);
696 cpumask_set_cpu(new, &stor_device->alloced_cpus);
697
Andrea Parri (Microsoft)21d20522020-06-17 18:46:41 +0200698 spin_unlock_irqrestore(&stor_device->lock, flags);
Andrea Parri (Microsoft)7769e182020-04-06 02:15:14 +0200699}
700
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +0200701static u64 storvsc_next_request_id(struct vmbus_channel *channel, u64 rqst_addr)
702{
703 struct storvsc_cmd_request *request =
704 (struct storvsc_cmd_request *)(unsigned long)rqst_addr;
705
706 if (rqst_addr == VMBUS_RQST_INIT)
707 return VMBUS_RQST_INIT;
708 if (rqst_addr == VMBUS_RQST_RESET)
709 return VMBUS_RQST_RESET;
710
711 /*
712 * Cannot return an ID of 0, which is reserved for an unsolicited
713 * message from Hyper-V.
714 */
Martin K. Petersenc5bf1982021-08-10 00:52:41 -0400715 return (u64)blk_mq_unique_tag(scsi_cmd_to_rq(request->cmd)) + 1;
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +0200716}
717
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700718static void handle_sc_creation(struct vmbus_channel *new_sc)
719{
720 struct hv_device *device = new_sc->primary_channel->device_obj;
Dexuan Cuic9675902018-11-26 00:26:17 +0000721 struct device *dev = &device->device;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700722 struct storvsc_device *stor_device;
723 struct vmstorage_channel_properties props;
Dexuan Cuic9675902018-11-26 00:26:17 +0000724 int ret;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700725
726 stor_device = get_out_stor_device(device);
727 if (!stor_device)
728 return;
729
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700730 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
Andres Beltranadae1e92021-04-08 18:14:39 +0200731 new_sc->max_pkt_size = STORVSC_MAX_PKT_SIZE;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700732
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +0200733 new_sc->next_request_id_callback = storvsc_next_request_id;
Andres Beltran453de212020-11-09 11:04:01 +0100734
Dexuan Cuic9675902018-11-26 00:26:17 +0000735 ret = vmbus_open(new_sc,
736 storvsc_ringbuffer_size,
737 storvsc_ringbuffer_size,
738 (void *)&props,
739 sizeof(struct vmstorage_channel_properties),
740 storvsc_on_channel_callback, new_sc);
K. Y. Srinivasand86adf42016-12-14 18:46:00 -0800741
Dexuan Cuic9675902018-11-26 00:26:17 +0000742 /* In case vmbus_open() fails, we don't use the sub-channel. */
743 if (ret != 0) {
744 dev_err(dev, "Failed to open sub-channel: err=%d\n", ret);
745 return;
K. Y. Srinivasand86adf42016-12-14 18:46:00 -0800746 }
Dexuan Cuic9675902018-11-26 00:26:17 +0000747
Andrea Parri (Microsoft)7769e182020-04-06 02:15:14 +0200748 new_sc->change_target_cpu_callback = storvsc_change_target_cpu;
749
Dexuan Cuic9675902018-11-26 00:26:17 +0000750 /* Add the sub-channel to the array of available channels. */
751 stor_device->stor_chns[new_sc->target_cpu] = new_sc;
752 cpumask_set_cpu(new_sc->target_cpu, &stor_device->alloced_cpus);
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700753}
754
755static void handle_multichannel_storage(struct hv_device *device, int max_chns)
756{
Dexuan Cuic9675902018-11-26 00:26:17 +0000757 struct device *dev = &device->device;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700758 struct storvsc_device *stor_device;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700759 int num_sc;
760 struct storvsc_cmd_request *request;
761 struct vstor_packet *vstor_packet;
762 int ret, t;
763
Michael Kelley382e06d2019-04-01 16:10:52 +0000764 /*
765 * If the number of CPUs is artificially restricted, such as
766 * with maxcpus=1 on the kernel boot line, Hyper-V could offer
767 * sub-channels >= the number of CPUs. These sub-channels
768 * should not be created. The primary channel is already created
769 * and assigned to one CPU, so check against # CPUs - 1.
770 */
771 num_sc = min((int)(num_online_cpus() - 1), max_chns);
772 if (!num_sc)
773 return;
774
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700775 stor_device = get_out_stor_device(device);
776 if (!stor_device)
777 return;
778
K. Y. Srinivasand86adf42016-12-14 18:46:00 -0800779 stor_device->num_sc = num_sc;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700780 request = &stor_device->init_request;
781 vstor_packet = &request->vstor_packet;
782
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700783 /*
784 * Establish a handler for dealing with subchannels.
785 */
786 vmbus_set_sc_create_callback(device->channel, handle_sc_creation);
787
788 /*
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700789 * Request the host to create sub-channels.
790 */
791 memset(request, 0, sizeof(struct storvsc_cmd_request));
792 init_completion(&request->wait_event);
793 vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
794 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
795 vstor_packet->sub_channel_count = num_sc;
796
797 ret = vmbus_sendpacket(device->channel, vstor_packet,
798 (sizeof(struct vstor_packet) -
Andrea Parri (Microsoft)244808e2020-12-17 21:33:20 +0100799 stor_device->vmscsi_size_delta),
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +0200800 VMBUS_RQST_INIT,
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700801 VM_PKT_DATA_INBAND,
802 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
803
Dexuan Cuic9675902018-11-26 00:26:17 +0000804 if (ret != 0) {
805 dev_err(dev, "Failed to create sub-channel: err=%d\n", ret);
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700806 return;
Dexuan Cuic9675902018-11-26 00:26:17 +0000807 }
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700808
809 t = wait_for_completion_timeout(&request->wait_event, 10*HZ);
Dexuan Cuic9675902018-11-26 00:26:17 +0000810 if (t == 0) {
811 dev_err(dev, "Failed to create sub-channel: timed out\n");
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700812 return;
Dexuan Cuic9675902018-11-26 00:26:17 +0000813 }
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700814
815 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
Dexuan Cuic9675902018-11-26 00:26:17 +0000816 vstor_packet->status != 0) {
817 dev_err(dev, "Failed to create sub-channel: op=%d, sts=%d\n",
818 vstor_packet->operation, vstor_packet->status);
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700819 return;
Dexuan Cuic9675902018-11-26 00:26:17 +0000820 }
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700821
822 /*
Dexuan Cuic9675902018-11-26 00:26:17 +0000823 * We need to do nothing here, because vmbus_process_offer()
824 * invokes channel->sc_creation_callback, which will open and use
825 * the sub-channel(s).
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700826 */
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700827}
828
K. Y. Srinivasandac58242015-12-23 13:15:49 -0800829static void cache_wwn(struct storvsc_device *stor_device,
830 struct vstor_packet *vstor_packet)
831{
832 /*
833 * Cache the currently active port and node ww names.
834 */
835 if (vstor_packet->wwn_packet.primary_active) {
836 stor_device->node_name =
837 wwn_to_u64(vstor_packet->wwn_packet.primary_node_wwn);
838 stor_device->port_name =
839 wwn_to_u64(vstor_packet->wwn_packet.primary_port_wwn);
840 } else {
841 stor_device->node_name =
842 wwn_to_u64(vstor_packet->wwn_packet.secondary_node_wwn);
843 stor_device->port_name =
844 wwn_to_u64(vstor_packet->wwn_packet.secondary_port_wwn);
845 }
846}
847
K. Y. Srinivasan59635012015-12-23 13:15:50 -0800848
849static int storvsc_execute_vstor_op(struct hv_device *device,
850 struct storvsc_cmd_request *request,
851 bool status_check)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700852{
Andrea Parri (Microsoft)244808e2020-12-17 21:33:20 +0100853 struct storvsc_device *stor_device;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700854 struct vstor_packet *vstor_packet;
K. Y. Srinivasan59635012015-12-23 13:15:50 -0800855 int ret, t;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700856
Andrea Parri (Microsoft)244808e2020-12-17 21:33:20 +0100857 stor_device = get_out_stor_device(device);
858 if (!stor_device)
859 return -ENODEV;
860
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700861 vstor_packet = &request->vstor_packet;
862
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700863 init_completion(&request->wait_event);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700864 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
865
866 ret = vmbus_sendpacket(device->channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700867 (sizeof(struct vstor_packet) -
Andrea Parri (Microsoft)244808e2020-12-17 21:33:20 +0100868 stor_device->vmscsi_size_delta),
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +0200869 VMBUS_RQST_INIT,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700870 VM_PKT_DATA_INBAND,
871 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
872 if (ret != 0)
K. Y. Srinivasandac58242015-12-23 13:15:49 -0800873 return ret;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700874
875 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
K. Y. Srinivasandac58242015-12-23 13:15:49 -0800876 if (t == 0)
877 return -ETIMEDOUT;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700878
K. Y. Srinivasan59635012015-12-23 13:15:50 -0800879 if (!status_check)
880 return ret;
881
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700882 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
K. Y. Srinivasandac58242015-12-23 13:15:49 -0800883 vstor_packet->status != 0)
884 return -EINVAL;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700885
K. Y. Srinivasan59635012015-12-23 13:15:50 -0800886 return ret;
887}
888
889static int storvsc_channel_init(struct hv_device *device, bool is_fc)
890{
891 struct storvsc_device *stor_device;
892 struct storvsc_cmd_request *request;
893 struct vstor_packet *vstor_packet;
894 int ret, i;
895 int max_chns;
896 bool process_sub_channels = false;
897
898 stor_device = get_out_stor_device(device);
899 if (!stor_device)
900 return -ENODEV;
901
902 request = &stor_device->init_request;
903 vstor_packet = &request->vstor_packet;
904
905 /*
906 * Now, initiate the vsc/vsp initialization protocol on the open
907 * channel
908 */
909 memset(request, 0, sizeof(struct storvsc_cmd_request));
910 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
911 ret = storvsc_execute_vstor_op(device, request, true);
912 if (ret)
913 return ret;
914 /*
915 * Query host supported protocol version.
916 */
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700917
Keith Mange1a363102015-08-13 08:43:48 -0700918 for (i = 0; i < ARRAY_SIZE(vmstor_protocols); i++) {
919 /* reuse the packet for version range supported */
920 memset(vstor_packet, 0, sizeof(struct vstor_packet));
921 vstor_packet->operation =
922 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700923
Keith Mange1a363102015-08-13 08:43:48 -0700924 vstor_packet->version.major_minor =
925 vmstor_protocols[i].protocol_version;
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800926
Keith Mange1a363102015-08-13 08:43:48 -0700927 /*
928 * The revision number is only used in Windows; set it to 0.
929 */
930 vstor_packet->version.revision = 0;
K. Y. Srinivasan59635012015-12-23 13:15:50 -0800931 ret = storvsc_execute_vstor_op(device, request, false);
Keith Mange1a363102015-08-13 08:43:48 -0700932 if (ret != 0)
K. Y. Srinivasandac58242015-12-23 13:15:49 -0800933 return ret;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700934
K. Y. Srinivasandac58242015-12-23 13:15:49 -0800935 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO)
936 return -EINVAL;
Keith Mange1a363102015-08-13 08:43:48 -0700937
938 if (vstor_packet->status == 0) {
939 vmstor_proto_version =
940 vmstor_protocols[i].protocol_version;
941
942 sense_buffer_size =
943 vmstor_protocols[i].sense_buffer_size;
944
Andrea Parri (Microsoft)244808e2020-12-17 21:33:20 +0100945 stor_device->vmscsi_size_delta =
Keith Mange1a363102015-08-13 08:43:48 -0700946 vmstor_protocols[i].vmscsi_size_delta;
947
948 break;
949 }
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700950 }
951
K. Y. Srinivasandac58242015-12-23 13:15:49 -0800952 if (vstor_packet->status != 0)
953 return -EINVAL;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700954
955
956 memset(vstor_packet, 0, sizeof(struct vstor_packet));
957 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
K. Y. Srinivasan59635012015-12-23 13:15:50 -0800958 ret = storvsc_execute_vstor_op(device, request, true);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700959 if (ret != 0)
K. Y. Srinivasandac58242015-12-23 13:15:49 -0800960 return ret;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700961
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700962 /*
963 * Check to see if multi-channel support is there.
964 * Hosts that implement protocol version of 5.1 and above
965 * support multi-channel.
966 */
967 max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
K. Y. Srinivasand86adf42016-12-14 18:46:00 -0800968
969 /*
970 * Allocate state to manage the sub-channels.
971 * We allocate an array based on the numbers of possible CPUs
972 * (Hyper-V does not support cpu online/offline).
973 * This Array will be sparseley populated with unique
974 * channels - primary + sub-channels.
975 * We will however populate all the slots to evenly distribute
976 * the load.
977 */
Miguel Bernal Marine0408522017-03-16 00:58:23 -0600978 stor_device->stor_chns = kcalloc(num_possible_cpus(), sizeof(void *),
K. Y. Srinivasand86adf42016-12-14 18:46:00 -0800979 GFP_KERNEL);
980 if (stor_device->stor_chns == NULL)
981 return -ENOMEM;
982
Andrea Parri (Microsoft)7769e182020-04-06 02:15:14 +0200983 device->channel->change_target_cpu_callback = storvsc_change_target_cpu;
984
K. Y. Srinivasand86adf42016-12-14 18:46:00 -0800985 stor_device->stor_chns[device->channel->target_cpu] = device->channel;
986 cpumask_set_cpu(device->channel->target_cpu,
987 &stor_device->alloced_cpus);
988
Keith Mangee6c4bc62015-08-13 08:43:50 -0700989 if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN8) {
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700990 if (vstor_packet->storage_channel_properties.flags &
991 STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
992 process_sub_channels = true;
993 }
K. Y. Srinivasan5117b932015-03-27 00:27:17 -0700994 stor_device->max_transfer_bytes =
995 vstor_packet->storage_channel_properties.max_transfer_bytes;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700996
K. Y. Srinivasandac58242015-12-23 13:15:49 -0800997 if (!is_fc)
998 goto done;
999
K. Y. Srinivasan59635012015-12-23 13:15:50 -08001000 /*
1001 * For FC devices retrieve FC HBA data.
1002 */
K. Y. Srinivasandac58242015-12-23 13:15:49 -08001003 memset(vstor_packet, 0, sizeof(struct vstor_packet));
1004 vstor_packet->operation = VSTOR_OPERATION_FCHBA_DATA;
K. Y. Srinivasan59635012015-12-23 13:15:50 -08001005 ret = storvsc_execute_vstor_op(device, request, true);
K. Y. Srinivasandac58242015-12-23 13:15:49 -08001006 if (ret != 0)
1007 return ret;
1008
K. Y. Srinivasandac58242015-12-23 13:15:49 -08001009 /*
1010 * Cache the currently active port and node ww names.
1011 */
1012 cache_wwn(stor_device, vstor_packet);
1013
1014done:
1015
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001016 memset(vstor_packet, 0, sizeof(struct vstor_packet));
1017 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
K. Y. Srinivasan59635012015-12-23 13:15:50 -08001018 ret = storvsc_execute_vstor_op(device, request, true);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001019 if (ret != 0)
K. Y. Srinivasandac58242015-12-23 13:15:49 -08001020 return ret;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001021
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001022 if (process_sub_channels)
1023 handle_multichannel_storage(device, max_chns);
1024
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001025 return ret;
1026}
1027
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001028static void storvsc_handle_error(struct vmscsi_request *vm_srb,
1029 struct scsi_cmnd *scmnd,
1030 struct Scsi_Host *host,
1031 u8 asc, u8 ascq)
1032{
1033 struct storvsc_scan_work *wrk;
1034 void (*process_err_fn)(struct work_struct *work);
Cathy Avery436ad942017-10-31 08:52:06 -04001035 struct hv_host_device *host_dev = shost_priv(host);
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001036
Michael Kelley52e1b3b2021-06-04 10:21:03 -07001037 /*
1038 * In some situations, Hyper-V sets multiple bits in the
1039 * srb_status, such as ABORTED and ERROR. So process them
1040 * individually, with the most specific bits first.
1041 */
1042
1043 if (vm_srb->srb_status & SRB_STATUS_INVALID_LUN) {
1044 set_host_byte(scmnd, DID_NO_CONNECT);
1045 process_err_fn = storvsc_remove_lun;
1046 goto do_work;
1047 }
1048
1049 if (vm_srb->srb_status & SRB_STATUS_ABORTED) {
1050 if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID &&
1051 /* Capacity data has changed */
1052 (asc == 0x2a) && (ascq == 0x9)) {
1053 process_err_fn = storvsc_device_scan;
1054 /*
1055 * Retry the I/O that triggered this.
1056 */
1057 set_host_byte(scmnd, DID_REQUEUE);
1058 goto do_work;
1059 }
1060 }
1061
1062 if (vm_srb->srb_status & SRB_STATUS_ERROR) {
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001063 /*
Long Libba5dc32016-12-14 18:46:02 -08001064 * Let upper layer deal with error when
1065 * sense message is present.
1066 */
Long Libba5dc32016-12-14 18:46:02 -08001067 if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID)
Michael Kelley52e1b3b2021-06-04 10:21:03 -07001068 return;
1069
Long Libba5dc32016-12-14 18:46:02 -08001070 /*
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001071 * If there is an error; offline the device since all
1072 * error recovery strategies would have already been
1073 * deployed on the host side. However, if the command
1074 * were a pass-through command deal with it appropriately.
1075 */
1076 switch (scmnd->cmnd[0]) {
1077 case ATA_16:
1078 case ATA_12:
1079 set_host_byte(scmnd, DID_PASSTHROUGH);
1080 break;
K. Y. Srinivasan3533f8602014-07-12 09:48:32 -07001081 /*
Michael Kelley52e1b3b2021-06-04 10:21:03 -07001082 * On some Hyper-V hosts TEST_UNIT_READY command can
1083 * return SRB_STATUS_ERROR. Let the upper level code
1084 * deal with it based on the sense information.
K. Y. Srinivasan3533f8602014-07-12 09:48:32 -07001085 */
1086 case TEST_UNIT_READY:
1087 break;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001088 default:
Cathy Averyd1b8b232017-12-19 13:32:48 -05001089 set_host_byte(scmnd, DID_ERROR);
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001090 }
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001091 }
Michael Kelley52e1b3b2021-06-04 10:21:03 -07001092 return;
K. Y. Srinivasan67812092013-02-21 12:04:53 -08001093
Michael Kelley52e1b3b2021-06-04 10:21:03 -07001094do_work:
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001095 /*
1096 * We need to schedule work to process this error; schedule it.
1097 */
1098 wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
1099 if (!wrk) {
1100 set_host_byte(scmnd, DID_TARGET_FAILURE);
1101 return;
1102 }
1103
1104 wrk->host = host;
1105 wrk->lun = vm_srb->lun;
K. Y. Srinivasan98441222016-01-27 18:22:45 -08001106 wrk->tgt_id = vm_srb->target_id;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001107 INIT_WORK(&wrk->work, process_err_fn);
Cathy Avery436ad942017-10-31 08:52:06 -04001108 queue_work(host_dev->handle_error_wq, &wrk->work);
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001109}
1110
K. Y. Srinivasan2707388c2012-01-12 12:38:01 -08001111
K. Y. Srinivasan03996f22015-12-23 13:15:51 -08001112static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request,
1113 struct storvsc_device *stor_dev)
K. Y. Srinivasan2707388c2012-01-12 12:38:01 -08001114{
K. Y. Srinivasan2707388c2012-01-12 12:38:01 -08001115 struct scsi_cmnd *scmnd = cmd_request->cmd;
K. Y. Srinivasan2707388c2012-01-12 12:38:01 -08001116 struct scsi_sense_hdr sense_hdr;
1117 struct vmscsi_request *vm_srb;
Long Li40630f42016-12-14 18:46:03 -08001118 u32 data_transfer_length;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001119 struct Scsi_Host *host;
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001120 u32 payload_sz = cmd_request->payload_sz;
1121 void *payload = cmd_request->payload;
Michael Kelley08f76542021-06-04 10:21:02 -07001122 bool sense_ok;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001123
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001124 host = stor_dev->host;
K. Y. Srinivasan2707388c2012-01-12 12:38:01 -08001125
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001126 vm_srb = &cmd_request->vstor_packet.vm_srb;
Long Li40630f42016-12-14 18:46:03 -08001127 data_transfer_length = vm_srb->data_transfer_length;
K. Y. Srinivasan2707388c2012-01-12 12:38:01 -08001128
K. Y. Srinivasan42e22ca2012-04-05 12:26:52 -07001129 scmnd->result = vm_srb->scsi_status;
1130
K. Y. Srinivasan2707388c2012-01-12 12:38:01 -08001131 if (scmnd->result) {
Michael Kelley08f76542021-06-04 10:21:02 -07001132 sense_ok = scsi_normalize_sense(scmnd->sense_buffer,
1133 SCSI_SENSE_BUFFERSIZE, &sense_hdr);
1134
1135 if (sense_ok && do_logging(STORVSC_LOGGING_WARN))
Hannes Reinecked811b842014-10-24 14:26:45 +02001136 scsi_print_sense_hdr(scmnd->device, "storvsc",
1137 &sense_hdr);
K. Y. Srinivasan2707388c2012-01-12 12:38:01 -08001138 }
1139
Long Li40630f42016-12-14 18:46:03 -08001140 if (vm_srb->srb_status != SRB_STATUS_SUCCESS) {
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001141 storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
1142 sense_hdr.ascq);
Long Li40630f42016-12-14 18:46:03 -08001143 /*
1144 * The Windows driver set data_transfer_length on
1145 * SRB_STATUS_DATA_OVERRUN. On other errors, this value
1146 * is untouched. In these cases we set it to 0.
1147 */
1148 if (vm_srb->srb_status != SRB_STATUS_DATA_OVERRUN)
1149 data_transfer_length = 0;
1150 }
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001151
Andres Beltran0a765662020-07-06 12:09:28 -04001152 /* Validate data_transfer_length (from Hyper-V) */
1153 if (data_transfer_length > cmd_request->payload->range.len)
1154 data_transfer_length = cmd_request->payload->range.len;
1155
K. Y. Srinivasan2707388c2012-01-12 12:38:01 -08001156 scsi_set_resid(scmnd,
Long Li40630f42016-12-14 18:46:03 -08001157 cmd_request->payload->range.len - data_transfer_length);
K. Y. Srinivasan2707388c2012-01-12 12:38:01 -08001158
Bart Van Assche0c31fa02021-10-07 13:29:08 -07001159 scsi_done(scmnd);
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001160
1161 if (payload_sz >
1162 sizeof(struct vmbus_channel_packet_multipage_buffer))
1163 kfree(payload);
K. Y. Srinivasan2707388c2012-01-12 12:38:01 -08001164}
1165
K. Y. Srinivasan03996f22015-12-23 13:15:51 -08001166static void storvsc_on_io_completion(struct storvsc_device *stor_device,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001167 struct vstor_packet *vstor_packet,
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001168 struct storvsc_cmd_request *request)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001169{
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001170 struct vstor_packet *stor_pkt;
K. Y. Srinivasan03996f22015-12-23 13:15:51 -08001171 struct hv_device *device = stor_device->device;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001172
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001173 stor_pkt = &request->vstor_packet;
1174
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -07001175 /*
1176 * The current SCSI handling on the host side does
1177 * not correctly handle:
1178 * INQUIRY command with page code parameter set to 0x80
1179 * MODE_SENSE command with cmd[2] == 0x1c
1180 *
1181 * Setup srb and scsi status so this won't be fatal.
1182 * We do this so we can distinguish truly fatal failues
1183 * (srb status == 0x4) and off-line the device in that case.
1184 */
1185
1186 if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
K. Y. Srinivasana8c18c52012-01-12 12:38:00 -08001187 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -07001188 vstor_packet->vm_srb.scsi_status = 0;
K. Y. Srinivasan16046322012-01-12 12:37:57 -08001189 vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -07001190 }
1191
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001192 /* Copy over the status...etc */
1193 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
1194 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
Andres Beltran0a765662020-07-06 12:09:28 -04001195
Michael Kelleyd4674852021-06-04 10:21:01 -07001196 /*
1197 * Copy over the sense_info_length, but limit to the known max
1198 * size if Hyper-V returns a bad value.
1199 */
1200 stor_pkt->vm_srb.sense_info_length = min_t(u8, sense_buffer_size,
1201 vstor_packet->vm_srb.sense_info_length);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001202
Long Lif8aea702015-12-04 00:07:24 -08001203 if (vstor_packet->vm_srb.scsi_status != 0 ||
Michael Kelleydbe76332021-08-06 10:12:50 -07001204 vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS) {
1205
1206 /*
1207 * Log TEST_UNIT_READY errors only as warnings. Hyper-V can
1208 * return errors when detecting devices using TEST_UNIT_READY,
1209 * and logging these as errors produces unhelpful noise.
1210 */
1211 int loglevel = (stor_pkt->vm_srb.cdb[0] == TEST_UNIT_READY) ?
1212 STORVSC_LOGGING_WARN : STORVSC_LOGGING_ERROR;
1213
1214 storvsc_log(device, loglevel,
Michael Kelley08f76542021-06-04 10:21:02 -07001215 "tag#%d cmd 0x%x status: scsi 0x%x srb 0x%x hv 0x%x\n",
Martin K. Petersenc5bf1982021-08-10 00:52:41 -04001216 scsi_cmd_to_rq(request->cmd)->tag,
Long Lif8aea702015-12-04 00:07:24 -08001217 stor_pkt->vm_srb.cdb[0],
1218 vstor_packet->vm_srb.scsi_status,
Michael Kelley08f76542021-06-04 10:21:02 -07001219 vstor_packet->vm_srb.srb_status,
1220 vstor_packet->status);
Michael Kelleydbe76332021-08-06 10:12:50 -07001221 }
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001222
Michael Kelleyd4674852021-06-04 10:21:01 -07001223 if (vstor_packet->vm_srb.scsi_status == SAM_STAT_CHECK_CONDITION &&
1224 (vstor_packet->vm_srb.srb_status & SRB_STATUS_AUTOSENSE_VALID))
1225 memcpy(request->cmd->sense_buffer,
1226 vstor_packet->vm_srb.sense_data,
1227 stor_pkt->vm_srb.sense_info_length);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001228
1229 stor_pkt->vm_srb.data_transfer_length =
Michael Kelleyd4674852021-06-04 10:21:01 -07001230 vstor_packet->vm_srb.data_transfer_length;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001231
K. Y. Srinivasan03996f22015-12-23 13:15:51 -08001232 storvsc_command_completion(request, stor_device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001233
1234 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
1235 stor_device->drain_notify)
1236 wake_up(&stor_device->waiting_to_drain);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001237}
1238
K. Y. Srinivasan03996f22015-12-23 13:15:51 -08001239static void storvsc_on_receive(struct storvsc_device *stor_device,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001240 struct vstor_packet *vstor_packet,
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001241 struct storvsc_cmd_request *request)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001242{
Long Lic58cc702017-10-31 14:58:08 -07001243 struct hv_host_device *host_dev;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001244 switch (vstor_packet->operation) {
1245 case VSTOR_OPERATION_COMPLETE_IO:
K. Y. Srinivasan03996f22015-12-23 13:15:51 -08001246 storvsc_on_io_completion(stor_device, vstor_packet, request);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001247 break;
K. Y. Srinivasan126757992011-11-08 09:01:49 -08001248
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001249 case VSTOR_OPERATION_REMOVE_DEVICE:
K. Y. Srinivasan126757992011-11-08 09:01:49 -08001250 case VSTOR_OPERATION_ENUMERATE_BUS:
Long Lic58cc702017-10-31 14:58:08 -07001251 host_dev = shost_priv(stor_device->host);
1252 queue_work(
1253 host_dev->handle_error_wq, &host_dev->host_scan_work);
K. Y. Srinivasan126757992011-11-08 09:01:49 -08001254 break;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001255
K. Y. Srinivasandac58242015-12-23 13:15:49 -08001256 case VSTOR_OPERATION_FCHBA_DATA:
K. Y. Srinivasandac58242015-12-23 13:15:49 -08001257 cache_wwn(stor_device, vstor_packet);
1258#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1259 fc_host_node_name(stor_device->host) = stor_device->node_name;
1260 fc_host_port_name(stor_device->host) = stor_device->port_name;
1261#endif
1262 break;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001263 default:
1264 break;
1265 }
1266}
1267
1268static void storvsc_on_channel_callback(void *context)
1269{
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001270 struct vmbus_channel *channel = (struct vmbus_channel *)context;
Stephen Hemmingerddccd952017-05-18 09:18:11 -07001271 const struct vmpacket_descriptor *desc;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001272 struct hv_device *device;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001273 struct storvsc_device *stor_device;
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001274 struct Scsi_Host *shost;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001275
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001276 if (channel->primary_channel != NULL)
1277 device = channel->primary_channel->device_obj;
1278 else
1279 device = channel->device_obj;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001280
1281 stor_device = get_in_stor_device(device);
1282 if (!stor_device)
1283 return;
1284
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001285 shost = stor_device->host;
1286
Stephen Hemmingerddccd952017-05-18 09:18:11 -07001287 foreach_vmbus_pkt(desc, channel) {
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001288 struct vstor_packet *packet = hv_pkt_data(desc);
1289 struct storvsc_cmd_request *request = NULL;
Andrea Parri (Microsoft)6fd13d62021-10-07 14:28:28 +02001290 u32 pktlen = hv_pkt_datalen(desc);
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001291 u64 rqst_id = desc->trans_id;
Andrea Parri (Microsoft)6fd13d62021-10-07 14:28:28 +02001292 u32 minlen = rqst_id ? sizeof(struct vstor_packet) -
1293 stor_device->vmscsi_size_delta : sizeof(enum vstor_packet_operation);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001294
Andrea Parri (Microsoft)6fd13d62021-10-07 14:28:28 +02001295 if (pktlen < minlen) {
1296 dev_err(&device->device,
1297 "Invalid pkt: id=%llu, len=%u, minlen=%u\n",
1298 rqst_id, pktlen, minlen);
Andrea Parri (Microsoft)91b1b642020-12-17 21:33:21 +01001299 continue;
1300 }
1301
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001302 if (rqst_id == VMBUS_RQST_INIT) {
1303 request = &stor_device->init_request;
1304 } else if (rqst_id == VMBUS_RQST_RESET) {
1305 request = &stor_device->reset_request;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001306 } else {
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001307 /* Hyper-V can send an unsolicited message with ID of 0 */
1308 if (rqst_id == 0) {
1309 /*
1310 * storvsc_on_receive() looks at the vstor_packet in the message
Andrea Parri (Microsoft)6fd13d62021-10-07 14:28:28 +02001311 * from the ring buffer.
1312 *
1313 * - If the operation in the vstor_packet is COMPLETE_IO, then
1314 * we call storvsc_on_io_completion(), and dereference the
1315 * guest memory address. Make sure we don't call
1316 * storvsc_on_io_completion() with a guest memory address
1317 * that is zero if Hyper-V were to construct and send such
1318 * a bogus packet.
1319 *
1320 * - If the operation in the vstor_packet is FCHBA_DATA, then
1321 * we call cache_wwn(), and access the data payload area of
1322 * the packet (wwn_packet); however, there is no guarantee
1323 * that the packet is big enough to contain such area.
1324 * Future-proof the code by rejecting such a bogus packet.
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001325 */
Andrea Parri (Microsoft)6fd13d62021-10-07 14:28:28 +02001326 if (packet->operation == VSTOR_OPERATION_COMPLETE_IO ||
1327 packet->operation == VSTOR_OPERATION_FCHBA_DATA) {
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001328 dev_err(&device->device, "Invalid packet with ID of 0\n");
1329 continue;
1330 }
1331 } else {
1332 struct scsi_cmnd *scmnd;
1333
1334 /* Transaction 'rqst_id' corresponds to tag 'rqst_id - 1' */
1335 scmnd = scsi_host_find_tag(shost, rqst_id - 1);
1336 if (scmnd == NULL) {
1337 dev_err(&device->device, "Incorrect transaction ID\n");
1338 continue;
1339 }
1340 request = (struct storvsc_cmd_request *)scsi_cmd_priv(scmnd);
Tianyu Lan743b2372021-12-13 02:14:05 -05001341 scsi_dma_unmap(scmnd);
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001342 }
1343
Stephen Hemmingerddccd952017-05-18 09:18:11 -07001344 storvsc_on_receive(stor_device, packet, request);
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001345 continue;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001346 }
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001347
1348 memcpy(&request->vstor_packet, packet,
1349 (sizeof(struct vstor_packet) - stor_device->vmscsi_size_delta));
1350 complete(&request->wait_event);
Stephen Hemmingerddccd952017-05-18 09:18:11 -07001351 }
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001352}
1353
K. Y. Srinivasandac58242015-12-23 13:15:49 -08001354static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
1355 bool is_fc)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001356{
1357 struct vmstorage_channel_properties props;
1358 int ret;
1359
1360 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
1361
Andres Beltranadae1e92021-04-08 18:14:39 +02001362 device->channel->max_pkt_size = STORVSC_MAX_PKT_SIZE;
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001363 device->channel->next_request_id_callback = storvsc_next_request_id;
Andres Beltran453de212020-11-09 11:04:01 +01001364
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001365 ret = vmbus_open(device->channel,
1366 ring_size,
1367 ring_size,
1368 (void *)&props,
1369 sizeof(struct vmstorage_channel_properties),
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001370 storvsc_on_channel_callback, device->channel);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001371
1372 if (ret != 0)
1373 return ret;
1374
K. Y. Srinivasandac58242015-12-23 13:15:49 -08001375 ret = storvsc_channel_init(device, is_fc);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001376
1377 return ret;
1378}
1379
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -07001380static int storvsc_dev_remove(struct hv_device *device)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001381{
1382 struct storvsc_device *stor_device;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001383
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001384 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001385
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001386 stor_device->destroy = true;
Stephen Hemminger2371cd92017-05-18 09:18:12 -07001387
1388 /* Make sure flag is set before waiting */
1389 wmb();
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001390
1391 /*
1392 * At this point, all outbound traffic should be disable. We
1393 * only allow inbound traffic (responses) to proceed so that
1394 * outstanding requests can be completed.
1395 */
1396
1397 storvsc_wait_to_drain(stor_device);
1398
1399 /*
1400 * Since we have already drained, we don't need to busy wait
1401 * as was done in final_release_stor_device()
1402 * Note that we cannot set the ext pointer to NULL until
1403 * we have drained - to drain the outgoing packets, we need to
1404 * allow incoming packets.
1405 */
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001406 hv_set_drvdata(device, NULL);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001407
1408 /* Close the channel */
1409 vmbus_close(device->channel);
1410
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08001411 kfree(stor_device->stor_chns);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001412 kfree(stor_device);
1413 return 0;
1414}
1415
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08001416static struct vmbus_channel *get_og_chn(struct storvsc_device *stor_device,
1417 u16 q_num)
1418{
1419 u16 slot = 0;
1420 u16 hash_qnum;
Michael Kelley1b25a8c42018-05-17 14:07:40 -07001421 const struct cpumask *node_mask;
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08001422 int num_channels, tgt_cpu;
1423
Andrea Parri (Microsoft)7769e182020-04-06 02:15:14 +02001424 if (stor_device->num_sc == 0) {
1425 stor_device->stor_chns[q_num] = stor_device->device->channel;
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08001426 return stor_device->device->channel;
Andrea Parri (Microsoft)7769e182020-04-06 02:15:14 +02001427 }
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08001428
1429 /*
1430 * Our channel array is sparsley populated and we
1431 * initiated I/O on a processor/hw-q that does not
1432 * currently have a designated channel. Fix this.
1433 * The strategy is simple:
1434 * I. Ensure NUMA locality
1435 * II. Distribute evenly (best effort)
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08001436 */
1437
Michael Kelley1b25a8c42018-05-17 14:07:40 -07001438 node_mask = cpumask_of_node(cpu_to_node(q_num));
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08001439
Michael Kelley1b25a8c42018-05-17 14:07:40 -07001440 num_channels = 0;
1441 for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
1442 if (cpumask_test_cpu(tgt_cpu, node_mask))
1443 num_channels++;
1444 }
Andrea Parri (Microsoft)7769e182020-04-06 02:15:14 +02001445 if (num_channels == 0) {
1446 stor_device->stor_chns[q_num] = stor_device->device->channel;
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08001447 return stor_device->device->channel;
Andrea Parri (Microsoft)7769e182020-04-06 02:15:14 +02001448 }
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08001449
1450 hash_qnum = q_num;
1451 while (hash_qnum >= num_channels)
1452 hash_qnum -= num_channels;
1453
Michael Kelley1b25a8c42018-05-17 14:07:40 -07001454 for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
1455 if (!cpumask_test_cpu(tgt_cpu, node_mask))
1456 continue;
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08001457 if (slot == hash_qnum)
1458 break;
1459 slot++;
1460 }
1461
1462 stor_device->stor_chns[q_num] = stor_device->stor_chns[tgt_cpu];
1463
1464 return stor_device->stor_chns[q_num];
1465}
1466
1467
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -07001468static int storvsc_do_io(struct hv_device *device,
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08001469 struct storvsc_cmd_request *request, u16 q_num)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001470{
1471 struct storvsc_device *stor_device;
1472 struct vstor_packet *vstor_packet;
Long Li2217a472018-04-19 14:54:24 -07001473 struct vmbus_channel *outgoing_channel, *channel;
Andrea Parri (Microsoft)7769e182020-04-06 02:15:14 +02001474 unsigned long flags;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001475 int ret = 0;
Michael Kelley1b25a8c42018-05-17 14:07:40 -07001476 const struct cpumask *node_mask;
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08001477 int tgt_cpu;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001478
1479 vstor_packet = &request->vstor_packet;
1480 stor_device = get_out_stor_device(device);
1481
1482 if (!stor_device)
1483 return -ENODEV;
1484
1485
1486 request->device = device;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001487 /*
Andrea Parri (Microsoft)7769e182020-04-06 02:15:14 +02001488 * Select an appropriate channel to send the request out.
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001489 */
Andrea Parri (Microsoft)7769e182020-04-06 02:15:14 +02001490 /* See storvsc_change_target_cpu(). */
1491 outgoing_channel = READ_ONCE(stor_device->stor_chns[q_num]);
1492 if (outgoing_channel != NULL) {
Long Li2217a472018-04-19 14:54:24 -07001493 if (outgoing_channel->target_cpu == q_num) {
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08001494 /*
1495 * Ideally, we want to pick a different channel if
1496 * available on the same NUMA node.
1497 */
Michael Kelley1b25a8c42018-05-17 14:07:40 -07001498 node_mask = cpumask_of_node(cpu_to_node(q_num));
1499 for_each_cpu_wrap(tgt_cpu,
1500 &stor_device->alloced_cpus, q_num + 1) {
1501 if (!cpumask_test_cpu(tgt_cpu, node_mask))
1502 continue;
Long Li2217a472018-04-19 14:54:24 -07001503 if (tgt_cpu == q_num)
1504 continue;
Andrea Parri (Microsoft)7769e182020-04-06 02:15:14 +02001505 channel = READ_ONCE(
1506 stor_device->stor_chns[tgt_cpu]);
1507 if (channel == NULL)
1508 continue;
Long Li2217a472018-04-19 14:54:24 -07001509 if (hv_get_avail_to_write_percent(
1510 &channel->outbound)
1511 > ring_avail_percent_lowater) {
1512 outgoing_channel = channel;
1513 goto found_channel;
1514 }
1515 }
1516
1517 /*
1518 * All the other channels on the same NUMA node are
1519 * busy. Try to use the channel on the current CPU
1520 */
1521 if (hv_get_avail_to_write_percent(
1522 &outgoing_channel->outbound)
1523 > ring_avail_percent_lowater)
1524 goto found_channel;
1525
1526 /*
1527 * If we reach here, all the channels on the current
1528 * NUMA node are busy. Try to find a channel in
1529 * other NUMA nodes
1530 */
Michael Kelley1b25a8c42018-05-17 14:07:40 -07001531 for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
1532 if (cpumask_test_cpu(tgt_cpu, node_mask))
1533 continue;
Andrea Parri (Microsoft)7769e182020-04-06 02:15:14 +02001534 channel = READ_ONCE(
1535 stor_device->stor_chns[tgt_cpu]);
1536 if (channel == NULL)
1537 continue;
Long Li2217a472018-04-19 14:54:24 -07001538 if (hv_get_avail_to_write_percent(
1539 &channel->outbound)
1540 > ring_avail_percent_lowater) {
1541 outgoing_channel = channel;
1542 goto found_channel;
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08001543 }
1544 }
1545 }
1546 } else {
Andrea Parri (Microsoft)21d20522020-06-17 18:46:41 +02001547 spin_lock_irqsave(&stor_device->lock, flags);
Andrea Parri (Microsoft)7769e182020-04-06 02:15:14 +02001548 outgoing_channel = stor_device->stor_chns[q_num];
1549 if (outgoing_channel != NULL) {
Andrea Parri (Microsoft)21d20522020-06-17 18:46:41 +02001550 spin_unlock_irqrestore(&stor_device->lock, flags);
Andrea Parri (Microsoft)7769e182020-04-06 02:15:14 +02001551 goto found_channel;
1552 }
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08001553 outgoing_channel = get_og_chn(stor_device, q_num);
Andrea Parri (Microsoft)21d20522020-06-17 18:46:41 +02001554 spin_unlock_irqrestore(&stor_device->lock, flags);
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08001555 }
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001556
Long Li2217a472018-04-19 14:54:24 -07001557found_channel:
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001558 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
1559
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001560 vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) -
Andrea Parri (Microsoft)244808e2020-12-17 21:33:20 +01001561 stor_device->vmscsi_size_delta);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001562
1563
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001564 vstor_packet->vm_srb.sense_info_length = sense_buffer_size;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001565
1566
1567 vstor_packet->vm_srb.data_transfer_length =
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001568 request->payload->range.len;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001569
1570 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
1571
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001572 if (request->payload->range.len) {
1573
1574 ret = vmbus_sendpacket_mpb_desc(outgoing_channel,
1575 request->payload, request->payload_sz,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001576 vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001577 (sizeof(struct vstor_packet) -
Andrea Parri (Microsoft)244808e2020-12-17 21:33:20 +01001578 stor_device->vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001579 (unsigned long)request);
1580 } else {
K. Y. Srinivasan0147dab2015-03-27 00:27:16 -07001581 ret = vmbus_sendpacket(outgoing_channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001582 (sizeof(struct vstor_packet) -
Andrea Parri (Microsoft)244808e2020-12-17 21:33:20 +01001583 stor_device->vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001584 (unsigned long)request,
1585 VM_PKT_DATA_INBAND,
1586 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1587 }
1588
1589 if (ret != 0)
1590 return ret;
1591
1592 atomic_inc(&stor_device->num_outstanding_req);
1593
1594 return ret;
1595}
1596
Stephen Hemmingerf1c635b2017-03-07 09:15:53 -08001597static int storvsc_device_alloc(struct scsi_device *sdevice)
1598{
1599 /*
1600 * Set blist flag to permit the reading of the VPD pages even when
1601 * the target may claim SPC-2 compliance. MSFT targets currently
1602 * claim SPC-2 compliance while they implement post SPC-2 features.
1603 * With this flag we can correctly handle WRITE_SAME_16 issues.
1604 *
1605 * Hypervisor reports SCSI_UNKNOWN type for DVD ROM device but
1606 * still supports REPORT LUN.
1607 */
1608 sdevice->sdev_bflags = BLIST_REPORTLUN2 | BLIST_TRY_VPD_PAGES;
1609
1610 return 0;
1611}
1612
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001613static int storvsc_device_configure(struct scsi_device *sdevice)
1614{
K. Y. Srinivasan893def32013-06-04 12:05:05 -07001615 blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
1616
Olaf Hering3e8f4f42013-02-21 12:04:51 -08001617 sdevice->no_write_same = 1;
1618
K. Y. Srinivasanf3cfabc2014-07-21 16:06:01 -07001619 /*
K. Y. Srinivasanb0a93d92014-12-16 13:21:45 -08001620 * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3
Keith Mangeb95f5be2015-08-13 08:43:51 -07001621 * if the device is a MSFT virtual device. If the host is
1622 * WIN10 or newer, allow write_same.
K. Y. Srinivasanb0a93d92014-12-16 13:21:45 -08001623 */
1624 if (!strncmp(sdevice->vendor, "Msft", 4)) {
Keith Mangee6c4bc62015-08-13 08:43:50 -07001625 switch (vmstor_proto_version) {
1626 case VMSTOR_PROTO_VERSION_WIN8:
1627 case VMSTOR_PROTO_VERSION_WIN8_1:
K. Y. Srinivasanb0a93d92014-12-16 13:21:45 -08001628 sdevice->scsi_level = SCSI_SPC_3;
1629 break;
1630 }
Keith Mangeb95f5be2015-08-13 08:43:51 -07001631
1632 if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN10)
1633 sdevice->no_write_same = 0;
K. Y. Srinivasanb0a93d92014-12-16 13:21:45 -08001634 }
1635
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001636 return 0;
1637}
1638
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001639static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1640 sector_t capacity, int *info)
1641{
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001642 sector_t nsect = capacity;
1643 sector_t cylinders = nsect;
1644 int heads, sectors_pt;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001645
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001646 /*
1647 * We are making up these values; let us keep it simple.
1648 */
1649 heads = 0xff;
1650 sectors_pt = 0x3f; /* Sectors per track */
1651 sector_div(cylinders, heads * sectors_pt);
1652 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1653 cylinders = 0xffff;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001654
1655 info[0] = heads;
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001656 info[1] = sectors_pt;
1657 info[2] = (int)cylinders;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001658
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001659 return 0;
1660}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001661
K. Y. Srinivasan4b270c82012-01-12 12:37:58 -08001662static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001663{
K. Y. Srinivasan4b270c82012-01-12 12:37:58 -08001664 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1665 struct hv_device *device = host_dev->dev;
1666
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001667 struct storvsc_device *stor_device;
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001668 struct storvsc_cmd_request *request;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001669 struct vstor_packet *vstor_packet;
1670 int ret, t;
1671
K. Y. Srinivasan1eaaddf2011-08-27 11:31:03 -07001672 stor_device = get_out_stor_device(device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001673 if (!stor_device)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001674 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001675
1676 request = &stor_device->reset_request;
1677 vstor_packet = &request->vstor_packet;
Andres Beltran0a765662020-07-06 12:09:28 -04001678 memset(vstor_packet, 0, sizeof(struct vstor_packet));
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001679
1680 init_completion(&request->wait_event);
1681
1682 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1683 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1684 vstor_packet->vm_srb.path_id = stor_device->path_id;
1685
1686 ret = vmbus_sendpacket(device->channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001687 (sizeof(struct vstor_packet) -
Andrea Parri (Microsoft)244808e2020-12-17 21:33:20 +01001688 stor_device->vmscsi_size_delta),
Andrea Parri (Microsoft)bf5fd8c2021-05-10 23:08:41 +02001689 VMBUS_RQST_RESET,
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001690 VM_PKT_DATA_INBAND,
1691 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1692 if (ret != 0)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001693 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001694
K. Y. Srinivasan46d2eb62011-06-16 13:16:36 -07001695 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001696 if (t == 0)
1697 return TIMEOUT_ERROR;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001698
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001699
1700 /*
1701 * At this point, all outstanding requests in the adapter
1702 * should have been flushed out and return to us
K. Y. Srinivasan5c1b10a2012-10-02 11:03:31 -07001703 * There is a potential race here where the host may be in
1704 * the process of responding when we return from here.
1705 * Just wait for all in-transit packets to be accounted for
1706 * before we return from here.
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001707 */
K. Y. Srinivasan5c1b10a2012-10-02 11:03:31 -07001708 storvsc_wait_to_drain(stor_device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001709
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001710 return SUCCESS;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001711}
1712
K. Y. Srinivasan56b26e62014-07-12 09:48:30 -07001713/*
1714 * The host guarantees to respond to each command, although I/O latencies might
1715 * be unbounded on Azure. Reset the timer unconditionally to give the host a
1716 * chance to perform EH.
1717 */
1718static enum blk_eh_timer_return storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
1719{
Long Li3930d732017-06-13 14:34:05 -07001720#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1721 if (scmnd->device->host->transportt == fc_transport_template)
1722 return fc_eh_timed_out(scmnd);
1723#endif
K. Y. Srinivasan56b26e62014-07-12 09:48:30 -07001724 return BLK_EH_RESET_TIMER;
1725}
1726
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001727static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001728{
1729 bool allowed = true;
1730 u8 scsi_op = scmnd->cmnd[0];
1731
1732 switch (scsi_op) {
Olaf Hering3e8f4f42013-02-21 12:04:51 -08001733 /* the host does not handle WRITE_SAME, log accident usage */
1734 case WRITE_SAME:
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001735 /*
1736 * smartd sends this command and the host does not handle
1737 * this. So, don't send it.
1738 */
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001739 case SET_WINDOW:
Michael Kelleyd4674852021-06-04 10:21:01 -07001740 set_host_byte(scmnd, DID_ERROR);
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001741 allowed = false;
1742 break;
1743 default:
1744 break;
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001745 }
1746 return allowed;
1747}
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001748
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001749static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001750{
1751 int ret;
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001752 struct hv_host_device *host_dev = shost_priv(host);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001753 struct hv_device *dev = host_dev->dev;
Christoph Hellwigead37002014-12-29 20:05:09 -08001754 struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001755 struct scatterlist *sgl;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001756 struct vmscsi_request *vm_srb;
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001757 struct vmbus_packet_mpb_array *payload;
1758 u32 payload_sz;
1759 u32 length;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001760
Keith Mange2492fd72015-08-13 08:43:47 -07001761 if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) {
K. Y. Srinivasan8caf92d2014-07-12 09:48:28 -07001762 /*
1763 * On legacy hosts filter unimplemented commands.
1764 * Future hosts are expected to correctly handle
1765 * unsupported commands. Furthermore, it is
1766 * possible that some of the currently
1767 * unsupported commands maybe supported in
1768 * future versions of the host.
1769 */
1770 if (!storvsc_scsi_cmd_ok(scmnd)) {
Bart Van Assche0c31fa02021-10-07 13:29:08 -07001771 scsi_done(scmnd);
K. Y. Srinivasan8caf92d2014-07-12 09:48:28 -07001772 return 0;
1773 }
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001774 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001775
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001776 /* Setup the cmd request */
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001777 cmd_request->cmd = scmnd;
1778
Andres Beltran0a765662020-07-06 12:09:28 -04001779 memset(&cmd_request->vstor_packet, 0, sizeof(struct vstor_packet));
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001780 vm_srb = &cmd_request->vstor_packet.vm_srb;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001781 vm_srb->win8_extension.time_out_value = 60;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001782
K. Y. Srinivasanf885fb72014-07-12 09:48:31 -07001783 vm_srb->win8_extension.srb_flags |=
K. Y. Srinivasan8cf308e2015-08-31 08:21:54 -07001784 SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001785
Long Li3cd6d3d2016-12-14 18:46:01 -08001786 if (scmnd->device->tagged_supported) {
1787 vm_srb->win8_extension.srb_flags |=
1788 (SRB_FLAGS_QUEUE_ACTION_ENABLE | SRB_FLAGS_NO_QUEUE_FREEZE);
1789 vm_srb->win8_extension.queue_tag = SP_UNTAGGED;
1790 vm_srb->win8_extension.queue_action = SRB_SIMPLE_TAG_REQUEST;
1791 }
1792
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001793 /* Build the SRB */
1794 switch (scmnd->sc_data_direction) {
1795 case DMA_TO_DEVICE:
1796 vm_srb->data_in = WRITE_TYPE;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001797 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001798 break;
1799 case DMA_FROM_DEVICE:
1800 vm_srb->data_in = READ_TYPE;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001801 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001802 break;
Vitaly Kuznetsovcb1cf082015-06-25 18:12:11 +02001803 case DMA_NONE:
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001804 vm_srb->data_in = UNKNOWN_TYPE;
K. Y. Srinivasandc457082015-05-01 11:03:02 -07001805 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001806 break;
Vitaly Kuznetsovcb1cf082015-06-25 18:12:11 +02001807 default:
1808 /*
1809 * This is DMA_BIDIRECTIONAL or something else we are never
1810 * supposed to see here.
1811 */
1812 WARN(1, "Unexpected data direction: %d\n",
1813 scmnd->sc_data_direction);
1814 return -EINVAL;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001815 }
1816
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001817
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001818 vm_srb->port_number = host_dev->port;
1819 vm_srb->path_id = scmnd->device->channel;
1820 vm_srb->target_id = scmnd->device->id;
1821 vm_srb->lun = scmnd->device->lun;
1822
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001823 vm_srb->cdb_length = scmnd->cmd_len;
1824
1825 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1826
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001827 sgl = (struct scatterlist *)scsi_sglist(scmnd);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001828
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001829 length = scsi_bufflen(scmnd);
1830 payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
1831 payload_sz = sizeof(cmd_request->mpb);
1832
YueHaibing0bd2fbe2021-12-27 12:03:11 +08001833 if (scsi_sg_count(scmnd)) {
Michael Kelley3d9c3dc2021-02-23 14:44:54 -08001834 unsigned long offset_in_hvpg = offset_in_hvpage(sgl->offset);
Boqun Feng8f437102020-09-16 11:48:17 +08001835 unsigned int hvpg_count = HVPFN_UP(offset_in_hvpg + length);
Tianyu Lan743b2372021-12-13 02:14:05 -05001836 struct scatterlist *sg;
1837 unsigned long hvpfn, hvpfns_to_add;
YueHaibing0bd2fbe2021-12-27 12:03:11 +08001838 int j, i = 0, sg_count;
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001839
Boqun Feng8f437102020-09-16 11:48:17 +08001840 if (hvpg_count > MAX_PAGE_BUFFER_COUNT) {
1841
1842 payload_sz = (hvpg_count * sizeof(u64) +
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001843 sizeof(struct vmbus_packet_mpb_array));
Cathy Averyb0120d92016-11-23 08:46:33 -05001844 payload = kzalloc(payload_sz, GFP_ATOMIC);
Vitaly Kuznetsov81988a02015-10-01 14:53:17 -07001845 if (!payload)
1846 return SCSI_MLQUEUE_DEVICE_BUSY;
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001847 }
1848
1849 payload->range.len = length;
Boqun Feng8f437102020-09-16 11:48:17 +08001850 payload->range.offset = offset_in_hvpg;
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001851
Tianyu Lan743b2372021-12-13 02:14:05 -05001852 sg_count = scsi_dma_map(scmnd);
Juan Vazquez4eea5332022-01-08 16:17:58 -08001853 if (sg_count < 0) {
1854 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1855 goto err_free_payload;
1856 }
Michael Kelley3d9c3dc2021-02-23 14:44:54 -08001857
Tianyu Lan743b2372021-12-13 02:14:05 -05001858 for_each_sg(sgl, sg, sg_count, j) {
Boqun Feng8f437102020-09-16 11:48:17 +08001859 /*
Tianyu Lan743b2372021-12-13 02:14:05 -05001860 * Init values for the current sgl entry. hvpfns_to_add
1861 * is in units of Hyper-V size pages. Handling the
1862 * PAGE_SIZE != HV_HYP_PAGE_SIZE case also handles
1863 * values of sgl->offset that are larger than PAGE_SIZE.
1864 * Such offsets are handled even on other than the first
1865 * sgl entry, provided they are a multiple of PAGE_SIZE.
Boqun Feng8f437102020-09-16 11:48:17 +08001866 */
Tianyu Lan743b2372021-12-13 02:14:05 -05001867 hvpfn = HVPFN_DOWN(sg_dma_address(sg));
1868 hvpfns_to_add = HVPFN_UP(sg_dma_address(sg) +
1869 sg_dma_len(sg)) - hvpfn;
Boqun Feng8f437102020-09-16 11:48:17 +08001870
1871 /*
Michael Kelley3d9c3dc2021-02-23 14:44:54 -08001872 * Fill the next portion of the PFN array with
1873 * sequential Hyper-V PFNs for the continguous physical
1874 * memory described by the sgl entry. The end of the
1875 * last sgl should be reached at the same time that
1876 * the PFN array is filled.
Boqun Feng8f437102020-09-16 11:48:17 +08001877 */
Michael Kelley3d9c3dc2021-02-23 14:44:54 -08001878 while (hvpfns_to_add--)
Tianyu Lan743b2372021-12-13 02:14:05 -05001879 payload->range.pfn_array[i++] = hvpfn++;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -07001880 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001881 }
1882
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001883 cmd_request->payload = payload;
1884 cmd_request->payload_sz = payload_sz;
1885
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001886 /* Invokes the vsc to start an IO */
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08001887 ret = storvsc_do_io(dev, cmd_request, get_cpu());
1888 put_cpu();
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -07001889
K. Y. Srinivasand2598f02011-08-25 09:48:58 -07001890 if (ret == -EAGAIN) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001891 /* no more space */
Juan Vazquez4eea5332022-01-08 16:17:58 -08001892 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1893 goto err_free_payload;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001894 }
1895
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001896 return 0;
Juan Vazquez4eea5332022-01-08 16:17:58 -08001897
1898err_free_payload:
1899 if (payload_sz > sizeof(cmd_request->mpb))
1900 kfree(payload);
1901
1902 return ret;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001903}
1904
Hank Janssenbef4a342009-07-13 16:01:31 -07001905static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001906 .module = THIS_MODULE,
1907 .name = "storvsc_host_t",
Christoph Hellwigead37002014-12-29 20:05:09 -08001908 .cmd_size = sizeof(struct storvsc_cmd_request),
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001909 .bios_param = storvsc_get_chs,
1910 .queuecommand = storvsc_queuecommand,
1911 .eh_host_reset_handler = storvsc_host_reset_handler,
Christoph Hellwigead37002014-12-29 20:05:09 -08001912 .proc_name = "storvsc_host",
K. Y. Srinivasan56b26e62014-07-12 09:48:30 -07001913 .eh_timed_out = storvsc_eh_timed_out,
Stephen Hemmingerf1c635b2017-03-07 09:15:53 -08001914 .slave_alloc = storvsc_device_alloc,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001915 .slave_configure = storvsc_device_configure,
Michael Kelley (EOSG)cabe92a2018-01-24 22:49:57 +00001916 .cmd_per_lun = 2048,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001917 .this_id = -1,
Christoph Hellwig83eed452019-06-17 14:19:56 +02001918 /* Ensure there are no gaps in presented sgls */
1919 .virt_boundary_mask = PAGE_SIZE-1,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04001920 .no_write_same = 1,
K. Y. Srinivasanf64dad22016-12-14 18:45:58 -08001921 .track_queue_depth = 1,
Branden Bonabyadfbd022019-06-14 19:48:22 -04001922 .change_queue_depth = storvsc_change_queue_depth,
Hank Janssenbef4a342009-07-13 16:01:31 -07001923};
1924
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001925enum {
1926 SCSI_GUID,
1927 IDE_GUID,
K. Y. Srinivasanbde6d0f2013-06-04 12:05:08 -07001928 SFC_GUID,
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001929};
1930
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001931static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001932 /* SCSI guid */
K. Y. Srinivasan35c3bc22013-01-23 17:42:43 -08001933 { HV_SCSI_GUID,
1934 .driver_data = SCSI_GUID
1935 },
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001936 /* IDE guid */
K. Y. Srinivasan35c3bc22013-01-23 17:42:43 -08001937 { HV_IDE_GUID,
1938 .driver_data = IDE_GUID
1939 },
K. Y. Srinivasanbde6d0f2013-06-04 12:05:08 -07001940 /* Fibre Channel GUID */
1941 {
1942 HV_SYNTHFC_GUID,
1943 .driver_data = SFC_GUID
1944 },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001945 { },
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001946};
Hank Janssenbef4a342009-07-13 16:01:31 -07001947
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001948MODULE_DEVICE_TABLE(vmbus, id_table);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001949
Dexuan Cui56fb1052019-09-11 23:35:16 +00001950static const struct { guid_t guid; } fc_guid = { HV_SYNTHFC_GUID };
1951
1952static bool hv_dev_is_fc(struct hv_device *hv_dev)
1953{
1954 return guid_equal(&fc_guid.guid, &hv_dev->dev_type);
1955}
1956
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001957static int storvsc_probe(struct hv_device *device,
1958 const struct hv_vmbus_device_id *dev_id)
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001959{
1960 int ret;
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -07001961 int num_cpus = num_online_cpus();
Melanie Plageman (Microsoft)a81a38c2021-02-24 23:29:48 +00001962 int num_present_cpus = num_present_cpus();
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001963 struct Scsi_Host *host;
1964 struct hv_host_device *host_dev;
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001965 bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
K. Y. Srinivasandac58242015-12-23 13:15:49 -08001966 bool is_fc = ((dev_id->driver_data == SFC_GUID) ? true : false);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001967 int target = 0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001968 struct storvsc_device *stor_device;
K. Y. Srinivasan0fb8db22014-12-16 13:21:44 -08001969 int max_luns_per_target;
1970 int max_targets;
1971 int max_channels;
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -07001972 int max_sub_channels = 0;
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001973
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001974 /*
1975 * Based on the windows host we are running on,
1976 * set state to properly communicate with the host.
1977 */
1978
Keith Mange6ee5c612015-08-13 08:43:46 -07001979 if (vmbus_proto_version < VERSION_WIN8) {
K. Y. Srinivasan0fb8db22014-12-16 13:21:44 -08001980 max_luns_per_target = STORVSC_IDE_MAX_LUNS_PER_TARGET;
1981 max_targets = STORVSC_IDE_MAX_TARGETS;
1982 max_channels = STORVSC_IDE_MAX_CHANNELS;
Keith Mange6ee5c612015-08-13 08:43:46 -07001983 } else {
K. Y. Srinivasan0fb8db22014-12-16 13:21:44 -08001984 max_luns_per_target = STORVSC_MAX_LUNS_PER_TARGET;
1985 max_targets = STORVSC_MAX_TARGETS;
1986 max_channels = STORVSC_MAX_CHANNELS;
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -07001987 /*
Long Lif2862992018-03-22 14:47:18 -07001988 * On Windows8 and above, we support sub-channels for storage
1989 * on SCSI and FC controllers.
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -07001990 * The number of sub-channels offerred is based on the number of
1991 * VCPUs in the guest.
1992 */
Long Lif2862992018-03-22 14:47:18 -07001993 if (!dev_is_ide)
1994 max_sub_channels =
1995 (num_cpus - 1) / storvsc_vcpus_per_sub_channel;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001996 }
1997
Long Li2217a472018-04-19 14:54:24 -07001998 scsi_driver.can_queue = max_outstanding_req_per_channel *
1999 (max_sub_channels + 1) *
2000 (100 - ring_avail_percent_lowater) / 100;
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -07002001
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07002002 host = scsi_host_alloc(&scsi_driver,
2003 sizeof(struct hv_host_device));
2004 if (!host)
2005 return -ENOMEM;
2006
K. Y. Srinivasan7f33f302011-11-08 09:01:44 -08002007 host_dev = shost_priv(host);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07002008 memset(host_dev, 0, sizeof(struct hv_host_device));
2009
2010 host_dev->port = host->host_no;
2011 host_dev->dev = device;
Long Lic58cc702017-10-31 14:58:08 -07002012 host_dev->host = host;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07002013
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08002014
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07002015 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07002016 if (!stor_device) {
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08002017 ret = -ENOMEM;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08002018 goto err_out0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07002019 }
2020
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07002021 stor_device->destroy = false;
2022 init_waitqueue_head(&stor_device->waiting_to_drain);
2023 stor_device->device = device;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07002024 stor_device->host = host;
Andrea Parri (Microsoft)244808e2020-12-17 21:33:20 +01002025 stor_device->vmscsi_size_delta = sizeof(struct vmscsi_win8_extension);
Andrea Parri (Microsoft)21d20522020-06-17 18:46:41 +02002026 spin_lock_init(&stor_device->lock);
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07002027 hv_set_drvdata(device, stor_device);
Tianyu Lan743b2372021-12-13 02:14:05 -05002028 dma_set_min_align_mask(&device->device, HV_HYP_PAGE_SIZE - 1);
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07002029
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07002030 stor_device->port_number = host->host_no;
K. Y. Srinivasandac58242015-12-23 13:15:49 -08002031 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size, is_fc);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08002032 if (ret)
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08002033 goto err_out1;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07002034
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07002035 host_dev->path = stor_device->path_id;
2036 host_dev->target = stor_device->target_id;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07002037
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -07002038 switch (dev_id->driver_data) {
2039 case SFC_GUID:
2040 host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
2041 host->max_id = STORVSC_FC_MAX_TARGETS;
2042 host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
K. Y. Srinivasandac58242015-12-23 13:15:49 -08002043#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2044 host->transportt = fc_transport_template;
2045#endif
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -07002046 break;
2047
2048 case SCSI_GUID:
K. Y. Srinivasan0fb8db22014-12-16 13:21:44 -08002049 host->max_lun = max_luns_per_target;
2050 host->max_id = max_targets;
2051 host->max_channel = max_channels - 1;
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -07002052 break;
2053
2054 default:
2055 host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
2056 host->max_id = STORVSC_IDE_MAX_TARGETS;
2057 host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
2058 break;
2059 }
Mike Sterlingcf55f4a2011-09-06 16:10:55 -07002060 /* max cmd length */
2061 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07002062
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07002063 /*
2064 * set the table size based on the info we got
2065 * from the host.
2066 */
2067 host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08002068 /*
Long Li7b571c12020-01-13 16:08:36 -08002069 * For non-IDE disks, the host supports multiple channels.
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08002070 * Set the number of HW queues we are supporting.
2071 */
Melanie Plageman (Microsoft)a81a38c2021-02-24 23:29:48 +00002072 if (!dev_is_ide) {
2073 if (storvsc_max_hw_queues > num_present_cpus) {
2074 storvsc_max_hw_queues = 0;
2075 storvsc_log(device, STORVSC_LOGGING_WARN,
2076 "Resetting invalid storvsc_max_hw_queues value to default.\n");
2077 }
2078 if (storvsc_max_hw_queues)
2079 host->nr_hw_queues = storvsc_max_hw_queues;
2080 else
2081 host->nr_hw_queues = num_present_cpus;
2082 }
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07002083
Cathy Avery436ad942017-10-31 08:52:06 -04002084 /*
2085 * Set the error handler work queue.
2086 */
2087 host_dev->handle_error_wq =
2088 alloc_ordered_workqueue("storvsc_error_wq_%d",
2089 WQ_MEM_RECLAIM,
2090 host->host_no);
Jing Xiangfeng6112ff42020-11-27 11:02:06 +08002091 if (!host_dev->handle_error_wq) {
2092 ret = -ENOMEM;
Cathy Avery436ad942017-10-31 08:52:06 -04002093 goto err_out2;
Jing Xiangfeng6112ff42020-11-27 11:02:06 +08002094 }
Long Lic58cc702017-10-31 14:58:08 -07002095 INIT_WORK(&host_dev->host_scan_work, storvsc_host_scan);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07002096 /* Register the HBA and start the scsi bus scan */
2097 ret = scsi_add_host(host, &device->device);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07002098 if (ret != 0)
Cathy Avery436ad942017-10-31 08:52:06 -04002099 goto err_out3;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07002100
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07002101 if (!dev_is_ide) {
2102 scsi_scan_host(host);
K. Y. Srinivasan59d22952012-01-12 12:37:55 -08002103 } else {
2104 target = (device->dev_instance.b[5] << 8 |
2105 device->dev_instance.b[4]);
2106 ret = scsi_add_device(host, 0, target, 0);
Cathy Averydaf0cd42017-04-17 14:37:46 -04002107 if (ret)
Cathy Avery436ad942017-10-31 08:52:06 -04002108 goto err_out4;
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07002109 }
K. Y. Srinivasandac58242015-12-23 13:15:49 -08002110#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2111 if (host->transportt == fc_transport_template) {
Cathy Averydaf0cd42017-04-17 14:37:46 -04002112 struct fc_rport_identifiers ids = {
2113 .roles = FC_PORT_ROLE_FCP_DUMMY_INITIATOR,
2114 };
2115
K. Y. Srinivasandac58242015-12-23 13:15:49 -08002116 fc_host_node_name(host) = stor_device->node_name;
2117 fc_host_port_name(host) = stor_device->port_name;
Cathy Averydaf0cd42017-04-17 14:37:46 -04002118 stor_device->rport = fc_remote_port_add(host, 0, &ids);
Dan Carpenterca8dc692018-01-16 13:40:22 +03002119 if (!stor_device->rport) {
2120 ret = -ENOMEM;
Cathy Avery436ad942017-10-31 08:52:06 -04002121 goto err_out4;
Dan Carpenterca8dc692018-01-16 13:40:22 +03002122 }
K. Y. Srinivasandac58242015-12-23 13:15:49 -08002123 }
2124#endif
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07002125 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07002126
Cathy Avery436ad942017-10-31 08:52:06 -04002127err_out4:
Cathy Averydaf0cd42017-04-17 14:37:46 -04002128 scsi_remove_host(host);
2129
Cathy Avery436ad942017-10-31 08:52:06 -04002130err_out3:
2131 destroy_workqueue(host_dev->handle_error_wq);
2132
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08002133err_out2:
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08002134 /*
2135 * Once we have connected with the host, we would need to
2136 * to invoke storvsc_dev_remove() to rollback this state and
2137 * this call also frees up the stor_device; hence the jump around
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08002138 * err_out1 label.
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08002139 */
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07002140 storvsc_dev_remove(device);
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08002141 goto err_out0;
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08002142
2143err_out1:
K. Y. Srinivasand86adf42016-12-14 18:46:00 -08002144 kfree(stor_device->stor_chns);
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08002145 kfree(stor_device);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08002146
2147err_out0:
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07002148 scsi_host_put(host);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08002149 return ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07002150}
2151
Branden Bonabyadfbd022019-06-14 19:48:22 -04002152/* Change a scsi target's queue depth */
2153static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth)
2154{
2155 if (queue_depth > scsi_driver.can_queue)
2156 queue_depth = scsi_driver.can_queue;
2157
2158 return scsi_change_queue_depth(sdev, queue_depth);
2159}
2160
K. Y. Srinivasanddcbf652012-01-12 12:37:59 -08002161static int storvsc_remove(struct hv_device *dev)
2162{
2163 struct storvsc_device *stor_device = hv_get_drvdata(dev);
2164 struct Scsi_Host *host = stor_device->host;
Cathy Avery436ad942017-10-31 08:52:06 -04002165 struct hv_host_device *host_dev = shost_priv(host);
K. Y. Srinivasanddcbf652012-01-12 12:37:59 -08002166
K. Y. Srinivasandac58242015-12-23 13:15:49 -08002167#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
Cathy Averydaf0cd42017-04-17 14:37:46 -04002168 if (host->transportt == fc_transport_template) {
2169 fc_remote_port_delete(stor_device->rport);
K. Y. Srinivasandac58242015-12-23 13:15:49 -08002170 fc_remove_host(host);
Cathy Averydaf0cd42017-04-17 14:37:46 -04002171 }
K. Y. Srinivasandac58242015-12-23 13:15:49 -08002172#endif
Cathy Avery436ad942017-10-31 08:52:06 -04002173 destroy_workqueue(host_dev->handle_error_wq);
K. Y. Srinivasanddcbf652012-01-12 12:37:59 -08002174 scsi_remove_host(host);
2175 storvsc_dev_remove(dev);
2176 scsi_host_put(host);
2177
2178 return 0;
2179}
2180
Dexuan Cui56fb1052019-09-11 23:35:16 +00002181static int storvsc_suspend(struct hv_device *hv_dev)
2182{
2183 struct storvsc_device *stor_device = hv_get_drvdata(hv_dev);
2184 struct Scsi_Host *host = stor_device->host;
2185 struct hv_host_device *host_dev = shost_priv(host);
2186
2187 storvsc_wait_to_drain(stor_device);
2188
2189 drain_workqueue(host_dev->handle_error_wq);
2190
2191 vmbus_close(hv_dev->channel);
2192
Dexuan Cui56fb1052019-09-11 23:35:16 +00002193 kfree(stor_device->stor_chns);
2194 stor_device->stor_chns = NULL;
2195
2196 cpumask_clear(&stor_device->alloced_cpus);
2197
2198 return 0;
2199}
2200
2201static int storvsc_resume(struct hv_device *hv_dev)
2202{
2203 int ret;
2204
2205 ret = storvsc_connect_to_vsp(hv_dev, storvsc_ringbuffer_size,
2206 hv_dev_is_fc(hv_dev));
2207 return ret;
2208}
2209
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07002210static struct hv_driver storvsc_drv = {
K. Y. Srinivasanfafb0ef2011-11-08 09:01:46 -08002211 .name = KBUILD_MODNAME,
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07002212 .id_table = id_table,
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07002213 .probe = storvsc_probe,
2214 .remove = storvsc_remove,
Dexuan Cui56fb1052019-09-11 23:35:16 +00002215 .suspend = storvsc_suspend,
2216 .resume = storvsc_resume,
Arjan van de Venaf0a5642018-06-05 13:37:49 -07002217 .driver = {
2218 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
2219 },
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -07002220};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07002221
K. Y. Srinivasandac58242015-12-23 13:15:49 -08002222#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2223static struct fc_function_template fc_transport_functions = {
2224 .show_host_node_name = 1,
2225 .show_host_port_name = 1,
2226};
2227#endif
2228
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07002229static int __init storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07002230{
K. Y. Srinivasandac58242015-12-23 13:15:49 -08002231 int ret;
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07002232
2233 /*
2234 * Divide the ring buffer data size (which is 1 page less
2235 * than the ring buffer size since that page is reserved for
2236 * the ring buffer indices) by the max request size (which is
2237 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
Andrea Parri (Microsoft)ab548fd2020-12-17 21:33:19 +01002238 *
2239 * The computation underestimates max_outstanding_req_per_channel
2240 * for Win7 and older hosts because it does not take into account
2241 * the vmscsi_size_delta correction to the max request size.
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07002242 */
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07002243 max_outstanding_req_per_channel =
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07002244 ((storvsc_ringbuffer_size - PAGE_SIZE) /
2245 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
Andrea Parri (Microsoft)ab548fd2020-12-17 21:33:19 +01002246 sizeof(struct vstor_packet) + sizeof(u64),
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07002247 sizeof(u64)));
Hank Janssenbef4a342009-07-13 16:01:31 -07002248
K. Y. Srinivasandac58242015-12-23 13:15:49 -08002249#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2250 fc_transport_template = fc_attach_transport(&fc_transport_functions);
2251 if (!fc_transport_template)
2252 return -ENODEV;
2253#endif
2254
2255 ret = vmbus_driver_register(&storvsc_drv);
2256
2257#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2258 if (ret)
2259 fc_release_transport(fc_transport_template);
2260#endif
2261
2262 return ret;
Hank Janssenbef4a342009-07-13 16:01:31 -07002263}
2264
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07002265static void __exit storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07002266{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07002267 vmbus_driver_unregister(&storvsc_drv);
K. Y. Srinivasandac58242015-12-23 13:15:49 -08002268#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2269 fc_release_transport(fc_transport_template);
2270#endif
Hank Janssenbef4a342009-07-13 16:01:31 -07002271}
2272
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07002273MODULE_LICENSE("GPL");
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -07002274MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07002275module_init(storvsc_drv_init);
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07002276module_exit(storvsc_drv_exit);