qplib_sp.c 25 KB

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