18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright(c) 2015 - 2019 Intel Corporation.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This file is provided under a dual BSD/GPLv2 license.  When using or
58c2ecf20Sopenharmony_ci * redistributing this file, you may do so under either license.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * GPL LICENSE SUMMARY
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
108c2ecf20Sopenharmony_ci * it under the terms of version 2 of the GNU General Public License as
118c2ecf20Sopenharmony_ci * published by the Free Software Foundation.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, but
148c2ecf20Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
158c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
168c2ecf20Sopenharmony_ci * General Public License for more details.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * BSD LICENSE
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
218c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
228c2ecf20Sopenharmony_ci * are met:
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci *  - Redistributions of source code must retain the above copyright
258c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
268c2ecf20Sopenharmony_ci *  - Redistributions in binary form must reproduce the above copyright
278c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in
288c2ecf20Sopenharmony_ci *    the documentation and/or other materials provided with the
298c2ecf20Sopenharmony_ci *    distribution.
308c2ecf20Sopenharmony_ci *  - Neither the name of Intel Corporation nor the names of its
318c2ecf20Sopenharmony_ci *    contributors may be used to endorse or promote products derived
328c2ecf20Sopenharmony_ci *    from this software without specific prior written permission.
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
358c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
368c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
378c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
388c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
398c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
408c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
418c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
428c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
438c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
448c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#include <linux/net.h>
498c2ecf20Sopenharmony_ci#include <rdma/ib_smi.h>
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#include "hfi.h"
528c2ecf20Sopenharmony_ci#include "mad.h"
538c2ecf20Sopenharmony_ci#include "verbs_txreq.h"
548c2ecf20Sopenharmony_ci#include "trace_ibhdrs.h"
558c2ecf20Sopenharmony_ci#include "qp.h"
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/* We support only two types - 9B and 16B for now */
588c2ecf20Sopenharmony_cistatic const hfi1_make_req hfi1_make_ud_req_tbl[2] = {
598c2ecf20Sopenharmony_ci	[HFI1_PKT_TYPE_9B] = &hfi1_make_ud_req_9B,
608c2ecf20Sopenharmony_ci	[HFI1_PKT_TYPE_16B] = &hfi1_make_ud_req_16B
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/**
648c2ecf20Sopenharmony_ci * ud_loopback - handle send on loopback QPs
658c2ecf20Sopenharmony_ci * @sqp: the sending QP
668c2ecf20Sopenharmony_ci * @swqe: the send work request
678c2ecf20Sopenharmony_ci *
688c2ecf20Sopenharmony_ci * This is called from hfi1_make_ud_req() to forward a WQE addressed
698c2ecf20Sopenharmony_ci * to the same HFI.
708c2ecf20Sopenharmony_ci * Note that the receive interrupt handler may be calling hfi1_ud_rcv()
718c2ecf20Sopenharmony_ci * while this is being called.
728c2ecf20Sopenharmony_ci */
738c2ecf20Sopenharmony_cistatic void ud_loopback(struct rvt_qp *sqp, struct rvt_swqe *swqe)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
768c2ecf20Sopenharmony_ci	struct hfi1_pportdata *ppd;
778c2ecf20Sopenharmony_ci	struct hfi1_qp_priv *priv = sqp->priv;
788c2ecf20Sopenharmony_ci	struct rvt_qp *qp;
798c2ecf20Sopenharmony_ci	struct rdma_ah_attr *ah_attr;
808c2ecf20Sopenharmony_ci	unsigned long flags;
818c2ecf20Sopenharmony_ci	struct rvt_sge_state ssge;
828c2ecf20Sopenharmony_ci	struct rvt_sge *sge;
838c2ecf20Sopenharmony_ci	struct ib_wc wc;
848c2ecf20Sopenharmony_ci	u32 length;
858c2ecf20Sopenharmony_ci	enum ib_qp_type sqptype, dqptype;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	rcu_read_lock();
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
908c2ecf20Sopenharmony_ci			    rvt_get_swqe_remote_qpn(swqe));
918c2ecf20Sopenharmony_ci	if (!qp) {
928c2ecf20Sopenharmony_ci		ibp->rvp.n_pkt_drops++;
938c2ecf20Sopenharmony_ci		rcu_read_unlock();
948c2ecf20Sopenharmony_ci		return;
958c2ecf20Sopenharmony_ci	}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	sqptype = sqp->ibqp.qp_type == IB_QPT_GSI ?
988c2ecf20Sopenharmony_ci			IB_QPT_UD : sqp->ibqp.qp_type;
998c2ecf20Sopenharmony_ci	dqptype = qp->ibqp.qp_type == IB_QPT_GSI ?
1008c2ecf20Sopenharmony_ci			IB_QPT_UD : qp->ibqp.qp_type;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	if (dqptype != sqptype ||
1038c2ecf20Sopenharmony_ci	    !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
1048c2ecf20Sopenharmony_ci		ibp->rvp.n_pkt_drops++;
1058c2ecf20Sopenharmony_ci		goto drop;
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	ah_attr = rvt_get_swqe_ah_attr(swqe);
1098c2ecf20Sopenharmony_ci	ppd = ppd_from_ibp(ibp);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	if (qp->ibqp.qp_num > 1) {
1128c2ecf20Sopenharmony_ci		u16 pkey;
1138c2ecf20Sopenharmony_ci		u32 slid;
1148c2ecf20Sopenharmony_ci		u8 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci		pkey = hfi1_get_pkey(ibp, sqp->s_pkey_index);
1178c2ecf20Sopenharmony_ci		slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
1188c2ecf20Sopenharmony_ci				   ((1 << ppd->lmc) - 1));
1198c2ecf20Sopenharmony_ci		if (unlikely(ingress_pkey_check(ppd, pkey, sc5,
1208c2ecf20Sopenharmony_ci						qp->s_pkey_index,
1218c2ecf20Sopenharmony_ci						slid, false))) {
1228c2ecf20Sopenharmony_ci			hfi1_bad_pkey(ibp, pkey,
1238c2ecf20Sopenharmony_ci				      rdma_ah_get_sl(ah_attr),
1248c2ecf20Sopenharmony_ci				      sqp->ibqp.qp_num, qp->ibqp.qp_num,
1258c2ecf20Sopenharmony_ci				      slid, rdma_ah_get_dlid(ah_attr));
1268c2ecf20Sopenharmony_ci			goto drop;
1278c2ecf20Sopenharmony_ci		}
1288c2ecf20Sopenharmony_ci	}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	/*
1318c2ecf20Sopenharmony_ci	 * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
1328c2ecf20Sopenharmony_ci	 * Qkeys with the high order bit set mean use the
1338c2ecf20Sopenharmony_ci	 * qkey from the QP context instead of the WR (see 10.2.5).
1348c2ecf20Sopenharmony_ci	 */
1358c2ecf20Sopenharmony_ci	if (qp->ibqp.qp_num) {
1368c2ecf20Sopenharmony_ci		u32 qkey;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci		qkey = (int)rvt_get_swqe_remote_qkey(swqe) < 0 ?
1398c2ecf20Sopenharmony_ci			sqp->qkey : rvt_get_swqe_remote_qkey(swqe);
1408c2ecf20Sopenharmony_ci		if (unlikely(qkey != qp->qkey))
1418c2ecf20Sopenharmony_ci			goto drop; /* silently drop per IBTA spec */
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/*
1458c2ecf20Sopenharmony_ci	 * A GRH is expected to precede the data even if not
1468c2ecf20Sopenharmony_ci	 * present on the wire.
1478c2ecf20Sopenharmony_ci	 */
1488c2ecf20Sopenharmony_ci	length = swqe->length;
1498c2ecf20Sopenharmony_ci	memset(&wc, 0, sizeof(wc));
1508c2ecf20Sopenharmony_ci	wc.byte_len = length + sizeof(struct ib_grh);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
1538c2ecf20Sopenharmony_ci		wc.wc_flags = IB_WC_WITH_IMM;
1548c2ecf20Sopenharmony_ci		wc.ex.imm_data = swqe->wr.ex.imm_data;
1558c2ecf20Sopenharmony_ci	}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	spin_lock_irqsave(&qp->r_lock, flags);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	/*
1608c2ecf20Sopenharmony_ci	 * Get the next work request entry to find where to put the data.
1618c2ecf20Sopenharmony_ci	 */
1628c2ecf20Sopenharmony_ci	if (qp->r_flags & RVT_R_REUSE_SGE) {
1638c2ecf20Sopenharmony_ci		qp->r_flags &= ~RVT_R_REUSE_SGE;
1648c2ecf20Sopenharmony_ci	} else {
1658c2ecf20Sopenharmony_ci		int ret;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci		ret = rvt_get_rwqe(qp, false);
1688c2ecf20Sopenharmony_ci		if (ret < 0) {
1698c2ecf20Sopenharmony_ci			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
1708c2ecf20Sopenharmony_ci			goto bail_unlock;
1718c2ecf20Sopenharmony_ci		}
1728c2ecf20Sopenharmony_ci		if (!ret) {
1738c2ecf20Sopenharmony_ci			if (qp->ibqp.qp_num == 0)
1748c2ecf20Sopenharmony_ci				ibp->rvp.n_vl15_dropped++;
1758c2ecf20Sopenharmony_ci			goto bail_unlock;
1768c2ecf20Sopenharmony_ci		}
1778c2ecf20Sopenharmony_ci	}
1788c2ecf20Sopenharmony_ci	/* Silently drop packets which are too big. */
1798c2ecf20Sopenharmony_ci	if (unlikely(wc.byte_len > qp->r_len)) {
1808c2ecf20Sopenharmony_ci		qp->r_flags |= RVT_R_REUSE_SGE;
1818c2ecf20Sopenharmony_ci		ibp->rvp.n_pkt_drops++;
1828c2ecf20Sopenharmony_ci		goto bail_unlock;
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
1868c2ecf20Sopenharmony_ci		struct ib_grh grh;
1878c2ecf20Sopenharmony_ci		struct ib_global_route grd = *(rdma_ah_read_grh(ah_attr));
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci		/*
1908c2ecf20Sopenharmony_ci		 * For loopback packets with extended LIDs, the
1918c2ecf20Sopenharmony_ci		 * sgid_index in the GRH is 0 and the dgid is
1928c2ecf20Sopenharmony_ci		 * OPA GID of the sender. While creating a response
1938c2ecf20Sopenharmony_ci		 * to the loopback packet, IB core creates the new
1948c2ecf20Sopenharmony_ci		 * sgid_index from the DGID and that will be the
1958c2ecf20Sopenharmony_ci		 * OPA_GID_INDEX. The new dgid is from the sgid
1968c2ecf20Sopenharmony_ci		 * index and that will be in the IB GID format.
1978c2ecf20Sopenharmony_ci		 *
1988c2ecf20Sopenharmony_ci		 * We now have a case where the sent packet had a
1998c2ecf20Sopenharmony_ci		 * different sgid_index and dgid compared to the
2008c2ecf20Sopenharmony_ci		 * one that was received in response.
2018c2ecf20Sopenharmony_ci		 *
2028c2ecf20Sopenharmony_ci		 * Fix this inconsistency.
2038c2ecf20Sopenharmony_ci		 */
2048c2ecf20Sopenharmony_ci		if (priv->hdr_type == HFI1_PKT_TYPE_16B) {
2058c2ecf20Sopenharmony_ci			if (grd.sgid_index == 0)
2068c2ecf20Sopenharmony_ci				grd.sgid_index = OPA_GID_INDEX;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci			if (ib_is_opa_gid(&grd.dgid))
2098c2ecf20Sopenharmony_ci				grd.dgid.global.interface_id =
2108c2ecf20Sopenharmony_ci				cpu_to_be64(ppd->guids[HFI1_PORT_GUID_INDEX]);
2118c2ecf20Sopenharmony_ci		}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci		hfi1_make_grh(ibp, &grh, &grd, 0, 0);
2148c2ecf20Sopenharmony_ci		rvt_copy_sge(qp, &qp->r_sge, &grh,
2158c2ecf20Sopenharmony_ci			     sizeof(grh), true, false);
2168c2ecf20Sopenharmony_ci		wc.wc_flags |= IB_WC_GRH;
2178c2ecf20Sopenharmony_ci	} else {
2188c2ecf20Sopenharmony_ci		rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_ci	ssge.sg_list = swqe->sg_list + 1;
2218c2ecf20Sopenharmony_ci	ssge.sge = *swqe->sg_list;
2228c2ecf20Sopenharmony_ci	ssge.num_sge = swqe->wr.num_sge;
2238c2ecf20Sopenharmony_ci	sge = &ssge.sge;
2248c2ecf20Sopenharmony_ci	while (length) {
2258c2ecf20Sopenharmony_ci		u32 len = rvt_get_sge_length(sge, length);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci		WARN_ON_ONCE(len == 0);
2288c2ecf20Sopenharmony_ci		rvt_copy_sge(qp, &qp->r_sge, sge->vaddr, len, true, false);
2298c2ecf20Sopenharmony_ci		rvt_update_sge(&ssge, len, false);
2308c2ecf20Sopenharmony_ci		length -= len;
2318c2ecf20Sopenharmony_ci	}
2328c2ecf20Sopenharmony_ci	rvt_put_ss(&qp->r_sge);
2338c2ecf20Sopenharmony_ci	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
2348c2ecf20Sopenharmony_ci		goto bail_unlock;
2358c2ecf20Sopenharmony_ci	wc.wr_id = qp->r_wr_id;
2368c2ecf20Sopenharmony_ci	wc.status = IB_WC_SUCCESS;
2378c2ecf20Sopenharmony_ci	wc.opcode = IB_WC_RECV;
2388c2ecf20Sopenharmony_ci	wc.qp = &qp->ibqp;
2398c2ecf20Sopenharmony_ci	wc.src_qp = sqp->ibqp.qp_num;
2408c2ecf20Sopenharmony_ci	if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI) {
2418c2ecf20Sopenharmony_ci		if (sqp->ibqp.qp_type == IB_QPT_GSI ||
2428c2ecf20Sopenharmony_ci		    sqp->ibqp.qp_type == IB_QPT_SMI)
2438c2ecf20Sopenharmony_ci			wc.pkey_index = rvt_get_swqe_pkey_index(swqe);
2448c2ecf20Sopenharmony_ci		else
2458c2ecf20Sopenharmony_ci			wc.pkey_index = sqp->s_pkey_index;
2468c2ecf20Sopenharmony_ci	} else {
2478c2ecf20Sopenharmony_ci		wc.pkey_index = 0;
2488c2ecf20Sopenharmony_ci	}
2498c2ecf20Sopenharmony_ci	wc.slid = (ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
2508c2ecf20Sopenharmony_ci				   ((1 << ppd->lmc) - 1))) & U16_MAX;
2518c2ecf20Sopenharmony_ci	/* Check for loopback when the port lid is not set */
2528c2ecf20Sopenharmony_ci	if (wc.slid == 0 && sqp->ibqp.qp_type == IB_QPT_GSI)
2538c2ecf20Sopenharmony_ci		wc.slid = be16_to_cpu(IB_LID_PERMISSIVE);
2548c2ecf20Sopenharmony_ci	wc.sl = rdma_ah_get_sl(ah_attr);
2558c2ecf20Sopenharmony_ci	wc.dlid_path_bits = rdma_ah_get_dlid(ah_attr) & ((1 << ppd->lmc) - 1);
2568c2ecf20Sopenharmony_ci	wc.port_num = qp->port_num;
2578c2ecf20Sopenharmony_ci	/* Signal completion event if the solicited bit is set. */
2588c2ecf20Sopenharmony_ci	rvt_recv_cq(qp, &wc, swqe->wr.send_flags & IB_SEND_SOLICITED);
2598c2ecf20Sopenharmony_ci	ibp->rvp.n_loop_pkts++;
2608c2ecf20Sopenharmony_cibail_unlock:
2618c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&qp->r_lock, flags);
2628c2ecf20Sopenharmony_cidrop:
2638c2ecf20Sopenharmony_ci	rcu_read_unlock();
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic void hfi1_make_bth_deth(struct rvt_qp *qp, struct rvt_swqe *wqe,
2678c2ecf20Sopenharmony_ci			       struct ib_other_headers *ohdr,
2688c2ecf20Sopenharmony_ci			       u16 *pkey, u32 extra_bytes, bool bypass)
2698c2ecf20Sopenharmony_ci{
2708c2ecf20Sopenharmony_ci	u32 bth0;
2718c2ecf20Sopenharmony_ci	struct hfi1_ibport *ibp;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	ibp = to_iport(qp->ibqp.device, qp->port_num);
2748c2ecf20Sopenharmony_ci	if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
2758c2ecf20Sopenharmony_ci		ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
2768c2ecf20Sopenharmony_ci		bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
2778c2ecf20Sopenharmony_ci	} else {
2788c2ecf20Sopenharmony_ci		bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
2798c2ecf20Sopenharmony_ci	}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	if (wqe->wr.send_flags & IB_SEND_SOLICITED)
2828c2ecf20Sopenharmony_ci		bth0 |= IB_BTH_SOLICITED;
2838c2ecf20Sopenharmony_ci	bth0 |= extra_bytes << 20;
2848c2ecf20Sopenharmony_ci	if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI)
2858c2ecf20Sopenharmony_ci		*pkey = hfi1_get_pkey(ibp, rvt_get_swqe_pkey_index(wqe));
2868c2ecf20Sopenharmony_ci	else
2878c2ecf20Sopenharmony_ci		*pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
2888c2ecf20Sopenharmony_ci	if (!bypass)
2898c2ecf20Sopenharmony_ci		bth0 |= *pkey;
2908c2ecf20Sopenharmony_ci	ohdr->bth[0] = cpu_to_be32(bth0);
2918c2ecf20Sopenharmony_ci	ohdr->bth[1] = cpu_to_be32(rvt_get_swqe_remote_qpn(wqe));
2928c2ecf20Sopenharmony_ci	ohdr->bth[2] = cpu_to_be32(mask_psn(wqe->psn));
2938c2ecf20Sopenharmony_ci	/*
2948c2ecf20Sopenharmony_ci	 * Qkeys with the high order bit set mean use the
2958c2ecf20Sopenharmony_ci	 * qkey from the QP context instead of the WR (see 10.2.5).
2968c2ecf20Sopenharmony_ci	 */
2978c2ecf20Sopenharmony_ci	ohdr->u.ud.deth[0] =
2988c2ecf20Sopenharmony_ci		cpu_to_be32((int)rvt_get_swqe_remote_qkey(wqe) < 0 ? qp->qkey :
2998c2ecf20Sopenharmony_ci			    rvt_get_swqe_remote_qkey(wqe));
3008c2ecf20Sopenharmony_ci	ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_civoid hfi1_make_ud_req_9B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
3048c2ecf20Sopenharmony_ci			 struct rvt_swqe *wqe)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	u32 nwords, extra_bytes;
3078c2ecf20Sopenharmony_ci	u16 len, slid, dlid, pkey;
3088c2ecf20Sopenharmony_ci	u16 lrh0 = 0;
3098c2ecf20Sopenharmony_ci	u8 sc5;
3108c2ecf20Sopenharmony_ci	struct hfi1_qp_priv *priv = qp->priv;
3118c2ecf20Sopenharmony_ci	struct ib_other_headers *ohdr;
3128c2ecf20Sopenharmony_ci	struct rdma_ah_attr *ah_attr;
3138c2ecf20Sopenharmony_ci	struct hfi1_pportdata *ppd;
3148c2ecf20Sopenharmony_ci	struct hfi1_ibport *ibp;
3158c2ecf20Sopenharmony_ci	struct ib_grh *grh;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	ibp = to_iport(qp->ibqp.device, qp->port_num);
3188c2ecf20Sopenharmony_ci	ppd = ppd_from_ibp(ibp);
3198c2ecf20Sopenharmony_ci	ah_attr = rvt_get_swqe_ah_attr(wqe);
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	extra_bytes = -wqe->length & 3;
3228c2ecf20Sopenharmony_ci	nwords = ((wqe->length + extra_bytes) >> 2) + SIZE_OF_CRC;
3238c2ecf20Sopenharmony_ci	/* header size in dwords LRH+BTH+DETH = (8+12+8)/4. */
3248c2ecf20Sopenharmony_ci	ps->s_txreq->hdr_dwords = 7;
3258c2ecf20Sopenharmony_ci	if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
3268c2ecf20Sopenharmony_ci		ps->s_txreq->hdr_dwords++;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
3298c2ecf20Sopenharmony_ci		grh = &ps->s_txreq->phdr.hdr.ibh.u.l.grh;
3308c2ecf20Sopenharmony_ci		ps->s_txreq->hdr_dwords +=
3318c2ecf20Sopenharmony_ci			hfi1_make_grh(ibp, grh, rdma_ah_read_grh(ah_attr),
3328c2ecf20Sopenharmony_ci				      ps->s_txreq->hdr_dwords - LRH_9B_DWORDS,
3338c2ecf20Sopenharmony_ci				      nwords);
3348c2ecf20Sopenharmony_ci		lrh0 = HFI1_LRH_GRH;
3358c2ecf20Sopenharmony_ci		ohdr = &ps->s_txreq->phdr.hdr.ibh.u.l.oth;
3368c2ecf20Sopenharmony_ci	} else {
3378c2ecf20Sopenharmony_ci		lrh0 = HFI1_LRH_BTH;
3388c2ecf20Sopenharmony_ci		ohdr = &ps->s_txreq->phdr.hdr.ibh.u.oth;
3398c2ecf20Sopenharmony_ci	}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
3428c2ecf20Sopenharmony_ci	lrh0 |= (rdma_ah_get_sl(ah_attr) & 0xf) << 4;
3438c2ecf20Sopenharmony_ci	if (qp->ibqp.qp_type == IB_QPT_SMI) {
3448c2ecf20Sopenharmony_ci		lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
3458c2ecf20Sopenharmony_ci		priv->s_sc = 0xf;
3468c2ecf20Sopenharmony_ci	} else {
3478c2ecf20Sopenharmony_ci		lrh0 |= (sc5 & 0xf) << 12;
3488c2ecf20Sopenharmony_ci		priv->s_sc = sc5;
3498c2ecf20Sopenharmony_ci	}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 9B);
3528c2ecf20Sopenharmony_ci	if (dlid == be16_to_cpu(IB_LID_PERMISSIVE)) {
3538c2ecf20Sopenharmony_ci		slid = be16_to_cpu(IB_LID_PERMISSIVE);
3548c2ecf20Sopenharmony_ci	} else {
3558c2ecf20Sopenharmony_ci		u16 lid = (u16)ppd->lid;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci		if (lid) {
3588c2ecf20Sopenharmony_ci			lid |= rdma_ah_get_path_bits(ah_attr) &
3598c2ecf20Sopenharmony_ci				((1 << ppd->lmc) - 1);
3608c2ecf20Sopenharmony_ci			slid = lid;
3618c2ecf20Sopenharmony_ci		} else {
3628c2ecf20Sopenharmony_ci			slid = be16_to_cpu(IB_LID_PERMISSIVE);
3638c2ecf20Sopenharmony_ci		}
3648c2ecf20Sopenharmony_ci	}
3658c2ecf20Sopenharmony_ci	hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, false);
3668c2ecf20Sopenharmony_ci	len = ps->s_txreq->hdr_dwords + nwords;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	/* Setup the packet */
3698c2ecf20Sopenharmony_ci	ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_9B;
3708c2ecf20Sopenharmony_ci	hfi1_make_ib_hdr(&ps->s_txreq->phdr.hdr.ibh,
3718c2ecf20Sopenharmony_ci			 lrh0, len, dlid, slid);
3728c2ecf20Sopenharmony_ci}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_civoid hfi1_make_ud_req_16B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
3758c2ecf20Sopenharmony_ci			  struct rvt_swqe *wqe)
3768c2ecf20Sopenharmony_ci{
3778c2ecf20Sopenharmony_ci	struct hfi1_qp_priv *priv = qp->priv;
3788c2ecf20Sopenharmony_ci	struct ib_other_headers *ohdr;
3798c2ecf20Sopenharmony_ci	struct rdma_ah_attr *ah_attr;
3808c2ecf20Sopenharmony_ci	struct hfi1_pportdata *ppd;
3818c2ecf20Sopenharmony_ci	struct hfi1_ibport *ibp;
3828c2ecf20Sopenharmony_ci	u32 dlid, slid, nwords, extra_bytes;
3838c2ecf20Sopenharmony_ci	u32 dest_qp = rvt_get_swqe_remote_qpn(wqe);
3848c2ecf20Sopenharmony_ci	u32 src_qp = qp->ibqp.qp_num;
3858c2ecf20Sopenharmony_ci	u16 len, pkey;
3868c2ecf20Sopenharmony_ci	u8 l4, sc5;
3878c2ecf20Sopenharmony_ci	bool is_mgmt = false;
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	ibp = to_iport(qp->ibqp.device, qp->port_num);
3908c2ecf20Sopenharmony_ci	ppd = ppd_from_ibp(ibp);
3918c2ecf20Sopenharmony_ci	ah_attr = rvt_get_swqe_ah_attr(wqe);
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	/*
3948c2ecf20Sopenharmony_ci	 * Build 16B Management Packet if either the destination
3958c2ecf20Sopenharmony_ci	 * or source queue pair number is 0 or 1.
3968c2ecf20Sopenharmony_ci	 */
3978c2ecf20Sopenharmony_ci	if (dest_qp == 0 || src_qp == 0 || dest_qp == 1 || src_qp == 1) {
3988c2ecf20Sopenharmony_ci		/* header size in dwords 16B LRH+L4_FM = (16+8)/4. */
3998c2ecf20Sopenharmony_ci		ps->s_txreq->hdr_dwords = 6;
4008c2ecf20Sopenharmony_ci		is_mgmt = true;
4018c2ecf20Sopenharmony_ci	} else {
4028c2ecf20Sopenharmony_ci		/* header size in dwords 16B LRH+BTH+DETH = (16+12+8)/4. */
4038c2ecf20Sopenharmony_ci		ps->s_txreq->hdr_dwords = 9;
4048c2ecf20Sopenharmony_ci		if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
4058c2ecf20Sopenharmony_ci			ps->s_txreq->hdr_dwords++;
4068c2ecf20Sopenharmony_ci	}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	/* SW provides space for CRC and LT for bypass packets. */
4098c2ecf20Sopenharmony_ci	extra_bytes = hfi1_get_16b_padding((ps->s_txreq->hdr_dwords << 2),
4108c2ecf20Sopenharmony_ci					   wqe->length);
4118c2ecf20Sopenharmony_ci	nwords = ((wqe->length + extra_bytes + SIZE_OF_LT) >> 2) + SIZE_OF_CRC;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	if ((rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) &&
4148c2ecf20Sopenharmony_ci	    hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) {
4158c2ecf20Sopenharmony_ci		struct ib_grh *grh;
4168c2ecf20Sopenharmony_ci		struct ib_global_route *grd = rdma_ah_retrieve_grh(ah_attr);
4178c2ecf20Sopenharmony_ci		/*
4188c2ecf20Sopenharmony_ci		 * Ensure OPA GIDs are transformed to IB gids
4198c2ecf20Sopenharmony_ci		 * before creating the GRH.
4208c2ecf20Sopenharmony_ci		 */
4218c2ecf20Sopenharmony_ci		if (grd->sgid_index == OPA_GID_INDEX) {
4228c2ecf20Sopenharmony_ci			dd_dev_warn(ppd->dd, "Bad sgid_index. sgid_index: %d\n",
4238c2ecf20Sopenharmony_ci				    grd->sgid_index);
4248c2ecf20Sopenharmony_ci			grd->sgid_index = 0;
4258c2ecf20Sopenharmony_ci		}
4268c2ecf20Sopenharmony_ci		grh = &ps->s_txreq->phdr.hdr.opah.u.l.grh;
4278c2ecf20Sopenharmony_ci		ps->s_txreq->hdr_dwords += hfi1_make_grh(
4288c2ecf20Sopenharmony_ci			ibp, grh, grd,
4298c2ecf20Sopenharmony_ci			ps->s_txreq->hdr_dwords - LRH_16B_DWORDS,
4308c2ecf20Sopenharmony_ci			nwords);
4318c2ecf20Sopenharmony_ci		ohdr = &ps->s_txreq->phdr.hdr.opah.u.l.oth;
4328c2ecf20Sopenharmony_ci		l4 = OPA_16B_L4_IB_GLOBAL;
4338c2ecf20Sopenharmony_ci	} else {
4348c2ecf20Sopenharmony_ci		ohdr = &ps->s_txreq->phdr.hdr.opah.u.oth;
4358c2ecf20Sopenharmony_ci		l4 = OPA_16B_L4_IB_LOCAL;
4368c2ecf20Sopenharmony_ci	}
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
4398c2ecf20Sopenharmony_ci	if (qp->ibqp.qp_type == IB_QPT_SMI)
4408c2ecf20Sopenharmony_ci		priv->s_sc = 0xf;
4418c2ecf20Sopenharmony_ci	else
4428c2ecf20Sopenharmony_ci		priv->s_sc = sc5;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 16B);
4458c2ecf20Sopenharmony_ci	if (!ppd->lid)
4468c2ecf20Sopenharmony_ci		slid = be32_to_cpu(OPA_LID_PERMISSIVE);
4478c2ecf20Sopenharmony_ci	else
4488c2ecf20Sopenharmony_ci		slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
4498c2ecf20Sopenharmony_ci			   ((1 << ppd->lmc) - 1));
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	if (is_mgmt) {
4528c2ecf20Sopenharmony_ci		l4 = OPA_16B_L4_FM;
4538c2ecf20Sopenharmony_ci		pkey = hfi1_get_pkey(ibp, rvt_get_swqe_pkey_index(wqe));
4548c2ecf20Sopenharmony_ci		hfi1_16B_set_qpn(&ps->s_txreq->phdr.hdr.opah.u.mgmt,
4558c2ecf20Sopenharmony_ci				 dest_qp, src_qp);
4568c2ecf20Sopenharmony_ci	} else {
4578c2ecf20Sopenharmony_ci		hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, true);
4588c2ecf20Sopenharmony_ci	}
4598c2ecf20Sopenharmony_ci	/* Convert dwords to flits */
4608c2ecf20Sopenharmony_ci	len = (ps->s_txreq->hdr_dwords + nwords) >> 1;
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	/* Setup the packet */
4638c2ecf20Sopenharmony_ci	ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_16B;
4648c2ecf20Sopenharmony_ci	hfi1_make_16b_hdr(&ps->s_txreq->phdr.hdr.opah,
4658c2ecf20Sopenharmony_ci			  slid, dlid, len, pkey, 0, 0, l4, priv->s_sc);
4668c2ecf20Sopenharmony_ci}
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci/**
4698c2ecf20Sopenharmony_ci * hfi1_make_ud_req - construct a UD request packet
4708c2ecf20Sopenharmony_ci * @qp: the QP
4718c2ecf20Sopenharmony_ci *
4728c2ecf20Sopenharmony_ci * Assume s_lock is held.
4738c2ecf20Sopenharmony_ci *
4748c2ecf20Sopenharmony_ci * Return 1 if constructed; otherwise, return 0.
4758c2ecf20Sopenharmony_ci */
4768c2ecf20Sopenharmony_ciint hfi1_make_ud_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
4778c2ecf20Sopenharmony_ci{
4788c2ecf20Sopenharmony_ci	struct hfi1_qp_priv *priv = qp->priv;
4798c2ecf20Sopenharmony_ci	struct rdma_ah_attr *ah_attr;
4808c2ecf20Sopenharmony_ci	struct hfi1_pportdata *ppd;
4818c2ecf20Sopenharmony_ci	struct hfi1_ibport *ibp;
4828c2ecf20Sopenharmony_ci	struct rvt_swqe *wqe;
4838c2ecf20Sopenharmony_ci	int next_cur;
4848c2ecf20Sopenharmony_ci	u32 lid;
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	ps->s_txreq = get_txreq(ps->dev, qp);
4878c2ecf20Sopenharmony_ci	if (!ps->s_txreq)
4888c2ecf20Sopenharmony_ci		goto bail_no_tx;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
4918c2ecf20Sopenharmony_ci		if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
4928c2ecf20Sopenharmony_ci			goto bail;
4938c2ecf20Sopenharmony_ci		/* We are in the error state, flush the work request. */
4948c2ecf20Sopenharmony_ci		if (qp->s_last == READ_ONCE(qp->s_head))
4958c2ecf20Sopenharmony_ci			goto bail;
4968c2ecf20Sopenharmony_ci		/* If DMAs are in progress, we can't flush immediately. */
4978c2ecf20Sopenharmony_ci		if (iowait_sdma_pending(&priv->s_iowait)) {
4988c2ecf20Sopenharmony_ci			qp->s_flags |= RVT_S_WAIT_DMA;
4998c2ecf20Sopenharmony_ci			goto bail;
5008c2ecf20Sopenharmony_ci		}
5018c2ecf20Sopenharmony_ci		wqe = rvt_get_swqe_ptr(qp, qp->s_last);
5028c2ecf20Sopenharmony_ci		rvt_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
5038c2ecf20Sopenharmony_ci		goto done_free_tx;
5048c2ecf20Sopenharmony_ci	}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	/* see post_one_send() */
5078c2ecf20Sopenharmony_ci	if (qp->s_cur == READ_ONCE(qp->s_head))
5088c2ecf20Sopenharmony_ci		goto bail;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
5118c2ecf20Sopenharmony_ci	next_cur = qp->s_cur + 1;
5128c2ecf20Sopenharmony_ci	if (next_cur >= qp->s_size)
5138c2ecf20Sopenharmony_ci		next_cur = 0;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	/* Construct the header. */
5168c2ecf20Sopenharmony_ci	ibp = to_iport(qp->ibqp.device, qp->port_num);
5178c2ecf20Sopenharmony_ci	ppd = ppd_from_ibp(ibp);
5188c2ecf20Sopenharmony_ci	ah_attr = rvt_get_swqe_ah_attr(wqe);
5198c2ecf20Sopenharmony_ci	priv->hdr_type = hfi1_get_hdr_type(ppd->lid, ah_attr);
5208c2ecf20Sopenharmony_ci	if ((!hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) ||
5218c2ecf20Sopenharmony_ci	    (rdma_ah_get_dlid(ah_attr) == be32_to_cpu(OPA_LID_PERMISSIVE))) {
5228c2ecf20Sopenharmony_ci		lid = rdma_ah_get_dlid(ah_attr) & ~((1 << ppd->lmc) - 1);
5238c2ecf20Sopenharmony_ci		if (unlikely(!loopback &&
5248c2ecf20Sopenharmony_ci			     ((lid == ppd->lid) ||
5258c2ecf20Sopenharmony_ci			      ((lid == be32_to_cpu(OPA_LID_PERMISSIVE)) &&
5268c2ecf20Sopenharmony_ci			       (qp->ibqp.qp_type == IB_QPT_GSI))))) {
5278c2ecf20Sopenharmony_ci			unsigned long tflags = ps->flags;
5288c2ecf20Sopenharmony_ci			/*
5298c2ecf20Sopenharmony_ci			 * If DMAs are in progress, we can't generate
5308c2ecf20Sopenharmony_ci			 * a completion for the loopback packet since
5318c2ecf20Sopenharmony_ci			 * it would be out of order.
5328c2ecf20Sopenharmony_ci			 * Instead of waiting, we could queue a
5338c2ecf20Sopenharmony_ci			 * zero length descriptor so we get a callback.
5348c2ecf20Sopenharmony_ci			 */
5358c2ecf20Sopenharmony_ci			if (iowait_sdma_pending(&priv->s_iowait)) {
5368c2ecf20Sopenharmony_ci				qp->s_flags |= RVT_S_WAIT_DMA;
5378c2ecf20Sopenharmony_ci				goto bail;
5388c2ecf20Sopenharmony_ci			}
5398c2ecf20Sopenharmony_ci			qp->s_cur = next_cur;
5408c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&qp->s_lock, tflags);
5418c2ecf20Sopenharmony_ci			ud_loopback(qp, wqe);
5428c2ecf20Sopenharmony_ci			spin_lock_irqsave(&qp->s_lock, tflags);
5438c2ecf20Sopenharmony_ci			ps->flags = tflags;
5448c2ecf20Sopenharmony_ci			rvt_send_complete(qp, wqe, IB_WC_SUCCESS);
5458c2ecf20Sopenharmony_ci			goto done_free_tx;
5468c2ecf20Sopenharmony_ci		}
5478c2ecf20Sopenharmony_ci	}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	qp->s_cur = next_cur;
5508c2ecf20Sopenharmony_ci	ps->s_txreq->s_cur_size = wqe->length;
5518c2ecf20Sopenharmony_ci	ps->s_txreq->ss = &qp->s_sge;
5528c2ecf20Sopenharmony_ci	qp->s_srate = rdma_ah_get_static_rate(ah_attr);
5538c2ecf20Sopenharmony_ci	qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
5548c2ecf20Sopenharmony_ci	qp->s_wqe = wqe;
5558c2ecf20Sopenharmony_ci	qp->s_sge.sge = wqe->sg_list[0];
5568c2ecf20Sopenharmony_ci	qp->s_sge.sg_list = wqe->sg_list + 1;
5578c2ecf20Sopenharmony_ci	qp->s_sge.num_sge = wqe->wr.num_sge;
5588c2ecf20Sopenharmony_ci	qp->s_sge.total_len = wqe->length;
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	/* Make the appropriate header */
5618c2ecf20Sopenharmony_ci	hfi1_make_ud_req_tbl[priv->hdr_type](qp, ps, qp->s_wqe);
5628c2ecf20Sopenharmony_ci	priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
5638c2ecf20Sopenharmony_ci	ps->s_txreq->sde = priv->s_sde;
5648c2ecf20Sopenharmony_ci	priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
5658c2ecf20Sopenharmony_ci	ps->s_txreq->psc = priv->s_sendcontext;
5668c2ecf20Sopenharmony_ci	/* disarm any ahg */
5678c2ecf20Sopenharmony_ci	priv->s_ahg->ahgcount = 0;
5688c2ecf20Sopenharmony_ci	priv->s_ahg->ahgidx = 0;
5698c2ecf20Sopenharmony_ci	priv->s_ahg->tx_flags = 0;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	return 1;
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_cidone_free_tx:
5748c2ecf20Sopenharmony_ci	hfi1_put_txreq(ps->s_txreq);
5758c2ecf20Sopenharmony_ci	ps->s_txreq = NULL;
5768c2ecf20Sopenharmony_ci	return 1;
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_cibail:
5798c2ecf20Sopenharmony_ci	hfi1_put_txreq(ps->s_txreq);
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_cibail_no_tx:
5828c2ecf20Sopenharmony_ci	ps->s_txreq = NULL;
5838c2ecf20Sopenharmony_ci	qp->s_flags &= ~RVT_S_BUSY;
5848c2ecf20Sopenharmony_ci	return 0;
5858c2ecf20Sopenharmony_ci}
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci/*
5888c2ecf20Sopenharmony_ci * Hardware can't check this so we do it here.
5898c2ecf20Sopenharmony_ci *
5908c2ecf20Sopenharmony_ci * This is a slightly different algorithm than the standard pkey check.  It
5918c2ecf20Sopenharmony_ci * special cases the management keys and allows for 0x7fff and 0xffff to be in
5928c2ecf20Sopenharmony_ci * the table at the same time.
5938c2ecf20Sopenharmony_ci *
5948c2ecf20Sopenharmony_ci * @returns the index found or -1 if not found
5958c2ecf20Sopenharmony_ci */
5968c2ecf20Sopenharmony_ciint hfi1_lookup_pkey_idx(struct hfi1_ibport *ibp, u16 pkey)
5978c2ecf20Sopenharmony_ci{
5988c2ecf20Sopenharmony_ci	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
5998c2ecf20Sopenharmony_ci	unsigned i;
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	if (pkey == FULL_MGMT_P_KEY || pkey == LIM_MGMT_P_KEY) {
6028c2ecf20Sopenharmony_ci		unsigned lim_idx = -1;
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i) {
6058c2ecf20Sopenharmony_ci			/* here we look for an exact match */
6068c2ecf20Sopenharmony_ci			if (ppd->pkeys[i] == pkey)
6078c2ecf20Sopenharmony_ci				return i;
6088c2ecf20Sopenharmony_ci			if (ppd->pkeys[i] == LIM_MGMT_P_KEY)
6098c2ecf20Sopenharmony_ci				lim_idx = i;
6108c2ecf20Sopenharmony_ci		}
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci		/* did not find 0xffff return 0x7fff idx if found */
6138c2ecf20Sopenharmony_ci		if (pkey == FULL_MGMT_P_KEY)
6148c2ecf20Sopenharmony_ci			return lim_idx;
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci		/* no match...  */
6178c2ecf20Sopenharmony_ci		return -1;
6188c2ecf20Sopenharmony_ci	}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	pkey &= 0x7fff; /* remove limited/full membership bit */
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i)
6238c2ecf20Sopenharmony_ci		if ((ppd->pkeys[i] & 0x7fff) == pkey)
6248c2ecf20Sopenharmony_ci			return i;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	/*
6278c2ecf20Sopenharmony_ci	 * Should not get here, this means hardware failed to validate pkeys.
6288c2ecf20Sopenharmony_ci	 */
6298c2ecf20Sopenharmony_ci	return -1;
6308c2ecf20Sopenharmony_ci}
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_civoid return_cnp_16B(struct hfi1_ibport *ibp, struct rvt_qp *qp,
6338c2ecf20Sopenharmony_ci		    u32 remote_qpn, u16 pkey, u32 slid, u32 dlid,
6348c2ecf20Sopenharmony_ci		    u8 sc5, const struct ib_grh *old_grh)
6358c2ecf20Sopenharmony_ci{
6368c2ecf20Sopenharmony_ci	u64 pbc, pbc_flags = 0;
6378c2ecf20Sopenharmony_ci	u32 bth0, plen, vl, hwords = 7;
6388c2ecf20Sopenharmony_ci	u16 len;
6398c2ecf20Sopenharmony_ci	u8 l4;
6408c2ecf20Sopenharmony_ci	struct hfi1_opa_header hdr;
6418c2ecf20Sopenharmony_ci	struct ib_other_headers *ohdr;
6428c2ecf20Sopenharmony_ci	struct pio_buf *pbuf;
6438c2ecf20Sopenharmony_ci	struct send_context *ctxt = qp_to_send_context(qp, sc5);
6448c2ecf20Sopenharmony_ci	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
6458c2ecf20Sopenharmony_ci	u32 nwords;
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci	hdr.hdr_type = HFI1_PKT_TYPE_16B;
6488c2ecf20Sopenharmony_ci	/* Populate length */
6498c2ecf20Sopenharmony_ci	nwords = ((hfi1_get_16b_padding(hwords << 2, 0) +
6508c2ecf20Sopenharmony_ci		   SIZE_OF_LT) >> 2) + SIZE_OF_CRC;
6518c2ecf20Sopenharmony_ci	if (old_grh) {
6528c2ecf20Sopenharmony_ci		struct ib_grh *grh = &hdr.opah.u.l.grh;
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci		grh->version_tclass_flow = old_grh->version_tclass_flow;
6558c2ecf20Sopenharmony_ci		grh->paylen = cpu_to_be16(
6568c2ecf20Sopenharmony_ci			(hwords - LRH_16B_DWORDS + nwords) << 2);
6578c2ecf20Sopenharmony_ci		grh->hop_limit = 0xff;
6588c2ecf20Sopenharmony_ci		grh->sgid = old_grh->dgid;
6598c2ecf20Sopenharmony_ci		grh->dgid = old_grh->sgid;
6608c2ecf20Sopenharmony_ci		ohdr = &hdr.opah.u.l.oth;
6618c2ecf20Sopenharmony_ci		l4 = OPA_16B_L4_IB_GLOBAL;
6628c2ecf20Sopenharmony_ci		hwords += sizeof(struct ib_grh) / sizeof(u32);
6638c2ecf20Sopenharmony_ci	} else {
6648c2ecf20Sopenharmony_ci		ohdr = &hdr.opah.u.oth;
6658c2ecf20Sopenharmony_ci		l4 = OPA_16B_L4_IB_LOCAL;
6668c2ecf20Sopenharmony_ci	}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	/* BIT 16 to 19 is TVER. Bit 20 to 22 is pad cnt */
6698c2ecf20Sopenharmony_ci	bth0 = (IB_OPCODE_CNP << 24) | (1 << 16) |
6708c2ecf20Sopenharmony_ci	       (hfi1_get_16b_padding(hwords << 2, 0) << 20);
6718c2ecf20Sopenharmony_ci	ohdr->bth[0] = cpu_to_be32(bth0);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	ohdr->bth[1] = cpu_to_be32(remote_qpn);
6748c2ecf20Sopenharmony_ci	ohdr->bth[2] = 0; /* PSN 0 */
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	/* Convert dwords to flits */
6778c2ecf20Sopenharmony_ci	len = (hwords + nwords) >> 1;
6788c2ecf20Sopenharmony_ci	hfi1_make_16b_hdr(&hdr.opah, slid, dlid, len, pkey, 1, 0, l4, sc5);
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	plen = 2 /* PBC */ + hwords + nwords;
6818c2ecf20Sopenharmony_ci	pbc_flags |= PBC_PACKET_BYPASS | PBC_INSERT_BYPASS_ICRC;
6828c2ecf20Sopenharmony_ci	vl = sc_to_vlt(ppd->dd, sc5);
6838c2ecf20Sopenharmony_ci	pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
6848c2ecf20Sopenharmony_ci	if (ctxt) {
6858c2ecf20Sopenharmony_ci		pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
6868c2ecf20Sopenharmony_ci		if (!IS_ERR_OR_NULL(pbuf)) {
6878c2ecf20Sopenharmony_ci			trace_pio_output_ibhdr(ppd->dd, &hdr, sc5);
6888c2ecf20Sopenharmony_ci			ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
6898c2ecf20Sopenharmony_ci						 &hdr, hwords);
6908c2ecf20Sopenharmony_ci		}
6918c2ecf20Sopenharmony_ci	}
6928c2ecf20Sopenharmony_ci}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_civoid return_cnp(struct hfi1_ibport *ibp, struct rvt_qp *qp, u32 remote_qpn,
6958c2ecf20Sopenharmony_ci		u16 pkey, u32 slid, u32 dlid, u8 sc5,
6968c2ecf20Sopenharmony_ci		const struct ib_grh *old_grh)
6978c2ecf20Sopenharmony_ci{
6988c2ecf20Sopenharmony_ci	u64 pbc, pbc_flags = 0;
6998c2ecf20Sopenharmony_ci	u32 bth0, plen, vl, hwords = 5;
7008c2ecf20Sopenharmony_ci	u16 lrh0;
7018c2ecf20Sopenharmony_ci	u8 sl = ibp->sc_to_sl[sc5];
7028c2ecf20Sopenharmony_ci	struct hfi1_opa_header hdr;
7038c2ecf20Sopenharmony_ci	struct ib_other_headers *ohdr;
7048c2ecf20Sopenharmony_ci	struct pio_buf *pbuf;
7058c2ecf20Sopenharmony_ci	struct send_context *ctxt = qp_to_send_context(qp, sc5);
7068c2ecf20Sopenharmony_ci	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	hdr.hdr_type = HFI1_PKT_TYPE_9B;
7098c2ecf20Sopenharmony_ci	if (old_grh) {
7108c2ecf20Sopenharmony_ci		struct ib_grh *grh = &hdr.ibh.u.l.grh;
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci		grh->version_tclass_flow = old_grh->version_tclass_flow;
7138c2ecf20Sopenharmony_ci		grh->paylen = cpu_to_be16(
7148c2ecf20Sopenharmony_ci			(hwords - LRH_9B_DWORDS + SIZE_OF_CRC) << 2);
7158c2ecf20Sopenharmony_ci		grh->hop_limit = 0xff;
7168c2ecf20Sopenharmony_ci		grh->sgid = old_grh->dgid;
7178c2ecf20Sopenharmony_ci		grh->dgid = old_grh->sgid;
7188c2ecf20Sopenharmony_ci		ohdr = &hdr.ibh.u.l.oth;
7198c2ecf20Sopenharmony_ci		lrh0 = HFI1_LRH_GRH;
7208c2ecf20Sopenharmony_ci		hwords += sizeof(struct ib_grh) / sizeof(u32);
7218c2ecf20Sopenharmony_ci	} else {
7228c2ecf20Sopenharmony_ci		ohdr = &hdr.ibh.u.oth;
7238c2ecf20Sopenharmony_ci		lrh0 = HFI1_LRH_BTH;
7248c2ecf20Sopenharmony_ci	}
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	lrh0 |= (sc5 & 0xf) << 12 | sl << 4;
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	bth0 = pkey | (IB_OPCODE_CNP << 24);
7298c2ecf20Sopenharmony_ci	ohdr->bth[0] = cpu_to_be32(bth0);
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	ohdr->bth[1] = cpu_to_be32(remote_qpn | (1 << IB_BECN_SHIFT));
7328c2ecf20Sopenharmony_ci	ohdr->bth[2] = 0; /* PSN 0 */
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	hfi1_make_ib_hdr(&hdr.ibh, lrh0, hwords + SIZE_OF_CRC, dlid, slid);
7358c2ecf20Sopenharmony_ci	plen = 2 /* PBC */ + hwords;
7368c2ecf20Sopenharmony_ci	pbc_flags |= (ib_is_sc5(sc5) << PBC_DC_INFO_SHIFT);
7378c2ecf20Sopenharmony_ci	vl = sc_to_vlt(ppd->dd, sc5);
7388c2ecf20Sopenharmony_ci	pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
7398c2ecf20Sopenharmony_ci	if (ctxt) {
7408c2ecf20Sopenharmony_ci		pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
7418c2ecf20Sopenharmony_ci		if (!IS_ERR_OR_NULL(pbuf)) {
7428c2ecf20Sopenharmony_ci			trace_pio_output_ibhdr(ppd->dd, &hdr, sc5);
7438c2ecf20Sopenharmony_ci			ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
7448c2ecf20Sopenharmony_ci						 &hdr, hwords);
7458c2ecf20Sopenharmony_ci		}
7468c2ecf20Sopenharmony_ci	}
7478c2ecf20Sopenharmony_ci}
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci/*
7508c2ecf20Sopenharmony_ci * opa_smp_check() - Do the regular pkey checking, and the additional
7518c2ecf20Sopenharmony_ci * checks for SMPs specified in OPAv1 rev 1.0, 9/19/2016 update, section
7528c2ecf20Sopenharmony_ci * 9.10.25 ("SMA Packet Checks").
7538c2ecf20Sopenharmony_ci *
7548c2ecf20Sopenharmony_ci * Note that:
7558c2ecf20Sopenharmony_ci *   - Checks are done using the pkey directly from the packet's BTH,
7568c2ecf20Sopenharmony_ci *     and specifically _not_ the pkey that we attach to the completion,
7578c2ecf20Sopenharmony_ci *     which may be different.
7588c2ecf20Sopenharmony_ci *   - These checks are specifically for "non-local" SMPs (i.e., SMPs
7598c2ecf20Sopenharmony_ci *     which originated on another node). SMPs which are sent from, and
7608c2ecf20Sopenharmony_ci *     destined to this node are checked in opa_local_smp_check().
7618c2ecf20Sopenharmony_ci *
7628c2ecf20Sopenharmony_ci * At the point where opa_smp_check() is called, we know:
7638c2ecf20Sopenharmony_ci *   - destination QP is QP0
7648c2ecf20Sopenharmony_ci *
7658c2ecf20Sopenharmony_ci * opa_smp_check() returns 0 if all checks succeed, 1 otherwise.
7668c2ecf20Sopenharmony_ci */
7678c2ecf20Sopenharmony_cistatic int opa_smp_check(struct hfi1_ibport *ibp, u16 pkey, u8 sc5,
7688c2ecf20Sopenharmony_ci			 struct rvt_qp *qp, u16 slid, struct opa_smp *smp)
7698c2ecf20Sopenharmony_ci{
7708c2ecf20Sopenharmony_ci	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	/*
7738c2ecf20Sopenharmony_ci	 * I don't think it's possible for us to get here with sc != 0xf,
7748c2ecf20Sopenharmony_ci	 * but check it to be certain.
7758c2ecf20Sopenharmony_ci	 */
7768c2ecf20Sopenharmony_ci	if (sc5 != 0xf)
7778c2ecf20Sopenharmony_ci		return 1;
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	if (rcv_pkey_check(ppd, pkey, sc5, slid))
7808c2ecf20Sopenharmony_ci		return 1;
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	/*
7838c2ecf20Sopenharmony_ci	 * At this point we know (and so don't need to check again) that
7848c2ecf20Sopenharmony_ci	 * the pkey is either LIM_MGMT_P_KEY, or FULL_MGMT_P_KEY
7858c2ecf20Sopenharmony_ci	 * (see ingress_pkey_check).
7868c2ecf20Sopenharmony_ci	 */
7878c2ecf20Sopenharmony_ci	if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE &&
7888c2ecf20Sopenharmony_ci	    smp->mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED) {
7898c2ecf20Sopenharmony_ci		ingress_pkey_table_fail(ppd, pkey, slid);
7908c2ecf20Sopenharmony_ci		return 1;
7918c2ecf20Sopenharmony_ci	}
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	/*
7948c2ecf20Sopenharmony_ci	 * SMPs fall into one of four (disjoint) categories:
7958c2ecf20Sopenharmony_ci	 * SMA request, SMA response, SMA trap, or SMA trap repress.
7968c2ecf20Sopenharmony_ci	 * Our response depends, in part, on which type of SMP we're
7978c2ecf20Sopenharmony_ci	 * processing.
7988c2ecf20Sopenharmony_ci	 *
7998c2ecf20Sopenharmony_ci	 * If this is an SMA response, skip the check here.
8008c2ecf20Sopenharmony_ci	 *
8018c2ecf20Sopenharmony_ci	 * If this is an SMA request or SMA trap repress:
8028c2ecf20Sopenharmony_ci	 *   - pkey != FULL_MGMT_P_KEY =>
8038c2ecf20Sopenharmony_ci	 *       increment port recv constraint errors, drop MAD
8048c2ecf20Sopenharmony_ci	 *
8058c2ecf20Sopenharmony_ci	 * Otherwise:
8068c2ecf20Sopenharmony_ci	 *    - accept if the port is running an SM
8078c2ecf20Sopenharmony_ci	 *    - drop MAD if it's an SMA trap
8088c2ecf20Sopenharmony_ci	 *    - pkey == FULL_MGMT_P_KEY =>
8098c2ecf20Sopenharmony_ci	 *        reply with unsupported method
8108c2ecf20Sopenharmony_ci	 *    - pkey != FULL_MGMT_P_KEY =>
8118c2ecf20Sopenharmony_ci	 *	  increment port recv constraint errors, drop MAD
8128c2ecf20Sopenharmony_ci	 */
8138c2ecf20Sopenharmony_ci	switch (smp->method) {
8148c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_GET_RESP:
8158c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_REPORT_RESP:
8168c2ecf20Sopenharmony_ci		break;
8178c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_GET:
8188c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_SET:
8198c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_REPORT:
8208c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_TRAP_REPRESS:
8218c2ecf20Sopenharmony_ci		if (pkey != FULL_MGMT_P_KEY) {
8228c2ecf20Sopenharmony_ci			ingress_pkey_table_fail(ppd, pkey, slid);
8238c2ecf20Sopenharmony_ci			return 1;
8248c2ecf20Sopenharmony_ci		}
8258c2ecf20Sopenharmony_ci		break;
8268c2ecf20Sopenharmony_ci	default:
8278c2ecf20Sopenharmony_ci		if (ibp->rvp.port_cap_flags & IB_PORT_SM)
8288c2ecf20Sopenharmony_ci			return 0;
8298c2ecf20Sopenharmony_ci		if (smp->method == IB_MGMT_METHOD_TRAP)
8308c2ecf20Sopenharmony_ci			return 1;
8318c2ecf20Sopenharmony_ci		if (pkey == FULL_MGMT_P_KEY) {
8328c2ecf20Sopenharmony_ci			smp->status |= IB_SMP_UNSUP_METHOD;
8338c2ecf20Sopenharmony_ci			return 0;
8348c2ecf20Sopenharmony_ci		}
8358c2ecf20Sopenharmony_ci		ingress_pkey_table_fail(ppd, pkey, slid);
8368c2ecf20Sopenharmony_ci		return 1;
8378c2ecf20Sopenharmony_ci	}
8388c2ecf20Sopenharmony_ci	return 0;
8398c2ecf20Sopenharmony_ci}
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci/**
8428c2ecf20Sopenharmony_ci * hfi1_ud_rcv - receive an incoming UD packet
8438c2ecf20Sopenharmony_ci * @ibp: the port the packet came in on
8448c2ecf20Sopenharmony_ci * @hdr: the packet header
8458c2ecf20Sopenharmony_ci * @rcv_flags: flags relevant to rcv processing
8468c2ecf20Sopenharmony_ci * @data: the packet data
8478c2ecf20Sopenharmony_ci * @tlen: the packet length
8488c2ecf20Sopenharmony_ci * @qp: the QP the packet came on
8498c2ecf20Sopenharmony_ci *
8508c2ecf20Sopenharmony_ci * This is called from qp_rcv() to process an incoming UD packet
8518c2ecf20Sopenharmony_ci * for the given QP.
8528c2ecf20Sopenharmony_ci * Called at interrupt level.
8538c2ecf20Sopenharmony_ci */
8548c2ecf20Sopenharmony_civoid hfi1_ud_rcv(struct hfi1_packet *packet)
8558c2ecf20Sopenharmony_ci{
8568c2ecf20Sopenharmony_ci	u32 hdrsize = packet->hlen;
8578c2ecf20Sopenharmony_ci	struct ib_wc wc;
8588c2ecf20Sopenharmony_ci	u32 src_qp;
8598c2ecf20Sopenharmony_ci	u16 pkey;
8608c2ecf20Sopenharmony_ci	int mgmt_pkey_idx = -1;
8618c2ecf20Sopenharmony_ci	struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
8628c2ecf20Sopenharmony_ci	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
8638c2ecf20Sopenharmony_ci	void *data = packet->payload;
8648c2ecf20Sopenharmony_ci	u32 tlen = packet->tlen;
8658c2ecf20Sopenharmony_ci	struct rvt_qp *qp = packet->qp;
8668c2ecf20Sopenharmony_ci	u8 sc5 = packet->sc;
8678c2ecf20Sopenharmony_ci	u8 sl_from_sc;
8688c2ecf20Sopenharmony_ci	u8 opcode = packet->opcode;
8698c2ecf20Sopenharmony_ci	u8 sl = packet->sl;
8708c2ecf20Sopenharmony_ci	u32 dlid = packet->dlid;
8718c2ecf20Sopenharmony_ci	u32 slid = packet->slid;
8728c2ecf20Sopenharmony_ci	u8 extra_bytes;
8738c2ecf20Sopenharmony_ci	u8 l4 = 0;
8748c2ecf20Sopenharmony_ci	bool dlid_is_permissive;
8758c2ecf20Sopenharmony_ci	bool slid_is_permissive;
8768c2ecf20Sopenharmony_ci	bool solicited = false;
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	extra_bytes = packet->pad + packet->extra_byte + (SIZE_OF_CRC << 2);
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	if (packet->etype == RHF_RCV_TYPE_BYPASS) {
8818c2ecf20Sopenharmony_ci		u32 permissive_lid =
8828c2ecf20Sopenharmony_ci			opa_get_lid(be32_to_cpu(OPA_LID_PERMISSIVE), 16B);
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci		l4 = hfi1_16B_get_l4(packet->hdr);
8858c2ecf20Sopenharmony_ci		pkey = hfi1_16B_get_pkey(packet->hdr);
8868c2ecf20Sopenharmony_ci		dlid_is_permissive = (dlid == permissive_lid);
8878c2ecf20Sopenharmony_ci		slid_is_permissive = (slid == permissive_lid);
8888c2ecf20Sopenharmony_ci	} else {
8898c2ecf20Sopenharmony_ci		pkey = ib_bth_get_pkey(packet->ohdr);
8908c2ecf20Sopenharmony_ci		dlid_is_permissive = (dlid == be16_to_cpu(IB_LID_PERMISSIVE));
8918c2ecf20Sopenharmony_ci		slid_is_permissive = (slid == be16_to_cpu(IB_LID_PERMISSIVE));
8928c2ecf20Sopenharmony_ci	}
8938c2ecf20Sopenharmony_ci	sl_from_sc = ibp->sc_to_sl[sc5];
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	if (likely(l4 != OPA_16B_L4_FM)) {
8968c2ecf20Sopenharmony_ci		src_qp = ib_get_sqpn(packet->ohdr);
8978c2ecf20Sopenharmony_ci		solicited = ib_bth_is_solicited(packet->ohdr);
8988c2ecf20Sopenharmony_ci	} else {
8998c2ecf20Sopenharmony_ci		src_qp = hfi1_16B_get_src_qpn(packet->mgmt);
9008c2ecf20Sopenharmony_ci	}
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci	process_ecn(qp, packet);
9038c2ecf20Sopenharmony_ci	/*
9048c2ecf20Sopenharmony_ci	 * Get the number of bytes the message was padded by
9058c2ecf20Sopenharmony_ci	 * and drop incomplete packets.
9068c2ecf20Sopenharmony_ci	 */
9078c2ecf20Sopenharmony_ci	if (unlikely(tlen < (hdrsize + extra_bytes)))
9088c2ecf20Sopenharmony_ci		goto drop;
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	tlen -= hdrsize + extra_bytes;
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	/*
9138c2ecf20Sopenharmony_ci	 * Check that the permissive LID is only used on QP0
9148c2ecf20Sopenharmony_ci	 * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
9158c2ecf20Sopenharmony_ci	 */
9168c2ecf20Sopenharmony_ci	if (qp->ibqp.qp_num) {
9178c2ecf20Sopenharmony_ci		if (unlikely(dlid_is_permissive || slid_is_permissive))
9188c2ecf20Sopenharmony_ci			goto drop;
9198c2ecf20Sopenharmony_ci		if (qp->ibqp.qp_num > 1) {
9208c2ecf20Sopenharmony_ci			if (unlikely(rcv_pkey_check(ppd, pkey, sc5, slid))) {
9218c2ecf20Sopenharmony_ci				/*
9228c2ecf20Sopenharmony_ci				 * Traps will not be sent for packets dropped
9238c2ecf20Sopenharmony_ci				 * by the HW. This is fine, as sending trap
9248c2ecf20Sopenharmony_ci				 * for invalid pkeys is optional according to
9258c2ecf20Sopenharmony_ci				 * IB spec (release 1.3, section 10.9.4)
9268c2ecf20Sopenharmony_ci				 */
9278c2ecf20Sopenharmony_ci				hfi1_bad_pkey(ibp,
9288c2ecf20Sopenharmony_ci					      pkey, sl,
9298c2ecf20Sopenharmony_ci					      src_qp, qp->ibqp.qp_num,
9308c2ecf20Sopenharmony_ci					      slid, dlid);
9318c2ecf20Sopenharmony_ci				return;
9328c2ecf20Sopenharmony_ci			}
9338c2ecf20Sopenharmony_ci		} else {
9348c2ecf20Sopenharmony_ci			/* GSI packet */
9358c2ecf20Sopenharmony_ci			mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
9368c2ecf20Sopenharmony_ci			if (mgmt_pkey_idx < 0)
9378c2ecf20Sopenharmony_ci				goto drop;
9388c2ecf20Sopenharmony_ci		}
9398c2ecf20Sopenharmony_ci		if (unlikely(l4 != OPA_16B_L4_FM &&
9408c2ecf20Sopenharmony_ci			     ib_get_qkey(packet->ohdr) != qp->qkey))
9418c2ecf20Sopenharmony_ci			return; /* Silent drop */
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci		/* Drop invalid MAD packets (see 13.5.3.1). */
9448c2ecf20Sopenharmony_ci		if (unlikely(qp->ibqp.qp_num == 1 &&
9458c2ecf20Sopenharmony_ci			     (tlen > 2048 || (sc5 == 0xF))))
9468c2ecf20Sopenharmony_ci			goto drop;
9478c2ecf20Sopenharmony_ci	} else {
9488c2ecf20Sopenharmony_ci		/* Received on QP0, and so by definition, this is an SMP */
9498c2ecf20Sopenharmony_ci		struct opa_smp *smp = (struct opa_smp *)data;
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci		if (opa_smp_check(ibp, pkey, sc5, qp, slid, smp))
9528c2ecf20Sopenharmony_ci			goto drop;
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci		if (tlen > 2048)
9558c2ecf20Sopenharmony_ci			goto drop;
9568c2ecf20Sopenharmony_ci		if ((dlid_is_permissive || slid_is_permissive) &&
9578c2ecf20Sopenharmony_ci		    smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
9588c2ecf20Sopenharmony_ci			goto drop;
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci		/* look up SMI pkey */
9618c2ecf20Sopenharmony_ci		mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
9628c2ecf20Sopenharmony_ci		if (mgmt_pkey_idx < 0)
9638c2ecf20Sopenharmony_ci			goto drop;
9648c2ecf20Sopenharmony_ci	}
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci	if (qp->ibqp.qp_num > 1 &&
9678c2ecf20Sopenharmony_ci	    opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
9688c2ecf20Sopenharmony_ci		wc.ex.imm_data = packet->ohdr->u.ud.imm_data;
9698c2ecf20Sopenharmony_ci		wc.wc_flags = IB_WC_WITH_IMM;
9708c2ecf20Sopenharmony_ci	} else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
9718c2ecf20Sopenharmony_ci		wc.ex.imm_data = 0;
9728c2ecf20Sopenharmony_ci		wc.wc_flags = 0;
9738c2ecf20Sopenharmony_ci	} else {
9748c2ecf20Sopenharmony_ci		goto drop;
9758c2ecf20Sopenharmony_ci	}
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ci	/*
9788c2ecf20Sopenharmony_ci	 * A GRH is expected to precede the data even if not
9798c2ecf20Sopenharmony_ci	 * present on the wire.
9808c2ecf20Sopenharmony_ci	 */
9818c2ecf20Sopenharmony_ci	wc.byte_len = tlen + sizeof(struct ib_grh);
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci	/*
9848c2ecf20Sopenharmony_ci	 * Get the next work request entry to find where to put the data.
9858c2ecf20Sopenharmony_ci	 */
9868c2ecf20Sopenharmony_ci	if (qp->r_flags & RVT_R_REUSE_SGE) {
9878c2ecf20Sopenharmony_ci		qp->r_flags &= ~RVT_R_REUSE_SGE;
9888c2ecf20Sopenharmony_ci	} else {
9898c2ecf20Sopenharmony_ci		int ret;
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci		ret = rvt_get_rwqe(qp, false);
9928c2ecf20Sopenharmony_ci		if (ret < 0) {
9938c2ecf20Sopenharmony_ci			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
9948c2ecf20Sopenharmony_ci			return;
9958c2ecf20Sopenharmony_ci		}
9968c2ecf20Sopenharmony_ci		if (!ret) {
9978c2ecf20Sopenharmony_ci			if (qp->ibqp.qp_num == 0)
9988c2ecf20Sopenharmony_ci				ibp->rvp.n_vl15_dropped++;
9998c2ecf20Sopenharmony_ci			return;
10008c2ecf20Sopenharmony_ci		}
10018c2ecf20Sopenharmony_ci	}
10028c2ecf20Sopenharmony_ci	/* Silently drop packets which are too big. */
10038c2ecf20Sopenharmony_ci	if (unlikely(wc.byte_len > qp->r_len)) {
10048c2ecf20Sopenharmony_ci		qp->r_flags |= RVT_R_REUSE_SGE;
10058c2ecf20Sopenharmony_ci		goto drop;
10068c2ecf20Sopenharmony_ci	}
10078c2ecf20Sopenharmony_ci	if (packet->grh) {
10088c2ecf20Sopenharmony_ci		rvt_copy_sge(qp, &qp->r_sge, packet->grh,
10098c2ecf20Sopenharmony_ci			     sizeof(struct ib_grh), true, false);
10108c2ecf20Sopenharmony_ci		wc.wc_flags |= IB_WC_GRH;
10118c2ecf20Sopenharmony_ci	} else if (packet->etype == RHF_RCV_TYPE_BYPASS) {
10128c2ecf20Sopenharmony_ci		struct ib_grh grh;
10138c2ecf20Sopenharmony_ci		/*
10148c2ecf20Sopenharmony_ci		 * Assuming we only created 16B on the send side
10158c2ecf20Sopenharmony_ci		 * if we want to use large LIDs, since GRH was stripped
10168c2ecf20Sopenharmony_ci		 * out when creating 16B, add back the GRH here.
10178c2ecf20Sopenharmony_ci		 */
10188c2ecf20Sopenharmony_ci		hfi1_make_ext_grh(packet, &grh, slid, dlid);
10198c2ecf20Sopenharmony_ci		rvt_copy_sge(qp, &qp->r_sge, &grh,
10208c2ecf20Sopenharmony_ci			     sizeof(struct ib_grh), true, false);
10218c2ecf20Sopenharmony_ci		wc.wc_flags |= IB_WC_GRH;
10228c2ecf20Sopenharmony_ci	} else {
10238c2ecf20Sopenharmony_ci		rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
10248c2ecf20Sopenharmony_ci	}
10258c2ecf20Sopenharmony_ci	rvt_copy_sge(qp, &qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh),
10268c2ecf20Sopenharmony_ci		     true, false);
10278c2ecf20Sopenharmony_ci	rvt_put_ss(&qp->r_sge);
10288c2ecf20Sopenharmony_ci	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
10298c2ecf20Sopenharmony_ci		return;
10308c2ecf20Sopenharmony_ci	wc.wr_id = qp->r_wr_id;
10318c2ecf20Sopenharmony_ci	wc.status = IB_WC_SUCCESS;
10328c2ecf20Sopenharmony_ci	wc.opcode = IB_WC_RECV;
10338c2ecf20Sopenharmony_ci	wc.vendor_err = 0;
10348c2ecf20Sopenharmony_ci	wc.qp = &qp->ibqp;
10358c2ecf20Sopenharmony_ci	wc.src_qp = src_qp;
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci	if (qp->ibqp.qp_type == IB_QPT_GSI ||
10388c2ecf20Sopenharmony_ci	    qp->ibqp.qp_type == IB_QPT_SMI) {
10398c2ecf20Sopenharmony_ci		if (mgmt_pkey_idx < 0) {
10408c2ecf20Sopenharmony_ci			if (net_ratelimit()) {
10418c2ecf20Sopenharmony_ci				struct hfi1_devdata *dd = ppd->dd;
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci				dd_dev_err(dd, "QP type %d mgmt_pkey_idx < 0 and packet not dropped???\n",
10448c2ecf20Sopenharmony_ci					   qp->ibqp.qp_type);
10458c2ecf20Sopenharmony_ci				mgmt_pkey_idx = 0;
10468c2ecf20Sopenharmony_ci			}
10478c2ecf20Sopenharmony_ci		}
10488c2ecf20Sopenharmony_ci		wc.pkey_index = (unsigned)mgmt_pkey_idx;
10498c2ecf20Sopenharmony_ci	} else {
10508c2ecf20Sopenharmony_ci		wc.pkey_index = 0;
10518c2ecf20Sopenharmony_ci	}
10528c2ecf20Sopenharmony_ci	if (slid_is_permissive)
10538c2ecf20Sopenharmony_ci		slid = be32_to_cpu(OPA_LID_PERMISSIVE);
10548c2ecf20Sopenharmony_ci	wc.slid = slid & U16_MAX;
10558c2ecf20Sopenharmony_ci	wc.sl = sl_from_sc;
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	/*
10588c2ecf20Sopenharmony_ci	 * Save the LMC lower bits if the destination LID is a unicast LID.
10598c2ecf20Sopenharmony_ci	 */
10608c2ecf20Sopenharmony_ci	wc.dlid_path_bits = hfi1_check_mcast(dlid) ? 0 :
10618c2ecf20Sopenharmony_ci		dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
10628c2ecf20Sopenharmony_ci	wc.port_num = qp->port_num;
10638c2ecf20Sopenharmony_ci	/* Signal completion event if the solicited bit is set. */
10648c2ecf20Sopenharmony_ci	rvt_recv_cq(qp, &wc, solicited);
10658c2ecf20Sopenharmony_ci	return;
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_cidrop:
10688c2ecf20Sopenharmony_ci	ibp->rvp.n_pkt_drops++;
10698c2ecf20Sopenharmony_ci}
1070