ud.c 29.7 KB
Newer Older
M
Mike Marciniszyn 已提交
1
/*
J
Jubin John 已提交
2
 * Copyright(c) 2015, 2016 Intel Corporation.
M
Mike Marciniszyn 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
 *
 * This file is provided under a dual BSD/GPLv2 license.  When using or
 * redistributing this file, you may do so under either license.
 *
 * GPL LICENSE SUMMARY
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * BSD LICENSE
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  - Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  - Neither the name of Intel Corporation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <linux/net.h>
#include <rdma/ib_smi.h>

#include "hfi.h"
#include "mad.h"
53
#include "verbs_txreq.h"
54
#include "qp.h"
M
Mike Marciniszyn 已提交
55

D
Don Hiatt 已提交
56 57 58 59 60 61
/* We support only two types - 9B and 16B for now */
static const hfi1_make_req hfi1_make_ud_req_tbl[2] = {
	[HFI1_PKT_TYPE_9B] = &hfi1_make_ud_req_9B,
	[HFI1_PKT_TYPE_16B] = &hfi1_make_ud_req_16B
};

M
Mike Marciniszyn 已提交
62 63 64 65 66 67 68 69 70 71
/**
 * ud_loopback - handle send on loopback QPs
 * @sqp: the sending QP
 * @swqe: the send work request
 *
 * This is called from hfi1_make_ud_req() to forward a WQE addressed
 * to the same HFI.
 * Note that the receive interrupt handler may be calling hfi1_ud_rcv()
 * while this is being called.
 */
72
static void ud_loopback(struct rvt_qp *sqp, struct rvt_swqe *swqe)
M
Mike Marciniszyn 已提交
73 74 75
{
	struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
	struct hfi1_pportdata *ppd;
D
Don Hiatt 已提交
76
	struct hfi1_qp_priv *priv = sqp->priv;
77
	struct rvt_qp *qp;
78
	struct rdma_ah_attr *ah_attr;
M
Mike Marciniszyn 已提交
79
	unsigned long flags;
80 81
	struct rvt_sge_state ssge;
	struct rvt_sge *sge;
M
Mike Marciniszyn 已提交
82 83 84 85 86 87
	struct ib_wc wc;
	u32 length;
	enum ib_qp_type sqptype, dqptype;

	rcu_read_lock();

88 89
	qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
			    swqe->ud_wr.remote_qpn);
M
Mike Marciniszyn 已提交
90
	if (!qp) {
91
		ibp->rvp.n_pkt_drops++;
M
Mike Marciniszyn 已提交
92 93 94 95 96 97 98 99 100 101
		rcu_read_unlock();
		return;
	}

	sqptype = sqp->ibqp.qp_type == IB_QPT_GSI ?
			IB_QPT_UD : sqp->ibqp.qp_type;
	dqptype = qp->ibqp.qp_type == IB_QPT_GSI ?
			IB_QPT_UD : qp->ibqp.qp_type;

	if (dqptype != sqptype ||
102
	    !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
103
		ibp->rvp.n_pkt_drops++;
M
Mike Marciniszyn 已提交
104 105 106
		goto drop;
	}

107
	ah_attr = &ibah_to_rvtah(swqe->ud_wr.ah)->attr;
M
Mike Marciniszyn 已提交
108 109 110 111
	ppd = ppd_from_ibp(ibp);

	if (qp->ibqp.qp_num > 1) {
		u16 pkey;
D
Don Hiatt 已提交
112
		u32 slid;
113
		u8 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
M
Mike Marciniszyn 已提交
114 115

		pkey = hfi1_get_pkey(ibp, sqp->s_pkey_index);
116
		slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
M
Mike Marciniszyn 已提交
117 118
				   ((1 << ppd->lmc) - 1));
		if (unlikely(ingress_pkey_check(ppd, pkey, sc5,
119 120
						qp->s_pkey_index,
						slid, false))) {
121 122 123 124
			hfi1_bad_pkey(ibp, pkey,
				      rdma_ah_get_sl(ah_attr),
				      sqp->ibqp.qp_num, qp->ibqp.qp_num,
				      slid, rdma_ah_get_dlid(ah_attr));
M
Mike Marciniszyn 已提交
125 126 127 128 129 130 131 132 133 134 135 136
			goto drop;
		}
	}

	/*
	 * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
	 * Qkeys with the high order bit set mean use the
	 * qkey from the QP context instead of the WR (see 10.2.5).
	 */
	if (qp->ibqp.qp_num) {
		u32 qkey;

C
Christoph Hellwig 已提交
137 138
		qkey = (int)swqe->ud_wr.remote_qkey < 0 ?
			sqp->qkey : swqe->ud_wr.remote_qkey;
139 140
		if (unlikely(qkey != qp->qkey))
			goto drop; /* silently drop per IBTA spec */
M
Mike Marciniszyn 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	}

	/*
	 * A GRH is expected to precede the data even if not
	 * present on the wire.
	 */
	length = swqe->length;
	memset(&wc, 0, sizeof(wc));
	wc.byte_len = length + sizeof(struct ib_grh);

	if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
		wc.wc_flags = IB_WC_WITH_IMM;
		wc.ex.imm_data = swqe->wr.ex.imm_data;
	}

	spin_lock_irqsave(&qp->r_lock, flags);

	/*
	 * Get the next work request entry to find where to put the data.
	 */
