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