ud.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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_pqkey(ibp, OPA_TRAP_BAD_P_KEY, 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. u16 lid;
  121. lid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
  122. ((1 << ppd->lmc) - 1));
  123. hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_Q_KEY, qkey,
  124. rdma_ah_get_sl(ah_attr),
  125. sqp->ibqp.qp_num, qp->ibqp.qp_num,
  126. lid,
  127. rdma_ah_get_dlid(ah_attr));
  128. goto drop;
  129. }
  130. }
  131. /*
  132. * A GRH is expected to precede the data even if not
  133. * present on the wire.
  134. */
  135. length = swqe->length;
  136. memset(&wc, 0, sizeof(wc));
  137. wc.byte_len = length + sizeof(struct ib_grh);
  138. if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
  139. wc.wc_flags = IB_WC_WITH_IMM;
  140. wc.ex.imm_data = swqe->wr.ex.imm_data;
  141. }
  142. spin_lock_irqsave(&qp->r_lock, flags);
  143. /*
  144. * Get the next work request entry to find where to put the data.
  145. */
  146. if (qp->r_flags & RVT_R_REUSE_SGE) {
  147. qp->r_flags &= ~RVT_R_REUSE_SGE;
  148. } else {
  149. int ret;
  150. ret = hfi1_rvt_get_rwqe(qp, 0);
  151. if (ret < 0) {
  152. rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
  153. goto bail_unlock;
  154. }
  155. if (!ret) {
  156. if (qp->ibqp.qp_num == 0)
  157. ibp->rvp.n_vl15_dropped++;
  158. goto bail_unlock;
  159. }
  160. }
  161. /* Silently drop packets which are too big. */
  162. if (unlikely(wc.byte_len > qp->r_len)) {
  163. qp->r_flags |= RVT_R_REUSE_SGE;
  164. ibp->rvp.n_pkt_drops++;
  165. goto bail_unlock;
  166. }
  167. if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
  168. struct ib_grh grh;
  169. const struct ib_global_route *grd = rdma_ah_read_grh(ah_attr);
  170. hfi1_make_grh(ibp, &grh, grd, 0, 0);
  171. hfi1_copy_sge(&qp->r_sge, &grh,
  172. sizeof(grh), true, false);
  173. wc.wc_flags |= IB_WC_GRH;
  174. } else {
  175. rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
  176. }
  177. ssge.sg_list = swqe->sg_list + 1;
  178. ssge.sge = *swqe->sg_list;
  179. ssge.num_sge = swqe->wr.num_sge;
  180. sge = &ssge.sge;
  181. while (length) {
  182. u32 len = sge->length;
  183. if (len > length)
  184. len = length;
  185. if (len > sge->sge_length)
  186. len = sge->sge_length;
  187. WARN_ON_ONCE(len == 0);
  188. hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, true, false);
  189. sge->vaddr += len;
  190. sge->length -= len;
  191. sge->sge_length -= len;
  192. if (sge->sge_length == 0) {
  193. if (--ssge.num_sge)
  194. *sge = *ssge.sg_list++;
  195. } else if (sge->length == 0 && sge->mr->lkey) {
  196. if (++sge->n >= RVT_SEGSZ) {
  197. if (++sge->m >= sge->mr->mapsz)
  198. break;
  199. sge->n = 0;
  200. }
  201. sge->vaddr =
  202. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  203. sge->length =
  204. sge->mr->map[sge->m]->segs[sge->n].length;
  205. }
  206. length -= len;
  207. }
  208. rvt_put_ss(&qp->r_sge);
  209. if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
  210. goto bail_unlock;
  211. wc.wr_id = qp->r_wr_id;
  212. wc.status = IB_WC_SUCCESS;
  213. wc.opcode = IB_WC_RECV;
  214. wc.qp = &qp->ibqp;
  215. wc.src_qp = sqp->ibqp.qp_num;
  216. if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI) {
  217. if (sqp->ibqp.qp_type == IB_QPT_GSI ||
  218. sqp->ibqp.qp_type == IB_QPT_SMI)
  219. wc.pkey_index = swqe->ud_wr.pkey_index;
  220. else
  221. wc.pkey_index = sqp->s_pkey_index;
  222. } else {
  223. wc.pkey_index = 0;
  224. }
  225. wc.slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
  226. ((1 << ppd->lmc) - 1));
  227. /* Check for loopback when the port lid is not set */
  228. if (wc.slid == 0 && sqp->ibqp.qp_type == IB_QPT_GSI)
  229. wc.slid = be16_to_cpu(IB_LID_PERMISSIVE);
  230. wc.sl = rdma_ah_get_sl(ah_attr);
  231. wc.dlid_path_bits = rdma_ah_get_dlid(ah_attr) & ((1 << ppd->lmc) - 1);
  232. wc.port_num = qp->port_num;
  233. /* Signal completion event if the solicited bit is set. */
  234. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
  235. swqe->wr.send_flags & IB_SEND_SOLICITED);
  236. ibp->rvp.n_loop_pkts++;
  237. bail_unlock:
  238. spin_unlock_irqrestore(&qp->r_lock, flags);
  239. drop:
  240. rcu_read_unlock();
  241. }
  242. /**
  243. * hfi1_make_ud_req - construct a UD request packet
  244. * @qp: the QP
  245. *
  246. * Assume s_lock is held.
  247. *
  248. * Return 1 if constructed; otherwise, return 0.
  249. */
  250. int hfi1_make_ud_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
  251. {
  252. struct hfi1_qp_priv *priv = qp->priv;
  253. struct ib_other_headers *ohdr;
  254. struct rdma_ah_attr *ah_attr;
  255. struct hfi1_pportdata *ppd;
  256. struct hfi1_ibport *ibp;
  257. struct rvt_swqe *wqe;
  258. u32 nwords;
  259. u32 extra_bytes;
  260. u32 bth0;
  261. u16 lrh0;
  262. u16 lid;
  263. int next_cur;
  264. u8 sc5;
  265. ps->s_txreq = get_txreq(ps->dev, qp);
  266. if (IS_ERR(ps->s_txreq))
  267. goto bail_no_tx;
  268. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
  269. if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
  270. goto bail;
  271. /* We are in the error state, flush the work request. */
  272. smp_read_barrier_depends(); /* see post_one_send */
  273. if (qp->s_last == ACCESS_ONCE(qp->s_head))
  274. goto bail;
  275. /* If DMAs are in progress, we can't flush immediately. */
  276. if (iowait_sdma_pending(&priv->s_iowait)) {
  277. qp->s_flags |= RVT_S_WAIT_DMA;
  278. goto bail;
  279. }
  280. wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  281. hfi1_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
  282. goto done_free_tx;
  283. }
  284. /* see post_one_send() */
  285. smp_read_barrier_depends();
  286. if (qp->s_cur == ACCESS_ONCE(qp->s_head))
  287. goto bail;
  288. wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
  289. next_cur = qp->s_cur + 1;
  290. if (next_cur >= qp->s_size)
  291. next_cur = 0;
  292. /* Construct the header. */
  293. ibp = to_iport(qp->ibqp.device, qp->port_num);
  294. ppd = ppd_from_ibp(ibp);
  295. ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
  296. if (rdma_ah_get_dlid(ah_attr) < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
  297. rdma_ah_get_dlid(ah_attr) == be16_to_cpu(IB_LID_PERMISSIVE)) {
  298. lid = rdma_ah_get_dlid(ah_attr) & ~((1 << ppd->lmc) - 1);
  299. if (unlikely(!loopback &&
  300. (lid == ppd->lid ||
  301. (lid == be16_to_cpu(IB_LID_PERMISSIVE) &&
  302. qp->ibqp.qp_type == IB_QPT_GSI)))) {
  303. unsigned long tflags = ps->flags;
  304. /*
  305. * If DMAs are in progress, we can't generate
  306. * a completion for the loopback packet since
  307. * it would be out of order.
  308. * Instead of waiting, we could queue a
  309. * zero length descriptor so we get a callback.
  310. */
  311. if (iowait_sdma_pending(&priv->s_iowait)) {
  312. qp->s_flags |= RVT_S_WAIT_DMA;
  313. goto bail;
  314. }
  315. qp->s_cur = next_cur;
  316. spin_unlock_irqrestore(&qp->s_lock, tflags);
  317. ud_loopback(qp, wqe);
  318. spin_lock_irqsave(&qp->s_lock, tflags);
  319. ps->flags = tflags;
  320. hfi1_send_complete(qp, wqe, IB_WC_SUCCESS);
  321. goto done_free_tx;
  322. }
  323. }
  324. qp->s_cur = next_cur;
  325. extra_bytes = -wqe->length & 3;
  326. nwords = (wqe->length + extra_bytes) >> 2;
  327. /* header size in 32-bit words LRH+BTH+DETH = (8+12+8)/4. */
  328. qp->s_hdrwords = 7;
  329. ps->s_txreq->s_cur_size = wqe->length;
  330. ps->s_txreq->ss = &qp->s_sge;
  331. qp->s_srate = rdma_ah_get_static_rate(ah_attr);
  332. qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
  333. qp->s_wqe = wqe;
  334. qp->s_sge.sge = wqe->sg_list[0];
  335. qp->s_sge.sg_list = wqe->sg_list + 1;
  336. qp->s_sge.num_sge = wqe->wr.num_sge;
  337. qp->s_sge.total_len = wqe->length;
  338. if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
  339. /* Header size in 32-bit words. */
  340. qp->s_hdrwords += hfi1_make_grh(ibp,
  341. &ps->s_txreq->phdr.hdr.u.l.grh,
  342. rdma_ah_read_grh(ah_attr),
  343. qp->s_hdrwords, nwords);
  344. lrh0 = HFI1_LRH_GRH;
  345. ohdr = &ps->s_txreq->phdr.hdr.u.l.oth;
  346. /*
  347. * Don't worry about sending to locally attached multicast
  348. * QPs. It is unspecified by the spec. what happens.
  349. */
  350. } else {
  351. /* Header size in 32-bit words. */
  352. lrh0 = HFI1_LRH_BTH;
  353. ohdr = &ps->s_txreq->phdr.hdr.u.oth;
  354. }
  355. if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
  356. qp->s_hdrwords++;
  357. ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
  358. bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
  359. } else {
  360. bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
  361. }
  362. sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
  363. lrh0 |= (rdma_ah_get_sl(ah_attr) & 0xf) << 4;
  364. if (qp->ibqp.qp_type == IB_QPT_SMI) {
  365. lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
  366. priv->s_sc = 0xf;
  367. } else {
  368. lrh0 |= (sc5 & 0xf) << 12;
  369. priv->s_sc = sc5;
  370. }
  371. priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
  372. ps->s_txreq->sde = priv->s_sde;
  373. priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
  374. ps->s_txreq->psc = priv->s_sendcontext;
  375. ps->s_txreq->phdr.hdr.lrh[0] = cpu_to_be16(lrh0);
  376. ps->s_txreq->phdr.hdr.lrh[1] =
  377. cpu_to_be16(rdma_ah_get_dlid(ah_attr));
  378. ps->s_txreq->phdr.hdr.lrh[2] =
  379. cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
  380. if (rdma_ah_get_dlid(ah_attr) == be16_to_cpu(IB_LID_PERMISSIVE)) {
  381. ps->s_txreq->phdr.hdr.lrh[3] = IB_LID_PERMISSIVE;
  382. } else {
  383. lid = ppd->lid;
  384. if (lid) {
  385. lid |= rdma_ah_get_path_bits(ah_attr) &
  386. ((1 << ppd->lmc) - 1);
  387. ps->s_txreq->phdr.hdr.lrh[3] = cpu_to_be16(lid);
  388. } else {
  389. ps->s_txreq->phdr.hdr.lrh[3] = IB_LID_PERMISSIVE;
  390. }
  391. }
  392. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  393. bth0 |= IB_BTH_SOLICITED;
  394. bth0 |= extra_bytes << 20;
  395. if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI)
  396. bth0 |= hfi1_get_pkey(ibp, wqe->ud_wr.pkey_index);
  397. else
  398. bth0 |= hfi1_get_pkey(ibp, qp->s_pkey_index);
  399. ohdr->bth[0] = cpu_to_be32(bth0);
  400. ohdr->bth[1] = cpu_to_be32(wqe->ud_wr.remote_qpn);
  401. ohdr->bth[2] = cpu_to_be32(mask_psn(wqe->psn));
  402. /*
  403. * Qkeys with the high order bit set mean use the
  404. * qkey from the QP context instead of the WR (see 10.2.5).
  405. */
  406. ohdr->u.ud.deth[0] = cpu_to_be32((int)wqe->ud_wr.remote_qkey < 0 ?
  407. qp->qkey : wqe->ud_wr.remote_qkey);
  408. ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
  409. /* disarm any ahg */
  410. priv->s_ahg->ahgcount = 0;
  411. priv->s_ahg->ahgidx = 0;
  412. priv->s_ahg->tx_flags = 0;
  413. /* pbc */
  414. ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
  415. return 1;
  416. done_free_tx:
  417. hfi1_put_txreq(ps->s_txreq);
  418. ps->s_txreq = NULL;
  419. return 1;
  420. bail:
  421. hfi1_put_txreq(ps->s_txreq);
  422. bail_no_tx:
  423. ps->s_txreq = NULL;
  424. qp->s_flags &= ~RVT_S_BUSY;
  425. qp->s_hdrwords = 0;
  426. return 0;
  427. }
  428. /*
  429. * Hardware can't check this so we do it here.
  430. *
  431. * This is a slightly different algorithm than the standard pkey check. It
  432. * special cases the management keys and allows for 0x7fff and 0xffff to be in
  433. * the table at the same time.
  434. *
  435. * @returns the index found or -1 if not found
  436. */
  437. int hfi1_lookup_pkey_idx(struct hfi1_ibport *ibp, u16 pkey)
  438. {
  439. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  440. unsigned i;
  441. if (pkey == FULL_MGMT_P_KEY || pkey == LIM_MGMT_P_KEY) {
  442. unsigned lim_idx = -1;
  443. for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i) {
  444. /* here we look for an exact match */
  445. if (ppd->pkeys[i] == pkey)
  446. return i;
  447. if (ppd->pkeys[i] == LIM_MGMT_P_KEY)
  448. lim_idx = i;
  449. }
  450. /* did not find 0xffff return 0x7fff idx if found */
  451. if (pkey == FULL_MGMT_P_KEY)
  452. return lim_idx;
  453. /* no match... */
  454. return -1;
  455. }
  456. pkey &= 0x7fff; /* remove limited/full membership bit */
  457. for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i)
  458. if ((ppd->pkeys[i] & 0x7fff) == pkey)
  459. return i;
  460. /*
  461. * Should not get here, this means hardware failed to validate pkeys.
  462. */
  463. return -1;
  464. }
  465. void return_cnp(struct hfi1_ibport *ibp, struct rvt_qp *qp, u32 remote_qpn,
  466. u32 pkey, u32 slid, u32 dlid, u8 sc5,
  467. const struct ib_grh *old_grh)
  468. {
  469. u64 pbc, pbc_flags = 0;
  470. u32 bth0, plen, vl, hwords = 5;
  471. u16 lrh0;
  472. u8 sl = ibp->sc_to_sl[sc5];
  473. struct ib_header hdr;
  474. struct ib_other_headers *ohdr;
  475. struct pio_buf *pbuf;
  476. struct send_context *ctxt = qp_to_send_context(qp, sc5);
  477. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  478. if (old_grh) {
  479. struct ib_grh *grh = &hdr.u.l.grh;
  480. grh->version_tclass_flow = old_grh->version_tclass_flow;
  481. grh->paylen = cpu_to_be16((hwords - 2 + SIZE_OF_CRC) << 2);
  482. grh->hop_limit = 0xff;
  483. grh->sgid = old_grh->dgid;
  484. grh->dgid = old_grh->sgid;
  485. ohdr = &hdr.u.l.oth;
  486. lrh0 = HFI1_LRH_GRH;
  487. hwords += sizeof(struct ib_grh) / sizeof(u32);
  488. } else {
  489. ohdr = &hdr.u.oth;
  490. lrh0 = HFI1_LRH_BTH;
  491. }
  492. lrh0 |= (sc5 & 0xf) << 12 | sl << 4;
  493. bth0 = pkey | (IB_OPCODE_CNP << 24);
  494. ohdr->bth[0] = cpu_to_be32(bth0);
  495. ohdr->bth[1] = cpu_to_be32(remote_qpn | (1 << IB_BECN_SHIFT));
  496. ohdr->bth[2] = 0; /* PSN 0 */
  497. hdr.lrh[0] = cpu_to_be16(lrh0);
  498. hdr.lrh[1] = cpu_to_be16(dlid);
  499. hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
  500. hdr.lrh[3] = cpu_to_be16(slid);
  501. plen = 2 /* PBC */ + hwords;
  502. pbc_flags |= (!!(sc5 & 0x10)) << PBC_DC_INFO_SHIFT;
  503. vl = sc_to_vlt(ppd->dd, sc5);
  504. pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
  505. if (ctxt) {
  506. pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
  507. if (pbuf)
  508. ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
  509. &hdr, hwords);
  510. }
  511. }
  512. /*
  513. * opa_smp_check() - Do the regular pkey checking, and the additional
  514. * checks for SMPs specified in OPAv1 rev 1.0, 9/19/2016 update, section
  515. * 9.10.25 ("SMA Packet Checks").
  516. *
  517. * Note that:
  518. * - Checks are done using the pkey directly from the packet's BTH,
  519. * and specifically _not_ the pkey that we attach to the completion,
  520. * which may be different.
  521. * - These checks are specifically for "non-local" SMPs (i.e., SMPs
  522. * which originated on another node). SMPs which are sent from, and
  523. * destined to this node are checked in opa_local_smp_check().
  524. *
  525. * At the point where opa_smp_check() is called, we know:
  526. * - destination QP is QP0
  527. *
  528. * opa_smp_check() returns 0 if all checks succeed, 1 otherwise.
  529. */
  530. static int opa_smp_check(struct hfi1_ibport *ibp, u16 pkey, u8 sc5,
  531. struct rvt_qp *qp, u16 slid, struct opa_smp *smp)
  532. {
  533. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  534. /*
  535. * I don't think it's possible for us to get here with sc != 0xf,
  536. * but check it to be certain.
  537. */
  538. if (sc5 != 0xf)
  539. return 1;
  540. if (rcv_pkey_check(ppd, pkey, sc5, slid))
  541. return 1;
  542. /*
  543. * At this point we know (and so don't need to check again) that
  544. * the pkey is either LIM_MGMT_P_KEY, or FULL_MGMT_P_KEY
  545. * (see ingress_pkey_check).
  546. */
  547. if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE &&
  548. smp->mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED) {
  549. ingress_pkey_table_fail(ppd, pkey, slid);
  550. return 1;
  551. }
  552. /*
  553. * SMPs fall into one of four (disjoint) categories:
  554. * SMA request, SMA response, SMA trap, or SMA trap repress.
  555. * Our response depends, in part, on which type of SMP we're
  556. * processing.
  557. *
  558. * If this is an SMA response, skip the check here.
  559. *
  560. * If this is an SMA request or SMA trap repress:
  561. * - pkey != FULL_MGMT_P_KEY =>
  562. * increment port recv constraint errors, drop MAD
  563. *
  564. * Otherwise:
  565. * - accept if the port is running an SM
  566. * - drop MAD if it's an SMA trap
  567. * - pkey == FULL_MGMT_P_KEY =>
  568. * reply with unsupported method
  569. * - pkey != FULL_MGMT_P_KEY =>
  570. * increment port recv constraint errors, drop MAD
  571. */
  572. switch (smp->method) {
  573. case IB_MGMT_METHOD_GET_RESP:
  574. case IB_MGMT_METHOD_REPORT_RESP:
  575. break;
  576. case IB_MGMT_METHOD_GET:
  577. case IB_MGMT_METHOD_SET:
  578. case IB_MGMT_METHOD_REPORT:
  579. case IB_MGMT_METHOD_TRAP_REPRESS:
  580. if (pkey != FULL_MGMT_P_KEY) {
  581. ingress_pkey_table_fail(ppd, pkey, slid);
  582. return 1;
  583. }
  584. break;
  585. default:
  586. if (ibp->rvp.port_cap_flags & IB_PORT_SM)
  587. return 0;
  588. if (smp->method == IB_MGMT_METHOD_TRAP)
  589. return 1;
  590. if (pkey == FULL_MGMT_P_KEY) {
  591. smp->status |= IB_SMP_UNSUP_METHOD;
  592. return 0;
  593. }
  594. ingress_pkey_table_fail(ppd, pkey, slid);
  595. return 1;
  596. }
  597. return 0;
  598. }
  599. /**
  600. * hfi1_ud_rcv - receive an incoming UD packet
  601. * @ibp: the port the packet came in on
  602. * @hdr: the packet header
  603. * @rcv_flags: flags relevant to rcv processing
  604. * @data: the packet data
  605. * @tlen: the packet length
  606. * @qp: the QP the packet came on
  607. *
  608. * This is called from qp_rcv() to process an incoming UD packet
  609. * for the given QP.
  610. * Called at interrupt level.
  611. */
  612. void hfi1_ud_rcv(struct hfi1_packet *packet)
  613. {
  614. struct ib_other_headers *ohdr = packet->ohdr;
  615. int opcode;
  616. u32 hdrsize = packet->hlen;
  617. struct ib_wc wc;
  618. u32 qkey;
  619. u32 src_qp;
  620. u16 dlid, pkey;
  621. int mgmt_pkey_idx = -1;
  622. struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
  623. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  624. struct ib_header *hdr = packet->hdr;
  625. u32 rcv_flags = packet->rcv_flags;
  626. void *data = packet->ebuf;
  627. u32 tlen = packet->tlen;
  628. struct rvt_qp *qp = packet->qp;
  629. bool has_grh = rcv_flags & HFI1_HAS_GRH;
  630. u8 sc5 = hfi1_9B_get_sc5(hdr, packet->rhf);
  631. u32 bth1;
  632. u8 sl_from_sc, sl;
  633. u16 slid;
  634. u8 extra_bytes;
  635. qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
  636. src_qp = be32_to_cpu(ohdr->u.ud.deth[1]) & RVT_QPN_MASK;
  637. dlid = ib_get_dlid(hdr);
  638. bth1 = be32_to_cpu(ohdr->bth[1]);
  639. slid = ib_get_slid(hdr);
  640. pkey = ib_bth_get_pkey(ohdr);
  641. opcode = ib_bth_get_opcode(ohdr);
  642. sl = ib_get_sl(hdr);
  643. extra_bytes = ib_bth_get_pad(ohdr);
  644. extra_bytes += (SIZE_OF_CRC << 2);
  645. sl_from_sc = ibp->sc_to_sl[sc5];
  646. process_ecn(qp, packet, (opcode != IB_OPCODE_CNP));
  647. /*
  648. * Get the number of bytes the message was padded by
  649. * and drop incomplete packets.
  650. */
  651. if (unlikely(tlen < (hdrsize + extra_bytes)))
  652. goto drop;
  653. tlen -= hdrsize + extra_bytes;
  654. /*
  655. * Check that the permissive LID is only used on QP0
  656. * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
  657. */
  658. if (qp->ibqp.qp_num) {
  659. if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||
  660. hdr->lrh[3] == IB_LID_PERMISSIVE))
  661. goto drop;
  662. if (qp->ibqp.qp_num > 1) {
  663. if (unlikely(rcv_pkey_check(ppd, pkey, sc5, slid))) {
  664. /*
  665. * Traps will not be sent for packets dropped
  666. * by the HW. This is fine, as sending trap
  667. * for invalid pkeys is optional according to
  668. * IB spec (release 1.3, section 10.9.4)
  669. */
  670. hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY,
  671. pkey, sl,
  672. src_qp, qp->ibqp.qp_num,
  673. slid, dlid);
  674. return;
  675. }
  676. } else {
  677. /* GSI packet */
  678. mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
  679. if (mgmt_pkey_idx < 0)
  680. goto drop;
  681. }
  682. if (unlikely(qkey != qp->qkey)) {
  683. hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_Q_KEY, qkey, sl,
  684. src_qp, qp->ibqp.qp_num,
  685. slid, dlid);
  686. return;
  687. }
  688. /* Drop invalid MAD packets (see 13.5.3.1). */
  689. if (unlikely(qp->ibqp.qp_num == 1 &&
  690. (tlen > 2048 || (sc5 == 0xF))))
  691. goto drop;
  692. } else {
  693. /* Received on QP0, and so by definition, this is an SMP */
  694. struct opa_smp *smp = (struct opa_smp *)data;
  695. if (opa_smp_check(ibp, pkey, sc5, qp, slid, smp))
  696. goto drop;
  697. if (tlen > 2048)
  698. goto drop;
  699. if ((hdr->lrh[1] == IB_LID_PERMISSIVE ||
  700. hdr->lrh[3] == IB_LID_PERMISSIVE) &&
  701. smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  702. goto drop;
  703. /* look up SMI pkey */
  704. mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
  705. if (mgmt_pkey_idx < 0)
  706. goto drop;
  707. }
  708. if (qp->ibqp.qp_num > 1 &&
  709. opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
  710. wc.ex.imm_data = ohdr->u.ud.imm_data;
  711. wc.wc_flags = IB_WC_WITH_IMM;
  712. tlen -= sizeof(u32);
  713. } else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
  714. wc.ex.imm_data = 0;
  715. wc.wc_flags = 0;
  716. } else {
  717. goto drop;
  718. }
  719. /*
  720. * A GRH is expected to precede the data even if not
  721. * present on the wire.
  722. */
  723. wc.byte_len = tlen + sizeof(struct ib_grh);
  724. /*
  725. * Get the next work request entry to find where to put the data.
  726. */
  727. if (qp->r_flags & RVT_R_REUSE_SGE) {
  728. qp->r_flags &= ~RVT_R_REUSE_SGE;
  729. } else {
  730. int ret;
  731. ret = hfi1_rvt_get_rwqe(qp, 0);
  732. if (ret < 0) {
  733. rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
  734. return;
  735. }
  736. if (!ret) {
  737. if (qp->ibqp.qp_num == 0)
  738. ibp->rvp.n_vl15_dropped++;
  739. return;
  740. }
  741. }
  742. /* Silently drop packets which are too big. */
  743. if (unlikely(wc.byte_len > qp->r_len)) {
  744. qp->r_flags |= RVT_R_REUSE_SGE;
  745. goto drop;
  746. }
  747. if (has_grh) {
  748. hfi1_copy_sge(&qp->r_sge, &hdr->u.l.grh,
  749. sizeof(struct ib_grh), true, false);
  750. wc.wc_flags |= IB_WC_GRH;
  751. } else {
  752. rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
  753. }
  754. hfi1_copy_sge(&qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh),
  755. true, false);
  756. rvt_put_ss(&qp->r_sge);
  757. if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
  758. return;
  759. wc.wr_id = qp->r_wr_id;
  760. wc.status = IB_WC_SUCCESS;
  761. wc.opcode = IB_WC_RECV;
  762. wc.vendor_err = 0;
  763. wc.qp = &qp->ibqp;
  764. wc.src_qp = src_qp;
  765. if (qp->ibqp.qp_type == IB_QPT_GSI ||
  766. qp->ibqp.qp_type == IB_QPT_SMI) {
  767. if (mgmt_pkey_idx < 0) {
  768. if (net_ratelimit()) {
  769. struct hfi1_devdata *dd = ppd->dd;
  770. dd_dev_err(dd, "QP type %d mgmt_pkey_idx < 0 and packet not dropped???\n",
  771. qp->ibqp.qp_type);
  772. mgmt_pkey_idx = 0;
  773. }
  774. }
  775. wc.pkey_index = (unsigned)mgmt_pkey_idx;
  776. } else {
  777. wc.pkey_index = 0;
  778. }
  779. wc.slid = slid;
  780. wc.sl = sl_from_sc;
  781. /*
  782. * Save the LMC lower bits if the destination LID is a unicast LID.
  783. */
  784. wc.dlid_path_bits = dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE) ? 0 :
  785. dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
  786. wc.port_num = qp->port_num;
  787. /* Signal completion event if the solicited bit is set. */
  788. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
  789. (ohdr->bth[0] &
  790. cpu_to_be32(IB_BTH_SOLICITED)) != 0);
  791. return;
  792. drop:
  793. ibp->rvp.n_pkt_drops++;
  794. }