161
	if (qp->r_flags & RVT_R_REUSE_SGE) {
162
		qp->r_flags &= ~RVT_R_REUSE_SGE;
163
	} else {
M
Mike Marciniszyn 已提交
164 165
		int ret;

166
		ret = hfi1_rvt_get_rwqe(qp, 0);
M
Mike Marciniszyn 已提交
167
		if (ret < 0) {
168
			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
M
Mike Marciniszyn 已提交
169 170 171 172
			goto bail_unlock;
		}
		if (!ret) {
			if (qp->ibqp.qp_num == 0)
173
				ibp->rvp.n_vl15_dropped++;
M
Mike Marciniszyn 已提交
174 175 176 177 178
			goto bail_unlock;
		}
	}
	/* Silently drop packets which are too big. */
	if (unlikely(wc.byte_len > qp->r_len)) {
179
		qp->r_flags |= RVT_R_REUSE_SGE;
180
		ibp->rvp.n_pkt_drops++;
M
Mike Marciniszyn 已提交
181 182 183
		goto bail_unlock;
	}

184
	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
185
		struct ib_grh grh;
D
Don Hiatt 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
		struct ib_global_route grd = *(rdma_ah_read_grh(ah_attr));

		/*
		 * For loopback packets with extended LIDs, the
		 * sgid_index in the GRH is 0 and the dgid is
		 * OPA GID of the sender. While creating a response
		 * to the loopback packet, IB core creates the new
		 * sgid_index from the DGID and that will be the
		 * OPA_GID_INDEX. The new dgid is from the sgid
		 * index and that will be in the IB GID format.
		 *
		 * We now have a case where the sent packet had a
		 * different sgid_index and dgid compared to the
		 * one that was received in response.
		 *
		 * Fix this inconsistency.
		 */
		if (priv->hdr_type == HFI1_PKT_TYPE_16B) {
			if (grd.sgid_index == 0)
				grd.sgid_index = OPA_GID_INDEX;
206

D
Don Hiatt 已提交
207 208 209 210 211 212
			if (ib_is_opa_gid(&grd.dgid))
				grd.dgid.global.interface_id =
				cpu_to_be64(ppd->guids[HFI1_PORT_GUID_INDEX]);
		}

		hfi1_make_grh(ibp, &grh, &grd, 0, 0);
213
		hfi1_copy_sge(&qp->r_sge, &grh,
214
			      sizeof(grh), true, false);
M
Mike Marciniszyn 已提交
215
		wc.wc_flags |= IB_WC_GRH;
216
	} else {
217
		rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
218
	}
M
Mike Marciniszyn 已提交
219 220 221 222 223 224 225 226 227 228 229 230
	ssge.sg_list = swqe->sg_list + 1;
	ssge.sge = *swqe->sg_list;
	ssge.num_sge = swqe->wr.num_sge;
	sge = &ssge.sge;
	while (length) {
		u32 len = sge->length;

		if (len > length)
			len = length;
		if (len > sge->sge_length)
			len = sge->sge_length;
		WARN_ON_ONCE(len == 0);
231
		hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, true, false);
M
Mike Marciniszyn 已提交
232 233 234 235 236 237 238
		sge->vaddr += len;
		sge->length -= len;
		sge->sge_length -= len;
		if (sge->sge_length == 0) {
			if (--ssge.num_sge)
				*sge = *ssge.sg_list++;
		} else if (sge->length == 0 && sge->mr->lkey) {
239
			if (++sge->n >= RVT_SEGSZ) {
M
Mike Marciniszyn 已提交
240 241 242 243 244 245 246 247 248 249 250
				if (++sge->m >= sge->mr->mapsz)
					break;
				sge->n = 0;
			}
			sge->vaddr =
				sge->mr->map[sge->m]->segs[sge->n].vaddr;
			sge->length =
				sge->mr->map[sge->m]->segs[sge->n].length;
		}
		length -= len;
	}
251
	rvt_put_ss(&qp->r_sge);
252
	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
M
Mike Marciniszyn 已提交
253 254 255 256 257 258 259 260 261
		goto bail_unlock;
	wc.wr_id = qp->r_wr_id;
	wc.status = IB_WC_SUCCESS;
	wc.opcode = IB_WC_RECV;
	wc.qp = &qp->ibqp;
	wc.src_qp = sqp->ibqp.qp_num;
	if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI) {
		if (sqp->ibqp.qp_type == IB_QPT_GSI ||
		    sqp->ibqp.qp_type == IB_QPT_SMI)
C
Christoph Hellwig 已提交
262
			wc.pkey_index = swqe->ud_wr.pkey_index;
M
Mike Marciniszyn 已提交
263 264 265 266 267
		else
			wc.pkey_index = sqp->s_pkey_index;
	} else {
		wc.pkey_index = 0;
	}
268 269
	wc.slid = (ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
				   ((1 << ppd->lmc) - 1))) & U16_MAX;
M
Mike Marciniszyn 已提交
270 271
	/* Check for loopback when the port lid is not set */
	if (wc.slid == 0 && sqp->ibqp.qp_type == IB_QPT_GSI)
272
		wc.slid = be16_to_cpu(IB_LID_PERMISSIVE);
273 274
	wc.sl = rdma_ah_get_sl(ah_attr);
	wc.dlid_path_bits = rdma_ah_get_dlid(ah_attr) & ((1 << ppd->lmc) - 1);
M
Mike Marciniszyn 已提交
275 276
	wc.port_num = qp->port_num;
	/* Signal completion event if the solicited bit is set. */
277 278
	rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
		     swqe->wr.send_flags & IB_SEND_SOLICITED);
279
	ibp->rvp.n_loop_pkts++;
M
Mike Marciniszyn 已提交
280 281 282 283 284 285
bail_unlock:
	spin_unlock_irqrestore(&qp->r_lock, flags);
