ud.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /*
  2. * Copyright(c) 2015, 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/net.h>
  48. #include <rdma/ib_smi.h>
  49. #include "hfi.h"
  50. #include "mad.h"
  51. #include "verbs_txreq.h"
  52. #include "qp.h"
  53. /**
  54. * ud_loopback - handle send on loopback QPs
  55. * @sqp: the sending QP
  56. * @swqe: the send work request
  57. *
  58. * This is called from hfi1_make_ud_req() to forward a WQE addressed
  59. * to the same HFI.
  60. * Note that the receive interrupt handler may be calling hfi1_ud_rcv()
  61. * while this is being called.
  62. */
  63. static void ud_loopback(struct rvt_qp *sqp, struct rvt_swqe *swqe)
  64. {
  65. struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
  66. struct hfi1_pportdata *ppd;
  67. struct rvt_qp *qp;
  68. struct rdma_ah_attr *ah_attr;
  69. unsigned long flags;
  70. struct rvt_sge_state ssge;
  71. struct rvt_sge *sge;
  72. struct ib_wc wc;
  73. u32 length;
  74. enum ib_qp_type sqptype, dqptype;
  75. rcu_read_lock();
  76. qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
  77. swqe->ud_wr.remote_qpn);
  78. if (!qp) {
  79. ibp->rvp.n_pkt_drops++;
  80. rcu_read_unlock();
  81. return;
  82. }
  83. sqptype = sqp->ibqp.qp_type == IB_QPT_GSI ?
  84. IB_QPT_UD : sqp->ibqp.qp_type;
  85. dqptype = qp->ibqp.qp_type == IB_QPT_GSI ?
  86. IB_QPT_UD : qp->ibqp.qp_type;
  87. if (dqptype != sqptype ||
  88. !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
  89. ibp->rvp.n_pkt_drops++;
  90. goto drop;
  91. }
  92. ah_attr = &ibah_to_rvtah(swqe->ud_wr.ah)->attr;
  93. ppd = ppd_from_ibp(ibp);
  94. if (qp->ibqp.qp_num > 1) {
  95. u16 pkey;
  96. u16 slid;
  97. u8 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
  98. pkey = hfi1_get_pkey(ibp, sqp->s_pkey_index);
  99. slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
  100. ((1 << ppd->lmc) - 1));
  101. if (unlikely(ingress_pkey_check(ppd, pkey, sc5,
  102. qp->s_pkey_index, slid))) {
  103. hfi1_bad_pkey(ibp, pkey,
  104. rdma_ah_get_sl(ah_attr),
  105. sqp->ibqp.qp_num, qp->ibqp.qp_num,
  106. slid, rdma_ah_get_dlid(ah_attr));
  107. goto drop;
  108. }
  109. }
  110. /*
  111. * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
  112. * Qkeys with the high order bit set mean use the
  113. * qkey from the QP context instead of the WR (see 10.2.5).
  114. */
  115. if (qp->ibqp.qp_num) {
  116. u32 qkey;
  117. qkey = (int)swqe->ud_wr.remote_qkey < 0 ?
  118. sqp->qkey : swqe->ud_wr.remote_qkey;
  119. if (unlikely(qkey != qp->qkey))
  120. goto drop; /* silently drop per IBTA spec */
  121. }
  122. /*
  123. * A GRH is expected to precede the data even if not
  124. * present on the wire.
  125. */
  126. length = swqe->length;
  127. memset(&wc, 0, sizeof(wc));
  128. wc.byte_len = length + sizeof(struct ib_grh);
  129. if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
  130. wc.wc_flags = IB_WC_WITH_IMM;
  131. wc.ex.imm_data = swqe->wr.ex.imm_data;
  132. }
  133. spin_lock_irqsave(&qp->r_lock, flags);
  134. /*
  135. * Get the next work request entry to find where to put the data.
  136. */
  137. if (qp->r_flags & RVT_R_REUSE_SGE) {
  138. qp->r_flags &= ~RVT_R_REUSE_SGE;
  139. } else {
  140. int ret;
  141. ret = hfi1_rvt_get_rwqe(qp, 0);
  142. if (ret < 0) {
  143. rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
  144. goto bail_unlock;
  145. }
  146. if (!ret) {
  147. if (qp->ibqp.qp_num == 0)
  148. ibp->rvp.n_vl15_dropped++;
  149. goto bail_unlock;
  150. }
  151. }
  152. /* Silently drop packets which are too big. */
  153. if (unlikely(wc.byte_len > qp->r_len)) {
  154. qp->r_flags |= RVT_R_REUSE_SGE;
  155. ibp->rvp.n_pkt_drops++;
  156. goto bail_unlock;
  157. }
  158. if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
  159. struct ib_grh grh;
  160. const struct ib_global_route *grd = rdma_ah_read_grh(ah_attr);
  161. hfi1_make_grh(ibp, &grh, grd, 0, 0);
  162. hfi1_copy_sge(&qp->r_sge, &grh,
  163. sizeof(grh), true, false);
  164. wc.wc_flags |= IB_WC_GRH;
  165. } else {
  166. rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
  167. }
  168. ssge.sg_list = swqe->sg_list + 1;
  169. ssge.sge = *swqe->sg_list;
  170. ssge.num_sge = swqe->wr.num_sge;
  171. sge = &ssge.sge;
  172. while (length) {
  173. u32 len = sge->length;
  174. if (len > length)
  175. len = length;
  176. if (len > sge->sge_length)
  177. len = sge->sge_length;
  178. WARN_ON_ONCE(len == 0);
  179. hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, true, false);
  180. sge->vaddr += len;
  181. sge->length -= len;
  182. sge->sge_length -= len;
  183. if (sge->sge_length == 0) {
  184. if (--ssge.num_sge)
  185. *sge = *ssge.sg_list++;
  186. } else if (sge->length == 0 && sge->mr->lkey) {
  187. if (++sge->n >= RVT_SEGSZ) {
  188. if (++sge->m >= sge->mr->mapsz)
  189. break;
  190. sge->n = 0;
  191. }
  192. sge->vaddr =
  193. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  194. sge->length =
  195. sge->mr->map[sge->m]->segs[sge->n].length;
  196. }
  197. length -= len;
  198. }
  199. rvt_put_ss(&qp->r_sge);
  200. if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
  201. goto bail_unlock;
  202. wc.wr_id = qp->r_wr_id;
  203. wc.status = IB_WC_SUCCESS;
  204. wc.opcode = IB_WC_RECV;
  205. wc.qp = &qp->ibqp;
  206. wc.src_qp = sqp->ibqp.qp_num;
  207. if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI) {
  208. if (sqp->ibqp.qp_type == IB_QPT_GSI ||
  209. sqp->ibqp.qp_type == IB_QPT_SMI)
  210. wc.pkey_index = swqe->ud_wr.pkey_index;
  211. else
  212. wc.pkey_index = sqp->s_pkey_index;
  213. } else {
  214. wc.pkey_index = 0;
  215. }
  216. wc.slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
  217. ((1 << ppd->lmc) - 1));
  218. /* Check for loopback when the port lid is not set */
  219. if (wc.slid == 0 && sqp->ibqp.qp_type == IB_QPT_GSI)
  220. wc.slid = be16_to_cpu(IB_LID_PERMISSIVE);
  221. wc.sl = rdma_ah_get_sl(ah_attr);
  222. wc.dlid_path_bits = rdma_ah_get_dlid(ah_attr) & ((1 << ppd->lmc) - 1);
  223. wc.port_num = qp->port_num;
  224. /* Signal completion event if the solicited bit is set. */
  225. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
  226. swqe->wr.send_flags & IB_SEND_SOLICITED);
  227. ibp->rvp.n_loop_pkts++;
  228. bail_unlock:
  229. spin_unlock_irqrestore(&qp->r_lock, flags);
  230. drop:
  231. rcu_read_unlock();
  232. }
  233. /**
  234. * hfi1_make_ud_req - construct a UD request packet
  235. * @qp: the QP
  236. *
  237. * Assume s_lock is held.
  238. *
  239. * Return 1 if constructed; otherwise, return 0.
  240. */
  241. int hfi1_make_ud_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
  242. {
  243. struct hfi1_qp_priv *priv = qp->priv;
  244. struct ib_other_headers *ohdr;
  245. struct rdma_ah_attr *ah_attr;
  246. struct hfi1_pportdata *ppd;
  247. struct hfi1_ibport *ibp;
  248. struct rvt_swqe *wqe;
  249. u32 nwords;
  250. u32 extra_bytes;
  251. u32 bth0;
  252. u16 lrh0;
  253. u16 lid;
  254. int next_cur;
  255. u8 sc5;
  256. ps->s_txreq = get_txreq(ps->dev, qp);
  257. if (IS_ERR(ps->s_txreq))
  258. goto bail_no_tx;
  259. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
  260. if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
  261. goto bail;
  262. /* We are in the error state, flush the work request. */
  263. smp_read_barrier_depends(); /* see post_one_send */
  264. if (qp->s_last == ACCESS_ONCE(qp->s_head))
  265. goto bail;
  266. /* If DMAs are in progress, we can't flush immediately. */
  267. if (iowait_sdma_pending(&priv->s_iowait)) {
  268. qp->s_flags |= RVT_S_WAIT_DMA;
  269. goto bail;
  270. }
  271. wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  272. hfi1_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
  273. goto done_free_tx;
  274. }
  275. /* see post_one_send() */
  276. smp_read_barrier_depends();
  277. if (qp->s_cur == ACCESS_ONCE(qp->s_head))
  278. goto bail;
  279. wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
  280. next_cur = qp->s_cur + 1;
  281. if (next_cur >= qp->s_size)
  282. next_cur = 0;
  283. /* Construct the header. */
  284. ibp = to_iport(qp->ibqp.device, qp->port_num);
  285. ppd = ppd_from_ibp(ibp);
  286. ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
  287. if (rdma_ah_get_dlid(ah_attr) < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
  288. rdma_ah_get_dlid(ah_attr) == be16_to_cpu(IB_LID_PERMISSIVE)) {
  289. lid = rdma_ah_get_dlid(ah_attr) & ~((1 << ppd->lmc) - 1);
  290. if (unlikely(!loopback &&
  291. (lid == ppd->lid ||
  292. (lid == be16_to_cpu(IB_LID_PERMISSIVE) &&
  293. qp->ibqp.qp_type == IB_QPT_GSI)))) {
  294. unsigned long tflags = ps->flags;
  295. /*
  296. * If DMAs are in progress, we can't generate
  297. * a completion for the loopback packet since
  298. * it would be out of order.
  299. * Instead of waiting, we could queue a
  300. * zero length descriptor so we get a callback.
  301. */
  302. if (iowait_sdma_pending(&priv->s_iowait)) {
  303. qp->s_flags |= RVT_S_WAIT_DMA;
  304. goto bail;
  305. }
  306. qp->s_cur = next_cur;
  307. spin_unlock_irqrestore(&qp->s_lock, tflags);
  308. ud_loopback(qp, wqe);
  309. spin_lock_irqsave(&qp->s_lock, tflags);
  310. ps->flags = tflags;
  311. hfi1_send_complete(qp, wqe, IB_WC_SUCCESS);
  312. goto done_free_tx;
  313. }
  314. }
  315. qp->s_cur = next_cur;
  316. extra_bytes = -wqe->length & 3;
  317. nwords = (wqe->length + extra_bytes) >> 2;
  318. /* header size in 32-bit words LRH+BTH+DETH = (8+12+8)/4. */
  319. qp->s_hdrwords = 7;
  320. ps->s_txreq->s_cur_size = wqe->length;
  321. ps->s_txreq->ss = &qp->s_sge;
  322. qp->s_srate = rdma_ah_get_static_rate(ah_attr);
  323. qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
  324. qp->s_wqe = wqe;
  325. qp->s_sge.sge = wqe->sg_list[0];
  326. qp->s_sge.sg_list = wqe->sg_list + 1;
  327. qp->s_sge.num_sge = wqe->wr.num_sge;
  328. qp->s_sge.total_len = wqe->length;
  329. if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
  330. /* Header size in 32-bit words. */
  331. qp->s_hdrwords += hfi1_make_grh(ibp,
  332. &ps->s_txreq->phdr.hdr.u.l.grh,
  333. rdma_ah_read_grh(ah_attr),
  334. qp->s_hdrwords, nwords);
  335. lrh0 = HFI1_LRH_GRH;
  336. ohdr = &ps->s_txreq->phdr.hdr.u.l.oth;
  337. /*
  338. * Don't worry about sending to locally attached multicast
  339. * QPs. It is unspecified by the spec. what happens.
  340. */
  341. } else {
  342. /* Header size in 32-bit words. */
  343. lrh0 = HFI1_LRH_BTH;
  344. ohdr = &ps->s_txreq->phdr.hdr.u.oth;
  345. }
  346. if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
  347. qp->s_hdrwords++;
  348. ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
  349. bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
  350. } else {
  351. bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
  352. }
  353. sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
  354. lrh0 |= (rdma_ah_get_sl(ah_attr) & 0xf) << 4;
  355. if (qp->ibqp.qp_type == IB_QPT_SMI) {
  356. lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
  357. priv->s_sc = 0xf;
  358. } else {
  359. lrh0 |= (sc5 & 0xf) << 12;
  360. priv->s_sc = sc5;
  361. }
  362. priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
  363. ps->s_txreq->sde = priv->s_sde;
  364. priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
  365. ps->s_txreq->psc = priv->s_sendcontext;
  366. ps->s_txreq->phdr.hdr.lrh[0] = cpu_to_be16(lrh0);
  367. ps->s_txreq->phdr.hdr.lrh[1] =
  368. cpu_to_be16(rdma_ah_get_dlid(ah_attr));
  369. ps->s_txreq->phdr.hdr.lrh[2] =
  370. cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
  371. if (rdma_ah_get_dlid(ah_attr) == be16_to_cpu(IB_LID_PERMISSIVE)) {
  372. ps->s_txreq->phdr.hdr.lrh[3] = IB_LID_PERMISSIVE;
  373. } else {
  374. lid = ppd->lid;
  375. if (lid) {
  376. lid |= rdma_ah_get_path_bits(ah_attr) &
  377. ((1 << ppd->lmc) - 1);
  378. ps->s_txreq->phdr.hdr.lrh[3] = cpu_to_be16(lid);
  379. } else {
  380. ps->s_txreq->phdr.hdr.lrh[3] = IB_LID_PERMISSIVE;
  381. }
  382. }
  383. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  384. bth0 |= IB_BTH_SOLICITED;
  385. bth0 |= extra_bytes << 20;
  386. if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI)
  387. bth0 |= hfi1_get_pkey(ibp, wqe->ud_wr.pkey_index);
  388. else
  389. bth0 |= hfi1_get_pkey(ibp, qp->s_pkey_index);
  390. ohdr->bth[0] = cpu_to_be32(bth0);
  391. ohdr->bth[1] = cpu_to_be32(wqe->ud_wr.remote_qpn);
  392. ohdr->bth[2] = cpu_to_be32(mask_psn(wqe->psn));
  393. /*
  394. * Qkeys with the high order bit set mean use the
  395. * qkey from the QP context instead of the WR (see 10.2.5).
  396. */
  397. ohdr->u.ud.deth[0] = cpu_to_be32((int)wqe->ud_wr.remote_qkey < 0 ?
  398. qp->qkey : wqe->ud_wr.remote_qkey);
  399. ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
  400. /* disarm any ahg */
  401. priv->s_ahg->ahgcount = 0;
  402. priv->s_ahg->ahgidx = 0;
  403. priv->s_ahg->tx_flags = 0;
  404. /* pbc */
  405. ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
  406. return 1;
  407. done_free_tx:
  408. hfi1_put_txreq(ps->s_txreq);
  409. ps->s_txreq = NULL;
  410. return 1;
  411. bail:
  412. hfi1_put_txreq(ps->s_txreq);
  413. bail_no_tx:
  414. ps->s_txreq = NULL;
  415. qp->s_flags &= ~RVT_S_BUSY;
  416. qp->s_hdrwords = 0;
  417. return 0;
  418. }
  419. /*
  420. * Hardware can't check this so we do it here.
  421. *
  422. * This is a slightly different algorithm than the standard pkey check. It
  423. * special cases the management keys and allows for 0x7fff and 0xffff to be in
  424. * the table at the same time.
  425. *
  426. * @returns the index found or -1 if not found
  427. */
  428. int hfi1_lookup_pkey_idx(struct hfi1_ibport *ibp, u16 pkey)
  429. {
  430. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  431. unsigned i;
  432. if (pkey == FULL_MGMT_P_KEY || pkey == LIM_MGMT_P_KEY) {
  433. unsigned lim_idx = -1;
  434. for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i) {
  435. /* here we look for an exact match */
  436. if (ppd->pkeys[i] == pkey)
  437. return i;
  438. if (ppd->pkeys[i] == LIM_MGMT_P_KEY)
  439. lim_idx = i;
  440. }
  441. /* did not find 0xffff return 0x7fff idx if found */
  442. if (pkey == FULL_MGMT_P_KEY)
  443. return lim_idx;
  444. /* no match... */
  445. return -1;
  446. }
  447. pkey &= 0x7fff; /* remove limited/full membership bit */
  448. for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i)
  449. if ((ppd->pkeys[i] & 0x7fff) == pkey)
  450. return i;
  451. /*
  452. * Should not get here, this means hardware failed to validate pkeys.
  453. */
  454. return -1;
  455. }
  456. void return_cnp(struct hfi1_ibport *ibp, struct rvt_qp *qp, u32 remote_qpn,
  457. u32 pkey, u32 slid, u32 dlid, u8 sc5,
  458. const struct ib_grh *old_grh)
  459. {
  460. u64 pbc, pbc_flags = 0;
  461. u32 bth0, plen, vl, hwords = 5;
  462. u16 lrh0;
  463. u8 sl = ibp->sc_to_sl[sc5];
  464. struct ib_header hdr;
  465. struct ib_other_headers *ohdr;
  466. struct pio_buf *pbuf;
  467. struct send_context *ctxt = qp_to_send_context(qp, sc5);
  468. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  469. if (old_grh) {
  470. struct ib_grh *grh = &hdr.u.l.grh;
  471. grh->version_tclass_flow = old_grh->version_tclass_flow;
  472. grh->paylen = cpu_to_be16((hwords - 2 + SIZE_OF_CRC) << 2);
  473. grh->hop_limit = 0xff;
  474. grh->sgid = old_grh->dgid;
  475. grh->dgid = old_grh->sgid;
  476. ohdr = &hdr.u.l.oth;
  477. lrh0 = HFI1_LRH_GRH;
  478. hwords += sizeof(struct ib_grh) / sizeof(u32);
  479. } else {
  480. ohdr = &hdr.u.oth;
  481. lrh0 = HFI1_LRH_BTH;
  482. }
  483. lrh0 |= (sc5 & 0xf) << 12 | sl << 4;
  484. bth0 = pkey | (IB_OPCODE_CNP << 24);
  485. ohdr->bth[0] = cpu_to_be32(bth0);
  486. ohdr->bth[1] = cpu_to_be32(remote_qpn | (1 << IB_BECN_SHIFT));
  487. ohdr->bth[2] = 0; /* PSN 0 */
  488. hdr.lrh[0] = cpu_to_be16(lrh0);
  489. hdr.lrh[1] = cpu_to_be16(dlid);
  490. hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
  491. hdr.lrh[3] = cpu_to_be16(slid);
  492. plen = 2 /* PBC */ + hwords;
  493. pbc_flags |= (ib_is_sc5(sc5) << PBC_DC_INFO_SHIFT);
  494. vl = sc_to_vlt(ppd->dd, sc5);
  495. pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
  496. if (ctxt) {
  497. pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
  498. if (pbuf)
  499. ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
  500. &hdr, hwords);
  501. }
  502. }
  503. /*
  504. * opa_smp_check() - Do the regular pkey checking, and the additional
  505. * checks for SMPs specified in OPAv1 rev 1.0, 9/19/2016 update, section
  506. * 9.10.25 ("SMA Packet Checks").
  507. *
  508. * Note that:
  509. * - Checks are done using the pkey directly from the packet's BTH,
  510. * and specifically _not_ the pkey that we attach to the completion,
  511. * which may be different.
  512. * - These checks are specifically for "non-local" SMPs (i.e., SMPs
  513. * which originated on another node). SMPs which are sent from, and
  514. * destined to this node are checked in opa_local_smp_check().
  515. *
  516. * At the point where opa_smp_check() is called, we know:
  517. * - destination QP is QP0
  518. *
  519. * opa_smp_check() returns 0 if all checks succeed, 1 otherwise.
  520. */
  521. static int opa_smp_check(struct hfi1_ibport *ibp, u16 pkey, u8 sc5,
  522. struct rvt_qp *qp, u16 slid, struct opa_smp *smp)
  523. {
  524. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  525. /*
  526. * I don't think it's possible for us to get here with sc != 0xf,
  527. * but check it to be certain.
  528. */
  529. if (sc5 != 0xf)
  530. return 1;
  531. if (rcv_pkey_check(ppd, pkey, sc5, slid))
  532. return 1;
  533. /*
  534. * At this point we know (and so don't need to check again) that
  535. * the pkey is either LIM_MGMT_P_KEY, or FULL_MGMT_P_KEY
  536. * (see ingress_pkey_check).
  537. */
  538. if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE &&
  539. smp->mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED) {
  540. ingress_pkey_table_fail(ppd, pkey, slid);
  541. return 1;
  542. }
  543. /*
  544. * SMPs fall into one of four (disjoint) categories:
  545. * SMA request, SMA response, SMA trap, or SMA trap repress.
  546. * Our response depends, in part, on which type of SMP we're
  547. * processing.
  548. *
  549. * If this is an SMA response, skip the check here.
  550. *
  551. * If this is an SMA request or SMA trap repress:
  552. * - pkey != FULL_MGMT_P_KEY =>
  553. * increment port recv constraint errors, drop MAD
  554. *
  555. * Otherwise:
  556. * - accept if the port is running an SM
  557. * - drop MAD if it's an SMA trap
  558. * - pkey == FULL_MGMT_P_KEY =>
  559. * reply with unsupported method
  560. * - pkey != FULL_MGMT_P_KEY =>
  561. * increment port recv constraint errors, drop MAD
  562. */
  563. switch (smp->method) {
  564. case IB_MGMT_METHOD_GET_RESP:
  565. case IB_MGMT_METHOD_REPORT_RESP:
  566. break;
  567. case IB_MGMT_METHOD_GET:
  568. case IB_MGMT_METHOD_SET:
  569. case IB_MGMT_METHOD_REPORT:
  570. case IB_MGMT_METHOD_TRAP_REPRESS:
  571. if (pkey != FULL_MGMT_P_KEY) {
  572. ingress_pkey_table_fail(ppd, pkey, slid);
  573. return 1;
  574. }
  575. break;
  576. default:
  577. if (ibp->rvp.port_cap_flags & IB_PORT_SM)
  578. return 0;
  579. if (smp->method == IB_MGMT_METHOD_TRAP)
  580. return 1;
  581. if (pkey == FULL_MGMT_P_KEY) {
  582. smp->status |= IB_SMP_UNSUP_METHOD;
  583. return 0;
  584. }
  585. ingress_pkey_table_fail(ppd, pkey, slid);
  586. return 1;
  587. }
  588. return 0;
  589. }
  590. /**
  591. * hfi1_ud_rcv - receive an incoming UD packet
  592. * @ibp: the port the packet came in on
  593. * @hdr: the packet header
  594. * @rcv_flags: flags relevant to rcv processing
  595. * @data: the packet data
  596. * @tlen: the packet length
  597. * @qp: the QP the packet came on
  598. *
  599. * This is called from qp_rcv() to process an incoming UD packet
  600. * for the given QP.
  601. * Called at interrupt level.
  602. */
  603. void hfi1_ud_rcv(struct hfi1_packet *packet)
  604. {
  605. struct ib_other_headers *ohdr = packet->ohdr;
  606. u32 hdrsize = packet->hlen;
  607. struct ib_wc wc;
  608. u32 qkey;
  609. u32 src_qp;
  610. u16 pkey;
  611. int mgmt_pkey_idx = -1;
  612. struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
  613. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  614. struct ib_header *hdr = packet->hdr;
  615. void *data = packet->payload;
  616. u32 tlen = packet->tlen;
  617. struct rvt_qp *qp = packet->qp;
  618. u8 sc5 = hfi1_9B_get_sc5(hdr, packet->rhf);
  619. u8 sl_from_sc;
  620. u8 extra_bytes = packet->pad;
  621. u8 opcode = packet->opcode;
  622. u8 sl = packet->sl;
  623. u32 dlid = packet->dlid;
  624. u32 slid = packet->slid;
  625. qkey = ib_get_qkey(ohdr);
  626. src_qp = ib_get_sqpn(ohdr);
  627. pkey = ib_bth_get_pkey(ohdr);
  628. extra_bytes += (SIZE_OF_CRC << 2);
  629. sl_from_sc = ibp->sc_to_sl[sc5];
  630. process_ecn(qp, packet, (opcode != IB_OPCODE_CNP));
  631. /*
  632. * Get the number of bytes the message was padded by
  633. * and drop incomplete packets.
  634. */
  635. if (unlikely(tlen < (hdrsize + extra_bytes)))
  636. goto drop;
  637. tlen -= hdrsize + extra_bytes;
  638. /*
  639. * Check that the permissive LID is only used on QP0
  640. * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
  641. */
  642. if (qp->ibqp.qp_num) {
  643. if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||
  644. hdr->lrh[3] == IB_LID_PERMISSIVE))
  645. goto drop;
  646. if (qp->ibqp.qp_num > 1) {
  647. if (unlikely(rcv_pkey_check(ppd, pkey, sc5, slid))) {
  648. /*
  649. * Traps will not be sent for packets dropped
  650. * by the HW. This is fine, as sending trap
  651. * for invalid pkeys is optional according to
  652. * IB spec (release 1.3, section 10.9.4)
  653. */
  654. hfi1_bad_pkey(ibp,
  655. pkey, sl,
  656. src_qp, qp->ibqp.qp_num,
  657. slid, dlid);
  658. return;
  659. }
  660. } else {
  661. /* GSI packet */
  662. mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
  663. if (mgmt_pkey_idx < 0)
  664. goto drop;
  665. }
  666. if (unlikely(qkey != qp->qkey)) /* Silent drop */
  667. return;
  668. /* Drop invalid MAD packets (see 13.5.3.1). */
  669. if (unlikely(qp->ibqp.qp_num == 1 &&
  670. (tlen > 2048 || (sc5 == 0xF))))
  671. goto drop;
  672. } else {
  673. /* Received on QP0, and so by definition, this is an SMP */
  674. struct opa_smp *smp = (struct opa_smp *)data;
  675. if (opa_smp_check(ibp, pkey, sc5, qp, slid, smp))
  676. goto drop;
  677. if (tlen > 2048)
  678. goto drop;
  679. if ((hdr->lrh[1] == IB_LID_PERMISSIVE ||
  680. hdr->lrh[3] == IB_LID_PERMISSIVE) &&
  681. smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  682. goto drop;
  683. /* look up SMI pkey */
  684. mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
  685. if (mgmt_pkey_idx < 0)
  686. goto drop;
  687. }
  688. if (qp->ibqp.qp_num > 1 &&
  689. opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
  690. wc.ex.imm_data = ohdr->u.ud.imm_data;
  691. wc.wc_flags = IB_WC_WITH_IMM;
  692. tlen -= sizeof(u32);
  693. } else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
  694. wc.ex.imm_data = 0;
  695. wc.wc_flags = 0;
  696. } else {
  697. goto drop;
  698. }
  699. /*
  700. * A GRH is expected to precede the data even if not
  701. * present on the wire.
  702. */
  703. wc.byte_len = tlen + sizeof(struct ib_grh);
  704. /*
  705. * Get the next work request entry to find where to put the data.
  706. */
  707. if (qp->r_flags & RVT_R_REUSE_SGE) {
  708. qp->r_flags &= ~RVT_R_REUSE_SGE;
  709. } else {
  710. int ret;
  711. ret = hfi1_rvt_get_rwqe(qp, 0);
  712. if (ret < 0) {
  713. rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
  714. return;
  715. }
  716. if (!ret) {
  717. if (qp->ibqp.qp_num == 0)
  718. ibp->rvp.n_vl15_dropped++;
  719. return;
  720. }
  721. }
  722. /* Silently drop packets which are too big. */
  723. if (unlikely(wc.byte_len > qp->r_len)) {
  724. qp->r_flags |= RVT_R_REUSE_SGE;
  725. goto drop;
  726. }
  727. if (packet->grh) {
  728. hfi1_copy_sge(&qp->r_sge, &hdr->u.l.grh,
  729. sizeof(struct ib_grh), true, false);
  730. wc.wc_flags |= IB_WC_GRH;
  731. } else {
  732. rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
  733. }
  734. hfi1_copy_sge(&qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh),
  735. true, false);
  736. rvt_put_ss(&qp->r_sge);
  737. if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
  738. return;
  739. wc.wr_id = qp->r_wr_id;
  740. wc.status = IB_WC_SUCCESS;
  741. wc.opcode = IB_WC_RECV;
  742. wc.vendor_err = 0;
  743. wc.qp = &qp->ibqp;
  744. wc.src_qp = src_qp;
  745. if (qp->ibqp.qp_type == IB_QPT_GSI ||
  746. qp->ibqp.qp_type == IB_QPT_SMI) {
  747. if (mgmt_pkey_idx < 0) {
  748. if (net_ratelimit()) {
  749. struct hfi1_devdata *dd = ppd->dd;
  750. dd_dev_err(dd, "QP type %d mgmt_pkey_idx < 0 and packet not dropped???\n",
  751. qp->ibqp.qp_type);
  752. mgmt_pkey_idx = 0;
  753. }
  754. }
  755. wc.pkey_index = (unsigned)mgmt_pkey_idx;
  756. } else {
  757. wc.pkey_index = 0;
  758. }
  759. wc.slid = slid;
  760. wc.sl = sl_from_sc;
  761. /*
  762. * Save the LMC lower bits if the destination LID is a unicast LID.
  763. */
  764. wc.dlid_path_bits = dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE) ? 0 :
  765. dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
  766. wc.port_num = qp->port_num;
  767. /* Signal completion event if the solicited bit is set. */
  768. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
  769. (ohdr->bth[0] &
  770. cpu_to_be32(IB_BTH_SOLICITED)) != 0);
  771. return;
  772. drop:
  773. ibp->rvp.n_pkt_drops++;
  774. }