qib_ud.c 17 KB

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