drop:
	rcu_read_unlock();
}

D
Don Hiatt 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
static void hfi1_make_bth_deth(struct rvt_qp *qp, struct rvt_swqe *wqe,
			       struct ib_other_headers *ohdr,
			       u16 *pkey, u32 extra_bytes, bool bypass)
{
	u32 bth0;
	struct hfi1_ibport *ibp;

	ibp = to_iport(qp->ibqp.device, qp->port_num);
	if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
		ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
		bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
	} else {
		bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
	}

	if (wqe->wr.send_flags & IB_SEND_SOLICITED)
		bth0 |= IB_BTH_SOLICITED;
	bth0 |= extra_bytes << 20;
	if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI)
		*pkey = hfi1_get_pkey(ibp, wqe->ud_wr.pkey_index);
	else
		*pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
	if (!bypass)
		bth0 |= *pkey;
	ohdr->bth[0] = cpu_to_be32(bth0);
	ohdr->bth[1] = cpu_to_be32(wqe->ud_wr.remote_qpn);
	ohdr->bth[2] = cpu_to_be32(mask_psn(wqe->psn));
	/*
	 * Qkeys with the high order bit set mean use the
	 * qkey from the QP context instead of the WR (see 10.2.5).
	 */
	ohdr->u.ud.deth[0] = cpu_to_be32((int)wqe->ud_wr.remote_qkey < 0 ?
					 qp->qkey : wqe->ud_wr.remote_qkey);
	ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
}

void hfi1_make_ud_req_9B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
			 struct rvt_swqe *wqe)
{
	u32 nwords, extra_bytes;
	u16 len, slid, dlid, pkey;
	u16 lrh0 = 0;
	u8 sc5;
	struct hfi1_qp_priv *priv = qp->priv;
	struct ib_other_headers *ohdr;
	struct rdma_ah_attr *ah_attr;
	struct hfi1_pportdata *ppd;
	struct hfi1_ibport *ibp;
	struct ib_grh *grh;

	ibp = to_iport(qp->ibqp.device, qp->port_num);
	ppd = ppd_from_ibp(ibp);
	ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;

	extra_bytes = -wqe->length & 3;
	nwords = ((wqe->length + extra_bytes) >> 2) + SIZE_OF_CRC;
	/* header size in dwords LRH+BTH+DETH = (8+12+8)/4. */
	qp->s_hdrwords = 7;
	if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
		qp->s_hdrwords++;

	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
		grh = &ps->s_txreq->phdr.hdr.ibh.u.l.grh;
		qp->s_hdrwords += hfi1_make_grh(ibp, grh,
						rdma_ah_read_grh(ah_attr),
						qp->s_hdrwords - 2, nwords);
		lrh0 = HFI1_LRH_GRH;
		ohdr = &ps->s_txreq->phdr.hdr.ibh.u.l.oth;
	} else {
		lrh0 = HFI1_LRH_BTH;
		ohdr = &ps->s_txreq->phdr.hdr.ibh.u.oth;
	}

	sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
	lrh0 |= (rdma_ah_get_sl(ah_attr) & 0xf) << 4;
	if (qp->ibqp.qp_type == IB_QPT_SMI) {
		lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
		priv->s_sc = 0xf;
	} else {
		lrh0 |= (sc5 & 0xf) << 12;
		priv->s_sc = sc5;
	}

	dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 9B);
	if (dlid == be16_to_cpu(IB_LID_PERMISSIVE)) {
		slid = be16_to_cpu(IB_LID_PERMISSIVE);
	} else {
		u16 lid = (u16)ppd->lid;

		if (lid) {
			lid |= rdma_ah_get_path_bits(ah_attr) &
				((1 << ppd->lmc) - 1);
			slid = lid;
		} else {
			slid = be16_to_cpu(IB_LID_PERMISSIVE);
		}
	}
	hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, false);
	len = qp->s_hdrwords + nwords;

	/* Setup the packet */
	ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_9B;
	hfi1_make_ib_hdr(&ps->s_txreq->phdr.hdr.ibh,
			 lrh0, len, dlid, slid);
}

void hfi1_make_ud_req_16B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
			  struct rvt_swqe *wqe)
{
	struct hfi1_qp_priv *priv = qp->priv;
	struct ib_other_headers *ohdr;
	struct rdma_ah_attr *ah_attr;
	struct hfi1_pportdata *ppd;
	struct hfi1_ibport *ibp;
	u32 dlid, slid, nwords, extra_bytes;
	u16 len, pkey;
	u8 l4, sc5;

	ibp = to_iport(qp->ibqp.device, qp->port_num);
	ppd = ppd_from_ibp(ibp);
	ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
	/* header size in dwords 16B LRH+BTH+DETH = (16+12+8)/4. */
	qp->s_hdrwords = 9;
	if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
		qp->s_hdrwords++;

	/* SW provides space for CRC and LT for bypass packets. */
	extra_bytes = hfi1_get_16b_padding((qp->s_hdrwords << 2),
					   wqe->length);
	nwords = ((wqe->length + extra_bytes + SIZE_OF_LT) >> 2) + SIZE_OF_CRC;

