cq.c 22 KB

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