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