blob: 40c43aeb4ff364898f160d7528d93ea4ef46a00a [file] [log] [blame]
Hank Janssenbef4a342009-07-13 16:01:31 -07001/*
Hank Janssenbef4a342009-07-13 16:01:31 -07002 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
K. Y. Srinivasan972621c2011-05-10 07:54:19 -070020 * K. Y. Srinivasan <kys@microsoft.com>
Hank Janssenbef4a342009-07-13 16:01:31 -070021 */
K. Y. Srinivasana1be1702011-08-27 11:31:23 -070022
23#include <linux/kernel.h>
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070024#include <linux/wait.h>
K. Y. Srinivasana1be1702011-08-27 11:31:23 -070025#include <linux/sched.h>
26#include <linux/completion.h>
27#include <linux/string.h>
28#include <linux/mm.h>
29#include <linux/delay.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070030#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070032#include <linux/module.h>
33#include <linux/device.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070034#include <linux/hyperv.h>
K. Y. Srinivasan56b26e62014-07-12 09:48:30 -070035#include <linux/blkdev.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070036#include <scsi/scsi.h>
37#include <scsi/scsi_cmnd.h>
38#include <scsi/scsi_host.h>
39#include <scsi/scsi_device.h>
40#include <scsi/scsi_tcq.h>
41#include <scsi/scsi_eh.h>
42#include <scsi/scsi_devinfo.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070043#include <scsi/scsi_dbg.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070044
K. Y. Srinivasanc6491142012-01-12 12:38:03 -080045/*
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -080046 * All wire protocol details (storage protocol between the guest and the host)
47 * are consolidated here.
48 *
49 * Begin protocol definitions.
K. Y. Srinivasanc6491142012-01-12 12:38:03 -080050 */
51
K. Y. Srinivasan09f03552012-01-12 12:37:54 -080052/*
53 * Version history:
54 * V1 Beta: 0.1
55 * V1 RC < 2008/1/31: 1.0
56 * V1 RC > 2008/1/31: 2.0
57 * Win7: 4.2
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -070058 * Win8: 5.1
Keith Mange1a363102015-08-13 08:43:48 -070059 * Win8.1: 6.0
60 * Win10: 6.2
K. Y. Srinivasan09f03552012-01-12 12:37:54 -080061 */
62
Keith Mange2492fd72015-08-13 08:43:47 -070063#define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
64 (((MINOR_) & 0xff)))
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -070065
Keith Mange1a363102015-08-13 08:43:48 -070066#define VMSTOR_PROTO_VERSION_WIN6 VMSTOR_PROTO_VERSION(2, 0)
Keith Mange2492fd72015-08-13 08:43:47 -070067#define VMSTOR_PROTO_VERSION_WIN7 VMSTOR_PROTO_VERSION(4, 2)
68#define VMSTOR_PROTO_VERSION_WIN8 VMSTOR_PROTO_VERSION(5, 1)
Keith Mange1a363102015-08-13 08:43:48 -070069#define VMSTOR_PROTO_VERSION_WIN8_1 VMSTOR_PROTO_VERSION(6, 0)
70#define VMSTOR_PROTO_VERSION_WIN10 VMSTOR_PROTO_VERSION(6, 2)
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070071
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070072/* Packet structure describing virtual storage requests. */
73enum vstor_packet_operation {
74 VSTOR_OPERATION_COMPLETE_IO = 1,
75 VSTOR_OPERATION_REMOVE_DEVICE = 2,
76 VSTOR_OPERATION_EXECUTE_SRB = 3,
77 VSTOR_OPERATION_RESET_LUN = 4,
78 VSTOR_OPERATION_RESET_ADAPTER = 5,
79 VSTOR_OPERATION_RESET_BUS = 6,
80 VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
81 VSTOR_OPERATION_END_INITIALIZATION = 8,
82 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
83 VSTOR_OPERATION_QUERY_PROPERTIES = 10,
K. Y. Srinivasan2b9525f2011-11-08 09:01:48 -080084 VSTOR_OPERATION_ENUMERATE_BUS = 11,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -070085 VSTOR_OPERATION_FCHBA_DATA = 12,
86 VSTOR_OPERATION_CREATE_SUB_CHANNELS = 13,
87 VSTOR_OPERATION_MAXIMUM = 13
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070088};
89
90/*
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -070091 * WWN packet for Fibre Channel HBA
92 */
93
94struct hv_fc_wwn_packet {
95 bool primary_active;
96 u8 reserved1;
97 u8 reserved2;
98 u8 primary_port_wwn[8];
99 u8 primary_node_wwn[8];
100 u8 secondary_port_wwn[8];
101 u8 secondary_node_wwn[8];
102};
103
104
105
106/*
107 * SRB Flag Bits
108 */
109
110#define SRB_FLAGS_QUEUE_ACTION_ENABLE 0x00000002
111#define SRB_FLAGS_DISABLE_DISCONNECT 0x00000004
112#define SRB_FLAGS_DISABLE_SYNCH_TRANSFER 0x00000008
113#define SRB_FLAGS_BYPASS_FROZEN_QUEUE 0x00000010
114#define SRB_FLAGS_DISABLE_AUTOSENSE 0x00000020
115#define SRB_FLAGS_DATA_IN 0x00000040
116#define SRB_FLAGS_DATA_OUT 0x00000080
117#define SRB_FLAGS_NO_DATA_TRANSFER 0x00000000
118#define SRB_FLAGS_UNSPECIFIED_DIRECTION (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT)
119#define SRB_FLAGS_NO_QUEUE_FREEZE 0x00000100
120#define SRB_FLAGS_ADAPTER_CACHE_ENABLE 0x00000200
121#define SRB_FLAGS_FREE_SENSE_BUFFER 0x00000400
122
123/*
124 * This flag indicates the request is part of the workflow for processing a D3.
125 */
126#define SRB_FLAGS_D3_PROCESSING 0x00000800
127#define SRB_FLAGS_IS_ACTIVE 0x00010000
128#define SRB_FLAGS_ALLOCATED_FROM_ZONE 0x00020000
129#define SRB_FLAGS_SGLIST_FROM_POOL 0x00040000
130#define SRB_FLAGS_BYPASS_LOCKED_QUEUE 0x00080000
131#define SRB_FLAGS_NO_KEEP_AWAKE 0x00100000
132#define SRB_FLAGS_PORT_DRIVER_ALLOCSENSE 0x00200000
133#define SRB_FLAGS_PORT_DRIVER_SENSEHASPORT 0x00400000
134#define SRB_FLAGS_DONT_START_NEXT_PACKET 0x00800000
135#define SRB_FLAGS_PORT_DRIVER_RESERVED 0x0F000000
136#define SRB_FLAGS_CLASS_DRIVER_RESERVED 0xF0000000
137
138
139/*
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700140 * Platform neutral description of a scsi request -
141 * this remains the same across the write regardless of 32/64 bit
142 * note: it's patterned off the SCSI_PASS_THROUGH structure
143 */
K. Y. Srinivasan6b2f9492012-01-12 12:38:05 -0800144#define STORVSC_MAX_CMD_LEN 0x10
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700145
146#define POST_WIN7_STORVSC_SENSE_BUFFER_SIZE 0x14
147#define PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE 0x12
148
149#define STORVSC_SENSE_BUFFER_SIZE 0x14
K. Y. Srinivasan6b2f9492012-01-12 12:38:05 -0800150#define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700151
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700152/*
153 * Sense buffer size changed in win8; have a run-time
Keith Mangecb11fead2015-08-13 08:43:49 -0700154 * variable to track the size we should use. This value will
155 * likely change during protocol negotiation but it is valid
156 * to start by assuming pre-Win8.
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700157 */
Keith Mangecb11fead2015-08-13 08:43:49 -0700158static int sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700159
160/*
Keith Mangecb11fead2015-08-13 08:43:49 -0700161 * The storage protocol version is determined during the
162 * initial exchange with the host. It will indicate which
163 * storage functionality is available in the host.
164*/
Keith Mange2492fd72015-08-13 08:43:47 -0700165static int vmstor_proto_version;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700166
167struct vmscsi_win8_extension {
168 /*
169 * The following were added in Windows 8
170 */
171 u16 reserve;
172 u8 queue_tag;
173 u8 queue_action;
174 u32 srb_flags;
175 u32 time_out_value;
176 u32 queue_sort_ey;
177} __packed;
178
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700179struct vmscsi_request {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800180 u16 length;
181 u8 srb_status;
182 u8 scsi_status;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700183
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800184 u8 port_number;
185 u8 path_id;
186 u8 target_id;
187 u8 lun;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700188
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800189 u8 cdb_length;
190 u8 sense_info_length;
191 u8 data_in;
192 u8 reserved;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700193
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800194 u32 data_transfer_length;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700195
196 union {
K. Y. Srinivasan6b2f9492012-01-12 12:38:05 -0800197 u8 cdb[STORVSC_MAX_CMD_LEN];
198 u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
199 u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700200 };
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700201 /*
202 * The following was added in win8.
203 */
204 struct vmscsi_win8_extension win8_extension;
205
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700206} __attribute((packed));
207
208
209/*
Keith Mangecb11fead2015-08-13 08:43:49 -0700210 * The size of the vmscsi_request has changed in win8. The
211 * additional size is because of new elements added to the
212 * structure. These elements are valid only when we are talking
213 * to a win8 host.
214 * Track the correction to size we need to apply. This value
215 * will likely change during protocol negotiation but it is
216 * valid to start by assuming pre-Win8.
217 */
218static int vmscsi_size_delta = sizeof(struct vmscsi_win8_extension);
219
220/*
Keith Mange1a363102015-08-13 08:43:48 -0700221 * The list of storage protocols in order of preference.
222 */
223struct vmstor_protocol {
224 int protocol_version;
225 int sense_buffer_size;
226 int vmscsi_size_delta;
227};
228
229
230static const struct vmstor_protocol vmstor_protocols[] = {
231 {
232 VMSTOR_PROTO_VERSION_WIN10,
233 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
234 0
235 },
236 {
237 VMSTOR_PROTO_VERSION_WIN8_1,
238 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
239 0
240 },
241 {
242 VMSTOR_PROTO_VERSION_WIN8,
243 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
244 0
245 },
246 {
247 VMSTOR_PROTO_VERSION_WIN7,
248 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
249 sizeof(struct vmscsi_win8_extension),
250 },
251 {
252 VMSTOR_PROTO_VERSION_WIN6,
253 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
254 sizeof(struct vmscsi_win8_extension),
255 }
256};
257
258
259/*
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700260 * This structure is sent during the intialization phase to get the different
261 * properties of the channel.
262 */
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700263
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700264#define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1
265
266struct vmstorage_channel_properties {
267 u32 reserved;
268 u16 max_channel_cnt;
269 u16 reserved1;
270
271 u32 flags;
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800272 u32 max_transfer_bytes;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700273
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700274 u64 reserved2;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700275} __packed;
276
277/* This structure is sent during the storage protocol negotiations. */
278struct vmstorage_protocol_version {
279 /* Major (MSW) and minor (LSW) version numbers. */
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800280 u16 major_minor;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700281
282 /*
283 * Revision number is auto-incremented whenever this file is changed
284 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
285 * definitely indicate incompatibility--but it does indicate mismatched
286 * builds.
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800287 * This is only used on the windows side. Just set it to 0.
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700288 */
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800289 u16 revision;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700290} __packed;
291
292/* Channel Property Flags */
293#define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
294#define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
295
296struct vstor_packet {
297 /* Requested operation type */
298 enum vstor_packet_operation operation;
299
300 /* Flags - see below for values */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800301 u32 flags;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700302
303 /* Status of the request returned from the server side. */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800304 u32 status;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700305
306 /* Data payload area */
307 union {
308 /*
309 * Structure used to forward SCSI commands from the
310 * client to the server.
311 */
312 struct vmscsi_request vm_srb;
313
314 /* Structure used to query channel properties. */
315 struct vmstorage_channel_properties storage_channel_properties;
316
317 /* Used during version negotiations. */
318 struct vmstorage_protocol_version version;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700319
320 /* Fibre channel address packet */
321 struct hv_fc_wwn_packet wwn_packet;
322
323 /* Number of sub-channels to create */
324 u16 sub_channel_count;
325
326 /* This will be the maximum of the union members */
327 u8 buffer[0x34];
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700328 };
329} __packed;
330
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700331/*
K. Y. Srinivasan09f03552012-01-12 12:37:54 -0800332 * Packet Flags:
333 *
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700334 * This flag indicates that the server should send back a completion for this
335 * packet.
336 */
K. Y. Srinivasan09f03552012-01-12 12:37:54 -0800337
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700338#define REQUEST_COMPLETION_FLAG 0x1
339
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700340/* Matches Windows-end */
341enum storvsc_request_type {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800342 WRITE_TYPE = 0,
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700343 READ_TYPE,
344 UNKNOWN_TYPE,
345};
346
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800347/*
348 * SRB status codes and masks; a subset of the codes used here.
349 */
350
351#define SRB_STATUS_AUTOSENSE_VALID 0x80
352#define SRB_STATUS_INVALID_LUN 0x20
353#define SRB_STATUS_SUCCESS 0x01
K. Y. Srinivasan67812092013-02-21 12:04:53 -0800354#define SRB_STATUS_ABORTED 0x02
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800355#define SRB_STATUS_ERROR 0x04
356
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800357/*
358 * This is the end of Protocol specific defines.
359 */
360
K. Y. Srinivasanb9ec3a52015-03-27 00:27:14 -0700361static int storvsc_ringbuffer_size = (256 * PAGE_SIZE);
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -0700362static u32 max_outstanding_req_per_channel;
363
364static int storvsc_vcpus_per_sub_channel = 4;
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800365
366module_param(storvsc_ringbuffer_size, int, S_IRUGO);
367MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
368
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -0700369module_param(storvsc_vcpus_per_sub_channel, int, S_IRUGO);
370MODULE_PARM_DESC(vcpus_per_sub_channel, "Ratio of VCPUs to subchannels");
K. Y. Srinivasan893def32013-06-04 12:05:05 -0700371/*
372 * Timeout in seconds for all devices managed by this driver.
373 */
374static int storvsc_timeout = 180;
375
K. Y. Srinivasanf3cfabc2014-07-21 16:06:01 -0700376static int msft_blist_flags = BLIST_TRY_VPD_PAGES;
377
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800378
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700379static void storvsc_on_channel_callback(void *context);
380
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -0700381#define STORVSC_MAX_LUNS_PER_TARGET 255
382#define STORVSC_MAX_TARGETS 2
383#define STORVSC_MAX_CHANNELS 8
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800384
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -0700385#define STORVSC_FC_MAX_LUNS_PER_TARGET 255
386#define STORVSC_FC_MAX_TARGETS 128
387#define STORVSC_FC_MAX_CHANNELS 8
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800388
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -0700389#define STORVSC_IDE_MAX_LUNS_PER_TARGET 64
390#define STORVSC_IDE_MAX_TARGETS 1
391#define STORVSC_IDE_MAX_CHANNELS 1
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800392
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800393struct storvsc_cmd_request {
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800394 struct scsi_cmnd *cmd;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700395
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800396 unsigned int bounce_sgl_count;
397 struct scatterlist *bounce_sgl;
398
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700399 struct hv_device *device;
400
401 /* Synchronize the request/response if needed */
402 struct completion wait_event;
403
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -0700404 struct vmbus_channel_packet_multipage_buffer mpb;
405 struct vmbus_packet_mpb_array *payload;
406 u32 payload_sz;
407
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700408 struct vstor_packet vstor_packet;
409};
410
411
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700412/* A storvsc device is a device object that contains a vmbus channel */
413struct storvsc_device {
414 struct hv_device *device;
415
416 bool destroy;
417 bool drain_notify;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700418 bool open_sub_channel;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700419 atomic_t num_outstanding_req;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700420 struct Scsi_Host *host;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700421
422 wait_queue_head_t waiting_to_drain;
423
424 /*
425 * Each unique Port/Path/Target represents 1 channel ie scsi
426 * controller. In reality, the pathid, targetid is always 0
427 * and the port is set by us
428 */
429 unsigned int port_number;
430 unsigned char path_id;
431 unsigned char target_id;
432
K. Y. Srinivasan5117b932015-03-27 00:27:17 -0700433 /*
434 * Max I/O, the device can support.
435 */
436 u32 max_transfer_bytes;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700437 /* Used for vsc/vsp channel reset process */
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800438 struct storvsc_cmd_request init_request;
439 struct storvsc_cmd_request reset_request;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700440};
441
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800442struct hv_host_device {
443 struct hv_device *dev;
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700444 unsigned int port;
445 unsigned char path;
446 unsigned char target;
447};
448
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800449struct storvsc_scan_work {
450 struct work_struct work;
451 struct Scsi_Host *host;
452 uint lun;
453};
454
K. Y. Srinivasan67812092013-02-21 12:04:53 -0800455static void storvsc_device_scan(struct work_struct *work)
456{
457 struct storvsc_scan_work *wrk;
458 uint lun;
459 struct scsi_device *sdev;
460
461 wrk = container_of(work, struct storvsc_scan_work, work);
462 lun = wrk->lun;
463
464 sdev = scsi_device_lookup(wrk->host, 0, 0, lun);
465 if (!sdev)
466 goto done;
467 scsi_rescan_device(&sdev->sdev_gendev);
468 scsi_device_put(sdev);
469
470done:
471 kfree(wrk);
472}
473
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -0800474static void storvsc_host_scan(struct work_struct *work)
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800475{
476 struct storvsc_scan_work *wrk;
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -0800477 struct Scsi_Host *host;
K. Y. Srinivasan34a716b2014-12-16 13:21:43 -0800478 struct scsi_device *sdev;
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800479
480 wrk = container_of(work, struct storvsc_scan_work, work);
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -0800481 host = wrk->host;
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800482
K. Y. Srinivasan34a716b2014-12-16 13:21:43 -0800483 /*
484 * Before scanning the host, first check to see if any of the
485 * currrently known devices have been hot removed. We issue a
486 * "unit ready" command against all currently known devices.
487 * This I/O will result in an error for devices that have been
488 * removed. As part of handling the I/O error, we remove the device.
489 *
490 * When a LUN is added or removed, the host sends us a signal to
491 * scan the host. Thus we are forced to discover the LUNs that
492 * may have been removed this way.
493 */
494 mutex_lock(&host->scan_mutex);
Vitaly Kuznetsov8d6a9f52015-07-01 11:31:27 +0200495 shost_for_each_device(sdev, host)
K. Y. Srinivasan34a716b2014-12-16 13:21:43 -0800496 scsi_test_unit_ready(sdev, 1, 1, NULL);
K. Y. Srinivasan34a716b2014-12-16 13:21:43 -0800497 mutex_unlock(&host->scan_mutex);
498 /*
499 * Now scan the host to discover LUNs that may have been added.
500 */
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -0800501 scsi_scan_host(host);
502
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800503 kfree(wrk);
504}
505
K. Y. Srinivasanb4017312011-11-08 09:01:50 -0800506static void storvsc_remove_lun(struct work_struct *work)
507{
508 struct storvsc_scan_work *wrk;
509 struct scsi_device *sdev;
510
511 wrk = container_of(work, struct storvsc_scan_work, work);
512 if (!scsi_host_get(wrk->host))
513 goto done;
514
515 sdev = scsi_device_lookup(wrk->host, 0, 0, wrk->lun);
516
517 if (sdev) {
518 scsi_remove_device(sdev);
519 scsi_device_put(sdev);
520 }
521 scsi_host_put(wrk->host);
522
523done:
524 kfree(wrk);
525}
526
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800527
528/*
K. Y. Srinivasana8c18c52012-01-12 12:38:00 -0800529 * We can get incoming messages from the host that are not in response to
530 * messages that we have sent out. An example of this would be messages
531 * received by the guest to notify dynamic addition/removal of LUNs. To
532 * deal with potential race conditions where the driver may be in the
533 * midst of being unloaded when we might receive an unsolicited message
534 * from the host, we have implemented a mechanism to gurantee sequential
535 * consistency:
536 *
537 * 1) Once the device is marked as being destroyed, we will fail all
538 * outgoing messages.
539 * 2) We permit incoming messages when the device is being destroyed,
540 * only to properly account for messages already sent out.
541 */
542
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700543static inline struct storvsc_device *get_out_stor_device(
544 struct hv_device *device)
545{
546 struct storvsc_device *stor_device;
547
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700548 stor_device = hv_get_drvdata(device);
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700549
550 if (stor_device && stor_device->destroy)
551 stor_device = NULL;
552
553 return stor_device;
554}
555
556
557static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
558{
559 dev->drain_notify = true;
560 wait_event(dev->waiting_to_drain,
561 atomic_read(&dev->num_outstanding_req) == 0);
562 dev->drain_notify = false;
563}
Hank Janssenbef4a342009-07-13 16:01:31 -0700564
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700565static inline struct storvsc_device *get_in_stor_device(
566 struct hv_device *device)
567{
568 struct storvsc_device *stor_device;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700569
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700570 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700571
572 if (!stor_device)
573 goto get_in_err;
574
575 /*
576 * If the device is being destroyed; allow incoming
577 * traffic only to cleanup outstanding requests.
578 */
579
580 if (stor_device->destroy &&
581 (atomic_read(&stor_device->num_outstanding_req) == 0))
582 stor_device = NULL;
583
584get_in_err:
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700585 return stor_device;
586
587}
588
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800589static void destroy_bounce_buffer(struct scatterlist *sgl,
590 unsigned int sg_count)
591{
592 int i;
593 struct page *page_buf;
594
595 for (i = 0; i < sg_count; i++) {
596 page_buf = sg_page((&sgl[i]));
597 if (page_buf != NULL)
598 __free_page(page_buf);
599 }
600
601 kfree(sgl);
602}
603
604static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
605{
606 int i;
607
608 /* No need to check */
609 if (sg_count < 2)
610 return -1;
611
612 /* We have at least 2 sg entries */
613 for (i = 0; i < sg_count; i++) {
614 if (i == 0) {
615 /* make sure 1st one does not have hole */
616 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
617 return i;
618 } else if (i == sg_count - 1) {
619 /* make sure last one does not have hole */
620 if (sgl[i].offset != 0)
621 return i;
622 } else {
623 /* make sure no hole in the middle */
624 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
625 return i;
626 }
627 }
628 return -1;
629}
630
631static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
632 unsigned int sg_count,
633 unsigned int len,
634 int write)
635{
636 int i;
637 int num_pages;
638 struct scatterlist *bounce_sgl;
639 struct page *page_buf;
640 unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);
641
642 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
643
644 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
645 if (!bounce_sgl)
646 return NULL;
647
K. Y. Srinivasan9d2696e2013-02-06 05:15:28 -0800648 sg_init_table(bounce_sgl, num_pages);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800649 for (i = 0; i < num_pages; i++) {
650 page_buf = alloc_page(GFP_ATOMIC);
651 if (!page_buf)
652 goto cleanup;
653 sg_set_page(&bounce_sgl[i], page_buf, buf_len, 0);
654 }
655
656 return bounce_sgl;
657
658cleanup:
659 destroy_bounce_buffer(bounce_sgl, num_pages);
660 return NULL;
661}
662
663/* Assume the original sgl has enough room */
664static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
665 struct scatterlist *bounce_sgl,
666 unsigned int orig_sgl_count,
667 unsigned int bounce_sgl_count)
668{
669 int i;
670 int j = 0;
671 unsigned long src, dest;
672 unsigned int srclen, destlen, copylen;
673 unsigned int total_copied = 0;
674 unsigned long bounce_addr = 0;
675 unsigned long dest_addr = 0;
676 unsigned long flags;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700677 struct scatterlist *cur_dest_sgl;
678 struct scatterlist *cur_src_sgl;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800679
680 local_irq_save(flags);
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700681 cur_dest_sgl = orig_sgl;
682 cur_src_sgl = bounce_sgl;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800683 for (i = 0; i < orig_sgl_count; i++) {
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700684 dest_addr = (unsigned long)
685 kmap_atomic(sg_page(cur_dest_sgl)) +
686 cur_dest_sgl->offset;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800687 dest = dest_addr;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700688 destlen = cur_dest_sgl->length;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800689
690 if (bounce_addr == 0)
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700691 bounce_addr = (unsigned long)kmap_atomic(
692 sg_page(cur_src_sgl));
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800693
694 while (destlen) {
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700695 src = bounce_addr + cur_src_sgl->offset;
696 srclen = cur_src_sgl->length - cur_src_sgl->offset;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800697
698 copylen = min(srclen, destlen);
699 memcpy((void *)dest, (void *)src, copylen);
700
701 total_copied += copylen;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700702 cur_src_sgl->offset += copylen;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800703 destlen -= copylen;
704 dest += copylen;
705
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700706 if (cur_src_sgl->offset == cur_src_sgl->length) {
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800707 /* full */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700708 kunmap_atomic((void *)bounce_addr);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800709 j++;
710
711 /*
712 * It is possible that the number of elements
713 * in the bounce buffer may not be equal to
714 * the number of elements in the original
715 * scatter list. Handle this correctly.
716 */
717
718 if (j == bounce_sgl_count) {
719 /*
720 * We are done; cleanup and return.
721 */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700722 kunmap_atomic((void *)(dest_addr -
723 cur_dest_sgl->offset));
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800724 local_irq_restore(flags);
725 return total_copied;
726 }
727
728 /* if we need to use another bounce buffer */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700729 if (destlen || i != orig_sgl_count - 1) {
730 cur_src_sgl = sg_next(cur_src_sgl);
731 bounce_addr = (unsigned long)
732 kmap_atomic(
733 sg_page(cur_src_sgl));
734 }
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800735 } else if (destlen == 0 && i == orig_sgl_count - 1) {
736 /* unmap the last bounce that is < PAGE_SIZE */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700737 kunmap_atomic((void *)bounce_addr);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800738 }
739 }
740
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700741 kunmap_atomic((void *)(dest_addr - cur_dest_sgl->offset));
742 cur_dest_sgl = sg_next(cur_dest_sgl);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800743 }
744
745 local_irq_restore(flags);
746
747 return total_copied;
748}
749
750/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
751static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
752 struct scatterlist *bounce_sgl,
753 unsigned int orig_sgl_count)
754{
755 int i;
756 int j = 0;
757 unsigned long src, dest;
758 unsigned int srclen, destlen, copylen;
759 unsigned int total_copied = 0;
760 unsigned long bounce_addr = 0;
761 unsigned long src_addr = 0;
762 unsigned long flags;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700763 struct scatterlist *cur_src_sgl;
764 struct scatterlist *cur_dest_sgl;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800765
766 local_irq_save(flags);
767
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700768 cur_src_sgl = orig_sgl;
769 cur_dest_sgl = bounce_sgl;
770
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800771 for (i = 0; i < orig_sgl_count; i++) {
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700772 src_addr = (unsigned long)
773 kmap_atomic(sg_page(cur_src_sgl)) +
774 cur_src_sgl->offset;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800775 src = src_addr;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700776 srclen = cur_src_sgl->length;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800777
778 if (bounce_addr == 0)
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700779 bounce_addr = (unsigned long)
780 kmap_atomic(sg_page(cur_dest_sgl));
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800781
782 while (srclen) {
783 /* assume bounce offset always == 0 */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700784 dest = bounce_addr + cur_dest_sgl->length;
785 destlen = PAGE_SIZE - cur_dest_sgl->length;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800786
787 copylen = min(srclen, destlen);
788 memcpy((void *)dest, (void *)src, copylen);
789
790 total_copied += copylen;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700791 cur_dest_sgl->length += copylen;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800792 srclen -= copylen;
793 src += copylen;
794
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700795 if (cur_dest_sgl->length == PAGE_SIZE) {
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800796 /* full..move to next entry */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700797 kunmap_atomic((void *)bounce_addr);
K. Y. Srinivasan8de58072015-03-27 00:27:18 -0700798 bounce_addr = 0;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800799 j++;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800800 }
K. Y. Srinivasan8de58072015-03-27 00:27:18 -0700801
802 /* if we need to use another bounce buffer */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700803 if (srclen && bounce_addr == 0) {
804 cur_dest_sgl = sg_next(cur_dest_sgl);
805 bounce_addr = (unsigned long)
806 kmap_atomic(
807 sg_page(cur_dest_sgl));
808 }
K. Y. Srinivasan8de58072015-03-27 00:27:18 -0700809
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800810 }
811
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700812 kunmap_atomic((void *)(src_addr - cur_src_sgl->offset));
813 cur_src_sgl = sg_next(cur_src_sgl);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800814 }
815
K. Y. Srinivasan8de58072015-03-27 00:27:18 -0700816 if (bounce_addr)
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700817 kunmap_atomic((void *)bounce_addr);
K. Y. Srinivasan8de58072015-03-27 00:27:18 -0700818
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800819 local_irq_restore(flags);
820
821 return total_copied;
822}
823
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700824static void handle_sc_creation(struct vmbus_channel *new_sc)
825{
826 struct hv_device *device = new_sc->primary_channel->device_obj;
827 struct storvsc_device *stor_device;
828 struct vmstorage_channel_properties props;
829
830 stor_device = get_out_stor_device(device);
831 if (!stor_device)
832 return;
833
834 if (stor_device->open_sub_channel == false)
835 return;
836
837 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
838
839 vmbus_open(new_sc,
840 storvsc_ringbuffer_size,
841 storvsc_ringbuffer_size,
842 (void *)&props,
843 sizeof(struct vmstorage_channel_properties),
844 storvsc_on_channel_callback, new_sc);
845}
846
847static void handle_multichannel_storage(struct hv_device *device, int max_chns)
848{
849 struct storvsc_device *stor_device;
850 int num_cpus = num_online_cpus();
851 int num_sc;
852 struct storvsc_cmd_request *request;
853 struct vstor_packet *vstor_packet;
854 int ret, t;
855
856 num_sc = ((max_chns > num_cpus) ? num_cpus : max_chns);
857 stor_device = get_out_stor_device(device);
858 if (!stor_device)
859 return;
860
861 request = &stor_device->init_request;
862 vstor_packet = &request->vstor_packet;
863
864 stor_device->open_sub_channel = true;
865 /*
866 * Establish a handler for dealing with subchannels.
867 */
868 vmbus_set_sc_create_callback(device->channel, handle_sc_creation);
869
870 /*
871 * Check to see if sub-channels have already been created. This
872 * can happen when this driver is re-loaded after unloading.
873 */
874
875 if (vmbus_are_subchannels_present(device->channel))
876 return;
877
878 stor_device->open_sub_channel = false;
879 /*
880 * Request the host to create sub-channels.
881 */
882 memset(request, 0, sizeof(struct storvsc_cmd_request));
883 init_completion(&request->wait_event);
884 vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
885 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
886 vstor_packet->sub_channel_count = num_sc;
887
888 ret = vmbus_sendpacket(device->channel, vstor_packet,
889 (sizeof(struct vstor_packet) -
890 vmscsi_size_delta),
891 (unsigned long)request,
892 VM_PKT_DATA_INBAND,
893 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
894
895 if (ret != 0)
896 return;
897
898 t = wait_for_completion_timeout(&request->wait_event, 10*HZ);
899 if (t == 0)
900 return;
901
902 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
903 vstor_packet->status != 0)
904 return;
905
906 /*
907 * Now that we created the sub-channels, invoke the check; this
908 * may trigger the callback.
909 */
910 stor_device->open_sub_channel = true;
911 vmbus_are_subchannels_present(device->channel);
912}
913
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700914static int storvsc_channel_init(struct hv_device *device)
915{
916 struct storvsc_device *stor_device;
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800917 struct storvsc_cmd_request *request;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700918 struct vstor_packet *vstor_packet;
Keith Mange1a363102015-08-13 08:43:48 -0700919 int ret, t, i;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700920 int max_chns;
921 bool process_sub_channels = false;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700922
923 stor_device = get_out_stor_device(device);
924 if (!stor_device)
925 return -ENODEV;
926
927 request = &stor_device->init_request;
928 vstor_packet = &request->vstor_packet;
929
930 /*
931 * Now, initiate the vsc/vsp initialization protocol on the open
932 * channel
933 */
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800934 memset(request, 0, sizeof(struct storvsc_cmd_request));
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700935 init_completion(&request->wait_event);
936 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
937 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
938
939 ret = vmbus_sendpacket(device->channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700940 (sizeof(struct vstor_packet) -
941 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700942 (unsigned long)request,
943 VM_PKT_DATA_INBAND,
944 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
945 if (ret != 0)
946 goto cleanup;
947
948 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
949 if (t == 0) {
950 ret = -ETIMEDOUT;
951 goto cleanup;
952 }
953
954 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
K. Y. Srinivasan111f2d12015-08-13 08:43:52 -0700955 vstor_packet->status != 0) {
956 ret = -EINVAL;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700957 goto cleanup;
K. Y. Srinivasan111f2d12015-08-13 08:43:52 -0700958 }
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700959
960
Keith Mange1a363102015-08-13 08:43:48 -0700961 for (i = 0; i < ARRAY_SIZE(vmstor_protocols); i++) {
962 /* reuse the packet for version range supported */
963 memset(vstor_packet, 0, sizeof(struct vstor_packet));
964 vstor_packet->operation =
965 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
966 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700967
Keith Mange1a363102015-08-13 08:43:48 -0700968 vstor_packet->version.major_minor =
969 vmstor_protocols[i].protocol_version;
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800970
Keith Mange1a363102015-08-13 08:43:48 -0700971 /*
972 * The revision number is only used in Windows; set it to 0.
973 */
974 vstor_packet->version.revision = 0;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700975
Keith Mange1a363102015-08-13 08:43:48 -0700976 ret = vmbus_sendpacket(device->channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700977 (sizeof(struct vstor_packet) -
978 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700979 (unsigned long)request,
980 VM_PKT_DATA_INBAND,
981 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Keith Mange1a363102015-08-13 08:43:48 -0700982 if (ret != 0)
983 goto cleanup;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700984
Keith Mange1a363102015-08-13 08:43:48 -0700985 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
986 if (t == 0) {
987 ret = -ETIMEDOUT;
988 goto cleanup;
989 }
990
991 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO) {
992 ret = -EINVAL;
993 goto cleanup;
994 }
995
996 if (vstor_packet->status == 0) {
997 vmstor_proto_version =
998 vmstor_protocols[i].protocol_version;
999
1000 sense_buffer_size =
1001 vmstor_protocols[i].sense_buffer_size;
1002
1003 vmscsi_size_delta =
1004 vmstor_protocols[i].vmscsi_size_delta;
1005
1006 break;
1007 }
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001008 }
1009
Keith Mange1a363102015-08-13 08:43:48 -07001010 if (vstor_packet->status != 0) {
1011 ret = -EINVAL;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001012 goto cleanup;
Keith Mange1a363102015-08-13 08:43:48 -07001013 }
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001014
1015
1016 memset(vstor_packet, 0, sizeof(struct vstor_packet));
1017 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
1018 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001019
1020 ret = vmbus_sendpacket(device->channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001021 (sizeof(struct vstor_packet) -
1022 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001023 (unsigned long)request,
1024 VM_PKT_DATA_INBAND,
1025 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1026
1027 if (ret != 0)
1028 goto cleanup;
1029
1030 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
1031 if (t == 0) {
1032 ret = -ETIMEDOUT;
1033 goto cleanup;
1034 }
1035
1036 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
K. Y. Srinivasan111f2d12015-08-13 08:43:52 -07001037 vstor_packet->status != 0) {
1038 ret = -EINVAL;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001039 goto cleanup;
K. Y. Srinivasan111f2d12015-08-13 08:43:52 -07001040 }
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001041
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001042 /*
1043 * Check to see if multi-channel support is there.
1044 * Hosts that implement protocol version of 5.1 and above
1045 * support multi-channel.
1046 */
1047 max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
Keith Mangee6c4bc62015-08-13 08:43:50 -07001048 if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN8) {
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001049 if (vstor_packet->storage_channel_properties.flags &
1050 STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
1051 process_sub_channels = true;
1052 }
K. Y. Srinivasan5117b932015-03-27 00:27:17 -07001053 stor_device->max_transfer_bytes =
1054 vstor_packet->storage_channel_properties.max_transfer_bytes;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001055
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001056 memset(vstor_packet, 0, sizeof(struct vstor_packet));
1057 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
1058 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1059
1060 ret = vmbus_sendpacket(device->channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001061 (sizeof(struct vstor_packet) -
1062 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001063 (unsigned long)request,
1064 VM_PKT_DATA_INBAND,
1065 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1066
1067 if (ret != 0)
1068 goto cleanup;
1069
1070 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
1071 if (t == 0) {
1072 ret = -ETIMEDOUT;
1073 goto cleanup;
1074 }
1075
1076 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
K. Y. Srinivasan111f2d12015-08-13 08:43:52 -07001077 vstor_packet->status != 0) {
1078 ret = -EINVAL;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001079 goto cleanup;
K. Y. Srinivasan111f2d12015-08-13 08:43:52 -07001080 }
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001081
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001082 if (process_sub_channels)
1083 handle_multichannel_storage(device, max_chns);
1084
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001085
1086cleanup:
1087 return ret;
1088}
1089
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001090static void storvsc_handle_error(struct vmscsi_request *vm_srb,
1091 struct scsi_cmnd *scmnd,
1092 struct Scsi_Host *host,
1093 u8 asc, u8 ascq)
1094{
1095 struct storvsc_scan_work *wrk;
1096 void (*process_err_fn)(struct work_struct *work);
1097 bool do_work = false;
1098
1099 switch (vm_srb->srb_status) {
1100 case SRB_STATUS_ERROR:
1101 /*
1102 * If there is an error; offline the device since all
1103 * error recovery strategies would have already been
1104 * deployed on the host side. However, if the command
1105 * were a pass-through command deal with it appropriately.
1106 */
1107 switch (scmnd->cmnd[0]) {
1108 case ATA_16:
1109 case ATA_12:
1110 set_host_byte(scmnd, DID_PASSTHROUGH);
1111 break;
K. Y. Srinivasan3533f8602014-07-12 09:48:32 -07001112 /*
1113 * On Some Windows hosts TEST_UNIT_READY command can return
1114 * SRB_STATUS_ERROR, let the upper level code deal with it
1115 * based on the sense information.
1116 */
1117 case TEST_UNIT_READY:
1118 break;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001119 default:
1120 set_host_byte(scmnd, DID_TARGET_FAILURE);
1121 }
1122 break;
1123 case SRB_STATUS_INVALID_LUN:
1124 do_work = true;
1125 process_err_fn = storvsc_remove_lun;
1126 break;
K. Y. Srinivasan67812092013-02-21 12:04:53 -08001127 case (SRB_STATUS_ABORTED | SRB_STATUS_AUTOSENSE_VALID):
1128 if ((asc == 0x2a) && (ascq == 0x9)) {
1129 do_work = true;
1130 process_err_fn = storvsc_device_scan;
1131 /*
1132 * Retry the I/O that trigerred this.
1133 */
1134 set_host_byte(scmnd, DID_REQUEUE);
1135 }
1136 break;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001137 }
K. Y. Srinivasan67812092013-02-21 12:04:53 -08001138
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001139 if (!do_work)
1140 return;
1141
1142 /*
1143 * We need to schedule work to process this error; schedule it.
1144 */
1145 wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
1146 if (!wrk) {
1147 set_host_byte(scmnd, DID_TARGET_FAILURE);
1148 return;
1149 }
1150
1151 wrk->host = host;
1152 wrk->lun = vm_srb->lun;
1153 INIT_WORK(&wrk->work, process_err_fn);
1154 schedule_work(&wrk->work);
1155}
1156
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001157
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001158static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request)
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001159{
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001160 struct scsi_cmnd *scmnd = cmd_request->cmd;
1161 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001162 struct scsi_sense_hdr sense_hdr;
1163 struct vmscsi_request *vm_srb;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001164 struct Scsi_Host *host;
1165 struct storvsc_device *stor_dev;
1166 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001167 u32 payload_sz = cmd_request->payload_sz;
1168 void *payload = cmd_request->payload;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001169
1170 stor_dev = get_in_stor_device(dev);
1171 host = stor_dev->host;
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001172
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001173 vm_srb = &cmd_request->vstor_packet.vm_srb;
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001174 if (cmd_request->bounce_sgl_count) {
1175 if (vm_srb->data_in == READ_TYPE)
1176 copy_from_bounce_buffer(scsi_sglist(scmnd),
1177 cmd_request->bounce_sgl,
1178 scsi_sg_count(scmnd),
1179 cmd_request->bounce_sgl_count);
1180 destroy_bounce_buffer(cmd_request->bounce_sgl,
1181 cmd_request->bounce_sgl_count);
1182 }
1183
K. Y. Srinivasan42e22ca2012-04-05 12:26:52 -07001184 scmnd->result = vm_srb->scsi_status;
1185
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001186 if (scmnd->result) {
1187 if (scsi_normalize_sense(scmnd->sense_buffer,
1188 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
Hannes Reinecked811b842014-10-24 14:26:45 +02001189 scsi_print_sense_hdr(scmnd->device, "storvsc",
1190 &sense_hdr);
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001191 }
1192
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001193 if (vm_srb->srb_status != SRB_STATUS_SUCCESS)
1194 storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
1195 sense_hdr.ascq);
1196
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001197 scsi_set_resid(scmnd,
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001198 cmd_request->payload->range.len -
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001199 vm_srb->data_transfer_length);
1200
Christoph Hellwigead37002014-12-29 20:05:09 -08001201 scmnd->scsi_done(scmnd);
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001202
1203 if (payload_sz >
1204 sizeof(struct vmbus_channel_packet_multipage_buffer))
1205 kfree(payload);
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001206}
1207
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001208static void storvsc_on_io_completion(struct hv_device *device,
1209 struct vstor_packet *vstor_packet,
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001210 struct storvsc_cmd_request *request)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001211{
1212 struct storvsc_device *stor_device;
1213 struct vstor_packet *stor_pkt;
1214
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001215 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001216 stor_pkt = &request->vstor_packet;
1217
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -07001218 /*
1219 * The current SCSI handling on the host side does
1220 * not correctly handle:
1221 * INQUIRY command with page code parameter set to 0x80
1222 * MODE_SENSE command with cmd[2] == 0x1c
1223 *
1224 * Setup srb and scsi status so this won't be fatal.
1225 * We do this so we can distinguish truly fatal failues
1226 * (srb status == 0x4) and off-line the device in that case.
1227 */
1228
1229 if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
K. Y. Srinivasana8c18c52012-01-12 12:38:00 -08001230 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -07001231 vstor_packet->vm_srb.scsi_status = 0;
K. Y. Srinivasan16046322012-01-12 12:37:57 -08001232 vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -07001233 }
1234
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001235
1236 /* Copy over the status...etc */
1237 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
1238 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
1239 stor_pkt->vm_srb.sense_info_length =
1240 vstor_packet->vm_srb.sense_info_length;
1241
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001242
1243 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
1244 /* CHECK_CONDITION */
K. Y. Srinivasan16046322012-01-12 12:37:57 -08001245 if (vstor_packet->vm_srb.srb_status &
1246 SRB_STATUS_AUTOSENSE_VALID) {
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001247 /* autosense data available */
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001248
Christoph Hellwigead37002014-12-29 20:05:09 -08001249 memcpy(request->cmd->sense_buffer,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001250 vstor_packet->vm_srb.sense_data,
1251 vstor_packet->vm_srb.sense_info_length);
1252
1253 }
1254 }
1255
1256 stor_pkt->vm_srb.data_transfer_length =
1257 vstor_packet->vm_srb.data_transfer_length;
1258
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001259 storvsc_command_completion(request);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001260
1261 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
1262 stor_device->drain_notify)
1263 wake_up(&stor_device->waiting_to_drain);
1264
1265
1266}
1267
1268static void storvsc_on_receive(struct hv_device *device,
1269 struct vstor_packet *vstor_packet,
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001270 struct storvsc_cmd_request *request)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001271{
K. Y. Srinivasan12675792011-11-08 09:01:49 -08001272 struct storvsc_scan_work *work;
1273 struct storvsc_device *stor_device;
1274
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001275 switch (vstor_packet->operation) {
1276 case VSTOR_OPERATION_COMPLETE_IO:
1277 storvsc_on_io_completion(device, vstor_packet, request);
1278 break;
K. Y. Srinivasan12675792011-11-08 09:01:49 -08001279
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001280 case VSTOR_OPERATION_REMOVE_DEVICE:
K. Y. Srinivasan12675792011-11-08 09:01:49 -08001281 case VSTOR_OPERATION_ENUMERATE_BUS:
1282 stor_device = get_in_stor_device(device);
1283 work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
1284 if (!work)
1285 return;
1286
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -08001287 INIT_WORK(&work->work, storvsc_host_scan);
K. Y. Srinivasan12675792011-11-08 09:01:49 -08001288 work->host = stor_device->host;
1289 schedule_work(&work->work);
1290 break;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001291
1292 default:
1293 break;
1294 }
1295}
1296
1297static void storvsc_on_channel_callback(void *context)
1298{
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001299 struct vmbus_channel *channel = (struct vmbus_channel *)context;
1300 struct hv_device *device;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001301 struct storvsc_device *stor_device;
1302 u32 bytes_recvd;
1303 u64 request_id;
1304 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001305 struct storvsc_cmd_request *request;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001306 int ret;
1307
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001308 if (channel->primary_channel != NULL)
1309 device = channel->primary_channel->device_obj;
1310 else
1311 device = channel->device_obj;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001312
1313 stor_device = get_in_stor_device(device);
1314 if (!stor_device)
1315 return;
1316
1317 do {
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001318 ret = vmbus_recvpacket(channel, packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001319 ALIGN((sizeof(struct vstor_packet) -
1320 vmscsi_size_delta), 8),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001321 &bytes_recvd, &request_id);
1322 if (ret == 0 && bytes_recvd > 0) {
1323
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001324 request = (struct storvsc_cmd_request *)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001325 (unsigned long)request_id;
1326
1327 if ((request == &stor_device->init_request) ||
1328 (request == &stor_device->reset_request)) {
1329
1330 memcpy(&request->vstor_packet, packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001331 (sizeof(struct vstor_packet) -
1332 vmscsi_size_delta));
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001333 complete(&request->wait_event);
1334 } else {
1335 storvsc_on_receive(device,
1336 (struct vstor_packet *)packet,
1337 request);
1338 }
1339 } else {
1340 break;
1341 }
1342 } while (1);
1343
1344 return;
1345}
1346
1347static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
1348{
1349 struct vmstorage_channel_properties props;
1350 int ret;
1351
1352 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
1353
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001354 ret = vmbus_open(device->channel,
1355 ring_size,
1356 ring_size,
1357 (void *)&props,
1358 sizeof(struct vmstorage_channel_properties),
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001359 storvsc_on_channel_callback, device->channel);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001360
1361 if (ret != 0)
1362 return ret;
1363
1364 ret = storvsc_channel_init(device);
1365
1366 return ret;
1367}
1368
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -07001369static int storvsc_dev_remove(struct hv_device *device)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001370{
1371 struct storvsc_device *stor_device;
1372 unsigned long flags;
1373
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001374 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001375
1376 spin_lock_irqsave(&device->channel->inbound_lock, flags);
1377 stor_device->destroy = true;
1378 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1379
1380 /*
1381 * At this point, all outbound traffic should be disable. We
1382 * only allow inbound traffic (responses) to proceed so that
1383 * outstanding requests can be completed.
1384 */
1385
1386 storvsc_wait_to_drain(stor_device);
1387
1388 /*
1389 * Since we have already drained, we don't need to busy wait
1390 * as was done in final_release_stor_device()
1391 * Note that we cannot set the ext pointer to NULL until
1392 * we have drained - to drain the outgoing packets, we need to
1393 * allow incoming packets.
1394 */
1395 spin_lock_irqsave(&device->channel->inbound_lock, flags);
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001396 hv_set_drvdata(device, NULL);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001397 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1398
1399 /* Close the channel */
1400 vmbus_close(device->channel);
1401
1402 kfree(stor_device);
1403 return 0;
1404}
1405
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -07001406static int storvsc_do_io(struct hv_device *device,
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001407 struct storvsc_cmd_request *request)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001408{
1409 struct storvsc_device *stor_device;
1410 struct vstor_packet *vstor_packet;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001411 struct vmbus_channel *outgoing_channel;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001412 int ret = 0;
1413
1414 vstor_packet = &request->vstor_packet;
1415 stor_device = get_out_stor_device(device);
1416
1417 if (!stor_device)
1418 return -ENODEV;
1419
1420
1421 request->device = device;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001422 /*
1423 * Select an an appropriate channel to send the request out.
1424 */
1425
1426 outgoing_channel = vmbus_get_outgoing_channel(device->channel);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001427
1428
1429 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
1430
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001431 vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) -
1432 vmscsi_size_delta);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001433
1434
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001435 vstor_packet->vm_srb.sense_info_length = sense_buffer_size;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001436
1437
1438 vstor_packet->vm_srb.data_transfer_length =
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001439 request->payload->range.len;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001440
1441 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
1442
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001443 if (request->payload->range.len) {
1444
1445 ret = vmbus_sendpacket_mpb_desc(outgoing_channel,
1446 request->payload, request->payload_sz,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001447 vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001448 (sizeof(struct vstor_packet) -
1449 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001450 (unsigned long)request);
1451 } else {
K. Y. Srinivasan0147dab2015-03-27 00:27:16 -07001452 ret = vmbus_sendpacket(outgoing_channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001453 (sizeof(struct vstor_packet) -
1454 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001455 (unsigned long)request,
1456 VM_PKT_DATA_INBAND,
1457 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1458 }
1459
1460 if (ret != 0)
1461 return ret;
1462
1463 atomic_inc(&stor_device->num_outstanding_req);
1464
1465 return ret;
1466}
1467
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001468static int storvsc_device_configure(struct scsi_device *sdevice)
1469{
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001470
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001471 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
1472
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001473 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001474
K. Y. Srinivasan893def32013-06-04 12:05:05 -07001475 blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
1476
Olaf Hering3e8f4f42013-02-21 12:04:51 -08001477 sdevice->no_write_same = 1;
1478
K. Y. Srinivasanf3cfabc2014-07-21 16:06:01 -07001479 /*
1480 * Add blist flags to permit the reading of the VPD pages even when
1481 * the target may claim SPC-2 compliance. MSFT targets currently
1482 * claim SPC-2 compliance while they implement post SPC-2 features.
1483 * With this patch we can correctly handle WRITE_SAME_16 issues.
1484 */
1485 sdevice->sdev_bflags |= msft_blist_flags;
1486
K. Y. Srinivasanb0a93d92014-12-16 13:21:45 -08001487 /*
1488 * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3
Keith Mangeb95f5be2015-08-13 08:43:51 -07001489 * if the device is a MSFT virtual device. If the host is
1490 * WIN10 or newer, allow write_same.
K. Y. Srinivasanb0a93d92014-12-16 13:21:45 -08001491 */
1492 if (!strncmp(sdevice->vendor, "Msft", 4)) {
Keith Mangee6c4bc62015-08-13 08:43:50 -07001493 switch (vmstor_proto_version) {
1494 case VMSTOR_PROTO_VERSION_WIN8:
1495 case VMSTOR_PROTO_VERSION_WIN8_1:
K. Y. Srinivasanb0a93d92014-12-16 13:21:45 -08001496 sdevice->scsi_level = SCSI_SPC_3;
1497 break;
1498 }
Keith Mangeb95f5be2015-08-13 08:43:51 -07001499
1500 if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN10)
1501 sdevice->no_write_same = 0;
K. Y. Srinivasanb0a93d92014-12-16 13:21:45 -08001502 }
1503
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001504 return 0;
1505}
1506
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001507static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1508 sector_t capacity, int *info)
1509{
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001510 sector_t nsect = capacity;
1511 sector_t cylinders = nsect;
1512 int heads, sectors_pt;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001513
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001514 /*
1515 * We are making up these values; let us keep it simple.
1516 */
1517 heads = 0xff;
1518 sectors_pt = 0x3f; /* Sectors per track */
1519 sector_div(cylinders, heads * sectors_pt);
1520 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1521 cylinders = 0xffff;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001522
1523 info[0] = heads;
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001524 info[1] = sectors_pt;
1525 info[2] = (int)cylinders;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001526
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001527 return 0;
1528}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001529
K. Y. Srinivasan4b270c82012-01-12 12:37:58 -08001530static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001531{
K. Y. Srinivasan4b270c82012-01-12 12:37:58 -08001532 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1533 struct hv_device *device = host_dev->dev;
1534
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001535 struct storvsc_device *stor_device;
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001536 struct storvsc_cmd_request *request;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001537 struct vstor_packet *vstor_packet;
1538 int ret, t;
1539
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001540
K. Y. Srinivasan1eaaddf2011-08-27 11:31:03 -07001541 stor_device = get_out_stor_device(device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001542 if (!stor_device)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001543 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001544
1545 request = &stor_device->reset_request;
1546 vstor_packet = &request->vstor_packet;
1547
1548 init_completion(&request->wait_event);
1549
1550 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1551 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1552 vstor_packet->vm_srb.path_id = stor_device->path_id;
1553
1554 ret = vmbus_sendpacket(device->channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001555 (sizeof(struct vstor_packet) -
1556 vmscsi_size_delta),
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001557 (unsigned long)&stor_device->reset_request,
1558 VM_PKT_DATA_INBAND,
1559 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1560 if (ret != 0)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001561 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001562
K. Y. Srinivasan46d2eb62011-06-16 13:16:36 -07001563 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001564 if (t == 0)
1565 return TIMEOUT_ERROR;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001566
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001567
1568 /*
1569 * At this point, all outstanding requests in the adapter
1570 * should have been flushed out and return to us
K. Y. Srinivasan5c1b10a2012-10-02 11:03:31 -07001571 * There is a potential race here where the host may be in
1572 * the process of responding when we return from here.
1573 * Just wait for all in-transit packets to be accounted for
1574 * before we return from here.
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001575 */
K. Y. Srinivasan5c1b10a2012-10-02 11:03:31 -07001576 storvsc_wait_to_drain(stor_device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001577
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001578 return SUCCESS;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001579}
1580
K. Y. Srinivasan56b26e62014-07-12 09:48:30 -07001581/*
1582 * The host guarantees to respond to each command, although I/O latencies might
1583 * be unbounded on Azure. Reset the timer unconditionally to give the host a
1584 * chance to perform EH.
1585 */
1586static enum blk_eh_timer_return storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
1587{
1588 return BLK_EH_RESET_TIMER;
1589}
1590
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001591static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001592{
1593 bool allowed = true;
1594 u8 scsi_op = scmnd->cmnd[0];
1595
1596 switch (scsi_op) {
Olaf Hering3e8f4f42013-02-21 12:04:51 -08001597 /* the host does not handle WRITE_SAME, log accident usage */
1598 case WRITE_SAME:
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001599 /*
1600 * smartd sends this command and the host does not handle
1601 * this. So, don't send it.
1602 */
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001603 case SET_WINDOW:
K. Y. Srinivasan59e00e72011-11-08 09:01:42 -08001604 scmnd->result = ILLEGAL_REQUEST << 16;
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001605 allowed = false;
1606 break;
1607 default:
1608 break;
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001609 }
1610 return allowed;
1611}
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001612
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001613static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001614{
1615 int ret;
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001616 struct hv_host_device *host_dev = shost_priv(host);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001617 struct hv_device *dev = host_dev->dev;
Christoph Hellwigead37002014-12-29 20:05:09 -08001618 struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001619 int i;
1620 struct scatterlist *sgl;
1621 unsigned int sg_count = 0;
1622 struct vmscsi_request *vm_srb;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -07001623 struct scatterlist *cur_sgl;
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001624 struct vmbus_packet_mpb_array *payload;
1625 u32 payload_sz;
1626 u32 length;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001627
Keith Mange2492fd72015-08-13 08:43:47 -07001628 if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) {
K. Y. Srinivasan8caf92d2014-07-12 09:48:28 -07001629 /*
1630 * On legacy hosts filter unimplemented commands.
1631 * Future hosts are expected to correctly handle
1632 * unsupported commands. Furthermore, it is
1633 * possible that some of the currently
1634 * unsupported commands maybe supported in
1635 * future versions of the host.
1636 */
1637 if (!storvsc_scsi_cmd_ok(scmnd)) {
1638 scmnd->scsi_done(scmnd);
1639 return 0;
1640 }
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001641 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001642
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001643 /* Setup the cmd request */
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001644 cmd_request->cmd = scmnd;
1645
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001646 vm_srb = &cmd_request->vstor_packet.vm_srb;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001647 vm_srb->win8_extension.time_out_value = 60;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001648
K. Y. Srinivasanf885fb72014-07-12 09:48:31 -07001649 vm_srb->win8_extension.srb_flags |=
1650 (SRB_FLAGS_QUEUE_ACTION_ENABLE |
1651 SRB_FLAGS_DISABLE_SYNCH_TRANSFER);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001652
1653 /* Build the SRB */
1654 switch (scmnd->sc_data_direction) {
1655 case DMA_TO_DEVICE:
1656 vm_srb->data_in = WRITE_TYPE;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001657 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001658 break;
1659 case DMA_FROM_DEVICE:
1660 vm_srb->data_in = READ_TYPE;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001661 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001662 break;
Vitaly Kuznetsovcb1cf082015-06-25 18:12:11 +02001663 case DMA_NONE:
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001664 vm_srb->data_in = UNKNOWN_TYPE;
K. Y. Srinivasandc457082015-05-01 11:03:02 -07001665 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001666 break;
Vitaly Kuznetsovcb1cf082015-06-25 18:12:11 +02001667 default:
1668 /*
1669 * This is DMA_BIDIRECTIONAL or something else we are never
1670 * supposed to see here.
1671 */
1672 WARN(1, "Unexpected data direction: %d\n",
1673 scmnd->sc_data_direction);
1674 return -EINVAL;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001675 }
1676
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001677
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001678 vm_srb->port_number = host_dev->port;
1679 vm_srb->path_id = scmnd->device->channel;
1680 vm_srb->target_id = scmnd->device->id;
1681 vm_srb->lun = scmnd->device->lun;
1682
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001683 vm_srb->cdb_length = scmnd->cmd_len;
1684
1685 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1686
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001687 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1688 sg_count = scsi_sg_count(scmnd);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001689
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001690 length = scsi_bufflen(scmnd);
1691 payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
1692 payload_sz = sizeof(cmd_request->mpb);
1693
1694 if (sg_count) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001695 /* check if we need to bounce the sgl */
1696 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
1697 cmd_request->bounce_sgl =
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001698 create_bounce_buffer(sgl, sg_count,
1699 length,
K. Y. Srinivasan6e8087a2011-12-07 07:15:52 -08001700 vm_srb->data_in);
Christoph Hellwigead37002014-12-29 20:05:09 -08001701 if (!cmd_request->bounce_sgl)
1702 return SCSI_MLQUEUE_HOST_BUSY;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001703
1704 cmd_request->bounce_sgl_count =
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001705 ALIGN(length, PAGE_SIZE) >> PAGE_SHIFT;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001706
K. Y. Srinivasanfa23b8c2011-08-27 11:31:21 -07001707 if (vm_srb->data_in == WRITE_TYPE)
1708 copy_to_bounce_buffer(sgl,
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001709 cmd_request->bounce_sgl, sg_count);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001710
1711 sgl = cmd_request->bounce_sgl;
1712 sg_count = cmd_request->bounce_sgl_count;
1713 }
1714
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001715
1716 if (sg_count > MAX_PAGE_BUFFER_COUNT) {
1717
1718 payload_sz = (sg_count * sizeof(void *) +
1719 sizeof(struct vmbus_packet_mpb_array));
1720 payload = kmalloc(payload_sz, GFP_ATOMIC);
1721 if (!payload) {
1722 if (cmd_request->bounce_sgl_count)
1723 destroy_bounce_buffer(
1724 cmd_request->bounce_sgl,
1725 cmd_request->bounce_sgl_count);
1726
1727 return SCSI_MLQUEUE_DEVICE_BUSY;
1728 }
1729 }
1730
1731 payload->range.len = length;
1732 payload->range.offset = sgl[0].offset;
1733
K. Y. Srinivasanaaced992015-03-27 00:27:19 -07001734 cur_sgl = sgl;
1735 for (i = 0; i < sg_count; i++) {
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001736 payload->range.pfn_array[i] =
K. Y. Srinivasanaaced992015-03-27 00:27:19 -07001737 page_to_pfn(sg_page((cur_sgl)));
1738 cur_sgl = sg_next(cur_sgl);
1739 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001740
1741 } else if (scsi_sglist(scmnd)) {
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001742 payload->range.len = length;
1743 payload->range.offset =
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001744 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001745 payload->range.pfn_array[0] =
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001746 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1747 }
1748
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001749 cmd_request->payload = payload;
1750 cmd_request->payload_sz = payload_sz;
1751
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001752 /* Invokes the vsc to start an IO */
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001753 ret = storvsc_do_io(dev, cmd_request);
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -07001754
K. Y. Srinivasand2598f02011-08-25 09:48:58 -07001755 if (ret == -EAGAIN) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001756 /* no more space */
1757
Long Lie86fb5e2014-12-05 19:38:18 -08001758 if (cmd_request->bounce_sgl_count)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001759 destroy_bounce_buffer(cmd_request->bounce_sgl,
K. Y. Srinivasan70691ec2011-08-27 11:31:29 -07001760 cmd_request->bounce_sgl_count);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001761
Christoph Hellwigead37002014-12-29 20:05:09 -08001762 return SCSI_MLQUEUE_DEVICE_BUSY;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001763 }
1764
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001765 return 0;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001766}
1767
Hank Janssenbef4a342009-07-13 16:01:31 -07001768static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001769 .module = THIS_MODULE,
1770 .name = "storvsc_host_t",
Christoph Hellwigead37002014-12-29 20:05:09 -08001771 .cmd_size = sizeof(struct storvsc_cmd_request),
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001772 .bios_param = storvsc_get_chs,
1773 .queuecommand = storvsc_queuecommand,
1774 .eh_host_reset_handler = storvsc_host_reset_handler,
Christoph Hellwigead37002014-12-29 20:05:09 -08001775 .proc_name = "storvsc_host",
K. Y. Srinivasan56b26e62014-07-12 09:48:30 -07001776 .eh_timed_out = storvsc_eh_timed_out,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001777 .slave_configure = storvsc_device_configure,
K. Y. Srinivasan52f96142014-07-12 09:48:27 -07001778 .cmd_per_lun = 255,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001779 .this_id = -1,
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001780 .use_clustering = ENABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001781 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001782 .dma_boundary = PAGE_SIZE-1,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04001783 .no_write_same = 1,
Hank Janssenbef4a342009-07-13 16:01:31 -07001784};
1785
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001786enum {
1787 SCSI_GUID,
1788 IDE_GUID,
K. Y. Srinivasanbde6d0f2013-06-04 12:05:08 -07001789 SFC_GUID,
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001790};
1791
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001792static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001793 /* SCSI guid */
K. Y. Srinivasan35c3bc22013-01-23 17:42:43 -08001794 { HV_SCSI_GUID,
1795 .driver_data = SCSI_GUID
1796 },
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001797 /* IDE guid */
K. Y. Srinivasan35c3bc22013-01-23 17:42:43 -08001798 { HV_IDE_GUID,
1799 .driver_data = IDE_GUID
1800 },
K. Y. Srinivasanbde6d0f2013-06-04 12:05:08 -07001801 /* Fibre Channel GUID */
1802 {
1803 HV_SYNTHFC_GUID,
1804 .driver_data = SFC_GUID
1805 },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001806 { },
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001807};
Hank Janssenbef4a342009-07-13 16:01:31 -07001808
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001809MODULE_DEVICE_TABLE(vmbus, id_table);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001810
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001811static int storvsc_probe(struct hv_device *device,
1812 const struct hv_vmbus_device_id *dev_id)
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001813{
1814 int ret;
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -07001815 int num_cpus = num_online_cpus();
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001816 struct Scsi_Host *host;
1817 struct hv_host_device *host_dev;
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001818 bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001819 int target = 0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001820 struct storvsc_device *stor_device;
K. Y. Srinivasan0fb8db22014-12-16 13:21:44 -08001821 int max_luns_per_target;
1822 int max_targets;
1823 int max_channels;
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -07001824 int max_sub_channels = 0;
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001825
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001826 /*
1827 * Based on the windows host we are running on,
1828 * set state to properly communicate with the host.
1829 */
1830
Keith Mange6ee5c612015-08-13 08:43:46 -07001831 if (vmbus_proto_version < VERSION_WIN8) {
K. Y. Srinivasan0fb8db22014-12-16 13:21:44 -08001832 max_luns_per_target = STORVSC_IDE_MAX_LUNS_PER_TARGET;
1833 max_targets = STORVSC_IDE_MAX_TARGETS;
1834 max_channels = STORVSC_IDE_MAX_CHANNELS;
Keith Mange6ee5c612015-08-13 08:43:46 -07001835 } else {
K. Y. Srinivasan0fb8db22014-12-16 13:21:44 -08001836 max_luns_per_target = STORVSC_MAX_LUNS_PER_TARGET;
1837 max_targets = STORVSC_MAX_TARGETS;
1838 max_channels = STORVSC_MAX_CHANNELS;
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -07001839 /*
1840 * On Windows8 and above, we support sub-channels for storage.
1841 * The number of sub-channels offerred is based on the number of
1842 * VCPUs in the guest.
1843 */
1844 max_sub_channels = (num_cpus / storvsc_vcpus_per_sub_channel);
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001845 }
1846
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -07001847 scsi_driver.can_queue = (max_outstanding_req_per_channel *
1848 (max_sub_channels + 1));
1849
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001850 host = scsi_host_alloc(&scsi_driver,
1851 sizeof(struct hv_host_device));
1852 if (!host)
1853 return -ENOMEM;
1854
K. Y. Srinivasan7f33f302011-11-08 09:01:44 -08001855 host_dev = shost_priv(host);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001856 memset(host_dev, 0, sizeof(struct hv_host_device));
1857
1858 host_dev->port = host->host_no;
1859 host_dev->dev = device;
1860
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001861
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001862 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001863 if (!stor_device) {
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001864 ret = -ENOMEM;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001865 goto err_out0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001866 }
1867
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001868 stor_device->destroy = false;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001869 stor_device->open_sub_channel = false;
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001870 init_waitqueue_head(&stor_device->waiting_to_drain);
1871 stor_device->device = device;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001872 stor_device->host = host;
1873 hv_set_drvdata(device, stor_device);
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001874
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001875 stor_device->port_number = host->host_no;
1876 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001877 if (ret)
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001878 goto err_out1;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001879
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001880 host_dev->path = stor_device->path_id;
1881 host_dev->target = stor_device->target_id;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001882
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -07001883 switch (dev_id->driver_data) {
1884 case SFC_GUID:
1885 host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
1886 host->max_id = STORVSC_FC_MAX_TARGETS;
1887 host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
1888 break;
1889
1890 case SCSI_GUID:
K. Y. Srinivasan0fb8db22014-12-16 13:21:44 -08001891 host->max_lun = max_luns_per_target;
1892 host->max_id = max_targets;
1893 host->max_channel = max_channels - 1;
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -07001894 break;
1895
1896 default:
1897 host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
1898 host->max_id = STORVSC_IDE_MAX_TARGETS;
1899 host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
1900 break;
1901 }
Mike Sterlingcf55f4a2011-09-06 16:10:55 -07001902 /* max cmd length */
1903 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001904
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001905 /*
1906 * set the table size based on the info we got
1907 * from the host.
1908 */
1909 host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
1910
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001911 /* Register the HBA and start the scsi bus scan */
1912 ret = scsi_add_host(host, &device->device);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001913 if (ret != 0)
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001914 goto err_out2;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001915
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001916 if (!dev_is_ide) {
1917 scsi_scan_host(host);
K. Y. Srinivasan59d22952012-01-12 12:37:55 -08001918 } else {
1919 target = (device->dev_instance.b[5] << 8 |
1920 device->dev_instance.b[4]);
1921 ret = scsi_add_device(host, 0, target, 0);
1922 if (ret) {
1923 scsi_remove_host(host);
1924 goto err_out2;
1925 }
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001926 }
1927 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001928
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001929err_out2:
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001930 /*
1931 * Once we have connected with the host, we would need to
1932 * to invoke storvsc_dev_remove() to rollback this state and
1933 * this call also frees up the stor_device; hence the jump around
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001934 * err_out1 label.
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001935 */
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001936 storvsc_dev_remove(device);
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001937 goto err_out0;
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001938
1939err_out1:
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001940 kfree(stor_device);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001941
1942err_out0:
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001943 scsi_host_put(host);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001944 return ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001945}
1946
K. Y. Srinivasanddcbf652012-01-12 12:37:59 -08001947static int storvsc_remove(struct hv_device *dev)
1948{
1949 struct storvsc_device *stor_device = hv_get_drvdata(dev);
1950 struct Scsi_Host *host = stor_device->host;
1951
1952 scsi_remove_host(host);
1953 storvsc_dev_remove(dev);
1954 scsi_host_put(host);
1955
1956 return 0;
1957}
1958
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001959static struct hv_driver storvsc_drv = {
K. Y. Srinivasanfafb0ef2011-11-08 09:01:46 -08001960 .name = KBUILD_MODNAME,
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001961 .id_table = id_table,
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001962 .probe = storvsc_probe,
1963 .remove = storvsc_remove,
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -07001964};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07001965
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001966static int __init storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001967{
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001968
1969 /*
1970 * Divide the ring buffer data size (which is 1 page less
1971 * than the ring buffer size since that page is reserved for
1972 * the ring buffer indices) by the max request size (which is
1973 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1974 */
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001975 max_outstanding_req_per_channel =
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001976 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1977 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001978 sizeof(struct vstor_packet) + sizeof(u64) -
1979 vmscsi_size_delta,
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001980 sizeof(u64)));
Hank Janssenbef4a342009-07-13 16:01:31 -07001981
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001982 return vmbus_driver_register(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001983}
1984
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001985static void __exit storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001986{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001987 vmbus_driver_unregister(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001988}
1989
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001990MODULE_LICENSE("GPL");
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -07001991MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001992module_init(storvsc_drv_init);
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001993module_exit(storvsc_drv_exit);