xref: /openbmc/linux/drivers/infiniband/hw/hfi1/ruc.c (revision 5ef12cb4a3a78ffb331c03a795a15eea4ae35155)
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 	bool migrated = packet->migrated;
229 	u16 pkey = packet->pkey;
230 
231 	if (qp->s_mig_state == IB_MIG_ARMED && migrated) {
232 		if (!packet->grh) {
233 			if ((rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
234 			     IB_AH_GRH) &&
235 			    (packet->etype != RHF_RCV_TYPE_BYPASS))
236 				return 1;
237 		} else {
238 			const struct ib_global_route *grh;
239 
240 			if (!(rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
241 			      IB_AH_GRH))
242 				return 1;
243 			grh = rdma_ah_read_grh(&qp->alt_ah_attr);
244 			guid = get_sguid(ibp, grh->sgid_index);
245 			if (!gid_ok(&packet->grh->dgid, ibp->rvp.gid_prefix,
246 				    guid))
247 				return 1;
248 			if (!gid_ok(
249 				&packet->grh->sgid,
250 				grh->dgid.global.subnet_prefix,
251 				grh->dgid.global.interface_id))
252 				return 1;
253 		}
254 		if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), pkey,
255 					    sc5, slid))) {
256 			hfi1_bad_pkey(ibp, pkey, sl, 0, qp->ibqp.qp_num,
257 				      slid, dlid);
258 			return 1;
259 		}
260 		/* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
261 		if (slid != rdma_ah_get_dlid(&qp->alt_ah_attr) ||
262 		    ppd_from_ibp(ibp)->port !=
263 			rdma_ah_get_port_num(&qp->alt_ah_attr))
264 			return 1;
265 		spin_lock_irqsave(&qp->s_lock, flags);
266 		hfi1_migrate_qp(qp);
267 		spin_unlock_irqrestore(&qp->s_lock, flags);
268 	} else {
269 		if (!packet->grh) {
270 			if ((rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
271 			     IB_AH_GRH) &&
272 			    (packet->etype != RHF_RCV_TYPE_BYPASS))
273 				return 1;
274 		} else {
275 			const struct ib_global_route *grh;
276 
277 			if (!(rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
278 						   IB_AH_GRH))
279 				return 1;
280 			grh = rdma_ah_read_grh(&qp->remote_ah_attr);
281 			guid = get_sguid(ibp, grh->sgid_index);
282 			if (!gid_ok(&packet->grh->dgid, ibp->rvp.gid_prefix,
283 				    guid))
284 				return 1;
285 			if (!gid_ok(
286 			     &packet->grh->sgid,
287 			     grh->dgid.global.subnet_prefix,
288 			     grh->dgid.global.interface_id))
289 				return 1;
290 		}
291 		if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), pkey,
292 					    sc5, slid))) {
293 			hfi1_bad_pkey(ibp, pkey, sl, 0, qp->ibqp.qp_num,
294 				      slid, dlid);
295 			return 1;
296 		}
297 		/* Validate the SLID. See Ch. 9.6.1.5 */
298 		if ((slid != rdma_ah_get_dlid(&qp->remote_ah_attr)) ||
299 		    ppd_from_ibp(ibp)->port != qp->port_num)
300 			return 1;
301 		if (qp->s_mig_state == IB_MIG_REARM && !migrated)
302 			qp->s_mig_state = IB_MIG_ARMED;
303 	}
304 
305 	return 0;
306 }
307 
308 /**
309  * ruc_loopback - handle UC and RC loopback requests
310  * @sqp: the sending QP
311  *
312  * This is called from hfi1_do_send() to
313  * forward a WQE addressed to the same HFI.
314  * Note that although we are single threaded due to the send engine, we still
315  * have to protect against post_send().  We don't have to worry about
316  * receive interrupts since this is a connected protocol and all packets
317  * will pass through here.
318  */
319 static void ruc_loopback(struct rvt_qp *sqp)
320 {
321 	struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
322 	struct rvt_qp *qp;
323 	struct rvt_swqe *wqe;
324 	struct rvt_sge *sge;
325 	unsigned long flags;
326 	struct ib_wc wc;
327 	u64 sdata;
328 	atomic64_t *maddr;
329 	enum ib_wc_status send_status;
330 	bool release;
331 	int ret;
332 	bool copy_last = false;
333 	int local_ops = 0;
334 
335 	rcu_read_lock();
336 
337 	/*
338 	 * Note that we check the responder QP state after
339 	 * checking the requester's state.
340 	 */
341 	qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
342 			    sqp->remote_qpn);
343 
344 	spin_lock_irqsave(&sqp->s_lock, flags);
345 
346 	/* Return if we are already busy processing a work request. */
347 	if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
348 	    !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
349 		goto unlock;
350 
351 	sqp->s_flags |= RVT_S_BUSY;
352 
353 again:
354 	if (sqp->s_last == READ_ONCE(sqp->s_head))
355 		goto clr_busy;
356 	wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
357 
358 	/* Return if it is not OK to start a new work request. */
359 	if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
360 		if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
361 			goto clr_busy;
362 		/* We are in the error state, flush the work request. */
363 		send_status = IB_WC_WR_FLUSH_ERR;
364 		goto flush_send;
365 	}
366 
367 	/*
368 	 * We can rely on the entry not changing without the s_lock
369 	 * being held until we update s_last.
370 	 * We increment s_cur to indicate s_last is in progress.
371 	 */
372 	if (sqp->s_last == sqp->s_cur) {
373 		if (++sqp->s_cur >= sqp->s_size)
374 			sqp->s_cur = 0;
375 	}
376 	spin_unlock_irqrestore(&sqp->s_lock, flags);
377 
378 	if (!qp || !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
379 	    qp->ibqp.qp_type != sqp->ibqp.qp_type) {
380 		ibp->rvp.n_pkt_drops++;
381 		/*
382 		 * For RC, the requester would timeout and retry so
383 		 * shortcut the timeouts and just signal too many retries.
384 		 */
385 		if (sqp->ibqp.qp_type == IB_QPT_RC)
386 			send_status = IB_WC_RETRY_EXC_ERR;
387 		else
388 			send_status = IB_WC_SUCCESS;
389 		goto serr;
390 	}
391 
392 	memset(&wc, 0, sizeof(wc));
393 	send_status = IB_WC_SUCCESS;
394 
395 	release = true;
396 	sqp->s_sge.sge = wqe->sg_list[0];
397 	sqp->s_sge.sg_list = wqe->sg_list + 1;
398 	sqp->s_sge.num_sge = wqe->wr.num_sge;
399 	sqp->s_len = wqe->length;
400 	switch (wqe->wr.opcode) {
401 	case IB_WR_REG_MR:
402 		goto send_comp;
403 
404 	case IB_WR_LOCAL_INV:
405 		if (!(wqe->wr.send_flags & RVT_SEND_COMPLETION_ONLY)) {
406 			if (rvt_invalidate_rkey(sqp,
407 						wqe->wr.ex.invalidate_rkey))
408 				send_status = IB_WC_LOC_PROT_ERR;
409 			local_ops = 1;
410 		}
411 		goto send_comp;
412 
413 	case IB_WR_SEND_WITH_INV:
414 		if (!rvt_invalidate_rkey(qp, wqe->wr.ex.invalidate_rkey)) {
415 			wc.wc_flags = IB_WC_WITH_INVALIDATE;
416 			wc.ex.invalidate_rkey = wqe->wr.ex.invalidate_rkey;
417 		}
418 		goto send;
419 
420 	case IB_WR_SEND_WITH_IMM:
421 		wc.wc_flags = IB_WC_WITH_IMM;
422 		wc.ex.imm_data = wqe->wr.ex.imm_data;
423 		/* FALLTHROUGH */
424 	case IB_WR_SEND:
425 send:
426 		ret = hfi1_rvt_get_rwqe(qp, 0);
427 		if (ret < 0)
428 			goto op_err;
429 		if (!ret)
430 			goto rnr_nak;
431 		break;
432 
433 	case IB_WR_RDMA_WRITE_WITH_IMM:
434 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
435 			goto inv_err;
436 		wc.wc_flags = IB_WC_WITH_IMM;
437 		wc.ex.imm_data = wqe->wr.ex.imm_data;
438 		ret = hfi1_rvt_get_rwqe(qp, 1);
439 		if (ret < 0)
440 			goto op_err;
441 		if (!ret)
442 			goto rnr_nak;
443 		/* skip copy_last set and qp_access_flags recheck */
444 		goto do_write;
445 	case IB_WR_RDMA_WRITE:
446 		copy_last = rvt_is_user_qp(qp);
447 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
448 			goto inv_err;
449 do_write:
450 		if (wqe->length == 0)
451 			break;
452 		if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
453 					  wqe->rdma_wr.remote_addr,
454 					  wqe->rdma_wr.rkey,
455 					  IB_ACCESS_REMOTE_WRITE)))
456 			goto acc_err;
457 		qp->r_sge.sg_list = NULL;
458 		qp->r_sge.num_sge = 1;
459 		qp->r_sge.total_len = wqe->length;
460 		break;
461 
462 	case IB_WR_RDMA_READ:
463 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
464 			goto inv_err;
465 		if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
466 					  wqe->rdma_wr.remote_addr,
467 					  wqe->rdma_wr.rkey,
468 					  IB_ACCESS_REMOTE_READ)))
469 			goto acc_err;
470 		release = false;
471 		sqp->s_sge.sg_list = NULL;
472 		sqp->s_sge.num_sge = 1;
473 		qp->r_sge.sge = wqe->sg_list[0];
474 		qp->r_sge.sg_list = wqe->sg_list + 1;
475 		qp->r_sge.num_sge = wqe->wr.num_sge;
476 		qp->r_sge.total_len = wqe->length;
477 		break;
478 
479 	case IB_WR_ATOMIC_CMP_AND_SWP:
480 	case IB_WR_ATOMIC_FETCH_AND_ADD:
481 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
482 			goto inv_err;
483 		if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
484 					  wqe->atomic_wr.remote_addr,
485 					  wqe->atomic_wr.rkey,
486 					  IB_ACCESS_REMOTE_ATOMIC)))
487 			goto acc_err;
488 		/* Perform atomic OP and save result. */
489 		maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
490 		sdata = wqe->atomic_wr.compare_add;
491 		*(u64 *)sqp->s_sge.sge.vaddr =
492 			(wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
493 			(u64)atomic64_add_return(sdata, maddr) - sdata :
494 			(u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
495 				      sdata, wqe->atomic_wr.swap);
496 		rvt_put_mr(qp->r_sge.sge.mr);
497 		qp->r_sge.num_sge = 0;
498 		goto send_comp;
499 
500 	default:
501 		send_status = IB_WC_LOC_QP_OP_ERR;
502 		goto serr;
503 	}
504 
505 	sge = &sqp->s_sge.sge;
506 	while (sqp->s_len) {
507 		u32 len = sqp->s_len;
508 
509 		if (len > sge->length)
510 			len = sge->length;
511 		if (len > sge->sge_length)
512 			len = sge->sge_length;
513 		WARN_ON_ONCE(len == 0);
514 		hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, release, copy_last);
515 		sge->vaddr += len;
516 		sge->length -= len;
517 		sge->sge_length -= len;
518 		if (sge->sge_length == 0) {
519 			if (!release)
520 				rvt_put_mr(sge->mr);
521 			if (--sqp->s_sge.num_sge)
522 				*sge = *sqp->s_sge.sg_list++;
523 		} else if (sge->length == 0 && sge->mr->lkey) {
524 			if (++sge->n >= RVT_SEGSZ) {
525 				if (++sge->m >= sge->mr->mapsz)
526 					break;
527 				sge->n = 0;
528 			}
529 			sge->vaddr =
530 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
531 			sge->length =
532 				sge->mr->map[sge->m]->segs[sge->n].length;
533 		}
534 		sqp->s_len -= len;
535 	}
536 	if (release)
537 		rvt_put_ss(&qp->r_sge);
538 
539 	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
540 		goto send_comp;
541 
542 	if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
543 		wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
544 	else
545 		wc.opcode = IB_WC_RECV;
546 	wc.wr_id = qp->r_wr_id;
547 	wc.status = IB_WC_SUCCESS;
548 	wc.byte_len = wqe->length;
549 	wc.qp = &qp->ibqp;
550 	wc.src_qp = qp->remote_qpn;
551 	wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr) & U16_MAX;
552 	wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
553 	wc.port_num = 1;
554 	/* Signal completion event if the solicited bit is set. */
555 	rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
556 		     wqe->wr.send_flags & IB_SEND_SOLICITED);
557 
558 send_comp:
559 	spin_lock_irqsave(&sqp->s_lock, flags);
560 	ibp->rvp.n_loop_pkts++;
561 flush_send:
562 	sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
563 	hfi1_send_complete(sqp, wqe, send_status);
564 	if (local_ops) {
565 		atomic_dec(&sqp->local_ops_pending);
566 		local_ops = 0;
567 	}
568 	goto again;
569 
570 rnr_nak:
571 	/* Handle RNR NAK */
572 	if (qp->ibqp.qp_type == IB_QPT_UC)
573 		goto send_comp;
574 	ibp->rvp.n_rnr_naks++;
575 	/*
576 	 * Note: we don't need the s_lock held since the BUSY flag
577 	 * makes this single threaded.
578 	 */
579 	if (sqp->s_rnr_retry == 0) {
580 		send_status = IB_WC_RNR_RETRY_EXC_ERR;
581 		goto serr;
582 	}
583 	if (sqp->s_rnr_retry_cnt < 7)
584 		sqp->s_rnr_retry--;
585 	spin_lock_irqsave(&sqp->s_lock, flags);
586 	if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
587 		goto clr_busy;
588 	rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer <<
589 				IB_AETH_CREDIT_SHIFT);
590 	goto clr_busy;
591 
592 op_err:
593 	send_status = IB_WC_REM_OP_ERR;
594 	wc.status = IB_WC_LOC_QP_OP_ERR;
595 	goto err;
596 
597 inv_err:
598 	send_status = IB_WC_REM_INV_REQ_ERR;
599 	wc.status = IB_WC_LOC_QP_OP_ERR;
600 	goto err;
601 
602 acc_err:
603 	send_status = IB_WC_REM_ACCESS_ERR;
604 	wc.status = IB_WC_LOC_PROT_ERR;
605 err:
606 	/* responder goes to error state */
607 	rvt_rc_error(qp, wc.status);
608 
609 serr:
610 	spin_lock_irqsave(&sqp->s_lock, flags);
611 	hfi1_send_complete(sqp, wqe, send_status);
612 	if (sqp->ibqp.qp_type == IB_QPT_RC) {
613 		int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
614 
615 		sqp->s_flags &= ~RVT_S_BUSY;
616 		spin_unlock_irqrestore(&sqp->s_lock, flags);
617 		if (lastwqe) {
618 			struct ib_event ev;
619 
620 			ev.device = sqp->ibqp.device;
621 			ev.element.qp = &sqp->ibqp;
622 			ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
623 			sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
624 		}
625 		goto done;
626 	}
627 clr_busy:
628 	sqp->s_flags &= ~RVT_S_BUSY;
629 unlock:
630 	spin_unlock_irqrestore(&sqp->s_lock, flags);
631 done:
632 	rcu_read_unlock();
633 }
634 
635 /**
636  * hfi1_make_grh - construct a GRH header
637  * @ibp: a pointer to the IB port
638  * @hdr: a pointer to the GRH header being constructed
639  * @grh: the global route address to send to
640  * @hwords: size of header after grh being sent in dwords
641  * @nwords: the number of 32 bit words of data being sent
642  *
643  * Return the size of the header in 32 bit words.
644  */
645 u32 hfi1_make_grh(struct hfi1_ibport *ibp, struct ib_grh *hdr,
646 		  const struct ib_global_route *grh, u32 hwords, u32 nwords)
647 {
648 	hdr->version_tclass_flow =
649 		cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
650 			    (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
651 			    (grh->flow_label << IB_GRH_FLOW_SHIFT));
652 	hdr->paylen = cpu_to_be16((hwords + nwords) << 2);
653 	/* next_hdr is defined by C8-7 in ch. 8.4.1 */
654 	hdr->next_hdr = IB_GRH_NEXT_HDR;
655 	hdr->hop_limit = grh->hop_limit;
656 	/* The SGID is 32-bit aligned. */
657 	hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
658 	hdr->sgid.global.interface_id =
659 		grh->sgid_index < HFI1_GUIDS_PER_PORT ?
660 		get_sguid(ibp, grh->sgid_index) :
661 		get_sguid(ibp, HFI1_PORT_GUID_INDEX);
662 	hdr->dgid = grh->dgid;
663 
664 	/* GRH header size in 32-bit words. */
665 	return sizeof(struct ib_grh) / sizeof(u32);
666 }
667 
668 #define BTH2_OFFSET (offsetof(struct hfi1_sdma_header, \
669 			      hdr.ibh.u.oth.bth[2]) / 4)
670 
671 /**
672  * build_ahg - create ahg in s_ahg
673  * @qp: a pointer to QP
674  * @npsn: the next PSN for the request/response
675  *
676  * This routine handles the AHG by allocating an ahg entry and causing the
677  * copy of the first middle.
678  *
679  * Subsequent middles use the copied entry, editing the
680  * PSN with 1 or 2 edits.
681  */
682 static inline void build_ahg(struct rvt_qp *qp, u32 npsn)
683 {
684 	struct hfi1_qp_priv *priv = qp->priv;
685 
686 	if (unlikely(qp->s_flags & RVT_S_AHG_CLEAR))
687 		clear_ahg(qp);
688 	if (!(qp->s_flags & RVT_S_AHG_VALID)) {
689 		/* first middle that needs copy  */
690 		if (qp->s_ahgidx < 0)
691 			qp->s_ahgidx = sdma_ahg_alloc(priv->s_sde);
692 		if (qp->s_ahgidx >= 0) {
693 			qp->s_ahgpsn = npsn;
694 			priv->s_ahg->tx_flags |= SDMA_TXREQ_F_AHG_COPY;
695 			/* save to protect a change in another thread */
696 			priv->s_ahg->ahgidx = qp->s_ahgidx;
697 			qp->s_flags |= RVT_S_AHG_VALID;
698 		}
699 	} else {
700 		/* subsequent middle after valid */
701 		if (qp->s_ahgidx >= 0) {
702 			priv->s_ahg->tx_flags |= SDMA_TXREQ_F_USE_AHG;
703 			priv->s_ahg->ahgidx = qp->s_ahgidx;
704 			priv->s_ahg->ahgcount++;
705 			priv->s_ahg->ahgdesc[0] =
706 				sdma_build_ahg_descriptor(
707 					(__force u16)cpu_to_be16((u16)npsn),
708 					BTH2_OFFSET,
709 					16,
710 					16);
711 			if ((npsn & 0xffff0000) !=
712 					(qp->s_ahgpsn & 0xffff0000)) {
713 				priv->s_ahg->ahgcount++;
714 				priv->s_ahg->ahgdesc[1] =
715 					sdma_build_ahg_descriptor(
716 						(__force u16)cpu_to_be16(
717 							(u16)(npsn >> 16)),
718 						BTH2_OFFSET,
719 						0,
720 						16);
721 			}
722 		}
723 	}
724 }
725 
726 static inline void hfi1_make_ruc_bth(struct rvt_qp *qp,
727 				     struct ib_other_headers *ohdr,
728 				     u32 bth0, u32 bth1, u32 bth2)
729 {
730 	bth1 |= qp->remote_qpn;
731 	ohdr->bth[0] = cpu_to_be32(bth0);
732 	ohdr->bth[1] = cpu_to_be32(bth1);
733 	ohdr->bth[2] = cpu_to_be32(bth2);
734 }
735 
736 /**
737  * hfi1_make_ruc_header_16B - build a 16B header
738  * @qp: the queue pair
739  * @ohdr: a pointer to the destination header memory
740  * @bth0: bth0 passed in from the RC/UC builder
741  * @bth2: bth2 passed in from the RC/UC builder
742  * @middle: non zero implies indicates ahg "could" be used
743  * @ps: the current packet state
744  *
745  * This routine may disarm ahg under these situations:
746  * - packet needs a GRH
747  * - BECN needed
748  * - migration state not IB_MIG_MIGRATED
749  */
750 static inline void hfi1_make_ruc_header_16B(struct rvt_qp *qp,
751 					    struct ib_other_headers *ohdr,
752 					    u32 bth0, u32 bth2, int middle,
753 					    struct hfi1_pkt_state *ps)
754 {
755 	struct hfi1_qp_priv *priv = qp->priv;
756 	struct hfi1_ibport *ibp = ps->ibp;
757 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
758 	u32 bth1 = 0;
759 	u32 slid;
760 	u16 pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
761 	u8 l4 = OPA_16B_L4_IB_LOCAL;
762 	u8 extra_bytes = hfi1_get_16b_padding(
763 				(ps->s_txreq->hdr_dwords << 2),
764 				ps->s_txreq->s_cur_size);
765 	u32 nwords = SIZE_OF_CRC + ((ps->s_txreq->s_cur_size +
766 				 extra_bytes + SIZE_OF_LT) >> 2);
767 	bool becn = false;
768 
769 	if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH) &&
770 	    hfi1_check_mcast(rdma_ah_get_dlid(&qp->remote_ah_attr))) {
771 		struct ib_grh *grh;
772 		struct ib_global_route *grd =
773 			rdma_ah_retrieve_grh(&qp->remote_ah_attr);
774 		/*
775 		 * Ensure OPA GIDs are transformed to IB gids
776 		 * before creating the GRH.
777 		 */
778 		if (grd->sgid_index == OPA_GID_INDEX)
779 			grd->sgid_index = 0;
780 		grh = &ps->s_txreq->phdr.hdr.opah.u.l.grh;
781 		l4 = OPA_16B_L4_IB_GLOBAL;
782 		ps->s_txreq->hdr_dwords +=
783 			hfi1_make_grh(ibp, grh, grd,
784 				      ps->s_txreq->hdr_dwords - LRH_16B_DWORDS,
785 				      nwords);
786 		middle = 0;
787 	}
788 
789 	if (qp->s_mig_state == IB_MIG_MIGRATED)
790 		bth1 |= OPA_BTH_MIG_REQ;
791 	else
792 		middle = 0;
793 
794 	if (qp->s_flags & RVT_S_ECN) {
795 		qp->s_flags &= ~RVT_S_ECN;
796 		/* we recently received a FECN, so return a BECN */
797 		becn = true;
798 		middle = 0;
799 	}
800 	if (middle)
801 		build_ahg(qp, bth2);
802 	else
803 		qp->s_flags &= ~RVT_S_AHG_VALID;
804 
805 	bth0 |= pkey;
806 	bth0 |= extra_bytes << 20;
807 	hfi1_make_ruc_bth(qp, ohdr, bth0, bth1, bth2);
808 
809 	if (!ppd->lid)
810 		slid = be32_to_cpu(OPA_LID_PERMISSIVE);
811 	else
812 		slid = ppd->lid |
813 			(rdma_ah_get_path_bits(&qp->remote_ah_attr) &
814 			((1 << ppd->lmc) - 1));
815 
816 	hfi1_make_16b_hdr(&ps->s_txreq->phdr.hdr.opah,
817 			  slid,
818 			  opa_get_lid(rdma_ah_get_dlid(&qp->remote_ah_attr),
819 				      16B),
820 			  (ps->s_txreq->hdr_dwords + nwords) >> 1,
821 			  pkey, becn, 0, l4, priv->s_sc);
822 }
823 
824 /**
825  * hfi1_make_ruc_header_9B - build a 9B header
826  * @qp: the queue pair
827  * @ohdr: a pointer to the destination header memory
828  * @bth0: bth0 passed in from the RC/UC builder
829  * @bth2: bth2 passed in from the RC/UC builder
830  * @middle: non zero implies indicates ahg "could" be used
831  * @ps: the current packet state
832  *
833  * This routine may disarm ahg under these situations:
834  * - packet needs a GRH
835  * - BECN needed
836  * - migration state not IB_MIG_MIGRATED
837  */
838 static inline void hfi1_make_ruc_header_9B(struct rvt_qp *qp,
839 					   struct ib_other_headers *ohdr,
840 					   u32 bth0, u32 bth2, int middle,
841 					   struct hfi1_pkt_state *ps)
842 {
843 	struct hfi1_qp_priv *priv = qp->priv;
844 	struct hfi1_ibport *ibp = ps->ibp;
845 	u32 bth1 = 0;
846 	u16 pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
847 	u16 lrh0 = HFI1_LRH_BTH;
848 	u8 extra_bytes = -ps->s_txreq->s_cur_size & 3;
849 	u32 nwords = SIZE_OF_CRC + ((ps->s_txreq->s_cur_size +
850 					 extra_bytes) >> 2);
851 
852 	if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)) {
853 		struct ib_grh *grh = &ps->s_txreq->phdr.hdr.ibh.u.l.grh;
854 
855 		lrh0 = HFI1_LRH_GRH;
856 		ps->s_txreq->hdr_dwords +=
857 			hfi1_make_grh(ibp, grh,
858 				      rdma_ah_read_grh(&qp->remote_ah_attr),
859 				      ps->s_txreq->hdr_dwords - LRH_9B_DWORDS,
860 				      nwords);
861 		middle = 0;
862 	}
863 	lrh0 |= (priv->s_sc & 0xf) << 12 |
864 		(rdma_ah_get_sl(&qp->remote_ah_attr) & 0xf) << 4;
865 
866 	if (qp->s_mig_state == IB_MIG_MIGRATED)
867 		bth0 |= IB_BTH_MIG_REQ;
868 	else
869 		middle = 0;
870 
871 	if (qp->s_flags & RVT_S_ECN) {
872 		qp->s_flags &= ~RVT_S_ECN;
873 		/* we recently received a FECN, so return a BECN */
874 		bth1 |= (IB_BECN_MASK << IB_BECN_SHIFT);
875 		middle = 0;
876 	}
877 	if (middle)
878 		build_ahg(qp, bth2);
879 	else
880 		qp->s_flags &= ~RVT_S_AHG_VALID;
881 
882 	bth0 |= pkey;
883 	bth0 |= extra_bytes << 20;
884 	hfi1_make_ruc_bth(qp, ohdr, bth0, bth1, bth2);
885 	hfi1_make_ib_hdr(&ps->s_txreq->phdr.hdr.ibh,
886 			 lrh0,
887 			 ps->s_txreq->hdr_dwords + nwords,
888 			 opa_get_lid(rdma_ah_get_dlid(&qp->remote_ah_attr), 9B),
889 			 ppd_from_ibp(ibp)->lid |
890 				rdma_ah_get_path_bits(&qp->remote_ah_attr));
891 }
892 
893 typedef void (*hfi1_make_ruc_hdr)(struct rvt_qp *qp,
894 				  struct ib_other_headers *ohdr,
895 				  u32 bth0, u32 bth2, int middle,
896 				  struct hfi1_pkt_state *ps);
897 
898 /* We support only two types - 9B and 16B for now */
899 static const hfi1_make_ruc_hdr hfi1_ruc_header_tbl[2] = {
900 	[HFI1_PKT_TYPE_9B] = &hfi1_make_ruc_header_9B,
901 	[HFI1_PKT_TYPE_16B] = &hfi1_make_ruc_header_16B
902 };
903 
904 void hfi1_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
905 			  u32 bth0, u32 bth2, int middle,
906 			  struct hfi1_pkt_state *ps)
907 {
908 	struct hfi1_qp_priv *priv = qp->priv;
909 
910 	/*
911 	 * reset s_ahg/AHG fields
912 	 *
913 	 * This insures that the ahgentry/ahgcount
914 	 * are at a non-AHG default to protect
915 	 * build_verbs_tx_desc() from using
916 	 * an include ahgidx.
917 	 *
918 	 * build_ahg() will modify as appropriate
919 	 * to use the AHG feature.
920 	 */
921 	priv->s_ahg->tx_flags = 0;
922 	priv->s_ahg->ahgcount = 0;
923 	priv->s_ahg->ahgidx = 0;
924 
925 	/* Make the appropriate header */
926 	hfi1_ruc_header_tbl[priv->hdr_type](qp, ohdr, bth0, bth2, middle, ps);
927 }
928 
929 /* when sending, force a reschedule every one of these periods */
930 #define SEND_RESCHED_TIMEOUT (5 * HZ)  /* 5s in jiffies */
931 
932 /**
933  * schedule_send_yield - test for a yield required for QP send engine
934  * @timeout: Final time for timeout slice for jiffies
935  * @qp: a pointer to QP
936  * @ps: a pointer to a structure with commonly lookup values for
937  *      the the send engine progress
938  *
939  * This routine checks if the time slice for the QP has expired
940  * for RC QPs, if so an additional work entry is queued. At this
941  * point, other QPs have an opportunity to be scheduled. It
942  * returns true if a yield is required, otherwise, false
943  * is returned.
944  */
945 static bool schedule_send_yield(struct rvt_qp *qp,
946 				struct hfi1_pkt_state *ps)
947 {
948 	ps->pkts_sent = true;
949 
950 	if (unlikely(time_after(jiffies, ps->timeout))) {
951 		if (!ps->in_thread ||
952 		    workqueue_congested(ps->cpu, ps->ppd->hfi1_wq)) {
953 			spin_lock_irqsave(&qp->s_lock, ps->flags);
954 			qp->s_flags &= ~RVT_S_BUSY;
955 			hfi1_schedule_send(qp);
956 			spin_unlock_irqrestore(&qp->s_lock, ps->flags);
957 			this_cpu_inc(*ps->ppd->dd->send_schedule);
958 			trace_hfi1_rc_expired_time_slice(qp, true);
959 			return true;
960 		}
961 
962 		cond_resched();
963 		this_cpu_inc(*ps->ppd->dd->send_schedule);
964 		ps->timeout = jiffies + ps->timeout_int;
965 	}
966 
967 	trace_hfi1_rc_expired_time_slice(qp, false);
968 	return false;
969 }
970 
971 void hfi1_do_send_from_rvt(struct rvt_qp *qp)
972 {
973 	hfi1_do_send(qp, false);
974 }
975 
976 void _hfi1_do_send(struct work_struct *work)
977 {
978 	struct iowait *wait = container_of(work, struct iowait, iowork);
979 	struct rvt_qp *qp = iowait_to_qp(wait);
980 
981 	hfi1_do_send(qp, true);
982 }
983 
984 /**
985  * hfi1_do_send - perform a send on a QP
986  * @work: contains a pointer to the QP
987  * @in_thread: true if in a workqueue thread
988  *
989  * Process entries in the send work queue until credit or queue is
990  * exhausted.  Only allow one CPU to send a packet per QP.
991  * Otherwise, two threads could send packets out of order.
992  */
993 void hfi1_do_send(struct rvt_qp *qp, bool in_thread)
994 {
995 	struct hfi1_pkt_state ps;
996 	struct hfi1_qp_priv *priv = qp->priv;
997 	int (*make_req)(struct rvt_qp *qp, struct hfi1_pkt_state *ps);
998 
999 	ps.dev = to_idev(qp->ibqp.device);
1000 	ps.ibp = to_iport(qp->ibqp.device, qp->port_num);
1001 	ps.ppd = ppd_from_ibp(ps.ibp);
1002 	ps.in_thread = in_thread;
1003 
1004 	trace_hfi1_rc_do_send(qp, in_thread);
1005 
1006 	switch (qp->ibqp.qp_type) {
1007 	case IB_QPT_RC:
1008 		if (!loopback && ((rdma_ah_get_dlid(&qp->remote_ah_attr) &
1009 				   ~((1 << ps.ppd->lmc) - 1)) ==
1010 				  ps.ppd->lid)) {
1011 			ruc_loopback(qp);
1012 			return;
1013 		}
1014 		make_req = hfi1_make_rc_req;
1015 		ps.timeout_int = qp->timeout_jiffies;
1016 		break;
1017 	case IB_QPT_UC:
1018 		if (!loopback && ((rdma_ah_get_dlid(&qp->remote_ah_attr) &
1019 				   ~((1 << ps.ppd->lmc) - 1)) ==
1020 				  ps.ppd->lid)) {
1021 			ruc_loopback(qp);
1022 			return;
1023 		}
1024 		make_req = hfi1_make_uc_req;
1025 		ps.timeout_int = SEND_RESCHED_TIMEOUT;
1026 		break;
1027 	default:
1028 		make_req = hfi1_make_ud_req;
1029 		ps.timeout_int = SEND_RESCHED_TIMEOUT;
1030 	}
1031 
1032 	spin_lock_irqsave(&qp->s_lock, ps.flags);
1033 
1034 	/* Return if we are already busy processing a work request. */
1035 	if (!hfi1_send_ok(qp)) {
1036 		spin_unlock_irqrestore(&qp->s_lock, ps.flags);
1037 		return;
1038 	}
1039 
1040 	qp->s_flags |= RVT_S_BUSY;
1041 
1042 	ps.timeout_int = ps.timeout_int / 8;
1043 	ps.timeout = jiffies + ps.timeout_int;
1044 	ps.cpu = priv->s_sde ? priv->s_sde->cpu :
1045 			cpumask_first(cpumask_of_node(ps.ppd->dd->node));
1046 	ps.pkts_sent = false;
1047 
1048 	/* insure a pre-built packet is handled  */
1049 	ps.s_txreq = get_waiting_verbs_txreq(qp);
1050 	do {
1051 		/* Check for a constructed packet to be sent. */
1052 		if (ps.s_txreq) {
1053 			spin_unlock_irqrestore(&qp->s_lock, ps.flags);
1054 			/*
1055 			 * If the packet cannot be sent now, return and
1056 			 * the send engine will be woken up later.
1057 			 */
1058 			if (hfi1_verbs_send(qp, &ps))
1059 				return;
1060 			/* allow other tasks to run */
1061 			if (schedule_send_yield(qp, &ps))
1062 				return;
1063 
1064 			spin_lock_irqsave(&qp->s_lock, ps.flags);
1065 		}
1066 	} while (make_req(qp, &ps));
1067 	iowait_starve_clear(ps.pkts_sent, &priv->s_iowait);
1068 	spin_unlock_irqrestore(&qp->s_lock, ps.flags);
1069 }
1070 
1071 /*
1072  * This should be called with s_lock held.
1073  */
1074 void hfi1_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
1075 			enum ib_wc_status status)
1076 {
1077 	u32 old_last, last;
1078 
1079 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
1080 		return;
1081 
1082 	last = qp->s_last;
1083 	old_last = last;
1084 	trace_hfi1_qp_send_completion(qp, wqe, last);
1085 	if (++last >= qp->s_size)
1086 		last = 0;
1087 	trace_hfi1_qp_send_completion(qp, wqe, last);
1088 	qp->s_last = last;
1089 	/* See post_send() */
1090 	barrier();
1091 	rvt_put_swqe(wqe);
1092 	if (qp->ibqp.qp_type == IB_QPT_UD ||
1093 	    qp->ibqp.qp_type == IB_QPT_SMI ||
1094 	    qp->ibqp.qp_type == IB_QPT_GSI)
1095 		atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
1096 
1097 	rvt_qp_swqe_complete(qp,
1098 			     wqe,
1099 			     ib_hfi1_wc_opcode[wqe->wr.opcode],
1100 			     status);
1101 
1102 	if (qp->s_acked == old_last)
1103 		qp->s_acked = last;
1104 	if (qp->s_cur == old_last)
1105 		qp->s_cur = last;
1106 	if (qp->s_tail == old_last)
1107 		qp->s_tail = last;
1108 	if (qp->state == IB_QPS_SQD && last == qp->s_cur)
1109 		qp->s_draining = 0;
1110 }
1111