Linux Audio

Check our new training course

Loading...
v6.8
  1/*
  2 * Copyright (c) 2012 - 2019 Intel Corporation.  All rights reserved.
  3 * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
  4 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  5 *
  6 * This software is available to you under a choice of one of two
  7 * licenses.  You may choose to be licensed under the terms of the GNU
  8 * General Public License (GPL) Version 2, available from the file
  9 * COPYING in the main directory of this source tree, or the
 10 * OpenIB.org BSD license below:
 11 *
 12 *     Redistribution and use in source and binary forms, with or
 13 *     without modification, are permitted provided that the following
 14 *     conditions are met:
 15 *
 16 *      - Redistributions of source code must retain the above
 17 *        copyright notice, this list of conditions and the following
 18 *        disclaimer.
 19 *
 20 *      - Redistributions in binary form must reproduce the above
 21 *        copyright notice, this list of conditions and the following
 22 *        disclaimer in the documentation and/or other materials
 23 *        provided with the distribution.
 24 *
 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 32 * SOFTWARE.
 33 */
 34
 35#include <rdma/ib_smi.h>
 36#include <rdma/ib_verbs.h>
 37
 38#include "qib.h"
 39#include "qib_mad.h"
 40
 41/**
 42 * qib_ud_loopback - handle send on loopback QPs
 43 * @sqp: the sending QP
 44 * @swqe: the send work request
 45 *
 46 * This is called from qib_make_ud_req() to forward a WQE addressed
 47 * to the same HCA.
 48 * Note that the receive interrupt handler may be calling qib_ud_rcv()
 49 * while this is being called.
 50 */
 51static void qib_ud_loopback(struct rvt_qp *sqp, struct rvt_swqe *swqe)
 52{
 53	struct qib_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
 54	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
 55	struct qib_devdata *dd = ppd->dd;
 56	struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
 57	struct rvt_qp *qp;
 58	struct rdma_ah_attr *ah_attr;
 59	unsigned long flags;
 60	struct rvt_sge_state ssge;
 61	struct rvt_sge *sge;
 62	struct ib_wc wc;
 63	u32 length;
 64	enum ib_qp_type sqptype, dqptype;
 65
 66	rcu_read_lock();
 67	qp = rvt_lookup_qpn(rdi, &ibp->rvp, rvt_get_swqe_remote_qpn(swqe));
 68	if (!qp) {
 69		ibp->rvp.n_pkt_drops++;
 70		goto drop;
 71	}
 72
 73	sqptype = sqp->ibqp.qp_type == IB_QPT_GSI ?
 74			IB_QPT_UD : sqp->ibqp.qp_type;
 75	dqptype = qp->ibqp.qp_type == IB_QPT_GSI ?
 76			IB_QPT_UD : qp->ibqp.qp_type;
 77
 78	if (dqptype != sqptype ||
 79	    !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
 80		ibp->rvp.n_pkt_drops++;
 81		goto drop;
 82	}
 83
 84	ah_attr = rvt_get_swqe_ah_attr(swqe);
 85	ppd = ppd_from_ibp(ibp);
 86
 87	if (qp->ibqp.qp_num > 1) {
 88		u16 pkey1;
 89		u16 pkey2;
 90		u16 lid;
 91
 92		pkey1 = qib_get_pkey(ibp, sqp->s_pkey_index);
 93		pkey2 = qib_get_pkey(ibp, qp->s_pkey_index);
 94		if (unlikely(!qib_pkey_ok(pkey1, pkey2))) {
 95			lid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
 96					  ((1 << ppd->lmc) - 1));
 97			qib_bad_pkey(ibp, pkey1,
 98				     rdma_ah_get_sl(ah_attr),
 99				     sqp->ibqp.qp_num, qp->ibqp.qp_num,
100				     cpu_to_be16(lid),
101				     cpu_to_be16(rdma_ah_get_dlid(ah_attr)));
102			goto drop;
103		}
104	}
105
106	/*
107	 * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
108	 * Qkeys with the high order bit set mean use the
109	 * qkey from the QP context instead of the WR (see 10.2.5).
110	 */
111	if (qp->ibqp.qp_num) {
112		u32 qkey;
113
114		qkey = (int)rvt_get_swqe_remote_qkey(swqe) < 0 ?
115			sqp->qkey : rvt_get_swqe_remote_qkey(swqe);
116		if (unlikely(qkey != qp->qkey))
117			goto drop;
118	}
119
120	/*
121	 * A GRH is expected to precede the data even if not
122	 * present on the wire.
123	 */
124	length = swqe->length;
125	memset(&wc, 0, sizeof(wc));
126	wc.byte_len = length + sizeof(struct ib_grh);
127
128	if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
129		wc.wc_flags = IB_WC_WITH_IMM;
130		wc.ex.imm_data = swqe->wr.ex.imm_data;
131	}
132
133	spin_lock_irqsave(&qp->r_lock, flags);
134
135	/*
136	 * Get the next work request entry to find where to put the data.
137	 */
138	if (qp->r_flags & RVT_R_REUSE_SGE)
139		qp->r_flags &= ~RVT_R_REUSE_SGE;
140	else {
141		int ret;
142
143		ret = rvt_get_rwqe(qp, false);
144		if (ret < 0) {
145			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
146			goto bail_unlock;
147		}
148		if (!ret) {
149			if (qp->ibqp.qp_num == 0)
150				ibp->rvp.n_vl15_dropped++;
151			goto bail_unlock;
152		}
153	}
154	/* Silently drop packets which are too big. */
155	if (unlikely(wc.byte_len > qp->r_len)) {
156		qp->r_flags |= RVT_R_REUSE_SGE;
157		ibp->rvp.n_pkt_drops++;
158		goto bail_unlock;
159	}
160
161	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
162		struct ib_grh grh;
163		const struct ib_global_route *grd = rdma_ah_read_grh(ah_attr);
164
165		qib_make_grh(ibp, &grh, grd, 0, 0);
166		rvt_copy_sge(qp, &qp->r_sge, &grh,
167			     sizeof(grh), true, false);
168		wc.wc_flags |= IB_WC_GRH;
169	} else
170		rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
171	ssge.sg_list = swqe->sg_list + 1;
172	ssge.sge = *swqe->sg_list;
173	ssge.num_sge = swqe->wr.num_sge;
174	sge = &ssge.sge;
175	while (length) {
176		u32 len = rvt_get_sge_length(sge, length);
177
178		rvt_copy_sge(qp, &qp->r_sge, sge->vaddr, len, true, false);
 
 
 
 
 
179		sge->vaddr += len;
180		sge->length -= len;
181		sge->sge_length -= len;
182		if (sge->sge_length == 0) {
183			if (--ssge.num_sge)
184				*sge = *ssge.sg_list++;
185		} else if (sge->length == 0 && sge->mr->lkey) {
186			if (++sge->n >= RVT_SEGSZ) {
187				if (++sge->m >= sge->mr->mapsz)
188					break;
189				sge->n = 0;
190			}
191			sge->vaddr =
192				sge->mr->map[sge->m]->segs[sge->n].vaddr;
193			sge->length =
194				sge->mr->map[sge->m]->segs[sge->n].length;
195		}
196		length -= len;
197	}
198	rvt_put_ss(&qp->r_sge);
199	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
200		goto bail_unlock;
201	wc.wr_id = qp->r_wr_id;
202	wc.status = IB_WC_SUCCESS;
203	wc.opcode = IB_WC_RECV;
204	wc.qp = &qp->ibqp;
205	wc.src_qp = sqp->ibqp.qp_num;
206	wc.pkey_index = qp->ibqp.qp_type == IB_QPT_GSI ?
207		rvt_get_swqe_pkey_index(swqe) : 0;
208	wc.slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
209				((1 << ppd->lmc) - 1));
210	wc.sl = rdma_ah_get_sl(ah_attr);
211	wc.dlid_path_bits = rdma_ah_get_dlid(ah_attr) & ((1 << ppd->lmc) - 1);
212	wc.port_num = qp->port_num;
213	/* Signal completion event if the solicited bit is set. */
214	rvt_recv_cq(qp, &wc, swqe->wr.send_flags & IB_SEND_SOLICITED);
 
