162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2012 - 2019 Intel Corporation.  All rights reserved.
362306a36Sopenharmony_ci * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
462306a36Sopenharmony_ci * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * This software is available to you under a choice of one of two
762306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
862306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
962306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
1062306a36Sopenharmony_ci * OpenIB.org BSD license below:
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1362306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1462306a36Sopenharmony_ci *     conditions are met:
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
1762306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
1862306a36Sopenharmony_ci *        disclaimer.
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
2162306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2262306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
2362306a36Sopenharmony_ci *        provided with the distribution.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2662306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2762306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2862306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2962306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3062306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3162306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3262306a36Sopenharmony_ci * SOFTWARE.
3362306a36Sopenharmony_ci */
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci#include <rdma/ib_smi.h>
3662306a36Sopenharmony_ci#include <rdma/ib_verbs.h>
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#include "qib.h"
3962306a36Sopenharmony_ci#include "qib_mad.h"
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci/**
4262306a36Sopenharmony_ci * qib_ud_loopback - handle send on loopback QPs
4362306a36Sopenharmony_ci * @sqp: the sending QP
4462306a36Sopenharmony_ci * @swqe: the send work request
4562306a36Sopenharmony_ci *
4662306a36Sopenharmony_ci * This is called from qib_make_ud_req() to forward a WQE addressed
4762306a36Sopenharmony_ci * to the same HCA.
4862306a36Sopenharmony_ci * Note that the receive interrupt handler may be calling qib_ud_rcv()
4962306a36Sopenharmony_ci * while this is being called.
5062306a36Sopenharmony_ci */
5162306a36Sopenharmony_cistatic void qib_ud_loopback(struct rvt_qp *sqp, struct rvt_swqe *swqe)
5262306a36Sopenharmony_ci{
5362306a36Sopenharmony_ci	struct qib_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
5462306a36Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
5562306a36Sopenharmony_ci	struct qib_devdata *dd = ppd->dd;
5662306a36Sopenharmony_ci	struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
5762306a36Sopenharmony_ci	struct rvt_qp *qp;
5862306a36Sopenharmony_ci	struct rdma_ah_attr *ah_attr;
5962306a36Sopenharmony_ci	unsigned long flags;
6062306a36Sopenharmony_ci	struct rvt_sge_state ssge;
6162306a36Sopenharmony_ci	struct rvt_sge *sge;
6262306a36Sopenharmony_ci	struct ib_wc wc;
6362306a36Sopenharmony_ci	u32 length;
6462306a36Sopenharmony_ci	enum ib_qp_type sqptype, dqptype;
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	rcu_read_lock();
6762306a36Sopenharmony_ci	qp = rvt_lookup_qpn(rdi, &ibp->rvp, rvt_get_swqe_remote_qpn(swqe));
6862306a36Sopenharmony_ci	if (!qp) {
6962306a36Sopenharmony_ci		ibp->rvp.n_pkt_drops++;
7062306a36Sopenharmony_ci		goto drop;
7162306a36Sopenharmony_ci	}
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci	sqptype = sqp->ibqp.qp_type == IB_QPT_GSI ?
7462306a36Sopenharmony_ci			IB_QPT_UD : sqp->ibqp.qp_type;
7562306a36Sopenharmony_ci	dqptype = qp->ibqp.qp_type == IB_QPT_GSI ?
7662306a36Sopenharmony_ci			IB_QPT_UD : qp->ibqp.qp_type;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	if (dqptype != sqptype ||
7962306a36Sopenharmony_ci	    !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
8062306a36Sopenharmony_ci		ibp->rvp.n_pkt_drops++;
8162306a36Sopenharmony_ci		goto drop;
8262306a36Sopenharmony_ci	}
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	ah_attr = rvt_get_swqe_ah_attr(swqe);
8562306a36Sopenharmony_ci	ppd = ppd_from_ibp(ibp);
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	if (qp->ibqp.qp_num > 1) {
8862306a36Sopenharmony_ci		u16 pkey1;
8962306a36Sopenharmony_ci		u16 pkey2;
9062306a36Sopenharmony_ci		u16 lid;
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci		pkey1 = qib_get_pkey(ibp, sqp->s_pkey_index);
9362306a36Sopenharmony_ci		pkey2 = qib_get_pkey(ibp, qp->s_pkey_index);
9462306a36Sopenharmony_ci		if (unlikely(!qib_pkey_ok(pkey1, pkey2))) {
9562306a36Sopenharmony_ci			lid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
9662306a36Sopenharmony_ci					  ((1 << ppd->lmc) - 1));
9762306a36Sopenharmony_ci			qib_bad_pkey(ibp, pkey1,
9862306a36Sopenharmony_ci				     rdma_ah_get_sl(ah_attr),
9962306a36Sopenharmony_ci				     sqp->ibqp.qp_num, qp->ibqp.qp_num,
10062306a36Sopenharmony_ci				     cpu_to_be16(lid),
10162306a36Sopenharmony_ci				     cpu_to_be16(rdma_ah_get_dlid(ah_attr)));
10262306a36Sopenharmony_ci			goto drop;
10362306a36Sopenharmony_ci		}
10462306a36Sopenharmony_ci	}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	/*
10762306a36Sopenharmony_ci	 * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
10862306a36Sopenharmony_ci	 * Qkeys with the high order bit set mean use the
10962306a36Sopenharmony_ci	 * qkey from the QP context instead of the WR (see 10.2.5).
11062306a36Sopenharmony_ci	 */
11162306a36Sopenharmony_ci	if (qp->ibqp.qp_num) {
11262306a36Sopenharmony_ci		u32 qkey;
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci		qkey = (int)rvt_get_swqe_remote_qkey(swqe) < 0 ?
11562306a36Sopenharmony_ci			sqp->qkey : rvt_get_swqe_remote_qkey(swqe);
11662306a36Sopenharmony_ci		if (unlikely(qkey != qp->qkey))
11762306a36Sopenharmony_ci			goto drop;
11862306a36Sopenharmony_ci	}
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	/*
12162306a36Sopenharmony_ci	 * A GRH is expected to precede the data even if not
12262306a36Sopenharmony_ci	 * present on the wire.
12362306a36Sopenharmony_ci	 */
12462306a36Sopenharmony_ci	length = swqe->length;
12562306a36Sopenharmony_ci	memset(&wc, 0, sizeof(wc));
12662306a36Sopenharmony_ci	wc.byte_len = length + sizeof(struct ib_grh);
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
12962306a36Sopenharmony_ci		wc.wc_flags = IB_WC_WITH_IMM;
13062306a36Sopenharmony_ci		wc.ex.imm_data = swqe->wr.ex.imm_data;
13162306a36Sopenharmony_ci	}
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	spin_lock_irqsave(&qp->r_lock, flags);
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	/*
13662306a36Sopenharmony_ci	 * Get the next work request entry to find where to put the data.
13762306a36Sopenharmony_ci	 */
13862306a36Sopenharmony_ci	if (qp->r_flags & RVT_R_REUSE_SGE)
13962306a36Sopenharmony_ci		qp->r_flags &= ~RVT_R_REUSE_SGE;
14062306a36Sopenharmony_ci	else {
14162306a36Sopenharmony_ci		int ret;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci		ret = rvt_get_rwqe(qp, false);
14462306a36Sopenharmony_ci		if (ret < 0) {
14562306a36Sopenharmony_ci			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
14662306a36Sopenharmony_ci			goto bail_unlock;
14762306a36Sopenharmony_ci		}
14862306a36Sopenharmony_ci		if (!ret) {
14962306a36Sopenharmony_ci			if (qp->ibqp.qp_num == 0)
15062306a36Sopenharmony_ci				ibp->rvp.n_vl15_dropped++;
15162306a36Sopenharmony_ci			goto bail_unlock;
15262306a36Sopenharmony_ci		}
15362306a36Sopenharmony_ci	}
15462306a36Sopenharmony_ci	/* Silently drop packets which are too big. */
15562306a36Sopenharmony_ci	if (unlikely(wc.byte_len > qp->r_len)) {
15662306a36Sopenharmony_ci		qp->r_flags |= RVT_R_REUSE_SGE;
15762306a36Sopenharmony_ci		ibp->rvp.n_pkt_drops++;
15862306a36Sopenharmony_ci		goto bail_unlock;
15962306a36Sopenharmony_ci	}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
16262306a36Sopenharmony_ci		struct ib_grh grh;
16362306a36Sopenharmony_ci		const struct ib_global_route *grd = rdma_ah_read_grh(ah_attr);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci		qib_make_grh(ibp, &grh, grd, 0, 0);
16662306a36Sopenharmony_ci		rvt_copy_sge(qp, &qp->r_sge, &grh,
16762306a36Sopenharmony_ci			     sizeof(grh), true, false);
16862306a36Sopenharmony_ci		wc.wc_flags |= IB_WC_GRH;
16962306a36Sopenharmony_ci	} else
17062306a36Sopenharmony_ci		rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
17162306a36Sopenharmony_ci	ssge.sg_list = swqe->sg_list + 1;
17262306a36Sopenharmony_ci	ssge.sge = *swqe->sg_list;
17362306a36Sopenharmony_ci	ssge.num_sge = swqe->wr.num_sge;
17462306a36Sopenharmony_ci	sge = &ssge.sge;
17562306a36Sopenharmony_ci	while (length) {
17662306a36Sopenharmony_ci		u32 len = rvt_get_sge_length(sge, length);
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci		rvt_copy_sge(qp, &qp->r_sge, sge->vaddr, len, true, false);
17962306a36Sopenharmony_ci		sge->vaddr += len;
18062306a36Sopenharmony_ci		sge->length -= len;
18162306a36Sopenharmony_ci		sge->sge_length -= len;
18262306a36Sopenharmony_ci		if (sge->sge_length == 0) {
18362306a36Sopenharmony_ci			if (--ssge.num_sge)
18462306a36Sopenharmony_ci				*sge = *ssge.sg_list++;
18562306a36Sopenharmony_ci		} else if (sge->length == 0 && sge->mr->lkey) {
18662306a36Sopenharmony_ci			if (++sge->n >= RVT_SEGSZ) {
18762306a36Sopenharmony_ci				if (++sge->m >= sge->mr->mapsz)
18862306a36Sopenharmony_ci					break;
18962306a36Sopenharmony_ci				sge->n = 0;
19062306a36Sopenharmony_ci			}
19162306a36Sopenharmony_ci			sge->vaddr =
19262306a36Sopenharmony_ci				sge->mr->map[sge->m]->segs[sge->n].vaddr;
19362306a36Sopenharmony_ci			sge->length =
19462306a36Sopenharmony_ci				sge->mr->map[sge->m]->segs[sge->n].length;
19562306a36Sopenharmony_ci		}
19662306a36Sopenharmony_ci		length -= len;
19762306a36Sopenharmony_ci	}
19862306a36Sopenharmony_ci	rvt_put_ss(&qp->r_sge);
19962306a36Sopenharmony_ci	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
20062306a36Sopenharmony_ci		goto bail_unlock;
20162306a36Sopenharmony_ci	wc.wr_id = qp->r_wr_id;
20262306a36Sopenharmony_ci	wc.status = IB_WC_SUCCESS;
20362306a36Sopenharmony_ci	wc.opcode = IB_WC_RECV;
20462306a36Sopenharmony_ci	wc.qp = &qp->ibqp;
20562306a36Sopenharmony_ci	wc.src_qp = sqp->ibqp.qp_num;
20662306a36Sopenharmony_ci	wc.pkey_index = qp->ibqp.qp_type == IB_QPT_GSI ?
20762306a36Sopenharmony_ci		rvt_get_swqe_pkey_index(swqe) : 0;
20862306a36Sopenharmony_ci	wc.slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
20962306a36Sopenharmony_ci				((1 << ppd->lmc) - 1));
21062306a36Sopenharmony_ci	wc.sl = rdma_ah_get_sl(ah_attr);
21162306a36Sopenharmony_ci	wc.dlid_path_bits = rdma_ah_get_dlid(ah_attr) & ((1 << ppd->lmc) - 1);
21262306a36Sopenharmony_ci	wc.port_num = qp->port_num;
21362306a36Sopenharmony_ci	/* Signal completion event if the solicited bit is set. */
21462306a36Sopenharmony_ci	rvt_recv_cq(qp, &wc, swqe->wr.send_flags & IB_SEND_SOLICITED);
21562306a36Sopenharmony_ci	ibp->rvp.n_loop_pkts++;
21662306a36Sopenharmony_cibail_unlock:
21762306a36Sopenharmony_ci	spin_unlock_irqrestore(&qp->r_lock, flags);
21862306a36Sopenharmony_cidrop:
21962306a36Sopenharmony_ci	rcu_read_unlock();
22062306a36Sopenharmony_ci}
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci/**
22362306a36Sopenharmony_ci * qib_make_ud_req - construct a UD request packet
22462306a36Sopenharmony_ci * @qp: the QP
22562306a36Sopenharmony_ci * @flags: flags to modify and pass back to caller
22662306a36Sopenharmony_ci *
22762306a36Sopenharmony_ci * Assumes the s_lock is held.
22862306a36Sopenharmony_ci *
22962306a36Sopenharmony_ci * Return 1 if constructed; otherwise, return 0.
23062306a36Sopenharmony_ci */
23162306a36Sopenharmony_ciint qib_make_ud_req(struct rvt_qp *qp, unsigned long *flags)
23262306a36Sopenharmony_ci{
23362306a36Sopenharmony_ci	struct qib_qp_priv *priv = qp->priv;
23462306a36Sopenharmony_ci	struct ib_other_headers *ohdr;
23562306a36Sopenharmony_ci	struct rdma_ah_attr *ah_attr;
23662306a36Sopenharmony_ci	struct qib_pportdata *ppd;
23762306a36Sopenharmony_ci	struct qib_ibport *ibp;
23862306a36Sopenharmony_ci	struct rvt_swqe *wqe;
23962306a36Sopenharmony_ci	u32 nwords;
24062306a36Sopenharmony_ci	u32 extra_bytes;
24162306a36Sopenharmony_ci	u32 bth0;
24262306a36Sopenharmony_ci	u16 lrh0;
24362306a36Sopenharmony_ci	u16 lid;
24462306a36Sopenharmony_ci	int ret = 0;
24562306a36Sopenharmony_ci	int next_cur;
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
24862306a36Sopenharmony_ci		if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
24962306a36Sopenharmony_ci			goto bail;
25062306a36Sopenharmony_ci		/* We are in the error state, flush the work request. */
25162306a36Sopenharmony_ci		if (qp->s_last == READ_ONCE(qp->s_head))
25262306a36Sopenharmony_ci			goto bail;
25362306a36Sopenharmony_ci		/* If DMAs are in progress, we can't flush immediately. */
25462306a36Sopenharmony_ci		if (atomic_read(&priv->s_dma_busy)) {
25562306a36Sopenharmony_ci			qp->s_flags |= RVT_S_WAIT_DMA;
25662306a36Sopenharmony_ci			goto bail;
25762306a36Sopenharmony_ci		}
25862306a36Sopenharmony_ci		wqe = rvt_get_swqe_ptr(qp, qp->s_last);
25962306a36Sopenharmony_ci		rvt_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
26062306a36Sopenharmony_ci		goto done;
26162306a36Sopenharmony_ci	}
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci	/* see post_one_send() */
26462306a36Sopenharmony_ci	if (qp->s_cur == READ_ONCE(qp->s_head))
26562306a36Sopenharmony_ci		goto bail;
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci	wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
26862306a36Sopenharmony_ci	next_cur = qp->s_cur + 1;
26962306a36Sopenharmony_ci	if (next_cur >= qp->s_size)
27062306a36Sopenharmony_ci		next_cur = 0;
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci	/* Construct the header. */
27362306a36Sopenharmony_ci	ibp = to_iport(qp->ibqp.device, qp->port_num);
27462306a36Sopenharmony_ci	ppd = ppd_from_ibp(ibp);
27562306a36Sopenharmony_ci	ah_attr = rvt_get_swqe_ah_attr(wqe);
27662306a36Sopenharmony_ci	if (rdma_ah_get_dlid(ah_attr) >= be16_to_cpu(IB_MULTICAST_LID_BASE)) {
27762306a36Sopenharmony_ci		if (rdma_ah_get_dlid(ah_attr) !=
27862306a36Sopenharmony_ci				be16_to_cpu(IB_LID_PERMISSIVE))
27962306a36Sopenharmony_ci			this_cpu_inc(ibp->pmastats->n_multicast_xmit);
28062306a36Sopenharmony_ci		else
28162306a36Sopenharmony_ci			this_cpu_inc(ibp->pmastats->n_unicast_xmit);
28262306a36Sopenharmony_ci	} else {
28362306a36Sopenharmony_ci		this_cpu_inc(ibp->pmastats->n_unicast_xmit);
28462306a36Sopenharmony_ci		lid = rdma_ah_get_dlid(ah_attr) & ~((1 << ppd->lmc) - 1);
28562306a36Sopenharmony_ci		if (unlikely(lid == ppd->lid)) {
28662306a36Sopenharmony_ci			unsigned long tflags = *flags;
28762306a36Sopenharmony_ci			/*
28862306a36Sopenharmony_ci			 * If DMAs are in progress, we can't generate
28962306a36Sopenharmony_ci			 * a completion for the loopback packet since
29062306a36Sopenharmony_ci			 * it would be out of order.
29162306a36Sopenharmony_ci			 * XXX Instead of waiting, we could queue a
29262306a36Sopenharmony_ci			 * zero length descriptor so we get a callback.
29362306a36Sopenharmony_ci			 */
29462306a36Sopenharmony_ci			if (atomic_read(&priv->s_dma_busy)) {
29562306a36Sopenharmony_ci				qp->s_flags |= RVT_S_WAIT_DMA;
29662306a36Sopenharmony_ci				goto bail;
29762306a36Sopenharmony_ci			}
29862306a36Sopenharmony_ci			qp->s_cur = next_cur;
29962306a36Sopenharmony_ci			spin_unlock_irqrestore(&qp->s_lock, tflags);
30062306a36Sopenharmony_ci			qib_ud_loopback(qp, wqe);
30162306a36Sopenharmony_ci			spin_lock_irqsave(&qp->s_lock, tflags);
30262306a36Sopenharmony_ci			*flags = tflags;
30362306a36Sopenharmony_ci			rvt_send_complete(qp, wqe, IB_WC_SUCCESS);
30462306a36Sopenharmony_ci			goto done;
30562306a36Sopenharmony_ci		}
30662306a36Sopenharmony_ci	}
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ci	qp->s_cur = next_cur;
30962306a36Sopenharmony_ci	extra_bytes = -wqe->length & 3;
31062306a36Sopenharmony_ci	nwords = (wqe->length + extra_bytes) >> 2;
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci	/* header size in 32-bit words LRH+BTH+DETH = (8+12+8)/4. */
31362306a36Sopenharmony_ci	qp->s_hdrwords = 7;
31462306a36Sopenharmony_ci	qp->s_cur_size = wqe->length;
31562306a36Sopenharmony_ci	qp->s_cur_sge = &qp->s_sge;
31662306a36Sopenharmony_ci	qp->s_srate = rdma_ah_get_static_rate(ah_attr);
31762306a36Sopenharmony_ci	qp->s_wqe = wqe;
31862306a36Sopenharmony_ci	qp->s_sge.sge = wqe->sg_list[0];
31962306a36Sopenharmony_ci	qp->s_sge.sg_list = wqe->sg_list + 1;
32062306a36Sopenharmony_ci	qp->s_sge.num_sge = wqe->wr.num_sge;
32162306a36Sopenharmony_ci	qp->s_sge.total_len = wqe->length;
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
32462306a36Sopenharmony_ci		/* Header size in 32-bit words. */
32562306a36Sopenharmony_ci		qp->s_hdrwords += qib_make_grh(ibp, &priv->s_hdr->u.l.grh,
32662306a36Sopenharmony_ci					       rdma_ah_read_grh(ah_attr),
32762306a36Sopenharmony_ci					       qp->s_hdrwords, nwords);
32862306a36Sopenharmony_ci		lrh0 = QIB_LRH_GRH;
32962306a36Sopenharmony_ci		ohdr = &priv->s_hdr->u.l.oth;
33062306a36Sopenharmony_ci		/*
33162306a36Sopenharmony_ci		 * Don't worry about sending to locally attached multicast
33262306a36Sopenharmony_ci		 * QPs.  It is unspecified by the spec. what happens.
33362306a36Sopenharmony_ci		 */
33462306a36Sopenharmony_ci	} else {
33562306a36Sopenharmony_ci		/* Header size in 32-bit words. */
33662306a36Sopenharmony_ci		lrh0 = QIB_LRH_BTH;
33762306a36Sopenharmony_ci		ohdr = &priv->s_hdr->u.oth;
33862306a36Sopenharmony_ci	}
33962306a36Sopenharmony_ci	if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
34062306a36Sopenharmony_ci		qp->s_hdrwords++;
34162306a36Sopenharmony_ci		ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
34262306a36Sopenharmony_ci		bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
34362306a36Sopenharmony_ci	} else
34462306a36Sopenharmony_ci		bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
34562306a36Sopenharmony_ci	lrh0 |= rdma_ah_get_sl(ah_attr) << 4;
34662306a36Sopenharmony_ci	if (qp->ibqp.qp_type == IB_QPT_SMI)
34762306a36Sopenharmony_ci		lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
34862306a36Sopenharmony_ci	else
34962306a36Sopenharmony_ci		lrh0 |= ibp->sl_to_vl[rdma_ah_get_sl(ah_attr)] << 12;
35062306a36Sopenharmony_ci	priv->s_hdr->lrh[0] = cpu_to_be16(lrh0);
35162306a36Sopenharmony_ci	priv->s_hdr->lrh[1] =
35262306a36Sopenharmony_ci			cpu_to_be16(rdma_ah_get_dlid(ah_attr));  /* DEST LID */
35362306a36Sopenharmony_ci	priv->s_hdr->lrh[2] =
35462306a36Sopenharmony_ci			cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
35562306a36Sopenharmony_ci	lid = ppd->lid;
35662306a36Sopenharmony_ci	if (lid) {
35762306a36Sopenharmony_ci		lid |= rdma_ah_get_path_bits(ah_attr) &
35862306a36Sopenharmony_ci			((1 << ppd->lmc) - 1);
35962306a36Sopenharmony_ci		priv->s_hdr->lrh[3] = cpu_to_be16(lid);
36062306a36Sopenharmony_ci	} else
36162306a36Sopenharmony_ci		priv->s_hdr->lrh[3] = IB_LID_PERMISSIVE;
36262306a36Sopenharmony_ci	if (wqe->wr.send_flags & IB_SEND_SOLICITED)
36362306a36Sopenharmony_ci		bth0 |= IB_BTH_SOLICITED;
36462306a36Sopenharmony_ci	bth0 |= extra_bytes << 20;
36562306a36Sopenharmony_ci	bth0 |= qp->ibqp.qp_type == IB_QPT_SMI ? QIB_DEFAULT_P_KEY :
36662306a36Sopenharmony_ci		qib_get_pkey(ibp, qp->ibqp.qp_type == IB_QPT_GSI ?
36762306a36Sopenharmony_ci			     rvt_get_swqe_pkey_index(wqe) : qp->s_pkey_index);
36862306a36Sopenharmony_ci	ohdr->bth[0] = cpu_to_be32(bth0);
36962306a36Sopenharmony_ci	/*
37062306a36Sopenharmony_ci	 * Use the multicast QP if the destination LID is a multicast LID.
37162306a36Sopenharmony_ci	 */
37262306a36Sopenharmony_ci	ohdr->bth[1] = rdma_ah_get_dlid(ah_attr) >=
37362306a36Sopenharmony_ci			be16_to_cpu(IB_MULTICAST_LID_BASE) &&
37462306a36Sopenharmony_ci		rdma_ah_get_dlid(ah_attr) != be16_to_cpu(IB_LID_PERMISSIVE) ?
37562306a36Sopenharmony_ci		cpu_to_be32(QIB_MULTICAST_QPN) :
37662306a36Sopenharmony_ci		cpu_to_be32(rvt_get_swqe_remote_qpn(wqe));
37762306a36Sopenharmony_ci	ohdr->bth[2] = cpu_to_be32(wqe->psn & QIB_PSN_MASK);
37862306a36Sopenharmony_ci	/*
37962306a36Sopenharmony_ci	 * Qkeys with the high order bit set mean use the
38062306a36Sopenharmony_ci	 * qkey from the QP context instead of the WR (see 10.2.5).
38162306a36Sopenharmony_ci	 */
38262306a36Sopenharmony_ci	ohdr->u.ud.deth[0] =
38362306a36Sopenharmony_ci		cpu_to_be32((int)rvt_get_swqe_remote_qkey(wqe) < 0 ? qp->qkey :
38462306a36Sopenharmony_ci			    rvt_get_swqe_remote_qkey(wqe));
38562306a36Sopenharmony_ci	ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_cidone:
38862306a36Sopenharmony_ci	return 1;
38962306a36Sopenharmony_cibail:
39062306a36Sopenharmony_ci	qp->s_flags &= ~RVT_S_BUSY;
39162306a36Sopenharmony_ci	return ret;
39262306a36Sopenharmony_ci}
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_cistatic unsigned qib_lookup_pkey(struct qib_ibport *ibp, u16 pkey)
39562306a36Sopenharmony_ci{
39662306a36Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
39762306a36Sopenharmony_ci	struct qib_devdata *dd = ppd->dd;
39862306a36Sopenharmony_ci	unsigned ctxt = ppd->hw_pidx;
39962306a36Sopenharmony_ci	unsigned i;
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci	pkey &= 0x7fff;	/* remove limited/full membership bit */
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(dd->rcd[ctxt]->pkeys); ++i)
40462306a36Sopenharmony_ci		if ((dd->rcd[ctxt]->pkeys[i] & 0x7fff) == pkey)
40562306a36Sopenharmony_ci			return i;
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_ci	/*
40862306a36Sopenharmony_ci	 * Should not get here, this means hardware failed to validate pkeys.
40962306a36Sopenharmony_ci	 * Punt and return index 0.
41062306a36Sopenharmony_ci	 */
41162306a36Sopenharmony_ci	return 0;
41262306a36Sopenharmony_ci}
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ci/**
41562306a36Sopenharmony_ci * qib_ud_rcv - receive an incoming UD packet
41662306a36Sopenharmony_ci * @ibp: the port the packet came in on
41762306a36Sopenharmony_ci * @hdr: the packet header
41862306a36Sopenharmony_ci * @has_grh: true if the packet has a GRH
41962306a36Sopenharmony_ci * @data: the packet data
42062306a36Sopenharmony_ci * @tlen: the packet length
42162306a36Sopenharmony_ci * @qp: the QP the packet came on
42262306a36Sopenharmony_ci *
42362306a36Sopenharmony_ci * This is called from qib_qp_rcv() to process an incoming UD packet
42462306a36Sopenharmony_ci * for the given QP.
42562306a36Sopenharmony_ci * Called at interrupt level.
42662306a36Sopenharmony_ci */
42762306a36Sopenharmony_civoid qib_ud_rcv(struct qib_ibport *ibp, struct ib_header *hdr,
42862306a36Sopenharmony_ci		int has_grh, void *data, u32 tlen, struct rvt_qp *qp)
42962306a36Sopenharmony_ci{
43062306a36Sopenharmony_ci	struct ib_other_headers *ohdr;
43162306a36Sopenharmony_ci	int opcode;
43262306a36Sopenharmony_ci	u32 hdrsize;
43362306a36Sopenharmony_ci	u32 pad;
43462306a36Sopenharmony_ci	struct ib_wc wc;
43562306a36Sopenharmony_ci	u32 qkey;
43662306a36Sopenharmony_ci	u32 src_qp;
43762306a36Sopenharmony_ci	u16 dlid;
43862306a36Sopenharmony_ci
43962306a36Sopenharmony_ci	/* Check for GRH */
44062306a36Sopenharmony_ci	if (!has_grh) {
44162306a36Sopenharmony_ci		ohdr = &hdr->u.oth;
44262306a36Sopenharmony_ci		hdrsize = 8 + 12 + 8;   /* LRH + BTH + DETH */
44362306a36Sopenharmony_ci	} else {
44462306a36Sopenharmony_ci		ohdr = &hdr->u.l.oth;
44562306a36Sopenharmony_ci		hdrsize = 8 + 40 + 12 + 8; /* LRH + GRH + BTH + DETH */
44662306a36Sopenharmony_ci	}
44762306a36Sopenharmony_ci	qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
44862306a36Sopenharmony_ci	src_qp = be32_to_cpu(ohdr->u.ud.deth[1]) & RVT_QPN_MASK;
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_ci	/*
45162306a36Sopenharmony_ci	 * Get the number of bytes the message was padded by
45262306a36Sopenharmony_ci	 * and drop incomplete packets.
45362306a36Sopenharmony_ci	 */
45462306a36Sopenharmony_ci	pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
45562306a36Sopenharmony_ci	if (unlikely(tlen < (hdrsize + pad + 4)))
45662306a36Sopenharmony_ci		goto drop;
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_ci	tlen -= hdrsize + pad + 4;
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci	/*
46162306a36Sopenharmony_ci	 * Check that the permissive LID is only used on QP0
46262306a36Sopenharmony_ci	 * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
46362306a36Sopenharmony_ci	 */
46462306a36Sopenharmony_ci	if (qp->ibqp.qp_num) {
46562306a36Sopenharmony_ci		if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||
46662306a36Sopenharmony_ci			     hdr->lrh[3] == IB_LID_PERMISSIVE))
46762306a36Sopenharmony_ci			goto drop;
46862306a36Sopenharmony_ci		if (qp->ibqp.qp_num > 1) {
46962306a36Sopenharmony_ci			u16 pkey1, pkey2;
47062306a36Sopenharmony_ci
47162306a36Sopenharmony_ci			pkey1 = be32_to_cpu(ohdr->bth[0]);
47262306a36Sopenharmony_ci			pkey2 = qib_get_pkey(ibp, qp->s_pkey_index);
47362306a36Sopenharmony_ci			if (unlikely(!qib_pkey_ok(pkey1, pkey2))) {
47462306a36Sopenharmony_ci				qib_bad_pkey(ibp,
47562306a36Sopenharmony_ci					     pkey1,
47662306a36Sopenharmony_ci					     (be16_to_cpu(hdr->lrh[0]) >> 4) &
47762306a36Sopenharmony_ci						0xF,
47862306a36Sopenharmony_ci					     src_qp, qp->ibqp.qp_num,
47962306a36Sopenharmony_ci					     hdr->lrh[3], hdr->lrh[1]);
48062306a36Sopenharmony_ci				return;
48162306a36Sopenharmony_ci			}
48262306a36Sopenharmony_ci		}
48362306a36Sopenharmony_ci		if (unlikely(qkey != qp->qkey))
48462306a36Sopenharmony_ci			return;
48562306a36Sopenharmony_ci
48662306a36Sopenharmony_ci		/* Drop invalid MAD packets (see 13.5.3.1). */
48762306a36Sopenharmony_ci		if (unlikely(qp->ibqp.qp_num == 1 &&
48862306a36Sopenharmony_ci			     (tlen != 256 ||
48962306a36Sopenharmony_ci			      (be16_to_cpu(hdr->lrh[0]) >> 12) == 15)))
49062306a36Sopenharmony_ci			goto drop;
49162306a36Sopenharmony_ci	} else {
49262306a36Sopenharmony_ci		struct ib_smp *smp;
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_ci		/* Drop invalid MAD packets (see 13.5.3.1). */
49562306a36Sopenharmony_ci		if (tlen != 256 || (be16_to_cpu(hdr->lrh[0]) >> 12) != 15)
49662306a36Sopenharmony_ci			goto drop;
49762306a36Sopenharmony_ci		smp = (struct ib_smp *) data;
49862306a36Sopenharmony_ci		if ((hdr->lrh[1] == IB_LID_PERMISSIVE ||
49962306a36Sopenharmony_ci		     hdr->lrh[3] == IB_LID_PERMISSIVE) &&
50062306a36Sopenharmony_ci		    smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
50162306a36Sopenharmony_ci			goto drop;
50262306a36Sopenharmony_ci	}
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_ci	/*
50562306a36Sopenharmony_ci	 * The opcode is in the low byte when its in network order
50662306a36Sopenharmony_ci	 * (top byte when in host order).
50762306a36Sopenharmony_ci	 */
50862306a36Sopenharmony_ci	opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
50962306a36Sopenharmony_ci	if (qp->ibqp.qp_num > 1 &&
51062306a36Sopenharmony_ci	    opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
51162306a36Sopenharmony_ci		wc.ex.imm_data = ohdr->u.ud.imm_data;
51262306a36Sopenharmony_ci		wc.wc_flags = IB_WC_WITH_IMM;
51362306a36Sopenharmony_ci	} else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
51462306a36Sopenharmony_ci		wc.ex.imm_data = 0;
51562306a36Sopenharmony_ci		wc.wc_flags = 0;
51662306a36Sopenharmony_ci	} else
51762306a36Sopenharmony_ci		goto drop;
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci	/*
52062306a36Sopenharmony_ci	 * A GRH is expected to precede the data even if not
52162306a36Sopenharmony_ci	 * present on the wire.
52262306a36Sopenharmony_ci	 */
52362306a36Sopenharmony_ci	wc.byte_len = tlen + sizeof(struct ib_grh);
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci	/*
52662306a36Sopenharmony_ci	 * Get the next work request entry to find where to put the data.
52762306a36Sopenharmony_ci	 */
52862306a36Sopenharmony_ci	if (qp->r_flags & RVT_R_REUSE_SGE)
52962306a36Sopenharmony_ci		qp->r_flags &= ~RVT_R_REUSE_SGE;
53062306a36Sopenharmony_ci	else {
53162306a36Sopenharmony_ci		int ret;
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_ci		ret = rvt_get_rwqe(qp, false);
53462306a36Sopenharmony_ci		if (ret < 0) {
53562306a36Sopenharmony_ci			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
53662306a36Sopenharmony_ci			return;
53762306a36Sopenharmony_ci		}
53862306a36Sopenharmony_ci		if (!ret) {
53962306a36Sopenharmony_ci			if (qp->ibqp.qp_num == 0)
54062306a36Sopenharmony_ci				ibp->rvp.n_vl15_dropped++;
54162306a36Sopenharmony_ci			return;
54262306a36Sopenharmony_ci		}
54362306a36Sopenharmony_ci	}
54462306a36Sopenharmony_ci	/* Silently drop packets which are too big. */
54562306a36Sopenharmony_ci	if (unlikely(wc.byte_len > qp->r_len)) {
54662306a36Sopenharmony_ci		qp->r_flags |= RVT_R_REUSE_SGE;
54762306a36Sopenharmony_ci		goto drop;
54862306a36Sopenharmony_ci	}
54962306a36Sopenharmony_ci	if (has_grh) {
55062306a36Sopenharmony_ci		rvt_copy_sge(qp, &qp->r_sge, &hdr->u.l.grh,
55162306a36Sopenharmony_ci			     sizeof(struct ib_grh), true, false);
55262306a36Sopenharmony_ci		wc.wc_flags |= IB_WC_GRH;
55362306a36Sopenharmony_ci	} else
55462306a36Sopenharmony_ci		rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
55562306a36Sopenharmony_ci	rvt_copy_sge(qp, &qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh),
55662306a36Sopenharmony_ci		     true, false);
55762306a36Sopenharmony_ci	rvt_put_ss(&qp->r_sge);
55862306a36Sopenharmony_ci	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
55962306a36Sopenharmony_ci		return;
56062306a36Sopenharmony_ci	wc.wr_id = qp->r_wr_id;
56162306a36Sopenharmony_ci	wc.status = IB_WC_SUCCESS;
56262306a36Sopenharmony_ci	wc.opcode = IB_WC_RECV;
56362306a36Sopenharmony_ci	wc.vendor_err = 0;
56462306a36Sopenharmony_ci	wc.qp = &qp->ibqp;
56562306a36Sopenharmony_ci	wc.src_qp = src_qp;
56662306a36Sopenharmony_ci	wc.pkey_index = qp->ibqp.qp_type == IB_QPT_GSI ?
56762306a36Sopenharmony_ci		qib_lookup_pkey(ibp, be32_to_cpu(ohdr->bth[0])) : 0;
56862306a36Sopenharmony_ci	wc.slid = be16_to_cpu(hdr->lrh[3]);
56962306a36Sopenharmony_ci	wc.sl = (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF;
57062306a36Sopenharmony_ci	dlid = be16_to_cpu(hdr->lrh[1]);
57162306a36Sopenharmony_ci	/*
57262306a36Sopenharmony_ci	 * Save the LMC lower bits if the destination LID is a unicast LID.
57362306a36Sopenharmony_ci	 */
57462306a36Sopenharmony_ci	wc.dlid_path_bits = dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE) ? 0 :
57562306a36Sopenharmony_ci		dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
57662306a36Sopenharmony_ci	wc.port_num = qp->port_num;
57762306a36Sopenharmony_ci	/* Signal completion event if the solicited bit is set. */
57862306a36Sopenharmony_ci	rvt_recv_cq(qp, &wc, ib_bth_is_solicited(ohdr));
57962306a36Sopenharmony_ci	return;
58062306a36Sopenharmony_ci
58162306a36Sopenharmony_cidrop:
58262306a36Sopenharmony_ci	ibp->rvp.n_pkt_drops++;
58362306a36Sopenharmony_ci}
584