Commit | Line | Data |
---|---|---|
3b20eb23 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
bef4a34a | 2 | /* |
bef4a34a HJ |
3 | * Copyright (c) 2009, Microsoft Corporation. |
4 | * | |
bef4a34a HJ |
5 | * Authors: |
6 | * Haiyang Zhang <haiyangz@microsoft.com> | |
7 | * Hank Janssen <hjanssen@microsoft.com> | |
972621c9 | 8 | * K. Y. Srinivasan <kys@microsoft.com> |
bef4a34a | 9 | */ |
a1be1706 S |
10 | |
11 | #include <linux/kernel.h> | |
f0d79fe9 | 12 | #include <linux/wait.h> |
a1be1706 S |
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> | |
bef4a34a | 18 | #include <linux/init.h> |
5a0e3ad6 | 19 | #include <linux/slab.h> |
bef4a34a HJ |
20 | #include <linux/module.h> |
21 | #include <linux/device.h> | |
46a97191 | 22 | #include <linux/hyperv.h> |
56b26e69 | 23 | #include <linux/blkdev.h> |
743b237c TL |
24 | #include <linux/dma-mapping.h> |
25 | ||
bef4a34a HJ |
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> | |
bef4a34a | 33 | #include <scsi/scsi_dbg.h> |
dac58241 | 34 | #include <scsi/scsi_transport_fc.h> |
d791a8c6 | 35 | #include <scsi/scsi_transport.h> |
3f335ea2 | 36 | |
09f0355f | 37 | /* |
af9584b8 S |
38 | * All wire protocol details (storage protocol between the guest and the host) |
39 | * are consolidated here. | |
40 | * | |
41 | * Begin protocol definitions. | |
09f0355f S |
42 | */ |
43 | ||
09f0355f S |
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 | |
8b612fa2 | 50 | * Win8: 5.1 |
1a363108 KM |
51 | * Win8.1: 6.0 |
52 | * Win10: 6.2 | |
09f0355f S |
53 | */ |
54 | ||
2492fd7a KM |
55 | #define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \ |
56 | (((MINOR_) & 0xff))) | |
8b612fa2 | 57 | |
1a363108 | 58 | #define VMSTOR_PROTO_VERSION_WIN6 VMSTOR_PROTO_VERSION(2, 0) |
2492fd7a KM |
59 | #define VMSTOR_PROTO_VERSION_WIN7 VMSTOR_PROTO_VERSION(4, 2) |
60 | #define VMSTOR_PROTO_VERSION_WIN8 VMSTOR_PROTO_VERSION(5, 1) | |
1a363108 KM |
61 | #define VMSTOR_PROTO_VERSION_WIN8_1 VMSTOR_PROTO_VERSION(6, 0) |
62 | #define VMSTOR_PROTO_VERSION_WIN10 VMSTOR_PROTO_VERSION(6, 2) | |
f0d79fe9 | 63 | |
f0d79fe9 S |
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, | |
2b9525f5 | 76 | VSTOR_OPERATION_ENUMERATE_BUS = 11, |
8b612fa2 S |
77 | VSTOR_OPERATION_FCHBA_DATA = 12, |
78 | VSTOR_OPERATION_CREATE_SUB_CHANNELS = 13, | |
79 | VSTOR_OPERATION_MAXIMUM = 13 | |
80 | }; | |
81 | ||
82 | /* | |
83 | * WWN packet for Fibre Channel HBA | |
84 | */ | |
85 | ||
86 | struct hv_fc_wwn_packet { | |
83d1e8b9 S |
87 | u8 primary_active; |
88 | u8 reserved1[3]; | |
8b612fa2 S |
89 | u8 primary_port_wwn[8]; |
90 | u8 primary_node_wwn[8]; | |
91 | u8 secondary_port_wwn[8]; | |
92 | u8 secondary_node_wwn[8]; | |
f0d79fe9 S |
93 | }; |
94 | ||
8b612fa2 S |
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 | ||
3cd6d3d9 LL |
129 | #define SP_UNTAGGED ((unsigned char) ~0) |
130 | #define SRB_SIMPLE_TAG_REQUEST 0x20 | |
8b612fa2 | 131 | |
f0d79fe9 S |
132 | /* |
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 | */ | |
6b2f9495 | 137 | #define STORVSC_MAX_CMD_LEN 0x10 |
8b612fa2 S |
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 | |
6b2f9495 | 143 | #define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14 |
f0d79fe9 | 144 | |
8b612fa2 S |
145 | /* |
146 | * Sense buffer size changed in win8; have a run-time | |
cb11fead KM |
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. | |
8b612fa2 | 150 | */ |
cb11fead | 151 | static int sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE; |
8b612fa2 S |
152 | |
153 | /* | |
cb11fead KM |
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 | */ | |
2492fd7a | 158 | static int vmstor_proto_version; |
8b612fa2 | 159 | |
f8aea701 LL |
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 | ||
8b612fa2 S |
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 | ||
f0d79fe9 | 192 | struct vmscsi_request { |
c649114a S |
193 | u16 length; |
194 | u8 srb_status; | |
195 | u8 scsi_status; | |
f0d79fe9 | 196 | |
c649114a S |
197 | u8 port_number; |
198 | u8 path_id; | |
199 | u8 target_id; | |
200 | u8 lun; | |
f0d79fe9 | 201 | |
c649114a S |
202 | u8 cdb_length; |
203 | u8 sense_info_length; | |
204 | u8 data_in; | |
205 | u8 reserved; | |
f0d79fe9 | 206 | |
c649114a | 207 | u32 data_transfer_length; |
f0d79fe9 S |
208 | |
209 | union { | |
6b2f9495 S |
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]; | |
f0d79fe9 | 213 | }; |
8b612fa2 S |
214 | /* |
215 | * The following was added in win8. | |
216 | */ | |
217 | struct vmscsi_win8_extension win8_extension; | |
218 | ||
f0d79fe9 S |
219 | } __attribute((packed)); |
220 | ||
1a363108 KM |
221 | /* |
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 | ||
f0d79fe9 | 260 | /* |
183b8021 | 261 | * This structure is sent during the initialization phase to get the different |
f0d79fe9 S |
262 | * properties of the channel. |
263 | */ | |
8b612fa2 S |
264 | |
265 | #define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1 | |
266 | ||
f0d79fe9 | 267 | struct vmstorage_channel_properties { |
8b612fa2 S |
268 | u32 reserved; |
269 | u16 max_channel_cnt; | |
270 | u16 reserved1; | |
f0d79fe9 | 271 | |
8b612fa2 | 272 | u32 flags; |
c649114a | 273 | u32 max_transfer_bytes; |
f0d79fe9 | 274 | |
8b612fa2 | 275 | u64 reserved2; |
f0d79fe9 S |
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. */ | |
85904a5e | 281 | u16 major_minor; |
f0d79fe9 S |
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. | |
c649114a | 288 | * This is only used on the windows side. Just set it to 0. |
f0d79fe9 | 289 | */ |
85904a5e | 290 | u16 revision; |
f0d79fe9 S |
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 */ | |
c649114a | 302 | u32 flags; |
f0d79fe9 S |
303 | |
304 | /* Status of the request returned from the server side. */ | |
c649114a | 305 | u32 status; |
f0d79fe9 S |
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; | |
8b612fa2 S |
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]; | |
f0d79fe9 S |
329 | }; |
330 | } __packed; | |
331 | ||
f0d79fe9 | 332 | /* |
09f0355f S |
333 | * Packet Flags: |
334 | * | |
f0d79fe9 S |
335 | * This flag indicates that the server should send back a completion for this |
336 | * packet. | |
337 | */ | |
09f0355f | 338 | |
f0d79fe9 S |
339 | #define REQUEST_COMPLETION_FLAG 0x1 |
340 | ||
f0d79fe9 S |
341 | /* Matches Windows-end */ |
342 | enum storvsc_request_type { | |
c649114a | 343 | WRITE_TYPE = 0, |
f0d79fe9 S |
344 | READ_TYPE, |
345 | UNKNOWN_TYPE, | |
346 | }; | |
347 | ||
16046320 S |
348 | /* |
349 | * SRB status codes and masks; a subset of the codes used here. | |
350 | */ | |
351 | ||
352 | #define SRB_STATUS_AUTOSENSE_VALID 0x80 | |
3209f9d7 | 353 | #define SRB_STATUS_QUEUE_FROZEN 0x40 |
16046320 S |
354 | #define SRB_STATUS_INVALID_LUN 0x20 |
355 | #define SRB_STATUS_SUCCESS 0x01 | |
6781209e | 356 | #define SRB_STATUS_ABORTED 0x02 |
16046320 | 357 | #define SRB_STATUS_ERROR 0x04 |
40630f46 | 358 | #define SRB_STATUS_DATA_OVERRUN 0x12 |
16046320 | 359 | |
3209f9d7 S |
360 | #define SRB_STATUS(status) \ |
361 | (status & ~(SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_QUEUE_FROZEN)) | |
af9584b8 S |
362 | /* |
363 | * This is the end of Protocol specific defines. | |
364 | */ | |
365 | ||
ed2e63aa | 366 | static int storvsc_ringbuffer_size = (128 * 1024); |
f458aada | 367 | static u32 max_outstanding_req_per_channel; |
adfbd028 | 368 | static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth); |
f458aada S |
369 | |
370 | static int storvsc_vcpus_per_sub_channel = 4; | |
a81a38cc | 371 | static unsigned int storvsc_max_hw_queues; |
af9584b8 S |
372 | |
373 | module_param(storvsc_ringbuffer_size, int, S_IRUGO); | |
374 | MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)"); | |
375 | ||
a81a38cc MPM |
376 | module_param(storvsc_max_hw_queues, uint, 0644); |
377 | MODULE_PARM_DESC(storvsc_max_hw_queues, "Maximum number of hardware queues"); | |
378 | ||
f458aada | 379 | module_param(storvsc_vcpus_per_sub_channel, int, S_IRUGO); |
74e26784 | 380 | MODULE_PARM_DESC(storvsc_vcpus_per_sub_channel, "Ratio of VCPUs to subchannels"); |
2217a47d LL |
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 | ||
893def38 S |
387 | /* |
388 | * Timeout in seconds for all devices managed by this driver. | |
389 | */ | |
390 | static int storvsc_timeout = 180; | |
391 | ||
dac58241 S |
392 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) |
393 | static struct scsi_transport_template *fc_transport_template; | |
394 | #endif | |
af9584b8 | 395 | |
453de21c | 396 | static struct scsi_host_template scsi_driver; |
6f94d5de S |
397 | static void storvsc_on_channel_callback(void *context); |
398 | ||
4cd83ecd S |
399 | #define STORVSC_MAX_LUNS_PER_TARGET 255 |
400 | #define STORVSC_MAX_TARGETS 2 | |
401 | #define STORVSC_MAX_CHANNELS 8 | |
af9584b8 | 402 | |
4cd83ecd S |
403 | #define STORVSC_FC_MAX_LUNS_PER_TARGET 255 |
404 | #define STORVSC_FC_MAX_TARGETS 128 | |
405 | #define STORVSC_FC_MAX_CHANNELS 8 | |
af9584b8 | 406 | |
4cd83ecd S |
407 | #define STORVSC_IDE_MAX_LUNS_PER_TARGET 64 |
408 | #define STORVSC_IDE_MAX_TARGETS 1 | |
409 | #define STORVSC_IDE_MAX_CHANNELS 1 | |
16046320 | 410 | |
adae1e93 AB |
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 | ||
61eaffc9 | 419 | struct storvsc_cmd_request { |
61eaffc9 S |
420 | struct scsi_cmnd *cmd; |
421 | ||
f0d79fe9 S |
422 | struct hv_device *device; |
423 | ||
424 | /* Synchronize the request/response if needed */ | |
425 | struct completion wait_event; | |
426 | ||
be0cf6ca S |
427 | struct vmbus_channel_packet_multipage_buffer mpb; |
428 | struct vmbus_packet_mpb_array *payload; | |
429 | u32 payload_sz; | |
430 | ||
f0d79fe9 S |
431 | struct vstor_packet vstor_packet; |
432 | }; | |
433 | ||
434 | ||
f0d79fe9 S |
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; | |
cd654ea1 | 442 | struct Scsi_Host *host; |
f0d79fe9 S |
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 | ||
244808e0 APM |
455 | /* |
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 | ||
5117b936 S |
466 | /* |
467 | * Max I/O, the device can support. | |
468 | */ | |
469 | u32 max_transfer_bytes; | |
d86adf48 S |
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; | |
21d2052c APM |
479 | /* |
480 | * Serializes modifications of stor_chns[] from storvsc_do_io() | |
481 | * and storvsc_change_target_cpu(). | |
482 | */ | |
483 | spinlock_t lock; | |
f0d79fe9 | 484 | /* Used for vsc/vsp channel reset process */ |
61eaffc9 S |
485 | struct storvsc_cmd_request init_request; |
486 | struct storvsc_cmd_request reset_request; | |
dac58241 S |
487 | /* |
488 | * Currently active port and node names for FC devices. | |
489 | */ | |
490 | u64 node_name; | |
491 | u64 port_name; | |
daf0cd44 CA |
492 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) |
493 | struct fc_rport *rport; | |
494 | #endif | |
f0d79fe9 S |
495 | }; |
496 | ||
ce3e301c S |
497 | struct hv_host_device { |
498 | struct hv_device *dev; | |
c1b3d067 S |
499 | unsigned int port; |
500 | unsigned char path; | |
501 | unsigned char target; | |
436ad941 | 502 | struct workqueue_struct *handle_error_wq; |
c58cc70f LL |
503 | struct work_struct host_scan_work; |
504 | struct Scsi_Host *host; | |
c1b3d067 S |
505 | }; |
506 | ||
12675799 S |
507 | struct storvsc_scan_work { |
508 | struct work_struct work; | |
509 | struct Scsi_Host *host; | |
98441221 S |
510 | u8 lun; |
511 | u8 tgt_id; | |
12675799 S |
512 | }; |
513 | ||
6781209e S |
514 | static void storvsc_device_scan(struct work_struct *work) |
515 | { | |
516 | struct storvsc_scan_work *wrk; | |
6781209e S |
517 | struct scsi_device *sdev; |
518 | ||
519 | wrk = container_of(work, struct storvsc_scan_work, work); | |
6781209e | 520 | |
98441221 | 521 | sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun); |
6781209e S |
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 | ||
2a09ed3d | 531 | static void storvsc_host_scan(struct work_struct *work) |
12675799 | 532 | { |
2a09ed3d | 533 | struct Scsi_Host *host; |
34a716bc | 534 | struct scsi_device *sdev; |
c58cc70f LL |
535 | struct hv_host_device *host_device = |
536 | container_of(work, struct hv_host_device, host_scan_work); | |
12675799 | 537 | |
c58cc70f | 538 | host = host_device->host; |
34a716bc S |
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); | |
8d6a9f56 | 551 | shost_for_each_device(sdev, host) |
34a716bc | 552 | scsi_test_unit_ready(sdev, 1, 1, NULL); |
34a716bc S |
553 | mutex_unlock(&host->scan_mutex); |
554 | /* | |
555 | * Now scan the host to discover LUNs that may have been added. | |
556 | */ | |
2a09ed3d | 557 | scsi_scan_host(host); |
12675799 S |
558 | } |
559 | ||
b4017319 S |
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 | ||
98441221 | 569 | sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun); |
b4017319 S |
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 | ||
af9584b8 | 581 | |
a8c18c57 S |
582 | /* |
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 | ||
f0d79fe9 S |
597 | static inline struct storvsc_device *get_out_stor_device( |
598 | struct hv_device *device) | |
599 | { | |
600 | struct storvsc_device *stor_device; | |
601 | ||
cd654ea1 | 602 | stor_device = hv_get_drvdata(device); |
f0d79fe9 S |
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 | } | |
bef4a34a | 618 | |
8dcf37d4 S |
619 | static inline struct storvsc_device *get_in_stor_device( |
620 | struct hv_device *device) | |
621 | { | |
622 | struct storvsc_device *stor_device; | |
8dcf37d4 | 623 | |
cd654ea1 | 624 | stor_device = hv_get_drvdata(device); |
8dcf37d4 S |
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: | |
8dcf37d4 S |
639 | return stor_device; |
640 | ||
641 | } | |
642 | ||
7769e18c APM |
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(). */ | |
21d2052c | 661 | spin_lock_irqsave(&stor_device->lock, flags); |
7769e18c APM |
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 | ||
21d2052c | 698 | spin_unlock_irqrestore(&stor_device->lock, flags); |
7769e18c APM |
699 | } |
700 | ||
bf5fd8ca APM |
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 | */ | |
c5bf198c | 715 | return (u64)blk_mq_unique_tag(scsi_cmd_to_rq(request->cmd)) + 1; |
bf5fd8ca APM |
716 | } |
717 | ||
6f94d5de S |
718 | static void handle_sc_creation(struct vmbus_channel *new_sc) |
719 | { | |
720 | struct hv_device *device = new_sc->primary_channel->device_obj; | |
c9675904 | 721 | struct device *dev = &device->device; |
6f94d5de S |
722 | struct storvsc_device *stor_device; |
723 | struct vmstorage_channel_properties props; | |
c9675904 | 724 | int ret; |
6f94d5de S |
725 | |
726 | stor_device = get_out_stor_device(device); | |
727 | if (!stor_device) | |
728 | return; | |
729 | ||
6f94d5de | 730 | memset(&props, 0, sizeof(struct vmstorage_channel_properties)); |
adae1e93 | 731 | new_sc->max_pkt_size = STORVSC_MAX_PKT_SIZE; |
6f94d5de | 732 | |
bf5fd8ca | 733 | new_sc->next_request_id_callback = storvsc_next_request_id; |
453de21c | 734 | |
c9675904 DC |
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); | |
d86adf48 | 741 | |
c9675904 DC |
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; | |
d86adf48 | 746 | } |
c9675904 | 747 | |
7769e18c APM |
748 | new_sc->change_target_cpu_callback = storvsc_change_target_cpu; |
749 | ||
c9675904 DC |
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); | |
6f94d5de S |
753 | } |
754 | ||
755 | static void handle_multichannel_storage(struct hv_device *device, int max_chns) | |
756 | { | |
c9675904 | 757 | struct device *dev = &device->device; |
6f94d5de | 758 | struct storvsc_device *stor_device; |
6f94d5de S |
759 | int num_sc; |
760 | struct storvsc_cmd_request *request; | |
761 | struct vstor_packet *vstor_packet; | |
762 | int ret, t; | |
763 | ||
382e06d1 MK |
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 | ||
6f94d5de S |
775 | stor_device = get_out_stor_device(device); |
776 | if (!stor_device) | |
777 | return; | |
778 | ||
d86adf48 | 779 | stor_device->num_sc = num_sc; |
6f94d5de S |
780 | request = &stor_device->init_request; |
781 | vstor_packet = &request->vstor_packet; | |
782 | ||
6f94d5de S |
783 | /* |
784 | * Establish a handler for dealing with subchannels. | |
785 | */ | |
786 | vmbus_set_sc_create_callback(device->channel, handle_sc_creation); | |
787 | ||
6f94d5de S |
788 | /* |
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) - | |
244808e0 | 799 | stor_device->vmscsi_size_delta), |
bf5fd8ca | 800 | VMBUS_RQST_INIT, |
6f94d5de S |
801 | VM_PKT_DATA_INBAND, |
802 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
803 | ||
c9675904 DC |
804 | if (ret != 0) { |
805 | dev_err(dev, "Failed to create sub-channel: err=%d\n", ret); | |
6f94d5de | 806 | return; |
c9675904 | 807 | } |
6f94d5de S |
808 | |
809 | t = wait_for_completion_timeout(&request->wait_event, 10*HZ); | |
c9675904 DC |
810 | if (t == 0) { |
811 | dev_err(dev, "Failed to create sub-channel: timed out\n"); | |
6f94d5de | 812 | return; |
c9675904 | 813 | } |
6f94d5de S |
814 | |
815 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || | |
c9675904 DC |
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); | |
6f94d5de | 819 | return; |
c9675904 | 820 | } |
6f94d5de S |
821 | |
822 | /* | |
c9675904 DC |
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). | |
6f94d5de | 826 | */ |
6f94d5de S |
827 | } |
828 | ||
dac58241 S |
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 | ||
59635018 S |
848 | |
849 | static int storvsc_execute_vstor_op(struct hv_device *device, | |
850 | struct storvsc_cmd_request *request, | |
851 | bool status_check) | |
8dcf37d4 | 852 | { |
244808e0 | 853 | struct storvsc_device *stor_device; |
8dcf37d4 | 854 | struct vstor_packet *vstor_packet; |
59635018 | 855 | int ret, t; |
8dcf37d4 | 856 | |
244808e0 APM |
857 | stor_device = get_out_stor_device(device); |
858 | if (!stor_device) | |
859 | return -ENODEV; | |
860 | ||
8dcf37d4 S |
861 | vstor_packet = &request->vstor_packet; |
862 | ||
8dcf37d4 | 863 | init_completion(&request->wait_event); |
8dcf37d4 S |
864 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; |
865 | ||
866 | ret = vmbus_sendpacket(device->channel, vstor_packet, | |
8b612fa2 | 867 | (sizeof(struct vstor_packet) - |
244808e0 | 868 | stor_device->vmscsi_size_delta), |
bf5fd8ca | 869 | VMBUS_RQST_INIT, |
8dcf37d4 S |
870 | VM_PKT_DATA_INBAND, |
871 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
872 | if (ret != 0) | |
dac58241 | 873 | return ret; |
8dcf37d4 S |
874 | |
875 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); | |
dac58241 S |
876 | if (t == 0) |
877 | return -ETIMEDOUT; | |
8dcf37d4 | 878 | |
59635018 S |
879 | if (!status_check) |
880 | return ret; | |
881 | ||
8dcf37d4 | 882 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
dac58241 S |
883 | vstor_packet->status != 0) |
884 | return -EINVAL; | |
8dcf37d4 | 885 | |
59635018 S |
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 | */ | |
8dcf37d4 | 917 | |
1a363108 KM |
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; | |
8dcf37d4 | 923 | |
1a363108 KM |
924 | vstor_packet->version.major_minor = |
925 | vmstor_protocols[i].protocol_version; | |
85904a5e | 926 | |
1a363108 KM |
927 | /* |
928 | * The revision number is only used in Windows; set it to 0. | |
929 | */ | |
930 | vstor_packet->version.revision = 0; | |
59635018 | 931 | ret = storvsc_execute_vstor_op(device, request, false); |
1a363108 | 932 | if (ret != 0) |
dac58241 | 933 | return ret; |
8dcf37d4 | 934 | |
dac58241 S |
935 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO) |
936 | return -EINVAL; | |
1a363108 KM |
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 | ||
244808e0 | 945 | stor_device->vmscsi_size_delta = |
1a363108 KM |
946 | vmstor_protocols[i].vmscsi_size_delta; |
947 | ||
948 | break; | |
949 | } | |
8dcf37d4 S |
950 | } |
951 | ||
dac58241 S |
952 | if (vstor_packet->status != 0) |
953 | return -EINVAL; | |
8dcf37d4 S |
954 | |
955 | ||
956 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); | |
957 | vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES; | |
59635018 | 958 | ret = storvsc_execute_vstor_op(device, request, true); |
8dcf37d4 | 959 | if (ret != 0) |
dac58241 | 960 | return ret; |
8dcf37d4 | 961 | |
6f94d5de S |
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; | |
d86adf48 S |
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 | */ | |
e0408528 | 978 | stor_device->stor_chns = kcalloc(num_possible_cpus(), sizeof(void *), |
d86adf48 S |
979 | GFP_KERNEL); |
980 | if (stor_device->stor_chns == NULL) | |
981 | return -ENOMEM; | |
982 | ||
7769e18c APM |
983 | device->channel->change_target_cpu_callback = storvsc_change_target_cpu; |
984 | ||
d86adf48 S |
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 | ||
e6c4bc66 | 989 | if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN8) { |
6f94d5de S |
990 | if (vstor_packet->storage_channel_properties.flags & |
991 | STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL) | |
992 | process_sub_channels = true; | |
993 | } | |
5117b936 S |
994 | stor_device->max_transfer_bytes = |
995 | vstor_packet->storage_channel_properties.max_transfer_bytes; | |
6f94d5de | 996 | |
dac58241 S |
997 | if (!is_fc) |
998 | goto done; | |
999 | ||
59635018 S |
1000 | /* |
1001 | * For FC devices retrieve FC HBA data. | |
1002 | */ | |
dac58241 S |
1003 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
1004 | vstor_packet->operation = VSTOR_OPERATION_FCHBA_DATA; | |
59635018 | 1005 | ret = storvsc_execute_vstor_op(device, request, true); |
dac58241 S |
1006 | if (ret != 0) |
1007 | return ret; | |
1008 | ||
dac58241 S |
1009 | /* |
1010 | * Cache the currently active port and node ww names. | |
1011 | */ | |
1012 | cache_wwn(stor_device, vstor_packet); | |
1013 | ||
1014 | done: | |
1015 | ||
8dcf37d4 S |
1016 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
1017 | vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION; | |
59635018 | 1018 | ret = storvsc_execute_vstor_op(device, request, true); |
8dcf37d4 | 1019 | if (ret != 0) |
dac58241 | 1020 | return ret; |
8dcf37d4 | 1021 | |
6f94d5de S |
1022 | if (process_sub_channels) |
1023 | handle_multichannel_storage(device, max_chns); | |
1024 | ||
8dcf37d4 S |
1025 | return ret; |
1026 | } | |
1027 | ||
c50bd448 S |
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); | |
436ad941 | 1035 | struct hv_host_device *host_dev = shost_priv(host); |
c50bd448 | 1036 | |
52e1b3b3 MK |
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) { | |
bba5dc33 LL |
1063 | /* |
1064 | * Let upper layer deal with error when | |
1065 | * sense message is present. | |
1066 | */ | |
bba5dc33 | 1067 | if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID) |
52e1b3b3 MK |
1068 | return; |
1069 | ||
c50bd448 S |
1070 | /* |
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; | |
3533f860 | 1081 | /* |
52e1b3b3 MK |
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. | |
3533f860 S |
1085 | */ |
1086 | case TEST_UNIT_READY: | |
1087 | break; | |
c50bd448 | 1088 | default: |
d1b8b239 | 1089 | set_host_byte(scmnd, DID_ERROR); |
c50bd448 | 1090 | } |
c50bd448 | 1091 | } |
52e1b3b3 | 1092 | return; |
6781209e | 1093 | |
52e1b3b3 | 1094 | do_work: |
c50bd448 S |
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; | |
98441221 | 1106 | wrk->tgt_id = vm_srb->target_id; |
c50bd448 | 1107 | INIT_WORK(&wrk->work, process_err_fn); |
436ad941 | 1108 | queue_work(host_dev->handle_error_wq, &wrk->work); |
c50bd448 S |
1109 | } |
1110 | ||
2707388c | 1111 | |
03996f20 S |
1112 | static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request, |
1113 | struct storvsc_device *stor_dev) | |
8dcf37d4 | 1114 | { |
2707388c | 1115 | struct scsi_cmnd *scmnd = cmd_request->cmd; |
2707388c S |
1116 | struct scsi_sense_hdr sense_hdr; |
1117 | struct vmscsi_request *vm_srb; | |
40630f46 | 1118 | u32 data_transfer_length; |
c50bd448 | 1119 | struct Scsi_Host *host; |
be0cf6ca S |
1120 | u32 payload_sz = cmd_request->payload_sz; |
1121 | void *payload = cmd_request->payload; | |
08f76547 | 1122 | bool sense_ok; |
c50bd448 | 1123 | |
c50bd448 | 1124 | host = stor_dev->host; |
8dcf37d4 | 1125 | |
61eaffc9 | 1126 | vm_srb = &cmd_request->vstor_packet.vm_srb; |
40630f46 | 1127 | data_transfer_length = vm_srb->data_transfer_length; |
8dcf37d4 | 1128 | |
42e22cac S |
1129 | scmnd->result = vm_srb->scsi_status; |
1130 | ||
2707388c | 1131 | if (scmnd->result) { |
08f76547 MK |
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)) | |
d811b848 HR |
1136 | scsi_print_sense_hdr(scmnd->device, "storvsc", |
1137 | &sense_hdr); | |
2707388c S |
1138 | } |
1139 | ||
40630f46 | 1140 | if (vm_srb->srb_status != SRB_STATUS_SUCCESS) { |
c50bd448 S |
1141 | storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc, |
1142 | sense_hdr.ascq); | |
40630f46 LL |
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 | } | |
c50bd448 | 1151 | |
0a765665 AB |
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 | ||
2707388c | 1156 | scsi_set_resid(scmnd, |
40630f46 | 1157 | cmd_request->payload->range.len - data_transfer_length); |
2707388c | 1158 | |
0c31fa0e | 1159 | scsi_done(scmnd); |
be0cf6ca S |
1160 | |
1161 | if (payload_sz > | |
1162 | sizeof(struct vmbus_channel_packet_multipage_buffer)) | |
1163 | kfree(payload); | |
2707388c S |
1164 | } |
1165 | ||
03996f20 | 1166 | static void storvsc_on_io_completion(struct storvsc_device *stor_device, |
2707388c | 1167 | struct vstor_packet *vstor_packet, |
61eaffc9 | 1168 | struct storvsc_cmd_request *request) |
2707388c | 1169 | { |
2707388c | 1170 | struct vstor_packet *stor_pkt; |
03996f20 | 1171 | struct hv_device *device = stor_device->device; |
2707388c | 1172 | |
2707388c S |
1173 | stor_pkt = &request->vstor_packet; |
1174 | ||
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 | |
4ed51a21 S |
1183 | * (srb status == 0x4) and off-line the device in that case. |
1184 | */ | |
1185 | ||
1186 | if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) || | |
a8c18c57 | 1187 | (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) { |
4ed51a21 | 1188 | vstor_packet->vm_srb.scsi_status = 0; |
16046320 | 1189 | vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS; |
4ed51a21 S |
1190 | } |
1191 | ||
8dcf37d4 S |
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; | |
0a765665 | 1195 | |
d4674859 MK |
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); | |
8dcf37d4 | 1202 | |
f8aea701 | 1203 | if (vstor_packet->vm_srb.scsi_status != 0 || |
dbe7633c MK |
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, | |
08f76547 | 1215 | "tag#%d cmd 0x%x status: scsi 0x%x srb 0x%x hv 0x%x\n", |
c5bf198c | 1216 | scsi_cmd_to_rq(request->cmd)->tag, |
f8aea701 LL |
1217 | stor_pkt->vm_srb.cdb[0], |
1218 | vstor_packet->vm_srb.scsi_status, | |
08f76547 MK |
1219 | vstor_packet->vm_srb.srb_status, |
1220 | vstor_packet->status); | |
dbe7633c | 1221 | } |
f8aea701 | 1222 | |
d4674859 MK |
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); | |
8dcf37d4 S |
1228 | |
1229 | stor_pkt->vm_srb.data_transfer_length = | |
d4674859 | 1230 | vstor_packet->vm_srb.data_transfer_length; |
8dcf37d4 | 1231 | |
03996f20 | 1232 | storvsc_command_completion(request, stor_device); |
8dcf37d4 S |
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); | |
8dcf37d4 S |
1237 | } |
1238 | ||
03996f20 | 1239 | static void storvsc_on_receive(struct storvsc_device *stor_device, |
8dcf37d4 | 1240 | struct vstor_packet *vstor_packet, |
61eaffc9 | 1241 | struct storvsc_cmd_request *request) |
8dcf37d4 | 1242 | { |
c58cc70f | 1243 | struct hv_host_device *host_dev; |
8dcf37d4 S |
1244 | switch (vstor_packet->operation) { |
1245 | case VSTOR_OPERATION_COMPLETE_IO: | |
03996f20 | 1246 | storvsc_on_io_completion(stor_device, vstor_packet, request); |
8dcf37d4 | 1247 | break; |
12675799 | 1248 | |
8dcf37d4 | 1249 | case VSTOR_OPERATION_REMOVE_DEVICE: |
12675799 | 1250 | case VSTOR_OPERATION_ENUMERATE_BUS: |
c58cc70f LL |
1251 | host_dev = shost_priv(stor_device->host); |
1252 | queue_work( | |
1253 | host_dev->handle_error_wq, &host_dev->host_scan_work); | |
12675799 | 1254 | break; |
8dcf37d4 | 1255 | |
dac58241 | 1256 | case VSTOR_OPERATION_FCHBA_DATA: |
dac58241 S |
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; | |
8dcf37d4 S |
1263 | default: |
1264 | break; | |
1265 | } | |
1266 | } | |
1267 | ||
1268 | static void storvsc_on_channel_callback(void *context) | |
1269 | { | |
6f94d5de | 1270 | struct vmbus_channel *channel = (struct vmbus_channel *)context; |
ddccd952 | 1271 | const struct vmpacket_descriptor *desc; |
6f94d5de | 1272 | struct hv_device *device; |
8dcf37d4 | 1273 | struct storvsc_device *stor_device; |
bf5fd8ca | 1274 | struct Scsi_Host *shost; |
8dcf37d4 | 1275 | |
6f94d5de S |
1276 | if (channel->primary_channel != NULL) |
1277 | device = channel->primary_channel->device_obj; | |
1278 | else | |
1279 | device = channel->device_obj; | |
8dcf37d4 S |
1280 | |
1281 | stor_device = get_in_stor_device(device); | |
1282 | if (!stor_device) | |
1283 | return; | |
1284 | ||
bf5fd8ca | 1285 | shost = stor_device->host; |
ddccd952 | 1286 | |
bf5fd8ca APM |
1287 | foreach_vmbus_pkt(desc, channel) { |
1288 | struct vstor_packet *packet = hv_pkt_data(desc); | |
1289 | struct storvsc_cmd_request *request = NULL; | |
6fd13d69 | 1290 | u32 pktlen = hv_pkt_datalen(desc); |
bf5fd8ca | 1291 | u64 rqst_id = desc->trans_id; |
6fd13d69 APM |
1292 | u32 minlen = rqst_id ? sizeof(struct vstor_packet) - |
1293 | stor_device->vmscsi_size_delta : sizeof(enum vstor_packet_operation); | |
ddccd952 | 1294 | |
6fd13d69 APM |
1295 | if (pktlen < minlen) { |
1296 | dev_err(&device->device, | |
1297 | "Invalid pkt: id=%llu, len=%u, minlen=%u\n", | |
1298 | rqst_id, pktlen, minlen); | |
91b1b640 APM |
1299 | continue; |
1300 | } | |
1301 | ||
bf5fd8ca APM |
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; | |
8dcf37d4 | 1306 | } else { |
bf5fd8ca APM |
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 | |
6fd13d69 APM |
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. | |
bf5fd8ca | 1325 | */ |
6fd13d69 APM |
1326 | if (packet->operation == VSTOR_OPERATION_COMPLETE_IO || |
1327 | packet->operation == VSTOR_OPERATION_FCHBA_DATA) { | |
bf5fd8ca APM |
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); | |
743b237c | 1341 | scsi_dma_unmap(scmnd); |
bf5fd8ca APM |
1342 | } |
1343 | ||
ddccd952 | 1344 | storvsc_on_receive(stor_device, packet, request); |
bf5fd8ca | 1345 | continue; |
8dcf37d4 | 1346 | } |
bf5fd8ca APM |
1347 | |
1348 | memcpy(&request->vstor_packet, packet, | |
1349 | (sizeof(struct vstor_packet) - stor_device->vmscsi_size_delta)); | |
1350 | complete(&request->wait_event); | |
ddccd952 | 1351 | } |
8dcf37d4 S |
1352 | } |
1353 | ||
dac58241 S |
1354 | static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size, |
1355 | bool is_fc) | |
8dcf37d4 S |
1356 | { |
1357 | struct vmstorage_channel_properties props; | |
1358 | int ret; | |
1359 | ||
1360 | memset(&props, 0, sizeof(struct vmstorage_channel_properties)); | |
1361 | ||
adae1e93 | 1362 | device->channel->max_pkt_size = STORVSC_MAX_PKT_SIZE; |
bf5fd8ca | 1363 | device->channel->next_request_id_callback = storvsc_next_request_id; |
453de21c | 1364 | |
8dcf37d4 S |
1365 | ret = vmbus_open(device->channel, |
1366 | ring_size, | |
1367 | ring_size, | |
1368 | (void *)&props, | |
1369 | sizeof(struct vmstorage_channel_properties), | |
6f94d5de | 1370 | storvsc_on_channel_callback, device->channel); |
8dcf37d4 S |
1371 | |
1372 | if (ret != 0) | |
1373 | return ret; | |
1374 | ||
dac58241 | 1375 | ret = storvsc_channel_init(device, is_fc); |
8dcf37d4 S |
1376 | |
1377 | return ret; | |
1378 | } | |
1379 | ||
c1b3d067 | 1380 | static int storvsc_dev_remove(struct hv_device *device) |
8dcf37d4 S |
1381 | { |
1382 | struct storvsc_device *stor_device; | |
8dcf37d4 | 1383 | |
cd654ea1 | 1384 | stor_device = hv_get_drvdata(device); |
8dcf37d4 | 1385 | |
8dcf37d4 | 1386 | stor_device->destroy = true; |
2371cd90 SH |
1387 | |
1388 | /* Make sure flag is set before waiting */ | |
1389 | wmb(); | |
8dcf37d4 S |
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 | */ | |
cd654ea1 | 1406 | hv_set_drvdata(device, NULL); |
8dcf37d4 S |
1407 | |
1408 | /* Close the channel */ | |
1409 | vmbus_close(device->channel); | |
1410 | ||
d86adf48 | 1411 | kfree(stor_device->stor_chns); |
8dcf37d4 S |
1412 | kfree(stor_device); |
1413 | return 0; | |
1414 | } | |
1415 | ||
d86adf48 S |
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; | |
1b25a8c4 | 1421 | const struct cpumask *node_mask; |
d86adf48 S |
1422 | int num_channels, tgt_cpu; |
1423 | ||
7769e18c APM |
1424 | if (stor_device->num_sc == 0) { |
1425 | stor_device->stor_chns[q_num] = stor_device->device->channel; | |
d86adf48 | 1426 | return stor_device->device->channel; |
7769e18c | 1427 | } |
d86adf48 S |
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) | |
d86adf48 S |
1436 | */ |
1437 | ||
1b25a8c4 | 1438 | node_mask = cpumask_of_node(cpu_to_node(q_num)); |
d86adf48 | 1439 | |
1b25a8c4 MK |
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 | } | |
7769e18c APM |
1445 | if (num_channels == 0) { |
1446 | stor_device->stor_chns[q_num] = stor_device->device->channel; | |
d86adf48 | 1447 | return stor_device->device->channel; |
7769e18c | 1448 | } |
d86adf48 S |
1449 | |
1450 | hash_qnum = q_num; | |
1451 | while (hash_qnum >= num_channels) | |
1452 | hash_qnum -= num_channels; | |
1453 | ||
1b25a8c4 MK |
1454 | for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) { |
1455 | if (!cpumask_test_cpu(tgt_cpu, node_mask)) | |
1456 | continue; | |
d86adf48 S |
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 | ||
c1b3d067 | 1468 | static int storvsc_do_io(struct hv_device *device, |
d86adf48 | 1469 | struct storvsc_cmd_request *request, u16 q_num) |
8dcf37d4 S |
1470 | { |
1471 | struct storvsc_device *stor_device; | |
1472 | struct vstor_packet *vstor_packet; | |
2217a47d | 1473 | struct vmbus_channel *outgoing_channel, *channel; |
7769e18c | 1474 | unsigned long flags; |
8dcf37d4 | 1475 | int ret = 0; |
1b25a8c4 | 1476 | const struct cpumask *node_mask; |
d86adf48 | 1477 | int tgt_cpu; |
8dcf37d4 S |
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; | |
6f94d5de | 1487 | /* |
7769e18c | 1488 | * Select an appropriate channel to send the request out. |
6f94d5de | 1489 | */ |
7769e18c APM |
1490 | /* See storvsc_change_target_cpu(). */ |
1491 | outgoing_channel = READ_ONCE(stor_device->stor_chns[q_num]); | |
1492 | if (outgoing_channel != NULL) { | |
2217a47d | 1493 | if (outgoing_channel->target_cpu == q_num) { |
d86adf48 S |
1494 | /* |
1495 | * Ideally, we want to pick a different channel if | |
1496 | * available on the same NUMA node. | |
1497 | */ | |
1b25a8c4 MK |
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; | |
2217a47d LL |
1503 | if (tgt_cpu == q_num) |
1504 | continue; | |
7769e18c APM |
1505 | channel = READ_ONCE( |
1506 | stor_device->stor_chns[tgt_cpu]); | |
1507 | if (channel == NULL) | |
1508 | continue; | |
2217a47d LL |
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 | */ | |
1b25a8c4 MK |
1531 | for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) { |
1532 | if (cpumask_test_cpu(tgt_cpu, node_mask)) | |
1533 | continue; | |
7769e18c APM |
1534 | channel = READ_ONCE( |
1535 | stor_device->stor_chns[tgt_cpu]); | |
1536 | if (channel == NULL) | |
1537 | continue; | |
2217a47d LL |
1538 | if (hv_get_avail_to_write_percent( |
1539 | &channel->outbound) | |
1540 | > ring_avail_percent_lowater) { | |
1541 | outgoing_channel = channel; | |
1542 | goto found_channel; | |
d86adf48 S |
1543 | } |
1544 | } | |
1545 | } | |
1546 | } else { | |
21d2052c | 1547 | spin_lock_irqsave(&stor_device->lock, flags); |
7769e18c APM |
1548 | outgoing_channel = stor_device->stor_chns[q_num]; |
1549 | if (outgoing_channel != NULL) { | |
21d2052c | 1550 | spin_unlock_irqrestore(&stor_device->lock, flags); |
7769e18c APM |
1551 | goto found_channel; |
1552 | } | |
d86adf48 | 1553 | outgoing_channel = get_og_chn(stor_device, q_num); |
21d2052c | 1554 | spin_unlock_irqrestore(&stor_device->lock, flags); |
d86adf48 | 1555 | } |
8dcf37d4 | 1556 | |
2217a47d | 1557 | found_channel: |
8dcf37d4 S |
1558 | vstor_packet->flags |= REQUEST_COMPLETION_FLAG; |
1559 | ||
8b612fa2 | 1560 | vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) - |
244808e0 | 1561 | stor_device->vmscsi_size_delta); |
8dcf37d4 S |
1562 | |
1563 | ||
8b612fa2 | 1564 | vstor_packet->vm_srb.sense_info_length = sense_buffer_size; |
8dcf37d4 S |
1565 | |
1566 | ||
1567 | vstor_packet->vm_srb.data_transfer_length = | |
be0cf6ca | 1568 | request->payload->range.len; |
8dcf37d4 S |
1569 | |
1570 | vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB; | |
1571 | ||
be0cf6ca S |
1572 | if (request->payload->range.len) { |
1573 | ||
1574 | ret = vmbus_sendpacket_mpb_desc(outgoing_channel, | |
1575 | request->payload, request->payload_sz, | |
8dcf37d4 | 1576 | vstor_packet, |
8b612fa2 | 1577 | (sizeof(struct vstor_packet) - |
244808e0 | 1578 | stor_device->vmscsi_size_delta), |
8dcf37d4 S |
1579 | (unsigned long)request); |
1580 | } else { | |
0147dabc | 1581 | ret = vmbus_sendpacket(outgoing_channel, vstor_packet, |
8b612fa2 | 1582 | (sizeof(struct vstor_packet) - |
244808e0 | 1583 | stor_device->vmscsi_size_delta), |
8dcf37d4 S |
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 | ||
f1c635b4 SH |
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 | ||
419f2d03 S |
1613 | static int storvsc_device_configure(struct scsi_device *sdevice) |
1614 | { | |
893def38 S |
1615 | blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ)); |
1616 | ||
3e8f4f40 OH |
1617 | sdevice->no_write_same = 1; |
1618 | ||
b0a93d96 S |
1619 | /* |
1620 | * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3 | |
b95f5be0 KM |
1621 | * if the device is a MSFT virtual device. If the host is |
1622 | * WIN10 or newer, allow write_same. | |
b0a93d96 S |
1623 | */ |
1624 | if (!strncmp(sdevice->vendor, "Msft", 4)) { | |
e6c4bc66 KM |
1625 | switch (vmstor_proto_version) { |
1626 | case VMSTOR_PROTO_VERSION_WIN8: | |
1627 | case VMSTOR_PROTO_VERSION_WIN8_1: | |
b0a93d96 S |
1628 | sdevice->scsi_level = SCSI_SPC_3; |
1629 | break; | |
1630 | } | |
b95f5be0 KM |
1631 | |
1632 | if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN10) | |
1633 | sdevice->no_write_same = 0; | |
b0a93d96 S |
1634 | } |
1635 | ||
419f2d03 S |
1636 | return 0; |
1637 | } | |
1638 | ||
62838ce2 S |
1639 | static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, |
1640 | sector_t capacity, int *info) | |
1641 | { | |
5326fd5c S |
1642 | sector_t nsect = capacity; |
1643 | sector_t cylinders = nsect; | |
1644 | int heads, sectors_pt; | |
62838ce2 | 1645 | |
5326fd5c S |
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; | |
62838ce2 S |
1654 | |
1655 | info[0] = heads; | |
5326fd5c S |
1656 | info[1] = sectors_pt; |
1657 | info[2] = (int)cylinders; | |
62838ce2 | 1658 | |
62838ce2 S |
1659 | return 0; |
1660 | } | |
aa3d789e | 1661 | |
4b270c8b | 1662 | static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd) |
aa3d789e | 1663 | { |
4b270c8b S |
1664 | struct hv_host_device *host_dev = shost_priv(scmnd->device->host); |
1665 | struct hv_device *device = host_dev->dev; | |
1666 | ||
aa3d789e | 1667 | struct storvsc_device *stor_device; |
61eaffc9 | 1668 | struct storvsc_cmd_request *request; |
aa3d789e S |
1669 | struct vstor_packet *vstor_packet; |
1670 | int ret, t; | |
1671 | ||
1eaaddf9 | 1672 | stor_device = get_out_stor_device(device); |
aa3d789e | 1673 | if (!stor_device) |
a00e8224 | 1674 | return FAILED; |
aa3d789e S |
1675 | |
1676 | request = &stor_device->reset_request; | |
1677 | vstor_packet = &request->vstor_packet; | |
0a765665 | 1678 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
aa3d789e S |
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, | |
8b612fa2 | 1687 | (sizeof(struct vstor_packet) - |
244808e0 | 1688 | stor_device->vmscsi_size_delta), |
bf5fd8ca | 1689 | VMBUS_RQST_RESET, |
aa3d789e S |
1690 | VM_PKT_DATA_INBAND, |
1691 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
1692 | if (ret != 0) | |
a00e8224 | 1693 | return FAILED; |
aa3d789e | 1694 | |
46d2eb6d | 1695 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); |
a00e8224 S |
1696 | if (t == 0) |
1697 | return TIMEOUT_ERROR; | |
aa3d789e | 1698 | |
aa3d789e S |
1699 | |
1700 | /* | |
1701 | * At this point, all outstanding requests in the adapter | |
1702 | * should have been flushed out and return to us | |
5c1b10ab S |
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. | |
aa3d789e | 1707 | */ |
5c1b10ab | 1708 | storvsc_wait_to_drain(stor_device); |
aa3d789e | 1709 | |
a00e8224 | 1710 | return SUCCESS; |
aa3d789e S |
1711 | } |
1712 | ||
56b26e69 S |
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 | { | |
3930d730 LL |
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 | |
56b26e69 S |
1724 | return BLK_EH_RESET_TIMER; |
1725 | } | |
1726 | ||
c77b63b6 | 1727 | static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd) |
92ae4ebd OH |
1728 | { |
1729 | bool allowed = true; | |
1730 | u8 scsi_op = scmnd->cmnd[0]; | |
1731 | ||
1732 | switch (scsi_op) { | |
3e8f4f40 OH |
1733 | /* the host does not handle WRITE_SAME, log accident usage */ |
1734 | case WRITE_SAME: | |
c77b63b6 S |
1735 | /* |
1736 | * smartd sends this command and the host does not handle | |
1737 | * this. So, don't send it. | |
1738 | */ | |
41098f8f | 1739 | case SET_WINDOW: |
d4674859 | 1740 | set_host_byte(scmnd, DID_ERROR); |
41098f8f S |
1741 | allowed = false; |
1742 | break; | |
1743 | default: | |
1744 | break; | |
92ae4ebd OH |
1745 | } |
1746 | return allowed; | |
1747 | } | |
c5b463ae | 1748 | |
bab445e1 | 1749 | static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd) |
c5b463ae S |
1750 | { |
1751 | int ret; | |
bab445e1 | 1752 | struct hv_host_device *host_dev = shost_priv(host); |
c5b463ae | 1753 | struct hv_device *dev = host_dev->dev; |
ead3700d | 1754 | struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd); |
c5b463ae | 1755 | struct scatterlist *sgl; |
c5b463ae | 1756 | struct vmscsi_request *vm_srb; |
be0cf6ca S |
1757 | struct vmbus_packet_mpb_array *payload; |
1758 | u32 payload_sz; | |
1759 | u32 length; | |
c5b463ae | 1760 | |
2492fd7a | 1761 | if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) { |
8caf92d8 S |
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)) { | |
0c31fa0e | 1771 | scsi_done(scmnd); |
8caf92d8 S |
1772 | return 0; |
1773 | } | |
92ae4ebd | 1774 | } |
c5b463ae | 1775 | |
c5b463ae | 1776 | /* Setup the cmd request */ |
c5b463ae S |
1777 | cmd_request->cmd = scmnd; |
1778 | ||
0a765665 | 1779 | memset(&cmd_request->vstor_packet, 0, sizeof(struct vstor_packet)); |
61eaffc9 | 1780 | vm_srb = &cmd_request->vstor_packet.vm_srb; |
8b612fa2 | 1781 | vm_srb->win8_extension.time_out_value = 60; |
c5b463ae | 1782 | |
f885fb73 | 1783 | vm_srb->win8_extension.srb_flags |= |
8cf308e1 | 1784 | SRB_FLAGS_DISABLE_SYNCH_TRANSFER; |
c5b463ae | 1785 | |
3cd6d3d9 LL |
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 | ||
c5b463ae S |
1793 | /* Build the SRB */ |
1794 | switch (scmnd->sc_data_direction) { | |
1795 | case DMA_TO_DEVICE: | |
1796 | vm_srb->data_in = WRITE_TYPE; | |
8b612fa2 | 1797 | vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT; |
c5b463ae S |
1798 | break; |
1799 | case DMA_FROM_DEVICE: | |
1800 | vm_srb->data_in = READ_TYPE; | |
8b612fa2 | 1801 | vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN; |
c5b463ae | 1802 | break; |
cb1cf080 | 1803 | case DMA_NONE: |
c5b463ae | 1804 | vm_srb->data_in = UNKNOWN_TYPE; |
dc45708c | 1805 | vm_srb->win8_extension.srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER; |
c5b463ae | 1806 | break; |
cb1cf080 VK |
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; | |
c5b463ae S |
1815 | } |
1816 | ||
c5b463ae | 1817 | |
c5b463ae S |
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 | ||
c5b463ae S |
1823 | vm_srb->cdb_length = scmnd->cmd_len; |
1824 | ||
1825 | memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length); | |
1826 | ||
be0cf6ca | 1827 | sgl = (struct scatterlist *)scsi_sglist(scmnd); |
c5b463ae | 1828 | |
be0cf6ca S |
1829 | length = scsi_bufflen(scmnd); |
1830 | payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb; | |
1831 | payload_sz = sizeof(cmd_request->mpb); | |
1832 | ||
0bd2fbee | 1833 | if (scsi_sg_count(scmnd)) { |
3d9c3dcc | 1834 | unsigned long offset_in_hvpg = offset_in_hvpage(sgl->offset); |
8f437105 | 1835 | unsigned int hvpg_count = HVPFN_UP(offset_in_hvpg + length); |
743b237c TL |
1836 | struct scatterlist *sg; |
1837 | unsigned long hvpfn, hvpfns_to_add; | |
0bd2fbee | 1838 | int j, i = 0, sg_count; |
be0cf6ca | 1839 | |
8f437105 BF |
1840 | if (hvpg_count > MAX_PAGE_BUFFER_COUNT) { |
1841 | ||
1842 | payload_sz = (hvpg_count * sizeof(u64) + | |
be0cf6ca | 1843 | sizeof(struct vmbus_packet_mpb_array)); |
b0120d99 | 1844 | payload = kzalloc(payload_sz, GFP_ATOMIC); |
81988a0e VK |
1845 | if (!payload) |
1846 | return SCSI_MLQUEUE_DEVICE_BUSY; | |
be0cf6ca S |
1847 | } |
1848 | ||
1849 | payload->range.len = length; | |
8f437105 | 1850 | payload->range.offset = offset_in_hvpg; |
be0cf6ca | 1851 | |
743b237c | 1852 | sg_count = scsi_dma_map(scmnd); |
4eea5332 JV |
1853 | if (sg_count < 0) { |
1854 | ret = SCSI_MLQUEUE_DEVICE_BUSY; | |
1855 | goto err_free_payload; | |
1856 | } | |
3d9c3dcc | 1857 | |
743b237c | 1858 | for_each_sg(sgl, sg, sg_count, j) { |
8f437105 | 1859 | /* |
743b237c TL |
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. | |
8f437105 | 1866 | */ |
743b237c TL |
1867 | hvpfn = HVPFN_DOWN(sg_dma_address(sg)); |
1868 | hvpfns_to_add = HVPFN_UP(sg_dma_address(sg) + | |
1869 | sg_dma_len(sg)) - hvpfn; | |
8f437105 BF |
1870 | |
1871 | /* | |
3d9c3dcc MK |
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. | |
8f437105 | 1877 | */ |
3d9c3dcc | 1878 | while (hvpfns_to_add--) |
743b237c | 1879 | payload->range.pfn_array[i++] = hvpfn++; |
aaced993 | 1880 | } |
c5b463ae S |
1881 | } |
1882 | ||
be0cf6ca S |
1883 | cmd_request->payload = payload; |
1884 | cmd_request->payload_sz = payload_sz; | |
1885 | ||
c5b463ae | 1886 | /* Invokes the vsc to start an IO */ |
d86adf48 S |
1887 | ret = storvsc_do_io(dev, cmd_request, get_cpu()); |
1888 | put_cpu(); | |
636f0fd1 | 1889 | |
d2598f01 | 1890 | if (ret == -EAGAIN) { |
c5b463ae | 1891 | /* no more space */ |
4eea5332 JV |
1892 | ret = SCSI_MLQUEUE_DEVICE_BUSY; |
1893 | goto err_free_payload; | |
c5b463ae S |
1894 | } |
1895 | ||
c77b63b6 | 1896 | return 0; |
4eea5332 JV |
1897 | |
1898 | err_free_payload: | |
1899 | if (payload_sz > sizeof(cmd_request->mpb)) | |
1900 | kfree(payload); | |
1901 | ||
1902 | return ret; | |
c5b463ae S |
1903 | } |
1904 | ||
bef4a34a | 1905 | static struct scsi_host_template scsi_driver = { |
ff568d3a GKH |
1906 | .module = THIS_MODULE, |
1907 | .name = "storvsc_host_t", | |
ead3700d | 1908 | .cmd_size = sizeof(struct storvsc_cmd_request), |
ff568d3a GKH |
1909 | .bios_param = storvsc_get_chs, |
1910 | .queuecommand = storvsc_queuecommand, | |
1911 | .eh_host_reset_handler = storvsc_host_reset_handler, | |
ead3700d | 1912 | .proc_name = "storvsc_host", |
56b26e69 | 1913 | .eh_timed_out = storvsc_eh_timed_out, |
f1c635b4 | 1914 | .slave_alloc = storvsc_device_alloc, |
ff568d3a | 1915 | .slave_configure = storvsc_device_configure, |
cabe92a5 | 1916 | .cmd_per_lun = 2048, |
ff568d3a | 1917 | .this_id = -1, |
83eed459 CH |
1918 | /* Ensure there are no gaps in presented sgls */ |
1919 | .virt_boundary_mask = PAGE_SIZE-1, | |
54b2b50c | 1920 | .no_write_same = 1, |
f64dad26 | 1921 | .track_queue_depth = 1, |
adfbd028 | 1922 | .change_queue_depth = storvsc_change_queue_depth, |
bef4a34a HJ |
1923 | }; |
1924 | ||
ef52a81b S |
1925 | enum { |
1926 | SCSI_GUID, | |
1927 | IDE_GUID, | |
bde6d0f9 | 1928 | SFC_GUID, |
ef52a81b S |
1929 | }; |
1930 | ||
d847b5fe | 1931 | static const struct hv_vmbus_device_id id_table[] = { |
c45cf2d4 | 1932 | /* SCSI guid */ |
35c3bc20 S |
1933 | { HV_SCSI_GUID, |
1934 | .driver_data = SCSI_GUID | |
1935 | }, | |
21e37742 | 1936 | /* IDE guid */ |
35c3bc20 S |
1937 | { HV_IDE_GUID, |
1938 | .driver_data = IDE_GUID | |
1939 | }, | |
bde6d0f9 S |
1940 | /* Fibre Channel GUID */ |
1941 | { | |
1942 | HV_SYNTHFC_GUID, | |
1943 | .driver_data = SFC_GUID | |
1944 | }, | |
c45cf2d4 | 1945 | { }, |
d847b5fe | 1946 | }; |
bef4a34a | 1947 | |
d847b5fe | 1948 | MODULE_DEVICE_TABLE(vmbus, id_table); |
bd1f5d6a | 1949 | |
56fb1058 DC |
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 | ||
84946899 S |
1957 | static int storvsc_probe(struct hv_device *device, |
1958 | const struct hv_vmbus_device_id *dev_id) | |
bef4a34a | 1959 | { |
ff568d3a | 1960 | int ret; |
f458aada | 1961 | int num_cpus = num_online_cpus(); |
a81a38cc | 1962 | int num_present_cpus = num_present_cpus(); |
bef4a34a | 1963 | struct Scsi_Host *host; |
795b613d | 1964 | struct hv_host_device *host_dev; |
ef52a81b | 1965 | bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false); |
dac58241 | 1966 | bool is_fc = ((dev_id->driver_data == SFC_GUID) ? true : false); |
bd1f5d6a | 1967 | int target = 0; |
6e4198ce | 1968 | struct storvsc_device *stor_device; |
f458aada | 1969 | int max_sub_channels = 0; |
bd1f5d6a | 1970 | |
8b612fa2 | 1971 | /* |
106b98a5 MK |
1972 | * We support sub-channels for storage on SCSI and FC controllers. |
1973 | * The number of sub-channels offerred is based on the number of | |
1974 | * VCPUs in the guest. | |
8b612fa2 | 1975 | */ |
106b98a5 MK |
1976 | if (!dev_is_ide) |
1977 | max_sub_channels = | |
1978 | (num_cpus - 1) / storvsc_vcpus_per_sub_channel; | |
8b612fa2 | 1979 | |
2217a47d LL |
1980 | scsi_driver.can_queue = max_outstanding_req_per_channel * |
1981 | (max_sub_channels + 1) * | |
1982 | (100 - ring_avail_percent_lowater) / 100; | |
f458aada | 1983 | |
ff568d3a | 1984 | host = scsi_host_alloc(&scsi_driver, |
972621c9 | 1985 | sizeof(struct hv_host_device)); |
f8feed06 | 1986 | if (!host) |
bef4a34a | 1987 | return -ENOMEM; |
bef4a34a | 1988 | |
7f33f30a | 1989 | host_dev = shost_priv(host); |
795b613d | 1990 | memset(host_dev, 0, sizeof(struct hv_host_device)); |
bef4a34a | 1991 | |
795b613d | 1992 | host_dev->port = host->host_no; |
97c15296 | 1993 | host_dev->dev = device; |
c58cc70f | 1994 | host_dev->host = host; |
bef4a34a | 1995 | |
4e03e697 | 1996 | |
a13d35ab | 1997 | stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL); |
6e4198ce | 1998 | if (!stor_device) { |
225ce6ea | 1999 | ret = -ENOMEM; |
ce3e301c | 2000 | goto err_out0; |
6e4198ce | 2001 | } |
9efd21e1 | 2002 | |
a13d35ab S |
2003 | stor_device->destroy = false; |
2004 | init_waitqueue_head(&stor_device->waiting_to_drain); | |
2005 | stor_device->device = device; | |
cd654ea1 | 2006 | stor_device->host = host; |
244808e0 | 2007 | stor_device->vmscsi_size_delta = sizeof(struct vmscsi_win8_extension); |
21d2052c | 2008 | spin_lock_init(&stor_device->lock); |
cd654ea1 | 2009 | hv_set_drvdata(device, stor_device); |
743b237c | 2010 | dma_set_min_align_mask(&device->device, HV_HYP_PAGE_SIZE - 1); |
a13d35ab | 2011 | |
6e4198ce | 2012 | stor_device->port_number = host->host_no; |
dac58241 | 2013 | ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size, is_fc); |
225ce6ea | 2014 | if (ret) |
ce3e301c | 2015 | goto err_out1; |
bef4a34a | 2016 | |
6e4198ce S |
2017 | host_dev->path = stor_device->path_id; |
2018 | host_dev->target = stor_device->target_id; | |
bef4a34a | 2019 | |
4cd83ecd S |
2020 | switch (dev_id->driver_data) { |
2021 | case SFC_GUID: | |
2022 | host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET; | |
2023 | host->max_id = STORVSC_FC_MAX_TARGETS; | |
2024 | host->max_channel = STORVSC_FC_MAX_CHANNELS - 1; | |
dac58241 S |
2025 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) |
2026 | host->transportt = fc_transport_template; | |
2027 | #endif | |
4cd83ecd S |
2028 | break; |
2029 | ||
2030 | case SCSI_GUID: | |
106b98a5 MK |
2031 | host->max_lun = STORVSC_MAX_LUNS_PER_TARGET; |
2032 | host->max_id = STORVSC_MAX_TARGETS; | |
2033 | host->max_channel = STORVSC_MAX_CHANNELS - 1; | |
4cd83ecd S |
2034 | break; |
2035 | ||
2036 | default: | |
2037 | host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET; | |
2038 | host->max_id = STORVSC_IDE_MAX_TARGETS; | |
2039 | host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1; | |
2040 | break; | |
2041 | } | |
cf55f4a8 MS |
2042 | /* max cmd length */ |
2043 | host->max_cmd_len = STORVSC_MAX_CMD_LEN; | |
bef4a34a | 2044 | |
be0cf6ca S |
2045 | /* |
2046 | * set the table size based on the info we got | |
2047 | * from the host. | |
2048 | */ | |
2049 | host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT); | |
d86adf48 | 2050 | /* |
7b571c19 | 2051 | * For non-IDE disks, the host supports multiple channels. |
d86adf48 S |
2052 | * Set the number of HW queues we are supporting. |
2053 | */ | |
a81a38cc MPM |
2054 | if (!dev_is_ide) { |
2055 | if (storvsc_max_hw_queues > num_present_cpus) { | |
2056 | storvsc_max_hw_queues = 0; | |
2057 | storvsc_log(device, STORVSC_LOGGING_WARN, | |
2058 | "Resetting invalid storvsc_max_hw_queues value to default.\n"); | |
2059 | } | |
2060 | if (storvsc_max_hw_queues) | |
2061 | host->nr_hw_queues = storvsc_max_hw_queues; | |
2062 | else | |
2063 | host->nr_hw_queues = num_present_cpus; | |
2064 | } | |
be0cf6ca | 2065 | |
436ad941 CA |
2066 | /* |
2067 | * Set the error handler work queue. | |
2068 | */ | |
2069 | host_dev->handle_error_wq = | |
2070 | alloc_ordered_workqueue("storvsc_error_wq_%d", | |
2071 | WQ_MEM_RECLAIM, | |
2072 | host->host_no); | |
6112ff4e JX |
2073 | if (!host_dev->handle_error_wq) { |
2074 | ret = -ENOMEM; | |
436ad941 | 2075 | goto err_out2; |
6112ff4e | 2076 | } |
c58cc70f | 2077 | INIT_WORK(&host_dev->host_scan_work, storvsc_host_scan); |
454f18a9 | 2078 | /* Register the HBA and start the scsi bus scan */ |
9efd21e1 | 2079 | ret = scsi_add_host(host, &device->device); |
bd1f5d6a | 2080 | if (ret != 0) |
436ad941 | 2081 | goto err_out3; |
bef4a34a | 2082 | |
bd1f5d6a S |
2083 | if (!dev_is_ide) { |
2084 | scsi_scan_host(host); | |
59d22950 S |
2085 | } else { |
2086 | target = (device->dev_instance.b[5] << 8 | | |
2087 | device->dev_instance.b[4]); | |
2088 | ret = scsi_add_device(host, 0, target, 0); | |
daf0cd44 | 2089 | if (ret) |
436ad941 | 2090 | goto err_out4; |
bef4a34a | 2091 | } |
dac58241 S |
2092 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) |
2093 | if (host->transportt == fc_transport_template) { | |
daf0cd44 CA |
2094 | struct fc_rport_identifiers ids = { |
2095 | .roles = FC_PORT_ROLE_FCP_DUMMY_INITIATOR, | |
2096 | }; | |
2097 | ||
dac58241 S |
2098 | fc_host_node_name(host) = stor_device->node_name; |
2099 | fc_host_port_name(host) = stor_device->port_name; | |
daf0cd44 | 2100 | stor_device->rport = fc_remote_port_add(host, 0, &ids); |
ca8dc694 DC |
2101 | if (!stor_device->rport) { |
2102 | ret = -ENOMEM; | |
436ad941 | 2103 | goto err_out4; |
ca8dc694 | 2104 | } |
dac58241 S |
2105 | } |
2106 | #endif | |
bd1f5d6a | 2107 | return 0; |
bef4a34a | 2108 | |
436ad941 | 2109 | err_out4: |
daf0cd44 CA |
2110 | scsi_remove_host(host); |
2111 | ||
436ad941 CA |
2112 | err_out3: |
2113 | destroy_workqueue(host_dev->handle_error_wq); | |
2114 | ||
ce3e301c | 2115 | err_out2: |
225ce6ea S |
2116 | /* |
2117 | * Once we have connected with the host, we would need to | |
2118 | * to invoke storvsc_dev_remove() to rollback this state and | |
2119 | * this call also frees up the stor_device; hence the jump around | |
ce3e301c | 2120 | * err_out1 label. |
225ce6ea | 2121 | */ |
bd1f5d6a | 2122 | storvsc_dev_remove(device); |
ce3e301c | 2123 | goto err_out0; |
225ce6ea S |
2124 | |
2125 | err_out1: | |
d86adf48 | 2126 | kfree(stor_device->stor_chns); |
ce3e301c | 2127 | kfree(stor_device); |
225ce6ea S |
2128 | |
2129 | err_out0: | |
bd1f5d6a | 2130 | scsi_host_put(host); |
225ce6ea | 2131 | return ret; |
bef4a34a HJ |
2132 | } |
2133 | ||
adfbd028 BB |
2134 | /* Change a scsi target's queue depth */ |
2135 | static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth) | |
2136 | { | |
2137 | if (queue_depth > scsi_driver.can_queue) | |
2138 | queue_depth = scsi_driver.can_queue; | |
2139 | ||
2140 | return scsi_change_queue_depth(sdev, queue_depth); | |
2141 | } | |
2142 | ||
ddcbf65e S |
2143 | static int storvsc_remove(struct hv_device *dev) |
2144 | { | |
2145 | struct storvsc_device *stor_device = hv_get_drvdata(dev); | |
2146 | struct Scsi_Host *host = stor_device->host; | |
436ad941 | 2147 | struct hv_host_device *host_dev = shost_priv(host); |
ddcbf65e | 2148 | |
dac58241 | 2149 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) |
daf0cd44 CA |
2150 | if (host->transportt == fc_transport_template) { |
2151 | fc_remote_port_delete(stor_device->rport); | |
dac58241 | 2152 | fc_remove_host(host); |
daf0cd44 | 2153 | } |
dac58241 | 2154 | #endif |
436ad941 | 2155 | destroy_workqueue(host_dev->handle_error_wq); |
ddcbf65e S |
2156 | scsi_remove_host(host); |
2157 | storvsc_dev_remove(dev); | |
2158 | scsi_host_put(host); | |
2159 | ||
2160 | return 0; | |
2161 | } | |
2162 | ||
56fb1058 DC |
2163 | static int storvsc_suspend(struct hv_device *hv_dev) |
2164 | { | |
2165 | struct storvsc_device *stor_device = hv_get_drvdata(hv_dev); | |
2166 | struct Scsi_Host *host = stor_device->host; | |
2167 | struct hv_host_device *host_dev = shost_priv(host); | |
2168 | ||
2169 | storvsc_wait_to_drain(stor_device); | |
2170 | ||
2171 | drain_workqueue(host_dev->handle_error_wq); | |
2172 | ||
2173 | vmbus_close(hv_dev->channel); | |
2174 | ||
56fb1058 DC |
2175 | kfree(stor_device->stor_chns); |
2176 | stor_device->stor_chns = NULL; | |
2177 | ||
2178 | cpumask_clear(&stor_device->alloced_cpus); | |
2179 | ||
2180 | return 0; | |
2181 | } | |
2182 | ||
2183 | static int storvsc_resume(struct hv_device *hv_dev) | |
2184 | { | |
2185 | int ret; | |
2186 | ||
2187 | ret = storvsc_connect_to_vsp(hv_dev, storvsc_ringbuffer_size, | |
2188 | hv_dev_is_fc(hv_dev)); | |
2189 | return ret; | |
2190 | } | |
2191 | ||
40bf63ed | 2192 | static struct hv_driver storvsc_drv = { |
fafb0efc | 2193 | .name = KBUILD_MODNAME, |
d847b5fe | 2194 | .id_table = id_table, |
40bf63ed S |
2195 | .probe = storvsc_probe, |
2196 | .remove = storvsc_remove, | |
56fb1058 DC |
2197 | .suspend = storvsc_suspend, |
2198 | .resume = storvsc_resume, | |
af0a5646 AV |
2199 | .driver = { |
2200 | .probe_type = PROBE_PREFER_ASYNCHRONOUS, | |
2201 | }, | |
39ae6fae | 2202 | }; |
7bd05b91 | 2203 | |
dac58241 S |
2204 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) |
2205 | static struct fc_function_template fc_transport_functions = { | |
2206 | .show_host_node_name = 1, | |
2207 | .show_host_port_name = 1, | |
2208 | }; | |
2209 | #endif | |
2210 | ||
d9bbae83 | 2211 | static int __init storvsc_drv_init(void) |
f5c78872 | 2212 | { |
dac58241 | 2213 | int ret; |
01415ab3 S |
2214 | |
2215 | /* | |
2216 | * Divide the ring buffer data size (which is 1 page less | |
2217 | * than the ring buffer size since that page is reserved for | |
2218 | * the ring buffer indices) by the max request size (which is | |
2219 | * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64) | |
ab548fd2 APM |
2220 | * |
2221 | * The computation underestimates max_outstanding_req_per_channel | |
2222 | * for Win7 and older hosts because it does not take into account | |
2223 | * the vmscsi_size_delta correction to the max request size. | |
01415ab3 | 2224 | */ |
01415ab3 | 2225 | max_outstanding_req_per_channel = |
768fa219 GKH |
2226 | ((storvsc_ringbuffer_size - PAGE_SIZE) / |
2227 | ALIGN(MAX_MULTIPAGE_BUFFER_PACKET + | |
ab548fd2 | 2228 | sizeof(struct vstor_packet) + sizeof(u64), |
768fa219 | 2229 | sizeof(u64))); |
f5c78872 | 2230 | |
dac58241 S |
2231 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) |
2232 | fc_transport_template = fc_attach_transport(&fc_transport_functions); | |
2233 | if (!fc_transport_template) | |
2234 | return -ENODEV; | |
2235 | #endif | |
2236 | ||
2237 | ret = vmbus_driver_register(&storvsc_drv); | |
2238 | ||
2239 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) | |
2240 | if (ret) | |
2241 | fc_release_transport(fc_transport_template); | |
2242 | #endif | |
2243 | ||
2244 | return ret; | |
f5c78872 S |
2245 | } |
2246 | ||
c63ba9e1 | 2247 | static void __exit storvsc_drv_exit(void) |
f5c78872 | 2248 | { |
768fa219 | 2249 | vmbus_driver_unregister(&storvsc_drv); |
dac58241 S |
2250 | #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS) |
2251 | fc_release_transport(fc_transport_template); | |
2252 | #endif | |
f5c78872 S |
2253 | } |
2254 | ||
ff568d3a | 2255 | MODULE_LICENSE("GPL"); |
3afc7cc3 | 2256 | MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver"); |
d9bbae83 | 2257 | module_init(storvsc_drv_init); |
c63ba9e1 | 2258 | module_exit(storvsc_drv_exit); |