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