blob: 0bc43b67d0b8a1641b00d27af7b1fd0f5033e49f [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
Jubin John05d6ac12016-02-14 20:22:17 -08002 * Copyright(c) 2015, 2016 Intel Corporation.
Mike Marciniszyn77241052015-07-30 15:17:43 -04003 *
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
Mike Marciniszyn77241052015-07-30 15:17:43 -04009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
Mike Marciniszyn77241052015-07-30 15:17:43 -040020 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
47
48#include <linux/io.h>
Dennis Dalessandroec4274f2016-01-19 14:43:44 -080049#include <rdma/rdma_vt.h>
50#include <rdma/rdmavt_qp.h>
Mike Marciniszyn77241052015-07-30 15:17:43 -040051
52#include "hfi.h"
53#include "qp.h"
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -080054#include "verbs_txreq.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040055#include "trace.h"
56
57/* cut down ridiculously long IB macro names */
58#define OP(x) IB_OPCODE_RC_##x
59
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080060/**
61 * hfi1_add_retry_timer - add/start a retry timer
62 * @qp - the QP
63 *
64 * add a retry timer on the QP
65 */
66static inline void hfi1_add_retry_timer(struct rvt_qp *qp)
67{
Vennila Megavannanbfee5e32016-02-09 14:29:49 -080068 struct ib_qp *ibqp = &qp->ibqp;
69 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
70
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080071 qp->s_flags |= RVT_S_TIMER;
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080072 /* 4.096 usec. * (1 << qp->timeout) */
Vennila Megavannanbfee5e32016-02-09 14:29:49 -080073 qp->s_timer.expires = jiffies + qp->timeout_jiffies +
74 rdi->busy_jiffies;
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080075 add_timer(&qp->s_timer);
76}
77
78/**
79 * hfi1_add_rnr_timer - add/start an rnr timer
80 * @qp - the QP
81 * @to - timeout in usecs
82 *
83 * add an rnr timer on the QP
84 */
Mike Marciniszyn34cee282016-02-09 14:29:31 -080085void hfi1_add_rnr_timer(struct rvt_qp *qp, u32 to)
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080086{
Mike Marciniszyn08279d52016-02-04 10:59:36 -080087 struct hfi1_qp_priv *priv = qp->priv;
88
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080089 qp->s_flags |= RVT_S_WAIT_RNR;
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080090 qp->s_timer.expires = jiffies + usecs_to_jiffies(to);
Mike Marciniszyn08279d52016-02-04 10:59:36 -080091 add_timer(&priv->s_rnr_timer);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -080092}
93
94/**
95 * hfi1_mod_retry_timer - mod a retry timer
96 * @qp - the QP
97 *
98 * Modify a potentially already running retry
99 * timer
100 */
101static inline void hfi1_mod_retry_timer(struct rvt_qp *qp)
102{
Vennila Megavannanbfee5e32016-02-09 14:29:49 -0800103 struct ib_qp *ibqp = &qp->ibqp;
104 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
105
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800106 qp->s_flags |= RVT_S_TIMER;
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800107 /* 4.096 usec. * (1 << qp->timeout) */
Vennila Megavannanbfee5e32016-02-09 14:29:49 -0800108 mod_timer(&qp->s_timer, jiffies + qp->timeout_jiffies +
109 rdi->busy_jiffies);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800110}
111
112/**
113 * hfi1_stop_retry_timer - stop a retry timer
114 * @qp - the QP
115 *
116 * stop a retry timer and return if the timer
117 * had been pending.
118 */
119static inline int hfi1_stop_retry_timer(struct rvt_qp *qp)
120{
121 int rval = 0;
122
123 /* Remove QP from retry */
124 if (qp->s_flags & RVT_S_TIMER) {
125 qp->s_flags &= ~RVT_S_TIMER;
126 rval = del_timer(&qp->s_timer);
127 }
128 return rval;
129}
130
131/**
132 * hfi1_stop_rc_timers - stop all timers
133 * @qp - the QP
134 *
135 * stop any pending timers
136 */
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800137void hfi1_stop_rc_timers(struct rvt_qp *qp)
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800138{
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800139 struct hfi1_qp_priv *priv = qp->priv;
140
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800141 /* Remove QP from all timers */
142 if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
143 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
144 del_timer(&qp->s_timer);
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800145 del_timer(&priv->s_rnr_timer);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800146 }
147}
148
149/**
150 * hfi1_stop_rnr_timer - stop an rnr timer
151 * @qp - the QP
152 *
153 * stop an rnr timer and return if the timer
154 * had been pending.
155 */
156static inline int hfi1_stop_rnr_timer(struct rvt_qp *qp)
157{
158 int rval = 0;
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800159 struct hfi1_qp_priv *priv = qp->priv;
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800160
161 /* Remove QP from rnr timer */
162 if (qp->s_flags & RVT_S_WAIT_RNR) {
163 qp->s_flags &= ~RVT_S_WAIT_RNR;
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800164 rval = del_timer(&priv->s_rnr_timer);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800165 }
166 return rval;
167}
168
169/**
170 * hfi1_del_timers_sync - wait for any timeout routines to exit
171 * @qp - the QP
172 */
Mike Marciniszyn3c9d1492016-02-04 10:59:27 -0800173void hfi1_del_timers_sync(struct rvt_qp *qp)
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800174{
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800175 struct hfi1_qp_priv *priv = qp->priv;
176
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800177 del_timer_sync(&qp->s_timer);
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800178 del_timer_sync(&priv->s_rnr_timer);
Mike Marciniszyn9171bfd2016-02-04 10:59:01 -0800179}
180
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800181/* only opcode mask for adaptive pio */
182const u32 rc_only_opcode =
183 BIT(OP(SEND_ONLY) & 0x1f) |
184 BIT(OP(SEND_ONLY_WITH_IMMEDIATE & 0x1f)) |
185 BIT(OP(RDMA_WRITE_ONLY & 0x1f)) |
186 BIT(OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE & 0x1f)) |
187 BIT(OP(RDMA_READ_REQUEST & 0x1f)) |
188 BIT(OP(ACKNOWLEDGE & 0x1f)) |
189 BIT(OP(ATOMIC_ACKNOWLEDGE & 0x1f)) |
190 BIT(OP(COMPARE_SWAP & 0x1f)) |
191 BIT(OP(FETCH_ADD & 0x1f));
192
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800193static u32 restart_sge(struct rvt_sge_state *ss, struct rvt_swqe *wqe,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400194 u32 psn, u32 pmtu)
195{
196 u32 len;
197
198 len = delta_psn(psn, wqe->psn) * pmtu;
199 ss->sge = wqe->sg_list[0];
200 ss->sg_list = wqe->sg_list + 1;
201 ss->num_sge = wqe->wr.num_sge;
202 ss->total_len = wqe->length;
203 hfi1_skip_sge(ss, len, 0);
204 return wqe->length - len;
205}
206
Mike Marciniszyn77241052015-07-30 15:17:43 -0400207/**
208 * make_rc_ack - construct a response packet (ACK, NAK, or RDMA read)
209 * @dev: the device for this QP
210 * @qp: a pointer to the QP
211 * @ohdr: a pointer to the IB header being constructed
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800212 * @ps: the xmit packet state
Mike Marciniszyn77241052015-07-30 15:17:43 -0400213 *
214 * Return 1 if constructed; otherwise, return 0.
215 * Note that we are in the responder's side of the QP context.
216 * Note the QP s_lock must be held.
217 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800218static int make_rc_ack(struct hfi1_ibdev *dev, struct rvt_qp *qp,
Mike Marciniszyn1235bef82016-02-14 12:45:09 -0800219 struct hfi1_other_headers *ohdr,
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800220 struct hfi1_pkt_state *ps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400221{
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800222 struct rvt_ack_entry *e;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400223 u32 hwords;
224 u32 len;
225 u32 bth0;
226 u32 bth2;
227 int middle = 0;
Mike Marciniszyn1235bef82016-02-14 12:45:09 -0800228 u32 pmtu = qp->pmtu;
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800229 struct hfi1_qp_priv *priv = qp->priv;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400230
231 /* Don't send an ACK if we aren't supposed to. */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800232 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400233 goto bail;
234
235 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
236 hwords = 5;
237
238 switch (qp->s_ack_state) {
239 case OP(RDMA_READ_RESPONSE_LAST):
240 case OP(RDMA_READ_RESPONSE_ONLY):
241 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
242 if (e->rdma_sge.mr) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800243 rvt_put_mr(e->rdma_sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400244 e->rdma_sge.mr = NULL;
245 }
246 /* FALLTHROUGH */
247 case OP(ATOMIC_ACKNOWLEDGE):
248 /*
249 * We can increment the tail pointer now that the last
250 * response has been sent instead of only being
251 * constructed.
252 */
253 if (++qp->s_tail_ack_queue > HFI1_MAX_RDMA_ATOMIC)
254 qp->s_tail_ack_queue = 0;
255 /* FALLTHROUGH */
256 case OP(SEND_ONLY):
257 case OP(ACKNOWLEDGE):
258 /* Check for no next entry in the queue. */
259 if (qp->r_head_ack_queue == qp->s_tail_ack_queue) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800260 if (qp->s_flags & RVT_S_ACK_PENDING)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400261 goto normal;
262 goto bail;
263 }
264
265 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
266 if (e->opcode == OP(RDMA_READ_REQUEST)) {
267 /*
268 * If a RDMA read response is being resent and
269 * we haven't seen the duplicate request yet,
270 * then stop sending the remaining responses the
271 * responder has seen until the requester re-sends it.
272 */
273 len = e->rdma_sge.sge_length;
274 if (len && !e->rdma_sge.mr) {
275 qp->s_tail_ack_queue = qp->r_head_ack_queue;
276 goto bail;
277 }
278 /* Copy SGE state in case we need to resend */
Mike Marciniszync239a5b2016-02-14 12:44:52 -0800279 ps->s_txreq->mr = e->rdma_sge.mr;
280 if (ps->s_txreq->mr)
281 rvt_get_mr(ps->s_txreq->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400282 qp->s_ack_rdma_sge.sge = e->rdma_sge;
283 qp->s_ack_rdma_sge.num_sge = 1;
284 qp->s_cur_sge = &qp->s_ack_rdma_sge;
285 if (len > pmtu) {
286 len = pmtu;
287 qp->s_ack_state = OP(RDMA_READ_RESPONSE_FIRST);
288 } else {
289 qp->s_ack_state = OP(RDMA_READ_RESPONSE_ONLY);
290 e->sent = 1;
291 }
292 ohdr->u.aeth = hfi1_compute_aeth(qp);
293 hwords++;
294 qp->s_ack_rdma_psn = e->psn;
295 bth2 = mask_psn(qp->s_ack_rdma_psn++);
296 } else {
297 /* COMPARE_SWAP or FETCH_ADD */
298 qp->s_cur_sge = NULL;
299 len = 0;
300 qp->s_ack_state = OP(ATOMIC_ACKNOWLEDGE);
301 ohdr->u.at.aeth = hfi1_compute_aeth(qp);
302 ohdr->u.at.atomic_ack_eth[0] =
303 cpu_to_be32(e->atomic_data >> 32);
304 ohdr->u.at.atomic_ack_eth[1] =
305 cpu_to_be32(e->atomic_data);
306 hwords += sizeof(ohdr->u.at) / sizeof(u32);
307 bth2 = mask_psn(e->psn);
308 e->sent = 1;
309 }
310 bth0 = qp->s_ack_state << 24;
311 break;
312
313 case OP(RDMA_READ_RESPONSE_FIRST):
314 qp->s_ack_state = OP(RDMA_READ_RESPONSE_MIDDLE);
315 /* FALLTHROUGH */
316 case OP(RDMA_READ_RESPONSE_MIDDLE):
317 qp->s_cur_sge = &qp->s_ack_rdma_sge;
Mike Marciniszync239a5b2016-02-14 12:44:52 -0800318 ps->s_txreq->mr = qp->s_ack_rdma_sge.sge.mr;
319 if (ps->s_txreq->mr)
320 rvt_get_mr(ps->s_txreq->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400321 len = qp->s_ack_rdma_sge.sge.sge_length;
322 if (len > pmtu) {
323 len = pmtu;
324 middle = HFI1_CAP_IS_KSET(SDMA_AHG);
325 } else {
326 ohdr->u.aeth = hfi1_compute_aeth(qp);
327 hwords++;
328 qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
329 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
330 e->sent = 1;
331 }
332 bth0 = qp->s_ack_state << 24;
333 bth2 = mask_psn(qp->s_ack_rdma_psn++);
334 break;
335
336 default:
337normal:
338 /*
339 * Send a regular ACK.
340 * Set the s_ack_state so we wait until after sending
341 * the ACK before setting s_ack_state to ACKNOWLEDGE
342 * (see above).
343 */
344 qp->s_ack_state = OP(SEND_ONLY);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800345 qp->s_flags &= ~RVT_S_ACK_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400346 qp->s_cur_sge = NULL;
347 if (qp->s_nak_state)
348 ohdr->u.aeth =
349 cpu_to_be32((qp->r_msn & HFI1_MSN_MASK) |
350 (qp->s_nak_state <<
351 HFI1_AETH_CREDIT_SHIFT));
352 else
353 ohdr->u.aeth = hfi1_compute_aeth(qp);
354 hwords++;
355 len = 0;
356 bth0 = OP(ACKNOWLEDGE) << 24;
357 bth2 = mask_psn(qp->s_ack_psn);
358 }
359 qp->s_rdma_ack_cnt++;
360 qp->s_hdrwords = hwords;
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800361 ps->s_txreq->sde = priv->s_sde;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400362 qp->s_cur_size = len;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800363 hfi1_make_ruc_header(qp, ohdr, bth0, bth2, middle, ps);
Jianxin Xiongaa0ad412016-02-26 13:33:13 -0800364 /* pbc */
365 ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400366 return 1;
367
368bail:
369 qp->s_ack_state = OP(ACKNOWLEDGE);
370 /*
371 * Ensure s_rdma_ack_cnt changes are committed prior to resetting
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800372 * RVT_S_RESP_PENDING
Mike Marciniszyn77241052015-07-30 15:17:43 -0400373 */
374 smp_wmb();
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800375 qp->s_flags &= ~(RVT_S_RESP_PENDING
376 | RVT_S_ACK_PENDING
377 | RVT_S_AHG_VALID);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400378 return 0;
379}
380
381/**
382 * hfi1_make_rc_req - construct a request packet (SEND, RDMA r/w, ATOMIC)
383 * @qp: a pointer to the QP
384 *
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800385 * Assumes s_lock is held.
386 *
Mike Marciniszyn77241052015-07-30 15:17:43 -0400387 * Return 1 if constructed; otherwise, return 0.
388 */
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800389int hfi1_make_rc_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400390{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800391 struct hfi1_qp_priv *priv = qp->priv;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400392 struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
393 struct hfi1_other_headers *ohdr;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800394 struct rvt_sge_state *ss;
395 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400396 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
397 u32 hwords = 5;
398 u32 len;
399 u32 bth0 = 0;
400 u32 bth2;
401 u32 pmtu = qp->pmtu;
402 char newreq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400403 int middle = 0;
404 int delta;
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700405 int err;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400406
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800407 ps->s_txreq = get_txreq(ps->dev, qp);
408 if (IS_ERR(ps->s_txreq))
409 goto bail_no_tx;
410
411 ohdr = &ps->s_txreq->phdr.hdr.u.oth;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400412 if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800413 ohdr = &ps->s_txreq->phdr.hdr.u.l.oth;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400414
Mike Marciniszyn77241052015-07-30 15:17:43 -0400415 /* Sending responses has higher priority over sending requests. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800416 if ((qp->s_flags & RVT_S_RESP_PENDING) &&
Mike Marciniszyn1235bef82016-02-14 12:45:09 -0800417 make_rc_ack(dev, qp, ohdr, ps))
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800418 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400419
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800420 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_SEND_OK)) {
421 if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400422 goto bail;
423 /* We are in the error state, flush the work request. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800424 smp_read_barrier_depends(); /* see post_one_send() */
425 if (qp->s_last == ACCESS_ONCE(qp->s_head))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400426 goto bail;
427 /* If DMAs are in progress, we can't flush immediately. */
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800428 if (iowait_sdma_pending(&priv->s_iowait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800429 qp->s_flags |= RVT_S_WAIT_DMA;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400430 goto bail;
431 }
432 clear_ahg(qp);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800433 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400434 hfi1_send_complete(qp, wqe, qp->s_last != qp->s_acked ?
435 IB_WC_SUCCESS : IB_WC_WR_FLUSH_ERR);
436 /* will get called again */
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800437 goto done_free_tx;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400438 }
439
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800440 if (qp->s_flags & (RVT_S_WAIT_RNR | RVT_S_WAIT_ACK))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400441 goto bail;
442
443 if (cmp_psn(qp->s_psn, qp->s_sending_hpsn) <= 0) {
444 if (cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800445 qp->s_flags |= RVT_S_WAIT_PSN;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400446 goto bail;
447 }
448 qp->s_sending_psn = qp->s_psn;
449 qp->s_sending_hpsn = qp->s_psn - 1;
450 }
451
452 /* Send a request. */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800453 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400454 switch (qp->s_state) {
455 default:
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800456 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400457 goto bail;
458 /*
459 * Resend an old request or start a new one.
460 *
461 * We keep track of the current SWQE so that
462 * we don't reset the "furthest progress" state
463 * if we need to back up.
464 */
465 newreq = 0;
466 if (qp->s_cur == qp->s_tail) {
467 /* Check if send work queue is empty. */
468 if (qp->s_tail == qp->s_head) {
469 clear_ahg(qp);
470 goto bail;
471 }
472 /*
473 * If a fence is requested, wait for previous
474 * RDMA read and atomic operations to finish.
475 */
476 if ((wqe->wr.send_flags & IB_SEND_FENCE) &&
477 qp->s_num_rd_atomic) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800478 qp->s_flags |= RVT_S_WAIT_FENCE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400479 goto bail;
480 }
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700481 /*
482 * Local operations are processed immediately
483 * after all prior requests have completed
484 */
485 if (wqe->wr.opcode == IB_WR_REG_MR ||
486 wqe->wr.opcode == IB_WR_LOCAL_INV) {
487 if (qp->s_last != qp->s_cur)
488 goto bail;
489 if (++qp->s_cur == qp->s_size)
490 qp->s_cur = 0;
491 if (++qp->s_tail == qp->s_size)
492 qp->s_tail = 0;
493 if (wqe->wr.opcode == IB_WR_REG_MR)
494 err = rvt_fast_reg_mr(
495 qp, wqe->reg_wr.mr,
496 wqe->reg_wr.key,
497 wqe->reg_wr.access);
498 else
499 err = rvt_invalidate_rkey(
500 qp,
501 wqe->wr.ex.invalidate_rkey);
502 hfi1_send_complete(qp, wqe,
503 err ? IB_WC_LOC_PROT_ERR
504 : IB_WC_SUCCESS);
505 atomic_dec(&qp->local_ops_pending);
506 qp->s_hdrwords = 0;
507 goto done_free_tx;
508 }
509
Mike Marciniszyn77241052015-07-30 15:17:43 -0400510 newreq = 1;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800511 qp->s_psn = wqe->psn;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400512 }
513 /*
514 * Note that we have to be careful not to modify the
515 * original work request since we may need to resend
516 * it.
517 */
518 len = wqe->length;
519 ss = &qp->s_sge;
520 bth2 = mask_psn(qp->s_psn);
521 switch (wqe->wr.opcode) {
522 case IB_WR_SEND:
523 case IB_WR_SEND_WITH_IMM:
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700524 case IB_WR_SEND_WITH_INV:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400525 /* If no credit, return. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800526 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
Mike Marciniszyn77241052015-07-30 15:17:43 -0400527 cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800528 qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400529 goto bail;
530 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400531 if (len > pmtu) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400532 qp->s_state = OP(SEND_FIRST);
533 len = pmtu;
534 break;
535 }
Jubin Johne4909742016-02-14 20:22:00 -0800536 if (wqe->wr.opcode == IB_WR_SEND) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400537 qp->s_state = OP(SEND_ONLY);
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700538 } else if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400539 qp->s_state = OP(SEND_ONLY_WITH_IMMEDIATE);
540 /* Immediate data comes after the BTH */
541 ohdr->u.imm_data = wqe->wr.ex.imm_data;
542 hwords += 1;
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700543 } else {
544 qp->s_state = OP(SEND_ONLY_WITH_INVALIDATE);
545 /* Invalidate rkey comes after the BTH */
546 ohdr->u.ieth = cpu_to_be32(
547 wqe->wr.ex.invalidate_rkey);
548 hwords += 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400549 }
550 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
551 bth0 |= IB_BTH_SOLICITED;
552 bth2 |= IB_BTH_REQ_ACK;
553 if (++qp->s_cur == qp->s_size)
554 qp->s_cur = 0;
555 break;
556
557 case IB_WR_RDMA_WRITE:
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800558 if (newreq && !(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400559 qp->s_lsn++;
560 /* FALLTHROUGH */
561 case IB_WR_RDMA_WRITE_WITH_IMM:
562 /* If no credit, return. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800563 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
Mike Marciniszyn77241052015-07-30 15:17:43 -0400564 cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800565 qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400566 goto bail;
567 }
568 ohdr->u.rc.reth.vaddr =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100569 cpu_to_be64(wqe->rdma_wr.remote_addr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400570 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100571 cpu_to_be32(wqe->rdma_wr.rkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400572 ohdr->u.rc.reth.length = cpu_to_be32(len);
573 hwords += sizeof(struct ib_reth) / sizeof(u32);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400574 if (len > pmtu) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400575 qp->s_state = OP(RDMA_WRITE_FIRST);
576 len = pmtu;
577 break;
578 }
Jubin Johne4909742016-02-14 20:22:00 -0800579 if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400580 qp->s_state = OP(RDMA_WRITE_ONLY);
Jubin Johne4909742016-02-14 20:22:00 -0800581 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400582 qp->s_state =
583 OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
584 /* Immediate data comes after RETH */
585 ohdr->u.rc.imm_data = wqe->wr.ex.imm_data;
586 hwords += 1;
587 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
588 bth0 |= IB_BTH_SOLICITED;
589 }
590 bth2 |= IB_BTH_REQ_ACK;
591 if (++qp->s_cur == qp->s_size)
592 qp->s_cur = 0;
593 break;
594
595 case IB_WR_RDMA_READ:
596 /*
597 * Don't allow more operations to be started
598 * than the QP limits allow.
599 */
600 if (newreq) {
601 if (qp->s_num_rd_atomic >=
602 qp->s_max_rd_atomic) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800603 qp->s_flags |= RVT_S_WAIT_RDMAR;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400604 goto bail;
605 }
606 qp->s_num_rd_atomic++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800607 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400608 qp->s_lsn++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400609 }
610 ohdr->u.rc.reth.vaddr =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100611 cpu_to_be64(wqe->rdma_wr.remote_addr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400612 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100613 cpu_to_be32(wqe->rdma_wr.rkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400614 ohdr->u.rc.reth.length = cpu_to_be32(len);
615 qp->s_state = OP(RDMA_READ_REQUEST);
616 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
617 ss = NULL;
618 len = 0;
619 bth2 |= IB_BTH_REQ_ACK;
620 if (++qp->s_cur == qp->s_size)
621 qp->s_cur = 0;
622 break;
623
624 case IB_WR_ATOMIC_CMP_AND_SWP:
625 case IB_WR_ATOMIC_FETCH_AND_ADD:
626 /*
627 * Don't allow more operations to be started
628 * than the QP limits allow.
629 */
630 if (newreq) {
631 if (qp->s_num_rd_atomic >=
632 qp->s_max_rd_atomic) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800633 qp->s_flags |= RVT_S_WAIT_RDMAR;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400634 goto bail;
635 }
636 qp->s_num_rd_atomic++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800637 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400638 qp->s_lsn++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400639 }
640 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
641 qp->s_state = OP(COMPARE_SWAP);
642 ohdr->u.atomic_eth.swap_data = cpu_to_be64(
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100643 wqe->atomic_wr.swap);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400644 ohdr->u.atomic_eth.compare_data = cpu_to_be64(
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100645 wqe->atomic_wr.compare_add);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400646 } else {
647 qp->s_state = OP(FETCH_ADD);
648 ohdr->u.atomic_eth.swap_data = cpu_to_be64(
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100649 wqe->atomic_wr.compare_add);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400650 ohdr->u.atomic_eth.compare_data = 0;
651 }
652 ohdr->u.atomic_eth.vaddr[0] = cpu_to_be32(
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100653 wqe->atomic_wr.remote_addr >> 32);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400654 ohdr->u.atomic_eth.vaddr[1] = cpu_to_be32(
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100655 wqe->atomic_wr.remote_addr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400656 ohdr->u.atomic_eth.rkey = cpu_to_be32(
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100657 wqe->atomic_wr.rkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400658 hwords += sizeof(struct ib_atomic_eth) / sizeof(u32);
659 ss = NULL;
660 len = 0;
661 bth2 |= IB_BTH_REQ_ACK;
662 if (++qp->s_cur == qp->s_size)
663 qp->s_cur = 0;
664 break;
665
666 default:
667 goto bail;
668 }
669 qp->s_sge.sge = wqe->sg_list[0];
670 qp->s_sge.sg_list = wqe->sg_list + 1;
671 qp->s_sge.num_sge = wqe->wr.num_sge;
672 qp->s_sge.total_len = wqe->length;
673 qp->s_len = wqe->length;
674 if (newreq) {
675 qp->s_tail++;
676 if (qp->s_tail >= qp->s_size)
677 qp->s_tail = 0;
678 }
679 if (wqe->wr.opcode == IB_WR_RDMA_READ)
680 qp->s_psn = wqe->lpsn + 1;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800681 else
Mike Marciniszyn77241052015-07-30 15:17:43 -0400682 qp->s_psn++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400683 break;
684
685 case OP(RDMA_READ_RESPONSE_FIRST):
686 /*
687 * qp->s_state is normally set to the opcode of the
688 * last packet constructed for new requests and therefore
689 * is never set to RDMA read response.
690 * RDMA_READ_RESPONSE_FIRST is used by the ACK processing
691 * thread to indicate a SEND needs to be restarted from an
692 * earlier PSN without interfering with the sending thread.
693 * See restart_rc().
694 */
695 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
696 /* FALLTHROUGH */
697 case OP(SEND_FIRST):
698 qp->s_state = OP(SEND_MIDDLE);
699 /* FALLTHROUGH */
700 case OP(SEND_MIDDLE):
701 bth2 = mask_psn(qp->s_psn++);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400702 ss = &qp->s_sge;
703 len = qp->s_len;
704 if (len > pmtu) {
705 len = pmtu;
706 middle = HFI1_CAP_IS_KSET(SDMA_AHG);
707 break;
708 }
Jubin Johne4909742016-02-14 20:22:00 -0800709 if (wqe->wr.opcode == IB_WR_SEND) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400710 qp->s_state = OP(SEND_LAST);
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700711 } else if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400712 qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
713 /* Immediate data comes after the BTH */
714 ohdr->u.imm_data = wqe->wr.ex.imm_data;
715 hwords += 1;
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700716 } else {
717 qp->s_state = OP(SEND_LAST_WITH_INVALIDATE);
718 /* invalidate data comes after the BTH */
719 ohdr->u.ieth = cpu_to_be32(wqe->wr.ex.invalidate_rkey);
720 hwords += 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400721 }
722 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
723 bth0 |= IB_BTH_SOLICITED;
724 bth2 |= IB_BTH_REQ_ACK;
725 qp->s_cur++;
726 if (qp->s_cur >= qp->s_size)
727 qp->s_cur = 0;
728 break;
729
730 case OP(RDMA_READ_RESPONSE_LAST):
731 /*
732 * qp->s_state is normally set to the opcode of the
733 * last packet constructed for new requests and therefore
734 * is never set to RDMA read response.
735 * RDMA_READ_RESPONSE_LAST is used by the ACK processing
736 * thread to indicate a RDMA write needs to be restarted from
737 * an earlier PSN without interfering with the sending thread.
738 * See restart_rc().
739 */
740 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
741 /* FALLTHROUGH */
742 case OP(RDMA_WRITE_FIRST):
743 qp->s_state = OP(RDMA_WRITE_MIDDLE);
744 /* FALLTHROUGH */
745 case OP(RDMA_WRITE_MIDDLE):
746 bth2 = mask_psn(qp->s_psn++);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400747 ss = &qp->s_sge;
748 len = qp->s_len;
749 if (len > pmtu) {
750 len = pmtu;
751 middle = HFI1_CAP_IS_KSET(SDMA_AHG);
752 break;
753 }
Jubin Johne4909742016-02-14 20:22:00 -0800754 if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400755 qp->s_state = OP(RDMA_WRITE_LAST);
Jubin Johne4909742016-02-14 20:22:00 -0800756 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400757 qp->s_state = OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
758 /* Immediate data comes after the BTH */
759 ohdr->u.imm_data = wqe->wr.ex.imm_data;
760 hwords += 1;
761 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
762 bth0 |= IB_BTH_SOLICITED;
763 }
764 bth2 |= IB_BTH_REQ_ACK;
765 qp->s_cur++;
766 if (qp->s_cur >= qp->s_size)
767 qp->s_cur = 0;
768 break;
769
770 case OP(RDMA_READ_RESPONSE_MIDDLE):
771 /*
772 * qp->s_state is normally set to the opcode of the
773 * last packet constructed for new requests and therefore
774 * is never set to RDMA read response.
775 * RDMA_READ_RESPONSE_MIDDLE is used by the ACK processing
776 * thread to indicate a RDMA read needs to be restarted from
777 * an earlier PSN without interfering with the sending thread.
778 * See restart_rc().
779 */
780 len = (delta_psn(qp->s_psn, wqe->psn)) * pmtu;
781 ohdr->u.rc.reth.vaddr =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100782 cpu_to_be64(wqe->rdma_wr.remote_addr + len);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400783 ohdr->u.rc.reth.rkey =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100784 cpu_to_be32(wqe->rdma_wr.rkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400785 ohdr->u.rc.reth.length = cpu_to_be32(wqe->length - len);
786 qp->s_state = OP(RDMA_READ_REQUEST);
787 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
788 bth2 = mask_psn(qp->s_psn) | IB_BTH_REQ_ACK;
789 qp->s_psn = wqe->lpsn + 1;
790 ss = NULL;
791 len = 0;
792 qp->s_cur++;
793 if (qp->s_cur == qp->s_size)
794 qp->s_cur = 0;
795 break;
796 }
797 qp->s_sending_hpsn = bth2;
798 delta = delta_psn(bth2, wqe->psn);
799 if (delta && delta % HFI1_PSN_CREDIT == 0)
800 bth2 |= IB_BTH_REQ_ACK;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800801 if (qp->s_flags & RVT_S_SEND_ONE) {
802 qp->s_flags &= ~RVT_S_SEND_ONE;
803 qp->s_flags |= RVT_S_WAIT_ACK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400804 bth2 |= IB_BTH_REQ_ACK;
805 }
806 qp->s_len -= len;
807 qp->s_hdrwords = hwords;
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800808 ps->s_txreq->sde = priv->s_sde;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400809 qp->s_cur_sge = ss;
810 qp->s_cur_size = len;
811 hfi1_make_ruc_header(
812 qp,
813 ohdr,
814 bth0 | (qp->s_state << 24),
815 bth2,
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800816 middle,
817 ps);
Jianxin Xiongaa0ad412016-02-26 13:33:13 -0800818 /* pbc */
819 ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800820 return 1;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800821
822done_free_tx:
823 hfi1_put_txreq(ps->s_txreq);
824 ps->s_txreq = NULL;
825 return 1;
826
Mike Marciniszyn77241052015-07-30 15:17:43 -0400827bail:
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800828 hfi1_put_txreq(ps->s_txreq);
829
830bail_no_tx:
831 ps->s_txreq = NULL;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800832 qp->s_flags &= ~RVT_S_BUSY;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800833 qp->s_hdrwords = 0;
834 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400835}
836
837/**
838 * hfi1_send_rc_ack - Construct an ACK packet and send it
839 * @qp: a pointer to the QP
840 *
841 * This is called from hfi1_rc_rcv() and handle_receive_interrupt().
842 * Note that RDMA reads and atomics are handled in the
843 * send side QP state and tasklet.
844 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800845void hfi1_send_rc_ack(struct hfi1_ctxtdata *rcd, struct rvt_qp *qp,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400846 int is_fecn)
847{
848 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
849 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
850 u64 pbc, pbc_flags = 0;
851 u16 lrh0;
852 u16 sc5;
853 u32 bth0;
854 u32 hwords;
855 u32 vl, plen;
856 struct send_context *sc;
857 struct pio_buf *pbuf;
858 struct hfi1_ib_header hdr;
859 struct hfi1_other_headers *ohdr;
Dean Luickb77d7132015-10-26 10:28:43 -0400860 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400861
862 /* Don't send ACK or NAK if a RDMA read or atomic is pending. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800863 if (qp->s_flags & RVT_S_RESP_PENDING)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400864 goto queue_ack;
865
866 /* Ensure s_rdma_ack_cnt changes are committed */
867 smp_read_barrier_depends();
868 if (qp->s_rdma_ack_cnt)
869 goto queue_ack;
870
871 /* Construct the header */
872 /* header size in 32-bit words LRH+BTH+AETH = (8+12+4)/4 */
873 hwords = 6;
874 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
875 hwords += hfi1_make_grh(ibp, &hdr.u.l.grh,
876 &qp->remote_ah_attr.grh, hwords, 0);
877 ohdr = &hdr.u.l.oth;
878 lrh0 = HFI1_LRH_GRH;
879 } else {
880 ohdr = &hdr.u.oth;
881 lrh0 = HFI1_LRH_BTH;
882 }
883 /* read pkey_index w/o lock (its atomic) */
884 bth0 = hfi1_get_pkey(ibp, qp->s_pkey_index) | (OP(ACKNOWLEDGE) << 24);
885 if (qp->s_mig_state == IB_MIG_MIGRATED)
886 bth0 |= IB_BTH_MIG_REQ;
887 if (qp->r_nak_state)
888 ohdr->u.aeth = cpu_to_be32((qp->r_msn & HFI1_MSN_MASK) |
889 (qp->r_nak_state <<
890 HFI1_AETH_CREDIT_SHIFT));
891 else
892 ohdr->u.aeth = hfi1_compute_aeth(qp);
893 sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl];
894 /* set PBC_DC_INFO bit (aka SC[4]) in pbc_flags */
895 pbc_flags |= ((!!(sc5 & 0x10)) << PBC_DC_INFO_SHIFT);
896 lrh0 |= (sc5 & 0xf) << 12 | (qp->remote_ah_attr.sl & 0xf) << 4;
897 hdr.lrh[0] = cpu_to_be16(lrh0);
898 hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
899 hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
900 hdr.lrh[3] = cpu_to_be16(ppd->lid | qp->remote_ah_attr.src_path_bits);
901 ohdr->bth[0] = cpu_to_be32(bth0);
902 ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
903 ohdr->bth[1] |= cpu_to_be32((!!is_fecn) << HFI1_BECN_SHIFT);
904 ohdr->bth[2] = cpu_to_be32(mask_psn(qp->r_ack_psn));
905
906 /* Don't try to send ACKs if the link isn't ACTIVE */
907 if (driver_lstate(ppd) != IB_PORT_ACTIVE)
908 return;
909
910 sc = rcd->sc;
911 plen = 2 /* PBC */ + hwords;
912 vl = sc_to_vlt(ppd->dd, sc5);
913 pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
914
915 pbuf = sc_buffer_alloc(sc, plen, NULL, NULL);
916 if (!pbuf) {
917 /*
918 * We have no room to send at the moment. Pass
919 * responsibility for sending the ACK to the send tasklet
920 * so that when enough buffer space becomes available,
921 * the ACK is sent ahead of other outgoing packets.
922 */
923 goto queue_ack;
924 }
925
Mike Marciniszyn1db78ee2016-03-07 11:35:19 -0800926 trace_ack_output_ibhdr(dd_from_ibdev(qp->ibqp.device), &hdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400927
928 /* write the pbc and data */
929 ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc, &hdr, hwords);
930
931 return;
932
933queue_ack:
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800934 this_cpu_inc(*ibp->rvp.rc_qacks);
Dean Luickb77d7132015-10-26 10:28:43 -0400935 spin_lock_irqsave(&qp->s_lock, flags);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800936 qp->s_flags |= RVT_S_ACK_PENDING | RVT_S_RESP_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400937 qp->s_nak_state = qp->r_nak_state;
938 qp->s_ack_psn = qp->r_ack_psn;
939 if (is_fecn)
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800940 qp->s_flags |= RVT_S_ECN;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400941
942 /* Schedule the send tasklet. */
943 hfi1_schedule_send(qp);
Dean Luickb77d7132015-10-26 10:28:43 -0400944 spin_unlock_irqrestore(&qp->s_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400945}
946
947/**
948 * reset_psn - reset the QP state to send starting from PSN
949 * @qp: the QP
950 * @psn: the packet sequence number to restart at
951 *
952 * This is called from hfi1_rc_rcv() to process an incoming RC ACK
953 * for the given QP.
954 * Called at interrupt level with the QP s_lock held.
955 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800956static void reset_psn(struct rvt_qp *qp, u32 psn)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400957{
958 u32 n = qp->s_acked;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800959 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, n);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400960 u32 opcode;
961
962 qp->s_cur = n;
963
964 /*
965 * If we are starting the request from the beginning,
966 * let the normal send code handle initialization.
967 */
968 if (cmp_psn(psn, wqe->psn) <= 0) {
969 qp->s_state = OP(SEND_LAST);
970 goto done;
971 }
972
973 /* Find the work request opcode corresponding to the given PSN. */
974 opcode = wqe->wr.opcode;
975 for (;;) {
976 int diff;
977
978 if (++n == qp->s_size)
979 n = 0;
980 if (n == qp->s_tail)
981 break;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800982 wqe = rvt_get_swqe_ptr(qp, n);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400983 diff = cmp_psn(psn, wqe->psn);
984 if (diff < 0)
985 break;
986 qp->s_cur = n;
987 /*
988 * If we are starting the request from the beginning,
989 * let the normal send code handle initialization.
990 */
991 if (diff == 0) {
992 qp->s_state = OP(SEND_LAST);
993 goto done;
994 }
995 opcode = wqe->wr.opcode;
996 }
997
998 /*
999 * Set the state to restart in the middle of a request.
1000 * Don't change the s_sge, s_cur_sge, or s_cur_size.
1001 * See hfi1_make_rc_req().
1002 */
1003 switch (opcode) {
1004 case IB_WR_SEND:
1005 case IB_WR_SEND_WITH_IMM:
1006 qp->s_state = OP(RDMA_READ_RESPONSE_FIRST);
1007 break;
1008
1009 case IB_WR_RDMA_WRITE:
1010 case IB_WR_RDMA_WRITE_WITH_IMM:
1011 qp->s_state = OP(RDMA_READ_RESPONSE_LAST);
1012 break;
1013
1014 case IB_WR_RDMA_READ:
1015 qp->s_state = OP(RDMA_READ_RESPONSE_MIDDLE);
1016 break;
1017
1018 default:
1019 /*
1020 * This case shouldn't happen since its only
1021 * one PSN per req.
1022 */
1023 qp->s_state = OP(SEND_LAST);
1024 }
1025done:
1026 qp->s_psn = psn;
1027 /*
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001028 * Set RVT_S_WAIT_PSN as rc_complete() may start the timer
Mike Marciniszyn77241052015-07-30 15:17:43 -04001029 * asynchronously before the send tasklet can get scheduled.
1030 * Doing it in hfi1_make_rc_req() is too late.
1031 */
1032 if ((cmp_psn(qp->s_psn, qp->s_sending_hpsn) <= 0) &&
1033 (cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0))
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001034 qp->s_flags |= RVT_S_WAIT_PSN;
1035 qp->s_flags &= ~RVT_S_AHG_VALID;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001036}
1037
1038/*
1039 * Back up requester to resend the last un-ACKed request.
1040 * The QP r_lock and s_lock should be held and interrupts disabled.
1041 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001042static void restart_rc(struct rvt_qp *qp, u32 psn, int wait)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001043{
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001044 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001045 struct hfi1_ibport *ibp;
1046
1047 if (qp->s_retry == 0) {
1048 if (qp->s_mig_state == IB_MIG_ARMED) {
1049 hfi1_migrate_qp(qp);
1050 qp->s_retry = qp->s_retry_cnt;
1051 } else if (qp->s_last == qp->s_acked) {
1052 hfi1_send_complete(qp, wqe, IB_WC_RETRY_EXC_ERR);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08001053 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001054 return;
Jubin Johne4909742016-02-14 20:22:00 -08001055 } else { /* need to handle delayed completion */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001056 return;
Jubin Johne4909742016-02-14 20:22:00 -08001057 }
1058 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001059 qp->s_retry--;
Jubin Johne4909742016-02-14 20:22:00 -08001060 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001061
1062 ibp = to_iport(qp->ibqp.device, qp->port_num);
1063 if (wqe->wr.opcode == IB_WR_RDMA_READ)
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001064 ibp->rvp.n_rc_resends++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001065 else
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001066 ibp->rvp.n_rc_resends += delta_psn(qp->s_psn, psn);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001067
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001068 qp->s_flags &= ~(RVT_S_WAIT_FENCE | RVT_S_WAIT_RDMAR |
1069 RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_PSN |
1070 RVT_S_WAIT_ACK);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001071 if (wait)
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001072 qp->s_flags |= RVT_S_SEND_ONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001073 reset_psn(qp, psn);
1074}
1075
1076/*
1077 * This is called from s_timer for missing responses.
1078 */
Mike Marciniszyn08279d52016-02-04 10:59:36 -08001079void hfi1_rc_timeout(unsigned long arg)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001080{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001081 struct rvt_qp *qp = (struct rvt_qp *)arg;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001082 struct hfi1_ibport *ibp;
1083 unsigned long flags;
1084
1085 spin_lock_irqsave(&qp->r_lock, flags);
1086 spin_lock(&qp->s_lock);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001087 if (qp->s_flags & RVT_S_TIMER) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001088 ibp = to_iport(qp->ibqp.device, qp->port_num);
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001089 ibp->rvp.n_rc_timeouts++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001090 qp->s_flags &= ~RVT_S_TIMER;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001091 del_timer(&qp->s_timer);
Sebastian Sanchez462b6b22016-07-01 16:01:06 -07001092 trace_hfi1_timeout(qp, qp->s_last_psn + 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001093 restart_rc(qp, qp->s_last_psn + 1, 1);
1094 hfi1_schedule_send(qp);
1095 }
1096 spin_unlock(&qp->s_lock);
1097 spin_unlock_irqrestore(&qp->r_lock, flags);
1098}
1099
1100/*
1101 * This is called from s_timer for RNR timeouts.
1102 */
1103void hfi1_rc_rnr_retry(unsigned long arg)
1104{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001105 struct rvt_qp *qp = (struct rvt_qp *)arg;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001106 unsigned long flags;
1107
1108 spin_lock_irqsave(&qp->s_lock, flags);
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001109 hfi1_stop_rnr_timer(qp);
1110 hfi1_schedule_send(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001111 spin_unlock_irqrestore(&qp->s_lock, flags);
1112}
1113
1114/*
1115 * Set qp->s_sending_psn to the next PSN after the given one.
1116 * This would be psn+1 except when RDMA reads are present.
1117 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001118static void reset_sending_psn(struct rvt_qp *qp, u32 psn)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001119{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001120 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001121 u32 n = qp->s_last;
1122
1123 /* Find the work request corresponding to the given PSN. */
1124 for (;;) {
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001125 wqe = rvt_get_swqe_ptr(qp, n);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001126 if (cmp_psn(psn, wqe->lpsn) <= 0) {
1127 if (wqe->wr.opcode == IB_WR_RDMA_READ)
1128 qp->s_sending_psn = wqe->lpsn + 1;
1129 else
1130 qp->s_sending_psn = psn + 1;
1131 break;
1132 }
1133 if (++n == qp->s_size)
1134 n = 0;
1135 if (n == qp->s_tail)
1136 break;
1137 }
1138}
1139
1140/*
1141 * This should be called with the QP s_lock held and interrupts disabled.
1142 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001143void hfi1_rc_send_complete(struct rvt_qp *qp, struct hfi1_ib_header *hdr)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001144{
1145 struct hfi1_other_headers *ohdr;
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001146 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001147 struct ib_wc wc;
1148 unsigned i;
1149 u32 opcode;
1150 u32 psn;
1151
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
1243 /*
1244 * Don't decrement refcount and don't generate a
1245 * completion if the SWQE is being resent until the send
1246 * is finished.
1247 */
1248 if (cmp_psn(wqe->lpsn, qp->s_sending_psn) < 0 ||
1249 cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -08001250 u32 s_last;
1251
Mike Marciniszyn77241052015-07-30 15:17:43 -04001252 for (i = 0; i < wqe->wr.num_sge; i++) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001253 struct rvt_sge *sge = &wqe->sg_list[i];
Mike Marciniszyn77241052015-07-30 15:17:43 -04001254
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001255 rvt_put_mr(sge->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001256 }
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -08001257 s_last = qp->s_last;
1258 if (++s_last >= qp->s_size)
1259 s_last = 0;
1260 qp->s_last = s_last;
1261 /* see post_send() */
1262 barrier();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001263 /* Post a send completion queue entry if requested. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001264 if (!(qp->s_flags & RVT_S_SIGNAL_REQ_WR) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04001265 (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
1266 memset(&wc, 0, sizeof(wc));
1267 wc.wr_id = wqe->wr.wr_id;
1268 wc.status = IB_WC_SUCCESS;
1269 wc.opcode = ib_hfi1_wc_opcode[wqe->wr.opcode];
1270 wc.byte_len = wqe->length;
1271 wc.qp = &qp->ibqp;
Dennis Dalessandroabd712d2016-01-19 14:43:22 -08001272 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.send_cq), &wc, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001273 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001274 } else {
1275 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
1276
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001277 this_cpu_inc(*ibp->rvp.rc_delayed_comp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001278 /*
1279 * If send progress not running attempt to progress
1280 * SDMA queue.
1281 */
1282 if (ppd->dd->flags & HFI1_HAS_SEND_DMA) {
1283 struct sdma_engine *engine;
1284 u8 sc5;
1285
1286 /* For now use sc to find engine */
1287 sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl];
1288 engine = qp_to_sdma_engine(qp, sc5);
1289 sdma_engine_progress_schedule(engine);
1290 }
1291 }
1292
1293 qp->s_retry = qp->s_retry_cnt;
1294 update_last_psn(qp, wqe->lpsn);
1295
1296 /*
1297 * If we are completing a request which is in the process of
1298 * being resent, we can stop re-sending it since we know the
1299 * responder has already seen it.
1300 */
1301 if (qp->s_acked == qp->s_cur) {
1302 if (++qp->s_cur >= qp->s_size)
1303 qp->s_cur = 0;
1304 qp->s_acked = qp->s_cur;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001305 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001306 if (qp->s_acked != qp->s_tail) {
1307 qp->s_state = OP(SEND_LAST);
1308 qp->s_psn = wqe->psn;
1309 }
1310 } else {
1311 if (++qp->s_acked >= qp->s_size)
1312 qp->s_acked = 0;
1313 if (qp->state == IB_QPS_SQD && qp->s_acked == qp->s_cur)
1314 qp->s_draining = 0;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001315 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001316 }
1317 return wqe;
1318}
1319
1320/**
1321 * do_rc_ack - process an incoming RC ACK
1322 * @qp: the QP the ACK came in on
1323 * @psn: the packet sequence number of the ACK
1324 * @opcode: the opcode of the request that resulted in the ACK
1325 *
1326 * This is called from rc_rcv_resp() to process an incoming RC ACK
1327 * for the given QP.
Dean Luickb77d7132015-10-26 10:28:43 -04001328 * May be called at interrupt level, with the QP s_lock held.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001329 * Returns 1 if OK, 0 if current operation should be aborted (NAK).
1330 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001331static int do_rc_ack(struct rvt_qp *qp, u32 aeth, u32 psn, int opcode,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001332 u64 val, struct hfi1_ctxtdata *rcd)
1333{
1334 struct hfi1_ibport *ibp;
1335 enum ib_wc_status status;
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001336 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001337 int ret = 0;
1338 u32 ack_psn;
1339 int diff;
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001340 unsigned long to;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001341
Mike Marciniszyn77241052015-07-30 15:17:43 -04001342 /*
1343 * Note that NAKs implicitly ACK outstanding SEND and RDMA write
1344 * requests and implicitly NAK RDMA read and atomic requests issued
1345 * before the NAK'ed request. The MSN won't include the NAK'ed
1346 * request but will include an ACK'ed request(s).
1347 */
1348 ack_psn = psn;
1349 if (aeth >> 29)
1350 ack_psn--;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001351 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001352 ibp = to_iport(qp->ibqp.device, qp->port_num);
1353
1354 /*
1355 * The MSN might be for a later WQE than the PSN indicates so
1356 * only complete WQEs that the PSN finishes.
1357 */
1358 while ((diff = delta_psn(ack_psn, wqe->lpsn)) >= 0) {
1359 /*
1360 * RDMA_READ_RESPONSE_ONLY is a special case since
1361 * we want to generate completion events for everything
1362 * before the RDMA read, copy the data, then generate
1363 * the completion for the read.
1364 */
1365 if (wqe->wr.opcode == IB_WR_RDMA_READ &&
1366 opcode == OP(RDMA_READ_RESPONSE_ONLY) &&
1367 diff == 0) {
1368 ret = 1;
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001369 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001370 }
1371 /*
1372 * If this request is a RDMA read or atomic, and the ACK is
1373 * for a later operation, this ACK NAKs the RDMA read or
1374 * atomic. In other words, only a RDMA_READ_LAST or ONLY
1375 * can ACK a RDMA read and likewise for atomic ops. Note
1376 * that the NAK case can only happen if relaxed ordering is
1377 * used and requests are sent after an RDMA read or atomic
1378 * is sent but before the response is received.
1379 */
1380 if ((wqe->wr.opcode == IB_WR_RDMA_READ &&
1381 (opcode != OP(RDMA_READ_RESPONSE_LAST) || diff != 0)) ||
1382 ((wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1383 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) &&
1384 (opcode != OP(ATOMIC_ACKNOWLEDGE) || diff != 0))) {
1385 /* Retry this request. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001386 if (!(qp->r_flags & RVT_R_RDMAR_SEQ)) {
1387 qp->r_flags |= RVT_R_RDMAR_SEQ;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001388 restart_rc(qp, qp->s_last_psn + 1, 0);
1389 if (list_empty(&qp->rspwait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001390 qp->r_flags |= RVT_R_RSP_SEND;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001391 atomic_inc(&qp->refcount);
1392 list_add_tail(&qp->rspwait,
1393 &rcd->qp_wait_list);
1394 }
1395 }
1396 /*
1397 * No need to process the ACK/NAK since we are
1398 * restarting an earlier request.
1399 */
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001400 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001401 }
1402 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1403 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
1404 u64 *vaddr = wqe->sg_list[0].vaddr;
1405 *vaddr = val;
1406 }
1407 if (qp->s_num_rd_atomic &&
1408 (wqe->wr.opcode == IB_WR_RDMA_READ ||
1409 wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1410 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)) {
1411 qp->s_num_rd_atomic--;
1412 /* Restart sending task if fence is complete */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001413 if ((qp->s_flags & RVT_S_WAIT_FENCE) &&
Mike Marciniszyn77241052015-07-30 15:17:43 -04001414 !qp->s_num_rd_atomic) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001415 qp->s_flags &= ~(RVT_S_WAIT_FENCE |
1416 RVT_S_WAIT_ACK);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001417 hfi1_schedule_send(qp);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001418 } else if (qp->s_flags & RVT_S_WAIT_RDMAR) {
1419 qp->s_flags &= ~(RVT_S_WAIT_RDMAR |
1420 RVT_S_WAIT_ACK);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001421 hfi1_schedule_send(qp);
1422 }
1423 }
1424 wqe = do_rc_completion(qp, wqe, ibp);
1425 if (qp->s_acked == qp->s_tail)
1426 break;
1427 }
1428
1429 switch (aeth >> 29) {
1430 case 0: /* ACK */
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001431 this_cpu_inc(*ibp->rvp.rc_acks);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001432 if (qp->s_acked != qp->s_tail) {
1433 /*
1434 * We are expecting more ACKs so
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001435 * mod the retry timer.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001436 */
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001437 hfi1_mod_retry_timer(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001438 /*
1439 * We can stop re-sending the earlier packets and
1440 * continue with the next packet the receiver wants.
1441 */
1442 if (cmp_psn(qp->s_psn, psn) <= 0)
1443 reset_psn(qp, psn + 1);
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001444 } else {
1445 /* No more acks - kill all timers */
1446 hfi1_stop_rc_timers(qp);
1447 if (cmp_psn(qp->s_psn, psn) <= 0) {
1448 qp->s_state = OP(SEND_LAST);
1449 qp->s_psn = psn + 1;
1450 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001451 }
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001452 if (qp->s_flags & RVT_S_WAIT_ACK) {
1453 qp->s_flags &= ~RVT_S_WAIT_ACK;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001454 hfi1_schedule_send(qp);
1455 }
1456 hfi1_get_credit(qp, aeth);
1457 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1458 qp->s_retry = qp->s_retry_cnt;
1459 update_last_psn(qp, psn);
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001460 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001461
1462 case 1: /* RNR NAK */
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001463 ibp->rvp.n_rnr_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001464 if (qp->s_acked == qp->s_tail)
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001465 goto bail_stop;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001466 if (qp->s_flags & RVT_S_WAIT_RNR)
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001467 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001468 if (qp->s_rnr_retry == 0) {
1469 status = IB_WC_RNR_RETRY_EXC_ERR;
1470 goto class_b;
1471 }
1472 if (qp->s_rnr_retry_cnt < 7)
1473 qp->s_rnr_retry--;
1474
1475 /* The last valid PSN is the previous PSN. */
1476 update_last_psn(qp, psn - 1);
1477
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001478 ibp->rvp.n_rc_resends += delta_psn(qp->s_psn, psn);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001479
1480 reset_psn(qp, psn);
1481
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001482 qp->s_flags &= ~(RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_ACK);
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001483 hfi1_stop_rc_timers(qp);
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001484 to =
Mike Marciniszyn77241052015-07-30 15:17:43 -04001485 ib_hfi1_rnr_table[(aeth >> HFI1_AETH_CREDIT_SHIFT) &
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001486 HFI1_AETH_CREDIT_MASK];
1487 hfi1_add_rnr_timer(qp, to);
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001488 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001489
1490 case 3: /* NAK */
1491 if (qp->s_acked == qp->s_tail)
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001492 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001493 /* The last valid PSN is the previous PSN. */
1494 update_last_psn(qp, psn - 1);
1495 switch ((aeth >> HFI1_AETH_CREDIT_SHIFT) &
1496 HFI1_AETH_CREDIT_MASK) {
1497 case 0: /* PSN sequence error */
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001498 ibp->rvp.n_seq_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001499 /*
1500 * Back up to the responder's expected PSN.
1501 * Note that we might get a NAK in the middle of an
1502 * RDMA READ response which terminates the RDMA
1503 * READ.
1504 */
1505 restart_rc(qp, psn, 0);
1506 hfi1_schedule_send(qp);
1507 break;
1508
1509 case 1: /* Invalid Request */
1510 status = IB_WC_REM_INV_REQ_ERR;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001511 ibp->rvp.n_other_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001512 goto class_b;
1513
1514 case 2: /* Remote Access Error */
1515 status = IB_WC_REM_ACCESS_ERR;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001516 ibp->rvp.n_other_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001517 goto class_b;
1518
1519 case 3: /* Remote Operation Error */
1520 status = IB_WC_REM_OP_ERR;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001521 ibp->rvp.n_other_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001522class_b:
1523 if (qp->s_last == qp->s_acked) {
1524 hfi1_send_complete(qp, wqe, status);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08001525 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001526 }
1527 break;
1528
1529 default:
1530 /* Ignore other reserved NAK error codes */
1531 goto reserved;
1532 }
1533 qp->s_retry = qp->s_retry_cnt;
1534 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001535 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001536
1537 default: /* 2: reserved */
1538reserved:
1539 /* Ignore reserved NAK codes. */
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001540 goto bail_stop;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001541 }
Mike Marciniszyn87717f02016-04-12 11:28:56 -07001542 /* cannot be reached */
Mike Marciniszyn633d2732016-02-04 10:59:18 -08001543bail_stop:
1544 hfi1_stop_rc_timers(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001545 return ret;
1546}
1547
1548/*
1549 * We have seen an out of sequence RDMA read middle or last packet.
1550 * This ACKs SENDs and RDMA writes up to the first RDMA read or atomic SWQE.
1551 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001552static void rdma_seq_err(struct rvt_qp *qp, struct hfi1_ibport *ibp, u32 psn,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001553 struct hfi1_ctxtdata *rcd)
1554{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001555 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001556
1557 /* Remove QP from retry timer */
Mike Marciniszyne6f8c2b2016-02-04 10:59:09 -08001558 hfi1_stop_rc_timers(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001559
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001560 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001561
1562 while (cmp_psn(psn, wqe->lpsn) > 0) {
1563 if (wqe->wr.opcode == IB_WR_RDMA_READ ||
1564 wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1565 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
1566 break;
1567 wqe = do_rc_completion(qp, wqe, ibp);
1568 }
1569
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001570 ibp->rvp.n_rdma_seq++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001571 qp->r_flags |= RVT_R_RDMAR_SEQ;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001572 restart_rc(qp, qp->s_last_psn + 1, 0);
1573 if (list_empty(&qp->rspwait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001574 qp->r_flags |= RVT_R_RSP_SEND;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001575 atomic_inc(&qp->refcount);
1576 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1577 }
1578}
1579
1580/**
1581 * rc_rcv_resp - process an incoming RC response packet
1582 * @ibp: the port this packet came in on
1583 * @ohdr: the other headers for this packet
1584 * @data: the packet data
1585 * @tlen: the packet length
1586 * @qp: the QP for this packet
1587 * @opcode: the opcode for this packet
1588 * @psn: the packet sequence number for this packet
1589 * @hdrsize: the header length
1590 * @pmtu: the path MTU
1591 *
1592 * This is called from hfi1_rc_rcv() to process an incoming RC response
1593 * packet for the given QP.
1594 * Called at interrupt level.
1595 */
1596static void rc_rcv_resp(struct hfi1_ibport *ibp,
1597 struct hfi1_other_headers *ohdr,
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001598 void *data, u32 tlen, struct rvt_qp *qp,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001599 u32 opcode, u32 psn, u32 hdrsize, u32 pmtu,
1600 struct hfi1_ctxtdata *rcd)
1601{
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001602 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001603 enum ib_wc_status status;
1604 unsigned long flags;
1605 int diff;
1606 u32 pad;
1607 u32 aeth;
1608 u64 val;
1609
1610 spin_lock_irqsave(&qp->s_lock, flags);
1611
Sebastian Sanchez462b6b22016-07-01 16:01:06 -07001612 trace_hfi1_ack(qp, psn);
Mike Marciniszyn83525b62015-10-26 10:28:48 -04001613
Mike Marciniszyn77241052015-07-30 15:17:43 -04001614 /* Ignore invalid responses. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001615 smp_read_barrier_depends(); /* see post_one_send */
1616 if (cmp_psn(psn, ACCESS_ONCE(qp->s_next_psn)) >= 0)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001617 goto ack_done;
1618
1619 /* Ignore duplicate responses. */
1620 diff = cmp_psn(psn, qp->s_last_psn);
1621 if (unlikely(diff <= 0)) {
1622 /* Update credits for "ghost" ACKs */
1623 if (diff == 0 && opcode == OP(ACKNOWLEDGE)) {
1624 aeth = be32_to_cpu(ohdr->u.aeth);
1625 if ((aeth >> 29) == 0)
1626 hfi1_get_credit(qp, aeth);
1627 }
1628 goto ack_done;
1629 }
1630
1631 /*
1632 * Skip everything other than the PSN we expect, if we are waiting
1633 * for a reply to a restarted RDMA read or atomic op.
1634 */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001635 if (qp->r_flags & RVT_R_RDMAR_SEQ) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001636 if (cmp_psn(psn, qp->s_last_psn + 1) != 0)
1637 goto ack_done;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001638 qp->r_flags &= ~RVT_R_RDMAR_SEQ;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001639 }
1640
1641 if (unlikely(qp->s_acked == qp->s_tail))
1642 goto ack_done;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001643 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001644 status = IB_WC_SUCCESS;
1645
1646 switch (opcode) {
1647 case OP(ACKNOWLEDGE):
1648 case OP(ATOMIC_ACKNOWLEDGE):
1649 case OP(RDMA_READ_RESPONSE_FIRST):
1650 aeth = be32_to_cpu(ohdr->u.aeth);
1651 if (opcode == OP(ATOMIC_ACKNOWLEDGE)) {
1652 __be32 *p = ohdr->u.at.atomic_ack_eth;
1653
Jubin John50e5dcb2016-02-14 20:19:41 -08001654 val = ((u64)be32_to_cpu(p[0]) << 32) |
Mike Marciniszyn77241052015-07-30 15:17:43 -04001655 be32_to_cpu(p[1]);
Jubin Johne4909742016-02-14 20:22:00 -08001656 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001657 val = 0;
Jubin Johne4909742016-02-14 20:22:00 -08001658 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001659 if (!do_rc_ack(qp, aeth, psn, opcode, val, rcd) ||
1660 opcode != OP(RDMA_READ_RESPONSE_FIRST))
1661 goto ack_done;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001662 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001663 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1664 goto ack_op_err;
1665 /*
1666 * If this is a response to a resent RDMA read, we
1667 * have to be careful to copy the data to the right
1668 * location.
1669 */
1670 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1671 wqe, psn, pmtu);
1672 goto read_middle;
1673
1674 case OP(RDMA_READ_RESPONSE_MIDDLE):
1675 /* no AETH, no ACK */
1676 if (unlikely(cmp_psn(psn, qp->s_last_psn + 1)))
1677 goto ack_seq_err;
1678 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1679 goto ack_op_err;
1680read_middle:
1681 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1682 goto ack_len_err;
1683 if (unlikely(pmtu >= qp->s_rdma_read_len))
1684 goto ack_len_err;
1685
1686 /*
1687 * We got a response so update the timeout.
1688 * 4.096 usec. * (1 << qp->timeout)
1689 */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001690 qp->s_flags |= RVT_S_TIMER;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001691 mod_timer(&qp->s_timer, jiffies + qp->timeout_jiffies);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001692 if (qp->s_flags & RVT_S_WAIT_ACK) {
1693 qp->s_flags &= ~RVT_S_WAIT_ACK;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001694 hfi1_schedule_send(qp);
1695 }
1696
1697 if (opcode == OP(RDMA_READ_RESPONSE_MIDDLE))
1698 qp->s_retry = qp->s_retry_cnt;
1699
1700 /*
1701 * Update the RDMA receive state but do the copy w/o
1702 * holding the locks and blocking interrupts.
1703 */
1704 qp->s_rdma_read_len -= pmtu;
1705 update_last_psn(qp, psn);
1706 spin_unlock_irqrestore(&qp->s_lock, flags);
Dean Luick7b0b01a2016-02-03 14:35:49 -08001707 hfi1_copy_sge(&qp->s_rdma_read_sge, data, pmtu, 0, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001708 goto bail;
1709
1710 case OP(RDMA_READ_RESPONSE_ONLY):
1711 aeth = be32_to_cpu(ohdr->u.aeth);
1712 if (!do_rc_ack(qp, aeth, psn, opcode, 0, rcd))
1713 goto ack_done;
1714 /* Get the number of bytes the message was padded by. */
1715 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1716 /*
1717 * Check that the data size is >= 0 && <= pmtu.
1718 * Remember to account for ICRC (4).
1719 */
1720 if (unlikely(tlen < (hdrsize + pad + 4)))
1721 goto ack_len_err;
1722 /*
1723 * If this is a response to a resent RDMA read, we
1724 * have to be careful to copy the data to the right
1725 * location.
1726 */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001727 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001728 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1729 wqe, psn, pmtu);
1730 goto read_last;
1731
1732 case OP(RDMA_READ_RESPONSE_LAST):
1733 /* ACKs READ req. */
1734 if (unlikely(cmp_psn(psn, qp->s_last_psn + 1)))
1735 goto ack_seq_err;
1736 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1737 goto ack_op_err;
1738 /* Get the number of bytes the message was padded by. */
1739 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1740 /*
1741 * Check that the data size is >= 1 && <= pmtu.
1742 * Remember to account for ICRC (4).
1743 */
1744 if (unlikely(tlen <= (hdrsize + pad + 4)))
1745 goto ack_len_err;
1746read_last:
1747 tlen -= hdrsize + pad + 4;
1748 if (unlikely(tlen != qp->s_rdma_read_len))
1749 goto ack_len_err;
1750 aeth = be32_to_cpu(ohdr->u.aeth);
Dean Luick7b0b01a2016-02-03 14:35:49 -08001751 hfi1_copy_sge(&qp->s_rdma_read_sge, data, tlen, 0, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001752 WARN_ON(qp->s_rdma_read_sge.num_sge);
Jubin John50e5dcb2016-02-14 20:19:41 -08001753 (void)do_rc_ack(qp, aeth, psn,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001754 OP(RDMA_READ_RESPONSE_LAST), 0, rcd);
1755 goto ack_done;
1756 }
1757
1758ack_op_err:
1759 status = IB_WC_LOC_QP_OP_ERR;
1760 goto ack_err;
1761
1762ack_seq_err:
1763 rdma_seq_err(qp, ibp, psn, rcd);
1764 goto ack_done;
1765
1766ack_len_err:
1767 status = IB_WC_LOC_LEN_ERR;
1768ack_err:
1769 if (qp->s_last == qp->s_acked) {
1770 hfi1_send_complete(qp, wqe, status);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08001771 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001772 }
1773ack_done:
1774 spin_unlock_irqrestore(&qp->s_lock, flags);
1775bail:
1776 return;
1777}
1778
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05001779static inline void rc_defered_ack(struct hfi1_ctxtdata *rcd,
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001780 struct rvt_qp *qp)
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05001781{
1782 if (list_empty(&qp->rspwait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001783 qp->r_flags |= RVT_R_RSP_NAK;
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05001784 atomic_inc(&qp->refcount);
1785 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1786 }
1787}
1788
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001789static inline void rc_cancel_ack(struct rvt_qp *qp)
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05001790{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08001791 struct hfi1_qp_priv *priv = qp->priv;
1792
1793 priv->r_adefered = 0;
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05001794 if (list_empty(&qp->rspwait))
1795 return;
1796 list_del_init(&qp->rspwait);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001797 qp->r_flags &= ~RVT_R_RSP_NAK;
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05001798 if (atomic_dec_and_test(&qp->refcount))
1799 wake_up(&qp->wait);
1800}
1801
Mike Marciniszyn77241052015-07-30 15:17:43 -04001802/**
1803 * rc_rcv_error - process an incoming duplicate or error RC packet
1804 * @ohdr: the other headers for this packet
1805 * @data: the packet data
1806 * @qp: the QP for this packet
1807 * @opcode: the opcode for this packet
1808 * @psn: the packet sequence number for this packet
1809 * @diff: the difference between the PSN and the expected PSN
1810 *
1811 * This is called from hfi1_rc_rcv() to process an unexpected
1812 * incoming RC packet for the given QP.
1813 * Called at interrupt level.
1814 * Return 1 if no more processing is needed; otherwise return 0 to
1815 * schedule a response to be sent.
1816 */
1817static noinline int rc_rcv_error(struct hfi1_other_headers *ohdr, void *data,
Jubin John17fb4f22016-02-14 20:21:52 -08001818 struct rvt_qp *qp, u32 opcode, u32 psn,
1819 int diff, struct hfi1_ctxtdata *rcd)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001820{
1821 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001822 struct rvt_ack_entry *e;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001823 unsigned long flags;
1824 u8 i, prev;
1825 int old_req;
1826
Sebastian Sanchez462b6b22016-07-01 16:01:06 -07001827 trace_hfi1_rcv_error(qp, psn);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001828 if (diff > 0) {
1829 /*
1830 * Packet sequence error.
1831 * A NAK will ACK earlier sends and RDMA writes.
1832 * Don't queue the NAK if we already sent one.
1833 */
1834 if (!qp->r_nak_state) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001835 ibp->rvp.n_rc_seqnak++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001836 qp->r_nak_state = IB_NAK_PSN_ERROR;
1837 /* Use the expected PSN. */
1838 qp->r_ack_psn = qp->r_psn;
1839 /*
1840 * Wait to send the sequence NAK until all packets
1841 * in the receive queue have been processed.
1842 * Otherwise, we end up propagating congestion.
1843 */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05001844 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001845 }
1846 goto done;
1847 }
1848
1849 /*
1850 * Handle a duplicate request. Don't re-execute SEND, RDMA
1851 * write or atomic op. Don't NAK errors, just silently drop
1852 * the duplicate request. Note that r_sge, r_len, and
1853 * r_rcv_len may be in use so don't modify them.
1854 *
1855 * We are supposed to ACK the earliest duplicate PSN but we
1856 * can coalesce an outstanding duplicate ACK. We have to
1857 * send the earliest so that RDMA reads can be restarted at
1858 * the requester's expected PSN.
1859 *
1860 * First, find where this duplicate PSN falls within the
1861 * ACKs previously sent.
1862 * old_req is true if there is an older response that is scheduled
1863 * to be sent before sending this one.
1864 */
1865 e = NULL;
1866 old_req = 1;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08001867 ibp->rvp.n_rc_dupreq++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001868
1869 spin_lock_irqsave(&qp->s_lock, flags);
1870
1871 for (i = qp->r_head_ack_queue; ; i = prev) {
1872 if (i == qp->s_tail_ack_queue)
1873 old_req = 0;
1874 if (i)
1875 prev = i - 1;
1876 else
1877 prev = HFI1_MAX_RDMA_ATOMIC;
1878 if (prev == qp->r_head_ack_queue) {
1879 e = NULL;
1880 break;
1881 }
1882 e = &qp->s_ack_queue[prev];
1883 if (!e->opcode) {
1884 e = NULL;
1885 break;
1886 }
1887 if (cmp_psn(psn, e->psn) >= 0) {
1888 if (prev == qp->s_tail_ack_queue &&
1889 cmp_psn(psn, e->lpsn) <= 0)
1890 old_req = 0;
1891 break;
1892 }
1893 }
1894 switch (opcode) {
1895 case OP(RDMA_READ_REQUEST): {
1896 struct ib_reth *reth;
1897 u32 offset;
1898 u32 len;
1899
1900 /*
1901 * If we didn't find the RDMA read request in the ack queue,
1902 * we can ignore this request.
1903 */
1904 if (!e || e->opcode != OP(RDMA_READ_REQUEST))
1905 goto unlock_done;
1906 /* RETH comes after BTH */
1907 reth = &ohdr->u.rc.reth;
1908 /*
1909 * Address range must be a subset of the original
1910 * request and start on pmtu boundaries.
1911 * We reuse the old ack_queue slot since the requester
1912 * should not back up and request an earlier PSN for the
1913 * same request.
1914 */
1915 offset = delta_psn(psn, e->psn) * qp->pmtu;
1916 len = be32_to_cpu(reth->length);
1917 if (unlikely(offset + len != e->rdma_sge.sge_length))
1918 goto unlock_done;
1919 if (e->rdma_sge.mr) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001920 rvt_put_mr(e->rdma_sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001921 e->rdma_sge.mr = NULL;
1922 }
1923 if (len != 0) {
1924 u32 rkey = be32_to_cpu(reth->rkey);
1925 u64 vaddr = be64_to_cpu(reth->vaddr);
1926 int ok;
1927
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001928 ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr, rkey,
1929 IB_ACCESS_REMOTE_READ);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001930 if (unlikely(!ok))
1931 goto unlock_done;
1932 } else {
1933 e->rdma_sge.vaddr = NULL;
1934 e->rdma_sge.length = 0;
1935 e->rdma_sge.sge_length = 0;
1936 }
1937 e->psn = psn;
1938 if (old_req)
1939 goto unlock_done;
1940 qp->s_tail_ack_queue = prev;
1941 break;
1942 }
1943
1944 case OP(COMPARE_SWAP):
1945 case OP(FETCH_ADD): {
1946 /*
1947 * If we didn't find the atomic request in the ack queue
1948 * or the send tasklet is already backed up to send an
1949 * earlier entry, we can ignore this request.
1950 */
Jubin John50e5dcb2016-02-14 20:19:41 -08001951 if (!e || e->opcode != (u8)opcode || old_req)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001952 goto unlock_done;
1953 qp->s_tail_ack_queue = prev;
1954 break;
1955 }
1956
1957 default:
1958 /*
1959 * Ignore this operation if it doesn't request an ACK
1960 * or an earlier RDMA read or atomic is going to be resent.
1961 */
1962 if (!(psn & IB_BTH_REQ_ACK) || old_req)
1963 goto unlock_done;
1964 /*
1965 * Resend the most recent ACK if this request is
1966 * after all the previous RDMA reads and atomics.
1967 */
1968 if (i == qp->r_head_ack_queue) {
1969 spin_unlock_irqrestore(&qp->s_lock, flags);
1970 qp->r_nak_state = 0;
1971 qp->r_ack_psn = qp->r_psn - 1;
1972 goto send_ack;
1973 }
1974
1975 /*
1976 * Resend the RDMA read or atomic op which
1977 * ACKs this duplicate request.
1978 */
1979 qp->s_tail_ack_queue = i;
1980 break;
1981 }
1982 qp->s_ack_state = OP(ACKNOWLEDGE);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001983 qp->s_flags |= RVT_S_RESP_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001984 qp->r_nak_state = 0;
1985 hfi1_schedule_send(qp);
1986
1987unlock_done:
1988 spin_unlock_irqrestore(&qp->s_lock, flags);
1989done:
1990 return 1;
1991
1992send_ack:
1993 return 0;
1994}
1995
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001996void hfi1_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001997{
1998 unsigned long flags;
1999 int lastwqe;
2000
2001 spin_lock_irqsave(&qp->s_lock, flags);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002002 lastwqe = rvt_error_qp(qp, err);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002003 spin_unlock_irqrestore(&qp->s_lock, flags);
2004
2005 if (lastwqe) {
2006 struct ib_event ev;
2007
2008 ev.device = qp->ibqp.device;
2009 ev.element.qp = &qp->ibqp;
2010 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
2011 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
2012 }
2013}
2014
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002015static inline void update_ack_queue(struct rvt_qp *qp, unsigned n)
Mike Marciniszyn77241052015-07-30 15:17:43 -04002016{
2017 unsigned next;
2018
2019 next = n + 1;
2020 if (next > HFI1_MAX_RDMA_ATOMIC)
2021 next = 0;
2022 qp->s_tail_ack_queue = next;
2023 qp->s_ack_state = OP(ACKNOWLEDGE);
2024}
2025
2026static void log_cca_event(struct hfi1_pportdata *ppd, u8 sl, u32 rlid,
2027 u32 lqpn, u32 rqpn, u8 svc_type)
2028{
2029 struct opa_hfi1_cong_log_event_internal *cc_event;
Dean Luickb77d7132015-10-26 10:28:43 -04002030 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002031
2032 if (sl >= OPA_MAX_SLS)
2033 return;
2034
Dean Luickb77d7132015-10-26 10:28:43 -04002035 spin_lock_irqsave(&ppd->cc_log_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002036
Jubin John8638b772016-02-14 20:19:24 -08002037 ppd->threshold_cong_event_map[sl / 8] |= 1 << (sl % 8);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002038 ppd->threshold_event_counter++;
2039
2040 cc_event = &ppd->cc_events[ppd->cc_log_idx++];
2041 if (ppd->cc_log_idx == OPA_CONG_LOG_ELEMS)
2042 ppd->cc_log_idx = 0;
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002043 cc_event->lqpn = lqpn & RVT_QPN_MASK;
2044 cc_event->rqpn = rqpn & RVT_QPN_MASK;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002045 cc_event->sl = sl;
2046 cc_event->svc_type = svc_type;
2047 cc_event->rlid = rlid;
2048 /* keep timestamp in units of 1.024 usec */
2049 cc_event->timestamp = ktime_to_ns(ktime_get()) / 1024;
2050
Dean Luickb77d7132015-10-26 10:28:43 -04002051 spin_unlock_irqrestore(&ppd->cc_log_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002052}
2053
2054void process_becn(struct hfi1_pportdata *ppd, u8 sl, u16 rlid, u32 lqpn,
2055 u32 rqpn, u8 svc_type)
2056{
2057 struct cca_timer *cca_timer;
2058 u16 ccti, ccti_incr, ccti_timer, ccti_limit;
2059 u8 trigger_threshold;
2060 struct cc_state *cc_state;
Dean Luickb77d7132015-10-26 10:28:43 -04002061 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002062
2063 if (sl >= OPA_MAX_SLS)
2064 return;
2065
Mike Marciniszyn77241052015-07-30 15:17:43 -04002066 cc_state = get_cc_state(ppd);
2067
Jubin Johnd125a6c2016-02-14 20:19:49 -08002068 if (!cc_state)
Mike Marciniszyn77241052015-07-30 15:17:43 -04002069 return;
2070
2071 /*
2072 * 1) increase CCTI (for this SL)
2073 * 2) select IPG (i.e., call set_link_ipg())
2074 * 3) start timer
2075 */
2076 ccti_limit = cc_state->cct.ccti_limit;
2077 ccti_incr = cc_state->cong_setting.entries[sl].ccti_increase;
2078 ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer;
2079 trigger_threshold =
2080 cc_state->cong_setting.entries[sl].trigger_threshold;
2081
Dean Luickb77d7132015-10-26 10:28:43 -04002082 spin_lock_irqsave(&ppd->cca_timer_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002083
Jubin Johnd35cf742016-04-14 08:31:53 -07002084 cca_timer = &ppd->cca_timer[sl];
Mike Marciniszyn77241052015-07-30 15:17:43 -04002085 if (cca_timer->ccti < ccti_limit) {
2086 if (cca_timer->ccti + ccti_incr <= ccti_limit)
2087 cca_timer->ccti += ccti_incr;
2088 else
2089 cca_timer->ccti = ccti_limit;
2090 set_link_ipg(ppd);
2091 }
2092
Mike Marciniszyn77241052015-07-30 15:17:43 -04002093 ccti = cca_timer->ccti;
2094
2095 if (!hrtimer_active(&cca_timer->hrtimer)) {
2096 /* ccti_timer is in units of 1.024 usec */
2097 unsigned long nsec = 1024 * ccti_timer;
2098
2099 hrtimer_start(&cca_timer->hrtimer, ns_to_ktime(nsec),
2100 HRTIMER_MODE_REL);
2101 }
2102
Jubin Johnd35cf742016-04-14 08:31:53 -07002103 spin_unlock_irqrestore(&ppd->cca_timer_lock, flags);
2104
Mike Marciniszyn77241052015-07-30 15:17:43 -04002105 if ((trigger_threshold != 0) && (ccti >= trigger_threshold))
2106 log_cca_event(ppd, sl, rlid, lqpn, rqpn, svc_type);
2107}
2108
2109/**
2110 * hfi1_rc_rcv - process an incoming RC packet
2111 * @rcd: the context pointer
2112 * @hdr: the header of this packet
2113 * @rcv_flags: flags relevant to rcv processing
2114 * @data: the packet data
2115 * @tlen: the packet length
2116 * @qp: the QP for this packet
2117 *
2118 * This is called from qp_rcv() to process an incoming RC packet
2119 * for the given QP.
Dean Luickb77d7132015-10-26 10:28:43 -04002120 * May be called at interrupt level.
Mike Marciniszyn77241052015-07-30 15:17:43 -04002121 */
2122void hfi1_rc_rcv(struct hfi1_packet *packet)
2123{
2124 struct hfi1_ctxtdata *rcd = packet->rcd;
2125 struct hfi1_ib_header *hdr = packet->hdr;
2126 u32 rcv_flags = packet->rcv_flags;
2127 void *data = packet->ebuf;
2128 u32 tlen = packet->tlen;
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002129 struct rvt_qp *qp = packet->qp;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002130 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002131 struct hfi1_other_headers *ohdr = packet->ohdr;
2132 u32 bth0, opcode;
2133 u32 hdrsize = packet->hlen;
2134 u32 psn;
2135 u32 pad;
2136 struct ib_wc wc;
2137 u32 pmtu = qp->pmtu;
2138 int diff;
2139 struct ib_reth *reth;
2140 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002141 int ret, is_fecn = 0;
Dean Luick7b0b01a2016-02-03 14:35:49 -08002142 int copy_last = 0;
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002143 u32 rkey;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002144
2145 bth0 = be32_to_cpu(ohdr->bth[0]);
2146 if (hfi1_ruc_check_hdr(ibp, hdr, rcv_flags & HFI1_HAS_GRH, qp, bth0))
2147 return;
2148
Mitko Haralanov5fd2b562016-07-25 13:38:07 -07002149 is_fecn = process_ecn(qp, packet, false);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002150
2151 psn = be32_to_cpu(ohdr->bth[2]);
Arthur Kepner977940b2015-11-04 21:10:10 -05002152 opcode = (bth0 >> 24) & 0xff;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002153
2154 /*
2155 * Process responses (ACKs) before anything else. Note that the
2156 * packet sequence number will be for something in the send work
2157 * queue rather than the expected receive packet sequence number.
2158 * In other words, this QP is the requester.
2159 */
2160 if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
2161 opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
2162 rc_rcv_resp(ibp, ohdr, data, tlen, qp, opcode, psn,
2163 hdrsize, pmtu, rcd);
2164 if (is_fecn)
2165 goto send_ack;
2166 return;
2167 }
2168
2169 /* Compute 24 bits worth of difference. */
2170 diff = delta_psn(psn, qp->r_psn);
2171 if (unlikely(diff)) {
2172 if (rc_rcv_error(ohdr, data, qp, opcode, psn, diff, rcd))
2173 return;
2174 goto send_ack;
2175 }
2176
2177 /* Check for opcode sequence errors. */
2178 switch (qp->r_state) {
2179 case OP(SEND_FIRST):
2180 case OP(SEND_MIDDLE):
2181 if (opcode == OP(SEND_MIDDLE) ||
2182 opcode == OP(SEND_LAST) ||
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002183 opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
2184 opcode == OP(SEND_LAST_WITH_INVALIDATE))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002185 break;
2186 goto nack_inv;
2187
2188 case OP(RDMA_WRITE_FIRST):
2189 case OP(RDMA_WRITE_MIDDLE):
2190 if (opcode == OP(RDMA_WRITE_MIDDLE) ||
2191 opcode == OP(RDMA_WRITE_LAST) ||
2192 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
2193 break;
2194 goto nack_inv;
2195
2196 default:
2197 if (opcode == OP(SEND_MIDDLE) ||
2198 opcode == OP(SEND_LAST) ||
2199 opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002200 opcode == OP(SEND_LAST_WITH_INVALIDATE) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04002201 opcode == OP(RDMA_WRITE_MIDDLE) ||
2202 opcode == OP(RDMA_WRITE_LAST) ||
2203 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
2204 goto nack_inv;
2205 /*
2206 * Note that it is up to the requester to not send a new
2207 * RDMA read or atomic operation before receiving an ACK
2208 * for the previous operation.
2209 */
2210 break;
2211 }
2212
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08002213 if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002214 qp_comm_est(qp);
2215
2216 /* OK, process the packet. */
2217 switch (opcode) {
2218 case OP(SEND_FIRST):
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002219 ret = hfi1_rvt_get_rwqe(qp, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002220 if (ret < 0)
2221 goto nack_op_err;
2222 if (!ret)
2223 goto rnr_nak;
2224 qp->r_rcv_len = 0;
2225 /* FALLTHROUGH */
2226 case OP(SEND_MIDDLE):
2227 case OP(RDMA_WRITE_MIDDLE):
2228send_middle:
2229 /* Check for invalid length PMTU or posted rwqe len. */
2230 if (unlikely(tlen != (hdrsize + pmtu + 4)))
2231 goto nack_inv;
2232 qp->r_rcv_len += pmtu;
2233 if (unlikely(qp->r_rcv_len > qp->r_len))
2234 goto nack_inv;
Dean Luick7b0b01a2016-02-03 14:35:49 -08002235 hfi1_copy_sge(&qp->r_sge, data, pmtu, 1, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002236 break;
2237
2238 case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
2239 /* consume RWQE */
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002240 ret = hfi1_rvt_get_rwqe(qp, 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002241 if (ret < 0)
2242 goto nack_op_err;
2243 if (!ret)
2244 goto rnr_nak;
2245 goto send_last_imm;
2246
2247 case OP(SEND_ONLY):
2248 case OP(SEND_ONLY_WITH_IMMEDIATE):
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002249 case OP(SEND_ONLY_WITH_INVALIDATE):
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002250 ret = hfi1_rvt_get_rwqe(qp, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002251 if (ret < 0)
2252 goto nack_op_err;
2253 if (!ret)
2254 goto rnr_nak;
2255 qp->r_rcv_len = 0;
2256 if (opcode == OP(SEND_ONLY))
2257 goto no_immediate_data;
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002258 if (opcode == OP(SEND_ONLY_WITH_INVALIDATE))
2259 goto send_last_inv;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002260 /* FALLTHROUGH for SEND_ONLY_WITH_IMMEDIATE */
2261 case OP(SEND_LAST_WITH_IMMEDIATE):
2262send_last_imm:
2263 wc.ex.imm_data = ohdr->u.imm_data;
2264 wc.wc_flags = IB_WC_WITH_IMM;
2265 goto send_last;
Jianxin Xionga2df0c82016-07-25 13:38:31 -07002266 case OP(SEND_LAST_WITH_INVALIDATE):
2267send_last_inv:
2268 rkey = be32_to_cpu(ohdr->u.ieth);
2269 if (rvt_invalidate_rkey(qp, rkey))
2270 goto no_immediate_data;
2271 wc.ex.invalidate_rkey = rkey;
2272 wc.wc_flags = IB_WC_WITH_INVALIDATE;
2273 goto send_last;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002274 case OP(RDMA_WRITE_LAST):
Dean Luick7b0b01a2016-02-03 14:35:49 -08002275 copy_last = ibpd_to_rvtpd(qp->ibqp.pd)->user;
2276 /* fall through */
2277 case OP(SEND_LAST):
Mike Marciniszyn77241052015-07-30 15:17:43 -04002278no_immediate_data:
2279 wc.wc_flags = 0;
2280 wc.ex.imm_data = 0;
2281send_last:
2282 /* Get the number of bytes the message was padded by. */
2283 pad = (bth0 >> 20) & 3;
2284 /* Check for invalid length. */
2285 /* LAST len should be >= 1 */
2286 if (unlikely(tlen < (hdrsize + pad + 4)))
2287 goto nack_inv;
2288 /* Don't count the CRC. */
2289 tlen -= (hdrsize + pad + 4);
2290 wc.byte_len = tlen + qp->r_rcv_len;
2291 if (unlikely(wc.byte_len > qp->r_len))
2292 goto nack_inv;
Dean Luick7b0b01a2016-02-03 14:35:49 -08002293 hfi1_copy_sge(&qp->r_sge, data, tlen, 1, copy_last);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002294 rvt_put_ss(&qp->r_sge);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002295 qp->r_msn++;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08002296 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002297 break;
2298 wc.wr_id = qp->r_wr_id;
2299 wc.status = IB_WC_SUCCESS;
2300 if (opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE) ||
2301 opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
2302 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
2303 else
2304 wc.opcode = IB_WC_RECV;
2305 wc.qp = &qp->ibqp;
2306 wc.src_qp = qp->remote_qpn;
2307 wc.slid = qp->remote_ah_attr.dlid;
2308 /*
2309 * It seems that IB mandates the presence of an SL in a
2310 * work completion only for the UD transport (see section
2311 * 11.4.2 of IBTA Vol. 1).
2312 *
2313 * However, the way the SL is chosen below is consistent
2314 * with the way that IB/qib works and is trying avoid
2315 * introducing incompatibilities.
2316 *
2317 * See also OPA Vol. 1, section 9.7.6, and table 9-17.
2318 */
2319 wc.sl = qp->remote_ah_attr.sl;
2320 /* zero fields that are N/A */
2321 wc.vendor_err = 0;
2322 wc.pkey_index = 0;
2323 wc.dlid_path_bits = 0;
2324 wc.port_num = 0;
2325 /* Signal completion event if the solicited bit is set. */
Dennis Dalessandroabd712d2016-01-19 14:43:22 -08002326 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
2327 (bth0 & IB_BTH_SOLICITED) != 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002328 break;
2329
Mike Marciniszyn77241052015-07-30 15:17:43 -04002330 case OP(RDMA_WRITE_ONLY):
Dean Luick7b0b01a2016-02-03 14:35:49 -08002331 copy_last = 1;
2332 /* fall through */
2333 case OP(RDMA_WRITE_FIRST):
Mike Marciniszyn77241052015-07-30 15:17:43 -04002334 case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
2335 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
2336 goto nack_inv;
2337 /* consume RWQE */
2338 reth = &ohdr->u.rc.reth;
2339 qp->r_len = be32_to_cpu(reth->length);
2340 qp->r_rcv_len = 0;
2341 qp->r_sge.sg_list = NULL;
2342 if (qp->r_len != 0) {
2343 u32 rkey = be32_to_cpu(reth->rkey);
2344 u64 vaddr = be64_to_cpu(reth->vaddr);
2345 int ok;
2346
2347 /* Check rkey & NAK */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002348 ok = rvt_rkey_ok(qp, &qp->r_sge.sge, qp->r_len, vaddr,
2349 rkey, IB_ACCESS_REMOTE_WRITE);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002350 if (unlikely(!ok))
2351 goto nack_acc;
2352 qp->r_sge.num_sge = 1;
2353 } else {
2354 qp->r_sge.num_sge = 0;
2355 qp->r_sge.sge.mr = NULL;
2356 qp->r_sge.sge.vaddr = NULL;
2357 qp->r_sge.sge.length = 0;
2358 qp->r_sge.sge.sge_length = 0;
2359 }
2360 if (opcode == OP(RDMA_WRITE_FIRST))
2361 goto send_middle;
2362 else if (opcode == OP(RDMA_WRITE_ONLY))
2363 goto no_immediate_data;
Dennis Dalessandroec4274f2016-01-19 14:43:44 -08002364 ret = hfi1_rvt_get_rwqe(qp, 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002365 if (ret < 0)
2366 goto nack_op_err;
2367 if (!ret)
2368 goto rnr_nak;
2369 wc.ex.imm_data = ohdr->u.rc.imm_data;
2370 wc.wc_flags = IB_WC_WITH_IMM;
2371 goto send_last;
2372
2373 case OP(RDMA_READ_REQUEST): {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002374 struct rvt_ack_entry *e;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002375 u32 len;
2376 u8 next;
2377
2378 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
2379 goto nack_inv;
2380 next = qp->r_head_ack_queue + 1;
2381 /* s_ack_queue is size HFI1_MAX_RDMA_ATOMIC+1 so use > not >= */
2382 if (next > HFI1_MAX_RDMA_ATOMIC)
2383 next = 0;
2384 spin_lock_irqsave(&qp->s_lock, flags);
2385 if (unlikely(next == qp->s_tail_ack_queue)) {
2386 if (!qp->s_ack_queue[next].sent)
2387 goto nack_inv_unlck;
2388 update_ack_queue(qp, next);
2389 }
2390 e = &qp->s_ack_queue[qp->r_head_ack_queue];
2391 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002392 rvt_put_mr(e->rdma_sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002393 e->rdma_sge.mr = NULL;
2394 }
2395 reth = &ohdr->u.rc.reth;
2396 len = be32_to_cpu(reth->length);
2397 if (len) {
2398 u32 rkey = be32_to_cpu(reth->rkey);
2399 u64 vaddr = be64_to_cpu(reth->vaddr);
2400 int ok;
2401
2402 /* Check rkey & NAK */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002403 ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr,
2404 rkey, IB_ACCESS_REMOTE_READ);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002405 if (unlikely(!ok))
2406 goto nack_acc_unlck;
2407 /*
2408 * Update the next expected PSN. We add 1 later
2409 * below, so only add the remainder here.
2410 */
2411 if (len > pmtu)
2412 qp->r_psn += (len - 1) / pmtu;
2413 } else {
2414 e->rdma_sge.mr = NULL;
2415 e->rdma_sge.vaddr = NULL;
2416 e->rdma_sge.length = 0;
2417 e->rdma_sge.sge_length = 0;
2418 }
2419 e->opcode = opcode;
2420 e->sent = 0;
2421 e->psn = psn;
2422 e->lpsn = qp->r_psn;
2423 /*
2424 * We need to increment the MSN here instead of when we
2425 * finish sending the result since a duplicate request would
2426 * increment it more than once.
2427 */
2428 qp->r_msn++;
2429 qp->r_psn++;
2430 qp->r_state = opcode;
2431 qp->r_nak_state = 0;
2432 qp->r_head_ack_queue = next;
2433
2434 /* Schedule the send tasklet. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08002435 qp->s_flags |= RVT_S_RESP_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002436 hfi1_schedule_send(qp);
2437
2438 spin_unlock_irqrestore(&qp->s_lock, flags);
2439 if (is_fecn)
2440 goto send_ack;
2441 return;
2442 }
2443
2444 case OP(COMPARE_SWAP):
2445 case OP(FETCH_ADD): {
2446 struct ib_atomic_eth *ateth;
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002447 struct rvt_ack_entry *e;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002448 u64 vaddr;
2449 atomic64_t *maddr;
2450 u64 sdata;
2451 u32 rkey;
2452 u8 next;
2453
2454 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
2455 goto nack_inv;
2456 next = qp->r_head_ack_queue + 1;
2457 if (next > HFI1_MAX_RDMA_ATOMIC)
2458 next = 0;
2459 spin_lock_irqsave(&qp->s_lock, flags);
2460 if (unlikely(next == qp->s_tail_ack_queue)) {
2461 if (!qp->s_ack_queue[next].sent)
2462 goto nack_inv_unlck;
2463 update_ack_queue(qp, next);
2464 }
2465 e = &qp->s_ack_queue[qp->r_head_ack_queue];
2466 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002467 rvt_put_mr(e->rdma_sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002468 e->rdma_sge.mr = NULL;
2469 }
2470 ateth = &ohdr->u.atomic_eth;
Jubin John50e5dcb2016-02-14 20:19:41 -08002471 vaddr = ((u64)be32_to_cpu(ateth->vaddr[0]) << 32) |
Mike Marciniszyn77241052015-07-30 15:17:43 -04002472 be32_to_cpu(ateth->vaddr[1]);
2473 if (unlikely(vaddr & (sizeof(u64) - 1)))
2474 goto nack_inv_unlck;
2475 rkey = be32_to_cpu(ateth->rkey);
2476 /* Check rkey & NAK */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002477 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
2478 vaddr, rkey,
2479 IB_ACCESS_REMOTE_ATOMIC)))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002480 goto nack_acc_unlck;
2481 /* Perform atomic OP and save result. */
Jubin John50e5dcb2016-02-14 20:19:41 -08002482 maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002483 sdata = be64_to_cpu(ateth->swap_data);
2484 e->atomic_data = (opcode == OP(FETCH_ADD)) ?
Jubin John50e5dcb2016-02-14 20:19:41 -08002485 (u64)atomic64_add_return(sdata, maddr) - sdata :
2486 (u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
Mike Marciniszyn77241052015-07-30 15:17:43 -04002487 be64_to_cpu(ateth->compare_data),
2488 sdata);
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002489 rvt_put_mr(qp->r_sge.sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002490 qp->r_sge.num_sge = 0;
2491 e->opcode = opcode;
2492 e->sent = 0;
2493 e->psn = psn;
2494 e->lpsn = psn;
2495 qp->r_msn++;
2496 qp->r_psn++;
2497 qp->r_state = opcode;
2498 qp->r_nak_state = 0;
2499 qp->r_head_ack_queue = next;
2500
2501 /* Schedule the send tasklet. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08002502 qp->s_flags |= RVT_S_RESP_PENDING;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002503 hfi1_schedule_send(qp);
2504
2505 spin_unlock_irqrestore(&qp->s_lock, flags);
2506 if (is_fecn)
2507 goto send_ack;
2508 return;
2509 }
2510
2511 default:
2512 /* NAK unknown opcodes. */
2513 goto nack_inv;
2514 }
2515 qp->r_psn++;
2516 qp->r_state = opcode;
2517 qp->r_ack_psn = psn;
2518 qp->r_nak_state = 0;
2519 /* Send an ACK if requested or required. */
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05002520 if (psn & IB_BTH_REQ_ACK) {
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08002521 struct hfi1_qp_priv *priv = qp->priv;
2522
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05002523 if (packet->numpkt == 0) {
2524 rc_cancel_ack(qp);
2525 goto send_ack;
2526 }
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08002527 if (priv->r_adefered >= HFI1_PSN_CREDIT) {
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05002528 rc_cancel_ack(qp);
2529 goto send_ack;
2530 }
2531 if (unlikely(is_fecn)) {
2532 rc_cancel_ack(qp);
2533 goto send_ack;
2534 }
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08002535 priv->r_adefered++;
Mike Marciniszyn7c091e52015-11-10 09:14:01 -05002536 rc_defered_ack(rcd, qp);
2537 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04002538 return;
2539
2540rnr_nak:
Harish Chegondibf640092016-03-05 08:49:29 -08002541 qp->r_nak_state = qp->r_min_rnr_timer | IB_RNR_NAK;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002542 qp->r_ack_psn = qp->r_psn;
2543 /* Queue RNR NAK for later */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05002544 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002545 return;
2546
2547nack_op_err:
2548 hfi1_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
2549 qp->r_nak_state = IB_NAK_REMOTE_OPERATIONAL_ERROR;
2550 qp->r_ack_psn = qp->r_psn;
2551 /* Queue NAK for later */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05002552 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002553 return;
2554
2555nack_inv_unlck:
2556 spin_unlock_irqrestore(&qp->s_lock, flags);
2557nack_inv:
2558 hfi1_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
2559 qp->r_nak_state = IB_NAK_INVALID_REQUEST;
2560 qp->r_ack_psn = qp->r_psn;
2561 /* Queue NAK for later */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05002562 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002563 return;
2564
2565nack_acc_unlck:
2566 spin_unlock_irqrestore(&qp->s_lock, flags);
2567nack_acc:
2568 hfi1_rc_error(qp, IB_WC_LOC_PROT_ERR);
2569 qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
2570 qp->r_ack_psn = qp->r_psn;
2571send_ack:
2572 hfi1_send_rc_ack(rcd, qp, is_fecn);
2573}
2574
2575void hfi1_rc_hdrerr(
2576 struct hfi1_ctxtdata *rcd,
2577 struct hfi1_ib_header *hdr,
2578 u32 rcv_flags,
Dennis Dalessandro895420d2016-01-19 14:42:28 -08002579 struct rvt_qp *qp)
Mike Marciniszyn77241052015-07-30 15:17:43 -04002580{
2581 int has_grh = rcv_flags & HFI1_HAS_GRH;
2582 struct hfi1_other_headers *ohdr;
2583 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
2584 int diff;
Nicolas Iooss49c32032015-09-20 16:07:15 +02002585 u32 opcode;
Arthur Kepner977940b2015-11-04 21:10:10 -05002586 u32 psn, bth0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002587
2588 /* Check for GRH */
2589 ohdr = &hdr->u.oth;
2590 if (has_grh)
2591 ohdr = &hdr->u.l.oth;
2592
Arthur Kepner977940b2015-11-04 21:10:10 -05002593 bth0 = be32_to_cpu(ohdr->bth[0]);
2594 if (hfi1_ruc_check_hdr(ibp, hdr, has_grh, qp, bth0))
Mike Marciniszyn77241052015-07-30 15:17:43 -04002595 return;
2596
2597 psn = be32_to_cpu(ohdr->bth[2]);
Arthur Kepner977940b2015-11-04 21:10:10 -05002598 opcode = (bth0 >> 24) & 0xff;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002599
2600 /* Only deal with RDMA Writes for now */
2601 if (opcode < IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST) {
2602 diff = delta_psn(psn, qp->r_psn);
2603 if (!qp->r_nak_state && diff >= 0) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -08002604 ibp->rvp.n_rc_seqnak++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002605 qp->r_nak_state = IB_NAK_PSN_ERROR;
2606 /* Use the expected PSN. */
2607 qp->r_ack_psn = qp->r_psn;
2608 /*
2609 * Wait to send the sequence
2610 * NAK until all packets
2611 * in the receive queue have
2612 * been processed.
2613 * Otherwise, we end up
2614 * propagating congestion.
2615 */
Mike Marciniszyn2fd36862015-11-10 09:13:55 -05002616 rc_defered_ack(rcd, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002617 } /* Out of sequence NAK */
2618 } /* QP Request NAKs */
2619}