restrack.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * Copyright (c) 2018 Chelsio, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <rdma/rdma_cm.h>
  33. #include "iw_cxgb4.h"
  34. #include <rdma/restrack.h>
  35. #include <uapi/rdma/rdma_netlink.h>
  36. static int fill_sq(struct sk_buff *msg, struct t4_wq *wq)
  37. {
  38. /* WQ+SQ */
  39. if (rdma_nl_put_driver_u32(msg, "sqid", wq->sq.qid))
  40. goto err;
  41. if (rdma_nl_put_driver_u32(msg, "flushed", wq->flushed))
  42. goto err;
  43. if (rdma_nl_put_driver_u32(msg, "memsize", wq->sq.memsize))
  44. goto err;
  45. if (rdma_nl_put_driver_u32(msg, "cidx", wq->sq.cidx))
  46. goto err;
  47. if (rdma_nl_put_driver_u32(msg, "pidx", wq->sq.pidx))
  48. goto err;
  49. if (rdma_nl_put_driver_u32(msg, "wq_pidx", wq->sq.wq_pidx))
  50. goto err;
  51. if (rdma_nl_put_driver_u32(msg, "flush_cidx", wq->sq.flush_cidx))
  52. goto err;
  53. if (rdma_nl_put_driver_u32(msg, "in_use", wq->sq.in_use))
  54. goto err;
  55. if (rdma_nl_put_driver_u32(msg, "size", wq->sq.size))
  56. goto err;
  57. if (rdma_nl_put_driver_u32_hex(msg, "flags", wq->sq.flags))
  58. goto err;
  59. return 0;
  60. err:
  61. return -EMSGSIZE;
  62. }
  63. static int fill_rq(struct sk_buff *msg, struct t4_wq *wq)
  64. {
  65. /* RQ */
  66. if (rdma_nl_put_driver_u32(msg, "rqid", wq->rq.qid))
  67. goto err;
  68. if (rdma_nl_put_driver_u32(msg, "memsize", wq->rq.memsize))
  69. goto err;
  70. if (rdma_nl_put_driver_u32(msg, "cidx", wq->rq.cidx))
  71. goto err;
  72. if (rdma_nl_put_driver_u32(msg, "pidx", wq->rq.pidx))
  73. goto err;
  74. if (rdma_nl_put_driver_u32(msg, "wq_pidx", wq->rq.wq_pidx))
  75. goto err;
  76. if (rdma_nl_put_driver_u32(msg, "msn", wq->rq.msn))
  77. goto err;
  78. if (rdma_nl_put_driver_u32_hex(msg, "rqt_hwaddr", wq->rq.rqt_hwaddr))
  79. goto err;
  80. if (rdma_nl_put_driver_u32(msg, "rqt_size", wq->rq.rqt_size))
  81. goto err;
  82. if (rdma_nl_put_driver_u32(msg, "in_use", wq->rq.in_use))
  83. goto err;
  84. if (rdma_nl_put_driver_u32(msg, "size", wq->rq.size))
  85. goto err;
  86. return 0;
  87. err:
  88. return -EMSGSIZE;
  89. }
  90. static int fill_swsqe(struct sk_buff *msg, struct t4_sq *sq, u16 idx,
  91. struct t4_swsqe *sqe)
  92. {
  93. if (rdma_nl_put_driver_u32(msg, "idx", idx))
  94. goto err;
  95. if (rdma_nl_put_driver_u32(msg, "opcode", sqe->opcode))
  96. goto err;
  97. if (rdma_nl_put_driver_u32(msg, "complete", sqe->complete))
  98. goto err;
  99. if (sqe->complete &&
  100. rdma_nl_put_driver_u32(msg, "cqe_status", CQE_STATUS(&sqe->cqe)))
  101. goto err;
  102. if (rdma_nl_put_driver_u32(msg, "signaled", sqe->signaled))
  103. goto err;
  104. if (rdma_nl_put_driver_u32(msg, "flushed", sqe->flushed))
  105. goto err;
  106. return 0;
  107. err:
  108. return -EMSGSIZE;
  109. }
  110. /*
  111. * Dump the first and last pending sqes.
  112. */
  113. static int fill_swsqes(struct sk_buff *msg, struct t4_sq *sq,
  114. u16 first_idx, struct t4_swsqe *first_sqe,
  115. u16 last_idx, struct t4_swsqe *last_sqe)
  116. {
  117. if (!first_sqe)
  118. goto out;
  119. if (fill_swsqe(msg, sq, first_idx, first_sqe))
  120. goto err;
  121. if (!last_sqe)
  122. goto out;
  123. if (fill_swsqe(msg, sq, last_idx, last_sqe))
  124. goto err;
  125. out:
  126. return 0;
  127. err:
  128. return -EMSGSIZE;
  129. }
  130. static int fill_res_qp_entry(struct sk_buff *msg,
  131. struct rdma_restrack_entry *res)
  132. {
  133. struct ib_qp *ibqp = container_of(res, struct ib_qp, res);
  134. struct t4_swsqe *fsp = NULL, *lsp = NULL;
  135. struct c4iw_qp *qhp = to_c4iw_qp(ibqp);
  136. u16 first_sq_idx = 0, last_sq_idx = 0;
  137. struct t4_swsqe first_sqe, last_sqe;
  138. struct nlattr *table_attr;
  139. struct t4_wq wq;
  140. /* User qp state is not available, so don't dump user qps */
  141. if (qhp->ucontext)
  142. return 0;
  143. table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_DRIVER);
  144. if (!table_attr)
  145. goto err;
  146. /* Get a consistent snapshot */
  147. spin_lock_irq(&qhp->lock);
  148. wq = qhp->wq;
  149. /* If there are any pending sqes, copy the first and last */
  150. if (wq.sq.cidx != wq.sq.pidx) {
  151. first_sq_idx = wq.sq.cidx;
  152. first_sqe = qhp->wq.sq.sw_sq[first_sq_idx];
  153. fsp = &first_sqe;
  154. last_sq_idx = wq.sq.pidx;
  155. if (last_sq_idx-- == 0)
  156. last_sq_idx = wq.sq.size - 1;
  157. if (last_sq_idx != first_sq_idx) {
  158. last_sqe = qhp->wq.sq.sw_sq[last_sq_idx];
  159. lsp = &last_sqe;
  160. }
  161. }
  162. spin_unlock_irq(&qhp->lock);
  163. if (fill_sq(msg, &wq))
  164. goto err_cancel_table;
  165. if (fill_swsqes(msg, &wq.sq, first_sq_idx, fsp, last_sq_idx, lsp))
  166. goto err_cancel_table;
  167. if (fill_rq(msg, &wq))
  168. goto err_cancel_table;
  169. nla_nest_end(msg, table_attr);
  170. return 0;
  171. err_cancel_table:
  172. nla_nest_cancel(msg, table_attr);
  173. err:
  174. return -EMSGSIZE;
  175. }
  176. union union_ep {
  177. struct c4iw_listen_ep lep;
  178. struct c4iw_ep ep;
  179. };
  180. static int fill_res_ep_entry(struct sk_buff *msg,
  181. struct rdma_restrack_entry *res)
  182. {
  183. struct rdma_cm_id *cm_id = rdma_res_to_id(res);
  184. struct nlattr *table_attr;
  185. struct c4iw_ep_common *epcp;
  186. struct c4iw_listen_ep *listen_ep = NULL;
  187. struct c4iw_ep *ep = NULL;
  188. struct iw_cm_id *iw_cm_id;
  189. union union_ep *uep;
  190. iw_cm_id = rdma_iw_cm_id(cm_id);
  191. if (!iw_cm_id)
  192. return 0;
  193. epcp = (struct c4iw_ep_common *)iw_cm_id->provider_data;
  194. if (!epcp)
  195. return 0;
  196. uep = kcalloc(1, sizeof(*uep), GFP_KERNEL);
  197. if (!uep)
  198. return 0;
  199. table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_DRIVER);
  200. if (!table_attr)
  201. goto err_free_uep;
  202. /* Get a consistent snapshot */
  203. mutex_lock(&epcp->mutex);
  204. if (epcp->state == LISTEN) {
  205. uep->lep = *(struct c4iw_listen_ep *)epcp;
  206. mutex_unlock(&epcp->mutex);
  207. listen_ep = &uep->lep;
  208. epcp = &listen_ep->com;
  209. } else {
  210. uep->ep = *(struct c4iw_ep *)epcp;
  211. mutex_unlock(&epcp->mutex);
  212. ep = &uep->ep;
  213. epcp = &ep->com;
  214. }
  215. if (rdma_nl_put_driver_u32(msg, "state", epcp->state))
  216. goto err_cancel_table;
  217. if (rdma_nl_put_driver_u64_hex(msg, "flags", epcp->flags))
  218. goto err_cancel_table;
  219. if (rdma_nl_put_driver_u64_hex(msg, "history", epcp->history))
  220. goto err_cancel_table;
  221. if (epcp->state == LISTEN) {
  222. if (rdma_nl_put_driver_u32(msg, "stid", listen_ep->stid))
  223. goto err_cancel_table;
  224. if (rdma_nl_put_driver_u32(msg, "backlog", listen_ep->backlog))
  225. goto err_cancel_table;
  226. } else {
  227. if (rdma_nl_put_driver_u32(msg, "hwtid", ep->hwtid))
  228. goto err_cancel_table;
  229. if (rdma_nl_put_driver_u32(msg, "ord", ep->ord))
  230. goto err_cancel_table;
  231. if (rdma_nl_put_driver_u32(msg, "ird", ep->ird))
  232. goto err_cancel_table;
  233. if (rdma_nl_put_driver_u32(msg, "emss", ep->emss))
  234. goto err_cancel_table;
  235. if (!ep->parent_ep && rdma_nl_put_driver_u32(msg, "atid",
  236. ep->atid))
  237. goto err_cancel_table;
  238. }
  239. nla_nest_end(msg, table_attr);
  240. kfree(uep);
  241. return 0;
  242. err_cancel_table:
  243. nla_nest_cancel(msg, table_attr);
  244. err_free_uep:
  245. kfree(uep);
  246. return -EMSGSIZE;
  247. }
  248. static int fill_cq(struct sk_buff *msg, struct t4_cq *cq)
  249. {
  250. if (rdma_nl_put_driver_u32(msg, "cqid", cq->cqid))
  251. goto err;
  252. if (rdma_nl_put_driver_u32(msg, "memsize", cq->memsize))
  253. goto err;
  254. if (rdma_nl_put_driver_u32(msg, "size", cq->size))
  255. goto err;
  256. if (rdma_nl_put_driver_u32(msg, "cidx", cq->cidx))
  257. goto err;
  258. if (rdma_nl_put_driver_u32(msg, "cidx_inc", cq->cidx_inc))
  259. goto err;
  260. if (rdma_nl_put_driver_u32(msg, "sw_cidx", cq->sw_cidx))
  261. goto err;
  262. if (rdma_nl_put_driver_u32(msg, "sw_pidx", cq->sw_pidx))
  263. goto err;
  264. if (rdma_nl_put_driver_u32(msg, "sw_in_use", cq->sw_in_use))
  265. goto err;
  266. if (rdma_nl_put_driver_u32(msg, "vector", cq->vector))
  267. goto err;
  268. if (rdma_nl_put_driver_u32(msg, "gen", cq->gen))
  269. goto err;
  270. if (rdma_nl_put_driver_u32(msg, "error", cq->error))
  271. goto err;
  272. if (rdma_nl_put_driver_u64_hex(msg, "bits_type_ts",
  273. be64_to_cpu(cq->bits_type_ts)))
  274. goto err;
  275. if (rdma_nl_put_driver_u64_hex(msg, "flags", cq->flags))
  276. goto err;
  277. return 0;
  278. err:
  279. return -EMSGSIZE;
  280. }
  281. static int fill_cqe(struct sk_buff *msg, struct t4_cqe *cqe, u16 idx,
  282. const char *qstr)
  283. {
  284. if (rdma_nl_put_driver_u32(msg, qstr, idx))
  285. goto err;
  286. if (rdma_nl_put_driver_u32_hex(msg, "header",
  287. be32_to_cpu(cqe->header)))
  288. goto err;
  289. if (rdma_nl_put_driver_u32(msg, "len", be32_to_cpu(cqe->len)))
  290. goto err;
  291. if (rdma_nl_put_driver_u32_hex(msg, "wrid_hi",
  292. be32_to_cpu(cqe->u.gen.wrid_hi)))
  293. goto err;
  294. if (rdma_nl_put_driver_u32_hex(msg, "wrid_low",
  295. be32_to_cpu(cqe->u.gen.wrid_low)))
  296. goto err;
  297. if (rdma_nl_put_driver_u64_hex(msg, "bits_type_ts",
  298. be64_to_cpu(cqe->bits_type_ts)))
  299. goto err;
  300. return 0;
  301. err:
  302. return -EMSGSIZE;
  303. }
  304. static int fill_hwcqes(struct sk_buff *msg, struct t4_cq *cq,
  305. struct t4_cqe *cqes)
  306. {
  307. u16 idx;
  308. idx = (cq->cidx > 0) ? cq->cidx - 1 : cq->size - 1;
  309. if (fill_cqe(msg, cqes, idx, "hwcq_idx"))
  310. goto err;
  311. idx = cq->cidx;
  312. if (fill_cqe(msg, cqes + 1, idx, "hwcq_idx"))
  313. goto err;
  314. return 0;
  315. err:
  316. return -EMSGSIZE;
  317. }
  318. static int fill_swcqes(struct sk_buff *msg, struct t4_cq *cq,
  319. struct t4_cqe *cqes)
  320. {
  321. u16 idx;
  322. if (!cq->sw_in_use)
  323. return 0;
  324. idx = cq->sw_cidx;
  325. if (fill_cqe(msg, cqes, idx, "swcq_idx"))
  326. goto err;
  327. if (cq->sw_in_use == 1)
  328. goto out;
  329. idx = (cq->sw_pidx > 0) ? cq->sw_pidx - 1 : cq->size - 1;
  330. if (fill_cqe(msg, cqes + 1, idx, "swcq_idx"))
  331. goto err;
  332. out:
  333. return 0;
  334. err:
  335. return -EMSGSIZE;
  336. }
  337. static int fill_res_cq_entry(struct sk_buff *msg,
  338. struct rdma_restrack_entry *res)
  339. {
  340. struct ib_cq *ibcq = container_of(res, struct ib_cq, res);
  341. struct c4iw_cq *chp = to_c4iw_cq(ibcq);
  342. struct nlattr *table_attr;
  343. struct t4_cqe hwcqes[2];
  344. struct t4_cqe swcqes[2];
  345. struct t4_cq cq;
  346. u16 idx;
  347. /* User cq state is not available, so don't dump user cqs */
  348. if (ibcq->uobject)
  349. return 0;
  350. table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_DRIVER);
  351. if (!table_attr)
  352. goto err;
  353. /* Get a consistent snapshot */
  354. spin_lock_irq(&chp->lock);
  355. /* t4_cq struct */
  356. cq = chp->cq;
  357. /* get 2 hw cqes: cidx-1, and cidx */
  358. idx = (cq.cidx > 0) ? cq.cidx - 1 : cq.size - 1;
  359. hwcqes[0] = chp->cq.queue[idx];
  360. idx = cq.cidx;
  361. hwcqes[1] = chp->cq.queue[idx];
  362. /* get first and last sw cqes */
  363. if (cq.sw_in_use) {
  364. swcqes[0] = chp->cq.sw_queue[cq.sw_cidx];
  365. if (cq.sw_in_use > 1) {
  366. idx = (cq.sw_pidx > 0) ? cq.sw_pidx - 1 : cq.size - 1;
  367. swcqes[1] = chp->cq.sw_queue[idx];
  368. }
  369. }
  370. spin_unlock_irq(&chp->lock);
  371. if (fill_cq(msg, &cq))
  372. goto err_cancel_table;
  373. if (fill_swcqes(msg, &cq, swcqes))
  374. goto err_cancel_table;
  375. if (fill_hwcqes(msg, &cq, hwcqes))
  376. goto err_cancel_table;
  377. nla_nest_end(msg, table_attr);
  378. return 0;
  379. err_cancel_table:
  380. nla_nest_cancel(msg, table_attr);
  381. err:
  382. return -EMSGSIZE;
  383. }
  384. static int fill_res_mr_entry(struct sk_buff *msg,
  385. struct rdma_restrack_entry *res)
  386. {
  387. struct ib_mr *ibmr = container_of(res, struct ib_mr, res);
  388. struct c4iw_mr *mhp = to_c4iw_mr(ibmr);
  389. struct c4iw_dev *dev = mhp->rhp;
  390. u32 stag = mhp->attr.stag;
  391. struct nlattr *table_attr;
  392. struct fw_ri_tpte tpte;
  393. int ret;
  394. if (!stag)
  395. return 0;
  396. table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_DRIVER);
  397. if (!table_attr)
  398. goto err;
  399. ret = cxgb4_read_tpte(dev->rdev.lldi.ports[0], stag, (__be32 *)&tpte);
  400. if (ret) {
  401. dev_err(&dev->rdev.lldi.pdev->dev,
  402. "%s cxgb4_read_tpte err %d\n", __func__, ret);
  403. return 0;
  404. }
  405. if (rdma_nl_put_driver_u32_hex(msg, "idx", stag >> 8))
  406. goto err_cancel_table;
  407. if (rdma_nl_put_driver_u32(msg, "valid",
  408. FW_RI_TPTE_VALID_G(ntohl(tpte.valid_to_pdid))))
  409. goto err_cancel_table;
  410. if (rdma_nl_put_driver_u32_hex(msg, "key", stag & 0xff))
  411. goto err_cancel_table;
  412. if (rdma_nl_put_driver_u32(msg, "state",
  413. FW_RI_TPTE_STAGSTATE_G(ntohl(tpte.valid_to_pdid))))
  414. goto err_cancel_table;
  415. if (rdma_nl_put_driver_u32(msg, "pdid",
  416. FW_RI_TPTE_PDID_G(ntohl(tpte.valid_to_pdid))))
  417. goto err_cancel_table;
  418. if (rdma_nl_put_driver_u32_hex(msg, "perm",
  419. FW_RI_TPTE_PERM_G(ntohl(tpte.locread_to_qpid))))
  420. goto err_cancel_table;
  421. if (rdma_nl_put_driver_u32(msg, "ps",
  422. FW_RI_TPTE_PS_G(ntohl(tpte.locread_to_qpid))))
  423. goto err_cancel_table;
  424. if (rdma_nl_put_driver_u64(msg, "len",
  425. ((u64)ntohl(tpte.len_hi) << 32) | ntohl(tpte.len_lo)))
  426. goto err_cancel_table;
  427. if (rdma_nl_put_driver_u32_hex(msg, "pbl_addr",
  428. FW_RI_TPTE_PBLADDR_G(ntohl(tpte.nosnoop_pbladdr))))
  429. goto err_cancel_table;
  430. nla_nest_end(msg, table_attr);
  431. return 0;
  432. err_cancel_table:
  433. nla_nest_cancel(msg, table_attr);
  434. err:
  435. return -EMSGSIZE;
  436. }
  437. c4iw_restrack_func *c4iw_restrack_funcs[RDMA_RESTRACK_MAX] = {
  438. [RDMA_RESTRACK_QP] = fill_res_qp_entry,
  439. [RDMA_RESTRACK_CM_ID] = fill_res_ep_entry,
  440. [RDMA_RESTRACK_CQ] = fill_res_cq_entry,
  441. [RDMA_RESTRACK_MR] = fill_res_mr_entry,
  442. };