ipath_uc.c 14 KB

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