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