qib_ud.c 16 KB

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