215	ibp->rvp.n_loop_pkts++;
216bail_unlock:
217	spin_unlock_irqrestore(&qp->r_lock, flags);
218drop:
219	rcu_read_unlock();
220}
221
222/**
223 * qib_make_ud_req - construct a UD request packet
224 * @qp: the QP
225 * @flags: flags to modify and pass back to caller
226 *
227 * Assumes the s_lock is held.
228 *
229 * Return 1 if constructed; otherwise, return 0.
230 */
231int qib_make_ud_req(struct rvt_qp *qp, unsigned long *flags)
232{
233	struct qib_qp_priv *priv = qp->priv;
234	struct ib_other_headers *ohdr;
235	struct rdma_ah_attr *ah_attr;
236	struct qib_pportdata *ppd;
237	struct qib_ibport *ibp;
238	struct rvt_swqe *wqe;
239	u32 nwords;
240	u32 extra_bytes;
241	u32 bth0;
242	u16 lrh0;
243	u16 lid;
244	int ret = 0;
245	int next_cur;
246
247	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
248		if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
249			goto bail;
250		/* We are in the error state, flush the work request. */
251		if (qp->s_last == READ_ONCE(qp->s_head))
252			goto bail;
253		/* If DMAs are in progress, we can't flush immediately. */
254		if (atomic_read(&priv->s_dma_busy)) {
255			qp->s_flags |= RVT_S_WAIT_DMA;
256			goto bail;
257		}
258		wqe = rvt_get_swqe_ptr(qp, qp->s_last);
259		rvt_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
260		goto done;
261	}
262
263	/* see post_one_send() */
264	if (qp->s_cur == READ_ONCE(qp->s_head))
265		goto bail;
266
267	wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
268	next_cur = qp->s_cur + 1;
269	if (next_cur >= qp->s_size)
270		next_cur = 0;
271
272	/* Construct the header. */
273	ibp = to_iport(qp->ibqp.device, qp->port_num);
274	ppd = ppd_from_ibp(ibp);
275	ah_attr = rvt_get_swqe_ah_attr(wqe);
276	if (rdma_ah_get_dlid(ah_attr) >= be16_to_cpu(IB_MULTICAST_LID_BASE)) {
277		if (rdma_ah_get_dlid(ah_attr) !=
278				be16_to_cpu(IB_LID_PERMISSIVE))
279			this_cpu_inc(ibp->pmastats->n_multicast_xmit);
280		else
281			this_cpu_inc(ibp->pmastats->n_unicast_xmit);
282	} else {
283		this_cpu_inc(ibp->pmastats->n_unicast_xmit);
284		lid = rdma_ah_get_dlid(ah_attr) & ~((1 << ppd->lmc) - 1);
285		if (unlikely(lid == ppd->lid)) {
286			unsigned long tflags = *flags;
287			/*
288			 * If DMAs are in progress, we can't generate
289			 * a completion for the loopback packet since
290			 * it would be out of order.
291			 * XXX Instead of waiting, we could queue a
292			 * zero length descriptor so we get a callback.
293			 */
294			if (atomic_read(&priv->s_dma_busy)) {
295				qp->s_flags |= RVT_S_WAIT_DMA;
296				goto bail;
297			}
298			qp->s_cur = next_cur;
299			spin_unlock_irqrestore(&qp->s_lock, tflags);
300			qib_ud_loopback(qp, wqe);
301			spin_lock_irqsave(&qp->s_lock, tflags);
302			*flags = tflags;
303			rvt_send_complete(qp, wqe, IB_WC_SUCCESS);
304			goto done;
305		}
306	}
307
308	qp->s_cur = next_cur;
309	extra_bytes = -wqe->length & 3;
310	nwords = (wqe->length + extra_bytes) >> 2;
311
312	/* header size in 32-bit words LRH+BTH+DETH = (8+12+8)/4. */
313	qp->s_hdrwords = 7;
314	qp->s_cur_size = wqe->length;
315	qp->s_cur_sge = &qp->s_sge;
316	qp->s_srate = rdma_ah_get_static_rate(ah_attr);
317	qp->s_wqe = wqe;
318	qp->s_sge.sge = wqe->sg_list[0];
319	qp->s_sge.sg_list = wqe->sg_list + 1;
320	qp->s_sge.num_sge = wqe->wr.num_sge;
321	qp->s_sge.total_len = wqe->length;
322
323	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
324		/* Header size in 32-bit words. */
325		qp->s_hdrwords += qib_make_grh(ibp, &priv->s_hdr->u.l.grh,
326					       rdma_ah_read_grh(ah_attr),
327					       qp->s_hdrwords, nwords);
328		lrh0 = QIB_LRH_GRH;
329		ohdr = &priv->s_hdr->u.l.oth;
330		/*
331		 * Don't worry about sending to locally attached multicast
332		 * QPs.  It is unspecified by the spec. what happens.
333		 */
334	} else {
335		/* Header size in 32-bit words. */
336		lrh0 = QIB_LRH_BTH;
337		ohdr = &priv->s_hdr->u.oth;
338	}
339	if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
340		qp->s_hdrwords++;
341		ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
342		bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
343	} else
344		bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
345	lrh0 |= rdma_ah_get_sl(ah_attr) << 4;
346	if (qp->ibqp.qp_type == IB_QPT_SMI)
347		lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
348	else
349		lrh0 |= ibp->sl_to_vl[rdma_ah_get_sl(ah_attr)] << 12;
350	priv->s_hdr->lrh[0] = cpu_to_be16(lrh0);
351	priv->s_hdr->lrh[1] =
352			cpu_to_be16(rdma_ah_get_dlid(ah_attr));  /* DEST LID */
353	priv->s_hdr->lrh[2] =
354			cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
355	lid = ppd->lid;
356	if (lid) {
357		lid |= rdma_ah_get_path_bits(ah_attr) &
358			((1 << ppd->lmc) - 1);
359		priv->s_hdr->lrh[3] = cpu_to_be16(lid);
360	} else
361		priv->s_hdr->lrh[3] = IB_LID_PERMISSIVE;
362	if (wqe->wr.send_flags & IB_SEND_SOLICITED)
363		bth0 |= IB_BTH_SOLICITED;
364	bth0 |= extra_bytes << 20;
365	bth0 |= qp->ibqp.qp_type == IB_QPT_SMI ? QIB_DEFAULT_P_KEY :
366		qib_get_pkey(ibp, qp->ibqp.qp_type == IB_QPT_GSI ?
367			     rvt_get_swqe_pkey_index(wqe) : qp->s_pkey_index);
368	ohdr->bth[0] = cpu_to_be32(bth0);
369	/*
370	 * Use the multicast QP if the destination LID is a multicast LID.
371	 */
372	ohdr->bth[1] = rdma_ah_get_dlid(ah_attr) >=
373			be16_to_cpu(IB_MULTICAST_LID_BASE) &&
374		rdma_ah_get_dlid(ah_attr) != be16_to_cpu(IB_LID_PERMISSIVE) ?
375		cpu_to_be32(QIB_MULTICAST_QPN) :
376		cpu_to_be32(rvt_get_swqe_remote_qpn(wqe));
377	ohdr->bth[2] = cpu_to_be32(wqe->psn & QIB_PSN_MASK);
378	/*
379	 * Qkeys with the high order bit set mean use the
380	 * qkey from the QP context instead of the WR (see 10.2.5).
381	 */
382	ohdr->u.ud.deth[0] =
383		cpu_to_be32((int)rvt_get_swqe_remote_qkey(wqe) < 0 ? qp->qkey :
384			    rvt_get_swqe_remote_qkey(wqe));
385	ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
386
387done:
388	return 1;
389bail:
390	qp->s_flags &= ~RVT_S_BUSY;
391	return ret;
392}
393
394static unsigned qib_lookup_pkey(struct qib_ibport *ibp, u16 pkey)
395{
396	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
397	struct qib_devdata *dd = ppd->dd;
398	unsigned ctxt = ppd->hw_pidx;
399	unsigned i;
400
401	pkey &= 0x7fff;	/* remove limited/full membership bit */
402
403	for (i = 0; i < ARRAY_SIZE(dd->rcd[ctxt]->pkeys); ++i)
404		if ((dd->rcd[ctxt]->pkeys[i] & 0x7fff) == pkey)
405			return i;
406
407	/*
408	 * Should not get here, this means hardware failed to validate pkeys.
409	 * Punt and return index 0.
410	 */
411	return 0;
412}
413
414/**
415 * qib_ud_rcv - receive an incoming UD packet
416 * @ibp: the port the packet came in on
417 * @hdr: the packet header
418 * @has_grh: true if the packet has a GRH
419 * @data: the packet data
420 * @tlen: the packet length
421 * @qp: the QP the packet came on
422 *
423 * This is called from qib_qp_rcv() to process an incoming UD packet
424 * for the given QP.
425 * Called at interrupt level.
426 */
427void qib_ud_rcv(struct qib_ibport *ibp, struct ib_header *hdr,
428		int has_grh, void *data, u32 tlen, struct rvt_qp *qp)
429{
430	struct ib_other_headers *ohdr;
431	int opcode;
432	u32 hdrsize;
433	u32 pad;
434	struct ib_wc wc;
435	u32 qkey;
436	u32 src_qp;
437	u16 dlid;
438
439	/* Check for GRH */
440	if (!has_grh) {
441		ohdr = &hdr->u.oth;
442		hdrsize = 8 + 12 + 8;   /* LRH + BTH + DETH */
443	} else {
444		ohdr = &hdr->u.l.oth;
445		hdrsize = 8 + 40 + 12 + 8; /* LRH + GRH + BTH + DETH */
446	}
447	qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
448	src_qp = be32_to_cpu(ohdr->u.ud.deth[1]) & RVT_QPN_MASK;
449
450	/*
451	 * Get the number of bytes the message was padded by
452	 * and drop incomplete packets.
453	 */
454	pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
455	if (unlikely(tlen < (hdrsize + pad + 4)))
456		goto drop;
457
458	tlen -= hdrsize + pad + 4;
459
460	/*
461	 * Check that the permissive LID is only used on QP0
462	 * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
463	 */
464	if (qp->ibqp.qp_num) {
465		if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||
466			     hdr->lrh[3] == IB_LID_PERMISSIVE))
467			goto drop;
468		if (qp->ibqp.qp_num > 1) {
469			u16 pkey1, pkey2;
470
471			pkey1 = be32_to_cpu(ohdr->bth[0]);
472			pkey2 = qib_get_pkey(ibp, qp->s_pkey_index);
473			if (unlikely(!qib_pkey_ok(pkey1, pkey2))) {
474				qib_bad_pkey(ibp,
475					     pkey1,
476					     (be16_to_cpu(hdr->lrh[0]) >> 4) &
477						0xF,
478					     src_qp, qp->ibqp.qp_num,
479					     hdr->lrh[3], hdr->lrh[1]);
480				return;
481			}
482		}
483		if (unlikely(qkey != qp->qkey))
484			return;
485
486		/* Drop invalid MAD packets (see 13.5.3.1). */
487		if (unlikely(qp->ibqp.qp_num == 1 &&
488			     (tlen != 256 ||
489			      (be16_to_cpu(hdr->lrh[0]) >> 12) == 15)))
490			goto drop;
491	} else {
492		struct ib_smp *smp;
493
494		/* Drop invalid MAD packets (see 13.5.3.1). */
495		if (tlen != 256 || (be16_to_cpu(hdr->lrh[0]) >> 12) != 15)
496			goto drop;
497		smp = (struct ib_smp *) data;
498		if ((hdr->lrh[1] == IB_LID_PERMISSIVE ||
499		     hdr->lrh[3] == IB_LID_PERMISSIVE) &&
500		    smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
501			goto drop;
502	}
503
504	/*
505	 * The opcode is in the low byte when its in network order
506	 * (top byte when in host order).
507	 */
508	opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
509	if (qp->ibqp.qp_num > 1 &&
510	    opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
511		wc.ex.imm_data = ohdr->u.ud.imm_data;
512		wc.wc_flags = IB_WC_WITH_IMM;
 
513	} else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
514		wc.ex.imm_data = 0;
515		wc.wc_flags = 0;
516	} else
517		goto drop;
518
519	/*
520	 * A GRH is expected to precede the data even if not
521	 * present on the wire.
522	 */
523	wc.byte_len = tlen + sizeof(struct ib_grh);
524
525	/*
526	 * Get the next work request entry to find where to put the data.
527	 */
528	if (qp->r_flags & RVT_R_REUSE_SGE)
529		qp->r_flags &= ~RVT_R_REUSE_SGE;
530	else {
531		int ret;
532
533		ret = rvt_get_rwqe(qp, false);
534		if (ret < 0) {
535			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
536			return;
537		}
538		if (!ret) {
539			if (qp->ibqp.qp_num == 0)
540				ibp->rvp.n_vl15_dropped++;
541			return;
542		}
543	}
544	/* Silently drop packets which are too big. */
545	if (unlikely(wc.byte_len > qp->r_len)) {
546		qp->r_flags |= RVT_R_REUSE_SGE;
547		goto drop;
548	}
549	if (has_grh) {
550		rvt_copy_sge(qp, &qp->r_sge, &hdr->u.l.grh,
551			     sizeof(struct ib_grh), true, false);
552		wc.wc_flags |= IB_WC_GRH;
553	} else
554		rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
555	rvt_copy_sge(qp, &qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh),
556		     true, false);
557	rvt_put_ss(&qp->r_sge);
558	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
559		return;
560	wc.wr_id = qp->r_wr_id;
561	wc.status = IB_WC_SUCCESS;
562	wc.opcode = IB_WC_RECV;
563	wc.vendor_err = 0;
564	wc.qp = &qp->ibqp;
565	wc.src_qp = src_qp;
566	wc.pkey_index = qp->ibqp.qp_type == IB_QPT_GSI ?
567		qib_lookup_pkey(ibp, be32_to_cpu(ohdr->bth[0])) : 0;
568	wc.slid = be16_to_cpu(hdr->lrh[3]);
569	wc.sl = (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF;
570	dlid = be16_to_cpu(hdr->lrh[1]);
571	/*
572	 * Save the LMC lower bits if the destination LID is a unicast LID.
573	 */
574	wc.dlid_path_bits = dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE) ? 0 :
575		dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
576	wc.port_num = qp->port_num;
577	/* Signal completion event if the solicited bit is set. */
578	rvt_recv_cq(qp, &wc, ib_bth_is_solicited(ohdr));
 
