xref: /linux/drivers/infiniband/hw/hfi1/ud.c (revision fe4dd4239277486ca3a468e7bbeafd7ef3a5634e)
1f48ad614SDennis Dalessandro /*
2b697d7d8SMichael J. Ruhl  * Copyright(c) 2015 - 2018 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"
54*fe4dd423SMitko Haralanov #include "trace_ibhdrs.h"
55f48ad614SDennis Dalessandro #include "qp.h"
56f48ad614SDennis Dalessandro 
5788733e3bSDon Hiatt /* We support only two types - 9B and 16B for now */
5888733e3bSDon Hiatt static const hfi1_make_req hfi1_make_ud_req_tbl[2] = {
5988733e3bSDon Hiatt 	[HFI1_PKT_TYPE_9B] = &hfi1_make_ud_req_9B,
6088733e3bSDon Hiatt 	[HFI1_PKT_TYPE_16B] = &hfi1_make_ud_req_16B
6188733e3bSDon Hiatt };
6288733e3bSDon Hiatt 
63f48ad614SDennis Dalessandro /**
64f48ad614SDennis Dalessandro  * ud_loopback - handle send on loopback QPs
65f48ad614SDennis Dalessandro  * @sqp: the sending QP
66f48ad614SDennis Dalessandro  * @swqe: the send work request
67f48ad614SDennis Dalessandro  *
68f48ad614SDennis Dalessandro  * This is called from hfi1_make_ud_req() to forward a WQE addressed
69f48ad614SDennis Dalessandro  * to the same HFI.
70f48ad614SDennis Dalessandro  * Note that the receive interrupt handler may be calling hfi1_ud_rcv()
71f48ad614SDennis Dalessandro  * while this is being called.
72f48ad614SDennis Dalessandro  */
73f48ad614SDennis Dalessandro static void ud_loopback(struct rvt_qp *sqp, struct rvt_swqe *swqe)
74f48ad614SDennis Dalessandro {
75f48ad614SDennis Dalessandro 	struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
76f48ad614SDennis Dalessandro 	struct hfi1_pportdata *ppd;
7788733e3bSDon Hiatt 	struct hfi1_qp_priv *priv = sqp->priv;
78f48ad614SDennis Dalessandro 	struct rvt_qp *qp;
7990898850SDasaratharaman Chandramouli 	struct rdma_ah_attr *ah_attr;
80f48ad614SDennis Dalessandro 	unsigned long flags;
81f48ad614SDennis Dalessandro 	struct rvt_sge_state ssge;
82f48ad614SDennis Dalessandro 	struct rvt_sge *sge;
83f48ad614SDennis Dalessandro 	struct ib_wc wc;
84f48ad614SDennis Dalessandro 	u32 length;
85f48ad614SDennis Dalessandro 	enum ib_qp_type sqptype, dqptype;
86f48ad614SDennis Dalessandro 
87f48ad614SDennis Dalessandro 	rcu_read_lock();
88f48ad614SDennis Dalessandro 
89f48ad614SDennis Dalessandro 	qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
90f48ad614SDennis Dalessandro 			    swqe->ud_wr.remote_qpn);
91f48ad614SDennis Dalessandro 	if (!qp) {
92f48ad614SDennis Dalessandro 		ibp->rvp.n_pkt_drops++;
93f48ad614SDennis Dalessandro 		rcu_read_unlock();
94f48ad614SDennis Dalessandro 		return;
95f48ad614SDennis Dalessandro 	}
96f48ad614SDennis Dalessandro 
97f48ad614SDennis Dalessandro 	sqptype = sqp->ibqp.qp_type == IB_QPT_GSI ?
98f48ad614SDennis Dalessandro 			IB_QPT_UD : sqp->ibqp.qp_type;
99f48ad614SDennis Dalessandro 	dqptype = qp->ibqp.qp_type == IB_QPT_GSI ?
100f48ad614SDennis Dalessandro 			IB_QPT_UD : qp->ibqp.qp_type;
101f48ad614SDennis Dalessandro 
102f48ad614SDennis Dalessandro 	if (dqptype != sqptype ||
103f48ad614SDennis Dalessandro 	    !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
104f48ad614SDennis Dalessandro 		ibp->rvp.n_pkt_drops++;
105f48ad614SDennis Dalessandro 		goto drop;
106f48ad614SDennis Dalessandro 	}
107f48ad614SDennis Dalessandro 
108f48ad614SDennis Dalessandro 	ah_attr = &ibah_to_rvtah(swqe->ud_wr.ah)->attr;
109f48ad614SDennis Dalessandro 	ppd = ppd_from_ibp(ibp);
110f48ad614SDennis Dalessandro 
111f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_num > 1) {
112f48ad614SDennis Dalessandro 		u16 pkey;
11388733e3bSDon Hiatt 		u32 slid;
114d8966fcdSDasaratharaman Chandramouli 		u8 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
115f48ad614SDennis Dalessandro 
116f48ad614SDennis Dalessandro 		pkey = hfi1_get_pkey(ibp, sqp->s_pkey_index);
117d8966fcdSDasaratharaman Chandramouli 		slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
118f48ad614SDennis Dalessandro 				   ((1 << ppd->lmc) - 1));
119f48ad614SDennis Dalessandro 		if (unlikely(ingress_pkey_check(ppd, pkey, sc5,
1205786adf3SDon Hiatt 						qp->s_pkey_index,
1215786adf3SDon Hiatt 						slid, false))) {
12213d84914SDennis Dalessandro 			hfi1_bad_pkey(ibp, pkey,
123d8966fcdSDasaratharaman Chandramouli 				      rdma_ah_get_sl(ah_attr),
124f48ad614SDennis Dalessandro 				      sqp->ibqp.qp_num, qp->ibqp.qp_num,
125d8966fcdSDasaratharaman Chandramouli 				      slid, rdma_ah_get_dlid(ah_attr));
126f48ad614SDennis Dalessandro 			goto drop;
127f48ad614SDennis Dalessandro 		}
128f48ad614SDennis Dalessandro 	}
129f48ad614SDennis Dalessandro 
130f48ad614SDennis Dalessandro 	/*
131f48ad614SDennis Dalessandro 	 * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
132f48ad614SDennis Dalessandro 	 * Qkeys with the high order bit set mean use the
133f48ad614SDennis Dalessandro 	 * qkey from the QP context instead of the WR (see 10.2.5).
134f48ad614SDennis Dalessandro 	 */
135f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_num) {
136f48ad614SDennis Dalessandro 		u32 qkey;
137f48ad614SDennis Dalessandro 
138f48ad614SDennis Dalessandro 		qkey = (int)swqe->ud_wr.remote_qkey < 0 ?
139f48ad614SDennis Dalessandro 			sqp->qkey : swqe->ud_wr.remote_qkey;
14013d84914SDennis Dalessandro 		if (unlikely(qkey != qp->qkey))
14113d84914SDennis Dalessandro 			goto drop; /* silently drop per IBTA spec */
142f48ad614SDennis Dalessandro 	}
143f48ad614SDennis Dalessandro 
144f48ad614SDennis Dalessandro 	/*
145f48ad614SDennis Dalessandro 	 * A GRH is expected to precede the data even if not
146f48ad614SDennis Dalessandro 	 * present on the wire.
147f48ad614SDennis Dalessandro 	 */
148f48ad614SDennis Dalessandro 	length = swqe->length;
149f48ad614SDennis Dalessandro 	memset(&wc, 0, sizeof(wc));
150f48ad614SDennis Dalessandro 	wc.byte_len = length + sizeof(struct ib_grh);
151f48ad614SDennis Dalessandro 
152f48ad614SDennis Dalessandro 	if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
153f48ad614SDennis Dalessandro 		wc.wc_flags = IB_WC_WITH_IMM;
154f48ad614SDennis Dalessandro 		wc.ex.imm_data = swqe->wr.ex.imm_data;
155f48ad614SDennis Dalessandro 	}
156f48ad614SDennis Dalessandro 
157f48ad614SDennis Dalessandro 	spin_lock_irqsave(&qp->r_lock, flags);
158f48ad614SDennis Dalessandro 
159f48ad614SDennis Dalessandro 	/*
160f48ad614SDennis Dalessandro 	 * Get the next work request entry to find where to put the data.
161f48ad614SDennis Dalessandro 	 */
162f48ad614SDennis Dalessandro 	if (qp->r_flags & RVT_R_REUSE_SGE) {
163f48ad614SDennis Dalessandro 		qp->r_flags &= ~RVT_R_REUSE_SGE;
164f48ad614SDennis Dalessandro 	} else {
165f48ad614SDennis Dalessandro 		int ret;
166f48ad614SDennis Dalessandro 
167832369faSBrian Welty 		ret = rvt_get_rwqe(qp, false);
168f48ad614SDennis Dalessandro 		if (ret < 0) {
169beb5a042SBrian Welty 			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
170f48ad614SDennis Dalessandro 			goto bail_unlock;
171f48ad614SDennis Dalessandro 		}
172f48ad614SDennis Dalessandro 		if (!ret) {
173f48ad614SDennis Dalessandro 			if (qp->ibqp.qp_num == 0)
174f48ad614SDennis Dalessandro 				ibp->rvp.n_vl15_dropped++;
175f48ad614SDennis Dalessandro 			goto bail_unlock;
176f48ad614SDennis Dalessandro 		}
177f48ad614SDennis Dalessandro 	}
178f48ad614SDennis Dalessandro 	/* Silently drop packets which are too big. */
179f48ad614SDennis Dalessandro 	if (unlikely(wc.byte_len > qp->r_len)) {
180f48ad614SDennis Dalessandro 		qp->r_flags |= RVT_R_REUSE_SGE;
181f48ad614SDennis Dalessandro 		ibp->rvp.n_pkt_drops++;
182f48ad614SDennis Dalessandro 		goto bail_unlock;
183f48ad614SDennis Dalessandro 	}
184f48ad614SDennis Dalessandro 
185d8966fcdSDasaratharaman Chandramouli 	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
186527dbf12SDasaratharaman Chandramouli 		struct ib_grh grh;
18788733e3bSDon Hiatt 		struct ib_global_route grd = *(rdma_ah_read_grh(ah_attr));
188527dbf12SDasaratharaman Chandramouli 
18988733e3bSDon Hiatt 		/*
19088733e3bSDon Hiatt 		 * For loopback packets with extended LIDs, the
19188733e3bSDon Hiatt 		 * sgid_index in the GRH is 0 and the dgid is
19288733e3bSDon Hiatt 		 * OPA GID of the sender. While creating a response
19388733e3bSDon Hiatt 		 * to the loopback packet, IB core creates the new
19488733e3bSDon Hiatt 		 * sgid_index from the DGID and that will be the
19588733e3bSDon Hiatt 		 * OPA_GID_INDEX. The new dgid is from the sgid
19688733e3bSDon Hiatt 		 * index and that will be in the IB GID format.
19788733e3bSDon Hiatt 		 *
19888733e3bSDon Hiatt 		 * We now have a case where the sent packet had a
19988733e3bSDon Hiatt 		 * different sgid_index and dgid compared to the
20088733e3bSDon Hiatt 		 * one that was received in response.
20188733e3bSDon Hiatt 		 *
20288733e3bSDon Hiatt 		 * Fix this inconsistency.
20388733e3bSDon Hiatt 		 */
20488733e3bSDon Hiatt 		if (priv->hdr_type == HFI1_PKT_TYPE_16B) {
20588733e3bSDon Hiatt 			if (grd.sgid_index == 0)
20688733e3bSDon Hiatt 				grd.sgid_index = OPA_GID_INDEX;
20788733e3bSDon Hiatt 
20888733e3bSDon Hiatt 			if (ib_is_opa_gid(&grd.dgid))
20988733e3bSDon Hiatt 				grd.dgid.global.interface_id =
21088733e3bSDon Hiatt 				cpu_to_be64(ppd->guids[HFI1_PORT_GUID_INDEX]);
21188733e3bSDon Hiatt 		}
21288733e3bSDon Hiatt 
21388733e3bSDon Hiatt 		hfi1_make_grh(ibp, &grh, &grd, 0, 0);
214019f118bSBrian Welty 		rvt_copy_sge(qp, &qp->r_sge, &grh,
2150128fceaSBrian Welty 			     sizeof(grh), true, false);
216f48ad614SDennis Dalessandro 		wc.wc_flags |= IB_WC_GRH;
217f48ad614SDennis Dalessandro 	} else {
2181198fceaSBrian Welty 		rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
219f48ad614SDennis Dalessandro 	}
220f48ad614SDennis Dalessandro 	ssge.sg_list = swqe->sg_list + 1;
221f48ad614SDennis Dalessandro 	ssge.sge = *swqe->sg_list;
222f48ad614SDennis Dalessandro 	ssge.num_sge = swqe->wr.num_sge;
223f48ad614SDennis Dalessandro 	sge = &ssge.sge;
224f48ad614SDennis Dalessandro 	while (length) {
225f48ad614SDennis Dalessandro 		u32 len = sge->length;
226f48ad614SDennis Dalessandro 
227f48ad614SDennis Dalessandro 		if (len > length)
228f48ad614SDennis Dalessandro 			len = length;
229f48ad614SDennis Dalessandro 		if (len > sge->sge_length)
230f48ad614SDennis Dalessandro 			len = sge->sge_length;
231f48ad614SDennis Dalessandro 		WARN_ON_ONCE(len == 0);
232019f118bSBrian Welty 		rvt_copy_sge(qp, &qp->r_sge, sge->vaddr, len, true, false);
233f48ad614SDennis Dalessandro 		sge->vaddr += len;
234f48ad614SDennis Dalessandro 		sge->length -= len;
235f48ad614SDennis Dalessandro 		sge->sge_length -= len;
236f48ad614SDennis Dalessandro 		if (sge->sge_length == 0) {
237f48ad614SDennis Dalessandro 			if (--ssge.num_sge)
238f48ad614SDennis Dalessandro 				*sge = *ssge.sg_list++;
239f48ad614SDennis Dalessandro 		} else if (sge->length == 0 && sge->mr->lkey) {
240f48ad614SDennis Dalessandro 			if (++sge->n >= RVT_SEGSZ) {
241f48ad614SDennis Dalessandro 				if (++sge->m >= sge->mr->mapsz)
242f48ad614SDennis Dalessandro 					break;
243f48ad614SDennis Dalessandro 				sge->n = 0;
244f48ad614SDennis Dalessandro 			}
245f48ad614SDennis Dalessandro 			sge->vaddr =
246f48ad614SDennis Dalessandro 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
247f48ad614SDennis Dalessandro 			sge->length =
248f48ad614SDennis Dalessandro 				sge->mr->map[sge->m]->segs[sge->n].length;
249f48ad614SDennis Dalessandro 		}
250f48ad614SDennis Dalessandro 		length -= len;
251f48ad614SDennis Dalessandro 	}
252f48ad614SDennis Dalessandro 	rvt_put_ss(&qp->r_sge);
253f48ad614SDennis Dalessandro 	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
254f48ad614SDennis Dalessandro 		goto bail_unlock;
255f48ad614SDennis Dalessandro 	wc.wr_id = qp->r_wr_id;
256f48ad614SDennis Dalessandro 	wc.status = IB_WC_SUCCESS;
257f48ad614SDennis Dalessandro 	wc.opcode = IB_WC_RECV;
258f48ad614SDennis Dalessandro 	wc.qp = &qp->ibqp;
259f48ad614SDennis Dalessandro 	wc.src_qp = sqp->ibqp.qp_num;
260f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI) {
261f48ad614SDennis Dalessandro 		if (sqp->ibqp.qp_type == IB_QPT_GSI ||
262f48ad614SDennis Dalessandro 		    sqp->ibqp.qp_type == IB_QPT_SMI)
263f48ad614SDennis Dalessandro 			wc.pkey_index = swqe->ud_wr.pkey_index;
264f48ad614SDennis Dalessandro 		else
265f48ad614SDennis Dalessandro 			wc.pkey_index = sqp->s_pkey_index;
266f48ad614SDennis Dalessandro 	} else {
267f48ad614SDennis Dalessandro 		wc.pkey_index = 0;
268f48ad614SDennis Dalessandro 	}
269b64581adSDon Hiatt 	wc.slid = (ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
270b64581adSDon Hiatt 				   ((1 << ppd->lmc) - 1))) & U16_MAX;
271f48ad614SDennis Dalessandro 	/* Check for loopback when the port lid is not set */
272f48ad614SDennis Dalessandro 	if (wc.slid == 0 && sqp->ibqp.qp_type == IB_QPT_GSI)
273f48ad614SDennis Dalessandro 		wc.slid = be16_to_cpu(IB_LID_PERMISSIVE);
274d8966fcdSDasaratharaman Chandramouli 	wc.sl = rdma_ah_get_sl(ah_attr);
275d8966fcdSDasaratharaman Chandramouli 	wc.dlid_path_bits = rdma_ah_get_dlid(ah_attr) & ((1 << ppd->lmc) - 1);
276f48ad614SDennis Dalessandro 	wc.port_num = qp->port_num;
277f48ad614SDennis Dalessandro 	/* Signal completion event if the solicited bit is set. */
278f48ad614SDennis Dalessandro 	rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
279f48ad614SDennis Dalessandro 		     swqe->wr.send_flags & IB_SEND_SOLICITED);
280f48ad614SDennis Dalessandro 	ibp->rvp.n_loop_pkts++;
281f48ad614SDennis Dalessandro bail_unlock:
282f48ad614SDennis Dalessandro 	spin_unlock_irqrestore(&qp->r_lock, flags);
283f48ad614SDennis Dalessandro drop:
284f48ad614SDennis Dalessandro 	rcu_read_unlock();
285f48ad614SDennis Dalessandro }
286f48ad614SDennis Dalessandro 
28788733e3bSDon Hiatt static void hfi1_make_bth_deth(struct rvt_qp *qp, struct rvt_swqe *wqe,
28888733e3bSDon Hiatt 			       struct ib_other_headers *ohdr,
28988733e3bSDon Hiatt 			       u16 *pkey, u32 extra_bytes, bool bypass)
29088733e3bSDon Hiatt {
29188733e3bSDon Hiatt 	u32 bth0;
29288733e3bSDon Hiatt 	struct hfi1_ibport *ibp;
29388733e3bSDon Hiatt 
29488733e3bSDon Hiatt 	ibp = to_iport(qp->ibqp.device, qp->port_num);
29588733e3bSDon Hiatt 	if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
29688733e3bSDon Hiatt 		ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
29788733e3bSDon Hiatt 		bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
29888733e3bSDon Hiatt 	} else {
29988733e3bSDon Hiatt 		bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
30088733e3bSDon Hiatt 	}
30188733e3bSDon Hiatt 
30288733e3bSDon Hiatt 	if (wqe->wr.send_flags & IB_SEND_SOLICITED)
30388733e3bSDon Hiatt 		bth0 |= IB_BTH_SOLICITED;
30488733e3bSDon Hiatt 	bth0 |= extra_bytes << 20;
30588733e3bSDon Hiatt 	if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI)
30688733e3bSDon Hiatt 		*pkey = hfi1_get_pkey(ibp, wqe->ud_wr.pkey_index);
30788733e3bSDon Hiatt 	else
30888733e3bSDon Hiatt 		*pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
30988733e3bSDon Hiatt 	if (!bypass)
31088733e3bSDon Hiatt 		bth0 |= *pkey;
31188733e3bSDon Hiatt 	ohdr->bth[0] = cpu_to_be32(bth0);
31288733e3bSDon Hiatt 	ohdr->bth[1] = cpu_to_be32(wqe->ud_wr.remote_qpn);
31388733e3bSDon Hiatt 	ohdr->bth[2] = cpu_to_be32(mask_psn(wqe->psn));
31488733e3bSDon Hiatt 	/*
31588733e3bSDon Hiatt 	 * Qkeys with the high order bit set mean use the
31688733e3bSDon Hiatt 	 * qkey from the QP context instead of the WR (see 10.2.5).
31788733e3bSDon Hiatt 	 */
31888733e3bSDon Hiatt 	ohdr->u.ud.deth[0] = cpu_to_be32((int)wqe->ud_wr.remote_qkey < 0 ?
31988733e3bSDon Hiatt 					 qp->qkey : wqe->ud_wr.remote_qkey);
32088733e3bSDon Hiatt 	ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
32188733e3bSDon Hiatt }
32288733e3bSDon Hiatt 
32388733e3bSDon Hiatt void hfi1_make_ud_req_9B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
32488733e3bSDon Hiatt 			 struct rvt_swqe *wqe)
32588733e3bSDon Hiatt {
32688733e3bSDon Hiatt 	u32 nwords, extra_bytes;
32788733e3bSDon Hiatt 	u16 len, slid, dlid, pkey;
32888733e3bSDon Hiatt 	u16 lrh0 = 0;
32988733e3bSDon Hiatt 	u8 sc5;
33088733e3bSDon Hiatt 	struct hfi1_qp_priv *priv = qp->priv;
33188733e3bSDon Hiatt 	struct ib_other_headers *ohdr;
33288733e3bSDon Hiatt 	struct rdma_ah_attr *ah_attr;
33388733e3bSDon Hiatt 	struct hfi1_pportdata *ppd;
33488733e3bSDon Hiatt 	struct hfi1_ibport *ibp;
33588733e3bSDon Hiatt 	struct ib_grh *grh;
33688733e3bSDon Hiatt 
33788733e3bSDon Hiatt 	ibp = to_iport(qp->ibqp.device, qp->port_num);
33888733e3bSDon Hiatt 	ppd = ppd_from_ibp(ibp);
33988733e3bSDon Hiatt 	ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
34088733e3bSDon Hiatt 
34188733e3bSDon Hiatt 	extra_bytes = -wqe->length & 3;
34288733e3bSDon Hiatt 	nwords = ((wqe->length + extra_bytes) >> 2) + SIZE_OF_CRC;
34388733e3bSDon Hiatt 	/* header size in dwords LRH+BTH+DETH = (8+12+8)/4. */
3449636258fSMitko Haralanov 	ps->s_txreq->hdr_dwords = 7;
34588733e3bSDon Hiatt 	if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
3469636258fSMitko Haralanov 		ps->s_txreq->hdr_dwords++;
34788733e3bSDon Hiatt 
34888733e3bSDon Hiatt 	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
34988733e3bSDon Hiatt 		grh = &ps->s_txreq->phdr.hdr.ibh.u.l.grh;
3509636258fSMitko Haralanov 		ps->s_txreq->hdr_dwords +=
3519636258fSMitko Haralanov 			hfi1_make_grh(ibp, grh, rdma_ah_read_grh(ah_attr),
35278d3633bSMike Marciniszyn 				      ps->s_txreq->hdr_dwords - LRH_9B_DWORDS,
35378d3633bSMike Marciniszyn 				      nwords);
35488733e3bSDon Hiatt 		lrh0 = HFI1_LRH_GRH;
35588733e3bSDon Hiatt 		ohdr = &ps->s_txreq->phdr.hdr.ibh.u.l.oth;
35688733e3bSDon Hiatt 	} else {
35788733e3bSDon Hiatt 		lrh0 = HFI1_LRH_BTH;
35888733e3bSDon Hiatt 		ohdr = &ps->s_txreq->phdr.hdr.ibh.u.oth;
35988733e3bSDon Hiatt 	}
36088733e3bSDon Hiatt 
36188733e3bSDon Hiatt 	sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
36288733e3bSDon Hiatt 	lrh0 |= (rdma_ah_get_sl(ah_attr) & 0xf) << 4;
36388733e3bSDon Hiatt 	if (qp->ibqp.qp_type == IB_QPT_SMI) {
36488733e3bSDon Hiatt 		lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
36588733e3bSDon Hiatt 		priv->s_sc = 0xf;
36688733e3bSDon Hiatt 	} else {
36788733e3bSDon Hiatt 		lrh0 |= (sc5 & 0xf) << 12;
36888733e3bSDon Hiatt 		priv->s_sc = sc5;
36988733e3bSDon Hiatt 	}
37088733e3bSDon Hiatt 
37188733e3bSDon Hiatt 	dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 9B);
37288733e3bSDon Hiatt 	if (dlid == be16_to_cpu(IB_LID_PERMISSIVE)) {
37388733e3bSDon Hiatt 		slid = be16_to_cpu(IB_LID_PERMISSIVE);
37488733e3bSDon Hiatt 	} else {
37588733e3bSDon Hiatt 		u16 lid = (u16)ppd->lid;
37688733e3bSDon Hiatt 
37788733e3bSDon Hiatt 		if (lid) {
37888733e3bSDon Hiatt 			lid |= rdma_ah_get_path_bits(ah_attr) &
37988733e3bSDon Hiatt 				((1 << ppd->lmc) - 1);
38088733e3bSDon Hiatt 			slid = lid;
38188733e3bSDon Hiatt 		} else {
38288733e3bSDon Hiatt 			slid = be16_to_cpu(IB_LID_PERMISSIVE);
38388733e3bSDon Hiatt 		}
38488733e3bSDon Hiatt 	}
38588733e3bSDon Hiatt 	hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, false);
3869636258fSMitko Haralanov 	len = ps->s_txreq->hdr_dwords + nwords;
38788733e3bSDon Hiatt 
38888733e3bSDon Hiatt 	/* Setup the packet */
38988733e3bSDon Hiatt 	ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_9B;
39088733e3bSDon Hiatt 	hfi1_make_ib_hdr(&ps->s_txreq->phdr.hdr.ibh,
39188733e3bSDon Hiatt 			 lrh0, len, dlid, slid);
39288733e3bSDon Hiatt }
39388733e3bSDon Hiatt 
39488733e3bSDon Hiatt void hfi1_make_ud_req_16B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
39588733e3bSDon Hiatt 			  struct rvt_swqe *wqe)
39688733e3bSDon Hiatt {
39788733e3bSDon Hiatt 	struct hfi1_qp_priv *priv = qp->priv;
39888733e3bSDon Hiatt 	struct ib_other_headers *ohdr;
39988733e3bSDon Hiatt 	struct rdma_ah_attr *ah_attr;
40088733e3bSDon Hiatt 	struct hfi1_pportdata *ppd;
40188733e3bSDon Hiatt 	struct hfi1_ibport *ibp;
40288733e3bSDon Hiatt 	u32 dlid, slid, nwords, extra_bytes;
40381cd3891SDon Hiatt 	u32 dest_qp = wqe->ud_wr.remote_qpn;
40481cd3891SDon Hiatt 	u32 src_qp = qp->ibqp.qp_num;
40588733e3bSDon Hiatt 	u16 len, pkey;
40688733e3bSDon Hiatt 	u8 l4, sc5;
40781cd3891SDon Hiatt 	bool is_mgmt = false;
40888733e3bSDon Hiatt 
40988733e3bSDon Hiatt 	ibp = to_iport(qp->ibqp.device, qp->port_num);
41088733e3bSDon Hiatt 	ppd = ppd_from_ibp(ibp);
41188733e3bSDon Hiatt 	ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
41281cd3891SDon Hiatt 
41381cd3891SDon Hiatt 	/*
41481cd3891SDon Hiatt 	 * Build 16B Management Packet if either the destination
41581cd3891SDon Hiatt 	 * or source queue pair number is 0 or 1.
41681cd3891SDon Hiatt 	 */
41781cd3891SDon Hiatt 	if (dest_qp == 0 || src_qp == 0 || dest_qp == 1 || src_qp == 1) {
41881cd3891SDon Hiatt 		/* header size in dwords 16B LRH+L4_FM = (16+8)/4. */
41981cd3891SDon Hiatt 		ps->s_txreq->hdr_dwords = 6;
42081cd3891SDon Hiatt 		is_mgmt = true;
42181cd3891SDon Hiatt 	} else {
42288733e3bSDon Hiatt 		/* header size in dwords 16B LRH+BTH+DETH = (16+12+8)/4. */
4239636258fSMitko Haralanov 		ps->s_txreq->hdr_dwords = 9;
42488733e3bSDon Hiatt 		if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
4259636258fSMitko Haralanov 			ps->s_txreq->hdr_dwords++;
42681cd3891SDon Hiatt 	}
42788733e3bSDon Hiatt 
42888733e3bSDon Hiatt 	/* SW provides space for CRC and LT for bypass packets. */
4299636258fSMitko Haralanov 	extra_bytes = hfi1_get_16b_padding((ps->s_txreq->hdr_dwords << 2),
43088733e3bSDon Hiatt 					   wqe->length);
43188733e3bSDon Hiatt 	nwords = ((wqe->length + extra_bytes + SIZE_OF_LT) >> 2) + SIZE_OF_CRC;
43288733e3bSDon Hiatt 
43388733e3bSDon Hiatt 	if ((rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) &&
43488733e3bSDon Hiatt 	    hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) {
43588733e3bSDon Hiatt 		struct ib_grh *grh;
43688733e3bSDon Hiatt 		struct ib_global_route *grd = rdma_ah_retrieve_grh(ah_attr);
43788733e3bSDon Hiatt 		/*
43888733e3bSDon Hiatt 		 * Ensure OPA GIDs are transformed to IB gids
43988733e3bSDon Hiatt 		 * before creating the GRH.
44088733e3bSDon Hiatt 		 */
44188733e3bSDon Hiatt 		if (grd->sgid_index == OPA_GID_INDEX) {
44288733e3bSDon Hiatt 			dd_dev_warn(ppd->dd, "Bad sgid_index. sgid_index: %d\n",
44388733e3bSDon Hiatt 				    grd->sgid_index);
44488733e3bSDon Hiatt 			grd->sgid_index = 0;
44588733e3bSDon Hiatt 		}
44688733e3bSDon Hiatt 		grh = &ps->s_txreq->phdr.hdr.opah.u.l.grh;
44778d3633bSMike Marciniszyn 		ps->s_txreq->hdr_dwords += hfi1_make_grh(
44878d3633bSMike Marciniszyn 			ibp, grh, grd,
44978d3633bSMike Marciniszyn 			ps->s_txreq->hdr_dwords - LRH_16B_DWORDS,
45078d3633bSMike Marciniszyn 			nwords);
45188733e3bSDon Hiatt 		ohdr = &ps->s_txreq->phdr.hdr.opah.u.l.oth;
45288733e3bSDon Hiatt 		l4 = OPA_16B_L4_IB_GLOBAL;
45388733e3bSDon Hiatt 	} else {
45488733e3bSDon Hiatt 		ohdr = &ps->s_txreq->phdr.hdr.opah.u.oth;
45588733e3bSDon Hiatt 		l4 = OPA_16B_L4_IB_LOCAL;
45688733e3bSDon Hiatt 	}
45788733e3bSDon Hiatt 
45888733e3bSDon Hiatt 	sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
45988733e3bSDon Hiatt 	if (qp->ibqp.qp_type == IB_QPT_SMI)
46088733e3bSDon Hiatt 		priv->s_sc = 0xf;
46188733e3bSDon Hiatt 	else
46288733e3bSDon Hiatt 		priv->s_sc = sc5;
46388733e3bSDon Hiatt 
46488733e3bSDon Hiatt 	dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 16B);
46588733e3bSDon Hiatt 	if (!ppd->lid)
46688733e3bSDon Hiatt 		slid = be32_to_cpu(OPA_LID_PERMISSIVE);
46788733e3bSDon Hiatt 	else
46888733e3bSDon Hiatt 		slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
46988733e3bSDon Hiatt 			   ((1 << ppd->lmc) - 1));
47088733e3bSDon Hiatt 
47181cd3891SDon Hiatt 	if (is_mgmt) {
47281cd3891SDon Hiatt 		l4 = OPA_16B_L4_FM;
47381cd3891SDon Hiatt 		pkey = hfi1_get_pkey(ibp, wqe->ud_wr.pkey_index);
47481cd3891SDon Hiatt 		hfi1_16B_set_qpn(&ps->s_txreq->phdr.hdr.opah.u.mgmt,
47581cd3891SDon Hiatt 				 dest_qp, src_qp);
47681cd3891SDon Hiatt 	} else {
47788733e3bSDon Hiatt 		hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, true);
47881cd3891SDon Hiatt 	}
47988733e3bSDon Hiatt 	/* Convert dwords to flits */
4809636258fSMitko Haralanov 	len = (ps->s_txreq->hdr_dwords + nwords) >> 1;
48188733e3bSDon Hiatt 
48288733e3bSDon Hiatt 	/* Setup the packet */
48388733e3bSDon Hiatt 	ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_16B;
48488733e3bSDon Hiatt 	hfi1_make_16b_hdr(&ps->s_txreq->phdr.hdr.opah,
48588733e3bSDon Hiatt 			  slid, dlid, len, pkey, 0, 0, l4, priv->s_sc);
48688733e3bSDon Hiatt }
48788733e3bSDon Hiatt 
488f48ad614SDennis Dalessandro /**
489f48ad614SDennis Dalessandro  * hfi1_make_ud_req - construct a UD request packet
490f48ad614SDennis Dalessandro  * @qp: the QP
491f48ad614SDennis Dalessandro  *
492f48ad614SDennis Dalessandro  * Assume s_lock is held.
493f48ad614SDennis Dalessandro  *
494f48ad614SDennis Dalessandro  * Return 1 if constructed; otherwise, return 0.
495f48ad614SDennis Dalessandro  */
496f48ad614SDennis Dalessandro int hfi1_make_ud_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
497f48ad614SDennis Dalessandro {
498f48ad614SDennis Dalessandro 	struct hfi1_qp_priv *priv = qp->priv;
49990898850SDasaratharaman Chandramouli 	struct rdma_ah_attr *ah_attr;
500f48ad614SDennis Dalessandro 	struct hfi1_pportdata *ppd;
501f48ad614SDennis Dalessandro 	struct hfi1_ibport *ibp;
502f48ad614SDennis Dalessandro 	struct rvt_swqe *wqe;
503f48ad614SDennis Dalessandro 	int next_cur;
50488733e3bSDon Hiatt 	u32 lid;
505f48ad614SDennis Dalessandro 
506f48ad614SDennis Dalessandro 	ps->s_txreq = get_txreq(ps->dev, qp);
507b697d7d8SMichael J. Ruhl 	if (!ps->s_txreq)
508f48ad614SDennis Dalessandro 		goto bail_no_tx;
509f48ad614SDennis Dalessandro 
510f48ad614SDennis Dalessandro 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
511f48ad614SDennis Dalessandro 		if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
512f48ad614SDennis Dalessandro 			goto bail;
513f48ad614SDennis Dalessandro 		/* We are in the error state, flush the work request. */
5146aa7de05SMark Rutland 		if (qp->s_last == READ_ONCE(qp->s_head))
515f48ad614SDennis Dalessandro 			goto bail;
516f48ad614SDennis Dalessandro 		/* If DMAs are in progress, we can't flush immediately. */
517f48ad614SDennis Dalessandro 		if (iowait_sdma_pending(&priv->s_iowait)) {
518f48ad614SDennis Dalessandro 			qp->s_flags |= RVT_S_WAIT_DMA;
519f48ad614SDennis Dalessandro 			goto bail;
520f48ad614SDennis Dalessandro 		}
521f48ad614SDennis Dalessandro 		wqe = rvt_get_swqe_ptr(qp, qp->s_last);
522116aa033SVenkata Sandeep Dhanalakota 		rvt_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
523f48ad614SDennis Dalessandro 		goto done_free_tx;
524f48ad614SDennis Dalessandro 	}
525f48ad614SDennis Dalessandro 
526f48ad614SDennis Dalessandro 	/* see post_one_send() */
5276aa7de05SMark Rutland 	if (qp->s_cur == READ_ONCE(qp->s_head))
528f48ad614SDennis Dalessandro 		goto bail;
529f48ad614SDennis Dalessandro 
530f48ad614SDennis Dalessandro 	wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
531f48ad614SDennis Dalessandro 	next_cur = qp->s_cur + 1;
532f48ad614SDennis Dalessandro 	if (next_cur >= qp->s_size)
533f48ad614SDennis Dalessandro 		next_cur = 0;
534f48ad614SDennis Dalessandro 
535f48ad614SDennis Dalessandro 	/* Construct the header. */
536f48ad614SDennis Dalessandro 	ibp = to_iport(qp->ibqp.device, qp->port_num);
537f48ad614SDennis Dalessandro 	ppd = ppd_from_ibp(ibp);
538f48ad614SDennis Dalessandro 	ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
53988733e3bSDon Hiatt 	priv->hdr_type = hfi1_get_hdr_type(ppd->lid, ah_attr);
54088733e3bSDon Hiatt 	if ((!hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) ||
54188733e3bSDon Hiatt 	    (rdma_ah_get_dlid(ah_attr) == be32_to_cpu(OPA_LID_PERMISSIVE))) {
542d8966fcdSDasaratharaman Chandramouli 		lid = rdma_ah_get_dlid(ah_attr) & ~((1 << ppd->lmc) - 1);
543f48ad614SDennis Dalessandro 		if (unlikely(!loopback &&
54488733e3bSDon Hiatt 			     ((lid == ppd->lid) ||
54588733e3bSDon Hiatt 			      ((lid == be32_to_cpu(OPA_LID_PERMISSIVE)) &&
54688733e3bSDon Hiatt 			       (qp->ibqp.qp_type == IB_QPT_GSI))))) {
547f48ad614SDennis Dalessandro 			unsigned long tflags = ps->flags;
548f48ad614SDennis Dalessandro 			/*
549f48ad614SDennis Dalessandro 			 * If DMAs are in progress, we can't generate
550f48ad614SDennis Dalessandro 			 * a completion for the loopback packet since
551f48ad614SDennis Dalessandro 			 * it would be out of order.
552f48ad614SDennis Dalessandro 			 * Instead of waiting, we could queue a
553f48ad614SDennis Dalessandro 			 * zero length descriptor so we get a callback.
554f48ad614SDennis Dalessandro 			 */
555f48ad614SDennis Dalessandro 			if (iowait_sdma_pending(&priv->s_iowait)) {
556f48ad614SDennis Dalessandro 				qp->s_flags |= RVT_S_WAIT_DMA;
557f48ad614SDennis Dalessandro 				goto bail;
558f48ad614SDennis Dalessandro 			}
559f48ad614SDennis Dalessandro 			qp->s_cur = next_cur;
560f48ad614SDennis Dalessandro 			spin_unlock_irqrestore(&qp->s_lock, tflags);
561f48ad614SDennis Dalessandro 			ud_loopback(qp, wqe);
562f48ad614SDennis Dalessandro 			spin_lock_irqsave(&qp->s_lock, tflags);
563f48ad614SDennis Dalessandro 			ps->flags = tflags;
564116aa033SVenkata Sandeep Dhanalakota 			rvt_send_complete(qp, wqe, IB_WC_SUCCESS);
565f48ad614SDennis Dalessandro 			goto done_free_tx;
566f48ad614SDennis Dalessandro 		}
567f48ad614SDennis Dalessandro 	}
568f48ad614SDennis Dalessandro 
569f48ad614SDennis Dalessandro 	qp->s_cur = next_cur;
570e922ae06SDon Hiatt 	ps->s_txreq->s_cur_size = wqe->length;
571b777f154SMitko Haralanov 	ps->s_txreq->ss = &qp->s_sge;
572d8966fcdSDasaratharaman Chandramouli 	qp->s_srate = rdma_ah_get_static_rate(ah_attr);
573f48ad614SDennis Dalessandro 	qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
574f48ad614SDennis Dalessandro 	qp->s_wqe = wqe;
575f48ad614SDennis Dalessandro 	qp->s_sge.sge = wqe->sg_list[0];
576f48ad614SDennis Dalessandro 	qp->s_sge.sg_list = wqe->sg_list + 1;
577f48ad614SDennis Dalessandro 	qp->s_sge.num_sge = wqe->wr.num_sge;
578f48ad614SDennis Dalessandro 	qp->s_sge.total_len = wqe->length;
579f48ad614SDennis Dalessandro 
58088733e3bSDon Hiatt 	/* Make the appropriate header */
58188733e3bSDon Hiatt 	hfi1_make_ud_req_tbl[priv->hdr_type](qp, ps, qp->s_wqe);
582f48ad614SDennis Dalessandro 	priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
583f48ad614SDennis Dalessandro 	ps->s_txreq->sde = priv->s_sde;
584f48ad614SDennis Dalessandro 	priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
585f48ad614SDennis Dalessandro 	ps->s_txreq->psc = priv->s_sendcontext;
586f48ad614SDennis Dalessandro 	/* disarm any ahg */
587a9b6b3bcSDasaratharaman Chandramouli 	priv->s_ahg->ahgcount = 0;
588a9b6b3bcSDasaratharaman Chandramouli 	priv->s_ahg->ahgidx = 0;
589a9b6b3bcSDasaratharaman Chandramouli 	priv->s_ahg->tx_flags = 0;
590f48ad614SDennis Dalessandro 
591f48ad614SDennis Dalessandro 	return 1;
592f48ad614SDennis Dalessandro 
593f48ad614SDennis Dalessandro done_free_tx:
594f48ad614SDennis Dalessandro 	hfi1_put_txreq(ps->s_txreq);
595f48ad614SDennis Dalessandro 	ps->s_txreq = NULL;
596f48ad614SDennis Dalessandro 	return 1;
597f48ad614SDennis Dalessandro 
598f48ad614SDennis Dalessandro bail:
599f48ad614SDennis Dalessandro 	hfi1_put_txreq(ps->s_txreq);
600f48ad614SDennis Dalessandro 
601f48ad614SDennis Dalessandro bail_no_tx:
602f48ad614SDennis Dalessandro 	ps->s_txreq = NULL;
603f48ad614SDennis Dalessandro 	qp->s_flags &= ~RVT_S_BUSY;
604f48ad614SDennis Dalessandro 	return 0;
605f48ad614SDennis Dalessandro }
606f48ad614SDennis Dalessandro 
607f48ad614SDennis Dalessandro /*
608f48ad614SDennis Dalessandro  * Hardware can't check this so we do it here.
609f48ad614SDennis Dalessandro  *
610f48ad614SDennis Dalessandro  * This is a slightly different algorithm than the standard pkey check.  It
611f48ad614SDennis Dalessandro  * special cases the management keys and allows for 0x7fff and 0xffff to be in
612f48ad614SDennis Dalessandro  * the table at the same time.
613f48ad614SDennis Dalessandro  *
614f48ad614SDennis Dalessandro  * @returns the index found or -1 if not found
615f48ad614SDennis Dalessandro  */
616f48ad614SDennis Dalessandro int hfi1_lookup_pkey_idx(struct hfi1_ibport *ibp, u16 pkey)
617f48ad614SDennis Dalessandro {
618f48ad614SDennis Dalessandro 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
619f48ad614SDennis Dalessandro 	unsigned i;
620f48ad614SDennis Dalessandro 
621f48ad614SDennis Dalessandro 	if (pkey == FULL_MGMT_P_KEY || pkey == LIM_MGMT_P_KEY) {
622f48ad614SDennis Dalessandro 		unsigned lim_idx = -1;
623f48ad614SDennis Dalessandro 
624f48ad614SDennis Dalessandro 		for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i) {
625f48ad614SDennis Dalessandro 			/* here we look for an exact match */
626f48ad614SDennis Dalessandro 			if (ppd->pkeys[i] == pkey)
627f48ad614SDennis Dalessandro 				return i;
628f48ad614SDennis Dalessandro 			if (ppd->pkeys[i] == LIM_MGMT_P_KEY)
629f48ad614SDennis Dalessandro 				lim_idx = i;
630f48ad614SDennis Dalessandro 		}
631f48ad614SDennis Dalessandro 
632f48ad614SDennis Dalessandro 		/* did not find 0xffff return 0x7fff idx if found */
633f48ad614SDennis Dalessandro 		if (pkey == FULL_MGMT_P_KEY)
634f48ad614SDennis Dalessandro 			return lim_idx;
635f48ad614SDennis Dalessandro 
636f48ad614SDennis Dalessandro 		/* no match...  */
637f48ad614SDennis Dalessandro 		return -1;
638f48ad614SDennis Dalessandro 	}
639f48ad614SDennis Dalessandro 
640f48ad614SDennis Dalessandro 	pkey &= 0x7fff; /* remove limited/full membership bit */
641f48ad614SDennis Dalessandro 
642f48ad614SDennis Dalessandro 	for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i)
643f48ad614SDennis Dalessandro 		if ((ppd->pkeys[i] & 0x7fff) == pkey)
644f48ad614SDennis Dalessandro 			return i;
645f48ad614SDennis Dalessandro 
646f48ad614SDennis Dalessandro 	/*
647f48ad614SDennis Dalessandro 	 * Should not get here, this means hardware failed to validate pkeys.
648f48ad614SDennis Dalessandro 	 */
649f48ad614SDennis Dalessandro 	return -1;
650f48ad614SDennis Dalessandro }
651f48ad614SDennis Dalessandro 
65288733e3bSDon Hiatt void return_cnp_16B(struct hfi1_ibport *ibp, struct rvt_qp *qp,
653f59fb9e0SMike Marciniszyn 		    u32 remote_qpn, u16 pkey, u32 slid, u32 dlid,
65488733e3bSDon Hiatt 		    u8 sc5, const struct ib_grh *old_grh)
65588733e3bSDon Hiatt {
65688733e3bSDon Hiatt 	u64 pbc, pbc_flags = 0;
65788733e3bSDon Hiatt 	u32 bth0, plen, vl, hwords = 7;
65888733e3bSDon Hiatt 	u16 len;
65988733e3bSDon Hiatt 	u8 l4;
660*fe4dd423SMitko Haralanov 	struct hfi1_opa_header hdr;
66188733e3bSDon Hiatt 	struct ib_other_headers *ohdr;
66288733e3bSDon Hiatt 	struct pio_buf *pbuf;
66388733e3bSDon Hiatt 	struct send_context *ctxt = qp_to_send_context(qp, sc5);
66488733e3bSDon Hiatt 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
66588733e3bSDon Hiatt 	u32 nwords;
66688733e3bSDon Hiatt 
667*fe4dd423SMitko Haralanov 	hdr.hdr_type = HFI1_PKT_TYPE_16B;
66888733e3bSDon Hiatt 	/* Populate length */
66988733e3bSDon Hiatt 	nwords = ((hfi1_get_16b_padding(hwords << 2, 0) +
67088733e3bSDon Hiatt 		   SIZE_OF_LT) >> 2) + SIZE_OF_CRC;
67188733e3bSDon Hiatt 	if (old_grh) {
672*fe4dd423SMitko Haralanov 		struct ib_grh *grh = &hdr.opah.u.l.grh;
67388733e3bSDon Hiatt 
67488733e3bSDon Hiatt 		grh->version_tclass_flow = old_grh->version_tclass_flow;
67578d3633bSMike Marciniszyn 		grh->paylen = cpu_to_be16(
67678d3633bSMike Marciniszyn 			(hwords - LRH_16B_DWORDS + nwords) << 2);
67788733e3bSDon Hiatt 		grh->hop_limit = 0xff;
67888733e3bSDon Hiatt 		grh->sgid = old_grh->dgid;
67988733e3bSDon Hiatt 		grh->dgid = old_grh->sgid;
680*fe4dd423SMitko Haralanov 		ohdr = &hdr.opah.u.l.oth;
68188733e3bSDon Hiatt 		l4 = OPA_16B_L4_IB_GLOBAL;
68288733e3bSDon Hiatt 		hwords += sizeof(struct ib_grh) / sizeof(u32);
68388733e3bSDon Hiatt 	} else {
684*fe4dd423SMitko Haralanov 		ohdr = &hdr.opah.u.oth;
68588733e3bSDon Hiatt 		l4 = OPA_16B_L4_IB_LOCAL;
68688733e3bSDon Hiatt 	}
68788733e3bSDon Hiatt 
68888733e3bSDon Hiatt 	/* BIT 16 to 19 is TVER. Bit 20 to 22 is pad cnt */
68988733e3bSDon Hiatt 	bth0 = (IB_OPCODE_CNP << 24) | (1 << 16) |
69088733e3bSDon Hiatt 	       (hfi1_get_16b_padding(hwords << 2, 0) << 20);
69188733e3bSDon Hiatt 	ohdr->bth[0] = cpu_to_be32(bth0);
69288733e3bSDon Hiatt 
69388733e3bSDon Hiatt 	ohdr->bth[1] = cpu_to_be32(remote_qpn);
69488733e3bSDon Hiatt 	ohdr->bth[2] = 0; /* PSN 0 */
69588733e3bSDon Hiatt 
69688733e3bSDon Hiatt 	/* Convert dwords to flits */
69788733e3bSDon Hiatt 	len = (hwords + nwords) >> 1;
698*fe4dd423SMitko Haralanov 	hfi1_make_16b_hdr(&hdr.opah, slid, dlid, len, pkey, 1, 0, l4, sc5);
69988733e3bSDon Hiatt 
70088733e3bSDon Hiatt 	plen = 2 /* PBC */ + hwords + nwords;
70188733e3bSDon Hiatt 	pbc_flags |= PBC_PACKET_BYPASS | PBC_INSERT_BYPASS_ICRC;
70288733e3bSDon Hiatt 	vl = sc_to_vlt(ppd->dd, sc5);
70388733e3bSDon Hiatt 	pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
70488733e3bSDon Hiatt 	if (ctxt) {
70588733e3bSDon Hiatt 		pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
706*fe4dd423SMitko Haralanov 		if (pbuf) {
707*fe4dd423SMitko Haralanov 			trace_pio_output_ibhdr(ppd->dd, &hdr, sc5);
70888733e3bSDon Hiatt 			ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
70988733e3bSDon Hiatt 						 &hdr, hwords);
71088733e3bSDon Hiatt 		}
71188733e3bSDon Hiatt 	}
712*fe4dd423SMitko Haralanov }
71388733e3bSDon Hiatt 
714f48ad614SDennis Dalessandro void return_cnp(struct hfi1_ibport *ibp, struct rvt_qp *qp, u32 remote_qpn,
715f59fb9e0SMike Marciniszyn 		u16 pkey, u32 slid, u32 dlid, u8 sc5,
716f48ad614SDennis Dalessandro 		const struct ib_grh *old_grh)
717f48ad614SDennis Dalessandro {
718f48ad614SDennis Dalessandro 	u64 pbc, pbc_flags = 0;
719f48ad614SDennis Dalessandro 	u32 bth0, plen, vl, hwords = 5;
720f48ad614SDennis Dalessandro 	u16 lrh0;
721f48ad614SDennis Dalessandro 	u8 sl = ibp->sc_to_sl[sc5];
722*fe4dd423SMitko Haralanov 	struct hfi1_opa_header hdr;
723261a4351SMike Marciniszyn 	struct ib_other_headers *ohdr;
724f48ad614SDennis Dalessandro 	struct pio_buf *pbuf;
725f48ad614SDennis Dalessandro 	struct send_context *ctxt = qp_to_send_context(qp, sc5);
726f48ad614SDennis Dalessandro 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
727f48ad614SDennis Dalessandro 
728*fe4dd423SMitko Haralanov 	hdr.hdr_type = HFI1_PKT_TYPE_9B;
729f48ad614SDennis Dalessandro 	if (old_grh) {
730*fe4dd423SMitko Haralanov 		struct ib_grh *grh = &hdr.ibh.u.l.grh;
731f48ad614SDennis Dalessandro 
732f48ad614SDennis Dalessandro 		grh->version_tclass_flow = old_grh->version_tclass_flow;
73378d3633bSMike Marciniszyn 		grh->paylen = cpu_to_be16(
73478d3633bSMike Marciniszyn 			(hwords - LRH_9B_DWORDS + SIZE_OF_CRC) << 2);
735f48ad614SDennis Dalessandro 		grh->hop_limit = 0xff;
736f48ad614SDennis Dalessandro 		grh->sgid = old_grh->dgid;
737f48ad614SDennis Dalessandro 		grh->dgid = old_grh->sgid;
738*fe4dd423SMitko Haralanov 		ohdr = &hdr.ibh.u.l.oth;
739f48ad614SDennis Dalessandro 		lrh0 = HFI1_LRH_GRH;
740f48ad614SDennis Dalessandro 		hwords += sizeof(struct ib_grh) / sizeof(u32);
741f48ad614SDennis Dalessandro 	} else {
742*fe4dd423SMitko Haralanov 		ohdr = &hdr.ibh.u.oth;
743f48ad614SDennis Dalessandro 		lrh0 = HFI1_LRH_BTH;
744f48ad614SDennis Dalessandro 	}
745f48ad614SDennis Dalessandro 
746f48ad614SDennis Dalessandro 	lrh0 |= (sc5 & 0xf) << 12 | sl << 4;
747f48ad614SDennis Dalessandro 
748f48ad614SDennis Dalessandro 	bth0 = pkey | (IB_OPCODE_CNP << 24);
749f48ad614SDennis Dalessandro 	ohdr->bth[0] = cpu_to_be32(bth0);
750f48ad614SDennis Dalessandro 
7513d591099SDon Hiatt 	ohdr->bth[1] = cpu_to_be32(remote_qpn | (1 << IB_BECN_SHIFT));
752f48ad614SDennis Dalessandro 	ohdr->bth[2] = 0; /* PSN 0 */
753f48ad614SDennis Dalessandro 
754*fe4dd423SMitko Haralanov 	hfi1_make_ib_hdr(&hdr.ibh, lrh0, hwords + SIZE_OF_CRC, dlid, slid);
755f48ad614SDennis Dalessandro 	plen = 2 /* PBC */ + hwords;
7567dafbab3SDon Hiatt 	pbc_flags |= (ib_is_sc5(sc5) << PBC_DC_INFO_SHIFT);
757f48ad614SDennis Dalessandro 	vl = sc_to_vlt(ppd->dd, sc5);
758f48ad614SDennis Dalessandro 	pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
759f48ad614SDennis Dalessandro 	if (ctxt) {
760f48ad614SDennis Dalessandro 		pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
761*fe4dd423SMitko Haralanov 		if (pbuf) {
762*fe4dd423SMitko Haralanov 			trace_pio_output_ibhdr(ppd->dd, &hdr, sc5);
763f48ad614SDennis Dalessandro 			ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
764f48ad614SDennis Dalessandro 						 &hdr, hwords);
765f48ad614SDennis Dalessandro 		}
766f48ad614SDennis Dalessandro 	}
767*fe4dd423SMitko Haralanov }
768f48ad614SDennis Dalessandro 
769f48ad614SDennis Dalessandro /*
770f48ad614SDennis Dalessandro  * opa_smp_check() - Do the regular pkey checking, and the additional
771f3809209SJianxin Xiong  * checks for SMPs specified in OPAv1 rev 1.0, 9/19/2016 update, section
772f3809209SJianxin Xiong  * 9.10.25 ("SMA Packet Checks").
773f48ad614SDennis Dalessandro  *
774f48ad614SDennis Dalessandro  * Note that:
775f48ad614SDennis Dalessandro  *   - Checks are done using the pkey directly from the packet's BTH,
776f48ad614SDennis Dalessandro  *     and specifically _not_ the pkey that we attach to the completion,
777f48ad614SDennis Dalessandro  *     which may be different.
778f48ad614SDennis Dalessandro  *   - These checks are specifically for "non-local" SMPs (i.e., SMPs
779f48ad614SDennis Dalessandro  *     which originated on another node). SMPs which are sent from, and
780f48ad614SDennis Dalessandro  *     destined to this node are checked in opa_local_smp_check().
781f48ad614SDennis Dalessandro  *
782f48ad614SDennis Dalessandro  * At the point where opa_smp_check() is called, we know:
783f48ad614SDennis Dalessandro  *   - destination QP is QP0
784f48ad614SDennis Dalessandro  *
785f48ad614SDennis Dalessandro  * opa_smp_check() returns 0 if all checks succeed, 1 otherwise.
786f48ad614SDennis Dalessandro  */
787f48ad614SDennis Dalessandro static int opa_smp_check(struct hfi1_ibport *ibp, u16 pkey, u8 sc5,
788f48ad614SDennis Dalessandro 			 struct rvt_qp *qp, u16 slid, struct opa_smp *smp)
789f48ad614SDennis Dalessandro {
790f48ad614SDennis Dalessandro 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
791f48ad614SDennis Dalessandro 
792f48ad614SDennis Dalessandro 	/*
793f48ad614SDennis Dalessandro 	 * I don't think it's possible for us to get here with sc != 0xf,
794f48ad614SDennis Dalessandro 	 * but check it to be certain.
795f48ad614SDennis Dalessandro 	 */
796f48ad614SDennis Dalessandro 	if (sc5 != 0xf)
797f48ad614SDennis Dalessandro 		return 1;
798f48ad614SDennis Dalessandro 
799f48ad614SDennis Dalessandro 	if (rcv_pkey_check(ppd, pkey, sc5, slid))
800f48ad614SDennis Dalessandro 		return 1;
801f48ad614SDennis Dalessandro 
802f48ad614SDennis Dalessandro 	/*
803f48ad614SDennis Dalessandro 	 * At this point we know (and so don't need to check again) that
804f48ad614SDennis Dalessandro 	 * the pkey is either LIM_MGMT_P_KEY, or FULL_MGMT_P_KEY
805f48ad614SDennis Dalessandro 	 * (see ingress_pkey_check).
806f48ad614SDennis Dalessandro 	 */
807f48ad614SDennis Dalessandro 	if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE &&
808f48ad614SDennis Dalessandro 	    smp->mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED) {
809f48ad614SDennis Dalessandro 		ingress_pkey_table_fail(ppd, pkey, slid);
810f48ad614SDennis Dalessandro 		return 1;
811f48ad614SDennis Dalessandro 	}
812f48ad614SDennis Dalessandro 
813f48ad614SDennis Dalessandro 	/*
814f48ad614SDennis Dalessandro 	 * SMPs fall into one of four (disjoint) categories:
815f3809209SJianxin Xiong 	 * SMA request, SMA response, SMA trap, or SMA trap repress.
816f3809209SJianxin Xiong 	 * Our response depends, in part, on which type of SMP we're
817f3809209SJianxin Xiong 	 * processing.
818f48ad614SDennis Dalessandro 	 *
819f3809209SJianxin Xiong 	 * If this is an SMA response, skip the check here.
820f3809209SJianxin Xiong 	 *
821f3809209SJianxin Xiong 	 * If this is an SMA request or SMA trap repress:
822f3809209SJianxin Xiong 	 *   - pkey != FULL_MGMT_P_KEY =>
823f48ad614SDennis Dalessandro 	 *       increment port recv constraint errors, drop MAD
824f3809209SJianxin Xiong 	 *
825f3809209SJianxin Xiong 	 * Otherwise:
826f3809209SJianxin Xiong 	 *    - accept if the port is running an SM
827f3809209SJianxin Xiong 	 *    - drop MAD if it's an SMA trap
828f3809209SJianxin Xiong 	 *    - pkey == FULL_MGMT_P_KEY =>
829f3809209SJianxin Xiong 	 *        reply with unsupported method
830f48ad614SDennis Dalessandro 	 *    - pkey != FULL_MGMT_P_KEY =>
831f48ad614SDennis Dalessandro 	 *	  increment port recv constraint errors, drop MAD
832f48ad614SDennis Dalessandro 	 */
833f48ad614SDennis Dalessandro 	switch (smp->method) {
834f3809209SJianxin Xiong 	case IB_MGMT_METHOD_GET_RESP:
835f3809209SJianxin Xiong 	case IB_MGMT_METHOD_REPORT_RESP:
836f3809209SJianxin Xiong 		break;
837f48ad614SDennis Dalessandro 	case IB_MGMT_METHOD_GET:
838f48ad614SDennis Dalessandro 	case IB_MGMT_METHOD_SET:
839f48ad614SDennis Dalessandro 	case IB_MGMT_METHOD_REPORT:
840f48ad614SDennis Dalessandro 	case IB_MGMT_METHOD_TRAP_REPRESS:
841f48ad614SDennis Dalessandro 		if (pkey != FULL_MGMT_P_KEY) {
842f48ad614SDennis Dalessandro 			ingress_pkey_table_fail(ppd, pkey, slid);
843f48ad614SDennis Dalessandro 			return 1;
844f48ad614SDennis Dalessandro 		}
845f48ad614SDennis Dalessandro 		break;
846f3809209SJianxin Xiong 	default:
847f48ad614SDennis Dalessandro 		if (ibp->rvp.port_cap_flags & IB_PORT_SM)
848f48ad614SDennis Dalessandro 			return 0;
849f3809209SJianxin Xiong 		if (smp->method == IB_MGMT_METHOD_TRAP)
850f3809209SJianxin Xiong 			return 1;
851f48ad614SDennis Dalessandro 		if (pkey == FULL_MGMT_P_KEY) {
852f48ad614SDennis Dalessandro 			smp->status |= IB_SMP_UNSUP_METHOD;
853f48ad614SDennis Dalessandro 			return 0;
854f48ad614SDennis Dalessandro 		}
855f48ad614SDennis Dalessandro 		ingress_pkey_table_fail(ppd, pkey, slid);
856f48ad614SDennis Dalessandro 		return 1;
857f48ad614SDennis Dalessandro 	}
858f48ad614SDennis Dalessandro 	return 0;
859f48ad614SDennis Dalessandro }
860f48ad614SDennis Dalessandro 
861f48ad614SDennis Dalessandro /**
862f48ad614SDennis Dalessandro  * hfi1_ud_rcv - receive an incoming UD packet
863f48ad614SDennis Dalessandro  * @ibp: the port the packet came in on
864f48ad614SDennis Dalessandro  * @hdr: the packet header
865f48ad614SDennis Dalessandro  * @rcv_flags: flags relevant to rcv processing
866f48ad614SDennis Dalessandro  * @data: the packet data
867f48ad614SDennis Dalessandro  * @tlen: the packet length
868f48ad614SDennis Dalessandro  * @qp: the QP the packet came on
869f48ad614SDennis Dalessandro  *
870f48ad614SDennis Dalessandro  * This is called from qp_rcv() to process an incoming UD packet
871f48ad614SDennis Dalessandro  * for the given QP.
872f48ad614SDennis Dalessandro  * Called at interrupt level.
873f48ad614SDennis Dalessandro  */
874f48ad614SDennis Dalessandro void hfi1_ud_rcv(struct hfi1_packet *packet)
875f48ad614SDennis Dalessandro {
876f48ad614SDennis Dalessandro 	u32 hdrsize = packet->hlen;
877f48ad614SDennis Dalessandro 	struct ib_wc wc;
878f48ad614SDennis Dalessandro 	u32 src_qp;
8799039746cSDon Hiatt 	u16 pkey;
880f48ad614SDennis Dalessandro 	int mgmt_pkey_idx = -1;
881f3e862cbSSebastian Sanchez 	struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
88289c057caSDasaratharaman Chandramouli 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
88372c07e2bSDon Hiatt 	void *data = packet->payload;
884f48ad614SDennis Dalessandro 	u32 tlen = packet->tlen;
885f48ad614SDennis Dalessandro 	struct rvt_qp *qp = packet->qp;
88688733e3bSDon Hiatt 	u8 sc5 = packet->sc;
8879039746cSDon Hiatt 	u8 sl_from_sc;
8889039746cSDon Hiatt 	u8 opcode = packet->opcode;
8899039746cSDon Hiatt 	u8 sl = packet->sl;
8909039746cSDon Hiatt 	u32 dlid = packet->dlid;
8919039746cSDon Hiatt 	u32 slid = packet->slid;
89288733e3bSDon Hiatt 	u8 extra_bytes;
89381cd3891SDon Hiatt 	u8 l4 = 0;
89488733e3bSDon Hiatt 	bool dlid_is_permissive;
89588733e3bSDon Hiatt 	bool slid_is_permissive;
89681cd3891SDon Hiatt 	bool solicited = false;
897f48ad614SDennis Dalessandro 
89888733e3bSDon Hiatt 	extra_bytes = packet->pad + packet->extra_byte + (SIZE_OF_CRC << 2);
89988733e3bSDon Hiatt 
90088733e3bSDon Hiatt 	if (packet->etype == RHF_RCV_TYPE_BYPASS) {
90188733e3bSDon Hiatt 		u32 permissive_lid =
90288733e3bSDon Hiatt 			opa_get_lid(be32_to_cpu(OPA_LID_PERMISSIVE), 16B);
90388733e3bSDon Hiatt 
90481cd3891SDon Hiatt 		l4 = hfi1_16B_get_l4(packet->hdr);
90588733e3bSDon Hiatt 		pkey = hfi1_16B_get_pkey(packet->hdr);
90688733e3bSDon Hiatt 		dlid_is_permissive = (dlid == permissive_lid);
90788733e3bSDon Hiatt 		slid_is_permissive = (slid == permissive_lid);
90888733e3bSDon Hiatt 	} else {
90981cd3891SDon Hiatt 		pkey = ib_bth_get_pkey(packet->ohdr);
91088733e3bSDon Hiatt 		dlid_is_permissive = (dlid == be16_to_cpu(IB_LID_PERMISSIVE));
91188733e3bSDon Hiatt 		slid_is_permissive = (slid == be16_to_cpu(IB_LID_PERMISSIVE));
91288733e3bSDon Hiatt 	}
91389c057caSDasaratharaman Chandramouli 	sl_from_sc = ibp->sc_to_sl[sc5];
914f48ad614SDennis Dalessandro 
91581cd3891SDon Hiatt 	if (likely(l4 != OPA_16B_L4_FM)) {
91681cd3891SDon Hiatt 		src_qp = ib_get_sqpn(packet->ohdr);
91781cd3891SDon Hiatt 		solicited = ib_bth_is_solicited(packet->ohdr);
91881cd3891SDon Hiatt 	} else {
91981cd3891SDon Hiatt 		src_qp = hfi1_16B_get_src_qpn(packet->mgmt);
92081cd3891SDon Hiatt 	}
92181cd3891SDon Hiatt 
922*fe4dd423SMitko Haralanov 	process_ecn(qp, packet);
923f48ad614SDennis Dalessandro 	/*
924f48ad614SDennis Dalessandro 	 * Get the number of bytes the message was padded by
925f48ad614SDennis Dalessandro 	 * and drop incomplete packets.
926f48ad614SDennis Dalessandro 	 */
92789c057caSDasaratharaman Chandramouli 	if (unlikely(tlen < (hdrsize + extra_bytes)))
928f48ad614SDennis Dalessandro 		goto drop;
929f48ad614SDennis Dalessandro 
93089c057caSDasaratharaman Chandramouli 	tlen -= hdrsize + extra_bytes;
931f48ad614SDennis Dalessandro 
932f48ad614SDennis Dalessandro 	/*
933f48ad614SDennis Dalessandro 	 * Check that the permissive LID is only used on QP0
934f48ad614SDennis Dalessandro 	 * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
935f48ad614SDennis Dalessandro 	 */
936f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_num) {
93788733e3bSDon Hiatt 		if (unlikely(dlid_is_permissive || slid_is_permissive))
938f48ad614SDennis Dalessandro 			goto drop;
939f48ad614SDennis Dalessandro 		if (qp->ibqp.qp_num > 1) {
940f48ad614SDennis Dalessandro 			if (unlikely(rcv_pkey_check(ppd, pkey, sc5, slid))) {
941f48ad614SDennis Dalessandro 				/*
942f48ad614SDennis Dalessandro 				 * Traps will not be sent for packets dropped
943f48ad614SDennis Dalessandro 				 * by the HW. This is fine, as sending trap
944f48ad614SDennis Dalessandro 				 * for invalid pkeys is optional according to
945f48ad614SDennis Dalessandro 				 * IB spec (release 1.3, section 10.9.4)
946f48ad614SDennis Dalessandro 				 */
94713d84914SDennis Dalessandro 				hfi1_bad_pkey(ibp,
94889c057caSDasaratharaman Chandramouli 					      pkey, sl,
949f48ad614SDennis Dalessandro 					      src_qp, qp->ibqp.qp_num,
95089c057caSDasaratharaman Chandramouli 					      slid, dlid);
951f48ad614SDennis Dalessandro 				return;
952f48ad614SDennis Dalessandro 			}
953f48ad614SDennis Dalessandro 		} else {
954f48ad614SDennis Dalessandro 			/* GSI packet */
955f48ad614SDennis Dalessandro 			mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
956f48ad614SDennis Dalessandro 			if (mgmt_pkey_idx < 0)
957f48ad614SDennis Dalessandro 				goto drop;
958f48ad614SDennis Dalessandro 		}
95981cd3891SDon Hiatt 		if (unlikely(l4 != OPA_16B_L4_FM &&
96081cd3891SDon Hiatt 			     ib_get_qkey(packet->ohdr) != qp->qkey))
96181cd3891SDon Hiatt 			return; /* Silent drop */
96213d84914SDennis Dalessandro 
963f48ad614SDennis Dalessandro 		/* Drop invalid MAD packets (see 13.5.3.1). */
964f48ad614SDennis Dalessandro 		if (unlikely(qp->ibqp.qp_num == 1 &&
96589c057caSDasaratharaman Chandramouli 			     (tlen > 2048 || (sc5 == 0xF))))
966f48ad614SDennis Dalessandro 			goto drop;
967f48ad614SDennis Dalessandro 	} else {
968f48ad614SDennis Dalessandro 		/* Received on QP0, and so by definition, this is an SMP */
969f48ad614SDennis Dalessandro 		struct opa_smp *smp = (struct opa_smp *)data;
970f48ad614SDennis Dalessandro 
971f48ad614SDennis Dalessandro 		if (opa_smp_check(ibp, pkey, sc5, qp, slid, smp))
972f48ad614SDennis Dalessandro 			goto drop;
973f48ad614SDennis Dalessandro 
974f48ad614SDennis Dalessandro 		if (tlen > 2048)
975f48ad614SDennis Dalessandro 			goto drop;
97688733e3bSDon Hiatt 		if ((dlid_is_permissive || slid_is_permissive) &&
977f48ad614SDennis Dalessandro 		    smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
978f48ad614SDennis Dalessandro 			goto drop;
979f48ad614SDennis Dalessandro 
980f48ad614SDennis Dalessandro 		/* look up SMI pkey */
981f48ad614SDennis Dalessandro 		mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
982f48ad614SDennis Dalessandro 		if (mgmt_pkey_idx < 0)
983f48ad614SDennis Dalessandro 			goto drop;
984f48ad614SDennis Dalessandro 	}
985f48ad614SDennis Dalessandro 
986f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_num > 1 &&
987f48ad614SDennis Dalessandro 	    opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
98881cd3891SDon Hiatt 		wc.ex.imm_data = packet->ohdr->u.ud.imm_data;
989f48ad614SDennis Dalessandro 		wc.wc_flags = IB_WC_WITH_IMM;
990f48ad614SDennis Dalessandro 		tlen -= sizeof(u32);
991f48ad614SDennis Dalessandro 	} else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
992f48ad614SDennis Dalessandro 		wc.ex.imm_data = 0;
993f48ad614SDennis Dalessandro 		wc.wc_flags = 0;
994f48ad614SDennis Dalessandro 	} else {
995f48ad614SDennis Dalessandro 		goto drop;
996f48ad614SDennis Dalessandro 	}
997f48ad614SDennis Dalessandro 
998f48ad614SDennis Dalessandro 	/*
999f48ad614SDennis Dalessandro 	 * A GRH is expected to precede the data even if not
1000f48ad614SDennis Dalessandro 	 * present on the wire.
1001f48ad614SDennis Dalessandro 	 */
1002f48ad614SDennis Dalessandro 	wc.byte_len = tlen + sizeof(struct ib_grh);
1003f48ad614SDennis Dalessandro 
1004f48ad614SDennis Dalessandro 	/*
1005f48ad614SDennis Dalessandro 	 * Get the next work request entry to find where to put the data.
1006f48ad614SDennis Dalessandro 	 */
1007f48ad614SDennis Dalessandro 	if (qp->r_flags & RVT_R_REUSE_SGE) {
1008f48ad614SDennis Dalessandro 		qp->r_flags &= ~RVT_R_REUSE_SGE;
1009f48ad614SDennis Dalessandro 	} else {
1010f48ad614SDennis Dalessandro 		int ret;
1011f48ad614SDennis Dalessandro 
1012832369faSBrian Welty 		ret = rvt_get_rwqe(qp, false);
1013f48ad614SDennis Dalessandro 		if (ret < 0) {
1014beb5a042SBrian Welty 			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
1015f48ad614SDennis Dalessandro 			return;
1016f48ad614SDennis Dalessandro 		}
1017f48ad614SDennis Dalessandro 		if (!ret) {
1018f48ad614SDennis Dalessandro 			if (qp->ibqp.qp_num == 0)
1019f48ad614SDennis Dalessandro 				ibp->rvp.n_vl15_dropped++;
1020f48ad614SDennis Dalessandro 			return;
1021f48ad614SDennis Dalessandro 		}
1022f48ad614SDennis Dalessandro 	}
1023f48ad614SDennis Dalessandro 	/* Silently drop packets which are too big. */
1024f48ad614SDennis Dalessandro 	if (unlikely(wc.byte_len > qp->r_len)) {
1025f48ad614SDennis Dalessandro 		qp->r_flags |= RVT_R_REUSE_SGE;
1026f48ad614SDennis Dalessandro 		goto drop;
1027f48ad614SDennis Dalessandro 	}
10289039746cSDon Hiatt 	if (packet->grh) {
1029019f118bSBrian Welty 		rvt_copy_sge(qp, &qp->r_sge, packet->grh,
103088733e3bSDon Hiatt 			     sizeof(struct ib_grh), true, false);
103188733e3bSDon Hiatt 		wc.wc_flags |= IB_WC_GRH;
103288733e3bSDon Hiatt 	} else if (packet->etype == RHF_RCV_TYPE_BYPASS) {
103388733e3bSDon Hiatt 		struct ib_grh grh;
103488733e3bSDon Hiatt 		/*
103588733e3bSDon Hiatt 		 * Assuming we only created 16B on the send side
103688733e3bSDon Hiatt 		 * if we want to use large LIDs, since GRH was stripped
103788733e3bSDon Hiatt 		 * out when creating 16B, add back the GRH here.
103888733e3bSDon Hiatt 		 */
103988733e3bSDon Hiatt 		hfi1_make_ext_grh(packet, &grh, slid, dlid);
1040019f118bSBrian Welty 		rvt_copy_sge(qp, &qp->r_sge, &grh,
10410128fceaSBrian Welty 			     sizeof(struct ib_grh), true, false);
1042f48ad614SDennis Dalessandro 		wc.wc_flags |= IB_WC_GRH;
1043f48ad614SDennis Dalessandro 	} else {
10441198fceaSBrian Welty 		rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
1045f48ad614SDennis Dalessandro 	}
1046019f118bSBrian Welty 	rvt_copy_sge(qp, &qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh),
10470128fceaSBrian Welty 		     true, false);
1048f48ad614SDennis Dalessandro 	rvt_put_ss(&qp->r_sge);
1049f48ad614SDennis Dalessandro 	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
1050f48ad614SDennis Dalessandro 		return;
1051f48ad614SDennis Dalessandro 	wc.wr_id = qp->r_wr_id;
1052f48ad614SDennis Dalessandro 	wc.status = IB_WC_SUCCESS;
1053f48ad614SDennis Dalessandro 	wc.opcode = IB_WC_RECV;
1054f48ad614SDennis Dalessandro 	wc.vendor_err = 0;
1055f48ad614SDennis Dalessandro 	wc.qp = &qp->ibqp;
1056f48ad614SDennis Dalessandro 	wc.src_qp = src_qp;
1057f48ad614SDennis Dalessandro 
1058f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_type == IB_QPT_GSI ||
1059f48ad614SDennis Dalessandro 	    qp->ibqp.qp_type == IB_QPT_SMI) {
1060f48ad614SDennis Dalessandro 		if (mgmt_pkey_idx < 0) {
1061f48ad614SDennis Dalessandro 			if (net_ratelimit()) {
1062f48ad614SDennis Dalessandro 				struct hfi1_devdata *dd = ppd->dd;
1063f48ad614SDennis Dalessandro 
1064f48ad614SDennis Dalessandro 				dd_dev_err(dd, "QP type %d mgmt_pkey_idx < 0 and packet not dropped???\n",
1065f48ad614SDennis Dalessandro 					   qp->ibqp.qp_type);
1066f48ad614SDennis Dalessandro 				mgmt_pkey_idx = 0;
1067f48ad614SDennis Dalessandro 			}
1068f48ad614SDennis Dalessandro 		}
1069f48ad614SDennis Dalessandro 		wc.pkey_index = (unsigned)mgmt_pkey_idx;
1070f48ad614SDennis Dalessandro 	} else {
1071f48ad614SDennis Dalessandro 		wc.pkey_index = 0;
1072f48ad614SDennis Dalessandro 	}
107388733e3bSDon Hiatt 	if (slid_is_permissive)
107488733e3bSDon Hiatt 		slid = be32_to_cpu(OPA_LID_PERMISSIVE);
1075b64581adSDon Hiatt 	wc.slid = slid & U16_MAX;
107689c057caSDasaratharaman Chandramouli 	wc.sl = sl_from_sc;
1077f48ad614SDennis Dalessandro 
1078f48ad614SDennis Dalessandro 	/*
1079f48ad614SDennis Dalessandro 	 * Save the LMC lower bits if the destination LID is a unicast LID.
1080f48ad614SDennis Dalessandro 	 */
108188733e3bSDon Hiatt 	wc.dlid_path_bits = hfi1_check_mcast(dlid) ? 0 :
1082f48ad614SDennis Dalessandro 		dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
1083f48ad614SDennis Dalessandro 	wc.port_num = qp->port_num;
1084f48ad614SDennis Dalessandro 	/* Signal completion event if the solicited bit is set. */
108581cd3891SDon Hiatt 	rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, solicited);
1086f48ad614SDennis Dalessandro 	return;
1087f48ad614SDennis Dalessandro 
1088f48ad614SDennis Dalessandro drop:
1089f48ad614SDennis Dalessandro 	ibp->rvp.n_pkt_drops++;
1090f48ad614SDennis Dalessandro }
1091