0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/kernel.h>
0012 #include <linux/wait.h>
0013 #include <linux/sched.h>
0014 #include <linux/completion.h>
0015 #include <linux/string.h>
0016 #include <linux/mm.h>
0017 #include <linux/delay.h>
0018 #include <linux/init.h>
0019 #include <linux/slab.h>
0020 #include <linux/module.h>
0021 #include <linux/device.h>
0022 #include <linux/hyperv.h>
0023 #include <linux/blkdev.h>
0024 #include <linux/dma-mapping.h>
0025
0026 #include <scsi/scsi.h>
0027 #include <scsi/scsi_cmnd.h>
0028 #include <scsi/scsi_host.h>
0029 #include <scsi/scsi_device.h>
0030 #include <scsi/scsi_tcq.h>
0031 #include <scsi/scsi_eh.h>
0032 #include <scsi/scsi_devinfo.h>
0033 #include <scsi/scsi_dbg.h>
0034 #include <scsi/scsi_transport_fc.h>
0035 #include <scsi/scsi_transport.h>
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055 #define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
0056 (((MINOR_) & 0xff)))
0057 #define VMSTOR_PROTO_VERSION_WIN6 VMSTOR_PROTO_VERSION(2, 0)
0058 #define VMSTOR_PROTO_VERSION_WIN7 VMSTOR_PROTO_VERSION(4, 2)
0059 #define VMSTOR_PROTO_VERSION_WIN8 VMSTOR_PROTO_VERSION(5, 1)
0060 #define VMSTOR_PROTO_VERSION_WIN8_1 VMSTOR_PROTO_VERSION(6, 0)
0061 #define VMSTOR_PROTO_VERSION_WIN10 VMSTOR_PROTO_VERSION(6, 2)
0062
0063
0064 enum vstor_packet_operation {
0065 VSTOR_OPERATION_COMPLETE_IO = 1,
0066 VSTOR_OPERATION_REMOVE_DEVICE = 2,
0067 VSTOR_OPERATION_EXECUTE_SRB = 3,
0068 VSTOR_OPERATION_RESET_LUN = 4,
0069 VSTOR_OPERATION_RESET_ADAPTER = 5,
0070 VSTOR_OPERATION_RESET_BUS = 6,
0071 VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
0072 VSTOR_OPERATION_END_INITIALIZATION = 8,
0073 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
0074 VSTOR_OPERATION_QUERY_PROPERTIES = 10,
0075 VSTOR_OPERATION_ENUMERATE_BUS = 11,
0076 VSTOR_OPERATION_FCHBA_DATA = 12,
0077 VSTOR_OPERATION_CREATE_SUB_CHANNELS = 13,
0078 VSTOR_OPERATION_MAXIMUM = 13
0079 };
0080
0081
0082
0083
0084
0085 struct hv_fc_wwn_packet {
0086 u8 primary_active;
0087 u8 reserved1[3];
0088 u8 primary_port_wwn[8];
0089 u8 primary_node_wwn[8];
0090 u8 secondary_port_wwn[8];
0091 u8 secondary_node_wwn[8];
0092 };
0093
0094
0095
0096
0097
0098
0099
0100 #define SRB_FLAGS_QUEUE_ACTION_ENABLE 0x00000002
0101 #define SRB_FLAGS_DISABLE_DISCONNECT 0x00000004
0102 #define SRB_FLAGS_DISABLE_SYNCH_TRANSFER 0x00000008
0103 #define SRB_FLAGS_BYPASS_FROZEN_QUEUE 0x00000010
0104 #define SRB_FLAGS_DISABLE_AUTOSENSE 0x00000020
0105 #define SRB_FLAGS_DATA_IN 0x00000040
0106 #define SRB_FLAGS_DATA_OUT 0x00000080
0107 #define SRB_FLAGS_NO_DATA_TRANSFER 0x00000000
0108 #define SRB_FLAGS_UNSPECIFIED_DIRECTION (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT)
0109 #define SRB_FLAGS_NO_QUEUE_FREEZE 0x00000100
0110 #define SRB_FLAGS_ADAPTER_CACHE_ENABLE 0x00000200
0111 #define SRB_FLAGS_FREE_SENSE_BUFFER 0x00000400
0112
0113
0114
0115
0116 #define SRB_FLAGS_D3_PROCESSING 0x00000800
0117 #define SRB_FLAGS_IS_ACTIVE 0x00010000
0118 #define SRB_FLAGS_ALLOCATED_FROM_ZONE 0x00020000
0119 #define SRB_FLAGS_SGLIST_FROM_POOL 0x00040000
0120 #define SRB_FLAGS_BYPASS_LOCKED_QUEUE 0x00080000
0121 #define SRB_FLAGS_NO_KEEP_AWAKE 0x00100000
0122 #define SRB_FLAGS_PORT_DRIVER_ALLOCSENSE 0x00200000
0123 #define SRB_FLAGS_PORT_DRIVER_SENSEHASPORT 0x00400000
0124 #define SRB_FLAGS_DONT_START_NEXT_PACKET 0x00800000
0125 #define SRB_FLAGS_PORT_DRIVER_RESERVED 0x0F000000
0126 #define SRB_FLAGS_CLASS_DRIVER_RESERVED 0xF0000000
0127
0128 #define SP_UNTAGGED ((unsigned char) ~0)
0129 #define SRB_SIMPLE_TAG_REQUEST 0x20
0130
0131
0132
0133
0134
0135
0136 #define STORVSC_MAX_CMD_LEN 0x10
0137
0138
0139 #define STORVSC_SENSE_BUFFER_SIZE 0x14
0140 #define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
0141
0142
0143
0144
0145
0146
0147 static int vmstor_proto_version;
0148
0149 #define STORVSC_LOGGING_NONE 0
0150 #define STORVSC_LOGGING_ERROR 1
0151 #define STORVSC_LOGGING_WARN 2
0152
0153 static int logging_level = STORVSC_LOGGING_ERROR;
0154 module_param(logging_level, int, S_IRUGO|S_IWUSR);
0155 MODULE_PARM_DESC(logging_level,
0156 "Logging level, 0 - None, 1 - Error (default), 2 - Warning.");
0157
0158 static inline bool do_logging(int level)
0159 {
0160 return logging_level >= level;
0161 }
0162
0163 #define storvsc_log(dev, level, fmt, ...) \
0164 do { \
0165 if (do_logging(level)) \
0166 dev_warn(&(dev)->device, fmt, ##__VA_ARGS__); \
0167 } while (0)
0168
0169 struct vmscsi_request {
0170 u16 length;
0171 u8 srb_status;
0172 u8 scsi_status;
0173
0174 u8 port_number;
0175 u8 path_id;
0176 u8 target_id;
0177 u8 lun;
0178
0179 u8 cdb_length;
0180 u8 sense_info_length;
0181 u8 data_in;
0182 u8 reserved;
0183
0184 u32 data_transfer_length;
0185
0186 union {
0187 u8 cdb[STORVSC_MAX_CMD_LEN];
0188 u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
0189 u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
0190 };
0191
0192
0193
0194 u16 reserve;
0195 u8 queue_tag;
0196 u8 queue_action;
0197 u32 srb_flags;
0198 u32 time_out_value;
0199 u32 queue_sort_ey;
0200
0201 } __attribute((packed));
0202
0203
0204
0205
0206
0207 static const int protocol_version[] = {
0208 VMSTOR_PROTO_VERSION_WIN10,
0209 VMSTOR_PROTO_VERSION_WIN8_1,
0210 VMSTOR_PROTO_VERSION_WIN8,
0211 };
0212
0213
0214
0215
0216
0217
0218
0219 #define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1
0220
0221 struct vmstorage_channel_properties {
0222 u32 reserved;
0223 u16 max_channel_cnt;
0224 u16 reserved1;
0225
0226 u32 flags;
0227 u32 max_transfer_bytes;
0228
0229 u64 reserved2;
0230 } __packed;
0231
0232
0233 struct vmstorage_protocol_version {
0234
0235 u16 major_minor;
0236
0237
0238
0239
0240
0241
0242
0243
0244 u16 revision;
0245 } __packed;
0246
0247
0248 #define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
0249 #define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
0250
0251 struct vstor_packet {
0252
0253 enum vstor_packet_operation operation;
0254
0255
0256 u32 flags;
0257
0258
0259 u32 status;
0260
0261
0262 union {
0263
0264
0265
0266
0267 struct vmscsi_request vm_srb;
0268
0269
0270 struct vmstorage_channel_properties storage_channel_properties;
0271
0272
0273 struct vmstorage_protocol_version version;
0274
0275
0276 struct hv_fc_wwn_packet wwn_packet;
0277
0278
0279 u16 sub_channel_count;
0280
0281
0282 u8 buffer[0x34];
0283 };
0284 } __packed;
0285
0286
0287
0288
0289
0290
0291
0292
0293 #define REQUEST_COMPLETION_FLAG 0x1
0294
0295
0296 enum storvsc_request_type {
0297 WRITE_TYPE = 0,
0298 READ_TYPE,
0299 UNKNOWN_TYPE,
0300 };
0301
0302
0303
0304
0305
0306 #define SRB_STATUS_AUTOSENSE_VALID 0x80
0307 #define SRB_STATUS_QUEUE_FROZEN 0x40
0308 #define SRB_STATUS_INVALID_LUN 0x20
0309 #define SRB_STATUS_SUCCESS 0x01
0310 #define SRB_STATUS_ABORTED 0x02
0311 #define SRB_STATUS_ERROR 0x04
0312 #define SRB_STATUS_DATA_OVERRUN 0x12
0313
0314 #define SRB_STATUS(status) \
0315 (status & ~(SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_QUEUE_FROZEN))
0316
0317
0318
0319
0320 static int storvsc_ringbuffer_size = (128 * 1024);
0321 static u32 max_outstanding_req_per_channel;
0322 static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth);
0323
0324 static int storvsc_vcpus_per_sub_channel = 4;
0325 static unsigned int storvsc_max_hw_queues;
0326
0327 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
0328 MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
0329
0330 module_param(storvsc_max_hw_queues, uint, 0644);
0331 MODULE_PARM_DESC(storvsc_max_hw_queues, "Maximum number of hardware queues");
0332
0333 module_param(storvsc_vcpus_per_sub_channel, int, S_IRUGO);
0334 MODULE_PARM_DESC(storvsc_vcpus_per_sub_channel, "Ratio of VCPUs to subchannels");
0335
0336 static int ring_avail_percent_lowater = 10;
0337 module_param(ring_avail_percent_lowater, int, S_IRUGO);
0338 MODULE_PARM_DESC(ring_avail_percent_lowater,
0339 "Select a channel if available ring size > this in percent");
0340
0341
0342
0343
0344 static int storvsc_timeout = 180;
0345
0346 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
0347 static struct scsi_transport_template *fc_transport_template;
0348 #endif
0349
0350 static struct scsi_host_template scsi_driver;
0351 static void storvsc_on_channel_callback(void *context);
0352
0353 #define STORVSC_MAX_LUNS_PER_TARGET 255
0354 #define STORVSC_MAX_TARGETS 2
0355 #define STORVSC_MAX_CHANNELS 8
0356
0357 #define STORVSC_FC_MAX_LUNS_PER_TARGET 255
0358 #define STORVSC_FC_MAX_TARGETS 128
0359 #define STORVSC_FC_MAX_CHANNELS 8
0360
0361 #define STORVSC_IDE_MAX_LUNS_PER_TARGET 64
0362 #define STORVSC_IDE_MAX_TARGETS 1
0363 #define STORVSC_IDE_MAX_CHANNELS 1
0364
0365
0366
0367
0368 #define STORVSC_MAX_PKT_SIZE (sizeof(struct vmpacket_descriptor) +\
0369 sizeof(struct vstor_packet))
0370
0371 struct storvsc_cmd_request {
0372 struct scsi_cmnd *cmd;
0373
0374 struct hv_device *device;
0375
0376
0377 struct completion wait_event;
0378
0379 struct vmbus_channel_packet_multipage_buffer mpb;
0380 struct vmbus_packet_mpb_array *payload;
0381 u32 payload_sz;
0382
0383 struct vstor_packet vstor_packet;
0384 };
0385
0386
0387
0388 struct storvsc_device {
0389 struct hv_device *device;
0390
0391 bool destroy;
0392 bool drain_notify;
0393 atomic_t num_outstanding_req;
0394 struct Scsi_Host *host;
0395
0396 wait_queue_head_t waiting_to_drain;
0397
0398
0399
0400
0401
0402
0403 unsigned int port_number;
0404 unsigned char path_id;
0405 unsigned char target_id;
0406
0407
0408
0409
0410 u32 max_transfer_bytes;
0411
0412
0413
0414 u16 num_sc;
0415 struct vmbus_channel **stor_chns;
0416
0417
0418
0419 struct cpumask alloced_cpus;
0420
0421
0422
0423
0424 spinlock_t lock;
0425
0426 struct storvsc_cmd_request init_request;
0427 struct storvsc_cmd_request reset_request;
0428
0429
0430
0431 u64 node_name;
0432 u64 port_name;
0433 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
0434 struct fc_rport *rport;
0435 #endif
0436 };
0437
0438 struct hv_host_device {
0439 struct hv_device *dev;
0440 unsigned int port;
0441 unsigned char path;
0442 unsigned char target;
0443 struct workqueue_struct *handle_error_wq;
0444 struct work_struct host_scan_work;
0445 struct Scsi_Host *host;
0446 };
0447
0448 struct storvsc_scan_work {
0449 struct work_struct work;
0450 struct Scsi_Host *host;
0451 u8 lun;
0452 u8 tgt_id;
0453 };
0454
0455 static void storvsc_device_scan(struct work_struct *work)
0456 {
0457 struct storvsc_scan_work *wrk;
0458 struct scsi_device *sdev;
0459
0460 wrk = container_of(work, struct storvsc_scan_work, work);
0461
0462 sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
0463 if (!sdev)
0464 goto done;
0465 scsi_rescan_device(&sdev->sdev_gendev);
0466 scsi_device_put(sdev);
0467
0468 done:
0469 kfree(wrk);
0470 }
0471
0472 static void storvsc_host_scan(struct work_struct *work)
0473 {
0474 struct Scsi_Host *host;
0475 struct scsi_device *sdev;
0476 struct hv_host_device *host_device =
0477 container_of(work, struct hv_host_device, host_scan_work);
0478
0479 host = host_device->host;
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491 mutex_lock(&host->scan_mutex);
0492 shost_for_each_device(sdev, host)
0493 scsi_test_unit_ready(sdev, 1, 1, NULL);
0494 mutex_unlock(&host->scan_mutex);
0495
0496
0497
0498 scsi_scan_host(host);
0499 }
0500
0501 static void storvsc_remove_lun(struct work_struct *work)
0502 {
0503 struct storvsc_scan_work *wrk;
0504 struct scsi_device *sdev;
0505
0506 wrk = container_of(work, struct storvsc_scan_work, work);
0507 if (!scsi_host_get(wrk->host))
0508 goto done;
0509
0510 sdev = scsi_device_lookup(wrk->host, 0, wrk->tgt_id, wrk->lun);
0511
0512 if (sdev) {
0513 scsi_remove_device(sdev);
0514 scsi_device_put(sdev);
0515 }
0516 scsi_host_put(wrk->host);
0517
0518 done:
0519 kfree(wrk);
0520 }
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537
0538 static inline struct storvsc_device *get_out_stor_device(
0539 struct hv_device *device)
0540 {
0541 struct storvsc_device *stor_device;
0542
0543 stor_device = hv_get_drvdata(device);
0544
0545 if (stor_device && stor_device->destroy)
0546 stor_device = NULL;
0547
0548 return stor_device;
0549 }
0550
0551
0552 static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
0553 {
0554 dev->drain_notify = true;
0555 wait_event(dev->waiting_to_drain,
0556 atomic_read(&dev->num_outstanding_req) == 0);
0557 dev->drain_notify = false;
0558 }
0559
0560 static inline struct storvsc_device *get_in_stor_device(
0561 struct hv_device *device)
0562 {
0563 struct storvsc_device *stor_device;
0564
0565 stor_device = hv_get_drvdata(device);
0566
0567 if (!stor_device)
0568 goto get_in_err;
0569
0570
0571
0572
0573
0574
0575 if (stor_device->destroy &&
0576 (atomic_read(&stor_device->num_outstanding_req) == 0))
0577 stor_device = NULL;
0578
0579 get_in_err:
0580 return stor_device;
0581
0582 }
0583
0584 static void storvsc_change_target_cpu(struct vmbus_channel *channel, u32 old,
0585 u32 new)
0586 {
0587 struct storvsc_device *stor_device;
0588 struct vmbus_channel *cur_chn;
0589 bool old_is_alloced = false;
0590 struct hv_device *device;
0591 unsigned long flags;
0592 int cpu;
0593
0594 device = channel->primary_channel ?
0595 channel->primary_channel->device_obj
0596 : channel->device_obj;
0597 stor_device = get_out_stor_device(device);
0598 if (!stor_device)
0599 return;
0600
0601
0602 spin_lock_irqsave(&stor_device->lock, flags);
0603
0604
0605
0606
0607
0608
0609 if (device->channel != channel && device->channel->target_cpu == old) {
0610 cur_chn = device->channel;
0611 old_is_alloced = true;
0612 goto old_is_alloced;
0613 }
0614 list_for_each_entry(cur_chn, &device->channel->sc_list, sc_list) {
0615 if (cur_chn == channel)
0616 continue;
0617 if (cur_chn->target_cpu == old) {
0618 old_is_alloced = true;
0619 goto old_is_alloced;
0620 }
0621 }
0622
0623 old_is_alloced:
0624 if (old_is_alloced)
0625 WRITE_ONCE(stor_device->stor_chns[old], cur_chn);
0626 else
0627 cpumask_clear_cpu(old, &stor_device->alloced_cpus);
0628
0629
0630 for_each_possible_cpu(cpu) {
0631 if (stor_device->stor_chns[cpu] && !cpumask_test_cpu(
0632 cpu, &stor_device->alloced_cpus))
0633 WRITE_ONCE(stor_device->stor_chns[cpu], NULL);
0634 }
0635
0636 WRITE_ONCE(stor_device->stor_chns[new], channel);
0637 cpumask_set_cpu(new, &stor_device->alloced_cpus);
0638
0639 spin_unlock_irqrestore(&stor_device->lock, flags);
0640 }
0641
0642 static u64 storvsc_next_request_id(struct vmbus_channel *channel, u64 rqst_addr)
0643 {
0644 struct storvsc_cmd_request *request =
0645 (struct storvsc_cmd_request *)(unsigned long)rqst_addr;
0646
0647 if (rqst_addr == VMBUS_RQST_INIT)
0648 return VMBUS_RQST_INIT;
0649 if (rqst_addr == VMBUS_RQST_RESET)
0650 return VMBUS_RQST_RESET;
0651
0652
0653
0654
0655
0656 return (u64)blk_mq_unique_tag(scsi_cmd_to_rq(request->cmd)) + 1;
0657 }
0658
0659 static void handle_sc_creation(struct vmbus_channel *new_sc)
0660 {
0661 struct hv_device *device = new_sc->primary_channel->device_obj;
0662 struct device *dev = &device->device;
0663 struct storvsc_device *stor_device;
0664 struct vmstorage_channel_properties props;
0665 int ret;
0666
0667 stor_device = get_out_stor_device(device);
0668 if (!stor_device)
0669 return;
0670
0671 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
0672 new_sc->max_pkt_size = STORVSC_MAX_PKT_SIZE;
0673
0674 new_sc->next_request_id_callback = storvsc_next_request_id;
0675
0676 ret = vmbus_open(new_sc,
0677 storvsc_ringbuffer_size,
0678 storvsc_ringbuffer_size,
0679 (void *)&props,
0680 sizeof(struct vmstorage_channel_properties),
0681 storvsc_on_channel_callback, new_sc);
0682
0683
0684 if (ret != 0) {
0685 dev_err(dev, "Failed to open sub-channel: err=%d\n", ret);
0686 return;
0687 }
0688
0689 new_sc->change_target_cpu_callback = storvsc_change_target_cpu;
0690
0691
0692 stor_device->stor_chns[new_sc->target_cpu] = new_sc;
0693 cpumask_set_cpu(new_sc->target_cpu, &stor_device->alloced_cpus);
0694 }
0695
0696 static void handle_multichannel_storage(struct hv_device *device, int max_chns)
0697 {
0698 struct device *dev = &device->device;
0699 struct storvsc_device *stor_device;
0700 int num_sc;
0701 struct storvsc_cmd_request *request;
0702 struct vstor_packet *vstor_packet;
0703 int ret, t;
0704
0705
0706
0707
0708
0709
0710
0711
0712 num_sc = min((int)(num_online_cpus() - 1), max_chns);
0713 if (!num_sc)
0714 return;
0715
0716 stor_device = get_out_stor_device(device);
0717 if (!stor_device)
0718 return;
0719
0720 stor_device->num_sc = num_sc;
0721 request = &stor_device->init_request;
0722 vstor_packet = &request->vstor_packet;
0723
0724
0725
0726
0727 vmbus_set_sc_create_callback(device->channel, handle_sc_creation);
0728
0729
0730
0731
0732 memset(request, 0, sizeof(struct storvsc_cmd_request));
0733 init_completion(&request->wait_event);
0734 vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
0735 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
0736 vstor_packet->sub_channel_count = num_sc;
0737
0738 ret = vmbus_sendpacket(device->channel, vstor_packet,
0739 sizeof(struct vstor_packet),
0740 VMBUS_RQST_INIT,
0741 VM_PKT_DATA_INBAND,
0742 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
0743
0744 if (ret != 0) {
0745 dev_err(dev, "Failed to create sub-channel: err=%d\n", ret);
0746 return;
0747 }
0748
0749 t = wait_for_completion_timeout(&request->wait_event, 10*HZ);
0750 if (t == 0) {
0751 dev_err(dev, "Failed to create sub-channel: timed out\n");
0752 return;
0753 }
0754
0755 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
0756 vstor_packet->status != 0) {
0757 dev_err(dev, "Failed to create sub-channel: op=%d, sts=%d\n",
0758 vstor_packet->operation, vstor_packet->status);
0759 return;
0760 }
0761
0762
0763
0764
0765
0766
0767 }
0768
0769 static void cache_wwn(struct storvsc_device *stor_device,
0770 struct vstor_packet *vstor_packet)
0771 {
0772
0773
0774
0775 if (vstor_packet->wwn_packet.primary_active) {
0776 stor_device->node_name =
0777 wwn_to_u64(vstor_packet->wwn_packet.primary_node_wwn);
0778 stor_device->port_name =
0779 wwn_to_u64(vstor_packet->wwn_packet.primary_port_wwn);
0780 } else {
0781 stor_device->node_name =
0782 wwn_to_u64(vstor_packet->wwn_packet.secondary_node_wwn);
0783 stor_device->port_name =
0784 wwn_to_u64(vstor_packet->wwn_packet.secondary_port_wwn);
0785 }
0786 }
0787
0788
0789 static int storvsc_execute_vstor_op(struct hv_device *device,
0790 struct storvsc_cmd_request *request,
0791 bool status_check)
0792 {
0793 struct storvsc_device *stor_device;
0794 struct vstor_packet *vstor_packet;
0795 int ret, t;
0796
0797 stor_device = get_out_stor_device(device);
0798 if (!stor_device)
0799 return -ENODEV;
0800
0801 vstor_packet = &request->vstor_packet;
0802
0803 init_completion(&request->wait_event);
0804 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
0805
0806 ret = vmbus_sendpacket(device->channel, vstor_packet,
0807 sizeof(struct vstor_packet),
0808 VMBUS_RQST_INIT,
0809 VM_PKT_DATA_INBAND,
0810 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
0811 if (ret != 0)
0812 return ret;
0813
0814 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
0815 if (t == 0)
0816 return -ETIMEDOUT;
0817
0818 if (!status_check)
0819 return ret;
0820
0821 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
0822 vstor_packet->status != 0)
0823 return -EINVAL;
0824
0825 return ret;
0826 }
0827
0828 static int storvsc_channel_init(struct hv_device *device, bool is_fc)
0829 {
0830 struct storvsc_device *stor_device;
0831 struct storvsc_cmd_request *request;
0832 struct vstor_packet *vstor_packet;
0833 int ret, i;
0834 int max_chns;
0835 bool process_sub_channels = false;
0836
0837 stor_device = get_out_stor_device(device);
0838 if (!stor_device)
0839 return -ENODEV;
0840
0841 request = &stor_device->init_request;
0842 vstor_packet = &request->vstor_packet;
0843
0844
0845
0846
0847
0848 memset(request, 0, sizeof(struct storvsc_cmd_request));
0849 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
0850 ret = storvsc_execute_vstor_op(device, request, true);
0851 if (ret)
0852 return ret;
0853
0854
0855
0856
0857 for (i = 0; i < ARRAY_SIZE(protocol_version); i++) {
0858
0859 memset(vstor_packet, 0, sizeof(struct vstor_packet));
0860 vstor_packet->operation =
0861 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
0862
0863 vstor_packet->version.major_minor = protocol_version[i];
0864
0865
0866
0867
0868 vstor_packet->version.revision = 0;
0869 ret = storvsc_execute_vstor_op(device, request, false);
0870 if (ret != 0)
0871 return ret;
0872
0873 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO)
0874 return -EINVAL;
0875
0876 if (vstor_packet->status == 0) {
0877 vmstor_proto_version = protocol_version[i];
0878
0879 break;
0880 }
0881 }
0882
0883 if (vstor_packet->status != 0) {
0884 dev_err(&device->device, "Obsolete Hyper-V version\n");
0885 return -EINVAL;
0886 }
0887
0888
0889 memset(vstor_packet, 0, sizeof(struct vstor_packet));
0890 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
0891 ret = storvsc_execute_vstor_op(device, request, true);
0892 if (ret != 0)
0893 return ret;
0894
0895
0896
0897
0898
0899
0900 max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
0901
0902
0903
0904
0905
0906
0907
0908
0909
0910
0911 stor_device->stor_chns = kcalloc(num_possible_cpus(), sizeof(void *),
0912 GFP_KERNEL);
0913 if (stor_device->stor_chns == NULL)
0914 return -ENOMEM;
0915
0916 device->channel->change_target_cpu_callback = storvsc_change_target_cpu;
0917
0918 stor_device->stor_chns[device->channel->target_cpu] = device->channel;
0919 cpumask_set_cpu(device->channel->target_cpu,
0920 &stor_device->alloced_cpus);
0921
0922 if (vstor_packet->storage_channel_properties.flags &
0923 STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
0924 process_sub_channels = true;
0925
0926 stor_device->max_transfer_bytes =
0927 vstor_packet->storage_channel_properties.max_transfer_bytes;
0928
0929 if (!is_fc)
0930 goto done;
0931
0932
0933
0934
0935 memset(vstor_packet, 0, sizeof(struct vstor_packet));
0936 vstor_packet->operation = VSTOR_OPERATION_FCHBA_DATA;
0937 ret = storvsc_execute_vstor_op(device, request, true);
0938 if (ret != 0)
0939 return ret;
0940
0941
0942
0943
0944 cache_wwn(stor_device, vstor_packet);
0945
0946 done:
0947
0948 memset(vstor_packet, 0, sizeof(struct vstor_packet));
0949 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
0950 ret = storvsc_execute_vstor_op(device, request, true);
0951 if (ret != 0)
0952 return ret;
0953
0954 if (process_sub_channels)
0955 handle_multichannel_storage(device, max_chns);
0956
0957 return ret;
0958 }
0959
0960 static void storvsc_handle_error(struct vmscsi_request *vm_srb,
0961 struct scsi_cmnd *scmnd,
0962 struct Scsi_Host *host,
0963 u8 asc, u8 ascq)
0964 {
0965 struct storvsc_scan_work *wrk;
0966 void (*process_err_fn)(struct work_struct *work);
0967 struct hv_host_device *host_dev = shost_priv(host);
0968
0969
0970
0971
0972
0973
0974
0975 if (vm_srb->srb_status & SRB_STATUS_INVALID_LUN) {
0976 set_host_byte(scmnd, DID_NO_CONNECT);
0977 process_err_fn = storvsc_remove_lun;
0978 goto do_work;
0979 }
0980
0981 if (vm_srb->srb_status & SRB_STATUS_ABORTED) {
0982 if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID &&
0983
0984 (asc == 0x2a) && (ascq == 0x9)) {
0985 process_err_fn = storvsc_device_scan;
0986
0987
0988
0989 set_host_byte(scmnd, DID_REQUEUE);
0990 goto do_work;
0991 }
0992 }
0993
0994 if (vm_srb->srb_status & SRB_STATUS_ERROR) {
0995
0996
0997
0998
0999 if (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID)
1000 return;
1001
1002
1003
1004
1005
1006
1007
1008 switch (scmnd->cmnd[0]) {
1009 case ATA_16:
1010 case ATA_12:
1011 set_host_byte(scmnd, DID_PASSTHROUGH);
1012 break;
1013
1014
1015
1016
1017
1018 case TEST_UNIT_READY:
1019 break;
1020 default:
1021 set_host_byte(scmnd, DID_ERROR);
1022 }
1023 }
1024 return;
1025
1026 do_work:
1027
1028
1029
1030 wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
1031 if (!wrk) {
1032 set_host_byte(scmnd, DID_TARGET_FAILURE);
1033 return;
1034 }
1035
1036 wrk->host = host;
1037 wrk->lun = vm_srb->lun;
1038 wrk->tgt_id = vm_srb->target_id;
1039 INIT_WORK(&wrk->work, process_err_fn);
1040 queue_work(host_dev->handle_error_wq, &wrk->work);
1041 }
1042
1043
1044 static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request,
1045 struct storvsc_device *stor_dev)
1046 {
1047 struct scsi_cmnd *scmnd = cmd_request->cmd;
1048 struct scsi_sense_hdr sense_hdr;
1049 struct vmscsi_request *vm_srb;
1050 u32 data_transfer_length;
1051 struct Scsi_Host *host;
1052 u32 payload_sz = cmd_request->payload_sz;
1053 void *payload = cmd_request->payload;
1054 bool sense_ok;
1055
1056 host = stor_dev->host;
1057
1058 vm_srb = &cmd_request->vstor_packet.vm_srb;
1059 data_transfer_length = vm_srb->data_transfer_length;
1060
1061 scmnd->result = vm_srb->scsi_status;
1062
1063 if (scmnd->result) {
1064 sense_ok = scsi_normalize_sense(scmnd->sense_buffer,
1065 SCSI_SENSE_BUFFERSIZE, &sense_hdr);
1066
1067 if (sense_ok && do_logging(STORVSC_LOGGING_WARN))
1068 scsi_print_sense_hdr(scmnd->device, "storvsc",
1069 &sense_hdr);
1070 }
1071
1072 if (vm_srb->srb_status != SRB_STATUS_SUCCESS) {
1073 storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
1074 sense_hdr.ascq);
1075
1076
1077
1078
1079
1080 if (vm_srb->srb_status != SRB_STATUS_DATA_OVERRUN)
1081 data_transfer_length = 0;
1082 }
1083
1084
1085 if (data_transfer_length > cmd_request->payload->range.len)
1086 data_transfer_length = cmd_request->payload->range.len;
1087
1088 scsi_set_resid(scmnd,
1089 cmd_request->payload->range.len - data_transfer_length);
1090
1091 scsi_done(scmnd);
1092
1093 if (payload_sz >
1094 sizeof(struct vmbus_channel_packet_multipage_buffer))
1095 kfree(payload);
1096 }
1097
1098 static void storvsc_on_io_completion(struct storvsc_device *stor_device,
1099 struct vstor_packet *vstor_packet,
1100 struct storvsc_cmd_request *request)
1101 {
1102 struct vstor_packet *stor_pkt;
1103 struct hv_device *device = stor_device->device;
1104
1105 stor_pkt = &request->vstor_packet;
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118 if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
1119 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
1120 vstor_packet->vm_srb.scsi_status = 0;
1121 vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
1122 }
1123
1124
1125 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
1126 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
1127
1128
1129
1130
1131
1132 stor_pkt->vm_srb.sense_info_length = min_t(u8, STORVSC_SENSE_BUFFER_SIZE,
1133 vstor_packet->vm_srb.sense_info_length);
1134
1135 if (vstor_packet->vm_srb.scsi_status != 0 ||
1136 vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS) {
1137
1138
1139
1140
1141
1142
1143 int loglevel = (stor_pkt->vm_srb.cdb[0] == TEST_UNIT_READY) ?
1144 STORVSC_LOGGING_WARN : STORVSC_LOGGING_ERROR;
1145
1146 storvsc_log(device, loglevel,
1147 "tag#%d cmd 0x%x status: scsi 0x%x srb 0x%x hv 0x%x\n",
1148 scsi_cmd_to_rq(request->cmd)->tag,
1149 stor_pkt->vm_srb.cdb[0],
1150 vstor_packet->vm_srb.scsi_status,
1151 vstor_packet->vm_srb.srb_status,
1152 vstor_packet->status);
1153 }
1154
1155 if (vstor_packet->vm_srb.scsi_status == SAM_STAT_CHECK_CONDITION &&
1156 (vstor_packet->vm_srb.srb_status & SRB_STATUS_AUTOSENSE_VALID))
1157 memcpy(request->cmd->sense_buffer,
1158 vstor_packet->vm_srb.sense_data,
1159 stor_pkt->vm_srb.sense_info_length);
1160
1161 stor_pkt->vm_srb.data_transfer_length =
1162 vstor_packet->vm_srb.data_transfer_length;
1163
1164 storvsc_command_completion(request, stor_device);
1165
1166 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
1167 stor_device->drain_notify)
1168 wake_up(&stor_device->waiting_to_drain);
1169 }
1170
1171 static void storvsc_on_receive(struct storvsc_device *stor_device,
1172 struct vstor_packet *vstor_packet,
1173 struct storvsc_cmd_request *request)
1174 {
1175 struct hv_host_device *host_dev;
1176 switch (vstor_packet->operation) {
1177 case VSTOR_OPERATION_COMPLETE_IO:
1178 storvsc_on_io_completion(stor_device, vstor_packet, request);
1179 break;
1180
1181 case VSTOR_OPERATION_REMOVE_DEVICE:
1182 case VSTOR_OPERATION_ENUMERATE_BUS:
1183 host_dev = shost_priv(stor_device->host);
1184 queue_work(
1185 host_dev->handle_error_wq, &host_dev->host_scan_work);
1186 break;
1187
1188 case VSTOR_OPERATION_FCHBA_DATA:
1189 cache_wwn(stor_device, vstor_packet);
1190 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1191 fc_host_node_name(stor_device->host) = stor_device->node_name;
1192 fc_host_port_name(stor_device->host) = stor_device->port_name;
1193 #endif
1194 break;
1195 default:
1196 break;
1197 }
1198 }
1199
1200 static void storvsc_on_channel_callback(void *context)
1201 {
1202 struct vmbus_channel *channel = (struct vmbus_channel *)context;
1203 const struct vmpacket_descriptor *desc;
1204 struct hv_device *device;
1205 struct storvsc_device *stor_device;
1206 struct Scsi_Host *shost;
1207
1208 if (channel->primary_channel != NULL)
1209 device = channel->primary_channel->device_obj;
1210 else
1211 device = channel->device_obj;
1212
1213 stor_device = get_in_stor_device(device);
1214 if (!stor_device)
1215 return;
1216
1217 shost = stor_device->host;
1218
1219 foreach_vmbus_pkt(desc, channel) {
1220 struct vstor_packet *packet = hv_pkt_data(desc);
1221 struct storvsc_cmd_request *request = NULL;
1222 u32 pktlen = hv_pkt_datalen(desc);
1223 u64 rqst_id = desc->trans_id;
1224 u32 minlen = rqst_id ? sizeof(struct vstor_packet) :
1225 sizeof(enum vstor_packet_operation);
1226
1227 if (pktlen < minlen) {
1228 dev_err(&device->device,
1229 "Invalid pkt: id=%llu, len=%u, minlen=%u\n",
1230 rqst_id, pktlen, minlen);
1231 continue;
1232 }
1233
1234 if (rqst_id == VMBUS_RQST_INIT) {
1235 request = &stor_device->init_request;
1236 } else if (rqst_id == VMBUS_RQST_RESET) {
1237 request = &stor_device->reset_request;
1238 } else {
1239
1240 if (rqst_id == 0) {
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258 if (packet->operation == VSTOR_OPERATION_COMPLETE_IO ||
1259 packet->operation == VSTOR_OPERATION_FCHBA_DATA) {
1260 dev_err(&device->device, "Invalid packet with ID of 0\n");
1261 continue;
1262 }
1263 } else {
1264 struct scsi_cmnd *scmnd;
1265
1266
1267 scmnd = scsi_host_find_tag(shost, rqst_id - 1);
1268 if (scmnd == NULL) {
1269 dev_err(&device->device, "Incorrect transaction ID\n");
1270 continue;
1271 }
1272 request = (struct storvsc_cmd_request *)scsi_cmd_priv(scmnd);
1273 scsi_dma_unmap(scmnd);
1274 }
1275
1276 storvsc_on_receive(stor_device, packet, request);
1277 continue;
1278 }
1279
1280 memcpy(&request->vstor_packet, packet,
1281 sizeof(struct vstor_packet));
1282 complete(&request->wait_event);
1283 }
1284 }
1285
1286 static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
1287 bool is_fc)
1288 {
1289 struct vmstorage_channel_properties props;
1290 int ret;
1291
1292 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
1293
1294 device->channel->max_pkt_size = STORVSC_MAX_PKT_SIZE;
1295 device->channel->next_request_id_callback = storvsc_next_request_id;
1296
1297 ret = vmbus_open(device->channel,
1298 ring_size,
1299 ring_size,
1300 (void *)&props,
1301 sizeof(struct vmstorage_channel_properties),
1302 storvsc_on_channel_callback, device->channel);
1303
1304 if (ret != 0)
1305 return ret;
1306
1307 ret = storvsc_channel_init(device, is_fc);
1308
1309 return ret;
1310 }
1311
1312 static int storvsc_dev_remove(struct hv_device *device)
1313 {
1314 struct storvsc_device *stor_device;
1315
1316 stor_device = hv_get_drvdata(device);
1317
1318 stor_device->destroy = true;
1319
1320
1321 wmb();
1322
1323
1324
1325
1326
1327
1328
1329 storvsc_wait_to_drain(stor_device);
1330
1331
1332
1333
1334
1335
1336
1337
1338 hv_set_drvdata(device, NULL);
1339
1340
1341 vmbus_close(device->channel);
1342
1343 kfree(stor_device->stor_chns);
1344 kfree(stor_device);
1345 return 0;
1346 }
1347
1348 static struct vmbus_channel *get_og_chn(struct storvsc_device *stor_device,
1349 u16 q_num)
1350 {
1351 u16 slot = 0;
1352 u16 hash_qnum;
1353 const struct cpumask *node_mask;
1354 int num_channels, tgt_cpu;
1355
1356 if (stor_device->num_sc == 0) {
1357 stor_device->stor_chns[q_num] = stor_device->device->channel;
1358 return stor_device->device->channel;
1359 }
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370 node_mask = cpumask_of_node(cpu_to_node(q_num));
1371
1372 num_channels = 0;
1373 for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
1374 if (cpumask_test_cpu(tgt_cpu, node_mask))
1375 num_channels++;
1376 }
1377 if (num_channels == 0) {
1378 stor_device->stor_chns[q_num] = stor_device->device->channel;
1379 return stor_device->device->channel;
1380 }
1381
1382 hash_qnum = q_num;
1383 while (hash_qnum >= num_channels)
1384 hash_qnum -= num_channels;
1385
1386 for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
1387 if (!cpumask_test_cpu(tgt_cpu, node_mask))
1388 continue;
1389 if (slot == hash_qnum)
1390 break;
1391 slot++;
1392 }
1393
1394 stor_device->stor_chns[q_num] = stor_device->stor_chns[tgt_cpu];
1395
1396 return stor_device->stor_chns[q_num];
1397 }
1398
1399
1400 static int storvsc_do_io(struct hv_device *device,
1401 struct storvsc_cmd_request *request, u16 q_num)
1402 {
1403 struct storvsc_device *stor_device;
1404 struct vstor_packet *vstor_packet;
1405 struct vmbus_channel *outgoing_channel, *channel;
1406 unsigned long flags;
1407 int ret = 0;
1408 const struct cpumask *node_mask;
1409 int tgt_cpu;
1410
1411 vstor_packet = &request->vstor_packet;
1412 stor_device = get_out_stor_device(device);
1413
1414 if (!stor_device)
1415 return -ENODEV;
1416
1417
1418 request->device = device;
1419
1420
1421
1422
1423 outgoing_channel = READ_ONCE(stor_device->stor_chns[q_num]);
1424 if (outgoing_channel != NULL) {
1425 if (outgoing_channel->target_cpu == q_num) {
1426
1427
1428
1429
1430 node_mask = cpumask_of_node(cpu_to_node(q_num));
1431 for_each_cpu_wrap(tgt_cpu,
1432 &stor_device->alloced_cpus, q_num + 1) {
1433 if (!cpumask_test_cpu(tgt_cpu, node_mask))
1434 continue;
1435 if (tgt_cpu == q_num)
1436 continue;
1437 channel = READ_ONCE(
1438 stor_device->stor_chns[tgt_cpu]);
1439 if (channel == NULL)
1440 continue;
1441 if (hv_get_avail_to_write_percent(
1442 &channel->outbound)
1443 > ring_avail_percent_lowater) {
1444 outgoing_channel = channel;
1445 goto found_channel;
1446 }
1447 }
1448
1449
1450
1451
1452
1453 if (hv_get_avail_to_write_percent(
1454 &outgoing_channel->outbound)
1455 > ring_avail_percent_lowater)
1456 goto found_channel;
1457
1458
1459
1460
1461
1462
1463 for_each_cpu(tgt_cpu, &stor_device->alloced_cpus) {
1464 if (cpumask_test_cpu(tgt_cpu, node_mask))
1465 continue;
1466 channel = READ_ONCE(
1467 stor_device->stor_chns[tgt_cpu]);
1468 if (channel == NULL)
1469 continue;
1470 if (hv_get_avail_to_write_percent(
1471 &channel->outbound)
1472 > ring_avail_percent_lowater) {
1473 outgoing_channel = channel;
1474 goto found_channel;
1475 }
1476 }
1477 }
1478 } else {
1479 spin_lock_irqsave(&stor_device->lock, flags);
1480 outgoing_channel = stor_device->stor_chns[q_num];
1481 if (outgoing_channel != NULL) {
1482 spin_unlock_irqrestore(&stor_device->lock, flags);
1483 goto found_channel;
1484 }
1485 outgoing_channel = get_og_chn(stor_device, q_num);
1486 spin_unlock_irqrestore(&stor_device->lock, flags);
1487 }
1488
1489 found_channel:
1490 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
1491
1492 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
1493
1494
1495 vstor_packet->vm_srb.sense_info_length = STORVSC_SENSE_BUFFER_SIZE;
1496
1497
1498 vstor_packet->vm_srb.data_transfer_length =
1499 request->payload->range.len;
1500
1501 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
1502
1503 if (request->payload->range.len) {
1504
1505 ret = vmbus_sendpacket_mpb_desc(outgoing_channel,
1506 request->payload, request->payload_sz,
1507 vstor_packet,
1508 sizeof(struct vstor_packet),
1509 (unsigned long)request);
1510 } else {
1511 ret = vmbus_sendpacket(outgoing_channel, vstor_packet,
1512 sizeof(struct vstor_packet),
1513 (unsigned long)request,
1514 VM_PKT_DATA_INBAND,
1515 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1516 }
1517
1518 if (ret != 0)
1519 return ret;
1520
1521 atomic_inc(&stor_device->num_outstanding_req);
1522
1523 return ret;
1524 }
1525
1526 static int storvsc_device_alloc(struct scsi_device *sdevice)
1527 {
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537 sdevice->sdev_bflags = BLIST_REPORTLUN2 | BLIST_TRY_VPD_PAGES;
1538
1539 return 0;
1540 }
1541
1542 static int storvsc_device_configure(struct scsi_device *sdevice)
1543 {
1544 blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
1545
1546 sdevice->no_write_same = 1;
1547
1548
1549
1550
1551
1552
1553 if (!strncmp(sdevice->vendor, "Msft", 4)) {
1554 switch (vmstor_proto_version) {
1555 case VMSTOR_PROTO_VERSION_WIN8:
1556 case VMSTOR_PROTO_VERSION_WIN8_1:
1557 sdevice->scsi_level = SCSI_SPC_3;
1558 break;
1559 }
1560
1561 if (vmstor_proto_version >= VMSTOR_PROTO_VERSION_WIN10)
1562 sdevice->no_write_same = 0;
1563 }
1564
1565 return 0;
1566 }
1567
1568 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1569 sector_t capacity, int *info)
1570 {
1571 sector_t nsect = capacity;
1572 sector_t cylinders = nsect;
1573 int heads, sectors_pt;
1574
1575
1576
1577
1578 heads = 0xff;
1579 sectors_pt = 0x3f;
1580 sector_div(cylinders, heads * sectors_pt);
1581 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1582 cylinders = 0xffff;
1583
1584 info[0] = heads;
1585 info[1] = sectors_pt;
1586 info[2] = (int)cylinders;
1587
1588 return 0;
1589 }
1590
1591 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
1592 {
1593 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1594 struct hv_device *device = host_dev->dev;
1595
1596 struct storvsc_device *stor_device;
1597 struct storvsc_cmd_request *request;
1598 struct vstor_packet *vstor_packet;
1599 int ret, t;
1600
1601 stor_device = get_out_stor_device(device);
1602 if (!stor_device)
1603 return FAILED;
1604
1605 request = &stor_device->reset_request;
1606 vstor_packet = &request->vstor_packet;
1607 memset(vstor_packet, 0, sizeof(struct vstor_packet));
1608
1609 init_completion(&request->wait_event);
1610
1611 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1612 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1613 vstor_packet->vm_srb.path_id = stor_device->path_id;
1614
1615 ret = vmbus_sendpacket(device->channel, vstor_packet,
1616 sizeof(struct vstor_packet),
1617 VMBUS_RQST_RESET,
1618 VM_PKT_DATA_INBAND,
1619 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1620 if (ret != 0)
1621 return FAILED;
1622
1623 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
1624 if (t == 0)
1625 return TIMEOUT_ERROR;
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636 storvsc_wait_to_drain(stor_device);
1637
1638 return SUCCESS;
1639 }
1640
1641
1642
1643
1644
1645
1646 static enum blk_eh_timer_return storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
1647 {
1648 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1649 if (scmnd->device->host->transportt == fc_transport_template)
1650 return fc_eh_timed_out(scmnd);
1651 #endif
1652 return BLK_EH_RESET_TIMER;
1653 }
1654
1655 static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
1656 {
1657 bool allowed = true;
1658 u8 scsi_op = scmnd->cmnd[0];
1659
1660 switch (scsi_op) {
1661
1662 case WRITE_SAME:
1663
1664
1665
1666
1667 case SET_WINDOW:
1668 set_host_byte(scmnd, DID_ERROR);
1669 allowed = false;
1670 break;
1671 default:
1672 break;
1673 }
1674 return allowed;
1675 }
1676
1677 static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
1678 {
1679 int ret;
1680 struct hv_host_device *host_dev = shost_priv(host);
1681 struct hv_device *dev = host_dev->dev;
1682 struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd);
1683 struct scatterlist *sgl;
1684 struct vmscsi_request *vm_srb;
1685 struct vmbus_packet_mpb_array *payload;
1686 u32 payload_sz;
1687 u32 length;
1688
1689 if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) {
1690
1691
1692
1693
1694
1695
1696
1697
1698 if (!storvsc_scsi_cmd_ok(scmnd)) {
1699 scsi_done(scmnd);
1700 return 0;
1701 }
1702 }
1703
1704
1705 cmd_request->cmd = scmnd;
1706
1707 memset(&cmd_request->vstor_packet, 0, sizeof(struct vstor_packet));
1708 vm_srb = &cmd_request->vstor_packet.vm_srb;
1709 vm_srb->time_out_value = 60;
1710
1711 vm_srb->srb_flags |=
1712 SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
1713
1714 if (scmnd->device->tagged_supported) {
1715 vm_srb->srb_flags |=
1716 (SRB_FLAGS_QUEUE_ACTION_ENABLE | SRB_FLAGS_NO_QUEUE_FREEZE);
1717 vm_srb->queue_tag = SP_UNTAGGED;
1718 vm_srb->queue_action = SRB_SIMPLE_TAG_REQUEST;
1719 }
1720
1721
1722 switch (scmnd->sc_data_direction) {
1723 case DMA_TO_DEVICE:
1724 vm_srb->data_in = WRITE_TYPE;
1725 vm_srb->srb_flags |= SRB_FLAGS_DATA_OUT;
1726 break;
1727 case DMA_FROM_DEVICE:
1728 vm_srb->data_in = READ_TYPE;
1729 vm_srb->srb_flags |= SRB_FLAGS_DATA_IN;
1730 break;
1731 case DMA_NONE:
1732 vm_srb->data_in = UNKNOWN_TYPE;
1733 vm_srb->srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER;
1734 break;
1735 default:
1736
1737
1738
1739
1740 WARN(1, "Unexpected data direction: %d\n",
1741 scmnd->sc_data_direction);
1742 return -EINVAL;
1743 }
1744
1745
1746 vm_srb->port_number = host_dev->port;
1747 vm_srb->path_id = scmnd->device->channel;
1748 vm_srb->target_id = scmnd->device->id;
1749 vm_srb->lun = scmnd->device->lun;
1750
1751 vm_srb->cdb_length = scmnd->cmd_len;
1752
1753 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1754
1755 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1756
1757 length = scsi_bufflen(scmnd);
1758 payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
1759 payload_sz = sizeof(cmd_request->mpb);
1760
1761 if (scsi_sg_count(scmnd)) {
1762 unsigned long offset_in_hvpg = offset_in_hvpage(sgl->offset);
1763 unsigned int hvpg_count = HVPFN_UP(offset_in_hvpg + length);
1764 struct scatterlist *sg;
1765 unsigned long hvpfn, hvpfns_to_add;
1766 int j, i = 0, sg_count;
1767
1768 if (hvpg_count > MAX_PAGE_BUFFER_COUNT) {
1769
1770 payload_sz = (hvpg_count * sizeof(u64) +
1771 sizeof(struct vmbus_packet_mpb_array));
1772 payload = kzalloc(payload_sz, GFP_ATOMIC);
1773 if (!payload)
1774 return SCSI_MLQUEUE_DEVICE_BUSY;
1775 }
1776
1777 payload->range.len = length;
1778 payload->range.offset = offset_in_hvpg;
1779
1780 sg_count = scsi_dma_map(scmnd);
1781 if (sg_count < 0) {
1782 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1783 goto err_free_payload;
1784 }
1785
1786 for_each_sg(sgl, sg, sg_count, j) {
1787
1788
1789
1790
1791
1792
1793
1794
1795 hvpfn = HVPFN_DOWN(sg_dma_address(sg));
1796 hvpfns_to_add = HVPFN_UP(sg_dma_address(sg) +
1797 sg_dma_len(sg)) - hvpfn;
1798
1799
1800
1801
1802
1803
1804
1805
1806 while (hvpfns_to_add--)
1807 payload->range.pfn_array[i++] = hvpfn++;
1808 }
1809 }
1810
1811 cmd_request->payload = payload;
1812 cmd_request->payload_sz = payload_sz;
1813
1814
1815 ret = storvsc_do_io(dev, cmd_request, get_cpu());
1816 put_cpu();
1817
1818 if (ret == -EAGAIN) {
1819
1820 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1821 goto err_free_payload;
1822 }
1823
1824 return 0;
1825
1826 err_free_payload:
1827 if (payload_sz > sizeof(cmd_request->mpb))
1828 kfree(payload);
1829
1830 return ret;
1831 }
1832
1833 static struct scsi_host_template scsi_driver = {
1834 .module = THIS_MODULE,
1835 .name = "storvsc_host_t",
1836 .cmd_size = sizeof(struct storvsc_cmd_request),
1837 .bios_param = storvsc_get_chs,
1838 .queuecommand = storvsc_queuecommand,
1839 .eh_host_reset_handler = storvsc_host_reset_handler,
1840 .proc_name = "storvsc_host",
1841 .eh_timed_out = storvsc_eh_timed_out,
1842 .slave_alloc = storvsc_device_alloc,
1843 .slave_configure = storvsc_device_configure,
1844 .cmd_per_lun = 2048,
1845 .this_id = -1,
1846
1847 .virt_boundary_mask = HV_HYP_PAGE_SIZE - 1,
1848 .no_write_same = 1,
1849 .track_queue_depth = 1,
1850 .change_queue_depth = storvsc_change_queue_depth,
1851 };
1852
1853 enum {
1854 SCSI_GUID,
1855 IDE_GUID,
1856 SFC_GUID,
1857 };
1858
1859 static const struct hv_vmbus_device_id id_table[] = {
1860
1861 { HV_SCSI_GUID,
1862 .driver_data = SCSI_GUID
1863 },
1864
1865 { HV_IDE_GUID,
1866 .driver_data = IDE_GUID
1867 },
1868
1869 {
1870 HV_SYNTHFC_GUID,
1871 .driver_data = SFC_GUID
1872 },
1873 { },
1874 };
1875
1876 MODULE_DEVICE_TABLE(vmbus, id_table);
1877
1878 static const struct { guid_t guid; } fc_guid = { HV_SYNTHFC_GUID };
1879
1880 static bool hv_dev_is_fc(struct hv_device *hv_dev)
1881 {
1882 return guid_equal(&fc_guid.guid, &hv_dev->dev_type);
1883 }
1884
1885 static int storvsc_probe(struct hv_device *device,
1886 const struct hv_vmbus_device_id *dev_id)
1887 {
1888 int ret;
1889 int num_cpus = num_online_cpus();
1890 int num_present_cpus = num_present_cpus();
1891 struct Scsi_Host *host;
1892 struct hv_host_device *host_dev;
1893 bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
1894 bool is_fc = ((dev_id->driver_data == SFC_GUID) ? true : false);
1895 int target = 0;
1896 struct storvsc_device *stor_device;
1897 int max_sub_channels = 0;
1898 u32 max_xfer_bytes;
1899
1900
1901
1902
1903
1904
1905 if (!dev_is_ide)
1906 max_sub_channels =
1907 (num_cpus - 1) / storvsc_vcpus_per_sub_channel;
1908
1909 scsi_driver.can_queue = max_outstanding_req_per_channel *
1910 (max_sub_channels + 1) *
1911 (100 - ring_avail_percent_lowater) / 100;
1912
1913 host = scsi_host_alloc(&scsi_driver,
1914 sizeof(struct hv_host_device));
1915 if (!host)
1916 return -ENOMEM;
1917
1918 host_dev = shost_priv(host);
1919 memset(host_dev, 0, sizeof(struct hv_host_device));
1920
1921 host_dev->port = host->host_no;
1922 host_dev->dev = device;
1923 host_dev->host = host;
1924
1925
1926 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
1927 if (!stor_device) {
1928 ret = -ENOMEM;
1929 goto err_out0;
1930 }
1931
1932 stor_device->destroy = false;
1933 init_waitqueue_head(&stor_device->waiting_to_drain);
1934 stor_device->device = device;
1935 stor_device->host = host;
1936 spin_lock_init(&stor_device->lock);
1937 hv_set_drvdata(device, stor_device);
1938 dma_set_min_align_mask(&device->device, HV_HYP_PAGE_SIZE - 1);
1939
1940 stor_device->port_number = host->host_no;
1941 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size, is_fc);
1942 if (ret)
1943 goto err_out1;
1944
1945 host_dev->path = stor_device->path_id;
1946 host_dev->target = stor_device->target_id;
1947
1948 switch (dev_id->driver_data) {
1949 case SFC_GUID:
1950 host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
1951 host->max_id = STORVSC_FC_MAX_TARGETS;
1952 host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
1953 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
1954 host->transportt = fc_transport_template;
1955 #endif
1956 break;
1957
1958 case SCSI_GUID:
1959 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
1960 host->max_id = STORVSC_MAX_TARGETS;
1961 host->max_channel = STORVSC_MAX_CHANNELS - 1;
1962 break;
1963
1964 default:
1965 host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
1966 host->max_id = STORVSC_IDE_MAX_TARGETS;
1967 host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
1968 break;
1969 }
1970
1971 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
1972
1973
1974
1975
1976
1977 max_xfer_bytes = round_down(stor_device->max_transfer_bytes, HV_HYP_PAGE_SIZE);
1978
1979 host->max_sectors = max_xfer_bytes >> 9;
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993 host->sg_tablesize = (max_xfer_bytes >> HV_HYP_PAGE_SHIFT) + 1;
1994
1995
1996
1997
1998 if (!dev_is_ide) {
1999 if (storvsc_max_hw_queues > num_present_cpus) {
2000 storvsc_max_hw_queues = 0;
2001 storvsc_log(device, STORVSC_LOGGING_WARN,
2002 "Resetting invalid storvsc_max_hw_queues value to default.\n");
2003 }
2004 if (storvsc_max_hw_queues)
2005 host->nr_hw_queues = storvsc_max_hw_queues;
2006 else
2007 host->nr_hw_queues = num_present_cpus;
2008 }
2009
2010
2011
2012
2013 host_dev->handle_error_wq =
2014 alloc_ordered_workqueue("storvsc_error_wq_%d",
2015 0,
2016 host->host_no);
2017 if (!host_dev->handle_error_wq) {
2018 ret = -ENOMEM;
2019 goto err_out2;
2020 }
2021 INIT_WORK(&host_dev->host_scan_work, storvsc_host_scan);
2022
2023 ret = scsi_add_host(host, &device->device);
2024 if (ret != 0)
2025 goto err_out3;
2026
2027 if (!dev_is_ide) {
2028 scsi_scan_host(host);
2029 } else {
2030 target = (device->dev_instance.b[5] << 8 |
2031 device->dev_instance.b[4]);
2032 ret = scsi_add_device(host, 0, target, 0);
2033 if (ret)
2034 goto err_out4;
2035 }
2036 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2037 if (host->transportt == fc_transport_template) {
2038 struct fc_rport_identifiers ids = {
2039 .roles = FC_PORT_ROLE_FCP_DUMMY_INITIATOR,
2040 };
2041
2042 fc_host_node_name(host) = stor_device->node_name;
2043 fc_host_port_name(host) = stor_device->port_name;
2044 stor_device->rport = fc_remote_port_add(host, 0, &ids);
2045 if (!stor_device->rport) {
2046 ret = -ENOMEM;
2047 goto err_out4;
2048 }
2049 }
2050 #endif
2051 return 0;
2052
2053 err_out4:
2054 scsi_remove_host(host);
2055
2056 err_out3:
2057 destroy_workqueue(host_dev->handle_error_wq);
2058
2059 err_out2:
2060
2061
2062
2063
2064
2065
2066 storvsc_dev_remove(device);
2067 goto err_out0;
2068
2069 err_out1:
2070 kfree(stor_device->stor_chns);
2071 kfree(stor_device);
2072
2073 err_out0:
2074 scsi_host_put(host);
2075 return ret;
2076 }
2077
2078
2079 static int storvsc_change_queue_depth(struct scsi_device *sdev, int queue_depth)
2080 {
2081 if (queue_depth > scsi_driver.can_queue)
2082 queue_depth = scsi_driver.can_queue;
2083
2084 return scsi_change_queue_depth(sdev, queue_depth);
2085 }
2086
2087 static int storvsc_remove(struct hv_device *dev)
2088 {
2089 struct storvsc_device *stor_device = hv_get_drvdata(dev);
2090 struct Scsi_Host *host = stor_device->host;
2091 struct hv_host_device *host_dev = shost_priv(host);
2092
2093 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2094 if (host->transportt == fc_transport_template) {
2095 fc_remote_port_delete(stor_device->rport);
2096 fc_remove_host(host);
2097 }
2098 #endif
2099 destroy_workqueue(host_dev->handle_error_wq);
2100 scsi_remove_host(host);
2101 storvsc_dev_remove(dev);
2102 scsi_host_put(host);
2103
2104 return 0;
2105 }
2106
2107 static int storvsc_suspend(struct hv_device *hv_dev)
2108 {
2109 struct storvsc_device *stor_device = hv_get_drvdata(hv_dev);
2110 struct Scsi_Host *host = stor_device->host;
2111 struct hv_host_device *host_dev = shost_priv(host);
2112
2113 storvsc_wait_to_drain(stor_device);
2114
2115 drain_workqueue(host_dev->handle_error_wq);
2116
2117 vmbus_close(hv_dev->channel);
2118
2119 kfree(stor_device->stor_chns);
2120 stor_device->stor_chns = NULL;
2121
2122 cpumask_clear(&stor_device->alloced_cpus);
2123
2124 return 0;
2125 }
2126
2127 static int storvsc_resume(struct hv_device *hv_dev)
2128 {
2129 int ret;
2130
2131 ret = storvsc_connect_to_vsp(hv_dev, storvsc_ringbuffer_size,
2132 hv_dev_is_fc(hv_dev));
2133 return ret;
2134 }
2135
2136 static struct hv_driver storvsc_drv = {
2137 .name = KBUILD_MODNAME,
2138 .id_table = id_table,
2139 .probe = storvsc_probe,
2140 .remove = storvsc_remove,
2141 .suspend = storvsc_suspend,
2142 .resume = storvsc_resume,
2143 .driver = {
2144 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
2145 },
2146 };
2147
2148 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2149 static struct fc_function_template fc_transport_functions = {
2150 .show_host_node_name = 1,
2151 .show_host_port_name = 1,
2152 };
2153 #endif
2154
2155 static int __init storvsc_drv_init(void)
2156 {
2157 int ret;
2158
2159
2160
2161
2162
2163
2164
2165 max_outstanding_req_per_channel =
2166 ((storvsc_ringbuffer_size - PAGE_SIZE) /
2167 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
2168 sizeof(struct vstor_packet) + sizeof(u64),
2169 sizeof(u64)));
2170
2171 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2172 fc_transport_template = fc_attach_transport(&fc_transport_functions);
2173 if (!fc_transport_template)
2174 return -ENODEV;
2175 #endif
2176
2177 ret = vmbus_driver_register(&storvsc_drv);
2178
2179 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2180 if (ret)
2181 fc_release_transport(fc_transport_template);
2182 #endif
2183
2184 return ret;
2185 }
2186
2187 static void __exit storvsc_drv_exit(void)
2188 {
2189 vmbus_driver_unregister(&storvsc_drv);
2190 #if IS_ENABLED(CONFIG_SCSI_FC_ATTRS)
2191 fc_release_transport(fc_transport_template);
2192 #endif
2193 }
2194
2195 MODULE_LICENSE("GPL");
2196 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
2197 module_init(storvsc_drv_init);
2198 module_exit(storvsc_drv_exit);