2 * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. 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
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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
38 /* cut down ridiculously long IB macro names */
39 #define OP(x) IB_OPCODE_RC_##x
41 static void rc_timeout(unsigned long arg);
43 static u32 restart_sge(struct qib_sge_state *ss, struct qib_swqe *wqe,
48 len = ((psn - wqe->psn) & QIB_PSN_MASK) * pmtu;
49 ss->sge = wqe->sg_list[0];
50 ss->sg_list = wqe->sg_list + 1;
51 ss->num_sge = wqe->wr.num_sge;
52 ss->total_len = wqe->length;
53 qib_skip_sge(ss, len, 0);
54 return wqe->length - len;
57 static void start_timer(struct qib_qp *qp)
59 qp->s_flags |= QIB_S_TIMER;
60 qp->s_timer.function = rc_timeout;
61 /* 4.096 usec. * (1 << qp->timeout) */
62 qp->s_timer.expires = jiffies +
63 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) / 1000UL);
64 add_timer(&qp->s_timer);
68 * qib_make_rc_ack - construct a response packet (ACK, NAK, or RDMA read)
69 * @dev: the device for this QP
70 * @qp: a pointer to the QP
71 * @ohdr: a pointer to the IB header being constructed
74 * Return 1 if constructed; otherwise, return 0.
75 * Note that we are in the responder's side of the QP context.
76 * Note the QP s_lock must be held.
78 static int qib_make_rc_ack(struct qib_ibdev *dev, struct qib_qp *qp,
79 struct qib_other_headers *ohdr, u32 pmtu)
81 struct qib_ack_entry *e;
87 /* Don't send an ACK if we aren't supposed to. */
88 if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK))
91 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
94 switch (qp->s_ack_state) {
95 case OP(RDMA_READ_RESPONSE_LAST):
96 case OP(RDMA_READ_RESPONSE_ONLY):
97 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
99 atomic_dec(&e->rdma_sge.mr->refcount);
100 e->rdma_sge.mr = NULL;
103 case OP(ATOMIC_ACKNOWLEDGE):
105 * We can increment the tail pointer now that the last
106 * response has been sent instead of only being
109 if (++qp->s_tail_ack_queue > QIB_MAX_RDMA_ATOMIC)
110 qp->s_tail_ack_queue = 0;
113 case OP(ACKNOWLEDGE):
114 /* Check for no next entry in the queue. */
115 if (qp->r_head_ack_queue == qp->s_tail_ack_queue) {
116 if (qp->s_flags & QIB_S_ACK_PENDING)
121 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
122 if (e->opcode == OP(RDMA_READ_REQUEST)) {
124 * If a RDMA read response is being resent and
125 * we haven't seen the duplicate request yet,
126 * then stop sending the remaining responses the
127 * responder has seen until the requester resends it.
129 len = e->rdma_sge.sge_length;
130 if (len && !e->rdma_sge.mr) {
131 qp->s_tail_ack_queue = qp->r_head_ack_queue;
134 /* Copy SGE state in case we need to resend */
135 qp->s_rdma_mr = e->rdma_sge.mr;
137 atomic_inc(&qp->s_rdma_mr->refcount);
138 qp->s_ack_rdma_sge.sge = e->rdma_sge;
139 qp->s_ack_rdma_sge.num_sge = 1;
140 qp->s_cur_sge = &qp->s_ack_rdma_sge;
143 qp->s_ack_state = OP(RDMA_READ_RESPONSE_FIRST);
145 qp->s_ack_state = OP(RDMA_READ_RESPONSE_ONLY);
148 ohdr->u.aeth = qib_compute_aeth(qp);
150 qp->s_ack_rdma_psn = e->psn;
151 bth2 = qp->s_ack_rdma_psn++ & QIB_PSN_MASK;
153 /* COMPARE_SWAP or FETCH_ADD */
154 qp->s_cur_sge = NULL;
156 qp->s_ack_state = OP(ATOMIC_ACKNOWLEDGE);
157 ohdr->u.at.aeth = qib_compute_aeth(qp);
158 ohdr->u.at.atomic_ack_eth[0] =
159 cpu_to_be32(e->atomic_data >> 32);
160 ohdr->u.at.atomic_ack_eth[1] =
161 cpu_to_be32(e->atomic_data);
162 hwords += sizeof(ohdr->u.at) / sizeof(u32);
163 bth2 = e->psn & QIB_PSN_MASK;
166 bth0 = qp->s_ack_state << 24;
169 case OP(RDMA_READ_RESPONSE_FIRST):
170 qp->s_ack_state = OP(RDMA_READ_RESPONSE_MIDDLE);
172 case OP(RDMA_READ_RESPONSE_MIDDLE):
173 qp->s_cur_sge = &qp->s_ack_rdma_sge;
174 qp->s_rdma_mr = qp->s_ack_rdma_sge.sge.mr;
176 atomic_inc(&qp->s_rdma_mr->refcount);
177 len = qp->s_ack_rdma_sge.sge.sge_length;
181 ohdr->u.aeth = qib_compute_aeth(qp);
183 qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
184 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
187 bth0 = qp->s_ack_state << 24;
188 bth2 = qp->s_ack_rdma_psn++ & QIB_PSN_MASK;
194 * Send a regular ACK.
195 * Set the s_ack_state so we wait until after sending
196 * the ACK before setting s_ack_state to ACKNOWLEDGE
199 qp->s_ack_state = OP(SEND_ONLY);
200 qp->s_flags &= ~QIB_S_ACK_PENDING;
201 qp->s_cur_sge = NULL;
204 cpu_to_be32((qp->r_msn & QIB_MSN_MASK) |
206 QIB_AETH_CREDIT_SHIFT));
208 ohdr->u.aeth = qib_compute_aeth(qp);
211 bth0 = OP(ACKNOWLEDGE) << 24;
212 bth2 = qp->s_ack_psn & QIB_PSN_MASK;
214 qp->s_rdma_ack_cnt++;
215 qp->s_hdrwords = hwords;
216 qp->s_cur_size = len;
217 qib_make_ruc_header(qp, ohdr, bth0, bth2);
221 qp->s_ack_state = OP(ACKNOWLEDGE);
222 qp->s_flags &= ~(QIB_S_RESP_PENDING | QIB_S_ACK_PENDING);
227 * qib_make_rc_req - construct a request packet (SEND, RDMA r/w, ATOMIC)
228 * @qp: a pointer to the QP
230 * Return 1 if constructed; otherwise, return 0.
232 int qib_make_rc_req(struct qib_qp *qp)
234 struct qib_ibdev *dev = to_idev(qp->ibqp.device);
235 struct qib_other_headers *ohdr;
236 struct qib_sge_state *ss;
237 struct qib_swqe *wqe;
242 u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
248 ohdr = &qp->s_hdr.u.oth;
249 if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
250 ohdr = &qp->s_hdr.u.l.oth;
253 * The lock is needed to synchronize between the sending tasklet,
254 * the receive interrupt handler, and timeout resends.
256 spin_lock_irqsave(&qp->s_lock, flags);
258 /* Sending responses has higher priority over sending requests. */
259 if ((qp->s_flags & QIB_S_RESP_PENDING) &&
260 qib_make_rc_ack(dev, qp, ohdr, pmtu))
263 if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_SEND_OK)) {
264 if (!(ib_qib_state_ops[qp->state] & QIB_FLUSH_SEND))
266 /* We are in the error state, flush the work request. */
267 if (qp->s_last == qp->s_head)
269 /* If DMAs are in progress, we can't flush immediately. */
270 if (atomic_read(&qp->s_dma_busy)) {
271 qp->s_flags |= QIB_S_WAIT_DMA;
274 wqe = get_swqe_ptr(qp, qp->s_last);
275 while (qp->s_last != qp->s_acked) {
276 qib_send_complete(qp, wqe, IB_WC_SUCCESS);
277 if (++qp->s_last >= qp->s_size)
279 wqe = get_swqe_ptr(qp, qp->s_last);
281 qib_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
285 if (qp->s_flags & (QIB_S_WAIT_RNR | QIB_S_WAIT_ACK))
288 if (qib_cmp24(qp->s_psn, qp->s_sending_hpsn) <= 0) {
289 if (qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) <= 0) {
290 qp->s_flags |= QIB_S_WAIT_PSN;
293 qp->s_sending_psn = qp->s_psn;
294 qp->s_sending_hpsn = qp->s_psn - 1;
297 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
301 /* Send a request. */
302 wqe = get_swqe_ptr(qp, qp->s_cur);
303 switch (qp->s_state) {
305 if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_NEXT_SEND_OK))
308 * Resend an old request or start a new one.
310 * We keep track of the current SWQE so that
311 * we don't reset the "furthest progress" state
312 * if we need to back up.
315 if (qp->s_cur == qp->s_tail) {
316 /* Check if send work queue is empty. */
317 if (qp->s_tail == qp->s_head)
320 * If a fence is requested, wait for previous
321 * RDMA read and atomic operations to finish.
323 if ((wqe->wr.send_flags & IB_SEND_FENCE) &&
324 qp->s_num_rd_atomic) {
325 qp->s_flags |= QIB_S_WAIT_FENCE;
328 wqe->psn = qp->s_next_psn;
332 * Note that we have to be careful not to modify the
333 * original work request since we may need to resend
338 bth2 = qp->s_psn & QIB_PSN_MASK;
339 switch (wqe->wr.opcode) {
341 case IB_WR_SEND_WITH_IMM:
342 /* If no credit, return. */
343 if (!(qp->s_flags & QIB_S_UNLIMITED_CREDIT) &&
344 qib_cmp24(wqe->ssn, qp->s_lsn + 1) > 0) {
345 qp->s_flags |= QIB_S_WAIT_SSN_CREDIT;
348 wqe->lpsn = wqe->psn;
350 wqe->lpsn += (len - 1) / pmtu;
351 qp->s_state = OP(SEND_FIRST);
355 if (wqe->wr.opcode == IB_WR_SEND)
356 qp->s_state = OP(SEND_ONLY);
358 qp->s_state = OP(SEND_ONLY_WITH_IMMEDIATE);
359 /* Immediate data comes after the BTH */
360 ohdr->u.imm_data = wqe->wr.ex.imm_data;
363 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
364 bth0 |= IB_BTH_SOLICITED;
365 bth2 |= IB_BTH_REQ_ACK;
366 if (++qp->s_cur == qp->s_size)
370 case IB_WR_RDMA_WRITE:
371 if (newreq && !(qp->s_flags & QIB_S_UNLIMITED_CREDIT))
374 case IB_WR_RDMA_WRITE_WITH_IMM:
375 /* If no credit, return. */
376 if (!(qp->s_flags & QIB_S_UNLIMITED_CREDIT) &&
377 qib_cmp24(wqe->ssn, qp->s_lsn + 1) > 0) {
378 qp->s_flags |= QIB_S_WAIT_SSN_CREDIT;
381 ohdr->u.rc.reth.vaddr =
382 cpu_to_be64(wqe->wr.wr.rdma.remote_addr);
383 ohdr->u.rc.reth.rkey =
384 cpu_to_be32(wqe->wr.wr.rdma.rkey);
385 ohdr->u.rc.reth.length = cpu_to_be32(len);
386 hwords += sizeof(struct ib_reth) / sizeof(u32);
387 wqe->lpsn = wqe->psn;
389 wqe->lpsn += (len - 1) / pmtu;
390 qp->s_state = OP(RDMA_WRITE_FIRST);
394 if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
395 qp->s_state = OP(RDMA_WRITE_ONLY);
398 OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
399 /* Immediate data comes after RETH */
400 ohdr->u.rc.imm_data = wqe->wr.ex.imm_data;
402 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
403 bth0 |= IB_BTH_SOLICITED;
405 bth2 |= IB_BTH_REQ_ACK;
406 if (++qp->s_cur == qp->s_size)
410 case IB_WR_RDMA_READ:
412 * Don't allow more operations to be started
413 * than the QP limits allow.
416 if (qp->s_num_rd_atomic >=
417 qp->s_max_rd_atomic) {
418 qp->s_flags |= QIB_S_WAIT_RDMAR;
421 qp->s_num_rd_atomic++;
422 if (!(qp->s_flags & QIB_S_UNLIMITED_CREDIT))
425 * Adjust s_next_psn to count the
426 * expected number of responses.
429 qp->s_next_psn += (len - 1) / pmtu;
430 wqe->lpsn = qp->s_next_psn++;
432 ohdr->u.rc.reth.vaddr =
433 cpu_to_be64(wqe->wr.wr.rdma.remote_addr);
434 ohdr->u.rc.reth.rkey =
435 cpu_to_be32(wqe->wr.wr.rdma.rkey);
436 ohdr->u.rc.reth.length = cpu_to_be32(len);
437 qp->s_state = OP(RDMA_READ_REQUEST);
438 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
441 bth2 |= IB_BTH_REQ_ACK;
442 if (++qp->s_cur == qp->s_size)
446 case IB_WR_ATOMIC_CMP_AND_SWP:
447 case IB_WR_ATOMIC_FETCH_AND_ADD:
449 * Don't allow more operations to be started
450 * than the QP limits allow.
453 if (qp->s_num_rd_atomic >=
454 qp->s_max_rd_atomic) {
455 qp->s_flags |= QIB_S_WAIT_RDMAR;
458 qp->s_num_rd_atomic++;
459 if (!(qp->s_flags & QIB_S_UNLIMITED_CREDIT))
461 wqe->lpsn = wqe->psn;
463 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
464 qp->s_state = OP(COMPARE_SWAP);
465 ohdr->u.atomic_eth.swap_data = cpu_to_be64(
466 wqe->wr.wr.atomic.swap);
467 ohdr->u.atomic_eth.compare_data = cpu_to_be64(
468 wqe->wr.wr.atomic.compare_add);
470 qp->s_state = OP(FETCH_ADD);
471 ohdr->u.atomic_eth.swap_data = cpu_to_be64(
472 wqe->wr.wr.atomic.compare_add);
473 ohdr->u.atomic_eth.compare_data = 0;
475 ohdr->u.atomic_eth.vaddr[0] = cpu_to_be32(
476 wqe->wr.wr.atomic.remote_addr >> 32);
477 ohdr->u.atomic_eth.vaddr[1] = cpu_to_be32(
478 wqe->wr.wr.atomic.remote_addr);
479 ohdr->u.atomic_eth.rkey = cpu_to_be32(
480 wqe->wr.wr.atomic.rkey);
481 hwords += sizeof(struct ib_atomic_eth) / sizeof(u32);
484 bth2 |= IB_BTH_REQ_ACK;
485 if (++qp->s_cur == qp->s_size)
492 qp->s_sge.sge = wqe->sg_list[0];
493 qp->s_sge.sg_list = wqe->sg_list + 1;
494 qp->s_sge.num_sge = wqe->wr.num_sge;
495 qp->s_sge.total_len = wqe->length;
496 qp->s_len = wqe->length;
499 if (qp->s_tail >= qp->s_size)
502 if (wqe->wr.opcode == IB_WR_RDMA_READ)
503 qp->s_psn = wqe->lpsn + 1;
506 if (qib_cmp24(qp->s_psn, qp->s_next_psn) > 0)
507 qp->s_next_psn = qp->s_psn;
511 case OP(RDMA_READ_RESPONSE_FIRST):
513 * qp->s_state is normally set to the opcode of the
514 * last packet constructed for new requests and therefore
515 * is never set to RDMA read response.
516 * RDMA_READ_RESPONSE_FIRST is used by the ACK processing
517 * thread to indicate a SEND needs to be restarted from an
518 * earlier PSN without interferring with the sending thread.
519 * See qib_restart_rc().
521 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
524 qp->s_state = OP(SEND_MIDDLE);
526 case OP(SEND_MIDDLE):
527 bth2 = qp->s_psn++ & QIB_PSN_MASK;
528 if (qib_cmp24(qp->s_psn, qp->s_next_psn) > 0)
529 qp->s_next_psn = qp->s_psn;
536 if (wqe->wr.opcode == IB_WR_SEND)
537 qp->s_state = OP(SEND_LAST);
539 qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
540 /* Immediate data comes after the BTH */
541 ohdr->u.imm_data = wqe->wr.ex.imm_data;
544 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
545 bth0 |= IB_BTH_SOLICITED;
546 bth2 |= IB_BTH_REQ_ACK;
548 if (qp->s_cur >= qp->s_size)
552 case OP(RDMA_READ_RESPONSE_LAST):
554 * qp->s_state is normally set to the opcode of the
555 * last packet constructed for new requests and therefore
556 * is never set to RDMA read response.
557 * RDMA_READ_RESPONSE_LAST is used by the ACK processing
558 * thread to indicate a RDMA write needs to be restarted from
559 * an earlier PSN without interferring with the sending thread.
560 * See qib_restart_rc().
562 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
564 case OP(RDMA_WRITE_FIRST):
565 qp->s_state = OP(RDMA_WRITE_MIDDLE);
567 case OP(RDMA_WRITE_MIDDLE):
568 bth2 = qp->s_psn++ & QIB_PSN_MASK;
569 if (qib_cmp24(qp->s_psn, qp->s_next_psn) > 0)
570 qp->s_next_psn = qp->s_psn;
577 if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
578 qp->s_state = OP(RDMA_WRITE_LAST);
580 qp->s_state = OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
581 /* Immediate data comes after the BTH */
582 ohdr->u.imm_data = wqe->wr.ex.imm_data;
584 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
585 bth0 |= IB_BTH_SOLICITED;
587 bth2 |= IB_BTH_REQ_ACK;
589 if (qp->s_cur >= qp->s_size)
593 case OP(RDMA_READ_RESPONSE_MIDDLE):
595 * qp->s_state is normally set to the opcode of the
596 * last packet constructed for new requests and therefore
597 * is never set to RDMA read response.
598 * RDMA_READ_RESPONSE_MIDDLE is used by the ACK processing
599 * thread to indicate a RDMA read needs to be restarted from
600 * an earlier PSN without interferring with the sending thread.
601 * See qib_restart_rc().
603 len = ((qp->s_psn - wqe->psn) & QIB_PSN_MASK) * pmtu;
604 ohdr->u.rc.reth.vaddr =
605 cpu_to_be64(wqe->wr.wr.rdma.remote_addr + len);
606 ohdr->u.rc.reth.rkey =
607 cpu_to_be32(wqe->wr.wr.rdma.rkey);
608 ohdr->u.rc.reth.length = cpu_to_be32(wqe->length - len);
609 qp->s_state = OP(RDMA_READ_REQUEST);
610 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
611 bth2 = (qp->s_psn & QIB_PSN_MASK) | IB_BTH_REQ_ACK;
612 qp->s_psn = wqe->lpsn + 1;
616 if (qp->s_cur == qp->s_size)
620 qp->s_sending_hpsn = bth2;
621 delta = (((int) bth2 - (int) wqe->psn) << 8) >> 8;
622 if (delta && delta % QIB_PSN_CREDIT == 0)
623 bth2 |= IB_BTH_REQ_ACK;
624 if (qp->s_flags & QIB_S_SEND_ONE) {
625 qp->s_flags &= ~QIB_S_SEND_ONE;
626 qp->s_flags |= QIB_S_WAIT_ACK;
627 bth2 |= IB_BTH_REQ_ACK;
630 qp->s_hdrwords = hwords;
632 qp->s_cur_size = len;
633 qib_make_ruc_header(qp, ohdr, bth0 | (qp->s_state << 24), bth2);
639 qp->s_flags &= ~QIB_S_BUSY;
641 spin_unlock_irqrestore(&qp->s_lock, flags);
646 * qib_send_rc_ack - Construct an ACK packet and send it
647 * @qp: a pointer to the QP
649 * This is called from qib_rc_rcv() and qib_kreceive().
650 * Note that RDMA reads and atomics are handled in the
651 * send side QP state and tasklet.
653 void qib_send_rc_ack(struct qib_qp *qp)
655 struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
656 struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
657 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
664 struct qib_ib_header hdr;
665 struct qib_other_headers *ohdr;
669 spin_lock_irqsave(&qp->s_lock, flags);
671 if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK))
674 /* Don't send ACK or NAK if a RDMA read or atomic is pending. */
675 if ((qp->s_flags & QIB_S_RESP_PENDING) || qp->s_rdma_ack_cnt)
678 /* Construct the header with s_lock held so APM doesn't change it. */
681 /* header size in 32-bit words LRH+BTH+AETH = (8+12+4)/4. */
683 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
684 hwords += qib_make_grh(ibp, &hdr.u.l.grh,
685 &qp->remote_ah_attr.grh, hwords, 0);
689 /* read pkey_index w/o lock (its atomic) */
690 bth0 = qib_get_pkey(ibp, qp->s_pkey_index) | (OP(ACKNOWLEDGE) << 24);
691 if (qp->s_mig_state == IB_MIG_MIGRATED)
692 bth0 |= IB_BTH_MIG_REQ;
694 ohdr->u.aeth = cpu_to_be32((qp->r_msn & QIB_MSN_MASK) |
696 QIB_AETH_CREDIT_SHIFT));
698 ohdr->u.aeth = qib_compute_aeth(qp);
699 lrh0 |= ibp->sl_to_vl[qp->remote_ah_attr.sl] << 12 |
700 qp->remote_ah_attr.sl << 4;
701 hdr.lrh[0] = cpu_to_be16(lrh0);
702 hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
703 hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
704 hdr.lrh[3] = cpu_to_be16(ppd->lid | qp->remote_ah_attr.src_path_bits);
705 ohdr->bth[0] = cpu_to_be32(bth0);
706 ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
707 ohdr->bth[2] = cpu_to_be32(qp->r_ack_psn & QIB_PSN_MASK);
709 spin_unlock_irqrestore(&qp->s_lock, flags);
711 /* Don't try to send ACKs if the link isn't ACTIVE */
712 if (!(ppd->lflags & QIBL_LINKACTIVE))
715 control = dd->f_setpbc_control(ppd, hwords + SIZE_OF_CRC,
716 qp->s_srate, lrh0 >> 12);
717 /* length is + 1 for the control dword */
718 pbc = ((u64) control << 32) | (hwords + 1);
720 piobuf = dd->f_getsendbuf(ppd, pbc, &pbufn);
723 * We are out of PIO buffers at the moment.
724 * Pass responsibility for sending the ACK to the
725 * send tasklet so that when a PIO buffer becomes
726 * available, the ACK is sent ahead of other outgoing
729 spin_lock_irqsave(&qp->s_lock, flags);
735 * We have to flush after the PBC for correctness
736 * on some cpus or WC buffer can be written out of order.
740 if (dd->flags & QIB_PIO_FLUSH_WC) {
741 u32 *hdrp = (u32 *) &hdr;
744 qib_pio_copy(piobuf + 2, hdrp, hwords - 1);
746 __raw_writel(hdrp[hwords - 1], piobuf + hwords + 1);
748 qib_pio_copy(piobuf + 2, (u32 *) &hdr, hwords);
750 if (dd->flags & QIB_USE_SPCL_TRIG) {
751 u32 spcl_off = (pbufn >= dd->piobcnt2k) ? 2047 : 1023;
754 __raw_writel(0xaebecede, piobuf + spcl_off);
758 qib_sendbuf_done(dd, pbufn);
760 ibp->n_unicast_xmit++;
764 if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK) {
766 qp->s_flags |= QIB_S_ACK_PENDING | QIB_S_RESP_PENDING;
767 qp->s_nak_state = qp->r_nak_state;
768 qp->s_ack_psn = qp->r_ack_psn;
770 /* Schedule the send tasklet. */
771 qib_schedule_send(qp);
774 spin_unlock_irqrestore(&qp->s_lock, flags);
780 * reset_psn - reset the QP state to send starting from PSN
782 * @psn: the packet sequence number to restart at
784 * This is called from qib_rc_rcv() to process an incoming RC ACK
786 * Called at interrupt level with the QP s_lock held.
788 static void reset_psn(struct qib_qp *qp, u32 psn)
791 struct qib_swqe *wqe = get_swqe_ptr(qp, n);
797 * If we are starting the request from the beginning,
798 * let the normal send code handle initialization.
800 if (qib_cmp24(psn, wqe->psn) <= 0) {
801 qp->s_state = OP(SEND_LAST);
805 /* Find the work request opcode corresponding to the given PSN. */
806 opcode = wqe->wr.opcode;
810 if (++n == qp->s_size)
814 wqe = get_swqe_ptr(qp, n);
815 diff = qib_cmp24(psn, wqe->psn);
820 * If we are starting the request from the beginning,
821 * let the normal send code handle initialization.
824 qp->s_state = OP(SEND_LAST);
827 opcode = wqe->wr.opcode;
831 * Set the state to restart in the middle of a request.
832 * Don't change the s_sge, s_cur_sge, or s_cur_size.
833 * See qib_make_rc_req().
837 case IB_WR_SEND_WITH_IMM:
838 qp->s_state = OP(RDMA_READ_RESPONSE_FIRST);
841 case IB_WR_RDMA_WRITE:
842 case IB_WR_RDMA_WRITE_WITH_IMM:
843 qp->s_state = OP(RDMA_READ_RESPONSE_LAST);
846 case IB_WR_RDMA_READ:
847 qp->s_state = OP(RDMA_READ_RESPONSE_MIDDLE);
852 * This case shouldn't happen since its only
855 qp->s_state = OP(SEND_LAST);
860 * Set QIB_S_WAIT_PSN as qib_rc_complete() may start the timer
861 * asynchronously before the send tasklet can get scheduled.
862 * Doing it in qib_make_rc_req() is too late.
864 if ((qib_cmp24(qp->s_psn, qp->s_sending_hpsn) <= 0) &&
865 (qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) <= 0))
866 qp->s_flags |= QIB_S_WAIT_PSN;
870 * Back up requester to resend the last un-ACKed request.
871 * The QP r_lock and s_lock should be held and interrupts disabled.
873 static void qib_restart_rc(struct qib_qp *qp, u32 psn, int wait)
875 struct qib_swqe *wqe = get_swqe_ptr(qp, qp->s_acked);
876 struct qib_ibport *ibp;
878 if (qp->s_retry == 0) {
879 if (qp->s_mig_state == IB_MIG_ARMED) {
881 qp->s_retry = qp->s_retry_cnt;
882 } else if (qp->s_last == qp->s_acked) {
883 qib_send_complete(qp, wqe, IB_WC_RETRY_EXC_ERR);
884 qib_error_qp(qp, IB_WC_WR_FLUSH_ERR);
886 } else /* XXX need to handle delayed completion */
891 ibp = to_iport(qp->ibqp.device, qp->port_num);
892 if (wqe->wr.opcode == IB_WR_RDMA_READ)
895 ibp->n_rc_resends += (qp->s_psn - psn) & QIB_PSN_MASK;
897 qp->s_flags &= ~(QIB_S_WAIT_FENCE | QIB_S_WAIT_RDMAR |
898 QIB_S_WAIT_SSN_CREDIT | QIB_S_WAIT_PSN |
901 qp->s_flags |= QIB_S_SEND_ONE;
906 * This is called from s_timer for missing responses.
908 static void rc_timeout(unsigned long arg)
910 struct qib_qp *qp = (struct qib_qp *)arg;
911 struct qib_ibport *ibp;
914 spin_lock_irqsave(&qp->r_lock, flags);
915 spin_lock(&qp->s_lock);
916 if (qp->s_flags & QIB_S_TIMER) {
917 ibp = to_iport(qp->ibqp.device, qp->port_num);
918 ibp->n_rc_timeouts++;
919 qp->s_flags &= ~QIB_S_TIMER;
920 del_timer(&qp->s_timer);
921 qib_restart_rc(qp, qp->s_last_psn + 1, 1);
922 qib_schedule_send(qp);
924 spin_unlock(&qp->s_lock);
925 spin_unlock_irqrestore(&qp->r_lock, flags);
929 * This is called from s_timer for RNR timeouts.
931 void qib_rc_rnr_retry(unsigned long arg)
933 struct qib_qp *qp = (struct qib_qp *)arg;
936 spin_lock_irqsave(&qp->s_lock, flags);
937 if (qp->s_flags & QIB_S_WAIT_RNR) {
938 qp->s_flags &= ~QIB_S_WAIT_RNR;
939 del_timer(&qp->s_timer);
940 qib_schedule_send(qp);
942 spin_unlock_irqrestore(&qp->s_lock, flags);
946 * Set qp->s_sending_psn to the next PSN after the given one.
947 * This would be psn+1 except when RDMA reads are present.
949 static void reset_sending_psn(struct qib_qp *qp, u32 psn)
951 struct qib_swqe *wqe;
954 /* Find the work request corresponding to the given PSN. */
956 wqe = get_swqe_ptr(qp, n);
957 if (qib_cmp24(psn, wqe->lpsn) <= 0) {
958 if (wqe->wr.opcode == IB_WR_RDMA_READ)
959 qp->s_sending_psn = wqe->lpsn + 1;
961 qp->s_sending_psn = psn + 1;
964 if (++n == qp->s_size)
972 * This should be called with the QP s_lock held and interrupts disabled.
974 void qib_rc_send_complete(struct qib_qp *qp, struct qib_ib_header *hdr)
976 struct qib_other_headers *ohdr;
977 struct qib_swqe *wqe;
983 if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_OR_FLUSH_SEND))
986 /* Find out where the BTH is */
987 if ((be16_to_cpu(hdr->lrh[0]) & 3) == QIB_LRH_BTH)
990 ohdr = &hdr->u.l.oth;
992 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
993 if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
994 opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
995 WARN_ON(!qp->s_rdma_ack_cnt);
996 qp->s_rdma_ack_cnt--;
1000 psn = be32_to_cpu(ohdr->bth[2]);
1001 reset_sending_psn(qp, psn);
1004 * Start timer after a packet requesting an ACK has been sent and
1005 * there are still requests that haven't been acked.
1007 if ((psn & IB_BTH_REQ_ACK) && qp->s_acked != qp->s_tail &&
1008 !(qp->s_flags & (QIB_S_TIMER | QIB_S_WAIT_RNR | QIB_S_WAIT_PSN)) &&
1009 (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK))
1012 while (qp->s_last != qp->s_acked) {
1013 wqe = get_swqe_ptr(qp, qp->s_last);
1014 if (qib_cmp24(wqe->lpsn, qp->s_sending_psn) >= 0 &&
1015 qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) <= 0)
1017 for (i = 0; i < wqe->wr.num_sge; i++) {
1018 struct qib_sge *sge = &wqe->sg_list[i];
1020 atomic_dec(&sge->mr->refcount);
1022 /* Post a send completion queue entry if requested. */
1023 if (!(qp->s_flags & QIB_S_SIGNAL_REQ_WR) ||
1024 (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
1025 memset(&wc, 0, sizeof wc);
1026 wc.wr_id = wqe->wr.wr_id;
1027 wc.status = IB_WC_SUCCESS;
1028 wc.opcode = ib_qib_wc_opcode[wqe->wr.opcode];
1029 wc.byte_len = wqe->length;
1031 qib_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 0);
1033 if (++qp->s_last >= qp->s_size)
1037 * If we were waiting for sends to complete before resending,
1038 * and they are now complete, restart sending.
1040 if (qp->s_flags & QIB_S_WAIT_PSN &&
1041 qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
1042 qp->s_flags &= ~QIB_S_WAIT_PSN;
1043 qp->s_sending_psn = qp->s_psn;
1044 qp->s_sending_hpsn = qp->s_psn - 1;
1045 qib_schedule_send(qp);
1049 static inline void update_last_psn(struct qib_qp *qp, u32 psn)
1051 qp->s_last_psn = psn;
1055 * Generate a SWQE completion.
1056 * This is similar to qib_send_complete but has to check to be sure
1057 * that the SGEs are not being referenced if the SWQE is being resent.
1059 static struct qib_swqe *do_rc_completion(struct qib_qp *qp,
1060 struct qib_swqe *wqe,
1061 struct qib_ibport *ibp)
1067 * Don't decrement refcount and don't generate a
1068 * completion if the SWQE is being resent until the send
1071 if (qib_cmp24(wqe->lpsn, qp->s_sending_psn) < 0 ||
1072 qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
1073 for (i = 0; i < wqe->wr.num_sge; i++) {
1074 struct qib_sge *sge = &wqe->sg_list[i];
1076 atomic_dec(&sge->mr->refcount);
1078 /* Post a send completion queue entry if requested. */
1079 if (!(qp->s_flags & QIB_S_SIGNAL_REQ_WR) ||
1080 (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
1081 memset(&wc, 0, sizeof wc);
1082 wc.wr_id = wqe->wr.wr_id;
1083 wc.status = IB_WC_SUCCESS;
1084 wc.opcode = ib_qib_wc_opcode[wqe->wr.opcode];
1085 wc.byte_len = wqe->length;
1087 qib_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 0);
1089 if (++qp->s_last >= qp->s_size)
1092 ibp->n_rc_delayed_comp++;
1094 qp->s_retry = qp->s_retry_cnt;
1095 update_last_psn(qp, wqe->lpsn);
1098 * If we are completing a request which is in the process of
1099 * being resent, we can stop resending it since we know the
1100 * responder has already seen it.
1102 if (qp->s_acked == qp->s_cur) {
1103 if (++qp->s_cur >= qp->s_size)
1105 qp->s_acked = qp->s_cur;
1106 wqe = get_swqe_ptr(qp, qp->s_cur);
1107 if (qp->s_acked != qp->s_tail) {
1108 qp->s_state = OP(SEND_LAST);
1109 qp->s_psn = wqe->psn;
1112 if (++qp->s_acked >= qp->s_size)
1114 if (qp->state == IB_QPS_SQD && qp->s_acked == qp->s_cur)
1116 wqe = get_swqe_ptr(qp, qp->s_acked);
1122 * do_rc_ack - process an incoming RC ACK
1123 * @qp: the QP the ACK came in on
1124 * @psn: the packet sequence number of the ACK
1125 * @opcode: the opcode of the request that resulted in the ACK
1127 * This is called from qib_rc_rcv_resp() to process an incoming RC ACK
1129 * Called at interrupt level with the QP s_lock held.
1130 * Returns 1 if OK, 0 if current operation should be aborted (NAK).
1132 static int do_rc_ack(struct qib_qp *qp, u32 aeth, u32 psn, int opcode,
1133 u64 val, struct qib_ctxtdata *rcd)
1135 struct qib_ibport *ibp;
1136 enum ib_wc_status status;
1137 struct qib_swqe *wqe;
1142 /* Remove QP from retry timer */
1143 if (qp->s_flags & (QIB_S_TIMER | QIB_S_WAIT_RNR)) {
1144 qp->s_flags &= ~(QIB_S_TIMER | QIB_S_WAIT_RNR);
1145 del_timer(&qp->s_timer);
1149 * Note that NAKs implicitly ACK outstanding SEND and RDMA write
1150 * requests and implicitly NAK RDMA read and atomic requests issued
1151 * before the NAK'ed request. The MSN won't include the NAK'ed
1152 * request but will include an ACK'ed request(s).
1157 wqe = get_swqe_ptr(qp, qp->s_acked);
1158 ibp = to_iport(qp->ibqp.device, qp->port_num);
1161 * The MSN might be for a later WQE than the PSN indicates so
1162 * only complete WQEs that the PSN finishes.
1164 while ((diff = qib_cmp24(ack_psn, wqe->lpsn)) >= 0) {
1166 * RDMA_READ_RESPONSE_ONLY is a special case since
1167 * we want to generate completion events for everything
1168 * before the RDMA read, copy the data, then generate
1169 * the completion for the read.
1171 if (wqe->wr.opcode == IB_WR_RDMA_READ &&
1172 opcode == OP(RDMA_READ_RESPONSE_ONLY) &&
1178 * If this request is a RDMA read or atomic, and the ACK is
1179 * for a later operation, this ACK NAKs the RDMA read or
1180 * atomic. In other words, only a RDMA_READ_LAST or ONLY
1181 * can ACK a RDMA read and likewise for atomic ops. Note
1182 * that the NAK case can only happen if relaxed ordering is
1183 * used and requests are sent after an RDMA read or atomic
1184 * is sent but before the response is received.
1186 if ((wqe->wr.opcode == IB_WR_RDMA_READ &&
1187 (opcode != OP(RDMA_READ_RESPONSE_LAST) || diff != 0)) ||
1188 ((wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1189 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) &&
1190 (opcode != OP(ATOMIC_ACKNOWLEDGE) || diff != 0))) {
1191 /* Retry this request. */
1192 if (!(qp->r_flags & QIB_R_RDMAR_SEQ)) {
1193 qp->r_flags |= QIB_R_RDMAR_SEQ;
1194 qib_restart_rc(qp, qp->s_last_psn + 1, 0);
1195 if (list_empty(&qp->rspwait)) {
1196 qp->r_flags |= QIB_R_RSP_SEND;
1197 atomic_inc(&qp->refcount);
1198 list_add_tail(&qp->rspwait,
1199 &rcd->qp_wait_list);
1203 * No need to process the ACK/NAK since we are
1204 * restarting an earlier request.
1208 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1209 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
1210 u64 *vaddr = wqe->sg_list[0].vaddr;
1213 if (qp->s_num_rd_atomic &&
1214 (wqe->wr.opcode == IB_WR_RDMA_READ ||
1215 wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1216 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)) {
1217 qp->s_num_rd_atomic--;
1218 /* Restart sending task if fence is complete */
1219 if ((qp->s_flags & QIB_S_WAIT_FENCE) &&
1220 !qp->s_num_rd_atomic) {
1221 qp->s_flags &= ~(QIB_S_WAIT_FENCE |
1223 qib_schedule_send(qp);
1224 } else if (qp->s_flags & QIB_S_WAIT_RDMAR) {
1225 qp->s_flags &= ~(QIB_S_WAIT_RDMAR |
1227 qib_schedule_send(qp);
1230 wqe = do_rc_completion(qp, wqe, ibp);
1231 if (qp->s_acked == qp->s_tail)
1235 switch (aeth >> 29) {
1238 if (qp->s_acked != qp->s_tail) {
1240 * We are expecting more ACKs so
1241 * reset the retransmit timer.
1245 * We can stop resending the earlier packets and
1246 * continue with the next packet the receiver wants.
1248 if (qib_cmp24(qp->s_psn, psn) <= 0)
1249 reset_psn(qp, psn + 1);
1250 } else if (qib_cmp24(qp->s_psn, psn) <= 0) {
1251 qp->s_state = OP(SEND_LAST);
1252 qp->s_psn = psn + 1;
1254 if (qp->s_flags & QIB_S_WAIT_ACK) {
1255 qp->s_flags &= ~QIB_S_WAIT_ACK;
1256 qib_schedule_send(qp);
1258 qib_get_credit(qp, aeth);
1259 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1260 qp->s_retry = qp->s_retry_cnt;
1261 update_last_psn(qp, psn);
1265 case 1: /* RNR NAK */
1267 if (qp->s_acked == qp->s_tail)
1269 if (qp->s_flags & QIB_S_WAIT_RNR)
1271 if (qp->s_rnr_retry == 0) {
1272 status = IB_WC_RNR_RETRY_EXC_ERR;
1275 if (qp->s_rnr_retry_cnt < 7)
1278 /* The last valid PSN is the previous PSN. */
1279 update_last_psn(qp, psn - 1);
1281 ibp->n_rc_resends += (qp->s_psn - psn) & QIB_PSN_MASK;
1285 qp->s_flags &= ~(QIB_S_WAIT_SSN_CREDIT | QIB_S_WAIT_ACK);
1286 qp->s_flags |= QIB_S_WAIT_RNR;
1287 qp->s_timer.function = qib_rc_rnr_retry;
1288 qp->s_timer.expires = jiffies + usecs_to_jiffies(
1289 ib_qib_rnr_table[(aeth >> QIB_AETH_CREDIT_SHIFT) &
1290 QIB_AETH_CREDIT_MASK]);
1291 add_timer(&qp->s_timer);
1295 if (qp->s_acked == qp->s_tail)
1297 /* The last valid PSN is the previous PSN. */
1298 update_last_psn(qp, psn - 1);
1299 switch ((aeth >> QIB_AETH_CREDIT_SHIFT) &
1300 QIB_AETH_CREDIT_MASK) {
1301 case 0: /* PSN sequence error */
1304 * Back up to the responder's expected PSN.
1305 * Note that we might get a NAK in the middle of an
1306 * RDMA READ response which terminates the RDMA
1309 qib_restart_rc(qp, psn, 0);
1310 qib_schedule_send(qp);
1313 case 1: /* Invalid Request */
1314 status = IB_WC_REM_INV_REQ_ERR;
1315 ibp->n_other_naks++;
1318 case 2: /* Remote Access Error */
1319 status = IB_WC_REM_ACCESS_ERR;
1320 ibp->n_other_naks++;
1323 case 3: /* Remote Operation Error */
1324 status = IB_WC_REM_OP_ERR;
1325 ibp->n_other_naks++;
1327 if (qp->s_last == qp->s_acked) {
1328 qib_send_complete(qp, wqe, status);
1329 qib_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1334 /* Ignore other reserved NAK error codes */
1337 qp->s_retry = qp->s_retry_cnt;
1338 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1341 default: /* 2: reserved */
1343 /* Ignore reserved NAK codes. */
1352 * We have seen an out of sequence RDMA read middle or last packet.
1353 * This ACKs SENDs and RDMA writes up to the first RDMA read or atomic SWQE.
1355 static void rdma_seq_err(struct qib_qp *qp, struct qib_ibport *ibp, u32 psn,
1356 struct qib_ctxtdata *rcd)
1358 struct qib_swqe *wqe;
1360 /* Remove QP from retry timer */
1361 if (qp->s_flags & (QIB_S_TIMER | QIB_S_WAIT_RNR)) {
1362 qp->s_flags &= ~(QIB_S_TIMER | QIB_S_WAIT_RNR);
1363 del_timer(&qp->s_timer);
1366 wqe = get_swqe_ptr(qp, qp->s_acked);
1368 while (qib_cmp24(psn, wqe->lpsn) > 0) {
1369 if (wqe->wr.opcode == IB_WR_RDMA_READ ||
1370 wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
1371 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
1373 wqe = do_rc_completion(qp, wqe, ibp);
1377 qp->r_flags |= QIB_R_RDMAR_SEQ;
1378 qib_restart_rc(qp, qp->s_last_psn + 1, 0);
1379 if (list_empty(&qp->rspwait)) {
1380 qp->r_flags |= QIB_R_RSP_SEND;
1381 atomic_inc(&qp->refcount);
1382 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1387 * qib_rc_rcv_resp - process an incoming RC response packet
1388 * @ibp: the port this packet came in on
1389 * @ohdr: the other headers for this packet
1390 * @data: the packet data
1391 * @tlen: the packet length
1392 * @qp: the QP for this packet
1393 * @opcode: the opcode for this packet
1394 * @psn: the packet sequence number for this packet
1395 * @hdrsize: the header length
1396 * @pmtu: the path MTU
1398 * This is called from qib_rc_rcv() to process an incoming RC response
1399 * packet for the given QP.
1400 * Called at interrupt level.
1402 static void qib_rc_rcv_resp(struct qib_ibport *ibp,
1403 struct qib_other_headers *ohdr,
1404 void *data, u32 tlen,
1407 u32 psn, u32 hdrsize, u32 pmtu,
1408 struct qib_ctxtdata *rcd)
1410 struct qib_swqe *wqe;
1411 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1412 enum ib_wc_status status;
1413 unsigned long flags;
1419 if (opcode != OP(RDMA_READ_RESPONSE_MIDDLE)) {
1421 * If ACK'd PSN on SDMA busy list try to make progress to
1422 * reclaim SDMA credits.
1424 if ((qib_cmp24(psn, qp->s_sending_psn) >= 0) &&
1425 (qib_cmp24(qp->s_sending_psn, qp->s_sending_hpsn) <= 0)) {
1428 * If send tasklet not running attempt to progress
1431 if (!(qp->s_flags & QIB_S_BUSY)) {
1432 /* Acquire SDMA Lock */
1433 spin_lock_irqsave(&ppd->sdma_lock, flags);
1434 /* Invoke sdma make progress */
1435 qib_sdma_make_progress(ppd);
1436 /* Release SDMA Lock */
1437 spin_unlock_irqrestore(&ppd->sdma_lock, flags);
1442 spin_lock_irqsave(&qp->s_lock, flags);
1443 if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK))
1446 /* Ignore invalid responses. */
1447 if (qib_cmp24(psn, qp->s_next_psn) >= 0)
1450 /* Ignore duplicate responses. */
1451 diff = qib_cmp24(psn, qp->s_last_psn);
1452 if (unlikely(diff <= 0)) {
1453 /* Update credits for "ghost" ACKs */
1454 if (diff == 0 && opcode == OP(ACKNOWLEDGE)) {
1455 aeth = be32_to_cpu(ohdr->u.aeth);
1456 if ((aeth >> 29) == 0)
1457 qib_get_credit(qp, aeth);
1463 * Skip everything other than the PSN we expect, if we are waiting
1464 * for a reply to a restarted RDMA read or atomic op.
1466 if (qp->r_flags & QIB_R_RDMAR_SEQ) {
1467 if (qib_cmp24(psn, qp->s_last_psn + 1) != 0)
1469 qp->r_flags &= ~QIB_R_RDMAR_SEQ;
1472 if (unlikely(qp->s_acked == qp->s_tail))
1474 wqe = get_swqe_ptr(qp, qp->s_acked);
1475 status = IB_WC_SUCCESS;
1478 case OP(ACKNOWLEDGE):
1479 case OP(ATOMIC_ACKNOWLEDGE):
1480 case OP(RDMA_READ_RESPONSE_FIRST):
1481 aeth = be32_to_cpu(ohdr->u.aeth);
1482 if (opcode == OP(ATOMIC_ACKNOWLEDGE)) {
1483 __be32 *p = ohdr->u.at.atomic_ack_eth;
1485 val = ((u64) be32_to_cpu(p[0]) << 32) |
1489 if (!do_rc_ack(qp, aeth, psn, opcode, val, rcd) ||
1490 opcode != OP(RDMA_READ_RESPONSE_FIRST))
1493 wqe = get_swqe_ptr(qp, qp->s_acked);
1494 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1497 * If this is a response to a resent RDMA read, we
1498 * have to be careful to copy the data to the right
1501 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1505 case OP(RDMA_READ_RESPONSE_MIDDLE):
1506 /* no AETH, no ACK */
1507 if (unlikely(qib_cmp24(psn, qp->s_last_psn + 1)))
1509 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1512 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1514 if (unlikely(pmtu >= qp->s_rdma_read_len))
1518 * We got a response so update the timeout.
1519 * 4.096 usec. * (1 << qp->timeout)
1521 qp->s_flags |= QIB_S_TIMER;
1522 mod_timer(&qp->s_timer, jiffies +
1523 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
1525 if (qp->s_flags & QIB_S_WAIT_ACK) {
1526 qp->s_flags &= ~QIB_S_WAIT_ACK;
1527 qib_schedule_send(qp);
1530 if (opcode == OP(RDMA_READ_RESPONSE_MIDDLE))
1531 qp->s_retry = qp->s_retry_cnt;
1534 * Update the RDMA receive state but do the copy w/o
1535 * holding the locks and blocking interrupts.
1537 qp->s_rdma_read_len -= pmtu;
1538 update_last_psn(qp, psn);
1539 spin_unlock_irqrestore(&qp->s_lock, flags);
1540 qib_copy_sge(&qp->s_rdma_read_sge, data, pmtu, 0);
1543 case OP(RDMA_READ_RESPONSE_ONLY):
1544 aeth = be32_to_cpu(ohdr->u.aeth);
1545 if (!do_rc_ack(qp, aeth, psn, opcode, 0, rcd))
1547 /* Get the number of bytes the message was padded by. */
1548 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1550 * Check that the data size is >= 0 && <= pmtu.
1551 * Remember to account for the AETH header (4) and
1554 if (unlikely(tlen < (hdrsize + pad + 8)))
1557 * If this is a response to a resent RDMA read, we
1558 * have to be careful to copy the data to the right
1561 wqe = get_swqe_ptr(qp, qp->s_acked);
1562 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1566 case OP(RDMA_READ_RESPONSE_LAST):
1567 /* ACKs READ req. */
1568 if (unlikely(qib_cmp24(psn, qp->s_last_psn + 1)))
1570 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1572 /* Get the number of bytes the message was padded by. */
1573 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1575 * Check that the data size is >= 1 && <= pmtu.
1576 * Remember to account for the AETH header (4) and
1579 if (unlikely(tlen <= (hdrsize + pad + 8)))
1582 tlen -= hdrsize + pad + 8;
1583 if (unlikely(tlen != qp->s_rdma_read_len))
1585 aeth = be32_to_cpu(ohdr->u.aeth);
1586 qib_copy_sge(&qp->s_rdma_read_sge, data, tlen, 0);
1587 WARN_ON(qp->s_rdma_read_sge.num_sge);
1588 (void) do_rc_ack(qp, aeth, psn,
1589 OP(RDMA_READ_RESPONSE_LAST), 0, rcd);
1594 status = IB_WC_LOC_QP_OP_ERR;
1598 rdma_seq_err(qp, ibp, psn, rcd);
1602 status = IB_WC_LOC_LEN_ERR;
1604 if (qp->s_last == qp->s_acked) {
1605 qib_send_complete(qp, wqe, status);
1606 qib_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1609 spin_unlock_irqrestore(&qp->s_lock, flags);
1615 * qib_rc_rcv_error - process an incoming duplicate or error RC packet
1616 * @ohdr: the other headers for this packet
1617 * @data: the packet data
1618 * @qp: the QP for this packet
1619 * @opcode: the opcode for this packet
1620 * @psn: the packet sequence number for this packet
1621 * @diff: the difference between the PSN and the expected PSN
1623 * This is called from qib_rc_rcv() to process an unexpected
1624 * incoming RC packet for the given QP.
1625 * Called at interrupt level.
1626 * Return 1 if no more processing is needed; otherwise return 0 to
1627 * schedule a response to be sent.
1629 static int qib_rc_rcv_error(struct qib_other_headers *ohdr,
1635 struct qib_ctxtdata *rcd)
1637 struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
1638 struct qib_ack_entry *e;
1639 unsigned long flags;
1645 * Packet sequence error.
1646 * A NAK will ACK earlier sends and RDMA writes.
1647 * Don't queue the NAK if we already sent one.
1649 if (!qp->r_nak_state) {
1651 qp->r_nak_state = IB_NAK_PSN_ERROR;
1652 /* Use the expected PSN. */
1653 qp->r_ack_psn = qp->r_psn;
1655 * Wait to send the sequence NAK until all packets
1656 * in the receive queue have been processed.
1657 * Otherwise, we end up propagating congestion.
1659 if (list_empty(&qp->rspwait)) {
1660 qp->r_flags |= QIB_R_RSP_NAK;
1661 atomic_inc(&qp->refcount);
1662 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
1669 * Handle a duplicate request. Don't re-execute SEND, RDMA
1670 * write or atomic op. Don't NAK errors, just silently drop
1671 * the duplicate request. Note that r_sge, r_len, and
1672 * r_rcv_len may be in use so don't modify them.
1674 * We are supposed to ACK the earliest duplicate PSN but we
1675 * can coalesce an outstanding duplicate ACK. We have to
1676 * send the earliest so that RDMA reads can be restarted at
1677 * the requester's expected PSN.
1679 * First, find where this duplicate PSN falls within the
1680 * ACKs previously sent.
1681 * old_req is true if there is an older response that is scheduled
1682 * to be sent before sending this one.
1688 spin_lock_irqsave(&qp->s_lock, flags);
1690 for (i = qp->r_head_ack_queue; ; i = prev) {
1691 if (i == qp->s_tail_ack_queue)
1696 prev = QIB_MAX_RDMA_ATOMIC;
1697 if (prev == qp->r_head_ack_queue) {
1701 e = &qp->s_ack_queue[prev];
1706 if (qib_cmp24(psn, e->psn) >= 0) {
1707 if (prev == qp->s_tail_ack_queue &&
1708 qib_cmp24(psn, e->lpsn) <= 0)
1714 case OP(RDMA_READ_REQUEST): {
1715 struct ib_reth *reth;
1720 * If we didn't find the RDMA read request in the ack queue,
1721 * we can ignore this request.
1723 if (!e || e->opcode != OP(RDMA_READ_REQUEST))
1725 /* RETH comes after BTH */
1726 reth = &ohdr->u.rc.reth;
1728 * Address range must be a subset of the original
1729 * request and start on pmtu boundaries.
1730 * We reuse the old ack_queue slot since the requester
1731 * should not back up and request an earlier PSN for the
1734 offset = ((psn - e->psn) & QIB_PSN_MASK) *
1735 ib_mtu_enum_to_int(qp->path_mtu);
1736 len = be32_to_cpu(reth->length);
1737 if (unlikely(offset + len != e->rdma_sge.sge_length))
1739 if (e->rdma_sge.mr) {
1740 atomic_dec(&e->rdma_sge.mr->refcount);
1741 e->rdma_sge.mr = NULL;
1744 u32 rkey = be32_to_cpu(reth->rkey);
1745 u64 vaddr = be64_to_cpu(reth->vaddr);
1748 ok = qib_rkey_ok(qp, &e->rdma_sge, len, vaddr, rkey,
1749 IB_ACCESS_REMOTE_READ);
1753 e->rdma_sge.vaddr = NULL;
1754 e->rdma_sge.length = 0;
1755 e->rdma_sge.sge_length = 0;
1760 qp->s_tail_ack_queue = prev;
1764 case OP(COMPARE_SWAP):
1765 case OP(FETCH_ADD): {
1767 * If we didn't find the atomic request in the ack queue
1768 * or the send tasklet is already backed up to send an
1769 * earlier entry, we can ignore this request.
1771 if (!e || e->opcode != (u8) opcode || old_req)
1773 qp->s_tail_ack_queue = prev;
1779 * Ignore this operation if it doesn't request an ACK
1780 * or an earlier RDMA read or atomic is going to be resent.
1782 if (!(psn & IB_BTH_REQ_ACK) || old_req)
1785 * Resend the most recent ACK if this request is
1786 * after all the previous RDMA reads and atomics.
1788 if (i == qp->r_head_ack_queue) {
1789 spin_unlock_irqrestore(&qp->s_lock, flags);
1790 qp->r_nak_state = 0;
1791 qp->r_ack_psn = qp->r_psn - 1;
1795 * Try to send a simple ACK to work around a Mellanox bug
1796 * which doesn't accept a RDMA read response or atomic
1797 * response as an ACK for earlier SENDs or RDMA writes.
1799 if (!(qp->s_flags & QIB_S_RESP_PENDING)) {
1800 spin_unlock_irqrestore(&qp->s_lock, flags);
1801 qp->r_nak_state = 0;
1802 qp->r_ack_psn = qp->s_ack_queue[i].psn - 1;
1806 * Resend the RDMA read or atomic op which
1807 * ACKs this duplicate request.
1809 qp->s_tail_ack_queue = i;
1812 qp->s_ack_state = OP(ACKNOWLEDGE);
1813 qp->s_flags |= QIB_S_RESP_PENDING;
1814 qp->r_nak_state = 0;
1815 qib_schedule_send(qp);
1818 spin_unlock_irqrestore(&qp->s_lock, flags);
1826 void qib_rc_error(struct qib_qp *qp, enum ib_wc_status err)
1828 unsigned long flags;
1831 spin_lock_irqsave(&qp->s_lock, flags);
1832 lastwqe = qib_error_qp(qp, err);
1833 spin_unlock_irqrestore(&qp->s_lock, flags);
1838 ev.device = qp->ibqp.device;
1839 ev.element.qp = &qp->ibqp;
1840 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1841 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1845 static inline void qib_update_ack_queue(struct qib_qp *qp, unsigned n)
1850 if (next > QIB_MAX_RDMA_ATOMIC)
1852 qp->s_tail_ack_queue = next;
1853 qp->s_ack_state = OP(ACKNOWLEDGE);
1857 * qib_rc_rcv - process an incoming RC packet
1858 * @rcd: the context pointer
1859 * @hdr: the header of this packet
1860 * @has_grh: true if the header has a GRH
1861 * @data: the packet data
1862 * @tlen: the packet length
1863 * @qp: the QP for this packet
1865 * This is called from qib_qp_rcv() to process an incoming RC packet
1867 * Called at interrupt level.
1869 void qib_rc_rcv(struct qib_ctxtdata *rcd, struct qib_ib_header *hdr,
1870 int has_grh, void *data, u32 tlen, struct qib_qp *qp)
1872 struct qib_ibport *ibp = &rcd->ppd->ibport_data;
1873 struct qib_other_headers *ohdr;
1879 u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
1881 struct ib_reth *reth;
1882 unsigned long flags;
1888 hdrsize = 8 + 12; /* LRH + BTH */
1890 ohdr = &hdr->u.l.oth;
1891 hdrsize = 8 + 40 + 12; /* LRH + GRH + BTH */
1894 opcode = be32_to_cpu(ohdr->bth[0]);
1895 spin_lock_irqsave(&qp->s_lock, flags);
1896 if (qib_ruc_check_hdr(ibp, hdr, has_grh, qp, opcode))
1898 spin_unlock_irqrestore(&qp->s_lock, flags);
1900 psn = be32_to_cpu(ohdr->bth[2]);
1904 * Process responses (ACKs) before anything else. Note that the
1905 * packet sequence number will be for something in the send work
1906 * queue rather than the expected receive packet sequence number.
1907 * In other words, this QP is the requester.
1909 if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
1910 opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
1911 qib_rc_rcv_resp(ibp, ohdr, data, tlen, qp, opcode, psn,
1912 hdrsize, pmtu, rcd);
1916 /* Compute 24 bits worth of difference. */
1917 diff = qib_cmp24(psn, qp->r_psn);
1918 if (unlikely(diff)) {
1919 if (qib_rc_rcv_error(ohdr, data, qp, opcode, psn, diff, rcd))
1924 /* Check for opcode sequence errors. */
1925 switch (qp->r_state) {
1926 case OP(SEND_FIRST):
1927 case OP(SEND_MIDDLE):
1928 if (opcode == OP(SEND_MIDDLE) ||
1929 opcode == OP(SEND_LAST) ||
1930 opcode == OP(SEND_LAST_WITH_IMMEDIATE))
1934 case OP(RDMA_WRITE_FIRST):
1935 case OP(RDMA_WRITE_MIDDLE):
1936 if (opcode == OP(RDMA_WRITE_MIDDLE) ||
1937 opcode == OP(RDMA_WRITE_LAST) ||
1938 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
1943 if (opcode == OP(SEND_MIDDLE) ||
1944 opcode == OP(SEND_LAST) ||
1945 opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
1946 opcode == OP(RDMA_WRITE_MIDDLE) ||
1947 opcode == OP(RDMA_WRITE_LAST) ||
1948 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
1951 * Note that it is up to the requester to not send a new
1952 * RDMA read or atomic operation before receiving an ACK
1953 * for the previous operation.
1958 memset(&wc, 0, sizeof wc);
1960 if (qp->state == IB_QPS_RTR && !(qp->r_flags & QIB_R_COMM_EST)) {
1961 qp->r_flags |= QIB_R_COMM_EST;
1962 if (qp->ibqp.event_handler) {
1965 ev.device = qp->ibqp.device;
1966 ev.element.qp = &qp->ibqp;
1967 ev.event = IB_EVENT_COMM_EST;
1968 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1972 /* OK, process the packet. */
1974 case OP(SEND_FIRST):
1975 ret = qib_get_rwqe(qp, 0);
1982 case OP(SEND_MIDDLE):
1983 case OP(RDMA_WRITE_MIDDLE):
1985 /* Check for invalid length PMTU or posted rwqe len. */
1986 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1988 qp->r_rcv_len += pmtu;
1989 if (unlikely(qp->r_rcv_len > qp->r_len))
1991 qib_copy_sge(&qp->r_sge, data, pmtu, 1);
1994 case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
1996 ret = qib_get_rwqe(qp, 1);
2004 case OP(SEND_ONLY_WITH_IMMEDIATE):
2005 ret = qib_get_rwqe(qp, 0);
2011 if (opcode == OP(SEND_ONLY))
2014 case OP(SEND_LAST_WITH_IMMEDIATE):
2016 wc.ex.imm_data = ohdr->u.imm_data;
2018 wc.wc_flags = IB_WC_WITH_IMM;
2021 case OP(RDMA_WRITE_LAST):
2023 /* Get the number of bytes the message was padded by. */
2024 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
2025 /* Check for invalid length. */
2026 /* XXX LAST len should be >= 1 */
2027 if (unlikely(tlen < (hdrsize + pad + 4)))
2029 /* Don't count the CRC. */
2030 tlen -= (hdrsize + pad + 4);
2031 wc.byte_len = tlen + qp->r_rcv_len;
2032 if (unlikely(wc.byte_len > qp->r_len))
2034 qib_copy_sge(&qp->r_sge, data, tlen, 1);
2035 while (qp->r_sge.num_sge) {
2036 atomic_dec(&qp->r_sge.sge.mr->refcount);
2037 if (--qp->r_sge.num_sge)
2038 qp->r_sge.sge = *qp->r_sge.sg_list++;
2041 if (!test_and_clear_bit(QIB_R_WRID_VALID, &qp->r_aflags))
2043 wc.wr_id = qp->r_wr_id;
2044 wc.status = IB_WC_SUCCESS;
2045 if (opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE) ||
2046 opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
2047 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
2049 wc.opcode = IB_WC_RECV;
2051 wc.src_qp = qp->remote_qpn;
2052 wc.slid = qp->remote_ah_attr.dlid;
2053 wc.sl = qp->remote_ah_attr.sl;
2054 /* Signal completion event if the solicited bit is set. */
2055 qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
2057 cpu_to_be32(IB_BTH_SOLICITED)) != 0);
2060 case OP(RDMA_WRITE_FIRST):
2061 case OP(RDMA_WRITE_ONLY):
2062 case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
2063 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
2066 reth = &ohdr->u.rc.reth;
2067 hdrsize += sizeof(*reth);
2068 qp->r_len = be32_to_cpu(reth->length);
2070 qp->r_sge.sg_list = NULL;
2071 if (qp->r_len != 0) {
2072 u32 rkey = be32_to_cpu(reth->rkey);
2073 u64 vaddr = be64_to_cpu(reth->vaddr);
2076 /* Check rkey & NAK */
2077 ok = qib_rkey_ok(qp, &qp->r_sge.sge, qp->r_len, vaddr,
2078 rkey, IB_ACCESS_REMOTE_WRITE);
2081 qp->r_sge.num_sge = 1;
2083 qp->r_sge.num_sge = 0;
2084 qp->r_sge.sge.mr = NULL;
2085 qp->r_sge.sge.vaddr = NULL;
2086 qp->r_sge.sge.length = 0;
2087 qp->r_sge.sge.sge_length = 0;
2089 if (opcode == OP(RDMA_WRITE_FIRST))
2091 else if (opcode == OP(RDMA_WRITE_ONLY))
2093 ret = qib_get_rwqe(qp, 1);
2098 wc.ex.imm_data = ohdr->u.rc.imm_data;
2100 wc.wc_flags = IB_WC_WITH_IMM;
2103 case OP(RDMA_READ_REQUEST): {
2104 struct qib_ack_entry *e;
2108 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
2110 next = qp->r_head_ack_queue + 1;
2111 /* s_ack_queue is size QIB_MAX_RDMA_ATOMIC+1 so use > not >= */
2112 if (next > QIB_MAX_RDMA_ATOMIC)
2114 spin_lock_irqsave(&qp->s_lock, flags);
2115 if (unlikely(next == qp->s_tail_ack_queue)) {
2116 if (!qp->s_ack_queue[next].sent)
2117 goto nack_inv_unlck;
2118 qib_update_ack_queue(qp, next);
2120 e = &qp->s_ack_queue[qp->r_head_ack_queue];
2121 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
2122 atomic_dec(&e->rdma_sge.mr->refcount);
2123 e->rdma_sge.mr = NULL;
2125 reth = &ohdr->u.rc.reth;
2126 len = be32_to_cpu(reth->length);
2128 u32 rkey = be32_to_cpu(reth->rkey);
2129 u64 vaddr = be64_to_cpu(reth->vaddr);
2132 /* Check rkey & NAK */
2133 ok = qib_rkey_ok(qp, &e->rdma_sge, len, vaddr,
2134 rkey, IB_ACCESS_REMOTE_READ);
2136 goto nack_acc_unlck;
2138 * Update the next expected PSN. We add 1 later
2139 * below, so only add the remainder here.
2142 qp->r_psn += (len - 1) / pmtu;
2144 e->rdma_sge.mr = NULL;
2145 e->rdma_sge.vaddr = NULL;
2146 e->rdma_sge.length = 0;
2147 e->rdma_sge.sge_length = 0;
2152 e->lpsn = qp->r_psn;
2154 * We need to increment the MSN here instead of when we
2155 * finish sending the result since a duplicate request would
2156 * increment it more than once.
2160 qp->r_state = opcode;
2161 qp->r_nak_state = 0;
2162 qp->r_head_ack_queue = next;
2164 /* Schedule the send tasklet. */
2165 qp->s_flags |= QIB_S_RESP_PENDING;
2166 qib_schedule_send(qp);
2171 case OP(COMPARE_SWAP):
2172 case OP(FETCH_ADD): {
2173 struct ib_atomic_eth *ateth;
2174 struct qib_ack_entry *e;
2181 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
2183 next = qp->r_head_ack_queue + 1;
2184 if (next > QIB_MAX_RDMA_ATOMIC)
2186 spin_lock_irqsave(&qp->s_lock, flags);
2187 if (unlikely(next == qp->s_tail_ack_queue)) {
2188 if (!qp->s_ack_queue[next].sent)
2189 goto nack_inv_unlck;
2190 qib_update_ack_queue(qp, next);
2192 e = &qp->s_ack_queue[qp->r_head_ack_queue];
2193 if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
2194 atomic_dec(&e->rdma_sge.mr->refcount);
2195 e->rdma_sge.mr = NULL;
2197 ateth = &ohdr->u.atomic_eth;
2198 vaddr = ((u64) be32_to_cpu(ateth->vaddr[0]) << 32) |
2199 be32_to_cpu(ateth->vaddr[1]);
2200 if (unlikely(vaddr & (sizeof(u64) - 1)))
2201 goto nack_inv_unlck;
2202 rkey = be32_to_cpu(ateth->rkey);
2203 /* Check rkey & NAK */
2204 if (unlikely(!qib_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
2206 IB_ACCESS_REMOTE_ATOMIC)))
2207 goto nack_acc_unlck;
2208 /* Perform atomic OP and save result. */
2209 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
2210 sdata = be64_to_cpu(ateth->swap_data);
2211 e->atomic_data = (opcode == OP(FETCH_ADD)) ?
2212 (u64) atomic64_add_return(sdata, maddr) - sdata :
2213 (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
2214 be64_to_cpu(ateth->compare_data),
2216 atomic_dec(&qp->r_sge.sge.mr->refcount);
2217 qp->r_sge.num_sge = 0;
2224 qp->r_state = opcode;
2225 qp->r_nak_state = 0;
2226 qp->r_head_ack_queue = next;
2228 /* Schedule the send tasklet. */
2229 qp->s_flags |= QIB_S_RESP_PENDING;
2230 qib_schedule_send(qp);
2236 /* NAK unknown opcodes. */
2240 qp->r_state = opcode;
2241 qp->r_ack_psn = psn;
2242 qp->r_nak_state = 0;
2243 /* Send an ACK if requested or required. */
2244 if (psn & (1 << 31))
2249 qp->r_nak_state = IB_RNR_NAK | qp->r_min_rnr_timer;
2250 qp->r_ack_psn = qp->r_psn;
2251 /* Queue RNR NAK for later */
2252 if (list_empty(&qp->rspwait)) {
2253 qp->r_flags |= QIB_R_RSP_NAK;
2254 atomic_inc(&qp->refcount);
2255 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
2260 qib_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
2261 qp->r_nak_state = IB_NAK_REMOTE_OPERATIONAL_ERROR;
2262 qp->r_ack_psn = qp->r_psn;
2263 /* Queue NAK for later */
2264 if (list_empty(&qp->rspwait)) {
2265 qp->r_flags |= QIB_R_RSP_NAK;
2266 atomic_inc(&qp->refcount);
2267 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
2272 spin_unlock_irqrestore(&qp->s_lock, flags);
2274 qib_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
2275 qp->r_nak_state = IB_NAK_INVALID_REQUEST;
2276 qp->r_ack_psn = qp->r_psn;
2277 /* Queue NAK for later */
2278 if (list_empty(&qp->rspwait)) {
2279 qp->r_flags |= QIB_R_RSP_NAK;
2280 atomic_inc(&qp->refcount);
2281 list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
2286 spin_unlock_irqrestore(&qp->s_lock, flags);
2288 qib_rc_error(qp, IB_WC_LOC_PROT_ERR);
2289 qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
2290 qp->r_ack_psn = qp->r_psn;
2292 qib_send_rc_ack(qp);
2296 spin_unlock_irqrestore(&qp->s_lock, flags);