smartpqi_sis.c 12 KB

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