]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/infiniband/hw/hfi1/rc.c
scsi: cxgb4i: libcxgbi: in error case RST tcp conn
[karo-tx-linux.git] / drivers / infiniband / hw / hfi1 / rc.c
1 /*
2  * Copyright(c) 2015, 2016 Intel Corporation.
3  *
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  *
9  * 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  *
20  * 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>
49 #include <rdma/rdma_vt.h>
50 #include <rdma/rdmavt_qp.h>
51
52 #include "hfi.h"
53 #include "qp.h"
54 #include "verbs_txreq.h"
55 #include "trace.h"
56
57 /* cut down ridiculously long IB macro names */
58 #define OP(x) RC_OP(x)
59
60 static u32 restart_sge(struct rvt_sge_state *ss, struct rvt_swqe *wqe,
61                        u32 psn, u32 pmtu)
62 {
63         u32 len;
64
65         len = delta_psn(psn, wqe->psn) * pmtu;
66         ss->sge = wqe->sg_list[0];
67         ss->sg_list = wqe->sg_list + 1;
68         ss->num_sge = wqe->wr.num_sge;
69         ss->total_len = wqe->length;
70         rvt_skip_sge(ss, len, false);
71         return wqe->length - len;
72 }
73
74 /**
75  * make_rc_ack - construct a response packet (ACK, NAK, or RDMA read)
76  * @dev: the device for this QP
77  * @qp: a pointer to the QP
78  * @ohdr: a pointer to the IB header being constructed
79  * @ps: the xmit packet state
80  *
81  * Return 1 if constructed; otherwise, return 0.
82  * Note that we are in the responder's side of the QP context.
83  * Note the QP s_lock must be held.
84  */
85 static int make_rc_ack(struct hfi1_ibdev *dev, struct rvt_qp *qp,
86                        struct ib_other_headers *ohdr,
87                        struct hfi1_pkt_state *ps)
88 {
89         struct rvt_ack_entry *e;
90         u32 hwords;
91         u32 len;
92         u32 bth0;
93         u32 bth2;
94         int middle = 0;
95         u32 pmtu = qp->pmtu;
96         struct hfi1_qp_priv *priv = qp->priv;
97
98         lockdep_assert_held(&qp->s_lock);
99         /* Don't send an ACK if we aren't supposed to. */
100         if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
101                 goto bail;
102
103         /* header size in 32-bit words LRH+BTH = (8+12)/4. */
104         hwords = 5;
105
106         switch (qp->s_ack_state) {
107         case OP(RDMA_READ_RESPONSE_LAST):
108         case OP(RDMA_READ_RESPONSE_ONLY):
109                 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
110                 if (e->rdma_sge.mr) {
111                         rvt_put_mr(e->rdma_sge.mr);
112                         e->rdma_sge.mr = NULL;
113                 }
114                 /* FALLTHROUGH */
115         case OP(ATOMIC_ACKNOWLEDGE):
116                 /*
117                  * We can increment the tail pointer now that the last
118                  * response has been sent instead of only being
119                  * constructed.
120                  */
121                 if (++qp->s_tail_ack_queue > HFI1_MAX_RDMA_ATOMIC)
122                         qp->s_tail_ack_queue = 0;
123                 /* FALLTHROUGH */
124         case OP(SEND_ONLY):
125         case OP(ACKNOWLEDGE):
126                 /* Check for no next entry in the queue. */
127                 if (qp->r_head_ack_queue == qp->s_tail_ack_queue) {
128                         if (qp->s_flags & RVT_S_ACK_PENDING)
129                                 goto normal;
130                         goto bail;
131                 }
132
133                 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
134                 if (e->opcode == OP(RDMA_READ_REQUEST)) {
135                         /*
136                          * If a RDMA read response is being resent and
137                          * we haven't seen the duplicate request yet,
138                          * then stop sending the remaining responses the
139                          * responder has seen until the requester re-sends it.
140                          */
141                         len = e->rdma_sge.sge_length;
142                         if (len && !e->rdma_sge.mr) {
143                                 qp->s_tail_ack_queue = qp->r_head_ack_queue;
144                                 goto bail;
145                         }
146                         /* Copy SGE state in case we need to resend */
147                         ps->s_txreq->mr = e->rdma_sge.mr;
148                         if (ps->s_txreq->mr)
149                                 rvt_get_mr(ps->s_txreq->mr);
150                         qp->s_ack_rdma_sge.sge = e->rdma_sge;
151                         qp->s_ack_rdma_sge.num_sge = 1;
152                         ps->s_txreq->ss = &qp->s_ack_rdma_sge;
153                         if (len > pmtu) {
154                                 len = pmtu;
155                                 qp->s_ack_state = OP(RDMA_READ_RESPONSE_FIRST);
156                         } else {
157                                 qp->s_ack_state = OP(RDMA_READ_RESPONSE_ONLY);
158                                 e->sent = 1;
159                         }
160                         ohdr->u.aeth = rvt_compute_aeth(qp);
161                         hwords++;
162                         qp->s_ack_rdma_psn = e->psn;
163                         bth2 = mask_psn(qp->s_ack_rdma_psn++);
164                 } else {
165                         /* COMPARE_SWAP or FETCH_ADD */
166                         ps->s_txreq->ss = NULL;
167                         len = 0;
168                         qp->s_ack_state = OP(ATOMIC_ACKNOWLEDGE);
169                         ohdr->u.at.aeth = rvt_compute_aeth(qp);
170                         ib_u64_put(e->atomic_data, &ohdr->u.at.atomic_ack_eth);
171                         hwords += sizeof(ohdr->u.at) / sizeof(u32);
172                         bth2 = mask_psn(e->psn);
173                         e->sent = 1;
174                 }
175                 bth0 = qp->s_ack_state << 24;
176                 break;
177
178         case OP(RDMA_READ_RESPONSE_FIRST):
179                 qp->s_ack_state = OP(RDMA_READ_RESPONSE_MIDDLE);
180                 /* FALLTHROUGH */
181         case OP(RDMA_READ_RESPONSE_MIDDLE):
182                 ps->s_txreq->ss = &qp->s_ack_rdma_sge;
183                 ps->s_txreq->mr = qp->s_ack_rdma_sge.sge.mr;
184                 if (ps->s_txreq->mr)
185                         rvt_get_mr(ps->s_txreq->mr);
186                 len = qp->s_ack_rdma_sge.sge.sge_length;
187                 if (len > pmtu) {
188                         len = pmtu;
189                         middle = HFI1_CAP_IS_KSET(SDMA_AHG);
190                 } else {
191                         ohdr->u.aeth = rvt_compute_aeth(qp);
192                         hwords++;
193                         qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
194                         e = &qp->s_ack_queue[qp->s_tail_ack_queue];
195                         e->sent = 1;
196                 }
197                 bth0 = qp->s_ack_state << 24;
198                 bth2 = mask_psn(qp->s_ack_rdma_psn++);
199                 break;
200
201         default:
202 normal:
203                 /*
204                  * Send a regular ACK.
205                  * Set the s_ack_state so we wait until after sending
206                  * the ACK before setting s_ack_state to ACKNOWLEDGE
207                  * (see above).
208                  */
209                 qp->s_ack_state = OP(SEND_ONLY);
210                 qp->s_flags &= ~RVT_S_ACK_PENDING;
211                 ps->s_txreq->ss = NULL;
212                 if (qp->s_nak_state)
213                         ohdr->u.aeth =
214                                 cpu_to_be32((qp->r_msn & IB_MSN_MASK) |
215                                             (qp->s_nak_state <<
216                                              IB_AETH_CREDIT_SHIFT));
217                 else
218                         ohdr->u.aeth = rvt_compute_aeth(qp);
219                 hwords++;
220                 len = 0;
221                 bth0 = OP(ACKNOWLEDGE) << 24;
222                 bth2 = mask_psn(qp->s_ack_psn);
223         }
224         qp->s_rdma_ack_cnt++;
225         qp->s_hdrwords = hwords;
226         ps->s_txreq->sde = priv->s_sde;
227         ps->s_txreq->s_cur_size = len;
228         hfi1_make_ruc_header(qp, ohdr, bth0, bth2, middle, ps);
229         /* pbc */
230         ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
231         return 1;
232
233 bail:
234         qp->s_ack_state = OP(ACKNOWLEDGE);
235         /*
236          * Ensure s_rdma_ack_cnt changes are committed prior to resetting
237          * RVT_S_RESP_PENDING
238          */
239         smp_wmb();
240         qp->s_flags &= ~(RVT_S_RESP_PENDING
241                                 | RVT_S_ACK_PENDING
242                                 | RVT_S_AHG_VALID);
243         return 0;
244 }
245
246 /**
247  * hfi1_make_rc_req - construct a request packet (SEND, RDMA r/w, ATOMIC)
248  * @qp: a pointer to the QP
249  *
250  * Assumes s_lock is held.
251  *
252  * Return 1 if constructed; otherwise, return 0.
253  */
254 int hfi1_make_rc_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
255 {
256         struct hfi1_qp_priv *priv = qp->priv;
257         struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
258         struct ib_other_headers *ohdr;
259         struct rvt_sge_state *ss;
260         struct rvt_swqe *wqe;
261         /* header size in 32-bit words LRH+BTH = (8+12)/4. */
262         u32 hwords = 5;
263         u32 len;
264         u32 bth0 = 0;
265         u32 bth2;
266         u32 pmtu = qp->pmtu;
267         char newreq;
268         int middle = 0;
269         int delta;
270
271         lockdep_assert_held(&qp->s_lock);
272         ps->s_txreq = get_txreq(ps->dev, qp);
273         if (IS_ERR(ps->s_txreq))
274                 goto bail_no_tx;
275
276         ohdr = &ps->s_txreq->phdr.hdr.u.oth;
277         if (rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)
278                 ohdr = &ps->s_txreq->phdr.hdr.u.l.oth;
279
280         /* Sending responses has higher priority over sending requests. */
281         if ((qp->s_flags & RVT_S_RESP_PENDING) &&
282             make_rc_ack(dev, qp, ohdr, ps))
283                 return 1;
284
285         if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_SEND_OK)) {
286                 if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
287                         goto bail;
288                 /* We are in the error state, flush the work request. */
289                 smp_read_barrier_depends(); /* see post_one_send() */
290                 if (qp->s_last == READ_ONCE(qp->s_head))
291                         goto bail;
292                 /* If DMAs are in progress, we can't flush immediately. */
293                 if (iowait_sdma_pending(&priv->s_iowait)) {
294                         qp->s_flags |= RVT_S_WAIT_DMA;
295                         goto bail;
296                 }
297                 clear_ahg(qp);
298                 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
299                 hfi1_send_complete(qp, wqe, qp->s_last != qp->s_acked ?
300                         IB_WC_SUCCESS : IB_WC_WR_FLUSH_ERR);
301                 /* will get called again */
302                 goto done_free_tx;
303         }
304
305         if (qp->s_flags & (RVT_S_WAIT_RNR | RVT_S_WAIT_ACK))
306                 goto bail;
307
308         if (cmp_psn(qp->s_psn, qp->s_sending_hpsn) <= 0) {
309                 if (cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0) {
310                         qp->s_flags |= RVT_S_WAIT_PSN;
311                         goto bail;
312                 }
313                 qp->s_sending_psn = qp->s_psn;
314                 qp->s_sending_hpsn = qp->s_psn - 1;
315         }
316
317         /* Send a request. */
318         wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
319         switch (qp->s_state) {
320         default:
321                 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK))
322                         goto bail;
323                 /*
324                  * Resend an old request or start a new one.
325                  *
326                  * We keep track of the current SWQE so that
327                  * we don't reset the "furthest progress" state
328                  * if we need to back up.
329                  */
330                 newreq = 0;
331                 if (qp->s_cur == qp->s_tail) {
332                         /* Check if send work queue is empty. */
333                         smp_read_barrier_depends(); /* see post_one_send() */
334                         if (qp->s_tail == READ_ONCE(qp->s_head)) {
335                                 clear_ahg(qp);
336                                 goto bail;
337                         }
338                         /*
339                          * If a fence is requested, wait for previous
340                          * RDMA read and atomic operations to finish.
341                          */
342                         if ((wqe->wr.send_flags & IB_SEND_FENCE) &&
343                             qp->s_num_rd_atomic) {
344                                 qp->s_flags |= RVT_S_WAIT_FENCE;
345                                 goto bail;
346                         }
347                         /*
348                          * Local operations are processed immediately
349                          * after all prior requests have completed
350                          */
351                         if (wqe->wr.opcode == IB_WR_REG_MR ||
352                             wqe->wr.opcode == IB_WR_LOCAL_INV) {
353                                 int local_ops = 0;
354                                 int err = 0;
355
356                                 if (qp->s_last != qp->s_cur)
357                                         goto bail;
358                                 if (++qp->s_cur == qp->s_size)
359                                         qp->s_cur = 0;
360                                 if (++qp->s_tail == qp->s_size)
361                                         qp->s_tail = 0;
362                                 if (!(wqe->wr.send_flags &
363                                       RVT_SEND_COMPLETION_ONLY)) {
364                                         err = rvt_invalidate_rkey(
365                                                 qp,
366                                                 wqe->wr.ex.invalidate_rkey);
367                                         local_ops = 1;
368                                 }
369                                 hfi1_send_complete(qp, wqe,
370                                                    err ? IB_WC_LOC_PROT_ERR
371                                                        : IB_WC_SUCCESS);
372                                 if (local_ops)
373                                         atomic_dec(&qp->local_ops_pending);
374                                 qp->s_hdrwords = 0;
375                                 goto done_free_tx;
376                         }
377
378                         newreq = 1;
379                         qp->s_psn = wqe->psn;
380                 }
381                 /*
382                  * Note that we have to be careful not to modify the
383                  * original work request since we may need to resend
384                  * it.
385                  */
386                 len = wqe->length;
387                 ss = &qp->s_sge;
388                 bth2 = mask_psn(qp->s_psn);
389                 switch (wqe->wr.opcode) {
390                 case IB_WR_SEND:
391                 case IB_WR_SEND_WITH_IMM:
392                 case IB_WR_SEND_WITH_INV:
393                         /* If no credit, return. */
394                         if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
395                             rvt_cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
396                                 qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
397                                 goto bail;
398                         }
399                         if (len > pmtu) {
400                                 qp->s_state = OP(SEND_FIRST);
401                                 len = pmtu;
402                                 break;
403                         }
404                         if (wqe->wr.opcode == IB_WR_SEND) {
405                                 qp->s_state = OP(SEND_ONLY);
406                         } else if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
407                                 qp->s_state = OP(SEND_ONLY_WITH_IMMEDIATE);
408                                 /* Immediate data comes after the BTH */
409                                 ohdr->u.imm_data = wqe->wr.ex.imm_data;
410                                 hwords += 1;
411                         } else {
412                                 qp->s_state = OP(SEND_ONLY_WITH_INVALIDATE);
413                                 /* Invalidate rkey comes after the BTH */
414                                 ohdr->u.ieth = cpu_to_be32(
415                                                 wqe->wr.ex.invalidate_rkey);
416                                 hwords += 1;
417                         }
418                         if (wqe->wr.send_flags & IB_SEND_SOLICITED)
419                                 bth0 |= IB_BTH_SOLICITED;
420                         bth2 |= IB_BTH_REQ_ACK;
421                         if (++qp->s_cur == qp->s_size)
422                                 qp->s_cur = 0;
423                         break;
424
425                 case IB_WR_RDMA_WRITE:
426                         if (newreq && !(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
427                                 qp->s_lsn++;
428                         /* FALLTHROUGH */
429                 case IB_WR_RDMA_WRITE_WITH_IMM:
430                         /* If no credit, return. */
431                         if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
432                             rvt_cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
433                                 qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
434                                 goto bail;
435                         }
436                         put_ib_reth_vaddr(
437                                 wqe->rdma_wr.remote_addr,
438                                 &ohdr->u.rc.reth);
439                         ohdr->u.rc.reth.rkey =
440                                 cpu_to_be32(wqe->rdma_wr.rkey);
441                         ohdr->u.rc.reth.length = cpu_to_be32(len);
442                         hwords += sizeof(struct ib_reth) / sizeof(u32);
443                         if (len > pmtu) {
444                                 qp->s_state = OP(RDMA_WRITE_FIRST);
445                                 len = pmtu;
446                                 break;
447                         }
448                         if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
449                                 qp->s_state = OP(RDMA_WRITE_ONLY);
450                         } else {
451                                 qp->s_state =
452                                         OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
453                                 /* Immediate data comes after RETH */
454                                 ohdr->u.rc.imm_data = wqe->wr.ex.imm_data;
455                                 hwords += 1;
456                                 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
457                                         bth0 |= IB_BTH_SOLICITED;
458                         }
459                         bth2 |= IB_BTH_REQ_ACK;
460                         if (++qp->s_cur == qp->s_size)
461                                 qp->s_cur = 0;
462                         break;
463
464                 case IB_WR_RDMA_READ:
465                         /*
466                          * Don't allow more operations to be started
467                          * than the QP limits allow.
468                          */
469                         if (newreq) {
470                                 if (qp->s_num_rd_atomic >=
471                                     qp->s_max_rd_atomic) {
472                                         qp->s_flags |= RVT_S_WAIT_RDMAR;
473                                         goto bail;
474                                 }
475                                 qp->s_num_rd_atomic++;
476                                 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
477                                         qp->s_lsn++;
478                         }
479                         put_ib_reth_vaddr(
480                                 wqe->rdma_wr.remote_addr,
481                                 &ohdr->u.rc.reth);
482                         ohdr->u.rc.reth.rkey =
483                                 cpu_to_be32(wqe->rdma_wr.rkey);
484                         ohdr->u.rc.reth.length = cpu_to_be32(len);
485                         qp->s_state = OP(RDMA_READ_REQUEST);
486                         hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
487                         ss = NULL;
488                         len = 0;
489                         bth2 |= IB_BTH_REQ_ACK;
490                         if (++qp->s_cur == qp->s_size)
491                                 qp->s_cur = 0;
492                         break;
493
494                 case IB_WR_ATOMIC_CMP_AND_SWP:
495                 case IB_WR_ATOMIC_FETCH_AND_ADD:
496                         /*
497                          * Don't allow more operations to be started
498                          * than the QP limits allow.
499                          */
500                         if (newreq) {
501                                 if (qp->s_num_rd_atomic >=
502                                     qp->s_max_rd_atomic) {
503                                         qp->s_flags |= RVT_S_WAIT_RDMAR;
504                                         goto bail;
505                                 }
506                                 qp->s_num_rd_atomic++;
507                                 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
508                                         qp->s_lsn++;
509                         }
510                         if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
511                                 qp->s_state = OP(COMPARE_SWAP);
512                                 put_ib_ateth_swap(wqe->atomic_wr.swap,
513                                                   &ohdr->u.atomic_eth);
514                                 put_ib_ateth_compare(wqe->atomic_wr.compare_add,
515                                                      &ohdr->u.atomic_eth);
516                         } else {
517                                 qp->s_state = OP(FETCH_ADD);
518                                 put_ib_ateth_swap(wqe->atomic_wr.compare_add,
519                                                   &ohdr->u.atomic_eth);
520                                 put_ib_ateth_compare(0, &ohdr->u.atomic_eth);
521                         }
522                         put_ib_ateth_vaddr(wqe->atomic_wr.remote_addr,
523                                            &ohdr->u.atomic_eth);
524                         ohdr->u.atomic_eth.rkey = cpu_to_be32(
525                                 wqe->atomic_wr.rkey);
526                         hwords += sizeof(struct ib_atomic_eth) / sizeof(u32);
527                         ss = NULL;
528                         len = 0;
529                         bth2 |= IB_BTH_REQ_ACK;
530                         if (++qp->s_cur == qp->s_size)
531                                 qp->s_cur = 0;
532                         break;
533
534                 default:
535                         goto bail;
536                 }
537                 qp->s_sge.sge = wqe->sg_list[0];
538                 qp->s_sge.sg_list = wqe->sg_list + 1;
539                 qp->s_sge.num_sge = wqe->wr.num_sge;
540                 qp->s_sge.total_len = wqe->length;
541                 qp->s_len = wqe->length;
542                 if (newreq) {
543                         qp->s_tail++;
544                         if (qp->s_tail >= qp->s_size)
545                                 qp->s_tail = 0;
546                 }
547                 if (wqe->wr.opcode == IB_WR_RDMA_READ)
548                         qp->s_psn = wqe->lpsn + 1;
549                 else
550                         qp->s_psn++;
551                 break;
552
553         case OP(RDMA_READ_RESPONSE_FIRST):
554                 /*
555                  * qp->s_state is normally set to the opcode of the
556                  * last packet constructed for new requests and therefore
557                  * is never set to RDMA read response.
558                  * RDMA_READ_RESPONSE_FIRST is used by the ACK processing
559                  * thread to indicate a SEND needs to be restarted from an
560                  * earlier PSN without interfering with the sending thread.
561                  * See restart_rc().
562                  */
563                 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
564                 /* FALLTHROUGH */
565         case OP(SEND_FIRST):
566                 qp->s_state = OP(SEND_MIDDLE);
567                 /* FALLTHROUGH */
568         case OP(SEND_MIDDLE):
569                 bth2 = mask_psn(qp->s_psn++);
570                 ss = &qp->s_sge;
571                 len = qp->s_len;
572                 if (len > pmtu) {
573                         len = pmtu;
574                         middle = HFI1_CAP_IS_KSET(SDMA_AHG);
575                         break;
576                 }
577                 if (wqe->wr.opcode == IB_WR_SEND) {
578                         qp->s_state = OP(SEND_LAST);
579                 } else if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
580                         qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
581                         /* Immediate data comes after the BTH */
582                         ohdr->u.imm_data = wqe->wr.ex.imm_data;
583                         hwords += 1;
584                 } else {
585                         qp->s_state = OP(SEND_LAST_WITH_INVALIDATE);
586                         /* invalidate data comes after the BTH */
587                         ohdr->u.ieth = cpu_to_be32(wqe->wr.ex.invalidate_rkey);
588                         hwords += 1;
589                 }
590                 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
591                         bth0 |= IB_BTH_SOLICITED;
592                 bth2 |= IB_BTH_REQ_ACK;
593                 qp->s_cur++;
594                 if (qp->s_cur >= qp->s_size)
595                         qp->s_cur = 0;
596                 break;
597
598         case OP(RDMA_READ_RESPONSE_LAST):
599                 /*
600                  * qp->s_state is normally set to the opcode of the
601                  * last packet constructed for new requests and therefore
602                  * is never set to RDMA read response.
603                  * RDMA_READ_RESPONSE_LAST is used by the ACK processing
604                  * thread to indicate a RDMA write needs to be restarted from
605                  * an earlier PSN without interfering with the sending thread.
606                  * See restart_rc().
607                  */
608                 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
609                 /* FALLTHROUGH */
610         case OP(RDMA_WRITE_FIRST):
611                 qp->s_state = OP(RDMA_WRITE_MIDDLE);
612                 /* FALLTHROUGH */
613         case OP(RDMA_WRITE_MIDDLE):
614                 bth2 = mask_psn(qp->s_psn++);
615                 ss = &qp->s_sge;
616                 len = qp->s_len;
617                 if (len > pmtu) {
618                         len = pmtu;
619                         middle = HFI1_CAP_IS_KSET(SDMA_AHG);
620                         break;
621                 }
622                 if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
623                         qp->s_state = OP(RDMA_WRITE_LAST);
624                 } else {
625                         qp->s_state = OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
626                         /* Immediate data comes after the BTH */
627                         ohdr->u.imm_data = wqe->wr.ex.imm_data;
628                         hwords += 1;
629                         if (wqe->wr.send_flags & IB_SEND_SOLICITED)
630                                 bth0 |= IB_BTH_SOLICITED;
631                 }
632                 bth2 |= IB_BTH_REQ_ACK;
633                 qp->s_cur++;
634                 if (qp->s_cur >= qp->s_size)
635                         qp->s_cur = 0;
636                 break;
637
638         case OP(RDMA_READ_RESPONSE_MIDDLE):
639                 /*
640                  * qp->s_state is normally set to the opcode of the
641                  * last packet constructed for new requests and therefore
642                  * is never set to RDMA read response.
643                  * RDMA_READ_RESPONSE_MIDDLE is used by the ACK processing
644                  * thread to indicate a RDMA read needs to be restarted from
645                  * an earlier PSN without interfering with the sending thread.
646                  * See restart_rc().
647                  */
648                 len = (delta_psn(qp->s_psn, wqe->psn)) * pmtu;
649                 put_ib_reth_vaddr(
650                         wqe->rdma_wr.remote_addr + len,
651                         &ohdr->u.rc.reth);
652                 ohdr->u.rc.reth.rkey =
653                         cpu_to_be32(wqe->rdma_wr.rkey);
654                 ohdr->u.rc.reth.length = cpu_to_be32(wqe->length - len);
655                 qp->s_state = OP(RDMA_READ_REQUEST);
656                 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
657                 bth2 = mask_psn(qp->s_psn) | IB_BTH_REQ_ACK;
658                 qp->s_psn = wqe->lpsn + 1;
659                 ss = NULL;
660                 len = 0;
661                 qp->s_cur++;
662                 if (qp->s_cur == qp->s_size)
663                         qp->s_cur = 0;
664                 break;
665         }
666         qp->s_sending_hpsn = bth2;
667         delta = delta_psn(bth2, wqe->psn);
668         if (delta && delta % HFI1_PSN_CREDIT == 0)
669                 bth2 |= IB_BTH_REQ_ACK;
670         if (qp->s_flags & RVT_S_SEND_ONE) {
671                 qp->s_flags &= ~RVT_S_SEND_ONE;
672                 qp->s_flags |= RVT_S_WAIT_ACK;
673                 bth2 |= IB_BTH_REQ_ACK;
674         }
675         qp->s_len -= len;
676         qp->s_hdrwords = hwords;
677         ps->s_txreq->sde = priv->s_sde;
678         ps->s_txreq->ss = ss;
679         ps->s_txreq->s_cur_size = len;
680         hfi1_make_ruc_header(
681                 qp,
682                 ohdr,
683                 bth0 | (qp->s_state << 24),
684                 bth2,
685                 middle,
686                 ps);
687         /* pbc */
688         ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
689         return 1;
690
691 done_free_tx:
692         hfi1_put_txreq(ps->s_txreq);
693         ps->s_txreq = NULL;
694         return 1;
695
696 bail:
697         hfi1_put_txreq(ps->s_txreq);
698
699 bail_no_tx:
700         ps->s_txreq = NULL;
701         qp->s_flags &= ~RVT_S_BUSY;
702         qp->s_hdrwords = 0;
703         return 0;
704 }
705
706 /**
707  * hfi1_send_rc_ack - Construct an ACK packet and send it
708  * @qp: a pointer to the QP
709  *
710  * This is called from hfi1_rc_rcv() and handle_receive_interrupt().
711  * Note that RDMA reads and atomics are handled in the
712  * send side QP state and send engine.
713  */
714 void hfi1_send_rc_ack(struct hfi1_ctxtdata *rcd, struct rvt_qp *qp,
715                       int is_fecn)
716 {
717         struct hfi1_ibport *ibp = rcd_to_iport(rcd);
718         struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
719         u64 pbc, pbc_flags = 0;
720         u16 lrh0;
721         u16 sc5;
722         u32 bth0;
723         u32 hwords;
724         u32 vl, plen;
725         struct send_context *sc;
726         struct pio_buf *pbuf;
727         struct ib_header hdr;
728         struct ib_other_headers *ohdr;
729         unsigned long flags;
730         struct hfi1_qp_priv *priv = qp->priv;
731
732         /* clear the defer count */
733         priv->r_adefered = 0;
734
735         /* Don't send ACK or NAK if a RDMA read or atomic is pending. */
736         if (qp->s_flags & RVT_S_RESP_PENDING)
737                 goto queue_ack;
738
739         /* Ensure s_rdma_ack_cnt changes are committed */
740         smp_read_barrier_depends();
741         if (qp->s_rdma_ack_cnt)
742                 goto queue_ack;
743
744         /* Construct the header */
745         /* header size in 32-bit words LRH+BTH+AETH = (8+12+4)/4 */
746         hwords = 6;
747         if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)) {
748                 hwords += hfi1_make_grh(ibp, &hdr.u.l.grh,
749                                         rdma_ah_read_grh(&qp->remote_ah_attr),
750                                         hwords, 0);
751                 ohdr = &hdr.u.l.oth;
752                 lrh0 = HFI1_LRH_GRH;
753         } else {
754                 ohdr = &hdr.u.oth;
755                 lrh0 = HFI1_LRH_BTH;
756         }
757         /* read pkey_index w/o lock (its atomic) */
758         bth0 = hfi1_get_pkey(ibp, qp->s_pkey_index) | (OP(ACKNOWLEDGE) << 24);
759         if (qp->s_mig_state == IB_MIG_MIGRATED)
760                 bth0 |= IB_BTH_MIG_REQ;
761         if (qp->r_nak_state)
762                 ohdr->u.aeth = cpu_to_be32((qp->r_msn & IB_MSN_MASK) |
763                                             (qp->r_nak_state <<
764                                              IB_AETH_CREDIT_SHIFT));
765         else
766                 ohdr->u.aeth = rvt_compute_aeth(qp);
767         sc5 = ibp->sl_to_sc[rdma_ah_get_sl(&qp->remote_ah_attr)];
768         /* set PBC_DC_INFO bit (aka SC[4]) in pbc_flags */
769         pbc_flags |= ((!!(sc5 & 0x10)) << PBC_DC_INFO_SHIFT);
770         lrh0 |= (sc5 & 0xf) << 12 | (rdma_ah_get_sl(&qp->remote_ah_attr)
771                                      & 0xf) << 4;
772         hdr.lrh[0] = cpu_to_be16(lrh0);
773         hdr.lrh[1] = cpu_to_be16(rdma_ah_get_dlid(&qp->remote_ah_attr));
774         hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
775         hdr.lrh[3] = cpu_to_be16(ppd->lid |
776                                  rdma_ah_get_path_bits(&qp->remote_ah_attr));
777         ohdr->bth[0] = cpu_to_be32(bth0);
778         ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
779         ohdr->bth[1] |= cpu_to_be32((!!is_fecn) << IB_BECN_SHIFT);
780         ohdr->bth[2] = cpu_to_be32(mask_psn(qp->r_ack_psn));
781
782         /* Don't try to send ACKs if the link isn't ACTIVE */
783         if (driver_lstate(ppd) != IB_PORT_ACTIVE)
784                 return;
785
786         sc = rcd->sc;
787         plen = 2 /* PBC */ + hwords;
788         vl = sc_to_vlt(ppd->dd, sc5);
789         pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
790
791         pbuf = sc_buffer_alloc(sc, plen, NULL, NULL);
792         if (!pbuf) {
793                 /*
794                  * We have no room to send at the moment.  Pass
795                  * responsibility for sending the ACK to the send engine
796                  * so that when enough buffer space becomes available,
797                  * the ACK is sent ahead of other outgoing packets.
798                  */
799                 goto queue_ack;
800         }
801
802         trace_ack_output_ibhdr(dd_from_ibdev(qp->ibqp.device), &hdr);
803
804         /* write the pbc and data */
805         ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc, &hdr, hwords);
806
807         return;
808
809 queue_ack:
810         spin_lock_irqsave(&qp->s_lock, flags);
811         if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
812                 goto unlock;
813         this_cpu_inc(*ibp->rvp.rc_qacks);
814         qp->s_flags |= RVT_S_ACK_PENDING | RVT_S_RESP_PENDING;
815         qp->s_nak_state = qp->r_nak_state;
816         qp->s_ack_psn = qp->r_ack_psn;
817         if (is_fecn)
818                 qp->s_flags |= RVT_S_ECN;
819
820         /* Schedule the send engine. */
821         hfi1_schedule_send(qp);
822 unlock:
823         spin_unlock_irqrestore(&qp->s_lock, flags);
824 }
825
826 /**
827  * reset_psn - reset the QP state to send starting from PSN
828  * @qp: the QP
829  * @psn: the packet sequence number to restart at
830  *
831  * This is called from hfi1_rc_rcv() to process an incoming RC ACK
832  * for the given QP.
833  * Called at interrupt level with the QP s_lock held.
834  */
835 static void reset_psn(struct rvt_qp *qp, u32 psn)
836 {
837         u32 n = qp->s_acked;
838         struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, n);
839         u32 opcode;
840
841         lockdep_assert_held(&qp->s_lock);
842         qp->s_cur = n;
843
844         /*
845          * If we are starting the request from the beginning,
846          * let the normal send code handle initialization.
847          */
848         if (cmp_psn(psn, wqe->psn) <= 0) {
849                 qp->s_state = OP(SEND_LAST);
850                 goto done;
851         }
852
853         /* Find the work request opcode corresponding to the given PSN. */
854         opcode = wqe->wr.opcode;
855         for (;;) {
856                 int diff;
857
858                 if (++n == qp->s_size)
859                         n = 0;
860                 if (n == qp->s_tail)
861                         break;
862                 wqe = rvt_get_swqe_ptr(qp, n);
863                 diff = cmp_psn(psn, wqe->psn);
864                 if (diff < 0)
865                         break;
866                 qp->s_cur = n;
867                 /*
868                  * If we are starting the request from the beginning,
869                  * let the normal send code handle initialization.
870                  */
871                 if (diff == 0) {
872                         qp->s_state = OP(SEND_LAST);
873                         goto done;
874                 }
875                 opcode = wqe->wr.opcode;
876         }
877
878         /*
879          * Set the state to restart in the middle of a request.
880          * Don't change the s_sge, s_cur_sge, or s_cur_size.
881          * See hfi1_make_rc_req().
882          */
883         switch (opcode) {
884         case IB_WR_SEND:
885         case IB_WR_SEND_WITH_IMM:
886                 qp->s_state = OP(RDMA_READ_RESPONSE_FIRST);
887                 break;
888
889         case IB_WR_RDMA_WRITE:
890         case IB_WR_RDMA_WRITE_WITH_IMM:
891                 qp->s_state = OP(RDMA_READ_RESPONSE_LAST);
892                 break;
893
894         case IB_WR_RDMA_READ:
895                 qp->s_state = OP(RDMA_READ_RESPONSE_MIDDLE);
896                 break;
897
898         default:
899                 /*
900                  * This case shouldn't happen since its only
901                  * one PSN per req.
902                  */
903                 qp->s_state = OP(SEND_LAST);
904         }
905 done:
906         qp->s_psn = psn;
907         /*
908          * Set RVT_S_WAIT_PSN as rc_complete() may start the timer
909          * asynchronously before the send engine can get scheduled.
910          * Doing it in hfi1_make_rc_req() is too late.
911          */
912         if ((cmp_psn(qp->s_psn, qp->s_sending_hpsn) <= 0) &&
913             (cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0))
914                 qp->s_flags |= RVT_S_WAIT_PSN;
915         qp->s_flags &= ~RVT_S_AHG_VALID;
916 }
917
918 /*
919  * Back up requester to resend the last un-ACKed request.
920  * The QP r_lock and s_lock should be held and interrupts disabled.
921  */
922 void hfi1_restart_rc(struct rvt_qp *qp, u32 psn, int wait)
923 {
924         struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
925         struct hfi1_ibport *ibp;
926
927         lockdep_assert_held(&qp->r_lock);
928         lockdep_assert_held(&qp->s_lock);
929         if (qp->s_retry == 0) {
930                 if (qp->s_mig_state == IB_MIG_ARMED) {
931                         hfi1_migrate_qp(qp);
932                         qp->s_retry = qp->s_retry_cnt;
933                 } else if (qp->s_last == qp->s_acked) {
934                         hfi1_send_complete(qp, wqe, IB_WC_RETRY_EXC_ERR);
935                         rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
936                         return;
937                 } else { /* need to handle delayed completion */
938                         return;
939                 }
940         } else {
941                 qp->s_retry--;
942         }
943
944         ibp = to_iport(qp->ibqp.device, qp->port_num);
945         if (wqe->wr.opcode == IB_WR_RDMA_READ)
946                 ibp->rvp.n_rc_resends++;
947         else
948                 ibp->rvp.n_rc_resends += delta_psn(qp->s_psn, psn);
949
950         qp->s_flags &= ~(RVT_S_WAIT_FENCE | RVT_S_WAIT_RDMAR |
951                          RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_PSN |
952                          RVT_S_WAIT_ACK);
953         if (wait)
954                 qp->s_flags |= RVT_S_SEND_ONE;
955         reset_psn(qp, psn);
956 }
957
958 /*
959  * Set qp->s_sending_psn to the next PSN after the given one.
960  * This would be psn+1 except when RDMA reads are present.
961  */
962 static void reset_sending_psn(struct rvt_qp *qp, u32 psn)
963 {
964         struct rvt_swqe *wqe;
965         u32 n = qp->s_last;
966
967         lockdep_assert_held(&qp->s_lock);
968         /* Find the work request corresponding to the given PSN. */
969         for (;;) {
970                 wqe = rvt_get_swqe_ptr(qp, n);
971                 if (cmp_psn(psn, wqe->lpsn) <= 0) {
972                         if (wqe->wr.opcode == IB_WR_RDMA_READ)
973                                 qp->s_sending_psn = wqe->lpsn + 1;
974                         else
975                                 qp->s_sending_psn = psn + 1;
976                         break;
977                 }
978                 if (++n == qp->s_size)
979                         n = 0;
980                 if (n == qp->s_tail)
981                         break;
982         }
983 }
984
985 /*
986  * This should be called with the QP s_lock held and interrupts disabled.
987  */
988 void hfi1_rc_send_complete(struct rvt_qp *qp, struct ib_header *hdr)
989 {
990         struct ib_other_headers *ohdr;
991         struct rvt_swqe *wqe;
992         u32 opcode;
993         u32 psn;
994
995         lockdep_assert_held(&qp->s_lock);
996         if (!(ib_rvt_state_ops[qp->state] & RVT_SEND_OR_FLUSH_OR_RECV_OK))
997                 return;
998
999         /* Find out where the BTH is */
1000         if (ib_get_lnh(hdr) == HFI1_LRH_BTH)
1001                 ohdr = &hdr->u.oth;
1002         else
1003                 ohdr = &hdr->u.l.oth;
1004
1005         opcode = ib_bth_get_opcode(ohdr);
1006         if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
1007             opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
1008                 WARN_ON(!qp->s_rdma_ack_cnt);
1009                 qp->s_rdma_ack_cnt--;
1010                 return;
1011         }
1012
1013         psn = be32_to_cpu(ohdr->bth[2]);
1014         reset_sending_psn(qp, psn);
1015
1016         /*
1017          * Start timer after a packet requesting an ACK has been sent and
1018          * there are still requests that haven't been acked.
1019          */
1020         if ((psn & IB_BTH_REQ_ACK) && qp->s_acked != qp->s_tail &&
1021             !(qp->s_flags &
1022                 (RVT_S_TIMER | RVT_S_WAIT_RNR | RVT_S_WAIT_PSN)) &&
1023                 (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
1024                 rvt_add_retry_timer(qp);
1025
1026         while (qp->s_last != qp->s_acked) {
1027                 u32 s_last;
1028
1029                 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
1030                 if (cmp_psn(wqe->lpsn, qp->s_sending_psn) >= 0 &&
1031                     cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0)
1032                         break;
1033                 s_last = qp->s_last;
1034                 trace_hfi1_qp_send_completion(qp, wqe, s_last);
1035                 if (++s_last >= qp->s_size)
1036                         s_last = 0;
1037                 qp->s_last = s_last;
1038                 /* see post_send() */
1039                 barrier();
1040                 rvt_put_swqe(wqe);
1041                 rvt_qp_swqe_complete(qp,
1042                                      wqe,
1043                                      ib_hfi1_wc_opcode[wqe->wr.opcode],
1044                                      IB_WC_SUCCESS);
1045         }
1046         /*
1047          * If we were waiting for sends to complete before re-sending,
1048          * and they are now complete, restart sending.
1049          */
1050         trace_hfi1_sendcomplete(qp, psn);
1051         if (qp->s_flags & RVT_S_WAIT_PSN &&
1052             cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
1053                 qp->s_flags &= ~RVT_S_WAIT_PSN;
1054                 qp->s_sending_psn = qp->s_psn;
1055                 qp->s_sending_hpsn = qp->s_psn - 1;
1056                 hfi1_schedule_send(qp);
1057         }
1058 }
1059
1060 static inline void update_last_psn(struct rvt_qp *qp, u32 psn)
1061 {
1062         qp->s_last_psn = psn;
1063 }
1064
1065 /*
1066  * Generate a SWQE completion.
1067  * This is similar to hfi1_send_complete but has to check to be sure
1068  * that the SGEs are not being referenced if the SWQE is being resent.
1069  */
1070 static struct rvt_swqe *do_rc_completion(struct rvt_qp *qp,
1071                                          struct rvt_swqe *wqe,
1072                                          struct hfi1_ibport *ibp)
1073 {
1074         lockdep_assert_held(&qp->s_lock);
1075         /*
1076          * Don't decrement refcount and don't generate a
1077          * completion if the SWQE is being resent until the send
1078          * is finished.
1079          */
1080         if (cmp_psn(wqe->lpsn, qp->s_sending_psn) < 0 ||
1081             cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
1082                 u32 s_last;
1083
1084                 rvt_put_swqe(wqe);
1085                 s_last = qp->s_last;
1086                 trace_hfi1_qp_send_completion(qp, wqe, s_last);
1087                 if (++s_last >= qp->s_size)
1088                         s_last = 0;
1089                 qp->s_last = s_last;
1090                 /* see post_send() */
1091                 barrier();
1092                 rvt_qp_swqe_complete(qp,
1093                                      wqe,
1094                                      ib_hfi1_wc_opcode[wqe->wr.opcode],
1095                                      IB_WC_SUCCESS);
1096         } else {
1097                 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
1098
1099                 this_cpu_inc(*ibp->rvp.rc_delayed_comp);
1100                 /*
1101                  * If send progress not running attempt to progress
1102                  * SDMA queue.
1103                  */
1104                 if (ppd->dd->flags & HFI1_HAS_SEND_DMA) {
1105                         struct sdma_engine *engine;
1106                         u8 sl = rdma_ah_get_sl(&qp->remote_ah_attr);
1107                         u8 sc5;
1108
1109                         /* For now use sc to find engine */
1110                         sc5 = ibp->sl_to_sc[sl];
1111                         engine = qp_to_sdma_engine(qp, sc5);
1112                         sdma_engine_progress_schedule(engine);
1113                 }
1114         }
1115
1116         qp->s_retry = qp->s_retry_cnt;
1117         update_last_psn(qp, wqe->lpsn);
1118
1119         /*
1120          * If we are completing a request which is in the process of
1121          * being resent, we can stop re-sending it since we know the
1122          * responder has already seen it.
1123          */
1124         if (qp->s_acked == qp->s_cur) {
1125                 if (++qp->s_cur >= qp->s_size)
1126                         qp->s_cur = 0;
1127                 qp->s_acked = qp->s_cur;
1128                 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
1129                 if (qp->s_acked != qp->s_tail) {
1130                         qp->s_state = OP(SEND_LAST);
1131                         qp->s_psn = wqe->psn;
1132                 }
1133         } else {
1134                 if (++qp->s_acked >= qp->s_size)
1135                         qp->s_acked = 0;
1136                 if (qp->state == IB_QPS_SQD && qp->s_acked == qp->s_cur)
1137                         qp->s_draining = 0;
1138                 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
1139         }
1140         return wqe;
1141 }
1142
1143 /**
1144  * do_rc_ack - process an incoming RC ACK
1145  * @qp: the QP the ACK came in on
1146  * @psn: the packet sequence number of the ACK
1147  * @opcode: the opcode of the request that resulted in the ACK
1148  *
1149  * This is called from rc_rcv_resp() to process an incoming RC ACK
1150  * for the given QP.
1151  * May be called at interrupt level, with the QP s_lock held.
1152  * Returns 1 if OK, 0 if current operation should be aborted (NAK).
1153  */
1154 static int do_rc_ack(struct rvt_qp *qp, u32 aeth, u32 psn, int opcode,
1155                      u64 val, struct hfi1_ctxtdata *rcd)
1156 {
1157         struct hfi1_ibport *ibp;
1158         enum ib_wc_status status;
1159         struct rvt_swqe *wqe;
1160         int ret = 0;
1161         u32 ack_psn;
1162         int diff;
1163
1164         lockdep_assert_held(&qp->s_lock);
1165         /*
1166          * Note that NAKs implicitly ACK outstanding SEND and RDMA write
1167          * requests and implicitly NAK RDMA read and atomic requests issued
1168          * before the NAK'ed request.  The MSN won't include the NAK'ed
1169          * request but will include an ACK'ed request(s).
1170          */
1171         ack_psn = psn;
1172         if (aeth >> IB_AETH_NAK_SHIFT)
1173                 ack_psn--;
1174         wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
1175         ibp = rcd_to_iport(rcd);
1176
1177         /*
1178          * The MSN might be for a later WQE than the PSN indicates so
1179          * only complete WQEs that the PSN finishes.
1180          */
1181         while ((diff = delta_psn(ack_psn, wqe->lpsn)) >= 0) {
1182                 /*
1183                  * RDMA_READ_RESPONSE_ONLY is a special case since
1184                  * we want to generate completion events for everything
1185                  * before the RDMA read, copy the data, then generate
1186                  * the completion for the read.
1187                  */
1188                 if (wqe->wr.opcode == IB_WR_RDMA_READ &&
1189                     opcode == OP(RDMA_READ_RESPONSE_ONLY) &&
1190                     diff == 0) {
1191                         ret = 1;
1192                         goto bail_stop;
1193                 }
1194                 /*
1195                  * If this request is a RDMA read or atomic, and the ACK is
1196                  * for a later operation, this ACK NAKs the RDMA read or
1197                  * atomic.  In other words, only a RDMA_READ_LAST or ONLY
1198                  * can ACK a RDMA read and likewise for atomic ops.  Note
1199                  * that the NAK case can only happen if relaxed ordering is
1200                  * used and requests are sent after an RDMA read or atomic
1201                  * is sent but before the response is received.
1202                  */
1203                 if ((wqe->wr.opcode == IB_WR_RDMA_READ &&
1204                      (opcode != OP(RDMA_READ_RESPONSE_LAST) || diff != 0)) ||
1205                     ((wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1206                       wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) &&
1207                      (opcode != OP(ATOMIC_ACKNOWLEDGE) || diff != 0))) {
1208                         /* Retry this request. */
1209                         if (!(qp->r_flags & RVT_R_RDMAR_SEQ)) {
1210                                 qp->r_flags |= RVT_R_RDMAR_SEQ;
1211                                 hfi1_restart_rc(qp, qp->s_last_psn + 1, 0);
1212                                 if (list_empty(&qp->rspwait)) {
1213                                         qp->r_flags |= RVT_R_RSP_SEND;
1214                                         rvt_get_qp(qp);
1215                                         list_add_tail(&qp->rspwait,
1216                                                       &rcd->qp_wait_list);
1217                                 }
1218                         }
1219                         /*
1220                          * No need to process the ACK/NAK since we are
1221                          * restarting an earlier request.
1222                          */
1223                         goto bail_stop;
1224                 }
1225                 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1226                     wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
1227                         u64 *vaddr = wqe->sg_list[0].vaddr;
1228                         *vaddr = val;
1229                 }
1230                 if (qp->s_num_rd_atomic &&
1231                     (wqe->wr.opcode == IB_WR_RDMA_READ ||
1232                      wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1233                      wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)) {
1234                         qp->s_num_rd_atomic--;
1235                         /* Restart sending task if fence is complete */
1236                         if ((qp->s_flags & RVT_S_WAIT_FENCE) &&
1237                             !qp->s_num_rd_atomic) {
1238                                 qp->s_flags &= ~(RVT_S_WAIT_FENCE |
1239                                                  RVT_S_WAIT_ACK);
1240                                 hfi1_schedule_send(qp);
1241                         } else if (qp->s_flags & RVT_S_WAIT_RDMAR) {
1242                                 qp->s_flags &= ~(RVT_S_WAIT_RDMAR |
1243                                                  RVT_S_WAIT_ACK);
1244                                 hfi1_schedule_send(qp);
1245                         }
1246                 }
1247                 wqe = do_rc_completion(qp, wqe, ibp);
1248                 if (qp->s_acked == qp->s_tail)
1249                         break;
1250         }
1251
1252         switch (aeth >> IB_AETH_NAK_SHIFT) {
1253         case 0:         /* ACK */
1254                 this_cpu_inc(*ibp->rvp.rc_acks);
1255                 if (qp->s_acked != qp->s_tail) {
1256                         /*
1257                          * We are expecting more ACKs so
1258                          * mod the retry timer.
1259                          */
1260                         rvt_mod_retry_timer(qp);
1261                         /*
1262                          * We can stop re-sending the earlier packets and
1263                          * continue with the next packet the receiver wants.
1264                          */
1265                         if (cmp_psn(qp->s_psn, psn) <= 0)
1266                                 reset_psn(qp, psn + 1);
1267                 } else {
1268                         /* No more acks - kill all timers */
1269                         rvt_stop_rc_timers(qp);
1270                         if (cmp_psn(qp->s_psn, psn) <= 0) {
1271                                 qp->s_state = OP(SEND_LAST);
1272                                 qp->s_psn = psn + 1;
1273                         }
1274                 }
1275                 if (qp->s_flags & RVT_S_WAIT_ACK) {
1276                         qp->s_flags &= ~RVT_S_WAIT_ACK;
1277                         hfi1_schedule_send(qp);
1278                 }
1279                 rvt_get_credit(qp, aeth);
1280                 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1281                 qp->s_retry = qp->s_retry_cnt;
1282                 update_last_psn(qp, psn);
1283                 return 1;
1284
1285         case 1:         /* RNR NAK */
1286                 ibp->rvp.n_rnr_naks++;
1287                 if (qp->s_acked == qp->s_tail)
1288                         goto bail_stop;
1289                 if (qp->s_flags & RVT_S_WAIT_RNR)
1290                         goto bail_stop;
1291                 if (qp->s_rnr_retry == 0) {
1292                         status = IB_WC_RNR_RETRY_EXC_ERR;
1293                         goto class_b;
1294                 }
1295                 if (qp->s_rnr_retry_cnt < 7)
1296                         qp->s_rnr_retry--;
1297
1298                 /* The last valid PSN is the previous PSN. */
1299                 update_last_psn(qp, psn - 1);
1300
1301                 ibp->rvp.n_rc_resends += delta_psn(qp->s_psn, psn);
1302
1303                 reset_psn(qp, psn);
1304
1305                 qp->s_flags &= ~(RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_ACK);
1306                 rvt_stop_rc_timers(qp);
1307                 rvt_add_rnr_timer(qp, aeth);
1308                 return 0;
1309
1310         case 3:         /* NAK */
1311                 if (qp->s_acked == qp->s_tail)
1312                         goto bail_stop;
1313                 /* The last valid PSN is the previous PSN. */
1314                 update_last_psn(qp, psn - 1);
1315                 switch ((aeth >> IB_AETH_CREDIT_SHIFT) &
1316                         IB_AETH_CREDIT_MASK) {
1317                 case 0: /* PSN sequence error */
1318                         ibp->rvp.n_seq_naks++;
1319                         /*
1320                          * Back up to the responder's expected PSN.
1321                          * Note that we might get a NAK in the middle of an
1322                          * RDMA READ response which terminates the RDMA
1323                          * READ.
1324                          */
1325                         hfi1_restart_rc(qp, psn, 0);
1326                         hfi1_schedule_send(qp);
1327                         break;
1328
1329                 case 1: /* Invalid Request */
1330                         status = IB_WC_REM_INV_REQ_ERR;
1331                         ibp->rvp.n_other_naks++;
1332                         goto class_b;
1333
1334                 case 2: /* Remote Access Error */
1335                         status = IB_WC_REM_ACCESS_ERR;
1336                         ibp->rvp.n_other_naks++;
1337                         goto class_b;
1338
1339                 case 3: /* Remote Operation Error */
1340                         status = IB_WC_REM_OP_ERR;
1341                         ibp->rvp.n_other_naks++;
1342 class_b:
1343                         if (qp->s_last == qp->s_acked) {
1344                                 hfi1_send_complete(qp, wqe, status);
1345                                 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1346                         }
1347                         break;
1348
1349                 default:
1350                         /* Ignore other reserved NAK error codes */
1351                         goto reserved;
1352                 }
1353                 qp->s_retry = qp->s_retry_cnt;
1354                 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1355                 goto bail_stop;
1356
1357         default:                /* 2: reserved */
1358 reserved:
1359                 /* Ignore reserved NAK codes. */
1360                 goto bail_stop;
1361         }
1362         /* cannot be reached  */
1363 bail_stop:
1364         rvt_stop_rc_timers(qp);
1365         return ret;
1366 }
1367
1368 /*
1369  * We have seen an out of sequence RDMA read middle or last packet.
1370  * This ACKs SENDs and RDMA writes up to the first RDMA read or atomic SWQE.
1371  */
1372 static void rdma_seq_err(struct rvt_qp *qp, struct hfi1_ibport *ibp, u32 psn,
1373                          struct hfi1_ctxtdata *rcd)
1374 {
1375         struct rvt_swqe *wqe;
1376
1377         lockdep_assert_held(&qp->s_lock);
1378         /* Remove QP from retry timer */
1379         rvt_stop_rc_timers(qp);
1380
1381         wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
1382
1383         while (cmp_psn(psn, wqe->lpsn) > 0) {
1384                 if (wqe->wr.opcode == IB_WR_RDMA_READ ||
1385                     wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1386                     wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
1387                         break;
1388                 wqe = do_rc_completion(qp, wqe, ibp);
1389         }
1390
1391         ibp->rvp.n_rdma_seq++;
1392         qp->r_flags |= RVT_R_RDMAR_SEQ;
1393         hfi1_restart_rc(qp, qp->s_last_psn + 1, 0);
1394         if (list_empty(&qp->rspwait)) {
1395                 qp->r_flags |= RVT_R_RSP_SEND;
1396                 rvt_get_qp(qp);
1397                 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1398         }
1399 }
1400
1401 /**
1402  * rc_rcv_resp - process an incoming RC response packet
1403  * @ibp: the port this packet came in on
1404  * @ohdr: the other headers for this packet
1405  * @data: the packet data
1406  * @tlen: the packet length
1407  * @qp: the QP for this packet
1408  * @opcode: the opcode for this packet
1409  * @psn: the packet sequence number for this packet
1410  * @hdrsize: the header length
1411  * @pmtu: the path MTU
1412  *
1413  * This is called from hfi1_rc_rcv() to process an incoming RC response
1414  * packet for the given QP.
1415  * Called at interrupt level.
1416  */
1417 static void rc_rcv_resp(struct hfi1_ibport *ibp,
1418                         struct ib_other_headers *ohdr,
1419                         void *data, u32 tlen, struct rvt_qp *qp,
1420                         u32 opcode, u32 psn, u32 hdrsize, u32 pmtu,
1421                         struct hfi1_ctxtdata *rcd)
1422 {
1423         struct rvt_swqe *wqe;
1424         enum ib_wc_status status;
1425         unsigned long flags;
1426         int diff;
1427         u32 pad;
1428         u32 aeth;
1429         u64 val;
1430
1431         spin_lock_irqsave(&qp->s_lock, flags);
1432
1433         trace_hfi1_ack(qp, psn);
1434
1435         /* Ignore invalid responses. */
1436         smp_read_barrier_depends(); /* see post_one_send */
1437         if (cmp_psn(psn, READ_ONCE(qp->s_next_psn)) >= 0)
1438                 goto ack_done;
1439
1440         /* Ignore duplicate responses. */
1441         diff = cmp_psn(psn, qp->s_last_psn);
1442         if (unlikely(diff <= 0)) {
1443                 /* Update credits for "ghost" ACKs */
1444                 if (diff == 0 && opcode == OP(ACKNOWLEDGE)) {
1445                         aeth = be32_to_cpu(ohdr->u.aeth);
1446                         if ((aeth >> IB_AETH_NAK_SHIFT) == 0)
1447                                 rvt_get_credit(qp, aeth);
1448                 }
1449                 goto ack_done;
1450         }
1451
1452         /*
1453          * Skip everything other than the PSN we expect, if we are waiting
1454          * for a reply to a restarted RDMA read or atomic op.
1455          */
1456         if (qp->r_flags & RVT_R_RDMAR_SEQ) {
1457                 if (cmp_psn(psn, qp->s_last_psn + 1) != 0)
1458                         goto ack_done;
1459                 qp->r_flags &= ~RVT_R_RDMAR_SEQ;
1460         }
1461
1462         if (unlikely(qp->s_acked == qp->s_tail))
1463                 goto ack_done;
1464         wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
1465         status = IB_WC_SUCCESS;
1466
1467         switch (opcode) {
1468         case OP(ACKNOWLEDGE):
1469         case OP(ATOMIC_ACKNOWLEDGE):
1470         case OP(RDMA_READ_RESPONSE_FIRST):
1471                 aeth = be32_to_cpu(ohdr->u.aeth);
1472                 if (opcode == OP(ATOMIC_ACKNOWLEDGE))
1473                         val = ib_u64_get(&ohdr->u.at.atomic_ack_eth);
1474                 else
1475                         val = 0;
1476                 if (!do_rc_ack(qp, aeth, psn, opcode, val, rcd) ||
1477                     opcode != OP(RDMA_READ_RESPONSE_FIRST))
1478                         goto ack_done;
1479                 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
1480                 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1481                         goto ack_op_err;
1482                 /*
1483                  * If this is a response to a resent RDMA read, we
1484                  * have to be careful to copy the data to the right
1485                  * location.
1486                  */
1487                 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1488                                                   wqe, psn, pmtu);
1489                 goto read_middle;
1490
1491         case OP(RDMA_READ_RESPONSE_MIDDLE):
1492                 /* no AETH, no ACK */
1493                 if (unlikely(cmp_psn(psn, qp->s_last_psn + 1)))
1494                         goto ack_seq_err;
1495                 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1496                         goto ack_op_err;
1497 read_middle:
1498                 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1499                         goto ack_len_err;
1500                 if (unlikely(pmtu >= qp->s_rdma_read_len))
1501                         goto ack_len_err;
1502
1503                 /*
1504                  * We got a response so update the timeout.
1505                  * 4.096 usec. * (1 << qp->timeout)
1506                  */
1507                 rvt_mod_retry_timer(qp);
1508                 if (qp->s_flags & RVT_S_WAIT_ACK) {
1509                         qp->s_flags &= ~RVT_S_WAIT_ACK;
1510                         hfi1_schedule_send(qp);
1511                 }
1512
1513                 if (opcode == OP(RDMA_READ_RESPONSE_MIDDLE))
1514                         qp->s_retry = qp->s_retry_cnt;
1515
1516                 /*
1517                  * Update the RDMA receive state but do the copy w/o
1518                  * holding the locks and blocking interrupts.
1519                  */
1520                 qp->s_rdma_read_len -= pmtu;
1521                 update_last_psn(qp, psn);
1522                 spin_unlock_irqrestore(&qp->s_lock, flags);
1523                 hfi1_copy_sge(&qp->s_rdma_read_sge, data, pmtu, false, false);
1524                 goto bail;
1525
1526         case OP(RDMA_READ_RESPONSE_ONLY):
1527                 aeth = be32_to_cpu(ohdr->u.aeth);
1528                 if (!do_rc_ack(qp, aeth, psn, opcode, 0, rcd))
1529                         goto ack_done;
1530                 /* Get the number of bytes the message was padded by. */
1531                 pad = ib_bth_get_pad(ohdr);
1532                 /*
1533                  * Check that the data size is >= 0 && <= pmtu.
1534                  * Remember to account for ICRC (4).
1535                  */
1536                 if (unlikely(tlen < (hdrsize + pad + 4)))
1537                         goto ack_len_err;
1538                 /*
1539                  * If this is a response to a resent RDMA read, we
1540                  * have to be careful to copy the data to the right
1541                  * location.
1542                  */
1543                 wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
1544                 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1545                                                   wqe, psn, pmtu);
1546                 goto read_last;
1547
1548         case OP(RDMA_READ_RESPONSE_LAST):
1549                 /* ACKs READ req. */
1550                 if (unlikely(cmp_psn(psn, qp->s_last_psn + 1)))
1551                         goto ack_seq_err;
1552                 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1553                         goto ack_op_err;
1554                 /* Get the number of bytes the message was padded by. */
1555                 pad = ib_bth_get_pad(ohdr);
1556                 /*
1557                  * Check that the data size is >= 1 && <= pmtu.
1558                  * Remember to account for ICRC (4).
1559                  */
1560                 if (unlikely(tlen <= (hdrsize + pad + 4)))
1561                         goto ack_len_err;
1562 read_last:
1563                 tlen -= hdrsize + pad + 4;
1564                 if (unlikely(tlen != qp->s_rdma_read_len))
1565                         goto ack_len_err;
1566                 aeth = be32_to_cpu(ohdr->u.aeth);
1567                 hfi1_copy_sge(&qp->s_rdma_read_sge, data, tlen, false, false);
1568                 WARN_ON(qp->s_rdma_read_sge.num_sge);
1569                 (void)do_rc_ack(qp, aeth, psn,
1570                                  OP(RDMA_READ_RESPONSE_LAST), 0, rcd);
1571                 goto ack_done;
1572         }
1573
1574 ack_op_err:
1575         status = IB_WC_LOC_QP_OP_ERR;
1576         goto ack_err;
1577
1578 ack_seq_err:
1579         rdma_seq_err(qp, ibp, psn, rcd);
1580         goto ack_done;
1581
1582 ack_len_err:
1583         status = IB_WC_LOC_LEN_ERR;
1584 ack_err:
1585         if (qp->s_last == qp->s_acked) {
1586                 hfi1_send_complete(qp, wqe, status);
1587                 rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1588         }
1589 ack_done:
1590         spin_unlock_irqrestore(&qp->s_lock, flags);
1591 bail:
1592         return;
1593 }
1594
1595 static inline void rc_defered_ack(struct hfi1_ctxtdata *rcd,
1596                                   struct rvt_qp *qp)
1597 {
1598         if (list_empty(&qp->rspwait)) {
1599                 qp->r_flags |= RVT_R_RSP_NAK;
1600                 rvt_get_qp(qp);
1601                 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1602         }
1603 }
1604
1605 static inline void rc_cancel_ack(struct rvt_qp *qp)
1606 {
1607         struct hfi1_qp_priv *priv = qp->priv;
1608
1609         priv->r_adefered = 0;
1610         if (list_empty(&qp->rspwait))
1611                 return;
1612         list_del_init(&qp->rspwait);
1613         qp->r_flags &= ~RVT_R_RSP_NAK;
1614         rvt_put_qp(qp);
1615 }
1616
1617 /**
1618  * rc_rcv_error - process an incoming duplicate or error RC packet
1619  * @ohdr: the other headers for this packet
1620  * @data: the packet data
1621  * @qp: the QP for this packet
1622  * @opcode: the opcode for this packet
1623  * @psn: the packet sequence number for this packet
1624  * @diff: the difference between the PSN and the expected PSN
1625  *
1626  * This is called from hfi1_rc_rcv() to process an unexpected
1627  * incoming RC packet for the given QP.
1628  * Called at interrupt level.
1629  * Return 1 if no more processing is needed; otherwise return 0 to
1630  * schedule a response to be sent.
1631  */
1632 static noinline int rc_rcv_error(struct ib_other_headers *ohdr, void *data,
1633                                  struct rvt_qp *qp, u32 opcode, u32 psn,
1634                                  int diff, struct hfi1_ctxtdata *rcd)
1635 {
1636         struct hfi1_ibport *ibp = rcd_to_iport(rcd);
1637         struct rvt_ack_entry *e;
1638         unsigned long flags;
1639         u8 i, prev;
1640         int old_req;
1641
1642         trace_hfi1_rcv_error(qp, psn);
1643         if (diff > 0) {
1644                 /*
1645                  * Packet sequence error.
1646                  * A NAK will ACK earlier sends and RDMA writes.
1647                  * Don't queue the NAK if we already sent one.
1648                  */
1649                 if (!qp->r_nak_state) {
1650                         ibp->rvp.n_rc_seqnak++;
1651                         qp->r_nak_state = IB_NAK_PSN_ERROR;
1652                         /* Use the expected PSN. */
1653                         qp->r_ack_psn = qp->r_psn;
1654                         /*
1655                          * Wait to send the sequence NAK until all packets
1656                          * in the receive queue have been processed.
1657                          * Otherwise, we end up propagating congestion.
1658                          */
1659                         rc_defered_ack(rcd, qp);
1660                 }
1661                 goto done;
1662         }
1663
1664         /*
1665          * Handle a duplicate request.  Don't re-execute SEND, RDMA
1666          * write or atomic op.  Don't NAK errors, just silently drop
1667          * the duplicate request.  Note that r_sge, r_len, and
1668          * r_rcv_len may be in use so don't modify them.
1669          *
1670          * We are supposed to ACK the earliest duplicate PSN but we
1671          * can coalesce an outstanding duplicate ACK.  We have to
1672          * send the earliest so that RDMA reads can be restarted at
1673          * the requester's expected PSN.
1674          *
1675          * First, find where this duplicate PSN falls within the
1676          * ACKs previously sent.
1677          * old_req is true if there is an older response that is scheduled
1678          * to be sent before sending this one.
1679          */
1680         e = NULL;
1681         old_req = 1;
1682         ibp->rvp.n_rc_dupreq++;
1683
1684         spin_lock_irqsave(&qp->s_lock, flags);
1685
1686         for (i = qp->r_head_ack_queue; ; i = prev) {
1687                 if (i == qp->s_tail_ack_queue)
1688                         old_req = 0;
1689                 if (i)
1690                         prev = i - 1;
1691                 else
1692                         prev = HFI1_MAX_RDMA_ATOMIC;
1693                 if (prev == qp->r_head_ack_queue) {
1694                         e = NULL;
1695                         break;
1696                 }
1697                 e = &qp->s_ack_queue[prev];
1698                 if (!e->opcode) {
1699                         e = NULL;
1700                         break;
1701                 }
1702                 if (cmp_psn(psn, e->psn) >= 0) {
1703                         if (prev == qp->s_tail_ack_queue &&
1704                             cmp_psn(psn, e->lpsn) <= 0)
1705                                 old_req = 0;
1706                         break;
1707                 }
1708         }
1709         switch (opcode) {
1710         case OP(RDMA_READ_REQUEST): {
1711                 struct ib_reth *reth;
1712                 u32 offset;
1713                 u32 len;
1714
1715                 /*
1716                  * If we didn't find the RDMA read request in the ack queue,
1717                  * we can ignore this request.
1718                  */
1719                 if (!e || e->opcode != OP(RDMA_READ_REQUEST))
1720                         goto unlock_done;
1721                 /* RETH comes after BTH */
1722                 reth = &ohdr->u.rc.reth;
1723                 /*
1724                  * Address range must be a subset of the original
1725                  * request and start on pmtu boundaries.
1726                  * We reuse the old ack_queue slot since the requester
1727                  * should not back up and request an earlier PSN for the
1728                  * same request.
1729                  */
1730                 offset = delta_psn(psn, e->psn) * qp->pmtu;
1731                 len = be32_to_cpu(reth->length);
1732                 if (unlikely(offset + len != e->rdma_sge.sge_length))
1733                         goto unlock_done;
1734                 if (e->rdma_sge.mr) {
1735                         rvt_put_mr(e->rdma_sge.mr);
1736                         e->rdma_sge.mr = NULL;
1737                 }
1738                 if (len != 0) {
1739                         u32 rkey = be32_to_cpu(reth->rkey);
1740                         u64 vaddr = get_ib_reth_vaddr(reth);
1741                         int ok;
1742
1743                         ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr, rkey,
1744                                          IB_ACCESS_REMOTE_READ);
1745                         if (unlikely(!ok))
1746                                 goto unlock_done;
1747                 } else {
1748                         e->rdma_sge.vaddr = NULL;
1749                         e->rdma_sge.length = 0;
1750                         e->rdma_sge.sge_length = 0;
1751                 }
1752                 e->psn = psn;
1753                 if (old_req)
1754                         goto unlock_done;
1755                 qp->s_tail_ack_queue = prev;
1756                 break;
1757         }
1758
1759         case OP(COMPARE_SWAP):
1760         case OP(FETCH_ADD): {
1761                 /*
1762                  * If we didn't find the atomic request in the ack queue
1763                  * or the send engine is already backed up to send an
1764                  * earlier entry, we can ignore this request.
1765                  */
1766                 if (!e || e->opcode != (u8)opcode || old_req)
1767                         goto unlock_done;
1768                 qp->s_tail_ack_queue = prev;
1769                 break;
1770         }
1771
1772         default:
1773                 /*
1774                  * Ignore this operation if it doesn't request an ACK
1775                  * or an earlier RDMA read or atomic is going to be resent.
1776                  */
1777                 if (!(psn & IB_BTH_REQ_ACK) || old_req)
1778                         goto unlock_done;
1779                 /*
1780                  * Resend the most recent ACK if this request is
1781                  * after all the previous RDMA reads and atomics.
1782                  */
1783                 if (i == qp->r_head_ack_queue) {
1784                         spin_unlock_irqrestore(&qp->s_lock, flags);
1785                         qp->r_nak_state = 0;
1786                         qp->r_ack_psn = qp->r_psn - 1;
1787                         goto send_ack;
1788                 }
1789
1790                 /*
1791                  * Resend the RDMA read or atomic op which
1792                  * ACKs this duplicate request.
1793                  */
1794                 qp->s_tail_ack_queue = i;
1795                 break;
1796         }
1797         qp->s_ack_state = OP(ACKNOWLEDGE);
1798         qp->s_flags |= RVT_S_RESP_PENDING;
1799         qp->r_nak_state = 0;
1800         hfi1_schedule_send(qp);
1801
1802 unlock_done:
1803         spin_unlock_irqrestore(&qp->s_lock, flags);
1804 done:
1805         return 1;
1806
1807 send_ack:
1808         return 0;
1809 }
1810
1811 static inline void update_ack_queue(struct rvt_qp *qp, unsigned n)
1812 {
1813         unsigned next;
1814
1815         next = n + 1;
1816         if (next > HFI1_MAX_RDMA_ATOMIC)
1817                 next = 0;
1818         qp->s_tail_ack_queue = next;
1819         qp->s_ack_state = OP(ACKNOWLEDGE);
1820 }
1821
1822 static void log_cca_event(struct hfi1_pportdata *ppd, u8 sl, u32 rlid,
1823                           u32 lqpn, u32 rqpn, u8 svc_type)
1824 {
1825         struct opa_hfi1_cong_log_event_internal *cc_event;
1826         unsigned long flags;
1827
1828         if (sl >= OPA_MAX_SLS)
1829                 return;
1830
1831         spin_lock_irqsave(&ppd->cc_log_lock, flags);
1832
1833         ppd->threshold_cong_event_map[sl / 8] |= 1 << (sl % 8);
1834         ppd->threshold_event_counter++;
1835
1836         cc_event = &ppd->cc_events[ppd->cc_log_idx++];
1837         if (ppd->cc_log_idx == OPA_CONG_LOG_ELEMS)
1838                 ppd->cc_log_idx = 0;
1839         cc_event->lqpn = lqpn & RVT_QPN_MASK;
1840         cc_event->rqpn = rqpn & RVT_QPN_MASK;
1841         cc_event->sl = sl;
1842         cc_event->svc_type = svc_type;
1843         cc_event->rlid = rlid;
1844         /* keep timestamp in units of 1.024 usec */
1845         cc_event->timestamp = ktime_to_ns(ktime_get()) / 1024;
1846
1847         spin_unlock_irqrestore(&ppd->cc_log_lock, flags);
1848 }
1849
1850 void process_becn(struct hfi1_pportdata *ppd, u8 sl, u16 rlid, u32 lqpn,
1851                   u32 rqpn, u8 svc_type)
1852 {
1853         struct cca_timer *cca_timer;
1854         u16 ccti, ccti_incr, ccti_timer, ccti_limit;
1855         u8 trigger_threshold;
1856         struct cc_state *cc_state;
1857         unsigned long flags;
1858
1859         if (sl >= OPA_MAX_SLS)
1860                 return;
1861
1862         cc_state = get_cc_state(ppd);
1863
1864         if (!cc_state)
1865                 return;
1866
1867         /*
1868          * 1) increase CCTI (for this SL)
1869          * 2) select IPG (i.e., call set_link_ipg())
1870          * 3) start timer
1871          */
1872         ccti_limit = cc_state->cct.ccti_limit;
1873         ccti_incr = cc_state->cong_setting.entries[sl].ccti_increase;
1874         ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer;
1875         trigger_threshold =
1876                 cc_state->cong_setting.entries[sl].trigger_threshold;
1877
1878         spin_lock_irqsave(&ppd->cca_timer_lock, flags);
1879
1880         cca_timer = &ppd->cca_timer[sl];
1881         if (cca_timer->ccti < ccti_limit) {
1882                 if (cca_timer->ccti + ccti_incr <= ccti_limit)
1883                         cca_timer->ccti += ccti_incr;
1884                 else
1885                         cca_timer->ccti = ccti_limit;
1886                 set_link_ipg(ppd);
1887         }
1888
1889         ccti = cca_timer->ccti;
1890
1891         if (!hrtimer_active(&cca_timer->hrtimer)) {
1892                 /* ccti_timer is in units of 1.024 usec */
1893                 unsigned long nsec = 1024 * ccti_timer;
1894
1895                 hrtimer_start(&cca_timer->hrtimer, ns_to_ktime(nsec),
1896                               HRTIMER_MODE_REL);
1897         }
1898
1899         spin_unlock_irqrestore(&ppd->cca_timer_lock, flags);
1900
1901         if ((trigger_threshold != 0) && (ccti >= trigger_threshold))
1902                 log_cca_event(ppd, sl, rlid, lqpn, rqpn, svc_type);
1903 }
1904
1905 /**
1906  * hfi1_rc_rcv - process an incoming RC packet
1907  * @rcd: the context pointer
1908  * @hdr: the header of this packet
1909  * @rcv_flags: flags relevant to rcv processing
1910  * @data: the packet data
1911  * @tlen: the packet length
1912  * @qp: the QP for this packet
1913  *
1914  * This is called from qp_rcv() to process an incoming RC packet
1915  * for the given QP.
1916  * May be called at interrupt level.
1917  */
1918 void hfi1_rc_rcv(struct hfi1_packet *packet)
1919 {
1920         struct hfi1_ctxtdata *rcd = packet->rcd;
1921         struct ib_header *hdr = packet->hdr;
1922         u32 rcv_flags = packet->rcv_flags;
1923         void *data = packet->ebuf;
1924         u32 tlen = packet->tlen;
1925         struct rvt_qp *qp = packet->qp;
1926         struct hfi1_ibport *ibp = rcd_to_iport(rcd);
1927         struct ib_other_headers *ohdr = packet->ohdr;
1928         u32 bth0, opcode;
1929         u32 hdrsize = packet->hlen;
1930         u32 psn;
1931         u32 pad;
1932         struct ib_wc wc;
1933         u32 pmtu = qp->pmtu;
1934         int diff;
1935         struct ib_reth *reth;
1936         unsigned long flags;
1937         int ret;
1938         bool is_fecn = false;
1939         bool copy_last = false;
1940         u32 rkey;
1941
1942         lockdep_assert_held(&qp->r_lock);
1943         bth0 = be32_to_cpu(ohdr->bth[0]);
1944         if (hfi1_ruc_check_hdr(ibp, hdr, rcv_flags & HFI1_HAS_GRH, qp, bth0))
1945                 return;
1946
1947         is_fecn = process_ecn(qp, packet, false);
1948
1949         psn = be32_to_cpu(ohdr->bth[2]);
1950         opcode = ib_bth_get_opcode(ohdr);
1951
1952         /*
1953          * Process responses (ACKs) before anything else.  Note that the
1954          * packet sequence number will be for something in the send work
1955          * queue rather than the expected receive packet sequence number.
1956          * In other words, this QP is the requester.
1957          */
1958         if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
1959             opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
1960                 rc_rcv_resp(ibp, ohdr, data, tlen, qp, opcode, psn,
1961                             hdrsize, pmtu, rcd);
1962                 if (is_fecn)
1963                         goto send_ack;
1964                 return;
1965         }
1966
1967         /* Compute 24 bits worth of difference. */
1968         diff = delta_psn(psn, qp->r_psn);
1969         if (unlikely(diff)) {
1970                 if (rc_rcv_error(ohdr, data, qp, opcode, psn, diff, rcd))
1971                         return;
1972                 goto send_ack;
1973         }
1974
1975         /* Check for opcode sequence errors. */
1976         switch (qp->r_state) {
1977         case OP(SEND_FIRST):
1978         case OP(SEND_MIDDLE):
1979                 if (opcode == OP(SEND_MIDDLE) ||
1980                     opcode == OP(SEND_LAST) ||
1981                     opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
1982                     opcode == OP(SEND_LAST_WITH_INVALIDATE))
1983                         break;
1984                 goto nack_inv;
1985
1986         case OP(RDMA_WRITE_FIRST):
1987         case OP(RDMA_WRITE_MIDDLE):
1988                 if (opcode == OP(RDMA_WRITE_MIDDLE) ||
1989                     opcode == OP(RDMA_WRITE_LAST) ||
1990                     opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
1991                         break;
1992                 goto nack_inv;
1993
1994         default:
1995                 if (opcode == OP(SEND_MIDDLE) ||
1996                     opcode == OP(SEND_LAST) ||
1997                     opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
1998                     opcode == OP(SEND_LAST_WITH_INVALIDATE) ||
1999                     opcode == OP(RDMA_WRITE_MIDDLE) ||
2000                     opcode == OP(RDMA_WRITE_LAST) ||
2001                     opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
2002                         goto nack_inv;
2003                 /*
2004                  * Note that it is up to the requester to not send a new
2005                  * RDMA read or atomic operation before receiving an ACK
2006                  * for the previous operation.
2007                  */
2008                 break;
2009         }
2010
2011         if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
2012                 rvt_comm_est(qp);
2013
2014         /* OK, process the packet. */
2015         switch (opcode) {
2016         case OP(SEND_FIRST):
2017                 ret = hfi1_rvt_get_rwqe(qp, 0);
2018                 if (ret < 0)
2019                         goto nack_op_err;
2020                 if (!ret)
2021                         goto rnr_nak;
2022                 qp->r_rcv_len = 0;
2023                 /* FALLTHROUGH */
2024         case OP(SEND_MIDDLE):
2025         case OP(RDMA_WRITE_MIDDLE):
2026 send_middle:
2027                 /* Check for invalid length PMTU or posted rwqe len. */
2028                 if (unlikely(tlen != (hdrsize + pmtu + 4)))
2029                         goto nack_inv;
2030                 qp->r_rcv_len += pmtu;
2031                 if (unlikely(qp->r_rcv_len > qp->r_len))
2032                         goto nack_inv;
2033                 hfi1_copy_sge(&qp->r_sge, data, pmtu, true, false);
2034                 break;
2035
2036         case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
2037                 /* consume RWQE */
2038                 ret = hfi1_rvt_get_rwqe(qp, 1);
2039                 if (ret < 0)
2040                         goto nack_op_err;
2041                 if (!ret)
2042                         goto rnr_nak;
2043                 goto send_last_imm;
2044
2045         case OP(SEND_ONLY):
2046         case OP(SEND_ONLY_WITH_IMMEDIATE):
2047         case OP(SEND_ONLY_WITH_INVALIDATE):
2048                 ret = hfi1_rvt_get_rwqe(qp, 0);
2049                 if (ret < 0)
2050                         goto nack_op_err;
2051                 if (!ret)
2052                         goto rnr_nak;
2053                 qp->r_rcv_len = 0;
2054                 if (opcode == OP(SEND_ONLY))
2055                         goto no_immediate_data;
2056                 if (opcode == OP(SEND_ONLY_WITH_INVALIDATE))
2057                         goto send_last_inv;
2058                 /* FALLTHROUGH for SEND_ONLY_WITH_IMMEDIATE */
2059         case OP(SEND_LAST_WITH_IMMEDIATE):
2060 send_last_imm:
2061                 wc.ex.imm_data = ohdr->u.imm_data;
2062                 wc.wc_flags = IB_WC_WITH_IMM;
2063                 goto send_last;
2064         case OP(SEND_LAST_WITH_INVALIDATE):
2065 send_last_inv:
2066                 rkey = be32_to_cpu(ohdr->u.ieth);
2067                 if (rvt_invalidate_rkey(qp, rkey))
2068                         goto no_immediate_data;
2069                 wc.ex.invalidate_rkey = rkey;
2070                 wc.wc_flags = IB_WC_WITH_INVALIDATE;
2071                 goto send_last;
2072         case OP(RDMA_WRITE_LAST):
2073                 copy_last = rvt_is_user_qp(qp);
2074                 /* fall through */
2075         case OP(SEND_LAST):
2076 no_immediate_data:
2077                 wc.wc_flags = 0;
2078                 wc.ex.imm_data = 0;
2079 send_last:
2080                 /* Get the number of bytes the message was padded by. */
2081                 pad = ib_bth_get_pad(ohdr);
2082                 /* Check for invalid length. */
2083                 /* LAST len should be >= 1 */
2084                 if (unlikely(tlen < (hdrsize + pad + 4)))
2085                         goto nack_inv;
2086                 /* Don't count the CRC. */
2087                 tlen -= (hdrsize + pad + 4);
2088                 wc.byte_len = tlen + qp->r_rcv_len;
2089                 if (unlikely(wc.byte_len > qp->r_len))
2090                         goto nack_inv;
2091                 hfi1_copy_sge(&qp->r_sge, data, tlen, true, copy_last);
2092                 rvt_put_ss(&qp->r_sge);
2093                 qp->r_msn++;
2094                 if (!__test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
2095                         break;
2096                 wc.wr_id = qp->r_wr_id;
2097                 wc.status = IB_WC_SUCCESS;
2098                 if (opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE) ||
2099                     opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
2100                         wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
2101                 else
2102                         wc.opcode = IB_WC_RECV;
2103                 wc.qp = &qp->ibqp;
2104                 wc.src_qp = qp->remote_qpn;
2105                 wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr);
2106                 /*
2107                  * It seems that IB mandates the presence of an SL in a
2108                  * work completion only for the UD transport (see section
2109                  * 11.4.2 of IBTA Vol. 1).
2110                  *
2111                  * However, the way the SL is chosen below is consistent
2112                  * with the way that IB/qib works and is trying avoid
2113                  * introducing incompatibilities.
2114                  *
2115                  * See also OPA Vol. 1, section 9.7.6, and table 9-17.
2116                  */
2117                 wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
2118                 /* zero fields that are N/A */
2119                 wc.vendor_err = 0;
2120                 wc.pkey_index = 0;
2121                 wc.dlid_path_bits = 0;
2122                 wc.port_num = 0;
2123                 /* Signal completion event if the solicited bit is set. */
2124                 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
2125                              (bth0 & IB_BTH_SOLICITED) != 0);
2126                 break;
2127
2128         case OP(RDMA_WRITE_ONLY):
2129                 copy_last = rvt_is_user_qp(qp);
2130                 /* fall through */
2131         case OP(RDMA_WRITE_FIRST):
2132         case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
2133                 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
2134                         goto nack_inv;
2135                 /* consume RWQE */
2136                 reth = &ohdr->u.rc.reth;
2137                 qp->r_len = be32_to_cpu(reth->length);
2138                 qp->r_rcv_len = 0;
2139                 qp->r_sge.sg_list = NULL;
2140                 if (qp->r_len != 0) {
2141                         u32 rkey = be32_to_cpu(reth->rkey);
2142                         u64 vaddr = get_ib_reth_vaddr(reth);
2143                         int ok;
2144
2145                         /* Check rkey & NAK */
2146                         ok = rvt_rkey_ok(qp, &qp->r_sge.sge, qp->r_len, vaddr,
2147                                          rkey, IB_ACCESS_REMOTE_WRITE);
2148                         if (unlikely(!ok))
2149                                 goto nack_acc;
2150                         qp->r_sge.num_sge = 1;
2151                 } else {
2152                         qp->r_sge.num_sge = 0;
2153                         qp->r_sge.sge.mr = NULL;
2154                         qp->r_sge.sge.vaddr = NULL;
2155                         qp->r_sge.sge.length = 0;
2156                         qp->r_sge.sge.sge_length = 0;
2157                 }
2158                 if (opcode == OP(RDMA_WRITE_FIRST))
2159                         goto send_middle;
2160                 else if (opcode == OP(RDMA_WRITE_ONLY))
2161                         goto no_immediate_data;
2162                 ret = hfi1_rvt_get_rwqe(qp, 1);
2163                 if (ret < 0)
2164                         goto nack_op_err;
2165                 if (!ret)
2166                         goto rnr_nak;
2167                 wc.ex.imm_data = ohdr->u.rc.imm_data;
2168                 wc.wc_flags = IB_WC_WITH_IMM;
2169                 goto send_last;
2170
2171         case OP(RDMA_READ_REQUEST): {
2172                 struct rvt_ack_entry *e;
2173                 u32 len;
2174                 u8 next;
2175
2176                 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
2177                         goto nack_inv;
2178                 next = qp->r_head_ack_queue + 1;
2179                 /* s_ack_queue is size HFI1_MAX_RDMA_ATOMIC+1 so use > not >= */
2180                 if (next > HFI1_MAX_RDMA_ATOMIC)
2181                         next = 0;
2182                 spin_lock_irqsave(&qp->s_lock, flags);
2183                 if (unlikely(next == qp->s_tail_ack_queue)) {
2184                         if (!qp->s_ack_queue[next].sent)
2185                                 goto nack_inv_unlck;
2186                         update_ack_queue(qp, next);
2187                 }
2188                 e = &qp->s_ack_queue[qp->r_head_ack_queue];
2189                 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
2190                         rvt_put_mr(e->rdma_sge.mr);
2191                         e->rdma_sge.mr = NULL;
2192                 }
2193                 reth = &ohdr->u.rc.reth;
2194                 len = be32_to_cpu(reth->length);
2195                 if (len) {
2196                         u32 rkey = be32_to_cpu(reth->rkey);
2197                         u64 vaddr = get_ib_reth_vaddr(reth);
2198                         int ok;
2199
2200                         /* Check rkey & NAK */
2201                         ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr,
2202                                          rkey, IB_ACCESS_REMOTE_READ);
2203                         if (unlikely(!ok))
2204                                 goto nack_acc_unlck;
2205                         /*
2206                          * Update the next expected PSN.  We add 1 later
2207                          * below, so only add the remainder here.
2208                          */
2209                         qp->r_psn += rvt_div_mtu(qp, len - 1);
2210                 } else {
2211                         e->rdma_sge.mr = NULL;
2212                         e->rdma_sge.vaddr = NULL;
2213                         e->rdma_sge.length = 0;
2214                         e->rdma_sge.sge_length = 0;
2215                 }
2216                 e->opcode = opcode;
2217                 e->sent = 0;
2218                 e->psn = psn;
2219                 e->lpsn = qp->r_psn;
2220                 /*
2221                  * We need to increment the MSN here instead of when we
2222                  * finish sending the result since a duplicate request would
2223                  * increment it more than once.
2224                  */
2225                 qp->r_msn++;
2226                 qp->r_psn++;
2227                 qp->r_state = opcode;
2228                 qp->r_nak_state = 0;
2229                 qp->r_head_ack_queue = next;
2230
2231                 /* Schedule the send engine. */
2232                 qp->s_flags |= RVT_S_RESP_PENDING;
2233                 hfi1_schedule_send(qp);
2234
2235                 spin_unlock_irqrestore(&qp->s_lock, flags);
2236                 if (is_fecn)
2237                         goto send_ack;
2238                 return;
2239         }
2240
2241         case OP(COMPARE_SWAP):
2242         case OP(FETCH_ADD): {
2243                 struct ib_atomic_eth *ateth;
2244                 struct rvt_ack_entry *e;
2245                 u64 vaddr;
2246                 atomic64_t *maddr;
2247                 u64 sdata;
2248                 u32 rkey;
2249                 u8 next;
2250
2251                 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
2252                         goto nack_inv;
2253                 next = qp->r_head_ack_queue + 1;
2254                 if (next > HFI1_MAX_RDMA_ATOMIC)
2255                         next = 0;
2256                 spin_lock_irqsave(&qp->s_lock, flags);
2257                 if (unlikely(next == qp->s_tail_ack_queue)) {
2258                         if (!qp->s_ack_queue[next].sent)
2259                                 goto nack_inv_unlck;
2260                         update_ack_queue(qp, next);
2261                 }
2262                 e = &qp->s_ack_queue[qp->r_head_ack_queue];
2263                 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
2264                         rvt_put_mr(e->rdma_sge.mr);
2265                         e->rdma_sge.mr = NULL;
2266                 }
2267                 ateth = &ohdr->u.atomic_eth;
2268                 vaddr = get_ib_ateth_vaddr(ateth);
2269                 if (unlikely(vaddr & (sizeof(u64) - 1)))
2270                         goto nack_inv_unlck;
2271                 rkey = be32_to_cpu(ateth->rkey);
2272                 /* Check rkey & NAK */
2273                 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
2274                                           vaddr, rkey,
2275                                           IB_ACCESS_REMOTE_ATOMIC)))
2276                         goto nack_acc_unlck;
2277                 /* Perform atomic OP and save result. */
2278                 maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
2279                 sdata = get_ib_ateth_swap(ateth);
2280                 e->atomic_data = (opcode == OP(FETCH_ADD)) ?
2281                         (u64)atomic64_add_return(sdata, maddr) - sdata :
2282                         (u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
2283                                       get_ib_ateth_compare(ateth),
2284                                       sdata);
2285                 rvt_put_mr(qp->r_sge.sge.mr);
2286                 qp->r_sge.num_sge = 0;
2287                 e->opcode = opcode;
2288                 e->sent = 0;
2289                 e->psn = psn;
2290                 e->lpsn = psn;
2291                 qp->r_msn++;
2292                 qp->r_psn++;
2293                 qp->r_state = opcode;
2294                 qp->r_nak_state = 0;
2295                 qp->r_head_ack_queue = next;
2296
2297                 /* Schedule the send engine. */
2298                 qp->s_flags |= RVT_S_RESP_PENDING;
2299                 hfi1_schedule_send(qp);
2300
2301                 spin_unlock_irqrestore(&qp->s_lock, flags);
2302                 if (is_fecn)
2303                         goto send_ack;
2304                 return;
2305         }
2306
2307         default:
2308                 /* NAK unknown opcodes. */
2309                 goto nack_inv;
2310         }
2311         qp->r_psn++;
2312         qp->r_state = opcode;
2313         qp->r_ack_psn = psn;
2314         qp->r_nak_state = 0;
2315         /* Send an ACK if requested or required. */
2316         if (psn & IB_BTH_REQ_ACK) {
2317                 struct hfi1_qp_priv *priv = qp->priv;
2318
2319                 if (packet->numpkt == 0) {
2320                         rc_cancel_ack(qp);
2321                         goto send_ack;
2322                 }
2323                 if (priv->r_adefered >= HFI1_PSN_CREDIT) {
2324                         rc_cancel_ack(qp);
2325                         goto send_ack;
2326                 }
2327                 if (unlikely(is_fecn)) {
2328                         rc_cancel_ack(qp);
2329                         goto send_ack;
2330                 }
2331                 priv->r_adefered++;
2332                 rc_defered_ack(rcd, qp);
2333         }
2334         return;
2335
2336 rnr_nak:
2337         qp->r_nak_state = qp->r_min_rnr_timer | IB_RNR_NAK;
2338         qp->r_ack_psn = qp->r_psn;
2339         /* Queue RNR NAK for later */
2340         rc_defered_ack(rcd, qp);
2341         return;
2342
2343 nack_op_err:
2344         rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
2345         qp->r_nak_state = IB_NAK_REMOTE_OPERATIONAL_ERROR;
2346         qp->r_ack_psn = qp->r_psn;
2347         /* Queue NAK for later */
2348         rc_defered_ack(rcd, qp);
2349         return;
2350
2351 nack_inv_unlck:
2352         spin_unlock_irqrestore(&qp->s_lock, flags);
2353 nack_inv:
2354         rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
2355         qp->r_nak_state = IB_NAK_INVALID_REQUEST;
2356         qp->r_ack_psn = qp->r_psn;
2357         /* Queue NAK for later */
2358         rc_defered_ack(rcd, qp);
2359         return;
2360
2361 nack_acc_unlck:
2362         spin_unlock_irqrestore(&qp->s_lock, flags);
2363 nack_acc:
2364         rvt_rc_error(qp, IB_WC_LOC_PROT_ERR);
2365         qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
2366         qp->r_ack_psn = qp->r_psn;
2367 send_ack:
2368         hfi1_send_rc_ack(rcd, qp, is_fecn);
2369 }
2370
2371 void hfi1_rc_hdrerr(
2372         struct hfi1_ctxtdata *rcd,
2373         struct ib_header *hdr,
2374         u32 rcv_flags,
2375         struct rvt_qp *qp)
2376 {
2377         int has_grh = rcv_flags & HFI1_HAS_GRH;
2378         struct ib_other_headers *ohdr;
2379         struct hfi1_ibport *ibp = rcd_to_iport(rcd);
2380         int diff;
2381         u32 opcode;
2382         u32 psn, bth0;
2383
2384         /* Check for GRH */
2385         ohdr = &hdr->u.oth;
2386         if (has_grh)
2387                 ohdr = &hdr->u.l.oth;
2388
2389         bth0 = be32_to_cpu(ohdr->bth[0]);
2390         if (hfi1_ruc_check_hdr(ibp, hdr, has_grh, qp, bth0))
2391                 return;
2392
2393         psn = be32_to_cpu(ohdr->bth[2]);
2394         opcode = ib_bth_get_opcode(ohdr);
2395
2396         /* Only deal with RDMA Writes for now */
2397         if (opcode < IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST) {
2398                 diff = delta_psn(psn, qp->r_psn);
2399                 if (!qp->r_nak_state && diff >= 0) {
2400                         ibp->rvp.n_rc_seqnak++;
2401                         qp->r_nak_state = IB_NAK_PSN_ERROR;
2402                         /* Use the expected PSN. */
2403                         qp->r_ack_psn = qp->r_psn;
2404                         /*
2405                          * Wait to send the sequence
2406                          * NAK until all packets
2407                          * in the receive queue have
2408                          * been processed.
2409                          * Otherwise, we end up
2410                          * propagating congestion.
2411                          */
2412                         rc_defered_ack(rcd, qp);
2413                 } /* Out of sequence NAK */
2414         } /* QP Request NAKs */
2415 }