blob: 023498745b8a64a509f8080f312ea338dc1cb8d1 [file] [log] [blame]
Ralph Campbellf9315512010-05-23 21:44:54 -07001/*
2 * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
3 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34#include <linux/io.h>
35
36#include "qib.h"
37
38/* cut down ridiculously long IB macro names */
39#define OP(x) IB_OPCODE_RC_##x
40
Ralph Campbellf9315512010-05-23 21:44:54 -070041
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080042static u32 restart_sge(struct rvt_sge_state *ss, struct rvt_swqe *wqe,
Ralph Campbellf9315512010-05-23 21:44:54 -070043 u32 psn, u32 pmtu)
44{
45 u32 len;
46
47 len = ((psn - wqe->psn) & QIB_PSN_MASK) * pmtu;
48 ss->sge = wqe->sg_list[0];
49 ss->sg_list = wqe->sg_list + 1;
50 ss->num_sge = wqe->wr.num_sge;
51 ss->total_len = wqe->length;
Brian Welty3fc4a092017-02-08 05:27:43 -080052 rvt_skip_sge(ss, len, false);
Ralph Campbellf9315512010-05-23 21:44:54 -070053 return wqe->length - len;
54}
55
Ralph Campbellf9315512010-05-23 21:44:54 -070056/**
57 * qib_make_rc_ack - construct a response packet (ACK, NAK, or RDMA read)
58 * @dev: the device for this QP
59 * @qp: a pointer to the QP
60 * @ohdr: a pointer to the IB header being constructed
61 * @pmtu: the path MTU
62 *
63 * Return 1 if constructed; otherwise, return 0.
64 * Note that we are in the responder's side of the QP context.
65 * Note the QP s_lock must be held.
66 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080067static int qib_make_rc_ack(struct qib_ibdev *dev, struct rvt_qp *qp,
Mike Marciniszyn261a4352016-09-06 04:35:05 -070068 struct ib_other_headers *ohdr, u32 pmtu)
Ralph Campbellf9315512010-05-23 21:44:54 -070069{
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080070 struct rvt_ack_entry *e;
Ralph Campbellf9315512010-05-23 21:44:54 -070071 u32 hwords;
72 u32 len;
73 u32 bth0;
74 u32 bth2;
75
76 /* Don't send an ACK if we aren't supposed to. */
Harish Chegondidb3ef0e2016-01-22 13:07:42 -080077 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
Ralph Campbellf9315512010-05-23 21:44:54 -070078 goto bail;
79
80 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
81 hwords = 5;
82
83 switch (qp->s_ack_state) {
84 case OP(RDMA_READ_RESPONSE_LAST):
85 case OP(RDMA_READ_RESPONSE_ONLY):
86 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
87 if (e->rdma_sge.mr) {
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080088 rvt_put_mr(e->rdma_sge.mr);
Ralph Campbellf9315512010-05-23 21:44:54 -070089 e->rdma_sge.mr = NULL;
90 }
91 /* FALLTHROUGH */
92 case OP(ATOMIC_ACKNOWLEDGE):
93 /*
94 * We can increment the tail pointer now that the last
95 * response has been sent instead of only being
96 * constructed.
97 */
98 if (++qp->s_tail_ack_queue > QIB_MAX_RDMA_ATOMIC)
99 qp->s_tail_ack_queue = 0;
100 /* FALLTHROUGH */
101 case OP(SEND_ONLY):
102 case OP(ACKNOWLEDGE):
103 /* Check for no next entry in the queue. */
104 if (qp->r_head_ack_queue == qp->s_tail_ack_queue) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800105 if (qp->s_flags & RVT_S_ACK_PENDING)
Ralph Campbellf9315512010-05-23 21:44:54 -0700106 goto normal;
107 goto bail;
108 }
109
110 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
111 if (e->opcode == OP(RDMA_READ_REQUEST)) {
112 /*
113 * If a RDMA read response is being resent and
114 * we haven't seen the duplicate request yet,
115 * then stop sending the remaining responses the
116 * responder has seen until the requester resends it.
117 */
118 len = e->rdma_sge.sge_length;
119 if (len && !e->rdma_sge.mr) {
120 qp->s_tail_ack_queue = qp->r_head_ack_queue;
121 goto bail;
122 }
123 /* Copy SGE state in case we need to resend */
124 qp->s_rdma_mr = e->rdma_sge.mr;
125 if (qp->s_rdma_mr)
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800126 rvt_get_mr(qp->s_rdma_mr);
Ralph Campbellf9315512010-05-23 21:44:54 -0700127 qp->s_ack_rdma_sge.sge = e->rdma_sge;
128 qp->s_ack_rdma_sge.num_sge = 1;
129 qp->s_cur_sge = &qp->s_ack_rdma_sge;
130 if (len > pmtu) {
131 len = pmtu;
132 qp->s_ack_state = OP(RDMA_READ_RESPONSE_FIRST);
133 } else {
134 qp->s_ack_state = OP(RDMA_READ_RESPONSE_ONLY);
135 e->sent = 1;
136 }
Brian Welty696513e2017-02-08 05:27:07 -0800137 ohdr->u.aeth = rvt_compute_aeth(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -0700138 hwords++;
139 qp->s_ack_rdma_psn = e->psn;
140 bth2 = qp->s_ack_rdma_psn++ & QIB_PSN_MASK;
141 } else {
142 /* COMPARE_SWAP or FETCH_ADD */
143 qp->s_cur_sge = NULL;
144 len = 0;
145 qp->s_ack_state = OP(ATOMIC_ACKNOWLEDGE);
Brian Welty696513e2017-02-08 05:27:07 -0800146 ohdr->u.at.aeth = rvt_compute_aeth(qp);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700147 ib_u64_put(e->atomic_data, &ohdr->u.at.atomic_ack_eth);
Ralph Campbellf9315512010-05-23 21:44:54 -0700148 hwords += sizeof(ohdr->u.at) / sizeof(u32);
149 bth2 = e->psn & QIB_PSN_MASK;
150 e->sent = 1;
151 }
152 bth0 = qp->s_ack_state << 24;
153 break;
154
155 case OP(RDMA_READ_RESPONSE_FIRST):
156 qp->s_ack_state = OP(RDMA_READ_RESPONSE_MIDDLE);
157 /* FALLTHROUGH */
158 case OP(RDMA_READ_RESPONSE_MIDDLE):
159 qp->s_cur_sge = &qp->s_ack_rdma_sge;
160 qp->s_rdma_mr = qp->s_ack_rdma_sge.sge.mr;
161 if (qp->s_rdma_mr)
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800162 rvt_get_mr(qp->s_rdma_mr);
Ralph Campbellf9315512010-05-23 21:44:54 -0700163 len = qp->s_ack_rdma_sge.sge.sge_length;
164 if (len > pmtu)
165 len = pmtu;
166 else {
Brian Welty696513e2017-02-08 05:27:07 -0800167 ohdr->u.aeth = rvt_compute_aeth(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -0700168 hwords++;
169 qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
170 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
171 e->sent = 1;
172 }
173 bth0 = qp->s_ack_state << 24;
174 bth2 = qp->s_ack_rdma_psn++ & QIB_PSN_MASK;
175 break;
176
177 default:
178normal:
179 /*
180 * Send a regular ACK.
181 * Set the s_ack_state so we wait until after sending
182 * the ACK before setting s_ack_state to ACKNOWLEDGE
183 * (see above).
184 */
185 qp->s_ack_state = OP(SEND_ONLY);
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800186 qp->s_flags &= ~RVT_S_ACK_PENDING;
Ralph Campbellf9315512010-05-23 21:44:54 -0700187 qp->s_cur_sge = NULL;
188 if (qp->s_nak_state)
189 ohdr->u.aeth =
Don Hiatt832666c2017-02-08 05:28:25 -0800190 cpu_to_be32((qp->r_msn & IB_MSN_MASK) |
Ralph Campbellf9315512010-05-23 21:44:54 -0700191 (qp->s_nak_state <<
Don Hiatt832666c2017-02-08 05:28:25 -0800192 IB_AETH_CREDIT_SHIFT));
Ralph Campbellf9315512010-05-23 21:44:54 -0700193 else
Brian Welty696513e2017-02-08 05:27:07 -0800194 ohdr->u.aeth = rvt_compute_aeth(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -0700195 hwords++;
196 len = 0;
197 bth0 = OP(ACKNOWLEDGE) << 24;
198 bth2 = qp->s_ack_psn & QIB_PSN_MASK;
199 }
200 qp->s_rdma_ack_cnt++;
201 qp->s_hdrwords = hwords;
202 qp->s_cur_size = len;
203 qib_make_ruc_header(qp, ohdr, bth0, bth2);
204 return 1;
205
206bail:
207 qp->s_ack_state = OP(ACKNOWLEDGE);
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800208 qp->s_flags &= ~(RVT_S_RESP_PENDING | RVT_S_ACK_PENDING);
Ralph Campbellf9315512010-05-23 21:44:54 -0700209 return 0;
210}
211
212/**
213 * qib_make_rc_req - construct a request packet (SEND, RDMA r/w, ATOMIC)
214 * @qp: a pointer to the QP
215 *
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800216 * Assumes the s_lock is held.
217 *
Ralph Campbellf9315512010-05-23 21:44:54 -0700218 * Return 1 if constructed; otherwise, return 0.
219 */
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700220int qib_make_rc_req(struct rvt_qp *qp, unsigned long *flags)
Ralph Campbellf9315512010-05-23 21:44:54 -0700221{
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800222 struct qib_qp_priv *priv = qp->priv;
Ralph Campbellf9315512010-05-23 21:44:54 -0700223 struct qib_ibdev *dev = to_idev(qp->ibqp.device);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700224 struct ib_other_headers *ohdr;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800225 struct rvt_sge_state *ss;
226 struct rvt_swqe *wqe;
Ralph Campbellf9315512010-05-23 21:44:54 -0700227 u32 hwords;
228 u32 len;
229 u32 bth0;
230 u32 bth2;
Mike Marciniszyncc6ea132011-09-23 13:16:34 -0400231 u32 pmtu = qp->pmtu;
Ralph Campbellf9315512010-05-23 21:44:54 -0700232 char newreq;
Ralph Campbellf9315512010-05-23 21:44:54 -0700233 int ret = 0;
234 int delta;
235
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800236 ohdr = &priv->s_hdr->u.oth;
Ralph Campbellf9315512010-05-23 21:44:54 -0700237 if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800238 ohdr = &priv->s_hdr->u.l.oth;
Ralph Campbellf9315512010-05-23 21:44:54 -0700239
Ralph Campbellf9315512010-05-23 21:44:54 -0700240 /* Sending responses has higher priority over sending requests. */
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800241 if ((qp->s_flags & RVT_S_RESP_PENDING) &&
Ralph Campbellf9315512010-05-23 21:44:54 -0700242 qib_make_rc_ack(dev, qp, ohdr, pmtu))
243 goto done;
244
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800245 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_SEND_OK)) {
246 if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
Ralph Campbellf9315512010-05-23 21:44:54 -0700247 goto bail;
248 /* We are in the error state, flush the work request. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800249 smp_read_barrier_depends(); /* see post_one_send() */
Mike Marciniszyneb04ff02017-02-08 05:26:08 -0800250 if (qp->s_last == READ_ONCE(qp->s_head))
Ralph Campbellf9315512010-05-23 21:44:54 -0700251 goto bail;
252 /* If DMAs are in progress, we can't flush immediately. */
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800253 if (atomic_read(&priv->s_dma_busy)) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800254 qp->s_flags |= RVT_S_WAIT_DMA;
Ralph Campbellf9315512010-05-23 21:44:54 -0700255 goto bail;
256 }
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800257 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
Mike Marciniszyn30ab7e22011-11-04 08:26:52 -0400258 qib_send_complete(qp, wqe, qp->s_last != qp->s_acked ?
259 IB_WC_SUCCESS : IB_WC_WR_FLUSH_ERR);
260 /* will get called again */
Ralph Campbellf9315512010-05-23 21:44:54 -0700261 goto done;
262 }
263
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800264 if (qp->s_flags & (RVT_S_WAIT_RNR | RVT_S_WAIT_ACK))
Ralph Campbellf9315512010-05-23 21:44:54 -0700265 goto bail;
266
267 if (qib_cmp24(qp->s_psn, qp->s_sending_hpsn) <= 0) {
268 if (qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) <= 0) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800269 qp->s_flags |= RVT_S_WAIT_PSN;
Ralph Campbellf9315512010-05-23 21:44:54 -0700270 goto bail;
271 }
272 qp->s_sending_psn = qp->s_psn;
273 qp->s_sending_hpsn = qp->s_psn - 1;
274 }
275
276 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
277 hwords = 5;
278 bth0 = 0;
279
280 /* Send a request. */
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800281 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
Ralph Campbellf9315512010-05-23 21:44:54 -0700282 switch (qp->s_state) {
283 default:
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800284 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK))
Ralph Campbellf9315512010-05-23 21:44:54 -0700285 goto bail;
286 /*
287 * Resend an old request or start a new one.
288 *
289 * We keep track of the current SWQE so that
290 * we don't reset the "furthest progress" state
291 * if we need to back up.
292 */
293 newreq = 0;
294 if (qp->s_cur == qp->s_tail) {
295 /* Check if send work queue is empty. */
Mike Marciniszyneb04ff02017-02-08 05:26:08 -0800296 smp_read_barrier_depends(); /* see post_one_send() */
297 if (qp->s_tail == READ_ONCE(qp->s_head))
Ralph Campbellf9315512010-05-23 21:44:54 -0700298 goto bail;
299 /*
300 * If a fence is requested, wait for previous
301 * RDMA read and atomic operations to finish.
302 */
303 if ((wqe->wr.send_flags & IB_SEND_FENCE) &&
304 qp->s_num_rd_atomic) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800305 qp->s_flags |= RVT_S_WAIT_FENCE;
Ralph Campbellf9315512010-05-23 21:44:54 -0700306 goto bail;
307 }
Ralph Campbellf9315512010-05-23 21:44:54 -0700308 newreq = 1;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800309 qp->s_psn = wqe->psn;
Ralph Campbellf9315512010-05-23 21:44:54 -0700310 }
311 /*
312 * Note that we have to be careful not to modify the
313 * original work request since we may need to resend
314 * it.
315 */
316 len = wqe->length;
317 ss = &qp->s_sge;
318 bth2 = qp->s_psn & QIB_PSN_MASK;
319 switch (wqe->wr.opcode) {
320 case IB_WR_SEND:
321 case IB_WR_SEND_WITH_IMM:
322 /* If no credit, return. */
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800323 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
Brian Welty696513e2017-02-08 05:27:07 -0800324 rvt_cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800325 qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
Ralph Campbellf9315512010-05-23 21:44:54 -0700326 goto bail;
327 }
Ralph Campbellf9315512010-05-23 21:44:54 -0700328 if (len > pmtu) {
Ralph Campbellf9315512010-05-23 21:44:54 -0700329 qp->s_state = OP(SEND_FIRST);
330 len = pmtu;
331 break;
332 }
333 if (wqe->wr.opcode == IB_WR_SEND)
334 qp->s_state = OP(SEND_ONLY);
335 else {
336 qp->s_state = OP(SEND_ONLY_WITH_IMMEDIATE);
337 /* Immediate data comes after the BTH */
338 ohdr->u.imm_data = wqe->wr.ex.imm_data;
339 hwords += 1;
340 }
341 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
342 bth0 |= IB_BTH_SOLICITED;
343 bth2 |= IB_BTH_REQ_ACK;
344 if (++qp->s_cur == qp->s_size)
345 qp->s_cur = 0;
346 break;
347
348 case IB_WR_RDMA_WRITE:
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800349 if (newreq && !(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Ralph Campbellf9315512010-05-23 21:44:54 -0700350 qp->s_lsn++;
351 /* FALLTHROUGH */
352 case IB_WR_RDMA_WRITE_WITH_IMM:
353 /* If no credit, return. */
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800354 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
Brian Welty696513e2017-02-08 05:27:07 -0800355 rvt_cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800356 qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
Ralph Campbellf9315512010-05-23 21:44:54 -0700357 goto bail;
358 }
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100359
Ralph Campbellf9315512010-05-23 21:44:54 -0700360 ohdr->u.rc.reth.vaddr =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100361 cpu_to_be64(wqe->rdma_wr.remote_addr);
Ralph Campbellf9315512010-05-23 21:44:54 -0700362 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100363 cpu_to_be32(wqe->rdma_wr.rkey);
Ralph Campbellf9315512010-05-23 21:44:54 -0700364 ohdr->u.rc.reth.length = cpu_to_be32(len);
365 hwords += sizeof(struct ib_reth) / sizeof(u32);
Ralph Campbellf9315512010-05-23 21:44:54 -0700366 if (len > pmtu) {
Ralph Campbellf9315512010-05-23 21:44:54 -0700367 qp->s_state = OP(RDMA_WRITE_FIRST);
368 len = pmtu;
369 break;
370 }
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100371 if (wqe->rdma_wr.wr.opcode == IB_WR_RDMA_WRITE)
Ralph Campbellf9315512010-05-23 21:44:54 -0700372 qp->s_state = OP(RDMA_WRITE_ONLY);
373 else {
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100374 qp->s_state = OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
Ralph Campbellf9315512010-05-23 21:44:54 -0700375 /* Immediate data comes after RETH */
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100376 ohdr->u.rc.imm_data =
377 wqe->rdma_wr.wr.ex.imm_data;
Ralph Campbellf9315512010-05-23 21:44:54 -0700378 hwords += 1;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100379 if (wqe->rdma_wr.wr.send_flags & IB_SEND_SOLICITED)
Ralph Campbellf9315512010-05-23 21:44:54 -0700380 bth0 |= IB_BTH_SOLICITED;
381 }
382 bth2 |= IB_BTH_REQ_ACK;
383 if (++qp->s_cur == qp->s_size)
384 qp->s_cur = 0;
385 break;
386
387 case IB_WR_RDMA_READ:
388 /*
389 * Don't allow more operations to be started
390 * than the QP limits allow.
391 */
392 if (newreq) {
393 if (qp->s_num_rd_atomic >=
394 qp->s_max_rd_atomic) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800395 qp->s_flags |= RVT_S_WAIT_RDMAR;
Ralph Campbellf9315512010-05-23 21:44:54 -0700396 goto bail;
397 }
398 qp->s_num_rd_atomic++;
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800399 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Ralph Campbellf9315512010-05-23 21:44:54 -0700400 qp->s_lsn++;
Ralph Campbellf9315512010-05-23 21:44:54 -0700401 }
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100402
Ralph Campbellf9315512010-05-23 21:44:54 -0700403 ohdr->u.rc.reth.vaddr =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100404 cpu_to_be64(wqe->rdma_wr.remote_addr);
Ralph Campbellf9315512010-05-23 21:44:54 -0700405 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100406 cpu_to_be32(wqe->rdma_wr.rkey);
Ralph Campbellf9315512010-05-23 21:44:54 -0700407 ohdr->u.rc.reth.length = cpu_to_be32(len);
408 qp->s_state = OP(RDMA_READ_REQUEST);
409 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
410 ss = NULL;
411 len = 0;
412 bth2 |= IB_BTH_REQ_ACK;
413 if (++qp->s_cur == qp->s_size)
414 qp->s_cur = 0;
415 break;
416
417 case IB_WR_ATOMIC_CMP_AND_SWP:
418 case IB_WR_ATOMIC_FETCH_AND_ADD:
419 /*
420 * Don't allow more operations to be started
421 * than the QP limits allow.
422 */
423 if (newreq) {
424 if (qp->s_num_rd_atomic >=
425 qp->s_max_rd_atomic) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800426 qp->s_flags |= RVT_S_WAIT_RDMAR;
Ralph Campbellf9315512010-05-23 21:44:54 -0700427 goto bail;
428 }
429 qp->s_num_rd_atomic++;
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800430 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Ralph Campbellf9315512010-05-23 21:44:54 -0700431 qp->s_lsn++;
Ralph Campbellf9315512010-05-23 21:44:54 -0700432 }
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100433 if (wqe->atomic_wr.wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
Ralph Campbellf9315512010-05-23 21:44:54 -0700434 qp->s_state = OP(COMPARE_SWAP);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700435 put_ib_ateth_swap(wqe->atomic_wr.swap,
436 &ohdr->u.atomic_eth);
437 put_ib_ateth_swap(wqe->atomic_wr.compare_add,
438 &ohdr->u.atomic_eth);
Ralph Campbellf9315512010-05-23 21:44:54 -0700439 } else {
440 qp->s_state = OP(FETCH_ADD);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700441 put_ib_ateth_swap(wqe->atomic_wr.compare_add,
442 &ohdr->u.atomic_eth);
443 put_ib_ateth_swap(0, &ohdr->u.atomic_eth);
Ralph Campbellf9315512010-05-23 21:44:54 -0700444 }
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700445 put_ib_ateth_vaddr(wqe->atomic_wr.remote_addr,
446 &ohdr->u.atomic_eth);
Ralph Campbellf9315512010-05-23 21:44:54 -0700447 ohdr->u.atomic_eth.rkey = cpu_to_be32(
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100448 wqe->atomic_wr.rkey);
Ralph Campbellf9315512010-05-23 21:44:54 -0700449 hwords += sizeof(struct ib_atomic_eth) / sizeof(u32);
450 ss = NULL;
451 len = 0;
452 bth2 |= IB_BTH_REQ_ACK;
453 if (++qp->s_cur == qp->s_size)
454 qp->s_cur = 0;
455 break;
456
457 default:
458 goto bail;
459 }
460 qp->s_sge.sge = wqe->sg_list[0];
461 qp->s_sge.sg_list = wqe->sg_list + 1;
462 qp->s_sge.num_sge = wqe->wr.num_sge;
463 qp->s_sge.total_len = wqe->length;
464 qp->s_len = wqe->length;
465 if (newreq) {
466 qp->s_tail++;
467 if (qp->s_tail >= qp->s_size)
468 qp->s_tail = 0;
469 }
470 if (wqe->wr.opcode == IB_WR_RDMA_READ)
471 qp->s_psn = wqe->lpsn + 1;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800472 else
Ralph Campbellf9315512010-05-23 21:44:54 -0700473 qp->s_psn++;
Ralph Campbellf9315512010-05-23 21:44:54 -0700474 break;
475
476 case OP(RDMA_READ_RESPONSE_FIRST):
477 /*
478 * qp->s_state is normally set to the opcode of the
479 * last packet constructed for new requests and therefore
480 * is never set to RDMA read response.
481 * RDMA_READ_RESPONSE_FIRST is used by the ACK processing
482 * thread to indicate a SEND needs to be restarted from an
483 * earlier PSN without interferring with the sending thread.
484 * See qib_restart_rc().
485 */
486 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
487 /* FALLTHROUGH */
488 case OP(SEND_FIRST):
489 qp->s_state = OP(SEND_MIDDLE);
490 /* FALLTHROUGH */
491 case OP(SEND_MIDDLE):
492 bth2 = qp->s_psn++ & QIB_PSN_MASK;
Ralph Campbellf9315512010-05-23 21:44:54 -0700493 ss = &qp->s_sge;
494 len = qp->s_len;
495 if (len > pmtu) {
496 len = pmtu;
497 break;
498 }
499 if (wqe->wr.opcode == IB_WR_SEND)
500 qp->s_state = OP(SEND_LAST);
501 else {
502 qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
503 /* Immediate data comes after the BTH */
504 ohdr->u.imm_data = wqe->wr.ex.imm_data;
505 hwords += 1;
506 }
507 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
508 bth0 |= IB_BTH_SOLICITED;
509 bth2 |= IB_BTH_REQ_ACK;
510 qp->s_cur++;
511 if (qp->s_cur >= qp->s_size)
512 qp->s_cur = 0;
513 break;
514
515 case OP(RDMA_READ_RESPONSE_LAST):
516 /*
517 * qp->s_state is normally set to the opcode of the
518 * last packet constructed for new requests and therefore
519 * is never set to RDMA read response.
520 * RDMA_READ_RESPONSE_LAST is used by the ACK processing
521 * thread to indicate a RDMA write needs to be restarted from
522 * an earlier PSN without interferring with the sending thread.
523 * See qib_restart_rc().
524 */
525 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
526 /* FALLTHROUGH */
527 case OP(RDMA_WRITE_FIRST):
528 qp->s_state = OP(RDMA_WRITE_MIDDLE);
529 /* FALLTHROUGH */
530 case OP(RDMA_WRITE_MIDDLE):
531 bth2 = qp->s_psn++ & QIB_PSN_MASK;
Ralph Campbellf9315512010-05-23 21:44:54 -0700532 ss = &qp->s_sge;
533 len = qp->s_len;
534 if (len > pmtu) {
535 len = pmtu;
536 break;
537 }
538 if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
539 qp->s_state = OP(RDMA_WRITE_LAST);
540 else {
541 qp->s_state = OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
542 /* Immediate data comes after the BTH */
543 ohdr->u.imm_data = wqe->wr.ex.imm_data;
544 hwords += 1;
545 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
546 bth0 |= IB_BTH_SOLICITED;
547 }
548 bth2 |= IB_BTH_REQ_ACK;
549 qp->s_cur++;
550 if (qp->s_cur >= qp->s_size)
551 qp->s_cur = 0;
552 break;
553
554 case OP(RDMA_READ_RESPONSE_MIDDLE):
555 /*
556 * qp->s_state is normally set to the opcode of the
557 * last packet constructed for new requests and therefore
558 * is never set to RDMA read response.
559 * RDMA_READ_RESPONSE_MIDDLE is used by the ACK processing
560 * thread to indicate a RDMA read needs to be restarted from
561 * an earlier PSN without interferring with the sending thread.
562 * See qib_restart_rc().
563 */
564 len = ((qp->s_psn - wqe->psn) & QIB_PSN_MASK) * pmtu;
565 ohdr->u.rc.reth.vaddr =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100566 cpu_to_be64(wqe->rdma_wr.remote_addr + len);
Ralph Campbellf9315512010-05-23 21:44:54 -0700567 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100568 cpu_to_be32(wqe->rdma_wr.rkey);
Ralph Campbellf9315512010-05-23 21:44:54 -0700569 ohdr->u.rc.reth.length = cpu_to_be32(wqe->length - len);
570 qp->s_state = OP(RDMA_READ_REQUEST);
571 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
572 bth2 = (qp->s_psn & QIB_PSN_MASK) | IB_BTH_REQ_ACK;
573 qp->s_psn = wqe->lpsn + 1;
574 ss = NULL;
575 len = 0;
576 qp->s_cur++;
577 if (qp->s_cur == qp->s_size)
578 qp->s_cur = 0;
579 break;
580 }
581 qp->s_sending_hpsn = bth2;
582 delta = (((int) bth2 - (int) wqe->psn) << 8) >> 8;
583 if (delta && delta % QIB_PSN_CREDIT == 0)
584 bth2 |= IB_BTH_REQ_ACK;
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800585 if (qp->s_flags & RVT_S_SEND_ONE) {
586 qp->s_flags &= ~RVT_S_SEND_ONE;
587 qp->s_flags |= RVT_S_WAIT_ACK;
Ralph Campbellf9315512010-05-23 21:44:54 -0700588 bth2 |= IB_BTH_REQ_ACK;
589 }
590 qp->s_len -= len;
591 qp->s_hdrwords = hwords;
592 qp->s_cur_sge = ss;
593 qp->s_cur_size = len;
594 qib_make_ruc_header(qp, ohdr, bth0 | (qp->s_state << 24), bth2);
595done:
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800596 return 1;
Ralph Campbellf9315512010-05-23 21:44:54 -0700597bail:
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800598 qp->s_flags &= ~RVT_S_BUSY;
Ralph Campbellf9315512010-05-23 21:44:54 -0700599 return ret;
600}
601
602/**
603 * qib_send_rc_ack - Construct an ACK packet and send it
604 * @qp: a pointer to the QP
605 *
606 * This is called from qib_rc_rcv() and qib_kreceive().
607 * Note that RDMA reads and atomics are handled in the
608 * send side QP state and tasklet.
609 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800610void qib_send_rc_ack(struct rvt_qp *qp)
Ralph Campbellf9315512010-05-23 21:44:54 -0700611{
612 struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
613 struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
614 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
615 u64 pbc;
616 u16 lrh0;
617 u32 bth0;
618 u32 hwords;
619 u32 pbufn;
620 u32 __iomem *piobuf;
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700621 struct ib_header hdr;
622 struct ib_other_headers *ohdr;
Ralph Campbellf9315512010-05-23 21:44:54 -0700623 u32 control;
624 unsigned long flags;
625
626 spin_lock_irqsave(&qp->s_lock, flags);
627
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800628 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
Ralph Campbellf9315512010-05-23 21:44:54 -0700629 goto unlock;
630
631 /* Don't send ACK or NAK if a RDMA read or atomic is pending. */
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800632 if ((qp->s_flags & RVT_S_RESP_PENDING) || qp->s_rdma_ack_cnt)
Ralph Campbellf9315512010-05-23 21:44:54 -0700633 goto queue_ack;
634
635 /* Construct the header with s_lock held so APM doesn't change it. */
636 ohdr = &hdr.u.oth;
637 lrh0 = QIB_LRH_BTH;
638 /* header size in 32-bit words LRH+BTH+AETH = (8+12+4)/4. */
639 hwords = 6;
640 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
641 hwords += qib_make_grh(ibp, &hdr.u.l.grh,
642 &qp->remote_ah_attr.grh, hwords, 0);
643 ohdr = &hdr.u.l.oth;
644 lrh0 = QIB_LRH_GRH;
645 }
646 /* read pkey_index w/o lock (its atomic) */
647 bth0 = qib_get_pkey(ibp, qp->s_pkey_index) | (OP(ACKNOWLEDGE) << 24);
648 if (qp->s_mig_state == IB_MIG_MIGRATED)
649 bth0 |= IB_BTH_MIG_REQ;
650 if (qp->r_nak_state)
Don Hiatt832666c2017-02-08 05:28:25 -0800651 ohdr->u.aeth = cpu_to_be32((qp->r_msn & IB_MSN_MASK) |
Ralph Campbellf9315512010-05-23 21:44:54 -0700652 (qp->r_nak_state <<
Don Hiatt832666c2017-02-08 05:28:25 -0800653 IB_AETH_CREDIT_SHIFT));
Ralph Campbellf9315512010-05-23 21:44:54 -0700654 else
Brian Welty696513e2017-02-08 05:27:07 -0800655 ohdr->u.aeth = rvt_compute_aeth(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -0700656 lrh0 |= ibp->sl_to_vl[qp->remote_ah_attr.sl] << 12 |
657 qp->remote_ah_attr.sl << 4;
658 hdr.lrh[0] = cpu_to_be16(lrh0);
659 hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
660 hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
661 hdr.lrh[3] = cpu_to_be16(ppd->lid | qp->remote_ah_attr.src_path_bits);
662 ohdr->bth[0] = cpu_to_be32(bth0);
663 ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
664 ohdr->bth[2] = cpu_to_be32(qp->r_ack_psn & QIB_PSN_MASK);
665
666 spin_unlock_irqrestore(&qp->s_lock, flags);
667
668 /* Don't try to send ACKs if the link isn't ACTIVE */
669 if (!(ppd->lflags & QIBL_LINKACTIVE))
670 goto done;
671
672 control = dd->f_setpbc_control(ppd, hwords + SIZE_OF_CRC,
673 qp->s_srate, lrh0 >> 12);
674 /* length is + 1 for the control dword */
675 pbc = ((u64) control << 32) | (hwords + 1);
676
677 piobuf = dd->f_getsendbuf(ppd, pbc, &pbufn);
678 if (!piobuf) {
679 /*
680 * We are out of PIO buffers at the moment.
681 * Pass responsibility for sending the ACK to the
682 * send tasklet so that when a PIO buffer becomes
683 * available, the ACK is sent ahead of other outgoing
684 * packets.
685 */
686 spin_lock_irqsave(&qp->s_lock, flags);
687 goto queue_ack;
688 }
689
690 /*
691 * Write the pbc.
692 * We have to flush after the PBC for correctness
693 * on some cpus or WC buffer can be written out of order.
694 */
695 writeq(pbc, piobuf);
696
697 if (dd->flags & QIB_PIO_FLUSH_WC) {
698 u32 *hdrp = (u32 *) &hdr;
699
700 qib_flush_wc();
701 qib_pio_copy(piobuf + 2, hdrp, hwords - 1);
702 qib_flush_wc();
703 __raw_writel(hdrp[hwords - 1], piobuf + hwords + 1);
704 } else
705 qib_pio_copy(piobuf + 2, (u32 *) &hdr, hwords);
706
707 if (dd->flags & QIB_USE_SPCL_TRIG) {
708 u32 spcl_off = (pbufn >= dd->piobcnt2k) ? 2047 : 1023;
709
710 qib_flush_wc();
711 __raw_writel(0xaebecede, piobuf + spcl_off);
712 }
713
714 qib_flush_wc();
715 qib_sendbuf_done(dd, pbufn);
716
Mike Marciniszyn7d7632a2014-03-07 08:40:55 -0500717 this_cpu_inc(ibp->pmastats->n_unicast_xmit);
Ralph Campbellf9315512010-05-23 21:44:54 -0700718 goto done;
719
720queue_ack:
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800721 if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
Harish Chegondif24a6d42016-01-22 12:56:02 -0800722 this_cpu_inc(*ibp->rvp.rc_qacks);
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800723 qp->s_flags |= RVT_S_ACK_PENDING | RVT_S_RESP_PENDING;
Ralph Campbellf9315512010-05-23 21:44:54 -0700724 qp->s_nak_state = qp->r_nak_state;
725 qp->s_ack_psn = qp->r_ack_psn;
726
727 /* Schedule the send tasklet. */
728 qib_schedule_send(qp);
729 }
730unlock:
731 spin_unlock_irqrestore(&qp->s_lock, flags);
732done:
733 return;
734}
735
736/**
737 * reset_psn - reset the QP state to send starting from PSN
738 * @qp: the QP
739 * @psn: the packet sequence number to restart at
740 *
741 * This is called from qib_rc_rcv() to process an incoming RC ACK
742 * for the given QP.
743 * Called at interrupt level with the QP s_lock held.
744 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800745static void reset_psn(struct rvt_qp *qp, u32 psn)
Ralph Campbellf9315512010-05-23 21:44:54 -0700746{
747 u32 n = qp->s_acked;
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800748 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, n);
Ralph Campbellf9315512010-05-23 21:44:54 -0700749 u32 opcode;
750
751 qp->s_cur = n;
752
753 /*
754 * If we are starting the request from the beginning,
755 * let the normal send code handle initialization.
756 */
757 if (qib_cmp24(psn, wqe->psn) <= 0) {
758 qp->s_state = OP(SEND_LAST);
759 goto done;
760 }
761
762 /* Find the work request opcode corresponding to the given PSN. */
763 opcode = wqe->wr.opcode;
764 for (;;) {
765 int diff;
766
767 if (++n == qp->s_size)
768 n = 0;
769 if (n == qp->s_tail)
770 break;
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800771 wqe = rvt_get_swqe_ptr(qp, n);
Ralph Campbellf9315512010-05-23 21:44:54 -0700772 diff = qib_cmp24(psn, wqe->psn);
773 if (diff < 0)
774 break;
775 qp->s_cur = n;
776 /*
777 * If we are starting the request from the beginning,
778 * let the normal send code handle initialization.
779 */
780 if (diff == 0) {
781 qp->s_state = OP(SEND_LAST);
782 goto done;
783 }
784 opcode = wqe->wr.opcode;
785 }
786
787 /*
788 * Set the state to restart in the middle of a request.
789 * Don't change the s_sge, s_cur_sge, or s_cur_size.
790 * See qib_make_rc_req().
791 */
792 switch (opcode) {
793 case IB_WR_SEND:
794 case IB_WR_SEND_WITH_IMM:
795 qp->s_state = OP(RDMA_READ_RESPONSE_FIRST);
796 break;
797
798 case IB_WR_RDMA_WRITE:
799 case IB_WR_RDMA_WRITE_WITH_IMM:
800 qp->s_state = OP(RDMA_READ_RESPONSE_LAST);
801 break;
802
803 case IB_WR_RDMA_READ:
804 qp->s_state = OP(RDMA_READ_RESPONSE_MIDDLE);
805 break;
806
807 default:
808 /*
809 * This case shouldn't happen since its only
810 * one PSN per req.
811 */
812 qp->s_state = OP(SEND_LAST);
813 }
814done:
815 qp->s_psn = psn;
816 /*
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800817 * Set RVT_S_WAIT_PSN as qib_rc_complete() may start the timer
Ralph Campbellf9315512010-05-23 21:44:54 -0700818 * asynchronously before the send tasklet can get scheduled.
819 * Doing it in qib_make_rc_req() is too late.
820 */
821 if ((qib_cmp24(qp->s_psn, qp->s_sending_hpsn) <= 0) &&
822 (qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) <= 0))
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800823 qp->s_flags |= RVT_S_WAIT_PSN;
Ralph Campbellf9315512010-05-23 21:44:54 -0700824}
825
826/*
827 * Back up requester to resend the last un-ACKed request.
Ralph Campbella5210c12010-08-02 22:39:30 +0000828 * The QP r_lock and s_lock should be held and interrupts disabled.
Ralph Campbellf9315512010-05-23 21:44:54 -0700829 */
Venkata Sandeep Dhanalakotab4238e72017-02-08 05:27:25 -0800830void qib_restart_rc(struct rvt_qp *qp, u32 psn, int wait)
Ralph Campbellf9315512010-05-23 21:44:54 -0700831{
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800832 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Ralph Campbellf9315512010-05-23 21:44:54 -0700833 struct qib_ibport *ibp;
834
835 if (qp->s_retry == 0) {
836 if (qp->s_mig_state == IB_MIG_ARMED) {
837 qib_migrate_qp(qp);
838 qp->s_retry = qp->s_retry_cnt;
839 } else if (qp->s_last == qp->s_acked) {
840 qib_send_complete(qp, wqe, IB_WC_RETRY_EXC_ERR);
Harish Chegondi70696ea2016-02-03 14:20:27 -0800841 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Ralph Campbellf9315512010-05-23 21:44:54 -0700842 return;
843 } else /* XXX need to handle delayed completion */
844 return;
845 } else
846 qp->s_retry--;
847
848 ibp = to_iport(qp->ibqp.device, qp->port_num);
849 if (wqe->wr.opcode == IB_WR_RDMA_READ)
Harish Chegondif24a6d42016-01-22 12:56:02 -0800850 ibp->rvp.n_rc_resends++;
Ralph Campbellf9315512010-05-23 21:44:54 -0700851 else
Harish Chegondif24a6d42016-01-22 12:56:02 -0800852 ibp->rvp.n_rc_resends += (qp->s_psn - psn) & QIB_PSN_MASK;
Ralph Campbellf9315512010-05-23 21:44:54 -0700853
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800854 qp->s_flags &= ~(RVT_S_WAIT_FENCE | RVT_S_WAIT_RDMAR |
855 RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_PSN |
856 RVT_S_WAIT_ACK);
Ralph Campbellf9315512010-05-23 21:44:54 -0700857 if (wait)
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800858 qp->s_flags |= RVT_S_SEND_ONE;
Ralph Campbellf9315512010-05-23 21:44:54 -0700859 reset_psn(qp, psn);
860}
861
862/*
Ralph Campbellf9315512010-05-23 21:44:54 -0700863 * Set qp->s_sending_psn to the next PSN after the given one.
864 * This would be psn+1 except when RDMA reads are present.
865 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800866static void reset_sending_psn(struct rvt_qp *qp, u32 psn)
Ralph Campbellf9315512010-05-23 21:44:54 -0700867{
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800868 struct rvt_swqe *wqe;
Ralph Campbellf9315512010-05-23 21:44:54 -0700869 u32 n = qp->s_last;
870
871 /* Find the work request corresponding to the given PSN. */
872 for (;;) {
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800873 wqe = rvt_get_swqe_ptr(qp, n);
Ralph Campbellf9315512010-05-23 21:44:54 -0700874 if (qib_cmp24(psn, wqe->lpsn) <= 0) {
875 if (wqe->wr.opcode == IB_WR_RDMA_READ)
876 qp->s_sending_psn = wqe->lpsn + 1;
877 else
878 qp->s_sending_psn = psn + 1;
879 break;
880 }
881 if (++n == qp->s_size)
882 n = 0;
883 if (n == qp->s_tail)
884 break;
885 }
886}
887
888/*
889 * This should be called with the QP s_lock held and interrupts disabled.
890 */
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700891void qib_rc_send_complete(struct rvt_qp *qp, struct ib_header *hdr)
Ralph Campbellf9315512010-05-23 21:44:54 -0700892{
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700893 struct ib_other_headers *ohdr;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800894 struct rvt_swqe *wqe;
Ralph Campbellf9315512010-05-23 21:44:54 -0700895 u32 opcode;
896 u32 psn;
897
Mike Marciniszynf9215b52017-02-08 05:27:49 -0800898 if (!(ib_rvt_state_ops[qp->state] & RVT_SEND_OR_FLUSH_OR_RECV_OK))
Ralph Campbellf9315512010-05-23 21:44:54 -0700899 return;
900
901 /* Find out where the BTH is */
902 if ((be16_to_cpu(hdr->lrh[0]) & 3) == QIB_LRH_BTH)
903 ohdr = &hdr->u.oth;
904 else
905 ohdr = &hdr->u.l.oth;
906
907 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
908 if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
909 opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
910 WARN_ON(!qp->s_rdma_ack_cnt);
911 qp->s_rdma_ack_cnt--;
912 return;
913 }
914
915 psn = be32_to_cpu(ohdr->bth[2]);
916 reset_sending_psn(qp, psn);
917
918 /*
919 * Start timer after a packet requesting an ACK has been sent and
920 * there are still requests that haven't been acked.
921 */
922 if ((psn & IB_BTH_REQ_ACK) && qp->s_acked != qp->s_tail &&
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800923 !(qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR | RVT_S_WAIT_PSN)) &&
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800924 (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
Venkata Sandeep Dhanalakotab4238e72017-02-08 05:27:25 -0800925 rvt_add_retry_timer(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -0700926
927 while (qp->s_last != qp->s_acked) {
Mike Marciniszynee845412016-02-04 11:03:28 -0800928 u32 s_last;
929
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800930 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
Ralph Campbellf9315512010-05-23 21:44:54 -0700931 if (qib_cmp24(wqe->lpsn, qp->s_sending_psn) >= 0 &&
932 qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) <= 0)
933 break;
Mike Marciniszynee845412016-02-04 11:03:28 -0800934 s_last = qp->s_last;
935 if (++s_last >= qp->s_size)
936 s_last = 0;
937 qp->s_last = s_last;
938 /* see post_send() */
939 barrier();
Mike Marciniszync64607a2016-12-07 19:34:31 -0800940 rvt_put_swqe(wqe);
Mike Marciniszyn43a474a2017-03-20 17:25:04 -0700941 rvt_qp_swqe_complete(qp,
942 wqe,
943 ib_qib_wc_opcode[wqe->wr.opcode],
944 IB_WC_SUCCESS);
Ralph Campbellf9315512010-05-23 21:44:54 -0700945 }
946 /*
947 * If we were waiting for sends to complete before resending,
948 * and they are now complete, restart sending.
949 */
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800950 if (qp->s_flags & RVT_S_WAIT_PSN &&
Ralph Campbellf9315512010-05-23 21:44:54 -0700951 qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800952 qp->s_flags &= ~RVT_S_WAIT_PSN;
Ralph Campbellf9315512010-05-23 21:44:54 -0700953 qp->s_sending_psn = qp->s_psn;
954 qp->s_sending_hpsn = qp->s_psn - 1;
955 qib_schedule_send(qp);
956 }
957}
958
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800959static inline void update_last_psn(struct rvt_qp *qp, u32 psn)
Ralph Campbellf9315512010-05-23 21:44:54 -0700960{
961 qp->s_last_psn = psn;
962}
963
964/*
965 * Generate a SWQE completion.
966 * This is similar to qib_send_complete but has to check to be sure
967 * that the SGEs are not being referenced if the SWQE is being resent.
968 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800969static struct rvt_swqe *do_rc_completion(struct rvt_qp *qp,
970 struct rvt_swqe *wqe,
Ralph Campbellf9315512010-05-23 21:44:54 -0700971 struct qib_ibport *ibp)
972{
Ralph Campbellf9315512010-05-23 21:44:54 -0700973 /*
974 * Don't decrement refcount and don't generate a
975 * completion if the SWQE is being resent until the send
976 * is finished.
977 */
978 if (qib_cmp24(wqe->lpsn, qp->s_sending_psn) < 0 ||
979 qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
Mike Marciniszynee845412016-02-04 11:03:28 -0800980 u32 s_last;
981
Mike Marciniszync64607a2016-12-07 19:34:31 -0800982 rvt_put_swqe(wqe);
Mike Marciniszynee845412016-02-04 11:03:28 -0800983 s_last = qp->s_last;
984 if (++s_last >= qp->s_size)
985 s_last = 0;
986 qp->s_last = s_last;
987 /* see post_send() */
988 barrier();
Mike Marciniszyn43a474a2017-03-20 17:25:04 -0700989 rvt_qp_swqe_complete(qp,
990 wqe,
991 ib_qib_wc_opcode[wqe->wr.opcode],
992 IB_WC_SUCCESS);
Ralph Campbellf9315512010-05-23 21:44:54 -0700993 } else
Harish Chegondif24a6d42016-01-22 12:56:02 -0800994 this_cpu_inc(*ibp->rvp.rc_delayed_comp);
Ralph Campbellf9315512010-05-23 21:44:54 -0700995
996 qp->s_retry = qp->s_retry_cnt;
997 update_last_psn(qp, wqe->lpsn);
998
999 /*
1000 * If we are completing a request which is in the process of
1001 * being resent, we can stop resending it since we know the
1002 * responder has already seen it.
1003 */
1004 if (qp->s_acked == qp->s_cur) {
1005 if (++qp->s_cur >= qp->s_size)
1006 qp->s_cur = 0;
1007 qp->s_acked = qp->s_cur;
Harish Chegondidb3ef0e2016-01-22 13:07:42 -08001008 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
Ralph Campbellf9315512010-05-23 21:44:54 -07001009 if (qp->s_acked != qp->s_tail) {
1010 qp->s_state = OP(SEND_LAST);
1011 qp->s_psn = wqe->psn;
1012 }
1013 } else {
1014 if (++qp->s_acked >= qp->s_size)
1015 qp->s_acked = 0;
1016 if (qp->state == IB_QPS_SQD && qp->s_acked == qp->s_cur)
1017 qp->s_draining = 0;
Harish Chegondidb3ef0e2016-01-22 13:07:42 -08001018 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Ralph Campbellf9315512010-05-23 21:44:54 -07001019 }
1020 return wqe;
1021}
1022
1023/**
1024 * do_rc_ack - process an incoming RC ACK
1025 * @qp: the QP the ACK came in on
1026 * @psn: the packet sequence number of the ACK
1027 * @opcode: the opcode of the request that resulted in the ACK
1028 *
1029 * This is called from qib_rc_rcv_resp() to process an incoming RC ACK
1030 * for the given QP.
1031 * Called at interrupt level with the QP s_lock held.
1032 * Returns 1 if OK, 0 if current operation should be aborted (NAK).
1033 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001034static int do_rc_ack(struct rvt_qp *qp, u32 aeth, u32 psn, int opcode,
Ralph Campbellf9315512010-05-23 21:44:54 -07001035 u64 val, struct qib_ctxtdata *rcd)
1036{
1037 struct qib_ibport *ibp;
1038 enum ib_wc_status status;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001039 struct rvt_swqe *wqe;
Ralph Campbellf9315512010-05-23 21:44:54 -07001040 int ret = 0;
1041 u32 ack_psn;
1042 int diff;
1043
Ralph Campbellf9315512010-05-23 21:44:54 -07001044 /*
1045 * Note that NAKs implicitly ACK outstanding SEND and RDMA write
1046 * requests and implicitly NAK RDMA read and atomic requests issued
1047 * before the NAK'ed request. The MSN won't include the NAK'ed
1048 * request but will include an ACK'ed request(s).
1049 */
1050 ack_psn = psn;
Don Hiatt832666c2017-02-08 05:28:25 -08001051 if (aeth >> IB_AETH_NAK_SHIFT)
Ralph Campbellf9315512010-05-23 21:44:54 -07001052 ack_psn--;
Harish Chegondidb3ef0e2016-01-22 13:07:42 -08001053 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Ralph Campbellf9315512010-05-23 21:44:54 -07001054 ibp = to_iport(qp->ibqp.device, qp->port_num);
1055
1056 /*
1057 * The MSN might be for a later WQE than the PSN indicates so
1058 * only complete WQEs that the PSN finishes.
1059 */
1060 while ((diff = qib_cmp24(ack_psn, wqe->lpsn)) >= 0) {
1061 /*
1062 * RDMA_READ_RESPONSE_ONLY is a special case since
1063 * we want to generate completion events for everything
1064 * before the RDMA read, copy the data, then generate
1065 * the completion for the read.
1066 */
1067 if (wqe->wr.opcode == IB_WR_RDMA_READ &&
1068 opcode == OP(RDMA_READ_RESPONSE_ONLY) &&
1069 diff == 0) {
1070 ret = 1;
1071 goto bail;
1072 }
1073 /*
1074 * If this request is a RDMA read or atomic, and the ACK is
1075 * for a later operation, this ACK NAKs the RDMA read or
1076 * atomic. In other words, only a RDMA_READ_LAST or ONLY
1077 * can ACK a RDMA read and likewise for atomic ops. Note
1078 * that the NAK case can only happen if relaxed ordering is
1079 * used and requests are sent after an RDMA read or atomic
1080 * is sent but before the response is received.
1081 */
1082 if ((wqe->wr.opcode == IB_WR_RDMA_READ &&
1083 (opcode != OP(RDMA_READ_RESPONSE_LAST) || diff != 0)) ||
1084 ((wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1085 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) &&
1086 (opcode != OP(ATOMIC_ACKNOWLEDGE) || diff != 0))) {
1087 /* Retry this request. */
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001088 if (!(qp->r_flags & RVT_R_RDMAR_SEQ)) {
1089 qp->r_flags |= RVT_R_RDMAR_SEQ;
Ralph Campbellf9315512010-05-23 21:44:54 -07001090 qib_restart_rc(qp, qp->s_last_psn + 1, 0);
1091 if (list_empty(&qp->rspwait)) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001092 qp->r_flags |= RVT_R_RSP_SEND;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001093 rvt_get_qp(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07001094 list_add_tail(&qp->rspwait,
1095 &rcd->qp_wait_list);
1096 }
1097 }
1098 /*
1099 * No need to process the ACK/NAK since we are
1100 * restarting an earlier request.
1101 */
1102 goto bail;
1103 }
1104 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1105 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
1106 u64 *vaddr = wqe->sg_list[0].vaddr;
1107 *vaddr = val;
1108 }
1109 if (qp->s_num_rd_atomic &&
1110 (wqe->wr.opcode == IB_WR_RDMA_READ ||
1111 wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1112 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)) {
1113 qp->s_num_rd_atomic--;
1114 /* Restart sending task if fence is complete */
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001115 if ((qp->s_flags & RVT_S_WAIT_FENCE) &&
Ralph Campbellf9315512010-05-23 21:44:54 -07001116 !qp->s_num_rd_atomic) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001117 qp->s_flags &= ~(RVT_S_WAIT_FENCE |
1118 RVT_S_WAIT_ACK);
Ralph Campbellf9315512010-05-23 21:44:54 -07001119 qib_schedule_send(qp);
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001120 } else if (qp->s_flags & RVT_S_WAIT_RDMAR) {
1121 qp->s_flags &= ~(RVT_S_WAIT_RDMAR |
1122 RVT_S_WAIT_ACK);
Ralph Campbellf9315512010-05-23 21:44:54 -07001123 qib_schedule_send(qp);
1124 }
1125 }
1126 wqe = do_rc_completion(qp, wqe, ibp);
1127 if (qp->s_acked == qp->s_tail)
1128 break;
1129 }
1130
Don Hiatt832666c2017-02-08 05:28:25 -08001131 switch (aeth >> IB_AETH_NAK_SHIFT) {
Ralph Campbellf9315512010-05-23 21:44:54 -07001132 case 0: /* ACK */
Harish Chegondif24a6d42016-01-22 12:56:02 -08001133 this_cpu_inc(*ibp->rvp.rc_acks);
Ralph Campbellf9315512010-05-23 21:44:54 -07001134 if (qp->s_acked != qp->s_tail) {
1135 /*
1136 * We are expecting more ACKs so
1137 * reset the retransmit timer.
1138 */
Venkata Sandeep Dhanalakotab4238e72017-02-08 05:27:25 -08001139 rvt_mod_retry_timer(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07001140 /*
1141 * We can stop resending the earlier packets and
1142 * continue with the next packet the receiver wants.
1143 */
1144 if (qib_cmp24(qp->s_psn, psn) <= 0)
1145 reset_psn(qp, psn + 1);
Venkata Sandeep Dhanalakotab4238e72017-02-08 05:27:25 -08001146 } else {
1147 /* No more acks - kill all timers */
1148 rvt_stop_rc_timers(qp);
1149 if (qib_cmp24(qp->s_psn, psn) <= 0) {
1150 qp->s_state = OP(SEND_LAST);
1151 qp->s_psn = psn + 1;
1152 }
Ralph Campbellf9315512010-05-23 21:44:54 -07001153 }
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001154 if (qp->s_flags & RVT_S_WAIT_ACK) {
1155 qp->s_flags &= ~RVT_S_WAIT_ACK;
Ralph Campbellf9315512010-05-23 21:44:54 -07001156 qib_schedule_send(qp);
1157 }
Brian Welty696513e2017-02-08 05:27:07 -08001158 rvt_get_credit(qp, aeth);
Ralph Campbellf9315512010-05-23 21:44:54 -07001159 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1160 qp->s_retry = qp->s_retry_cnt;
1161 update_last_psn(qp, psn);
Venkata Sandeep Dhanalakotab4238e72017-02-08 05:27:25 -08001162 return 1;
Ralph Campbellf9315512010-05-23 21:44:54 -07001163
1164 case 1: /* RNR NAK */
Harish Chegondif24a6d42016-01-22 12:56:02 -08001165 ibp->rvp.n_rnr_naks++;
Ralph Campbellf9315512010-05-23 21:44:54 -07001166 if (qp->s_acked == qp->s_tail)
1167 goto bail;
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001168 if (qp->s_flags & RVT_S_WAIT_RNR)
Ralph Campbellf9315512010-05-23 21:44:54 -07001169 goto bail;
1170 if (qp->s_rnr_retry == 0) {
1171 status = IB_WC_RNR_RETRY_EXC_ERR;
1172 goto class_b;
1173 }
1174 if (qp->s_rnr_retry_cnt < 7)
1175 qp->s_rnr_retry--;
1176
1177 /* The last valid PSN is the previous PSN. */
1178 update_last_psn(qp, psn - 1);
1179
Harish Chegondif24a6d42016-01-22 12:56:02 -08001180 ibp->rvp.n_rc_resends += (qp->s_psn - psn) & QIB_PSN_MASK;
Ralph Campbellf9315512010-05-23 21:44:54 -07001181
1182 reset_psn(qp, psn);
1183
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001184 qp->s_flags &= ~(RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_ACK);
Venkata Sandeep Dhanalakotab4238e72017-02-08 05:27:25 -08001185 rvt_stop_rc_timers(qp);
1186 rvt_add_rnr_timer(qp, aeth);
1187 return 0;
Ralph Campbellf9315512010-05-23 21:44:54 -07001188
1189 case 3: /* NAK */
1190 if (qp->s_acked == qp->s_tail)
1191 goto bail;
1192 /* The last valid PSN is the previous PSN. */
1193 update_last_psn(qp, psn - 1);
Don Hiatt832666c2017-02-08 05:28:25 -08001194 switch ((aeth >> IB_AETH_CREDIT_SHIFT) &
1195 IB_AETH_CREDIT_MASK) {
Ralph Campbellf9315512010-05-23 21:44:54 -07001196 case 0: /* PSN sequence error */
Harish Chegondif24a6d42016-01-22 12:56:02 -08001197 ibp->rvp.n_seq_naks++;
Ralph Campbellf9315512010-05-23 21:44:54 -07001198 /*
1199 * Back up to the responder's expected PSN.
1200 * Note that we might get a NAK in the middle of an
1201 * RDMA READ response which terminates the RDMA
1202 * READ.
1203 */
1204 qib_restart_rc(qp, psn, 0);
1205 qib_schedule_send(qp);
1206 break;
1207
1208 case 1: /* Invalid Request */
1209 status = IB_WC_REM_INV_REQ_ERR;
Harish Chegondif24a6d42016-01-22 12:56:02 -08001210 ibp->rvp.n_other_naks++;
Ralph Campbellf9315512010-05-23 21:44:54 -07001211 goto class_b;
1212
1213 case 2: /* Remote Access Error */
1214 status = IB_WC_REM_ACCESS_ERR;
Harish Chegondif24a6d42016-01-22 12:56:02 -08001215 ibp->rvp.n_other_naks++;
Ralph Campbellf9315512010-05-23 21:44:54 -07001216 goto class_b;
1217
1218 case 3: /* Remote Operation Error */
1219 status = IB_WC_REM_OP_ERR;
Harish Chegondif24a6d42016-01-22 12:56:02 -08001220 ibp->rvp.n_other_naks++;
Ralph Campbellf9315512010-05-23 21:44:54 -07001221class_b:
1222 if (qp->s_last == qp->s_acked) {
1223 qib_send_complete(qp, wqe, status);
Harish Chegondi70696ea2016-02-03 14:20:27 -08001224 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Ralph Campbellf9315512010-05-23 21:44:54 -07001225 }
1226 break;
1227
1228 default:
1229 /* Ignore other reserved NAK error codes */
1230 goto reserved;
1231 }
1232 qp->s_retry = qp->s_retry_cnt;
1233 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1234 goto bail;
1235
1236 default: /* 2: reserved */
1237reserved:
1238 /* Ignore reserved NAK codes. */
1239 goto bail;
1240 }
1241
1242bail:
Venkata Sandeep Dhanalakotab4238e72017-02-08 05:27:25 -08001243 rvt_stop_rc_timers(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07001244 return ret;
1245}
1246
1247/*
1248 * We have seen an out of sequence RDMA read middle or last packet.
1249 * This ACKs SENDs and RDMA writes up to the first RDMA read or atomic SWQE.
1250 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001251static void rdma_seq_err(struct rvt_qp *qp, struct qib_ibport *ibp, u32 psn,
Ralph Campbellf9315512010-05-23 21:44:54 -07001252 struct qib_ctxtdata *rcd)
1253{
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001254 struct rvt_swqe *wqe;
Ralph Campbellf9315512010-05-23 21:44:54 -07001255
1256 /* Remove QP from retry timer */
Venkata Sandeep Dhanalakotab4238e72017-02-08 05:27:25 -08001257 rvt_stop_rc_timers(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07001258
Harish Chegondidb3ef0e2016-01-22 13:07:42 -08001259 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Ralph Campbellf9315512010-05-23 21:44:54 -07001260
1261 while (qib_cmp24(psn, wqe->lpsn) > 0) {
1262 if (wqe->wr.opcode == IB_WR_RDMA_READ ||
1263 wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1264 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
1265 break;
1266 wqe = do_rc_completion(qp, wqe, ibp);
1267 }
1268
Harish Chegondif24a6d42016-01-22 12:56:02 -08001269 ibp->rvp.n_rdma_seq++;
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001270 qp->r_flags |= RVT_R_RDMAR_SEQ;
Ralph Campbellf9315512010-05-23 21:44:54 -07001271 qib_restart_rc(qp, qp->s_last_psn + 1, 0);
1272 if (list_empty(&qp->rspwait)) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001273 qp->r_flags |= RVT_R_RSP_SEND;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001274 rvt_get_qp(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07001275 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1276 }
1277}
1278
1279/**
1280 * qib_rc_rcv_resp - process an incoming RC response packet
1281 * @ibp: the port this packet came in on
1282 * @ohdr: the other headers for this packet
1283 * @data: the packet data
1284 * @tlen: the packet length
1285 * @qp: the QP for this packet
1286 * @opcode: the opcode for this packet
1287 * @psn: the packet sequence number for this packet
1288 * @hdrsize: the header length
1289 * @pmtu: the path MTU
1290 *
1291 * This is called from qib_rc_rcv() to process an incoming RC response
1292 * packet for the given QP.
1293 * Called at interrupt level.
1294 */
1295static void qib_rc_rcv_resp(struct qib_ibport *ibp,
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001296 struct ib_other_headers *ohdr,
Ralph Campbellf9315512010-05-23 21:44:54 -07001297 void *data, u32 tlen,
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001298 struct rvt_qp *qp,
Ralph Campbellf9315512010-05-23 21:44:54 -07001299 u32 opcode,
1300 u32 psn, u32 hdrsize, u32 pmtu,
1301 struct qib_ctxtdata *rcd)
1302{
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001303 struct rvt_swqe *wqe;
Mike Marciniszyndd04e432011-01-10 17:42:22 -08001304 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
Ralph Campbellf9315512010-05-23 21:44:54 -07001305 enum ib_wc_status status;
1306 unsigned long flags;
1307 int diff;
1308 u32 pad;
1309 u32 aeth;
1310 u64 val;
1311
Mike Marciniszyndd04e432011-01-10 17:42:22 -08001312 if (opcode != OP(RDMA_READ_RESPONSE_MIDDLE)) {
1313 /*
1314 * If ACK'd PSN on SDMA busy list try to make progress to
1315 * reclaim SDMA credits.
1316 */
1317 if ((qib_cmp24(psn, qp->s_sending_psn) >= 0) &&
1318 (qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) <= 0)) {
1319
1320 /*
1321 * If send tasklet not running attempt to progress
1322 * SDMA queue.
1323 */
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001324 if (!(qp->s_flags & RVT_S_BUSY)) {
Mike Marciniszyndd04e432011-01-10 17:42:22 -08001325 /* Acquire SDMA Lock */
1326 spin_lock_irqsave(&ppd->sdma_lock, flags);
1327 /* Invoke sdma make progress */
1328 qib_sdma_make_progress(ppd);
1329 /* Release SDMA Lock */
1330 spin_unlock_irqrestore(&ppd->sdma_lock, flags);
1331 }
1332 }
1333 }
1334
Ralph Campbellf9315512010-05-23 21:44:54 -07001335 spin_lock_irqsave(&qp->s_lock, flags);
Harish Chegondidb3ef0e2016-01-22 13:07:42 -08001336 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
Mike Marciniszyn414ed902011-02-10 14:11:28 +00001337 goto ack_done;
Ralph Campbellf9315512010-05-23 21:44:54 -07001338
Ralph Campbellf9315512010-05-23 21:44:54 -07001339 /* Ignore invalid responses. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001340 smp_read_barrier_depends(); /* see post_one_send */
Mike Marciniszyneb04ff02017-02-08 05:26:08 -08001341 if (qib_cmp24(psn, READ_ONCE(qp->s_next_psn)) >= 0)
Ralph Campbellf9315512010-05-23 21:44:54 -07001342 goto ack_done;
1343
1344 /* Ignore duplicate responses. */
1345 diff = qib_cmp24(psn, qp->s_last_psn);
1346 if (unlikely(diff <= 0)) {
1347 /* Update credits for "ghost" ACKs */
1348 if (diff == 0 && opcode == OP(ACKNOWLEDGE)) {
1349 aeth = be32_to_cpu(ohdr->u.aeth);
Don Hiatt832666c2017-02-08 05:28:25 -08001350 if ((aeth >> IB_AETH_NAK_SHIFT) == 0)
Brian Welty696513e2017-02-08 05:27:07 -08001351 rvt_get_credit(qp, aeth);
Ralph Campbellf9315512010-05-23 21:44:54 -07001352 }
1353 goto ack_done;
1354 }
1355
1356 /*
1357 * Skip everything other than the PSN we expect, if we are waiting
1358 * for a reply to a restarted RDMA read or atomic op.
1359 */
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001360 if (qp->r_flags & RVT_R_RDMAR_SEQ) {
Ralph Campbellf9315512010-05-23 21:44:54 -07001361 if (qib_cmp24(psn, qp->s_last_psn + 1) != 0)
1362 goto ack_done;
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001363 qp->r_flags &= ~RVT_R_RDMAR_SEQ;
Ralph Campbellf9315512010-05-23 21:44:54 -07001364 }
1365
1366 if (unlikely(qp->s_acked == qp->s_tail))
1367 goto ack_done;
Harish Chegondidb3ef0e2016-01-22 13:07:42 -08001368 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Ralph Campbellf9315512010-05-23 21:44:54 -07001369 status = IB_WC_SUCCESS;
1370
1371 switch (opcode) {
1372 case OP(ACKNOWLEDGE):
1373 case OP(ATOMIC_ACKNOWLEDGE):
1374 case OP(RDMA_READ_RESPONSE_FIRST):
1375 aeth = be32_to_cpu(ohdr->u.aeth);
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001376 if (opcode == OP(ATOMIC_ACKNOWLEDGE))
1377 val = ib_u64_get(&ohdr->u.at.atomic_ack_eth);
1378 else
Ralph Campbellf9315512010-05-23 21:44:54 -07001379 val = 0;
1380 if (!do_rc_ack(qp, aeth, psn, opcode, val, rcd) ||
1381 opcode != OP(RDMA_READ_RESPONSE_FIRST))
1382 goto ack_done;
1383 hdrsize += 4;
Harish Chegondidb3ef0e2016-01-22 13:07:42 -08001384 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Ralph Campbellf9315512010-05-23 21:44:54 -07001385 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1386 goto ack_op_err;
1387 /*
1388 * If this is a response to a resent RDMA read, we
1389 * have to be careful to copy the data to the right
1390 * location.
1391 */
1392 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1393 wqe, psn, pmtu);
1394 goto read_middle;
1395
1396 case OP(RDMA_READ_RESPONSE_MIDDLE):
1397 /* no AETH, no ACK */
1398 if (unlikely(qib_cmp24(psn, qp->s_last_psn + 1)))
1399 goto ack_seq_err;
1400 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1401 goto ack_op_err;
1402read_middle:
1403 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1404 goto ack_len_err;
1405 if (unlikely(pmtu >= qp->s_rdma_read_len))
1406 goto ack_len_err;
1407
1408 /*
1409 * We got a response so update the timeout.
1410 * 4.096 usec. * (1 << qp->timeout)
1411 */
Venkata Sandeep Dhanalakotab4238e72017-02-08 05:27:25 -08001412 rvt_mod_retry_timer(qp);
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001413 if (qp->s_flags & RVT_S_WAIT_ACK) {
1414 qp->s_flags &= ~RVT_S_WAIT_ACK;
Ralph Campbellf9315512010-05-23 21:44:54 -07001415 qib_schedule_send(qp);
1416 }
1417
1418 if (opcode == OP(RDMA_READ_RESPONSE_MIDDLE))
1419 qp->s_retry = qp->s_retry_cnt;
1420
1421 /*
1422 * Update the RDMA receive state but do the copy w/o
1423 * holding the locks and blocking interrupts.
1424 */
1425 qp->s_rdma_read_len -= pmtu;
1426 update_last_psn(qp, psn);
1427 spin_unlock_irqrestore(&qp->s_lock, flags);
1428 qib_copy_sge(&qp->s_rdma_read_sge, data, pmtu, 0);
1429 goto bail;
1430
1431 case OP(RDMA_READ_RESPONSE_ONLY):
1432 aeth = be32_to_cpu(ohdr->u.aeth);
1433 if (!do_rc_ack(qp, aeth, psn, opcode, 0, rcd))
1434 goto ack_done;
1435 /* Get the number of bytes the message was padded by. */
1436 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1437 /*
1438 * Check that the data size is >= 0 && <= pmtu.
1439 * Remember to account for the AETH header (4) and
1440 * ICRC (4).
1441 */
1442 if (unlikely(tlen < (hdrsize + pad + 8)))
1443 goto ack_len_err;
1444 /*
1445 * If this is a response to a resent RDMA read, we
1446 * have to be careful to copy the data to the right
1447 * location.
1448 */
Harish Chegondidb3ef0e2016-01-22 13:07:42 -08001449 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Ralph Campbellf9315512010-05-23 21:44:54 -07001450 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1451 wqe, psn, pmtu);
1452 goto read_last;
1453
1454 case OP(RDMA_READ_RESPONSE_LAST):
1455 /* ACKs READ req. */
1456 if (unlikely(qib_cmp24(psn, qp->s_last_psn + 1)))
1457 goto ack_seq_err;
1458 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1459 goto ack_op_err;
1460 /* Get the number of bytes the message was padded by. */
1461 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1462 /*
1463 * Check that the data size is >= 1 && <= pmtu.
1464 * Remember to account for the AETH header (4) and
1465 * ICRC (4).
1466 */
1467 if (unlikely(tlen <= (hdrsize + pad + 8)))
1468 goto ack_len_err;
1469read_last:
1470 tlen -= hdrsize + pad + 8;
1471 if (unlikely(tlen != qp->s_rdma_read_len))
1472 goto ack_len_err;
1473 aeth = be32_to_cpu(ohdr->u.aeth);
1474 qib_copy_sge(&qp->s_rdma_read_sge, data, tlen, 0);
1475 WARN_ON(qp->s_rdma_read_sge.num_sge);
1476 (void) do_rc_ack(qp, aeth, psn,
1477 OP(RDMA_READ_RESPONSE_LAST), 0, rcd);
1478 goto ack_done;
1479 }
1480
1481ack_op_err:
1482 status = IB_WC_LOC_QP_OP_ERR;
1483 goto ack_err;
1484
1485ack_seq_err:
1486 rdma_seq_err(qp, ibp, psn, rcd);
1487 goto ack_done;
1488
1489ack_len_err:
1490 status = IB_WC_LOC_LEN_ERR;
1491ack_err:
1492 if (qp->s_last == qp->s_acked) {
1493 qib_send_complete(qp, wqe, status);
Harish Chegondi70696ea2016-02-03 14:20:27 -08001494 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Ralph Campbellf9315512010-05-23 21:44:54 -07001495 }
1496ack_done:
1497 spin_unlock_irqrestore(&qp->s_lock, flags);
1498bail:
1499 return;
1500}
1501
1502/**
1503 * qib_rc_rcv_error - process an incoming duplicate or error RC packet
1504 * @ohdr: the other headers for this packet
1505 * @data: the packet data
1506 * @qp: the QP for this packet
1507 * @opcode: the opcode for this packet
1508 * @psn: the packet sequence number for this packet
1509 * @diff: the difference between the PSN and the expected PSN
1510 *
1511 * This is called from qib_rc_rcv() to process an unexpected
1512 * incoming RC packet for the given QP.
1513 * Called at interrupt level.
1514 * Return 1 if no more processing is needed; otherwise return 0 to
1515 * schedule a response to be sent.
1516 */
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001517static int qib_rc_rcv_error(struct ib_other_headers *ohdr,
Ralph Campbellf9315512010-05-23 21:44:54 -07001518 void *data,
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001519 struct rvt_qp *qp,
Ralph Campbellf9315512010-05-23 21:44:54 -07001520 u32 opcode,
1521 u32 psn,
1522 int diff,
1523 struct qib_ctxtdata *rcd)
1524{
1525 struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001526 struct rvt_ack_entry *e;
Ralph Campbellf9315512010-05-23 21:44:54 -07001527 unsigned long flags;
1528 u8 i, prev;
1529 int old_req;
1530
1531 if (diff > 0) {
1532 /*
1533 * Packet sequence error.
1534 * A NAK will ACK earlier sends and RDMA writes.
1535 * Don't queue the NAK if we already sent one.
1536 */
1537 if (!qp->r_nak_state) {
Harish Chegondif24a6d42016-01-22 12:56:02 -08001538 ibp->rvp.n_rc_seqnak++;
Ralph Campbellf9315512010-05-23 21:44:54 -07001539 qp->r_nak_state = IB_NAK_PSN_ERROR;
1540 /* Use the expected PSN. */
1541 qp->r_ack_psn = qp->r_psn;
1542 /*
1543 * Wait to send the sequence NAK until all packets
1544 * in the receive queue have been processed.
1545 * Otherwise, we end up propagating congestion.
1546 */
1547 if (list_empty(&qp->rspwait)) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001548 qp->r_flags |= RVT_R_RSP_NAK;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001549 rvt_get_qp(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07001550 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1551 }
1552 }
1553 goto done;
1554 }
1555
1556 /*
1557 * Handle a duplicate request. Don't re-execute SEND, RDMA
1558 * write or atomic op. Don't NAK errors, just silently drop
1559 * the duplicate request. Note that r_sge, r_len, and
1560 * r_rcv_len may be in use so don't modify them.
1561 *
1562 * We are supposed to ACK the earliest duplicate PSN but we
1563 * can coalesce an outstanding duplicate ACK. We have to
1564 * send the earliest so that RDMA reads can be restarted at
1565 * the requester's expected PSN.
1566 *
1567 * First, find where this duplicate PSN falls within the
1568 * ACKs previously sent.
1569 * old_req is true if there is an older response that is scheduled
1570 * to be sent before sending this one.
1571 */
1572 e = NULL;
1573 old_req = 1;
Harish Chegondif24a6d42016-01-22 12:56:02 -08001574 ibp->rvp.n_rc_dupreq++;
Ralph Campbellf9315512010-05-23 21:44:54 -07001575
1576 spin_lock_irqsave(&qp->s_lock, flags);
Ralph Campbellf9315512010-05-23 21:44:54 -07001577
1578 for (i = qp->r_head_ack_queue; ; i = prev) {
1579 if (i == qp->s_tail_ack_queue)
1580 old_req = 0;
1581 if (i)
1582 prev = i - 1;
1583 else
1584 prev = QIB_MAX_RDMA_ATOMIC;
1585 if (prev == qp->r_head_ack_queue) {
1586 e = NULL;
1587 break;
1588 }
1589 e = &qp->s_ack_queue[prev];
1590 if (!e->opcode) {
1591 e = NULL;
1592 break;
1593 }
1594 if (qib_cmp24(psn, e->psn) >= 0) {
1595 if (prev == qp->s_tail_ack_queue &&
1596 qib_cmp24(psn, e->lpsn) <= 0)
1597 old_req = 0;
1598 break;
1599 }
1600 }
1601 switch (opcode) {
1602 case OP(RDMA_READ_REQUEST): {
1603 struct ib_reth *reth;
1604 u32 offset;
1605 u32 len;
1606
1607 /*
1608 * If we didn't find the RDMA read request in the ack queue,
1609 * we can ignore this request.
1610 */
1611 if (!e || e->opcode != OP(RDMA_READ_REQUEST))
1612 goto unlock_done;
1613 /* RETH comes after BTH */
1614 reth = &ohdr->u.rc.reth;
1615 /*
1616 * Address range must be a subset of the original
1617 * request and start on pmtu boundaries.
1618 * We reuse the old ack_queue slot since the requester
1619 * should not back up and request an earlier PSN for the
1620 * same request.
1621 */
1622 offset = ((psn - e->psn) & QIB_PSN_MASK) *
Mike Marciniszyncc6ea132011-09-23 13:16:34 -04001623 qp->pmtu;
Ralph Campbellf9315512010-05-23 21:44:54 -07001624 len = be32_to_cpu(reth->length);
1625 if (unlikely(offset + len != e->rdma_sge.sge_length))
1626 goto unlock_done;
1627 if (e->rdma_sge.mr) {
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001628 rvt_put_mr(e->rdma_sge.mr);
Ralph Campbellf9315512010-05-23 21:44:54 -07001629 e->rdma_sge.mr = NULL;
1630 }
1631 if (len != 0) {
1632 u32 rkey = be32_to_cpu(reth->rkey);
1633 u64 vaddr = be64_to_cpu(reth->vaddr);
1634 int ok;
1635
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001636 ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr, rkey,
Ralph Campbellf9315512010-05-23 21:44:54 -07001637 IB_ACCESS_REMOTE_READ);
1638 if (unlikely(!ok))
1639 goto unlock_done;
1640 } else {
1641 e->rdma_sge.vaddr = NULL;
1642 e->rdma_sge.length = 0;
1643 e->rdma_sge.sge_length = 0;
1644 }
1645 e->psn = psn;
1646 if (old_req)
1647 goto unlock_done;
1648 qp->s_tail_ack_queue = prev;
1649 break;
1650 }
1651
1652 case OP(COMPARE_SWAP):
1653 case OP(FETCH_ADD): {
1654 /*
1655 * If we didn't find the atomic request in the ack queue
1656 * or the send tasklet is already backed up to send an
1657 * earlier entry, we can ignore this request.
1658 */
1659 if (!e || e->opcode != (u8) opcode || old_req)
1660 goto unlock_done;
1661 qp->s_tail_ack_queue = prev;
1662 break;
1663 }
1664
1665 default:
1666 /*
1667 * Ignore this operation if it doesn't request an ACK
1668 * or an earlier RDMA read or atomic is going to be resent.
1669 */
1670 if (!(psn & IB_BTH_REQ_ACK) || old_req)
1671 goto unlock_done;
1672 /*
1673 * Resend the most recent ACK if this request is
1674 * after all the previous RDMA reads and atomics.
1675 */
1676 if (i == qp->r_head_ack_queue) {
1677 spin_unlock_irqrestore(&qp->s_lock, flags);
1678 qp->r_nak_state = 0;
1679 qp->r_ack_psn = qp->r_psn - 1;
1680 goto send_ack;
1681 }
1682 /*
1683 * Try to send a simple ACK to work around a Mellanox bug
1684 * which doesn't accept a RDMA read response or atomic
1685 * response as an ACK for earlier SENDs or RDMA writes.
1686 */
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001687 if (!(qp->s_flags & RVT_S_RESP_PENDING)) {
Ralph Campbellf9315512010-05-23 21:44:54 -07001688 spin_unlock_irqrestore(&qp->s_lock, flags);
1689 qp->r_nak_state = 0;
1690 qp->r_ack_psn = qp->s_ack_queue[i].psn - 1;
1691 goto send_ack;
1692 }
1693 /*
1694 * Resend the RDMA read or atomic op which
1695 * ACKs this duplicate request.
1696 */
1697 qp->s_tail_ack_queue = i;
1698 break;
1699 }
1700 qp->s_ack_state = OP(ACKNOWLEDGE);
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001701 qp->s_flags |= RVT_S_RESP_PENDING;
Ralph Campbellf9315512010-05-23 21:44:54 -07001702 qp->r_nak_state = 0;
1703 qib_schedule_send(qp);
1704
1705unlock_done:
1706 spin_unlock_irqrestore(&qp->s_lock, flags);
1707done:
1708 return 1;
1709
1710send_ack:
1711 return 0;
1712}
1713
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001714static inline void qib_update_ack_queue(struct rvt_qp *qp, unsigned n)
Ralph Campbellf9315512010-05-23 21:44:54 -07001715{
1716 unsigned next;
1717
1718 next = n + 1;
1719 if (next > QIB_MAX_RDMA_ATOMIC)
1720 next = 0;
1721 qp->s_tail_ack_queue = next;
1722 qp->s_ack_state = OP(ACKNOWLEDGE);
1723}
1724
1725/**
1726 * qib_rc_rcv - process an incoming RC packet
1727 * @rcd: the context pointer
1728 * @hdr: the header of this packet
1729 * @has_grh: true if the header has a GRH
1730 * @data: the packet data
1731 * @tlen: the packet length
1732 * @qp: the QP for this packet
1733 *
1734 * This is called from qib_qp_rcv() to process an incoming RC packet
1735 * for the given QP.
1736 * Called at interrupt level.
1737 */
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001738void qib_rc_rcv(struct qib_ctxtdata *rcd, struct ib_header *hdr,
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001739 int has_grh, void *data, u32 tlen, struct rvt_qp *qp)
Ralph Campbellf9315512010-05-23 21:44:54 -07001740{
1741 struct qib_ibport *ibp = &rcd->ppd->ibport_data;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001742 struct ib_other_headers *ohdr;
Ralph Campbellf9315512010-05-23 21:44:54 -07001743 u32 opcode;
1744 u32 hdrsize;
1745 u32 psn;
1746 u32 pad;
1747 struct ib_wc wc;
Mike Marciniszyncc6ea132011-09-23 13:16:34 -04001748 u32 pmtu = qp->pmtu;
Ralph Campbellf9315512010-05-23 21:44:54 -07001749 int diff;
1750 struct ib_reth *reth;
1751 unsigned long flags;
1752 int ret;
1753
1754 /* Check for GRH */
1755 if (!has_grh) {
1756 ohdr = &hdr->u.oth;
1757 hdrsize = 8 + 12; /* LRH + BTH */
1758 } else {
1759 ohdr = &hdr->u.l.oth;
1760 hdrsize = 8 + 40 + 12; /* LRH + GRH + BTH */
1761 }
1762
1763 opcode = be32_to_cpu(ohdr->bth[0]);
Ralph Campbellf9315512010-05-23 21:44:54 -07001764 if (qib_ruc_check_hdr(ibp, hdr, has_grh, qp, opcode))
Mike Marciniszyn9fd54732011-09-23 13:17:00 -04001765 return;
Ralph Campbellf9315512010-05-23 21:44:54 -07001766
1767 psn = be32_to_cpu(ohdr->bth[2]);
1768 opcode >>= 24;
1769
Ralph Campbellf9315512010-05-23 21:44:54 -07001770 /*
1771 * Process responses (ACKs) before anything else. Note that the
1772 * packet sequence number will be for something in the send work
1773 * queue rather than the expected receive packet sequence number.
1774 * In other words, this QP is the requester.
1775 */
1776 if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
1777 opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
1778 qib_rc_rcv_resp(ibp, ohdr, data, tlen, qp, opcode, psn,
1779 hdrsize, pmtu, rcd);
Ralph Campbella5210c12010-08-02 22:39:30 +00001780 return;
Ralph Campbellf9315512010-05-23 21:44:54 -07001781 }
1782
1783 /* Compute 24 bits worth of difference. */
1784 diff = qib_cmp24(psn, qp->r_psn);
1785 if (unlikely(diff)) {
1786 if (qib_rc_rcv_error(ohdr, data, qp, opcode, psn, diff, rcd))
Ralph Campbella5210c12010-08-02 22:39:30 +00001787 return;
Ralph Campbellf9315512010-05-23 21:44:54 -07001788 goto send_ack;
1789 }
1790
1791 /* Check for opcode sequence errors. */
1792 switch (qp->r_state) {
1793 case OP(SEND_FIRST):
1794 case OP(SEND_MIDDLE):
1795 if (opcode == OP(SEND_MIDDLE) ||
1796 opcode == OP(SEND_LAST) ||
1797 opcode == OP(SEND_LAST_WITH_IMMEDIATE))
1798 break;
1799 goto nack_inv;
1800
1801 case OP(RDMA_WRITE_FIRST):
1802 case OP(RDMA_WRITE_MIDDLE):
1803 if (opcode == OP(RDMA_WRITE_MIDDLE) ||
1804 opcode == OP(RDMA_WRITE_LAST) ||
1805 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
1806 break;
1807 goto nack_inv;
1808
1809 default:
1810 if (opcode == OP(SEND_MIDDLE) ||
1811 opcode == OP(SEND_LAST) ||
1812 opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
1813 opcode == OP(RDMA_WRITE_MIDDLE) ||
1814 opcode == OP(RDMA_WRITE_LAST) ||
1815 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
1816 goto nack_inv;
1817 /*
1818 * Note that it is up to the requester to not send a new
1819 * RDMA read or atomic operation before receiving an ACK
1820 * for the previous operation.
1821 */
1822 break;
1823 }
1824
Brian Weltybeb5a042017-02-08 05:27:01 -08001825 if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
1826 rvt_comm_est(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07001827
1828 /* OK, process the packet. */
1829 switch (opcode) {
1830 case OP(SEND_FIRST):
1831 ret = qib_get_rwqe(qp, 0);
1832 if (ret < 0)
1833 goto nack_op_err;
1834 if (!ret)
1835 goto rnr_nak;
1836 qp->r_rcv_len = 0;
1837 /* FALLTHROUGH */
1838 case OP(SEND_MIDDLE):
1839 case OP(RDMA_WRITE_MIDDLE):
1840send_middle:
1841 /* Check for invalid length PMTU or posted rwqe len. */
1842 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1843 goto nack_inv;
1844 qp->r_rcv_len += pmtu;
1845 if (unlikely(qp->r_rcv_len > qp->r_len))
1846 goto nack_inv;
1847 qib_copy_sge(&qp->r_sge, data, pmtu, 1);
1848 break;
1849
1850 case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
1851 /* consume RWQE */
1852 ret = qib_get_rwqe(qp, 1);
1853 if (ret < 0)
1854 goto nack_op_err;
1855 if (!ret)
1856 goto rnr_nak;
1857 goto send_last_imm;
1858
1859 case OP(SEND_ONLY):
1860 case OP(SEND_ONLY_WITH_IMMEDIATE):
1861 ret = qib_get_rwqe(qp, 0);
1862 if (ret < 0)
1863 goto nack_op_err;
1864 if (!ret)
1865 goto rnr_nak;
1866 qp->r_rcv_len = 0;
1867 if (opcode == OP(SEND_ONLY))
Mike Marciniszyn2fc109c2011-09-23 13:16:29 -04001868 goto no_immediate_data;
1869 /* FALLTHROUGH for SEND_ONLY_WITH_IMMEDIATE */
Ralph Campbellf9315512010-05-23 21:44:54 -07001870 case OP(SEND_LAST_WITH_IMMEDIATE):
1871send_last_imm:
1872 wc.ex.imm_data = ohdr->u.imm_data;
1873 hdrsize += 4;
1874 wc.wc_flags = IB_WC_WITH_IMM;
Mike Marciniszyn2fc109c2011-09-23 13:16:29 -04001875 goto send_last;
Ralph Campbellf9315512010-05-23 21:44:54 -07001876 case OP(SEND_LAST):
1877 case OP(RDMA_WRITE_LAST):
Mike Marciniszyn2fc109c2011-09-23 13:16:29 -04001878no_immediate_data:
1879 wc.wc_flags = 0;
1880 wc.ex.imm_data = 0;
Ralph Campbellf9315512010-05-23 21:44:54 -07001881send_last:
1882 /* Get the number of bytes the message was padded by. */
1883 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1884 /* Check for invalid length. */
1885 /* XXX LAST len should be >= 1 */
1886 if (unlikely(tlen < (hdrsize + pad + 4)))
1887 goto nack_inv;
1888 /* Don't count the CRC. */
1889 tlen -= (hdrsize + pad + 4);
1890 wc.byte_len = tlen + qp->r_rcv_len;
1891 if (unlikely(wc.byte_len > qp->r_len))
1892 goto nack_inv;
1893 qib_copy_sge(&qp->r_sge, data, tlen, 1);
Harish Chegondi70696ea2016-02-03 14:20:27 -08001894 rvt_put_ss(&qp->r_sge);
Ralph Campbellf9315512010-05-23 21:44:54 -07001895 qp->r_msn++;
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001896 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
Ralph Campbellf9315512010-05-23 21:44:54 -07001897 break;
1898 wc.wr_id = qp->r_wr_id;
1899 wc.status = IB_WC_SUCCESS;
1900 if (opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE) ||
1901 opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
1902 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
1903 else
1904 wc.opcode = IB_WC_RECV;
1905 wc.qp = &qp->ibqp;
1906 wc.src_qp = qp->remote_qpn;
1907 wc.slid = qp->remote_ah_attr.dlid;
1908 wc.sl = qp->remote_ah_attr.sl;
Mike Marciniszyn2fc109c2011-09-23 13:16:29 -04001909 /* zero fields that are N/A */
1910 wc.vendor_err = 0;
1911 wc.pkey_index = 0;
1912 wc.dlid_path_bits = 0;
1913 wc.port_num = 0;
Ralph Campbellf9315512010-05-23 21:44:54 -07001914 /* Signal completion event if the solicited bit is set. */
Harish Chegondi4bb88e52016-01-22 13:07:36 -08001915 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
Ralph Campbellf9315512010-05-23 21:44:54 -07001916 (ohdr->bth[0] &
1917 cpu_to_be32(IB_BTH_SOLICITED)) != 0);
1918 break;
1919
1920 case OP(RDMA_WRITE_FIRST):
1921 case OP(RDMA_WRITE_ONLY):
1922 case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
1923 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
1924 goto nack_inv;
1925 /* consume RWQE */
1926 reth = &ohdr->u.rc.reth;
1927 hdrsize += sizeof(*reth);
1928 qp->r_len = be32_to_cpu(reth->length);
1929 qp->r_rcv_len = 0;
1930 qp->r_sge.sg_list = NULL;
1931 if (qp->r_len != 0) {
1932 u32 rkey = be32_to_cpu(reth->rkey);
1933 u64 vaddr = be64_to_cpu(reth->vaddr);
1934 int ok;
1935
1936 /* Check rkey & NAK */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001937 ok = rvt_rkey_ok(qp, &qp->r_sge.sge, qp->r_len, vaddr,
Ralph Campbellf9315512010-05-23 21:44:54 -07001938 rkey, IB_ACCESS_REMOTE_WRITE);
1939 if (unlikely(!ok))
1940 goto nack_acc;
1941 qp->r_sge.num_sge = 1;
1942 } else {
1943 qp->r_sge.num_sge = 0;
1944 qp->r_sge.sge.mr = NULL;
1945 qp->r_sge.sge.vaddr = NULL;
1946 qp->r_sge.sge.length = 0;
1947 qp->r_sge.sge.sge_length = 0;
1948 }
1949 if (opcode == OP(RDMA_WRITE_FIRST))
1950 goto send_middle;
1951 else if (opcode == OP(RDMA_WRITE_ONLY))
Mike Marciniszyn2fc109c2011-09-23 13:16:29 -04001952 goto no_immediate_data;
Ralph Campbellf9315512010-05-23 21:44:54 -07001953 ret = qib_get_rwqe(qp, 1);
1954 if (ret < 0)
1955 goto nack_op_err;
1956 if (!ret)
1957 goto rnr_nak;
Jason Gunthorpe5715f5d2010-10-22 22:00:48 +00001958 wc.ex.imm_data = ohdr->u.rc.imm_data;
1959 hdrsize += 4;
1960 wc.wc_flags = IB_WC_WITH_IMM;
1961 goto send_last;
Ralph Campbellf9315512010-05-23 21:44:54 -07001962
1963 case OP(RDMA_READ_REQUEST): {
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001964 struct rvt_ack_entry *e;
Ralph Campbellf9315512010-05-23 21:44:54 -07001965 u32 len;
1966 u8 next;
1967
1968 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
1969 goto nack_inv;
1970 next = qp->r_head_ack_queue + 1;
1971 /* s_ack_queue is size QIB_MAX_RDMA_ATOMIC+1 so use > not >= */
1972 if (next > QIB_MAX_RDMA_ATOMIC)
1973 next = 0;
1974 spin_lock_irqsave(&qp->s_lock, flags);
Ralph Campbellf9315512010-05-23 21:44:54 -07001975 if (unlikely(next == qp->s_tail_ack_queue)) {
1976 if (!qp->s_ack_queue[next].sent)
1977 goto nack_inv_unlck;
1978 qib_update_ack_queue(qp, next);
1979 }
1980 e = &qp->s_ack_queue[qp->r_head_ack_queue];
1981 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001982 rvt_put_mr(e->rdma_sge.mr);
Ralph Campbellf9315512010-05-23 21:44:54 -07001983 e->rdma_sge.mr = NULL;
1984 }
1985 reth = &ohdr->u.rc.reth;
1986 len = be32_to_cpu(reth->length);
1987 if (len) {
1988 u32 rkey = be32_to_cpu(reth->rkey);
1989 u64 vaddr = be64_to_cpu(reth->vaddr);
1990 int ok;
1991
1992 /* Check rkey & NAK */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001993 ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr,
Ralph Campbellf9315512010-05-23 21:44:54 -07001994 rkey, IB_ACCESS_REMOTE_READ);
1995 if (unlikely(!ok))
1996 goto nack_acc_unlck;
1997 /*
1998 * Update the next expected PSN. We add 1 later
1999 * below, so only add the remainder here.
2000 */
Mike Marciniszyn5dc80602016-12-07 19:34:37 -08002001 qp->r_psn += rvt_div_mtu(qp, len - 1);
Ralph Campbellf9315512010-05-23 21:44:54 -07002002 } else {
2003 e->rdma_sge.mr = NULL;
2004 e->rdma_sge.vaddr = NULL;
2005 e->rdma_sge.length = 0;
2006 e->rdma_sge.sge_length = 0;
2007 }
2008 e->opcode = opcode;
2009 e->sent = 0;
2010 e->psn = psn;
2011 e->lpsn = qp->r_psn;
2012 /*
2013 * We need to increment the MSN here instead of when we
2014 * finish sending the result since a duplicate request would
2015 * increment it more than once.
2016 */
2017 qp->r_msn++;
2018 qp->r_psn++;
2019 qp->r_state = opcode;
2020 qp->r_nak_state = 0;
2021 qp->r_head_ack_queue = next;
2022
2023 /* Schedule the send tasklet. */
Harish Chegondi01ba79d2016-01-22 12:56:46 -08002024 qp->s_flags |= RVT_S_RESP_PENDING;
Ralph Campbellf9315512010-05-23 21:44:54 -07002025 qib_schedule_send(qp);
2026
Ralph Campbella5210c12010-08-02 22:39:30 +00002027 goto sunlock;
Ralph Campbellf9315512010-05-23 21:44:54 -07002028 }
2029
2030 case OP(COMPARE_SWAP):
2031 case OP(FETCH_ADD): {
2032 struct ib_atomic_eth *ateth;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08002033 struct rvt_ack_entry *e;
Ralph Campbellf9315512010-05-23 21:44:54 -07002034 u64 vaddr;
2035 atomic64_t *maddr;
2036 u64 sdata;
2037 u32 rkey;
2038 u8 next;
2039
2040 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
2041 goto nack_inv;
2042 next = qp->r_head_ack_queue + 1;
2043 if (next > QIB_MAX_RDMA_ATOMIC)
2044 next = 0;
2045 spin_lock_irqsave(&qp->s_lock, flags);
Ralph Campbellf9315512010-05-23 21:44:54 -07002046 if (unlikely(next == qp->s_tail_ack_queue)) {
2047 if (!qp->s_ack_queue[next].sent)
2048 goto nack_inv_unlck;
2049 qib_update_ack_queue(qp, next);
2050 }
2051 e = &qp->s_ack_queue[qp->r_head_ack_queue];
2052 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08002053 rvt_put_mr(e->rdma_sge.mr);
Ralph Campbellf9315512010-05-23 21:44:54 -07002054 e->rdma_sge.mr = NULL;
2055 }
2056 ateth = &ohdr->u.atomic_eth;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002057 vaddr = get_ib_ateth_vaddr(ateth);
Ralph Campbellf9315512010-05-23 21:44:54 -07002058 if (unlikely(vaddr & (sizeof(u64) - 1)))
2059 goto nack_inv_unlck;
2060 rkey = be32_to_cpu(ateth->rkey);
2061 /* Check rkey & NAK */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08002062 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
Ralph Campbellf9315512010-05-23 21:44:54 -07002063 vaddr, rkey,
2064 IB_ACCESS_REMOTE_ATOMIC)))
2065 goto nack_acc_unlck;
2066 /* Perform atomic OP and save result. */
2067 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002068 sdata = get_ib_ateth_swap(ateth);
Ralph Campbellf9315512010-05-23 21:44:54 -07002069 e->atomic_data = (opcode == OP(FETCH_ADD)) ?
2070 (u64) atomic64_add_return(sdata, maddr) - sdata :
2071 (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002072 get_ib_ateth_compare(ateth),
Ralph Campbellf9315512010-05-23 21:44:54 -07002073 sdata);
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08002074 rvt_put_mr(qp->r_sge.sge.mr);
Ralph Campbellf9315512010-05-23 21:44:54 -07002075 qp->r_sge.num_sge = 0;
2076 e->opcode = opcode;
2077 e->sent = 0;
2078 e->psn = psn;
2079 e->lpsn = psn;
2080 qp->r_msn++;
2081 qp->r_psn++;
2082 qp->r_state = opcode;
2083 qp->r_nak_state = 0;
2084 qp->r_head_ack_queue = next;
2085
2086 /* Schedule the send tasklet. */
Harish Chegondi01ba79d2016-01-22 12:56:46 -08002087 qp->s_flags |= RVT_S_RESP_PENDING;
Ralph Campbellf9315512010-05-23 21:44:54 -07002088 qib_schedule_send(qp);
2089
Ralph Campbella5210c12010-08-02 22:39:30 +00002090 goto sunlock;
Ralph Campbellf9315512010-05-23 21:44:54 -07002091 }
2092
2093 default:
2094 /* NAK unknown opcodes. */
2095 goto nack_inv;
2096 }
2097 qp->r_psn++;
2098 qp->r_state = opcode;
2099 qp->r_ack_psn = psn;
2100 qp->r_nak_state = 0;
2101 /* Send an ACK if requested or required. */
2102 if (psn & (1 << 31))
2103 goto send_ack;
Ralph Campbella5210c12010-08-02 22:39:30 +00002104 return;
Ralph Campbellf9315512010-05-23 21:44:54 -07002105
2106rnr_nak:
2107 qp->r_nak_state = IB_RNR_NAK | qp->r_min_rnr_timer;
2108 qp->r_ack_psn = qp->r_psn;
2109 /* Queue RNR NAK for later */
2110 if (list_empty(&qp->rspwait)) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -08002111 qp->r_flags |= RVT_R_RSP_NAK;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07002112 rvt_get_qp(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07002113 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
2114 }
Ralph Campbella5210c12010-08-02 22:39:30 +00002115 return;
Ralph Campbellf9315512010-05-23 21:44:54 -07002116
2117nack_op_err:
Brian Weltybeb5a042017-02-08 05:27:01 -08002118 rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
Ralph Campbellf9315512010-05-23 21:44:54 -07002119 qp->r_nak_state = IB_NAK_REMOTE_OPERATIONAL_ERROR;
2120 qp->r_ack_psn = qp->r_psn;
2121 /* Queue NAK for later */
2122 if (list_empty(&qp->rspwait)) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -08002123 qp->r_flags |= RVT_R_RSP_NAK;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07002124 rvt_get_qp(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07002125 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
2126 }
Ralph Campbella5210c12010-08-02 22:39:30 +00002127 return;
Ralph Campbellf9315512010-05-23 21:44:54 -07002128
2129nack_inv_unlck:
2130 spin_unlock_irqrestore(&qp->s_lock, flags);
2131nack_inv:
Brian Weltybeb5a042017-02-08 05:27:01 -08002132 rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
Ralph Campbellf9315512010-05-23 21:44:54 -07002133 qp->r_nak_state = IB_NAK_INVALID_REQUEST;
2134 qp->r_ack_psn = qp->r_psn;
2135 /* Queue NAK for later */
2136 if (list_empty(&qp->rspwait)) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -08002137 qp->r_flags |= RVT_R_RSP_NAK;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07002138 rvt_get_qp(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07002139 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
2140 }
Ralph Campbella5210c12010-08-02 22:39:30 +00002141 return;
Ralph Campbellf9315512010-05-23 21:44:54 -07002142
2143nack_acc_unlck:
2144 spin_unlock_irqrestore(&qp->s_lock, flags);
2145nack_acc:
Brian Weltybeb5a042017-02-08 05:27:01 -08002146 rvt_rc_error(qp, IB_WC_LOC_PROT_ERR);
Ralph Campbellf9315512010-05-23 21:44:54 -07002147 qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
2148 qp->r_ack_psn = qp->r_psn;
2149send_ack:
2150 qib_send_rc_ack(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07002151 return;
2152
2153sunlock:
2154 spin_unlock_irqrestore(&qp->s_lock, flags);
2155}