	if ((rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) &&
	    hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) {
		struct ib_grh *grh;
		struct ib_global_route *grd = rdma_ah_retrieve_grh(ah_attr);
		/*
		 * Ensure OPA GIDs are transformed to IB gids
		 * before creating the GRH.
		 */
		if (grd->sgid_index == OPA_GID_INDEX) {
			dd_dev_warn(ppd->dd, "Bad sgid_index. sgid_index: %d\n",
				    grd->sgid_index);
			grd->sgid_index = 0;
		}
		grh = &ps->s_txreq->phdr.hdr.opah.u.l.grh;
		qp->s_hdrwords += hfi1_make_grh(ibp, grh, grd,
					qp->s_hdrwords - 4, nwords);
		ohdr = &ps->s_txreq->phdr.hdr.opah.u.l.oth;
		l4 = OPA_16B_L4_IB_GLOBAL;
	} else {
		ohdr = &ps->s_txreq->phdr.hdr.opah.u.oth;
		l4 = OPA_16B_L4_IB_LOCAL;
	}

	sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
	if (qp->ibqp.qp_type == IB_QPT_SMI)
		priv->s_sc = 0xf;
	else
		priv->s_sc = sc5;

	dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 16B);
	if (!ppd->lid)
		slid = be32_to_cpu(OPA_LID_PERMISSIVE);
	else
		slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
			   ((1 << ppd->lmc) - 1));

	hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, true);
	/* Convert dwords to flits */
	len = (qp->s_hdrwords + nwords) >> 1;

	/* Setup the packet */
	ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_16B;
	hfi1_make_16b_hdr(&ps->s_txreq->phdr.hdr.opah,
			  slid, dlid, len, pkey, 0, 0, l4, priv->s_sc);
}

M
Mike Marciniszyn 已提交
463 464 465 466
/**
 * hfi1_make_ud_req - construct a UD request packet
 * @qp: the QP
 *
467 468
 * Assume s_lock is held.
 *
M
Mike Marciniszyn 已提交
469 470
 * Return 1 if constructed; otherwise, return 0.
 */
471
int hfi1_make_ud_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
M
Mike Marciniszyn 已提交
472
{
473
	struct hfi1_qp_priv *priv = qp->priv;
474
	struct rdma_ah_attr *ah_attr;
M
Mike Marciniszyn 已提交
475 476
	struct hfi1_pportdata *ppd;
	struct hfi1_ibport *ibp;
477
	struct rvt_swqe *wqe;
M
Mike Marciniszyn 已提交
478
	int next_cur;
D
Don Hiatt 已提交
479
	u32 lid;
M
Mike Marciniszyn 已提交
480

481 482 483 484
	ps->s_txreq = get_txreq(ps->dev, qp);
	if (IS_ERR(ps->s_txreq))
		goto bail_no_tx;

485 486
	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
		if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
M
Mike Marciniszyn 已提交
487 488
			goto bail;
		/* We are in the error state, flush the work request. */
489
		smp_read_barrier_depends(); /* see post_one_send */
490
		if (qp->s_last == READ_ONCE(qp->s_head))
M
Mike Marciniszyn 已提交
491 492
			goto bail;
		/* If DMAs are in progress, we can't flush immediately. */
493
		if (iowait_sdma_pending(&priv->s_iowait)) {
494
			qp->s_flags |= RVT_S_WAIT_DMA;
M
Mike Marciniszyn 已提交
495 496
			goto bail;
		}
497
		wqe = rvt_get_swqe_ptr(qp, qp->s_last);
M
Mike Marciniszyn 已提交
498
		hfi1_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
499
		goto done_free_tx;
M
Mike Marciniszyn 已提交
500 501
	}

502 503
	/* see post_one_send() */
	smp_read_barrier_depends();
504
	if (qp->s_cur == READ_ONCE(qp->s_head))
M
Mike Marciniszyn 已提交
505 506
		goto bail;

507
	wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
M
Mike Marciniszyn 已提交
508 509 510 511 512 513 514
	next_cur = qp->s_cur + 1;
	if (next_cur >= qp->s_size)
		next_cur = 0;

	/* Construct the header. */
	ibp = to_iport(qp->ibqp.device, qp->port_num);
	ppd = ppd_from_ibp(ibp);
515
	ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
D
Don Hiatt 已提交
516 517 518
	priv->hdr_type = hfi1_get_hdr_type(ppd->lid, ah_attr);
	if ((!hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) ||
	    (rdma_ah_get_dlid(ah_attr) == be32_to_cpu(OPA_LID_PERMISSIVE))) {
519
		lid = rdma_ah_get_dlid(ah_attr) & ~((1 << ppd->lmc) - 1);
520
		if (unlikely(!loopback &&
D
Don Hiatt 已提交
521 522 523
			     ((lid == ppd->lid) ||
			      ((lid == be32_to_cpu(OPA_LID_PERMISSIVE)) &&
			       (qp->ibqp.qp_type == IB_QPT_GSI))))) {
524
			unsigned long tflags = ps->flags;
M
Mike Marciniszyn 已提交
525 526 527 528 529 530 531
			/*
			 * If DMAs are in progress, we can't generate
			 * a completion for the loopback packet since
			 * it would be out of order.
			 * Instead of waiting, we could queue a
			 * zero length descriptor so we get a callback.
			 */
532
			if (iowait_sdma_pending(&priv->s_iowait)) {
533
				qp->s_flags |= RVT_S_WAIT_DMA;
M
Mike Marciniszyn 已提交
534 535 536
				goto bail;
			}
			qp->s_cur = next_cur;
537
			spin_unlock_irqrestore(&qp->s_lock, tflags);
M
Mike Marciniszyn 已提交
538
			ud_loopback(qp, wqe);
539 540
			spin_lock_irqsave(&qp->s_lock, tflags);
			ps->flags = tflags;
M
Mike Marciniszyn 已提交
541
			hfi1_send_complete(qp, wqe, IB_WC_SUCCESS);
542
			goto done_free_tx;
M
Mike Marciniszyn 已提交
543 544 545 546
		}
	}

	qp->s_cur = next_cur;
