qib_ud.c 16 KB

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