blob: db6396752a979fbb54f099d3effc83f05ca493eb [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 Marciniszyn68e78b32016-09-06 04:37:41 -070071 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080072 qp->s_flags |= RVT_S_TIMER;
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080073 /* 4.096 usec. * (1 << qp->timeout) */
Vennila Megavannanbfee5e32016-02-09 14:29:49 -080074 qp->s_timer.expires = jiffies + qp->timeout_jiffies +
75 rdi->busy_jiffies;
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080076 add_timer(&qp->s_timer);
77}
78
79/**
80 * hfi1_add_rnr_timer - add/start an rnr timer
81 * @qp - the QP
82 * @to - timeout in usecs
83 *
84 * add an rnr timer on the QP
85 */
Mike Marciniszyn34cee282016-02-09 14:29:31 -080086void hfi1_add_rnr_timer(struct rvt_qp *qp, u32 to)
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080087{
Mike Marciniszyn08279d52016-02-04 10:59:36 -080088 struct hfi1_qp_priv *priv = qp->priv;
89
Mike Marciniszyn68e78b32016-09-06 04:37:41 -070090 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080091 qp->s_flags |= RVT_S_WAIT_RNR;
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080092 qp->s_timer.expires = jiffies + usecs_to_jiffies(to);
Mike Marciniszyn08279d52016-02-04 10:59:36 -080093 add_timer(&priv->s_rnr_timer);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080094}
95
96/**
97 * hfi1_mod_retry_timer - mod a retry timer
98 * @qp - the QP
99 *
100 * Modify a potentially already running retry
101 * timer
102 */
103static inline void hfi1_mod_retry_timer(struct rvt_qp *qp)
104{
Vennila Megavannanbfee5e32016-02-09 14:29:49 -0800105 struct ib_qp *ibqp = &qp->ibqp;
106 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
107
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700108 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800109 qp->s_flags |= RVT_S_TIMER;
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800110 /* 4.096 usec. * (1 << qp->timeout) */
Vennila Megavannanbfee5e32016-02-09 14:29:49 -0800111 mod_timer(&qp->s_timer, jiffies + qp->timeout_jiffies +
112 rdi->busy_jiffies);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800113}
114
115/**
116 * hfi1_stop_retry_timer - stop a retry timer
117 * @qp - the QP
118 *
119 * stop a retry timer and return if the timer
120 * had been pending.
121 */
122static inline int hfi1_stop_retry_timer(struct rvt_qp *qp)
123{
124 int rval = 0;
125
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700126 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800127 /* Remove QP from retry */
128 if (qp->s_flags & RVT_S_TIMER) {
129 qp->s_flags &= ~RVT_S_TIMER;
130 rval = del_timer(&qp->s_timer);
131 }
132 return rval;
133}
134
135/**
136 * hfi1_stop_rc_timers - stop all timers
137 * @qp - the QP
138 *
139 * stop any pending timers
140 */
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800141void hfi1_stop_rc_timers(struct rvt_qp *qp)
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800142{
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800143 struct hfi1_qp_priv *priv = qp->priv;
144
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700145 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800146 /* Remove QP from all timers */
147 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
148 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
149 del_timer(&qp->s_timer);
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800150 del_timer(&priv->s_rnr_timer);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800151 }
152}
153
154/**
155 * hfi1_stop_rnr_timer - stop an rnr timer
156 * @qp - the QP
157 *
158 * stop an rnr timer and return if the timer
159 * had been pending.
160 */
161static inline int hfi1_stop_rnr_timer(struct rvt_qp *qp)
162{
163 int rval = 0;
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800164 struct hfi1_qp_priv *priv = qp->priv;
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800165
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700166 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800167 /* Remove QP from rnr timer */
168 if (qp->s_flags & RVT_S_WAIT_RNR) {
169 qp->s_flags &= ~RVT_S_WAIT_RNR;
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800170 rval = del_timer(&priv->s_rnr_timer);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800171 }
172 return rval;
173}
174
175/**
176 * hfi1_del_timers_sync - wait for any timeout routines to exit
177 * @qp - the QP
178 */
Mike Marciniszyn3c9d1492016-02-04 10:59:27 -0800179void hfi1_del_timers_sync(struct rvt_qp *qp)
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800180{
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800181 struct hfi1_qp_priv *priv = qp->priv;
182
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800183 del_timer_sync(&qp->s_timer);
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800184 del_timer_sync(&priv->s_rnr_timer);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800185}
186
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800187/* only opcode mask for adaptive pio */
188const u32 rc_only_opcode =
189 BIT(OP(SEND_ONLY) & 0x1f) |
190 BIT(OP(SEND_ONLY_WITH_IMMEDIATE & 0x1f)) |
191 BIT(OP(RDMA_WRITE_ONLY & 0x1f)) |
192 BIT(OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE & 0x1f)) |
193 BIT(OP(RDMA_READ_REQUEST & 0x1f)) |
194 BIT(OP(ACKNOWLEDGE & 0x1f)) |
195 BIT(OP(ATOMIC_ACKNOWLEDGE & 0x1f)) |
196 BIT(OP(COMPARE_SWAP & 0x1f)) |
197 BIT(OP(FETCH_ADD & 0x1f));
198
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800199static u32 restart_sge(struct rvt_sge_state *ss, struct rvt_swqe *wqe,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400200 u32 psn, u32 pmtu)
201{
202 u32 len;
203
204 len = delta_psn(psn, wqe->psn) * pmtu;
205 ss->sge = wqe->sg_list[0];
206 ss->sg_list = wqe->sg_list + 1;
207 ss->num_sge = wqe->wr.num_sge;
208 ss->total_len = wqe->length;
209 hfi1_skip_sge(ss, len, 0);
210 return wqe->length - len;
211}
212
Mike Marciniszyn77241052015-07-30 15:17:43 -0400213/**
214 * make_rc_ack - construct a response packet (ACK, NAK, or RDMA read)
215 * @dev: the device for this QP
216 * @qp: a pointer to the QP
217 * @ohdr: a pointer to the IB header being constructed
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800218 * @ps: the xmit packet state
Mike Marciniszyn77241052015-07-30 15:17:43 -0400219 *
220 * Return 1 if constructed; otherwise, return 0.
221 * Note that we are in the responder's side of the QP context.
222 * Note the QP s_lock must be held.
223 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800224static int make_rc_ack(struct hfi1_ibdev *dev, struct rvt_qp *qp,
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700225 struct ib_other_headers *ohdr,
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800226 struct hfi1_pkt_state *ps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400227{
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800228 struct rvt_ack_entry *e;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400229 u32 hwords;
230 u32 len;
231 u32 bth0;
232 u32 bth2;
233 int middle = 0;
Mike Marciniszyn1235bef2016-02-14 12:45:09 -0800234 u32 pmtu = qp->pmtu;
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800235 struct hfi1_qp_priv *priv = qp->priv;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400236
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700237 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400238 /* Don't send an ACK if we aren't supposed to. */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800239 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400240 goto bail;
241
242 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
243 hwords = 5;
244
245 switch (qp->s_ack_state) {
246 case OP(RDMA_READ_RESPONSE_LAST):
247 case OP(RDMA_READ_RESPONSE_ONLY):
248 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
249 if (e->rdma_sge.mr) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800250 rvt_put_mr(e->rdma_sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400251 e->rdma_sge.mr = NULL;
252 }
253 /* FALLTHROUGH */
254 case OP(ATOMIC_ACKNOWLEDGE):
255 /*
256 * We can increment the tail pointer now that the last
257 * response has been sent instead of only being
258 * constructed.
259 */
260 if (++qp->s_tail_ack_queue > HFI1_MAX_RDMA_ATOMIC)
261 qp->s_tail_ack_queue = 0;
262 /* FALLTHROUGH */
263 case OP(SEND_ONLY):
264 case OP(ACKNOWLEDGE):
265 /* Check for no next entry in the queue. */
266 if (qp->r_head_ack_queue == qp->s_tail_ack_queue) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800267 if (qp->s_flags & RVT_S_ACK_PENDING)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400268 goto normal;
269 goto bail;
270 }
271
272 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
273 if (e->opcode == OP(RDMA_READ_REQUEST)) {
274 /*
275 * If a RDMA read response is being resent and
276 * we haven't seen the duplicate request yet,
277 * then stop sending the remaining responses the
278 * responder has seen until the requester re-sends it.
279 */
280 len = e->rdma_sge.sge_length;
281 if (len && !e->rdma_sge.mr) {
282 qp->s_tail_ack_queue = qp->r_head_ack_queue;
283 goto bail;
284 }
285 /* Copy SGE state in case we need to resend */
Mike Marciniszync239a5b2016-02-14 12:44:52 -0800286 ps->s_txreq->mr = e->rdma_sge.mr;
287 if (ps->s_txreq->mr)
288 rvt_get_mr(ps->s_txreq->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400289 qp->s_ack_rdma_sge.sge = e->rdma_sge;
290 qp->s_ack_rdma_sge.num_sge = 1;
291 qp->s_cur_sge = &qp->s_ack_rdma_sge;
292 if (len > pmtu) {
293 len = pmtu;
294 qp->s_ack_state = OP(RDMA_READ_RESPONSE_FIRST);
295 } else {
296 qp->s_ack_state = OP(RDMA_READ_RESPONSE_ONLY);
297 e->sent = 1;
298 }
299 ohdr->u.aeth = hfi1_compute_aeth(qp);
300 hwords++;
301 qp->s_ack_rdma_psn = e->psn;
302 bth2 = mask_psn(qp->s_ack_rdma_psn++);
303 } else {
304 /* COMPARE_SWAP or FETCH_ADD */
305 qp->s_cur_sge = NULL;
306 len = 0;
307 qp->s_ack_state = OP(ATOMIC_ACKNOWLEDGE);
308 ohdr->u.at.aeth = hfi1_compute_aeth(qp);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700309 ib_u64_put(e->atomic_data, &ohdr->u.at.atomic_ack_eth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400310 hwords += sizeof(ohdr->u.at) / sizeof(u32);
311 bth2 = mask_psn(e->psn);
312 e->sent = 1;
313 }
314 bth0 = qp->s_ack_state << 24;
315 break;
316
317 case OP(RDMA_READ_RESPONSE_FIRST):
318 qp->s_ack_state = OP(RDMA_READ_RESPONSE_MIDDLE);
319 /* FALLTHROUGH */
320 case OP(RDMA_READ_RESPONSE_MIDDLE):
321 qp->s_cur_sge = &qp->s_ack_rdma_sge;
Mike Marciniszync239a5b2016-02-14 12:44:52 -0800322 ps->s_txreq->mr = qp->s_ack_rdma_sge.sge.mr;
323 if (ps->s_txreq->mr)
324 rvt_get_mr(ps->s_txreq->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400325 len = qp->s_ack_rdma_sge.sge.sge_length;
326 if (len > pmtu) {
327 len = pmtu;
328 middle = HFI1_CAP_IS_KSET(SDMA_AHG);
329 } else {
330 ohdr->u.aeth = hfi1_compute_aeth(qp);
331 hwords++;
332 qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
333 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
334 e->sent = 1;
335 }
336 bth0 = qp->s_ack_state << 24;
337 bth2 = mask_psn(qp->s_ack_rdma_psn++);
338 break;
339
340 default:
341normal:
342 /*
343 * Send a regular ACK.
344 * Set the s_ack_state so we wait until after sending
345 * the ACK before setting s_ack_state to ACKNOWLEDGE
346 * (see above).
347 */
348 qp->s_ack_state = OP(SEND_ONLY);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800349 qp->s_flags &= ~RVT_S_ACK_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400350 qp->s_cur_sge = NULL;
351 if (qp->s_nak_state)
352 ohdr->u.aeth =
353 cpu_to_be32((qp->r_msn & HFI1_MSN_MASK) |
354 (qp->s_nak_state <<
355 HFI1_AETH_CREDIT_SHIFT));
356 else
357 ohdr->u.aeth = hfi1_compute_aeth(qp);
358 hwords++;
359 len = 0;
360 bth0 = OP(ACKNOWLEDGE) << 24;
361 bth2 = mask_psn(qp->s_ack_psn);
362 }
363 qp->s_rdma_ack_cnt++;
364 qp->s_hdrwords = hwords;
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800365 ps->s_txreq->sde = priv->s_sde;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400366 qp->s_cur_size = len;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800367 hfi1_make_ruc_header(qp, ohdr, bth0, bth2, middle, ps);
Jianxin Xiongaa0ad412016-02-26 13:33:13 -0800368 /* pbc */
369 ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400370 return 1;
371
372bail:
373 qp->s_ack_state = OP(ACKNOWLEDGE);
374 /*
375 * Ensure s_rdma_ack_cnt changes are committed prior to resetting
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800376 * RVT_S_RESP_PENDING
Mike Marciniszyn77241052015-07-30 15:17:43 -0400377 */
378 smp_wmb();
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800379 qp->s_flags &= ~(RVT_S_RESP_PENDING
380 | RVT_S_ACK_PENDING
381 | RVT_S_AHG_VALID);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400382 return 0;
383}
384
385/**
386 * hfi1_make_rc_req - construct a request packet (SEND, RDMA r/w, ATOMIC)
387 * @qp: a pointer to the QP
388 *
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800389 * Assumes s_lock is held.
390 *
Mike Marciniszyn77241052015-07-30 15:17:43 -0400391 * Return 1 if constructed; otherwise, return 0.
392 */
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800393int hfi1_make_rc_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400394{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800395 struct hfi1_qp_priv *priv = qp->priv;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400396 struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700397 struct ib_other_headers *ohdr;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800398 struct rvt_sge_state *ss;
399 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400400 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
401 u32 hwords = 5;
402 u32 len;
403 u32 bth0 = 0;
404 u32 bth2;
405 u32 pmtu = qp->pmtu;
406 char newreq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400407 int middle = 0;
408 int delta;
409
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700410 lockdep_assert_held(&qp->s_lock);
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800411 ps->s_txreq = get_txreq(ps->dev, qp);
412 if (IS_ERR(ps->s_txreq))
413 goto bail_no_tx;
414
415 ohdr = &ps->s_txreq->phdr.hdr.u.oth;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400416 if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800417 ohdr = &ps->s_txreq->phdr.hdr.u.l.oth;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400418
Mike Marciniszyn77241052015-07-30 15:17:43 -0400419 /* Sending responses has higher priority over sending requests. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800420 if ((qp->s_flags & RVT_S_RESP_PENDING) &&
Mike Marciniszyn1235bef2016-02-14 12:45:09 -0800421 make_rc_ack(dev, qp, ohdr, ps))
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800422 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400423
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800424 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_SEND_OK)) {
425 if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400426 goto bail;
427 /* We are in the error state, flush the work request. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800428 smp_read_barrier_depends(); /* see post_one_send() */
429 if (qp->s_last == ACCESS_ONCE(qp->s_head))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400430 goto bail;
431 /* If DMAs are in progress, we can't flush immediately. */
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800432 if (iowait_sdma_pending(&priv->s_iowait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800433 qp->s_flags |= RVT_S_WAIT_DMA;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400434 goto bail;
435 }
436 clear_ahg(qp);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800437 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400438 hfi1_send_complete(qp, wqe, qp->s_last != qp->s_acked ?
439 IB_WC_SUCCESS : IB_WC_WR_FLUSH_ERR);
440 /* will get called again */
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800441 goto done_free_tx;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400442 }
443
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800444 if (qp->s_flags & (RVT_S_WAIT_RNR | RVT_S_WAIT_ACK))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400445 goto bail;
446
447 if (cmp_psn(qp->s_psn, qp->s_sending_hpsn) <= 0) {
448 if (cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800449 qp->s_flags |= RVT_S_WAIT_PSN;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400450 goto bail;
451 }
452 qp->s_sending_psn = qp->s_psn;
453 qp->s_sending_hpsn = qp->s_psn - 1;
454 }
455
456 /* Send a request. */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800457 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400458 switch (qp->s_state) {
459 default:
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800460 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400461 goto bail;
462 /*
463 * Resend an old request or start a new one.
464 *
465 * We keep track of the current SWQE so that
466 * we don't reset the "furthest progress" state
467 * if we need to back up.
468 */
469 newreq = 0;
470 if (qp->s_cur == qp->s_tail) {
471 /* Check if send work queue is empty. */
472 if (qp->s_tail == qp->s_head) {
473 clear_ahg(qp);
474 goto bail;
475 }
476 /*
477 * If a fence is requested, wait for previous
478 * RDMA read and atomic operations to finish.
479 */
480 if ((wqe->wr.send_flags & IB_SEND_FENCE) &&
481 qp->s_num_rd_atomic) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800482 qp->s_flags |= RVT_S_WAIT_FENCE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400483 goto bail;
484 }
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700485 /*
486 * Local operations are processed immediately
487 * after all prior requests have completed
488 */
489 if (wqe->wr.opcode == IB_WR_REG_MR ||
490 wqe->wr.opcode == IB_WR_LOCAL_INV) {
Jianxin Xiongd9b13c22016-07-25 13:39:45 -0700491 int local_ops = 0;
492 int err = 0;
493
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700494 if (qp->s_last != qp->s_cur)
495 goto bail;
496 if (++qp->s_cur == qp->s_size)
497 qp->s_cur = 0;
498 if (++qp->s_tail == qp->s_size)
499 qp->s_tail = 0;
Jianxin Xiongd9b13c22016-07-25 13:39:45 -0700500 if (!(wqe->wr.send_flags &
501 RVT_SEND_COMPLETION_ONLY)) {
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700502 err = rvt_invalidate_rkey(
503 qp,
504 wqe->wr.ex.invalidate_rkey);
Jianxin Xiongd9b13c22016-07-25 13:39:45 -0700505 local_ops = 1;
506 }
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700507 hfi1_send_complete(qp, wqe,
508 err ? IB_WC_LOC_PROT_ERR
509 : IB_WC_SUCCESS);
Jianxin Xiongd9b13c22016-07-25 13:39:45 -0700510 if (local_ops)
511 atomic_dec(&qp->local_ops_pending);
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700512 qp->s_hdrwords = 0;
513 goto done_free_tx;
514 }
515
Mike Marciniszyn77241052015-07-30 15:17:43 -0400516 newreq = 1;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800517 qp->s_psn = wqe->psn;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400518 }
519 /*
520 * Note that we have to be careful not to modify the
521 * original work request since we may need to resend
522 * it.
523 */
524 len = wqe->length;
525 ss = &qp->s_sge;
526 bth2 = mask_psn(qp->s_psn);
527 switch (wqe->wr.opcode) {
528 case IB_WR_SEND:
529 case IB_WR_SEND_WITH_IMM:
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700530 case IB_WR_SEND_WITH_INV:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400531 /* If no credit, return. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800532 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
Mike Marciniszyn77241052015-07-30 15:17:43 -0400533 cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800534 qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400535 goto bail;
536 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400537 if (len > pmtu) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400538 qp->s_state = OP(SEND_FIRST);
539 len = pmtu;
540 break;
541 }
Jubin Johne4909742016-02-14 20:22:00 -0800542 if (wqe->wr.opcode == IB_WR_SEND) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400543 qp->s_state = OP(SEND_ONLY);
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700544 } else if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400545 qp->s_state = OP(SEND_ONLY_WITH_IMMEDIATE);
546 /* Immediate data comes after the BTH */
547 ohdr->u.imm_data = wqe->wr.ex.imm_data;
548 hwords += 1;
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700549 } else {
550 qp->s_state = OP(SEND_ONLY_WITH_INVALIDATE);
551 /* Invalidate rkey comes after the BTH */
552 ohdr->u.ieth = cpu_to_be32(
553 wqe->wr.ex.invalidate_rkey);
554 hwords += 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400555 }
556 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
557 bth0 |= IB_BTH_SOLICITED;
558 bth2 |= IB_BTH_REQ_ACK;
559 if (++qp->s_cur == qp->s_size)
560 qp->s_cur = 0;
561 break;
562
563 case IB_WR_RDMA_WRITE:
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800564 if (newreq && !(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400565 qp->s_lsn++;
566 /* FALLTHROUGH */
567 case IB_WR_RDMA_WRITE_WITH_IMM:
568 /* If no credit, return. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800569 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
Mike Marciniszyn77241052015-07-30 15:17:43 -0400570 cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800571 qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400572 goto bail;
573 }
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700574 put_ib_reth_vaddr(
575 wqe->rdma_wr.remote_addr,
576 &ohdr->u.rc.reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400577 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100578 cpu_to_be32(wqe->rdma_wr.rkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400579 ohdr->u.rc.reth.length = cpu_to_be32(len);
580 hwords += sizeof(struct ib_reth) / sizeof(u32);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400581 if (len > pmtu) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400582 qp->s_state = OP(RDMA_WRITE_FIRST);
583 len = pmtu;
584 break;
585 }
Jubin Johne4909742016-02-14 20:22:00 -0800586 if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400587 qp->s_state = OP(RDMA_WRITE_ONLY);
Jubin Johne4909742016-02-14 20:22:00 -0800588 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400589 qp->s_state =
590 OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
591 /* Immediate data comes after RETH */
592 ohdr->u.rc.imm_data = wqe->wr.ex.imm_data;
593 hwords += 1;
594 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
595 bth0 |= IB_BTH_SOLICITED;
596 }
597 bth2 |= IB_BTH_REQ_ACK;
598 if (++qp->s_cur == qp->s_size)
599 qp->s_cur = 0;
600 break;
601
602 case IB_WR_RDMA_READ:
603 /*
604 * Don't allow more operations to be started
605 * than the QP limits allow.
606 */
607 if (newreq) {
608 if (qp->s_num_rd_atomic >=
609 qp->s_max_rd_atomic) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800610 qp->s_flags |= RVT_S_WAIT_RDMAR;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400611 goto bail;
612 }
613 qp->s_num_rd_atomic++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800614 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400615 qp->s_lsn++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400616 }
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700617 put_ib_reth_vaddr(
618 wqe->rdma_wr.remote_addr,
619 &ohdr->u.rc.reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400620 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100621 cpu_to_be32(wqe->rdma_wr.rkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400622 ohdr->u.rc.reth.length = cpu_to_be32(len);
623 qp->s_state = OP(RDMA_READ_REQUEST);
624 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
625 ss = NULL;
626 len = 0;
627 bth2 |= IB_BTH_REQ_ACK;
628 if (++qp->s_cur == qp->s_size)
629 qp->s_cur = 0;
630 break;
631
632 case IB_WR_ATOMIC_CMP_AND_SWP:
633 case IB_WR_ATOMIC_FETCH_AND_ADD:
634 /*
635 * Don't allow more operations to be started
636 * than the QP limits allow.
637 */
638 if (newreq) {
639 if (qp->s_num_rd_atomic >=
640 qp->s_max_rd_atomic) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800641 qp->s_flags |= RVT_S_WAIT_RDMAR;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400642 goto bail;
643 }
644 qp->s_num_rd_atomic++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800645 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400646 qp->s_lsn++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400647 }
648 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
649 qp->s_state = OP(COMPARE_SWAP);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700650 put_ib_ateth_swap(wqe->atomic_wr.swap,
651 &ohdr->u.atomic_eth);
652 put_ib_ateth_compare(wqe->atomic_wr.compare_add,
653 &ohdr->u.atomic_eth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400654 } else {
655 qp->s_state = OP(FETCH_ADD);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700656 put_ib_ateth_swap(wqe->atomic_wr.compare_add,
657 &ohdr->u.atomic_eth);
658 put_ib_ateth_compare(0, &ohdr->u.atomic_eth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400659 }
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700660 put_ib_ateth_vaddr(wqe->atomic_wr.remote_addr,
661 &ohdr->u.atomic_eth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400662 ohdr->u.atomic_eth.rkey = cpu_to_be32(
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100663 wqe->atomic_wr.rkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400664 hwords += sizeof(struct ib_atomic_eth) / sizeof(u32);
665 ss = NULL;
666 len = 0;
667 bth2 |= IB_BTH_REQ_ACK;
668 if (++qp->s_cur == qp->s_size)
669 qp->s_cur = 0;
670 break;
671
672 default:
673 goto bail;
674 }
675 qp->s_sge.sge = wqe->sg_list[0];
676 qp->s_sge.sg_list = wqe->sg_list + 1;
677 qp->s_sge.num_sge = wqe->wr.num_sge;
678 qp->s_sge.total_len = wqe->length;
679 qp->s_len = wqe->length;
680 if (newreq) {
681 qp->s_tail++;
682 if (qp->s_tail >= qp->s_size)
683 qp->s_tail = 0;
684 }
685 if (wqe->wr.opcode == IB_WR_RDMA_READ)
686 qp->s_psn = wqe->lpsn + 1;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800687 else
Mike Marciniszyn77241052015-07-30 15:17:43 -0400688 qp->s_psn++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400689 break;
690
691 case OP(RDMA_READ_RESPONSE_FIRST):
692 /*
693 * qp->s_state is normally set to the opcode of the
694 * last packet constructed for new requests and therefore
695 * is never set to RDMA read response.
696 * RDMA_READ_RESPONSE_FIRST is used by the ACK processing
697 * thread to indicate a SEND needs to be restarted from an
698 * earlier PSN without interfering with the sending thread.
699 * See restart_rc().
700 */
701 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
702 /* FALLTHROUGH */
703 case OP(SEND_FIRST):
704 qp->s_state = OP(SEND_MIDDLE);
705 /* FALLTHROUGH */
706 case OP(SEND_MIDDLE):
707 bth2 = mask_psn(qp->s_psn++);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400708 ss = &qp->s_sge;
709 len = qp->s_len;
710 if (len > pmtu) {
711 len = pmtu;
712 middle = HFI1_CAP_IS_KSET(SDMA_AHG);
713 break;
714 }
Jubin Johne4909742016-02-14 20:22:00 -0800715 if (wqe->wr.opcode == IB_WR_SEND) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400716 qp->s_state = OP(SEND_LAST);
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700717 } else if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400718 qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
719 /* Immediate data comes after the BTH */
720 ohdr->u.imm_data = wqe->wr.ex.imm_data;
721 hwords += 1;
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700722 } else {
723 qp->s_state = OP(SEND_LAST_WITH_INVALIDATE);
724 /* invalidate data comes after the BTH */
725 ohdr->u.ieth = cpu_to_be32(wqe->wr.ex.invalidate_rkey);
726 hwords += 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400727 }
728 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
729 bth0 |= IB_BTH_SOLICITED;
730 bth2 |= IB_BTH_REQ_ACK;
731 qp->s_cur++;
732 if (qp->s_cur >= qp->s_size)
733 qp->s_cur = 0;
734 break;
735
736 case OP(RDMA_READ_RESPONSE_LAST):
737 /*
738 * qp->s_state is normally set to the opcode of the
739 * last packet constructed for new requests and therefore
740 * is never set to RDMA read response.
741 * RDMA_READ_RESPONSE_LAST is used by the ACK processing
742 * thread to indicate a RDMA write needs to be restarted from
743 * an earlier PSN without interfering with the sending thread.
744 * See restart_rc().
745 */
746 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
747 /* FALLTHROUGH */
748 case OP(RDMA_WRITE_FIRST):
749 qp->s_state = OP(RDMA_WRITE_MIDDLE);
750 /* FALLTHROUGH */
751 case OP(RDMA_WRITE_MIDDLE):
752 bth2 = mask_psn(qp->s_psn++);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400753 ss = &qp->s_sge;
754 len = qp->s_len;
755 if (len > pmtu) {
756 len = pmtu;
757 middle = HFI1_CAP_IS_KSET(SDMA_AHG);
758 break;
759 }
Jubin Johne4909742016-02-14 20:22:00 -0800760 if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400761 qp->s_state = OP(RDMA_WRITE_LAST);
Jubin Johne4909742016-02-14 20:22:00 -0800762 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400763 qp->s_state = OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
764 /* Immediate data comes after the BTH */
765 ohdr->u.imm_data = wqe->wr.ex.imm_data;
766 hwords += 1;
767 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
768 bth0 |= IB_BTH_SOLICITED;
769 }
770 bth2 |= IB_BTH_REQ_ACK;
771 qp->s_cur++;
772 if (qp->s_cur >= qp->s_size)
773 qp->s_cur = 0;
774 break;
775
776 case OP(RDMA_READ_RESPONSE_MIDDLE):
777 /*
778 * qp->s_state is normally set to the opcode of the
779 * last packet constructed for new requests and therefore
780 * is never set to RDMA read response.
781 * RDMA_READ_RESPONSE_MIDDLE is used by the ACK processing
782 * thread to indicate a RDMA read needs to be restarted from
783 * an earlier PSN without interfering with the sending thread.
784 * See restart_rc().
785 */
786 len = (delta_psn(qp->s_psn, wqe->psn)) * pmtu;
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700787 put_ib_reth_vaddr(
788 wqe->rdma_wr.remote_addr + len,
789 &ohdr->u.rc.reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400790 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100791 cpu_to_be32(wqe->rdma_wr.rkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400792 ohdr->u.rc.reth.length = cpu_to_be32(wqe->length - len);
793 qp->s_state = OP(RDMA_READ_REQUEST);
794 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
795 bth2 = mask_psn(qp->s_psn) | IB_BTH_REQ_ACK;
796 qp->s_psn = wqe->lpsn + 1;
797 ss = NULL;
798 len = 0;
799 qp->s_cur++;
800 if (qp->s_cur == qp->s_size)
801 qp->s_cur = 0;
802 break;
803 }
804 qp->s_sending_hpsn = bth2;
805 delta = delta_psn(bth2, wqe->psn);
806 if (delta && delta % HFI1_PSN_CREDIT == 0)
807 bth2 |= IB_BTH_REQ_ACK;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800808 if (qp->s_flags & RVT_S_SEND_ONE) {
809 qp->s_flags &= ~RVT_S_SEND_ONE;
810 qp->s_flags |= RVT_S_WAIT_ACK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400811 bth2 |= IB_BTH_REQ_ACK;
812 }
813 qp->s_len -= len;
814 qp->s_hdrwords = hwords;
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800815 ps->s_txreq->sde = priv->s_sde;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400816 qp->s_cur_sge = ss;
817 qp->s_cur_size = len;
818 hfi1_make_ruc_header(
819 qp,
820 ohdr,
821 bth0 | (qp->s_state << 24),
822 bth2,
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800823 middle,
824 ps);
Jianxin Xiongaa0ad412016-02-26 13:33:13 -0800825 /* pbc */
826 ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800827 return 1;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800828
829done_free_tx:
830 hfi1_put_txreq(ps->s_txreq);
831 ps->s_txreq = NULL;
832 return 1;
833
Mike Marciniszyn77241052015-07-30 15:17:43 -0400834bail:
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800835 hfi1_put_txreq(ps->s_txreq);
836
837bail_no_tx:
838 ps->s_txreq = NULL;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800839 qp->s_flags &= ~RVT_S_BUSY;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800840 qp->s_hdrwords = 0;
841 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400842}
843
844/**
845 * hfi1_send_rc_ack - Construct an ACK packet and send it
846 * @qp: a pointer to the QP
847 *
848 * This is called from hfi1_rc_rcv() and handle_receive_interrupt().
849 * Note that RDMA reads and atomics are handled in the
850 * send side QP state and tasklet.
851 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800852void hfi1_send_rc_ack(struct hfi1_ctxtdata *rcd, struct rvt_qp *qp,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400853 int is_fecn)
854{
855 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
856 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
857 u64 pbc, pbc_flags = 0;
858 u16 lrh0;
859 u16 sc5;
860 u32 bth0;
861 u32 hwords;
862 u32 vl, plen;
863 struct send_context *sc;
864 struct pio_buf *pbuf;
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700865 struct ib_header hdr;
866 struct ib_other_headers *ohdr;
Dean Luickb77d7132015-10-26 10:28:43 -0400867 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400868
869 /* Don't send ACK or NAK if a RDMA read or atomic is pending. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800870 if (qp->s_flags & RVT_S_RESP_PENDING)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400871 goto queue_ack;
872
873 /* Ensure s_rdma_ack_cnt changes are committed */
874 smp_read_barrier_depends();
875 if (qp->s_rdma_ack_cnt)
876 goto queue_ack;
877
878 /* Construct the header */
879 /* header size in 32-bit words LRH+BTH+AETH = (8+12+4)/4 */
880 hwords = 6;
881 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
882 hwords += hfi1_make_grh(ibp, &hdr.u.l.grh,
883 &qp->remote_ah_attr.grh, hwords, 0);
884 ohdr = &hdr.u.l.oth;
885 lrh0 = HFI1_LRH_GRH;
886 } else {
887 ohdr = &hdr.u.oth;
888 lrh0 = HFI1_LRH_BTH;
889 }
890 /* read pkey_index w/o lock (its atomic) */
891 bth0 = hfi1_get_pkey(ibp, qp->s_pkey_index) | (OP(ACKNOWLEDGE) << 24);
892 if (qp->s_mig_state == IB_MIG_MIGRATED)
893 bth0 |= IB_BTH_MIG_REQ;
894 if (qp->r_nak_state)
895 ohdr->u.aeth = cpu_to_be32((qp->r_msn & HFI1_MSN_MASK) |
896 (qp->r_nak_state <<
897 HFI1_AETH_CREDIT_SHIFT));
898 else
899 ohdr->u.aeth = hfi1_compute_aeth(qp);
900 sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl];
901 /* set PBC_DC_INFO bit (aka SC[4]) in pbc_flags */
902 pbc_flags |= ((!!(sc5 & 0x10)) << PBC_DC_INFO_SHIFT);
903 lrh0 |= (sc5 & 0xf) << 12 | (qp->remote_ah_attr.sl & 0xf) << 4;
904 hdr.lrh[0] = cpu_to_be16(lrh0);
905 hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
906 hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
907 hdr.lrh[3] = cpu_to_be16(ppd->lid | qp->remote_ah_attr.src_path_bits);
908 ohdr->bth[0] = cpu_to_be32(bth0);
909 ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
910 ohdr->bth[1] |= cpu_to_be32((!!is_fecn) << HFI1_BECN_SHIFT);
911 ohdr->bth[2] = cpu_to_be32(mask_psn(qp->r_ack_psn));
912
913 /* Don't try to send ACKs if the link isn't ACTIVE */
914 if (driver_lstate(ppd) != IB_PORT_ACTIVE)
915 return;
916
917 sc = rcd->sc;
918 plen = 2 /* PBC */ + hwords;
919 vl = sc_to_vlt(ppd->dd, sc5);
920 pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
921
922 pbuf = sc_buffer_alloc(sc, plen, NULL, NULL);
923 if (!pbuf) {
924 /*
925 * We have no room to send at the moment. Pass
926 * responsibility for sending the ACK to the send tasklet
927 * so that when enough buffer space becomes available,
928 * the ACK is sent ahead of other outgoing packets.
929 */
930 goto queue_ack;
931 }
932
Mike Marciniszyn1db78ee2016-03-07 11:35:19 -0800933 trace_ack_output_ibhdr(dd_from_ibdev(qp->ibqp.device), &hdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400934
935 /* write the pbc and data */
936 ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc, &hdr, hwords);
937
938 return;
939
940queue_ack:
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800941 this_cpu_inc(*ibp->rvp.rc_qacks);
Dean Luickb77d7132015-10-26 10:28:43 -0400942 spin_lock_irqsave(&qp->s_lock, flags);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800943 qp->s_flags |= RVT_S_ACK_PENDING | RVT_S_RESP_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400944 qp->s_nak_state = qp->r_nak_state;
945 qp->s_ack_psn = qp->r_ack_psn;
946 if (is_fecn)
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800947 qp->s_flags |= RVT_S_ECN;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400948
949 /* Schedule the send tasklet. */
950 hfi1_schedule_send(qp);
Dean Luickb77d7132015-10-26 10:28:43 -0400951 spin_unlock_irqrestore(&qp->s_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400952}
953
954/**
955 * reset_psn - reset the QP state to send starting from PSN
956 * @qp: the QP
957 * @psn: the packet sequence number to restart at
958 *
959 * This is called from hfi1_rc_rcv() to process an incoming RC ACK
960 * for the given QP.
961 * Called at interrupt level with the QP s_lock held.
962 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800963static void reset_psn(struct rvt_qp *qp, u32 psn)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400964{
965 u32 n = qp->s_acked;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800966 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, n);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400967 u32 opcode;
968
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700969 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400970 qp->s_cur = n;
971
972 /*
973 * If we are starting the request from the beginning,
974 * let the normal send code handle initialization.
975 */
976 if (cmp_psn(psn, wqe->psn) <= 0) {
977 qp->s_state = OP(SEND_LAST);
978 goto done;
979 }
980
981 /* Find the work request opcode corresponding to the given PSN. */
982 opcode = wqe->wr.opcode;
983 for (;;) {
984 int diff;
985
986 if (++n == qp->s_size)
987 n = 0;
988 if (n == qp->s_tail)
989 break;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800990 wqe = rvt_get_swqe_ptr(qp, n);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400991 diff = cmp_psn(psn, wqe->psn);
992 if (diff < 0)
993 break;
994 qp->s_cur = n;
995 /*
996 * If we are starting the request from the beginning,
997 * let the normal send code handle initialization.
998 */
999 if (diff == 0) {
1000 qp->s_state = OP(SEND_LAST);
1001 goto done;
1002 }
1003 opcode = wqe->wr.opcode;
1004 }
1005
1006 /*
1007 * Set the state to restart in the middle of a request.
1008 * Don't change the s_sge, s_cur_sge, or s_cur_size.
1009 * See hfi1_make_rc_req().
1010 */
1011 switch (opcode) {
1012 case IB_WR_SEND:
1013 case IB_WR_SEND_WITH_IMM:
1014 qp->s_state = OP(RDMA_READ_RESPONSE_FIRST);
1015 break;
1016
1017 case IB_WR_RDMA_WRITE:
1018 case IB_WR_RDMA_WRITE_WITH_IMM:
1019 qp->s_state = OP(RDMA_READ_RESPONSE_LAST);
1020 break;
1021
1022 case IB_WR_RDMA_READ:
1023 qp->s_state = OP(RDMA_READ_RESPONSE_MIDDLE);
1024 break;
1025
1026 default:
1027 /*
1028 * This case shouldn't happen since its only
1029 * one PSN per req.
1030 */
1031 qp->s_state = OP(SEND_LAST);
1032 }
1033done:
1034 qp->s_psn = psn;
1035 /*
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001036 * Set RVT_S_WAIT_PSN as rc_complete() may start the timer
Mike Marciniszyn77241052015-07-30 15:17:43 -04001037 * asynchronously before the send tasklet can get scheduled.
1038 * Doing it in hfi1_make_rc_req() is too late.
1039 */
1040 if ((cmp_psn(qp->s_psn, qp->s_sending_hpsn) <= 0) &&
1041 (cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0))
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001042 qp->s_flags |= RVT_S_WAIT_PSN;
1043 qp->s_flags &= ~RVT_S_AHG_VALID;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001044}
1045
1046/*
1047 * Back up requester to resend the last un-ACKed request.
1048 * The QP r_lock and s_lock should be held and interrupts disabled.
1049 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001050static void restart_rc(struct rvt_qp *qp, u32 psn, int wait)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001051{
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001052 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001053 struct hfi1_ibport *ibp;
1054
Mike Marciniszyn68e78b32016-09-06 04:37:41 -07001055 lockdep_assert_held(&qp->r_lock);
1056 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001057 if (qp->s_retry == 0) {
1058 if (qp->s_mig_state == IB_MIG_ARMED) {
1059 hfi1_migrate_qp(qp);
1060 qp->s_retry = qp->s_retry_cnt;
1061 } else if (qp->s_last == qp->s_acked) {
1062 hfi1_send_complete(qp, wqe, IB_WC_RETRY_EXC_ERR);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08001063 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001064 return;
Jubin Johne4909742016-02-14 20:22:00 -08001065 } else { /* need to handle delayed completion */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001066 return;
Jubin Johne4909742016-02-14 20:22:00 -08001067 }
1068 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001069 qp->s_retry--;
Jubin Johne4909742016-02-14 20:22:00 -08001070 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001071
1072 ibp = to_iport(qp->ibqp.device, qp->port_num);
1073 if (wqe->wr.opcode == IB_WR_RDMA_READ)
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001074 ibp->rvp.n_rc_resends++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001075 else
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001076 ibp->rvp.n_rc_resends += delta_psn(qp->s_psn, psn);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001077
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001078 qp->s_flags &= ~(RVT_S_WAIT_FENCE | RVT_S_WAIT_RDMAR |
1079 RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_PSN |
1080 RVT_S_WAIT_ACK);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001081 if (wait)
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001082 qp->s_flags |= RVT_S_SEND_ONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001083 reset_psn(qp, psn);
1084}
1085
1086/*
1087 * This is called from s_timer for missing responses.
1088 */
Mike Marciniszyn08279d52016-02-04 10:59:36 -08001089void hfi1_rc_timeout(unsigned long arg)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001090{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001091 struct rvt_qp *qp = (struct rvt_qp *)arg;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001092 struct hfi1_ibport *ibp;
1093 unsigned long flags;
1094
1095 spin_lock_irqsave(&qp->r_lock, flags);
1096 spin_lock(&qp->s_lock);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001097 if (qp->s_flags & RVT_S_TIMER) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001098 ibp = to_iport(qp->ibqp.device, qp->port_num);
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001099 ibp->rvp.n_rc_timeouts++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001100 qp->s_flags &= ~RVT_S_TIMER;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001101 del_timer(&qp->s_timer);
Sebastian Sanchez462b6b22016-07-01 16:01:06 -07001102 trace_hfi1_timeout(qp, qp->s_last_psn + 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001103 restart_rc(qp, qp->s_last_psn + 1, 1);
1104 hfi1_schedule_send(qp);
1105 }
1106 spin_unlock(&qp->s_lock);
1107 spin_unlock_irqrestore(&qp->r_lock, flags);
1108}
1109
1110/*
1111 * This is called from s_timer for RNR timeouts.
1112 */
1113void hfi1_rc_rnr_retry(unsigned long arg)
1114{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001115 struct rvt_qp *qp = (struct rvt_qp *)arg;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001116 unsigned long flags;
1117
1118 spin_lock_irqsave(&qp->s_lock, flags);
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001119 hfi1_stop_rnr_timer(qp);
1120 hfi1_schedule_send(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001121 spin_unlock_irqrestore(&qp->s_lock, flags);
1122}
1123
1124/*
1125 * Set qp->s_sending_psn to the next PSN after the given one.
1126 * This would be psn+1 except when RDMA reads are present.
1127 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001128static void reset_sending_psn(struct rvt_qp *qp, u32 psn)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001129{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001130 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001131 u32 n = qp->s_last;
1132
Mike Marciniszyn68e78b32016-09-06 04:37:41 -07001133 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001134 /* Find the work request corresponding to the given PSN. */
1135 for (;;) {
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001136 wqe = rvt_get_swqe_ptr(qp, n);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001137 if (cmp_psn(psn, wqe->lpsn) <= 0) {
1138 if (wqe->wr.opcode == IB_WR_RDMA_READ)
1139 qp->s_sending_psn = wqe->lpsn + 1;
1140 else
1141 qp->s_sending_psn = psn + 1;
1142 break;
1143 }
1144 if (++n == qp->s_size)
1145 n = 0;
1146 if (n == qp->s_tail)
1147 break;
1148 }
1149}
1150
1151/*
1152 * This should be called with the QP s_lock held and interrupts disabled.
1153 */
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001154void hfi1_rc_send_complete(struct rvt_qp *qp, struct ib_header *hdr)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001155{
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001156 struct ib_other_headers *ohdr;
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001157 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001158 struct ib_wc wc;
1159 unsigned i;
1160 u32 opcode;
1161 u32 psn;
1162
Mike Marciniszyn68e78b32016-09-06 04:37:41 -07001163 lockdep_assert_held(&qp->s_lock);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001164 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001165 return;
1166
1167 /* Find out where the BTH is */
1168 if ((be16_to_cpu(hdr->lrh[0]) & 3) == HFI1_LRH_BTH)
1169 ohdr = &hdr->u.oth;
1170 else
1171 ohdr = &hdr->u.l.oth;
1172
1173 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
1174 if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
1175 opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
1176 WARN_ON(!qp->s_rdma_ack_cnt);
1177 qp->s_rdma_ack_cnt--;
1178 return;
1179 }
1180
1181 psn = be32_to_cpu(ohdr->bth[2]);
1182 reset_sending_psn(qp, psn);
1183
1184 /*
1185 * Start timer after a packet requesting an ACK has been sent and
1186 * there are still requests that haven't been acked.
1187 */
1188 if ((psn & IB_BTH_REQ_ACK) && qp->s_acked != qp->s_tail &&
1189 !(qp->s_flags &
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001190 (RVT_S_TIMER | RVT_S_WAIT_RNR | RVT_S_WAIT_PSN)) &&
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001191 (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001192 hfi1_add_retry_timer(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001193
1194 while (qp->s_last != qp->s_acked) {
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -08001195 u32 s_last;
1196
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001197 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001198 if (cmp_psn(wqe->lpsn, qp->s_sending_psn) >= 0 &&
1199 cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0)
1200 break;
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -08001201 s_last = qp->s_last;
1202 if (++s_last >= qp->s_size)
1203 s_last = 0;
1204 qp->s_last = s_last;
1205 /* see post_send() */
1206 barrier();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001207 for (i = 0; i < wqe->wr.num_sge; i++) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001208 struct rvt_sge *sge = &wqe->sg_list[i];
Mike Marciniszyn77241052015-07-30 15:17:43 -04001209
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001210 rvt_put_mr(sge->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001211 }
1212 /* Post a send completion queue entry if requested. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001213 if (!(qp->s_flags & RVT_S_SIGNAL_REQ_WR) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04001214 (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
1215 memset(&wc, 0, sizeof(wc));
1216 wc.wr_id = wqe->wr.wr_id;
1217 wc.status = IB_WC_SUCCESS;
1218 wc.opcode = ib_hfi1_wc_opcode[wqe->wr.opcode];
1219 wc.byte_len = wqe->length;
1220 wc.qp = &qp->ibqp;
Dennis Dalessandroabd712d2016-01-19 14:43:22 -08001221 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.send_cq), &wc, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001222 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001223 }
1224 /*
1225 * If we were waiting for sends to complete before re-sending,
1226 * and they are now complete, restart sending.
1227 */
Sebastian Sanchez462b6b22016-07-01 16:01:06 -07001228 trace_hfi1_sendcomplete(qp, psn);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001229 if (qp->s_flags & RVT_S_WAIT_PSN &&
Mike Marciniszyn77241052015-07-30 15:17:43 -04001230 cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001231 qp->s_flags &= ~RVT_S_WAIT_PSN;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001232 qp->s_sending_psn = qp->s_psn;
1233 qp->s_sending_hpsn = qp->s_psn - 1;
1234 hfi1_schedule_send(qp);
1235 }
1236}
1237
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001238static inline void update_last_psn(struct rvt_qp *qp, u32 psn)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001239{
1240 qp->s_last_psn = psn;
1241}
1242
1243/*
1244 * Generate a SWQE completion.
1245 * This is similar to hfi1_send_complete but has to check to be sure
1246 * that the SGEs are not being referenced if the SWQE is being resent.
1247 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001248static struct rvt_swqe *do_rc_completion(struct rvt_qp *qp,
1249 struct rvt_swqe *wqe,
1250 struct hfi1_ibport *ibp)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001251{
1252 struct ib_wc wc;
1253 unsigned i;
1254
Mike Marciniszyn68e78b32016-09-06 04:37:41 -07001255 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001256 /*
1257 * Don't decrement refcount and don't generate a
1258 * completion if the SWQE is being resent until the send
1259 * is finished.
1260 */
1261 if (cmp_psn(wqe->lpsn, qp->s_sending_psn) < 0 ||
1262 cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -08001263 u32 s_last;
1264
Mike Marciniszyn77241052015-07-30 15:17:43 -04001265 for (i = 0; i < wqe->wr.num_sge; i++) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001266 struct rvt_sge *sge = &wqe->sg_list[i];
Mike Marciniszyn77241052015-07-30 15:17:43 -04001267
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001268 rvt_put_mr(sge->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001269 }
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -08001270 s_last = qp->s_last;
1271 if (++s_last >= qp->s_size)
1272 s_last = 0;
1273 qp->s_last = s_last;
1274 /* see post_send() */
1275 barrier();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001276 /* Post a send completion queue entry if requested. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001277 if (!(qp->s_flags & RVT_S_SIGNAL_REQ_WR) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04001278 (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
1279 memset(&wc, 0, sizeof(wc));
1280 wc.wr_id = wqe->wr.wr_id;
1281 wc.status = IB_WC_SUCCESS;
1282 wc.opcode = ib_hfi1_wc_opcode[wqe->wr.opcode];
1283 wc.byte_len = wqe->length;
1284 wc.qp = &qp->ibqp;
Dennis Dalessandroabd712d2016-01-19 14:43:22 -08001285 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.send_cq), &wc, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001286 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001287 } else {
1288 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
1289
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001290 this_cpu_inc(*ibp->rvp.rc_delayed_comp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001291 /*
1292 * If send progress not running attempt to progress
1293 * SDMA queue.
1294 */
1295 if (ppd->dd->flags & HFI1_HAS_SEND_DMA) {
1296 struct sdma_engine *engine;
1297 u8 sc5;
1298
1299 /* For now use sc to find engine */
1300 sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl];
1301 engine = qp_to_sdma_engine(qp, sc5);
1302 sdma_engine_progress_schedule(engine);
1303 }
1304 }
1305
1306 qp->s_retry = qp->s_retry_cnt;
1307 update_last_psn(qp, wqe->lpsn);
1308
1309 /*
1310 * If we are completing a request which is in the process of
1311 * being resent, we can stop re-sending it since we know the
1312 * responder has already seen it.
1313 */
1314 if (qp->s_acked == qp->s_cur) {
1315 if (++qp->s_cur >= qp->s_size)
1316 qp->s_cur = 0;
1317 qp->s_acked = qp->s_cur;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001318 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001319 if (qp->s_acked != qp->s_tail) {
1320 qp->s_state = OP(SEND_LAST);
1321 qp->s_psn = wqe->psn;
1322 }
1323 } else {
1324 if (++qp->s_acked >= qp->s_size)
1325 qp->s_acked = 0;
1326 if (qp->state == IB_QPS_SQD && qp->s_acked == qp->s_cur)
1327 qp->s_draining = 0;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001328 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001329 }
1330 return wqe;
1331}
1332
1333/**
1334 * do_rc_ack - process an incoming RC ACK
1335 * @qp: the QP the ACK came in on
1336 * @psn: the packet sequence number of the ACK
1337 * @opcode: the opcode of the request that resulted in the ACK
1338 *
1339 * This is called from rc_rcv_resp() to process an incoming RC ACK
1340 * for the given QP.
Dean Luickb77d7132015-10-26 10:28:43 -04001341 * May be called at interrupt level, with the QP s_lock held.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001342 * Returns 1 if OK, 0 if current operation should be aborted (NAK).
1343 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001344static int do_rc_ack(struct rvt_qp *qp, u32 aeth, u32 psn, int opcode,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001345 u64 val, struct hfi1_ctxtdata *rcd)
1346{
1347 struct hfi1_ibport *ibp;
1348 enum ib_wc_status status;
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001349 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001350 int ret = 0;
1351 u32 ack_psn;
1352 int diff;
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001353 unsigned long to;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001354
Mike Marciniszyn68e78b32016-09-06 04:37:41 -07001355 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001356 /*
1357 * Note that NAKs implicitly ACK outstanding SEND and RDMA write
1358 * requests and implicitly NAK RDMA read and atomic requests issued
1359 * before the NAK'ed request. The MSN won't include the NAK'ed
1360 * request but will include an ACK'ed request(s).
1361 */
1362 ack_psn = psn;
1363 if (aeth >> 29)
1364 ack_psn--;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001365 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001366 ibp = to_iport(qp->ibqp.device, qp->port_num);
1367
1368 /*
1369 * The MSN might be for a later WQE than the PSN indicates so
1370 * only complete WQEs that the PSN finishes.
1371 */
1372 while ((diff = delta_psn(ack_psn, wqe->lpsn)) >= 0) {
1373 /*
1374 * RDMA_READ_RESPONSE_ONLY is a special case since
1375 * we want to generate completion events for everything
1376 * before the RDMA read, copy the data, then generate
1377 * the completion for the read.
1378 */
1379 if (wqe->wr.opcode == IB_WR_RDMA_READ &&
1380 opcode == OP(RDMA_READ_RESPONSE_ONLY) &&
1381 diff == 0) {
1382 ret = 1;
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001383 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001384 }
1385 /*
1386 * If this request is a RDMA read or atomic, and the ACK is
1387 * for a later operation, this ACK NAKs the RDMA read or
1388 * atomic. In other words, only a RDMA_READ_LAST or ONLY
1389 * can ACK a RDMA read and likewise for atomic ops. Note
1390 * that the NAK case can only happen if relaxed ordering is
1391 * used and requests are sent after an RDMA read or atomic
1392 * is sent but before the response is received.
1393 */
1394 if ((wqe->wr.opcode == IB_WR_RDMA_READ &&
1395 (opcode != OP(RDMA_READ_RESPONSE_LAST) || diff != 0)) ||
1396 ((wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1397 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) &&
1398 (opcode != OP(ATOMIC_ACKNOWLEDGE) || diff != 0))) {
1399 /* Retry this request. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001400 if (!(qp->r_flags & RVT_R_RDMAR_SEQ)) {
1401 qp->r_flags |= RVT_R_RDMAR_SEQ;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001402 restart_rc(qp, qp->s_last_psn + 1, 0);
1403 if (list_empty(&qp->rspwait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001404 qp->r_flags |= RVT_R_RSP_SEND;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001405 rvt_get_qp(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001406 list_add_tail(&qp->rspwait,
1407 &rcd->qp_wait_list);
1408 }
1409 }
1410 /*
1411 * No need to process the ACK/NAK since we are
1412 * restarting an earlier request.
1413 */
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001414 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001415 }
1416 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1417 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
1418 u64 *vaddr = wqe->sg_list[0].vaddr;
1419 *vaddr = val;
1420 }
1421 if (qp->s_num_rd_atomic &&
1422 (wqe->wr.opcode == IB_WR_RDMA_READ ||
1423 wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1424 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)) {
1425 qp->s_num_rd_atomic--;
1426 /* Restart sending task if fence is complete */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001427 if ((qp->s_flags & RVT_S_WAIT_FENCE) &&
Mike Marciniszyn77241052015-07-30 15:17:43 -04001428 !qp->s_num_rd_atomic) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001429 qp->s_flags &= ~(RVT_S_WAIT_FENCE |
1430 RVT_S_WAIT_ACK);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001431 hfi1_schedule_send(qp);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001432 } else if (qp->s_flags & RVT_S_WAIT_RDMAR) {
1433 qp->s_flags &= ~(RVT_S_WAIT_RDMAR |
1434 RVT_S_WAIT_ACK);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001435 hfi1_schedule_send(qp);
1436 }
1437 }
1438 wqe = do_rc_completion(qp, wqe, ibp);
1439 if (qp->s_acked == qp->s_tail)
1440 break;
1441 }
1442
1443 switch (aeth >> 29) {
1444 case 0: /* ACK */
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001445 this_cpu_inc(*ibp->rvp.rc_acks);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001446 if (qp->s_acked != qp->s_tail) {
1447 /*
1448 * We are expecting more ACKs so
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001449 * mod the retry timer.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001450 */
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001451 hfi1_mod_retry_timer(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001452 /*
1453 * We can stop re-sending the earlier packets and
1454 * continue with the next packet the receiver wants.
1455 */
1456 if (cmp_psn(qp->s_psn, psn) <= 0)
1457 reset_psn(qp, psn + 1);
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001458 } else {
1459 /* No more acks - kill all timers */
1460 hfi1_stop_rc_timers(qp);
1461 if (cmp_psn(qp->s_psn, psn) <= 0) {
1462 qp->s_state = OP(SEND_LAST);
1463 qp->s_psn = psn + 1;
1464 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001465 }
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001466 if (qp->s_flags & RVT_S_WAIT_ACK) {
1467 qp->s_flags &= ~RVT_S_WAIT_ACK;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001468 hfi1_schedule_send(qp);
1469 }
1470 hfi1_get_credit(qp, aeth);
1471 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1472 qp->s_retry = qp->s_retry_cnt;
1473 update_last_psn(qp, psn);
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001474 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001475
1476 case 1: /* RNR NAK */
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001477 ibp->rvp.n_rnr_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001478 if (qp->s_acked == qp->s_tail)
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001479 goto bail_stop;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001480 if (qp->s_flags & RVT_S_WAIT_RNR)
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001481 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001482 if (qp->s_rnr_retry == 0) {
1483 status = IB_WC_RNR_RETRY_EXC_ERR;
1484 goto class_b;
1485 }
1486 if (qp->s_rnr_retry_cnt < 7)
1487 qp->s_rnr_retry--;
1488
1489 /* The last valid PSN is the previous PSN. */
1490 update_last_psn(qp, psn - 1);
1491
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001492 ibp->rvp.n_rc_resends += delta_psn(qp->s_psn, psn);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001493
1494 reset_psn(qp, psn);
1495
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001496 qp->s_flags &= ~(RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_ACK);
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001497 hfi1_stop_rc_timers(qp);
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001498 to =
Mike Marciniszyn77241052015-07-30 15:17:43 -04001499 ib_hfi1_rnr_table[(aeth >> HFI1_AETH_CREDIT_SHIFT) &
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001500 HFI1_AETH_CREDIT_MASK];
1501 hfi1_add_rnr_timer(qp, to);
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001502 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001503
1504 case 3: /* NAK */
1505 if (qp->s_acked == qp->s_tail)
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001506 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001507 /* The last valid PSN is the previous PSN. */
1508 update_last_psn(qp, psn - 1);
1509 switch ((aeth >> HFI1_AETH_CREDIT_SHIFT) &
1510 HFI1_AETH_CREDIT_MASK) {
1511 case 0: /* PSN sequence error */
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001512 ibp->rvp.n_seq_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001513 /*
1514 * Back up to the responder's expected PSN.
1515 * Note that we might get a NAK in the middle of an
1516 * RDMA READ response which terminates the RDMA
1517 * READ.
1518 */
1519 restart_rc(qp, psn, 0);
1520 hfi1_schedule_send(qp);
1521 break;
1522
1523 case 1: /* Invalid Request */
1524 status = IB_WC_REM_INV_REQ_ERR;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001525 ibp->rvp.n_other_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001526 goto class_b;
1527
1528 case 2: /* Remote Access Error */
1529 status = IB_WC_REM_ACCESS_ERR;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001530 ibp->rvp.n_other_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001531 goto class_b;
1532
1533 case 3: /* Remote Operation Error */
1534 status = IB_WC_REM_OP_ERR;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001535 ibp->rvp.n_other_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001536class_b:
1537 if (qp->s_last == qp->s_acked) {
1538 hfi1_send_complete(qp, wqe, status);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08001539 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001540 }
1541 break;
1542
1543 default:
1544 /* Ignore other reserved NAK error codes */
1545 goto reserved;
1546 }
1547 qp->s_retry = qp->s_retry_cnt;
1548 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001549 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001550
1551 default: /* 2: reserved */
1552reserved:
1553 /* Ignore reserved NAK codes. */
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001554 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001555 }
Mike Marciniszyn87717f02016-04-12 11:28:56 -07001556 /* cannot be reached */
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001557bail_stop:
1558 hfi1_stop_rc_timers(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001559 return ret;
1560}
1561
1562/*
1563 * We have seen an out of sequence RDMA read middle or last packet.
1564 * This ACKs SENDs and RDMA writes up to the first RDMA read or atomic SWQE.
1565 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001566static void rdma_seq_err(struct rvt_qp *qp, struct hfi1_ibport *ibp, u32 psn,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001567 struct hfi1_ctxtdata *rcd)
1568{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001569 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001570
Mike Marciniszyn68e78b32016-09-06 04:37:41 -07001571 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001572 /* Remove QP from retry timer */
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001573 hfi1_stop_rc_timers(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001574
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001575 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001576
1577 while (cmp_psn(psn, wqe->lpsn) > 0) {
1578 if (wqe->wr.opcode == IB_WR_RDMA_READ ||
1579 wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1580 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
1581 break;
1582 wqe = do_rc_completion(qp, wqe, ibp);
1583 }
1584
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001585 ibp->rvp.n_rdma_seq++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001586 qp->r_flags |= RVT_R_RDMAR_SEQ;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001587 restart_rc(qp, qp->s_last_psn + 1, 0);
1588 if (list_empty(&qp->rspwait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001589 qp->r_flags |= RVT_R_RSP_SEND;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001590 rvt_get_qp(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001591 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1592 }
1593}
1594
1595/**
1596 * rc_rcv_resp - process an incoming RC response packet
1597 * @ibp: the port this packet came in on
1598 * @ohdr: the other headers for this packet
1599 * @data: the packet data
1600 * @tlen: the packet length
1601 * @qp: the QP for this packet
1602 * @opcode: the opcode for this packet
1603 * @psn: the packet sequence number for this packet
1604 * @hdrsize: the header length
1605 * @pmtu: the path MTU
1606 *
1607 * This is called from hfi1_rc_rcv() to process an incoming RC response
1608 * packet for the given QP.
1609 * Called at interrupt level.
1610 */
1611static void rc_rcv_resp(struct hfi1_ibport *ibp,
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001612 struct ib_other_headers *ohdr,
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001613 void *data, u32 tlen, struct rvt_qp *qp,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001614 u32 opcode, u32 psn, u32 hdrsize, u32 pmtu,
1615 struct hfi1_ctxtdata *rcd)
1616{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001617 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001618 enum ib_wc_status status;
1619 unsigned long flags;
1620 int diff;
1621 u32 pad;
1622 u32 aeth;
1623 u64 val;
1624
1625 spin_lock_irqsave(&qp->s_lock, flags);
1626
Sebastian Sanchez462b6b22016-07-01 16:01:06 -07001627 trace_hfi1_ack(qp, psn);
Mike Marciniszyn83525b62015-10-26 10:28:48 -04001628
Mike Marciniszyn77241052015-07-30 15:17:43 -04001629 /* Ignore invalid responses. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001630 smp_read_barrier_depends(); /* see post_one_send */
1631 if (cmp_psn(psn, ACCESS_ONCE(qp->s_next_psn)) >= 0)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001632 goto ack_done;
1633
1634 /* Ignore duplicate responses. */
1635 diff = cmp_psn(psn, qp->s_last_psn);
1636 if (unlikely(diff <= 0)) {
1637 /* Update credits for "ghost" ACKs */
1638 if (diff == 0 && opcode == OP(ACKNOWLEDGE)) {
1639 aeth = be32_to_cpu(ohdr->u.aeth);
1640 if ((aeth >> 29) == 0)
1641 hfi1_get_credit(qp, aeth);
1642 }
1643 goto ack_done;
1644 }
1645
1646 /*
1647 * Skip everything other than the PSN we expect, if we are waiting
1648 * for a reply to a restarted RDMA read or atomic op.
1649 */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001650 if (qp->r_flags & RVT_R_RDMAR_SEQ) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001651 if (cmp_psn(psn, qp->s_last_psn + 1) != 0)
1652 goto ack_done;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001653 qp->r_flags &= ~RVT_R_RDMAR_SEQ;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001654 }
1655
1656 if (unlikely(qp->s_acked == qp->s_tail))
1657 goto ack_done;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001658 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001659 status = IB_WC_SUCCESS;
1660
1661 switch (opcode) {
1662 case OP(ACKNOWLEDGE):
1663 case OP(ATOMIC_ACKNOWLEDGE):
1664 case OP(RDMA_READ_RESPONSE_FIRST):
1665 aeth = be32_to_cpu(ohdr->u.aeth);
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001666 if (opcode == OP(ATOMIC_ACKNOWLEDGE))
1667 val = ib_u64_get(&ohdr->u.at.atomic_ack_eth);
1668 else
Mike Marciniszyn77241052015-07-30 15:17:43 -04001669 val = 0;
1670 if (!do_rc_ack(qp, aeth, psn, opcode, val, rcd) ||
1671 opcode != OP(RDMA_READ_RESPONSE_FIRST))
1672 goto ack_done;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001673 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001674 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1675 goto ack_op_err;
1676 /*
1677 * If this is a response to a resent RDMA read, we
1678 * have to be careful to copy the data to the right
1679 * location.
1680 */
1681 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1682 wqe, psn, pmtu);
1683 goto read_middle;
1684
1685 case OP(RDMA_READ_RESPONSE_MIDDLE):
1686 /* no AETH, no ACK */
1687 if (unlikely(cmp_psn(psn, qp->s_last_psn + 1)))
1688 goto ack_seq_err;
1689 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1690 goto ack_op_err;
1691read_middle:
1692 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1693 goto ack_len_err;
1694 if (unlikely(pmtu >= qp->s_rdma_read_len))
1695 goto ack_len_err;
1696
1697 /*
1698 * We got a response so update the timeout.
1699 * 4.096 usec. * (1 << qp->timeout)
1700 */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001701 qp->s_flags |= RVT_S_TIMER;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001702 mod_timer(&qp->s_timer, jiffies + qp->timeout_jiffies);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001703 if (qp->s_flags & RVT_S_WAIT_ACK) {
1704 qp->s_flags &= ~RVT_S_WAIT_ACK;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001705 hfi1_schedule_send(qp);
1706 }
1707
1708 if (opcode == OP(RDMA_READ_RESPONSE_MIDDLE))
1709 qp->s_retry = qp->s_retry_cnt;
1710
1711 /*
1712 * Update the RDMA receive state but do the copy w/o
1713 * holding the locks and blocking interrupts.
1714 */
1715 qp->s_rdma_read_len -= pmtu;
1716 update_last_psn(qp, psn);
1717 spin_unlock_irqrestore(&qp->s_lock, flags);
Dean Luick7b0b01a2016-02-03 14:35:49 -08001718 hfi1_copy_sge(&qp->s_rdma_read_sge, data, pmtu, 0, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001719 goto bail;
1720
1721 case OP(RDMA_READ_RESPONSE_ONLY):
1722 aeth = be32_to_cpu(ohdr->u.aeth);
1723 if (!do_rc_ack(qp, aeth, psn, opcode, 0, rcd))
1724 goto ack_done;
1725 /* Get the number of bytes the message was padded by. */
1726 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1727 /*
1728 * Check that the data size is >= 0 && <= pmtu.
1729 * Remember to account for ICRC (4).
1730 */
1731 if (unlikely(tlen < (hdrsize + pad + 4)))
1732 goto ack_len_err;
1733 /*
1734 * If this is a response to a resent RDMA read, we
1735 * have to be careful to copy the data to the right
1736 * location.
1737 */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001738 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001739 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1740 wqe, psn, pmtu);
1741 goto read_last;
1742
1743 case OP(RDMA_READ_RESPONSE_LAST):
1744 /* ACKs READ req. */
1745 if (unlikely(cmp_psn(psn, qp->s_last_psn + 1)))
1746 goto ack_seq_err;
1747 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1748 goto ack_op_err;
1749 /* Get the number of bytes the message was padded by. */
1750 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1751 /*
1752 * Check that the data size is >= 1 && <= pmtu.
1753 * Remember to account for ICRC (4).
1754 */
1755 if (unlikely(tlen <= (hdrsize + pad + 4)))
1756 goto ack_len_err;
1757read_last:
1758 tlen -= hdrsize + pad + 4;
1759 if (unlikely(tlen != qp->s_rdma_read_len))
1760 goto ack_len_err;
1761 aeth = be32_to_cpu(ohdr->u.aeth);
Dean Luick7b0b01a2016-02-03 14:35:49 -08001762 hfi1_copy_sge(&qp->s_rdma_read_sge, data, tlen, 0, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001763 WARN_ON(qp->s_rdma_read_sge.num_sge);
Jubin John50e5dcb2016-02-14 20:19:41 -08001764 (void)do_rc_ack(qp, aeth, psn,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001765 OP(RDMA_READ_RESPONSE_LAST), 0, rcd);
1766 goto ack_done;
1767 }
1768
1769ack_op_err:
1770 status = IB_WC_LOC_QP_OP_ERR;
1771 goto ack_err;
1772
1773ack_seq_err:
1774 rdma_seq_err(qp, ibp, psn, rcd);
1775 goto ack_done;
1776
1777ack_len_err:
1778 status = IB_WC_LOC_LEN_ERR;
1779ack_err:
1780 if (qp->s_last == qp->s_acked) {
1781 hfi1_send_complete(qp, wqe, status);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08001782 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001783 }
1784ack_done:
1785 spin_unlock_irqrestore(&qp->s_lock, flags);
1786bail:
1787 return;
1788}
1789
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05001790static inline void rc_defered_ack(struct hfi1_ctxtdata *rcd,
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001791 struct rvt_qp *qp)
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05001792{
1793 if (list_empty(&qp->rspwait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001794 qp->r_flags |= RVT_R_RSP_NAK;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001795 rvt_get_qp(qp);
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05001796 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1797 }
1798}
1799
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001800static inline void rc_cancel_ack(struct rvt_qp *qp)
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05001801{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08001802 struct hfi1_qp_priv *priv = qp->priv;
1803
1804 priv->r_adefered = 0;
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05001805 if (list_empty(&qp->rspwait))
1806 return;
1807 list_del_init(&qp->rspwait);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001808 qp->r_flags &= ~RVT_R_RSP_NAK;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001809 rvt_put_qp(qp);
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05001810}
1811
Mike Marciniszyn77241052015-07-30 15:17:43 -04001812/**
1813 * rc_rcv_error - process an incoming duplicate or error RC packet
1814 * @ohdr: the other headers for this packet
1815 * @data: the packet data
1816 * @qp: the QP for this packet
1817 * @opcode: the opcode for this packet
1818 * @psn: the packet sequence number for this packet
1819 * @diff: the difference between the PSN and the expected PSN
1820 *
1821 * This is called from hfi1_rc_rcv() to process an unexpected
1822 * incoming RC packet for the given QP.
1823 * Called at interrupt level.
1824 * Return 1 if no more processing is needed; otherwise return 0 to
1825 * schedule a response to be sent.
1826 */
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001827static noinline int rc_rcv_error(struct ib_other_headers *ohdr, void *data,
Jubin John17fb4f22016-02-14 20:21:52 -08001828 struct rvt_qp *qp, u32 opcode, u32 psn,
1829 int diff, struct hfi1_ctxtdata *rcd)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001830{
1831 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001832 struct rvt_ack_entry *e;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001833 unsigned long flags;
1834 u8 i, prev;
1835 int old_req;
1836
Sebastian Sanchez462b6b22016-07-01 16:01:06 -07001837 trace_hfi1_rcv_error(qp, psn);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001838 if (diff > 0) {
1839 /*
1840 * Packet sequence error.
1841 * A NAK will ACK earlier sends and RDMA writes.
1842 * Don't queue the NAK if we already sent one.
1843 */
1844 if (!qp->r_nak_state) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001845 ibp->rvp.n_rc_seqnak++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001846 qp->r_nak_state = IB_NAK_PSN_ERROR;
1847 /* Use the expected PSN. */
1848 qp->r_ack_psn = qp->r_psn;
1849 /*
1850 * Wait to send the sequence NAK until all packets
1851 * in the receive queue have been processed.
1852 * Otherwise, we end up propagating congestion.
1853 */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05001854 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001855 }
1856 goto done;
1857 }
1858
1859 /*
1860 * Handle a duplicate request. Don't re-execute SEND, RDMA
1861 * write or atomic op. Don't NAK errors, just silently drop
1862 * the duplicate request. Note that r_sge, r_len, and
1863 * r_rcv_len may be in use so don't modify them.
1864 *
1865 * We are supposed to ACK the earliest duplicate PSN but we
1866 * can coalesce an outstanding duplicate ACK. We have to
1867 * send the earliest so that RDMA reads can be restarted at
1868 * the requester's expected PSN.
1869 *
1870 * First, find where this duplicate PSN falls within the
1871 * ACKs previously sent.
1872 * old_req is true if there is an older response that is scheduled
1873 * to be sent before sending this one.
1874 */
1875 e = NULL;
1876 old_req = 1;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001877 ibp->rvp.n_rc_dupreq++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001878
1879 spin_lock_irqsave(&qp->s_lock, flags);
1880
1881 for (i = qp->r_head_ack_queue; ; i = prev) {
1882 if (i == qp->s_tail_ack_queue)
1883 old_req = 0;
1884 if (i)
1885 prev = i - 1;
1886 else
1887 prev = HFI1_MAX_RDMA_ATOMIC;
1888 if (prev == qp->r_head_ack_queue) {
1889 e = NULL;
1890 break;
1891 }
1892 e = &qp->s_ack_queue[prev];
1893 if (!e->opcode) {
1894 e = NULL;
1895 break;
1896 }
1897 if (cmp_psn(psn, e->psn) >= 0) {
1898 if (prev == qp->s_tail_ack_queue &&
1899 cmp_psn(psn, e->lpsn) <= 0)
1900 old_req = 0;
1901 break;
1902 }
1903 }
1904 switch (opcode) {
1905 case OP(RDMA_READ_REQUEST): {
1906 struct ib_reth *reth;
1907 u32 offset;
1908 u32 len;
1909
1910 /*
1911 * If we didn't find the RDMA read request in the ack queue,
1912 * we can ignore this request.
1913 */
1914 if (!e || e->opcode != OP(RDMA_READ_REQUEST))
1915 goto unlock_done;
1916 /* RETH comes after BTH */
1917 reth = &ohdr->u.rc.reth;
1918 /*
1919 * Address range must be a subset of the original
1920 * request and start on pmtu boundaries.
1921 * We reuse the old ack_queue slot since the requester
1922 * should not back up and request an earlier PSN for the
1923 * same request.
1924 */
1925 offset = delta_psn(psn, e->psn) * qp->pmtu;
1926 len = be32_to_cpu(reth->length);
1927 if (unlikely(offset + len != e->rdma_sge.sge_length))
1928 goto unlock_done;
1929 if (e->rdma_sge.mr) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001930 rvt_put_mr(e->rdma_sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001931 e->rdma_sge.mr = NULL;
1932 }
1933 if (len != 0) {
1934 u32 rkey = be32_to_cpu(reth->rkey);
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001935 u64 vaddr = get_ib_reth_vaddr(reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001936 int ok;
1937
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001938 ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr, rkey,
1939 IB_ACCESS_REMOTE_READ);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001940 if (unlikely(!ok))
1941 goto unlock_done;
1942 } else {
1943 e->rdma_sge.vaddr = NULL;
1944 e->rdma_sge.length = 0;
1945 e->rdma_sge.sge_length = 0;
1946 }
1947 e->psn = psn;
1948 if (old_req)
1949 goto unlock_done;
1950 qp->s_tail_ack_queue = prev;
1951 break;
1952 }
1953
1954 case OP(COMPARE_SWAP):
1955 case OP(FETCH_ADD): {
1956 /*
1957 * If we didn't find the atomic request in the ack queue
1958 * or the send tasklet is already backed up to send an
1959 * earlier entry, we can ignore this request.
1960 */
Jubin John50e5dcb2016-02-14 20:19:41 -08001961 if (!e || e->opcode != (u8)opcode || old_req)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001962 goto unlock_done;
1963 qp->s_tail_ack_queue = prev;
1964 break;
1965 }
1966
1967 default:
1968 /*
1969 * Ignore this operation if it doesn't request an ACK
1970 * or an earlier RDMA read or atomic is going to be resent.
1971 */
1972 if (!(psn & IB_BTH_REQ_ACK) || old_req)
1973 goto unlock_done;
1974 /*
1975 * Resend the most recent ACK if this request is
1976 * after all the previous RDMA reads and atomics.
1977 */
1978 if (i == qp->r_head_ack_queue) {
1979 spin_unlock_irqrestore(&qp->s_lock, flags);
1980 qp->r_nak_state = 0;
1981 qp->r_ack_psn = qp->r_psn - 1;
1982 goto send_ack;
1983 }
1984
1985 /*
1986 * Resend the RDMA read or atomic op which
1987 * ACKs this duplicate request.
1988 */
1989 qp->s_tail_ack_queue = i;
1990 break;
1991 }
1992 qp->s_ack_state = OP(ACKNOWLEDGE);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001993 qp->s_flags |= RVT_S_RESP_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001994 qp->r_nak_state = 0;
1995 hfi1_schedule_send(qp);
1996
1997unlock_done:
1998 spin_unlock_irqrestore(&qp->s_lock, flags);
1999done:
2000 return 1;
2001
2002send_ack:
2003 return 0;
2004}
2005
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002006void hfi1_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
Mike Marciniszyn77241052015-07-30 15:17:43 -04002007{
2008 unsigned long flags;
2009 int lastwqe;
2010
2011 spin_lock_irqsave(&qp->s_lock, flags);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002012 lastwqe = rvt_error_qp(qp, err);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002013 spin_unlock_irqrestore(&qp->s_lock, flags);
2014
2015 if (lastwqe) {
2016 struct ib_event ev;
2017
2018 ev.device = qp->ibqp.device;
2019 ev.element.qp = &qp->ibqp;
2020 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
2021 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
2022 }
2023}
2024
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002025static inline void update_ack_queue(struct rvt_qp *qp, unsigned n)
Mike Marciniszyn77241052015-07-30 15:17:43 -04002026{
2027 unsigned next;
2028
2029 next = n + 1;
2030 if (next > HFI1_MAX_RDMA_ATOMIC)
2031 next = 0;
2032 qp->s_tail_ack_queue = next;
2033 qp->s_ack_state = OP(ACKNOWLEDGE);
2034}
2035
2036static void log_cca_event(struct hfi1_pportdata *ppd, u8 sl, u32 rlid,
2037 u32 lqpn, u32 rqpn, u8 svc_type)
2038{
2039 struct opa_hfi1_cong_log_event_internal *cc_event;
Dean Luickb77d7132015-10-26 10:28:43 -04002040 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002041
2042 if (sl >= OPA_MAX_SLS)
2043 return;
2044
Dean Luickb77d7132015-10-26 10:28:43 -04002045 spin_lock_irqsave(&ppd->cc_log_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002046
Jubin John8638b772016-02-14 20:19:24 -08002047 ppd->threshold_cong_event_map[sl / 8] |= 1 << (sl % 8);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002048 ppd->threshold_event_counter++;
2049
2050 cc_event = &ppd->cc_events[ppd->cc_log_idx++];
2051 if (ppd->cc_log_idx == OPA_CONG_LOG_ELEMS)
2052 ppd->cc_log_idx = 0;
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002053 cc_event->lqpn = lqpn & RVT_QPN_MASK;
2054 cc_event->rqpn = rqpn & RVT_QPN_MASK;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002055 cc_event->sl = sl;
2056 cc_event->svc_type = svc_type;
2057 cc_event->rlid = rlid;
2058 /* keep timestamp in units of 1.024 usec */
2059 cc_event->timestamp = ktime_to_ns(ktime_get()) / 1024;
2060
Dean Luickb77d7132015-10-26 10:28:43 -04002061 spin_unlock_irqrestore(&ppd->cc_log_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002062}
2063
2064void process_becn(struct hfi1_pportdata *ppd, u8 sl, u16 rlid, u32 lqpn,
2065 u32 rqpn, u8 svc_type)
2066{
2067 struct cca_timer *cca_timer;
2068 u16 ccti, ccti_incr, ccti_timer, ccti_limit;
2069 u8 trigger_threshold;
2070 struct cc_state *cc_state;
Dean Luickb77d7132015-10-26 10:28:43 -04002071 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002072
2073 if (sl >= OPA_MAX_SLS)
2074 return;
2075
Mike Marciniszyn77241052015-07-30 15:17:43 -04002076 cc_state = get_cc_state(ppd);
2077
Jubin Johnd125a6c2016-02-14 20:19:49 -08002078 if (!cc_state)
Mike Marciniszyn77241052015-07-30 15:17:43 -04002079 return;
2080
2081 /*
2082 * 1) increase CCTI (for this SL)
2083 * 2) select IPG (i.e., call set_link_ipg())
2084 * 3) start timer
2085 */
2086 ccti_limit = cc_state->cct.ccti_limit;
2087 ccti_incr = cc_state->cong_setting.entries[sl].ccti_increase;
2088 ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer;
2089 trigger_threshold =
2090 cc_state->cong_setting.entries[sl].trigger_threshold;
2091
Dean Luickb77d7132015-10-26 10:28:43 -04002092 spin_lock_irqsave(&ppd->cca_timer_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002093
Jubin Johnd35cf7442016-04-14 08:31:53 -07002094 cca_timer = &ppd->cca_timer[sl];
Mike Marciniszyn77241052015-07-30 15:17:43 -04002095 if (cca_timer->ccti < ccti_limit) {
2096 if (cca_timer->ccti + ccti_incr <= ccti_limit)
2097 cca_timer->ccti += ccti_incr;
2098 else
2099 cca_timer->ccti = ccti_limit;
2100 set_link_ipg(ppd);
2101 }
2102
Mike Marciniszyn77241052015-07-30 15:17:43 -04002103 ccti = cca_timer->ccti;
2104
2105 if (!hrtimer_active(&cca_timer->hrtimer)) {
2106 /* ccti_timer is in units of 1.024 usec */
2107 unsigned long nsec = 1024 * ccti_timer;
2108
2109 hrtimer_start(&cca_timer->hrtimer, ns_to_ktime(nsec),
2110 HRTIMER_MODE_REL);
2111 }
2112
Jubin Johnd35cf7442016-04-14 08:31:53 -07002113 spin_unlock_irqrestore(&ppd->cca_timer_lock, flags);
2114
Mike Marciniszyn77241052015-07-30 15:17:43 -04002115 if ((trigger_threshold != 0) && (ccti >= trigger_threshold))
2116 log_cca_event(ppd, sl, rlid, lqpn, rqpn, svc_type);
2117}
2118
2119/**
2120 * hfi1_rc_rcv - process an incoming RC packet
2121 * @rcd: the context pointer
2122 * @hdr: the header of this packet
2123 * @rcv_flags: flags relevant to rcv processing
2124 * @data: the packet data
2125 * @tlen: the packet length
2126 * @qp: the QP for this packet
2127 *
2128 * This is called from qp_rcv() to process an incoming RC packet
2129 * for the given QP.
Dean Luickb77d7132015-10-26 10:28:43 -04002130 * May be called at interrupt level.
Mike Marciniszyn77241052015-07-30 15:17:43 -04002131 */
2132void hfi1_rc_rcv(struct hfi1_packet *packet)
2133{
2134 struct hfi1_ctxtdata *rcd = packet->rcd;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002135 struct ib_header *hdr = packet->hdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002136 u32 rcv_flags = packet->rcv_flags;
2137 void *data = packet->ebuf;
2138 u32 tlen = packet->tlen;
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002139 struct rvt_qp *qp = packet->qp;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002140 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002141 struct ib_other_headers *ohdr = packet->ohdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002142 u32 bth0, opcode;
2143 u32 hdrsize = packet->hlen;
2144 u32 psn;
2145 u32 pad;
2146 struct ib_wc wc;
2147 u32 pmtu = qp->pmtu;
2148 int diff;
2149 struct ib_reth *reth;
2150 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002151 int ret, is_fecn = 0;
Dean Luick7b0b01a2016-02-03 14:35:49 -08002152 int copy_last = 0;
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002153 u32 rkey;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002154
Mike Marciniszyn68e78b32016-09-06 04:37:41 -07002155 lockdep_assert_held(&qp->r_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002156 bth0 = be32_to_cpu(ohdr->bth[0]);
2157 if (hfi1_ruc_check_hdr(ibp, hdr, rcv_flags & HFI1_HAS_GRH, qp, bth0))
2158 return;
2159
Mitko Haralanov5fd2b562016-07-25 13:38:07 -07002160 is_fecn = process_ecn(qp, packet, false);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002161
2162 psn = be32_to_cpu(ohdr->bth[2]);
Arthur Kepner977940b2015-11-04 21:10:10 -05002163 opcode = (bth0 >> 24) & 0xff;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002164
2165 /*
2166 * Process responses (ACKs) before anything else. Note that the
2167 * packet sequence number will be for something in the send work
2168 * queue rather than the expected receive packet sequence number.
2169 * In other words, this QP is the requester.
2170 */
2171 if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
2172 opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
2173 rc_rcv_resp(ibp, ohdr, data, tlen, qp, opcode, psn,
2174 hdrsize, pmtu, rcd);
2175 if (is_fecn)
2176 goto send_ack;
2177 return;
2178 }
2179
2180 /* Compute 24 bits worth of difference. */
2181 diff = delta_psn(psn, qp->r_psn);
2182 if (unlikely(diff)) {
2183 if (rc_rcv_error(ohdr, data, qp, opcode, psn, diff, rcd))
2184 return;
2185 goto send_ack;
2186 }
2187
2188 /* Check for opcode sequence errors. */
2189 switch (qp->r_state) {
2190 case OP(SEND_FIRST):
2191 case OP(SEND_MIDDLE):
2192 if (opcode == OP(SEND_MIDDLE) ||
2193 opcode == OP(SEND_LAST) ||
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002194 opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
2195 opcode == OP(SEND_LAST_WITH_INVALIDATE))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002196 break;
2197 goto nack_inv;
2198
2199 case OP(RDMA_WRITE_FIRST):
2200 case OP(RDMA_WRITE_MIDDLE):
2201 if (opcode == OP(RDMA_WRITE_MIDDLE) ||
2202 opcode == OP(RDMA_WRITE_LAST) ||
2203 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
2204 break;
2205 goto nack_inv;
2206
2207 default:
2208 if (opcode == OP(SEND_MIDDLE) ||
2209 opcode == OP(SEND_LAST) ||
2210 opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002211 opcode == OP(SEND_LAST_WITH_INVALIDATE) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04002212 opcode == OP(RDMA_WRITE_MIDDLE) ||
2213 opcode == OP(RDMA_WRITE_LAST) ||
2214 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
2215 goto nack_inv;
2216 /*
2217 * Note that it is up to the requester to not send a new
2218 * RDMA read or atomic operation before receiving an ACK
2219 * for the previous operation.
2220 */
2221 break;
2222 }
2223
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08002224 if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002225 qp_comm_est(qp);
2226
2227 /* OK, process the packet. */
2228 switch (opcode) {
2229 case OP(SEND_FIRST):
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002230 ret = hfi1_rvt_get_rwqe(qp, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002231 if (ret < 0)
2232 goto nack_op_err;
2233 if (!ret)
2234 goto rnr_nak;
2235 qp->r_rcv_len = 0;
2236 /* FALLTHROUGH */
2237 case OP(SEND_MIDDLE):
2238 case OP(RDMA_WRITE_MIDDLE):
2239send_middle:
2240 /* Check for invalid length PMTU or posted rwqe len. */
2241 if (unlikely(tlen != (hdrsize + pmtu + 4)))
2242 goto nack_inv;
2243 qp->r_rcv_len += pmtu;
2244 if (unlikely(qp->r_rcv_len > qp->r_len))
2245 goto nack_inv;
Dean Luick7b0b01a2016-02-03 14:35:49 -08002246 hfi1_copy_sge(&qp->r_sge, data, pmtu, 1, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002247 break;
2248
2249 case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
2250 /* consume RWQE */
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002251 ret = hfi1_rvt_get_rwqe(qp, 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002252 if (ret < 0)
2253 goto nack_op_err;
2254 if (!ret)
2255 goto rnr_nak;
2256 goto send_last_imm;
2257
2258 case OP(SEND_ONLY):
2259 case OP(SEND_ONLY_WITH_IMMEDIATE):
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002260 case OP(SEND_ONLY_WITH_INVALIDATE):
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002261 ret = hfi1_rvt_get_rwqe(qp, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002262 if (ret < 0)
2263 goto nack_op_err;
2264 if (!ret)
2265 goto rnr_nak;
2266 qp->r_rcv_len = 0;
2267 if (opcode == OP(SEND_ONLY))
2268 goto no_immediate_data;
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002269 if (opcode == OP(SEND_ONLY_WITH_INVALIDATE))
2270 goto send_last_inv;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002271 /* FALLTHROUGH for SEND_ONLY_WITH_IMMEDIATE */
2272 case OP(SEND_LAST_WITH_IMMEDIATE):
2273send_last_imm:
2274 wc.ex.imm_data = ohdr->u.imm_data;
2275 wc.wc_flags = IB_WC_WITH_IMM;
2276 goto send_last;
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002277 case OP(SEND_LAST_WITH_INVALIDATE):
2278send_last_inv:
2279 rkey = be32_to_cpu(ohdr->u.ieth);
2280 if (rvt_invalidate_rkey(qp, rkey))
2281 goto no_immediate_data;
2282 wc.ex.invalidate_rkey = rkey;
2283 wc.wc_flags = IB_WC_WITH_INVALIDATE;
2284 goto send_last;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002285 case OP(RDMA_WRITE_LAST):
Dean Luick7b0b01a2016-02-03 14:35:49 -08002286 copy_last = ibpd_to_rvtpd(qp->ibqp.pd)->user;
2287 /* fall through */
2288 case OP(SEND_LAST):
Mike Marciniszyn77241052015-07-30 15:17:43 -04002289no_immediate_data:
2290 wc.wc_flags = 0;
2291 wc.ex.imm_data = 0;
2292send_last:
2293 /* Get the number of bytes the message was padded by. */
2294 pad = (bth0 >> 20) & 3;
2295 /* Check for invalid length. */
2296 /* LAST len should be >= 1 */
2297 if (unlikely(tlen < (hdrsize + pad + 4)))
2298 goto nack_inv;
2299 /* Don't count the CRC. */
2300 tlen -= (hdrsize + pad + 4);
2301 wc.byte_len = tlen + qp->r_rcv_len;
2302 if (unlikely(wc.byte_len > qp->r_len))
2303 goto nack_inv;
Dean Luick7b0b01a2016-02-03 14:35:49 -08002304 hfi1_copy_sge(&qp->r_sge, data, tlen, 1, copy_last);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002305 rvt_put_ss(&qp->r_sge);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002306 qp->r_msn++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08002307 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002308 break;
2309 wc.wr_id = qp->r_wr_id;
2310 wc.status = IB_WC_SUCCESS;
2311 if (opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE) ||
2312 opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
2313 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
2314 else
2315 wc.opcode = IB_WC_RECV;
2316 wc.qp = &qp->ibqp;
2317 wc.src_qp = qp->remote_qpn;
2318 wc.slid = qp->remote_ah_attr.dlid;
2319 /*
2320 * It seems that IB mandates the presence of an SL in a
2321 * work completion only for the UD transport (see section
2322 * 11.4.2 of IBTA Vol. 1).
2323 *
2324 * However, the way the SL is chosen below is consistent
2325 * with the way that IB/qib works and is trying avoid
2326 * introducing incompatibilities.
2327 *
2328 * See also OPA Vol. 1, section 9.7.6, and table 9-17.
2329 */
2330 wc.sl = qp->remote_ah_attr.sl;
2331 /* zero fields that are N/A */
2332 wc.vendor_err = 0;
2333 wc.pkey_index = 0;
2334 wc.dlid_path_bits = 0;
2335 wc.port_num = 0;
2336 /* Signal completion event if the solicited bit is set. */
Dennis Dalessandroabd712d2016-01-19 14:43:22 -08002337 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
2338 (bth0 & IB_BTH_SOLICITED) != 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002339 break;
2340
Mike Marciniszyn77241052015-07-30 15:17:43 -04002341 case OP(RDMA_WRITE_ONLY):
Dean Luick7b0b01a2016-02-03 14:35:49 -08002342 copy_last = 1;
2343 /* fall through */
2344 case OP(RDMA_WRITE_FIRST):
Mike Marciniszyn77241052015-07-30 15:17:43 -04002345 case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
2346 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
2347 goto nack_inv;
2348 /* consume RWQE */
2349 reth = &ohdr->u.rc.reth;
2350 qp->r_len = be32_to_cpu(reth->length);
2351 qp->r_rcv_len = 0;
2352 qp->r_sge.sg_list = NULL;
2353 if (qp->r_len != 0) {
2354 u32 rkey = be32_to_cpu(reth->rkey);
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002355 u64 vaddr = get_ib_reth_vaddr(reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002356 int ok;
2357
2358 /* Check rkey & NAK */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002359 ok = rvt_rkey_ok(qp, &qp->r_sge.sge, qp->r_len, vaddr,
2360 rkey, IB_ACCESS_REMOTE_WRITE);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002361 if (unlikely(!ok))
2362 goto nack_acc;
2363 qp->r_sge.num_sge = 1;
2364 } else {
2365 qp->r_sge.num_sge = 0;
2366 qp->r_sge.sge.mr = NULL;
2367 qp->r_sge.sge.vaddr = NULL;
2368 qp->r_sge.sge.length = 0;
2369 qp->r_sge.sge.sge_length = 0;
2370 }
2371 if (opcode == OP(RDMA_WRITE_FIRST))
2372 goto send_middle;
2373 else if (opcode == OP(RDMA_WRITE_ONLY))
2374 goto no_immediate_data;
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002375 ret = hfi1_rvt_get_rwqe(qp, 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002376 if (ret < 0)
2377 goto nack_op_err;
2378 if (!ret)
2379 goto rnr_nak;
2380 wc.ex.imm_data = ohdr->u.rc.imm_data;
2381 wc.wc_flags = IB_WC_WITH_IMM;
2382 goto send_last;
2383
2384 case OP(RDMA_READ_REQUEST): {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002385 struct rvt_ack_entry *e;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002386 u32 len;
2387 u8 next;
2388
2389 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
2390 goto nack_inv;
2391 next = qp->r_head_ack_queue + 1;
2392 /* s_ack_queue is size HFI1_MAX_RDMA_ATOMIC+1 so use > not >= */
2393 if (next > HFI1_MAX_RDMA_ATOMIC)
2394 next = 0;
2395 spin_lock_irqsave(&qp->s_lock, flags);
2396 if (unlikely(next == qp->s_tail_ack_queue)) {
2397 if (!qp->s_ack_queue[next].sent)
2398 goto nack_inv_unlck;
2399 update_ack_queue(qp, next);
2400 }
2401 e = &qp->s_ack_queue[qp->r_head_ack_queue];
2402 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002403 rvt_put_mr(e->rdma_sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002404 e->rdma_sge.mr = NULL;
2405 }
2406 reth = &ohdr->u.rc.reth;
2407 len = be32_to_cpu(reth->length);
2408 if (len) {
2409 u32 rkey = be32_to_cpu(reth->rkey);
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002410 u64 vaddr = get_ib_reth_vaddr(reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002411 int ok;
2412
2413 /* Check rkey & NAK */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002414 ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr,
2415 rkey, IB_ACCESS_REMOTE_READ);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002416 if (unlikely(!ok))
2417 goto nack_acc_unlck;
2418 /*
2419 * Update the next expected PSN. We add 1 later
2420 * below, so only add the remainder here.
2421 */
2422 if (len > pmtu)
2423 qp->r_psn += (len - 1) / pmtu;
2424 } else {
2425 e->rdma_sge.mr = NULL;
2426 e->rdma_sge.vaddr = NULL;
2427 e->rdma_sge.length = 0;
2428 e->rdma_sge.sge_length = 0;
2429 }
2430 e->opcode = opcode;
2431 e->sent = 0;
2432 e->psn = psn;
2433 e->lpsn = qp->r_psn;
2434 /*
2435 * We need to increment the MSN here instead of when we
2436 * finish sending the result since a duplicate request would
2437 * increment it more than once.
2438 */
2439 qp->r_msn++;
2440 qp->r_psn++;
2441 qp->r_state = opcode;
2442 qp->r_nak_state = 0;
2443 qp->r_head_ack_queue = next;
2444
2445 /* Schedule the send tasklet. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08002446 qp->s_flags |= RVT_S_RESP_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002447 hfi1_schedule_send(qp);
2448
2449 spin_unlock_irqrestore(&qp->s_lock, flags);
2450 if (is_fecn)
2451 goto send_ack;
2452 return;
2453 }
2454
2455 case OP(COMPARE_SWAP):
2456 case OP(FETCH_ADD): {
2457 struct ib_atomic_eth *ateth;
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002458 struct rvt_ack_entry *e;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002459 u64 vaddr;
2460 atomic64_t *maddr;
2461 u64 sdata;
2462 u32 rkey;
2463 u8 next;
2464
2465 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
2466 goto nack_inv;
2467 next = qp->r_head_ack_queue + 1;
2468 if (next > HFI1_MAX_RDMA_ATOMIC)
2469 next = 0;
2470 spin_lock_irqsave(&qp->s_lock, flags);
2471 if (unlikely(next == qp->s_tail_ack_queue)) {
2472 if (!qp->s_ack_queue[next].sent)
2473 goto nack_inv_unlck;
2474 update_ack_queue(qp, next);
2475 }
2476 e = &qp->s_ack_queue[qp->r_head_ack_queue];
2477 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002478 rvt_put_mr(e->rdma_sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002479 e->rdma_sge.mr = NULL;
2480 }
2481 ateth = &ohdr->u.atomic_eth;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002482 vaddr = get_ib_ateth_vaddr(ateth);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002483 if (unlikely(vaddr & (sizeof(u64) - 1)))
2484 goto nack_inv_unlck;
2485 rkey = be32_to_cpu(ateth->rkey);
2486 /* Check rkey & NAK */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002487 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
2488 vaddr, rkey,
2489 IB_ACCESS_REMOTE_ATOMIC)))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002490 goto nack_acc_unlck;
2491 /* Perform atomic OP and save result. */
Jubin John50e5dcb2016-02-14 20:19:41 -08002492 maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002493 sdata = get_ib_ateth_swap(ateth);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002494 e->atomic_data = (opcode == OP(FETCH_ADD)) ?
Jubin John50e5dcb2016-02-14 20:19:41 -08002495 (u64)atomic64_add_return(sdata, maddr) - sdata :
2496 (u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002497 get_ib_ateth_compare(ateth),
Mike Marciniszyn77241052015-07-30 15:17:43 -04002498 sdata);
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002499 rvt_put_mr(qp->r_sge.sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002500 qp->r_sge.num_sge = 0;
2501 e->opcode = opcode;
2502 e->sent = 0;
2503 e->psn = psn;
2504 e->lpsn = psn;
2505 qp->r_msn++;
2506 qp->r_psn++;
2507 qp->r_state = opcode;
2508 qp->r_nak_state = 0;
2509 qp->r_head_ack_queue = next;
2510
2511 /* Schedule the send tasklet. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08002512 qp->s_flags |= RVT_S_RESP_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002513 hfi1_schedule_send(qp);
2514
2515 spin_unlock_irqrestore(&qp->s_lock, flags);
2516 if (is_fecn)
2517 goto send_ack;
2518 return;
2519 }
2520
2521 default:
2522 /* NAK unknown opcodes. */
2523 goto nack_inv;
2524 }
2525 qp->r_psn++;
2526 qp->r_state = opcode;
2527 qp->r_ack_psn = psn;
2528 qp->r_nak_state = 0;
2529 /* Send an ACK if requested or required. */
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05002530 if (psn & IB_BTH_REQ_ACK) {
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08002531 struct hfi1_qp_priv *priv = qp->priv;
2532
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05002533 if (packet->numpkt == 0) {
2534 rc_cancel_ack(qp);
2535 goto send_ack;
2536 }
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08002537 if (priv->r_adefered >= HFI1_PSN_CREDIT) {
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05002538 rc_cancel_ack(qp);
2539 goto send_ack;
2540 }
2541 if (unlikely(is_fecn)) {
2542 rc_cancel_ack(qp);
2543 goto send_ack;
2544 }
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08002545 priv->r_adefered++;
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05002546 rc_defered_ack(rcd, qp);
2547 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04002548 return;
2549
2550rnr_nak:
Harish Chegondibf640092016-03-05 08:49:29 -08002551 qp->r_nak_state = qp->r_min_rnr_timer | IB_RNR_NAK;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002552 qp->r_ack_psn = qp->r_psn;
2553 /* Queue RNR NAK for later */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05002554 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002555 return;
2556
2557nack_op_err:
2558 hfi1_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
2559 qp->r_nak_state = IB_NAK_REMOTE_OPERATIONAL_ERROR;
2560 qp->r_ack_psn = qp->r_psn;
2561 /* Queue NAK for later */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05002562 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002563 return;
2564
2565nack_inv_unlck:
2566 spin_unlock_irqrestore(&qp->s_lock, flags);
2567nack_inv:
2568 hfi1_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
2569 qp->r_nak_state = IB_NAK_INVALID_REQUEST;
2570 qp->r_ack_psn = qp->r_psn;
2571 /* Queue NAK for later */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05002572 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002573 return;
2574
2575nack_acc_unlck:
2576 spin_unlock_irqrestore(&qp->s_lock, flags);
2577nack_acc:
2578 hfi1_rc_error(qp, IB_WC_LOC_PROT_ERR);
2579 qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
2580 qp->r_ack_psn = qp->r_psn;
2581send_ack:
2582 hfi1_send_rc_ack(rcd, qp, is_fecn);
2583}
2584
2585void hfi1_rc_hdrerr(
2586 struct hfi1_ctxtdata *rcd,
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002587 struct ib_header *hdr,
Mike Marciniszyn77241052015-07-30 15:17:43 -04002588 u32 rcv_flags,
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002589 struct rvt_qp *qp)
Mike Marciniszyn77241052015-07-30 15:17:43 -04002590{
2591 int has_grh = rcv_flags & HFI1_HAS_GRH;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002592 struct ib_other_headers *ohdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002593 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
2594 int diff;
Nicolas Iooss49c32032015-09-20 16:07:15 +02002595 u32 opcode;
Arthur Kepner977940b2015-11-04 21:10:10 -05002596 u32 psn, bth0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002597
2598 /* Check for GRH */
2599 ohdr = &hdr->u.oth;
2600 if (has_grh)
2601 ohdr = &hdr->u.l.oth;
2602
Arthur Kepner977940b2015-11-04 21:10:10 -05002603 bth0 = be32_to_cpu(ohdr->bth[0]);
2604 if (hfi1_ruc_check_hdr(ibp, hdr, has_grh, qp, bth0))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002605 return;
2606
2607 psn = be32_to_cpu(ohdr->bth[2]);
Arthur Kepner977940b2015-11-04 21:10:10 -05002608 opcode = (bth0 >> 24) & 0xff;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002609
2610 /* Only deal with RDMA Writes for now */
2611 if (opcode < IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST) {
2612 diff = delta_psn(psn, qp->r_psn);
2613 if (!qp->r_nak_state && diff >= 0) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08002614 ibp->rvp.n_rc_seqnak++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002615 qp->r_nak_state = IB_NAK_PSN_ERROR;
2616 /* Use the expected PSN. */
2617 qp->r_ack_psn = qp->r_psn;
2618 /*
2619 * Wait to send the sequence
2620 * NAK until all packets
2621 * in the receive queue have
2622 * been processed.
2623 * Otherwise, we end up
2624 * propagating congestion.
2625 */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05002626 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002627 } /* Out of sequence NAK */
2628 } /* QP Request NAKs */
2629}