xref: /linux/drivers/infiniband/hw/hfi1/ud.c (revision 832369fa6410c93547264ad449ebbf16567bbccd)
1f48ad614SDennis Dalessandro /*
2f48ad614SDennis Dalessandro  * Copyright(c) 2015, 2016 Intel Corporation.
3f48ad614SDennis Dalessandro  *
4f48ad614SDennis Dalessandro  * This file is provided under a dual BSD/GPLv2 license.  When using or
5f48ad614SDennis Dalessandro  * redistributing this file, you may do so under either license.
6f48ad614SDennis Dalessandro  *
7f48ad614SDennis Dalessandro  * GPL LICENSE SUMMARY
8f48ad614SDennis Dalessandro  *
9f48ad614SDennis Dalessandro  * This program is free software; you can redistribute it and/or modify
10f48ad614SDennis Dalessandro  * it under the terms of version 2 of the GNU General Public License as
11f48ad614SDennis Dalessandro  * published by the Free Software Foundation.
12f48ad614SDennis Dalessandro  *
13f48ad614SDennis Dalessandro  * This program is distributed in the hope that it will be useful, but
14f48ad614SDennis Dalessandro  * WITHOUT ANY WARRANTY; without even the implied warranty of
15f48ad614SDennis Dalessandro  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16f48ad614SDennis Dalessandro  * General Public License for more details.
17f48ad614SDennis Dalessandro  *
18f48ad614SDennis Dalessandro  * BSD LICENSE
19f48ad614SDennis Dalessandro  *
20f48ad614SDennis Dalessandro  * Redistribution and use in source and binary forms, with or without
21f48ad614SDennis Dalessandro  * modification, are permitted provided that the following conditions
22f48ad614SDennis Dalessandro  * are met:
23f48ad614SDennis Dalessandro  *
24f48ad614SDennis Dalessandro  *  - Redistributions of source code must retain the above copyright
25f48ad614SDennis Dalessandro  *    notice, this list of conditions and the following disclaimer.
26f48ad614SDennis Dalessandro  *  - Redistributions in binary form must reproduce the above copyright
27f48ad614SDennis Dalessandro  *    notice, this list of conditions and the following disclaimer in
28f48ad614SDennis Dalessandro  *    the documentation and/or other materials provided with the
29f48ad614SDennis Dalessandro  *    distribution.
30f48ad614SDennis Dalessandro  *  - Neither the name of Intel Corporation nor the names of its
31f48ad614SDennis Dalessandro  *    contributors may be used to endorse or promote products derived
32f48ad614SDennis Dalessandro  *    from this software without specific prior written permission.
33f48ad614SDennis Dalessandro  *
34f48ad614SDennis Dalessandro  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35f48ad614SDennis Dalessandro  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36f48ad614SDennis Dalessandro  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37f48ad614SDennis Dalessandro  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38f48ad614SDennis Dalessandro  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39f48ad614SDennis Dalessandro  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40f48ad614SDennis Dalessandro  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41f48ad614SDennis Dalessandro  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42f48ad614SDennis Dalessandro  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43f48ad614SDennis Dalessandro  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44f48ad614SDennis Dalessandro  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45f48ad614SDennis Dalessandro  *
46f48ad614SDennis Dalessandro  */
47f48ad614SDennis Dalessandro 
48f48ad614SDennis Dalessandro #include <linux/net.h>
49f48ad614SDennis Dalessandro #include <rdma/ib_smi.h>
50f48ad614SDennis Dalessandro 
51f48ad614SDennis Dalessandro #include "hfi.h"
52f48ad614SDennis Dalessandro #include "mad.h"
53f48ad614SDennis Dalessandro #include "verbs_txreq.h"
54f48ad614SDennis Dalessandro #include "qp.h"
55f48ad614SDennis Dalessandro 
5688733e3bSDon Hiatt /* We support only two types - 9B and 16B for now */
5788733e3bSDon Hiatt static const hfi1_make_req hfi1_make_ud_req_tbl[2] = {
5888733e3bSDon Hiatt 	[HFI1_PKT_TYPE_9B] = &hfi1_make_ud_req_9B,
5988733e3bSDon Hiatt 	[HFI1_PKT_TYPE_16B] = &hfi1_make_ud_req_16B
6088733e3bSDon Hiatt };
6188733e3bSDon Hiatt 
62f48ad614SDennis Dalessandro /**
63f48ad614SDennis Dalessandro  * ud_loopback - handle send on loopback QPs
64f48ad614SDennis Dalessandro  * @sqp: the sending QP
65f48ad614SDennis Dalessandro  * @swqe: the send work request
66f48ad614SDennis Dalessandro  *
67f48ad614SDennis Dalessandro  * This is called from hfi1_make_ud_req() to forward a WQE addressed
68f48ad614SDennis Dalessandro  * to the same HFI.
69f48ad614SDennis Dalessandro  * Note that the receive interrupt handler may be calling hfi1_ud_rcv()
70f48ad614SDennis Dalessandro  * while this is being called.
71f48ad614SDennis Dalessandro  */
72f48ad614SDennis Dalessandro static void ud_loopback(struct rvt_qp *sqp, struct rvt_swqe *swqe)
73f48ad614SDennis Dalessandro {
74f48ad614SDennis Dalessandro 	struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
75f48ad614SDennis Dalessandro 	struct hfi1_pportdata *ppd;
7688733e3bSDon Hiatt 	struct hfi1_qp_priv *priv = sqp->priv;
77f48ad614SDennis Dalessandro 	struct rvt_qp *qp;
7890898850SDasaratharaman Chandramouli 	struct rdma_ah_attr *ah_attr;
79f48ad614SDennis Dalessandro 	unsigned long flags;
80f48ad614SDennis Dalessandro 	struct rvt_sge_state ssge;
81f48ad614SDennis Dalessandro 	struct rvt_sge *sge;
82f48ad614SDennis Dalessandro 	struct ib_wc wc;
83f48ad614SDennis Dalessandro 	u32 length;
84f48ad614SDennis Dalessandro 	enum ib_qp_type sqptype, dqptype;
85f48ad614SDennis Dalessandro 
86f48ad614SDennis Dalessandro 	rcu_read_lock();
87f48ad614SDennis Dalessandro 
88f48ad614SDennis Dalessandro 	qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
89f48ad614SDennis Dalessandro 			    swqe->ud_wr.remote_qpn);
90f48ad614SDennis Dalessandro 	if (!qp) {
91f48ad614SDennis Dalessandro 		ibp->rvp.n_pkt_drops++;
92f48ad614SDennis Dalessandro 		rcu_read_unlock();
93f48ad614SDennis Dalessandro 		return;
94f48ad614SDennis Dalessandro 	}
95f48ad614SDennis Dalessandro 
96f48ad614SDennis Dalessandro 	sqptype = sqp->ibqp.qp_type == IB_QPT_GSI ?
97f48ad614SDennis Dalessandro 			IB_QPT_UD : sqp->ibqp.qp_type;
98f48ad614SDennis Dalessandro 	dqptype = qp->ibqp.qp_type == IB_QPT_GSI ?
99f48ad614SDennis Dalessandro 			IB_QPT_UD : qp->ibqp.qp_type;
100f48ad614SDennis Dalessandro 
101f48ad614SDennis Dalessandro 	if (dqptype != sqptype ||
102f48ad614SDennis Dalessandro 	    !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
103f48ad614SDennis Dalessandro 		ibp->rvp.n_pkt_drops++;
104f48ad614SDennis Dalessandro 		goto drop;
105f48ad614SDennis Dalessandro 	}
106f48ad614SDennis Dalessandro 
107f48ad614SDennis Dalessandro 	ah_attr = &ibah_to_rvtah(swqe->ud_wr.ah)->attr;
108f48ad614SDennis Dalessandro 	ppd = ppd_from_ibp(ibp);
109f48ad614SDennis Dalessandro 
110f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_num > 1) {
111f48ad614SDennis Dalessandro 		u16 pkey;
11288733e3bSDon Hiatt 		u32 slid;
113d8966fcdSDasaratharaman Chandramouli 		u8 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
114f48ad614SDennis Dalessandro 
115f48ad614SDennis Dalessandro 		pkey = hfi1_get_pkey(ibp, sqp->s_pkey_index);
116d8966fcdSDasaratharaman Chandramouli 		slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
117f48ad614SDennis Dalessandro 				   ((1 << ppd->lmc) - 1));
118f48ad614SDennis Dalessandro 		if (unlikely(ingress_pkey_check(ppd, pkey, sc5,
1195786adf3SDon Hiatt 						qp->s_pkey_index,
1205786adf3SDon Hiatt 						slid, false))) {
12113d84914SDennis Dalessandro 			hfi1_bad_pkey(ibp, pkey,
122d8966fcdSDasaratharaman Chandramouli 				      rdma_ah_get_sl(ah_attr),
123f48ad614SDennis Dalessandro 				      sqp->ibqp.qp_num, qp->ibqp.qp_num,
124d8966fcdSDasaratharaman Chandramouli 				      slid, rdma_ah_get_dlid(ah_attr));
125f48ad614SDennis Dalessandro 			goto drop;
126f48ad614SDennis Dalessandro 		}
127f48ad614SDennis Dalessandro 	}
128f48ad614SDennis Dalessandro 
129f48ad614SDennis Dalessandro 	/*
130f48ad614SDennis Dalessandro 	 * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
131f48ad614SDennis Dalessandro 	 * Qkeys with the high order bit set mean use the
132f48ad614SDennis Dalessandro 	 * qkey from the QP context instead of the WR (see 10.2.5).
133f48ad614SDennis Dalessandro 	 */
134f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_num) {
135f48ad614SDennis Dalessandro 		u32 qkey;
136f48ad614SDennis Dalessandro 
137f48ad614SDennis Dalessandro 		qkey = (int)swqe->ud_wr.remote_qkey < 0 ?
138f48ad614SDennis Dalessandro 			sqp->qkey : swqe->ud_wr.remote_qkey;
13913d84914SDennis Dalessandro 		if (unlikely(qkey != qp->qkey))
14013d84914SDennis Dalessandro 			goto drop; /* silently drop per IBTA spec */
141f48ad614SDennis Dalessandro 	}
142f48ad614SDennis Dalessandro 
143f48ad614SDennis Dalessandro 	/*
144f48ad614SDennis Dalessandro 	 * A GRH is expected to precede the data even if not
145f48ad614SDennis Dalessandro 	 * present on the wire.
146f48ad614SDennis Dalessandro 	 */
147f48ad614SDennis Dalessandro 	length = swqe->length;
148f48ad614SDennis Dalessandro 	memset(&wc, 0, sizeof(wc));
149f48ad614SDennis Dalessandro 	wc.byte_len = length + sizeof(struct ib_grh);
150f48ad614SDennis Dalessandro 
151f48ad614SDennis Dalessandro 	if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
152f48ad614SDennis Dalessandro 		wc.wc_flags = IB_WC_WITH_IMM;
153f48ad614SDennis Dalessandro 		wc.ex.imm_data = swqe->wr.ex.imm_data;
154f48ad614SDennis Dalessandro 	}
155f48ad614SDennis Dalessandro 
156f48ad614SDennis Dalessandro 	spin_lock_irqsave(&qp->r_lock, flags);
157f48ad614SDennis Dalessandro 
158f48ad614SDennis Dalessandro 	/*
159f48ad614SDennis Dalessandro 	 * Get the next work request entry to find where to put the data.
160f48ad614SDennis Dalessandro 	 */
161f48ad614SDennis Dalessandro 	if (qp->r_flags & RVT_R_REUSE_SGE) {
162f48ad614SDennis Dalessandro 		qp->r_flags &= ~RVT_R_REUSE_SGE;
163f48ad614SDennis Dalessandro 	} else {
164f48ad614SDennis Dalessandro 		int ret;
165f48ad614SDennis Dalessandro 
166*832369faSBrian Welty 		ret = rvt_get_rwqe(qp, false);
167f48ad614SDennis Dalessandro 		if (ret < 0) {
168beb5a042SBrian Welty 			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
169f48ad614SDennis Dalessandro 			goto bail_unlock;
170f48ad614SDennis Dalessandro 		}
171f48ad614SDennis Dalessandro 		if (!ret) {
172f48ad614SDennis Dalessandro 			if (qp->ibqp.qp_num == 0)
173f48ad614SDennis Dalessandro 				ibp->rvp.n_vl15_dropped++;
174f48ad614SDennis Dalessandro 			goto bail_unlock;
175f48ad614SDennis Dalessandro 		}
176f48ad614SDennis Dalessandro 	}
177f48ad614SDennis Dalessandro 	/* Silently drop packets which are too big. */
178f48ad614SDennis Dalessandro 	if (unlikely(wc.byte_len > qp->r_len)) {
179f48ad614SDennis Dalessandro 		qp->r_flags |= RVT_R_REUSE_SGE;
180f48ad614SDennis Dalessandro 		ibp->rvp.n_pkt_drops++;
181f48ad614SDennis Dalessandro 		goto bail_unlock;
182f48ad614SDennis Dalessandro 	}
183f48ad614SDennis Dalessandro 
184d8966fcdSDasaratharaman Chandramouli 	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
185527dbf12SDasaratharaman Chandramouli 		struct ib_grh grh;
18688733e3bSDon Hiatt 		struct ib_global_route grd = *(rdma_ah_read_grh(ah_attr));
187527dbf12SDasaratharaman Chandramouli 
18888733e3bSDon Hiatt 		/*
18988733e3bSDon Hiatt 		 * For loopback packets with extended LIDs, the
19088733e3bSDon Hiatt 		 * sgid_index in the GRH is 0 and the dgid is
19188733e3bSDon Hiatt 		 * OPA GID of the sender. While creating a response
19288733e3bSDon Hiatt 		 * to the loopback packet, IB core creates the new
19388733e3bSDon Hiatt 		 * sgid_index from the DGID and that will be the
19488733e3bSDon Hiatt 		 * OPA_GID_INDEX. The new dgid is from the sgid
19588733e3bSDon Hiatt 		 * index and that will be in the IB GID format.
19688733e3bSDon Hiatt 		 *
19788733e3bSDon Hiatt 		 * We now have a case where the sent packet had a
19888733e3bSDon Hiatt 		 * different sgid_index and dgid compared to the
19988733e3bSDon Hiatt 		 * one that was received in response.
20088733e3bSDon Hiatt 		 *
20188733e3bSDon Hiatt 		 * Fix this inconsistency.
20288733e3bSDon Hiatt 		 */
20388733e3bSDon Hiatt 		if (priv->hdr_type == HFI1_PKT_TYPE_16B) {
20488733e3bSDon Hiatt 			if (grd.sgid_index == 0)
20588733e3bSDon Hiatt 				grd.sgid_index = OPA_GID_INDEX;
20688733e3bSDon Hiatt 
20788733e3bSDon Hiatt 			if (ib_is_opa_gid(&grd.dgid))
20888733e3bSDon Hiatt 				grd.dgid.global.interface_id =
20988733e3bSDon Hiatt 				cpu_to_be64(ppd->guids[HFI1_PORT_GUID_INDEX]);
21088733e3bSDon Hiatt 		}
21188733e3bSDon Hiatt 
21288733e3bSDon Hiatt 		hfi1_make_grh(ibp, &grh, &grd, 0, 0);
213527dbf12SDasaratharaman Chandramouli 		hfi1_copy_sge(&qp->r_sge, &grh,
2140128fceaSBrian Welty 			      sizeof(grh), true, false);
215f48ad614SDennis Dalessandro 		wc.wc_flags |= IB_WC_GRH;
216f48ad614SDennis Dalessandro 	} else {
2171198fceaSBrian Welty 		rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
218f48ad614SDennis Dalessandro 	}
219f48ad614SDennis Dalessandro 	ssge.sg_list = swqe->sg_list + 1;
220f48ad614SDennis Dalessandro 	ssge.sge = *swqe->sg_list;
221f48ad614SDennis Dalessandro 	ssge.num_sge = swqe->wr.num_sge;
222f48ad614SDennis Dalessandro 	sge = &ssge.sge;
223f48ad614SDennis Dalessandro 	while (length) {
224f48ad614SDennis Dalessandro 		u32 len = sge->length;
225f48ad614SDennis Dalessandro 
226f48ad614SDennis Dalessandro 		if (len > length)
227f48ad614SDennis Dalessandro 			len = length;
228f48ad614SDennis Dalessandro 		if (len > sge->sge_length)
229f48ad614SDennis Dalessandro 			len = sge->sge_length;
230f48ad614SDennis Dalessandro 		WARN_ON_ONCE(len == 0);
2310128fceaSBrian Welty 		hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, true, false);
232f48ad614SDennis Dalessandro 		sge->vaddr += len;
233f48ad614SDennis Dalessandro 		sge->length -= len;
234f48ad614SDennis Dalessandro 		sge->sge_length -= len;
235f48ad614SDennis Dalessandro 		if (sge->sge_length == 0) {
236f48ad614SDennis Dalessandro 			if (--ssge.num_sge)
237f48ad614SDennis Dalessandro 				*sge = *ssge.sg_list++;
238f48ad614SDennis Dalessandro 		} else if (sge->length == 0 && sge->mr->lkey) {
239f48ad614SDennis Dalessandro 			if (++sge->n >= RVT_SEGSZ) {
240f48ad614SDennis Dalessandro 				if (++sge->m >= sge->mr->mapsz)
241f48ad614SDennis Dalessandro 					break;
242f48ad614SDennis Dalessandro 				sge->n = 0;
243f48ad614SDennis Dalessandro 			}
244f48ad614SDennis Dalessandro 			sge->vaddr =
245f48ad614SDennis Dalessandro 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
246f48ad614SDennis Dalessandro 			sge->length =
247f48ad614SDennis Dalessandro 				sge->mr->map[sge->m]->segs[sge->n].length;
248f48ad614SDennis Dalessandro 		}
249f48ad614SDennis Dalessandro 		length -= len;
250f48ad614SDennis Dalessandro 	}
251f48ad614SDennis Dalessandro 	rvt_put_ss(&qp->r_sge);
252f48ad614SDennis Dalessandro 	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
253f48ad614SDennis Dalessandro 		goto bail_unlock;
254f48ad614SDennis Dalessandro 	wc.wr_id = qp->r_wr_id;
255f48ad614SDennis Dalessandro 	wc.status = IB_WC_SUCCESS;
256f48ad614SDennis Dalessandro 	wc.opcode = IB_WC_RECV;
257f48ad614SDennis Dalessandro 	wc.qp = &qp->ibqp;
258f48ad614SDennis Dalessandro 	wc.src_qp = sqp->ibqp.qp_num;
259f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI) {
260f48ad614SDennis Dalessandro 		if (sqp->ibqp.qp_type == IB_QPT_GSI ||
261f48ad614SDennis Dalessandro 		    sqp->ibqp.qp_type == IB_QPT_SMI)
262f48ad614SDennis Dalessandro 			wc.pkey_index = swqe->ud_wr.pkey_index;
263f48ad614SDennis Dalessandro 		else
264f48ad614SDennis Dalessandro 			wc.pkey_index = sqp->s_pkey_index;
265f48ad614SDennis Dalessandro 	} else {
266f48ad614SDennis Dalessandro 		wc.pkey_index = 0;
267f48ad614SDennis Dalessandro 	}
268b64581adSDon Hiatt 	wc.slid = (ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
269b64581adSDon Hiatt 				   ((1 << ppd->lmc) - 1))) & U16_MAX;
270f48ad614SDennis Dalessandro 	/* Check for loopback when the port lid is not set */
271f48ad614SDennis Dalessandro 	if (wc.slid == 0 && sqp->ibqp.qp_type == IB_QPT_GSI)
272f48ad614SDennis Dalessandro 		wc.slid = be16_to_cpu(IB_LID_PERMISSIVE);
273d8966fcdSDasaratharaman Chandramouli 	wc.sl = rdma_ah_get_sl(ah_attr);
274d8966fcdSDasaratharaman Chandramouli 	wc.dlid_path_bits = rdma_ah_get_dlid(ah_attr) & ((1 << ppd->lmc) - 1);
275f48ad614SDennis Dalessandro 	wc.port_num = qp->port_num;
276f48ad614SDennis Dalessandro 	/* Signal completion event if the solicited bit is set. */
277f48ad614SDennis Dalessandro 	rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
278f48ad614SDennis Dalessandro 		     swqe->wr.send_flags & IB_SEND_SOLICITED);
279f48ad614SDennis Dalessandro 	ibp->rvp.n_loop_pkts++;
280f48ad614SDennis Dalessandro bail_unlock:
281f48ad614SDennis Dalessandro 	spin_unlock_irqrestore(&qp->r_lock, flags);
282f48ad614SDennis Dalessandro drop:
283f48ad614SDennis Dalessandro 	rcu_read_unlock();
284f48ad614SDennis Dalessandro }
285f48ad614SDennis Dalessandro 
28688733e3bSDon Hiatt static void hfi1_make_bth_deth(struct rvt_qp *qp, struct rvt_swqe *wqe,
28788733e3bSDon Hiatt 			       struct ib_other_headers *ohdr,
28888733e3bSDon Hiatt 			       u16 *pkey, u32 extra_bytes, bool bypass)
28988733e3bSDon Hiatt {
29088733e3bSDon Hiatt 	u32 bth0;
29188733e3bSDon Hiatt 	struct hfi1_ibport *ibp;
29288733e3bSDon Hiatt 
29388733e3bSDon Hiatt 	ibp = to_iport(qp->ibqp.device, qp->port_num);
29488733e3bSDon Hiatt 	if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
29588733e3bSDon Hiatt 		ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
29688733e3bSDon Hiatt 		bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
29788733e3bSDon Hiatt 	} else {
29888733e3bSDon Hiatt 		bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
29988733e3bSDon Hiatt 	}
30088733e3bSDon Hiatt 
30188733e3bSDon Hiatt 	if (wqe->wr.send_flags & IB_SEND_SOLICITED)
30288733e3bSDon Hiatt 		bth0 |= IB_BTH_SOLICITED;
30388733e3bSDon Hiatt 	bth0 |= extra_bytes << 20;
30488733e3bSDon Hiatt 	if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI)
30588733e3bSDon Hiatt 		*pkey = hfi1_get_pkey(ibp, wqe->ud_wr.pkey_index);
30688733e3bSDon Hiatt 	else
30788733e3bSDon Hiatt 		*pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
30888733e3bSDon Hiatt 	if (!bypass)
30988733e3bSDon Hiatt 		bth0 |= *pkey;
31088733e3bSDon Hiatt 	ohdr->bth[0] = cpu_to_be32(bth0);
31188733e3bSDon Hiatt 	ohdr->bth[1] = cpu_to_be32(wqe->ud_wr.remote_qpn);
31288733e3bSDon Hiatt 	ohdr->bth[2] = cpu_to_be32(mask_psn(wqe->psn));
31388733e3bSDon Hiatt 	/*
31488733e3bSDon Hiatt 	 * Qkeys with the high order bit set mean use the
31588733e3bSDon Hiatt 	 * qkey from the QP context instead of the WR (see 10.2.5).
31688733e3bSDon Hiatt 	 */
31788733e3bSDon Hiatt 	ohdr->u.ud.deth[0] = cpu_to_be32((int)wqe->ud_wr.remote_qkey < 0 ?
31888733e3bSDon Hiatt 					 qp->qkey : wqe->ud_wr.remote_qkey);
31988733e3bSDon Hiatt 	ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
32088733e3bSDon Hiatt }
32188733e3bSDon Hiatt 
32288733e3bSDon Hiatt void hfi1_make_ud_req_9B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
32388733e3bSDon Hiatt 			 struct rvt_swqe *wqe)
32488733e3bSDon Hiatt {
32588733e3bSDon Hiatt 	u32 nwords, extra_bytes;
32688733e3bSDon Hiatt 	u16 len, slid, dlid, pkey;
32788733e3bSDon Hiatt 	u16 lrh0 = 0;
32888733e3bSDon Hiatt 	u8 sc5;
32988733e3bSDon Hiatt 	struct hfi1_qp_priv *priv = qp->priv;
33088733e3bSDon Hiatt 	struct ib_other_headers *ohdr;
33188733e3bSDon Hiatt 	struct rdma_ah_attr *ah_attr;
33288733e3bSDon Hiatt 	struct hfi1_pportdata *ppd;
33388733e3bSDon Hiatt 	struct hfi1_ibport *ibp;
33488733e3bSDon Hiatt 	struct ib_grh *grh;
33588733e3bSDon Hiatt 
33688733e3bSDon Hiatt 	ibp = to_iport(qp->ibqp.device, qp->port_num);
33788733e3bSDon Hiatt 	ppd = ppd_from_ibp(ibp);
33888733e3bSDon Hiatt 	ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
33988733e3bSDon Hiatt 
34088733e3bSDon Hiatt 	extra_bytes = -wqe->length & 3;
34188733e3bSDon Hiatt 	nwords = ((wqe->length + extra_bytes) >> 2) + SIZE_OF_CRC;
34288733e3bSDon Hiatt 	/* header size in dwords LRH+BTH+DETH = (8+12+8)/4. */
3439636258fSMitko Haralanov 	ps->s_txreq->hdr_dwords = 7;
34488733e3bSDon Hiatt 	if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
3459636258fSMitko Haralanov 		ps->s_txreq->hdr_dwords++;
34688733e3bSDon Hiatt 
34788733e3bSDon Hiatt 	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
34888733e3bSDon Hiatt 		grh = &ps->s_txreq->phdr.hdr.ibh.u.l.grh;
3499636258fSMitko Haralanov 		ps->s_txreq->hdr_dwords +=
3509636258fSMitko Haralanov 			hfi1_make_grh(ibp, grh, rdma_ah_read_grh(ah_attr),
35178d3633bSMike Marciniszyn 				      ps->s_txreq->hdr_dwords - LRH_9B_DWORDS,
35278d3633bSMike Marciniszyn 				      nwords);
35388733e3bSDon Hiatt 		lrh0 = HFI1_LRH_GRH;
35488733e3bSDon Hiatt 		ohdr = &ps->s_txreq->phdr.hdr.ibh.u.l.oth;
35588733e3bSDon Hiatt 	} else {
35688733e3bSDon Hiatt 		lrh0 = HFI1_LRH_BTH;
35788733e3bSDon Hiatt 		ohdr = &ps->s_txreq->phdr.hdr.ibh.u.oth;
35888733e3bSDon Hiatt 	}
35988733e3bSDon Hiatt 
36088733e3bSDon Hiatt 	sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
36188733e3bSDon Hiatt 	lrh0 |= (rdma_ah_get_sl(ah_attr) & 0xf) << 4;
36288733e3bSDon Hiatt 	if (qp->ibqp.qp_type == IB_QPT_SMI) {
36388733e3bSDon Hiatt 		lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
36488733e3bSDon Hiatt 		priv->s_sc = 0xf;
36588733e3bSDon Hiatt 	} else {
36688733e3bSDon Hiatt 		lrh0 |= (sc5 & 0xf) << 12;
36788733e3bSDon Hiatt 		priv->s_sc = sc5;
36888733e3bSDon Hiatt 	}
36988733e3bSDon Hiatt 
37088733e3bSDon Hiatt 	dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 9B);
37188733e3bSDon Hiatt 	if (dlid == be16_to_cpu(IB_LID_PERMISSIVE)) {
37288733e3bSDon Hiatt 		slid = be16_to_cpu(IB_LID_PERMISSIVE);
37388733e3bSDon Hiatt 	} else {
37488733e3bSDon Hiatt 		u16 lid = (u16)ppd->lid;
37588733e3bSDon Hiatt 
37688733e3bSDon Hiatt 		if (lid) {
37788733e3bSDon Hiatt 			lid |= rdma_ah_get_path_bits(ah_attr) &
37888733e3bSDon Hiatt 				((1 << ppd->lmc) - 1);
37988733e3bSDon Hiatt 			slid = lid;
38088733e3bSDon Hiatt 		} else {
38188733e3bSDon Hiatt 			slid = be16_to_cpu(IB_LID_PERMISSIVE);
38288733e3bSDon Hiatt 		}
38388733e3bSDon Hiatt 	}
38488733e3bSDon Hiatt 	hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, false);
3859636258fSMitko Haralanov 	len = ps->s_txreq->hdr_dwords + nwords;
38688733e3bSDon Hiatt 
38788733e3bSDon Hiatt 	/* Setup the packet */
38888733e3bSDon Hiatt 	ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_9B;
38988733e3bSDon Hiatt 	hfi1_make_ib_hdr(&ps->s_txreq->phdr.hdr.ibh,
39088733e3bSDon Hiatt 			 lrh0, len, dlid, slid);
39188733e3bSDon Hiatt }
39288733e3bSDon Hiatt 
39388733e3bSDon Hiatt void hfi1_make_ud_req_16B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
39488733e3bSDon Hiatt 			  struct rvt_swqe *wqe)
39588733e3bSDon Hiatt {
39688733e3bSDon Hiatt 	struct hfi1_qp_priv *priv = qp->priv;
39788733e3bSDon Hiatt 	struct ib_other_headers *ohdr;
39888733e3bSDon Hiatt 	struct rdma_ah_attr *ah_attr;
39988733e3bSDon Hiatt 	struct hfi1_pportdata *ppd;
40088733e3bSDon Hiatt 	struct hfi1_ibport *ibp;
40188733e3bSDon Hiatt 	u32 dlid, slid, nwords, extra_bytes;
40288733e3bSDon Hiatt 	u16 len, pkey;
40388733e3bSDon Hiatt 	u8 l4, sc5;
40488733e3bSDon Hiatt 
40588733e3bSDon Hiatt 	ibp = to_iport(qp->ibqp.device, qp->port_num);
40688733e3bSDon Hiatt 	ppd = ppd_from_ibp(ibp);
40788733e3bSDon Hiatt 	ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
40888733e3bSDon Hiatt 	/* header size in dwords 16B LRH+BTH+DETH = (16+12+8)/4. */
4099636258fSMitko Haralanov 	ps->s_txreq->hdr_dwords = 9;
41088733e3bSDon Hiatt 	if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
4119636258fSMitko Haralanov 		ps->s_txreq->hdr_dwords++;
41288733e3bSDon Hiatt 
41388733e3bSDon Hiatt 	/* SW provides space for CRC and LT for bypass packets. */
4149636258fSMitko Haralanov 	extra_bytes = hfi1_get_16b_padding((ps->s_txreq->hdr_dwords << 2),
41588733e3bSDon Hiatt 					   wqe->length);
41688733e3bSDon Hiatt 	nwords = ((wqe->length + extra_bytes + SIZE_OF_LT) >> 2) + SIZE_OF_CRC;
41788733e3bSDon Hiatt 
41888733e3bSDon Hiatt 	if ((rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) &&
41988733e3bSDon Hiatt 	    hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) {
42088733e3bSDon Hiatt 		struct ib_grh *grh;
42188733e3bSDon Hiatt 		struct ib_global_route *grd = rdma_ah_retrieve_grh(ah_attr);
42288733e3bSDon Hiatt 		/*
42388733e3bSDon Hiatt 		 * Ensure OPA GIDs are transformed to IB gids
42488733e3bSDon Hiatt 		 * before creating the GRH.
42588733e3bSDon Hiatt 		 */
42688733e3bSDon Hiatt 		if (grd->sgid_index == OPA_GID_INDEX) {
42788733e3bSDon Hiatt 			dd_dev_warn(ppd->dd, "Bad sgid_index. sgid_index: %d\n",
42888733e3bSDon Hiatt 				    grd->sgid_index);
42988733e3bSDon Hiatt 			grd->sgid_index = 0;
43088733e3bSDon Hiatt 		}
43188733e3bSDon Hiatt 		grh = &ps->s_txreq->phdr.hdr.opah.u.l.grh;
43278d3633bSMike Marciniszyn 		ps->s_txreq->hdr_dwords += hfi1_make_grh(
43378d3633bSMike Marciniszyn 			ibp, grh, grd,
43478d3633bSMike Marciniszyn 			ps->s_txreq->hdr_dwords - LRH_16B_DWORDS,
43578d3633bSMike Marciniszyn 			nwords);
43688733e3bSDon Hiatt 		ohdr = &ps->s_txreq->phdr.hdr.opah.u.l.oth;
43788733e3bSDon Hiatt 		l4 = OPA_16B_L4_IB_GLOBAL;
43888733e3bSDon Hiatt 	} else {
43988733e3bSDon Hiatt 		ohdr = &ps->s_txreq->phdr.hdr.opah.u.oth;
44088733e3bSDon Hiatt 		l4 = OPA_16B_L4_IB_LOCAL;
44188733e3bSDon Hiatt 	}
44288733e3bSDon Hiatt 
44388733e3bSDon Hiatt 	sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
44488733e3bSDon Hiatt 	if (qp->ibqp.qp_type == IB_QPT_SMI)
44588733e3bSDon Hiatt 		priv->s_sc = 0xf;
44688733e3bSDon Hiatt 	else
44788733e3bSDon Hiatt 		priv->s_sc = sc5;
44888733e3bSDon Hiatt 
44988733e3bSDon Hiatt 	dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 16B);
45088733e3bSDon Hiatt 	if (!ppd->lid)
45188733e3bSDon Hiatt 		slid = be32_to_cpu(OPA_LID_PERMISSIVE);
45288733e3bSDon Hiatt 	else
45388733e3bSDon Hiatt 		slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
45488733e3bSDon Hiatt 			   ((1 << ppd->lmc) - 1));
45588733e3bSDon Hiatt 
45688733e3bSDon Hiatt 	hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, true);
45788733e3bSDon Hiatt 	/* Convert dwords to flits */
4589636258fSMitko Haralanov 	len = (ps->s_txreq->hdr_dwords + nwords) >> 1;
45988733e3bSDon Hiatt 
46088733e3bSDon Hiatt 	/* Setup the packet */
46188733e3bSDon Hiatt 	ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_16B;
46288733e3bSDon Hiatt 	hfi1_make_16b_hdr(&ps->s_txreq->phdr.hdr.opah,
46388733e3bSDon Hiatt 			  slid, dlid, len, pkey, 0, 0, l4, priv->s_sc);
46488733e3bSDon Hiatt }
46588733e3bSDon Hiatt 
466f48ad614SDennis Dalessandro /**
467f48ad614SDennis Dalessandro  * hfi1_make_ud_req - construct a UD request packet
468f48ad614SDennis Dalessandro  * @qp: the QP
469f48ad614SDennis Dalessandro  *
470f48ad614SDennis Dalessandro  * Assume s_lock is held.
471f48ad614SDennis Dalessandro  *
472f48ad614SDennis Dalessandro  * Return 1 if constructed; otherwise, return 0.
473f48ad614SDennis Dalessandro  */
474f48ad614SDennis Dalessandro int hfi1_make_ud_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
475f48ad614SDennis Dalessandro {
476f48ad614SDennis Dalessandro 	struct hfi1_qp_priv *priv = qp->priv;
47790898850SDasaratharaman Chandramouli 	struct rdma_ah_attr *ah_attr;
478f48ad614SDennis Dalessandro 	struct hfi1_pportdata *ppd;
479f48ad614SDennis Dalessandro 	struct hfi1_ibport *ibp;
480f48ad614SDennis Dalessandro 	struct rvt_swqe *wqe;
481f48ad614SDennis Dalessandro 	int next_cur;
48288733e3bSDon Hiatt 	u32 lid;
483f48ad614SDennis Dalessandro 
484f48ad614SDennis Dalessandro 	ps->s_txreq = get_txreq(ps->dev, qp);
485f48ad614SDennis Dalessandro 	if (IS_ERR(ps->s_txreq))
486f48ad614SDennis Dalessandro 		goto bail_no_tx;
487f48ad614SDennis Dalessandro 
488f48ad614SDennis Dalessandro 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
489f48ad614SDennis Dalessandro 		if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
490f48ad614SDennis Dalessandro 			goto bail;
491f48ad614SDennis Dalessandro 		/* We are in the error state, flush the work request. */
4926aa7de05SMark Rutland 		if (qp->s_last == READ_ONCE(qp->s_head))
493f48ad614SDennis Dalessandro 			goto bail;
494f48ad614SDennis Dalessandro 		/* If DMAs are in progress, we can't flush immediately. */
495f48ad614SDennis Dalessandro 		if (iowait_sdma_pending(&priv->s_iowait)) {
496f48ad614SDennis Dalessandro 			qp->s_flags |= RVT_S_WAIT_DMA;
497f48ad614SDennis Dalessandro 			goto bail;
498f48ad614SDennis Dalessandro 		}
499f48ad614SDennis Dalessandro 		wqe = rvt_get_swqe_ptr(qp, qp->s_last);
500f48ad614SDennis Dalessandro 		hfi1_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
501f48ad614SDennis Dalessandro 		goto done_free_tx;
502f48ad614SDennis Dalessandro 	}
503f48ad614SDennis Dalessandro 
504f48ad614SDennis Dalessandro 	/* see post_one_send() */
5056aa7de05SMark Rutland 	if (qp->s_cur == READ_ONCE(qp->s_head))
506f48ad614SDennis Dalessandro 		goto bail;
507f48ad614SDennis Dalessandro 
508f48ad614SDennis Dalessandro 	wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
509f48ad614SDennis Dalessandro 	next_cur = qp->s_cur + 1;
510f48ad614SDennis Dalessandro 	if (next_cur >= qp->s_size)
511f48ad614SDennis Dalessandro 		next_cur = 0;
512f48ad614SDennis Dalessandro 
513f48ad614SDennis Dalessandro 	/* Construct the header. */
514f48ad614SDennis Dalessandro 	ibp = to_iport(qp->ibqp.device, qp->port_num);
515f48ad614SDennis Dalessandro 	ppd = ppd_from_ibp(ibp);
516f48ad614SDennis Dalessandro 	ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
51788733e3bSDon Hiatt 	priv->hdr_type = hfi1_get_hdr_type(ppd->lid, ah_attr);
51888733e3bSDon Hiatt 	if ((!hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) ||
51988733e3bSDon Hiatt 	    (rdma_ah_get_dlid(ah_attr) == be32_to_cpu(OPA_LID_PERMISSIVE))) {
520d8966fcdSDasaratharaman Chandramouli 		lid = rdma_ah_get_dlid(ah_attr) & ~((1 << ppd->lmc) - 1);
521f48ad614SDennis Dalessandro 		if (unlikely(!loopback &&
52288733e3bSDon Hiatt 			     ((lid == ppd->lid) ||
52388733e3bSDon Hiatt 			      ((lid == be32_to_cpu(OPA_LID_PERMISSIVE)) &&
52488733e3bSDon Hiatt 			       (qp->ibqp.qp_type == IB_QPT_GSI))))) {
525f48ad614SDennis Dalessandro 			unsigned long tflags = ps->flags;
526f48ad614SDennis Dalessandro 			/*
527f48ad614SDennis Dalessandro 			 * If DMAs are in progress, we can't generate
528f48ad614SDennis Dalessandro 			 * a completion for the loopback packet since
529f48ad614SDennis Dalessandro 			 * it would be out of order.
530f48ad614SDennis Dalessandro 			 * Instead of waiting, we could queue a
531f48ad614SDennis Dalessandro 			 * zero length descriptor so we get a callback.
532f48ad614SDennis Dalessandro 			 */
533f48ad614SDennis Dalessandro 			if (iowait_sdma_pending(&priv->s_iowait)) {
534f48ad614SDennis Dalessandro 				qp->s_flags |= RVT_S_WAIT_DMA;
535f48ad614SDennis Dalessandro 				goto bail;
536f48ad614SDennis Dalessandro 			}
537f48ad614SDennis Dalessandro 			qp->s_cur = next_cur;
538f48ad614SDennis Dalessandro 			spin_unlock_irqrestore(&qp->s_lock, tflags);
539f48ad614SDennis Dalessandro 			ud_loopback(qp, wqe);
540f48ad614SDennis Dalessandro 			spin_lock_irqsave(&qp->s_lock, tflags);
541f48ad614SDennis Dalessandro 			ps->flags = tflags;
542f48ad614SDennis Dalessandro 			hfi1_send_complete(qp, wqe, IB_WC_SUCCESS);
543f48ad614SDennis Dalessandro 			goto done_free_tx;
544f48ad614SDennis Dalessandro 		}
545f48ad614SDennis Dalessandro 	}
546f48ad614SDennis Dalessandro 
547f48ad614SDennis Dalessandro 	qp->s_cur = next_cur;
548e922ae06SDon Hiatt 	ps->s_txreq->s_cur_size = wqe->length;
549b777f154SMitko Haralanov 	ps->s_txreq->ss = &qp->s_sge;
550d8966fcdSDasaratharaman Chandramouli 	qp->s_srate = rdma_ah_get_static_rate(ah_attr);
551f48ad614SDennis Dalessandro 	qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
552f48ad614SDennis Dalessandro 	qp->s_wqe = wqe;
553f48ad614SDennis Dalessandro 	qp->s_sge.sge = wqe->sg_list[0];
554f48ad614SDennis Dalessandro 	qp->s_sge.sg_list = wqe->sg_list + 1;
555f48ad614SDennis Dalessandro 	qp->s_sge.num_sge = wqe->wr.num_sge;
556f48ad614SDennis Dalessandro 	qp->s_sge.total_len = wqe->length;
557f48ad614SDennis Dalessandro 
55888733e3bSDon Hiatt 	/* Make the appropriate header */
55988733e3bSDon Hiatt 	hfi1_make_ud_req_tbl[priv->hdr_type](qp, ps, qp->s_wqe);
560f48ad614SDennis Dalessandro 	priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
561f48ad614SDennis Dalessandro 	ps->s_txreq->sde = priv->s_sde;
562f48ad614SDennis Dalessandro 	priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
563f48ad614SDennis Dalessandro 	ps->s_txreq->psc = priv->s_sendcontext;
564f48ad614SDennis Dalessandro 	/* disarm any ahg */
565a9b6b3bcSDasaratharaman Chandramouli 	priv->s_ahg->ahgcount = 0;
566a9b6b3bcSDasaratharaman Chandramouli 	priv->s_ahg->ahgidx = 0;
567a9b6b3bcSDasaratharaman Chandramouli 	priv->s_ahg->tx_flags = 0;
568f48ad614SDennis Dalessandro 
569f48ad614SDennis Dalessandro 	return 1;
570f48ad614SDennis Dalessandro 
571f48ad614SDennis Dalessandro done_free_tx:
572f48ad614SDennis Dalessandro 	hfi1_put_txreq(ps->s_txreq);
573f48ad614SDennis Dalessandro 	ps->s_txreq = NULL;
574f48ad614SDennis Dalessandro 	return 1;
575f48ad614SDennis Dalessandro 
576f48ad614SDennis Dalessandro bail:
577f48ad614SDennis Dalessandro 	hfi1_put_txreq(ps->s_txreq);
578f48ad614SDennis Dalessandro 
579f48ad614SDennis Dalessandro bail_no_tx:
580f48ad614SDennis Dalessandro 	ps->s_txreq = NULL;
581f48ad614SDennis Dalessandro 	qp->s_flags &= ~RVT_S_BUSY;
582f48ad614SDennis Dalessandro 	return 0;
583f48ad614SDennis Dalessandro }
584f48ad614SDennis Dalessandro 
585f48ad614SDennis Dalessandro /*
586f48ad614SDennis Dalessandro  * Hardware can't check this so we do it here.
587f48ad614SDennis Dalessandro  *
588f48ad614SDennis Dalessandro  * This is a slightly different algorithm than the standard pkey check.  It
589f48ad614SDennis Dalessandro  * special cases the management keys and allows for 0x7fff and 0xffff to be in
590f48ad614SDennis Dalessandro  * the table at the same time.
591f48ad614SDennis Dalessandro  *
592f48ad614SDennis Dalessandro  * @returns the index found or -1 if not found
593f48ad614SDennis Dalessandro  */
594f48ad614SDennis Dalessandro int hfi1_lookup_pkey_idx(struct hfi1_ibport *ibp, u16 pkey)
595f48ad614SDennis Dalessandro {
596f48ad614SDennis Dalessandro 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
597f48ad614SDennis Dalessandro 	unsigned i;
598f48ad614SDennis Dalessandro 
599f48ad614SDennis Dalessandro 	if (pkey == FULL_MGMT_P_KEY || pkey == LIM_MGMT_P_KEY) {
600f48ad614SDennis Dalessandro 		unsigned lim_idx = -1;
601f48ad614SDennis Dalessandro 
602f48ad614SDennis Dalessandro 		for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i) {
603f48ad614SDennis Dalessandro 			/* here we look for an exact match */
604f48ad614SDennis Dalessandro 			if (ppd->pkeys[i] == pkey)
605f48ad614SDennis Dalessandro 				return i;
606f48ad614SDennis Dalessandro 			if (ppd->pkeys[i] == LIM_MGMT_P_KEY)
607f48ad614SDennis Dalessandro 				lim_idx = i;
608f48ad614SDennis Dalessandro 		}
609f48ad614SDennis Dalessandro 
610f48ad614SDennis Dalessandro 		/* did not find 0xffff return 0x7fff idx if found */
611f48ad614SDennis Dalessandro 		if (pkey == FULL_MGMT_P_KEY)
612f48ad614SDennis Dalessandro 			return lim_idx;
613f48ad614SDennis Dalessandro 
614f48ad614SDennis Dalessandro 		/* no match...  */
615f48ad614SDennis Dalessandro 		return -1;
616f48ad614SDennis Dalessandro 	}
617f48ad614SDennis Dalessandro 
618f48ad614SDennis Dalessandro 	pkey &= 0x7fff; /* remove limited/full membership bit */
619f48ad614SDennis Dalessandro 
620f48ad614SDennis Dalessandro 	for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i)
621f48ad614SDennis Dalessandro 		if ((ppd->pkeys[i] & 0x7fff) == pkey)
622f48ad614SDennis Dalessandro 			return i;
623f48ad614SDennis Dalessandro 
624f48ad614SDennis Dalessandro 	/*
625f48ad614SDennis Dalessandro 	 * Should not get here, this means hardware failed to validate pkeys.
626f48ad614SDennis Dalessandro 	 */
627f48ad614SDennis Dalessandro 	return -1;
628f48ad614SDennis Dalessandro }
629f48ad614SDennis Dalessandro 
63088733e3bSDon Hiatt void return_cnp_16B(struct hfi1_ibport *ibp, struct rvt_qp *qp,
631f59fb9e0SMike Marciniszyn 		    u32 remote_qpn, u16 pkey, u32 slid, u32 dlid,
63288733e3bSDon Hiatt 		    u8 sc5, const struct ib_grh *old_grh)
63388733e3bSDon Hiatt {
63488733e3bSDon Hiatt 	u64 pbc, pbc_flags = 0;
63588733e3bSDon Hiatt 	u32 bth0, plen, vl, hwords = 7;
63688733e3bSDon Hiatt 	u16 len;
63788733e3bSDon Hiatt 	u8 l4;
63888733e3bSDon Hiatt 	struct hfi1_16b_header hdr;
63988733e3bSDon Hiatt 	struct ib_other_headers *ohdr;
64088733e3bSDon Hiatt 	struct pio_buf *pbuf;
64188733e3bSDon Hiatt 	struct send_context *ctxt = qp_to_send_context(qp, sc5);
64288733e3bSDon Hiatt 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
64388733e3bSDon Hiatt 	u32 nwords;
64488733e3bSDon Hiatt 
64588733e3bSDon Hiatt 	/* Populate length */
64688733e3bSDon Hiatt 	nwords = ((hfi1_get_16b_padding(hwords << 2, 0) +
64788733e3bSDon Hiatt 		   SIZE_OF_LT) >> 2) + SIZE_OF_CRC;
64888733e3bSDon Hiatt 	if (old_grh) {
64988733e3bSDon Hiatt 		struct ib_grh *grh = &hdr.u.l.grh;
65088733e3bSDon Hiatt 
65188733e3bSDon Hiatt 		grh->version_tclass_flow = old_grh->version_tclass_flow;
65278d3633bSMike Marciniszyn 		grh->paylen = cpu_to_be16(
65378d3633bSMike Marciniszyn 			(hwords - LRH_16B_DWORDS + nwords) << 2);
65488733e3bSDon Hiatt 		grh->hop_limit = 0xff;
65588733e3bSDon Hiatt 		grh->sgid = old_grh->dgid;
65688733e3bSDon Hiatt 		grh->dgid = old_grh->sgid;
65788733e3bSDon Hiatt 		ohdr = &hdr.u.l.oth;
65888733e3bSDon Hiatt 		l4 = OPA_16B_L4_IB_GLOBAL;
65988733e3bSDon Hiatt 		hwords += sizeof(struct ib_grh) / sizeof(u32);
66088733e3bSDon Hiatt 	} else {
66188733e3bSDon Hiatt 		ohdr = &hdr.u.oth;
66288733e3bSDon Hiatt 		l4 = OPA_16B_L4_IB_LOCAL;
66388733e3bSDon Hiatt 	}
66488733e3bSDon Hiatt 
66588733e3bSDon Hiatt 	/* BIT 16 to 19 is TVER. Bit 20 to 22 is pad cnt */
66688733e3bSDon Hiatt 	bth0 = (IB_OPCODE_CNP << 24) | (1 << 16) |
66788733e3bSDon Hiatt 	       (hfi1_get_16b_padding(hwords << 2, 0) << 20);
66888733e3bSDon Hiatt 	ohdr->bth[0] = cpu_to_be32(bth0);
66988733e3bSDon Hiatt 
67088733e3bSDon Hiatt 	ohdr->bth[1] = cpu_to_be32(remote_qpn);
67188733e3bSDon Hiatt 	ohdr->bth[2] = 0; /* PSN 0 */
67288733e3bSDon Hiatt 
67388733e3bSDon Hiatt 	/* Convert dwords to flits */
67488733e3bSDon Hiatt 	len = (hwords + nwords) >> 1;
67588733e3bSDon Hiatt 	hfi1_make_16b_hdr(&hdr, slid, dlid, len, pkey, 1, 0, l4, sc5);
67688733e3bSDon Hiatt 
67788733e3bSDon Hiatt 	plen = 2 /* PBC */ + hwords + nwords;
67888733e3bSDon Hiatt 	pbc_flags |= PBC_PACKET_BYPASS | PBC_INSERT_BYPASS_ICRC;
67988733e3bSDon Hiatt 	vl = sc_to_vlt(ppd->dd, sc5);
68088733e3bSDon Hiatt 	pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
68188733e3bSDon Hiatt 	if (ctxt) {
68288733e3bSDon Hiatt 		pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
68388733e3bSDon Hiatt 		if (pbuf)
68488733e3bSDon Hiatt 			ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
68588733e3bSDon Hiatt 						 &hdr, hwords);
68688733e3bSDon Hiatt 	}
68788733e3bSDon Hiatt }
68888733e3bSDon Hiatt 
689f48ad614SDennis Dalessandro void return_cnp(struct hfi1_ibport *ibp, struct rvt_qp *qp, u32 remote_qpn,
690f59fb9e0SMike Marciniszyn 		u16 pkey, u32 slid, u32 dlid, u8 sc5,
691f48ad614SDennis Dalessandro 		const struct ib_grh *old_grh)
692f48ad614SDennis Dalessandro {
693f48ad614SDennis Dalessandro 	u64 pbc, pbc_flags = 0;
694f48ad614SDennis Dalessandro 	u32 bth0, plen, vl, hwords = 5;
695f48ad614SDennis Dalessandro 	u16 lrh0;
696f48ad614SDennis Dalessandro 	u8 sl = ibp->sc_to_sl[sc5];
697261a4351SMike Marciniszyn 	struct ib_header hdr;
698261a4351SMike Marciniszyn 	struct ib_other_headers *ohdr;
699f48ad614SDennis Dalessandro 	struct pio_buf *pbuf;
700f48ad614SDennis Dalessandro 	struct send_context *ctxt = qp_to_send_context(qp, sc5);
701f48ad614SDennis Dalessandro 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
702f48ad614SDennis Dalessandro 
703f48ad614SDennis Dalessandro 	if (old_grh) {
704f48ad614SDennis Dalessandro 		struct ib_grh *grh = &hdr.u.l.grh;
705f48ad614SDennis Dalessandro 
706f48ad614SDennis Dalessandro 		grh->version_tclass_flow = old_grh->version_tclass_flow;
70778d3633bSMike Marciniszyn 		grh->paylen = cpu_to_be16(
70878d3633bSMike Marciniszyn 			(hwords - LRH_9B_DWORDS + SIZE_OF_CRC) << 2);
709f48ad614SDennis Dalessandro 		grh->hop_limit = 0xff;
710f48ad614SDennis Dalessandro 		grh->sgid = old_grh->dgid;
711f48ad614SDennis Dalessandro 		grh->dgid = old_grh->sgid;
712f48ad614SDennis Dalessandro 		ohdr = &hdr.u.l.oth;
713f48ad614SDennis Dalessandro 		lrh0 = HFI1_LRH_GRH;
714f48ad614SDennis Dalessandro 		hwords += sizeof(struct ib_grh) / sizeof(u32);
715f48ad614SDennis Dalessandro 	} else {
716f48ad614SDennis Dalessandro 		ohdr = &hdr.u.oth;
717f48ad614SDennis Dalessandro 		lrh0 = HFI1_LRH_BTH;
718f48ad614SDennis Dalessandro 	}
719f48ad614SDennis Dalessandro 
720f48ad614SDennis Dalessandro 	lrh0 |= (sc5 & 0xf) << 12 | sl << 4;
721f48ad614SDennis Dalessandro 
722f48ad614SDennis Dalessandro 	bth0 = pkey | (IB_OPCODE_CNP << 24);
723f48ad614SDennis Dalessandro 	ohdr->bth[0] = cpu_to_be32(bth0);
724f48ad614SDennis Dalessandro 
7253d591099SDon Hiatt 	ohdr->bth[1] = cpu_to_be32(remote_qpn | (1 << IB_BECN_SHIFT));
726f48ad614SDennis Dalessandro 	ohdr->bth[2] = 0; /* PSN 0 */
727f48ad614SDennis Dalessandro 
72888733e3bSDon Hiatt 	hfi1_make_ib_hdr(&hdr, lrh0, hwords + SIZE_OF_CRC, dlid, slid);
729f48ad614SDennis Dalessandro 	plen = 2 /* PBC */ + hwords;
7307dafbab3SDon Hiatt 	pbc_flags |= (ib_is_sc5(sc5) << PBC_DC_INFO_SHIFT);
731f48ad614SDennis Dalessandro 	vl = sc_to_vlt(ppd->dd, sc5);
732f48ad614SDennis Dalessandro 	pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
733f48ad614SDennis Dalessandro 	if (ctxt) {
734f48ad614SDennis Dalessandro 		pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
735f48ad614SDennis Dalessandro 		if (pbuf)
736f48ad614SDennis Dalessandro 			ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
737f48ad614SDennis Dalessandro 						 &hdr, hwords);
738f48ad614SDennis Dalessandro 	}
739f48ad614SDennis Dalessandro }
740f48ad614SDennis Dalessandro 
741f48ad614SDennis Dalessandro /*
742f48ad614SDennis Dalessandro  * opa_smp_check() - Do the regular pkey checking, and the additional
743f3809209SJianxin Xiong  * checks for SMPs specified in OPAv1 rev 1.0, 9/19/2016 update, section
744f3809209SJianxin Xiong  * 9.10.25 ("SMA Packet Checks").
745f48ad614SDennis Dalessandro  *
746f48ad614SDennis Dalessandro  * Note that:
747f48ad614SDennis Dalessandro  *   - Checks are done using the pkey directly from the packet's BTH,
748f48ad614SDennis Dalessandro  *     and specifically _not_ the pkey that we attach to the completion,
749f48ad614SDennis Dalessandro  *     which may be different.
750f48ad614SDennis Dalessandro  *   - These checks are specifically for "non-local" SMPs (i.e., SMPs
751f48ad614SDennis Dalessandro  *     which originated on another node). SMPs which are sent from, and
752f48ad614SDennis Dalessandro  *     destined to this node are checked in opa_local_smp_check().
753f48ad614SDennis Dalessandro  *
754f48ad614SDennis Dalessandro  * At the point where opa_smp_check() is called, we know:
755f48ad614SDennis Dalessandro  *   - destination QP is QP0
756f48ad614SDennis Dalessandro  *
757f48ad614SDennis Dalessandro  * opa_smp_check() returns 0 if all checks succeed, 1 otherwise.
758f48ad614SDennis Dalessandro  */
759f48ad614SDennis Dalessandro static int opa_smp_check(struct hfi1_ibport *ibp, u16 pkey, u8 sc5,
760f48ad614SDennis Dalessandro 			 struct rvt_qp *qp, u16 slid, struct opa_smp *smp)
761f48ad614SDennis Dalessandro {
762f48ad614SDennis Dalessandro 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
763f48ad614SDennis Dalessandro 
764f48ad614SDennis Dalessandro 	/*
765f48ad614SDennis Dalessandro 	 * I don't think it's possible for us to get here with sc != 0xf,
766f48ad614SDennis Dalessandro 	 * but check it to be certain.
767f48ad614SDennis Dalessandro 	 */
768f48ad614SDennis Dalessandro 	if (sc5 != 0xf)
769f48ad614SDennis Dalessandro 		return 1;
770f48ad614SDennis Dalessandro 
771f48ad614SDennis Dalessandro 	if (rcv_pkey_check(ppd, pkey, sc5, slid))
772f48ad614SDennis Dalessandro 		return 1;
773f48ad614SDennis Dalessandro 
774f48ad614SDennis Dalessandro 	/*
775f48ad614SDennis Dalessandro 	 * At this point we know (and so don't need to check again) that
776f48ad614SDennis Dalessandro 	 * the pkey is either LIM_MGMT_P_KEY, or FULL_MGMT_P_KEY
777f48ad614SDennis Dalessandro 	 * (see ingress_pkey_check).
778f48ad614SDennis Dalessandro 	 */
779f48ad614SDennis Dalessandro 	if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE &&
780f48ad614SDennis Dalessandro 	    smp->mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED) {
781f48ad614SDennis Dalessandro 		ingress_pkey_table_fail(ppd, pkey, slid);
782f48ad614SDennis Dalessandro 		return 1;
783f48ad614SDennis Dalessandro 	}
784f48ad614SDennis Dalessandro 
785f48ad614SDennis Dalessandro 	/*
786f48ad614SDennis Dalessandro 	 * SMPs fall into one of four (disjoint) categories:
787f3809209SJianxin Xiong 	 * SMA request, SMA response, SMA trap, or SMA trap repress.
788f3809209SJianxin Xiong 	 * Our response depends, in part, on which type of SMP we're
789f3809209SJianxin Xiong 	 * processing.
790f48ad614SDennis Dalessandro 	 *
791f3809209SJianxin Xiong 	 * If this is an SMA response, skip the check here.
792f3809209SJianxin Xiong 	 *
793f3809209SJianxin Xiong 	 * If this is an SMA request or SMA trap repress:
794f3809209SJianxin Xiong 	 *   - pkey != FULL_MGMT_P_KEY =>
795f48ad614SDennis Dalessandro 	 *       increment port recv constraint errors, drop MAD
796f3809209SJianxin Xiong 	 *
797f3809209SJianxin Xiong 	 * Otherwise:
798f3809209SJianxin Xiong 	 *    - accept if the port is running an SM
799f3809209SJianxin Xiong 	 *    - drop MAD if it's an SMA trap
800f3809209SJianxin Xiong 	 *    - pkey == FULL_MGMT_P_KEY =>
801f3809209SJianxin Xiong 	 *        reply with unsupported method
802f48ad614SDennis Dalessandro 	 *    - pkey != FULL_MGMT_P_KEY =>
803f48ad614SDennis Dalessandro 	 *	  increment port recv constraint errors, drop MAD
804f48ad614SDennis Dalessandro 	 */
805f48ad614SDennis Dalessandro 	switch (smp->method) {
806f3809209SJianxin Xiong 	case IB_MGMT_METHOD_GET_RESP:
807f3809209SJianxin Xiong 	case IB_MGMT_METHOD_REPORT_RESP:
808f3809209SJianxin Xiong 		break;
809f48ad614SDennis Dalessandro 	case IB_MGMT_METHOD_GET:
810f48ad614SDennis Dalessandro 	case IB_MGMT_METHOD_SET:
811f48ad614SDennis Dalessandro 	case IB_MGMT_METHOD_REPORT:
812f48ad614SDennis Dalessandro 	case IB_MGMT_METHOD_TRAP_REPRESS:
813f48ad614SDennis Dalessandro 		if (pkey != FULL_MGMT_P_KEY) {
814f48ad614SDennis Dalessandro 			ingress_pkey_table_fail(ppd, pkey, slid);
815f48ad614SDennis Dalessandro 			return 1;
816f48ad614SDennis Dalessandro 		}
817f48ad614SDennis Dalessandro 		break;
818f3809209SJianxin Xiong 	default:
819f48ad614SDennis Dalessandro 		if (ibp->rvp.port_cap_flags & IB_PORT_SM)
820f48ad614SDennis Dalessandro 			return 0;
821f3809209SJianxin Xiong 		if (smp->method == IB_MGMT_METHOD_TRAP)
822f3809209SJianxin Xiong 			return 1;
823f48ad614SDennis Dalessandro 		if (pkey == FULL_MGMT_P_KEY) {
824f48ad614SDennis Dalessandro 			smp->status |= IB_SMP_UNSUP_METHOD;
825f48ad614SDennis Dalessandro 			return 0;
826f48ad614SDennis Dalessandro 		}
827f48ad614SDennis Dalessandro 		ingress_pkey_table_fail(ppd, pkey, slid);
828f48ad614SDennis Dalessandro 		return 1;
829f48ad614SDennis Dalessandro 	}
830f48ad614SDennis Dalessandro 	return 0;
831f48ad614SDennis Dalessandro }
832f48ad614SDennis Dalessandro 
833f48ad614SDennis Dalessandro /**
834f48ad614SDennis Dalessandro  * hfi1_ud_rcv - receive an incoming UD packet
835f48ad614SDennis Dalessandro  * @ibp: the port the packet came in on
836f48ad614SDennis Dalessandro  * @hdr: the packet header
837f48ad614SDennis Dalessandro  * @rcv_flags: flags relevant to rcv processing
838f48ad614SDennis Dalessandro  * @data: the packet data
839f48ad614SDennis Dalessandro  * @tlen: the packet length
840f48ad614SDennis Dalessandro  * @qp: the QP the packet came on
841f48ad614SDennis Dalessandro  *
842f48ad614SDennis Dalessandro  * This is called from qp_rcv() to process an incoming UD packet
843f48ad614SDennis Dalessandro  * for the given QP.
844f48ad614SDennis Dalessandro  * Called at interrupt level.
845f48ad614SDennis Dalessandro  */
846f48ad614SDennis Dalessandro void hfi1_ud_rcv(struct hfi1_packet *packet)
847f48ad614SDennis Dalessandro {
848261a4351SMike Marciniszyn 	struct ib_other_headers *ohdr = packet->ohdr;
849f48ad614SDennis Dalessandro 	u32 hdrsize = packet->hlen;
850f48ad614SDennis Dalessandro 	struct ib_wc wc;
851f48ad614SDennis Dalessandro 	u32 qkey;
852f48ad614SDennis Dalessandro 	u32 src_qp;
8539039746cSDon Hiatt 	u16 pkey;
854f48ad614SDennis Dalessandro 	int mgmt_pkey_idx = -1;
855f3e862cbSSebastian Sanchez 	struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
85689c057caSDasaratharaman Chandramouli 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
85772c07e2bSDon Hiatt 	void *data = packet->payload;
858f48ad614SDennis Dalessandro 	u32 tlen = packet->tlen;
859f48ad614SDennis Dalessandro 	struct rvt_qp *qp = packet->qp;
86088733e3bSDon Hiatt 	u8 sc5 = packet->sc;
8619039746cSDon Hiatt 	u8 sl_from_sc;
8629039746cSDon Hiatt 	u8 opcode = packet->opcode;
8639039746cSDon Hiatt 	u8 sl = packet->sl;
8649039746cSDon Hiatt 	u32 dlid = packet->dlid;
8659039746cSDon Hiatt 	u32 slid = packet->slid;
86688733e3bSDon Hiatt 	u8 extra_bytes;
86788733e3bSDon Hiatt 	bool dlid_is_permissive;
86888733e3bSDon Hiatt 	bool slid_is_permissive;
869f48ad614SDennis Dalessandro 
87088733e3bSDon Hiatt 	extra_bytes = packet->pad + packet->extra_byte + (SIZE_OF_CRC << 2);
8717dafbab3SDon Hiatt 	qkey = ib_get_qkey(ohdr);
8727dafbab3SDon Hiatt 	src_qp = ib_get_sqpn(ohdr);
87388733e3bSDon Hiatt 
87488733e3bSDon Hiatt 	if (packet->etype == RHF_RCV_TYPE_BYPASS) {
87588733e3bSDon Hiatt 		u32 permissive_lid =
87688733e3bSDon Hiatt 			opa_get_lid(be32_to_cpu(OPA_LID_PERMISSIVE), 16B);
87788733e3bSDon Hiatt 
87888733e3bSDon Hiatt 		pkey = hfi1_16B_get_pkey(packet->hdr);
87988733e3bSDon Hiatt 		dlid_is_permissive = (dlid == permissive_lid);
88088733e3bSDon Hiatt 		slid_is_permissive = (slid == permissive_lid);
88188733e3bSDon Hiatt 	} else {
882cb427057SDon Hiatt 		pkey = ib_bth_get_pkey(ohdr);
88388733e3bSDon Hiatt 		dlid_is_permissive = (dlid == be16_to_cpu(IB_LID_PERMISSIVE));
88488733e3bSDon Hiatt 		slid_is_permissive = (slid == be16_to_cpu(IB_LID_PERMISSIVE));
88588733e3bSDon Hiatt 	}
88689c057caSDasaratharaman Chandramouli 	sl_from_sc = ibp->sc_to_sl[sc5];
887f48ad614SDennis Dalessandro 
8885fd2b562SMitko Haralanov 	process_ecn(qp, packet, (opcode != IB_OPCODE_CNP));
889f48ad614SDennis Dalessandro 	/*
890f48ad614SDennis Dalessandro 	 * Get the number of bytes the message was padded by
891f48ad614SDennis Dalessandro 	 * and drop incomplete packets.
892f48ad614SDennis Dalessandro 	 */
89389c057caSDasaratharaman Chandramouli 	if (unlikely(tlen < (hdrsize + extra_bytes)))
894f48ad614SDennis Dalessandro 		goto drop;
895f48ad614SDennis Dalessandro 
89689c057caSDasaratharaman Chandramouli 	tlen -= hdrsize + extra_bytes;
897f48ad614SDennis Dalessandro 
898f48ad614SDennis Dalessandro 	/*
899f48ad614SDennis Dalessandro 	 * Check that the permissive LID is only used on QP0
900f48ad614SDennis Dalessandro 	 * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
901f48ad614SDennis Dalessandro 	 */
902f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_num) {
90388733e3bSDon Hiatt 		if (unlikely(dlid_is_permissive || slid_is_permissive))
904f48ad614SDennis Dalessandro 			goto drop;
905f48ad614SDennis Dalessandro 		if (qp->ibqp.qp_num > 1) {
906f48ad614SDennis Dalessandro 			if (unlikely(rcv_pkey_check(ppd, pkey, sc5, slid))) {
907f48ad614SDennis Dalessandro 				/*
908f48ad614SDennis Dalessandro 				 * Traps will not be sent for packets dropped
909f48ad614SDennis Dalessandro 				 * by the HW. This is fine, as sending trap
910f48ad614SDennis Dalessandro 				 * for invalid pkeys is optional according to
911f48ad614SDennis Dalessandro 				 * IB spec (release 1.3, section 10.9.4)
912f48ad614SDennis Dalessandro 				 */
91313d84914SDennis Dalessandro 				hfi1_bad_pkey(ibp,
91489c057caSDasaratharaman Chandramouli 					      pkey, sl,
915f48ad614SDennis Dalessandro 					      src_qp, qp->ibqp.qp_num,
91689c057caSDasaratharaman Chandramouli 					      slid, dlid);
917f48ad614SDennis Dalessandro 				return;
918f48ad614SDennis Dalessandro 			}
919f48ad614SDennis Dalessandro 		} else {
920f48ad614SDennis Dalessandro 			/* GSI packet */
921f48ad614SDennis Dalessandro 			mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
922f48ad614SDennis Dalessandro 			if (mgmt_pkey_idx < 0)
923f48ad614SDennis Dalessandro 				goto drop;
924f48ad614SDennis Dalessandro 		}
92513d84914SDennis Dalessandro 		if (unlikely(qkey != qp->qkey)) /* Silent drop */
926f48ad614SDennis Dalessandro 			return;
92713d84914SDennis Dalessandro 
928f48ad614SDennis Dalessandro 		/* Drop invalid MAD packets (see 13.5.3.1). */
929f48ad614SDennis Dalessandro 		if (unlikely(qp->ibqp.qp_num == 1 &&
93089c057caSDasaratharaman Chandramouli 			     (tlen > 2048 || (sc5 == 0xF))))
931f48ad614SDennis Dalessandro 			goto drop;
932f48ad614SDennis Dalessandro 	} else {
933f48ad614SDennis Dalessandro 		/* Received on QP0, and so by definition, this is an SMP */
934f48ad614SDennis Dalessandro 		struct opa_smp *smp = (struct opa_smp *)data;
935f48ad614SDennis Dalessandro 
936f48ad614SDennis Dalessandro 		if (opa_smp_check(ibp, pkey, sc5, qp, slid, smp))
937f48ad614SDennis Dalessandro 			goto drop;
938f48ad614SDennis Dalessandro 
939f48ad614SDennis Dalessandro 		if (tlen > 2048)
940f48ad614SDennis Dalessandro 			goto drop;
94188733e3bSDon Hiatt 		if ((dlid_is_permissive || slid_is_permissive) &&
942f48ad614SDennis Dalessandro 		    smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
943f48ad614SDennis Dalessandro 			goto drop;
944f48ad614SDennis Dalessandro 
945f48ad614SDennis Dalessandro 		/* look up SMI pkey */
946f48ad614SDennis Dalessandro 		mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
947f48ad614SDennis Dalessandro 		if (mgmt_pkey_idx < 0)
948f48ad614SDennis Dalessandro 			goto drop;
949f48ad614SDennis Dalessandro 	}
950f48ad614SDennis Dalessandro 
951f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_num > 1 &&
952f48ad614SDennis Dalessandro 	    opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
953f48ad614SDennis Dalessandro 		wc.ex.imm_data = ohdr->u.ud.imm_data;
954f48ad614SDennis Dalessandro 		wc.wc_flags = IB_WC_WITH_IMM;
955f48ad614SDennis Dalessandro 		tlen -= sizeof(u32);
956f48ad614SDennis Dalessandro 	} else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
957f48ad614SDennis Dalessandro 		wc.ex.imm_data = 0;
958f48ad614SDennis Dalessandro 		wc.wc_flags = 0;
959f48ad614SDennis Dalessandro 	} else {
960f48ad614SDennis Dalessandro 		goto drop;
961f48ad614SDennis Dalessandro 	}
962f48ad614SDennis Dalessandro 
963f48ad614SDennis Dalessandro 	/*
964f48ad614SDennis Dalessandro 	 * A GRH is expected to precede the data even if not
965f48ad614SDennis Dalessandro 	 * present on the wire.
966f48ad614SDennis Dalessandro 	 */
967f48ad614SDennis Dalessandro 	wc.byte_len = tlen + sizeof(struct ib_grh);
968f48ad614SDennis Dalessandro 
969f48ad614SDennis Dalessandro 	/*
970f48ad614SDennis Dalessandro 	 * Get the next work request entry to find where to put the data.
971f48ad614SDennis Dalessandro 	 */
972f48ad614SDennis Dalessandro 	if (qp->r_flags & RVT_R_REUSE_SGE) {
973f48ad614SDennis Dalessandro 		qp->r_flags &= ~RVT_R_REUSE_SGE;
974f48ad614SDennis Dalessandro 	} else {
975f48ad614SDennis Dalessandro 		int ret;
976f48ad614SDennis Dalessandro 
977*832369faSBrian Welty 		ret = rvt_get_rwqe(qp, false);
978f48ad614SDennis Dalessandro 		if (ret < 0) {
979beb5a042SBrian Welty 			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
980f48ad614SDennis Dalessandro 			return;
981f48ad614SDennis Dalessandro 		}
982f48ad614SDennis Dalessandro 		if (!ret) {
983f48ad614SDennis Dalessandro 			if (qp->ibqp.qp_num == 0)
984f48ad614SDennis Dalessandro 				ibp->rvp.n_vl15_dropped++;
985f48ad614SDennis Dalessandro 			return;
986f48ad614SDennis Dalessandro 		}
987f48ad614SDennis Dalessandro 	}
988f48ad614SDennis Dalessandro 	/* Silently drop packets which are too big. */
989f48ad614SDennis Dalessandro 	if (unlikely(wc.byte_len > qp->r_len)) {
990f48ad614SDennis Dalessandro 		qp->r_flags |= RVT_R_REUSE_SGE;
991f48ad614SDennis Dalessandro 		goto drop;
992f48ad614SDennis Dalessandro 	}
9939039746cSDon Hiatt 	if (packet->grh) {
99488733e3bSDon Hiatt 		hfi1_copy_sge(&qp->r_sge, packet->grh,
99588733e3bSDon Hiatt 			      sizeof(struct ib_grh), true, false);
99688733e3bSDon Hiatt 		wc.wc_flags |= IB_WC_GRH;
99788733e3bSDon Hiatt 	} else if (packet->etype == RHF_RCV_TYPE_BYPASS) {
99888733e3bSDon Hiatt 		struct ib_grh grh;
99988733e3bSDon Hiatt 		/*
100088733e3bSDon Hiatt 		 * Assuming we only created 16B on the send side
100188733e3bSDon Hiatt 		 * if we want to use large LIDs, since GRH was stripped
100288733e3bSDon Hiatt 		 * out when creating 16B, add back the GRH here.
100388733e3bSDon Hiatt 		 */
100488733e3bSDon Hiatt 		hfi1_make_ext_grh(packet, &grh, slid, dlid);
100588733e3bSDon Hiatt 		hfi1_copy_sge(&qp->r_sge, &grh,
10060128fceaSBrian Welty 			      sizeof(struct ib_grh), true, false);
1007f48ad614SDennis Dalessandro 		wc.wc_flags |= IB_WC_GRH;
1008f48ad614SDennis Dalessandro 	} else {
10091198fceaSBrian Welty 		rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
1010f48ad614SDennis Dalessandro 	}
1011f48ad614SDennis Dalessandro 	hfi1_copy_sge(&qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh),
10120128fceaSBrian Welty 		      true, false);
1013f48ad614SDennis Dalessandro 	rvt_put_ss(&qp->r_sge);
1014f48ad614SDennis Dalessandro 	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
1015f48ad614SDennis Dalessandro 		return;
1016f48ad614SDennis Dalessandro 	wc.wr_id = qp->r_wr_id;
1017f48ad614SDennis Dalessandro 	wc.status = IB_WC_SUCCESS;
1018f48ad614SDennis Dalessandro 	wc.opcode = IB_WC_RECV;
1019f48ad614SDennis Dalessandro 	wc.vendor_err = 0;
1020f48ad614SDennis Dalessandro 	wc.qp = &qp->ibqp;
1021f48ad614SDennis Dalessandro 	wc.src_qp = src_qp;
1022f48ad614SDennis Dalessandro 
1023f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_type == IB_QPT_GSI ||
1024f48ad614SDennis Dalessandro 	    qp->ibqp.qp_type == IB_QPT_SMI) {
1025f48ad614SDennis Dalessandro 		if (mgmt_pkey_idx < 0) {
1026f48ad614SDennis Dalessandro 			if (net_ratelimit()) {
1027f48ad614SDennis Dalessandro 				struct hfi1_devdata *dd = ppd->dd;
1028f48ad614SDennis Dalessandro 
1029f48ad614SDennis Dalessandro 				dd_dev_err(dd, "QP type %d mgmt_pkey_idx < 0 and packet not dropped???\n",
1030f48ad614SDennis Dalessandro 					   qp->ibqp.qp_type);
1031f48ad614SDennis Dalessandro 				mgmt_pkey_idx = 0;
1032f48ad614SDennis Dalessandro 			}
1033f48ad614SDennis Dalessandro 		}
1034f48ad614SDennis Dalessandro 		wc.pkey_index = (unsigned)mgmt_pkey_idx;
1035f48ad614SDennis Dalessandro 	} else {
1036f48ad614SDennis Dalessandro 		wc.pkey_index = 0;
1037f48ad614SDennis Dalessandro 	}
103888733e3bSDon Hiatt 	if (slid_is_permissive)
103988733e3bSDon Hiatt 		slid = be32_to_cpu(OPA_LID_PERMISSIVE);
1040b64581adSDon Hiatt 	wc.slid = slid & U16_MAX;
104189c057caSDasaratharaman Chandramouli 	wc.sl = sl_from_sc;
1042f48ad614SDennis Dalessandro 
1043f48ad614SDennis Dalessandro 	/*
1044f48ad614SDennis Dalessandro 	 * Save the LMC lower bits if the destination LID is a unicast LID.
1045f48ad614SDennis Dalessandro 	 */
104688733e3bSDon Hiatt 	wc.dlid_path_bits = hfi1_check_mcast(dlid) ? 0 :
1047f48ad614SDennis Dalessandro 		dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
1048f48ad614SDennis Dalessandro 	wc.port_num = qp->port_num;
1049f48ad614SDennis Dalessandro 	/* Signal completion event if the solicited bit is set. */
1050f48ad614SDennis Dalessandro 	rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
1051f150e273SSebastian Sanchez 		     ib_bth_is_solicited(ohdr));
1052f48ad614SDennis Dalessandro 	return;
1053f48ad614SDennis Dalessandro 
1054f48ad614SDennis Dalessandro drop:
1055f48ad614SDennis Dalessandro 	ibp->rvp.n_pkt_drops++;
1056f48ad614SDennis Dalessandro }
1057