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
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
34 #include "ipath_verbs.h"
35 #include "ipath_common.h"
37 /* cut down ridiculously long IB macro names */
38 #define OP(x) IB_OPCODE_RC_##x
41 * ipath_init_restart- initialize the qp->s_sge after a restart
42 * @qp: the QP who's SGE we're restarting
43 * @wqe: the work queue to initialize the QP's SGE from
45 * The QP s_lock should be held and interrupts disabled.
47 static void ipath_init_restart(struct ipath_qp *qp, struct ipath_swqe *wqe)
49 struct ipath_ibdev *dev;
52 len = ((qp->s_psn - wqe->psn) & IPATH_PSN_MASK) *
53 ib_mtu_enum_to_int(qp->path_mtu);
54 qp->s_sge.sge = wqe->sg_list[0];
55 qp->s_sge.sg_list = wqe->sg_list + 1;
56 qp->s_sge.num_sge = wqe->wr.num_sge;
57 ipath_skip_sge(&qp->s_sge, len);
58 qp->s_len = wqe->length - len;
59 dev = to_idev(qp->ibqp.device);
60 spin_lock(&dev->pending_lock);
61 if (list_empty(&qp->timerwait))
62 list_add_tail(&qp->timerwait,
63 &dev->pending[dev->pending_index]);
64 spin_unlock(&dev->pending_lock);
68 * ipath_make_rc_ack - construct a response packet (ACK, NAK, or RDMA read)
69 * @qp: a pointer to the QP
70 * @ohdr: a pointer to the IB header being constructed
73 * Return bth0 if constructed; otherwise, return 0.
74 * Note the QP s_lock must be held.
76 u32 ipath_make_rc_ack(struct ipath_qp *qp,
77 struct ipath_other_headers *ohdr,
84 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
88 * Send a response. Note that we are in the responder's
89 * side of the QP context.
91 switch (qp->s_ack_state) {
92 case OP(RDMA_READ_REQUEST):
93 qp->s_cur_sge = &qp->s_rdma_sge;
97 qp->s_ack_state = OP(RDMA_READ_RESPONSE_FIRST);
99 qp->s_ack_state = OP(RDMA_READ_RESPONSE_ONLY);
100 qp->s_rdma_len -= len;
101 bth0 = qp->s_ack_state << 24;
102 ohdr->u.aeth = ipath_compute_aeth(qp);
106 case OP(RDMA_READ_RESPONSE_FIRST):
107 qp->s_ack_state = OP(RDMA_READ_RESPONSE_MIDDLE);
109 case OP(RDMA_READ_RESPONSE_MIDDLE):
110 qp->s_cur_sge = &qp->s_rdma_sge;
111 len = qp->s_rdma_len;
115 ohdr->u.aeth = ipath_compute_aeth(qp);
117 qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
119 qp->s_rdma_len -= len;
120 bth0 = qp->s_ack_state << 24;
123 case OP(RDMA_READ_RESPONSE_LAST):
124 case OP(RDMA_READ_RESPONSE_ONLY):
126 * We have to prevent new requests from changing
127 * the r_sge state while a ipath_verbs_send()
130 qp->s_ack_state = OP(ACKNOWLEDGE);
134 case OP(COMPARE_SWAP):
136 qp->s_cur_sge = NULL;
139 * Set the s_ack_state so the receive interrupt handler
140 * won't try to send an ACK (out of order) until this one
143 qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
144 bth0 = OP(ATOMIC_ACKNOWLEDGE) << 24;
145 ohdr->u.at.aeth = ipath_compute_aeth(qp);
146 ohdr->u.at.atomic_ack_eth = cpu_to_be64(qp->r_atomic_data);
147 hwords += sizeof(ohdr->u.at) / 4;
151 /* Send a regular ACK. */
152 qp->s_cur_sge = NULL;
155 * Set the s_ack_state so the receive interrupt handler
156 * won't try to send an ACK (out of order) until this one
159 qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
160 bth0 = OP(ACKNOWLEDGE) << 24;
162 ohdr->u.aeth = cpu_to_be32((qp->r_msn & IPATH_MSN_MASK) |
164 IPATH_AETH_CREDIT_SHIFT));
166 ohdr->u.aeth = ipath_compute_aeth(qp);
169 qp->s_hdrwords = hwords;
170 qp->s_cur_size = len;
177 * ipath_make_rc_req - construct a request packet (SEND, RDMA r/w, ATOMIC)
178 * @qp: a pointer to the QP
179 * @ohdr: a pointer to the IB header being constructed
180 * @pmtu: the path MTU
181 * @bth0p: pointer to the BTH opcode word
182 * @bth2p: pointer to the BTH PSN word
184 * Return 1 if constructed; otherwise, return 0.
185 * Note the QP s_lock must be held and interrupts disabled.
187 int ipath_make_rc_req(struct ipath_qp *qp,
188 struct ipath_other_headers *ohdr,
189 u32 pmtu, u32 *bth0p, u32 *bth2p)
191 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
192 struct ipath_sge_state *ss;
193 struct ipath_swqe *wqe;
200 if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_SEND_OK) ||
204 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
208 /* Send a request. */
209 wqe = get_swqe_ptr(qp, qp->s_cur);
210 switch (qp->s_state) {
213 * Resend an old request or start a new one.
215 * We keep track of the current SWQE so that
216 * we don't reset the "furthest progress" state
217 * if we need to back up.
220 if (qp->s_cur == qp->s_tail) {
221 /* Check if send work queue is empty. */
222 if (qp->s_tail == qp->s_head)
224 qp->s_psn = wqe->psn = qp->s_next_psn;
228 * Note that we have to be careful not to modify the
229 * original work request since we may need to resend
232 qp->s_sge.sge = wqe->sg_list[0];
233 qp->s_sge.sg_list = wqe->sg_list + 1;
234 qp->s_sge.num_sge = wqe->wr.num_sge;
235 qp->s_len = len = wqe->length;
238 switch (wqe->wr.opcode) {
240 case IB_WR_SEND_WITH_IMM:
241 /* If no credit, return. */
242 if (qp->s_lsn != (u32) -1 &&
243 ipath_cmp24(wqe->ssn, qp->s_lsn + 1) > 0)
245 wqe->lpsn = wqe->psn;
247 wqe->lpsn += (len - 1) / pmtu;
248 qp->s_state = OP(SEND_FIRST);
252 if (wqe->wr.opcode == IB_WR_SEND)
253 qp->s_state = OP(SEND_ONLY);
255 qp->s_state = OP(SEND_ONLY_WITH_IMMEDIATE);
256 /* Immediate data comes after the BTH */
257 ohdr->u.imm_data = wqe->wr.imm_data;
260 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
262 bth2 = 1 << 31; /* Request ACK. */
263 if (++qp->s_cur == qp->s_size)
267 case IB_WR_RDMA_WRITE:
268 if (newreq && qp->s_lsn != (u32) -1)
271 case IB_WR_RDMA_WRITE_WITH_IMM:
272 /* If no credit, return. */
273 if (qp->s_lsn != (u32) -1 &&
274 ipath_cmp24(wqe->ssn, qp->s_lsn + 1) > 0)
276 ohdr->u.rc.reth.vaddr =
277 cpu_to_be64(wqe->wr.wr.rdma.remote_addr);
278 ohdr->u.rc.reth.rkey =
279 cpu_to_be32(wqe->wr.wr.rdma.rkey);
280 ohdr->u.rc.reth.length = cpu_to_be32(len);
281 hwords += sizeof(struct ib_reth) / 4;
282 wqe->lpsn = wqe->psn;
284 wqe->lpsn += (len - 1) / pmtu;
285 qp->s_state = OP(RDMA_WRITE_FIRST);
289 if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
290 qp->s_state = OP(RDMA_WRITE_ONLY);
293 OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
294 /* Immediate data comes after RETH */
295 ohdr->u.rc.imm_data = wqe->wr.imm_data;
297 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
300 bth2 = 1 << 31; /* Request ACK. */
301 if (++qp->s_cur == qp->s_size)
305 case IB_WR_RDMA_READ:
306 ohdr->u.rc.reth.vaddr =
307 cpu_to_be64(wqe->wr.wr.rdma.remote_addr);
308 ohdr->u.rc.reth.rkey =
309 cpu_to_be32(wqe->wr.wr.rdma.rkey);
310 ohdr->u.rc.reth.length = cpu_to_be32(len);
311 qp->s_state = OP(RDMA_READ_REQUEST);
312 hwords += sizeof(ohdr->u.rc.reth) / 4;
314 if (qp->s_lsn != (u32) -1)
317 * Adjust s_next_psn to count the
318 * expected number of responses.
321 qp->s_next_psn += (len - 1) / pmtu;
322 wqe->lpsn = qp->s_next_psn++;
326 if (++qp->s_cur == qp->s_size)
330 case IB_WR_ATOMIC_CMP_AND_SWP:
331 case IB_WR_ATOMIC_FETCH_AND_ADD:
332 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP)
333 qp->s_state = OP(COMPARE_SWAP);
335 qp->s_state = OP(FETCH_ADD);
336 ohdr->u.atomic_eth.vaddr = cpu_to_be64(
337 wqe->wr.wr.atomic.remote_addr);
338 ohdr->u.atomic_eth.rkey = cpu_to_be32(
339 wqe->wr.wr.atomic.rkey);
340 ohdr->u.atomic_eth.swap_data = cpu_to_be64(
341 wqe->wr.wr.atomic.swap);
342 ohdr->u.atomic_eth.compare_data = cpu_to_be64(
343 wqe->wr.wr.atomic.compare_add);
344 hwords += sizeof(struct ib_atomic_eth) / 4;
346 if (qp->s_lsn != (u32) -1)
348 wqe->lpsn = wqe->psn;
350 if (++qp->s_cur == qp->s_size)
361 if (qp->s_tail >= qp->s_size)
364 bth2 |= qp->s_psn++ & IPATH_PSN_MASK;
365 if ((int)(qp->s_psn - qp->s_next_psn) > 0)
366 qp->s_next_psn = qp->s_psn;
368 * Put the QP on the pending list so lost ACKs will cause
369 * a retry. More than one request can be pending so the
370 * QP may already be on the dev->pending list.
372 spin_lock(&dev->pending_lock);
373 if (list_empty(&qp->timerwait))
374 list_add_tail(&qp->timerwait,
375 &dev->pending[dev->pending_index]);
376 spin_unlock(&dev->pending_lock);
379 case OP(RDMA_READ_RESPONSE_FIRST):
381 * This case can only happen if a send is restarted.
382 * See ipath_restart_rc().
384 ipath_init_restart(qp, wqe);
387 qp->s_state = OP(SEND_MIDDLE);
389 case OP(SEND_MIDDLE):
390 bth2 = qp->s_psn++ & IPATH_PSN_MASK;
391 if ((int)(qp->s_psn - qp->s_next_psn) > 0)
392 qp->s_next_psn = qp->s_psn;
397 * Request an ACK every 1/2 MB to avoid retransmit
400 if (((wqe->length - len) % (512 * 1024)) == 0)
405 if (wqe->wr.opcode == IB_WR_SEND)
406 qp->s_state = OP(SEND_LAST);
408 qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
409 /* Immediate data comes after the BTH */
410 ohdr->u.imm_data = wqe->wr.imm_data;
413 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
415 bth2 |= 1 << 31; /* Request ACK. */
417 if (qp->s_cur >= qp->s_size)
421 case OP(RDMA_READ_RESPONSE_LAST):
423 * This case can only happen if a RDMA write is restarted.
424 * See ipath_restart_rc().
426 ipath_init_restart(qp, wqe);
428 case OP(RDMA_WRITE_FIRST):
429 qp->s_state = OP(RDMA_WRITE_MIDDLE);
431 case OP(RDMA_WRITE_MIDDLE):
432 bth2 = qp->s_psn++ & IPATH_PSN_MASK;
433 if ((int)(qp->s_psn - qp->s_next_psn) > 0)
434 qp->s_next_psn = qp->s_psn;
439 * Request an ACK every 1/2 MB to avoid retransmit
442 if (((wqe->length - len) % (512 * 1024)) == 0)
447 if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
448 qp->s_state = OP(RDMA_WRITE_LAST);
450 qp->s_state = OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
451 /* Immediate data comes after the BTH */
452 ohdr->u.imm_data = wqe->wr.imm_data;
454 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
457 bth2 |= 1 << 31; /* Request ACK. */
459 if (qp->s_cur >= qp->s_size)
463 case OP(RDMA_READ_RESPONSE_MIDDLE):
465 * This case can only happen if a RDMA read is restarted.
466 * See ipath_restart_rc().
468 ipath_init_restart(qp, wqe);
469 len = ((qp->s_psn - wqe->psn) & IPATH_PSN_MASK) * pmtu;
470 ohdr->u.rc.reth.vaddr =
471 cpu_to_be64(wqe->wr.wr.rdma.remote_addr + len);
472 ohdr->u.rc.reth.rkey =
473 cpu_to_be32(wqe->wr.wr.rdma.rkey);
474 ohdr->u.rc.reth.length = cpu_to_be32(qp->s_len);
475 qp->s_state = OP(RDMA_READ_REQUEST);
476 hwords += sizeof(ohdr->u.rc.reth) / 4;
477 bth2 = qp->s_psn++ & IPATH_PSN_MASK;
478 if ((int)(qp->s_psn - qp->s_next_psn) > 0)
479 qp->s_next_psn = qp->s_psn;
483 if (qp->s_cur == qp->s_size)
487 case OP(RDMA_READ_REQUEST):
488 case OP(COMPARE_SWAP):
491 * We shouldn't start anything new until this request is
492 * finished. The ACK will handle rescheduling us. XXX The
493 * number of outstanding ones is negotiated at connection
494 * setup time (see pg. 258,289)? XXX Also, if we support
495 * multiple outstanding requests, we need to check the WQE
496 * IB_SEND_FENCE flag and not send a new request if a RDMA
497 * read or atomic is pending.
502 qp->s_hdrwords = hwords;
504 qp->s_cur_size = len;
505 *bth0p = bth0 | (qp->s_state << 24);
514 * send_rc_ack - Construct an ACK packet and send it
515 * @qp: a pointer to the QP
517 * This is called from ipath_rc_rcv() and only uses the receive
519 * Note that RDMA reads are handled in the send side QP state and tasklet.
521 static void send_rc_ack(struct ipath_qp *qp)
523 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
527 struct ipath_ib_header hdr;
528 struct ipath_other_headers *ohdr;
530 /* Construct the header. */
532 lrh0 = IPATH_LRH_BTH;
533 /* header size in 32-bit words LRH+BTH+AETH = (8+12+4)/4. */
535 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
536 hwords += ipath_make_grh(dev, &hdr.u.l.grh,
537 &qp->remote_ah_attr.grh,
540 lrh0 = IPATH_LRH_GRH;
542 /* read pkey_index w/o lock (its atomic) */
543 bth0 = ipath_layer_get_pkey(dev->dd, qp->s_pkey_index);
545 ohdr->u.aeth = cpu_to_be32((qp->r_msn & IPATH_MSN_MASK) |
547 IPATH_AETH_CREDIT_SHIFT));
549 ohdr->u.aeth = ipath_compute_aeth(qp);
550 if (qp->r_ack_state >= OP(COMPARE_SWAP)) {
551 bth0 |= OP(ATOMIC_ACKNOWLEDGE) << 24;
552 ohdr->u.at.atomic_ack_eth = cpu_to_be64(qp->r_atomic_data);
553 hwords += sizeof(ohdr->u.at.atomic_ack_eth) / 4;
555 bth0 |= OP(ACKNOWLEDGE) << 24;
556 lrh0 |= qp->remote_ah_attr.sl << 4;
557 hdr.lrh[0] = cpu_to_be16(lrh0);
558 hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
559 hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
560 hdr.lrh[3] = cpu_to_be16(ipath_layer_get_lid(dev->dd));
561 ohdr->bth[0] = cpu_to_be32(bth0);
562 ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
563 ohdr->bth[2] = cpu_to_be32(qp->r_ack_psn & IPATH_PSN_MASK);
566 * If we can send the ACK, clear the ACK state.
568 if (ipath_verbs_send(dev->dd, hwords, (u32 *) &hdr, 0, NULL) == 0) {
569 qp->r_ack_state = OP(ACKNOWLEDGE);
570 dev->n_unicast_xmit++;
573 * We are out of PIO buffers at the moment.
574 * Pass responsibility for sending the ACK to the
575 * send tasklet so that when a PIO buffer becomes
576 * available, the ACK is sent ahead of other outgoing
580 spin_lock_irq(&qp->s_lock);
581 /* Don't coalesce if a RDMA read or atomic is pending. */
582 if (qp->s_ack_state == OP(ACKNOWLEDGE) ||
583 qp->s_ack_state < OP(RDMA_READ_REQUEST)) {
584 qp->s_ack_state = qp->r_ack_state;
585 qp->s_nak_state = qp->r_nak_state;
586 qp->s_ack_psn = qp->r_ack_psn;
587 qp->r_ack_state = OP(ACKNOWLEDGE);
589 spin_unlock_irq(&qp->s_lock);
591 /* Call ipath_do_rc_send() in another thread. */
592 tasklet_hi_schedule(&qp->s_task);
597 * reset_psn - reset the QP state to send starting from PSN
599 * @psn: the packet sequence number to restart at
601 * This is called from ipath_rc_rcv() to process an incoming RC ACK
603 * Called at interrupt level with the QP s_lock held.
605 static void reset_psn(struct ipath_qp *qp, u32 psn)
608 struct ipath_swqe *wqe = get_swqe_ptr(qp, n);
614 * If we are starting the request from the beginning,
615 * let the normal send code handle initialization.
617 if (ipath_cmp24(psn, wqe->psn) <= 0) {
618 qp->s_state = OP(SEND_LAST);
622 /* Find the work request opcode corresponding to the given PSN. */
623 opcode = wqe->wr.opcode;
627 if (++n == qp->s_size)
631 wqe = get_swqe_ptr(qp, n);
632 diff = ipath_cmp24(psn, wqe->psn);
637 * If we are starting the request from the beginning,
638 * let the normal send code handle initialization.
641 qp->s_state = OP(SEND_LAST);
644 opcode = wqe->wr.opcode;
648 * Set the state to restart in the middle of a request.
649 * Don't change the s_sge, s_cur_sge, or s_cur_size.
650 * See ipath_do_rc_send().
654 case IB_WR_SEND_WITH_IMM:
655 qp->s_state = OP(RDMA_READ_RESPONSE_FIRST);
658 case IB_WR_RDMA_WRITE:
659 case IB_WR_RDMA_WRITE_WITH_IMM:
660 qp->s_state = OP(RDMA_READ_RESPONSE_LAST);
663 case IB_WR_RDMA_READ:
664 qp->s_state = OP(RDMA_READ_RESPONSE_MIDDLE);
669 * This case shouldn't happen since its only
672 qp->s_state = OP(SEND_LAST);
679 * ipath_restart_rc - back up requester to resend the last un-ACKed request
680 * @qp: the QP to restart
681 * @psn: packet sequence number for the request
682 * @wc: the work completion request
684 * The QP s_lock should be held and interrupts disabled.
686 void ipath_restart_rc(struct ipath_qp *qp, u32 psn, struct ib_wc *wc)
688 struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
689 struct ipath_ibdev *dev;
692 * If there are no requests pending, we are done.
694 if (ipath_cmp24(psn, qp->s_next_psn) >= 0 ||
695 qp->s_last == qp->s_tail)
698 if (qp->s_retry == 0) {
699 wc->wr_id = wqe->wr.wr_id;
700 wc->status = IB_WC_RETRY_EXC_ERR;
701 wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
704 wc->qp_num = qp->ibqp.qp_num;
705 wc->src_qp = qp->remote_qpn;
707 wc->slid = qp->remote_ah_attr.dlid;
708 wc->sl = qp->remote_ah_attr.sl;
709 wc->dlid_path_bits = 0;
711 ipath_sqerror_qp(qp, wc);
717 * Remove the QP from the timeout queue.
718 * Note: it may already have been removed by ipath_ib_timer().
720 dev = to_idev(qp->ibqp.device);
721 spin_lock(&dev->pending_lock);
722 if (!list_empty(&qp->timerwait))
723 list_del_init(&qp->timerwait);
724 spin_unlock(&dev->pending_lock);
726 if (wqe->wr.opcode == IB_WR_RDMA_READ)
729 dev->n_rc_resends += (int)qp->s_psn - (int)psn;
734 tasklet_hi_schedule(&qp->s_task);
741 * do_rc_ack - process an incoming RC ACK
742 * @qp: the QP the ACK came in on
743 * @psn: the packet sequence number of the ACK
744 * @opcode: the opcode of the request that resulted in the ACK
746 * This is called from ipath_rc_rcv_resp() to process an incoming RC ACK
748 * Called at interrupt level with the QP s_lock held and interrupts disabled.
749 * Returns 1 if OK, 0 if current operation should be aborted (NAK).
751 static int do_rc_ack(struct ipath_qp *qp, u32 aeth, u32 psn, int opcode)
753 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
755 struct ipath_swqe *wqe;
759 * Remove the QP from the timeout queue (or RNR timeout queue).
760 * If ipath_ib_timer() has already removed it,
761 * it's OK since we hold the QP s_lock and ipath_restart_rc()
762 * just won't find anything to restart if we ACK everything.
764 spin_lock(&dev->pending_lock);
765 if (!list_empty(&qp->timerwait))
766 list_del_init(&qp->timerwait);
767 spin_unlock(&dev->pending_lock);
770 * Note that NAKs implicitly ACK outstanding SEND and RDMA write
771 * requests and implicitly NAK RDMA read and atomic requests issued
772 * before the NAK'ed request. The MSN won't include the NAK'ed
773 * request but will include an ACK'ed request(s).
775 wqe = get_swqe_ptr(qp, qp->s_last);
777 /* Nothing is pending to ACK/NAK. */
778 if (qp->s_last == qp->s_tail)
782 * The MSN might be for a later WQE than the PSN indicates so
783 * only complete WQEs that the PSN finishes.
785 while (ipath_cmp24(psn, wqe->lpsn) >= 0) {
786 /* If we are ACKing a WQE, the MSN should be >= the SSN. */
787 if (ipath_cmp24(aeth, wqe->ssn) < 0)
790 * If this request is a RDMA read or atomic, and the ACK is
791 * for a later operation, this ACK NAKs the RDMA read or
792 * atomic. In other words, only a RDMA_READ_LAST or ONLY
793 * can ACK a RDMA read and likewise for atomic ops. Note
794 * that the NAK case can only happen if relaxed ordering is
795 * used and requests are sent after an RDMA read or atomic
796 * is sent but before the response is received.
798 if ((wqe->wr.opcode == IB_WR_RDMA_READ &&
799 opcode != OP(RDMA_READ_RESPONSE_LAST)) ||
800 ((wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
801 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) &&
802 (opcode != OP(ATOMIC_ACKNOWLEDGE) ||
803 ipath_cmp24(wqe->psn, psn) != 0))) {
805 * The last valid PSN seen is the previous
808 qp->s_last_psn = wqe->psn - 1;
809 /* Retry this request. */
810 ipath_restart_rc(qp, wqe->psn, &wc);
812 * No need to process the ACK/NAK since we are
813 * restarting an earlier request.
817 /* Post a send completion queue entry if requested. */
818 if (!test_bit(IPATH_S_SIGNAL_REQ_WR, &qp->s_flags) ||
819 (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
820 wc.wr_id = wqe->wr.wr_id;
821 wc.status = IB_WC_SUCCESS;
822 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
824 wc.byte_len = wqe->length;
825 wc.qp_num = qp->ibqp.qp_num;
826 wc.src_qp = qp->remote_qpn;
828 wc.slid = qp->remote_ah_attr.dlid;
829 wc.sl = qp->remote_ah_attr.sl;
830 wc.dlid_path_bits = 0;
832 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 0);
834 qp->s_retry = qp->s_retry_cnt;
836 * If we are completing a request which is in the process of
837 * being resent, we can stop resending it since we know the
838 * responder has already seen it.
840 if (qp->s_last == qp->s_cur) {
841 if (++qp->s_cur >= qp->s_size)
843 wqe = get_swqe_ptr(qp, qp->s_cur);
844 qp->s_state = OP(SEND_LAST);
845 qp->s_psn = wqe->psn;
847 if (++qp->s_last >= qp->s_size)
849 wqe = get_swqe_ptr(qp, qp->s_last);
850 if (qp->s_last == qp->s_tail)
854 switch (aeth >> 29) {
857 /* If this is a partial ACK, reset the retransmit timer. */
858 if (qp->s_last != qp->s_tail) {
859 spin_lock(&dev->pending_lock);
860 list_add_tail(&qp->timerwait,
861 &dev->pending[dev->pending_index]);
862 spin_unlock(&dev->pending_lock);
864 ipath_get_credit(qp, aeth);
865 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
866 qp->s_retry = qp->s_retry_cnt;
867 qp->s_last_psn = psn;
871 case 1: /* RNR NAK */
873 if (qp->s_rnr_retry == 0) {
874 if (qp->s_last == qp->s_tail)
877 wc.status = IB_WC_RNR_RETRY_EXC_ERR;
880 if (qp->s_rnr_retry_cnt < 7)
882 if (qp->s_last == qp->s_tail)
885 /* The last valid PSN is the previous PSN. */
886 qp->s_last_psn = psn - 1;
888 dev->n_rc_resends += (int)qp->s_psn - (int)psn;
893 ib_ipath_rnr_table[(aeth >> IPATH_AETH_CREDIT_SHIFT) &
894 IPATH_AETH_CREDIT_MASK];
895 ipath_insert_rnr_queue(qp);
899 /* The last valid PSN seen is the previous request's. */
900 if (qp->s_last != qp->s_tail)
901 qp->s_last_psn = wqe->psn - 1;
902 switch ((aeth >> IPATH_AETH_CREDIT_SHIFT) &
903 IPATH_AETH_CREDIT_MASK) {
904 case 0: /* PSN sequence error */
907 * Back up to the responder's expected PSN. XXX
908 * Note that we might get a NAK in the middle of an
909 * RDMA READ response which terminates the RDMA
912 if (qp->s_last == qp->s_tail)
915 if (ipath_cmp24(psn, wqe->psn) < 0)
918 /* Retry the request. */
919 ipath_restart_rc(qp, psn, &wc);
922 case 1: /* Invalid Request */
923 wc.status = IB_WC_REM_INV_REQ_ERR;
927 case 2: /* Remote Access Error */
928 wc.status = IB_WC_REM_ACCESS_ERR;
932 case 3: /* Remote Operation Error */
933 wc.status = IB_WC_REM_OP_ERR;
936 wc.wr_id = wqe->wr.wr_id;
937 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
940 wc.qp_num = qp->ibqp.qp_num;
941 wc.src_qp = qp->remote_qpn;
943 wc.slid = qp->remote_ah_attr.dlid;
944 wc.sl = qp->remote_ah_attr.sl;
945 wc.dlid_path_bits = 0;
947 ipath_sqerror_qp(qp, &wc);
951 /* Ignore other reserved NAK error codes */
954 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
957 default: /* 2: reserved */
959 /* Ignore reserved NAK codes. */
968 * ipath_rc_rcv_resp - process an incoming RC response packet
969 * @dev: the device this packet came in on
970 * @ohdr: the other headers for this packet
971 * @data: the packet data
972 * @tlen: the packet length
973 * @qp: the QP for this packet
974 * @opcode: the opcode for this packet
975 * @psn: the packet sequence number for this packet
976 * @hdrsize: the header length
977 * @pmtu: the path MTU
978 * @header_in_data: true if part of the header data is in the data buffer
980 * This is called from ipath_rc_rcv() to process an incoming RC response
981 * packet for the given QP.
982 * Called at interrupt level.
984 static inline void ipath_rc_rcv_resp(struct ipath_ibdev *dev,
985 struct ipath_other_headers *ohdr,
986 void *data, u32 tlen,
989 u32 psn, u32 hdrsize, u32 pmtu,
998 spin_lock_irqsave(&qp->s_lock, flags);
1000 /* Ignore invalid responses. */
1001 if (ipath_cmp24(psn, qp->s_next_psn) >= 0)
1004 /* Ignore duplicate responses. */
1005 diff = ipath_cmp24(psn, qp->s_last_psn);
1006 if (unlikely(diff <= 0)) {
1007 /* Update credits for "ghost" ACKs */
1008 if (diff == 0 && opcode == OP(ACKNOWLEDGE)) {
1009 if (!header_in_data)
1010 aeth = be32_to_cpu(ohdr->u.aeth);
1012 aeth = be32_to_cpu(((__be32 *) data)[0]);
1013 data += sizeof(__be32);
1015 if ((aeth >> 29) == 0)
1016 ipath_get_credit(qp, aeth);
1022 case OP(ACKNOWLEDGE):
1023 case OP(ATOMIC_ACKNOWLEDGE):
1024 case OP(RDMA_READ_RESPONSE_FIRST):
1025 if (!header_in_data)
1026 aeth = be32_to_cpu(ohdr->u.aeth);
1028 aeth = be32_to_cpu(((__be32 *) data)[0]);
1029 data += sizeof(__be32);
1031 if (opcode == OP(ATOMIC_ACKNOWLEDGE))
1032 *(u64 *) qp->s_sge.sge.vaddr = *(u64 *) data;
1033 if (!do_rc_ack(qp, aeth, psn, opcode) ||
1034 opcode != OP(RDMA_READ_RESPONSE_FIRST))
1038 * do_rc_ack() has already checked the PSN so skip
1039 * the sequence check.
1043 case OP(RDMA_READ_RESPONSE_MIDDLE):
1044 /* no AETH, no ACK */
1045 if (unlikely(ipath_cmp24(psn, qp->s_last_psn + 1))) {
1047 ipath_restart_rc(qp, qp->s_last_psn + 1, &wc);
1051 if (unlikely(qp->s_state != OP(RDMA_READ_REQUEST)))
1053 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1055 if (unlikely(pmtu >= qp->s_len))
1057 /* We got a response so update the timeout. */
1058 if (unlikely(qp->s_last == qp->s_tail ||
1059 get_swqe_ptr(qp, qp->s_last)->wr.opcode !=
1062 spin_lock(&dev->pending_lock);
1063 if (qp->s_rnr_timeout == 0 && !list_empty(&qp->timerwait))
1064 list_move_tail(&qp->timerwait,
1065 &dev->pending[dev->pending_index]);
1066 spin_unlock(&dev->pending_lock);
1068 * Update the RDMA receive state but do the copy w/o
1069 * holding the locks and blocking interrupts.
1070 * XXX Yet another place that affects relaxed RDMA order
1071 * since we don't want s_sge modified.
1074 qp->s_last_psn = psn;
1075 spin_unlock_irqrestore(&qp->s_lock, flags);
1076 ipath_copy_sge(&qp->s_sge, data, pmtu);
1079 case OP(RDMA_READ_RESPONSE_LAST):
1080 /* ACKs READ req. */
1081 if (unlikely(ipath_cmp24(psn, qp->s_last_psn + 1))) {
1083 ipath_restart_rc(qp, qp->s_last_psn + 1, &wc);
1087 case OP(RDMA_READ_RESPONSE_ONLY):
1088 if (unlikely(qp->s_state != OP(RDMA_READ_REQUEST)))
1091 * Get the number of bytes the message was padded by.
1093 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1095 * Check that the data size is >= 1 && <= pmtu.
1096 * Remember to account for the AETH header (4) and
1099 if (unlikely(tlen <= (hdrsize + pad + 8))) {
1100 /* XXX Need to generate an error CQ entry. */
1103 tlen -= hdrsize + pad + 8;
1104 if (unlikely(tlen != qp->s_len)) {
1105 /* XXX Need to generate an error CQ entry. */
1108 if (!header_in_data)
1109 aeth = be32_to_cpu(ohdr->u.aeth);
1111 aeth = be32_to_cpu(((__be32 *) data)[0]);
1112 data += sizeof(__be32);
1114 ipath_copy_sge(&qp->s_sge, data, tlen);
1115 if (do_rc_ack(qp, aeth, psn, OP(RDMA_READ_RESPONSE_LAST))) {
1117 * Change the state so we contimue
1118 * processing new requests and wake up the
1119 * tasklet if there are posted sends.
1121 qp->s_state = OP(SEND_LAST);
1122 if (qp->s_tail != qp->s_head)
1123 tasklet_hi_schedule(&qp->s_task);
1129 spin_unlock_irqrestore(&qp->s_lock, flags);
1135 * ipath_rc_rcv_error - process an incoming duplicate or error RC packet
1136 * @dev: the device this packet came in on
1137 * @ohdr: the other headers for this packet
1138 * @data: the packet data
1139 * @qp: the QP for this packet
1140 * @opcode: the opcode for this packet
1141 * @psn: the packet sequence number for this packet
1142 * @diff: the difference between the PSN and the expected PSN
1143 * @header_in_data: true if part of the header data is in the data buffer
1145 * This is called from ipath_rc_rcv() to process an unexpected
1146 * incoming RC packet for the given QP.
1147 * Called at interrupt level.
1148 * Return 1 if no more processing is needed; otherwise return 0 to
1149 * schedule a response to be sent and the s_lock unlocked.
1151 static inline int ipath_rc_rcv_error(struct ipath_ibdev *dev,
1152 struct ipath_other_headers *ohdr,
1154 struct ipath_qp *qp,
1160 struct ib_reth *reth;
1164 * Packet sequence error.
1165 * A NAK will ACK earlier sends and RDMA writes.
1166 * Don't queue the NAK if a RDMA read, atomic, or
1167 * NAK is pending though.
1169 if (qp->s_ack_state != OP(ACKNOWLEDGE) ||
1170 qp->r_nak_state != 0)
1172 if (qp->r_ack_state < OP(COMPARE_SWAP)) {
1173 qp->r_ack_state = OP(SEND_ONLY);
1174 qp->r_nak_state = IB_NAK_PSN_ERROR;
1175 /* Use the expected PSN. */
1176 qp->r_ack_psn = qp->r_psn;
1182 * Handle a duplicate request. Don't re-execute SEND, RDMA
1183 * write or atomic op. Don't NAK errors, just silently drop
1184 * the duplicate request. Note that r_sge, r_len, and
1185 * r_rcv_len may be in use so don't modify them.
1187 * We are supposed to ACK the earliest duplicate PSN but we
1188 * can coalesce an outstanding duplicate ACK. We have to
1189 * send the earliest so that RDMA reads can be restarted at
1190 * the requester's expected PSN.
1192 if (opcode == OP(RDMA_READ_REQUEST)) {
1193 /* RETH comes after BTH */
1194 if (!header_in_data)
1195 reth = &ohdr->u.rc.reth;
1197 reth = (struct ib_reth *)data;
1198 data += sizeof(*reth);
1201 * If we receive a duplicate RDMA request, it means the
1202 * requester saw a sequence error and needs to restart
1203 * from an earlier point. We can abort the current
1204 * RDMA read send in that case.
1206 spin_lock_irq(&qp->s_lock);
1207 if (qp->s_ack_state != OP(ACKNOWLEDGE) &&
1208 (qp->s_hdrwords || ipath_cmp24(psn, qp->s_ack_psn) >= 0)) {
1210 * We are already sending earlier requested data.
1211 * Don't abort it to send later out of sequence data.
1213 spin_unlock_irq(&qp->s_lock);
1216 qp->s_rdma_len = be32_to_cpu(reth->length);
1217 if (qp->s_rdma_len != 0) {
1218 u32 rkey = be32_to_cpu(reth->rkey);
1219 u64 vaddr = be64_to_cpu(reth->vaddr);
1223 * Address range must be a subset of the original
1224 * request and start on pmtu boundaries.
1226 ok = ipath_rkey_ok(dev, &qp->s_rdma_sge,
1227 qp->s_rdma_len, vaddr, rkey,
1228 IB_ACCESS_REMOTE_READ);
1229 if (unlikely(!ok)) {
1230 spin_unlock_irq(&qp->s_lock);
1234 qp->s_rdma_sge.sg_list = NULL;
1235 qp->s_rdma_sge.num_sge = 0;
1236 qp->s_rdma_sge.sge.mr = NULL;
1237 qp->s_rdma_sge.sge.vaddr = NULL;
1238 qp->s_rdma_sge.sge.length = 0;
1239 qp->s_rdma_sge.sge.sge_length = 0;
1241 qp->s_ack_state = opcode;
1242 qp->s_ack_psn = psn;
1243 spin_unlock_irq(&qp->s_lock);
1244 tasklet_hi_schedule(&qp->s_task);
1249 * A pending RDMA read will ACK anything before it so
1250 * ignore earlier duplicate requests.
1252 if (qp->s_ack_state != OP(ACKNOWLEDGE))
1256 * If an ACK is pending, don't replace the pending ACK
1257 * with an earlier one since the later one will ACK the earlier.
1258 * Also, if we already have a pending atomic, send it.
1260 if (qp->r_ack_state != OP(ACKNOWLEDGE) &&
1261 (ipath_cmp24(psn, qp->r_ack_psn) <= 0 ||
1262 qp->r_ack_state >= OP(COMPARE_SWAP)))
1265 case OP(COMPARE_SWAP):
1268 * Check for the PSN of the last atomic operation
1269 * performed and resend the result if found.
1271 if ((psn & IPATH_PSN_MASK) != qp->r_atomic_psn)
1275 qp->r_ack_state = opcode;
1276 qp->r_nak_state = 0;
1277 qp->r_ack_psn = psn;
1286 * ipath_rc_rcv - process an incoming RC packet
1287 * @dev: the device this packet came in on
1288 * @hdr: the header of this packet
1289 * @has_grh: true if the header has a GRH
1290 * @data: the packet data
1291 * @tlen: the packet length
1292 * @qp: the QP for this packet
1294 * This is called from ipath_qp_rcv() to process an incoming RC packet
1296 * Called at interrupt level.
1298 void ipath_rc_rcv(struct ipath_ibdev *dev, struct ipath_ib_header *hdr,
1299 int has_grh, void *data, u32 tlen, struct ipath_qp *qp)
1301 struct ipath_other_headers *ohdr;
1307 u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
1309 struct ib_reth *reth;
1315 hdrsize = 8 + 12; /* LRH + BTH */
1316 psn = be32_to_cpu(ohdr->bth[2]);
1319 ohdr = &hdr->u.l.oth;
1320 hdrsize = 8 + 40 + 12; /* LRH + GRH + BTH */
1322 * The header with GRH is 60 bytes and the core driver sets
1323 * the eager header buffer size to 56 bytes so the last 4
1324 * bytes of the BTH header (PSN) is in the data buffer.
1327 ipath_layer_get_rcvhdrentsize(dev->dd) == 16;
1328 if (header_in_data) {
1329 psn = be32_to_cpu(((__be32 *) data)[0]);
1330 data += sizeof(__be32);
1332 psn = be32_to_cpu(ohdr->bth[2]);
1336 * Process responses (ACKs) before anything else. Note that the
1337 * packet sequence number will be for something in the send work
1338 * queue rather than the expected receive packet sequence number.
1339 * In other words, this QP is the requester.
1341 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
1342 if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
1343 opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
1344 ipath_rc_rcv_resp(dev, ohdr, data, tlen, qp, opcode, psn,
1345 hdrsize, pmtu, header_in_data);
1349 /* Compute 24 bits worth of difference. */
1350 diff = ipath_cmp24(psn, qp->r_psn);
1351 if (unlikely(diff)) {
1352 if (ipath_rc_rcv_error(dev, ohdr, data, qp, opcode,
1353 psn, diff, header_in_data))
1358 /* Check for opcode sequence errors. */
1359 switch (qp->r_state) {
1360 case OP(SEND_FIRST):
1361 case OP(SEND_MIDDLE):
1362 if (opcode == OP(SEND_MIDDLE) ||
1363 opcode == OP(SEND_LAST) ||
1364 opcode == OP(SEND_LAST_WITH_IMMEDIATE))
1368 * A NAK will ACK earlier sends and RDMA writes.
1369 * Don't queue the NAK if a RDMA read, atomic, or NAK
1370 * is pending though.
1372 if (qp->r_ack_state >= OP(COMPARE_SWAP))
1374 /* XXX Flush WQEs */
1375 qp->state = IB_QPS_ERR;
1376 qp->r_ack_state = OP(SEND_ONLY);
1377 qp->r_nak_state = IB_NAK_INVALID_REQUEST;
1378 qp->r_ack_psn = qp->r_psn;
1381 case OP(RDMA_WRITE_FIRST):
1382 case OP(RDMA_WRITE_MIDDLE):
1383 if (opcode == OP(RDMA_WRITE_MIDDLE) ||
1384 opcode == OP(RDMA_WRITE_LAST) ||
1385 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
1390 if (opcode == OP(SEND_MIDDLE) ||
1391 opcode == OP(SEND_LAST) ||
1392 opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
1393 opcode == OP(RDMA_WRITE_MIDDLE) ||
1394 opcode == OP(RDMA_WRITE_LAST) ||
1395 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
1398 * Note that it is up to the requester to not send a new
1399 * RDMA read or atomic operation before receiving an ACK
1400 * for the previous operation.
1408 /* OK, process the packet. */
1410 case OP(SEND_FIRST):
1411 if (!ipath_get_rwqe(qp, 0)) {
1414 * A RNR NAK will ACK earlier sends and RDMA writes.
1415 * Don't queue the NAK if a RDMA read or atomic
1416 * is pending though.
1418 if (qp->r_ack_state >= OP(COMPARE_SWAP))
1420 qp->r_ack_state = OP(SEND_ONLY);
1421 qp->r_nak_state = IB_RNR_NAK | qp->r_min_rnr_timer;
1422 qp->r_ack_psn = qp->r_psn;
1427 case OP(SEND_MIDDLE):
1428 case OP(RDMA_WRITE_MIDDLE):
1430 /* Check for invalid length PMTU or posted rwqe len. */
1431 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1433 qp->r_rcv_len += pmtu;
1434 if (unlikely(qp->r_rcv_len > qp->r_len))
1436 ipath_copy_sge(&qp->r_sge, data, pmtu);
1439 case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
1441 if (!ipath_get_rwqe(qp, 1))
1446 case OP(SEND_ONLY_WITH_IMMEDIATE):
1447 if (!ipath_get_rwqe(qp, 0))
1450 if (opcode == OP(SEND_ONLY))
1453 case OP(SEND_LAST_WITH_IMMEDIATE):
1455 if (header_in_data) {
1456 wc.imm_data = *(__be32 *) data;
1457 data += sizeof(__be32);
1459 /* Immediate data comes after BTH */
1460 wc.imm_data = ohdr->u.imm_data;
1463 wc.wc_flags = IB_WC_WITH_IMM;
1466 case OP(RDMA_WRITE_LAST):
1468 /* Get the number of bytes the message was padded by. */
1469 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1470 /* Check for invalid length. */
1471 /* XXX LAST len should be >= 1 */
1472 if (unlikely(tlen < (hdrsize + pad + 4)))
1474 /* Don't count the CRC. */
1475 tlen -= (hdrsize + pad + 4);
1476 wc.byte_len = tlen + qp->r_rcv_len;
1477 if (unlikely(wc.byte_len > qp->r_len))
1479 ipath_copy_sge(&qp->r_sge, data, tlen);
1481 if (opcode == OP(RDMA_WRITE_LAST) ||
1482 opcode == OP(RDMA_WRITE_ONLY))
1484 wc.wr_id = qp->r_wr_id;
1485 wc.status = IB_WC_SUCCESS;
1486 wc.opcode = IB_WC_RECV;
1488 wc.qp_num = qp->ibqp.qp_num;
1489 wc.src_qp = qp->remote_qpn;
1491 wc.slid = qp->remote_ah_attr.dlid;
1492 wc.sl = qp->remote_ah_attr.sl;
1493 wc.dlid_path_bits = 0;
1495 /* Signal completion event if the solicited bit is set. */
1496 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
1498 __constant_cpu_to_be32(1 << 23)) != 0);
1501 case OP(RDMA_WRITE_FIRST):
1502 case OP(RDMA_WRITE_ONLY):
1503 case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
1505 /* RETH comes after BTH */
1506 if (!header_in_data)
1507 reth = &ohdr->u.rc.reth;
1509 reth = (struct ib_reth *)data;
1510 data += sizeof(*reth);
1512 hdrsize += sizeof(*reth);
1513 qp->r_len = be32_to_cpu(reth->length);
1515 if (qp->r_len != 0) {
1516 u32 rkey = be32_to_cpu(reth->rkey);
1517 u64 vaddr = be64_to_cpu(reth->vaddr);
1520 /* Check rkey & NAK */
1521 ok = ipath_rkey_ok(dev, &qp->r_sge,
1522 qp->r_len, vaddr, rkey,
1523 IB_ACCESS_REMOTE_WRITE);
1527 qp->r_sge.sg_list = NULL;
1528 qp->r_sge.sge.mr = NULL;
1529 qp->r_sge.sge.vaddr = NULL;
1530 qp->r_sge.sge.length = 0;
1531 qp->r_sge.sge.sge_length = 0;
1533 if (unlikely(!(qp->qp_access_flags &
1534 IB_ACCESS_REMOTE_WRITE)))
1536 if (opcode == OP(RDMA_WRITE_FIRST))
1538 else if (opcode == OP(RDMA_WRITE_ONLY))
1540 if (!ipath_get_rwqe(qp, 1))
1544 case OP(RDMA_READ_REQUEST):
1545 /* RETH comes after BTH */
1546 if (!header_in_data)
1547 reth = &ohdr->u.rc.reth;
1549 reth = (struct ib_reth *)data;
1550 data += sizeof(*reth);
1552 if (unlikely(!(qp->qp_access_flags &
1553 IB_ACCESS_REMOTE_READ)))
1555 spin_lock_irq(&qp->s_lock);
1556 qp->s_rdma_len = be32_to_cpu(reth->length);
1557 if (qp->s_rdma_len != 0) {
1558 u32 rkey = be32_to_cpu(reth->rkey);
1559 u64 vaddr = be64_to_cpu(reth->vaddr);
1562 /* Check rkey & NAK */
1563 ok = ipath_rkey_ok(dev, &qp->s_rdma_sge,
1564 qp->s_rdma_len, vaddr, rkey,
1565 IB_ACCESS_REMOTE_READ);
1566 if (unlikely(!ok)) {
1567 spin_unlock_irq(&qp->s_lock);
1571 * Update the next expected PSN. We add 1 later
1572 * below, so only add the remainder here.
1574 if (qp->s_rdma_len > pmtu)
1575 qp->r_psn += (qp->s_rdma_len - 1) / pmtu;
1577 qp->s_rdma_sge.sg_list = NULL;
1578 qp->s_rdma_sge.num_sge = 0;
1579 qp->s_rdma_sge.sge.mr = NULL;
1580 qp->s_rdma_sge.sge.vaddr = NULL;
1581 qp->s_rdma_sge.sge.length = 0;
1582 qp->s_rdma_sge.sge.sge_length = 0;
1585 * We need to increment the MSN here instead of when we
1586 * finish sending the result since a duplicate request would
1587 * increment it more than once.
1591 qp->s_ack_state = opcode;
1592 qp->s_ack_psn = psn;
1593 spin_unlock_irq(&qp->s_lock);
1596 qp->r_state = opcode;
1597 qp->r_nak_state = 0;
1599 /* Call ipath_do_rc_send() in another thread. */
1600 tasklet_hi_schedule(&qp->s_task);
1604 case OP(COMPARE_SWAP):
1605 case OP(FETCH_ADD): {
1606 struct ib_atomic_eth *ateth;
1611 if (!header_in_data)
1612 ateth = &ohdr->u.atomic_eth;
1614 ateth = (struct ib_atomic_eth *)data;
1615 data += sizeof(*ateth);
1617 vaddr = be64_to_cpu(ateth->vaddr);
1618 if (unlikely(vaddr & (sizeof(u64) - 1)))
1620 rkey = be32_to_cpu(ateth->rkey);
1621 /* Check rkey & NAK */
1622 if (unlikely(!ipath_rkey_ok(dev, &qp->r_sge,
1623 sizeof(u64), vaddr, rkey,
1624 IB_ACCESS_REMOTE_ATOMIC)))
1626 if (unlikely(!(qp->qp_access_flags &
1627 IB_ACCESS_REMOTE_ATOMIC)))
1629 /* Perform atomic OP and save result. */
1630 sdata = be64_to_cpu(ateth->swap_data);
1631 spin_lock_irq(&dev->pending_lock);
1632 qp->r_atomic_data = *(u64 *) qp->r_sge.sge.vaddr;
1633 if (opcode == OP(FETCH_ADD))
1634 *(u64 *) qp->r_sge.sge.vaddr =
1635 qp->r_atomic_data + sdata;
1636 else if (qp->r_atomic_data ==
1637 be64_to_cpu(ateth->compare_data))
1638 *(u64 *) qp->r_sge.sge.vaddr = sdata;
1639 spin_unlock_irq(&dev->pending_lock);
1641 qp->r_atomic_psn = psn & IPATH_PSN_MASK;
1647 /* Drop packet for unknown opcodes. */
1651 qp->r_state = opcode;
1652 qp->r_nak_state = 0;
1653 /* Send an ACK if requested or required. */
1654 if (psn & (1 << 31)) {
1656 * Coalesce ACKs unless there is a RDMA READ or
1659 if (qp->r_ack_state < OP(COMPARE_SWAP)) {
1660 qp->r_ack_state = opcode;
1661 qp->r_ack_psn = psn;
1669 * A NAK will ACK earlier sends and RDMA writes.
1670 * Don't queue the NAK if a RDMA read, atomic, or NAK
1671 * is pending though.
1673 if (qp->r_ack_state < OP(COMPARE_SWAP)) {
1674 /* XXX Flush WQEs */
1675 qp->state = IB_QPS_ERR;
1676 qp->r_ack_state = OP(RDMA_WRITE_ONLY);
1677 qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
1678 qp->r_ack_psn = qp->r_psn;
1681 /* Send ACK right away unless the send tasklet has a pending ACK. */
1682 if (qp->s_ack_state == OP(ACKNOWLEDGE))