smartpqi_sis.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * driver for Microchip PQI-based storage controllers
  4. * Copyright (c) 2019-2022 Microchip Technology Inc. and its subsidiaries
  5. * Copyright (c) 2016-2018 Microsemi Corporation
  6. * Copyright (c) 2016 PMC-Sierra, Inc.
  7. *
  8. * Questions/Comments/Bugfixes to [email protected]
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/delay.h>
  14. #include <linux/pci.h>
  15. #include <scsi/scsi_device.h>
  16. #include <asm/unaligned.h>
  17. #include "smartpqi.h"
  18. #include "smartpqi_sis.h"
  19. /* legacy SIS interface commands */
  20. #define SIS_CMD_GET_ADAPTER_PROPERTIES 0x19
  21. #define SIS_CMD_INIT_BASE_STRUCT_ADDRESS 0x1b
  22. #define SIS_CMD_GET_PQI_CAPABILITIES 0x3000
  23. /* for submission of legacy SIS commands */
  24. #define SIS_REENABLE_SIS_MODE 0x1
  25. #define SIS_ENABLE_MSIX 0x40
  26. #define SIS_ENABLE_INTX 0x80
  27. #define SIS_SOFT_RESET 0x100
  28. #define SIS_CMD_READY 0x200
  29. #define SIS_TRIGGER_SHUTDOWN 0x800000
  30. #define SIS_PQI_RESET_QUIESCE 0x1000000
  31. #define SIS_CMD_COMPLETE 0x1000
  32. #define SIS_CLEAR_CTRL_TO_HOST_DOORBELL 0x1000
  33. #define SIS_CMD_STATUS_SUCCESS 0x1
  34. #define SIS_CMD_COMPLETE_TIMEOUT_SECS 30
  35. #define SIS_CMD_COMPLETE_POLL_INTERVAL_MSECS 10
  36. /* used with SIS_CMD_GET_ADAPTER_PROPERTIES command */
  37. #define SIS_EXTENDED_PROPERTIES_SUPPORTED 0x800000
  38. #define SIS_SMARTARRAY_FEATURES_SUPPORTED 0x2
  39. #define SIS_PQI_MODE_SUPPORTED 0x4
  40. #define SIS_PQI_RESET_QUIESCE_SUPPORTED 0x8
  41. #define SIS_REQUIRED_EXTENDED_PROPERTIES \
  42. (SIS_SMARTARRAY_FEATURES_SUPPORTED | SIS_PQI_MODE_SUPPORTED)
  43. /* used with SIS_CMD_INIT_BASE_STRUCT_ADDRESS command */
  44. #define SIS_BASE_STRUCT_REVISION 9
  45. #define SIS_BASE_STRUCT_ALIGNMENT 16
  46. #define SIS_CTRL_KERNEL_FW_TRIAGE 0x3
  47. #define SIS_CTRL_KERNEL_UP 0x80
  48. #define SIS_CTRL_KERNEL_PANIC 0x100
  49. #define SIS_CTRL_READY_TIMEOUT_SECS 180
  50. #define SIS_CTRL_READY_RESUME_TIMEOUT_SECS 90
  51. #define SIS_CTRL_READY_POLL_INTERVAL_MSECS 10
  52. enum sis_fw_triage_status {
  53. FW_TRIAGE_NOT_STARTED = 0,
  54. FW_TRIAGE_STARTED,
  55. FW_TRIAGE_COND_INVALID,
  56. FW_TRIAGE_COMPLETED
  57. };
  58. #pragma pack(1)
  59. /* for use with SIS_CMD_INIT_BASE_STRUCT_ADDRESS command */
  60. struct sis_base_struct {
  61. __le32 revision; /* revision of this structure */
  62. __le32 flags; /* reserved */
  63. __le32 error_buffer_paddr_low; /* lower 32 bits of physical memory */
  64. /* buffer for PQI error response */
  65. /* data */
  66. __le32 error_buffer_paddr_high; /* upper 32 bits of physical */
  67. /* memory buffer for PQI */
  68. /* error response data */
  69. __le32 error_buffer_element_length; /* length of each PQI error */
  70. /* response buffer element */
  71. /* in bytes */
  72. __le32 error_buffer_num_elements; /* total number of PQI error */
  73. /* response buffers available */
  74. };
  75. #pragma pack()
  76. unsigned int sis_ctrl_ready_timeout_secs = SIS_CTRL_READY_TIMEOUT_SECS;
  77. static int sis_wait_for_ctrl_ready_with_timeout(struct pqi_ctrl_info *ctrl_info,
  78. unsigned int timeout_secs)
  79. {
  80. unsigned long timeout;
  81. u32 status;
  82. timeout = (timeout_secs * HZ) + jiffies;
  83. while (1) {
  84. status = readl(&ctrl_info->registers->sis_firmware_status);
  85. if (status != ~0) {
  86. if (status & SIS_CTRL_KERNEL_PANIC) {
  87. dev_err(&ctrl_info->pci_dev->dev,
  88. "controller is offline: status code 0x%x\n",
  89. readl(
  90. &ctrl_info->registers->sis_mailbox[7]));
  91. return -ENODEV;
  92. }
  93. if (status & SIS_CTRL_KERNEL_UP)
  94. break;
  95. }
  96. if (time_after(jiffies, timeout)) {
  97. dev_err(&ctrl_info->pci_dev->dev,
  98. "controller not ready after %u seconds\n",
  99. timeout_secs);
  100. return -ETIMEDOUT;
  101. }
  102. msleep(SIS_CTRL_READY_POLL_INTERVAL_MSECS);
  103. }
  104. return 0;
  105. }
  106. int sis_wait_for_ctrl_ready(struct pqi_ctrl_info *ctrl_info)
  107. {
  108. return sis_wait_for_ctrl_ready_with_timeout(ctrl_info,
  109. sis_ctrl_ready_timeout_secs);
  110. }
  111. int sis_wait_for_ctrl_ready_resume(struct pqi_ctrl_info *ctrl_info)
  112. {
  113. return sis_wait_for_ctrl_ready_with_timeout(ctrl_info,
  114. SIS_CTRL_READY_RESUME_TIMEOUT_SECS);
  115. }
  116. bool sis_is_firmware_running(struct pqi_ctrl_info *ctrl_info)
  117. {
  118. bool running;
  119. u32 status;
  120. status = readl(&ctrl_info->registers->sis_firmware_status);
  121. if (status != ~0 && (status & SIS_CTRL_KERNEL_PANIC))
  122. running = false;
  123. else
  124. running = true;
  125. if (!running)
  126. dev_err(&ctrl_info->pci_dev->dev,
  127. "controller is offline: status code 0x%x\n",
  128. readl(&ctrl_info->registers->sis_mailbox[7]));
  129. return running;
  130. }
  131. bool sis_is_kernel_up(struct pqi_ctrl_info *ctrl_info)
  132. {
  133. return readl(&ctrl_info->registers->sis_firmware_status) &
  134. SIS_CTRL_KERNEL_UP;
  135. }
  136. u32 sis_get_product_id(struct pqi_ctrl_info *ctrl_info)
  137. {
  138. return readl(&ctrl_info->registers->sis_product_identifier);
  139. }
  140. /* used for passing command parameters/results when issuing SIS commands */
  141. struct sis_sync_cmd_params {
  142. u32 mailbox[6]; /* mailboxes 0-5 */
  143. };
  144. static int sis_send_sync_cmd(struct pqi_ctrl_info *ctrl_info,
  145. u32 cmd, struct sis_sync_cmd_params *params)
  146. {
  147. struct pqi_ctrl_registers __iomem *registers;
  148. unsigned int i;
  149. unsigned long timeout;
  150. u32 doorbell;
  151. u32 cmd_status;
  152. registers = ctrl_info->registers;
  153. /* Write the command to mailbox 0. */
  154. writel(cmd, &registers->sis_mailbox[0]);
  155. /*
  156. * Write the command parameters to mailboxes 1-4 (mailbox 5 is not used
  157. * when sending a command to the controller).
  158. */
  159. for (i = 1; i <= 4; i++)
  160. writel(params->mailbox[i], &registers->sis_mailbox[i]);
  161. /* Clear the command doorbell. */
  162. writel(SIS_CLEAR_CTRL_TO_HOST_DOORBELL,
  163. &registers->sis_ctrl_to_host_doorbell_clear);
  164. /* Disable doorbell interrupts by masking all interrupts. */
  165. writel(~0, &registers->sis_interrupt_mask);
  166. usleep_range(1000, 2000);
  167. /*
  168. * Force the completion of the interrupt mask register write before
  169. * submitting the command.
  170. */
  171. readl(&registers->sis_interrupt_mask);
  172. /* Submit the command to the controller. */
  173. writel(SIS_CMD_READY, &registers->sis_host_to_ctrl_doorbell);
  174. /*
  175. * Poll for command completion. Note that the call to msleep() is at
  176. * the top of the loop in order to give the controller time to start
  177. * processing the command before we start polling.
  178. */
  179. timeout = (SIS_CMD_COMPLETE_TIMEOUT_SECS * HZ) + jiffies;
  180. while (1) {
  181. msleep(SIS_CMD_COMPLETE_POLL_INTERVAL_MSECS);
  182. doorbell = readl(&registers->sis_ctrl_to_host_doorbell);
  183. if (doorbell & SIS_CMD_COMPLETE)
  184. break;
  185. if (time_after(jiffies, timeout))
  186. return -ETIMEDOUT;
  187. }
  188. /* Read the command status from mailbox 0. */
  189. cmd_status = readl(&registers->sis_mailbox[0]);
  190. if (cmd_status != SIS_CMD_STATUS_SUCCESS) {
  191. dev_err(&ctrl_info->pci_dev->dev,
  192. "SIS command failed for command 0x%x: status = 0x%x\n",
  193. cmd, cmd_status);
  194. return -EINVAL;
  195. }
  196. /*
  197. * The command completed successfully, so save the command status and
  198. * read the values returned in mailboxes 1-5.
  199. */
  200. params->mailbox[0] = cmd_status;
  201. for (i = 1; i < ARRAY_SIZE(params->mailbox); i++)
  202. params->mailbox[i] = readl(&registers->sis_mailbox[i]);
  203. return 0;
  204. }
  205. /*
  206. * This function verifies that we are talking to a controller that speaks PQI.
  207. */
  208. int sis_get_ctrl_properties(struct pqi_ctrl_info *ctrl_info)
  209. {
  210. int rc;
  211. u32 properties;
  212. u32 extended_properties;
  213. struct sis_sync_cmd_params params;
  214. memset(&params, 0, sizeof(params));
  215. rc = sis_send_sync_cmd(ctrl_info, SIS_CMD_GET_ADAPTER_PROPERTIES,
  216. &params);
  217. if (rc)
  218. return rc;
  219. properties = params.mailbox[1];
  220. if (!(properties & SIS_EXTENDED_PROPERTIES_SUPPORTED))
  221. return -ENODEV;
  222. extended_properties = params.mailbox[4];
  223. if ((extended_properties & SIS_REQUIRED_EXTENDED_PROPERTIES) !=
  224. SIS_REQUIRED_EXTENDED_PROPERTIES)
  225. return -ENODEV;
  226. if (extended_properties & SIS_PQI_RESET_QUIESCE_SUPPORTED)
  227. ctrl_info->pqi_reset_quiesce_supported = true;
  228. return 0;
  229. }
  230. int sis_get_pqi_capabilities(struct pqi_ctrl_info *ctrl_info)
  231. {
  232. int rc;
  233. struct sis_sync_cmd_params params;
  234. memset(&params, 0, sizeof(params));
  235. rc = sis_send_sync_cmd(ctrl_info, SIS_CMD_GET_PQI_CAPABILITIES,
  236. &params);
  237. if (rc)
  238. return rc;
  239. ctrl_info->max_sg_entries = params.mailbox[1];
  240. ctrl_info->max_transfer_size = params.mailbox[2];
  241. ctrl_info->max_outstanding_requests = params.mailbox[3];
  242. ctrl_info->config_table_offset = params.mailbox[4];
  243. ctrl_info->config_table_length = params.mailbox[5];
  244. return 0;
  245. }
  246. int sis_init_base_struct_addr(struct pqi_ctrl_info *ctrl_info)
  247. {
  248. int rc;
  249. void *base_struct_unaligned;
  250. struct sis_base_struct *base_struct;
  251. struct sis_sync_cmd_params params;
  252. unsigned long error_buffer_paddr;
  253. dma_addr_t bus_address;
  254. base_struct_unaligned = kzalloc(sizeof(*base_struct)
  255. + SIS_BASE_STRUCT_ALIGNMENT - 1, GFP_KERNEL);
  256. if (!base_struct_unaligned)
  257. return -ENOMEM;
  258. base_struct = PTR_ALIGN(base_struct_unaligned,
  259. SIS_BASE_STRUCT_ALIGNMENT);
  260. error_buffer_paddr = (unsigned long)ctrl_info->error_buffer_dma_handle;
  261. put_unaligned_le32(SIS_BASE_STRUCT_REVISION, &base_struct->revision);
  262. put_unaligned_le32(lower_32_bits(error_buffer_paddr),
  263. &base_struct->error_buffer_paddr_low);
  264. put_unaligned_le32(upper_32_bits(error_buffer_paddr),
  265. &base_struct->error_buffer_paddr_high);
  266. put_unaligned_le32(PQI_ERROR_BUFFER_ELEMENT_LENGTH,
  267. &base_struct->error_buffer_element_length);
  268. put_unaligned_le32(ctrl_info->max_io_slots,
  269. &base_struct->error_buffer_num_elements);
  270. bus_address = dma_map_single(&ctrl_info->pci_dev->dev, base_struct,
  271. sizeof(*base_struct), DMA_TO_DEVICE);
  272. if (dma_mapping_error(&ctrl_info->pci_dev->dev, bus_address)) {
  273. rc = -ENOMEM;
  274. goto out;
  275. }
  276. memset(&params, 0, sizeof(params));
  277. params.mailbox[1] = lower_32_bits((u64)bus_address);
  278. params.mailbox[2] = upper_32_bits((u64)bus_address);
  279. params.mailbox[3] = sizeof(*base_struct);
  280. rc = sis_send_sync_cmd(ctrl_info, SIS_CMD_INIT_BASE_STRUCT_ADDRESS,
  281. &params);
  282. dma_unmap_single(&ctrl_info->pci_dev->dev, bus_address,
  283. sizeof(*base_struct), DMA_TO_DEVICE);
  284. out:
  285. kfree(base_struct_unaligned);
  286. return rc;
  287. }
  288. #define SIS_DOORBELL_BIT_CLEAR_TIMEOUT_SECS 30
  289. static int sis_wait_for_doorbell_bit_to_clear(
  290. struct pqi_ctrl_info *ctrl_info, u32 bit)
  291. {
  292. int rc = 0;
  293. u32 doorbell_register;
  294. unsigned long timeout;
  295. timeout = (SIS_DOORBELL_BIT_CLEAR_TIMEOUT_SECS * HZ) + jiffies;
  296. while (1) {
  297. doorbell_register =
  298. readl(&ctrl_info->registers->sis_host_to_ctrl_doorbell);
  299. if ((doorbell_register & bit) == 0)
  300. break;
  301. if (readl(&ctrl_info->registers->sis_firmware_status) &
  302. SIS_CTRL_KERNEL_PANIC) {
  303. rc = -ENODEV;
  304. break;
  305. }
  306. if (time_after(jiffies, timeout)) {
  307. dev_err(&ctrl_info->pci_dev->dev,
  308. "doorbell register bit 0x%x not cleared\n",
  309. bit);
  310. rc = -ETIMEDOUT;
  311. break;
  312. }
  313. usleep_range(1000, 2000);
  314. }
  315. return rc;
  316. }
  317. static inline int sis_set_doorbell_bit(struct pqi_ctrl_info *ctrl_info, u32 bit)
  318. {
  319. writel(bit, &ctrl_info->registers->sis_host_to_ctrl_doorbell);
  320. usleep_range(1000, 2000);
  321. return sis_wait_for_doorbell_bit_to_clear(ctrl_info, bit);
  322. }
  323. void sis_enable_msix(struct pqi_ctrl_info *ctrl_info)
  324. {
  325. sis_set_doorbell_bit(ctrl_info, SIS_ENABLE_MSIX);
  326. }
  327. void sis_enable_intx(struct pqi_ctrl_info *ctrl_info)
  328. {
  329. sis_set_doorbell_bit(ctrl_info, SIS_ENABLE_INTX);
  330. }
  331. void sis_shutdown_ctrl(struct pqi_ctrl_info *ctrl_info,
  332. enum pqi_ctrl_shutdown_reason ctrl_shutdown_reason)
  333. {
  334. if (readl(&ctrl_info->registers->sis_firmware_status) &
  335. SIS_CTRL_KERNEL_PANIC)
  336. return;
  337. if (ctrl_info->firmware_triage_supported)
  338. writel(ctrl_shutdown_reason, &ctrl_info->registers->sis_ctrl_shutdown_reason_code);
  339. writel(SIS_TRIGGER_SHUTDOWN, &ctrl_info->registers->sis_host_to_ctrl_doorbell);
  340. }
  341. int sis_pqi_reset_quiesce(struct pqi_ctrl_info *ctrl_info)
  342. {
  343. return sis_set_doorbell_bit(ctrl_info, SIS_PQI_RESET_QUIESCE);
  344. }
  345. int sis_reenable_sis_mode(struct pqi_ctrl_info *ctrl_info)
  346. {
  347. return sis_set_doorbell_bit(ctrl_info, SIS_REENABLE_SIS_MODE);
  348. }
  349. void sis_write_driver_scratch(struct pqi_ctrl_info *ctrl_info, u32 value)
  350. {
  351. writel(value, &ctrl_info->registers->sis_driver_scratch);
  352. usleep_range(1000, 2000);
  353. }
  354. u32 sis_read_driver_scratch(struct pqi_ctrl_info *ctrl_info)
  355. {
  356. return readl(&ctrl_info->registers->sis_driver_scratch);
  357. }
  358. static inline enum sis_fw_triage_status
  359. sis_read_firmware_triage_status(struct pqi_ctrl_info *ctrl_info)
  360. {
  361. return ((enum sis_fw_triage_status)(readl(&ctrl_info->registers->sis_firmware_status) &
  362. SIS_CTRL_KERNEL_FW_TRIAGE));
  363. }
  364. void sis_soft_reset(struct pqi_ctrl_info *ctrl_info)
  365. {
  366. writel(SIS_SOFT_RESET,
  367. &ctrl_info->registers->sis_host_to_ctrl_doorbell);
  368. }
  369. #define SIS_FW_TRIAGE_STATUS_TIMEOUT_SECS 300
  370. #define SIS_FW_TRIAGE_STATUS_POLL_INTERVAL_SECS 1
  371. int sis_wait_for_fw_triage_completion(struct pqi_ctrl_info *ctrl_info)
  372. {
  373. int rc;
  374. enum sis_fw_triage_status status;
  375. unsigned long timeout;
  376. timeout = (SIS_FW_TRIAGE_STATUS_TIMEOUT_SECS * HZ) + jiffies;
  377. while (1) {
  378. status = sis_read_firmware_triage_status(ctrl_info);
  379. if (status == FW_TRIAGE_COND_INVALID) {
  380. dev_err(&ctrl_info->pci_dev->dev,
  381. "firmware triage condition invalid\n");
  382. rc = -EINVAL;
  383. break;
  384. } else if (status == FW_TRIAGE_NOT_STARTED ||
  385. status == FW_TRIAGE_COMPLETED) {
  386. rc = 0;
  387. break;
  388. }
  389. if (time_after(jiffies, timeout)) {
  390. dev_err(&ctrl_info->pci_dev->dev,
  391. "timed out waiting for firmware triage status\n");
  392. rc = -ETIMEDOUT;
  393. break;
  394. }
  395. ssleep(SIS_FW_TRIAGE_STATUS_POLL_INTERVAL_SECS);
  396. }
  397. return rc;
  398. }
  399. void sis_verify_structures(void)
  400. {
  401. BUILD_BUG_ON(offsetof(struct sis_base_struct,
  402. revision) != 0x0);
  403. BUILD_BUG_ON(offsetof(struct sis_base_struct,
  404. flags) != 0x4);
  405. BUILD_BUG_ON(offsetof(struct sis_base_struct,
  406. error_buffer_paddr_low) != 0x8);
  407. BUILD_BUG_ON(offsetof(struct sis_base_struct,
  408. error_buffer_paddr_high) != 0xc);
  409. BUILD_BUG_ON(offsetof(struct sis_base_struct,
  410. error_buffer_element_length) != 0x10);
  411. BUILD_BUG_ON(offsetof(struct sis_base_struct,
  412. error_buffer_num_elements) != 0x14);
  413. BUILD_BUG_ON(sizeof(struct sis_base_struct) != 0x18);
  414. }