xref: /linux/drivers/infiniband/hw/hfi1/ruc.c (revision 55f3538c4923e9dfca132e99ebec370e8094afda)
1 /*
2  * Copyright(c) 2015 - 2017 Intel Corporation.
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * BSD LICENSE
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  *
24  *  - Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  *  - Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in
28  *    the documentation and/or other materials provided with the
29  *    distribution.
30  *  - Neither the name of Intel Corporation nor the names of its
31  *    contributors may be used to endorse or promote products derived
32  *    from this software without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  */
47 
48 #include <linux/spinlock.h>
49 
50 #include "hfi.h"
51 #include "mad.h"
52 #include "qp.h"
53 #include "verbs_txreq.h"
54 #include "trace.h"
55 
56 /*
57  * Validate a RWQE and fill in the SGE state.
58  * Return 1 if OK.
59  */
60 static int init_sge(struct rvt_qp *qp, struct rvt_rwqe *wqe)
61 {
62 	int i, j, ret;
63 	struct ib_wc wc;
64 	struct rvt_lkey_table *rkt;
65 	struct rvt_pd *pd;
66 	struct rvt_sge_state *ss;
67 
68 	rkt = &to_idev(qp->ibqp.device)->rdi.lkey_table;
69 	pd = ibpd_to_rvtpd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd);
70 	ss = &qp->r_sge;
71 	ss->sg_list = qp->r_sg_list;
72 	qp->r_len = 0;
73 	for (i = j = 0; i < wqe->num_sge; i++) {
74 		if (wqe->sg_list[i].length == 0)
75 			continue;
76 		/* Check LKEY */
77 		ret = rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,
78 				  NULL, &wqe->sg_list[i],
79 				  IB_ACCESS_LOCAL_WRITE);
80 		if (unlikely(ret <= 0))
81 			goto bad_lkey;
82 		qp->r_len += wqe->sg_list[i].length;
83 		j++;
84 	}
85 	ss->num_sge = j;
86 	ss->total_len = qp->r_len;
87 	ret = 1;
88 	goto bail;
89 
90 bad_lkey:
91 	while (j) {
92 		struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;
93 
94 		rvt_put_mr(sge->mr);
95 	}
96 	ss->num_sge = 0;
97 	memset(&wc, 0, sizeof(wc));
98 	wc.wr_id = wqe->wr_id;
99 	wc.status = IB_WC_LOC_PROT_ERR;
100 	wc.opcode = IB_WC_RECV;
101 	wc.qp = &qp->ibqp;
102 	/* Signal solicited completion event. */
103 	rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
104 	ret = 0;
105 bail:
106 	return ret;
107 }
108 
109 /**
110  * hfi1_rvt_get_rwqe - copy the next RWQE into the QP's RWQE
111  * @qp: the QP
112  * @wr_id_only: update qp->r_wr_id only, not qp->r_sge
113  *
114  * Return -1 if there is a local error, 0 if no RWQE is available,
115  * otherwise return 1.
116  *
117  * Can be called from interrupt level.
118  */
119 int hfi1_rvt_get_rwqe(struct rvt_qp *qp, int wr_id_only)
120 {
121 	unsigned long flags;
122 	struct rvt_rq *rq;
123 	struct rvt_rwq *wq;
124 	struct rvt_srq *srq;
125 	struct rvt_rwqe *wqe;
126 	void (*handler)(struct ib_event *, void *);
127 	u32 tail;
128 	int ret;
129 
130 	if (qp->ibqp.srq) {
131 		srq = ibsrq_to_rvtsrq(qp->ibqp.srq);
132 		handler = srq->ibsrq.event_handler;
133 		rq = &srq->rq;
134 	} else {
135 		srq = NULL;
136 		handler = NULL;
137 		rq = &qp->r_rq;
138 	}
139 
140 	spin_lock_irqsave(&rq->lock, flags);
141 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
142 		ret = 0;
143 		goto unlock;
144 	}
145 
146 	wq = rq->wq;
147 	tail = wq->tail;
148 	/* Validate tail before using it since it is user writable. */
149 	if (tail >= rq->size)
150 		tail = 0;
151 	if (unlikely(tail == wq->head)) {
152 		ret = 0;
153 		goto unlock;
154 	}
155 	/* Make sure entry is read after head index is read. */
156 	smp_rmb();
157 	wqe = rvt_get_rwqe_ptr(rq, tail);
158 	/*
159 	 * Even though we update the tail index in memory, the verbs
160 	 * consumer is not supposed to post more entries until a
161 	 * completion is generated.
162 	 */
163 	if (++tail >= rq->size)
164 		tail = 0;
165 	wq->tail = tail;
166 	if (!wr_id_only && !init_sge(qp, wqe)) {
167 		ret = -1;
168 		goto unlock;
169 	}
170 	qp->r_wr_id = wqe->wr_id;
171 
172 	ret = 1;
173 	set_bit(RVT_R_WRID_VALID, &qp->r_aflags);
174 	if (handler) {
175 		u32 n;
176 
177 		/*
178 		 * Validate head pointer value and compute
179 		 * the number of remaining WQEs.
180 		 */
181 		n = wq->head;
182 		if (n >= rq->size)
183 			n = 0;
184 		if (n < tail)
185 			n += rq->size - tail;
186 		else
187 			n -= tail;
188 		if (n < srq->limit) {
189 			struct ib_event ev;
190 
191 			srq->limit = 0;
192 			spin_unlock_irqrestore(&rq->lock, flags);
193 			ev.device = qp->ibqp.device;
194 			ev.element.srq = qp->ibqp.srq;
195 			ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
196 			handler(&ev, srq->ibsrq.srq_context);
197 			goto bail;
198 		}
199 	}
200 unlock:
201 	spin_unlock_irqrestore(&rq->lock, flags);
202 bail:
203 	return ret;
204 }
205 
206 static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)
207 {
208 	return (gid->global.interface_id == id &&
209 		(gid->global.subnet_prefix == gid_prefix ||
210 		 gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));
211 }
212 
213 /*
214  *
215  * This should be called with the QP r_lock held.
216  *
217  * The s_lock will be acquired around the hfi1_migrate_qp() call.
218  */
219 int hfi1_ruc_check_hdr(struct hfi1_ibport *ibp, struct hfi1_packet *packet)
220 {
221 	__be64 guid;
222 	unsigned long flags;
223 	struct rvt_qp *qp = packet->qp;
224 	u8 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(&qp->remote_ah_attr)];
225 	u32 dlid = packet->dlid;
226 	u32 slid = packet->slid;
227 	u32 sl = packet->sl;
228 	int migrated;
229 	u32 bth0, bth1;
230 	u16 pkey;
231 
232 	bth0 = be32_to_cpu(packet->ohdr->bth[0]);
233 	bth1 = be32_to_cpu(packet->ohdr->bth[1]);
234 	if (packet->etype == RHF_RCV_TYPE_BYPASS) {
235 		pkey = hfi1_16B_get_pkey(packet->hdr);
236 		migrated = bth1 & OPA_BTH_MIG_REQ;
237 	} else {
238 		pkey = ib_bth_get_pkey(packet->ohdr);
239 		migrated = bth0 & IB_BTH_MIG_REQ;
240 	}
241 
242 	if (qp->s_mig_state == IB_MIG_ARMED && migrated) {
243 		if (!packet->grh) {
244 			if ((rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
245 			     IB_AH_GRH) &&
246 			    (packet->etype != RHF_RCV_TYPE_BYPASS))
247 				return 1;
248 		} else {
249 			const struct ib_global_route *grh;
250 
251 			if (!(rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
252 			      IB_AH_GRH))
253 				return 1;
254 			grh = rdma_ah_read_grh(&qp->alt_ah_attr);
255 			guid = get_sguid(ibp, grh->sgid_index);
256 			if (!gid_ok(&packet->grh->dgid, ibp->rvp.gid_prefix,
257 				    guid))
258 				return 1;
259 			if (!gid_ok(
260 				&packet->grh->sgid,
261 				grh->dgid.global.subnet_prefix,
262 				grh->dgid.global.interface_id))
263 				return 1;
264 		}
265 		if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), pkey,
266 					    sc5, slid))) {
267 			hfi1_bad_pkey(ibp, pkey, sl, 0, qp->ibqp.qp_num,
268 				      slid, dlid);
269 			return 1;
270 		}
271 		/* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
272 		if (slid != rdma_ah_get_dlid(&qp->alt_ah_attr) ||
273 		    ppd_from_ibp(ibp)->port !=
274 			rdma_ah_get_port_num(&qp->alt_ah_attr))
275 			return 1;
276 		spin_lock_irqsave(&qp->s_lock, flags);
277 		hfi1_migrate_qp(qp);
278 		spin_unlock_irqrestore(&qp->s_lock, flags);
279 	} else {
280 		if (!packet->grh) {
281 			if ((rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
282 			     IB_AH_GRH) &&
283 			    (packet->etype != RHF_RCV_TYPE_BYPASS))
284 				return 1;
285 		} else {
286 			const struct ib_global_route *grh;
287 
288 			if (!(rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
289 						   IB_AH_GRH))
290 				return 1;
291 			grh = rdma_ah_read_grh(&qp->remote_ah_attr);
292 			guid = get_sguid(ibp, grh->sgid_index);
293 			if (!gid_ok(&packet->grh->dgid, ibp->rvp.gid_prefix,
294 				    guid))
295 				return 1;
296 			if (!gid_ok(
297 			     &packet->grh->sgid,
298 			     grh->dgid.global.subnet_prefix,
299 			     grh->dgid.global.interface_id))
300 				return 1;
301 		}
302 		if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), pkey,
303 					    sc5, slid))) {
304 			hfi1_bad_pkey(ibp, pkey, sl, 0, qp->ibqp.qp_num,
305 				      slid, dlid);
306 			return 1;
307 		}
308 		/* Validate the SLID. See Ch. 9.6.1.5 */
309 		if ((slid != rdma_ah_get_dlid(&qp->remote_ah_attr)) ||
310 		    ppd_from_ibp(ibp)->port != qp->port_num)
311 			return 1;
312 		if (qp->s_mig_state == IB_MIG_REARM && !migrated)
313 			qp->s_mig_state = IB_MIG_ARMED;
314 	}
315 
316 	return 0;
317 }
318 
319 /**
320  * ruc_loopback - handle UC and RC loopback requests
321  * @sqp: the sending QP
322  *
323  * This is called from hfi1_do_send() to
324  * forward a WQE addressed to the same HFI.
325  * Note that although we are single threaded due to the send engine, we still
326  * have to protect against post_send().  We don't have to worry about
327  * receive interrupts since this is a connected protocol and all packets
328  * will pass through here.
329  */
330 static void ruc_loopback(struct rvt_qp *sqp)
331 {
332 	struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
333 	struct rvt_qp *qp;
334 	struct rvt_swqe *wqe;
335 	struct rvt_sge *sge;
336 	unsigned long flags;
337 	struct ib_wc wc;
338 	u64 sdata;
339 	atomic64_t *maddr;
340 	enum ib_wc_status send_status;
341 	bool release;
342 	int ret;
343 	bool copy_last = false;
344 	int local_ops = 0;
345 
346 	rcu_read_lock();
347 
348 	/*
349 	 * Note that we check the responder QP state after
350 	 * checking the requester's state.
351 	 */
352 	qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
353 			    sqp->remote_qpn);
354 
355 	spin_lock_irqsave(&sqp->s_lock, flags);
356 
357 	/* Return if we are already busy processing a work request. */
358 	if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
359 	    !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
360 		goto unlock;
361 
362 	sqp->s_flags |= RVT_S_BUSY;
363 
364 again:
365 	if (sqp->s_last == READ_ONCE(sqp->s_head))
366 		goto clr_busy;
367 	wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
368 
369 	/* Return if it is not OK to start a new work request. */
370 	if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
371 		if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
372 			goto clr_busy;
373 		/* We are in the error state, flush the work request. */
374 		send_status = IB_WC_WR_FLUSH_ERR;
375 		goto flush_send;
376 	}
377 
378 	/*
379 	 * We can rely on the entry not changing without the s_lock
380 	 * being held until we update s_last.
381 	 * We increment s_cur to indicate s_last is in progress.
382 	 */
383 	if (sqp->s_last == sqp->s_cur) {
384 		if (++sqp->s_cur >= sqp->s_size)
385 			sqp->s_cur = 0;
386 	}
387 	spin_unlock_irqrestore(&sqp->s_lock, flags);
388 
389 	if (!qp || !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
390 	    qp->ibqp.qp_type != sqp->ibqp.qp_type) {
391 		ibp->rvp.n_pkt_drops++;
392 		/*
393 		 * For RC, the requester would timeout and retry so
394 		 * shortcut the timeouts and just signal too many retries.
395 		 */
396 		if (sqp->ibqp.qp_type == IB_QPT_RC)
397 			send_status = IB_WC_RETRY_EXC_ERR;
398 		else
399 			send_status = IB_WC_SUCCESS;
400 		goto serr;
401 	}
402 
403 	memset(&wc, 0, sizeof(wc));
404 	send_status = IB_WC_SUCCESS;
405 
406 	release = true;
407 	sqp->s_sge.sge = wqe->sg_list[0];
408 	sqp->s_sge.sg_list = wqe->sg_list + 1;
409 	sqp->s_sge.num_sge = wqe->wr.num_sge;
410 	sqp->s_len = wqe->length;
411 	switch (wqe->wr.opcode) {
412 	case IB_WR_REG_MR:
413 		goto send_comp;
414 
415 	case IB_WR_LOCAL_INV:
416 		if (!(wqe->wr.send_flags & RVT_SEND_COMPLETION_ONLY)) {
417 			if (rvt_invalidate_rkey(sqp,
418 						wqe->wr.ex.invalidate_rkey))
419 				send_status = IB_WC_LOC_PROT_ERR;
420 			local_ops = 1;
421 		}
422 		goto send_comp;
423 
424 	case IB_WR_SEND_WITH_INV:
425 		if (!rvt_invalidate_rkey(qp, wqe->wr.ex.invalidate_rkey)) {
426 			wc.wc_flags = IB_WC_WITH_INVALIDATE;
427 			wc.ex.invalidate_rkey = wqe->wr.ex.invalidate_rkey;
428 		}
429 		goto send;
430 
431 	case IB_WR_SEND_WITH_IMM:
432 		wc.wc_flags = IB_WC_WITH_IMM;
433 		wc.ex.imm_data = wqe->wr.ex.imm_data;
434 		/* FALLTHROUGH */
435 	case IB_WR_SEND:
436 send:
437 		ret = hfi1_rvt_get_rwqe(qp, 0);
438 		if (ret < 0)
439 			goto op_err;
440 		if (!ret)
441 			goto rnr_nak;
442 		break;
443 
444 	case IB_WR_RDMA_WRITE_WITH_IMM:
445 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
446 			goto inv_err;
447 		wc.wc_flags = IB_WC_WITH_IMM;
448 		wc.ex.imm_data = wqe->wr.ex.imm_data;
449 		ret = hfi1_rvt_get_rwqe(qp, 1);
450 		if (ret < 0)
451 			goto op_err;
452 		if (!ret)
453 			goto rnr_nak;
454 		/* skip copy_last set and qp_access_flags recheck */
455 		goto do_write;
456 	case IB_WR_RDMA_WRITE:
457 		copy_last = rvt_is_user_qp(qp);
458 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
459 			goto inv_err;
460 do_write:
461 		if (wqe->length == 0)
462 			break;
463 		if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
464 					  wqe->rdma_wr.remote_addr,
465 					  wqe->rdma_wr.rkey,
466 					  IB_ACCESS_REMOTE_WRITE)))
467 			goto acc_err;
468 		qp->r_sge.sg_list = NULL;
469 		qp->r_sge.num_sge = 1;
470 		qp->r_sge.total_len = wqe->length;
471 		break;
472 
473 	case IB_WR_RDMA_READ:
474 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
475 			goto inv_err;
476 		if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
477 					  wqe->rdma_wr.remote_addr,
478 					  wqe->rdma_wr.rkey,
479 					  IB_ACCESS_REMOTE_READ)))
480 			goto acc_err;
481 		release = false;
482 		sqp->s_sge.sg_list = NULL;
483 		sqp->s_sge.num_sge = 1;
484 		qp->r_sge.sge = wqe->sg_list[0];
485 		qp->r_sge.sg_list = wqe->sg_list + 1;
486 		qp->r_sge.num_sge = wqe->wr.num_sge;
487 		qp->r_sge.total_len = wqe->length;
488 		break;
489 
490 	case IB_WR_ATOMIC_CMP_AND_SWP:
491 	case IB_WR_ATOMIC_FETCH_AND_ADD:
492 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
493 			goto inv_err;
494 		if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
495 					  wqe->atomic_wr.remote_addr,
496 					  wqe->atomic_wr.rkey,
497 					  IB_ACCESS_REMOTE_ATOMIC)))
498 			goto acc_err;
499 		/* Perform atomic OP and save result. */
500 		maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
501 		sdata = wqe->atomic_wr.compare_add;
502 		*(u64 *)sqp->s_sge.sge.vaddr =
503 			(wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
504 			(u64)atomic64_add_return(sdata, maddr) - sdata :
505 			(u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
506 				      sdata, wqe->atomic_wr.swap);
507 		rvt_put_mr(qp->r_sge.sge.mr);
508 		qp->r_sge.num_sge = 0;
509 		goto send_comp;
510 
511 	default:
512 		send_status = IB_WC_LOC_QP_OP_ERR;
513 		goto serr;
514 	}
515 
516 	sge = &sqp->s_sge.sge;
517 	while (sqp->s_len) {
518 		u32 len = sqp->s_len;
519 
520 		if (len > sge->length)
521 			len = sge->length;
522 		if (len > sge->sge_length)
523 			len = sge->sge_length;
524 		WARN_ON_ONCE(len == 0);
525 		hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, release, copy_last);
526 		sge->vaddr += len;
527 		sge->length -= len;
528 		sge->sge_length -= len;
529 		if (sge->sge_length == 0) {
530 			if (!release)
531 				rvt_put_mr(sge->mr);
532 			if (--sqp->s_sge.num_sge)
533 				*sge = *sqp->s_sge.sg_list++;
534 		} else if (sge->length == 0 && sge->mr->lkey) {
535 			if (++sge->n >= RVT_SEGSZ) {
536 				if (++sge->m >= sge->mr->mapsz)
537 					break;
538 				sge->n = 0;
539 			}
540 			sge->vaddr =
541 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
542 			sge->length =
543 				sge->mr->map[sge->m]->segs[sge->n].length;
544 		}
545 		sqp->s_len -= len;
546 	}
547 	if (release)
548 		rvt_put_ss(&qp->r_sge);
549 
550 	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
551 		goto send_comp;
552 
553 	if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
554 		wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
555 	else
556 		wc.opcode = IB_WC_RECV;
557 	wc.wr_id = qp->r_wr_id;
558 	wc.status = IB_WC_SUCCESS;
559 	wc.byte_len = wqe->length;
560 	wc.qp = &qp->ibqp;
561 	wc.src_qp = qp->remote_qpn;
562 	wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr) & U16_MAX;
563 	wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
564 	wc.port_num = 1;
565 	/* Signal completion event if the solicited bit is set. */
566 	rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
567 		     wqe->wr.send_flags & IB_SEND_SOLICITED);
568 
569 send_comp:
570 	spin_lock_irqsave(&sqp->s_lock, flags);
571 	ibp->rvp.n_loop_pkts++;
572 flush_send:
573 	sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
574 	hfi1_send_complete(sqp, wqe, send_status);
575 	if (local_ops) {
576 		atomic_dec(&sqp->local_ops_pending);
577 		local_ops = 0;
578 	}
579 	goto again;
580 
581 rnr_nak:
582 	/* Handle RNR NAK */
583 	if (qp->ibqp.qp_type == IB_QPT_UC)
584 		goto send_comp;
585 	ibp->rvp.n_rnr_naks++;
586 	/*
587 	 * Note: we don't need the s_lock held since the BUSY flag
588 	 * makes this single threaded.
589 	 */
590 	if (sqp->s_rnr_retry == 0) {
591 		send_status = IB_WC_RNR_RETRY_EXC_ERR;
592 		goto serr;
593 	}
594 	if (sqp->s_rnr_retry_cnt < 7)
595 		sqp->s_rnr_retry--;
596 	spin_lock_irqsave(&sqp->s_lock, flags);
597 	if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
598 		goto clr_busy;
599 	rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer <<
600 				IB_AETH_CREDIT_SHIFT);
601 	goto clr_busy;
602 
603 op_err:
604 	send_status = IB_WC_REM_OP_ERR;
605 	wc.status = IB_WC_LOC_QP_OP_ERR;
606 	goto err;
607 
608 inv_err:
609 	send_status = IB_WC_REM_INV_REQ_ERR;
610 	wc.status = IB_WC_LOC_QP_OP_ERR;
611 	goto err;
612 
613 acc_err:
614 	send_status = IB_WC_REM_ACCESS_ERR;
615 	wc.status = IB_WC_LOC_PROT_ERR;
616 err:
617 	/* responder goes to error state */
618 	rvt_rc_error(qp, wc.status);
619 
620 serr:
621 	spin_lock_irqsave(&sqp->s_lock, flags);
622 	hfi1_send_complete(sqp, wqe, send_status);
623 	if (sqp->ibqp.qp_type == IB_QPT_RC) {
624 		int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
625 
626 		sqp->s_flags &= ~RVT_S_BUSY;
627 		spin_unlock_irqrestore(&sqp->s_lock, flags);
628 		if (lastwqe) {
629 			struct ib_event ev;
630 
631 			ev.device = sqp->ibqp.device;
632 			ev.element.qp = &sqp->ibqp;
633 			ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
634 			sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
635 		}
636 		goto done;
637 	}
638 clr_busy:
639 	sqp->s_flags &= ~RVT_S_BUSY;
640 unlock:
641 	spin_unlock_irqrestore(&sqp->s_lock, flags);
642 done:
643 	rcu_read_unlock();
644 }
645 
646 /**
647  * hfi1_make_grh - construct a GRH header
648  * @ibp: a pointer to the IB port
649  * @hdr: a pointer to the GRH header being constructed
650  * @grh: the global route address to send to
651  * @hwords: size of header after grh being sent in dwords
652  * @nwords: the number of 32 bit words of data being sent
653  *
654  * Return the size of the header in 32 bit words.
655  */
656 u32 hfi1_make_grh(struct hfi1_ibport *ibp, struct ib_grh *hdr,
657 		  const struct ib_global_route *grh, u32 hwords, u32 nwords)
658 {
659 	hdr->version_tclass_flow =
660 		cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
661 			    (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
662 			    (grh->flow_label << IB_GRH_FLOW_SHIFT));
663 	hdr->paylen = cpu_to_be16((hwords + nwords) << 2);
664 	/* next_hdr is defined by C8-7 in ch. 8.4.1 */
665 	hdr->next_hdr = IB_GRH_NEXT_HDR;
666 	hdr->hop_limit = grh->hop_limit;
667 	/* The SGID is 32-bit aligned. */
668 	hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
669 	hdr->sgid.global.interface_id =
670 		grh->sgid_index < HFI1_GUIDS_PER_PORT ?
671 		get_sguid(ibp, grh->sgid_index) :
672 		get_sguid(ibp, HFI1_PORT_GUID_INDEX);
673 	hdr->dgid = grh->dgid;
674 
675 	/* GRH header size in 32-bit words. */
676 	return sizeof(struct ib_grh) / sizeof(u32);
677 }
678 
679 #define BTH2_OFFSET (offsetof(struct hfi1_sdma_header, \
680 			      hdr.ibh.u.oth.bth[2]) / 4)
681 
682 /**
683  * build_ahg - create ahg in s_ahg
684  * @qp: a pointer to QP
685  * @npsn: the next PSN for the request/response
686  *
687  * This routine handles the AHG by allocating an ahg entry and causing the
688  * copy of the first middle.
689  *
690  * Subsequent middles use the copied entry, editing the
691  * PSN with 1 or 2 edits.
692  */
693 static inline void build_ahg(struct rvt_qp *qp, u32 npsn)
694 {
695 	struct hfi1_qp_priv *priv = qp->priv;
696 
697 	if (unlikely(qp->s_flags & RVT_S_AHG_CLEAR))
698 		clear_ahg(qp);
699 	if (!(qp->s_flags & RVT_S_AHG_VALID)) {
700 		/* first middle that needs copy  */
701 		if (qp->s_ahgidx < 0)
702 			qp->s_ahgidx = sdma_ahg_alloc(priv->s_sde);
703 		if (qp->s_ahgidx >= 0) {
704 			qp->s_ahgpsn = npsn;
705 			priv->s_ahg->tx_flags |= SDMA_TXREQ_F_AHG_COPY;
706 			/* save to protect a change in another thread */
707 			priv->s_ahg->ahgidx = qp->s_ahgidx;
708 			qp->s_flags |= RVT_S_AHG_VALID;
709 		}
710 	} else {
711 		/* subsequent middle after valid */
712 		if (qp->s_ahgidx >= 0) {
713 			priv->s_ahg->tx_flags |= SDMA_TXREQ_F_USE_AHG;
714 			priv->s_ahg->ahgidx = qp->s_ahgidx;
715 			priv->s_ahg->ahgcount++;
716 			priv->s_ahg->ahgdesc[0] =
717 				sdma_build_ahg_descriptor(
718 					(__force u16)cpu_to_be16((u16)npsn),
719 					BTH2_OFFSET,
720 					16,
721 					16);
722 			if ((npsn & 0xffff0000) !=
723 					(qp->s_ahgpsn & 0xffff0000)) {
724 				priv->s_ahg->ahgcount++;
725 				priv->s_ahg->ahgdesc[1] =
726 					sdma_build_ahg_descriptor(
727 						(__force u16)cpu_to_be16(
728 							(u16)(npsn >> 16)),
729 						BTH2_OFFSET,
730 						0,
731 						16);
732 			}
733 		}
734 	}
735 }
736 
737 static inline void hfi1_make_ruc_bth(struct rvt_qp *qp,
738 				     struct ib_other_headers *ohdr,
739 				     u32 bth0, u32 bth1, u32 bth2)
740 {
741 	bth1 |= qp->remote_qpn;
742 	ohdr->bth[0] = cpu_to_be32(bth0);
743 	ohdr->bth[1] = cpu_to_be32(bth1);
744 	ohdr->bth[2] = cpu_to_be32(bth2);
745 }
746 
747 static inline void hfi1_make_ruc_header_16B(struct rvt_qp *qp,
748 					    struct ib_other_headers *ohdr,
749 					    u32 bth0, u32 bth2, int middle,
750 					    struct hfi1_pkt_state *ps)
751 {
752 	struct hfi1_qp_priv *priv = qp->priv;
753 	struct hfi1_ibport *ibp = ps->ibp;
754 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
755 	u32 bth1 = 0;
756 	u32 slid;
757 	u16 pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
758 	u8 l4 = OPA_16B_L4_IB_LOCAL;
759 	u8 extra_bytes = hfi1_get_16b_padding((qp->s_hdrwords << 2),
760 				   ps->s_txreq->s_cur_size);
761 	u32 nwords = SIZE_OF_CRC + ((ps->s_txreq->s_cur_size +
762 				 extra_bytes + SIZE_OF_LT) >> 2);
763 	u8 becn = 0;
764 
765 	if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH) &&
766 	    hfi1_check_mcast(rdma_ah_get_dlid(&qp->remote_ah_attr))) {
767 		struct ib_grh *grh;
768 		struct ib_global_route *grd =
769 			rdma_ah_retrieve_grh(&qp->remote_ah_attr);
770 		int hdrwords;
771 
772 		/*
773 		 * Ensure OPA GIDs are transformed to IB gids
774 		 * before creating the GRH.
775 		 */
776 		if (grd->sgid_index == OPA_GID_INDEX)
777 			grd->sgid_index = 0;
778 		grh = &ps->s_txreq->phdr.hdr.opah.u.l.grh;
779 		l4 = OPA_16B_L4_IB_GLOBAL;
780 		hdrwords = qp->s_hdrwords - 4;
781 		qp->s_hdrwords += hfi1_make_grh(ibp, grh, grd,
782 						hdrwords, nwords);
783 		middle = 0;
784 	}
785 
786 	if (qp->s_mig_state == IB_MIG_MIGRATED)
787 		bth1 |= OPA_BTH_MIG_REQ;
788 	else
789 		middle = 0;
790 
791 	if (middle)
792 		build_ahg(qp, bth2);
793 	else
794 		qp->s_flags &= ~RVT_S_AHG_VALID;
795 
796 	bth0 |= pkey;
797 	bth0 |= extra_bytes << 20;
798 	if (qp->s_flags & RVT_S_ECN) {
799 		qp->s_flags &= ~RVT_S_ECN;
800 		/* we recently received a FECN, so return a BECN */
801 		becn = 1;
802 	}
803 	hfi1_make_ruc_bth(qp, ohdr, bth0, bth1, bth2);
804 
805 	if (!ppd->lid)
806 		slid = be32_to_cpu(OPA_LID_PERMISSIVE);
807 	else
808 		slid = ppd->lid |
809 			(rdma_ah_get_path_bits(&qp->remote_ah_attr) &
810 			((1 << ppd->lmc) - 1));
811 
812 	hfi1_make_16b_hdr(&ps->s_txreq->phdr.hdr.opah,
813 			  slid,
814 			  opa_get_lid(rdma_ah_get_dlid(&qp->remote_ah_attr),
815 				      16B),
816 			  (qp->s_hdrwords + nwords) >> 1,
817 			  pkey, becn, 0, l4, priv->s_sc);
818 }
819 
820 static inline void hfi1_make_ruc_header_9B(struct rvt_qp *qp,
821 					   struct ib_other_headers *ohdr,
822 					   u32 bth0, u32 bth2, int middle,
823 					   struct hfi1_pkt_state *ps)
824 {
825 	struct hfi1_qp_priv *priv = qp->priv;
826 	struct hfi1_ibport *ibp = ps->ibp;
827 	u32 bth1 = 0;
828 	u16 pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
829 	u16 lrh0 = HFI1_LRH_BTH;
830 	u8 extra_bytes = -ps->s_txreq->s_cur_size & 3;
831 	u32 nwords = SIZE_OF_CRC + ((ps->s_txreq->s_cur_size +
832 					 extra_bytes) >> 2);
833 
834 	if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)) {
835 		struct ib_grh *grh = &ps->s_txreq->phdr.hdr.ibh.u.l.grh;
836 		int hdrwords = qp->s_hdrwords - 2;
837 
838 		lrh0 = HFI1_LRH_GRH;
839 		qp->s_hdrwords +=
840 			hfi1_make_grh(ibp, grh,
841 				      rdma_ah_read_grh(&qp->remote_ah_attr),
842 				      hdrwords, nwords);
843 		middle = 0;
844 	}
845 	lrh0 |= (priv->s_sc & 0xf) << 12 |
846 		(rdma_ah_get_sl(&qp->remote_ah_attr) & 0xf) << 4;
847 
848 	if (qp->s_mig_state == IB_MIG_MIGRATED)
849 		bth0 |= IB_BTH_MIG_REQ;
850 	else
851 		middle = 0;
852 
853 	if (middle)
854 		build_ahg(qp, bth2);
855 	else
856 		qp->s_flags &= ~RVT_S_AHG_VALID;
857 
858 	bth0 |= pkey;
859 	bth0 |= extra_bytes << 20;
860 	if (qp->s_flags & RVT_S_ECN) {
861 		qp->s_flags &= ~RVT_S_ECN;
862 		/* we recently received a FECN, so return a BECN */
863 		bth1 |= (IB_BECN_MASK << IB_BECN_SHIFT);
864 	}
865 	hfi1_make_ruc_bth(qp, ohdr, bth0, bth1, bth2);
866 	hfi1_make_ib_hdr(&ps->s_txreq->phdr.hdr.ibh,
867 			 lrh0,
868 			 qp->s_hdrwords + nwords,
869 			 opa_get_lid(rdma_ah_get_dlid(&qp->remote_ah_attr), 9B),
870 			 ppd_from_ibp(ibp)->lid |
871 				rdma_ah_get_path_bits(&qp->remote_ah_attr));
872 }
873 
874 typedef void (*hfi1_make_ruc_hdr)(struct rvt_qp *qp,
875 				  struct ib_other_headers *ohdr,
876 				  u32 bth0, u32 bth2, int middle,
877 				  struct hfi1_pkt_state *ps);
878 
879 /* We support only two types - 9B and 16B for now */
880 static const hfi1_make_ruc_hdr hfi1_ruc_header_tbl[2] = {
881 	[HFI1_PKT_TYPE_9B] = &hfi1_make_ruc_header_9B,
882 	[HFI1_PKT_TYPE_16B] = &hfi1_make_ruc_header_16B
883 };
884 
885 void hfi1_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
886 			  u32 bth0, u32 bth2, int middle,
887 			  struct hfi1_pkt_state *ps)
888 {
889 	struct hfi1_qp_priv *priv = qp->priv;
890 
891 	/*
892 	 * reset s_ahg/AHG fields
893 	 *
894 	 * This insures that the ahgentry/ahgcount
895 	 * are at a non-AHG default to protect
896 	 * build_verbs_tx_desc() from using
897 	 * an include ahgidx.
898 	 *
899 	 * build_ahg() will modify as appropriate
900 	 * to use the AHG feature.
901 	 */
902 	priv->s_ahg->tx_flags = 0;
903 	priv->s_ahg->ahgcount = 0;
904 	priv->s_ahg->ahgidx = 0;
905 
906 	/* Make the appropriate header */
907 	hfi1_ruc_header_tbl[priv->hdr_type](qp, ohdr, bth0, bth2, middle, ps);
908 }
909 
910 /* when sending, force a reschedule every one of these periods */
911 #define SEND_RESCHED_TIMEOUT (5 * HZ)  /* 5s in jiffies */
912 
913 /**
914  * schedule_send_yield - test for a yield required for QP send engine
915  * @timeout: Final time for timeout slice for jiffies
916  * @qp: a pointer to QP
917  * @ps: a pointer to a structure with commonly lookup values for
918  *      the the send engine progress
919  *
920  * This routine checks if the time slice for the QP has expired
921  * for RC QPs, if so an additional work entry is queued. At this
922  * point, other QPs have an opportunity to be scheduled. It
923  * returns true if a yield is required, otherwise, false
924  * is returned.
925  */
926 static bool schedule_send_yield(struct rvt_qp *qp,
927 				struct hfi1_pkt_state *ps)
928 {
929 	ps->pkts_sent = true;
930 
931 	if (unlikely(time_after(jiffies, ps->timeout))) {
932 		if (!ps->in_thread ||
933 		    workqueue_congested(ps->cpu, ps->ppd->hfi1_wq)) {
934 			spin_lock_irqsave(&qp->s_lock, ps->flags);
935 			qp->s_flags &= ~RVT_S_BUSY;
936 			hfi1_schedule_send(qp);
937 			spin_unlock_irqrestore(&qp->s_lock, ps->flags);
938 			this_cpu_inc(*ps->ppd->dd->send_schedule);
939 			trace_hfi1_rc_expired_time_slice(qp, true);
940 			return true;
941 		}
942 
943 		cond_resched();
944 		this_cpu_inc(*ps->ppd->dd->send_schedule);
945 		ps->timeout = jiffies + ps->timeout_int;
946 	}
947 
948 	trace_hfi1_rc_expired_time_slice(qp, false);
949 	return false;
950 }
951 
952 void hfi1_do_send_from_rvt(struct rvt_qp *qp)
953 {
954 	hfi1_do_send(qp, false);
955 }
956 
957 void _hfi1_do_send(struct work_struct *work)
958 {
959 	struct iowait *wait = container_of(work, struct iowait, iowork);
960 	struct rvt_qp *qp = iowait_to_qp(wait);
961 
962 	hfi1_do_send(qp, true);
963 }
964 
965 /**
966  * hfi1_do_send - perform a send on a QP
967  * @work: contains a pointer to the QP
968  * @in_thread: true if in a workqueue thread
969  *
970  * Process entries in the send work queue until credit or queue is
971  * exhausted.  Only allow one CPU to send a packet per QP.
972  * Otherwise, two threads could send packets out of order.
973  */
974 void hfi1_do_send(struct rvt_qp *qp, bool in_thread)
975 {
976 	struct hfi1_pkt_state ps;
977 	struct hfi1_qp_priv *priv = qp->priv;
978 	int (*make_req)(struct rvt_qp *qp, struct hfi1_pkt_state *ps);
979 
980 	ps.dev = to_idev(qp->ibqp.device);
981 	ps.ibp = to_iport(qp->ibqp.device, qp->port_num);
982 	ps.ppd = ppd_from_ibp(ps.ibp);
983 	ps.in_thread = in_thread;
984 
985 	trace_hfi1_rc_do_send(qp, in_thread);
986 
987 	switch (qp->ibqp.qp_type) {
988 	case IB_QPT_RC:
989 		if (!loopback && ((rdma_ah_get_dlid(&qp->remote_ah_attr) &
990 				   ~((1 << ps.ppd->lmc) - 1)) ==
991 				  ps.ppd->lid)) {
992 			ruc_loopback(qp);
993 			return;
994 		}
995 		make_req = hfi1_make_rc_req;
996 		ps.timeout_int = qp->timeout_jiffies;
997 		break;
998 	case IB_QPT_UC:
999 		if (!loopback && ((rdma_ah_get_dlid(&qp->remote_ah_attr) &
1000 				   ~((1 << ps.ppd->lmc) - 1)) ==
1001 				  ps.ppd->lid)) {
1002 			ruc_loopback(qp);
1003 			return;
1004 		}
1005 		make_req = hfi1_make_uc_req;
1006 		ps.timeout_int = SEND_RESCHED_TIMEOUT;
1007 		break;
1008 	default:
1009 		make_req = hfi1_make_ud_req;
1010 		ps.timeout_int = SEND_RESCHED_TIMEOUT;
1011 	}
1012 
1013 	spin_lock_irqsave(&qp->s_lock, ps.flags);
1014 
1015 	/* Return if we are already busy processing a work request. */
1016 	if (!hfi1_send_ok(qp)) {
1017 		spin_unlock_irqrestore(&qp->s_lock, ps.flags);
1018 		return;
1019 	}
1020 
1021 	qp->s_flags |= RVT_S_BUSY;
1022 
1023 	ps.timeout_int = ps.timeout_int / 8;
1024 	ps.timeout = jiffies + ps.timeout_int;
1025 	ps.cpu = priv->s_sde ? priv->s_sde->cpu :
1026 			cpumask_first(cpumask_of_node(ps.ppd->dd->node));
1027 	ps.pkts_sent = false;
1028 
1029 	/* insure a pre-built packet is handled  */
1030 	ps.s_txreq = get_waiting_verbs_txreq(qp);
1031 	do {
1032 		/* Check for a constructed packet to be sent. */
1033 		if (qp->s_hdrwords != 0) {
1034 			spin_unlock_irqrestore(&qp->s_lock, ps.flags);
1035 			/*
1036 			 * If the packet cannot be sent now, return and
1037 			 * the send engine will be woken up later.
1038 			 */
1039 			if (hfi1_verbs_send(qp, &ps))
1040 				return;
1041 			/* Record that s_ahg is empty. */
1042 			qp->s_hdrwords = 0;
1043 			/* allow other tasks to run */
1044 			if (schedule_send_yield(qp, &ps))
1045 				return;
1046 
1047 			spin_lock_irqsave(&qp->s_lock, ps.flags);
1048 		}
1049 	} while (make_req(qp, &ps));
1050 	iowait_starve_clear(ps.pkts_sent, &priv->s_iowait);
1051 	spin_unlock_irqrestore(&qp->s_lock, ps.flags);
1052 }
1053 
1054 /*
1055  * This should be called with s_lock held.
1056  */
1057 void hfi1_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
1058 			enum ib_wc_status status)
1059 {
1060 	u32 old_last, last;
1061 
1062 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
1063 		return;
1064 
1065 	last = qp->s_last;
1066 	old_last = last;
1067 	trace_hfi1_qp_send_completion(qp, wqe, last);
1068 	if (++last >= qp->s_size)
1069 		last = 0;
1070 	trace_hfi1_qp_send_completion(qp, wqe, last);
1071 	qp->s_last = last;
1072 	/* See post_send() */
1073 	barrier();
1074 	rvt_put_swqe(wqe);
1075 	if (qp->ibqp.qp_type == IB_QPT_UD ||
1076 	    qp->ibqp.qp_type == IB_QPT_SMI ||
1077 	    qp->ibqp.qp_type == IB_QPT_GSI)
1078 		atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
1079 
1080 	rvt_qp_swqe_complete(qp,
1081 			     wqe,
1082 			     ib_hfi1_wc_opcode[wqe->wr.opcode],
1083 			     status);
1084 
1085 	if (qp->s_acked == old_last)
1086 		qp->s_acked = last;
1087 	if (qp->s_cur == old_last)
1088 		qp->s_cur = last;
1089 	if (qp->s_tail == old_last)
1090 		qp->s_tail = last;
1091 	if (qp->state == IB_QPS_SQD && last == qp->s_cur)
1092 		qp->s_draining = 0;
1093 }
1094