xref: /openbmc/linux/drivers/infiniband/hw/hfi1/rc.c (revision 95777591)
1 /*
2  * Copyright(c) 2015 - 2018 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/io.h>
49 #include <rdma/rdma_vt.h>
50 #include <rdma/rdmavt_qp.h>
51 
52 #include "hfi.h"
53 #include "qp.h"
54 #include "verbs_txreq.h"
55 #include "trace.h"
56 
57 /* cut down ridiculously long IB macro names */
58 #define OP(x) RC_OP(x)
59 
60 static u32 restart_sge(struct rvt_sge_state *ss, struct rvt_swqe *wqe,
61 		       u32 psn, u32 pmtu)
62 {
63 	u32 len;
64 
65 	len = delta_psn(psn, wqe->psn) * pmtu;
66 	ss->sge = wqe->sg_list[0];
67 	ss->sg_list = wqe->sg_list + 1;
68 	ss->num_sge = wqe->wr.num_sge;
69 	ss->total_len = wqe->length;
70 	rvt_skip_sge(ss, len, false);
71 	return wqe->length - len;
72 }
73 
74 /**
75  * make_rc_ack - construct a response packet (ACK, NAK, or RDMA read)
76  * @dev: the device for this QP
77  * @qp: a pointer to the QP
78  * @ohdr: a pointer to the IB header being constructed
79  * @ps: the xmit packet state
80  *
81  * Return 1 if constructed; otherwise, return 0.
82  * Note that we are in the responder's side of the QP context.
83  * Note the QP s_lock must be held.
84  */
85 static int make_rc_ack(struct hfi1_ibdev *dev, struct rvt_qp *qp,
86 		       struct ib_other_headers *ohdr,
87 		       struct hfi1_pkt_state *ps)
88 {
89 	struct rvt_ack_entry *e;
90 	u32 hwords;
91 	u32 len;
92 	u32 bth0;
93 	u32 bth2;
94 	int middle = 0;
95 	u32 pmtu = qp->pmtu;
96 	struct hfi1_qp_priv *priv = qp->priv;
97 
98 	lockdep_assert_held(&qp->s_lock);
99 	/* Don't send an ACK if we aren't supposed to. */
100 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
101 		goto bail;
102 
103 	if (priv->hdr_type == HFI1_PKT_TYPE_9B)
104 		/* header size in 32-bit words LRH+BTH = (8+12)/4. */
105 		hwords = 5;
106 	else
107 		/* header size in 32-bit words 16B LRH+BTH = (16+12)/4. */
108 		hwords = 7;
109 
110 	switch (qp->s_ack_state) {
111 	case OP(RDMA_READ_RESPONSE_LAST):
112 	case OP(RDMA_READ_RESPONSE_ONLY):
113 		e = &qp->s_ack_queue[qp->s_tail_ack_queue];
114 		if (e->rdma_sge.mr) {
115 			rvt_put_mr(e->rdma_sge.mr);
116 			e->rdma_sge.mr = NULL;
117 		}
118 		/* FALLTHROUGH */
119 	case OP(ATOMIC_ACKNOWLEDGE):
120 		/*
121 		 * We can increment the tail pointer now that the last
122 		 * response has been sent instead of only being
123 		 * constructed.
124 		 */
125 		if (++qp->s_tail_ack_queue > HFI1_MAX_RDMA_ATOMIC)
126 			qp->s_tail_ack_queue = 0;
127 		/* FALLTHROUGH */
128 	case OP(SEND_ONLY):
129 	case OP(ACKNOWLEDGE):
130 		/* Check for no next entry in the queue. */
131 		if (qp->r_head_ack_queue == qp->s_tail_ack_queue) {
132 			if (qp->s_flags & RVT_S_ACK_PENDING)
133 				goto normal;
134 			goto bail;
135 		}
136 
137 		e = &qp->s_ack_queue[qp->s_tail_ack_queue];
138 		if (e->opcode == OP(RDMA_READ_REQUEST)) {
139 			/*
140 			 * If a RDMA read response is being resent and
141 			 * we haven't seen the duplicate request yet,
142 			 * then stop sending the remaining responses the
143 			 * responder has seen until the requester re-sends it.
144 			 */
145 			len = e->rdma_sge.sge_length;
146 			if (len && !e->rdma_sge.mr) {
147 				qp->s_tail_ack_queue = qp->r_head_ack_queue;
148 				goto bail;
149 			}
150 			/* Copy SGE state in case we need to resend */
151 			ps->s_txreq->mr = e->rdma_sge.mr;
152 			if (ps->s_txreq->mr)
153 				rvt_get_mr(ps->s_txreq->mr);
154 			qp->s_ack_rdma_sge.sge = e->rdma_sge;
155 			qp->s_ack_rdma_sge.num_sge = 1;
156 			ps->s_txreq->ss = &qp->s_ack_rdma_sge;
157 			if (len > pmtu) {
158 				len = pmtu;
159 				qp->s_ack_state = OP(RDMA_READ_RESPONSE_FIRST);
160 			} else {
161 				qp->s_ack_state = OP(RDMA_READ_RESPONSE_ONLY);
162 				e->sent = 1;
163 			}
164 			ohdr->u.aeth = rvt_compute_aeth(qp);
165 			hwords++;
166 			qp->s_ack_rdma_psn = e->psn;
167 			bth2 = mask_psn(qp->s_ack_rdma_psn++);
168 		} else {
169 			/* COMPARE_SWAP or FETCH_ADD */
170 			ps->s_txreq->ss = NULL;
171 			len = 0;
172 			qp->s_ack_state = OP(ATOMIC_ACKNOWLEDGE);
173 			ohdr->u.at.aeth = rvt_compute_aeth(qp);
174 			ib_u64_put(e->atomic_data, &ohdr->u.at.atomic_ack_eth);
175 			hwords += sizeof(ohdr->u.at) / sizeof(u32);
176 			bth2 = mask_psn(e->psn);
177 			e->sent = 1;
178 		}
179 		bth0 = qp->s_ack_state << 24;
180 		break;
181 
182 	case OP(RDMA_READ_RESPONSE_FIRST):
183 		qp->s_ack_state = OP(RDMA_READ_RESPONSE_MIDDLE);
184 		/* FALLTHROUGH */
185 	case OP(RDMA_READ_RESPONSE_MIDDLE):
186 		ps->s_txreq->ss = &qp->s_ack_rdma_sge;
187 		ps->s_txreq->mr = qp->s_ack_rdma_sge.sge.mr;
188 		if (ps->s_txreq->mr)
189 			rvt_get_mr(ps->s_txreq->mr);
190 		len = qp->s_ack_rdma_sge.sge.sge_length;
191 		if (len > pmtu) {
192 			len = pmtu;
193 			middle = HFI1_CAP_IS_KSET(SDMA_AHG);
194 		} else {
195 			ohdr->u.aeth = rvt_compute_aeth(qp);
196 			hwords++;
197 			qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
198 			e = &qp->s_ack_queue[qp->s_tail_ack_queue];
199 			e->sent = 1;
200 		}
201 		bth0 = qp->s_ack_state << 24;
202 		bth2 = mask_psn(qp->s_ack_rdma_psn++);
203 		break;
204 
205 	default:
206 normal:
207 		/*
208 		 * Send a regular ACK.
209 		 * Set the s_ack_state so we wait until after sending
210 		 * the ACK before setting s_ack_state to ACKNOWLEDGE
211 		 * (see above).
212 		 */
213 		qp->s_ack_state = OP(SEND_ONLY);
214 		qp->s_flags &= ~RVT_S_ACK_PENDING;
215 		ps->s_txreq->ss = NULL;
216 		if (qp->s_nak_state)
217 			ohdr->u.aeth =
218 				cpu_to_be32((qp->r_msn & IB_MSN_MASK) |
219 					    (qp->s_nak_state <<
220 					     IB_AETH_CREDIT_SHIFT));
221 		else
222 			ohdr->u.aeth = rvt_compute_aeth(qp);
223 		hwords++;
224 		len = 0;
225 		bth0 = OP(ACKNOWLEDGE) << 24;
226 		bth2 = mask_psn(qp->s_ack_psn);
227 	}
228 	qp->s_rdma_ack_cnt++;
229 	ps->s_txreq->sde = priv->s_sde;
230 	ps->s_txreq->s_cur_size = len;
231 	ps->s_txreq->hdr_dwords = hwords;
232 	hfi1_make_ruc_header(qp, ohdr, bth0, bth2, middle, ps);
233 	return 1;
234 
235 bail:
236 	qp->s_ack_state = OP(ACKNOWLEDGE);
237 	/*
238 	 * Ensure s_rdma_ack_cnt changes are committed prior to resetting
239 	 * RVT_S_RESP_PENDING
240 	 */
241 	smp_wmb();
242 	qp->s_flags &= ~(RVT_S_RESP_PENDING
243 				| RVT_S_ACK_PENDING
244 				| HFI1_S_AHG_VALID);
245 	return 0;
246 }
247 
248 /**
249  * hfi1_make_rc_req - construct a request packet (SEND, RDMA r/w, ATOMIC)
250  * @qp: a pointer to the QP
251  *
252  * Assumes s_lock is held.
253  *
254  * Return 1 if constructed; otherwise, return 0.
255  */
256 int hfi1_make_rc_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
257 {
258 	struct hfi1_qp_priv *priv = qp->priv;
259 	struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
260 	struct ib_other_headers *ohdr;
261 	struct rvt_sge_state *ss;
262 	struct rvt_swqe *wqe;
263 	u32 hwords;
264 	u32 len;
265 	u32 bth0 = 0;
266 	u32 bth2;
267 	u32 pmtu = qp->pmtu;
268 	char newreq;
269 	int middle = 0;
270 	int delta;
271 
272 	lockdep_assert_held(&qp->s_lock);
273 	ps->s_txreq = get_txreq(ps->dev, qp);
274 	if (!ps->s_txreq)
275 		goto bail_no_tx;
276 
277 	if (priv->hdr_type == HFI1_PKT_TYPE_9B) {
278 		/* header size in 32-bit words LRH+BTH = (8+12)/4. */
279 		hwords = 5;
280 		if (rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)
281 			ohdr = &ps->s_txreq->phdr.hdr.ibh.u.l.oth;
282 		else
283 			ohdr = &ps->s_txreq->phdr.hdr.ibh.u.oth;
284 	} else {
285 		/* header size in 32-bit words 16B LRH+BTH = (16+12)/4. */
286 		hwords = 7;
287 		if ((rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH) &&
288 		    (hfi1_check_mcast(rdma_ah_get_dlid(&qp->remote_ah_attr))))
289 			ohdr = &ps->s_txreq->phdr.hdr.opah.u.l.oth;
290 		else
291 			ohdr = &ps->s_txreq->phdr.hdr.opah.u.oth;
292 	}
293 
294 	/* Sending responses has higher priority over sending requests. */
295 	if ((qp->s_flags & RVT_S_RESP_PENDING) &&
296 	    make_rc_ack(dev, qp, ohdr, ps))
297 		return 1;
298 
299 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_SEND_OK)) {
300 		if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
301 			goto bail;
302 		/* We are in the error state, flush the work request. */
303 		if (qp->s_last == READ_ONCE(qp->s_head))
304 			goto bail;
305 		/* If DMAs are in progress, we can't flush immediately. */
306 		if (iowait_sdma_pending(&priv->s_iowait)) {
307 			qp->s_flags |= RVT_S_WAIT_DMA;
308 			goto bail;
309 		}
310 		clear_ahg(qp);
311 		wqe = rvt_get_swqe_ptr(qp, qp->s_last);
312 		rvt_send_complete(qp, wqe, qp->s_last != qp->s_acked ?
313 			IB_WC_SUCCESS : IB_WC_WR_FLUSH_ERR);
314 		/* will get called again */
315 		goto done_free_tx;
316 	}
317 
318 	if (qp->s_flags & (RVT_S_WAIT_RNR | RVT_S_WAIT_ACK))
319 		goto bail;
320 
321 	if (cmp_psn(qp->s_psn, qp->s_sending_hpsn) <= 0) {
322 		if (cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0) {
323 			qp->s_flags |= RVT_S_WAIT_PSN;
324 			goto bail;
325 		}
326 		qp->s_sending_psn = qp->s_psn;
327 		qp->s_sending_hpsn = qp->s_psn - 1;
328 	}
329 
330 	/* Send a request. */
331 	wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
332 	switch (qp->s_state) {
333 	default:
334 		if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK))
335 			goto bail;
336 		/*
337 		 * Resend an old request or start a new one.
338 		 *
339 		 * We keep track of the current SWQE so that
340 		 * we don't reset the "furthest progress" state
341 		 * if we need to back up.
342 		 */
343 		newreq = 0;
344 		if (qp->s_cur == qp->s_tail) {
345 			/* Check if send work queue is empty. */
346 			if (qp->s_tail == READ_ONCE(qp->s_head)) {
347 				clear_ahg(qp);
348 				goto bail;
349 			}
350 			/*
351 			 * If a fence is requested, wait for previous
352 			 * RDMA read and atomic operations to finish.
353 			 */
354 			if ((wqe->wr.send_flags & IB_SEND_FENCE) &&
355 			    qp->s_num_rd_atomic) {
356 				qp->s_flags |= RVT_S_WAIT_FENCE;
357 				goto bail;
358 			}
359 			/*
360 			 * Local operations are processed immediately
361 			 * after all prior requests have completed
362 			 */
363 			if (wqe->wr.opcode == IB_WR_REG_MR ||
364 			    wqe->wr.opcode == IB_WR_LOCAL_INV) {
365 				int local_ops = 0;
366 				int err = 0;
367 
368 				if (qp->s_last != qp->s_cur)
369 					goto bail;
370 				if (++qp->s_cur == qp->s_size)
371 					qp->s_cur = 0;
372 				if (++qp->s_tail == qp->s_size)
373 					qp->s_tail = 0;
374 				if (!(wqe->wr.send_flags &
375 				      RVT_SEND_COMPLETION_ONLY)) {
376 					err = rvt_invalidate_rkey(
377 						qp,
378 						wqe->wr.ex.invalidate_rkey);
379 					local_ops = 1;
380 				}
381 				rvt_send_complete(qp, wqe,
382 						  err ? IB_WC_LOC_PROT_ERR
383 						      : IB_WC_SUCCESS);
384 				if (local_ops)
385 					atomic_dec(&qp->local_ops_pending);
386 				goto done_free_tx;
387 			}
388 
389 			newreq = 1;
390 			qp->s_psn = wqe->psn;
391 		}
392 		/*
393 		 * Note that we have to be careful not to modify the
394 		 * original work request since we may need to resend
395 		 * it.
396 		 */
397 		len = wqe->length;
398 		ss = &qp->s_sge;
399 		bth2 = mask_psn(qp->s_psn);
400 		switch (wqe->wr.opcode) {
401 		case IB_WR_SEND:
402 		case IB_WR_SEND_WITH_IMM:
403 		case IB_WR_SEND_WITH_INV:
404 			/* If no credit, return. */
405 			if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
406 			    rvt_cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
407 				qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
408 				goto bail;
409 			}
410 			if (len > pmtu) {
411 				qp->s_state = OP(SEND_FIRST);
412 				len = pmtu;
413 				break;
414 			}
415 			if (wqe->wr.opcode == IB_WR_SEND) {
416 				qp->s_state = OP(SEND_ONLY);
417 			} else if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
418 				qp->s_state = OP(SEND_ONLY_WITH_IMMEDIATE);
419 				/* Immediate data comes after the BTH */
420 				ohdr->u.imm_data = wqe->wr.ex.imm_data;
421 				hwords += 1;
422 			} else {
423 				qp->s_state = OP(SEND_ONLY_WITH_INVALIDATE);
424 				/* Invalidate rkey comes after the BTH */
425 				ohdr->u.ieth = cpu_to_be32(
426 						wqe->wr.ex.invalidate_rkey);
427 				hwords += 1;
428 			}
429 			if (wqe->wr.send_flags & IB_SEND_SOLICITED)
430 				bth0 |= IB_BTH_SOLICITED;
431 			bth2 |= IB_BTH_REQ_ACK;
432 			if (++qp->s_cur == qp->s_size)
433 				qp->s_cur = 0;
434 			break;
435 
436 		case IB_WR_RDMA_WRITE:
437 			if (newreq && !(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
438 				qp->s_lsn++;
439 			goto no_flow_control;
440 		case IB_WR_RDMA_WRITE_WITH_IMM:
441 			/* If no credit, return. */
442 			if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
443 			    rvt_cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
444 				qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
445 				goto bail;
446 			}
447 no_flow_control:
448 			put_ib_reth_vaddr(
449 				wqe->rdma_wr.remote_addr,
450 				&ohdr->u.rc.reth);
451 			ohdr->u.rc.reth.rkey =
452 				cpu_to_be32(wqe->rdma_wr.rkey);
453 			ohdr->u.rc.reth.length = cpu_to_be32(len);
454 			hwords += sizeof(struct ib_reth) / sizeof(u32);
455 			if (len > pmtu) {
456 				qp->s_state = OP(RDMA_WRITE_FIRST);
457 				len = pmtu;
458 				break;
459 			}
460 			if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
461 				qp->s_state = OP(RDMA_WRITE_ONLY);
462 			} else {
463 				qp->s_state =
464 					OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
465 				/* Immediate data comes after RETH */
466 				ohdr->u.rc.imm_data = wqe->wr.ex.imm_data;
467 				hwords += 1;
468 				if (wqe->wr.send_flags & IB_SEND_SOLICITED)
469 					bth0 |= IB_BTH_SOLICITED;
470 			}
471 			bth2 |= IB_BTH_REQ_ACK;
472 			if (++qp->s_cur == qp->s_size)
473 				qp->s_cur = 0;
474 			break;
475 
476 		case IB_WR_RDMA_READ:
477 			/*
478 			 * Don't allow more operations to be started
479 			 * than the QP limits allow.
480 			 */
481 			if (newreq) {
482 				if (qp->s_num_rd_atomic >=
483 				    qp->s_max_rd_atomic) {
484 					qp->s_flags |= RVT_S_WAIT_RDMAR;
485 					goto bail;
486 				}
487 				qp->s_num_rd_atomic++;
488 				if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
489 					qp->s_lsn++;
490 			}
491 			put_ib_reth_vaddr(
492 				wqe->rdma_wr.remote_addr,
493 				&ohdr->u.rc.reth);
494 			ohdr->u.rc.reth.rkey =
495 				cpu_to_be32(wqe->rdma_wr.rkey);
496 			ohdr->u.rc.reth.length = cpu_to_be32(len);
497 			qp->s_state = OP(RDMA_READ_REQUEST);
498 			hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
499 			ss = NULL;
500 			len = 0;
501 			bth2 |= IB_BTH_REQ_ACK;
502 			if (++qp->s_cur == qp->s_size)
503 				qp->s_cur = 0;
504 			break;
505 
506 		case IB_WR_ATOMIC_CMP_AND_SWP:
507 		case IB_WR_ATOMIC_FETCH_AND_ADD:
508 			/*
509 			 * Don't allow more operations to be started
510 			 * than the QP limits allow.
511 			 */
512 			if (newreq) {
513 				if (qp->s_num_rd_atomic >=
514 				    qp->s_max_rd_atomic) {
515 					qp->s_flags |= RVT_S_WAIT_RDMAR;
516 					goto bail;
517 				}
518 				qp->s_num_rd_atomic++;
519 				if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
520 					qp->s_lsn++;
521 			}
522 			if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
523 				qp->s_state = OP(COMPARE_SWAP);
524 				put_ib_ateth_swap(wqe->atomic_wr.swap,
525 						  &ohdr->u.atomic_eth);
526 				put_ib_ateth_compare(wqe->atomic_wr.compare_add,
527 						     &ohdr->u.atomic_eth);
528 			} else {
529 				qp->s_state = OP(FETCH_ADD);
530 				put_ib_ateth_swap(wqe->atomic_wr.compare_add,
531 						  &ohdr->u.atomic_eth);
532 				put_ib_ateth_compare(0, &ohdr->u.atomic_eth);
533 			}
534 			put_ib_ateth_vaddr(wqe->atomic_wr.remote_addr,
535 					   &ohdr->u.atomic_eth);
536 			ohdr->u.atomic_eth.rkey = cpu_to_be32(
537 				wqe->atomic_wr.rkey);
538 			hwords += sizeof(struct ib_atomic_eth) / sizeof(u32);
539 			ss = NULL;
540 			len = 0;
541 			bth2 |= IB_BTH_REQ_ACK;
542 			if (++qp->s_cur == qp->s_size)
543 				qp->s_cur = 0;
544 			break;
545 
546 		default:
547 			goto bail;
548 		}
549 		qp->s_sge.sge = wqe->sg_list[0];
550 		qp->s_sge.sg_list = wqe->sg_list + 1;
551 		qp->s_sge.num_sge = wqe->wr.num_sge;
552 		qp->s_sge.total_len = wqe->length;
553 		qp->s_len = wqe->length;
554 		if (newreq) {
555 			qp->s_tail++;
556 			if (qp->s_tail >= qp->s_size)
557 				qp->s_tail = 0;
558 		}
559 		if (wqe->wr.opcode == IB_WR_RDMA_READ)
560 			qp->s_psn = wqe->lpsn + 1;
561 		else
562 			qp->s_psn++;
563 		break;
564 
565 	case OP(RDMA_READ_RESPONSE_FIRST):
566 		/*
567 		 * qp->s_state is normally set to the opcode of the
568 		 * last packet constructed for new requests and therefore
569 		 * is never set to RDMA read response.
570 		 * RDMA_READ_RESPONSE_FIRST is used by the ACK processing
571 		 * thread to indicate a SEND needs to be restarted from an
572 		 * earlier PSN without interfering with the sending thread.
573 		 * See restart_rc().
574 		 */
575 		qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
576 		/* FALLTHROUGH */
577 	case OP(SEND_FIRST):
578 		qp->s_state = OP(SEND_MIDDLE);
579 		/* FALLTHROUGH */
580 	case OP(SEND_MIDDLE):
581 		bth2 = mask_psn(qp->s_psn++);
582 		ss = &qp->s_sge;
583 		len = qp->s_len;
584 		if (len > pmtu) {
585 			len = pmtu;
586 			middle = HFI1_CAP_IS_KSET(SDMA_AHG);
587 			break;
588 		}
589 		if (wqe->wr.opcode == IB_WR_SEND) {
590 			qp->s_state = OP(SEND_LAST);
591 		} else if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
592 			qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
593 			/* Immediate data comes after the BTH */
594 			ohdr->u.imm_data = wqe->wr.ex.imm_data;
595 			hwords += 1;
596 		} else {
597 			qp->s_state = OP(SEND_LAST_WITH_INVALIDATE);
598 			/* invalidate data comes after the BTH */
599 			ohdr->u.ieth = cpu_to_be32(wqe->wr.ex.invalidate_rkey);
600 			hwords += 1;
601 		}
602 		if (wqe->wr.send_flags & IB_SEND_SOLICITED)
603 			bth0 |= IB_BTH_SOLICITED;
604 		bth2 |= IB_BTH_REQ_ACK;
605 		qp->s_cur++;
606 		if (qp->s_cur >= qp->s_size)
607 			qp->s_cur = 0;
608 		break;
609 
610 	case OP(RDMA_READ_RESPONSE_LAST):
611 		/*
612 		 * qp->s_state is normally set to the opcode of the
613 		 * last packet constructed for new requests and therefore
614 		 * is never set to RDMA read response.
615 		 * RDMA_READ_RESPONSE_LAST is used by the ACK processing
616 		 * thread to indicate a RDMA write needs to be restarted from
617 		 * an earlier PSN without interfering with the sending thread.
618 		 * See restart_rc().
619 		 */
620 		qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
621 		/* FALLTHROUGH */
622 	case OP(RDMA_WRITE_FIRST):
623 		qp->s_state = OP(RDMA_WRITE_MIDDLE);
624 		/* FALLTHROUGH */
625 	case OP(RDMA_WRITE_MIDDLE):
626 		bth2 = mask_psn(qp->s_psn++);
627 		ss = &qp->s_sge;
628 		len = qp->s_len;
629 		if (len > pmtu) {
630 			len = pmtu;
631 			middle = HFI1_CAP_IS_KSET(SDMA_AHG);
632 			break;
633 		}
634 		if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
635 			qp->s_state = OP(RDMA_WRITE_LAST);
636 		} else {
637 			qp->s_state = OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
638 			/* Immediate data comes after the BTH */
639 			ohdr->u.imm_data = wqe->wr.ex.imm_data;
640 			hwords += 1;
641 			if (wqe->wr.send_flags & IB_SEND_SOLICITED)
642 				bth0 |= IB_BTH_SOLICITED;
643 		}
644 		bth2 |= IB_BTH_REQ_ACK;
645 		qp->s_cur++;
646 		if (qp->s_cur >= qp->s_size)
647 			qp->s_cur = 0;
648 		break;
649 
650 	case OP(RDMA_READ_RESPONSE_MIDDLE):
651 		/*
652 		 * qp->s_state is normally set to the opcode of the
653 		 * last packet constructed for new requests and therefore
654 		 * is never set to RDMA read response.
655 		 * RDMA_READ_RESPONSE_MIDDLE is used by the ACK processing
656 		 * thread to indicate a RDMA read needs to be restarted from
657 		 * an earlier PSN without interfering with the sending thread.
658 		 * See restart_rc().
659 		 */
660 		len = (delta_psn(qp->s_psn, wqe->psn)) * pmtu;
661 		put_ib_reth_vaddr(
662 			wqe->rdma_wr.remote_addr + len,
663 			&ohdr->u.rc.reth);
664 		ohdr->u.rc.reth.rkey =
665 			cpu_to_be32(wqe->rdma_wr.rkey);
666 		ohdr->u.rc.reth.length = cpu_to_be32(wqe->length - len);
667 		qp->s_state = OP(RDMA_READ_REQUEST);
668 		hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
669 		bth2 = mask_psn(qp->s_psn) | IB_BTH_REQ_ACK;
670 		qp->s_psn = wqe->lpsn + 1;
671 		ss = NULL;
672 		len = 0;
673 		qp->s_cur++;
674 		if (qp->s_cur == qp->s_size)
675 			qp->s_cur = 0;
676 		break;
677 	}
678 	qp->s_sending_hpsn = bth2;
679 	delta = delta_psn(bth2, wqe->psn);
680 	if (delta && delta % HFI1_PSN_CREDIT == 0)
681 		bth2 |= IB_BTH_REQ_ACK;
682 	if (qp->s_flags & RVT_S_SEND_ONE) {
683 		qp->s_flags &= ~RVT_S_SEND_ONE;
684 		qp->s_flags |= RVT_S_WAIT_ACK;
685 		bth2 |= IB_BTH_REQ_ACK;
686 	}
687 	qp->s_len -= len;
688 	ps->s_txreq->hdr_dwords = hwords;
689 	ps->s_txreq->sde = priv->s_sde;
690 	ps->s_txreq->ss = ss;
691 	ps->s_txreq->s_cur_size = len;
692 	hfi1_make_ruc_header(
693 		qp,
694 		ohdr,
695 		bth0 | (qp->s_state << 24),
696 		bth2,
697 		middle,
698 		ps);
699 	return 1;
700 
701 done_free_tx:
702 	hfi1_put_txreq(ps->s_txreq);
703 	ps->s_txreq = NULL;
704 	return 1;
705 
706 bail:
707 	hfi1_put_txreq(ps->s_txreq);
708 
709 bail_no_tx:
710 	ps->s_txreq = NULL;
711 	qp->s_flags &= ~RVT_S_BUSY;
712 	return 0;
713 }
714 
715 static inline void hfi1_make_bth_aeth(struct rvt_qp *qp,
716 				      struct ib_other_headers *ohdr,
717 				      u32 bth0, u32 bth1)
718 {
719 	if (qp->r_nak_state)
720 		ohdr->u.aeth = cpu_to_be32((qp->r_msn & IB_MSN_MASK) |
721 					    (qp->r_nak_state <<
722 					     IB_AETH_CREDIT_SHIFT));
723 	else
724 		ohdr->u.aeth = rvt_compute_aeth(qp);
725 
726 	ohdr->bth[0] = cpu_to_be32(bth0);
727 	ohdr->bth[1] = cpu_to_be32(bth1 | qp->remote_qpn);
728 	ohdr->bth[2] = cpu_to_be32(mask_psn(qp->r_ack_psn));
729 }
730 
731 static inline void hfi1_queue_rc_ack(struct hfi1_packet *packet, bool is_fecn)
732 {
733 	struct rvt_qp *qp = packet->qp;
734 	struct hfi1_ibport *ibp;
735 	unsigned long flags;
736 
737 	spin_lock_irqsave(&qp->s_lock, flags);
738 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
739 		goto unlock;
740 	ibp = rcd_to_iport(packet->rcd);
741 	this_cpu_inc(*ibp->rvp.rc_qacks);
742 	qp->s_flags |= RVT_S_ACK_PENDING | RVT_S_RESP_PENDING;
743 	qp->s_nak_state = qp->r_nak_state;
744 	qp->s_ack_psn = qp->r_ack_psn;
745 	if (is_fecn)
746 		qp->s_flags |= RVT_S_ECN;
747 
748 	/* Schedule the send tasklet. */
749 	hfi1_schedule_send(qp);
750 unlock:
751 	spin_unlock_irqrestore(&qp->s_lock, flags);
752 }
753 
754 static inline void hfi1_make_rc_ack_9B(struct hfi1_packet *packet,
755 				       struct hfi1_opa_header *opa_hdr,
756 				       u8 sc5, bool is_fecn,
757 				       u64 *pbc_flags, u32 *hwords,
758 				       u32 *nwords)
759 {
760 	struct rvt_qp *qp = packet->qp;
761 	struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
762 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
763 	struct ib_header *hdr = &opa_hdr->ibh;
764 	struct ib_other_headers *ohdr;
765 	u16 lrh0 = HFI1_LRH_BTH;
766 	u16 pkey;
767 	u32 bth0, bth1;
768 
769 	opa_hdr->hdr_type = HFI1_PKT_TYPE_9B;
770 	ohdr = &hdr->u.oth;
771 	/* header size in 32-bit words LRH+BTH+AETH = (8+12+4)/4 */
772 	*hwords = 6;
773 
774 	if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)) {
775 		*hwords += hfi1_make_grh(ibp, &hdr->u.l.grh,
776 					 rdma_ah_read_grh(&qp->remote_ah_attr),
777 					 *hwords - 2, SIZE_OF_CRC);
778 		ohdr = &hdr->u.l.oth;
779 		lrh0 = HFI1_LRH_GRH;
780 	}
781 	/* set PBC_DC_INFO bit (aka SC[4]) in pbc_flags */
782 	*pbc_flags |= ((!!(sc5 & 0x10)) << PBC_DC_INFO_SHIFT);
783 
784 	/* read pkey_index w/o lock (its atomic) */
785 	pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
786 
787 	lrh0 |= (sc5 & IB_SC_MASK) << IB_SC_SHIFT |
788 		(rdma_ah_get_sl(&qp->remote_ah_attr) & IB_SL_MASK) <<
789 			IB_SL_SHIFT;
790 
791 	hfi1_make_ib_hdr(hdr, lrh0, *hwords + SIZE_OF_CRC,
792 			 opa_get_lid(rdma_ah_get_dlid(&qp->remote_ah_attr), 9B),
793 			 ppd->lid | rdma_ah_get_path_bits(&qp->remote_ah_attr));
794 
795 	bth0 = pkey | (OP(ACKNOWLEDGE) << 24);
796 	if (qp->s_mig_state == IB_MIG_MIGRATED)
797 		bth0 |= IB_BTH_MIG_REQ;
798 	bth1 = (!!is_fecn) << IB_BECN_SHIFT;
799 	hfi1_make_bth_aeth(qp, ohdr, bth0, bth1);
800 }
801 
802 static inline void hfi1_make_rc_ack_16B(struct hfi1_packet *packet,
803 					struct hfi1_opa_header *opa_hdr,
804 					u8 sc5, bool is_fecn,
805 					u64 *pbc_flags, u32 *hwords,
806 					u32 *nwords)
807 {
808 	struct rvt_qp *qp = packet->qp;
809 	struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
810 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
811 	struct hfi1_16b_header *hdr = &opa_hdr->opah;
812 	struct ib_other_headers *ohdr;
813 	u32 bth0, bth1 = 0;
814 	u16 len, pkey;
815 	bool becn = is_fecn;
816 	u8 l4 = OPA_16B_L4_IB_LOCAL;
817 	u8 extra_bytes;
818 
819 	opa_hdr->hdr_type = HFI1_PKT_TYPE_16B;
820 	ohdr = &hdr->u.oth;
821 	/* header size in 32-bit words 16B LRH+BTH+AETH = (16+12+4)/4 */
822 	*hwords = 8;
823 	extra_bytes = hfi1_get_16b_padding(*hwords << 2, 0);
824 	*nwords = SIZE_OF_CRC + ((extra_bytes + SIZE_OF_LT) >> 2);
825 
826 	if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH) &&
827 	    hfi1_check_mcast(rdma_ah_get_dlid(&qp->remote_ah_attr))) {
828 		*hwords += hfi1_make_grh(ibp, &hdr->u.l.grh,
829 					 rdma_ah_read_grh(&qp->remote_ah_attr),
830 					 *hwords - 4, *nwords);
831 		ohdr = &hdr->u.l.oth;
832 		l4 = OPA_16B_L4_IB_GLOBAL;
833 	}
834 	*pbc_flags |= PBC_PACKET_BYPASS | PBC_INSERT_BYPASS_ICRC;
835 
836 	/* read pkey_index w/o lock (its atomic) */
837 	pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
838 
839 	/* Convert dwords to flits */
840 	len = (*hwords + *nwords) >> 1;
841 
842 	hfi1_make_16b_hdr(hdr, ppd->lid |
843 			  (rdma_ah_get_path_bits(&qp->remote_ah_attr) &
844 			  ((1 << ppd->lmc) - 1)),
845 			  opa_get_lid(rdma_ah_get_dlid(&qp->remote_ah_attr),
846 				      16B), len, pkey, becn, 0, l4, sc5);
847 
848 	bth0 = pkey | (OP(ACKNOWLEDGE) << 24);
849 	bth0 |= extra_bytes << 20;
850 	if (qp->s_mig_state == IB_MIG_MIGRATED)
851 		bth1 = OPA_BTH_MIG_REQ;
852 	hfi1_make_bth_aeth(qp, ohdr, bth0, bth1);
853 }
854 
855 typedef void (*hfi1_make_rc_ack)(struct hfi1_packet *packet,
856 				 struct hfi1_opa_header *opa_hdr,
857 				 u8 sc5, bool is_fecn,
858 				 u64 *pbc_flags, u32 *hwords,
859 				 u32 *nwords);
860 
861 /* We support only two types - 9B and 16B for now */
862 static const hfi1_make_rc_ack hfi1_make_rc_ack_tbl[2] = {
863 	[HFI1_PKT_TYPE_9B] = &hfi1_make_rc_ack_9B,
864 	[HFI1_PKT_TYPE_16B] = &hfi1_make_rc_ack_16B
865 };
866 
867 /**
868  * hfi1_send_rc_ack - Construct an ACK packet and send it
869  * @qp: a pointer to the QP
870  *
871  * This is called from hfi1_rc_rcv() and handle_receive_interrupt().
872  * Note that RDMA reads and atomics are handled in the
873  * send side QP state and send engine.
874  */
875 void hfi1_send_rc_ack(struct hfi1_packet *packet, bool is_fecn)
876 {
877 	struct hfi1_ctxtdata *rcd = packet->rcd;
878 	struct rvt_qp *qp = packet->qp;
879 	struct hfi1_ibport *ibp = rcd_to_iport(rcd);
880 	struct hfi1_qp_priv *priv = qp->priv;
881 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
882 	u8 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(&qp->remote_ah_attr)];
883 	u64 pbc, pbc_flags = 0;
884 	u32 hwords = 0;
885 	u32 nwords = 0;
886 	u32 plen;
887 	struct pio_buf *pbuf;
888 	struct hfi1_opa_header opa_hdr;
889 
890 	/* clear the defer count */
891 	qp->r_adefered = 0;
892 
893 	/* Don't send ACK or NAK if a RDMA read or atomic is pending. */
894 	if (qp->s_flags & RVT_S_RESP_PENDING) {
895 		hfi1_queue_rc_ack(packet, is_fecn);
896 		return;
897 	}
898 
899 	/* Ensure s_rdma_ack_cnt changes are committed */
900 	if (qp->s_rdma_ack_cnt) {
901 		hfi1_queue_rc_ack(packet, is_fecn);
902 		return;
903 	}
904 
905 	/* Don't try to send ACKs if the link isn't ACTIVE */
906 	if (driver_lstate(ppd) != IB_PORT_ACTIVE)
907 		return;
908 
909 	/* Make the appropriate header */
910 	hfi1_make_rc_ack_tbl[priv->hdr_type](packet, &opa_hdr, sc5, is_fecn,
911 					     &pbc_flags, &hwords, &nwords);
912 
913 	plen = 2 /* PBC */ + hwords + nwords;
914 	pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps,
915 			 sc_to_vlt(ppd->dd, sc5), plen);
916 	pbuf = sc_buffer_alloc(rcd->sc, plen, NULL, NULL);
917 	if (!pbuf) {
918 		/*
919 		 * We have no room to send at the moment.  Pass
920 		 * responsibility for sending the ACK to the send engine
921 		 * so that when enough buffer space becomes available,
922 		 * the ACK is sent ahead of other outgoing packets.
923 		 */
924 		hfi1_queue_rc_ack(packet, is_fecn);
925 		return;
926 	}
927 	trace_ack_output_ibhdr(dd_from_ibdev(qp->ibqp.device),
928 			       &opa_hdr, ib_is_sc5(sc5));
929 
930 	/* write the pbc and data */
931 	ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
932 				 (priv->hdr_type == HFI1_PKT_TYPE_9B ?
933 				 (void *)&opa_hdr.ibh :
934 				 (void *)&opa_hdr.opah), hwords);
935 	return;
936 }
937 
938 /**
939  * reset_psn - reset the QP state to send starting from PSN
940  * @qp: the QP
941  * @psn: the packet sequence number to restart at
942  *
943  * This is called from hfi1_rc_rcv() to process an incoming RC ACK
944  * for the given QP.
945  * Called at interrupt level with the QP s_lock held.
946  */
947 static void reset_psn(struct rvt_qp *qp, u32 psn)
948 {
949 	u32 n = qp->s_acked;
950 	struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, n);
951 	u32 opcode;
952 
953 	lockdep_assert_held(&qp->s_lock);
954 	qp->s_cur = n;
955 
956 	/*
957 	 * If we are starting the request from the beginning,
958 	 * let the normal send code handle initialization.
959 	 */
960 	if (cmp_psn(psn, wqe->psn) <= 0) {
961 		qp->s_state = OP(SEND_LAST);
962 		goto done;
963 	}
964 
965 	/* Find the work request opcode corresponding to the given PSN. */
966 	opcode = wqe->wr.opcode;
967 	for (;;) {
968 		int diff;
969 
970 		if (++n == qp->s_size)
971 			n = 0;
972 		if (n == qp->s_tail)
973 			break;
974 		wqe = rvt_get_swqe_ptr(qp, n);
975 		diff = cmp_psn(psn, wqe->psn);
976 		if (diff < 0)
977 			break;
978 		qp->s_cur = n;
979 		/*
980 		 * If we are starting the request from the beginning,
981 		 * let the normal send code handle initialization.
982 		 */
983 		if (diff == 0) {
984 			qp->s_state = OP(SEND_LAST);
985 			goto done;
986 		}
987 		opcode = wqe->wr.opcode;
988 	}
989 
990 	/*
991 	 * Set the state to restart in the middle of a request.
992 	 * Don't change the s_sge, s_cur_sge, or s_cur_size.
993 	 * See hfi1_make_rc_req().
994 	 */
995 	switch (opcode) {
996 	case IB_WR_SEND:
997 	case IB_WR_SEND_WITH_IMM:
998 		qp->s_state = OP(RDMA_READ_RESPONSE_FIRST);
999 		break;
1000 
1001 	case IB_WR_RDMA_WRITE:
1002 	case IB_WR_RDMA_WRITE_WITH_IMM:
1003 		qp->s_state = OP(RDMA_READ_RESPONSE_LAST);
1004 		break;
1005 
1006 	case IB_WR_RDMA_READ:
1007 		qp->s_state = OP(RDMA_READ_RESPONSE_MIDDLE);
1008 		break;
1009 
1010 	default:
1011 		/*
1012 		 * This case shouldn't happen since its only
1013 		 * one PSN per req.
1014 		 */
1015 		qp->s_state = OP(SEND_LAST);
1016 	}
1017 done:
1018 	qp->s_psn = psn;
1019 	/*
1020 	 * Set RVT_S_WAIT_PSN as rc_complete() may start the timer
1021 	 * asynchronously before the send engine can get scheduled.
1022 	 * Doing it in hfi1_make_rc_req() is too late.
1023 	 */
1024 	if ((cmp_psn(qp->s_psn, qp->s_sending_hpsn) <= 0) &&
1025 	    (cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0))
1026 		qp->s_flags |= RVT_S_WAIT_PSN;
1027 	qp->s_flags &= ~HFI1_S_AHG_VALID;
1028 }
1029 
1030 /*
1031  * Back up requester to resend the last un-ACKed request.
1032  * The QP r_lock and s_lock should be held and interrupts disabled.
1033  */
1034 void hfi1_restart_rc(struct rvt_qp *qp, u32 psn, int wait)
1035 {
1036 	struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
1037 	struct hfi1_ibport *ibp;
1038 
1039 	lockdep_assert_held(&qp->r_lock);
1040 	lockdep_assert_held(&qp->s_lock);
1041 	if (qp->s_retry == 0) {
1042 		if (qp->s_mig_state == IB_MIG_ARMED) {
1043 			hfi1_migrate_qp(qp);
1044 			qp->s_retry = qp->s_retry_cnt;
1045 		} else if (qp->s_last == qp->s_acked) {
1046 			rvt_send_complete(qp, wqe, IB_WC_RETRY_EXC_ERR);
1047 			rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1048 			return;
1049 		} else { /* need to handle delayed completion */
1050 			return;
1051 		}
1052 	} else {
1053 		qp->s_retry--;
1054 	}
1055 
1056 	ibp = to_iport(qp->ibqp.device, qp->port_num);
1057 	if (wqe->wr.opcode == IB_WR_RDMA_READ)
1058 		ibp->rvp.n_rc_resends++;
1059 	else
1060 		ibp->rvp.n_rc_resends += delta_psn(qp->s_psn, psn);
1061 
1062 	qp->s_flags &= ~(RVT_S_WAIT_FENCE | RVT_S_WAIT_RDMAR |
1063 			 RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_PSN |
1064 			 RVT_S_WAIT_ACK);
1065 	if (wait)
1066 		qp->s_flags |= RVT_S_SEND_ONE;
1067 	reset_psn(qp, psn);
1068 }
1069 
1070 /*
1071  * Set qp->s_sending_psn to the next PSN after the given one.
1072  * This would be psn+1 except when RDMA reads are present.
1073  */
1074 static void reset_sending_psn(struct rvt_qp *qp, u32 psn)
1075 {
1076 	struct rvt_swqe *wqe;
1077 	u32 n = qp->s_last;
1078 
1079 	lockdep_assert_held(&qp->s_lock);
1080 	/* Find the work request corresponding to the given PSN. */
1081 	for (;;) {
1082 		wqe = rvt_get_swqe_ptr(qp, n);
1083 		if (cmp_psn(psn, wqe->lpsn) <= 0) {
1084 			if (wqe->wr.opcode == IB_WR_RDMA_READ)
1085 				qp->s_sending_psn = wqe->lpsn + 1;
1086 			else
1087 				qp->s_sending_psn = psn + 1;
1088 			break;
1089 		}
1090 		if (++n == qp->s_size)
1091 			n = 0;
1092 		if (n == qp->s_tail)
1093 			break;
1094 	}
1095 }
1096 
1097 /*
1098  * This should be called with the QP s_lock held and interrupts disabled.
1099  */
1100 void hfi1_rc_send_complete(struct rvt_qp *qp, struct hfi1_opa_header *opah)
1101 {
1102 	struct ib_other_headers *ohdr;
1103 	struct hfi1_qp_priv *priv = qp->priv;
1104 	struct rvt_swqe *wqe;
1105 	struct ib_header *hdr = NULL;
1106 	struct hfi1_16b_header *hdr_16b = NULL;
1107 	u32 opcode;
1108 	u32 psn;
1109 
1110 	lockdep_assert_held(&qp->s_lock);
1111 	if (!(ib_rvt_state_ops[qp->state] & RVT_SEND_OR_FLUSH_OR_RECV_OK))
1112 		return;
1113 
1114 	/* Find out where the BTH is */
1115 	if (priv->hdr_type == HFI1_PKT_TYPE_9B) {
1116 		hdr = &opah->ibh;
1117 		if (ib_get_lnh(hdr) == HFI1_LRH_BTH)
1118 			ohdr = &hdr->u.oth;
1119 		else
1120 			ohdr = &hdr->u.l.oth;
1121 	} else {
1122 		u8 l4;
1123 
1124 		hdr_16b = &opah->opah;
1125 		l4  = hfi1_16B_get_l4(hdr_16b);
1126 		if (l4 == OPA_16B_L4_IB_LOCAL)
1127 			ohdr = &hdr_16b->u.oth;
1128 		else
1129 			ohdr = &hdr_16b->u.l.oth;
1130 	}
1131 
1132 	opcode = ib_bth_get_opcode(ohdr);
1133 	if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
1134 	    opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
1135 		WARN_ON(!qp->s_rdma_ack_cnt);
1136 		qp->s_rdma_ack_cnt--;
1137 		return;
1138 	}
1139 
1140 	psn = ib_bth_get_psn(ohdr);
1141 	reset_sending_psn(qp, psn);
1142 
1143 	/*
1144 	 * Start timer after a packet requesting an ACK has been sent and
1145 	 * there are still requests that haven't been acked.
1146 	 */
1147 	if ((psn & IB_BTH_REQ_ACK) && qp->s_acked != qp->s_tail &&
1148 	    !(qp->s_flags &
1149 		(RVT_S_TIMER | RVT_S_WAIT_RNR | RVT_S_WAIT_PSN)) &&
1150 		(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
1151 		rvt_add_retry_timer(qp);
1152 
1153 	while (qp->s_last != qp->s_acked) {
1154 		u32 s_last;
1155 
1156 		wqe = rvt_get_swqe_ptr(qp, qp->s_last);
1157 		if (cmp_psn(wqe->lpsn, qp->s_sending_psn) >= 0 &&
1158 		    cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0)
1159 			break;
1160 		rvt_qp_wqe_unreserve(qp, wqe);
1161 		s_last = qp->s_last;
1162 		trace_hfi1_qp_send_completion(qp, wqe, s_last);
1163 		if (++s_last >= qp->s_size)
1164 			s_last = 0;
1165 		qp->s_last = s_last;
1166 		/* see post_send() */
1167 		barrier();
1168 		rvt_put_swqe(wqe);
1169 		rvt_qp_swqe_complete(qp,
1170 				     wqe,
1171 				     ib_hfi1_wc_opcode[wqe->wr.opcode],
1172 				     IB_WC_SUCCESS);
1173 	}
1174 	/*
1175 	 * If we were waiting for sends to complete before re-sending,
1176 	 * and they are now complete, restart sending.
1177 	 */
1178 	trace_hfi1_sendcomplete(qp, psn);
1179 	if (qp->s_flags & RVT_S_WAIT_PSN &&
1180 	    cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
1181 		qp->s_flags &= ~RVT_S_WAIT_PSN;
1182 		qp->s_sending_psn = qp->s_psn;
1183 		qp->s_sending_hpsn = qp->s_psn - 1;
1184 		hfi1_schedule_send(qp);
1185 	}
1186 }
1187 
1188 static inline void update_last_psn(struct rvt_qp *qp, u32 psn)
1189 {
1190 	qp->s_last_psn = psn;
1191 }
1192 
1193 /*
1194  * Generate a SWQE completion.
1195  * This is similar to hfi1_send_complete but has to check to be sure
1196  * that the SGEs are not being referenced if the SWQE is being resent.
1197  */
1198 static struct rvt_swqe *do_rc_completion(struct rvt_qp *qp,
1199 					 struct rvt_swqe *wqe,
1200 					 struct hfi1_ibport *ibp)
1201 {
1202 	lockdep_assert_held(&qp->s_lock);
1203 	/*
1204 	 * Don't decrement refcount and don't generate a
1205 	 * completion if the SWQE is being resent until the send
1206 	 * is finished.
1207 	 */
1208 	if (cmp_psn(wqe->lpsn, qp->s_sending_psn) < 0 ||
1209 	    cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
1210 		u32 s_last;
1211 
1212 		rvt_put_swqe(wqe);
1213 		rvt_qp_wqe_unreserve(qp, wqe);
1214 		s_last = qp->s_last;
1215 		trace_hfi1_qp_send_completion(qp, wqe, s_last);
1216 		if (++s_last >= qp->s_size)
1217 			s_last = 0;
1218 		qp->s_last = s_last;
1219 		/* see post_send() */
1220 		barrier();
1221 		rvt_qp_swqe_complete(qp,
1222 				     wqe,
1223 				     ib_hfi1_wc_opcode[wqe->wr.opcode],
1224 				     IB_WC_SUCCESS);
1225 	} else {
1226 		struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
1227 
1228 		this_cpu_inc(*ibp->rvp.rc_delayed_comp);
1229 		/*
1230 		 * If send progress not running attempt to progress
1231 		 * SDMA queue.
1232 		 */
1233 		if (ppd->dd->flags & HFI1_HAS_SEND_DMA) {
1234 			struct sdma_engine *engine;
1235 			u8 sl = rdma_ah_get_sl(&qp->remote_ah_attr);
1236 			u8 sc5;
1237 
1238 			/* For now use sc to find engine */
1239 			sc5 = ibp->sl_to_sc[sl];
1240 			engine = qp_to_sdma_engine(qp, sc5);
1241 			sdma_engine_progress_schedule(engine);
1242 		}
1243 	}
1244 
1245 	qp->s_retry = qp->s_retry_cnt;
1246 	update_last_psn(qp, wqe->lpsn);
1247 
1248 	/*
1249 	 * If we are completing a request which is in the process of
1250 	 * being resent, we can stop re-sending it since we know the
1251 	 * responder has already seen it.
1252 	 */
1253 	if (qp->s_acked == qp->s_cur) {
1254 		if (++qp->s_cur >= qp->s_size)
1255 			qp->s_cur = 0;
1256 		qp->s_acked = qp->s_cur;
1257 		wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
1258 		if (qp->s_acked != qp->s_tail) {
1259 			qp->s_state = OP(SEND_LAST);
1260 			qp->s_psn = wqe->psn;
1261 		}
1262 	} else {
1263 		if (++qp->s_acked >= qp->s_size)
1264 			qp->s_acked = 0;
1265 		if (qp->state == IB_QPS_SQD && qp->s_acked == qp->s_cur)
1266 			qp->s_draining = 0;
1267 		wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
1268 	}
1269 	return wqe;
1270 }
1271 
1272 /**
1273  * do_rc_ack - process an incoming RC ACK
1274  * @qp: the QP the ACK came in on
1275  * @psn: the packet sequence number of the ACK
1276  * @opcode: the opcode of the request that resulted in the ACK
1277  *
1278  * This is called from rc_rcv_resp() to process an incoming RC ACK
1279  * for the given QP.
1280  * May be called at interrupt level, with the QP s_lock held.
1281  * Returns 1 if OK, 0 if current operation should be aborted (NAK).
1282  */
1283 static int do_rc_ack(struct rvt_qp *qp, u32 aeth, u32 psn, int opcode,
1284 		     u64 val, struct hfi1_ctxtdata *rcd)
1285 {
1286 	struct hfi1_ibport *ibp;
1287 	enum ib_wc_status status;
1288 	struct rvt_swqe *wqe;
1289 	int ret = 0;
1290 	u32 ack_psn;
1291 	int diff;
1292 
1293 	lockdep_assert_held(&qp->s_lock);
1294 	/*
1295 	 * Note that NAKs implicitly ACK outstanding SEND and RDMA write
1296 	 * requests and implicitly NAK RDMA read and atomic requests issued
1297 	 * before the NAK'ed request.  The MSN won't include the NAK'ed
1298 	 * request but will include an ACK'ed request(s).
1299 	 */
1300 	ack_psn = psn;
1301 	if (aeth >> IB_AETH_NAK_SHIFT)
1302 		ack_psn--;
1303 	wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
1304 	ibp = rcd_to_iport(rcd);
1305 
1306 	/*
1307 	 * The MSN might be for a later WQE than the PSN indicates so
1308 	 * only complete WQEs that the PSN finishes.
1309 	 */
1310 	while ((diff = delta_psn(ack_psn, wqe->lpsn)) >= 0) {
1311 		/*
1312 		 * RDMA_READ_RESPONSE_ONLY is a special case since
1313 		 * we want to generate completion events for everything
1314 		 * before the RDMA read, copy the data, then generate
1315 		 * the completion for the read.
1316 		 */
1317 		if (wqe->wr.opcode == IB_WR_RDMA_READ &&
1318 		    opcode == OP(RDMA_READ_RESPONSE_ONLY) &&
1319 		    diff == 0) {
1320 			ret = 1;
1321 			goto bail_stop;
1322 		}
1323 		/*
1324 		 * If this request is a RDMA read or atomic, and the ACK is
1325 		 * for a later operation, this ACK NAKs the RDMA read or
1326 		 * atomic.  In other words, only a RDMA_READ_LAST or ONLY
1327 		 * can ACK a RDMA read and likewise for atomic ops.  Note
1328 		 * that the NAK case can only happen if relaxed ordering is
1329 		 * used and requests are sent after an RDMA read or atomic
1330 		 * is sent but before the response is received.
1331 		 */
1332 		if ((wqe->wr.opcode == IB_WR_RDMA_READ &&
1333 		     (opcode != OP(RDMA_READ_RESPONSE_LAST) || diff != 0)) ||
1334 		    ((wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1335 		      wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) &&
1336 		     (opcode != OP(ATOMIC_ACKNOWLEDGE) || diff != 0))) {
1337 			/* Retry this request. */
1338 			if (!(qp->r_flags & RVT_R_RDMAR_SEQ)) {
1339 				qp->r_flags |= RVT_R_RDMAR_SEQ;
1340 				hfi1_restart_rc(qp, qp->s_last_psn + 1, 0);
1341 				if (list_empty(&qp->rspwait)) {
1342 					qp->r_flags |= RVT_R_RSP_SEND;
1343 					rvt_get_qp(qp);
1344 					list_add_tail(&qp->rspwait,
1345 						      &rcd->qp_wait_list);
1346 				}
1347 			}
1348 			/*
1349 			 * No need to process the ACK/NAK since we are
1350 			 * restarting an earlier request.
1351 			 */
1352 			goto bail_stop;
1353 		}
1354 		if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1355 		    wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
1356 			u64 *vaddr = wqe->sg_list[0].vaddr;
1357 			*vaddr = val;
1358 		}
1359 		if (qp->s_num_rd_atomic &&
1360 		    (wqe->wr.opcode == IB_WR_RDMA_READ ||
1361 		     wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1362 		     wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)) {
1363 			qp->s_num_rd_atomic--;
1364 			/* Restart sending task if fence is complete */
1365 			if ((qp->s_flags & RVT_S_WAIT_FENCE) &&
1366 			    !qp->s_num_rd_atomic) {
1367 				qp->s_flags &= ~(RVT_S_WAIT_FENCE |
1368 						 RVT_S_WAIT_ACK);
1369 				hfi1_schedule_send(qp);
1370 			} else if (qp->s_flags & RVT_S_WAIT_RDMAR) {
1371 				qp->s_flags &= ~(RVT_S_WAIT_RDMAR |
1372 						 RVT_S_WAIT_ACK);
1373 				hfi1_schedule_send(qp);
1374 			}
1375 		}
1376 		wqe = do_rc_completion(qp, wqe, ibp);
1377 		if (qp->s_acked == qp->s_tail)
1378 			break;
1379 	}
1380 
1381 	switch (aeth >> IB_AETH_NAK_SHIFT) {
1382 	case 0:         /* ACK */
1383 		this_cpu_inc(*ibp->rvp.rc_acks);
1384 		if (qp->s_acked != qp->s_tail) {
1385 			/*
1386 			 * We are expecting more ACKs so
1387 			 * mod the retry timer.
1388 			 */
1389 			rvt_mod_retry_timer(qp);
1390 			/*
1391 			 * We can stop re-sending the earlier packets and
1392 			 * continue with the next packet the receiver wants.
1393 			 */
1394 			if (cmp_psn(qp->s_psn, psn) <= 0)
1395 				reset_psn(qp, psn + 1);
1396 		} else {
1397 			/* No more acks - kill all timers */
1398 			rvt_stop_rc_timers(qp);
1399 			if (cmp_psn(qp->s_psn, psn) <= 0) {
1400 				qp->s_state = OP(SEND_LAST);
1401 				qp->s_psn = psn + 1;
1402 			}
1403 		}
1404 		if (qp->s_flags & RVT_S_WAIT_ACK) {
1405 			qp->s_flags &= ~RVT_S_WAIT_ACK;
1406 			hfi1_schedule_send(qp);
1407 		}
1408 		rvt_get_credit(qp, aeth);
1409 		qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1410 		qp->s_retry = qp->s_retry_cnt;
1411 		update_last_psn(qp, psn);
1412 		return 1;
1413 
1414 	case 1:         /* RNR NAK */
1415 		ibp->rvp.n_rnr_naks++;
1416 		if (qp->s_acked == qp->s_tail)
1417 			goto bail_stop;
1418 		if (qp->s_flags & RVT_S_WAIT_RNR)
1419 			goto bail_stop;
1420 		if (qp->s_rnr_retry == 0) {
1421 			status = IB_WC_RNR_RETRY_EXC_ERR;
1422 			goto class_b;
1423 		}
1424 		if (qp->s_rnr_retry_cnt < 7)
1425 			qp->s_rnr_retry--;
1426 
1427 		/* The last valid PSN is the previous PSN. */
1428 		update_last_psn(qp, psn - 1);
1429 
1430 		ibp->rvp.n_rc_resends += delta_psn(qp->s_psn, psn);
1431 
1432 		reset_psn(qp, psn);
1433 
1434 		qp->s_flags &= ~(RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_ACK);
1435 		rvt_stop_rc_timers(qp);
1436 		rvt_add_rnr_timer(qp, aeth);
1437 		return 0;
1438 
1439 	case 3:         /* NAK */
1440 		if (qp->s_acked == qp->s_tail)
1441 			goto bail_stop;
1442 		/* The last valid PSN is the previous PSN. */
1443 		update_last_psn(qp, psn - 1);
1444 		switch ((aeth >> IB_AETH_CREDIT_SHIFT) &
1445 			IB_AETH_CREDIT_MASK) {
1446 		case 0: /* PSN sequence error */
1447 			ibp->rvp.n_seq_naks++;
1448 			/*
1449 			 * Back up to the responder's expected PSN.
1450 			 * Note that we might get a NAK in the middle of an
1451 			 * RDMA READ response which terminates the RDMA
1452 			 * READ.
1453 			 */
1454 			hfi1_restart_rc(qp, psn, 0);
1455 			hfi1_schedule_send(qp);
1456 			break;
1457 
1458 		case 1: /* Invalid Request */
1459 			status = IB_WC_REM_INV_REQ_ERR;
1460 			ibp->rvp.n_other_naks++;
1461 			goto class_b;
1462 
1463 		case 2: /* Remote Access Error */
1464 			status = IB_WC_REM_ACCESS_ERR;
1465 			ibp->rvp.n_other_naks++;
1466 			goto class_b;
1467 
1468 		case 3: /* Remote Operation Error */
1469 			status = IB_WC_REM_OP_ERR;
1470 			ibp->rvp.n_other_naks++;
1471 class_b:
1472 			if (qp->s_last == qp->s_acked) {
1473 				rvt_send_complete(qp, wqe, status);
1474 				rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1475 			}
1476 			break;
1477 
1478 		default:
1479 			/* Ignore other reserved NAK error codes */
1480 			goto reserved;
1481 		}
1482 		qp->s_retry = qp->s_retry_cnt;
1483 		qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1484 		goto bail_stop;
1485 
1486 	default:                /* 2: reserved */
1487 reserved:
1488 		/* Ignore reserved NAK codes. */
1489 		goto bail_stop;
1490 	}
1491 	/* cannot be reached  */
1492 bail_stop:
1493 	rvt_stop_rc_timers(qp);
1494 	return ret;
1495 }
1496 
1497 /*
1498  * We have seen an out of sequence RDMA read middle or last packet.
1499  * This ACKs SENDs and RDMA writes up to the first RDMA read or atomic SWQE.
1500  */
1501 static void rdma_seq_err(struct rvt_qp *qp, struct hfi1_ibport *ibp, u32 psn,
1502 			 struct hfi1_ctxtdata *rcd)
1503 {
1504 	struct rvt_swqe *wqe;
1505 
1506 	lockdep_assert_held(&qp->s_lock);
1507 	/* Remove QP from retry timer */
1508 	rvt_stop_rc_timers(qp);
1509 
1510 	wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
1511 
1512 	while (cmp_psn(psn, wqe->lpsn) > 0) {
1513 		if (wqe->wr.opcode == IB_WR_RDMA_READ ||
1514 		    wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1515 		    wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
1516 			break;
1517 		wqe = do_rc_completion(qp, wqe, ibp);
1518 	}
1519 
1520 	ibp->rvp.n_rdma_seq++;
1521 	qp->r_flags |= RVT_R_RDMAR_SEQ;
1522 	hfi1_restart_rc(qp, qp->s_last_psn + 1, 0);
1523 	if (list_empty(&qp->rspwait)) {
1524 		qp->r_flags |= RVT_R_RSP_SEND;
1525 		rvt_get_qp(qp);
1526 		list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1527 	}
1528 }
1529 
1530 /**
1531  * rc_rcv_resp - process an incoming RC response packet
1532  * @packet: data packet information
1533  *
1534  * This is called from hfi1_rc_rcv() to process an incoming RC response
1535  * packet for the given QP.
1536  * Called at interrupt level.
1537  */
1538 static void rc_rcv_resp(struct hfi1_packet *packet)
1539 {
1540 	struct hfi1_ctxtdata *rcd = packet->rcd;
1541 	void *data = packet->payload;
1542 	u32 tlen = packet->tlen;
1543 	struct rvt_qp *qp = packet->qp;
1544 	struct hfi1_ibport *ibp;
1545 	struct ib_other_headers *ohdr = packet->ohdr;
1546 	struct rvt_swqe *wqe;
1547 	enum ib_wc_status status;
1548 	unsigned long flags;
1549 	int diff;
1550 	u64 val;
1551 	u32 aeth;
1552 	u32 psn = ib_bth_get_psn(packet->ohdr);
1553 	u32 pmtu = qp->pmtu;
1554 	u16 hdrsize = packet->hlen;
1555 	u8 opcode = packet->opcode;
1556 	u8 pad = packet->pad;
1557 	u8 extra_bytes = pad + packet->extra_byte + (SIZE_OF_CRC << 2);
1558 
1559 	spin_lock_irqsave(&qp->s_lock, flags);
1560 	trace_hfi1_ack(qp, psn);
1561 
1562 	/* Ignore invalid responses. */
1563 	if (cmp_psn(psn, READ_ONCE(qp->s_next_psn)) >= 0)
1564 		goto ack_done;
1565 
1566 	/* Ignore duplicate responses. */
1567 	diff = cmp_psn(psn, qp->s_last_psn);
1568 	if (unlikely(diff <= 0)) {
1569 		/* Update credits for "ghost" ACKs */
1570 		if (diff == 0 && opcode == OP(ACKNOWLEDGE)) {
1571 			aeth = be32_to_cpu(ohdr->u.aeth);
1572 			if ((aeth >> IB_AETH_NAK_SHIFT) == 0)
1573 				rvt_get_credit(qp, aeth);
1574 		}
1575 		goto ack_done;
1576 	}
1577 
1578 	/*
1579 	 * Skip everything other than the PSN we expect, if we are waiting
1580 	 * for a reply to a restarted RDMA read or atomic op.
1581 	 */
1582 	if (qp->r_flags & RVT_R_RDMAR_SEQ) {
1583 		if (cmp_psn(psn, qp->s_last_psn + 1) != 0)
1584 			goto ack_done;
1585 		qp->r_flags &= ~RVT_R_RDMAR_SEQ;
1586 	}
1587 
1588 	if (unlikely(qp->s_acked == qp->s_tail))
1589 		goto ack_done;
1590 	wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
1591 	status = IB_WC_SUCCESS;
1592 
1593 	switch (opcode) {
1594 	case OP(ACKNOWLEDGE):
1595 	case OP(ATOMIC_ACKNOWLEDGE):
1596 	case OP(RDMA_READ_RESPONSE_FIRST):
1597 		aeth = be32_to_cpu(ohdr->u.aeth);
1598 		if (opcode == OP(ATOMIC_ACKNOWLEDGE))
1599 			val = ib_u64_get(&ohdr->u.at.atomic_ack_eth);
1600 		else
1601 			val = 0;
1602 		if (!do_rc_ack(qp, aeth, psn, opcode, val, rcd) ||
1603 		    opcode != OP(RDMA_READ_RESPONSE_FIRST))
1604 			goto ack_done;
1605 		wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
1606 		if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1607 			goto ack_op_err;
1608 		/*
1609 		 * If this is a response to a resent RDMA read, we
1610 		 * have to be careful to copy the data to the right
1611 		 * location.
1612 		 */
1613 		qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1614 						  wqe, psn, pmtu);
1615 		goto read_middle;
1616 
1617 	case OP(RDMA_READ_RESPONSE_MIDDLE):
1618 		/* no AETH, no ACK */
1619 		if (unlikely(cmp_psn(psn, qp->s_last_psn + 1)))
1620 			goto ack_seq_err;
1621 		if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1622 			goto ack_op_err;
1623 read_middle:
1624 		if (unlikely(tlen != (hdrsize + pmtu + extra_bytes)))
1625 			goto ack_len_err;
1626 		if (unlikely(pmtu >= qp->s_rdma_read_len))
1627 			goto ack_len_err;
1628 
1629 		/*
1630 		 * We got a response so update the timeout.
1631 		 * 4.096 usec. * (1 << qp->timeout)
1632 		 */
1633 		rvt_mod_retry_timer(qp);
1634 		if (qp->s_flags & RVT_S_WAIT_ACK) {
1635 			qp->s_flags &= ~RVT_S_WAIT_ACK;
1636 			hfi1_schedule_send(qp);
1637 		}
1638 
1639 		if (opcode == OP(RDMA_READ_RESPONSE_MIDDLE))
1640 			qp->s_retry = qp->s_retry_cnt;
1641 
1642 		/*
1643 		 * Update the RDMA receive state but do the copy w/o
1644 		 * holding the locks and blocking interrupts.
1645 		 */
1646 		qp->s_rdma_read_len -= pmtu;
1647 		update_last_psn(qp, psn);
1648 		spin_unlock_irqrestore(&qp->s_lock, flags);
1649 		rvt_copy_sge(qp, &qp->s_rdma_read_sge,
1650 			     data, pmtu, false, false);
1651 		goto bail;
1652 
1653 	case OP(RDMA_READ_RESPONSE_ONLY):
1654 		aeth = be32_to_cpu(ohdr->u.aeth);
1655 		if (!do_rc_ack(qp, aeth, psn, opcode, 0, rcd))
1656 			goto ack_done;
1657 		/*
1658 		 * Check that the data size is >= 0 && <= pmtu.
1659 		 * Remember to account for ICRC (4).
1660 		 */
1661 		if (unlikely(tlen < (hdrsize + extra_bytes)))
1662 			goto ack_len_err;
1663 		/*
1664 		 * If this is a response to a resent RDMA read, we
1665 		 * have to be careful to copy the data to the right
1666 		 * location.
1667 		 */
1668 		wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
1669 		qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1670 						  wqe, psn, pmtu);
1671 		goto read_last;
1672 
1673 	case OP(RDMA_READ_RESPONSE_LAST):
1674 		/* ACKs READ req. */
1675 		if (unlikely(cmp_psn(psn, qp->s_last_psn + 1)))
1676 			goto ack_seq_err;
1677 		if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1678 			goto ack_op_err;
1679 		/*
1680 		 * Check that the data size is >= 1 && <= pmtu.
1681 		 * Remember to account for ICRC (4).
1682 		 */
1683 		if (unlikely(tlen <= (hdrsize + extra_bytes)))
1684 			goto ack_len_err;
1685 read_last:
1686 		tlen -= hdrsize + extra_bytes;
1687 		if (unlikely(tlen != qp->s_rdma_read_len))
1688 			goto ack_len_err;
1689 		aeth = be32_to_cpu(ohdr->u.aeth);
1690 		rvt_copy_sge(qp, &qp->s_rdma_read_sge,
1691 			     data, tlen, false, false);
1692 		WARN_ON(qp->s_rdma_read_sge.num_sge);
1693 		(void)do_rc_ack(qp, aeth, psn,
1694 				 OP(RDMA_READ_RESPONSE_LAST), 0, rcd);
1695 		goto ack_done;
1696 	}
1697 
1698 ack_op_err:
1699 	status = IB_WC_LOC_QP_OP_ERR;
1700 	goto ack_err;
1701 
1702 ack_seq_err:
1703 	ibp = rcd_to_iport(rcd);
1704 	rdma_seq_err(qp, ibp, psn, rcd);
1705 	goto ack_done;
1706 
1707 ack_len_err:
1708 	status = IB_WC_LOC_LEN_ERR;
1709 ack_err:
1710 	if (qp->s_last == qp->s_acked) {
1711 		rvt_send_complete(qp, wqe, status);
1712 		rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1713 	}
1714 ack_done:
1715 	spin_unlock_irqrestore(&qp->s_lock, flags);
1716 bail:
1717 	return;
1718 }
1719 
1720 static inline void rc_defered_ack(struct hfi1_ctxtdata *rcd,
1721 				  struct rvt_qp *qp)
1722 {
1723 	if (list_empty(&qp->rspwait)) {
1724 		qp->r_flags |= RVT_R_RSP_NAK;
1725 		rvt_get_qp(qp);
1726 		list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1727 	}
1728 }
1729 
1730 static inline void rc_cancel_ack(struct rvt_qp *qp)
1731 {
1732 	qp->r_adefered = 0;
1733 	if (list_empty(&qp->rspwait))
1734 		return;
1735 	list_del_init(&qp->rspwait);
1736 	qp->r_flags &= ~RVT_R_RSP_NAK;
1737 	rvt_put_qp(qp);
1738 }
1739 
1740 /**
1741  * rc_rcv_error - process an incoming duplicate or error RC packet
1742  * @ohdr: the other headers for this packet
1743  * @data: the packet data
1744  * @qp: the QP for this packet
1745  * @opcode: the opcode for this packet
1746  * @psn: the packet sequence number for this packet
1747  * @diff: the difference between the PSN and the expected PSN
1748  *
1749  * This is called from hfi1_rc_rcv() to process an unexpected
1750  * incoming RC packet for the given QP.
1751  * Called at interrupt level.
1752  * Return 1 if no more processing is needed; otherwise return 0 to
1753  * schedule a response to be sent.
1754  */
1755 static noinline int rc_rcv_error(struct ib_other_headers *ohdr, void *data,
1756 				 struct rvt_qp *qp, u32 opcode, u32 psn,
1757 				 int diff, struct hfi1_ctxtdata *rcd)
1758 {
1759 	struct hfi1_ibport *ibp = rcd_to_iport(rcd);
1760 	struct rvt_ack_entry *e;
1761 	unsigned long flags;
1762 	u8 i, prev;
1763 	int old_req;
1764 
1765 	trace_hfi1_rcv_error(qp, psn);
1766 	if (diff > 0) {
1767 		/*
1768 		 * Packet sequence error.
1769 		 * A NAK will ACK earlier sends and RDMA writes.
1770 		 * Don't queue the NAK if we already sent one.
1771 		 */
1772 		if (!qp->r_nak_state) {
1773 			ibp->rvp.n_rc_seqnak++;
1774 			qp->r_nak_state = IB_NAK_PSN_ERROR;
1775 			/* Use the expected PSN. */
1776 			qp->r_ack_psn = qp->r_psn;
1777 			/*
1778 			 * Wait to send the sequence NAK until all packets
1779 			 * in the receive queue have been processed.
1780 			 * Otherwise, we end up propagating congestion.
1781 			 */
1782 			rc_defered_ack(rcd, qp);
1783 		}
1784 		goto done;
1785 	}
1786 
1787 	/*
1788 	 * Handle a duplicate request.  Don't re-execute SEND, RDMA
1789 	 * write or atomic op.  Don't NAK errors, just silently drop
1790 	 * the duplicate request.  Note that r_sge, r_len, and
1791 	 * r_rcv_len may be in use so don't modify them.
1792 	 *
1793 	 * We are supposed to ACK the earliest duplicate PSN but we
1794 	 * can coalesce an outstanding duplicate ACK.  We have to
1795 	 * send the earliest so that RDMA reads can be restarted at
1796 	 * the requester's expected PSN.
1797 	 *
1798 	 * First, find where this duplicate PSN falls within the
1799 	 * ACKs previously sent.
1800 	 * old_req is true if there is an older response that is scheduled
1801 	 * to be sent before sending this one.
1802 	 */
1803 	e = NULL;
1804 	old_req = 1;
1805 	ibp->rvp.n_rc_dupreq++;
1806 
1807 	spin_lock_irqsave(&qp->s_lock, flags);
1808 
1809 	for (i = qp->r_head_ack_queue; ; i = prev) {
1810 		if (i == qp->s_tail_ack_queue)
1811 			old_req = 0;
1812 		if (i)
1813 			prev = i - 1;
1814 		else
1815 			prev = HFI1_MAX_RDMA_ATOMIC;
1816 		if (prev == qp->r_head_ack_queue) {
1817 			e = NULL;
1818 			break;
1819 		}
1820 		e = &qp->s_ack_queue[prev];
1821 		if (!e->opcode) {
1822 			e = NULL;
1823 			break;
1824 		}
1825 		if (cmp_psn(psn, e->psn) >= 0) {
1826 			if (prev == qp->s_tail_ack_queue &&
1827 			    cmp_psn(psn, e->lpsn) <= 0)
1828 				old_req = 0;
1829 			break;
1830 		}
1831 	}
1832 	switch (opcode) {
1833 	case OP(RDMA_READ_REQUEST): {
1834 		struct ib_reth *reth;
1835 		u32 offset;
1836 		u32 len;
1837 
1838 		/*
1839 		 * If we didn't find the RDMA read request in the ack queue,
1840 		 * we can ignore this request.
1841 		 */
1842 		if (!e || e->opcode != OP(RDMA_READ_REQUEST))
1843 			goto unlock_done;
1844 		/* RETH comes after BTH */
1845 		reth = &ohdr->u.rc.reth;
1846 		/*
1847 		 * Address range must be a subset of the original
1848 		 * request and start on pmtu boundaries.
1849 		 * We reuse the old ack_queue slot since the requester
1850 		 * should not back up and request an earlier PSN for the
1851 		 * same request.
1852 		 */
1853 		offset = delta_psn(psn, e->psn) * qp->pmtu;
1854 		len = be32_to_cpu(reth->length);
1855 		if (unlikely(offset + len != e->rdma_sge.sge_length))
1856 			goto unlock_done;
1857 		if (e->rdma_sge.mr) {
1858 			rvt_put_mr(e->rdma_sge.mr);
1859 			e->rdma_sge.mr = NULL;
1860 		}
1861 		if (len != 0) {
1862 			u32 rkey = be32_to_cpu(reth->rkey);
1863 			u64 vaddr = get_ib_reth_vaddr(reth);
1864 			int ok;
1865 
1866 			ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr, rkey,
1867 					 IB_ACCESS_REMOTE_READ);
1868 			if (unlikely(!ok))
1869 				goto unlock_done;
1870 		} else {
1871 			e->rdma_sge.vaddr = NULL;
1872 			e->rdma_sge.length = 0;
1873 			e->rdma_sge.sge_length = 0;
1874 		}
1875 		e->psn = psn;
1876 		if (old_req)
1877 			goto unlock_done;
1878 		qp->s_tail_ack_queue = prev;
1879 		break;
1880 	}
1881 
1882 	case OP(COMPARE_SWAP):
1883 	case OP(FETCH_ADD): {
1884 		/*
1885 		 * If we didn't find the atomic request in the ack queue
1886 		 * or the send engine is already backed up to send an
1887 		 * earlier entry, we can ignore this request.
1888 		 */
1889 		if (!e || e->opcode != (u8)opcode || old_req)
1890 			goto unlock_done;
1891 		qp->s_tail_ack_queue = prev;
1892 		break;
1893 	}
1894 
1895 	default:
1896 		/*
1897 		 * Ignore this operation if it doesn't request an ACK
1898 		 * or an earlier RDMA read or atomic is going to be resent.
1899 		 */
1900 		if (!(psn & IB_BTH_REQ_ACK) || old_req)
1901 			goto unlock_done;
1902 		/*
1903 		 * Resend the most recent ACK if this request is
1904 		 * after all the previous RDMA reads and atomics.
1905 		 */
1906 		if (i == qp->r_head_ack_queue) {
1907 			spin_unlock_irqrestore(&qp->s_lock, flags);
1908 			qp->r_nak_state = 0;
1909 			qp->r_ack_psn = qp->r_psn - 1;
1910 			goto send_ack;
1911 		}
1912 
1913 		/*
1914 		 * Resend the RDMA read or atomic op which
1915 		 * ACKs this duplicate request.
1916 		 */
1917 		qp->s_tail_ack_queue = i;
1918 		break;
1919 	}
1920 	qp->s_ack_state = OP(ACKNOWLEDGE);
1921 	qp->s_flags |= RVT_S_RESP_PENDING;
1922 	qp->r_nak_state = 0;
1923 	hfi1_schedule_send(qp);
1924 
1925 unlock_done:
1926 	spin_unlock_irqrestore(&qp->s_lock, flags);
1927 done:
1928 	return 1;
1929 
1930 send_ack:
1931 	return 0;
1932 }
1933 
1934 static inline void update_ack_queue(struct rvt_qp *qp, unsigned n)
1935 {
1936 	unsigned next;
1937 
1938 	next = n + 1;
1939 	if (next > HFI1_MAX_RDMA_ATOMIC)
1940 		next = 0;
1941 	qp->s_tail_ack_queue = next;
1942 	qp->s_ack_state = OP(ACKNOWLEDGE);
1943 }
1944 
1945 static void log_cca_event(struct hfi1_pportdata *ppd, u8 sl, u32 rlid,
1946 			  u32 lqpn, u32 rqpn, u8 svc_type)
1947 {
1948 	struct opa_hfi1_cong_log_event_internal *cc_event;
1949 	unsigned long flags;
1950 
1951 	if (sl >= OPA_MAX_SLS)
1952 		return;
1953 
1954 	spin_lock_irqsave(&ppd->cc_log_lock, flags);
1955 
1956 	ppd->threshold_cong_event_map[sl / 8] |= 1 << (sl % 8);
1957 	ppd->threshold_event_counter++;
1958 
1959 	cc_event = &ppd->cc_events[ppd->cc_log_idx++];
1960 	if (ppd->cc_log_idx == OPA_CONG_LOG_ELEMS)
1961 		ppd->cc_log_idx = 0;
1962 	cc_event->lqpn = lqpn & RVT_QPN_MASK;
1963 	cc_event->rqpn = rqpn & RVT_QPN_MASK;
1964 	cc_event->sl = sl;
1965 	cc_event->svc_type = svc_type;
1966 	cc_event->rlid = rlid;
1967 	/* keep timestamp in units of 1.024 usec */
1968 	cc_event->timestamp = ktime_get_ns() / 1024;
1969 
1970 	spin_unlock_irqrestore(&ppd->cc_log_lock, flags);
1971 }
1972 
1973 void process_becn(struct hfi1_pportdata *ppd, u8 sl, u32 rlid, u32 lqpn,
1974 		  u32 rqpn, u8 svc_type)
1975 {
1976 	struct cca_timer *cca_timer;
1977 	u16 ccti, ccti_incr, ccti_timer, ccti_limit;
1978 	u8 trigger_threshold;
1979 	struct cc_state *cc_state;
1980 	unsigned long flags;
1981 
1982 	if (sl >= OPA_MAX_SLS)
1983 		return;
1984 
1985 	cc_state = get_cc_state(ppd);
1986 
1987 	if (!cc_state)
1988 		return;
1989 
1990 	/*
1991 	 * 1) increase CCTI (for this SL)
1992 	 * 2) select IPG (i.e., call set_link_ipg())
1993 	 * 3) start timer
1994 	 */
1995 	ccti_limit = cc_state->cct.ccti_limit;
1996 	ccti_incr = cc_state->cong_setting.entries[sl].ccti_increase;
1997 	ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer;
1998 	trigger_threshold =
1999 		cc_state->cong_setting.entries[sl].trigger_threshold;
2000 
2001 	spin_lock_irqsave(&ppd->cca_timer_lock, flags);
2002 
2003 	cca_timer = &ppd->cca_timer[sl];
2004 	if (cca_timer->ccti < ccti_limit) {
2005 		if (cca_timer->ccti + ccti_incr <= ccti_limit)
2006 			cca_timer->ccti += ccti_incr;
2007 		else
2008 			cca_timer->ccti = ccti_limit;
2009 		set_link_ipg(ppd);
2010 	}
2011 
2012 	ccti = cca_timer->ccti;
2013 
2014 	if (!hrtimer_active(&cca_timer->hrtimer)) {
2015 		/* ccti_timer is in units of 1.024 usec */
2016 		unsigned long nsec = 1024 * ccti_timer;
2017 
2018 		hrtimer_start(&cca_timer->hrtimer, ns_to_ktime(nsec),
2019 			      HRTIMER_MODE_REL_PINNED);
2020 	}
2021 
2022 	spin_unlock_irqrestore(&ppd->cca_timer_lock, flags);
2023 
2024 	if ((trigger_threshold != 0) && (ccti >= trigger_threshold))
2025 		log_cca_event(ppd, sl, rlid, lqpn, rqpn, svc_type);
2026 }
2027 
2028 /**
2029  * hfi1_rc_rcv - process an incoming RC packet
2030  * @packet: data packet information
2031  *
2032  * This is called from qp_rcv() to process an incoming RC packet
2033  * for the given QP.
2034  * May be called at interrupt level.
2035  */
2036 void hfi1_rc_rcv(struct hfi1_packet *packet)
2037 {
2038 	struct hfi1_ctxtdata *rcd = packet->rcd;
2039 	void *data = packet->payload;
2040 	u32 tlen = packet->tlen;
2041 	struct rvt_qp *qp = packet->qp;
2042 	struct hfi1_ibport *ibp = rcd_to_iport(rcd);
2043 	struct ib_other_headers *ohdr = packet->ohdr;
2044 	u32 opcode = packet->opcode;
2045 	u32 hdrsize = packet->hlen;
2046 	u32 psn = ib_bth_get_psn(packet->ohdr);
2047 	u32 pad = packet->pad;
2048 	struct ib_wc wc;
2049 	u32 pmtu = qp->pmtu;
2050 	int diff;
2051 	struct ib_reth *reth;
2052 	unsigned long flags;
2053 	int ret;
2054 	bool copy_last = false, fecn;
2055 	u32 rkey;
2056 	u8 extra_bytes = pad + packet->extra_byte + (SIZE_OF_CRC << 2);
2057 
2058 	lockdep_assert_held(&qp->r_lock);
2059 
2060 	if (hfi1_ruc_check_hdr(ibp, packet))
2061 		return;
2062 
2063 	fecn = process_ecn(qp, packet);
2064 
2065 	/*
2066 	 * Process responses (ACKs) before anything else.  Note that the
2067 	 * packet sequence number will be for something in the send work
2068 	 * queue rather than the expected receive packet sequence number.
2069 	 * In other words, this QP is the requester.
2070 	 */
2071 	if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
2072 	    opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
2073 		rc_rcv_resp(packet);
2074 		return;
2075 	}
2076 
2077 	/* Compute 24 bits worth of difference. */
2078 	diff = delta_psn(psn, qp->r_psn);
2079 	if (unlikely(diff)) {
2080 		if (rc_rcv_error(ohdr, data, qp, opcode, psn, diff, rcd))
2081 			return;
2082 		goto send_ack;
2083 	}
2084 
2085 	/* Check for opcode sequence errors. */
2086 	switch (qp->r_state) {
2087 	case OP(SEND_FIRST):
2088 	case OP(SEND_MIDDLE):
2089 		if (opcode == OP(SEND_MIDDLE) ||
2090 		    opcode == OP(SEND_LAST) ||
2091 		    opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
2092 		    opcode == OP(SEND_LAST_WITH_INVALIDATE))
2093 			break;
2094 		goto nack_inv;
2095 
2096 	case OP(RDMA_WRITE_FIRST):
2097 	case OP(RDMA_WRITE_MIDDLE):
2098 		if (opcode == OP(RDMA_WRITE_MIDDLE) ||
2099 		    opcode == OP(RDMA_WRITE_LAST) ||
2100 		    opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
2101 			break;
2102 		goto nack_inv;
2103 
2104 	default:
2105 		if (opcode == OP(SEND_MIDDLE) ||
2106 		    opcode == OP(SEND_LAST) ||
2107 		    opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
2108 		    opcode == OP(SEND_LAST_WITH_INVALIDATE) ||
2109 		    opcode == OP(RDMA_WRITE_MIDDLE) ||
2110 		    opcode == OP(RDMA_WRITE_LAST) ||
2111 		    opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
2112 			goto nack_inv;
2113 		/*
2114 		 * Note that it is up to the requester to not send a new
2115 		 * RDMA read or atomic operation before receiving an ACK
2116 		 * for the previous operation.
2117 		 */
2118 		break;
2119 	}
2120 
2121 	if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
2122 		rvt_comm_est(qp);
2123 
2124 	/* OK, process the packet. */
2125 	switch (opcode) {
2126 	case OP(SEND_FIRST):
2127 		ret = rvt_get_rwqe(qp, false);
2128 		if (ret < 0)
2129 			goto nack_op_err;
2130 		if (!ret)
2131 			goto rnr_nak;
2132 		qp->r_rcv_len = 0;
2133 		/* FALLTHROUGH */
2134 	case OP(SEND_MIDDLE):
2135 	case OP(RDMA_WRITE_MIDDLE):
2136 send_middle:
2137 		/* Check for invalid length PMTU or posted rwqe len. */
2138 		/*
2139 		 * There will be no padding for 9B packet but 16B packets
2140 		 * will come in with some padding since we always add
2141 		 * CRC and LT bytes which will need to be flit aligned
2142 		 */
2143 		if (unlikely(tlen != (hdrsize + pmtu + extra_bytes)))
2144 			goto nack_inv;
2145 		qp->r_rcv_len += pmtu;
2146 		if (unlikely(qp->r_rcv_len > qp->r_len))
2147 			goto nack_inv;
2148 		rvt_copy_sge(qp, &qp->r_sge, data, pmtu, true, false);
2149 		break;
2150 
2151 	case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
2152 		/* consume RWQE */
2153 		ret = rvt_get_rwqe(qp, true);
2154 		if (ret < 0)
2155 			goto nack_op_err;
2156 		if (!ret)
2157 			goto rnr_nak;
2158 		goto send_last_imm;
2159 
2160 	case OP(SEND_ONLY):
2161 	case OP(SEND_ONLY_WITH_IMMEDIATE):
2162 	case OP(SEND_ONLY_WITH_INVALIDATE):
2163 		ret = rvt_get_rwqe(qp, false);
2164 		if (ret < 0)
2165 			goto nack_op_err;
2166 		if (!ret)
2167 			goto rnr_nak;
2168 		qp->r_rcv_len = 0;
2169 		if (opcode == OP(SEND_ONLY))
2170 			goto no_immediate_data;
2171 		if (opcode == OP(SEND_ONLY_WITH_INVALIDATE))
2172 			goto send_last_inv;
2173 		/* FALLTHROUGH -- for SEND_ONLY_WITH_IMMEDIATE */
2174 	case OP(SEND_LAST_WITH_IMMEDIATE):
2175 send_last_imm:
2176 		wc.ex.imm_data = ohdr->u.imm_data;
2177 		wc.wc_flags = IB_WC_WITH_IMM;
2178 		goto send_last;
2179 	case OP(SEND_LAST_WITH_INVALIDATE):
2180 send_last_inv:
2181 		rkey = be32_to_cpu(ohdr->u.ieth);
2182 		if (rvt_invalidate_rkey(qp, rkey))
2183 			goto no_immediate_data;
2184 		wc.ex.invalidate_rkey = rkey;
2185 		wc.wc_flags = IB_WC_WITH_INVALIDATE;
2186 		goto send_last;
2187 	case OP(RDMA_WRITE_LAST):
2188 		copy_last = rvt_is_user_qp(qp);
2189 		/* fall through */
2190 	case OP(SEND_LAST):
2191 no_immediate_data:
2192 		wc.wc_flags = 0;
2193 		wc.ex.imm_data = 0;
2194 send_last:
2195 		/* Check for invalid length. */
2196 		/* LAST len should be >= 1 */
2197 		if (unlikely(tlen < (hdrsize + extra_bytes)))
2198 			goto nack_inv;
2199 		/* Don't count the CRC(and padding and LT byte for 16B). */
2200 		tlen -= (hdrsize + extra_bytes);
2201 		wc.byte_len = tlen + qp->r_rcv_len;
2202 		if (unlikely(wc.byte_len > qp->r_len))
2203 			goto nack_inv;
2204 		rvt_copy_sge(qp, &qp->r_sge, data, tlen, true, copy_last);
2205 		rvt_put_ss(&qp->r_sge);
2206 		qp->r_msn++;
2207 		if (!__test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
2208 			break;
2209 		wc.wr_id = qp->r_wr_id;
2210 		wc.status = IB_WC_SUCCESS;
2211 		if (opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE) ||
2212 		    opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
2213 			wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
2214 		else
2215 			wc.opcode = IB_WC_RECV;
2216 		wc.qp = &qp->ibqp;
2217 		wc.src_qp = qp->remote_qpn;
2218 		wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr) & U16_MAX;
2219 		/*
2220 		 * It seems that IB mandates the presence of an SL in a
2221 		 * work completion only for the UD transport (see section
2222 		 * 11.4.2 of IBTA Vol. 1).
2223 		 *
2224 		 * However, the way the SL is chosen below is consistent
2225 		 * with the way that IB/qib works and is trying avoid
2226 		 * introducing incompatibilities.
2227 		 *
2228 		 * See also OPA Vol. 1, section 9.7.6, and table 9-17.
2229 		 */
2230 		wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
2231 		/* zero fields that are N/A */
2232 		wc.vendor_err = 0;
2233 		wc.pkey_index = 0;
2234 		wc.dlid_path_bits = 0;
2235 		wc.port_num = 0;
2236 		/* Signal completion event if the solicited bit is set. */
2237 		rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
2238 			     ib_bth_is_solicited(ohdr));
2239 		break;
2240 
2241 	case OP(RDMA_WRITE_ONLY):
2242 		copy_last = rvt_is_user_qp(qp);
2243 		/* fall through */
2244 	case OP(RDMA_WRITE_FIRST):
2245 	case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
2246 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
2247 			goto nack_inv;
2248 		/* consume RWQE */
2249 		reth = &ohdr->u.rc.reth;
2250 		qp->r_len = be32_to_cpu(reth->length);
2251 		qp->r_rcv_len = 0;
2252 		qp->r_sge.sg_list = NULL;
2253 		if (qp->r_len != 0) {
2254 			u32 rkey = be32_to_cpu(reth->rkey);
2255 			u64 vaddr = get_ib_reth_vaddr(reth);
2256 			int ok;
2257 
2258 			/* Check rkey & NAK */
2259 			ok = rvt_rkey_ok(qp, &qp->r_sge.sge, qp->r_len, vaddr,
2260 					 rkey, IB_ACCESS_REMOTE_WRITE);
2261 			if (unlikely(!ok))
2262 				goto nack_acc;
2263 			qp->r_sge.num_sge = 1;
2264 		} else {
2265 			qp->r_sge.num_sge = 0;
2266 			qp->r_sge.sge.mr = NULL;
2267 			qp->r_sge.sge.vaddr = NULL;
2268 			qp->r_sge.sge.length = 0;
2269 			qp->r_sge.sge.sge_length = 0;
2270 		}
2271 		if (opcode == OP(RDMA_WRITE_FIRST))
2272 			goto send_middle;
2273 		else if (opcode == OP(RDMA_WRITE_ONLY))
2274 			goto no_immediate_data;
2275 		ret = rvt_get_rwqe(qp, true);
2276 		if (ret < 0)
2277 			goto nack_op_err;
2278 		if (!ret) {
2279 			/* peer will send again */
2280 			rvt_put_ss(&qp->r_sge);
2281 			goto rnr_nak;
2282 		}
2283 		wc.ex.imm_data = ohdr->u.rc.imm_data;
2284 		wc.wc_flags = IB_WC_WITH_IMM;
2285 		goto send_last;
2286 
2287 	case OP(RDMA_READ_REQUEST): {
2288 		struct rvt_ack_entry *e;
2289 		u32 len;
2290 		u8 next;
2291 
2292 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
2293 			goto nack_inv;
2294 		next = qp->r_head_ack_queue + 1;
2295 		/* s_ack_queue is size HFI1_MAX_RDMA_ATOMIC+1 so use > not >= */
2296 		if (next > HFI1_MAX_RDMA_ATOMIC)
2297 			next = 0;
2298 		spin_lock_irqsave(&qp->s_lock, flags);
2299 		if (unlikely(next == qp->s_tail_ack_queue)) {
2300 			if (!qp->s_ack_queue[next].sent)
2301 				goto nack_inv_unlck;
2302 			update_ack_queue(qp, next);
2303 		}
2304 		e = &qp->s_ack_queue[qp->r_head_ack_queue];
2305 		if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
2306 			rvt_put_mr(e->rdma_sge.mr);
2307 			e->rdma_sge.mr = NULL;
2308 		}
2309 		reth = &ohdr->u.rc.reth;
2310 		len = be32_to_cpu(reth->length);
2311 		if (len) {
2312 			u32 rkey = be32_to_cpu(reth->rkey);
2313 			u64 vaddr = get_ib_reth_vaddr(reth);
2314 			int ok;
2315 
2316 			/* Check rkey & NAK */
2317 			ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr,
2318 					 rkey, IB_ACCESS_REMOTE_READ);
2319 			if (unlikely(!ok))
2320 				goto nack_acc_unlck;
2321 			/*
2322 			 * Update the next expected PSN.  We add 1 later
2323 			 * below, so only add the remainder here.
2324 			 */
2325 			qp->r_psn += rvt_div_mtu(qp, len - 1);
2326 		} else {
2327 			e->rdma_sge.mr = NULL;
2328 			e->rdma_sge.vaddr = NULL;
2329 			e->rdma_sge.length = 0;
2330 			e->rdma_sge.sge_length = 0;
2331 		}
2332 		e->opcode = opcode;
2333 		e->sent = 0;
2334 		e->psn = psn;
2335 		e->lpsn = qp->r_psn;
2336 		/*
2337 		 * We need to increment the MSN here instead of when we
2338 		 * finish sending the result since a duplicate request would
2339 		 * increment it more than once.
2340 		 */
2341 		qp->r_msn++;
2342 		qp->r_psn++;
2343 		qp->r_state = opcode;
2344 		qp->r_nak_state = 0;
2345 		qp->r_head_ack_queue = next;
2346 
2347 		/* Schedule the send engine. */
2348 		qp->s_flags |= RVT_S_RESP_PENDING;
2349 		if (fecn)
2350 			qp->s_flags |= RVT_S_ECN;
2351 		hfi1_schedule_send(qp);
2352 
2353 		spin_unlock_irqrestore(&qp->s_lock, flags);
2354 		return;
2355 	}
2356 
2357 	case OP(COMPARE_SWAP):
2358 	case OP(FETCH_ADD): {
2359 		struct ib_atomic_eth *ateth;
2360 		struct rvt_ack_entry *e;
2361 		u64 vaddr;
2362 		atomic64_t *maddr;
2363 		u64 sdata;
2364 		u32 rkey;
2365 		u8 next;
2366 
2367 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
2368 			goto nack_inv;
2369 		next = qp->r_head_ack_queue + 1;
2370 		if (next > HFI1_MAX_RDMA_ATOMIC)
2371 			next = 0;
2372 		spin_lock_irqsave(&qp->s_lock, flags);
2373 		if (unlikely(next == qp->s_tail_ack_queue)) {
2374 			if (!qp->s_ack_queue[next].sent)
2375 				goto nack_inv_unlck;
2376 			update_ack_queue(qp, next);
2377 		}
2378 		e = &qp->s_ack_queue[qp->r_head_ack_queue];
2379 		if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
2380 			rvt_put_mr(e->rdma_sge.mr);
2381 			e->rdma_sge.mr = NULL;
2382 		}
2383 		ateth = &ohdr->u.atomic_eth;
2384 		vaddr = get_ib_ateth_vaddr(ateth);
2385 		if (unlikely(vaddr & (sizeof(u64) - 1)))
2386 			goto nack_inv_unlck;
2387 		rkey = be32_to_cpu(ateth->rkey);
2388 		/* Check rkey & NAK */
2389 		if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
2390 					  vaddr, rkey,
2391 					  IB_ACCESS_REMOTE_ATOMIC)))
2392 			goto nack_acc_unlck;
2393 		/* Perform atomic OP and save result. */
2394 		maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
2395 		sdata = get_ib_ateth_swap(ateth);
2396 		e->atomic_data = (opcode == OP(FETCH_ADD)) ?
2397 			(u64)atomic64_add_return(sdata, maddr) - sdata :
2398 			(u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
2399 				      get_ib_ateth_compare(ateth),
2400 				      sdata);
2401 		rvt_put_mr(qp->r_sge.sge.mr);
2402 		qp->r_sge.num_sge = 0;
2403 		e->opcode = opcode;
2404 		e->sent = 0;
2405 		e->psn = psn;
2406 		e->lpsn = psn;
2407 		qp->r_msn++;
2408 		qp->r_psn++;
2409 		qp->r_state = opcode;
2410 		qp->r_nak_state = 0;
2411 		qp->r_head_ack_queue = next;
2412 
2413 		/* Schedule the send engine. */
2414 		qp->s_flags |= RVT_S_RESP_PENDING;
2415 		if (fecn)
2416 			qp->s_flags |= RVT_S_ECN;
2417 		hfi1_schedule_send(qp);
2418 
2419 		spin_unlock_irqrestore(&qp->s_lock, flags);
2420 		return;
2421 	}
2422 
2423 	default:
2424 		/* NAK unknown opcodes. */
2425 		goto nack_inv;
2426 	}
2427 	qp->r_psn++;
2428 	qp->r_state = opcode;
2429 	qp->r_ack_psn = psn;
2430 	qp->r_nak_state = 0;
2431 	/* Send an ACK if requested or required. */
2432 	if (psn & IB_BTH_REQ_ACK || fecn) {
2433 		if (packet->numpkt == 0 || fecn ||
2434 		    qp->r_adefered >= HFI1_PSN_CREDIT) {
2435 			rc_cancel_ack(qp);
2436 			goto send_ack;
2437 		}
2438 		qp->r_adefered++;
2439 		rc_defered_ack(rcd, qp);
2440 	}
2441 	return;
2442 
2443 rnr_nak:
2444 	qp->r_nak_state = qp->r_min_rnr_timer | IB_RNR_NAK;
2445 	qp->r_ack_psn = qp->r_psn;
2446 	/* Queue RNR NAK for later */
2447 	rc_defered_ack(rcd, qp);
2448 	return;
2449 
2450 nack_op_err:
2451 	rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
2452 	qp->r_nak_state = IB_NAK_REMOTE_OPERATIONAL_ERROR;
2453 	qp->r_ack_psn = qp->r_psn;
2454 	/* Queue NAK for later */
2455 	rc_defered_ack(rcd, qp);
2456 	return;
2457 
2458 nack_inv_unlck:
2459 	spin_unlock_irqrestore(&qp->s_lock, flags);
2460 nack_inv:
2461 	rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
2462 	qp->r_nak_state = IB_NAK_INVALID_REQUEST;
2463 	qp->r_ack_psn = qp->r_psn;
2464 	/* Queue NAK for later */
2465 	rc_defered_ack(rcd, qp);
2466 	return;
2467 
2468 nack_acc_unlck:
2469 	spin_unlock_irqrestore(&qp->s_lock, flags);
2470 nack_acc:
2471 	rvt_rc_error(qp, IB_WC_LOC_PROT_ERR);
2472 	qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
2473 	qp->r_ack_psn = qp->r_psn;
2474 send_ack:
2475 	hfi1_send_rc_ack(packet, fecn);
2476 }
2477 
2478 void hfi1_rc_hdrerr(
2479 	struct hfi1_ctxtdata *rcd,
2480 	struct hfi1_packet *packet,
2481 	struct rvt_qp *qp)
2482 {
2483 	struct hfi1_ibport *ibp = rcd_to_iport(rcd);
2484 	int diff;
2485 	u32 opcode;
2486 	u32 psn;
2487 
2488 	if (hfi1_ruc_check_hdr(ibp, packet))
2489 		return;
2490 
2491 	psn = ib_bth_get_psn(packet->ohdr);
2492 	opcode = ib_bth_get_opcode(packet->ohdr);
2493 
2494 	/* Only deal with RDMA Writes for now */
2495 	if (opcode < IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST) {
2496 		diff = delta_psn(psn, qp->r_psn);
2497 		if (!qp->r_nak_state && diff >= 0) {
2498 			ibp->rvp.n_rc_seqnak++;
2499 			qp->r_nak_state = IB_NAK_PSN_ERROR;
2500 			/* Use the expected PSN. */
2501 			qp->r_ack_psn = qp->r_psn;
2502 			/*
2503 			 * Wait to send the sequence
2504 			 * NAK until all packets
2505 			 * in the receive queue have
2506 			 * been processed.
2507 			 * Otherwise, we end up
2508 			 * propagating congestion.
2509 			 */
2510 			rc_defered_ack(rcd, qp);
2511 		} /* Out of sequence NAK */
2512 	} /* QP Request NAKs */
2513 }
2514