qib_ud.c 17 KB

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