xref: /OK3568_Linux_fs/kernel/drivers/infiniband/hw/hfi1/ud.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright(c) 2015 - 2019 Intel Corporation.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * This file is provided under a dual BSD/GPLv2 license.  When using or
5*4882a593Smuzhiyun  * redistributing this file, you may do so under either license.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * GPL LICENSE SUMMARY
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun  * it under the terms of version 2 of the GNU General Public License as
11*4882a593Smuzhiyun  * published by the Free Software Foundation.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful, but
14*4882a593Smuzhiyun  * WITHOUT ANY WARRANTY; without even the implied warranty of
15*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16*4882a593Smuzhiyun  * General Public License for more details.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * BSD LICENSE
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
21*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
22*4882a593Smuzhiyun  * are met:
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  *  - Redistributions of source code must retain the above copyright
25*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer.
26*4882a593Smuzhiyun  *  - Redistributions in binary form must reproduce the above copyright
27*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer in
28*4882a593Smuzhiyun  *    the documentation and/or other materials provided with the
29*4882a593Smuzhiyun  *    distribution.
30*4882a593Smuzhiyun  *  - Neither the name of Intel Corporation nor the names of its
31*4882a593Smuzhiyun  *    contributors may be used to endorse or promote products derived
32*4882a593Smuzhiyun  *    from this software without specific prior written permission.
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38*4882a593Smuzhiyun  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39*4882a593Smuzhiyun  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40*4882a593Smuzhiyun  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41*4882a593Smuzhiyun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42*4882a593Smuzhiyun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44*4882a593Smuzhiyun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  */
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #include <linux/net.h>
49*4882a593Smuzhiyun #include <rdma/ib_smi.h>
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #include "hfi.h"
52*4882a593Smuzhiyun #include "mad.h"
53*4882a593Smuzhiyun #include "verbs_txreq.h"
54*4882a593Smuzhiyun #include "trace_ibhdrs.h"
55*4882a593Smuzhiyun #include "qp.h"
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun /* We support only two types - 9B and 16B for now */
58*4882a593Smuzhiyun static const hfi1_make_req hfi1_make_ud_req_tbl[2] = {
59*4882a593Smuzhiyun 	[HFI1_PKT_TYPE_9B] = &hfi1_make_ud_req_9B,
60*4882a593Smuzhiyun 	[HFI1_PKT_TYPE_16B] = &hfi1_make_ud_req_16B
61*4882a593Smuzhiyun };
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /**
64*4882a593Smuzhiyun  * ud_loopback - handle send on loopback QPs
65*4882a593Smuzhiyun  * @sqp: the sending QP
66*4882a593Smuzhiyun  * @swqe: the send work request
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * This is called from hfi1_make_ud_req() to forward a WQE addressed
69*4882a593Smuzhiyun  * to the same HFI.
70*4882a593Smuzhiyun  * Note that the receive interrupt handler may be calling hfi1_ud_rcv()
71*4882a593Smuzhiyun  * while this is being called.
72*4882a593Smuzhiyun  */
ud_loopback(struct rvt_qp * sqp,struct rvt_swqe * swqe)73*4882a593Smuzhiyun static void ud_loopback(struct rvt_qp *sqp, struct rvt_swqe *swqe)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
76*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd;
77*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = sqp->priv;
78*4882a593Smuzhiyun 	struct rvt_qp *qp;
79*4882a593Smuzhiyun 	struct rdma_ah_attr *ah_attr;
80*4882a593Smuzhiyun 	unsigned long flags;
81*4882a593Smuzhiyun 	struct rvt_sge_state ssge;
82*4882a593Smuzhiyun 	struct rvt_sge *sge;
83*4882a593Smuzhiyun 	struct ib_wc wc;
84*4882a593Smuzhiyun 	u32 length;
85*4882a593Smuzhiyun 	enum ib_qp_type sqptype, dqptype;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	rcu_read_lock();
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
90*4882a593Smuzhiyun 			    rvt_get_swqe_remote_qpn(swqe));
91*4882a593Smuzhiyun 	if (!qp) {
92*4882a593Smuzhiyun 		ibp->rvp.n_pkt_drops++;
93*4882a593Smuzhiyun 		rcu_read_unlock();
94*4882a593Smuzhiyun 		return;
95*4882a593Smuzhiyun 	}
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	sqptype = sqp->ibqp.qp_type == IB_QPT_GSI ?
98*4882a593Smuzhiyun 			IB_QPT_UD : sqp->ibqp.qp_type;
99*4882a593Smuzhiyun 	dqptype = qp->ibqp.qp_type == IB_QPT_GSI ?
100*4882a593Smuzhiyun 			IB_QPT_UD : qp->ibqp.qp_type;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	if (dqptype != sqptype ||
103*4882a593Smuzhiyun 	    !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
104*4882a593Smuzhiyun 		ibp->rvp.n_pkt_drops++;
105*4882a593Smuzhiyun 		goto drop;
106*4882a593Smuzhiyun 	}
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	ah_attr = rvt_get_swqe_ah_attr(swqe);
109*4882a593Smuzhiyun 	ppd = ppd_from_ibp(ibp);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	if (qp->ibqp.qp_num > 1) {
112*4882a593Smuzhiyun 		u16 pkey;
113*4882a593Smuzhiyun 		u32 slid;
114*4882a593Smuzhiyun 		u8 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 		pkey = hfi1_get_pkey(ibp, sqp->s_pkey_index);
117*4882a593Smuzhiyun 		slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
118*4882a593Smuzhiyun 				   ((1 << ppd->lmc) - 1));
119*4882a593Smuzhiyun 		if (unlikely(ingress_pkey_check(ppd, pkey, sc5,
120*4882a593Smuzhiyun 						qp->s_pkey_index,
121*4882a593Smuzhiyun 						slid, false))) {
122*4882a593Smuzhiyun 			hfi1_bad_pkey(ibp, pkey,
123*4882a593Smuzhiyun 				      rdma_ah_get_sl(ah_attr),
124*4882a593Smuzhiyun 				      sqp->ibqp.qp_num, qp->ibqp.qp_num,
125*4882a593Smuzhiyun 				      slid, rdma_ah_get_dlid(ah_attr));
126*4882a593Smuzhiyun 			goto drop;
127*4882a593Smuzhiyun 		}
128*4882a593Smuzhiyun 	}
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	/*
131*4882a593Smuzhiyun 	 * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
132*4882a593Smuzhiyun 	 * Qkeys with the high order bit set mean use the
133*4882a593Smuzhiyun 	 * qkey from the QP context instead of the WR (see 10.2.5).
134*4882a593Smuzhiyun 	 */
135*4882a593Smuzhiyun 	if (qp->ibqp.qp_num) {
136*4882a593Smuzhiyun 		u32 qkey;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 		qkey = (int)rvt_get_swqe_remote_qkey(swqe) < 0 ?
139*4882a593Smuzhiyun 			sqp->qkey : rvt_get_swqe_remote_qkey(swqe);
140*4882a593Smuzhiyun 		if (unlikely(qkey != qp->qkey))
141*4882a593Smuzhiyun 			goto drop; /* silently drop per IBTA spec */
142*4882a593Smuzhiyun 	}
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	/*
145*4882a593Smuzhiyun 	 * A GRH is expected to precede the data even if not
146*4882a593Smuzhiyun 	 * present on the wire.
147*4882a593Smuzhiyun 	 */
148*4882a593Smuzhiyun 	length = swqe->length;
149*4882a593Smuzhiyun 	memset(&wc, 0, sizeof(wc));
150*4882a593Smuzhiyun 	wc.byte_len = length + sizeof(struct ib_grh);
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
153*4882a593Smuzhiyun 		wc.wc_flags = IB_WC_WITH_IMM;
154*4882a593Smuzhiyun 		wc.ex.imm_data = swqe->wr.ex.imm_data;
155*4882a593Smuzhiyun 	}
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	spin_lock_irqsave(&qp->r_lock, flags);
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	/*
160*4882a593Smuzhiyun 	 * Get the next work request entry to find where to put the data.
161*4882a593Smuzhiyun 	 */
162*4882a593Smuzhiyun 	if (qp->r_flags & RVT_R_REUSE_SGE) {
163*4882a593Smuzhiyun 		qp->r_flags &= ~RVT_R_REUSE_SGE;
164*4882a593Smuzhiyun 	} else {
165*4882a593Smuzhiyun 		int ret;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 		ret = rvt_get_rwqe(qp, false);
168*4882a593Smuzhiyun 		if (ret < 0) {
169*4882a593Smuzhiyun 			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
170*4882a593Smuzhiyun 			goto bail_unlock;
171*4882a593Smuzhiyun 		}
172*4882a593Smuzhiyun 		if (!ret) {
173*4882a593Smuzhiyun 			if (qp->ibqp.qp_num == 0)
174*4882a593Smuzhiyun 				ibp->rvp.n_vl15_dropped++;
175*4882a593Smuzhiyun 			goto bail_unlock;
176*4882a593Smuzhiyun 		}
177*4882a593Smuzhiyun 	}
178*4882a593Smuzhiyun 	/* Silently drop packets which are too big. */
179*4882a593Smuzhiyun 	if (unlikely(wc.byte_len > qp->r_len)) {
180*4882a593Smuzhiyun 		qp->r_flags |= RVT_R_REUSE_SGE;
181*4882a593Smuzhiyun 		ibp->rvp.n_pkt_drops++;
182*4882a593Smuzhiyun 		goto bail_unlock;
183*4882a593Smuzhiyun 	}
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
186*4882a593Smuzhiyun 		struct ib_grh grh;
187*4882a593Smuzhiyun 		struct ib_global_route grd = *(rdma_ah_read_grh(ah_attr));
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 		/*
190*4882a593Smuzhiyun 		 * For loopback packets with extended LIDs, the
191*4882a593Smuzhiyun 		 * sgid_index in the GRH is 0 and the dgid is
192*4882a593Smuzhiyun 		 * OPA GID of the sender. While creating a response
193*4882a593Smuzhiyun 		 * to the loopback packet, IB core creates the new
194*4882a593Smuzhiyun 		 * sgid_index from the DGID and that will be the
195*4882a593Smuzhiyun 		 * OPA_GID_INDEX. The new dgid is from the sgid
196*4882a593Smuzhiyun 		 * index and that will be in the IB GID format.
197*4882a593Smuzhiyun 		 *
198*4882a593Smuzhiyun 		 * We now have a case where the sent packet had a
199*4882a593Smuzhiyun 		 * different sgid_index and dgid compared to the
200*4882a593Smuzhiyun 		 * one that was received in response.
201*4882a593Smuzhiyun 		 *
202*4882a593Smuzhiyun 		 * Fix this inconsistency.
203*4882a593Smuzhiyun 		 */
204*4882a593Smuzhiyun 		if (priv->hdr_type == HFI1_PKT_TYPE_16B) {
205*4882a593Smuzhiyun 			if (grd.sgid_index == 0)
206*4882a593Smuzhiyun 				grd.sgid_index = OPA_GID_INDEX;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 			if (ib_is_opa_gid(&grd.dgid))
209*4882a593Smuzhiyun 				grd.dgid.global.interface_id =
210*4882a593Smuzhiyun 				cpu_to_be64(ppd->guids[HFI1_PORT_GUID_INDEX]);
211*4882a593Smuzhiyun 		}
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 		hfi1_make_grh(ibp, &grh, &grd, 0, 0);
214*4882a593Smuzhiyun 		rvt_copy_sge(qp, &qp->r_sge, &grh,
215*4882a593Smuzhiyun 			     sizeof(grh), true, false);
216*4882a593Smuzhiyun 		wc.wc_flags |= IB_WC_GRH;
217*4882a593Smuzhiyun 	} else {
218*4882a593Smuzhiyun 		rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
219*4882a593Smuzhiyun 	}
220*4882a593Smuzhiyun 	ssge.sg_list = swqe->sg_list + 1;
221*4882a593Smuzhiyun 	ssge.sge = *swqe->sg_list;
222*4882a593Smuzhiyun 	ssge.num_sge = swqe->wr.num_sge;
223*4882a593Smuzhiyun 	sge = &ssge.sge;
224*4882a593Smuzhiyun 	while (length) {
225*4882a593Smuzhiyun 		u32 len = rvt_get_sge_length(sge, length);
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 		WARN_ON_ONCE(len == 0);
228*4882a593Smuzhiyun 		rvt_copy_sge(qp, &qp->r_sge, sge->vaddr, len, true, false);
229*4882a593Smuzhiyun 		rvt_update_sge(&ssge, len, false);
230*4882a593Smuzhiyun 		length -= len;
231*4882a593Smuzhiyun 	}
232*4882a593Smuzhiyun 	rvt_put_ss(&qp->r_sge);
233*4882a593Smuzhiyun 	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
234*4882a593Smuzhiyun 		goto bail_unlock;
235*4882a593Smuzhiyun 	wc.wr_id = qp->r_wr_id;
236*4882a593Smuzhiyun 	wc.status = IB_WC_SUCCESS;
237*4882a593Smuzhiyun 	wc.opcode = IB_WC_RECV;
238*4882a593Smuzhiyun 	wc.qp = &qp->ibqp;
239*4882a593Smuzhiyun 	wc.src_qp = sqp->ibqp.qp_num;
240*4882a593Smuzhiyun 	if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI) {
241*4882a593Smuzhiyun 		if (sqp->ibqp.qp_type == IB_QPT_GSI ||
242*4882a593Smuzhiyun 		    sqp->ibqp.qp_type == IB_QPT_SMI)
243*4882a593Smuzhiyun 			wc.pkey_index = rvt_get_swqe_pkey_index(swqe);
244*4882a593Smuzhiyun 		else
245*4882a593Smuzhiyun 			wc.pkey_index = sqp->s_pkey_index;
246*4882a593Smuzhiyun 	} else {
247*4882a593Smuzhiyun 		wc.pkey_index = 0;
248*4882a593Smuzhiyun 	}
249*4882a593Smuzhiyun 	wc.slid = (ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
250*4882a593Smuzhiyun 				   ((1 << ppd->lmc) - 1))) & U16_MAX;
251*4882a593Smuzhiyun 	/* Check for loopback when the port lid is not set */
252*4882a593Smuzhiyun 	if (wc.slid == 0 && sqp->ibqp.qp_type == IB_QPT_GSI)
253*4882a593Smuzhiyun 		wc.slid = be16_to_cpu(IB_LID_PERMISSIVE);
254*4882a593Smuzhiyun 	wc.sl = rdma_ah_get_sl(ah_attr);
255*4882a593Smuzhiyun 	wc.dlid_path_bits = rdma_ah_get_dlid(ah_attr) & ((1 << ppd->lmc) - 1);
256*4882a593Smuzhiyun 	wc.port_num = qp->port_num;
257*4882a593Smuzhiyun 	/* Signal completion event if the solicited bit is set. */
258*4882a593Smuzhiyun 	rvt_recv_cq(qp, &wc, swqe->wr.send_flags & IB_SEND_SOLICITED);
259*4882a593Smuzhiyun 	ibp->rvp.n_loop_pkts++;
260*4882a593Smuzhiyun bail_unlock:
261*4882a593Smuzhiyun 	spin_unlock_irqrestore(&qp->r_lock, flags);
262*4882a593Smuzhiyun drop:
263*4882a593Smuzhiyun 	rcu_read_unlock();
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun 
hfi1_make_bth_deth(struct rvt_qp * qp,struct rvt_swqe * wqe,struct ib_other_headers * ohdr,u16 * pkey,u32 extra_bytes,bool bypass)266*4882a593Smuzhiyun static void hfi1_make_bth_deth(struct rvt_qp *qp, struct rvt_swqe *wqe,
267*4882a593Smuzhiyun 			       struct ib_other_headers *ohdr,
268*4882a593Smuzhiyun 			       u16 *pkey, u32 extra_bytes, bool bypass)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun 	u32 bth0;
271*4882a593Smuzhiyun 	struct hfi1_ibport *ibp;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	ibp = to_iport(qp->ibqp.device, qp->port_num);
274*4882a593Smuzhiyun 	if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
275*4882a593Smuzhiyun 		ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
276*4882a593Smuzhiyun 		bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
277*4882a593Smuzhiyun 	} else {
278*4882a593Smuzhiyun 		bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
279*4882a593Smuzhiyun 	}
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	if (wqe->wr.send_flags & IB_SEND_SOLICITED)
282*4882a593Smuzhiyun 		bth0 |= IB_BTH_SOLICITED;
283*4882a593Smuzhiyun 	bth0 |= extra_bytes << 20;
284*4882a593Smuzhiyun 	if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI)
285*4882a593Smuzhiyun 		*pkey = hfi1_get_pkey(ibp, rvt_get_swqe_pkey_index(wqe));
286*4882a593Smuzhiyun 	else
287*4882a593Smuzhiyun 		*pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
288*4882a593Smuzhiyun 	if (!bypass)
289*4882a593Smuzhiyun 		bth0 |= *pkey;
290*4882a593Smuzhiyun 	ohdr->bth[0] = cpu_to_be32(bth0);
291*4882a593Smuzhiyun 	ohdr->bth[1] = cpu_to_be32(rvt_get_swqe_remote_qpn(wqe));
292*4882a593Smuzhiyun 	ohdr->bth[2] = cpu_to_be32(mask_psn(wqe->psn));
293*4882a593Smuzhiyun 	/*
294*4882a593Smuzhiyun 	 * Qkeys with the high order bit set mean use the
295*4882a593Smuzhiyun 	 * qkey from the QP context instead of the WR (see 10.2.5).
296*4882a593Smuzhiyun 	 */
297*4882a593Smuzhiyun 	ohdr->u.ud.deth[0] =
298*4882a593Smuzhiyun 		cpu_to_be32((int)rvt_get_swqe_remote_qkey(wqe) < 0 ? qp->qkey :
299*4882a593Smuzhiyun 			    rvt_get_swqe_remote_qkey(wqe));
300*4882a593Smuzhiyun 	ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun 
hfi1_make_ud_req_9B(struct rvt_qp * qp,struct hfi1_pkt_state * ps,struct rvt_swqe * wqe)303*4882a593Smuzhiyun void hfi1_make_ud_req_9B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
304*4882a593Smuzhiyun 			 struct rvt_swqe *wqe)
305*4882a593Smuzhiyun {
306*4882a593Smuzhiyun 	u32 nwords, extra_bytes;
307*4882a593Smuzhiyun 	u16 len, slid, dlid, pkey;
308*4882a593Smuzhiyun 	u16 lrh0 = 0;
309*4882a593Smuzhiyun 	u8 sc5;
310*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
311*4882a593Smuzhiyun 	struct ib_other_headers *ohdr;
312*4882a593Smuzhiyun 	struct rdma_ah_attr *ah_attr;
313*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd;
314*4882a593Smuzhiyun 	struct hfi1_ibport *ibp;
315*4882a593Smuzhiyun 	struct ib_grh *grh;
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	ibp = to_iport(qp->ibqp.device, qp->port_num);
318*4882a593Smuzhiyun 	ppd = ppd_from_ibp(ibp);
319*4882a593Smuzhiyun 	ah_attr = rvt_get_swqe_ah_attr(wqe);
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	extra_bytes = -wqe->length & 3;
322*4882a593Smuzhiyun 	nwords = ((wqe->length + extra_bytes) >> 2) + SIZE_OF_CRC;
323*4882a593Smuzhiyun 	/* header size in dwords LRH+BTH+DETH = (8+12+8)/4. */
324*4882a593Smuzhiyun 	ps->s_txreq->hdr_dwords = 7;
325*4882a593Smuzhiyun 	if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
326*4882a593Smuzhiyun 		ps->s_txreq->hdr_dwords++;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
329*4882a593Smuzhiyun 		grh = &ps->s_txreq->phdr.hdr.ibh.u.l.grh;
330*4882a593Smuzhiyun 		ps->s_txreq->hdr_dwords +=
331*4882a593Smuzhiyun 			hfi1_make_grh(ibp, grh, rdma_ah_read_grh(ah_attr),
332*4882a593Smuzhiyun 				      ps->s_txreq->hdr_dwords - LRH_9B_DWORDS,
333*4882a593Smuzhiyun 				      nwords);
334*4882a593Smuzhiyun 		lrh0 = HFI1_LRH_GRH;
335*4882a593Smuzhiyun 		ohdr = &ps->s_txreq->phdr.hdr.ibh.u.l.oth;
336*4882a593Smuzhiyun 	} else {
337*4882a593Smuzhiyun 		lrh0 = HFI1_LRH_BTH;
338*4882a593Smuzhiyun 		ohdr = &ps->s_txreq->phdr.hdr.ibh.u.oth;
339*4882a593Smuzhiyun 	}
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
342*4882a593Smuzhiyun 	lrh0 |= (rdma_ah_get_sl(ah_attr) & 0xf) << 4;
343*4882a593Smuzhiyun 	if (qp->ibqp.qp_type == IB_QPT_SMI) {
344*4882a593Smuzhiyun 		lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
345*4882a593Smuzhiyun 		priv->s_sc = 0xf;
346*4882a593Smuzhiyun 	} else {
347*4882a593Smuzhiyun 		lrh0 |= (sc5 & 0xf) << 12;
348*4882a593Smuzhiyun 		priv->s_sc = sc5;
349*4882a593Smuzhiyun 	}
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 9B);
352*4882a593Smuzhiyun 	if (dlid == be16_to_cpu(IB_LID_PERMISSIVE)) {
353*4882a593Smuzhiyun 		slid = be16_to_cpu(IB_LID_PERMISSIVE);
354*4882a593Smuzhiyun 	} else {
355*4882a593Smuzhiyun 		u16 lid = (u16)ppd->lid;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 		if (lid) {
358*4882a593Smuzhiyun 			lid |= rdma_ah_get_path_bits(ah_attr) &
359*4882a593Smuzhiyun 				((1 << ppd->lmc) - 1);
360*4882a593Smuzhiyun 			slid = lid;
361*4882a593Smuzhiyun 		} else {
362*4882a593Smuzhiyun 			slid = be16_to_cpu(IB_LID_PERMISSIVE);
363*4882a593Smuzhiyun 		}
364*4882a593Smuzhiyun 	}
365*4882a593Smuzhiyun 	hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, false);
366*4882a593Smuzhiyun 	len = ps->s_txreq->hdr_dwords + nwords;
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	/* Setup the packet */
369*4882a593Smuzhiyun 	ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_9B;
370*4882a593Smuzhiyun 	hfi1_make_ib_hdr(&ps->s_txreq->phdr.hdr.ibh,
371*4882a593Smuzhiyun 			 lrh0, len, dlid, slid);
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun 
hfi1_make_ud_req_16B(struct rvt_qp * qp,struct hfi1_pkt_state * ps,struct rvt_swqe * wqe)374*4882a593Smuzhiyun void hfi1_make_ud_req_16B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
375*4882a593Smuzhiyun 			  struct rvt_swqe *wqe)
376*4882a593Smuzhiyun {
377*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
378*4882a593Smuzhiyun 	struct ib_other_headers *ohdr;
379*4882a593Smuzhiyun 	struct rdma_ah_attr *ah_attr;
380*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd;
381*4882a593Smuzhiyun 	struct hfi1_ibport *ibp;
382*4882a593Smuzhiyun 	u32 dlid, slid, nwords, extra_bytes;
383*4882a593Smuzhiyun 	u32 dest_qp = rvt_get_swqe_remote_qpn(wqe);
384*4882a593Smuzhiyun 	u32 src_qp = qp->ibqp.qp_num;
385*4882a593Smuzhiyun 	u16 len, pkey;
386*4882a593Smuzhiyun 	u8 l4, sc5;
387*4882a593Smuzhiyun 	bool is_mgmt = false;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	ibp = to_iport(qp->ibqp.device, qp->port_num);
390*4882a593Smuzhiyun 	ppd = ppd_from_ibp(ibp);
391*4882a593Smuzhiyun 	ah_attr = rvt_get_swqe_ah_attr(wqe);
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	/*
394*4882a593Smuzhiyun 	 * Build 16B Management Packet if either the destination
395*4882a593Smuzhiyun 	 * or source queue pair number is 0 or 1.
396*4882a593Smuzhiyun 	 */
397*4882a593Smuzhiyun 	if (dest_qp == 0 || src_qp == 0 || dest_qp == 1 || src_qp == 1) {
398*4882a593Smuzhiyun 		/* header size in dwords 16B LRH+L4_FM = (16+8)/4. */
399*4882a593Smuzhiyun 		ps->s_txreq->hdr_dwords = 6;
400*4882a593Smuzhiyun 		is_mgmt = true;
401*4882a593Smuzhiyun 	} else {
402*4882a593Smuzhiyun 		/* header size in dwords 16B LRH+BTH+DETH = (16+12+8)/4. */
403*4882a593Smuzhiyun 		ps->s_txreq->hdr_dwords = 9;
404*4882a593Smuzhiyun 		if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
405*4882a593Smuzhiyun 			ps->s_txreq->hdr_dwords++;
406*4882a593Smuzhiyun 	}
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	/* SW provides space for CRC and LT for bypass packets. */
409*4882a593Smuzhiyun 	extra_bytes = hfi1_get_16b_padding((ps->s_txreq->hdr_dwords << 2),
410*4882a593Smuzhiyun 					   wqe->length);
411*4882a593Smuzhiyun 	nwords = ((wqe->length + extra_bytes + SIZE_OF_LT) >> 2) + SIZE_OF_CRC;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	if ((rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) &&
414*4882a593Smuzhiyun 	    hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) {
415*4882a593Smuzhiyun 		struct ib_grh *grh;
416*4882a593Smuzhiyun 		struct ib_global_route *grd = rdma_ah_retrieve_grh(ah_attr);
417*4882a593Smuzhiyun 		/*
418*4882a593Smuzhiyun 		 * Ensure OPA GIDs are transformed to IB gids
419*4882a593Smuzhiyun 		 * before creating the GRH.
420*4882a593Smuzhiyun 		 */
421*4882a593Smuzhiyun 		if (grd->sgid_index == OPA_GID_INDEX) {
422*4882a593Smuzhiyun 			dd_dev_warn(ppd->dd, "Bad sgid_index. sgid_index: %d\n",
423*4882a593Smuzhiyun 				    grd->sgid_index);
424*4882a593Smuzhiyun 			grd->sgid_index = 0;
425*4882a593Smuzhiyun 		}
426*4882a593Smuzhiyun 		grh = &ps->s_txreq->phdr.hdr.opah.u.l.grh;
427*4882a593Smuzhiyun 		ps->s_txreq->hdr_dwords += hfi1_make_grh(
428*4882a593Smuzhiyun 			ibp, grh, grd,
429*4882a593Smuzhiyun 			ps->s_txreq->hdr_dwords - LRH_16B_DWORDS,
430*4882a593Smuzhiyun 			nwords);
431*4882a593Smuzhiyun 		ohdr = &ps->s_txreq->phdr.hdr.opah.u.l.oth;
432*4882a593Smuzhiyun 		l4 = OPA_16B_L4_IB_GLOBAL;
433*4882a593Smuzhiyun 	} else {
434*4882a593Smuzhiyun 		ohdr = &ps->s_txreq->phdr.hdr.opah.u.oth;
435*4882a593Smuzhiyun 		l4 = OPA_16B_L4_IB_LOCAL;
436*4882a593Smuzhiyun 	}
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
439*4882a593Smuzhiyun 	if (qp->ibqp.qp_type == IB_QPT_SMI)
440*4882a593Smuzhiyun 		priv->s_sc = 0xf;
441*4882a593Smuzhiyun 	else
442*4882a593Smuzhiyun 		priv->s_sc = sc5;
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 16B);
445*4882a593Smuzhiyun 	if (!ppd->lid)
446*4882a593Smuzhiyun 		slid = be32_to_cpu(OPA_LID_PERMISSIVE);
447*4882a593Smuzhiyun 	else
448*4882a593Smuzhiyun 		slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
449*4882a593Smuzhiyun 			   ((1 << ppd->lmc) - 1));
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	if (is_mgmt) {
452*4882a593Smuzhiyun 		l4 = OPA_16B_L4_FM;
453*4882a593Smuzhiyun 		pkey = hfi1_get_pkey(ibp, rvt_get_swqe_pkey_index(wqe));
454*4882a593Smuzhiyun 		hfi1_16B_set_qpn(&ps->s_txreq->phdr.hdr.opah.u.mgmt,
455*4882a593Smuzhiyun 				 dest_qp, src_qp);
456*4882a593Smuzhiyun 	} else {
457*4882a593Smuzhiyun 		hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, true);
458*4882a593Smuzhiyun 	}
459*4882a593Smuzhiyun 	/* Convert dwords to flits */
460*4882a593Smuzhiyun 	len = (ps->s_txreq->hdr_dwords + nwords) >> 1;
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	/* Setup the packet */
463*4882a593Smuzhiyun 	ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_16B;
464*4882a593Smuzhiyun 	hfi1_make_16b_hdr(&ps->s_txreq->phdr.hdr.opah,
465*4882a593Smuzhiyun 			  slid, dlid, len, pkey, 0, 0, l4, priv->s_sc);
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun /**
469*4882a593Smuzhiyun  * hfi1_make_ud_req - construct a UD request packet
470*4882a593Smuzhiyun  * @qp: the QP
471*4882a593Smuzhiyun  *
472*4882a593Smuzhiyun  * Assume s_lock is held.
473*4882a593Smuzhiyun  *
474*4882a593Smuzhiyun  * Return 1 if constructed; otherwise, return 0.
475*4882a593Smuzhiyun  */
hfi1_make_ud_req(struct rvt_qp * qp,struct hfi1_pkt_state * ps)476*4882a593Smuzhiyun int hfi1_make_ud_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
479*4882a593Smuzhiyun 	struct rdma_ah_attr *ah_attr;
480*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd;
481*4882a593Smuzhiyun 	struct hfi1_ibport *ibp;
482*4882a593Smuzhiyun 	struct rvt_swqe *wqe;
483*4882a593Smuzhiyun 	int next_cur;
484*4882a593Smuzhiyun 	u32 lid;
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	ps->s_txreq = get_txreq(ps->dev, qp);
487*4882a593Smuzhiyun 	if (!ps->s_txreq)
488*4882a593Smuzhiyun 		goto bail_no_tx;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
491*4882a593Smuzhiyun 		if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
492*4882a593Smuzhiyun 			goto bail;
493*4882a593Smuzhiyun 		/* We are in the error state, flush the work request. */
494*4882a593Smuzhiyun 		if (qp->s_last == READ_ONCE(qp->s_head))
495*4882a593Smuzhiyun 			goto bail;
496*4882a593Smuzhiyun 		/* If DMAs are in progress, we can't flush immediately. */
497*4882a593Smuzhiyun 		if (iowait_sdma_pending(&priv->s_iowait)) {
498*4882a593Smuzhiyun 			qp->s_flags |= RVT_S_WAIT_DMA;
499*4882a593Smuzhiyun 			goto bail;
500*4882a593Smuzhiyun 		}
501*4882a593Smuzhiyun 		wqe = rvt_get_swqe_ptr(qp, qp->s_last);
502*4882a593Smuzhiyun 		rvt_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
503*4882a593Smuzhiyun 		goto done_free_tx;
504*4882a593Smuzhiyun 	}
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	/* see post_one_send() */
507*4882a593Smuzhiyun 	if (qp->s_cur == READ_ONCE(qp->s_head))
508*4882a593Smuzhiyun 		goto bail;
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
511*4882a593Smuzhiyun 	next_cur = qp->s_cur + 1;
512*4882a593Smuzhiyun 	if (next_cur >= qp->s_size)
513*4882a593Smuzhiyun 		next_cur = 0;
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	/* Construct the header. */
516*4882a593Smuzhiyun 	ibp = to_iport(qp->ibqp.device, qp->port_num);
517*4882a593Smuzhiyun 	ppd = ppd_from_ibp(ibp);
518*4882a593Smuzhiyun 	ah_attr = rvt_get_swqe_ah_attr(wqe);
519*4882a593Smuzhiyun 	priv->hdr_type = hfi1_get_hdr_type(ppd->lid, ah_attr);
520*4882a593Smuzhiyun 	if ((!hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) ||
521*4882a593Smuzhiyun 	    (rdma_ah_get_dlid(ah_attr) == be32_to_cpu(OPA_LID_PERMISSIVE))) {
522*4882a593Smuzhiyun 		lid = rdma_ah_get_dlid(ah_attr) & ~((1 << ppd->lmc) - 1);
523*4882a593Smuzhiyun 		if (unlikely(!loopback &&
524*4882a593Smuzhiyun 			     ((lid == ppd->lid) ||
525*4882a593Smuzhiyun 			      ((lid == be32_to_cpu(OPA_LID_PERMISSIVE)) &&
526*4882a593Smuzhiyun 			       (qp->ibqp.qp_type == IB_QPT_GSI))))) {
527*4882a593Smuzhiyun 			unsigned long tflags = ps->flags;
528*4882a593Smuzhiyun 			/*
529*4882a593Smuzhiyun 			 * If DMAs are in progress, we can't generate
530*4882a593Smuzhiyun 			 * a completion for the loopback packet since
531*4882a593Smuzhiyun 			 * it would be out of order.
532*4882a593Smuzhiyun 			 * Instead of waiting, we could queue a
533*4882a593Smuzhiyun 			 * zero length descriptor so we get a callback.
534*4882a593Smuzhiyun 			 */
535*4882a593Smuzhiyun 			if (iowait_sdma_pending(&priv->s_iowait)) {
536*4882a593Smuzhiyun 				qp->s_flags |= RVT_S_WAIT_DMA;
537*4882a593Smuzhiyun 				goto bail;
538*4882a593Smuzhiyun 			}
539*4882a593Smuzhiyun 			qp->s_cur = next_cur;
540*4882a593Smuzhiyun 			spin_unlock_irqrestore(&qp->s_lock, tflags);
541*4882a593Smuzhiyun 			ud_loopback(qp, wqe);
542*4882a593Smuzhiyun 			spin_lock_irqsave(&qp->s_lock, tflags);
543*4882a593Smuzhiyun 			ps->flags = tflags;
544*4882a593Smuzhiyun 			rvt_send_complete(qp, wqe, IB_WC_SUCCESS);
545*4882a593Smuzhiyun 			goto done_free_tx;
546*4882a593Smuzhiyun 		}
547*4882a593Smuzhiyun 	}
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	qp->s_cur = next_cur;
550*4882a593Smuzhiyun 	ps->s_txreq->s_cur_size = wqe->length;
551*4882a593Smuzhiyun 	ps->s_txreq->ss = &qp->s_sge;
552*4882a593Smuzhiyun 	qp->s_srate = rdma_ah_get_static_rate(ah_attr);
553*4882a593Smuzhiyun 	qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
554*4882a593Smuzhiyun 	qp->s_wqe = wqe;
555*4882a593Smuzhiyun 	qp->s_sge.sge = wqe->sg_list[0];
556*4882a593Smuzhiyun 	qp->s_sge.sg_list = wqe->sg_list + 1;
557*4882a593Smuzhiyun 	qp->s_sge.num_sge = wqe->wr.num_sge;
558*4882a593Smuzhiyun 	qp->s_sge.total_len = wqe->length;
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	/* Make the appropriate header */
561*4882a593Smuzhiyun 	hfi1_make_ud_req_tbl[priv->hdr_type](qp, ps, qp->s_wqe);
562*4882a593Smuzhiyun 	priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
563*4882a593Smuzhiyun 	ps->s_txreq->sde = priv->s_sde;
564*4882a593Smuzhiyun 	priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
565*4882a593Smuzhiyun 	ps->s_txreq->psc = priv->s_sendcontext;
566*4882a593Smuzhiyun 	/* disarm any ahg */
567*4882a593Smuzhiyun 	priv->s_ahg->ahgcount = 0;
568*4882a593Smuzhiyun 	priv->s_ahg->ahgidx = 0;
569*4882a593Smuzhiyun 	priv->s_ahg->tx_flags = 0;
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 	return 1;
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun done_free_tx:
574*4882a593Smuzhiyun 	hfi1_put_txreq(ps->s_txreq);
575*4882a593Smuzhiyun 	ps->s_txreq = NULL;
576*4882a593Smuzhiyun 	return 1;
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun bail:
579*4882a593Smuzhiyun 	hfi1_put_txreq(ps->s_txreq);
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun bail_no_tx:
582*4882a593Smuzhiyun 	ps->s_txreq = NULL;
583*4882a593Smuzhiyun 	qp->s_flags &= ~RVT_S_BUSY;
584*4882a593Smuzhiyun 	return 0;
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun /*
588*4882a593Smuzhiyun  * Hardware can't check this so we do it here.
589*4882a593Smuzhiyun  *
590*4882a593Smuzhiyun  * This is a slightly different algorithm than the standard pkey check.  It
591*4882a593Smuzhiyun  * special cases the management keys and allows for 0x7fff and 0xffff to be in
592*4882a593Smuzhiyun  * the table at the same time.
593*4882a593Smuzhiyun  *
594*4882a593Smuzhiyun  * @returns the index found or -1 if not found
595*4882a593Smuzhiyun  */
hfi1_lookup_pkey_idx(struct hfi1_ibport * ibp,u16 pkey)596*4882a593Smuzhiyun int hfi1_lookup_pkey_idx(struct hfi1_ibport *ibp, u16 pkey)
597*4882a593Smuzhiyun {
598*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
599*4882a593Smuzhiyun 	unsigned i;
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	if (pkey == FULL_MGMT_P_KEY || pkey == LIM_MGMT_P_KEY) {
602*4882a593Smuzhiyun 		unsigned lim_idx = -1;
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 		for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i) {
605*4882a593Smuzhiyun 			/* here we look for an exact match */
606*4882a593Smuzhiyun 			if (ppd->pkeys[i] == pkey)
607*4882a593Smuzhiyun 				return i;
608*4882a593Smuzhiyun 			if (ppd->pkeys[i] == LIM_MGMT_P_KEY)
609*4882a593Smuzhiyun 				lim_idx = i;
610*4882a593Smuzhiyun 		}
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 		/* did not find 0xffff return 0x7fff idx if found */
613*4882a593Smuzhiyun 		if (pkey == FULL_MGMT_P_KEY)
614*4882a593Smuzhiyun 			return lim_idx;
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 		/* no match...  */
617*4882a593Smuzhiyun 		return -1;
618*4882a593Smuzhiyun 	}
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	pkey &= 0x7fff; /* remove limited/full membership bit */
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i)
623*4882a593Smuzhiyun 		if ((ppd->pkeys[i] & 0x7fff) == pkey)
624*4882a593Smuzhiyun 			return i;
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun 	/*
627*4882a593Smuzhiyun 	 * Should not get here, this means hardware failed to validate pkeys.
628*4882a593Smuzhiyun 	 */
629*4882a593Smuzhiyun 	return -1;
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun 
return_cnp_16B(struct hfi1_ibport * ibp,struct rvt_qp * qp,u32 remote_qpn,u16 pkey,u32 slid,u32 dlid,u8 sc5,const struct ib_grh * old_grh)632*4882a593Smuzhiyun void return_cnp_16B(struct hfi1_ibport *ibp, struct rvt_qp *qp,
633*4882a593Smuzhiyun 		    u32 remote_qpn, u16 pkey, u32 slid, u32 dlid,
634*4882a593Smuzhiyun 		    u8 sc5, const struct ib_grh *old_grh)
635*4882a593Smuzhiyun {
636*4882a593Smuzhiyun 	u64 pbc, pbc_flags = 0;
637*4882a593Smuzhiyun 	u32 bth0, plen, vl, hwords = 7;
638*4882a593Smuzhiyun 	u16 len;
639*4882a593Smuzhiyun 	u8 l4;
640*4882a593Smuzhiyun 	struct hfi1_opa_header hdr;
641*4882a593Smuzhiyun 	struct ib_other_headers *ohdr;
642*4882a593Smuzhiyun 	struct pio_buf *pbuf;
643*4882a593Smuzhiyun 	struct send_context *ctxt = qp_to_send_context(qp, sc5);
644*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
645*4882a593Smuzhiyun 	u32 nwords;
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun 	hdr.hdr_type = HFI1_PKT_TYPE_16B;
648*4882a593Smuzhiyun 	/* Populate length */
649*4882a593Smuzhiyun 	nwords = ((hfi1_get_16b_padding(hwords << 2, 0) +
650*4882a593Smuzhiyun 		   SIZE_OF_LT) >> 2) + SIZE_OF_CRC;
651*4882a593Smuzhiyun 	if (old_grh) {
652*4882a593Smuzhiyun 		struct ib_grh *grh = &hdr.opah.u.l.grh;
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 		grh->version_tclass_flow = old_grh->version_tclass_flow;
655*4882a593Smuzhiyun 		grh->paylen = cpu_to_be16(
656*4882a593Smuzhiyun 			(hwords - LRH_16B_DWORDS + nwords) << 2);
657*4882a593Smuzhiyun 		grh->hop_limit = 0xff;
658*4882a593Smuzhiyun 		grh->sgid = old_grh->dgid;
659*4882a593Smuzhiyun 		grh->dgid = old_grh->sgid;
660*4882a593Smuzhiyun 		ohdr = &hdr.opah.u.l.oth;
661*4882a593Smuzhiyun 		l4 = OPA_16B_L4_IB_GLOBAL;
662*4882a593Smuzhiyun 		hwords += sizeof(struct ib_grh) / sizeof(u32);
663*4882a593Smuzhiyun 	} else {
664*4882a593Smuzhiyun 		ohdr = &hdr.opah.u.oth;
665*4882a593Smuzhiyun 		l4 = OPA_16B_L4_IB_LOCAL;
666*4882a593Smuzhiyun 	}
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	/* BIT 16 to 19 is TVER. Bit 20 to 22 is pad cnt */
669*4882a593Smuzhiyun 	bth0 = (IB_OPCODE_CNP << 24) | (1 << 16) |
670*4882a593Smuzhiyun 	       (hfi1_get_16b_padding(hwords << 2, 0) << 20);
671*4882a593Smuzhiyun 	ohdr->bth[0] = cpu_to_be32(bth0);
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	ohdr->bth[1] = cpu_to_be32(remote_qpn);
674*4882a593Smuzhiyun 	ohdr->bth[2] = 0; /* PSN 0 */
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	/* Convert dwords to flits */
677*4882a593Smuzhiyun 	len = (hwords + nwords) >> 1;
678*4882a593Smuzhiyun 	hfi1_make_16b_hdr(&hdr.opah, slid, dlid, len, pkey, 1, 0, l4, sc5);
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	plen = 2 /* PBC */ + hwords + nwords;
681*4882a593Smuzhiyun 	pbc_flags |= PBC_PACKET_BYPASS | PBC_INSERT_BYPASS_ICRC;
682*4882a593Smuzhiyun 	vl = sc_to_vlt(ppd->dd, sc5);
683*4882a593Smuzhiyun 	pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
684*4882a593Smuzhiyun 	if (ctxt) {
685*4882a593Smuzhiyun 		pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
686*4882a593Smuzhiyun 		if (!IS_ERR_OR_NULL(pbuf)) {
687*4882a593Smuzhiyun 			trace_pio_output_ibhdr(ppd->dd, &hdr, sc5);
688*4882a593Smuzhiyun 			ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
689*4882a593Smuzhiyun 						 &hdr, hwords);
690*4882a593Smuzhiyun 		}
691*4882a593Smuzhiyun 	}
692*4882a593Smuzhiyun }
693*4882a593Smuzhiyun 
return_cnp(struct hfi1_ibport * ibp,struct rvt_qp * qp,u32 remote_qpn,u16 pkey,u32 slid,u32 dlid,u8 sc5,const struct ib_grh * old_grh)694*4882a593Smuzhiyun void return_cnp(struct hfi1_ibport *ibp, struct rvt_qp *qp, u32 remote_qpn,
695*4882a593Smuzhiyun 		u16 pkey, u32 slid, u32 dlid, u8 sc5,
696*4882a593Smuzhiyun 		const struct ib_grh *old_grh)
697*4882a593Smuzhiyun {
698*4882a593Smuzhiyun 	u64 pbc, pbc_flags = 0;
699*4882a593Smuzhiyun 	u32 bth0, plen, vl, hwords = 5;
700*4882a593Smuzhiyun 	u16 lrh0;
701*4882a593Smuzhiyun 	u8 sl = ibp->sc_to_sl[sc5];
702*4882a593Smuzhiyun 	struct hfi1_opa_header hdr;
703*4882a593Smuzhiyun 	struct ib_other_headers *ohdr;
704*4882a593Smuzhiyun 	struct pio_buf *pbuf;
705*4882a593Smuzhiyun 	struct send_context *ctxt = qp_to_send_context(qp, sc5);
706*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun 	hdr.hdr_type = HFI1_PKT_TYPE_9B;
709*4882a593Smuzhiyun 	if (old_grh) {
710*4882a593Smuzhiyun 		struct ib_grh *grh = &hdr.ibh.u.l.grh;
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun 		grh->version_tclass_flow = old_grh->version_tclass_flow;
713*4882a593Smuzhiyun 		grh->paylen = cpu_to_be16(
714*4882a593Smuzhiyun 			(hwords - LRH_9B_DWORDS + SIZE_OF_CRC) << 2);
715*4882a593Smuzhiyun 		grh->hop_limit = 0xff;
716*4882a593Smuzhiyun 		grh->sgid = old_grh->dgid;
717*4882a593Smuzhiyun 		grh->dgid = old_grh->sgid;
718*4882a593Smuzhiyun 		ohdr = &hdr.ibh.u.l.oth;
719*4882a593Smuzhiyun 		lrh0 = HFI1_LRH_GRH;
720*4882a593Smuzhiyun 		hwords += sizeof(struct ib_grh) / sizeof(u32);
721*4882a593Smuzhiyun 	} else {
722*4882a593Smuzhiyun 		ohdr = &hdr.ibh.u.oth;
723*4882a593Smuzhiyun 		lrh0 = HFI1_LRH_BTH;
724*4882a593Smuzhiyun 	}
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	lrh0 |= (sc5 & 0xf) << 12 | sl << 4;
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	bth0 = pkey | (IB_OPCODE_CNP << 24);
729*4882a593Smuzhiyun 	ohdr->bth[0] = cpu_to_be32(bth0);
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	ohdr->bth[1] = cpu_to_be32(remote_qpn | (1 << IB_BECN_SHIFT));
732*4882a593Smuzhiyun 	ohdr->bth[2] = 0; /* PSN 0 */
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	hfi1_make_ib_hdr(&hdr.ibh, lrh0, hwords + SIZE_OF_CRC, dlid, slid);
735*4882a593Smuzhiyun 	plen = 2 /* PBC */ + hwords;
736*4882a593Smuzhiyun 	pbc_flags |= (ib_is_sc5(sc5) << PBC_DC_INFO_SHIFT);
737*4882a593Smuzhiyun 	vl = sc_to_vlt(ppd->dd, sc5);
738*4882a593Smuzhiyun 	pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
739*4882a593Smuzhiyun 	if (ctxt) {
740*4882a593Smuzhiyun 		pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
741*4882a593Smuzhiyun 		if (!IS_ERR_OR_NULL(pbuf)) {
742*4882a593Smuzhiyun 			trace_pio_output_ibhdr(ppd->dd, &hdr, sc5);
743*4882a593Smuzhiyun 			ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
744*4882a593Smuzhiyun 						 &hdr, hwords);
745*4882a593Smuzhiyun 		}
746*4882a593Smuzhiyun 	}
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun /*
750*4882a593Smuzhiyun  * opa_smp_check() - Do the regular pkey checking, and the additional
751*4882a593Smuzhiyun  * checks for SMPs specified in OPAv1 rev 1.0, 9/19/2016 update, section
752*4882a593Smuzhiyun  * 9.10.25 ("SMA Packet Checks").
753*4882a593Smuzhiyun  *
754*4882a593Smuzhiyun  * Note that:
755*4882a593Smuzhiyun  *   - Checks are done using the pkey directly from the packet's BTH,
756*4882a593Smuzhiyun  *     and specifically _not_ the pkey that we attach to the completion,
757*4882a593Smuzhiyun  *     which may be different.
758*4882a593Smuzhiyun  *   - These checks are specifically for "non-local" SMPs (i.e., SMPs
759*4882a593Smuzhiyun  *     which originated on another node). SMPs which are sent from, and
760*4882a593Smuzhiyun  *     destined to this node are checked in opa_local_smp_check().
761*4882a593Smuzhiyun  *
762*4882a593Smuzhiyun  * At the point where opa_smp_check() is called, we know:
763*4882a593Smuzhiyun  *   - destination QP is QP0
764*4882a593Smuzhiyun  *
765*4882a593Smuzhiyun  * opa_smp_check() returns 0 if all checks succeed, 1 otherwise.
766*4882a593Smuzhiyun  */
opa_smp_check(struct hfi1_ibport * ibp,u16 pkey,u8 sc5,struct rvt_qp * qp,u16 slid,struct opa_smp * smp)767*4882a593Smuzhiyun static int opa_smp_check(struct hfi1_ibport *ibp, u16 pkey, u8 sc5,
768*4882a593Smuzhiyun 			 struct rvt_qp *qp, u16 slid, struct opa_smp *smp)
769*4882a593Smuzhiyun {
770*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 	/*
773*4882a593Smuzhiyun 	 * I don't think it's possible for us to get here with sc != 0xf,
774*4882a593Smuzhiyun 	 * but check it to be certain.
775*4882a593Smuzhiyun 	 */
776*4882a593Smuzhiyun 	if (sc5 != 0xf)
777*4882a593Smuzhiyun 		return 1;
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 	if (rcv_pkey_check(ppd, pkey, sc5, slid))
780*4882a593Smuzhiyun 		return 1;
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	/*
783*4882a593Smuzhiyun 	 * At this point we know (and so don't need to check again) that
784*4882a593Smuzhiyun 	 * the pkey is either LIM_MGMT_P_KEY, or FULL_MGMT_P_KEY
785*4882a593Smuzhiyun 	 * (see ingress_pkey_check).
786*4882a593Smuzhiyun 	 */
787*4882a593Smuzhiyun 	if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE &&
788*4882a593Smuzhiyun 	    smp->mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED) {
789*4882a593Smuzhiyun 		ingress_pkey_table_fail(ppd, pkey, slid);
790*4882a593Smuzhiyun 		return 1;
791*4882a593Smuzhiyun 	}
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 	/*
794*4882a593Smuzhiyun 	 * SMPs fall into one of four (disjoint) categories:
795*4882a593Smuzhiyun 	 * SMA request, SMA response, SMA trap, or SMA trap repress.
796*4882a593Smuzhiyun 	 * Our response depends, in part, on which type of SMP we're
797*4882a593Smuzhiyun 	 * processing.
798*4882a593Smuzhiyun 	 *
799*4882a593Smuzhiyun 	 * If this is an SMA response, skip the check here.
800*4882a593Smuzhiyun 	 *
801*4882a593Smuzhiyun 	 * If this is an SMA request or SMA trap repress:
802*4882a593Smuzhiyun 	 *   - pkey != FULL_MGMT_P_KEY =>
803*4882a593Smuzhiyun 	 *       increment port recv constraint errors, drop MAD
804*4882a593Smuzhiyun 	 *
805*4882a593Smuzhiyun 	 * Otherwise:
806*4882a593Smuzhiyun 	 *    - accept if the port is running an SM
807*4882a593Smuzhiyun 	 *    - drop MAD if it's an SMA trap
808*4882a593Smuzhiyun 	 *    - pkey == FULL_MGMT_P_KEY =>
809*4882a593Smuzhiyun 	 *        reply with unsupported method
810*4882a593Smuzhiyun 	 *    - pkey != FULL_MGMT_P_KEY =>
811*4882a593Smuzhiyun 	 *	  increment port recv constraint errors, drop MAD
812*4882a593Smuzhiyun 	 */
813*4882a593Smuzhiyun 	switch (smp->method) {
814*4882a593Smuzhiyun 	case IB_MGMT_METHOD_GET_RESP:
815*4882a593Smuzhiyun 	case IB_MGMT_METHOD_REPORT_RESP:
816*4882a593Smuzhiyun 		break;
817*4882a593Smuzhiyun 	case IB_MGMT_METHOD_GET:
818*4882a593Smuzhiyun 	case IB_MGMT_METHOD_SET:
819*4882a593Smuzhiyun 	case IB_MGMT_METHOD_REPORT:
820*4882a593Smuzhiyun 	case IB_MGMT_METHOD_TRAP_REPRESS:
821*4882a593Smuzhiyun 		if (pkey != FULL_MGMT_P_KEY) {
822*4882a593Smuzhiyun 			ingress_pkey_table_fail(ppd, pkey, slid);
823*4882a593Smuzhiyun 			return 1;
824*4882a593Smuzhiyun 		}
825*4882a593Smuzhiyun 		break;
826*4882a593Smuzhiyun 	default:
827*4882a593Smuzhiyun 		if (ibp->rvp.port_cap_flags & IB_PORT_SM)
828*4882a593Smuzhiyun 			return 0;
829*4882a593Smuzhiyun 		if (smp->method == IB_MGMT_METHOD_TRAP)
830*4882a593Smuzhiyun 			return 1;
831*4882a593Smuzhiyun 		if (pkey == FULL_MGMT_P_KEY) {
832*4882a593Smuzhiyun 			smp->status |= IB_SMP_UNSUP_METHOD;
833*4882a593Smuzhiyun 			return 0;
834*4882a593Smuzhiyun 		}
835*4882a593Smuzhiyun 		ingress_pkey_table_fail(ppd, pkey, slid);
836*4882a593Smuzhiyun 		return 1;
837*4882a593Smuzhiyun 	}
838*4882a593Smuzhiyun 	return 0;
839*4882a593Smuzhiyun }
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun /**
842*4882a593Smuzhiyun  * hfi1_ud_rcv - receive an incoming UD packet
843*4882a593Smuzhiyun  * @ibp: the port the packet came in on
844*4882a593Smuzhiyun  * @hdr: the packet header
845*4882a593Smuzhiyun  * @rcv_flags: flags relevant to rcv processing
846*4882a593Smuzhiyun  * @data: the packet data
847*4882a593Smuzhiyun  * @tlen: the packet length
848*4882a593Smuzhiyun  * @qp: the QP the packet came on
849*4882a593Smuzhiyun  *
850*4882a593Smuzhiyun  * This is called from qp_rcv() to process an incoming UD packet
851*4882a593Smuzhiyun  * for the given QP.
852*4882a593Smuzhiyun  * Called at interrupt level.
853*4882a593Smuzhiyun  */
hfi1_ud_rcv(struct hfi1_packet * packet)854*4882a593Smuzhiyun void hfi1_ud_rcv(struct hfi1_packet *packet)
855*4882a593Smuzhiyun {
856*4882a593Smuzhiyun 	u32 hdrsize = packet->hlen;
857*4882a593Smuzhiyun 	struct ib_wc wc;
858*4882a593Smuzhiyun 	u32 src_qp;
859*4882a593Smuzhiyun 	u16 pkey;
860*4882a593Smuzhiyun 	int mgmt_pkey_idx = -1;
861*4882a593Smuzhiyun 	struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
862*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
863*4882a593Smuzhiyun 	void *data = packet->payload;
864*4882a593Smuzhiyun 	u32 tlen = packet->tlen;
865*4882a593Smuzhiyun 	struct rvt_qp *qp = packet->qp;
866*4882a593Smuzhiyun 	u8 sc5 = packet->sc;
867*4882a593Smuzhiyun 	u8 sl_from_sc;
868*4882a593Smuzhiyun 	u8 opcode = packet->opcode;
869*4882a593Smuzhiyun 	u8 sl = packet->sl;
870*4882a593Smuzhiyun 	u32 dlid = packet->dlid;
871*4882a593Smuzhiyun 	u32 slid = packet->slid;
872*4882a593Smuzhiyun 	u8 extra_bytes;
873*4882a593Smuzhiyun 	u8 l4 = 0;
874*4882a593Smuzhiyun 	bool dlid_is_permissive;
875*4882a593Smuzhiyun 	bool slid_is_permissive;
876*4882a593Smuzhiyun 	bool solicited = false;
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 	extra_bytes = packet->pad + packet->extra_byte + (SIZE_OF_CRC << 2);
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun 	if (packet->etype == RHF_RCV_TYPE_BYPASS) {
881*4882a593Smuzhiyun 		u32 permissive_lid =
882*4882a593Smuzhiyun 			opa_get_lid(be32_to_cpu(OPA_LID_PERMISSIVE), 16B);
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 		l4 = hfi1_16B_get_l4(packet->hdr);
885*4882a593Smuzhiyun 		pkey = hfi1_16B_get_pkey(packet->hdr);
886*4882a593Smuzhiyun 		dlid_is_permissive = (dlid == permissive_lid);
887*4882a593Smuzhiyun 		slid_is_permissive = (slid == permissive_lid);
888*4882a593Smuzhiyun 	} else {
889*4882a593Smuzhiyun 		pkey = ib_bth_get_pkey(packet->ohdr);
890*4882a593Smuzhiyun 		dlid_is_permissive = (dlid == be16_to_cpu(IB_LID_PERMISSIVE));
891*4882a593Smuzhiyun 		slid_is_permissive = (slid == be16_to_cpu(IB_LID_PERMISSIVE));
892*4882a593Smuzhiyun 	}
893*4882a593Smuzhiyun 	sl_from_sc = ibp->sc_to_sl[sc5];
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 	if (likely(l4 != OPA_16B_L4_FM)) {
896*4882a593Smuzhiyun 		src_qp = ib_get_sqpn(packet->ohdr);
897*4882a593Smuzhiyun 		solicited = ib_bth_is_solicited(packet->ohdr);
898*4882a593Smuzhiyun 	} else {
899*4882a593Smuzhiyun 		src_qp = hfi1_16B_get_src_qpn(packet->mgmt);
900*4882a593Smuzhiyun 	}
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	process_ecn(qp, packet);
903*4882a593Smuzhiyun 	/*
904*4882a593Smuzhiyun 	 * Get the number of bytes the message was padded by
905*4882a593Smuzhiyun 	 * and drop incomplete packets.
906*4882a593Smuzhiyun 	 */
907*4882a593Smuzhiyun 	if (unlikely(tlen < (hdrsize + extra_bytes)))
908*4882a593Smuzhiyun 		goto drop;
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun 	tlen -= hdrsize + extra_bytes;
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	/*
913*4882a593Smuzhiyun 	 * Check that the permissive LID is only used on QP0
914*4882a593Smuzhiyun 	 * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
915*4882a593Smuzhiyun 	 */
916*4882a593Smuzhiyun 	if (qp->ibqp.qp_num) {
917*4882a593Smuzhiyun 		if (unlikely(dlid_is_permissive || slid_is_permissive))
918*4882a593Smuzhiyun 			goto drop;
919*4882a593Smuzhiyun 		if (qp->ibqp.qp_num > 1) {
920*4882a593Smuzhiyun 			if (unlikely(rcv_pkey_check(ppd, pkey, sc5, slid))) {
921*4882a593Smuzhiyun 				/*
922*4882a593Smuzhiyun 				 * Traps will not be sent for packets dropped
923*4882a593Smuzhiyun 				 * by the HW. This is fine, as sending trap
924*4882a593Smuzhiyun 				 * for invalid pkeys is optional according to
925*4882a593Smuzhiyun 				 * IB spec (release 1.3, section 10.9.4)
926*4882a593Smuzhiyun 				 */
927*4882a593Smuzhiyun 				hfi1_bad_pkey(ibp,
928*4882a593Smuzhiyun 					      pkey, sl,
929*4882a593Smuzhiyun 					      src_qp, qp->ibqp.qp_num,
930*4882a593Smuzhiyun 					      slid, dlid);
931*4882a593Smuzhiyun 				return;
932*4882a593Smuzhiyun 			}
933*4882a593Smuzhiyun 		} else {
934*4882a593Smuzhiyun 			/* GSI packet */
935*4882a593Smuzhiyun 			mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
936*4882a593Smuzhiyun 			if (mgmt_pkey_idx < 0)
937*4882a593Smuzhiyun 				goto drop;
938*4882a593Smuzhiyun 		}
939*4882a593Smuzhiyun 		if (unlikely(l4 != OPA_16B_L4_FM &&
940*4882a593Smuzhiyun 			     ib_get_qkey(packet->ohdr) != qp->qkey))
941*4882a593Smuzhiyun 			return; /* Silent drop */
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun 		/* Drop invalid MAD packets (see 13.5.3.1). */
944*4882a593Smuzhiyun 		if (unlikely(qp->ibqp.qp_num == 1 &&
945*4882a593Smuzhiyun 			     (tlen > 2048 || (sc5 == 0xF))))
946*4882a593Smuzhiyun 			goto drop;
947*4882a593Smuzhiyun 	} else {
948*4882a593Smuzhiyun 		/* Received on QP0, and so by definition, this is an SMP */
949*4882a593Smuzhiyun 		struct opa_smp *smp = (struct opa_smp *)data;
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 		if (opa_smp_check(ibp, pkey, sc5, qp, slid, smp))
952*4882a593Smuzhiyun 			goto drop;
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun 		if (tlen > 2048)
955*4882a593Smuzhiyun 			goto drop;
956*4882a593Smuzhiyun 		if ((dlid_is_permissive || slid_is_permissive) &&
957*4882a593Smuzhiyun 		    smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
958*4882a593Smuzhiyun 			goto drop;
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 		/* look up SMI pkey */
961*4882a593Smuzhiyun 		mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
962*4882a593Smuzhiyun 		if (mgmt_pkey_idx < 0)
963*4882a593Smuzhiyun 			goto drop;
964*4882a593Smuzhiyun 	}
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun 	if (qp->ibqp.qp_num > 1 &&
967*4882a593Smuzhiyun 	    opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
968*4882a593Smuzhiyun 		wc.ex.imm_data = packet->ohdr->u.ud.imm_data;
969*4882a593Smuzhiyun 		wc.wc_flags = IB_WC_WITH_IMM;
970*4882a593Smuzhiyun 	} else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
971*4882a593Smuzhiyun 		wc.ex.imm_data = 0;
972*4882a593Smuzhiyun 		wc.wc_flags = 0;
973*4882a593Smuzhiyun 	} else {
974*4882a593Smuzhiyun 		goto drop;
975*4882a593Smuzhiyun 	}
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 	/*
978*4882a593Smuzhiyun 	 * A GRH is expected to precede the data even if not
979*4882a593Smuzhiyun 	 * present on the wire.
980*4882a593Smuzhiyun 	 */
981*4882a593Smuzhiyun 	wc.byte_len = tlen + sizeof(struct ib_grh);
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 	/*
984*4882a593Smuzhiyun 	 * Get the next work request entry to find where to put the data.
985*4882a593Smuzhiyun 	 */
986*4882a593Smuzhiyun 	if (qp->r_flags & RVT_R_REUSE_SGE) {
987*4882a593Smuzhiyun 		qp->r_flags &= ~RVT_R_REUSE_SGE;
988*4882a593Smuzhiyun 	} else {
989*4882a593Smuzhiyun 		int ret;
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun 		ret = rvt_get_rwqe(qp, false);
992*4882a593Smuzhiyun 		if (ret < 0) {
993*4882a593Smuzhiyun 			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
994*4882a593Smuzhiyun 			return;
995*4882a593Smuzhiyun 		}
996*4882a593Smuzhiyun 		if (!ret) {
997*4882a593Smuzhiyun 			if (qp->ibqp.qp_num == 0)
998*4882a593Smuzhiyun 				ibp->rvp.n_vl15_dropped++;
999*4882a593Smuzhiyun 			return;
1000*4882a593Smuzhiyun 		}
1001*4882a593Smuzhiyun 	}
1002*4882a593Smuzhiyun 	/* Silently drop packets which are too big. */
1003*4882a593Smuzhiyun 	if (unlikely(wc.byte_len > qp->r_len)) {
1004*4882a593Smuzhiyun 		qp->r_flags |= RVT_R_REUSE_SGE;
1005*4882a593Smuzhiyun 		goto drop;
1006*4882a593Smuzhiyun 	}
1007*4882a593Smuzhiyun 	if (packet->grh) {
1008*4882a593Smuzhiyun 		rvt_copy_sge(qp, &qp->r_sge, packet->grh,
1009*4882a593Smuzhiyun 			     sizeof(struct ib_grh), true, false);
1010*4882a593Smuzhiyun 		wc.wc_flags |= IB_WC_GRH;
1011*4882a593Smuzhiyun 	} else if (packet->etype == RHF_RCV_TYPE_BYPASS) {
1012*4882a593Smuzhiyun 		struct ib_grh grh;
1013*4882a593Smuzhiyun 		/*
1014*4882a593Smuzhiyun 		 * Assuming we only created 16B on the send side
1015*4882a593Smuzhiyun 		 * if we want to use large LIDs, since GRH was stripped
1016*4882a593Smuzhiyun 		 * out when creating 16B, add back the GRH here.
1017*4882a593Smuzhiyun 		 */
1018*4882a593Smuzhiyun 		hfi1_make_ext_grh(packet, &grh, slid, dlid);
1019*4882a593Smuzhiyun 		rvt_copy_sge(qp, &qp->r_sge, &grh,
1020*4882a593Smuzhiyun 			     sizeof(struct ib_grh), true, false);
1021*4882a593Smuzhiyun 		wc.wc_flags |= IB_WC_GRH;
1022*4882a593Smuzhiyun 	} else {
1023*4882a593Smuzhiyun 		rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
1024*4882a593Smuzhiyun 	}
1025*4882a593Smuzhiyun 	rvt_copy_sge(qp, &qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh),
1026*4882a593Smuzhiyun 		     true, false);
1027*4882a593Smuzhiyun 	rvt_put_ss(&qp->r_sge);
1028*4882a593Smuzhiyun 	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
1029*4882a593Smuzhiyun 		return;
1030*4882a593Smuzhiyun 	wc.wr_id = qp->r_wr_id;
1031*4882a593Smuzhiyun 	wc.status = IB_WC_SUCCESS;
1032*4882a593Smuzhiyun 	wc.opcode = IB_WC_RECV;
1033*4882a593Smuzhiyun 	wc.vendor_err = 0;
1034*4882a593Smuzhiyun 	wc.qp = &qp->ibqp;
1035*4882a593Smuzhiyun 	wc.src_qp = src_qp;
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 	if (qp->ibqp.qp_type == IB_QPT_GSI ||
1038*4882a593Smuzhiyun 	    qp->ibqp.qp_type == IB_QPT_SMI) {
1039*4882a593Smuzhiyun 		if (mgmt_pkey_idx < 0) {
1040*4882a593Smuzhiyun 			if (net_ratelimit()) {
1041*4882a593Smuzhiyun 				struct hfi1_devdata *dd = ppd->dd;
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 				dd_dev_err(dd, "QP type %d mgmt_pkey_idx < 0 and packet not dropped???\n",
1044*4882a593Smuzhiyun 					   qp->ibqp.qp_type);
1045*4882a593Smuzhiyun 				mgmt_pkey_idx = 0;
1046*4882a593Smuzhiyun 			}
1047*4882a593Smuzhiyun 		}
1048*4882a593Smuzhiyun 		wc.pkey_index = (unsigned)mgmt_pkey_idx;
1049*4882a593Smuzhiyun 	} else {
1050*4882a593Smuzhiyun 		wc.pkey_index = 0;
1051*4882a593Smuzhiyun 	}
1052*4882a593Smuzhiyun 	if (slid_is_permissive)
1053*4882a593Smuzhiyun 		slid = be32_to_cpu(OPA_LID_PERMISSIVE);
1054*4882a593Smuzhiyun 	wc.slid = slid & U16_MAX;
1055*4882a593Smuzhiyun 	wc.sl = sl_from_sc;
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	/*
1058*4882a593Smuzhiyun 	 * Save the LMC lower bits if the destination LID is a unicast LID.
1059*4882a593Smuzhiyun 	 */
1060*4882a593Smuzhiyun 	wc.dlid_path_bits = hfi1_check_mcast(dlid) ? 0 :
1061*4882a593Smuzhiyun 		dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
1062*4882a593Smuzhiyun 	wc.port_num = qp->port_num;
1063*4882a593Smuzhiyun 	/* Signal completion event if the solicited bit is set. */
1064*4882a593Smuzhiyun 	rvt_recv_cq(qp, &wc, solicited);
1065*4882a593Smuzhiyun 	return;
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun drop:
1068*4882a593Smuzhiyun 	ibp->rvp.n_pkt_drops++;
1069*4882a593Smuzhiyun }
1070