ipath_uc.c 13 KB

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