579	return;
580
581drop:
582	ibp->rvp.n_pkt_drops++;
583}
v4.17
  1/*
 
  2 * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
  3 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  4 *
  5 * This software is available to you under a choice of one of two
  6 * licenses.  You may choose to be licensed under the terms of the GNU
  7 * General Public License (GPL) Version 2, available from the file
  8 * COPYING in the main directory of this source tree, or the
  9 * OpenIB.org BSD license below:
 10 *
 11 *     Redistribution and use in source and binary forms, with or
 12 *     without modification, are permitted provided that the following
 13 *     conditions are met:
 14 *
 15 *      - Redistributions of source code must retain the above
 16 *        copyright notice, this list of conditions and the following
 17 *        disclaimer.
 18 *
 19 *      - Redistributions in binary form must reproduce the above
 20 *        copyright notice, this list of conditions and the following
 21 *        disclaimer in the documentation and/or other materials
 22 *        provided with the distribution.
 23 *
 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 31 * SOFTWARE.
 32 */
 33
 34#include <rdma/ib_smi.h>
 35#include <rdma/ib_verbs.h>
 36
 37#include "qib.h"
 38#include "qib_mad.h"
 39
 40/**
 41 * qib_ud_loopback - handle send on loopback QPs
 42 * @sqp: the sending QP
 43 * @swqe: the send work request
 44 *
 45 * This is called from qib_make_ud_req() to forward a WQE addressed
 46 * to the same HCA.
 47 * Note that the receive interrupt handler may be calling qib_ud_rcv()
 48 * while this is being called.
 49 */
 50static void qib_ud_loopback(struct rvt_qp *sqp, struct rvt_swqe *swqe)
 51{
 52	struct qib_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
 53	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
 54	struct qib_devdata *dd = ppd->dd;
 55	struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
 56	struct rvt_qp *qp;
 57	struct rdma_ah_attr *ah_attr;
 58	unsigned long flags;
 59	struct rvt_sge_state ssge;
 60	struct rvt_sge *sge;
 61	struct ib_wc wc;
 62	u32 length;
 63	enum ib_qp_type sqptype, dqptype;
 64
 65	rcu_read_lock();
 66	qp = rvt_lookup_qpn(rdi, &ibp->rvp, swqe->ud_wr.remote_qpn);
 67	if (!qp) {
 68		ibp->rvp.n_pkt_drops++;
 69		goto drop;
 70	}
 71
 72	sqptype = sqp->ibqp.qp_type == IB_QPT_GSI ?
 73			IB_QPT_UD : sqp->ibqp.qp_type;
 74	dqptype = qp->ibqp.qp_type == IB_QPT_GSI ?
 75			IB_QPT_UD : qp->ibqp.qp_type;
 76
 77	if (dqptype != sqptype ||
 78	    !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
 79		ibp->rvp.n_pkt_drops++;
 80		goto drop;
 81	}
 82
 83	ah_attr = &ibah_to_rvtah(swqe->ud_wr.ah)->attr;
 84	ppd = ppd_from_ibp(ibp);
 85
 86	if (qp->ibqp.qp_num > 1) {
 87		u16 pkey1;
 88		u16 pkey2;
 89		u16 lid;
 90
 91		pkey1 = qib_get_pkey(ibp, sqp->s_pkey_index);
 92		pkey2 = qib_get_pkey(ibp, qp->s_pkey_index);
 93		if (unlikely(!qib_pkey_ok(pkey1, pkey2))) {
 94			lid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
 95					  ((1 << ppd->lmc) - 1));
 96			qib_bad_pkey(ibp, pkey1,
 97				     rdma_ah_get_sl(ah_attr),
 98				     sqp->ibqp.qp_num, qp->ibqp.qp_num,
 99				     cpu_to_be16(lid),
100				     cpu_to_be16(rdma_ah_get_dlid(ah_attr)));
101			goto drop;
102		}
103	}
104
105	/*
106	 * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
107	 * Qkeys with the high order bit set mean use the
108	 * qkey from the QP context instead of the WR (see 10.2.5).
109	 */
110	if (qp->ibqp.qp_num) {
111		u32 qkey;
112
113		qkey = (int)swqe->ud_wr.remote_qkey < 0 ?
114			sqp->qkey : swqe->ud_wr.remote_qkey;
115		if (unlikely(qkey != qp->qkey))
116			goto drop;
117	}
118
119	/*
120	 * A GRH is expected to precede the data even if not
121	 * present on the wire.
122	 */
123	length = swqe->length;
124	memset(&wc, 0, sizeof(wc));
125	wc.byte_len = length + sizeof(struct ib_grh);
126
127	if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
128		wc.wc_flags = IB_WC_WITH_IMM;
129		wc.ex.imm_data = swqe->wr.ex.imm_data;
130	}
131
132	spin_lock_irqsave(&qp->r_lock, flags);
133
134	/*
135	 * Get the next work request entry to find where to put the data.
136	 */
137	if (qp->r_flags & RVT_R_REUSE_SGE)
138		qp->r_flags &= ~RVT_R_REUSE_SGE;
139	else {
140		int ret;
141
142		ret = qib_get_rwqe(qp, 0);
143		if (ret < 0) {
144			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
145			goto bail_unlock;
146		}
147		if (!ret) {
148			if (qp->ibqp.qp_num == 0)
149				ibp->rvp.n_vl15_dropped++;
150			goto bail_unlock;
151		}
152	}
153	/* Silently drop packets which are too big. */
154	if (unlikely(wc.byte_len > qp->r_len)) {
155		qp->r_flags |= RVT_R_REUSE_SGE;
156		ibp->rvp.n_pkt_drops++;
157		goto bail_unlock;
158	}
159
160	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
161		struct ib_grh grh;
162		const struct ib_global_route *grd = rdma_ah_read_grh(ah_attr);
163
164		qib_make_grh(ibp, &grh, grd, 0, 0);
165		qib_copy_sge(&qp->r_sge, &grh,
166			     sizeof(grh), 1);
167		wc.wc_flags |= IB_WC_GRH;
168	} else
169		rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
170	ssge.sg_list = swqe->sg_list + 1;
171	ssge.sge = *swqe->sg_list;
172	ssge.num_sge = swqe->wr.num_sge;
173	sge = &ssge.sge;
174	while (length) {
175		u32 len = sge->length;
176
177		if (len > length)
178			len = length;
179		if (len > sge->sge_length)
180			len = sge->sge_length;
181		BUG_ON(len == 0);
182		qib_copy_sge(&qp->r_sge, sge->vaddr, len, 1);
183		sge->vaddr += len;
184		sge->length -= len;
185		sge->sge_length -= len;
186		if (sge->sge_length == 0) {
187			if (--ssge.num_sge)
188				*sge = *ssge.sg_list++;
189		} else if (sge->length == 0 && sge->mr->lkey) {
190			if (++sge->n >= RVT_SEGSZ) {
191				if (++sge->m >= sge->mr->mapsz)
192					break;
193				sge->n = 0;
194			}
195			sge->vaddr =
196				sge->mr->map[sge->m]->segs[sge->n].vaddr;
197			sge->length =
198				sge->mr->map[sge->m]->segs[sge->n].length;
199		}
200		length -= len;
201	}
202	rvt_put_ss(&qp->r_sge);
203	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
204		goto bail_unlock;
205	wc.wr_id = qp->r_wr_id;
206	wc.status = IB_WC_SUCCESS;
207	wc.opcode = IB_WC_RECV;
208	wc.qp = &qp->ibqp;
209	wc.src_qp = sqp->ibqp.qp_num;
210	wc.pkey_index = qp->ibqp.qp_type == IB_QPT_GSI ?
211		swqe->ud_wr.pkey_index : 0;
212	wc.slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
213				((1 << ppd->lmc) - 1));
214	wc.sl = rdma_ah_get_sl(ah_attr);
215	wc.dlid_path_bits = rdma_ah_get_dlid(ah_attr) & ((1 << ppd->lmc) - 1);
216	wc.port_num = qp->port_num;
217	/* Signal completion event if the solicited bit is set. */
218	rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
219		     swqe->wr.send_flags & IB_SEND_SOLICITED);
220	ibp->rvp.n_loop_pkts++;
221bail_unlock:
222	spin_unlock_irqrestore(&qp->r_lock, flags);
223drop:
224	rcu_read_unlock();
225}
226
227/**
228 * qib_make_ud_req - construct a UD request packet
229 * @qp: the QP
 
230 *
231 * Assumes the s_lock is held.
232 *
233 * Return 1 if constructed; otherwise, return 0.
234 */
235int qib_make_ud_req(struct rvt_qp *qp, unsigned long *flags)
236{
237	struct qib_qp_priv *priv = qp->priv;
238	struct ib_other_headers *ohdr;
239	struct rdma_ah_attr *ah_attr;
240	struct qib_pportdata *ppd;
241	struct qib_ibport *ibp;
242	struct rvt_swqe *wqe;
243	u32 nwords;
244	u32 extra_bytes;
245	u32 bth0;
246	u16 lrh0;
247	u16 lid;
248	int ret = 0;
249	int next_cur;
250
251	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
252		if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
253			goto bail;
254		/* We are in the error state, flush the work request. */
255		if (qp->s_last == READ_ONCE(qp->s_head))
256			goto bail;
257		/* If DMAs are in progress, we can't flush immediately. */
258		if (atomic_read(&priv->s_dma_busy)) {
259			qp->s_flags |= RVT_S_WAIT_DMA;
260			goto bail;
261		}
262		wqe = rvt_get_swqe_ptr(qp, qp->s_last);
263		qib_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
264		goto done;
265	}
266
267	/* see post_one_send() */
268	if (qp->s_cur == READ_ONCE(qp->s_head))
269		goto bail;
270
271	wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
272	next_cur = qp->s_cur + 1;
273	if (next_cur >= qp->s_size)
274		next_cur = 0;
275
276	/* Construct the header. */
277	ibp = to_iport(qp->ibqp.device, qp->port_num);
278	ppd = ppd_from_ibp(ibp);
279	ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
280	if (rdma_ah_get_dlid(ah_attr) >= be16_to_cpu(IB_MULTICAST_LID_BASE)) {
281		if (rdma_ah_get_dlid(ah_attr) !=
282				be16_to_cpu(IB_LID_PERMISSIVE))
283			this_cpu_inc(ibp->pmastats->n_multicast_xmit);
284		else
285			this_cpu_inc(ibp->pmastats->n_unicast_xmit);
286	} else {
287		this_cpu_inc(ibp->pmastats->n_unicast_xmit);
288		lid = rdma_ah_get_dlid(ah_attr) & ~((1 << ppd->lmc) - 1);
289		if (unlikely(lid == ppd->lid)) {
290			unsigned long tflags = *flags;
291			/*
292			 * If DMAs are in progress, we can't generate
293			 * a completion for the loopback packet since
294			 * it would be out of order.
295			 * XXX Instead of waiting, we could queue a
296			 * zero length descriptor so we get a callback.
297			 */
298			if (atomic_read(&priv->s_dma_busy)) {
299				qp->s_flags |= RVT_S_WAIT_DMA;
300				goto bail;
301			}
302			qp->s_cur = next_cur;
303			spin_unlock_irqrestore(&qp->s_lock, tflags);
304			qib_ud_loopback(qp, wqe);
305			spin_lock_irqsave(&qp->s_lock, tflags);
306			*flags = tflags;
307			qib_send_complete(qp, wqe, IB_WC_SUCCESS);
308			goto done;
309		}
310	}
311
312	qp->s_cur = next_cur;
313	extra_bytes = -wqe->length & 3;
314	nwords = (wqe->length + extra_bytes) >> 2;
315
316	/* header size in 32-bit words LRH+BTH+DETH = (8+12+8)/4. */
317	qp->s_hdrwords = 7;
318	qp->s_cur_size = wqe->length;
319	qp->s_cur_sge = &qp->s_sge;
320	qp->s_srate = rdma_ah_get_static_rate(ah_attr);
321	qp->s_wqe = wqe;
322	qp->s_sge.sge = wqe->sg_list[0];
323	qp->s_sge.sg_list = wqe->sg_list + 1;
324	qp->s_sge.num_sge = wqe->wr.num_sge;
325	qp->s_sge.total_len = wqe->length;
326
327	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
328		/* Header size in 32-bit words. */
329		qp->s_hdrwords += qib_make_grh(ibp, &priv->s_hdr->u.l.grh,
330					       rdma_ah_read_grh(ah_attr),
331					       qp->s_hdrwords, nwords);
332		lrh0 = QIB_LRH_GRH;
333		ohdr = &priv->s_hdr->u.l.oth;
334		/*
335		 * Don't worry about sending to locally attached multicast
336		 * QPs.  It is unspecified by the spec. what happens.
337		 */
338	} else {
339		/* Header size in 32-bit words. */
340		lrh0 = QIB_LRH_BTH;
341		ohdr = &priv->s_hdr->u.oth;
342	}
343	if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
344		qp->s_hdrwords++;
345		ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
346		bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
347	} else
348		bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
349	lrh0 |= rdma_ah_get_sl(ah_attr) << 4;
350	if (qp->ibqp.qp_type == IB_QPT_SMI)
351		lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
352	else
353		lrh0 |= ibp->sl_to_vl[rdma_ah_get_sl(ah_attr)] << 12;
354	priv->s_hdr->lrh[0] = cpu_to_be16(lrh0);
355	priv->s_hdr->lrh[1] =
356			cpu_to_be16(rdma_ah_get_dlid(ah_attr));  /* DEST LID */
357	priv->s_hdr->lrh[2] =
358			cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
359	lid = ppd->lid;
360	if (lid) {
361		lid |= rdma_ah_get_path_bits(ah_attr) &
362			((1 << ppd->lmc) - 1);
363		priv->s_hdr->lrh[3] = cpu_to_be16(lid);
364	} else
365		priv->s_hdr->lrh[3] = IB_LID_PERMISSIVE;
366	if (wqe->wr.send_flags & IB_SEND_SOLICITED)
367		bth0 |= IB_BTH_SOLICITED;
368	bth0 |= extra_bytes << 20;
369	bth0 |= qp->ibqp.qp_type == IB_QPT_SMI ? QIB_DEFAULT_P_KEY :
370		qib_get_pkey(ibp, qp->ibqp.qp_type == IB_QPT_GSI ?
371			     wqe->ud_wr.pkey_index : qp->s_pkey_index);
372	ohdr->bth[0] = cpu_to_be32(bth0);
373	/*
374	 * Use the multicast QP if the destination LID is a multicast LID.
375	 */
376	ohdr->bth[1] = rdma_ah_get_dlid(ah_attr) >=
377			be16_to_cpu(IB_MULTICAST_LID_BASE) &&
378		rdma_ah_get_dlid(ah_attr) != be16_to_cpu(IB_LID_PERMISSIVE) ?
379		cpu_to_be32(QIB_MULTICAST_QPN) :
380		cpu_to_be32(wqe->ud_wr.remote_qpn);
381	ohdr->bth[2] = cpu_to_be32(wqe->psn & QIB_PSN_MASK);
382	/*
383	 * Qkeys with the high order bit set mean use the
384	 * qkey from the QP context instead of the WR (see 10.2.5).
385	 */
386	ohdr->u.ud.deth[0] = cpu_to_be32((int)wqe->ud_wr.remote_qkey < 0 ?
387					 qp->qkey : wqe->ud_wr.remote_qkey);
 
