blob: 5b0d01be7e50c02bd1e1476a9138852bb1bfd18c [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
41static void rc_timeout(unsigned long arg);
42
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080043static u32 restart_sge(struct rvt_sge_state *ss, struct rvt_swqe *wqe,
Ralph Campbellf9315512010-05-23 21:44:54 -070044 u32 psn, u32 pmtu)
45{
46 u32 len;
47
48 len = ((psn - wqe->psn) & QIB_PSN_MASK) * pmtu;
49 ss->sge = wqe->sg_list[0];
50 ss->sg_list = wqe->sg_list + 1;
51 ss->num_sge = wqe->wr.num_sge;
52 ss->total_len = wqe->length;
53 qib_skip_sge(ss, len, 0);
54 return wqe->length - len;
55}
56
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080057static void start_timer(struct rvt_qp *qp)
Ralph Campbellf9315512010-05-23 21:44:54 -070058{
Harish Chegondi01ba79d2016-01-22 12:56:46 -080059 qp->s_flags |= RVT_S_TIMER;
Ralph Campbellf9315512010-05-23 21:44:54 -070060 qp->s_timer.function = rc_timeout;
61 /* 4.096 usec. * (1 << qp->timeout) */
Mike Marciniszynd0f2faf2011-09-23 13:16:49 -040062 qp->s_timer.expires = jiffies + qp->timeout_jiffies;
Ralph Campbellf9315512010-05-23 21:44:54 -070063 add_timer(&qp->s_timer);
64}
65
66/**
67 * qib_make_rc_ack - construct a response packet (ACK, NAK, or RDMA read)
68 * @dev: the device for this QP
69 * @qp: a pointer to the QP
70 * @ohdr: a pointer to the IB header being constructed
71 * @pmtu: the path MTU
72 *
73 * Return 1 if constructed; otherwise, return 0.
74 * Note that we are in the responder's side of the QP context.
75 * Note the QP s_lock must be held.
76 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080077static int qib_make_rc_ack(struct qib_ibdev *dev, struct rvt_qp *qp,
Mike Marciniszyn261a4352016-09-06 04:35:05 -070078 struct ib_other_headers *ohdr, u32 pmtu)
Ralph Campbellf9315512010-05-23 21:44:54 -070079{
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080080 struct rvt_ack_entry *e;
Ralph Campbellf9315512010-05-23 21:44:54 -070081 u32 hwords;
82 u32 len;
83 u32 bth0;
84 u32 bth2;
85
86 /* Don't send an ACK if we aren't supposed to. */
Harish Chegondidb3ef0e2016-01-22 13:07:42 -080087 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
Ralph Campbellf9315512010-05-23 21:44:54 -070088 goto bail;
89
90 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
91 hwords = 5;
92
93 switch (qp->s_ack_state) {
94 case OP(RDMA_READ_RESPONSE_LAST):
95 case OP(RDMA_READ_RESPONSE_ONLY):
96 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
97 if (e->rdma_sge.mr) {
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080098 rvt_put_mr(e->rdma_sge.mr);
Ralph Campbellf9315512010-05-23 21:44:54 -070099 e->rdma_sge.mr = NULL;
100 }
101 /* FALLTHROUGH */
102 case OP(ATOMIC_ACKNOWLEDGE):
103 /*
104 * We can increment the tail pointer now that the last
105 * response has been sent instead of only being
106 * constructed.
107 */
108 if (++qp->s_tail_ack_queue > QIB_MAX_RDMA_ATOMIC)
109 qp->s_tail_ack_queue = 0;
110 /* FALLTHROUGH */
111 case OP(SEND_ONLY):
112 case OP(ACKNOWLEDGE):
113 /* Check for no next entry in the queue. */
114 if (qp->r_head_ack_queue == qp->s_tail_ack_queue) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800115 if (qp->s_flags & RVT_S_ACK_PENDING)
Ralph Campbellf9315512010-05-23 21:44:54 -0700116 goto normal;
117 goto bail;
118 }
119
120 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
121 if (e->opcode == OP(RDMA_READ_REQUEST)) {
122 /*
123 * If a RDMA read response is being resent and
124 * we haven't seen the duplicate request yet,
125 * then stop sending the remaining responses the
126 * responder has seen until the requester resends it.
127 */
128 len = e->rdma_sge.sge_length;
129 if (len && !e->rdma_sge.mr) {
130 qp->s_tail_ack_queue = qp->r_head_ack_queue;
131 goto bail;
132 }
133 /* Copy SGE state in case we need to resend */
134 qp->s_rdma_mr = e->rdma_sge.mr;
135 if (qp->s_rdma_mr)
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800136 rvt_get_mr(qp->s_rdma_mr);
Ralph Campbellf9315512010-05-23 21:44:54 -0700137 qp->s_ack_rdma_sge.sge = e->rdma_sge;
138 qp->s_ack_rdma_sge.num_sge = 1;
139 qp->s_cur_sge = &qp->s_ack_rdma_sge;
140 if (len > pmtu) {
141 len = pmtu;
142 qp->s_ack_state = OP(RDMA_READ_RESPONSE_FIRST);
143 } else {
144 qp->s_ack_state = OP(RDMA_READ_RESPONSE_ONLY);
145 e->sent = 1;
146 }
147 ohdr->u.aeth = qib_compute_aeth(qp);
148 hwords++;
149 qp->s_ack_rdma_psn = e->psn;
150 bth2 = qp->s_ack_rdma_psn++ & QIB_PSN_MASK;
151 } else {
152 /* COMPARE_SWAP or FETCH_ADD */
153 qp->s_cur_sge = NULL;
154 len = 0;
155 qp->s_ack_state = OP(ATOMIC_ACKNOWLEDGE);
156 ohdr->u.at.aeth = qib_compute_aeth(qp);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700157 ib_u64_put(e->atomic_data, &ohdr->u.at.atomic_ack_eth);
Ralph Campbellf9315512010-05-23 21:44:54 -0700158 hwords += sizeof(ohdr->u.at) / sizeof(u32);
159 bth2 = e->psn & QIB_PSN_MASK;
160 e->sent = 1;
161 }
162 bth0 = qp->s_ack_state << 24;
163 break;
164
165 case OP(RDMA_READ_RESPONSE_FIRST):
166 qp->s_ack_state = OP(RDMA_READ_RESPONSE_MIDDLE);
167 /* FALLTHROUGH */
168 case OP(RDMA_READ_RESPONSE_MIDDLE):
169 qp->s_cur_sge = &qp->s_ack_rdma_sge;
170 qp->s_rdma_mr = qp->s_ack_rdma_sge.sge.mr;
171 if (qp->s_rdma_mr)
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800172 rvt_get_mr(qp->s_rdma_mr);
Ralph Campbellf9315512010-05-23 21:44:54 -0700173 len = qp->s_ack_rdma_sge.sge.sge_length;
174 if (len > pmtu)
175 len = pmtu;
176 else {
177 ohdr->u.aeth = qib_compute_aeth(qp);
178 hwords++;
179 qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
180 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
181 e->sent = 1;
182 }
183 bth0 = qp->s_ack_state << 24;
184 bth2 = qp->s_ack_rdma_psn++ & QIB_PSN_MASK;
185 break;
186
187 default:
188normal:
189 /*
190 * Send a regular ACK.
191 * Set the s_ack_state so we wait until after sending
192 * the ACK before setting s_ack_state to ACKNOWLEDGE
193 * (see above).
194 */
195 qp->s_ack_state = OP(SEND_ONLY);
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800196 qp->s_flags &= ~RVT_S_ACK_PENDING;
Ralph Campbellf9315512010-05-23 21:44:54 -0700197 qp->s_cur_sge = NULL;
198 if (qp->s_nak_state)
199 ohdr->u.aeth =
200 cpu_to_be32((qp->r_msn & QIB_MSN_MASK) |
201 (qp->s_nak_state <<
202 QIB_AETH_CREDIT_SHIFT));
203 else
204 ohdr->u.aeth = qib_compute_aeth(qp);
205 hwords++;
206 len = 0;
207 bth0 = OP(ACKNOWLEDGE) << 24;
208 bth2 = qp->s_ack_psn & QIB_PSN_MASK;
209 }
210 qp->s_rdma_ack_cnt++;
211 qp->s_hdrwords = hwords;
212 qp->s_cur_size = len;
213 qib_make_ruc_header(qp, ohdr, bth0, bth2);
214 return 1;
215
216bail:
217 qp->s_ack_state = OP(ACKNOWLEDGE);
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800218 qp->s_flags &= ~(RVT_S_RESP_PENDING | RVT_S_ACK_PENDING);
Ralph Campbellf9315512010-05-23 21:44:54 -0700219 return 0;
220}
221
222/**
223 * qib_make_rc_req - construct a request packet (SEND, RDMA r/w, ATOMIC)
224 * @qp: a pointer to the QP
225 *
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800226 * Assumes the s_lock is held.
227 *
Ralph Campbellf9315512010-05-23 21:44:54 -0700228 * Return 1 if constructed; otherwise, return 0.
229 */
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700230int qib_make_rc_req(struct rvt_qp *qp, unsigned long *flags)
Ralph Campbellf9315512010-05-23 21:44:54 -0700231{
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800232 struct qib_qp_priv *priv = qp->priv;
Ralph Campbellf9315512010-05-23 21:44:54 -0700233 struct qib_ibdev *dev = to_idev(qp->ibqp.device);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700234 struct ib_other_headers *ohdr;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800235 struct rvt_sge_state *ss;
236 struct rvt_swqe *wqe;
Ralph Campbellf9315512010-05-23 21:44:54 -0700237 u32 hwords;
238 u32 len;
239 u32 bth0;
240 u32 bth2;
Mike Marciniszyncc6ea132011-09-23 13:16:34 -0400241 u32 pmtu = qp->pmtu;
Ralph Campbellf9315512010-05-23 21:44:54 -0700242 char newreq;
Ralph Campbellf9315512010-05-23 21:44:54 -0700243 int ret = 0;
244 int delta;
245
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800246 ohdr = &priv->s_hdr->u.oth;
Ralph Campbellf9315512010-05-23 21:44:54 -0700247 if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800248 ohdr = &priv->s_hdr->u.l.oth;
Ralph Campbellf9315512010-05-23 21:44:54 -0700249
Ralph Campbellf9315512010-05-23 21:44:54 -0700250 /* Sending responses has higher priority over sending requests. */
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800251 if ((qp->s_flags & RVT_S_RESP_PENDING) &&
Ralph Campbellf9315512010-05-23 21:44:54 -0700252 qib_make_rc_ack(dev, qp, ohdr, pmtu))
253 goto done;
254
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800255 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_SEND_OK)) {
256 if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
Ralph Campbellf9315512010-05-23 21:44:54 -0700257 goto bail;
258 /* We are in the error state, flush the work request. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800259 smp_read_barrier_depends(); /* see post_one_send() */
Mike Marciniszyneb04ff02017-02-08 05:26:08 -0800260 if (qp->s_last == READ_ONCE(qp->s_head))
Ralph Campbellf9315512010-05-23 21:44:54 -0700261 goto bail;
262 /* If DMAs are in progress, we can't flush immediately. */
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800263 if (atomic_read(&priv->s_dma_busy)) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800264 qp->s_flags |= RVT_S_WAIT_DMA;
Ralph Campbellf9315512010-05-23 21:44:54 -0700265 goto bail;
266 }
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800267 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
Mike Marciniszyn30ab7e22011-11-04 08:26:52 -0400268 qib_send_complete(qp, wqe, qp->s_last != qp->s_acked ?
269 IB_WC_SUCCESS : IB_WC_WR_FLUSH_ERR);
270 /* will get called again */
Ralph Campbellf9315512010-05-23 21:44:54 -0700271 goto done;
272 }
273
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800274 if (qp->s_flags & (RVT_S_WAIT_RNR | RVT_S_WAIT_ACK))
Ralph Campbellf9315512010-05-23 21:44:54 -0700275 goto bail;
276
277 if (qib_cmp24(qp->s_psn, qp->s_sending_hpsn) <= 0) {
278 if (qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) <= 0) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800279 qp->s_flags |= RVT_S_WAIT_PSN;
Ralph Campbellf9315512010-05-23 21:44:54 -0700280 goto bail;
281 }
282 qp->s_sending_psn = qp->s_psn;
283 qp->s_sending_hpsn = qp->s_psn - 1;
284 }
285
286 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
287 hwords = 5;
288 bth0 = 0;
289
290 /* Send a request. */
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800291 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
Ralph Campbellf9315512010-05-23 21:44:54 -0700292 switch (qp->s_state) {
293 default:
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800294 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK))
Ralph Campbellf9315512010-05-23 21:44:54 -0700295 goto bail;
296 /*
297 * Resend an old request or start a new one.
298 *
299 * We keep track of the current SWQE so that
300 * we don't reset the "furthest progress" state
301 * if we need to back up.
302 */
303 newreq = 0;
304 if (qp->s_cur == qp->s_tail) {
305 /* Check if send work queue is empty. */
Mike Marciniszyneb04ff02017-02-08 05:26:08 -0800306 smp_read_barrier_depends(); /* see post_one_send() */
307 if (qp->s_tail == READ_ONCE(qp->s_head))
Ralph Campbellf9315512010-05-23 21:44:54 -0700308 goto bail;
309 /*
310 * If a fence is requested, wait for previous
311 * RDMA read and atomic operations to finish.
312 */
313 if ((wqe->wr.send_flags & IB_SEND_FENCE) &&
314 qp->s_num_rd_atomic) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800315 qp->s_flags |= RVT_S_WAIT_FENCE;
Ralph Campbellf9315512010-05-23 21:44:54 -0700316 goto bail;
317 }
Ralph Campbellf9315512010-05-23 21:44:54 -0700318 newreq = 1;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800319 qp->s_psn = wqe->psn;
Ralph Campbellf9315512010-05-23 21:44:54 -0700320 }
321 /*
322 * Note that we have to be careful not to modify the
323 * original work request since we may need to resend
324 * it.
325 */
326 len = wqe->length;
327 ss = &qp->s_sge;
328 bth2 = qp->s_psn & QIB_PSN_MASK;
329 switch (wqe->wr.opcode) {
330 case IB_WR_SEND:
331 case IB_WR_SEND_WITH_IMM:
332 /* If no credit, return. */
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800333 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
Ralph Campbellf9315512010-05-23 21:44:54 -0700334 qib_cmp24(wqe->ssn, qp->s_lsn + 1) > 0) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800335 qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
Ralph Campbellf9315512010-05-23 21:44:54 -0700336 goto bail;
337 }
Ralph Campbellf9315512010-05-23 21:44:54 -0700338 if (len > pmtu) {
Ralph Campbellf9315512010-05-23 21:44:54 -0700339 qp->s_state = OP(SEND_FIRST);
340 len = pmtu;
341 break;
342 }
343 if (wqe->wr.opcode == IB_WR_SEND)
344 qp->s_state = OP(SEND_ONLY);
345 else {
346 qp->s_state = OP(SEND_ONLY_WITH_IMMEDIATE);
347 /* Immediate data comes after the BTH */
348 ohdr->u.imm_data = wqe->wr.ex.imm_data;
349 hwords += 1;
350 }
351 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
352 bth0 |= IB_BTH_SOLICITED;
353 bth2 |= IB_BTH_REQ_ACK;
354 if (++qp->s_cur == qp->s_size)
355 qp->s_cur = 0;
356 break;
357
358 case IB_WR_RDMA_WRITE:
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800359 if (newreq && !(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Ralph Campbellf9315512010-05-23 21:44:54 -0700360 qp->s_lsn++;
361 /* FALLTHROUGH */
362 case IB_WR_RDMA_WRITE_WITH_IMM:
363 /* If no credit, return. */
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800364 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
Ralph Campbellf9315512010-05-23 21:44:54 -0700365 qib_cmp24(wqe->ssn, qp->s_lsn + 1) > 0) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800366 qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
Ralph Campbellf9315512010-05-23 21:44:54 -0700367 goto bail;
368 }
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100369
Ralph Campbellf9315512010-05-23 21:44:54 -0700370 ohdr->u.rc.reth.vaddr =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100371 cpu_to_be64(wqe->rdma_wr.remote_addr);
Ralph Campbellf9315512010-05-23 21:44:54 -0700372 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100373 cpu_to_be32(wqe->rdma_wr.rkey);
Ralph Campbellf9315512010-05-23 21:44:54 -0700374 ohdr->u.rc.reth.length = cpu_to_be32(len);
375 hwords += sizeof(struct ib_reth) / sizeof(u32);
Ralph Campbellf9315512010-05-23 21:44:54 -0700376 if (len > pmtu) {
Ralph Campbellf9315512010-05-23 21:44:54 -0700377 qp->s_state = OP(RDMA_WRITE_FIRST);
378 len = pmtu;
379 break;
380 }
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100381 if (wqe->rdma_wr.wr.opcode == IB_WR_RDMA_WRITE)
Ralph Campbellf9315512010-05-23 21:44:54 -0700382 qp->s_state = OP(RDMA_WRITE_ONLY);
383 else {
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100384 qp->s_state = OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
Ralph Campbellf9315512010-05-23 21:44:54 -0700385 /* Immediate data comes after RETH */
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100386 ohdr->u.rc.imm_data =
387 wqe->rdma_wr.wr.ex.imm_data;
Ralph Campbellf9315512010-05-23 21:44:54 -0700388 hwords += 1;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100389 if (wqe->rdma_wr.wr.send_flags & IB_SEND_SOLICITED)
Ralph Campbellf9315512010-05-23 21:44:54 -0700390 bth0 |= IB_BTH_SOLICITED;
391 }
392 bth2 |= IB_BTH_REQ_ACK;
393 if (++qp->s_cur == qp->s_size)
394 qp->s_cur = 0;
395 break;
396
397 case IB_WR_RDMA_READ:
398 /*
399 * Don't allow more operations to be started
400 * than the QP limits allow.
401 */
402 if (newreq) {
403 if (qp->s_num_rd_atomic >=
404 qp->s_max_rd_atomic) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800405 qp->s_flags |= RVT_S_WAIT_RDMAR;
Ralph Campbellf9315512010-05-23 21:44:54 -0700406 goto bail;
407 }
408 qp->s_num_rd_atomic++;
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800409 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Ralph Campbellf9315512010-05-23 21:44:54 -0700410 qp->s_lsn++;
Ralph Campbellf9315512010-05-23 21:44:54 -0700411 }
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100412
Ralph Campbellf9315512010-05-23 21:44:54 -0700413 ohdr->u.rc.reth.vaddr =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100414 cpu_to_be64(wqe->rdma_wr.remote_addr);
Ralph Campbellf9315512010-05-23 21:44:54 -0700415 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100416 cpu_to_be32(wqe->rdma_wr.rkey);
Ralph Campbellf9315512010-05-23 21:44:54 -0700417 ohdr->u.rc.reth.length = cpu_to_be32(len);
418 qp->s_state = OP(RDMA_READ_REQUEST);
419 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
420 ss = NULL;
421 len = 0;
422 bth2 |= IB_BTH_REQ_ACK;
423 if (++qp->s_cur == qp->s_size)
424 qp->s_cur = 0;
425 break;
426
427 case IB_WR_ATOMIC_CMP_AND_SWP:
428 case IB_WR_ATOMIC_FETCH_AND_ADD:
429 /*
430 * Don't allow more operations to be started
431 * than the QP limits allow.
432 */
433 if (newreq) {
434 if (qp->s_num_rd_atomic >=
435 qp->s_max_rd_atomic) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800436 qp->s_flags |= RVT_S_WAIT_RDMAR;
Ralph Campbellf9315512010-05-23 21:44:54 -0700437 goto bail;
438 }
439 qp->s_num_rd_atomic++;
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800440 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Ralph Campbellf9315512010-05-23 21:44:54 -0700441 qp->s_lsn++;
Ralph Campbellf9315512010-05-23 21:44:54 -0700442 }
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100443 if (wqe->atomic_wr.wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
Ralph Campbellf9315512010-05-23 21:44:54 -0700444 qp->s_state = OP(COMPARE_SWAP);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700445 put_ib_ateth_swap(wqe->atomic_wr.swap,
446 &ohdr->u.atomic_eth);
447 put_ib_ateth_swap(wqe->atomic_wr.compare_add,
448 &ohdr->u.atomic_eth);
Ralph Campbellf9315512010-05-23 21:44:54 -0700449 } else {
450 qp->s_state = OP(FETCH_ADD);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700451 put_ib_ateth_swap(wqe->atomic_wr.compare_add,
452 &ohdr->u.atomic_eth);
453 put_ib_ateth_swap(0, &ohdr->u.atomic_eth);
Ralph Campbellf9315512010-05-23 21:44:54 -0700454 }
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700455 put_ib_ateth_vaddr(wqe->atomic_wr.remote_addr,
456 &ohdr->u.atomic_eth);
Ralph Campbellf9315512010-05-23 21:44:54 -0700457 ohdr->u.atomic_eth.rkey = cpu_to_be32(
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100458 wqe->atomic_wr.rkey);
Ralph Campbellf9315512010-05-23 21:44:54 -0700459 hwords += sizeof(struct ib_atomic_eth) / sizeof(u32);
460 ss = NULL;
461 len = 0;
462 bth2 |= IB_BTH_REQ_ACK;
463 if (++qp->s_cur == qp->s_size)
464 qp->s_cur = 0;
465 break;
466
467 default:
468 goto bail;
469 }
470 qp->s_sge.sge = wqe->sg_list[0];
471 qp->s_sge.sg_list = wqe->sg_list + 1;
472 qp->s_sge.num_sge = wqe->wr.num_sge;
473 qp->s_sge.total_len = wqe->length;
474 qp->s_len = wqe->length;
475 if (newreq) {
476 qp->s_tail++;
477 if (qp->s_tail >= qp->s_size)
478 qp->s_tail = 0;
479 }
480 if (wqe->wr.opcode == IB_WR_RDMA_READ)
481 qp->s_psn = wqe->lpsn + 1;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800482 else
Ralph Campbellf9315512010-05-23 21:44:54 -0700483 qp->s_psn++;
Ralph Campbellf9315512010-05-23 21:44:54 -0700484 break;
485
486 case OP(RDMA_READ_RESPONSE_FIRST):
487 /*
488 * qp->s_state is normally set to the opcode of the
489 * last packet constructed for new requests and therefore
490 * is never set to RDMA read response.
491 * RDMA_READ_RESPONSE_FIRST is used by the ACK processing
492 * thread to indicate a SEND needs to be restarted from an
493 * earlier PSN without interferring with the sending thread.
494 * See qib_restart_rc().
495 */
496 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
497 /* FALLTHROUGH */
498 case OP(SEND_FIRST):
499 qp->s_state = OP(SEND_MIDDLE);
500 /* FALLTHROUGH */
501 case OP(SEND_MIDDLE):
502 bth2 = qp->s_psn++ & QIB_PSN_MASK;
Ralph Campbellf9315512010-05-23 21:44:54 -0700503 ss = &qp->s_sge;
504 len = qp->s_len;
505 if (len > pmtu) {
506 len = pmtu;
507 break;
508 }
509 if (wqe->wr.opcode == IB_WR_SEND)
510 qp->s_state = OP(SEND_LAST);
511 else {
512 qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
513 /* Immediate data comes after the BTH */
514 ohdr->u.imm_data = wqe->wr.ex.imm_data;
515 hwords += 1;
516 }
517 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
518 bth0 |= IB_BTH_SOLICITED;
519 bth2 |= IB_BTH_REQ_ACK;
520 qp->s_cur++;
521 if (qp->s_cur >= qp->s_size)
522 qp->s_cur = 0;
523 break;
524
525 case OP(RDMA_READ_RESPONSE_LAST):
526 /*
527 * qp->s_state is normally set to the opcode of the
528 * last packet constructed for new requests and therefore
529 * is never set to RDMA read response.
530 * RDMA_READ_RESPONSE_LAST is used by the ACK processing
531 * thread to indicate a RDMA write needs to be restarted from
532 * an earlier PSN without interferring with the sending thread.
533 * See qib_restart_rc().
534 */
535 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
536 /* FALLTHROUGH */
537 case OP(RDMA_WRITE_FIRST):
538 qp->s_state = OP(RDMA_WRITE_MIDDLE);
539 /* FALLTHROUGH */
540 case OP(RDMA_WRITE_MIDDLE):
541 bth2 = qp->s_psn++ & QIB_PSN_MASK;
Ralph Campbellf9315512010-05-23 21:44:54 -0700542 ss = &qp->s_sge;
543 len = qp->s_len;
544 if (len > pmtu) {
545 len = pmtu;
546 break;
547 }
548 if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
549 qp->s_state = OP(RDMA_WRITE_LAST);
550 else {
551 qp->s_state = OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
552 /* Immediate data comes after the BTH */
553 ohdr->u.imm_data = wqe->wr.ex.imm_data;
554 hwords += 1;
555 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
556 bth0 |= IB_BTH_SOLICITED;
557 }
558 bth2 |= IB_BTH_REQ_ACK;
559 qp->s_cur++;
560 if (qp->s_cur >= qp->s_size)
561 qp->s_cur = 0;
562 break;
563
564 case OP(RDMA_READ_RESPONSE_MIDDLE):
565 /*
566 * qp->s_state is normally set to the opcode of the
567 * last packet constructed for new requests and therefore
568 * is never set to RDMA read response.
569 * RDMA_READ_RESPONSE_MIDDLE is used by the ACK processing
570 * thread to indicate a RDMA read needs to be restarted from
571 * an earlier PSN without interferring with the sending thread.
572 * See qib_restart_rc().
573 */
574 len = ((qp->s_psn - wqe->psn) & QIB_PSN_MASK) * pmtu;
575 ohdr->u.rc.reth.vaddr =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100576 cpu_to_be64(wqe->rdma_wr.remote_addr + len);
Ralph Campbellf9315512010-05-23 21:44:54 -0700577 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100578 cpu_to_be32(wqe->rdma_wr.rkey);
Ralph Campbellf9315512010-05-23 21:44:54 -0700579 ohdr->u.rc.reth.length = cpu_to_be32(wqe->length - len);
580 qp->s_state = OP(RDMA_READ_REQUEST);
581 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
582 bth2 = (qp->s_psn & QIB_PSN_MASK) | IB_BTH_REQ_ACK;
583 qp->s_psn = wqe->lpsn + 1;
584 ss = NULL;
585 len = 0;
586 qp->s_cur++;
587 if (qp->s_cur == qp->s_size)
588 qp->s_cur = 0;
589 break;
590 }
591 qp->s_sending_hpsn = bth2;
592 delta = (((int) bth2 - (int) wqe->psn) << 8) >> 8;
593 if (delta && delta % QIB_PSN_CREDIT == 0)
594 bth2 |= IB_BTH_REQ_ACK;
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800595 if (qp->s_flags & RVT_S_SEND_ONE) {
596 qp->s_flags &= ~RVT_S_SEND_ONE;
597 qp->s_flags |= RVT_S_WAIT_ACK;
Ralph Campbellf9315512010-05-23 21:44:54 -0700598 bth2 |= IB_BTH_REQ_ACK;
599 }
600 qp->s_len -= len;
601 qp->s_hdrwords = hwords;
602 qp->s_cur_sge = ss;
603 qp->s_cur_size = len;
604 qib_make_ruc_header(qp, ohdr, bth0 | (qp->s_state << 24), bth2);
605done:
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800606 return 1;
Ralph Campbellf9315512010-05-23 21:44:54 -0700607bail:
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800608 qp->s_flags &= ~RVT_S_BUSY;
Ralph Campbellf9315512010-05-23 21:44:54 -0700609 return ret;
610}
611
612/**
613 * qib_send_rc_ack - Construct an ACK packet and send it
614 * @qp: a pointer to the QP
615 *
616 * This is called from qib_rc_rcv() and qib_kreceive().
617 * Note that RDMA reads and atomics are handled in the
618 * send side QP state and tasklet.
619 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800620void qib_send_rc_ack(struct rvt_qp *qp)
Ralph Campbellf9315512010-05-23 21:44:54 -0700621{
622 struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
623 struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
624 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
625 u64 pbc;
626 u16 lrh0;
627 u32 bth0;
628 u32 hwords;
629 u32 pbufn;
630 u32 __iomem *piobuf;
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700631 struct ib_header hdr;
632 struct ib_other_headers *ohdr;
Ralph Campbellf9315512010-05-23 21:44:54 -0700633 u32 control;
634 unsigned long flags;
635
636 spin_lock_irqsave(&qp->s_lock, flags);
637
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800638 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
Ralph Campbellf9315512010-05-23 21:44:54 -0700639 goto unlock;
640
641 /* Don't send ACK or NAK if a RDMA read or atomic is pending. */
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800642 if ((qp->s_flags & RVT_S_RESP_PENDING) || qp->s_rdma_ack_cnt)
Ralph Campbellf9315512010-05-23 21:44:54 -0700643 goto queue_ack;
644
645 /* Construct the header with s_lock held so APM doesn't change it. */
646 ohdr = &hdr.u.oth;
647 lrh0 = QIB_LRH_BTH;
648 /* header size in 32-bit words LRH+BTH+AETH = (8+12+4)/4. */
649 hwords = 6;
650 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
651 hwords += qib_make_grh(ibp, &hdr.u.l.grh,
652 &qp->remote_ah_attr.grh, hwords, 0);
653 ohdr = &hdr.u.l.oth;
654 lrh0 = QIB_LRH_GRH;
655 }
656 /* read pkey_index w/o lock (its atomic) */
657 bth0 = qib_get_pkey(ibp, qp->s_pkey_index) | (OP(ACKNOWLEDGE) << 24);
658 if (qp->s_mig_state == IB_MIG_MIGRATED)
659 bth0 |= IB_BTH_MIG_REQ;
660 if (qp->r_nak_state)
661 ohdr->u.aeth = cpu_to_be32((qp->r_msn & QIB_MSN_MASK) |
662 (qp->r_nak_state <<
663 QIB_AETH_CREDIT_SHIFT));
664 else
665 ohdr->u.aeth = qib_compute_aeth(qp);
666 lrh0 |= ibp->sl_to_vl[qp->remote_ah_attr.sl] << 12 |
667 qp->remote_ah_attr.sl << 4;
668 hdr.lrh[0] = cpu_to_be16(lrh0);
669 hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
670 hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
671 hdr.lrh[3] = cpu_to_be16(ppd->lid | qp->remote_ah_attr.src_path_bits);
672 ohdr->bth[0] = cpu_to_be32(bth0);
673 ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
674 ohdr->bth[2] = cpu_to_be32(qp->r_ack_psn & QIB_PSN_MASK);
675
676 spin_unlock_irqrestore(&qp->s_lock, flags);
677
678 /* Don't try to send ACKs if the link isn't ACTIVE */
679 if (!(ppd->lflags & QIBL_LINKACTIVE))
680 goto done;
681
682 control = dd->f_setpbc_control(ppd, hwords + SIZE_OF_CRC,
683 qp->s_srate, lrh0 >> 12);
684 /* length is + 1 for the control dword */
685 pbc = ((u64) control << 32) | (hwords + 1);
686
687 piobuf = dd->f_getsendbuf(ppd, pbc, &pbufn);
688 if (!piobuf) {
689 /*
690 * We are out of PIO buffers at the moment.
691 * Pass responsibility for sending the ACK to the
692 * send tasklet so that when a PIO buffer becomes
693 * available, the ACK is sent ahead of other outgoing
694 * packets.
695 */
696 spin_lock_irqsave(&qp->s_lock, flags);
697 goto queue_ack;
698 }
699
700 /*
701 * Write the pbc.
702 * We have to flush after the PBC for correctness
703 * on some cpus or WC buffer can be written out of order.
704 */
705 writeq(pbc, piobuf);
706
707 if (dd->flags & QIB_PIO_FLUSH_WC) {
708 u32 *hdrp = (u32 *) &hdr;
709
710 qib_flush_wc();
711 qib_pio_copy(piobuf + 2, hdrp, hwords - 1);
712 qib_flush_wc();
713 __raw_writel(hdrp[hwords - 1], piobuf + hwords + 1);
714 } else
715 qib_pio_copy(piobuf + 2, (u32 *) &hdr, hwords);
716
717 if (dd->flags & QIB_USE_SPCL_TRIG) {
718 u32 spcl_off = (pbufn >= dd->piobcnt2k) ? 2047 : 1023;
719
720 qib_flush_wc();
721 __raw_writel(0xaebecede, piobuf + spcl_off);
722 }
723
724 qib_flush_wc();
725 qib_sendbuf_done(dd, pbufn);
726
Mike Marciniszyn7d7632a2014-03-07 08:40:55 -0500727 this_cpu_inc(ibp->pmastats->n_unicast_xmit);
Ralph Campbellf9315512010-05-23 21:44:54 -0700728 goto done;
729
730queue_ack:
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800731 if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
Harish Chegondif24a6d42016-01-22 12:56:02 -0800732 this_cpu_inc(*ibp->rvp.rc_qacks);
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800733 qp->s_flags |= RVT_S_ACK_PENDING | RVT_S_RESP_PENDING;
Ralph Campbellf9315512010-05-23 21:44:54 -0700734 qp->s_nak_state = qp->r_nak_state;
735 qp->s_ack_psn = qp->r_ack_psn;
736
737 /* Schedule the send tasklet. */
738 qib_schedule_send(qp);
739 }
740unlock:
741 spin_unlock_irqrestore(&qp->s_lock, flags);
742done:
743 return;
744}
745
746/**
747 * reset_psn - reset the QP state to send starting from PSN
748 * @qp: the QP
749 * @psn: the packet sequence number to restart at
750 *
751 * This is called from qib_rc_rcv() to process an incoming RC ACK
752 * for the given QP.
753 * Called at interrupt level with the QP s_lock held.
754 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800755static void reset_psn(struct rvt_qp *qp, u32 psn)
Ralph Campbellf9315512010-05-23 21:44:54 -0700756{
757 u32 n = qp->s_acked;
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800758 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, n);
Ralph Campbellf9315512010-05-23 21:44:54 -0700759 u32 opcode;
760
761 qp->s_cur = n;
762
763 /*
764 * If we are starting the request from the beginning,
765 * let the normal send code handle initialization.
766 */
767 if (qib_cmp24(psn, wqe->psn) <= 0) {
768 qp->s_state = OP(SEND_LAST);
769 goto done;
770 }
771
772 /* Find the work request opcode corresponding to the given PSN. */
773 opcode = wqe->wr.opcode;
774 for (;;) {
775 int diff;
776
777 if (++n == qp->s_size)
778 n = 0;
779 if (n == qp->s_tail)
780 break;
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800781 wqe = rvt_get_swqe_ptr(qp, n);
Ralph Campbellf9315512010-05-23 21:44:54 -0700782 diff = qib_cmp24(psn, wqe->psn);
783 if (diff < 0)
784 break;
785 qp->s_cur = n;
786 /*
787 * If we are starting the request from the beginning,
788 * let the normal send code handle initialization.
789 */
790 if (diff == 0) {
791 qp->s_state = OP(SEND_LAST);
792 goto done;
793 }
794 opcode = wqe->wr.opcode;
795 }
796
797 /*
798 * Set the state to restart in the middle of a request.
799 * Don't change the s_sge, s_cur_sge, or s_cur_size.
800 * See qib_make_rc_req().
801 */
802 switch (opcode) {
803 case IB_WR_SEND:
804 case IB_WR_SEND_WITH_IMM:
805 qp->s_state = OP(RDMA_READ_RESPONSE_FIRST);
806 break;
807
808 case IB_WR_RDMA_WRITE:
809 case IB_WR_RDMA_WRITE_WITH_IMM:
810 qp->s_state = OP(RDMA_READ_RESPONSE_LAST);
811 break;
812
813 case IB_WR_RDMA_READ:
814 qp->s_state = OP(RDMA_READ_RESPONSE_MIDDLE);
815 break;
816
817 default:
818 /*
819 * This case shouldn't happen since its only
820 * one PSN per req.
821 */
822 qp->s_state = OP(SEND_LAST);
823 }
824done:
825 qp->s_psn = psn;
826 /*
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800827 * Set RVT_S_WAIT_PSN as qib_rc_complete() may start the timer
Ralph Campbellf9315512010-05-23 21:44:54 -0700828 * asynchronously before the send tasklet can get scheduled.
829 * Doing it in qib_make_rc_req() is too late.
830 */
831 if ((qib_cmp24(qp->s_psn, qp->s_sending_hpsn) <= 0) &&
832 (qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) <= 0))
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800833 qp->s_flags |= RVT_S_WAIT_PSN;
Ralph Campbellf9315512010-05-23 21:44:54 -0700834}
835
836/*
837 * Back up requester to resend the last un-ACKed request.
Ralph Campbella5210c12010-08-02 22:39:30 +0000838 * The QP r_lock and s_lock should be held and interrupts disabled.
Ralph Campbellf9315512010-05-23 21:44:54 -0700839 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800840static void qib_restart_rc(struct rvt_qp *qp, u32 psn, int wait)
Ralph Campbellf9315512010-05-23 21:44:54 -0700841{
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800842 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Ralph Campbellf9315512010-05-23 21:44:54 -0700843 struct qib_ibport *ibp;
844
845 if (qp->s_retry == 0) {
846 if (qp->s_mig_state == IB_MIG_ARMED) {
847 qib_migrate_qp(qp);
848 qp->s_retry = qp->s_retry_cnt;
849 } else if (qp->s_last == qp->s_acked) {
850 qib_send_complete(qp, wqe, IB_WC_RETRY_EXC_ERR);
Harish Chegondi70696ea2016-02-03 14:20:27 -0800851 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Ralph Campbellf9315512010-05-23 21:44:54 -0700852 return;
853 } else /* XXX need to handle delayed completion */
854 return;
855 } else
856 qp->s_retry--;
857
858 ibp = to_iport(qp->ibqp.device, qp->port_num);
859 if (wqe->wr.opcode == IB_WR_RDMA_READ)
Harish Chegondif24a6d42016-01-22 12:56:02 -0800860 ibp->rvp.n_rc_resends++;
Ralph Campbellf9315512010-05-23 21:44:54 -0700861 else
Harish Chegondif24a6d42016-01-22 12:56:02 -0800862 ibp->rvp.n_rc_resends += (qp->s_psn - psn) & QIB_PSN_MASK;
Ralph Campbellf9315512010-05-23 21:44:54 -0700863
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800864 qp->s_flags &= ~(RVT_S_WAIT_FENCE | RVT_S_WAIT_RDMAR |
865 RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_PSN |
866 RVT_S_WAIT_ACK);
Ralph Campbellf9315512010-05-23 21:44:54 -0700867 if (wait)
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800868 qp->s_flags |= RVT_S_SEND_ONE;
Ralph Campbellf9315512010-05-23 21:44:54 -0700869 reset_psn(qp, psn);
870}
871
872/*
873 * This is called from s_timer for missing responses.
874 */
875static void rc_timeout(unsigned long arg)
876{
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800877 struct rvt_qp *qp = (struct rvt_qp *)arg;
Ralph Campbellf9315512010-05-23 21:44:54 -0700878 struct qib_ibport *ibp;
879 unsigned long flags;
880
Ralph Campbella5210c12010-08-02 22:39:30 +0000881 spin_lock_irqsave(&qp->r_lock, flags);
882 spin_lock(&qp->s_lock);
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800883 if (qp->s_flags & RVT_S_TIMER) {
Ralph Campbellf9315512010-05-23 21:44:54 -0700884 ibp = to_iport(qp->ibqp.device, qp->port_num);
Harish Chegondif24a6d42016-01-22 12:56:02 -0800885 ibp->rvp.n_rc_timeouts++;
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800886 qp->s_flags &= ~RVT_S_TIMER;
Ralph Campbellf9315512010-05-23 21:44:54 -0700887 del_timer(&qp->s_timer);
888 qib_restart_rc(qp, qp->s_last_psn + 1, 1);
889 qib_schedule_send(qp);
890 }
Ralph Campbella5210c12010-08-02 22:39:30 +0000891 spin_unlock(&qp->s_lock);
892 spin_unlock_irqrestore(&qp->r_lock, flags);
Ralph Campbellf9315512010-05-23 21:44:54 -0700893}
894
895/*
896 * This is called from s_timer for RNR timeouts.
897 */
898void qib_rc_rnr_retry(unsigned long arg)
899{
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800900 struct rvt_qp *qp = (struct rvt_qp *)arg;
Ralph Campbellf9315512010-05-23 21:44:54 -0700901 unsigned long flags;
902
903 spin_lock_irqsave(&qp->s_lock, flags);
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800904 if (qp->s_flags & RVT_S_WAIT_RNR) {
905 qp->s_flags &= ~RVT_S_WAIT_RNR;
Ralph Campbellf9315512010-05-23 21:44:54 -0700906 del_timer(&qp->s_timer);
907 qib_schedule_send(qp);
908 }
909 spin_unlock_irqrestore(&qp->s_lock, flags);
910}
911
912/*
913 * Set qp->s_sending_psn to the next PSN after the given one.
914 * This would be psn+1 except when RDMA reads are present.
915 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800916static void reset_sending_psn(struct rvt_qp *qp, u32 psn)
Ralph Campbellf9315512010-05-23 21:44:54 -0700917{
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800918 struct rvt_swqe *wqe;
Ralph Campbellf9315512010-05-23 21:44:54 -0700919 u32 n = qp->s_last;
920
921 /* Find the work request corresponding to the given PSN. */
922 for (;;) {
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800923 wqe = rvt_get_swqe_ptr(qp, n);
Ralph Campbellf9315512010-05-23 21:44:54 -0700924 if (qib_cmp24(psn, wqe->lpsn) <= 0) {
925 if (wqe->wr.opcode == IB_WR_RDMA_READ)
926 qp->s_sending_psn = wqe->lpsn + 1;
927 else
928 qp->s_sending_psn = psn + 1;
929 break;
930 }
931 if (++n == qp->s_size)
932 n = 0;
933 if (n == qp->s_tail)
934 break;
935 }
936}
937
938/*
939 * This should be called with the QP s_lock held and interrupts disabled.
940 */
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700941void qib_rc_send_complete(struct rvt_qp *qp, struct ib_header *hdr)
Ralph Campbellf9315512010-05-23 21:44:54 -0700942{
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700943 struct ib_other_headers *ohdr;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800944 struct rvt_swqe *wqe;
Ralph Campbellf9315512010-05-23 21:44:54 -0700945 u32 opcode;
946 u32 psn;
947
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800948 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
Ralph Campbellf9315512010-05-23 21:44:54 -0700949 return;
950
951 /* Find out where the BTH is */
952 if ((be16_to_cpu(hdr->lrh[0]) & 3) == QIB_LRH_BTH)
953 ohdr = &hdr->u.oth;
954 else
955 ohdr = &hdr->u.l.oth;
956
957 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
958 if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
959 opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
960 WARN_ON(!qp->s_rdma_ack_cnt);
961 qp->s_rdma_ack_cnt--;
962 return;
963 }
964
965 psn = be32_to_cpu(ohdr->bth[2]);
966 reset_sending_psn(qp, psn);
967
968 /*
969 * Start timer after a packet requesting an ACK has been sent and
970 * there are still requests that haven't been acked.
971 */
972 if ((psn & IB_BTH_REQ_ACK) && qp->s_acked != qp->s_tail &&
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800973 !(qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR | RVT_S_WAIT_PSN)) &&
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800974 (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
Ralph Campbellf9315512010-05-23 21:44:54 -0700975 start_timer(qp);
976
977 while (qp->s_last != qp->s_acked) {
Mike Marciniszynee845412016-02-04 11:03:28 -0800978 u32 s_last;
979
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800980 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
Ralph Campbellf9315512010-05-23 21:44:54 -0700981 if (qib_cmp24(wqe->lpsn, qp->s_sending_psn) >= 0 &&
982 qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) <= 0)
983 break;
Mike Marciniszynee845412016-02-04 11:03:28 -0800984 s_last = qp->s_last;
985 if (++s_last >= qp->s_size)
986 s_last = 0;
987 qp->s_last = s_last;
988 /* see post_send() */
989 barrier();
Mike Marciniszync64607a2016-12-07 19:34:31 -0800990 rvt_put_swqe(wqe);
Mike Marciniszyn0771da52016-12-07 19:34:12 -0800991 rvt_qp_swqe_complete(qp, wqe, IB_WC_SUCCESS);
Ralph Campbellf9315512010-05-23 21:44:54 -0700992 }
993 /*
994 * If we were waiting for sends to complete before resending,
995 * and they are now complete, restart sending.
996 */
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800997 if (qp->s_flags & RVT_S_WAIT_PSN &&
Ralph Campbellf9315512010-05-23 21:44:54 -0700998 qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800999 qp->s_flags &= ~RVT_S_WAIT_PSN;
Ralph Campbellf9315512010-05-23 21:44:54 -07001000 qp->s_sending_psn = qp->s_psn;
1001 qp->s_sending_hpsn = qp->s_psn - 1;
1002 qib_schedule_send(qp);
1003 }
1004}
1005
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001006static inline void update_last_psn(struct rvt_qp *qp, u32 psn)
Ralph Campbellf9315512010-05-23 21:44:54 -07001007{
1008 qp->s_last_psn = psn;
1009}
1010
1011/*
1012 * Generate a SWQE completion.
1013 * This is similar to qib_send_complete but has to check to be sure
1014 * that the SGEs are not being referenced if the SWQE is being resent.
1015 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001016static struct rvt_swqe *do_rc_completion(struct rvt_qp *qp,
1017 struct rvt_swqe *wqe,
Ralph Campbellf9315512010-05-23 21:44:54 -07001018 struct qib_ibport *ibp)
1019{
Ralph Campbellf9315512010-05-23 21:44:54 -07001020 /*
1021 * Don't decrement refcount and don't generate a
1022 * completion if the SWQE is being resent until the send
1023 * is finished.
1024 */
1025 if (qib_cmp24(wqe->lpsn, qp->s_sending_psn) < 0 ||
1026 qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
Mike Marciniszynee845412016-02-04 11:03:28 -08001027 u32 s_last;
1028
Mike Marciniszync64607a2016-12-07 19:34:31 -08001029 rvt_put_swqe(wqe);
Mike Marciniszynee845412016-02-04 11:03:28 -08001030 s_last = qp->s_last;
1031 if (++s_last >= qp->s_size)
1032 s_last = 0;
1033 qp->s_last = s_last;
1034 /* see post_send() */
1035 barrier();
Mike Marciniszyn0771da52016-12-07 19:34:12 -08001036 rvt_qp_swqe_complete(qp, wqe, IB_WC_SUCCESS);
Ralph Campbellf9315512010-05-23 21:44:54 -07001037 } else
Harish Chegondif24a6d42016-01-22 12:56:02 -08001038 this_cpu_inc(*ibp->rvp.rc_delayed_comp);
Ralph Campbellf9315512010-05-23 21:44:54 -07001039
1040 qp->s_retry = qp->s_retry_cnt;
1041 update_last_psn(qp, wqe->lpsn);
1042
1043 /*
1044 * If we are completing a request which is in the process of
1045 * being resent, we can stop resending it since we know the
1046 * responder has already seen it.
1047 */
1048 if (qp->s_acked == qp->s_cur) {
1049 if (++qp->s_cur >= qp->s_size)
1050 qp->s_cur = 0;
1051 qp->s_acked = qp->s_cur;
Harish Chegondidb3ef0e2016-01-22 13:07:42 -08001052 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
Ralph Campbellf9315512010-05-23 21:44:54 -07001053 if (qp->s_acked != qp->s_tail) {
1054 qp->s_state = OP(SEND_LAST);
1055 qp->s_psn = wqe->psn;
1056 }
1057 } else {
1058 if (++qp->s_acked >= qp->s_size)
1059 qp->s_acked = 0;
1060 if (qp->state == IB_QPS_SQD && qp->s_acked == qp->s_cur)
1061 qp->s_draining = 0;
Harish Chegondidb3ef0e2016-01-22 13:07:42 -08001062 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Ralph Campbellf9315512010-05-23 21:44:54 -07001063 }
1064 return wqe;
1065}
1066
1067/**
1068 * do_rc_ack - process an incoming RC ACK
1069 * @qp: the QP the ACK came in on
1070 * @psn: the packet sequence number of the ACK
1071 * @opcode: the opcode of the request that resulted in the ACK
1072 *
1073 * This is called from qib_rc_rcv_resp() to process an incoming RC ACK
1074 * for the given QP.
1075 * Called at interrupt level with the QP s_lock held.
1076 * Returns 1 if OK, 0 if current operation should be aborted (NAK).
1077 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001078static int do_rc_ack(struct rvt_qp *qp, u32 aeth, u32 psn, int opcode,
Ralph Campbellf9315512010-05-23 21:44:54 -07001079 u64 val, struct qib_ctxtdata *rcd)
1080{
1081 struct qib_ibport *ibp;
1082 enum ib_wc_status status;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001083 struct rvt_swqe *wqe;
Ralph Campbellf9315512010-05-23 21:44:54 -07001084 int ret = 0;
1085 u32 ack_psn;
1086 int diff;
1087
1088 /* Remove QP from retry timer */
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001089 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
1090 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
Ralph Campbellf9315512010-05-23 21:44:54 -07001091 del_timer(&qp->s_timer);
1092 }
1093
1094 /*
1095 * Note that NAKs implicitly ACK outstanding SEND and RDMA write
1096 * requests and implicitly NAK RDMA read and atomic requests issued
1097 * before the NAK'ed request. The MSN won't include the NAK'ed
1098 * request but will include an ACK'ed request(s).
1099 */
1100 ack_psn = psn;
1101 if (aeth >> 29)
1102 ack_psn--;
Harish Chegondidb3ef0e2016-01-22 13:07:42 -08001103 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Ralph Campbellf9315512010-05-23 21:44:54 -07001104 ibp = to_iport(qp->ibqp.device, qp->port_num);
1105
1106 /*
1107 * The MSN might be for a later WQE than the PSN indicates so
1108 * only complete WQEs that the PSN finishes.
1109 */
1110 while ((diff = qib_cmp24(ack_psn, wqe->lpsn)) >= 0) {
1111 /*
1112 * RDMA_READ_RESPONSE_ONLY is a special case since
1113 * we want to generate completion events for everything
1114 * before the RDMA read, copy the data, then generate
1115 * the completion for the read.
1116 */
1117 if (wqe->wr.opcode == IB_WR_RDMA_READ &&
1118 opcode == OP(RDMA_READ_RESPONSE_ONLY) &&
1119 diff == 0) {
1120 ret = 1;
1121 goto bail;
1122 }
1123 /*
1124 * If this request is a RDMA read or atomic, and the ACK is
1125 * for a later operation, this ACK NAKs the RDMA read or
1126 * atomic. In other words, only a RDMA_READ_LAST or ONLY
1127 * can ACK a RDMA read and likewise for atomic ops. Note
1128 * that the NAK case can only happen if relaxed ordering is
1129 * used and requests are sent after an RDMA read or atomic
1130 * is sent but before the response is received.
1131 */
1132 if ((wqe->wr.opcode == IB_WR_RDMA_READ &&
1133 (opcode != OP(RDMA_READ_RESPONSE_LAST) || diff != 0)) ||
1134 ((wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1135 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) &&
1136 (opcode != OP(ATOMIC_ACKNOWLEDGE) || diff != 0))) {
1137 /* Retry this request. */
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001138 if (!(qp->r_flags & RVT_R_RDMAR_SEQ)) {
1139 qp->r_flags |= RVT_R_RDMAR_SEQ;
Ralph Campbellf9315512010-05-23 21:44:54 -07001140 qib_restart_rc(qp, qp->s_last_psn + 1, 0);
1141 if (list_empty(&qp->rspwait)) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001142 qp->r_flags |= RVT_R_RSP_SEND;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001143 rvt_get_qp(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07001144 list_add_tail(&qp->rspwait,
1145 &rcd->qp_wait_list);
1146 }
1147 }
1148 /*
1149 * No need to process the ACK/NAK since we are
1150 * restarting an earlier request.
1151 */
1152 goto bail;
1153 }
1154 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1155 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
1156 u64 *vaddr = wqe->sg_list[0].vaddr;
1157 *vaddr = val;
1158 }
1159 if (qp->s_num_rd_atomic &&
1160 (wqe->wr.opcode == IB_WR_RDMA_READ ||
1161 wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1162 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)) {
1163 qp->s_num_rd_atomic--;
1164 /* Restart sending task if fence is complete */
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001165 if ((qp->s_flags & RVT_S_WAIT_FENCE) &&
Ralph Campbellf9315512010-05-23 21:44:54 -07001166 !qp->s_num_rd_atomic) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001167 qp->s_flags &= ~(RVT_S_WAIT_FENCE |
1168 RVT_S_WAIT_ACK);
Ralph Campbellf9315512010-05-23 21:44:54 -07001169 qib_schedule_send(qp);
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001170 } else if (qp->s_flags & RVT_S_WAIT_RDMAR) {
1171 qp->s_flags &= ~(RVT_S_WAIT_RDMAR |
1172 RVT_S_WAIT_ACK);
Ralph Campbellf9315512010-05-23 21:44:54 -07001173 qib_schedule_send(qp);
1174 }
1175 }
1176 wqe = do_rc_completion(qp, wqe, ibp);
1177 if (qp->s_acked == qp->s_tail)
1178 break;
1179 }
1180
1181 switch (aeth >> 29) {
1182 case 0: /* ACK */
Harish Chegondif24a6d42016-01-22 12:56:02 -08001183 this_cpu_inc(*ibp->rvp.rc_acks);
Ralph Campbellf9315512010-05-23 21:44:54 -07001184 if (qp->s_acked != qp->s_tail) {
1185 /*
1186 * We are expecting more ACKs so
1187 * reset the retransmit timer.
1188 */
1189 start_timer(qp);
1190 /*
1191 * We can stop resending the earlier packets and
1192 * continue with the next packet the receiver wants.
1193 */
1194 if (qib_cmp24(qp->s_psn, psn) <= 0)
1195 reset_psn(qp, psn + 1);
1196 } else if (qib_cmp24(qp->s_psn, psn) <= 0) {
1197 qp->s_state = OP(SEND_LAST);
1198 qp->s_psn = psn + 1;
1199 }
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001200 if (qp->s_flags & RVT_S_WAIT_ACK) {
1201 qp->s_flags &= ~RVT_S_WAIT_ACK;
Ralph Campbellf9315512010-05-23 21:44:54 -07001202 qib_schedule_send(qp);
1203 }
1204 qib_get_credit(qp, aeth);
1205 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1206 qp->s_retry = qp->s_retry_cnt;
1207 update_last_psn(qp, psn);
1208 ret = 1;
1209 goto bail;
1210
1211 case 1: /* RNR NAK */
Harish Chegondif24a6d42016-01-22 12:56:02 -08001212 ibp->rvp.n_rnr_naks++;
Ralph Campbellf9315512010-05-23 21:44:54 -07001213 if (qp->s_acked == qp->s_tail)
1214 goto bail;
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001215 if (qp->s_flags & RVT_S_WAIT_RNR)
Ralph Campbellf9315512010-05-23 21:44:54 -07001216 goto bail;
1217 if (qp->s_rnr_retry == 0) {
1218 status = IB_WC_RNR_RETRY_EXC_ERR;
1219 goto class_b;
1220 }
1221 if (qp->s_rnr_retry_cnt < 7)
1222 qp->s_rnr_retry--;
1223
1224 /* The last valid PSN is the previous PSN. */
1225 update_last_psn(qp, psn - 1);
1226
Harish Chegondif24a6d42016-01-22 12:56:02 -08001227 ibp->rvp.n_rc_resends += (qp->s_psn - psn) & QIB_PSN_MASK;
Ralph Campbellf9315512010-05-23 21:44:54 -07001228
1229 reset_psn(qp, psn);
1230
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001231 qp->s_flags &= ~(RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_ACK);
1232 qp->s_flags |= RVT_S_WAIT_RNR;
Ralph Campbellf9315512010-05-23 21:44:54 -07001233 qp->s_timer.function = qib_rc_rnr_retry;
1234 qp->s_timer.expires = jiffies + usecs_to_jiffies(
1235 ib_qib_rnr_table[(aeth >> QIB_AETH_CREDIT_SHIFT) &
1236 QIB_AETH_CREDIT_MASK]);
1237 add_timer(&qp->s_timer);
1238 goto bail;
1239
1240 case 3: /* NAK */
1241 if (qp->s_acked == qp->s_tail)
1242 goto bail;
1243 /* The last valid PSN is the previous PSN. */
1244 update_last_psn(qp, psn - 1);
1245 switch ((aeth >> QIB_AETH_CREDIT_SHIFT) &
1246 QIB_AETH_CREDIT_MASK) {
1247 case 0: /* PSN sequence error */
Harish Chegondif24a6d42016-01-22 12:56:02 -08001248 ibp->rvp.n_seq_naks++;
Ralph Campbellf9315512010-05-23 21:44:54 -07001249 /*
1250 * Back up to the responder's expected PSN.
1251 * Note that we might get a NAK in the middle of an
1252 * RDMA READ response which terminates the RDMA
1253 * READ.
1254 */
1255 qib_restart_rc(qp, psn, 0);
1256 qib_schedule_send(qp);
1257 break;
1258
1259 case 1: /* Invalid Request */
1260 status = IB_WC_REM_INV_REQ_ERR;
Harish Chegondif24a6d42016-01-22 12:56:02 -08001261 ibp->rvp.n_other_naks++;
Ralph Campbellf9315512010-05-23 21:44:54 -07001262 goto class_b;
1263
1264 case 2: /* Remote Access Error */
1265 status = IB_WC_REM_ACCESS_ERR;
Harish Chegondif24a6d42016-01-22 12:56:02 -08001266 ibp->rvp.n_other_naks++;
Ralph Campbellf9315512010-05-23 21:44:54 -07001267 goto class_b;
1268
1269 case 3: /* Remote Operation Error */
1270 status = IB_WC_REM_OP_ERR;
Harish Chegondif24a6d42016-01-22 12:56:02 -08001271 ibp->rvp.n_other_naks++;
Ralph Campbellf9315512010-05-23 21:44:54 -07001272class_b:
1273 if (qp->s_last == qp->s_acked) {
1274 qib_send_complete(qp, wqe, status);
Harish Chegondi70696ea2016-02-03 14:20:27 -08001275 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Ralph Campbellf9315512010-05-23 21:44:54 -07001276 }
1277 break;
1278
1279 default:
1280 /* Ignore other reserved NAK error codes */
1281 goto reserved;
1282 }
1283 qp->s_retry = qp->s_retry_cnt;
1284 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1285 goto bail;
1286
1287 default: /* 2: reserved */
1288reserved:
1289 /* Ignore reserved NAK codes. */
1290 goto bail;
1291 }
1292
1293bail:
1294 return ret;
1295}
1296
1297/*
1298 * We have seen an out of sequence RDMA read middle or last packet.
1299 * This ACKs SENDs and RDMA writes up to the first RDMA read or atomic SWQE.
1300 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001301static void rdma_seq_err(struct rvt_qp *qp, struct qib_ibport *ibp, u32 psn,
Ralph Campbellf9315512010-05-23 21:44:54 -07001302 struct qib_ctxtdata *rcd)
1303{
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001304 struct rvt_swqe *wqe;
Ralph Campbellf9315512010-05-23 21:44:54 -07001305
1306 /* Remove QP from retry timer */
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001307 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
1308 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
Ralph Campbellf9315512010-05-23 21:44:54 -07001309 del_timer(&qp->s_timer);
1310 }
1311
Harish Chegondidb3ef0e2016-01-22 13:07:42 -08001312 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Ralph Campbellf9315512010-05-23 21:44:54 -07001313
1314 while (qib_cmp24(psn, wqe->lpsn) > 0) {
1315 if (wqe->wr.opcode == IB_WR_RDMA_READ ||
1316 wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1317 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
1318 break;
1319 wqe = do_rc_completion(qp, wqe, ibp);
1320 }
1321
Harish Chegondif24a6d42016-01-22 12:56:02 -08001322 ibp->rvp.n_rdma_seq++;
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001323 qp->r_flags |= RVT_R_RDMAR_SEQ;
Ralph Campbellf9315512010-05-23 21:44:54 -07001324 qib_restart_rc(qp, qp->s_last_psn + 1, 0);
1325 if (list_empty(&qp->rspwait)) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001326 qp->r_flags |= RVT_R_RSP_SEND;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001327 rvt_get_qp(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07001328 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1329 }
1330}
1331
1332/**
1333 * qib_rc_rcv_resp - process an incoming RC response packet
1334 * @ibp: the port this packet came in on
1335 * @ohdr: the other headers for this packet
1336 * @data: the packet data
1337 * @tlen: the packet length
1338 * @qp: the QP for this packet
1339 * @opcode: the opcode for this packet
1340 * @psn: the packet sequence number for this packet
1341 * @hdrsize: the header length
1342 * @pmtu: the path MTU
1343 *
1344 * This is called from qib_rc_rcv() to process an incoming RC response
1345 * packet for the given QP.
1346 * Called at interrupt level.
1347 */
1348static void qib_rc_rcv_resp(struct qib_ibport *ibp,
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001349 struct ib_other_headers *ohdr,
Ralph Campbellf9315512010-05-23 21:44:54 -07001350 void *data, u32 tlen,
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001351 struct rvt_qp *qp,
Ralph Campbellf9315512010-05-23 21:44:54 -07001352 u32 opcode,
1353 u32 psn, u32 hdrsize, u32 pmtu,
1354 struct qib_ctxtdata *rcd)
1355{
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001356 struct rvt_swqe *wqe;
Mike Marciniszyndd04e432011-01-10 17:42:22 -08001357 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
Ralph Campbellf9315512010-05-23 21:44:54 -07001358 enum ib_wc_status status;
1359 unsigned long flags;
1360 int diff;
1361 u32 pad;
1362 u32 aeth;
1363 u64 val;
1364
Mike Marciniszyndd04e432011-01-10 17:42:22 -08001365 if (opcode != OP(RDMA_READ_RESPONSE_MIDDLE)) {
1366 /*
1367 * If ACK'd PSN on SDMA busy list try to make progress to
1368 * reclaim SDMA credits.
1369 */
1370 if ((qib_cmp24(psn, qp->s_sending_psn) >= 0) &&
1371 (qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) <= 0)) {
1372
1373 /*
1374 * If send tasklet not running attempt to progress
1375 * SDMA queue.
1376 */
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001377 if (!(qp->s_flags & RVT_S_BUSY)) {
Mike Marciniszyndd04e432011-01-10 17:42:22 -08001378 /* Acquire SDMA Lock */
1379 spin_lock_irqsave(&ppd->sdma_lock, flags);
1380 /* Invoke sdma make progress */
1381 qib_sdma_make_progress(ppd);
1382 /* Release SDMA Lock */
1383 spin_unlock_irqrestore(&ppd->sdma_lock, flags);
1384 }
1385 }
1386 }
1387
Ralph Campbellf9315512010-05-23 21:44:54 -07001388 spin_lock_irqsave(&qp->s_lock, flags);
Harish Chegondidb3ef0e2016-01-22 13:07:42 -08001389 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
Mike Marciniszyn414ed902011-02-10 14:11:28 +00001390 goto ack_done;
Ralph Campbellf9315512010-05-23 21:44:54 -07001391
Ralph Campbellf9315512010-05-23 21:44:54 -07001392 /* Ignore invalid responses. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001393 smp_read_barrier_depends(); /* see post_one_send */
Mike Marciniszyneb04ff02017-02-08 05:26:08 -08001394 if (qib_cmp24(psn, READ_ONCE(qp->s_next_psn)) >= 0)
Ralph Campbellf9315512010-05-23 21:44:54 -07001395 goto ack_done;
1396
1397 /* Ignore duplicate responses. */
1398 diff = qib_cmp24(psn, qp->s_last_psn);
1399 if (unlikely(diff <= 0)) {
1400 /* Update credits for "ghost" ACKs */
1401 if (diff == 0 && opcode == OP(ACKNOWLEDGE)) {
1402 aeth = be32_to_cpu(ohdr->u.aeth);
1403 if ((aeth >> 29) == 0)
1404 qib_get_credit(qp, aeth);
1405 }
1406 goto ack_done;
1407 }
1408
1409 /*
1410 * Skip everything other than the PSN we expect, if we are waiting
1411 * for a reply to a restarted RDMA read or atomic op.
1412 */
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001413 if (qp->r_flags & RVT_R_RDMAR_SEQ) {
Ralph Campbellf9315512010-05-23 21:44:54 -07001414 if (qib_cmp24(psn, qp->s_last_psn + 1) != 0)
1415 goto ack_done;
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001416 qp->r_flags &= ~RVT_R_RDMAR_SEQ;
Ralph Campbellf9315512010-05-23 21:44:54 -07001417 }
1418
1419 if (unlikely(qp->s_acked == qp->s_tail))
1420 goto ack_done;
Harish Chegondidb3ef0e2016-01-22 13:07:42 -08001421 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Ralph Campbellf9315512010-05-23 21:44:54 -07001422 status = IB_WC_SUCCESS;
1423
1424 switch (opcode) {
1425 case OP(ACKNOWLEDGE):
1426 case OP(ATOMIC_ACKNOWLEDGE):
1427 case OP(RDMA_READ_RESPONSE_FIRST):
1428 aeth = be32_to_cpu(ohdr->u.aeth);
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001429 if (opcode == OP(ATOMIC_ACKNOWLEDGE))
1430 val = ib_u64_get(&ohdr->u.at.atomic_ack_eth);
1431 else
Ralph Campbellf9315512010-05-23 21:44:54 -07001432 val = 0;
1433 if (!do_rc_ack(qp, aeth, psn, opcode, val, rcd) ||
1434 opcode != OP(RDMA_READ_RESPONSE_FIRST))
1435 goto ack_done;
1436 hdrsize += 4;
Harish Chegondidb3ef0e2016-01-22 13:07:42 -08001437 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Ralph Campbellf9315512010-05-23 21:44:54 -07001438 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1439 goto ack_op_err;
1440 /*
1441 * If this is a response to a resent RDMA read, we
1442 * have to be careful to copy the data to the right
1443 * location.
1444 */
1445 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1446 wqe, psn, pmtu);
1447 goto read_middle;
1448
1449 case OP(RDMA_READ_RESPONSE_MIDDLE):
1450 /* no AETH, no ACK */
1451 if (unlikely(qib_cmp24(psn, qp->s_last_psn + 1)))
1452 goto ack_seq_err;
1453 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1454 goto ack_op_err;
1455read_middle:
1456 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1457 goto ack_len_err;
1458 if (unlikely(pmtu >= qp->s_rdma_read_len))
1459 goto ack_len_err;
1460
1461 /*
1462 * We got a response so update the timeout.
1463 * 4.096 usec. * (1 << qp->timeout)
1464 */
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001465 qp->s_flags |= RVT_S_TIMER;
Mike Marciniszynd0f2faf2011-09-23 13:16:49 -04001466 mod_timer(&qp->s_timer, jiffies + qp->timeout_jiffies);
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001467 if (qp->s_flags & RVT_S_WAIT_ACK) {
1468 qp->s_flags &= ~RVT_S_WAIT_ACK;
Ralph Campbellf9315512010-05-23 21:44:54 -07001469 qib_schedule_send(qp);
1470 }
1471
1472 if (opcode == OP(RDMA_READ_RESPONSE_MIDDLE))
1473 qp->s_retry = qp->s_retry_cnt;
1474
1475 /*
1476 * Update the RDMA receive state but do the copy w/o
1477 * holding the locks and blocking interrupts.
1478 */
1479 qp->s_rdma_read_len -= pmtu;
1480 update_last_psn(qp, psn);
1481 spin_unlock_irqrestore(&qp->s_lock, flags);
1482 qib_copy_sge(&qp->s_rdma_read_sge, data, pmtu, 0);
1483 goto bail;
1484
1485 case OP(RDMA_READ_RESPONSE_ONLY):
1486 aeth = be32_to_cpu(ohdr->u.aeth);
1487 if (!do_rc_ack(qp, aeth, psn, opcode, 0, rcd))
1488 goto ack_done;
1489 /* Get the number of bytes the message was padded by. */
1490 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1491 /*
1492 * Check that the data size is >= 0 && <= pmtu.
1493 * Remember to account for the AETH header (4) and
1494 * ICRC (4).
1495 */
1496 if (unlikely(tlen < (hdrsize + pad + 8)))
1497 goto ack_len_err;
1498 /*
1499 * If this is a response to a resent RDMA read, we
1500 * have to be careful to copy the data to the right
1501 * location.
1502 */
Harish Chegondidb3ef0e2016-01-22 13:07:42 -08001503 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Ralph Campbellf9315512010-05-23 21:44:54 -07001504 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1505 wqe, psn, pmtu);
1506 goto read_last;
1507
1508 case OP(RDMA_READ_RESPONSE_LAST):
1509 /* ACKs READ req. */
1510 if (unlikely(qib_cmp24(psn, qp->s_last_psn + 1)))
1511 goto ack_seq_err;
1512 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1513 goto ack_op_err;
1514 /* Get the number of bytes the message was padded by. */
1515 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1516 /*
1517 * Check that the data size is >= 1 && <= pmtu.
1518 * Remember to account for the AETH header (4) and
1519 * ICRC (4).
1520 */
1521 if (unlikely(tlen <= (hdrsize + pad + 8)))
1522 goto ack_len_err;
1523read_last:
1524 tlen -= hdrsize + pad + 8;
1525 if (unlikely(tlen != qp->s_rdma_read_len))
1526 goto ack_len_err;
1527 aeth = be32_to_cpu(ohdr->u.aeth);
1528 qib_copy_sge(&qp->s_rdma_read_sge, data, tlen, 0);
1529 WARN_ON(qp->s_rdma_read_sge.num_sge);
1530 (void) do_rc_ack(qp, aeth, psn,
1531 OP(RDMA_READ_RESPONSE_LAST), 0, rcd);
1532 goto ack_done;
1533 }
1534
1535ack_op_err:
1536 status = IB_WC_LOC_QP_OP_ERR;
1537 goto ack_err;
1538
1539ack_seq_err:
1540 rdma_seq_err(qp, ibp, psn, rcd);
1541 goto ack_done;
1542
1543ack_len_err:
1544 status = IB_WC_LOC_LEN_ERR;
1545ack_err:
1546 if (qp->s_last == qp->s_acked) {
1547 qib_send_complete(qp, wqe, status);
Harish Chegondi70696ea2016-02-03 14:20:27 -08001548 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Ralph Campbellf9315512010-05-23 21:44:54 -07001549 }
1550ack_done:
1551 spin_unlock_irqrestore(&qp->s_lock, flags);
1552bail:
1553 return;
1554}
1555
1556/**
1557 * qib_rc_rcv_error - process an incoming duplicate or error RC packet
1558 * @ohdr: the other headers for this packet
1559 * @data: the packet data
1560 * @qp: the QP for this packet
1561 * @opcode: the opcode for this packet
1562 * @psn: the packet sequence number for this packet
1563 * @diff: the difference between the PSN and the expected PSN
1564 *
1565 * This is called from qib_rc_rcv() to process an unexpected
1566 * incoming RC packet for the given QP.
1567 * Called at interrupt level.
1568 * Return 1 if no more processing is needed; otherwise return 0 to
1569 * schedule a response to be sent.
1570 */
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001571static int qib_rc_rcv_error(struct ib_other_headers *ohdr,
Ralph Campbellf9315512010-05-23 21:44:54 -07001572 void *data,
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001573 struct rvt_qp *qp,
Ralph Campbellf9315512010-05-23 21:44:54 -07001574 u32 opcode,
1575 u32 psn,
1576 int diff,
1577 struct qib_ctxtdata *rcd)
1578{
1579 struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001580 struct rvt_ack_entry *e;
Ralph Campbellf9315512010-05-23 21:44:54 -07001581 unsigned long flags;
1582 u8 i, prev;
1583 int old_req;
1584
1585 if (diff > 0) {
1586 /*
1587 * Packet sequence error.
1588 * A NAK will ACK earlier sends and RDMA writes.
1589 * Don't queue the NAK if we already sent one.
1590 */
1591 if (!qp->r_nak_state) {
Harish Chegondif24a6d42016-01-22 12:56:02 -08001592 ibp->rvp.n_rc_seqnak++;
Ralph Campbellf9315512010-05-23 21:44:54 -07001593 qp->r_nak_state = IB_NAK_PSN_ERROR;
1594 /* Use the expected PSN. */
1595 qp->r_ack_psn = qp->r_psn;
1596 /*
1597 * Wait to send the sequence NAK until all packets
1598 * in the receive queue have been processed.
1599 * Otherwise, we end up propagating congestion.
1600 */
1601 if (list_empty(&qp->rspwait)) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001602 qp->r_flags |= RVT_R_RSP_NAK;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001603 rvt_get_qp(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07001604 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1605 }
1606 }
1607 goto done;
1608 }
1609
1610 /*
1611 * Handle a duplicate request. Don't re-execute SEND, RDMA
1612 * write or atomic op. Don't NAK errors, just silently drop
1613 * the duplicate request. Note that r_sge, r_len, and
1614 * r_rcv_len may be in use so don't modify them.
1615 *
1616 * We are supposed to ACK the earliest duplicate PSN but we
1617 * can coalesce an outstanding duplicate ACK. We have to
1618 * send the earliest so that RDMA reads can be restarted at
1619 * the requester's expected PSN.
1620 *
1621 * First, find where this duplicate PSN falls within the
1622 * ACKs previously sent.
1623 * old_req is true if there is an older response that is scheduled
1624 * to be sent before sending this one.
1625 */
1626 e = NULL;
1627 old_req = 1;
Harish Chegondif24a6d42016-01-22 12:56:02 -08001628 ibp->rvp.n_rc_dupreq++;
Ralph Campbellf9315512010-05-23 21:44:54 -07001629
1630 spin_lock_irqsave(&qp->s_lock, flags);
Ralph Campbellf9315512010-05-23 21:44:54 -07001631
1632 for (i = qp->r_head_ack_queue; ; i = prev) {
1633 if (i == qp->s_tail_ack_queue)
1634 old_req = 0;
1635 if (i)
1636 prev = i - 1;
1637 else
1638 prev = QIB_MAX_RDMA_ATOMIC;
1639 if (prev == qp->r_head_ack_queue) {
1640 e = NULL;
1641 break;
1642 }
1643 e = &qp->s_ack_queue[prev];
1644 if (!e->opcode) {
1645 e = NULL;
1646 break;
1647 }
1648 if (qib_cmp24(psn, e->psn) >= 0) {
1649 if (prev == qp->s_tail_ack_queue &&
1650 qib_cmp24(psn, e->lpsn) <= 0)
1651 old_req = 0;
1652 break;
1653 }
1654 }
1655 switch (opcode) {
1656 case OP(RDMA_READ_REQUEST): {
1657 struct ib_reth *reth;
1658 u32 offset;
1659 u32 len;
1660
1661 /*
1662 * If we didn't find the RDMA read request in the ack queue,
1663 * we can ignore this request.
1664 */
1665 if (!e || e->opcode != OP(RDMA_READ_REQUEST))
1666 goto unlock_done;
1667 /* RETH comes after BTH */
1668 reth = &ohdr->u.rc.reth;
1669 /*
1670 * Address range must be a subset of the original
1671 * request and start on pmtu boundaries.
1672 * We reuse the old ack_queue slot since the requester
1673 * should not back up and request an earlier PSN for the
1674 * same request.
1675 */
1676 offset = ((psn - e->psn) & QIB_PSN_MASK) *
Mike Marciniszyncc6ea132011-09-23 13:16:34 -04001677 qp->pmtu;
Ralph Campbellf9315512010-05-23 21:44:54 -07001678 len = be32_to_cpu(reth->length);
1679 if (unlikely(offset + len != e->rdma_sge.sge_length))
1680 goto unlock_done;
1681 if (e->rdma_sge.mr) {
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001682 rvt_put_mr(e->rdma_sge.mr);
Ralph Campbellf9315512010-05-23 21:44:54 -07001683 e->rdma_sge.mr = NULL;
1684 }
1685 if (len != 0) {
1686 u32 rkey = be32_to_cpu(reth->rkey);
1687 u64 vaddr = be64_to_cpu(reth->vaddr);
1688 int ok;
1689
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001690 ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr, rkey,
Ralph Campbellf9315512010-05-23 21:44:54 -07001691 IB_ACCESS_REMOTE_READ);
1692 if (unlikely(!ok))
1693 goto unlock_done;
1694 } else {
1695 e->rdma_sge.vaddr = NULL;
1696 e->rdma_sge.length = 0;
1697 e->rdma_sge.sge_length = 0;
1698 }
1699 e->psn = psn;
1700 if (old_req)
1701 goto unlock_done;
1702 qp->s_tail_ack_queue = prev;
1703 break;
1704 }
1705
1706 case OP(COMPARE_SWAP):
1707 case OP(FETCH_ADD): {
1708 /*
1709 * If we didn't find the atomic request in the ack queue
1710 * or the send tasklet is already backed up to send an
1711 * earlier entry, we can ignore this request.
1712 */
1713 if (!e || e->opcode != (u8) opcode || old_req)
1714 goto unlock_done;
1715 qp->s_tail_ack_queue = prev;
1716 break;
1717 }
1718
1719 default:
1720 /*
1721 * Ignore this operation if it doesn't request an ACK
1722 * or an earlier RDMA read or atomic is going to be resent.
1723 */
1724 if (!(psn & IB_BTH_REQ_ACK) || old_req)
1725 goto unlock_done;
1726 /*
1727 * Resend the most recent ACK if this request is
1728 * after all the previous RDMA reads and atomics.
1729 */
1730 if (i == qp->r_head_ack_queue) {
1731 spin_unlock_irqrestore(&qp->s_lock, flags);
1732 qp->r_nak_state = 0;
1733 qp->r_ack_psn = qp->r_psn - 1;
1734 goto send_ack;
1735 }
1736 /*
1737 * Try to send a simple ACK to work around a Mellanox bug
1738 * which doesn't accept a RDMA read response or atomic
1739 * response as an ACK for earlier SENDs or RDMA writes.
1740 */
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001741 if (!(qp->s_flags & RVT_S_RESP_PENDING)) {
Ralph Campbellf9315512010-05-23 21:44:54 -07001742 spin_unlock_irqrestore(&qp->s_lock, flags);
1743 qp->r_nak_state = 0;
1744 qp->r_ack_psn = qp->s_ack_queue[i].psn - 1;
1745 goto send_ack;
1746 }
1747 /*
1748 * Resend the RDMA read or atomic op which
1749 * ACKs this duplicate request.
1750 */
1751 qp->s_tail_ack_queue = i;
1752 break;
1753 }
1754 qp->s_ack_state = OP(ACKNOWLEDGE);
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001755 qp->s_flags |= RVT_S_RESP_PENDING;
Ralph Campbellf9315512010-05-23 21:44:54 -07001756 qp->r_nak_state = 0;
1757 qib_schedule_send(qp);
1758
1759unlock_done:
1760 spin_unlock_irqrestore(&qp->s_lock, flags);
1761done:
1762 return 1;
1763
1764send_ack:
1765 return 0;
1766}
1767
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001768static inline void qib_update_ack_queue(struct rvt_qp *qp, unsigned n)
Ralph Campbellf9315512010-05-23 21:44:54 -07001769{
1770 unsigned next;
1771
1772 next = n + 1;
1773 if (next > QIB_MAX_RDMA_ATOMIC)
1774 next = 0;
1775 qp->s_tail_ack_queue = next;
1776 qp->s_ack_state = OP(ACKNOWLEDGE);
1777}
1778
1779/**
1780 * qib_rc_rcv - process an incoming RC packet
1781 * @rcd: the context pointer
1782 * @hdr: the header of this packet
1783 * @has_grh: true if the header has a GRH
1784 * @data: the packet data
1785 * @tlen: the packet length
1786 * @qp: the QP for this packet
1787 *
1788 * This is called from qib_qp_rcv() to process an incoming RC packet
1789 * for the given QP.
1790 * Called at interrupt level.
1791 */
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001792void qib_rc_rcv(struct qib_ctxtdata *rcd, struct ib_header *hdr,
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001793 int has_grh, void *data, u32 tlen, struct rvt_qp *qp)
Ralph Campbellf9315512010-05-23 21:44:54 -07001794{
1795 struct qib_ibport *ibp = &rcd->ppd->ibport_data;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001796 struct ib_other_headers *ohdr;
Ralph Campbellf9315512010-05-23 21:44:54 -07001797 u32 opcode;
1798 u32 hdrsize;
1799 u32 psn;
1800 u32 pad;
1801 struct ib_wc wc;
Mike Marciniszyncc6ea132011-09-23 13:16:34 -04001802 u32 pmtu = qp->pmtu;
Ralph Campbellf9315512010-05-23 21:44:54 -07001803 int diff;
1804 struct ib_reth *reth;
1805 unsigned long flags;
1806 int ret;
1807
1808 /* Check for GRH */
1809 if (!has_grh) {
1810 ohdr = &hdr->u.oth;
1811 hdrsize = 8 + 12; /* LRH + BTH */
1812 } else {
1813 ohdr = &hdr->u.l.oth;
1814 hdrsize = 8 + 40 + 12; /* LRH + GRH + BTH */
1815 }
1816
1817 opcode = be32_to_cpu(ohdr->bth[0]);
Ralph Campbellf9315512010-05-23 21:44:54 -07001818 if (qib_ruc_check_hdr(ibp, hdr, has_grh, qp, opcode))
Mike Marciniszyn9fd54732011-09-23 13:17:00 -04001819 return;
Ralph Campbellf9315512010-05-23 21:44:54 -07001820
1821 psn = be32_to_cpu(ohdr->bth[2]);
1822 opcode >>= 24;
1823
Ralph Campbellf9315512010-05-23 21:44:54 -07001824 /*
1825 * Process responses (ACKs) before anything else. Note that the
1826 * packet sequence number will be for something in the send work
1827 * queue rather than the expected receive packet sequence number.
1828 * In other words, this QP is the requester.
1829 */
1830 if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
1831 opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
1832 qib_rc_rcv_resp(ibp, ohdr, data, tlen, qp, opcode, psn,
1833 hdrsize, pmtu, rcd);
Ralph Campbella5210c12010-08-02 22:39:30 +00001834 return;
Ralph Campbellf9315512010-05-23 21:44:54 -07001835 }
1836
1837 /* Compute 24 bits worth of difference. */
1838 diff = qib_cmp24(psn, qp->r_psn);
1839 if (unlikely(diff)) {
1840 if (qib_rc_rcv_error(ohdr, data, qp, opcode, psn, diff, rcd))
Ralph Campbella5210c12010-08-02 22:39:30 +00001841 return;
Ralph Campbellf9315512010-05-23 21:44:54 -07001842 goto send_ack;
1843 }
1844
1845 /* Check for opcode sequence errors. */
1846 switch (qp->r_state) {
1847 case OP(SEND_FIRST):
1848 case OP(SEND_MIDDLE):
1849 if (opcode == OP(SEND_MIDDLE) ||
1850 opcode == OP(SEND_LAST) ||
1851 opcode == OP(SEND_LAST_WITH_IMMEDIATE))
1852 break;
1853 goto nack_inv;
1854
1855 case OP(RDMA_WRITE_FIRST):
1856 case OP(RDMA_WRITE_MIDDLE):
1857 if (opcode == OP(RDMA_WRITE_MIDDLE) ||
1858 opcode == OP(RDMA_WRITE_LAST) ||
1859 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
1860 break;
1861 goto nack_inv;
1862
1863 default:
1864 if (opcode == OP(SEND_MIDDLE) ||
1865 opcode == OP(SEND_LAST) ||
1866 opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
1867 opcode == OP(RDMA_WRITE_MIDDLE) ||
1868 opcode == OP(RDMA_WRITE_LAST) ||
1869 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
1870 goto nack_inv;
1871 /*
1872 * Note that it is up to the requester to not send a new
1873 * RDMA read or atomic operation before receiving an ACK
1874 * for the previous operation.
1875 */
1876 break;
1877 }
1878
Brian Weltybeb5a042017-02-08 05:27:01 -08001879 if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
1880 rvt_comm_est(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07001881
1882 /* OK, process the packet. */
1883 switch (opcode) {
1884 case OP(SEND_FIRST):
1885 ret = qib_get_rwqe(qp, 0);
1886 if (ret < 0)
1887 goto nack_op_err;
1888 if (!ret)
1889 goto rnr_nak;
1890 qp->r_rcv_len = 0;
1891 /* FALLTHROUGH */
1892 case OP(SEND_MIDDLE):
1893 case OP(RDMA_WRITE_MIDDLE):
1894send_middle:
1895 /* Check for invalid length PMTU or posted rwqe len. */
1896 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1897 goto nack_inv;
1898 qp->r_rcv_len += pmtu;
1899 if (unlikely(qp->r_rcv_len > qp->r_len))
1900 goto nack_inv;
1901 qib_copy_sge(&qp->r_sge, data, pmtu, 1);
1902 break;
1903
1904 case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
1905 /* consume RWQE */
1906 ret = qib_get_rwqe(qp, 1);
1907 if (ret < 0)
1908 goto nack_op_err;
1909 if (!ret)
1910 goto rnr_nak;
1911 goto send_last_imm;
1912
1913 case OP(SEND_ONLY):
1914 case OP(SEND_ONLY_WITH_IMMEDIATE):
1915 ret = qib_get_rwqe(qp, 0);
1916 if (ret < 0)
1917 goto nack_op_err;
1918 if (!ret)
1919 goto rnr_nak;
1920 qp->r_rcv_len = 0;
1921 if (opcode == OP(SEND_ONLY))
Mike Marciniszyn2fc109c2011-09-23 13:16:29 -04001922 goto no_immediate_data;
1923 /* FALLTHROUGH for SEND_ONLY_WITH_IMMEDIATE */
Ralph Campbellf9315512010-05-23 21:44:54 -07001924 case OP(SEND_LAST_WITH_IMMEDIATE):
1925send_last_imm:
1926 wc.ex.imm_data = ohdr->u.imm_data;
1927 hdrsize += 4;
1928 wc.wc_flags = IB_WC_WITH_IMM;
Mike Marciniszyn2fc109c2011-09-23 13:16:29 -04001929 goto send_last;
Ralph Campbellf9315512010-05-23 21:44:54 -07001930 case OP(SEND_LAST):
1931 case OP(RDMA_WRITE_LAST):
Mike Marciniszyn2fc109c2011-09-23 13:16:29 -04001932no_immediate_data:
1933 wc.wc_flags = 0;
1934 wc.ex.imm_data = 0;
Ralph Campbellf9315512010-05-23 21:44:54 -07001935send_last:
1936 /* Get the number of bytes the message was padded by. */
1937 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1938 /* Check for invalid length. */
1939 /* XXX LAST len should be >= 1 */
1940 if (unlikely(tlen < (hdrsize + pad + 4)))
1941 goto nack_inv;
1942 /* Don't count the CRC. */
1943 tlen -= (hdrsize + pad + 4);
1944 wc.byte_len = tlen + qp->r_rcv_len;
1945 if (unlikely(wc.byte_len > qp->r_len))
1946 goto nack_inv;
1947 qib_copy_sge(&qp->r_sge, data, tlen, 1);
Harish Chegondi70696ea2016-02-03 14:20:27 -08001948 rvt_put_ss(&qp->r_sge);
Ralph Campbellf9315512010-05-23 21:44:54 -07001949 qp->r_msn++;
Harish Chegondi01ba79d2016-01-22 12:56:46 -08001950 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
Ralph Campbellf9315512010-05-23 21:44:54 -07001951 break;
1952 wc.wr_id = qp->r_wr_id;
1953 wc.status = IB_WC_SUCCESS;
1954 if (opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE) ||
1955 opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
1956 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
1957 else
1958 wc.opcode = IB_WC_RECV;
1959 wc.qp = &qp->ibqp;
1960 wc.src_qp = qp->remote_qpn;
1961 wc.slid = qp->remote_ah_attr.dlid;
1962 wc.sl = qp->remote_ah_attr.sl;
Mike Marciniszyn2fc109c2011-09-23 13:16:29 -04001963 /* zero fields that are N/A */
1964 wc.vendor_err = 0;
1965 wc.pkey_index = 0;
1966 wc.dlid_path_bits = 0;
1967 wc.port_num = 0;
Ralph Campbellf9315512010-05-23 21:44:54 -07001968 /* Signal completion event if the solicited bit is set. */
Harish Chegondi4bb88e52016-01-22 13:07:36 -08001969 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
Ralph Campbellf9315512010-05-23 21:44:54 -07001970 (ohdr->bth[0] &
1971 cpu_to_be32(IB_BTH_SOLICITED)) != 0);
1972 break;
1973
1974 case OP(RDMA_WRITE_FIRST):
1975 case OP(RDMA_WRITE_ONLY):
1976 case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
1977 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
1978 goto nack_inv;
1979 /* consume RWQE */
1980 reth = &ohdr->u.rc.reth;
1981 hdrsize += sizeof(*reth);
1982 qp->r_len = be32_to_cpu(reth->length);
1983 qp->r_rcv_len = 0;
1984 qp->r_sge.sg_list = NULL;
1985 if (qp->r_len != 0) {
1986 u32 rkey = be32_to_cpu(reth->rkey);
1987 u64 vaddr = be64_to_cpu(reth->vaddr);
1988 int ok;
1989
1990 /* Check rkey & NAK */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08001991 ok = rvt_rkey_ok(qp, &qp->r_sge.sge, qp->r_len, vaddr,
Ralph Campbellf9315512010-05-23 21:44:54 -07001992 rkey, IB_ACCESS_REMOTE_WRITE);
1993 if (unlikely(!ok))
1994 goto nack_acc;
1995 qp->r_sge.num_sge = 1;
1996 } else {
1997 qp->r_sge.num_sge = 0;
1998 qp->r_sge.sge.mr = NULL;
1999 qp->r_sge.sge.vaddr = NULL;
2000 qp->r_sge.sge.length = 0;
2001 qp->r_sge.sge.sge_length = 0;
2002 }
2003 if (opcode == OP(RDMA_WRITE_FIRST))
2004 goto send_middle;
2005 else if (opcode == OP(RDMA_WRITE_ONLY))
Mike Marciniszyn2fc109c2011-09-23 13:16:29 -04002006 goto no_immediate_data;
Ralph Campbellf9315512010-05-23 21:44:54 -07002007 ret = qib_get_rwqe(qp, 1);
2008 if (ret < 0)
2009 goto nack_op_err;
2010 if (!ret)
2011 goto rnr_nak;
Jason Gunthorpe5715f5d2010-10-22 22:00:48 +00002012 wc.ex.imm_data = ohdr->u.rc.imm_data;
2013 hdrsize += 4;
2014 wc.wc_flags = IB_WC_WITH_IMM;
2015 goto send_last;
Ralph Campbellf9315512010-05-23 21:44:54 -07002016
2017 case OP(RDMA_READ_REQUEST): {
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08002018 struct rvt_ack_entry *e;
Ralph Campbellf9315512010-05-23 21:44:54 -07002019 u32 len;
2020 u8 next;
2021
2022 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
2023 goto nack_inv;
2024 next = qp->r_head_ack_queue + 1;
2025 /* s_ack_queue is size QIB_MAX_RDMA_ATOMIC+1 so use > not >= */
2026 if (next > QIB_MAX_RDMA_ATOMIC)
2027 next = 0;
2028 spin_lock_irqsave(&qp->s_lock, flags);
Ralph Campbellf9315512010-05-23 21:44:54 -07002029 if (unlikely(next == qp->s_tail_ack_queue)) {
2030 if (!qp->s_ack_queue[next].sent)
2031 goto nack_inv_unlck;
2032 qib_update_ack_queue(qp, next);
2033 }
2034 e = &qp->s_ack_queue[qp->r_head_ack_queue];
2035 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08002036 rvt_put_mr(e->rdma_sge.mr);
Ralph Campbellf9315512010-05-23 21:44:54 -07002037 e->rdma_sge.mr = NULL;
2038 }
2039 reth = &ohdr->u.rc.reth;
2040 len = be32_to_cpu(reth->length);
2041 if (len) {
2042 u32 rkey = be32_to_cpu(reth->rkey);
2043 u64 vaddr = be64_to_cpu(reth->vaddr);
2044 int ok;
2045
2046 /* Check rkey & NAK */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08002047 ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr,
Ralph Campbellf9315512010-05-23 21:44:54 -07002048 rkey, IB_ACCESS_REMOTE_READ);
2049 if (unlikely(!ok))
2050 goto nack_acc_unlck;
2051 /*
2052 * Update the next expected PSN. We add 1 later
2053 * below, so only add the remainder here.
2054 */
Mike Marciniszyn5dc80602016-12-07 19:34:37 -08002055 qp->r_psn += rvt_div_mtu(qp, len - 1);
Ralph Campbellf9315512010-05-23 21:44:54 -07002056 } else {
2057 e->rdma_sge.mr = NULL;
2058 e->rdma_sge.vaddr = NULL;
2059 e->rdma_sge.length = 0;
2060 e->rdma_sge.sge_length = 0;
2061 }
2062 e->opcode = opcode;
2063 e->sent = 0;
2064 e->psn = psn;
2065 e->lpsn = qp->r_psn;
2066 /*
2067 * We need to increment the MSN here instead of when we
2068 * finish sending the result since a duplicate request would
2069 * increment it more than once.
2070 */
2071 qp->r_msn++;
2072 qp->r_psn++;
2073 qp->r_state = opcode;
2074 qp->r_nak_state = 0;
2075 qp->r_head_ack_queue = next;
2076
2077 /* Schedule the send tasklet. */
Harish Chegondi01ba79d2016-01-22 12:56:46 -08002078 qp->s_flags |= RVT_S_RESP_PENDING;
Ralph Campbellf9315512010-05-23 21:44:54 -07002079 qib_schedule_send(qp);
2080
Ralph Campbella5210c12010-08-02 22:39:30 +00002081 goto sunlock;
Ralph Campbellf9315512010-05-23 21:44:54 -07002082 }
2083
2084 case OP(COMPARE_SWAP):
2085 case OP(FETCH_ADD): {
2086 struct ib_atomic_eth *ateth;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08002087 struct rvt_ack_entry *e;
Ralph Campbellf9315512010-05-23 21:44:54 -07002088 u64 vaddr;
2089 atomic64_t *maddr;
2090 u64 sdata;
2091 u32 rkey;
2092 u8 next;
2093
2094 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
2095 goto nack_inv;
2096 next = qp->r_head_ack_queue + 1;
2097 if (next > QIB_MAX_RDMA_ATOMIC)
2098 next = 0;
2099 spin_lock_irqsave(&qp->s_lock, flags);
Ralph Campbellf9315512010-05-23 21:44:54 -07002100 if (unlikely(next == qp->s_tail_ack_queue)) {
2101 if (!qp->s_ack_queue[next].sent)
2102 goto nack_inv_unlck;
2103 qib_update_ack_queue(qp, next);
2104 }
2105 e = &qp->s_ack_queue[qp->r_head_ack_queue];
2106 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08002107 rvt_put_mr(e->rdma_sge.mr);
Ralph Campbellf9315512010-05-23 21:44:54 -07002108 e->rdma_sge.mr = NULL;
2109 }
2110 ateth = &ohdr->u.atomic_eth;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002111 vaddr = get_ib_ateth_vaddr(ateth);
Ralph Campbellf9315512010-05-23 21:44:54 -07002112 if (unlikely(vaddr & (sizeof(u64) - 1)))
2113 goto nack_inv_unlck;
2114 rkey = be32_to_cpu(ateth->rkey);
2115 /* Check rkey & NAK */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08002116 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
Ralph Campbellf9315512010-05-23 21:44:54 -07002117 vaddr, rkey,
2118 IB_ACCESS_REMOTE_ATOMIC)))
2119 goto nack_acc_unlck;
2120 /* Perform atomic OP and save result. */
2121 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002122 sdata = get_ib_ateth_swap(ateth);
Ralph Campbellf9315512010-05-23 21:44:54 -07002123 e->atomic_data = (opcode == OP(FETCH_ADD)) ?
2124 (u64) atomic64_add_return(sdata, maddr) - sdata :
2125 (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002126 get_ib_ateth_compare(ateth),
Ralph Campbellf9315512010-05-23 21:44:54 -07002127 sdata);
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -08002128 rvt_put_mr(qp->r_sge.sge.mr);
Ralph Campbellf9315512010-05-23 21:44:54 -07002129 qp->r_sge.num_sge = 0;
2130 e->opcode = opcode;
2131 e->sent = 0;
2132 e->psn = psn;
2133 e->lpsn = psn;
2134 qp->r_msn++;
2135 qp->r_psn++;
2136 qp->r_state = opcode;
2137 qp->r_nak_state = 0;
2138 qp->r_head_ack_queue = next;
2139
2140 /* Schedule the send tasklet. */
Harish Chegondi01ba79d2016-01-22 12:56:46 -08002141 qp->s_flags |= RVT_S_RESP_PENDING;
Ralph Campbellf9315512010-05-23 21:44:54 -07002142 qib_schedule_send(qp);
2143
Ralph Campbella5210c12010-08-02 22:39:30 +00002144 goto sunlock;
Ralph Campbellf9315512010-05-23 21:44:54 -07002145 }
2146
2147 default:
2148 /* NAK unknown opcodes. */
2149 goto nack_inv;
2150 }
2151 qp->r_psn++;
2152 qp->r_state = opcode;
2153 qp->r_ack_psn = psn;
2154 qp->r_nak_state = 0;
2155 /* Send an ACK if requested or required. */
2156 if (psn & (1 << 31))
2157 goto send_ack;
Ralph Campbella5210c12010-08-02 22:39:30 +00002158 return;
Ralph Campbellf9315512010-05-23 21:44:54 -07002159
2160rnr_nak:
2161 qp->r_nak_state = IB_RNR_NAK | qp->r_min_rnr_timer;
2162 qp->r_ack_psn = qp->r_psn;
2163 /* Queue RNR NAK for later */
2164 if (list_empty(&qp->rspwait)) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -08002165 qp->r_flags |= RVT_R_RSP_NAK;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07002166 rvt_get_qp(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07002167 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
2168 }
Ralph Campbella5210c12010-08-02 22:39:30 +00002169 return;
Ralph Campbellf9315512010-05-23 21:44:54 -07002170
2171nack_op_err:
Brian Weltybeb5a042017-02-08 05:27:01 -08002172 rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
Ralph Campbellf9315512010-05-23 21:44:54 -07002173 qp->r_nak_state = IB_NAK_REMOTE_OPERATIONAL_ERROR;
2174 qp->r_ack_psn = qp->r_psn;
2175 /* Queue NAK for later */
2176 if (list_empty(&qp->rspwait)) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -08002177 qp->r_flags |= RVT_R_RSP_NAK;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07002178 rvt_get_qp(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07002179 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
2180 }
Ralph Campbella5210c12010-08-02 22:39:30 +00002181 return;
Ralph Campbellf9315512010-05-23 21:44:54 -07002182
2183nack_inv_unlck:
2184 spin_unlock_irqrestore(&qp->s_lock, flags);
2185nack_inv:
Brian Weltybeb5a042017-02-08 05:27:01 -08002186 rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
Ralph Campbellf9315512010-05-23 21:44:54 -07002187 qp->r_nak_state = IB_NAK_INVALID_REQUEST;
2188 qp->r_ack_psn = qp->r_psn;
2189 /* Queue NAK for later */
2190 if (list_empty(&qp->rspwait)) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -08002191 qp->r_flags |= RVT_R_RSP_NAK;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07002192 rvt_get_qp(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07002193 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
2194 }
Ralph Campbella5210c12010-08-02 22:39:30 +00002195 return;
Ralph Campbellf9315512010-05-23 21:44:54 -07002196
2197nack_acc_unlck:
2198 spin_unlock_irqrestore(&qp->s_lock, flags);
2199nack_acc:
Brian Weltybeb5a042017-02-08 05:27:01 -08002200 rvt_rc_error(qp, IB_WC_LOC_PROT_ERR);
Ralph Campbellf9315512010-05-23 21:44:54 -07002201 qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
2202 qp->r_ack_psn = qp->r_psn;
2203send_ack:
2204 qib_send_rc_ack(qp);
Ralph Campbellf9315512010-05-23 21:44:54 -07002205 return;
2206
2207sunlock:
2208 spin_unlock_irqrestore(&qp->s_lock, flags);
2209}