Thomas Gleixner | 3b20eb2 | 2019-05-29 16:57:35 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 2 | /* |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 3 | * Copyright (c) 2009, Microsoft Corporation. |
| 4 | * |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 5 | * Authors: |
| 6 | * Haiyang Zhang <haiyangz@microsoft.com> |
| 7 | * Hank Janssen <hjanssen@microsoft.com> |
K. Y. Srinivasan | 972621c | 2011-05-10 07:54:19 -0700 | [diff] [blame] | 8 | * K. Y. Srinivasan <kys@microsoft.com> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 9 | */ |
K. Y. Srinivasan | a1be170 | 2011-08-27 11:31:23 -0700 | [diff] [blame] | 10 | |
| 11 | #include <linux/kernel.h> |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 12 | #include <linux/wait.h> |
K. Y. Srinivasan | a1be170 | 2011-08-27 11:31:23 -0700 | [diff] [blame] | 13 | #include <linux/sched.h> |
| 14 | #include <linux/completion.h> |
| 15 | #include <linux/string.h> |
| 16 | #include <linux/mm.h> |
| 17 | #include <linux/delay.h> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 18 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 20 | #include <linux/module.h> |
| 21 | #include <linux/device.h> |
Greg Kroah-Hartman | 46a9719 | 2011-10-04 12:29:52 -0700 | [diff] [blame] | 22 | #include <linux/hyperv.h> |
K. Y. Srinivasan | 56b26e6 | 2014-07-12 09:48:30 -0700 | [diff] [blame] | 23 | #include <linux/blkdev.h> |
Tianyu Lan | 743b237 | 2021-12-13 02:14:05 -0500 | [diff] [blame] | 24 | #include <linux/dma-mapping.h> |
| 25 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 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> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 33 | #include <scsi/scsi_dbg.h> |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 34 | #include <scsi/scsi_transport_fc.h> |
K. Y. Srinivasan | d791a8c | 2016-01-27 18:22:44 -0800 | [diff] [blame] | 35 | #include <scsi/scsi_transport.h> |
K. Y. Srinivasan | 3f335ea | 2011-05-12 19:34:15 -0700 | [diff] [blame] | 36 | |
K. Y. Srinivasan | c649114 | 2012-01-12 12:38:03 -0800 | [diff] [blame] | 37 | /* |
K. Y. Srinivasan | af9584b | 2012-01-12 12:38:08 -0800 | [diff] [blame] | 38 | * All wire protocol details (storage protocol between the guest and the host) |
| 39 | * are consolidated here. |
| 40 | * |
| 41 | * Begin protocol definitions. |
K. Y. Srinivasan | c649114 | 2012-01-12 12:38:03 -0800 | [diff] [blame] | 42 | */ |
| 43 | |
K. Y. Srinivasan | 09f0355 | 2012-01-12 12:37:54 -0800 | [diff] [blame] | 44 | /* |
| 45 | * Version history: |
| 46 | * V1 Beta: 0.1 |
| 47 | * V1 RC < 2008/1/31: 1.0 |
| 48 | * V1 RC > 2008/1/31: 2.0 |
| 49 | * Win7: 4.2 |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 50 | * Win8: 5.1 |
Keith Mange | 1a36310 | 2015-08-13 08:43:48 -0700 | [diff] [blame] | 51 | * Win8.1: 6.0 |
| 52 | * Win10: 6.2 |
K. Y. Srinivasan | 09f0355 | 2012-01-12 12:37:54 -0800 | [diff] [blame] | 53 | */ |
| 54 | |
Keith Mange | 2492fd7 | 2015-08-13 08:43:47 -0700 | [diff] [blame] | 55 | #define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \ |
| 56 | (((MINOR_) & 0xff))) |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 57 | |
Keith Mange | 1a36310 | 2015-08-13 08:43:48 -0700 | [diff] [blame] | 58 | #define VMSTOR_PROTO_VERSION_WIN6 VMSTOR_PROTO_VERSION(2, 0) |
Keith Mange | 2492fd7 | 2015-08-13 08:43:47 -0700 | [diff] [blame] | 59 | #define VMSTOR_PROTO_VERSION_WIN7 VMSTOR_PROTO_VERSION(4, 2) |
| 60 | #define VMSTOR_PROTO_VERSION_WIN8 VMSTOR_PROTO_VERSION(5, 1) |
Keith Mange | 1a36310 | 2015-08-13 08:43:48 -0700 | [diff] [blame] | 61 | #define VMSTOR_PROTO_VERSION_WIN8_1 VMSTOR_PROTO_VERSION(6, 0) |
| 62 | #define VMSTOR_PROTO_VERSION_WIN10 VMSTOR_PROTO_VERSION(6, 2) |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 63 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 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, |
K. Y. Srinivasan | 2b9525f | 2011-11-08 09:01:48 -0800 | [diff] [blame] | 76 | VSTOR_OPERATION_ENUMERATE_BUS = 11, |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 77 | VSTOR_OPERATION_FCHBA_DATA = 12, |
| 78 | VSTOR_OPERATION_CREATE_SUB_CHANNELS = 13, |
| 79 | VSTOR_OPERATION_MAXIMUM = 13 |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | /* |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 83 | * WWN packet for Fibre Channel HBA |
| 84 | */ |
| 85 | |
| 86 | struct hv_fc_wwn_packet { |
K. Y. Srinivasan | 83d1e8b | 2015-12-23 13:15:48 -0800 | [diff] [blame] | 87 | u8 primary_active; |
| 88 | u8 reserved1[3]; |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 89 | u8 primary_port_wwn[8]; |
| 90 | u8 primary_node_wwn[8]; |
| 91 | u8 secondary_port_wwn[8]; |
| 92 | u8 secondary_node_wwn[8]; |
| 93 | }; |
| 94 | |
| 95 | |
| 96 | |
| 97 | /* |
| 98 | * SRB Flag Bits |
| 99 | */ |
| 100 | |
| 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 |
| 113 | |
| 114 | /* |
| 115 | * This flag indicates the request is part of the workflow for processing a D3. |
| 116 | */ |
| 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 |
| 128 | |
Long Li | 3cd6d3d | 2016-12-14 18:46:01 -0800 | [diff] [blame] | 129 | #define SP_UNTAGGED ((unsigned char) ~0) |
| 130 | #define SRB_SIMPLE_TAG_REQUEST 0x20 |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 131 | |
| 132 | /* |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 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 |
| 136 | */ |
K. Y. Srinivasan | 6b2f949 | 2012-01-12 12:38:05 -0800 | [diff] [blame] | 137 | #define STORVSC_MAX_CMD_LEN 0x10 |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 138 | |
| 139 | #define POST_WIN7_STORVSC_SENSE_BUFFER_SIZE 0x14 |
| 140 | #define PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE 0x12 |
| 141 | |
| 142 | #define STORVSC_SENSE_BUFFER_SIZE 0x14 |
K. Y. Srinivasan | 6b2f949 | 2012-01-12 12:38:05 -0800 | [diff] [blame] | 143 | #define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14 |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 144 | |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 145 | /* |
| 146 | * Sense buffer size changed in win8; have a run-time |
Keith Mange | cb11fead | 2015-08-13 08:43:49 -0700 | [diff] [blame] | 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. |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 150 | */ |
Keith Mange | cb11fead | 2015-08-13 08:43:49 -0700 | [diff] [blame] | 151 | static int sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE; |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 152 | |
| 153 | /* |
Keith Mange | cb11fead | 2015-08-13 08:43:49 -0700 | [diff] [blame] | 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. |
| 157 | */ |
Keith Mange | 2492fd7 | 2015-08-13 08:43:47 -0700 | [diff] [blame] | 158 | static int vmstor_proto_version; |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 159 | |
Long Li | f8aea70 | 2015-12-04 00:07:24 -0800 | [diff] [blame] | 160 | #define STORVSC_LOGGING_NONE 0 |
| 161 | #define STORVSC_LOGGING_ERROR 1 |
| 162 | #define STORVSC_LOGGING_WARN 2 |
| 163 | |
| 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."); |
| 168 | |
| 169 | static inline bool do_logging(int level) |
| 170 | { |
| 171 | return logging_level >= level; |
| 172 | } |
| 173 | |
| 174 | #define storvsc_log(dev, level, fmt, ...) \ |
| 175 | do { \ |
| 176 | if (do_logging(level)) \ |
| 177 | dev_warn(&(dev)->device, fmt, ##__VA_ARGS__); \ |
| 178 | } while (0) |
| 179 | |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 180 | struct vmscsi_win8_extension { |
| 181 | /* |
| 182 | * The following were added in Windows 8 |
| 183 | */ |
| 184 | u16 reserve; |
| 185 | u8 queue_tag; |
| 186 | u8 queue_action; |
| 187 | u32 srb_flags; |
| 188 | u32 time_out_value; |
| 189 | u32 queue_sort_ey; |
| 190 | } __packed; |
| 191 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 192 | struct vmscsi_request { |
K. Y. Srinivasan | c649114 | 2012-01-12 12:38:03 -0800 | [diff] [blame] | 193 | u16 length; |
| 194 | u8 srb_status; |
| 195 | u8 scsi_status; |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 196 | |
K. Y. Srinivasan | c649114 | 2012-01-12 12:38:03 -0800 | [diff] [blame] | 197 | u8 port_number; |
| 198 | u8 path_id; |
| 199 | u8 target_id; |
| 200 | u8 lun; |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 201 | |
K. Y. Srinivasan | c649114 | 2012-01-12 12:38:03 -0800 | [diff] [blame] | 202 | u8 cdb_length; |
| 203 | u8 sense_info_length; |
| 204 | u8 data_in; |
| 205 | u8 reserved; |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 206 | |
K. Y. Srinivasan | c649114 | 2012-01-12 12:38:03 -0800 | [diff] [blame] | 207 | u32 data_transfer_length; |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 208 | |
| 209 | union { |
K. Y. Srinivasan | 6b2f949 | 2012-01-12 12:38:05 -0800 | [diff] [blame] | 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]; |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 213 | }; |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 214 | /* |
| 215 | * The following was added in win8. |
| 216 | */ |
| 217 | struct vmscsi_win8_extension win8_extension; |
| 218 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 219 | } __attribute((packed)); |
| 220 | |
Keith Mange | cb11fead | 2015-08-13 08:43:49 -0700 | [diff] [blame] | 221 | /* |
Keith Mange | 1a36310 | 2015-08-13 08:43:48 -0700 | [diff] [blame] | 222 | * The list of storage protocols in order of preference. |
| 223 | */ |
| 224 | struct vmstor_protocol { |
| 225 | int protocol_version; |
| 226 | int sense_buffer_size; |
| 227 | int vmscsi_size_delta; |
| 228 | }; |
| 229 | |
| 230 | |
| 231 | static const struct vmstor_protocol vmstor_protocols[] = { |
| 232 | { |
| 233 | VMSTOR_PROTO_VERSION_WIN10, |
| 234 | POST_WIN7_STORVSC_SENSE_BUFFER_SIZE, |
| 235 | 0 |
| 236 | }, |
| 237 | { |
| 238 | VMSTOR_PROTO_VERSION_WIN8_1, |
| 239 | POST_WIN7_STORVSC_SENSE_BUFFER_SIZE, |
| 240 | 0 |
| 241 | }, |
| 242 | { |
| 243 | VMSTOR_PROTO_VERSION_WIN8, |
| 244 | POST_WIN7_STORVSC_SENSE_BUFFER_SIZE, |
| 245 | 0 |
| 246 | }, |
| 247 | { |
| 248 | VMSTOR_PROTO_VERSION_WIN7, |
| 249 | PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE, |
| 250 | sizeof(struct vmscsi_win8_extension), |
| 251 | }, |
| 252 | { |
| 253 | VMSTOR_PROTO_VERSION_WIN6, |
| 254 | PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE, |
| 255 | sizeof(struct vmscsi_win8_extension), |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | |
| 260 | /* |
Masahiro Yamada | 183b802 | 2017-02-27 14:29:20 -0800 | [diff] [blame] | 261 | * This structure is sent during the initialization phase to get the different |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 262 | * properties of the channel. |
| 263 | */ |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 264 | |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 265 | #define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1 |
| 266 | |
| 267 | struct vmstorage_channel_properties { |
| 268 | u32 reserved; |
| 269 | u16 max_channel_cnt; |
| 270 | u16 reserved1; |
| 271 | |
| 272 | u32 flags; |
K. Y. Srinivasan | c649114 | 2012-01-12 12:38:03 -0800 | [diff] [blame] | 273 | u32 max_transfer_bytes; |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 274 | |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 275 | u64 reserved2; |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 276 | } __packed; |
| 277 | |
| 278 | /* This structure is sent during the storage protocol negotiations. */ |
| 279 | struct vmstorage_protocol_version { |
| 280 | /* Major (MSW) and minor (LSW) version numbers. */ |
K. Y. Srinivasan | 85904a5 | 2012-01-12 12:38:04 -0800 | [diff] [blame] | 281 | u16 major_minor; |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 282 | |
| 283 | /* |
| 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 |
| 287 | * builds. |
K. Y. Srinivasan | c649114 | 2012-01-12 12:38:03 -0800 | [diff] [blame] | 288 | * This is only used on the windows side. Just set it to 0. |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 289 | */ |
K. Y. Srinivasan | 85904a5 | 2012-01-12 12:38:04 -0800 | [diff] [blame] | 290 | u16 revision; |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 291 | } __packed; |
| 292 | |
| 293 | /* Channel Property Flags */ |
| 294 | #define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1 |
| 295 | #define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2 |
| 296 | |
| 297 | struct vstor_packet { |
| 298 | /* Requested operation type */ |
| 299 | enum vstor_packet_operation operation; |
| 300 | |
| 301 | /* Flags - see below for values */ |
K. Y. Srinivasan | c649114 | 2012-01-12 12:38:03 -0800 | [diff] [blame] | 302 | u32 flags; |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 303 | |
| 304 | /* Status of the request returned from the server side. */ |
K. Y. Srinivasan | c649114 | 2012-01-12 12:38:03 -0800 | [diff] [blame] | 305 | u32 status; |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 306 | |
| 307 | /* Data payload area */ |
| 308 | union { |
| 309 | /* |
| 310 | * Structure used to forward SCSI commands from the |
| 311 | * client to the server. |
| 312 | */ |
| 313 | struct vmscsi_request vm_srb; |
| 314 | |
| 315 | /* Structure used to query channel properties. */ |
| 316 | struct vmstorage_channel_properties storage_channel_properties; |
| 317 | |
| 318 | /* Used during version negotiations. */ |
| 319 | struct vmstorage_protocol_version version; |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 320 | |
| 321 | /* Fibre channel address packet */ |
| 322 | struct hv_fc_wwn_packet wwn_packet; |
| 323 | |
| 324 | /* Number of sub-channels to create */ |
| 325 | u16 sub_channel_count; |
| 326 | |
| 327 | /* This will be the maximum of the union members */ |
| 328 | u8 buffer[0x34]; |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 329 | }; |
| 330 | } __packed; |
| 331 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 332 | /* |
K. Y. Srinivasan | 09f0355 | 2012-01-12 12:37:54 -0800 | [diff] [blame] | 333 | * Packet Flags: |
| 334 | * |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 335 | * This flag indicates that the server should send back a completion for this |
| 336 | * packet. |
| 337 | */ |
K. Y. Srinivasan | 09f0355 | 2012-01-12 12:37:54 -0800 | [diff] [blame] | 338 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 339 | #define REQUEST_COMPLETION_FLAG 0x1 |
| 340 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 341 | /* Matches Windows-end */ |
| 342 | enum storvsc_request_type { |
K. Y. Srinivasan | c649114 | 2012-01-12 12:38:03 -0800 | [diff] [blame] | 343 | WRITE_TYPE = 0, |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 344 | READ_TYPE, |
| 345 | UNKNOWN_TYPE, |
| 346 | }; |
| 347 | |
K. Y. Srinivasan | 1604632 | 2012-01-12 12:37:57 -0800 | [diff] [blame] | 348 | /* |
| 349 | * SRB status codes and masks; a subset of the codes used here. |
| 350 | */ |
| 351 | |
| 352 | #define SRB_STATUS_AUTOSENSE_VALID 0x80 |
K. Y. Srinivasan | 3209f9d | 2015-10-06 18:04:01 -0700 | [diff] [blame] | 353 | #define SRB_STATUS_QUEUE_FROZEN 0x40 |
K. Y. Srinivasan | 1604632 | 2012-01-12 12:37:57 -0800 | [diff] [blame] | 354 | #define SRB_STATUS_INVALID_LUN 0x20 |
| 355 | #define SRB_STATUS_SUCCESS 0x01 |
K. Y. Srinivasan | 6781209 | 2013-02-21 12:04:53 -0800 | [diff] [blame] | 356 | #define SRB_STATUS_ABORTED 0x02 |
K. Y. Srinivasan | 1604632 | 2012-01-12 12:37:57 -0800 | [diff] [blame] | 357 | #define SRB_STATUS_ERROR 0x04 |
Long Li | 40630f4 | 2016-12-14 18:46:03 -0800 | [diff] [blame] | 358 | #define SRB_STATUS_DATA_OVERRUN 0x12 |
K. Y. Srinivasan | 1604632 | 2012-01-12 12:37:57 -0800 | [diff] [blame] | 359 | |
K. Y. Srinivasan | 3209f9d | 2015-10-06 18:04:01 -0700 | [diff] [blame] | 360 | #define SRB_STATUS(status) \ |
| 361 | (status & ~(SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_QUEUE_FROZEN)) |
K. Y. Srinivasan | af9584b | 2012-01-12 12:38:08 -0800 | [diff] [blame] | 362 | /* |
| 363 | * This is the end of Protocol specific defines. |
| 364 | */ |
| 365 | |
Michael Kelley | ed2e63a | 2019-04-01 21:42:06 +0000 | [diff] [blame] | 366 | static int storvsc_ringbuffer_size = (128 * 1024); |
K. Y. Srinivasan | f458aad | 2015-03-27 00:27:15 -0700 | [diff] [blame] | 367 | static u32 max_outstanding_req_per_channel; |
Branden Bonaby | adfbd02 | 2019-06-14 19:48:22 -0400 | [diff] [blame] | 368 | static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth); |
K. Y. Srinivasan | f458aad | 2015-03-27 00:27:15 -0700 | [diff] [blame] | 369 | |
| 370 | static int storvsc_vcpus_per_sub_channel = 4; |
Melanie Plageman (Microsoft) | a81a38c | 2021-02-24 23:29:48 +0000 | [diff] [blame] | 371 | static unsigned int storvsc_max_hw_queues; |
K. Y. Srinivasan | af9584b | 2012-01-12 12:38:08 -0800 | [diff] [blame] | 372 | |
| 373 | module_param(storvsc_ringbuffer_size, int, S_IRUGO); |
| 374 | MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)"); |
| 375 | |
Melanie Plageman (Microsoft) | a81a38c | 2021-02-24 23:29:48 +0000 | [diff] [blame] | 376 | module_param(storvsc_max_hw_queues, uint, 0644); |
| 377 | MODULE_PARM_DESC(storvsc_max_hw_queues, "Maximum number of hardware queues"); |
| 378 | |
K. Y. Srinivasan | f458aad | 2015-03-27 00:27:15 -0700 | [diff] [blame] | 379 | module_param(storvsc_vcpus_per_sub_channel, int, S_IRUGO); |
Dan Carpenter | 74e2678 | 2016-01-08 14:02:04 +0300 | [diff] [blame] | 380 | MODULE_PARM_DESC(storvsc_vcpus_per_sub_channel, "Ratio of VCPUs to subchannels"); |
Long Li | 2217a47 | 2018-04-19 14:54:24 -0700 | [diff] [blame] | 381 | |
| 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"); |
| 386 | |
K. Y. Srinivasan | 893def3 | 2013-06-04 12:05:05 -0700 | [diff] [blame] | 387 | /* |
| 388 | * Timeout in seconds for all devices managed by this driver. |
| 389 | */ |
| 390 | static int storvsc_timeout = 180; |
| 391 | |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 392 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) |
| 393 | static struct scsi_transport_template *fc_transport_template; |
| 394 | #endif |
K. Y. Srinivasan | af9584b | 2012-01-12 12:38:08 -0800 | [diff] [blame] | 395 | |
Andres Beltran | 453de21 | 2020-11-09 11:04:01 +0100 | [diff] [blame] | 396 | static struct scsi_host_template scsi_driver; |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 397 | static void storvsc_on_channel_callback(void *context); |
| 398 | |
K. Y. Srinivasan | 4cd83ec | 2014-07-12 09:48:26 -0700 | [diff] [blame] | 399 | #define STORVSC_MAX_LUNS_PER_TARGET 255 |
| 400 | #define STORVSC_MAX_TARGETS 2 |
| 401 | #define STORVSC_MAX_CHANNELS 8 |
K. Y. Srinivasan | af9584b | 2012-01-12 12:38:08 -0800 | [diff] [blame] | 402 | |
K. Y. Srinivasan | 4cd83ec | 2014-07-12 09:48:26 -0700 | [diff] [blame] | 403 | #define STORVSC_FC_MAX_LUNS_PER_TARGET 255 |
| 404 | #define STORVSC_FC_MAX_TARGETS 128 |
| 405 | #define STORVSC_FC_MAX_CHANNELS 8 |
K. Y. Srinivasan | af9584b | 2012-01-12 12:38:08 -0800 | [diff] [blame] | 406 | |
K. Y. Srinivasan | 4cd83ec | 2014-07-12 09:48:26 -0700 | [diff] [blame] | 407 | #define STORVSC_IDE_MAX_LUNS_PER_TARGET 64 |
| 408 | #define STORVSC_IDE_MAX_TARGETS 1 |
| 409 | #define STORVSC_IDE_MAX_CHANNELS 1 |
K. Y. Srinivasan | 1604632 | 2012-01-12 12:37:57 -0800 | [diff] [blame] | 410 | |
Andres Beltran | adae1e9 | 2021-04-08 18:14:39 +0200 | [diff] [blame] | 411 | /* |
| 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 |
| 415 | */ |
| 416 | #define STORVSC_MAX_PKT_SIZE (sizeof(struct vmpacket_descriptor) +\ |
| 417 | sizeof(struct vstor_packet)) |
| 418 | |
K. Y. Srinivasan | 61eaffc | 2012-01-12 12:38:07 -0800 | [diff] [blame] | 419 | struct storvsc_cmd_request { |
K. Y. Srinivasan | 61eaffc | 2012-01-12 12:38:07 -0800 | [diff] [blame] | 420 | struct scsi_cmnd *cmd; |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 421 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 422 | struct hv_device *device; |
| 423 | |
| 424 | /* Synchronize the request/response if needed */ |
| 425 | struct completion wait_event; |
| 426 | |
K. Y. Srinivasan | be0cf6c | 2015-03-27 00:27:20 -0700 | [diff] [blame] | 427 | struct vmbus_channel_packet_multipage_buffer mpb; |
| 428 | struct vmbus_packet_mpb_array *payload; |
| 429 | u32 payload_sz; |
| 430 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 431 | struct vstor_packet vstor_packet; |
| 432 | }; |
| 433 | |
| 434 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 435 | /* A storvsc device is a device object that contains a vmbus channel */ |
| 436 | struct storvsc_device { |
| 437 | struct hv_device *device; |
| 438 | |
| 439 | bool destroy; |
| 440 | bool drain_notify; |
| 441 | atomic_t num_outstanding_req; |
K. Y. Srinivasan | cd654ea | 2011-09-13 10:59:48 -0700 | [diff] [blame] | 442 | struct Scsi_Host *host; |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 443 | |
| 444 | wait_queue_head_t waiting_to_drain; |
| 445 | |
| 446 | /* |
| 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 |
| 450 | */ |
| 451 | unsigned int port_number; |
| 452 | unsigned char path_id; |
| 453 | unsigned char target_id; |
| 454 | |
K. Y. Srinivasan | 5117b93 | 2015-03-27 00:27:17 -0700 | [diff] [blame] | 455 | /* |
Andrea Parri (Microsoft) | 244808e | 2020-12-17 21:33:20 +0100 | [diff] [blame] | 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 |
| 459 | * to a win8 host. |
| 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. |
| 463 | */ |
| 464 | int vmscsi_size_delta; |
| 465 | |
| 466 | /* |
K. Y. Srinivasan | 5117b93 | 2015-03-27 00:27:17 -0700 | [diff] [blame] | 467 | * Max I/O, the device can support. |
| 468 | */ |
| 469 | u32 max_transfer_bytes; |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 470 | /* |
| 471 | * Number of sub-channels we will open. |
| 472 | */ |
| 473 | u16 num_sc; |
| 474 | struct vmbus_channel **stor_chns; |
| 475 | /* |
| 476 | * Mask of CPUs bound to subchannels. |
| 477 | */ |
| 478 | struct cpumask alloced_cpus; |
Andrea Parri (Microsoft) | 21d2052 | 2020-06-17 18:46:41 +0200 | [diff] [blame] | 479 | /* |
| 480 | * Serializes modifications of stor_chns[] from storvsc_do_io() |
| 481 | * and storvsc_change_target_cpu(). |
| 482 | */ |
| 483 | spinlock_t lock; |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 484 | /* Used for vsc/vsp channel reset process */ |
K. Y. Srinivasan | 61eaffc | 2012-01-12 12:38:07 -0800 | [diff] [blame] | 485 | struct storvsc_cmd_request init_request; |
| 486 | struct storvsc_cmd_request reset_request; |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 487 | /* |
| 488 | * Currently active port and node names for FC devices. |
| 489 | */ |
| 490 | u64 node_name; |
| 491 | u64 port_name; |
Cathy Avery | daf0cd4 | 2017-04-17 14:37:46 -0400 | [diff] [blame] | 492 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) |
| 493 | struct fc_rport *rport; |
| 494 | #endif |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 495 | }; |
| 496 | |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 497 | struct hv_host_device { |
| 498 | struct hv_device *dev; |
K. Y. Srinivasan | c1b3d06 | 2011-08-27 11:31:25 -0700 | [diff] [blame] | 499 | unsigned int port; |
| 500 | unsigned char path; |
| 501 | unsigned char target; |
Cathy Avery | 436ad94 | 2017-10-31 08:52:06 -0400 | [diff] [blame] | 502 | struct workqueue_struct *handle_error_wq; |
Long Li | c58cc70 | 2017-10-31 14:58:08 -0700 | [diff] [blame] | 503 | struct work_struct host_scan_work; |
| 504 | struct Scsi_Host *host; |
K. Y. Srinivasan | c1b3d06 | 2011-08-27 11:31:25 -0700 | [diff] [blame] | 505 | }; |
| 506 | |
K. Y. Srinivasan | 12675799 | 2011-11-08 09:01:49 -0800 | [diff] [blame] | 507 | struct storvsc_scan_work { |
| 508 | struct work_struct work; |
| 509 | struct Scsi_Host *host; |
K. Y. Srinivasan | 9844122 | 2016-01-27 18:22:45 -0800 | [diff] [blame] | 510 | u8 lun; |
| 511 | u8 tgt_id; |
K. Y. Srinivasan | 12675799 | 2011-11-08 09:01:49 -0800 | [diff] [blame] | 512 | }; |
| 513 | |
K. Y. Srinivasan | 6781209 | 2013-02-21 12:04:53 -0800 | [diff] [blame] | 514 | static void storvsc_device_scan(struct work_struct *work) |
| 515 | { |
| 516 | struct storvsc_scan_work *wrk; |
K. Y. Srinivasan | 6781209 | 2013-02-21 12:04:53 -0800 | [diff] [blame] | 517 | struct scsi_device *sdev; |
| 518 | |
| 519 | wrk = container_of(work, struct storvsc_scan_work, work); |
K. Y. Srinivasan | 6781209 | 2013-02-21 12:04:53 -0800 | [diff] [blame] | 520 | |
K. Y. Srinivasan | 9844122 | 2016-01-27 18:22:45 -0800 | [diff] [blame] | 521 | sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun); |
K. Y. Srinivasan | 6781209 | 2013-02-21 12:04:53 -0800 | [diff] [blame] | 522 | if (!sdev) |
| 523 | goto done; |
| 524 | scsi_rescan_device(&sdev->sdev_gendev); |
| 525 | scsi_device_put(sdev); |
| 526 | |
| 527 | done: |
| 528 | kfree(wrk); |
| 529 | } |
| 530 | |
K. Y. Srinivasan | 2a09ed3 | 2014-12-16 13:21:42 -0800 | [diff] [blame] | 531 | static void storvsc_host_scan(struct work_struct *work) |
K. Y. Srinivasan | 12675799 | 2011-11-08 09:01:49 -0800 | [diff] [blame] | 532 | { |
K. Y. Srinivasan | 2a09ed3 | 2014-12-16 13:21:42 -0800 | [diff] [blame] | 533 | struct Scsi_Host *host; |
K. Y. Srinivasan | 34a716b | 2014-12-16 13:21:43 -0800 | [diff] [blame] | 534 | struct scsi_device *sdev; |
Long Li | c58cc70 | 2017-10-31 14:58:08 -0700 | [diff] [blame] | 535 | struct hv_host_device *host_device = |
| 536 | container_of(work, struct hv_host_device, host_scan_work); |
K. Y. Srinivasan | 12675799 | 2011-11-08 09:01:49 -0800 | [diff] [blame] | 537 | |
Long Li | c58cc70 | 2017-10-31 14:58:08 -0700 | [diff] [blame] | 538 | host = host_device->host; |
K. Y. Srinivasan | 34a716b | 2014-12-16 13:21:43 -0800 | [diff] [blame] | 539 | /* |
| 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. |
| 545 | * |
| 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. |
| 549 | */ |
| 550 | mutex_lock(&host->scan_mutex); |
Vitaly Kuznetsov | 8d6a9f5 | 2015-07-01 11:31:27 +0200 | [diff] [blame] | 551 | shost_for_each_device(sdev, host) |
K. Y. Srinivasan | 34a716b | 2014-12-16 13:21:43 -0800 | [diff] [blame] | 552 | scsi_test_unit_ready(sdev, 1, 1, NULL); |
K. Y. Srinivasan | 34a716b | 2014-12-16 13:21:43 -0800 | [diff] [blame] | 553 | mutex_unlock(&host->scan_mutex); |
| 554 | /* |
| 555 | * Now scan the host to discover LUNs that may have been added. |
| 556 | */ |
K. Y. Srinivasan | 2a09ed3 | 2014-12-16 13:21:42 -0800 | [diff] [blame] | 557 | scsi_scan_host(host); |
K. Y. Srinivasan | 12675799 | 2011-11-08 09:01:49 -0800 | [diff] [blame] | 558 | } |
| 559 | |
K. Y. Srinivasan | b401731 | 2011-11-08 09:01:50 -0800 | [diff] [blame] | 560 | static void storvsc_remove_lun(struct work_struct *work) |
| 561 | { |
| 562 | struct storvsc_scan_work *wrk; |
| 563 | struct scsi_device *sdev; |
| 564 | |
| 565 | wrk = container_of(work, struct storvsc_scan_work, work); |
| 566 | if (!scsi_host_get(wrk->host)) |
| 567 | goto done; |
| 568 | |
K. Y. Srinivasan | 9844122 | 2016-01-27 18:22:45 -0800 | [diff] [blame] | 569 | sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun); |
K. Y. Srinivasan | b401731 | 2011-11-08 09:01:50 -0800 | [diff] [blame] | 570 | |
| 571 | if (sdev) { |
| 572 | scsi_remove_device(sdev); |
| 573 | scsi_device_put(sdev); |
| 574 | } |
| 575 | scsi_host_put(wrk->host); |
| 576 | |
| 577 | done: |
| 578 | kfree(wrk); |
| 579 | } |
| 580 | |
K. Y. Srinivasan | af9584b | 2012-01-12 12:38:08 -0800 | [diff] [blame] | 581 | |
| 582 | /* |
K. Y. Srinivasan | a8c18c5 | 2012-01-12 12:38:00 -0800 | [diff] [blame] | 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 |
| 589 | * consistency: |
| 590 | * |
| 591 | * 1) Once the device is marked as being destroyed, we will fail all |
| 592 | * outgoing messages. |
| 593 | * 2) We permit incoming messages when the device is being destroyed, |
| 594 | * only to properly account for messages already sent out. |
| 595 | */ |
| 596 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 597 | static inline struct storvsc_device *get_out_stor_device( |
| 598 | struct hv_device *device) |
| 599 | { |
| 600 | struct storvsc_device *stor_device; |
| 601 | |
K. Y. Srinivasan | cd654ea | 2011-09-13 10:59:48 -0700 | [diff] [blame] | 602 | stor_device = hv_get_drvdata(device); |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 603 | |
| 604 | if (stor_device && stor_device->destroy) |
| 605 | stor_device = NULL; |
| 606 | |
| 607 | return stor_device; |
| 608 | } |
| 609 | |
| 610 | |
| 611 | static inline void storvsc_wait_to_drain(struct storvsc_device *dev) |
| 612 | { |
| 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; |
| 617 | } |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 618 | |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 619 | static inline struct storvsc_device *get_in_stor_device( |
| 620 | struct hv_device *device) |
| 621 | { |
| 622 | struct storvsc_device *stor_device; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 623 | |
K. Y. Srinivasan | cd654ea | 2011-09-13 10:59:48 -0700 | [diff] [blame] | 624 | stor_device = hv_get_drvdata(device); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 625 | |
| 626 | if (!stor_device) |
| 627 | goto get_in_err; |
| 628 | |
| 629 | /* |
| 630 | * If the device is being destroyed; allow incoming |
| 631 | * traffic only to cleanup outstanding requests. |
| 632 | */ |
| 633 | |
| 634 | if (stor_device->destroy && |
| 635 | (atomic_read(&stor_device->num_outstanding_req) == 0)) |
| 636 | stor_device = NULL; |
| 637 | |
| 638 | get_in_err: |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 639 | return stor_device; |
| 640 | |
| 641 | } |
| 642 | |
Andrea Parri (Microsoft) | 7769e18 | 2020-04-06 02:15:14 +0200 | [diff] [blame] | 643 | static void storvsc_change_target_cpu(struct vmbus_channel *channel, u32 old, |
| 644 | u32 new) |
| 645 | { |
| 646 | struct storvsc_device *stor_device; |
| 647 | struct vmbus_channel *cur_chn; |
| 648 | bool old_is_alloced = false; |
| 649 | struct hv_device *device; |
| 650 | unsigned long flags; |
| 651 | int cpu; |
| 652 | |
| 653 | device = channel->primary_channel ? |
| 654 | channel->primary_channel->device_obj |
| 655 | : channel->device_obj; |
| 656 | stor_device = get_out_stor_device(device); |
| 657 | if (!stor_device) |
| 658 | return; |
| 659 | |
| 660 | /* See storvsc_do_io() -> get_og_chn(). */ |
Andrea Parri (Microsoft) | 21d2052 | 2020-06-17 18:46:41 +0200 | [diff] [blame] | 661 | spin_lock_irqsave(&stor_device->lock, flags); |
Andrea Parri (Microsoft) | 7769e18 | 2020-04-06 02:15:14 +0200 | [diff] [blame] | 662 | |
| 663 | /* |
| 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 |
| 666 | * array. |
| 667 | */ |
| 668 | if (device->channel != channel && device->channel->target_cpu == old) { |
| 669 | cur_chn = device->channel; |
| 670 | old_is_alloced = true; |
| 671 | goto old_is_alloced; |
| 672 | } |
| 673 | list_for_each_entry(cur_chn, &device->channel->sc_list, sc_list) { |
| 674 | if (cur_chn == channel) |
| 675 | continue; |
| 676 | if (cur_chn->target_cpu == old) { |
| 677 | old_is_alloced = true; |
| 678 | goto old_is_alloced; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | old_is_alloced: |
| 683 | if (old_is_alloced) |
| 684 | WRITE_ONCE(stor_device->stor_chns[old], cur_chn); |
| 685 | else |
| 686 | cpumask_clear_cpu(old, &stor_device->alloced_cpus); |
| 687 | |
| 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); |
| 693 | } |
| 694 | |
| 695 | WRITE_ONCE(stor_device->stor_chns[new], channel); |
| 696 | cpumask_set_cpu(new, &stor_device->alloced_cpus); |
| 697 | |
Andrea Parri (Microsoft) | 21d2052 | 2020-06-17 18:46:41 +0200 | [diff] [blame] | 698 | spin_unlock_irqrestore(&stor_device->lock, flags); |
Andrea Parri (Microsoft) | 7769e18 | 2020-04-06 02:15:14 +0200 | [diff] [blame] | 699 | } |
| 700 | |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 701 | static u64 storvsc_next_request_id(struct vmbus_channel *channel, u64 rqst_addr) |
| 702 | { |
| 703 | struct storvsc_cmd_request *request = |
| 704 | (struct storvsc_cmd_request *)(unsigned long)rqst_addr; |
| 705 | |
| 706 | if (rqst_addr == VMBUS_RQST_INIT) |
| 707 | return VMBUS_RQST_INIT; |
| 708 | if (rqst_addr == VMBUS_RQST_RESET) |
| 709 | return VMBUS_RQST_RESET; |
| 710 | |
| 711 | /* |
| 712 | * Cannot return an ID of 0, which is reserved for an unsolicited |
| 713 | * message from Hyper-V. |
| 714 | */ |
Martin K. Petersen | c5bf198 | 2021-08-10 00:52:41 -0400 | [diff] [blame] | 715 | return (u64)blk_mq_unique_tag(scsi_cmd_to_rq(request->cmd)) + 1; |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 716 | } |
| 717 | |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 718 | static void handle_sc_creation(struct vmbus_channel *new_sc) |
| 719 | { |
| 720 | struct hv_device *device = new_sc->primary_channel->device_obj; |
Dexuan Cui | c967590 | 2018-11-26 00:26:17 +0000 | [diff] [blame] | 721 | struct device *dev = &device->device; |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 722 | struct storvsc_device *stor_device; |
| 723 | struct vmstorage_channel_properties props; |
Dexuan Cui | c967590 | 2018-11-26 00:26:17 +0000 | [diff] [blame] | 724 | int ret; |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 725 | |
| 726 | stor_device = get_out_stor_device(device); |
| 727 | if (!stor_device) |
| 728 | return; |
| 729 | |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 730 | memset(&props, 0, sizeof(struct vmstorage_channel_properties)); |
Andres Beltran | adae1e9 | 2021-04-08 18:14:39 +0200 | [diff] [blame] | 731 | new_sc->max_pkt_size = STORVSC_MAX_PKT_SIZE; |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 732 | |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 733 | new_sc->next_request_id_callback = storvsc_next_request_id; |
Andres Beltran | 453de21 | 2020-11-09 11:04:01 +0100 | [diff] [blame] | 734 | |
Dexuan Cui | c967590 | 2018-11-26 00:26:17 +0000 | [diff] [blame] | 735 | ret = vmbus_open(new_sc, |
| 736 | storvsc_ringbuffer_size, |
| 737 | storvsc_ringbuffer_size, |
| 738 | (void *)&props, |
| 739 | sizeof(struct vmstorage_channel_properties), |
| 740 | storvsc_on_channel_callback, new_sc); |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 741 | |
Dexuan Cui | c967590 | 2018-11-26 00:26:17 +0000 | [diff] [blame] | 742 | /* In case vmbus_open() fails, we don't use the sub-channel. */ |
| 743 | if (ret != 0) { |
| 744 | dev_err(dev, "Failed to open sub-channel: err=%d\n", ret); |
| 745 | return; |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 746 | } |
Dexuan Cui | c967590 | 2018-11-26 00:26:17 +0000 | [diff] [blame] | 747 | |
Andrea Parri (Microsoft) | 7769e18 | 2020-04-06 02:15:14 +0200 | [diff] [blame] | 748 | new_sc->change_target_cpu_callback = storvsc_change_target_cpu; |
| 749 | |
Dexuan Cui | c967590 | 2018-11-26 00:26:17 +0000 | [diff] [blame] | 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); |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | static void handle_multichannel_storage(struct hv_device *device, int max_chns) |
| 756 | { |
Dexuan Cui | c967590 | 2018-11-26 00:26:17 +0000 | [diff] [blame] | 757 | struct device *dev = &device->device; |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 758 | struct storvsc_device *stor_device; |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 759 | int num_sc; |
| 760 | struct storvsc_cmd_request *request; |
| 761 | struct vstor_packet *vstor_packet; |
| 762 | int ret, t; |
| 763 | |
Michael Kelley | 382e06d | 2019-04-01 16:10:52 +0000 | [diff] [blame] | 764 | /* |
| 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. |
| 770 | */ |
| 771 | num_sc = min((int)(num_online_cpus() - 1), max_chns); |
| 772 | if (!num_sc) |
| 773 | return; |
| 774 | |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 775 | stor_device = get_out_stor_device(device); |
| 776 | if (!stor_device) |
| 777 | return; |
| 778 | |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 779 | stor_device->num_sc = num_sc; |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 780 | request = &stor_device->init_request; |
| 781 | vstor_packet = &request->vstor_packet; |
| 782 | |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 783 | /* |
| 784 | * Establish a handler for dealing with subchannels. |
| 785 | */ |
| 786 | vmbus_set_sc_create_callback(device->channel, handle_sc_creation); |
| 787 | |
| 788 | /* |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 789 | * Request the host to create sub-channels. |
| 790 | */ |
| 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; |
| 796 | |
| 797 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
| 798 | (sizeof(struct vstor_packet) - |
Andrea Parri (Microsoft) | 244808e | 2020-12-17 21:33:20 +0100 | [diff] [blame] | 799 | stor_device->vmscsi_size_delta), |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 800 | VMBUS_RQST_INIT, |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 801 | VM_PKT_DATA_INBAND, |
| 802 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 803 | |
Dexuan Cui | c967590 | 2018-11-26 00:26:17 +0000 | [diff] [blame] | 804 | if (ret != 0) { |
| 805 | dev_err(dev, "Failed to create sub-channel: err=%d\n", ret); |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 806 | return; |
Dexuan Cui | c967590 | 2018-11-26 00:26:17 +0000 | [diff] [blame] | 807 | } |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 808 | |
| 809 | t = wait_for_completion_timeout(&request->wait_event, 10*HZ); |
Dexuan Cui | c967590 | 2018-11-26 00:26:17 +0000 | [diff] [blame] | 810 | if (t == 0) { |
| 811 | dev_err(dev, "Failed to create sub-channel: timed out\n"); |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 812 | return; |
Dexuan Cui | c967590 | 2018-11-26 00:26:17 +0000 | [diff] [blame] | 813 | } |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 814 | |
| 815 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
Dexuan Cui | c967590 | 2018-11-26 00:26:17 +0000 | [diff] [blame] | 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); |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 819 | return; |
Dexuan Cui | c967590 | 2018-11-26 00:26:17 +0000 | [diff] [blame] | 820 | } |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 821 | |
| 822 | /* |
Dexuan Cui | c967590 | 2018-11-26 00:26:17 +0000 | [diff] [blame] | 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). |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 826 | */ |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 827 | } |
| 828 | |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 829 | static void cache_wwn(struct storvsc_device *stor_device, |
| 830 | struct vstor_packet *vstor_packet) |
| 831 | { |
| 832 | /* |
| 833 | * Cache the currently active port and node ww names. |
| 834 | */ |
| 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); |
| 840 | } else { |
| 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); |
| 845 | } |
| 846 | } |
| 847 | |
K. Y. Srinivasan | 5963501 | 2015-12-23 13:15:50 -0800 | [diff] [blame] | 848 | |
| 849 | static int storvsc_execute_vstor_op(struct hv_device *device, |
| 850 | struct storvsc_cmd_request *request, |
| 851 | bool status_check) |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 852 | { |
Andrea Parri (Microsoft) | 244808e | 2020-12-17 21:33:20 +0100 | [diff] [blame] | 853 | struct storvsc_device *stor_device; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 854 | struct vstor_packet *vstor_packet; |
K. Y. Srinivasan | 5963501 | 2015-12-23 13:15:50 -0800 | [diff] [blame] | 855 | int ret, t; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 856 | |
Andrea Parri (Microsoft) | 244808e | 2020-12-17 21:33:20 +0100 | [diff] [blame] | 857 | stor_device = get_out_stor_device(device); |
| 858 | if (!stor_device) |
| 859 | return -ENODEV; |
| 860 | |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 861 | vstor_packet = &request->vstor_packet; |
| 862 | |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 863 | init_completion(&request->wait_event); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 864 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; |
| 865 | |
| 866 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 867 | (sizeof(struct vstor_packet) - |
Andrea Parri (Microsoft) | 244808e | 2020-12-17 21:33:20 +0100 | [diff] [blame] | 868 | stor_device->vmscsi_size_delta), |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 869 | VMBUS_RQST_INIT, |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 870 | VM_PKT_DATA_INBAND, |
| 871 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 872 | if (ret != 0) |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 873 | return ret; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 874 | |
| 875 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 876 | if (t == 0) |
| 877 | return -ETIMEDOUT; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 878 | |
K. Y. Srinivasan | 5963501 | 2015-12-23 13:15:50 -0800 | [diff] [blame] | 879 | if (!status_check) |
| 880 | return ret; |
| 881 | |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 882 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 883 | vstor_packet->status != 0) |
| 884 | return -EINVAL; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 885 | |
K. Y. Srinivasan | 5963501 | 2015-12-23 13:15:50 -0800 | [diff] [blame] | 886 | return ret; |
| 887 | } |
| 888 | |
| 889 | static int storvsc_channel_init(struct hv_device *device, bool is_fc) |
| 890 | { |
| 891 | struct storvsc_device *stor_device; |
| 892 | struct storvsc_cmd_request *request; |
| 893 | struct vstor_packet *vstor_packet; |
| 894 | int ret, i; |
| 895 | int max_chns; |
| 896 | bool process_sub_channels = false; |
| 897 | |
| 898 | stor_device = get_out_stor_device(device); |
| 899 | if (!stor_device) |
| 900 | return -ENODEV; |
| 901 | |
| 902 | request = &stor_device->init_request; |
| 903 | vstor_packet = &request->vstor_packet; |
| 904 | |
| 905 | /* |
| 906 | * Now, initiate the vsc/vsp initialization protocol on the open |
| 907 | * channel |
| 908 | */ |
| 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); |
| 912 | if (ret) |
| 913 | return ret; |
| 914 | /* |
| 915 | * Query host supported protocol version. |
| 916 | */ |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 917 | |
Keith Mange | 1a36310 | 2015-08-13 08:43:48 -0700 | [diff] [blame] | 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; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 923 | |
Keith Mange | 1a36310 | 2015-08-13 08:43:48 -0700 | [diff] [blame] | 924 | vstor_packet->version.major_minor = |
| 925 | vmstor_protocols[i].protocol_version; |
K. Y. Srinivasan | 85904a5 | 2012-01-12 12:38:04 -0800 | [diff] [blame] | 926 | |
Keith Mange | 1a36310 | 2015-08-13 08:43:48 -0700 | [diff] [blame] | 927 | /* |
| 928 | * The revision number is only used in Windows; set it to 0. |
| 929 | */ |
| 930 | vstor_packet->version.revision = 0; |
K. Y. Srinivasan | 5963501 | 2015-12-23 13:15:50 -0800 | [diff] [blame] | 931 | ret = storvsc_execute_vstor_op(device, request, false); |
Keith Mange | 1a36310 | 2015-08-13 08:43:48 -0700 | [diff] [blame] | 932 | if (ret != 0) |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 933 | return ret; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 934 | |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 935 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO) |
| 936 | return -EINVAL; |
Keith Mange | 1a36310 | 2015-08-13 08:43:48 -0700 | [diff] [blame] | 937 | |
| 938 | if (vstor_packet->status == 0) { |
| 939 | vmstor_proto_version = |
| 940 | vmstor_protocols[i].protocol_version; |
| 941 | |
| 942 | sense_buffer_size = |
| 943 | vmstor_protocols[i].sense_buffer_size; |
| 944 | |
Andrea Parri (Microsoft) | 244808e | 2020-12-17 21:33:20 +0100 | [diff] [blame] | 945 | stor_device->vmscsi_size_delta = |
Keith Mange | 1a36310 | 2015-08-13 08:43:48 -0700 | [diff] [blame] | 946 | vmstor_protocols[i].vmscsi_size_delta; |
| 947 | |
| 948 | break; |
| 949 | } |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 950 | } |
| 951 | |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 952 | if (vstor_packet->status != 0) |
| 953 | return -EINVAL; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 954 | |
| 955 | |
| 956 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
| 957 | vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES; |
K. Y. Srinivasan | 5963501 | 2015-12-23 13:15:50 -0800 | [diff] [blame] | 958 | ret = storvsc_execute_vstor_op(device, request, true); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 959 | if (ret != 0) |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 960 | return ret; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 961 | |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 962 | /* |
| 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. |
| 966 | */ |
| 967 | max_chns = vstor_packet->storage_channel_properties.max_channel_cnt; |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 968 | |
| 969 | /* |
| 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 |
| 976 | * the load. |
| 977 | */ |
Miguel Bernal Marin | e040852 | 2017-03-16 00:58:23 -0600 | [diff] [blame] | 978 | stor_device->stor_chns = kcalloc(num_possible_cpus(), sizeof(void *), |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 979 | GFP_KERNEL); |
| 980 | if (stor_device->stor_chns == NULL) |
| 981 | return -ENOMEM; |
| 982 | |
Andrea Parri (Microsoft) | 7769e18 | 2020-04-06 02:15:14 +0200 | [diff] [blame] | 983 | device->channel->change_target_cpu_callback = storvsc_change_target_cpu; |
| 984 | |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 985 | stor_device->stor_chns[device->channel->target_cpu] = device->channel; |
| 986 | cpumask_set_cpu(device->channel->target_cpu, |
| 987 | &stor_device->alloced_cpus); |
| 988 | |
Keith Mange | e6c4bc6 | 2015-08-13 08:43:50 -0700 | [diff] [blame] | 989 | if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN8) { |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 990 | if (vstor_packet->storage_channel_properties.flags & |
| 991 | STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL) |
| 992 | process_sub_channels = true; |
| 993 | } |
K. Y. Srinivasan | 5117b93 | 2015-03-27 00:27:17 -0700 | [diff] [blame] | 994 | stor_device->max_transfer_bytes = |
| 995 | vstor_packet->storage_channel_properties.max_transfer_bytes; |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 996 | |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 997 | if (!is_fc) |
| 998 | goto done; |
| 999 | |
K. Y. Srinivasan | 5963501 | 2015-12-23 13:15:50 -0800 | [diff] [blame] | 1000 | /* |
| 1001 | * For FC devices retrieve FC HBA data. |
| 1002 | */ |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 1003 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
| 1004 | vstor_packet->operation = VSTOR_OPERATION_FCHBA_DATA; |
K. Y. Srinivasan | 5963501 | 2015-12-23 13:15:50 -0800 | [diff] [blame] | 1005 | ret = storvsc_execute_vstor_op(device, request, true); |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 1006 | if (ret != 0) |
| 1007 | return ret; |
| 1008 | |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 1009 | /* |
| 1010 | * Cache the currently active port and node ww names. |
| 1011 | */ |
| 1012 | cache_wwn(stor_device, vstor_packet); |
| 1013 | |
| 1014 | done: |
| 1015 | |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1016 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
| 1017 | vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION; |
K. Y. Srinivasan | 5963501 | 2015-12-23 13:15:50 -0800 | [diff] [blame] | 1018 | ret = storvsc_execute_vstor_op(device, request, true); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1019 | if (ret != 0) |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 1020 | return ret; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1021 | |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 1022 | if (process_sub_channels) |
| 1023 | handle_multichannel_storage(device, max_chns); |
| 1024 | |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1025 | return ret; |
| 1026 | } |
| 1027 | |
K. Y. Srinivasan | c50bd44 | 2013-02-21 12:04:52 -0800 | [diff] [blame] | 1028 | static void storvsc_handle_error(struct vmscsi_request *vm_srb, |
| 1029 | struct scsi_cmnd *scmnd, |
| 1030 | struct Scsi_Host *host, |
| 1031 | u8 asc, u8 ascq) |
| 1032 | { |
| 1033 | struct storvsc_scan_work *wrk; |
| 1034 | void (*process_err_fn)(struct work_struct *work); |
Cathy Avery | 436ad94 | 2017-10-31 08:52:06 -0400 | [diff] [blame] | 1035 | struct hv_host_device *host_dev = shost_priv(host); |
K. Y. Srinivasan | c50bd44 | 2013-02-21 12:04:52 -0800 | [diff] [blame] | 1036 | |
Michael Kelley | 52e1b3b | 2021-06-04 10:21:03 -0700 | [diff] [blame] | 1037 | /* |
| 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. |
| 1041 | */ |
| 1042 | |
| 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; |
| 1046 | goto do_work; |
| 1047 | } |
| 1048 | |
| 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; |
| 1054 | /* |
| 1055 | * Retry the I/O that triggered this. |
| 1056 | */ |
| 1057 | set_host_byte(scmnd, DID_REQUEUE); |
| 1058 | goto do_work; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | if (vm_srb->srb_status & SRB_STATUS_ERROR) { |
K. Y. Srinivasan | c50bd44 | 2013-02-21 12:04:52 -0800 | [diff] [blame] | 1063 | /* |
Long Li | bba5dc3 | 2016-12-14 18:46:02 -0800 | [diff] [blame] | 1064 | * Let upper layer deal with error when |
| 1065 | * sense message is present. |
| 1066 | */ |
Long Li | bba5dc3 | 2016-12-14 18:46:02 -0800 | [diff] [blame] | 1067 | if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID) |
Michael Kelley | 52e1b3b | 2021-06-04 10:21:03 -0700 | [diff] [blame] | 1068 | return; |
| 1069 | |
Long Li | bba5dc3 | 2016-12-14 18:46:02 -0800 | [diff] [blame] | 1070 | /* |
K. Y. Srinivasan | c50bd44 | 2013-02-21 12:04:52 -0800 | [diff] [blame] | 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. |
| 1075 | */ |
| 1076 | switch (scmnd->cmnd[0]) { |
| 1077 | case ATA_16: |
| 1078 | case ATA_12: |
| 1079 | set_host_byte(scmnd, DID_PASSTHROUGH); |
| 1080 | break; |
K. Y. Srinivasan | 3533f860 | 2014-07-12 09:48:32 -0700 | [diff] [blame] | 1081 | /* |
Michael Kelley | 52e1b3b | 2021-06-04 10:21:03 -0700 | [diff] [blame] | 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. |
K. Y. Srinivasan | 3533f860 | 2014-07-12 09:48:32 -0700 | [diff] [blame] | 1085 | */ |
| 1086 | case TEST_UNIT_READY: |
| 1087 | break; |
K. Y. Srinivasan | c50bd44 | 2013-02-21 12:04:52 -0800 | [diff] [blame] | 1088 | default: |
Cathy Avery | d1b8b23 | 2017-12-19 13:32:48 -0500 | [diff] [blame] | 1089 | set_host_byte(scmnd, DID_ERROR); |
K. Y. Srinivasan | c50bd44 | 2013-02-21 12:04:52 -0800 | [diff] [blame] | 1090 | } |
K. Y. Srinivasan | c50bd44 | 2013-02-21 12:04:52 -0800 | [diff] [blame] | 1091 | } |
Michael Kelley | 52e1b3b | 2021-06-04 10:21:03 -0700 | [diff] [blame] | 1092 | return; |
K. Y. Srinivasan | 6781209 | 2013-02-21 12:04:53 -0800 | [diff] [blame] | 1093 | |
Michael Kelley | 52e1b3b | 2021-06-04 10:21:03 -0700 | [diff] [blame] | 1094 | do_work: |
K. Y. Srinivasan | c50bd44 | 2013-02-21 12:04:52 -0800 | [diff] [blame] | 1095 | /* |
| 1096 | * We need to schedule work to process this error; schedule it. |
| 1097 | */ |
| 1098 | wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC); |
| 1099 | if (!wrk) { |
| 1100 | set_host_byte(scmnd, DID_TARGET_FAILURE); |
| 1101 | return; |
| 1102 | } |
| 1103 | |
| 1104 | wrk->host = host; |
| 1105 | wrk->lun = vm_srb->lun; |
K. Y. Srinivasan | 9844122 | 2016-01-27 18:22:45 -0800 | [diff] [blame] | 1106 | wrk->tgt_id = vm_srb->target_id; |
K. Y. Srinivasan | c50bd44 | 2013-02-21 12:04:52 -0800 | [diff] [blame] | 1107 | INIT_WORK(&wrk->work, process_err_fn); |
Cathy Avery | 436ad94 | 2017-10-31 08:52:06 -0400 | [diff] [blame] | 1108 | queue_work(host_dev->handle_error_wq, &wrk->work); |
K. Y. Srinivasan | c50bd44 | 2013-02-21 12:04:52 -0800 | [diff] [blame] | 1109 | } |
| 1110 | |
K. Y. Srinivasan | 2707388c | 2012-01-12 12:38:01 -0800 | [diff] [blame] | 1111 | |
K. Y. Srinivasan | 03996f2 | 2015-12-23 13:15:51 -0800 | [diff] [blame] | 1112 | static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request, |
| 1113 | struct storvsc_device *stor_dev) |
K. Y. Srinivasan | 2707388c | 2012-01-12 12:38:01 -0800 | [diff] [blame] | 1114 | { |
K. Y. Srinivasan | 2707388c | 2012-01-12 12:38:01 -0800 | [diff] [blame] | 1115 | struct scsi_cmnd *scmnd = cmd_request->cmd; |
K. Y. Srinivasan | 2707388c | 2012-01-12 12:38:01 -0800 | [diff] [blame] | 1116 | struct scsi_sense_hdr sense_hdr; |
| 1117 | struct vmscsi_request *vm_srb; |
Long Li | 40630f4 | 2016-12-14 18:46:03 -0800 | [diff] [blame] | 1118 | u32 data_transfer_length; |
K. Y. Srinivasan | c50bd44 | 2013-02-21 12:04:52 -0800 | [diff] [blame] | 1119 | struct Scsi_Host *host; |
K. Y. Srinivasan | be0cf6c | 2015-03-27 00:27:20 -0700 | [diff] [blame] | 1120 | u32 payload_sz = cmd_request->payload_sz; |
| 1121 | void *payload = cmd_request->payload; |
Michael Kelley | 08f7654 | 2021-06-04 10:21:02 -0700 | [diff] [blame] | 1122 | bool sense_ok; |
K. Y. Srinivasan | c50bd44 | 2013-02-21 12:04:52 -0800 | [diff] [blame] | 1123 | |
K. Y. Srinivasan | c50bd44 | 2013-02-21 12:04:52 -0800 | [diff] [blame] | 1124 | host = stor_dev->host; |
K. Y. Srinivasan | 2707388c | 2012-01-12 12:38:01 -0800 | [diff] [blame] | 1125 | |
K. Y. Srinivasan | 61eaffc | 2012-01-12 12:38:07 -0800 | [diff] [blame] | 1126 | vm_srb = &cmd_request->vstor_packet.vm_srb; |
Long Li | 40630f4 | 2016-12-14 18:46:03 -0800 | [diff] [blame] | 1127 | data_transfer_length = vm_srb->data_transfer_length; |
K. Y. Srinivasan | 2707388c | 2012-01-12 12:38:01 -0800 | [diff] [blame] | 1128 | |
K. Y. Srinivasan | 42e22ca | 2012-04-05 12:26:52 -0700 | [diff] [blame] | 1129 | scmnd->result = vm_srb->scsi_status; |
| 1130 | |
K. Y. Srinivasan | 2707388c | 2012-01-12 12:38:01 -0800 | [diff] [blame] | 1131 | if (scmnd->result) { |
Michael Kelley | 08f7654 | 2021-06-04 10:21:02 -0700 | [diff] [blame] | 1132 | sense_ok = scsi_normalize_sense(scmnd->sense_buffer, |
| 1133 | SCSI_SENSE_BUFFERSIZE, &sense_hdr); |
| 1134 | |
| 1135 | if (sense_ok && do_logging(STORVSC_LOGGING_WARN)) |
Hannes Reinecke | d811b84 | 2014-10-24 14:26:45 +0200 | [diff] [blame] | 1136 | scsi_print_sense_hdr(scmnd->device, "storvsc", |
| 1137 | &sense_hdr); |
K. Y. Srinivasan | 2707388c | 2012-01-12 12:38:01 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
Long Li | 40630f4 | 2016-12-14 18:46:03 -0800 | [diff] [blame] | 1140 | if (vm_srb->srb_status != SRB_STATUS_SUCCESS) { |
K. Y. Srinivasan | c50bd44 | 2013-02-21 12:04:52 -0800 | [diff] [blame] | 1141 | storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc, |
| 1142 | sense_hdr.ascq); |
Long Li | 40630f4 | 2016-12-14 18:46:03 -0800 | [diff] [blame] | 1143 | /* |
| 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. |
| 1147 | */ |
| 1148 | if (vm_srb->srb_status != SRB_STATUS_DATA_OVERRUN) |
| 1149 | data_transfer_length = 0; |
| 1150 | } |
K. Y. Srinivasan | c50bd44 | 2013-02-21 12:04:52 -0800 | [diff] [blame] | 1151 | |
Andres Beltran | 0a76566 | 2020-07-06 12:09:28 -0400 | [diff] [blame] | 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; |
| 1155 | |
K. Y. Srinivasan | 2707388c | 2012-01-12 12:38:01 -0800 | [diff] [blame] | 1156 | scsi_set_resid(scmnd, |
Long Li | 40630f4 | 2016-12-14 18:46:03 -0800 | [diff] [blame] | 1157 | cmd_request->payload->range.len - data_transfer_length); |
K. Y. Srinivasan | 2707388c | 2012-01-12 12:38:01 -0800 | [diff] [blame] | 1158 | |
Bart Van Assche | 0c31fa0 | 2021-10-07 13:29:08 -0700 | [diff] [blame] | 1159 | scsi_done(scmnd); |
K. Y. Srinivasan | be0cf6c | 2015-03-27 00:27:20 -0700 | [diff] [blame] | 1160 | |
| 1161 | if (payload_sz > |
| 1162 | sizeof(struct vmbus_channel_packet_multipage_buffer)) |
| 1163 | kfree(payload); |
K. Y. Srinivasan | 2707388c | 2012-01-12 12:38:01 -0800 | [diff] [blame] | 1164 | } |
| 1165 | |
K. Y. Srinivasan | 03996f2 | 2015-12-23 13:15:51 -0800 | [diff] [blame] | 1166 | static void storvsc_on_io_completion(struct storvsc_device *stor_device, |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1167 | struct vstor_packet *vstor_packet, |
K. Y. Srinivasan | 61eaffc | 2012-01-12 12:38:07 -0800 | [diff] [blame] | 1168 | struct storvsc_cmd_request *request) |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1169 | { |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1170 | struct vstor_packet *stor_pkt; |
K. Y. Srinivasan | 03996f2 | 2015-12-23 13:15:51 -0800 | [diff] [blame] | 1171 | struct hv_device *device = stor_device->device; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1172 | |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1173 | stor_pkt = &request->vstor_packet; |
| 1174 | |
K. Y. Srinivasan | 4ed51a2 | 2011-08-27 11:31:26 -0700 | [diff] [blame] | 1175 | /* |
| 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 |
| 1180 | * |
| 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. |
| 1184 | */ |
| 1185 | |
| 1186 | if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) || |
K. Y. Srinivasan | a8c18c5 | 2012-01-12 12:38:00 -0800 | [diff] [blame] | 1187 | (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) { |
K. Y. Srinivasan | 4ed51a2 | 2011-08-27 11:31:26 -0700 | [diff] [blame] | 1188 | vstor_packet->vm_srb.scsi_status = 0; |
K. Y. Srinivasan | 1604632 | 2012-01-12 12:37:57 -0800 | [diff] [blame] | 1189 | vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS; |
K. Y. Srinivasan | 4ed51a2 | 2011-08-27 11:31:26 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 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; |
Andres Beltran | 0a76566 | 2020-07-06 12:09:28 -0400 | [diff] [blame] | 1195 | |
Michael Kelley | d467485 | 2021-06-04 10:21:01 -0700 | [diff] [blame] | 1196 | /* |
| 1197 | * Copy over the sense_info_length, but limit to the known max |
| 1198 | * size if Hyper-V returns a bad value. |
| 1199 | */ |
| 1200 | stor_pkt->vm_srb.sense_info_length = min_t(u8, sense_buffer_size, |
| 1201 | vstor_packet->vm_srb.sense_info_length); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1202 | |
Long Li | f8aea70 | 2015-12-04 00:07:24 -0800 | [diff] [blame] | 1203 | if (vstor_packet->vm_srb.scsi_status != 0 || |
Michael Kelley | dbe7633 | 2021-08-06 10:12:50 -0700 | [diff] [blame] | 1204 | vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS) { |
| 1205 | |
| 1206 | /* |
| 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. |
| 1210 | */ |
| 1211 | int loglevel = (stor_pkt->vm_srb.cdb[0] == TEST_UNIT_READY) ? |
| 1212 | STORVSC_LOGGING_WARN : STORVSC_LOGGING_ERROR; |
| 1213 | |
| 1214 | storvsc_log(device, loglevel, |
Michael Kelley | 08f7654 | 2021-06-04 10:21:02 -0700 | [diff] [blame] | 1215 | "tag#%d cmd 0x%x status: scsi 0x%x srb 0x%x hv 0x%x\n", |
Martin K. Petersen | c5bf198 | 2021-08-10 00:52:41 -0400 | [diff] [blame] | 1216 | scsi_cmd_to_rq(request->cmd)->tag, |
Long Li | f8aea70 | 2015-12-04 00:07:24 -0800 | [diff] [blame] | 1217 | stor_pkt->vm_srb.cdb[0], |
| 1218 | vstor_packet->vm_srb.scsi_status, |
Michael Kelley | 08f7654 | 2021-06-04 10:21:02 -0700 | [diff] [blame] | 1219 | vstor_packet->vm_srb.srb_status, |
| 1220 | vstor_packet->status); |
Michael Kelley | dbe7633 | 2021-08-06 10:12:50 -0700 | [diff] [blame] | 1221 | } |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1222 | |
Michael Kelley | d467485 | 2021-06-04 10:21:01 -0700 | [diff] [blame] | 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); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1228 | |
| 1229 | stor_pkt->vm_srb.data_transfer_length = |
Michael Kelley | d467485 | 2021-06-04 10:21:01 -0700 | [diff] [blame] | 1230 | vstor_packet->vm_srb.data_transfer_length; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1231 | |
K. Y. Srinivasan | 03996f2 | 2015-12-23 13:15:51 -0800 | [diff] [blame] | 1232 | storvsc_command_completion(request, stor_device); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1233 | |
| 1234 | if (atomic_dec_and_test(&stor_device->num_outstanding_req) && |
| 1235 | stor_device->drain_notify) |
| 1236 | wake_up(&stor_device->waiting_to_drain); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
K. Y. Srinivasan | 03996f2 | 2015-12-23 13:15:51 -0800 | [diff] [blame] | 1239 | static void storvsc_on_receive(struct storvsc_device *stor_device, |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1240 | struct vstor_packet *vstor_packet, |
K. Y. Srinivasan | 61eaffc | 2012-01-12 12:38:07 -0800 | [diff] [blame] | 1241 | struct storvsc_cmd_request *request) |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1242 | { |
Long Li | c58cc70 | 2017-10-31 14:58:08 -0700 | [diff] [blame] | 1243 | struct hv_host_device *host_dev; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1244 | switch (vstor_packet->operation) { |
| 1245 | case VSTOR_OPERATION_COMPLETE_IO: |
K. Y. Srinivasan | 03996f2 | 2015-12-23 13:15:51 -0800 | [diff] [blame] | 1246 | storvsc_on_io_completion(stor_device, vstor_packet, request); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1247 | break; |
K. Y. Srinivasan | 12675799 | 2011-11-08 09:01:49 -0800 | [diff] [blame] | 1248 | |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1249 | case VSTOR_OPERATION_REMOVE_DEVICE: |
K. Y. Srinivasan | 12675799 | 2011-11-08 09:01:49 -0800 | [diff] [blame] | 1250 | case VSTOR_OPERATION_ENUMERATE_BUS: |
Long Li | c58cc70 | 2017-10-31 14:58:08 -0700 | [diff] [blame] | 1251 | host_dev = shost_priv(stor_device->host); |
| 1252 | queue_work( |
| 1253 | host_dev->handle_error_wq, &host_dev->host_scan_work); |
K. Y. Srinivasan | 12675799 | 2011-11-08 09:01:49 -0800 | [diff] [blame] | 1254 | break; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1255 | |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 1256 | case VSTOR_OPERATION_FCHBA_DATA: |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 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; |
| 1261 | #endif |
| 1262 | break; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1263 | default: |
| 1264 | break; |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | static void storvsc_on_channel_callback(void *context) |
| 1269 | { |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 1270 | struct vmbus_channel *channel = (struct vmbus_channel *)context; |
Stephen Hemminger | ddccd95 | 2017-05-18 09:18:11 -0700 | [diff] [blame] | 1271 | const struct vmpacket_descriptor *desc; |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 1272 | struct hv_device *device; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1273 | struct storvsc_device *stor_device; |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 1274 | struct Scsi_Host *shost; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1275 | |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 1276 | if (channel->primary_channel != NULL) |
| 1277 | device = channel->primary_channel->device_obj; |
| 1278 | else |
| 1279 | device = channel->device_obj; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1280 | |
| 1281 | stor_device = get_in_stor_device(device); |
| 1282 | if (!stor_device) |
| 1283 | return; |
| 1284 | |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 1285 | shost = stor_device->host; |
| 1286 | |
Stephen Hemminger | ddccd95 | 2017-05-18 09:18:11 -0700 | [diff] [blame] | 1287 | foreach_vmbus_pkt(desc, channel) { |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 1288 | struct vstor_packet *packet = hv_pkt_data(desc); |
| 1289 | struct storvsc_cmd_request *request = NULL; |
Andrea Parri (Microsoft) | 6fd13d6 | 2021-10-07 14:28:28 +0200 | [diff] [blame] | 1290 | u32 pktlen = hv_pkt_datalen(desc); |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 1291 | u64 rqst_id = desc->trans_id; |
Andrea Parri (Microsoft) | 6fd13d6 | 2021-10-07 14:28:28 +0200 | [diff] [blame] | 1292 | u32 minlen = rqst_id ? sizeof(struct vstor_packet) - |
| 1293 | stor_device->vmscsi_size_delta : sizeof(enum vstor_packet_operation); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1294 | |
Andrea Parri (Microsoft) | 6fd13d6 | 2021-10-07 14:28:28 +0200 | [diff] [blame] | 1295 | if (pktlen < minlen) { |
| 1296 | dev_err(&device->device, |
| 1297 | "Invalid pkt: id=%llu, len=%u, minlen=%u\n", |
| 1298 | rqst_id, pktlen, minlen); |
Andrea Parri (Microsoft) | 91b1b64 | 2020-12-17 21:33:21 +0100 | [diff] [blame] | 1299 | continue; |
| 1300 | } |
| 1301 | |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 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; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1306 | } else { |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 1307 | /* Hyper-V can send an unsolicited message with ID of 0 */ |
| 1308 | if (rqst_id == 0) { |
| 1309 | /* |
| 1310 | * storvsc_on_receive() looks at the vstor_packet in the message |
Andrea Parri (Microsoft) | 6fd13d6 | 2021-10-07 14:28:28 +0200 | [diff] [blame] | 1311 | * from the ring buffer. |
| 1312 | * |
| 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 |
| 1318 | * a bogus packet. |
| 1319 | * |
| 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. |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 1325 | */ |
Andrea Parri (Microsoft) | 6fd13d6 | 2021-10-07 14:28:28 +0200 | [diff] [blame] | 1326 | if (packet->operation == VSTOR_OPERATION_COMPLETE_IO || |
| 1327 | packet->operation == VSTOR_OPERATION_FCHBA_DATA) { |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 1328 | dev_err(&device->device, "Invalid packet with ID of 0\n"); |
| 1329 | continue; |
| 1330 | } |
| 1331 | } else { |
| 1332 | struct scsi_cmnd *scmnd; |
| 1333 | |
| 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"); |
| 1338 | continue; |
| 1339 | } |
| 1340 | request = (struct storvsc_cmd_request *)scsi_cmd_priv(scmnd); |
Tianyu Lan | 743b237 | 2021-12-13 02:14:05 -0500 | [diff] [blame] | 1341 | scsi_dma_unmap(scmnd); |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 1342 | } |
| 1343 | |
Stephen Hemminger | ddccd95 | 2017-05-18 09:18:11 -0700 | [diff] [blame] | 1344 | storvsc_on_receive(stor_device, packet, request); |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 1345 | continue; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1346 | } |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 1347 | |
| 1348 | memcpy(&request->vstor_packet, packet, |
| 1349 | (sizeof(struct vstor_packet) - stor_device->vmscsi_size_delta)); |
| 1350 | complete(&request->wait_event); |
Stephen Hemminger | ddccd95 | 2017-05-18 09:18:11 -0700 | [diff] [blame] | 1351 | } |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 1354 | static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size, |
| 1355 | bool is_fc) |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1356 | { |
| 1357 | struct vmstorage_channel_properties props; |
| 1358 | int ret; |
| 1359 | |
| 1360 | memset(&props, 0, sizeof(struct vmstorage_channel_properties)); |
| 1361 | |
Andres Beltran | adae1e9 | 2021-04-08 18:14:39 +0200 | [diff] [blame] | 1362 | device->channel->max_pkt_size = STORVSC_MAX_PKT_SIZE; |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 1363 | device->channel->next_request_id_callback = storvsc_next_request_id; |
Andres Beltran | 453de21 | 2020-11-09 11:04:01 +0100 | [diff] [blame] | 1364 | |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1365 | ret = vmbus_open(device->channel, |
| 1366 | ring_size, |
| 1367 | ring_size, |
| 1368 | (void *)&props, |
| 1369 | sizeof(struct vmstorage_channel_properties), |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 1370 | storvsc_on_channel_callback, device->channel); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1371 | |
| 1372 | if (ret != 0) |
| 1373 | return ret; |
| 1374 | |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 1375 | ret = storvsc_channel_init(device, is_fc); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1376 | |
| 1377 | return ret; |
| 1378 | } |
| 1379 | |
K. Y. Srinivasan | c1b3d06 | 2011-08-27 11:31:25 -0700 | [diff] [blame] | 1380 | static int storvsc_dev_remove(struct hv_device *device) |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1381 | { |
| 1382 | struct storvsc_device *stor_device; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1383 | |
K. Y. Srinivasan | cd654ea | 2011-09-13 10:59:48 -0700 | [diff] [blame] | 1384 | stor_device = hv_get_drvdata(device); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1385 | |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1386 | stor_device->destroy = true; |
Stephen Hemminger | 2371cd9 | 2017-05-18 09:18:12 -0700 | [diff] [blame] | 1387 | |
| 1388 | /* Make sure flag is set before waiting */ |
| 1389 | wmb(); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1390 | |
| 1391 | /* |
| 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. |
| 1395 | */ |
| 1396 | |
| 1397 | storvsc_wait_to_drain(stor_device); |
| 1398 | |
| 1399 | /* |
| 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. |
| 1405 | */ |
K. Y. Srinivasan | cd654ea | 2011-09-13 10:59:48 -0700 | [diff] [blame] | 1406 | hv_set_drvdata(device, NULL); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1407 | |
| 1408 | /* Close the channel */ |
| 1409 | vmbus_close(device->channel); |
| 1410 | |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 1411 | kfree(stor_device->stor_chns); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1412 | kfree(stor_device); |
| 1413 | return 0; |
| 1414 | } |
| 1415 | |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 1416 | static struct vmbus_channel *get_og_chn(struct storvsc_device *stor_device, |
| 1417 | u16 q_num) |
| 1418 | { |
| 1419 | u16 slot = 0; |
| 1420 | u16 hash_qnum; |
Michael Kelley | 1b25a8c4 | 2018-05-17 14:07:40 -0700 | [diff] [blame] | 1421 | const struct cpumask *node_mask; |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 1422 | int num_channels, tgt_cpu; |
| 1423 | |
Andrea Parri (Microsoft) | 7769e18 | 2020-04-06 02:15:14 +0200 | [diff] [blame] | 1424 | if (stor_device->num_sc == 0) { |
| 1425 | stor_device->stor_chns[q_num] = stor_device->device->channel; |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 1426 | return stor_device->device->channel; |
Andrea Parri (Microsoft) | 7769e18 | 2020-04-06 02:15:14 +0200 | [diff] [blame] | 1427 | } |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 1428 | |
| 1429 | /* |
| 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) |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 1436 | */ |
| 1437 | |
Michael Kelley | 1b25a8c4 | 2018-05-17 14:07:40 -0700 | [diff] [blame] | 1438 | node_mask = cpumask_of_node(cpu_to_node(q_num)); |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 1439 | |
Michael Kelley | 1b25a8c4 | 2018-05-17 14:07:40 -0700 | [diff] [blame] | 1440 | num_channels = 0; |
| 1441 | for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) { |
| 1442 | if (cpumask_test_cpu(tgt_cpu, node_mask)) |
| 1443 | num_channels++; |
| 1444 | } |
Andrea Parri (Microsoft) | 7769e18 | 2020-04-06 02:15:14 +0200 | [diff] [blame] | 1445 | if (num_channels == 0) { |
| 1446 | stor_device->stor_chns[q_num] = stor_device->device->channel; |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 1447 | return stor_device->device->channel; |
Andrea Parri (Microsoft) | 7769e18 | 2020-04-06 02:15:14 +0200 | [diff] [blame] | 1448 | } |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 1449 | |
| 1450 | hash_qnum = q_num; |
| 1451 | while (hash_qnum >= num_channels) |
| 1452 | hash_qnum -= num_channels; |
| 1453 | |
Michael Kelley | 1b25a8c4 | 2018-05-17 14:07:40 -0700 | [diff] [blame] | 1454 | for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) { |
| 1455 | if (!cpumask_test_cpu(tgt_cpu, node_mask)) |
| 1456 | continue; |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 1457 | if (slot == hash_qnum) |
| 1458 | break; |
| 1459 | slot++; |
| 1460 | } |
| 1461 | |
| 1462 | stor_device->stor_chns[q_num] = stor_device->stor_chns[tgt_cpu]; |
| 1463 | |
| 1464 | return stor_device->stor_chns[q_num]; |
| 1465 | } |
| 1466 | |
| 1467 | |
K. Y. Srinivasan | c1b3d06 | 2011-08-27 11:31:25 -0700 | [diff] [blame] | 1468 | static int storvsc_do_io(struct hv_device *device, |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 1469 | struct storvsc_cmd_request *request, u16 q_num) |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1470 | { |
| 1471 | struct storvsc_device *stor_device; |
| 1472 | struct vstor_packet *vstor_packet; |
Long Li | 2217a47 | 2018-04-19 14:54:24 -0700 | [diff] [blame] | 1473 | struct vmbus_channel *outgoing_channel, *channel; |
Andrea Parri (Microsoft) | 7769e18 | 2020-04-06 02:15:14 +0200 | [diff] [blame] | 1474 | unsigned long flags; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1475 | int ret = 0; |
Michael Kelley | 1b25a8c4 | 2018-05-17 14:07:40 -0700 | [diff] [blame] | 1476 | const struct cpumask *node_mask; |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 1477 | int tgt_cpu; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1478 | |
| 1479 | vstor_packet = &request->vstor_packet; |
| 1480 | stor_device = get_out_stor_device(device); |
| 1481 | |
| 1482 | if (!stor_device) |
| 1483 | return -ENODEV; |
| 1484 | |
| 1485 | |
| 1486 | request->device = device; |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 1487 | /* |
Andrea Parri (Microsoft) | 7769e18 | 2020-04-06 02:15:14 +0200 | [diff] [blame] | 1488 | * Select an appropriate channel to send the request out. |
K. Y. Srinivasan | 6f94d5d | 2013-06-04 12:05:07 -0700 | [diff] [blame] | 1489 | */ |
Andrea Parri (Microsoft) | 7769e18 | 2020-04-06 02:15:14 +0200 | [diff] [blame] | 1490 | /* See storvsc_change_target_cpu(). */ |
| 1491 | outgoing_channel = READ_ONCE(stor_device->stor_chns[q_num]); |
| 1492 | if (outgoing_channel != NULL) { |
Long Li | 2217a47 | 2018-04-19 14:54:24 -0700 | [diff] [blame] | 1493 | if (outgoing_channel->target_cpu == q_num) { |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 1494 | /* |
| 1495 | * Ideally, we want to pick a different channel if |
| 1496 | * available on the same NUMA node. |
| 1497 | */ |
Michael Kelley | 1b25a8c4 | 2018-05-17 14:07:40 -0700 | [diff] [blame] | 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)) |
| 1502 | continue; |
Long Li | 2217a47 | 2018-04-19 14:54:24 -0700 | [diff] [blame] | 1503 | if (tgt_cpu == q_num) |
| 1504 | continue; |
Andrea Parri (Microsoft) | 7769e18 | 2020-04-06 02:15:14 +0200 | [diff] [blame] | 1505 | channel = READ_ONCE( |
| 1506 | stor_device->stor_chns[tgt_cpu]); |
| 1507 | if (channel == NULL) |
| 1508 | continue; |
Long Li | 2217a47 | 2018-04-19 14:54:24 -0700 | [diff] [blame] | 1509 | if (hv_get_avail_to_write_percent( |
| 1510 | &channel->outbound) |
| 1511 | > ring_avail_percent_lowater) { |
| 1512 | outgoing_channel = channel; |
| 1513 | goto found_channel; |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | /* |
| 1518 | * All the other channels on the same NUMA node are |
| 1519 | * busy. Try to use the channel on the current CPU |
| 1520 | */ |
| 1521 | if (hv_get_avail_to_write_percent( |
| 1522 | &outgoing_channel->outbound) |
| 1523 | > ring_avail_percent_lowater) |
| 1524 | goto found_channel; |
| 1525 | |
| 1526 | /* |
| 1527 | * If we reach here, all the channels on the current |
| 1528 | * NUMA node are busy. Try to find a channel in |
| 1529 | * other NUMA nodes |
| 1530 | */ |
Michael Kelley | 1b25a8c4 | 2018-05-17 14:07:40 -0700 | [diff] [blame] | 1531 | for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) { |
| 1532 | if (cpumask_test_cpu(tgt_cpu, node_mask)) |
| 1533 | continue; |
Andrea Parri (Microsoft) | 7769e18 | 2020-04-06 02:15:14 +0200 | [diff] [blame] | 1534 | channel = READ_ONCE( |
| 1535 | stor_device->stor_chns[tgt_cpu]); |
| 1536 | if (channel == NULL) |
| 1537 | continue; |
Long Li | 2217a47 | 2018-04-19 14:54:24 -0700 | [diff] [blame] | 1538 | if (hv_get_avail_to_write_percent( |
| 1539 | &channel->outbound) |
| 1540 | > ring_avail_percent_lowater) { |
| 1541 | outgoing_channel = channel; |
| 1542 | goto found_channel; |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 1543 | } |
| 1544 | } |
| 1545 | } |
| 1546 | } else { |
Andrea Parri (Microsoft) | 21d2052 | 2020-06-17 18:46:41 +0200 | [diff] [blame] | 1547 | spin_lock_irqsave(&stor_device->lock, flags); |
Andrea Parri (Microsoft) | 7769e18 | 2020-04-06 02:15:14 +0200 | [diff] [blame] | 1548 | outgoing_channel = stor_device->stor_chns[q_num]; |
| 1549 | if (outgoing_channel != NULL) { |
Andrea Parri (Microsoft) | 21d2052 | 2020-06-17 18:46:41 +0200 | [diff] [blame] | 1550 | spin_unlock_irqrestore(&stor_device->lock, flags); |
Andrea Parri (Microsoft) | 7769e18 | 2020-04-06 02:15:14 +0200 | [diff] [blame] | 1551 | goto found_channel; |
| 1552 | } |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 1553 | outgoing_channel = get_og_chn(stor_device, q_num); |
Andrea Parri (Microsoft) | 21d2052 | 2020-06-17 18:46:41 +0200 | [diff] [blame] | 1554 | spin_unlock_irqrestore(&stor_device->lock, flags); |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 1555 | } |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1556 | |
Long Li | 2217a47 | 2018-04-19 14:54:24 -0700 | [diff] [blame] | 1557 | found_channel: |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1558 | vstor_packet->flags |= REQUEST_COMPLETION_FLAG; |
| 1559 | |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 1560 | vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) - |
Andrea Parri (Microsoft) | 244808e | 2020-12-17 21:33:20 +0100 | [diff] [blame] | 1561 | stor_device->vmscsi_size_delta); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1562 | |
| 1563 | |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 1564 | vstor_packet->vm_srb.sense_info_length = sense_buffer_size; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1565 | |
| 1566 | |
| 1567 | vstor_packet->vm_srb.data_transfer_length = |
K. Y. Srinivasan | be0cf6c | 2015-03-27 00:27:20 -0700 | [diff] [blame] | 1568 | request->payload->range.len; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1569 | |
| 1570 | vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB; |
| 1571 | |
K. Y. Srinivasan | be0cf6c | 2015-03-27 00:27:20 -0700 | [diff] [blame] | 1572 | if (request->payload->range.len) { |
| 1573 | |
| 1574 | ret = vmbus_sendpacket_mpb_desc(outgoing_channel, |
| 1575 | request->payload, request->payload_sz, |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1576 | vstor_packet, |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 1577 | (sizeof(struct vstor_packet) - |
Andrea Parri (Microsoft) | 244808e | 2020-12-17 21:33:20 +0100 | [diff] [blame] | 1578 | stor_device->vmscsi_size_delta), |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1579 | (unsigned long)request); |
| 1580 | } else { |
K. Y. Srinivasan | 0147dab | 2015-03-27 00:27:16 -0700 | [diff] [blame] | 1581 | ret = vmbus_sendpacket(outgoing_channel, vstor_packet, |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 1582 | (sizeof(struct vstor_packet) - |
Andrea Parri (Microsoft) | 244808e | 2020-12-17 21:33:20 +0100 | [diff] [blame] | 1583 | stor_device->vmscsi_size_delta), |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 1584 | (unsigned long)request, |
| 1585 | VM_PKT_DATA_INBAND, |
| 1586 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 1587 | } |
| 1588 | |
| 1589 | if (ret != 0) |
| 1590 | return ret; |
| 1591 | |
| 1592 | atomic_inc(&stor_device->num_outstanding_req); |
| 1593 | |
| 1594 | return ret; |
| 1595 | } |
| 1596 | |
Stephen Hemminger | f1c635b | 2017-03-07 09:15:53 -0800 | [diff] [blame] | 1597 | static int storvsc_device_alloc(struct scsi_device *sdevice) |
| 1598 | { |
| 1599 | /* |
| 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. |
| 1604 | * |
| 1605 | * Hypervisor reports SCSI_UNKNOWN type for DVD ROM device but |
| 1606 | * still supports REPORT LUN. |
| 1607 | */ |
| 1608 | sdevice->sdev_bflags = BLIST_REPORTLUN2 | BLIST_TRY_VPD_PAGES; |
| 1609 | |
| 1610 | return 0; |
| 1611 | } |
| 1612 | |
K. Y. Srinivasan | 419f2d0 | 2011-05-10 07:54:27 -0700 | [diff] [blame] | 1613 | static int storvsc_device_configure(struct scsi_device *sdevice) |
| 1614 | { |
K. Y. Srinivasan | 893def3 | 2013-06-04 12:05:05 -0700 | [diff] [blame] | 1615 | blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ)); |
| 1616 | |
Olaf Hering | 3e8f4f4 | 2013-02-21 12:04:51 -0800 | [diff] [blame] | 1617 | sdevice->no_write_same = 1; |
| 1618 | |
K. Y. Srinivasan | f3cfabc | 2014-07-21 16:06:01 -0700 | [diff] [blame] | 1619 | /* |
K. Y. Srinivasan | b0a93d9 | 2014-12-16 13:21:45 -0800 | [diff] [blame] | 1620 | * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3 |
Keith Mange | b95f5be | 2015-08-13 08:43:51 -0700 | [diff] [blame] | 1621 | * if the device is a MSFT virtual device. If the host is |
| 1622 | * WIN10 or newer, allow write_same. |
K. Y. Srinivasan | b0a93d9 | 2014-12-16 13:21:45 -0800 | [diff] [blame] | 1623 | */ |
| 1624 | if (!strncmp(sdevice->vendor, "Msft", 4)) { |
Keith Mange | e6c4bc6 | 2015-08-13 08:43:50 -0700 | [diff] [blame] | 1625 | switch (vmstor_proto_version) { |
| 1626 | case VMSTOR_PROTO_VERSION_WIN8: |
| 1627 | case VMSTOR_PROTO_VERSION_WIN8_1: |
K. Y. Srinivasan | b0a93d9 | 2014-12-16 13:21:45 -0800 | [diff] [blame] | 1628 | sdevice->scsi_level = SCSI_SPC_3; |
| 1629 | break; |
| 1630 | } |
Keith Mange | b95f5be | 2015-08-13 08:43:51 -0700 | [diff] [blame] | 1631 | |
| 1632 | if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN10) |
| 1633 | sdevice->no_write_same = 0; |
K. Y. Srinivasan | b0a93d9 | 2014-12-16 13:21:45 -0800 | [diff] [blame] | 1634 | } |
| 1635 | |
K. Y. Srinivasan | 419f2d0 | 2011-05-10 07:54:27 -0700 | [diff] [blame] | 1636 | return 0; |
| 1637 | } |
| 1638 | |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 1639 | static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, |
| 1640 | sector_t capacity, int *info) |
| 1641 | { |
K. Y. Srinivasan | 5326fd5 | 2011-05-10 07:54:52 -0700 | [diff] [blame] | 1642 | sector_t nsect = capacity; |
| 1643 | sector_t cylinders = nsect; |
| 1644 | int heads, sectors_pt; |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 1645 | |
K. Y. Srinivasan | 5326fd5 | 2011-05-10 07:54:52 -0700 | [diff] [blame] | 1646 | /* |
| 1647 | * We are making up these values; let us keep it simple. |
| 1648 | */ |
| 1649 | heads = 0xff; |
| 1650 | sectors_pt = 0x3f; /* Sectors per track */ |
| 1651 | sector_div(cylinders, heads * sectors_pt); |
| 1652 | if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect) |
| 1653 | cylinders = 0xffff; |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 1654 | |
| 1655 | info[0] = heads; |
K. Y. Srinivasan | 5326fd5 | 2011-05-10 07:54:52 -0700 | [diff] [blame] | 1656 | info[1] = sectors_pt; |
| 1657 | info[2] = (int)cylinders; |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 1658 | |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 1659 | return 0; |
| 1660 | } |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1661 | |
K. Y. Srinivasan | 4b270c8 | 2012-01-12 12:37:58 -0800 | [diff] [blame] | 1662 | static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd) |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1663 | { |
K. Y. Srinivasan | 4b270c8 | 2012-01-12 12:37:58 -0800 | [diff] [blame] | 1664 | struct hv_host_device *host_dev = shost_priv(scmnd->device->host); |
| 1665 | struct hv_device *device = host_dev->dev; |
| 1666 | |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1667 | struct storvsc_device *stor_device; |
K. Y. Srinivasan | 61eaffc | 2012-01-12 12:38:07 -0800 | [diff] [blame] | 1668 | struct storvsc_cmd_request *request; |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1669 | struct vstor_packet *vstor_packet; |
| 1670 | int ret, t; |
| 1671 | |
K. Y. Srinivasan | 1eaaddf | 2011-08-27 11:31:03 -0700 | [diff] [blame] | 1672 | stor_device = get_out_stor_device(device); |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1673 | if (!stor_device) |
K. Y. Srinivasan | a00e822 | 2011-11-08 09:01:43 -0800 | [diff] [blame] | 1674 | return FAILED; |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1675 | |
| 1676 | request = &stor_device->reset_request; |
| 1677 | vstor_packet = &request->vstor_packet; |
Andres Beltran | 0a76566 | 2020-07-06 12:09:28 -0400 | [diff] [blame] | 1678 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1679 | |
| 1680 | init_completion(&request->wait_event); |
| 1681 | |
| 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; |
| 1685 | |
| 1686 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 1687 | (sizeof(struct vstor_packet) - |
Andrea Parri (Microsoft) | 244808e | 2020-12-17 21:33:20 +0100 | [diff] [blame] | 1688 | stor_device->vmscsi_size_delta), |
Andrea Parri (Microsoft) | bf5fd8c | 2021-05-10 23:08:41 +0200 | [diff] [blame] | 1689 | VMBUS_RQST_RESET, |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1690 | VM_PKT_DATA_INBAND, |
| 1691 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 1692 | if (ret != 0) |
K. Y. Srinivasan | a00e822 | 2011-11-08 09:01:43 -0800 | [diff] [blame] | 1693 | return FAILED; |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1694 | |
K. Y. Srinivasan | 46d2eb6 | 2011-06-16 13:16:36 -0700 | [diff] [blame] | 1695 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); |
K. Y. Srinivasan | a00e822 | 2011-11-08 09:01:43 -0800 | [diff] [blame] | 1696 | if (t == 0) |
| 1697 | return TIMEOUT_ERROR; |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1698 | |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1699 | |
| 1700 | /* |
| 1701 | * At this point, all outstanding requests in the adapter |
| 1702 | * should have been flushed out and return to us |
K. Y. Srinivasan | 5c1b10a | 2012-10-02 11:03:31 -0700 | [diff] [blame] | 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. |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1707 | */ |
K. Y. Srinivasan | 5c1b10a | 2012-10-02 11:03:31 -0700 | [diff] [blame] | 1708 | storvsc_wait_to_drain(stor_device); |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1709 | |
K. Y. Srinivasan | a00e822 | 2011-11-08 09:01:43 -0800 | [diff] [blame] | 1710 | return SUCCESS; |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
K. Y. Srinivasan | 56b26e6 | 2014-07-12 09:48:30 -0700 | [diff] [blame] | 1713 | /* |
| 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. |
| 1717 | */ |
| 1718 | static enum blk_eh_timer_return storvsc_eh_timed_out(struct scsi_cmnd *scmnd) |
| 1719 | { |
Long Li | 3930d73 | 2017-06-13 14:34:05 -0700 | [diff] [blame] | 1720 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) |
| 1721 | if (scmnd->device->host->transportt == fc_transport_template) |
| 1722 | return fc_eh_timed_out(scmnd); |
| 1723 | #endif |
K. Y. Srinivasan | 56b26e6 | 2014-07-12 09:48:30 -0700 | [diff] [blame] | 1724 | return BLK_EH_RESET_TIMER; |
| 1725 | } |
| 1726 | |
K. Y. Srinivasan | c77b63b | 2012-01-12 12:37:56 -0800 | [diff] [blame] | 1727 | static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd) |
Olaf Hering | 92ae4eb | 2011-10-10 09:37:39 +0200 | [diff] [blame] | 1728 | { |
| 1729 | bool allowed = true; |
| 1730 | u8 scsi_op = scmnd->cmnd[0]; |
| 1731 | |
| 1732 | switch (scsi_op) { |
Olaf Hering | 3e8f4f4 | 2013-02-21 12:04:51 -0800 | [diff] [blame] | 1733 | /* the host does not handle WRITE_SAME, log accident usage */ |
| 1734 | case WRITE_SAME: |
K. Y. Srinivasan | c77b63b | 2012-01-12 12:37:56 -0800 | [diff] [blame] | 1735 | /* |
| 1736 | * smartd sends this command and the host does not handle |
| 1737 | * this. So, don't send it. |
| 1738 | */ |
K. Y. Srinivasan | 41098f8 | 2011-10-14 21:31:57 -0700 | [diff] [blame] | 1739 | case SET_WINDOW: |
Michael Kelley | d467485 | 2021-06-04 10:21:01 -0700 | [diff] [blame] | 1740 | set_host_byte(scmnd, DID_ERROR); |
K. Y. Srinivasan | 41098f8 | 2011-10-14 21:31:57 -0700 | [diff] [blame] | 1741 | allowed = false; |
| 1742 | break; |
| 1743 | default: |
| 1744 | break; |
Olaf Hering | 92ae4eb | 2011-10-10 09:37:39 +0200 | [diff] [blame] | 1745 | } |
| 1746 | return allowed; |
| 1747 | } |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1748 | |
K. Y. Srinivasan | bab445e | 2011-11-08 09:01:45 -0800 | [diff] [blame] | 1749 | static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd) |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1750 | { |
| 1751 | int ret; |
K. Y. Srinivasan | bab445e | 2011-11-08 09:01:45 -0800 | [diff] [blame] | 1752 | struct hv_host_device *host_dev = shost_priv(host); |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1753 | struct hv_device *dev = host_dev->dev; |
Christoph Hellwig | ead3700 | 2014-12-29 20:05:09 -0800 | [diff] [blame] | 1754 | struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd); |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1755 | struct scatterlist *sgl; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1756 | struct vmscsi_request *vm_srb; |
K. Y. Srinivasan | be0cf6c | 2015-03-27 00:27:20 -0700 | [diff] [blame] | 1757 | struct vmbus_packet_mpb_array *payload; |
| 1758 | u32 payload_sz; |
| 1759 | u32 length; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1760 | |
Keith Mange | 2492fd7 | 2015-08-13 08:43:47 -0700 | [diff] [blame] | 1761 | if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) { |
K. Y. Srinivasan | 8caf92d | 2014-07-12 09:48:28 -0700 | [diff] [blame] | 1762 | /* |
| 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. |
| 1769 | */ |
| 1770 | if (!storvsc_scsi_cmd_ok(scmnd)) { |
Bart Van Assche | 0c31fa0 | 2021-10-07 13:29:08 -0700 | [diff] [blame] | 1771 | scsi_done(scmnd); |
K. Y. Srinivasan | 8caf92d | 2014-07-12 09:48:28 -0700 | [diff] [blame] | 1772 | return 0; |
| 1773 | } |
Olaf Hering | 92ae4eb | 2011-10-10 09:37:39 +0200 | [diff] [blame] | 1774 | } |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1775 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1776 | /* Setup the cmd request */ |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1777 | cmd_request->cmd = scmnd; |
| 1778 | |
Andres Beltran | 0a76566 | 2020-07-06 12:09:28 -0400 | [diff] [blame] | 1779 | memset(&cmd_request->vstor_packet, 0, sizeof(struct vstor_packet)); |
K. Y. Srinivasan | 61eaffc | 2012-01-12 12:38:07 -0800 | [diff] [blame] | 1780 | vm_srb = &cmd_request->vstor_packet.vm_srb; |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 1781 | vm_srb->win8_extension.time_out_value = 60; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1782 | |
K. Y. Srinivasan | f885fb7 | 2014-07-12 09:48:31 -0700 | [diff] [blame] | 1783 | vm_srb->win8_extension.srb_flags |= |
K. Y. Srinivasan | 8cf308e | 2015-08-31 08:21:54 -0700 | [diff] [blame] | 1784 | SRB_FLAGS_DISABLE_SYNCH_TRANSFER; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1785 | |
Long Li | 3cd6d3d | 2016-12-14 18:46:01 -0800 | [diff] [blame] | 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; |
| 1791 | } |
| 1792 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1793 | /* Build the SRB */ |
| 1794 | switch (scmnd->sc_data_direction) { |
| 1795 | case DMA_TO_DEVICE: |
| 1796 | vm_srb->data_in = WRITE_TYPE; |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 1797 | vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1798 | break; |
| 1799 | case DMA_FROM_DEVICE: |
| 1800 | vm_srb->data_in = READ_TYPE; |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 1801 | vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1802 | break; |
Vitaly Kuznetsov | cb1cf08 | 2015-06-25 18:12:11 +0200 | [diff] [blame] | 1803 | case DMA_NONE: |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1804 | vm_srb->data_in = UNKNOWN_TYPE; |
K. Y. Srinivasan | dc45708 | 2015-05-01 11:03:02 -0700 | [diff] [blame] | 1805 | vm_srb->win8_extension.srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1806 | break; |
Vitaly Kuznetsov | cb1cf08 | 2015-06-25 18:12:11 +0200 | [diff] [blame] | 1807 | default: |
| 1808 | /* |
| 1809 | * This is DMA_BIDIRECTIONAL or something else we are never |
| 1810 | * supposed to see here. |
| 1811 | */ |
| 1812 | WARN(1, "Unexpected data direction: %d\n", |
| 1813 | scmnd->sc_data_direction); |
| 1814 | return -EINVAL; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1815 | } |
| 1816 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1817 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 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; |
| 1822 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1823 | vm_srb->cdb_length = scmnd->cmd_len; |
| 1824 | |
| 1825 | memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length); |
| 1826 | |
K. Y. Srinivasan | be0cf6c | 2015-03-27 00:27:20 -0700 | [diff] [blame] | 1827 | sgl = (struct scatterlist *)scsi_sglist(scmnd); |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1828 | |
K. Y. Srinivasan | be0cf6c | 2015-03-27 00:27:20 -0700 | [diff] [blame] | 1829 | length = scsi_bufflen(scmnd); |
| 1830 | payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb; |
| 1831 | payload_sz = sizeof(cmd_request->mpb); |
| 1832 | |
YueHaibing | 0bd2fbe | 2021-12-27 12:03:11 +0800 | [diff] [blame] | 1833 | if (scsi_sg_count(scmnd)) { |
Michael Kelley | 3d9c3dc | 2021-02-23 14:44:54 -0800 | [diff] [blame] | 1834 | unsigned long offset_in_hvpg = offset_in_hvpage(sgl->offset); |
Boqun Feng | 8f43710 | 2020-09-16 11:48:17 +0800 | [diff] [blame] | 1835 | unsigned int hvpg_count = HVPFN_UP(offset_in_hvpg + length); |
Tianyu Lan | 743b237 | 2021-12-13 02:14:05 -0500 | [diff] [blame] | 1836 | struct scatterlist *sg; |
| 1837 | unsigned long hvpfn, hvpfns_to_add; |
YueHaibing | 0bd2fbe | 2021-12-27 12:03:11 +0800 | [diff] [blame] | 1838 | int j, i = 0, sg_count; |
K. Y. Srinivasan | be0cf6c | 2015-03-27 00:27:20 -0700 | [diff] [blame] | 1839 | |
Boqun Feng | 8f43710 | 2020-09-16 11:48:17 +0800 | [diff] [blame] | 1840 | if (hvpg_count > MAX_PAGE_BUFFER_COUNT) { |
| 1841 | |
| 1842 | payload_sz = (hvpg_count * sizeof(u64) + |
K. Y. Srinivasan | be0cf6c | 2015-03-27 00:27:20 -0700 | [diff] [blame] | 1843 | sizeof(struct vmbus_packet_mpb_array)); |
Cathy Avery | b0120d9 | 2016-11-23 08:46:33 -0500 | [diff] [blame] | 1844 | payload = kzalloc(payload_sz, GFP_ATOMIC); |
Vitaly Kuznetsov | 81988a0 | 2015-10-01 14:53:17 -0700 | [diff] [blame] | 1845 | if (!payload) |
| 1846 | return SCSI_MLQUEUE_DEVICE_BUSY; |
K. Y. Srinivasan | be0cf6c | 2015-03-27 00:27:20 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | payload->range.len = length; |
Boqun Feng | 8f43710 | 2020-09-16 11:48:17 +0800 | [diff] [blame] | 1850 | payload->range.offset = offset_in_hvpg; |
K. Y. Srinivasan | be0cf6c | 2015-03-27 00:27:20 -0700 | [diff] [blame] | 1851 | |
Tianyu Lan | 743b237 | 2021-12-13 02:14:05 -0500 | [diff] [blame] | 1852 | sg_count = scsi_dma_map(scmnd); |
Juan Vazquez | 4eea533 | 2022-01-08 16:17:58 -0800 | [diff] [blame] | 1853 | if (sg_count < 0) { |
| 1854 | ret = SCSI_MLQUEUE_DEVICE_BUSY; |
| 1855 | goto err_free_payload; |
| 1856 | } |
Michael Kelley | 3d9c3dc | 2021-02-23 14:44:54 -0800 | [diff] [blame] | 1857 | |
Tianyu Lan | 743b237 | 2021-12-13 02:14:05 -0500 | [diff] [blame] | 1858 | for_each_sg(sgl, sg, sg_count, j) { |
Boqun Feng | 8f43710 | 2020-09-16 11:48:17 +0800 | [diff] [blame] | 1859 | /* |
Tianyu Lan | 743b237 | 2021-12-13 02:14:05 -0500 | [diff] [blame] | 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. |
Boqun Feng | 8f43710 | 2020-09-16 11:48:17 +0800 | [diff] [blame] | 1866 | */ |
Tianyu Lan | 743b237 | 2021-12-13 02:14:05 -0500 | [diff] [blame] | 1867 | hvpfn = HVPFN_DOWN(sg_dma_address(sg)); |
| 1868 | hvpfns_to_add = HVPFN_UP(sg_dma_address(sg) + |
| 1869 | sg_dma_len(sg)) - hvpfn; |
Boqun Feng | 8f43710 | 2020-09-16 11:48:17 +0800 | [diff] [blame] | 1870 | |
| 1871 | /* |
Michael Kelley | 3d9c3dc | 2021-02-23 14:44:54 -0800 | [diff] [blame] | 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. |
Boqun Feng | 8f43710 | 2020-09-16 11:48:17 +0800 | [diff] [blame] | 1877 | */ |
Michael Kelley | 3d9c3dc | 2021-02-23 14:44:54 -0800 | [diff] [blame] | 1878 | while (hvpfns_to_add--) |
Tianyu Lan | 743b237 | 2021-12-13 02:14:05 -0500 | [diff] [blame] | 1879 | payload->range.pfn_array[i++] = hvpfn++; |
K. Y. Srinivasan | aaced99 | 2015-03-27 00:27:19 -0700 | [diff] [blame] | 1880 | } |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1881 | } |
| 1882 | |
K. Y. Srinivasan | be0cf6c | 2015-03-27 00:27:20 -0700 | [diff] [blame] | 1883 | cmd_request->payload = payload; |
| 1884 | cmd_request->payload_sz = payload_sz; |
| 1885 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1886 | /* Invokes the vsc to start an IO */ |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 1887 | ret = storvsc_do_io(dev, cmd_request, get_cpu()); |
| 1888 | put_cpu(); |
K. Y. Srinivasan | 636f0fd | 2011-05-10 07:54:50 -0700 | [diff] [blame] | 1889 | |
K. Y. Srinivasan | d2598f0 | 2011-08-25 09:48:58 -0700 | [diff] [blame] | 1890 | if (ret == -EAGAIN) { |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1891 | /* no more space */ |
Juan Vazquez | 4eea533 | 2022-01-08 16:17:58 -0800 | [diff] [blame] | 1892 | ret = SCSI_MLQUEUE_DEVICE_BUSY; |
| 1893 | goto err_free_payload; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1894 | } |
| 1895 | |
K. Y. Srinivasan | c77b63b | 2012-01-12 12:37:56 -0800 | [diff] [blame] | 1896 | return 0; |
Juan Vazquez | 4eea533 | 2022-01-08 16:17:58 -0800 | [diff] [blame] | 1897 | |
| 1898 | err_free_payload: |
| 1899 | if (payload_sz > sizeof(cmd_request->mpb)) |
| 1900 | kfree(payload); |
| 1901 | |
| 1902 | return ret; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1903 | } |
| 1904 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1905 | static struct scsi_host_template scsi_driver = { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 1906 | .module = THIS_MODULE, |
| 1907 | .name = "storvsc_host_t", |
Christoph Hellwig | ead3700 | 2014-12-29 20:05:09 -0800 | [diff] [blame] | 1908 | .cmd_size = sizeof(struct storvsc_cmd_request), |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 1909 | .bios_param = storvsc_get_chs, |
| 1910 | .queuecommand = storvsc_queuecommand, |
| 1911 | .eh_host_reset_handler = storvsc_host_reset_handler, |
Christoph Hellwig | ead3700 | 2014-12-29 20:05:09 -0800 | [diff] [blame] | 1912 | .proc_name = "storvsc_host", |
K. Y. Srinivasan | 56b26e6 | 2014-07-12 09:48:30 -0700 | [diff] [blame] | 1913 | .eh_timed_out = storvsc_eh_timed_out, |
Stephen Hemminger | f1c635b | 2017-03-07 09:15:53 -0800 | [diff] [blame] | 1914 | .slave_alloc = storvsc_device_alloc, |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 1915 | .slave_configure = storvsc_device_configure, |
Michael Kelley (EOSG) | cabe92a | 2018-01-24 22:49:57 +0000 | [diff] [blame] | 1916 | .cmd_per_lun = 2048, |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 1917 | .this_id = -1, |
Christoph Hellwig | 83eed45 | 2019-06-17 14:19:56 +0200 | [diff] [blame] | 1918 | /* Ensure there are no gaps in presented sgls */ |
| 1919 | .virt_boundary_mask = PAGE_SIZE-1, |
Martin K. Petersen | 54b2b50 | 2013-10-23 06:25:40 -0400 | [diff] [blame] | 1920 | .no_write_same = 1, |
K. Y. Srinivasan | f64dad2 | 2016-12-14 18:45:58 -0800 | [diff] [blame] | 1921 | .track_queue_depth = 1, |
Branden Bonaby | adfbd02 | 2019-06-14 19:48:22 -0400 | [diff] [blame] | 1922 | .change_queue_depth = storvsc_change_queue_depth, |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1923 | }; |
| 1924 | |
K. Y. Srinivasan | ef52a81 | 2011-09-13 10:59:39 -0700 | [diff] [blame] | 1925 | enum { |
| 1926 | SCSI_GUID, |
| 1927 | IDE_GUID, |
K. Y. Srinivasan | bde6d0f | 2013-06-04 12:05:08 -0700 | [diff] [blame] | 1928 | SFC_GUID, |
K. Y. Srinivasan | ef52a81 | 2011-09-13 10:59:39 -0700 | [diff] [blame] | 1929 | }; |
| 1930 | |
K. Y. Srinivasan | d847b5f | 2011-08-25 09:48:33 -0700 | [diff] [blame] | 1931 | static const struct hv_vmbus_device_id id_table[] = { |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 1932 | /* SCSI guid */ |
K. Y. Srinivasan | 35c3bc2 | 2013-01-23 17:42:43 -0800 | [diff] [blame] | 1933 | { HV_SCSI_GUID, |
| 1934 | .driver_data = SCSI_GUID |
| 1935 | }, |
K. Y. Srinivasan | 21e3774 | 2011-08-27 11:31:18 -0700 | [diff] [blame] | 1936 | /* IDE guid */ |
K. Y. Srinivasan | 35c3bc2 | 2013-01-23 17:42:43 -0800 | [diff] [blame] | 1937 | { HV_IDE_GUID, |
| 1938 | .driver_data = IDE_GUID |
| 1939 | }, |
K. Y. Srinivasan | bde6d0f | 2013-06-04 12:05:08 -0700 | [diff] [blame] | 1940 | /* Fibre Channel GUID */ |
| 1941 | { |
| 1942 | HV_SYNTHFC_GUID, |
| 1943 | .driver_data = SFC_GUID |
| 1944 | }, |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 1945 | { }, |
K. Y. Srinivasan | d847b5f | 2011-08-25 09:48:33 -0700 | [diff] [blame] | 1946 | }; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1947 | |
K. Y. Srinivasan | d847b5f | 2011-08-25 09:48:33 -0700 | [diff] [blame] | 1948 | MODULE_DEVICE_TABLE(vmbus, id_table); |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1949 | |
Dexuan Cui | 56fb105 | 2019-09-11 23:35:16 +0000 | [diff] [blame] | 1950 | static const struct { guid_t guid; } fc_guid = { HV_SYNTHFC_GUID }; |
| 1951 | |
| 1952 | static bool hv_dev_is_fc(struct hv_device *hv_dev) |
| 1953 | { |
| 1954 | return guid_equal(&fc_guid.guid, &hv_dev->dev_type); |
| 1955 | } |
| 1956 | |
K. Y. Srinivasan | 8494689 | 2011-09-13 10:59:38 -0700 | [diff] [blame] | 1957 | static int storvsc_probe(struct hv_device *device, |
| 1958 | const struct hv_vmbus_device_id *dev_id) |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1959 | { |
| 1960 | int ret; |
K. Y. Srinivasan | f458aad | 2015-03-27 00:27:15 -0700 | [diff] [blame] | 1961 | int num_cpus = num_online_cpus(); |
Melanie Plageman (Microsoft) | a81a38c | 2021-02-24 23:29:48 +0000 | [diff] [blame] | 1962 | int num_present_cpus = num_present_cpus(); |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1963 | struct Scsi_Host *host; |
| 1964 | struct hv_host_device *host_dev; |
K. Y. Srinivasan | ef52a81 | 2011-09-13 10:59:39 -0700 | [diff] [blame] | 1965 | bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false); |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 1966 | bool is_fc = ((dev_id->driver_data == SFC_GUID) ? true : false); |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1967 | int target = 0; |
K. Y. Srinivasan | 6e4198c | 2011-09-13 10:59:45 -0700 | [diff] [blame] | 1968 | struct storvsc_device *stor_device; |
K. Y. Srinivasan | 0fb8db2 | 2014-12-16 13:21:44 -0800 | [diff] [blame] | 1969 | int max_luns_per_target; |
| 1970 | int max_targets; |
| 1971 | int max_channels; |
K. Y. Srinivasan | f458aad | 2015-03-27 00:27:15 -0700 | [diff] [blame] | 1972 | int max_sub_channels = 0; |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1973 | |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 1974 | /* |
| 1975 | * Based on the windows host we are running on, |
| 1976 | * set state to properly communicate with the host. |
| 1977 | */ |
| 1978 | |
Keith Mange | 6ee5c61 | 2015-08-13 08:43:46 -0700 | [diff] [blame] | 1979 | if (vmbus_proto_version < VERSION_WIN8) { |
K. Y. Srinivasan | 0fb8db2 | 2014-12-16 13:21:44 -0800 | [diff] [blame] | 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; |
Keith Mange | 6ee5c61 | 2015-08-13 08:43:46 -0700 | [diff] [blame] | 1983 | } else { |
K. Y. Srinivasan | 0fb8db2 | 2014-12-16 13:21:44 -0800 | [diff] [blame] | 1984 | max_luns_per_target = STORVSC_MAX_LUNS_PER_TARGET; |
| 1985 | max_targets = STORVSC_MAX_TARGETS; |
| 1986 | max_channels = STORVSC_MAX_CHANNELS; |
K. Y. Srinivasan | f458aad | 2015-03-27 00:27:15 -0700 | [diff] [blame] | 1987 | /* |
Long Li | f286299 | 2018-03-22 14:47:18 -0700 | [diff] [blame] | 1988 | * On Windows8 and above, we support sub-channels for storage |
| 1989 | * on SCSI and FC controllers. |
K. Y. Srinivasan | f458aad | 2015-03-27 00:27:15 -0700 | [diff] [blame] | 1990 | * The number of sub-channels offerred is based on the number of |
| 1991 | * VCPUs in the guest. |
| 1992 | */ |
Long Li | f286299 | 2018-03-22 14:47:18 -0700 | [diff] [blame] | 1993 | if (!dev_is_ide) |
| 1994 | max_sub_channels = |
| 1995 | (num_cpus - 1) / storvsc_vcpus_per_sub_channel; |
K. Y. Srinivasan | 8b612fa | 2013-06-04 12:05:06 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
Long Li | 2217a47 | 2018-04-19 14:54:24 -0700 | [diff] [blame] | 1998 | scsi_driver.can_queue = max_outstanding_req_per_channel * |
| 1999 | (max_sub_channels + 1) * |
| 2000 | (100 - ring_avail_percent_lowater) / 100; |
K. Y. Srinivasan | f458aad | 2015-03-27 00:27:15 -0700 | [diff] [blame] | 2001 | |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 2002 | host = scsi_host_alloc(&scsi_driver, |
| 2003 | sizeof(struct hv_host_device)); |
| 2004 | if (!host) |
| 2005 | return -ENOMEM; |
| 2006 | |
K. Y. Srinivasan | 7f33f30 | 2011-11-08 09:01:44 -0800 | [diff] [blame] | 2007 | host_dev = shost_priv(host); |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 2008 | memset(host_dev, 0, sizeof(struct hv_host_device)); |
| 2009 | |
| 2010 | host_dev->port = host->host_no; |
| 2011 | host_dev->dev = device; |
Long Li | c58cc70 | 2017-10-31 14:58:08 -0700 | [diff] [blame] | 2012 | host_dev->host = host; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 2013 | |
K. Y. Srinivasan | 4e03e69 | 2011-11-08 09:01:40 -0800 | [diff] [blame] | 2014 | |
K. Y. Srinivasan | a13d35a | 2011-09-13 10:59:46 -0700 | [diff] [blame] | 2015 | stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL); |
K. Y. Srinivasan | 6e4198c | 2011-09-13 10:59:45 -0700 | [diff] [blame] | 2016 | if (!stor_device) { |
K. Y. Srinivasan | 225ce6e | 2011-11-08 09:01:41 -0800 | [diff] [blame] | 2017 | ret = -ENOMEM; |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 2018 | goto err_out0; |
K. Y. Srinivasan | 6e4198c | 2011-09-13 10:59:45 -0700 | [diff] [blame] | 2019 | } |
| 2020 | |
K. Y. Srinivasan | a13d35a | 2011-09-13 10:59:46 -0700 | [diff] [blame] | 2021 | stor_device->destroy = false; |
| 2022 | init_waitqueue_head(&stor_device->waiting_to_drain); |
| 2023 | stor_device->device = device; |
K. Y. Srinivasan | cd654ea | 2011-09-13 10:59:48 -0700 | [diff] [blame] | 2024 | stor_device->host = host; |
Andrea Parri (Microsoft) | 244808e | 2020-12-17 21:33:20 +0100 | [diff] [blame] | 2025 | stor_device->vmscsi_size_delta = sizeof(struct vmscsi_win8_extension); |
Andrea Parri (Microsoft) | 21d2052 | 2020-06-17 18:46:41 +0200 | [diff] [blame] | 2026 | spin_lock_init(&stor_device->lock); |
K. Y. Srinivasan | cd654ea | 2011-09-13 10:59:48 -0700 | [diff] [blame] | 2027 | hv_set_drvdata(device, stor_device); |
Tianyu Lan | 743b237 | 2021-12-13 02:14:05 -0500 | [diff] [blame] | 2028 | dma_set_min_align_mask(&device->device, HV_HYP_PAGE_SIZE - 1); |
K. Y. Srinivasan | a13d35a | 2011-09-13 10:59:46 -0700 | [diff] [blame] | 2029 | |
K. Y. Srinivasan | 6e4198c | 2011-09-13 10:59:45 -0700 | [diff] [blame] | 2030 | stor_device->port_number = host->host_no; |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 2031 | ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size, is_fc); |
K. Y. Srinivasan | 225ce6e | 2011-11-08 09:01:41 -0800 | [diff] [blame] | 2032 | if (ret) |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 2033 | goto err_out1; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 2034 | |
K. Y. Srinivasan | 6e4198c | 2011-09-13 10:59:45 -0700 | [diff] [blame] | 2035 | host_dev->path = stor_device->path_id; |
| 2036 | host_dev->target = stor_device->target_id; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 2037 | |
K. Y. Srinivasan | 4cd83ec | 2014-07-12 09:48:26 -0700 | [diff] [blame] | 2038 | switch (dev_id->driver_data) { |
| 2039 | case SFC_GUID: |
| 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; |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 2043 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) |
| 2044 | host->transportt = fc_transport_template; |
| 2045 | #endif |
K. Y. Srinivasan | 4cd83ec | 2014-07-12 09:48:26 -0700 | [diff] [blame] | 2046 | break; |
| 2047 | |
| 2048 | case SCSI_GUID: |
K. Y. Srinivasan | 0fb8db2 | 2014-12-16 13:21:44 -0800 | [diff] [blame] | 2049 | host->max_lun = max_luns_per_target; |
| 2050 | host->max_id = max_targets; |
| 2051 | host->max_channel = max_channels - 1; |
K. Y. Srinivasan | 4cd83ec | 2014-07-12 09:48:26 -0700 | [diff] [blame] | 2052 | break; |
| 2053 | |
| 2054 | default: |
| 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; |
| 2058 | break; |
| 2059 | } |
Mike Sterling | cf55f4a | 2011-09-06 16:10:55 -0700 | [diff] [blame] | 2060 | /* max cmd length */ |
| 2061 | host->max_cmd_len = STORVSC_MAX_CMD_LEN; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 2062 | |
K. Y. Srinivasan | be0cf6c | 2015-03-27 00:27:20 -0700 | [diff] [blame] | 2063 | /* |
| 2064 | * set the table size based on the info we got |
| 2065 | * from the host. |
| 2066 | */ |
| 2067 | host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT); |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 2068 | /* |
Long Li | 7b571c1 | 2020-01-13 16:08:36 -0800 | [diff] [blame] | 2069 | * For non-IDE disks, the host supports multiple channels. |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 2070 | * Set the number of HW queues we are supporting. |
| 2071 | */ |
Melanie Plageman (Microsoft) | a81a38c | 2021-02-24 23:29:48 +0000 | [diff] [blame] | 2072 | if (!dev_is_ide) { |
| 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"); |
| 2077 | } |
| 2078 | if (storvsc_max_hw_queues) |
| 2079 | host->nr_hw_queues = storvsc_max_hw_queues; |
| 2080 | else |
| 2081 | host->nr_hw_queues = num_present_cpus; |
| 2082 | } |
K. Y. Srinivasan | be0cf6c | 2015-03-27 00:27:20 -0700 | [diff] [blame] | 2083 | |
Cathy Avery | 436ad94 | 2017-10-31 08:52:06 -0400 | [diff] [blame] | 2084 | /* |
| 2085 | * Set the error handler work queue. |
| 2086 | */ |
| 2087 | host_dev->handle_error_wq = |
| 2088 | alloc_ordered_workqueue("storvsc_error_wq_%d", |
| 2089 | WQ_MEM_RECLAIM, |
| 2090 | host->host_no); |
Jing Xiangfeng | 6112ff4 | 2020-11-27 11:02:06 +0800 | [diff] [blame] | 2091 | if (!host_dev->handle_error_wq) { |
| 2092 | ret = -ENOMEM; |
Cathy Avery | 436ad94 | 2017-10-31 08:52:06 -0400 | [diff] [blame] | 2093 | goto err_out2; |
Jing Xiangfeng | 6112ff4 | 2020-11-27 11:02:06 +0800 | [diff] [blame] | 2094 | } |
Long Li | c58cc70 | 2017-10-31 14:58:08 -0700 | [diff] [blame] | 2095 | INIT_WORK(&host_dev->host_scan_work, storvsc_host_scan); |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 2096 | /* Register the HBA and start the scsi bus scan */ |
| 2097 | ret = scsi_add_host(host, &device->device); |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 2098 | if (ret != 0) |
Cathy Avery | 436ad94 | 2017-10-31 08:52:06 -0400 | [diff] [blame] | 2099 | goto err_out3; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 2100 | |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 2101 | if (!dev_is_ide) { |
| 2102 | scsi_scan_host(host); |
K. Y. Srinivasan | 59d2295 | 2012-01-12 12:37:55 -0800 | [diff] [blame] | 2103 | } else { |
| 2104 | target = (device->dev_instance.b[5] << 8 | |
| 2105 | device->dev_instance.b[4]); |
| 2106 | ret = scsi_add_device(host, 0, target, 0); |
Cathy Avery | daf0cd4 | 2017-04-17 14:37:46 -0400 | [diff] [blame] | 2107 | if (ret) |
Cathy Avery | 436ad94 | 2017-10-31 08:52:06 -0400 | [diff] [blame] | 2108 | goto err_out4; |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 2109 | } |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 2110 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) |
| 2111 | if (host->transportt == fc_transport_template) { |
Cathy Avery | daf0cd4 | 2017-04-17 14:37:46 -0400 | [diff] [blame] | 2112 | struct fc_rport_identifiers ids = { |
| 2113 | .roles = FC_PORT_ROLE_FCP_DUMMY_INITIATOR, |
| 2114 | }; |
| 2115 | |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 2116 | fc_host_node_name(host) = stor_device->node_name; |
| 2117 | fc_host_port_name(host) = stor_device->port_name; |
Cathy Avery | daf0cd4 | 2017-04-17 14:37:46 -0400 | [diff] [blame] | 2118 | stor_device->rport = fc_remote_port_add(host, 0, &ids); |
Dan Carpenter | ca8dc69 | 2018-01-16 13:40:22 +0300 | [diff] [blame] | 2119 | if (!stor_device->rport) { |
| 2120 | ret = -ENOMEM; |
Cathy Avery | 436ad94 | 2017-10-31 08:52:06 -0400 | [diff] [blame] | 2121 | goto err_out4; |
Dan Carpenter | ca8dc69 | 2018-01-16 13:40:22 +0300 | [diff] [blame] | 2122 | } |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 2123 | } |
| 2124 | #endif |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 2125 | return 0; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 2126 | |
Cathy Avery | 436ad94 | 2017-10-31 08:52:06 -0400 | [diff] [blame] | 2127 | err_out4: |
Cathy Avery | daf0cd4 | 2017-04-17 14:37:46 -0400 | [diff] [blame] | 2128 | scsi_remove_host(host); |
| 2129 | |
Cathy Avery | 436ad94 | 2017-10-31 08:52:06 -0400 | [diff] [blame] | 2130 | err_out3: |
| 2131 | destroy_workqueue(host_dev->handle_error_wq); |
| 2132 | |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 2133 | err_out2: |
K. Y. Srinivasan | 225ce6e | 2011-11-08 09:01:41 -0800 | [diff] [blame] | 2134 | /* |
| 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 |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 2138 | * err_out1 label. |
K. Y. Srinivasan | 225ce6e | 2011-11-08 09:01:41 -0800 | [diff] [blame] | 2139 | */ |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 2140 | storvsc_dev_remove(device); |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 2141 | goto err_out0; |
K. Y. Srinivasan | 225ce6e | 2011-11-08 09:01:41 -0800 | [diff] [blame] | 2142 | |
| 2143 | err_out1: |
K. Y. Srinivasan | d86adf4 | 2016-12-14 18:46:00 -0800 | [diff] [blame] | 2144 | kfree(stor_device->stor_chns); |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 2145 | kfree(stor_device); |
K. Y. Srinivasan | 225ce6e | 2011-11-08 09:01:41 -0800 | [diff] [blame] | 2146 | |
| 2147 | err_out0: |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 2148 | scsi_host_put(host); |
K. Y. Srinivasan | 225ce6e | 2011-11-08 09:01:41 -0800 | [diff] [blame] | 2149 | return ret; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 2150 | } |
| 2151 | |
Branden Bonaby | adfbd02 | 2019-06-14 19:48:22 -0400 | [diff] [blame] | 2152 | /* Change a scsi target's queue depth */ |
| 2153 | static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth) |
| 2154 | { |
| 2155 | if (queue_depth > scsi_driver.can_queue) |
| 2156 | queue_depth = scsi_driver.can_queue; |
| 2157 | |
| 2158 | return scsi_change_queue_depth(sdev, queue_depth); |
| 2159 | } |
| 2160 | |
K. Y. Srinivasan | ddcbf65 | 2012-01-12 12:37:59 -0800 | [diff] [blame] | 2161 | static int storvsc_remove(struct hv_device *dev) |
| 2162 | { |
| 2163 | struct storvsc_device *stor_device = hv_get_drvdata(dev); |
| 2164 | struct Scsi_Host *host = stor_device->host; |
Cathy Avery | 436ad94 | 2017-10-31 08:52:06 -0400 | [diff] [blame] | 2165 | struct hv_host_device *host_dev = shost_priv(host); |
K. Y. Srinivasan | ddcbf65 | 2012-01-12 12:37:59 -0800 | [diff] [blame] | 2166 | |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 2167 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) |
Cathy Avery | daf0cd4 | 2017-04-17 14:37:46 -0400 | [diff] [blame] | 2168 | if (host->transportt == fc_transport_template) { |
| 2169 | fc_remote_port_delete(stor_device->rport); |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 2170 | fc_remove_host(host); |
Cathy Avery | daf0cd4 | 2017-04-17 14:37:46 -0400 | [diff] [blame] | 2171 | } |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 2172 | #endif |
Cathy Avery | 436ad94 | 2017-10-31 08:52:06 -0400 | [diff] [blame] | 2173 | destroy_workqueue(host_dev->handle_error_wq); |
K. Y. Srinivasan | ddcbf65 | 2012-01-12 12:37:59 -0800 | [diff] [blame] | 2174 | scsi_remove_host(host); |
| 2175 | storvsc_dev_remove(dev); |
| 2176 | scsi_host_put(host); |
| 2177 | |
| 2178 | return 0; |
| 2179 | } |
| 2180 | |
Dexuan Cui | 56fb105 | 2019-09-11 23:35:16 +0000 | [diff] [blame] | 2181 | static int storvsc_suspend(struct hv_device *hv_dev) |
| 2182 | { |
| 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); |
| 2186 | |
| 2187 | storvsc_wait_to_drain(stor_device); |
| 2188 | |
| 2189 | drain_workqueue(host_dev->handle_error_wq); |
| 2190 | |
| 2191 | vmbus_close(hv_dev->channel); |
| 2192 | |
Dexuan Cui | 56fb105 | 2019-09-11 23:35:16 +0000 | [diff] [blame] | 2193 | kfree(stor_device->stor_chns); |
| 2194 | stor_device->stor_chns = NULL; |
| 2195 | |
| 2196 | cpumask_clear(&stor_device->alloced_cpus); |
| 2197 | |
| 2198 | return 0; |
| 2199 | } |
| 2200 | |
| 2201 | static int storvsc_resume(struct hv_device *hv_dev) |
| 2202 | { |
| 2203 | int ret; |
| 2204 | |
| 2205 | ret = storvsc_connect_to_vsp(hv_dev, storvsc_ringbuffer_size, |
| 2206 | hv_dev_is_fc(hv_dev)); |
| 2207 | return ret; |
| 2208 | } |
| 2209 | |
K. Y. Srinivasan | 40bf63e | 2011-05-10 07:56:09 -0700 | [diff] [blame] | 2210 | static struct hv_driver storvsc_drv = { |
K. Y. Srinivasan | fafb0ef | 2011-11-08 09:01:46 -0800 | [diff] [blame] | 2211 | .name = KBUILD_MODNAME, |
K. Y. Srinivasan | d847b5f | 2011-08-25 09:48:33 -0700 | [diff] [blame] | 2212 | .id_table = id_table, |
K. Y. Srinivasan | 40bf63e | 2011-05-10 07:56:09 -0700 | [diff] [blame] | 2213 | .probe = storvsc_probe, |
| 2214 | .remove = storvsc_remove, |
Dexuan Cui | 56fb105 | 2019-09-11 23:35:16 +0000 | [diff] [blame] | 2215 | .suspend = storvsc_suspend, |
| 2216 | .resume = storvsc_resume, |
Arjan van de Ven | af0a564 | 2018-06-05 13:37:49 -0700 | [diff] [blame] | 2217 | .driver = { |
| 2218 | .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
| 2219 | }, |
K. Y. Srinivasan | 39ae6fa | 2011-05-10 07:54:46 -0700 | [diff] [blame] | 2220 | }; |
K. Y. Srinivasan | 7bd05b9 | 2011-05-10 07:54:45 -0700 | [diff] [blame] | 2221 | |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 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, |
| 2226 | }; |
| 2227 | #endif |
| 2228 | |
K. Y. Srinivasan | d9bbae8 | 2011-06-06 15:49:27 -0700 | [diff] [blame] | 2229 | static int __init storvsc_drv_init(void) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 2230 | { |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 2231 | int ret; |
K. Y. Srinivasan | 01415ab3 | 2011-05-10 07:55:58 -0700 | [diff] [blame] | 2232 | |
| 2233 | /* |
| 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) |
Andrea Parri (Microsoft) | ab548fd | 2020-12-17 21:33:19 +0100 | [diff] [blame] | 2238 | * |
| 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. |
K. Y. Srinivasan | 01415ab3 | 2011-05-10 07:55:58 -0700 | [diff] [blame] | 2242 | */ |
K. Y. Srinivasan | 01415ab3 | 2011-05-10 07:55:58 -0700 | [diff] [blame] | 2243 | max_outstanding_req_per_channel = |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame] | 2244 | ((storvsc_ringbuffer_size - PAGE_SIZE) / |
| 2245 | ALIGN(MAX_MULTIPAGE_BUFFER_PACKET + |
Andrea Parri (Microsoft) | ab548fd | 2020-12-17 21:33:19 +0100 | [diff] [blame] | 2246 | sizeof(struct vstor_packet) + sizeof(u64), |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame] | 2247 | sizeof(u64))); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 2248 | |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 2249 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) |
| 2250 | fc_transport_template = fc_attach_transport(&fc_transport_functions); |
| 2251 | if (!fc_transport_template) |
| 2252 | return -ENODEV; |
| 2253 | #endif |
| 2254 | |
| 2255 | ret = vmbus_driver_register(&storvsc_drv); |
| 2256 | |
| 2257 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) |
| 2258 | if (ret) |
| 2259 | fc_release_transport(fc_transport_template); |
| 2260 | #endif |
| 2261 | |
| 2262 | return ret; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 2263 | } |
| 2264 | |
K. Y. Srinivasan | c63ba9e | 2011-06-06 15:49:26 -0700 | [diff] [blame] | 2265 | static void __exit storvsc_drv_exit(void) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 2266 | { |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame] | 2267 | vmbus_driver_unregister(&storvsc_drv); |
K. Y. Srinivasan | dac5824 | 2015-12-23 13:15:49 -0800 | [diff] [blame] | 2268 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) |
| 2269 | fc_release_transport(fc_transport_template); |
| 2270 | #endif |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 2271 | } |
| 2272 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 2273 | MODULE_LICENSE("GPL"); |
Stephen Hemminger | 3afc7cc3 | 2010-05-06 21:44:45 -0700 | [diff] [blame] | 2274 | MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver"); |
K. Y. Srinivasan | d9bbae8 | 2011-06-06 15:49:27 -0700 | [diff] [blame] | 2275 | module_init(storvsc_drv_init); |
K. Y. Srinivasan | c63ba9e | 2011-06-06 15:49:26 -0700 | [diff] [blame] | 2276 | module_exit(storvsc_drv_exit); |