547
	ps->s_txreq->s_cur_size = wqe->length;
548
	ps->s_txreq->ss = &qp->s_sge;
549
	qp->s_srate = rdma_ah_get_static_rate(ah_attr);
M
Mike Marciniszyn 已提交
550 551 552 553 554 555 556
	qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
	qp->s_wqe = wqe;
	qp->s_sge.sge = wqe->sg_list[0];
	qp->s_sge.sg_list = wqe->sg_list + 1;
	qp->s_sge.num_sge = wqe->wr.num_sge;
	qp->s_sge.total_len = wqe->length;

D
Don Hiatt 已提交
557 558
	/* Make the appropriate header */
	hfi1_make_ud_req_tbl[priv->hdr_type](qp, ps, qp->s_wqe);
559
	priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
560
	ps->s_txreq->sde = priv->s_sde;
561
	priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
562
	ps->s_txreq->psc = priv->s_sendcontext;
M
Mike Marciniszyn 已提交
563
	/* disarm any ahg */
564 565 566
	priv->s_ahg->ahgcount = 0;
	priv->s_ahg->ahgidx = 0;
	priv->s_ahg->tx_flags = 0;
567 568
	/* pbc */
	ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
M
Mike Marciniszyn 已提交
569

570
	return 1;
571 572 573 574 575 576

done_free_tx:
	hfi1_put_txreq(ps->s_txreq);
	ps->s_txreq = NULL;
	return 1;

M
Mike Marciniszyn 已提交
577
bail:
578 579 580 581
	hfi1_put_txreq(ps->s_txreq);

bail_no_tx:
	ps->s_txreq = NULL;
582
	qp->s_flags &= ~RVT_S_BUSY;
583 584
	qp->s_hdrwords = 0;
	return 0;
M
Mike Marciniszyn 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
}

/*
 * Hardware can't check this so we do it here.
 *
 * This is a slightly different algorithm than the standard pkey check.  It
 * special cases the management keys and allows for 0x7fff and 0xffff to be in
 * the table at the same time.
 *
 * @returns the index found or -1 if not found
 */
int hfi1_lookup_pkey_idx(struct hfi1_ibport *ibp, u16 pkey)
{
	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
	unsigned i;

	if (pkey == FULL_MGMT_P_KEY || pkey == LIM_MGMT_P_KEY) {
		unsigned lim_idx = -1;

		for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i) {
			/* here we look for an exact match */
			if (ppd->pkeys[i] == pkey)
				return i;
			if (ppd->pkeys[i] == LIM_MGMT_P_KEY)
				lim_idx = i;
		}

		/* did not find 0xffff return 0x7fff idx if found */
		if (pkey == FULL_MGMT_P_KEY)
			return lim_idx;

		/* no match...  */
		return -1;
	}

	pkey &= 0x7fff; /* remove limited/full membership bit */

	for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i)
		if ((ppd->pkeys[i] & 0x7fff) == pkey)
			return i;

	/*
	 * Should not get here, this means hardware failed to validate pkeys.
	 */
	return -1;
}

D
Don Hiatt 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
void return_cnp_16B(struct hfi1_ibport *ibp, struct rvt_qp *qp,
		    u32 remote_qpn, u32 pkey, u32 slid, u32 dlid,
		    u8 sc5, const struct ib_grh *old_grh)
{
	u64 pbc, pbc_flags = 0;
	u32 bth0, plen, vl, hwords = 7;
	u16 len;
	u8 l4;
	struct hfi1_16b_header hdr;
	struct ib_other_headers *ohdr;
	struct pio_buf *pbuf;
	struct send_context *ctxt = qp_to_send_context(qp, sc5);
	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
	u32 nwords;

	/* Populate length */
	nwords = ((hfi1_get_16b_padding(hwords << 2, 0) +
		   SIZE_OF_LT) >> 2) + SIZE_OF_CRC;
	if (old_grh) {
		struct ib_grh *grh = &hdr.u.l.grh;

		grh->version_tclass_flow = old_grh->version_tclass_flow;
		grh->paylen = cpu_to_be16((hwords - 4 + nwords) << 2);
		grh->hop_limit = 0xff;
		grh->sgid = old_grh->dgid;
		grh->dgid = old_grh->sgid;
		ohdr = &hdr.u.l.oth;
		l4 = OPA_16B_L4_IB_GLOBAL;
		hwords += sizeof(struct ib_grh) / sizeof(u32);
	} else {
		ohdr = &hdr.u.oth;
		l4 = OPA_16B_L4_IB_LOCAL;
	}

	/* BIT 16 to 19 is TVER. Bit 20 to 22 is pad cnt */
	bth0 = (IB_OPCODE_CNP << 24) | (1 << 16) |
	       (hfi1_get_16b_padding(hwords << 2, 0) << 20);
	ohdr->bth[0] = cpu_to_be32(bth0);

	ohdr->bth[1] = cpu_to_be32(remote_qpn);
	ohdr->bth[2] = 0; /* PSN 0 */

	/* Convert dwords to flits */
	len = (hwords + nwords) >> 1;
	hfi1_make_16b_hdr(&hdr, slid, dlid, len, pkey, 1, 0, l4, sc5);

	plen = 2 /* PBC */ + hwords + nwords;
	pbc_flags |= PBC_PACKET_BYPASS | PBC_INSERT_BYPASS_ICRC;
	vl = sc_to_vlt(ppd->dd, sc5);
	pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
	if (ctxt) {
		pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
		if (pbuf)
			ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
						 &hdr, hwords);
	}
}

