pandora: defconfig: update
[pandora-kernel.git] / drivers / infiniband / hw / qib / qib_rc.c
1 /*
2  * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
3  * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #include <linux/io.h>
35
36 #include "qib.h"
37
38 /* cut down ridiculously long IB macro names */
39 #define OP(x) IB_OPCODE_RC_##x
40
41 static void rc_timeout(unsigned long arg);
42
43 static u32 restart_sge(struct qib_sge_state *ss, struct qib_swqe *wqe,
44                        u32 psn, u32 pmtu)
45 {
46         u32 len;
47
48         len = ((psn - wqe->psn) & QIB_PSN_MASK) * pmtu;
49         ss->sge = wqe->sg_list[0];
50         ss->sg_list = wqe->sg_list + 1;
51         ss->num_sge = wqe->wr.num_sge;
52         ss->total_len = wqe->length;
53         qib_skip_sge(ss, len, 0);
54         return wqe->length - len;
55 }
56
57 static void start_timer(struct qib_qp *qp)
58 {
59         qp->s_flags |= QIB_S_TIMER;
60         qp->s_timer.function = rc_timeout;
61         /* 4.096 usec. * (1 << qp->timeout) */
62         qp->s_timer.expires = jiffies + qp->timeout_jiffies;
63         add_timer(&qp->s_timer);
64 }
65
66 /**
67  * qib_make_rc_ack - construct a response packet (ACK, NAK, or RDMA read)
68  * @dev: the device for this QP
69  * @qp: a pointer to the QP
70  * @ohdr: a pointer to the IB header being constructed
71  * @pmtu: the path MTU
72  *
73  * Return 1 if constructed; otherwise, return 0.
74  * Note that we are in the responder's side of the QP context.
75  * Note the QP s_lock must be held.
76  */
77 static int qib_make_rc_ack(struct qib_ibdev *dev, struct qib_qp *qp,
78                            struct qib_other_headers *ohdr, u32 pmtu)
79 {
80         struct qib_ack_entry *e;
81         u32 hwords;
82         u32 len;
83         u32 bth0;
84         u32 bth2;
85
86         /* Don't send an ACK if we aren't supposed to. */
87         if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK))
88                 goto bail;
89
90         /* header size in 32-bit words LRH+BTH = (8+12)/4. */
91         hwords = 5;
92
93         switch (qp->s_ack_state) {
94         case OP(RDMA_READ_RESPONSE_LAST):
95         case OP(RDMA_READ_RESPONSE_ONLY):
96                 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
97                 if (e->rdma_sge.mr) {
98                         atomic_dec(&e->rdma_sge.mr->refcount);
99                         e->rdma_sge.mr = NULL;
100                 }
101                 /* FALLTHROUGH */
102         case OP(ATOMIC_ACKNOWLEDGE):
103                 /*
104                  * We can increment the tail pointer now that the last
105                  * response has been sent instead of only being
106                  * constructed.
107                  */
108                 if (++qp->s_tail_ack_queue > QIB_MAX_RDMA_ATOMIC)
109                         qp->s_tail_ack_queue = 0;
110                 /* FALLTHROUGH */
111         case OP(SEND_ONLY):
112         case OP(ACKNOWLEDGE):
113                 /* Check for no next entry in the queue. */
114                 if (qp->r_head_ack_queue == qp->s_tail_ack_queue) {
115                         if (qp->s_flags & QIB_S_ACK_PENDING)
116                                 goto normal;
117                         goto bail;
118                 }
119
120                 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
121                 if (e->opcode == OP(RDMA_READ_REQUEST)) {
122                         /*
123                          * If a RDMA read response is being resent and
124                          * we haven't seen the duplicate request yet,
125                          * then stop sending the remaining responses the
126                          * responder has seen until the requester resends it.
127                          */
128                         len = e->rdma_sge.sge_length;
129                         if (len && !e->rdma_sge.mr) {
130                                 qp->s_tail_ack_queue = qp->r_head_ack_queue;
131                                 goto bail;
132                         }
133                         /* Copy SGE state in case we need to resend */
134                         qp->s_rdma_mr = e->rdma_sge.mr;
135                         if (qp->s_rdma_mr)
136                                 atomic_inc(&qp->s_rdma_mr->refcount);
137                         qp->s_ack_rdma_sge.sge = e->rdma_sge;
138                         qp->s_ack_rdma_sge.num_sge = 1;
139                         qp->s_cur_sge = &qp->s_ack_rdma_sge;
140                         if (len > pmtu) {
141                                 len = pmtu;
142                                 qp->s_ack_state = OP(RDMA_READ_RESPONSE_FIRST);
143                         } else {
144                                 qp->s_ack_state = OP(RDMA_READ_RESPONSE_ONLY);
145                                 e->sent = 1;
146                         }
147                         ohdr->u.aeth = qib_compute_aeth(qp);
148                         hwords++;
149                         qp->s_ack_rdma_psn = e->psn;
150                         bth2 = qp->s_ack_rdma_psn++ & QIB_PSN_MASK;
151                 } else {
152                         /* COMPARE_SWAP or FETCH_ADD */
153                         qp->s_cur_sge = NULL;
154                         len = 0;
155                         qp->s_ack_state = OP(ATOMIC_ACKNOWLEDGE);
156                         ohdr->u.at.aeth = qib_compute_aeth(qp);
157                         ohdr->u.at.atomic_ack_eth[0] =
158                                 cpu_to_be32(e->atomic_data >> 32);
159                         ohdr->u.at.atomic_ack_eth[1] =
160                                 cpu_to_be32(e->atomic_data);
161                         hwords += sizeof(ohdr->u.at) / sizeof(u32);
162                         bth2 = e->psn & QIB_PSN_MASK;
163                         e->sent = 1;
164                 }
165                 bth0 = qp->s_ack_state << 24;
166                 break;
167
168         case OP(RDMA_READ_RESPONSE_FIRST):
169                 qp->s_ack_state = OP(RDMA_READ_RESPONSE_MIDDLE);
170                 /* FALLTHROUGH */
171         case OP(RDMA_READ_RESPONSE_MIDDLE):
172                 qp->s_cur_sge = &qp->s_ack_rdma_sge;
173                 qp->s_rdma_mr = qp->s_ack_rdma_sge.sge.mr;
174                 if (qp->s_rdma_mr)
175                         atomic_inc(&qp->s_rdma_mr->refcount);
176                 len = qp->s_ack_rdma_sge.sge.sge_length;
177                 if (len > pmtu)
178                         len = pmtu;
179                 else {
180                         ohdr->u.aeth = qib_compute_aeth(qp);
181                         hwords++;
182                         qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
183                         e = &qp->s_ack_queue[qp->s_tail_ack_queue];
184                         e->sent = 1;
185                 }
186                 bth0 = qp->s_ack_state << 24;
187                 bth2 = qp->s_ack_rdma_psn++ & QIB_PSN_MASK;
188                 break;
189
190         default:
191 normal:
192                 /*
193                  * Send a regular ACK.
194                  * Set the s_ack_state so we wait until after sending
195                  * the ACK before setting s_ack_state to ACKNOWLEDGE
196                  * (see above).
197                  */
198                 qp->s_ack_state = OP(SEND_ONLY);
199                 qp->s_flags &= ~QIB_S_ACK_PENDING;
200                 qp->s_cur_sge = NULL;
201                 if (qp->s_nak_state)
202                         ohdr->u.aeth =
203                                 cpu_to_be32((qp->r_msn & QIB_MSN_MASK) |
204                                             (qp->s_nak_state <<
205                                              QIB_AETH_CREDIT_SHIFT));
206                 else
207                         ohdr->u.aeth = qib_compute_aeth(qp);
208                 hwords++;
209                 len = 0;
210                 bth0 = OP(ACKNOWLEDGE) << 24;
211                 bth2 = qp->s_ack_psn & QIB_PSN_MASK;
212         }
213         qp->s_rdma_ack_cnt++;
214         qp->s_hdrwords = hwords;
215         qp->s_cur_size = len;
216         qib_make_ruc_header(qp, ohdr, bth0, bth2);
217         return 1;
218
219 bail:
220         qp->s_ack_state = OP(ACKNOWLEDGE);
221         qp->s_flags &= ~(QIB_S_RESP_PENDING | QIB_S_ACK_PENDING);
222         return 0;
223 }
224
225 /**
226  * qib_make_rc_req - construct a request packet (SEND, RDMA r/w, ATOMIC)
227  * @qp: a pointer to the QP
228  *
229  * Return 1 if constructed; otherwise, return 0.
230  */
231 int qib_make_rc_req(struct qib_qp *qp)
232 {
233         struct qib_ibdev *dev = to_idev(qp->ibqp.device);
234         struct qib_other_headers *ohdr;
235         struct qib_sge_state *ss;
236         struct qib_swqe *wqe;
237         u32 hwords;
238         u32 len;
239         u32 bth0;
240         u32 bth2;
241         u32 pmtu = qp->pmtu;
242         char newreq;
243         unsigned long flags;
244         int ret = 0;
245         int delta;
246
247         ohdr = &qp->s_hdr.u.oth;
248         if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
249                 ohdr = &qp->s_hdr.u.l.oth;
250
251         /*
252          * The lock is needed to synchronize between the sending tasklet,
253          * the receive interrupt handler, and timeout resends.
254          */
255         spin_lock_irqsave(&qp->s_lock, flags);
256
257         /* Sending responses has higher priority over sending requests. */
258         if ((qp->s_flags & QIB_S_RESP_PENDING) &&
259             qib_make_rc_ack(dev, qp, ohdr, pmtu))
260                 goto done;
261
262         if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_SEND_OK)) {
263                 if (!(ib_qib_state_ops[qp->state] & QIB_FLUSH_SEND))
264                         goto bail;
265                 /* We are in the error state, flush the work request. */
266                 if (qp->s_last == qp->s_head)
267                         goto bail;
268                 /* If DMAs are in progress, we can't flush immediately. */
269                 if (atomic_read(&qp->s_dma_busy)) {
270                         qp->s_flags |= QIB_S_WAIT_DMA;
271                         goto bail;
272                 }
273                 wqe = get_swqe_ptr(qp, qp->s_last);
274                 qib_send_complete(qp, wqe, qp->s_last != qp->s_acked ?
275                         IB_WC_SUCCESS : IB_WC_WR_FLUSH_ERR);
276                 /* will get called again */
277                 goto done;
278         }
279
280         if (qp->s_flags & (QIB_S_WAIT_RNR | QIB_S_WAIT_ACK))
281                 goto bail;
282
283         if (qib_cmp24(qp->s_psn, qp->s_sending_hpsn) <= 0) {
284                 if (qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) <= 0) {
285                         qp->s_flags |= QIB_S_WAIT_PSN;
286                         goto bail;
287                 }
288                 qp->s_sending_psn = qp->s_psn;
289                 qp->s_sending_hpsn = qp->s_psn - 1;
290         }
291
292         /* header size in 32-bit words LRH+BTH = (8+12)/4. */
293         hwords = 5;
294         bth0 = 0;
295
296         /* Send a request. */
297         wqe = get_swqe_ptr(qp, qp->s_cur);
298         switch (qp->s_state) {
299         default:
300                 if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_NEXT_SEND_OK))
301                         goto bail;
302                 /*
303                  * Resend an old request or start a new one.
304                  *
305                  * We keep track of the current SWQE so that
306                  * we don't reset the "furthest progress" state
307                  * if we need to back up.
308                  */
309                 newreq = 0;
310                 if (qp->s_cur == qp->s_tail) {
311                         /* Check if send work queue is empty. */
312                         if (qp->s_tail == qp->s_head)
313                                 goto bail;
314                         /*
315                          * If a fence is requested, wait for previous
316                          * RDMA read and atomic operations to finish.
317                          */
318                         if ((wqe->wr.send_flags & IB_SEND_FENCE) &&
319                             qp->s_num_rd_atomic) {
320                                 qp->s_flags |= QIB_S_WAIT_FENCE;
321                                 goto bail;
322                         }
323                         wqe->psn = qp->s_next_psn;
324                         newreq = 1;
325                 }
326                 /*
327                  * Note that we have to be careful not to modify the
328                  * original work request since we may need to resend
329                  * it.
330                  */
331                 len = wqe->length;
332                 ss = &qp->s_sge;
333                 bth2 = qp->s_psn & QIB_PSN_MASK;
334                 switch (wqe->wr.opcode) {
335                 case IB_WR_SEND:
336                 case IB_WR_SEND_WITH_IMM:
337                         /* If no credit, return. */
338                         if (!(qp->s_flags & QIB_S_UNLIMITED_CREDIT) &&
339                             qib_cmp24(wqe->ssn, qp->s_lsn + 1) > 0) {
340                                 qp->s_flags |= QIB_S_WAIT_SSN_CREDIT;
341                                 goto bail;
342                         }
343                         wqe->lpsn = wqe->psn;
344                         if (len > pmtu) {
345                                 wqe->lpsn += (len - 1) / pmtu;
346                                 qp->s_state = OP(SEND_FIRST);
347                                 len = pmtu;
348                                 break;
349                         }
350                         if (wqe->wr.opcode == IB_WR_SEND)
351                                 qp->s_state = OP(SEND_ONLY);
352                         else {
353                                 qp->s_state = OP(SEND_ONLY_WITH_IMMEDIATE);
354                                 /* Immediate data comes after the BTH */
355                                 ohdr->u.imm_data = wqe->wr.ex.imm_data;
356                                 hwords += 1;
357                         }
358                         if (wqe->wr.send_flags & IB_SEND_SOLICITED)
359                                 bth0 |= IB_BTH_SOLICITED;
360                         bth2 |= IB_BTH_REQ_ACK;
361                         if (++qp->s_cur == qp->s_size)
362                                 qp->s_cur = 0;
363                         break;
364
365                 case IB_WR_RDMA_WRITE:
366                         if (newreq && !(qp->s_flags & QIB_S_UNLIMITED_CREDIT))
367                                 qp->s_lsn++;
368                         goto no_flow_control;
369                 case IB_WR_RDMA_WRITE_WITH_IMM:
370                         /* If no credit, return. */
371                         if (!(qp->s_flags & QIB_S_UNLIMITED_CREDIT) &&
372                             qib_cmp24(wqe->ssn, qp->s_lsn + 1) > 0) {
373                                 qp->s_flags |= QIB_S_WAIT_SSN_CREDIT;
374                                 goto bail;
375                         }
376 no_flow_control:
377                         ohdr->u.rc.reth.vaddr =
378                                 cpu_to_be64(wqe->wr.wr.rdma.remote_addr);
379                         ohdr->u.rc.reth.rkey =
380                                 cpu_to_be32(wqe->wr.wr.rdma.rkey);
381                         ohdr->u.rc.reth.length = cpu_to_be32(len);
382                         hwords += sizeof(struct ib_reth) / sizeof(u32);
383                         wqe->lpsn = wqe->psn;
384                         if (len > pmtu) {
385                                 wqe->lpsn += (len - 1) / pmtu;
386                                 qp->s_state = OP(RDMA_WRITE_FIRST);
387                                 len = pmtu;
388                                 break;
389                         }
390                         if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
391                                 qp->s_state = OP(RDMA_WRITE_ONLY);
392                         else {
393                                 qp->s_state =
394                                         OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
395                                 /* Immediate data comes after RETH */
396                                 ohdr->u.rc.imm_data = wqe->wr.ex.imm_data;
397                                 hwords += 1;
398                                 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
399                                         bth0 |= IB_BTH_SOLICITED;
400                         }
401                         bth2 |= IB_BTH_REQ_ACK;
402                         if (++qp->s_cur == qp->s_size)
403                                 qp->s_cur = 0;
404                         break;
405
406                 case IB_WR_RDMA_READ:
407                         /*
408                          * Don't allow more operations to be started
409                          * than the QP limits allow.
410                          */
411                         if (newreq) {
412                                 if (qp->s_num_rd_atomic >=
413                                     qp->s_max_rd_atomic) {
414                                         qp->s_flags |= QIB_S_WAIT_RDMAR;
415                                         goto bail;
416                                 }
417                                 qp->s_num_rd_atomic++;
418                                 if (!(qp->s_flags & QIB_S_UNLIMITED_CREDIT))
419                                         qp->s_lsn++;
420                                 /*
421                                  * Adjust s_next_psn to count the
422                                  * expected number of responses.
423                                  */
424                                 if (len > pmtu)
425                                         qp->s_next_psn += (len - 1) / pmtu;
426                                 wqe->lpsn = qp->s_next_psn++;
427                         }
428                         ohdr->u.rc.reth.vaddr =
429                                 cpu_to_be64(wqe->wr.wr.rdma.remote_addr);
430                         ohdr->u.rc.reth.rkey =
431                                 cpu_to_be32(wqe->wr.wr.rdma.rkey);
432                         ohdr->u.rc.reth.length = cpu_to_be32(len);
433                         qp->s_state = OP(RDMA_READ_REQUEST);
434                         hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
435                         ss = NULL;
436                         len = 0;
437                         bth2 |= IB_BTH_REQ_ACK;
438                         if (++qp->s_cur == qp->s_size)
439                                 qp->s_cur = 0;
440                         break;
441
442                 case IB_WR_ATOMIC_CMP_AND_SWP:
443                 case IB_WR_ATOMIC_FETCH_AND_ADD:
444                         /*
445                          * Don't allow more operations to be started
446                          * than the QP limits allow.
447                          */
448                         if (newreq) {
449                                 if (qp->s_num_rd_atomic >=
450                                     qp->s_max_rd_atomic) {
451                                         qp->s_flags |= QIB_S_WAIT_RDMAR;
452                                         goto bail;
453                                 }
454                                 qp->s_num_rd_atomic++;
455                                 if (!(qp->s_flags & QIB_S_UNLIMITED_CREDIT))
456                                         qp->s_lsn++;
457                                 wqe->lpsn = wqe->psn;
458                         }
459                         if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
460                                 qp->s_state = OP(COMPARE_SWAP);
461                                 ohdr->u.atomic_eth.swap_data = cpu_to_be64(
462                                         wqe->wr.wr.atomic.swap);
463                                 ohdr->u.atomic_eth.compare_data = cpu_to_be64(
464                                         wqe->wr.wr.atomic.compare_add);
465                         } else {
466                                 qp->s_state = OP(FETCH_ADD);
467                                 ohdr->u.atomic_eth.swap_data = cpu_to_be64(
468                                         wqe->wr.wr.atomic.compare_add);
469                                 ohdr->u.atomic_eth.compare_data = 0;
470                         }
471                         ohdr->u.atomic_eth.vaddr[0] = cpu_to_be32(
472                                 wqe->wr.wr.atomic.remote_addr >> 32);
473                         ohdr->u.atomic_eth.vaddr[1] = cpu_to_be32(
474                                 wqe->wr.wr.atomic.remote_addr);
475                         ohdr->u.atomic_eth.rkey = cpu_to_be32(
476                                 wqe->wr.wr.atomic.rkey);
477                         hwords += sizeof(struct ib_atomic_eth) / sizeof(u32);
478                         ss = NULL;
479                         len = 0;
480                         bth2 |= IB_BTH_REQ_ACK;
481                         if (++qp->s_cur == qp->s_size)
482                                 qp->s_cur = 0;
483                         break;
484
485                 default:
486                         goto bail;
487                 }
488                 qp->s_sge.sge = wqe->sg_list[0];
489                 qp->s_sge.sg_list = wqe->sg_list + 1;
490                 qp->s_sge.num_sge = wqe->wr.num_sge;
491                 qp->s_sge.total_len = wqe->length;
492                 qp->s_len = wqe->length;
493                 if (newreq) {
494                         qp->s_tail++;
495                         if (qp->s_tail >= qp->s_size)
496                                 qp->s_tail = 0;
497                 }
498                 if (wqe->wr.opcode == IB_WR_RDMA_READ)
499                         qp->s_psn = wqe->lpsn + 1;
500                 else {
501                         qp->s_psn++;
502                         if (qib_cmp24(qp->s_psn, qp->s_next_psn) > 0)
503                                 qp->s_next_psn = qp->s_psn;
504                 }
505                 break;
506
507         case OP(RDMA_READ_RESPONSE_FIRST):
508                 /*
509                  * qp->s_state is normally set to the opcode of the
510                  * last packet constructed for new requests and therefore
511                  * is never set to RDMA read response.
512                  * RDMA_READ_RESPONSE_FIRST is used by the ACK processing
513                  * thread to indicate a SEND needs to be restarted from an
514                  * earlier PSN without interferring with the sending thread.
515                  * See qib_restart_rc().
516                  */
517                 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
518                 /* FALLTHROUGH */
519         case OP(SEND_FIRST):
520                 qp->s_state = OP(SEND_MIDDLE);
521                 /* FALLTHROUGH */
522         case OP(SEND_MIDDLE):
523                 bth2 = qp->s_psn++ & QIB_PSN_MASK;
524                 if (qib_cmp24(qp->s_psn, qp->s_next_psn) > 0)
525                         qp->s_next_psn = qp->s_psn;
526                 ss = &qp->s_sge;
527                 len = qp->s_len;
528                 if (len > pmtu) {
529                         len = pmtu;
530                         break;
531                 }
532                 if (wqe->wr.opcode == IB_WR_SEND)
533                         qp->s_state = OP(SEND_LAST);
534                 else {
535                         qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
536                         /* Immediate data comes after the BTH */
537                         ohdr->u.imm_data = wqe->wr.ex.imm_data;
538                         hwords += 1;
539                 }
540                 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
541                         bth0 |= IB_BTH_SOLICITED;
542                 bth2 |= IB_BTH_REQ_ACK;
543                 qp->s_cur++;
544                 if (qp->s_cur >= qp->s_size)
545                         qp->s_cur = 0;
546                 break;
547
548         case OP(RDMA_READ_RESPONSE_LAST):
549                 /*
550                  * qp->s_state is normally set to the opcode of the
551                  * last packet constructed for new requests and therefore
552                  * is never set to RDMA read response.
553                  * RDMA_READ_RESPONSE_LAST is used by the ACK processing
554                  * thread to indicate a RDMA write needs to be restarted from
555                  * an earlier PSN without interferring with the sending thread.
556                  * See qib_restart_rc().
557                  */
558                 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
559                 /* FALLTHROUGH */
560         case OP(RDMA_WRITE_FIRST):
561                 qp->s_state = OP(RDMA_WRITE_MIDDLE);
562                 /* FALLTHROUGH */
563         case OP(RDMA_WRITE_MIDDLE):
564                 bth2 = qp->s_psn++ & QIB_PSN_MASK;
565                 if (qib_cmp24(qp->s_psn, qp->s_next_psn) > 0)
566                         qp->s_next_psn = qp->s_psn;
567                 ss = &qp->s_sge;
568                 len = qp->s_len;
569                 if (len > pmtu) {
570                         len = pmtu;
571                         break;
572                 }
573                 if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
574                         qp->s_state = OP(RDMA_WRITE_LAST);
575                 else {
576                         qp->s_state = OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
577                         /* Immediate data comes after the BTH */
578                         ohdr->u.imm_data = wqe->wr.ex.imm_data;
579                         hwords += 1;
580                         if (wqe->wr.send_flags & IB_SEND_SOLICITED)
581                                 bth0 |= IB_BTH_SOLICITED;
582                 }
583                 bth2 |= IB_BTH_REQ_ACK;
584                 qp->s_cur++;
585                 if (qp->s_cur >= qp->s_size)
586                         qp->s_cur = 0;
587                 break;
588
589         case OP(RDMA_READ_RESPONSE_MIDDLE):
590                 /*
591                  * qp->s_state is normally set to the opcode of the
592                  * last packet constructed for new requests and therefore
593                  * is never set to RDMA read response.
594                  * RDMA_READ_RESPONSE_MIDDLE is used by the ACK processing
595                  * thread to indicate a RDMA read needs to be restarted from
596                  * an earlier PSN without interferring with the sending thread.
597                  * See qib_restart_rc().
598                  */
599                 len = ((qp->s_psn - wqe->psn) & QIB_PSN_MASK) * pmtu;
600                 ohdr->u.rc.reth.vaddr =
601                         cpu_to_be64(wqe->wr.wr.rdma.remote_addr + len);
602                 ohdr->u.rc.reth.rkey =
603                         cpu_to_be32(wqe->wr.wr.rdma.rkey);
604                 ohdr->u.rc.reth.length = cpu_to_be32(wqe->length - len);
605                 qp->s_state = OP(RDMA_READ_REQUEST);
606                 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
607                 bth2 = (qp->s_psn & QIB_PSN_MASK) | IB_BTH_REQ_ACK;
608                 qp->s_psn = wqe->lpsn + 1;
609                 ss = NULL;
610                 len = 0;
611                 qp->s_cur++;
612                 if (qp->s_cur == qp->s_size)
613                         qp->s_cur = 0;
614                 break;
615         }
616         qp->s_sending_hpsn = bth2;
617         delta = (((int) bth2 - (int) wqe->psn) << 8) >> 8;
618         if (delta && delta % QIB_PSN_CREDIT == 0)
619                 bth2 |= IB_BTH_REQ_ACK;
620         if (qp->s_flags & QIB_S_SEND_ONE) {
621                 qp->s_flags &= ~QIB_S_SEND_ONE;
622                 qp->s_flags |= QIB_S_WAIT_ACK;
623                 bth2 |= IB_BTH_REQ_ACK;
624         }
625         qp->s_len -= len;
626         qp->s_hdrwords = hwords;
627         qp->s_cur_sge = ss;
628         qp->s_cur_size = len;
629         qib_make_ruc_header(qp, ohdr, bth0 | (qp->s_state << 24), bth2);
630 done:
631         ret = 1;
632         goto unlock;
633
634 bail:
635         qp->s_flags &= ~QIB_S_BUSY;
636 unlock:
637         spin_unlock_irqrestore(&qp->s_lock, flags);
638         return ret;
639 }
640
641 /**
642  * qib_send_rc_ack - Construct an ACK packet and send it
643  * @qp: a pointer to the QP
644  *
645  * This is called from qib_rc_rcv() and qib_kreceive().
646  * Note that RDMA reads and atomics are handled in the
647  * send side QP state and tasklet.
648  */
649 void qib_send_rc_ack(struct qib_qp *qp)
650 {
651         struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
652         struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
653         struct qib_pportdata *ppd = ppd_from_ibp(ibp);
654         u64 pbc;
655         u16 lrh0;
656         u32 bth0;
657         u32 hwords;
658         u32 pbufn;
659         u32 __iomem *piobuf;
660         struct qib_ib_header hdr;
661         struct qib_other_headers *ohdr;
662         u32 control;
663         unsigned long flags;
664
665         spin_lock_irqsave(&qp->s_lock, flags);
666
667         if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK))
668                 goto unlock;
669
670         /* Don't send ACK or NAK if a RDMA read or atomic is pending. */
671         if ((qp->s_flags & QIB_S_RESP_PENDING) || qp->s_rdma_ack_cnt)
672                 goto queue_ack;
673
674         /* Construct the header with s_lock held so APM doesn't change it. */
675         ohdr = &hdr.u.oth;
676         lrh0 = QIB_LRH_BTH;
677         /* header size in 32-bit words LRH+BTH+AETH = (8+12+4)/4. */
678         hwords = 6;
679         if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
680                 hwords += qib_make_grh(ibp, &hdr.u.l.grh,
681                                        &qp->remote_ah_attr.grh, hwords, 0);
682                 ohdr = &hdr.u.l.oth;
683                 lrh0 = QIB_LRH_GRH;
684         }
685         /* read pkey_index w/o lock (its atomic) */
686         bth0 = qib_get_pkey(ibp, qp->s_pkey_index) | (OP(ACKNOWLEDGE) << 24);
687         if (qp->s_mig_state == IB_MIG_MIGRATED)
688                 bth0 |= IB_BTH_MIG_REQ;
689         if (qp->r_nak_state)
690                 ohdr->u.aeth = cpu_to_be32((qp->r_msn & QIB_MSN_MASK) |
691                                             (qp->r_nak_state <<
692                                              QIB_AETH_CREDIT_SHIFT));
693         else
694                 ohdr->u.aeth = qib_compute_aeth(qp);
695         lrh0 |= ibp->sl_to_vl[qp->remote_ah_attr.sl] << 12 |
696                 qp->remote_ah_attr.sl << 4;
697         hdr.lrh[0] = cpu_to_be16(lrh0);
698         hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
699         hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
700         hdr.lrh[3] = cpu_to_be16(ppd->lid | qp->remote_ah_attr.src_path_bits);
701         ohdr->bth[0] = cpu_to_be32(bth0);
702         ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
703         ohdr->bth[2] = cpu_to_be32(qp->r_ack_psn & QIB_PSN_MASK);
704
705         spin_unlock_irqrestore(&qp->s_lock, flags);
706
707         /* Don't try to send ACKs if the link isn't ACTIVE */
708         if (!(ppd->lflags & QIBL_LINKACTIVE))
709                 goto done;
710
711         control = dd->f_setpbc_control(ppd, hwords + SIZE_OF_CRC,
712                                        qp->s_srate, lrh0 >> 12);
713         /* length is + 1 for the control dword */
714         pbc = ((u64) control << 32) | (hwords + 1);
715
716         piobuf = dd->f_getsendbuf(ppd, pbc, &pbufn);
717         if (!piobuf) {
718                 /*
719                  * We are out of PIO buffers at the moment.
720                  * Pass responsibility for sending the ACK to the
721                  * send tasklet so that when a PIO buffer becomes
722                  * available, the ACK is sent ahead of other outgoing
723                  * packets.
724                  */
725                 spin_lock_irqsave(&qp->s_lock, flags);
726                 goto queue_ack;
727         }
728
729         /*
730          * Write the pbc.
731          * We have to flush after the PBC for correctness
732          * on some cpus or WC buffer can be written out of order.
733          */
734         writeq(pbc, piobuf);
735
736         if (dd->flags & QIB_PIO_FLUSH_WC) {
737                 u32 *hdrp = (u32 *) &hdr;
738
739                 qib_flush_wc();
740                 qib_pio_copy(piobuf + 2, hdrp, hwords - 1);
741                 qib_flush_wc();
742                 __raw_writel(hdrp[hwords - 1], piobuf + hwords + 1);
743         } else
744                 qib_pio_copy(piobuf + 2, (u32 *) &hdr, hwords);
745
746         if (dd->flags & QIB_USE_SPCL_TRIG) {
747                 u32 spcl_off = (pbufn >= dd->piobcnt2k) ? 2047 : 1023;
748
749                 qib_flush_wc();
750                 __raw_writel(0xaebecede, piobuf + spcl_off);
751         }
752
753         qib_flush_wc();
754         qib_sendbuf_done(dd, pbufn);
755
756         ibp->n_unicast_xmit++;
757         goto done;
758
759 queue_ack:
760         if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK) {
761                 ibp->n_rc_qacks++;
762                 qp->s_flags |= QIB_S_ACK_PENDING | QIB_S_RESP_PENDING;
763                 qp->s_nak_state = qp->r_nak_state;
764                 qp->s_ack_psn = qp->r_ack_psn;
765
766                 /* Schedule the send tasklet. */
767                 qib_schedule_send(qp);
768         }
769 unlock:
770         spin_unlock_irqrestore(&qp->s_lock, flags);
771 done:
772         return;
773 }
774
775 /**
776  * reset_psn - reset the QP state to send starting from PSN
777  * @qp: the QP
778  * @psn: the packet sequence number to restart at
779  *
780  * This is called from qib_rc_rcv() to process an incoming RC ACK
781  * for the given QP.
782  * Called at interrupt level with the QP s_lock held.
783  */
784 static void reset_psn(struct qib_qp *qp, u32 psn)
785 {
786         u32 n = qp->s_acked;
787         struct qib_swqe *wqe = get_swqe_ptr(qp, n);
788         u32 opcode;
789
790         qp->s_cur = n;
791
792         /*
793          * If we are starting the request from the beginning,
794          * let the normal send code handle initialization.
795          */
796         if (qib_cmp24(psn, wqe->psn) <= 0) {
797                 qp->s_state = OP(SEND_LAST);
798                 goto done;
799         }
800
801         /* Find the work request opcode corresponding to the given PSN. */
802         opcode = wqe->wr.opcode;
803         for (;;) {
804                 int diff;
805
806                 if (++n == qp->s_size)
807                         n = 0;
808                 if (n == qp->s_tail)
809                         break;
810                 wqe = get_swqe_ptr(qp, n);
811                 diff = qib_cmp24(psn, wqe->psn);
812                 if (diff < 0)
813                         break;
814                 qp->s_cur = n;
815                 /*
816                  * If we are starting the request from the beginning,
817                  * let the normal send code handle initialization.
818                  */
819                 if (diff == 0) {
820                         qp->s_state = OP(SEND_LAST);
821                         goto done;
822                 }
823                 opcode = wqe->wr.opcode;
824         }
825
826         /*
827          * Set the state to restart in the middle of a request.
828          * Don't change the s_sge, s_cur_sge, or s_cur_size.
829          * See qib_make_rc_req().
830          */
831         switch (opcode) {
832         case IB_WR_SEND:
833         case IB_WR_SEND_WITH_IMM:
834                 qp->s_state = OP(RDMA_READ_RESPONSE_FIRST);
835                 break;
836
837         case IB_WR_RDMA_WRITE:
838         case IB_WR_RDMA_WRITE_WITH_IMM:
839                 qp->s_state = OP(RDMA_READ_RESPONSE_LAST);
840                 break;
841
842         case IB_WR_RDMA_READ:
843                 qp->s_state = OP(RDMA_READ_RESPONSE_MIDDLE);
844                 break;
845
846         default:
847                 /*
848                  * This case shouldn't happen since its only
849                  * one PSN per req.
850                  */
851                 qp->s_state = OP(SEND_LAST);
852         }
853 done:
854         qp->s_psn = psn;
855         /*
856          * Set QIB_S_WAIT_PSN as qib_rc_complete() may start the timer
857          * asynchronously before the send tasklet can get scheduled.
858          * Doing it in qib_make_rc_req() is too late.
859          */
860         if ((qib_cmp24(qp->s_psn, qp->s_sending_hpsn) <= 0) &&
861             (qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) <= 0))
862                 qp->s_flags |= QIB_S_WAIT_PSN;
863 }
864
865 /*
866  * Back up requester to resend the last un-ACKed request.
867  * The QP r_lock and s_lock should be held and interrupts disabled.
868  */
869 static void qib_restart_rc(struct qib_qp *qp, u32 psn, int wait)
870 {
871         struct qib_swqe *wqe = get_swqe_ptr(qp, qp->s_acked);
872         struct qib_ibport *ibp;
873
874         if (qp->s_retry == 0) {
875                 if (qp->s_mig_state == IB_MIG_ARMED) {
876                         qib_migrate_qp(qp);
877                         qp->s_retry = qp->s_retry_cnt;
878                 } else if (qp->s_last == qp->s_acked) {
879                         qib_send_complete(qp, wqe, IB_WC_RETRY_EXC_ERR);
880                         qib_error_qp(qp, IB_WC_WR_FLUSH_ERR);
881                         return;
882                 } else /* XXX need to handle delayed completion */
883                         return;
884         } else
885                 qp->s_retry--;
886
887         ibp = to_iport(qp->ibqp.device, qp->port_num);
888         if (wqe->wr.opcode == IB_WR_RDMA_READ)
889                 ibp->n_rc_resends++;
890         else
891                 ibp->n_rc_resends += (qp->s_psn - psn) & QIB_PSN_MASK;
892
893         qp->s_flags &= ~(QIB_S_WAIT_FENCE | QIB_S_WAIT_RDMAR |
894                          QIB_S_WAIT_SSN_CREDIT | QIB_S_WAIT_PSN |
895                          QIB_S_WAIT_ACK);
896         if (wait)
897                 qp->s_flags |= QIB_S_SEND_ONE;
898         reset_psn(qp, psn);
899 }
900
901 /*
902  * This is called from s_timer for missing responses.
903  */
904 static void rc_timeout(unsigned long arg)
905 {
906         struct qib_qp *qp = (struct qib_qp *)arg;
907         struct qib_ibport *ibp;
908         unsigned long flags;
909
910         spin_lock_irqsave(&qp->r_lock, flags);
911         spin_lock(&qp->s_lock);
912         if (qp->s_flags & QIB_S_TIMER) {
913                 ibp = to_iport(qp->ibqp.device, qp->port_num);
914                 ibp->n_rc_timeouts++;
915                 qp->s_flags &= ~QIB_S_TIMER;
916                 del_timer(&qp->s_timer);
917                 qib_restart_rc(qp, qp->s_last_psn + 1, 1);
918                 qib_schedule_send(qp);
919         }
920         spin_unlock(&qp->s_lock);
921         spin_unlock_irqrestore(&qp->r_lock, flags);
922 }
923
924 /*
925  * This is called from s_timer for RNR timeouts.
926  */
927 void qib_rc_rnr_retry(unsigned long arg)
928 {
929         struct qib_qp *qp = (struct qib_qp *)arg;
930         unsigned long flags;
931
932         spin_lock_irqsave(&qp->s_lock, flags);
933         if (qp->s_flags & QIB_S_WAIT_RNR) {
934                 qp->s_flags &= ~QIB_S_WAIT_RNR;
935                 del_timer(&qp->s_timer);
936                 qib_schedule_send(qp);
937         }
938         spin_unlock_irqrestore(&qp->s_lock, flags);
939 }
940
941 /*
942  * Set qp->s_sending_psn to the next PSN after the given one.
943  * This would be psn+1 except when RDMA reads are present.
944  */
945 static void reset_sending_psn(struct qib_qp *qp, u32 psn)
946 {
947         struct qib_swqe *wqe;
948         u32 n = qp->s_last;
949
950         /* Find the work request corresponding to the given PSN. */
951         for (;;) {
952                 wqe = get_swqe_ptr(qp, n);
953                 if (qib_cmp24(psn, wqe->lpsn) <= 0) {
954                         if (wqe->wr.opcode == IB_WR_RDMA_READ)
955                                 qp->s_sending_psn = wqe->lpsn + 1;
956                         else
957                                 qp->s_sending_psn = psn + 1;
958                         break;
959                 }
960                 if (++n == qp->s_size)
961                         n = 0;
962                 if (n == qp->s_tail)
963                         break;
964         }
965 }
966
967 /*
968  * This should be called with the QP s_lock held and interrupts disabled.
969  */
970 void qib_rc_send_complete(struct qib_qp *qp, struct qib_ib_header *hdr)
971 {
972         struct qib_other_headers *ohdr;
973         struct qib_swqe *wqe;
974         struct ib_wc wc;
975         unsigned i;
976         u32 opcode;
977         u32 psn;
978
979         if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_OR_FLUSH_SEND))
980                 return;
981
982         /* Find out where the BTH is */
983         if ((be16_to_cpu(hdr->lrh[0]) & 3) == QIB_LRH_BTH)
984                 ohdr = &hdr->u.oth;
985         else
986                 ohdr = &hdr->u.l.oth;
987
988         opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
989         if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
990             opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
991                 WARN_ON(!qp->s_rdma_ack_cnt);
992                 qp->s_rdma_ack_cnt--;
993                 return;
994         }
995
996         psn = be32_to_cpu(ohdr->bth[2]);
997         reset_sending_psn(qp, psn);
998
999         /*
1000          * Start timer after a packet requesting an ACK has been sent and
1001          * there are still requests that haven't been acked.
1002          */
1003         if ((psn & IB_BTH_REQ_ACK) && qp->s_acked != qp->s_tail &&
1004             !(qp->s_flags & (QIB_S_TIMER | QIB_S_WAIT_RNR | QIB_S_WAIT_PSN)) &&
1005             (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK))
1006                 start_timer(qp);
1007
1008         while (qp->s_last != qp->s_acked) {
1009                 wqe = get_swqe_ptr(qp, qp->s_last);
1010                 if (qib_cmp24(wqe->lpsn, qp->s_sending_psn) >= 0 &&
1011                     qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) <= 0)
1012                         break;
1013                 for (i = 0; i < wqe->wr.num_sge; i++) {
1014                         struct qib_sge *sge = &wqe->sg_list[i];
1015
1016                         atomic_dec(&sge->mr->refcount);
1017                 }
1018                 /* Post a send completion queue entry if requested. */
1019                 if (!(qp->s_flags & QIB_S_SIGNAL_REQ_WR) ||
1020                     (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
1021                         memset(&wc, 0, sizeof wc);
1022                         wc.wr_id = wqe->wr.wr_id;
1023                         wc.status = IB_WC_SUCCESS;
1024                         wc.opcode = ib_qib_wc_opcode[wqe->wr.opcode];
1025                         wc.byte_len = wqe->length;
1026                         wc.qp = &qp->ibqp;
1027                         qib_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 0);
1028                 }
1029                 if (++qp->s_last >= qp->s_size)
1030                         qp->s_last = 0;
1031         }
1032         /*
1033          * If we were waiting for sends to complete before resending,
1034          * and they are now complete, restart sending.
1035          */
1036         if (qp->s_flags & QIB_S_WAIT_PSN &&
1037             qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
1038                 qp->s_flags &= ~QIB_S_WAIT_PSN;
1039                 qp->s_sending_psn = qp->s_psn;
1040                 qp->s_sending_hpsn = qp->s_psn - 1;
1041                 qib_schedule_send(qp);
1042         }
1043 }
1044
1045 static inline void update_last_psn(struct qib_qp *qp, u32 psn)
1046 {
1047         qp->s_last_psn = psn;
1048 }
1049
1050 /*
1051  * Generate a SWQE completion.
1052  * This is similar to qib_send_complete but has to check to be sure
1053  * that the SGEs are not being referenced if the SWQE is being resent.
1054  */
1055 static struct qib_swqe *do_rc_completion(struct qib_qp *qp,
1056                                          struct qib_swqe *wqe,
1057                                          struct qib_ibport *ibp)
1058 {
1059         struct ib_wc wc;
1060         unsigned i;
1061
1062         /*
1063          * Don't decrement refcount and don't generate a
1064          * completion if the SWQE is being resent until the send
1065          * is finished.
1066          */
1067         if (qib_cmp24(wqe->lpsn, qp->s_sending_psn) < 0 ||
1068             qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
1069                 for (i = 0; i < wqe->wr.num_sge; i++) {
1070                         struct qib_sge *sge = &wqe->sg_list[i];
1071
1072                         atomic_dec(&sge->mr->refcount);
1073                 }
1074                 /* Post a send completion queue entry if requested. */
1075                 if (!(qp->s_flags & QIB_S_SIGNAL_REQ_WR) ||
1076                     (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
1077                         memset(&wc, 0, sizeof wc);
1078                         wc.wr_id = wqe->wr.wr_id;
1079                         wc.status = IB_WC_SUCCESS;
1080                         wc.opcode = ib_qib_wc_opcode[wqe->wr.opcode];
1081                         wc.byte_len = wqe->length;
1082                         wc.qp = &qp->ibqp;
1083                         qib_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 0);
1084                 }
1085                 if (++qp->s_last >= qp->s_size)
1086                         qp->s_last = 0;
1087         } else
1088                 ibp->n_rc_delayed_comp++;
1089
1090         qp->s_retry = qp->s_retry_cnt;
1091         update_last_psn(qp, wqe->lpsn);
1092
1093         /*
1094          * If we are completing a request which is in the process of
1095          * being resent, we can stop resending it since we know the
1096          * responder has already seen it.
1097          */
1098         if (qp->s_acked == qp->s_cur) {
1099                 if (++qp->s_cur >= qp->s_size)
1100                         qp->s_cur = 0;
1101                 qp->s_acked = qp->s_cur;
1102                 wqe = get_swqe_ptr(qp, qp->s_cur);
1103                 if (qp->s_acked != qp->s_tail) {
1104                         qp->s_state = OP(SEND_LAST);
1105                         qp->s_psn = wqe->psn;
1106                 }
1107         } else {
1108                 if (++qp->s_acked >= qp->s_size)
1109                         qp->s_acked = 0;
1110                 if (qp->state == IB_QPS_SQD && qp->s_acked == qp->s_cur)
1111                         qp->s_draining = 0;
1112                 wqe = get_swqe_ptr(qp, qp->s_acked);
1113         }
1114         return wqe;
1115 }
1116
1117 /**
1118  * do_rc_ack - process an incoming RC ACK
1119  * @qp: the QP the ACK came in on
1120  * @psn: the packet sequence number of the ACK
1121  * @opcode: the opcode of the request that resulted in the ACK
1122  *
1123  * This is called from qib_rc_rcv_resp() to process an incoming RC ACK
1124  * for the given QP.
1125  * Called at interrupt level with the QP s_lock held.
1126  * Returns 1 if OK, 0 if current operation should be aborted (NAK).
1127  */
1128 static int do_rc_ack(struct qib_qp *qp, u32 aeth, u32 psn, int opcode,
1129                      u64 val, struct qib_ctxtdata *rcd)
1130 {
1131         struct qib_ibport *ibp;
1132         enum ib_wc_status status;
1133         struct qib_swqe *wqe;
1134         int ret = 0;
1135         u32 ack_psn;
1136         int diff;
1137
1138         /* Remove QP from retry timer */
1139         if (qp->s_flags & (QIB_S_TIMER | QIB_S_WAIT_RNR)) {
1140                 qp->s_flags &= ~(QIB_S_TIMER | QIB_S_WAIT_RNR);
1141                 del_timer(&qp->s_timer);
1142         }
1143
1144         /*
1145          * Note that NAKs implicitly ACK outstanding SEND and RDMA write
1146          * requests and implicitly NAK RDMA read and atomic requests issued
1147          * before the NAK'ed request.  The MSN won't include the NAK'ed
1148          * request but will include an ACK'ed request(s).
1149          */
1150         ack_psn = psn;
1151         if (aeth >> 29)
1152                 ack_psn--;
1153         wqe = get_swqe_ptr(qp, qp->s_acked);
1154         ibp = to_iport(qp->ibqp.device, qp->port_num);
1155
1156         /*
1157          * The MSN might be for a later WQE than the PSN indicates so
1158          * only complete WQEs that the PSN finishes.
1159          */
1160         while ((diff = qib_cmp24(ack_psn, wqe->lpsn)) >= 0) {
1161                 /*
1162                  * RDMA_READ_RESPONSE_ONLY is a special case since
1163                  * we want to generate completion events for everything
1164                  * before the RDMA read, copy the data, then generate
1165                  * the completion for the read.
1166                  */
1167                 if (wqe->wr.opcode == IB_WR_RDMA_READ &&
1168                     opcode == OP(RDMA_READ_RESPONSE_ONLY) &&
1169                     diff == 0) {
1170                         ret = 1;
1171                         goto bail;
1172                 }
1173                 /*
1174                  * If this request is a RDMA read or atomic, and the ACK is
1175                  * for a later operation, this ACK NAKs the RDMA read or
1176                  * atomic.  In other words, only a RDMA_READ_LAST or ONLY
1177                  * can ACK a RDMA read and likewise for atomic ops.  Note
1178                  * that the NAK case can only happen if relaxed ordering is
1179                  * used and requests are sent after an RDMA read or atomic
1180                  * is sent but before the response is received.
1181                  */
1182                 if ((wqe->wr.opcode == IB_WR_RDMA_READ &&
1183                      (opcode != OP(RDMA_READ_RESPONSE_LAST) || diff != 0)) ||
1184                     ((wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1185                       wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) &&
1186                      (opcode != OP(ATOMIC_ACKNOWLEDGE) || diff != 0))) {
1187                         /* Retry this request. */
1188                         if (!(qp->r_flags & QIB_R_RDMAR_SEQ)) {
1189                                 qp->r_flags |= QIB_R_RDMAR_SEQ;
1190                                 qib_restart_rc(qp, qp->s_last_psn + 1, 0);
1191                                 if (list_empty(&qp->rspwait)) {
1192                                         qp->r_flags |= QIB_R_RSP_SEND;
1193                                         atomic_inc(&qp->refcount);
1194                                         list_add_tail(&qp->rspwait,
1195                                                       &rcd->qp_wait_list);
1196                                 }
1197                         }
1198                         /*
1199                          * No need to process the ACK/NAK since we are
1200                          * restarting an earlier request.
1201                          */
1202                         goto bail;
1203                 }
1204                 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1205                     wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
1206                         u64 *vaddr = wqe->sg_list[0].vaddr;
1207                         *vaddr = val;
1208                 }
1209                 if (qp->s_num_rd_atomic &&
1210                     (wqe->wr.opcode == IB_WR_RDMA_READ ||
1211                      wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1212                      wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)) {
1213                         qp->s_num_rd_atomic--;
1214                         /* Restart sending task if fence is complete */
1215                         if ((qp->s_flags & QIB_S_WAIT_FENCE) &&
1216                             !qp->s_num_rd_atomic) {
1217                                 qp->s_flags &= ~(QIB_S_WAIT_FENCE |
1218                                                  QIB_S_WAIT_ACK);
1219                                 qib_schedule_send(qp);
1220                         } else if (qp->s_flags & QIB_S_WAIT_RDMAR) {
1221                                 qp->s_flags &= ~(QIB_S_WAIT_RDMAR |
1222                                                  QIB_S_WAIT_ACK);
1223                                 qib_schedule_send(qp);
1224                         }
1225                 }
1226                 wqe = do_rc_completion(qp, wqe, ibp);
1227                 if (qp->s_acked == qp->s_tail)
1228                         break;
1229         }
1230
1231         switch (aeth >> 29) {
1232         case 0:         /* ACK */
1233                 ibp->n_rc_acks++;
1234                 if (qp->s_acked != qp->s_tail) {
1235                         /*
1236                          * We are expecting more ACKs so
1237                          * reset the retransmit timer.
1238                          */
1239                         start_timer(qp);
1240                         /*
1241                          * We can stop resending the earlier packets and
1242                          * continue with the next packet the receiver wants.
1243                          */
1244                         if (qib_cmp24(qp->s_psn, psn) <= 0)
1245                                 reset_psn(qp, psn + 1);
1246                 } else if (qib_cmp24(qp->s_psn, psn) <= 0) {
1247                         qp->s_state = OP(SEND_LAST);
1248                         qp->s_psn = psn + 1;
1249                 }
1250                 if (qp->s_flags & QIB_S_WAIT_ACK) {
1251                         qp->s_flags &= ~QIB_S_WAIT_ACK;
1252                         qib_schedule_send(qp);
1253                 }
1254                 qib_get_credit(qp, aeth);
1255                 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1256                 qp->s_retry = qp->s_retry_cnt;
1257                 update_last_psn(qp, psn);
1258                 ret = 1;
1259                 goto bail;
1260
1261         case 1:         /* RNR NAK */
1262                 ibp->n_rnr_naks++;
1263                 if (qp->s_acked == qp->s_tail)
1264                         goto bail;
1265                 if (qp->s_flags & QIB_S_WAIT_RNR)
1266                         goto bail;
1267                 if (qp->s_rnr_retry == 0) {
1268                         status = IB_WC_RNR_RETRY_EXC_ERR;
1269                         goto class_b;
1270                 }
1271                 if (qp->s_rnr_retry_cnt < 7)
1272                         qp->s_rnr_retry--;
1273
1274                 /* The last valid PSN is the previous PSN. */
1275                 update_last_psn(qp, psn - 1);
1276
1277                 ibp->n_rc_resends += (qp->s_psn - psn) & QIB_PSN_MASK;
1278
1279                 reset_psn(qp, psn);
1280
1281                 qp->s_flags &= ~(QIB_S_WAIT_SSN_CREDIT | QIB_S_WAIT_ACK);
1282                 qp->s_flags |= QIB_S_WAIT_RNR;
1283                 qp->s_timer.function = qib_rc_rnr_retry;
1284                 qp->s_timer.expires = jiffies + usecs_to_jiffies(
1285                         ib_qib_rnr_table[(aeth >> QIB_AETH_CREDIT_SHIFT) &
1286                                            QIB_AETH_CREDIT_MASK]);
1287                 add_timer(&qp->s_timer);
1288                 goto bail;
1289
1290         case 3:         /* NAK */
1291                 if (qp->s_acked == qp->s_tail)
1292                         goto bail;
1293                 /* The last valid PSN is the previous PSN. */
1294                 update_last_psn(qp, psn - 1);
1295                 switch ((aeth >> QIB_AETH_CREDIT_SHIFT) &
1296                         QIB_AETH_CREDIT_MASK) {
1297                 case 0: /* PSN sequence error */
1298                         ibp->n_seq_naks++;
1299                         /*
1300                          * Back up to the responder's expected PSN.
1301                          * Note that we might get a NAK in the middle of an
1302                          * RDMA READ response which terminates the RDMA
1303                          * READ.
1304                          */
1305                         qib_restart_rc(qp, psn, 0);
1306                         qib_schedule_send(qp);
1307                         break;
1308
1309                 case 1: /* Invalid Request */
1310                         status = IB_WC_REM_INV_REQ_ERR;
1311                         ibp->n_other_naks++;
1312                         goto class_b;
1313
1314                 case 2: /* Remote Access Error */
1315                         status = IB_WC_REM_ACCESS_ERR;
1316                         ibp->n_other_naks++;
1317                         goto class_b;
1318
1319                 case 3: /* Remote Operation Error */
1320                         status = IB_WC_REM_OP_ERR;
1321                         ibp->n_other_naks++;
1322 class_b:
1323                         if (qp->s_last == qp->s_acked) {
1324                                 qib_send_complete(qp, wqe, status);
1325                                 qib_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1326                         }
1327                         break;
1328
1329                 default:
1330                         /* Ignore other reserved NAK error codes */
1331                         goto reserved;
1332                 }
1333                 qp->s_retry = qp->s_retry_cnt;
1334                 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1335                 goto bail;
1336
1337         default:                /* 2: reserved */
1338 reserved:
1339                 /* Ignore reserved NAK codes. */
1340                 goto bail;
1341         }
1342
1343 bail:
1344         return ret;
1345 }
1346
1347 /*
1348  * We have seen an out of sequence RDMA read middle or last packet.
1349  * This ACKs SENDs and RDMA writes up to the first RDMA read or atomic SWQE.
1350  */
1351 static void rdma_seq_err(struct qib_qp *qp, struct qib_ibport *ibp, u32 psn,
1352                          struct qib_ctxtdata *rcd)
1353 {
1354         struct qib_swqe *wqe;
1355
1356         /* Remove QP from retry timer */
1357         if (qp->s_flags & (QIB_S_TIMER | QIB_S_WAIT_RNR)) {
1358                 qp->s_flags &= ~(QIB_S_TIMER | QIB_S_WAIT_RNR);
1359                 del_timer(&qp->s_timer);
1360         }
1361
1362         wqe = get_swqe_ptr(qp, qp->s_acked);
1363
1364         while (qib_cmp24(psn, wqe->lpsn) > 0) {
1365                 if (wqe->wr.opcode == IB_WR_RDMA_READ ||
1366                     wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1367                     wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
1368                         break;
1369                 wqe = do_rc_completion(qp, wqe, ibp);
1370         }
1371
1372         ibp->n_rdma_seq++;
1373         qp->r_flags |= QIB_R_RDMAR_SEQ;
1374         qib_restart_rc(qp, qp->s_last_psn + 1, 0);
1375         if (list_empty(&qp->rspwait)) {
1376                 qp->r_flags |= QIB_R_RSP_SEND;
1377                 atomic_inc(&qp->refcount);
1378                 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1379         }
1380 }
1381
1382 /**
1383  * qib_rc_rcv_resp - process an incoming RC response packet
1384  * @ibp: the port this packet came in on
1385  * @ohdr: the other headers for this packet
1386  * @data: the packet data
1387  * @tlen: the packet length
1388  * @qp: the QP for this packet
1389  * @opcode: the opcode for this packet
1390  * @psn: the packet sequence number for this packet
1391  * @hdrsize: the header length
1392  * @pmtu: the path MTU
1393  *
1394  * This is called from qib_rc_rcv() to process an incoming RC response
1395  * packet for the given QP.
1396  * Called at interrupt level.
1397  */
1398 static void qib_rc_rcv_resp(struct qib_ibport *ibp,
1399                             struct qib_other_headers *ohdr,
1400                             void *data, u32 tlen,
1401                             struct qib_qp *qp,
1402                             u32 opcode,
1403                             u32 psn, u32 hdrsize, u32 pmtu,
1404                             struct qib_ctxtdata *rcd)
1405 {
1406         struct qib_swqe *wqe;
1407         struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1408         enum ib_wc_status status;
1409         unsigned long flags;
1410         int diff;
1411         u32 pad;
1412         u32 aeth;
1413         u64 val;
1414
1415         if (opcode != OP(RDMA_READ_RESPONSE_MIDDLE)) {
1416                 /*
1417                  * If ACK'd PSN on SDMA busy list try to make progress to
1418                  * reclaim SDMA credits.
1419                  */
1420                 if ((qib_cmp24(psn, qp->s_sending_psn) >= 0) &&
1421                     (qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) <= 0)) {
1422
1423                         /*
1424                          * If send tasklet not running attempt to progress
1425                          * SDMA queue.
1426                          */
1427                         if (!(qp->s_flags & QIB_S_BUSY)) {
1428                                 /* Acquire SDMA Lock */
1429                                 spin_lock_irqsave(&ppd->sdma_lock, flags);
1430                                 /* Invoke sdma make progress */
1431                                 qib_sdma_make_progress(ppd);
1432                                 /* Release SDMA Lock */
1433                                 spin_unlock_irqrestore(&ppd->sdma_lock, flags);
1434                         }
1435                 }
1436         }
1437
1438         spin_lock_irqsave(&qp->s_lock, flags);
1439         if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK))
1440                 goto ack_done;
1441
1442         /* Ignore invalid responses. */
1443         if (qib_cmp24(psn, qp->s_next_psn) >= 0)
1444                 goto ack_done;
1445
1446         /* Ignore duplicate responses. */
1447         diff = qib_cmp24(psn, qp->s_last_psn);
1448         if (unlikely(diff <= 0)) {
1449                 /* Update credits for "ghost" ACKs */
1450                 if (diff == 0 && opcode == OP(ACKNOWLEDGE)) {
1451                         aeth = be32_to_cpu(ohdr->u.aeth);
1452                         if ((aeth >> 29) == 0)
1453                                 qib_get_credit(qp, aeth);
1454                 }
1455                 goto ack_done;
1456         }
1457
1458         /*
1459          * Skip everything other than the PSN we expect, if we are waiting
1460          * for a reply to a restarted RDMA read or atomic op.
1461          */
1462         if (qp->r_flags & QIB_R_RDMAR_SEQ) {
1463                 if (qib_cmp24(psn, qp->s_last_psn + 1) != 0)
1464                         goto ack_done;
1465                 qp->r_flags &= ~QIB_R_RDMAR_SEQ;
1466         }
1467
1468         if (unlikely(qp->s_acked == qp->s_tail))
1469                 goto ack_done;
1470         wqe = get_swqe_ptr(qp, qp->s_acked);
1471         status = IB_WC_SUCCESS;
1472
1473         switch (opcode) {
1474         case OP(ACKNOWLEDGE):
1475         case OP(ATOMIC_ACKNOWLEDGE):
1476         case OP(RDMA_READ_RESPONSE_FIRST):
1477                 aeth = be32_to_cpu(ohdr->u.aeth);
1478                 if (opcode == OP(ATOMIC_ACKNOWLEDGE)) {
1479                         __be32 *p = ohdr->u.at.atomic_ack_eth;
1480
1481                         val = ((u64) be32_to_cpu(p[0]) << 32) |
1482                                 be32_to_cpu(p[1]);
1483                 } else
1484                         val = 0;
1485                 if (!do_rc_ack(qp, aeth, psn, opcode, val, rcd) ||
1486                     opcode != OP(RDMA_READ_RESPONSE_FIRST))
1487                         goto ack_done;
1488                 hdrsize += 4;
1489                 wqe = get_swqe_ptr(qp, qp->s_acked);
1490                 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1491                         goto ack_op_err;
1492                 /*
1493                  * If this is a response to a resent RDMA read, we
1494                  * have to be careful to copy the data to the right
1495                  * location.
1496                  */
1497                 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1498                                                   wqe, psn, pmtu);
1499                 goto read_middle;
1500
1501         case OP(RDMA_READ_RESPONSE_MIDDLE):
1502                 /* no AETH, no ACK */
1503                 if (unlikely(qib_cmp24(psn, qp->s_last_psn + 1)))
1504                         goto ack_seq_err;
1505                 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1506                         goto ack_op_err;
1507 read_middle:
1508                 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1509                         goto ack_len_err;
1510                 if (unlikely(pmtu >= qp->s_rdma_read_len))
1511                         goto ack_len_err;
1512
1513                 /*
1514                  * We got a response so update the timeout.
1515                  * 4.096 usec. * (1 << qp->timeout)
1516                  */
1517                 qp->s_flags |= QIB_S_TIMER;
1518                 mod_timer(&qp->s_timer, jiffies + qp->timeout_jiffies);
1519                 if (qp->s_flags & QIB_S_WAIT_ACK) {
1520                         qp->s_flags &= ~QIB_S_WAIT_ACK;
1521                         qib_schedule_send(qp);
1522                 }
1523
1524                 if (opcode == OP(RDMA_READ_RESPONSE_MIDDLE))
1525                         qp->s_retry = qp->s_retry_cnt;
1526
1527                 /*
1528                  * Update the RDMA receive state but do the copy w/o
1529                  * holding the locks and blocking interrupts.
1530                  */
1531                 qp->s_rdma_read_len -= pmtu;
1532                 update_last_psn(qp, psn);
1533                 spin_unlock_irqrestore(&qp->s_lock, flags);
1534                 qib_copy_sge(&qp->s_rdma_read_sge, data, pmtu, 0);
1535                 goto bail;
1536
1537         case OP(RDMA_READ_RESPONSE_ONLY):
1538                 aeth = be32_to_cpu(ohdr->u.aeth);
1539                 if (!do_rc_ack(qp, aeth, psn, opcode, 0, rcd))
1540                         goto ack_done;
1541                 /* Get the number of bytes the message was padded by. */
1542                 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1543                 /*
1544                  * Check that the data size is >= 0 && <= pmtu.
1545                  * Remember to account for the AETH header (4) and
1546                  * ICRC (4).
1547                  */
1548                 if (unlikely(tlen < (hdrsize + pad + 8)))
1549                         goto ack_len_err;
1550                 /*
1551                  * If this is a response to a resent RDMA read, we
1552                  * have to be careful to copy the data to the right
1553                  * location.
1554                  */
1555                 wqe = get_swqe_ptr(qp, qp->s_acked);
1556                 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1557                                                   wqe, psn, pmtu);
1558                 goto read_last;
1559
1560         case OP(RDMA_READ_RESPONSE_LAST):
1561                 /* ACKs READ req. */
1562                 if (unlikely(qib_cmp24(psn, qp->s_last_psn + 1)))
1563                         goto ack_seq_err;
1564                 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1565                         goto ack_op_err;
1566                 /* Get the number of bytes the message was padded by. */
1567                 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1568                 /*
1569                  * Check that the data size is >= 1 && <= pmtu.
1570                  * Remember to account for the AETH header (4) and
1571                  * ICRC (4).
1572                  */
1573                 if (unlikely(tlen <= (hdrsize + pad + 8)))
1574                         goto ack_len_err;
1575 read_last:
1576                 tlen -= hdrsize + pad + 8;
1577                 if (unlikely(tlen != qp->s_rdma_read_len))
1578                         goto ack_len_err;
1579                 aeth = be32_to_cpu(ohdr->u.aeth);
1580                 qib_copy_sge(&qp->s_rdma_read_sge, data, tlen, 0);
1581                 WARN_ON(qp->s_rdma_read_sge.num_sge);
1582                 (void) do_rc_ack(qp, aeth, psn,
1583                                  OP(RDMA_READ_RESPONSE_LAST), 0, rcd);
1584                 goto ack_done;
1585         }
1586
1587 ack_op_err:
1588         status = IB_WC_LOC_QP_OP_ERR;
1589         goto ack_err;
1590
1591 ack_seq_err:
1592         rdma_seq_err(qp, ibp, psn, rcd);
1593         goto ack_done;
1594
1595 ack_len_err:
1596         status = IB_WC_LOC_LEN_ERR;
1597 ack_err:
1598         if (qp->s_last == qp->s_acked) {
1599                 qib_send_complete(qp, wqe, status);
1600                 qib_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1601         }
1602 ack_done:
1603         spin_unlock_irqrestore(&qp->s_lock, flags);
1604 bail:
1605         return;
1606 }
1607
1608 /**
1609  * qib_rc_rcv_error - process an incoming duplicate or error RC packet
1610  * @ohdr: the other headers for this packet
1611  * @data: the packet data
1612  * @qp: the QP for this packet
1613  * @opcode: the opcode for this packet
1614  * @psn: the packet sequence number for this packet
1615  * @diff: the difference between the PSN and the expected PSN
1616  *
1617  * This is called from qib_rc_rcv() to process an unexpected
1618  * incoming RC packet for the given QP.
1619  * Called at interrupt level.
1620  * Return 1 if no more processing is needed; otherwise return 0 to
1621  * schedule a response to be sent.
1622  */
1623 static int qib_rc_rcv_error(struct qib_other_headers *ohdr,
1624                             void *data,
1625                             struct qib_qp *qp,
1626                             u32 opcode,
1627                             u32 psn,
1628                             int diff,
1629                             struct qib_ctxtdata *rcd)
1630 {
1631         struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
1632         struct qib_ack_entry *e;
1633         unsigned long flags;
1634         u8 i, prev;
1635         int old_req;
1636
1637         if (diff > 0) {
1638                 /*
1639                  * Packet sequence error.
1640                  * A NAK will ACK earlier sends and RDMA writes.
1641                  * Don't queue the NAK if we already sent one.
1642                  */
1643                 if (!qp->r_nak_state) {
1644                         ibp->n_rc_seqnak++;
1645                         qp->r_nak_state = IB_NAK_PSN_ERROR;
1646                         /* Use the expected PSN. */
1647                         qp->r_ack_psn = qp->r_psn;
1648                         /*
1649                          * Wait to send the sequence NAK until all packets
1650                          * in the receive queue have been processed.
1651                          * Otherwise, we end up propagating congestion.
1652                          */
1653                         if (list_empty(&qp->rspwait)) {
1654                                 qp->r_flags |= QIB_R_RSP_NAK;
1655                                 atomic_inc(&qp->refcount);
1656                                 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1657                         }
1658                 }
1659                 goto done;
1660         }
1661
1662         /*
1663          * Handle a duplicate request.  Don't re-execute SEND, RDMA
1664          * write or atomic op.  Don't NAK errors, just silently drop
1665          * the duplicate request.  Note that r_sge, r_len, and
1666          * r_rcv_len may be in use so don't modify them.
1667          *
1668          * We are supposed to ACK the earliest duplicate PSN but we
1669          * can coalesce an outstanding duplicate ACK.  We have to
1670          * send the earliest so that RDMA reads can be restarted at
1671          * the requester's expected PSN.
1672          *
1673          * First, find where this duplicate PSN falls within the
1674          * ACKs previously sent.
1675          * old_req is true if there is an older response that is scheduled
1676          * to be sent before sending this one.
1677          */
1678         e = NULL;
1679         old_req = 1;
1680         ibp->n_rc_dupreq++;
1681
1682         spin_lock_irqsave(&qp->s_lock, flags);
1683
1684         for (i = qp->r_head_ack_queue; ; i = prev) {
1685                 if (i == qp->s_tail_ack_queue)
1686                         old_req = 0;
1687                 if (i)
1688                         prev = i - 1;
1689                 else
1690                         prev = QIB_MAX_RDMA_ATOMIC;
1691                 if (prev == qp->r_head_ack_queue) {
1692                         e = NULL;
1693                         break;
1694                 }
1695                 e = &qp->s_ack_queue[prev];
1696                 if (!e->opcode) {
1697                         e = NULL;
1698                         break;
1699                 }
1700                 if (qib_cmp24(psn, e->psn) >= 0) {
1701                         if (prev == qp->s_tail_ack_queue &&
1702                             qib_cmp24(psn, e->lpsn) <= 0)
1703                                 old_req = 0;
1704                         break;
1705                 }
1706         }
1707         switch (opcode) {
1708         case OP(RDMA_READ_REQUEST): {
1709                 struct ib_reth *reth;
1710                 u32 offset;
1711                 u32 len;
1712
1713                 /*
1714                  * If we didn't find the RDMA read request in the ack queue,
1715                  * we can ignore this request.
1716                  */
1717                 if (!e || e->opcode != OP(RDMA_READ_REQUEST))
1718                         goto unlock_done;
1719                 /* RETH comes after BTH */
1720                 reth = &ohdr->u.rc.reth;
1721                 /*
1722                  * Address range must be a subset of the original
1723                  * request and start on pmtu boundaries.
1724                  * We reuse the old ack_queue slot since the requester
1725                  * should not back up and request an earlier PSN for the
1726                  * same request.
1727                  */
1728                 offset = ((psn - e->psn) & QIB_PSN_MASK) *
1729                         qp->pmtu;
1730                 len = be32_to_cpu(reth->length);
1731                 if (unlikely(offset + len != e->rdma_sge.sge_length))
1732                         goto unlock_done;
1733                 if (e->rdma_sge.mr) {
1734                         atomic_dec(&e->rdma_sge.mr->refcount);
1735                         e->rdma_sge.mr = NULL;
1736                 }
1737                 if (len != 0) {
1738                         u32 rkey = be32_to_cpu(reth->rkey);
1739                         u64 vaddr = be64_to_cpu(reth->vaddr);
1740                         int ok;
1741
1742                         ok = qib_rkey_ok(qp, &e->rdma_sge, len, vaddr, rkey,
1743                                          IB_ACCESS_REMOTE_READ);
1744                         if (unlikely(!ok))
1745                                 goto unlock_done;
1746                 } else {
1747                         e->rdma_sge.vaddr = NULL;
1748                         e->rdma_sge.length = 0;
1749                         e->rdma_sge.sge_length = 0;
1750                 }
1751                 e->psn = psn;
1752                 if (old_req)
1753                         goto unlock_done;
1754                 qp->s_tail_ack_queue = prev;
1755                 break;
1756         }
1757
1758         case OP(COMPARE_SWAP):
1759         case OP(FETCH_ADD): {
1760                 /*
1761                  * If we didn't find the atomic request in the ack queue
1762                  * or the send tasklet is already backed up to send an
1763                  * earlier entry, we can ignore this request.
1764                  */
1765                 if (!e || e->opcode != (u8) opcode || old_req)
1766                         goto unlock_done;
1767                 qp->s_tail_ack_queue = prev;
1768                 break;
1769         }
1770
1771         default:
1772                 /*
1773                  * Ignore this operation if it doesn't request an ACK
1774                  * or an earlier RDMA read or atomic is going to be resent.
1775                  */
1776                 if (!(psn & IB_BTH_REQ_ACK) || old_req)
1777                         goto unlock_done;
1778                 /*
1779                  * Resend the most recent ACK if this request is
1780                  * after all the previous RDMA reads and atomics.
1781                  */
1782                 if (i == qp->r_head_ack_queue) {
1783                         spin_unlock_irqrestore(&qp->s_lock, flags);
1784                         qp->r_nak_state = 0;
1785                         qp->r_ack_psn = qp->r_psn - 1;
1786                         goto send_ack;
1787                 }
1788                 /*
1789                  * Try to send a simple ACK to work around a Mellanox bug
1790                  * which doesn't accept a RDMA read response or atomic
1791                  * response as an ACK for earlier SENDs or RDMA writes.
1792                  */
1793                 if (!(qp->s_flags & QIB_S_RESP_PENDING)) {
1794                         spin_unlock_irqrestore(&qp->s_lock, flags);
1795                         qp->r_nak_state = 0;
1796                         qp->r_ack_psn = qp->s_ack_queue[i].psn - 1;
1797                         goto send_ack;
1798                 }
1799                 /*
1800                  * Resend the RDMA read or atomic op which
1801                  * ACKs this duplicate request.
1802                  */
1803                 qp->s_tail_ack_queue = i;
1804                 break;
1805         }
1806         qp->s_ack_state = OP(ACKNOWLEDGE);
1807         qp->s_flags |= QIB_S_RESP_PENDING;
1808         qp->r_nak_state = 0;
1809         qib_schedule_send(qp);
1810
1811 unlock_done:
1812         spin_unlock_irqrestore(&qp->s_lock, flags);
1813 done:
1814         return 1;
1815
1816 send_ack:
1817         return 0;
1818 }
1819
1820 void qib_rc_error(struct qib_qp *qp, enum ib_wc_status err)
1821 {
1822         unsigned long flags;
1823         int lastwqe;
1824
1825         spin_lock_irqsave(&qp->s_lock, flags);
1826         lastwqe = qib_error_qp(qp, err);
1827         spin_unlock_irqrestore(&qp->s_lock, flags);
1828
1829         if (lastwqe) {
1830                 struct ib_event ev;
1831
1832                 ev.device = qp->ibqp.device;
1833                 ev.element.qp = &qp->ibqp;
1834                 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1835                 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1836         }
1837 }
1838
1839 static inline void qib_update_ack_queue(struct qib_qp *qp, unsigned n)
1840 {
1841         unsigned next;
1842
1843         next = n + 1;
1844         if (next > QIB_MAX_RDMA_ATOMIC)
1845                 next = 0;
1846         qp->s_tail_ack_queue = next;
1847         qp->s_ack_state = OP(ACKNOWLEDGE);
1848 }
1849
1850 /**
1851  * qib_rc_rcv - process an incoming RC packet
1852  * @rcd: the context pointer
1853  * @hdr: the header of this packet
1854  * @has_grh: true if the header has a GRH
1855  * @data: the packet data
1856  * @tlen: the packet length
1857  * @qp: the QP for this packet
1858  *
1859  * This is called from qib_qp_rcv() to process an incoming RC packet
1860  * for the given QP.
1861  * Called at interrupt level.
1862  */
1863 void qib_rc_rcv(struct qib_ctxtdata *rcd, struct qib_ib_header *hdr,
1864                 int has_grh, void *data, u32 tlen, struct qib_qp *qp)
1865 {
1866         struct qib_ibport *ibp = &rcd->ppd->ibport_data;
1867         struct qib_other_headers *ohdr;
1868         u32 opcode;
1869         u32 hdrsize;
1870         u32 psn;
1871         u32 pad;
1872         struct ib_wc wc;
1873         u32 pmtu = qp->pmtu;
1874         int diff;
1875         struct ib_reth *reth;
1876         unsigned long flags;
1877         int ret;
1878
1879         /* Check for GRH */
1880         if (!has_grh) {
1881                 ohdr = &hdr->u.oth;
1882                 hdrsize = 8 + 12;       /* LRH + BTH */
1883         } else {
1884                 ohdr = &hdr->u.l.oth;
1885                 hdrsize = 8 + 40 + 12;  /* LRH + GRH + BTH */
1886         }
1887
1888         opcode = be32_to_cpu(ohdr->bth[0]);
1889         if (qib_ruc_check_hdr(ibp, hdr, has_grh, qp, opcode))
1890                 return;
1891
1892         psn = be32_to_cpu(ohdr->bth[2]);
1893         opcode >>= 24;
1894
1895         /*
1896          * Process responses (ACKs) before anything else.  Note that the
1897          * packet sequence number will be for something in the send work
1898          * queue rather than the expected receive packet sequence number.
1899          * In other words, this QP is the requester.
1900          */
1901         if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
1902             opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
1903                 qib_rc_rcv_resp(ibp, ohdr, data, tlen, qp, opcode, psn,
1904                                 hdrsize, pmtu, rcd);
1905                 return;
1906         }
1907
1908         /* Compute 24 bits worth of difference. */
1909         diff = qib_cmp24(psn, qp->r_psn);
1910         if (unlikely(diff)) {
1911                 if (qib_rc_rcv_error(ohdr, data, qp, opcode, psn, diff, rcd))
1912                         return;
1913                 goto send_ack;
1914         }
1915
1916         /* Check for opcode sequence errors. */
1917         switch (qp->r_state) {
1918         case OP(SEND_FIRST):
1919         case OP(SEND_MIDDLE):
1920                 if (opcode == OP(SEND_MIDDLE) ||
1921                     opcode == OP(SEND_LAST) ||
1922                     opcode == OP(SEND_LAST_WITH_IMMEDIATE))
1923                         break;
1924                 goto nack_inv;
1925
1926         case OP(RDMA_WRITE_FIRST):
1927         case OP(RDMA_WRITE_MIDDLE):
1928                 if (opcode == OP(RDMA_WRITE_MIDDLE) ||
1929                     opcode == OP(RDMA_WRITE_LAST) ||
1930                     opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
1931                         break;
1932                 goto nack_inv;
1933
1934         default:
1935                 if (opcode == OP(SEND_MIDDLE) ||
1936                     opcode == OP(SEND_LAST) ||
1937                     opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
1938                     opcode == OP(RDMA_WRITE_MIDDLE) ||
1939                     opcode == OP(RDMA_WRITE_LAST) ||
1940                     opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
1941                         goto nack_inv;
1942                 /*
1943                  * Note that it is up to the requester to not send a new
1944                  * RDMA read or atomic operation before receiving an ACK
1945                  * for the previous operation.
1946                  */
1947                 break;
1948         }
1949
1950         if (qp->state == IB_QPS_RTR && !(qp->r_flags & QIB_R_COMM_EST)) {
1951                 qp->r_flags |= QIB_R_COMM_EST;
1952                 if (qp->ibqp.event_handler) {
1953                         struct ib_event ev;
1954
1955                         ev.device = qp->ibqp.device;
1956                         ev.element.qp = &qp->ibqp;
1957                         ev.event = IB_EVENT_COMM_EST;
1958                         qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1959                 }
1960         }
1961
1962         /* OK, process the packet. */
1963         switch (opcode) {
1964         case OP(SEND_FIRST):
1965                 ret = qib_get_rwqe(qp, 0);
1966                 if (ret < 0)
1967                         goto nack_op_err;
1968                 if (!ret)
1969                         goto rnr_nak;
1970                 qp->r_rcv_len = 0;
1971                 /* FALLTHROUGH */
1972         case OP(SEND_MIDDLE):
1973         case OP(RDMA_WRITE_MIDDLE):
1974 send_middle:
1975                 /* Check for invalid length PMTU or posted rwqe len. */
1976                 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1977                         goto nack_inv;
1978                 qp->r_rcv_len += pmtu;
1979                 if (unlikely(qp->r_rcv_len > qp->r_len))
1980                         goto nack_inv;
1981                 qib_copy_sge(&qp->r_sge, data, pmtu, 1);
1982                 break;
1983
1984         case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
1985                 /* consume RWQE */
1986                 ret = qib_get_rwqe(qp, 1);
1987                 if (ret < 0)
1988                         goto nack_op_err;
1989                 if (!ret)
1990                         goto rnr_nak;
1991                 goto send_last_imm;
1992
1993         case OP(SEND_ONLY):
1994         case OP(SEND_ONLY_WITH_IMMEDIATE):
1995                 ret = qib_get_rwqe(qp, 0);
1996                 if (ret < 0)
1997                         goto nack_op_err;
1998                 if (!ret)
1999                         goto rnr_nak;
2000                 qp->r_rcv_len = 0;
2001                 if (opcode == OP(SEND_ONLY))
2002                         goto no_immediate_data;
2003                 /* FALLTHROUGH for SEND_ONLY_WITH_IMMEDIATE */
2004         case OP(SEND_LAST_WITH_IMMEDIATE):
2005 send_last_imm:
2006                 wc.ex.imm_data = ohdr->u.imm_data;
2007                 hdrsize += 4;
2008                 wc.wc_flags = IB_WC_WITH_IMM;
2009                 goto send_last;
2010         case OP(SEND_LAST):
2011         case OP(RDMA_WRITE_LAST):
2012 no_immediate_data:
2013                 wc.wc_flags = 0;
2014                 wc.ex.imm_data = 0;
2015 send_last:
2016                 /* Get the number of bytes the message was padded by. */
2017                 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
2018                 /* Check for invalid length. */
2019                 /* XXX LAST len should be >= 1 */
2020                 if (unlikely(tlen < (hdrsize + pad + 4)))
2021                         goto nack_inv;
2022                 /* Don't count the CRC. */
2023                 tlen -= (hdrsize + pad + 4);
2024                 wc.byte_len = tlen + qp->r_rcv_len;
2025                 if (unlikely(wc.byte_len > qp->r_len))
2026                         goto nack_inv;
2027                 qib_copy_sge(&qp->r_sge, data, tlen, 1);
2028                 while (qp->r_sge.num_sge) {
2029                         atomic_dec(&qp->r_sge.sge.mr->refcount);
2030                         if (--qp->r_sge.num_sge)
2031                                 qp->r_sge.sge = *qp->r_sge.sg_list++;
2032                 }
2033                 qp->r_msn++;
2034                 if (!test_and_clear_bit(QIB_R_WRID_VALID, &qp->r_aflags))
2035                         break;
2036                 wc.wr_id = qp->r_wr_id;
2037                 wc.status = IB_WC_SUCCESS;
2038                 if (opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE) ||
2039                     opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
2040                         wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
2041                 else
2042                         wc.opcode = IB_WC_RECV;
2043                 wc.qp = &qp->ibqp;
2044                 wc.src_qp = qp->remote_qpn;
2045                 wc.slid = qp->remote_ah_attr.dlid;
2046                 wc.sl = qp->remote_ah_attr.sl;
2047                 /* zero fields that are N/A */
2048                 wc.vendor_err = 0;
2049                 wc.pkey_index = 0;
2050                 wc.dlid_path_bits = 0;
2051                 wc.port_num = 0;
2052                 wc.csum_ok = 0;
2053                 /* Signal completion event if the solicited bit is set. */
2054                 qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
2055                              (ohdr->bth[0] &
2056                               cpu_to_be32(IB_BTH_SOLICITED)) != 0);
2057                 break;
2058
2059         case OP(RDMA_WRITE_FIRST):
2060         case OP(RDMA_WRITE_ONLY):
2061         case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
2062                 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
2063                         goto nack_inv;
2064                 /* consume RWQE */
2065                 reth = &ohdr->u.rc.reth;
2066                 hdrsize += sizeof(*reth);
2067                 qp->r_len = be32_to_cpu(reth->length);
2068                 qp->r_rcv_len = 0;
2069                 qp->r_sge.sg_list = NULL;
2070                 if (qp->r_len != 0) {
2071                         u32 rkey = be32_to_cpu(reth->rkey);
2072                         u64 vaddr = be64_to_cpu(reth->vaddr);
2073                         int ok;
2074
2075                         /* Check rkey & NAK */
2076                         ok = qib_rkey_ok(qp, &qp->r_sge.sge, qp->r_len, vaddr,
2077                                          rkey, IB_ACCESS_REMOTE_WRITE);
2078                         if (unlikely(!ok))
2079                                 goto nack_acc;
2080                         qp->r_sge.num_sge = 1;
2081                 } else {
2082                         qp->r_sge.num_sge = 0;
2083                         qp->r_sge.sge.mr = NULL;
2084                         qp->r_sge.sge.vaddr = NULL;
2085                         qp->r_sge.sge.length = 0;
2086                         qp->r_sge.sge.sge_length = 0;
2087                 }
2088                 if (opcode == OP(RDMA_WRITE_FIRST))
2089                         goto send_middle;
2090                 else if (opcode == OP(RDMA_WRITE_ONLY))
2091                         goto no_immediate_data;
2092                 ret = qib_get_rwqe(qp, 1);
2093                 if (ret < 0)
2094                         goto nack_op_err;
2095                 if (!ret)
2096                         goto rnr_nak;
2097                 wc.ex.imm_data = ohdr->u.rc.imm_data;
2098                 hdrsize += 4;
2099                 wc.wc_flags = IB_WC_WITH_IMM;
2100                 goto send_last;
2101
2102         case OP(RDMA_READ_REQUEST): {
2103                 struct qib_ack_entry *e;
2104                 u32 len;
2105                 u8 next;
2106
2107                 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
2108                         goto nack_inv;
2109                 next = qp->r_head_ack_queue + 1;
2110                 /* s_ack_queue is size QIB_MAX_RDMA_ATOMIC+1 so use > not >= */
2111                 if (next > QIB_MAX_RDMA_ATOMIC)
2112                         next = 0;
2113                 spin_lock_irqsave(&qp->s_lock, flags);
2114                 if (unlikely(next == qp->s_tail_ack_queue)) {
2115                         if (!qp->s_ack_queue[next].sent)
2116                                 goto nack_inv_unlck;
2117                         qib_update_ack_queue(qp, next);
2118                 }
2119                 e = &qp->s_ack_queue[qp->r_head_ack_queue];
2120                 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
2121                         atomic_dec(&e->rdma_sge.mr->refcount);
2122                         e->rdma_sge.mr = NULL;
2123                 }
2124                 reth = &ohdr->u.rc.reth;
2125                 len = be32_to_cpu(reth->length);
2126                 if (len) {
2127                         u32 rkey = be32_to_cpu(reth->rkey);
2128                         u64 vaddr = be64_to_cpu(reth->vaddr);
2129                         int ok;
2130
2131                         /* Check rkey & NAK */
2132                         ok = qib_rkey_ok(qp, &e->rdma_sge, len, vaddr,
2133                                          rkey, IB_ACCESS_REMOTE_READ);
2134                         if (unlikely(!ok))
2135                                 goto nack_acc_unlck;
2136                         /*
2137                          * Update the next expected PSN.  We add 1 later
2138                          * below, so only add the remainder here.
2139                          */
2140                         if (len > pmtu)
2141                                 qp->r_psn += (len - 1) / pmtu;
2142                 } else {
2143                         e->rdma_sge.mr = NULL;
2144                         e->rdma_sge.vaddr = NULL;
2145                         e->rdma_sge.length = 0;
2146                         e->rdma_sge.sge_length = 0;
2147                 }
2148                 e->opcode = opcode;
2149                 e->sent = 0;
2150                 e->psn = psn;
2151                 e->lpsn = qp->r_psn;
2152                 /*
2153                  * We need to increment the MSN here instead of when we
2154                  * finish sending the result since a duplicate request would
2155                  * increment it more than once.
2156                  */
2157                 qp->r_msn++;
2158                 qp->r_psn++;
2159                 qp->r_state = opcode;
2160                 qp->r_nak_state = 0;
2161                 qp->r_head_ack_queue = next;
2162
2163                 /* Schedule the send tasklet. */
2164                 qp->s_flags |= QIB_S_RESP_PENDING;
2165                 qib_schedule_send(qp);
2166
2167                 goto sunlock;
2168         }
2169
2170         case OP(COMPARE_SWAP):
2171         case OP(FETCH_ADD): {
2172                 struct ib_atomic_eth *ateth;
2173                 struct qib_ack_entry *e;
2174                 u64 vaddr;
2175                 atomic64_t *maddr;
2176                 u64 sdata;
2177                 u32 rkey;
2178                 u8 next;
2179
2180                 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
2181                         goto nack_inv;
2182                 next = qp->r_head_ack_queue + 1;
2183                 if (next > QIB_MAX_RDMA_ATOMIC)
2184                         next = 0;
2185                 spin_lock_irqsave(&qp->s_lock, flags);
2186                 if (unlikely(next == qp->s_tail_ack_queue)) {
2187                         if (!qp->s_ack_queue[next].sent)
2188                                 goto nack_inv_unlck;
2189                         qib_update_ack_queue(qp, next);
2190                 }
2191                 e = &qp->s_ack_queue[qp->r_head_ack_queue];
2192                 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
2193                         atomic_dec(&e->rdma_sge.mr->refcount);
2194                         e->rdma_sge.mr = NULL;
2195                 }
2196                 ateth = &ohdr->u.atomic_eth;
2197                 vaddr = ((u64) be32_to_cpu(ateth->vaddr[0]) << 32) |
2198                         be32_to_cpu(ateth->vaddr[1]);
2199                 if (unlikely(vaddr & (sizeof(u64) - 1)))
2200                         goto nack_inv_unlck;
2201                 rkey = be32_to_cpu(ateth->rkey);
2202                 /* Check rkey & NAK */
2203                 if (unlikely(!qib_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
2204                                           vaddr, rkey,
2205                                           IB_ACCESS_REMOTE_ATOMIC)))
2206                         goto nack_acc_unlck;
2207                 /* Perform atomic OP and save result. */
2208                 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
2209                 sdata = be64_to_cpu(ateth->swap_data);
2210                 e->atomic_data = (opcode == OP(FETCH_ADD)) ?
2211                         (u64) atomic64_add_return(sdata, maddr) - sdata :
2212                         (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
2213                                       be64_to_cpu(ateth->compare_data),
2214                                       sdata);
2215                 atomic_dec(&qp->r_sge.sge.mr->refcount);
2216                 qp->r_sge.num_sge = 0;
2217                 e->opcode = opcode;
2218                 e->sent = 0;
2219                 e->psn = psn;
2220                 e->lpsn = psn;
2221                 qp->r_msn++;
2222                 qp->r_psn++;
2223                 qp->r_state = opcode;
2224                 qp->r_nak_state = 0;
2225                 qp->r_head_ack_queue = next;
2226
2227                 /* Schedule the send tasklet. */
2228                 qp->s_flags |= QIB_S_RESP_PENDING;
2229                 qib_schedule_send(qp);
2230
2231                 goto sunlock;
2232         }
2233
2234         default:
2235                 /* NAK unknown opcodes. */
2236                 goto nack_inv;
2237         }
2238         qp->r_psn++;
2239         qp->r_state = opcode;
2240         qp->r_ack_psn = psn;
2241         qp->r_nak_state = 0;
2242         /* Send an ACK if requested or required. */
2243         if (psn & (1 << 31))
2244                 goto send_ack;
2245         return;
2246
2247 rnr_nak:
2248         qp->r_nak_state = IB_RNR_NAK | qp->r_min_rnr_timer;
2249         qp->r_ack_psn = qp->r_psn;
2250         /* Queue RNR NAK for later */
2251         if (list_empty(&qp->rspwait)) {
2252                 qp->r_flags |= QIB_R_RSP_NAK;
2253                 atomic_inc(&qp->refcount);
2254                 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
2255         }
2256         return;
2257
2258 nack_op_err:
2259         qib_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
2260         qp->r_nak_state = IB_NAK_REMOTE_OPERATIONAL_ERROR;
2261         qp->r_ack_psn = qp->r_psn;
2262         /* Queue NAK for later */
2263         if (list_empty(&qp->rspwait)) {
2264                 qp->r_flags |= QIB_R_RSP_NAK;
2265                 atomic_inc(&qp->refcount);
2266                 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
2267         }
2268         return;
2269
2270 nack_inv_unlck:
2271         spin_unlock_irqrestore(&qp->s_lock, flags);
2272 nack_inv:
2273         qib_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
2274         qp->r_nak_state = IB_NAK_INVALID_REQUEST;
2275         qp->r_ack_psn = qp->r_psn;
2276         /* Queue NAK for later */
2277         if (list_empty(&qp->rspwait)) {
2278                 qp->r_flags |= QIB_R_RSP_NAK;
2279                 atomic_inc(&qp->refcount);
2280                 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
2281         }
2282         return;
2283
2284 nack_acc_unlck:
2285         spin_unlock_irqrestore(&qp->s_lock, flags);
2286 nack_acc:
2287         qib_rc_error(qp, IB_WC_LOC_PROT_ERR);
2288         qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
2289         qp->r_ack_psn = qp->r_psn;
2290 send_ack:
2291         qib_send_rc_ack(qp);
2292         return;
2293
2294 sunlock:
2295         spin_unlock_irqrestore(&qp->s_lock, flags);
2296 }