ud.c 24 KB

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