690
void return_cnp(struct hfi1_ibport *ibp, struct rvt_qp *qp, u32 remote_qpn,
M
Mike Marciniszyn 已提交
691 692 693 694 695 696 697
		u32 pkey, u32 slid, u32 dlid, u8 sc5,
		const struct ib_grh *old_grh)
{
	u64 pbc, pbc_flags = 0;
	u32 bth0, plen, vl, hwords = 5;
	u16 lrh0;
	u8 sl = ibp->sc_to_sl[sc5];
698 699
	struct ib_header hdr;
	struct ib_other_headers *ohdr;
M
Mike Marciniszyn 已提交
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
	struct pio_buf *pbuf;
	struct send_context *ctxt = qp_to_send_context(qp, sc5);
	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);

	if (old_grh) {
		struct ib_grh *grh = &hdr.u.l.grh;

		grh->version_tclass_flow = old_grh->version_tclass_flow;
		grh->paylen = cpu_to_be16((hwords - 2 + SIZE_OF_CRC) << 2);
		grh->hop_limit = 0xff;
		grh->sgid = old_grh->dgid;
		grh->dgid = old_grh->sgid;
		ohdr = &hdr.u.l.oth;
		lrh0 = HFI1_LRH_GRH;
		hwords += sizeof(struct ib_grh) / sizeof(u32);
	} else {
		ohdr = &hdr.u.oth;
		lrh0 = HFI1_LRH_BTH;
	}

	lrh0 |= (sc5 & 0xf) << 12 | sl << 4;

	bth0 = pkey | (IB_OPCODE_CNP << 24);
	ohdr->bth[0] = cpu_to_be32(bth0);

725
	ohdr->bth[1] = cpu_to_be32(remote_qpn | (1 << IB_BECN_SHIFT));
M
Mike Marciniszyn 已提交
726 727
	ohdr->bth[2] = 0; /* PSN 0 */

D
Don Hiatt 已提交
728
	hfi1_make_ib_hdr(&hdr, lrh0, hwords + SIZE_OF_CRC, dlid, slid);
M
Mike Marciniszyn 已提交
729
	plen = 2 /* PBC */ + hwords;
730
	pbc_flags |= (ib_is_sc5(sc5) << PBC_DC_INFO_SHIFT);
M
Mike Marciniszyn 已提交
731 732 733 734 735 736 737 738 739 740 741 742
	vl = sc_to_vlt(ppd->dd, sc5);
	pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
	if (ctxt) {
		pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
		if (pbuf)
			ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
						 &hdr, hwords);
	}
}

/*
 * opa_smp_check() - Do the regular pkey checking, and the additional
743 744
 * checks for SMPs specified in OPAv1 rev 1.0, 9/19/2016 update, section
 * 9.10.25 ("SMA Packet Checks").
M
Mike Marciniszyn 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
 *
 * Note that:
 *   - Checks are done using the pkey directly from the packet's BTH,
 *     and specifically _not_ the pkey that we attach to the completion,
 *     which may be different.
 *   - These checks are specifically for "non-local" SMPs (i.e., SMPs
 *     which originated on another node). SMPs which are sent from, and
 *     destined to this node are checked in opa_local_smp_check().
 *
 * At the point where opa_smp_check() is called, we know:
 *   - destination QP is QP0
 *
 * opa_smp_check() returns 0 if all checks succeed, 1 otherwise.
 */
static int opa_smp_check(struct hfi1_ibport *ibp, u16 pkey, u8 sc5,
760
			 struct rvt_qp *qp, u16 slid, struct opa_smp *smp)
M
Mike Marciniszyn 已提交
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
{
	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);

	/*
	 * I don't think it's possible for us to get here with sc != 0xf,
	 * but check it to be certain.
	 */
	if (sc5 != 0xf)
		return 1;

	if (rcv_pkey_check(ppd, pkey, sc5, slid))
		return 1;

	/*
	 * At this point we know (and so don't need to check again) that
	 * the pkey is either LIM_MGMT_P_KEY, or FULL_MGMT_P_KEY
	 * (see ingress_pkey_check).
	 */
	if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE &&
	    smp->mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED) {
		ingress_pkey_table_fail(ppd, pkey, slid);
		return 1;
	}

	/*
	 * SMPs fall into one of four (disjoint) categories:
787 788 789
	 * SMA request, SMA response, SMA trap, or SMA trap repress.
	 * Our response depends, in part, on which type of SMP we're
	 * processing.
M
Mike Marciniszyn 已提交
790
	 *
791 792 793
	 * If this is an SMA response, skip the check here.
	 *
	 * If this is an SMA request or SMA trap repress:
M
Mike Marciniszyn 已提交
794 795
	 *   - pkey != FULL_MGMT_P_KEY =>
	 *       increment port recv constraint errors, drop MAD
796 797 798 799 800 801 802 803
	 *
	 * Otherwise:
	 *    - accept if the port is running an SM
	 *    - drop MAD if it's an SMA trap
	 *    - pkey == FULL_MGMT_P_KEY =>
	 *        reply with unsupported method
	 *    - pkey != FULL_MGMT_P_KEY =>
	 *	  increment port recv constraint errors, drop MAD
M
Mike Marciniszyn 已提交
804 805
	 */
	switch (smp->method) {
806 807 808
	case IB_MGMT_METHOD_GET_RESP:
	case IB_MGMT_METHOD_REPORT_RESP:
		break;
M
Mike Marciniszyn 已提交
809 810 811 812 813 814 815 816 817
	case IB_MGMT_METHOD_GET:
	case IB_MGMT_METHOD_SET:
	case IB_MGMT_METHOD_REPORT:
	case IB_MGMT_METHOD_TRAP_REPRESS:
		if (pkey != FULL_MGMT_P_KEY) {
			ingress_pkey_table_fail(ppd, pkey, slid);
			return 1;
		}
		break;
818
	default:
819
		if (ibp->rvp.port_cap_flags & IB_PORT_SM)
M
Mike Marciniszyn 已提交
820
			return 0;
821 822
		if (smp->method == IB_MGMT_METHOD_TRAP)
			return 1;
M
Mike Marciniszyn 已提交
823 824 825 826
		if (pkey == FULL_MGMT_P_KEY) {
			smp->status |= IB_SMP_UNSUP_METHOD;
			return 0;
		}
827 828
		ingress_pkey_table_fail(ppd, pkey, slid);
		return 1;
M
Mike Marciniszyn 已提交
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
	}
	return 0;
}

