ipath_uc.c 14 KB

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