ipath_uc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * Copyright (c) 2006 QLogic, Inc. All rights reserved.
  3. * Copyright (c) 2005, 2006 PathScale, 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 "ipath_verbs.h"
  34. #include "ipath_common.h"
  35. /* cut down ridiculously long IB macro names */
  36. #define OP(x) IB_OPCODE_UC_##x
  37. static void complete_last_send(struct ipath_qp *qp, struct ipath_swqe *wqe,
  38. struct ib_wc *wc)
  39. {
  40. if (++qp->s_last == qp->s_size)
  41. qp->s_last = 0;
  42. if (!test_bit(IPATH_S_SIGNAL_REQ_WR, &qp->s_flags) ||
  43. (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
  44. wc->wr_id = wqe->wr.wr_id;
  45. wc->status = IB_WC_SUCCESS;
  46. wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  47. wc->vendor_err = 0;
  48. wc->byte_len = wqe->length;
  49. wc->qp_num = qp->ibqp.qp_num;
  50. wc->src_qp = qp->remote_qpn;
  51. wc->pkey_index = 0;
  52. wc->slid = qp->remote_ah_attr.dlid;
  53. wc->sl = qp->remote_ah_attr.sl;
  54. wc->dlid_path_bits = 0;
  55. wc->port_num = 0;
  56. ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 0);
  57. }
  58. wqe = get_swqe_ptr(qp, qp->s_last);
  59. }
  60. /**
  61. * ipath_make_uc_req - construct a request packet (SEND, RDMA write)
  62. * @qp: a pointer to the QP
  63. * @ohdr: a pointer to the IB header being constructed
  64. * @pmtu: the path MTU
  65. * @bth0p: pointer to the BTH opcode word
  66. * @bth2p: pointer to the BTH PSN word
  67. *
  68. * Return 1 if constructed; otherwise, return 0.
  69. * Note the QP s_lock must be held and interrupts disabled.
  70. */
  71. int ipath_make_uc_req(struct ipath_qp *qp,
  72. struct ipath_other_headers *ohdr,
  73. u32 pmtu, u32 *bth0p, u32 *bth2p)
  74. {
  75. struct ipath_swqe *wqe;
  76. u32 hwords;
  77. u32 bth0;
  78. u32 len;
  79. struct ib_wc wc;
  80. if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_SEND_OK))
  81. goto done;
  82. /* header size in 32-bit words LRH+BTH = (8+12)/4. */
  83. hwords = 5;
  84. bth0 = 0;
  85. /* Get the next send request. */
  86. wqe = get_swqe_ptr(qp, qp->s_last);
  87. switch (qp->s_state) {
  88. default:
  89. /*
  90. * Signal the completion of the last send
  91. * (if there is one).
  92. */
  93. if (qp->s_last != qp->s_tail)
  94. complete_last_send(qp, wqe, &wc);
  95. /* Check if send work queue is empty. */
  96. if (qp->s_tail == qp->s_head)
  97. goto done;
  98. /*
  99. * Start a new request.
  100. */
  101. qp->s_psn = wqe->psn = qp->s_next_psn;
  102. qp->s_sge.sge = wqe->sg_list[0];
  103. qp->s_sge.sg_list = wqe->sg_list + 1;
  104. qp->s_sge.num_sge = wqe->wr.num_sge;
  105. qp->s_len = len = wqe->length;
  106. switch (wqe->wr.opcode) {
  107. case IB_WR_SEND:
  108. case IB_WR_SEND_WITH_IMM:
  109. if (len > pmtu) {
  110. qp->s_state = OP(SEND_FIRST);
  111. len = pmtu;
  112. break;
  113. }
  114. if (wqe->wr.opcode == IB_WR_SEND)
  115. qp->s_state = OP(SEND_ONLY);
  116. else {
  117. qp->s_state =
  118. OP(SEND_ONLY_WITH_IMMEDIATE);
  119. /* Immediate data comes after the BTH */
  120. ohdr->u.imm_data = wqe->wr.imm_data;
  121. hwords += 1;
  122. }
  123. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  124. bth0 |= 1 << 23;
  125. break;
  126. case IB_WR_RDMA_WRITE:
  127. case IB_WR_RDMA_WRITE_WITH_IMM:
  128. ohdr->u.rc.reth.vaddr =
  129. cpu_to_be64(wqe->wr.wr.rdma.remote_addr);
  130. ohdr->u.rc.reth.rkey =
  131. cpu_to_be32(wqe->wr.wr.rdma.rkey);
  132. ohdr->u.rc.reth.length = cpu_to_be32(len);
  133. hwords += sizeof(struct ib_reth) / 4;
  134. if (len > pmtu) {
  135. qp->s_state = OP(RDMA_WRITE_FIRST);
  136. len = pmtu;
  137. break;
  138. }
  139. if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
  140. qp->s_state = OP(RDMA_WRITE_ONLY);
  141. else {
  142. qp->s_state =
  143. OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
  144. /* Immediate data comes after the RETH */
  145. ohdr->u.rc.imm_data = wqe->wr.imm_data;
  146. hwords += 1;
  147. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  148. bth0 |= 1 << 23;
  149. }
  150. break;
  151. default:
  152. goto done;
  153. }
  154. if (++qp->s_tail >= qp->s_size)
  155. qp->s_tail = 0;
  156. break;
  157. case OP(SEND_FIRST):
  158. qp->s_state = OP(SEND_MIDDLE);
  159. /* FALLTHROUGH */
  160. case OP(SEND_MIDDLE):
  161. len = qp->s_len;
  162. if (len > pmtu) {
  163. len = pmtu;
  164. break;
  165. }
  166. if (wqe->wr.opcode == IB_WR_SEND)
  167. qp->s_state = OP(SEND_LAST);
  168. else {
  169. qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
  170. /* Immediate data comes after the BTH */
  171. ohdr->u.imm_data = wqe->wr.imm_data;
  172. hwords += 1;
  173. }
  174. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  175. bth0 |= 1 << 23;
  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.imm_data;
  193. hwords += 1;
  194. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  195. bth0 |= 1 << 23;
  196. }
  197. break;
  198. }
  199. qp->s_len -= len;
  200. qp->s_hdrwords = hwords;
  201. qp->s_cur_sge = &qp->s_sge;
  202. qp->s_cur_size = len;
  203. *bth0p = bth0 | (qp->s_state << 24);
  204. *bth2p = qp->s_next_psn++ & IPATH_PSN_MASK;
  205. return 1;
  206. done:
  207. return 0;
  208. }
  209. /**
  210. * ipath_uc_rcv - handle an incoming UC packet
  211. * @dev: the device the packet came in on
  212. * @hdr: the header of the packet
  213. * @has_grh: true if the packet has a GRH
  214. * @data: the packet data
  215. * @tlen: the length of the packet
  216. * @qp: the QP for this packet.
  217. *
  218. * This is called from ipath_qp_rcv() to process an incoming UC packet
  219. * for the given QP.
  220. * Called at interrupt level.
  221. */
  222. void ipath_uc_rcv(struct ipath_ibdev *dev, struct ipath_ib_header *hdr,
  223. int has_grh, void *data, u32 tlen, struct ipath_qp *qp)
  224. {
  225. struct ipath_other_headers *ohdr;
  226. int opcode;
  227. u32 hdrsize;
  228. u32 psn;
  229. u32 pad;
  230. struct ib_wc wc;
  231. u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
  232. struct ib_reth *reth;
  233. int header_in_data;
  234. /* Check for GRH */
  235. if (!has_grh) {
  236. ohdr = &hdr->u.oth;
  237. hdrsize = 8 + 12; /* LRH + BTH */
  238. psn = be32_to_cpu(ohdr->bth[2]);
  239. header_in_data = 0;
  240. } else {
  241. ohdr = &hdr->u.l.oth;
  242. hdrsize = 8 + 40 + 12; /* LRH + GRH + BTH */
  243. /*
  244. * The header with GRH is 60 bytes and the
  245. * core driver sets the eager header buffer
  246. * size to 56 bytes so the last 4 bytes of
  247. * the BTH header (PSN) is in the data buffer.
  248. */
  249. header_in_data =
  250. ipath_layer_get_rcvhdrentsize(dev->dd) == 16;
  251. if (header_in_data) {
  252. psn = be32_to_cpu(((__be32 *) data)[0]);
  253. data += sizeof(__be32);
  254. } else
  255. psn = be32_to_cpu(ohdr->bth[2]);
  256. }
  257. /*
  258. * The opcode is in the low byte when its in network order
  259. * (top byte when in host order).
  260. */
  261. opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
  262. wc.imm_data = 0;
  263. wc.wc_flags = 0;
  264. /* Compare the PSN verses the expected PSN. */
  265. if (unlikely(ipath_cmp24(psn, qp->r_psn) != 0)) {
  266. /*
  267. * Handle a sequence error.
  268. * Silently drop any current message.
  269. */
  270. qp->r_psn = psn;
  271. inv:
  272. qp->r_state = OP(SEND_LAST);
  273. switch (opcode) {
  274. case OP(SEND_FIRST):
  275. case OP(SEND_ONLY):
  276. case OP(SEND_ONLY_WITH_IMMEDIATE):
  277. goto send_first;
  278. case OP(RDMA_WRITE_FIRST):
  279. case OP(RDMA_WRITE_ONLY):
  280. case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
  281. goto rdma_first;
  282. default:
  283. dev->n_pkt_drops++;
  284. goto done;
  285. }
  286. }
  287. /* Check for opcode sequence errors. */
  288. switch (qp->r_state) {
  289. case OP(SEND_FIRST):
  290. case OP(SEND_MIDDLE):
  291. if (opcode == OP(SEND_MIDDLE) ||
  292. opcode == OP(SEND_LAST) ||
  293. opcode == OP(SEND_LAST_WITH_IMMEDIATE))
  294. break;
  295. goto inv;
  296. case OP(RDMA_WRITE_FIRST):
  297. case OP(RDMA_WRITE_MIDDLE):
  298. if (opcode == OP(RDMA_WRITE_MIDDLE) ||
  299. opcode == OP(RDMA_WRITE_LAST) ||
  300. opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
  301. break;
  302. goto inv;
  303. default:
  304. if (opcode == OP(SEND_FIRST) ||
  305. opcode == OP(SEND_ONLY) ||
  306. opcode == OP(SEND_ONLY_WITH_IMMEDIATE) ||
  307. opcode == OP(RDMA_WRITE_FIRST) ||
  308. opcode == OP(RDMA_WRITE_ONLY) ||
  309. opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
  310. break;
  311. goto inv;
  312. }
  313. /* OK, process the packet. */
  314. switch (opcode) {
  315. case OP(SEND_FIRST):
  316. case OP(SEND_ONLY):
  317. case OP(SEND_ONLY_WITH_IMMEDIATE):
  318. send_first:
  319. if (qp->r_reuse_sge) {
  320. qp->r_reuse_sge = 0;
  321. qp->r_sge = qp->s_rdma_sge;
  322. } else if (!ipath_get_rwqe(qp, 0)) {
  323. dev->n_pkt_drops++;
  324. goto done;
  325. }
  326. /* Save the WQE so we can reuse it in case of an error. */
  327. qp->s_rdma_sge = qp->r_sge;
  328. qp->r_rcv_len = 0;
  329. if (opcode == OP(SEND_ONLY))
  330. goto send_last;
  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. qp->r_reuse_sge = 1;
  338. dev->n_pkt_drops++;
  339. goto done;
  340. }
  341. qp->r_rcv_len += pmtu;
  342. if (unlikely(qp->r_rcv_len > qp->r_len)) {
  343. qp->r_reuse_sge = 1;
  344. dev->n_pkt_drops++;
  345. goto done;
  346. }
  347. ipath_copy_sge(&qp->r_sge, data, pmtu);
  348. break;
  349. case OP(SEND_LAST_WITH_IMMEDIATE):
  350. send_last_imm:
  351. if (header_in_data) {
  352. wc.imm_data = *(__be32 *) data;
  353. data += sizeof(__be32);
  354. } else {
  355. /* Immediate data comes after BTH */
  356. wc.imm_data = ohdr->u.imm_data;
  357. }
  358. hdrsize += 4;
  359. wc.wc_flags = IB_WC_WITH_IMM;
  360. /* FALLTHROUGH */
  361. case OP(SEND_LAST):
  362. send_last:
  363. /* Get the number of bytes the message was padded by. */
  364. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  365. /* Check for invalid length. */
  366. /* XXX LAST len should be >= 1 */
  367. if (unlikely(tlen < (hdrsize + pad + 4))) {
  368. qp->r_reuse_sge = 1;
  369. dev->n_pkt_drops++;
  370. goto done;
  371. }
  372. /* Don't count the CRC. */
  373. tlen -= (hdrsize + pad + 4);
  374. wc.byte_len = tlen + qp->r_rcv_len;
  375. if (unlikely(wc.byte_len > qp->r_len)) {
  376. qp->r_reuse_sge = 1;
  377. dev->n_pkt_drops++;
  378. goto done;
  379. }
  380. /* XXX Need to free SGEs */
  381. last_imm:
  382. ipath_copy_sge(&qp->r_sge, data, tlen);
  383. wc.wr_id = qp->r_wr_id;
  384. wc.status = IB_WC_SUCCESS;
  385. wc.opcode = IB_WC_RECV;
  386. wc.vendor_err = 0;
  387. wc.qp_num = qp->ibqp.qp_num;
  388. wc.src_qp = qp->remote_qpn;
  389. wc.pkey_index = 0;
  390. wc.slid = qp->remote_ah_attr.dlid;
  391. wc.sl = qp->remote_ah_attr.sl;
  392. wc.dlid_path_bits = 0;
  393. wc.port_num = 0;
  394. /* Signal completion event if the solicited bit is set. */
  395. ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
  396. (ohdr->bth[0] &
  397. __constant_cpu_to_be32(1 << 23)) != 0);
  398. break;
  399. case OP(RDMA_WRITE_FIRST):
  400. case OP(RDMA_WRITE_ONLY):
  401. case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE): /* consume RWQE */
  402. rdma_first:
  403. /* RETH comes after BTH */
  404. if (!header_in_data)
  405. reth = &ohdr->u.rc.reth;
  406. else {
  407. reth = (struct ib_reth *)data;
  408. data += sizeof(*reth);
  409. }
  410. hdrsize += sizeof(*reth);
  411. qp->r_len = be32_to_cpu(reth->length);
  412. qp->r_rcv_len = 0;
  413. if (qp->r_len != 0) {
  414. u32 rkey = be32_to_cpu(reth->rkey);
  415. u64 vaddr = be64_to_cpu(reth->vaddr);
  416. int ok;
  417. /* Check rkey */
  418. ok = ipath_rkey_ok(dev, &qp->r_sge, qp->r_len,
  419. vaddr, rkey,
  420. IB_ACCESS_REMOTE_WRITE);
  421. if (unlikely(!ok)) {
  422. dev->n_pkt_drops++;
  423. goto done;
  424. }
  425. } else {
  426. qp->r_sge.sg_list = NULL;
  427. qp->r_sge.sge.mr = NULL;
  428. qp->r_sge.sge.vaddr = NULL;
  429. qp->r_sge.sge.length = 0;
  430. qp->r_sge.sge.sge_length = 0;
  431. }
  432. if (unlikely(!(qp->qp_access_flags &
  433. IB_ACCESS_REMOTE_WRITE))) {
  434. dev->n_pkt_drops++;
  435. goto done;
  436. }
  437. if (opcode == OP(RDMA_WRITE_ONLY))
  438. goto rdma_last;
  439. else if (opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
  440. goto rdma_last_imm;
  441. /* FALLTHROUGH */
  442. case OP(RDMA_WRITE_MIDDLE):
  443. /* Check for invalid length PMTU or posted rwqe len. */
  444. if (unlikely(tlen != (hdrsize + pmtu + 4))) {
  445. dev->n_pkt_drops++;
  446. goto done;
  447. }
  448. qp->r_rcv_len += pmtu;
  449. if (unlikely(qp->r_rcv_len > qp->r_len)) {
  450. dev->n_pkt_drops++;
  451. goto done;
  452. }
  453. ipath_copy_sge(&qp->r_sge, data, pmtu);
  454. break;
  455. case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
  456. rdma_last_imm:
  457. /* Get the number of bytes the message was padded by. */
  458. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  459. /* Check for invalid length. */
  460. /* XXX LAST len should be >= 1 */
  461. if (unlikely(tlen < (hdrsize + pad + 4))) {
  462. dev->n_pkt_drops++;
  463. goto done;
  464. }
  465. /* Don't count the CRC. */
  466. tlen -= (hdrsize + pad + 4);
  467. if (unlikely(tlen + qp->r_rcv_len != qp->r_len)) {
  468. dev->n_pkt_drops++;
  469. goto done;
  470. }
  471. if (qp->r_reuse_sge)
  472. qp->r_reuse_sge = 0;
  473. else if (!ipath_get_rwqe(qp, 1)) {
  474. dev->n_pkt_drops++;
  475. goto done;
  476. }
  477. if (header_in_data) {
  478. wc.imm_data = *(__be32 *) data;
  479. data += sizeof(__be32);
  480. } else {
  481. /* Immediate data comes after BTH */
  482. wc.imm_data = ohdr->u.imm_data;
  483. }
  484. hdrsize += 4;
  485. wc.wc_flags = IB_WC_WITH_IMM;
  486. wc.byte_len = 0;
  487. goto last_imm;
  488. case OP(RDMA_WRITE_LAST):
  489. rdma_last:
  490. /* Get the number of bytes the message was padded by. */
  491. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  492. /* Check for invalid length. */
  493. /* XXX LAST len should be >= 1 */
  494. if (unlikely(tlen < (hdrsize + pad + 4))) {
  495. dev->n_pkt_drops++;
  496. goto done;
  497. }
  498. /* Don't count the CRC. */
  499. tlen -= (hdrsize + pad + 4);
  500. if (unlikely(tlen + qp->r_rcv_len != qp->r_len)) {
  501. dev->n_pkt_drops++;
  502. goto done;
  503. }
  504. ipath_copy_sge(&qp->r_sge, data, tlen);
  505. break;
  506. default:
  507. /* Drop packet for unknown opcodes. */
  508. dev->n_pkt_drops++;
  509. goto done;
  510. }
  511. qp->r_psn++;
  512. qp->r_state = opcode;
  513. done:
  514. return;
  515. }