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