blob: d32f0c86a6234e88aab71e83893cb44033196561 [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 */
Mike Marciniszynb374e062016-09-25 07:40:58 -070058#define OP(x) RC_OP(x)
Mike Marciniszyn77241052015-07-30 15:17:43 -040059
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
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800187static u32 restart_sge(struct rvt_sge_state *ss, struct rvt_swqe *wqe,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400188 u32 psn, u32 pmtu)
189{
190 u32 len;
191
192 len = delta_psn(psn, wqe->psn) * pmtu;
193 ss->sge = wqe->sg_list[0];
194 ss->sg_list = wqe->sg_list + 1;
195 ss->num_sge = wqe->wr.num_sge;
196 ss->total_len = wqe->length;
197 hfi1_skip_sge(ss, len, 0);
198 return wqe->length - len;
199}
200
Mike Marciniszyn77241052015-07-30 15:17:43 -0400201/**
202 * make_rc_ack - construct a response packet (ACK, NAK, or RDMA read)
203 * @dev: the device for this QP
204 * @qp: a pointer to the QP
205 * @ohdr: a pointer to the IB header being constructed
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800206 * @ps: the xmit packet state
Mike Marciniszyn77241052015-07-30 15:17:43 -0400207 *
208 * Return 1 if constructed; otherwise, return 0.
209 * Note that we are in the responder's side of the QP context.
210 * Note the QP s_lock must be held.
211 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800212static int make_rc_ack(struct hfi1_ibdev *dev, struct rvt_qp *qp,
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700213 struct ib_other_headers *ohdr,
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800214 struct hfi1_pkt_state *ps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400215{
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800216 struct rvt_ack_entry *e;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400217 u32 hwords;
218 u32 len;
219 u32 bth0;
220 u32 bth2;
221 int middle = 0;
Mike Marciniszyn1235bef2016-02-14 12:45:09 -0800222 u32 pmtu = qp->pmtu;
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800223 struct hfi1_qp_priv *priv = qp->priv;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400224
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700225 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400226 /* Don't send an ACK if we aren't supposed to. */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800227 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400228 goto bail;
229
230 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
231 hwords = 5;
232
233 switch (qp->s_ack_state) {
234 case OP(RDMA_READ_RESPONSE_LAST):
235 case OP(RDMA_READ_RESPONSE_ONLY):
236 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
237 if (e->rdma_sge.mr) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800238 rvt_put_mr(e->rdma_sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400239 e->rdma_sge.mr = NULL;
240 }
241 /* FALLTHROUGH */
242 case OP(ATOMIC_ACKNOWLEDGE):
243 /*
244 * We can increment the tail pointer now that the last
245 * response has been sent instead of only being
246 * constructed.
247 */
248 if (++qp->s_tail_ack_queue > HFI1_MAX_RDMA_ATOMIC)
249 qp->s_tail_ack_queue = 0;
250 /* FALLTHROUGH */
251 case OP(SEND_ONLY):
252 case OP(ACKNOWLEDGE):
253 /* Check for no next entry in the queue. */
254 if (qp->r_head_ack_queue == qp->s_tail_ack_queue) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800255 if (qp->s_flags & RVT_S_ACK_PENDING)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400256 goto normal;
257 goto bail;
258 }
259
260 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
261 if (e->opcode == OP(RDMA_READ_REQUEST)) {
262 /*
263 * If a RDMA read response is being resent and
264 * we haven't seen the duplicate request yet,
265 * then stop sending the remaining responses the
266 * responder has seen until the requester re-sends it.
267 */
268 len = e->rdma_sge.sge_length;
269 if (len && !e->rdma_sge.mr) {
270 qp->s_tail_ack_queue = qp->r_head_ack_queue;
271 goto bail;
272 }
273 /* Copy SGE state in case we need to resend */
Mike Marciniszync239a5b2016-02-14 12:44:52 -0800274 ps->s_txreq->mr = e->rdma_sge.mr;
275 if (ps->s_txreq->mr)
276 rvt_get_mr(ps->s_txreq->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400277 qp->s_ack_rdma_sge.sge = e->rdma_sge;
278 qp->s_ack_rdma_sge.num_sge = 1;
279 qp->s_cur_sge = &qp->s_ack_rdma_sge;
280 if (len > pmtu) {
281 len = pmtu;
282 qp->s_ack_state = OP(RDMA_READ_RESPONSE_FIRST);
283 } else {
284 qp->s_ack_state = OP(RDMA_READ_RESPONSE_ONLY);
285 e->sent = 1;
286 }
287 ohdr->u.aeth = hfi1_compute_aeth(qp);
288 hwords++;
289 qp->s_ack_rdma_psn = e->psn;
290 bth2 = mask_psn(qp->s_ack_rdma_psn++);
291 } else {
292 /* COMPARE_SWAP or FETCH_ADD */
293 qp->s_cur_sge = NULL;
294 len = 0;
295 qp->s_ack_state = OP(ATOMIC_ACKNOWLEDGE);
296 ohdr->u.at.aeth = hfi1_compute_aeth(qp);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700297 ib_u64_put(e->atomic_data, &ohdr->u.at.atomic_ack_eth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400298 hwords += sizeof(ohdr->u.at) / sizeof(u32);
299 bth2 = mask_psn(e->psn);
300 e->sent = 1;
301 }
302 bth0 = qp->s_ack_state << 24;
303 break;
304
305 case OP(RDMA_READ_RESPONSE_FIRST):
306 qp->s_ack_state = OP(RDMA_READ_RESPONSE_MIDDLE);
307 /* FALLTHROUGH */
308 case OP(RDMA_READ_RESPONSE_MIDDLE):
309 qp->s_cur_sge = &qp->s_ack_rdma_sge;
Mike Marciniszync239a5b2016-02-14 12:44:52 -0800310 ps->s_txreq->mr = qp->s_ack_rdma_sge.sge.mr;
311 if (ps->s_txreq->mr)
312 rvt_get_mr(ps->s_txreq->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400313 len = qp->s_ack_rdma_sge.sge.sge_length;
314 if (len > pmtu) {
315 len = pmtu;
316 middle = HFI1_CAP_IS_KSET(SDMA_AHG);
317 } else {
318 ohdr->u.aeth = hfi1_compute_aeth(qp);
319 hwords++;
320 qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
321 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
322 e->sent = 1;
323 }
324 bth0 = qp->s_ack_state << 24;
325 bth2 = mask_psn(qp->s_ack_rdma_psn++);
326 break;
327
328 default:
329normal:
330 /*
331 * Send a regular ACK.
332 * Set the s_ack_state so we wait until after sending
333 * the ACK before setting s_ack_state to ACKNOWLEDGE
334 * (see above).
335 */
336 qp->s_ack_state = OP(SEND_ONLY);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800337 qp->s_flags &= ~RVT_S_ACK_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400338 qp->s_cur_sge = NULL;
339 if (qp->s_nak_state)
340 ohdr->u.aeth =
341 cpu_to_be32((qp->r_msn & HFI1_MSN_MASK) |
342 (qp->s_nak_state <<
343 HFI1_AETH_CREDIT_SHIFT));
344 else
345 ohdr->u.aeth = hfi1_compute_aeth(qp);
346 hwords++;
347 len = 0;
348 bth0 = OP(ACKNOWLEDGE) << 24;
349 bth2 = mask_psn(qp->s_ack_psn);
350 }
351 qp->s_rdma_ack_cnt++;
352 qp->s_hdrwords = hwords;
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800353 ps->s_txreq->sde = priv->s_sde;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400354 qp->s_cur_size = len;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800355 hfi1_make_ruc_header(qp, ohdr, bth0, bth2, middle, ps);
Jianxin Xiongaa0ad412016-02-26 13:33:13 -0800356 /* pbc */
357 ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400358 return 1;
359
360bail:
361 qp->s_ack_state = OP(ACKNOWLEDGE);
362 /*
363 * Ensure s_rdma_ack_cnt changes are committed prior to resetting
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800364 * RVT_S_RESP_PENDING
Mike Marciniszyn77241052015-07-30 15:17:43 -0400365 */
366 smp_wmb();
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800367 qp->s_flags &= ~(RVT_S_RESP_PENDING
368 | RVT_S_ACK_PENDING
369 | RVT_S_AHG_VALID);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400370 return 0;
371}
372
373/**
374 * hfi1_make_rc_req - construct a request packet (SEND, RDMA r/w, ATOMIC)
375 * @qp: a pointer to the QP
376 *
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800377 * Assumes s_lock is held.
378 *
Mike Marciniszyn77241052015-07-30 15:17:43 -0400379 * Return 1 if constructed; otherwise, return 0.
380 */
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800381int hfi1_make_rc_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400382{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800383 struct hfi1_qp_priv *priv = qp->priv;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400384 struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700385 struct ib_other_headers *ohdr;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800386 struct rvt_sge_state *ss;
387 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400388 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
389 u32 hwords = 5;
390 u32 len;
391 u32 bth0 = 0;
392 u32 bth2;
393 u32 pmtu = qp->pmtu;
394 char newreq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400395 int middle = 0;
396 int delta;
397
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700398 lockdep_assert_held(&qp->s_lock);
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800399 ps->s_txreq = get_txreq(ps->dev, qp);
400 if (IS_ERR(ps->s_txreq))
401 goto bail_no_tx;
402
403 ohdr = &ps->s_txreq->phdr.hdr.u.oth;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400404 if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800405 ohdr = &ps->s_txreq->phdr.hdr.u.l.oth;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400406
Mike Marciniszyn77241052015-07-30 15:17:43 -0400407 /* Sending responses has higher priority over sending requests. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800408 if ((qp->s_flags & RVT_S_RESP_PENDING) &&
Mike Marciniszyn1235bef2016-02-14 12:45:09 -0800409 make_rc_ack(dev, qp, ohdr, ps))
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800410 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400411
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800412 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_SEND_OK)) {
413 if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400414 goto bail;
415 /* We are in the error state, flush the work request. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800416 smp_read_barrier_depends(); /* see post_one_send() */
417 if (qp->s_last == ACCESS_ONCE(qp->s_head))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400418 goto bail;
419 /* If DMAs are in progress, we can't flush immediately. */
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800420 if (iowait_sdma_pending(&priv->s_iowait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800421 qp->s_flags |= RVT_S_WAIT_DMA;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400422 goto bail;
423 }
424 clear_ahg(qp);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800425 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400426 hfi1_send_complete(qp, wqe, qp->s_last != qp->s_acked ?
427 IB_WC_SUCCESS : IB_WC_WR_FLUSH_ERR);
428 /* will get called again */
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800429 goto done_free_tx;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400430 }
431
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800432 if (qp->s_flags & (RVT_S_WAIT_RNR | RVT_S_WAIT_ACK))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400433 goto bail;
434
435 if (cmp_psn(qp->s_psn, qp->s_sending_hpsn) <= 0) {
436 if (cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800437 qp->s_flags |= RVT_S_WAIT_PSN;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400438 goto bail;
439 }
440 qp->s_sending_psn = qp->s_psn;
441 qp->s_sending_hpsn = qp->s_psn - 1;
442 }
443
444 /* Send a request. */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800445 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400446 switch (qp->s_state) {
447 default:
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800448 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400449 goto bail;
450 /*
451 * Resend an old request or start a new one.
452 *
453 * We keep track of the current SWQE so that
454 * we don't reset the "furthest progress" state
455 * if we need to back up.
456 */
457 newreq = 0;
458 if (qp->s_cur == qp->s_tail) {
459 /* Check if send work queue is empty. */
460 if (qp->s_tail == qp->s_head) {
461 clear_ahg(qp);
462 goto bail;
463 }
464 /*
465 * If a fence is requested, wait for previous
466 * RDMA read and atomic operations to finish.
467 */
468 if ((wqe->wr.send_flags & IB_SEND_FENCE) &&
469 qp->s_num_rd_atomic) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800470 qp->s_flags |= RVT_S_WAIT_FENCE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400471 goto bail;
472 }
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700473 /*
474 * Local operations are processed immediately
475 * after all prior requests have completed
476 */
477 if (wqe->wr.opcode == IB_WR_REG_MR ||
478 wqe->wr.opcode == IB_WR_LOCAL_INV) {
Jianxin Xiongd9b13c22016-07-25 13:39:45 -0700479 int local_ops = 0;
480 int err = 0;
481
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700482 if (qp->s_last != qp->s_cur)
483 goto bail;
484 if (++qp->s_cur == qp->s_size)
485 qp->s_cur = 0;
486 if (++qp->s_tail == qp->s_size)
487 qp->s_tail = 0;
Jianxin Xiongd9b13c22016-07-25 13:39:45 -0700488 if (!(wqe->wr.send_flags &
489 RVT_SEND_COMPLETION_ONLY)) {
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700490 err = rvt_invalidate_rkey(
491 qp,
492 wqe->wr.ex.invalidate_rkey);
Jianxin Xiongd9b13c22016-07-25 13:39:45 -0700493 local_ops = 1;
494 }
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700495 hfi1_send_complete(qp, wqe,
496 err ? IB_WC_LOC_PROT_ERR
497 : IB_WC_SUCCESS);
Jianxin Xiongd9b13c22016-07-25 13:39:45 -0700498 if (local_ops)
499 atomic_dec(&qp->local_ops_pending);
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700500 qp->s_hdrwords = 0;
501 goto done_free_tx;
502 }
503
Mike Marciniszyn77241052015-07-30 15:17:43 -0400504 newreq = 1;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800505 qp->s_psn = wqe->psn;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400506 }
507 /*
508 * Note that we have to be careful not to modify the
509 * original work request since we may need to resend
510 * it.
511 */
512 len = wqe->length;
513 ss = &qp->s_sge;
514 bth2 = mask_psn(qp->s_psn);
515 switch (wqe->wr.opcode) {
516 case IB_WR_SEND:
517 case IB_WR_SEND_WITH_IMM:
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700518 case IB_WR_SEND_WITH_INV:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400519 /* If no credit, return. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800520 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
Mike Marciniszyn77241052015-07-30 15:17:43 -0400521 cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800522 qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400523 goto bail;
524 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400525 if (len > pmtu) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400526 qp->s_state = OP(SEND_FIRST);
527 len = pmtu;
528 break;
529 }
Jubin Johne4909742016-02-14 20:22:00 -0800530 if (wqe->wr.opcode == IB_WR_SEND) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400531 qp->s_state = OP(SEND_ONLY);
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700532 } else if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400533 qp->s_state = OP(SEND_ONLY_WITH_IMMEDIATE);
534 /* Immediate data comes after the BTH */
535 ohdr->u.imm_data = wqe->wr.ex.imm_data;
536 hwords += 1;
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700537 } else {
538 qp->s_state = OP(SEND_ONLY_WITH_INVALIDATE);
539 /* Invalidate rkey comes after the BTH */
540 ohdr->u.ieth = cpu_to_be32(
541 wqe->wr.ex.invalidate_rkey);
542 hwords += 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400543 }
544 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
545 bth0 |= IB_BTH_SOLICITED;
546 bth2 |= IB_BTH_REQ_ACK;
547 if (++qp->s_cur == qp->s_size)
548 qp->s_cur = 0;
549 break;
550
551 case IB_WR_RDMA_WRITE:
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800552 if (newreq && !(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400553 qp->s_lsn++;
554 /* FALLTHROUGH */
555 case IB_WR_RDMA_WRITE_WITH_IMM:
556 /* If no credit, return. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800557 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
Mike Marciniszyn77241052015-07-30 15:17:43 -0400558 cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800559 qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400560 goto bail;
561 }
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700562 put_ib_reth_vaddr(
563 wqe->rdma_wr.remote_addr,
564 &ohdr->u.rc.reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400565 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100566 cpu_to_be32(wqe->rdma_wr.rkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400567 ohdr->u.rc.reth.length = cpu_to_be32(len);
568 hwords += sizeof(struct ib_reth) / sizeof(u32);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400569 if (len > pmtu) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400570 qp->s_state = OP(RDMA_WRITE_FIRST);
571 len = pmtu;
572 break;
573 }
Jubin Johne4909742016-02-14 20:22:00 -0800574 if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400575 qp->s_state = OP(RDMA_WRITE_ONLY);
Jubin Johne4909742016-02-14 20:22:00 -0800576 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400577 qp->s_state =
578 OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
579 /* Immediate data comes after RETH */
580 ohdr->u.rc.imm_data = wqe->wr.ex.imm_data;
581 hwords += 1;
582 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
583 bth0 |= IB_BTH_SOLICITED;
584 }
585 bth2 |= IB_BTH_REQ_ACK;
586 if (++qp->s_cur == qp->s_size)
587 qp->s_cur = 0;
588 break;
589
590 case IB_WR_RDMA_READ:
591 /*
592 * Don't allow more operations to be started
593 * than the QP limits allow.
594 */
595 if (newreq) {
596 if (qp->s_num_rd_atomic >=
597 qp->s_max_rd_atomic) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800598 qp->s_flags |= RVT_S_WAIT_RDMAR;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400599 goto bail;
600 }
601 qp->s_num_rd_atomic++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800602 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400603 qp->s_lsn++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400604 }
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700605 put_ib_reth_vaddr(
606 wqe->rdma_wr.remote_addr,
607 &ohdr->u.rc.reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400608 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100609 cpu_to_be32(wqe->rdma_wr.rkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400610 ohdr->u.rc.reth.length = cpu_to_be32(len);
611 qp->s_state = OP(RDMA_READ_REQUEST);
612 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
613 ss = NULL;
614 len = 0;
615 bth2 |= IB_BTH_REQ_ACK;
616 if (++qp->s_cur == qp->s_size)
617 qp->s_cur = 0;
618 break;
619
620 case IB_WR_ATOMIC_CMP_AND_SWP:
621 case IB_WR_ATOMIC_FETCH_AND_ADD:
622 /*
623 * Don't allow more operations to be started
624 * than the QP limits allow.
625 */
626 if (newreq) {
627 if (qp->s_num_rd_atomic >=
628 qp->s_max_rd_atomic) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800629 qp->s_flags |= RVT_S_WAIT_RDMAR;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400630 goto bail;
631 }
632 qp->s_num_rd_atomic++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800633 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400634 qp->s_lsn++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400635 }
636 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
637 qp->s_state = OP(COMPARE_SWAP);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700638 put_ib_ateth_swap(wqe->atomic_wr.swap,
639 &ohdr->u.atomic_eth);
640 put_ib_ateth_compare(wqe->atomic_wr.compare_add,
641 &ohdr->u.atomic_eth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400642 } else {
643 qp->s_state = OP(FETCH_ADD);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700644 put_ib_ateth_swap(wqe->atomic_wr.compare_add,
645 &ohdr->u.atomic_eth);
646 put_ib_ateth_compare(0, &ohdr->u.atomic_eth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400647 }
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700648 put_ib_ateth_vaddr(wqe->atomic_wr.remote_addr,
649 &ohdr->u.atomic_eth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400650 ohdr->u.atomic_eth.rkey = cpu_to_be32(
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100651 wqe->atomic_wr.rkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400652 hwords += sizeof(struct ib_atomic_eth) / sizeof(u32);
653 ss = NULL;
654 len = 0;
655 bth2 |= IB_BTH_REQ_ACK;
656 if (++qp->s_cur == qp->s_size)
657 qp->s_cur = 0;
658 break;
659
660 default:
661 goto bail;
662 }
663 qp->s_sge.sge = wqe->sg_list[0];
664 qp->s_sge.sg_list = wqe->sg_list + 1;
665 qp->s_sge.num_sge = wqe->wr.num_sge;
666 qp->s_sge.total_len = wqe->length;
667 qp->s_len = wqe->length;
668 if (newreq) {
669 qp->s_tail++;
670 if (qp->s_tail >= qp->s_size)
671 qp->s_tail = 0;
672 }
673 if (wqe->wr.opcode == IB_WR_RDMA_READ)
674 qp->s_psn = wqe->lpsn + 1;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800675 else
Mike Marciniszyn77241052015-07-30 15:17:43 -0400676 qp->s_psn++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400677 break;
678
679 case OP(RDMA_READ_RESPONSE_FIRST):
680 /*
681 * qp->s_state is normally set to the opcode of the
682 * last packet constructed for new requests and therefore
683 * is never set to RDMA read response.
684 * RDMA_READ_RESPONSE_FIRST is used by the ACK processing
685 * thread to indicate a SEND needs to be restarted from an
686 * earlier PSN without interfering with the sending thread.
687 * See restart_rc().
688 */
689 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
690 /* FALLTHROUGH */
691 case OP(SEND_FIRST):
692 qp->s_state = OP(SEND_MIDDLE);
693 /* FALLTHROUGH */
694 case OP(SEND_MIDDLE):
695 bth2 = mask_psn(qp->s_psn++);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400696 ss = &qp->s_sge;
697 len = qp->s_len;
698 if (len > pmtu) {
699 len = pmtu;
700 middle = HFI1_CAP_IS_KSET(SDMA_AHG);
701 break;
702 }
Jubin Johne4909742016-02-14 20:22:00 -0800703 if (wqe->wr.opcode == IB_WR_SEND) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400704 qp->s_state = OP(SEND_LAST);
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700705 } else if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400706 qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
707 /* Immediate data comes after the BTH */
708 ohdr->u.imm_data = wqe->wr.ex.imm_data;
709 hwords += 1;
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700710 } else {
711 qp->s_state = OP(SEND_LAST_WITH_INVALIDATE);
712 /* invalidate data comes after the BTH */
713 ohdr->u.ieth = cpu_to_be32(wqe->wr.ex.invalidate_rkey);
714 hwords += 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400715 }
716 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
717 bth0 |= IB_BTH_SOLICITED;
718 bth2 |= IB_BTH_REQ_ACK;
719 qp->s_cur++;
720 if (qp->s_cur >= qp->s_size)
721 qp->s_cur = 0;
722 break;
723
724 case OP(RDMA_READ_RESPONSE_LAST):
725 /*
726 * qp->s_state is normally set to the opcode of the
727 * last packet constructed for new requests and therefore
728 * is never set to RDMA read response.
729 * RDMA_READ_RESPONSE_LAST is used by the ACK processing
730 * thread to indicate a RDMA write needs to be restarted from
731 * an earlier PSN without interfering with the sending thread.
732 * See restart_rc().
733 */
734 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
735 /* FALLTHROUGH */
736 case OP(RDMA_WRITE_FIRST):
737 qp->s_state = OP(RDMA_WRITE_MIDDLE);
738 /* FALLTHROUGH */
739 case OP(RDMA_WRITE_MIDDLE):
740 bth2 = mask_psn(qp->s_psn++);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400741 ss = &qp->s_sge;
742 len = qp->s_len;
743 if (len > pmtu) {
744 len = pmtu;
745 middle = HFI1_CAP_IS_KSET(SDMA_AHG);
746 break;
747 }
Jubin Johne4909742016-02-14 20:22:00 -0800748 if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400749 qp->s_state = OP(RDMA_WRITE_LAST);
Jubin Johne4909742016-02-14 20:22:00 -0800750 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400751 qp->s_state = OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
752 /* Immediate data comes after the BTH */
753 ohdr->u.imm_data = wqe->wr.ex.imm_data;
754 hwords += 1;
755 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
756 bth0 |= IB_BTH_SOLICITED;
757 }
758 bth2 |= IB_BTH_REQ_ACK;
759 qp->s_cur++;
760 if (qp->s_cur >= qp->s_size)
761 qp->s_cur = 0;
762 break;
763
764 case OP(RDMA_READ_RESPONSE_MIDDLE):
765 /*
766 * qp->s_state is normally set to the opcode of the
767 * last packet constructed for new requests and therefore
768 * is never set to RDMA read response.
769 * RDMA_READ_RESPONSE_MIDDLE is used by the ACK processing
770 * thread to indicate a RDMA read needs to be restarted from
771 * an earlier PSN without interfering with the sending thread.
772 * See restart_rc().
773 */
774 len = (delta_psn(qp->s_psn, wqe->psn)) * pmtu;
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700775 put_ib_reth_vaddr(
776 wqe->rdma_wr.remote_addr + len,
777 &ohdr->u.rc.reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400778 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100779 cpu_to_be32(wqe->rdma_wr.rkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400780 ohdr->u.rc.reth.length = cpu_to_be32(wqe->length - len);
781 qp->s_state = OP(RDMA_READ_REQUEST);
782 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
783 bth2 = mask_psn(qp->s_psn) | IB_BTH_REQ_ACK;
784 qp->s_psn = wqe->lpsn + 1;
785 ss = NULL;
786 len = 0;
787 qp->s_cur++;
788 if (qp->s_cur == qp->s_size)
789 qp->s_cur = 0;
790 break;
791 }
792 qp->s_sending_hpsn = bth2;
793 delta = delta_psn(bth2, wqe->psn);
794 if (delta && delta % HFI1_PSN_CREDIT == 0)
795 bth2 |= IB_BTH_REQ_ACK;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800796 if (qp->s_flags & RVT_S_SEND_ONE) {
797 qp->s_flags &= ~RVT_S_SEND_ONE;
798 qp->s_flags |= RVT_S_WAIT_ACK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400799 bth2 |= IB_BTH_REQ_ACK;
800 }
801 qp->s_len -= len;
802 qp->s_hdrwords = hwords;
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800803 ps->s_txreq->sde = priv->s_sde;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400804 qp->s_cur_sge = ss;
805 qp->s_cur_size = len;
806 hfi1_make_ruc_header(
807 qp,
808 ohdr,
809 bth0 | (qp->s_state << 24),
810 bth2,
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800811 middle,
812 ps);
Jianxin Xiongaa0ad412016-02-26 13:33:13 -0800813 /* pbc */
814 ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800815 return 1;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800816
817done_free_tx:
818 hfi1_put_txreq(ps->s_txreq);
819 ps->s_txreq = NULL;
820 return 1;
821
Mike Marciniszyn77241052015-07-30 15:17:43 -0400822bail:
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800823 hfi1_put_txreq(ps->s_txreq);
824
825bail_no_tx:
826 ps->s_txreq = NULL;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800827 qp->s_flags &= ~RVT_S_BUSY;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800828 qp->s_hdrwords = 0;
829 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400830}
831
832/**
833 * hfi1_send_rc_ack - Construct an ACK packet and send it
834 * @qp: a pointer to the QP
835 *
836 * This is called from hfi1_rc_rcv() and handle_receive_interrupt().
837 * Note that RDMA reads and atomics are handled in the
838 * send side QP state and tasklet.
839 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800840void hfi1_send_rc_ack(struct hfi1_ctxtdata *rcd, struct rvt_qp *qp,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400841 int is_fecn)
842{
843 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
844 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
845 u64 pbc, pbc_flags = 0;
846 u16 lrh0;
847 u16 sc5;
848 u32 bth0;
849 u32 hwords;
850 u32 vl, plen;
851 struct send_context *sc;
852 struct pio_buf *pbuf;
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700853 struct ib_header hdr;
854 struct ib_other_headers *ohdr;
Dean Luickb77d7132015-10-26 10:28:43 -0400855 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400856
857 /* Don't send ACK or NAK if a RDMA read or atomic is pending. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800858 if (qp->s_flags & RVT_S_RESP_PENDING)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400859 goto queue_ack;
860
861 /* Ensure s_rdma_ack_cnt changes are committed */
862 smp_read_barrier_depends();
863 if (qp->s_rdma_ack_cnt)
864 goto queue_ack;
865
866 /* Construct the header */
867 /* header size in 32-bit words LRH+BTH+AETH = (8+12+4)/4 */
868 hwords = 6;
869 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
870 hwords += hfi1_make_grh(ibp, &hdr.u.l.grh,
871 &qp->remote_ah_attr.grh, hwords, 0);
872 ohdr = &hdr.u.l.oth;
873 lrh0 = HFI1_LRH_GRH;
874 } else {
875 ohdr = &hdr.u.oth;
876 lrh0 = HFI1_LRH_BTH;
877 }
878 /* read pkey_index w/o lock (its atomic) */
879 bth0 = hfi1_get_pkey(ibp, qp->s_pkey_index) | (OP(ACKNOWLEDGE) << 24);
880 if (qp->s_mig_state == IB_MIG_MIGRATED)
881 bth0 |= IB_BTH_MIG_REQ;
882 if (qp->r_nak_state)
883 ohdr->u.aeth = cpu_to_be32((qp->r_msn & HFI1_MSN_MASK) |
884 (qp->r_nak_state <<
885 HFI1_AETH_CREDIT_SHIFT));
886 else
887 ohdr->u.aeth = hfi1_compute_aeth(qp);
888 sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl];
889 /* set PBC_DC_INFO bit (aka SC[4]) in pbc_flags */
890 pbc_flags |= ((!!(sc5 & 0x10)) << PBC_DC_INFO_SHIFT);
891 lrh0 |= (sc5 & 0xf) << 12 | (qp->remote_ah_attr.sl & 0xf) << 4;
892 hdr.lrh[0] = cpu_to_be16(lrh0);
893 hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
894 hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
895 hdr.lrh[3] = cpu_to_be16(ppd->lid | qp->remote_ah_attr.src_path_bits);
896 ohdr->bth[0] = cpu_to_be32(bth0);
897 ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
898 ohdr->bth[1] |= cpu_to_be32((!!is_fecn) << HFI1_BECN_SHIFT);
899 ohdr->bth[2] = cpu_to_be32(mask_psn(qp->r_ack_psn));
900
901 /* Don't try to send ACKs if the link isn't ACTIVE */
902 if (driver_lstate(ppd) != IB_PORT_ACTIVE)
903 return;
904
905 sc = rcd->sc;
906 plen = 2 /* PBC */ + hwords;
907 vl = sc_to_vlt(ppd->dd, sc5);
908 pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
909
910 pbuf = sc_buffer_alloc(sc, plen, NULL, NULL);
911 if (!pbuf) {
912 /*
913 * We have no room to send at the moment. Pass
914 * responsibility for sending the ACK to the send tasklet
915 * so that when enough buffer space becomes available,
916 * the ACK is sent ahead of other outgoing packets.
917 */
918 goto queue_ack;
919 }
920
Mike Marciniszyn1db78ee2016-03-07 11:35:19 -0800921 trace_ack_output_ibhdr(dd_from_ibdev(qp->ibqp.device), &hdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400922
923 /* write the pbc and data */
924 ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc, &hdr, hwords);
925
926 return;
927
928queue_ack:
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800929 this_cpu_inc(*ibp->rvp.rc_qacks);
Dean Luickb77d7132015-10-26 10:28:43 -0400930 spin_lock_irqsave(&qp->s_lock, flags);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800931 qp->s_flags |= RVT_S_ACK_PENDING | RVT_S_RESP_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400932 qp->s_nak_state = qp->r_nak_state;
933 qp->s_ack_psn = qp->r_ack_psn;
934 if (is_fecn)
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800935 qp->s_flags |= RVT_S_ECN;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400936
937 /* Schedule the send tasklet. */
938 hfi1_schedule_send(qp);
Dean Luickb77d7132015-10-26 10:28:43 -0400939 spin_unlock_irqrestore(&qp->s_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400940}
941
942/**
943 * reset_psn - reset the QP state to send starting from PSN
944 * @qp: the QP
945 * @psn: the packet sequence number to restart at
946 *
947 * This is called from hfi1_rc_rcv() to process an incoming RC ACK
948 * for the given QP.
949 * Called at interrupt level with the QP s_lock held.
950 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800951static void reset_psn(struct rvt_qp *qp, u32 psn)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400952{
953 u32 n = qp->s_acked;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800954 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, n);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400955 u32 opcode;
956
Mike Marciniszyn68e78b32016-09-06 04:37:41 -0700957 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400958 qp->s_cur = n;
959
960 /*
961 * If we are starting the request from the beginning,
962 * let the normal send code handle initialization.
963 */
964 if (cmp_psn(psn, wqe->psn) <= 0) {
965 qp->s_state = OP(SEND_LAST);
966 goto done;
967 }
968
969 /* Find the work request opcode corresponding to the given PSN. */
970 opcode = wqe->wr.opcode;
971 for (;;) {
972 int diff;
973
974 if (++n == qp->s_size)
975 n = 0;
976 if (n == qp->s_tail)
977 break;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800978 wqe = rvt_get_swqe_ptr(qp, n);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400979 diff = cmp_psn(psn, wqe->psn);
980 if (diff < 0)
981 break;
982 qp->s_cur = n;
983 /*
984 * If we are starting the request from the beginning,
985 * let the normal send code handle initialization.
986 */
987 if (diff == 0) {
988 qp->s_state = OP(SEND_LAST);
989 goto done;
990 }
991 opcode = wqe->wr.opcode;
992 }
993
994 /*
995 * Set the state to restart in the middle of a request.
996 * Don't change the s_sge, s_cur_sge, or s_cur_size.
997 * See hfi1_make_rc_req().
998 */
999 switch (opcode) {
1000 case IB_WR_SEND:
1001 case IB_WR_SEND_WITH_IMM:
1002 qp->s_state = OP(RDMA_READ_RESPONSE_FIRST);
1003 break;
1004
1005 case IB_WR_RDMA_WRITE:
1006 case IB_WR_RDMA_WRITE_WITH_IMM:
1007 qp->s_state = OP(RDMA_READ_RESPONSE_LAST);
1008 break;
1009
1010 case IB_WR_RDMA_READ:
1011 qp->s_state = OP(RDMA_READ_RESPONSE_MIDDLE);
1012 break;
1013
1014 default:
1015 /*
1016 * This case shouldn't happen since its only
1017 * one PSN per req.
1018 */
1019 qp->s_state = OP(SEND_LAST);
1020 }
1021done:
1022 qp->s_psn = psn;
1023 /*
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001024 * Set RVT_S_WAIT_PSN as rc_complete() may start the timer
Mike Marciniszyn77241052015-07-30 15:17:43 -04001025 * asynchronously before the send tasklet can get scheduled.
1026 * Doing it in hfi1_make_rc_req() is too late.
1027 */
1028 if ((cmp_psn(qp->s_psn, qp->s_sending_hpsn) <= 0) &&
1029 (cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0))
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001030 qp->s_flags |= RVT_S_WAIT_PSN;
1031 qp->s_flags &= ~RVT_S_AHG_VALID;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001032}
1033
1034/*
1035 * Back up requester to resend the last un-ACKed request.
1036 * The QP r_lock and s_lock should be held and interrupts disabled.
1037 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001038static void restart_rc(struct rvt_qp *qp, u32 psn, int wait)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001039{
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001040 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001041 struct hfi1_ibport *ibp;
1042
Mike Marciniszyn68e78b32016-09-06 04:37:41 -07001043 lockdep_assert_held(&qp->r_lock);
1044 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001045 if (qp->s_retry == 0) {
1046 if (qp->s_mig_state == IB_MIG_ARMED) {
1047 hfi1_migrate_qp(qp);
1048 qp->s_retry = qp->s_retry_cnt;
1049 } else if (qp->s_last == qp->s_acked) {
1050 hfi1_send_complete(qp, wqe, IB_WC_RETRY_EXC_ERR);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08001051 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001052 return;
Jubin Johne4909742016-02-14 20:22:00 -08001053 } else { /* need to handle delayed completion */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001054 return;
Jubin Johne4909742016-02-14 20:22:00 -08001055 }
1056 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001057 qp->s_retry--;
Jubin Johne4909742016-02-14 20:22:00 -08001058 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001059
1060 ibp = to_iport(qp->ibqp.device, qp->port_num);
1061 if (wqe->wr.opcode == IB_WR_RDMA_READ)
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001062 ibp->rvp.n_rc_resends++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001063 else
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001064 ibp->rvp.n_rc_resends += delta_psn(qp->s_psn, psn);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001065
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001066 qp->s_flags &= ~(RVT_S_WAIT_FENCE | RVT_S_WAIT_RDMAR |
1067 RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_PSN |
1068 RVT_S_WAIT_ACK);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001069 if (wait)
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001070 qp->s_flags |= RVT_S_SEND_ONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001071 reset_psn(qp, psn);
1072}
1073
1074/*
1075 * This is called from s_timer for missing responses.
1076 */
Mike Marciniszyn08279d52016-02-04 10:59:36 -08001077void hfi1_rc_timeout(unsigned long arg)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001078{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001079 struct rvt_qp *qp = (struct rvt_qp *)arg;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001080 struct hfi1_ibport *ibp;
1081 unsigned long flags;
1082
1083 spin_lock_irqsave(&qp->r_lock, flags);
1084 spin_lock(&qp->s_lock);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001085 if (qp->s_flags & RVT_S_TIMER) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001086 ibp = to_iport(qp->ibqp.device, qp->port_num);
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001087 ibp->rvp.n_rc_timeouts++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001088 qp->s_flags &= ~RVT_S_TIMER;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001089 del_timer(&qp->s_timer);
Sebastian Sanchez462b6b22016-07-01 16:01:06 -07001090 trace_hfi1_timeout(qp, qp->s_last_psn + 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001091 restart_rc(qp, qp->s_last_psn + 1, 1);
1092 hfi1_schedule_send(qp);
1093 }
1094 spin_unlock(&qp->s_lock);
1095 spin_unlock_irqrestore(&qp->r_lock, flags);
1096}
1097
1098/*
1099 * This is called from s_timer for RNR timeouts.
1100 */
1101void hfi1_rc_rnr_retry(unsigned long arg)
1102{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001103 struct rvt_qp *qp = (struct rvt_qp *)arg;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001104 unsigned long flags;
1105
1106 spin_lock_irqsave(&qp->s_lock, flags);
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001107 hfi1_stop_rnr_timer(qp);
1108 hfi1_schedule_send(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001109 spin_unlock_irqrestore(&qp->s_lock, flags);
1110}
1111
1112/*
1113 * Set qp->s_sending_psn to the next PSN after the given one.
1114 * This would be psn+1 except when RDMA reads are present.
1115 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001116static void reset_sending_psn(struct rvt_qp *qp, u32 psn)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001117{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001118 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001119 u32 n = qp->s_last;
1120
Mike Marciniszyn68e78b32016-09-06 04:37:41 -07001121 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001122 /* Find the work request corresponding to the given PSN. */
1123 for (;;) {
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001124 wqe = rvt_get_swqe_ptr(qp, n);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001125 if (cmp_psn(psn, wqe->lpsn) <= 0) {
1126 if (wqe->wr.opcode == IB_WR_RDMA_READ)
1127 qp->s_sending_psn = wqe->lpsn + 1;
1128 else
1129 qp->s_sending_psn = psn + 1;
1130 break;
1131 }
1132 if (++n == qp->s_size)
1133 n = 0;
1134 if (n == qp->s_tail)
1135 break;
1136 }
1137}
1138
1139/*
1140 * This should be called with the QP s_lock held and interrupts disabled.
1141 */
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001142void hfi1_rc_send_complete(struct rvt_qp *qp, struct ib_header *hdr)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001143{
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001144 struct ib_other_headers *ohdr;
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001145 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001146 struct ib_wc wc;
1147 unsigned i;
1148 u32 opcode;
1149 u32 psn;
1150
Mike Marciniszyn68e78b32016-09-06 04:37:41 -07001151 lockdep_assert_held(&qp->s_lock);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001152 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001153 return;
1154
1155 /* Find out where the BTH is */
1156 if ((be16_to_cpu(hdr->lrh[0]) & 3) == HFI1_LRH_BTH)
1157 ohdr = &hdr->u.oth;
1158 else
1159 ohdr = &hdr->u.l.oth;
1160
1161 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
1162 if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
1163 opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
1164 WARN_ON(!qp->s_rdma_ack_cnt);
1165 qp->s_rdma_ack_cnt--;
1166 return;
1167 }
1168
1169 psn = be32_to_cpu(ohdr->bth[2]);
1170 reset_sending_psn(qp, psn);
1171
1172 /*
1173 * Start timer after a packet requesting an ACK has been sent and
1174 * there are still requests that haven't been acked.
1175 */
1176 if ((psn & IB_BTH_REQ_ACK) && qp->s_acked != qp->s_tail &&
1177 !(qp->s_flags &
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001178 (RVT_S_TIMER | RVT_S_WAIT_RNR | RVT_S_WAIT_PSN)) &&
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001179 (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001180 hfi1_add_retry_timer(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001181
1182 while (qp->s_last != qp->s_acked) {
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -08001183 u32 s_last;
1184
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001185 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001186 if (cmp_psn(wqe->lpsn, qp->s_sending_psn) >= 0 &&
1187 cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0)
1188 break;
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -08001189 s_last = qp->s_last;
1190 if (++s_last >= qp->s_size)
1191 s_last = 0;
1192 qp->s_last = s_last;
1193 /* see post_send() */
1194 barrier();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001195 for (i = 0; i < wqe->wr.num_sge; i++) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001196 struct rvt_sge *sge = &wqe->sg_list[i];
Mike Marciniszyn77241052015-07-30 15:17:43 -04001197
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001198 rvt_put_mr(sge->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001199 }
1200 /* Post a send completion queue entry if requested. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001201 if (!(qp->s_flags & RVT_S_SIGNAL_REQ_WR) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04001202 (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
1203 memset(&wc, 0, sizeof(wc));
1204 wc.wr_id = wqe->wr.wr_id;
1205 wc.status = IB_WC_SUCCESS;
1206 wc.opcode = ib_hfi1_wc_opcode[wqe->wr.opcode];
1207 wc.byte_len = wqe->length;
1208 wc.qp = &qp->ibqp;
Dennis Dalessandroabd712d2016-01-19 14:43:22 -08001209 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.send_cq), &wc, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001210 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001211 }
1212 /*
1213 * If we were waiting for sends to complete before re-sending,
1214 * and they are now complete, restart sending.
1215 */
Sebastian Sanchez462b6b22016-07-01 16:01:06 -07001216 trace_hfi1_sendcomplete(qp, psn);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001217 if (qp->s_flags & RVT_S_WAIT_PSN &&
Mike Marciniszyn77241052015-07-30 15:17:43 -04001218 cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001219 qp->s_flags &= ~RVT_S_WAIT_PSN;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001220 qp->s_sending_psn = qp->s_psn;
1221 qp->s_sending_hpsn = qp->s_psn - 1;
1222 hfi1_schedule_send(qp);
1223 }
1224}
1225
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001226static inline void update_last_psn(struct rvt_qp *qp, u32 psn)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001227{
1228 qp->s_last_psn = psn;
1229}
1230
1231/*
1232 * Generate a SWQE completion.
1233 * This is similar to hfi1_send_complete but has to check to be sure
1234 * that the SGEs are not being referenced if the SWQE is being resent.
1235 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001236static struct rvt_swqe *do_rc_completion(struct rvt_qp *qp,
1237 struct rvt_swqe *wqe,
1238 struct hfi1_ibport *ibp)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001239{
1240 struct ib_wc wc;
1241 unsigned i;
1242
Mike Marciniszyn68e78b32016-09-06 04:37:41 -07001243 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001244 /*
1245 * Don't decrement refcount and don't generate a
1246 * completion if the SWQE is being resent until the send
1247 * is finished.
1248 */
1249 if (cmp_psn(wqe->lpsn, qp->s_sending_psn) < 0 ||
1250 cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -08001251 u32 s_last;
1252
Mike Marciniszyn77241052015-07-30 15:17:43 -04001253 for (i = 0; i < wqe->wr.num_sge; i++) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001254 struct rvt_sge *sge = &wqe->sg_list[i];
Mike Marciniszyn77241052015-07-30 15:17:43 -04001255
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001256 rvt_put_mr(sge->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001257 }
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -08001258 s_last = qp->s_last;
1259 if (++s_last >= qp->s_size)
1260 s_last = 0;
1261 qp->s_last = s_last;
1262 /* see post_send() */
1263 barrier();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001264 /* Post a send completion queue entry if requested. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001265 if (!(qp->s_flags & RVT_S_SIGNAL_REQ_WR) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04001266 (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
1267 memset(&wc, 0, sizeof(wc));
1268 wc.wr_id = wqe->wr.wr_id;
1269 wc.status = IB_WC_SUCCESS;
1270 wc.opcode = ib_hfi1_wc_opcode[wqe->wr.opcode];
1271 wc.byte_len = wqe->length;
1272 wc.qp = &qp->ibqp;
Dennis Dalessandroabd712d2016-01-19 14:43:22 -08001273 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.send_cq), &wc, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001274 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001275 } else {
1276 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
1277
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001278 this_cpu_inc(*ibp->rvp.rc_delayed_comp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001279 /*
1280 * If send progress not running attempt to progress
1281 * SDMA queue.
1282 */
1283 if (ppd->dd->flags & HFI1_HAS_SEND_DMA) {
1284 struct sdma_engine *engine;
1285 u8 sc5;
1286
1287 /* For now use sc to find engine */
1288 sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl];
1289 engine = qp_to_sdma_engine(qp, sc5);
1290 sdma_engine_progress_schedule(engine);
1291 }
1292 }
1293
1294 qp->s_retry = qp->s_retry_cnt;
1295 update_last_psn(qp, wqe->lpsn);
1296
1297 /*
1298 * If we are completing a request which is in the process of
1299 * being resent, we can stop re-sending it since we know the
1300 * responder has already seen it.
1301 */
1302 if (qp->s_acked == qp->s_cur) {
1303 if (++qp->s_cur >= qp->s_size)
1304 qp->s_cur = 0;
1305 qp->s_acked = qp->s_cur;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001306 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001307 if (qp->s_acked != qp->s_tail) {
1308 qp->s_state = OP(SEND_LAST);
1309 qp->s_psn = wqe->psn;
1310 }
1311 } else {
1312 if (++qp->s_acked >= qp->s_size)
1313 qp->s_acked = 0;
1314 if (qp->state == IB_QPS_SQD && qp->s_acked == qp->s_cur)
1315 qp->s_draining = 0;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001316 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001317 }
1318 return wqe;
1319}
1320
1321/**
1322 * do_rc_ack - process an incoming RC ACK
1323 * @qp: the QP the ACK came in on
1324 * @psn: the packet sequence number of the ACK
1325 * @opcode: the opcode of the request that resulted in the ACK
1326 *
1327 * This is called from rc_rcv_resp() to process an incoming RC ACK
1328 * for the given QP.
Dean Luickb77d7132015-10-26 10:28:43 -04001329 * May be called at interrupt level, with the QP s_lock held.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001330 * Returns 1 if OK, 0 if current operation should be aborted (NAK).
1331 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001332static int do_rc_ack(struct rvt_qp *qp, u32 aeth, u32 psn, int opcode,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001333 u64 val, struct hfi1_ctxtdata *rcd)
1334{
1335 struct hfi1_ibport *ibp;
1336 enum ib_wc_status status;
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001337 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001338 int ret = 0;
1339 u32 ack_psn;
1340 int diff;
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001341 unsigned long to;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001342
Mike Marciniszyn68e78b32016-09-06 04:37:41 -07001343 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001344 /*
1345 * Note that NAKs implicitly ACK outstanding SEND and RDMA write
1346 * requests and implicitly NAK RDMA read and atomic requests issued
1347 * before the NAK'ed request. The MSN won't include the NAK'ed
1348 * request but will include an ACK'ed request(s).
1349 */
1350 ack_psn = psn;
1351 if (aeth >> 29)
1352 ack_psn--;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001353 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001354 ibp = to_iport(qp->ibqp.device, qp->port_num);
1355
1356 /*
1357 * The MSN might be for a later WQE than the PSN indicates so
1358 * only complete WQEs that the PSN finishes.
1359 */
1360 while ((diff = delta_psn(ack_psn, wqe->lpsn)) >= 0) {
1361 /*
1362 * RDMA_READ_RESPONSE_ONLY is a special case since
1363 * we want to generate completion events for everything
1364 * before the RDMA read, copy the data, then generate
1365 * the completion for the read.
1366 */
1367 if (wqe->wr.opcode == IB_WR_RDMA_READ &&
1368 opcode == OP(RDMA_READ_RESPONSE_ONLY) &&
1369 diff == 0) {
1370 ret = 1;
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001371 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001372 }
1373 /*
1374 * If this request is a RDMA read or atomic, and the ACK is
1375 * for a later operation, this ACK NAKs the RDMA read or
1376 * atomic. In other words, only a RDMA_READ_LAST or ONLY
1377 * can ACK a RDMA read and likewise for atomic ops. Note
1378 * that the NAK case can only happen if relaxed ordering is
1379 * used and requests are sent after an RDMA read or atomic
1380 * is sent but before the response is received.
1381 */
1382 if ((wqe->wr.opcode == IB_WR_RDMA_READ &&
1383 (opcode != OP(RDMA_READ_RESPONSE_LAST) || diff != 0)) ||
1384 ((wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1385 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) &&
1386 (opcode != OP(ATOMIC_ACKNOWLEDGE) || diff != 0))) {
1387 /* Retry this request. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001388 if (!(qp->r_flags & RVT_R_RDMAR_SEQ)) {
1389 qp->r_flags |= RVT_R_RDMAR_SEQ;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001390 restart_rc(qp, qp->s_last_psn + 1, 0);
1391 if (list_empty(&qp->rspwait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001392 qp->r_flags |= RVT_R_RSP_SEND;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001393 rvt_get_qp(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001394 list_add_tail(&qp->rspwait,
1395 &rcd->qp_wait_list);
1396 }
1397 }
1398 /*
1399 * No need to process the ACK/NAK since we are
1400 * restarting an earlier request.
1401 */
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001402 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001403 }
1404 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1405 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
1406 u64 *vaddr = wqe->sg_list[0].vaddr;
1407 *vaddr = val;
1408 }
1409 if (qp->s_num_rd_atomic &&
1410 (wqe->wr.opcode == IB_WR_RDMA_READ ||
1411 wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1412 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)) {
1413 qp->s_num_rd_atomic--;
1414 /* Restart sending task if fence is complete */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001415 if ((qp->s_flags & RVT_S_WAIT_FENCE) &&
Mike Marciniszyn77241052015-07-30 15:17:43 -04001416 !qp->s_num_rd_atomic) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001417 qp->s_flags &= ~(RVT_S_WAIT_FENCE |
1418 RVT_S_WAIT_ACK);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001419 hfi1_schedule_send(qp);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001420 } else if (qp->s_flags & RVT_S_WAIT_RDMAR) {
1421 qp->s_flags &= ~(RVT_S_WAIT_RDMAR |
1422 RVT_S_WAIT_ACK);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001423 hfi1_schedule_send(qp);
1424 }
1425 }
1426 wqe = do_rc_completion(qp, wqe, ibp);
1427 if (qp->s_acked == qp->s_tail)
1428 break;
1429 }
1430
1431 switch (aeth >> 29) {
1432 case 0: /* ACK */
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001433 this_cpu_inc(*ibp->rvp.rc_acks);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001434 if (qp->s_acked != qp->s_tail) {
1435 /*
1436 * We are expecting more ACKs so
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001437 * mod the retry timer.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001438 */
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001439 hfi1_mod_retry_timer(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001440 /*
1441 * We can stop re-sending the earlier packets and
1442 * continue with the next packet the receiver wants.
1443 */
1444 if (cmp_psn(qp->s_psn, psn) <= 0)
1445 reset_psn(qp, psn + 1);
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001446 } else {
1447 /* No more acks - kill all timers */
1448 hfi1_stop_rc_timers(qp);
1449 if (cmp_psn(qp->s_psn, psn) <= 0) {
1450 qp->s_state = OP(SEND_LAST);
1451 qp->s_psn = psn + 1;
1452 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001453 }
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001454 if (qp->s_flags & RVT_S_WAIT_ACK) {
1455 qp->s_flags &= ~RVT_S_WAIT_ACK;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001456 hfi1_schedule_send(qp);
1457 }
1458 hfi1_get_credit(qp, aeth);
1459 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1460 qp->s_retry = qp->s_retry_cnt;
1461 update_last_psn(qp, psn);
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001462 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001463
1464 case 1: /* RNR NAK */
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001465 ibp->rvp.n_rnr_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001466 if (qp->s_acked == qp->s_tail)
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001467 goto bail_stop;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001468 if (qp->s_flags & RVT_S_WAIT_RNR)
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001469 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001470 if (qp->s_rnr_retry == 0) {
1471 status = IB_WC_RNR_RETRY_EXC_ERR;
1472 goto class_b;
1473 }
1474 if (qp->s_rnr_retry_cnt < 7)
1475 qp->s_rnr_retry--;
1476
1477 /* The last valid PSN is the previous PSN. */
1478 update_last_psn(qp, psn - 1);
1479
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001480 ibp->rvp.n_rc_resends += delta_psn(qp->s_psn, psn);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001481
1482 reset_psn(qp, psn);
1483
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001484 qp->s_flags &= ~(RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_ACK);
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001485 hfi1_stop_rc_timers(qp);
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001486 to =
Mike Marciniszyn77241052015-07-30 15:17:43 -04001487 ib_hfi1_rnr_table[(aeth >> HFI1_AETH_CREDIT_SHIFT) &
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001488 HFI1_AETH_CREDIT_MASK];
1489 hfi1_add_rnr_timer(qp, to);
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001490 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001491
1492 case 3: /* NAK */
1493 if (qp->s_acked == qp->s_tail)
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001494 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001495 /* The last valid PSN is the previous PSN. */
1496 update_last_psn(qp, psn - 1);
1497 switch ((aeth >> HFI1_AETH_CREDIT_SHIFT) &
1498 HFI1_AETH_CREDIT_MASK) {
1499 case 0: /* PSN sequence error */
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001500 ibp->rvp.n_seq_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001501 /*
1502 * Back up to the responder's expected PSN.
1503 * Note that we might get a NAK in the middle of an
1504 * RDMA READ response which terminates the RDMA
1505 * READ.
1506 */
1507 restart_rc(qp, psn, 0);
1508 hfi1_schedule_send(qp);
1509 break;
1510
1511 case 1: /* Invalid Request */
1512 status = IB_WC_REM_INV_REQ_ERR;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001513 ibp->rvp.n_other_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001514 goto class_b;
1515
1516 case 2: /* Remote Access Error */
1517 status = IB_WC_REM_ACCESS_ERR;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001518 ibp->rvp.n_other_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001519 goto class_b;
1520
1521 case 3: /* Remote Operation Error */
1522 status = IB_WC_REM_OP_ERR;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001523 ibp->rvp.n_other_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001524class_b:
1525 if (qp->s_last == qp->s_acked) {
1526 hfi1_send_complete(qp, wqe, status);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08001527 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001528 }
1529 break;
1530
1531 default:
1532 /* Ignore other reserved NAK error codes */
1533 goto reserved;
1534 }
1535 qp->s_retry = qp->s_retry_cnt;
1536 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001537 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001538
1539 default: /* 2: reserved */
1540reserved:
1541 /* Ignore reserved NAK codes. */
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001542 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001543 }
Mike Marciniszyn87717f02016-04-12 11:28:56 -07001544 /* cannot be reached */
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001545bail_stop:
1546 hfi1_stop_rc_timers(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001547 return ret;
1548}
1549
1550/*
1551 * We have seen an out of sequence RDMA read middle or last packet.
1552 * This ACKs SENDs and RDMA writes up to the first RDMA read or atomic SWQE.
1553 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001554static void rdma_seq_err(struct rvt_qp *qp, struct hfi1_ibport *ibp, u32 psn,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001555 struct hfi1_ctxtdata *rcd)
1556{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001557 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001558
Mike Marciniszyn68e78b32016-09-06 04:37:41 -07001559 lockdep_assert_held(&qp->s_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001560 /* Remove QP from retry timer */
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001561 hfi1_stop_rc_timers(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001562
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001563 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001564
1565 while (cmp_psn(psn, wqe->lpsn) > 0) {
1566 if (wqe->wr.opcode == IB_WR_RDMA_READ ||
1567 wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1568 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
1569 break;
1570 wqe = do_rc_completion(qp, wqe, ibp);
1571 }
1572
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001573 ibp->rvp.n_rdma_seq++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001574 qp->r_flags |= RVT_R_RDMAR_SEQ;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001575 restart_rc(qp, qp->s_last_psn + 1, 0);
1576 if (list_empty(&qp->rspwait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001577 qp->r_flags |= RVT_R_RSP_SEND;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001578 rvt_get_qp(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001579 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1580 }
1581}
1582
1583/**
1584 * rc_rcv_resp - process an incoming RC response packet
1585 * @ibp: the port this packet came in on
1586 * @ohdr: the other headers for this packet
1587 * @data: the packet data
1588 * @tlen: the packet length
1589 * @qp: the QP for this packet
1590 * @opcode: the opcode for this packet
1591 * @psn: the packet sequence number for this packet
1592 * @hdrsize: the header length
1593 * @pmtu: the path MTU
1594 *
1595 * This is called from hfi1_rc_rcv() to process an incoming RC response
1596 * packet for the given QP.
1597 * Called at interrupt level.
1598 */
1599static void rc_rcv_resp(struct hfi1_ibport *ibp,
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001600 struct ib_other_headers *ohdr,
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001601 void *data, u32 tlen, struct rvt_qp *qp,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001602 u32 opcode, u32 psn, u32 hdrsize, u32 pmtu,
1603 struct hfi1_ctxtdata *rcd)
1604{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001605 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001606 enum ib_wc_status status;
1607 unsigned long flags;
1608 int diff;
1609 u32 pad;
1610 u32 aeth;
1611 u64 val;
1612
1613 spin_lock_irqsave(&qp->s_lock, flags);
1614
Sebastian Sanchez462b6b22016-07-01 16:01:06 -07001615 trace_hfi1_ack(qp, psn);
Mike Marciniszyn83525b62015-10-26 10:28:48 -04001616
Mike Marciniszyn77241052015-07-30 15:17:43 -04001617 /* Ignore invalid responses. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001618 smp_read_barrier_depends(); /* see post_one_send */
1619 if (cmp_psn(psn, ACCESS_ONCE(qp->s_next_psn)) >= 0)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001620 goto ack_done;
1621
1622 /* Ignore duplicate responses. */
1623 diff = cmp_psn(psn, qp->s_last_psn);
1624 if (unlikely(diff <= 0)) {
1625 /* Update credits for "ghost" ACKs */
1626 if (diff == 0 && opcode == OP(ACKNOWLEDGE)) {
1627 aeth = be32_to_cpu(ohdr->u.aeth);
1628 if ((aeth >> 29) == 0)
1629 hfi1_get_credit(qp, aeth);
1630 }
1631 goto ack_done;
1632 }
1633
1634 /*
1635 * Skip everything other than the PSN we expect, if we are waiting
1636 * for a reply to a restarted RDMA read or atomic op.
1637 */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001638 if (qp->r_flags & RVT_R_RDMAR_SEQ) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001639 if (cmp_psn(psn, qp->s_last_psn + 1) != 0)
1640 goto ack_done;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001641 qp->r_flags &= ~RVT_R_RDMAR_SEQ;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001642 }
1643
1644 if (unlikely(qp->s_acked == qp->s_tail))
1645 goto ack_done;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001646 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001647 status = IB_WC_SUCCESS;
1648
1649 switch (opcode) {
1650 case OP(ACKNOWLEDGE):
1651 case OP(ATOMIC_ACKNOWLEDGE):
1652 case OP(RDMA_READ_RESPONSE_FIRST):
1653 aeth = be32_to_cpu(ohdr->u.aeth);
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001654 if (opcode == OP(ATOMIC_ACKNOWLEDGE))
1655 val = ib_u64_get(&ohdr->u.at.atomic_ack_eth);
1656 else
Mike Marciniszyn77241052015-07-30 15:17:43 -04001657 val = 0;
1658 if (!do_rc_ack(qp, aeth, psn, opcode, val, rcd) ||
1659 opcode != OP(RDMA_READ_RESPONSE_FIRST))
1660 goto ack_done;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001661 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001662 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1663 goto ack_op_err;
1664 /*
1665 * If this is a response to a resent RDMA read, we
1666 * have to be careful to copy the data to the right
1667 * location.
1668 */
1669 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1670 wqe, psn, pmtu);
1671 goto read_middle;
1672
1673 case OP(RDMA_READ_RESPONSE_MIDDLE):
1674 /* no AETH, no ACK */
1675 if (unlikely(cmp_psn(psn, qp->s_last_psn + 1)))
1676 goto ack_seq_err;
1677 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1678 goto ack_op_err;
1679read_middle:
1680 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1681 goto ack_len_err;
1682 if (unlikely(pmtu >= qp->s_rdma_read_len))
1683 goto ack_len_err;
1684
1685 /*
1686 * We got a response so update the timeout.
1687 * 4.096 usec. * (1 << qp->timeout)
1688 */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001689 qp->s_flags |= RVT_S_TIMER;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001690 mod_timer(&qp->s_timer, jiffies + qp->timeout_jiffies);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001691 if (qp->s_flags & RVT_S_WAIT_ACK) {
1692 qp->s_flags &= ~RVT_S_WAIT_ACK;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001693 hfi1_schedule_send(qp);
1694 }
1695
1696 if (opcode == OP(RDMA_READ_RESPONSE_MIDDLE))
1697 qp->s_retry = qp->s_retry_cnt;
1698
1699 /*
1700 * Update the RDMA receive state but do the copy w/o
1701 * holding the locks and blocking interrupts.
1702 */
1703 qp->s_rdma_read_len -= pmtu;
1704 update_last_psn(qp, psn);
1705 spin_unlock_irqrestore(&qp->s_lock, flags);
Dean Luick7b0b01a2016-02-03 14:35:49 -08001706 hfi1_copy_sge(&qp->s_rdma_read_sge, data, pmtu, 0, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001707 goto bail;
1708
1709 case OP(RDMA_READ_RESPONSE_ONLY):
1710 aeth = be32_to_cpu(ohdr->u.aeth);
1711 if (!do_rc_ack(qp, aeth, psn, opcode, 0, rcd))
1712 goto ack_done;
1713 /* Get the number of bytes the message was padded by. */
1714 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1715 /*
1716 * Check that the data size is >= 0 && <= pmtu.
1717 * Remember to account for ICRC (4).
1718 */
1719 if (unlikely(tlen < (hdrsize + pad + 4)))
1720 goto ack_len_err;
1721 /*
1722 * If this is a response to a resent RDMA read, we
1723 * have to be careful to copy the data to the right
1724 * location.
1725 */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001726 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001727 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1728 wqe, psn, pmtu);
1729 goto read_last;
1730
1731 case OP(RDMA_READ_RESPONSE_LAST):
1732 /* ACKs READ req. */
1733 if (unlikely(cmp_psn(psn, qp->s_last_psn + 1)))
1734 goto ack_seq_err;
1735 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1736 goto ack_op_err;
1737 /* Get the number of bytes the message was padded by. */
1738 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1739 /*
1740 * Check that the data size is >= 1 && <= pmtu.
1741 * Remember to account for ICRC (4).
1742 */
1743 if (unlikely(tlen <= (hdrsize + pad + 4)))
1744 goto ack_len_err;
1745read_last:
1746 tlen -= hdrsize + pad + 4;
1747 if (unlikely(tlen != qp->s_rdma_read_len))
1748 goto ack_len_err;
1749 aeth = be32_to_cpu(ohdr->u.aeth);
Dean Luick7b0b01a2016-02-03 14:35:49 -08001750 hfi1_copy_sge(&qp->s_rdma_read_sge, data, tlen, 0, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001751 WARN_ON(qp->s_rdma_read_sge.num_sge);
Jubin John50e5dcb2016-02-14 20:19:41 -08001752 (void)do_rc_ack(qp, aeth, psn,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001753 OP(RDMA_READ_RESPONSE_LAST), 0, rcd);
1754 goto ack_done;
1755 }
1756
1757ack_op_err:
1758 status = IB_WC_LOC_QP_OP_ERR;
1759 goto ack_err;
1760
1761ack_seq_err:
1762 rdma_seq_err(qp, ibp, psn, rcd);
1763 goto ack_done;
1764
1765ack_len_err:
1766 status = IB_WC_LOC_LEN_ERR;
1767ack_err:
1768 if (qp->s_last == qp->s_acked) {
1769 hfi1_send_complete(qp, wqe, status);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08001770 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001771 }
1772ack_done:
1773 spin_unlock_irqrestore(&qp->s_lock, flags);
1774bail:
1775 return;
1776}
1777
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05001778static inline void rc_defered_ack(struct hfi1_ctxtdata *rcd,
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001779 struct rvt_qp *qp)
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05001780{
1781 if (list_empty(&qp->rspwait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001782 qp->r_flags |= RVT_R_RSP_NAK;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001783 rvt_get_qp(qp);
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05001784 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1785 }
1786}
1787
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001788static inline void rc_cancel_ack(struct rvt_qp *qp)
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05001789{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08001790 struct hfi1_qp_priv *priv = qp->priv;
1791
1792 priv->r_adefered = 0;
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05001793 if (list_empty(&qp->rspwait))
1794 return;
1795 list_del_init(&qp->rspwait);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001796 qp->r_flags &= ~RVT_R_RSP_NAK;
Mike Marciniszyn4d6f85c2016-09-06 04:34:35 -07001797 rvt_put_qp(qp);
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05001798}
1799
Mike Marciniszyn77241052015-07-30 15:17:43 -04001800/**
1801 * rc_rcv_error - process an incoming duplicate or error RC packet
1802 * @ohdr: the other headers for this packet
1803 * @data: the packet data
1804 * @qp: the QP for this packet
1805 * @opcode: the opcode for this packet
1806 * @psn: the packet sequence number for this packet
1807 * @diff: the difference between the PSN and the expected PSN
1808 *
1809 * This is called from hfi1_rc_rcv() to process an unexpected
1810 * incoming RC packet for the given QP.
1811 * Called at interrupt level.
1812 * Return 1 if no more processing is needed; otherwise return 0 to
1813 * schedule a response to be sent.
1814 */
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001815static noinline int rc_rcv_error(struct ib_other_headers *ohdr, void *data,
Jubin John17fb4f22016-02-14 20:21:52 -08001816 struct rvt_qp *qp, u32 opcode, u32 psn,
1817 int diff, struct hfi1_ctxtdata *rcd)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001818{
1819 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001820 struct rvt_ack_entry *e;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001821 unsigned long flags;
1822 u8 i, prev;
1823 int old_req;
1824
Sebastian Sanchez462b6b22016-07-01 16:01:06 -07001825 trace_hfi1_rcv_error(qp, psn);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001826 if (diff > 0) {
1827 /*
1828 * Packet sequence error.
1829 * A NAK will ACK earlier sends and RDMA writes.
1830 * Don't queue the NAK if we already sent one.
1831 */
1832 if (!qp->r_nak_state) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001833 ibp->rvp.n_rc_seqnak++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001834 qp->r_nak_state = IB_NAK_PSN_ERROR;
1835 /* Use the expected PSN. */
1836 qp->r_ack_psn = qp->r_psn;
1837 /*
1838 * Wait to send the sequence NAK until all packets
1839 * in the receive queue have been processed.
1840 * Otherwise, we end up propagating congestion.
1841 */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05001842 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001843 }
1844 goto done;
1845 }
1846
1847 /*
1848 * Handle a duplicate request. Don't re-execute SEND, RDMA
1849 * write or atomic op. Don't NAK errors, just silently drop
1850 * the duplicate request. Note that r_sge, r_len, and
1851 * r_rcv_len may be in use so don't modify them.
1852 *
1853 * We are supposed to ACK the earliest duplicate PSN but we
1854 * can coalesce an outstanding duplicate ACK. We have to
1855 * send the earliest so that RDMA reads can be restarted at
1856 * the requester's expected PSN.
1857 *
1858 * First, find where this duplicate PSN falls within the
1859 * ACKs previously sent.
1860 * old_req is true if there is an older response that is scheduled
1861 * to be sent before sending this one.
1862 */
1863 e = NULL;
1864 old_req = 1;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001865 ibp->rvp.n_rc_dupreq++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001866
1867 spin_lock_irqsave(&qp->s_lock, flags);
1868
1869 for (i = qp->r_head_ack_queue; ; i = prev) {
1870 if (i == qp->s_tail_ack_queue)
1871 old_req = 0;
1872 if (i)
1873 prev = i - 1;
1874 else
1875 prev = HFI1_MAX_RDMA_ATOMIC;
1876 if (prev == qp->r_head_ack_queue) {
1877 e = NULL;
1878 break;
1879 }
1880 e = &qp->s_ack_queue[prev];
1881 if (!e->opcode) {
1882 e = NULL;
1883 break;
1884 }
1885 if (cmp_psn(psn, e->psn) >= 0) {
1886 if (prev == qp->s_tail_ack_queue &&
1887 cmp_psn(psn, e->lpsn) <= 0)
1888 old_req = 0;
1889 break;
1890 }
1891 }
1892 switch (opcode) {
1893 case OP(RDMA_READ_REQUEST): {
1894 struct ib_reth *reth;
1895 u32 offset;
1896 u32 len;
1897
1898 /*
1899 * If we didn't find the RDMA read request in the ack queue,
1900 * we can ignore this request.
1901 */
1902 if (!e || e->opcode != OP(RDMA_READ_REQUEST))
1903 goto unlock_done;
1904 /* RETH comes after BTH */
1905 reth = &ohdr->u.rc.reth;
1906 /*
1907 * Address range must be a subset of the original
1908 * request and start on pmtu boundaries.
1909 * We reuse the old ack_queue slot since the requester
1910 * should not back up and request an earlier PSN for the
1911 * same request.
1912 */
1913 offset = delta_psn(psn, e->psn) * qp->pmtu;
1914 len = be32_to_cpu(reth->length);
1915 if (unlikely(offset + len != e->rdma_sge.sge_length))
1916 goto unlock_done;
1917 if (e->rdma_sge.mr) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001918 rvt_put_mr(e->rdma_sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001919 e->rdma_sge.mr = NULL;
1920 }
1921 if (len != 0) {
1922 u32 rkey = be32_to_cpu(reth->rkey);
Mike Marciniszyn261a4352016-09-06 04:35:05 -07001923 u64 vaddr = get_ib_reth_vaddr(reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001924 int ok;
1925
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001926 ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr, rkey,
1927 IB_ACCESS_REMOTE_READ);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001928 if (unlikely(!ok))
1929 goto unlock_done;
1930 } else {
1931 e->rdma_sge.vaddr = NULL;
1932 e->rdma_sge.length = 0;
1933 e->rdma_sge.sge_length = 0;
1934 }
1935 e->psn = psn;
1936 if (old_req)
1937 goto unlock_done;
1938 qp->s_tail_ack_queue = prev;
1939 break;
1940 }
1941
1942 case OP(COMPARE_SWAP):
1943 case OP(FETCH_ADD): {
1944 /*
1945 * If we didn't find the atomic request in the ack queue
1946 * or the send tasklet is already backed up to send an
1947 * earlier entry, we can ignore this request.
1948 */
Jubin John50e5dcb2016-02-14 20:19:41 -08001949 if (!e || e->opcode != (u8)opcode || old_req)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001950 goto unlock_done;
1951 qp->s_tail_ack_queue = prev;
1952 break;
1953 }
1954
1955 default:
1956 /*
1957 * Ignore this operation if it doesn't request an ACK
1958 * or an earlier RDMA read or atomic is going to be resent.
1959 */
1960 if (!(psn & IB_BTH_REQ_ACK) || old_req)
1961 goto unlock_done;
1962 /*
1963 * Resend the most recent ACK if this request is
1964 * after all the previous RDMA reads and atomics.
1965 */
1966 if (i == qp->r_head_ack_queue) {
1967 spin_unlock_irqrestore(&qp->s_lock, flags);
1968 qp->r_nak_state = 0;
1969 qp->r_ack_psn = qp->r_psn - 1;
1970 goto send_ack;
1971 }
1972
1973 /*
1974 * Resend the RDMA read or atomic op which
1975 * ACKs this duplicate request.
1976 */
1977 qp->s_tail_ack_queue = i;
1978 break;
1979 }
1980 qp->s_ack_state = OP(ACKNOWLEDGE);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001981 qp->s_flags |= RVT_S_RESP_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001982 qp->r_nak_state = 0;
1983 hfi1_schedule_send(qp);
1984
1985unlock_done:
1986 spin_unlock_irqrestore(&qp->s_lock, flags);
1987done:
1988 return 1;
1989
1990send_ack:
1991 return 0;
1992}
1993
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001994void hfi1_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001995{
1996 unsigned long flags;
1997 int lastwqe;
1998
1999 spin_lock_irqsave(&qp->s_lock, flags);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002000 lastwqe = rvt_error_qp(qp, err);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002001 spin_unlock_irqrestore(&qp->s_lock, flags);
2002
2003 if (lastwqe) {
2004 struct ib_event ev;
2005
2006 ev.device = qp->ibqp.device;
2007 ev.element.qp = &qp->ibqp;
2008 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
2009 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
2010 }
2011}
2012
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002013static inline void update_ack_queue(struct rvt_qp *qp, unsigned n)
Mike Marciniszyn77241052015-07-30 15:17:43 -04002014{
2015 unsigned next;
2016
2017 next = n + 1;
2018 if (next > HFI1_MAX_RDMA_ATOMIC)
2019 next = 0;
2020 qp->s_tail_ack_queue = next;
2021 qp->s_ack_state = OP(ACKNOWLEDGE);
2022}
2023
2024static void log_cca_event(struct hfi1_pportdata *ppd, u8 sl, u32 rlid,
2025 u32 lqpn, u32 rqpn, u8 svc_type)
2026{
2027 struct opa_hfi1_cong_log_event_internal *cc_event;
Dean Luickb77d7132015-10-26 10:28:43 -04002028 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002029
2030 if (sl >= OPA_MAX_SLS)
2031 return;
2032
Dean Luickb77d7132015-10-26 10:28:43 -04002033 spin_lock_irqsave(&ppd->cc_log_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002034
Jubin John8638b772016-02-14 20:19:24 -08002035 ppd->threshold_cong_event_map[sl / 8] |= 1 << (sl % 8);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002036 ppd->threshold_event_counter++;
2037
2038 cc_event = &ppd->cc_events[ppd->cc_log_idx++];
2039 if (ppd->cc_log_idx == OPA_CONG_LOG_ELEMS)
2040 ppd->cc_log_idx = 0;
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002041 cc_event->lqpn = lqpn & RVT_QPN_MASK;
2042 cc_event->rqpn = rqpn & RVT_QPN_MASK;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002043 cc_event->sl = sl;
2044 cc_event->svc_type = svc_type;
2045 cc_event->rlid = rlid;
2046 /* keep timestamp in units of 1.024 usec */
2047 cc_event->timestamp = ktime_to_ns(ktime_get()) / 1024;
2048
Dean Luickb77d7132015-10-26 10:28:43 -04002049 spin_unlock_irqrestore(&ppd->cc_log_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002050}
2051
2052void process_becn(struct hfi1_pportdata *ppd, u8 sl, u16 rlid, u32 lqpn,
2053 u32 rqpn, u8 svc_type)
2054{
2055 struct cca_timer *cca_timer;
2056 u16 ccti, ccti_incr, ccti_timer, ccti_limit;
2057 u8 trigger_threshold;
2058 struct cc_state *cc_state;
Dean Luickb77d7132015-10-26 10:28:43 -04002059 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002060
2061 if (sl >= OPA_MAX_SLS)
2062 return;
2063
Mike Marciniszyn77241052015-07-30 15:17:43 -04002064 cc_state = get_cc_state(ppd);
2065
Jubin Johnd125a6c2016-02-14 20:19:49 -08002066 if (!cc_state)
Mike Marciniszyn77241052015-07-30 15:17:43 -04002067 return;
2068
2069 /*
2070 * 1) increase CCTI (for this SL)
2071 * 2) select IPG (i.e., call set_link_ipg())
2072 * 3) start timer
2073 */
2074 ccti_limit = cc_state->cct.ccti_limit;
2075 ccti_incr = cc_state->cong_setting.entries[sl].ccti_increase;
2076 ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer;
2077 trigger_threshold =
2078 cc_state->cong_setting.entries[sl].trigger_threshold;
2079
Dean Luickb77d7132015-10-26 10:28:43 -04002080 spin_lock_irqsave(&ppd->cca_timer_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002081
Jubin Johnd35cf7442016-04-14 08:31:53 -07002082 cca_timer = &ppd->cca_timer[sl];
Mike Marciniszyn77241052015-07-30 15:17:43 -04002083 if (cca_timer->ccti < ccti_limit) {
2084 if (cca_timer->ccti + ccti_incr <= ccti_limit)
2085 cca_timer->ccti += ccti_incr;
2086 else
2087 cca_timer->ccti = ccti_limit;
2088 set_link_ipg(ppd);
2089 }
2090
Mike Marciniszyn77241052015-07-30 15:17:43 -04002091 ccti = cca_timer->ccti;
2092
2093 if (!hrtimer_active(&cca_timer->hrtimer)) {
2094 /* ccti_timer is in units of 1.024 usec */
2095 unsigned long nsec = 1024 * ccti_timer;
2096
2097 hrtimer_start(&cca_timer->hrtimer, ns_to_ktime(nsec),
2098 HRTIMER_MODE_REL);
2099 }
2100
Jubin Johnd35cf7442016-04-14 08:31:53 -07002101 spin_unlock_irqrestore(&ppd->cca_timer_lock, flags);
2102
Mike Marciniszyn77241052015-07-30 15:17:43 -04002103 if ((trigger_threshold != 0) && (ccti >= trigger_threshold))
2104 log_cca_event(ppd, sl, rlid, lqpn, rqpn, svc_type);
2105}
2106
2107/**
2108 * hfi1_rc_rcv - process an incoming RC packet
2109 * @rcd: the context pointer
2110 * @hdr: the header of this packet
2111 * @rcv_flags: flags relevant to rcv processing
2112 * @data: the packet data
2113 * @tlen: the packet length
2114 * @qp: the QP for this packet
2115 *
2116 * This is called from qp_rcv() to process an incoming RC packet
2117 * for the given QP.
Dean Luickb77d7132015-10-26 10:28:43 -04002118 * May be called at interrupt level.
Mike Marciniszyn77241052015-07-30 15:17:43 -04002119 */
2120void hfi1_rc_rcv(struct hfi1_packet *packet)
2121{
2122 struct hfi1_ctxtdata *rcd = packet->rcd;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002123 struct ib_header *hdr = packet->hdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002124 u32 rcv_flags = packet->rcv_flags;
2125 void *data = packet->ebuf;
2126 u32 tlen = packet->tlen;
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002127 struct rvt_qp *qp = packet->qp;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002128 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002129 struct ib_other_headers *ohdr = packet->ohdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002130 u32 bth0, opcode;
2131 u32 hdrsize = packet->hlen;
2132 u32 psn;
2133 u32 pad;
2134 struct ib_wc wc;
2135 u32 pmtu = qp->pmtu;
2136 int diff;
2137 struct ib_reth *reth;
2138 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002139 int ret, is_fecn = 0;
Dean Luick7b0b01a2016-02-03 14:35:49 -08002140 int copy_last = 0;
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002141 u32 rkey;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002142
Mike Marciniszyn68e78b32016-09-06 04:37:41 -07002143 lockdep_assert_held(&qp->r_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002144 bth0 = be32_to_cpu(ohdr->bth[0]);
2145 if (hfi1_ruc_check_hdr(ibp, hdr, rcv_flags & HFI1_HAS_GRH, qp, bth0))
2146 return;
2147
Mitko Haralanov5fd2b562016-07-25 13:38:07 -07002148 is_fecn = process_ecn(qp, packet, false);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002149
2150 psn = be32_to_cpu(ohdr->bth[2]);
Arthur Kepner977940b2015-11-04 21:10:10 -05002151 opcode = (bth0 >> 24) & 0xff;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002152
2153 /*
2154 * Process responses (ACKs) before anything else. Note that the
2155 * packet sequence number will be for something in the send work
2156 * queue rather than the expected receive packet sequence number.
2157 * In other words, this QP is the requester.
2158 */
2159 if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
2160 opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
2161 rc_rcv_resp(ibp, ohdr, data, tlen, qp, opcode, psn,
2162 hdrsize, pmtu, rcd);
2163 if (is_fecn)
2164 goto send_ack;
2165 return;
2166 }
2167
2168 /* Compute 24 bits worth of difference. */
2169 diff = delta_psn(psn, qp->r_psn);
2170 if (unlikely(diff)) {
2171 if (rc_rcv_error(ohdr, data, qp, opcode, psn, diff, rcd))
2172 return;
2173 goto send_ack;
2174 }
2175
2176 /* Check for opcode sequence errors. */
2177 switch (qp->r_state) {
2178 case OP(SEND_FIRST):
2179 case OP(SEND_MIDDLE):
2180 if (opcode == OP(SEND_MIDDLE) ||
2181 opcode == OP(SEND_LAST) ||
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002182 opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
2183 opcode == OP(SEND_LAST_WITH_INVALIDATE))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002184 break;
2185 goto nack_inv;
2186
2187 case OP(RDMA_WRITE_FIRST):
2188 case OP(RDMA_WRITE_MIDDLE):
2189 if (opcode == OP(RDMA_WRITE_MIDDLE) ||
2190 opcode == OP(RDMA_WRITE_LAST) ||
2191 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
2192 break;
2193 goto nack_inv;
2194
2195 default:
2196 if (opcode == OP(SEND_MIDDLE) ||
2197 opcode == OP(SEND_LAST) ||
2198 opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002199 opcode == OP(SEND_LAST_WITH_INVALIDATE) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04002200 opcode == OP(RDMA_WRITE_MIDDLE) ||
2201 opcode == OP(RDMA_WRITE_LAST) ||
2202 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
2203 goto nack_inv;
2204 /*
2205 * Note that it is up to the requester to not send a new
2206 * RDMA read or atomic operation before receiving an ACK
2207 * for the previous operation.
2208 */
2209 break;
2210 }
2211
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08002212 if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002213 qp_comm_est(qp);
2214
2215 /* OK, process the packet. */
2216 switch (opcode) {
2217 case OP(SEND_FIRST):
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002218 ret = hfi1_rvt_get_rwqe(qp, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002219 if (ret < 0)
2220 goto nack_op_err;
2221 if (!ret)
2222 goto rnr_nak;
2223 qp->r_rcv_len = 0;
2224 /* FALLTHROUGH */
2225 case OP(SEND_MIDDLE):
2226 case OP(RDMA_WRITE_MIDDLE):
2227send_middle:
2228 /* Check for invalid length PMTU or posted rwqe len. */
2229 if (unlikely(tlen != (hdrsize + pmtu + 4)))
2230 goto nack_inv;
2231 qp->r_rcv_len += pmtu;
2232 if (unlikely(qp->r_rcv_len > qp->r_len))
2233 goto nack_inv;
Dean Luick7b0b01a2016-02-03 14:35:49 -08002234 hfi1_copy_sge(&qp->r_sge, data, pmtu, 1, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002235 break;
2236
2237 case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
2238 /* consume RWQE */
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002239 ret = hfi1_rvt_get_rwqe(qp, 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002240 if (ret < 0)
2241 goto nack_op_err;
2242 if (!ret)
2243 goto rnr_nak;
2244 goto send_last_imm;
2245
2246 case OP(SEND_ONLY):
2247 case OP(SEND_ONLY_WITH_IMMEDIATE):
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002248 case OP(SEND_ONLY_WITH_INVALIDATE):
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002249 ret = hfi1_rvt_get_rwqe(qp, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002250 if (ret < 0)
2251 goto nack_op_err;
2252 if (!ret)
2253 goto rnr_nak;
2254 qp->r_rcv_len = 0;
2255 if (opcode == OP(SEND_ONLY))
2256 goto no_immediate_data;
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002257 if (opcode == OP(SEND_ONLY_WITH_INVALIDATE))
2258 goto send_last_inv;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002259 /* FALLTHROUGH for SEND_ONLY_WITH_IMMEDIATE */
2260 case OP(SEND_LAST_WITH_IMMEDIATE):
2261send_last_imm:
2262 wc.ex.imm_data = ohdr->u.imm_data;
2263 wc.wc_flags = IB_WC_WITH_IMM;
2264 goto send_last;
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002265 case OP(SEND_LAST_WITH_INVALIDATE):
2266send_last_inv:
2267 rkey = be32_to_cpu(ohdr->u.ieth);
2268 if (rvt_invalidate_rkey(qp, rkey))
2269 goto no_immediate_data;
2270 wc.ex.invalidate_rkey = rkey;
2271 wc.wc_flags = IB_WC_WITH_INVALIDATE;
2272 goto send_last;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002273 case OP(RDMA_WRITE_LAST):
Dean Luick7b0b01a2016-02-03 14:35:49 -08002274 copy_last = ibpd_to_rvtpd(qp->ibqp.pd)->user;
2275 /* fall through */
2276 case OP(SEND_LAST):
Mike Marciniszyn77241052015-07-30 15:17:43 -04002277no_immediate_data:
2278 wc.wc_flags = 0;
2279 wc.ex.imm_data = 0;
2280send_last:
2281 /* Get the number of bytes the message was padded by. */
2282 pad = (bth0 >> 20) & 3;
2283 /* Check for invalid length. */
2284 /* LAST len should be >= 1 */
2285 if (unlikely(tlen < (hdrsize + pad + 4)))
2286 goto nack_inv;
2287 /* Don't count the CRC. */
2288 tlen -= (hdrsize + pad + 4);
2289 wc.byte_len = tlen + qp->r_rcv_len;
2290 if (unlikely(wc.byte_len > qp->r_len))
2291 goto nack_inv;
Dean Luick7b0b01a2016-02-03 14:35:49 -08002292 hfi1_copy_sge(&qp->r_sge, data, tlen, 1, copy_last);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002293 rvt_put_ss(&qp->r_sge);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002294 qp->r_msn++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08002295 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002296 break;
2297 wc.wr_id = qp->r_wr_id;
2298 wc.status = IB_WC_SUCCESS;
2299 if (opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE) ||
2300 opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
2301 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
2302 else
2303 wc.opcode = IB_WC_RECV;
2304 wc.qp = &qp->ibqp;
2305 wc.src_qp = qp->remote_qpn;
2306 wc.slid = qp->remote_ah_attr.dlid;
2307 /*
2308 * It seems that IB mandates the presence of an SL in a
2309 * work completion only for the UD transport (see section
2310 * 11.4.2 of IBTA Vol. 1).
2311 *
2312 * However, the way the SL is chosen below is consistent
2313 * with the way that IB/qib works and is trying avoid
2314 * introducing incompatibilities.
2315 *
2316 * See also OPA Vol. 1, section 9.7.6, and table 9-17.
2317 */
2318 wc.sl = qp->remote_ah_attr.sl;
2319 /* zero fields that are N/A */
2320 wc.vendor_err = 0;
2321 wc.pkey_index = 0;
2322 wc.dlid_path_bits = 0;
2323 wc.port_num = 0;
2324 /* Signal completion event if the solicited bit is set. */
Dennis Dalessandroabd712d2016-01-19 14:43:22 -08002325 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
2326 (bth0 & IB_BTH_SOLICITED) != 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002327 break;
2328
Mike Marciniszyn77241052015-07-30 15:17:43 -04002329 case OP(RDMA_WRITE_ONLY):
Dean Luick7b0b01a2016-02-03 14:35:49 -08002330 copy_last = 1;
2331 /* fall through */
2332 case OP(RDMA_WRITE_FIRST):
Mike Marciniszyn77241052015-07-30 15:17:43 -04002333 case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
2334 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
2335 goto nack_inv;
2336 /* consume RWQE */
2337 reth = &ohdr->u.rc.reth;
2338 qp->r_len = be32_to_cpu(reth->length);
2339 qp->r_rcv_len = 0;
2340 qp->r_sge.sg_list = NULL;
2341 if (qp->r_len != 0) {
2342 u32 rkey = be32_to_cpu(reth->rkey);
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002343 u64 vaddr = get_ib_reth_vaddr(reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002344 int ok;
2345
2346 /* Check rkey & NAK */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002347 ok = rvt_rkey_ok(qp, &qp->r_sge.sge, qp->r_len, vaddr,
2348 rkey, IB_ACCESS_REMOTE_WRITE);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002349 if (unlikely(!ok))
2350 goto nack_acc;
2351 qp->r_sge.num_sge = 1;
2352 } else {
2353 qp->r_sge.num_sge = 0;
2354 qp->r_sge.sge.mr = NULL;
2355 qp->r_sge.sge.vaddr = NULL;
2356 qp->r_sge.sge.length = 0;
2357 qp->r_sge.sge.sge_length = 0;
2358 }
2359 if (opcode == OP(RDMA_WRITE_FIRST))
2360 goto send_middle;
2361 else if (opcode == OP(RDMA_WRITE_ONLY))
2362 goto no_immediate_data;
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002363 ret = hfi1_rvt_get_rwqe(qp, 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002364 if (ret < 0)
2365 goto nack_op_err;
2366 if (!ret)
2367 goto rnr_nak;
2368 wc.ex.imm_data = ohdr->u.rc.imm_data;
2369 wc.wc_flags = IB_WC_WITH_IMM;
2370 goto send_last;
2371
2372 case OP(RDMA_READ_REQUEST): {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002373 struct rvt_ack_entry *e;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002374 u32 len;
2375 u8 next;
2376
2377 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
2378 goto nack_inv;
2379 next = qp->r_head_ack_queue + 1;
2380 /* s_ack_queue is size HFI1_MAX_RDMA_ATOMIC+1 so use > not >= */
2381 if (next > HFI1_MAX_RDMA_ATOMIC)
2382 next = 0;
2383 spin_lock_irqsave(&qp->s_lock, flags);
2384 if (unlikely(next == qp->s_tail_ack_queue)) {
2385 if (!qp->s_ack_queue[next].sent)
2386 goto nack_inv_unlck;
2387 update_ack_queue(qp, next);
2388 }
2389 e = &qp->s_ack_queue[qp->r_head_ack_queue];
2390 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002391 rvt_put_mr(e->rdma_sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002392 e->rdma_sge.mr = NULL;
2393 }
2394 reth = &ohdr->u.rc.reth;
2395 len = be32_to_cpu(reth->length);
2396 if (len) {
2397 u32 rkey = be32_to_cpu(reth->rkey);
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002398 u64 vaddr = get_ib_reth_vaddr(reth);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002399 int ok;
2400
2401 /* Check rkey & NAK */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002402 ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr,
2403 rkey, IB_ACCESS_REMOTE_READ);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002404 if (unlikely(!ok))
2405 goto nack_acc_unlck;
2406 /*
2407 * Update the next expected PSN. We add 1 later
2408 * below, so only add the remainder here.
2409 */
2410 if (len > pmtu)
2411 qp->r_psn += (len - 1) / pmtu;
2412 } else {
2413 e->rdma_sge.mr = NULL;
2414 e->rdma_sge.vaddr = NULL;
2415 e->rdma_sge.length = 0;
2416 e->rdma_sge.sge_length = 0;
2417 }
2418 e->opcode = opcode;
2419 e->sent = 0;
2420 e->psn = psn;
2421 e->lpsn = qp->r_psn;
2422 /*
2423 * We need to increment the MSN here instead of when we
2424 * finish sending the result since a duplicate request would
2425 * increment it more than once.
2426 */
2427 qp->r_msn++;
2428 qp->r_psn++;
2429 qp->r_state = opcode;
2430 qp->r_nak_state = 0;
2431 qp->r_head_ack_queue = next;
2432
2433 /* Schedule the send tasklet. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08002434 qp->s_flags |= RVT_S_RESP_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002435 hfi1_schedule_send(qp);
2436
2437 spin_unlock_irqrestore(&qp->s_lock, flags);
2438 if (is_fecn)
2439 goto send_ack;
2440 return;
2441 }
2442
2443 case OP(COMPARE_SWAP):
2444 case OP(FETCH_ADD): {
2445 struct ib_atomic_eth *ateth;
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002446 struct rvt_ack_entry *e;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002447 u64 vaddr;
2448 atomic64_t *maddr;
2449 u64 sdata;
2450 u32 rkey;
2451 u8 next;
2452
2453 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
2454 goto nack_inv;
2455 next = qp->r_head_ack_queue + 1;
2456 if (next > HFI1_MAX_RDMA_ATOMIC)
2457 next = 0;
2458 spin_lock_irqsave(&qp->s_lock, flags);
2459 if (unlikely(next == qp->s_tail_ack_queue)) {
2460 if (!qp->s_ack_queue[next].sent)
2461 goto nack_inv_unlck;
2462 update_ack_queue(qp, next);
2463 }
2464 e = &qp->s_ack_queue[qp->r_head_ack_queue];
2465 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002466 rvt_put_mr(e->rdma_sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002467 e->rdma_sge.mr = NULL;
2468 }
2469 ateth = &ohdr->u.atomic_eth;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002470 vaddr = get_ib_ateth_vaddr(ateth);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002471 if (unlikely(vaddr & (sizeof(u64) - 1)))
2472 goto nack_inv_unlck;
2473 rkey = be32_to_cpu(ateth->rkey);
2474 /* Check rkey & NAK */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002475 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
2476 vaddr, rkey,
2477 IB_ACCESS_REMOTE_ATOMIC)))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002478 goto nack_acc_unlck;
2479 /* Perform atomic OP and save result. */
Jubin John50e5dcb2016-02-14 20:19:41 -08002480 maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002481 sdata = get_ib_ateth_swap(ateth);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002482 e->atomic_data = (opcode == OP(FETCH_ADD)) ?
Jubin John50e5dcb2016-02-14 20:19:41 -08002483 (u64)atomic64_add_return(sdata, maddr) - sdata :
2484 (u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002485 get_ib_ateth_compare(ateth),
Mike Marciniszyn77241052015-07-30 15:17:43 -04002486 sdata);
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002487 rvt_put_mr(qp->r_sge.sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002488 qp->r_sge.num_sge = 0;
2489 e->opcode = opcode;
2490 e->sent = 0;
2491 e->psn = psn;
2492 e->lpsn = psn;
2493 qp->r_msn++;
2494 qp->r_psn++;
2495 qp->r_state = opcode;
2496 qp->r_nak_state = 0;
2497 qp->r_head_ack_queue = next;
2498
2499 /* Schedule the send tasklet. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08002500 qp->s_flags |= RVT_S_RESP_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002501 hfi1_schedule_send(qp);
2502
2503 spin_unlock_irqrestore(&qp->s_lock, flags);
2504 if (is_fecn)
2505 goto send_ack;
2506 return;
2507 }
2508
2509 default:
2510 /* NAK unknown opcodes. */
2511 goto nack_inv;
2512 }
2513 qp->r_psn++;
2514 qp->r_state = opcode;
2515 qp->r_ack_psn = psn;
2516 qp->r_nak_state = 0;
2517 /* Send an ACK if requested or required. */
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05002518 if (psn & IB_BTH_REQ_ACK) {
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08002519 struct hfi1_qp_priv *priv = qp->priv;
2520
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05002521 if (packet->numpkt == 0) {
2522 rc_cancel_ack(qp);
2523 goto send_ack;
2524 }
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08002525 if (priv->r_adefered >= HFI1_PSN_CREDIT) {
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05002526 rc_cancel_ack(qp);
2527 goto send_ack;
2528 }
2529 if (unlikely(is_fecn)) {
2530 rc_cancel_ack(qp);
2531 goto send_ack;
2532 }
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08002533 priv->r_adefered++;
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05002534 rc_defered_ack(rcd, qp);
2535 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04002536 return;
2537
2538rnr_nak:
Harish Chegondibf640092016-03-05 08:49:29 -08002539 qp->r_nak_state = qp->r_min_rnr_timer | IB_RNR_NAK;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002540 qp->r_ack_psn = qp->r_psn;
2541 /* Queue RNR NAK for later */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05002542 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002543 return;
2544
2545nack_op_err:
2546 hfi1_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
2547 qp->r_nak_state = IB_NAK_REMOTE_OPERATIONAL_ERROR;
2548 qp->r_ack_psn = qp->r_psn;
2549 /* Queue NAK for later */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05002550 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002551 return;
2552
2553nack_inv_unlck:
2554 spin_unlock_irqrestore(&qp->s_lock, flags);
2555nack_inv:
2556 hfi1_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
2557 qp->r_nak_state = IB_NAK_INVALID_REQUEST;
2558 qp->r_ack_psn = qp->r_psn;
2559 /* Queue NAK for later */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05002560 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002561 return;
2562
2563nack_acc_unlck:
2564 spin_unlock_irqrestore(&qp->s_lock, flags);
2565nack_acc:
2566 hfi1_rc_error(qp, IB_WC_LOC_PROT_ERR);
2567 qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
2568 qp->r_ack_psn = qp->r_psn;
2569send_ack:
2570 hfi1_send_rc_ack(rcd, qp, is_fecn);
2571}
2572
2573void hfi1_rc_hdrerr(
2574 struct hfi1_ctxtdata *rcd,
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002575 struct ib_header *hdr,
Mike Marciniszyn77241052015-07-30 15:17:43 -04002576 u32 rcv_flags,
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002577 struct rvt_qp *qp)
Mike Marciniszyn77241052015-07-30 15:17:43 -04002578{
2579 int has_grh = rcv_flags & HFI1_HAS_GRH;
Mike Marciniszyn261a4352016-09-06 04:35:05 -07002580 struct ib_other_headers *ohdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002581 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
2582 int diff;
Nicolas Iooss49c32032015-09-20 16:07:15 +02002583 u32 opcode;
Arthur Kepner977940b2015-11-04 21:10:10 -05002584 u32 psn, bth0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002585
2586 /* Check for GRH */
2587 ohdr = &hdr->u.oth;
2588 if (has_grh)
2589 ohdr = &hdr->u.l.oth;
2590
Arthur Kepner977940b2015-11-04 21:10:10 -05002591 bth0 = be32_to_cpu(ohdr->bth[0]);
2592 if (hfi1_ruc_check_hdr(ibp, hdr, has_grh, qp, bth0))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002593 return;
2594
2595 psn = be32_to_cpu(ohdr->bth[2]);
Arthur Kepner977940b2015-11-04 21:10:10 -05002596 opcode = (bth0 >> 24) & 0xff;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002597
2598 /* Only deal with RDMA Writes for now */
2599 if (opcode < IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST) {
2600 diff = delta_psn(psn, qp->r_psn);
2601 if (!qp->r_nak_state && diff >= 0) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08002602 ibp->rvp.n_rc_seqnak++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002603 qp->r_nak_state = IB_NAK_PSN_ERROR;
2604 /* Use the expected PSN. */
2605 qp->r_ack_psn = qp->r_psn;
2606 /*
2607 * Wait to send the sequence
2608 * NAK until all packets
2609 * in the receive queue have
2610 * been processed.
2611 * Otherwise, we end up
2612 * propagating congestion.
2613 */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05002614 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002615 } /* Out of sequence NAK */
2616 } /* QP Request NAKs */
2617}