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