rxe_comp.c 19 KB

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