blob: d6a42c0000481ec3dc195421bfe2bc92a143112d [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
154 * variable to track the size we should use.
155 */
156static int sense_buffer_size;
157
158/*
159 * The size of the vmscsi_request has changed in win8. The
160 * additional size is because of new elements added to the
161 * structure. These elements are valid only when we are talking
162 * to a win8 host.
163 * Track the correction to size we need to apply.
164 */
165
166static int vmscsi_size_delta;
Keith Mange2492fd72015-08-13 08:43:47 -0700167static int vmstor_proto_version;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700168
169struct vmscsi_win8_extension {
170 /*
171 * The following were added in Windows 8
172 */
173 u16 reserve;
174 u8 queue_tag;
175 u8 queue_action;
176 u32 srb_flags;
177 u32 time_out_value;
178 u32 queue_sort_ey;
179} __packed;
180
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700181struct vmscsi_request {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800182 u16 length;
183 u8 srb_status;
184 u8 scsi_status;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700185
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800186 u8 port_number;
187 u8 path_id;
188 u8 target_id;
189 u8 lun;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700190
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800191 u8 cdb_length;
192 u8 sense_info_length;
193 u8 data_in;
194 u8 reserved;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700195
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800196 u32 data_transfer_length;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700197
198 union {
K. Y. Srinivasan6b2f9492012-01-12 12:38:05 -0800199 u8 cdb[STORVSC_MAX_CMD_LEN];
200 u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
201 u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700202 };
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700203 /*
204 * The following was added in win8.
205 */
206 struct vmscsi_win8_extension win8_extension;
207
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700208} __attribute((packed));
209
210
211/*
Keith Mange1a363102015-08-13 08:43:48 -0700212 * The list of storage protocols in order of preference.
213 */
214struct vmstor_protocol {
215 int protocol_version;
216 int sense_buffer_size;
217 int vmscsi_size_delta;
218};
219
220
221static const struct vmstor_protocol vmstor_protocols[] = {
222 {
223 VMSTOR_PROTO_VERSION_WIN10,
224 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
225 0
226 },
227 {
228 VMSTOR_PROTO_VERSION_WIN8_1,
229 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
230 0
231 },
232 {
233 VMSTOR_PROTO_VERSION_WIN8,
234 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
235 0
236 },
237 {
238 VMSTOR_PROTO_VERSION_WIN7,
239 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
240 sizeof(struct vmscsi_win8_extension),
241 },
242 {
243 VMSTOR_PROTO_VERSION_WIN6,
244 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
245 sizeof(struct vmscsi_win8_extension),
246 }
247};
248
249
250/*
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700251 * This structure is sent during the intialization phase to get the different
252 * properties of the channel.
253 */
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700254
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700255#define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1
256
257struct vmstorage_channel_properties {
258 u32 reserved;
259 u16 max_channel_cnt;
260 u16 reserved1;
261
262 u32 flags;
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800263 u32 max_transfer_bytes;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700264
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700265 u64 reserved2;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700266} __packed;
267
268/* This structure is sent during the storage protocol negotiations. */
269struct vmstorage_protocol_version {
270 /* Major (MSW) and minor (LSW) version numbers. */
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800271 u16 major_minor;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700272
273 /*
274 * Revision number is auto-incremented whenever this file is changed
275 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
276 * definitely indicate incompatibility--but it does indicate mismatched
277 * builds.
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800278 * This is only used on the windows side. Just set it to 0.
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700279 */
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800280 u16 revision;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700281} __packed;
282
283/* Channel Property Flags */
284#define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
285#define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
286
287struct vstor_packet {
288 /* Requested operation type */
289 enum vstor_packet_operation operation;
290
291 /* Flags - see below for values */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800292 u32 flags;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700293
294 /* Status of the request returned from the server side. */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800295 u32 status;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700296
297 /* Data payload area */
298 union {
299 /*
300 * Structure used to forward SCSI commands from the
301 * client to the server.
302 */
303 struct vmscsi_request vm_srb;
304
305 /* Structure used to query channel properties. */
306 struct vmstorage_channel_properties storage_channel_properties;
307
308 /* Used during version negotiations. */
309 struct vmstorage_protocol_version version;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700310
311 /* Fibre channel address packet */
312 struct hv_fc_wwn_packet wwn_packet;
313
314 /* Number of sub-channels to create */
315 u16 sub_channel_count;
316
317 /* This will be the maximum of the union members */
318 u8 buffer[0x34];
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700319 };
320} __packed;
321
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700322/*
K. Y. Srinivasan09f03552012-01-12 12:37:54 -0800323 * Packet Flags:
324 *
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700325 * This flag indicates that the server should send back a completion for this
326 * packet.
327 */
K. Y. Srinivasan09f03552012-01-12 12:37:54 -0800328
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700329#define REQUEST_COMPLETION_FLAG 0x1
330
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700331/* Matches Windows-end */
332enum storvsc_request_type {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800333 WRITE_TYPE = 0,
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700334 READ_TYPE,
335 UNKNOWN_TYPE,
336};
337
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800338/*
339 * SRB status codes and masks; a subset of the codes used here.
340 */
341
342#define SRB_STATUS_AUTOSENSE_VALID 0x80
343#define SRB_STATUS_INVALID_LUN 0x20
344#define SRB_STATUS_SUCCESS 0x01
K. Y. Srinivasan67812092013-02-21 12:04:53 -0800345#define SRB_STATUS_ABORTED 0x02
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800346#define SRB_STATUS_ERROR 0x04
347
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800348/*
349 * This is the end of Protocol specific defines.
350 */
351
K. Y. Srinivasanb9ec3a52015-03-27 00:27:14 -0700352static int storvsc_ringbuffer_size = (256 * PAGE_SIZE);
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -0700353static u32 max_outstanding_req_per_channel;
354
355static int storvsc_vcpus_per_sub_channel = 4;
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800356
357module_param(storvsc_ringbuffer_size, int, S_IRUGO);
358MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
359
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -0700360module_param(storvsc_vcpus_per_sub_channel, int, S_IRUGO);
361MODULE_PARM_DESC(vcpus_per_sub_channel, "Ratio of VCPUs to subchannels");
K. Y. Srinivasan893def32013-06-04 12:05:05 -0700362/*
363 * Timeout in seconds for all devices managed by this driver.
364 */
365static int storvsc_timeout = 180;
366
K. Y. Srinivasanf3cfabc2014-07-21 16:06:01 -0700367static int msft_blist_flags = BLIST_TRY_VPD_PAGES;
368
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800369
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700370static void storvsc_on_channel_callback(void *context);
371
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -0700372#define STORVSC_MAX_LUNS_PER_TARGET 255
373#define STORVSC_MAX_TARGETS 2
374#define STORVSC_MAX_CHANNELS 8
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800375
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -0700376#define STORVSC_FC_MAX_LUNS_PER_TARGET 255
377#define STORVSC_FC_MAX_TARGETS 128
378#define STORVSC_FC_MAX_CHANNELS 8
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800379
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -0700380#define STORVSC_IDE_MAX_LUNS_PER_TARGET 64
381#define STORVSC_IDE_MAX_TARGETS 1
382#define STORVSC_IDE_MAX_CHANNELS 1
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800383
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800384struct storvsc_cmd_request {
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800385 struct scsi_cmnd *cmd;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700386
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800387 unsigned int bounce_sgl_count;
388 struct scatterlist *bounce_sgl;
389
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700390 struct hv_device *device;
391
392 /* Synchronize the request/response if needed */
393 struct completion wait_event;
394
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -0700395 struct vmbus_channel_packet_multipage_buffer mpb;
396 struct vmbus_packet_mpb_array *payload;
397 u32 payload_sz;
398
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700399 struct vstor_packet vstor_packet;
400};
401
402
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700403/* A storvsc device is a device object that contains a vmbus channel */
404struct storvsc_device {
405 struct hv_device *device;
406
407 bool destroy;
408 bool drain_notify;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700409 bool open_sub_channel;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700410 atomic_t num_outstanding_req;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700411 struct Scsi_Host *host;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700412
413 wait_queue_head_t waiting_to_drain;
414
415 /*
416 * Each unique Port/Path/Target represents 1 channel ie scsi
417 * controller. In reality, the pathid, targetid is always 0
418 * and the port is set by us
419 */
420 unsigned int port_number;
421 unsigned char path_id;
422 unsigned char target_id;
423
K. Y. Srinivasan5117b932015-03-27 00:27:17 -0700424 /*
425 * Max I/O, the device can support.
426 */
427 u32 max_transfer_bytes;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700428 /* Used for vsc/vsp channel reset process */
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800429 struct storvsc_cmd_request init_request;
430 struct storvsc_cmd_request reset_request;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700431};
432
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800433struct hv_host_device {
434 struct hv_device *dev;
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700435 unsigned int port;
436 unsigned char path;
437 unsigned char target;
438};
439
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800440struct storvsc_scan_work {
441 struct work_struct work;
442 struct Scsi_Host *host;
443 uint lun;
444};
445
K. Y. Srinivasan67812092013-02-21 12:04:53 -0800446static void storvsc_device_scan(struct work_struct *work)
447{
448 struct storvsc_scan_work *wrk;
449 uint lun;
450 struct scsi_device *sdev;
451
452 wrk = container_of(work, struct storvsc_scan_work, work);
453 lun = wrk->lun;
454
455 sdev = scsi_device_lookup(wrk->host, 0, 0, lun);
456 if (!sdev)
457 goto done;
458 scsi_rescan_device(&sdev->sdev_gendev);
459 scsi_device_put(sdev);
460
461done:
462 kfree(wrk);
463}
464
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -0800465static void storvsc_host_scan(struct work_struct *work)
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800466{
467 struct storvsc_scan_work *wrk;
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -0800468 struct Scsi_Host *host;
K. Y. Srinivasan34a716b2014-12-16 13:21:43 -0800469 struct scsi_device *sdev;
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800470
471 wrk = container_of(work, struct storvsc_scan_work, work);
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -0800472 host = wrk->host;
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800473
K. Y. Srinivasan34a716b2014-12-16 13:21:43 -0800474 /*
475 * Before scanning the host, first check to see if any of the
476 * currrently known devices have been hot removed. We issue a
477 * "unit ready" command against all currently known devices.
478 * This I/O will result in an error for devices that have been
479 * removed. As part of handling the I/O error, we remove the device.
480 *
481 * When a LUN is added or removed, the host sends us a signal to
482 * scan the host. Thus we are forced to discover the LUNs that
483 * may have been removed this way.
484 */
485 mutex_lock(&host->scan_mutex);
Vitaly Kuznetsov8d6a9f52015-07-01 11:31:27 +0200486 shost_for_each_device(sdev, host)
K. Y. Srinivasan34a716b2014-12-16 13:21:43 -0800487 scsi_test_unit_ready(sdev, 1, 1, NULL);
K. Y. Srinivasan34a716b2014-12-16 13:21:43 -0800488 mutex_unlock(&host->scan_mutex);
489 /*
490 * Now scan the host to discover LUNs that may have been added.
491 */
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -0800492 scsi_scan_host(host);
493
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800494 kfree(wrk);
495}
496
K. Y. Srinivasanb4017312011-11-08 09:01:50 -0800497static void storvsc_remove_lun(struct work_struct *work)
498{
499 struct storvsc_scan_work *wrk;
500 struct scsi_device *sdev;
501
502 wrk = container_of(work, struct storvsc_scan_work, work);
503 if (!scsi_host_get(wrk->host))
504 goto done;
505
506 sdev = scsi_device_lookup(wrk->host, 0, 0, wrk->lun);
507
508 if (sdev) {
509 scsi_remove_device(sdev);
510 scsi_device_put(sdev);
511 }
512 scsi_host_put(wrk->host);
513
514done:
515 kfree(wrk);
516}
517
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800518
519/*
K. Y. Srinivasana8c18c52012-01-12 12:38:00 -0800520 * We can get incoming messages from the host that are not in response to
521 * messages that we have sent out. An example of this would be messages
522 * received by the guest to notify dynamic addition/removal of LUNs. To
523 * deal with potential race conditions where the driver may be in the
524 * midst of being unloaded when we might receive an unsolicited message
525 * from the host, we have implemented a mechanism to gurantee sequential
526 * consistency:
527 *
528 * 1) Once the device is marked as being destroyed, we will fail all
529 * outgoing messages.
530 * 2) We permit incoming messages when the device is being destroyed,
531 * only to properly account for messages already sent out.
532 */
533
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700534static inline struct storvsc_device *get_out_stor_device(
535 struct hv_device *device)
536{
537 struct storvsc_device *stor_device;
538
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700539 stor_device = hv_get_drvdata(device);
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700540
541 if (stor_device && stor_device->destroy)
542 stor_device = NULL;
543
544 return stor_device;
545}
546
547
548static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
549{
550 dev->drain_notify = true;
551 wait_event(dev->waiting_to_drain,
552 atomic_read(&dev->num_outstanding_req) == 0);
553 dev->drain_notify = false;
554}
Hank Janssenbef4a342009-07-13 16:01:31 -0700555
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700556static inline struct storvsc_device *get_in_stor_device(
557 struct hv_device *device)
558{
559 struct storvsc_device *stor_device;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700560
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700561 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700562
563 if (!stor_device)
564 goto get_in_err;
565
566 /*
567 * If the device is being destroyed; allow incoming
568 * traffic only to cleanup outstanding requests.
569 */
570
571 if (stor_device->destroy &&
572 (atomic_read(&stor_device->num_outstanding_req) == 0))
573 stor_device = NULL;
574
575get_in_err:
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700576 return stor_device;
577
578}
579
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800580static void destroy_bounce_buffer(struct scatterlist *sgl,
581 unsigned int sg_count)
582{
583 int i;
584 struct page *page_buf;
585
586 for (i = 0; i < sg_count; i++) {
587 page_buf = sg_page((&sgl[i]));
588 if (page_buf != NULL)
589 __free_page(page_buf);
590 }
591
592 kfree(sgl);
593}
594
595static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
596{
597 int i;
598
599 /* No need to check */
600 if (sg_count < 2)
601 return -1;
602
603 /* We have at least 2 sg entries */
604 for (i = 0; i < sg_count; i++) {
605 if (i == 0) {
606 /* make sure 1st one does not have hole */
607 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
608 return i;
609 } else if (i == sg_count - 1) {
610 /* make sure last one does not have hole */
611 if (sgl[i].offset != 0)
612 return i;
613 } else {
614 /* make sure no hole in the middle */
615 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
616 return i;
617 }
618 }
619 return -1;
620}
621
622static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
623 unsigned int sg_count,
624 unsigned int len,
625 int write)
626{
627 int i;
628 int num_pages;
629 struct scatterlist *bounce_sgl;
630 struct page *page_buf;
631 unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);
632
633 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
634
635 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
636 if (!bounce_sgl)
637 return NULL;
638
K. Y. Srinivasan9d2696e2013-02-06 05:15:28 -0800639 sg_init_table(bounce_sgl, num_pages);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800640 for (i = 0; i < num_pages; i++) {
641 page_buf = alloc_page(GFP_ATOMIC);
642 if (!page_buf)
643 goto cleanup;
644 sg_set_page(&bounce_sgl[i], page_buf, buf_len, 0);
645 }
646
647 return bounce_sgl;
648
649cleanup:
650 destroy_bounce_buffer(bounce_sgl, num_pages);
651 return NULL;
652}
653
654/* Assume the original sgl has enough room */
655static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
656 struct scatterlist *bounce_sgl,
657 unsigned int orig_sgl_count,
658 unsigned int bounce_sgl_count)
659{
660 int i;
661 int j = 0;
662 unsigned long src, dest;
663 unsigned int srclen, destlen, copylen;
664 unsigned int total_copied = 0;
665 unsigned long bounce_addr = 0;
666 unsigned long dest_addr = 0;
667 unsigned long flags;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700668 struct scatterlist *cur_dest_sgl;
669 struct scatterlist *cur_src_sgl;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800670
671 local_irq_save(flags);
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700672 cur_dest_sgl = orig_sgl;
673 cur_src_sgl = bounce_sgl;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800674 for (i = 0; i < orig_sgl_count; i++) {
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700675 dest_addr = (unsigned long)
676 kmap_atomic(sg_page(cur_dest_sgl)) +
677 cur_dest_sgl->offset;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800678 dest = dest_addr;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700679 destlen = cur_dest_sgl->length;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800680
681 if (bounce_addr == 0)
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700682 bounce_addr = (unsigned long)kmap_atomic(
683 sg_page(cur_src_sgl));
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800684
685 while (destlen) {
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700686 src = bounce_addr + cur_src_sgl->offset;
687 srclen = cur_src_sgl->length - cur_src_sgl->offset;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800688
689 copylen = min(srclen, destlen);
690 memcpy((void *)dest, (void *)src, copylen);
691
692 total_copied += copylen;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700693 cur_src_sgl->offset += copylen;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800694 destlen -= copylen;
695 dest += copylen;
696
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700697 if (cur_src_sgl->offset == cur_src_sgl->length) {
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800698 /* full */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700699 kunmap_atomic((void *)bounce_addr);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800700 j++;
701
702 /*
703 * It is possible that the number of elements
704 * in the bounce buffer may not be equal to
705 * the number of elements in the original
706 * scatter list. Handle this correctly.
707 */
708
709 if (j == bounce_sgl_count) {
710 /*
711 * We are done; cleanup and return.
712 */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700713 kunmap_atomic((void *)(dest_addr -
714 cur_dest_sgl->offset));
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800715 local_irq_restore(flags);
716 return total_copied;
717 }
718
719 /* if we need to use another bounce buffer */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700720 if (destlen || i != orig_sgl_count - 1) {
721 cur_src_sgl = sg_next(cur_src_sgl);
722 bounce_addr = (unsigned long)
723 kmap_atomic(
724 sg_page(cur_src_sgl));
725 }
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800726 } else if (destlen == 0 && i == orig_sgl_count - 1) {
727 /* unmap the last bounce that is < PAGE_SIZE */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700728 kunmap_atomic((void *)bounce_addr);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800729 }
730 }
731
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700732 kunmap_atomic((void *)(dest_addr - cur_dest_sgl->offset));
733 cur_dest_sgl = sg_next(cur_dest_sgl);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800734 }
735
736 local_irq_restore(flags);
737
738 return total_copied;
739}
740
741/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
742static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
743 struct scatterlist *bounce_sgl,
744 unsigned int orig_sgl_count)
745{
746 int i;
747 int j = 0;
748 unsigned long src, dest;
749 unsigned int srclen, destlen, copylen;
750 unsigned int total_copied = 0;
751 unsigned long bounce_addr = 0;
752 unsigned long src_addr = 0;
753 unsigned long flags;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700754 struct scatterlist *cur_src_sgl;
755 struct scatterlist *cur_dest_sgl;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800756
757 local_irq_save(flags);
758
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700759 cur_src_sgl = orig_sgl;
760 cur_dest_sgl = bounce_sgl;
761
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800762 for (i = 0; i < orig_sgl_count; i++) {
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700763 src_addr = (unsigned long)
764 kmap_atomic(sg_page(cur_src_sgl)) +
765 cur_src_sgl->offset;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800766 src = src_addr;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700767 srclen = cur_src_sgl->length;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800768
769 if (bounce_addr == 0)
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700770 bounce_addr = (unsigned long)
771 kmap_atomic(sg_page(cur_dest_sgl));
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800772
773 while (srclen) {
774 /* assume bounce offset always == 0 */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700775 dest = bounce_addr + cur_dest_sgl->length;
776 destlen = PAGE_SIZE - cur_dest_sgl->length;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800777
778 copylen = min(srclen, destlen);
779 memcpy((void *)dest, (void *)src, copylen);
780
781 total_copied += copylen;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700782 cur_dest_sgl->length += copylen;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800783 srclen -= copylen;
784 src += copylen;
785
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700786 if (cur_dest_sgl->length == PAGE_SIZE) {
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800787 /* full..move to next entry */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700788 kunmap_atomic((void *)bounce_addr);
K. Y. Srinivasan8de58072015-03-27 00:27:18 -0700789 bounce_addr = 0;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800790 j++;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800791 }
K. Y. Srinivasan8de58072015-03-27 00:27:18 -0700792
793 /* if we need to use another bounce buffer */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700794 if (srclen && bounce_addr == 0) {
795 cur_dest_sgl = sg_next(cur_dest_sgl);
796 bounce_addr = (unsigned long)
797 kmap_atomic(
798 sg_page(cur_dest_sgl));
799 }
K. Y. Srinivasan8de58072015-03-27 00:27:18 -0700800
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800801 }
802
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700803 kunmap_atomic((void *)(src_addr - cur_src_sgl->offset));
804 cur_src_sgl = sg_next(cur_src_sgl);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800805 }
806
K. Y. Srinivasan8de58072015-03-27 00:27:18 -0700807 if (bounce_addr)
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700808 kunmap_atomic((void *)bounce_addr);
K. Y. Srinivasan8de58072015-03-27 00:27:18 -0700809
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800810 local_irq_restore(flags);
811
812 return total_copied;
813}
814
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700815static void handle_sc_creation(struct vmbus_channel *new_sc)
816{
817 struct hv_device *device = new_sc->primary_channel->device_obj;
818 struct storvsc_device *stor_device;
819 struct vmstorage_channel_properties props;
820
821 stor_device = get_out_stor_device(device);
822 if (!stor_device)
823 return;
824
825 if (stor_device->open_sub_channel == false)
826 return;
827
828 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
829
830 vmbus_open(new_sc,
831 storvsc_ringbuffer_size,
832 storvsc_ringbuffer_size,
833 (void *)&props,
834 sizeof(struct vmstorage_channel_properties),
835 storvsc_on_channel_callback, new_sc);
836}
837
838static void handle_multichannel_storage(struct hv_device *device, int max_chns)
839{
840 struct storvsc_device *stor_device;
841 int num_cpus = num_online_cpus();
842 int num_sc;
843 struct storvsc_cmd_request *request;
844 struct vstor_packet *vstor_packet;
845 int ret, t;
846
847 num_sc = ((max_chns > num_cpus) ? num_cpus : max_chns);
848 stor_device = get_out_stor_device(device);
849 if (!stor_device)
850 return;
851
852 request = &stor_device->init_request;
853 vstor_packet = &request->vstor_packet;
854
855 stor_device->open_sub_channel = true;
856 /*
857 * Establish a handler for dealing with subchannels.
858 */
859 vmbus_set_sc_create_callback(device->channel, handle_sc_creation);
860
861 /*
862 * Check to see if sub-channels have already been created. This
863 * can happen when this driver is re-loaded after unloading.
864 */
865
866 if (vmbus_are_subchannels_present(device->channel))
867 return;
868
869 stor_device->open_sub_channel = false;
870 /*
871 * Request the host to create sub-channels.
872 */
873 memset(request, 0, sizeof(struct storvsc_cmd_request));
874 init_completion(&request->wait_event);
875 vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
876 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
877 vstor_packet->sub_channel_count = num_sc;
878
879 ret = vmbus_sendpacket(device->channel, vstor_packet,
880 (sizeof(struct vstor_packet) -
881 vmscsi_size_delta),
882 (unsigned long)request,
883 VM_PKT_DATA_INBAND,
884 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
885
886 if (ret != 0)
887 return;
888
889 t = wait_for_completion_timeout(&request->wait_event, 10*HZ);
890 if (t == 0)
891 return;
892
893 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
894 vstor_packet->status != 0)
895 return;
896
897 /*
898 * Now that we created the sub-channels, invoke the check; this
899 * may trigger the callback.
900 */
901 stor_device->open_sub_channel = true;
902 vmbus_are_subchannels_present(device->channel);
903}
904
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700905static int storvsc_channel_init(struct hv_device *device)
906{
907 struct storvsc_device *stor_device;
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800908 struct storvsc_cmd_request *request;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700909 struct vstor_packet *vstor_packet;
Keith Mange1a363102015-08-13 08:43:48 -0700910 int ret, t, i;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700911 int max_chns;
912 bool process_sub_channels = false;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700913
914 stor_device = get_out_stor_device(device);
915 if (!stor_device)
916 return -ENODEV;
917
918 request = &stor_device->init_request;
919 vstor_packet = &request->vstor_packet;
920
921 /*
922 * Now, initiate the vsc/vsp initialization protocol on the open
923 * channel
924 */
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800925 memset(request, 0, sizeof(struct storvsc_cmd_request));
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700926 init_completion(&request->wait_event);
927 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
928 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
929
930 ret = vmbus_sendpacket(device->channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700931 (sizeof(struct vstor_packet) -
932 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700933 (unsigned long)request,
934 VM_PKT_DATA_INBAND,
935 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
936 if (ret != 0)
937 goto cleanup;
938
939 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
940 if (t == 0) {
941 ret = -ETIMEDOUT;
942 goto cleanup;
943 }
944
945 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
946 vstor_packet->status != 0)
947 goto cleanup;
948
949
Keith Mange1a363102015-08-13 08:43:48 -0700950 for (i = 0; i < ARRAY_SIZE(vmstor_protocols); i++) {
951 /* reuse the packet for version range supported */
952 memset(vstor_packet, 0, sizeof(struct vstor_packet));
953 vstor_packet->operation =
954 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
955 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700956
Keith Mange1a363102015-08-13 08:43:48 -0700957 vstor_packet->version.major_minor =
958 vmstor_protocols[i].protocol_version;
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800959
Keith Mange1a363102015-08-13 08:43:48 -0700960 /*
961 * The revision number is only used in Windows; set it to 0.
962 */
963 vstor_packet->version.revision = 0;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700964
Keith Mange1a363102015-08-13 08:43:48 -0700965 ret = vmbus_sendpacket(device->channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700966 (sizeof(struct vstor_packet) -
967 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700968 (unsigned long)request,
969 VM_PKT_DATA_INBAND,
970 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Keith Mange1a363102015-08-13 08:43:48 -0700971 if (ret != 0)
972 goto cleanup;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700973
Keith Mange1a363102015-08-13 08:43:48 -0700974 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
975 if (t == 0) {
976 ret = -ETIMEDOUT;
977 goto cleanup;
978 }
979
980 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO) {
981 ret = -EINVAL;
982 goto cleanup;
983 }
984
985 if (vstor_packet->status == 0) {
986 vmstor_proto_version =
987 vmstor_protocols[i].protocol_version;
988
989 sense_buffer_size =
990 vmstor_protocols[i].sense_buffer_size;
991
992 vmscsi_size_delta =
993 vmstor_protocols[i].vmscsi_size_delta;
994
995 break;
996 }
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700997 }
998
Keith Mange1a363102015-08-13 08:43:48 -0700999 if (vstor_packet->status != 0) {
1000 ret = -EINVAL;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001001 goto cleanup;
Keith Mange1a363102015-08-13 08:43:48 -07001002 }
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001003
1004
1005 memset(vstor_packet, 0, sizeof(struct vstor_packet));
1006 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
1007 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001008
1009 ret = vmbus_sendpacket(device->channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001010 (sizeof(struct vstor_packet) -
1011 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001012 (unsigned long)request,
1013 VM_PKT_DATA_INBAND,
1014 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1015
1016 if (ret != 0)
1017 goto cleanup;
1018
1019 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
1020 if (t == 0) {
1021 ret = -ETIMEDOUT;
1022 goto cleanup;
1023 }
1024
1025 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
1026 vstor_packet->status != 0)
1027 goto cleanup;
1028
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001029 /*
1030 * Check to see if multi-channel support is there.
1031 * Hosts that implement protocol version of 5.1 and above
1032 * support multi-channel.
1033 */
1034 max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
Keith Mange6ee5c612015-08-13 08:43:46 -07001035 if (vmbus_proto_version >= VERSION_WIN8) {
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001036 if (vstor_packet->storage_channel_properties.flags &
1037 STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
1038 process_sub_channels = true;
1039 }
K. Y. Srinivasan5117b932015-03-27 00:27:17 -07001040 stor_device->max_transfer_bytes =
1041 vstor_packet->storage_channel_properties.max_transfer_bytes;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001042
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001043 memset(vstor_packet, 0, sizeof(struct vstor_packet));
1044 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
1045 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1046
1047 ret = vmbus_sendpacket(device->channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001048 (sizeof(struct vstor_packet) -
1049 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001050 (unsigned long)request,
1051 VM_PKT_DATA_INBAND,
1052 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1053
1054 if (ret != 0)
1055 goto cleanup;
1056
1057 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
1058 if (t == 0) {
1059 ret = -ETIMEDOUT;
1060 goto cleanup;
1061 }
1062
1063 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
1064 vstor_packet->status != 0)
1065 goto cleanup;
1066
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001067 if (process_sub_channels)
1068 handle_multichannel_storage(device, max_chns);
1069
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001070
1071cleanup:
1072 return ret;
1073}
1074
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001075static void storvsc_handle_error(struct vmscsi_request *vm_srb,
1076 struct scsi_cmnd *scmnd,
1077 struct Scsi_Host *host,
1078 u8 asc, u8 ascq)
1079{
1080 struct storvsc_scan_work *wrk;
1081 void (*process_err_fn)(struct work_struct *work);
1082 bool do_work = false;
1083
1084 switch (vm_srb->srb_status) {
1085 case SRB_STATUS_ERROR:
1086 /*
1087 * If there is an error; offline the device since all
1088 * error recovery strategies would have already been
1089 * deployed on the host side. However, if the command
1090 * were a pass-through command deal with it appropriately.
1091 */
1092 switch (scmnd->cmnd[0]) {
1093 case ATA_16:
1094 case ATA_12:
1095 set_host_byte(scmnd, DID_PASSTHROUGH);
1096 break;
K. Y. Srinivasan3533f8602014-07-12 09:48:32 -07001097 /*
1098 * On Some Windows hosts TEST_UNIT_READY command can return
1099 * SRB_STATUS_ERROR, let the upper level code deal with it
1100 * based on the sense information.
1101 */
1102 case TEST_UNIT_READY:
1103 break;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001104 default:
1105 set_host_byte(scmnd, DID_TARGET_FAILURE);
1106 }
1107 break;
1108 case SRB_STATUS_INVALID_LUN:
1109 do_work = true;
1110 process_err_fn = storvsc_remove_lun;
1111 break;
K. Y. Srinivasan67812092013-02-21 12:04:53 -08001112 case (SRB_STATUS_ABORTED | SRB_STATUS_AUTOSENSE_VALID):
1113 if ((asc == 0x2a) && (ascq == 0x9)) {
1114 do_work = true;
1115 process_err_fn = storvsc_device_scan;
1116 /*
1117 * Retry the I/O that trigerred this.
1118 */
1119 set_host_byte(scmnd, DID_REQUEUE);
1120 }
1121 break;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001122 }
K. Y. Srinivasan67812092013-02-21 12:04:53 -08001123
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001124 if (!do_work)
1125 return;
1126
1127 /*
1128 * We need to schedule work to process this error; schedule it.
1129 */
1130 wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
1131 if (!wrk) {
1132 set_host_byte(scmnd, DID_TARGET_FAILURE);
1133 return;
1134 }
1135
1136 wrk->host = host;
1137 wrk->lun = vm_srb->lun;
1138 INIT_WORK(&wrk->work, process_err_fn);
1139 schedule_work(&wrk->work);
1140}
1141
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001142
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001143static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request)
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001144{
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001145 struct scsi_cmnd *scmnd = cmd_request->cmd;
1146 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001147 struct scsi_sense_hdr sense_hdr;
1148 struct vmscsi_request *vm_srb;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001149 struct Scsi_Host *host;
1150 struct storvsc_device *stor_dev;
1151 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001152 u32 payload_sz = cmd_request->payload_sz;
1153 void *payload = cmd_request->payload;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001154
1155 stor_dev = get_in_stor_device(dev);
1156 host = stor_dev->host;
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001157
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001158 vm_srb = &cmd_request->vstor_packet.vm_srb;
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001159 if (cmd_request->bounce_sgl_count) {
1160 if (vm_srb->data_in == READ_TYPE)
1161 copy_from_bounce_buffer(scsi_sglist(scmnd),
1162 cmd_request->bounce_sgl,
1163 scsi_sg_count(scmnd),
1164 cmd_request->bounce_sgl_count);
1165 destroy_bounce_buffer(cmd_request->bounce_sgl,
1166 cmd_request->bounce_sgl_count);
1167 }
1168
K. Y. Srinivasan42e22ca2012-04-05 12:26:52 -07001169 scmnd->result = vm_srb->scsi_status;
1170
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001171 if (scmnd->result) {
1172 if (scsi_normalize_sense(scmnd->sense_buffer,
1173 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
Hannes Reinecked811b842014-10-24 14:26:45 +02001174 scsi_print_sense_hdr(scmnd->device, "storvsc",
1175 &sense_hdr);
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001176 }
1177
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001178 if (vm_srb->srb_status != SRB_STATUS_SUCCESS)
1179 storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
1180 sense_hdr.ascq);
1181
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001182 scsi_set_resid(scmnd,
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001183 cmd_request->payload->range.len -
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001184 vm_srb->data_transfer_length);
1185
Christoph Hellwigead37002014-12-29 20:05:09 -08001186 scmnd->scsi_done(scmnd);
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001187
1188 if (payload_sz >
1189 sizeof(struct vmbus_channel_packet_multipage_buffer))
1190 kfree(payload);
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001191}
1192
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001193static void storvsc_on_io_completion(struct hv_device *device,
1194 struct vstor_packet *vstor_packet,
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001195 struct storvsc_cmd_request *request)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001196{
1197 struct storvsc_device *stor_device;
1198 struct vstor_packet *stor_pkt;
1199
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001200 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001201 stor_pkt = &request->vstor_packet;
1202
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -07001203 /*
1204 * The current SCSI handling on the host side does
1205 * not correctly handle:
1206 * INQUIRY command with page code parameter set to 0x80
1207 * MODE_SENSE command with cmd[2] == 0x1c
1208 *
1209 * Setup srb and scsi status so this won't be fatal.
1210 * We do this so we can distinguish truly fatal failues
1211 * (srb status == 0x4) and off-line the device in that case.
1212 */
1213
1214 if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
K. Y. Srinivasana8c18c52012-01-12 12:38:00 -08001215 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -07001216 vstor_packet->vm_srb.scsi_status = 0;
K. Y. Srinivasan16046322012-01-12 12:37:57 -08001217 vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -07001218 }
1219
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001220
1221 /* Copy over the status...etc */
1222 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
1223 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
1224 stor_pkt->vm_srb.sense_info_length =
1225 vstor_packet->vm_srb.sense_info_length;
1226
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001227
1228 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
1229 /* CHECK_CONDITION */
K. Y. Srinivasan16046322012-01-12 12:37:57 -08001230 if (vstor_packet->vm_srb.srb_status &
1231 SRB_STATUS_AUTOSENSE_VALID) {
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001232 /* autosense data available */
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001233
Christoph Hellwigead37002014-12-29 20:05:09 -08001234 memcpy(request->cmd->sense_buffer,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001235 vstor_packet->vm_srb.sense_data,
1236 vstor_packet->vm_srb.sense_info_length);
1237
1238 }
1239 }
1240
1241 stor_pkt->vm_srb.data_transfer_length =
1242 vstor_packet->vm_srb.data_transfer_length;
1243
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001244 storvsc_command_completion(request);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001245
1246 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
1247 stor_device->drain_notify)
1248 wake_up(&stor_device->waiting_to_drain);
1249
1250
1251}
1252
1253static void storvsc_on_receive(struct hv_device *device,
1254 struct vstor_packet *vstor_packet,
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001255 struct storvsc_cmd_request *request)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001256{
K. Y. Srinivasan12675792011-11-08 09:01:49 -08001257 struct storvsc_scan_work *work;
1258 struct storvsc_device *stor_device;
1259
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001260 switch (vstor_packet->operation) {
1261 case VSTOR_OPERATION_COMPLETE_IO:
1262 storvsc_on_io_completion(device, vstor_packet, request);
1263 break;
K. Y. Srinivasan12675792011-11-08 09:01:49 -08001264
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001265 case VSTOR_OPERATION_REMOVE_DEVICE:
K. Y. Srinivasan12675792011-11-08 09:01:49 -08001266 case VSTOR_OPERATION_ENUMERATE_BUS:
1267 stor_device = get_in_stor_device(device);
1268 work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
1269 if (!work)
1270 return;
1271
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -08001272 INIT_WORK(&work->work, storvsc_host_scan);
K. Y. Srinivasan12675792011-11-08 09:01:49 -08001273 work->host = stor_device->host;
1274 schedule_work(&work->work);
1275 break;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001276
1277 default:
1278 break;
1279 }
1280}
1281
1282static void storvsc_on_channel_callback(void *context)
1283{
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001284 struct vmbus_channel *channel = (struct vmbus_channel *)context;
1285 struct hv_device *device;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001286 struct storvsc_device *stor_device;
1287 u32 bytes_recvd;
1288 u64 request_id;
1289 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001290 struct storvsc_cmd_request *request;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001291 int ret;
1292
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001293 if (channel->primary_channel != NULL)
1294 device = channel->primary_channel->device_obj;
1295 else
1296 device = channel->device_obj;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001297
1298 stor_device = get_in_stor_device(device);
1299 if (!stor_device)
1300 return;
1301
1302 do {
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001303 ret = vmbus_recvpacket(channel, packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001304 ALIGN((sizeof(struct vstor_packet) -
1305 vmscsi_size_delta), 8),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001306 &bytes_recvd, &request_id);
1307 if (ret == 0 && bytes_recvd > 0) {
1308
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001309 request = (struct storvsc_cmd_request *)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001310 (unsigned long)request_id;
1311
1312 if ((request == &stor_device->init_request) ||
1313 (request == &stor_device->reset_request)) {
1314
1315 memcpy(&request->vstor_packet, packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001316 (sizeof(struct vstor_packet) -
1317 vmscsi_size_delta));
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001318 complete(&request->wait_event);
1319 } else {
1320 storvsc_on_receive(device,
1321 (struct vstor_packet *)packet,
1322 request);
1323 }
1324 } else {
1325 break;
1326 }
1327 } while (1);
1328
1329 return;
1330}
1331
1332static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
1333{
1334 struct vmstorage_channel_properties props;
1335 int ret;
1336
1337 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
1338
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001339 ret = vmbus_open(device->channel,
1340 ring_size,
1341 ring_size,
1342 (void *)&props,
1343 sizeof(struct vmstorage_channel_properties),
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001344 storvsc_on_channel_callback, device->channel);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001345
1346 if (ret != 0)
1347 return ret;
1348
1349 ret = storvsc_channel_init(device);
1350
1351 return ret;
1352}
1353
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -07001354static int storvsc_dev_remove(struct hv_device *device)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001355{
1356 struct storvsc_device *stor_device;
1357 unsigned long flags;
1358
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001359 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001360
1361 spin_lock_irqsave(&device->channel->inbound_lock, flags);
1362 stor_device->destroy = true;
1363 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1364
1365 /*
1366 * At this point, all outbound traffic should be disable. We
1367 * only allow inbound traffic (responses) to proceed so that
1368 * outstanding requests can be completed.
1369 */
1370
1371 storvsc_wait_to_drain(stor_device);
1372
1373 /*
1374 * Since we have already drained, we don't need to busy wait
1375 * as was done in final_release_stor_device()
1376 * Note that we cannot set the ext pointer to NULL until
1377 * we have drained - to drain the outgoing packets, we need to
1378 * allow incoming packets.
1379 */
1380 spin_lock_irqsave(&device->channel->inbound_lock, flags);
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001381 hv_set_drvdata(device, NULL);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001382 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1383
1384 /* Close the channel */
1385 vmbus_close(device->channel);
1386
1387 kfree(stor_device);
1388 return 0;
1389}
1390
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -07001391static int storvsc_do_io(struct hv_device *device,
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001392 struct storvsc_cmd_request *request)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001393{
1394 struct storvsc_device *stor_device;
1395 struct vstor_packet *vstor_packet;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001396 struct vmbus_channel *outgoing_channel;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001397 int ret = 0;
1398
1399 vstor_packet = &request->vstor_packet;
1400 stor_device = get_out_stor_device(device);
1401
1402 if (!stor_device)
1403 return -ENODEV;
1404
1405
1406 request->device = device;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001407 /*
1408 * Select an an appropriate channel to send the request out.
1409 */
1410
1411 outgoing_channel = vmbus_get_outgoing_channel(device->channel);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001412
1413
1414 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
1415
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001416 vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) -
1417 vmscsi_size_delta);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001418
1419
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001420 vstor_packet->vm_srb.sense_info_length = sense_buffer_size;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001421
1422
1423 vstor_packet->vm_srb.data_transfer_length =
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001424 request->payload->range.len;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001425
1426 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
1427
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001428 if (request->payload->range.len) {
1429
1430 ret = vmbus_sendpacket_mpb_desc(outgoing_channel,
1431 request->payload, request->payload_sz,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001432 vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001433 (sizeof(struct vstor_packet) -
1434 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001435 (unsigned long)request);
1436 } else {
K. Y. Srinivasan0147dab2015-03-27 00:27:16 -07001437 ret = vmbus_sendpacket(outgoing_channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001438 (sizeof(struct vstor_packet) -
1439 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001440 (unsigned long)request,
1441 VM_PKT_DATA_INBAND,
1442 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1443 }
1444
1445 if (ret != 0)
1446 return ret;
1447
1448 atomic_inc(&stor_device->num_outstanding_req);
1449
1450 return ret;
1451}
1452
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001453static int storvsc_device_configure(struct scsi_device *sdevice)
1454{
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001455
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001456 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
1457
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001458 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001459
K. Y. Srinivasan893def32013-06-04 12:05:05 -07001460 blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
1461
Olaf Hering3e8f4f42013-02-21 12:04:51 -08001462 sdevice->no_write_same = 1;
1463
K. Y. Srinivasanf3cfabc2014-07-21 16:06:01 -07001464 /*
1465 * Add blist flags to permit the reading of the VPD pages even when
1466 * the target may claim SPC-2 compliance. MSFT targets currently
1467 * claim SPC-2 compliance while they implement post SPC-2 features.
1468 * With this patch we can correctly handle WRITE_SAME_16 issues.
1469 */
1470 sdevice->sdev_bflags |= msft_blist_flags;
1471
K. Y. Srinivasanb0a93d92014-12-16 13:21:45 -08001472 /*
1473 * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3
1474 * if the device is a MSFT virtual device.
1475 */
1476 if (!strncmp(sdevice->vendor, "Msft", 4)) {
1477 switch (vmbus_proto_version) {
1478 case VERSION_WIN8:
1479 case VERSION_WIN8_1:
1480 sdevice->scsi_level = SCSI_SPC_3;
1481 break;
1482 }
1483 }
1484
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001485 return 0;
1486}
1487
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001488static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1489 sector_t capacity, int *info)
1490{
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001491 sector_t nsect = capacity;
1492 sector_t cylinders = nsect;
1493 int heads, sectors_pt;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001494
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001495 /*
1496 * We are making up these values; let us keep it simple.
1497 */
1498 heads = 0xff;
1499 sectors_pt = 0x3f; /* Sectors per track */
1500 sector_div(cylinders, heads * sectors_pt);
1501 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1502 cylinders = 0xffff;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001503
1504 info[0] = heads;
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001505 info[1] = sectors_pt;
1506 info[2] = (int)cylinders;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001507
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001508 return 0;
1509}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001510
K. Y. Srinivasan4b270c82012-01-12 12:37:58 -08001511static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001512{
K. Y. Srinivasan4b270c82012-01-12 12:37:58 -08001513 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1514 struct hv_device *device = host_dev->dev;
1515
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001516 struct storvsc_device *stor_device;
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001517 struct storvsc_cmd_request *request;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001518 struct vstor_packet *vstor_packet;
1519 int ret, t;
1520
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001521
K. Y. Srinivasan1eaaddf2011-08-27 11:31:03 -07001522 stor_device = get_out_stor_device(device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001523 if (!stor_device)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001524 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001525
1526 request = &stor_device->reset_request;
1527 vstor_packet = &request->vstor_packet;
1528
1529 init_completion(&request->wait_event);
1530
1531 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1532 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1533 vstor_packet->vm_srb.path_id = stor_device->path_id;
1534
1535 ret = vmbus_sendpacket(device->channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001536 (sizeof(struct vstor_packet) -
1537 vmscsi_size_delta),
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001538 (unsigned long)&stor_device->reset_request,
1539 VM_PKT_DATA_INBAND,
1540 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1541 if (ret != 0)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001542 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001543
K. Y. Srinivasan46d2eb62011-06-16 13:16:36 -07001544 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001545 if (t == 0)
1546 return TIMEOUT_ERROR;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001547
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001548
1549 /*
1550 * At this point, all outstanding requests in the adapter
1551 * should have been flushed out and return to us
K. Y. Srinivasan5c1b10a2012-10-02 11:03:31 -07001552 * There is a potential race here where the host may be in
1553 * the process of responding when we return from here.
1554 * Just wait for all in-transit packets to be accounted for
1555 * before we return from here.
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001556 */
K. Y. Srinivasan5c1b10a2012-10-02 11:03:31 -07001557 storvsc_wait_to_drain(stor_device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001558
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001559 return SUCCESS;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001560}
1561
K. Y. Srinivasan56b26e62014-07-12 09:48:30 -07001562/*
1563 * The host guarantees to respond to each command, although I/O latencies might
1564 * be unbounded on Azure. Reset the timer unconditionally to give the host a
1565 * chance to perform EH.
1566 */
1567static enum blk_eh_timer_return storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
1568{
1569 return BLK_EH_RESET_TIMER;
1570}
1571
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001572static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001573{
1574 bool allowed = true;
1575 u8 scsi_op = scmnd->cmnd[0];
1576
1577 switch (scsi_op) {
Olaf Hering3e8f4f42013-02-21 12:04:51 -08001578 /* the host does not handle WRITE_SAME, log accident usage */
1579 case WRITE_SAME:
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001580 /*
1581 * smartd sends this command and the host does not handle
1582 * this. So, don't send it.
1583 */
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001584 case SET_WINDOW:
K. Y. Srinivasan59e00e72011-11-08 09:01:42 -08001585 scmnd->result = ILLEGAL_REQUEST << 16;
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001586 allowed = false;
1587 break;
1588 default:
1589 break;
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001590 }
1591 return allowed;
1592}
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001593
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001594static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001595{
1596 int ret;
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001597 struct hv_host_device *host_dev = shost_priv(host);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001598 struct hv_device *dev = host_dev->dev;
Christoph Hellwigead37002014-12-29 20:05:09 -08001599 struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001600 int i;
1601 struct scatterlist *sgl;
1602 unsigned int sg_count = 0;
1603 struct vmscsi_request *vm_srb;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -07001604 struct scatterlist *cur_sgl;
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001605 struct vmbus_packet_mpb_array *payload;
1606 u32 payload_sz;
1607 u32 length;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001608
Keith Mange2492fd72015-08-13 08:43:47 -07001609 if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) {
K. Y. Srinivasan8caf92d2014-07-12 09:48:28 -07001610 /*
1611 * On legacy hosts filter unimplemented commands.
1612 * Future hosts are expected to correctly handle
1613 * unsupported commands. Furthermore, it is
1614 * possible that some of the currently
1615 * unsupported commands maybe supported in
1616 * future versions of the host.
1617 */
1618 if (!storvsc_scsi_cmd_ok(scmnd)) {
1619 scmnd->scsi_done(scmnd);
1620 return 0;
1621 }
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001622 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001623
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001624 /* Setup the cmd request */
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001625 cmd_request->cmd = scmnd;
1626
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001627 vm_srb = &cmd_request->vstor_packet.vm_srb;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001628 vm_srb->win8_extension.time_out_value = 60;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001629
K. Y. Srinivasanf885fb72014-07-12 09:48:31 -07001630 vm_srb->win8_extension.srb_flags |=
1631 (SRB_FLAGS_QUEUE_ACTION_ENABLE |
1632 SRB_FLAGS_DISABLE_SYNCH_TRANSFER);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001633
1634 /* Build the SRB */
1635 switch (scmnd->sc_data_direction) {
1636 case DMA_TO_DEVICE:
1637 vm_srb->data_in = WRITE_TYPE;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001638 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001639 break;
1640 case DMA_FROM_DEVICE:
1641 vm_srb->data_in = READ_TYPE;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001642 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001643 break;
Vitaly Kuznetsovcb1cf082015-06-25 18:12:11 +02001644 case DMA_NONE:
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001645 vm_srb->data_in = UNKNOWN_TYPE;
K. Y. Srinivasandc457082015-05-01 11:03:02 -07001646 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001647 break;
Vitaly Kuznetsovcb1cf082015-06-25 18:12:11 +02001648 default:
1649 /*
1650 * This is DMA_BIDIRECTIONAL or something else we are never
1651 * supposed to see here.
1652 */
1653 WARN(1, "Unexpected data direction: %d\n",
1654 scmnd->sc_data_direction);
1655 return -EINVAL;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001656 }
1657
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001658
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001659 vm_srb->port_number = host_dev->port;
1660 vm_srb->path_id = scmnd->device->channel;
1661 vm_srb->target_id = scmnd->device->id;
1662 vm_srb->lun = scmnd->device->lun;
1663
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001664 vm_srb->cdb_length = scmnd->cmd_len;
1665
1666 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1667
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001668 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1669 sg_count = scsi_sg_count(scmnd);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001670
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001671 length = scsi_bufflen(scmnd);
1672 payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
1673 payload_sz = sizeof(cmd_request->mpb);
1674
1675 if (sg_count) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001676 /* check if we need to bounce the sgl */
1677 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
1678 cmd_request->bounce_sgl =
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001679 create_bounce_buffer(sgl, sg_count,
1680 length,
K. Y. Srinivasan6e8087a2011-12-07 07:15:52 -08001681 vm_srb->data_in);
Christoph Hellwigead37002014-12-29 20:05:09 -08001682 if (!cmd_request->bounce_sgl)
1683 return SCSI_MLQUEUE_HOST_BUSY;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001684
1685 cmd_request->bounce_sgl_count =
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001686 ALIGN(length, PAGE_SIZE) >> PAGE_SHIFT;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001687
K. Y. Srinivasanfa23b8c2011-08-27 11:31:21 -07001688 if (vm_srb->data_in == WRITE_TYPE)
1689 copy_to_bounce_buffer(sgl,
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001690 cmd_request->bounce_sgl, sg_count);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001691
1692 sgl = cmd_request->bounce_sgl;
1693 sg_count = cmd_request->bounce_sgl_count;
1694 }
1695
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001696
1697 if (sg_count > MAX_PAGE_BUFFER_COUNT) {
1698
1699 payload_sz = (sg_count * sizeof(void *) +
1700 sizeof(struct vmbus_packet_mpb_array));
1701 payload = kmalloc(payload_sz, GFP_ATOMIC);
1702 if (!payload) {
1703 if (cmd_request->bounce_sgl_count)
1704 destroy_bounce_buffer(
1705 cmd_request->bounce_sgl,
1706 cmd_request->bounce_sgl_count);
1707
1708 return SCSI_MLQUEUE_DEVICE_BUSY;
1709 }
1710 }
1711
1712 payload->range.len = length;
1713 payload->range.offset = sgl[0].offset;
1714
K. Y. Srinivasanaaced992015-03-27 00:27:19 -07001715 cur_sgl = sgl;
1716 for (i = 0; i < sg_count; i++) {
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001717 payload->range.pfn_array[i] =
K. Y. Srinivasanaaced992015-03-27 00:27:19 -07001718 page_to_pfn(sg_page((cur_sgl)));
1719 cur_sgl = sg_next(cur_sgl);
1720 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001721
1722 } else if (scsi_sglist(scmnd)) {
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001723 payload->range.len = length;
1724 payload->range.offset =
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001725 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001726 payload->range.pfn_array[0] =
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001727 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1728 }
1729
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001730 cmd_request->payload = payload;
1731 cmd_request->payload_sz = payload_sz;
1732
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001733 /* Invokes the vsc to start an IO */
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001734 ret = storvsc_do_io(dev, cmd_request);
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -07001735
K. Y. Srinivasand2598f02011-08-25 09:48:58 -07001736 if (ret == -EAGAIN) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001737 /* no more space */
1738
Long Lie86fb5e82014-12-05 19:38:18 -08001739 if (cmd_request->bounce_sgl_count)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001740 destroy_bounce_buffer(cmd_request->bounce_sgl,
K. Y. Srinivasan70691ec2011-08-27 11:31:29 -07001741 cmd_request->bounce_sgl_count);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001742
Christoph Hellwigead37002014-12-29 20:05:09 -08001743 return SCSI_MLQUEUE_DEVICE_BUSY;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001744 }
1745
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001746 return 0;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001747}
1748
Hank Janssenbef4a342009-07-13 16:01:31 -07001749static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001750 .module = THIS_MODULE,
1751 .name = "storvsc_host_t",
Christoph Hellwigead37002014-12-29 20:05:09 -08001752 .cmd_size = sizeof(struct storvsc_cmd_request),
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001753 .bios_param = storvsc_get_chs,
1754 .queuecommand = storvsc_queuecommand,
1755 .eh_host_reset_handler = storvsc_host_reset_handler,
Christoph Hellwigead37002014-12-29 20:05:09 -08001756 .proc_name = "storvsc_host",
K. Y. Srinivasan56b26e62014-07-12 09:48:30 -07001757 .eh_timed_out = storvsc_eh_timed_out,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001758 .slave_configure = storvsc_device_configure,
K. Y. Srinivasan52f96142014-07-12 09:48:27 -07001759 .cmd_per_lun = 255,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001760 .this_id = -1,
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001761 .use_clustering = ENABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001762 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001763 .dma_boundary = PAGE_SIZE-1,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04001764 .no_write_same = 1,
Hank Janssenbef4a342009-07-13 16:01:31 -07001765};
1766
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001767enum {
1768 SCSI_GUID,
1769 IDE_GUID,
K. Y. Srinivasanbde6d0f2013-06-04 12:05:08 -07001770 SFC_GUID,
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001771};
1772
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001773static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001774 /* SCSI guid */
K. Y. Srinivasan35c3bc22013-01-23 17:42:43 -08001775 { HV_SCSI_GUID,
1776 .driver_data = SCSI_GUID
1777 },
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001778 /* IDE guid */
K. Y. Srinivasan35c3bc22013-01-23 17:42:43 -08001779 { HV_IDE_GUID,
1780 .driver_data = IDE_GUID
1781 },
K. Y. Srinivasanbde6d0f2013-06-04 12:05:08 -07001782 /* Fibre Channel GUID */
1783 {
1784 HV_SYNTHFC_GUID,
1785 .driver_data = SFC_GUID
1786 },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001787 { },
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001788};
Hank Janssenbef4a342009-07-13 16:01:31 -07001789
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001790MODULE_DEVICE_TABLE(vmbus, id_table);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001791
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001792static int storvsc_probe(struct hv_device *device,
1793 const struct hv_vmbus_device_id *dev_id)
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001794{
1795 int ret;
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -07001796 int num_cpus = num_online_cpus();
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001797 struct Scsi_Host *host;
1798 struct hv_host_device *host_dev;
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001799 bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001800 int target = 0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001801 struct storvsc_device *stor_device;
K. Y. Srinivasan0fb8db22014-12-16 13:21:44 -08001802 int max_luns_per_target;
1803 int max_targets;
1804 int max_channels;
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -07001805 int max_sub_channels = 0;
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001806
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001807 /*
1808 * Based on the windows host we are running on,
1809 * set state to properly communicate with the host.
1810 */
1811
Keith Mange6ee5c612015-08-13 08:43:46 -07001812 if (vmbus_proto_version < VERSION_WIN8) {
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001813 sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
1814 vmscsi_size_delta = sizeof(struct vmscsi_win8_extension);
K. Y. Srinivasan0fb8db22014-12-16 13:21:44 -08001815 max_luns_per_target = STORVSC_IDE_MAX_LUNS_PER_TARGET;
1816 max_targets = STORVSC_IDE_MAX_TARGETS;
1817 max_channels = STORVSC_IDE_MAX_CHANNELS;
Keith Mange6ee5c612015-08-13 08:43:46 -07001818 } else {
K. Y. Srinivasanadb6f9e2014-07-12 09:48:29 -07001819 sense_buffer_size = POST_WIN7_STORVSC_SENSE_BUFFER_SIZE;
1820 vmscsi_size_delta = 0;
K. Y. Srinivasan0fb8db22014-12-16 13:21:44 -08001821 max_luns_per_target = STORVSC_MAX_LUNS_PER_TARGET;
1822 max_targets = STORVSC_MAX_TARGETS;
1823 max_channels = STORVSC_MAX_CHANNELS;
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -07001824 /*
1825 * On Windows8 and above, we support sub-channels for storage.
1826 * The number of sub-channels offerred is based on the number of
1827 * VCPUs in the guest.
1828 */
1829 max_sub_channels = (num_cpus / storvsc_vcpus_per_sub_channel);
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001830 }
1831
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -07001832 scsi_driver.can_queue = (max_outstanding_req_per_channel *
1833 (max_sub_channels + 1));
1834
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001835 host = scsi_host_alloc(&scsi_driver,
1836 sizeof(struct hv_host_device));
1837 if (!host)
1838 return -ENOMEM;
1839
K. Y. Srinivasan7f33f302011-11-08 09:01:44 -08001840 host_dev = shost_priv(host);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001841 memset(host_dev, 0, sizeof(struct hv_host_device));
1842
1843 host_dev->port = host->host_no;
1844 host_dev->dev = device;
1845
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001846
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001847 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001848 if (!stor_device) {
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001849 ret = -ENOMEM;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001850 goto err_out0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001851 }
1852
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001853 stor_device->destroy = false;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001854 stor_device->open_sub_channel = false;
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001855 init_waitqueue_head(&stor_device->waiting_to_drain);
1856 stor_device->device = device;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001857 stor_device->host = host;
1858 hv_set_drvdata(device, stor_device);
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001859
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001860 stor_device->port_number = host->host_no;
1861 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001862 if (ret)
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001863 goto err_out1;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001864
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001865 host_dev->path = stor_device->path_id;
1866 host_dev->target = stor_device->target_id;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001867
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -07001868 switch (dev_id->driver_data) {
1869 case SFC_GUID:
1870 host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
1871 host->max_id = STORVSC_FC_MAX_TARGETS;
1872 host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
1873 break;
1874
1875 case SCSI_GUID:
K. Y. Srinivasan0fb8db22014-12-16 13:21:44 -08001876 host->max_lun = max_luns_per_target;
1877 host->max_id = max_targets;
1878 host->max_channel = max_channels - 1;
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -07001879 break;
1880
1881 default:
1882 host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
1883 host->max_id = STORVSC_IDE_MAX_TARGETS;
1884 host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
1885 break;
1886 }
Mike Sterlingcf55f4a2011-09-06 16:10:55 -07001887 /* max cmd length */
1888 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001889
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001890 /*
1891 * set the table size based on the info we got
1892 * from the host.
1893 */
1894 host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
1895
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001896 /* Register the HBA and start the scsi bus scan */
1897 ret = scsi_add_host(host, &device->device);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001898 if (ret != 0)
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001899 goto err_out2;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001900
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001901 if (!dev_is_ide) {
1902 scsi_scan_host(host);
K. Y. Srinivasan59d22952012-01-12 12:37:55 -08001903 } else {
1904 target = (device->dev_instance.b[5] << 8 |
1905 device->dev_instance.b[4]);
1906 ret = scsi_add_device(host, 0, target, 0);
1907 if (ret) {
1908 scsi_remove_host(host);
1909 goto err_out2;
1910 }
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001911 }
1912 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001913
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001914err_out2:
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001915 /*
1916 * Once we have connected with the host, we would need to
1917 * to invoke storvsc_dev_remove() to rollback this state and
1918 * this call also frees up the stor_device; hence the jump around
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001919 * err_out1 label.
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001920 */
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001921 storvsc_dev_remove(device);
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001922 goto err_out0;
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001923
1924err_out1:
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001925 kfree(stor_device);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001926
1927err_out0:
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001928 scsi_host_put(host);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001929 return ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001930}
1931
K. Y. Srinivasanddcbf652012-01-12 12:37:59 -08001932static int storvsc_remove(struct hv_device *dev)
1933{
1934 struct storvsc_device *stor_device = hv_get_drvdata(dev);
1935 struct Scsi_Host *host = stor_device->host;
1936
1937 scsi_remove_host(host);
1938 storvsc_dev_remove(dev);
1939 scsi_host_put(host);
1940
1941 return 0;
1942}
1943
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001944static struct hv_driver storvsc_drv = {
K. Y. Srinivasanfafb0ef2011-11-08 09:01:46 -08001945 .name = KBUILD_MODNAME,
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001946 .id_table = id_table,
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001947 .probe = storvsc_probe,
1948 .remove = storvsc_remove,
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -07001949};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07001950
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001951static int __init storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001952{
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001953
1954 /*
1955 * Divide the ring buffer data size (which is 1 page less
1956 * than the ring buffer size since that page is reserved for
1957 * the ring buffer indices) by the max request size (which is
1958 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1959 */
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001960 max_outstanding_req_per_channel =
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001961 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1962 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001963 sizeof(struct vstor_packet) + sizeof(u64) -
1964 vmscsi_size_delta,
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001965 sizeof(u64)));
Hank Janssenbef4a342009-07-13 16:01:31 -07001966
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001967 return vmbus_driver_register(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001968}
1969
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001970static void __exit storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001971{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001972 vmbus_driver_unregister(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001973}
1974
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001975MODULE_LICENSE("GPL");
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -07001976MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001977module_init(storvsc_drv_init);
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001978module_exit(storvsc_drv_exit);