/**
 * hfi1_ud_rcv - receive an incoming UD packet
 * @ibp: the port the packet came in on
 * @hdr: the packet header
 * @rcv_flags: flags relevant to rcv processing
 * @data: the packet data
 * @tlen: the packet length
 * @qp: the QP the packet came on
 *
 * This is called from qp_rcv() to process an incoming UD packet
 * for the given QP.
 * Called at interrupt level.
 */
void hfi1_ud_rcv(struct hfi1_packet *packet)
{
848
	struct ib_other_headers *ohdr = packet->ohdr;
M
Mike Marciniszyn 已提交
849 850 851 852
	u32 hdrsize = packet->hlen;
	struct ib_wc wc;
	u32 qkey;
	u32 src_qp;
853
	u16 pkey;
M
Mike Marciniszyn 已提交
854
	int mgmt_pkey_idx = -1;
855
	struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
856
	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
857
	void *data = packet->payload;
M
Mike Marciniszyn 已提交
858
	u32 tlen = packet->tlen;
859
	struct rvt_qp *qp = packet->qp;
D
Don Hiatt 已提交
860
	u8 sc5 = packet->sc;
861 862 863 864 865
	u8 sl_from_sc;
	u8 opcode = packet->opcode;
	u8 sl = packet->sl;
	u32 dlid = packet->dlid;
	u32 slid = packet->slid;
D
Don Hiatt 已提交
866 867 868
	u8 extra_bytes;
	bool dlid_is_permissive;
	bool slid_is_permissive;
M
Mike Marciniszyn 已提交
869

D
Don Hiatt 已提交
870
	extra_bytes = packet->pad + packet->extra_byte + (SIZE_OF_CRC << 2);
871 872
	qkey = ib_get_qkey(ohdr);
	src_qp = ib_get_sqpn(ohdr);
D
Don Hiatt 已提交
873 874 875 876 877 878 879 880 881 882 883 884 885

	if (packet->etype == RHF_RCV_TYPE_BYPASS) {
		u32 permissive_lid =
			opa_get_lid(be32_to_cpu(OPA_LID_PERMISSIVE), 16B);

		pkey = hfi1_16B_get_pkey(packet->hdr);
		dlid_is_permissive = (dlid == permissive_lid);
		slid_is_permissive = (slid == permissive_lid);
	} else {
		pkey = ib_bth_get_pkey(ohdr);
		dlid_is_permissive = (dlid == be16_to_cpu(IB_LID_PERMISSIVE));
		slid_is_permissive = (slid == be16_to_cpu(IB_LID_PERMISSIVE));
	}
886
	sl_from_sc = ibp->sc_to_sl[sc5];
M
Mike Marciniszyn 已提交
887

888
	process_ecn(qp, packet, (opcode != IB_OPCODE_CNP));
M
Mike Marciniszyn 已提交
889 890 891 892
	/*
	 * Get the number of bytes the message was padded by
	 * and drop incomplete packets.
	 */
893
	if (unlikely(tlen < (hdrsize + extra_bytes)))
M
Mike Marciniszyn 已提交
894 895
		goto drop;

896
	tlen -= hdrsize + extra_bytes;
M
Mike Marciniszyn 已提交
897 898 899 900 901 902

	/*
	 * Check that the permissive LID is only used on QP0
	 * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
	 */
	if (qp->ibqp.qp_num) {
D
Don Hiatt 已提交
903
		if (unlikely(dlid_is_permissive || slid_is_permissive))
M
Mike Marciniszyn 已提交
904 905 906 907 908 909 910 911 912
			goto drop;
		if (qp->ibqp.qp_num > 1) {
			if (unlikely(rcv_pkey_check(ppd, pkey, sc5, slid))) {
				/*
				 * Traps will not be sent for packets dropped
				 * by the HW. This is fine, as sending trap
				 * for invalid pkeys is optional according to
				 * IB spec (release 1.3, section 10.9.4)
				 */
913 914 915 916
				hfi1_bad_pkey(ibp,
					      pkey, sl,
					      src_qp, qp->ibqp.qp_num,
					      slid, dlid);
M
Mike Marciniszyn 已提交
917 918 919 920 921 922 923 924
				return;
			}
		} else {
			/* GSI packet */
			mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
			if (mgmt_pkey_idx < 0)
				goto drop;
		}
925
		if (unlikely(qkey != qp->qkey)) /* Silent drop */
M
Mike Marciniszyn 已提交
926
			return;
927

M
Mike Marciniszyn 已提交
928 929
		/* Drop invalid MAD packets (see 13.5.3.1). */
		if (unlikely(qp->ibqp.qp_num == 1 &&
930
			     (tlen > 2048 || (sc5 == 0xF))))
M
Mike Marciniszyn 已提交
931 932 933 934 935 936 937 938 939 940
			goto drop;
	} else {
		/* Received on QP0, and so by definition, this is an SMP */
		struct opa_smp *smp = (struct opa_smp *)data;

		if (opa_smp_check(ibp, pkey, sc5, qp, slid, smp))
			goto drop;

		if (tlen > 2048)
			goto drop;
D
Don Hiatt 已提交
941
		if ((dlid_is_permissive || slid_is_permissive) &&
M
Mike Marciniszyn 已提交
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
		    smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
			goto drop;

		/* look up SMI pkey */
		mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
		if (mgmt_pkey_idx < 0)
			goto drop;
	}

	if (qp->ibqp.qp_num > 1 &&
	    opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
		wc.ex.imm_data = ohdr->u.ud.imm_data;
		wc.wc_flags = IB_WC_WITH_IMM;
		tlen -= sizeof(u32);
	} else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
		wc.ex.imm_data = 0;
		wc.wc_flags = 0;
959
	} else {
M
Mike Marciniszyn 已提交
960
		goto drop;
961
	}
M
Mike Marciniszyn 已提交
962 963 964 965 966 967 968 969 970 971

	/*
	 * A GRH is expected to precede the data even if not
	 * present on the wire.
	 */
	wc.byte_len = tlen + sizeof(struct ib_grh);

	/*
	 * Get the next work request entry to find where to put the data.
	 */
972
	if (qp->r_flags & RVT_R_REUSE_SGE) {
973
		qp->r_flags &= ~RVT_R_REUSE_SGE;
974
	} else {
M
Mike Marciniszyn 已提交
975 976
		int ret;

977
		ret = hfi1_rvt_get_rwqe(qp, 0);
M
Mike Marciniszyn 已提交
978
		if (ret < 0) {
979
			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
M
Mike Marciniszyn 已提交
980 981 982 983
			return;
		}
		if (!ret) {
			if (qp->ibqp.qp_num == 0)
984
				ibp->rvp.n_vl15_dropped++;
M
Mike Marciniszyn 已提交
985 986 987 988 989
			return;
		}
	}
	/* Silently drop packets which are too big. */
	if (unlikely(wc.byte_len > qp->r_len)) {
990
		qp->r_flags |= RVT_R_REUSE_SGE;
M
Mike Marciniszyn 已提交
991 992
		goto drop;
	}
993
	if (packet->grh) {
D
Don Hiatt 已提交
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
		hfi1_copy_sge(&qp->r_sge, packet->grh,
			      sizeof(struct ib_grh), true, false);
		wc.wc_flags |= IB_WC_GRH;
	} else if (packet->etype == RHF_RCV_TYPE_BYPASS) {
		struct ib_grh grh;
		/*
		 * Assuming we only created 16B on the send side
		 * if we want to use large LIDs, since GRH was stripped
		 * out when creating 16B, add back the GRH here.
		 */
		hfi1_make_ext_grh(packet, &grh, slid, dlid);
		hfi1_copy_sge(&qp->r_sge, &grh,
1006
			      sizeof(struct ib_grh), true, false);
M
Mike Marciniszyn 已提交
1007
		wc.wc_flags |= IB_WC_GRH;
1008
	} else {
1009
		rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
1010
	}
1011
	hfi1_copy_sge(&qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh),
1012
		      true, false);
