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