xref: /linux/drivers/infiniband/hw/hfi1/ud.c (revision beb5a0426794c9698c4e0349c626d819b5f3b2c7)
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 
56f48ad614SDennis Dalessandro /**
57f48ad614SDennis Dalessandro  * ud_loopback - handle send on loopback QPs
58f48ad614SDennis Dalessandro  * @sqp: the sending QP
59f48ad614SDennis Dalessandro  * @swqe: the send work request
60f48ad614SDennis Dalessandro  *
61f48ad614SDennis Dalessandro  * This is called from hfi1_make_ud_req() to forward a WQE addressed
62f48ad614SDennis Dalessandro  * to the same HFI.
63f48ad614SDennis Dalessandro  * Note that the receive interrupt handler may be calling hfi1_ud_rcv()
64f48ad614SDennis Dalessandro  * while this is being called.
65f48ad614SDennis Dalessandro  */
66f48ad614SDennis Dalessandro static void ud_loopback(struct rvt_qp *sqp, struct rvt_swqe *swqe)
67f48ad614SDennis Dalessandro {
68f48ad614SDennis Dalessandro 	struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
69f48ad614SDennis Dalessandro 	struct hfi1_pportdata *ppd;
70f48ad614SDennis Dalessandro 	struct rvt_qp *qp;
71f48ad614SDennis Dalessandro 	struct ib_ah_attr *ah_attr;
72f48ad614SDennis Dalessandro 	unsigned long flags;
73f48ad614SDennis Dalessandro 	struct rvt_sge_state ssge;
74f48ad614SDennis Dalessandro 	struct rvt_sge *sge;
75f48ad614SDennis Dalessandro 	struct ib_wc wc;
76f48ad614SDennis Dalessandro 	u32 length;
77f48ad614SDennis Dalessandro 	enum ib_qp_type sqptype, dqptype;
78f48ad614SDennis Dalessandro 
79f48ad614SDennis Dalessandro 	rcu_read_lock();
80f48ad614SDennis Dalessandro 
81f48ad614SDennis Dalessandro 	qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
82f48ad614SDennis Dalessandro 			    swqe->ud_wr.remote_qpn);
83f48ad614SDennis Dalessandro 	if (!qp) {
84f48ad614SDennis Dalessandro 		ibp->rvp.n_pkt_drops++;
85f48ad614SDennis Dalessandro 		rcu_read_unlock();
86f48ad614SDennis Dalessandro 		return;
87f48ad614SDennis Dalessandro 	}
88f48ad614SDennis Dalessandro 
89f48ad614SDennis Dalessandro 	sqptype = sqp->ibqp.qp_type == IB_QPT_GSI ?
90f48ad614SDennis Dalessandro 			IB_QPT_UD : sqp->ibqp.qp_type;
91f48ad614SDennis Dalessandro 	dqptype = qp->ibqp.qp_type == IB_QPT_GSI ?
92f48ad614SDennis Dalessandro 			IB_QPT_UD : qp->ibqp.qp_type;
93f48ad614SDennis Dalessandro 
94f48ad614SDennis Dalessandro 	if (dqptype != sqptype ||
95f48ad614SDennis Dalessandro 	    !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
96f48ad614SDennis Dalessandro 		ibp->rvp.n_pkt_drops++;
97f48ad614SDennis Dalessandro 		goto drop;
98f48ad614SDennis Dalessandro 	}
99f48ad614SDennis Dalessandro 
100f48ad614SDennis Dalessandro 	ah_attr = &ibah_to_rvtah(swqe->ud_wr.ah)->attr;
101f48ad614SDennis Dalessandro 	ppd = ppd_from_ibp(ibp);
102f48ad614SDennis Dalessandro 
103f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_num > 1) {
104f48ad614SDennis Dalessandro 		u16 pkey;
105f48ad614SDennis Dalessandro 		u16 slid;
106f48ad614SDennis Dalessandro 		u8 sc5 = ibp->sl_to_sc[ah_attr->sl];
107f48ad614SDennis Dalessandro 
108f48ad614SDennis Dalessandro 		pkey = hfi1_get_pkey(ibp, sqp->s_pkey_index);
109f48ad614SDennis Dalessandro 		slid = ppd->lid | (ah_attr->src_path_bits &
110f48ad614SDennis Dalessandro 				   ((1 << ppd->lmc) - 1));
111f48ad614SDennis Dalessandro 		if (unlikely(ingress_pkey_check(ppd, pkey, sc5,
112f48ad614SDennis Dalessandro 						qp->s_pkey_index, slid))) {
113f48ad614SDennis Dalessandro 			hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY, pkey,
114f48ad614SDennis Dalessandro 				       ah_attr->sl,
115f48ad614SDennis Dalessandro 				       sqp->ibqp.qp_num, qp->ibqp.qp_num,
116f48ad614SDennis Dalessandro 				       slid, ah_attr->dlid);
117f48ad614SDennis Dalessandro 			goto drop;
118f48ad614SDennis Dalessandro 		}
119f48ad614SDennis Dalessandro 	}
120f48ad614SDennis Dalessandro 
121f48ad614SDennis Dalessandro 	/*
122f48ad614SDennis Dalessandro 	 * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
123f48ad614SDennis Dalessandro 	 * Qkeys with the high order bit set mean use the
124f48ad614SDennis Dalessandro 	 * qkey from the QP context instead of the WR (see 10.2.5).
125f48ad614SDennis Dalessandro 	 */
126f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_num) {
127f48ad614SDennis Dalessandro 		u32 qkey;
128f48ad614SDennis Dalessandro 
129f48ad614SDennis Dalessandro 		qkey = (int)swqe->ud_wr.remote_qkey < 0 ?
130f48ad614SDennis Dalessandro 			sqp->qkey : swqe->ud_wr.remote_qkey;
131f48ad614SDennis Dalessandro 		if (unlikely(qkey != qp->qkey)) {
132f48ad614SDennis Dalessandro 			u16 lid;
133f48ad614SDennis Dalessandro 
134f48ad614SDennis Dalessandro 			lid = ppd->lid | (ah_attr->src_path_bits &
135f48ad614SDennis Dalessandro 					  ((1 << ppd->lmc) - 1));
136f48ad614SDennis Dalessandro 			hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_Q_KEY, qkey,
137f48ad614SDennis Dalessandro 				       ah_attr->sl,
138f48ad614SDennis Dalessandro 				       sqp->ibqp.qp_num, qp->ibqp.qp_num,
139f48ad614SDennis Dalessandro 				       lid,
140f48ad614SDennis Dalessandro 				       ah_attr->dlid);
141f48ad614SDennis Dalessandro 			goto drop;
142f48ad614SDennis Dalessandro 		}
143f48ad614SDennis Dalessandro 	}
144f48ad614SDennis Dalessandro 
145f48ad614SDennis Dalessandro 	/*
146f48ad614SDennis Dalessandro 	 * A GRH is expected to precede the data even if not
147f48ad614SDennis Dalessandro 	 * present on the wire.
148f48ad614SDennis Dalessandro 	 */
149f48ad614SDennis Dalessandro 	length = swqe->length;
150f48ad614SDennis Dalessandro 	memset(&wc, 0, sizeof(wc));
151f48ad614SDennis Dalessandro 	wc.byte_len = length + sizeof(struct ib_grh);
152f48ad614SDennis Dalessandro 
153f48ad614SDennis Dalessandro 	if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
154f48ad614SDennis Dalessandro 		wc.wc_flags = IB_WC_WITH_IMM;
155f48ad614SDennis Dalessandro 		wc.ex.imm_data = swqe->wr.ex.imm_data;
156f48ad614SDennis Dalessandro 	}
157f48ad614SDennis Dalessandro 
158f48ad614SDennis Dalessandro 	spin_lock_irqsave(&qp->r_lock, flags);
159f48ad614SDennis Dalessandro 
160f48ad614SDennis Dalessandro 	/*
161f48ad614SDennis Dalessandro 	 * Get the next work request entry to find where to put the data.
162f48ad614SDennis Dalessandro 	 */
163f48ad614SDennis Dalessandro 	if (qp->r_flags & RVT_R_REUSE_SGE) {
164f48ad614SDennis Dalessandro 		qp->r_flags &= ~RVT_R_REUSE_SGE;
165f48ad614SDennis Dalessandro 	} else {
166f48ad614SDennis Dalessandro 		int ret;
167f48ad614SDennis Dalessandro 
168f48ad614SDennis Dalessandro 		ret = hfi1_rvt_get_rwqe(qp, 0);
169f48ad614SDennis Dalessandro 		if (ret < 0) {
170*beb5a042SBrian Welty 			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
171f48ad614SDennis Dalessandro 			goto bail_unlock;
172f48ad614SDennis Dalessandro 		}
173f48ad614SDennis Dalessandro 		if (!ret) {
174f48ad614SDennis Dalessandro 			if (qp->ibqp.qp_num == 0)
175f48ad614SDennis Dalessandro 				ibp->rvp.n_vl15_dropped++;
176f48ad614SDennis Dalessandro 			goto bail_unlock;
177f48ad614SDennis Dalessandro 		}
178f48ad614SDennis Dalessandro 	}
179f48ad614SDennis Dalessandro 	/* Silently drop packets which are too big. */
180f48ad614SDennis Dalessandro 	if (unlikely(wc.byte_len > qp->r_len)) {
181f48ad614SDennis Dalessandro 		qp->r_flags |= RVT_R_REUSE_SGE;
182f48ad614SDennis Dalessandro 		ibp->rvp.n_pkt_drops++;
183f48ad614SDennis Dalessandro 		goto bail_unlock;
184f48ad614SDennis Dalessandro 	}
185f48ad614SDennis Dalessandro 
186f48ad614SDennis Dalessandro 	if (ah_attr->ah_flags & IB_AH_GRH) {
187527dbf12SDasaratharaman Chandramouli 		struct ib_grh grh;
188527dbf12SDasaratharaman Chandramouli 		struct ib_global_route grd = ah_attr->grh;
189527dbf12SDasaratharaman Chandramouli 
190527dbf12SDasaratharaman Chandramouli 		hfi1_make_grh(ibp, &grh, &grd, 0, 0);
191527dbf12SDasaratharaman Chandramouli 		hfi1_copy_sge(&qp->r_sge, &grh,
192527dbf12SDasaratharaman Chandramouli 			      sizeof(grh), 1, 0);
193f48ad614SDennis Dalessandro 		wc.wc_flags |= IB_WC_GRH;
194f48ad614SDennis Dalessandro 	} else {
195f48ad614SDennis Dalessandro 		hfi1_skip_sge(&qp->r_sge, sizeof(struct ib_grh), 1);
196f48ad614SDennis Dalessandro 	}
197f48ad614SDennis Dalessandro 	ssge.sg_list = swqe->sg_list + 1;
198f48ad614SDennis Dalessandro 	ssge.sge = *swqe->sg_list;
199f48ad614SDennis Dalessandro 	ssge.num_sge = swqe->wr.num_sge;
200f48ad614SDennis Dalessandro 	sge = &ssge.sge;
201f48ad614SDennis Dalessandro 	while (length) {
202f48ad614SDennis Dalessandro 		u32 len = sge->length;
203f48ad614SDennis Dalessandro 
204f48ad614SDennis Dalessandro 		if (len > length)
205f48ad614SDennis Dalessandro 			len = length;
206f48ad614SDennis Dalessandro 		if (len > sge->sge_length)
207f48ad614SDennis Dalessandro 			len = sge->sge_length;
208f48ad614SDennis Dalessandro 		WARN_ON_ONCE(len == 0);
209f48ad614SDennis Dalessandro 		hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, 1, 0);
210f48ad614SDennis Dalessandro 		sge->vaddr += len;
211f48ad614SDennis Dalessandro 		sge->length -= len;
212f48ad614SDennis Dalessandro 		sge->sge_length -= len;
213f48ad614SDennis Dalessandro 		if (sge->sge_length == 0) {
214f48ad614SDennis Dalessandro 			if (--ssge.num_sge)
215f48ad614SDennis Dalessandro 				*sge = *ssge.sg_list++;
216f48ad614SDennis Dalessandro 		} else if (sge->length == 0 && sge->mr->lkey) {
217f48ad614SDennis Dalessandro 			if (++sge->n >= RVT_SEGSZ) {
218f48ad614SDennis Dalessandro 				if (++sge->m >= sge->mr->mapsz)
219f48ad614SDennis Dalessandro 					break;
220f48ad614SDennis Dalessandro 				sge->n = 0;
221f48ad614SDennis Dalessandro 			}
222f48ad614SDennis Dalessandro 			sge->vaddr =
223f48ad614SDennis Dalessandro 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
224f48ad614SDennis Dalessandro 			sge->length =
225f48ad614SDennis Dalessandro 				sge->mr->map[sge->m]->segs[sge->n].length;
226f48ad614SDennis Dalessandro 		}
227f48ad614SDennis Dalessandro 		length -= len;
228f48ad614SDennis Dalessandro 	}
229f48ad614SDennis Dalessandro 	rvt_put_ss(&qp->r_sge);
230f48ad614SDennis Dalessandro 	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
231f48ad614SDennis Dalessandro 		goto bail_unlock;
232f48ad614SDennis Dalessandro 	wc.wr_id = qp->r_wr_id;
233f48ad614SDennis Dalessandro 	wc.status = IB_WC_SUCCESS;
234f48ad614SDennis Dalessandro 	wc.opcode = IB_WC_RECV;
235f48ad614SDennis Dalessandro 	wc.qp = &qp->ibqp;
236f48ad614SDennis Dalessandro 	wc.src_qp = sqp->ibqp.qp_num;
237f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI) {
238f48ad614SDennis Dalessandro 		if (sqp->ibqp.qp_type == IB_QPT_GSI ||
239f48ad614SDennis Dalessandro 		    sqp->ibqp.qp_type == IB_QPT_SMI)
240f48ad614SDennis Dalessandro 			wc.pkey_index = swqe->ud_wr.pkey_index;
241f48ad614SDennis Dalessandro 		else
242f48ad614SDennis Dalessandro 			wc.pkey_index = sqp->s_pkey_index;
243f48ad614SDennis Dalessandro 	} else {
244f48ad614SDennis Dalessandro 		wc.pkey_index = 0;
245f48ad614SDennis Dalessandro 	}
246f48ad614SDennis Dalessandro 	wc.slid = ppd->lid | (ah_attr->src_path_bits & ((1 << ppd->lmc) - 1));
247f48ad614SDennis Dalessandro 	/* Check for loopback when the port lid is not set */
248f48ad614SDennis Dalessandro 	if (wc.slid == 0 && sqp->ibqp.qp_type == IB_QPT_GSI)
249f48ad614SDennis Dalessandro 		wc.slid = be16_to_cpu(IB_LID_PERMISSIVE);
250f48ad614SDennis Dalessandro 	wc.sl = ah_attr->sl;
251f48ad614SDennis Dalessandro 	wc.dlid_path_bits = ah_attr->dlid & ((1 << ppd->lmc) - 1);
252f48ad614SDennis Dalessandro 	wc.port_num = qp->port_num;
253f48ad614SDennis Dalessandro 	/* Signal completion event if the solicited bit is set. */
254f48ad614SDennis Dalessandro 	rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
255f48ad614SDennis Dalessandro 		     swqe->wr.send_flags & IB_SEND_SOLICITED);
256f48ad614SDennis Dalessandro 	ibp->rvp.n_loop_pkts++;
257f48ad614SDennis Dalessandro bail_unlock:
258f48ad614SDennis Dalessandro 	spin_unlock_irqrestore(&qp->r_lock, flags);
259f48ad614SDennis Dalessandro drop:
260f48ad614SDennis Dalessandro 	rcu_read_unlock();
261f48ad614SDennis Dalessandro }
262f48ad614SDennis Dalessandro 
263f48ad614SDennis Dalessandro /**
264f48ad614SDennis Dalessandro  * hfi1_make_ud_req - construct a UD request packet
265f48ad614SDennis Dalessandro  * @qp: the QP
266f48ad614SDennis Dalessandro  *
267f48ad614SDennis Dalessandro  * Assume s_lock is held.
268f48ad614SDennis Dalessandro  *
269f48ad614SDennis Dalessandro  * Return 1 if constructed; otherwise, return 0.
270f48ad614SDennis Dalessandro  */
271f48ad614SDennis Dalessandro int hfi1_make_ud_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
272f48ad614SDennis Dalessandro {
273f48ad614SDennis Dalessandro 	struct hfi1_qp_priv *priv = qp->priv;
274261a4351SMike Marciniszyn 	struct ib_other_headers *ohdr;
275f48ad614SDennis Dalessandro 	struct ib_ah_attr *ah_attr;
276f48ad614SDennis Dalessandro 	struct hfi1_pportdata *ppd;
277f48ad614SDennis Dalessandro 	struct hfi1_ibport *ibp;
278f48ad614SDennis Dalessandro 	struct rvt_swqe *wqe;
279f48ad614SDennis Dalessandro 	u32 nwords;
280f48ad614SDennis Dalessandro 	u32 extra_bytes;
281f48ad614SDennis Dalessandro 	u32 bth0;
282f48ad614SDennis Dalessandro 	u16 lrh0;
283f48ad614SDennis Dalessandro 	u16 lid;
284f48ad614SDennis Dalessandro 	int next_cur;
285f48ad614SDennis Dalessandro 	u8 sc5;
286f48ad614SDennis Dalessandro 
287f48ad614SDennis Dalessandro 	ps->s_txreq = get_txreq(ps->dev, qp);
288f48ad614SDennis Dalessandro 	if (IS_ERR(ps->s_txreq))
289f48ad614SDennis Dalessandro 		goto bail_no_tx;
290f48ad614SDennis Dalessandro 
291f48ad614SDennis Dalessandro 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
292f48ad614SDennis Dalessandro 		if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
293f48ad614SDennis Dalessandro 			goto bail;
294f48ad614SDennis Dalessandro 		/* We are in the error state, flush the work request. */
295f48ad614SDennis Dalessandro 		smp_read_barrier_depends(); /* see post_one_send */
296f48ad614SDennis Dalessandro 		if (qp->s_last == ACCESS_ONCE(qp->s_head))
297f48ad614SDennis Dalessandro 			goto bail;
298f48ad614SDennis Dalessandro 		/* If DMAs are in progress, we can't flush immediately. */
299f48ad614SDennis Dalessandro 		if (iowait_sdma_pending(&priv->s_iowait)) {
300f48ad614SDennis Dalessandro 			qp->s_flags |= RVT_S_WAIT_DMA;
301f48ad614SDennis Dalessandro 			goto bail;
302f48ad614SDennis Dalessandro 		}
303f48ad614SDennis Dalessandro 		wqe = rvt_get_swqe_ptr(qp, qp->s_last);
304f48ad614SDennis Dalessandro 		hfi1_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
305f48ad614SDennis Dalessandro 		goto done_free_tx;
306f48ad614SDennis Dalessandro 	}
307f48ad614SDennis Dalessandro 
308f48ad614SDennis Dalessandro 	/* see post_one_send() */
309f48ad614SDennis Dalessandro 	smp_read_barrier_depends();
310f48ad614SDennis Dalessandro 	if (qp->s_cur == ACCESS_ONCE(qp->s_head))
311f48ad614SDennis Dalessandro 		goto bail;
312f48ad614SDennis Dalessandro 
313f48ad614SDennis Dalessandro 	wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
314f48ad614SDennis Dalessandro 	next_cur = qp->s_cur + 1;
315f48ad614SDennis Dalessandro 	if (next_cur >= qp->s_size)
316f48ad614SDennis Dalessandro 		next_cur = 0;
317f48ad614SDennis Dalessandro 
318f48ad614SDennis Dalessandro 	/* Construct the header. */
319f48ad614SDennis Dalessandro 	ibp = to_iport(qp->ibqp.device, qp->port_num);
320f48ad614SDennis Dalessandro 	ppd = ppd_from_ibp(ibp);
321f48ad614SDennis Dalessandro 	ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
322f48ad614SDennis Dalessandro 	if (ah_attr->dlid < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
323f48ad614SDennis Dalessandro 	    ah_attr->dlid == be16_to_cpu(IB_LID_PERMISSIVE)) {
324f48ad614SDennis Dalessandro 		lid = ah_attr->dlid & ~((1 << ppd->lmc) - 1);
325f48ad614SDennis Dalessandro 		if (unlikely(!loopback &&
326f48ad614SDennis Dalessandro 			     (lid == ppd->lid ||
327f48ad614SDennis Dalessandro 			      (lid == be16_to_cpu(IB_LID_PERMISSIVE) &&
328f48ad614SDennis Dalessandro 			      qp->ibqp.qp_type == IB_QPT_GSI)))) {
329f48ad614SDennis Dalessandro 			unsigned long tflags = ps->flags;
330f48ad614SDennis Dalessandro 			/*
331f48ad614SDennis Dalessandro 			 * If DMAs are in progress, we can't generate
332f48ad614SDennis Dalessandro 			 * a completion for the loopback packet since
333f48ad614SDennis Dalessandro 			 * it would be out of order.
334f48ad614SDennis Dalessandro 			 * Instead of waiting, we could queue a
335f48ad614SDennis Dalessandro 			 * zero length descriptor so we get a callback.
336f48ad614SDennis Dalessandro 			 */
337f48ad614SDennis Dalessandro 			if (iowait_sdma_pending(&priv->s_iowait)) {
338f48ad614SDennis Dalessandro 				qp->s_flags |= RVT_S_WAIT_DMA;
339f48ad614SDennis Dalessandro 				goto bail;
340f48ad614SDennis Dalessandro 			}
341f48ad614SDennis Dalessandro 			qp->s_cur = next_cur;
342f48ad614SDennis Dalessandro 			spin_unlock_irqrestore(&qp->s_lock, tflags);
343f48ad614SDennis Dalessandro 			ud_loopback(qp, wqe);
344f48ad614SDennis Dalessandro 			spin_lock_irqsave(&qp->s_lock, tflags);
345f48ad614SDennis Dalessandro 			ps->flags = tflags;
346f48ad614SDennis Dalessandro 			hfi1_send_complete(qp, wqe, IB_WC_SUCCESS);
347f48ad614SDennis Dalessandro 			goto done_free_tx;
348f48ad614SDennis Dalessandro 		}
349f48ad614SDennis Dalessandro 	}
350f48ad614SDennis Dalessandro 
351f48ad614SDennis Dalessandro 	qp->s_cur = next_cur;
352f48ad614SDennis Dalessandro 	extra_bytes = -wqe->length & 3;
353f48ad614SDennis Dalessandro 	nwords = (wqe->length + extra_bytes) >> 2;
354f48ad614SDennis Dalessandro 
355f48ad614SDennis Dalessandro 	/* header size in 32-bit words LRH+BTH+DETH = (8+12+8)/4. */
356f48ad614SDennis Dalessandro 	qp->s_hdrwords = 7;
357e922ae06SDon Hiatt 	ps->s_txreq->s_cur_size = wqe->length;
358b777f154SMitko Haralanov 	ps->s_txreq->ss = &qp->s_sge;
359f48ad614SDennis Dalessandro 	qp->s_srate = ah_attr->static_rate;
360f48ad614SDennis Dalessandro 	qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
361f48ad614SDennis Dalessandro 	qp->s_wqe = wqe;
362f48ad614SDennis Dalessandro 	qp->s_sge.sge = wqe->sg_list[0];
363f48ad614SDennis Dalessandro 	qp->s_sge.sg_list = wqe->sg_list + 1;
364f48ad614SDennis Dalessandro 	qp->s_sge.num_sge = wqe->wr.num_sge;
365f48ad614SDennis Dalessandro 	qp->s_sge.total_len = wqe->length;
366f48ad614SDennis Dalessandro 
367f48ad614SDennis Dalessandro 	if (ah_attr->ah_flags & IB_AH_GRH) {
368f48ad614SDennis Dalessandro 		/* Header size in 32-bit words. */
369f48ad614SDennis Dalessandro 		qp->s_hdrwords += hfi1_make_grh(ibp,
370f48ad614SDennis Dalessandro 						&ps->s_txreq->phdr.hdr.u.l.grh,
371f48ad614SDennis Dalessandro 						&ah_attr->grh,
372f48ad614SDennis Dalessandro 						qp->s_hdrwords, nwords);
373f48ad614SDennis Dalessandro 		lrh0 = HFI1_LRH_GRH;
374f48ad614SDennis Dalessandro 		ohdr = &ps->s_txreq->phdr.hdr.u.l.oth;
375f48ad614SDennis Dalessandro 		/*
376f48ad614SDennis Dalessandro 		 * Don't worry about sending to locally attached multicast
377f48ad614SDennis Dalessandro 		 * QPs.  It is unspecified by the spec. what happens.
378f48ad614SDennis Dalessandro 		 */
379f48ad614SDennis Dalessandro 	} else {
380f48ad614SDennis Dalessandro 		/* Header size in 32-bit words. */
381f48ad614SDennis Dalessandro 		lrh0 = HFI1_LRH_BTH;
382f48ad614SDennis Dalessandro 		ohdr = &ps->s_txreq->phdr.hdr.u.oth;
383f48ad614SDennis Dalessandro 	}
384f48ad614SDennis Dalessandro 	if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
385f48ad614SDennis Dalessandro 		qp->s_hdrwords++;
386f48ad614SDennis Dalessandro 		ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
387f48ad614SDennis Dalessandro 		bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
388f48ad614SDennis Dalessandro 	} else {
389f48ad614SDennis Dalessandro 		bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
390f48ad614SDennis Dalessandro 	}
391f48ad614SDennis Dalessandro 	sc5 = ibp->sl_to_sc[ah_attr->sl];
392f48ad614SDennis Dalessandro 	lrh0 |= (ah_attr->sl & 0xf) << 4;
393f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_type == IB_QPT_SMI) {
394f48ad614SDennis Dalessandro 		lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
395f48ad614SDennis Dalessandro 		priv->s_sc = 0xf;
396f48ad614SDennis Dalessandro 	} else {
397f48ad614SDennis Dalessandro 		lrh0 |= (sc5 & 0xf) << 12;
398f48ad614SDennis Dalessandro 		priv->s_sc = sc5;
399f48ad614SDennis Dalessandro 	}
400f48ad614SDennis Dalessandro 	priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
401f48ad614SDennis Dalessandro 	ps->s_txreq->sde = priv->s_sde;
402f48ad614SDennis Dalessandro 	priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
403f48ad614SDennis Dalessandro 	ps->s_txreq->psc = priv->s_sendcontext;
404f48ad614SDennis Dalessandro 	ps->s_txreq->phdr.hdr.lrh[0] = cpu_to_be16(lrh0);
405f48ad614SDennis Dalessandro 	ps->s_txreq->phdr.hdr.lrh[1] = cpu_to_be16(ah_attr->dlid);
406f48ad614SDennis Dalessandro 	ps->s_txreq->phdr.hdr.lrh[2] =
407f48ad614SDennis Dalessandro 		cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
408f48ad614SDennis Dalessandro 	if (ah_attr->dlid == be16_to_cpu(IB_LID_PERMISSIVE)) {
409f48ad614SDennis Dalessandro 		ps->s_txreq->phdr.hdr.lrh[3] = IB_LID_PERMISSIVE;
410f48ad614SDennis Dalessandro 	} else {
411f48ad614SDennis Dalessandro 		lid = ppd->lid;
412f48ad614SDennis Dalessandro 		if (lid) {
413f48ad614SDennis Dalessandro 			lid |= ah_attr->src_path_bits & ((1 << ppd->lmc) - 1);
414f48ad614SDennis Dalessandro 			ps->s_txreq->phdr.hdr.lrh[3] = cpu_to_be16(lid);
415f48ad614SDennis Dalessandro 		} else {
416f48ad614SDennis Dalessandro 			ps->s_txreq->phdr.hdr.lrh[3] = IB_LID_PERMISSIVE;
417f48ad614SDennis Dalessandro 		}
418f48ad614SDennis Dalessandro 	}
419f48ad614SDennis Dalessandro 	if (wqe->wr.send_flags & IB_SEND_SOLICITED)
420f48ad614SDennis Dalessandro 		bth0 |= IB_BTH_SOLICITED;
421f48ad614SDennis Dalessandro 	bth0 |= extra_bytes << 20;
422f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI)
423f48ad614SDennis Dalessandro 		bth0 |= hfi1_get_pkey(ibp, wqe->ud_wr.pkey_index);
424f48ad614SDennis Dalessandro 	else
425f48ad614SDennis Dalessandro 		bth0 |= hfi1_get_pkey(ibp, qp->s_pkey_index);
426f48ad614SDennis Dalessandro 	ohdr->bth[0] = cpu_to_be32(bth0);
427f48ad614SDennis Dalessandro 	ohdr->bth[1] = cpu_to_be32(wqe->ud_wr.remote_qpn);
428f48ad614SDennis Dalessandro 	ohdr->bth[2] = cpu_to_be32(mask_psn(wqe->psn));
429f48ad614SDennis Dalessandro 	/*
430f48ad614SDennis Dalessandro 	 * Qkeys with the high order bit set mean use the
431f48ad614SDennis Dalessandro 	 * qkey from the QP context instead of the WR (see 10.2.5).
432f48ad614SDennis Dalessandro 	 */
433f48ad614SDennis Dalessandro 	ohdr->u.ud.deth[0] = cpu_to_be32((int)wqe->ud_wr.remote_qkey < 0 ?
434f48ad614SDennis Dalessandro 					 qp->qkey : wqe->ud_wr.remote_qkey);
435f48ad614SDennis Dalessandro 	ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
436f48ad614SDennis Dalessandro 	/* disarm any ahg */
437a9b6b3bcSDasaratharaman Chandramouli 	priv->s_ahg->ahgcount = 0;
438a9b6b3bcSDasaratharaman Chandramouli 	priv->s_ahg->ahgidx = 0;
439a9b6b3bcSDasaratharaman Chandramouli 	priv->s_ahg->tx_flags = 0;
440f48ad614SDennis Dalessandro 	/* pbc */
441f48ad614SDennis Dalessandro 	ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
442f48ad614SDennis Dalessandro 
443f48ad614SDennis Dalessandro 	return 1;
444f48ad614SDennis Dalessandro 
445f48ad614SDennis Dalessandro done_free_tx:
446f48ad614SDennis Dalessandro 	hfi1_put_txreq(ps->s_txreq);
447f48ad614SDennis Dalessandro 	ps->s_txreq = NULL;
448f48ad614SDennis Dalessandro 	return 1;
449f48ad614SDennis Dalessandro 
450f48ad614SDennis Dalessandro bail:
451f48ad614SDennis Dalessandro 	hfi1_put_txreq(ps->s_txreq);
452f48ad614SDennis Dalessandro 
453f48ad614SDennis Dalessandro bail_no_tx:
454f48ad614SDennis Dalessandro 	ps->s_txreq = NULL;
455f48ad614SDennis Dalessandro 	qp->s_flags &= ~RVT_S_BUSY;
456f48ad614SDennis Dalessandro 	qp->s_hdrwords = 0;
457f48ad614SDennis Dalessandro 	return 0;
458f48ad614SDennis Dalessandro }
459f48ad614SDennis Dalessandro 
460f48ad614SDennis Dalessandro /*
461f48ad614SDennis Dalessandro  * Hardware can't check this so we do it here.
462f48ad614SDennis Dalessandro  *
463f48ad614SDennis Dalessandro  * This is a slightly different algorithm than the standard pkey check.  It
464f48ad614SDennis Dalessandro  * special cases the management keys and allows for 0x7fff and 0xffff to be in
465f48ad614SDennis Dalessandro  * the table at the same time.
466f48ad614SDennis Dalessandro  *
467f48ad614SDennis Dalessandro  * @returns the index found or -1 if not found
468f48ad614SDennis Dalessandro  */
469f48ad614SDennis Dalessandro int hfi1_lookup_pkey_idx(struct hfi1_ibport *ibp, u16 pkey)
470f48ad614SDennis Dalessandro {
471f48ad614SDennis Dalessandro 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
472f48ad614SDennis Dalessandro 	unsigned i;
473f48ad614SDennis Dalessandro 
474f48ad614SDennis Dalessandro 	if (pkey == FULL_MGMT_P_KEY || pkey == LIM_MGMT_P_KEY) {
475f48ad614SDennis Dalessandro 		unsigned lim_idx = -1;
476f48ad614SDennis Dalessandro 
477f48ad614SDennis Dalessandro 		for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i) {
478f48ad614SDennis Dalessandro 			/* here we look for an exact match */
479f48ad614SDennis Dalessandro 			if (ppd->pkeys[i] == pkey)
480f48ad614SDennis Dalessandro 				return i;
481f48ad614SDennis Dalessandro 			if (ppd->pkeys[i] == LIM_MGMT_P_KEY)
482f48ad614SDennis Dalessandro 				lim_idx = i;
483f48ad614SDennis Dalessandro 		}
484f48ad614SDennis Dalessandro 
485f48ad614SDennis Dalessandro 		/* did not find 0xffff return 0x7fff idx if found */
486f48ad614SDennis Dalessandro 		if (pkey == FULL_MGMT_P_KEY)
487f48ad614SDennis Dalessandro 			return lim_idx;
488f48ad614SDennis Dalessandro 
489f48ad614SDennis Dalessandro 		/* no match...  */
490f48ad614SDennis Dalessandro 		return -1;
491f48ad614SDennis Dalessandro 	}
492f48ad614SDennis Dalessandro 
493f48ad614SDennis Dalessandro 	pkey &= 0x7fff; /* remove limited/full membership bit */
494f48ad614SDennis Dalessandro 
495f48ad614SDennis Dalessandro 	for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i)
496f48ad614SDennis Dalessandro 		if ((ppd->pkeys[i] & 0x7fff) == pkey)
497f48ad614SDennis Dalessandro 			return i;
498f48ad614SDennis Dalessandro 
499f48ad614SDennis Dalessandro 	/*
500f48ad614SDennis Dalessandro 	 * Should not get here, this means hardware failed to validate pkeys.
501f48ad614SDennis Dalessandro 	 */
502f48ad614SDennis Dalessandro 	return -1;
503f48ad614SDennis Dalessandro }
504f48ad614SDennis Dalessandro 
505f48ad614SDennis Dalessandro void return_cnp(struct hfi1_ibport *ibp, struct rvt_qp *qp, u32 remote_qpn,
506f48ad614SDennis Dalessandro 		u32 pkey, u32 slid, u32 dlid, u8 sc5,
507f48ad614SDennis Dalessandro 		const struct ib_grh *old_grh)
508f48ad614SDennis Dalessandro {
509f48ad614SDennis Dalessandro 	u64 pbc, pbc_flags = 0;
510f48ad614SDennis Dalessandro 	u32 bth0, plen, vl, hwords = 5;
511f48ad614SDennis Dalessandro 	u16 lrh0;
512f48ad614SDennis Dalessandro 	u8 sl = ibp->sc_to_sl[sc5];
513261a4351SMike Marciniszyn 	struct ib_header hdr;
514261a4351SMike Marciniszyn 	struct ib_other_headers *ohdr;
515f48ad614SDennis Dalessandro 	struct pio_buf *pbuf;
516f48ad614SDennis Dalessandro 	struct send_context *ctxt = qp_to_send_context(qp, sc5);
517f48ad614SDennis Dalessandro 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
518f48ad614SDennis Dalessandro 
519f48ad614SDennis Dalessandro 	if (old_grh) {
520f48ad614SDennis Dalessandro 		struct ib_grh *grh = &hdr.u.l.grh;
521f48ad614SDennis Dalessandro 
522f48ad614SDennis Dalessandro 		grh->version_tclass_flow = old_grh->version_tclass_flow;
523f48ad614SDennis Dalessandro 		grh->paylen = cpu_to_be16((hwords - 2 + SIZE_OF_CRC) << 2);
524f48ad614SDennis Dalessandro 		grh->hop_limit = 0xff;
525f48ad614SDennis Dalessandro 		grh->sgid = old_grh->dgid;
526f48ad614SDennis Dalessandro 		grh->dgid = old_grh->sgid;
527f48ad614SDennis Dalessandro 		ohdr = &hdr.u.l.oth;
528f48ad614SDennis Dalessandro 		lrh0 = HFI1_LRH_GRH;
529f48ad614SDennis Dalessandro 		hwords += sizeof(struct ib_grh) / sizeof(u32);
530f48ad614SDennis Dalessandro 	} else {
531f48ad614SDennis Dalessandro 		ohdr = &hdr.u.oth;
532f48ad614SDennis Dalessandro 		lrh0 = HFI1_LRH_BTH;
533f48ad614SDennis Dalessandro 	}
534f48ad614SDennis Dalessandro 
535f48ad614SDennis Dalessandro 	lrh0 |= (sc5 & 0xf) << 12 | sl << 4;
536f48ad614SDennis Dalessandro 
537f48ad614SDennis Dalessandro 	bth0 = pkey | (IB_OPCODE_CNP << 24);
538f48ad614SDennis Dalessandro 	ohdr->bth[0] = cpu_to_be32(bth0);
539f48ad614SDennis Dalessandro 
540f48ad614SDennis Dalessandro 	ohdr->bth[1] = cpu_to_be32(remote_qpn | (1 << HFI1_BECN_SHIFT));
541f48ad614SDennis Dalessandro 	ohdr->bth[2] = 0; /* PSN 0 */
542f48ad614SDennis Dalessandro 
543f48ad614SDennis Dalessandro 	hdr.lrh[0] = cpu_to_be16(lrh0);
544f48ad614SDennis Dalessandro 	hdr.lrh[1] = cpu_to_be16(dlid);
545f48ad614SDennis Dalessandro 	hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
546f48ad614SDennis Dalessandro 	hdr.lrh[3] = cpu_to_be16(slid);
547f48ad614SDennis Dalessandro 
548f48ad614SDennis Dalessandro 	plen = 2 /* PBC */ + hwords;
549f48ad614SDennis Dalessandro 	pbc_flags |= (!!(sc5 & 0x10)) << PBC_DC_INFO_SHIFT;
550f48ad614SDennis Dalessandro 	vl = sc_to_vlt(ppd->dd, sc5);
551f48ad614SDennis Dalessandro 	pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
552f48ad614SDennis Dalessandro 	if (ctxt) {
553f48ad614SDennis Dalessandro 		pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
554f48ad614SDennis Dalessandro 		if (pbuf)
555f48ad614SDennis Dalessandro 			ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
556f48ad614SDennis Dalessandro 						 &hdr, hwords);
557f48ad614SDennis Dalessandro 	}
558f48ad614SDennis Dalessandro }
559f48ad614SDennis Dalessandro 
560f48ad614SDennis Dalessandro /*
561f48ad614SDennis Dalessandro  * opa_smp_check() - Do the regular pkey checking, and the additional
562f3809209SJianxin Xiong  * checks for SMPs specified in OPAv1 rev 1.0, 9/19/2016 update, section
563f3809209SJianxin Xiong  * 9.10.25 ("SMA Packet Checks").
564f48ad614SDennis Dalessandro  *
565f48ad614SDennis Dalessandro  * Note that:
566f48ad614SDennis Dalessandro  *   - Checks are done using the pkey directly from the packet's BTH,
567f48ad614SDennis Dalessandro  *     and specifically _not_ the pkey that we attach to the completion,
568f48ad614SDennis Dalessandro  *     which may be different.
569f48ad614SDennis Dalessandro  *   - These checks are specifically for "non-local" SMPs (i.e., SMPs
570f48ad614SDennis Dalessandro  *     which originated on another node). SMPs which are sent from, and
571f48ad614SDennis Dalessandro  *     destined to this node are checked in opa_local_smp_check().
572f48ad614SDennis Dalessandro  *
573f48ad614SDennis Dalessandro  * At the point where opa_smp_check() is called, we know:
574f48ad614SDennis Dalessandro  *   - destination QP is QP0
575f48ad614SDennis Dalessandro  *
576f48ad614SDennis Dalessandro  * opa_smp_check() returns 0 if all checks succeed, 1 otherwise.
577f48ad614SDennis Dalessandro  */
578f48ad614SDennis Dalessandro static int opa_smp_check(struct hfi1_ibport *ibp, u16 pkey, u8 sc5,
579f48ad614SDennis Dalessandro 			 struct rvt_qp *qp, u16 slid, struct opa_smp *smp)
580f48ad614SDennis Dalessandro {
581f48ad614SDennis Dalessandro 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
582f48ad614SDennis Dalessandro 
583f48ad614SDennis Dalessandro 	/*
584f48ad614SDennis Dalessandro 	 * I don't think it's possible for us to get here with sc != 0xf,
585f48ad614SDennis Dalessandro 	 * but check it to be certain.
586f48ad614SDennis Dalessandro 	 */
587f48ad614SDennis Dalessandro 	if (sc5 != 0xf)
588f48ad614SDennis Dalessandro 		return 1;
589f48ad614SDennis Dalessandro 
590f48ad614SDennis Dalessandro 	if (rcv_pkey_check(ppd, pkey, sc5, slid))
591f48ad614SDennis Dalessandro 		return 1;
592f48ad614SDennis Dalessandro 
593f48ad614SDennis Dalessandro 	/*
594f48ad614SDennis Dalessandro 	 * At this point we know (and so don't need to check again) that
595f48ad614SDennis Dalessandro 	 * the pkey is either LIM_MGMT_P_KEY, or FULL_MGMT_P_KEY
596f48ad614SDennis Dalessandro 	 * (see ingress_pkey_check).
597f48ad614SDennis Dalessandro 	 */
598f48ad614SDennis Dalessandro 	if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE &&
599f48ad614SDennis Dalessandro 	    smp->mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED) {
600f48ad614SDennis Dalessandro 		ingress_pkey_table_fail(ppd, pkey, slid);
601f48ad614SDennis Dalessandro 		return 1;
602f48ad614SDennis Dalessandro 	}
603f48ad614SDennis Dalessandro 
604f48ad614SDennis Dalessandro 	/*
605f48ad614SDennis Dalessandro 	 * SMPs fall into one of four (disjoint) categories:
606f3809209SJianxin Xiong 	 * SMA request, SMA response, SMA trap, or SMA trap repress.
607f3809209SJianxin Xiong 	 * Our response depends, in part, on which type of SMP we're
608f3809209SJianxin Xiong 	 * processing.
609f48ad614SDennis Dalessandro 	 *
610f3809209SJianxin Xiong 	 * If this is an SMA response, skip the check here.
611f3809209SJianxin Xiong 	 *
612f3809209SJianxin Xiong 	 * If this is an SMA request or SMA trap repress:
613f3809209SJianxin Xiong 	 *   - pkey != FULL_MGMT_P_KEY =>
614f48ad614SDennis Dalessandro 	 *       increment port recv constraint errors, drop MAD
615f3809209SJianxin Xiong 	 *
616f3809209SJianxin Xiong 	 * Otherwise:
617f3809209SJianxin Xiong 	 *    - accept if the port is running an SM
618f3809209SJianxin Xiong 	 *    - drop MAD if it's an SMA trap
619f3809209SJianxin Xiong 	 *    - pkey == FULL_MGMT_P_KEY =>
620f3809209SJianxin Xiong 	 *        reply with unsupported method
621f48ad614SDennis Dalessandro 	 *    - pkey != FULL_MGMT_P_KEY =>
622f48ad614SDennis Dalessandro 	 *	  increment port recv constraint errors, drop MAD
623f48ad614SDennis Dalessandro 	 */
624f48ad614SDennis Dalessandro 	switch (smp->method) {
625f3809209SJianxin Xiong 	case IB_MGMT_METHOD_GET_RESP:
626f3809209SJianxin Xiong 	case IB_MGMT_METHOD_REPORT_RESP:
627f3809209SJianxin Xiong 		break;
628f48ad614SDennis Dalessandro 	case IB_MGMT_METHOD_GET:
629f48ad614SDennis Dalessandro 	case IB_MGMT_METHOD_SET:
630f48ad614SDennis Dalessandro 	case IB_MGMT_METHOD_REPORT:
631f48ad614SDennis Dalessandro 	case IB_MGMT_METHOD_TRAP_REPRESS:
632f48ad614SDennis Dalessandro 		if (pkey != FULL_MGMT_P_KEY) {
633f48ad614SDennis Dalessandro 			ingress_pkey_table_fail(ppd, pkey, slid);
634f48ad614SDennis Dalessandro 			return 1;
635f48ad614SDennis Dalessandro 		}
636f48ad614SDennis Dalessandro 		break;
637f3809209SJianxin Xiong 	default:
638f48ad614SDennis Dalessandro 		if (ibp->rvp.port_cap_flags & IB_PORT_SM)
639f48ad614SDennis Dalessandro 			return 0;
640f3809209SJianxin Xiong 		if (smp->method == IB_MGMT_METHOD_TRAP)
641f3809209SJianxin Xiong 			return 1;
642f48ad614SDennis Dalessandro 		if (pkey == FULL_MGMT_P_KEY) {
643f48ad614SDennis Dalessandro 			smp->status |= IB_SMP_UNSUP_METHOD;
644f48ad614SDennis Dalessandro 			return 0;
645f48ad614SDennis Dalessandro 		}
646f48ad614SDennis Dalessandro 		ingress_pkey_table_fail(ppd, pkey, slid);
647f48ad614SDennis Dalessandro 		return 1;
648f48ad614SDennis Dalessandro 	}
649f48ad614SDennis Dalessandro 	return 0;
650f48ad614SDennis Dalessandro }
651f48ad614SDennis Dalessandro 
652f48ad614SDennis Dalessandro /**
653f48ad614SDennis Dalessandro  * hfi1_ud_rcv - receive an incoming UD packet
654f48ad614SDennis Dalessandro  * @ibp: the port the packet came in on
655f48ad614SDennis Dalessandro  * @hdr: the packet header
656f48ad614SDennis Dalessandro  * @rcv_flags: flags relevant to rcv processing
657f48ad614SDennis Dalessandro  * @data: the packet data
658f48ad614SDennis Dalessandro  * @tlen: the packet length
659f48ad614SDennis Dalessandro  * @qp: the QP the packet came on
660f48ad614SDennis Dalessandro  *
661f48ad614SDennis Dalessandro  * This is called from qp_rcv() to process an incoming UD packet
662f48ad614SDennis Dalessandro  * for the given QP.
663f48ad614SDennis Dalessandro  * Called at interrupt level.
664f48ad614SDennis Dalessandro  */
665f48ad614SDennis Dalessandro void hfi1_ud_rcv(struct hfi1_packet *packet)
666f48ad614SDennis Dalessandro {
667261a4351SMike Marciniszyn 	struct ib_other_headers *ohdr = packet->ohdr;
668f48ad614SDennis Dalessandro 	int opcode;
669f48ad614SDennis Dalessandro 	u32 hdrsize = packet->hlen;
670f48ad614SDennis Dalessandro 	struct ib_wc wc;
671f48ad614SDennis Dalessandro 	u32 qkey;
672f48ad614SDennis Dalessandro 	u32 src_qp;
673f48ad614SDennis Dalessandro 	u16 dlid, pkey;
674f48ad614SDennis Dalessandro 	int mgmt_pkey_idx = -1;
675f3e862cbSSebastian Sanchez 	struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
67689c057caSDasaratharaman Chandramouli 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
677261a4351SMike Marciniszyn 	struct ib_header *hdr = packet->hdr;
678f48ad614SDennis Dalessandro 	u32 rcv_flags = packet->rcv_flags;
679f48ad614SDennis Dalessandro 	void *data = packet->ebuf;
680f48ad614SDennis Dalessandro 	u32 tlen = packet->tlen;
681f48ad614SDennis Dalessandro 	struct rvt_qp *qp = packet->qp;
682f48ad614SDennis Dalessandro 	bool has_grh = rcv_flags & HFI1_HAS_GRH;
683261a4351SMike Marciniszyn 	u8 sc5 = hdr2sc(hdr, packet->rhf);
68489c057caSDasaratharaman Chandramouli 	u32 bth1;
68589c057caSDasaratharaman Chandramouli 	u8 sl_from_sc, sl;
68689c057caSDasaratharaman Chandramouli 	u16 slid;
68789c057caSDasaratharaman Chandramouli 	u8 extra_bytes;
688f48ad614SDennis Dalessandro 
689f48ad614SDennis Dalessandro 	qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
690f48ad614SDennis Dalessandro 	src_qp = be32_to_cpu(ohdr->u.ud.deth[1]) & RVT_QPN_MASK;
691f48ad614SDennis Dalessandro 	dlid = be16_to_cpu(hdr->lrh[1]);
69289c057caSDasaratharaman Chandramouli 	bth1 = be32_to_cpu(ohdr->bth[1]);
69389c057caSDasaratharaman Chandramouli 	slid = be16_to_cpu(hdr->lrh[3]);
69489c057caSDasaratharaman Chandramouli 	pkey = (u16)be32_to_cpu(ohdr->bth[0]);
69589c057caSDasaratharaman Chandramouli 	sl = (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xf;
69689c057caSDasaratharaman Chandramouli 	extra_bytes = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
69789c057caSDasaratharaman Chandramouli 	extra_bytes += (SIZE_OF_CRC << 2);
69889c057caSDasaratharaman Chandramouli 	sl_from_sc = ibp->sc_to_sl[sc5];
699f48ad614SDennis Dalessandro 
700f48ad614SDennis Dalessandro 	opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
701f48ad614SDennis Dalessandro 	opcode &= 0xff;
702f48ad614SDennis Dalessandro 
7035fd2b562SMitko Haralanov 	process_ecn(qp, packet, (opcode != IB_OPCODE_CNP));
704f48ad614SDennis Dalessandro 	/*
705f48ad614SDennis Dalessandro 	 * Get the number of bytes the message was padded by
706f48ad614SDennis Dalessandro 	 * and drop incomplete packets.
707f48ad614SDennis Dalessandro 	 */
70889c057caSDasaratharaman Chandramouli 	if (unlikely(tlen < (hdrsize + extra_bytes)))
709f48ad614SDennis Dalessandro 		goto drop;
710f48ad614SDennis Dalessandro 
71189c057caSDasaratharaman Chandramouli 	tlen -= hdrsize + extra_bytes;
712f48ad614SDennis Dalessandro 
713f48ad614SDennis Dalessandro 	/*
714f48ad614SDennis Dalessandro 	 * Check that the permissive LID is only used on QP0
715f48ad614SDennis Dalessandro 	 * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
716f48ad614SDennis Dalessandro 	 */
717f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_num) {
718f48ad614SDennis Dalessandro 		if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||
719f48ad614SDennis Dalessandro 			     hdr->lrh[3] == IB_LID_PERMISSIVE))
720f48ad614SDennis Dalessandro 			goto drop;
721f48ad614SDennis Dalessandro 		if (qp->ibqp.qp_num > 1) {
722f48ad614SDennis Dalessandro 			if (unlikely(rcv_pkey_check(ppd, pkey, sc5, slid))) {
723f48ad614SDennis Dalessandro 				/*
724f48ad614SDennis Dalessandro 				 * Traps will not be sent for packets dropped
725f48ad614SDennis Dalessandro 				 * by the HW. This is fine, as sending trap
726f48ad614SDennis Dalessandro 				 * for invalid pkeys is optional according to
727f48ad614SDennis Dalessandro 				 * IB spec (release 1.3, section 10.9.4)
728f48ad614SDennis Dalessandro 				 */
729f48ad614SDennis Dalessandro 				hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY,
73089c057caSDasaratharaman Chandramouli 					       pkey, sl,
731f48ad614SDennis Dalessandro 					       src_qp, qp->ibqp.qp_num,
73289c057caSDasaratharaman Chandramouli 					       slid, dlid);
733f48ad614SDennis Dalessandro 				return;
734f48ad614SDennis Dalessandro 			}
735f48ad614SDennis Dalessandro 		} else {
736f48ad614SDennis Dalessandro 			/* GSI packet */
737f48ad614SDennis Dalessandro 			mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
738f48ad614SDennis Dalessandro 			if (mgmt_pkey_idx < 0)
739f48ad614SDennis Dalessandro 				goto drop;
740f48ad614SDennis Dalessandro 		}
741f48ad614SDennis Dalessandro 		if (unlikely(qkey != qp->qkey)) {
74289c057caSDasaratharaman Chandramouli 			hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_Q_KEY, qkey, sl,
743f48ad614SDennis Dalessandro 				       src_qp, qp->ibqp.qp_num,
74489c057caSDasaratharaman Chandramouli 				       slid, dlid);
745f48ad614SDennis Dalessandro 			return;
746f48ad614SDennis Dalessandro 		}
747f48ad614SDennis Dalessandro 		/* Drop invalid MAD packets (see 13.5.3.1). */
748f48ad614SDennis Dalessandro 		if (unlikely(qp->ibqp.qp_num == 1 &&
74989c057caSDasaratharaman Chandramouli 			     (tlen > 2048 || (sc5 == 0xF))))
750f48ad614SDennis Dalessandro 			goto drop;
751f48ad614SDennis Dalessandro 	} else {
752f48ad614SDennis Dalessandro 		/* Received on QP0, and so by definition, this is an SMP */
753f48ad614SDennis Dalessandro 		struct opa_smp *smp = (struct opa_smp *)data;
754f48ad614SDennis Dalessandro 
755f48ad614SDennis Dalessandro 		if (opa_smp_check(ibp, pkey, sc5, qp, slid, smp))
756f48ad614SDennis Dalessandro 			goto drop;
757f48ad614SDennis Dalessandro 
758f48ad614SDennis Dalessandro 		if (tlen > 2048)
759f48ad614SDennis Dalessandro 			goto drop;
760f48ad614SDennis Dalessandro 		if ((hdr->lrh[1] == IB_LID_PERMISSIVE ||
761f48ad614SDennis Dalessandro 		     hdr->lrh[3] == IB_LID_PERMISSIVE) &&
762f48ad614SDennis Dalessandro 		    smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
763f48ad614SDennis Dalessandro 			goto drop;
764f48ad614SDennis Dalessandro 
765f48ad614SDennis Dalessandro 		/* look up SMI pkey */
766f48ad614SDennis Dalessandro 		mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
767f48ad614SDennis Dalessandro 		if (mgmt_pkey_idx < 0)
768f48ad614SDennis Dalessandro 			goto drop;
769f48ad614SDennis Dalessandro 	}
770f48ad614SDennis Dalessandro 
771f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_num > 1 &&
772f48ad614SDennis Dalessandro 	    opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
773f48ad614SDennis Dalessandro 		wc.ex.imm_data = ohdr->u.ud.imm_data;
774f48ad614SDennis Dalessandro 		wc.wc_flags = IB_WC_WITH_IMM;
775f48ad614SDennis Dalessandro 		tlen -= sizeof(u32);
776f48ad614SDennis Dalessandro 	} else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
777f48ad614SDennis Dalessandro 		wc.ex.imm_data = 0;
778f48ad614SDennis Dalessandro 		wc.wc_flags = 0;
779f48ad614SDennis Dalessandro 	} else {
780f48ad614SDennis Dalessandro 		goto drop;
781f48ad614SDennis Dalessandro 	}
782f48ad614SDennis Dalessandro 
783f48ad614SDennis Dalessandro 	/*
784f48ad614SDennis Dalessandro 	 * A GRH is expected to precede the data even if not
785f48ad614SDennis Dalessandro 	 * present on the wire.
786f48ad614SDennis Dalessandro 	 */
787f48ad614SDennis Dalessandro 	wc.byte_len = tlen + sizeof(struct ib_grh);
788f48ad614SDennis Dalessandro 
789f48ad614SDennis Dalessandro 	/*
790f48ad614SDennis Dalessandro 	 * Get the next work request entry to find where to put the data.
791f48ad614SDennis Dalessandro 	 */
792f48ad614SDennis Dalessandro 	if (qp->r_flags & RVT_R_REUSE_SGE) {
793f48ad614SDennis Dalessandro 		qp->r_flags &= ~RVT_R_REUSE_SGE;
794f48ad614SDennis Dalessandro 	} else {
795f48ad614SDennis Dalessandro 		int ret;
796f48ad614SDennis Dalessandro 
797f48ad614SDennis Dalessandro 		ret = hfi1_rvt_get_rwqe(qp, 0);
798f48ad614SDennis Dalessandro 		if (ret < 0) {
799*beb5a042SBrian Welty 			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
800f48ad614SDennis Dalessandro 			return;
801f48ad614SDennis Dalessandro 		}
802f48ad614SDennis Dalessandro 		if (!ret) {
803f48ad614SDennis Dalessandro 			if (qp->ibqp.qp_num == 0)
804f48ad614SDennis Dalessandro 				ibp->rvp.n_vl15_dropped++;
805f48ad614SDennis Dalessandro 			return;
806f48ad614SDennis Dalessandro 		}
807f48ad614SDennis Dalessandro 	}
808f48ad614SDennis Dalessandro 	/* Silently drop packets which are too big. */
809f48ad614SDennis Dalessandro 	if (unlikely(wc.byte_len > qp->r_len)) {
810f48ad614SDennis Dalessandro 		qp->r_flags |= RVT_R_REUSE_SGE;
811f48ad614SDennis Dalessandro 		goto drop;
812f48ad614SDennis Dalessandro 	}
813f48ad614SDennis Dalessandro 	if (has_grh) {
814f48ad614SDennis Dalessandro 		hfi1_copy_sge(&qp->r_sge, &hdr->u.l.grh,
815f48ad614SDennis Dalessandro 			      sizeof(struct ib_grh), 1, 0);
816f48ad614SDennis Dalessandro 		wc.wc_flags |= IB_WC_GRH;
817f48ad614SDennis Dalessandro 	} else {
818f48ad614SDennis Dalessandro 		hfi1_skip_sge(&qp->r_sge, sizeof(struct ib_grh), 1);
819f48ad614SDennis Dalessandro 	}
820f48ad614SDennis Dalessandro 	hfi1_copy_sge(&qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh),
821f48ad614SDennis Dalessandro 		      1, 0);
822f48ad614SDennis Dalessandro 	rvt_put_ss(&qp->r_sge);
823f48ad614SDennis Dalessandro 	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
824f48ad614SDennis Dalessandro 		return;
825f48ad614SDennis Dalessandro 	wc.wr_id = qp->r_wr_id;
826f48ad614SDennis Dalessandro 	wc.status = IB_WC_SUCCESS;
827f48ad614SDennis Dalessandro 	wc.opcode = IB_WC_RECV;
828f48ad614SDennis Dalessandro 	wc.vendor_err = 0;
829f48ad614SDennis Dalessandro 	wc.qp = &qp->ibqp;
830f48ad614SDennis Dalessandro 	wc.src_qp = src_qp;
831f48ad614SDennis Dalessandro 
832f48ad614SDennis Dalessandro 	if (qp->ibqp.qp_type == IB_QPT_GSI ||
833f48ad614SDennis Dalessandro 	    qp->ibqp.qp_type == IB_QPT_SMI) {
834f48ad614SDennis Dalessandro 		if (mgmt_pkey_idx < 0) {
835f48ad614SDennis Dalessandro 			if (net_ratelimit()) {
836f48ad614SDennis Dalessandro 				struct hfi1_devdata *dd = ppd->dd;
837f48ad614SDennis Dalessandro 
838f48ad614SDennis Dalessandro 				dd_dev_err(dd, "QP type %d mgmt_pkey_idx < 0 and packet not dropped???\n",
839f48ad614SDennis Dalessandro 					   qp->ibqp.qp_type);
840f48ad614SDennis Dalessandro 				mgmt_pkey_idx = 0;
841f48ad614SDennis Dalessandro 			}
842f48ad614SDennis Dalessandro 		}
843f48ad614SDennis Dalessandro 		wc.pkey_index = (unsigned)mgmt_pkey_idx;
844f48ad614SDennis Dalessandro 	} else {
845f48ad614SDennis Dalessandro 		wc.pkey_index = 0;
846f48ad614SDennis Dalessandro 	}
847f48ad614SDennis Dalessandro 
84889c057caSDasaratharaman Chandramouli 	wc.slid = slid;
84989c057caSDasaratharaman Chandramouli 	wc.sl = sl_from_sc;
850f48ad614SDennis Dalessandro 
851f48ad614SDennis Dalessandro 	/*
852f48ad614SDennis Dalessandro 	 * Save the LMC lower bits if the destination LID is a unicast LID.
853f48ad614SDennis Dalessandro 	 */
854f48ad614SDennis Dalessandro 	wc.dlid_path_bits = dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE) ? 0 :
855f48ad614SDennis Dalessandro 		dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
856f48ad614SDennis Dalessandro 	wc.port_num = qp->port_num;
857f48ad614SDennis Dalessandro 	/* Signal completion event if the solicited bit is set. */
858f48ad614SDennis Dalessandro 	rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
859f48ad614SDennis Dalessandro 		     (ohdr->bth[0] &
860f48ad614SDennis Dalessandro 		      cpu_to_be32(IB_BTH_SOLICITED)) != 0);
861f48ad614SDennis Dalessandro 	return;
862f48ad614SDennis Dalessandro 
863f48ad614SDennis Dalessandro drop:
864f48ad614SDennis Dalessandro 	ibp->rvp.n_pkt_drops++;
865f48ad614SDennis Dalessandro }
866