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