1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2009, Microsoft Corporation.
4 *
5 * Authors:
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
8 * K. Y. Srinivasan <kys@microsoft.com>
9 */
10
11 #include <linux/kernel.h>
12 #include <linux/wait.h>
13 #include <linux/sched.h>
14 #include <linux/completion.h>
15 #include <linux/string.h>
16 #include <linux/mm.h>
17 #include <linux/delay.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/device.h>
22 #include <linux/hyperv.h>
23 #include <linux/blkdev.h>
24 #include <linux/dma-mapping.h>
25
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_cmnd.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_tcq.h>
31 #include <scsi/scsi_eh.h>
32 #include <scsi/scsi_devinfo.h>
33 #include <scsi/scsi_dbg.h>
34 #include <scsi/scsi_transport_fc.h>
35 #include <scsi/scsi_transport.h>
36
37 /*
38 * All wire protocol details (storage protocol between the guest and the host)
39 * are consolidated here.
40 *
41 * Begin protocol definitions.
42 */
43
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
50 * Win8/WS2012: 5.1
51 * Win8.1/WS2012R2: 6.0 (also for HvLite paravisor in Azure)
52 * Win10/WS2016: 6.2
53 *
54 * Protocol versions earlier than Win8.1 are no longer supported since
55 * Win8.1/WS2012R2 and earlier hosts are no longer supported by Linux.
56 * But protocol version 6.0 is retained since it is used by the HvLite
57 * paravisor in Azure. The #define's for the earlier versions remain
58 * for the historical record.
59 */
60
61 #define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
62 (((MINOR_) & 0xff)))
63 #define VMSTOR_PROTO_VERSION_WIN6 VMSTOR_PROTO_VERSION(2, 0)
64 #define VMSTOR_PROTO_VERSION_WIN7 VMSTOR_PROTO_VERSION(4, 2)
65 #define VMSTOR_PROTO_VERSION_WIN8 VMSTOR_PROTO_VERSION(5, 1)
66 #define VMSTOR_PROTO_VERSION_WIN8_1 VMSTOR_PROTO_VERSION(6, 0)
67 #define VMSTOR_PROTO_VERSION_WIN10 VMSTOR_PROTO_VERSION(6, 2)
68
69 /* channel callback timeout in ms */
70 #define CALLBACK_TIMEOUT 2
71
72 /* Packet structure describing virtual storage requests. */
73 enum vstor_packet_operation {
74 VSTOR_OPERATION_COMPLETE_IO = 1,
75 VSTOR_OPERATION_REMOVE_DEVICE = 2,
76 VSTOR_OPERATION_EXECUTE_SRB = 3,
77 VSTOR_OPERATION_RESET_LUN = 4,
78 VSTOR_OPERATION_RESET_ADAPTER = 5,
79 VSTOR_OPERATION_RESET_BUS = 6,
80 VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
81 VSTOR_OPERATION_END_INITIALIZATION = 8,
82 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
83 VSTOR_OPERATION_QUERY_PROPERTIES = 10,
84 VSTOR_OPERATION_ENUMERATE_BUS = 11,
85 VSTOR_OPERATION_FCHBA_DATA = 12,
86 VSTOR_OPERATION_CREATE_SUB_CHANNELS = 13,
87 VSTOR_OPERATION_MAXIMUM = 13
88 };
89
90 /*
91 * WWN packet for Fibre Channel HBA
92 */
93
94 struct hv_fc_wwn_packet {
95 u8 primary_active;
96 u8 reserved1[3];
97 u8 primary_port_wwn[8];
98 u8 primary_node_wwn[8];
99 u8 secondary_port_wwn[8];
100 u8 secondary_node_wwn[8];
101 };
102
103
104
105 /*
106 * SRB Flag Bits
107 */
108
109 #define SRB_FLAGS_QUEUE_ACTION_ENABLE 0x00000002
110 #define SRB_FLAGS_DISABLE_DISCONNECT 0x00000004
111 #define SRB_FLAGS_DISABLE_SYNCH_TRANSFER 0x00000008
112 #define SRB_FLAGS_BYPASS_FROZEN_QUEUE 0x00000010
113 #define SRB_FLAGS_DISABLE_AUTOSENSE 0x00000020
114 #define SRB_FLAGS_DATA_IN 0x00000040
115 #define SRB_FLAGS_DATA_OUT 0x00000080
116 #define SRB_FLAGS_NO_DATA_TRANSFER 0x00000000
117 #define SRB_FLAGS_UNSPECIFIED_DIRECTION (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT)
118 #define SRB_FLAGS_NO_QUEUE_FREEZE 0x00000100
119 #define SRB_FLAGS_ADAPTER_CACHE_ENABLE 0x00000200
120 #define SRB_FLAGS_FREE_SENSE_BUFFER 0x00000400
121
122 /*
123 * This flag indicates the request is part of the workflow for processing a D3.
124 */
125 #define SRB_FLAGS_D3_PROCESSING 0x00000800
126 #define SRB_FLAGS_IS_ACTIVE 0x00010000
127 #define SRB_FLAGS_ALLOCATED_FROM_ZONE 0x00020000
128 #define SRB_FLAGS_SGLIST_FROM_POOL 0x00040000
129 #define SRB_FLAGS_BYPASS_LOCKED_QUEUE 0x00080000
130 #define SRB_FLAGS_NO_KEEP_AWAKE 0x00100000
131 #define SRB_FLAGS_PORT_DRIVER_ALLOCSENSE 0x00200000
132 #define SRB_FLAGS_PORT_DRIVER_SENSEHASPORT 0x00400000
133 #define SRB_FLAGS_DONT_START_NEXT_PACKET 0x00800000
134 #define SRB_FLAGS_PORT_DRIVER_RESERVED 0x0F000000
135 #define SRB_FLAGS_CLASS_DRIVER_RESERVED 0xF0000000
136
137 #define SP_UNTAGGED ((unsigned char) ~0)
138 #define SRB_SIMPLE_TAG_REQUEST 0x20
139
140 /*
141 * Platform neutral description of a scsi request -
142 * this remains the same across the write regardless of 32/64 bit
143 * note: it's patterned off the SCSI_PASS_THROUGH structure
144 */
145 #define STORVSC_MAX_CMD_LEN 0x10
146
147 /* Sense buffer size is the same for all versions since Windows 8 */
148 #define STORVSC_SENSE_BUFFER_SIZE 0x14
149 #define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
150
151 /*
152 * The storage protocol version is determined during the
153 * initial exchange with the host. It will indicate which
154 * storage functionality is available in the host.
155 */
156 static int vmstor_proto_version;
157
158 static bool hv_dev_is_fc(struct hv_device *hv_dev);
159
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, 0644);
166 MODULE_PARM_DESC(logging_level,
167 "Logging level, 0 - None, 1 - Error (default), 2 - Warning.");
168
do_logging(int level)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
180 #define storvsc_log_ratelimited(dev, level, fmt, ...) \
181 do { \
182 if (do_logging(level)) \
183 dev_warn_ratelimited(&(dev)->device, fmt, ##__VA_ARGS__); \
184 } while (0)
185
186 struct vmscsi_request {
187 u16 length;
188 u8 srb_status;
189 u8 scsi_status;
190
191 u8 port_number;
192 u8 path_id;
193 u8 target_id;
194 u8 lun;
195
196 u8 cdb_length;
197 u8 sense_info_length;
198 u8 data_in;
199 u8 reserved;
200
201 u32 data_transfer_length;
202
203 union {
204 u8 cdb[STORVSC_MAX_CMD_LEN];
205 u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
206 u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
207 };
208 /*
209 * The following was added in win8.
210 */
211 u16 reserve;
212 u8 queue_tag;
213 u8 queue_action;
214 u32 srb_flags;
215 u32 time_out_value;
216 u32 queue_sort_ey;
217
218 } __attribute((packed));
219
220 /*
221 * The list of windows version in order of preference.
222 */
223
224 static const int protocol_version[] = {
225 VMSTOR_PROTO_VERSION_WIN10,
226 VMSTOR_PROTO_VERSION_WIN8_1,
227 };
228
229
230 /*
231 * This structure is sent during the initialization phase to get the different
232 * properties of the channel.
233 */
234
235 #define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1
236
237 struct vmstorage_channel_properties {
238 u32 reserved;
239 u16 max_channel_cnt;
240 u16 reserved1;
241
242 u32 flags;
243 u32 max_transfer_bytes;
244
245 u64 reserved2;
246 } __packed;
247
248 /* This structure is sent during the storage protocol negotiations. */
249 struct vmstorage_protocol_version {
250 /* Major (MSW) and minor (LSW) version numbers. */
251 u16 major_minor;
252
253 /*
254 * Revision number is auto-incremented whenever this file is changed
255 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
256 * definitely indicate incompatibility--but it does indicate mismatched
257 * builds.
258 * This is only used on the windows side. Just set it to 0.
259 */
260 u16 revision;
261 } __packed;
262
263 /* Channel Property Flags */
264 #define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
265 #define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
266
267 struct vstor_packet {
268 /* Requested operation type */
269 enum vstor_packet_operation operation;
270
271 /* Flags - see below for values */
272 u32 flags;
273
274 /* Status of the request returned from the server side. */
275 u32 status;
276
277 /* Data payload area */
278 union {
279 /*
280 * Structure used to forward SCSI commands from the
281 * client to the server.
282 */
283 struct vmscsi_request vm_srb;
284
285 /* Structure used to query channel properties. */
286 struct vmstorage_channel_properties storage_channel_properties;
287
288 /* Used during version negotiations. */
289 struct vmstorage_protocol_version version;
290
291 /* Fibre channel address packet */
292 struct hv_fc_wwn_packet wwn_packet;
293
294 /* Number of sub-channels to create */
295 u16 sub_channel_count;
296
297 /* This will be the maximum of the union members */
298 u8 buffer[0x34];
299 };
300 } __packed;
301
302 /*
303 * Packet Flags:
304 *
305 * This flag indicates that the server should send back a completion for this
306 * packet.
307 */
308
309 #define REQUEST_COMPLETION_FLAG 0x1
310
311 /* Matches Windows-end */
312 enum storvsc_request_type {
313 WRITE_TYPE = 0,
314 READ_TYPE,
315 UNKNOWN_TYPE,
316 };
317
318 /*
319 * SRB status codes and masks. In the 8-bit field, the two high order bits
320 * are flags, while the remaining 6 bits are an integer status code. The
321 * definitions here include only the subset of the integer status codes that
322 * are tested for in this driver.
323 */
324 #define SRB_STATUS_AUTOSENSE_VALID 0x80
325 #define SRB_STATUS_QUEUE_FROZEN 0x40
326
327 /* SRB status integer codes */
328 #define SRB_STATUS_SUCCESS 0x01
329 #define SRB_STATUS_ABORTED 0x02
330 #define SRB_STATUS_ERROR 0x04
331 #define SRB_STATUS_INVALID_REQUEST 0x06
332 #define SRB_STATUS_TIMEOUT 0x09
333 #define SRB_STATUS_SELECTION_TIMEOUT 0x0A
334 #define SRB_STATUS_BUS_RESET 0x0E
335 #define SRB_STATUS_DATA_OVERRUN 0x12
336 #define SRB_STATUS_INVALID_LUN 0x20
337 #define SRB_STATUS_INTERNAL_ERROR 0x30
338
339 #define SRB_STATUS(status) \
340 (status & ~(SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_QUEUE_FROZEN))
341 /*
342 * This is the end of Protocol specific defines.
343 */
344
345 static int storvsc_ringbuffer_size = (128 * 1024);
346 static int aligned_ringbuffer_size;
347 static u32 max_outstanding_req_per_channel;
348 static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth);
349
350 static int storvsc_vcpus_per_sub_channel = 4;
351 static unsigned int storvsc_max_hw_queues;
352
353 module_param(storvsc_ringbuffer_size, int, 0444);
354 MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
355
356 module_param(storvsc_max_hw_queues, uint, 0644);
357 MODULE_PARM_DESC(storvsc_max_hw_queues, "Maximum number of hardware queues");
358
359 module_param(storvsc_vcpus_per_sub_channel, int, 0444);
360 MODULE_PARM_DESC(storvsc_vcpus_per_sub_channel, "Ratio of VCPUs to subchannels");
361
362 static int ring_avail_percent_lowater = 10;
363 module_param(ring_avail_percent_lowater, int, 0444);
364 MODULE_PARM_DESC(ring_avail_percent_lowater,
365 "Select a channel if available ring size > this in percent");
366
367 /*
368 * Timeout in seconds for all devices managed by this driver.
369 */
370 static const int storvsc_timeout = 180;
371
372 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
373 static struct scsi_transport_template *fc_transport_template;
374 #endif
375
376 static struct scsi_host_template scsi_driver;
377 static void storvsc_on_channel_callback(void *context);
378
379 #define STORVSC_MAX_LUNS_PER_TARGET 255
380 #define STORVSC_MAX_TARGETS 2
381 #define STORVSC_MAX_CHANNELS 8
382
383 #define STORVSC_FC_MAX_LUNS_PER_TARGET 255
384 #define STORVSC_FC_MAX_TARGETS 128
385 #define STORVSC_FC_MAX_CHANNELS 8
386 #define STORVSC_FC_MAX_XFER_SIZE ((u32)(512 * 1024))
387
388 #define STORVSC_IDE_MAX_LUNS_PER_TARGET 64
389 #define STORVSC_IDE_MAX_TARGETS 1
390 #define STORVSC_IDE_MAX_CHANNELS 1
391
392 /*
393 * Upper bound on the size of a storvsc packet.
394 */
395 #define STORVSC_MAX_PKT_SIZE (sizeof(struct vmpacket_descriptor) +\
396 sizeof(struct vstor_packet))
397
398 struct storvsc_cmd_request {
399 struct scsi_cmnd *cmd;
400
401 struct hv_device *device;
402
403 /* Synchronize the request/response if needed */
404 struct completion wait_event;
405
406 struct vmbus_channel_packet_multipage_buffer mpb;
407 struct vmbus_packet_mpb_array *payload;
408 u32 payload_sz;
409
410 struct vstor_packet vstor_packet;
411 };
412
413
414 /* A storvsc device is a device object that contains a vmbus channel */
415 struct storvsc_device {
416 struct hv_device *device;
417
418 bool destroy;
419 bool drain_notify;
420 atomic_t num_outstanding_req;
421 struct Scsi_Host *host;
422
423 wait_queue_head_t waiting_to_drain;
424
425 /*
426 * Each unique Port/Path/Target represents 1 channel ie scsi
427 * controller. In reality, the pathid, targetid is always 0
428 * and the port is set by us
429 */
430 unsigned int port_number;
431 unsigned char path_id;
432 unsigned char target_id;
433
434 /*
435 * Max I/O, the device can support.
436 */
437 u32 max_transfer_bytes;
438 /*
439 * Number of sub-channels we will open.
440 */
441 u16 num_sc;
442 struct vmbus_channel **stor_chns;
443 /*
444 * Mask of CPUs bound to subchannels.
445 */
446 struct cpumask alloced_cpus;
447 /*
448 * Serializes modifications of stor_chns[] from storvsc_do_io()
449 * and storvsc_change_target_cpu().
450 */
451 spinlock_t lock;
452 /* Used for vsc/vsp channel reset process */
453 struct storvsc_cmd_request init_request;
454 struct storvsc_cmd_request reset_request;
455 /*
456 * Currently active port and node names for FC devices.
457 */
458 u64 node_name;
459 u64 port_name;
460 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
461 struct fc_rport *rport;
462 #endif
463 };
464
465 struct hv_host_device {
466 struct hv_device *dev;
467 unsigned int port;
468 unsigned char path;
469 unsigned char target;
470 struct workqueue_struct *handle_error_wq;
471 struct work_struct host_scan_work;
472 struct Scsi_Host *host;
473 };
474
475 struct storvsc_scan_work {
476 struct work_struct work;
477 struct Scsi_Host *host;
478 u8 lun;
479 u8 tgt_id;
480 };
481
storvsc_device_scan(struct work_struct * work)482 static void storvsc_device_scan(struct work_struct *work)
483 {
484 struct storvsc_scan_work *wrk;
485 struct scsi_device *sdev;
486
487 wrk = container_of(work, struct storvsc_scan_work, work);
488
489 sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
490 if (!sdev)
491 goto done;
492 scsi_rescan_device(sdev);
493 scsi_device_put(sdev);
494
495 done:
496 kfree(wrk);
497 }
498
storvsc_host_scan(struct work_struct * work)499 static void storvsc_host_scan(struct work_struct *work)
500 {
501 struct Scsi_Host *host;
502 struct scsi_device *sdev;
503 struct hv_host_device *host_device =
504 container_of(work, struct hv_host_device, host_scan_work);
505
506 host = host_device->host;
507 /*
508 * Before scanning the host, first check to see if any of the
509 * currently known devices have been hot removed. We issue a
510 * "unit ready" command against all currently known devices.
511 * This I/O will result in an error for devices that have been
512 * removed. As part of handling the I/O error, we remove the device.
513 *
514 * When a LUN is added or removed, the host sends us a signal to
515 * scan the host. Thus we are forced to discover the LUNs that
516 * may have been removed this way.
517 */
518 mutex_lock(&host->scan_mutex);
519 shost_for_each_device(sdev, host)
520 scsi_test_unit_ready(sdev, 1, 1, NULL);
521 mutex_unlock(&host->scan_mutex);
522 /*
523 * Now scan the host to discover LUNs that may have been added.
524 */
525 scsi_scan_host(host);
526 }
527
528 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
storvsc_user_scan(struct Scsi_Host * host,unsigned int channel,unsigned int id,u64 lun)529 static int storvsc_user_scan(struct Scsi_Host *host,
530 unsigned int channel,
531 unsigned int id,
532 u64 lun)
533 {
534 unsigned int first_channel, last_channel;
535 unsigned int first_id, end_id;
536 unsigned int ch, target;
537
538 if ((channel != SCAN_WILD_CARD && channel > host->max_channel) ||
539 (id != SCAN_WILD_CARD && id >= host->max_id) ||
540 (lun != SCAN_WILD_CARD && lun >= host->max_lun))
541 return -EINVAL;
542
543 first_channel = channel == SCAN_WILD_CARD ? 0 : channel;
544 last_channel = channel == SCAN_WILD_CARD ? host->max_channel : channel;
545 first_id = id == SCAN_WILD_CARD ? 0 : id;
546 end_id = id == SCAN_WILD_CARD ? host->max_id : id + 1;
547
548 for (ch = first_channel; ch <= last_channel; ch++) {
549 for (target = first_id; target < end_id; target++)
550 scsi_scan_target(&host->shost_gendev, ch, target, lun,
551 SCSI_SCAN_MANUAL);
552 }
553
554 return 0;
555 }
556 #endif
557
storvsc_remove_lun(struct work_struct * work)558 static void storvsc_remove_lun(struct work_struct *work)
559 {
560 struct storvsc_scan_work *wrk;
561 struct scsi_device *sdev;
562
563 wrk = container_of(work, struct storvsc_scan_work, work);
564 if (!scsi_host_get(wrk->host))
565 goto done;
566
567 sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
568
569 if (sdev) {
570 scsi_remove_device(sdev);
571 scsi_device_put(sdev);
572 }
573 scsi_host_put(wrk->host);
574
575 done:
576 kfree(wrk);
577 }
578
579
580 /*
581 * We can get incoming messages from the host that are not in response to
582 * messages that we have sent out. An example of this would be messages
583 * received by the guest to notify dynamic addition/removal of LUNs. To
584 * deal with potential race conditions where the driver may be in the
585 * midst of being unloaded when we might receive an unsolicited message
586 * from the host, we have implemented a mechanism to gurantee sequential
587 * consistency:
588 *
589 * 1) Once the device is marked as being destroyed, we will fail all
590 * outgoing messages.
591 * 2) We permit incoming messages when the device is being destroyed,
592 * only to properly account for messages already sent out.
593 */
594
get_out_stor_device(struct hv_device * device)595 static inline struct storvsc_device *get_out_stor_device(
596 struct hv_device *device)
597 {
598 struct storvsc_device *stor_device;
599
600 stor_device = hv_get_drvdata(device);
601
602 if (stor_device && stor_device->destroy)
603 stor_device = NULL;
604
605 return stor_device;
606 }
607
608
storvsc_wait_to_drain(struct storvsc_device * dev)609 static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
610 {
611 dev->drain_notify = true;
612 wait_event(dev->waiting_to_drain,
613 atomic_read(&dev->num_outstanding_req) == 0);
614 dev->drain_notify = false;
615 }
616
get_in_stor_device(struct hv_device * device)617 static inline struct storvsc_device *get_in_stor_device(
618 struct hv_device *device)
619 {
620 struct storvsc_device *stor_device;
621
622 stor_device = hv_get_drvdata(device);
623
624 if (!stor_device)
625 goto get_in_err;
626
627 /*
628 * If the device is being destroyed; allow incoming
629 * traffic only to cleanup outstanding requests.
630 */
631
632 if (stor_device->destroy &&
633 (atomic_read(&stor_device->num_outstanding_req) == 0))
634 stor_device = NULL;
635
636 get_in_err:
637 return stor_device;
638
639 }
640
storvsc_change_target_cpu(struct vmbus_channel * channel,u32 old,u32 new)641 static void storvsc_change_target_cpu(struct vmbus_channel *channel, u32 old,
642 u32 new)
643 {
644 struct storvsc_device *stor_device;
645 struct vmbus_channel *cur_chn;
646 bool old_is_alloced = false;
647 struct hv_device *device;
648 unsigned long flags;
649 int cpu;
650
651 device = channel->primary_channel ?
652 channel->primary_channel->device_obj
653 : channel->device_obj;
654 stor_device = get_out_stor_device(device);
655 if (!stor_device)
656 return;
657
658 /* See storvsc_do_io() -> get_og_chn(). */
659 spin_lock_irqsave(&stor_device->lock, flags);
660
661 /*
662 * Determines if the storvsc device has other channels assigned to
663 * the "old" CPU to update the alloced_cpus mask and the stor_chns
664 * array.
665 */
666 if (device->channel != channel && device->channel->target_cpu == old) {
667 cur_chn = device->channel;
668 old_is_alloced = true;
669 goto old_is_alloced;
670 }
671 list_for_each_entry(cur_chn, &device->channel->sc_list, sc_list) {
672 if (cur_chn == channel)
673 continue;
674 if (cur_chn->target_cpu == old) {
675 old_is_alloced = true;
676 goto old_is_alloced;
677 }
678 }
679
680 old_is_alloced:
681 if (old_is_alloced)
682 WRITE_ONCE(stor_device->stor_chns[old], cur_chn);
683 else
684 cpumask_clear_cpu(old, &stor_device->alloced_cpus);
685
686 /* "Flush" the stor_chns array. */
687 for_each_possible_cpu(cpu) {
688 if (stor_device->stor_chns[cpu] && !cpumask_test_cpu(
689 cpu, &stor_device->alloced_cpus))
690 WRITE_ONCE(stor_device->stor_chns[cpu], NULL);
691 }
692
693 WRITE_ONCE(stor_device->stor_chns[new], channel);
694 cpumask_set_cpu(new, &stor_device->alloced_cpus);
695
696 spin_unlock_irqrestore(&stor_device->lock, flags);
697 }
698
storvsc_next_request_id(struct vmbus_channel * channel,u64 rqst_addr)699 static u64 storvsc_next_request_id(struct vmbus_channel *channel, u64 rqst_addr)
700 {
701 struct storvsc_cmd_request *request =
702 (struct storvsc_cmd_request *)(unsigned long)rqst_addr;
703
704 if (rqst_addr == VMBUS_RQST_INIT)
705 return VMBUS_RQST_INIT;
706 if (rqst_addr == VMBUS_RQST_RESET)
707 return VMBUS_RQST_RESET;
708
709 /*
710 * Cannot return an ID of 0, which is reserved for an unsolicited
711 * message from Hyper-V.
712 */
713 return (u64)blk_mq_unique_tag(scsi_cmd_to_rq(request->cmd)) + 1;
714 }
715
handle_sc_creation(struct vmbus_channel * new_sc)716 static void handle_sc_creation(struct vmbus_channel *new_sc)
717 {
718 struct hv_device *device = new_sc->primary_channel->device_obj;
719 struct device *dev = &device->device;
720 struct storvsc_device *stor_device;
721 struct vmstorage_channel_properties props;
722 int ret;
723
724 stor_device = get_out_stor_device(device);
725 if (!stor_device)
726 return;
727
728 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
729 new_sc->max_pkt_size = STORVSC_MAX_PKT_SIZE;
730
731 new_sc->next_request_id_callback = storvsc_next_request_id;
732
733 ret = vmbus_open(new_sc,
734 aligned_ringbuffer_size,
735 aligned_ringbuffer_size,
736 (void *)&props,
737 sizeof(struct vmstorage_channel_properties),
738 storvsc_on_channel_callback, new_sc);
739
740 /* In case vmbus_open() fails, we don't use the sub-channel. */
741 if (ret != 0) {
742 dev_err(dev, "Failed to open sub-channel: err=%d\n", ret);
743 return;
744 }
745
746 new_sc->change_target_cpu_callback = storvsc_change_target_cpu;
747
748 /* Add the sub-channel to the array of available channels. */
749 stor_device->stor_chns[new_sc->target_cpu] = new_sc;
750 cpumask_set_cpu(new_sc->target_cpu, &stor_device->alloced_cpus);
751 }
752
handle_multichannel_storage(struct hv_device * device,int max_chns)753 static void handle_multichannel_storage(struct hv_device *device, int max_chns)
754 {
755 struct device *dev = &device->device;
756 struct storvsc_device *stor_device;
757 int num_sc;
758 struct storvsc_cmd_request *request;
759 struct vstor_packet *vstor_packet;
760 int ret, t;
761
762 /*
763 * If the number of CPUs is artificially restricted, such as
764 * with maxcpus=1 on the kernel boot line, Hyper-V could offer
765 * sub-channels >= the number of CPUs. These sub-channels
766 * should not be created. The primary channel is already created
767 * and assigned to one CPU, so check against # CPUs - 1.
768 */
769 num_sc = min((int)(num_online_cpus() - 1), max_chns);
770 if (!num_sc)
771 return;
772
773 stor_device = get_out_stor_device(device);
774 if (!stor_device)
775 return;
776
777 stor_device->num_sc = num_sc;
778 request = &stor_device->init_request;
779 vstor_packet = &request->vstor_packet;
780
781 /*
782 * Establish a handler for dealing with subchannels.
783 */
784 vmbus_set_sc_create_callback(device->channel, handle_sc_creation);
785
786 /*
787 * Request the host to create sub-channels.
788 */
789 memset(request, 0, sizeof(struct storvsc_cmd_request));
790 init_completion(&request->wait_event);
791 vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
792 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
793 vstor_packet->sub_channel_count = num_sc;
794
795 ret = vmbus_sendpacket(device->channel, vstor_packet,
796 sizeof(struct vstor_packet),
797 VMBUS_RQST_INIT,
798 VM_PKT_DATA_INBAND,
799 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
800
801 if (ret != 0) {
802 dev_err(dev, "Failed to create sub-channel: err=%d\n", ret);
803 return;
804 }
805
806 t = wait_for_completion_timeout(&request->wait_event, storvsc_timeout * HZ);
807 if (t == 0) {
808 dev_err(dev, "Failed to create sub-channel: timed out\n");
809 return;
810 }
811
812 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
813 vstor_packet->status != 0) {
814 dev_err(dev, "Failed to create sub-channel: op=%d, host=0x%x\n",
815 vstor_packet->operation, vstor_packet->status);
816 return;
817 }
818
819 /*
820 * We need to do nothing here, because vmbus_process_offer()
821 * invokes channel->sc_creation_callback, which will open and use
822 * the sub-channel(s).
823 */
824 }
825
cache_wwn(struct storvsc_device * stor_device,struct vstor_packet * vstor_packet)826 static void cache_wwn(struct storvsc_device *stor_device,
827 struct vstor_packet *vstor_packet)
828 {
829 /*
830 * Cache the currently active port and node ww names.
831 */
832 if (vstor_packet->wwn_packet.primary_active) {
833 stor_device->node_name =
834 wwn_to_u64(vstor_packet->wwn_packet.primary_node_wwn);
835 stor_device->port_name =
836 wwn_to_u64(vstor_packet->wwn_packet.primary_port_wwn);
837 } else {
838 stor_device->node_name =
839 wwn_to_u64(vstor_packet->wwn_packet.secondary_node_wwn);
840 stor_device->port_name =
841 wwn_to_u64(vstor_packet->wwn_packet.secondary_port_wwn);
842 }
843 }
844
845
storvsc_execute_vstor_op(struct hv_device * device,struct storvsc_cmd_request * request,bool status_check)846 static int storvsc_execute_vstor_op(struct hv_device *device,
847 struct storvsc_cmd_request *request,
848 bool status_check)
849 {
850 struct storvsc_device *stor_device;
851 struct vstor_packet *vstor_packet;
852 int ret, t;
853
854 stor_device = get_out_stor_device(device);
855 if (!stor_device)
856 return -ENODEV;
857
858 vstor_packet = &request->vstor_packet;
859
860 init_completion(&request->wait_event);
861 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
862
863 ret = vmbus_sendpacket(device->channel, vstor_packet,
864 sizeof(struct vstor_packet),
865 VMBUS_RQST_INIT,
866 VM_PKT_DATA_INBAND,
867 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
868 if (ret != 0)
869 return ret;
870
871 t = wait_for_completion_timeout(&request->wait_event, storvsc_timeout * HZ);
872 if (t == 0)
873 return -ETIMEDOUT;
874
875 if (!status_check)
876 return ret;
877
878 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
879 vstor_packet->status != 0)
880 return -EINVAL;
881
882 return ret;
883 }
884
storvsc_channel_init(struct hv_device * device,bool is_fc)885 static int storvsc_channel_init(struct hv_device *device, bool is_fc)
886 {
887 struct storvsc_device *stor_device;
888 struct storvsc_cmd_request *request;
889 struct vstor_packet *vstor_packet;
890 int ret, i;
891 int max_chns;
892 bool process_sub_channels = false;
893
894 stor_device = get_out_stor_device(device);
895 if (!stor_device)
896 return -ENODEV;
897
898 request = &stor_device->init_request;
899 vstor_packet = &request->vstor_packet;
900
901 /*
902 * Now, initiate the vsc/vsp initialization protocol on the open
903 * channel
904 */
905 memset(request, 0, sizeof(struct storvsc_cmd_request));
906 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
907 ret = storvsc_execute_vstor_op(device, request, true);
908 if (ret)
909 return ret;
910 /*
911 * Query host supported protocol version.
912 */
913
914 for (i = 0; i < ARRAY_SIZE(protocol_version); i++) {
915 /* reuse the packet for version range supported */
916 memset(vstor_packet, 0, sizeof(struct vstor_packet));
917 vstor_packet->operation =
918 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
919
920 vstor_packet->version.major_minor = protocol_version[i];
921
922 /*
923 * The revision number is only used in Windows; set it to 0.
924 */
925 vstor_packet->version.revision = 0;
926 ret = storvsc_execute_vstor_op(device, request, false);
927 if (ret != 0)
928 return ret;
929
930 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO)
931 return -EINVAL;
932
933 if (vstor_packet->status == 0) {
934 vmstor_proto_version = protocol_version[i];
935
936 break;
937 }
938 }
939
940 if (vstor_packet->status != 0) {
941 dev_err(&device->device, "Obsolete Hyper-V version\n");
942 return -EINVAL;
943 }
944
945
946 memset(vstor_packet, 0, sizeof(struct vstor_packet));
947 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
948 ret = storvsc_execute_vstor_op(device, request, true);
949 if (ret != 0)
950 return ret;
951
952 /*
953 * Check to see if multi-channel support is there.
954 * Hosts that implement protocol version of 5.1 and above
955 * support multi-channel.
956 */
957 max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
958
959 /*
960 * Allocate state to manage the sub-channels.
961 * We allocate an array based on the number of CPU ids. This array
962 * is initially sparsely populated for the CPUs assigned to channels:
963 * primary + sub-channels. As I/Os are initiated by different CPUs,
964 * the slots for all online CPUs are populated to evenly distribute
965 * the load across all channels.
966 */
967 stor_device->stor_chns = kcalloc(nr_cpu_ids, sizeof(void *),
968 GFP_KERNEL);
969 if (stor_device->stor_chns == NULL)
970 return -ENOMEM;
971
972 device->channel->change_target_cpu_callback = storvsc_change_target_cpu;
973
974 stor_device->stor_chns[device->channel->target_cpu] = device->channel;
975 cpumask_set_cpu(device->channel->target_cpu,
976 &stor_device->alloced_cpus);
977
978 if (vstor_packet->storage_channel_properties.flags &
979 STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
980 process_sub_channels = true;
981
982 stor_device->max_transfer_bytes =
983 vstor_packet->storage_channel_properties.max_transfer_bytes;
984
985 if (!is_fc)
986 goto done;
987
988 /*
989 * For FC devices retrieve FC HBA data.
990 */
991 memset(vstor_packet, 0, sizeof(struct vstor_packet));
992 vstor_packet->operation = VSTOR_OPERATION_FCHBA_DATA;
993 ret = storvsc_execute_vstor_op(device, request, true);
994 if (ret != 0)
995 return ret;
996
997 /*
998 * Cache the currently active port and node ww names.
999 */
1000 cache_wwn(stor_device, vstor_packet);
1001
1002 done:
1003
1004 memset(vstor_packet, 0, sizeof(struct vstor_packet));
1005 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
1006 ret = storvsc_execute_vstor_op(device, request, true);
1007 if (ret != 0)
1008 return ret;
1009
1010 if (process_sub_channels)
1011 handle_multichannel_storage(device, max_chns);
1012
1013 return ret;
1014 }
1015
storvsc_handle_error(struct vmscsi_request * vm_srb,struct scsi_cmnd * scmnd,struct Scsi_Host * host,u8 asc,u8 ascq)1016 static void storvsc_handle_error(struct vmscsi_request *vm_srb,
1017 struct scsi_cmnd *scmnd,
1018 struct Scsi_Host *host,
1019 u8 asc, u8 ascq)
1020 {
1021 struct storvsc_scan_work *wrk;
1022 void (*process_err_fn)(struct work_struct *work);
1023 struct hv_host_device *host_dev = shost_priv(host);
1024
1025 switch (SRB_STATUS(vm_srb->srb_status)) {
1026 case SRB_STATUS_ERROR:
1027 case SRB_STATUS_ABORTED:
1028 case SRB_STATUS_INVALID_REQUEST:
1029 case SRB_STATUS_INTERNAL_ERROR:
1030 case SRB_STATUS_TIMEOUT:
1031 case SRB_STATUS_SELECTION_TIMEOUT:
1032 case SRB_STATUS_BUS_RESET:
1033 case SRB_STATUS_DATA_OVERRUN:
1034 if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID) {
1035 /* Check for capacity change */
1036 if ((asc == 0x2a) && (ascq == 0x9)) {
1037 process_err_fn = storvsc_device_scan;
1038 /* Retry the I/O that triggered this. */
1039 set_host_byte(scmnd, DID_REQUEUE);
1040 goto do_work;
1041 }
1042
1043 /*
1044 * Check for "Operating parameters have changed"
1045 * due to Hyper-V changing the VHD/VHDX BlockSize
1046 * when adding/removing a differencing disk. This
1047 * causes discard_granularity to change, so do a
1048 * rescan to pick up the new granularity. We don't
1049 * want scsi_report_sense() to output a message
1050 * that a sysadmin wouldn't know what to do with.
1051 */
1052 if ((asc == 0x3f) && (ascq != 0x03) &&
1053 (ascq != 0x0e)) {
1054 process_err_fn = storvsc_device_scan;
1055 set_host_byte(scmnd, DID_REQUEUE);
1056 goto do_work;
1057 }
1058
1059 /*
1060 * Otherwise, let upper layer deal with the
1061 * error when sense message is present
1062 */
1063 return;
1064 }
1065
1066 /*
1067 * If there is an error; offline the device since all
1068 * error recovery strategies would have already been
1069 * deployed on the host side. However, if the command
1070 * were a pass-through command deal with it appropriately.
1071 */
1072 switch (scmnd->cmnd[0]) {
1073 case ATA_16:
1074 case ATA_12:
1075 set_host_byte(scmnd, DID_PASSTHROUGH);
1076 break;
1077 /*
1078 * On some Hyper-V hosts TEST_UNIT_READY command can
1079 * return SRB_STATUS_ERROR. Let the upper level code
1080 * deal with it based on the sense information.
1081 */
1082 case TEST_UNIT_READY:
1083 break;
1084 default:
1085 set_host_byte(scmnd, DID_ERROR);
1086 }
1087 return;
1088
1089 case SRB_STATUS_INVALID_LUN:
1090 set_host_byte(scmnd, DID_NO_CONNECT);
1091 process_err_fn = storvsc_remove_lun;
1092 goto do_work;
1093
1094 }
1095 return;
1096
1097 do_work:
1098 /*
1099 * We need to schedule work to process this error; schedule it.
1100 */
1101 wrk = kmalloc_obj(struct storvsc_scan_work, GFP_ATOMIC);
1102 if (!wrk) {
1103 set_host_byte(scmnd, DID_BAD_TARGET);
1104 return;
1105 }
1106
1107 wrk->host = host;
1108 wrk->lun = vm_srb->lun;
1109 wrk->tgt_id = vm_srb->target_id;
1110 INIT_WORK(&wrk->work, process_err_fn);
1111 queue_work(host_dev->handle_error_wq, &wrk->work);
1112 }
1113
1114
storvsc_command_completion(struct storvsc_cmd_request * cmd_request,struct storvsc_device * stor_dev)1115 static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request,
1116 struct storvsc_device *stor_dev)
1117 {
1118 struct scsi_cmnd *scmnd = cmd_request->cmd;
1119 struct scsi_sense_hdr sense_hdr;
1120 struct vmscsi_request *vm_srb;
1121 u32 data_transfer_length;
1122 struct Scsi_Host *host;
1123 u32 payload_sz = cmd_request->payload_sz;
1124 void *payload = cmd_request->payload;
1125 bool sense_ok;
1126
1127 host = stor_dev->host;
1128
1129 vm_srb = &cmd_request->vstor_packet.vm_srb;
1130 data_transfer_length = vm_srb->data_transfer_length;
1131
1132 scmnd->result = vm_srb->scsi_status;
1133
1134 if (scmnd->result) {
1135 sense_ok = scsi_normalize_sense(scmnd->sense_buffer,
1136 SCSI_SENSE_BUFFERSIZE, &sense_hdr);
1137
1138 if (sense_ok && do_logging(STORVSC_LOGGING_WARN))
1139 scsi_print_sense_hdr(scmnd->device, "storvsc",
1140 &sense_hdr);
1141 }
1142
1143 if (vm_srb->srb_status != SRB_STATUS_SUCCESS) {
1144 storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
1145 sense_hdr.ascq);
1146 /*
1147 * The Windows driver set data_transfer_length on
1148 * SRB_STATUS_DATA_OVERRUN. On other errors, this value
1149 * is untouched. In these cases we set it to 0.
1150 */
1151 if (vm_srb->srb_status != SRB_STATUS_DATA_OVERRUN)
1152 data_transfer_length = 0;
1153 }
1154
1155 /* Validate data_transfer_length (from Hyper-V) */
1156 if (data_transfer_length > cmd_request->payload->range.len)
1157 data_transfer_length = cmd_request->payload->range.len;
1158
1159 scsi_set_resid(scmnd,
1160 cmd_request->payload->range.len - data_transfer_length);
1161
1162 scsi_done(scmnd);
1163
1164 if (payload_sz >
1165 sizeof(struct vmbus_channel_packet_multipage_buffer))
1166 kfree(payload);
1167 }
1168
1169 /*
1170 * The current SCSI handling on the host side does not correctly handle:
1171 * INQUIRY with page code 0x80, MODE_SENSE / MODE_SENSE_10 with cmd[2] == 0x1c,
1172 * and (for FC) MAINTENANCE_IN / PERSISTENT_RESERVE_IN passthrough.
1173 */
storvsc_host_mishandles_cmd(u8 opcode,struct hv_device * device)1174 static bool storvsc_host_mishandles_cmd(u8 opcode, struct hv_device *device)
1175 {
1176 switch (opcode) {
1177 case INQUIRY:
1178 case MODE_SENSE:
1179 case MODE_SENSE_10:
1180 return true;
1181 case MAINTENANCE_IN:
1182 case PERSISTENT_RESERVE_IN:
1183 return hv_dev_is_fc(device);
1184 default:
1185 return false;
1186 }
1187 }
1188
storvsc_on_io_completion(struct storvsc_device * stor_device,struct vstor_packet * vstor_packet,struct storvsc_cmd_request * request)1189 static void storvsc_on_io_completion(struct storvsc_device *stor_device,
1190 struct vstor_packet *vstor_packet,
1191 struct storvsc_cmd_request *request)
1192 {
1193 struct vstor_packet *stor_pkt;
1194 struct hv_device *device = stor_device->device;
1195
1196 stor_pkt = &request->vstor_packet;
1197
1198 /*
1199 * Setup srb and scsi status so this won't be fatal.
1200 * We do this so we can distinguish truly fatal failues
1201 * (srb status == 0x4) and off-line the device in that case.
1202 */
1203
1204 if (storvsc_host_mishandles_cmd(stor_pkt->vm_srb.cdb[0], device)) {
1205 vstor_packet->vm_srb.scsi_status = 0;
1206 vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
1207 }
1208
1209 /* Copy over the status...etc */
1210 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
1211 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
1212
1213 /*
1214 * Copy over the sense_info_length, but limit to the known max
1215 * size if Hyper-V returns a bad value.
1216 */
1217 stor_pkt->vm_srb.sense_info_length = min_t(u8, STORVSC_SENSE_BUFFER_SIZE,
1218 vstor_packet->vm_srb.sense_info_length);
1219
1220 if (vstor_packet->vm_srb.scsi_status != 0 ||
1221 vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS) {
1222
1223 /*
1224 * Log TEST_UNIT_READY errors only as warnings. Hyper-V can
1225 * return errors when detecting devices using TEST_UNIT_READY,
1226 * and logging these as errors produces unhelpful noise.
1227 */
1228 int loglevel = (stor_pkt->vm_srb.cdb[0] == TEST_UNIT_READY) ?
1229 STORVSC_LOGGING_WARN : STORVSC_LOGGING_ERROR;
1230
1231 storvsc_log_ratelimited(device, loglevel,
1232 "tag#%d cmd 0x%x status: scsi 0x%x srb 0x%x host 0x%x\n",
1233 scsi_cmd_to_rq(request->cmd)->tag,
1234 stor_pkt->vm_srb.cdb[0],
1235 vstor_packet->vm_srb.scsi_status,
1236 vstor_packet->vm_srb.srb_status,
1237 vstor_packet->status);
1238 }
1239
1240 if (vstor_packet->vm_srb.scsi_status == SAM_STAT_CHECK_CONDITION &&
1241 (vstor_packet->vm_srb.srb_status & SRB_STATUS_AUTOSENSE_VALID))
1242 memcpy(request->cmd->sense_buffer,
1243 vstor_packet->vm_srb.sense_data,
1244 stor_pkt->vm_srb.sense_info_length);
1245
1246 stor_pkt->vm_srb.data_transfer_length =
1247 vstor_packet->vm_srb.data_transfer_length;
1248
1249 storvsc_command_completion(request, stor_device);
1250
1251 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
1252 stor_device->drain_notify)
1253 wake_up(&stor_device->waiting_to_drain);
1254 }
1255
storvsc_on_receive(struct storvsc_device * stor_device,struct vstor_packet * vstor_packet,struct storvsc_cmd_request * request)1256 static void storvsc_on_receive(struct storvsc_device *stor_device,
1257 struct vstor_packet *vstor_packet,
1258 struct storvsc_cmd_request *request)
1259 {
1260 struct hv_host_device *host_dev;
1261 switch (vstor_packet->operation) {
1262 case VSTOR_OPERATION_COMPLETE_IO:
1263 storvsc_on_io_completion(stor_device, vstor_packet, request);
1264 break;
1265
1266 case VSTOR_OPERATION_REMOVE_DEVICE:
1267 case VSTOR_OPERATION_ENUMERATE_BUS:
1268 host_dev = shost_priv(stor_device->host);
1269 queue_work(
1270 host_dev->handle_error_wq, &host_dev->host_scan_work);
1271 break;
1272
1273 case VSTOR_OPERATION_FCHBA_DATA:
1274 cache_wwn(stor_device, vstor_packet);
1275 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1276 fc_host_node_name(stor_device->host) = stor_device->node_name;
1277 fc_host_port_name(stor_device->host) = stor_device->port_name;
1278 #endif
1279 break;
1280 default:
1281 break;
1282 }
1283 }
1284
storvsc_on_channel_callback(void * context)1285 static void storvsc_on_channel_callback(void *context)
1286 {
1287 struct vmbus_channel *channel = (struct vmbus_channel *)context;
1288 const struct vmpacket_descriptor *desc;
1289 struct hv_device *device;
1290 struct storvsc_device *stor_device;
1291 struct Scsi_Host *shost;
1292 unsigned long time_limit = jiffies + msecs_to_jiffies(CALLBACK_TIMEOUT);
1293
1294 if (channel->primary_channel != NULL)
1295 device = channel->primary_channel->device_obj;
1296 else
1297 device = channel->device_obj;
1298
1299 stor_device = get_in_stor_device(device);
1300 if (!stor_device)
1301 return;
1302
1303 shost = stor_device->host;
1304
1305 foreach_vmbus_pkt(desc, channel) {
1306 struct vstor_packet *packet = hv_pkt_data(desc);
1307 struct storvsc_cmd_request *request = NULL;
1308 u32 pktlen = hv_pkt_datalen(desc);
1309 u64 rqst_id = desc->trans_id;
1310 u32 minlen = rqst_id ? sizeof(struct vstor_packet) :
1311 sizeof(enum vstor_packet_operation);
1312
1313 if (unlikely(time_after(jiffies, time_limit))) {
1314 hv_pkt_iter_close(channel);
1315 return;
1316 }
1317
1318 if (pktlen < minlen) {
1319 dev_err(&device->device,
1320 "Invalid pkt: id=%llu, len=%u, minlen=%u\n",
1321 rqst_id, pktlen, minlen);
1322 continue;
1323 }
1324
1325 if (rqst_id == VMBUS_RQST_INIT) {
1326 request = &stor_device->init_request;
1327 } else if (rqst_id == VMBUS_RQST_RESET) {
1328 request = &stor_device->reset_request;
1329 } else {
1330 /* Hyper-V can send an unsolicited message with ID of 0 */
1331 if (rqst_id == 0) {
1332 /*
1333 * storvsc_on_receive() looks at the vstor_packet in the message
1334 * from the ring buffer.
1335 *
1336 * - If the operation in the vstor_packet is COMPLETE_IO, then
1337 * we call storvsc_on_io_completion(), and dereference the
1338 * guest memory address. Make sure we don't call
1339 * storvsc_on_io_completion() with a guest memory address
1340 * that is zero if Hyper-V were to construct and send such
1341 * a bogus packet.
1342 *
1343 * - If the operation in the vstor_packet is FCHBA_DATA, then
1344 * we call cache_wwn(), and access the data payload area of
1345 * the packet (wwn_packet); however, there is no guarantee
1346 * that the packet is big enough to contain such area.
1347 * Future-proof the code by rejecting such a bogus packet.
1348 */
1349 if (packet->operation == VSTOR_OPERATION_COMPLETE_IO ||
1350 packet->operation == VSTOR_OPERATION_FCHBA_DATA) {
1351 dev_err(&device->device, "Invalid packet with ID of 0\n");
1352 continue;
1353 }
1354 } else {
1355 struct scsi_cmnd *scmnd;
1356
1357 /* Transaction 'rqst_id' corresponds to tag 'rqst_id - 1' */
1358 scmnd = scsi_host_find_tag(shost, rqst_id - 1);
1359 if (scmnd == NULL) {
1360 dev_err(&device->device, "Incorrect transaction ID\n");
1361 continue;
1362 }
1363 request = (struct storvsc_cmd_request *)scsi_cmd_priv(scmnd);
1364 scsi_dma_unmap(scmnd);
1365 }
1366
1367 storvsc_on_receive(stor_device, packet, request);
1368 continue;
1369 }
1370
1371 memcpy(&request->vstor_packet, packet,
1372 sizeof(struct vstor_packet));
1373 complete(&request->wait_event);
1374 }
1375 }
1376
storvsc_connect_to_vsp(struct hv_device * device,u32 ring_size,bool is_fc)1377 static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
1378 bool is_fc)
1379 {
1380 struct vmstorage_channel_properties props;
1381 int ret;
1382
1383 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
1384
1385 device->channel->max_pkt_size = STORVSC_MAX_PKT_SIZE;
1386 device->channel->next_request_id_callback = storvsc_next_request_id;
1387
1388 ret = vmbus_open(device->channel,
1389 ring_size,
1390 ring_size,
1391 (void *)&props,
1392 sizeof(struct vmstorage_channel_properties),
1393 storvsc_on_channel_callback, device->channel);
1394
1395 if (ret != 0)
1396 return ret;
1397
1398 ret = storvsc_channel_init(device, is_fc);
1399 if (ret)
1400 vmbus_close(device->channel);
1401
1402 return ret;
1403 }
1404
storvsc_dev_remove(struct hv_device * device)1405 static int storvsc_dev_remove(struct hv_device *device)
1406 {
1407 struct storvsc_device *stor_device;
1408
1409 stor_device = hv_get_drvdata(device);
1410
1411 stor_device->destroy = true;
1412
1413 /* Make sure flag is set before waiting */
1414 wmb();
1415
1416 /*
1417 * At this point, all outbound traffic should be disable. We
1418 * only allow inbound traffic (responses) to proceed so that
1419 * outstanding requests can be completed.
1420 */
1421
1422 storvsc_wait_to_drain(stor_device);
1423
1424 /*
1425 * Since we have already drained, we don't need to busy wait
1426 * as was done in final_release_stor_device()
1427 * Note that we cannot set the ext pointer to NULL until
1428 * we have drained - to drain the outgoing packets, we need to
1429 * allow incoming packets.
1430 */
1431 hv_set_drvdata(device, NULL);
1432
1433 /* Close the channel */
1434 vmbus_close(device->channel);
1435
1436 kfree(stor_device->stor_chns);
1437 kfree(stor_device);
1438 return 0;
1439 }
1440
get_og_chn(struct storvsc_device * stor_device,u16 q_num)1441 static struct vmbus_channel *get_og_chn(struct storvsc_device *stor_device,
1442 u16 q_num)
1443 {
1444 u16 slot = 0;
1445 u16 hash_qnum;
1446 const struct cpumask *node_mask;
1447 int num_channels, tgt_cpu;
1448
1449 if (stor_device->num_sc == 0) {
1450 stor_device->stor_chns[q_num] = stor_device->device->channel;
1451 return stor_device->device->channel;
1452 }
1453
1454 /*
1455 * Our channel array could be sparsley populated and we
1456 * initiated I/O on a processor/hw-q that does not
1457 * currently have a designated channel. Fix this.
1458 * The strategy is simple:
1459 * I. Prefer the channel associated with the current CPU
1460 * II. Ensure NUMA locality
1461 * III. Distribute evenly (best effort)
1462 */
1463
1464 /* Prefer the channel on the I/O issuing processor/hw-q */
1465 if (cpumask_test_cpu(q_num, &stor_device->alloced_cpus))
1466 return stor_device->stor_chns[q_num];
1467
1468 node_mask = cpumask_of_node(cpu_to_node(q_num));
1469
1470 num_channels = 0;
1471 for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
1472 if (cpumask_test_cpu(tgt_cpu, node_mask))
1473 num_channels++;
1474 }
1475 if (num_channels == 0) {
1476 stor_device->stor_chns[q_num] = stor_device->device->channel;
1477 return stor_device->device->channel;
1478 }
1479
1480 hash_qnum = q_num;
1481 while (hash_qnum >= num_channels)
1482 hash_qnum -= num_channels;
1483
1484 for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
1485 if (!cpumask_test_cpu(tgt_cpu, node_mask))
1486 continue;
1487 if (slot == hash_qnum)
1488 break;
1489 slot++;
1490 }
1491
1492 stor_device->stor_chns[q_num] = stor_device->stor_chns[tgt_cpu];
1493
1494 return stor_device->stor_chns[q_num];
1495 }
1496
1497
storvsc_do_io(struct hv_device * device,struct storvsc_cmd_request * request,u16 q_num)1498 static int storvsc_do_io(struct hv_device *device,
1499 struct storvsc_cmd_request *request, u16 q_num)
1500 {
1501 struct storvsc_device *stor_device;
1502 struct vstor_packet *vstor_packet;
1503 struct vmbus_channel *outgoing_channel, *channel;
1504 unsigned long flags;
1505 int ret = 0;
1506 const struct cpumask *node_mask;
1507 int tgt_cpu;
1508
1509 vstor_packet = &request->vstor_packet;
1510 stor_device = get_out_stor_device(device);
1511
1512 if (!stor_device)
1513 return -ENODEV;
1514
1515
1516 request->device = device;
1517 /*
1518 * Select an appropriate channel to send the request out.
1519 */
1520 /* See storvsc_change_target_cpu(). */
1521 outgoing_channel = READ_ONCE(stor_device->stor_chns[q_num]);
1522 if (outgoing_channel != NULL) {
1523 if (hv_get_avail_to_write_percent(&outgoing_channel->outbound)
1524 > ring_avail_percent_lowater)
1525 goto found_channel;
1526
1527 /*
1528 * Channel is busy, try to find a channel on the same NUMA node
1529 */
1530 node_mask = cpumask_of_node(cpu_to_node(q_num));
1531 for_each_cpu_wrap(tgt_cpu, &stor_device->alloced_cpus,
1532 q_num + 1) {
1533 if (!cpumask_test_cpu(tgt_cpu, node_mask))
1534 continue;
1535 channel = READ_ONCE(stor_device->stor_chns[tgt_cpu]);
1536 if (!channel)
1537 continue;
1538 if (hv_get_avail_to_write_percent(&channel->outbound)
1539 > ring_avail_percent_lowater) {
1540 outgoing_channel = channel;
1541 goto found_channel;
1542 }
1543 }
1544
1545 /*
1546 * If we reach here, all the channels on the current
1547 * NUMA node are busy. Try to find a channel in
1548 * all NUMA nodes
1549 */
1550 for_each_cpu_wrap(tgt_cpu, &stor_device->alloced_cpus,
1551 q_num + 1) {
1552 channel = READ_ONCE(stor_device->stor_chns[tgt_cpu]);
1553 if (!channel)
1554 continue;
1555 if (hv_get_avail_to_write_percent(&channel->outbound)
1556 > ring_avail_percent_lowater) {
1557 outgoing_channel = channel;
1558 goto found_channel;
1559 }
1560 }
1561 /*
1562 * If we reach here, all the channels are busy. Use the
1563 * original channel found.
1564 */
1565 } else {
1566 spin_lock_irqsave(&stor_device->lock, flags);
1567 outgoing_channel = stor_device->stor_chns[q_num];
1568 if (outgoing_channel != NULL) {
1569 spin_unlock_irqrestore(&stor_device->lock, flags);
1570 goto found_channel;
1571 }
1572 outgoing_channel = get_og_chn(stor_device, q_num);
1573 spin_unlock_irqrestore(&stor_device->lock, flags);
1574 }
1575
1576 found_channel:
1577 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
1578
1579 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
1580
1581
1582 vstor_packet->vm_srb.sense_info_length = STORVSC_SENSE_BUFFER_SIZE;
1583
1584
1585 vstor_packet->vm_srb.data_transfer_length =
1586 request->payload->range.len;
1587
1588 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
1589
1590 if (request->payload->range.len) {
1591
1592 ret = vmbus_sendpacket_mpb_desc(outgoing_channel,
1593 request->payload, request->payload_sz,
1594 vstor_packet,
1595 sizeof(struct vstor_packet),
1596 (unsigned long)request);
1597 } else {
1598 ret = vmbus_sendpacket(outgoing_channel, vstor_packet,
1599 sizeof(struct vstor_packet),
1600 (unsigned long)request,
1601 VM_PKT_DATA_INBAND,
1602 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1603 }
1604
1605 if (ret != 0)
1606 return ret;
1607
1608 atomic_inc(&stor_device->num_outstanding_req);
1609
1610 return ret;
1611 }
1612
storvsc_device_alloc(struct scsi_device * sdevice)1613 static int storvsc_device_alloc(struct scsi_device *sdevice)
1614 {
1615 /*
1616 * Set blist flag to permit the reading of the VPD pages even when
1617 * the target may claim SPC-2 compliance. MSFT targets currently
1618 * claim SPC-2 compliance while they implement post SPC-2 features.
1619 * With this flag we can correctly handle WRITE_SAME_16 issues.
1620 *
1621 * Hypervisor reports SCSI_UNKNOWN type for DVD ROM device but
1622 * still supports REPORT LUN.
1623 */
1624 sdevice->sdev_bflags = BLIST_REPORTLUN2 | BLIST_TRY_VPD_PAGES;
1625
1626 return 0;
1627 }
1628
storvsc_sdev_configure(struct scsi_device * sdevice,struct queue_limits * lim)1629 static int storvsc_sdev_configure(struct scsi_device *sdevice,
1630 struct queue_limits *lim)
1631 {
1632 blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
1633
1634 /* storvsc devices don't support MAINTENANCE_IN SCSI cmd */
1635 sdevice->no_report_opcodes = 1;
1636 sdevice->no_write_same = 1;
1637
1638 /*
1639 * If the host is WIN8 R2, claim conformance to SPC-3
1640 * if the device is a MSFT virtual device. If the host is
1641 * WIN10 or newer, allow write_same.
1642 */
1643 if (!strncmp(sdevice->vendor, "Msft", 4)) {
1644 switch (vmstor_proto_version) {
1645 case VMSTOR_PROTO_VERSION_WIN8_1:
1646 sdevice->scsi_level = SCSI_SPC_3;
1647 break;
1648 }
1649
1650 if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN10)
1651 sdevice->no_write_same = 0;
1652 }
1653
1654 return 0;
1655 }
1656
storvsc_get_chs(struct scsi_device * sdev,struct gendisk * unused,sector_t capacity,int * info)1657 static int storvsc_get_chs(struct scsi_device *sdev, struct gendisk *unused,
1658 sector_t capacity, int *info)
1659 {
1660 sector_t nsect = capacity;
1661 sector_t cylinders = nsect;
1662 int heads, sectors_pt;
1663
1664 /*
1665 * We are making up these values; let us keep it simple.
1666 */
1667 heads = 0xff;
1668 sectors_pt = 0x3f; /* Sectors per track */
1669 sector_div(cylinders, heads * sectors_pt);
1670 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1671 cylinders = 0xffff;
1672
1673 info[0] = heads;
1674 info[1] = sectors_pt;
1675 info[2] = (int)cylinders;
1676
1677 return 0;
1678 }
1679
storvsc_host_reset_handler(struct scsi_cmnd * scmnd)1680 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
1681 {
1682 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1683 struct hv_device *device = host_dev->dev;
1684
1685 struct storvsc_device *stor_device;
1686 struct storvsc_cmd_request *request;
1687 struct vstor_packet *vstor_packet;
1688 int ret, t;
1689
1690 stor_device = get_out_stor_device(device);
1691 if (!stor_device)
1692 return FAILED;
1693
1694 request = &stor_device->reset_request;
1695 vstor_packet = &request->vstor_packet;
1696 memset(vstor_packet, 0, sizeof(struct vstor_packet));
1697
1698 init_completion(&request->wait_event);
1699
1700 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1701 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1702 vstor_packet->vm_srb.path_id = stor_device->path_id;
1703
1704 ret = vmbus_sendpacket(device->channel, vstor_packet,
1705 sizeof(struct vstor_packet),
1706 VMBUS_RQST_RESET,
1707 VM_PKT_DATA_INBAND,
1708 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1709 if (ret != 0)
1710 return FAILED;
1711
1712 t = wait_for_completion_timeout(&request->wait_event, storvsc_timeout * HZ);
1713 if (t == 0)
1714 return TIMEOUT_ERROR;
1715
1716
1717 /*
1718 * At this point, all outstanding requests in the adapter
1719 * should have been flushed out and return to us
1720 * There is a potential race here where the host may be in
1721 * the process of responding when we return from here.
1722 * Just wait for all in-transit packets to be accounted for
1723 * before we return from here.
1724 */
1725 storvsc_wait_to_drain(stor_device);
1726
1727 return SUCCESS;
1728 }
1729
1730 /*
1731 * The host guarantees to respond to each command, although I/O latencies might
1732 * be unbounded on Azure. Reset the timer unconditionally to give the host a
1733 * chance to perform EH.
1734 */
storvsc_eh_timed_out(struct scsi_cmnd * scmnd)1735 static enum scsi_timeout_action storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
1736 {
1737 return SCSI_EH_RESET_TIMER;
1738 }
1739
storvsc_queuecommand(struct Scsi_Host * host,struct scsi_cmnd * scmnd)1740 static enum scsi_qc_status storvsc_queuecommand(struct Scsi_Host *host,
1741 struct scsi_cmnd *scmnd)
1742 {
1743 int ret;
1744 struct hv_host_device *host_dev = shost_priv(host);
1745 struct hv_device *dev = host_dev->dev;
1746 struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd);
1747 struct scatterlist *sgl;
1748 struct vmscsi_request *vm_srb;
1749 struct vmbus_packet_mpb_array *payload;
1750 u32 payload_sz;
1751 u32 length;
1752
1753 /* Setup the cmd request */
1754 cmd_request->cmd = scmnd;
1755
1756 memset(&cmd_request->vstor_packet, 0, sizeof(struct vstor_packet));
1757 vm_srb = &cmd_request->vstor_packet.vm_srb;
1758 vm_srb->time_out_value = 60;
1759
1760 vm_srb->srb_flags |=
1761 SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
1762
1763 if (scmnd->device->tagged_supported) {
1764 vm_srb->srb_flags |=
1765 (SRB_FLAGS_QUEUE_ACTION_ENABLE | SRB_FLAGS_NO_QUEUE_FREEZE);
1766 vm_srb->queue_tag = SP_UNTAGGED;
1767 vm_srb->queue_action = SRB_SIMPLE_TAG_REQUEST;
1768 }
1769
1770 /* Build the SRB */
1771 switch (scmnd->sc_data_direction) {
1772 case DMA_TO_DEVICE:
1773 vm_srb->data_in = WRITE_TYPE;
1774 vm_srb->srb_flags |= SRB_FLAGS_DATA_OUT;
1775 break;
1776 case DMA_FROM_DEVICE:
1777 vm_srb->data_in = READ_TYPE;
1778 vm_srb->srb_flags |= SRB_FLAGS_DATA_IN;
1779 break;
1780 case DMA_NONE:
1781 vm_srb->data_in = UNKNOWN_TYPE;
1782 vm_srb->srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER;
1783 break;
1784 default:
1785 /*
1786 * This is DMA_BIDIRECTIONAL or something else we are never
1787 * supposed to see here.
1788 */
1789 WARN(1, "Unexpected data direction: %d\n",
1790 scmnd->sc_data_direction);
1791 return -EINVAL;
1792 }
1793
1794
1795 vm_srb->port_number = host_dev->port;
1796 vm_srb->path_id = scmnd->device->channel;
1797 vm_srb->target_id = scmnd->device->id;
1798 vm_srb->lun = scmnd->device->lun;
1799
1800 vm_srb->cdb_length = scmnd->cmd_len;
1801
1802 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1803
1804 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1805
1806 length = scsi_bufflen(scmnd);
1807 payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
1808 payload->range.len = 0;
1809 payload_sz = 0;
1810
1811 if (scsi_sg_count(scmnd)) {
1812 unsigned long offset_in_hvpg = offset_in_hvpage(sgl->offset);
1813 unsigned int hvpg_count = HVPFN_UP(offset_in_hvpg + length);
1814 struct scatterlist *sg;
1815 unsigned long hvpfn, hvpfns_to_add;
1816 int j, i = 0, sg_count;
1817
1818 payload_sz = (hvpg_count * sizeof(u64) +
1819 sizeof(struct vmbus_packet_mpb_array));
1820
1821 if (hvpg_count > MAX_PAGE_BUFFER_COUNT) {
1822 payload = kzalloc(payload_sz, GFP_ATOMIC);
1823 if (!payload)
1824 return SCSI_MLQUEUE_DEVICE_BUSY;
1825 }
1826
1827 payload->rangecount = 1;
1828 payload->range.len = length;
1829 payload->range.offset = offset_in_hvpg;
1830
1831 sg_count = scsi_dma_map(scmnd);
1832 if (sg_count < 0) {
1833 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1834 goto err_free_payload;
1835 }
1836
1837 for_each_sg(sgl, sg, sg_count, j) {
1838 /*
1839 * Init values for the current sgl entry. hvpfns_to_add
1840 * is in units of Hyper-V size pages. Handling the
1841 * PAGE_SIZE != HV_HYP_PAGE_SIZE case also handles
1842 * values of sgl->offset that are larger than PAGE_SIZE.
1843 * Such offsets are handled even on other than the first
1844 * sgl entry, provided they are a multiple of PAGE_SIZE.
1845 */
1846 hvpfn = HVPFN_DOWN(sg_dma_address(sg));
1847 hvpfns_to_add = HVPFN_UP(sg_dma_address(sg) +
1848 sg_dma_len(sg)) - hvpfn;
1849
1850 /*
1851 * Fill the next portion of the PFN array with
1852 * sequential Hyper-V PFNs for the continguous physical
1853 * memory described by the sgl entry. The end of the
1854 * last sgl should be reached at the same time that
1855 * the PFN array is filled.
1856 */
1857 while (hvpfns_to_add--)
1858 payload->range.pfn_array[i++] = hvpfn++;
1859 }
1860 }
1861
1862 cmd_request->payload = payload;
1863 cmd_request->payload_sz = payload_sz;
1864
1865 /* Invokes the vsc to start an IO */
1866 migrate_disable();
1867 ret = storvsc_do_io(dev, cmd_request, smp_processor_id());
1868 migrate_enable();
1869
1870 if (ret)
1871 scsi_dma_unmap(scmnd);
1872
1873 if (ret == -EAGAIN) {
1874 /* no more space */
1875 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1876 goto err_free_payload;
1877 }
1878
1879 return 0;
1880
1881 err_free_payload:
1882 if (payload_sz > sizeof(cmd_request->mpb))
1883 kfree(payload);
1884
1885 return ret;
1886 }
1887
1888 static struct scsi_host_template scsi_driver = {
1889 .module = THIS_MODULE,
1890 .name = "storvsc_host_t",
1891 .cmd_size = sizeof(struct storvsc_cmd_request),
1892 .bios_param = storvsc_get_chs,
1893 .queuecommand = storvsc_queuecommand,
1894 .eh_host_reset_handler = storvsc_host_reset_handler,
1895 .proc_name = "storvsc_host",
1896 .eh_timed_out = storvsc_eh_timed_out,
1897 .sdev_init = storvsc_device_alloc,
1898 .sdev_configure = storvsc_sdev_configure,
1899 .cmd_per_lun = 2048,
1900 .this_id = -1,
1901 /* Ensure there are no gaps in presented sgls */
1902 .virt_boundary_mask = HV_HYP_PAGE_SIZE - 1,
1903 .no_write_same = 1,
1904 .track_queue_depth = 1,
1905 .change_queue_depth = storvsc_change_queue_depth,
1906 };
1907
1908 enum {
1909 SCSI_GUID,
1910 IDE_GUID,
1911 SFC_GUID,
1912 };
1913
1914 static const struct hv_vmbus_device_id id_table[] = {
1915 /* SCSI guid */
1916 { HV_SCSI_GUID,
1917 .driver_data = SCSI_GUID
1918 },
1919 /* IDE guid */
1920 { HV_IDE_GUID,
1921 .driver_data = IDE_GUID
1922 },
1923 /* Fibre Channel GUID */
1924 {
1925 HV_SYNTHFC_GUID,
1926 .driver_data = SFC_GUID
1927 },
1928 { },
1929 };
1930
1931 MODULE_DEVICE_TABLE(vmbus, id_table);
1932
1933 static const struct { guid_t guid; } fc_guid = { HV_SYNTHFC_GUID };
1934
hv_dev_is_fc(struct hv_device * hv_dev)1935 static bool hv_dev_is_fc(struct hv_device *hv_dev)
1936 {
1937 return guid_equal(&fc_guid.guid, &hv_dev->dev_type);
1938 }
1939
storvsc_probe(struct hv_device * device,const struct hv_vmbus_device_id * dev_id)1940 static int storvsc_probe(struct hv_device *device,
1941 const struct hv_vmbus_device_id *dev_id)
1942 {
1943 int ret;
1944 int num_cpus = num_online_cpus();
1945 int num_present_cpus = num_present_cpus();
1946 struct Scsi_Host *host;
1947 struct hv_host_device *host_dev;
1948 bool dev_is_ide = dev_id->driver_data == IDE_GUID;
1949 bool is_fc = dev_id->driver_data == SFC_GUID;
1950 int target = 0;
1951 struct storvsc_device *stor_device;
1952 int max_sub_channels = 0;
1953 u32 max_xfer_bytes;
1954
1955 /*
1956 * We support sub-channels for storage on SCSI and FC controllers.
1957 * The number of sub-channels offerred is based on the number of
1958 * VCPUs in the guest.
1959 */
1960 if (!dev_is_ide)
1961 max_sub_channels =
1962 (num_cpus - 1) / storvsc_vcpus_per_sub_channel;
1963
1964 scsi_driver.can_queue = max_outstanding_req_per_channel *
1965 (max_sub_channels + 1) *
1966 (100 - ring_avail_percent_lowater) / 100;
1967
1968 host = scsi_host_alloc(&scsi_driver,
1969 sizeof(struct hv_host_device));
1970 if (!host)
1971 return -ENOMEM;
1972
1973 host_dev = shost_priv(host);
1974 memset(host_dev, 0, sizeof(struct hv_host_device));
1975
1976 host_dev->port = host->host_no;
1977 host_dev->dev = device;
1978 host_dev->host = host;
1979
1980
1981 stor_device = kzalloc_obj(struct storvsc_device);
1982 if (!stor_device) {
1983 ret = -ENOMEM;
1984 goto err_out0;
1985 }
1986
1987 stor_device->destroy = false;
1988 init_waitqueue_head(&stor_device->waiting_to_drain);
1989 stor_device->device = device;
1990 stor_device->host = host;
1991 spin_lock_init(&stor_device->lock);
1992 hv_set_drvdata(device, stor_device);
1993 dma_set_min_align_mask(&device->device, HV_HYP_PAGE_SIZE - 1);
1994
1995 stor_device->port_number = host->host_no;
1996 ret = storvsc_connect_to_vsp(device, aligned_ringbuffer_size, is_fc);
1997 if (ret)
1998 goto err_out1;
1999
2000 host_dev->path = stor_device->path_id;
2001 host_dev->target = stor_device->target_id;
2002
2003 switch (dev_id->driver_data) {
2004 case SFC_GUID:
2005 host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
2006 host->max_id = STORVSC_FC_MAX_TARGETS;
2007 host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
2008 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2009 host->transportt = fc_transport_template;
2010 #endif
2011 break;
2012
2013 case SCSI_GUID:
2014 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
2015 host->max_id = STORVSC_MAX_TARGETS;
2016 host->max_channel = STORVSC_MAX_CHANNELS - 1;
2017 break;
2018
2019 default:
2020 host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
2021 host->max_id = STORVSC_IDE_MAX_TARGETS;
2022 host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
2023 break;
2024 }
2025 /* max cmd length */
2026 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
2027 /*
2028 * Any reasonable Hyper-V configuration should provide
2029 * max_transfer_bytes value aligning to HV_HYP_PAGE_SIZE,
2030 * protecting it from any weird value.
2031 */
2032 max_xfer_bytes = round_down(stor_device->max_transfer_bytes, HV_HYP_PAGE_SIZE);
2033 if (is_fc)
2034 max_xfer_bytes = min(max_xfer_bytes, STORVSC_FC_MAX_XFER_SIZE);
2035
2036 /* max_hw_sectors_kb */
2037 host->max_sectors = max_xfer_bytes >> 9;
2038 /*
2039 * There are 2 requirements for Hyper-V storvsc sgl segments,
2040 * based on which the below calculation for max segments is
2041 * done:
2042 *
2043 * 1. Except for the first and last sgl segment, all sgl segments
2044 * should be align to HV_HYP_PAGE_SIZE, that also means the
2045 * maximum number of segments in a sgl can be calculated by
2046 * dividing the total max transfer length by HV_HYP_PAGE_SIZE.
2047 *
2048 * 2. Except for the first and last, each entry in the SGL must
2049 * have an offset that is a multiple of HV_HYP_PAGE_SIZE.
2050 */
2051 host->sg_tablesize = (max_xfer_bytes >> HV_HYP_PAGE_SHIFT) + 1;
2052 /*
2053 * For non-IDE disks, the host supports multiple channels.
2054 * Set the number of HW queues we are supporting.
2055 */
2056 if (!dev_is_ide) {
2057 if (storvsc_max_hw_queues > num_present_cpus) {
2058 storvsc_max_hw_queues = 0;
2059 storvsc_log(device, STORVSC_LOGGING_WARN,
2060 "Resetting invalid storvsc_max_hw_queues value to default.\n");
2061 }
2062 if (storvsc_max_hw_queues)
2063 host->nr_hw_queues = storvsc_max_hw_queues;
2064 else
2065 host->nr_hw_queues = num_present_cpus;
2066 }
2067
2068 /*
2069 * Set the error handler work queue.
2070 */
2071 host_dev->handle_error_wq =
2072 alloc_ordered_workqueue("storvsc_error_wq_%d",
2073 0,
2074 host->host_no);
2075 if (!host_dev->handle_error_wq) {
2076 ret = -ENOMEM;
2077 goto err_out2;
2078 }
2079 INIT_WORK(&host_dev->host_scan_work, storvsc_host_scan);
2080 /* Register the HBA and start the scsi bus scan */
2081 ret = scsi_add_host(host, &device->device);
2082 if (ret != 0)
2083 goto err_out3;
2084
2085 if (!dev_is_ide) {
2086 scsi_scan_host(host);
2087 } else {
2088 target = (device->dev_instance.b[5] << 8 |
2089 device->dev_instance.b[4]);
2090 ret = scsi_add_device(host, 0, target, 0);
2091 if (ret)
2092 goto err_out4;
2093 }
2094 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2095 if (host->transportt == fc_transport_template) {
2096 struct fc_rport_identifiers ids = {
2097 .roles = FC_PORT_ROLE_FCP_DUMMY_INITIATOR,
2098 };
2099
2100 fc_host_node_name(host) = stor_device->node_name;
2101 fc_host_port_name(host) = stor_device->port_name;
2102 stor_device->rport = fc_remote_port_add(host, 0, &ids);
2103 if (!stor_device->rport) {
2104 ret = -ENOMEM;
2105 goto err_out4;
2106 }
2107 }
2108 #endif
2109 return 0;
2110
2111 err_out4:
2112 scsi_remove_host(host);
2113
2114 err_out3:
2115 destroy_workqueue(host_dev->handle_error_wq);
2116
2117 err_out2:
2118 /*
2119 * Once we have connected with the host, we would need to
2120 * invoke storvsc_dev_remove() to rollback this state and
2121 * this call also frees up the stor_device; hence the jump around
2122 * err_out1 label.
2123 */
2124 storvsc_dev_remove(device);
2125 goto err_out0;
2126
2127 err_out1:
2128 kfree(stor_device->stor_chns);
2129 kfree(stor_device);
2130
2131 err_out0:
2132 scsi_host_put(host);
2133 return ret;
2134 }
2135
2136 /* Change a scsi target's queue depth */
storvsc_change_queue_depth(struct scsi_device * sdev,int queue_depth)2137 static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth)
2138 {
2139 if (queue_depth > scsi_driver.can_queue)
2140 queue_depth = scsi_driver.can_queue;
2141
2142 return scsi_change_queue_depth(sdev, queue_depth);
2143 }
2144
storvsc_remove(struct hv_device * dev)2145 static void storvsc_remove(struct hv_device *dev)
2146 {
2147 struct storvsc_device *stor_device = hv_get_drvdata(dev);
2148 struct Scsi_Host *host = stor_device->host;
2149 struct hv_host_device *host_dev = shost_priv(host);
2150
2151 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2152 if (host->transportt == fc_transport_template) {
2153 fc_remote_port_delete(stor_device->rport);
2154 fc_remove_host(host);
2155 }
2156 #endif
2157 destroy_workqueue(host_dev->handle_error_wq);
2158 scsi_remove_host(host);
2159 storvsc_dev_remove(dev);
2160 scsi_host_put(host);
2161 }
2162
storvsc_suspend(struct hv_device * hv_dev)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
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
storvsc_resume(struct hv_device * hv_dev)2183 static int storvsc_resume(struct hv_device *hv_dev)
2184 {
2185 int ret;
2186
2187 ret = storvsc_connect_to_vsp(hv_dev, aligned_ringbuffer_size,
2188 hv_dev_is_fc(hv_dev));
2189 return ret;
2190 }
2191
2192 static struct hv_driver storvsc_drv = {
2193 .name = KBUILD_MODNAME,
2194 .id_table = id_table,
2195 .probe = storvsc_probe,
2196 .remove = storvsc_remove,
2197 .suspend = storvsc_suspend,
2198 .resume = storvsc_resume,
2199 .driver = {
2200 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
2201 },
2202 };
2203
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
storvsc_drv_init(void)2211 static int __init storvsc_drv_init(void)
2212 {
2213 int ret;
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)
2220 */
2221 aligned_ringbuffer_size = VMBUS_RING_SIZE(storvsc_ringbuffer_size);
2222 max_outstanding_req_per_channel =
2223 ((aligned_ringbuffer_size - PAGE_SIZE) /
2224 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
2225 sizeof(struct vstor_packet) + sizeof(u64),
2226 sizeof(u64)));
2227
2228 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2229 fc_transport_template = fc_attach_transport(&fc_transport_functions);
2230 if (!fc_transport_template)
2231 return -ENODEV;
2232
2233 fc_transport_template->user_scan = storvsc_user_scan;
2234 #endif
2235
2236 ret = vmbus_driver_register(&storvsc_drv);
2237
2238 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2239 if (ret)
2240 fc_release_transport(fc_transport_template);
2241 #endif
2242
2243 return ret;
2244 }
2245
storvsc_drv_exit(void)2246 static void __exit storvsc_drv_exit(void)
2247 {
2248 vmbus_driver_unregister(&storvsc_drv);
2249 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2250 fc_release_transport(fc_transport_template);
2251 #endif
2252 }
2253
2254 MODULE_LICENSE("GPL");
2255 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
2256 module_init(storvsc_drv_init);
2257 module_exit(storvsc_drv_exit);
2258