qib_ud.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * Copyright (c) 2006, 2007, 2008, 2009 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 <rdma/ib_smi.h>
  34. #include "qib.h"
  35. #include "qib_mad.h"
  36. /**
  37. * qib_ud_loopback - handle send on loopback QPs
  38. * @sqp: the sending QP
  39. * @swqe: the send work request
  40. *
  41. * This is called from qib_make_ud_req() to forward a WQE addressed
  42. * to the same HCA.
  43. * Note that the receive interrupt handler may be calling qib_ud_rcv()
  44. * while this is being called.
  45. */
  46. static void qib_ud_loopback(struct qib_qp *sqp, struct qib_swqe *swqe)
  47. {
  48. struct qib_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
  49. struct qib_pportdata *ppd;
  50. struct qib_qp *qp;
  51. struct ib_ah_attr *ah_attr;
  52. unsigned long flags;
  53. struct qib_sge_state ssge;
  54. struct qib_sge *sge;
  55. struct ib_wc wc;
  56. u32 length;
  57. qp = qib_lookup_qpn(ibp, swqe->wr.wr.ud.remote_qpn);
  58. if (!qp) {
  59. ibp->n_pkt_drops++;
  60. return;
  61. }
  62. if (qp->ibqp.qp_type != sqp->ibqp.qp_type ||
  63. !(ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK)) {
  64. ibp->n_pkt_drops++;
  65. goto drop;
  66. }
  67. ah_attr = &to_iah(swqe->wr.wr.ud.ah)->attr;
  68. ppd = ppd_from_ibp(ibp);
  69. if (qp->ibqp.qp_num > 1) {
  70. u16 pkey1;
  71. u16 pkey2;
  72. u16 lid;
  73. pkey1 = qib_get_pkey(ibp, sqp->s_pkey_index);
  74. pkey2 = qib_get_pkey(ibp, qp->s_pkey_index);
  75. if (unlikely(!qib_pkey_ok(pkey1, pkey2))) {
  76. lid = ppd->lid | (ah_attr->src_path_bits &
  77. ((1 << ppd->lmc) - 1));
  78. qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_PKEY, pkey1,
  79. ah_attr->sl,
  80. sqp->ibqp.qp_num, qp->ibqp.qp_num,
  81. cpu_to_be16(lid),
  82. cpu_to_be16(ah_attr->dlid));
  83. goto drop;
  84. }
  85. }
  86. /*
  87. * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
  88. * Qkeys with the high order bit set mean use the
  89. * qkey from the QP context instead of the WR (see 10.2.5).
  90. */
  91. if (qp->ibqp.qp_num) {
  92. u32 qkey;
  93. qkey = (int)swqe->wr.wr.ud.remote_qkey < 0 ?
  94. sqp->qkey : swqe->wr.wr.ud.remote_qkey;
  95. if (unlikely(qkey != qp->qkey)) {
  96. u16 lid;
  97. lid = ppd->lid | (ah_attr->src_path_bits &
  98. ((1 << ppd->lmc) - 1));
  99. qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_QKEY, qkey,
  100. ah_attr->sl,
  101. sqp->ibqp.qp_num, qp->ibqp.qp_num,
  102. cpu_to_be16(lid),
  103. cpu_to_be16(ah_attr->dlid));
  104. goto drop;
  105. }
  106. }
  107. /*
  108. * A GRH is expected to precede the data even if not
  109. * present on the wire.
  110. */
  111. length = swqe->length;
  112. memset(&wc, 0, sizeof wc);
  113. wc.byte_len = length + sizeof(struct ib_grh);
  114. if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
  115. wc.wc_flags = IB_WC_WITH_IMM;
  116. wc.ex.imm_data = swqe->wr.ex.imm_data;
  117. }
  118. spin_lock_irqsave(&qp->r_lock, flags);
  119. /*
  120. * Get the next work request entry to find where to put the data.
  121. */
  122. if (qp->r_flags & QIB_R_REUSE_SGE)
  123. qp->r_flags &= ~QIB_R_REUSE_SGE;
  124. else {
  125. int ret;
  126. ret = qib_get_rwqe(qp, 0);
  127. if (ret < 0) {
  128. qib_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
  129. goto bail_unlock;
  130. }
  131. if (!ret) {
  132. if (qp->ibqp.qp_num == 0)
  133. ibp->n_vl15_dropped++;
  134. goto bail_unlock;
  135. }
  136. }
  137. /* Silently drop packets which are too big. */
  138. if (unlikely(wc.byte_len > qp->r_len)) {
  139. qp->r_flags |= QIB_R_REUSE_SGE;
  140. ibp->n_pkt_drops++;
  141. goto bail_unlock;
  142. }
  143. if (ah_attr->ah_flags & IB_AH_GRH) {
  144. qib_copy_sge(&qp->r_sge, &ah_attr->grh,
  145. sizeof(struct ib_grh), 1);
  146. wc.wc_flags |= IB_WC_GRH;
  147. } else
  148. qib_skip_sge(&qp->r_sge, sizeof(struct ib_grh), 1);
  149. ssge.sg_list = swqe->sg_list + 1;
  150. ssge.sge = *swqe->sg_list;
  151. ssge.num_sge = swqe->wr.num_sge;
  152. sge = &ssge.sge;
  153. while (length) {
  154. u32 len = sge->length;
  155. if (len > length)
  156. len = length;
  157. if (len > sge->sge_length)
  158. len = sge->sge_length;
  159. BUG_ON(len == 0);
  160. qib_copy_sge(&qp->r_sge, sge->vaddr, len, 1);
  161. sge->vaddr += len;
  162. sge->length -= len;
  163. sge->sge_length -= len;
  164. if (sge->sge_length == 0) {
  165. if (--ssge.num_sge)
  166. *sge = *ssge.sg_list++;
  167. } else if (sge->length == 0 && sge->mr->lkey) {
  168. if (++sge->n >= QIB_SEGSZ) {
  169. if (++sge->m >= sge->mr->mapsz)
  170. break;
  171. sge->n = 0;
  172. }
  173. sge->vaddr =
  174. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  175. sge->length =
  176. sge->mr->map[sge->m]->segs[sge->n].length;
  177. }
  178. length -= len;
  179. }
  180. qib_put_ss(&qp->r_sge);
  181. if (!test_and_clear_bit(QIB_R_WRID_VALID, &qp->r_aflags))
  182. goto bail_unlock;
  183. wc.wr_id = qp->r_wr_id;
  184. wc.status = IB_WC_SUCCESS;
  185. wc.opcode = IB_WC_RECV;
  186. wc.qp = &qp->ibqp;
  187. wc.src_qp = sqp->ibqp.qp_num;
  188. wc.pkey_index = qp->ibqp.qp_type == IB_QPT_GSI ?
  189. swqe->wr.wr.ud.pkey_index : 0;
  190. wc.slid = ppd->lid | (ah_attr->src_path_bits & ((1 << ppd->lmc) - 1));
  191. wc.sl = ah_attr->sl;
  192. wc.dlid_path_bits = ah_attr->dlid & ((1 << ppd->lmc) - 1);
  193. wc.port_num = qp->port_num;
  194. /* Signal completion event if the solicited bit is set. */
  195. qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
  196. swqe->wr.send_flags & IB_SEND_SOLICITED);
  197. ibp->n_loop_pkts++;
  198. bail_unlock:
  199. spin_unlock_irqrestore(&qp->r_lock, flags);
  200. drop:
  201. if (atomic_dec_and_test(&qp->refcount))
  202. wake_up(&qp->wait);
  203. }
  204. /**
  205. * qib_make_ud_req - construct a UD request packet
  206. * @qp: the QP
  207. *
  208. * Return 1 if constructed; otherwise, return 0.
  209. */
  210. int qib_make_ud_req(struct qib_qp *qp)
  211. {
  212. struct qib_other_headers *ohdr;
  213. struct ib_ah_attr *ah_attr;
  214. struct qib_pportdata *ppd;
  215. struct qib_ibport *ibp;
  216. struct qib_swqe *wqe;
  217. unsigned long flags;
  218. u32 nwords;
  219. u32 extra_bytes;
  220. u32 bth0;
  221. u16 lrh0;
  222. u16 lid;
  223. int ret = 0;
  224. int next_cur;
  225. spin_lock_irqsave(&qp->s_lock, flags);
  226. if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_NEXT_SEND_OK)) {
  227. if (!(ib_qib_state_ops[qp->state] & QIB_FLUSH_SEND))
  228. goto bail;
  229. /* We are in the error state, flush the work request. */
  230. if (qp->s_last == qp->s_head)
  231. goto bail;
  232. /* If DMAs are in progress, we can't flush immediately. */
  233. if (atomic_read(&qp->s_dma_busy)) {
  234. qp->s_flags |= QIB_S_WAIT_DMA;
  235. goto bail;
  236. }
  237. wqe = get_swqe_ptr(qp, qp->s_last);
  238. qib_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
  239. goto done;
  240. }
  241. if (qp->s_cur == qp->s_head)
  242. goto bail;
  243. wqe = get_swqe_ptr(qp, qp->s_cur);
  244. next_cur = qp->s_cur + 1;
  245. if (next_cur >= qp->s_size)
  246. next_cur = 0;
  247. /* Construct the header. */
  248. ibp = to_iport(qp->ibqp.device, qp->port_num);
  249. ppd = ppd_from_ibp(ibp);
  250. ah_attr = &to_iah(wqe->wr.wr.ud.ah)->attr;
  251. if (ah_attr->dlid >= QIB_MULTICAST_LID_BASE) {
  252. if (ah_attr->dlid != QIB_PERMISSIVE_LID)
  253. ibp->n_multicast_xmit++;
  254. else
  255. ibp->n_unicast_xmit++;
  256. } else {
  257. ibp->n_unicast_xmit++;
  258. lid = ah_attr->dlid & ~((1 << ppd->lmc) - 1);
  259. if (unlikely(lid == ppd->lid)) {
  260. /*
  261. * If DMAs are in progress, we can't generate
  262. * a completion for the loopback packet since
  263. * it would be out of order.
  264. * XXX Instead of waiting, we could queue a
  265. * zero length descriptor so we get a callback.
  266. */
  267. if (atomic_read(&qp->s_dma_busy)) {
  268. qp->s_flags |= QIB_S_WAIT_DMA;
  269. goto bail;
  270. }
  271. qp->s_cur = next_cur;
  272. spin_unlock_irqrestore(&qp->s_lock, flags);
  273. qib_ud_loopback(qp, wqe);
  274. spin_lock_irqsave(&qp->s_lock, flags);
  275. qib_send_complete(qp, wqe, IB_WC_SUCCESS);
  276. goto done;
  277. }
  278. }
  279. qp->s_cur = next_cur;
  280. extra_bytes = -wqe->length & 3;
  281. nwords = (wqe->length + extra_bytes) >> 2;
  282. /* header size in 32-bit words LRH+BTH+DETH = (8+12+8)/4. */
  283. qp->s_hdrwords = 7;
  284. qp->s_cur_size = wqe->length;
  285. qp->s_cur_sge = &qp->s_sge;
  286. qp->s_srate = ah_attr->static_rate;
  287. qp->s_wqe = wqe;
  288. qp->s_sge.sge = wqe->sg_list[0];
  289. qp->s_sge.sg_list = wqe->sg_list + 1;
  290. qp->s_sge.num_sge = wqe->wr.num_sge;
  291. qp->s_sge.total_len = wqe->length;
  292. if (ah_attr->ah_flags & IB_AH_GRH) {
  293. /* Header size in 32-bit words. */
  294. qp->s_hdrwords += qib_make_grh(ibp, &qp->s_hdr->u.l.grh,
  295. &ah_attr->grh,
  296. qp->s_hdrwords, nwords);
  297. lrh0 = QIB_LRH_GRH;
  298. ohdr = &qp->s_hdr->u.l.oth;
  299. /*
  300. * Don't worry about sending to locally attached multicast
  301. * QPs. It is unspecified by the spec. what happens.
  302. */
  303. } else {
  304. /* Header size in 32-bit words. */
  305. lrh0 = QIB_LRH_BTH;
  306. ohdr = &qp->s_hdr->u.oth;
  307. }
  308. if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
  309. qp->s_hdrwords++;
  310. ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
  311. bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
  312. } else
  313. bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
  314. lrh0 |= ah_attr->sl << 4;
  315. if (qp->ibqp.qp_type == IB_QPT_SMI)
  316. lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
  317. else
  318. lrh0 |= ibp->sl_to_vl[ah_attr->sl] << 12;
  319. qp->s_hdr->lrh[0] = cpu_to_be16(lrh0);
  320. qp->s_hdr->lrh[1] = cpu_to_be16(ah_attr->dlid); /* DEST LID */
  321. qp->s_hdr->lrh[2] = cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
  322. lid = ppd->lid;
  323. if (lid) {
  324. lid |= ah_attr->src_path_bits & ((1 << ppd->lmc) - 1);
  325. qp->s_hdr->lrh[3] = cpu_to_be16(lid);
  326. } else
  327. qp->s_hdr->lrh[3] = IB_LID_PERMISSIVE;
  328. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  329. bth0 |= IB_BTH_SOLICITED;
  330. bth0 |= extra_bytes << 20;
  331. bth0 |= qp->ibqp.qp_type == IB_QPT_SMI ? QIB_DEFAULT_P_KEY :
  332. qib_get_pkey(ibp, qp->ibqp.qp_type == IB_QPT_GSI ?
  333. wqe->wr.wr.ud.pkey_index : qp->s_pkey_index);
  334. ohdr->bth[0] = cpu_to_be32(bth0);
  335. /*
  336. * Use the multicast QP if the destination LID is a multicast LID.
  337. */
  338. ohdr->bth[1] = ah_attr->dlid >= QIB_MULTICAST_LID_BASE &&
  339. ah_attr->dlid != QIB_PERMISSIVE_LID ?
  340. cpu_to_be32(QIB_MULTICAST_QPN) :
  341. cpu_to_be32(wqe->wr.wr.ud.remote_qpn);
  342. ohdr->bth[2] = cpu_to_be32(qp->s_next_psn++ & QIB_PSN_MASK);
  343. /*
  344. * Qkeys with the high order bit set mean use the
  345. * qkey from the QP context instead of the WR (see 10.2.5).
  346. */
  347. ohdr->u.ud.deth[0] = cpu_to_be32((int)wqe->wr.wr.ud.remote_qkey < 0 ?
  348. qp->qkey : wqe->wr.wr.ud.remote_qkey);
  349. ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
  350. done:
  351. ret = 1;
  352. goto unlock;
  353. bail:
  354. qp->s_flags &= ~QIB_S_BUSY;
  355. unlock:
  356. spin_unlock_irqrestore(&qp->s_lock, flags);
  357. return ret;
  358. }
  359. static unsigned qib_lookup_pkey(struct qib_ibport *ibp, u16 pkey)
  360. {
  361. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  362. struct qib_devdata *dd = ppd->dd;
  363. unsigned ctxt = ppd->hw_pidx;
  364. unsigned i;
  365. pkey &= 0x7fff; /* remove limited/full membership bit */
  366. for (i = 0; i < ARRAY_SIZE(dd->rcd[ctxt]->pkeys); ++i)
  367. if ((dd->rcd[ctxt]->pkeys[i] & 0x7fff) == pkey)
  368. return i;
  369. /*
  370. * Should not get here, this means hardware failed to validate pkeys.
  371. * Punt and return index 0.
  372. */
  373. return 0;
  374. }
  375. /**
  376. * qib_ud_rcv - receive an incoming UD packet
  377. * @ibp: the port the packet came in on
  378. * @hdr: the packet header
  379. * @has_grh: true if the packet has a GRH
  380. * @data: the packet data
  381. * @tlen: the packet length
  382. * @qp: the QP the packet came on
  383. *
  384. * This is called from qib_qp_rcv() to process an incoming UD packet
  385. * for the given QP.
  386. * Called at interrupt level.
  387. */
  388. void qib_ud_rcv(struct qib_ibport *ibp, struct qib_ib_header *hdr,
  389. int has_grh, void *data, u32 tlen, struct qib_qp *qp)
  390. {
  391. struct qib_other_headers *ohdr;
  392. int opcode;
  393. u32 hdrsize;
  394. u32 pad;
  395. struct ib_wc wc;
  396. u32 qkey;
  397. u32 src_qp;
  398. u16 dlid;
  399. /* Check for GRH */
  400. if (!has_grh) {
  401. ohdr = &hdr->u.oth;
  402. hdrsize = 8 + 12 + 8; /* LRH + BTH + DETH */
  403. } else {
  404. ohdr = &hdr->u.l.oth;
  405. hdrsize = 8 + 40 + 12 + 8; /* LRH + GRH + BTH + DETH */
  406. }
  407. qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
  408. src_qp = be32_to_cpu(ohdr->u.ud.deth[1]) & QIB_QPN_MASK;
  409. /*
  410. * Get the number of bytes the message was padded by
  411. * and drop incomplete packets.
  412. */
  413. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  414. if (unlikely(tlen < (hdrsize + pad + 4)))
  415. goto drop;
  416. tlen -= hdrsize + pad + 4;
  417. /*
  418. * Check that the permissive LID is only used on QP0
  419. * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
  420. */
  421. if (qp->ibqp.qp_num) {
  422. if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||
  423. hdr->lrh[3] == IB_LID_PERMISSIVE))
  424. goto drop;
  425. if (qp->ibqp.qp_num > 1) {
  426. u16 pkey1, pkey2;
  427. pkey1 = be32_to_cpu(ohdr->bth[0]);
  428. pkey2 = qib_get_pkey(ibp, qp->s_pkey_index);
  429. if (unlikely(!qib_pkey_ok(pkey1, pkey2))) {
  430. qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_PKEY,
  431. pkey1,
  432. (be16_to_cpu(hdr->lrh[0]) >> 4) &
  433. 0xF,
  434. src_qp, qp->ibqp.qp_num,
  435. hdr->lrh[3], hdr->lrh[1]);
  436. return;
  437. }
  438. }
  439. if (unlikely(qkey != qp->qkey)) {
  440. qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_QKEY, qkey,
  441. (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
  442. src_qp, qp->ibqp.qp_num,
  443. hdr->lrh[3], hdr->lrh[1]);
  444. return;
  445. }
  446. /* Drop invalid MAD packets (see 13.5.3.1). */
  447. if (unlikely(qp->ibqp.qp_num == 1 &&
  448. (tlen != 256 ||
  449. (be16_to_cpu(hdr->lrh[0]) >> 12) == 15)))
  450. goto drop;
  451. } else {
  452. struct ib_smp *smp;
  453. /* Drop invalid MAD packets (see 13.5.3.1). */
  454. if (tlen != 256 || (be16_to_cpu(hdr->lrh[0]) >> 12) != 15)
  455. goto drop;
  456. smp = (struct ib_smp *) data;
  457. if ((hdr->lrh[1] == IB_LID_PERMISSIVE ||
  458. hdr->lrh[3] == IB_LID_PERMISSIVE) &&
  459. smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  460. goto drop;
  461. }
  462. /*
  463. * The opcode is in the low byte when its in network order
  464. * (top byte when in host order).
  465. */
  466. opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
  467. if (qp->ibqp.qp_num > 1 &&
  468. opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
  469. wc.ex.imm_data = ohdr->u.ud.imm_data;
  470. wc.wc_flags = IB_WC_WITH_IMM;
  471. tlen -= sizeof(u32);
  472. } else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
  473. wc.ex.imm_data = 0;
  474. wc.wc_flags = 0;
  475. } else
  476. goto drop;
  477. /*
  478. * A GRH is expected to precede the data even if not
  479. * present on the wire.
  480. */
  481. wc.byte_len = tlen + sizeof(struct ib_grh);
  482. /*
  483. * Get the next work request entry to find where to put the data.
  484. */
  485. if (qp->r_flags & QIB_R_REUSE_SGE)
  486. qp->r_flags &= ~QIB_R_REUSE_SGE;
  487. else {
  488. int ret;
  489. ret = qib_get_rwqe(qp, 0);
  490. if (ret < 0) {
  491. qib_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
  492. return;
  493. }
  494. if (!ret) {
  495. if (qp->ibqp.qp_num == 0)
  496. ibp->n_vl15_dropped++;
  497. return;
  498. }
  499. }
  500. /* Silently drop packets which are too big. */
  501. if (unlikely(wc.byte_len > qp->r_len)) {
  502. qp->r_flags |= QIB_R_REUSE_SGE;
  503. goto drop;
  504. }
  505. if (has_grh) {
  506. qib_copy_sge(&qp->r_sge, &hdr->u.l.grh,
  507. sizeof(struct ib_grh), 1);
  508. wc.wc_flags |= IB_WC_GRH;
  509. } else
  510. qib_skip_sge(&qp->r_sge, sizeof(struct ib_grh), 1);
  511. qib_copy_sge(&qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh), 1);
  512. qib_put_ss(&qp->r_sge);
  513. if (!test_and_clear_bit(QIB_R_WRID_VALID, &qp->r_aflags))
  514. return;
  515. wc.wr_id = qp->r_wr_id;
  516. wc.status = IB_WC_SUCCESS;
  517. wc.opcode = IB_WC_RECV;
  518. wc.vendor_err = 0;
  519. wc.qp = &qp->ibqp;
  520. wc.src_qp = src_qp;
  521. wc.pkey_index = qp->ibqp.qp_type == IB_QPT_GSI ?
  522. qib_lookup_pkey(ibp, be32_to_cpu(ohdr->bth[0])) : 0;
  523. wc.slid = be16_to_cpu(hdr->lrh[3]);
  524. wc.sl = (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF;
  525. dlid = be16_to_cpu(hdr->lrh[1]);
  526. /*
  527. * Save the LMC lower bits if the destination LID is a unicast LID.
  528. */
  529. wc.dlid_path_bits = dlid >= QIB_MULTICAST_LID_BASE ? 0 :
  530. dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
  531. wc.port_num = qp->port_num;
  532. /* Signal completion event if the solicited bit is set. */
  533. qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
  534. (ohdr->bth[0] &
  535. cpu_to_be32(IB_BTH_SOLICITED)) != 0);
  536. return;
  537. drop:
  538. ibp->n_pkt_drops++;
  539. }