blob: 46744ea2babdb336510b3772ed7dfd9fb6d8608c [file] [log] [blame]
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001/*
John Gregor87427da2007-06-11 10:21:14 -07002 * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08003 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34#include "ipath_verbs.h"
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -070035#include "ipath_kernel.h"
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -080036
37/* cut down ridiculously long IB macro names */
38#define OP(x) IB_OPCODE_RC_##x
39
Ralph Campbell3859e392007-03-15 14:44:51 -070040static u32 restart_sge(struct ipath_sge_state *ss, struct ipath_swqe *wqe,
41 u32 psn, u32 pmtu)
42{
43 u32 len;
44
45 len = ((psn - wqe->psn) & IPATH_PSN_MASK) * pmtu;
46 ss->sge = wqe->sg_list[0];
47 ss->sg_list = wqe->sg_list + 1;
48 ss->num_sge = wqe->wr.num_sge;
49 ipath_skip_sge(ss, len);
50 return wqe->length - len;
51}
52
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -080053/**
54 * ipath_init_restart- initialize the qp->s_sge after a restart
55 * @qp: the QP who's SGE we're restarting
56 * @wqe: the work queue to initialize the QP's SGE from
57 *
Bryan O'Sullivan12eef412006-07-01 04:36:10 -070058 * The QP s_lock should be held and interrupts disabled.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -080059 */
60static void ipath_init_restart(struct ipath_qp *qp, struct ipath_swqe *wqe)
61{
62 struct ipath_ibdev *dev;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -080063
Ralph Campbell3859e392007-03-15 14:44:51 -070064 qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn,
65 ib_mtu_enum_to_int(qp->path_mtu));
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -080066 dev = to_idev(qp->ibqp.device);
67 spin_lock(&dev->pending_lock);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -070068 if (list_empty(&qp->timerwait))
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -080069 list_add_tail(&qp->timerwait,
70 &dev->pending[dev->pending_index]);
71 spin_unlock(&dev->pending_lock);
72}
73
74/**
75 * ipath_make_rc_ack - construct a response packet (ACK, NAK, or RDMA read)
76 * @qp: a pointer to the QP
77 * @ohdr: a pointer to the IB header being constructed
78 * @pmtu: the path MTU
79 *
Ralph Campbell3859e392007-03-15 14:44:51 -070080 * Return 1 if constructed; otherwise, return 0.
81 * Note that we are in the responder's side of the QP context.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -080082 * Note the QP s_lock must be held.
83 */
Ralph Campbell3859e392007-03-15 14:44:51 -070084static int ipath_make_rc_ack(struct ipath_qp *qp,
85 struct ipath_other_headers *ohdr,
86 u32 pmtu, u32 *bth0p, u32 *bth2p)
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -080087{
Ralph Campbell3859e392007-03-15 14:44:51 -070088 struct ipath_ack_entry *e;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -080089 u32 hwords;
90 u32 len;
91 u32 bth0;
Ralph Campbell3859e392007-03-15 14:44:51 -070092 u32 bth2;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -080093
94 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
95 hwords = 5;
96
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -080097 switch (qp->s_ack_state) {
Ralph Campbell3859e392007-03-15 14:44:51 -070098 case OP(RDMA_READ_RESPONSE_LAST):
99 case OP(RDMA_READ_RESPONSE_ONLY):
100 case OP(ATOMIC_ACKNOWLEDGE):
Ralph Campbell154257f2007-05-03 12:43:03 -0700101 /*
102 * We can increment the tail pointer now that the last
103 * response has been sent instead of only being
104 * constructed.
105 */
106 if (++qp->s_tail_ack_queue > IPATH_MAX_RDMA_ATOMIC)
107 qp->s_tail_ack_queue = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -0700108 /* FALLTHROUGH */
Ralph Campbell154257f2007-05-03 12:43:03 -0700109 case OP(SEND_ONLY):
Ralph Campbell3859e392007-03-15 14:44:51 -0700110 case OP(ACKNOWLEDGE):
111 /* Check for no next entry in the queue. */
112 if (qp->r_head_ack_queue == qp->s_tail_ack_queue) {
113 if (qp->s_flags & IPATH_S_ACK_PENDING)
114 goto normal;
Ralph Campbell154257f2007-05-03 12:43:03 -0700115 qp->s_ack_state = OP(ACKNOWLEDGE);
Ralph Campbell3859e392007-03-15 14:44:51 -0700116 goto bail;
117 }
118
119 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
120 if (e->opcode == OP(RDMA_READ_REQUEST)) {
121 /* Copy SGE state in case we need to resend */
122 qp->s_ack_rdma_sge = e->rdma_sge;
123 qp->s_cur_sge = &qp->s_ack_rdma_sge;
124 len = e->rdma_sge.sge.sge_length;
125 if (len > pmtu) {
126 len = pmtu;
127 qp->s_ack_state = OP(RDMA_READ_RESPONSE_FIRST);
Ralph Campbelld781b122007-06-18 14:24:44 -0700128 } else {
Ralph Campbell3859e392007-03-15 14:44:51 -0700129 qp->s_ack_state = OP(RDMA_READ_RESPONSE_ONLY);
Ralph Campbelld781b122007-06-18 14:24:44 -0700130 e->sent = 1;
131 }
Ralph Campbell3859e392007-03-15 14:44:51 -0700132 ohdr->u.aeth = ipath_compute_aeth(qp);
133 hwords++;
134 qp->s_ack_rdma_psn = e->psn;
135 bth2 = qp->s_ack_rdma_psn++ & IPATH_PSN_MASK;
136 } else {
137 /* COMPARE_SWAP or FETCH_ADD */
138 qp->s_cur_sge = NULL;
139 len = 0;
140 qp->s_ack_state = OP(ATOMIC_ACKNOWLEDGE);
141 ohdr->u.at.aeth = ipath_compute_aeth(qp);
142 ohdr->u.at.atomic_ack_eth[0] =
143 cpu_to_be32(e->atomic_data >> 32);
144 ohdr->u.at.atomic_ack_eth[1] =
145 cpu_to_be32(e->atomic_data);
146 hwords += sizeof(ohdr->u.at) / sizeof(u32);
147 bth2 = e->psn;
Ralph Campbelld781b122007-06-18 14:24:44 -0700148 e->sent = 1;
Ralph Campbell3859e392007-03-15 14:44:51 -0700149 }
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800150 bth0 = qp->s_ack_state << 24;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800151 break;
152
153 case OP(RDMA_READ_RESPONSE_FIRST):
154 qp->s_ack_state = OP(RDMA_READ_RESPONSE_MIDDLE);
155 /* FALLTHROUGH */
156 case OP(RDMA_READ_RESPONSE_MIDDLE):
Ralph Campbell3859e392007-03-15 14:44:51 -0700157 len = qp->s_ack_rdma_sge.sge.sge_length;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800158 if (len > pmtu)
159 len = pmtu;
160 else {
161 ohdr->u.aeth = ipath_compute_aeth(qp);
162 hwords++;
163 qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
Ralph Campbelld781b122007-06-18 14:24:44 -0700164 qp->s_ack_queue[qp->s_tail_ack_queue].sent = 1;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800165 }
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800166 bth0 = qp->s_ack_state << 24;
Ralph Campbell3859e392007-03-15 14:44:51 -0700167 bth2 = qp->s_ack_rdma_psn++ & IPATH_PSN_MASK;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800168 break;
169
170 default:
Ralph Campbell3859e392007-03-15 14:44:51 -0700171 normal:
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700172 /*
Ralph Campbell3859e392007-03-15 14:44:51 -0700173 * Send a regular ACK.
174 * Set the s_ack_state so we wait until after sending
175 * the ACK before setting s_ack_state to ACKNOWLEDGE
176 * (see above).
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700177 */
Ralph Campbell154257f2007-05-03 12:43:03 -0700178 qp->s_ack_state = OP(SEND_ONLY);
Ralph Campbell3859e392007-03-15 14:44:51 -0700179 qp->s_flags &= ~IPATH_S_ACK_PENDING;
180 qp->s_cur_sge = NULL;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700181 if (qp->s_nak_state)
Ralph Campbell3859e392007-03-15 14:44:51 -0700182 ohdr->u.aeth =
183 cpu_to_be32((qp->r_msn & IPATH_MSN_MASK) |
184 (qp->s_nak_state <<
185 IPATH_AETH_CREDIT_SHIFT));
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700186 else
187 ohdr->u.aeth = ipath_compute_aeth(qp);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800188 hwords++;
Ralph Campbell3859e392007-03-15 14:44:51 -0700189 len = 0;
190 bth0 = OP(ACKNOWLEDGE) << 24;
191 bth2 = qp->s_ack_psn & IPATH_PSN_MASK;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800192 }
193 qp->s_hdrwords = hwords;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800194 qp->s_cur_size = len;
Ralph Campbell1dd6a1b2007-06-18 14:24:38 -0700195 *bth0p = bth0 | (1 << 22); /* Set M bit */
Ralph Campbell3859e392007-03-15 14:44:51 -0700196 *bth2p = bth2;
197 return 1;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800198
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700199bail:
Ralph Campbell3859e392007-03-15 14:44:51 -0700200 return 0;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800201}
202
203/**
204 * ipath_make_rc_req - construct a request packet (SEND, RDMA r/w, ATOMIC)
205 * @qp: a pointer to the QP
206 * @ohdr: a pointer to the IB header being constructed
207 * @pmtu: the path MTU
208 * @bth0p: pointer to the BTH opcode word
209 * @bth2p: pointer to the BTH PSN word
210 *
211 * Return 1 if constructed; otherwise, return 0.
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700212 * Note the QP s_lock must be held and interrupts disabled.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800213 */
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700214int ipath_make_rc_req(struct ipath_qp *qp,
215 struct ipath_other_headers *ohdr,
216 u32 pmtu, u32 *bth0p, u32 *bth2p)
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800217{
218 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
219 struct ipath_sge_state *ss;
220 struct ipath_swqe *wqe;
221 u32 hwords;
222 u32 len;
223 u32 bth0;
224 u32 bth2;
225 char newreq;
226
Ralph Campbell3859e392007-03-15 14:44:51 -0700227 /* Sending responses has higher priority over sending requests. */
228 if ((qp->r_head_ack_queue != qp->s_tail_ack_queue ||
229 (qp->s_flags & IPATH_S_ACK_PENDING) ||
Ralph Campbell154257f2007-05-03 12:43:03 -0700230 qp->s_ack_state != OP(ACKNOWLEDGE)) &&
Ralph Campbell3859e392007-03-15 14:44:51 -0700231 ipath_make_rc_ack(qp, ohdr, pmtu, bth0p, bth2p))
232 goto done;
233
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800234 if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_SEND_OK) ||
Ralph Campbellc3af6642007-04-27 11:08:40 -0700235 qp->s_rnr_timeout || qp->s_wait_credit)
Ralph Campbell3859e392007-03-15 14:44:51 -0700236 goto bail;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800237
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700238 /* Limit the number of packets sent without an ACK. */
239 if (ipath_cmp24(qp->s_psn, qp->s_last_psn + IPATH_PSN_CREDIT) > 0) {
240 qp->s_wait_credit = 1;
241 dev->n_rc_stalls++;
Ralph Campbell3859e392007-03-15 14:44:51 -0700242 goto bail;
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700243 }
244
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800245 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
246 hwords = 5;
Ralph Campbell1dd6a1b2007-06-18 14:24:38 -0700247 bth0 = 1 << 22; /* Set M bit */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800248
249 /* Send a request. */
250 wqe = get_swqe_ptr(qp, qp->s_cur);
251 switch (qp->s_state) {
252 default:
253 /*
254 * Resend an old request or start a new one.
255 *
256 * We keep track of the current SWQE so that
257 * we don't reset the "furthest progress" state
258 * if we need to back up.
259 */
260 newreq = 0;
261 if (qp->s_cur == qp->s_tail) {
262 /* Check if send work queue is empty. */
263 if (qp->s_tail == qp->s_head)
Ralph Campbell3859e392007-03-15 14:44:51 -0700264 goto bail;
265 /*
266 * If a fence is requested, wait for previous
267 * RDMA read and atomic operations to finish.
268 */
269 if ((wqe->wr.send_flags & IB_SEND_FENCE) &&
270 qp->s_num_rd_atomic) {
271 qp->s_flags |= IPATH_S_FENCE_PENDING;
272 goto bail;
273 }
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700274 wqe->psn = qp->s_next_psn;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800275 newreq = 1;
276 }
277 /*
278 * Note that we have to be careful not to modify the
279 * original work request since we may need to resend
280 * it.
281 */
Ralph Campbell13b18c82006-09-29 14:37:51 -0700282 len = wqe->length;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800283 ss = &qp->s_sge;
284 bth2 = 0;
285 switch (wqe->wr.opcode) {
286 case IB_WR_SEND:
287 case IB_WR_SEND_WITH_IMM:
288 /* If no credit, return. */
289 if (qp->s_lsn != (u32) -1 &&
290 ipath_cmp24(wqe->ssn, qp->s_lsn + 1) > 0)
Ralph Campbell3859e392007-03-15 14:44:51 -0700291 goto bail;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800292 wqe->lpsn = wqe->psn;
293 if (len > pmtu) {
294 wqe->lpsn += (len - 1) / pmtu;
295 qp->s_state = OP(SEND_FIRST);
296 len = pmtu;
297 break;
298 }
299 if (wqe->wr.opcode == IB_WR_SEND)
300 qp->s_state = OP(SEND_ONLY);
301 else {
302 qp->s_state = OP(SEND_ONLY_WITH_IMMEDIATE);
303 /* Immediate data comes after the BTH */
304 ohdr->u.imm_data = wqe->wr.imm_data;
305 hwords += 1;
306 }
307 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
308 bth0 |= 1 << 23;
309 bth2 = 1 << 31; /* Request ACK. */
310 if (++qp->s_cur == qp->s_size)
311 qp->s_cur = 0;
312 break;
313
314 case IB_WR_RDMA_WRITE:
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700315 if (newreq && qp->s_lsn != (u32) -1)
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800316 qp->s_lsn++;
317 /* FALLTHROUGH */
318 case IB_WR_RDMA_WRITE_WITH_IMM:
319 /* If no credit, return. */
320 if (qp->s_lsn != (u32) -1 &&
321 ipath_cmp24(wqe->ssn, qp->s_lsn + 1) > 0)
Ralph Campbell3859e392007-03-15 14:44:51 -0700322 goto bail;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800323 ohdr->u.rc.reth.vaddr =
324 cpu_to_be64(wqe->wr.wr.rdma.remote_addr);
325 ohdr->u.rc.reth.rkey =
326 cpu_to_be32(wqe->wr.wr.rdma.rkey);
327 ohdr->u.rc.reth.length = cpu_to_be32(len);
Ralph Campbell3859e392007-03-15 14:44:51 -0700328 hwords += sizeof(struct ib_reth) / sizeof(u32);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800329 wqe->lpsn = wqe->psn;
330 if (len > pmtu) {
331 wqe->lpsn += (len - 1) / pmtu;
332 qp->s_state = OP(RDMA_WRITE_FIRST);
333 len = pmtu;
334 break;
335 }
336 if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
337 qp->s_state = OP(RDMA_WRITE_ONLY);
338 else {
339 qp->s_state =
340 OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700341 /* Immediate data comes after RETH */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800342 ohdr->u.rc.imm_data = wqe->wr.imm_data;
343 hwords += 1;
344 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
345 bth0 |= 1 << 23;
346 }
347 bth2 = 1 << 31; /* Request ACK. */
348 if (++qp->s_cur == qp->s_size)
349 qp->s_cur = 0;
350 break;
351
352 case IB_WR_RDMA_READ:
Ralph Campbell3859e392007-03-15 14:44:51 -0700353 /*
354 * Don't allow more operations to be started
355 * than the QP limits allow.
356 */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800357 if (newreq) {
Ralph Campbell3859e392007-03-15 14:44:51 -0700358 if (qp->s_num_rd_atomic >=
359 qp->s_max_rd_atomic) {
360 qp->s_flags |= IPATH_S_RDMAR_PENDING;
361 goto bail;
362 }
363 qp->s_num_rd_atomic++;
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700364 if (qp->s_lsn != (u32) -1)
365 qp->s_lsn++;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800366 /*
367 * Adjust s_next_psn to count the
368 * expected number of responses.
369 */
370 if (len > pmtu)
371 qp->s_next_psn += (len - 1) / pmtu;
372 wqe->lpsn = qp->s_next_psn++;
373 }
Ralph Campbell3859e392007-03-15 14:44:51 -0700374 ohdr->u.rc.reth.vaddr =
375 cpu_to_be64(wqe->wr.wr.rdma.remote_addr);
376 ohdr->u.rc.reth.rkey =
377 cpu_to_be32(wqe->wr.wr.rdma.rkey);
378 ohdr->u.rc.reth.length = cpu_to_be32(len);
379 qp->s_state = OP(RDMA_READ_REQUEST);
380 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800381 ss = NULL;
382 len = 0;
383 if (++qp->s_cur == qp->s_size)
384 qp->s_cur = 0;
385 break;
386
387 case IB_WR_ATOMIC_CMP_AND_SWP:
388 case IB_WR_ATOMIC_FETCH_AND_ADD:
Ralph Campbell3859e392007-03-15 14:44:51 -0700389 /*
390 * Don't allow more operations to be started
391 * than the QP limits allow.
392 */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800393 if (newreq) {
Ralph Campbell3859e392007-03-15 14:44:51 -0700394 if (qp->s_num_rd_atomic >=
395 qp->s_max_rd_atomic) {
396 qp->s_flags |= IPATH_S_RDMAR_PENDING;
397 goto bail;
398 }
399 qp->s_num_rd_atomic++;
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700400 if (qp->s_lsn != (u32) -1)
401 qp->s_lsn++;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800402 wqe->lpsn = wqe->psn;
403 }
Ralph Campbell3859e392007-03-15 14:44:51 -0700404 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
405 qp->s_state = OP(COMPARE_SWAP);
406 ohdr->u.atomic_eth.swap_data = cpu_to_be64(
407 wqe->wr.wr.atomic.swap);
408 ohdr->u.atomic_eth.compare_data = cpu_to_be64(
409 wqe->wr.wr.atomic.compare_add);
410 } else {
411 qp->s_state = OP(FETCH_ADD);
412 ohdr->u.atomic_eth.swap_data = cpu_to_be64(
413 wqe->wr.wr.atomic.compare_add);
414 ohdr->u.atomic_eth.compare_data = 0;
415 }
416 ohdr->u.atomic_eth.vaddr[0] = cpu_to_be32(
417 wqe->wr.wr.atomic.remote_addr >> 32);
418 ohdr->u.atomic_eth.vaddr[1] = cpu_to_be32(
419 wqe->wr.wr.atomic.remote_addr);
420 ohdr->u.atomic_eth.rkey = cpu_to_be32(
421 wqe->wr.wr.atomic.rkey);
422 hwords += sizeof(struct ib_atomic_eth) / sizeof(u32);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800423 ss = NULL;
424 len = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -0700425 if (++qp->s_cur == qp->s_size)
426 qp->s_cur = 0;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800427 break;
428
429 default:
Ralph Campbell3859e392007-03-15 14:44:51 -0700430 goto bail;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800431 }
Ralph Campbell13b18c82006-09-29 14:37:51 -0700432 qp->s_sge.sge = wqe->sg_list[0];
433 qp->s_sge.sg_list = wqe->sg_list + 1;
434 qp->s_sge.num_sge = wqe->wr.num_sge;
435 qp->s_len = wqe->length;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800436 if (newreq) {
437 qp->s_tail++;
438 if (qp->s_tail >= qp->s_size)
439 qp->s_tail = 0;
440 }
Ralph Campbell13b18c82006-09-29 14:37:51 -0700441 bth2 |= qp->s_psn & IPATH_PSN_MASK;
442 if (wqe->wr.opcode == IB_WR_RDMA_READ)
443 qp->s_psn = wqe->lpsn + 1;
444 else {
445 qp->s_psn++;
Ralph Campbell6f5c4072007-03-15 14:44:54 -0700446 if (ipath_cmp24(qp->s_psn, qp->s_next_psn) > 0)
Ralph Campbell13b18c82006-09-29 14:37:51 -0700447 qp->s_next_psn = qp->s_psn;
448 }
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700449 /*
450 * Put the QP on the pending list so lost ACKs will cause
451 * a retry. More than one request can be pending so the
452 * QP may already be on the dev->pending list.
453 */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800454 spin_lock(&dev->pending_lock);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700455 if (list_empty(&qp->timerwait))
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800456 list_add_tail(&qp->timerwait,
457 &dev->pending[dev->pending_index]);
458 spin_unlock(&dev->pending_lock);
459 break;
460
461 case OP(RDMA_READ_RESPONSE_FIRST):
462 /*
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700463 * This case can only happen if a send is restarted.
464 * See ipath_restart_rc().
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800465 */
466 ipath_init_restart(qp, wqe);
467 /* FALLTHROUGH */
468 case OP(SEND_FIRST):
469 qp->s_state = OP(SEND_MIDDLE);
470 /* FALLTHROUGH */
471 case OP(SEND_MIDDLE):
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700472 bth2 = qp->s_psn++ & IPATH_PSN_MASK;
Ralph Campbell6f5c4072007-03-15 14:44:54 -0700473 if (ipath_cmp24(qp->s_psn, qp->s_next_psn) > 0)
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800474 qp->s_next_psn = qp->s_psn;
475 ss = &qp->s_sge;
476 len = qp->s_len;
477 if (len > pmtu) {
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800478 len = pmtu;
479 break;
480 }
481 if (wqe->wr.opcode == IB_WR_SEND)
482 qp->s_state = OP(SEND_LAST);
483 else {
484 qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
485 /* Immediate data comes after the BTH */
486 ohdr->u.imm_data = wqe->wr.imm_data;
487 hwords += 1;
488 }
489 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
490 bth0 |= 1 << 23;
491 bth2 |= 1 << 31; /* Request ACK. */
492 qp->s_cur++;
493 if (qp->s_cur >= qp->s_size)
494 qp->s_cur = 0;
495 break;
496
497 case OP(RDMA_READ_RESPONSE_LAST):
498 /*
499 * This case can only happen if a RDMA write is restarted.
500 * See ipath_restart_rc().
501 */
502 ipath_init_restart(qp, wqe);
503 /* FALLTHROUGH */
504 case OP(RDMA_WRITE_FIRST):
505 qp->s_state = OP(RDMA_WRITE_MIDDLE);
506 /* FALLTHROUGH */
507 case OP(RDMA_WRITE_MIDDLE):
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700508 bth2 = qp->s_psn++ & IPATH_PSN_MASK;
Ralph Campbell6f5c4072007-03-15 14:44:54 -0700509 if (ipath_cmp24(qp->s_psn, qp->s_next_psn) > 0)
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800510 qp->s_next_psn = qp->s_psn;
511 ss = &qp->s_sge;
512 len = qp->s_len;
513 if (len > pmtu) {
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800514 len = pmtu;
515 break;
516 }
517 if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
518 qp->s_state = OP(RDMA_WRITE_LAST);
519 else {
520 qp->s_state = OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
521 /* Immediate data comes after the BTH */
522 ohdr->u.imm_data = wqe->wr.imm_data;
523 hwords += 1;
524 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
525 bth0 |= 1 << 23;
526 }
527 bth2 |= 1 << 31; /* Request ACK. */
528 qp->s_cur++;
529 if (qp->s_cur >= qp->s_size)
530 qp->s_cur = 0;
531 break;
532
533 case OP(RDMA_READ_RESPONSE_MIDDLE):
534 /*
535 * This case can only happen if a RDMA read is restarted.
536 * See ipath_restart_rc().
537 */
538 ipath_init_restart(qp, wqe);
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700539 len = ((qp->s_psn - wqe->psn) & IPATH_PSN_MASK) * pmtu;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800540 ohdr->u.rc.reth.vaddr =
541 cpu_to_be64(wqe->wr.wr.rdma.remote_addr + len);
542 ohdr->u.rc.reth.rkey =
543 cpu_to_be32(wqe->wr.wr.rdma.rkey);
544 ohdr->u.rc.reth.length = cpu_to_be32(qp->s_len);
545 qp->s_state = OP(RDMA_READ_REQUEST);
Ralph Campbell3859e392007-03-15 14:44:51 -0700546 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700547 bth2 = qp->s_psn++ & IPATH_PSN_MASK;
Ralph Campbell6f5c4072007-03-15 14:44:54 -0700548 if (ipath_cmp24(qp->s_psn, qp->s_next_psn) > 0)
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800549 qp->s_next_psn = qp->s_psn;
550 ss = NULL;
551 len = 0;
552 qp->s_cur++;
553 if (qp->s_cur == qp->s_size)
554 qp->s_cur = 0;
555 break;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800556 }
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700557 if (ipath_cmp24(qp->s_psn, qp->s_last_psn + IPATH_PSN_CREDIT - 1) >= 0)
558 bth2 |= 1 << 31; /* Request ACK. */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800559 qp->s_len -= len;
560 qp->s_hdrwords = hwords;
561 qp->s_cur_sge = ss;
562 qp->s_cur_size = len;
563 *bth0p = bth0 | (qp->s_state << 24);
564 *bth2p = bth2;
Ralph Campbell3859e392007-03-15 14:44:51 -0700565done:
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800566 return 1;
567
Ralph Campbell3859e392007-03-15 14:44:51 -0700568bail:
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800569 return 0;
570}
571
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800572/**
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700573 * send_rc_ack - Construct an ACK packet and send it
574 * @qp: a pointer to the QP
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800575 *
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700576 * This is called from ipath_rc_rcv() and only uses the receive
577 * side QP state.
Ralph Campbell3859e392007-03-15 14:44:51 -0700578 * Note that RDMA reads and atomics are handled in the
579 * send side QP state and tasklet.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800580 */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800581static void send_rc_ack(struct ipath_qp *qp)
582{
583 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
584 u16 lrh0;
585 u32 bth0;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700586 u32 hwords;
587 struct ipath_ib_header hdr;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800588 struct ipath_other_headers *ohdr;
Ralph Campbell35ff0322007-04-27 11:11:11 -0700589 unsigned long flags;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800590
Ralph Campbell3859e392007-03-15 14:44:51 -0700591 /* Don't send ACK or NAK if a RDMA read or atomic is pending. */
Ralph Campbell154257f2007-05-03 12:43:03 -0700592 if (qp->r_head_ack_queue != qp->s_tail_ack_queue ||
593 (qp->s_flags & IPATH_S_ACK_PENDING) ||
594 qp->s_ack_state != OP(ACKNOWLEDGE))
Ralph Campbell3859e392007-03-15 14:44:51 -0700595 goto queue_ack;
596
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800597 /* Construct the header. */
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700598 ohdr = &hdr.u.oth;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700599 lrh0 = IPATH_LRH_BTH;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800600 /* header size in 32-bit words LRH+BTH+AETH = (8+12+4)/4. */
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700601 hwords = 6;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800602 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700603 hwords += ipath_make_grh(dev, &hdr.u.l.grh,
604 &qp->remote_ah_attr.grh,
605 hwords, 0);
606 ohdr = &hdr.u.l.oth;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700607 lrh0 = IPATH_LRH_GRH;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800608 }
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700609 /* read pkey_index w/o lock (its atomic) */
Ralph Campbell3859e392007-03-15 14:44:51 -0700610 bth0 = ipath_get_pkey(dev->dd, qp->s_pkey_index) |
Ralph Campbell1dd6a1b2007-06-18 14:24:38 -0700611 (OP(ACKNOWLEDGE) << 24) | (1 << 22);
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700612 if (qp->r_nak_state)
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700613 ohdr->u.aeth = cpu_to_be32((qp->r_msn & IPATH_MSN_MASK) |
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700614 (qp->r_nak_state <<
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700615 IPATH_AETH_CREDIT_SHIFT));
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700616 else
617 ohdr->u.aeth = ipath_compute_aeth(qp);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800618 lrh0 |= qp->remote_ah_attr.sl << 4;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700619 hdr.lrh[0] = cpu_to_be16(lrh0);
620 hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
621 hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700622 hdr.lrh[3] = cpu_to_be16(dev->dd->ipath_lid);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800623 ohdr->bth[0] = cpu_to_be32(bth0);
624 ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700625 ohdr->bth[2] = cpu_to_be32(qp->r_ack_psn & IPATH_PSN_MASK);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800626
627 /*
628 * If we can send the ACK, clear the ACK state.
629 */
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700630 if (ipath_verbs_send(dev->dd, hwords, (u32 *) &hdr, 0, NULL) == 0) {
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800631 dev->n_unicast_xmit++;
Ralph Campbell3859e392007-03-15 14:44:51 -0700632 goto done;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800633 }
Ralph Campbell3859e392007-03-15 14:44:51 -0700634
635 /*
636 * We are out of PIO buffers at the moment.
637 * Pass responsibility for sending the ACK to the
638 * send tasklet so that when a PIO buffer becomes
639 * available, the ACK is sent ahead of other outgoing
640 * packets.
641 */
642 dev->n_rc_qacks++;
643
644queue_ack:
Ralph Campbell35ff0322007-04-27 11:11:11 -0700645 spin_lock_irqsave(&qp->s_lock, flags);
Ralph Campbell3859e392007-03-15 14:44:51 -0700646 qp->s_flags |= IPATH_S_ACK_PENDING;
647 qp->s_nak_state = qp->r_nak_state;
648 qp->s_ack_psn = qp->r_ack_psn;
Ralph Campbell35ff0322007-04-27 11:11:11 -0700649 spin_unlock_irqrestore(&qp->s_lock, flags);
Ralph Campbell3859e392007-03-15 14:44:51 -0700650
651 /* Call ipath_do_rc_send() in another thread. */
652 tasklet_hi_schedule(&qp->s_task);
653
654done:
655 return;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800656}
657
658/**
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700659 * reset_psn - reset the QP state to send starting from PSN
660 * @qp: the QP
661 * @psn: the packet sequence number to restart at
662 *
663 * This is called from ipath_rc_rcv() to process an incoming RC ACK
664 * for the given QP.
665 * Called at interrupt level with the QP s_lock held.
666 */
667static void reset_psn(struct ipath_qp *qp, u32 psn)
668{
669 u32 n = qp->s_last;
670 struct ipath_swqe *wqe = get_swqe_ptr(qp, n);
671 u32 opcode;
672
673 qp->s_cur = n;
674
675 /*
676 * If we are starting the request from the beginning,
677 * let the normal send code handle initialization.
678 */
679 if (ipath_cmp24(psn, wqe->psn) <= 0) {
680 qp->s_state = OP(SEND_LAST);
681 goto done;
682 }
683
684 /* Find the work request opcode corresponding to the given PSN. */
685 opcode = wqe->wr.opcode;
686 for (;;) {
687 int diff;
688
689 if (++n == qp->s_size)
690 n = 0;
691 if (n == qp->s_tail)
692 break;
693 wqe = get_swqe_ptr(qp, n);
694 diff = ipath_cmp24(psn, wqe->psn);
695 if (diff < 0)
696 break;
697 qp->s_cur = n;
698 /*
699 * If we are starting the request from the beginning,
700 * let the normal send code handle initialization.
701 */
702 if (diff == 0) {
703 qp->s_state = OP(SEND_LAST);
704 goto done;
705 }
706 opcode = wqe->wr.opcode;
707 }
708
709 /*
710 * Set the state to restart in the middle of a request.
711 * Don't change the s_sge, s_cur_sge, or s_cur_size.
712 * See ipath_do_rc_send().
713 */
714 switch (opcode) {
715 case IB_WR_SEND:
716 case IB_WR_SEND_WITH_IMM:
717 qp->s_state = OP(RDMA_READ_RESPONSE_FIRST);
718 break;
719
720 case IB_WR_RDMA_WRITE:
721 case IB_WR_RDMA_WRITE_WITH_IMM:
722 qp->s_state = OP(RDMA_READ_RESPONSE_LAST);
723 break;
724
725 case IB_WR_RDMA_READ:
726 qp->s_state = OP(RDMA_READ_RESPONSE_MIDDLE);
727 break;
728
729 default:
730 /*
731 * This case shouldn't happen since its only
732 * one PSN per req.
733 */
734 qp->s_state = OP(SEND_LAST);
735 }
736done:
737 qp->s_psn = psn;
738}
739
740/**
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800741 * ipath_restart_rc - back up requester to resend the last un-ACKed request
742 * @qp: the QP to restart
743 * @psn: packet sequence number for the request
744 * @wc: the work completion request
745 *
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700746 * The QP s_lock should be held and interrupts disabled.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800747 */
748void ipath_restart_rc(struct ipath_qp *qp, u32 psn, struct ib_wc *wc)
749{
750 struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
751 struct ipath_ibdev *dev;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800752
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800753 if (qp->s_retry == 0) {
754 wc->wr_id = wqe->wr.wr_id;
755 wc->status = IB_WC_RETRY_EXC_ERR;
756 wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
757 wc->vendor_err = 0;
758 wc->byte_len = 0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200759 wc->qp = &qp->ibqp;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800760 wc->src_qp = qp->remote_qpn;
761 wc->pkey_index = 0;
762 wc->slid = qp->remote_ah_attr.dlid;
763 wc->sl = qp->remote_ah_attr.sl;
764 wc->dlid_path_bits = 0;
765 wc->port_num = 0;
766 ipath_sqerror_qp(qp, wc);
767 goto bail;
768 }
769 qp->s_retry--;
770
771 /*
772 * Remove the QP from the timeout queue.
773 * Note: it may already have been removed by ipath_ib_timer().
774 */
775 dev = to_idev(qp->ibqp.device);
776 spin_lock(&dev->pending_lock);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700777 if (!list_empty(&qp->timerwait))
778 list_del_init(&qp->timerwait);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800779 spin_unlock(&dev->pending_lock);
780
781 if (wqe->wr.opcode == IB_WR_RDMA_READ)
782 dev->n_rc_resends++;
783 else
Ralph Campbell6f5c4072007-03-15 14:44:54 -0700784 dev->n_rc_resends += (qp->s_psn - psn) & IPATH_PSN_MASK;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800785
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700786 reset_psn(qp, psn);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800787 tasklet_hi_schedule(&qp->s_task);
788
789bail:
790 return;
791}
792
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700793static inline void update_last_psn(struct ipath_qp *qp, u32 psn)
794{
795 if (qp->s_wait_credit) {
796 qp->s_wait_credit = 0;
797 tasklet_hi_schedule(&qp->s_task);
798 }
799 qp->s_last_psn = psn;
800}
801
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800802/**
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800803 * do_rc_ack - process an incoming RC ACK
804 * @qp: the QP the ACK came in on
805 * @psn: the packet sequence number of the ACK
806 * @opcode: the opcode of the request that resulted in the ACK
807 *
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700808 * This is called from ipath_rc_rcv_resp() to process an incoming RC ACK
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800809 * for the given QP.
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700810 * Called at interrupt level with the QP s_lock held and interrupts disabled.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800811 * Returns 1 if OK, 0 if current operation should be aborted (NAK).
812 */
Ralph Campbell06ee1092007-06-18 14:24:42 -0700813static int do_rc_ack(struct ipath_qp *qp, u32 aeth, u32 psn, int opcode,
814 u64 val)
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800815{
816 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
817 struct ib_wc wc;
818 struct ipath_swqe *wqe;
819 int ret = 0;
Ralph Campbell13b18c82006-09-29 14:37:51 -0700820 u32 ack_psn;
Ralph Campbell06ee1092007-06-18 14:24:42 -0700821 int diff;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800822
823 /*
824 * Remove the QP from the timeout queue (or RNR timeout queue).
825 * If ipath_ib_timer() has already removed it,
826 * it's OK since we hold the QP s_lock and ipath_restart_rc()
827 * just won't find anything to restart if we ACK everything.
828 */
829 spin_lock(&dev->pending_lock);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700830 if (!list_empty(&qp->timerwait))
831 list_del_init(&qp->timerwait);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800832 spin_unlock(&dev->pending_lock);
833
834 /*
835 * Note that NAKs implicitly ACK outstanding SEND and RDMA write
836 * requests and implicitly NAK RDMA read and atomic requests issued
837 * before the NAK'ed request. The MSN won't include the NAK'ed
838 * request but will include an ACK'ed request(s).
839 */
Ralph Campbell13b18c82006-09-29 14:37:51 -0700840 ack_psn = psn;
841 if (aeth >> 29)
842 ack_psn--;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800843 wqe = get_swqe_ptr(qp, qp->s_last);
844
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800845 /*
846 * The MSN might be for a later WQE than the PSN indicates so
847 * only complete WQEs that the PSN finishes.
848 */
Ralph Campbell06ee1092007-06-18 14:24:42 -0700849 while ((diff = ipath_cmp24(ack_psn, wqe->lpsn)) >= 0) {
850 /*
851 * RDMA_READ_RESPONSE_ONLY is a special case since
852 * we want to generate completion events for everything
853 * before the RDMA read, copy the data, then generate
854 * the completion for the read.
855 */
856 if (wqe->wr.opcode == IB_WR_RDMA_READ &&
857 opcode == OP(RDMA_READ_RESPONSE_ONLY) &&
858 diff == 0) {
859 ret = 1;
860 goto bail;
861 }
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800862 /*
863 * If this request is a RDMA read or atomic, and the ACK is
864 * for a later operation, this ACK NAKs the RDMA read or
865 * atomic. In other words, only a RDMA_READ_LAST or ONLY
866 * can ACK a RDMA read and likewise for atomic ops. Note
867 * that the NAK case can only happen if relaxed ordering is
868 * used and requests are sent after an RDMA read or atomic
869 * is sent but before the response is received.
870 */
871 if ((wqe->wr.opcode == IB_WR_RDMA_READ &&
Ralph Campbell06ee1092007-06-18 14:24:42 -0700872 (opcode != OP(RDMA_READ_RESPONSE_LAST) || diff != 0)) ||
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800873 ((wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
874 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) &&
Ralph Campbell06ee1092007-06-18 14:24:42 -0700875 (opcode != OP(ATOMIC_ACKNOWLEDGE) || diff != 0))) {
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800876 /*
877 * The last valid PSN seen is the previous
878 * request's.
879 */
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700880 update_last_psn(qp, wqe->psn - 1);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800881 /* Retry this request. */
882 ipath_restart_rc(qp, wqe->psn, &wc);
883 /*
884 * No need to process the ACK/NAK since we are
885 * restarting an earlier request.
886 */
887 goto bail;
888 }
Ralph Campbell06ee1092007-06-18 14:24:42 -0700889 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
890 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
891 *(u64 *) wqe->sg_list[0].vaddr = val;
Ralph Campbell3859e392007-03-15 14:44:51 -0700892 if (qp->s_num_rd_atomic &&
893 (wqe->wr.opcode == IB_WR_RDMA_READ ||
894 wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
895 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)) {
896 qp->s_num_rd_atomic--;
897 /* Restart sending task if fence is complete */
898 if ((qp->s_flags & IPATH_S_FENCE_PENDING) &&
899 !qp->s_num_rd_atomic) {
900 qp->s_flags &= ~IPATH_S_FENCE_PENDING;
901 tasklet_hi_schedule(&qp->s_task);
902 } else if (qp->s_flags & IPATH_S_RDMAR_PENDING) {
903 qp->s_flags &= ~IPATH_S_RDMAR_PENDING;
904 tasklet_hi_schedule(&qp->s_task);
905 }
906 }
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800907 /* Post a send completion queue entry if requested. */
Ralph Campbell3859e392007-03-15 14:44:51 -0700908 if (!(qp->s_flags & IPATH_S_SIGNAL_REQ_WR) ||
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800909 (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
910 wc.wr_id = wqe->wr.wr_id;
911 wc.status = IB_WC_SUCCESS;
912 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
913 wc.vendor_err = 0;
914 wc.byte_len = wqe->length;
Ralph Campbell0434d272007-03-15 14:44:53 -0700915 wc.imm_data = 0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200916 wc.qp = &qp->ibqp;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800917 wc.src_qp = qp->remote_qpn;
Ralph Campbell0434d272007-03-15 14:44:53 -0700918 wc.wc_flags = 0;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800919 wc.pkey_index = 0;
920 wc.slid = qp->remote_ah_attr.dlid;
921 wc.sl = qp->remote_ah_attr.sl;
922 wc.dlid_path_bits = 0;
923 wc.port_num = 0;
924 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 0);
925 }
926 qp->s_retry = qp->s_retry_cnt;
927 /*
928 * If we are completing a request which is in the process of
929 * being resent, we can stop resending it since we know the
930 * responder has already seen it.
931 */
932 if (qp->s_last == qp->s_cur) {
933 if (++qp->s_cur >= qp->s_size)
934 qp->s_cur = 0;
Ralph Campbell6f5c4072007-03-15 14:44:54 -0700935 qp->s_last = qp->s_cur;
936 if (qp->s_last == qp->s_tail)
937 break;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800938 wqe = get_swqe_ptr(qp, qp->s_cur);
939 qp->s_state = OP(SEND_LAST);
940 qp->s_psn = wqe->psn;
Ralph Campbell6f5c4072007-03-15 14:44:54 -0700941 } else {
942 if (++qp->s_last >= qp->s_size)
943 qp->s_last = 0;
944 if (qp->s_last == qp->s_tail)
945 break;
946 wqe = get_swqe_ptr(qp, qp->s_last);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800947 }
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800948 }
949
950 switch (aeth >> 29) {
951 case 0: /* ACK */
952 dev->n_rc_acks++;
953 /* If this is a partial ACK, reset the retransmit timer. */
954 if (qp->s_last != qp->s_tail) {
955 spin_lock(&dev->pending_lock);
956 list_add_tail(&qp->timerwait,
957 &dev->pending[dev->pending_index]);
958 spin_unlock(&dev->pending_lock);
Ralph Campbell6f5c4072007-03-15 14:44:54 -0700959 /*
960 * If we get a partial ACK for a resent operation,
961 * we can stop resending the earlier packets and
962 * continue with the next packet the receiver wants.
963 */
964 if (ipath_cmp24(qp->s_psn, psn) <= 0) {
965 reset_psn(qp, psn + 1);
966 tasklet_hi_schedule(&qp->s_task);
967 }
968 } else if (ipath_cmp24(qp->s_psn, psn) <= 0) {
969 qp->s_state = OP(SEND_LAST);
970 qp->s_psn = psn + 1;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800971 }
972 ipath_get_credit(qp, aeth);
973 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
974 qp->s_retry = qp->s_retry_cnt;
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700975 update_last_psn(qp, psn);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800976 ret = 1;
977 goto bail;
978
979 case 1: /* RNR NAK */
980 dev->n_rnr_naks++;
Ralph Campbell6f5c4072007-03-15 14:44:54 -0700981 if (qp->s_last == qp->s_tail)
982 goto bail;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800983 if (qp->s_rnr_retry == 0) {
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800984 wc.status = IB_WC_RNR_RETRY_EXC_ERR;
985 goto class_b;
986 }
987 if (qp->s_rnr_retry_cnt < 7)
988 qp->s_rnr_retry--;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800989
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700990 /* The last valid PSN is the previous PSN. */
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700991 update_last_psn(qp, psn - 1);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800992
Ralph Campbell6f5c4072007-03-15 14:44:54 -0700993 if (wqe->wr.opcode == IB_WR_RDMA_READ)
994 dev->n_rc_resends++;
995 else
996 dev->n_rc_resends +=
997 (qp->s_psn - psn) & IPATH_PSN_MASK;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800998
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700999 reset_psn(qp, psn);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001000
1001 qp->s_rnr_timeout =
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -07001002 ib_ipath_rnr_table[(aeth >> IPATH_AETH_CREDIT_SHIFT) &
1003 IPATH_AETH_CREDIT_MASK];
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001004 ipath_insert_rnr_queue(qp);
1005 goto bail;
1006
1007 case 3: /* NAK */
Ralph Campbell6f5c4072007-03-15 14:44:54 -07001008 if (qp->s_last == qp->s_tail)
1009 goto bail;
1010 /* The last valid PSN is the previous PSN. */
1011 update_last_psn(qp, psn - 1);
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -07001012 switch ((aeth >> IPATH_AETH_CREDIT_SHIFT) &
1013 IPATH_AETH_CREDIT_MASK) {
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001014 case 0: /* PSN sequence error */
1015 dev->n_seq_naks++;
1016 /*
Ralph Campbell6f5c4072007-03-15 14:44:54 -07001017 * Back up to the responder's expected PSN.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001018 * Note that we might get a NAK in the middle of an
1019 * RDMA READ response which terminates the RDMA
1020 * READ.
1021 */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001022 ipath_restart_rc(qp, psn, &wc);
1023 break;
1024
1025 case 1: /* Invalid Request */
1026 wc.status = IB_WC_REM_INV_REQ_ERR;
1027 dev->n_other_naks++;
1028 goto class_b;
1029
1030 case 2: /* Remote Access Error */
1031 wc.status = IB_WC_REM_ACCESS_ERR;
1032 dev->n_other_naks++;
1033 goto class_b;
1034
1035 case 3: /* Remote Operation Error */
1036 wc.status = IB_WC_REM_OP_ERR;
1037 dev->n_other_naks++;
1038 class_b:
1039 wc.wr_id = wqe->wr.wr_id;
1040 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
1041 wc.vendor_err = 0;
1042 wc.byte_len = 0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +02001043 wc.qp = &qp->ibqp;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001044 wc.src_qp = qp->remote_qpn;
1045 wc.pkey_index = 0;
1046 wc.slid = qp->remote_ah_attr.dlid;
1047 wc.sl = qp->remote_ah_attr.sl;
1048 wc.dlid_path_bits = 0;
1049 wc.port_num = 0;
1050 ipath_sqerror_qp(qp, &wc);
1051 break;
1052
1053 default:
1054 /* Ignore other reserved NAK error codes */
1055 goto reserved;
1056 }
1057 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1058 goto bail;
1059
1060 default: /* 2: reserved */
1061 reserved:
1062 /* Ignore reserved NAK codes. */
1063 goto bail;
1064 }
1065
1066bail:
1067 return ret;
1068}
1069
1070/**
1071 * ipath_rc_rcv_resp - process an incoming RC response packet
1072 * @dev: the device this packet came in on
1073 * @ohdr: the other headers for this packet
1074 * @data: the packet data
1075 * @tlen: the packet length
1076 * @qp: the QP for this packet
1077 * @opcode: the opcode for this packet
1078 * @psn: the packet sequence number for this packet
1079 * @hdrsize: the header length
1080 * @pmtu: the path MTU
1081 * @header_in_data: true if part of the header data is in the data buffer
1082 *
1083 * This is called from ipath_rc_rcv() to process an incoming RC response
1084 * packet for the given QP.
1085 * Called at interrupt level.
1086 */
1087static inline void ipath_rc_rcv_resp(struct ipath_ibdev *dev,
1088 struct ipath_other_headers *ohdr,
1089 void *data, u32 tlen,
1090 struct ipath_qp *qp,
1091 u32 opcode,
1092 u32 psn, u32 hdrsize, u32 pmtu,
1093 int header_in_data)
1094{
Ralph Campbell3859e392007-03-15 14:44:51 -07001095 struct ipath_swqe *wqe;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001096 unsigned long flags;
1097 struct ib_wc wc;
1098 int diff;
1099 u32 pad;
1100 u32 aeth;
Ralph Campbell06ee1092007-06-18 14:24:42 -07001101 u64 val;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001102
1103 spin_lock_irqsave(&qp->s_lock, flags);
1104
1105 /* Ignore invalid responses. */
1106 if (ipath_cmp24(psn, qp->s_next_psn) >= 0)
1107 goto ack_done;
1108
1109 /* Ignore duplicate responses. */
1110 diff = ipath_cmp24(psn, qp->s_last_psn);
1111 if (unlikely(diff <= 0)) {
1112 /* Update credits for "ghost" ACKs */
1113 if (diff == 0 && opcode == OP(ACKNOWLEDGE)) {
1114 if (!header_in_data)
1115 aeth = be32_to_cpu(ohdr->u.aeth);
1116 else {
1117 aeth = be32_to_cpu(((__be32 *) data)[0]);
1118 data += sizeof(__be32);
1119 }
1120 if ((aeth >> 29) == 0)
1121 ipath_get_credit(qp, aeth);
1122 }
1123 goto ack_done;
1124 }
1125
Ralph Campbell3859e392007-03-15 14:44:51 -07001126 if (unlikely(qp->s_last == qp->s_tail))
1127 goto ack_done;
1128 wqe = get_swqe_ptr(qp, qp->s_last);
1129
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001130 switch (opcode) {
1131 case OP(ACKNOWLEDGE):
1132 case OP(ATOMIC_ACKNOWLEDGE):
1133 case OP(RDMA_READ_RESPONSE_FIRST):
1134 if (!header_in_data)
1135 aeth = be32_to_cpu(ohdr->u.aeth);
1136 else {
1137 aeth = be32_to_cpu(((__be32 *) data)[0]);
1138 data += sizeof(__be32);
1139 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001140 if (opcode == OP(ATOMIC_ACKNOWLEDGE)) {
Ralph Campbell3859e392007-03-15 14:44:51 -07001141 if (!header_in_data) {
1142 __be32 *p = ohdr->u.at.atomic_ack_eth;
1143
1144 val = ((u64) be32_to_cpu(p[0]) << 32) |
1145 be32_to_cpu(p[1]);
1146 } else
1147 val = be64_to_cpu(((__be64 *) data)[0]);
Ralph Campbell06ee1092007-06-18 14:24:42 -07001148 } else
1149 val = 0;
1150 if (!do_rc_ack(qp, aeth, psn, opcode, val) ||
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001151 opcode != OP(RDMA_READ_RESPONSE_FIRST))
1152 goto ack_done;
1153 hdrsize += 4;
Ralph Campbell06ee1092007-06-18 14:24:42 -07001154 wqe = get_swqe_ptr(qp, qp->s_last);
Ralph Campbell3859e392007-03-15 14:44:51 -07001155 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
Ralph Campbelldd5190b2007-03-15 14:45:00 -07001156 goto ack_op_err;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001157 /*
Ralph Campbell3859e392007-03-15 14:44:51 -07001158 * If this is a response to a resent RDMA read, we
1159 * have to be careful to copy the data to the right
1160 * location.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001161 */
Ralph Campbell3859e392007-03-15 14:44:51 -07001162 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1163 wqe, psn, pmtu);
1164 goto read_middle;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001165
1166 case OP(RDMA_READ_RESPONSE_MIDDLE):
1167 /* no AETH, no ACK */
1168 if (unlikely(ipath_cmp24(psn, qp->s_last_psn + 1))) {
1169 dev->n_rdma_seq++;
Ralph Campbell3859e392007-03-15 14:44:51 -07001170 ipath_restart_rc(qp, qp->s_last_psn + 1, &wc);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001171 goto ack_done;
1172 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001173 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
Ralph Campbelldd5190b2007-03-15 14:45:00 -07001174 goto ack_op_err;
Ralph Campbell3859e392007-03-15 14:44:51 -07001175 read_middle:
Bryan O'Sullivan7bbb15e2006-07-01 04:35:51 -07001176 if (unlikely(tlen != (hdrsize + pmtu + 4)))
Ralph Campbelldd5190b2007-03-15 14:45:00 -07001177 goto ack_len_err;
Ralph Campbell3859e392007-03-15 14:44:51 -07001178 if (unlikely(pmtu >= qp->s_rdma_read_len))
Ralph Campbelldd5190b2007-03-15 14:45:00 -07001179 goto ack_len_err;
Ralph Campbell3859e392007-03-15 14:44:51 -07001180
Bryan O'Sullivan7bbb15e2006-07-01 04:35:51 -07001181 /* We got a response so update the timeout. */
Bryan O'Sullivan7bbb15e2006-07-01 04:35:51 -07001182 spin_lock(&dev->pending_lock);
1183 if (qp->s_rnr_timeout == 0 && !list_empty(&qp->timerwait))
1184 list_move_tail(&qp->timerwait,
1185 &dev->pending[dev->pending_index]);
1186 spin_unlock(&dev->pending_lock);
1187 /*
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -07001188 * Update the RDMA receive state but do the copy w/o
1189 * holding the locks and blocking interrupts.
Bryan O'Sullivan7bbb15e2006-07-01 04:35:51 -07001190 */
Ralph Campbell3859e392007-03-15 14:44:51 -07001191 qp->s_rdma_read_len -= pmtu;
Bryan O'Sullivan60229432006-09-28 08:59:57 -07001192 update_last_psn(qp, psn);
Bryan O'Sullivan7bbb15e2006-07-01 04:35:51 -07001193 spin_unlock_irqrestore(&qp->s_lock, flags);
Ralph Campbell3859e392007-03-15 14:44:51 -07001194 ipath_copy_sge(&qp->s_rdma_read_sge, data, pmtu);
Bryan O'Sullivan7bbb15e2006-07-01 04:35:51 -07001195 goto bail;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001196
Ralph Campbell3859e392007-03-15 14:44:51 -07001197 case OP(RDMA_READ_RESPONSE_ONLY):
Ralph Campbell06ee1092007-06-18 14:24:42 -07001198 if (!header_in_data)
1199 aeth = be32_to_cpu(ohdr->u.aeth);
1200 else
1201 aeth = be32_to_cpu(((__be32 *) data)[0]);
1202 if (!do_rc_ack(qp, aeth, psn, opcode, 0))
Ralph Campbell3859e392007-03-15 14:44:51 -07001203 goto ack_done;
Ralph Campbelldd5190b2007-03-15 14:45:00 -07001204 /* Get the number of bytes the message was padded by. */
1205 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1206 /*
1207 * Check that the data size is >= 0 && <= pmtu.
1208 * Remember to account for the AETH header (4) and
1209 * ICRC (4).
1210 */
1211 if (unlikely(tlen < (hdrsize + pad + 8)))
1212 goto ack_len_err;
Ralph Campbell3859e392007-03-15 14:44:51 -07001213 /*
1214 * If this is a response to a resent RDMA read, we
1215 * have to be careful to copy the data to the right
1216 * location.
Ralph Campbell3859e392007-03-15 14:44:51 -07001217 */
Ralph Campbell06ee1092007-06-18 14:24:42 -07001218 wqe = get_swqe_ptr(qp, qp->s_last);
Ralph Campbell3859e392007-03-15 14:44:51 -07001219 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1220 wqe, psn, pmtu);
1221 goto read_last;
1222
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001223 case OP(RDMA_READ_RESPONSE_LAST):
1224 /* ACKs READ req. */
1225 if (unlikely(ipath_cmp24(psn, qp->s_last_psn + 1))) {
1226 dev->n_rdma_seq++;
Ralph Campbell3859e392007-03-15 14:44:51 -07001227 ipath_restart_rc(qp, qp->s_last_psn + 1, &wc);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001228 goto ack_done;
1229 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001230 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
Ralph Campbelldd5190b2007-03-15 14:45:00 -07001231 goto ack_op_err;
1232 /* Get the number of bytes the message was padded by. */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001233 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1234 /*
1235 * Check that the data size is >= 1 && <= pmtu.
1236 * Remember to account for the AETH header (4) and
1237 * ICRC (4).
1238 */
Ralph Campbelldd5190b2007-03-15 14:45:00 -07001239 if (unlikely(tlen <= (hdrsize + pad + 8)))
1240 goto ack_len_err;
1241 read_last:
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001242 tlen -= hdrsize + pad + 8;
Ralph Campbelldd5190b2007-03-15 14:45:00 -07001243 if (unlikely(tlen != qp->s_rdma_read_len))
1244 goto ack_len_err;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001245 if (!header_in_data)
1246 aeth = be32_to_cpu(ohdr->u.aeth);
1247 else {
1248 aeth = be32_to_cpu(((__be32 *) data)[0]);
1249 data += sizeof(__be32);
1250 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001251 ipath_copy_sge(&qp->s_rdma_read_sge, data, tlen);
Ralph Campbell06ee1092007-06-18 14:24:42 -07001252 (void) do_rc_ack(qp, aeth, psn,
1253 OP(RDMA_READ_RESPONSE_LAST), 0);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001254 goto ack_done;
1255 }
1256
1257ack_done:
1258 spin_unlock_irqrestore(&qp->s_lock, flags);
Ralph Campbelldd5190b2007-03-15 14:45:00 -07001259 goto bail;
1260
1261ack_op_err:
1262 wc.status = IB_WC_LOC_QP_OP_ERR;
1263 goto ack_err;
1264
1265ack_len_err:
1266 wc.status = IB_WC_LOC_LEN_ERR;
1267ack_err:
1268 wc.wr_id = wqe->wr.wr_id;
1269 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
1270 wc.vendor_err = 0;
1271 wc.byte_len = 0;
1272 wc.imm_data = 0;
1273 wc.qp = &qp->ibqp;
1274 wc.src_qp = qp->remote_qpn;
1275 wc.wc_flags = 0;
1276 wc.pkey_index = 0;
1277 wc.slid = qp->remote_ah_attr.dlid;
1278 wc.sl = qp->remote_ah_attr.sl;
1279 wc.dlid_path_bits = 0;
1280 wc.port_num = 0;
1281 ipath_sqerror_qp(qp, &wc);
Ralph Campbell6ed89b92007-05-03 12:40:51 -07001282 spin_unlock_irqrestore(&qp->s_lock, flags);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001283bail:
1284 return;
1285}
1286
1287/**
1288 * ipath_rc_rcv_error - process an incoming duplicate or error RC packet
1289 * @dev: the device this packet came in on
1290 * @ohdr: the other headers for this packet
1291 * @data: the packet data
1292 * @qp: the QP for this packet
1293 * @opcode: the opcode for this packet
1294 * @psn: the packet sequence number for this packet
1295 * @diff: the difference between the PSN and the expected PSN
1296 * @header_in_data: true if part of the header data is in the data buffer
1297 *
1298 * This is called from ipath_rc_rcv() to process an unexpected
1299 * incoming RC packet for the given QP.
1300 * Called at interrupt level.
1301 * Return 1 if no more processing is needed; otherwise return 0 to
Ralph Campbell3859e392007-03-15 14:44:51 -07001302 * schedule a response to be sent.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001303 */
1304static inline int ipath_rc_rcv_error(struct ipath_ibdev *dev,
1305 struct ipath_other_headers *ohdr,
1306 void *data,
1307 struct ipath_qp *qp,
1308 u32 opcode,
1309 u32 psn,
1310 int diff,
1311 int header_in_data)
1312{
Ralph Campbell3859e392007-03-15 14:44:51 -07001313 struct ipath_ack_entry *e;
1314 u8 i, prev;
1315 int old_req;
Ralph Campbell35ff0322007-04-27 11:11:11 -07001316 unsigned long flags;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001317
1318 if (diff > 0) {
1319 /*
1320 * Packet sequence error.
1321 * A NAK will ACK earlier sends and RDMA writes.
Ralph Campbell3859e392007-03-15 14:44:51 -07001322 * Don't queue the NAK if we already sent one.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001323 */
Ralph Campbell3859e392007-03-15 14:44:51 -07001324 if (!qp->r_nak_state) {
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001325 qp->r_nak_state = IB_NAK_PSN_ERROR;
1326 /* Use the expected PSN. */
1327 qp->r_ack_psn = qp->r_psn;
Ralph Campbell3859e392007-03-15 14:44:51 -07001328 goto send_ack;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001329 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001330 goto done;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001331 }
1332
1333 /*
1334 * Handle a duplicate request. Don't re-execute SEND, RDMA
1335 * write or atomic op. Don't NAK errors, just silently drop
1336 * the duplicate request. Note that r_sge, r_len, and
1337 * r_rcv_len may be in use so don't modify them.
1338 *
1339 * We are supposed to ACK the earliest duplicate PSN but we
1340 * can coalesce an outstanding duplicate ACK. We have to
1341 * send the earliest so that RDMA reads can be restarted at
1342 * the requester's expected PSN.
Ralph Campbell3859e392007-03-15 14:44:51 -07001343 *
1344 * First, find where this duplicate PSN falls within the
1345 * ACKs previously sent.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001346 */
Ralph Campbell3859e392007-03-15 14:44:51 -07001347 psn &= IPATH_PSN_MASK;
1348 e = NULL;
1349 old_req = 1;
Ralph Campbell35ff0322007-04-27 11:11:11 -07001350 spin_lock_irqsave(&qp->s_lock, flags);
Ralph Campbell3859e392007-03-15 14:44:51 -07001351 for (i = qp->r_head_ack_queue; ; i = prev) {
1352 if (i == qp->s_tail_ack_queue)
1353 old_req = 0;
1354 if (i)
1355 prev = i - 1;
1356 else
1357 prev = IPATH_MAX_RDMA_ATOMIC;
1358 if (prev == qp->r_head_ack_queue) {
1359 e = NULL;
1360 break;
1361 }
1362 e = &qp->s_ack_queue[prev];
1363 if (!e->opcode) {
1364 e = NULL;
1365 break;
1366 }
Ralph Campbell06ee1092007-06-18 14:24:42 -07001367 if (ipath_cmp24(psn, e->psn) >= 0) {
1368 if (prev == qp->s_tail_ack_queue)
1369 old_req = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -07001370 break;
Ralph Campbell06ee1092007-06-18 14:24:42 -07001371 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001372 }
1373 switch (opcode) {
1374 case OP(RDMA_READ_REQUEST): {
1375 struct ib_reth *reth;
1376 u32 offset;
1377 u32 len;
1378
1379 /*
1380 * If we didn't find the RDMA read request in the ack queue,
1381 * or the send tasklet is already backed up to send an
1382 * earlier entry, we can ignore this request.
1383 */
1384 if (!e || e->opcode != OP(RDMA_READ_REQUEST) || old_req)
1385 goto unlock_done;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001386 /* RETH comes after BTH */
1387 if (!header_in_data)
1388 reth = &ohdr->u.rc.reth;
1389 else {
1390 reth = (struct ib_reth *)data;
1391 data += sizeof(*reth);
1392 }
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001393 /*
Ralph Campbell3859e392007-03-15 14:44:51 -07001394 * Address range must be a subset of the original
1395 * request and start on pmtu boundaries.
1396 * We reuse the old ack_queue slot since the requester
1397 * should not back up and request an earlier PSN for the
1398 * same request.
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001399 */
Ralph Campbell3859e392007-03-15 14:44:51 -07001400 offset = ((psn - e->psn) & IPATH_PSN_MASK) *
1401 ib_mtu_enum_to_int(qp->path_mtu);
1402 len = be32_to_cpu(reth->length);
1403 if (unlikely(offset + len > e->rdma_sge.sge.sge_length))
1404 goto unlock_done;
1405 if (len != 0) {
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001406 u32 rkey = be32_to_cpu(reth->rkey);
1407 u64 vaddr = be64_to_cpu(reth->vaddr);
1408 int ok;
1409
Ralph Campbell3859e392007-03-15 14:44:51 -07001410 ok = ipath_rkey_ok(qp, &e->rdma_sge,
1411 len, vaddr, rkey,
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001412 IB_ACCESS_REMOTE_READ);
Ralph Campbell3859e392007-03-15 14:44:51 -07001413 if (unlikely(!ok))
1414 goto unlock_done;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001415 } else {
Ralph Campbell3859e392007-03-15 14:44:51 -07001416 e->rdma_sge.sg_list = NULL;
1417 e->rdma_sge.num_sge = 0;
1418 e->rdma_sge.sge.mr = NULL;
1419 e->rdma_sge.sge.vaddr = NULL;
1420 e->rdma_sge.sge.length = 0;
1421 e->rdma_sge.sge.sge_length = 0;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001422 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001423 e->psn = psn;
1424 qp->s_ack_state = OP(ACKNOWLEDGE);
1425 qp->s_tail_ack_queue = prev;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001426 break;
1427 }
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001428
Ralph Campbell3859e392007-03-15 14:44:51 -07001429 case OP(COMPARE_SWAP):
1430 case OP(FETCH_ADD): {
1431 /*
1432 * If we didn't find the atomic request in the ack queue
1433 * or the send tasklet is already backed up to send an
1434 * earlier entry, we can ignore this request.
1435 */
1436 if (!e || e->opcode != (u8) opcode || old_req)
1437 goto unlock_done;
1438 qp->s_ack_state = OP(ACKNOWLEDGE);
1439 qp->s_tail_ack_queue = prev;
1440 break;
1441 }
1442
1443 default:
1444 if (old_req)
1445 goto unlock_done;
1446 /*
1447 * Resend the most recent ACK if this request is
1448 * after all the previous RDMA reads and atomics.
1449 */
1450 if (i == qp->r_head_ack_queue) {
Ralph Campbell35ff0322007-04-27 11:11:11 -07001451 spin_unlock_irqrestore(&qp->s_lock, flags);
Ralph Campbell3859e392007-03-15 14:44:51 -07001452 qp->r_nak_state = 0;
1453 qp->r_ack_psn = qp->r_psn - 1;
1454 goto send_ack;
1455 }
1456 /*
1457 * Resend the RDMA read or atomic op which
1458 * ACKs this duplicate request.
1459 */
1460 qp->s_ack_state = OP(ACKNOWLEDGE);
1461 qp->s_tail_ack_queue = i;
1462 break;
1463 }
1464 qp->r_nak_state = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -07001465 tasklet_hi_schedule(&qp->s_task);
1466
1467unlock_done:
Ralph Campbell35ff0322007-04-27 11:11:11 -07001468 spin_unlock_irqrestore(&qp->s_lock, flags);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001469done:
1470 return 1;
Ralph Campbell3859e392007-03-15 14:44:51 -07001471
1472send_ack:
1473 return 0;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001474}
1475
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -07001476static void ipath_rc_error(struct ipath_qp *qp, enum ib_wc_status err)
1477{
Ralph Campbell35ff0322007-04-27 11:11:11 -07001478 unsigned long flags;
1479
1480 spin_lock_irqsave(&qp->s_lock, flags);
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -07001481 qp->state = IB_QPS_ERR;
1482 ipath_error_qp(qp, err);
Ralph Campbell35ff0322007-04-27 11:11:11 -07001483 spin_unlock_irqrestore(&qp->s_lock, flags);
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -07001484}
1485
Ralph Campbelld781b122007-06-18 14:24:44 -07001486static inline void ipath_update_ack_queue(struct ipath_qp *qp, unsigned n)
1487{
1488 unsigned long flags;
1489 unsigned next;
1490
1491 next = n + 1;
1492 if (next > IPATH_MAX_RDMA_ATOMIC)
1493 next = 0;
1494 spin_lock_irqsave(&qp->s_lock, flags);
1495 if (n == qp->s_tail_ack_queue) {
1496 qp->s_tail_ack_queue = next;
1497 qp->s_ack_state = OP(ACKNOWLEDGE);
1498 }
1499 spin_unlock_irqrestore(&qp->s_lock, flags);
1500}
1501
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001502/**
1503 * ipath_rc_rcv - process an incoming RC packet
1504 * @dev: the device this packet came in on
1505 * @hdr: the header of this packet
1506 * @has_grh: true if the header has a GRH
1507 * @data: the packet data
1508 * @tlen: the packet length
1509 * @qp: the QP for this packet
1510 *
1511 * This is called from ipath_qp_rcv() to process an incoming RC packet
1512 * for the given QP.
1513 * Called at interrupt level.
1514 */
1515void ipath_rc_rcv(struct ipath_ibdev *dev, struct ipath_ib_header *hdr,
1516 int has_grh, void *data, u32 tlen, struct ipath_qp *qp)
1517{
1518 struct ipath_other_headers *ohdr;
1519 u32 opcode;
1520 u32 hdrsize;
1521 u32 psn;
1522 u32 pad;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001523 struct ib_wc wc;
1524 u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
1525 int diff;
1526 struct ib_reth *reth;
1527 int header_in_data;
1528
Bryan O'Sullivan10aeb0e2006-09-28 09:00:06 -07001529 /* Validate the SLID. See Ch. 9.6.1.5 */
1530 if (unlikely(be16_to_cpu(hdr->lrh[3]) != qp->remote_ah_attr.dlid))
1531 goto done;
1532
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001533 /* Check for GRH */
1534 if (!has_grh) {
1535 ohdr = &hdr->u.oth;
1536 hdrsize = 8 + 12; /* LRH + BTH */
1537 psn = be32_to_cpu(ohdr->bth[2]);
1538 header_in_data = 0;
1539 } else {
1540 ohdr = &hdr->u.l.oth;
1541 hdrsize = 8 + 40 + 12; /* LRH + GRH + BTH */
1542 /*
1543 * The header with GRH is 60 bytes and the core driver sets
1544 * the eager header buffer size to 56 bytes so the last 4
1545 * bytes of the BTH header (PSN) is in the data buffer.
1546 */
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001547 header_in_data = dev->dd->ipath_rcvhdrentsize == 16;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001548 if (header_in_data) {
1549 psn = be32_to_cpu(((__be32 *) data)[0]);
1550 data += sizeof(__be32);
1551 } else
1552 psn = be32_to_cpu(ohdr->bth[2]);
1553 }
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001554
1555 /*
1556 * Process responses (ACKs) before anything else. Note that the
1557 * packet sequence number will be for something in the send work
1558 * queue rather than the expected receive packet sequence number.
1559 * In other words, this QP is the requester.
1560 */
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -07001561 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001562 if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
1563 opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
1564 ipath_rc_rcv_resp(dev, ohdr, data, tlen, qp, opcode, psn,
1565 hdrsize, pmtu, header_in_data);
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001566 goto done;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001567 }
1568
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001569 /* Compute 24 bits worth of difference. */
1570 diff = ipath_cmp24(psn, qp->r_psn);
1571 if (unlikely(diff)) {
1572 if (ipath_rc_rcv_error(dev, ohdr, data, qp, opcode,
1573 psn, diff, header_in_data))
1574 goto done;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001575 goto send_ack;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001576 }
1577
1578 /* Check for opcode sequence errors. */
1579 switch (qp->r_state) {
1580 case OP(SEND_FIRST):
1581 case OP(SEND_MIDDLE):
1582 if (opcode == OP(SEND_MIDDLE) ||
1583 opcode == OP(SEND_LAST) ||
1584 opcode == OP(SEND_LAST_WITH_IMMEDIATE))
1585 break;
1586 nack_inv:
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -07001587 ipath_rc_error(qp, IB_WC_REM_INV_REQ_ERR);
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001588 qp->r_nak_state = IB_NAK_INVALID_REQUEST;
1589 qp->r_ack_psn = qp->r_psn;
1590 goto send_ack;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001591
1592 case OP(RDMA_WRITE_FIRST):
1593 case OP(RDMA_WRITE_MIDDLE):
1594 if (opcode == OP(RDMA_WRITE_MIDDLE) ||
1595 opcode == OP(RDMA_WRITE_LAST) ||
1596 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
1597 break;
1598 goto nack_inv;
1599
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001600 default:
1601 if (opcode == OP(SEND_MIDDLE) ||
1602 opcode == OP(SEND_LAST) ||
1603 opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
1604 opcode == OP(RDMA_WRITE_MIDDLE) ||
1605 opcode == OP(RDMA_WRITE_LAST) ||
1606 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
1607 goto nack_inv;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001608 /*
1609 * Note that it is up to the requester to not send a new
1610 * RDMA read or atomic operation before receiving an ACK
1611 * for the previous operation.
1612 */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001613 break;
1614 }
1615
1616 wc.imm_data = 0;
1617 wc.wc_flags = 0;
1618
1619 /* OK, process the packet. */
1620 switch (opcode) {
1621 case OP(SEND_FIRST):
1622 if (!ipath_get_rwqe(qp, 0)) {
1623 rnr_nak:
1624 /*
1625 * A RNR NAK will ACK earlier sends and RDMA writes.
1626 * Don't queue the NAK if a RDMA read or atomic
1627 * is pending though.
1628 */
Ralph Campbell3859e392007-03-15 14:44:51 -07001629 if (qp->r_nak_state)
1630 goto done;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001631 qp->r_nak_state = IB_RNR_NAK | qp->r_min_rnr_timer;
1632 qp->r_ack_psn = qp->r_psn;
1633 goto send_ack;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001634 }
1635 qp->r_rcv_len = 0;
1636 /* FALLTHROUGH */
1637 case OP(SEND_MIDDLE):
1638 case OP(RDMA_WRITE_MIDDLE):
1639 send_middle:
1640 /* Check for invalid length PMTU or posted rwqe len. */
1641 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1642 goto nack_inv;
1643 qp->r_rcv_len += pmtu;
1644 if (unlikely(qp->r_rcv_len > qp->r_len))
1645 goto nack_inv;
1646 ipath_copy_sge(&qp->r_sge, data, pmtu);
1647 break;
1648
1649 case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
1650 /* consume RWQE */
1651 if (!ipath_get_rwqe(qp, 1))
1652 goto rnr_nak;
1653 goto send_last_imm;
1654
1655 case OP(SEND_ONLY):
1656 case OP(SEND_ONLY_WITH_IMMEDIATE):
1657 if (!ipath_get_rwqe(qp, 0))
1658 goto rnr_nak;
1659 qp->r_rcv_len = 0;
1660 if (opcode == OP(SEND_ONLY))
1661 goto send_last;
1662 /* FALLTHROUGH */
1663 case OP(SEND_LAST_WITH_IMMEDIATE):
1664 send_last_imm:
1665 if (header_in_data) {
1666 wc.imm_data = *(__be32 *) data;
1667 data += sizeof(__be32);
1668 } else {
1669 /* Immediate data comes after BTH */
1670 wc.imm_data = ohdr->u.imm_data;
1671 }
1672 hdrsize += 4;
1673 wc.wc_flags = IB_WC_WITH_IMM;
1674 /* FALLTHROUGH */
1675 case OP(SEND_LAST):
1676 case OP(RDMA_WRITE_LAST):
1677 send_last:
1678 /* Get the number of bytes the message was padded by. */
1679 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1680 /* Check for invalid length. */
1681 /* XXX LAST len should be >= 1 */
1682 if (unlikely(tlen < (hdrsize + pad + 4)))
1683 goto nack_inv;
1684 /* Don't count the CRC. */
1685 tlen -= (hdrsize + pad + 4);
1686 wc.byte_len = tlen + qp->r_rcv_len;
1687 if (unlikely(wc.byte_len > qp->r_len))
1688 goto nack_inv;
1689 ipath_copy_sge(&qp->r_sge, data, tlen);
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001690 qp->r_msn++;
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -07001691 if (!qp->r_wrid_valid)
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001692 break;
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -07001693 qp->r_wrid_valid = 0;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001694 wc.wr_id = qp->r_wr_id;
1695 wc.status = IB_WC_SUCCESS;
1696 wc.opcode = IB_WC_RECV;
1697 wc.vendor_err = 0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +02001698 wc.qp = &qp->ibqp;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001699 wc.src_qp = qp->remote_qpn;
1700 wc.pkey_index = 0;
1701 wc.slid = qp->remote_ah_attr.dlid;
1702 wc.sl = qp->remote_ah_attr.sl;
1703 wc.dlid_path_bits = 0;
1704 wc.port_num = 0;
1705 /* Signal completion event if the solicited bit is set. */
1706 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
1707 (ohdr->bth[0] &
1708 __constant_cpu_to_be32(1 << 23)) != 0);
1709 break;
1710
1711 case OP(RDMA_WRITE_FIRST):
1712 case OP(RDMA_WRITE_ONLY):
1713 case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
Robert Walshb506e1d2007-06-18 14:24:48 -07001714 if (unlikely(!(qp->qp_access_flags &
1715 IB_ACCESS_REMOTE_WRITE)))
1716 goto nack_inv;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001717 /* consume RWQE */
1718 /* RETH comes after BTH */
1719 if (!header_in_data)
1720 reth = &ohdr->u.rc.reth;
1721 else {
1722 reth = (struct ib_reth *)data;
1723 data += sizeof(*reth);
1724 }
1725 hdrsize += sizeof(*reth);
1726 qp->r_len = be32_to_cpu(reth->length);
1727 qp->r_rcv_len = 0;
1728 if (qp->r_len != 0) {
1729 u32 rkey = be32_to_cpu(reth->rkey);
1730 u64 vaddr = be64_to_cpu(reth->vaddr);
1731 int ok;
1732
1733 /* Check rkey & NAK */
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -07001734 ok = ipath_rkey_ok(qp, &qp->r_sge,
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001735 qp->r_len, vaddr, rkey,
1736 IB_ACCESS_REMOTE_WRITE);
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001737 if (unlikely(!ok))
1738 goto nack_acc;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001739 } else {
1740 qp->r_sge.sg_list = NULL;
1741 qp->r_sge.sge.mr = NULL;
1742 qp->r_sge.sge.vaddr = NULL;
1743 qp->r_sge.sge.length = 0;
1744 qp->r_sge.sge.sge_length = 0;
1745 }
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001746 if (opcode == OP(RDMA_WRITE_FIRST))
1747 goto send_middle;
1748 else if (opcode == OP(RDMA_WRITE_ONLY))
1749 goto send_last;
1750 if (!ipath_get_rwqe(qp, 1))
1751 goto rnr_nak;
1752 goto send_last_imm;
1753
Ralph Campbell3859e392007-03-15 14:44:51 -07001754 case OP(RDMA_READ_REQUEST): {
1755 struct ipath_ack_entry *e;
1756 u32 len;
1757 u8 next;
1758
Robert Walshb506e1d2007-06-18 14:24:48 -07001759 if (unlikely(!(qp->qp_access_flags &
1760 IB_ACCESS_REMOTE_READ)))
1761 goto nack_inv;
Ralph Campbell3859e392007-03-15 14:44:51 -07001762 next = qp->r_head_ack_queue + 1;
1763 if (next > IPATH_MAX_RDMA_ATOMIC)
1764 next = 0;
Ralph Campbelld781b122007-06-18 14:24:44 -07001765 if (unlikely(next == qp->s_tail_ack_queue)) {
1766 if (!qp->s_ack_queue[next].sent)
1767 goto nack_inv;
1768 ipath_update_ack_queue(qp, next);
1769 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001770 e = &qp->s_ack_queue[qp->r_head_ack_queue];
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001771 /* RETH comes after BTH */
1772 if (!header_in_data)
1773 reth = &ohdr->u.rc.reth;
1774 else {
1775 reth = (struct ib_reth *)data;
1776 data += sizeof(*reth);
1777 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001778 len = be32_to_cpu(reth->length);
1779 if (len) {
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001780 u32 rkey = be32_to_cpu(reth->rkey);
1781 u64 vaddr = be64_to_cpu(reth->vaddr);
1782 int ok;
1783
1784 /* Check rkey & NAK */
Ralph Campbell3859e392007-03-15 14:44:51 -07001785 ok = ipath_rkey_ok(qp, &e->rdma_sge, len, vaddr,
1786 rkey, IB_ACCESS_REMOTE_READ);
1787 if (unlikely(!ok))
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001788 goto nack_acc;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001789 /*
1790 * Update the next expected PSN. We add 1 later
1791 * below, so only add the remainder here.
1792 */
Ralph Campbell3859e392007-03-15 14:44:51 -07001793 if (len > pmtu)
1794 qp->r_psn += (len - 1) / pmtu;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001795 } else {
Ralph Campbell3859e392007-03-15 14:44:51 -07001796 e->rdma_sge.sg_list = NULL;
1797 e->rdma_sge.num_sge = 0;
1798 e->rdma_sge.sge.mr = NULL;
1799 e->rdma_sge.sge.vaddr = NULL;
1800 e->rdma_sge.sge.length = 0;
1801 e->rdma_sge.sge.sge_length = 0;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001802 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001803 e->opcode = opcode;
Ralph Campbelld781b122007-06-18 14:24:44 -07001804 e->sent = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -07001805 e->psn = psn;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001806 /*
1807 * We need to increment the MSN here instead of when we
1808 * finish sending the result since a duplicate request would
1809 * increment it more than once.
1810 */
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001811 qp->r_msn++;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001812 qp->r_psn++;
1813 qp->r_state = opcode;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001814 qp->r_nak_state = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -07001815 barrier();
1816 qp->r_head_ack_queue = next;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001817
1818 /* Call ipath_do_rc_send() in another thread. */
1819 tasklet_hi_schedule(&qp->s_task);
1820
1821 goto done;
Ralph Campbell3859e392007-03-15 14:44:51 -07001822 }
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001823
1824 case OP(COMPARE_SWAP):
1825 case OP(FETCH_ADD): {
1826 struct ib_atomic_eth *ateth;
Ralph Campbell3859e392007-03-15 14:44:51 -07001827 struct ipath_ack_entry *e;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001828 u64 vaddr;
Ralph Campbell3859e392007-03-15 14:44:51 -07001829 atomic64_t *maddr;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001830 u64 sdata;
1831 u32 rkey;
Ralph Campbell3859e392007-03-15 14:44:51 -07001832 u8 next;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001833
Ralph Campbell3859e392007-03-15 14:44:51 -07001834 if (unlikely(!(qp->qp_access_flags &
1835 IB_ACCESS_REMOTE_ATOMIC)))
Robert Walshb506e1d2007-06-18 14:24:48 -07001836 goto nack_inv;
Ralph Campbell3859e392007-03-15 14:44:51 -07001837 next = qp->r_head_ack_queue + 1;
1838 if (next > IPATH_MAX_RDMA_ATOMIC)
1839 next = 0;
Ralph Campbelld781b122007-06-18 14:24:44 -07001840 if (unlikely(next == qp->s_tail_ack_queue)) {
1841 if (!qp->s_ack_queue[next].sent)
1842 goto nack_inv;
1843 ipath_update_ack_queue(qp, next);
1844 }
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001845 if (!header_in_data)
1846 ateth = &ohdr->u.atomic_eth;
Ralph Campbell3859e392007-03-15 14:44:51 -07001847 else
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001848 ateth = (struct ib_atomic_eth *)data;
Ralph Campbell3859e392007-03-15 14:44:51 -07001849 vaddr = ((u64) be32_to_cpu(ateth->vaddr[0]) << 32) |
1850 be32_to_cpu(ateth->vaddr[1]);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001851 if (unlikely(vaddr & (sizeof(u64) - 1)))
1852 goto nack_inv;
1853 rkey = be32_to_cpu(ateth->rkey);
1854 /* Check rkey & NAK */
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -07001855 if (unlikely(!ipath_rkey_ok(qp, &qp->r_sge,
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001856 sizeof(u64), vaddr, rkey,
1857 IB_ACCESS_REMOTE_ATOMIC)))
1858 goto nack_acc;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001859 /* Perform atomic OP and save result. */
Ralph Campbell3859e392007-03-15 14:44:51 -07001860 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001861 sdata = be64_to_cpu(ateth->swap_data);
Ralph Campbell3859e392007-03-15 14:44:51 -07001862 e = &qp->s_ack_queue[qp->r_head_ack_queue];
1863 e->atomic_data = (opcode == OP(FETCH_ADD)) ?
1864 (u64) atomic64_add_return(sdata, maddr) - sdata :
1865 (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
1866 be64_to_cpu(ateth->compare_data),
1867 sdata);
1868 e->opcode = opcode;
Ralph Campbelld781b122007-06-18 14:24:44 -07001869 e->sent = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -07001870 e->psn = psn & IPATH_PSN_MASK;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001871 qp->r_msn++;
Ralph Campbell3859e392007-03-15 14:44:51 -07001872 qp->r_psn++;
1873 qp->r_state = opcode;
1874 qp->r_nak_state = 0;
1875 barrier();
1876 qp->r_head_ack_queue = next;
1877
1878 /* Call ipath_do_rc_send() in another thread. */
1879 tasklet_hi_schedule(&qp->s_task);
1880
1881 goto done;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001882 }
1883
1884 default:
Ralph Campbell3859e392007-03-15 14:44:51 -07001885 /* NAK unknown opcodes. */
1886 goto nack_inv;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001887 }
1888 qp->r_psn++;
1889 qp->r_state = opcode;
Ralph Campbell3859e392007-03-15 14:44:51 -07001890 qp->r_ack_psn = psn;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001891 qp->r_nak_state = 0;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001892 /* Send an ACK if requested or required. */
Ralph Campbell3859e392007-03-15 14:44:51 -07001893 if (psn & (1 << 31))
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001894 goto send_ack;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001895 goto done;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001896
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001897nack_acc:
Ralph Campbell3859e392007-03-15 14:44:51 -07001898 ipath_rc_error(qp, IB_WC_REM_ACCESS_ERR);
1899 qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
1900 qp->r_ack_psn = qp->r_psn;
1901
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001902send_ack:
Ralph Campbell3859e392007-03-15 14:44:51 -07001903 send_rc_ack(qp);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001904
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001905done:
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001906 return;
1907}