cq.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. /*
  2. * Copyright (c) 2009-2010 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 "iw_cxgb4.h"
  33. static int destroy_cq(struct c4iw_rdev *rdev, struct t4_cq *cq,
  34. struct c4iw_dev_ucontext *uctx, struct sk_buff *skb)
  35. {
  36. struct fw_ri_res_wr *res_wr;
  37. struct fw_ri_res *res;
  38. int wr_len;
  39. struct c4iw_wr_wait wr_wait;
  40. int ret;
  41. wr_len = sizeof *res_wr + sizeof *res;
  42. set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
  43. res_wr = __skb_put_zero(skb, wr_len);
  44. res_wr->op_nres = cpu_to_be32(
  45. FW_WR_OP_V(FW_RI_RES_WR) |
  46. FW_RI_RES_WR_NRES_V(1) |
  47. FW_WR_COMPL_F);
  48. res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
  49. res_wr->cookie = (uintptr_t)&wr_wait;
  50. res = res_wr->res;
  51. res->u.cq.restype = FW_RI_RES_TYPE_CQ;
  52. res->u.cq.op = FW_RI_RES_OP_RESET;
  53. res->u.cq.iqid = cpu_to_be32(cq->cqid);
  54. c4iw_init_wr_wait(&wr_wait);
  55. ret = c4iw_ofld_send(rdev, skb);
  56. if (!ret) {
  57. ret = c4iw_wait_for_reply(rdev, &wr_wait, 0, 0, __func__);
  58. }
  59. kfree(cq->sw_queue);
  60. dma_free_coherent(&(rdev->lldi.pdev->dev),
  61. cq->memsize, cq->queue,
  62. dma_unmap_addr(cq, mapping));
  63. c4iw_put_cqid(rdev, cq->cqid, uctx);
  64. return ret;
  65. }
  66. static int create_cq(struct c4iw_rdev *rdev, struct t4_cq *cq,
  67. struct c4iw_dev_ucontext *uctx)
  68. {
  69. struct fw_ri_res_wr *res_wr;
  70. struct fw_ri_res *res;
  71. int wr_len;
  72. int user = (uctx != &rdev->uctx);
  73. struct c4iw_wr_wait wr_wait;
  74. int ret;
  75. struct sk_buff *skb;
  76. cq->cqid = c4iw_get_cqid(rdev, uctx);
  77. if (!cq->cqid) {
  78. ret = -ENOMEM;
  79. goto err1;
  80. }
  81. if (!user) {
  82. cq->sw_queue = kzalloc(cq->memsize, GFP_KERNEL);
  83. if (!cq->sw_queue) {
  84. ret = -ENOMEM;
  85. goto err2;
  86. }
  87. }
  88. cq->queue = dma_alloc_coherent(&rdev->lldi.pdev->dev, cq->memsize,
  89. &cq->dma_addr, GFP_KERNEL);
  90. if (!cq->queue) {
  91. ret = -ENOMEM;
  92. goto err3;
  93. }
  94. dma_unmap_addr_set(cq, mapping, cq->dma_addr);
  95. memset(cq->queue, 0, cq->memsize);
  96. /* build fw_ri_res_wr */
  97. wr_len = sizeof *res_wr + sizeof *res;
  98. skb = alloc_skb(wr_len, GFP_KERNEL);
  99. if (!skb) {
  100. ret = -ENOMEM;
  101. goto err4;
  102. }
  103. set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
  104. res_wr = __skb_put_zero(skb, wr_len);
  105. res_wr->op_nres = cpu_to_be32(
  106. FW_WR_OP_V(FW_RI_RES_WR) |
  107. FW_RI_RES_WR_NRES_V(1) |
  108. FW_WR_COMPL_F);
  109. res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
  110. res_wr->cookie = (uintptr_t)&wr_wait;
  111. res = res_wr->res;
  112. res->u.cq.restype = FW_RI_RES_TYPE_CQ;
  113. res->u.cq.op = FW_RI_RES_OP_WRITE;
  114. res->u.cq.iqid = cpu_to_be32(cq->cqid);
  115. res->u.cq.iqandst_to_iqandstindex = cpu_to_be32(
  116. FW_RI_RES_WR_IQANUS_V(0) |
  117. FW_RI_RES_WR_IQANUD_V(1) |
  118. FW_RI_RES_WR_IQANDST_F |
  119. FW_RI_RES_WR_IQANDSTINDEX_V(
  120. rdev->lldi.ciq_ids[cq->vector]));
  121. res->u.cq.iqdroprss_to_iqesize = cpu_to_be16(
  122. FW_RI_RES_WR_IQDROPRSS_F |
  123. FW_RI_RES_WR_IQPCIECH_V(2) |
  124. FW_RI_RES_WR_IQINTCNTTHRESH_V(0) |
  125. FW_RI_RES_WR_IQO_F |
  126. FW_RI_RES_WR_IQESIZE_V(1));
  127. res->u.cq.iqsize = cpu_to_be16(cq->size);
  128. res->u.cq.iqaddr = cpu_to_be64(cq->dma_addr);
  129. c4iw_init_wr_wait(&wr_wait);
  130. ret = c4iw_ofld_send(rdev, skb);
  131. if (ret)
  132. goto err4;
  133. pr_debug("%s wait_event wr_wait %p\n", __func__, &wr_wait);
  134. ret = c4iw_wait_for_reply(rdev, &wr_wait, 0, 0, __func__);
  135. if (ret)
  136. goto err4;
  137. cq->gen = 1;
  138. cq->gts = rdev->lldi.gts_reg;
  139. cq->rdev = rdev;
  140. cq->bar2_va = c4iw_bar2_addrs(rdev, cq->cqid, T4_BAR2_QTYPE_INGRESS,
  141. &cq->bar2_qid,
  142. user ? &cq->bar2_pa : NULL);
  143. if (user && !cq->bar2_pa) {
  144. pr_warn("%s: cqid %u not in BAR2 range\n",
  145. pci_name(rdev->lldi.pdev), cq->cqid);
  146. ret = -EINVAL;
  147. goto err4;
  148. }
  149. return 0;
  150. err4:
  151. dma_free_coherent(&rdev->lldi.pdev->dev, cq->memsize, cq->queue,
  152. dma_unmap_addr(cq, mapping));
  153. err3:
  154. kfree(cq->sw_queue);
  155. err2:
  156. c4iw_put_cqid(rdev, cq->cqid, uctx);
  157. err1:
  158. return ret;
  159. }
  160. static void insert_recv_cqe(struct t4_wq *wq, struct t4_cq *cq)
  161. {
  162. struct t4_cqe cqe;
  163. pr_debug("%s wq %p cq %p sw_cidx %u sw_pidx %u\n", __func__,
  164. wq, cq, cq->sw_cidx, cq->sw_pidx);
  165. memset(&cqe, 0, sizeof(cqe));
  166. cqe.header = cpu_to_be32(CQE_STATUS_V(T4_ERR_SWFLUSH) |
  167. CQE_OPCODE_V(FW_RI_SEND) |
  168. CQE_TYPE_V(0) |
  169. CQE_SWCQE_V(1) |
  170. CQE_QPID_V(wq->sq.qid));
  171. cqe.bits_type_ts = cpu_to_be64(CQE_GENBIT_V((u64)cq->gen));
  172. cq->sw_queue[cq->sw_pidx] = cqe;
  173. t4_swcq_produce(cq);
  174. }
  175. int c4iw_flush_rq(struct t4_wq *wq, struct t4_cq *cq, int count)
  176. {
  177. int flushed = 0;
  178. int in_use = wq->rq.in_use - count;
  179. BUG_ON(in_use < 0);
  180. pr_debug("%s wq %p cq %p rq.in_use %u skip count %u\n", __func__,
  181. wq, cq, wq->rq.in_use, count);
  182. while (in_use--) {
  183. insert_recv_cqe(wq, cq);
  184. flushed++;
  185. }
  186. return flushed;
  187. }
  188. static void insert_sq_cqe(struct t4_wq *wq, struct t4_cq *cq,
  189. struct t4_swsqe *swcqe)
  190. {
  191. struct t4_cqe cqe;
  192. pr_debug("%s wq %p cq %p sw_cidx %u sw_pidx %u\n", __func__,
  193. wq, cq, cq->sw_cidx, cq->sw_pidx);
  194. memset(&cqe, 0, sizeof(cqe));
  195. cqe.header = cpu_to_be32(CQE_STATUS_V(T4_ERR_SWFLUSH) |
  196. CQE_OPCODE_V(swcqe->opcode) |
  197. CQE_TYPE_V(1) |
  198. CQE_SWCQE_V(1) |
  199. CQE_QPID_V(wq->sq.qid));
  200. CQE_WRID_SQ_IDX(&cqe) = swcqe->idx;
  201. cqe.bits_type_ts = cpu_to_be64(CQE_GENBIT_V((u64)cq->gen));
  202. cq->sw_queue[cq->sw_pidx] = cqe;
  203. t4_swcq_produce(cq);
  204. }
  205. static void advance_oldest_read(struct t4_wq *wq);
  206. int c4iw_flush_sq(struct c4iw_qp *qhp)
  207. {
  208. int flushed = 0;
  209. struct t4_wq *wq = &qhp->wq;
  210. struct c4iw_cq *chp = to_c4iw_cq(qhp->ibqp.send_cq);
  211. struct t4_cq *cq = &chp->cq;
  212. int idx;
  213. struct t4_swsqe *swsqe;
  214. if (wq->sq.flush_cidx == -1)
  215. wq->sq.flush_cidx = wq->sq.cidx;
  216. idx = wq->sq.flush_cidx;
  217. BUG_ON(idx >= wq->sq.size);
  218. while (idx != wq->sq.pidx) {
  219. swsqe = &wq->sq.sw_sq[idx];
  220. BUG_ON(swsqe->flushed);
  221. swsqe->flushed = 1;
  222. insert_sq_cqe(wq, cq, swsqe);
  223. if (wq->sq.oldest_read == swsqe) {
  224. BUG_ON(swsqe->opcode != FW_RI_READ_REQ);
  225. advance_oldest_read(wq);
  226. }
  227. flushed++;
  228. if (++idx == wq->sq.size)
  229. idx = 0;
  230. }
  231. wq->sq.flush_cidx += flushed;
  232. if (wq->sq.flush_cidx >= wq->sq.size)
  233. wq->sq.flush_cidx -= wq->sq.size;
  234. return flushed;
  235. }
  236. static void flush_completed_wrs(struct t4_wq *wq, struct t4_cq *cq)
  237. {
  238. struct t4_swsqe *swsqe;
  239. int cidx;
  240. if (wq->sq.flush_cidx == -1)
  241. wq->sq.flush_cidx = wq->sq.cidx;
  242. cidx = wq->sq.flush_cidx;
  243. BUG_ON(cidx > wq->sq.size);
  244. while (cidx != wq->sq.pidx) {
  245. swsqe = &wq->sq.sw_sq[cidx];
  246. if (!swsqe->signaled) {
  247. if (++cidx == wq->sq.size)
  248. cidx = 0;
  249. } else if (swsqe->complete) {
  250. BUG_ON(swsqe->flushed);
  251. /*
  252. * Insert this completed cqe into the swcq.
  253. */
  254. pr_debug("%s moving cqe into swcq sq idx %u cq idx %u\n",
  255. __func__, cidx, cq->sw_pidx);
  256. swsqe->cqe.header |= htonl(CQE_SWCQE_V(1));
  257. cq->sw_queue[cq->sw_pidx] = swsqe->cqe;
  258. t4_swcq_produce(cq);
  259. swsqe->flushed = 1;
  260. if (++cidx == wq->sq.size)
  261. cidx = 0;
  262. wq->sq.flush_cidx = cidx;
  263. } else
  264. break;
  265. }
  266. }
  267. static void create_read_req_cqe(struct t4_wq *wq, struct t4_cqe *hw_cqe,
  268. struct t4_cqe *read_cqe)
  269. {
  270. read_cqe->u.scqe.cidx = wq->sq.oldest_read->idx;
  271. read_cqe->len = htonl(wq->sq.oldest_read->read_len);
  272. read_cqe->header = htonl(CQE_QPID_V(CQE_QPID(hw_cqe)) |
  273. CQE_SWCQE_V(SW_CQE(hw_cqe)) |
  274. CQE_OPCODE_V(FW_RI_READ_REQ) |
  275. CQE_TYPE_V(1));
  276. read_cqe->bits_type_ts = hw_cqe->bits_type_ts;
  277. }
  278. static void advance_oldest_read(struct t4_wq *wq)
  279. {
  280. u32 rptr = wq->sq.oldest_read - wq->sq.sw_sq + 1;
  281. if (rptr == wq->sq.size)
  282. rptr = 0;
  283. while (rptr != wq->sq.pidx) {
  284. wq->sq.oldest_read = &wq->sq.sw_sq[rptr];
  285. if (wq->sq.oldest_read->opcode == FW_RI_READ_REQ)
  286. return;
  287. if (++rptr == wq->sq.size)
  288. rptr = 0;
  289. }
  290. wq->sq.oldest_read = NULL;
  291. }
  292. /*
  293. * Move all CQEs from the HWCQ into the SWCQ.
  294. * Deal with out-of-order and/or completions that complete
  295. * prior unsignalled WRs.
  296. */
  297. void c4iw_flush_hw_cq(struct c4iw_cq *chp)
  298. {
  299. struct t4_cqe *hw_cqe, *swcqe, read_cqe;
  300. struct c4iw_qp *qhp;
  301. struct t4_swsqe *swsqe;
  302. int ret;
  303. pr_debug("%s cqid 0x%x\n", __func__, chp->cq.cqid);
  304. ret = t4_next_hw_cqe(&chp->cq, &hw_cqe);
  305. /*
  306. * This logic is similar to poll_cq(), but not quite the same
  307. * unfortunately. Need to move pertinent HW CQEs to the SW CQ but
  308. * also do any translation magic that poll_cq() normally does.
  309. */
  310. while (!ret) {
  311. qhp = get_qhp(chp->rhp, CQE_QPID(hw_cqe));
  312. /*
  313. * drop CQEs with no associated QP
  314. */
  315. if (qhp == NULL)
  316. goto next_cqe;
  317. if (CQE_OPCODE(hw_cqe) == FW_RI_TERMINATE)
  318. goto next_cqe;
  319. if (CQE_OPCODE(hw_cqe) == FW_RI_READ_RESP) {
  320. /* If we have reached here because of async
  321. * event or other error, and have egress error
  322. * then drop
  323. */
  324. if (CQE_TYPE(hw_cqe) == 1)
  325. goto next_cqe;
  326. /* drop peer2peer RTR reads.
  327. */
  328. if (CQE_WRID_STAG(hw_cqe) == 1)
  329. goto next_cqe;
  330. /*
  331. * Eat completions for unsignaled read WRs.
  332. */
  333. if (!qhp->wq.sq.oldest_read->signaled) {
  334. advance_oldest_read(&qhp->wq);
  335. goto next_cqe;
  336. }
  337. /*
  338. * Don't write to the HWCQ, create a new read req CQE
  339. * in local memory and move it into the swcq.
  340. */
  341. create_read_req_cqe(&qhp->wq, hw_cqe, &read_cqe);
  342. hw_cqe = &read_cqe;
  343. advance_oldest_read(&qhp->wq);
  344. }
  345. /* if its a SQ completion, then do the magic to move all the
  346. * unsignaled and now in-order completions into the swcq.
  347. */
  348. if (SQ_TYPE(hw_cqe)) {
  349. swsqe = &qhp->wq.sq.sw_sq[CQE_WRID_SQ_IDX(hw_cqe)];
  350. swsqe->cqe = *hw_cqe;
  351. swsqe->complete = 1;
  352. flush_completed_wrs(&qhp->wq, &chp->cq);
  353. } else {
  354. swcqe = &chp->cq.sw_queue[chp->cq.sw_pidx];
  355. *swcqe = *hw_cqe;
  356. swcqe->header |= cpu_to_be32(CQE_SWCQE_V(1));
  357. t4_swcq_produce(&chp->cq);
  358. }
  359. next_cqe:
  360. t4_hwcq_consume(&chp->cq);
  361. ret = t4_next_hw_cqe(&chp->cq, &hw_cqe);
  362. }
  363. }
  364. static int cqe_completes_wr(struct t4_cqe *cqe, struct t4_wq *wq)
  365. {
  366. if (CQE_OPCODE(cqe) == FW_RI_TERMINATE)
  367. return 0;
  368. if ((CQE_OPCODE(cqe) == FW_RI_RDMA_WRITE) && RQ_TYPE(cqe))
  369. return 0;
  370. if ((CQE_OPCODE(cqe) == FW_RI_READ_RESP) && SQ_TYPE(cqe))
  371. return 0;
  372. if (CQE_SEND_OPCODE(cqe) && RQ_TYPE(cqe) && t4_rq_empty(wq))
  373. return 0;
  374. return 1;
  375. }
  376. void c4iw_count_rcqes(struct t4_cq *cq, struct t4_wq *wq, int *count)
  377. {
  378. struct t4_cqe *cqe;
  379. u32 ptr;
  380. *count = 0;
  381. pr_debug("%s count zero %d\n", __func__, *count);
  382. ptr = cq->sw_cidx;
  383. while (ptr != cq->sw_pidx) {
  384. cqe = &cq->sw_queue[ptr];
  385. if (RQ_TYPE(cqe) && (CQE_OPCODE(cqe) != FW_RI_READ_RESP) &&
  386. (CQE_QPID(cqe) == wq->sq.qid) && cqe_completes_wr(cqe, wq))
  387. (*count)++;
  388. if (++ptr == cq->size)
  389. ptr = 0;
  390. }
  391. pr_debug("%s cq %p count %d\n", __func__, cq, *count);
  392. }
  393. /*
  394. * poll_cq
  395. *
  396. * Caller must:
  397. * check the validity of the first CQE,
  398. * supply the wq assicated with the qpid.
  399. *
  400. * credit: cq credit to return to sge.
  401. * cqe_flushed: 1 iff the CQE is flushed.
  402. * cqe: copy of the polled CQE.
  403. *
  404. * return value:
  405. * 0 CQE returned ok.
  406. * -EAGAIN CQE skipped, try again.
  407. * -EOVERFLOW CQ overflow detected.
  408. */
  409. static int poll_cq(struct t4_wq *wq, struct t4_cq *cq, struct t4_cqe *cqe,
  410. u8 *cqe_flushed, u64 *cookie, u32 *credit)
  411. {
  412. int ret = 0;
  413. struct t4_cqe *hw_cqe, read_cqe;
  414. *cqe_flushed = 0;
  415. *credit = 0;
  416. ret = t4_next_cqe(cq, &hw_cqe);
  417. if (ret)
  418. return ret;
  419. pr_debug("%s CQE OVF %u qpid 0x%0x genbit %u type %u status 0x%0x opcode 0x%0x len 0x%0x wrid_hi_stag 0x%x wrid_low_msn 0x%x\n",
  420. __func__, CQE_OVFBIT(hw_cqe), CQE_QPID(hw_cqe),
  421. CQE_GENBIT(hw_cqe), CQE_TYPE(hw_cqe), CQE_STATUS(hw_cqe),
  422. CQE_OPCODE(hw_cqe), CQE_LEN(hw_cqe), CQE_WRID_HI(hw_cqe),
  423. CQE_WRID_LOW(hw_cqe));
  424. /*
  425. * skip cqe's not affiliated with a QP.
  426. */
  427. if (wq == NULL) {
  428. ret = -EAGAIN;
  429. goto skip_cqe;
  430. }
  431. /*
  432. * skip hw cqe's if the wq is flushed.
  433. */
  434. if (wq->flushed && !SW_CQE(hw_cqe)) {
  435. ret = -EAGAIN;
  436. goto skip_cqe;
  437. }
  438. /*
  439. * skip TERMINATE cqes...
  440. */
  441. if (CQE_OPCODE(hw_cqe) == FW_RI_TERMINATE) {
  442. ret = -EAGAIN;
  443. goto skip_cqe;
  444. }
  445. /*
  446. * Special cqe for drain WR completions...
  447. */
  448. if (CQE_OPCODE(hw_cqe) == C4IW_DRAIN_OPCODE) {
  449. *cookie = CQE_DRAIN_COOKIE(hw_cqe);
  450. *cqe = *hw_cqe;
  451. goto skip_cqe;
  452. }
  453. /*
  454. * Gotta tweak READ completions:
  455. * 1) the cqe doesn't contain the sq_wptr from the wr.
  456. * 2) opcode not reflected from the wr.
  457. * 3) read_len not reflected from the wr.
  458. * 4) cq_type is RQ_TYPE not SQ_TYPE.
  459. */
  460. if (RQ_TYPE(hw_cqe) && (CQE_OPCODE(hw_cqe) == FW_RI_READ_RESP)) {
  461. /* If we have reached here because of async
  462. * event or other error, and have egress error
  463. * then drop
  464. */
  465. if (CQE_TYPE(hw_cqe) == 1) {
  466. if (CQE_STATUS(hw_cqe))
  467. t4_set_wq_in_error(wq);
  468. ret = -EAGAIN;
  469. goto skip_cqe;
  470. }
  471. /* If this is an unsolicited read response, then the read
  472. * was generated by the kernel driver as part of peer-2-peer
  473. * connection setup. So ignore the completion.
  474. */
  475. if (CQE_WRID_STAG(hw_cqe) == 1) {
  476. if (CQE_STATUS(hw_cqe))
  477. t4_set_wq_in_error(wq);
  478. ret = -EAGAIN;
  479. goto skip_cqe;
  480. }
  481. /*
  482. * Eat completions for unsignaled read WRs.
  483. */
  484. if (!wq->sq.oldest_read->signaled) {
  485. advance_oldest_read(wq);
  486. ret = -EAGAIN;
  487. goto skip_cqe;
  488. }
  489. /*
  490. * Don't write to the HWCQ, so create a new read req CQE
  491. * in local memory.
  492. */
  493. create_read_req_cqe(wq, hw_cqe, &read_cqe);
  494. hw_cqe = &read_cqe;
  495. advance_oldest_read(wq);
  496. }
  497. if (CQE_STATUS(hw_cqe) || t4_wq_in_error(wq)) {
  498. *cqe_flushed = (CQE_STATUS(hw_cqe) == T4_ERR_SWFLUSH);
  499. t4_set_wq_in_error(wq);
  500. }
  501. /*
  502. * RECV completion.
  503. */
  504. if (RQ_TYPE(hw_cqe)) {
  505. /*
  506. * HW only validates 4 bits of MSN. So we must validate that
  507. * the MSN in the SEND is the next expected MSN. If its not,
  508. * then we complete this with T4_ERR_MSN and mark the wq in
  509. * error.
  510. */
  511. if (t4_rq_empty(wq)) {
  512. t4_set_wq_in_error(wq);
  513. ret = -EAGAIN;
  514. goto skip_cqe;
  515. }
  516. if (unlikely((CQE_WRID_MSN(hw_cqe) != (wq->rq.msn)))) {
  517. t4_set_wq_in_error(wq);
  518. hw_cqe->header |= htonl(CQE_STATUS_V(T4_ERR_MSN));
  519. goto proc_cqe;
  520. }
  521. goto proc_cqe;
  522. }
  523. /*
  524. * If we get here its a send completion.
  525. *
  526. * Handle out of order completion. These get stuffed
  527. * in the SW SQ. Then the SW SQ is walked to move any
  528. * now in-order completions into the SW CQ. This handles
  529. * 2 cases:
  530. * 1) reaping unsignaled WRs when the first subsequent
  531. * signaled WR is completed.
  532. * 2) out of order read completions.
  533. */
  534. if (!SW_CQE(hw_cqe) && (CQE_WRID_SQ_IDX(hw_cqe) != wq->sq.cidx)) {
  535. struct t4_swsqe *swsqe;
  536. pr_debug("%s out of order completion going in sw_sq at idx %u\n",
  537. __func__, CQE_WRID_SQ_IDX(hw_cqe));
  538. swsqe = &wq->sq.sw_sq[CQE_WRID_SQ_IDX(hw_cqe)];
  539. swsqe->cqe = *hw_cqe;
  540. swsqe->complete = 1;
  541. ret = -EAGAIN;
  542. goto flush_wq;
  543. }
  544. proc_cqe:
  545. *cqe = *hw_cqe;
  546. /*
  547. * Reap the associated WR(s) that are freed up with this
  548. * completion.
  549. */
  550. if (SQ_TYPE(hw_cqe)) {
  551. int idx = CQE_WRID_SQ_IDX(hw_cqe);
  552. BUG_ON(idx >= wq->sq.size);
  553. /*
  554. * Account for any unsignaled completions completed by
  555. * this signaled completion. In this case, cidx points
  556. * to the first unsignaled one, and idx points to the
  557. * signaled one. So adjust in_use based on this delta.
  558. * if this is not completing any unsigned wrs, then the
  559. * delta will be 0. Handle wrapping also!
  560. */
  561. if (idx < wq->sq.cidx)
  562. wq->sq.in_use -= wq->sq.size + idx - wq->sq.cidx;
  563. else
  564. wq->sq.in_use -= idx - wq->sq.cidx;
  565. BUG_ON(wq->sq.in_use <= 0 && wq->sq.in_use >= wq->sq.size);
  566. wq->sq.cidx = (uint16_t)idx;
  567. pr_debug("%s completing sq idx %u\n", __func__, wq->sq.cidx);
  568. *cookie = wq->sq.sw_sq[wq->sq.cidx].wr_id;
  569. if (c4iw_wr_log)
  570. c4iw_log_wr_stats(wq, hw_cqe);
  571. t4_sq_consume(wq);
  572. } else {
  573. pr_debug("%s completing rq idx %u\n", __func__, wq->rq.cidx);
  574. *cookie = wq->rq.sw_rq[wq->rq.cidx].wr_id;
  575. BUG_ON(t4_rq_empty(wq));
  576. if (c4iw_wr_log)
  577. c4iw_log_wr_stats(wq, hw_cqe);
  578. t4_rq_consume(wq);
  579. goto skip_cqe;
  580. }
  581. flush_wq:
  582. /*
  583. * Flush any completed cqes that are now in-order.
  584. */
  585. flush_completed_wrs(wq, cq);
  586. skip_cqe:
  587. if (SW_CQE(hw_cqe)) {
  588. pr_debug("%s cq %p cqid 0x%x skip sw cqe cidx %u\n",
  589. __func__, cq, cq->cqid, cq->sw_cidx);
  590. t4_swcq_consume(cq);
  591. } else {
  592. pr_debug("%s cq %p cqid 0x%x skip hw cqe cidx %u\n",
  593. __func__, cq, cq->cqid, cq->cidx);
  594. t4_hwcq_consume(cq);
  595. }
  596. return ret;
  597. }
  598. /*
  599. * Get one cq entry from c4iw and map it to openib.
  600. *
  601. * Returns:
  602. * 0 cqe returned
  603. * -ENODATA EMPTY;
  604. * -EAGAIN caller must try again
  605. * any other -errno fatal error
  606. */
  607. static int c4iw_poll_cq_one(struct c4iw_cq *chp, struct ib_wc *wc)
  608. {
  609. struct c4iw_qp *qhp = NULL;
  610. struct t4_cqe uninitialized_var(cqe), *rd_cqe;
  611. struct t4_wq *wq;
  612. u32 credit = 0;
  613. u8 cqe_flushed;
  614. u64 cookie = 0;
  615. int ret;
  616. ret = t4_next_cqe(&chp->cq, &rd_cqe);
  617. if (ret)
  618. return ret;
  619. qhp = get_qhp(chp->rhp, CQE_QPID(rd_cqe));
  620. if (!qhp)
  621. wq = NULL;
  622. else {
  623. spin_lock(&qhp->lock);
  624. wq = &(qhp->wq);
  625. }
  626. ret = poll_cq(wq, &(chp->cq), &cqe, &cqe_flushed, &cookie, &credit);
  627. if (ret)
  628. goto out;
  629. wc->wr_id = cookie;
  630. wc->qp = &qhp->ibqp;
  631. wc->vendor_err = CQE_STATUS(&cqe);
  632. wc->wc_flags = 0;
  633. pr_debug("%s qpid 0x%x type %d opcode %d status 0x%x len %u wrid hi 0x%x lo 0x%x cookie 0x%llx\n",
  634. __func__, CQE_QPID(&cqe),
  635. CQE_TYPE(&cqe), CQE_OPCODE(&cqe),
  636. CQE_STATUS(&cqe), CQE_LEN(&cqe),
  637. CQE_WRID_HI(&cqe), CQE_WRID_LOW(&cqe),
  638. (unsigned long long)cookie);
  639. if (CQE_TYPE(&cqe) == 0) {
  640. if (!CQE_STATUS(&cqe))
  641. wc->byte_len = CQE_LEN(&cqe);
  642. else
  643. wc->byte_len = 0;
  644. wc->opcode = IB_WC_RECV;
  645. if (CQE_OPCODE(&cqe) == FW_RI_SEND_WITH_INV ||
  646. CQE_OPCODE(&cqe) == FW_RI_SEND_WITH_SE_INV) {
  647. wc->ex.invalidate_rkey = CQE_WRID_STAG(&cqe);
  648. wc->wc_flags |= IB_WC_WITH_INVALIDATE;
  649. c4iw_invalidate_mr(qhp->rhp, wc->ex.invalidate_rkey);
  650. }
  651. } else {
  652. switch (CQE_OPCODE(&cqe)) {
  653. case FW_RI_RDMA_WRITE:
  654. wc->opcode = IB_WC_RDMA_WRITE;
  655. break;
  656. case FW_RI_READ_REQ:
  657. wc->opcode = IB_WC_RDMA_READ;
  658. wc->byte_len = CQE_LEN(&cqe);
  659. break;
  660. case FW_RI_SEND_WITH_INV:
  661. case FW_RI_SEND_WITH_SE_INV:
  662. wc->opcode = IB_WC_SEND;
  663. wc->wc_flags |= IB_WC_WITH_INVALIDATE;
  664. break;
  665. case FW_RI_SEND:
  666. case FW_RI_SEND_WITH_SE:
  667. wc->opcode = IB_WC_SEND;
  668. break;
  669. case FW_RI_LOCAL_INV:
  670. wc->opcode = IB_WC_LOCAL_INV;
  671. break;
  672. case FW_RI_FAST_REGISTER:
  673. wc->opcode = IB_WC_REG_MR;
  674. /* Invalidate the MR if the fastreg failed */
  675. if (CQE_STATUS(&cqe) != T4_ERR_SUCCESS)
  676. c4iw_invalidate_mr(qhp->rhp,
  677. CQE_WRID_FR_STAG(&cqe));
  678. break;
  679. case C4IW_DRAIN_OPCODE:
  680. wc->opcode = IB_WC_SEND;
  681. break;
  682. default:
  683. pr_err("Unexpected opcode %d in the CQE received for QPID=0x%0x\n",
  684. CQE_OPCODE(&cqe), CQE_QPID(&cqe));
  685. ret = -EINVAL;
  686. goto out;
  687. }
  688. }
  689. if (cqe_flushed)
  690. wc->status = IB_WC_WR_FLUSH_ERR;
  691. else {
  692. switch (CQE_STATUS(&cqe)) {
  693. case T4_ERR_SUCCESS:
  694. wc->status = IB_WC_SUCCESS;
  695. break;
  696. case T4_ERR_STAG:
  697. wc->status = IB_WC_LOC_ACCESS_ERR;
  698. break;
  699. case T4_ERR_PDID:
  700. wc->status = IB_WC_LOC_PROT_ERR;
  701. break;
  702. case T4_ERR_QPID:
  703. case T4_ERR_ACCESS:
  704. wc->status = IB_WC_LOC_ACCESS_ERR;
  705. break;
  706. case T4_ERR_WRAP:
  707. wc->status = IB_WC_GENERAL_ERR;
  708. break;
  709. case T4_ERR_BOUND:
  710. wc->status = IB_WC_LOC_LEN_ERR;
  711. break;
  712. case T4_ERR_INVALIDATE_SHARED_MR:
  713. case T4_ERR_INVALIDATE_MR_WITH_MW_BOUND:
  714. wc->status = IB_WC_MW_BIND_ERR;
  715. break;
  716. case T4_ERR_CRC:
  717. case T4_ERR_MARKER:
  718. case T4_ERR_PDU_LEN_ERR:
  719. case T4_ERR_OUT_OF_RQE:
  720. case T4_ERR_DDP_VERSION:
  721. case T4_ERR_RDMA_VERSION:
  722. case T4_ERR_DDP_QUEUE_NUM:
  723. case T4_ERR_MSN:
  724. case T4_ERR_TBIT:
  725. case T4_ERR_MO:
  726. case T4_ERR_MSN_RANGE:
  727. case T4_ERR_IRD_OVERFLOW:
  728. case T4_ERR_OPCODE:
  729. case T4_ERR_INTERNAL_ERR:
  730. wc->status = IB_WC_FATAL_ERR;
  731. break;
  732. case T4_ERR_SWFLUSH:
  733. wc->status = IB_WC_WR_FLUSH_ERR;
  734. break;
  735. default:
  736. pr_err("Unexpected cqe_status 0x%x for QPID=0x%0x\n",
  737. CQE_STATUS(&cqe), CQE_QPID(&cqe));
  738. wc->status = IB_WC_FATAL_ERR;
  739. }
  740. }
  741. out:
  742. if (wq)
  743. spin_unlock(&qhp->lock);
  744. return ret;
  745. }
  746. int c4iw_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
  747. {
  748. struct c4iw_cq *chp;
  749. unsigned long flags;
  750. int npolled;
  751. int err = 0;
  752. chp = to_c4iw_cq(ibcq);
  753. spin_lock_irqsave(&chp->lock, flags);
  754. for (npolled = 0; npolled < num_entries; ++npolled) {
  755. do {
  756. err = c4iw_poll_cq_one(chp, wc + npolled);
  757. } while (err == -EAGAIN);
  758. if (err)
  759. break;
  760. }
  761. spin_unlock_irqrestore(&chp->lock, flags);
  762. return !err || err == -ENODATA ? npolled : err;
  763. }
  764. int c4iw_destroy_cq(struct ib_cq *ib_cq)
  765. {
  766. struct c4iw_cq *chp;
  767. struct c4iw_ucontext *ucontext;
  768. pr_debug("%s ib_cq %p\n", __func__, ib_cq);
  769. chp = to_c4iw_cq(ib_cq);
  770. remove_handle(chp->rhp, &chp->rhp->cqidr, chp->cq.cqid);
  771. atomic_dec(&chp->refcnt);
  772. wait_event(chp->wait, !atomic_read(&chp->refcnt));
  773. ucontext = ib_cq->uobject ? to_c4iw_ucontext(ib_cq->uobject->context)
  774. : NULL;
  775. destroy_cq(&chp->rhp->rdev, &chp->cq,
  776. ucontext ? &ucontext->uctx : &chp->cq.rdev->uctx,
  777. chp->destroy_skb);
  778. chp->destroy_skb = NULL;
  779. kfree(chp);
  780. return 0;
  781. }
  782. struct ib_cq *c4iw_create_cq(struct ib_device *ibdev,
  783. const struct ib_cq_init_attr *attr,
  784. struct ib_ucontext *ib_context,
  785. struct ib_udata *udata)
  786. {
  787. int entries = attr->cqe;
  788. int vector = attr->comp_vector;
  789. struct c4iw_dev *rhp;
  790. struct c4iw_cq *chp;
  791. struct c4iw_create_cq_resp uresp;
  792. struct c4iw_ucontext *ucontext = NULL;
  793. int ret, wr_len;
  794. size_t memsize, hwentries;
  795. struct c4iw_mm_entry *mm, *mm2;
  796. pr_debug("%s ib_dev %p entries %d\n", __func__, ibdev, entries);
  797. if (attr->flags)
  798. return ERR_PTR(-EINVAL);
  799. rhp = to_c4iw_dev(ibdev);
  800. if (vector >= rhp->rdev.lldi.nciq)
  801. return ERR_PTR(-EINVAL);
  802. chp = kzalloc(sizeof(*chp), GFP_KERNEL);
  803. if (!chp)
  804. return ERR_PTR(-ENOMEM);
  805. wr_len = sizeof(struct fw_ri_res_wr) + sizeof(struct fw_ri_res);
  806. chp->destroy_skb = alloc_skb(wr_len, GFP_KERNEL);
  807. if (!chp->destroy_skb) {
  808. ret = -ENOMEM;
  809. goto err1;
  810. }
  811. if (ib_context)
  812. ucontext = to_c4iw_ucontext(ib_context);
  813. /* account for the status page. */
  814. entries++;
  815. /* IQ needs one extra entry to differentiate full vs empty. */
  816. entries++;
  817. /*
  818. * entries must be multiple of 16 for HW.
  819. */
  820. entries = roundup(entries, 16);
  821. /*
  822. * Make actual HW queue 2x to avoid cdix_inc overflows.
  823. */
  824. hwentries = min(entries * 2, rhp->rdev.hw_queue.t4_max_iq_size);
  825. /*
  826. * Make HW queue at least 64 entries so GTS updates aren't too
  827. * frequent.
  828. */
  829. if (hwentries < 64)
  830. hwentries = 64;
  831. memsize = hwentries * sizeof *chp->cq.queue;
  832. /*
  833. * memsize must be a multiple of the page size if its a user cq.
  834. */
  835. if (ucontext)
  836. memsize = roundup(memsize, PAGE_SIZE);
  837. chp->cq.size = hwentries;
  838. chp->cq.memsize = memsize;
  839. chp->cq.vector = vector;
  840. ret = create_cq(&rhp->rdev, &chp->cq,
  841. ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
  842. if (ret)
  843. goto err2;
  844. chp->rhp = rhp;
  845. chp->cq.size--; /* status page */
  846. chp->ibcq.cqe = entries - 2;
  847. spin_lock_init(&chp->lock);
  848. spin_lock_init(&chp->comp_handler_lock);
  849. atomic_set(&chp->refcnt, 1);
  850. init_waitqueue_head(&chp->wait);
  851. ret = insert_handle(rhp, &rhp->cqidr, chp, chp->cq.cqid);
  852. if (ret)
  853. goto err3;
  854. if (ucontext) {
  855. ret = -ENOMEM;
  856. mm = kmalloc(sizeof *mm, GFP_KERNEL);
  857. if (!mm)
  858. goto err4;
  859. mm2 = kmalloc(sizeof *mm2, GFP_KERNEL);
  860. if (!mm2)
  861. goto err5;
  862. uresp.qid_mask = rhp->rdev.cqmask;
  863. uresp.cqid = chp->cq.cqid;
  864. uresp.size = chp->cq.size;
  865. uresp.memsize = chp->cq.memsize;
  866. spin_lock(&ucontext->mmap_lock);
  867. uresp.key = ucontext->key;
  868. ucontext->key += PAGE_SIZE;
  869. uresp.gts_key = ucontext->key;
  870. ucontext->key += PAGE_SIZE;
  871. spin_unlock(&ucontext->mmap_lock);
  872. ret = ib_copy_to_udata(udata, &uresp,
  873. sizeof(uresp) - sizeof(uresp.reserved));
  874. if (ret)
  875. goto err6;
  876. mm->key = uresp.key;
  877. mm->addr = virt_to_phys(chp->cq.queue);
  878. mm->len = chp->cq.memsize;
  879. insert_mmap(ucontext, mm);
  880. mm2->key = uresp.gts_key;
  881. mm2->addr = chp->cq.bar2_pa;
  882. mm2->len = PAGE_SIZE;
  883. insert_mmap(ucontext, mm2);
  884. }
  885. pr_debug("%s cqid 0x%0x chp %p size %u memsize %zu, dma_addr 0x%0llx\n",
  886. __func__, chp->cq.cqid, chp, chp->cq.size,
  887. chp->cq.memsize, (unsigned long long)chp->cq.dma_addr);
  888. return &chp->ibcq;
  889. err6:
  890. kfree(mm2);
  891. err5:
  892. kfree(mm);
  893. err4:
  894. remove_handle(rhp, &rhp->cqidr, chp->cq.cqid);
  895. err3:
  896. destroy_cq(&chp->rhp->rdev, &chp->cq,
  897. ucontext ? &ucontext->uctx : &rhp->rdev.uctx,
  898. chp->destroy_skb);
  899. err2:
  900. kfree_skb(chp->destroy_skb);
  901. err1:
  902. kfree(chp);
  903. return ERR_PTR(ret);
  904. }
  905. int c4iw_resize_cq(struct ib_cq *cq, int cqe, struct ib_udata *udata)
  906. {
  907. return -ENOSYS;
  908. }
  909. int c4iw_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
  910. {
  911. struct c4iw_cq *chp;
  912. int ret = 0;
  913. unsigned long flag;
  914. chp = to_c4iw_cq(ibcq);
  915. spin_lock_irqsave(&chp->lock, flag);
  916. t4_arm_cq(&chp->cq,
  917. (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED);
  918. if (flags & IB_CQ_REPORT_MISSED_EVENTS)
  919. ret = t4_cq_notempty(&chp->cq);
  920. spin_unlock_irqrestore(&chp->lock, flag);
  921. return ret;
  922. }