qib_uc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. smp_read_barrier_depends(); /* see post_one_send() */
  60. if (qp->s_last == ACCESS_ONCE(qp->s_head))
  61. goto bail;
  62. /* If DMAs are in progress, we can't flush immediately. */
  63. if (atomic_read(&priv->s_dma_busy)) {
  64. qp->s_flags |= RVT_S_WAIT_DMA;
  65. goto bail;
  66. }
  67. wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  68. qib_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
  69. goto done;
  70. }
  71. ohdr = &priv->s_hdr->u.oth;
  72. if (rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)
  73. ohdr = &priv->s_hdr->u.l.oth;
  74. /* header size in 32-bit words LRH+BTH = (8+12)/4. */
  75. hwords = 5;
  76. bth0 = 0;
  77. /* Get the next send request. */
  78. wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
  79. qp->s_wqe = NULL;
  80. switch (qp->s_state) {
  81. default:
  82. if (!(ib_rvt_state_ops[qp->state] &
  83. RVT_PROCESS_NEXT_SEND_OK))
  84. goto bail;
  85. /* Check if send work queue is empty. */
  86. smp_read_barrier_depends(); /* see post_one_send() */
  87. if (qp->s_cur == ACCESS_ONCE(qp->s_head))
  88. goto bail;
  89. /*
  90. * Start a new request.
  91. */
  92. qp->s_psn = wqe->psn;
  93. qp->s_sge.sge = wqe->sg_list[0];
  94. qp->s_sge.sg_list = wqe->sg_list + 1;
  95. qp->s_sge.num_sge = wqe->wr.num_sge;
  96. qp->s_sge.total_len = wqe->length;
  97. len = wqe->length;
  98. qp->s_len = len;
  99. switch (wqe->wr.opcode) {
  100. case IB_WR_SEND:
  101. case IB_WR_SEND_WITH_IMM:
  102. if (len > pmtu) {
  103. qp->s_state = OP(SEND_FIRST);
  104. len = pmtu;
  105. break;
  106. }
  107. if (wqe->wr.opcode == IB_WR_SEND)
  108. qp->s_state = OP(SEND_ONLY);
  109. else {
  110. qp->s_state =
  111. OP(SEND_ONLY_WITH_IMMEDIATE);
  112. /* Immediate data comes after the BTH */
  113. ohdr->u.imm_data = wqe->wr.ex.imm_data;
  114. hwords += 1;
  115. }
  116. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  117. bth0 |= IB_BTH_SOLICITED;
  118. qp->s_wqe = wqe;
  119. if (++qp->s_cur >= qp->s_size)
  120. qp->s_cur = 0;
  121. break;
  122. case IB_WR_RDMA_WRITE:
  123. case IB_WR_RDMA_WRITE_WITH_IMM:
  124. ohdr->u.rc.reth.vaddr =
  125. cpu_to_be64(wqe->rdma_wr.remote_addr);
  126. ohdr->u.rc.reth.rkey =
  127. cpu_to_be32(wqe->rdma_wr.rkey);
  128. ohdr->u.rc.reth.length = cpu_to_be32(len);
  129. hwords += sizeof(struct ib_reth) / 4;
  130. if (len > pmtu) {
  131. qp->s_state = OP(RDMA_WRITE_FIRST);
  132. len = pmtu;
  133. break;
  134. }
  135. if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
  136. qp->s_state = OP(RDMA_WRITE_ONLY);
  137. else {
  138. qp->s_state =
  139. OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
  140. /* Immediate data comes after the RETH */
  141. ohdr->u.rc.imm_data = wqe->wr.ex.imm_data;
  142. hwords += 1;
  143. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  144. bth0 |= IB_BTH_SOLICITED;
  145. }
  146. qp->s_wqe = wqe;
  147. if (++qp->s_cur >= qp->s_size)
  148. qp->s_cur = 0;
  149. break;
  150. default:
  151. goto bail;
  152. }
  153. break;
  154. case OP(SEND_FIRST):
  155. qp->s_state = OP(SEND_MIDDLE);
  156. /* FALLTHROUGH */
  157. case OP(SEND_MIDDLE):
  158. len = qp->s_len;
  159. if (len > pmtu) {
  160. len = pmtu;
  161. break;
  162. }
  163. if (wqe->wr.opcode == IB_WR_SEND)
  164. qp->s_state = OP(SEND_LAST);
  165. else {
  166. qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
  167. /* Immediate data comes after the BTH */
  168. ohdr->u.imm_data = wqe->wr.ex.imm_data;
  169. hwords += 1;
  170. }
  171. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  172. bth0 |= IB_BTH_SOLICITED;
  173. qp->s_wqe = wqe;
  174. if (++qp->s_cur >= qp->s_size)
  175. qp->s_cur = 0;
  176. break;
  177. case OP(RDMA_WRITE_FIRST):
  178. qp->s_state = OP(RDMA_WRITE_MIDDLE);
  179. /* FALLTHROUGH */
  180. case OP(RDMA_WRITE_MIDDLE):
  181. len = qp->s_len;
  182. if (len > pmtu) {
  183. len = pmtu;
  184. break;
  185. }
  186. if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
  187. qp->s_state = OP(RDMA_WRITE_LAST);
  188. else {
  189. qp->s_state =
  190. OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
  191. /* Immediate data comes after the BTH */
  192. ohdr->u.imm_data = wqe->wr.ex.imm_data;
  193. hwords += 1;
  194. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  195. bth0 |= IB_BTH_SOLICITED;
  196. }
  197. qp->s_wqe = wqe;
  198. if (++qp->s_cur >= qp->s_size)
  199. qp->s_cur = 0;
  200. break;
  201. }
  202. qp->s_len -= len;
  203. qp->s_hdrwords = hwords;
  204. qp->s_cur_sge = &qp->s_sge;
  205. qp->s_cur_size = len;
  206. qib_make_ruc_header(qp, ohdr, bth0 | (qp->s_state << 24),
  207. qp->s_psn++ & QIB_PSN_MASK);
  208. done:
  209. return 1;
  210. bail:
  211. qp->s_flags &= ~RVT_S_BUSY;
  212. return ret;
  213. }
  214. /**
  215. * qib_uc_rcv - handle an incoming UC packet
  216. * @ibp: the port the packet came in on
  217. * @hdr: the header of the packet
  218. * @has_grh: true if the packet has a GRH
  219. * @data: the packet data
  220. * @tlen: the length of the packet
  221. * @qp: the QP for this packet.
  222. *
  223. * This is called from qib_qp_rcv() to process an incoming UC packet
  224. * for the given QP.
  225. * Called at interrupt level.
  226. */
  227. void qib_uc_rcv(struct qib_ibport *ibp, struct ib_header *hdr,
  228. int has_grh, void *data, u32 tlen, struct rvt_qp *qp)
  229. {
  230. struct ib_other_headers *ohdr;
  231. u32 opcode;
  232. u32 hdrsize;
  233. u32 psn;
  234. u32 pad;
  235. struct ib_wc wc;
  236. u32 pmtu = qp->pmtu;
  237. struct ib_reth *reth;
  238. int ret;
  239. /* Check for GRH */
  240. if (!has_grh) {
  241. ohdr = &hdr->u.oth;
  242. hdrsize = 8 + 12; /* LRH + BTH */
  243. } else {
  244. ohdr = &hdr->u.l.oth;
  245. hdrsize = 8 + 40 + 12; /* LRH + GRH + BTH */
  246. }
  247. opcode = be32_to_cpu(ohdr->bth[0]);
  248. if (qib_ruc_check_hdr(ibp, hdr, has_grh, qp, opcode))
  249. return;
  250. psn = be32_to_cpu(ohdr->bth[2]);
  251. opcode >>= 24;
  252. /* Compare the PSN verses the expected PSN. */
  253. if (unlikely(qib_cmp24(psn, qp->r_psn) != 0)) {
  254. /*
  255. * Handle a sequence error.
  256. * Silently drop any current message.
  257. */
  258. qp->r_psn = psn;
  259. inv:
  260. if (qp->r_state == OP(SEND_FIRST) ||
  261. qp->r_state == OP(SEND_MIDDLE)) {
  262. set_bit(RVT_R_REWIND_SGE, &qp->r_aflags);
  263. qp->r_sge.num_sge = 0;
  264. } else
  265. rvt_put_ss(&qp->r_sge);
  266. qp->r_state = OP(SEND_LAST);
  267. switch (opcode) {
  268. case OP(SEND_FIRST):
  269. case OP(SEND_ONLY):
  270. case OP(SEND_ONLY_WITH_IMMEDIATE):
  271. goto send_first;
  272. case OP(RDMA_WRITE_FIRST):
  273. case OP(RDMA_WRITE_ONLY):
  274. case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
  275. goto rdma_first;
  276. default:
  277. goto drop;
  278. }
  279. }
  280. /* Check for opcode sequence errors. */
  281. switch (qp->r_state) {
  282. case OP(SEND_FIRST):
  283. case OP(SEND_MIDDLE):
  284. if (opcode == OP(SEND_MIDDLE) ||
  285. opcode == OP(SEND_LAST) ||
  286. opcode == OP(SEND_LAST_WITH_IMMEDIATE))
  287. break;
  288. goto inv;
  289. case OP(RDMA_WRITE_FIRST):
  290. case OP(RDMA_WRITE_MIDDLE):
  291. if (opcode == OP(RDMA_WRITE_MIDDLE) ||
  292. opcode == OP(RDMA_WRITE_LAST) ||
  293. opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
  294. break;
  295. goto inv;
  296. default:
  297. if (opcode == OP(SEND_FIRST) ||
  298. opcode == OP(SEND_ONLY) ||
  299. opcode == OP(SEND_ONLY_WITH_IMMEDIATE) ||
  300. opcode == OP(RDMA_WRITE_FIRST) ||
  301. opcode == OP(RDMA_WRITE_ONLY) ||
  302. opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
  303. break;
  304. goto inv;
  305. }
  306. if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
  307. rvt_comm_est(qp);
  308. /* OK, process the packet. */
  309. switch (opcode) {
  310. case OP(SEND_FIRST):
  311. case OP(SEND_ONLY):
  312. case OP(SEND_ONLY_WITH_IMMEDIATE):
  313. send_first:
  314. if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
  315. qp->r_sge = qp->s_rdma_read_sge;
  316. else {
  317. ret = qib_get_rwqe(qp, 0);
  318. if (ret < 0)
  319. goto op_err;
  320. if (!ret)
  321. goto drop;
  322. /*
  323. * qp->s_rdma_read_sge will be the owner
  324. * of the mr references.
  325. */
  326. qp->s_rdma_read_sge = qp->r_sge;
  327. }
  328. qp->r_rcv_len = 0;
  329. if (opcode == OP(SEND_ONLY))
  330. goto no_immediate_data;
  331. else if (opcode == OP(SEND_ONLY_WITH_IMMEDIATE))
  332. goto send_last_imm;
  333. /* FALLTHROUGH */
  334. case OP(SEND_MIDDLE):
  335. /* Check for invalid length PMTU or posted rwqe len. */
  336. if (unlikely(tlen != (hdrsize + pmtu + 4)))
  337. goto rewind;
  338. qp->r_rcv_len += pmtu;
  339. if (unlikely(qp->r_rcv_len > qp->r_len))
  340. goto rewind;
  341. qib_copy_sge(&qp->r_sge, data, pmtu, 0);
  342. break;
  343. case OP(SEND_LAST_WITH_IMMEDIATE):
  344. send_last_imm:
  345. wc.ex.imm_data = ohdr->u.imm_data;
  346. hdrsize += 4;
  347. wc.wc_flags = IB_WC_WITH_IMM;
  348. goto send_last;
  349. case OP(SEND_LAST):
  350. no_immediate_data:
  351. wc.ex.imm_data = 0;
  352. wc.wc_flags = 0;
  353. send_last:
  354. /* Get the number of bytes the message was padded by. */
  355. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  356. /* Check for invalid length. */
  357. /* XXX LAST len should be >= 1 */
  358. if (unlikely(tlen < (hdrsize + pad + 4)))
  359. goto rewind;
  360. /* Don't count the CRC. */
  361. tlen -= (hdrsize + pad + 4);
  362. wc.byte_len = tlen + qp->r_rcv_len;
  363. if (unlikely(wc.byte_len > qp->r_len))
  364. goto rewind;
  365. wc.opcode = IB_WC_RECV;
  366. qib_copy_sge(&qp->r_sge, data, tlen, 0);
  367. rvt_put_ss(&qp->s_rdma_read_sge);
  368. last_imm:
  369. wc.wr_id = qp->r_wr_id;
  370. wc.status = IB_WC_SUCCESS;
  371. wc.qp = &qp->ibqp;
  372. wc.src_qp = qp->remote_qpn;
  373. wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr);
  374. wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
  375. /* zero fields that are N/A */
  376. wc.vendor_err = 0;
  377. wc.pkey_index = 0;
  378. wc.dlid_path_bits = 0;
  379. wc.port_num = 0;
  380. /* Signal completion event if the solicited bit is set. */
  381. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
  382. (ohdr->bth[0] &
  383. cpu_to_be32(IB_BTH_SOLICITED)) != 0);
  384. break;
  385. case OP(RDMA_WRITE_FIRST):
  386. case OP(RDMA_WRITE_ONLY):
  387. case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE): /* consume RWQE */
  388. rdma_first:
  389. if (unlikely(!(qp->qp_access_flags &
  390. IB_ACCESS_REMOTE_WRITE))) {
  391. goto drop;
  392. }
  393. reth = &ohdr->u.rc.reth;
  394. hdrsize += sizeof(*reth);
  395. qp->r_len = be32_to_cpu(reth->length);
  396. qp->r_rcv_len = 0;
  397. qp->r_sge.sg_list = NULL;
  398. if (qp->r_len != 0) {
  399. u32 rkey = be32_to_cpu(reth->rkey);
  400. u64 vaddr = be64_to_cpu(reth->vaddr);
  401. int ok;
  402. /* Check rkey */
  403. ok = rvt_rkey_ok(qp, &qp->r_sge.sge, qp->r_len,
  404. vaddr, rkey, IB_ACCESS_REMOTE_WRITE);
  405. if (unlikely(!ok))
  406. goto drop;
  407. qp->r_sge.num_sge = 1;
  408. } else {
  409. qp->r_sge.num_sge = 0;
  410. qp->r_sge.sge.mr = NULL;
  411. qp->r_sge.sge.vaddr = NULL;
  412. qp->r_sge.sge.length = 0;
  413. qp->r_sge.sge.sge_length = 0;
  414. }
  415. if (opcode == OP(RDMA_WRITE_ONLY))
  416. goto rdma_last;
  417. else if (opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE)) {
  418. wc.ex.imm_data = ohdr->u.rc.imm_data;
  419. goto rdma_last_imm;
  420. }
  421. /* FALLTHROUGH */
  422. case OP(RDMA_WRITE_MIDDLE):
  423. /* Check for invalid length PMTU or posted rwqe len. */
  424. if (unlikely(tlen != (hdrsize + pmtu + 4)))
  425. goto drop;
  426. qp->r_rcv_len += pmtu;
  427. if (unlikely(qp->r_rcv_len > qp->r_len))
  428. goto drop;
  429. qib_copy_sge(&qp->r_sge, data, pmtu, 1);
  430. break;
  431. case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
  432. wc.ex.imm_data = ohdr->u.imm_data;
  433. rdma_last_imm:
  434. hdrsize += 4;
  435. wc.wc_flags = IB_WC_WITH_IMM;
  436. /* Get the number of bytes the message was padded by. */
  437. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  438. /* Check for invalid length. */
  439. /* XXX LAST len should be >= 1 */
  440. if (unlikely(tlen < (hdrsize + pad + 4)))
  441. goto drop;
  442. /* Don't count the CRC. */
  443. tlen -= (hdrsize + pad + 4);
  444. if (unlikely(tlen + qp->r_rcv_len != qp->r_len))
  445. goto drop;
  446. if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
  447. rvt_put_ss(&qp->s_rdma_read_sge);
  448. else {
  449. ret = qib_get_rwqe(qp, 1);
  450. if (ret < 0)
  451. goto op_err;
  452. if (!ret)
  453. goto drop;
  454. }
  455. wc.byte_len = qp->r_len;
  456. wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
  457. qib_copy_sge(&qp->r_sge, data, tlen, 1);
  458. rvt_put_ss(&qp->r_sge);
  459. goto last_imm;
  460. case OP(RDMA_WRITE_LAST):
  461. rdma_last:
  462. /* Get the number of bytes the message was padded by. */
  463. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  464. /* Check for invalid length. */
  465. /* XXX LAST len should be >= 1 */
  466. if (unlikely(tlen < (hdrsize + pad + 4)))
  467. goto drop;
  468. /* Don't count the CRC. */
  469. tlen -= (hdrsize + pad + 4);
  470. if (unlikely(tlen + qp->r_rcv_len != qp->r_len))
  471. goto drop;
  472. qib_copy_sge(&qp->r_sge, data, tlen, 1);
  473. rvt_put_ss(&qp->r_sge);
  474. break;
  475. default:
  476. /* Drop packet for unknown opcodes. */
  477. goto drop;
  478. }
  479. qp->r_psn++;
  480. qp->r_state = opcode;
  481. return;
  482. rewind:
  483. set_bit(RVT_R_REWIND_SGE, &qp->r_aflags);
  484. qp->r_sge.num_sge = 0;
  485. drop:
  486. ibp->rvp.n_pkt_drops++;
  487. return;
  488. op_err:
  489. rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
  490. return;
  491. }