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