ipath_uc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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_kernel.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 (!(qp->s_flags & IPATH_S_SIGNAL_REQ_WR) ||
  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 = &qp->ibqp;
  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. /* Validate the SLID. See Ch. 9.6.1.5 */
  235. if (unlikely(be16_to_cpu(hdr->lrh[3]) != qp->remote_ah_attr.dlid))
  236. goto done;
  237. /* Check for GRH */
  238. if (!has_grh) {
  239. ohdr = &hdr->u.oth;
  240. hdrsize = 8 + 12; /* LRH + BTH */
  241. psn = be32_to_cpu(ohdr->bth[2]);
  242. header_in_data = 0;
  243. } else {
  244. ohdr = &hdr->u.l.oth;
  245. hdrsize = 8 + 40 + 12; /* LRH + GRH + BTH */
  246. /*
  247. * The header with GRH is 60 bytes and the
  248. * core driver sets the eager header buffer
  249. * size to 56 bytes so the last 4 bytes of
  250. * the BTH header (PSN) is in the data buffer.
  251. */
  252. header_in_data = dev->dd->ipath_rcvhdrentsize == 16;
  253. if (header_in_data) {
  254. psn = be32_to_cpu(((__be32 *) data)[0]);
  255. data += sizeof(__be32);
  256. } else
  257. psn = be32_to_cpu(ohdr->bth[2]);
  258. }
  259. /*
  260. * The opcode is in the low byte when its in network order
  261. * (top byte when in host order).
  262. */
  263. opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
  264. wc.imm_data = 0;
  265. wc.wc_flags = 0;
  266. /* Compare the PSN verses the expected PSN. */
  267. if (unlikely(ipath_cmp24(psn, qp->r_psn) != 0)) {
  268. /*
  269. * Handle a sequence error.
  270. * Silently drop any current message.
  271. */
  272. qp->r_psn = psn;
  273. inv:
  274. qp->r_state = OP(SEND_LAST);
  275. switch (opcode) {
  276. case OP(SEND_FIRST):
  277. case OP(SEND_ONLY):
  278. case OP(SEND_ONLY_WITH_IMMEDIATE):
  279. goto send_first;
  280. case OP(RDMA_WRITE_FIRST):
  281. case OP(RDMA_WRITE_ONLY):
  282. case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
  283. goto rdma_first;
  284. default:
  285. dev->n_pkt_drops++;
  286. goto done;
  287. }
  288. }
  289. /* Check for opcode sequence errors. */
  290. switch (qp->r_state) {
  291. case OP(SEND_FIRST):
  292. case OP(SEND_MIDDLE):
  293. if (opcode == OP(SEND_MIDDLE) ||
  294. opcode == OP(SEND_LAST) ||
  295. opcode == OP(SEND_LAST_WITH_IMMEDIATE))
  296. break;
  297. goto inv;
  298. case OP(RDMA_WRITE_FIRST):
  299. case OP(RDMA_WRITE_MIDDLE):
  300. if (opcode == OP(RDMA_WRITE_MIDDLE) ||
  301. opcode == OP(RDMA_WRITE_LAST) ||
  302. opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
  303. break;
  304. goto inv;
  305. default:
  306. if (opcode == OP(SEND_FIRST) ||
  307. opcode == OP(SEND_ONLY) ||
  308. opcode == OP(SEND_ONLY_WITH_IMMEDIATE) ||
  309. opcode == OP(RDMA_WRITE_FIRST) ||
  310. opcode == OP(RDMA_WRITE_ONLY) ||
  311. opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
  312. break;
  313. goto inv;
  314. }
  315. /* OK, process the packet. */
  316. switch (opcode) {
  317. case OP(SEND_FIRST):
  318. case OP(SEND_ONLY):
  319. case OP(SEND_ONLY_WITH_IMMEDIATE):
  320. send_first:
  321. if (qp->r_reuse_sge) {
  322. qp->r_reuse_sge = 0;
  323. qp->r_sge = qp->s_rdma_read_sge;
  324. } else if (!ipath_get_rwqe(qp, 0)) {
  325. dev->n_pkt_drops++;
  326. goto done;
  327. }
  328. /* Save the WQE so we can reuse it in case of an error. */
  329. qp->s_rdma_read_sge = qp->r_sge;
  330. qp->r_rcv_len = 0;
  331. if (opcode == OP(SEND_ONLY))
  332. goto send_last;
  333. else if (opcode == OP(SEND_ONLY_WITH_IMMEDIATE))
  334. goto send_last_imm;
  335. /* FALLTHROUGH */
  336. case OP(SEND_MIDDLE):
  337. /* Check for invalid length PMTU or posted rwqe len. */
  338. if (unlikely(tlen != (hdrsize + pmtu + 4))) {
  339. qp->r_reuse_sge = 1;
  340. dev->n_pkt_drops++;
  341. goto done;
  342. }
  343. qp->r_rcv_len += pmtu;
  344. if (unlikely(qp->r_rcv_len > qp->r_len)) {
  345. qp->r_reuse_sge = 1;
  346. dev->n_pkt_drops++;
  347. goto done;
  348. }
  349. ipath_copy_sge(&qp->r_sge, data, pmtu);
  350. break;
  351. case OP(SEND_LAST_WITH_IMMEDIATE):
  352. send_last_imm:
  353. if (header_in_data) {
  354. wc.imm_data = *(__be32 *) data;
  355. data += sizeof(__be32);
  356. } else {
  357. /* Immediate data comes after BTH */
  358. wc.imm_data = ohdr->u.imm_data;
  359. }
  360. hdrsize += 4;
  361. wc.wc_flags = IB_WC_WITH_IMM;
  362. /* FALLTHROUGH */
  363. case OP(SEND_LAST):
  364. send_last:
  365. /* Get the number of bytes the message was padded by. */
  366. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  367. /* Check for invalid length. */
  368. /* XXX LAST len should be >= 1 */
  369. if (unlikely(tlen < (hdrsize + pad + 4))) {
  370. qp->r_reuse_sge = 1;
  371. dev->n_pkt_drops++;
  372. goto done;
  373. }
  374. /* Don't count the CRC. */
  375. tlen -= (hdrsize + pad + 4);
  376. wc.byte_len = tlen + qp->r_rcv_len;
  377. if (unlikely(wc.byte_len > qp->r_len)) {
  378. qp->r_reuse_sge = 1;
  379. dev->n_pkt_drops++;
  380. goto done;
  381. }
  382. /* XXX Need to free SGEs */
  383. last_imm:
  384. ipath_copy_sge(&qp->r_sge, data, tlen);
  385. wc.wr_id = qp->r_wr_id;
  386. wc.status = IB_WC_SUCCESS;
  387. wc.opcode = IB_WC_RECV;
  388. wc.vendor_err = 0;
  389. wc.qp = &qp->ibqp;
  390. wc.src_qp = qp->remote_qpn;
  391. wc.pkey_index = 0;
  392. wc.slid = qp->remote_ah_attr.dlid;
  393. wc.sl = qp->remote_ah_attr.sl;
  394. wc.dlid_path_bits = 0;
  395. wc.port_num = 0;
  396. /* Signal completion event if the solicited bit is set. */
  397. ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
  398. (ohdr->bth[0] &
  399. __constant_cpu_to_be32(1 << 23)) != 0);
  400. break;
  401. case OP(RDMA_WRITE_FIRST):
  402. case OP(RDMA_WRITE_ONLY):
  403. case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE): /* consume RWQE */
  404. rdma_first:
  405. /* RETH comes after BTH */
  406. if (!header_in_data)
  407. reth = &ohdr->u.rc.reth;
  408. else {
  409. reth = (struct ib_reth *)data;
  410. data += sizeof(*reth);
  411. }
  412. hdrsize += sizeof(*reth);
  413. qp->r_len = be32_to_cpu(reth->length);
  414. qp->r_rcv_len = 0;
  415. if (qp->r_len != 0) {
  416. u32 rkey = be32_to_cpu(reth->rkey);
  417. u64 vaddr = be64_to_cpu(reth->vaddr);
  418. int ok;
  419. /* Check rkey */
  420. ok = ipath_rkey_ok(qp, &qp->r_sge, qp->r_len,
  421. vaddr, rkey,
  422. IB_ACCESS_REMOTE_WRITE);
  423. if (unlikely(!ok)) {
  424. dev->n_pkt_drops++;
  425. goto done;
  426. }
  427. } else {
  428. qp->r_sge.sg_list = NULL;
  429. qp->r_sge.sge.mr = NULL;
  430. qp->r_sge.sge.vaddr = NULL;
  431. qp->r_sge.sge.length = 0;
  432. qp->r_sge.sge.sge_length = 0;
  433. }
  434. if (unlikely(!(qp->qp_access_flags &
  435. IB_ACCESS_REMOTE_WRITE))) {
  436. dev->n_pkt_drops++;
  437. goto done;
  438. }
  439. if (opcode == OP(RDMA_WRITE_ONLY))
  440. goto rdma_last;
  441. else if (opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
  442. goto rdma_last_imm;
  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. dev->n_pkt_drops++;
  448. goto done;
  449. }
  450. qp->r_rcv_len += pmtu;
  451. if (unlikely(qp->r_rcv_len > qp->r_len)) {
  452. dev->n_pkt_drops++;
  453. goto done;
  454. }
  455. ipath_copy_sge(&qp->r_sge, data, pmtu);
  456. break;
  457. case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
  458. rdma_last_imm:
  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. dev->n_pkt_drops++;
  465. goto done;
  466. }
  467. /* Don't count the CRC. */
  468. tlen -= (hdrsize + pad + 4);
  469. if (unlikely(tlen + qp->r_rcv_len != qp->r_len)) {
  470. dev->n_pkt_drops++;
  471. goto done;
  472. }
  473. if (qp->r_reuse_sge)
  474. qp->r_reuse_sge = 0;
  475. else if (!ipath_get_rwqe(qp, 1)) {
  476. dev->n_pkt_drops++;
  477. goto done;
  478. }
  479. if (header_in_data) {
  480. wc.imm_data = *(__be32 *) data;
  481. data += sizeof(__be32);
  482. } else {
  483. /* Immediate data comes after BTH */
  484. wc.imm_data = ohdr->u.imm_data;
  485. }
  486. hdrsize += 4;
  487. wc.wc_flags = IB_WC_WITH_IMM;
  488. wc.byte_len = 0;
  489. goto last_imm;
  490. case OP(RDMA_WRITE_LAST):
  491. rdma_last:
  492. /* Get the number of bytes the message was padded by. */
  493. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  494. /* Check for invalid length. */
  495. /* XXX LAST len should be >= 1 */
  496. if (unlikely(tlen < (hdrsize + pad + 4))) {
  497. dev->n_pkt_drops++;
  498. goto done;
  499. }
  500. /* Don't count the CRC. */
  501. tlen -= (hdrsize + pad + 4);
  502. if (unlikely(tlen + qp->r_rcv_len != qp->r_len)) {
  503. dev->n_pkt_drops++;
  504. goto done;
  505. }
  506. ipath_copy_sge(&qp->r_sge, data, tlen);
  507. break;
  508. default:
  509. /* Drop packet for unknown opcodes. */
  510. dev->n_pkt_drops++;
  511. goto done;
  512. }
  513. qp->r_psn++;
  514. qp->r_state = opcode;
  515. done:
  516. return;
  517. }