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