388	ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
389
390done:
391	return 1;
392bail:
393	qp->s_flags &= ~RVT_S_BUSY;
394	return ret;
395}
396
397static unsigned qib_lookup_pkey(struct qib_ibport *ibp, u16 pkey)
398{
399	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
400	struct qib_devdata *dd = ppd->dd;
401	unsigned ctxt = ppd->hw_pidx;
402	unsigned i;
403
404	pkey &= 0x7fff;	/* remove limited/full membership bit */
405
406	for (i = 0; i < ARRAY_SIZE(dd->rcd[ctxt]->pkeys); ++i)
407		if ((dd->rcd[ctxt]->pkeys[i] & 0x7fff) == pkey)
408			return i;
409
410	/*
411	 * Should not get here, this means hardware failed to validate pkeys.
412	 * Punt and return index 0.
413	 */
414	return 0;
415}
416
417/**
418 * qib_ud_rcv - receive an incoming UD packet
419 * @ibp: the port the packet came in on
420 * @hdr: the packet header
421 * @has_grh: true if the packet has a GRH
422 * @data: the packet data
423 * @tlen: the packet length
424 * @qp: the QP the packet came on
425 *
426 * This is called from qib_qp_rcv() to process an incoming UD packet
427 * for the given QP.
428 * Called at interrupt level.
429 */
430void qib_ud_rcv(struct qib_ibport *ibp, struct ib_header *hdr,
431		int has_grh, void *data, u32 tlen, struct rvt_qp *qp)
432{
433	struct ib_other_headers *ohdr;
434	int opcode;
435	u32 hdrsize;
436	u32 pad;
437	struct ib_wc wc;
438	u32 qkey;
439	u32 src_qp;
440	u16 dlid;
441
442	/* Check for GRH */
443	if (!has_grh) {
444		ohdr = &hdr->u.oth;
445		hdrsize = 8 + 12 + 8;   /* LRH + BTH + DETH */
446	} else {
447		ohdr = &hdr->u.l.oth;
448		hdrsize = 8 + 40 + 12 + 8; /* LRH + GRH + BTH + DETH */
449	}
450	qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
451	src_qp = be32_to_cpu(ohdr->u.ud.deth[1]) & RVT_QPN_MASK;
452
453	/*
454	 * Get the number of bytes the message was padded by
455	 * and drop incomplete packets.
456	 */
457	pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
458	if (unlikely(tlen < (hdrsize + pad + 4)))
459		goto drop;
460
461	tlen -= hdrsize + pad + 4;
462
463	/*
464	 * Check that the permissive LID is only used on QP0
465	 * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
466	 */
467	if (qp->ibqp.qp_num) {
468		if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||
469			     hdr->lrh[3] == IB_LID_PERMISSIVE))
470			goto drop;
471		if (qp->ibqp.qp_num > 1) {
472			u16 pkey1, pkey2;
473
474			pkey1 = be32_to_cpu(ohdr->bth[0]);
475			pkey2 = qib_get_pkey(ibp, qp->s_pkey_index);
476			if (unlikely(!qib_pkey_ok(pkey1, pkey2))) {
477				qib_bad_pkey(ibp,
478					     pkey1,
479					     (be16_to_cpu(hdr->lrh[0]) >> 4) &
480						0xF,
481					     src_qp, qp->ibqp.qp_num,
482					     hdr->lrh[3], hdr->lrh[1]);
483				return;
484			}
485		}
486		if (unlikely(qkey != qp->qkey))
487			return;
488
489		/* Drop invalid MAD packets (see 13.5.3.1). */
490		if (unlikely(qp->ibqp.qp_num == 1 &&
491			     (tlen != 256 ||
492			      (be16_to_cpu(hdr->lrh[0]) >> 12) == 15)))
493			goto drop;
494	} else {
495		struct ib_smp *smp;
496
497		/* Drop invalid MAD packets (see 13.5.3.1). */
498		if (tlen != 256 || (be16_to_cpu(hdr->lrh[0]) >> 12) != 15)
499			goto drop;
500		smp = (struct ib_smp *) data;
501		if ((hdr->lrh[1] == IB_LID_PERMISSIVE ||
502		     hdr->lrh[3] == IB_LID_PERMISSIVE) &&
503		    smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
504			goto drop;
505	}
506
507	/*
508	 * The opcode is in the low byte when its in network order
509	 * (top byte when in host order).
510	 */
511	opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
512	if (qp->ibqp.qp_num > 1 &&
513	    opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
514		wc.ex.imm_data = ohdr->u.ud.imm_data;
515		wc.wc_flags = IB_WC_WITH_IMM;
516		tlen -= sizeof(u32);
517	} else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
518		wc.ex.imm_data = 0;
519		wc.wc_flags = 0;
520	} else
521		goto drop;
522
523	/*
524	 * A GRH is expected to precede the data even if not
525	 * present on the wire.
526	 */
527	wc.byte_len = tlen + sizeof(struct ib_grh);
528
529	/*
530	 * Get the next work request entry to find where to put the data.
531	 */
532	if (qp->r_flags & RVT_R_REUSE_SGE)
533		qp->r_flags &= ~RVT_R_REUSE_SGE;
534	else {
535		int ret;
536
537		ret = qib_get_rwqe(qp, 0);
538		if (ret < 0) {
539			rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
540			return;
541		}
542		if (!ret) {
543			if (qp->ibqp.qp_num == 0)
544				ibp->rvp.n_vl15_dropped++;
545			return;
546		}
547	}
548	/* Silently drop packets which are too big. */
549	if (unlikely(wc.byte_len > qp->r_len)) {
550		qp->r_flags |= RVT_R_REUSE_SGE;
551		goto drop;
552	}
553	if (has_grh) {
554		qib_copy_sge(&qp->r_sge, &hdr->u.l.grh,
555			     sizeof(struct ib_grh), 1);
556		wc.wc_flags |= IB_WC_GRH;
557	} else
558		rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
559	qib_copy_sge(&qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh), 1);
 
560	rvt_put_ss(&qp->r_sge);
561	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
562		return;
563	wc.wr_id = qp->r_wr_id;
564	wc.status = IB_WC_SUCCESS;
565	wc.opcode = IB_WC_RECV;
566	wc.vendor_err = 0;
567	wc.qp = &qp->ibqp;
568	wc.src_qp = src_qp;
569	wc.pkey_index = qp->ibqp.qp_type == IB_QPT_GSI ?
570		qib_lookup_pkey(ibp, be32_to_cpu(ohdr->bth[0])) : 0;
571	wc.slid = be16_to_cpu(hdr->lrh[3]);
572	wc.sl = (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF;
573	dlid = be16_to_cpu(hdr->lrh[1]);
574	/*
575	 * Save the LMC lower bits if the destination LID is a unicast LID.
576	 */
577	wc.dlid_path_bits = dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE) ? 0 :
578		dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
579	wc.port_num = qp->port_num;
580	/* Signal completion event if the solicited bit is set. */
581	rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
582		     ib_bth_is_solicited(ohdr));
583	return;
584
585drop:
586	ibp->rvp.n_pkt_drops++;
587}