1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2009, Microsoft Corporation.
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
8 * K. Y. Srinivasan <kys@microsoft.com>
11 #include <linux/kernel.h>
12 #include <linux/wait.h>
13 #include <linux/sched.h>
14 #include <linux/completion.h>
15 #include <linux/string.h>
17 #include <linux/delay.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/device.h>
22 #include <linux/hyperv.h>
23 #include <linux/blkdev.h>
24 #include <linux/dma-mapping.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_cmnd.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_tcq.h>
31 #include <scsi/scsi_eh.h>
32 #include <scsi/scsi_devinfo.h>
33 #include <scsi/scsi_dbg.h>
34 #include <scsi/scsi_transport_fc.h>
35 #include <scsi/scsi_transport.h>
38 * All wire protocol details (storage protocol between the guest and the host)
39 * are consolidated here.
41 * Begin protocol definitions.
47 * V1 RC < 2008/1/31: 1.0
48 * V1 RC > 2008/1/31: 2.0
55 #define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
58 #define VMSTOR_PROTO_VERSION_WIN6 VMSTOR_PROTO_VERSION(2, 0)
59 #define VMSTOR_PROTO_VERSION_WIN7 VMSTOR_PROTO_VERSION(4, 2)
60 #define VMSTOR_PROTO_VERSION_WIN8 VMSTOR_PROTO_VERSION(5, 1)
61 #define VMSTOR_PROTO_VERSION_WIN8_1 VMSTOR_PROTO_VERSION(6, 0)
62 #define VMSTOR_PROTO_VERSION_WIN10 VMSTOR_PROTO_VERSION(6, 2)
64 /* Packet structure describing virtual storage requests. */
65 enum vstor_packet_operation {
66 VSTOR_OPERATION_COMPLETE_IO = 1,
67 VSTOR_OPERATION_REMOVE_DEVICE = 2,
68 VSTOR_OPERATION_EXECUTE_SRB = 3,
69 VSTOR_OPERATION_RESET_LUN = 4,
70 VSTOR_OPERATION_RESET_ADAPTER = 5,
71 VSTOR_OPERATION_RESET_BUS = 6,
72 VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
73 VSTOR_OPERATION_END_INITIALIZATION = 8,
74 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
75 VSTOR_OPERATION_QUERY_PROPERTIES = 10,
76 VSTOR_OPERATION_ENUMERATE_BUS = 11,
77 VSTOR_OPERATION_FCHBA_DATA = 12,
78 VSTOR_OPERATION_CREATE_SUB_CHANNELS = 13,
79 VSTOR_OPERATION_MAXIMUM = 13
83 * WWN packet for Fibre Channel HBA
86 struct hv_fc_wwn_packet {
89 u8 primary_port_wwn[8];
90 u8 primary_node_wwn[8];
91 u8 secondary_port_wwn[8];
92 u8 secondary_node_wwn[8];
101 #define SRB_FLAGS_QUEUE_ACTION_ENABLE 0x00000002
102 #define SRB_FLAGS_DISABLE_DISCONNECT 0x00000004
103 #define SRB_FLAGS_DISABLE_SYNCH_TRANSFER 0x00000008
104 #define SRB_FLAGS_BYPASS_FROZEN_QUEUE 0x00000010
105 #define SRB_FLAGS_DISABLE_AUTOSENSE 0x00000020
106 #define SRB_FLAGS_DATA_IN 0x00000040
107 #define SRB_FLAGS_DATA_OUT 0x00000080
108 #define SRB_FLAGS_NO_DATA_TRANSFER 0x00000000
109 #define SRB_FLAGS_UNSPECIFIED_DIRECTION (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT)
110 #define SRB_FLAGS_NO_QUEUE_FREEZE 0x00000100
111 #define SRB_FLAGS_ADAPTER_CACHE_ENABLE 0x00000200
112 #define SRB_FLAGS_FREE_SENSE_BUFFER 0x00000400
115 * This flag indicates the request is part of the workflow for processing a D3.
117 #define SRB_FLAGS_D3_PROCESSING 0x00000800
118 #define SRB_FLAGS_IS_ACTIVE 0x00010000
119 #define SRB_FLAGS_ALLOCATED_FROM_ZONE 0x00020000
120 #define SRB_FLAGS_SGLIST_FROM_POOL 0x00040000
121 #define SRB_FLAGS_BYPASS_LOCKED_QUEUE 0x00080000
122 #define SRB_FLAGS_NO_KEEP_AWAKE 0x00100000
123 #define SRB_FLAGS_PORT_DRIVER_ALLOCSENSE 0x00200000
124 #define SRB_FLAGS_PORT_DRIVER_SENSEHASPORT 0x00400000
125 #define SRB_FLAGS_DONT_START_NEXT_PACKET 0x00800000
126 #define SRB_FLAGS_PORT_DRIVER_RESERVED 0x0F000000
127 #define SRB_FLAGS_CLASS_DRIVER_RESERVED 0xF0000000
129 #define SP_UNTAGGED ((unsigned char) ~0)
130 #define SRB_SIMPLE_TAG_REQUEST 0x20
133 * Platform neutral description of a scsi request -
134 * this remains the same across the write regardless of 32/64 bit
135 * note: it's patterned off the SCSI_PASS_THROUGH structure
137 #define STORVSC_MAX_CMD_LEN 0x10
139 #define POST_WIN7_STORVSC_SENSE_BUFFER_SIZE 0x14
140 #define PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE 0x12
142 #define STORVSC_SENSE_BUFFER_SIZE 0x14
143 #define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
146 * Sense buffer size changed in win8; have a run-time
147 * variable to track the size we should use. This value will
148 * likely change during protocol negotiation but it is valid
149 * to start by assuming pre-Win8.
151 static int sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
154 * The storage protocol version is determined during the
155 * initial exchange with the host. It will indicate which
156 * storage functionality is available in the host.
158 static int vmstor_proto_version;
160 #define STORVSC_LOGGING_NONE 0
161 #define STORVSC_LOGGING_ERROR 1
162 #define STORVSC_LOGGING_WARN 2
164 static int logging_level = STORVSC_LOGGING_ERROR;
165 module_param(logging_level, int, S_IRUGO|S_IWUSR);
166 MODULE_PARM_DESC(logging_level,
167 "Logging level, 0 - None, 1 - Error (default), 2 - Warning.");
169 static inline bool do_logging(int level)
171 return logging_level >= level;
174 #define storvsc_log(dev, level, fmt, ...) \
176 if (do_logging(level)) \
177 dev_warn(&(dev)->device, fmt, ##__VA_ARGS__); \
180 struct vmscsi_win8_extension {
182 * The following were added in Windows 8
192 struct vmscsi_request {
203 u8 sense_info_length;
207 u32 data_transfer_length;
210 u8 cdb[STORVSC_MAX_CMD_LEN];
211 u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
212 u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
215 * The following was added in win8.
217 struct vmscsi_win8_extension win8_extension;
219 } __attribute((packed));
222 * The list of storage protocols in order of preference.
224 struct vmstor_protocol {
225 int protocol_version;
226 int sense_buffer_size;
227 int vmscsi_size_delta;
231 static const struct vmstor_protocol vmstor_protocols[] = {
233 VMSTOR_PROTO_VERSION_WIN10,
234 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
238 VMSTOR_PROTO_VERSION_WIN8_1,
239 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
243 VMSTOR_PROTO_VERSION_WIN8,
244 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
248 VMSTOR_PROTO_VERSION_WIN7,
249 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
250 sizeof(struct vmscsi_win8_extension),
253 VMSTOR_PROTO_VERSION_WIN6,
254 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
255 sizeof(struct vmscsi_win8_extension),
261 * This structure is sent during the initialization phase to get the different
262 * properties of the channel.
265 #define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1
267 struct vmstorage_channel_properties {
273 u32 max_transfer_bytes;
278 /* This structure is sent during the storage protocol negotiations. */
279 struct vmstorage_protocol_version {
280 /* Major (MSW) and minor (LSW) version numbers. */
284 * Revision number is auto-incremented whenever this file is changed
285 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
286 * definitely indicate incompatibility--but it does indicate mismatched
288 * This is only used on the windows side. Just set it to 0.
293 /* Channel Property Flags */
294 #define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
295 #define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
297 struct vstor_packet {
298 /* Requested operation type */
299 enum vstor_packet_operation operation;
301 /* Flags - see below for values */
304 /* Status of the request returned from the server side. */
307 /* Data payload area */
310 * Structure used to forward SCSI commands from the
311 * client to the server.
313 struct vmscsi_request vm_srb;
315 /* Structure used to query channel properties. */
316 struct vmstorage_channel_properties storage_channel_properties;
318 /* Used during version negotiations. */
319 struct vmstorage_protocol_version version;
321 /* Fibre channel address packet */
322 struct hv_fc_wwn_packet wwn_packet;
324 /* Number of sub-channels to create */
325 u16 sub_channel_count;
327 /* This will be the maximum of the union members */
335 * This flag indicates that the server should send back a completion for this
339 #define REQUEST_COMPLETION_FLAG 0x1
341 /* Matches Windows-end */
342 enum storvsc_request_type {
349 * SRB status codes and masks; a subset of the codes used here.
352 #define SRB_STATUS_AUTOSENSE_VALID 0x80
353 #define SRB_STATUS_QUEUE_FROZEN 0x40
354 #define SRB_STATUS_INVALID_LUN 0x20
355 #define SRB_STATUS_SUCCESS 0x01
356 #define SRB_STATUS_ABORTED 0x02
357 #define SRB_STATUS_ERROR 0x04
358 #define SRB_STATUS_DATA_OVERRUN 0x12
360 #define SRB_STATUS(status) \
361 (status & ~(SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_QUEUE_FROZEN))
363 * This is the end of Protocol specific defines.
366 static int storvsc_ringbuffer_size = (128 * 1024);
367 static u32 max_outstanding_req_per_channel;
368 static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth);
370 static int storvsc_vcpus_per_sub_channel = 4;
371 static unsigned int storvsc_max_hw_queues;
373 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
374 MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
376 module_param(storvsc_max_hw_queues, uint, 0644);
377 MODULE_PARM_DESC(storvsc_max_hw_queues, "Maximum number of hardware queues");
379 module_param(storvsc_vcpus_per_sub_channel, int, S_IRUGO);
380 MODULE_PARM_DESC(storvsc_vcpus_per_sub_channel, "Ratio of VCPUs to subchannels");
382 static int ring_avail_percent_lowater = 10;
383 module_param(ring_avail_percent_lowater, int, S_IRUGO);
384 MODULE_PARM_DESC(ring_avail_percent_lowater,
385 "Select a channel if available ring size > this in percent");
388 * Timeout in seconds for all devices managed by this driver.
390 static int storvsc_timeout = 180;
392 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
393 static struct scsi_transport_template *fc_transport_template;
396 static struct scsi_host_template scsi_driver;
397 static void storvsc_on_channel_callback(void *context);
399 #define STORVSC_MAX_LUNS_PER_TARGET 255
400 #define STORVSC_MAX_TARGETS 2
401 #define STORVSC_MAX_CHANNELS 8
403 #define STORVSC_FC_MAX_LUNS_PER_TARGET 255
404 #define STORVSC_FC_MAX_TARGETS 128
405 #define STORVSC_FC_MAX_CHANNELS 8
407 #define STORVSC_IDE_MAX_LUNS_PER_TARGET 64
408 #define STORVSC_IDE_MAX_TARGETS 1
409 #define STORVSC_IDE_MAX_CHANNELS 1
412 * Upper bound on the size of a storvsc packet. vmscsi_size_delta is not
413 * included in the calculation because it is set after STORVSC_MAX_PKT_SIZE
414 * is used in storvsc_connect_to_vsp
416 #define STORVSC_MAX_PKT_SIZE (sizeof(struct vmpacket_descriptor) +\
417 sizeof(struct vstor_packet))
419 struct storvsc_cmd_request {
420 struct scsi_cmnd *cmd;
422 struct hv_device *device;
424 /* Synchronize the request/response if needed */
425 struct completion wait_event;
427 struct vmbus_channel_packet_multipage_buffer mpb;
428 struct vmbus_packet_mpb_array *payload;
431 struct vstor_packet vstor_packet;
435 /* A storvsc device is a device object that contains a vmbus channel */
436 struct storvsc_device {
437 struct hv_device *device;
441 atomic_t num_outstanding_req;
442 struct Scsi_Host *host;
444 wait_queue_head_t waiting_to_drain;
447 * Each unique Port/Path/Target represents 1 channel ie scsi
448 * controller. In reality, the pathid, targetid is always 0
449 * and the port is set by us
451 unsigned int port_number;
452 unsigned char path_id;
453 unsigned char target_id;
456 * The size of the vmscsi_request has changed in win8. The
457 * additional size is because of new elements added to the
458 * structure. These elements are valid only when we are talking
460 * Track the correction to size we need to apply. This value
461 * will likely change during protocol negotiation but it is
462 * valid to start by assuming pre-Win8.
464 int vmscsi_size_delta;
467 * Max I/O, the device can support.
469 u32 max_transfer_bytes;
471 * Number of sub-channels we will open.
474 struct vmbus_channel **stor_chns;
476 * Mask of CPUs bound to subchannels.
478 struct cpumask alloced_cpus;
480 * Serializes modifications of stor_chns[] from storvsc_do_io()
481 * and storvsc_change_target_cpu().
484 /* Used for vsc/vsp channel reset process */
485 struct storvsc_cmd_request init_request;
486 struct storvsc_cmd_request reset_request;
488 * Currently active port and node names for FC devices.
492 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
493 struct fc_rport *rport;
497 struct hv_host_device {
498 struct hv_device *dev;
501 unsigned char target;
502 struct workqueue_struct *handle_error_wq;
503 struct work_struct host_scan_work;
504 struct Scsi_Host *host;
507 struct storvsc_scan_work {
508 struct work_struct work;
509 struct Scsi_Host *host;
514 static void storvsc_device_scan(struct work_struct *work)
516 struct storvsc_scan_work *wrk;
517 struct scsi_device *sdev;
519 wrk = container_of(work, struct storvsc_scan_work, work);
521 sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
524 scsi_rescan_device(&sdev->sdev_gendev);
525 scsi_device_put(sdev);
531 static void storvsc_host_scan(struct work_struct *work)
533 struct Scsi_Host *host;
534 struct scsi_device *sdev;
535 struct hv_host_device *host_device =
536 container_of(work, struct hv_host_device, host_scan_work);
538 host = host_device->host;
540 * Before scanning the host, first check to see if any of the
541 * currrently known devices have been hot removed. We issue a
542 * "unit ready" command against all currently known devices.
543 * This I/O will result in an error for devices that have been
544 * removed. As part of handling the I/O error, we remove the device.
546 * When a LUN is added or removed, the host sends us a signal to
547 * scan the host. Thus we are forced to discover the LUNs that
548 * may have been removed this way.
550 mutex_lock(&host->scan_mutex);
551 shost_for_each_device(sdev, host)
552 scsi_test_unit_ready(sdev, 1, 1, NULL);
553 mutex_unlock(&host->scan_mutex);
555 * Now scan the host to discover LUNs that may have been added.
557 scsi_scan_host(host);
560 static void storvsc_remove_lun(struct work_struct *work)
562 struct storvsc_scan_work *wrk;
563 struct scsi_device *sdev;
565 wrk = container_of(work, struct storvsc_scan_work, work);
566 if (!scsi_host_get(wrk->host))
569 sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
572 scsi_remove_device(sdev);
573 scsi_device_put(sdev);
575 scsi_host_put(wrk->host);
583 * We can get incoming messages from the host that are not in response to
584 * messages that we have sent out. An example of this would be messages
585 * received by the guest to notify dynamic addition/removal of LUNs. To
586 * deal with potential race conditions where the driver may be in the
587 * midst of being unloaded when we might receive an unsolicited message
588 * from the host, we have implemented a mechanism to gurantee sequential
591 * 1) Once the device is marked as being destroyed, we will fail all
593 * 2) We permit incoming messages when the device is being destroyed,
594 * only to properly account for messages already sent out.
597 static inline struct storvsc_device *get_out_stor_device(
598 struct hv_device *device)
600 struct storvsc_device *stor_device;
602 stor_device = hv_get_drvdata(device);
604 if (stor_device && stor_device->destroy)
611 static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
613 dev->drain_notify = true;
614 wait_event(dev->waiting_to_drain,
615 atomic_read(&dev->num_outstanding_req) == 0);
616 dev->drain_notify = false;
619 static inline struct storvsc_device *get_in_stor_device(
620 struct hv_device *device)
622 struct storvsc_device *stor_device;
624 stor_device = hv_get_drvdata(device);
630 * If the device is being destroyed; allow incoming
631 * traffic only to cleanup outstanding requests.
634 if (stor_device->destroy &&
635 (atomic_read(&stor_device->num_outstanding_req) == 0))
643 static void storvsc_change_target_cpu(struct vmbus_channel *channel, u32 old,
646 struct storvsc_device *stor_device;
647 struct vmbus_channel *cur_chn;
648 bool old_is_alloced = false;
649 struct hv_device *device;
653 device = channel->primary_channel ?
654 channel->primary_channel->device_obj
655 : channel->device_obj;
656 stor_device = get_out_stor_device(device);
660 /* See storvsc_do_io() -> get_og_chn(). */
661 spin_lock_irqsave(&stor_device->lock, flags);
664 * Determines if the storvsc device has other channels assigned to
665 * the "old" CPU to update the alloced_cpus mask and the stor_chns
668 if (device->channel != channel && device->channel->target_cpu == old) {
669 cur_chn = device->channel;
670 old_is_alloced = true;
673 list_for_each_entry(cur_chn, &device->channel->sc_list, sc_list) {
674 if (cur_chn == channel)
676 if (cur_chn->target_cpu == old) {
677 old_is_alloced = true;
684 WRITE_ONCE(stor_device->stor_chns[old], cur_chn);
686 cpumask_clear_cpu(old, &stor_device->alloced_cpus);
688 /* "Flush" the stor_chns array. */
689 for_each_possible_cpu(cpu) {
690 if (stor_device->stor_chns[cpu] && !cpumask_test_cpu(
691 cpu, &stor_device->alloced_cpus))
692 WRITE_ONCE(stor_device->stor_chns[cpu], NULL);
695 WRITE_ONCE(stor_device->stor_chns[new], channel);
696 cpumask_set_cpu(new, &stor_device->alloced_cpus);
698 spin_unlock_irqrestore(&stor_device->lock, flags);
701 static u64 storvsc_next_request_id(struct vmbus_channel *channel, u64 rqst_addr)
703 struct storvsc_cmd_request *request =
704 (struct storvsc_cmd_request *)(unsigned long)rqst_addr;
706 if (rqst_addr == VMBUS_RQST_INIT)
707 return VMBUS_RQST_INIT;
708 if (rqst_addr == VMBUS_RQST_RESET)
709 return VMBUS_RQST_RESET;
712 * Cannot return an ID of 0, which is reserved for an unsolicited
713 * message from Hyper-V.
715 return (u64)blk_mq_unique_tag(scsi_cmd_to_rq(request->cmd)) + 1;
718 static void handle_sc_creation(struct vmbus_channel *new_sc)
720 struct hv_device *device = new_sc->primary_channel->device_obj;
721 struct device *dev = &device->device;
722 struct storvsc_device *stor_device;
723 struct vmstorage_channel_properties props;
726 stor_device = get_out_stor_device(device);
730 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
731 new_sc->max_pkt_size = STORVSC_MAX_PKT_SIZE;
733 new_sc->next_request_id_callback = storvsc_next_request_id;
735 ret = vmbus_open(new_sc,
736 storvsc_ringbuffer_size,
737 storvsc_ringbuffer_size,
739 sizeof(struct vmstorage_channel_properties),
740 storvsc_on_channel_callback, new_sc);
742 /* In case vmbus_open() fails, we don't use the sub-channel. */
744 dev_err(dev, "Failed to open sub-channel: err=%d\n", ret);
748 new_sc->change_target_cpu_callback = storvsc_change_target_cpu;
750 /* Add the sub-channel to the array of available channels. */
751 stor_device->stor_chns[new_sc->target_cpu] = new_sc;
752 cpumask_set_cpu(new_sc->target_cpu, &stor_device->alloced_cpus);
755 static void handle_multichannel_storage(struct hv_device *device, int max_chns)
757 struct device *dev = &device->device;
758 struct storvsc_device *stor_device;
760 struct storvsc_cmd_request *request;
761 struct vstor_packet *vstor_packet;
765 * If the number of CPUs is artificially restricted, such as
766 * with maxcpus=1 on the kernel boot line, Hyper-V could offer
767 * sub-channels >= the number of CPUs. These sub-channels
768 * should not be created. The primary channel is already created
769 * and assigned to one CPU, so check against # CPUs - 1.
771 num_sc = min((int)(num_online_cpus() - 1), max_chns);
775 stor_device = get_out_stor_device(device);
779 stor_device->num_sc = num_sc;
780 request = &stor_device->init_request;
781 vstor_packet = &request->vstor_packet;
784 * Establish a handler for dealing with subchannels.
786 vmbus_set_sc_create_callback(device->channel, handle_sc_creation);
789 * Request the host to create sub-channels.
791 memset(request, 0, sizeof(struct storvsc_cmd_request));
792 init_completion(&request->wait_event);
793 vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
794 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
795 vstor_packet->sub_channel_count = num_sc;
797 ret = vmbus_sendpacket(device->channel, vstor_packet,
798 (sizeof(struct vstor_packet) -
799 stor_device->vmscsi_size_delta),
802 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
805 dev_err(dev, "Failed to create sub-channel: err=%d\n", ret);
809 t = wait_for_completion_timeout(&request->wait_event, 10*HZ);
811 dev_err(dev, "Failed to create sub-channel: timed out\n");
815 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
816 vstor_packet->status != 0) {
817 dev_err(dev, "Failed to create sub-channel: op=%d, sts=%d\n",
818 vstor_packet->operation, vstor_packet->status);
823 * We need to do nothing here, because vmbus_process_offer()
824 * invokes channel->sc_creation_callback, which will open and use
825 * the sub-channel(s).
829 static void cache_wwn(struct storvsc_device *stor_device,
830 struct vstor_packet *vstor_packet)
833 * Cache the currently active port and node ww names.
835 if (vstor_packet->wwn_packet.primary_active) {
836 stor_device->node_name =
837 wwn_to_u64(vstor_packet->wwn_packet.primary_node_wwn);
838 stor_device->port_name =
839 wwn_to_u64(vstor_packet->wwn_packet.primary_port_wwn);
841 stor_device->node_name =
842 wwn_to_u64(vstor_packet->wwn_packet.secondary_node_wwn);
843 stor_device->port_name =
844 wwn_to_u64(vstor_packet->wwn_packet.secondary_port_wwn);
849 static int storvsc_execute_vstor_op(struct hv_device *device,
850 struct storvsc_cmd_request *request,
853 struct storvsc_device *stor_device;
854 struct vstor_packet *vstor_packet;
857 stor_device = get_out_stor_device(device);
861 vstor_packet = &request->vstor_packet;
863 init_completion(&request->wait_event);
864 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
866 ret = vmbus_sendpacket(device->channel, vstor_packet,
867 (sizeof(struct vstor_packet) -
868 stor_device->vmscsi_size_delta),
871 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
875 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
882 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
883 vstor_packet->status != 0)
889 static int storvsc_channel_init(struct hv_device *device, bool is_fc)
891 struct storvsc_device *stor_device;
892 struct storvsc_cmd_request *request;
893 struct vstor_packet *vstor_packet;
896 bool process_sub_channels = false;
898 stor_device = get_out_stor_device(device);
902 request = &stor_device->init_request;
903 vstor_packet = &request->vstor_packet;
906 * Now, initiate the vsc/vsp initialization protocol on the open
909 memset(request, 0, sizeof(struct storvsc_cmd_request));
910 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
911 ret = storvsc_execute_vstor_op(device, request, true);
915 * Query host supported protocol version.
918 for (i = 0; i < ARRAY_SIZE(vmstor_protocols); i++) {
919 /* reuse the packet for version range supported */
920 memset(vstor_packet, 0, sizeof(struct vstor_packet));
921 vstor_packet->operation =
922 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
924 vstor_packet->version.major_minor =
925 vmstor_protocols[i].protocol_version;
928 * The revision number is only used in Windows; set it to 0.
930 vstor_packet->version.revision = 0;
931 ret = storvsc_execute_vstor_op(device, request, false);
935 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO)
938 if (vstor_packet->status == 0) {
939 vmstor_proto_version =
940 vmstor_protocols[i].protocol_version;
943 vmstor_protocols[i].sense_buffer_size;
945 stor_device->vmscsi_size_delta =
946 vmstor_protocols[i].vmscsi_size_delta;
952 if (vstor_packet->status != 0)
956 memset(vstor_packet, 0, sizeof(struct vstor_packet));
957 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
958 ret = storvsc_execute_vstor_op(device, request, true);
963 * Check to see if multi-channel support is there.
964 * Hosts that implement protocol version of 5.1 and above
965 * support multi-channel.
967 max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
970 * Allocate state to manage the sub-channels.
971 * We allocate an array based on the numbers of possible CPUs
972 * (Hyper-V does not support cpu online/offline).
973 * This Array will be sparseley populated with unique
974 * channels - primary + sub-channels.
975 * We will however populate all the slots to evenly distribute
978 stor_device->stor_chns = kcalloc(num_possible_cpus(), sizeof(void *),
980 if (stor_device->stor_chns == NULL)
983 device->channel->change_target_cpu_callback = storvsc_change_target_cpu;
985 stor_device->stor_chns[device->channel->target_cpu] = device->channel;
986 cpumask_set_cpu(device->channel->target_cpu,
987 &stor_device->alloced_cpus);
989 if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN8) {
990 if (vstor_packet->storage_channel_properties.flags &
991 STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
992 process_sub_channels = true;
994 stor_device->max_transfer_bytes =
995 vstor_packet->storage_channel_properties.max_transfer_bytes;
1001 * For FC devices retrieve FC HBA data.
1003 memset(vstor_packet, 0, sizeof(struct vstor_packet));
1004 vstor_packet->operation = VSTOR_OPERATION_FCHBA_DATA;
1005 ret = storvsc_execute_vstor_op(device, request, true);
1010 * Cache the currently active port and node ww names.
1012 cache_wwn(stor_device, vstor_packet);
1016 memset(vstor_packet, 0, sizeof(struct vstor_packet));
1017 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
1018 ret = storvsc_execute_vstor_op(device, request, true);
1022 if (process_sub_channels)
1023 handle_multichannel_storage(device, max_chns);
1028 static void storvsc_handle_error(struct vmscsi_request *vm_srb,
1029 struct scsi_cmnd *scmnd,
1030 struct Scsi_Host *host,
1033 struct storvsc_scan_work *wrk;
1034 void (*process_err_fn)(struct work_struct *work);
1035 struct hv_host_device *host_dev = shost_priv(host);
1038 * In some situations, Hyper-V sets multiple bits in the
1039 * srb_status, such as ABORTED and ERROR. So process them
1040 * individually, with the most specific bits first.
1043 if (vm_srb->srb_status & SRB_STATUS_INVALID_LUN) {
1044 set_host_byte(scmnd, DID_NO_CONNECT);
1045 process_err_fn = storvsc_remove_lun;
1049 if (vm_srb->srb_status & SRB_STATUS_ABORTED) {
1050 if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID &&
1051 /* Capacity data has changed */
1052 (asc == 0x2a) && (ascq == 0x9)) {
1053 process_err_fn = storvsc_device_scan;
1055 * Retry the I/O that triggered this.
1057 set_host_byte(scmnd, DID_REQUEUE);
1062 if (vm_srb->srb_status & SRB_STATUS_ERROR) {
1064 * Let upper layer deal with error when
1065 * sense message is present.
1067 if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID)
1071 * If there is an error; offline the device since all
1072 * error recovery strategies would have already been
1073 * deployed on the host side. However, if the command
1074 * were a pass-through command deal with it appropriately.
1076 switch (scmnd->cmnd[0]) {
1079 set_host_byte(scmnd, DID_PASSTHROUGH);
1082 * On some Hyper-V hosts TEST_UNIT_READY command can
1083 * return SRB_STATUS_ERROR. Let the upper level code
1084 * deal with it based on the sense information.
1086 case TEST_UNIT_READY:
1089 set_host_byte(scmnd, DID_ERROR);
1096 * We need to schedule work to process this error; schedule it.
1098 wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
1100 set_host_byte(scmnd, DID_TARGET_FAILURE);
1105 wrk->lun = vm_srb->lun;
1106 wrk->tgt_id = vm_srb->target_id;
1107 INIT_WORK(&wrk->work, process_err_fn);
1108 queue_work(host_dev->handle_error_wq, &wrk->work);
1112 static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request,
1113 struct storvsc_device *stor_dev)
1115 struct scsi_cmnd *scmnd = cmd_request->cmd;
1116 struct scsi_sense_hdr sense_hdr;
1117 struct vmscsi_request *vm_srb;
1118 u32 data_transfer_length;
1119 struct Scsi_Host *host;
1120 u32 payload_sz = cmd_request->payload_sz;
1121 void *payload = cmd_request->payload;
1124 host = stor_dev->host;
1126 vm_srb = &cmd_request->vstor_packet.vm_srb;
1127 data_transfer_length = vm_srb->data_transfer_length;
1129 scmnd->result = vm_srb->scsi_status;
1131 if (scmnd->result) {
1132 sense_ok = scsi_normalize_sense(scmnd->sense_buffer,
1133 SCSI_SENSE_BUFFERSIZE, &sense_hdr);
1135 if (sense_ok && do_logging(STORVSC_LOGGING_WARN))
1136 scsi_print_sense_hdr(scmnd->device, "storvsc",
1140 if (vm_srb->srb_status != SRB_STATUS_SUCCESS) {
1141 storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
1144 * The Windows driver set data_transfer_length on
1145 * SRB_STATUS_DATA_OVERRUN. On other errors, this value
1146 * is untouched. In these cases we set it to 0.
1148 if (vm_srb->srb_status != SRB_STATUS_DATA_OVERRUN)
1149 data_transfer_length = 0;
1152 /* Validate data_transfer_length (from Hyper-V) */
1153 if (data_transfer_length > cmd_request->payload->range.len)
1154 data_transfer_length = cmd_request->payload->range.len;
1156 scsi_set_resid(scmnd,
1157 cmd_request->payload->range.len - data_transfer_length);
1162 sizeof(struct vmbus_channel_packet_multipage_buffer))
1166 static void storvsc_on_io_completion(struct storvsc_device *stor_device,
1167 struct vstor_packet *vstor_packet,
1168 struct storvsc_cmd_request *request)
1170 struct vstor_packet *stor_pkt;
1171 struct hv_device *device = stor_device->device;
1173 stor_pkt = &request->vstor_packet;
1176 * The current SCSI handling on the host side does
1177 * not correctly handle:
1178 * INQUIRY command with page code parameter set to 0x80
1179 * MODE_SENSE command with cmd[2] == 0x1c
1181 * Setup srb and scsi status so this won't be fatal.
1182 * We do this so we can distinguish truly fatal failues
1183 * (srb status == 0x4) and off-line the device in that case.
1186 if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
1187 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
1188 vstor_packet->vm_srb.scsi_status = 0;
1189 vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
1192 /* Copy over the status...etc */
1193 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
1194 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
1197 * Copy over the sense_info_length, but limit to the known max
1198 * size if Hyper-V returns a bad value.
1200 stor_pkt->vm_srb.sense_info_length = min_t(u8, sense_buffer_size,
1201 vstor_packet->vm_srb.sense_info_length);
1203 if (vstor_packet->vm_srb.scsi_status != 0 ||
1204 vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS) {
1207 * Log TEST_UNIT_READY errors only as warnings. Hyper-V can
1208 * return errors when detecting devices using TEST_UNIT_READY,
1209 * and logging these as errors produces unhelpful noise.
1211 int loglevel = (stor_pkt->vm_srb.cdb[0] == TEST_UNIT_READY) ?
1212 STORVSC_LOGGING_WARN : STORVSC_LOGGING_ERROR;
1214 storvsc_log(device, loglevel,
1215 "tag#%d cmd 0x%x status: scsi 0x%x srb 0x%x hv 0x%x\n",
1216 scsi_cmd_to_rq(request->cmd)->tag,
1217 stor_pkt->vm_srb.cdb[0],
1218 vstor_packet->vm_srb.scsi_status,
1219 vstor_packet->vm_srb.srb_status,
1220 vstor_packet->status);
1223 if (vstor_packet->vm_srb.scsi_status == SAM_STAT_CHECK_CONDITION &&
1224 (vstor_packet->vm_srb.srb_status & SRB_STATUS_AUTOSENSE_VALID))
1225 memcpy(request->cmd->sense_buffer,
1226 vstor_packet->vm_srb.sense_data,
1227 stor_pkt->vm_srb.sense_info_length);
1229 stor_pkt->vm_srb.data_transfer_length =
1230 vstor_packet->vm_srb.data_transfer_length;
1232 storvsc_command_completion(request, stor_device);
1234 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
1235 stor_device->drain_notify)
1236 wake_up(&stor_device->waiting_to_drain);
1239 static void storvsc_on_receive(struct storvsc_device *stor_device,
1240 struct vstor_packet *vstor_packet,
1241 struct storvsc_cmd_request *request)
1243 struct hv_host_device *host_dev;
1244 switch (vstor_packet->operation) {
1245 case VSTOR_OPERATION_COMPLETE_IO:
1246 storvsc_on_io_completion(stor_device, vstor_packet, request);
1249 case VSTOR_OPERATION_REMOVE_DEVICE:
1250 case VSTOR_OPERATION_ENUMERATE_BUS:
1251 host_dev = shost_priv(stor_device->host);
1253 host_dev->handle_error_wq, &host_dev->host_scan_work);
1256 case VSTOR_OPERATION_FCHBA_DATA:
1257 cache_wwn(stor_device, vstor_packet);
1258 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1259 fc_host_node_name(stor_device->host) = stor_device->node_name;
1260 fc_host_port_name(stor_device->host) = stor_device->port_name;
1268 static void storvsc_on_channel_callback(void *context)
1270 struct vmbus_channel *channel = (struct vmbus_channel *)context;
1271 const struct vmpacket_descriptor *desc;
1272 struct hv_device *device;
1273 struct storvsc_device *stor_device;
1274 struct Scsi_Host *shost;
1276 if (channel->primary_channel != NULL)
1277 device = channel->primary_channel->device_obj;
1279 device = channel->device_obj;
1281 stor_device = get_in_stor_device(device);
1285 shost = stor_device->host;
1287 foreach_vmbus_pkt(desc, channel) {
1288 struct vstor_packet *packet = hv_pkt_data(desc);
1289 struct storvsc_cmd_request *request = NULL;
1290 u32 pktlen = hv_pkt_datalen(desc);
1291 u64 rqst_id = desc->trans_id;
1292 u32 minlen = rqst_id ? sizeof(struct vstor_packet) -
1293 stor_device->vmscsi_size_delta : sizeof(enum vstor_packet_operation);
1295 if (pktlen < minlen) {
1296 dev_err(&device->device,
1297 "Invalid pkt: id=%llu, len=%u, minlen=%u\n",
1298 rqst_id, pktlen, minlen);
1302 if (rqst_id == VMBUS_RQST_INIT) {
1303 request = &stor_device->init_request;
1304 } else if (rqst_id == VMBUS_RQST_RESET) {
1305 request = &stor_device->reset_request;
1307 /* Hyper-V can send an unsolicited message with ID of 0 */
1310 * storvsc_on_receive() looks at the vstor_packet in the message
1311 * from the ring buffer.
1313 * - If the operation in the vstor_packet is COMPLETE_IO, then
1314 * we call storvsc_on_io_completion(), and dereference the
1315 * guest memory address. Make sure we don't call
1316 * storvsc_on_io_completion() with a guest memory address
1317 * that is zero if Hyper-V were to construct and send such
1320 * - If the operation in the vstor_packet is FCHBA_DATA, then
1321 * we call cache_wwn(), and access the data payload area of
1322 * the packet (wwn_packet); however, there is no guarantee
1323 * that the packet is big enough to contain such area.
1324 * Future-proof the code by rejecting such a bogus packet.
1326 if (packet->operation == VSTOR_OPERATION_COMPLETE_IO ||
1327 packet->operation == VSTOR_OPERATION_FCHBA_DATA) {
1328 dev_err(&device->device, "Invalid packet with ID of 0\n");
1332 struct scsi_cmnd *scmnd;
1334 /* Transaction 'rqst_id' corresponds to tag 'rqst_id - 1' */
1335 scmnd = scsi_host_find_tag(shost, rqst_id - 1);
1336 if (scmnd == NULL) {
1337 dev_err(&device->device, "Incorrect transaction ID\n");
1340 request = (struct storvsc_cmd_request *)scsi_cmd_priv(scmnd);
1341 scsi_dma_unmap(scmnd);
1344 storvsc_on_receive(stor_device, packet, request);
1348 memcpy(&request->vstor_packet, packet,
1349 (sizeof(struct vstor_packet) - stor_device->vmscsi_size_delta));
1350 complete(&request->wait_event);
1354 static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
1357 struct vmstorage_channel_properties props;
1360 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
1362 device->channel->max_pkt_size = STORVSC_MAX_PKT_SIZE;
1363 device->channel->next_request_id_callback = storvsc_next_request_id;
1365 ret = vmbus_open(device->channel,
1369 sizeof(struct vmstorage_channel_properties),
1370 storvsc_on_channel_callback, device->channel);
1375 ret = storvsc_channel_init(device, is_fc);
1380 static int storvsc_dev_remove(struct hv_device *device)
1382 struct storvsc_device *stor_device;
1384 stor_device = hv_get_drvdata(device);
1386 stor_device->destroy = true;
1388 /* Make sure flag is set before waiting */
1392 * At this point, all outbound traffic should be disable. We
1393 * only allow inbound traffic (responses) to proceed so that
1394 * outstanding requests can be completed.
1397 storvsc_wait_to_drain(stor_device);
1400 * Since we have already drained, we don't need to busy wait
1401 * as was done in final_release_stor_device()
1402 * Note that we cannot set the ext pointer to NULL until
1403 * we have drained - to drain the outgoing packets, we need to
1404 * allow incoming packets.
1406 hv_set_drvdata(device, NULL);
1408 /* Close the channel */
1409 vmbus_close(device->channel);
1411 kfree(stor_device->stor_chns);
1416 static struct vmbus_channel *get_og_chn(struct storvsc_device *stor_device,
1421 const struct cpumask *node_mask;
1422 int num_channels, tgt_cpu;
1424 if (stor_device->num_sc == 0) {
1425 stor_device->stor_chns[q_num] = stor_device->device->channel;
1426 return stor_device->device->channel;
1430 * Our channel array is sparsley populated and we
1431 * initiated I/O on a processor/hw-q that does not
1432 * currently have a designated channel. Fix this.
1433 * The strategy is simple:
1434 * I. Ensure NUMA locality
1435 * II. Distribute evenly (best effort)
1438 node_mask = cpumask_of_node(cpu_to_node(q_num));
1441 for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
1442 if (cpumask_test_cpu(tgt_cpu, node_mask))
1445 if (num_channels == 0) {
1446 stor_device->stor_chns[q_num] = stor_device->device->channel;
1447 return stor_device->device->channel;
1451 while (hash_qnum >= num_channels)
1452 hash_qnum -= num_channels;
1454 for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
1455 if (!cpumask_test_cpu(tgt_cpu, node_mask))
1457 if (slot == hash_qnum)
1462 stor_device->stor_chns[q_num] = stor_device->stor_chns[tgt_cpu];
1464 return stor_device->stor_chns[q_num];
1468 static int storvsc_do_io(struct hv_device *device,
1469 struct storvsc_cmd_request *request, u16 q_num)
1471 struct storvsc_device *stor_device;
1472 struct vstor_packet *vstor_packet;
1473 struct vmbus_channel *outgoing_channel, *channel;
1474 unsigned long flags;
1476 const struct cpumask *node_mask;
1479 vstor_packet = &request->vstor_packet;
1480 stor_device = get_out_stor_device(device);
1486 request->device = device;
1488 * Select an appropriate channel to send the request out.
1490 /* See storvsc_change_target_cpu(). */
1491 outgoing_channel = READ_ONCE(stor_device->stor_chns[q_num]);
1492 if (outgoing_channel != NULL) {
1493 if (outgoing_channel->target_cpu == q_num) {
1495 * Ideally, we want to pick a different channel if
1496 * available on the same NUMA node.
1498 node_mask = cpumask_of_node(cpu_to_node(q_num));
1499 for_each_cpu_wrap(tgt_cpu,
1500 &stor_device->alloced_cpus, q_num + 1) {
1501 if (!cpumask_test_cpu(tgt_cpu, node_mask))
1503 if (tgt_cpu == q_num)
1505 channel = READ_ONCE(
1506 stor_device->stor_chns[tgt_cpu]);
1507 if (channel == NULL)
1509 if (hv_get_avail_to_write_percent(
1511 > ring_avail_percent_lowater) {
1512 outgoing_channel = channel;
1518 * All the other channels on the same NUMA node are
1519 * busy. Try to use the channel on the current CPU
1521 if (hv_get_avail_to_write_percent(
1522 &outgoing_channel->outbound)
1523 > ring_avail_percent_lowater)
1527 * If we reach here, all the channels on the current
1528 * NUMA node are busy. Try to find a channel in
1531 for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
1532 if (cpumask_test_cpu(tgt_cpu, node_mask))
1534 channel = READ_ONCE(
1535 stor_device->stor_chns[tgt_cpu]);
1536 if (channel == NULL)
1538 if (hv_get_avail_to_write_percent(
1540 > ring_avail_percent_lowater) {
1541 outgoing_channel = channel;
1547 spin_lock_irqsave(&stor_device->lock, flags);
1548 outgoing_channel = stor_device->stor_chns[q_num];
1549 if (outgoing_channel != NULL) {
1550 spin_unlock_irqrestore(&stor_device->lock, flags);
1553 outgoing_channel = get_og_chn(stor_device, q_num);
1554 spin_unlock_irqrestore(&stor_device->lock, flags);
1558 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
1560 vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) -
1561 stor_device->vmscsi_size_delta);
1564 vstor_packet->vm_srb.sense_info_length = sense_buffer_size;
1567 vstor_packet->vm_srb.data_transfer_length =
1568 request->payload->range.len;
1570 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
1572 if (request->payload->range.len) {
1574 ret = vmbus_sendpacket_mpb_desc(outgoing_channel,
1575 request->payload, request->payload_sz,
1577 (sizeof(struct vstor_packet) -
1578 stor_device->vmscsi_size_delta),
1579 (unsigned long)request);
1581 ret = vmbus_sendpacket(outgoing_channel, vstor_packet,
1582 (sizeof(struct vstor_packet) -
1583 stor_device->vmscsi_size_delta),
1584 (unsigned long)request,
1586 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1592 atomic_inc(&stor_device->num_outstanding_req);
1597 static int storvsc_device_alloc(struct scsi_device *sdevice)
1600 * Set blist flag to permit the reading of the VPD pages even when
1601 * the target may claim SPC-2 compliance. MSFT targets currently
1602 * claim SPC-2 compliance while they implement post SPC-2 features.
1603 * With this flag we can correctly handle WRITE_SAME_16 issues.
1605 * Hypervisor reports SCSI_UNKNOWN type for DVD ROM device but
1606 * still supports REPORT LUN.
1608 sdevice->sdev_bflags = BLIST_REPORTLUN2 | BLIST_TRY_VPD_PAGES;
1613 static int storvsc_device_configure(struct scsi_device *sdevice)
1615 blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
1617 sdevice->no_write_same = 1;
1620 * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3
1621 * if the device is a MSFT virtual device. If the host is
1622 * WIN10 or newer, allow write_same.
1624 if (!strncmp(sdevice->vendor, "Msft", 4)) {
1625 switch (vmstor_proto_version) {
1626 case VMSTOR_PROTO_VERSION_WIN8:
1627 case VMSTOR_PROTO_VERSION_WIN8_1:
1628 sdevice->scsi_level = SCSI_SPC_3;
1632 if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN10)
1633 sdevice->no_write_same = 0;
1639 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1640 sector_t capacity, int *info)
1642 sector_t nsect = capacity;
1643 sector_t cylinders = nsect;
1644 int heads, sectors_pt;
1647 * We are making up these values; let us keep it simple.
1650 sectors_pt = 0x3f; /* Sectors per track */
1651 sector_div(cylinders, heads * sectors_pt);
1652 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1656 info[1] = sectors_pt;
1657 info[2] = (int)cylinders;
1662 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
1664 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1665 struct hv_device *device = host_dev->dev;
1667 struct storvsc_device *stor_device;
1668 struct storvsc_cmd_request *request;
1669 struct vstor_packet *vstor_packet;
1672 stor_device = get_out_stor_device(device);
1676 request = &stor_device->reset_request;
1677 vstor_packet = &request->vstor_packet;
1678 memset(vstor_packet, 0, sizeof(struct vstor_packet));
1680 init_completion(&request->wait_event);
1682 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1683 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1684 vstor_packet->vm_srb.path_id = stor_device->path_id;
1686 ret = vmbus_sendpacket(device->channel, vstor_packet,
1687 (sizeof(struct vstor_packet) -
1688 stor_device->vmscsi_size_delta),
1691 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1695 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
1697 return TIMEOUT_ERROR;
1701 * At this point, all outstanding requests in the adapter
1702 * should have been flushed out and return to us
1703 * There is a potential race here where the host may be in
1704 * the process of responding when we return from here.
1705 * Just wait for all in-transit packets to be accounted for
1706 * before we return from here.
1708 storvsc_wait_to_drain(stor_device);
1714 * The host guarantees to respond to each command, although I/O latencies might
1715 * be unbounded on Azure. Reset the timer unconditionally to give the host a
1716 * chance to perform EH.
1718 static enum blk_eh_timer_return storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
1720 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1721 if (scmnd->device->host->transportt == fc_transport_template)
1722 return fc_eh_timed_out(scmnd);
1724 return BLK_EH_RESET_TIMER;
1727 static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
1729 bool allowed = true;
1730 u8 scsi_op = scmnd->cmnd[0];
1733 /* the host does not handle WRITE_SAME, log accident usage */
1736 * smartd sends this command and the host does not handle
1737 * this. So, don't send it.
1740 set_host_byte(scmnd, DID_ERROR);
1749 static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
1752 struct hv_host_device *host_dev = shost_priv(host);
1753 struct hv_device *dev = host_dev->dev;
1754 struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd);
1755 struct scatterlist *sgl;
1756 struct vmscsi_request *vm_srb;
1757 struct vmbus_packet_mpb_array *payload;
1761 if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) {
1763 * On legacy hosts filter unimplemented commands.
1764 * Future hosts are expected to correctly handle
1765 * unsupported commands. Furthermore, it is
1766 * possible that some of the currently
1767 * unsupported commands maybe supported in
1768 * future versions of the host.
1770 if (!storvsc_scsi_cmd_ok(scmnd)) {
1776 /* Setup the cmd request */
1777 cmd_request->cmd = scmnd;
1779 memset(&cmd_request->vstor_packet, 0, sizeof(struct vstor_packet));
1780 vm_srb = &cmd_request->vstor_packet.vm_srb;
1781 vm_srb->win8_extension.time_out_value = 60;
1783 vm_srb->win8_extension.srb_flags |=
1784 SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
1786 if (scmnd->device->tagged_supported) {
1787 vm_srb->win8_extension.srb_flags |=
1788 (SRB_FLAGS_QUEUE_ACTION_ENABLE | SRB_FLAGS_NO_QUEUE_FREEZE);
1789 vm_srb->win8_extension.queue_tag = SP_UNTAGGED;
1790 vm_srb->win8_extension.queue_action = SRB_SIMPLE_TAG_REQUEST;
1794 switch (scmnd->sc_data_direction) {
1796 vm_srb->data_in = WRITE_TYPE;
1797 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT;
1799 case DMA_FROM_DEVICE:
1800 vm_srb->data_in = READ_TYPE;
1801 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN;
1804 vm_srb->data_in = UNKNOWN_TYPE;
1805 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER;
1809 * This is DMA_BIDIRECTIONAL or something else we are never
1810 * supposed to see here.
1812 WARN(1, "Unexpected data direction: %d\n",
1813 scmnd->sc_data_direction);
1818 vm_srb->port_number = host_dev->port;
1819 vm_srb->path_id = scmnd->device->channel;
1820 vm_srb->target_id = scmnd->device->id;
1821 vm_srb->lun = scmnd->device->lun;
1823 vm_srb->cdb_length = scmnd->cmd_len;
1825 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1827 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1829 length = scsi_bufflen(scmnd);
1830 payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
1831 payload_sz = sizeof(cmd_request->mpb);
1833 if (scsi_sg_count(scmnd)) {
1834 unsigned long offset_in_hvpg = offset_in_hvpage(sgl->offset);
1835 unsigned int hvpg_count = HVPFN_UP(offset_in_hvpg + length);
1836 struct scatterlist *sg;
1837 unsigned long hvpfn, hvpfns_to_add;
1838 int j, i = 0, sg_count;
1840 if (hvpg_count > MAX_PAGE_BUFFER_COUNT) {
1842 payload_sz = (hvpg_count * sizeof(u64) +
1843 sizeof(struct vmbus_packet_mpb_array));
1844 payload = kzalloc(payload_sz, GFP_ATOMIC);
1846 return SCSI_MLQUEUE_DEVICE_BUSY;
1849 payload->range.len = length;
1850 payload->range.offset = offset_in_hvpg;
1852 sg_count = scsi_dma_map(scmnd);
1854 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1855 goto err_free_payload;
1858 for_each_sg(sgl, sg, sg_count, j) {
1860 * Init values for the current sgl entry. hvpfns_to_add
1861 * is in units of Hyper-V size pages. Handling the
1862 * PAGE_SIZE != HV_HYP_PAGE_SIZE case also handles
1863 * values of sgl->offset that are larger than PAGE_SIZE.
1864 * Such offsets are handled even on other than the first
1865 * sgl entry, provided they are a multiple of PAGE_SIZE.
1867 hvpfn = HVPFN_DOWN(sg_dma_address(sg));
1868 hvpfns_to_add = HVPFN_UP(sg_dma_address(sg) +
1869 sg_dma_len(sg)) - hvpfn;
1872 * Fill the next portion of the PFN array with
1873 * sequential Hyper-V PFNs for the continguous physical
1874 * memory described by the sgl entry. The end of the
1875 * last sgl should be reached at the same time that
1876 * the PFN array is filled.
1878 while (hvpfns_to_add--)
1879 payload->range.pfn_array[i++] = hvpfn++;
1883 cmd_request->payload = payload;
1884 cmd_request->payload_sz = payload_sz;
1886 /* Invokes the vsc to start an IO */
1887 ret = storvsc_do_io(dev, cmd_request, get_cpu());
1890 if (ret == -EAGAIN) {
1892 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1893 goto err_free_payload;
1899 if (payload_sz > sizeof(cmd_request->mpb))
1905 static struct scsi_host_template scsi_driver = {
1906 .module = THIS_MODULE,
1907 .name = "storvsc_host_t",
1908 .cmd_size = sizeof(struct storvsc_cmd_request),
1909 .bios_param = storvsc_get_chs,
1910 .queuecommand = storvsc_queuecommand,
1911 .eh_host_reset_handler = storvsc_host_reset_handler,
1912 .proc_name = "storvsc_host",
1913 .eh_timed_out = storvsc_eh_timed_out,
1914 .slave_alloc = storvsc_device_alloc,
1915 .slave_configure = storvsc_device_configure,
1916 .cmd_per_lun = 2048,
1918 /* Ensure there are no gaps in presented sgls */
1919 .virt_boundary_mask = PAGE_SIZE-1,
1921 .track_queue_depth = 1,
1922 .change_queue_depth = storvsc_change_queue_depth,
1931 static const struct hv_vmbus_device_id id_table[] = {
1934 .driver_data = SCSI_GUID
1938 .driver_data = IDE_GUID
1940 /* Fibre Channel GUID */
1943 .driver_data = SFC_GUID
1948 MODULE_DEVICE_TABLE(vmbus, id_table);
1950 static const struct { guid_t guid; } fc_guid = { HV_SYNTHFC_GUID };
1952 static bool hv_dev_is_fc(struct hv_device *hv_dev)
1954 return guid_equal(&fc_guid.guid, &hv_dev->dev_type);
1957 static int storvsc_probe(struct hv_device *device,
1958 const struct hv_vmbus_device_id *dev_id)
1961 int num_cpus = num_online_cpus();
1962 int num_present_cpus = num_present_cpus();
1963 struct Scsi_Host *host;
1964 struct hv_host_device *host_dev;
1965 bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
1966 bool is_fc = ((dev_id->driver_data == SFC_GUID) ? true : false);
1968 struct storvsc_device *stor_device;
1969 int max_luns_per_target;
1972 int max_sub_channels = 0;
1975 * Based on the windows host we are running on,
1976 * set state to properly communicate with the host.
1979 if (vmbus_proto_version < VERSION_WIN8) {
1980 max_luns_per_target = STORVSC_IDE_MAX_LUNS_PER_TARGET;
1981 max_targets = STORVSC_IDE_MAX_TARGETS;
1982 max_channels = STORVSC_IDE_MAX_CHANNELS;
1984 max_luns_per_target = STORVSC_MAX_LUNS_PER_TARGET;
1985 max_targets = STORVSC_MAX_TARGETS;
1986 max_channels = STORVSC_MAX_CHANNELS;
1988 * On Windows8 and above, we support sub-channels for storage
1989 * on SCSI and FC controllers.
1990 * The number of sub-channels offerred is based on the number of
1991 * VCPUs in the guest.
1995 (num_cpus - 1) / storvsc_vcpus_per_sub_channel;
1998 scsi_driver.can_queue = max_outstanding_req_per_channel *
1999 (max_sub_channels + 1) *
2000 (100 - ring_avail_percent_lowater) / 100;
2002 host = scsi_host_alloc(&scsi_driver,
2003 sizeof(struct hv_host_device));
2007 host_dev = shost_priv(host);
2008 memset(host_dev, 0, sizeof(struct hv_host_device));
2010 host_dev->port = host->host_no;
2011 host_dev->dev = device;
2012 host_dev->host = host;
2015 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
2021 stor_device->destroy = false;
2022 init_waitqueue_head(&stor_device->waiting_to_drain);
2023 stor_device->device = device;
2024 stor_device->host = host;
2025 stor_device->vmscsi_size_delta = sizeof(struct vmscsi_win8_extension);
2026 spin_lock_init(&stor_device->lock);
2027 hv_set_drvdata(device, stor_device);
2028 dma_set_min_align_mask(&device->device, HV_HYP_PAGE_SIZE - 1);
2030 stor_device->port_number = host->host_no;
2031 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size, is_fc);
2035 host_dev->path = stor_device->path_id;
2036 host_dev->target = stor_device->target_id;
2038 switch (dev_id->driver_data) {
2040 host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
2041 host->max_id = STORVSC_FC_MAX_TARGETS;
2042 host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
2043 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2044 host->transportt = fc_transport_template;
2049 host->max_lun = max_luns_per_target;
2050 host->max_id = max_targets;
2051 host->max_channel = max_channels - 1;
2055 host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
2056 host->max_id = STORVSC_IDE_MAX_TARGETS;
2057 host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
2060 /* max cmd length */
2061 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
2064 * set the table size based on the info we got
2067 host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
2069 * For non-IDE disks, the host supports multiple channels.
2070 * Set the number of HW queues we are supporting.
2073 if (storvsc_max_hw_queues > num_present_cpus) {
2074 storvsc_max_hw_queues = 0;
2075 storvsc_log(device, STORVSC_LOGGING_WARN,
2076 "Resetting invalid storvsc_max_hw_queues value to default.\n");
2078 if (storvsc_max_hw_queues)
2079 host->nr_hw_queues = storvsc_max_hw_queues;
2081 host->nr_hw_queues = num_present_cpus;
2085 * Set the error handler work queue.
2087 host_dev->handle_error_wq =
2088 alloc_ordered_workqueue("storvsc_error_wq_%d",
2091 if (!host_dev->handle_error_wq) {
2095 INIT_WORK(&host_dev->host_scan_work, storvsc_host_scan);
2096 /* Register the HBA and start the scsi bus scan */
2097 ret = scsi_add_host(host, &device->device);
2102 scsi_scan_host(host);
2104 target = (device->dev_instance.b[5] << 8 |
2105 device->dev_instance.b[4]);
2106 ret = scsi_add_device(host, 0, target, 0);
2110 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2111 if (host->transportt == fc_transport_template) {
2112 struct fc_rport_identifiers ids = {
2113 .roles = FC_PORT_ROLE_FCP_DUMMY_INITIATOR,
2116 fc_host_node_name(host) = stor_device->node_name;
2117 fc_host_port_name(host) = stor_device->port_name;
2118 stor_device->rport = fc_remote_port_add(host, 0, &ids);
2119 if (!stor_device->rport) {
2128 scsi_remove_host(host);
2131 destroy_workqueue(host_dev->handle_error_wq);
2135 * Once we have connected with the host, we would need to
2136 * to invoke storvsc_dev_remove() to rollback this state and
2137 * this call also frees up the stor_device; hence the jump around
2140 storvsc_dev_remove(device);
2144 kfree(stor_device->stor_chns);
2148 scsi_host_put(host);
2152 /* Change a scsi target's queue depth */
2153 static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth)
2155 if (queue_depth > scsi_driver.can_queue)
2156 queue_depth = scsi_driver.can_queue;
2158 return scsi_change_queue_depth(sdev, queue_depth);
2161 static int storvsc_remove(struct hv_device *dev)
2163 struct storvsc_device *stor_device = hv_get_drvdata(dev);
2164 struct Scsi_Host *host = stor_device->host;
2165 struct hv_host_device *host_dev = shost_priv(host);
2167 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2168 if (host->transportt == fc_transport_template) {
2169 fc_remote_port_delete(stor_device->rport);
2170 fc_remove_host(host);
2173 destroy_workqueue(host_dev->handle_error_wq);
2174 scsi_remove_host(host);
2175 storvsc_dev_remove(dev);
2176 scsi_host_put(host);
2181 static int storvsc_suspend(struct hv_device *hv_dev)
2183 struct storvsc_device *stor_device = hv_get_drvdata(hv_dev);
2184 struct Scsi_Host *host = stor_device->host;
2185 struct hv_host_device *host_dev = shost_priv(host);
2187 storvsc_wait_to_drain(stor_device);
2189 drain_workqueue(host_dev->handle_error_wq);
2191 vmbus_close(hv_dev->channel);
2193 kfree(stor_device->stor_chns);
2194 stor_device->stor_chns = NULL;
2196 cpumask_clear(&stor_device->alloced_cpus);
2201 static int storvsc_resume(struct hv_device *hv_dev)
2205 ret = storvsc_connect_to_vsp(hv_dev, storvsc_ringbuffer_size,
2206 hv_dev_is_fc(hv_dev));
2210 static struct hv_driver storvsc_drv = {
2211 .name = KBUILD_MODNAME,
2212 .id_table = id_table,
2213 .probe = storvsc_probe,
2214 .remove = storvsc_remove,
2215 .suspend = storvsc_suspend,
2216 .resume = storvsc_resume,
2218 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
2222 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2223 static struct fc_function_template fc_transport_functions = {
2224 .show_host_node_name = 1,
2225 .show_host_port_name = 1,
2229 static int __init storvsc_drv_init(void)
2234 * Divide the ring buffer data size (which is 1 page less
2235 * than the ring buffer size since that page is reserved for
2236 * the ring buffer indices) by the max request size (which is
2237 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
2239 * The computation underestimates max_outstanding_req_per_channel
2240 * for Win7 and older hosts because it does not take into account
2241 * the vmscsi_size_delta correction to the max request size.
2243 max_outstanding_req_per_channel =
2244 ((storvsc_ringbuffer_size - PAGE_SIZE) /
2245 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
2246 sizeof(struct vstor_packet) + sizeof(u64),
2249 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2250 fc_transport_template = fc_attach_transport(&fc_transport_functions);
2251 if (!fc_transport_template)
2255 ret = vmbus_driver_register(&storvsc_drv);
2257 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2259 fc_release_transport(fc_transport_template);
2265 static void __exit storvsc_drv_exit(void)
2267 vmbus_driver_unregister(&storvsc_drv);
2268 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2269 fc_release_transport(fc_transport_template);
2273 MODULE_LICENSE("GPL");
2274 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
2275 module_init(storvsc_drv_init);
2276 module_exit(storvsc_drv_exit);