]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/infiniband/hw/ipath/ipath_rc.c
IB/ipath: Fix RC and UC error handling
[linux-2.6-omap-h63xx.git] / drivers / infiniband / hw / ipath / ipath_rc.c
1 /*
2  * Copyright (c) 2006, 2007, 2008 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 "ipath_verbs.h"
37 #include "ipath_kernel.h"
38
39 /* cut down ridiculously long IB macro names */
40 #define OP(x) IB_OPCODE_RC_##x
41
42 static u32 restart_sge(struct ipath_sge_state *ss, struct ipath_swqe *wqe,
43                        u32 psn, u32 pmtu)
44 {
45         u32 len;
46
47         len = ((psn - wqe->psn) & IPATH_PSN_MASK) * pmtu;
48         ss->sge = wqe->sg_list[0];
49         ss->sg_list = wqe->sg_list + 1;
50         ss->num_sge = wqe->wr.num_sge;
51         ipath_skip_sge(ss, len);
52         return wqe->length - len;
53 }
54
55 /**
56  * ipath_init_restart- initialize the qp->s_sge after a restart
57  * @qp: the QP who's SGE we're restarting
58  * @wqe: the work queue to initialize the QP's SGE from
59  *
60  * The QP s_lock should be held and interrupts disabled.
61  */
62 static void ipath_init_restart(struct ipath_qp *qp, struct ipath_swqe *wqe)
63 {
64         struct ipath_ibdev *dev;
65
66         qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn,
67                                 ib_mtu_enum_to_int(qp->path_mtu));
68         dev = to_idev(qp->ibqp.device);
69         spin_lock(&dev->pending_lock);
70         if (list_empty(&qp->timerwait))
71                 list_add_tail(&qp->timerwait,
72                               &dev->pending[dev->pending_index]);
73         spin_unlock(&dev->pending_lock);
74 }
75
76 /**
77  * ipath_make_rc_ack - construct a response packet (ACK, NAK, or RDMA read)
78  * @qp: a pointer to the QP
79  * @ohdr: a pointer to the IB header being constructed
80  * @pmtu: the path MTU
81  *
82  * Return 1 if constructed; otherwise, return 0.
83  * Note that we are in the responder's side of the QP context.
84  * Note the QP s_lock must be held.
85  */
86 static int ipath_make_rc_ack(struct ipath_ibdev *dev, struct ipath_qp *qp,
87                              struct ipath_other_headers *ohdr, u32 pmtu)
88 {
89         struct ipath_ack_entry *e;
90         u32 hwords;
91         u32 len;
92         u32 bth0;
93         u32 bth2;
94
95         /* header size in 32-bit words LRH+BTH = (8+12)/4. */
96         hwords = 5;
97
98         switch (qp->s_ack_state) {
99         case OP(RDMA_READ_RESPONSE_LAST):
100         case OP(RDMA_READ_RESPONSE_ONLY):
101         case OP(ATOMIC_ACKNOWLEDGE):
102                 /*
103                  * We can increment the tail pointer now that the last
104                  * response has been sent instead of only being
105                  * constructed.
106                  */
107                 if (++qp->s_tail_ack_queue > IPATH_MAX_RDMA_ATOMIC)
108                         qp->s_tail_ack_queue = 0;
109                 /* FALLTHROUGH */
110         case OP(SEND_ONLY):
111         case OP(ACKNOWLEDGE):
112                 /* Check for no next entry in the queue. */
113                 if (qp->r_head_ack_queue == qp->s_tail_ack_queue) {
114                         if (qp->s_flags & IPATH_S_ACK_PENDING)
115                                 goto normal;
116                         qp->s_ack_state = OP(ACKNOWLEDGE);
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                         /* Copy SGE state in case we need to resend */
123                         qp->s_ack_rdma_sge = e->rdma_sge;
124                         qp->s_cur_sge = &qp->s_ack_rdma_sge;
125                         len = e->rdma_sge.sge.sge_length;
126                         if (len > pmtu) {
127                                 len = pmtu;
128                                 qp->s_ack_state = OP(RDMA_READ_RESPONSE_FIRST);
129                         } else {
130                                 qp->s_ack_state = OP(RDMA_READ_RESPONSE_ONLY);
131                                 e->sent = 1;
132                         }
133                         ohdr->u.aeth = ipath_compute_aeth(qp);
134                         hwords++;
135                         qp->s_ack_rdma_psn = e->psn;
136                         bth2 = qp->s_ack_rdma_psn++ & IPATH_PSN_MASK;
137                 } else {
138                         /* COMPARE_SWAP or FETCH_ADD */
139                         qp->s_cur_sge = NULL;
140                         len = 0;
141                         qp->s_ack_state = OP(ATOMIC_ACKNOWLEDGE);
142                         ohdr->u.at.aeth = ipath_compute_aeth(qp);
143                         ohdr->u.at.atomic_ack_eth[0] =
144                                 cpu_to_be32(e->atomic_data >> 32);
145                         ohdr->u.at.atomic_ack_eth[1] =
146                                 cpu_to_be32(e->atomic_data);
147                         hwords += sizeof(ohdr->u.at) / sizeof(u32);
148                         bth2 = e->psn;
149                         e->sent = 1;
150                 }
151                 bth0 = qp->s_ack_state << 24;
152                 break;
153
154         case OP(RDMA_READ_RESPONSE_FIRST):
155                 qp->s_ack_state = OP(RDMA_READ_RESPONSE_MIDDLE);
156                 /* FALLTHROUGH */
157         case OP(RDMA_READ_RESPONSE_MIDDLE):
158                 len = qp->s_ack_rdma_sge.sge.sge_length;
159                 if (len > pmtu)
160                         len = pmtu;
161                 else {
162                         ohdr->u.aeth = ipath_compute_aeth(qp);
163                         hwords++;
164                         qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
165                         qp->s_ack_queue[qp->s_tail_ack_queue].sent = 1;
166                 }
167                 bth0 = qp->s_ack_state << 24;
168                 bth2 = qp->s_ack_rdma_psn++ & IPATH_PSN_MASK;
169                 break;
170
171         default:
172         normal:
173                 /*
174                  * Send a regular ACK.
175                  * Set the s_ack_state so we wait until after sending
176                  * the ACK before setting s_ack_state to ACKNOWLEDGE
177                  * (see above).
178                  */
179                 qp->s_ack_state = OP(SEND_ONLY);
180                 qp->s_flags &= ~IPATH_S_ACK_PENDING;
181                 qp->s_cur_sge = NULL;
182                 if (qp->s_nak_state)
183                         ohdr->u.aeth =
184                                 cpu_to_be32((qp->r_msn & IPATH_MSN_MASK) |
185                                             (qp->s_nak_state <<
186                                              IPATH_AETH_CREDIT_SHIFT));
187                 else
188                         ohdr->u.aeth = ipath_compute_aeth(qp);
189                 hwords++;
190                 len = 0;
191                 bth0 = OP(ACKNOWLEDGE) << 24;
192                 bth2 = qp->s_ack_psn & IPATH_PSN_MASK;
193         }
194         qp->s_hdrwords = hwords;
195         qp->s_cur_size = len;
196         ipath_make_ruc_header(dev, qp, ohdr, bth0, bth2);
197         return 1;
198
199 bail:
200         return 0;
201 }
202
203 /**
204  * ipath_make_rc_req - construct a request packet (SEND, RDMA r/w, ATOMIC)
205  * @qp: a pointer to the QP
206  *
207  * Return 1 if constructed; otherwise, return 0.
208  */
209 int ipath_make_rc_req(struct ipath_qp *qp)
210 {
211         struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
212         struct ipath_other_headers *ohdr;
213         struct ipath_sge_state *ss;
214         struct ipath_swqe *wqe;
215         u32 hwords;
216         u32 len;
217         u32 bth0;
218         u32 bth2;
219         u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
220         char newreq;
221         unsigned long flags;
222         int ret = 0;
223
224         ohdr = &qp->s_hdr.u.oth;
225         if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
226                 ohdr = &qp->s_hdr.u.l.oth;
227
228         /*
229          * The lock is needed to synchronize between the sending tasklet,
230          * the receive interrupt handler, and timeout resends.
231          */
232         spin_lock_irqsave(&qp->s_lock, flags);
233
234         /* Sending responses has higher priority over sending requests. */
235         if ((qp->r_head_ack_queue != qp->s_tail_ack_queue ||
236              (qp->s_flags & IPATH_S_ACK_PENDING) ||
237              qp->s_ack_state != OP(ACKNOWLEDGE)) &&
238             ipath_make_rc_ack(dev, qp, ohdr, pmtu))
239                 goto done;
240
241         if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_SEND_OK) ||
242             qp->s_rnr_timeout || qp->s_wait_credit)
243                 goto bail;
244
245         /* Limit the number of packets sent without an ACK. */
246         if (ipath_cmp24(qp->s_psn, qp->s_last_psn + IPATH_PSN_CREDIT) > 0) {
247                 qp->s_wait_credit = 1;
248                 dev->n_rc_stalls++;
249                 goto bail;
250         }
251
252         /* header size in 32-bit words LRH+BTH = (8+12)/4. */
253         hwords = 5;
254         bth0 = 1 << 22; /* Set M bit */
255
256         /* Send a request. */
257         wqe = get_swqe_ptr(qp, qp->s_cur);
258         switch (qp->s_state) {
259         default:
260                 /*
261                  * Resend an old request or start a new one.
262                  *
263                  * We keep track of the current SWQE so that
264                  * we don't reset the "furthest progress" state
265                  * if we need to back up.
266                  */
267                 newreq = 0;
268                 if (qp->s_cur == qp->s_tail) {
269                         /* Check if send work queue is empty. */
270                         if (qp->s_tail == qp->s_head)
271                                 goto bail;
272                         /*
273                          * If a fence is requested, wait for previous
274                          * RDMA read and atomic operations to finish.
275                          */
276                         if ((wqe->wr.send_flags & IB_SEND_FENCE) &&
277                             qp->s_num_rd_atomic) {
278                                 qp->s_flags |= IPATH_S_FENCE_PENDING;
279                                 goto bail;
280                         }
281                         wqe->psn = qp->s_next_psn;
282                         newreq = 1;
283                 }
284                 /*
285                  * Note that we have to be careful not to modify the
286                  * original work request since we may need to resend
287                  * it.
288                  */
289                 len = wqe->length;
290                 ss = &qp->s_sge;
291                 bth2 = 0;
292                 switch (wqe->wr.opcode) {
293                 case IB_WR_SEND:
294                 case IB_WR_SEND_WITH_IMM:
295                         /* If no credit, return. */
296                         if (qp->s_lsn != (u32) -1 &&
297                             ipath_cmp24(wqe->ssn, qp->s_lsn + 1) > 0)
298                                 goto bail;
299                         wqe->lpsn = wqe->psn;
300                         if (len > pmtu) {
301                                 wqe->lpsn += (len - 1) / pmtu;
302                                 qp->s_state = OP(SEND_FIRST);
303                                 len = pmtu;
304                                 break;
305                         }
306                         if (wqe->wr.opcode == IB_WR_SEND)
307                                 qp->s_state = OP(SEND_ONLY);
308                         else {
309                                 qp->s_state = OP(SEND_ONLY_WITH_IMMEDIATE);
310                                 /* Immediate data comes after the BTH */
311                                 ohdr->u.imm_data = wqe->wr.ex.imm_data;
312                                 hwords += 1;
313                         }
314                         if (wqe->wr.send_flags & IB_SEND_SOLICITED)
315                                 bth0 |= 1 << 23;
316                         bth2 = 1 << 31; /* Request ACK. */
317                         if (++qp->s_cur == qp->s_size)
318                                 qp->s_cur = 0;
319                         break;
320
321                 case IB_WR_RDMA_WRITE:
322                         if (newreq && qp->s_lsn != (u32) -1)
323                                 qp->s_lsn++;
324                         /* FALLTHROUGH */
325                 case IB_WR_RDMA_WRITE_WITH_IMM:
326                         /* If no credit, return. */
327                         if (qp->s_lsn != (u32) -1 &&
328                             ipath_cmp24(wqe->ssn, qp->s_lsn + 1) > 0)
329                                 goto bail;
330                         ohdr->u.rc.reth.vaddr =
331                                 cpu_to_be64(wqe->wr.wr.rdma.remote_addr);
332                         ohdr->u.rc.reth.rkey =
333                                 cpu_to_be32(wqe->wr.wr.rdma.rkey);
334                         ohdr->u.rc.reth.length = cpu_to_be32(len);
335                         hwords += sizeof(struct ib_reth) / sizeof(u32);
336                         wqe->lpsn = wqe->psn;
337                         if (len > pmtu) {
338                                 wqe->lpsn += (len - 1) / pmtu;
339                                 qp->s_state = OP(RDMA_WRITE_FIRST);
340                                 len = pmtu;
341                                 break;
342                         }
343                         if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
344                                 qp->s_state = OP(RDMA_WRITE_ONLY);
345                         else {
346                                 qp->s_state =
347                                         OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
348                                 /* Immediate data comes after RETH */
349                                 ohdr->u.rc.imm_data = wqe->wr.ex.imm_data;
350                                 hwords += 1;
351                                 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
352                                         bth0 |= 1 << 23;
353                         }
354                         bth2 = 1 << 31; /* Request ACK. */
355                         if (++qp->s_cur == qp->s_size)
356                                 qp->s_cur = 0;
357                         break;
358
359                 case IB_WR_RDMA_READ:
360                         /*
361                          * Don't allow more operations to be started
362                          * than the QP limits allow.
363                          */
364                         if (newreq) {
365                                 if (qp->s_num_rd_atomic >=
366                                     qp->s_max_rd_atomic) {
367                                         qp->s_flags |= IPATH_S_RDMAR_PENDING;
368                                         goto bail;
369                                 }
370                                 qp->s_num_rd_atomic++;
371                                 if (qp->s_lsn != (u32) -1)
372                                         qp->s_lsn++;
373                                 /*
374                                  * Adjust s_next_psn to count the
375                                  * expected number of responses.
376                                  */
377                                 if (len > pmtu)
378                                         qp->s_next_psn += (len - 1) / pmtu;
379                                 wqe->lpsn = qp->s_next_psn++;
380                         }
381                         ohdr->u.rc.reth.vaddr =
382                                 cpu_to_be64(wqe->wr.wr.rdma.remote_addr);
383                         ohdr->u.rc.reth.rkey =
384                                 cpu_to_be32(wqe->wr.wr.rdma.rkey);
385                         ohdr->u.rc.reth.length = cpu_to_be32(len);
386                         qp->s_state = OP(RDMA_READ_REQUEST);
387                         hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
388                         ss = NULL;
389                         len = 0;
390                         if (++qp->s_cur == qp->s_size)
391                                 qp->s_cur = 0;
392                         break;
393
394                 case IB_WR_ATOMIC_CMP_AND_SWP:
395                 case IB_WR_ATOMIC_FETCH_AND_ADD:
396                         /*
397                          * Don't allow more operations to be started
398                          * than the QP limits allow.
399                          */
400                         if (newreq) {
401                                 if (qp->s_num_rd_atomic >=
402                                     qp->s_max_rd_atomic) {
403                                         qp->s_flags |= IPATH_S_RDMAR_PENDING;
404                                         goto bail;
405                                 }
406                                 qp->s_num_rd_atomic++;
407                                 if (qp->s_lsn != (u32) -1)
408                                         qp->s_lsn++;
409                                 wqe->lpsn = wqe->psn;
410                         }
411                         if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
412                                 qp->s_state = OP(COMPARE_SWAP);
413                                 ohdr->u.atomic_eth.swap_data = cpu_to_be64(
414                                         wqe->wr.wr.atomic.swap);
415                                 ohdr->u.atomic_eth.compare_data = cpu_to_be64(
416                                         wqe->wr.wr.atomic.compare_add);
417                         } else {
418                                 qp->s_state = OP(FETCH_ADD);
419                                 ohdr->u.atomic_eth.swap_data = cpu_to_be64(
420                                         wqe->wr.wr.atomic.compare_add);
421                                 ohdr->u.atomic_eth.compare_data = 0;
422                         }
423                         ohdr->u.atomic_eth.vaddr[0] = cpu_to_be32(
424                                 wqe->wr.wr.atomic.remote_addr >> 32);
425                         ohdr->u.atomic_eth.vaddr[1] = cpu_to_be32(
426                                 wqe->wr.wr.atomic.remote_addr);
427                         ohdr->u.atomic_eth.rkey = cpu_to_be32(
428                                 wqe->wr.wr.atomic.rkey);
429                         hwords += sizeof(struct ib_atomic_eth) / sizeof(u32);
430                         ss = NULL;
431                         len = 0;
432                         if (++qp->s_cur == qp->s_size)
433                                 qp->s_cur = 0;
434                         break;
435
436                 default:
437                         goto bail;
438                 }
439                 qp->s_sge.sge = wqe->sg_list[0];
440                 qp->s_sge.sg_list = wqe->sg_list + 1;
441                 qp->s_sge.num_sge = wqe->wr.num_sge;
442                 qp->s_len = wqe->length;
443                 if (newreq) {
444                         qp->s_tail++;
445                         if (qp->s_tail >= qp->s_size)
446                                 qp->s_tail = 0;
447                 }
448                 bth2 |= qp->s_psn & IPATH_PSN_MASK;
449                 if (wqe->wr.opcode == IB_WR_RDMA_READ)
450                         qp->s_psn = wqe->lpsn + 1;
451                 else {
452                         qp->s_psn++;
453                         if (ipath_cmp24(qp->s_psn, qp->s_next_psn) > 0)
454                                 qp->s_next_psn = qp->s_psn;
455                 }
456                 /*
457                  * Put the QP on the pending list so lost ACKs will cause
458                  * a retry.  More than one request can be pending so the
459                  * QP may already be on the dev->pending list.
460                  */
461                 spin_lock(&dev->pending_lock);
462                 if (list_empty(&qp->timerwait))
463                         list_add_tail(&qp->timerwait,
464                                       &dev->pending[dev->pending_index]);
465                 spin_unlock(&dev->pending_lock);
466                 break;
467
468         case OP(RDMA_READ_RESPONSE_FIRST):
469                 /*
470                  * This case can only happen if a send is restarted.
471                  * See ipath_restart_rc().
472                  */
473                 ipath_init_restart(qp, wqe);
474                 /* FALLTHROUGH */
475         case OP(SEND_FIRST):
476                 qp->s_state = OP(SEND_MIDDLE);
477                 /* FALLTHROUGH */
478         case OP(SEND_MIDDLE):
479                 bth2 = qp->s_psn++ & IPATH_PSN_MASK;
480                 if (ipath_cmp24(qp->s_psn, qp->s_next_psn) > 0)
481                         qp->s_next_psn = qp->s_psn;
482                 ss = &qp->s_sge;
483                 len = qp->s_len;
484                 if (len > pmtu) {
485                         len = pmtu;
486                         break;
487                 }
488                 if (wqe->wr.opcode == IB_WR_SEND)
489                         qp->s_state = OP(SEND_LAST);
490                 else {
491                         qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
492                         /* Immediate data comes after the BTH */
493                         ohdr->u.imm_data = wqe->wr.ex.imm_data;
494                         hwords += 1;
495                 }
496                 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
497                         bth0 |= 1 << 23;
498                 bth2 |= 1 << 31;        /* Request ACK. */
499                 qp->s_cur++;
500                 if (qp->s_cur >= qp->s_size)
501                         qp->s_cur = 0;
502                 break;
503
504         case OP(RDMA_READ_RESPONSE_LAST):
505                 /*
506                  * This case can only happen if a RDMA write is restarted.
507                  * See ipath_restart_rc().
508                  */
509                 ipath_init_restart(qp, wqe);
510                 /* FALLTHROUGH */
511         case OP(RDMA_WRITE_FIRST):
512                 qp->s_state = OP(RDMA_WRITE_MIDDLE);
513                 /* FALLTHROUGH */
514         case OP(RDMA_WRITE_MIDDLE):
515                 bth2 = qp->s_psn++ & IPATH_PSN_MASK;
516                 if (ipath_cmp24(qp->s_psn, qp->s_next_psn) > 0)
517                         qp->s_next_psn = qp->s_psn;
518                 ss = &qp->s_sge;
519                 len = qp->s_len;
520                 if (len > pmtu) {
521                         len = pmtu;
522                         break;
523                 }
524                 if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
525                         qp->s_state = OP(RDMA_WRITE_LAST);
526                 else {
527                         qp->s_state = OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
528                         /* Immediate data comes after the BTH */
529                         ohdr->u.imm_data = wqe->wr.ex.imm_data;
530                         hwords += 1;
531                         if (wqe->wr.send_flags & IB_SEND_SOLICITED)
532                                 bth0 |= 1 << 23;
533                 }
534                 bth2 |= 1 << 31;        /* Request ACK. */
535                 qp->s_cur++;
536                 if (qp->s_cur >= qp->s_size)
537                         qp->s_cur = 0;
538                 break;
539
540         case OP(RDMA_READ_RESPONSE_MIDDLE):
541                 /*
542                  * This case can only happen if a RDMA read is restarted.
543                  * See ipath_restart_rc().
544                  */
545                 ipath_init_restart(qp, wqe);
546                 len = ((qp->s_psn - wqe->psn) & IPATH_PSN_MASK) * pmtu;
547                 ohdr->u.rc.reth.vaddr =
548                         cpu_to_be64(wqe->wr.wr.rdma.remote_addr + len);
549                 ohdr->u.rc.reth.rkey =
550                         cpu_to_be32(wqe->wr.wr.rdma.rkey);
551                 ohdr->u.rc.reth.length = cpu_to_be32(qp->s_len);
552                 qp->s_state = OP(RDMA_READ_REQUEST);
553                 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
554                 bth2 = qp->s_psn++ & IPATH_PSN_MASK;
555                 if (ipath_cmp24(qp->s_psn, qp->s_next_psn) > 0)
556                         qp->s_next_psn = qp->s_psn;
557                 ss = NULL;
558                 len = 0;
559                 qp->s_cur++;
560                 if (qp->s_cur == qp->s_size)
561                         qp->s_cur = 0;
562                 break;
563         }
564         if (ipath_cmp24(qp->s_psn, qp->s_last_psn + IPATH_PSN_CREDIT - 1) >= 0)
565                 bth2 |= 1 << 31;        /* Request ACK. */
566         qp->s_len -= len;
567         qp->s_hdrwords = hwords;
568         qp->s_cur_sge = ss;
569         qp->s_cur_size = len;
570         ipath_make_ruc_header(dev, qp, ohdr, bth0 | (qp->s_state << 24), bth2);
571 done:
572         ret = 1;
573 bail:
574         spin_unlock_irqrestore(&qp->s_lock, flags);
575         return ret;
576 }
577
578 /**
579  * send_rc_ack - Construct an ACK packet and send it
580  * @qp: a pointer to the QP
581  *
582  * This is called from ipath_rc_rcv() and only uses the receive
583  * side QP state.
584  * Note that RDMA reads and atomics are handled in the
585  * send side QP state and tasklet.
586  */
587 static void send_rc_ack(struct ipath_qp *qp)
588 {
589         struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
590         struct ipath_devdata *dd;
591         u16 lrh0;
592         u32 bth0;
593         u32 hwords;
594         u32 __iomem *piobuf;
595         struct ipath_ib_header hdr;
596         struct ipath_other_headers *ohdr;
597         unsigned long flags;
598
599         spin_lock_irqsave(&qp->s_lock, flags);
600
601         /* Don't send ACK or NAK if a RDMA read or atomic is pending. */
602         if (qp->r_head_ack_queue != qp->s_tail_ack_queue ||
603             (qp->s_flags & IPATH_S_ACK_PENDING) ||
604             qp->s_ack_state != OP(ACKNOWLEDGE))
605                 goto queue_ack;
606
607         spin_unlock_irqrestore(&qp->s_lock, flags);
608
609         dd = dev->dd;
610         piobuf = ipath_getpiobuf(dd, 0, NULL);
611         if (!piobuf) {
612                 /*
613                  * We are out of PIO buffers at the moment.
614                  * Pass responsibility for sending the ACK to the
615                  * send tasklet so that when a PIO buffer becomes
616                  * available, the ACK is sent ahead of other outgoing
617                  * packets.
618                  */
619                 spin_lock_irqsave(&qp->s_lock, flags);
620                 goto queue_ack;
621         }
622
623         /* Construct the header. */
624         ohdr = &hdr.u.oth;
625         lrh0 = IPATH_LRH_BTH;
626         /* header size in 32-bit words LRH+BTH+AETH = (8+12+4)/4. */
627         hwords = 6;
628         if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
629                 hwords += ipath_make_grh(dev, &hdr.u.l.grh,
630                                          &qp->remote_ah_attr.grh,
631                                          hwords, 0);
632                 ohdr = &hdr.u.l.oth;
633                 lrh0 = IPATH_LRH_GRH;
634         }
635         /* read pkey_index w/o lock (its atomic) */
636         bth0 = ipath_get_pkey(dd, qp->s_pkey_index) |
637                 (OP(ACKNOWLEDGE) << 24) | (1 << 22);
638         if (qp->r_nak_state)
639                 ohdr->u.aeth = cpu_to_be32((qp->r_msn & IPATH_MSN_MASK) |
640                                             (qp->r_nak_state <<
641                                              IPATH_AETH_CREDIT_SHIFT));
642         else
643                 ohdr->u.aeth = ipath_compute_aeth(qp);
644         lrh0 |= qp->remote_ah_attr.sl << 4;
645         hdr.lrh[0] = cpu_to_be16(lrh0);
646         hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
647         hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
648         hdr.lrh[3] = cpu_to_be16(dd->ipath_lid);
649         ohdr->bth[0] = cpu_to_be32(bth0);
650         ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
651         ohdr->bth[2] = cpu_to_be32(qp->r_ack_psn & IPATH_PSN_MASK);
652
653         writeq(hwords + 1, piobuf);
654
655         if (dd->ipath_flags & IPATH_PIO_FLUSH_WC) {
656                 u32 *hdrp = (u32 *) &hdr;
657
658                 ipath_flush_wc();
659                 __iowrite32_copy(piobuf + 2, hdrp, hwords - 1);
660                 ipath_flush_wc();
661                 __raw_writel(hdrp[hwords - 1], piobuf + hwords + 1);
662         } else
663                 __iowrite32_copy(piobuf + 2, (u32 *) &hdr, hwords);
664
665         ipath_flush_wc();
666
667         dev->n_unicast_xmit++;
668         goto done;
669
670 queue_ack:
671         dev->n_rc_qacks++;
672         qp->s_flags |= IPATH_S_ACK_PENDING;
673         qp->s_nak_state = qp->r_nak_state;
674         qp->s_ack_psn = qp->r_ack_psn;
675         spin_unlock_irqrestore(&qp->s_lock, flags);
676
677         /* Call ipath_do_rc_send() in another thread. */
678         tasklet_hi_schedule(&qp->s_task);
679
680 done:
681         return;
682 }
683
684 /**
685  * reset_psn - reset the QP state to send starting from PSN
686  * @qp: the QP
687  * @psn: the packet sequence number to restart at
688  *
689  * This is called from ipath_rc_rcv() to process an incoming RC ACK
690  * for the given QP.
691  * Called at interrupt level with the QP s_lock held.
692  */
693 static void reset_psn(struct ipath_qp *qp, u32 psn)
694 {
695         u32 n = qp->s_last;
696         struct ipath_swqe *wqe = get_swqe_ptr(qp, n);
697         u32 opcode;
698
699         qp->s_cur = n;
700
701         /*
702          * If we are starting the request from the beginning,
703          * let the normal send code handle initialization.
704          */
705         if (ipath_cmp24(psn, wqe->psn) <= 0) {
706                 qp->s_state = OP(SEND_LAST);
707                 goto done;
708         }
709
710         /* Find the work request opcode corresponding to the given PSN. */
711         opcode = wqe->wr.opcode;
712         for (;;) {
713                 int diff;
714
715                 if (++n == qp->s_size)
716                         n = 0;
717                 if (n == qp->s_tail)
718                         break;
719                 wqe = get_swqe_ptr(qp, n);
720                 diff = ipath_cmp24(psn, wqe->psn);
721                 if (diff < 0)
722                         break;
723                 qp->s_cur = n;
724                 /*
725                  * If we are starting the request from the beginning,
726                  * let the normal send code handle initialization.
727                  */
728                 if (diff == 0) {
729                         qp->s_state = OP(SEND_LAST);
730                         goto done;
731                 }
732                 opcode = wqe->wr.opcode;
733         }
734
735         /*
736          * Set the state to restart in the middle of a request.
737          * Don't change the s_sge, s_cur_sge, or s_cur_size.
738          * See ipath_do_rc_send().
739          */
740         switch (opcode) {
741         case IB_WR_SEND:
742         case IB_WR_SEND_WITH_IMM:
743                 qp->s_state = OP(RDMA_READ_RESPONSE_FIRST);
744                 break;
745
746         case IB_WR_RDMA_WRITE:
747         case IB_WR_RDMA_WRITE_WITH_IMM:
748                 qp->s_state = OP(RDMA_READ_RESPONSE_LAST);
749                 break;
750
751         case IB_WR_RDMA_READ:
752                 qp->s_state = OP(RDMA_READ_RESPONSE_MIDDLE);
753                 break;
754
755         default:
756                 /*
757                  * This case shouldn't happen since its only
758                  * one PSN per req.
759                  */
760                 qp->s_state = OP(SEND_LAST);
761         }
762 done:
763         qp->s_psn = psn;
764 }
765
766 /**
767  * ipath_restart_rc - back up requester to resend the last un-ACKed request
768  * @qp: the QP to restart
769  * @psn: packet sequence number for the request
770  * @wc: the work completion request
771  *
772  * The QP s_lock should be held and interrupts disabled.
773  */
774 void ipath_restart_rc(struct ipath_qp *qp, u32 psn)
775 {
776         struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
777         struct ipath_ibdev *dev;
778
779         if (qp->s_retry == 0) {
780                 ipath_send_complete(qp, wqe, IB_WC_RETRY_EXC_ERR);
781                 ipath_error_qp(qp, IB_WC_WR_FLUSH_ERR);
782                 goto bail;
783         }
784         qp->s_retry--;
785
786         /*
787          * Remove the QP from the timeout queue.
788          * Note: it may already have been removed by ipath_ib_timer().
789          */
790         dev = to_idev(qp->ibqp.device);
791         spin_lock(&dev->pending_lock);
792         if (!list_empty(&qp->timerwait))
793                 list_del_init(&qp->timerwait);
794         if (!list_empty(&qp->piowait))
795                 list_del_init(&qp->piowait);
796         spin_unlock(&dev->pending_lock);
797
798         if (wqe->wr.opcode == IB_WR_RDMA_READ)
799                 dev->n_rc_resends++;
800         else
801                 dev->n_rc_resends += (qp->s_psn - psn) & IPATH_PSN_MASK;
802
803         reset_psn(qp, psn);
804         tasklet_hi_schedule(&qp->s_task);
805
806 bail:
807         return;
808 }
809
810 static inline void update_last_psn(struct ipath_qp *qp, u32 psn)
811 {
812         if (qp->s_last_psn != psn) {
813                 qp->s_last_psn = psn;
814                 if (qp->s_wait_credit) {
815                         qp->s_wait_credit = 0;
816                         tasklet_hi_schedule(&qp->s_task);
817                 }
818         }
819 }
820
821 /**
822  * do_rc_ack - process an incoming RC ACK
823  * @qp: the QP the ACK came in on
824  * @psn: the packet sequence number of the ACK
825  * @opcode: the opcode of the request that resulted in the ACK
826  *
827  * This is called from ipath_rc_rcv_resp() to process an incoming RC ACK
828  * for the given QP.
829  * Called at interrupt level with the QP s_lock held and interrupts disabled.
830  * Returns 1 if OK, 0 if current operation should be aborted (NAK).
831  */
832 static int do_rc_ack(struct ipath_qp *qp, u32 aeth, u32 psn, int opcode,
833                      u64 val)
834 {
835         struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
836         struct ib_wc wc;
837         enum ib_wc_status status;
838         struct ipath_swqe *wqe;
839         int ret = 0;
840         u32 ack_psn;
841         int diff;
842
843         /*
844          * Remove the QP from the timeout queue (or RNR timeout queue).
845          * If ipath_ib_timer() has already removed it,
846          * it's OK since we hold the QP s_lock and ipath_restart_rc()
847          * just won't find anything to restart if we ACK everything.
848          */
849         spin_lock(&dev->pending_lock);
850         if (!list_empty(&qp->timerwait))
851                 list_del_init(&qp->timerwait);
852         spin_unlock(&dev->pending_lock);
853
854         /*
855          * Note that NAKs implicitly ACK outstanding SEND and RDMA write
856          * requests and implicitly NAK RDMA read and atomic requests issued
857          * before the NAK'ed request.  The MSN won't include the NAK'ed
858          * request but will include an ACK'ed request(s).
859          */
860         ack_psn = psn;
861         if (aeth >> 29)
862                 ack_psn--;
863         wqe = get_swqe_ptr(qp, qp->s_last);
864
865         /*
866          * The MSN might be for a later WQE than the PSN indicates so
867          * only complete WQEs that the PSN finishes.
868          */
869         while ((diff = ipath_cmp24(ack_psn, wqe->lpsn)) >= 0) {
870                 /*
871                  * RDMA_READ_RESPONSE_ONLY is a special case since
872                  * we want to generate completion events for everything
873                  * before the RDMA read, copy the data, then generate
874                  * the completion for the read.
875                  */
876                 if (wqe->wr.opcode == IB_WR_RDMA_READ &&
877                     opcode == OP(RDMA_READ_RESPONSE_ONLY) &&
878                     diff == 0) {
879                         ret = 1;
880                         goto bail;
881                 }
882                 /*
883                  * If this request is a RDMA read or atomic, and the ACK is
884                  * for a later operation, this ACK NAKs the RDMA read or
885                  * atomic.  In other words, only a RDMA_READ_LAST or ONLY
886                  * can ACK a RDMA read and likewise for atomic ops.  Note
887                  * that the NAK case can only happen if relaxed ordering is
888                  * used and requests are sent after an RDMA read or atomic
889                  * is sent but before the response is received.
890                  */
891                 if ((wqe->wr.opcode == IB_WR_RDMA_READ &&
892                      (opcode != OP(RDMA_READ_RESPONSE_LAST) || diff != 0)) ||
893                     ((wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
894                       wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) &&
895                      (opcode != OP(ATOMIC_ACKNOWLEDGE) || diff != 0))) {
896                         /*
897                          * The last valid PSN seen is the previous
898                          * request's.
899                          */
900                         update_last_psn(qp, wqe->psn - 1);
901                         /* Retry this request. */
902                         ipath_restart_rc(qp, wqe->psn);
903                         /*
904                          * No need to process the ACK/NAK since we are
905                          * restarting an earlier request.
906                          */
907                         goto bail;
908                 }
909                 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
910                     wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
911                         *(u64 *) wqe->sg_list[0].vaddr = val;
912                 if (qp->s_num_rd_atomic &&
913                     (wqe->wr.opcode == IB_WR_RDMA_READ ||
914                      wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
915                      wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)) {
916                         qp->s_num_rd_atomic--;
917                         /* Restart sending task if fence is complete */
918                         if ((qp->s_flags & IPATH_S_FENCE_PENDING) &&
919                             !qp->s_num_rd_atomic) {
920                                 qp->s_flags &= ~IPATH_S_FENCE_PENDING;
921                                 tasklet_hi_schedule(&qp->s_task);
922                         } else if (qp->s_flags & IPATH_S_RDMAR_PENDING) {
923                                 qp->s_flags &= ~IPATH_S_RDMAR_PENDING;
924                                 tasklet_hi_schedule(&qp->s_task);
925                         }
926                 }
927                 /* Post a send completion queue entry if requested. */
928                 if (!(qp->s_flags & IPATH_S_SIGNAL_REQ_WR) ||
929                     (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
930                         memset(&wc, 0, sizeof wc);
931                         wc.wr_id = wqe->wr.wr_id;
932                         wc.status = IB_WC_SUCCESS;
933                         wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
934                         wc.byte_len = wqe->length;
935                         wc.qp = &qp->ibqp;
936                         wc.src_qp = qp->remote_qpn;
937                         wc.slid = qp->remote_ah_attr.dlid;
938                         wc.sl = qp->remote_ah_attr.sl;
939                         ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 0);
940                 }
941                 qp->s_retry = qp->s_retry_cnt;
942                 /*
943                  * If we are completing a request which is in the process of
944                  * being resent, we can stop resending it since we know the
945                  * responder has already seen it.
946                  */
947                 if (qp->s_last == qp->s_cur) {
948                         if (++qp->s_cur >= qp->s_size)
949                                 qp->s_cur = 0;
950                         qp->s_last = qp->s_cur;
951                         if (qp->s_last == qp->s_tail)
952                                 break;
953                         wqe = get_swqe_ptr(qp, qp->s_cur);
954                         qp->s_state = OP(SEND_LAST);
955                         qp->s_psn = wqe->psn;
956                 } else {
957                         if (++qp->s_last >= qp->s_size)
958                                 qp->s_last = 0;
959                         if (qp->s_last == qp->s_tail)
960                                 break;
961                         wqe = get_swqe_ptr(qp, qp->s_last);
962                 }
963         }
964
965         switch (aeth >> 29) {
966         case 0:         /* ACK */
967                 dev->n_rc_acks++;
968                 /* If this is a partial ACK, reset the retransmit timer. */
969                 if (qp->s_last != qp->s_tail) {
970                         spin_lock(&dev->pending_lock);
971                         if (list_empty(&qp->timerwait))
972                                 list_add_tail(&qp->timerwait,
973                                         &dev->pending[dev->pending_index]);
974                         spin_unlock(&dev->pending_lock);
975                         /*
976                          * If we get a partial ACK for a resent operation,
977                          * we can stop resending the earlier packets and
978                          * continue with the next packet the receiver wants.
979                          */
980                         if (ipath_cmp24(qp->s_psn, psn) <= 0) {
981                                 reset_psn(qp, psn + 1);
982                                 tasklet_hi_schedule(&qp->s_task);
983                         }
984                 } else if (ipath_cmp24(qp->s_psn, psn) <= 0) {
985                         qp->s_state = OP(SEND_LAST);
986                         qp->s_psn = psn + 1;
987                 }
988                 ipath_get_credit(qp, aeth);
989                 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
990                 qp->s_retry = qp->s_retry_cnt;
991                 update_last_psn(qp, psn);
992                 ret = 1;
993                 goto bail;
994
995         case 1:         /* RNR NAK */
996                 dev->n_rnr_naks++;
997                 if (qp->s_last == qp->s_tail)
998                         goto bail;
999                 if (qp->s_rnr_retry == 0) {
1000                         status = IB_WC_RNR_RETRY_EXC_ERR;
1001                         goto class_b;
1002                 }
1003                 if (qp->s_rnr_retry_cnt < 7)
1004                         qp->s_rnr_retry--;
1005
1006                 /* The last valid PSN is the previous PSN. */
1007                 update_last_psn(qp, psn - 1);
1008
1009                 if (wqe->wr.opcode == IB_WR_RDMA_READ)
1010                         dev->n_rc_resends++;
1011                 else
1012                         dev->n_rc_resends +=
1013                                 (qp->s_psn - psn) & IPATH_PSN_MASK;
1014
1015                 reset_psn(qp, psn);
1016
1017                 qp->s_rnr_timeout =
1018                         ib_ipath_rnr_table[(aeth >> IPATH_AETH_CREDIT_SHIFT) &
1019                                            IPATH_AETH_CREDIT_MASK];
1020                 ipath_insert_rnr_queue(qp);
1021                 goto bail;
1022
1023         case 3:         /* NAK */
1024                 if (qp->s_last == qp->s_tail)
1025                         goto bail;
1026                 /* The last valid PSN is the previous PSN. */
1027                 update_last_psn(qp, psn - 1);
1028                 switch ((aeth >> IPATH_AETH_CREDIT_SHIFT) &
1029                         IPATH_AETH_CREDIT_MASK) {
1030                 case 0: /* PSN sequence error */
1031                         dev->n_seq_naks++;
1032                         /*
1033                          * Back up to the responder's expected PSN.
1034                          * Note that we might get a NAK in the middle of an
1035                          * RDMA READ response which terminates the RDMA
1036                          * READ.
1037                          */
1038                         ipath_restart_rc(qp, psn);
1039                         break;
1040
1041                 case 1: /* Invalid Request */
1042                         status = IB_WC_REM_INV_REQ_ERR;
1043                         dev->n_other_naks++;
1044                         goto class_b;
1045
1046                 case 2: /* Remote Access Error */
1047                         status = IB_WC_REM_ACCESS_ERR;
1048                         dev->n_other_naks++;
1049                         goto class_b;
1050
1051                 case 3: /* Remote Operation Error */
1052                         status = IB_WC_REM_OP_ERR;
1053                         dev->n_other_naks++;
1054                 class_b:
1055                         ipath_send_complete(qp, wqe, status);
1056                         ipath_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1057                         break;
1058
1059                 default:
1060                         /* Ignore other reserved NAK error codes */
1061                         goto reserved;
1062                 }
1063                 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1064                 goto bail;
1065
1066         default:                /* 2: reserved */
1067         reserved:
1068                 /* Ignore reserved NAK codes. */
1069                 goto bail;
1070         }
1071
1072 bail:
1073         return ret;
1074 }
1075
1076 /**
1077  * ipath_rc_rcv_resp - process an incoming RC response packet
1078  * @dev: the device this packet came in on
1079  * @ohdr: the other headers for this packet
1080  * @data: the packet data
1081  * @tlen: the packet length
1082  * @qp: the QP for this packet
1083  * @opcode: the opcode for this packet
1084  * @psn: the packet sequence number for this packet
1085  * @hdrsize: the header length
1086  * @pmtu: the path MTU
1087  * @header_in_data: true if part of the header data is in the data buffer
1088  *
1089  * This is called from ipath_rc_rcv() to process an incoming RC response
1090  * packet for the given QP.
1091  * Called at interrupt level.
1092  */
1093 static inline void ipath_rc_rcv_resp(struct ipath_ibdev *dev,
1094                                      struct ipath_other_headers *ohdr,
1095                                      void *data, u32 tlen,
1096                                      struct ipath_qp *qp,
1097                                      u32 opcode,
1098                                      u32 psn, u32 hdrsize, u32 pmtu,
1099                                      int header_in_data)
1100 {
1101         struct ipath_swqe *wqe;
1102         enum ib_wc_status status;
1103         unsigned long flags;
1104         int diff;
1105         u32 pad;
1106         u32 aeth;
1107         u64 val;
1108
1109         spin_lock_irqsave(&qp->s_lock, flags);
1110
1111         /* Ignore invalid responses. */
1112         if (ipath_cmp24(psn, qp->s_next_psn) >= 0)
1113                 goto ack_done;
1114
1115         /* Ignore duplicate responses. */
1116         diff = ipath_cmp24(psn, qp->s_last_psn);
1117         if (unlikely(diff <= 0)) {
1118                 /* Update credits for "ghost" ACKs */
1119                 if (diff == 0 && opcode == OP(ACKNOWLEDGE)) {
1120                         if (!header_in_data)
1121                                 aeth = be32_to_cpu(ohdr->u.aeth);
1122                         else {
1123                                 aeth = be32_to_cpu(((__be32 *) data)[0]);
1124                                 data += sizeof(__be32);
1125                         }
1126                         if ((aeth >> 29) == 0)
1127                                 ipath_get_credit(qp, aeth);
1128                 }
1129                 goto ack_done;
1130         }
1131
1132         if (unlikely(qp->s_last == qp->s_tail))
1133                 goto ack_done;
1134         wqe = get_swqe_ptr(qp, qp->s_last);
1135         status = IB_WC_SUCCESS;
1136
1137         switch (opcode) {
1138         case OP(ACKNOWLEDGE):
1139         case OP(ATOMIC_ACKNOWLEDGE):
1140         case OP(RDMA_READ_RESPONSE_FIRST):
1141                 if (!header_in_data)
1142                         aeth = be32_to_cpu(ohdr->u.aeth);
1143                 else {
1144                         aeth = be32_to_cpu(((__be32 *) data)[0]);
1145                         data += sizeof(__be32);
1146                 }
1147                 if (opcode == OP(ATOMIC_ACKNOWLEDGE)) {
1148                         if (!header_in_data) {
1149                                 __be32 *p = ohdr->u.at.atomic_ack_eth;
1150
1151                                 val = ((u64) be32_to_cpu(p[0]) << 32) |
1152                                         be32_to_cpu(p[1]);
1153                         } else
1154                                 val = be64_to_cpu(((__be64 *) data)[0]);
1155                 } else
1156                         val = 0;
1157                 if (!do_rc_ack(qp, aeth, psn, opcode, val) ||
1158                     opcode != OP(RDMA_READ_RESPONSE_FIRST))
1159                         goto ack_done;
1160                 hdrsize += 4;
1161                 wqe = get_swqe_ptr(qp, qp->s_last);
1162                 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1163                         goto ack_op_err;
1164                 /*
1165                  * If this is a response to a resent RDMA read, we
1166                  * have to be careful to copy the data to the right
1167                  * location.
1168                  */
1169                 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1170                                                   wqe, psn, pmtu);
1171                 goto read_middle;
1172
1173         case OP(RDMA_READ_RESPONSE_MIDDLE):
1174                 /* no AETH, no ACK */
1175                 if (unlikely(ipath_cmp24(psn, qp->s_last_psn + 1))) {
1176                         dev->n_rdma_seq++;
1177                         ipath_restart_rc(qp, qp->s_last_psn + 1);
1178                         goto ack_done;
1179                 }
1180                 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1181                         goto ack_op_err;
1182         read_middle:
1183                 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1184                         goto ack_len_err;
1185                 if (unlikely(pmtu >= qp->s_rdma_read_len))
1186                         goto ack_len_err;
1187
1188                 /* We got a response so update the timeout. */
1189                 spin_lock(&dev->pending_lock);
1190                 if (qp->s_rnr_timeout == 0 && !list_empty(&qp->timerwait))
1191                         list_move_tail(&qp->timerwait,
1192                                        &dev->pending[dev->pending_index]);
1193                 spin_unlock(&dev->pending_lock);
1194
1195                 if (opcode == OP(RDMA_READ_RESPONSE_MIDDLE))
1196                         qp->s_retry = qp->s_retry_cnt;
1197
1198                 /*
1199                  * Update the RDMA receive state but do the copy w/o
1200                  * holding the locks and blocking interrupts.
1201                  */
1202                 qp->s_rdma_read_len -= pmtu;
1203                 update_last_psn(qp, psn);
1204                 spin_unlock_irqrestore(&qp->s_lock, flags);
1205                 ipath_copy_sge(&qp->s_rdma_read_sge, data, pmtu);
1206                 goto bail;
1207
1208         case OP(RDMA_READ_RESPONSE_ONLY):
1209                 if (!header_in_data)
1210                         aeth = be32_to_cpu(ohdr->u.aeth);
1211                 else
1212                         aeth = be32_to_cpu(((__be32 *) data)[0]);
1213                 if (!do_rc_ack(qp, aeth, psn, opcode, 0))
1214                         goto ack_done;
1215                 /* Get the number of bytes the message was padded by. */
1216                 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1217                 /*
1218                  * Check that the data size is >= 0 && <= pmtu.
1219                  * Remember to account for the AETH header (4) and
1220                  * ICRC (4).
1221                  */
1222                 if (unlikely(tlen < (hdrsize + pad + 8)))
1223                         goto ack_len_err;
1224                 /*
1225                  * If this is a response to a resent RDMA read, we
1226                  * have to be careful to copy the data to the right
1227                  * location.
1228                  */
1229                 wqe = get_swqe_ptr(qp, qp->s_last);
1230                 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1231                                                   wqe, psn, pmtu);
1232                 goto read_last;
1233
1234         case OP(RDMA_READ_RESPONSE_LAST):
1235                 /* ACKs READ req. */
1236                 if (unlikely(ipath_cmp24(psn, qp->s_last_psn + 1))) {
1237                         dev->n_rdma_seq++;
1238                         ipath_restart_rc(qp, qp->s_last_psn + 1);
1239                         goto ack_done;
1240                 }
1241                 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1242                         goto ack_op_err;
1243                 /* Get the number of bytes the message was padded by. */
1244                 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1245                 /*
1246                  * Check that the data size is >= 1 && <= pmtu.
1247                  * Remember to account for the AETH header (4) and
1248                  * ICRC (4).
1249                  */
1250                 if (unlikely(tlen <= (hdrsize + pad + 8)))
1251                         goto ack_len_err;
1252         read_last:
1253                 tlen -= hdrsize + pad + 8;
1254                 if (unlikely(tlen != qp->s_rdma_read_len))
1255                         goto ack_len_err;
1256                 if (!header_in_data)
1257                         aeth = be32_to_cpu(ohdr->u.aeth);
1258                 else {
1259                         aeth = be32_to_cpu(((__be32 *) data)[0]);
1260                         data += sizeof(__be32);
1261                 }
1262                 ipath_copy_sge(&qp->s_rdma_read_sge, data, tlen);
1263                 (void) do_rc_ack(qp, aeth, psn,
1264                                  OP(RDMA_READ_RESPONSE_LAST), 0);
1265                 goto ack_done;
1266         }
1267
1268 ack_op_err:
1269         status = IB_WC_LOC_QP_OP_ERR;
1270         goto ack_err;
1271
1272 ack_len_err:
1273         status = IB_WC_LOC_LEN_ERR;
1274 ack_err:
1275         ipath_send_complete(qp, wqe, status);
1276         ipath_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1277 ack_done:
1278         spin_unlock_irqrestore(&qp->s_lock, flags);
1279 bail:
1280         return;
1281 }
1282
1283 /**
1284  * ipath_rc_rcv_error - process an incoming duplicate or error RC packet
1285  * @dev: the device this packet came in on
1286  * @ohdr: the other headers for this packet
1287  * @data: the packet data
1288  * @qp: the QP for this packet
1289  * @opcode: the opcode for this packet
1290  * @psn: the packet sequence number for this packet
1291  * @diff: the difference between the PSN and the expected PSN
1292  * @header_in_data: true if part of the header data is in the data buffer
1293  *
1294  * This is called from ipath_rc_rcv() to process an unexpected
1295  * incoming RC packet for the given QP.
1296  * Called at interrupt level.
1297  * Return 1 if no more processing is needed; otherwise return 0 to
1298  * schedule a response to be sent.
1299  */
1300 static inline int ipath_rc_rcv_error(struct ipath_ibdev *dev,
1301                                      struct ipath_other_headers *ohdr,
1302                                      void *data,
1303                                      struct ipath_qp *qp,
1304                                      u32 opcode,
1305                                      u32 psn,
1306                                      int diff,
1307                                      int header_in_data)
1308 {
1309         struct ipath_ack_entry *e;
1310         u8 i, prev;
1311         int old_req;
1312         unsigned long flags;
1313
1314         if (diff > 0) {
1315                 /*
1316                  * Packet sequence error.
1317                  * A NAK will ACK earlier sends and RDMA writes.
1318                  * Don't queue the NAK if we already sent one.
1319                  */
1320                 if (!qp->r_nak_state) {
1321                         qp->r_nak_state = IB_NAK_PSN_ERROR;
1322                         /* Use the expected PSN. */
1323                         qp->r_ack_psn = qp->r_psn;
1324                         goto send_ack;
1325                 }
1326                 goto done;
1327         }
1328
1329         /*
1330          * Handle a duplicate request.  Don't re-execute SEND, RDMA
1331          * write or atomic op.  Don't NAK errors, just silently drop
1332          * the duplicate request.  Note that r_sge, r_len, and
1333          * r_rcv_len may be in use so don't modify them.
1334          *
1335          * We are supposed to ACK the earliest duplicate PSN but we
1336          * can coalesce an outstanding duplicate ACK.  We have to
1337          * send the earliest so that RDMA reads can be restarted at
1338          * the requester's expected PSN.
1339          *
1340          * First, find where this duplicate PSN falls within the
1341          * ACKs previously sent.
1342          */
1343         psn &= IPATH_PSN_MASK;
1344         e = NULL;
1345         old_req = 1;
1346         spin_lock_irqsave(&qp->s_lock, flags);
1347         for (i = qp->r_head_ack_queue; ; i = prev) {
1348                 if (i == qp->s_tail_ack_queue)
1349                         old_req = 0;
1350                 if (i)
1351                         prev = i - 1;
1352                 else
1353                         prev = IPATH_MAX_RDMA_ATOMIC;
1354                 if (prev == qp->r_head_ack_queue) {
1355                         e = NULL;
1356                         break;
1357                 }
1358                 e = &qp->s_ack_queue[prev];
1359                 if (!e->opcode) {
1360                         e = NULL;
1361                         break;
1362                 }
1363                 if (ipath_cmp24(psn, e->psn) >= 0) {
1364                         if (prev == qp->s_tail_ack_queue)
1365                                 old_req = 0;
1366                         break;
1367                 }
1368         }
1369         switch (opcode) {
1370         case OP(RDMA_READ_REQUEST): {
1371                 struct ib_reth *reth;
1372                 u32 offset;
1373                 u32 len;
1374
1375                 /*
1376                  * If we didn't find the RDMA read request in the ack queue,
1377                  * or the send tasklet is already backed up to send an
1378                  * earlier entry, we can ignore this request.
1379                  */
1380                 if (!e || e->opcode != OP(RDMA_READ_REQUEST) || old_req)
1381                         goto unlock_done;
1382                 /* RETH comes after BTH */
1383                 if (!header_in_data)
1384                         reth = &ohdr->u.rc.reth;
1385                 else {
1386                         reth = (struct ib_reth *)data;
1387                         data += sizeof(*reth);
1388                 }
1389                 /*
1390                  * Address range must be a subset of the original
1391                  * request and start on pmtu boundaries.
1392                  * We reuse the old ack_queue slot since the requester
1393                  * should not back up and request an earlier PSN for the
1394                  * same request.
1395                  */
1396                 offset = ((psn - e->psn) & IPATH_PSN_MASK) *
1397                         ib_mtu_enum_to_int(qp->path_mtu);
1398                 len = be32_to_cpu(reth->length);
1399                 if (unlikely(offset + len > e->rdma_sge.sge.sge_length))
1400                         goto unlock_done;
1401                 if (len != 0) {
1402                         u32 rkey = be32_to_cpu(reth->rkey);
1403                         u64 vaddr = be64_to_cpu(reth->vaddr);
1404                         int ok;
1405
1406                         ok = ipath_rkey_ok(qp, &e->rdma_sge,
1407                                            len, vaddr, rkey,
1408                                            IB_ACCESS_REMOTE_READ);
1409                         if (unlikely(!ok))
1410                                 goto unlock_done;
1411                 } else {
1412                         e->rdma_sge.sg_list = NULL;
1413                         e->rdma_sge.num_sge = 0;
1414                         e->rdma_sge.sge.mr = NULL;
1415                         e->rdma_sge.sge.vaddr = NULL;
1416                         e->rdma_sge.sge.length = 0;
1417                         e->rdma_sge.sge.sge_length = 0;
1418                 }
1419                 e->psn = psn;
1420                 qp->s_ack_state = OP(ACKNOWLEDGE);
1421                 qp->s_tail_ack_queue = prev;
1422                 break;
1423         }
1424
1425         case OP(COMPARE_SWAP):
1426         case OP(FETCH_ADD): {
1427                 /*
1428                  * If we didn't find the atomic request in the ack queue
1429                  * or the send tasklet is already backed up to send an
1430                  * earlier entry, we can ignore this request.
1431                  */
1432                 if (!e || e->opcode != (u8) opcode || old_req)
1433                         goto unlock_done;
1434                 qp->s_ack_state = OP(ACKNOWLEDGE);
1435                 qp->s_tail_ack_queue = prev;
1436                 break;
1437         }
1438
1439         default:
1440                 if (old_req)
1441                         goto unlock_done;
1442                 /*
1443                  * Resend the most recent ACK if this request is
1444                  * after all the previous RDMA reads and atomics.
1445                  */
1446                 if (i == qp->r_head_ack_queue) {
1447                         spin_unlock_irqrestore(&qp->s_lock, flags);
1448                         qp->r_nak_state = 0;
1449                         qp->r_ack_psn = qp->r_psn - 1;
1450                         goto send_ack;
1451                 }
1452                 /*
1453                  * Try to send a simple ACK to work around a Mellanox bug
1454                  * which doesn't accept a RDMA read response or atomic
1455                  * response as an ACK for earlier SENDs or RDMA writes.
1456                  */
1457                 if (qp->r_head_ack_queue == qp->s_tail_ack_queue &&
1458                     !(qp->s_flags & IPATH_S_ACK_PENDING) &&
1459                     qp->s_ack_state == OP(ACKNOWLEDGE)) {
1460                         spin_unlock_irqrestore(&qp->s_lock, flags);
1461                         qp->r_nak_state = 0;
1462                         qp->r_ack_psn = qp->s_ack_queue[i].psn - 1;
1463                         goto send_ack;
1464                 }
1465                 /*
1466                  * Resend the RDMA read or atomic op which
1467                  * ACKs this duplicate request.
1468                  */
1469                 qp->s_ack_state = OP(ACKNOWLEDGE);
1470                 qp->s_tail_ack_queue = i;
1471                 break;
1472         }
1473         qp->r_nak_state = 0;
1474         tasklet_hi_schedule(&qp->s_task);
1475
1476 unlock_done:
1477         spin_unlock_irqrestore(&qp->s_lock, flags);
1478 done:
1479         return 1;
1480
1481 send_ack:
1482         return 0;
1483 }
1484
1485 void ipath_rc_error(struct ipath_qp *qp, enum ib_wc_status err)
1486 {
1487         unsigned long flags;
1488         int lastwqe;
1489
1490         spin_lock_irqsave(&qp->s_lock, flags);
1491         lastwqe = ipath_error_qp(qp, err);
1492         spin_unlock_irqrestore(&qp->s_lock, flags);
1493
1494         if (lastwqe) {
1495                 struct ib_event ev;
1496
1497                 ev.device = qp->ibqp.device;
1498                 ev.element.qp = &qp->ibqp;
1499                 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1500                 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1501         }
1502 }
1503
1504 static inline void ipath_update_ack_queue(struct ipath_qp *qp, unsigned n)
1505 {
1506         unsigned long flags;
1507         unsigned next;
1508
1509         next = n + 1;
1510         if (next > IPATH_MAX_RDMA_ATOMIC)
1511                 next = 0;
1512         spin_lock_irqsave(&qp->s_lock, flags);
1513         if (n == qp->s_tail_ack_queue) {
1514                 qp->s_tail_ack_queue = next;
1515                 qp->s_ack_state = OP(ACKNOWLEDGE);
1516         }
1517         spin_unlock_irqrestore(&qp->s_lock, flags);
1518 }
1519
1520 /**
1521  * ipath_rc_rcv - process an incoming RC packet
1522  * @dev: the device this packet came in on
1523  * @hdr: the header of this packet
1524  * @has_grh: true if the header has a GRH
1525  * @data: the packet data
1526  * @tlen: the packet length
1527  * @qp: the QP for this packet
1528  *
1529  * This is called from ipath_qp_rcv() to process an incoming RC packet
1530  * for the given QP.
1531  * Called at interrupt level.
1532  */
1533 void ipath_rc_rcv(struct ipath_ibdev *dev, struct ipath_ib_header *hdr,
1534                   int has_grh, void *data, u32 tlen, struct ipath_qp *qp)
1535 {
1536         struct ipath_other_headers *ohdr;
1537         u32 opcode;
1538         u32 hdrsize;
1539         u32 psn;
1540         u32 pad;
1541         struct ib_wc wc;
1542         u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
1543         int diff;
1544         struct ib_reth *reth;
1545         int header_in_data;
1546
1547         /* Validate the SLID. See Ch. 9.6.1.5 */
1548         if (unlikely(be16_to_cpu(hdr->lrh[3]) != qp->remote_ah_attr.dlid))
1549                 goto done;
1550
1551         /* Check for GRH */
1552         if (!has_grh) {
1553                 ohdr = &hdr->u.oth;
1554                 hdrsize = 8 + 12;       /* LRH + BTH */
1555                 psn = be32_to_cpu(ohdr->bth[2]);
1556                 header_in_data = 0;
1557         } else {
1558                 ohdr = &hdr->u.l.oth;
1559                 hdrsize = 8 + 40 + 12;  /* LRH + GRH + BTH */
1560                 /*
1561                  * The header with GRH is 60 bytes and the core driver sets
1562                  * the eager header buffer size to 56 bytes so the last 4
1563                  * bytes of the BTH header (PSN) is in the data buffer.
1564                  */
1565                 header_in_data = dev->dd->ipath_rcvhdrentsize == 16;
1566                 if (header_in_data) {
1567                         psn = be32_to_cpu(((__be32 *) data)[0]);
1568                         data += sizeof(__be32);
1569                 } else
1570                         psn = be32_to_cpu(ohdr->bth[2]);
1571         }
1572
1573         /*
1574          * Process responses (ACKs) before anything else.  Note that the
1575          * packet sequence number will be for something in the send work
1576          * queue rather than the expected receive packet sequence number.
1577          * In other words, this QP is the requester.
1578          */
1579         opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
1580         if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
1581             opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
1582                 ipath_rc_rcv_resp(dev, ohdr, data, tlen, qp, opcode, psn,
1583                                   hdrsize, pmtu, header_in_data);
1584                 goto done;
1585         }
1586
1587         /* Compute 24 bits worth of difference. */
1588         diff = ipath_cmp24(psn, qp->r_psn);
1589         if (unlikely(diff)) {
1590                 if (ipath_rc_rcv_error(dev, ohdr, data, qp, opcode,
1591                                        psn, diff, header_in_data))
1592                         goto done;
1593                 goto send_ack;
1594         }
1595
1596         /* Check for opcode sequence errors. */
1597         switch (qp->r_state) {
1598         case OP(SEND_FIRST):
1599         case OP(SEND_MIDDLE):
1600                 if (opcode == OP(SEND_MIDDLE) ||
1601                     opcode == OP(SEND_LAST) ||
1602                     opcode == OP(SEND_LAST_WITH_IMMEDIATE))
1603                         break;
1604                 goto nack_inv;
1605
1606         case OP(RDMA_WRITE_FIRST):
1607         case OP(RDMA_WRITE_MIDDLE):
1608                 if (opcode == OP(RDMA_WRITE_MIDDLE) ||
1609                     opcode == OP(RDMA_WRITE_LAST) ||
1610                     opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
1611                         break;
1612                 goto nack_inv;
1613
1614         default:
1615                 if (opcode == OP(SEND_MIDDLE) ||
1616                     opcode == OP(SEND_LAST) ||
1617                     opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
1618                     opcode == OP(RDMA_WRITE_MIDDLE) ||
1619                     opcode == OP(RDMA_WRITE_LAST) ||
1620                     opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
1621                         goto nack_inv;
1622                 /*
1623                  * Note that it is up to the requester to not send a new
1624                  * RDMA read or atomic operation before receiving an ACK
1625                  * for the previous operation.
1626                  */
1627                 break;
1628         }
1629
1630         memset(&wc, 0, sizeof wc);
1631
1632         /* OK, process the packet. */
1633         switch (opcode) {
1634         case OP(SEND_FIRST):
1635                 if (!ipath_get_rwqe(qp, 0))
1636                         goto rnr_nak;
1637                 qp->r_rcv_len = 0;
1638                 /* FALLTHROUGH */
1639         case OP(SEND_MIDDLE):
1640         case OP(RDMA_WRITE_MIDDLE):
1641         send_middle:
1642                 /* Check for invalid length PMTU or posted rwqe len. */
1643                 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1644                         goto nack_inv;
1645                 qp->r_rcv_len += pmtu;
1646                 if (unlikely(qp->r_rcv_len > qp->r_len))
1647                         goto nack_inv;
1648                 ipath_copy_sge(&qp->r_sge, data, pmtu);
1649                 break;
1650
1651         case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
1652                 /* consume RWQE */
1653                 if (!ipath_get_rwqe(qp, 1))
1654                         goto rnr_nak;
1655                 goto send_last_imm;
1656
1657         case OP(SEND_ONLY):
1658         case OP(SEND_ONLY_WITH_IMMEDIATE):
1659                 if (!ipath_get_rwqe(qp, 0))
1660                         goto rnr_nak;
1661                 qp->r_rcv_len = 0;
1662                 if (opcode == OP(SEND_ONLY))
1663                         goto send_last;
1664                 /* FALLTHROUGH */
1665         case OP(SEND_LAST_WITH_IMMEDIATE):
1666         send_last_imm:
1667                 if (header_in_data) {
1668                         wc.imm_data = *(__be32 *) data;
1669                         data += sizeof(__be32);
1670                 } else {
1671                         /* Immediate data comes after BTH */
1672                         wc.imm_data = ohdr->u.imm_data;
1673                 }
1674                 hdrsize += 4;
1675                 wc.wc_flags = IB_WC_WITH_IMM;
1676                 /* FALLTHROUGH */
1677         case OP(SEND_LAST):
1678         case OP(RDMA_WRITE_LAST):
1679         send_last:
1680                 /* Get the number of bytes the message was padded by. */
1681                 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1682                 /* Check for invalid length. */
1683                 /* XXX LAST len should be >= 1 */
1684                 if (unlikely(tlen < (hdrsize + pad + 4)))
1685                         goto nack_inv;
1686                 /* Don't count the CRC. */
1687                 tlen -= (hdrsize + pad + 4);
1688                 wc.byte_len = tlen + qp->r_rcv_len;
1689                 if (unlikely(wc.byte_len > qp->r_len))
1690                         goto nack_inv;
1691                 ipath_copy_sge(&qp->r_sge, data, tlen);
1692                 qp->r_msn++;
1693                 if (!qp->r_wrid_valid)
1694                         break;
1695                 qp->r_wrid_valid = 0;
1696                 wc.wr_id = qp->r_wr_id;
1697                 wc.status = IB_WC_SUCCESS;
1698                 if (opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE) ||
1699                     opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
1700                         wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
1701                 else
1702                         wc.opcode = IB_WC_RECV;
1703                 wc.qp = &qp->ibqp;
1704                 wc.src_qp = qp->remote_qpn;
1705                 wc.slid = qp->remote_ah_attr.dlid;
1706                 wc.sl = qp->remote_ah_attr.sl;
1707                 /* Signal completion event if the solicited bit is set. */
1708                 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
1709                                (ohdr->bth[0] &
1710                                 __constant_cpu_to_be32(1 << 23)) != 0);
1711                 break;
1712
1713         case OP(RDMA_WRITE_FIRST):
1714         case OP(RDMA_WRITE_ONLY):
1715         case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
1716                 if (unlikely(!(qp->qp_access_flags &
1717                                IB_ACCESS_REMOTE_WRITE)))
1718                         goto nack_inv;
1719                 /* consume RWQE */
1720                 /* RETH comes after BTH */
1721                 if (!header_in_data)
1722                         reth = &ohdr->u.rc.reth;
1723                 else {
1724                         reth = (struct ib_reth *)data;
1725                         data += sizeof(*reth);
1726                 }
1727                 hdrsize += sizeof(*reth);
1728                 qp->r_len = be32_to_cpu(reth->length);
1729                 qp->r_rcv_len = 0;
1730                 if (qp->r_len != 0) {
1731                         u32 rkey = be32_to_cpu(reth->rkey);
1732                         u64 vaddr = be64_to_cpu(reth->vaddr);
1733                         int ok;
1734
1735                         /* Check rkey & NAK */
1736                         ok = ipath_rkey_ok(qp, &qp->r_sge,
1737                                            qp->r_len, vaddr, rkey,
1738                                            IB_ACCESS_REMOTE_WRITE);
1739                         if (unlikely(!ok))
1740                                 goto nack_acc;
1741                 } else {
1742                         qp->r_sge.sg_list = NULL;
1743                         qp->r_sge.sge.mr = NULL;
1744                         qp->r_sge.sge.vaddr = NULL;
1745                         qp->r_sge.sge.length = 0;
1746                         qp->r_sge.sge.sge_length = 0;
1747                 }
1748                 if (opcode == OP(RDMA_WRITE_FIRST))
1749                         goto send_middle;
1750                 else if (opcode == OP(RDMA_WRITE_ONLY))
1751                         goto send_last;
1752                 if (!ipath_get_rwqe(qp, 1))
1753                         goto rnr_nak;
1754                 goto send_last_imm;
1755
1756         case OP(RDMA_READ_REQUEST): {
1757                 struct ipath_ack_entry *e;
1758                 u32 len;
1759                 u8 next;
1760
1761                 if (unlikely(!(qp->qp_access_flags &
1762                                IB_ACCESS_REMOTE_READ)))
1763                         goto nack_inv;
1764                 next = qp->r_head_ack_queue + 1;
1765                 if (next > IPATH_MAX_RDMA_ATOMIC)
1766                         next = 0;
1767                 if (unlikely(next == qp->s_tail_ack_queue)) {
1768                         if (!qp->s_ack_queue[next].sent)
1769                                 goto nack_inv;
1770                         ipath_update_ack_queue(qp, next);
1771                 }
1772                 e = &qp->s_ack_queue[qp->r_head_ack_queue];
1773                 /* RETH comes after BTH */
1774                 if (!header_in_data)
1775                         reth = &ohdr->u.rc.reth;
1776                 else {
1777                         reth = (struct ib_reth *)data;
1778                         data += sizeof(*reth);
1779                 }
1780                 len = be32_to_cpu(reth->length);
1781                 if (len) {
1782                         u32 rkey = be32_to_cpu(reth->rkey);
1783                         u64 vaddr = be64_to_cpu(reth->vaddr);
1784                         int ok;
1785
1786                         /* Check rkey & NAK */
1787                         ok = ipath_rkey_ok(qp, &e->rdma_sge, len, vaddr,
1788                                            rkey, IB_ACCESS_REMOTE_READ);
1789                         if (unlikely(!ok))
1790                                 goto nack_acc;
1791                         /*
1792                          * Update the next expected PSN.  We add 1 later
1793                          * below, so only add the remainder here.
1794                          */
1795                         if (len > pmtu)
1796                                 qp->r_psn += (len - 1) / pmtu;
1797                 } else {
1798                         e->rdma_sge.sg_list = NULL;
1799                         e->rdma_sge.num_sge = 0;
1800                         e->rdma_sge.sge.mr = NULL;
1801                         e->rdma_sge.sge.vaddr = NULL;
1802                         e->rdma_sge.sge.length = 0;
1803                         e->rdma_sge.sge.sge_length = 0;
1804                 }
1805                 e->opcode = opcode;
1806                 e->sent = 0;
1807                 e->psn = psn;
1808                 /*
1809                  * We need to increment the MSN here instead of when we
1810                  * finish sending the result since a duplicate request would
1811                  * increment it more than once.
1812                  */
1813                 qp->r_msn++;
1814                 qp->r_psn++;
1815                 qp->r_state = opcode;
1816                 qp->r_nak_state = 0;
1817                 barrier();
1818                 qp->r_head_ack_queue = next;
1819
1820                 /* Call ipath_do_rc_send() in another thread. */
1821                 tasklet_hi_schedule(&qp->s_task);
1822
1823                 goto done;
1824         }
1825
1826         case OP(COMPARE_SWAP):
1827         case OP(FETCH_ADD): {
1828                 struct ib_atomic_eth *ateth;
1829                 struct ipath_ack_entry *e;
1830                 u64 vaddr;
1831                 atomic64_t *maddr;
1832                 u64 sdata;
1833                 u32 rkey;
1834                 u8 next;
1835
1836                 if (unlikely(!(qp->qp_access_flags &
1837                                IB_ACCESS_REMOTE_ATOMIC)))
1838                         goto nack_inv;
1839                 next = qp->r_head_ack_queue + 1;
1840                 if (next > IPATH_MAX_RDMA_ATOMIC)
1841                         next = 0;
1842                 if (unlikely(next == qp->s_tail_ack_queue)) {
1843                         if (!qp->s_ack_queue[next].sent)
1844                                 goto nack_inv;
1845                         ipath_update_ack_queue(qp, next);
1846                 }
1847                 if (!header_in_data)
1848                         ateth = &ohdr->u.atomic_eth;
1849                 else
1850                         ateth = (struct ib_atomic_eth *)data;
1851                 vaddr = ((u64) be32_to_cpu(ateth->vaddr[0]) << 32) |
1852                         be32_to_cpu(ateth->vaddr[1]);
1853                 if (unlikely(vaddr & (sizeof(u64) - 1)))
1854                         goto nack_inv;
1855                 rkey = be32_to_cpu(ateth->rkey);
1856                 /* Check rkey & NAK */
1857                 if (unlikely(!ipath_rkey_ok(qp, &qp->r_sge,
1858                                             sizeof(u64), vaddr, rkey,
1859                                             IB_ACCESS_REMOTE_ATOMIC)))
1860                         goto nack_acc;
1861                 /* Perform atomic OP and save result. */
1862                 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
1863                 sdata = be64_to_cpu(ateth->swap_data);
1864                 e = &qp->s_ack_queue[qp->r_head_ack_queue];
1865                 e->atomic_data = (opcode == OP(FETCH_ADD)) ?
1866                         (u64) atomic64_add_return(sdata, maddr) - sdata :
1867                         (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
1868                                       be64_to_cpu(ateth->compare_data),
1869                                       sdata);
1870                 e->opcode = opcode;
1871                 e->sent = 0;
1872                 e->psn = psn & IPATH_PSN_MASK;
1873                 qp->r_msn++;
1874                 qp->r_psn++;
1875                 qp->r_state = opcode;
1876                 qp->r_nak_state = 0;
1877                 barrier();
1878                 qp->r_head_ack_queue = next;
1879
1880                 /* Call ipath_do_rc_send() in another thread. */
1881                 tasklet_hi_schedule(&qp->s_task);
1882
1883                 goto done;
1884         }
1885
1886         default:
1887                 /* NAK unknown opcodes. */
1888                 goto nack_inv;
1889         }
1890         qp->r_psn++;
1891         qp->r_state = opcode;
1892         qp->r_ack_psn = psn;
1893         qp->r_nak_state = 0;
1894         /* Send an ACK if requested or required. */
1895         if (psn & (1 << 31))
1896                 goto send_ack;
1897         goto done;
1898
1899 rnr_nak:
1900         qp->r_nak_state = IB_RNR_NAK | qp->r_min_rnr_timer;
1901         qp->r_ack_psn = qp->r_psn;
1902         goto send_ack;
1903
1904 nack_inv:
1905         ipath_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
1906         qp->r_nak_state = IB_NAK_INVALID_REQUEST;
1907         qp->r_ack_psn = qp->r_psn;
1908         goto send_ack;
1909
1910 nack_acc:
1911         ipath_rc_error(qp, IB_WC_LOC_PROT_ERR);
1912         qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
1913         qp->r_ack_psn = qp->r_psn;
1914 send_ack:
1915         send_rc_ack(qp);
1916
1917 done:
1918         return;
1919 }