cq.c 26 KB

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