uc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
  2. /*
  3. * Copyright(c) 2015 - 2018 Intel Corporation.
  4. */
  5. #include "hfi.h"
  6. #include "verbs_txreq.h"
  7. #include "qp.h"
  8. /* cut down ridiculously long IB macro names */
  9. #define OP(x) UC_OP(x)
  10. /**
  11. * hfi1_make_uc_req - construct a request packet (SEND, RDMA write)
  12. * @qp: a pointer to the QP
  13. * @ps: the current packet state
  14. *
  15. * Assume s_lock is held.
  16. *
  17. * Return 1 if constructed; otherwise, return 0.
  18. */
  19. int hfi1_make_uc_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
  20. {
  21. struct hfi1_qp_priv *priv = qp->priv;
  22. struct ib_other_headers *ohdr;
  23. struct rvt_swqe *wqe;
  24. u32 hwords;
  25. u32 bth0 = 0;
  26. u32 len;
  27. u32 pmtu = qp->pmtu;
  28. int middle = 0;
  29. ps->s_txreq = get_txreq(ps->dev, qp);
  30. if (!ps->s_txreq)
  31. goto bail_no_tx;
  32. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_SEND_OK)) {
  33. if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
  34. goto bail;
  35. /* We are in the error state, flush the work request. */
  36. if (qp->s_last == READ_ONCE(qp->s_head))
  37. goto bail;
  38. /* If DMAs are in progress, we can't flush immediately. */
  39. if (iowait_sdma_pending(&priv->s_iowait)) {
  40. qp->s_flags |= RVT_S_WAIT_DMA;
  41. goto bail;
  42. }
  43. clear_ahg(qp);
  44. wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  45. rvt_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
  46. goto done_free_tx;
  47. }
  48. if (priv->hdr_type == HFI1_PKT_TYPE_9B) {
  49. /* header size in 32-bit words LRH+BTH = (8+12)/4. */
  50. hwords = 5;
  51. if (rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)
  52. ohdr = &ps->s_txreq->phdr.hdr.ibh.u.l.oth;
  53. else
  54. ohdr = &ps->s_txreq->phdr.hdr.ibh.u.oth;
  55. } else {
  56. /* header size in 32-bit words 16B LRH+BTH = (16+12)/4. */
  57. hwords = 7;
  58. if ((rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH) &&
  59. (hfi1_check_mcast(rdma_ah_get_dlid(&qp->remote_ah_attr))))
  60. ohdr = &ps->s_txreq->phdr.hdr.opah.u.l.oth;
  61. else
  62. ohdr = &ps->s_txreq->phdr.hdr.opah.u.oth;
  63. }
  64. /* Get the next send request. */
  65. wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
  66. qp->s_wqe = NULL;
  67. switch (qp->s_state) {
  68. default:
  69. if (!(ib_rvt_state_ops[qp->state] &
  70. RVT_PROCESS_NEXT_SEND_OK))
  71. goto bail;
  72. /* Check if send work queue is empty. */
  73. if (qp->s_cur == READ_ONCE(qp->s_head)) {
  74. clear_ahg(qp);
  75. goto bail;
  76. }
  77. /*
  78. * Local operations are processed immediately
  79. * after all prior requests have completed.
  80. */
  81. if (wqe->wr.opcode == IB_WR_REG_MR ||
  82. wqe->wr.opcode == IB_WR_LOCAL_INV) {
  83. int local_ops = 0;
  84. int err = 0;
  85. if (qp->s_last != qp->s_cur)
  86. goto bail;
  87. if (++qp->s_cur == qp->s_size)
  88. qp->s_cur = 0;
  89. if (!(wqe->wr.send_flags & RVT_SEND_COMPLETION_ONLY)) {
  90. err = rvt_invalidate_rkey(
  91. qp, wqe->wr.ex.invalidate_rkey);
  92. local_ops = 1;
  93. }
  94. rvt_send_complete(qp, wqe, err ? IB_WC_LOC_PROT_ERR
  95. : IB_WC_SUCCESS);
  96. if (local_ops)
  97. atomic_dec(&qp->local_ops_pending);
  98. goto done_free_tx;
  99. }
  100. /*
  101. * Start a new request.
  102. */
  103. qp->s_psn = wqe->psn;
  104. qp->s_sge.sge = wqe->sg_list[0];
  105. qp->s_sge.sg_list = wqe->sg_list + 1;
  106. qp->s_sge.num_sge = wqe->wr.num_sge;
  107. qp->s_sge.total_len = wqe->length;
  108. len = wqe->length;
  109. qp->s_len = len;
  110. switch (wqe->wr.opcode) {
  111. case IB_WR_SEND:
  112. case IB_WR_SEND_WITH_IMM:
  113. if (len > pmtu) {
  114. qp->s_state = OP(SEND_FIRST);
  115. len = pmtu;
  116. break;
  117. }
  118. if (wqe->wr.opcode == IB_WR_SEND) {
  119. qp->s_state = OP(SEND_ONLY);
  120. } else {
  121. qp->s_state =
  122. OP(SEND_ONLY_WITH_IMMEDIATE);
  123. /* Immediate data comes after the BTH */
  124. ohdr->u.imm_data = wqe->wr.ex.imm_data;
  125. hwords += 1;
  126. }
  127. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  128. bth0 |= IB_BTH_SOLICITED;
  129. qp->s_wqe = wqe;
  130. if (++qp->s_cur >= qp->s_size)
  131. qp->s_cur = 0;
  132. break;
  133. case IB_WR_RDMA_WRITE:
  134. case IB_WR_RDMA_WRITE_WITH_IMM:
  135. ohdr->u.rc.reth.vaddr =
  136. cpu_to_be64(wqe->rdma_wr.remote_addr);
  137. ohdr->u.rc.reth.rkey =
  138. cpu_to_be32(wqe->rdma_wr.rkey);
  139. ohdr->u.rc.reth.length = cpu_to_be32(len);
  140. hwords += sizeof(struct ib_reth) / 4;
  141. if (len > pmtu) {
  142. qp->s_state = OP(RDMA_WRITE_FIRST);
  143. len = pmtu;
  144. break;
  145. }
  146. if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
  147. qp->s_state = OP(RDMA_WRITE_ONLY);
  148. } else {
  149. qp->s_state =
  150. OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
  151. /* Immediate data comes after the RETH */
  152. ohdr->u.rc.imm_data = wqe->wr.ex.imm_data;
  153. hwords += 1;
  154. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  155. bth0 |= IB_BTH_SOLICITED;
  156. }
  157. qp->s_wqe = wqe;
  158. if (++qp->s_cur >= qp->s_size)
  159. qp->s_cur = 0;
  160. break;
  161. default:
  162. goto bail;
  163. }
  164. break;
  165. case OP(SEND_FIRST):
  166. qp->s_state = OP(SEND_MIDDLE);
  167. fallthrough;
  168. case OP(SEND_MIDDLE):
  169. len = qp->s_len;
  170. if (len > pmtu) {
  171. len = pmtu;
  172. middle = HFI1_CAP_IS_KSET(SDMA_AHG);
  173. break;
  174. }
  175. if (wqe->wr.opcode == IB_WR_SEND) {
  176. qp->s_state = OP(SEND_LAST);
  177. } else {
  178. qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
  179. /* Immediate data comes after the BTH */
  180. ohdr->u.imm_data = wqe->wr.ex.imm_data;
  181. hwords += 1;
  182. }
  183. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  184. bth0 |= IB_BTH_SOLICITED;
  185. qp->s_wqe = wqe;
  186. if (++qp->s_cur >= qp->s_size)
  187. qp->s_cur = 0;
  188. break;
  189. case OP(RDMA_WRITE_FIRST):
  190. qp->s_state = OP(RDMA_WRITE_MIDDLE);
  191. fallthrough;
  192. case OP(RDMA_WRITE_MIDDLE):
  193. len = qp->s_len;
  194. if (len > pmtu) {
  195. len = pmtu;
  196. middle = HFI1_CAP_IS_KSET(SDMA_AHG);
  197. break;
  198. }
  199. if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
  200. qp->s_state = OP(RDMA_WRITE_LAST);
  201. } else {
  202. qp->s_state =
  203. OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
  204. /* Immediate data comes after the BTH */
  205. ohdr->u.imm_data = wqe->wr.ex.imm_data;
  206. hwords += 1;
  207. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  208. bth0 |= IB_BTH_SOLICITED;
  209. }
  210. qp->s_wqe = wqe;
  211. if (++qp->s_cur >= qp->s_size)
  212. qp->s_cur = 0;
  213. break;
  214. }
  215. qp->s_len -= len;
  216. ps->s_txreq->hdr_dwords = hwords;
  217. ps->s_txreq->sde = priv->s_sde;
  218. ps->s_txreq->ss = &qp->s_sge;
  219. ps->s_txreq->s_cur_size = len;
  220. hfi1_make_ruc_header(qp, ohdr, bth0 | (qp->s_state << 24),
  221. qp->remote_qpn, mask_psn(qp->s_psn++),
  222. middle, ps);
  223. return 1;
  224. done_free_tx:
  225. hfi1_put_txreq(ps->s_txreq);
  226. ps->s_txreq = NULL;
  227. return 1;
  228. bail:
  229. hfi1_put_txreq(ps->s_txreq);
  230. bail_no_tx:
  231. ps->s_txreq = NULL;
  232. qp->s_flags &= ~RVT_S_BUSY;
  233. return 0;
  234. }
  235. /**
  236. * hfi1_uc_rcv - handle an incoming UC packet
  237. * @packet: the packet structure
  238. *
  239. * This is called from qp_rcv() to process an incoming UC packet
  240. * for the given QP.
  241. * Called at interrupt level.
  242. */
  243. void hfi1_uc_rcv(struct hfi1_packet *packet)
  244. {
  245. struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
  246. void *data = packet->payload;
  247. u32 tlen = packet->tlen;
  248. struct rvt_qp *qp = packet->qp;
  249. struct ib_other_headers *ohdr = packet->ohdr;
  250. u32 opcode = packet->opcode;
  251. u32 hdrsize = packet->hlen;
  252. u32 psn;
  253. u32 pad = packet->pad;
  254. struct ib_wc wc;
  255. u32 pmtu = qp->pmtu;
  256. struct ib_reth *reth;
  257. int ret;
  258. u8 extra_bytes = pad + packet->extra_byte + (SIZE_OF_CRC << 2);
  259. if (hfi1_ruc_check_hdr(ibp, packet))
  260. return;
  261. process_ecn(qp, packet);
  262. psn = ib_bth_get_psn(ohdr);
  263. /* Compare the PSN verses the expected PSN. */
  264. if (unlikely(cmp_psn(psn, qp->r_psn) != 0)) {
  265. /*
  266. * Handle a sequence error.
  267. * Silently drop any current message.
  268. */
  269. qp->r_psn = psn;
  270. inv:
  271. if (qp->r_state == OP(SEND_FIRST) ||
  272. qp->r_state == OP(SEND_MIDDLE)) {
  273. set_bit(RVT_R_REWIND_SGE, &qp->r_aflags);
  274. qp->r_sge.num_sge = 0;
  275. } else {
  276. rvt_put_ss(&qp->r_sge);
  277. }
  278. qp->r_state = OP(SEND_LAST);
  279. switch (opcode) {
  280. case OP(SEND_FIRST):
  281. case OP(SEND_ONLY):
  282. case OP(SEND_ONLY_WITH_IMMEDIATE):
  283. goto send_first;
  284. case OP(RDMA_WRITE_FIRST):
  285. case OP(RDMA_WRITE_ONLY):
  286. case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
  287. goto rdma_first;
  288. default:
  289. goto drop;
  290. }
  291. }
  292. /* Check for opcode sequence errors. */
  293. switch (qp->r_state) {
  294. case OP(SEND_FIRST):
  295. case OP(SEND_MIDDLE):
  296. if (opcode == OP(SEND_MIDDLE) ||
  297. opcode == OP(SEND_LAST) ||
  298. opcode == OP(SEND_LAST_WITH_IMMEDIATE))
  299. break;
  300. goto inv;
  301. case OP(RDMA_WRITE_FIRST):
  302. case OP(RDMA_WRITE_MIDDLE):
  303. if (opcode == OP(RDMA_WRITE_MIDDLE) ||
  304. opcode == OP(RDMA_WRITE_LAST) ||
  305. opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
  306. break;
  307. goto inv;
  308. default:
  309. if (opcode == OP(SEND_FIRST) ||
  310. opcode == OP(SEND_ONLY) ||
  311. opcode == OP(SEND_ONLY_WITH_IMMEDIATE) ||
  312. opcode == OP(RDMA_WRITE_FIRST) ||
  313. opcode == OP(RDMA_WRITE_ONLY) ||
  314. opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
  315. break;
  316. goto inv;
  317. }
  318. if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
  319. rvt_comm_est(qp);
  320. /* OK, process the packet. */
  321. switch (opcode) {
  322. case OP(SEND_FIRST):
  323. case OP(SEND_ONLY):
  324. case OP(SEND_ONLY_WITH_IMMEDIATE):
  325. send_first:
  326. if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags)) {
  327. qp->r_sge = qp->s_rdma_read_sge;
  328. } else {
  329. ret = rvt_get_rwqe(qp, false);
  330. if (ret < 0)
  331. goto op_err;
  332. if (!ret)
  333. goto drop;
  334. /*
  335. * qp->s_rdma_read_sge will be the owner
  336. * of the mr references.
  337. */
  338. qp->s_rdma_read_sge = qp->r_sge;
  339. }
  340. qp->r_rcv_len = 0;
  341. if (opcode == OP(SEND_ONLY))
  342. goto no_immediate_data;
  343. else if (opcode == OP(SEND_ONLY_WITH_IMMEDIATE))
  344. goto send_last_imm;
  345. fallthrough;
  346. case OP(SEND_MIDDLE):
  347. /* Check for invalid length PMTU or posted rwqe len. */
  348. /*
  349. * There will be no padding for 9B packet but 16B packets
  350. * will come in with some padding since we always add
  351. * CRC and LT bytes which will need to be flit aligned
  352. */
  353. if (unlikely(tlen != (hdrsize + pmtu + extra_bytes)))
  354. goto rewind;
  355. qp->r_rcv_len += pmtu;
  356. if (unlikely(qp->r_rcv_len > qp->r_len))
  357. goto rewind;
  358. rvt_copy_sge(qp, &qp->r_sge, data, pmtu, false, false);
  359. break;
  360. case OP(SEND_LAST_WITH_IMMEDIATE):
  361. send_last_imm:
  362. wc.ex.imm_data = ohdr->u.imm_data;
  363. wc.wc_flags = IB_WC_WITH_IMM;
  364. goto send_last;
  365. case OP(SEND_LAST):
  366. no_immediate_data:
  367. wc.ex.imm_data = 0;
  368. wc.wc_flags = 0;
  369. send_last:
  370. /* Check for invalid length. */
  371. /* LAST len should be >= 1 */
  372. if (unlikely(tlen < (hdrsize + extra_bytes)))
  373. goto rewind;
  374. /* Don't count the CRC. */
  375. tlen -= (hdrsize + extra_bytes);
  376. wc.byte_len = tlen + qp->r_rcv_len;
  377. if (unlikely(wc.byte_len > qp->r_len))
  378. goto rewind;
  379. wc.opcode = IB_WC_RECV;
  380. rvt_copy_sge(qp, &qp->r_sge, data, tlen, false, false);
  381. rvt_put_ss(&qp->s_rdma_read_sge);
  382. last_imm:
  383. wc.wr_id = qp->r_wr_id;
  384. wc.status = IB_WC_SUCCESS;
  385. wc.qp = &qp->ibqp;
  386. wc.src_qp = qp->remote_qpn;
  387. wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr) & U16_MAX;
  388. /*
  389. * It seems that IB mandates the presence of an SL in a
  390. * work completion only for the UD transport (see section
  391. * 11.4.2 of IBTA Vol. 1).
  392. *
  393. * However, the way the SL is chosen below is consistent
  394. * with the way that IB/qib works and is trying avoid
  395. * introducing incompatibilities.
  396. *
  397. * See also OPA Vol. 1, section 9.7.6, and table 9-17.
  398. */
  399. wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
  400. /* zero fields that are N/A */
  401. wc.vendor_err = 0;
  402. wc.pkey_index = 0;
  403. wc.dlid_path_bits = 0;
  404. wc.port_num = 0;
  405. /* Signal completion event if the solicited bit is set. */
  406. rvt_recv_cq(qp, &wc, ib_bth_is_solicited(ohdr));
  407. break;
  408. case OP(RDMA_WRITE_FIRST):
  409. case OP(RDMA_WRITE_ONLY):
  410. case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE): /* consume RWQE */
  411. rdma_first:
  412. if (unlikely(!(qp->qp_access_flags &
  413. IB_ACCESS_REMOTE_WRITE))) {
  414. goto drop;
  415. }
  416. reth = &ohdr->u.rc.reth;
  417. qp->r_len = be32_to_cpu(reth->length);
  418. qp->r_rcv_len = 0;
  419. qp->r_sge.sg_list = NULL;
  420. if (qp->r_len != 0) {
  421. u32 rkey = be32_to_cpu(reth->rkey);
  422. u64 vaddr = be64_to_cpu(reth->vaddr);
  423. int ok;
  424. /* Check rkey */
  425. ok = rvt_rkey_ok(qp, &qp->r_sge.sge, qp->r_len,
  426. vaddr, rkey, IB_ACCESS_REMOTE_WRITE);
  427. if (unlikely(!ok))
  428. goto drop;
  429. qp->r_sge.num_sge = 1;
  430. } else {
  431. qp->r_sge.num_sge = 0;
  432. qp->r_sge.sge.mr = NULL;
  433. qp->r_sge.sge.vaddr = NULL;
  434. qp->r_sge.sge.length = 0;
  435. qp->r_sge.sge.sge_length = 0;
  436. }
  437. if (opcode == OP(RDMA_WRITE_ONLY)) {
  438. goto rdma_last;
  439. } else if (opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE)) {
  440. wc.ex.imm_data = ohdr->u.rc.imm_data;
  441. goto rdma_last_imm;
  442. }
  443. fallthrough;
  444. case OP(RDMA_WRITE_MIDDLE):
  445. /* Check for invalid length PMTU or posted rwqe len. */
  446. if (unlikely(tlen != (hdrsize + pmtu + 4)))
  447. goto drop;
  448. qp->r_rcv_len += pmtu;
  449. if (unlikely(qp->r_rcv_len > qp->r_len))
  450. goto drop;
  451. rvt_copy_sge(qp, &qp->r_sge, data, pmtu, true, false);
  452. break;
  453. case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
  454. wc.ex.imm_data = ohdr->u.imm_data;
  455. rdma_last_imm:
  456. wc.wc_flags = IB_WC_WITH_IMM;
  457. /* Check for invalid length. */
  458. /* LAST len should be >= 1 */
  459. if (unlikely(tlen < (hdrsize + pad + 4)))
  460. goto drop;
  461. /* Don't count the CRC. */
  462. tlen -= (hdrsize + extra_bytes);
  463. if (unlikely(tlen + qp->r_rcv_len != qp->r_len))
  464. goto drop;
  465. if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags)) {
  466. rvt_put_ss(&qp->s_rdma_read_sge);
  467. } else {
  468. ret = rvt_get_rwqe(qp, true);
  469. if (ret < 0)
  470. goto op_err;
  471. if (!ret)
  472. goto drop;
  473. }
  474. wc.byte_len = qp->r_len;
  475. wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
  476. rvt_copy_sge(qp, &qp->r_sge, data, tlen, true, false);
  477. rvt_put_ss(&qp->r_sge);
  478. goto last_imm;
  479. case OP(RDMA_WRITE_LAST):
  480. rdma_last:
  481. /* Check for invalid length. */
  482. /* LAST len should be >= 1 */
  483. if (unlikely(tlen < (hdrsize + pad + 4)))
  484. goto drop;
  485. /* Don't count the CRC. */
  486. tlen -= (hdrsize + extra_bytes);
  487. if (unlikely(tlen + qp->r_rcv_len != qp->r_len))
  488. goto drop;
  489. rvt_copy_sge(qp, &qp->r_sge, data, tlen, true, false);
  490. rvt_put_ss(&qp->r_sge);
  491. break;
  492. default:
  493. /* Drop packet for unknown opcodes. */
  494. goto drop;
  495. }
  496. qp->r_psn++;
  497. qp->r_state = opcode;
  498. return;
  499. rewind:
  500. set_bit(RVT_R_REWIND_SGE, &qp->r_aflags);
  501. qp->r_sge.num_sge = 0;
  502. drop:
  503. ibp->rvp.n_pkt_drops++;
  504. return;
  505. op_err:
  506. rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
  507. }