blob: 367f66cc1f0721b35b94184836f82d0a0583f1b6 [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
K. Y. Srinivasan09f03552012-01-12 12:37:54 -080059 */
60
Keith Mange2492fd72015-08-13 08:43:47 -070061#define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
62 (((MINOR_) & 0xff)))
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -070063
Keith Mange2492fd72015-08-13 08:43:47 -070064#define VMSTOR_PROTO_VERSION_WIN7 VMSTOR_PROTO_VERSION(4, 2)
65#define VMSTOR_PROTO_VERSION_WIN8 VMSTOR_PROTO_VERSION(5, 1)
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070066
67
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070068/* Packet structure describing virtual storage requests. */
69enum vstor_packet_operation {
70 VSTOR_OPERATION_COMPLETE_IO = 1,
71 VSTOR_OPERATION_REMOVE_DEVICE = 2,
72 VSTOR_OPERATION_EXECUTE_SRB = 3,
73 VSTOR_OPERATION_RESET_LUN = 4,
74 VSTOR_OPERATION_RESET_ADAPTER = 5,
75 VSTOR_OPERATION_RESET_BUS = 6,
76 VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
77 VSTOR_OPERATION_END_INITIALIZATION = 8,
78 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
79 VSTOR_OPERATION_QUERY_PROPERTIES = 10,
K. Y. Srinivasan2b9525f2011-11-08 09:01:48 -080080 VSTOR_OPERATION_ENUMERATE_BUS = 11,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -070081 VSTOR_OPERATION_FCHBA_DATA = 12,
82 VSTOR_OPERATION_CREATE_SUB_CHANNELS = 13,
83 VSTOR_OPERATION_MAXIMUM = 13
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070084};
85
86/*
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -070087 * WWN packet for Fibre Channel HBA
88 */
89
90struct hv_fc_wwn_packet {
91 bool primary_active;
92 u8 reserved1;
93 u8 reserved2;
94 u8 primary_port_wwn[8];
95 u8 primary_node_wwn[8];
96 u8 secondary_port_wwn[8];
97 u8 secondary_node_wwn[8];
98};
99
100
101
102/*
103 * SRB Flag Bits
104 */
105
106#define SRB_FLAGS_QUEUE_ACTION_ENABLE 0x00000002
107#define SRB_FLAGS_DISABLE_DISCONNECT 0x00000004
108#define SRB_FLAGS_DISABLE_SYNCH_TRANSFER 0x00000008
109#define SRB_FLAGS_BYPASS_FROZEN_QUEUE 0x00000010
110#define SRB_FLAGS_DISABLE_AUTOSENSE 0x00000020
111#define SRB_FLAGS_DATA_IN 0x00000040
112#define SRB_FLAGS_DATA_OUT 0x00000080
113#define SRB_FLAGS_NO_DATA_TRANSFER 0x00000000
114#define SRB_FLAGS_UNSPECIFIED_DIRECTION (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT)
115#define SRB_FLAGS_NO_QUEUE_FREEZE 0x00000100
116#define SRB_FLAGS_ADAPTER_CACHE_ENABLE 0x00000200
117#define SRB_FLAGS_FREE_SENSE_BUFFER 0x00000400
118
119/*
120 * This flag indicates the request is part of the workflow for processing a D3.
121 */
122#define SRB_FLAGS_D3_PROCESSING 0x00000800
123#define SRB_FLAGS_IS_ACTIVE 0x00010000
124#define SRB_FLAGS_ALLOCATED_FROM_ZONE 0x00020000
125#define SRB_FLAGS_SGLIST_FROM_POOL 0x00040000
126#define SRB_FLAGS_BYPASS_LOCKED_QUEUE 0x00080000
127#define SRB_FLAGS_NO_KEEP_AWAKE 0x00100000
128#define SRB_FLAGS_PORT_DRIVER_ALLOCSENSE 0x00200000
129#define SRB_FLAGS_PORT_DRIVER_SENSEHASPORT 0x00400000
130#define SRB_FLAGS_DONT_START_NEXT_PACKET 0x00800000
131#define SRB_FLAGS_PORT_DRIVER_RESERVED 0x0F000000
132#define SRB_FLAGS_CLASS_DRIVER_RESERVED 0xF0000000
133
134
135/*
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700136 * Platform neutral description of a scsi request -
137 * this remains the same across the write regardless of 32/64 bit
138 * note: it's patterned off the SCSI_PASS_THROUGH structure
139 */
K. Y. Srinivasan6b2f9492012-01-12 12:38:05 -0800140#define STORVSC_MAX_CMD_LEN 0x10
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700141
142#define POST_WIN7_STORVSC_SENSE_BUFFER_SIZE 0x14
143#define PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE 0x12
144
145#define STORVSC_SENSE_BUFFER_SIZE 0x14
K. Y. Srinivasan6b2f9492012-01-12 12:38:05 -0800146#define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700147
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700148/*
149 * Sense buffer size changed in win8; have a run-time
150 * variable to track the size we should use.
151 */
152static int sense_buffer_size;
153
154/*
155 * The size of the vmscsi_request has changed in win8. The
156 * additional size is because of new elements added to the
157 * structure. These elements are valid only when we are talking
158 * to a win8 host.
159 * Track the correction to size we need to apply.
160 */
161
162static int vmscsi_size_delta;
Keith Mange2492fd72015-08-13 08:43:47 -0700163static int vmstor_proto_version;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700164
165struct vmscsi_win8_extension {
166 /*
167 * The following were added in Windows 8
168 */
169 u16 reserve;
170 u8 queue_tag;
171 u8 queue_action;
172 u32 srb_flags;
173 u32 time_out_value;
174 u32 queue_sort_ey;
175} __packed;
176
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700177struct vmscsi_request {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800178 u16 length;
179 u8 srb_status;
180 u8 scsi_status;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700181
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800182 u8 port_number;
183 u8 path_id;
184 u8 target_id;
185 u8 lun;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700186
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800187 u8 cdb_length;
188 u8 sense_info_length;
189 u8 data_in;
190 u8 reserved;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700191
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800192 u32 data_transfer_length;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700193
194 union {
K. Y. Srinivasan6b2f9492012-01-12 12:38:05 -0800195 u8 cdb[STORVSC_MAX_CMD_LEN];
196 u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
197 u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700198 };
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700199 /*
200 * The following was added in win8.
201 */
202 struct vmscsi_win8_extension win8_extension;
203
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700204} __attribute((packed));
205
206
207/*
208 * This structure is sent during the intialization phase to get the different
209 * properties of the channel.
210 */
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700211
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700212#define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1
213
214struct vmstorage_channel_properties {
215 u32 reserved;
216 u16 max_channel_cnt;
217 u16 reserved1;
218
219 u32 flags;
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800220 u32 max_transfer_bytes;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700221
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700222 u64 reserved2;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700223} __packed;
224
225/* This structure is sent during the storage protocol negotiations. */
226struct vmstorage_protocol_version {
227 /* Major (MSW) and minor (LSW) version numbers. */
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800228 u16 major_minor;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700229
230 /*
231 * Revision number is auto-incremented whenever this file is changed
232 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
233 * definitely indicate incompatibility--but it does indicate mismatched
234 * builds.
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800235 * This is only used on the windows side. Just set it to 0.
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700236 */
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800237 u16 revision;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700238} __packed;
239
240/* Channel Property Flags */
241#define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
242#define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
243
244struct vstor_packet {
245 /* Requested operation type */
246 enum vstor_packet_operation operation;
247
248 /* Flags - see below for values */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800249 u32 flags;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700250
251 /* Status of the request returned from the server side. */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800252 u32 status;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700253
254 /* Data payload area */
255 union {
256 /*
257 * Structure used to forward SCSI commands from the
258 * client to the server.
259 */
260 struct vmscsi_request vm_srb;
261
262 /* Structure used to query channel properties. */
263 struct vmstorage_channel_properties storage_channel_properties;
264
265 /* Used during version negotiations. */
266 struct vmstorage_protocol_version version;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700267
268 /* Fibre channel address packet */
269 struct hv_fc_wwn_packet wwn_packet;
270
271 /* Number of sub-channels to create */
272 u16 sub_channel_count;
273
274 /* This will be the maximum of the union members */
275 u8 buffer[0x34];
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700276 };
277} __packed;
278
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700279/*
K. Y. Srinivasan09f03552012-01-12 12:37:54 -0800280 * Packet Flags:
281 *
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700282 * This flag indicates that the server should send back a completion for this
283 * packet.
284 */
K. Y. Srinivasan09f03552012-01-12 12:37:54 -0800285
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700286#define REQUEST_COMPLETION_FLAG 0x1
287
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700288/* Matches Windows-end */
289enum storvsc_request_type {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800290 WRITE_TYPE = 0,
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700291 READ_TYPE,
292 UNKNOWN_TYPE,
293};
294
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800295/*
296 * SRB status codes and masks; a subset of the codes used here.
297 */
298
299#define SRB_STATUS_AUTOSENSE_VALID 0x80
300#define SRB_STATUS_INVALID_LUN 0x20
301#define SRB_STATUS_SUCCESS 0x01
K. Y. Srinivasan67812092013-02-21 12:04:53 -0800302#define SRB_STATUS_ABORTED 0x02
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800303#define SRB_STATUS_ERROR 0x04
304
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800305/*
306 * This is the end of Protocol specific defines.
307 */
308
K. Y. Srinivasanb9ec3a52015-03-27 00:27:14 -0700309static int storvsc_ringbuffer_size = (256 * PAGE_SIZE);
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -0700310static u32 max_outstanding_req_per_channel;
311
312static int storvsc_vcpus_per_sub_channel = 4;
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800313
314module_param(storvsc_ringbuffer_size, int, S_IRUGO);
315MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
316
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -0700317module_param(storvsc_vcpus_per_sub_channel, int, S_IRUGO);
318MODULE_PARM_DESC(vcpus_per_sub_channel, "Ratio of VCPUs to subchannels");
K. Y. Srinivasan893def32013-06-04 12:05:05 -0700319/*
320 * Timeout in seconds for all devices managed by this driver.
321 */
322static int storvsc_timeout = 180;
323
K. Y. Srinivasanf3cfabc2014-07-21 16:06:01 -0700324static int msft_blist_flags = BLIST_TRY_VPD_PAGES;
325
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800326
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700327static void storvsc_on_channel_callback(void *context);
328
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -0700329#define STORVSC_MAX_LUNS_PER_TARGET 255
330#define STORVSC_MAX_TARGETS 2
331#define STORVSC_MAX_CHANNELS 8
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800332
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -0700333#define STORVSC_FC_MAX_LUNS_PER_TARGET 255
334#define STORVSC_FC_MAX_TARGETS 128
335#define STORVSC_FC_MAX_CHANNELS 8
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800336
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -0700337#define STORVSC_IDE_MAX_LUNS_PER_TARGET 64
338#define STORVSC_IDE_MAX_TARGETS 1
339#define STORVSC_IDE_MAX_CHANNELS 1
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800340
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800341struct storvsc_cmd_request {
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800342 struct scsi_cmnd *cmd;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700343
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800344 unsigned int bounce_sgl_count;
345 struct scatterlist *bounce_sgl;
346
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700347 struct hv_device *device;
348
349 /* Synchronize the request/response if needed */
350 struct completion wait_event;
351
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -0700352 struct vmbus_channel_packet_multipage_buffer mpb;
353 struct vmbus_packet_mpb_array *payload;
354 u32 payload_sz;
355
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700356 struct vstor_packet vstor_packet;
357};
358
359
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700360/* A storvsc device is a device object that contains a vmbus channel */
361struct storvsc_device {
362 struct hv_device *device;
363
364 bool destroy;
365 bool drain_notify;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700366 bool open_sub_channel;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700367 atomic_t num_outstanding_req;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700368 struct Scsi_Host *host;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700369
370 wait_queue_head_t waiting_to_drain;
371
372 /*
373 * Each unique Port/Path/Target represents 1 channel ie scsi
374 * controller. In reality, the pathid, targetid is always 0
375 * and the port is set by us
376 */
377 unsigned int port_number;
378 unsigned char path_id;
379 unsigned char target_id;
380
K. Y. Srinivasan5117b932015-03-27 00:27:17 -0700381 /*
382 * Max I/O, the device can support.
383 */
384 u32 max_transfer_bytes;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700385 /* Used for vsc/vsp channel reset process */
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800386 struct storvsc_cmd_request init_request;
387 struct storvsc_cmd_request reset_request;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700388};
389
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800390struct hv_host_device {
391 struct hv_device *dev;
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700392 unsigned int port;
393 unsigned char path;
394 unsigned char target;
395};
396
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800397struct storvsc_scan_work {
398 struct work_struct work;
399 struct Scsi_Host *host;
400 uint lun;
401};
402
K. Y. Srinivasan67812092013-02-21 12:04:53 -0800403static void storvsc_device_scan(struct work_struct *work)
404{
405 struct storvsc_scan_work *wrk;
406 uint lun;
407 struct scsi_device *sdev;
408
409 wrk = container_of(work, struct storvsc_scan_work, work);
410 lun = wrk->lun;
411
412 sdev = scsi_device_lookup(wrk->host, 0, 0, lun);
413 if (!sdev)
414 goto done;
415 scsi_rescan_device(&sdev->sdev_gendev);
416 scsi_device_put(sdev);
417
418done:
419 kfree(wrk);
420}
421
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -0800422static void storvsc_host_scan(struct work_struct *work)
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800423{
424 struct storvsc_scan_work *wrk;
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -0800425 struct Scsi_Host *host;
K. Y. Srinivasan34a716b2014-12-16 13:21:43 -0800426 struct scsi_device *sdev;
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800427
428 wrk = container_of(work, struct storvsc_scan_work, work);
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -0800429 host = wrk->host;
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800430
K. Y. Srinivasan34a716b2014-12-16 13:21:43 -0800431 /*
432 * Before scanning the host, first check to see if any of the
433 * currrently known devices have been hot removed. We issue a
434 * "unit ready" command against all currently known devices.
435 * This I/O will result in an error for devices that have been
436 * removed. As part of handling the I/O error, we remove the device.
437 *
438 * When a LUN is added or removed, the host sends us a signal to
439 * scan the host. Thus we are forced to discover the LUNs that
440 * may have been removed this way.
441 */
442 mutex_lock(&host->scan_mutex);
Vitaly Kuznetsov8d6a9f52015-07-01 11:31:27 +0200443 shost_for_each_device(sdev, host)
K. Y. Srinivasan34a716b2014-12-16 13:21:43 -0800444 scsi_test_unit_ready(sdev, 1, 1, NULL);
K. Y. Srinivasan34a716b2014-12-16 13:21:43 -0800445 mutex_unlock(&host->scan_mutex);
446 /*
447 * Now scan the host to discover LUNs that may have been added.
448 */
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -0800449 scsi_scan_host(host);
450
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800451 kfree(wrk);
452}
453
K. Y. Srinivasanb4017312011-11-08 09:01:50 -0800454static void storvsc_remove_lun(struct work_struct *work)
455{
456 struct storvsc_scan_work *wrk;
457 struct scsi_device *sdev;
458
459 wrk = container_of(work, struct storvsc_scan_work, work);
460 if (!scsi_host_get(wrk->host))
461 goto done;
462
463 sdev = scsi_device_lookup(wrk->host, 0, 0, wrk->lun);
464
465 if (sdev) {
466 scsi_remove_device(sdev);
467 scsi_device_put(sdev);
468 }
469 scsi_host_put(wrk->host);
470
471done:
472 kfree(wrk);
473}
474
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800475
476/*
K. Y. Srinivasana8c18c52012-01-12 12:38:00 -0800477 * We can get incoming messages from the host that are not in response to
478 * messages that we have sent out. An example of this would be messages
479 * received by the guest to notify dynamic addition/removal of LUNs. To
480 * deal with potential race conditions where the driver may be in the
481 * midst of being unloaded when we might receive an unsolicited message
482 * from the host, we have implemented a mechanism to gurantee sequential
483 * consistency:
484 *
485 * 1) Once the device is marked as being destroyed, we will fail all
486 * outgoing messages.
487 * 2) We permit incoming messages when the device is being destroyed,
488 * only to properly account for messages already sent out.
489 */
490
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700491static inline struct storvsc_device *get_out_stor_device(
492 struct hv_device *device)
493{
494 struct storvsc_device *stor_device;
495
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700496 stor_device = hv_get_drvdata(device);
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700497
498 if (stor_device && stor_device->destroy)
499 stor_device = NULL;
500
501 return stor_device;
502}
503
504
505static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
506{
507 dev->drain_notify = true;
508 wait_event(dev->waiting_to_drain,
509 atomic_read(&dev->num_outstanding_req) == 0);
510 dev->drain_notify = false;
511}
Hank Janssenbef4a342009-07-13 16:01:31 -0700512
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700513static inline struct storvsc_device *get_in_stor_device(
514 struct hv_device *device)
515{
516 struct storvsc_device *stor_device;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700517
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700518 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700519
520 if (!stor_device)
521 goto get_in_err;
522
523 /*
524 * If the device is being destroyed; allow incoming
525 * traffic only to cleanup outstanding requests.
526 */
527
528 if (stor_device->destroy &&
529 (atomic_read(&stor_device->num_outstanding_req) == 0))
530 stor_device = NULL;
531
532get_in_err:
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700533 return stor_device;
534
535}
536
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800537static void destroy_bounce_buffer(struct scatterlist *sgl,
538 unsigned int sg_count)
539{
540 int i;
541 struct page *page_buf;
542
543 for (i = 0; i < sg_count; i++) {
544 page_buf = sg_page((&sgl[i]));
545 if (page_buf != NULL)
546 __free_page(page_buf);
547 }
548
549 kfree(sgl);
550}
551
552static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
553{
554 int i;
555
556 /* No need to check */
557 if (sg_count < 2)
558 return -1;
559
560 /* We have at least 2 sg entries */
561 for (i = 0; i < sg_count; i++) {
562 if (i == 0) {
563 /* make sure 1st one does not have hole */
564 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
565 return i;
566 } else if (i == sg_count - 1) {
567 /* make sure last one does not have hole */
568 if (sgl[i].offset != 0)
569 return i;
570 } else {
571 /* make sure no hole in the middle */
572 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
573 return i;
574 }
575 }
576 return -1;
577}
578
579static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
580 unsigned int sg_count,
581 unsigned int len,
582 int write)
583{
584 int i;
585 int num_pages;
586 struct scatterlist *bounce_sgl;
587 struct page *page_buf;
588 unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);
589
590 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
591
592 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
593 if (!bounce_sgl)
594 return NULL;
595
K. Y. Srinivasan9d2696e2013-02-06 05:15:28 -0800596 sg_init_table(bounce_sgl, num_pages);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800597 for (i = 0; i < num_pages; i++) {
598 page_buf = alloc_page(GFP_ATOMIC);
599 if (!page_buf)
600 goto cleanup;
601 sg_set_page(&bounce_sgl[i], page_buf, buf_len, 0);
602 }
603
604 return bounce_sgl;
605
606cleanup:
607 destroy_bounce_buffer(bounce_sgl, num_pages);
608 return NULL;
609}
610
611/* Assume the original sgl has enough room */
612static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
613 struct scatterlist *bounce_sgl,
614 unsigned int orig_sgl_count,
615 unsigned int bounce_sgl_count)
616{
617 int i;
618 int j = 0;
619 unsigned long src, dest;
620 unsigned int srclen, destlen, copylen;
621 unsigned int total_copied = 0;
622 unsigned long bounce_addr = 0;
623 unsigned long dest_addr = 0;
624 unsigned long flags;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700625 struct scatterlist *cur_dest_sgl;
626 struct scatterlist *cur_src_sgl;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800627
628 local_irq_save(flags);
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700629 cur_dest_sgl = orig_sgl;
630 cur_src_sgl = bounce_sgl;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800631 for (i = 0; i < orig_sgl_count; i++) {
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700632 dest_addr = (unsigned long)
633 kmap_atomic(sg_page(cur_dest_sgl)) +
634 cur_dest_sgl->offset;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800635 dest = dest_addr;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700636 destlen = cur_dest_sgl->length;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800637
638 if (bounce_addr == 0)
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700639 bounce_addr = (unsigned long)kmap_atomic(
640 sg_page(cur_src_sgl));
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800641
642 while (destlen) {
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700643 src = bounce_addr + cur_src_sgl->offset;
644 srclen = cur_src_sgl->length - cur_src_sgl->offset;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800645
646 copylen = min(srclen, destlen);
647 memcpy((void *)dest, (void *)src, copylen);
648
649 total_copied += copylen;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700650 cur_src_sgl->offset += copylen;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800651 destlen -= copylen;
652 dest += copylen;
653
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700654 if (cur_src_sgl->offset == cur_src_sgl->length) {
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800655 /* full */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700656 kunmap_atomic((void *)bounce_addr);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800657 j++;
658
659 /*
660 * It is possible that the number of elements
661 * in the bounce buffer may not be equal to
662 * the number of elements in the original
663 * scatter list. Handle this correctly.
664 */
665
666 if (j == bounce_sgl_count) {
667 /*
668 * We are done; cleanup and return.
669 */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700670 kunmap_atomic((void *)(dest_addr -
671 cur_dest_sgl->offset));
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800672 local_irq_restore(flags);
673 return total_copied;
674 }
675
676 /* if we need to use another bounce buffer */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700677 if (destlen || i != orig_sgl_count - 1) {
678 cur_src_sgl = sg_next(cur_src_sgl);
679 bounce_addr = (unsigned long)
680 kmap_atomic(
681 sg_page(cur_src_sgl));
682 }
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800683 } else if (destlen == 0 && i == orig_sgl_count - 1) {
684 /* unmap the last bounce that is < PAGE_SIZE */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700685 kunmap_atomic((void *)bounce_addr);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800686 }
687 }
688
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700689 kunmap_atomic((void *)(dest_addr - cur_dest_sgl->offset));
690 cur_dest_sgl = sg_next(cur_dest_sgl);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800691 }
692
693 local_irq_restore(flags);
694
695 return total_copied;
696}
697
698/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
699static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
700 struct scatterlist *bounce_sgl,
701 unsigned int orig_sgl_count)
702{
703 int i;
704 int j = 0;
705 unsigned long src, dest;
706 unsigned int srclen, destlen, copylen;
707 unsigned int total_copied = 0;
708 unsigned long bounce_addr = 0;
709 unsigned long src_addr = 0;
710 unsigned long flags;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700711 struct scatterlist *cur_src_sgl;
712 struct scatterlist *cur_dest_sgl;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800713
714 local_irq_save(flags);
715
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700716 cur_src_sgl = orig_sgl;
717 cur_dest_sgl = bounce_sgl;
718
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800719 for (i = 0; i < orig_sgl_count; i++) {
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700720 src_addr = (unsigned long)
721 kmap_atomic(sg_page(cur_src_sgl)) +
722 cur_src_sgl->offset;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800723 src = src_addr;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700724 srclen = cur_src_sgl->length;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800725
726 if (bounce_addr == 0)
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700727 bounce_addr = (unsigned long)
728 kmap_atomic(sg_page(cur_dest_sgl));
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800729
730 while (srclen) {
731 /* assume bounce offset always == 0 */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700732 dest = bounce_addr + cur_dest_sgl->length;
733 destlen = PAGE_SIZE - cur_dest_sgl->length;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800734
735 copylen = min(srclen, destlen);
736 memcpy((void *)dest, (void *)src, copylen);
737
738 total_copied += copylen;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700739 cur_dest_sgl->length += copylen;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800740 srclen -= copylen;
741 src += copylen;
742
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700743 if (cur_dest_sgl->length == PAGE_SIZE) {
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800744 /* full..move to next entry */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700745 kunmap_atomic((void *)bounce_addr);
K. Y. Srinivasan8de58072015-03-27 00:27:18 -0700746 bounce_addr = 0;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800747 j++;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800748 }
K. Y. Srinivasan8de58072015-03-27 00:27:18 -0700749
750 /* if we need to use another bounce buffer */
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700751 if (srclen && bounce_addr == 0) {
752 cur_dest_sgl = sg_next(cur_dest_sgl);
753 bounce_addr = (unsigned long)
754 kmap_atomic(
755 sg_page(cur_dest_sgl));
756 }
K. Y. Srinivasan8de58072015-03-27 00:27:18 -0700757
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800758 }
759
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700760 kunmap_atomic((void *)(src_addr - cur_src_sgl->offset));
761 cur_src_sgl = sg_next(cur_src_sgl);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800762 }
763
K. Y. Srinivasan8de58072015-03-27 00:27:18 -0700764 if (bounce_addr)
K. Y. Srinivasanaaced992015-03-27 00:27:19 -0700765 kunmap_atomic((void *)bounce_addr);
K. Y. Srinivasan8de58072015-03-27 00:27:18 -0700766
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800767 local_irq_restore(flags);
768
769 return total_copied;
770}
771
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700772static void handle_sc_creation(struct vmbus_channel *new_sc)
773{
774 struct hv_device *device = new_sc->primary_channel->device_obj;
775 struct storvsc_device *stor_device;
776 struct vmstorage_channel_properties props;
777
778 stor_device = get_out_stor_device(device);
779 if (!stor_device)
780 return;
781
782 if (stor_device->open_sub_channel == false)
783 return;
784
785 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
786
787 vmbus_open(new_sc,
788 storvsc_ringbuffer_size,
789 storvsc_ringbuffer_size,
790 (void *)&props,
791 sizeof(struct vmstorage_channel_properties),
792 storvsc_on_channel_callback, new_sc);
793}
794
795static void handle_multichannel_storage(struct hv_device *device, int max_chns)
796{
797 struct storvsc_device *stor_device;
798 int num_cpus = num_online_cpus();
799 int num_sc;
800 struct storvsc_cmd_request *request;
801 struct vstor_packet *vstor_packet;
802 int ret, t;
803
804 num_sc = ((max_chns > num_cpus) ? num_cpus : max_chns);
805 stor_device = get_out_stor_device(device);
806 if (!stor_device)
807 return;
808
809 request = &stor_device->init_request;
810 vstor_packet = &request->vstor_packet;
811
812 stor_device->open_sub_channel = true;
813 /*
814 * Establish a handler for dealing with subchannels.
815 */
816 vmbus_set_sc_create_callback(device->channel, handle_sc_creation);
817
818 /*
819 * Check to see if sub-channels have already been created. This
820 * can happen when this driver is re-loaded after unloading.
821 */
822
823 if (vmbus_are_subchannels_present(device->channel))
824 return;
825
826 stor_device->open_sub_channel = false;
827 /*
828 * Request the host to create sub-channels.
829 */
830 memset(request, 0, sizeof(struct storvsc_cmd_request));
831 init_completion(&request->wait_event);
832 vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
833 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
834 vstor_packet->sub_channel_count = num_sc;
835
836 ret = vmbus_sendpacket(device->channel, vstor_packet,
837 (sizeof(struct vstor_packet) -
838 vmscsi_size_delta),
839 (unsigned long)request,
840 VM_PKT_DATA_INBAND,
841 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
842
843 if (ret != 0)
844 return;
845
846 t = wait_for_completion_timeout(&request->wait_event, 10*HZ);
847 if (t == 0)
848 return;
849
850 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
851 vstor_packet->status != 0)
852 return;
853
854 /*
855 * Now that we created the sub-channels, invoke the check; this
856 * may trigger the callback.
857 */
858 stor_device->open_sub_channel = true;
859 vmbus_are_subchannels_present(device->channel);
860}
861
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700862static int storvsc_channel_init(struct hv_device *device)
863{
864 struct storvsc_device *stor_device;
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800865 struct storvsc_cmd_request *request;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700866 struct vstor_packet *vstor_packet;
867 int ret, t;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700868 int max_chns;
869 bool process_sub_channels = false;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700870
871 stor_device = get_out_stor_device(device);
872 if (!stor_device)
873 return -ENODEV;
874
875 request = &stor_device->init_request;
876 vstor_packet = &request->vstor_packet;
877
878 /*
879 * Now, initiate the vsc/vsp initialization protocol on the open
880 * channel
881 */
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800882 memset(request, 0, sizeof(struct storvsc_cmd_request));
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700883 init_completion(&request->wait_event);
884 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
885 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
886
887 ret = vmbus_sendpacket(device->channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700888 (sizeof(struct vstor_packet) -
889 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700890 (unsigned long)request,
891 VM_PKT_DATA_INBAND,
892 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
893 if (ret != 0)
894 goto cleanup;
895
896 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
897 if (t == 0) {
898 ret = -ETIMEDOUT;
899 goto cleanup;
900 }
901
902 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
903 vstor_packet->status != 0)
904 goto cleanup;
905
906
907 /* reuse the packet for version range supported */
908 memset(vstor_packet, 0, sizeof(struct vstor_packet));
909 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
910 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
911
Keith Mange2492fd72015-08-13 08:43:47 -0700912 vstor_packet->version.major_minor = vmstor_proto_version;
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800913
914 /*
915 * The revision number is only used in Windows; set it to 0.
916 */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800917 vstor_packet->version.revision = 0;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700918
919 ret = vmbus_sendpacket(device->channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700920 (sizeof(struct vstor_packet) -
921 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700922 (unsigned long)request,
923 VM_PKT_DATA_INBAND,
924 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
925 if (ret != 0)
926 goto cleanup;
927
928 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
929 if (t == 0) {
930 ret = -ETIMEDOUT;
931 goto cleanup;
932 }
933
934 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
935 vstor_packet->status != 0)
936 goto cleanup;
937
938
939 memset(vstor_packet, 0, sizeof(struct vstor_packet));
940 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
941 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700942
943 ret = vmbus_sendpacket(device->channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700944 (sizeof(struct vstor_packet) -
945 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700946 (unsigned long)request,
947 VM_PKT_DATA_INBAND,
948 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
949
950 if (ret != 0)
951 goto cleanup;
952
953 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
954 if (t == 0) {
955 ret = -ETIMEDOUT;
956 goto cleanup;
957 }
958
959 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
960 vstor_packet->status != 0)
961 goto cleanup;
962
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700963 /*
964 * Check to see if multi-channel support is there.
965 * Hosts that implement protocol version of 5.1 and above
966 * support multi-channel.
967 */
968 max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
Keith Mange6ee5c612015-08-13 08:43:46 -0700969 if (vmbus_proto_version >= VERSION_WIN8) {
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700970 if (vstor_packet->storage_channel_properties.flags &
971 STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
972 process_sub_channels = true;
973 }
K. Y. Srinivasan5117b932015-03-27 00:27:17 -0700974 stor_device->max_transfer_bytes =
975 vstor_packet->storage_channel_properties.max_transfer_bytes;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -0700976
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700977 memset(vstor_packet, 0, sizeof(struct vstor_packet));
978 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
979 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
980
981 ret = vmbus_sendpacket(device->channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -0700982 (sizeof(struct vstor_packet) -
983 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700984 (unsigned long)request,
985 VM_PKT_DATA_INBAND,
986 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
987
988 if (ret != 0)
989 goto cleanup;
990
991 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
992 if (t == 0) {
993 ret = -ETIMEDOUT;
994 goto cleanup;
995 }
996
997 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
998 vstor_packet->status != 0)
999 goto cleanup;
1000
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001001 if (process_sub_channels)
1002 handle_multichannel_storage(device, max_chns);
1003
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001004
1005cleanup:
1006 return ret;
1007}
1008
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001009static void storvsc_handle_error(struct vmscsi_request *vm_srb,
1010 struct scsi_cmnd *scmnd,
1011 struct Scsi_Host *host,
1012 u8 asc, u8 ascq)
1013{
1014 struct storvsc_scan_work *wrk;
1015 void (*process_err_fn)(struct work_struct *work);
1016 bool do_work = false;
1017
1018 switch (vm_srb->srb_status) {
1019 case SRB_STATUS_ERROR:
1020 /*
1021 * If there is an error; offline the device since all
1022 * error recovery strategies would have already been
1023 * deployed on the host side. However, if the command
1024 * were a pass-through command deal with it appropriately.
1025 */
1026 switch (scmnd->cmnd[0]) {
1027 case ATA_16:
1028 case ATA_12:
1029 set_host_byte(scmnd, DID_PASSTHROUGH);
1030 break;
K. Y. Srinivasan3533f8602014-07-12 09:48:32 -07001031 /*
1032 * On Some Windows hosts TEST_UNIT_READY command can return
1033 * SRB_STATUS_ERROR, let the upper level code deal with it
1034 * based on the sense information.
1035 */
1036 case TEST_UNIT_READY:
1037 break;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001038 default:
1039 set_host_byte(scmnd, DID_TARGET_FAILURE);
1040 }
1041 break;
1042 case SRB_STATUS_INVALID_LUN:
1043 do_work = true;
1044 process_err_fn = storvsc_remove_lun;
1045 break;
K. Y. Srinivasan67812092013-02-21 12:04:53 -08001046 case (SRB_STATUS_ABORTED | SRB_STATUS_AUTOSENSE_VALID):
1047 if ((asc == 0x2a) && (ascq == 0x9)) {
1048 do_work = true;
1049 process_err_fn = storvsc_device_scan;
1050 /*
1051 * Retry the I/O that trigerred this.
1052 */
1053 set_host_byte(scmnd, DID_REQUEUE);
1054 }
1055 break;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001056 }
K. Y. Srinivasan67812092013-02-21 12:04:53 -08001057
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001058 if (!do_work)
1059 return;
1060
1061 /*
1062 * We need to schedule work to process this error; schedule it.
1063 */
1064 wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
1065 if (!wrk) {
1066 set_host_byte(scmnd, DID_TARGET_FAILURE);
1067 return;
1068 }
1069
1070 wrk->host = host;
1071 wrk->lun = vm_srb->lun;
1072 INIT_WORK(&wrk->work, process_err_fn);
1073 schedule_work(&wrk->work);
1074}
1075
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001076
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001077static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request)
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001078{
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001079 struct scsi_cmnd *scmnd = cmd_request->cmd;
1080 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001081 struct scsi_sense_hdr sense_hdr;
1082 struct vmscsi_request *vm_srb;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001083 struct Scsi_Host *host;
1084 struct storvsc_device *stor_dev;
1085 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001086 u32 payload_sz = cmd_request->payload_sz;
1087 void *payload = cmd_request->payload;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001088
1089 stor_dev = get_in_stor_device(dev);
1090 host = stor_dev->host;
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001091
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001092 vm_srb = &cmd_request->vstor_packet.vm_srb;
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001093 if (cmd_request->bounce_sgl_count) {
1094 if (vm_srb->data_in == READ_TYPE)
1095 copy_from_bounce_buffer(scsi_sglist(scmnd),
1096 cmd_request->bounce_sgl,
1097 scsi_sg_count(scmnd),
1098 cmd_request->bounce_sgl_count);
1099 destroy_bounce_buffer(cmd_request->bounce_sgl,
1100 cmd_request->bounce_sgl_count);
1101 }
1102
K. Y. Srinivasan42e22ca2012-04-05 12:26:52 -07001103 scmnd->result = vm_srb->scsi_status;
1104
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001105 if (scmnd->result) {
1106 if (scsi_normalize_sense(scmnd->sense_buffer,
1107 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
Hannes Reinecked811b842014-10-24 14:26:45 +02001108 scsi_print_sense_hdr(scmnd->device, "storvsc",
1109 &sense_hdr);
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001110 }
1111
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -08001112 if (vm_srb->srb_status != SRB_STATUS_SUCCESS)
1113 storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
1114 sense_hdr.ascq);
1115
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001116 scsi_set_resid(scmnd,
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001117 cmd_request->payload->range.len -
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001118 vm_srb->data_transfer_length);
1119
Christoph Hellwigead37002014-12-29 20:05:09 -08001120 scmnd->scsi_done(scmnd);
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001121
1122 if (payload_sz >
1123 sizeof(struct vmbus_channel_packet_multipage_buffer))
1124 kfree(payload);
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001125}
1126
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001127static void storvsc_on_io_completion(struct hv_device *device,
1128 struct vstor_packet *vstor_packet,
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001129 struct storvsc_cmd_request *request)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001130{
1131 struct storvsc_device *stor_device;
1132 struct vstor_packet *stor_pkt;
1133
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001134 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001135 stor_pkt = &request->vstor_packet;
1136
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -07001137 /*
1138 * The current SCSI handling on the host side does
1139 * not correctly handle:
1140 * INQUIRY command with page code parameter set to 0x80
1141 * MODE_SENSE command with cmd[2] == 0x1c
1142 *
1143 * Setup srb and scsi status so this won't be fatal.
1144 * We do this so we can distinguish truly fatal failues
1145 * (srb status == 0x4) and off-line the device in that case.
1146 */
1147
1148 if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
K. Y. Srinivasana8c18c52012-01-12 12:38:00 -08001149 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -07001150 vstor_packet->vm_srb.scsi_status = 0;
K. Y. Srinivasan16046322012-01-12 12:37:57 -08001151 vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -07001152 }
1153
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001154
1155 /* Copy over the status...etc */
1156 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
1157 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
1158 stor_pkt->vm_srb.sense_info_length =
1159 vstor_packet->vm_srb.sense_info_length;
1160
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001161
1162 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
1163 /* CHECK_CONDITION */
K. Y. Srinivasan16046322012-01-12 12:37:57 -08001164 if (vstor_packet->vm_srb.srb_status &
1165 SRB_STATUS_AUTOSENSE_VALID) {
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001166 /* autosense data available */
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001167
Christoph Hellwigead37002014-12-29 20:05:09 -08001168 memcpy(request->cmd->sense_buffer,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001169 vstor_packet->vm_srb.sense_data,
1170 vstor_packet->vm_srb.sense_info_length);
1171
1172 }
1173 }
1174
1175 stor_pkt->vm_srb.data_transfer_length =
1176 vstor_packet->vm_srb.data_transfer_length;
1177
K. Y. Srinivasan27073882012-01-12 12:38:01 -08001178 storvsc_command_completion(request);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001179
1180 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
1181 stor_device->drain_notify)
1182 wake_up(&stor_device->waiting_to_drain);
1183
1184
1185}
1186
1187static void storvsc_on_receive(struct hv_device *device,
1188 struct vstor_packet *vstor_packet,
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001189 struct storvsc_cmd_request *request)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001190{
K. Y. Srinivasan12675792011-11-08 09:01:49 -08001191 struct storvsc_scan_work *work;
1192 struct storvsc_device *stor_device;
1193
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001194 switch (vstor_packet->operation) {
1195 case VSTOR_OPERATION_COMPLETE_IO:
1196 storvsc_on_io_completion(device, vstor_packet, request);
1197 break;
K. Y. Srinivasan12675792011-11-08 09:01:49 -08001198
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001199 case VSTOR_OPERATION_REMOVE_DEVICE:
K. Y. Srinivasan12675792011-11-08 09:01:49 -08001200 case VSTOR_OPERATION_ENUMERATE_BUS:
1201 stor_device = get_in_stor_device(device);
1202 work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
1203 if (!work)
1204 return;
1205
K. Y. Srinivasan2a09ed32014-12-16 13:21:42 -08001206 INIT_WORK(&work->work, storvsc_host_scan);
K. Y. Srinivasan12675792011-11-08 09:01:49 -08001207 work->host = stor_device->host;
1208 schedule_work(&work->work);
1209 break;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001210
1211 default:
1212 break;
1213 }
1214}
1215
1216static void storvsc_on_channel_callback(void *context)
1217{
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001218 struct vmbus_channel *channel = (struct vmbus_channel *)context;
1219 struct hv_device *device;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001220 struct storvsc_device *stor_device;
1221 u32 bytes_recvd;
1222 u64 request_id;
1223 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001224 struct storvsc_cmd_request *request;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001225 int ret;
1226
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001227 if (channel->primary_channel != NULL)
1228 device = channel->primary_channel->device_obj;
1229 else
1230 device = channel->device_obj;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001231
1232 stor_device = get_in_stor_device(device);
1233 if (!stor_device)
1234 return;
1235
1236 do {
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001237 ret = vmbus_recvpacket(channel, packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001238 ALIGN((sizeof(struct vstor_packet) -
1239 vmscsi_size_delta), 8),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001240 &bytes_recvd, &request_id);
1241 if (ret == 0 && bytes_recvd > 0) {
1242
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001243 request = (struct storvsc_cmd_request *)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001244 (unsigned long)request_id;
1245
1246 if ((request == &stor_device->init_request) ||
1247 (request == &stor_device->reset_request)) {
1248
1249 memcpy(&request->vstor_packet, packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001250 (sizeof(struct vstor_packet) -
1251 vmscsi_size_delta));
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001252 complete(&request->wait_event);
1253 } else {
1254 storvsc_on_receive(device,
1255 (struct vstor_packet *)packet,
1256 request);
1257 }
1258 } else {
1259 break;
1260 }
1261 } while (1);
1262
1263 return;
1264}
1265
1266static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
1267{
1268 struct vmstorage_channel_properties props;
1269 int ret;
1270
1271 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
1272
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001273 ret = vmbus_open(device->channel,
1274 ring_size,
1275 ring_size,
1276 (void *)&props,
1277 sizeof(struct vmstorage_channel_properties),
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001278 storvsc_on_channel_callback, device->channel);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001279
1280 if (ret != 0)
1281 return ret;
1282
1283 ret = storvsc_channel_init(device);
1284
1285 return ret;
1286}
1287
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -07001288static int storvsc_dev_remove(struct hv_device *device)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001289{
1290 struct storvsc_device *stor_device;
1291 unsigned long flags;
1292
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001293 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001294
1295 spin_lock_irqsave(&device->channel->inbound_lock, flags);
1296 stor_device->destroy = true;
1297 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1298
1299 /*
1300 * At this point, all outbound traffic should be disable. We
1301 * only allow inbound traffic (responses) to proceed so that
1302 * outstanding requests can be completed.
1303 */
1304
1305 storvsc_wait_to_drain(stor_device);
1306
1307 /*
1308 * Since we have already drained, we don't need to busy wait
1309 * as was done in final_release_stor_device()
1310 * Note that we cannot set the ext pointer to NULL until
1311 * we have drained - to drain the outgoing packets, we need to
1312 * allow incoming packets.
1313 */
1314 spin_lock_irqsave(&device->channel->inbound_lock, flags);
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001315 hv_set_drvdata(device, NULL);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001316 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1317
1318 /* Close the channel */
1319 vmbus_close(device->channel);
1320
1321 kfree(stor_device);
1322 return 0;
1323}
1324
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -07001325static int storvsc_do_io(struct hv_device *device,
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001326 struct storvsc_cmd_request *request)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001327{
1328 struct storvsc_device *stor_device;
1329 struct vstor_packet *vstor_packet;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001330 struct vmbus_channel *outgoing_channel;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001331 int ret = 0;
1332
1333 vstor_packet = &request->vstor_packet;
1334 stor_device = get_out_stor_device(device);
1335
1336 if (!stor_device)
1337 return -ENODEV;
1338
1339
1340 request->device = device;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001341 /*
1342 * Select an an appropriate channel to send the request out.
1343 */
1344
1345 outgoing_channel = vmbus_get_outgoing_channel(device->channel);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001346
1347
1348 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
1349
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001350 vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) -
1351 vmscsi_size_delta);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001352
1353
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001354 vstor_packet->vm_srb.sense_info_length = sense_buffer_size;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001355
1356
1357 vstor_packet->vm_srb.data_transfer_length =
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001358 request->payload->range.len;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001359
1360 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
1361
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001362 if (request->payload->range.len) {
1363
1364 ret = vmbus_sendpacket_mpb_desc(outgoing_channel,
1365 request->payload, request->payload_sz,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001366 vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001367 (sizeof(struct vstor_packet) -
1368 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001369 (unsigned long)request);
1370 } else {
K. Y. Srinivasan0147dab2015-03-27 00:27:16 -07001371 ret = vmbus_sendpacket(outgoing_channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001372 (sizeof(struct vstor_packet) -
1373 vmscsi_size_delta),
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001374 (unsigned long)request,
1375 VM_PKT_DATA_INBAND,
1376 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1377 }
1378
1379 if (ret != 0)
1380 return ret;
1381
1382 atomic_inc(&stor_device->num_outstanding_req);
1383
1384 return ret;
1385}
1386
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001387static int storvsc_device_configure(struct scsi_device *sdevice)
1388{
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001389
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001390 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
1391
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001392 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001393
K. Y. Srinivasan893def32013-06-04 12:05:05 -07001394 blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
1395
Olaf Hering3e8f4f42013-02-21 12:04:51 -08001396 sdevice->no_write_same = 1;
1397
K. Y. Srinivasanf3cfabc2014-07-21 16:06:01 -07001398 /*
1399 * Add blist flags to permit the reading of the VPD pages even when
1400 * the target may claim SPC-2 compliance. MSFT targets currently
1401 * claim SPC-2 compliance while they implement post SPC-2 features.
1402 * With this patch we can correctly handle WRITE_SAME_16 issues.
1403 */
1404 sdevice->sdev_bflags |= msft_blist_flags;
1405
K. Y. Srinivasanb0a93d92014-12-16 13:21:45 -08001406 /*
1407 * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3
1408 * if the device is a MSFT virtual device.
1409 */
1410 if (!strncmp(sdevice->vendor, "Msft", 4)) {
1411 switch (vmbus_proto_version) {
1412 case VERSION_WIN8:
1413 case VERSION_WIN8_1:
1414 sdevice->scsi_level = SCSI_SPC_3;
1415 break;
1416 }
1417 }
1418
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001419 return 0;
1420}
1421
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001422static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1423 sector_t capacity, int *info)
1424{
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001425 sector_t nsect = capacity;
1426 sector_t cylinders = nsect;
1427 int heads, sectors_pt;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001428
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001429 /*
1430 * We are making up these values; let us keep it simple.
1431 */
1432 heads = 0xff;
1433 sectors_pt = 0x3f; /* Sectors per track */
1434 sector_div(cylinders, heads * sectors_pt);
1435 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1436 cylinders = 0xffff;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001437
1438 info[0] = heads;
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001439 info[1] = sectors_pt;
1440 info[2] = (int)cylinders;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001441
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001442 return 0;
1443}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001444
K. Y. Srinivasan4b270c82012-01-12 12:37:58 -08001445static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001446{
K. Y. Srinivasan4b270c82012-01-12 12:37:58 -08001447 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1448 struct hv_device *device = host_dev->dev;
1449
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001450 struct storvsc_device *stor_device;
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001451 struct storvsc_cmd_request *request;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001452 struct vstor_packet *vstor_packet;
1453 int ret, t;
1454
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001455
K. Y. Srinivasan1eaaddf2011-08-27 11:31:03 -07001456 stor_device = get_out_stor_device(device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001457 if (!stor_device)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001458 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001459
1460 request = &stor_device->reset_request;
1461 vstor_packet = &request->vstor_packet;
1462
1463 init_completion(&request->wait_event);
1464
1465 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1466 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1467 vstor_packet->vm_srb.path_id = stor_device->path_id;
1468
1469 ret = vmbus_sendpacket(device->channel, vstor_packet,
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001470 (sizeof(struct vstor_packet) -
1471 vmscsi_size_delta),
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001472 (unsigned long)&stor_device->reset_request,
1473 VM_PKT_DATA_INBAND,
1474 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1475 if (ret != 0)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001476 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001477
K. Y. Srinivasan46d2eb62011-06-16 13:16:36 -07001478 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001479 if (t == 0)
1480 return TIMEOUT_ERROR;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001481
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001482
1483 /*
1484 * At this point, all outstanding requests in the adapter
1485 * should have been flushed out and return to us
K. Y. Srinivasan5c1b10a2012-10-02 11:03:31 -07001486 * There is a potential race here where the host may be in
1487 * the process of responding when we return from here.
1488 * Just wait for all in-transit packets to be accounted for
1489 * before we return from here.
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001490 */
K. Y. Srinivasan5c1b10a2012-10-02 11:03:31 -07001491 storvsc_wait_to_drain(stor_device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001492
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001493 return SUCCESS;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001494}
1495
K. Y. Srinivasan56b26e62014-07-12 09:48:30 -07001496/*
1497 * The host guarantees to respond to each command, although I/O latencies might
1498 * be unbounded on Azure. Reset the timer unconditionally to give the host a
1499 * chance to perform EH.
1500 */
1501static enum blk_eh_timer_return storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
1502{
1503 return BLK_EH_RESET_TIMER;
1504}
1505
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001506static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001507{
1508 bool allowed = true;
1509 u8 scsi_op = scmnd->cmnd[0];
1510
1511 switch (scsi_op) {
Olaf Hering3e8f4f42013-02-21 12:04:51 -08001512 /* the host does not handle WRITE_SAME, log accident usage */
1513 case WRITE_SAME:
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001514 /*
1515 * smartd sends this command and the host does not handle
1516 * this. So, don't send it.
1517 */
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001518 case SET_WINDOW:
K. Y. Srinivasan59e00e72011-11-08 09:01:42 -08001519 scmnd->result = ILLEGAL_REQUEST << 16;
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001520 allowed = false;
1521 break;
1522 default:
1523 break;
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001524 }
1525 return allowed;
1526}
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001527
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001528static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001529{
1530 int ret;
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001531 struct hv_host_device *host_dev = shost_priv(host);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001532 struct hv_device *dev = host_dev->dev;
Christoph Hellwigead37002014-12-29 20:05:09 -08001533 struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001534 int i;
1535 struct scatterlist *sgl;
1536 unsigned int sg_count = 0;
1537 struct vmscsi_request *vm_srb;
K. Y. Srinivasanaaced992015-03-27 00:27:19 -07001538 struct scatterlist *cur_sgl;
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001539 struct vmbus_packet_mpb_array *payload;
1540 u32 payload_sz;
1541 u32 length;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001542
Keith Mange2492fd72015-08-13 08:43:47 -07001543 if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) {
K. Y. Srinivasan8caf92d2014-07-12 09:48:28 -07001544 /*
1545 * On legacy hosts filter unimplemented commands.
1546 * Future hosts are expected to correctly handle
1547 * unsupported commands. Furthermore, it is
1548 * possible that some of the currently
1549 * unsupported commands maybe supported in
1550 * future versions of the host.
1551 */
1552 if (!storvsc_scsi_cmd_ok(scmnd)) {
1553 scmnd->scsi_done(scmnd);
1554 return 0;
1555 }
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001556 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001557
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001558 /* Setup the cmd request */
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001559 cmd_request->cmd = scmnd;
1560
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001561 vm_srb = &cmd_request->vstor_packet.vm_srb;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001562 vm_srb->win8_extension.time_out_value = 60;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001563
K. Y. Srinivasanf885fb72014-07-12 09:48:31 -07001564 vm_srb->win8_extension.srb_flags |=
1565 (SRB_FLAGS_QUEUE_ACTION_ENABLE |
1566 SRB_FLAGS_DISABLE_SYNCH_TRANSFER);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001567
1568 /* Build the SRB */
1569 switch (scmnd->sc_data_direction) {
1570 case DMA_TO_DEVICE:
1571 vm_srb->data_in = WRITE_TYPE;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001572 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001573 break;
1574 case DMA_FROM_DEVICE:
1575 vm_srb->data_in = READ_TYPE;
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001576 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001577 break;
Vitaly Kuznetsovcb1cf082015-06-25 18:12:11 +02001578 case DMA_NONE:
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001579 vm_srb->data_in = UNKNOWN_TYPE;
K. Y. Srinivasandc457082015-05-01 11:03:02 -07001580 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001581 break;
Vitaly Kuznetsovcb1cf082015-06-25 18:12:11 +02001582 default:
1583 /*
1584 * This is DMA_BIDIRECTIONAL or something else we are never
1585 * supposed to see here.
1586 */
1587 WARN(1, "Unexpected data direction: %d\n",
1588 scmnd->sc_data_direction);
1589 return -EINVAL;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001590 }
1591
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001592
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001593 vm_srb->port_number = host_dev->port;
1594 vm_srb->path_id = scmnd->device->channel;
1595 vm_srb->target_id = scmnd->device->id;
1596 vm_srb->lun = scmnd->device->lun;
1597
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001598 vm_srb->cdb_length = scmnd->cmd_len;
1599
1600 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1601
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001602 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1603 sg_count = scsi_sg_count(scmnd);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001604
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001605 length = scsi_bufflen(scmnd);
1606 payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
1607 payload_sz = sizeof(cmd_request->mpb);
1608
1609 if (sg_count) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001610 /* check if we need to bounce the sgl */
1611 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
1612 cmd_request->bounce_sgl =
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001613 create_bounce_buffer(sgl, sg_count,
1614 length,
K. Y. Srinivasan6e8087a2011-12-07 07:15:52 -08001615 vm_srb->data_in);
Christoph Hellwigead37002014-12-29 20:05:09 -08001616 if (!cmd_request->bounce_sgl)
1617 return SCSI_MLQUEUE_HOST_BUSY;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001618
1619 cmd_request->bounce_sgl_count =
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001620 ALIGN(length, PAGE_SIZE) >> PAGE_SHIFT;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001621
K. Y. Srinivasanfa23b8c2011-08-27 11:31:21 -07001622 if (vm_srb->data_in == WRITE_TYPE)
1623 copy_to_bounce_buffer(sgl,
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001624 cmd_request->bounce_sgl, sg_count);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001625
1626 sgl = cmd_request->bounce_sgl;
1627 sg_count = cmd_request->bounce_sgl_count;
1628 }
1629
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001630
1631 if (sg_count > MAX_PAGE_BUFFER_COUNT) {
1632
1633 payload_sz = (sg_count * sizeof(void *) +
1634 sizeof(struct vmbus_packet_mpb_array));
1635 payload = kmalloc(payload_sz, GFP_ATOMIC);
1636 if (!payload) {
1637 if (cmd_request->bounce_sgl_count)
1638 destroy_bounce_buffer(
1639 cmd_request->bounce_sgl,
1640 cmd_request->bounce_sgl_count);
1641
1642 return SCSI_MLQUEUE_DEVICE_BUSY;
1643 }
1644 }
1645
1646 payload->range.len = length;
1647 payload->range.offset = sgl[0].offset;
1648
K. Y. Srinivasanaaced992015-03-27 00:27:19 -07001649 cur_sgl = sgl;
1650 for (i = 0; i < sg_count; i++) {
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001651 payload->range.pfn_array[i] =
K. Y. Srinivasanaaced992015-03-27 00:27:19 -07001652 page_to_pfn(sg_page((cur_sgl)));
1653 cur_sgl = sg_next(cur_sgl);
1654 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001655
1656 } else if (scsi_sglist(scmnd)) {
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001657 payload->range.len = length;
1658 payload->range.offset =
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001659 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001660 payload->range.pfn_array[0] =
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001661 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1662 }
1663
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001664 cmd_request->payload = payload;
1665 cmd_request->payload_sz = payload_sz;
1666
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001667 /* Invokes the vsc to start an IO */
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001668 ret = storvsc_do_io(dev, cmd_request);
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -07001669
K. Y. Srinivasand2598f02011-08-25 09:48:58 -07001670 if (ret == -EAGAIN) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001671 /* no more space */
1672
Long Lie86fb5e82014-12-05 19:38:18 -08001673 if (cmd_request->bounce_sgl_count)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001674 destroy_bounce_buffer(cmd_request->bounce_sgl,
K. Y. Srinivasan70691ec2011-08-27 11:31:29 -07001675 cmd_request->bounce_sgl_count);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001676
Christoph Hellwigead37002014-12-29 20:05:09 -08001677 return SCSI_MLQUEUE_DEVICE_BUSY;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001678 }
1679
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001680 return 0;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001681}
1682
Hank Janssenbef4a342009-07-13 16:01:31 -07001683static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001684 .module = THIS_MODULE,
1685 .name = "storvsc_host_t",
Christoph Hellwigead37002014-12-29 20:05:09 -08001686 .cmd_size = sizeof(struct storvsc_cmd_request),
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001687 .bios_param = storvsc_get_chs,
1688 .queuecommand = storvsc_queuecommand,
1689 .eh_host_reset_handler = storvsc_host_reset_handler,
Christoph Hellwigead37002014-12-29 20:05:09 -08001690 .proc_name = "storvsc_host",
K. Y. Srinivasan56b26e62014-07-12 09:48:30 -07001691 .eh_timed_out = storvsc_eh_timed_out,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001692 .slave_configure = storvsc_device_configure,
K. Y. Srinivasan52f96142014-07-12 09:48:27 -07001693 .cmd_per_lun = 255,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001694 .this_id = -1,
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001695 .use_clustering = ENABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001696 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001697 .dma_boundary = PAGE_SIZE-1,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04001698 .no_write_same = 1,
Hank Janssenbef4a342009-07-13 16:01:31 -07001699};
1700
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001701enum {
1702 SCSI_GUID,
1703 IDE_GUID,
K. Y. Srinivasanbde6d0f2013-06-04 12:05:08 -07001704 SFC_GUID,
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001705};
1706
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001707static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001708 /* SCSI guid */
K. Y. Srinivasan35c3bc22013-01-23 17:42:43 -08001709 { HV_SCSI_GUID,
1710 .driver_data = SCSI_GUID
1711 },
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001712 /* IDE guid */
K. Y. Srinivasan35c3bc22013-01-23 17:42:43 -08001713 { HV_IDE_GUID,
1714 .driver_data = IDE_GUID
1715 },
K. Y. Srinivasanbde6d0f2013-06-04 12:05:08 -07001716 /* Fibre Channel GUID */
1717 {
1718 HV_SYNTHFC_GUID,
1719 .driver_data = SFC_GUID
1720 },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001721 { },
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001722};
Hank Janssenbef4a342009-07-13 16:01:31 -07001723
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001724MODULE_DEVICE_TABLE(vmbus, id_table);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001725
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001726static int storvsc_probe(struct hv_device *device,
1727 const struct hv_vmbus_device_id *dev_id)
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001728{
1729 int ret;
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -07001730 int num_cpus = num_online_cpus();
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001731 struct Scsi_Host *host;
1732 struct hv_host_device *host_dev;
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001733 bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001734 int target = 0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001735 struct storvsc_device *stor_device;
K. Y. Srinivasan0fb8db22014-12-16 13:21:44 -08001736 int max_luns_per_target;
1737 int max_targets;
1738 int max_channels;
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -07001739 int max_sub_channels = 0;
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001740
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001741 /*
1742 * Based on the windows host we are running on,
1743 * set state to properly communicate with the host.
1744 */
1745
Keith Mange6ee5c612015-08-13 08:43:46 -07001746 if (vmbus_proto_version < VERSION_WIN8) {
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001747 sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
1748 vmscsi_size_delta = sizeof(struct vmscsi_win8_extension);
Keith Mange2492fd72015-08-13 08:43:47 -07001749 vmstor_proto_version = VMSTOR_PROTO_VERSION_WIN7;
K. Y. Srinivasan0fb8db22014-12-16 13:21:44 -08001750 max_luns_per_target = STORVSC_IDE_MAX_LUNS_PER_TARGET;
1751 max_targets = STORVSC_IDE_MAX_TARGETS;
1752 max_channels = STORVSC_IDE_MAX_CHANNELS;
Keith Mange6ee5c612015-08-13 08:43:46 -07001753 } else {
K. Y. Srinivasanadb6f9e2014-07-12 09:48:29 -07001754 sense_buffer_size = POST_WIN7_STORVSC_SENSE_BUFFER_SIZE;
1755 vmscsi_size_delta = 0;
Keith Mange2492fd72015-08-13 08:43:47 -07001756 vmstor_proto_version = VMSTOR_PROTO_VERSION_WIN8;
K. Y. Srinivasan0fb8db22014-12-16 13:21:44 -08001757 max_luns_per_target = STORVSC_MAX_LUNS_PER_TARGET;
1758 max_targets = STORVSC_MAX_TARGETS;
1759 max_channels = STORVSC_MAX_CHANNELS;
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -07001760 /*
1761 * On Windows8 and above, we support sub-channels for storage.
1762 * The number of sub-channels offerred is based on the number of
1763 * VCPUs in the guest.
1764 */
1765 max_sub_channels = (num_cpus / storvsc_vcpus_per_sub_channel);
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001766 }
1767
K. Y. Srinivasanf458aad2015-03-27 00:27:15 -07001768 scsi_driver.can_queue = (max_outstanding_req_per_channel *
1769 (max_sub_channels + 1));
1770
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001771 host = scsi_host_alloc(&scsi_driver,
1772 sizeof(struct hv_host_device));
1773 if (!host)
1774 return -ENOMEM;
1775
K. Y. Srinivasan7f33f302011-11-08 09:01:44 -08001776 host_dev = shost_priv(host);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001777 memset(host_dev, 0, sizeof(struct hv_host_device));
1778
1779 host_dev->port = host->host_no;
1780 host_dev->dev = device;
1781
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001782
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001783 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001784 if (!stor_device) {
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001785 ret = -ENOMEM;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001786 goto err_out0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001787 }
1788
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001789 stor_device->destroy = false;
K. Y. Srinivasan6f94d5d2013-06-04 12:05:07 -07001790 stor_device->open_sub_channel = false;
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001791 init_waitqueue_head(&stor_device->waiting_to_drain);
1792 stor_device->device = device;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001793 stor_device->host = host;
1794 hv_set_drvdata(device, stor_device);
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001795
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001796 stor_device->port_number = host->host_no;
1797 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001798 if (ret)
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001799 goto err_out1;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001800
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001801 host_dev->path = stor_device->path_id;
1802 host_dev->target = stor_device->target_id;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001803
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -07001804 switch (dev_id->driver_data) {
1805 case SFC_GUID:
1806 host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
1807 host->max_id = STORVSC_FC_MAX_TARGETS;
1808 host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
1809 break;
1810
1811 case SCSI_GUID:
K. Y. Srinivasan0fb8db22014-12-16 13:21:44 -08001812 host->max_lun = max_luns_per_target;
1813 host->max_id = max_targets;
1814 host->max_channel = max_channels - 1;
K. Y. Srinivasan4cd83ec2014-07-12 09:48:26 -07001815 break;
1816
1817 default:
1818 host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
1819 host->max_id = STORVSC_IDE_MAX_TARGETS;
1820 host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
1821 break;
1822 }
Mike Sterlingcf55f4a2011-09-06 16:10:55 -07001823 /* max cmd length */
1824 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001825
K. Y. Srinivasanbe0cf6c2015-03-27 00:27:20 -07001826 /*
1827 * set the table size based on the info we got
1828 * from the host.
1829 */
1830 host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
1831
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001832 /* Register the HBA and start the scsi bus scan */
1833 ret = scsi_add_host(host, &device->device);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001834 if (ret != 0)
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001835 goto err_out2;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001836
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001837 if (!dev_is_ide) {
1838 scsi_scan_host(host);
K. Y. Srinivasan59d22952012-01-12 12:37:55 -08001839 } else {
1840 target = (device->dev_instance.b[5] << 8 |
1841 device->dev_instance.b[4]);
1842 ret = scsi_add_device(host, 0, target, 0);
1843 if (ret) {
1844 scsi_remove_host(host);
1845 goto err_out2;
1846 }
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001847 }
1848 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001849
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001850err_out2:
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001851 /*
1852 * Once we have connected with the host, we would need to
1853 * to invoke storvsc_dev_remove() to rollback this state and
1854 * this call also frees up the stor_device; hence the jump around
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001855 * err_out1 label.
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001856 */
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001857 storvsc_dev_remove(device);
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001858 goto err_out0;
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001859
1860err_out1:
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001861 kfree(stor_device);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001862
1863err_out0:
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001864 scsi_host_put(host);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001865 return ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001866}
1867
K. Y. Srinivasanddcbf652012-01-12 12:37:59 -08001868static int storvsc_remove(struct hv_device *dev)
1869{
1870 struct storvsc_device *stor_device = hv_get_drvdata(dev);
1871 struct Scsi_Host *host = stor_device->host;
1872
1873 scsi_remove_host(host);
1874 storvsc_dev_remove(dev);
1875 scsi_host_put(host);
1876
1877 return 0;
1878}
1879
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001880static struct hv_driver storvsc_drv = {
K. Y. Srinivasanfafb0ef2011-11-08 09:01:46 -08001881 .name = KBUILD_MODNAME,
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001882 .id_table = id_table,
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001883 .probe = storvsc_probe,
1884 .remove = storvsc_remove,
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -07001885};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07001886
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001887static int __init storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001888{
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001889
1890 /*
1891 * Divide the ring buffer data size (which is 1 page less
1892 * than the ring buffer size since that page is reserved for
1893 * the ring buffer indices) by the max request size (which is
1894 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1895 */
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001896 max_outstanding_req_per_channel =
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001897 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1898 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
K. Y. Srinivasan8b612fa2013-06-04 12:05:06 -07001899 sizeof(struct vstor_packet) + sizeof(u64) -
1900 vmscsi_size_delta,
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001901 sizeof(u64)));
Hank Janssenbef4a342009-07-13 16:01:31 -07001902
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001903 return vmbus_driver_register(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001904}
1905
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001906static void __exit storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001907{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001908 vmbus_driver_unregister(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001909}
1910
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001911MODULE_LICENSE("GPL");
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -07001912MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001913module_init(storvsc_drv_init);
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001914module_exit(storvsc_drv_exit);