qplib_sp.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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: Slow Path Operators
  37. */
  38. #include <linux/interrupt.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/sched.h>
  41. #include <linux/pci.h>
  42. #include "roce_hsi.h"
  43. #include "qplib_res.h"
  44. #include "qplib_rcfw.h"
  45. #include "qplib_sp.h"
  46. const struct bnxt_qplib_gid bnxt_qplib_gid_zero = {{ 0, 0, 0, 0, 0, 0, 0, 0,
  47. 0, 0, 0, 0, 0, 0, 0, 0 } };
  48. /* Device */
  49. static void bnxt_qplib_query_version(struct bnxt_qplib_rcfw *rcfw,
  50. char *fw_ver)
  51. {
  52. struct cmdq_query_version req;
  53. struct creq_query_version_resp resp;
  54. u16 cmd_flags = 0;
  55. int rc = 0;
  56. RCFW_CMD_PREP(req, QUERY_VERSION, cmd_flags);
  57. rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
  58. (void *)&resp, NULL, 0);
  59. if (rc)
  60. return;
  61. fw_ver[0] = resp.fw_maj;
  62. fw_ver[1] = resp.fw_minor;
  63. fw_ver[2] = resp.fw_bld;
  64. fw_ver[3] = resp.fw_rsvd;
  65. }
  66. int bnxt_qplib_get_dev_attr(struct bnxt_qplib_rcfw *rcfw,
  67. struct bnxt_qplib_dev_attr *attr, bool vf)
  68. {
  69. struct cmdq_query_func req;
  70. struct creq_query_func_resp resp;
  71. struct bnxt_qplib_rcfw_sbuf *sbuf;
  72. struct creq_query_func_resp_sb *sb;
  73. u16 cmd_flags = 0;
  74. u32 temp;
  75. u8 *tqm_alloc;
  76. int i, rc = 0;
  77. RCFW_CMD_PREP(req, QUERY_FUNC, cmd_flags);
  78. sbuf = bnxt_qplib_rcfw_alloc_sbuf(rcfw, sizeof(*sb));
  79. if (!sbuf) {
  80. dev_err(&rcfw->pdev->dev,
  81. "QPLIB: SP: QUERY_FUNC alloc side buffer failed");
  82. return -ENOMEM;
  83. }
  84. sb = sbuf->sb;
  85. req.resp_size = sizeof(*sb) / BNXT_QPLIB_CMDQE_UNITS;
  86. rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
  87. (void *)sbuf, 0);
  88. if (rc)
  89. goto bail;
  90. /* Extract the context from the side buffer */
  91. attr->max_qp = le32_to_cpu(sb->max_qp);
  92. /* max_qp value reported by FW for PF doesn't include the QP1 for PF */
  93. if (!vf)
  94. attr->max_qp += 1;
  95. attr->max_qp_rd_atom =
  96. sb->max_qp_rd_atom > BNXT_QPLIB_MAX_OUT_RD_ATOM ?
  97. BNXT_QPLIB_MAX_OUT_RD_ATOM : sb->max_qp_rd_atom;
  98. attr->max_qp_init_rd_atom =
  99. sb->max_qp_init_rd_atom > BNXT_QPLIB_MAX_OUT_RD_ATOM ?
  100. BNXT_QPLIB_MAX_OUT_RD_ATOM : sb->max_qp_init_rd_atom;
  101. attr->max_qp_wqes = le16_to_cpu(sb->max_qp_wr);
  102. /*
  103. * 128 WQEs needs to be reserved for the HW (8916). Prevent
  104. * reporting the max number
  105. */
  106. attr->max_qp_wqes -= BNXT_QPLIB_RESERVED_QP_WRS;
  107. attr->max_qp_sges = sb->max_sge;
  108. attr->max_cq = le32_to_cpu(sb->max_cq);
  109. attr->max_cq_wqes = le32_to_cpu(sb->max_cqe);
  110. attr->max_cq_sges = attr->max_qp_sges;
  111. attr->max_mr = le32_to_cpu(sb->max_mr);
  112. attr->max_mw = le32_to_cpu(sb->max_mw);
  113. attr->max_mr_size = le64_to_cpu(sb->max_mr_size);
  114. attr->max_pd = 64 * 1024;
  115. attr->max_raw_ethy_qp = le32_to_cpu(sb->max_raw_eth_qp);
  116. attr->max_ah = le32_to_cpu(sb->max_ah);
  117. attr->max_fmr = le32_to_cpu(sb->max_fmr);
  118. attr->max_map_per_fmr = sb->max_map_per_fmr;
  119. attr->max_srq = le16_to_cpu(sb->max_srq);
  120. attr->max_srq_wqes = le32_to_cpu(sb->max_srq_wr) - 1;
  121. attr->max_srq_sges = sb->max_srq_sge;
  122. /* Bono only reports 1 PKEY for now, but it can support > 1 */
  123. attr->max_pkey = le32_to_cpu(sb->max_pkeys);
  124. attr->max_inline_data = le32_to_cpu(sb->max_inline_data);
  125. attr->l2_db_size = (sb->l2_db_space_size + 1) *
  126. (0x01 << RCFW_DBR_BASE_PAGE_SHIFT);
  127. attr->max_sgid = le32_to_cpu(sb->max_gid);
  128. bnxt_qplib_query_version(rcfw, attr->fw_ver);
  129. for (i = 0; i < MAX_TQM_ALLOC_REQ / 4; i++) {
  130. temp = le32_to_cpu(sb->tqm_alloc_reqs[i]);
  131. tqm_alloc = (u8 *)&temp;
  132. attr->tqm_alloc_reqs[i * 4] = *tqm_alloc;
  133. attr->tqm_alloc_reqs[i * 4 + 1] = *(++tqm_alloc);
  134. attr->tqm_alloc_reqs[i * 4 + 2] = *(++tqm_alloc);
  135. attr->tqm_alloc_reqs[i * 4 + 3] = *(++tqm_alloc);
  136. }
  137. attr->is_atomic = false;
  138. bail:
  139. bnxt_qplib_rcfw_free_sbuf(rcfw, sbuf);
  140. return rc;
  141. }
  142. int bnxt_qplib_set_func_resources(struct bnxt_qplib_res *res,
  143. struct bnxt_qplib_rcfw *rcfw,
  144. struct bnxt_qplib_ctx *ctx)
  145. {
  146. struct cmdq_set_func_resources req;
  147. struct creq_set_func_resources_resp resp;
  148. u16 cmd_flags = 0;
  149. int rc = 0;
  150. RCFW_CMD_PREP(req, SET_FUNC_RESOURCES, cmd_flags);
  151. req.number_of_qp = cpu_to_le32(ctx->qpc_count);
  152. req.number_of_mrw = cpu_to_le32(ctx->mrw_count);
  153. req.number_of_srq = cpu_to_le32(ctx->srqc_count);
  154. req.number_of_cq = cpu_to_le32(ctx->cq_count);
  155. req.max_qp_per_vf = cpu_to_le32(ctx->vf_res.max_qp_per_vf);
  156. req.max_mrw_per_vf = cpu_to_le32(ctx->vf_res.max_mrw_per_vf);
  157. req.max_srq_per_vf = cpu_to_le32(ctx->vf_res.max_srq_per_vf);
  158. req.max_cq_per_vf = cpu_to_le32(ctx->vf_res.max_cq_per_vf);
  159. req.max_gid_per_vf = cpu_to_le32(ctx->vf_res.max_gid_per_vf);
  160. rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
  161. (void *)&resp,
  162. NULL, 0);
  163. if (rc) {
  164. dev_err(&res->pdev->dev,
  165. "QPLIB: Failed to set function resources");
  166. }
  167. return rc;
  168. }
  169. /* SGID */
  170. int bnxt_qplib_get_sgid(struct bnxt_qplib_res *res,
  171. struct bnxt_qplib_sgid_tbl *sgid_tbl, int index,
  172. struct bnxt_qplib_gid *gid)
  173. {
  174. if (index > sgid_tbl->max) {
  175. dev_err(&res->pdev->dev,
  176. "QPLIB: Index %d exceeded SGID table max (%d)",
  177. index, sgid_tbl->max);
  178. return -EINVAL;
  179. }
  180. memcpy(gid, &sgid_tbl->tbl[index], sizeof(*gid));
  181. return 0;
  182. }
  183. int bnxt_qplib_del_sgid(struct bnxt_qplib_sgid_tbl *sgid_tbl,
  184. struct bnxt_qplib_gid *gid, bool update)
  185. {
  186. struct bnxt_qplib_res *res = to_bnxt_qplib(sgid_tbl,
  187. struct bnxt_qplib_res,
  188. sgid_tbl);
  189. struct bnxt_qplib_rcfw *rcfw = res->rcfw;
  190. int index;
  191. if (!sgid_tbl) {
  192. dev_err(&res->pdev->dev, "QPLIB: SGID table not allocated");
  193. return -EINVAL;
  194. }
  195. /* Do we need a sgid_lock here? */
  196. if (!sgid_tbl->active) {
  197. dev_err(&res->pdev->dev,
  198. "QPLIB: SGID table has no active entries");
  199. return -ENOMEM;
  200. }
  201. for (index = 0; index < sgid_tbl->max; index++) {
  202. if (!memcmp(&sgid_tbl->tbl[index], gid, sizeof(*gid)))
  203. break;
  204. }
  205. if (index == sgid_tbl->max) {
  206. dev_warn(&res->pdev->dev, "GID not found in the SGID table");
  207. return 0;
  208. }
  209. /* Remove GID from the SGID table */
  210. if (update) {
  211. struct cmdq_delete_gid req;
  212. struct creq_delete_gid_resp resp;
  213. u16 cmd_flags = 0;
  214. int rc;
  215. RCFW_CMD_PREP(req, DELETE_GID, cmd_flags);
  216. if (sgid_tbl->hw_id[index] == 0xFFFF) {
  217. dev_err(&res->pdev->dev,
  218. "QPLIB: GID entry contains an invalid HW id");
  219. return -EINVAL;
  220. }
  221. req.gid_index = cpu_to_le16(sgid_tbl->hw_id[index]);
  222. rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
  223. (void *)&resp, NULL, 0);
  224. if (rc)
  225. return rc;
  226. }
  227. memcpy(&sgid_tbl->tbl[index], &bnxt_qplib_gid_zero,
  228. sizeof(bnxt_qplib_gid_zero));
  229. sgid_tbl->vlan[index] = 0;
  230. sgid_tbl->active--;
  231. dev_dbg(&res->pdev->dev,
  232. "QPLIB: SGID deleted hw_id[0x%x] = 0x%x active = 0x%x",
  233. index, sgid_tbl->hw_id[index], sgid_tbl->active);
  234. sgid_tbl->hw_id[index] = (u16)-1;
  235. /* unlock */
  236. return 0;
  237. }
  238. int bnxt_qplib_add_sgid(struct bnxt_qplib_sgid_tbl *sgid_tbl,
  239. struct bnxt_qplib_gid *gid, u8 *smac, u16 vlan_id,
  240. bool update, u32 *index)
  241. {
  242. struct bnxt_qplib_res *res = to_bnxt_qplib(sgid_tbl,
  243. struct bnxt_qplib_res,
  244. sgid_tbl);
  245. struct bnxt_qplib_rcfw *rcfw = res->rcfw;
  246. int i, free_idx;
  247. if (!sgid_tbl) {
  248. dev_err(&res->pdev->dev, "QPLIB: SGID table not allocated");
  249. return -EINVAL;
  250. }
  251. /* Do we need a sgid_lock here? */
  252. if (sgid_tbl->active == sgid_tbl->max) {
  253. dev_err(&res->pdev->dev, "QPLIB: SGID table is full");
  254. return -ENOMEM;
  255. }
  256. free_idx = sgid_tbl->max;
  257. for (i = 0; i < sgid_tbl->max; i++) {
  258. if (!memcmp(&sgid_tbl->tbl[i], gid, sizeof(*gid))) {
  259. dev_dbg(&res->pdev->dev,
  260. "QPLIB: SGID entry already exist in entry %d!",
  261. i);
  262. *index = i;
  263. return -EALREADY;
  264. } else if (!memcmp(&sgid_tbl->tbl[i], &bnxt_qplib_gid_zero,
  265. sizeof(bnxt_qplib_gid_zero)) &&
  266. free_idx == sgid_tbl->max) {
  267. free_idx = i;
  268. }
  269. }
  270. if (free_idx == sgid_tbl->max) {
  271. dev_err(&res->pdev->dev,
  272. "QPLIB: SGID table is FULL but count is not MAX??");
  273. return -ENOMEM;
  274. }
  275. if (update) {
  276. struct cmdq_add_gid req;
  277. struct creq_add_gid_resp resp;
  278. u16 cmd_flags = 0;
  279. int rc;
  280. RCFW_CMD_PREP(req, ADD_GID, cmd_flags);
  281. req.gid[0] = cpu_to_be32(((u32 *)gid->data)[3]);
  282. req.gid[1] = cpu_to_be32(((u32 *)gid->data)[2]);
  283. req.gid[2] = cpu_to_be32(((u32 *)gid->data)[1]);
  284. req.gid[3] = cpu_to_be32(((u32 *)gid->data)[0]);
  285. /*
  286. * driver should ensure that all RoCE traffic is always VLAN
  287. * tagged if RoCE traffic is running on non-zero VLAN ID or
  288. * RoCE traffic is running on non-zero Priority.
  289. */
  290. if ((vlan_id != 0xFFFF) || res->prio) {
  291. if (vlan_id != 0xFFFF)
  292. req.vlan = cpu_to_le16
  293. (vlan_id & CMDQ_ADD_GID_VLAN_VLAN_ID_MASK);
  294. req.vlan |= cpu_to_le16
  295. (CMDQ_ADD_GID_VLAN_TPID_TPID_8100 |
  296. CMDQ_ADD_GID_VLAN_VLAN_EN);
  297. }
  298. /* MAC in network format */
  299. req.src_mac[0] = cpu_to_be16(((u16 *)smac)[0]);
  300. req.src_mac[1] = cpu_to_be16(((u16 *)smac)[1]);
  301. req.src_mac[2] = cpu_to_be16(((u16 *)smac)[2]);
  302. rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
  303. (void *)&resp, NULL, 0);
  304. if (rc)
  305. return rc;
  306. sgid_tbl->hw_id[free_idx] = le32_to_cpu(resp.xid);
  307. }
  308. /* Add GID to the sgid_tbl */
  309. memcpy(&sgid_tbl->tbl[free_idx], gid, sizeof(*gid));
  310. sgid_tbl->active++;
  311. if (vlan_id != 0xFFFF)
  312. sgid_tbl->vlan[free_idx] = 1;
  313. dev_dbg(&res->pdev->dev,
  314. "QPLIB: SGID added hw_id[0x%x] = 0x%x active = 0x%x",
  315. free_idx, sgid_tbl->hw_id[free_idx], sgid_tbl->active);
  316. *index = free_idx;
  317. /* unlock */
  318. return 0;
  319. }
  320. int bnxt_qplib_update_sgid(struct bnxt_qplib_sgid_tbl *sgid_tbl,
  321. struct bnxt_qplib_gid *gid, u16 gid_idx,
  322. u8 *smac)
  323. {
  324. struct bnxt_qplib_res *res = to_bnxt_qplib(sgid_tbl,
  325. struct bnxt_qplib_res,
  326. sgid_tbl);
  327. struct bnxt_qplib_rcfw *rcfw = res->rcfw;
  328. struct creq_modify_gid_resp resp;
  329. struct cmdq_modify_gid req;
  330. int rc;
  331. u16 cmd_flags = 0;
  332. RCFW_CMD_PREP(req, MODIFY_GID, cmd_flags);
  333. req.gid[0] = cpu_to_be32(((u32 *)gid->data)[3]);
  334. req.gid[1] = cpu_to_be32(((u32 *)gid->data)[2]);
  335. req.gid[2] = cpu_to_be32(((u32 *)gid->data)[1]);
  336. req.gid[3] = cpu_to_be32(((u32 *)gid->data)[0]);
  337. if (res->prio) {
  338. req.vlan |= cpu_to_le16
  339. (CMDQ_ADD_GID_VLAN_TPID_TPID_8100 |
  340. CMDQ_ADD_GID_VLAN_VLAN_EN);
  341. }
  342. /* MAC in network format */
  343. req.src_mac[0] = cpu_to_be16(((u16 *)smac)[0]);
  344. req.src_mac[1] = cpu_to_be16(((u16 *)smac)[1]);
  345. req.src_mac[2] = cpu_to_be16(((u16 *)smac)[2]);
  346. req.gid_index = cpu_to_le16(gid_idx);
  347. rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
  348. (void *)&resp, NULL, 0);
  349. return rc;
  350. }
  351. /* pkeys */
  352. int bnxt_qplib_get_pkey(struct bnxt_qplib_res *res,
  353. struct bnxt_qplib_pkey_tbl *pkey_tbl, u16 index,
  354. u16 *pkey)
  355. {
  356. if (index == 0xFFFF) {
  357. *pkey = 0xFFFF;
  358. return 0;
  359. }
  360. if (index > pkey_tbl->max) {
  361. dev_err(&res->pdev->dev,
  362. "QPLIB: Index %d exceeded PKEY table max (%d)",
  363. index, pkey_tbl->max);
  364. return -EINVAL;
  365. }
  366. memcpy(pkey, &pkey_tbl->tbl[index], sizeof(*pkey));
  367. return 0;
  368. }
  369. int bnxt_qplib_del_pkey(struct bnxt_qplib_res *res,
  370. struct bnxt_qplib_pkey_tbl *pkey_tbl, u16 *pkey,
  371. bool update)
  372. {
  373. int i, rc = 0;
  374. if (!pkey_tbl) {
  375. dev_err(&res->pdev->dev, "QPLIB: PKEY table not allocated");
  376. return -EINVAL;
  377. }
  378. /* Do we need a pkey_lock here? */
  379. if (!pkey_tbl->active) {
  380. dev_err(&res->pdev->dev,
  381. "QPLIB: PKEY table has no active entries");
  382. return -ENOMEM;
  383. }
  384. for (i = 0; i < pkey_tbl->max; i++) {
  385. if (!memcmp(&pkey_tbl->tbl[i], pkey, sizeof(*pkey)))
  386. break;
  387. }
  388. if (i == pkey_tbl->max) {
  389. dev_err(&res->pdev->dev,
  390. "QPLIB: PKEY 0x%04x not found in the pkey table",
  391. *pkey);
  392. return -ENOMEM;
  393. }
  394. memset(&pkey_tbl->tbl[i], 0, sizeof(*pkey));
  395. pkey_tbl->active--;
  396. /* unlock */
  397. return rc;
  398. }
  399. int bnxt_qplib_add_pkey(struct bnxt_qplib_res *res,
  400. struct bnxt_qplib_pkey_tbl *pkey_tbl, u16 *pkey,
  401. bool update)
  402. {
  403. int i, free_idx, rc = 0;
  404. if (!pkey_tbl) {
  405. dev_err(&res->pdev->dev, "QPLIB: PKEY table not allocated");
  406. return -EINVAL;
  407. }
  408. /* Do we need a pkey_lock here? */
  409. if (pkey_tbl->active == pkey_tbl->max) {
  410. dev_err(&res->pdev->dev, "QPLIB: PKEY table is full");
  411. return -ENOMEM;
  412. }
  413. free_idx = pkey_tbl->max;
  414. for (i = 0; i < pkey_tbl->max; i++) {
  415. if (!memcmp(&pkey_tbl->tbl[i], pkey, sizeof(*pkey)))
  416. return -EALREADY;
  417. else if (!pkey_tbl->tbl[i] && free_idx == pkey_tbl->max)
  418. free_idx = i;
  419. }
  420. if (free_idx == pkey_tbl->max) {
  421. dev_err(&res->pdev->dev,
  422. "QPLIB: PKEY table is FULL but count is not MAX??");
  423. return -ENOMEM;
  424. }
  425. /* Add PKEY to the pkey_tbl */
  426. memcpy(&pkey_tbl->tbl[free_idx], pkey, sizeof(*pkey));
  427. pkey_tbl->active++;
  428. /* unlock */
  429. return rc;
  430. }
  431. /* AH */
  432. int bnxt_qplib_create_ah(struct bnxt_qplib_res *res, struct bnxt_qplib_ah *ah)
  433. {
  434. struct bnxt_qplib_rcfw *rcfw = res->rcfw;
  435. struct cmdq_create_ah req;
  436. struct creq_create_ah_resp resp;
  437. u16 cmd_flags = 0;
  438. u32 temp32[4];
  439. u16 temp16[3];
  440. int rc;
  441. RCFW_CMD_PREP(req, CREATE_AH, cmd_flags);
  442. memcpy(temp32, ah->dgid.data, sizeof(struct bnxt_qplib_gid));
  443. req.dgid[0] = cpu_to_le32(temp32[0]);
  444. req.dgid[1] = cpu_to_le32(temp32[1]);
  445. req.dgid[2] = cpu_to_le32(temp32[2]);
  446. req.dgid[3] = cpu_to_le32(temp32[3]);
  447. req.type = ah->nw_type;
  448. req.hop_limit = ah->hop_limit;
  449. req.sgid_index = cpu_to_le16(res->sgid_tbl.hw_id[ah->sgid_index]);
  450. req.dest_vlan_id_flow_label = cpu_to_le32((ah->flow_label &
  451. CMDQ_CREATE_AH_FLOW_LABEL_MASK) |
  452. CMDQ_CREATE_AH_DEST_VLAN_ID_MASK);
  453. req.pd_id = cpu_to_le32(ah->pd->id);
  454. req.traffic_class = ah->traffic_class;
  455. /* MAC in network format */
  456. memcpy(temp16, ah->dmac, 6);
  457. req.dest_mac[0] = cpu_to_le16(temp16[0]);
  458. req.dest_mac[1] = cpu_to_le16(temp16[1]);
  459. req.dest_mac[2] = cpu_to_le16(temp16[2]);
  460. rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
  461. NULL, 1);
  462. if (rc)
  463. return rc;
  464. ah->id = le32_to_cpu(resp.xid);
  465. return 0;
  466. }
  467. int bnxt_qplib_destroy_ah(struct bnxt_qplib_res *res, struct bnxt_qplib_ah *ah)
  468. {
  469. struct bnxt_qplib_rcfw *rcfw = res->rcfw;
  470. struct cmdq_destroy_ah req;
  471. struct creq_destroy_ah_resp resp;
  472. u16 cmd_flags = 0;
  473. int rc;
  474. /* Clean up the AH table in the device */
  475. RCFW_CMD_PREP(req, DESTROY_AH, cmd_flags);
  476. req.ah_cid = cpu_to_le32(ah->id);
  477. rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
  478. NULL, 1);
  479. if (rc)
  480. return rc;
  481. return 0;
  482. }
  483. /* MRW */
  484. int bnxt_qplib_free_mrw(struct bnxt_qplib_res *res, struct bnxt_qplib_mrw *mrw)
  485. {
  486. struct bnxt_qplib_rcfw *rcfw = res->rcfw;
  487. struct cmdq_deallocate_key req;
  488. struct creq_deallocate_key_resp resp;
  489. u16 cmd_flags = 0;
  490. int rc;
  491. if (mrw->lkey == 0xFFFFFFFF) {
  492. dev_info(&res->pdev->dev,
  493. "QPLIB: SP: Free a reserved lkey MRW");
  494. return 0;
  495. }
  496. RCFW_CMD_PREP(req, DEALLOCATE_KEY, cmd_flags);
  497. req.mrw_flags = mrw->type;
  498. if ((mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE1) ||
  499. (mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2A) ||
  500. (mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2B))
  501. req.key = cpu_to_le32(mrw->rkey);
  502. else
  503. req.key = cpu_to_le32(mrw->lkey);
  504. rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
  505. NULL, 0);
  506. if (rc)
  507. return rc;
  508. /* Free the qplib's MRW memory */
  509. if (mrw->hwq.max_elements)
  510. bnxt_qplib_free_hwq(res->pdev, &mrw->hwq);
  511. return 0;
  512. }
  513. int bnxt_qplib_alloc_mrw(struct bnxt_qplib_res *res, struct bnxt_qplib_mrw *mrw)
  514. {
  515. struct bnxt_qplib_rcfw *rcfw = res->rcfw;
  516. struct cmdq_allocate_mrw req;
  517. struct creq_allocate_mrw_resp resp;
  518. u16 cmd_flags = 0;
  519. unsigned long tmp;
  520. int rc;
  521. RCFW_CMD_PREP(req, ALLOCATE_MRW, cmd_flags);
  522. req.pd_id = cpu_to_le32(mrw->pd->id);
  523. req.mrw_flags = mrw->type;
  524. if ((mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_PMR &&
  525. mrw->flags & BNXT_QPLIB_FR_PMR) ||
  526. mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2A ||
  527. mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2B)
  528. req.access = CMDQ_ALLOCATE_MRW_ACCESS_CONSUMER_OWNED_KEY;
  529. tmp = (unsigned long)mrw;
  530. req.mrw_handle = cpu_to_le64(tmp);
  531. rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
  532. (void *)&resp, NULL, 0);
  533. if (rc)
  534. return rc;
  535. if ((mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE1) ||
  536. (mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2A) ||
  537. (mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2B))
  538. mrw->rkey = le32_to_cpu(resp.xid);
  539. else
  540. mrw->lkey = le32_to_cpu(resp.xid);
  541. return 0;
  542. }
  543. int bnxt_qplib_dereg_mrw(struct bnxt_qplib_res *res, struct bnxt_qplib_mrw *mrw,
  544. bool block)
  545. {
  546. struct bnxt_qplib_rcfw *rcfw = res->rcfw;
  547. struct cmdq_deregister_mr req;
  548. struct creq_deregister_mr_resp resp;
  549. u16 cmd_flags = 0;
  550. int rc;
  551. RCFW_CMD_PREP(req, DEREGISTER_MR, cmd_flags);
  552. req.lkey = cpu_to_le32(mrw->lkey);
  553. rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
  554. (void *)&resp, NULL, block);
  555. if (rc)
  556. return rc;
  557. /* Free the qplib's MR memory */
  558. if (mrw->hwq.max_elements) {
  559. mrw->va = 0;
  560. mrw->total_size = 0;
  561. bnxt_qplib_free_hwq(res->pdev, &mrw->hwq);
  562. }
  563. return 0;
  564. }
  565. int bnxt_qplib_reg_mr(struct bnxt_qplib_res *res, struct bnxt_qplib_mrw *mr,
  566. u64 *pbl_tbl, int num_pbls, bool block, u32 buf_pg_size)
  567. {
  568. struct bnxt_qplib_rcfw *rcfw = res->rcfw;
  569. struct cmdq_register_mr req;
  570. struct creq_register_mr_resp resp;
  571. u16 cmd_flags = 0, level;
  572. int pg_ptrs, pages, i, rc;
  573. dma_addr_t **pbl_ptr;
  574. u32 pg_size;
  575. if (num_pbls) {
  576. /* Allocate memory for the non-leaf pages to store buf ptrs.
  577. * Non-leaf pages always uses system PAGE_SIZE
  578. */
  579. pg_ptrs = roundup_pow_of_two(num_pbls);
  580. pages = pg_ptrs >> MAX_PBL_LVL_1_PGS_SHIFT;
  581. if (!pages)
  582. pages++;
  583. if (pages > MAX_PBL_LVL_1_PGS) {
  584. dev_err(&res->pdev->dev, "QPLIB: SP: Reg MR pages ");
  585. dev_err(&res->pdev->dev,
  586. "requested (0x%x) exceeded max (0x%x)",
  587. pages, MAX_PBL_LVL_1_PGS);
  588. return -ENOMEM;
  589. }
  590. /* Free the hwq if it already exist, must be a rereg */
  591. if (mr->hwq.max_elements)
  592. bnxt_qplib_free_hwq(res->pdev, &mr->hwq);
  593. mr->hwq.max_elements = pages;
  594. /* Use system PAGE_SIZE */
  595. rc = bnxt_qplib_alloc_init_hwq(res->pdev, &mr->hwq, NULL, 0,
  596. &mr->hwq.max_elements,
  597. PAGE_SIZE, 0, PAGE_SIZE,
  598. HWQ_TYPE_CTX);
  599. if (rc) {
  600. dev_err(&res->pdev->dev,
  601. "SP: Reg MR memory allocation failed");
  602. return -ENOMEM;
  603. }
  604. /* Write to the hwq */
  605. pbl_ptr = (dma_addr_t **)mr->hwq.pbl_ptr;
  606. for (i = 0; i < num_pbls; i++)
  607. pbl_ptr[PTR_PG(i)][PTR_IDX(i)] =
  608. (pbl_tbl[i] & PAGE_MASK) | PTU_PTE_VALID;
  609. }
  610. RCFW_CMD_PREP(req, REGISTER_MR, cmd_flags);
  611. /* Configure the request */
  612. if (mr->hwq.level == PBL_LVL_MAX) {
  613. /* No PBL provided, just use system PAGE_SIZE */
  614. level = 0;
  615. req.pbl = 0;
  616. pg_size = PAGE_SIZE;
  617. } else {
  618. level = mr->hwq.level + 1;
  619. req.pbl = cpu_to_le64(mr->hwq.pbl[PBL_LVL_0].pg_map_arr[0]);
  620. }
  621. pg_size = buf_pg_size ? buf_pg_size : PAGE_SIZE;
  622. req.log2_pg_size_lvl = (level << CMDQ_REGISTER_MR_LVL_SFT) |
  623. ((ilog2(pg_size) <<
  624. CMDQ_REGISTER_MR_LOG2_PG_SIZE_SFT) &
  625. CMDQ_REGISTER_MR_LOG2_PG_SIZE_MASK);
  626. req.log2_pbl_pg_size = cpu_to_le16(((ilog2(PAGE_SIZE) <<
  627. CMDQ_REGISTER_MR_LOG2_PBL_PG_SIZE_SFT) &
  628. CMDQ_REGISTER_MR_LOG2_PBL_PG_SIZE_MASK));
  629. req.access = (mr->flags & 0xFFFF);
  630. req.va = cpu_to_le64(mr->va);
  631. req.key = cpu_to_le32(mr->lkey);
  632. req.mr_size = cpu_to_le64(mr->total_size);
  633. rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
  634. (void *)&resp, NULL, block);
  635. if (rc)
  636. goto fail;
  637. return 0;
  638. fail:
  639. if (mr->hwq.max_elements)
  640. bnxt_qplib_free_hwq(res->pdev, &mr->hwq);
  641. return rc;
  642. }
  643. int bnxt_qplib_alloc_fast_reg_page_list(struct bnxt_qplib_res *res,
  644. struct bnxt_qplib_frpl *frpl,
  645. int max_pg_ptrs)
  646. {
  647. int pg_ptrs, pages, rc;
  648. /* Re-calculate the max to fit the HWQ allocation model */
  649. pg_ptrs = roundup_pow_of_two(max_pg_ptrs);
  650. pages = pg_ptrs >> MAX_PBL_LVL_1_PGS_SHIFT;
  651. if (!pages)
  652. pages++;
  653. if (pages > MAX_PBL_LVL_1_PGS)
  654. return -ENOMEM;
  655. frpl->hwq.max_elements = pages;
  656. rc = bnxt_qplib_alloc_init_hwq(res->pdev, &frpl->hwq, NULL, 0,
  657. &frpl->hwq.max_elements, PAGE_SIZE, 0,
  658. PAGE_SIZE, HWQ_TYPE_CTX);
  659. if (!rc)
  660. frpl->max_pg_ptrs = pg_ptrs;
  661. return rc;
  662. }
  663. int bnxt_qplib_free_fast_reg_page_list(struct bnxt_qplib_res *res,
  664. struct bnxt_qplib_frpl *frpl)
  665. {
  666. bnxt_qplib_free_hwq(res->pdev, &frpl->hwq);
  667. return 0;
  668. }
  669. int bnxt_qplib_map_tc2cos(struct bnxt_qplib_res *res, u16 *cids)
  670. {
  671. struct bnxt_qplib_rcfw *rcfw = res->rcfw;
  672. struct cmdq_map_tc_to_cos req;
  673. struct creq_map_tc_to_cos_resp resp;
  674. u16 cmd_flags = 0;
  675. RCFW_CMD_PREP(req, MAP_TC_TO_COS, cmd_flags);
  676. req.cos0 = cpu_to_le16(cids[0]);
  677. req.cos1 = cpu_to_le16(cids[1]);
  678. bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp, NULL,
  679. 0);
  680. return 0;
  681. }
  682. int bnxt_qplib_get_roce_stats(struct bnxt_qplib_rcfw *rcfw,
  683. struct bnxt_qplib_roce_stats *stats)
  684. {
  685. struct cmdq_query_roce_stats req;
  686. struct creq_query_roce_stats_resp resp;
  687. struct bnxt_qplib_rcfw_sbuf *sbuf;
  688. struct creq_query_roce_stats_resp_sb *sb;
  689. u16 cmd_flags = 0;
  690. int rc = 0;
  691. RCFW_CMD_PREP(req, QUERY_ROCE_STATS, cmd_flags);
  692. sbuf = bnxt_qplib_rcfw_alloc_sbuf(rcfw, sizeof(*sb));
  693. if (!sbuf) {
  694. dev_err(&rcfw->pdev->dev,
  695. "QPLIB: SP: QUERY_ROCE_STATS alloc side buffer failed");
  696. return -ENOMEM;
  697. }
  698. sb = sbuf->sb;
  699. req.resp_size = sizeof(*sb) / BNXT_QPLIB_CMDQE_UNITS;
  700. rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
  701. (void *)sbuf, 0);
  702. if (rc)
  703. goto bail;
  704. /* Extract the context from the side buffer */
  705. stats->to_retransmits = le64_to_cpu(sb->to_retransmits);
  706. stats->seq_err_naks_rcvd = le64_to_cpu(sb->seq_err_naks_rcvd);
  707. stats->max_retry_exceeded = le64_to_cpu(sb->max_retry_exceeded);
  708. stats->rnr_naks_rcvd = le64_to_cpu(sb->rnr_naks_rcvd);
  709. stats->missing_resp = le64_to_cpu(sb->missing_resp);
  710. stats->unrecoverable_err = le64_to_cpu(sb->unrecoverable_err);
  711. stats->bad_resp_err = le64_to_cpu(sb->bad_resp_err);
  712. stats->local_qp_op_err = le64_to_cpu(sb->local_qp_op_err);
  713. stats->local_protection_err = le64_to_cpu(sb->local_protection_err);
  714. stats->mem_mgmt_op_err = le64_to_cpu(sb->mem_mgmt_op_err);
  715. stats->remote_invalid_req_err = le64_to_cpu(sb->remote_invalid_req_err);
  716. stats->remote_access_err = le64_to_cpu(sb->remote_access_err);
  717. stats->remote_op_err = le64_to_cpu(sb->remote_op_err);
  718. stats->dup_req = le64_to_cpu(sb->dup_req);
  719. stats->res_exceed_max = le64_to_cpu(sb->res_exceed_max);
  720. stats->res_length_mismatch = le64_to_cpu(sb->res_length_mismatch);
  721. stats->res_exceeds_wqe = le64_to_cpu(sb->res_exceeds_wqe);
  722. stats->res_opcode_err = le64_to_cpu(sb->res_opcode_err);
  723. stats->res_rx_invalid_rkey = le64_to_cpu(sb->res_rx_invalid_rkey);
  724. stats->res_rx_domain_err = le64_to_cpu(sb->res_rx_domain_err);
  725. stats->res_rx_no_perm = le64_to_cpu(sb->res_rx_no_perm);
  726. stats->res_rx_range_err = le64_to_cpu(sb->res_rx_range_err);
  727. stats->res_tx_invalid_rkey = le64_to_cpu(sb->res_tx_invalid_rkey);
  728. stats->res_tx_domain_err = le64_to_cpu(sb->res_tx_domain_err);
  729. stats->res_tx_no_perm = le64_to_cpu(sb->res_tx_no_perm);
  730. stats->res_tx_range_err = le64_to_cpu(sb->res_tx_range_err);
  731. stats->res_irrq_oflow = le64_to_cpu(sb->res_irrq_oflow);
  732. stats->res_unsup_opcode = le64_to_cpu(sb->res_unsup_opcode);
  733. stats->res_unaligned_atomic = le64_to_cpu(sb->res_unaligned_atomic);
  734. stats->res_rem_inv_err = le64_to_cpu(sb->res_rem_inv_err);
  735. stats->res_mem_error = le64_to_cpu(sb->res_mem_error);
  736. stats->res_srq_err = le64_to_cpu(sb->res_srq_err);
  737. stats->res_cmp_err = le64_to_cpu(sb->res_cmp_err);
  738. stats->res_invalid_dup_rkey = le64_to_cpu(sb->res_invalid_dup_rkey);
  739. stats->res_wqe_format_err = le64_to_cpu(sb->res_wqe_format_err);
  740. stats->res_cq_load_err = le64_to_cpu(sb->res_cq_load_err);
  741. stats->res_srq_load_err = le64_to_cpu(sb->res_srq_load_err);
  742. stats->res_tx_pci_err = le64_to_cpu(sb->res_tx_pci_err);
  743. stats->res_rx_pci_err = le64_to_cpu(sb->res_rx_pci_err);
  744. bail:
  745. bnxt_qplib_rcfw_free_sbuf(rcfw, sbuf);
  746. return rc;
  747. }