hinic_hw_if.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Huawei HiNIC PCI Express Linux driver
  3. * Copyright(c) 2017 Huawei Technologies Co., Ltd
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. */
  15. #include <linux/pci.h>
  16. #include <linux/device.h>
  17. #include <linux/errno.h>
  18. #include <linux/io.h>
  19. #include <linux/types.h>
  20. #include <linux/bitops.h>
  21. #include "hinic_hw_csr.h"
  22. #include "hinic_hw_if.h"
  23. #define PCIE_ATTR_ENTRY 0
  24. #define VALID_MSIX_IDX(attr, msix_index) ((msix_index) < (attr)->num_irqs)
  25. /**
  26. * hinic_msix_attr_set - set message attribute for msix entry
  27. * @hwif: the HW interface of a pci function device
  28. * @msix_index: msix_index
  29. * @pending_limit: the maximum pending interrupt events (unit 8)
  30. * @coalesc_timer: coalesc period for interrupt (unit 8 us)
  31. * @lli_timer: replenishing period for low latency credit (unit 8 us)
  32. * @lli_credit_limit: maximum credits for low latency msix messages (unit 8)
  33. * @resend_timer: maximum wait for resending msix (unit coalesc period)
  34. *
  35. * Return 0 - Success, negative - Failure
  36. **/
  37. int hinic_msix_attr_set(struct hinic_hwif *hwif, u16 msix_index,
  38. u8 pending_limit, u8 coalesc_timer,
  39. u8 lli_timer, u8 lli_credit_limit,
  40. u8 resend_timer)
  41. {
  42. u32 msix_ctrl, addr;
  43. if (!VALID_MSIX_IDX(&hwif->attr, msix_index))
  44. return -EINVAL;
  45. msix_ctrl = HINIC_MSIX_ATTR_SET(pending_limit, PENDING_LIMIT) |
  46. HINIC_MSIX_ATTR_SET(coalesc_timer, COALESC_TIMER) |
  47. HINIC_MSIX_ATTR_SET(lli_timer, LLI_TIMER) |
  48. HINIC_MSIX_ATTR_SET(lli_credit_limit, LLI_CREDIT) |
  49. HINIC_MSIX_ATTR_SET(resend_timer, RESEND_TIMER);
  50. addr = HINIC_CSR_MSIX_CTRL_ADDR(msix_index);
  51. hinic_hwif_write_reg(hwif, addr, msix_ctrl);
  52. return 0;
  53. }
  54. /**
  55. * hinic_msix_attr_get - get message attribute of msix entry
  56. * @hwif: the HW interface of a pci function device
  57. * @msix_index: msix_index
  58. * @pending_limit: the maximum pending interrupt events (unit 8)
  59. * @coalesc_timer: coalesc period for interrupt (unit 8 us)
  60. * @lli_timer: replenishing period for low latency credit (unit 8 us)
  61. * @lli_credit_limit: maximum credits for low latency msix messages (unit 8)
  62. * @resend_timer: maximum wait for resending msix (unit coalesc period)
  63. *
  64. * Return 0 - Success, negative - Failure
  65. **/
  66. int hinic_msix_attr_get(struct hinic_hwif *hwif, u16 msix_index,
  67. u8 *pending_limit, u8 *coalesc_timer,
  68. u8 *lli_timer, u8 *lli_credit_limit,
  69. u8 *resend_timer)
  70. {
  71. u32 addr, val;
  72. if (!VALID_MSIX_IDX(&hwif->attr, msix_index))
  73. return -EINVAL;
  74. addr = HINIC_CSR_MSIX_CTRL_ADDR(msix_index);
  75. val = hinic_hwif_read_reg(hwif, addr);
  76. *pending_limit = HINIC_MSIX_ATTR_GET(val, PENDING_LIMIT);
  77. *coalesc_timer = HINIC_MSIX_ATTR_GET(val, COALESC_TIMER);
  78. *lli_timer = HINIC_MSIX_ATTR_GET(val, LLI_TIMER);
  79. *lli_credit_limit = HINIC_MSIX_ATTR_GET(val, LLI_CREDIT);
  80. *resend_timer = HINIC_MSIX_ATTR_GET(val, RESEND_TIMER);
  81. return 0;
  82. }
  83. /**
  84. * hinic_msix_attr_cnt_clear - clear message attribute counters for msix entry
  85. * @hwif: the HW interface of a pci function device
  86. * @msix_index: msix_index
  87. *
  88. * Return 0 - Success, negative - Failure
  89. **/
  90. int hinic_msix_attr_cnt_clear(struct hinic_hwif *hwif, u16 msix_index)
  91. {
  92. u32 msix_ctrl, addr;
  93. if (!VALID_MSIX_IDX(&hwif->attr, msix_index))
  94. return -EINVAL;
  95. msix_ctrl = HINIC_MSIX_CNT_SET(1, RESEND_TIMER);
  96. addr = HINIC_CSR_MSIX_CNT_ADDR(msix_index);
  97. hinic_hwif_write_reg(hwif, addr, msix_ctrl);
  98. return 0;
  99. }
  100. /**
  101. * hinic_set_pf_action - set action on pf channel
  102. * @hwif: the HW interface of a pci function device
  103. * @action: action on pf channel
  104. *
  105. * Return 0 - Success, negative - Failure
  106. **/
  107. void hinic_set_pf_action(struct hinic_hwif *hwif, enum hinic_pf_action action)
  108. {
  109. u32 attr5 = hinic_hwif_read_reg(hwif, HINIC_CSR_FUNC_ATTR5_ADDR);
  110. attr5 = HINIC_FA5_CLEAR(attr5, PF_ACTION);
  111. attr5 |= HINIC_FA5_SET(action, PF_ACTION);
  112. hinic_hwif_write_reg(hwif, HINIC_CSR_FUNC_ATTR5_ADDR, attr5);
  113. }
  114. enum hinic_outbound_state hinic_outbound_state_get(struct hinic_hwif *hwif)
  115. {
  116. u32 attr4 = hinic_hwif_read_reg(hwif, HINIC_CSR_FUNC_ATTR4_ADDR);
  117. return HINIC_FA4_GET(attr4, OUTBOUND_STATE);
  118. }
  119. void hinic_outbound_state_set(struct hinic_hwif *hwif,
  120. enum hinic_outbound_state outbound_state)
  121. {
  122. u32 attr4 = hinic_hwif_read_reg(hwif, HINIC_CSR_FUNC_ATTR4_ADDR);
  123. attr4 = HINIC_FA4_CLEAR(attr4, OUTBOUND_STATE);
  124. attr4 |= HINIC_FA4_SET(outbound_state, OUTBOUND_STATE);
  125. hinic_hwif_write_reg(hwif, HINIC_CSR_FUNC_ATTR4_ADDR, attr4);
  126. }
  127. enum hinic_db_state hinic_db_state_get(struct hinic_hwif *hwif)
  128. {
  129. u32 attr4 = hinic_hwif_read_reg(hwif, HINIC_CSR_FUNC_ATTR4_ADDR);
  130. return HINIC_FA4_GET(attr4, DB_STATE);
  131. }
  132. void hinic_db_state_set(struct hinic_hwif *hwif,
  133. enum hinic_db_state db_state)
  134. {
  135. u32 attr4 = hinic_hwif_read_reg(hwif, HINIC_CSR_FUNC_ATTR4_ADDR);
  136. attr4 = HINIC_FA4_CLEAR(attr4, DB_STATE);
  137. attr4 |= HINIC_FA4_SET(db_state, DB_STATE);
  138. hinic_hwif_write_reg(hwif, HINIC_CSR_FUNC_ATTR4_ADDR, attr4);
  139. }
  140. /**
  141. * hwif_ready - test if the HW is ready for use
  142. * @hwif: the HW interface of a pci function device
  143. *
  144. * Return 0 - Success, negative - Failure
  145. **/
  146. static int hwif_ready(struct hinic_hwif *hwif)
  147. {
  148. struct pci_dev *pdev = hwif->pdev;
  149. u32 addr, attr1;
  150. addr = HINIC_CSR_FUNC_ATTR1_ADDR;
  151. attr1 = hinic_hwif_read_reg(hwif, addr);
  152. if (!HINIC_FA1_GET(attr1, INIT_STATUS)) {
  153. dev_err(&pdev->dev, "hwif status is not ready\n");
  154. return -EFAULT;
  155. }
  156. return 0;
  157. }
  158. /**
  159. * set_hwif_attr - set the attributes in the relevant members in hwif
  160. * @hwif: the HW interface of a pci function device
  161. * @attr0: the first attribute that was read from the hw
  162. * @attr1: the second attribute that was read from the hw
  163. **/
  164. static void set_hwif_attr(struct hinic_hwif *hwif, u32 attr0, u32 attr1)
  165. {
  166. hwif->attr.func_idx = HINIC_FA0_GET(attr0, FUNC_IDX);
  167. hwif->attr.pf_idx = HINIC_FA0_GET(attr0, PF_IDX);
  168. hwif->attr.pci_intf_idx = HINIC_FA0_GET(attr0, PCI_INTF_IDX);
  169. hwif->attr.func_type = HINIC_FA0_GET(attr0, FUNC_TYPE);
  170. hwif->attr.num_aeqs = BIT(HINIC_FA1_GET(attr1, AEQS_PER_FUNC));
  171. hwif->attr.num_ceqs = BIT(HINIC_FA1_GET(attr1, CEQS_PER_FUNC));
  172. hwif->attr.num_irqs = BIT(HINIC_FA1_GET(attr1, IRQS_PER_FUNC));
  173. hwif->attr.num_dma_attr = BIT(HINIC_FA1_GET(attr1, DMA_ATTR_PER_FUNC));
  174. }
  175. /**
  176. * read_hwif_attr - read the attributes and set members in hwif
  177. * @hwif: the HW interface of a pci function device
  178. **/
  179. static void read_hwif_attr(struct hinic_hwif *hwif)
  180. {
  181. u32 addr, attr0, attr1;
  182. addr = HINIC_CSR_FUNC_ATTR0_ADDR;
  183. attr0 = hinic_hwif_read_reg(hwif, addr);
  184. addr = HINIC_CSR_FUNC_ATTR1_ADDR;
  185. attr1 = hinic_hwif_read_reg(hwif, addr);
  186. set_hwif_attr(hwif, attr0, attr1);
  187. }
  188. /**
  189. * set_ppf - try to set hwif as ppf and set the type of hwif in this case
  190. * @hwif: the HW interface of a pci function device
  191. **/
  192. static void set_ppf(struct hinic_hwif *hwif)
  193. {
  194. struct hinic_func_attr *attr = &hwif->attr;
  195. u32 addr, val, ppf_election;
  196. /* Read Modify Write */
  197. addr = HINIC_CSR_PPF_ELECTION_ADDR(HINIC_HWIF_PCI_INTF(hwif));
  198. val = hinic_hwif_read_reg(hwif, addr);
  199. val = HINIC_PPF_ELECTION_CLEAR(val, IDX);
  200. ppf_election = HINIC_PPF_ELECTION_SET(HINIC_HWIF_FUNC_IDX(hwif), IDX);
  201. val |= ppf_election;
  202. hinic_hwif_write_reg(hwif, addr, val);
  203. /* check PPF */
  204. val = hinic_hwif_read_reg(hwif, addr);
  205. attr->ppf_idx = HINIC_PPF_ELECTION_GET(val, IDX);
  206. if (attr->ppf_idx == HINIC_HWIF_FUNC_IDX(hwif))
  207. attr->func_type = HINIC_PPF;
  208. }
  209. /**
  210. * set_dma_attr - set the dma attributes in the HW
  211. * @hwif: the HW interface of a pci function device
  212. * @entry_idx: the entry index in the dma table
  213. * @st: PCIE TLP steering tag
  214. * @at: PCIE TLP AT field
  215. * @ph: PCIE TLP Processing Hint field
  216. * @no_snooping: PCIE TLP No snooping
  217. * @tph_en: PCIE TLP Processing Hint Enable
  218. **/
  219. static void set_dma_attr(struct hinic_hwif *hwif, u32 entry_idx,
  220. u8 st, u8 at, u8 ph,
  221. enum hinic_pcie_nosnoop no_snooping,
  222. enum hinic_pcie_tph tph_en)
  223. {
  224. u32 addr, val, dma_attr_entry;
  225. /* Read Modify Write */
  226. addr = HINIC_CSR_DMA_ATTR_ADDR(entry_idx);
  227. val = hinic_hwif_read_reg(hwif, addr);
  228. val = HINIC_DMA_ATTR_CLEAR(val, ST) &
  229. HINIC_DMA_ATTR_CLEAR(val, AT) &
  230. HINIC_DMA_ATTR_CLEAR(val, PH) &
  231. HINIC_DMA_ATTR_CLEAR(val, NO_SNOOPING) &
  232. HINIC_DMA_ATTR_CLEAR(val, TPH_EN);
  233. dma_attr_entry = HINIC_DMA_ATTR_SET(st, ST) |
  234. HINIC_DMA_ATTR_SET(at, AT) |
  235. HINIC_DMA_ATTR_SET(ph, PH) |
  236. HINIC_DMA_ATTR_SET(no_snooping, NO_SNOOPING) |
  237. HINIC_DMA_ATTR_SET(tph_en, TPH_EN);
  238. val |= dma_attr_entry;
  239. hinic_hwif_write_reg(hwif, addr, val);
  240. }
  241. /**
  242. * dma_attr_table_init - initialize the the default dma attributes
  243. * @hwif: the HW interface of a pci function device
  244. **/
  245. static void dma_attr_init(struct hinic_hwif *hwif)
  246. {
  247. set_dma_attr(hwif, PCIE_ATTR_ENTRY, HINIC_PCIE_ST_DISABLE,
  248. HINIC_PCIE_AT_DISABLE, HINIC_PCIE_PH_DISABLE,
  249. HINIC_PCIE_SNOOP, HINIC_PCIE_TPH_DISABLE);
  250. }
  251. /**
  252. * hinic_init_hwif - initialize the hw interface
  253. * @hwif: the HW interface of a pci function device
  254. * @pdev: the pci device for acessing PCI resources
  255. *
  256. * Return 0 - Success, negative - Failure
  257. **/
  258. int hinic_init_hwif(struct hinic_hwif *hwif, struct pci_dev *pdev)
  259. {
  260. int err;
  261. hwif->pdev = pdev;
  262. hwif->cfg_regs_bar = pci_ioremap_bar(pdev, HINIC_PCI_CFG_REGS_BAR);
  263. if (!hwif->cfg_regs_bar) {
  264. dev_err(&pdev->dev, "Failed to map configuration regs\n");
  265. return -ENOMEM;
  266. }
  267. err = hwif_ready(hwif);
  268. if (err) {
  269. dev_err(&pdev->dev, "HW interface is not ready\n");
  270. goto err_hwif_ready;
  271. }
  272. read_hwif_attr(hwif);
  273. if (HINIC_IS_PF(hwif))
  274. set_ppf(hwif);
  275. /* No transactionss before DMA is initialized */
  276. dma_attr_init(hwif);
  277. return 0;
  278. err_hwif_ready:
  279. iounmap(hwif->cfg_regs_bar);
  280. return err;
  281. }
  282. /**
  283. * hinic_free_hwif - free the HW interface
  284. * @hwif: the HW interface of a pci function device
  285. **/
  286. void hinic_free_hwif(struct hinic_hwif *hwif)
  287. {
  288. iounmap(hwif->cfg_regs_bar);
  289. }