ud.c 24 KB

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