2 * Copyright (c) 2009, Microsoft Corporation.
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.
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
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.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
20 * K. Y. Srinivasan <kys@microsoft.com>
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>
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>
46 * All wire protocol details (storage protocol between the guest and the host)
47 * are consolidated here.
49 * Begin protocol definitions.
55 * V1 RC < 2008/1/31: 1.0
56 * V1 RC > 2008/1/31: 2.0
63 #define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
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)
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
91 * WWN packet for Fibre Channel HBA
94 struct hv_fc_wwn_packet {
98 u8 primary_port_wwn[8];
99 u8 primary_node_wwn[8];
100 u8 secondary_port_wwn[8];
101 u8 secondary_node_wwn[8];
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
124 * This flag indicates the request is part of the workflow for processing a D3.
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
140 * Platform neutral description of a scsi request -
141 * this remains the same across the write regardless of 32/64 bit
142 * note: it's patterned off the SCSI_PASS_THROUGH structure
144 #define STORVSC_MAX_CMD_LEN 0x10
146 #define POST_WIN7_STORVSC_SENSE_BUFFER_SIZE 0x14
147 #define PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE 0x12
149 #define STORVSC_SENSE_BUFFER_SIZE 0x14
150 #define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
153 * Sense buffer size changed in win8; have a run-time
154 * variable to track the size we should use. This value will
155 * likely change during protocol negotiation but it is valid
156 * to start by assuming pre-Win8.
158 static int sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
161 * The storage protocol version is determined during the
162 * initial exchange with the host. It will indicate which
163 * storage functionality is available in the host.
165 static int vmstor_proto_version;
167 struct vmscsi_win8_extension {
169 * The following were added in Windows 8
179 struct vmscsi_request {
190 u8 sense_info_length;
194 u32 data_transfer_length;
197 u8 cdb[STORVSC_MAX_CMD_LEN];
198 u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
199 u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
202 * The following was added in win8.
204 struct vmscsi_win8_extension win8_extension;
206 } __attribute((packed));
210 * The size of the vmscsi_request has changed in win8. The
211 * additional size is because of new elements added to the
212 * structure. These elements are valid only when we are talking
214 * Track the correction to size we need to apply. This value
215 * will likely change during protocol negotiation but it is
216 * valid to start by assuming pre-Win8.
218 static int vmscsi_size_delta = sizeof(struct vmscsi_win8_extension);
221 * The list of storage protocols in order of preference.
223 struct vmstor_protocol {
224 int protocol_version;
225 int sense_buffer_size;
226 int vmscsi_size_delta;
230 static const struct vmstor_protocol vmstor_protocols[] = {
232 VMSTOR_PROTO_VERSION_WIN10,
233 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
237 VMSTOR_PROTO_VERSION_WIN8_1,
238 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
242 VMSTOR_PROTO_VERSION_WIN8,
243 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
247 VMSTOR_PROTO_VERSION_WIN7,
248 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
249 sizeof(struct vmscsi_win8_extension),
252 VMSTOR_PROTO_VERSION_WIN6,
253 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
254 sizeof(struct vmscsi_win8_extension),
260 * This structure is sent during the intialization phase to get the different
261 * properties of the channel.
264 #define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1
266 struct vmstorage_channel_properties {
272 u32 max_transfer_bytes;
277 /* This structure is sent during the storage protocol negotiations. */
278 struct vmstorage_protocol_version {
279 /* Major (MSW) and minor (LSW) version numbers. */
283 * Revision number is auto-incremented whenever this file is changed
284 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
285 * definitely indicate incompatibility--but it does indicate mismatched
287 * This is only used on the windows side. Just set it to 0.
292 /* Channel Property Flags */
293 #define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
294 #define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
296 struct vstor_packet {
297 /* Requested operation type */
298 enum vstor_packet_operation operation;
300 /* Flags - see below for values */
303 /* Status of the request returned from the server side. */
306 /* Data payload area */
309 * Structure used to forward SCSI commands from the
310 * client to the server.
312 struct vmscsi_request vm_srb;
314 /* Structure used to query channel properties. */
315 struct vmstorage_channel_properties storage_channel_properties;
317 /* Used during version negotiations. */
318 struct vmstorage_protocol_version version;
320 /* Fibre channel address packet */
321 struct hv_fc_wwn_packet wwn_packet;
323 /* Number of sub-channels to create */
324 u16 sub_channel_count;
326 /* This will be the maximum of the union members */
334 * This flag indicates that the server should send back a completion for this
338 #define REQUEST_COMPLETION_FLAG 0x1
340 /* Matches Windows-end */
341 enum storvsc_request_type {
348 * SRB status codes and masks; a subset of the codes used here.
351 #define SRB_STATUS_AUTOSENSE_VALID 0x80
352 #define SRB_STATUS_INVALID_LUN 0x20
353 #define SRB_STATUS_SUCCESS 0x01
354 #define SRB_STATUS_ABORTED 0x02
355 #define SRB_STATUS_ERROR 0x04
358 * This is the end of Protocol specific defines.
361 static int storvsc_ringbuffer_size = (256 * PAGE_SIZE);
362 static u32 max_outstanding_req_per_channel;
364 static int storvsc_vcpus_per_sub_channel = 4;
366 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
367 MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
369 module_param(storvsc_vcpus_per_sub_channel, int, S_IRUGO);
370 MODULE_PARM_DESC(vcpus_per_sub_channel, "Ratio of VCPUs to subchannels");
372 * Timeout in seconds for all devices managed by this driver.
374 static int storvsc_timeout = 180;
376 static int msft_blist_flags = BLIST_TRY_VPD_PAGES;
379 static void storvsc_on_channel_callback(void *context);
381 #define STORVSC_MAX_LUNS_PER_TARGET 255
382 #define STORVSC_MAX_TARGETS 2
383 #define STORVSC_MAX_CHANNELS 8
385 #define STORVSC_FC_MAX_LUNS_PER_TARGET 255
386 #define STORVSC_FC_MAX_TARGETS 128
387 #define STORVSC_FC_MAX_CHANNELS 8
389 #define STORVSC_IDE_MAX_LUNS_PER_TARGET 64
390 #define STORVSC_IDE_MAX_TARGETS 1
391 #define STORVSC_IDE_MAX_CHANNELS 1
393 struct storvsc_cmd_request {
394 struct scsi_cmnd *cmd;
396 unsigned int bounce_sgl_count;
397 struct scatterlist *bounce_sgl;
399 struct hv_device *device;
401 /* Synchronize the request/response if needed */
402 struct completion wait_event;
404 struct vmbus_channel_packet_multipage_buffer mpb;
405 struct vmbus_packet_mpb_array *payload;
408 struct vstor_packet vstor_packet;
412 /* A storvsc device is a device object that contains a vmbus channel */
413 struct storvsc_device {
414 struct hv_device *device;
418 bool open_sub_channel;
419 atomic_t num_outstanding_req;
420 struct Scsi_Host *host;
422 wait_queue_head_t waiting_to_drain;
425 * Each unique Port/Path/Target represents 1 channel ie scsi
426 * controller. In reality, the pathid, targetid is always 0
427 * and the port is set by us
429 unsigned int port_number;
430 unsigned char path_id;
431 unsigned char target_id;
434 * Max I/O, the device can support.
436 u32 max_transfer_bytes;
437 /* Used for vsc/vsp channel reset process */
438 struct storvsc_cmd_request init_request;
439 struct storvsc_cmd_request reset_request;
442 struct hv_host_device {
443 struct hv_device *dev;
446 unsigned char target;
449 struct storvsc_scan_work {
450 struct work_struct work;
451 struct Scsi_Host *host;
455 static void storvsc_device_scan(struct work_struct *work)
457 struct storvsc_scan_work *wrk;
459 struct scsi_device *sdev;
461 wrk = container_of(work, struct storvsc_scan_work, work);
464 sdev = scsi_device_lookup(wrk->host, 0, 0, lun);
467 scsi_rescan_device(&sdev->sdev_gendev);
468 scsi_device_put(sdev);
474 static void storvsc_host_scan(struct work_struct *work)
476 struct storvsc_scan_work *wrk;
477 struct Scsi_Host *host;
478 struct scsi_device *sdev;
480 wrk = container_of(work, struct storvsc_scan_work, work);
484 * Before scanning the host, first check to see if any of the
485 * currrently known devices have been hot removed. We issue a
486 * "unit ready" command against all currently known devices.
487 * This I/O will result in an error for devices that have been
488 * removed. As part of handling the I/O error, we remove the device.
490 * When a LUN is added or removed, the host sends us a signal to
491 * scan the host. Thus we are forced to discover the LUNs that
492 * may have been removed this way.
494 mutex_lock(&host->scan_mutex);
495 shost_for_each_device(sdev, host)
496 scsi_test_unit_ready(sdev, 1, 1, NULL);
497 mutex_unlock(&host->scan_mutex);
499 * Now scan the host to discover LUNs that may have been added.
501 scsi_scan_host(host);
506 static void storvsc_remove_lun(struct work_struct *work)
508 struct storvsc_scan_work *wrk;
509 struct scsi_device *sdev;
511 wrk = container_of(work, struct storvsc_scan_work, work);
512 if (!scsi_host_get(wrk->host))
515 sdev = scsi_device_lookup(wrk->host, 0, 0, wrk->lun);
518 scsi_remove_device(sdev);
519 scsi_device_put(sdev);
521 scsi_host_put(wrk->host);
529 * We can get incoming messages from the host that are not in response to
530 * messages that we have sent out. An example of this would be messages
531 * received by the guest to notify dynamic addition/removal of LUNs. To
532 * deal with potential race conditions where the driver may be in the
533 * midst of being unloaded when we might receive an unsolicited message
534 * from the host, we have implemented a mechanism to gurantee sequential
537 * 1) Once the device is marked as being destroyed, we will fail all
539 * 2) We permit incoming messages when the device is being destroyed,
540 * only to properly account for messages already sent out.
543 static inline struct storvsc_device *get_out_stor_device(
544 struct hv_device *device)
546 struct storvsc_device *stor_device;
548 stor_device = hv_get_drvdata(device);
550 if (stor_device && stor_device->destroy)
557 static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
559 dev->drain_notify = true;
560 wait_event(dev->waiting_to_drain,
561 atomic_read(&dev->num_outstanding_req) == 0);
562 dev->drain_notify = false;
565 static inline struct storvsc_device *get_in_stor_device(
566 struct hv_device *device)
568 struct storvsc_device *stor_device;
570 stor_device = hv_get_drvdata(device);
576 * If the device is being destroyed; allow incoming
577 * traffic only to cleanup outstanding requests.
580 if (stor_device->destroy &&
581 (atomic_read(&stor_device->num_outstanding_req) == 0))
589 static void destroy_bounce_buffer(struct scatterlist *sgl,
590 unsigned int sg_count)
593 struct page *page_buf;
595 for (i = 0; i < sg_count; i++) {
596 page_buf = sg_page((&sgl[i]));
597 if (page_buf != NULL)
598 __free_page(page_buf);
604 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
608 /* No need to check */
612 /* We have at least 2 sg entries */
613 for (i = 0; i < sg_count; i++) {
615 /* make sure 1st one does not have hole */
616 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
618 } else if (i == sg_count - 1) {
619 /* make sure last one does not have hole */
620 if (sgl[i].offset != 0)
623 /* make sure no hole in the middle */
624 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
631 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
632 unsigned int sg_count,
638 struct scatterlist *bounce_sgl;
639 struct page *page_buf;
640 unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);
642 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
644 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
648 sg_init_table(bounce_sgl, num_pages);
649 for (i = 0; i < num_pages; i++) {
650 page_buf = alloc_page(GFP_ATOMIC);
653 sg_set_page(&bounce_sgl[i], page_buf, buf_len, 0);
659 destroy_bounce_buffer(bounce_sgl, num_pages);
663 /* Assume the original sgl has enough room */
664 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
665 struct scatterlist *bounce_sgl,
666 unsigned int orig_sgl_count,
667 unsigned int bounce_sgl_count)
671 unsigned long src, dest;
672 unsigned int srclen, destlen, copylen;
673 unsigned int total_copied = 0;
674 unsigned long bounce_addr = 0;
675 unsigned long dest_addr = 0;
677 struct scatterlist *cur_dest_sgl;
678 struct scatterlist *cur_src_sgl;
680 local_irq_save(flags);
681 cur_dest_sgl = orig_sgl;
682 cur_src_sgl = bounce_sgl;
683 for (i = 0; i < orig_sgl_count; i++) {
684 dest_addr = (unsigned long)
685 kmap_atomic(sg_page(cur_dest_sgl)) +
686 cur_dest_sgl->offset;
688 destlen = cur_dest_sgl->length;
690 if (bounce_addr == 0)
691 bounce_addr = (unsigned long)kmap_atomic(
692 sg_page(cur_src_sgl));
695 src = bounce_addr + cur_src_sgl->offset;
696 srclen = cur_src_sgl->length - cur_src_sgl->offset;
698 copylen = min(srclen, destlen);
699 memcpy((void *)dest, (void *)src, copylen);
701 total_copied += copylen;
702 cur_src_sgl->offset += copylen;
706 if (cur_src_sgl->offset == cur_src_sgl->length) {
708 kunmap_atomic((void *)bounce_addr);
712 * It is possible that the number of elements
713 * in the bounce buffer may not be equal to
714 * the number of elements in the original
715 * scatter list. Handle this correctly.
718 if (j == bounce_sgl_count) {
720 * We are done; cleanup and return.
722 kunmap_atomic((void *)(dest_addr -
723 cur_dest_sgl->offset));
724 local_irq_restore(flags);
728 /* if we need to use another bounce buffer */
729 if (destlen || i != orig_sgl_count - 1) {
730 cur_src_sgl = sg_next(cur_src_sgl);
731 bounce_addr = (unsigned long)
733 sg_page(cur_src_sgl));
735 } else if (destlen == 0 && i == orig_sgl_count - 1) {
736 /* unmap the last bounce that is < PAGE_SIZE */
737 kunmap_atomic((void *)bounce_addr);
741 kunmap_atomic((void *)(dest_addr - cur_dest_sgl->offset));
742 cur_dest_sgl = sg_next(cur_dest_sgl);
745 local_irq_restore(flags);
750 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
751 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
752 struct scatterlist *bounce_sgl,
753 unsigned int orig_sgl_count)
757 unsigned long src, dest;
758 unsigned int srclen, destlen, copylen;
759 unsigned int total_copied = 0;
760 unsigned long bounce_addr = 0;
761 unsigned long src_addr = 0;
763 struct scatterlist *cur_src_sgl;
764 struct scatterlist *cur_dest_sgl;
766 local_irq_save(flags);
768 cur_src_sgl = orig_sgl;
769 cur_dest_sgl = bounce_sgl;
771 for (i = 0; i < orig_sgl_count; i++) {
772 src_addr = (unsigned long)
773 kmap_atomic(sg_page(cur_src_sgl)) +
776 srclen = cur_src_sgl->length;
778 if (bounce_addr == 0)
779 bounce_addr = (unsigned long)
780 kmap_atomic(sg_page(cur_dest_sgl));
783 /* assume bounce offset always == 0 */
784 dest = bounce_addr + cur_dest_sgl->length;
785 destlen = PAGE_SIZE - cur_dest_sgl->length;
787 copylen = min(srclen, destlen);
788 memcpy((void *)dest, (void *)src, copylen);
790 total_copied += copylen;
791 cur_dest_sgl->length += copylen;
795 if (cur_dest_sgl->length == PAGE_SIZE) {
796 /* full..move to next entry */
797 kunmap_atomic((void *)bounce_addr);
802 /* if we need to use another bounce buffer */
803 if (srclen && bounce_addr == 0) {
804 cur_dest_sgl = sg_next(cur_dest_sgl);
805 bounce_addr = (unsigned long)
807 sg_page(cur_dest_sgl));
812 kunmap_atomic((void *)(src_addr - cur_src_sgl->offset));
813 cur_src_sgl = sg_next(cur_src_sgl);
817 kunmap_atomic((void *)bounce_addr);
819 local_irq_restore(flags);
824 static void handle_sc_creation(struct vmbus_channel *new_sc)
826 struct hv_device *device = new_sc->primary_channel->device_obj;
827 struct storvsc_device *stor_device;
828 struct vmstorage_channel_properties props;
830 stor_device = get_out_stor_device(device);
834 if (stor_device->open_sub_channel == false)
837 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
840 storvsc_ringbuffer_size,
841 storvsc_ringbuffer_size,
843 sizeof(struct vmstorage_channel_properties),
844 storvsc_on_channel_callback, new_sc);
847 static void handle_multichannel_storage(struct hv_device *device, int max_chns)
849 struct storvsc_device *stor_device;
850 int num_cpus = num_online_cpus();
852 struct storvsc_cmd_request *request;
853 struct vstor_packet *vstor_packet;
856 num_sc = ((max_chns > num_cpus) ? num_cpus : max_chns);
857 stor_device = get_out_stor_device(device);
861 request = &stor_device->init_request;
862 vstor_packet = &request->vstor_packet;
864 stor_device->open_sub_channel = true;
866 * Establish a handler for dealing with subchannels.
868 vmbus_set_sc_create_callback(device->channel, handle_sc_creation);
871 * Check to see if sub-channels have already been created. This
872 * can happen when this driver is re-loaded after unloading.
875 if (vmbus_are_subchannels_present(device->channel))
878 stor_device->open_sub_channel = false;
880 * Request the host to create sub-channels.
882 memset(request, 0, sizeof(struct storvsc_cmd_request));
883 init_completion(&request->wait_event);
884 vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
885 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
886 vstor_packet->sub_channel_count = num_sc;
888 ret = vmbus_sendpacket(device->channel, vstor_packet,
889 (sizeof(struct vstor_packet) -
891 (unsigned long)request,
893 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
898 t = wait_for_completion_timeout(&request->wait_event, 10*HZ);
902 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
903 vstor_packet->status != 0)
907 * Now that we created the sub-channels, invoke the check; this
908 * may trigger the callback.
910 stor_device->open_sub_channel = true;
911 vmbus_are_subchannels_present(device->channel);
914 static int storvsc_channel_init(struct hv_device *device)
916 struct storvsc_device *stor_device;
917 struct storvsc_cmd_request *request;
918 struct vstor_packet *vstor_packet;
921 bool process_sub_channels = false;
923 stor_device = get_out_stor_device(device);
927 request = &stor_device->init_request;
928 vstor_packet = &request->vstor_packet;
931 * Now, initiate the vsc/vsp initialization protocol on the open
934 memset(request, 0, sizeof(struct storvsc_cmd_request));
935 init_completion(&request->wait_event);
936 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
937 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
939 ret = vmbus_sendpacket(device->channel, vstor_packet,
940 (sizeof(struct vstor_packet) -
942 (unsigned long)request,
944 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
948 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
954 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
955 vstor_packet->status != 0) {
961 for (i = 0; i < ARRAY_SIZE(vmstor_protocols); i++) {
962 /* reuse the packet for version range supported */
963 memset(vstor_packet, 0, sizeof(struct vstor_packet));
964 vstor_packet->operation =
965 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
966 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
968 vstor_packet->version.major_minor =
969 vmstor_protocols[i].protocol_version;
972 * The revision number is only used in Windows; set it to 0.
974 vstor_packet->version.revision = 0;
976 ret = vmbus_sendpacket(device->channel, vstor_packet,
977 (sizeof(struct vstor_packet) -
979 (unsigned long)request,
981 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
985 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
991 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO) {
996 if (vstor_packet->status == 0) {
997 vmstor_proto_version =
998 vmstor_protocols[i].protocol_version;
1001 vmstor_protocols[i].sense_buffer_size;
1004 vmstor_protocols[i].vmscsi_size_delta;
1010 if (vstor_packet->status != 0) {
1016 memset(vstor_packet, 0, sizeof(struct vstor_packet));
1017 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
1018 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1020 ret = vmbus_sendpacket(device->channel, vstor_packet,
1021 (sizeof(struct vstor_packet) -
1023 (unsigned long)request,
1025 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1030 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
1036 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
1037 vstor_packet->status != 0) {
1043 * Check to see if multi-channel support is there.
1044 * Hosts that implement protocol version of 5.1 and above
1045 * support multi-channel.
1047 max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
1048 if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN8) {
1049 if (vstor_packet->storage_channel_properties.flags &
1050 STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
1051 process_sub_channels = true;
1053 stor_device->max_transfer_bytes =
1054 vstor_packet->storage_channel_properties.max_transfer_bytes;
1056 memset(vstor_packet, 0, sizeof(struct vstor_packet));
1057 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
1058 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1060 ret = vmbus_sendpacket(device->channel, vstor_packet,
1061 (sizeof(struct vstor_packet) -
1063 (unsigned long)request,
1065 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1070 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
1076 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
1077 vstor_packet->status != 0) {
1082 if (process_sub_channels)
1083 handle_multichannel_storage(device, max_chns);
1090 static void storvsc_handle_error(struct vmscsi_request *vm_srb,
1091 struct scsi_cmnd *scmnd,
1092 struct Scsi_Host *host,
1095 struct storvsc_scan_work *wrk;
1096 void (*process_err_fn)(struct work_struct *work);
1097 bool do_work = false;
1099 switch (vm_srb->srb_status) {
1100 case SRB_STATUS_ERROR:
1102 * If there is an error; offline the device since all
1103 * error recovery strategies would have already been
1104 * deployed on the host side. However, if the command
1105 * were a pass-through command deal with it appropriately.
1107 switch (scmnd->cmnd[0]) {
1110 set_host_byte(scmnd, DID_PASSTHROUGH);
1113 * On Some Windows hosts TEST_UNIT_READY command can return
1114 * SRB_STATUS_ERROR, let the upper level code deal with it
1115 * based on the sense information.
1117 case TEST_UNIT_READY:
1120 set_host_byte(scmnd, DID_TARGET_FAILURE);
1123 case SRB_STATUS_INVALID_LUN:
1125 process_err_fn = storvsc_remove_lun;
1127 case (SRB_STATUS_ABORTED | SRB_STATUS_AUTOSENSE_VALID):
1128 if ((asc == 0x2a) && (ascq == 0x9)) {
1130 process_err_fn = storvsc_device_scan;
1132 * Retry the I/O that trigerred this.
1134 set_host_byte(scmnd, DID_REQUEUE);
1143 * We need to schedule work to process this error; schedule it.
1145 wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
1147 set_host_byte(scmnd, DID_TARGET_FAILURE);
1152 wrk->lun = vm_srb->lun;
1153 INIT_WORK(&wrk->work, process_err_fn);
1154 schedule_work(&wrk->work);
1158 static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request)
1160 struct scsi_cmnd *scmnd = cmd_request->cmd;
1161 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1162 struct scsi_sense_hdr sense_hdr;
1163 struct vmscsi_request *vm_srb;
1164 struct Scsi_Host *host;
1165 struct storvsc_device *stor_dev;
1166 struct hv_device *dev = host_dev->dev;
1167 u32 payload_sz = cmd_request->payload_sz;
1168 void *payload = cmd_request->payload;
1170 stor_dev = get_in_stor_device(dev);
1171 host = stor_dev->host;
1173 vm_srb = &cmd_request->vstor_packet.vm_srb;
1174 if (cmd_request->bounce_sgl_count) {
1175 if (vm_srb->data_in == READ_TYPE)
1176 copy_from_bounce_buffer(scsi_sglist(scmnd),
1177 cmd_request->bounce_sgl,
1178 scsi_sg_count(scmnd),
1179 cmd_request->bounce_sgl_count);
1180 destroy_bounce_buffer(cmd_request->bounce_sgl,
1181 cmd_request->bounce_sgl_count);
1184 scmnd->result = vm_srb->scsi_status;
1186 if (scmnd->result) {
1187 if (scsi_normalize_sense(scmnd->sense_buffer,
1188 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1189 scsi_print_sense_hdr(scmnd->device, "storvsc",
1193 if (vm_srb->srb_status != SRB_STATUS_SUCCESS)
1194 storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
1197 scsi_set_resid(scmnd,
1198 cmd_request->payload->range.len -
1199 vm_srb->data_transfer_length);
1201 scmnd->scsi_done(scmnd);
1204 sizeof(struct vmbus_channel_packet_multipage_buffer))
1208 static void storvsc_on_io_completion(struct hv_device *device,
1209 struct vstor_packet *vstor_packet,
1210 struct storvsc_cmd_request *request)
1212 struct storvsc_device *stor_device;
1213 struct vstor_packet *stor_pkt;
1215 stor_device = hv_get_drvdata(device);
1216 stor_pkt = &request->vstor_packet;
1219 * The current SCSI handling on the host side does
1220 * not correctly handle:
1221 * INQUIRY command with page code parameter set to 0x80
1222 * MODE_SENSE command with cmd[2] == 0x1c
1224 * Setup srb and scsi status so this won't be fatal.
1225 * We do this so we can distinguish truly fatal failues
1226 * (srb status == 0x4) and off-line the device in that case.
1229 if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
1230 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
1231 vstor_packet->vm_srb.scsi_status = 0;
1232 vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
1236 /* Copy over the status...etc */
1237 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
1238 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
1239 stor_pkt->vm_srb.sense_info_length =
1240 vstor_packet->vm_srb.sense_info_length;
1243 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
1244 /* CHECK_CONDITION */
1245 if (vstor_packet->vm_srb.srb_status &
1246 SRB_STATUS_AUTOSENSE_VALID) {
1247 /* autosense data available */
1249 memcpy(request->cmd->sense_buffer,
1250 vstor_packet->vm_srb.sense_data,
1251 vstor_packet->vm_srb.sense_info_length);
1256 stor_pkt->vm_srb.data_transfer_length =
1257 vstor_packet->vm_srb.data_transfer_length;
1259 storvsc_command_completion(request);
1261 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
1262 stor_device->drain_notify)
1263 wake_up(&stor_device->waiting_to_drain);
1268 static void storvsc_on_receive(struct hv_device *device,
1269 struct vstor_packet *vstor_packet,
1270 struct storvsc_cmd_request *request)
1272 struct storvsc_scan_work *work;
1273 struct storvsc_device *stor_device;
1275 switch (vstor_packet->operation) {
1276 case VSTOR_OPERATION_COMPLETE_IO:
1277 storvsc_on_io_completion(device, vstor_packet, request);
1280 case VSTOR_OPERATION_REMOVE_DEVICE:
1281 case VSTOR_OPERATION_ENUMERATE_BUS:
1282 stor_device = get_in_stor_device(device);
1283 work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
1287 INIT_WORK(&work->work, storvsc_host_scan);
1288 work->host = stor_device->host;
1289 schedule_work(&work->work);
1297 static void storvsc_on_channel_callback(void *context)
1299 struct vmbus_channel *channel = (struct vmbus_channel *)context;
1300 struct hv_device *device;
1301 struct storvsc_device *stor_device;
1304 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
1305 struct storvsc_cmd_request *request;
1308 if (channel->primary_channel != NULL)
1309 device = channel->primary_channel->device_obj;
1311 device = channel->device_obj;
1313 stor_device = get_in_stor_device(device);
1318 ret = vmbus_recvpacket(channel, packet,
1319 ALIGN((sizeof(struct vstor_packet) -
1320 vmscsi_size_delta), 8),
1321 &bytes_recvd, &request_id);
1322 if (ret == 0 && bytes_recvd > 0) {
1324 request = (struct storvsc_cmd_request *)
1325 (unsigned long)request_id;
1327 if ((request == &stor_device->init_request) ||
1328 (request == &stor_device->reset_request)) {
1330 memcpy(&request->vstor_packet, packet,
1331 (sizeof(struct vstor_packet) -
1332 vmscsi_size_delta));
1333 complete(&request->wait_event);
1335 storvsc_on_receive(device,
1336 (struct vstor_packet *)packet,
1347 static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
1349 struct vmstorage_channel_properties props;
1352 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
1354 ret = vmbus_open(device->channel,
1358 sizeof(struct vmstorage_channel_properties),
1359 storvsc_on_channel_callback, device->channel);
1364 ret = storvsc_channel_init(device);
1369 static int storvsc_dev_remove(struct hv_device *device)
1371 struct storvsc_device *stor_device;
1372 unsigned long flags;
1374 stor_device = hv_get_drvdata(device);
1376 spin_lock_irqsave(&device->channel->inbound_lock, flags);
1377 stor_device->destroy = true;
1378 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1381 * At this point, all outbound traffic should be disable. We
1382 * only allow inbound traffic (responses) to proceed so that
1383 * outstanding requests can be completed.
1386 storvsc_wait_to_drain(stor_device);
1389 * Since we have already drained, we don't need to busy wait
1390 * as was done in final_release_stor_device()
1391 * Note that we cannot set the ext pointer to NULL until
1392 * we have drained - to drain the outgoing packets, we need to
1393 * allow incoming packets.
1395 spin_lock_irqsave(&device->channel->inbound_lock, flags);
1396 hv_set_drvdata(device, NULL);
1397 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1399 /* Close the channel */
1400 vmbus_close(device->channel);
1406 static int storvsc_do_io(struct hv_device *device,
1407 struct storvsc_cmd_request *request)
1409 struct storvsc_device *stor_device;
1410 struct vstor_packet *vstor_packet;
1411 struct vmbus_channel *outgoing_channel;
1414 vstor_packet = &request->vstor_packet;
1415 stor_device = get_out_stor_device(device);
1421 request->device = device;
1423 * Select an an appropriate channel to send the request out.
1426 outgoing_channel = vmbus_get_outgoing_channel(device->channel);
1429 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
1431 vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) -
1435 vstor_packet->vm_srb.sense_info_length = sense_buffer_size;
1438 vstor_packet->vm_srb.data_transfer_length =
1439 request->payload->range.len;
1441 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
1443 if (request->payload->range.len) {
1445 ret = vmbus_sendpacket_mpb_desc(outgoing_channel,
1446 request->payload, request->payload_sz,
1448 (sizeof(struct vstor_packet) -
1450 (unsigned long)request);
1452 ret = vmbus_sendpacket(outgoing_channel, vstor_packet,
1453 (sizeof(struct vstor_packet) -
1455 (unsigned long)request,
1457 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1463 atomic_inc(&stor_device->num_outstanding_req);
1468 static int storvsc_device_configure(struct scsi_device *sdevice)
1471 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
1473 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
1475 blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
1477 sdevice->no_write_same = 1;
1480 * Add blist flags to permit the reading of the VPD pages even when
1481 * the target may claim SPC-2 compliance. MSFT targets currently
1482 * claim SPC-2 compliance while they implement post SPC-2 features.
1483 * With this patch we can correctly handle WRITE_SAME_16 issues.
1485 sdevice->sdev_bflags |= msft_blist_flags;
1488 * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3
1489 * if the device is a MSFT virtual device. If the host is
1490 * WIN10 or newer, allow write_same.
1492 if (!strncmp(sdevice->vendor, "Msft", 4)) {
1493 switch (vmstor_proto_version) {
1494 case VMSTOR_PROTO_VERSION_WIN8:
1495 case VMSTOR_PROTO_VERSION_WIN8_1:
1496 sdevice->scsi_level = SCSI_SPC_3;
1500 if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN10)
1501 sdevice->no_write_same = 0;
1507 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1508 sector_t capacity, int *info)
1510 sector_t nsect = capacity;
1511 sector_t cylinders = nsect;
1512 int heads, sectors_pt;
1515 * We are making up these values; let us keep it simple.
1518 sectors_pt = 0x3f; /* Sectors per track */
1519 sector_div(cylinders, heads * sectors_pt);
1520 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1524 info[1] = sectors_pt;
1525 info[2] = (int)cylinders;
1530 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
1532 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1533 struct hv_device *device = host_dev->dev;
1535 struct storvsc_device *stor_device;
1536 struct storvsc_cmd_request *request;
1537 struct vstor_packet *vstor_packet;
1541 stor_device = get_out_stor_device(device);
1545 request = &stor_device->reset_request;
1546 vstor_packet = &request->vstor_packet;
1548 init_completion(&request->wait_event);
1550 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1551 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1552 vstor_packet->vm_srb.path_id = stor_device->path_id;
1554 ret = vmbus_sendpacket(device->channel, vstor_packet,
1555 (sizeof(struct vstor_packet) -
1557 (unsigned long)&stor_device->reset_request,
1559 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1563 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
1565 return TIMEOUT_ERROR;
1569 * At this point, all outstanding requests in the adapter
1570 * should have been flushed out and return to us
1571 * There is a potential race here where the host may be in
1572 * the process of responding when we return from here.
1573 * Just wait for all in-transit packets to be accounted for
1574 * before we return from here.
1576 storvsc_wait_to_drain(stor_device);
1582 * The host guarantees to respond to each command, although I/O latencies might
1583 * be unbounded on Azure. Reset the timer unconditionally to give the host a
1584 * chance to perform EH.
1586 static enum blk_eh_timer_return storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
1588 return BLK_EH_RESET_TIMER;
1591 static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
1593 bool allowed = true;
1594 u8 scsi_op = scmnd->cmnd[0];
1597 /* the host does not handle WRITE_SAME, log accident usage */
1600 * smartd sends this command and the host does not handle
1601 * this. So, don't send it.
1604 scmnd->result = ILLEGAL_REQUEST << 16;
1613 static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
1616 struct hv_host_device *host_dev = shost_priv(host);
1617 struct hv_device *dev = host_dev->dev;
1618 struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd);
1620 struct scatterlist *sgl;
1621 unsigned int sg_count = 0;
1622 struct vmscsi_request *vm_srb;
1623 struct scatterlist *cur_sgl;
1624 struct vmbus_packet_mpb_array *payload;
1628 if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) {
1630 * On legacy hosts filter unimplemented commands.
1631 * Future hosts are expected to correctly handle
1632 * unsupported commands. Furthermore, it is
1633 * possible that some of the currently
1634 * unsupported commands maybe supported in
1635 * future versions of the host.
1637 if (!storvsc_scsi_cmd_ok(scmnd)) {
1638 scmnd->scsi_done(scmnd);
1643 /* Setup the cmd request */
1644 cmd_request->cmd = scmnd;
1646 vm_srb = &cmd_request->vstor_packet.vm_srb;
1647 vm_srb->win8_extension.time_out_value = 60;
1649 vm_srb->win8_extension.srb_flags |=
1650 (SRB_FLAGS_QUEUE_ACTION_ENABLE |
1651 SRB_FLAGS_DISABLE_SYNCH_TRANSFER);
1654 switch (scmnd->sc_data_direction) {
1656 vm_srb->data_in = WRITE_TYPE;
1657 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT;
1659 case DMA_FROM_DEVICE:
1660 vm_srb->data_in = READ_TYPE;
1661 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN;
1664 vm_srb->data_in = UNKNOWN_TYPE;
1665 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER;
1669 * This is DMA_BIDIRECTIONAL or something else we are never
1670 * supposed to see here.
1672 WARN(1, "Unexpected data direction: %d\n",
1673 scmnd->sc_data_direction);
1678 vm_srb->port_number = host_dev->port;
1679 vm_srb->path_id = scmnd->device->channel;
1680 vm_srb->target_id = scmnd->device->id;
1681 vm_srb->lun = scmnd->device->lun;
1683 vm_srb->cdb_length = scmnd->cmd_len;
1685 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1687 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1688 sg_count = scsi_sg_count(scmnd);
1690 length = scsi_bufflen(scmnd);
1691 payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
1692 payload_sz = sizeof(cmd_request->mpb);
1695 /* check if we need to bounce the sgl */
1696 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
1697 cmd_request->bounce_sgl =
1698 create_bounce_buffer(sgl, sg_count,
1701 if (!cmd_request->bounce_sgl)
1702 return SCSI_MLQUEUE_HOST_BUSY;
1704 cmd_request->bounce_sgl_count =
1705 ALIGN(length, PAGE_SIZE) >> PAGE_SHIFT;
1707 if (vm_srb->data_in == WRITE_TYPE)
1708 copy_to_bounce_buffer(sgl,
1709 cmd_request->bounce_sgl, sg_count);
1711 sgl = cmd_request->bounce_sgl;
1712 sg_count = cmd_request->bounce_sgl_count;
1716 if (sg_count > MAX_PAGE_BUFFER_COUNT) {
1718 payload_sz = (sg_count * sizeof(void *) +
1719 sizeof(struct vmbus_packet_mpb_array));
1720 payload = kmalloc(payload_sz, GFP_ATOMIC);
1722 if (cmd_request->bounce_sgl_count)
1723 destroy_bounce_buffer(
1724 cmd_request->bounce_sgl,
1725 cmd_request->bounce_sgl_count);
1727 return SCSI_MLQUEUE_DEVICE_BUSY;
1731 payload->range.len = length;
1732 payload->range.offset = sgl[0].offset;
1735 for (i = 0; i < sg_count; i++) {
1736 payload->range.pfn_array[i] =
1737 page_to_pfn(sg_page((cur_sgl)));
1738 cur_sgl = sg_next(cur_sgl);
1741 } else if (scsi_sglist(scmnd)) {
1742 payload->range.len = length;
1743 payload->range.offset =
1744 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
1745 payload->range.pfn_array[0] =
1746 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1749 cmd_request->payload = payload;
1750 cmd_request->payload_sz = payload_sz;
1752 /* Invokes the vsc to start an IO */
1753 ret = storvsc_do_io(dev, cmd_request);
1755 if (ret == -EAGAIN) {
1758 if (cmd_request->bounce_sgl_count)
1759 destroy_bounce_buffer(cmd_request->bounce_sgl,
1760 cmd_request->bounce_sgl_count);
1762 return SCSI_MLQUEUE_DEVICE_BUSY;
1768 static struct scsi_host_template scsi_driver = {
1769 .module = THIS_MODULE,
1770 .name = "storvsc_host_t",
1771 .cmd_size = sizeof(struct storvsc_cmd_request),
1772 .bios_param = storvsc_get_chs,
1773 .queuecommand = storvsc_queuecommand,
1774 .eh_host_reset_handler = storvsc_host_reset_handler,
1775 .proc_name = "storvsc_host",
1776 .eh_timed_out = storvsc_eh_timed_out,
1777 .slave_configure = storvsc_device_configure,
1780 .use_clustering = ENABLE_CLUSTERING,
1781 /* Make sure we dont get a sg segment crosses a page boundary */
1782 .dma_boundary = PAGE_SIZE-1,
1792 static const struct hv_vmbus_device_id id_table[] = {
1795 .driver_data = SCSI_GUID
1799 .driver_data = IDE_GUID
1801 /* Fibre Channel GUID */
1804 .driver_data = SFC_GUID
1809 MODULE_DEVICE_TABLE(vmbus, id_table);
1811 static int storvsc_probe(struct hv_device *device,
1812 const struct hv_vmbus_device_id *dev_id)
1815 int num_cpus = num_online_cpus();
1816 struct Scsi_Host *host;
1817 struct hv_host_device *host_dev;
1818 bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
1820 struct storvsc_device *stor_device;
1821 int max_luns_per_target;
1824 int max_sub_channels = 0;
1827 * Based on the windows host we are running on,
1828 * set state to properly communicate with the host.
1831 if (vmbus_proto_version < VERSION_WIN8) {
1832 max_luns_per_target = STORVSC_IDE_MAX_LUNS_PER_TARGET;
1833 max_targets = STORVSC_IDE_MAX_TARGETS;
1834 max_channels = STORVSC_IDE_MAX_CHANNELS;
1836 max_luns_per_target = STORVSC_MAX_LUNS_PER_TARGET;
1837 max_targets = STORVSC_MAX_TARGETS;
1838 max_channels = STORVSC_MAX_CHANNELS;
1840 * On Windows8 and above, we support sub-channels for storage.
1841 * The number of sub-channels offerred is based on the number of
1842 * VCPUs in the guest.
1844 max_sub_channels = (num_cpus / storvsc_vcpus_per_sub_channel);
1847 scsi_driver.can_queue = (max_outstanding_req_per_channel *
1848 (max_sub_channels + 1));
1850 host = scsi_host_alloc(&scsi_driver,
1851 sizeof(struct hv_host_device));
1855 host_dev = shost_priv(host);
1856 memset(host_dev, 0, sizeof(struct hv_host_device));
1858 host_dev->port = host->host_no;
1859 host_dev->dev = device;
1862 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
1868 stor_device->destroy = false;
1869 stor_device->open_sub_channel = false;
1870 init_waitqueue_head(&stor_device->waiting_to_drain);
1871 stor_device->device = device;
1872 stor_device->host = host;
1873 hv_set_drvdata(device, stor_device);
1875 stor_device->port_number = host->host_no;
1876 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
1880 host_dev->path = stor_device->path_id;
1881 host_dev->target = stor_device->target_id;
1883 switch (dev_id->driver_data) {
1885 host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
1886 host->max_id = STORVSC_FC_MAX_TARGETS;
1887 host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
1891 host->max_lun = max_luns_per_target;
1892 host->max_id = max_targets;
1893 host->max_channel = max_channels - 1;
1897 host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
1898 host->max_id = STORVSC_IDE_MAX_TARGETS;
1899 host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
1902 /* max cmd length */
1903 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
1906 * set the table size based on the info we got
1909 host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
1911 /* Register the HBA and start the scsi bus scan */
1912 ret = scsi_add_host(host, &device->device);
1917 scsi_scan_host(host);
1919 target = (device->dev_instance.b[5] << 8 |
1920 device->dev_instance.b[4]);
1921 ret = scsi_add_device(host, 0, target, 0);
1923 scsi_remove_host(host);
1931 * Once we have connected with the host, we would need to
1932 * to invoke storvsc_dev_remove() to rollback this state and
1933 * this call also frees up the stor_device; hence the jump around
1936 storvsc_dev_remove(device);
1943 scsi_host_put(host);
1947 static int storvsc_remove(struct hv_device *dev)
1949 struct storvsc_device *stor_device = hv_get_drvdata(dev);
1950 struct Scsi_Host *host = stor_device->host;
1952 scsi_remove_host(host);
1953 storvsc_dev_remove(dev);
1954 scsi_host_put(host);
1959 static struct hv_driver storvsc_drv = {
1960 .name = KBUILD_MODNAME,
1961 .id_table = id_table,
1962 .probe = storvsc_probe,
1963 .remove = storvsc_remove,
1966 static int __init storvsc_drv_init(void)
1970 * Divide the ring buffer data size (which is 1 page less
1971 * than the ring buffer size since that page is reserved for
1972 * the ring buffer indices) by the max request size (which is
1973 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1975 max_outstanding_req_per_channel =
1976 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1977 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1978 sizeof(struct vstor_packet) + sizeof(u64) -
1982 return vmbus_driver_register(&storvsc_drv);
1985 static void __exit storvsc_drv_exit(void)
1987 vmbus_driver_unregister(&storvsc_drv);
1990 MODULE_LICENSE("GPL");
1991 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
1992 module_init(storvsc_drv_init);
1993 module_exit(storvsc_drv_exit);