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