rxe_comp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /*
  3. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  4. * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
  5. */
  6. #include <linux/skbuff.h>
  7. #include "rxe.h"
  8. #include "rxe_loc.h"
  9. #include "rxe_queue.h"
  10. #include "rxe_task.h"
  11. enum comp_state {
  12. COMPST_GET_ACK,
  13. COMPST_GET_WQE,
  14. COMPST_COMP_WQE,
  15. COMPST_COMP_ACK,
  16. COMPST_CHECK_PSN,
  17. COMPST_CHECK_ACK,
  18. COMPST_READ,
  19. COMPST_ATOMIC,
  20. COMPST_WRITE_SEND,
  21. COMPST_UPDATE_COMP,
  22. COMPST_ERROR_RETRY,
  23. COMPST_RNR_RETRY,
  24. COMPST_ERROR,
  25. COMPST_EXIT, /* We have an issue, and we want to rerun the completer */
  26. COMPST_DONE, /* The completer finished successflly */
  27. };
  28. static char *comp_state_name[] = {
  29. [COMPST_GET_ACK] = "GET ACK",
  30. [COMPST_GET_WQE] = "GET WQE",
  31. [COMPST_COMP_WQE] = "COMP WQE",
  32. [COMPST_COMP_ACK] = "COMP ACK",
  33. [COMPST_CHECK_PSN] = "CHECK PSN",
  34. [COMPST_CHECK_ACK] = "CHECK ACK",
  35. [COMPST_READ] = "READ",
  36. [COMPST_ATOMIC] = "ATOMIC",
  37. [COMPST_WRITE_SEND] = "WRITE/SEND",
  38. [COMPST_UPDATE_COMP] = "UPDATE COMP",
  39. [COMPST_ERROR_RETRY] = "ERROR RETRY",
  40. [COMPST_RNR_RETRY] = "RNR RETRY",
  41. [COMPST_ERROR] = "ERROR",
  42. [COMPST_EXIT] = "EXIT",
  43. [COMPST_DONE] = "DONE",
  44. };
  45. static unsigned long rnrnak_usec[32] = {
  46. [IB_RNR_TIMER_655_36] = 655360,
  47. [IB_RNR_TIMER_000_01] = 10,
  48. [IB_RNR_TIMER_000_02] = 20,
  49. [IB_RNR_TIMER_000_03] = 30,
  50. [IB_RNR_TIMER_000_04] = 40,
  51. [IB_RNR_TIMER_000_06] = 60,
  52. [IB_RNR_TIMER_000_08] = 80,
  53. [IB_RNR_TIMER_000_12] = 120,
  54. [IB_RNR_TIMER_000_16] = 160,
  55. [IB_RNR_TIMER_000_24] = 240,
  56. [IB_RNR_TIMER_000_32] = 320,
  57. [IB_RNR_TIMER_000_48] = 480,
  58. [IB_RNR_TIMER_000_64] = 640,
  59. [IB_RNR_TIMER_000_96] = 960,
  60. [IB_RNR_TIMER_001_28] = 1280,
  61. [IB_RNR_TIMER_001_92] = 1920,
  62. [IB_RNR_TIMER_002_56] = 2560,
  63. [IB_RNR_TIMER_003_84] = 3840,
  64. [IB_RNR_TIMER_005_12] = 5120,
  65. [IB_RNR_TIMER_007_68] = 7680,
  66. [IB_RNR_TIMER_010_24] = 10240,
  67. [IB_RNR_TIMER_015_36] = 15360,
  68. [IB_RNR_TIMER_020_48] = 20480,
  69. [IB_RNR_TIMER_030_72] = 30720,
  70. [IB_RNR_TIMER_040_96] = 40960,
  71. [IB_RNR_TIMER_061_44] = 61410,
  72. [IB_RNR_TIMER_081_92] = 81920,
  73. [IB_RNR_TIMER_122_88] = 122880,
  74. [IB_RNR_TIMER_163_84] = 163840,
  75. [IB_RNR_TIMER_245_76] = 245760,
  76. [IB_RNR_TIMER_327_68] = 327680,
  77. [IB_RNR_TIMER_491_52] = 491520,
  78. };
  79. static inline unsigned long rnrnak_jiffies(u8 timeout)
  80. {
  81. return max_t(unsigned long,
  82. usecs_to_jiffies(rnrnak_usec[timeout]), 1);
  83. }
  84. static enum ib_wc_opcode wr_to_wc_opcode(enum ib_wr_opcode opcode)
  85. {
  86. switch (opcode) {
  87. case IB_WR_RDMA_WRITE: return IB_WC_RDMA_WRITE;
  88. case IB_WR_RDMA_WRITE_WITH_IMM: return IB_WC_RDMA_WRITE;
  89. case IB_WR_SEND: return IB_WC_SEND;
  90. case IB_WR_SEND_WITH_IMM: return IB_WC_SEND;
  91. case IB_WR_RDMA_READ: return IB_WC_RDMA_READ;
  92. case IB_WR_ATOMIC_CMP_AND_SWP: return IB_WC_COMP_SWAP;
  93. case IB_WR_ATOMIC_FETCH_AND_ADD: return IB_WC_FETCH_ADD;
  94. case IB_WR_LSO: return IB_WC_LSO;
  95. case IB_WR_SEND_WITH_INV: return IB_WC_SEND;
  96. case IB_WR_RDMA_READ_WITH_INV: return IB_WC_RDMA_READ;
  97. case IB_WR_LOCAL_INV: return IB_WC_LOCAL_INV;
  98. case IB_WR_REG_MR: return IB_WC_REG_MR;
  99. case IB_WR_BIND_MW: return IB_WC_BIND_MW;
  100. default:
  101. return 0xff;
  102. }
  103. }
  104. void retransmit_timer(struct timer_list *t)
  105. {
  106. struct rxe_qp *qp = from_timer(qp, t, retrans_timer);
  107. pr_debug("%s: fired for qp#%d\n", __func__, qp->elem.index);
  108. if (qp->valid) {
  109. qp->comp.timeout = 1;
  110. rxe_sched_task(&qp->comp.task);
  111. }
  112. }
  113. void rxe_comp_queue_pkt(struct rxe_qp *qp, struct sk_buff *skb)
  114. {
  115. int must_sched;
  116. skb_queue_tail(&qp->resp_pkts, skb);
  117. must_sched = skb_queue_len(&qp->resp_pkts) > 1;
  118. if (must_sched != 0)
  119. rxe_counter_inc(SKB_TO_PKT(skb)->rxe, RXE_CNT_COMPLETER_SCHED);
  120. if (must_sched)
  121. rxe_sched_task(&qp->comp.task);
  122. else
  123. rxe_run_task(&qp->comp.task);
  124. }
  125. static inline enum comp_state get_wqe(struct rxe_qp *qp,
  126. struct rxe_pkt_info *pkt,
  127. struct rxe_send_wqe **wqe_p)
  128. {
  129. struct rxe_send_wqe *wqe;
  130. /* we come here whether or not we found a response packet to see if
  131. * there are any posted WQEs
  132. */
  133. wqe = queue_head(qp->sq.queue, QUEUE_TYPE_FROM_CLIENT);
  134. *wqe_p = wqe;
  135. /* no WQE or requester has not started it yet */
  136. if (!wqe || wqe->state == wqe_state_posted)
  137. return pkt ? COMPST_DONE : COMPST_EXIT;
  138. /* WQE does not require an ack */
  139. if (wqe->state == wqe_state_done)
  140. return COMPST_COMP_WQE;
  141. /* WQE caused an error */
  142. if (wqe->state == wqe_state_error)
  143. return COMPST_ERROR;
  144. /* we have a WQE, if we also have an ack check its PSN */
  145. return pkt ? COMPST_CHECK_PSN : COMPST_EXIT;
  146. }
  147. static inline void reset_retry_counters(struct rxe_qp *qp)
  148. {
  149. qp->comp.retry_cnt = qp->attr.retry_cnt;
  150. qp->comp.rnr_retry = qp->attr.rnr_retry;
  151. qp->comp.started_retry = 0;
  152. }
  153. static inline enum comp_state check_psn(struct rxe_qp *qp,
  154. struct rxe_pkt_info *pkt,
  155. struct rxe_send_wqe *wqe)
  156. {
  157. s32 diff;
  158. /* check to see if response is past the oldest WQE. if it is, complete
  159. * send/write or error read/atomic
  160. */
  161. diff = psn_compare(pkt->psn, wqe->last_psn);
  162. if (diff > 0) {
  163. if (wqe->state == wqe_state_pending) {
  164. if (wqe->mask & WR_ATOMIC_OR_READ_MASK)
  165. return COMPST_ERROR_RETRY;
  166. reset_retry_counters(qp);
  167. return COMPST_COMP_WQE;
  168. } else {
  169. return COMPST_DONE;
  170. }
  171. }
  172. /* compare response packet to expected response */
  173. diff = psn_compare(pkt->psn, qp->comp.psn);
  174. if (diff < 0) {
  175. /* response is most likely a retried packet if it matches an
  176. * uncompleted WQE go complete it else ignore it
  177. */
  178. if (pkt->psn == wqe->last_psn)
  179. return COMPST_COMP_ACK;
  180. else
  181. return COMPST_DONE;
  182. } else if ((diff > 0) && (wqe->mask & WR_ATOMIC_OR_READ_MASK)) {
  183. return COMPST_DONE;
  184. } else {
  185. return COMPST_CHECK_ACK;
  186. }
  187. }
  188. static inline enum comp_state check_ack(struct rxe_qp *qp,
  189. struct rxe_pkt_info *pkt,
  190. struct rxe_send_wqe *wqe)
  191. {
  192. unsigned int mask = pkt->mask;
  193. u8 syn;
  194. struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
  195. /* Check the sequence only */
  196. switch (qp->comp.opcode) {
  197. case -1:
  198. /* Will catch all *_ONLY cases. */
  199. if (!(mask & RXE_START_MASK))
  200. return COMPST_ERROR;
  201. break;
  202. case IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST:
  203. case IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE:
  204. if (pkt->opcode != IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE &&
  205. pkt->opcode != IB_OPCODE_RC_RDMA_READ_RESPONSE_LAST) {
  206. /* read retries of partial data may restart from
  207. * read response first or response only.
  208. */
  209. if ((pkt->psn == wqe->first_psn &&
  210. pkt->opcode ==
  211. IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST) ||
  212. (wqe->first_psn == wqe->last_psn &&
  213. pkt->opcode ==
  214. IB_OPCODE_RC_RDMA_READ_RESPONSE_ONLY))
  215. break;
  216. return COMPST_ERROR;
  217. }
  218. break;
  219. default:
  220. WARN_ON_ONCE(1);
  221. }
  222. /* Check operation validity. */
  223. switch (pkt->opcode) {
  224. case IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST:
  225. case IB_OPCODE_RC_RDMA_READ_RESPONSE_LAST:
  226. case IB_OPCODE_RC_RDMA_READ_RESPONSE_ONLY:
  227. syn = aeth_syn(pkt);
  228. if ((syn & AETH_TYPE_MASK) != AETH_ACK)
  229. return COMPST_ERROR;
  230. fallthrough;
  231. /* (IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE doesn't have an AETH)
  232. */
  233. case IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE:
  234. if (wqe->wr.opcode != IB_WR_RDMA_READ &&
  235. wqe->wr.opcode != IB_WR_RDMA_READ_WITH_INV) {
  236. wqe->status = IB_WC_FATAL_ERR;
  237. return COMPST_ERROR;
  238. }
  239. reset_retry_counters(qp);
  240. return COMPST_READ;
  241. case IB_OPCODE_RC_ATOMIC_ACKNOWLEDGE:
  242. syn = aeth_syn(pkt);
  243. if ((syn & AETH_TYPE_MASK) != AETH_ACK)
  244. return COMPST_ERROR;
  245. if (wqe->wr.opcode != IB_WR_ATOMIC_CMP_AND_SWP &&
  246. wqe->wr.opcode != IB_WR_ATOMIC_FETCH_AND_ADD)
  247. return COMPST_ERROR;
  248. reset_retry_counters(qp);
  249. return COMPST_ATOMIC;
  250. case IB_OPCODE_RC_ACKNOWLEDGE:
  251. syn = aeth_syn(pkt);
  252. switch (syn & AETH_TYPE_MASK) {
  253. case AETH_ACK:
  254. reset_retry_counters(qp);
  255. return COMPST_WRITE_SEND;
  256. case AETH_RNR_NAK:
  257. rxe_counter_inc(rxe, RXE_CNT_RCV_RNR);
  258. return COMPST_RNR_RETRY;
  259. case AETH_NAK:
  260. switch (syn) {
  261. case AETH_NAK_PSN_SEQ_ERROR:
  262. /* a nak implicitly acks all packets with psns
  263. * before
  264. */
  265. if (psn_compare(pkt->psn, qp->comp.psn) > 0) {
  266. rxe_counter_inc(rxe,
  267. RXE_CNT_RCV_SEQ_ERR);
  268. qp->comp.psn = pkt->psn;
  269. if (qp->req.wait_psn) {
  270. qp->req.wait_psn = 0;
  271. rxe_run_task(&qp->req.task);
  272. }
  273. }
  274. return COMPST_ERROR_RETRY;
  275. case AETH_NAK_INVALID_REQ:
  276. wqe->status = IB_WC_REM_INV_REQ_ERR;
  277. return COMPST_ERROR;
  278. case AETH_NAK_REM_ACC_ERR:
  279. wqe->status = IB_WC_REM_ACCESS_ERR;
  280. return COMPST_ERROR;
  281. case AETH_NAK_REM_OP_ERR:
  282. wqe->status = IB_WC_REM_OP_ERR;
  283. return COMPST_ERROR;
  284. default:
  285. pr_warn("unexpected nak %x\n", syn);
  286. wqe->status = IB_WC_REM_OP_ERR;
  287. return COMPST_ERROR;
  288. }
  289. default:
  290. return COMPST_ERROR;
  291. }
  292. break;
  293. default:
  294. pr_warn("unexpected opcode\n");
  295. }
  296. return COMPST_ERROR;
  297. }
  298. static inline enum comp_state do_read(struct rxe_qp *qp,
  299. struct rxe_pkt_info *pkt,
  300. struct rxe_send_wqe *wqe)
  301. {
  302. int ret;
  303. ret = copy_data(qp->pd, IB_ACCESS_LOCAL_WRITE,
  304. &wqe->dma, payload_addr(pkt),
  305. payload_size(pkt), RXE_TO_MR_OBJ);
  306. if (ret) {
  307. wqe->status = IB_WC_LOC_PROT_ERR;
  308. return COMPST_ERROR;
  309. }
  310. if (wqe->dma.resid == 0 && (pkt->mask & RXE_END_MASK))
  311. return COMPST_COMP_ACK;
  312. return COMPST_UPDATE_COMP;
  313. }
  314. static inline enum comp_state do_atomic(struct rxe_qp *qp,
  315. struct rxe_pkt_info *pkt,
  316. struct rxe_send_wqe *wqe)
  317. {
  318. int ret;
  319. u64 atomic_orig = atmack_orig(pkt);
  320. ret = copy_data(qp->pd, IB_ACCESS_LOCAL_WRITE,
  321. &wqe->dma, &atomic_orig,
  322. sizeof(u64), RXE_TO_MR_OBJ);
  323. if (ret) {
  324. wqe->status = IB_WC_LOC_PROT_ERR;
  325. return COMPST_ERROR;
  326. }
  327. return COMPST_COMP_ACK;
  328. }
  329. static void make_send_cqe(struct rxe_qp *qp, struct rxe_send_wqe *wqe,
  330. struct rxe_cqe *cqe)
  331. {
  332. struct ib_wc *wc = &cqe->ibwc;
  333. struct ib_uverbs_wc *uwc = &cqe->uibwc;
  334. memset(cqe, 0, sizeof(*cqe));
  335. if (!qp->is_user) {
  336. wc->wr_id = wqe->wr.wr_id;
  337. wc->status = wqe->status;
  338. wc->qp = &qp->ibqp;
  339. } else {
  340. uwc->wr_id = wqe->wr.wr_id;
  341. uwc->status = wqe->status;
  342. uwc->qp_num = qp->ibqp.qp_num;
  343. }
  344. if (wqe->status == IB_WC_SUCCESS) {
  345. if (!qp->is_user) {
  346. wc->opcode = wr_to_wc_opcode(wqe->wr.opcode);
  347. if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
  348. wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
  349. wc->wc_flags = IB_WC_WITH_IMM;
  350. wc->byte_len = wqe->dma.length;
  351. } else {
  352. uwc->opcode = wr_to_wc_opcode(wqe->wr.opcode);
  353. if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
  354. wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
  355. uwc->wc_flags = IB_WC_WITH_IMM;
  356. uwc->byte_len = wqe->dma.length;
  357. }
  358. }
  359. }
  360. /*
  361. * IBA Spec. Section 10.7.3.1 SIGNALED COMPLETIONS
  362. * ---------8<---------8<-------------
  363. * ...Note that if a completion error occurs, a Work Completion
  364. * will always be generated, even if the signaling
  365. * indicator requests an Unsignaled Completion.
  366. * ---------8<---------8<-------------
  367. */
  368. static void do_complete(struct rxe_qp *qp, struct rxe_send_wqe *wqe)
  369. {
  370. struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
  371. struct rxe_cqe cqe;
  372. bool post;
  373. /* do we need to post a completion */
  374. post = ((qp->sq_sig_type == IB_SIGNAL_ALL_WR) ||
  375. (wqe->wr.send_flags & IB_SEND_SIGNALED) ||
  376. wqe->status != IB_WC_SUCCESS);
  377. if (post)
  378. make_send_cqe(qp, wqe, &cqe);
  379. queue_advance_consumer(qp->sq.queue, QUEUE_TYPE_FROM_CLIENT);
  380. if (post)
  381. rxe_cq_post(qp->scq, &cqe, 0);
  382. if (wqe->wr.opcode == IB_WR_SEND ||
  383. wqe->wr.opcode == IB_WR_SEND_WITH_IMM ||
  384. wqe->wr.opcode == IB_WR_SEND_WITH_INV)
  385. rxe_counter_inc(rxe, RXE_CNT_RDMA_SEND);
  386. /*
  387. * we completed something so let req run again
  388. * if it is trying to fence
  389. */
  390. if (qp->req.wait_fence) {
  391. qp->req.wait_fence = 0;
  392. rxe_run_task(&qp->req.task);
  393. }
  394. }
  395. static inline enum comp_state complete_ack(struct rxe_qp *qp,
  396. struct rxe_pkt_info *pkt,
  397. struct rxe_send_wqe *wqe)
  398. {
  399. if (wqe->has_rd_atomic) {
  400. wqe->has_rd_atomic = 0;
  401. atomic_inc(&qp->req.rd_atomic);
  402. if (qp->req.need_rd_atomic) {
  403. qp->comp.timeout_retry = 0;
  404. qp->req.need_rd_atomic = 0;
  405. rxe_run_task(&qp->req.task);
  406. }
  407. }
  408. if (unlikely(qp->req.state == QP_STATE_DRAIN)) {
  409. /* state_lock used by requester & completer */
  410. spin_lock_bh(&qp->state_lock);
  411. if ((qp->req.state == QP_STATE_DRAIN) &&
  412. (qp->comp.psn == qp->req.psn)) {
  413. qp->req.state = QP_STATE_DRAINED;
  414. spin_unlock_bh(&qp->state_lock);
  415. if (qp->ibqp.event_handler) {
  416. struct ib_event ev;
  417. ev.device = qp->ibqp.device;
  418. ev.element.qp = &qp->ibqp;
  419. ev.event = IB_EVENT_SQ_DRAINED;
  420. qp->ibqp.event_handler(&ev,
  421. qp->ibqp.qp_context);
  422. }
  423. } else {
  424. spin_unlock_bh(&qp->state_lock);
  425. }
  426. }
  427. do_complete(qp, wqe);
  428. if (psn_compare(pkt->psn, qp->comp.psn) >= 0)
  429. return COMPST_UPDATE_COMP;
  430. else
  431. return COMPST_DONE;
  432. }
  433. static inline enum comp_state complete_wqe(struct rxe_qp *qp,
  434. struct rxe_pkt_info *pkt,
  435. struct rxe_send_wqe *wqe)
  436. {
  437. if (pkt && wqe->state == wqe_state_pending) {
  438. if (psn_compare(wqe->last_psn, qp->comp.psn) >= 0) {
  439. qp->comp.psn = (wqe->last_psn + 1) & BTH_PSN_MASK;
  440. qp->comp.opcode = -1;
  441. }
  442. if (qp->req.wait_psn) {
  443. qp->req.wait_psn = 0;
  444. rxe_sched_task(&qp->req.task);
  445. }
  446. }
  447. do_complete(qp, wqe);
  448. return COMPST_GET_WQE;
  449. }
  450. static void rxe_drain_resp_pkts(struct rxe_qp *qp, bool notify)
  451. {
  452. struct sk_buff *skb;
  453. struct rxe_send_wqe *wqe;
  454. struct rxe_queue *q = qp->sq.queue;
  455. while ((skb = skb_dequeue(&qp->resp_pkts))) {
  456. rxe_put(qp);
  457. kfree_skb(skb);
  458. ib_device_put(qp->ibqp.device);
  459. }
  460. while ((wqe = queue_head(q, q->type))) {
  461. if (notify) {
  462. wqe->status = IB_WC_WR_FLUSH_ERR;
  463. do_complete(qp, wqe);
  464. } else {
  465. queue_advance_consumer(q, q->type);
  466. }
  467. }
  468. }
  469. static void free_pkt(struct rxe_pkt_info *pkt)
  470. {
  471. struct sk_buff *skb = PKT_TO_SKB(pkt);
  472. struct rxe_qp *qp = pkt->qp;
  473. struct ib_device *dev = qp->ibqp.device;
  474. kfree_skb(skb);
  475. rxe_put(qp);
  476. ib_device_put(dev);
  477. }
  478. int rxe_completer(void *arg)
  479. {
  480. struct rxe_qp *qp = (struct rxe_qp *)arg;
  481. struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
  482. struct rxe_send_wqe *wqe = NULL;
  483. struct sk_buff *skb = NULL;
  484. struct rxe_pkt_info *pkt = NULL;
  485. enum comp_state state;
  486. int ret;
  487. if (!rxe_get(qp))
  488. return -EAGAIN;
  489. if (!qp->valid || qp->comp.state == QP_STATE_ERROR ||
  490. qp->comp.state == QP_STATE_RESET) {
  491. rxe_drain_resp_pkts(qp, qp->valid &&
  492. qp->comp.state == QP_STATE_ERROR);
  493. goto exit;
  494. }
  495. if (qp->comp.timeout) {
  496. qp->comp.timeout_retry = 1;
  497. qp->comp.timeout = 0;
  498. } else {
  499. qp->comp.timeout_retry = 0;
  500. }
  501. if (qp->req.need_retry)
  502. goto exit;
  503. state = COMPST_GET_ACK;
  504. while (1) {
  505. pr_debug("qp#%d state = %s\n", qp_num(qp),
  506. comp_state_name[state]);
  507. switch (state) {
  508. case COMPST_GET_ACK:
  509. skb = skb_dequeue(&qp->resp_pkts);
  510. if (skb) {
  511. pkt = SKB_TO_PKT(skb);
  512. qp->comp.timeout_retry = 0;
  513. }
  514. state = COMPST_GET_WQE;
  515. break;
  516. case COMPST_GET_WQE:
  517. state = get_wqe(qp, pkt, &wqe);
  518. break;
  519. case COMPST_CHECK_PSN:
  520. state = check_psn(qp, pkt, wqe);
  521. break;
  522. case COMPST_CHECK_ACK:
  523. state = check_ack(qp, pkt, wqe);
  524. break;
  525. case COMPST_READ:
  526. state = do_read(qp, pkt, wqe);
  527. break;
  528. case COMPST_ATOMIC:
  529. state = do_atomic(qp, pkt, wqe);
  530. break;
  531. case COMPST_WRITE_SEND:
  532. if (wqe->state == wqe_state_pending &&
  533. wqe->last_psn == pkt->psn)
  534. state = COMPST_COMP_ACK;
  535. else
  536. state = COMPST_UPDATE_COMP;
  537. break;
  538. case COMPST_COMP_ACK:
  539. state = complete_ack(qp, pkt, wqe);
  540. break;
  541. case COMPST_COMP_WQE:
  542. state = complete_wqe(qp, pkt, wqe);
  543. break;
  544. case COMPST_UPDATE_COMP:
  545. if (pkt->mask & RXE_END_MASK)
  546. qp->comp.opcode = -1;
  547. else
  548. qp->comp.opcode = pkt->opcode;
  549. if (psn_compare(pkt->psn, qp->comp.psn) >= 0)
  550. qp->comp.psn = (pkt->psn + 1) & BTH_PSN_MASK;
  551. if (qp->req.wait_psn) {
  552. qp->req.wait_psn = 0;
  553. rxe_sched_task(&qp->req.task);
  554. }
  555. state = COMPST_DONE;
  556. break;
  557. case COMPST_DONE:
  558. goto done;
  559. case COMPST_EXIT:
  560. if (qp->comp.timeout_retry && wqe) {
  561. state = COMPST_ERROR_RETRY;
  562. break;
  563. }
  564. /* re reset the timeout counter if
  565. * (1) QP is type RC
  566. * (2) the QP is alive
  567. * (3) there is a packet sent by the requester that
  568. * might be acked (we still might get spurious
  569. * timeouts but try to keep them as few as possible)
  570. * (4) the timeout parameter is set
  571. */
  572. if ((qp_type(qp) == IB_QPT_RC) &&
  573. (qp->req.state == QP_STATE_READY) &&
  574. (psn_compare(qp->req.psn, qp->comp.psn) > 0) &&
  575. qp->qp_timeout_jiffies)
  576. mod_timer(&qp->retrans_timer,
  577. jiffies + qp->qp_timeout_jiffies);
  578. goto exit;
  579. case COMPST_ERROR_RETRY:
  580. /* we come here if the retry timer fired and we did
  581. * not receive a response packet. try to retry the send
  582. * queue if that makes sense and the limits have not
  583. * been exceeded. remember that some timeouts are
  584. * spurious since we do not reset the timer but kick
  585. * it down the road or let it expire
  586. */
  587. /* there is nothing to retry in this case */
  588. if (!wqe || (wqe->state == wqe_state_posted))
  589. goto exit;
  590. /* if we've started a retry, don't start another
  591. * retry sequence, unless this is a timeout.
  592. */
  593. if (qp->comp.started_retry &&
  594. !qp->comp.timeout_retry)
  595. goto done;
  596. if (qp->comp.retry_cnt > 0) {
  597. if (qp->comp.retry_cnt != 7)
  598. qp->comp.retry_cnt--;
  599. /* no point in retrying if we have already
  600. * seen the last ack that the requester could
  601. * have caused
  602. */
  603. if (psn_compare(qp->req.psn,
  604. qp->comp.psn) > 0) {
  605. /* tell the requester to retry the
  606. * send queue next time around
  607. */
  608. rxe_counter_inc(rxe,
  609. RXE_CNT_COMP_RETRY);
  610. qp->req.need_retry = 1;
  611. qp->comp.started_retry = 1;
  612. rxe_run_task(&qp->req.task);
  613. }
  614. goto done;
  615. } else {
  616. rxe_counter_inc(rxe, RXE_CNT_RETRY_EXCEEDED);
  617. wqe->status = IB_WC_RETRY_EXC_ERR;
  618. state = COMPST_ERROR;
  619. }
  620. break;
  621. case COMPST_RNR_RETRY:
  622. /* we come here if we received an RNR NAK */
  623. if (qp->comp.rnr_retry > 0) {
  624. if (qp->comp.rnr_retry != 7)
  625. qp->comp.rnr_retry--;
  626. /* don't start a retry flow until the
  627. * rnr timer has fired
  628. */
  629. qp->req.wait_for_rnr_timer = 1;
  630. pr_debug("qp#%d set rnr nak timer\n",
  631. qp_num(qp));
  632. mod_timer(&qp->rnr_nak_timer,
  633. jiffies + rnrnak_jiffies(aeth_syn(pkt)
  634. & ~AETH_TYPE_MASK));
  635. goto exit;
  636. } else {
  637. rxe_counter_inc(rxe,
  638. RXE_CNT_RNR_RETRY_EXCEEDED);
  639. wqe->status = IB_WC_RNR_RETRY_EXC_ERR;
  640. state = COMPST_ERROR;
  641. }
  642. break;
  643. case COMPST_ERROR:
  644. WARN_ON_ONCE(wqe->status == IB_WC_SUCCESS);
  645. do_complete(qp, wqe);
  646. rxe_qp_error(qp);
  647. goto exit;
  648. }
  649. }
  650. /* A non-zero return value will cause rxe_do_task to
  651. * exit its loop and end the tasklet. A zero return
  652. * will continue looping and return to rxe_completer
  653. */
  654. done:
  655. ret = 0;
  656. goto out;
  657. exit:
  658. ret = -EAGAIN;
  659. out:
  660. if (pkt)
  661. free_pkt(pkt);
  662. rxe_put(qp);
  663. return ret;
  664. }