blob: 9f7900f1562771df5485a38a25bdf2b4f16cc842 [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
Jubin John05d6ac12016-02-14 20:22:17 -08002 * Copyright(c) 2015, 2016 Intel Corporation.
Mike Marciniszyn77241052015-07-30 15:17:43 -04003 *
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
Mike Marciniszyn77241052015-07-30 15:17:43 -04009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
Mike Marciniszyn77241052015-07-30 15:17:43 -040020 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
47
48#include <linux/io.h>
Dennis Dalessandroec4274f2016-01-19 14:43:44 -080049#include <rdma/rdma_vt.h>
50#include <rdma/rdmavt_qp.h>
Mike Marciniszyn77241052015-07-30 15:17:43 -040051
52#include "hfi.h"
53#include "qp.h"
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -080054#include "verbs_txreq.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040055#include "trace.h"
56
57/* cut down ridiculously long IB macro names */
58#define OP(x) IB_OPCODE_RC_##x
59
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080060/**
61 * hfi1_add_retry_timer - add/start a retry timer
62 * @qp - the QP
63 *
64 * add a retry timer on the QP
65 */
66static inline void hfi1_add_retry_timer(struct rvt_qp *qp)
67{
Vennila Megavannanbfee5e32016-02-09 14:29:49 -080068 struct ib_qp *ibqp = &qp->ibqp;
69 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
70
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080071 qp->s_flags |= RVT_S_TIMER;
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080072 /* 4.096 usec. * (1 << qp->timeout) */
Vennila Megavannanbfee5e32016-02-09 14:29:49 -080073 qp->s_timer.expires = jiffies + qp->timeout_jiffies +
74 rdi->busy_jiffies;
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080075 add_timer(&qp->s_timer);
76}
77
78/**
79 * hfi1_add_rnr_timer - add/start an rnr timer
80 * @qp - the QP
81 * @to - timeout in usecs
82 *
83 * add an rnr timer on the QP
84 */
Mike Marciniszyn34cee282016-02-09 14:29:31 -080085void hfi1_add_rnr_timer(struct rvt_qp *qp, u32 to)
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080086{
Mike Marciniszyn08279d52016-02-04 10:59:36 -080087 struct hfi1_qp_priv *priv = qp->priv;
88
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080089 qp->s_flags |= RVT_S_WAIT_RNR;
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080090 qp->s_timer.expires = jiffies + usecs_to_jiffies(to);
Mike Marciniszyn08279d52016-02-04 10:59:36 -080091 add_timer(&priv->s_rnr_timer);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080092}
93
94/**
95 * hfi1_mod_retry_timer - mod a retry timer
96 * @qp - the QP
97 *
98 * Modify a potentially already running retry
99 * timer
100 */
101static inline void hfi1_mod_retry_timer(struct rvt_qp *qp)
102{
Vennila Megavannanbfee5e32016-02-09 14:29:49 -0800103 struct ib_qp *ibqp = &qp->ibqp;
104 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
105
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800106 qp->s_flags |= RVT_S_TIMER;
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800107 /* 4.096 usec. * (1 << qp->timeout) */
Vennila Megavannanbfee5e32016-02-09 14:29:49 -0800108 mod_timer(&qp->s_timer, jiffies + qp->timeout_jiffies +
109 rdi->busy_jiffies);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800110}
111
112/**
113 * hfi1_stop_retry_timer - stop a retry timer
114 * @qp - the QP
115 *
116 * stop a retry timer and return if the timer
117 * had been pending.
118 */
119static inline int hfi1_stop_retry_timer(struct rvt_qp *qp)
120{
121 int rval = 0;
122
123 /* Remove QP from retry */
124 if (qp->s_flags & RVT_S_TIMER) {
125 qp->s_flags &= ~RVT_S_TIMER;
126 rval = del_timer(&qp->s_timer);
127 }
128 return rval;
129}
130
131/**
132 * hfi1_stop_rc_timers - stop all timers
133 * @qp - the QP
134 *
135 * stop any pending timers
136 */
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800137void hfi1_stop_rc_timers(struct rvt_qp *qp)
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800138{
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800139 struct hfi1_qp_priv *priv = qp->priv;
140
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800141 /* Remove QP from all timers */
142 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
143 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
144 del_timer(&qp->s_timer);
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800145 del_timer(&priv->s_rnr_timer);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800146 }
147}
148
149/**
150 * hfi1_stop_rnr_timer - stop an rnr timer
151 * @qp - the QP
152 *
153 * stop an rnr timer and return if the timer
154 * had been pending.
155 */
156static inline int hfi1_stop_rnr_timer(struct rvt_qp *qp)
157{
158 int rval = 0;
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800159 struct hfi1_qp_priv *priv = qp->priv;
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800160
161 /* Remove QP from rnr timer */
162 if (qp->s_flags & RVT_S_WAIT_RNR) {
163 qp->s_flags &= ~RVT_S_WAIT_RNR;
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800164 rval = del_timer(&priv->s_rnr_timer);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800165 }
166 return rval;
167}
168
169/**
170 * hfi1_del_timers_sync - wait for any timeout routines to exit
171 * @qp - the QP
172 */
Mike Marciniszyn3c9d1492016-02-04 10:59:27 -0800173void hfi1_del_timers_sync(struct rvt_qp *qp)
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800174{
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800175 struct hfi1_qp_priv *priv = qp->priv;
176
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800177 del_timer_sync(&qp->s_timer);
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800178 del_timer_sync(&priv->s_rnr_timer);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800179}
180
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800181/* only opcode mask for adaptive pio */
182const u32 rc_only_opcode =
183 BIT(OP(SEND_ONLY) & 0x1f) |
184 BIT(OP(SEND_ONLY_WITH_IMMEDIATE & 0x1f)) |
185 BIT(OP(RDMA_WRITE_ONLY & 0x1f)) |
186 BIT(OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE & 0x1f)) |
187 BIT(OP(RDMA_READ_REQUEST & 0x1f)) |
188 BIT(OP(ACKNOWLEDGE & 0x1f)) |
189 BIT(OP(ATOMIC_ACKNOWLEDGE & 0x1f)) |
190 BIT(OP(COMPARE_SWAP & 0x1f)) |
191 BIT(OP(FETCH_ADD & 0x1f));
192
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800193static u32 restart_sge(struct rvt_sge_state *ss, struct rvt_swqe *wqe,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400194 u32 psn, u32 pmtu)
195{
196 u32 len;
197
198 len = delta_psn(psn, wqe->psn) * pmtu;
199 ss->sge = wqe->sg_list[0];
200 ss->sg_list = wqe->sg_list + 1;
201 ss->num_sge = wqe->wr.num_sge;
202 ss->total_len = wqe->length;
203 hfi1_skip_sge(ss, len, 0);
204 return wqe->length - len;
205}
206
Mike Marciniszyn77241052015-07-30 15:17:43 -0400207/**
208 * make_rc_ack - construct a response packet (ACK, NAK, or RDMA read)
209 * @dev: the device for this QP
210 * @qp: a pointer to the QP
211 * @ohdr: a pointer to the IB header being constructed
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800212 * @ps: the xmit packet state
Mike Marciniszyn77241052015-07-30 15:17:43 -0400213 *
214 * Return 1 if constructed; otherwise, return 0.
215 * Note that we are in the responder's side of the QP context.
216 * Note the QP s_lock must be held.
217 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800218static int make_rc_ack(struct hfi1_ibdev *dev, struct rvt_qp *qp,
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700219 struct ib_other_headers *ohdr,
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800220 struct hfi1_pkt_state *ps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400221{
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800222 struct rvt_ack_entry *e;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400223 u32 hwords;
224 u32 len;
225 u32 bth0;
226 u32 bth2;
227 int middle = 0;
Mike Marciniszyn1235bef2016-02-14 12:45:09 -0800228 u32 pmtu = qp->pmtu;
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800229 struct hfi1_qp_priv *priv = qp->priv;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400230
231 /* Don't send an ACK if we aren't supposed to. */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800232 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400233 goto bail;
234
235 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
236 hwords = 5;
237
238 switch (qp->s_ack_state) {
239 case OP(RDMA_READ_RESPONSE_LAST):
240 case OP(RDMA_READ_RESPONSE_ONLY):
241 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
242 if (e->rdma_sge.mr) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800243 rvt_put_mr(e->rdma_sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400244 e->rdma_sge.mr = NULL;
245 }
246 /* FALLTHROUGH */
247 case OP(ATOMIC_ACKNOWLEDGE):
248 /*
249 * We can increment the tail pointer now that the last
250 * response has been sent instead of only being
251 * constructed.
252 */
253 if (++qp->s_tail_ack_queue > HFI1_MAX_RDMA_ATOMIC)
254 qp->s_tail_ack_queue = 0;
255 /* FALLTHROUGH */
256 case OP(SEND_ONLY):
257 case OP(ACKNOWLEDGE):
258 /* Check for no next entry in the queue. */
259 if (qp->r_head_ack_queue == qp->s_tail_ack_queue) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800260 if (qp->s_flags & RVT_S_ACK_PENDING)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400261 goto normal;
262 goto bail;
263 }
264
265 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
266 if (e->opcode == OP(RDMA_READ_REQUEST)) {
267 /*
268 * If a RDMA read response is being resent and
269 * we haven't seen the duplicate request yet,
270 * then stop sending the remaining responses the
271 * responder has seen until the requester re-sends it.
272 */
273 len = e->rdma_sge.sge_length;
274 if (len && !e->rdma_sge.mr) {
275 qp->s_tail_ack_queue = qp->r_head_ack_queue;
276 goto bail;
277 }
278 /* Copy SGE state in case we need to resend */
Mike Marciniszync239a5b2016-02-14 12:44:52 -0800279 ps->s_txreq->mr = e->rdma_sge.mr;
280 if (ps->s_txreq->mr)
281 rvt_get_mr(ps->s_txreq->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400282 qp->s_ack_rdma_sge.sge = e->rdma_sge;
283 qp->s_ack_rdma_sge.num_sge = 1;
284 qp->s_cur_sge = &qp->s_ack_rdma_sge;
285 if (len > pmtu) {
286 len = pmtu;
287 qp->s_ack_state = OP(RDMA_READ_RESPONSE_FIRST);
288 } else {
289 qp->s_ack_state = OP(RDMA_READ_RESPONSE_ONLY);
290 e->sent = 1;
291 }
292 ohdr->u.aeth = hfi1_compute_aeth(qp);
293 hwords++;
294 qp->s_ack_rdma_psn = e->psn;
295 bth2 = mask_psn(qp->s_ack_rdma_psn++);
296 } else {
297 /* COMPARE_SWAP or FETCH_ADD */
298 qp->s_cur_sge = NULL;
299 len = 0;
300 qp->s_ack_state = OP(ATOMIC_ACKNOWLEDGE);
301 ohdr->u.at.aeth = hfi1_compute_aeth(qp);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700302 ib_u64_put(e->atomic_data, &ohdr->u.at.atomic_ack_eth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400303 hwords += sizeof(ohdr->u.at) / sizeof(u32);
304 bth2 = mask_psn(e->psn);
305 e->sent = 1;
306 }
307 bth0 = qp->s_ack_state << 24;
308 break;
309
310 case OP(RDMA_READ_RESPONSE_FIRST):
311 qp->s_ack_state = OP(RDMA_READ_RESPONSE_MIDDLE);
312 /* FALLTHROUGH */
313 case OP(RDMA_READ_RESPONSE_MIDDLE):
314 qp->s_cur_sge = &qp->s_ack_rdma_sge;
Mike Marciniszync239a5b2016-02-14 12:44:52 -0800315 ps->s_txreq->mr = qp->s_ack_rdma_sge.sge.mr;
316 if (ps->s_txreq->mr)
317 rvt_get_mr(ps->s_txreq->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400318 len = qp->s_ack_rdma_sge.sge.sge_length;
319 if (len > pmtu) {
320 len = pmtu;
321 middle = HFI1_CAP_IS_KSET(SDMA_AHG);
322 } else {
323 ohdr->u.aeth = hfi1_compute_aeth(qp);
324 hwords++;
325 qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
326 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
327 e->sent = 1;
328 }
329 bth0 = qp->s_ack_state << 24;
330 bth2 = mask_psn(qp->s_ack_rdma_psn++);
331 break;
332
333 default:
334normal:
335 /*
336 * Send a regular ACK.
337 * Set the s_ack_state so we wait until after sending
338 * the ACK before setting s_ack_state to ACKNOWLEDGE
339 * (see above).
340 */
341 qp->s_ack_state = OP(SEND_ONLY);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800342 qp->s_flags &= ~RVT_S_ACK_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400343 qp->s_cur_sge = NULL;
344 if (qp->s_nak_state)
345 ohdr->u.aeth =
346 cpu_to_be32((qp->r_msn & HFI1_MSN_MASK) |
347 (qp->s_nak_state <<
348 HFI1_AETH_CREDIT_SHIFT));
349 else
350 ohdr->u.aeth = hfi1_compute_aeth(qp);
351 hwords++;
352 len = 0;
353 bth0 = OP(ACKNOWLEDGE) << 24;
354 bth2 = mask_psn(qp->s_ack_psn);
355 }
356 qp->s_rdma_ack_cnt++;
357 qp->s_hdrwords = hwords;
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800358 ps->s_txreq->sde = priv->s_sde;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400359 qp->s_cur_size = len;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800360 hfi1_make_ruc_header(qp, ohdr, bth0, bth2, middle, ps);
Jianxin Xiongaa0ad412016-02-26 13:33:13 -0800361 /* pbc */
362 ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400363 return 1;
364
365bail:
366 qp->s_ack_state = OP(ACKNOWLEDGE);
367 /*
368 * Ensure s_rdma_ack_cnt changes are committed prior to resetting
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800369 * RVT_S_RESP_PENDING
Mike Marciniszyn77241052015-07-30 15:17:43 -0400370 */
371 smp_wmb();
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800372 qp->s_flags &= ~(RVT_S_RESP_PENDING
373 | RVT_S_ACK_PENDING
374 | RVT_S_AHG_VALID);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400375 return 0;
376}
377
378/**
379 * hfi1_make_rc_req - construct a request packet (SEND, RDMA r/w, ATOMIC)
380 * @qp: a pointer to the QP
381 *
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800382 * Assumes s_lock is held.
383 *
Mike Marciniszyn77241052015-07-30 15:17:43 -0400384 * Return 1 if constructed; otherwise, return 0.
385 */
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800386int hfi1_make_rc_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400387{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800388 struct hfi1_qp_priv *priv = qp->priv;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400389 struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700390 struct ib_other_headers *ohdr;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800391 struct rvt_sge_state *ss;
392 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400393 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
394 u32 hwords = 5;
395 u32 len;
396 u32 bth0 = 0;
397 u32 bth2;
398 u32 pmtu = qp->pmtu;
399 char newreq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400400 int middle = 0;
401 int delta;
402
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800403 ps->s_txreq = get_txreq(ps->dev, qp);
404 if (IS_ERR(ps->s_txreq))
405 goto bail_no_tx;
406
407 ohdr = &ps->s_txreq->phdr.hdr.u.oth;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400408 if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800409 ohdr = &ps->s_txreq->phdr.hdr.u.l.oth;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400410
Mike Marciniszyn77241052015-07-30 15:17:43 -0400411 /* Sending responses has higher priority over sending requests. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800412 if ((qp->s_flags & RVT_S_RESP_PENDING) &&
Mike Marciniszyn1235bef2016-02-14 12:45:09 -0800413 make_rc_ack(dev, qp, ohdr, ps))
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800414 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400415
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800416 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_SEND_OK)) {
417 if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400418 goto bail;
419 /* We are in the error state, flush the work request. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800420 smp_read_barrier_depends(); /* see post_one_send() */
421 if (qp->s_last == ACCESS_ONCE(qp->s_head))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400422 goto bail;
423 /* If DMAs are in progress, we can't flush immediately. */
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800424 if (iowait_sdma_pending(&priv->s_iowait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800425 qp->s_flags |= RVT_S_WAIT_DMA;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400426 goto bail;
427 }
428 clear_ahg(qp);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800429 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400430 hfi1_send_complete(qp, wqe, qp->s_last != qp->s_acked ?
431 IB_WC_SUCCESS : IB_WC_WR_FLUSH_ERR);
432 /* will get called again */
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800433 goto done_free_tx;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400434 }
435
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800436 if (qp->s_flags & (RVT_S_WAIT_RNR | RVT_S_WAIT_ACK))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400437 goto bail;
438
439 if (cmp_psn(qp->s_psn, qp->s_sending_hpsn) <= 0) {
440 if (cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800441 qp->s_flags |= RVT_S_WAIT_PSN;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400442 goto bail;
443 }
444 qp->s_sending_psn = qp->s_psn;
445 qp->s_sending_hpsn = qp->s_psn - 1;
446 }
447
448 /* Send a request. */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800449 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400450 switch (qp->s_state) {
451 default:
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800452 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400453 goto bail;
454 /*
455 * Resend an old request or start a new one.
456 *
457 * We keep track of the current SWQE so that
458 * we don't reset the "furthest progress" state
459 * if we need to back up.
460 */
461 newreq = 0;
462 if (qp->s_cur == qp->s_tail) {
463 /* Check if send work queue is empty. */
464 if (qp->s_tail == qp->s_head) {
465 clear_ahg(qp);
466 goto bail;
467 }
468 /*
469 * If a fence is requested, wait for previous
470 * RDMA read and atomic operations to finish.
471 */
472 if ((wqe->wr.send_flags & IB_SEND_FENCE) &&
473 qp->s_num_rd_atomic) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800474 qp->s_flags |= RVT_S_WAIT_FENCE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400475 goto bail;
476 }
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700477 /*
478 * Local operations are processed immediately
479 * after all prior requests have completed
480 */
481 if (wqe->wr.opcode == IB_WR_REG_MR ||
482 wqe->wr.opcode == IB_WR_LOCAL_INV) {
Jianxin Xiongd9b13c22016-07-25 13:39:45 -0700483 int local_ops = 0;
484 int err = 0;
485
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700486 if (qp->s_last != qp->s_cur)
487 goto bail;
488 if (++qp->s_cur == qp->s_size)
489 qp->s_cur = 0;
490 if (++qp->s_tail == qp->s_size)
491 qp->s_tail = 0;
Jianxin Xiongd9b13c22016-07-25 13:39:45 -0700492 if (!(wqe->wr.send_flags &
493 RVT_SEND_COMPLETION_ONLY)) {
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700494 err = rvt_invalidate_rkey(
495 qp,
496 wqe->wr.ex.invalidate_rkey);
Jianxin Xiongd9b13c22016-07-25 13:39:45 -0700497 local_ops = 1;
498 }
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700499 hfi1_send_complete(qp, wqe,
500 err ? IB_WC_LOC_PROT_ERR
501 : IB_WC_SUCCESS);
Jianxin Xiongd9b13c22016-07-25 13:39:45 -0700502 if (local_ops)
503 atomic_dec(&qp->local_ops_pending);
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700504 qp->s_hdrwords = 0;
505 goto done_free_tx;
506 }
507
Mike Marciniszyn77241052015-07-30 15:17:43 -0400508 newreq = 1;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800509 qp->s_psn = wqe->psn;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400510 }
511 /*
512 * Note that we have to be careful not to modify the
513 * original work request since we may need to resend
514 * it.
515 */
516 len = wqe->length;
517 ss = &qp->s_sge;
518 bth2 = mask_psn(qp->s_psn);
519 switch (wqe->wr.opcode) {
520 case IB_WR_SEND:
521 case IB_WR_SEND_WITH_IMM:
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700522 case IB_WR_SEND_WITH_INV:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400523 /* If no credit, return. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800524 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
Mike Marciniszyn77241052015-07-30 15:17:43 -0400525 cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800526 qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400527 goto bail;
528 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400529 if (len > pmtu) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400530 qp->s_state = OP(SEND_FIRST);
531 len = pmtu;
532 break;
533 }
Jubin Johne4909742016-02-14 20:22:00 -0800534 if (wqe->wr.opcode == IB_WR_SEND) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400535 qp->s_state = OP(SEND_ONLY);
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700536 } else if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400537 qp->s_state = OP(SEND_ONLY_WITH_IMMEDIATE);
538 /* Immediate data comes after the BTH */
539 ohdr->u.imm_data = wqe->wr.ex.imm_data;
540 hwords += 1;
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700541 } else {
542 qp->s_state = OP(SEND_ONLY_WITH_INVALIDATE);
543 /* Invalidate rkey comes after the BTH */
544 ohdr->u.ieth = cpu_to_be32(
545 wqe->wr.ex.invalidate_rkey);
546 hwords += 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400547 }
548 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
549 bth0 |= IB_BTH_SOLICITED;
550 bth2 |= IB_BTH_REQ_ACK;
551 if (++qp->s_cur == qp->s_size)
552 qp->s_cur = 0;
553 break;
554
555 case IB_WR_RDMA_WRITE:
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800556 if (newreq && !(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400557 qp->s_lsn++;
558 /* FALLTHROUGH */
559 case IB_WR_RDMA_WRITE_WITH_IMM:
560 /* If no credit, return. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800561 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
Mike Marciniszyn77241052015-07-30 15:17:43 -0400562 cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800563 qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400564 goto bail;
565 }
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700566 put_ib_reth_vaddr(
567 wqe->rdma_wr.remote_addr,
568 &ohdr->u.rc.reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400569 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100570 cpu_to_be32(wqe->rdma_wr.rkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400571 ohdr->u.rc.reth.length = cpu_to_be32(len);
572 hwords += sizeof(struct ib_reth) / sizeof(u32);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400573 if (len > pmtu) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400574 qp->s_state = OP(RDMA_WRITE_FIRST);
575 len = pmtu;
576 break;
577 }
Jubin Johne4909742016-02-14 20:22:00 -0800578 if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400579 qp->s_state = OP(RDMA_WRITE_ONLY);
Jubin Johne4909742016-02-14 20:22:00 -0800580 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400581 qp->s_state =
582 OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
583 /* Immediate data comes after RETH */
584 ohdr->u.rc.imm_data = wqe->wr.ex.imm_data;
585 hwords += 1;
586 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
587 bth0 |= IB_BTH_SOLICITED;
588 }
589 bth2 |= IB_BTH_REQ_ACK;
590 if (++qp->s_cur == qp->s_size)
591 qp->s_cur = 0;
592 break;
593
594 case IB_WR_RDMA_READ:
595 /*
596 * Don't allow more operations to be started
597 * than the QP limits allow.
598 */
599 if (newreq) {
600 if (qp->s_num_rd_atomic >=
601 qp->s_max_rd_atomic) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800602 qp->s_flags |= RVT_S_WAIT_RDMAR;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400603 goto bail;
604 }
605 qp->s_num_rd_atomic++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800606 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400607 qp->s_lsn++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400608 }
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700609 put_ib_reth_vaddr(
610 wqe->rdma_wr.remote_addr,
611 &ohdr->u.rc.reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400612 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100613 cpu_to_be32(wqe->rdma_wr.rkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400614 ohdr->u.rc.reth.length = cpu_to_be32(len);
615 qp->s_state = OP(RDMA_READ_REQUEST);
616 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
617 ss = NULL;
618 len = 0;
619 bth2 |= IB_BTH_REQ_ACK;
620 if (++qp->s_cur == qp->s_size)
621 qp->s_cur = 0;
622 break;
623
624 case IB_WR_ATOMIC_CMP_AND_SWP:
625 case IB_WR_ATOMIC_FETCH_AND_ADD:
626 /*
627 * Don't allow more operations to be started
628 * than the QP limits allow.
629 */
630 if (newreq) {
631 if (qp->s_num_rd_atomic >=
632 qp->s_max_rd_atomic) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800633 qp->s_flags |= RVT_S_WAIT_RDMAR;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400634 goto bail;
635 }
636 qp->s_num_rd_atomic++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800637 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400638 qp->s_lsn++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400639 }
640 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
641 qp->s_state = OP(COMPARE_SWAP);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700642 put_ib_ateth_swap(wqe->atomic_wr.swap,
643 &ohdr->u.atomic_eth);
644 put_ib_ateth_compare(wqe->atomic_wr.compare_add,
645 &ohdr->u.atomic_eth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400646 } else {
647 qp->s_state = OP(FETCH_ADD);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700648 put_ib_ateth_swap(wqe->atomic_wr.compare_add,
649 &ohdr->u.atomic_eth);
650 put_ib_ateth_compare(0, &ohdr->u.atomic_eth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400651 }
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700652 put_ib_ateth_vaddr(wqe->atomic_wr.remote_addr,
653 &ohdr->u.atomic_eth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400654 ohdr->u.atomic_eth.rkey = cpu_to_be32(
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100655 wqe->atomic_wr.rkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400656 hwords += sizeof(struct ib_atomic_eth) / sizeof(u32);
657 ss = NULL;
658 len = 0;
659 bth2 |= IB_BTH_REQ_ACK;
660 if (++qp->s_cur == qp->s_size)
661 qp->s_cur = 0;
662 break;
663
664 default:
665 goto bail;
666 }
667 qp->s_sge.sge = wqe->sg_list[0];
668 qp->s_sge.sg_list = wqe->sg_list + 1;
669 qp->s_sge.num_sge = wqe->wr.num_sge;
670 qp->s_sge.total_len = wqe->length;
671 qp->s_len = wqe->length;
672 if (newreq) {
673 qp->s_tail++;
674 if (qp->s_tail >= qp->s_size)
675 qp->s_tail = 0;
676 }
677 if (wqe->wr.opcode == IB_WR_RDMA_READ)
678 qp->s_psn = wqe->lpsn + 1;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800679 else
Mike Marciniszyn77241052015-07-30 15:17:43 -0400680 qp->s_psn++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400681 break;
682
683 case OP(RDMA_READ_RESPONSE_FIRST):
684 /*
685 * qp->s_state is normally set to the opcode of the
686 * last packet constructed for new requests and therefore
687 * is never set to RDMA read response.
688 * RDMA_READ_RESPONSE_FIRST is used by the ACK processing
689 * thread to indicate a SEND needs to be restarted from an
690 * earlier PSN without interfering with the sending thread.
691 * See restart_rc().
692 */
693 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
694 /* FALLTHROUGH */
695 case OP(SEND_FIRST):
696 qp->s_state = OP(SEND_MIDDLE);
697 /* FALLTHROUGH */
698 case OP(SEND_MIDDLE):
699 bth2 = mask_psn(qp->s_psn++);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400700 ss = &qp->s_sge;
701 len = qp->s_len;
702 if (len > pmtu) {
703 len = pmtu;
704 middle = HFI1_CAP_IS_KSET(SDMA_AHG);
705 break;
706 }
Jubin Johne4909742016-02-14 20:22:00 -0800707 if (wqe->wr.opcode == IB_WR_SEND) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400708 qp->s_state = OP(SEND_LAST);
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700709 } else if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400710 qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
711 /* Immediate data comes after the BTH */
712 ohdr->u.imm_data = wqe->wr.ex.imm_data;
713 hwords += 1;
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700714 } else {
715 qp->s_state = OP(SEND_LAST_WITH_INVALIDATE);
716 /* invalidate data comes after the BTH */
717 ohdr->u.ieth = cpu_to_be32(wqe->wr.ex.invalidate_rkey);
718 hwords += 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400719 }
720 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
721 bth0 |= IB_BTH_SOLICITED;
722 bth2 |= IB_BTH_REQ_ACK;
723 qp->s_cur++;
724 if (qp->s_cur >= qp->s_size)
725 qp->s_cur = 0;
726 break;
727
728 case OP(RDMA_READ_RESPONSE_LAST):
729 /*
730 * qp->s_state is normally set to the opcode of the
731 * last packet constructed for new requests and therefore
732 * is never set to RDMA read response.
733 * RDMA_READ_RESPONSE_LAST is used by the ACK processing
734 * thread to indicate a RDMA write needs to be restarted from
735 * an earlier PSN without interfering with the sending thread.
736 * See restart_rc().
737 */
738 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
739 /* FALLTHROUGH */
740 case OP(RDMA_WRITE_FIRST):
741 qp->s_state = OP(RDMA_WRITE_MIDDLE);
742 /* FALLTHROUGH */
743 case OP(RDMA_WRITE_MIDDLE):
744 bth2 = mask_psn(qp->s_psn++);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400745 ss = &qp->s_sge;
746 len = qp->s_len;
747 if (len > pmtu) {
748 len = pmtu;
749 middle = HFI1_CAP_IS_KSET(SDMA_AHG);
750 break;
751 }
Jubin Johne4909742016-02-14 20:22:00 -0800752 if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400753 qp->s_state = OP(RDMA_WRITE_LAST);
Jubin Johne4909742016-02-14 20:22:00 -0800754 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400755 qp->s_state = OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
756 /* Immediate data comes after the BTH */
757 ohdr->u.imm_data = wqe->wr.ex.imm_data;
758 hwords += 1;
759 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
760 bth0 |= IB_BTH_SOLICITED;
761 }
762 bth2 |= IB_BTH_REQ_ACK;
763 qp->s_cur++;
764 if (qp->s_cur >= qp->s_size)
765 qp->s_cur = 0;
766 break;
767
768 case OP(RDMA_READ_RESPONSE_MIDDLE):
769 /*
770 * qp->s_state is normally set to the opcode of the
771 * last packet constructed for new requests and therefore
772 * is never set to RDMA read response.
773 * RDMA_READ_RESPONSE_MIDDLE is used by the ACK processing
774 * thread to indicate a RDMA read needs to be restarted from
775 * an earlier PSN without interfering with the sending thread.
776 * See restart_rc().
777 */
778 len = (delta_psn(qp->s_psn, wqe->psn)) * pmtu;
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700779 put_ib_reth_vaddr(
780 wqe->rdma_wr.remote_addr + len,
781 &ohdr->u.rc.reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400782 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100783 cpu_to_be32(wqe->rdma_wr.rkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400784 ohdr->u.rc.reth.length = cpu_to_be32(wqe->length - len);
785 qp->s_state = OP(RDMA_READ_REQUEST);
786 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
787 bth2 = mask_psn(qp->s_psn) | IB_BTH_REQ_ACK;
788 qp->s_psn = wqe->lpsn + 1;
789 ss = NULL;
790 len = 0;
791 qp->s_cur++;
792 if (qp->s_cur == qp->s_size)
793 qp->s_cur = 0;
794 break;
795 }
796 qp->s_sending_hpsn = bth2;
797 delta = delta_psn(bth2, wqe->psn);
798 if (delta && delta % HFI1_PSN_CREDIT == 0)
799 bth2 |= IB_BTH_REQ_ACK;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800800 if (qp->s_flags & RVT_S_SEND_ONE) {
801 qp->s_flags &= ~RVT_S_SEND_ONE;
802 qp->s_flags |= RVT_S_WAIT_ACK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400803 bth2 |= IB_BTH_REQ_ACK;
804 }
805 qp->s_len -= len;
806 qp->s_hdrwords = hwords;
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800807 ps->s_txreq->sde = priv->s_sde;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400808 qp->s_cur_sge = ss;
809 qp->s_cur_size = len;
810 hfi1_make_ruc_header(
811 qp,
812 ohdr,
813 bth0 | (qp->s_state << 24),
814 bth2,
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800815 middle,
816 ps);
Jianxin Xiongaa0ad412016-02-26 13:33:13 -0800817 /* pbc */
818 ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800819 return 1;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800820
821done_free_tx:
822 hfi1_put_txreq(ps->s_txreq);
823 ps->s_txreq = NULL;
824 return 1;
825
Mike Marciniszyn77241052015-07-30 15:17:43 -0400826bail:
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800827 hfi1_put_txreq(ps->s_txreq);
828
829bail_no_tx:
830 ps->s_txreq = NULL;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800831 qp->s_flags &= ~RVT_S_BUSY;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800832 qp->s_hdrwords = 0;
833 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400834}
835
836/**
837 * hfi1_send_rc_ack - Construct an ACK packet and send it
838 * @qp: a pointer to the QP
839 *
840 * This is called from hfi1_rc_rcv() and handle_receive_interrupt().
841 * Note that RDMA reads and atomics are handled in the
842 * send side QP state and tasklet.
843 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800844void hfi1_send_rc_ack(struct hfi1_ctxtdata *rcd, struct rvt_qp *qp,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400845 int is_fecn)
846{
847 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
848 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
849 u64 pbc, pbc_flags = 0;
850 u16 lrh0;
851 u16 sc5;
852 u32 bth0;
853 u32 hwords;
854 u32 vl, plen;
855 struct send_context *sc;
856 struct pio_buf *pbuf;
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700857 struct ib_header hdr;
858 struct ib_other_headers *ohdr;
Dean Luickb77d7132015-10-26 10:28:43 -0400859 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400860
861 /* Don't send ACK or NAK if a RDMA read or atomic is pending. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800862 if (qp->s_flags & RVT_S_RESP_PENDING)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400863 goto queue_ack;
864
865 /* Ensure s_rdma_ack_cnt changes are committed */
866 smp_read_barrier_depends();
867 if (qp->s_rdma_ack_cnt)
868 goto queue_ack;
869
870 /* Construct the header */
871 /* header size in 32-bit words LRH+BTH+AETH = (8+12+4)/4 */
872 hwords = 6;
873 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
874 hwords += hfi1_make_grh(ibp, &hdr.u.l.grh,
875 &qp->remote_ah_attr.grh, hwords, 0);
876 ohdr = &hdr.u.l.oth;
877 lrh0 = HFI1_LRH_GRH;
878 } else {
879 ohdr = &hdr.u.oth;
880 lrh0 = HFI1_LRH_BTH;
881 }
882 /* read pkey_index w/o lock (its atomic) */
883 bth0 = hfi1_get_pkey(ibp, qp->s_pkey_index) | (OP(ACKNOWLEDGE) << 24);
884 if (qp->s_mig_state == IB_MIG_MIGRATED)
885 bth0 |= IB_BTH_MIG_REQ;
886 if (qp->r_nak_state)
887 ohdr->u.aeth = cpu_to_be32((qp->r_msn & HFI1_MSN_MASK) |
888 (qp->r_nak_state <<
889 HFI1_AETH_CREDIT_SHIFT));
890 else
891 ohdr->u.aeth = hfi1_compute_aeth(qp);
892 sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl];
893 /* set PBC_DC_INFO bit (aka SC[4]) in pbc_flags */
894 pbc_flags |= ((!!(sc5 & 0x10)) << PBC_DC_INFO_SHIFT);
895 lrh0 |= (sc5 & 0xf) << 12 | (qp->remote_ah_attr.sl & 0xf) << 4;
896 hdr.lrh[0] = cpu_to_be16(lrh0);
897 hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
898 hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
899 hdr.lrh[3] = cpu_to_be16(ppd->lid | qp->remote_ah_attr.src_path_bits);
900 ohdr->bth[0] = cpu_to_be32(bth0);
901 ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
902 ohdr->bth[1] |= cpu_to_be32((!!is_fecn) << HFI1_BECN_SHIFT);
903 ohdr->bth[2] = cpu_to_be32(mask_psn(qp->r_ack_psn));
904
905 /* Don't try to send ACKs if the link isn't ACTIVE */
906 if (driver_lstate(ppd) != IB_PORT_ACTIVE)
907 return;
908
909 sc = rcd->sc;
910 plen = 2 /* PBC */ + hwords;
911 vl = sc_to_vlt(ppd->dd, sc5);
912 pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
913
914 pbuf = sc_buffer_alloc(sc, plen, NULL, NULL);
915 if (!pbuf) {
916 /*
917 * We have no room to send at the moment. Pass
918 * responsibility for sending the ACK to the send tasklet
919 * so that when enough buffer space becomes available,
920 * the ACK is sent ahead of other outgoing packets.
921 */
922 goto queue_ack;
923 }
924
Mike Marciniszyn1db78ee2016-03-07 11:35:19 -0800925 trace_ack_output_ibhdr(dd_from_ibdev(qp->ibqp.device), &hdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400926
927 /* write the pbc and data */
928 ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc, &hdr, hwords);
929
930 return;
931
932queue_ack:
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800933 this_cpu_inc(*ibp->rvp.rc_qacks);
Dean Luickb77d7132015-10-26 10:28:43 -0400934 spin_lock_irqsave(&qp->s_lock, flags);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800935 qp->s_flags |= RVT_S_ACK_PENDING | RVT_S_RESP_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400936 qp->s_nak_state = qp->r_nak_state;
937 qp->s_ack_psn = qp->r_ack_psn;
938 if (is_fecn)
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800939 qp->s_flags |= RVT_S_ECN;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400940
941 /* Schedule the send tasklet. */
942 hfi1_schedule_send(qp);
Dean Luickb77d7132015-10-26 10:28:43 -0400943 spin_unlock_irqrestore(&qp->s_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400944}
945
946/**
947 * reset_psn - reset the QP state to send starting from PSN
948 * @qp: the QP
949 * @psn: the packet sequence number to restart at
950 *
951 * This is called from hfi1_rc_rcv() to process an incoming RC ACK
952 * for the given QP.
953 * Called at interrupt level with the QP s_lock held.
954 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800955static void reset_psn(struct rvt_qp *qp, u32 psn)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400956{
957 u32 n = qp->s_acked;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800958 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, n);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400959 u32 opcode;
960
961 qp->s_cur = n;
962
963 /*
964 * If we are starting the request from the beginning,
965 * let the normal send code handle initialization.
966 */
967 if (cmp_psn(psn, wqe->psn) <= 0) {
968 qp->s_state = OP(SEND_LAST);
969 goto done;
970 }
971
972 /* Find the work request opcode corresponding to the given PSN. */
973 opcode = wqe->wr.opcode;
974 for (;;) {
975 int diff;
976
977 if (++n == qp->s_size)
978 n = 0;
979 if (n == qp->s_tail)
980 break;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800981 wqe = rvt_get_swqe_ptr(qp, n);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400982 diff = cmp_psn(psn, wqe->psn);
983 if (diff < 0)
984 break;
985 qp->s_cur = n;
986 /*
987 * If we are starting the request from the beginning,
988 * let the normal send code handle initialization.
989 */
990 if (diff == 0) {
991 qp->s_state = OP(SEND_LAST);
992 goto done;
993 }
994 opcode = wqe->wr.opcode;
995 }
996
997 /*
998 * Set the state to restart in the middle of a request.
999 * Don't change the s_sge, s_cur_sge, or s_cur_size.
1000 * See hfi1_make_rc_req().
1001 */
1002 switch (opcode) {
1003 case IB_WR_SEND:
1004 case IB_WR_SEND_WITH_IMM:
1005 qp->s_state = OP(RDMA_READ_RESPONSE_FIRST);
1006 break;
1007
1008 case IB_WR_RDMA_WRITE:
1009 case IB_WR_RDMA_WRITE_WITH_IMM:
1010 qp->s_state = OP(RDMA_READ_RESPONSE_LAST);
1011 break;
1012
1013 case IB_WR_RDMA_READ:
1014 qp->s_state = OP(RDMA_READ_RESPONSE_MIDDLE);
1015 break;
1016
1017 default:
1018 /*
1019 * This case shouldn't happen since its only
1020 * one PSN per req.
1021 */
1022 qp->s_state = OP(SEND_LAST);
1023 }
1024done:
1025 qp->s_psn = psn;
1026 /*
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001027 * Set RVT_S_WAIT_PSN as rc_complete() may start the timer
Mike Marciniszyn77241052015-07-30 15:17:43 -04001028 * asynchronously before the send tasklet can get scheduled.
1029 * Doing it in hfi1_make_rc_req() is too late.
1030 */
1031 if ((cmp_psn(qp->s_psn, qp->s_sending_hpsn) <= 0) &&
1032 (cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0))
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001033 qp->s_flags |= RVT_S_WAIT_PSN;
1034 qp->s_flags &= ~RVT_S_AHG_VALID;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001035}
1036
1037/*
1038 * Back up requester to resend the last un-ACKed request.
1039 * The QP r_lock and s_lock should be held and interrupts disabled.
1040 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001041static void restart_rc(struct rvt_qp *qp, u32 psn, int wait)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001042{
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001043 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001044 struct hfi1_ibport *ibp;
1045
1046 if (qp->s_retry == 0) {
1047 if (qp->s_mig_state == IB_MIG_ARMED) {
1048 hfi1_migrate_qp(qp);
1049 qp->s_retry = qp->s_retry_cnt;
1050 } else if (qp->s_last == qp->s_acked) {
1051 hfi1_send_complete(qp, wqe, IB_WC_RETRY_EXC_ERR);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08001052 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001053 return;
Jubin Johne4909742016-02-14 20:22:00 -08001054 } else { /* need to handle delayed completion */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001055 return;
Jubin Johne4909742016-02-14 20:22:00 -08001056 }
1057 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001058 qp->s_retry--;
Jubin Johne4909742016-02-14 20:22:00 -08001059 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001060
1061 ibp = to_iport(qp->ibqp.device, qp->port_num);
1062 if (wqe->wr.opcode == IB_WR_RDMA_READ)
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001063 ibp->rvp.n_rc_resends++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001064 else
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001065 ibp->rvp.n_rc_resends += delta_psn(qp->s_psn, psn);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001066
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001067 qp->s_flags &= ~(RVT_S_WAIT_FENCE | RVT_S_WAIT_RDMAR |
1068 RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_PSN |
1069 RVT_S_WAIT_ACK);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001070 if (wait)
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001071 qp->s_flags |= RVT_S_SEND_ONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001072 reset_psn(qp, psn);
1073}
1074
1075/*
1076 * This is called from s_timer for missing responses.
1077 */
Mike Marciniszyn08279d52016-02-04 10:59:36 -08001078void hfi1_rc_timeout(unsigned long arg)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001079{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001080 struct rvt_qp *qp = (struct rvt_qp *)arg;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001081 struct hfi1_ibport *ibp;
1082 unsigned long flags;
1083
1084 spin_lock_irqsave(&qp->r_lock, flags);
1085 spin_lock(&qp->s_lock);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001086 if (qp->s_flags & RVT_S_TIMER) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001087 ibp = to_iport(qp->ibqp.device, qp->port_num);
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001088 ibp->rvp.n_rc_timeouts++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001089 qp->s_flags &= ~RVT_S_TIMER;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001090 del_timer(&qp->s_timer);
Sebastian Sanchez462b6b22016-07-01 16:01:06 -07001091 trace_hfi1_timeout(qp, qp->s_last_psn + 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001092 restart_rc(qp, qp->s_last_psn + 1, 1);
1093 hfi1_schedule_send(qp);
1094 }
1095 spin_unlock(&qp->s_lock);
1096 spin_unlock_irqrestore(&qp->r_lock, flags);
1097}
1098
1099/*
1100 * This is called from s_timer for RNR timeouts.
1101 */
1102void hfi1_rc_rnr_retry(unsigned long arg)
1103{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001104 struct rvt_qp *qp = (struct rvt_qp *)arg;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001105 unsigned long flags;
1106
1107 spin_lock_irqsave(&qp->s_lock, flags);
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001108 hfi1_stop_rnr_timer(qp);
1109 hfi1_schedule_send(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001110 spin_unlock_irqrestore(&qp->s_lock, flags);
1111}
1112
1113/*
1114 * Set qp->s_sending_psn to the next PSN after the given one.
1115 * This would be psn+1 except when RDMA reads are present.
1116 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001117static void reset_sending_psn(struct rvt_qp *qp, u32 psn)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001118{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001119 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001120 u32 n = qp->s_last;
1121
1122 /* Find the work request corresponding to the given PSN. */
1123 for (;;) {
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001124 wqe = rvt_get_swqe_ptr(qp, n);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001125 if (cmp_psn(psn, wqe->lpsn) <= 0) {
1126 if (wqe->wr.opcode == IB_WR_RDMA_READ)
1127 qp->s_sending_psn = wqe->lpsn + 1;
1128 else
1129 qp->s_sending_psn = psn + 1;
1130 break;
1131 }
1132 if (++n == qp->s_size)
1133 n = 0;
1134 if (n == qp->s_tail)
1135 break;
1136 }
1137}
1138
1139/*
1140 * This should be called with the QP s_lock held and interrupts disabled.
1141 */
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001142void hfi1_rc_send_complete(struct rvt_qp *qp, struct ib_header *hdr)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001143{
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001144 struct ib_other_headers *ohdr;
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001145 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001146 struct ib_wc wc;
1147 unsigned i;
1148 u32 opcode;
1149 u32 psn;
1150
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001151 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001152 return;
1153
1154 /* Find out where the BTH is */
1155 if ((be16_to_cpu(hdr->lrh[0]) & 3) == HFI1_LRH_BTH)
1156 ohdr = &hdr->u.oth;
1157 else
1158 ohdr = &hdr->u.l.oth;
1159
1160 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
1161 if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
1162 opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
1163 WARN_ON(!qp->s_rdma_ack_cnt);
1164 qp->s_rdma_ack_cnt--;
1165 return;
1166 }
1167
1168 psn = be32_to_cpu(ohdr->bth[2]);
1169 reset_sending_psn(qp, psn);
1170
1171 /*
1172 * Start timer after a packet requesting an ACK has been sent and
1173 * there are still requests that haven't been acked.
1174 */
1175 if ((psn & IB_BTH_REQ_ACK) && qp->s_acked != qp->s_tail &&
1176 !(qp->s_flags &
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001177 (RVT_S_TIMER | RVT_S_WAIT_RNR | RVT_S_WAIT_PSN)) &&
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001178 (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001179 hfi1_add_retry_timer(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001180
1181 while (qp->s_last != qp->s_acked) {
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -08001182 u32 s_last;
1183
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001184 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001185 if (cmp_psn(wqe->lpsn, qp->s_sending_psn) >= 0 &&
1186 cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0)
1187 break;
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -08001188 s_last = qp->s_last;
1189 if (++s_last >= qp->s_size)
1190 s_last = 0;
1191 qp->s_last = s_last;
1192 /* see post_send() */
1193 barrier();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001194 for (i = 0; i < wqe->wr.num_sge; i++) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001195 struct rvt_sge *sge = &wqe->sg_list[i];
Mike Marciniszyn77241052015-07-30 15:17:43 -04001196
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001197 rvt_put_mr(sge->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001198 }
1199 /* Post a send completion queue entry if requested. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001200 if (!(qp->s_flags & RVT_S_SIGNAL_REQ_WR) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04001201 (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
1202 memset(&wc, 0, sizeof(wc));
1203 wc.wr_id = wqe->wr.wr_id;
1204 wc.status = IB_WC_SUCCESS;
1205 wc.opcode = ib_hfi1_wc_opcode[wqe->wr.opcode];
1206 wc.byte_len = wqe->length;
1207 wc.qp = &qp->ibqp;
Dennis Dalessandroabd712d2016-01-19 14:43:22 -08001208 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.send_cq), &wc, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001209 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001210 }
1211 /*
1212 * If we were waiting for sends to complete before re-sending,
1213 * and they are now complete, restart sending.
1214 */
Sebastian Sanchez462b6b22016-07-01 16:01:06 -07001215 trace_hfi1_sendcomplete(qp, psn);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001216 if (qp->s_flags & RVT_S_WAIT_PSN &&
Mike Marciniszyn77241052015-07-30 15:17:43 -04001217 cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001218 qp->s_flags &= ~RVT_S_WAIT_PSN;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001219 qp->s_sending_psn = qp->s_psn;
1220 qp->s_sending_hpsn = qp->s_psn - 1;
1221 hfi1_schedule_send(qp);
1222 }
1223}
1224
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001225static inline void update_last_psn(struct rvt_qp *qp, u32 psn)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001226{
1227 qp->s_last_psn = psn;
1228}
1229
1230/*
1231 * Generate a SWQE completion.
1232 * This is similar to hfi1_send_complete but has to check to be sure
1233 * that the SGEs are not being referenced if the SWQE is being resent.
1234 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001235static struct rvt_swqe *do_rc_completion(struct rvt_qp *qp,
1236 struct rvt_swqe *wqe,
1237 struct hfi1_ibport *ibp)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001238{
1239 struct ib_wc wc;
1240 unsigned i;
1241
1242 /*
1243 * Don't decrement refcount and don't generate a
1244 * completion if the SWQE is being resent until the send
1245 * is finished.
1246 */
1247 if (cmp_psn(wqe->lpsn, qp->s_sending_psn) < 0 ||
1248 cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -08001249 u32 s_last;
1250
Mike Marciniszyn77241052015-07-30 15:17:43 -04001251 for (i = 0; i < wqe->wr.num_sge; i++) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001252 struct rvt_sge *sge = &wqe->sg_list[i];
Mike Marciniszyn77241052015-07-30 15:17:43 -04001253
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001254 rvt_put_mr(sge->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001255 }
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -08001256 s_last = qp->s_last;
1257 if (++s_last >= qp->s_size)
1258 s_last = 0;
1259 qp->s_last = s_last;
1260 /* see post_send() */
1261 barrier();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001262 /* Post a send completion queue entry if requested. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001263 if (!(qp->s_flags & RVT_S_SIGNAL_REQ_WR) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04001264 (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
1265 memset(&wc, 0, sizeof(wc));
1266 wc.wr_id = wqe->wr.wr_id;
1267 wc.status = IB_WC_SUCCESS;
1268 wc.opcode = ib_hfi1_wc_opcode[wqe->wr.opcode];
1269 wc.byte_len = wqe->length;
1270 wc.qp = &qp->ibqp;
Dennis Dalessandroabd712d2016-01-19 14:43:22 -08001271 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.send_cq), &wc, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001272 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001273 } else {
1274 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
1275
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001276 this_cpu_inc(*ibp->rvp.rc_delayed_comp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001277 /*
1278 * If send progress not running attempt to progress
1279 * SDMA queue.
1280 */
1281 if (ppd->dd->flags & HFI1_HAS_SEND_DMA) {
1282 struct sdma_engine *engine;
1283 u8 sc5;
1284
1285 /* For now use sc to find engine */
1286 sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl];
1287 engine = qp_to_sdma_engine(qp, sc5);
1288 sdma_engine_progress_schedule(engine);
1289 }
1290 }
1291
1292 qp->s_retry = qp->s_retry_cnt;
1293 update_last_psn(qp, wqe->lpsn);
1294
1295 /*
1296 * If we are completing a request which is in the process of
1297 * being resent, we can stop re-sending it since we know the
1298 * responder has already seen it.
1299 */
1300 if (qp->s_acked == qp->s_cur) {
1301 if (++qp->s_cur >= qp->s_size)
1302 qp->s_cur = 0;
1303 qp->s_acked = qp->s_cur;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001304 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001305 if (qp->s_acked != qp->s_tail) {
1306 qp->s_state = OP(SEND_LAST);
1307 qp->s_psn = wqe->psn;
1308 }
1309 } else {
1310 if (++qp->s_acked >= qp->s_size)
1311 qp->s_acked = 0;
1312 if (qp->state == IB_QPS_SQD && qp->s_acked == qp->s_cur)
1313 qp->s_draining = 0;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001314 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001315 }
1316 return wqe;
1317}
1318
1319/**
1320 * do_rc_ack - process an incoming RC ACK
1321 * @qp: the QP the ACK came in on
1322 * @psn: the packet sequence number of the ACK
1323 * @opcode: the opcode of the request that resulted in the ACK
1324 *
1325 * This is called from rc_rcv_resp() to process an incoming RC ACK
1326 * for the given QP.
Dean Luickb77d7132015-10-26 10:28:43 -04001327 * May be called at interrupt level, with the QP s_lock held.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001328 * Returns 1 if OK, 0 if current operation should be aborted (NAK).
1329 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001330static int do_rc_ack(struct rvt_qp *qp, u32 aeth, u32 psn, int opcode,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001331 u64 val, struct hfi1_ctxtdata *rcd)
1332{
1333 struct hfi1_ibport *ibp;
1334 enum ib_wc_status status;
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001335 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001336 int ret = 0;
1337 u32 ack_psn;
1338 int diff;
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001339 unsigned long to;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001340
Mike Marciniszyn77241052015-07-30 15:17:43 -04001341 /*
1342 * Note that NAKs implicitly ACK outstanding SEND and RDMA write
1343 * requests and implicitly NAK RDMA read and atomic requests issued
1344 * before the NAK'ed request. The MSN won't include the NAK'ed
1345 * request but will include an ACK'ed request(s).
1346 */
1347 ack_psn = psn;
1348 if (aeth >> 29)
1349 ack_psn--;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001350 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001351 ibp = to_iport(qp->ibqp.device, qp->port_num);
1352
1353 /*
1354 * The MSN might be for a later WQE than the PSN indicates so
1355 * only complete WQEs that the PSN finishes.
1356 */
1357 while ((diff = delta_psn(ack_psn, wqe->lpsn)) >= 0) {
1358 /*
1359 * RDMA_READ_RESPONSE_ONLY is a special case since
1360 * we want to generate completion events for everything
1361 * before the RDMA read, copy the data, then generate
1362 * the completion for the read.
1363 */
1364 if (wqe->wr.opcode == IB_WR_RDMA_READ &&
1365 opcode == OP(RDMA_READ_RESPONSE_ONLY) &&
1366 diff == 0) {
1367 ret = 1;
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001368 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001369 }
1370 /*
1371 * If this request is a RDMA read or atomic, and the ACK is
1372 * for a later operation, this ACK NAKs the RDMA read or
1373 * atomic. In other words, only a RDMA_READ_LAST or ONLY
1374 * can ACK a RDMA read and likewise for atomic ops. Note
1375 * that the NAK case can only happen if relaxed ordering is
1376 * used and requests are sent after an RDMA read or atomic
1377 * is sent but before the response is received.
1378 */
1379 if ((wqe->wr.opcode == IB_WR_RDMA_READ &&
1380 (opcode != OP(RDMA_READ_RESPONSE_LAST) || diff != 0)) ||
1381 ((wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1382 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) &&
1383 (opcode != OP(ATOMIC_ACKNOWLEDGE) || diff != 0))) {
1384 /* Retry this request. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001385 if (!(qp->r_flags & RVT_R_RDMAR_SEQ)) {
1386 qp->r_flags |= RVT_R_RDMAR_SEQ;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001387 restart_rc(qp, qp->s_last_psn + 1, 0);
1388 if (list_empty(&qp->rspwait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001389 qp->r_flags |= RVT_R_RSP_SEND;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001390 rvt_get_qp(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001391 list_add_tail(&qp->rspwait,
1392 &rcd->qp_wait_list);
1393 }
1394 }
1395 /*
1396 * No need to process the ACK/NAK since we are
1397 * restarting an earlier request.
1398 */
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001399 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001400 }
1401 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1402 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
1403 u64 *vaddr = wqe->sg_list[0].vaddr;
1404 *vaddr = val;
1405 }
1406 if (qp->s_num_rd_atomic &&
1407 (wqe->wr.opcode == IB_WR_RDMA_READ ||
1408 wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1409 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)) {
1410 qp->s_num_rd_atomic--;
1411 /* Restart sending task if fence is complete */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001412 if ((qp->s_flags & RVT_S_WAIT_FENCE) &&
Mike Marciniszyn77241052015-07-30 15:17:43 -04001413 !qp->s_num_rd_atomic) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001414 qp->s_flags &= ~(RVT_S_WAIT_FENCE |
1415 RVT_S_WAIT_ACK);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001416 hfi1_schedule_send(qp);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001417 } else if (qp->s_flags & RVT_S_WAIT_RDMAR) {
1418 qp->s_flags &= ~(RVT_S_WAIT_RDMAR |
1419 RVT_S_WAIT_ACK);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001420 hfi1_schedule_send(qp);
1421 }
1422 }
1423 wqe = do_rc_completion(qp, wqe, ibp);
1424 if (qp->s_acked == qp->s_tail)
1425 break;
1426 }
1427
1428 switch (aeth >> 29) {
1429 case 0: /* ACK */
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001430 this_cpu_inc(*ibp->rvp.rc_acks);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001431 if (qp->s_acked != qp->s_tail) {
1432 /*
1433 * We are expecting more ACKs so
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001434 * mod the retry timer.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001435 */
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001436 hfi1_mod_retry_timer(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001437 /*
1438 * We can stop re-sending the earlier packets and
1439 * continue with the next packet the receiver wants.
1440 */
1441 if (cmp_psn(qp->s_psn, psn) <= 0)
1442 reset_psn(qp, psn + 1);
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001443 } else {
1444 /* No more acks - kill all timers */
1445 hfi1_stop_rc_timers(qp);
1446 if (cmp_psn(qp->s_psn, psn) <= 0) {
1447 qp->s_state = OP(SEND_LAST);
1448 qp->s_psn = psn + 1;
1449 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001450 }
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001451 if (qp->s_flags & RVT_S_WAIT_ACK) {
1452 qp->s_flags &= ~RVT_S_WAIT_ACK;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001453 hfi1_schedule_send(qp);
1454 }
1455 hfi1_get_credit(qp, aeth);
1456 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1457 qp->s_retry = qp->s_retry_cnt;
1458 update_last_psn(qp, psn);
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001459 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001460
1461 case 1: /* RNR NAK */
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001462 ibp->rvp.n_rnr_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001463 if (qp->s_acked == qp->s_tail)
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001464 goto bail_stop;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001465 if (qp->s_flags & RVT_S_WAIT_RNR)
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001466 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001467 if (qp->s_rnr_retry == 0) {
1468 status = IB_WC_RNR_RETRY_EXC_ERR;
1469 goto class_b;
1470 }
1471 if (qp->s_rnr_retry_cnt < 7)
1472 qp->s_rnr_retry--;
1473
1474 /* The last valid PSN is the previous PSN. */
1475 update_last_psn(qp, psn - 1);
1476
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001477 ibp->rvp.n_rc_resends += delta_psn(qp->s_psn, psn);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001478
1479 reset_psn(qp, psn);
1480
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001481 qp->s_flags &= ~(RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_ACK);
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001482 hfi1_stop_rc_timers(qp);
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001483 to =
Mike Marciniszyn77241052015-07-30 15:17:43 -04001484 ib_hfi1_rnr_table[(aeth >> HFI1_AETH_CREDIT_SHIFT) &
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001485 HFI1_AETH_CREDIT_MASK];
1486 hfi1_add_rnr_timer(qp, to);
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001487 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001488
1489 case 3: /* NAK */
1490 if (qp->s_acked == qp->s_tail)
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001491 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001492 /* The last valid PSN is the previous PSN. */
1493 update_last_psn(qp, psn - 1);
1494 switch ((aeth >> HFI1_AETH_CREDIT_SHIFT) &
1495 HFI1_AETH_CREDIT_MASK) {
1496 case 0: /* PSN sequence error */
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001497 ibp->rvp.n_seq_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001498 /*
1499 * Back up to the responder's expected PSN.
1500 * Note that we might get a NAK in the middle of an
1501 * RDMA READ response which terminates the RDMA
1502 * READ.
1503 */
1504 restart_rc(qp, psn, 0);
1505 hfi1_schedule_send(qp);
1506 break;
1507
1508 case 1: /* Invalid Request */
1509 status = IB_WC_REM_INV_REQ_ERR;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001510 ibp->rvp.n_other_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001511 goto class_b;
1512
1513 case 2: /* Remote Access Error */
1514 status = IB_WC_REM_ACCESS_ERR;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001515 ibp->rvp.n_other_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001516 goto class_b;
1517
1518 case 3: /* Remote Operation Error */
1519 status = IB_WC_REM_OP_ERR;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001520 ibp->rvp.n_other_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001521class_b:
1522 if (qp->s_last == qp->s_acked) {
1523 hfi1_send_complete(qp, wqe, status);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08001524 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001525 }
1526 break;
1527
1528 default:
1529 /* Ignore other reserved NAK error codes */
1530 goto reserved;
1531 }
1532 qp->s_retry = qp->s_retry_cnt;
1533 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001534 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001535
1536 default: /* 2: reserved */
1537reserved:
1538 /* Ignore reserved NAK codes. */
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001539 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001540 }
Mike Marciniszyn87717f02016-04-12 11:28:56 -07001541 /* cannot be reached */
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001542bail_stop:
1543 hfi1_stop_rc_timers(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001544 return ret;
1545}
1546
1547/*
1548 * We have seen an out of sequence RDMA read middle or last packet.
1549 * This ACKs SENDs and RDMA writes up to the first RDMA read or atomic SWQE.
1550 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001551static void rdma_seq_err(struct rvt_qp *qp, struct hfi1_ibport *ibp, u32 psn,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001552 struct hfi1_ctxtdata *rcd)
1553{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001554 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001555
1556 /* Remove QP from retry timer */
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001557 hfi1_stop_rc_timers(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001558
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001559 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001560
1561 while (cmp_psn(psn, wqe->lpsn) > 0) {
1562 if (wqe->wr.opcode == IB_WR_RDMA_READ ||
1563 wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1564 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
1565 break;
1566 wqe = do_rc_completion(qp, wqe, ibp);
1567 }
1568
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001569 ibp->rvp.n_rdma_seq++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001570 qp->r_flags |= RVT_R_RDMAR_SEQ;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001571 restart_rc(qp, qp->s_last_psn + 1, 0);
1572 if (list_empty(&qp->rspwait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001573 qp->r_flags |= RVT_R_RSP_SEND;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001574 rvt_get_qp(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001575 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1576 }
1577}
1578
1579/**
1580 * rc_rcv_resp - process an incoming RC response packet
1581 * @ibp: the port this packet came in on
1582 * @ohdr: the other headers for this packet
1583 * @data: the packet data
1584 * @tlen: the packet length
1585 * @qp: the QP for this packet
1586 * @opcode: the opcode for this packet
1587 * @psn: the packet sequence number for this packet
1588 * @hdrsize: the header length
1589 * @pmtu: the path MTU
1590 *
1591 * This is called from hfi1_rc_rcv() to process an incoming RC response
1592 * packet for the given QP.
1593 * Called at interrupt level.
1594 */
1595static void rc_rcv_resp(struct hfi1_ibport *ibp,
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001596 struct ib_other_headers *ohdr,
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001597 void *data, u32 tlen, struct rvt_qp *qp,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001598 u32 opcode, u32 psn, u32 hdrsize, u32 pmtu,
1599 struct hfi1_ctxtdata *rcd)
1600{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001601 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001602 enum ib_wc_status status;
1603 unsigned long flags;
1604 int diff;
1605 u32 pad;
1606 u32 aeth;
1607 u64 val;
1608
1609 spin_lock_irqsave(&qp->s_lock, flags);
1610
Sebastian Sanchez462b6b22016-07-01 16:01:06 -07001611 trace_hfi1_ack(qp, psn);
Mike Marciniszyn83525b62015-10-26 10:28:48 -04001612
Mike Marciniszyn77241052015-07-30 15:17:43 -04001613 /* Ignore invalid responses. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001614 smp_read_barrier_depends(); /* see post_one_send */
1615 if (cmp_psn(psn, ACCESS_ONCE(qp->s_next_psn)) >= 0)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001616 goto ack_done;
1617
1618 /* Ignore duplicate responses. */
1619 diff = cmp_psn(psn, qp->s_last_psn);
1620 if (unlikely(diff <= 0)) {
1621 /* Update credits for "ghost" ACKs */
1622 if (diff == 0 && opcode == OP(ACKNOWLEDGE)) {
1623 aeth = be32_to_cpu(ohdr->u.aeth);
1624 if ((aeth >> 29) == 0)
1625 hfi1_get_credit(qp, aeth);
1626 }
1627 goto ack_done;
1628 }
1629
1630 /*
1631 * Skip everything other than the PSN we expect, if we are waiting
1632 * for a reply to a restarted RDMA read or atomic op.
1633 */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001634 if (qp->r_flags & RVT_R_RDMAR_SEQ) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001635 if (cmp_psn(psn, qp->s_last_psn + 1) != 0)
1636 goto ack_done;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001637 qp->r_flags &= ~RVT_R_RDMAR_SEQ;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001638 }
1639
1640 if (unlikely(qp->s_acked == qp->s_tail))
1641 goto ack_done;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001642 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001643 status = IB_WC_SUCCESS;
1644
1645 switch (opcode) {
1646 case OP(ACKNOWLEDGE):
1647 case OP(ATOMIC_ACKNOWLEDGE):
1648 case OP(RDMA_READ_RESPONSE_FIRST):
1649 aeth = be32_to_cpu(ohdr->u.aeth);
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001650 if (opcode == OP(ATOMIC_ACKNOWLEDGE))
1651 val = ib_u64_get(&ohdr->u.at.atomic_ack_eth);
1652 else
Mike Marciniszyn77241052015-07-30 15:17:43 -04001653 val = 0;
1654 if (!do_rc_ack(qp, aeth, psn, opcode, val, rcd) ||
1655 opcode != OP(RDMA_READ_RESPONSE_FIRST))
1656 goto ack_done;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001657 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001658 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1659 goto ack_op_err;
1660 /*
1661 * If this is a response to a resent RDMA read, we
1662 * have to be careful to copy the data to the right
1663 * location.
1664 */
1665 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1666 wqe, psn, pmtu);
1667 goto read_middle;
1668
1669 case OP(RDMA_READ_RESPONSE_MIDDLE):
1670 /* no AETH, no ACK */
1671 if (unlikely(cmp_psn(psn, qp->s_last_psn + 1)))
1672 goto ack_seq_err;
1673 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1674 goto ack_op_err;
1675read_middle:
1676 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1677 goto ack_len_err;
1678 if (unlikely(pmtu >= qp->s_rdma_read_len))
1679 goto ack_len_err;
1680
1681 /*
1682 * We got a response so update the timeout.
1683 * 4.096 usec. * (1 << qp->timeout)
1684 */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001685 qp->s_flags |= RVT_S_TIMER;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001686 mod_timer(&qp->s_timer, jiffies + qp->timeout_jiffies);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001687 if (qp->s_flags & RVT_S_WAIT_ACK) {
1688 qp->s_flags &= ~RVT_S_WAIT_ACK;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001689 hfi1_schedule_send(qp);
1690 }
1691
1692 if (opcode == OP(RDMA_READ_RESPONSE_MIDDLE))
1693 qp->s_retry = qp->s_retry_cnt;
1694
1695 /*
1696 * Update the RDMA receive state but do the copy w/o
1697 * holding the locks and blocking interrupts.
1698 */
1699 qp->s_rdma_read_len -= pmtu;
1700 update_last_psn(qp, psn);
1701 spin_unlock_irqrestore(&qp->s_lock, flags);
Dean Luick7b0b01a2016-02-03 14:35:49 -08001702 hfi1_copy_sge(&qp->s_rdma_read_sge, data, pmtu, 0, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001703 goto bail;
1704
1705 case OP(RDMA_READ_RESPONSE_ONLY):
1706 aeth = be32_to_cpu(ohdr->u.aeth);
1707 if (!do_rc_ack(qp, aeth, psn, opcode, 0, rcd))
1708 goto ack_done;
1709 /* Get the number of bytes the message was padded by. */
1710 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1711 /*
1712 * Check that the data size is >= 0 && <= pmtu.
1713 * Remember to account for ICRC (4).
1714 */
1715 if (unlikely(tlen < (hdrsize + pad + 4)))
1716 goto ack_len_err;
1717 /*
1718 * If this is a response to a resent RDMA read, we
1719 * have to be careful to copy the data to the right
1720 * location.
1721 */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001722 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001723 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1724 wqe, psn, pmtu);
1725 goto read_last;
1726
1727 case OP(RDMA_READ_RESPONSE_LAST):
1728 /* ACKs READ req. */
1729 if (unlikely(cmp_psn(psn, qp->s_last_psn + 1)))
1730 goto ack_seq_err;
1731 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1732 goto ack_op_err;
1733 /* Get the number of bytes the message was padded by. */
1734 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1735 /*
1736 * Check that the data size is >= 1 && <= pmtu.
1737 * Remember to account for ICRC (4).
1738 */
1739 if (unlikely(tlen <= (hdrsize + pad + 4)))
1740 goto ack_len_err;
1741read_last:
1742 tlen -= hdrsize + pad + 4;
1743 if (unlikely(tlen != qp->s_rdma_read_len))
1744 goto ack_len_err;
1745 aeth = be32_to_cpu(ohdr->u.aeth);
Dean Luick7b0b01a2016-02-03 14:35:49 -08001746 hfi1_copy_sge(&qp->s_rdma_read_sge, data, tlen, 0, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001747 WARN_ON(qp->s_rdma_read_sge.num_sge);
Jubin John50e5dcb2016-02-14 20:19:41 -08001748 (void)do_rc_ack(qp, aeth, psn,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001749 OP(RDMA_READ_RESPONSE_LAST), 0, rcd);
1750 goto ack_done;
1751 }
1752
1753ack_op_err:
1754 status = IB_WC_LOC_QP_OP_ERR;
1755 goto ack_err;
1756
1757ack_seq_err:
1758 rdma_seq_err(qp, ibp, psn, rcd);
1759 goto ack_done;
1760
1761ack_len_err:
1762 status = IB_WC_LOC_LEN_ERR;
1763ack_err:
1764 if (qp->s_last == qp->s_acked) {
1765 hfi1_send_complete(qp, wqe, status);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08001766 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001767 }
1768ack_done:
1769 spin_unlock_irqrestore(&qp->s_lock, flags);
1770bail:
1771 return;
1772}
1773
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05001774static inline void rc_defered_ack(struct hfi1_ctxtdata *rcd,
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001775 struct rvt_qp *qp)
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05001776{
1777 if (list_empty(&qp->rspwait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001778 qp->r_flags |= RVT_R_RSP_NAK;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001779 rvt_get_qp(qp);
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05001780 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1781 }
1782}
1783
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001784static inline void rc_cancel_ack(struct rvt_qp *qp)
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05001785{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08001786 struct hfi1_qp_priv *priv = qp->priv;
1787
1788 priv->r_adefered = 0;
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05001789 if (list_empty(&qp->rspwait))
1790 return;
1791 list_del_init(&qp->rspwait);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001792 qp->r_flags &= ~RVT_R_RSP_NAK;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001793 rvt_put_qp(qp);
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05001794}
1795
Mike Marciniszyn77241052015-07-30 15:17:43 -04001796/**
1797 * rc_rcv_error - process an incoming duplicate or error RC packet
1798 * @ohdr: the other headers for this packet
1799 * @data: the packet data
1800 * @qp: the QP for this packet
1801 * @opcode: the opcode for this packet
1802 * @psn: the packet sequence number for this packet
1803 * @diff: the difference between the PSN and the expected PSN
1804 *
1805 * This is called from hfi1_rc_rcv() to process an unexpected
1806 * incoming RC packet for the given QP.
1807 * Called at interrupt level.
1808 * Return 1 if no more processing is needed; otherwise return 0 to
1809 * schedule a response to be sent.
1810 */
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001811static noinline int rc_rcv_error(struct ib_other_headers *ohdr, void *data,
Jubin John17fb4f22016-02-14 20:21:52 -08001812 struct rvt_qp *qp, u32 opcode, u32 psn,
1813 int diff, struct hfi1_ctxtdata *rcd)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001814{
1815 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001816 struct rvt_ack_entry *e;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001817 unsigned long flags;
1818 u8 i, prev;
1819 int old_req;
1820
Sebastian Sanchez462b6b22016-07-01 16:01:06 -07001821 trace_hfi1_rcv_error(qp, psn);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001822 if (diff > 0) {
1823 /*
1824 * Packet sequence error.
1825 * A NAK will ACK earlier sends and RDMA writes.
1826 * Don't queue the NAK if we already sent one.
1827 */
1828 if (!qp->r_nak_state) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001829 ibp->rvp.n_rc_seqnak++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001830 qp->r_nak_state = IB_NAK_PSN_ERROR;
1831 /* Use the expected PSN. */
1832 qp->r_ack_psn = qp->r_psn;
1833 /*
1834 * Wait to send the sequence NAK until all packets
1835 * in the receive queue have been processed.
1836 * Otherwise, we end up propagating congestion.
1837 */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05001838 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001839 }
1840 goto done;
1841 }
1842
1843 /*
1844 * Handle a duplicate request. Don't re-execute SEND, RDMA
1845 * write or atomic op. Don't NAK errors, just silently drop
1846 * the duplicate request. Note that r_sge, r_len, and
1847 * r_rcv_len may be in use so don't modify them.
1848 *
1849 * We are supposed to ACK the earliest duplicate PSN but we
1850 * can coalesce an outstanding duplicate ACK. We have to
1851 * send the earliest so that RDMA reads can be restarted at
1852 * the requester's expected PSN.
1853 *
1854 * First, find where this duplicate PSN falls within the
1855 * ACKs previously sent.
1856 * old_req is true if there is an older response that is scheduled
1857 * to be sent before sending this one.
1858 */
1859 e = NULL;
1860 old_req = 1;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001861 ibp->rvp.n_rc_dupreq++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001862
1863 spin_lock_irqsave(&qp->s_lock, flags);
1864
1865 for (i = qp->r_head_ack_queue; ; i = prev) {
1866 if (i == qp->s_tail_ack_queue)
1867 old_req = 0;
1868 if (i)
1869 prev = i - 1;
1870 else
1871 prev = HFI1_MAX_RDMA_ATOMIC;
1872 if (prev == qp->r_head_ack_queue) {
1873 e = NULL;
1874 break;
1875 }
1876 e = &qp->s_ack_queue[prev];
1877 if (!e->opcode) {
1878 e = NULL;
1879 break;
1880 }
1881 if (cmp_psn(psn, e->psn) >= 0) {
1882 if (prev == qp->s_tail_ack_queue &&
1883 cmp_psn(psn, e->lpsn) <= 0)
1884 old_req = 0;
1885 break;
1886 }
1887 }
1888 switch (opcode) {
1889 case OP(RDMA_READ_REQUEST): {
1890 struct ib_reth *reth;
1891 u32 offset;
1892 u32 len;
1893
1894 /*
1895 * If we didn't find the RDMA read request in the ack queue,
1896 * we can ignore this request.
1897 */
1898 if (!e || e->opcode != OP(RDMA_READ_REQUEST))
1899 goto unlock_done;
1900 /* RETH comes after BTH */
1901 reth = &ohdr->u.rc.reth;
1902 /*
1903 * Address range must be a subset of the original
1904 * request and start on pmtu boundaries.
1905 * We reuse the old ack_queue slot since the requester
1906 * should not back up and request an earlier PSN for the
1907 * same request.
1908 */
1909 offset = delta_psn(psn, e->psn) * qp->pmtu;
1910 len = be32_to_cpu(reth->length);
1911 if (unlikely(offset + len != e->rdma_sge.sge_length))
1912 goto unlock_done;
1913 if (e->rdma_sge.mr) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001914 rvt_put_mr(e->rdma_sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001915 e->rdma_sge.mr = NULL;
1916 }
1917 if (len != 0) {
1918 u32 rkey = be32_to_cpu(reth->rkey);
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001919 u64 vaddr = get_ib_reth_vaddr(reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001920 int ok;
1921
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001922 ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr, rkey,
1923 IB_ACCESS_REMOTE_READ);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001924 if (unlikely(!ok))
1925 goto unlock_done;
1926 } else {
1927 e->rdma_sge.vaddr = NULL;
1928 e->rdma_sge.length = 0;
1929 e->rdma_sge.sge_length = 0;
1930 }
1931 e->psn = psn;
1932 if (old_req)
1933 goto unlock_done;
1934 qp->s_tail_ack_queue = prev;
1935 break;
1936 }
1937
1938 case OP(COMPARE_SWAP):
1939 case OP(FETCH_ADD): {
1940 /*
1941 * If we didn't find the atomic request in the ack queue
1942 * or the send tasklet is already backed up to send an
1943 * earlier entry, we can ignore this request.
1944 */
Jubin John50e5dcb2016-02-14 20:19:41 -08001945 if (!e || e->opcode != (u8)opcode || old_req)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001946 goto unlock_done;
1947 qp->s_tail_ack_queue = prev;
1948 break;
1949 }
1950
1951 default:
1952 /*
1953 * Ignore this operation if it doesn't request an ACK
1954 * or an earlier RDMA read or atomic is going to be resent.
1955 */
1956 if (!(psn & IB_BTH_REQ_ACK) || old_req)
1957 goto unlock_done;
1958 /*
1959 * Resend the most recent ACK if this request is
1960 * after all the previous RDMA reads and atomics.
1961 */
1962 if (i == qp->r_head_ack_queue) {
1963 spin_unlock_irqrestore(&qp->s_lock, flags);
1964 qp->r_nak_state = 0;
1965 qp->r_ack_psn = qp->r_psn - 1;
1966 goto send_ack;
1967 }
1968
1969 /*
1970 * Resend the RDMA read or atomic op which
1971 * ACKs this duplicate request.
1972 */
1973 qp->s_tail_ack_queue = i;
1974 break;
1975 }
1976 qp->s_ack_state = OP(ACKNOWLEDGE);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001977 qp->s_flags |= RVT_S_RESP_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001978 qp->r_nak_state = 0;
1979 hfi1_schedule_send(qp);
1980
1981unlock_done:
1982 spin_unlock_irqrestore(&qp->s_lock, flags);
1983done:
1984 return 1;
1985
1986send_ack:
1987 return 0;
1988}
1989
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001990void hfi1_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001991{
1992 unsigned long flags;
1993 int lastwqe;
1994
1995 spin_lock_irqsave(&qp->s_lock, flags);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08001996 lastwqe = rvt_error_qp(qp, err);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001997 spin_unlock_irqrestore(&qp->s_lock, flags);
1998
1999 if (lastwqe) {
2000 struct ib_event ev;
2001
2002 ev.device = qp->ibqp.device;
2003 ev.element.qp = &qp->ibqp;
2004 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
2005 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
2006 }
2007}
2008
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002009static inline void update_ack_queue(struct rvt_qp *qp, unsigned n)
Mike Marciniszyn77241052015-07-30 15:17:43 -04002010{
2011 unsigned next;
2012
2013 next = n + 1;
2014 if (next > HFI1_MAX_RDMA_ATOMIC)
2015 next = 0;
2016 qp->s_tail_ack_queue = next;
2017 qp->s_ack_state = OP(ACKNOWLEDGE);
2018}
2019
2020static void log_cca_event(struct hfi1_pportdata *ppd, u8 sl, u32 rlid,
2021 u32 lqpn, u32 rqpn, u8 svc_type)
2022{
2023 struct opa_hfi1_cong_log_event_internal *cc_event;
Dean Luickb77d7132015-10-26 10:28:43 -04002024 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002025
2026 if (sl >= OPA_MAX_SLS)
2027 return;
2028
Dean Luickb77d7132015-10-26 10:28:43 -04002029 spin_lock_irqsave(&ppd->cc_log_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002030
Jubin John8638b772016-02-14 20:19:24 -08002031 ppd->threshold_cong_event_map[sl / 8] |= 1 << (sl % 8);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002032 ppd->threshold_event_counter++;
2033
2034 cc_event = &ppd->cc_events[ppd->cc_log_idx++];
2035 if (ppd->cc_log_idx == OPA_CONG_LOG_ELEMS)
2036 ppd->cc_log_idx = 0;
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002037 cc_event->lqpn = lqpn & RVT_QPN_MASK;
2038 cc_event->rqpn = rqpn & RVT_QPN_MASK;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002039 cc_event->sl = sl;
2040 cc_event->svc_type = svc_type;
2041 cc_event->rlid = rlid;
2042 /* keep timestamp in units of 1.024 usec */
2043 cc_event->timestamp = ktime_to_ns(ktime_get()) / 1024;
2044
Dean Luickb77d7132015-10-26 10:28:43 -04002045 spin_unlock_irqrestore(&ppd->cc_log_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002046}
2047
2048void process_becn(struct hfi1_pportdata *ppd, u8 sl, u16 rlid, u32 lqpn,
2049 u32 rqpn, u8 svc_type)
2050{
2051 struct cca_timer *cca_timer;
2052 u16 ccti, ccti_incr, ccti_timer, ccti_limit;
2053 u8 trigger_threshold;
2054 struct cc_state *cc_state;
Dean Luickb77d7132015-10-26 10:28:43 -04002055 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002056
2057 if (sl >= OPA_MAX_SLS)
2058 return;
2059
Mike Marciniszyn77241052015-07-30 15:17:43 -04002060 cc_state = get_cc_state(ppd);
2061
Jubin Johnd125a6c2016-02-14 20:19:49 -08002062 if (!cc_state)
Mike Marciniszyn77241052015-07-30 15:17:43 -04002063 return;
2064
2065 /*
2066 * 1) increase CCTI (for this SL)
2067 * 2) select IPG (i.e., call set_link_ipg())
2068 * 3) start timer
2069 */
2070 ccti_limit = cc_state->cct.ccti_limit;
2071 ccti_incr = cc_state->cong_setting.entries[sl].ccti_increase;
2072 ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer;
2073 trigger_threshold =
2074 cc_state->cong_setting.entries[sl].trigger_threshold;
2075
Dean Luickb77d7132015-10-26 10:28:43 -04002076 spin_lock_irqsave(&ppd->cca_timer_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002077
Jubin Johnd35cf7442016-04-14 08:31:53 -07002078 cca_timer = &ppd->cca_timer[sl];
Mike Marciniszyn77241052015-07-30 15:17:43 -04002079 if (cca_timer->ccti < ccti_limit) {
2080 if (cca_timer->ccti + ccti_incr <= ccti_limit)
2081 cca_timer->ccti += ccti_incr;
2082 else
2083 cca_timer->ccti = ccti_limit;
2084 set_link_ipg(ppd);
2085 }
2086
Mike Marciniszyn77241052015-07-30 15:17:43 -04002087 ccti = cca_timer->ccti;
2088
2089 if (!hrtimer_active(&cca_timer->hrtimer)) {
2090 /* ccti_timer is in units of 1.024 usec */
2091 unsigned long nsec = 1024 * ccti_timer;
2092
2093 hrtimer_start(&cca_timer->hrtimer, ns_to_ktime(nsec),
2094 HRTIMER_MODE_REL);
2095 }
2096
Jubin Johnd35cf7442016-04-14 08:31:53 -07002097 spin_unlock_irqrestore(&ppd->cca_timer_lock, flags);
2098
Mike Marciniszyn77241052015-07-30 15:17:43 -04002099 if ((trigger_threshold != 0) && (ccti >= trigger_threshold))
2100 log_cca_event(ppd, sl, rlid, lqpn, rqpn, svc_type);
2101}
2102
2103/**
2104 * hfi1_rc_rcv - process an incoming RC packet
2105 * @rcd: the context pointer
2106 * @hdr: the header of this packet
2107 * @rcv_flags: flags relevant to rcv processing
2108 * @data: the packet data
2109 * @tlen: the packet length
2110 * @qp: the QP for this packet
2111 *
2112 * This is called from qp_rcv() to process an incoming RC packet
2113 * for the given QP.
Dean Luickb77d7132015-10-26 10:28:43 -04002114 * May be called at interrupt level.
Mike Marciniszyn77241052015-07-30 15:17:43 -04002115 */
2116void hfi1_rc_rcv(struct hfi1_packet *packet)
2117{
2118 struct hfi1_ctxtdata *rcd = packet->rcd;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002119 struct ib_header *hdr = packet->hdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002120 u32 rcv_flags = packet->rcv_flags;
2121 void *data = packet->ebuf;
2122 u32 tlen = packet->tlen;
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002123 struct rvt_qp *qp = packet->qp;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002124 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002125 struct ib_other_headers *ohdr = packet->ohdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002126 u32 bth0, opcode;
2127 u32 hdrsize = packet->hlen;
2128 u32 psn;
2129 u32 pad;
2130 struct ib_wc wc;
2131 u32 pmtu = qp->pmtu;
2132 int diff;
2133 struct ib_reth *reth;
2134 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002135 int ret, is_fecn = 0;
Dean Luick7b0b01a2016-02-03 14:35:49 -08002136 int copy_last = 0;
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002137 u32 rkey;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002138
2139 bth0 = be32_to_cpu(ohdr->bth[0]);
2140 if (hfi1_ruc_check_hdr(ibp, hdr, rcv_flags & HFI1_HAS_GRH, qp, bth0))
2141 return;
2142
Mitko Haralanov5fd2b562016-07-25 13:38:07 -07002143 is_fecn = process_ecn(qp, packet, false);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002144
2145 psn = be32_to_cpu(ohdr->bth[2]);
Arthur Kepner977940b2015-11-04 21:10:10 -05002146 opcode = (bth0 >> 24) & 0xff;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002147
2148 /*
2149 * Process responses (ACKs) before anything else. Note that the
2150 * packet sequence number will be for something in the send work
2151 * queue rather than the expected receive packet sequence number.
2152 * In other words, this QP is the requester.
2153 */
2154 if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
2155 opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
2156 rc_rcv_resp(ibp, ohdr, data, tlen, qp, opcode, psn,
2157 hdrsize, pmtu, rcd);
2158 if (is_fecn)
2159 goto send_ack;
2160 return;
2161 }
2162
2163 /* Compute 24 bits worth of difference. */
2164 diff = delta_psn(psn, qp->r_psn);
2165 if (unlikely(diff)) {
2166 if (rc_rcv_error(ohdr, data, qp, opcode, psn, diff, rcd))
2167 return;
2168 goto send_ack;
2169 }
2170
2171 /* Check for opcode sequence errors. */
2172 switch (qp->r_state) {
2173 case OP(SEND_FIRST):
2174 case OP(SEND_MIDDLE):
2175 if (opcode == OP(SEND_MIDDLE) ||
2176 opcode == OP(SEND_LAST) ||
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002177 opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
2178 opcode == OP(SEND_LAST_WITH_INVALIDATE))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002179 break;
2180 goto nack_inv;
2181
2182 case OP(RDMA_WRITE_FIRST):
2183 case OP(RDMA_WRITE_MIDDLE):
2184 if (opcode == OP(RDMA_WRITE_MIDDLE) ||
2185 opcode == OP(RDMA_WRITE_LAST) ||
2186 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
2187 break;
2188 goto nack_inv;
2189
2190 default:
2191 if (opcode == OP(SEND_MIDDLE) ||
2192 opcode == OP(SEND_LAST) ||
2193 opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002194 opcode == OP(SEND_LAST_WITH_INVALIDATE) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04002195 opcode == OP(RDMA_WRITE_MIDDLE) ||
2196 opcode == OP(RDMA_WRITE_LAST) ||
2197 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
2198 goto nack_inv;
2199 /*
2200 * Note that it is up to the requester to not send a new
2201 * RDMA read or atomic operation before receiving an ACK
2202 * for the previous operation.
2203 */
2204 break;
2205 }
2206
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08002207 if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002208 qp_comm_est(qp);
2209
2210 /* OK, process the packet. */
2211 switch (opcode) {
2212 case OP(SEND_FIRST):
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002213 ret = hfi1_rvt_get_rwqe(qp, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002214 if (ret < 0)
2215 goto nack_op_err;
2216 if (!ret)
2217 goto rnr_nak;
2218 qp->r_rcv_len = 0;
2219 /* FALLTHROUGH */
2220 case OP(SEND_MIDDLE):
2221 case OP(RDMA_WRITE_MIDDLE):
2222send_middle:
2223 /* Check for invalid length PMTU or posted rwqe len. */
2224 if (unlikely(tlen != (hdrsize + pmtu + 4)))
2225 goto nack_inv;
2226 qp->r_rcv_len += pmtu;
2227 if (unlikely(qp->r_rcv_len > qp->r_len))
2228 goto nack_inv;
Dean Luick7b0b01a2016-02-03 14:35:49 -08002229 hfi1_copy_sge(&qp->r_sge, data, pmtu, 1, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002230 break;
2231
2232 case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
2233 /* consume RWQE */
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002234 ret = hfi1_rvt_get_rwqe(qp, 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002235 if (ret < 0)
2236 goto nack_op_err;
2237 if (!ret)
2238 goto rnr_nak;
2239 goto send_last_imm;
2240
2241 case OP(SEND_ONLY):
2242 case OP(SEND_ONLY_WITH_IMMEDIATE):
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002243 case OP(SEND_ONLY_WITH_INVALIDATE):
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002244 ret = hfi1_rvt_get_rwqe(qp, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002245 if (ret < 0)
2246 goto nack_op_err;
2247 if (!ret)
2248 goto rnr_nak;
2249 qp->r_rcv_len = 0;
2250 if (opcode == OP(SEND_ONLY))
2251 goto no_immediate_data;
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002252 if (opcode == OP(SEND_ONLY_WITH_INVALIDATE))
2253 goto send_last_inv;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002254 /* FALLTHROUGH for SEND_ONLY_WITH_IMMEDIATE */
2255 case OP(SEND_LAST_WITH_IMMEDIATE):
2256send_last_imm:
2257 wc.ex.imm_data = ohdr->u.imm_data;
2258 wc.wc_flags = IB_WC_WITH_IMM;
2259 goto send_last;
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002260 case OP(SEND_LAST_WITH_INVALIDATE):
2261send_last_inv:
2262 rkey = be32_to_cpu(ohdr->u.ieth);
2263 if (rvt_invalidate_rkey(qp, rkey))
2264 goto no_immediate_data;
2265 wc.ex.invalidate_rkey = rkey;
2266 wc.wc_flags = IB_WC_WITH_INVALIDATE;
2267 goto send_last;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002268 case OP(RDMA_WRITE_LAST):
Dean Luick7b0b01a2016-02-03 14:35:49 -08002269 copy_last = ibpd_to_rvtpd(qp->ibqp.pd)->user;
2270 /* fall through */
2271 case OP(SEND_LAST):
Mike Marciniszyn77241052015-07-30 15:17:43 -04002272no_immediate_data:
2273 wc.wc_flags = 0;
2274 wc.ex.imm_data = 0;
2275send_last:
2276 /* Get the number of bytes the message was padded by. */
2277 pad = (bth0 >> 20) & 3;
2278 /* Check for invalid length. */
2279 /* LAST len should be >= 1 */
2280 if (unlikely(tlen < (hdrsize + pad + 4)))
2281 goto nack_inv;
2282 /* Don't count the CRC. */
2283 tlen -= (hdrsize + pad + 4);
2284 wc.byte_len = tlen + qp->r_rcv_len;
2285 if (unlikely(wc.byte_len > qp->r_len))
2286 goto nack_inv;
Dean Luick7b0b01a2016-02-03 14:35:49 -08002287 hfi1_copy_sge(&qp->r_sge, data, tlen, 1, copy_last);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002288 rvt_put_ss(&qp->r_sge);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002289 qp->r_msn++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08002290 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002291 break;
2292 wc.wr_id = qp->r_wr_id;
2293 wc.status = IB_WC_SUCCESS;
2294 if (opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE) ||
2295 opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
2296 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
2297 else
2298 wc.opcode = IB_WC_RECV;
2299 wc.qp = &qp->ibqp;
2300 wc.src_qp = qp->remote_qpn;
2301 wc.slid = qp->remote_ah_attr.dlid;
2302 /*
2303 * It seems that IB mandates the presence of an SL in a
2304 * work completion only for the UD transport (see section
2305 * 11.4.2 of IBTA Vol. 1).
2306 *
2307 * However, the way the SL is chosen below is consistent
2308 * with the way that IB/qib works and is trying avoid
2309 * introducing incompatibilities.
2310 *
2311 * See also OPA Vol. 1, section 9.7.6, and table 9-17.
2312 */
2313 wc.sl = qp->remote_ah_attr.sl;
2314 /* zero fields that are N/A */
2315 wc.vendor_err = 0;
2316 wc.pkey_index = 0;
2317 wc.dlid_path_bits = 0;
2318 wc.port_num = 0;
2319 /* Signal completion event if the solicited bit is set. */
Dennis Dalessandroabd712d2016-01-19 14:43:22 -08002320 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
2321 (bth0 & IB_BTH_SOLICITED) != 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002322 break;
2323
Mike Marciniszyn77241052015-07-30 15:17:43 -04002324 case OP(RDMA_WRITE_ONLY):
Dean Luick7b0b01a2016-02-03 14:35:49 -08002325 copy_last = 1;
2326 /* fall through */
2327 case OP(RDMA_WRITE_FIRST):
Mike Marciniszyn77241052015-07-30 15:17:43 -04002328 case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
2329 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
2330 goto nack_inv;
2331 /* consume RWQE */
2332 reth = &ohdr->u.rc.reth;
2333 qp->r_len = be32_to_cpu(reth->length);
2334 qp->r_rcv_len = 0;
2335 qp->r_sge.sg_list = NULL;
2336 if (qp->r_len != 0) {
2337 u32 rkey = be32_to_cpu(reth->rkey);
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002338 u64 vaddr = get_ib_reth_vaddr(reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002339 int ok;
2340
2341 /* Check rkey & NAK */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002342 ok = rvt_rkey_ok(qp, &qp->r_sge.sge, qp->r_len, vaddr,
2343 rkey, IB_ACCESS_REMOTE_WRITE);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002344 if (unlikely(!ok))
2345 goto nack_acc;
2346 qp->r_sge.num_sge = 1;
2347 } else {
2348 qp->r_sge.num_sge = 0;
2349 qp->r_sge.sge.mr = NULL;
2350 qp->r_sge.sge.vaddr = NULL;
2351 qp->r_sge.sge.length = 0;
2352 qp->r_sge.sge.sge_length = 0;
2353 }
2354 if (opcode == OP(RDMA_WRITE_FIRST))
2355 goto send_middle;
2356 else if (opcode == OP(RDMA_WRITE_ONLY))
2357 goto no_immediate_data;
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002358 ret = hfi1_rvt_get_rwqe(qp, 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002359 if (ret < 0)
2360 goto nack_op_err;
2361 if (!ret)
2362 goto rnr_nak;
2363 wc.ex.imm_data = ohdr->u.rc.imm_data;
2364 wc.wc_flags = IB_WC_WITH_IMM;
2365 goto send_last;
2366
2367 case OP(RDMA_READ_REQUEST): {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002368 struct rvt_ack_entry *e;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002369 u32 len;
2370 u8 next;
2371
2372 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
2373 goto nack_inv;
2374 next = qp->r_head_ack_queue + 1;
2375 /* s_ack_queue is size HFI1_MAX_RDMA_ATOMIC+1 so use > not >= */
2376 if (next > HFI1_MAX_RDMA_ATOMIC)
2377 next = 0;
2378 spin_lock_irqsave(&qp->s_lock, flags);
2379 if (unlikely(next == qp->s_tail_ack_queue)) {
2380 if (!qp->s_ack_queue[next].sent)
2381 goto nack_inv_unlck;
2382 update_ack_queue(qp, next);
2383 }
2384 e = &qp->s_ack_queue[qp->r_head_ack_queue];
2385 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002386 rvt_put_mr(e->rdma_sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002387 e->rdma_sge.mr = NULL;
2388 }
2389 reth = &ohdr->u.rc.reth;
2390 len = be32_to_cpu(reth->length);
2391 if (len) {
2392 u32 rkey = be32_to_cpu(reth->rkey);
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002393 u64 vaddr = get_ib_reth_vaddr(reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002394 int ok;
2395
2396 /* Check rkey & NAK */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002397 ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr,
2398 rkey, IB_ACCESS_REMOTE_READ);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002399 if (unlikely(!ok))
2400 goto nack_acc_unlck;
2401 /*
2402 * Update the next expected PSN. We add 1 later
2403 * below, so only add the remainder here.
2404 */
2405 if (len > pmtu)
2406 qp->r_psn += (len - 1) / pmtu;
2407 } else {
2408 e->rdma_sge.mr = NULL;
2409 e->rdma_sge.vaddr = NULL;
2410 e->rdma_sge.length = 0;
2411 e->rdma_sge.sge_length = 0;
2412 }
2413 e->opcode = opcode;
2414 e->sent = 0;
2415 e->psn = psn;
2416 e->lpsn = qp->r_psn;
2417 /*
2418 * We need to increment the MSN here instead of when we
2419 * finish sending the result since a duplicate request would
2420 * increment it more than once.
2421 */
2422 qp->r_msn++;
2423 qp->r_psn++;
2424 qp->r_state = opcode;
2425 qp->r_nak_state = 0;
2426 qp->r_head_ack_queue = next;
2427
2428 /* Schedule the send tasklet. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08002429 qp->s_flags |= RVT_S_RESP_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002430 hfi1_schedule_send(qp);
2431
2432 spin_unlock_irqrestore(&qp->s_lock, flags);
2433 if (is_fecn)
2434 goto send_ack;
2435 return;
2436 }
2437
2438 case OP(COMPARE_SWAP):
2439 case OP(FETCH_ADD): {
2440 struct ib_atomic_eth *ateth;
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002441 struct rvt_ack_entry *e;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002442 u64 vaddr;
2443 atomic64_t *maddr;
2444 u64 sdata;
2445 u32 rkey;
2446 u8 next;
2447
2448 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
2449 goto nack_inv;
2450 next = qp->r_head_ack_queue + 1;
2451 if (next > HFI1_MAX_RDMA_ATOMIC)
2452 next = 0;
2453 spin_lock_irqsave(&qp->s_lock, flags);
2454 if (unlikely(next == qp->s_tail_ack_queue)) {
2455 if (!qp->s_ack_queue[next].sent)
2456 goto nack_inv_unlck;
2457 update_ack_queue(qp, next);
2458 }
2459 e = &qp->s_ack_queue[qp->r_head_ack_queue];
2460 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002461 rvt_put_mr(e->rdma_sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002462 e->rdma_sge.mr = NULL;
2463 }
2464 ateth = &ohdr->u.atomic_eth;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002465 vaddr = get_ib_ateth_vaddr(ateth);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002466 if (unlikely(vaddr & (sizeof(u64) - 1)))
2467 goto nack_inv_unlck;
2468 rkey = be32_to_cpu(ateth->rkey);
2469 /* Check rkey & NAK */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002470 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
2471 vaddr, rkey,
2472 IB_ACCESS_REMOTE_ATOMIC)))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002473 goto nack_acc_unlck;
2474 /* Perform atomic OP and save result. */
Jubin John50e5dcb2016-02-14 20:19:41 -08002475 maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002476 sdata = get_ib_ateth_swap(ateth);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002477 e->atomic_data = (opcode == OP(FETCH_ADD)) ?
Jubin John50e5dcb2016-02-14 20:19:41 -08002478 (u64)atomic64_add_return(sdata, maddr) - sdata :
2479 (u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002480 get_ib_ateth_compare(ateth),
Mike Marciniszyn77241052015-07-30 15:17:43 -04002481 sdata);
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002482 rvt_put_mr(qp->r_sge.sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002483 qp->r_sge.num_sge = 0;
2484 e->opcode = opcode;
2485 e->sent = 0;
2486 e->psn = psn;
2487 e->lpsn = psn;
2488 qp->r_msn++;
2489 qp->r_psn++;
2490 qp->r_state = opcode;
2491 qp->r_nak_state = 0;
2492 qp->r_head_ack_queue = next;
2493
2494 /* Schedule the send tasklet. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08002495 qp->s_flags |= RVT_S_RESP_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002496 hfi1_schedule_send(qp);
2497
2498 spin_unlock_irqrestore(&qp->s_lock, flags);
2499 if (is_fecn)
2500 goto send_ack;
2501 return;
2502 }
2503
2504 default:
2505 /* NAK unknown opcodes. */
2506 goto nack_inv;
2507 }
2508 qp->r_psn++;
2509 qp->r_state = opcode;
2510 qp->r_ack_psn = psn;
2511 qp->r_nak_state = 0;
2512 /* Send an ACK if requested or required. */
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05002513 if (psn & IB_BTH_REQ_ACK) {
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08002514 struct hfi1_qp_priv *priv = qp->priv;
2515
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05002516 if (packet->numpkt == 0) {
2517 rc_cancel_ack(qp);
2518 goto send_ack;
2519 }
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08002520 if (priv->r_adefered >= HFI1_PSN_CREDIT) {
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05002521 rc_cancel_ack(qp);
2522 goto send_ack;
2523 }
2524 if (unlikely(is_fecn)) {
2525 rc_cancel_ack(qp);
2526 goto send_ack;
2527 }
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08002528 priv->r_adefered++;
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05002529 rc_defered_ack(rcd, qp);
2530 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04002531 return;
2532
2533rnr_nak:
Harish Chegondibf640092016-03-05 08:49:29 -08002534 qp->r_nak_state = qp->r_min_rnr_timer | IB_RNR_NAK;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002535 qp->r_ack_psn = qp->r_psn;
2536 /* Queue RNR NAK for later */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05002537 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002538 return;
2539
2540nack_op_err:
2541 hfi1_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
2542 qp->r_nak_state = IB_NAK_REMOTE_OPERATIONAL_ERROR;
2543 qp->r_ack_psn = qp->r_psn;
2544 /* Queue NAK for later */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05002545 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002546 return;
2547
2548nack_inv_unlck:
2549 spin_unlock_irqrestore(&qp->s_lock, flags);
2550nack_inv:
2551 hfi1_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
2552 qp->r_nak_state = IB_NAK_INVALID_REQUEST;
2553 qp->r_ack_psn = qp->r_psn;
2554 /* Queue NAK for later */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05002555 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002556 return;
2557
2558nack_acc_unlck:
2559 spin_unlock_irqrestore(&qp->s_lock, flags);
2560nack_acc:
2561 hfi1_rc_error(qp, IB_WC_LOC_PROT_ERR);
2562 qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
2563 qp->r_ack_psn = qp->r_psn;
2564send_ack:
2565 hfi1_send_rc_ack(rcd, qp, is_fecn);
2566}
2567
2568void hfi1_rc_hdrerr(
2569 struct hfi1_ctxtdata *rcd,
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002570 struct ib_header *hdr,
Mike Marciniszyn77241052015-07-30 15:17:43 -04002571 u32 rcv_flags,
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002572 struct rvt_qp *qp)
Mike Marciniszyn77241052015-07-30 15:17:43 -04002573{
2574 int has_grh = rcv_flags & HFI1_HAS_GRH;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002575 struct ib_other_headers *ohdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002576 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
2577 int diff;
Nicolas Iooss49c32032015-09-20 16:07:15 +02002578 u32 opcode;
Arthur Kepner977940b2015-11-04 21:10:10 -05002579 u32 psn, bth0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002580
2581 /* Check for GRH */
2582 ohdr = &hdr->u.oth;
2583 if (has_grh)
2584 ohdr = &hdr->u.l.oth;
2585
Arthur Kepner977940b2015-11-04 21:10:10 -05002586 bth0 = be32_to_cpu(ohdr->bth[0]);
2587 if (hfi1_ruc_check_hdr(ibp, hdr, has_grh, qp, bth0))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002588 return;
2589
2590 psn = be32_to_cpu(ohdr->bth[2]);
Arthur Kepner977940b2015-11-04 21:10:10 -05002591 opcode = (bth0 >> 24) & 0xff;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002592
2593 /* Only deal with RDMA Writes for now */
2594 if (opcode < IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST) {
2595 diff = delta_psn(psn, qp->r_psn);
2596 if (!qp->r_nak_state && diff >= 0) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08002597 ibp->rvp.n_rc_seqnak++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002598 qp->r_nak_state = IB_NAK_PSN_ERROR;
2599 /* Use the expected PSN. */
2600 qp->r_ack_psn = qp->r_psn;
2601 /*
2602 * Wait to send the sequence
2603 * NAK until all packets
2604 * in the receive queue have
2605 * been processed.
2606 * Otherwise, we end up
2607 * propagating congestion.
2608 */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05002609 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002610 } /* Out of sequence NAK */
2611 } /* QP Request NAKs */
2612}