blob: 2e4d544957afd124f2a94d61d359fab71fefb064 [file] [log] [blame]
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001/*
Bryan O'Sullivan759d5762006-07-01 04:35:49 -07002 * Copyright (c) 2006 QLogic, Inc. 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):
101 qp->s_ack_state = OP(ACKNOWLEDGE);
102 /* FALLTHROUGH */
103 case OP(ACKNOWLEDGE):
104 /* Check for no next entry in the queue. */
105 if (qp->r_head_ack_queue == qp->s_tail_ack_queue) {
106 if (qp->s_flags & IPATH_S_ACK_PENDING)
107 goto normal;
108 goto bail;
109 }
110
111 e = &qp->s_ack_queue[qp->s_tail_ack_queue];
112 if (e->opcode == OP(RDMA_READ_REQUEST)) {
113 /* Copy SGE state in case we need to resend */
114 qp->s_ack_rdma_sge = e->rdma_sge;
115 qp->s_cur_sge = &qp->s_ack_rdma_sge;
116 len = e->rdma_sge.sge.sge_length;
117 if (len > pmtu) {
118 len = pmtu;
119 qp->s_ack_state = OP(RDMA_READ_RESPONSE_FIRST);
120 } else {
121 qp->s_ack_state = OP(RDMA_READ_RESPONSE_ONLY);
122 if (++qp->s_tail_ack_queue >
123 IPATH_MAX_RDMA_ATOMIC)
124 qp->s_tail_ack_queue = 0;
125 }
126 ohdr->u.aeth = ipath_compute_aeth(qp);
127 hwords++;
128 qp->s_ack_rdma_psn = e->psn;
129 bth2 = qp->s_ack_rdma_psn++ & IPATH_PSN_MASK;
130 } else {
131 /* COMPARE_SWAP or FETCH_ADD */
132 qp->s_cur_sge = NULL;
133 len = 0;
134 qp->s_ack_state = OP(ATOMIC_ACKNOWLEDGE);
135 ohdr->u.at.aeth = ipath_compute_aeth(qp);
136 ohdr->u.at.atomic_ack_eth[0] =
137 cpu_to_be32(e->atomic_data >> 32);
138 ohdr->u.at.atomic_ack_eth[1] =
139 cpu_to_be32(e->atomic_data);
140 hwords += sizeof(ohdr->u.at) / sizeof(u32);
141 bth2 = e->psn;
142 if (++qp->s_tail_ack_queue > IPATH_MAX_RDMA_ATOMIC)
143 qp->s_tail_ack_queue = 0;
144 }
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800145 bth0 = qp->s_ack_state << 24;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800146 break;
147
148 case OP(RDMA_READ_RESPONSE_FIRST):
149 qp->s_ack_state = OP(RDMA_READ_RESPONSE_MIDDLE);
150 /* FALLTHROUGH */
151 case OP(RDMA_READ_RESPONSE_MIDDLE):
Ralph Campbell3859e392007-03-15 14:44:51 -0700152 len = qp->s_ack_rdma_sge.sge.sge_length;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800153 if (len > pmtu)
154 len = pmtu;
155 else {
156 ohdr->u.aeth = ipath_compute_aeth(qp);
157 hwords++;
158 qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
Ralph Campbell3859e392007-03-15 14:44:51 -0700159 if (++qp->s_tail_ack_queue > IPATH_MAX_RDMA_ATOMIC)
160 qp->s_tail_ack_queue = 0;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800161 }
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800162 bth0 = qp->s_ack_state << 24;
Ralph Campbell3859e392007-03-15 14:44:51 -0700163 bth2 = qp->s_ack_rdma_psn++ & IPATH_PSN_MASK;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800164 break;
165
166 default:
Ralph Campbell3859e392007-03-15 14:44:51 -0700167 normal:
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700168 /*
Ralph Campbell3859e392007-03-15 14:44:51 -0700169 * Send a regular ACK.
170 * Set the s_ack_state so we wait until after sending
171 * the ACK before setting s_ack_state to ACKNOWLEDGE
172 * (see above).
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700173 */
Ralph Campbell3859e392007-03-15 14:44:51 -0700174 qp->s_ack_state = OP(ATOMIC_ACKNOWLEDGE);
175 qp->s_flags &= ~IPATH_S_ACK_PENDING;
176 qp->s_cur_sge = NULL;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700177 if (qp->s_nak_state)
Ralph Campbell3859e392007-03-15 14:44:51 -0700178 ohdr->u.aeth =
179 cpu_to_be32((qp->r_msn & IPATH_MSN_MASK) |
180 (qp->s_nak_state <<
181 IPATH_AETH_CREDIT_SHIFT));
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700182 else
183 ohdr->u.aeth = ipath_compute_aeth(qp);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800184 hwords++;
Ralph Campbell3859e392007-03-15 14:44:51 -0700185 len = 0;
186 bth0 = OP(ACKNOWLEDGE) << 24;
187 bth2 = qp->s_ack_psn & IPATH_PSN_MASK;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800188 }
189 qp->s_hdrwords = hwords;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800190 qp->s_cur_size = len;
Ralph Campbell3859e392007-03-15 14:44:51 -0700191 *bth0p = bth0;
192 *bth2p = bth2;
193 return 1;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800194
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700195bail:
Ralph Campbell3859e392007-03-15 14:44:51 -0700196 return 0;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800197}
198
199/**
200 * ipath_make_rc_req - construct a request packet (SEND, RDMA r/w, ATOMIC)
201 * @qp: a pointer to the QP
202 * @ohdr: a pointer to the IB header being constructed
203 * @pmtu: the path MTU
204 * @bth0p: pointer to the BTH opcode word
205 * @bth2p: pointer to the BTH PSN word
206 *
207 * Return 1 if constructed; otherwise, return 0.
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700208 * Note the QP s_lock must be held and interrupts disabled.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800209 */
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700210int ipath_make_rc_req(struct ipath_qp *qp,
211 struct ipath_other_headers *ohdr,
212 u32 pmtu, u32 *bth0p, u32 *bth2p)
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800213{
214 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
215 struct ipath_sge_state *ss;
216 struct ipath_swqe *wqe;
217 u32 hwords;
218 u32 len;
219 u32 bth0;
220 u32 bth2;
221 char newreq;
222
Ralph Campbell3859e392007-03-15 14:44:51 -0700223 /* Sending responses has higher priority over sending requests. */
224 if ((qp->r_head_ack_queue != qp->s_tail_ack_queue ||
225 (qp->s_flags & IPATH_S_ACK_PENDING) ||
226 qp->s_ack_state != IB_OPCODE_RC_ACKNOWLEDGE) &&
227 ipath_make_rc_ack(qp, ohdr, pmtu, bth0p, bth2p))
228 goto done;
229
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800230 if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_SEND_OK) ||
231 qp->s_rnr_timeout)
Ralph Campbell3859e392007-03-15 14:44:51 -0700232 goto bail;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800233
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700234 /* Limit the number of packets sent without an ACK. */
235 if (ipath_cmp24(qp->s_psn, qp->s_last_psn + IPATH_PSN_CREDIT) > 0) {
236 qp->s_wait_credit = 1;
237 dev->n_rc_stalls++;
238 spin_lock(&dev->pending_lock);
239 if (list_empty(&qp->timerwait))
240 list_add_tail(&qp->timerwait,
241 &dev->pending[dev->pending_index]);
242 spin_unlock(&dev->pending_lock);
Ralph Campbell3859e392007-03-15 14:44:51 -0700243 goto bail;
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700244 }
245
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800246 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
247 hwords = 5;
248 bth0 = 0;
249
250 /* Send a request. */
251 wqe = get_swqe_ptr(qp, qp->s_cur);
252 switch (qp->s_state) {
253 default:
254 /*
255 * Resend an old request or start a new one.
256 *
257 * We keep track of the current SWQE so that
258 * we don't reset the "furthest progress" state
259 * if we need to back up.
260 */
261 newreq = 0;
262 if (qp->s_cur == qp->s_tail) {
263 /* Check if send work queue is empty. */
264 if (qp->s_tail == qp->s_head)
Ralph Campbell3859e392007-03-15 14:44:51 -0700265 goto bail;
266 /*
267 * If a fence is requested, wait for previous
268 * RDMA read and atomic operations to finish.
269 */
270 if ((wqe->wr.send_flags & IB_SEND_FENCE) &&
271 qp->s_num_rd_atomic) {
272 qp->s_flags |= IPATH_S_FENCE_PENDING;
273 goto bail;
274 }
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700275 wqe->psn = qp->s_next_psn;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800276 newreq = 1;
277 }
278 /*
279 * Note that we have to be careful not to modify the
280 * original work request since we may need to resend
281 * it.
282 */
Ralph Campbell13b18c82006-09-29 14:37:51 -0700283 len = wqe->length;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800284 ss = &qp->s_sge;
285 bth2 = 0;
286 switch (wqe->wr.opcode) {
287 case IB_WR_SEND:
288 case IB_WR_SEND_WITH_IMM:
289 /* If no credit, return. */
290 if (qp->s_lsn != (u32) -1 &&
291 ipath_cmp24(wqe->ssn, qp->s_lsn + 1) > 0)
Ralph Campbell3859e392007-03-15 14:44:51 -0700292 goto bail;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800293 wqe->lpsn = wqe->psn;
294 if (len > pmtu) {
295 wqe->lpsn += (len - 1) / pmtu;
296 qp->s_state = OP(SEND_FIRST);
297 len = pmtu;
298 break;
299 }
300 if (wqe->wr.opcode == IB_WR_SEND)
301 qp->s_state = OP(SEND_ONLY);
302 else {
303 qp->s_state = OP(SEND_ONLY_WITH_IMMEDIATE);
304 /* Immediate data comes after the BTH */
305 ohdr->u.imm_data = wqe->wr.imm_data;
306 hwords += 1;
307 }
308 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
309 bth0 |= 1 << 23;
310 bth2 = 1 << 31; /* Request ACK. */
311 if (++qp->s_cur == qp->s_size)
312 qp->s_cur = 0;
313 break;
314
315 case IB_WR_RDMA_WRITE:
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700316 if (newreq && qp->s_lsn != (u32) -1)
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800317 qp->s_lsn++;
318 /* FALLTHROUGH */
319 case IB_WR_RDMA_WRITE_WITH_IMM:
320 /* If no credit, return. */
321 if (qp->s_lsn != (u32) -1 &&
322 ipath_cmp24(wqe->ssn, qp->s_lsn + 1) > 0)
Ralph Campbell3859e392007-03-15 14:44:51 -0700323 goto bail;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800324 ohdr->u.rc.reth.vaddr =
325 cpu_to_be64(wqe->wr.wr.rdma.remote_addr);
326 ohdr->u.rc.reth.rkey =
327 cpu_to_be32(wqe->wr.wr.rdma.rkey);
328 ohdr->u.rc.reth.length = cpu_to_be32(len);
Ralph Campbell3859e392007-03-15 14:44:51 -0700329 hwords += sizeof(struct ib_reth) / sizeof(u32);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800330 wqe->lpsn = wqe->psn;
331 if (len > pmtu) {
332 wqe->lpsn += (len - 1) / pmtu;
333 qp->s_state = OP(RDMA_WRITE_FIRST);
334 len = pmtu;
335 break;
336 }
337 if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
338 qp->s_state = OP(RDMA_WRITE_ONLY);
339 else {
340 qp->s_state =
341 OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700342 /* Immediate data comes after RETH */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800343 ohdr->u.rc.imm_data = wqe->wr.imm_data;
344 hwords += 1;
345 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
346 bth0 |= 1 << 23;
347 }
348 bth2 = 1 << 31; /* Request ACK. */
349 if (++qp->s_cur == qp->s_size)
350 qp->s_cur = 0;
351 break;
352
353 case IB_WR_RDMA_READ:
Ralph Campbell3859e392007-03-15 14:44:51 -0700354 /*
355 * Don't allow more operations to be started
356 * than the QP limits allow.
357 */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800358 if (newreq) {
Ralph Campbell3859e392007-03-15 14:44:51 -0700359 if (qp->s_num_rd_atomic >=
360 qp->s_max_rd_atomic) {
361 qp->s_flags |= IPATH_S_RDMAR_PENDING;
362 goto bail;
363 }
364 qp->s_num_rd_atomic++;
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700365 if (qp->s_lsn != (u32) -1)
366 qp->s_lsn++;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800367 /*
368 * Adjust s_next_psn to count the
369 * expected number of responses.
370 */
371 if (len > pmtu)
372 qp->s_next_psn += (len - 1) / pmtu;
373 wqe->lpsn = qp->s_next_psn++;
374 }
Ralph Campbell3859e392007-03-15 14:44:51 -0700375 ohdr->u.rc.reth.vaddr =
376 cpu_to_be64(wqe->wr.wr.rdma.remote_addr);
377 ohdr->u.rc.reth.rkey =
378 cpu_to_be32(wqe->wr.wr.rdma.rkey);
379 ohdr->u.rc.reth.length = cpu_to_be32(len);
380 qp->s_state = OP(RDMA_READ_REQUEST);
381 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800382 ss = NULL;
383 len = 0;
384 if (++qp->s_cur == qp->s_size)
385 qp->s_cur = 0;
386 break;
387
388 case IB_WR_ATOMIC_CMP_AND_SWP:
389 case IB_WR_ATOMIC_FETCH_AND_ADD:
Ralph Campbell3859e392007-03-15 14:44:51 -0700390 /*
391 * Don't allow more operations to be started
392 * than the QP limits allow.
393 */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800394 if (newreq) {
Ralph Campbell3859e392007-03-15 14:44:51 -0700395 if (qp->s_num_rd_atomic >=
396 qp->s_max_rd_atomic) {
397 qp->s_flags |= IPATH_S_RDMAR_PENDING;
398 goto bail;
399 }
400 qp->s_num_rd_atomic++;
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700401 if (qp->s_lsn != (u32) -1)
402 qp->s_lsn++;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800403 wqe->lpsn = wqe->psn;
404 }
Ralph Campbell3859e392007-03-15 14:44:51 -0700405 if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
406 qp->s_state = OP(COMPARE_SWAP);
407 ohdr->u.atomic_eth.swap_data = cpu_to_be64(
408 wqe->wr.wr.atomic.swap);
409 ohdr->u.atomic_eth.compare_data = cpu_to_be64(
410 wqe->wr.wr.atomic.compare_add);
411 } else {
412 qp->s_state = OP(FETCH_ADD);
413 ohdr->u.atomic_eth.swap_data = cpu_to_be64(
414 wqe->wr.wr.atomic.compare_add);
415 ohdr->u.atomic_eth.compare_data = 0;
416 }
417 ohdr->u.atomic_eth.vaddr[0] = cpu_to_be32(
418 wqe->wr.wr.atomic.remote_addr >> 32);
419 ohdr->u.atomic_eth.vaddr[1] = cpu_to_be32(
420 wqe->wr.wr.atomic.remote_addr);
421 ohdr->u.atomic_eth.rkey = cpu_to_be32(
422 wqe->wr.wr.atomic.rkey);
423 hwords += sizeof(struct ib_atomic_eth) / sizeof(u32);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800424 ss = NULL;
425 len = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -0700426 if (++qp->s_cur == qp->s_size)
427 qp->s_cur = 0;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800428 break;
429
430 default:
Ralph Campbell3859e392007-03-15 14:44:51 -0700431 goto bail;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800432 }
Ralph Campbell13b18c82006-09-29 14:37:51 -0700433 qp->s_sge.sge = wqe->sg_list[0];
434 qp->s_sge.sg_list = wqe->sg_list + 1;
435 qp->s_sge.num_sge = wqe->wr.num_sge;
436 qp->s_len = wqe->length;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800437 if (newreq) {
438 qp->s_tail++;
439 if (qp->s_tail >= qp->s_size)
440 qp->s_tail = 0;
441 }
Ralph Campbell13b18c82006-09-29 14:37:51 -0700442 bth2 |= qp->s_psn & IPATH_PSN_MASK;
443 if (wqe->wr.opcode == IB_WR_RDMA_READ)
444 qp->s_psn = wqe->lpsn + 1;
445 else {
446 qp->s_psn++;
447 if ((int)(qp->s_psn - qp->s_next_psn) > 0)
448 qp->s_next_psn = qp->s_psn;
449 }
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700450 /*
451 * Put the QP on the pending list so lost ACKs will cause
452 * a retry. More than one request can be pending so the
453 * QP may already be on the dev->pending list.
454 */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800455 spin_lock(&dev->pending_lock);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700456 if (list_empty(&qp->timerwait))
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800457 list_add_tail(&qp->timerwait,
458 &dev->pending[dev->pending_index]);
459 spin_unlock(&dev->pending_lock);
460 break;
461
462 case OP(RDMA_READ_RESPONSE_FIRST):
463 /*
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700464 * This case can only happen if a send is restarted.
465 * See ipath_restart_rc().
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800466 */
467 ipath_init_restart(qp, wqe);
468 /* FALLTHROUGH */
469 case OP(SEND_FIRST):
470 qp->s_state = OP(SEND_MIDDLE);
471 /* FALLTHROUGH */
472 case OP(SEND_MIDDLE):
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700473 bth2 = qp->s_psn++ & IPATH_PSN_MASK;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800474 if ((int)(qp->s_psn - qp->s_next_psn) > 0)
475 qp->s_next_psn = qp->s_psn;
476 ss = &qp->s_sge;
477 len = qp->s_len;
478 if (len > pmtu) {
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800479 len = pmtu;
480 break;
481 }
482 if (wqe->wr.opcode == IB_WR_SEND)
483 qp->s_state = OP(SEND_LAST);
484 else {
485 qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
486 /* Immediate data comes after the BTH */
487 ohdr->u.imm_data = wqe->wr.imm_data;
488 hwords += 1;
489 }
490 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
491 bth0 |= 1 << 23;
492 bth2 |= 1 << 31; /* Request ACK. */
493 qp->s_cur++;
494 if (qp->s_cur >= qp->s_size)
495 qp->s_cur = 0;
496 break;
497
498 case OP(RDMA_READ_RESPONSE_LAST):
499 /*
500 * This case can only happen if a RDMA write is restarted.
501 * See ipath_restart_rc().
502 */
503 ipath_init_restart(qp, wqe);
504 /* FALLTHROUGH */
505 case OP(RDMA_WRITE_FIRST):
506 qp->s_state = OP(RDMA_WRITE_MIDDLE);
507 /* FALLTHROUGH */
508 case OP(RDMA_WRITE_MIDDLE):
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700509 bth2 = qp->s_psn++ & IPATH_PSN_MASK;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800510 if ((int)(qp->s_psn - qp->s_next_psn) > 0)
511 qp->s_next_psn = qp->s_psn;
512 ss = &qp->s_sge;
513 len = qp->s_len;
514 if (len > pmtu) {
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800515 len = pmtu;
516 break;
517 }
518 if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
519 qp->s_state = OP(RDMA_WRITE_LAST);
520 else {
521 qp->s_state = OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
522 /* Immediate data comes after the BTH */
523 ohdr->u.imm_data = wqe->wr.imm_data;
524 hwords += 1;
525 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
526 bth0 |= 1 << 23;
527 }
528 bth2 |= 1 << 31; /* Request ACK. */
529 qp->s_cur++;
530 if (qp->s_cur >= qp->s_size)
531 qp->s_cur = 0;
532 break;
533
534 case OP(RDMA_READ_RESPONSE_MIDDLE):
535 /*
536 * This case can only happen if a RDMA read is restarted.
537 * See ipath_restart_rc().
538 */
539 ipath_init_restart(qp, wqe);
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700540 len = ((qp->s_psn - wqe->psn) & IPATH_PSN_MASK) * pmtu;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800541 ohdr->u.rc.reth.vaddr =
542 cpu_to_be64(wqe->wr.wr.rdma.remote_addr + len);
543 ohdr->u.rc.reth.rkey =
544 cpu_to_be32(wqe->wr.wr.rdma.rkey);
545 ohdr->u.rc.reth.length = cpu_to_be32(qp->s_len);
546 qp->s_state = OP(RDMA_READ_REQUEST);
Ralph Campbell3859e392007-03-15 14:44:51 -0700547 hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700548 bth2 = qp->s_psn++ & IPATH_PSN_MASK;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800549 if ((int)(qp->s_psn - qp->s_next_psn) > 0)
550 qp->s_next_psn = qp->s_psn;
551 ss = NULL;
552 len = 0;
553 qp->s_cur++;
554 if (qp->s_cur == qp->s_size)
555 qp->s_cur = 0;
556 break;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800557 }
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700558 if (ipath_cmp24(qp->s_psn, qp->s_last_psn + IPATH_PSN_CREDIT - 1) >= 0)
559 bth2 |= 1 << 31; /* Request ACK. */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800560 qp->s_len -= len;
561 qp->s_hdrwords = hwords;
562 qp->s_cur_sge = ss;
563 qp->s_cur_size = len;
564 *bth0p = bth0 | (qp->s_state << 24);
565 *bth2p = bth2;
Ralph Campbell3859e392007-03-15 14:44:51 -0700566done:
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800567 return 1;
568
Ralph Campbell3859e392007-03-15 14:44:51 -0700569bail:
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800570 return 0;
571}
572
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800573/**
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700574 * send_rc_ack - Construct an ACK packet and send it
575 * @qp: a pointer to the QP
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800576 *
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700577 * This is called from ipath_rc_rcv() and only uses the receive
578 * side QP state.
Ralph Campbell3859e392007-03-15 14:44:51 -0700579 * Note that RDMA reads and atomics are handled in the
580 * send side QP state and tasklet.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800581 */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800582static void send_rc_ack(struct ipath_qp *qp)
583{
584 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
585 u16 lrh0;
586 u32 bth0;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700587 u32 hwords;
588 struct ipath_ib_header hdr;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800589 struct ipath_other_headers *ohdr;
590
Ralph Campbell3859e392007-03-15 14:44:51 -0700591 /* Don't send ACK or NAK if a RDMA read or atomic is pending. */
592 if (qp->r_head_ack_queue != qp->s_tail_ack_queue)
593 goto queue_ack;
594
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800595 /* Construct the header. */
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700596 ohdr = &hdr.u.oth;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700597 lrh0 = IPATH_LRH_BTH;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800598 /* header size in 32-bit words LRH+BTH+AETH = (8+12+4)/4. */
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700599 hwords = 6;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800600 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700601 hwords += ipath_make_grh(dev, &hdr.u.l.grh,
602 &qp->remote_ah_attr.grh,
603 hwords, 0);
604 ohdr = &hdr.u.l.oth;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700605 lrh0 = IPATH_LRH_GRH;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800606 }
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700607 /* read pkey_index w/o lock (its atomic) */
Ralph Campbell3859e392007-03-15 14:44:51 -0700608 bth0 = ipath_get_pkey(dev->dd, qp->s_pkey_index) |
609 OP(ACKNOWLEDGE) << 24;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700610 if (qp->r_nak_state)
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700611 ohdr->u.aeth = cpu_to_be32((qp->r_msn & IPATH_MSN_MASK) |
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700612 (qp->r_nak_state <<
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700613 IPATH_AETH_CREDIT_SHIFT));
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700614 else
615 ohdr->u.aeth = ipath_compute_aeth(qp);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800616 lrh0 |= qp->remote_ah_attr.sl << 4;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700617 hdr.lrh[0] = cpu_to_be16(lrh0);
618 hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
619 hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700620 hdr.lrh[3] = cpu_to_be16(dev->dd->ipath_lid);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800621 ohdr->bth[0] = cpu_to_be32(bth0);
622 ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700623 ohdr->bth[2] = cpu_to_be32(qp->r_ack_psn & IPATH_PSN_MASK);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800624
625 /*
626 * If we can send the ACK, clear the ACK state.
627 */
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700628 if (ipath_verbs_send(dev->dd, hwords, (u32 *) &hdr, 0, NULL) == 0) {
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800629 dev->n_unicast_xmit++;
Ralph Campbell3859e392007-03-15 14:44:51 -0700630 goto done;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800631 }
Ralph Campbell3859e392007-03-15 14:44:51 -0700632
633 /*
634 * We are out of PIO buffers at the moment.
635 * Pass responsibility for sending the ACK to the
636 * send tasklet so that when a PIO buffer becomes
637 * available, the ACK is sent ahead of other outgoing
638 * packets.
639 */
640 dev->n_rc_qacks++;
641
642queue_ack:
643 spin_lock_irq(&qp->s_lock);
644 qp->s_flags |= IPATH_S_ACK_PENDING;
645 qp->s_nak_state = qp->r_nak_state;
646 qp->s_ack_psn = qp->r_ack_psn;
647 spin_unlock_irq(&qp->s_lock);
648
649 /* Call ipath_do_rc_send() in another thread. */
650 tasklet_hi_schedule(&qp->s_task);
651
652done:
653 return;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800654}
655
656/**
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700657 * reset_psn - reset the QP state to send starting from PSN
658 * @qp: the QP
659 * @psn: the packet sequence number to restart at
660 *
661 * This is called from ipath_rc_rcv() to process an incoming RC ACK
662 * for the given QP.
663 * Called at interrupt level with the QP s_lock held.
664 */
665static void reset_psn(struct ipath_qp *qp, u32 psn)
666{
667 u32 n = qp->s_last;
668 struct ipath_swqe *wqe = get_swqe_ptr(qp, n);
669 u32 opcode;
670
671 qp->s_cur = n;
672
673 /*
674 * If we are starting the request from the beginning,
675 * let the normal send code handle initialization.
676 */
677 if (ipath_cmp24(psn, wqe->psn) <= 0) {
678 qp->s_state = OP(SEND_LAST);
679 goto done;
680 }
681
682 /* Find the work request opcode corresponding to the given PSN. */
683 opcode = wqe->wr.opcode;
684 for (;;) {
685 int diff;
686
687 if (++n == qp->s_size)
688 n = 0;
689 if (n == qp->s_tail)
690 break;
691 wqe = get_swqe_ptr(qp, n);
692 diff = ipath_cmp24(psn, wqe->psn);
693 if (diff < 0)
694 break;
695 qp->s_cur = n;
696 /*
697 * If we are starting the request from the beginning,
698 * let the normal send code handle initialization.
699 */
700 if (diff == 0) {
701 qp->s_state = OP(SEND_LAST);
702 goto done;
703 }
704 opcode = wqe->wr.opcode;
705 }
706
707 /*
708 * Set the state to restart in the middle of a request.
709 * Don't change the s_sge, s_cur_sge, or s_cur_size.
710 * See ipath_do_rc_send().
711 */
712 switch (opcode) {
713 case IB_WR_SEND:
714 case IB_WR_SEND_WITH_IMM:
715 qp->s_state = OP(RDMA_READ_RESPONSE_FIRST);
716 break;
717
718 case IB_WR_RDMA_WRITE:
719 case IB_WR_RDMA_WRITE_WITH_IMM:
720 qp->s_state = OP(RDMA_READ_RESPONSE_LAST);
721 break;
722
723 case IB_WR_RDMA_READ:
724 qp->s_state = OP(RDMA_READ_RESPONSE_MIDDLE);
725 break;
726
727 default:
728 /*
729 * This case shouldn't happen since its only
730 * one PSN per req.
731 */
732 qp->s_state = OP(SEND_LAST);
733 }
734done:
735 qp->s_psn = psn;
736}
737
738/**
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800739 * ipath_restart_rc - back up requester to resend the last un-ACKed request
740 * @qp: the QP to restart
741 * @psn: packet sequence number for the request
742 * @wc: the work completion request
743 *
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700744 * The QP s_lock should be held and interrupts disabled.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800745 */
746void ipath_restart_rc(struct ipath_qp *qp, u32 psn, struct ib_wc *wc)
747{
748 struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
749 struct ipath_ibdev *dev;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800750
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800751 if (qp->s_retry == 0) {
752 wc->wr_id = wqe->wr.wr_id;
753 wc->status = IB_WC_RETRY_EXC_ERR;
754 wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
755 wc->vendor_err = 0;
756 wc->byte_len = 0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200757 wc->qp = &qp->ibqp;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800758 wc->src_qp = qp->remote_qpn;
759 wc->pkey_index = 0;
760 wc->slid = qp->remote_ah_attr.dlid;
761 wc->sl = qp->remote_ah_attr.sl;
762 wc->dlid_path_bits = 0;
763 wc->port_num = 0;
764 ipath_sqerror_qp(qp, wc);
765 goto bail;
766 }
767 qp->s_retry--;
768
769 /*
770 * Remove the QP from the timeout queue.
771 * Note: it may already have been removed by ipath_ib_timer().
772 */
773 dev = to_idev(qp->ibqp.device);
774 spin_lock(&dev->pending_lock);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700775 if (!list_empty(&qp->timerwait))
776 list_del_init(&qp->timerwait);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800777 spin_unlock(&dev->pending_lock);
778
779 if (wqe->wr.opcode == IB_WR_RDMA_READ)
780 dev->n_rc_resends++;
781 else
782 dev->n_rc_resends += (int)qp->s_psn - (int)psn;
783
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700784 reset_psn(qp, psn);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800785 tasklet_hi_schedule(&qp->s_task);
786
787bail:
788 return;
789}
790
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700791static inline void update_last_psn(struct ipath_qp *qp, u32 psn)
792{
793 if (qp->s_wait_credit) {
794 qp->s_wait_credit = 0;
795 tasklet_hi_schedule(&qp->s_task);
796 }
797 qp->s_last_psn = psn;
798}
799
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800800/**
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800801 * do_rc_ack - process an incoming RC ACK
802 * @qp: the QP the ACK came in on
803 * @psn: the packet sequence number of the ACK
804 * @opcode: the opcode of the request that resulted in the ACK
805 *
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700806 * This is called from ipath_rc_rcv_resp() to process an incoming RC ACK
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800807 * for the given QP.
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700808 * Called at interrupt level with the QP s_lock held and interrupts disabled.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800809 * Returns 1 if OK, 0 if current operation should be aborted (NAK).
810 */
811static int do_rc_ack(struct ipath_qp *qp, u32 aeth, u32 psn, int opcode)
812{
813 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
814 struct ib_wc wc;
815 struct ipath_swqe *wqe;
816 int ret = 0;
Ralph Campbell13b18c82006-09-29 14:37:51 -0700817 u32 ack_psn;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800818
819 /*
820 * Remove the QP from the timeout queue (or RNR timeout queue).
821 * If ipath_ib_timer() has already removed it,
822 * it's OK since we hold the QP s_lock and ipath_restart_rc()
823 * just won't find anything to restart if we ACK everything.
824 */
825 spin_lock(&dev->pending_lock);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700826 if (!list_empty(&qp->timerwait))
827 list_del_init(&qp->timerwait);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800828 spin_unlock(&dev->pending_lock);
829
830 /*
831 * Note that NAKs implicitly ACK outstanding SEND and RDMA write
832 * requests and implicitly NAK RDMA read and atomic requests issued
833 * before the NAK'ed request. The MSN won't include the NAK'ed
834 * request but will include an ACK'ed request(s).
835 */
Ralph Campbell13b18c82006-09-29 14:37:51 -0700836 ack_psn = psn;
837 if (aeth >> 29)
838 ack_psn--;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800839 wqe = get_swqe_ptr(qp, qp->s_last);
840
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800841 /*
842 * The MSN might be for a later WQE than the PSN indicates so
843 * only complete WQEs that the PSN finishes.
844 */
Ralph Campbell13b18c82006-09-29 14:37:51 -0700845 while (ipath_cmp24(ack_psn, wqe->lpsn) >= 0) {
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800846 /*
847 * If this request is a RDMA read or atomic, and the ACK is
848 * for a later operation, this ACK NAKs the RDMA read or
849 * atomic. In other words, only a RDMA_READ_LAST or ONLY
850 * can ACK a RDMA read and likewise for atomic ops. Note
851 * that the NAK case can only happen if relaxed ordering is
852 * used and requests are sent after an RDMA read or atomic
853 * is sent but before the response is received.
854 */
855 if ((wqe->wr.opcode == IB_WR_RDMA_READ &&
Ralph Campbell13b18c82006-09-29 14:37:51 -0700856 (opcode != OP(RDMA_READ_RESPONSE_LAST) ||
Ralph Campbell3859e392007-03-15 14:44:51 -0700857 ipath_cmp24(ack_psn, wqe->lpsn) != 0)) ||
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800858 ((wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
859 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) &&
860 (opcode != OP(ATOMIC_ACKNOWLEDGE) ||
861 ipath_cmp24(wqe->psn, psn) != 0))) {
862 /*
863 * The last valid PSN seen is the previous
864 * request's.
865 */
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700866 update_last_psn(qp, wqe->psn - 1);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800867 /* Retry this request. */
868 ipath_restart_rc(qp, wqe->psn, &wc);
869 /*
870 * No need to process the ACK/NAK since we are
871 * restarting an earlier request.
872 */
873 goto bail;
874 }
Ralph Campbell3859e392007-03-15 14:44:51 -0700875 if (qp->s_num_rd_atomic &&
876 (wqe->wr.opcode == IB_WR_RDMA_READ ||
877 wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
878 wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)) {
879 qp->s_num_rd_atomic--;
880 /* Restart sending task if fence is complete */
881 if ((qp->s_flags & IPATH_S_FENCE_PENDING) &&
882 !qp->s_num_rd_atomic) {
883 qp->s_flags &= ~IPATH_S_FENCE_PENDING;
884 tasklet_hi_schedule(&qp->s_task);
885 } else if (qp->s_flags & IPATH_S_RDMAR_PENDING) {
886 qp->s_flags &= ~IPATH_S_RDMAR_PENDING;
887 tasklet_hi_schedule(&qp->s_task);
888 }
889 }
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800890 /* Post a send completion queue entry if requested. */
Ralph Campbell3859e392007-03-15 14:44:51 -0700891 if (!(qp->s_flags & IPATH_S_SIGNAL_REQ_WR) ||
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800892 (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
893 wc.wr_id = wqe->wr.wr_id;
894 wc.status = IB_WC_SUCCESS;
895 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
896 wc.vendor_err = 0;
897 wc.byte_len = wqe->length;
Ralph Campbell0434d272007-03-15 14:44:53 -0700898 wc.imm_data = 0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200899 wc.qp = &qp->ibqp;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800900 wc.src_qp = qp->remote_qpn;
Ralph Campbell0434d272007-03-15 14:44:53 -0700901 wc.wc_flags = 0;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800902 wc.pkey_index = 0;
903 wc.slid = qp->remote_ah_attr.dlid;
904 wc.sl = qp->remote_ah_attr.sl;
905 wc.dlid_path_bits = 0;
906 wc.port_num = 0;
907 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 0);
908 }
909 qp->s_retry = qp->s_retry_cnt;
910 /*
911 * If we are completing a request which is in the process of
912 * being resent, we can stop resending it since we know the
913 * responder has already seen it.
914 */
915 if (qp->s_last == qp->s_cur) {
916 if (++qp->s_cur >= qp->s_size)
917 qp->s_cur = 0;
918 wqe = get_swqe_ptr(qp, qp->s_cur);
919 qp->s_state = OP(SEND_LAST);
920 qp->s_psn = wqe->psn;
921 }
922 if (++qp->s_last >= qp->s_size)
923 qp->s_last = 0;
924 wqe = get_swqe_ptr(qp, qp->s_last);
925 if (qp->s_last == qp->s_tail)
926 break;
927 }
928
929 switch (aeth >> 29) {
930 case 0: /* ACK */
931 dev->n_rc_acks++;
932 /* If this is a partial ACK, reset the retransmit timer. */
933 if (qp->s_last != qp->s_tail) {
934 spin_lock(&dev->pending_lock);
935 list_add_tail(&qp->timerwait,
936 &dev->pending[dev->pending_index]);
937 spin_unlock(&dev->pending_lock);
938 }
939 ipath_get_credit(qp, aeth);
940 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
941 qp->s_retry = qp->s_retry_cnt;
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700942 update_last_psn(qp, psn);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800943 ret = 1;
944 goto bail;
945
946 case 1: /* RNR NAK */
947 dev->n_rnr_naks++;
948 if (qp->s_rnr_retry == 0) {
949 if (qp->s_last == qp->s_tail)
950 goto bail;
951
952 wc.status = IB_WC_RNR_RETRY_EXC_ERR;
953 goto class_b;
954 }
955 if (qp->s_rnr_retry_cnt < 7)
956 qp->s_rnr_retry--;
957 if (qp->s_last == qp->s_tail)
958 goto bail;
959
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700960 /* The last valid PSN is the previous PSN. */
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700961 update_last_psn(qp, psn - 1);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800962
963 dev->n_rc_resends += (int)qp->s_psn - (int)psn;
964
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -0700965 reset_psn(qp, psn);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800966
967 qp->s_rnr_timeout =
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700968 ib_ipath_rnr_table[(aeth >> IPATH_AETH_CREDIT_SHIFT) &
969 IPATH_AETH_CREDIT_MASK];
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800970 ipath_insert_rnr_queue(qp);
971 goto bail;
972
973 case 3: /* NAK */
974 /* The last valid PSN seen is the previous request's. */
975 if (qp->s_last != qp->s_tail)
Bryan O'Sullivan60229432006-09-28 08:59:57 -0700976 update_last_psn(qp, wqe->psn - 1);
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700977 switch ((aeth >> IPATH_AETH_CREDIT_SHIFT) &
978 IPATH_AETH_CREDIT_MASK) {
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -0800979 case 0: /* PSN sequence error */
980 dev->n_seq_naks++;
981 /*
982 * Back up to the responder's expected PSN. XXX
983 * Note that we might get a NAK in the middle of an
984 * RDMA READ response which terminates the RDMA
985 * READ.
986 */
987 if (qp->s_last == qp->s_tail)
988 break;
989
990 if (ipath_cmp24(psn, wqe->psn) < 0)
991 break;
992
993 /* Retry the request. */
994 ipath_restart_rc(qp, psn, &wc);
995 break;
996
997 case 1: /* Invalid Request */
998 wc.status = IB_WC_REM_INV_REQ_ERR;
999 dev->n_other_naks++;
1000 goto class_b;
1001
1002 case 2: /* Remote Access Error */
1003 wc.status = IB_WC_REM_ACCESS_ERR;
1004 dev->n_other_naks++;
1005 goto class_b;
1006
1007 case 3: /* Remote Operation Error */
1008 wc.status = IB_WC_REM_OP_ERR;
1009 dev->n_other_naks++;
1010 class_b:
1011 wc.wr_id = wqe->wr.wr_id;
1012 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
1013 wc.vendor_err = 0;
1014 wc.byte_len = 0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +02001015 wc.qp = &qp->ibqp;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001016 wc.src_qp = qp->remote_qpn;
1017 wc.pkey_index = 0;
1018 wc.slid = qp->remote_ah_attr.dlid;
1019 wc.sl = qp->remote_ah_attr.sl;
1020 wc.dlid_path_bits = 0;
1021 wc.port_num = 0;
1022 ipath_sqerror_qp(qp, &wc);
1023 break;
1024
1025 default:
1026 /* Ignore other reserved NAK error codes */
1027 goto reserved;
1028 }
1029 qp->s_rnr_retry = qp->s_rnr_retry_cnt;
1030 goto bail;
1031
1032 default: /* 2: reserved */
1033 reserved:
1034 /* Ignore reserved NAK codes. */
1035 goto bail;
1036 }
1037
1038bail:
1039 return ret;
1040}
1041
1042/**
1043 * ipath_rc_rcv_resp - process an incoming RC response packet
1044 * @dev: the device this packet came in on
1045 * @ohdr: the other headers for this packet
1046 * @data: the packet data
1047 * @tlen: the packet length
1048 * @qp: the QP for this packet
1049 * @opcode: the opcode for this packet
1050 * @psn: the packet sequence number for this packet
1051 * @hdrsize: the header length
1052 * @pmtu: the path MTU
1053 * @header_in_data: true if part of the header data is in the data buffer
1054 *
1055 * This is called from ipath_rc_rcv() to process an incoming RC response
1056 * packet for the given QP.
1057 * Called at interrupt level.
1058 */
1059static inline void ipath_rc_rcv_resp(struct ipath_ibdev *dev,
1060 struct ipath_other_headers *ohdr,
1061 void *data, u32 tlen,
1062 struct ipath_qp *qp,
1063 u32 opcode,
1064 u32 psn, u32 hdrsize, u32 pmtu,
1065 int header_in_data)
1066{
Ralph Campbell3859e392007-03-15 14:44:51 -07001067 struct ipath_swqe *wqe;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001068 unsigned long flags;
1069 struct ib_wc wc;
1070 int diff;
1071 u32 pad;
1072 u32 aeth;
1073
1074 spin_lock_irqsave(&qp->s_lock, flags);
1075
1076 /* Ignore invalid responses. */
1077 if (ipath_cmp24(psn, qp->s_next_psn) >= 0)
1078 goto ack_done;
1079
1080 /* Ignore duplicate responses. */
1081 diff = ipath_cmp24(psn, qp->s_last_psn);
1082 if (unlikely(diff <= 0)) {
1083 /* Update credits for "ghost" ACKs */
1084 if (diff == 0 && opcode == OP(ACKNOWLEDGE)) {
1085 if (!header_in_data)
1086 aeth = be32_to_cpu(ohdr->u.aeth);
1087 else {
1088 aeth = be32_to_cpu(((__be32 *) data)[0]);
1089 data += sizeof(__be32);
1090 }
1091 if ((aeth >> 29) == 0)
1092 ipath_get_credit(qp, aeth);
1093 }
1094 goto ack_done;
1095 }
1096
Ralph Campbell3859e392007-03-15 14:44:51 -07001097 if (unlikely(qp->s_last == qp->s_tail))
1098 goto ack_done;
1099 wqe = get_swqe_ptr(qp, qp->s_last);
1100
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001101 switch (opcode) {
1102 case OP(ACKNOWLEDGE):
1103 case OP(ATOMIC_ACKNOWLEDGE):
1104 case OP(RDMA_READ_RESPONSE_FIRST):
1105 if (!header_in_data)
1106 aeth = be32_to_cpu(ohdr->u.aeth);
1107 else {
1108 aeth = be32_to_cpu(((__be32 *) data)[0]);
1109 data += sizeof(__be32);
1110 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001111 if (opcode == OP(ATOMIC_ACKNOWLEDGE)) {
1112 u64 val;
1113
1114 if (!header_in_data) {
1115 __be32 *p = ohdr->u.at.atomic_ack_eth;
1116
1117 val = ((u64) be32_to_cpu(p[0]) << 32) |
1118 be32_to_cpu(p[1]);
1119 } else
1120 val = be64_to_cpu(((__be64 *) data)[0]);
1121 *(u64 *) wqe->sg_list[0].vaddr = val;
1122 }
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001123 if (!do_rc_ack(qp, aeth, psn, opcode) ||
1124 opcode != OP(RDMA_READ_RESPONSE_FIRST))
1125 goto ack_done;
1126 hdrsize += 4;
Ralph Campbell3859e392007-03-15 14:44:51 -07001127 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1128 goto ack_done;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001129 /*
Ralph Campbell3859e392007-03-15 14:44:51 -07001130 * If this is a response to a resent RDMA read, we
1131 * have to be careful to copy the data to the right
1132 * location.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001133 */
Ralph Campbell3859e392007-03-15 14:44:51 -07001134 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1135 wqe, psn, pmtu);
1136 goto read_middle;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001137
1138 case OP(RDMA_READ_RESPONSE_MIDDLE):
1139 /* no AETH, no ACK */
1140 if (unlikely(ipath_cmp24(psn, qp->s_last_psn + 1))) {
1141 dev->n_rdma_seq++;
Ralph Campbell3859e392007-03-15 14:44:51 -07001142 ipath_restart_rc(qp, qp->s_last_psn + 1, &wc);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001143 goto ack_done;
1144 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001145 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
Bryan O'Sullivan7bbb15e2006-07-01 04:35:51 -07001146 goto ack_done;
Ralph Campbell3859e392007-03-15 14:44:51 -07001147 read_middle:
Bryan O'Sullivan7bbb15e2006-07-01 04:35:51 -07001148 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1149 goto ack_done;
Ralph Campbell3859e392007-03-15 14:44:51 -07001150 if (unlikely(pmtu >= qp->s_rdma_read_len))
Bryan O'Sullivan7bbb15e2006-07-01 04:35:51 -07001151 goto ack_done;
Ralph Campbell3859e392007-03-15 14:44:51 -07001152
Bryan O'Sullivan7bbb15e2006-07-01 04:35:51 -07001153 /* We got a response so update the timeout. */
Bryan O'Sullivan7bbb15e2006-07-01 04:35:51 -07001154 spin_lock(&dev->pending_lock);
1155 if (qp->s_rnr_timeout == 0 && !list_empty(&qp->timerwait))
1156 list_move_tail(&qp->timerwait,
1157 &dev->pending[dev->pending_index]);
1158 spin_unlock(&dev->pending_lock);
1159 /*
Bryan O'Sullivan6700efd2006-07-01 04:35:51 -07001160 * Update the RDMA receive state but do the copy w/o
1161 * holding the locks and blocking interrupts.
Bryan O'Sullivan7bbb15e2006-07-01 04:35:51 -07001162 */
Ralph Campbell3859e392007-03-15 14:44:51 -07001163 qp->s_rdma_read_len -= pmtu;
Bryan O'Sullivan60229432006-09-28 08:59:57 -07001164 update_last_psn(qp, psn);
Bryan O'Sullivan7bbb15e2006-07-01 04:35:51 -07001165 spin_unlock_irqrestore(&qp->s_lock, flags);
Ralph Campbell3859e392007-03-15 14:44:51 -07001166 ipath_copy_sge(&qp->s_rdma_read_sge, data, pmtu);
Bryan O'Sullivan7bbb15e2006-07-01 04:35:51 -07001167 goto bail;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001168
Ralph Campbell3859e392007-03-15 14:44:51 -07001169 case OP(RDMA_READ_RESPONSE_ONLY):
1170 if (unlikely(ipath_cmp24(psn, qp->s_last_psn + 1))) {
1171 dev->n_rdma_seq++;
1172 ipath_restart_rc(qp, qp->s_last_psn + 1, &wc);
1173 goto ack_done;
1174 }
1175 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
1176 goto ack_done;
1177 /*
1178 * If this is a response to a resent RDMA read, we
1179 * have to be careful to copy the data to the right
1180 * location.
1181 * XXX should check PSN and wqe opcode first.
1182 */
1183 qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
1184 wqe, psn, pmtu);
1185 goto read_last;
1186
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001187 case OP(RDMA_READ_RESPONSE_LAST):
1188 /* ACKs READ req. */
1189 if (unlikely(ipath_cmp24(psn, qp->s_last_psn + 1))) {
1190 dev->n_rdma_seq++;
Ralph Campbell3859e392007-03-15 14:44:51 -07001191 ipath_restart_rc(qp, qp->s_last_psn + 1, &wc);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001192 goto ack_done;
1193 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001194 if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001195 goto ack_done;
Ralph Campbell3859e392007-03-15 14:44:51 -07001196 read_last:
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001197 /*
1198 * Get the number of bytes the message was padded by.
1199 */
1200 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1201 /*
1202 * Check that the data size is >= 1 && <= pmtu.
1203 * Remember to account for the AETH header (4) and
1204 * ICRC (4).
1205 */
1206 if (unlikely(tlen <= (hdrsize + pad + 8))) {
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -07001207 /* XXX Need to generate an error CQ entry. */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001208 goto ack_done;
1209 }
1210 tlen -= hdrsize + pad + 8;
Ralph Campbell3859e392007-03-15 14:44:51 -07001211 if (unlikely(tlen != qp->s_rdma_read_len)) {
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -07001212 /* XXX Need to generate an error CQ entry. */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001213 goto ack_done;
1214 }
1215 if (!header_in_data)
1216 aeth = be32_to_cpu(ohdr->u.aeth);
1217 else {
1218 aeth = be32_to_cpu(((__be32 *) data)[0]);
1219 data += sizeof(__be32);
1220 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001221 ipath_copy_sge(&qp->s_rdma_read_sge, data, tlen);
1222 (void) do_rc_ack(qp, aeth, psn, OP(RDMA_READ_RESPONSE_LAST));
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001223 goto ack_done;
1224 }
1225
1226ack_done:
1227 spin_unlock_irqrestore(&qp->s_lock, flags);
1228bail:
1229 return;
1230}
1231
1232/**
1233 * ipath_rc_rcv_error - process an incoming duplicate or error RC packet
1234 * @dev: the device this packet came in on
1235 * @ohdr: the other headers for this packet
1236 * @data: the packet data
1237 * @qp: the QP for this packet
1238 * @opcode: the opcode for this packet
1239 * @psn: the packet sequence number for this packet
1240 * @diff: the difference between the PSN and the expected PSN
1241 * @header_in_data: true if part of the header data is in the data buffer
1242 *
1243 * This is called from ipath_rc_rcv() to process an unexpected
1244 * incoming RC packet for the given QP.
1245 * Called at interrupt level.
1246 * Return 1 if no more processing is needed; otherwise return 0 to
Ralph Campbell3859e392007-03-15 14:44:51 -07001247 * schedule a response to be sent.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001248 */
1249static inline int ipath_rc_rcv_error(struct ipath_ibdev *dev,
1250 struct ipath_other_headers *ohdr,
1251 void *data,
1252 struct ipath_qp *qp,
1253 u32 opcode,
1254 u32 psn,
1255 int diff,
1256 int header_in_data)
1257{
Ralph Campbell3859e392007-03-15 14:44:51 -07001258 struct ipath_ack_entry *e;
1259 u8 i, prev;
1260 int old_req;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001261
1262 if (diff > 0) {
1263 /*
1264 * Packet sequence error.
1265 * A NAK will ACK earlier sends and RDMA writes.
Ralph Campbell3859e392007-03-15 14:44:51 -07001266 * Don't queue the NAK if we already sent one.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001267 */
Ralph Campbell3859e392007-03-15 14:44:51 -07001268 if (!qp->r_nak_state) {
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001269 qp->r_nak_state = IB_NAK_PSN_ERROR;
1270 /* Use the expected PSN. */
1271 qp->r_ack_psn = qp->r_psn;
Ralph Campbell3859e392007-03-15 14:44:51 -07001272 goto send_ack;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001273 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001274 goto done;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001275 }
1276
1277 /*
1278 * Handle a duplicate request. Don't re-execute SEND, RDMA
1279 * write or atomic op. Don't NAK errors, just silently drop
1280 * the duplicate request. Note that r_sge, r_len, and
1281 * r_rcv_len may be in use so don't modify them.
1282 *
1283 * We are supposed to ACK the earliest duplicate PSN but we
1284 * can coalesce an outstanding duplicate ACK. We have to
1285 * send the earliest so that RDMA reads can be restarted at
1286 * the requester's expected PSN.
Ralph Campbell3859e392007-03-15 14:44:51 -07001287 *
1288 * First, find where this duplicate PSN falls within the
1289 * ACKs previously sent.
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001290 */
Ralph Campbell3859e392007-03-15 14:44:51 -07001291 psn &= IPATH_PSN_MASK;
1292 e = NULL;
1293 old_req = 1;
1294 spin_lock_irq(&qp->s_lock);
1295 for (i = qp->r_head_ack_queue; ; i = prev) {
1296 if (i == qp->s_tail_ack_queue)
1297 old_req = 0;
1298 if (i)
1299 prev = i - 1;
1300 else
1301 prev = IPATH_MAX_RDMA_ATOMIC;
1302 if (prev == qp->r_head_ack_queue) {
1303 e = NULL;
1304 break;
1305 }
1306 e = &qp->s_ack_queue[prev];
1307 if (!e->opcode) {
1308 e = NULL;
1309 break;
1310 }
1311 if (ipath_cmp24(psn, e->psn) >= 0)
1312 break;
1313 }
1314 switch (opcode) {
1315 case OP(RDMA_READ_REQUEST): {
1316 struct ib_reth *reth;
1317 u32 offset;
1318 u32 len;
1319
1320 /*
1321 * If we didn't find the RDMA read request in the ack queue,
1322 * or the send tasklet is already backed up to send an
1323 * earlier entry, we can ignore this request.
1324 */
1325 if (!e || e->opcode != OP(RDMA_READ_REQUEST) || old_req)
1326 goto unlock_done;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001327 /* RETH comes after BTH */
1328 if (!header_in_data)
1329 reth = &ohdr->u.rc.reth;
1330 else {
1331 reth = (struct ib_reth *)data;
1332 data += sizeof(*reth);
1333 }
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001334 /*
Ralph Campbell3859e392007-03-15 14:44:51 -07001335 * Address range must be a subset of the original
1336 * request and start on pmtu boundaries.
1337 * We reuse the old ack_queue slot since the requester
1338 * should not back up and request an earlier PSN for the
1339 * same request.
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001340 */
Ralph Campbell3859e392007-03-15 14:44:51 -07001341 offset = ((psn - e->psn) & IPATH_PSN_MASK) *
1342 ib_mtu_enum_to_int(qp->path_mtu);
1343 len = be32_to_cpu(reth->length);
1344 if (unlikely(offset + len > e->rdma_sge.sge.sge_length))
1345 goto unlock_done;
1346 if (len != 0) {
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001347 u32 rkey = be32_to_cpu(reth->rkey);
1348 u64 vaddr = be64_to_cpu(reth->vaddr);
1349 int ok;
1350
Ralph Campbell3859e392007-03-15 14:44:51 -07001351 ok = ipath_rkey_ok(qp, &e->rdma_sge,
1352 len, vaddr, rkey,
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001353 IB_ACCESS_REMOTE_READ);
Ralph Campbell3859e392007-03-15 14:44:51 -07001354 if (unlikely(!ok))
1355 goto unlock_done;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001356 } else {
Ralph Campbell3859e392007-03-15 14:44:51 -07001357 e->rdma_sge.sg_list = NULL;
1358 e->rdma_sge.num_sge = 0;
1359 e->rdma_sge.sge.mr = NULL;
1360 e->rdma_sge.sge.vaddr = NULL;
1361 e->rdma_sge.sge.length = 0;
1362 e->rdma_sge.sge.sge_length = 0;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001363 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001364 e->psn = psn;
1365 qp->s_ack_state = OP(ACKNOWLEDGE);
1366 qp->s_tail_ack_queue = prev;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001367 break;
1368 }
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001369
Ralph Campbell3859e392007-03-15 14:44:51 -07001370 case OP(COMPARE_SWAP):
1371 case OP(FETCH_ADD): {
1372 /*
1373 * If we didn't find the atomic request in the ack queue
1374 * or the send tasklet is already backed up to send an
1375 * earlier entry, we can ignore this request.
1376 */
1377 if (!e || e->opcode != (u8) opcode || old_req)
1378 goto unlock_done;
1379 qp->s_ack_state = OP(ACKNOWLEDGE);
1380 qp->s_tail_ack_queue = prev;
1381 break;
1382 }
1383
1384 default:
1385 if (old_req)
1386 goto unlock_done;
1387 /*
1388 * Resend the most recent ACK if this request is
1389 * after all the previous RDMA reads and atomics.
1390 */
1391 if (i == qp->r_head_ack_queue) {
1392 spin_unlock_irq(&qp->s_lock);
1393 qp->r_nak_state = 0;
1394 qp->r_ack_psn = qp->r_psn - 1;
1395 goto send_ack;
1396 }
1397 /*
1398 * Resend the RDMA read or atomic op which
1399 * ACKs this duplicate request.
1400 */
1401 qp->s_ack_state = OP(ACKNOWLEDGE);
1402 qp->s_tail_ack_queue = i;
1403 break;
1404 }
1405 qp->r_nak_state = 0;
1406 spin_unlock_irq(&qp->s_lock);
1407 tasklet_hi_schedule(&qp->s_task);
1408
1409unlock_done:
1410 spin_unlock_irq(&qp->s_lock);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001411done:
1412 return 1;
Ralph Campbell3859e392007-03-15 14:44:51 -07001413
1414send_ack:
1415 return 0;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001416}
1417
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -07001418static void ipath_rc_error(struct ipath_qp *qp, enum ib_wc_status err)
1419{
1420 spin_lock_irq(&qp->s_lock);
1421 qp->state = IB_QPS_ERR;
1422 ipath_error_qp(qp, err);
1423 spin_unlock_irq(&qp->s_lock);
1424}
1425
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001426/**
1427 * ipath_rc_rcv - process an incoming RC packet
1428 * @dev: the device this packet came in on
1429 * @hdr: the header of this packet
1430 * @has_grh: true if the header has a GRH
1431 * @data: the packet data
1432 * @tlen: the packet length
1433 * @qp: the QP for this packet
1434 *
1435 * This is called from ipath_qp_rcv() to process an incoming RC packet
1436 * for the given QP.
1437 * Called at interrupt level.
1438 */
1439void ipath_rc_rcv(struct ipath_ibdev *dev, struct ipath_ib_header *hdr,
1440 int has_grh, void *data, u32 tlen, struct ipath_qp *qp)
1441{
1442 struct ipath_other_headers *ohdr;
1443 u32 opcode;
1444 u32 hdrsize;
1445 u32 psn;
1446 u32 pad;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001447 struct ib_wc wc;
1448 u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
1449 int diff;
1450 struct ib_reth *reth;
1451 int header_in_data;
1452
Bryan O'Sullivan10aeb0e2006-09-28 09:00:06 -07001453 /* Validate the SLID. See Ch. 9.6.1.5 */
1454 if (unlikely(be16_to_cpu(hdr->lrh[3]) != qp->remote_ah_attr.dlid))
1455 goto done;
1456
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001457 /* Check for GRH */
1458 if (!has_grh) {
1459 ohdr = &hdr->u.oth;
1460 hdrsize = 8 + 12; /* LRH + BTH */
1461 psn = be32_to_cpu(ohdr->bth[2]);
1462 header_in_data = 0;
1463 } else {
1464 ohdr = &hdr->u.l.oth;
1465 hdrsize = 8 + 40 + 12; /* LRH + GRH + BTH */
1466 /*
1467 * The header with GRH is 60 bytes and the core driver sets
1468 * the eager header buffer size to 56 bytes so the last 4
1469 * bytes of the BTH header (PSN) is in the data buffer.
1470 */
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001471 header_in_data = dev->dd->ipath_rcvhdrentsize == 16;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001472 if (header_in_data) {
1473 psn = be32_to_cpu(((__be32 *) data)[0]);
1474 data += sizeof(__be32);
1475 } else
1476 psn = be32_to_cpu(ohdr->bth[2]);
1477 }
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001478
1479 /*
1480 * Process responses (ACKs) before anything else. Note that the
1481 * packet sequence number will be for something in the send work
1482 * queue rather than the expected receive packet sequence number.
1483 * In other words, this QP is the requester.
1484 */
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -07001485 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001486 if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
1487 opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
1488 ipath_rc_rcv_resp(dev, ohdr, data, tlen, qp, opcode, psn,
1489 hdrsize, pmtu, header_in_data);
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001490 goto done;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001491 }
1492
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001493 /* Compute 24 bits worth of difference. */
1494 diff = ipath_cmp24(psn, qp->r_psn);
1495 if (unlikely(diff)) {
1496 if (ipath_rc_rcv_error(dev, ohdr, data, qp, opcode,
1497 psn, diff, header_in_data))
1498 goto done;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001499 goto send_ack;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001500 }
1501
1502 /* Check for opcode sequence errors. */
1503 switch (qp->r_state) {
1504 case OP(SEND_FIRST):
1505 case OP(SEND_MIDDLE):
1506 if (opcode == OP(SEND_MIDDLE) ||
1507 opcode == OP(SEND_LAST) ||
1508 opcode == OP(SEND_LAST_WITH_IMMEDIATE))
1509 break;
1510 nack_inv:
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -07001511 ipath_rc_error(qp, IB_WC_REM_INV_REQ_ERR);
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001512 qp->r_nak_state = IB_NAK_INVALID_REQUEST;
1513 qp->r_ack_psn = qp->r_psn;
1514 goto send_ack;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001515
1516 case OP(RDMA_WRITE_FIRST):
1517 case OP(RDMA_WRITE_MIDDLE):
1518 if (opcode == OP(RDMA_WRITE_MIDDLE) ||
1519 opcode == OP(RDMA_WRITE_LAST) ||
1520 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
1521 break;
1522 goto nack_inv;
1523
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001524 default:
1525 if (opcode == OP(SEND_MIDDLE) ||
1526 opcode == OP(SEND_LAST) ||
1527 opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
1528 opcode == OP(RDMA_WRITE_MIDDLE) ||
1529 opcode == OP(RDMA_WRITE_LAST) ||
1530 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
1531 goto nack_inv;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001532 /*
1533 * Note that it is up to the requester to not send a new
1534 * RDMA read or atomic operation before receiving an ACK
1535 * for the previous operation.
1536 */
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001537 break;
1538 }
1539
1540 wc.imm_data = 0;
1541 wc.wc_flags = 0;
1542
1543 /* OK, process the packet. */
1544 switch (opcode) {
1545 case OP(SEND_FIRST):
1546 if (!ipath_get_rwqe(qp, 0)) {
1547 rnr_nak:
1548 /*
1549 * A RNR NAK will ACK earlier sends and RDMA writes.
1550 * Don't queue the NAK if a RDMA read or atomic
1551 * is pending though.
1552 */
Ralph Campbell3859e392007-03-15 14:44:51 -07001553 if (qp->r_nak_state)
1554 goto done;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001555 qp->r_nak_state = IB_RNR_NAK | qp->r_min_rnr_timer;
1556 qp->r_ack_psn = qp->r_psn;
1557 goto send_ack;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001558 }
1559 qp->r_rcv_len = 0;
1560 /* FALLTHROUGH */
1561 case OP(SEND_MIDDLE):
1562 case OP(RDMA_WRITE_MIDDLE):
1563 send_middle:
1564 /* Check for invalid length PMTU or posted rwqe len. */
1565 if (unlikely(tlen != (hdrsize + pmtu + 4)))
1566 goto nack_inv;
1567 qp->r_rcv_len += pmtu;
1568 if (unlikely(qp->r_rcv_len > qp->r_len))
1569 goto nack_inv;
1570 ipath_copy_sge(&qp->r_sge, data, pmtu);
1571 break;
1572
1573 case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
1574 /* consume RWQE */
1575 if (!ipath_get_rwqe(qp, 1))
1576 goto rnr_nak;
1577 goto send_last_imm;
1578
1579 case OP(SEND_ONLY):
1580 case OP(SEND_ONLY_WITH_IMMEDIATE):
1581 if (!ipath_get_rwqe(qp, 0))
1582 goto rnr_nak;
1583 qp->r_rcv_len = 0;
1584 if (opcode == OP(SEND_ONLY))
1585 goto send_last;
1586 /* FALLTHROUGH */
1587 case OP(SEND_LAST_WITH_IMMEDIATE):
1588 send_last_imm:
1589 if (header_in_data) {
1590 wc.imm_data = *(__be32 *) data;
1591 data += sizeof(__be32);
1592 } else {
1593 /* Immediate data comes after BTH */
1594 wc.imm_data = ohdr->u.imm_data;
1595 }
1596 hdrsize += 4;
1597 wc.wc_flags = IB_WC_WITH_IMM;
1598 /* FALLTHROUGH */
1599 case OP(SEND_LAST):
1600 case OP(RDMA_WRITE_LAST):
1601 send_last:
1602 /* Get the number of bytes the message was padded by. */
1603 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
1604 /* Check for invalid length. */
1605 /* XXX LAST len should be >= 1 */
1606 if (unlikely(tlen < (hdrsize + pad + 4)))
1607 goto nack_inv;
1608 /* Don't count the CRC. */
1609 tlen -= (hdrsize + pad + 4);
1610 wc.byte_len = tlen + qp->r_rcv_len;
1611 if (unlikely(wc.byte_len > qp->r_len))
1612 goto nack_inv;
1613 ipath_copy_sge(&qp->r_sge, data, tlen);
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001614 qp->r_msn++;
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -07001615 if (!qp->r_wrid_valid)
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001616 break;
Bryan O'Sullivan8d0208c2006-09-28 09:00:14 -07001617 qp->r_wrid_valid = 0;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001618 wc.wr_id = qp->r_wr_id;
1619 wc.status = IB_WC_SUCCESS;
1620 wc.opcode = IB_WC_RECV;
1621 wc.vendor_err = 0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +02001622 wc.qp = &qp->ibqp;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001623 wc.src_qp = qp->remote_qpn;
1624 wc.pkey_index = 0;
1625 wc.slid = qp->remote_ah_attr.dlid;
1626 wc.sl = qp->remote_ah_attr.sl;
1627 wc.dlid_path_bits = 0;
1628 wc.port_num = 0;
1629 /* Signal completion event if the solicited bit is set. */
1630 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
1631 (ohdr->bth[0] &
1632 __constant_cpu_to_be32(1 << 23)) != 0);
1633 break;
1634
1635 case OP(RDMA_WRITE_FIRST):
1636 case OP(RDMA_WRITE_ONLY):
1637 case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
1638 /* consume RWQE */
1639 /* RETH comes after BTH */
1640 if (!header_in_data)
1641 reth = &ohdr->u.rc.reth;
1642 else {
1643 reth = (struct ib_reth *)data;
1644 data += sizeof(*reth);
1645 }
1646 hdrsize += sizeof(*reth);
1647 qp->r_len = be32_to_cpu(reth->length);
1648 qp->r_rcv_len = 0;
1649 if (qp->r_len != 0) {
1650 u32 rkey = be32_to_cpu(reth->rkey);
1651 u64 vaddr = be64_to_cpu(reth->vaddr);
1652 int ok;
1653
1654 /* Check rkey & NAK */
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -07001655 ok = ipath_rkey_ok(qp, &qp->r_sge,
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001656 qp->r_len, vaddr, rkey,
1657 IB_ACCESS_REMOTE_WRITE);
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001658 if (unlikely(!ok))
1659 goto nack_acc;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001660 } else {
1661 qp->r_sge.sg_list = NULL;
1662 qp->r_sge.sge.mr = NULL;
1663 qp->r_sge.sge.vaddr = NULL;
1664 qp->r_sge.sge.length = 0;
1665 qp->r_sge.sge.sge_length = 0;
1666 }
1667 if (unlikely(!(qp->qp_access_flags &
1668 IB_ACCESS_REMOTE_WRITE)))
1669 goto nack_acc;
1670 if (opcode == OP(RDMA_WRITE_FIRST))
1671 goto send_middle;
1672 else if (opcode == OP(RDMA_WRITE_ONLY))
1673 goto send_last;
1674 if (!ipath_get_rwqe(qp, 1))
1675 goto rnr_nak;
1676 goto send_last_imm;
1677
Ralph Campbell3859e392007-03-15 14:44:51 -07001678 case OP(RDMA_READ_REQUEST): {
1679 struct ipath_ack_entry *e;
1680 u32 len;
1681 u8 next;
1682
1683 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
1684 goto nack_acc;
1685 next = qp->r_head_ack_queue + 1;
1686 if (next > IPATH_MAX_RDMA_ATOMIC)
1687 next = 0;
1688 if (unlikely(next == qp->s_tail_ack_queue))
1689 goto nack_inv;
1690 e = &qp->s_ack_queue[qp->r_head_ack_queue];
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001691 /* RETH comes after BTH */
1692 if (!header_in_data)
1693 reth = &ohdr->u.rc.reth;
1694 else {
1695 reth = (struct ib_reth *)data;
1696 data += sizeof(*reth);
1697 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001698 len = be32_to_cpu(reth->length);
1699 if (len) {
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001700 u32 rkey = be32_to_cpu(reth->rkey);
1701 u64 vaddr = be64_to_cpu(reth->vaddr);
1702 int ok;
1703
1704 /* Check rkey & NAK */
Ralph Campbell3859e392007-03-15 14:44:51 -07001705 ok = ipath_rkey_ok(qp, &e->rdma_sge, len, vaddr,
1706 rkey, IB_ACCESS_REMOTE_READ);
1707 if (unlikely(!ok))
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001708 goto nack_acc;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001709 /*
1710 * Update the next expected PSN. We add 1 later
1711 * below, so only add the remainder here.
1712 */
Ralph Campbell3859e392007-03-15 14:44:51 -07001713 if (len > pmtu)
1714 qp->r_psn += (len - 1) / pmtu;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001715 } else {
Ralph Campbell3859e392007-03-15 14:44:51 -07001716 e->rdma_sge.sg_list = NULL;
1717 e->rdma_sge.num_sge = 0;
1718 e->rdma_sge.sge.mr = NULL;
1719 e->rdma_sge.sge.vaddr = NULL;
1720 e->rdma_sge.sge.length = 0;
1721 e->rdma_sge.sge.sge_length = 0;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001722 }
Ralph Campbell3859e392007-03-15 14:44:51 -07001723 e->opcode = opcode;
1724 e->psn = psn;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001725 /*
1726 * We need to increment the MSN here instead of when we
1727 * finish sending the result since a duplicate request would
1728 * increment it more than once.
1729 */
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001730 qp->r_msn++;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001731 qp->r_psn++;
1732 qp->r_state = opcode;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001733 qp->r_nak_state = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -07001734 barrier();
1735 qp->r_head_ack_queue = next;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001736
1737 /* Call ipath_do_rc_send() in another thread. */
1738 tasklet_hi_schedule(&qp->s_task);
1739
1740 goto done;
Ralph Campbell3859e392007-03-15 14:44:51 -07001741 }
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001742
1743 case OP(COMPARE_SWAP):
1744 case OP(FETCH_ADD): {
1745 struct ib_atomic_eth *ateth;
Ralph Campbell3859e392007-03-15 14:44:51 -07001746 struct ipath_ack_entry *e;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001747 u64 vaddr;
Ralph Campbell3859e392007-03-15 14:44:51 -07001748 atomic64_t *maddr;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001749 u64 sdata;
1750 u32 rkey;
Ralph Campbell3859e392007-03-15 14:44:51 -07001751 u8 next;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001752
Ralph Campbell3859e392007-03-15 14:44:51 -07001753 if (unlikely(!(qp->qp_access_flags &
1754 IB_ACCESS_REMOTE_ATOMIC)))
1755 goto nack_acc;
1756 next = qp->r_head_ack_queue + 1;
1757 if (next > IPATH_MAX_RDMA_ATOMIC)
1758 next = 0;
1759 if (unlikely(next == qp->s_tail_ack_queue))
1760 goto nack_inv;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001761 if (!header_in_data)
1762 ateth = &ohdr->u.atomic_eth;
Ralph Campbell3859e392007-03-15 14:44:51 -07001763 else
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001764 ateth = (struct ib_atomic_eth *)data;
Ralph Campbell3859e392007-03-15 14:44:51 -07001765 vaddr = ((u64) be32_to_cpu(ateth->vaddr[0]) << 32) |
1766 be32_to_cpu(ateth->vaddr[1]);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001767 if (unlikely(vaddr & (sizeof(u64) - 1)))
1768 goto nack_inv;
1769 rkey = be32_to_cpu(ateth->rkey);
1770 /* Check rkey & NAK */
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -07001771 if (unlikely(!ipath_rkey_ok(qp, &qp->r_sge,
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001772 sizeof(u64), vaddr, rkey,
1773 IB_ACCESS_REMOTE_ATOMIC)))
1774 goto nack_acc;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001775 /* Perform atomic OP and save result. */
Ralph Campbell3859e392007-03-15 14:44:51 -07001776 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001777 sdata = be64_to_cpu(ateth->swap_data);
Ralph Campbell3859e392007-03-15 14:44:51 -07001778 e = &qp->s_ack_queue[qp->r_head_ack_queue];
1779 e->atomic_data = (opcode == OP(FETCH_ADD)) ?
1780 (u64) atomic64_add_return(sdata, maddr) - sdata :
1781 (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
1782 be64_to_cpu(ateth->compare_data),
1783 sdata);
1784 e->opcode = opcode;
1785 e->psn = psn & IPATH_PSN_MASK;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001786 qp->r_msn++;
Ralph Campbell3859e392007-03-15 14:44:51 -07001787 qp->r_psn++;
1788 qp->r_state = opcode;
1789 qp->r_nak_state = 0;
1790 barrier();
1791 qp->r_head_ack_queue = next;
1792
1793 /* Call ipath_do_rc_send() in another thread. */
1794 tasklet_hi_schedule(&qp->s_task);
1795
1796 goto done;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001797 }
1798
1799 default:
Ralph Campbell3859e392007-03-15 14:44:51 -07001800 /* NAK unknown opcodes. */
1801 goto nack_inv;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001802 }
1803 qp->r_psn++;
1804 qp->r_state = opcode;
Ralph Campbell3859e392007-03-15 14:44:51 -07001805 qp->r_ack_psn = psn;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001806 qp->r_nak_state = 0;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001807 /* Send an ACK if requested or required. */
Ralph Campbell3859e392007-03-15 14:44:51 -07001808 if (psn & (1 << 31))
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001809 goto send_ack;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001810 goto done;
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001811
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001812nack_acc:
Ralph Campbell3859e392007-03-15 14:44:51 -07001813 ipath_rc_error(qp, IB_WC_REM_ACCESS_ERR);
1814 qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
1815 qp->r_ack_psn = qp->r_psn;
1816
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001817send_ack:
Ralph Campbell3859e392007-03-15 14:44:51 -07001818 send_rc_ack(qp);
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001819
Bryan O'Sullivan12eef412006-07-01 04:36:10 -07001820done:
Bryan O'Sullivan97f9efb2006-03-29 15:23:35 -08001821 return;
1822}