1013
	rvt_put_ss(&qp->r_sge);
1014
	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
M
Mike Marciniszyn 已提交
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
		return;
	wc.wr_id = qp->r_wr_id;
	wc.status = IB_WC_SUCCESS;
	wc.opcode = IB_WC_RECV;
	wc.vendor_err = 0;
	wc.qp = &qp->ibqp;
	wc.src_qp = src_qp;

	if (qp->ibqp.qp_type == IB_QPT_GSI ||
	    qp->ibqp.qp_type == IB_QPT_SMI) {
		if (mgmt_pkey_idx < 0) {
			if (net_ratelimit()) {
				struct hfi1_devdata *dd = ppd->dd;

				dd_dev_err(dd, "QP type %d mgmt_pkey_idx < 0 and packet not dropped???\n",
					   qp->ibqp.qp_type);
				mgmt_pkey_idx = 0;
			}
		}
		wc.pkey_index = (unsigned)mgmt_pkey_idx;
1035
	} else {
M
Mike Marciniszyn 已提交
1036
		wc.pkey_index = 0;
1037
	}
D
Don Hiatt 已提交
1038 1039
	if (slid_is_permissive)
		slid = be32_to_cpu(OPA_LID_PERMISSIVE);
1040
	wc.slid = slid & U16_MAX;
1041
	wc.sl = sl_from_sc;
M
Mike Marciniszyn 已提交
1042 1043 1044 1045

	/*
	 * Save the LMC lower bits if the destination LID is a unicast LID.
	 */
D
Don Hiatt 已提交
1046
	wc.dlid_path_bits = hfi1_check_mcast(dlid) ? 0 :
M
Mike Marciniszyn 已提交
1047 1048 1049
		dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
	wc.port_num = qp->port_num;
	/* Signal completion event if the solicited bit is set. */
1050 1051 1052
	rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
		     (ohdr->bth[0] &
		      cpu_to_be32(IB_BTH_SOLICITED)) != 0);
M
Mike Marciniszyn 已提交
1053 1054 1055
	return;

drop:
1056
	ibp->rvp.n_pkt_drops++;
M
Mike Marciniszyn 已提交
1057
}