1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
75enum 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
94
95
96struct 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
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
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
144
145
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
157
158
159
160
161static int sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
162
163
164
165
166
167
168static int vmstor_proto_version;
169
170#define STORVSC_LOGGING_NONE 0
171#define STORVSC_LOGGING_ERROR 1
172#define STORVSC_LOGGING_WARN 2
173
174static int logging_level = STORVSC_LOGGING_ERROR;
175module_param(logging_level, int, S_IRUGO|S_IWUSR);
176MODULE_PARM_DESC(logging_level,
177 "Logging level, 0 - None, 1 - Error (default), 2 - Warning.");
178
179static inline bool do_logging(int level)
180{
181 return logging_level >= level;
182}
183
184#define storvsc_log(dev, level, fmt, ...) \
185do { \
186 if (do_logging(level)) \
187 dev_warn(&(dev)->device, fmt, ##__VA_ARGS__); \
188} while (0)
189
190struct vmscsi_win8_extension {
191
192
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
202struct 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
226
227 struct vmscsi_win8_extension win8_extension;
228
229} __attribute((packed));
230
231
232
233
234
235
236
237
238
239
240
241static int vmscsi_size_delta = sizeof(struct vmscsi_win8_extension);
242
243
244
245
246struct vmstor_protocol {
247 int protocol_version;
248 int sense_buffer_size;
249 int vmscsi_size_delta;
250};
251
252
253static 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
284
285
286
287#define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1
288
289struct 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
301struct vmstorage_protocol_version {
302
303 u16 major_minor;
304
305
306
307
308
309
310
311
312 u16 revision;
313} __packed;
314
315
316#define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
317#define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
318
319struct vstor_packet {
320
321 enum vstor_packet_operation operation;
322
323
324 u32 flags;
325
326
327 u32 status;
328
329
330 union {
331
332
333
334
335 struct vmscsi_request vm_srb;
336
337
338 struct vmstorage_channel_properties storage_channel_properties;
339
340
341 struct vmstorage_protocol_version version;
342
343
344 struct hv_fc_wwn_packet wwn_packet;
345
346
347 u16 sub_channel_count;
348
349
350 u8 buffer[0x34];
351 };
352} __packed;
353
354
355
356
357
358
359
360
361#define REQUEST_COMPLETION_FLAG 0x1
362
363
364enum storvsc_request_type {
365 WRITE_TYPE = 0,
366 READ_TYPE,
367 UNKNOWN_TYPE,
368};
369
370
371
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#define SRB_STATUS_DATA_OVERRUN 0x12
381
382#define SRB_STATUS(status) \
383 (status & ~(SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_QUEUE_FROZEN))
384
385
386
387
388static int storvsc_ringbuffer_size = (256 * PAGE_SIZE);
389static u32 max_outstanding_req_per_channel;
390
391static int storvsc_vcpus_per_sub_channel = 4;
392
393module_param(storvsc_ringbuffer_size, int, S_IRUGO);
394MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
395
396module_param(storvsc_vcpus_per_sub_channel, int, S_IRUGO);
397MODULE_PARM_DESC(storvsc_vcpus_per_sub_channel, "Ratio of VCPUs to subchannels");
398
399static int ring_avail_percent_lowater = 10;
400module_param(ring_avail_percent_lowater, int, S_IRUGO);
401MODULE_PARM_DESC(ring_avail_percent_lowater,
402 "Select a channel if available ring size > this in percent");
403
404
405
406
407static int storvsc_timeout = 180;
408
409#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
410static struct scsi_transport_template *fc_transport_template;
411#endif
412
413static void storvsc_on_channel_callback(void *context);
414
415#define STORVSC_MAX_LUNS_PER_TARGET 255
416#define STORVSC_MAX_TARGETS 2
417#define STORVSC_MAX_CHANNELS 8
418
419#define STORVSC_FC_MAX_LUNS_PER_TARGET 255
420#define STORVSC_FC_MAX_TARGETS 128
421#define STORVSC_FC_MAX_CHANNELS 8
422
423#define STORVSC_IDE_MAX_LUNS_PER_TARGET 64
424#define STORVSC_IDE_MAX_TARGETS 1
425#define STORVSC_IDE_MAX_CHANNELS 1
426
427struct storvsc_cmd_request {
428 struct scsi_cmnd *cmd;
429
430 struct hv_device *device;
431
432
433 struct completion wait_event;
434
435 struct vmbus_channel_packet_multipage_buffer mpb;
436 struct vmbus_packet_mpb_array *payload;
437 u32 payload_sz;
438
439 struct vstor_packet vstor_packet;
440};
441
442
443
444struct storvsc_device {
445 struct hv_device *device;
446
447 bool destroy;
448 bool drain_notify;
449 atomic_t num_outstanding_req;
450 struct Scsi_Host *host;
451
452 wait_queue_head_t waiting_to_drain;
453
454
455
456
457
458
459 unsigned int port_number;
460 unsigned char path_id;
461 unsigned char target_id;
462
463
464
465
466 u32 max_transfer_bytes;
467
468
469
470 u16 num_sc;
471 struct vmbus_channel **stor_chns;
472
473
474
475 struct cpumask alloced_cpus;
476
477 struct storvsc_cmd_request init_request;
478 struct storvsc_cmd_request reset_request;
479
480
481
482 u64 node_name;
483 u64 port_name;
484#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
485 struct fc_rport *rport;
486#endif
487};
488
489struct hv_host_device {
490 struct hv_device *dev;
491 unsigned int port;
492 unsigned char path;
493 unsigned char target;
494 struct workqueue_struct *handle_error_wq;
495 struct work_struct host_scan_work;
496 struct Scsi_Host *host;
497};
498
499struct storvsc_scan_work {
500 struct work_struct work;
501 struct Scsi_Host *host;
502 u8 lun;
503 u8 tgt_id;
504};
505
506static void storvsc_device_scan(struct work_struct *work)
507{
508 struct storvsc_scan_work *wrk;
509 struct scsi_device *sdev;
510
511 wrk = container_of(work, struct storvsc_scan_work, work);
512
513 sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
514 if (!sdev)
515 goto done;
516 scsi_rescan_device(&sdev->sdev_gendev);
517 scsi_device_put(sdev);
518
519done:
520 kfree(wrk);
521}
522
523static void storvsc_host_scan(struct work_struct *work)
524{
525 struct Scsi_Host *host;
526 struct scsi_device *sdev;
527 struct hv_host_device *host_device =
528 container_of(work, struct hv_host_device, host_scan_work);
529
530 host = host_device->host;
531
532
533
534
535
536
537
538
539
540
541
542 mutex_lock(&host->scan_mutex);
543 shost_for_each_device(sdev, host)
544 scsi_test_unit_ready(sdev, 1, 1, NULL);
545 mutex_unlock(&host->scan_mutex);
546
547
548
549 scsi_scan_host(host);
550}
551
552static void storvsc_remove_lun(struct work_struct *work)
553{
554 struct storvsc_scan_work *wrk;
555 struct scsi_device *sdev;
556
557 wrk = container_of(work, struct storvsc_scan_work, work);
558 if (!scsi_host_get(wrk->host))
559 goto done;
560
561 sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
562
563 if (sdev) {
564 scsi_remove_device(sdev);
565 scsi_device_put(sdev);
566 }
567 scsi_host_put(wrk->host);
568
569done:
570 kfree(wrk);
571}
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589static inline struct storvsc_device *get_out_stor_device(
590 struct hv_device *device)
591{
592 struct storvsc_device *stor_device;
593
594 stor_device = hv_get_drvdata(device);
595
596 if (stor_device && stor_device->destroy)
597 stor_device = NULL;
598
599 return stor_device;
600}
601
602
603static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
604{
605 dev->drain_notify = true;
606 wait_event(dev->waiting_to_drain,
607 atomic_read(&dev->num_outstanding_req) == 0);
608 dev->drain_notify = false;
609}
610
611static inline struct storvsc_device *get_in_stor_device(
612 struct hv_device *device)
613{
614 struct storvsc_device *stor_device;
615
616 stor_device = hv_get_drvdata(device);
617
618 if (!stor_device)
619 goto get_in_err;
620
621
622
623
624
625
626 if (stor_device->destroy &&
627 (atomic_read(&stor_device->num_outstanding_req) == 0))
628 stor_device = NULL;
629
630get_in_err:
631 return stor_device;
632
633}
634
635static void handle_sc_creation(struct vmbus_channel *new_sc)
636{
637 struct hv_device *device = new_sc->primary_channel->device_obj;
638 struct device *dev = &device->device;
639 struct storvsc_device *stor_device;
640 struct vmstorage_channel_properties props;
641 int ret;
642
643 stor_device = get_out_stor_device(device);
644 if (!stor_device)
645 return;
646
647 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
648
649 ret = vmbus_open(new_sc,
650 storvsc_ringbuffer_size,
651 storvsc_ringbuffer_size,
652 (void *)&props,
653 sizeof(struct vmstorage_channel_properties),
654 storvsc_on_channel_callback, new_sc);
655
656
657 if (ret != 0) {
658 dev_err(dev, "Failed to open sub-channel: err=%d\n", ret);
659 return;
660 }
661
662
663 stor_device->stor_chns[new_sc->target_cpu] = new_sc;
664 cpumask_set_cpu(new_sc->target_cpu, &stor_device->alloced_cpus);
665}
666
667static void handle_multichannel_storage(struct hv_device *device, int max_chns)
668{
669 struct device *dev = &device->device;
670 struct storvsc_device *stor_device;
671 int num_cpus = num_online_cpus();
672 int num_sc;
673 struct storvsc_cmd_request *request;
674 struct vstor_packet *vstor_packet;
675 int ret, t;
676
677 num_sc = ((max_chns > num_cpus) ? num_cpus : max_chns);
678 stor_device = get_out_stor_device(device);
679 if (!stor_device)
680 return;
681
682 stor_device->num_sc = num_sc;
683 request = &stor_device->init_request;
684 vstor_packet = &request->vstor_packet;
685
686
687
688
689 vmbus_set_sc_create_callback(device->channel, handle_sc_creation);
690
691
692
693
694 memset(request, 0, sizeof(struct storvsc_cmd_request));
695 init_completion(&request->wait_event);
696 vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
697 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
698 vstor_packet->sub_channel_count = num_sc;
699
700 ret = vmbus_sendpacket(device->channel, vstor_packet,
701 (sizeof(struct vstor_packet) -
702 vmscsi_size_delta),
703 (unsigned long)request,
704 VM_PKT_DATA_INBAND,
705 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
706
707 if (ret != 0) {
708 dev_err(dev, "Failed to create sub-channel: err=%d\n", ret);
709 return;
710 }
711
712 t = wait_for_completion_timeout(&request->wait_event, 10*HZ);
713 if (t == 0) {
714 dev_err(dev, "Failed to create sub-channel: timed out\n");
715 return;
716 }
717
718 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
719 vstor_packet->status != 0) {
720 dev_err(dev, "Failed to create sub-channel: op=%d, sts=%d\n",
721 vstor_packet->operation, vstor_packet->status);
722 return;
723 }
724
725
726
727
728
729
730}
731
732static void cache_wwn(struct storvsc_device *stor_device,
733 struct vstor_packet *vstor_packet)
734{
735
736
737
738 if (vstor_packet->wwn_packet.primary_active) {
739 stor_device->node_name =
740 wwn_to_u64(vstor_packet->wwn_packet.primary_node_wwn);
741 stor_device->port_name =
742 wwn_to_u64(vstor_packet->wwn_packet.primary_port_wwn);
743 } else {
744 stor_device->node_name =
745 wwn_to_u64(vstor_packet->wwn_packet.secondary_node_wwn);
746 stor_device->port_name =
747 wwn_to_u64(vstor_packet->wwn_packet.secondary_port_wwn);
748 }
749}
750
751
752static int storvsc_execute_vstor_op(struct hv_device *device,
753 struct storvsc_cmd_request *request,
754 bool status_check)
755{
756 struct vstor_packet *vstor_packet;
757 int ret, t;
758
759 vstor_packet = &request->vstor_packet;
760
761 init_completion(&request->wait_event);
762 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
763
764 ret = vmbus_sendpacket(device->channel, vstor_packet,
765 (sizeof(struct vstor_packet) -
766 vmscsi_size_delta),
767 (unsigned long)request,
768 VM_PKT_DATA_INBAND,
769 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
770 if (ret != 0)
771 return ret;
772
773 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
774 if (t == 0)
775 return -ETIMEDOUT;
776
777 if (!status_check)
778 return ret;
779
780 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
781 vstor_packet->status != 0)
782 return -EINVAL;
783
784 return ret;
785}
786
787static int storvsc_channel_init(struct hv_device *device, bool is_fc)
788{
789 struct storvsc_device *stor_device;
790 struct storvsc_cmd_request *request;
791 struct vstor_packet *vstor_packet;
792 int ret, i;
793 int max_chns;
794 bool process_sub_channels = false;
795
796 stor_device = get_out_stor_device(device);
797 if (!stor_device)
798 return -ENODEV;
799
800 request = &stor_device->init_request;
801 vstor_packet = &request->vstor_packet;
802
803
804
805
806
807 memset(request, 0, sizeof(struct storvsc_cmd_request));
808 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
809 ret = storvsc_execute_vstor_op(device, request, true);
810 if (ret)
811 return ret;
812
813
814
815
816 for (i = 0; i < ARRAY_SIZE(vmstor_protocols); i++) {
817
818 memset(vstor_packet, 0, sizeof(struct vstor_packet));
819 vstor_packet->operation =
820 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
821
822 vstor_packet->version.major_minor =
823 vmstor_protocols[i].protocol_version;
824
825
826
827
828 vstor_packet->version.revision = 0;
829 ret = storvsc_execute_vstor_op(device, request, false);
830 if (ret != 0)
831 return ret;
832
833 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO)
834 return -EINVAL;
835
836 if (vstor_packet->status == 0) {
837 vmstor_proto_version =
838 vmstor_protocols[i].protocol_version;
839
840 sense_buffer_size =
841 vmstor_protocols[i].sense_buffer_size;
842
843 vmscsi_size_delta =
844 vmstor_protocols[i].vmscsi_size_delta;
845
846 break;
847 }
848 }
849
850 if (vstor_packet->status != 0)
851 return -EINVAL;
852
853
854 memset(vstor_packet, 0, sizeof(struct vstor_packet));
855 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
856 ret = storvsc_execute_vstor_op(device, request, true);
857 if (ret != 0)
858 return ret;
859
860
861
862
863
864
865 max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
866
867
868
869
870
871
872
873
874
875
876 stor_device->stor_chns = kzalloc(sizeof(void *) * num_possible_cpus(),
877 GFP_KERNEL);
878 if (stor_device->stor_chns == NULL)
879 return -ENOMEM;
880
881 stor_device->stor_chns[device->channel->target_cpu] = device->channel;
882 cpumask_set_cpu(device->channel->target_cpu,
883 &stor_device->alloced_cpus);
884
885 if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN8) {
886 if (vstor_packet->storage_channel_properties.flags &
887 STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
888 process_sub_channels = true;
889 }
890 stor_device->max_transfer_bytes =
891 vstor_packet->storage_channel_properties.max_transfer_bytes;
892
893 if (!is_fc)
894 goto done;
895
896
897
898
899 memset(vstor_packet, 0, sizeof(struct vstor_packet));
900 vstor_packet->operation = VSTOR_OPERATION_FCHBA_DATA;
901 ret = storvsc_execute_vstor_op(device, request, true);
902 if (ret != 0)
903 return ret;
904
905
906
907
908 cache_wwn(stor_device, vstor_packet);
909
910done:
911
912 memset(vstor_packet, 0, sizeof(struct vstor_packet));
913 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
914 ret = storvsc_execute_vstor_op(device, request, true);
915 if (ret != 0)
916 return ret;
917
918 if (process_sub_channels)
919 handle_multichannel_storage(device, max_chns);
920
921 return ret;
922}
923
924static void storvsc_handle_error(struct vmscsi_request *vm_srb,
925 struct scsi_cmnd *scmnd,
926 struct Scsi_Host *host,
927 u8 asc, u8 ascq)
928{
929 struct storvsc_scan_work *wrk;
930 void (*process_err_fn)(struct work_struct *work);
931 struct hv_host_device *host_dev = shost_priv(host);
932 bool do_work = false;
933
934 switch (SRB_STATUS(vm_srb->srb_status)) {
935 case SRB_STATUS_ERROR:
936
937
938
939
940
941 if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID)
942 break;
943
944
945
946
947
948
949 switch (scmnd->cmnd[0]) {
950 case ATA_16:
951 case ATA_12:
952 set_host_byte(scmnd, DID_PASSTHROUGH);
953 break;
954
955
956
957
958
959 case TEST_UNIT_READY:
960 break;
961 default:
962 set_host_byte(scmnd, DID_ERROR);
963 }
964 break;
965 case SRB_STATUS_INVALID_LUN:
966 set_host_byte(scmnd, DID_NO_CONNECT);
967 do_work = true;
968 process_err_fn = storvsc_remove_lun;
969 break;
970 case SRB_STATUS_ABORTED:
971 if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID &&
972 (asc == 0x2a) && (ascq == 0x9)) {
973 do_work = true;
974 process_err_fn = storvsc_device_scan;
975
976
977
978 set_host_byte(scmnd, DID_REQUEUE);
979 }
980 break;
981 }
982
983 if (!do_work)
984 return;
985
986
987
988
989 wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
990 if (!wrk) {
991 set_host_byte(scmnd, DID_TARGET_FAILURE);
992 return;
993 }
994
995 wrk->host = host;
996 wrk->lun = vm_srb->lun;
997 wrk->tgt_id = vm_srb->target_id;
998 INIT_WORK(&wrk->work, process_err_fn);
999 queue_work(host_dev->handle_error_wq, &wrk->work);
1000}
1001
1002
1003static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request,
1004 struct storvsc_device *stor_dev)
1005{
1006 struct scsi_cmnd *scmnd = cmd_request->cmd;
1007 struct scsi_sense_hdr sense_hdr;
1008 struct vmscsi_request *vm_srb;
1009 u32 data_transfer_length;
1010 struct Scsi_Host *host;
1011 u32 payload_sz = cmd_request->payload_sz;
1012 void *payload = cmd_request->payload;
1013
1014 host = stor_dev->host;
1015
1016 vm_srb = &cmd_request->vstor_packet.vm_srb;
1017 data_transfer_length = vm_srb->data_transfer_length;
1018
1019 scmnd->result = vm_srb->scsi_status;
1020
1021 if (scmnd->result) {
1022 if (scsi_normalize_sense(scmnd->sense_buffer,
1023 SCSI_SENSE_BUFFERSIZE, &sense_hdr) &&
1024 !(sense_hdr.sense_key == NOT_READY &&
1025 sense_hdr.asc == 0x03A) &&
1026 do_logging(STORVSC_LOGGING_ERROR))
1027 scsi_print_sense_hdr(scmnd->device, "storvsc",
1028 &sense_hdr);
1029 }
1030
1031 if (vm_srb->srb_status != SRB_STATUS_SUCCESS) {
1032 storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
1033 sense_hdr.ascq);
1034
1035
1036
1037
1038
1039 if (vm_srb->srb_status != SRB_STATUS_DATA_OVERRUN)
1040 data_transfer_length = 0;
1041 }
1042
1043 scsi_set_resid(scmnd,
1044 cmd_request->payload->range.len - data_transfer_length);
1045
1046 scmnd->scsi_done(scmnd);
1047
1048 if (payload_sz >
1049 sizeof(struct vmbus_channel_packet_multipage_buffer))
1050 kfree(payload);
1051}
1052
1053static void storvsc_on_io_completion(struct storvsc_device *stor_device,
1054 struct vstor_packet *vstor_packet,
1055 struct storvsc_cmd_request *request)
1056{
1057 struct vstor_packet *stor_pkt;
1058 struct hv_device *device = stor_device->device;
1059
1060 stor_pkt = &request->vstor_packet;
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073 if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
1074 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
1075 vstor_packet->vm_srb.scsi_status = 0;
1076 vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
1077 }
1078
1079
1080
1081 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
1082 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
1083 stor_pkt->vm_srb.sense_info_length =
1084 vstor_packet->vm_srb.sense_info_length;
1085
1086 if (vstor_packet->vm_srb.scsi_status != 0 ||
1087 vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS)
1088 storvsc_log(device, STORVSC_LOGGING_WARN,
1089 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
1090 stor_pkt->vm_srb.cdb[0],
1091 vstor_packet->vm_srb.scsi_status,
1092 vstor_packet->vm_srb.srb_status);
1093
1094 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
1095
1096 if (vstor_packet->vm_srb.srb_status &
1097 SRB_STATUS_AUTOSENSE_VALID) {
1098
1099
1100 storvsc_log(device, STORVSC_LOGGING_WARN,
1101 "stor pkt %p autosense data valid - len %d\n",
1102 request, vstor_packet->vm_srb.sense_info_length);
1103
1104 memcpy(request->cmd->sense_buffer,
1105 vstor_packet->vm_srb.sense_data,
1106 vstor_packet->vm_srb.sense_info_length);
1107
1108 }
1109 }
1110
1111 stor_pkt->vm_srb.data_transfer_length =
1112 vstor_packet->vm_srb.data_transfer_length;
1113
1114 storvsc_command_completion(request, stor_device);
1115
1116 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
1117 stor_device->drain_notify)
1118 wake_up(&stor_device->waiting_to_drain);
1119
1120
1121}
1122
1123static void storvsc_on_receive(struct storvsc_device *stor_device,
1124 struct vstor_packet *vstor_packet,
1125 struct storvsc_cmd_request *request)
1126{
1127 struct hv_host_device *host_dev;
1128 switch (vstor_packet->operation) {
1129 case VSTOR_OPERATION_COMPLETE_IO:
1130 storvsc_on_io_completion(stor_device, vstor_packet, request);
1131 break;
1132
1133 case VSTOR_OPERATION_REMOVE_DEVICE:
1134 case VSTOR_OPERATION_ENUMERATE_BUS:
1135 host_dev = shost_priv(stor_device->host);
1136 queue_work(
1137 host_dev->handle_error_wq, &host_dev->host_scan_work);
1138 break;
1139
1140 case VSTOR_OPERATION_FCHBA_DATA:
1141 cache_wwn(stor_device, vstor_packet);
1142#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1143 fc_host_node_name(stor_device->host) = stor_device->node_name;
1144 fc_host_port_name(stor_device->host) = stor_device->port_name;
1145#endif
1146 break;
1147 default:
1148 break;
1149 }
1150}
1151
1152static void storvsc_on_channel_callback(void *context)
1153{
1154 struct vmbus_channel *channel = (struct vmbus_channel *)context;
1155 const struct vmpacket_descriptor *desc;
1156 struct hv_device *device;
1157 struct storvsc_device *stor_device;
1158
1159 if (channel->primary_channel != NULL)
1160 device = channel->primary_channel->device_obj;
1161 else
1162 device = channel->device_obj;
1163
1164 stor_device = get_in_stor_device(device);
1165 if (!stor_device)
1166 return;
1167
1168 foreach_vmbus_pkt(desc, channel) {
1169 void *packet = hv_pkt_data(desc);
1170 struct storvsc_cmd_request *request;
1171
1172 request = (struct storvsc_cmd_request *)
1173 ((unsigned long)desc->trans_id);
1174
1175 if (request == &stor_device->init_request ||
1176 request == &stor_device->reset_request) {
1177 memcpy(&request->vstor_packet, packet,
1178 (sizeof(struct vstor_packet) - vmscsi_size_delta));
1179 complete(&request->wait_event);
1180 } else {
1181 storvsc_on_receive(stor_device, packet, request);
1182 }
1183 }
1184}
1185
1186static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
1187 bool is_fc)
1188{
1189 struct vmstorage_channel_properties props;
1190 int ret;
1191
1192 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
1193
1194 ret = vmbus_open(device->channel,
1195 ring_size,
1196 ring_size,
1197 (void *)&props,
1198 sizeof(struct vmstorage_channel_properties),
1199 storvsc_on_channel_callback, device->channel);
1200
1201 if (ret != 0)
1202 return ret;
1203
1204 ret = storvsc_channel_init(device, is_fc);
1205
1206 return ret;
1207}
1208
1209static int storvsc_dev_remove(struct hv_device *device)
1210{
1211 struct storvsc_device *stor_device;
1212
1213 stor_device = hv_get_drvdata(device);
1214
1215 stor_device->destroy = true;
1216
1217
1218 wmb();
1219
1220
1221
1222
1223
1224
1225
1226 storvsc_wait_to_drain(stor_device);
1227
1228
1229
1230
1231
1232
1233
1234
1235 hv_set_drvdata(device, NULL);
1236
1237
1238 vmbus_close(device->channel);
1239
1240 kfree(stor_device->stor_chns);
1241 kfree(stor_device);
1242 return 0;
1243}
1244
1245static struct vmbus_channel *get_og_chn(struct storvsc_device *stor_device,
1246 u16 q_num)
1247{
1248 u16 slot = 0;
1249 u16 hash_qnum;
1250 const struct cpumask *node_mask;
1251 int num_channels, tgt_cpu;
1252
1253 if (stor_device->num_sc == 0)
1254 return stor_device->device->channel;
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266 node_mask = cpumask_of_node(cpu_to_node(q_num));
1267
1268 num_channels = 0;
1269 for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
1270 if (cpumask_test_cpu(tgt_cpu, node_mask))
1271 num_channels++;
1272 }
1273 if (num_channels == 0)
1274 return stor_device->device->channel;
1275
1276 hash_qnum = q_num;
1277 while (hash_qnum >= num_channels)
1278 hash_qnum -= num_channels;
1279
1280 for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
1281 if (!cpumask_test_cpu(tgt_cpu, node_mask))
1282 continue;
1283 if (slot == hash_qnum)
1284 break;
1285 slot++;
1286 }
1287
1288 stor_device->stor_chns[q_num] = stor_device->stor_chns[tgt_cpu];
1289
1290 return stor_device->stor_chns[q_num];
1291}
1292
1293
1294static int storvsc_do_io(struct hv_device *device,
1295 struct storvsc_cmd_request *request, u16 q_num)
1296{
1297 struct storvsc_device *stor_device;
1298 struct vstor_packet *vstor_packet;
1299 struct vmbus_channel *outgoing_channel, *channel;
1300 int ret = 0;
1301 const struct cpumask *node_mask;
1302 int tgt_cpu;
1303
1304 vstor_packet = &request->vstor_packet;
1305 stor_device = get_out_stor_device(device);
1306
1307 if (!stor_device)
1308 return -ENODEV;
1309
1310
1311 request->device = device;
1312
1313
1314
1315 if (stor_device->stor_chns[q_num] != NULL) {
1316 outgoing_channel = stor_device->stor_chns[q_num];
1317 if (outgoing_channel->target_cpu == q_num) {
1318
1319
1320
1321
1322 node_mask = cpumask_of_node(cpu_to_node(q_num));
1323 for_each_cpu_wrap(tgt_cpu,
1324 &stor_device->alloced_cpus, q_num + 1) {
1325 if (!cpumask_test_cpu(tgt_cpu, node_mask))
1326 continue;
1327 if (tgt_cpu == q_num)
1328 continue;
1329 channel = stor_device->stor_chns[tgt_cpu];
1330 if (hv_get_avail_to_write_percent(
1331 &channel->outbound)
1332 > ring_avail_percent_lowater) {
1333 outgoing_channel = channel;
1334 goto found_channel;
1335 }
1336 }
1337
1338
1339
1340
1341
1342 if (hv_get_avail_to_write_percent(
1343 &outgoing_channel->outbound)
1344 > ring_avail_percent_lowater)
1345 goto found_channel;
1346
1347
1348
1349
1350
1351
1352 for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
1353 if (cpumask_test_cpu(tgt_cpu, node_mask))
1354 continue;
1355 channel = stor_device->stor_chns[tgt_cpu];
1356 if (hv_get_avail_to_write_percent(
1357 &channel->outbound)
1358 > ring_avail_percent_lowater) {
1359 outgoing_channel = channel;
1360 goto found_channel;
1361 }
1362 }
1363 }
1364 } else {
1365 outgoing_channel = get_og_chn(stor_device, q_num);
1366 }
1367
1368found_channel:
1369 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
1370
1371 vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) -
1372 vmscsi_size_delta);
1373
1374
1375 vstor_packet->vm_srb.sense_info_length = sense_buffer_size;
1376
1377
1378 vstor_packet->vm_srb.data_transfer_length =
1379 request->payload->range.len;
1380
1381 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
1382
1383 if (request->payload->range.len) {
1384
1385 ret = vmbus_sendpacket_mpb_desc(outgoing_channel,
1386 request->payload, request->payload_sz,
1387 vstor_packet,
1388 (sizeof(struct vstor_packet) -
1389 vmscsi_size_delta),
1390 (unsigned long)request);
1391 } else {
1392 ret = vmbus_sendpacket(outgoing_channel, vstor_packet,
1393 (sizeof(struct vstor_packet) -
1394 vmscsi_size_delta),
1395 (unsigned long)request,
1396 VM_PKT_DATA_INBAND,
1397 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1398 }
1399
1400 if (ret != 0)
1401 return ret;
1402
1403 atomic_inc(&stor_device->num_outstanding_req);
1404
1405 return ret;
1406}
1407
1408static int storvsc_device_alloc(struct scsi_device *sdevice)
1409{
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419 sdevice->sdev_bflags = BLIST_REPORTLUN2 | BLIST_TRY_VPD_PAGES;
1420
1421 return 0;
1422}
1423
1424static int storvsc_device_configure(struct scsi_device *sdevice)
1425{
1426 blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
1427
1428
1429 blk_queue_virt_boundary(sdevice->request_queue, PAGE_SIZE - 1);
1430
1431 sdevice->no_write_same = 1;
1432
1433
1434
1435
1436
1437 if (!strncmp(sdevice->vendor, "Msft", 4)) {
1438 switch (vmstor_proto_version) {
1439 case VMSTOR_PROTO_VERSION_WIN8:
1440 case VMSTOR_PROTO_VERSION_WIN8_1:
1441 sdevice->scsi_level = SCSI_SPC_3;
1442 break;
1443 }
1444
1445 if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN10)
1446 sdevice->no_write_same = 0;
1447 }
1448
1449 return 0;
1450}
1451
1452static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1453 sector_t capacity, int *info)
1454{
1455 sector_t nsect = capacity;
1456 sector_t cylinders = nsect;
1457 int heads, sectors_pt;
1458
1459
1460
1461
1462 heads = 0xff;
1463 sectors_pt = 0x3f;
1464 sector_div(cylinders, heads * sectors_pt);
1465 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1466 cylinders = 0xffff;
1467
1468 info[0] = heads;
1469 info[1] = sectors_pt;
1470 info[2] = (int)cylinders;
1471
1472 return 0;
1473}
1474
1475static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
1476{
1477 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1478 struct hv_device *device = host_dev->dev;
1479
1480 struct storvsc_device *stor_device;
1481 struct storvsc_cmd_request *request;
1482 struct vstor_packet *vstor_packet;
1483 int ret, t;
1484
1485
1486 stor_device = get_out_stor_device(device);
1487 if (!stor_device)
1488 return FAILED;
1489
1490 request = &stor_device->reset_request;
1491 vstor_packet = &request->vstor_packet;
1492
1493 init_completion(&request->wait_event);
1494
1495 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1496 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1497 vstor_packet->vm_srb.path_id = stor_device->path_id;
1498
1499 ret = vmbus_sendpacket(device->channel, vstor_packet,
1500 (sizeof(struct vstor_packet) -
1501 vmscsi_size_delta),
1502 (unsigned long)&stor_device->reset_request,
1503 VM_PKT_DATA_INBAND,
1504 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1505 if (ret != 0)
1506 return FAILED;
1507
1508 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
1509 if (t == 0)
1510 return TIMEOUT_ERROR;
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521 storvsc_wait_to_drain(stor_device);
1522
1523 return SUCCESS;
1524}
1525
1526
1527
1528
1529
1530
1531static enum blk_eh_timer_return storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
1532{
1533 return BLK_EH_RESET_TIMER;
1534}
1535
1536static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
1537{
1538 bool allowed = true;
1539 u8 scsi_op = scmnd->cmnd[0];
1540
1541 switch (scsi_op) {
1542
1543 case WRITE_SAME:
1544
1545
1546
1547
1548 case SET_WINDOW:
1549 scmnd->result = ILLEGAL_REQUEST << 16;
1550 allowed = false;
1551 break;
1552 default:
1553 break;
1554 }
1555 return allowed;
1556}
1557
1558static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
1559{
1560 int ret;
1561 struct hv_host_device *host_dev = shost_priv(host);
1562 struct hv_device *dev = host_dev->dev;
1563 struct storvsc_cmd_request *cmd_request =
1564 (struct storvsc_cmd_request *)(scmnd + 1);
1565 int i;
1566 struct scatterlist *sgl;
1567 unsigned int sg_count = 0;
1568 struct vmscsi_request *vm_srb;
1569 struct scatterlist *cur_sgl;
1570 struct vmbus_packet_mpb_array *payload;
1571 u32 payload_sz;
1572 u32 length;
1573
1574 if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) {
1575
1576
1577
1578
1579
1580
1581
1582
1583 if (!storvsc_scsi_cmd_ok(scmnd)) {
1584 scmnd->scsi_done(scmnd);
1585 return 0;
1586 }
1587 }
1588
1589
1590 cmd_request->cmd = scmnd;
1591
1592 vm_srb = &cmd_request->vstor_packet.vm_srb;
1593 vm_srb->win8_extension.time_out_value = 60;
1594
1595 vm_srb->win8_extension.srb_flags |=
1596 SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
1597
1598 if (scmnd->device->tagged_supported) {
1599 vm_srb->win8_extension.srb_flags |=
1600 (SRB_FLAGS_QUEUE_ACTION_ENABLE | SRB_FLAGS_NO_QUEUE_FREEZE);
1601 vm_srb->win8_extension.queue_tag = SP_UNTAGGED;
1602 vm_srb->win8_extension.queue_action = SRB_SIMPLE_TAG_REQUEST;
1603 }
1604
1605
1606 switch (scmnd->sc_data_direction) {
1607 case DMA_TO_DEVICE:
1608 vm_srb->data_in = WRITE_TYPE;
1609 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT;
1610 break;
1611 case DMA_FROM_DEVICE:
1612 vm_srb->data_in = READ_TYPE;
1613 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN;
1614 break;
1615 case DMA_NONE:
1616 vm_srb->data_in = UNKNOWN_TYPE;
1617 vm_srb->win8_extension.srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER;
1618 break;
1619 default:
1620
1621
1622
1623
1624 WARN(1, "Unexpected data direction: %d\n",
1625 scmnd->sc_data_direction);
1626 return -EINVAL;
1627 }
1628
1629
1630 vm_srb->port_number = host_dev->port;
1631 vm_srb->path_id = scmnd->device->channel;
1632 vm_srb->target_id = scmnd->device->id;
1633 vm_srb->lun = scmnd->device->lun;
1634
1635 vm_srb->cdb_length = scmnd->cmd_len;
1636
1637 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1638
1639 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1640 sg_count = scsi_sg_count(scmnd);
1641
1642 length = scsi_bufflen(scmnd);
1643 payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
1644 payload_sz = sizeof(cmd_request->mpb);
1645
1646 if (sg_count) {
1647 if (sg_count > MAX_PAGE_BUFFER_COUNT) {
1648
1649 payload_sz = (sg_count * sizeof(void *) +
1650 sizeof(struct vmbus_packet_mpb_array));
1651 payload = kmalloc(payload_sz, GFP_ATOMIC);
1652 if (!payload)
1653 return SCSI_MLQUEUE_DEVICE_BUSY;
1654 }
1655
1656 payload->range.len = length;
1657 payload->range.offset = sgl[0].offset;
1658
1659 cur_sgl = sgl;
1660 for (i = 0; i < sg_count; i++) {
1661 payload->range.pfn_array[i] =
1662 page_to_pfn(sg_page((cur_sgl)));
1663 cur_sgl = sg_next(cur_sgl);
1664 }
1665 }
1666
1667 cmd_request->payload = payload;
1668 cmd_request->payload_sz = payload_sz;
1669
1670
1671 ret = storvsc_do_io(dev, cmd_request, get_cpu());
1672 put_cpu();
1673
1674 if (ret == -EAGAIN) {
1675 if (payload_sz > sizeof(cmd_request->mpb))
1676 kfree(payload);
1677
1678 return SCSI_MLQUEUE_DEVICE_BUSY;
1679 }
1680
1681 return 0;
1682}
1683
1684static struct scsi_host_template scsi_driver = {
1685 .module = THIS_MODULE,
1686 .name = "storvsc_host_t",
1687 .cmd_size = sizeof(struct storvsc_cmd_request),
1688 .bios_param = storvsc_get_chs,
1689 .queuecommand = storvsc_queuecommand,
1690 .eh_host_reset_handler = storvsc_host_reset_handler,
1691 .proc_name = "storvsc_host",
1692 .eh_timed_out = storvsc_eh_timed_out,
1693 .slave_alloc = storvsc_device_alloc,
1694 .slave_configure = storvsc_device_configure,
1695 .cmd_per_lun = 2048,
1696 .this_id = -1,
1697 .use_clustering = ENABLE_CLUSTERING,
1698
1699 .dma_boundary = PAGE_SIZE-1,
1700 .no_write_same = 1,
1701};
1702
1703enum {
1704 SCSI_GUID,
1705 IDE_GUID,
1706 SFC_GUID,
1707};
1708
1709static const struct hv_vmbus_device_id id_table[] = {
1710
1711 { HV_SCSI_GUID,
1712 .driver_data = SCSI_GUID
1713 },
1714
1715 { HV_IDE_GUID,
1716 .driver_data = IDE_GUID
1717 },
1718
1719 {
1720 HV_SYNTHFC_GUID,
1721 .driver_data = SFC_GUID
1722 },
1723 { },
1724};
1725
1726MODULE_DEVICE_TABLE(vmbus, id_table);
1727
1728static int storvsc_probe(struct hv_device *device,
1729 const struct hv_vmbus_device_id *dev_id)
1730{
1731 int ret;
1732 int num_cpus = num_online_cpus();
1733 struct Scsi_Host *host;
1734 struct hv_host_device *host_dev;
1735 bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
1736 bool is_fc = ((dev_id->driver_data == SFC_GUID) ? true : false);
1737 int target = 0;
1738 struct storvsc_device *stor_device;
1739 int max_luns_per_target;
1740 int max_targets;
1741 int max_channels;
1742 int max_sub_channels = 0;
1743
1744
1745
1746
1747
1748
1749 if (vmbus_proto_version < VERSION_WIN8) {
1750 max_luns_per_target = STORVSC_IDE_MAX_LUNS_PER_TARGET;
1751 max_targets = STORVSC_IDE_MAX_TARGETS;
1752 max_channels = STORVSC_IDE_MAX_CHANNELS;
1753 } else {
1754 max_luns_per_target = STORVSC_MAX_LUNS_PER_TARGET;
1755 max_targets = STORVSC_MAX_TARGETS;
1756 max_channels = STORVSC_MAX_CHANNELS;
1757
1758
1759
1760
1761
1762
1763 if (!dev_is_ide)
1764 max_sub_channels =
1765 (num_cpus - 1) / storvsc_vcpus_per_sub_channel;
1766 }
1767
1768 scsi_driver.can_queue = max_outstanding_req_per_channel *
1769 (max_sub_channels + 1) *
1770 (100 - ring_avail_percent_lowater) / 100;
1771
1772 host = scsi_host_alloc(&scsi_driver,
1773 sizeof(struct hv_host_device));
1774 if (!host)
1775 return -ENOMEM;
1776
1777 host_dev = shost_priv(host);
1778 memset(host_dev, 0, sizeof(struct hv_host_device));
1779
1780 host_dev->port = host->host_no;
1781 host_dev->dev = device;
1782 host_dev->host = host;
1783
1784
1785 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
1786 if (!stor_device) {
1787 ret = -ENOMEM;
1788 goto err_out0;
1789 }
1790
1791 stor_device->destroy = false;
1792 init_waitqueue_head(&stor_device->waiting_to_drain);
1793 stor_device->device = device;
1794 stor_device->host = host;
1795 hv_set_drvdata(device, stor_device);
1796
1797 stor_device->port_number = host->host_no;
1798 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size, is_fc);
1799 if (ret)
1800 goto err_out1;
1801
1802 host_dev->path = stor_device->path_id;
1803 host_dev->target = stor_device->target_id;
1804
1805 switch (dev_id->driver_data) {
1806 case SFC_GUID:
1807 host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
1808 host->max_id = STORVSC_FC_MAX_TARGETS;
1809 host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
1810#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1811 host->transportt = fc_transport_template;
1812#endif
1813 break;
1814
1815 case SCSI_GUID:
1816 host->max_lun = max_luns_per_target;
1817 host->max_id = max_targets;
1818 host->max_channel = max_channels - 1;
1819 break;
1820
1821 default:
1822 host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
1823 host->max_id = STORVSC_IDE_MAX_TARGETS;
1824 host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
1825 break;
1826 }
1827
1828 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
1829
1830
1831
1832
1833
1834 host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
1835
1836
1837
1838 if (stor_device->num_sc != 0)
1839 host->nr_hw_queues = stor_device->num_sc + 1;
1840
1841
1842
1843
1844 host_dev->handle_error_wq =
1845 alloc_ordered_workqueue("storvsc_error_wq_%d",
1846 WQ_MEM_RECLAIM,
1847 host->host_no);
1848 if (!host_dev->handle_error_wq)
1849 goto err_out2;
1850 INIT_WORK(&host_dev->host_scan_work, storvsc_host_scan);
1851
1852 ret = scsi_add_host(host, &device->device);
1853 if (ret != 0)
1854 goto err_out3;
1855
1856 if (!dev_is_ide) {
1857 scsi_scan_host(host);
1858 } else {
1859 target = (device->dev_instance.b[5] << 8 |
1860 device->dev_instance.b[4]);
1861 ret = scsi_add_device(host, 0, target, 0);
1862 if (ret)
1863 goto err_out4;
1864 }
1865#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1866 if (host->transportt == fc_transport_template) {
1867 struct fc_rport_identifiers ids = {
1868 .roles = FC_PORT_ROLE_FCP_DUMMY_INITIATOR,
1869 };
1870
1871 fc_host_node_name(host) = stor_device->node_name;
1872 fc_host_port_name(host) = stor_device->port_name;
1873 stor_device->rport = fc_remote_port_add(host, 0, &ids);
1874 if (!stor_device->rport) {
1875 ret = -ENOMEM;
1876 goto err_out4;
1877 }
1878 }
1879#endif
1880 return 0;
1881
1882err_out4:
1883 scsi_remove_host(host);
1884
1885err_out3:
1886 destroy_workqueue(host_dev->handle_error_wq);
1887
1888err_out2:
1889
1890
1891
1892
1893
1894
1895 storvsc_dev_remove(device);
1896 goto err_out0;
1897
1898err_out1:
1899 kfree(stor_device->stor_chns);
1900 kfree(stor_device);
1901
1902err_out0:
1903 scsi_host_put(host);
1904 return ret;
1905}
1906
1907static int storvsc_remove(struct hv_device *dev)
1908{
1909 struct storvsc_device *stor_device = hv_get_drvdata(dev);
1910 struct Scsi_Host *host = stor_device->host;
1911 struct hv_host_device *host_dev = shost_priv(host);
1912
1913#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1914 if (host->transportt == fc_transport_template) {
1915 fc_remote_port_delete(stor_device->rport);
1916 fc_remove_host(host);
1917 }
1918#endif
1919 destroy_workqueue(host_dev->handle_error_wq);
1920 scsi_remove_host(host);
1921 storvsc_dev_remove(dev);
1922 scsi_host_put(host);
1923
1924 return 0;
1925}
1926
1927static struct hv_driver storvsc_drv = {
1928 .name = KBUILD_MODNAME,
1929 .id_table = id_table,
1930 .probe = storvsc_probe,
1931 .remove = storvsc_remove,
1932};
1933
1934#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1935static struct fc_function_template fc_transport_functions = {
1936 .show_host_node_name = 1,
1937 .show_host_port_name = 1,
1938};
1939#endif
1940
1941static int __init storvsc_drv_init(void)
1942{
1943 int ret;
1944
1945
1946
1947
1948
1949
1950
1951 max_outstanding_req_per_channel =
1952 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1953 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1954 sizeof(struct vstor_packet) + sizeof(u64) -
1955 vmscsi_size_delta,
1956 sizeof(u64)));
1957
1958#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1959 fc_transport_template = fc_attach_transport(&fc_transport_functions);
1960 if (!fc_transport_template)
1961 return -ENODEV;
1962#endif
1963
1964 ret = vmbus_driver_register(&storvsc_drv);
1965
1966#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1967 if (ret)
1968 fc_release_transport(fc_transport_template);
1969#endif
1970
1971 return ret;
1972}
1973
1974static void __exit storvsc_drv_exit(void)
1975{
1976 vmbus_driver_unregister(&storvsc_drv);
1977#if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1978 fc_release_transport(fc_transport_template);
1979#endif
1980}
1981
1982MODULE_LICENSE("GPL");
1983MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
1984module_init(storvsc_drv_init);
1985module_exit(storvsc_drv_exit);
1986