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