qib_uc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * Copyright (c) 2006, 2007, 2008, 2009, 2010 QLogic Corporation.
  3. * All rights reserved.
  4. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include "qib.h"
  35. /* cut down ridiculously long IB macro names */
  36. #define OP(x) IB_OPCODE_UC_##x
  37. /**
  38. * qib_make_uc_req - construct a request packet (SEND, RDMA write)
  39. * @qp: a pointer to the QP
  40. *
  41. * Assumes the s_lock is held.
  42. *
  43. * Return 1 if constructed; otherwise, return 0.
  44. */
  45. int qib_make_uc_req(struct rvt_qp *qp, unsigned long *flags)
  46. {
  47. struct qib_qp_priv *priv = qp->priv;
  48. struct ib_other_headers *ohdr;
  49. struct rvt_swqe *wqe;
  50. u32 hwords;
  51. u32 bth0;
  52. u32 len;
  53. u32 pmtu = qp->pmtu;
  54. int ret = 0;
  55. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_SEND_OK)) {
  56. if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
  57. goto bail;
  58. /* We are in the error state, flush the work request. */
  59. if (qp->s_last == READ_ONCE(qp->s_head))
  60. goto bail;
  61. /* If DMAs are in progress, we can't flush immediately. */
  62. if (atomic_read(&priv->s_dma_busy)) {
  63. qp->s_flags |= RVT_S_WAIT_DMA;
  64. goto bail;
  65. }
  66. wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  67. qib_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
  68. goto done;
  69. }
  70. ohdr = &priv->s_hdr->u.oth;
  71. if (rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)
  72. ohdr = &priv->s_hdr->u.l.oth;
  73. /* header size in 32-bit words LRH+BTH = (8+12)/4. */
  74. hwords = 5;
  75. bth0 = 0;
  76. /* Get the next send request. */
  77. wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
  78. qp->s_wqe = NULL;
  79. switch (qp->s_state) {
  80. default:
  81. if (!(ib_rvt_state_ops[qp->state] &
  82. RVT_PROCESS_NEXT_SEND_OK))
  83. goto bail;
  84. /* Check if send work queue is empty. */
  85. if (qp->s_cur == READ_ONCE(qp->s_head))
  86. goto bail;
  87. /*
  88. * Start a new request.
  89. */
  90. qp->s_psn = wqe->psn;
  91. qp->s_sge.sge = wqe->sg_list[0];
  92. qp->s_sge.sg_list = wqe->sg_list + 1;
  93. qp->s_sge.num_sge = wqe->wr.num_sge;
  94. qp->s_sge.total_len = wqe->length;
  95. len = wqe->length;
  96. qp->s_len = len;
  97. switch (wqe->wr.opcode) {
  98. case IB_WR_SEND:
  99. case IB_WR_SEND_WITH_IMM:
  100. if (len > pmtu) {
  101. qp->s_state = OP(SEND_FIRST);
  102. len = pmtu;
  103. break;
  104. }
  105. if (wqe->wr.opcode == IB_WR_SEND)
  106. qp->s_state = OP(SEND_ONLY);
  107. else {
  108. qp->s_state =
  109. OP(SEND_ONLY_WITH_IMMEDIATE);
  110. /* Immediate data comes after the BTH */
  111. ohdr->u.imm_data = wqe->wr.ex.imm_data;
  112. hwords += 1;
  113. }
  114. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  115. bth0 |= IB_BTH_SOLICITED;
  116. qp->s_wqe = wqe;
  117. if (++qp->s_cur >= qp->s_size)
  118. qp->s_cur = 0;
  119. break;
  120. case IB_WR_RDMA_WRITE:
  121. case IB_WR_RDMA_WRITE_WITH_IMM:
  122. ohdr->u.rc.reth.vaddr =
  123. cpu_to_be64(wqe->rdma_wr.remote_addr);
  124. ohdr->u.rc.reth.rkey =
  125. cpu_to_be32(wqe->rdma_wr.rkey);
  126. ohdr->u.rc.reth.length = cpu_to_be32(len);
  127. hwords += sizeof(struct ib_reth) / 4;
  128. if (len > pmtu) {
  129. qp->s_state = OP(RDMA_WRITE_FIRST);
  130. len = pmtu;
  131. break;
  132. }
  133. if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
  134. qp->s_state = OP(RDMA_WRITE_ONLY);
  135. else {
  136. qp->s_state =
  137. OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
  138. /* Immediate data comes after the RETH */
  139. ohdr->u.rc.imm_data = wqe->wr.ex.imm_data;
  140. hwords += 1;
  141. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  142. bth0 |= IB_BTH_SOLICITED;
  143. }
  144. qp->s_wqe = wqe;
  145. if (++qp->s_cur >= qp->s_size)
  146. qp->s_cur = 0;
  147. break;
  148. default:
  149. goto bail;
  150. }
  151. break;
  152. case OP(SEND_FIRST):
  153. qp->s_state = OP(SEND_MIDDLE);
  154. /* FALLTHROUGH */
  155. case OP(SEND_MIDDLE):
  156. len = qp->s_len;
  157. if (len > pmtu) {
  158. len = pmtu;
  159. break;
  160. }
  161. if (wqe->wr.opcode == IB_WR_SEND)
  162. qp->s_state = OP(SEND_LAST);
  163. else {
  164. qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
  165. /* Immediate data comes after the BTH */
  166. ohdr->u.imm_data = wqe->wr.ex.imm_data;
  167. hwords += 1;
  168. }
  169. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  170. bth0 |= IB_BTH_SOLICITED;
  171. qp->s_wqe = wqe;
  172. if (++qp->s_cur >= qp->s_size)
  173. qp->s_cur = 0;
  174. break;
  175. case OP(RDMA_WRITE_FIRST):
  176. qp->s_state = OP(RDMA_WRITE_MIDDLE);
  177. /* FALLTHROUGH */
  178. case OP(RDMA_WRITE_MIDDLE):
  179. len = qp->s_len;
  180. if (len > pmtu) {
  181. len = pmtu;
  182. break;
  183. }
  184. if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
  185. qp->s_state = OP(RDMA_WRITE_LAST);
  186. else {
  187. qp->s_state =
  188. OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
  189. /* Immediate data comes after the BTH */
  190. ohdr->u.imm_data = wqe->wr.ex.imm_data;
  191. hwords += 1;
  192. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  193. bth0 |= IB_BTH_SOLICITED;
  194. }
  195. qp->s_wqe = wqe;
  196. if (++qp->s_cur >= qp->s_size)
  197. qp->s_cur = 0;
  198. break;
  199. }
  200. qp->s_len -= len;
  201. qp->s_hdrwords = hwords;
  202. qp->s_cur_sge = &qp->s_sge;
  203. qp->s_cur_size = len;
  204. qib_make_ruc_header(qp, ohdr, bth0 | (qp->s_state << 24),
  205. qp->s_psn++ & QIB_PSN_MASK);
  206. done:
  207. return 1;
  208. bail:
  209. qp->s_flags &= ~RVT_S_BUSY;
  210. return ret;
  211. }
  212. /**
  213. * qib_uc_rcv - handle an incoming UC packet
  214. * @ibp: the port the packet came in on
  215. * @hdr: the header of the packet
  216. * @has_grh: true if the packet has a GRH
  217. * @data: the packet data
  218. * @tlen: the length of the packet
  219. * @qp: the QP for this packet.
  220. *
  221. * This is called from qib_qp_rcv() to process an incoming UC packet
  222. * for the given QP.
  223. * Called at interrupt level.
  224. */
  225. void qib_uc_rcv(struct qib_ibport *ibp, struct ib_header *hdr,
  226. int has_grh, void *data, u32 tlen, struct rvt_qp *qp)
  227. {
  228. struct ib_other_headers *ohdr;
  229. u32 opcode;
  230. u32 hdrsize;
  231. u32 psn;
  232. u32 pad;
  233. struct ib_wc wc;
  234. u32 pmtu = qp->pmtu;
  235. struct ib_reth *reth;
  236. int ret;
  237. /* Check for GRH */
  238. if (!has_grh) {
  239. ohdr = &hdr->u.oth;
  240. hdrsize = 8 + 12; /* LRH + BTH */
  241. } else {
  242. ohdr = &hdr->u.l.oth;
  243. hdrsize = 8 + 40 + 12; /* LRH + GRH + BTH */
  244. }
  245. opcode = be32_to_cpu(ohdr->bth[0]);
  246. if (qib_ruc_check_hdr(ibp, hdr, has_grh, qp, opcode))
  247. return;
  248. psn = be32_to_cpu(ohdr->bth[2]);
  249. opcode >>= 24;
  250. /* Compare the PSN verses the expected PSN. */
  251. if (unlikely(qib_cmp24(psn, qp->r_psn) != 0)) {
  252. /*
  253. * Handle a sequence error.
  254. * Silently drop any current message.
  255. */
  256. qp->r_psn = psn;
  257. inv:
  258. if (qp->r_state == OP(SEND_FIRST) ||
  259. qp->r_state == OP(SEND_MIDDLE)) {
  260. set_bit(RVT_R_REWIND_SGE, &qp->r_aflags);
  261. qp->r_sge.num_sge = 0;
  262. } else
  263. rvt_put_ss(&qp->r_sge);
  264. qp->r_state = OP(SEND_LAST);
  265. switch (opcode) {
  266. case OP(SEND_FIRST):
  267. case OP(SEND_ONLY):
  268. case OP(SEND_ONLY_WITH_IMMEDIATE):
  269. goto send_first;
  270. case OP(RDMA_WRITE_FIRST):
  271. case OP(RDMA_WRITE_ONLY):
  272. case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
  273. goto rdma_first;
  274. default:
  275. goto drop;
  276. }
  277. }
  278. /* Check for opcode sequence errors. */
  279. switch (qp->r_state) {
  280. case OP(SEND_FIRST):
  281. case OP(SEND_MIDDLE):
  282. if (opcode == OP(SEND_MIDDLE) ||
  283. opcode == OP(SEND_LAST) ||
  284. opcode == OP(SEND_LAST_WITH_IMMEDIATE))
  285. break;
  286. goto inv;
  287. case OP(RDMA_WRITE_FIRST):
  288. case OP(RDMA_WRITE_MIDDLE):
  289. if (opcode == OP(RDMA_WRITE_MIDDLE) ||
  290. opcode == OP(RDMA_WRITE_LAST) ||
  291. opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
  292. break;
  293. goto inv;
  294. default:
  295. if (opcode == OP(SEND_FIRST) ||
  296. opcode == OP(SEND_ONLY) ||
  297. opcode == OP(SEND_ONLY_WITH_IMMEDIATE) ||
  298. opcode == OP(RDMA_WRITE_FIRST) ||
  299. opcode == OP(RDMA_WRITE_ONLY) ||
  300. opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
  301. break;
  302. goto inv;
  303. }
  304. if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
  305. rvt_comm_est(qp);
  306. /* OK, process the packet. */
  307. switch (opcode) {
  308. case OP(SEND_FIRST):
  309. case OP(SEND_ONLY):
  310. case OP(SEND_ONLY_WITH_IMMEDIATE):
  311. send_first:
  312. if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
  313. qp->r_sge = qp->s_rdma_read_sge;
  314. else {
  315. ret = rvt_get_rwqe(qp, false);
  316. if (ret < 0)
  317. goto op_err;
  318. if (!ret)
  319. goto drop;
  320. /*
  321. * qp->s_rdma_read_sge will be the owner
  322. * of the mr references.
  323. */
  324. qp->s_rdma_read_sge = qp->r_sge;
  325. }
  326. qp->r_rcv_len = 0;
  327. if (opcode == OP(SEND_ONLY))
  328. goto no_immediate_data;
  329. else if (opcode == OP(SEND_ONLY_WITH_IMMEDIATE))
  330. goto send_last_imm;
  331. /* FALLTHROUGH */
  332. case OP(SEND_MIDDLE):
  333. /* Check for invalid length PMTU or posted rwqe len. */
  334. if (unlikely(tlen != (hdrsize + pmtu + 4)))
  335. goto rewind;
  336. qp->r_rcv_len += pmtu;
  337. if (unlikely(qp->r_rcv_len > qp->r_len))
  338. goto rewind;
  339. qib_copy_sge(&qp->r_sge, data, pmtu, 0);
  340. break;
  341. case OP(SEND_LAST_WITH_IMMEDIATE):
  342. send_last_imm:
  343. wc.ex.imm_data = ohdr->u.imm_data;
  344. hdrsize += 4;
  345. wc.wc_flags = IB_WC_WITH_IMM;
  346. goto send_last;
  347. case OP(SEND_LAST):
  348. no_immediate_data:
  349. wc.ex.imm_data = 0;
  350. wc.wc_flags = 0;
  351. send_last:
  352. /* Get the number of bytes the message was padded by. */
  353. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  354. /* Check for invalid length. */
  355. /* XXX LAST len should be >= 1 */
  356. if (unlikely(tlen < (hdrsize + pad + 4)))
  357. goto rewind;
  358. /* Don't count the CRC. */
  359. tlen -= (hdrsize + pad + 4);
  360. wc.byte_len = tlen + qp->r_rcv_len;
  361. if (unlikely(wc.byte_len > qp->r_len))
  362. goto rewind;
  363. wc.opcode = IB_WC_RECV;
  364. qib_copy_sge(&qp->r_sge, data, tlen, 0);
  365. rvt_put_ss(&qp->s_rdma_read_sge);
  366. last_imm:
  367. wc.wr_id = qp->r_wr_id;
  368. wc.status = IB_WC_SUCCESS;
  369. wc.qp = &qp->ibqp;
  370. wc.src_qp = qp->remote_qpn;
  371. wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr);
  372. wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
  373. /* zero fields that are N/A */
  374. wc.vendor_err = 0;
  375. wc.pkey_index = 0;
  376. wc.dlid_path_bits = 0;
  377. wc.port_num = 0;
  378. /* Signal completion event if the solicited bit is set. */
  379. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
  380. ib_bth_is_solicited(ohdr));
  381. break;
  382. case OP(RDMA_WRITE_FIRST):
  383. case OP(RDMA_WRITE_ONLY):
  384. case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE): /* consume RWQE */
  385. rdma_first:
  386. if (unlikely(!(qp->qp_access_flags &
  387. IB_ACCESS_REMOTE_WRITE))) {
  388. goto drop;
  389. }
  390. reth = &ohdr->u.rc.reth;
  391. hdrsize += sizeof(*reth);
  392. qp->r_len = be32_to_cpu(reth->length);
  393. qp->r_rcv_len = 0;
  394. qp->r_sge.sg_list = NULL;
  395. if (qp->r_len != 0) {
  396. u32 rkey = be32_to_cpu(reth->rkey);
  397. u64 vaddr = be64_to_cpu(reth->vaddr);
  398. int ok;
  399. /* Check rkey */
  400. ok = rvt_rkey_ok(qp, &qp->r_sge.sge, qp->r_len,
  401. vaddr, rkey, IB_ACCESS_REMOTE_WRITE);
  402. if (unlikely(!ok))
  403. goto drop;
  404. qp->r_sge.num_sge = 1;
  405. } else {
  406. qp->r_sge.num_sge = 0;
  407. qp->r_sge.sge.mr = NULL;
  408. qp->r_sge.sge.vaddr = NULL;
  409. qp->r_sge.sge.length = 0;
  410. qp->r_sge.sge.sge_length = 0;
  411. }
  412. if (opcode == OP(RDMA_WRITE_ONLY))
  413. goto rdma_last;
  414. else if (opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE)) {
  415. wc.ex.imm_data = ohdr->u.rc.imm_data;
  416. goto rdma_last_imm;
  417. }
  418. /* FALLTHROUGH */
  419. case OP(RDMA_WRITE_MIDDLE):
  420. /* Check for invalid length PMTU or posted rwqe len. */
  421. if (unlikely(tlen != (hdrsize + pmtu + 4)))
  422. goto drop;
  423. qp->r_rcv_len += pmtu;
  424. if (unlikely(qp->r_rcv_len > qp->r_len))
  425. goto drop;
  426. qib_copy_sge(&qp->r_sge, data, pmtu, 1);
  427. break;
  428. case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
  429. wc.ex.imm_data = ohdr->u.imm_data;
  430. rdma_last_imm:
  431. hdrsize += 4;
  432. wc.wc_flags = IB_WC_WITH_IMM;
  433. /* Get the number of bytes the message was padded by. */
  434. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  435. /* Check for invalid length. */
  436. /* XXX LAST len should be >= 1 */
  437. if (unlikely(tlen < (hdrsize + pad + 4)))
  438. goto drop;
  439. /* Don't count the CRC. */
  440. tlen -= (hdrsize + pad + 4);
  441. if (unlikely(tlen + qp->r_rcv_len != qp->r_len))
  442. goto drop;
  443. if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
  444. rvt_put_ss(&qp->s_rdma_read_sge);
  445. else {
  446. ret = rvt_get_rwqe(qp, true);
  447. if (ret < 0)
  448. goto op_err;
  449. if (!ret)
  450. goto drop;
  451. }
  452. wc.byte_len = qp->r_len;
  453. wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
  454. qib_copy_sge(&qp->r_sge, data, tlen, 1);
  455. rvt_put_ss(&qp->r_sge);
  456. goto last_imm;
  457. case OP(RDMA_WRITE_LAST):
  458. rdma_last:
  459. /* Get the number of bytes the message was padded by. */
  460. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  461. /* Check for invalid length. */
  462. /* XXX LAST len should be >= 1 */
  463. if (unlikely(tlen < (hdrsize + pad + 4)))
  464. goto drop;
  465. /* Don't count the CRC. */
  466. tlen -= (hdrsize + pad + 4);
  467. if (unlikely(tlen + qp->r_rcv_len != qp->r_len))
  468. goto drop;
  469. qib_copy_sge(&qp->r_sge, data, tlen, 1);
  470. rvt_put_ss(&qp->r_sge);
  471. break;
  472. default:
  473. /* Drop packet for unknown opcodes. */
  474. goto drop;
  475. }
  476. qp->r_psn++;
  477. qp->r_state = opcode;
  478. return;
  479. rewind:
  480. set_bit(RVT_R_REWIND_SGE, &qp->r_aflags);
  481. qp->r_sge.num_sge = 0;
  482. drop:
  483. ibp->rvp.n_pkt_drops++;
  484. return;
  485. op_err:
  486. rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
  487. return;
  488. }