]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/scsi/storvsc_drv.c
scsi: storvsc: use tagged SRB requests if supported by the device
[mirror_ubuntu-artful-kernel.git] / drivers / scsi / storvsc_drv.c
1 /*
2 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
20 * K. Y. Srinivasan <kys@microsoft.com>
21 */
22
23 #include <linux/kernel.h>
24 #include <linux/wait.h>
25 #include <linux/sched.h>
26 #include <linux/completion.h>
27 #include <linux/string.h>
28 #include <linux/mm.h>
29 #include <linux/delay.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/module.h>
33 #include <linux/device.h>
34 #include <linux/hyperv.h>
35 #include <linux/blkdev.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_cmnd.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_device.h>
40 #include <scsi/scsi_tcq.h>
41 #include <scsi/scsi_eh.h>
42 #include <scsi/scsi_devinfo.h>
43 #include <scsi/scsi_dbg.h>
44 #include <scsi/scsi_transport_fc.h>
45 #include <scsi/scsi_transport.h>
46
47 /*
48 * All wire protocol details (storage protocol between the guest and the host)
49 * are consolidated here.
50 *
51 * Begin protocol definitions.
52 */
53
54 /*
55 * Version history:
56 * V1 Beta: 0.1
57 * V1 RC < 2008/1/31: 1.0
58 * V1 RC > 2008/1/31: 2.0
59 * Win7: 4.2
60 * Win8: 5.1
61 * Win8.1: 6.0
62 * Win10: 6.2
63 */
64
65 #define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
66 (((MINOR_) & 0xff)))
67
68 #define VMSTOR_PROTO_VERSION_WIN6 VMSTOR_PROTO_VERSION(2, 0)
69 #define VMSTOR_PROTO_VERSION_WIN7 VMSTOR_PROTO_VERSION(4, 2)
70 #define VMSTOR_PROTO_VERSION_WIN8 VMSTOR_PROTO_VERSION(5, 1)
71 #define VMSTOR_PROTO_VERSION_WIN8_1 VMSTOR_PROTO_VERSION(6, 0)
72 #define VMSTOR_PROTO_VERSION_WIN10 VMSTOR_PROTO_VERSION(6, 2)
73
74 /* Packet structure describing virtual storage requests. */
75 enum vstor_packet_operation {
76 VSTOR_OPERATION_COMPLETE_IO = 1,
77 VSTOR_OPERATION_REMOVE_DEVICE = 2,
78 VSTOR_OPERATION_EXECUTE_SRB = 3,
79 VSTOR_OPERATION_RESET_LUN = 4,
80 VSTOR_OPERATION_RESET_ADAPTER = 5,
81 VSTOR_OPERATION_RESET_BUS = 6,
82 VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
83 VSTOR_OPERATION_END_INITIALIZATION = 8,
84 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
85 VSTOR_OPERATION_QUERY_PROPERTIES = 10,
86 VSTOR_OPERATION_ENUMERATE_BUS = 11,
87 VSTOR_OPERATION_FCHBA_DATA = 12,
88 VSTOR_OPERATION_CREATE_SUB_CHANNELS = 13,
89 VSTOR_OPERATION_MAXIMUM = 13
90 };
91
92 /*
93 * WWN packet for Fibre Channel HBA
94 */
95
96 struct hv_fc_wwn_packet {
97 u8 primary_active;
98 u8 reserved1[3];
99 u8 primary_port_wwn[8];
100 u8 primary_node_wwn[8];
101 u8 secondary_port_wwn[8];
102 u8 secondary_node_wwn[8];
103 };
104
105
106
107 /*
108 * SRB Flag Bits
109 */
110
111 #define SRB_FLAGS_QUEUE_ACTION_ENABLE 0x00000002
112 #define SRB_FLAGS_DISABLE_DISCONNECT 0x00000004
113 #define SRB_FLAGS_DISABLE_SYNCH_TRANSFER 0x00000008
114 #define SRB_FLAGS_BYPASS_FROZEN_QUEUE 0x00000010
115 #define SRB_FLAGS_DISABLE_AUTOSENSE 0x00000020
116 #define SRB_FLAGS_DATA_IN 0x00000040
117 #define SRB_FLAGS_DATA_OUT 0x00000080
118 #define SRB_FLAGS_NO_DATA_TRANSFER 0x00000000
119 #define SRB_FLAGS_UNSPECIFIED_DIRECTION (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT)
120 #define SRB_FLAGS_NO_QUEUE_FREEZE 0x00000100
121 #define SRB_FLAGS_ADAPTER_CACHE_ENABLE 0x00000200
122 #define SRB_FLAGS_FREE_SENSE_BUFFER 0x00000400
123
124 /*
125 * This flag indicates the request is part of the workflow for processing a D3.
126 */
127 #define SRB_FLAGS_D3_PROCESSING 0x00000800
128 #define SRB_FLAGS_IS_ACTIVE 0x00010000
129 #define SRB_FLAGS_ALLOCATED_FROM_ZONE 0x00020000
130 #define SRB_FLAGS_SGLIST_FROM_POOL 0x00040000
131 #define SRB_FLAGS_BYPASS_LOCKED_QUEUE 0x00080000
132 #define SRB_FLAGS_NO_KEEP_AWAKE 0x00100000
133 #define SRB_FLAGS_PORT_DRIVER_ALLOCSENSE 0x00200000
134 #define SRB_FLAGS_PORT_DRIVER_SENSEHASPORT 0x00400000
135 #define SRB_FLAGS_DONT_START_NEXT_PACKET 0x00800000
136 #define SRB_FLAGS_PORT_DRIVER_RESERVED 0x0F000000
137 #define SRB_FLAGS_CLASS_DRIVER_RESERVED 0xF0000000
138
139 #define SP_UNTAGGED ((unsigned char) ~0)
140 #define SRB_SIMPLE_TAG_REQUEST 0x20
141
142 /*
143 * Platform neutral description of a scsi request -
144 * this remains the same across the write regardless of 32/64 bit
145 * note: it's patterned off the SCSI_PASS_THROUGH structure
146 */
147 #define STORVSC_MAX_CMD_LEN 0x10
148
149 #define POST_WIN7_STORVSC_SENSE_BUFFER_SIZE 0x14
150 #define PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE 0x12
151
152 #define STORVSC_SENSE_BUFFER_SIZE 0x14
153 #define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
154
155 /*
156 * Sense buffer size changed in win8; have a run-time
157 * variable to track the size we should use. This value will
158 * likely change during protocol negotiation but it is valid
159 * to start by assuming pre-Win8.
160 */
161 static int sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
162
163 /*
164 * The storage protocol version is determined during the
165 * initial exchange with the host. It will indicate which
166 * storage functionality is available in the host.
167 */
168 static int vmstor_proto_version;
169
170 #define STORVSC_LOGGING_NONE 0
171 #define STORVSC_LOGGING_ERROR 1
172 #define STORVSC_LOGGING_WARN 2
173
174 static int logging_level = STORVSC_LOGGING_ERROR;
175 module_param(logging_level, int, S_IRUGO|S_IWUSR);
176 MODULE_PARM_DESC(logging_level,
177 "Logging level, 0 - None, 1 - Error (default), 2 - Warning.");
178
179 static inline bool do_logging(int level)
180 {
181 return logging_level >= level;
182 }
183
184 #define storvsc_log(dev, level, fmt, ...) \
185 do { \
186 if (do_logging(level)) \
187 dev_warn(&(dev)->device, fmt, ##__VA_ARGS__); \
188 } while (0)
189
190 struct vmscsi_win8_extension {
191 /*
192 * The following were added in Windows 8
193 */
194 u16 reserve;
195 u8 queue_tag;
196 u8 queue_action;
197 u32 srb_flags;
198 u32 time_out_value;
199 u32 queue_sort_ey;
200 } __packed;
201
202 struct vmscsi_request {
203 u16 length;
204 u8 srb_status;
205 u8 scsi_status;
206
207 u8 port_number;
208 u8 path_id;
209 u8 target_id;
210 u8 lun;
211
212 u8 cdb_length;
213 u8 sense_info_length;
214 u8 data_in;
215 u8 reserved;
216
217 u32 data_transfer_length;
218
219 union {
220 u8 cdb[STORVSC_MAX_CMD_LEN];
221 u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
222 u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
223 };
224 /*
225 * The following was added in win8.
226 */
227 struct vmscsi_win8_extension win8_extension;
228
229 } __attribute((packed));
230
231
232 /*
233 * The size of the vmscsi_request has changed in win8. The
234 * additional size is because of new elements added to the
235 * structure. These elements are valid only when we are talking
236 * to a win8 host.
237 * Track the correction to size we need to apply. This value
238 * will likely change during protocol negotiation but it is
239 * valid to start by assuming pre-Win8.
240 */
241 static int vmscsi_size_delta = sizeof(struct vmscsi_win8_extension);
242
243 /*
244 * The list of storage protocols in order of preference.
245 */
246 struct vmstor_protocol {
247 int protocol_version;
248 int sense_buffer_size;
249 int vmscsi_size_delta;
250 };
251
252
253 static const struct vmstor_protocol vmstor_protocols[] = {
254 {
255 VMSTOR_PROTO_VERSION_WIN10,
256 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
257 0
258 },
259 {
260 VMSTOR_PROTO_VERSION_WIN8_1,
261 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
262 0
263 },
264 {
265 VMSTOR_PROTO_VERSION_WIN8,
266 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
267 0
268 },
269 {
270 VMSTOR_PROTO_VERSION_WIN7,
271 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
272 sizeof(struct vmscsi_win8_extension),
273 },
274 {
275 VMSTOR_PROTO_VERSION_WIN6,
276 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
277 sizeof(struct vmscsi_win8_extension),
278 }
279 };
280
281
282 /*
283 * This structure is sent during the intialization phase to get the different
284 * properties of the channel.
285 */
286
287 #define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1
288
289 struct vmstorage_channel_properties {
290 u32 reserved;
291 u16 max_channel_cnt;
292 u16 reserved1;
293
294 u32 flags;
295 u32 max_transfer_bytes;
296
297 u64 reserved2;
298 } __packed;
299
300 /* This structure is sent during the storage protocol negotiations. */
301 struct vmstorage_protocol_version {
302 /* Major (MSW) and minor (LSW) version numbers. */
303 u16 major_minor;
304
305 /*
306 * Revision number is auto-incremented whenever this file is changed
307 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
308 * definitely indicate incompatibility--but it does indicate mismatched
309 * builds.
310 * This is only used on the windows side. Just set it to 0.
311 */
312 u16 revision;
313 } __packed;
314
315 /* Channel Property Flags */
316 #define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
317 #define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
318
319 struct vstor_packet {
320 /* Requested operation type */
321 enum vstor_packet_operation operation;
322
323 /* Flags - see below for values */
324 u32 flags;
325
326 /* Status of the request returned from the server side. */
327 u32 status;
328
329 /* Data payload area */
330 union {
331 /*
332 * Structure used to forward SCSI commands from the
333 * client to the server.
334 */
335 struct vmscsi_request vm_srb;
336
337 /* Structure used to query channel properties. */
338 struct vmstorage_channel_properties storage_channel_properties;
339
340 /* Used during version negotiations. */
341 struct vmstorage_protocol_version version;
342
343 /* Fibre channel address packet */
344 struct hv_fc_wwn_packet wwn_packet;
345
346 /* Number of sub-channels to create */
347 u16 sub_channel_count;
348
349 /* This will be the maximum of the union members */
350 u8 buffer[0x34];
351 };
352 } __packed;
353
354 /*
355 * Packet Flags:
356 *
357 * This flag indicates that the server should send back a completion for this
358 * packet.
359 */
360
361 #define REQUEST_COMPLETION_FLAG 0x1
362
363 /* Matches Windows-end */
364 enum storvsc_request_type {
365 WRITE_TYPE = 0,
366 READ_TYPE,
367 UNKNOWN_TYPE,
368 };
369
370 /*
371 * SRB status codes and masks; a subset of the codes used here.
372 */
373
374 #define SRB_STATUS_AUTOSENSE_VALID 0x80
375 #define SRB_STATUS_QUEUE_FROZEN 0x40
376 #define SRB_STATUS_INVALID_LUN 0x20
377 #define SRB_STATUS_SUCCESS 0x01
378 #define SRB_STATUS_ABORTED 0x02
379 #define SRB_STATUS_ERROR 0x04
380
381 #define SRB_STATUS(status) \
382 (status & ~(SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_QUEUE_FROZEN))
383 /*
384 * This is the end of Protocol specific defines.
385 */
386
387 static int storvsc_ringbuffer_size = (256 * PAGE_SIZE);
388 static u32 max_outstanding_req_per_channel;
389
390 static int storvsc_vcpus_per_sub_channel = 4;
391
392 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
393 MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
394
395 module_param(storvsc_vcpus_per_sub_channel, int, S_IRUGO);
396 MODULE_PARM_DESC(storvsc_vcpus_per_sub_channel, "Ratio of VCPUs to subchannels");
397 /*
398 * Timeout in seconds for all devices managed by this driver.
399 */
400 static int storvsc_timeout = 180;
401
402 static int msft_blist_flags = BLIST_TRY_VPD_PAGES;
403
404 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
405 static struct scsi_transport_template *fc_transport_template;
406 #endif
407
408 static void storvsc_on_channel_callback(void *context);
409
410 #define STORVSC_MAX_LUNS_PER_TARGET 255
411 #define STORVSC_MAX_TARGETS 2
412 #define STORVSC_MAX_CHANNELS 8
413
414 #define STORVSC_FC_MAX_LUNS_PER_TARGET 255
415 #define STORVSC_FC_MAX_TARGETS 128
416 #define STORVSC_FC_MAX_CHANNELS 8
417
418 #define STORVSC_IDE_MAX_LUNS_PER_TARGET 64
419 #define STORVSC_IDE_MAX_TARGETS 1
420 #define STORVSC_IDE_MAX_CHANNELS 1
421
422 struct storvsc_cmd_request {
423 struct scsi_cmnd *cmd;
424
425 struct hv_device *device;
426
427 /* Synchronize the request/response if needed */
428 struct completion wait_event;
429
430 struct vmbus_channel_packet_multipage_buffer mpb;
431 struct vmbus_packet_mpb_array *payload;
432 u32 payload_sz;
433
434 struct vstor_packet vstor_packet;
435 };
436
437
438 /* A storvsc device is a device object that contains a vmbus channel */
439 struct storvsc_device {
440 struct hv_device *device;
441
442 bool destroy;
443 bool drain_notify;
444 bool open_sub_channel;
445 atomic_t num_outstanding_req;
446 struct Scsi_Host *host;
447
448 wait_queue_head_t waiting_to_drain;
449
450 /*
451 * Each unique Port/Path/Target represents 1 channel ie scsi
452 * controller. In reality, the pathid, targetid is always 0
453 * and the port is set by us
454 */
455 unsigned int port_number;
456 unsigned char path_id;
457 unsigned char target_id;
458
459 /*
460 * Max I/O, the device can support.
461 */
462 u32 max_transfer_bytes;
463 /*
464 * Number of sub-channels we will open.
465 */
466 u16 num_sc;
467 struct vmbus_channel **stor_chns;
468 /*
469 * Mask of CPUs bound to subchannels.
470 */
471 struct cpumask alloced_cpus;
472 /* Used for vsc/vsp channel reset process */
473 struct storvsc_cmd_request init_request;
474 struct storvsc_cmd_request reset_request;
475 /*
476 * Currently active port and node names for FC devices.
477 */
478 u64 node_name;
479 u64 port_name;
480 };
481
482 struct hv_host_device {
483 struct hv_device *dev;
484 unsigned int port;
485 unsigned char path;
486 unsigned char target;
487 };
488
489 struct storvsc_scan_work {
490 struct work_struct work;
491 struct Scsi_Host *host;
492 u8 lun;
493 u8 tgt_id;
494 };
495
496 static void storvsc_device_scan(struct work_struct *work)
497 {
498 struct storvsc_scan_work *wrk;
499 struct scsi_device *sdev;
500
501 wrk = container_of(work, struct storvsc_scan_work, work);
502
503 sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
504 if (!sdev)
505 goto done;
506 scsi_rescan_device(&sdev->sdev_gendev);
507 scsi_device_put(sdev);
508
509 done:
510 kfree(wrk);
511 }
512
513 static void storvsc_host_scan(struct work_struct *work)
514 {
515 struct storvsc_scan_work *wrk;
516 struct Scsi_Host *host;
517 struct scsi_device *sdev;
518
519 wrk = container_of(work, struct storvsc_scan_work, work);
520 host = wrk->host;
521
522 /*
523 * Before scanning the host, first check to see if any of the
524 * currrently known devices have been hot removed. We issue a
525 * "unit ready" command against all currently known devices.
526 * This I/O will result in an error for devices that have been
527 * removed. As part of handling the I/O error, we remove the device.
528 *
529 * When a LUN is added or removed, the host sends us a signal to
530 * scan the host. Thus we are forced to discover the LUNs that
531 * may have been removed this way.
532 */
533 mutex_lock(&host->scan_mutex);
534 shost_for_each_device(sdev, host)
535 scsi_test_unit_ready(sdev, 1, 1, NULL);
536 mutex_unlock(&host->scan_mutex);
537 /*
538 * Now scan the host to discover LUNs that may have been added.
539 */
540 scsi_scan_host(host);
541
542 kfree(wrk);
543 }
544
545 static void storvsc_remove_lun(struct work_struct *work)
546 {
547 struct storvsc_scan_work *wrk;
548 struct scsi_device *sdev;
549
550 wrk = container_of(work, struct storvsc_scan_work, work);
551 if (!scsi_host_get(wrk->host))
552 goto done;
553
554 sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
555
556 if (sdev) {
557 scsi_remove_device(sdev);
558 scsi_device_put(sdev);
559 }
560 scsi_host_put(wrk->host);
561
562 done:
563 kfree(wrk);
564 }
565
566
567 /*
568 * We can get incoming messages from the host that are not in response to
569 * messages that we have sent out. An example of this would be messages
570 * received by the guest to notify dynamic addition/removal of LUNs. To
571 * deal with potential race conditions where the driver may be in the
572 * midst of being unloaded when we might receive an unsolicited message
573 * from the host, we have implemented a mechanism to gurantee sequential
574 * consistency:
575 *
576 * 1) Once the device is marked as being destroyed, we will fail all
577 * outgoing messages.
578 * 2) We permit incoming messages when the device is being destroyed,
579 * only to properly account for messages already sent out.
580 */
581
582 static inline struct storvsc_device *get_out_stor_device(
583 struct hv_device *device)
584 {
585 struct storvsc_device *stor_device;
586
587 stor_device = hv_get_drvdata(device);
588
589 if (stor_device && stor_device->destroy)
590 stor_device = NULL;
591
592 return stor_device;
593 }
594
595
596 static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
597 {
598 dev->drain_notify = true;
599 wait_event(dev->waiting_to_drain,
600 atomic_read(&dev->num_outstanding_req) == 0);
601 dev->drain_notify = false;
602 }
603
604 static inline struct storvsc_device *get_in_stor_device(
605 struct hv_device *device)
606 {
607 struct storvsc_device *stor_device;
608
609 stor_device = hv_get_drvdata(device);
610
611 if (!stor_device)
612 goto get_in_err;
613
614 /*
615 * If the device is being destroyed; allow incoming
616 * traffic only to cleanup outstanding requests.
617 */
618
619 if (stor_device->destroy &&
620 (atomic_read(&stor_device->num_outstanding_req) == 0))
621 stor_device = NULL;
622
623 get_in_err:
624 return stor_device;
625
626 }
627
628 static void handle_sc_creation(struct vmbus_channel *new_sc)
629 {
630 struct hv_device *device = new_sc->primary_channel->device_obj;
631 struct storvsc_device *stor_device;
632 struct vmstorage_channel_properties props;
633
634 stor_device = get_out_stor_device(device);
635 if (!stor_device)
636 return;
637
638 if (stor_device->open_sub_channel == false)
639 return;
640
641 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
642
643 vmbus_open(new_sc,
644 storvsc_ringbuffer_size,
645 storvsc_ringbuffer_size,
646 (void *)&props,
647 sizeof(struct vmstorage_channel_properties),
648 storvsc_on_channel_callback, new_sc);
649
650 if (new_sc->state == CHANNEL_OPENED_STATE) {
651 stor_device->stor_chns[new_sc->target_cpu] = new_sc;
652 cpumask_set_cpu(new_sc->target_cpu, &stor_device->alloced_cpus);
653 }
654 }
655
656 static void handle_multichannel_storage(struct hv_device *device, int max_chns)
657 {
658 struct storvsc_device *stor_device;
659 int num_cpus = num_online_cpus();
660 int num_sc;
661 struct storvsc_cmd_request *request;
662 struct vstor_packet *vstor_packet;
663 int ret, t;
664
665 num_sc = ((max_chns > num_cpus) ? num_cpus : max_chns);
666 stor_device = get_out_stor_device(device);
667 if (!stor_device)
668 return;
669
670 stor_device->num_sc = num_sc;
671 request = &stor_device->init_request;
672 vstor_packet = &request->vstor_packet;
673
674 stor_device->open_sub_channel = true;
675 /*
676 * Establish a handler for dealing with subchannels.
677 */
678 vmbus_set_sc_create_callback(device->channel, handle_sc_creation);
679
680 /*
681 * Check to see if sub-channels have already been created. This
682 * can happen when this driver is re-loaded after unloading.
683 */
684
685 if (vmbus_are_subchannels_present(device->channel))
686 return;
687
688 stor_device->open_sub_channel = false;
689 /*
690 * Request the host to create sub-channels.
691 */
692 memset(request, 0, sizeof(struct storvsc_cmd_request));
693 init_completion(&request->wait_event);
694 vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
695 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
696 vstor_packet->sub_channel_count = num_sc;
697
698 ret = vmbus_sendpacket(device->channel, vstor_packet,
699 (sizeof(struct vstor_packet) -
700 vmscsi_size_delta),
701 (unsigned long)request,
702 VM_PKT_DATA_INBAND,
703 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
704
705 if (ret != 0)
706 return;
707
708 t = wait_for_completion_timeout(&request->wait_event, 10*HZ);
709 if (t == 0)
710 return;
711
712 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
713 vstor_packet->status != 0)
714 return;
715
716 /*
717 * Now that we created the sub-channels, invoke the check; this
718 * may trigger the callback.
719 */
720 stor_device->open_sub_channel = true;
721 vmbus_are_subchannels_present(device->channel);
722 }
723
724 static void cache_wwn(struct storvsc_device *stor_device,
725 struct vstor_packet *vstor_packet)
726 {
727 /*
728 * Cache the currently active port and node ww names.
729 */
730 if (vstor_packet->wwn_packet.primary_active) {
731 stor_device->node_name =
732 wwn_to_u64(vstor_packet->wwn_packet.primary_node_wwn);
733 stor_device->port_name =
734 wwn_to_u64(vstor_packet->wwn_packet.primary_port_wwn);
735 } else {
736 stor_device->node_name =
737 wwn_to_u64(vstor_packet->wwn_packet.secondary_node_wwn);
738 stor_device->port_name =
739 wwn_to_u64(vstor_packet->wwn_packet.secondary_port_wwn);
740 }
741 }
742
743
744 static int storvsc_execute_vstor_op(struct hv_device *device,
745 struct storvsc_cmd_request *request,
746 bool status_check)
747 {
748 struct vstor_packet *vstor_packet;
749 int ret, t;
750
751 vstor_packet = &request->vstor_packet;
752
753 init_completion(&request->wait_event);
754 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
755
756 ret = vmbus_sendpacket(device->channel, vstor_packet,
757 (sizeof(struct vstor_packet) -
758 vmscsi_size_delta),
759 (unsigned long)request,
760 VM_PKT_DATA_INBAND,
761 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
762 if (ret != 0)
763 return ret;
764
765 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
766 if (t == 0)
767 return -ETIMEDOUT;
768
769 if (!status_check)
770 return ret;
771
772 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
773 vstor_packet->status != 0)
774 return -EINVAL;
775
776 return ret;
777 }
778
779 static int storvsc_channel_init(struct hv_device *device, bool is_fc)
780 {
781 struct storvsc_device *stor_device;
782 struct storvsc_cmd_request *request;
783 struct vstor_packet *vstor_packet;
784 int ret, i;
785 int max_chns;
786 bool process_sub_channels = false;
787
788 stor_device = get_out_stor_device(device);
789 if (!stor_device)
790 return -ENODEV;
791
792 request = &stor_device->init_request;
793 vstor_packet = &request->vstor_packet;
794
795 /*
796 * Now, initiate the vsc/vsp initialization protocol on the open
797 * channel
798 */
799 memset(request, 0, sizeof(struct storvsc_cmd_request));
800 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
801 ret = storvsc_execute_vstor_op(device, request, true);
802 if (ret)
803 return ret;
804 /*
805 * Query host supported protocol version.
806 */
807
808 for (i = 0; i < ARRAY_SIZE(vmstor_protocols); i++) {
809 /* reuse the packet for version range supported */
810 memset(vstor_packet, 0, sizeof(struct vstor_packet));
811 vstor_packet->operation =
812 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
813
814 vstor_packet->version.major_minor =
815 vmstor_protocols[i].protocol_version;
816
817 /*
818 * The revision number is only used in Windows; set it to 0.
819 */
820 vstor_packet->version.revision = 0;
821 ret = storvsc_execute_vstor_op(device, request, false);
822 if (ret != 0)
823 return ret;
824
825 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO)
826 return -EINVAL;
827
828 if (vstor_packet->status == 0) {
829 vmstor_proto_version =
830 vmstor_protocols[i].protocol_version;
831
832 sense_buffer_size =
833 vmstor_protocols[i].sense_buffer_size;
834
835 vmscsi_size_delta =
836 vmstor_protocols[i].vmscsi_size_delta;
837
838 break;
839 }
840 }
841
842 if (vstor_packet->status != 0)
843 return -EINVAL;
844
845
846 memset(vstor_packet, 0, sizeof(struct vstor_packet));
847 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
848 ret = storvsc_execute_vstor_op(device, request, true);
849 if (ret != 0)
850 return ret;
851
852 /*
853 * Check to see if multi-channel support is there.
854 * Hosts that implement protocol version of 5.1 and above
855 * support multi-channel.
856 */
857 max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
858
859 /*
860 * Allocate state to manage the sub-channels.
861 * We allocate an array based on the numbers of possible CPUs
862 * (Hyper-V does not support cpu online/offline).
863 * This Array will be sparseley populated with unique
864 * channels - primary + sub-channels.
865 * We will however populate all the slots to evenly distribute
866 * the load.
867 */
868 stor_device->stor_chns = kzalloc(sizeof(void *) * num_possible_cpus(),
869 GFP_KERNEL);
870 if (stor_device->stor_chns == NULL)
871 return -ENOMEM;
872
873 stor_device->stor_chns[device->channel->target_cpu] = device->channel;
874 cpumask_set_cpu(device->channel->target_cpu,
875 &stor_device->alloced_cpus);
876
877 if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN8) {
878 if (vstor_packet->storage_channel_properties.flags &
879 STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
880 process_sub_channels = true;
881 }
882 stor_device->max_transfer_bytes =
883 vstor_packet->storage_channel_properties.max_transfer_bytes;
884
885 if (!is_fc)
886 goto done;
887
888 /*
889 * For FC devices retrieve FC HBA data.
890 */
891 memset(vstor_packet, 0, sizeof(struct vstor_packet));
892 vstor_packet->operation = VSTOR_OPERATION_FCHBA_DATA;
893 ret = storvsc_execute_vstor_op(device, request, true);
894 if (ret != 0)
895 return ret;
896
897 /*
898 * Cache the currently active port and node ww names.
899 */
900 cache_wwn(stor_device, vstor_packet);
901
902 done:
903
904 memset(vstor_packet, 0, sizeof(struct vstor_packet));
905 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
906 ret = storvsc_execute_vstor_op(device, request, true);
907 if (ret != 0)
908 return ret;
909
910 if (process_sub_channels)
911 handle_multichannel_storage(device, max_chns);
912
913 return ret;
914 }
915
916 static void storvsc_handle_error(struct vmscsi_request *vm_srb,
917 struct scsi_cmnd *scmnd,
918 struct Scsi_Host *host,
919 u8 asc, u8 ascq)
920 {
921 struct storvsc_scan_work *wrk;
922 void (*process_err_fn)(struct work_struct *work);
923 bool do_work = false;
924
925 switch (SRB_STATUS(vm_srb->srb_status)) {
926 case SRB_STATUS_ERROR:
927 /*
928 * If there is an error; offline the device since all
929 * error recovery strategies would have already been
930 * deployed on the host side. However, if the command
931 * were a pass-through command deal with it appropriately.
932 */
933 switch (scmnd->cmnd[0]) {
934 case ATA_16:
935 case ATA_12:
936 set_host_byte(scmnd, DID_PASSTHROUGH);
937 break;
938 /*
939 * On Some Windows hosts TEST_UNIT_READY command can return
940 * SRB_STATUS_ERROR, let the upper level code deal with it
941 * based on the sense information.
942 */
943 case TEST_UNIT_READY:
944 break;
945 default:
946 set_host_byte(scmnd, DID_TARGET_FAILURE);
947 }
948 break;
949 case SRB_STATUS_INVALID_LUN:
950 do_work = true;
951 process_err_fn = storvsc_remove_lun;
952 break;
953 case SRB_STATUS_ABORTED:
954 if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID &&
955 (asc == 0x2a) && (ascq == 0x9)) {
956 do_work = true;
957 process_err_fn = storvsc_device_scan;
958 /*
959 * Retry the I/O that trigerred this.
960 */
961 set_host_byte(scmnd, DID_REQUEUE);
962 }
963 break;
964 }
965
966 if (!do_work)
967 return;
968
969 /*
970 * We need to schedule work to process this error; schedule it.
971 */
972 wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
973 if (!wrk) {
974 set_host_byte(scmnd, DID_TARGET_FAILURE);
975 return;
976 }
977
978 wrk->host = host;
979 wrk->lun = vm_srb->lun;
980 wrk->tgt_id = vm_srb->target_id;
981 INIT_WORK(&wrk->work, process_err_fn);
982 schedule_work(&wrk->work);
983 }
984
985
986 static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request,
987 struct storvsc_device *stor_dev)
988 {
989 struct scsi_cmnd *scmnd = cmd_request->cmd;
990 struct scsi_sense_hdr sense_hdr;
991 struct vmscsi_request *vm_srb;
992 struct Scsi_Host *host;
993 u32 payload_sz = cmd_request->payload_sz;
994 void *payload = cmd_request->payload;
995
996 host = stor_dev->host;
997
998 vm_srb = &cmd_request->vstor_packet.vm_srb;
999
1000 scmnd->result = vm_srb->scsi_status;
1001
1002 if (scmnd->result) {
1003 if (scsi_normalize_sense(scmnd->sense_buffer,
1004 SCSI_SENSE_BUFFERSIZE, &sense_hdr) &&
1005 !(sense_hdr.sense_key == NOT_READY &&
1006 sense_hdr.asc == 0x03A) &&
1007 do_logging(STORVSC_LOGGING_ERROR))
1008 scsi_print_sense_hdr(scmnd->device, "storvsc",
1009 &sense_hdr);
1010 }
1011
1012 if (vm_srb->srb_status != SRB_STATUS_SUCCESS)
1013 storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
1014 sense_hdr.ascq);
1015
1016 scsi_set_resid(scmnd,
1017 cmd_request->payload->range.len -
1018 vm_srb->data_transfer_length);
1019
1020 scmnd->scsi_done(scmnd);
1021
1022 if (payload_sz >
1023 sizeof(struct vmbus_channel_packet_multipage_buffer))
1024 kfree(payload);
1025 }
1026
1027 static void storvsc_on_io_completion(struct storvsc_device *stor_device,
1028 struct vstor_packet *vstor_packet,
1029 struct storvsc_cmd_request *request)
1030 {
1031 struct vstor_packet *stor_pkt;
1032 struct hv_device *device = stor_device->device;
1033
1034 stor_pkt = &request->vstor_packet;
1035
1036 /*
1037 * The current SCSI handling on the host side does
1038 * not correctly handle:
1039 * INQUIRY command with page code parameter set to 0x80
1040 * MODE_SENSE command with cmd[2] == 0x1c
1041 *
1042 * Setup srb and scsi status so this won't be fatal.
1043 * We do this so we can distinguish truly fatal failues
1044 * (srb status == 0x4) and off-line the device in that case.
1045 */
1046
1047 if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
1048 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
1049 vstor_packet->vm_srb.scsi_status = 0;
1050 vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
1051 }
1052
1053
1054 /* Copy over the status...etc */
1055 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
1056 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
1057 stor_pkt->vm_srb.sense_info_length =
1058 vstor_packet->vm_srb.sense_info_length;
1059
1060 if (vstor_packet->vm_srb.scsi_status != 0 ||
1061 vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS)
1062 storvsc_log(device, STORVSC_LOGGING_WARN,
1063 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
1064 stor_pkt->vm_srb.cdb[0],
1065 vstor_packet->vm_srb.scsi_status,
1066 vstor_packet->vm_srb.srb_status);
1067
1068 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
1069 /* CHECK_CONDITION */
1070 if (vstor_packet->vm_srb.srb_status &
1071 SRB_STATUS_AUTOSENSE_VALID) {
1072 /* autosense data available */
1073
1074 storvsc_log(device, STORVSC_LOGGING_WARN,
1075 "stor pkt %p autosense data valid - len %d\n",
1076 request, vstor_packet->vm_srb.sense_info_length);
1077
1078 memcpy(request->cmd->sense_buffer,
1079 vstor_packet->vm_srb.sense_data,
1080 vstor_packet->vm_srb.sense_info_length);
1081
1082 }
1083 }
1084
1085 stor_pkt->vm_srb.data_transfer_length =
1086 vstor_packet->vm_srb.data_transfer_length;
1087
1088 storvsc_command_completion(request, stor_device);
1089
1090 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
1091 stor_device->drain_notify)
1092 wake_up(&stor_device->waiting_to_drain);
1093
1094
1095 }
1096
1097 static void storvsc_on_receive(struct storvsc_device *stor_device,
1098 struct vstor_packet *vstor_packet,
1099 struct storvsc_cmd_request *request)
1100 {
1101 struct storvsc_scan_work *work;
1102
1103 switch (vstor_packet->operation) {
1104 case VSTOR_OPERATION_COMPLETE_IO:
1105 storvsc_on_io_completion(stor_device, vstor_packet, request);
1106 break;
1107
1108 case VSTOR_OPERATION_REMOVE_DEVICE:
1109 case VSTOR_OPERATION_ENUMERATE_BUS:
1110 work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
1111 if (!work)
1112 return;
1113
1114 INIT_WORK(&work->work, storvsc_host_scan);
1115 work->host = stor_device->host;
1116 schedule_work(&work->work);
1117 break;
1118
1119 case VSTOR_OPERATION_FCHBA_DATA:
1120 cache_wwn(stor_device, vstor_packet);
1121 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1122 fc_host_node_name(stor_device->host) = stor_device->node_name;
1123 fc_host_port_name(stor_device->host) = stor_device->port_name;
1124 #endif
1125 break;
1126 default:
1127 break;
1128 }
1129 }
1130
1131 static void storvsc_on_channel_callback(void *context)
1132 {
1133 struct vmbus_channel *channel = (struct vmbus_channel *)context;
1134 struct hv_device *device;
1135 struct storvsc_device *stor_device;
1136 u32 bytes_recvd;
1137 u64 request_id;
1138 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
1139 struct storvsc_cmd_request *request;
1140 int ret;
1141
1142 if (channel->primary_channel != NULL)
1143 device = channel->primary_channel->device_obj;
1144 else
1145 device = channel->device_obj;
1146
1147 stor_device = get_in_stor_device(device);
1148 if (!stor_device)
1149 return;
1150
1151 do {
1152 ret = vmbus_recvpacket(channel, packet,
1153 ALIGN((sizeof(struct vstor_packet) -
1154 vmscsi_size_delta), 8),
1155 &bytes_recvd, &request_id);
1156 if (ret == 0 && bytes_recvd > 0) {
1157
1158 request = (struct storvsc_cmd_request *)
1159 (unsigned long)request_id;
1160
1161 if ((request == &stor_device->init_request) ||
1162 (request == &stor_device->reset_request)) {
1163
1164 memcpy(&request->vstor_packet, packet,
1165 (sizeof(struct vstor_packet) -
1166 vmscsi_size_delta));
1167 complete(&request->wait_event);
1168 } else {
1169 storvsc_on_receive(stor_device,
1170 (struct vstor_packet *)packet,
1171 request);
1172 }
1173 } else {
1174 break;
1175 }
1176 } while (1);
1177
1178 return;
1179 }
1180
1181 static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
1182 bool is_fc)
1183 {
1184 struct vmstorage_channel_properties props;
1185 int ret;
1186
1187 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
1188
1189 ret = vmbus_open(device->channel,
1190 ring_size,
1191 ring_size,
1192 (void *)&props,
1193 sizeof(struct vmstorage_channel_properties),
1194 storvsc_on_channel_callback, device->channel);
1195
1196 if (ret != 0)
1197 return ret;
1198
1199 ret = storvsc_channel_init(device, is_fc);
1200
1201 return ret;
1202 }
1203
1204 static int storvsc_dev_remove(struct hv_device *device)
1205 {
1206 struct storvsc_device *stor_device;
1207 unsigned long flags;
1208
1209 stor_device = hv_get_drvdata(device);
1210
1211 spin_lock_irqsave(&device->channel->inbound_lock, flags);
1212 stor_device->destroy = true;
1213 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1214
1215 /*
1216 * At this point, all outbound traffic should be disable. We
1217 * only allow inbound traffic (responses) to proceed so that
1218 * outstanding requests can be completed.
1219 */
1220
1221 storvsc_wait_to_drain(stor_device);
1222
1223 /*
1224 * Since we have already drained, we don't need to busy wait
1225 * as was done in final_release_stor_device()
1226 * Note that we cannot set the ext pointer to NULL until
1227 * we have drained - to drain the outgoing packets, we need to
1228 * allow incoming packets.
1229 */
1230 spin_lock_irqsave(&device->channel->inbound_lock, flags);
1231 hv_set_drvdata(device, NULL);
1232 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1233
1234 /* Close the channel */
1235 vmbus_close(device->channel);
1236
1237 kfree(stor_device->stor_chns);
1238 kfree(stor_device);
1239 return 0;
1240 }
1241
1242 static struct vmbus_channel *get_og_chn(struct storvsc_device *stor_device,
1243 u16 q_num)
1244 {
1245 u16 slot = 0;
1246 u16 hash_qnum;
1247 struct cpumask alloced_mask;
1248 int num_channels, tgt_cpu;
1249
1250 if (stor_device->num_sc == 0)
1251 return stor_device->device->channel;
1252
1253 /*
1254 * Our channel array is sparsley populated and we
1255 * initiated I/O on a processor/hw-q that does not
1256 * currently have a designated channel. Fix this.
1257 * The strategy is simple:
1258 * I. Ensure NUMA locality
1259 * II. Distribute evenly (best effort)
1260 * III. Mapping is persistent.
1261 */
1262
1263 cpumask_and(&alloced_mask, &stor_device->alloced_cpus,
1264 cpumask_of_node(cpu_to_node(q_num)));
1265
1266 num_channels = cpumask_weight(&alloced_mask);
1267 if (num_channels == 0)
1268 return stor_device->device->channel;
1269
1270 hash_qnum = q_num;
1271 while (hash_qnum >= num_channels)
1272 hash_qnum -= num_channels;
1273
1274 for_each_cpu(tgt_cpu, &alloced_mask) {
1275 if (slot == hash_qnum)
1276 break;
1277 slot++;
1278 }
1279
1280 stor_device->stor_chns[q_num] = stor_device->stor_chns[tgt_cpu];
1281
1282 return stor_device->stor_chns[q_num];
1283 }
1284
1285
1286 static int storvsc_do_io(struct hv_device *device,
1287 struct storvsc_cmd_request *request, u16 q_num)
1288 {
1289 struct storvsc_device *stor_device;
1290 struct vstor_packet *vstor_packet;
1291 struct vmbus_channel *outgoing_channel;
1292 int ret = 0;
1293 struct cpumask alloced_mask;
1294 int tgt_cpu;
1295
1296 vstor_packet = &request->vstor_packet;
1297 stor_device = get_out_stor_device(device);
1298
1299 if (!stor_device)
1300 return -ENODEV;
1301
1302
1303 request->device = device;
1304 /*
1305 * Select an an appropriate channel to send the request out.
1306 */
1307
1308 if (stor_device->stor_chns[q_num] != NULL) {
1309 outgoing_channel = stor_device->stor_chns[q_num];
1310 if (outgoing_channel->target_cpu == smp_processor_id()) {
1311 /*
1312 * Ideally, we want to pick a different channel if
1313 * available on the same NUMA node.
1314 */
1315 cpumask_and(&alloced_mask, &stor_device->alloced_cpus,
1316 cpumask_of_node(cpu_to_node(q_num)));
1317 for_each_cpu(tgt_cpu, &alloced_mask) {
1318 if (tgt_cpu != outgoing_channel->target_cpu) {
1319 outgoing_channel =
1320 stor_device->stor_chns[tgt_cpu];
1321 break;
1322 }
1323 }
1324 }
1325 } else {
1326 outgoing_channel = get_og_chn(stor_device, q_num);
1327 }
1328
1329
1330 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
1331
1332 vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) -
1333 vmscsi_size_delta);
1334
1335
1336 vstor_packet->vm_srb.sense_info_length = sense_buffer_size;
1337
1338
1339 vstor_packet->vm_srb.data_transfer_length =
1340 request->payload->range.len;
1341
1342 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
1343
1344 if (request->payload->range.len) {
1345
1346 ret = vmbus_sendpacket_mpb_desc(outgoing_channel,
1347 request->payload, request->payload_sz,
1348 vstor_packet,
1349 (sizeof(struct vstor_packet) -
1350 vmscsi_size_delta),
1351 (unsigned long)request);
1352 } else {
1353 ret = vmbus_sendpacket(outgoing_channel, vstor_packet,
1354 (sizeof(struct vstor_packet) -
1355 vmscsi_size_delta),
1356 (unsigned long)request,
1357 VM_PKT_DATA_INBAND,
1358 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1359 }
1360
1361 if (ret != 0)
1362 return ret;
1363
1364 atomic_inc(&stor_device->num_outstanding_req);
1365
1366 return ret;
1367 }
1368
1369 static int storvsc_device_configure(struct scsi_device *sdevice)
1370 {
1371
1372 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
1373
1374 blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
1375
1376 /* Ensure there are no gaps in presented sgls */
1377 blk_queue_virt_boundary(sdevice->request_queue, PAGE_SIZE - 1);
1378
1379 sdevice->no_write_same = 1;
1380
1381 /*
1382 * Add blist flags to permit the reading of the VPD pages even when
1383 * the target may claim SPC-2 compliance. MSFT targets currently
1384 * claim SPC-2 compliance while they implement post SPC-2 features.
1385 * With this patch we can correctly handle WRITE_SAME_16 issues.
1386 */
1387 sdevice->sdev_bflags |= msft_blist_flags;
1388
1389 /*
1390 * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3
1391 * if the device is a MSFT virtual device. If the host is
1392 * WIN10 or newer, allow write_same.
1393 */
1394 if (!strncmp(sdevice->vendor, "Msft", 4)) {
1395 switch (vmstor_proto_version) {
1396 case VMSTOR_PROTO_VERSION_WIN8:
1397 case VMSTOR_PROTO_VERSION_WIN8_1:
1398 sdevice->scsi_level = SCSI_SPC_3;
1399 break;
1400 }
1401
1402 if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN10)
1403 sdevice->no_write_same = 0;
1404 }
1405
1406 return 0;
1407 }
1408
1409 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1410 sector_t capacity, int *info)
1411 {
1412 sector_t nsect = capacity;
1413 sector_t cylinders = nsect;
1414 int heads, sectors_pt;
1415
1416 /*
1417 * We are making up these values; let us keep it simple.
1418 */
1419 heads = 0xff;
1420 sectors_pt = 0x3f; /* Sectors per track */
1421 sector_div(cylinders, heads * sectors_pt);
1422 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1423 cylinders = 0xffff;
1424
1425 info[0] = heads;
1426 info[1] = sectors_pt;
1427 info[2] = (int)cylinders;
1428
1429 return 0;
1430 }
1431
1432 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
1433 {
1434 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1435 struct hv_device *device = host_dev->dev;
1436
1437 struct storvsc_device *stor_device;
1438 struct storvsc_cmd_request *request;
1439 struct vstor_packet *vstor_packet;
1440 int ret, t;
1441
1442
1443 stor_device = get_out_stor_device(device);
1444 if (!stor_device)
1445 return FAILED;
1446
1447 request = &stor_device->reset_request;
1448 vstor_packet = &request->vstor_packet;
1449
1450 init_completion(&request->wait_event);
1451
1452 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1453 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1454 vstor_packet->vm_srb.path_id = stor_device->path_id;
1455
1456 ret = vmbus_sendpacket(device->channel, vstor_packet,
1457 (sizeof(struct vstor_packet) -
1458 vmscsi_size_delta),
1459 (unsigned long)&stor_device->reset_request,
1460 VM_PKT_DATA_INBAND,
1461 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1462 if (ret != 0)
1463 return FAILED;
1464
1465 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
1466 if (t == 0)
1467 return TIMEOUT_ERROR;
1468
1469
1470 /*
1471 * At this point, all outstanding requests in the adapter
1472 * should have been flushed out and return to us
1473 * There is a potential race here where the host may be in
1474 * the process of responding when we return from here.
1475 * Just wait for all in-transit packets to be accounted for
1476 * before we return from here.
1477 */
1478 storvsc_wait_to_drain(stor_device);
1479
1480 return SUCCESS;
1481 }
1482
1483 /*
1484 * The host guarantees to respond to each command, although I/O latencies might
1485 * be unbounded on Azure. Reset the timer unconditionally to give the host a
1486 * chance to perform EH.
1487 */
1488 static enum blk_eh_timer_return storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
1489 {
1490 return BLK_EH_RESET_TIMER;
1491 }
1492
1493 static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
1494 {
1495 bool allowed = true;
1496 u8 scsi_op = scmnd->cmnd[0];
1497
1498 switch (scsi_op) {
1499 /* the host does not handle WRITE_SAME, log accident usage */
1500 case WRITE_SAME:
1501 /*
1502 * smartd sends this command and the host does not handle
1503 * this. So, don't send it.
1504 */
1505 case SET_WINDOW:
1506 scmnd->result = ILLEGAL_REQUEST << 16;
1507 allowed = false;
1508 break;
1509 default:
1510 break;
1511 }
1512 return allowed;
1513 }
1514
1515 static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
1516 {
1517 int ret;
1518 struct hv_host_device *host_dev = shost_priv(host);
1519 struct hv_device *dev = host_dev->dev;
1520 struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd);
1521 int i;
1522 struct scatterlist *sgl;
1523 unsigned int sg_count = 0;
1524 struct vmscsi_request *vm_srb;
1525 struct scatterlist *cur_sgl;
1526 struct vmbus_packet_mpb_array *payload;
1527 u32 payload_sz;
1528 u32 length;
1529
1530 if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) {
1531 /*
1532 * On legacy hosts filter unimplemented commands.
1533 * Future hosts are expected to correctly handle
1534 * unsupported commands. Furthermore, it is
1535 * possible that some of the currently
1536 * unsupported commands maybe supported in
1537 * future versions of the host.
1538 */
1539 if (!storvsc_scsi_cmd_ok(scmnd)) {
1540 scmnd->scsi_done(scmnd);
1541 return 0;
1542 }
1543 }
1544
1545 /* Setup the cmd request */
1546 cmd_request->cmd = scmnd;
1547
1548 vm_srb = &cmd_request->vstor_packet.vm_srb;
1549 vm_srb->win8_extension.time_out_value = 60;
1550
1551 vm_srb->win8_extension.srb_flags |=
1552 SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
1553
1554 if (scmnd->device->tagged_supported) {
1555 vm_srb->win8_extension.srb_flags |=
1556 (SRB_FLAGS_QUEUE_ACTION_ENABLE | SRB_FLAGS_NO_QUEUE_FREEZE);
1557 vm_srb->win8_extension.queue_tag = SP_UNTAGGED;
1558 vm_srb->win8_extension.queue_action = SRB_SIMPLE_TAG_REQUEST;
1559 }
1560
1561 /* Build the SRB */
1562 switch (scmnd->sc_data_direction) {
1563 case DMA_TO_DEVICE:
1564 vm_srb->data_in = WRITE_TYPE;
1565 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT;
1566 break;
1567 case DMA_FROM_DEVICE:
1568 vm_srb->data_in = READ_TYPE;
1569 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN;
1570 break;
1571 case DMA_NONE:
1572 vm_srb->data_in = UNKNOWN_TYPE;
1573 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER;
1574 break;
1575 default:
1576 /*
1577 * This is DMA_BIDIRECTIONAL or something else we are never
1578 * supposed to see here.
1579 */
1580 WARN(1, "Unexpected data direction: %d\n",
1581 scmnd->sc_data_direction);
1582 return -EINVAL;
1583 }
1584
1585
1586 vm_srb->port_number = host_dev->port;
1587 vm_srb->path_id = scmnd->device->channel;
1588 vm_srb->target_id = scmnd->device->id;
1589 vm_srb->lun = scmnd->device->lun;
1590
1591 vm_srb->cdb_length = scmnd->cmd_len;
1592
1593 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1594
1595 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1596 sg_count = scsi_sg_count(scmnd);
1597
1598 length = scsi_bufflen(scmnd);
1599 payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
1600 payload_sz = sizeof(cmd_request->mpb);
1601
1602 if (sg_count) {
1603 if (sg_count > MAX_PAGE_BUFFER_COUNT) {
1604
1605 payload_sz = (sg_count * sizeof(u64) +
1606 sizeof(struct vmbus_packet_mpb_array));
1607 payload = kzalloc(payload_sz, GFP_ATOMIC);
1608 if (!payload)
1609 return SCSI_MLQUEUE_DEVICE_BUSY;
1610 }
1611
1612 payload->range.len = length;
1613 payload->range.offset = sgl[0].offset;
1614
1615 cur_sgl = sgl;
1616 for (i = 0; i < sg_count; i++) {
1617 payload->range.pfn_array[i] =
1618 page_to_pfn(sg_page((cur_sgl)));
1619 cur_sgl = sg_next(cur_sgl);
1620 }
1621
1622 } else if (scsi_sglist(scmnd)) {
1623 payload->range.len = length;
1624 payload->range.offset =
1625 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
1626 payload->range.pfn_array[0] =
1627 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1628 }
1629
1630 cmd_request->payload = payload;
1631 cmd_request->payload_sz = payload_sz;
1632
1633 /* Invokes the vsc to start an IO */
1634 ret = storvsc_do_io(dev, cmd_request, get_cpu());
1635 put_cpu();
1636
1637 if (ret == -EAGAIN) {
1638 /* no more space */
1639 return SCSI_MLQUEUE_DEVICE_BUSY;
1640 }
1641
1642 return 0;
1643 }
1644
1645 static struct scsi_host_template scsi_driver = {
1646 .module = THIS_MODULE,
1647 .name = "storvsc_host_t",
1648 .cmd_size = sizeof(struct storvsc_cmd_request),
1649 .bios_param = storvsc_get_chs,
1650 .queuecommand = storvsc_queuecommand,
1651 .eh_host_reset_handler = storvsc_host_reset_handler,
1652 .proc_name = "storvsc_host",
1653 .eh_timed_out = storvsc_eh_timed_out,
1654 .slave_configure = storvsc_device_configure,
1655 .cmd_per_lun = 255,
1656 .this_id = -1,
1657 .use_clustering = ENABLE_CLUSTERING,
1658 /* Make sure we dont get a sg segment crosses a page boundary */
1659 .dma_boundary = PAGE_SIZE-1,
1660 .no_write_same = 1,
1661 .track_queue_depth = 1,
1662 };
1663
1664 enum {
1665 SCSI_GUID,
1666 IDE_GUID,
1667 SFC_GUID,
1668 };
1669
1670 static const struct hv_vmbus_device_id id_table[] = {
1671 /* SCSI guid */
1672 { HV_SCSI_GUID,
1673 .driver_data = SCSI_GUID
1674 },
1675 /* IDE guid */
1676 { HV_IDE_GUID,
1677 .driver_data = IDE_GUID
1678 },
1679 /* Fibre Channel GUID */
1680 {
1681 HV_SYNTHFC_GUID,
1682 .driver_data = SFC_GUID
1683 },
1684 { },
1685 };
1686
1687 MODULE_DEVICE_TABLE(vmbus, id_table);
1688
1689 static int storvsc_probe(struct hv_device *device,
1690 const struct hv_vmbus_device_id *dev_id)
1691 {
1692 int ret;
1693 int num_cpus = num_online_cpus();
1694 struct Scsi_Host *host;
1695 struct hv_host_device *host_dev;
1696 bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
1697 bool is_fc = ((dev_id->driver_data == SFC_GUID) ? true : false);
1698 int target = 0;
1699 struct storvsc_device *stor_device;
1700 int max_luns_per_target;
1701 int max_targets;
1702 int max_channels;
1703 int max_sub_channels = 0;
1704
1705 /*
1706 * Based on the windows host we are running on,
1707 * set state to properly communicate with the host.
1708 */
1709
1710 if (vmbus_proto_version < VERSION_WIN8) {
1711 max_luns_per_target = STORVSC_IDE_MAX_LUNS_PER_TARGET;
1712 max_targets = STORVSC_IDE_MAX_TARGETS;
1713 max_channels = STORVSC_IDE_MAX_CHANNELS;
1714 } else {
1715 max_luns_per_target = STORVSC_MAX_LUNS_PER_TARGET;
1716 max_targets = STORVSC_MAX_TARGETS;
1717 max_channels = STORVSC_MAX_CHANNELS;
1718 /*
1719 * On Windows8 and above, we support sub-channels for storage.
1720 * The number of sub-channels offerred is based on the number of
1721 * VCPUs in the guest.
1722 */
1723 max_sub_channels = (num_cpus / storvsc_vcpus_per_sub_channel);
1724 }
1725
1726 scsi_driver.can_queue = (max_outstanding_req_per_channel *
1727 (max_sub_channels + 1));
1728
1729 host = scsi_host_alloc(&scsi_driver,
1730 sizeof(struct hv_host_device));
1731 if (!host)
1732 return -ENOMEM;
1733
1734 host_dev = shost_priv(host);
1735 memset(host_dev, 0, sizeof(struct hv_host_device));
1736
1737 host_dev->port = host->host_no;
1738 host_dev->dev = device;
1739
1740
1741 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
1742 if (!stor_device) {
1743 ret = -ENOMEM;
1744 goto err_out0;
1745 }
1746
1747 stor_device->destroy = false;
1748 stor_device->open_sub_channel = false;
1749 init_waitqueue_head(&stor_device->waiting_to_drain);
1750 stor_device->device = device;
1751 stor_device->host = host;
1752 hv_set_drvdata(device, stor_device);
1753
1754 stor_device->port_number = host->host_no;
1755 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size, is_fc);
1756 if (ret)
1757 goto err_out1;
1758
1759 host_dev->path = stor_device->path_id;
1760 host_dev->target = stor_device->target_id;
1761
1762 switch (dev_id->driver_data) {
1763 case SFC_GUID:
1764 host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
1765 host->max_id = STORVSC_FC_MAX_TARGETS;
1766 host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
1767 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1768 host->transportt = fc_transport_template;
1769 #endif
1770 break;
1771
1772 case SCSI_GUID:
1773 host->max_lun = max_luns_per_target;
1774 host->max_id = max_targets;
1775 host->max_channel = max_channels - 1;
1776 break;
1777
1778 default:
1779 host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
1780 host->max_id = STORVSC_IDE_MAX_TARGETS;
1781 host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
1782 break;
1783 }
1784 /* max cmd length */
1785 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
1786
1787 /*
1788 * set the table size based on the info we got
1789 * from the host.
1790 */
1791 host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
1792 /*
1793 * Set the number of HW queues we are supporting.
1794 */
1795 if (stor_device->num_sc != 0)
1796 host->nr_hw_queues = stor_device->num_sc + 1;
1797
1798 /* Register the HBA and start the scsi bus scan */
1799 ret = scsi_add_host(host, &device->device);
1800 if (ret != 0)
1801 goto err_out2;
1802
1803 if (!dev_is_ide) {
1804 scsi_scan_host(host);
1805 } else {
1806 target = (device->dev_instance.b[5] << 8 |
1807 device->dev_instance.b[4]);
1808 ret = scsi_add_device(host, 0, target, 0);
1809 if (ret) {
1810 scsi_remove_host(host);
1811 goto err_out2;
1812 }
1813 }
1814 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1815 if (host->transportt == fc_transport_template) {
1816 fc_host_node_name(host) = stor_device->node_name;
1817 fc_host_port_name(host) = stor_device->port_name;
1818 }
1819 #endif
1820 return 0;
1821
1822 err_out2:
1823 /*
1824 * Once we have connected with the host, we would need to
1825 * to invoke storvsc_dev_remove() to rollback this state and
1826 * this call also frees up the stor_device; hence the jump around
1827 * err_out1 label.
1828 */
1829 storvsc_dev_remove(device);
1830 goto err_out0;
1831
1832 err_out1:
1833 kfree(stor_device->stor_chns);
1834 kfree(stor_device);
1835
1836 err_out0:
1837 scsi_host_put(host);
1838 return ret;
1839 }
1840
1841 static int storvsc_remove(struct hv_device *dev)
1842 {
1843 struct storvsc_device *stor_device = hv_get_drvdata(dev);
1844 struct Scsi_Host *host = stor_device->host;
1845
1846 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1847 if (host->transportt == fc_transport_template)
1848 fc_remove_host(host);
1849 #endif
1850 scsi_remove_host(host);
1851 storvsc_dev_remove(dev);
1852 scsi_host_put(host);
1853
1854 return 0;
1855 }
1856
1857 static struct hv_driver storvsc_drv = {
1858 .name = KBUILD_MODNAME,
1859 .id_table = id_table,
1860 .probe = storvsc_probe,
1861 .remove = storvsc_remove,
1862 };
1863
1864 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1865 static struct fc_function_template fc_transport_functions = {
1866 .show_host_node_name = 1,
1867 .show_host_port_name = 1,
1868 };
1869 #endif
1870
1871 static int __init storvsc_drv_init(void)
1872 {
1873 int ret;
1874
1875 /*
1876 * Divide the ring buffer data size (which is 1 page less
1877 * than the ring buffer size since that page is reserved for
1878 * the ring buffer indices) by the max request size (which is
1879 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1880 */
1881 max_outstanding_req_per_channel =
1882 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1883 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1884 sizeof(struct vstor_packet) + sizeof(u64) -
1885 vmscsi_size_delta,
1886 sizeof(u64)));
1887
1888 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1889 fc_transport_template = fc_attach_transport(&fc_transport_functions);
1890 if (!fc_transport_template)
1891 return -ENODEV;
1892
1893 /*
1894 * Install Hyper-V specific timeout handler.
1895 */
1896 fc_transport_template->eh_timed_out = storvsc_eh_timed_out;
1897 #endif
1898
1899 ret = vmbus_driver_register(&storvsc_drv);
1900
1901 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1902 if (ret)
1903 fc_release_transport(fc_transport_template);
1904 #endif
1905
1906 return ret;
1907 }
1908
1909 static void __exit storvsc_drv_exit(void)
1910 {
1911 vmbus_driver_unregister(&storvsc_drv);
1912 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1913 fc_release_transport(fc_transport_template);
1914 #endif
1915 }
1916
1917 MODULE_LICENSE("GPL");
1918 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
1919 module_init(storvsc_drv_init);
1920 module_exit(storvsc_drv_exit);