qplib_res.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /*
  2. * Broadcom NetXtreme-E RoCE driver.
  3. *
  4. * Copyright (c) 2016 - 2017, Broadcom. All rights reserved. The term
  5. * Broadcom refers to Broadcom Limited and/or its subsidiaries.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  26. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
  28. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  31. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  32. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  33. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  34. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. * Description: QPLib resource manager
  37. */
  38. #define dev_fmt(fmt) "QPLIB: " fmt
  39. #include <linux/spinlock.h>
  40. #include <linux/pci.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/inetdevice.h>
  43. #include <linux/dma-mapping.h>
  44. #include <linux/if_vlan.h>
  45. #include "roce_hsi.h"
  46. #include "qplib_res.h"
  47. #include "qplib_sp.h"
  48. #include "qplib_rcfw.h"
  49. static void bnxt_qplib_free_stats_ctx(struct pci_dev *pdev,
  50. struct bnxt_qplib_stats *stats);
  51. static int bnxt_qplib_alloc_stats_ctx(struct pci_dev *pdev,
  52. struct bnxt_qplib_stats *stats);
  53. /* PBL */
  54. static void __free_pbl(struct pci_dev *pdev, struct bnxt_qplib_pbl *pbl,
  55. bool is_umem)
  56. {
  57. int i;
  58. if (!is_umem) {
  59. for (i = 0; i < pbl->pg_count; i++) {
  60. if (pbl->pg_arr[i])
  61. dma_free_coherent(&pdev->dev, pbl->pg_size,
  62. (void *)((unsigned long)
  63. pbl->pg_arr[i] &
  64. PAGE_MASK),
  65. pbl->pg_map_arr[i]);
  66. else
  67. dev_warn(&pdev->dev,
  68. "PBL free pg_arr[%d] empty?!\n", i);
  69. pbl->pg_arr[i] = NULL;
  70. }
  71. }
  72. kfree(pbl->pg_arr);
  73. pbl->pg_arr = NULL;
  74. kfree(pbl->pg_map_arr);
  75. pbl->pg_map_arr = NULL;
  76. pbl->pg_count = 0;
  77. pbl->pg_size = 0;
  78. }
  79. static int __alloc_pbl(struct pci_dev *pdev, struct bnxt_qplib_pbl *pbl,
  80. struct scatterlist *sghead, u32 pages, u32 pg_size)
  81. {
  82. struct scatterlist *sg;
  83. bool is_umem = false;
  84. int i;
  85. /* page ptr arrays */
  86. pbl->pg_arr = kcalloc(pages, sizeof(void *), GFP_KERNEL);
  87. if (!pbl->pg_arr)
  88. return -ENOMEM;
  89. pbl->pg_map_arr = kcalloc(pages, sizeof(dma_addr_t), GFP_KERNEL);
  90. if (!pbl->pg_map_arr) {
  91. kfree(pbl->pg_arr);
  92. pbl->pg_arr = NULL;
  93. return -ENOMEM;
  94. }
  95. pbl->pg_count = 0;
  96. pbl->pg_size = pg_size;
  97. if (!sghead) {
  98. for (i = 0; i < pages; i++) {
  99. pbl->pg_arr[i] = dma_zalloc_coherent(&pdev->dev,
  100. pbl->pg_size,
  101. &pbl->pg_map_arr[i],
  102. GFP_KERNEL);
  103. if (!pbl->pg_arr[i])
  104. goto fail;
  105. pbl->pg_count++;
  106. }
  107. } else {
  108. i = 0;
  109. is_umem = true;
  110. for_each_sg(sghead, sg, pages, i) {
  111. pbl->pg_map_arr[i] = sg_dma_address(sg);
  112. pbl->pg_arr[i] = sg_virt(sg);
  113. if (!pbl->pg_arr[i])
  114. goto fail;
  115. pbl->pg_count++;
  116. }
  117. }
  118. return 0;
  119. fail:
  120. __free_pbl(pdev, pbl, is_umem);
  121. return -ENOMEM;
  122. }
  123. /* HWQ */
  124. void bnxt_qplib_free_hwq(struct pci_dev *pdev, struct bnxt_qplib_hwq *hwq)
  125. {
  126. int i;
  127. if (!hwq->max_elements)
  128. return;
  129. if (hwq->level >= PBL_LVL_MAX)
  130. return;
  131. for (i = 0; i < hwq->level + 1; i++) {
  132. if (i == hwq->level)
  133. __free_pbl(pdev, &hwq->pbl[i], hwq->is_user);
  134. else
  135. __free_pbl(pdev, &hwq->pbl[i], false);
  136. }
  137. hwq->level = PBL_LVL_MAX;
  138. hwq->max_elements = 0;
  139. hwq->element_size = 0;
  140. hwq->prod = 0;
  141. hwq->cons = 0;
  142. hwq->cp_bit = 0;
  143. }
  144. /* All HWQs are power of 2 in size */
  145. int bnxt_qplib_alloc_init_hwq(struct pci_dev *pdev, struct bnxt_qplib_hwq *hwq,
  146. struct scatterlist *sghead, int nmap,
  147. u32 *elements, u32 element_size, u32 aux,
  148. u32 pg_size, enum bnxt_qplib_hwq_type hwq_type)
  149. {
  150. u32 pages, slots, size, aux_pages = 0, aux_size = 0;
  151. dma_addr_t *src_phys_ptr, **dst_virt_ptr;
  152. int i, rc;
  153. hwq->level = PBL_LVL_MAX;
  154. slots = roundup_pow_of_two(*elements);
  155. if (aux) {
  156. aux_size = roundup_pow_of_two(aux);
  157. aux_pages = (slots * aux_size) / pg_size;
  158. if ((slots * aux_size) % pg_size)
  159. aux_pages++;
  160. }
  161. size = roundup_pow_of_two(element_size);
  162. if (!sghead) {
  163. hwq->is_user = false;
  164. pages = (slots * size) / pg_size + aux_pages;
  165. if ((slots * size) % pg_size)
  166. pages++;
  167. if (!pages)
  168. return -EINVAL;
  169. } else {
  170. hwq->is_user = true;
  171. pages = nmap;
  172. }
  173. /* Alloc the 1st memory block; can be a PDL/PTL/PBL */
  174. if (sghead && (pages == MAX_PBL_LVL_0_PGS))
  175. rc = __alloc_pbl(pdev, &hwq->pbl[PBL_LVL_0], sghead,
  176. pages, pg_size);
  177. else
  178. rc = __alloc_pbl(pdev, &hwq->pbl[PBL_LVL_0], NULL, 1, pg_size);
  179. if (rc)
  180. goto fail;
  181. hwq->level = PBL_LVL_0;
  182. if (pages > MAX_PBL_LVL_0_PGS) {
  183. if (pages > MAX_PBL_LVL_1_PGS) {
  184. /* 2 levels of indirection */
  185. rc = __alloc_pbl(pdev, &hwq->pbl[PBL_LVL_1], NULL,
  186. MAX_PBL_LVL_1_PGS_FOR_LVL_2, pg_size);
  187. if (rc)
  188. goto fail;
  189. /* Fill in lvl0 PBL */
  190. dst_virt_ptr =
  191. (dma_addr_t **)hwq->pbl[PBL_LVL_0].pg_arr;
  192. src_phys_ptr = hwq->pbl[PBL_LVL_1].pg_map_arr;
  193. for (i = 0; i < hwq->pbl[PBL_LVL_1].pg_count; i++)
  194. dst_virt_ptr[PTR_PG(i)][PTR_IDX(i)] =
  195. src_phys_ptr[i] | PTU_PDE_VALID;
  196. hwq->level = PBL_LVL_1;
  197. rc = __alloc_pbl(pdev, &hwq->pbl[PBL_LVL_2], sghead,
  198. pages, pg_size);
  199. if (rc)
  200. goto fail;
  201. /* Fill in lvl1 PBL */
  202. dst_virt_ptr =
  203. (dma_addr_t **)hwq->pbl[PBL_LVL_1].pg_arr;
  204. src_phys_ptr = hwq->pbl[PBL_LVL_2].pg_map_arr;
  205. for (i = 0; i < hwq->pbl[PBL_LVL_2].pg_count; i++) {
  206. dst_virt_ptr[PTR_PG(i)][PTR_IDX(i)] =
  207. src_phys_ptr[i] | PTU_PTE_VALID;
  208. }
  209. if (hwq_type == HWQ_TYPE_QUEUE) {
  210. /* Find the last pg of the size */
  211. i = hwq->pbl[PBL_LVL_2].pg_count;
  212. dst_virt_ptr[PTR_PG(i - 1)][PTR_IDX(i - 1)] |=
  213. PTU_PTE_LAST;
  214. if (i > 1)
  215. dst_virt_ptr[PTR_PG(i - 2)]
  216. [PTR_IDX(i - 2)] |=
  217. PTU_PTE_NEXT_TO_LAST;
  218. }
  219. hwq->level = PBL_LVL_2;
  220. } else {
  221. u32 flag = hwq_type == HWQ_TYPE_L2_CMPL ? 0 :
  222. PTU_PTE_VALID;
  223. /* 1 level of indirection */
  224. rc = __alloc_pbl(pdev, &hwq->pbl[PBL_LVL_1], sghead,
  225. pages, pg_size);
  226. if (rc)
  227. goto fail;
  228. /* Fill in lvl0 PBL */
  229. dst_virt_ptr =
  230. (dma_addr_t **)hwq->pbl[PBL_LVL_0].pg_arr;
  231. src_phys_ptr = hwq->pbl[PBL_LVL_1].pg_map_arr;
  232. for (i = 0; i < hwq->pbl[PBL_LVL_1].pg_count; i++) {
  233. dst_virt_ptr[PTR_PG(i)][PTR_IDX(i)] =
  234. src_phys_ptr[i] | flag;
  235. }
  236. if (hwq_type == HWQ_TYPE_QUEUE) {
  237. /* Find the last pg of the size */
  238. i = hwq->pbl[PBL_LVL_1].pg_count;
  239. dst_virt_ptr[PTR_PG(i - 1)][PTR_IDX(i - 1)] |=
  240. PTU_PTE_LAST;
  241. if (i > 1)
  242. dst_virt_ptr[PTR_PG(i - 2)]
  243. [PTR_IDX(i - 2)] |=
  244. PTU_PTE_NEXT_TO_LAST;
  245. }
  246. hwq->level = PBL_LVL_1;
  247. }
  248. }
  249. hwq->pdev = pdev;
  250. spin_lock_init(&hwq->lock);
  251. hwq->prod = 0;
  252. hwq->cons = 0;
  253. *elements = hwq->max_elements = slots;
  254. hwq->element_size = size;
  255. /* For direct access to the elements */
  256. hwq->pbl_ptr = hwq->pbl[hwq->level].pg_arr;
  257. hwq->pbl_dma_ptr = hwq->pbl[hwq->level].pg_map_arr;
  258. return 0;
  259. fail:
  260. bnxt_qplib_free_hwq(pdev, hwq);
  261. return -ENOMEM;
  262. }
  263. /* Context Tables */
  264. void bnxt_qplib_free_ctx(struct pci_dev *pdev,
  265. struct bnxt_qplib_ctx *ctx)
  266. {
  267. int i;
  268. bnxt_qplib_free_hwq(pdev, &ctx->qpc_tbl);
  269. bnxt_qplib_free_hwq(pdev, &ctx->mrw_tbl);
  270. bnxt_qplib_free_hwq(pdev, &ctx->srqc_tbl);
  271. bnxt_qplib_free_hwq(pdev, &ctx->cq_tbl);
  272. bnxt_qplib_free_hwq(pdev, &ctx->tim_tbl);
  273. for (i = 0; i < MAX_TQM_ALLOC_REQ; i++)
  274. bnxt_qplib_free_hwq(pdev, &ctx->tqm_tbl[i]);
  275. bnxt_qplib_free_hwq(pdev, &ctx->tqm_pde);
  276. bnxt_qplib_free_stats_ctx(pdev, &ctx->stats);
  277. }
  278. /*
  279. * Routine: bnxt_qplib_alloc_ctx
  280. * Description:
  281. * Context tables are memories which are used by the chip fw.
  282. * The 6 tables defined are:
  283. * QPC ctx - holds QP states
  284. * MRW ctx - holds memory region and window
  285. * SRQ ctx - holds shared RQ states
  286. * CQ ctx - holds completion queue states
  287. * TQM ctx - holds Tx Queue Manager context
  288. * TIM ctx - holds timer context
  289. * Depending on the size of the tbl requested, either a 1 Page Buffer List
  290. * or a 1-to-2-stage indirection Page Directory List + 1 PBL is used
  291. * instead.
  292. * Table might be employed as follows:
  293. * For 0 < ctx size <= 1 PAGE, 0 level of ind is used
  294. * For 1 PAGE < ctx size <= 512 entries size, 1 level of ind is used
  295. * For 512 < ctx size <= MAX, 2 levels of ind is used
  296. * Returns:
  297. * 0 if success, else -ERRORS
  298. */
  299. int bnxt_qplib_alloc_ctx(struct pci_dev *pdev,
  300. struct bnxt_qplib_ctx *ctx,
  301. bool virt_fn)
  302. {
  303. int i, j, k, rc = 0;
  304. int fnz_idx = -1;
  305. __le64 **pbl_ptr;
  306. if (virt_fn)
  307. goto stats_alloc;
  308. /* QPC Tables */
  309. ctx->qpc_tbl.max_elements = ctx->qpc_count;
  310. rc = bnxt_qplib_alloc_init_hwq(pdev, &ctx->qpc_tbl, NULL, 0,
  311. &ctx->qpc_tbl.max_elements,
  312. BNXT_QPLIB_MAX_QP_CTX_ENTRY_SIZE, 0,
  313. PAGE_SIZE, HWQ_TYPE_CTX);
  314. if (rc)
  315. goto fail;
  316. /* MRW Tables */
  317. ctx->mrw_tbl.max_elements = ctx->mrw_count;
  318. rc = bnxt_qplib_alloc_init_hwq(pdev, &ctx->mrw_tbl, NULL, 0,
  319. &ctx->mrw_tbl.max_elements,
  320. BNXT_QPLIB_MAX_MRW_CTX_ENTRY_SIZE, 0,
  321. PAGE_SIZE, HWQ_TYPE_CTX);
  322. if (rc)
  323. goto fail;
  324. /* SRQ Tables */
  325. ctx->srqc_tbl.max_elements = ctx->srqc_count;
  326. rc = bnxt_qplib_alloc_init_hwq(pdev, &ctx->srqc_tbl, NULL, 0,
  327. &ctx->srqc_tbl.max_elements,
  328. BNXT_QPLIB_MAX_SRQ_CTX_ENTRY_SIZE, 0,
  329. PAGE_SIZE, HWQ_TYPE_CTX);
  330. if (rc)
  331. goto fail;
  332. /* CQ Tables */
  333. ctx->cq_tbl.max_elements = ctx->cq_count;
  334. rc = bnxt_qplib_alloc_init_hwq(pdev, &ctx->cq_tbl, NULL, 0,
  335. &ctx->cq_tbl.max_elements,
  336. BNXT_QPLIB_MAX_CQ_CTX_ENTRY_SIZE, 0,
  337. PAGE_SIZE, HWQ_TYPE_CTX);
  338. if (rc)
  339. goto fail;
  340. /* TQM Buffer */
  341. ctx->tqm_pde.max_elements = 512;
  342. rc = bnxt_qplib_alloc_init_hwq(pdev, &ctx->tqm_pde, NULL, 0,
  343. &ctx->tqm_pde.max_elements, sizeof(u64),
  344. 0, PAGE_SIZE, HWQ_TYPE_CTX);
  345. if (rc)
  346. goto fail;
  347. for (i = 0; i < MAX_TQM_ALLOC_REQ; i++) {
  348. if (!ctx->tqm_count[i])
  349. continue;
  350. ctx->tqm_tbl[i].max_elements = ctx->qpc_count *
  351. ctx->tqm_count[i];
  352. rc = bnxt_qplib_alloc_init_hwq(pdev, &ctx->tqm_tbl[i], NULL, 0,
  353. &ctx->tqm_tbl[i].max_elements, 1,
  354. 0, PAGE_SIZE, HWQ_TYPE_CTX);
  355. if (rc)
  356. goto fail;
  357. }
  358. pbl_ptr = (__le64 **)ctx->tqm_pde.pbl_ptr;
  359. for (i = 0, j = 0; i < MAX_TQM_ALLOC_REQ;
  360. i++, j += MAX_TQM_ALLOC_BLK_SIZE) {
  361. if (!ctx->tqm_tbl[i].max_elements)
  362. continue;
  363. if (fnz_idx == -1)
  364. fnz_idx = i;
  365. switch (ctx->tqm_tbl[i].level) {
  366. case PBL_LVL_2:
  367. for (k = 0; k < ctx->tqm_tbl[i].pbl[PBL_LVL_1].pg_count;
  368. k++)
  369. pbl_ptr[PTR_PG(j + k)][PTR_IDX(j + k)] =
  370. cpu_to_le64(
  371. ctx->tqm_tbl[i].pbl[PBL_LVL_1].pg_map_arr[k]
  372. | PTU_PTE_VALID);
  373. break;
  374. case PBL_LVL_1:
  375. case PBL_LVL_0:
  376. default:
  377. pbl_ptr[PTR_PG(j)][PTR_IDX(j)] = cpu_to_le64(
  378. ctx->tqm_tbl[i].pbl[PBL_LVL_0].pg_map_arr[0] |
  379. PTU_PTE_VALID);
  380. break;
  381. }
  382. }
  383. if (fnz_idx == -1)
  384. fnz_idx = 0;
  385. ctx->tqm_pde_level = ctx->tqm_tbl[fnz_idx].level == PBL_LVL_2 ?
  386. PBL_LVL_2 : ctx->tqm_tbl[fnz_idx].level + 1;
  387. /* TIM Buffer */
  388. ctx->tim_tbl.max_elements = ctx->qpc_count * 16;
  389. rc = bnxt_qplib_alloc_init_hwq(pdev, &ctx->tim_tbl, NULL, 0,
  390. &ctx->tim_tbl.max_elements, 1,
  391. 0, PAGE_SIZE, HWQ_TYPE_CTX);
  392. if (rc)
  393. goto fail;
  394. stats_alloc:
  395. /* Stats */
  396. rc = bnxt_qplib_alloc_stats_ctx(pdev, &ctx->stats);
  397. if (rc)
  398. goto fail;
  399. return 0;
  400. fail:
  401. bnxt_qplib_free_ctx(pdev, ctx);
  402. return rc;
  403. }
  404. /* GUID */
  405. void bnxt_qplib_get_guid(u8 *dev_addr, u8 *guid)
  406. {
  407. u8 mac[ETH_ALEN];
  408. /* MAC-48 to EUI-64 mapping */
  409. memcpy(mac, dev_addr, ETH_ALEN);
  410. guid[0] = mac[0] ^ 2;
  411. guid[1] = mac[1];
  412. guid[2] = mac[2];
  413. guid[3] = 0xff;
  414. guid[4] = 0xfe;
  415. guid[5] = mac[3];
  416. guid[6] = mac[4];
  417. guid[7] = mac[5];
  418. }
  419. static void bnxt_qplib_free_sgid_tbl(struct bnxt_qplib_res *res,
  420. struct bnxt_qplib_sgid_tbl *sgid_tbl)
  421. {
  422. kfree(sgid_tbl->tbl);
  423. kfree(sgid_tbl->hw_id);
  424. kfree(sgid_tbl->ctx);
  425. kfree(sgid_tbl->vlan);
  426. sgid_tbl->tbl = NULL;
  427. sgid_tbl->hw_id = NULL;
  428. sgid_tbl->ctx = NULL;
  429. sgid_tbl->vlan = NULL;
  430. sgid_tbl->max = 0;
  431. sgid_tbl->active = 0;
  432. }
  433. static int bnxt_qplib_alloc_sgid_tbl(struct bnxt_qplib_res *res,
  434. struct bnxt_qplib_sgid_tbl *sgid_tbl,
  435. u16 max)
  436. {
  437. sgid_tbl->tbl = kcalloc(max, sizeof(struct bnxt_qplib_gid), GFP_KERNEL);
  438. if (!sgid_tbl->tbl)
  439. return -ENOMEM;
  440. sgid_tbl->hw_id = kcalloc(max, sizeof(u16), GFP_KERNEL);
  441. if (!sgid_tbl->hw_id)
  442. goto out_free1;
  443. sgid_tbl->ctx = kcalloc(max, sizeof(void *), GFP_KERNEL);
  444. if (!sgid_tbl->ctx)
  445. goto out_free2;
  446. sgid_tbl->vlan = kcalloc(max, sizeof(u8), GFP_KERNEL);
  447. if (!sgid_tbl->vlan)
  448. goto out_free3;
  449. sgid_tbl->max = max;
  450. return 0;
  451. out_free3:
  452. kfree(sgid_tbl->ctx);
  453. sgid_tbl->ctx = NULL;
  454. out_free2:
  455. kfree(sgid_tbl->hw_id);
  456. sgid_tbl->hw_id = NULL;
  457. out_free1:
  458. kfree(sgid_tbl->tbl);
  459. sgid_tbl->tbl = NULL;
  460. return -ENOMEM;
  461. };
  462. static void bnxt_qplib_cleanup_sgid_tbl(struct bnxt_qplib_res *res,
  463. struct bnxt_qplib_sgid_tbl *sgid_tbl)
  464. {
  465. int i;
  466. for (i = 0; i < sgid_tbl->max; i++) {
  467. if (memcmp(&sgid_tbl->tbl[i], &bnxt_qplib_gid_zero,
  468. sizeof(bnxt_qplib_gid_zero)))
  469. bnxt_qplib_del_sgid(sgid_tbl, &sgid_tbl->tbl[i], true);
  470. }
  471. memset(sgid_tbl->tbl, 0, sizeof(struct bnxt_qplib_gid) * sgid_tbl->max);
  472. memset(sgid_tbl->hw_id, -1, sizeof(u16) * sgid_tbl->max);
  473. memset(sgid_tbl->vlan, 0, sizeof(u8) * sgid_tbl->max);
  474. sgid_tbl->active = 0;
  475. }
  476. static void bnxt_qplib_init_sgid_tbl(struct bnxt_qplib_sgid_tbl *sgid_tbl,
  477. struct net_device *netdev)
  478. {
  479. memset(sgid_tbl->tbl, 0, sizeof(struct bnxt_qplib_gid) * sgid_tbl->max);
  480. memset(sgid_tbl->hw_id, -1, sizeof(u16) * sgid_tbl->max);
  481. }
  482. static void bnxt_qplib_free_pkey_tbl(struct bnxt_qplib_res *res,
  483. struct bnxt_qplib_pkey_tbl *pkey_tbl)
  484. {
  485. if (!pkey_tbl->tbl)
  486. dev_dbg(&res->pdev->dev, "PKEY tbl not present\n");
  487. else
  488. kfree(pkey_tbl->tbl);
  489. pkey_tbl->tbl = NULL;
  490. pkey_tbl->max = 0;
  491. pkey_tbl->active = 0;
  492. }
  493. static int bnxt_qplib_alloc_pkey_tbl(struct bnxt_qplib_res *res,
  494. struct bnxt_qplib_pkey_tbl *pkey_tbl,
  495. u16 max)
  496. {
  497. pkey_tbl->tbl = kcalloc(max, sizeof(u16), GFP_KERNEL);
  498. if (!pkey_tbl->tbl)
  499. return -ENOMEM;
  500. pkey_tbl->max = max;
  501. return 0;
  502. };
  503. /* PDs */
  504. int bnxt_qplib_alloc_pd(struct bnxt_qplib_pd_tbl *pdt, struct bnxt_qplib_pd *pd)
  505. {
  506. u32 bit_num;
  507. bit_num = find_first_bit(pdt->tbl, pdt->max);
  508. if (bit_num == pdt->max)
  509. return -ENOMEM;
  510. /* Found unused PD */
  511. clear_bit(bit_num, pdt->tbl);
  512. pd->id = bit_num;
  513. return 0;
  514. }
  515. int bnxt_qplib_dealloc_pd(struct bnxt_qplib_res *res,
  516. struct bnxt_qplib_pd_tbl *pdt,
  517. struct bnxt_qplib_pd *pd)
  518. {
  519. if (test_and_set_bit(pd->id, pdt->tbl)) {
  520. dev_warn(&res->pdev->dev, "Freeing an unused PD? pdn = %d\n",
  521. pd->id);
  522. return -EINVAL;
  523. }
  524. pd->id = 0;
  525. return 0;
  526. }
  527. static void bnxt_qplib_free_pd_tbl(struct bnxt_qplib_pd_tbl *pdt)
  528. {
  529. kfree(pdt->tbl);
  530. pdt->tbl = NULL;
  531. pdt->max = 0;
  532. }
  533. static int bnxt_qplib_alloc_pd_tbl(struct bnxt_qplib_res *res,
  534. struct bnxt_qplib_pd_tbl *pdt,
  535. u32 max)
  536. {
  537. u32 bytes;
  538. bytes = max >> 3;
  539. if (!bytes)
  540. bytes = 1;
  541. pdt->tbl = kmalloc(bytes, GFP_KERNEL);
  542. if (!pdt->tbl)
  543. return -ENOMEM;
  544. pdt->max = max;
  545. memset((u8 *)pdt->tbl, 0xFF, bytes);
  546. return 0;
  547. }
  548. /* DPIs */
  549. int bnxt_qplib_alloc_dpi(struct bnxt_qplib_dpi_tbl *dpit,
  550. struct bnxt_qplib_dpi *dpi,
  551. void *app)
  552. {
  553. u32 bit_num;
  554. bit_num = find_first_bit(dpit->tbl, dpit->max);
  555. if (bit_num == dpit->max)
  556. return -ENOMEM;
  557. /* Found unused DPI */
  558. clear_bit(bit_num, dpit->tbl);
  559. dpit->app_tbl[bit_num] = app;
  560. dpi->dpi = bit_num;
  561. dpi->dbr = dpit->dbr_bar_reg_iomem + (bit_num * PAGE_SIZE);
  562. dpi->umdbr = dpit->unmapped_dbr + (bit_num * PAGE_SIZE);
  563. return 0;
  564. }
  565. int bnxt_qplib_dealloc_dpi(struct bnxt_qplib_res *res,
  566. struct bnxt_qplib_dpi_tbl *dpit,
  567. struct bnxt_qplib_dpi *dpi)
  568. {
  569. if (dpi->dpi >= dpit->max) {
  570. dev_warn(&res->pdev->dev, "Invalid DPI? dpi = %d\n", dpi->dpi);
  571. return -EINVAL;
  572. }
  573. if (test_and_set_bit(dpi->dpi, dpit->tbl)) {
  574. dev_warn(&res->pdev->dev, "Freeing an unused DPI? dpi = %d\n",
  575. dpi->dpi);
  576. return -EINVAL;
  577. }
  578. if (dpit->app_tbl)
  579. dpit->app_tbl[dpi->dpi] = NULL;
  580. memset(dpi, 0, sizeof(*dpi));
  581. return 0;
  582. }
  583. static void bnxt_qplib_free_dpi_tbl(struct bnxt_qplib_res *res,
  584. struct bnxt_qplib_dpi_tbl *dpit)
  585. {
  586. kfree(dpit->tbl);
  587. kfree(dpit->app_tbl);
  588. if (dpit->dbr_bar_reg_iomem)
  589. pci_iounmap(res->pdev, dpit->dbr_bar_reg_iomem);
  590. memset(dpit, 0, sizeof(*dpit));
  591. }
  592. static int bnxt_qplib_alloc_dpi_tbl(struct bnxt_qplib_res *res,
  593. struct bnxt_qplib_dpi_tbl *dpit,
  594. u32 dbr_offset)
  595. {
  596. u32 dbr_bar_reg = RCFW_DBR_PCI_BAR_REGION;
  597. resource_size_t bar_reg_base;
  598. u32 dbr_len, bytes;
  599. if (dpit->dbr_bar_reg_iomem) {
  600. dev_err(&res->pdev->dev, "DBR BAR region %d already mapped\n",
  601. dbr_bar_reg);
  602. return -EALREADY;
  603. }
  604. bar_reg_base = pci_resource_start(res->pdev, dbr_bar_reg);
  605. if (!bar_reg_base) {
  606. dev_err(&res->pdev->dev, "BAR region %d resc start failed\n",
  607. dbr_bar_reg);
  608. return -ENOMEM;
  609. }
  610. dbr_len = pci_resource_len(res->pdev, dbr_bar_reg) - dbr_offset;
  611. if (!dbr_len || ((dbr_len & (PAGE_SIZE - 1)) != 0)) {
  612. dev_err(&res->pdev->dev, "Invalid DBR length %d\n", dbr_len);
  613. return -ENOMEM;
  614. }
  615. dpit->dbr_bar_reg_iomem = ioremap_nocache(bar_reg_base + dbr_offset,
  616. dbr_len);
  617. if (!dpit->dbr_bar_reg_iomem) {
  618. dev_err(&res->pdev->dev,
  619. "FP: DBR BAR region %d mapping failed\n", dbr_bar_reg);
  620. return -ENOMEM;
  621. }
  622. dpit->unmapped_dbr = bar_reg_base + dbr_offset;
  623. dpit->max = dbr_len / PAGE_SIZE;
  624. dpit->app_tbl = kcalloc(dpit->max, sizeof(void *), GFP_KERNEL);
  625. if (!dpit->app_tbl)
  626. goto unmap_io;
  627. bytes = dpit->max >> 3;
  628. if (!bytes)
  629. bytes = 1;
  630. dpit->tbl = kmalloc(bytes, GFP_KERNEL);
  631. if (!dpit->tbl) {
  632. kfree(dpit->app_tbl);
  633. dpit->app_tbl = NULL;
  634. goto unmap_io;
  635. }
  636. memset((u8 *)dpit->tbl, 0xFF, bytes);
  637. return 0;
  638. unmap_io:
  639. pci_iounmap(res->pdev, dpit->dbr_bar_reg_iomem);
  640. return -ENOMEM;
  641. }
  642. /* PKEYs */
  643. static void bnxt_qplib_cleanup_pkey_tbl(struct bnxt_qplib_pkey_tbl *pkey_tbl)
  644. {
  645. memset(pkey_tbl->tbl, 0, sizeof(u16) * pkey_tbl->max);
  646. pkey_tbl->active = 0;
  647. }
  648. static void bnxt_qplib_init_pkey_tbl(struct bnxt_qplib_res *res,
  649. struct bnxt_qplib_pkey_tbl *pkey_tbl)
  650. {
  651. u16 pkey = 0xFFFF;
  652. memset(pkey_tbl->tbl, 0, sizeof(u16) * pkey_tbl->max);
  653. /* pkey default = 0xFFFF */
  654. bnxt_qplib_add_pkey(res, pkey_tbl, &pkey, false);
  655. }
  656. /* Stats */
  657. static void bnxt_qplib_free_stats_ctx(struct pci_dev *pdev,
  658. struct bnxt_qplib_stats *stats)
  659. {
  660. if (stats->dma) {
  661. dma_free_coherent(&pdev->dev, stats->size,
  662. stats->dma, stats->dma_map);
  663. }
  664. memset(stats, 0, sizeof(*stats));
  665. stats->fw_id = -1;
  666. }
  667. static int bnxt_qplib_alloc_stats_ctx(struct pci_dev *pdev,
  668. struct bnxt_qplib_stats *stats)
  669. {
  670. memset(stats, 0, sizeof(*stats));
  671. stats->fw_id = -1;
  672. stats->size = sizeof(struct ctx_hw_stats);
  673. stats->dma = dma_alloc_coherent(&pdev->dev, stats->size,
  674. &stats->dma_map, GFP_KERNEL);
  675. if (!stats->dma) {
  676. dev_err(&pdev->dev, "Stats DMA allocation failed\n");
  677. return -ENOMEM;
  678. }
  679. return 0;
  680. }
  681. void bnxt_qplib_cleanup_res(struct bnxt_qplib_res *res)
  682. {
  683. bnxt_qplib_cleanup_pkey_tbl(&res->pkey_tbl);
  684. bnxt_qplib_cleanup_sgid_tbl(res, &res->sgid_tbl);
  685. }
  686. int bnxt_qplib_init_res(struct bnxt_qplib_res *res)
  687. {
  688. bnxt_qplib_init_sgid_tbl(&res->sgid_tbl, res->netdev);
  689. bnxt_qplib_init_pkey_tbl(res, &res->pkey_tbl);
  690. return 0;
  691. }
  692. void bnxt_qplib_free_res(struct bnxt_qplib_res *res)
  693. {
  694. bnxt_qplib_free_pkey_tbl(res, &res->pkey_tbl);
  695. bnxt_qplib_free_sgid_tbl(res, &res->sgid_tbl);
  696. bnxt_qplib_free_pd_tbl(&res->pd_tbl);
  697. bnxt_qplib_free_dpi_tbl(res, &res->dpi_tbl);
  698. res->netdev = NULL;
  699. res->pdev = NULL;
  700. }
  701. int bnxt_qplib_alloc_res(struct bnxt_qplib_res *res, struct pci_dev *pdev,
  702. struct net_device *netdev,
  703. struct bnxt_qplib_dev_attr *dev_attr)
  704. {
  705. int rc = 0;
  706. res->pdev = pdev;
  707. res->netdev = netdev;
  708. rc = bnxt_qplib_alloc_sgid_tbl(res, &res->sgid_tbl, dev_attr->max_sgid);
  709. if (rc)
  710. goto fail;
  711. rc = bnxt_qplib_alloc_pkey_tbl(res, &res->pkey_tbl, dev_attr->max_pkey);
  712. if (rc)
  713. goto fail;
  714. rc = bnxt_qplib_alloc_pd_tbl(res, &res->pd_tbl, dev_attr->max_pd);
  715. if (rc)
  716. goto fail;
  717. rc = bnxt_qplib_alloc_dpi_tbl(res, &res->dpi_tbl, dev_attr->l2_db_size);
  718. if (rc)
  719. goto fail;
  720. return 0;
  721. fail:
  722. bnxt_qplib_free_res(res);
  723. return rc;
  724. }