PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/scsi/storvsc_drv.c

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