blob: 4b6b7ee8e5c14f4dda64e3ae0fb05e784b07136e [file] [log] [blame]
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -08001/*
John Gregor87427da2007-06-11 10:21:14 -07002 * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -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
Ralph Campbell4ee97182007-07-25 11:08:28 -070034#include <linux/spinlock.h>
35
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -080036#include "ipath_verbs.h"
Ralph Campbell373d9912006-09-22 15:22:26 -070037#include "ipath_kernel.h"
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -080038
39/*
40 * Convert the AETH RNR timeout code into the number of milliseconds.
41 */
42const u32 ib_ipath_rnr_table[32] = {
43 656, /* 0 */
44 1, /* 1 */
45 1, /* 2 */
46 1, /* 3 */
47 1, /* 4 */
48 1, /* 5 */
49 1, /* 6 */
50 1, /* 7 */
51 1, /* 8 */
52 1, /* 9 */
53 1, /* A */
54 1, /* B */
55 1, /* C */
56 1, /* D */
57 2, /* E */
58 2, /* F */
59 3, /* 10 */
60 4, /* 11 */
61 6, /* 12 */
62 8, /* 13 */
63 11, /* 14 */
64 16, /* 15 */
65 21, /* 16 */
66 31, /* 17 */
67 41, /* 18 */
68 62, /* 19 */
69 82, /* 1A */
70 123, /* 1B */
71 164, /* 1C */
72 246, /* 1D */
73 328, /* 1E */
74 492 /* 1F */
75};
76
77/**
78 * ipath_insert_rnr_queue - put QP on the RNR timeout list for the device
79 * @qp: the QP
80 *
81 * XXX Use a simple list for now. We might need a priority
82 * queue if we have lots of QPs waiting for RNR timeouts
83 * but that should be rare.
84 */
85void ipath_insert_rnr_queue(struct ipath_qp *qp)
86{
87 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
88 unsigned long flags;
89
90 spin_lock_irqsave(&dev->pending_lock, flags);
91 if (list_empty(&dev->rnrwait))
92 list_add(&qp->timerwait, &dev->rnrwait);
93 else {
94 struct list_head *l = &dev->rnrwait;
95 struct ipath_qp *nqp = list_entry(l->next, struct ipath_qp,
96 timerwait);
97
98 while (qp->s_rnr_timeout >= nqp->s_rnr_timeout) {
99 qp->s_rnr_timeout -= nqp->s_rnr_timeout;
100 l = l->next;
101 if (l->next == &dev->rnrwait)
102 break;
103 nqp = list_entry(l->next, struct ipath_qp,
104 timerwait);
105 }
106 list_add(&qp->timerwait, l);
107 }
108 spin_unlock_irqrestore(&dev->pending_lock, flags);
109}
110
Ralph Campbell4ee97182007-07-25 11:08:28 -0700111/**
112 * ipath_init_sge - Validate a RWQE and fill in the SGE state
113 * @qp: the QP
114 *
115 * Return 1 if OK.
116 */
117int ipath_init_sge(struct ipath_qp *qp, struct ipath_rwqe *wqe,
118 u32 *lengthp, struct ipath_sge_state *ss)
Ralph Campbell373d9912006-09-22 15:22:26 -0700119{
Ralph Campbell373d9912006-09-22 15:22:26 -0700120 int i, j, ret;
121 struct ib_wc wc;
122
Ralph Campbell4ee97182007-07-25 11:08:28 -0700123 *lengthp = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700124 for (i = j = 0; i < wqe->num_sge; i++) {
125 if (wqe->sg_list[i].length == 0)
126 continue;
127 /* Check LKEY */
Ralph Campbell4ee97182007-07-25 11:08:28 -0700128 if (!ipath_lkey_ok(qp, j ? &ss->sg_list[j - 1] : &ss->sge,
129 &wqe->sg_list[i], IB_ACCESS_LOCAL_WRITE))
Ralph Campbell373d9912006-09-22 15:22:26 -0700130 goto bad_lkey;
Ralph Campbell4ee97182007-07-25 11:08:28 -0700131 *lengthp += wqe->sg_list[i].length;
Ralph Campbell373d9912006-09-22 15:22:26 -0700132 j++;
133 }
Ralph Campbell4ee97182007-07-25 11:08:28 -0700134 ss->num_sge = j;
Ralph Campbell373d9912006-09-22 15:22:26 -0700135 ret = 1;
136 goto bail;
137
138bad_lkey:
139 wc.wr_id = wqe->wr_id;
140 wc.status = IB_WC_LOC_PROT_ERR;
141 wc.opcode = IB_WC_RECV;
142 wc.vendor_err = 0;
143 wc.byte_len = 0;
144 wc.imm_data = 0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200145 wc.qp = &qp->ibqp;
Ralph Campbell373d9912006-09-22 15:22:26 -0700146 wc.src_qp = 0;
147 wc.wc_flags = 0;
148 wc.pkey_index = 0;
149 wc.slid = 0;
150 wc.sl = 0;
151 wc.dlid_path_bits = 0;
152 wc.port_num = 0;
153 /* Signal solicited completion event. */
154 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
155 ret = 0;
156bail:
157 return ret;
158}
159
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800160/**
161 * ipath_get_rwqe - copy the next RWQE into the QP's RWQE
162 * @qp: the QP
163 * @wr_id_only: update wr_id only, not SGEs
164 *
165 * Return 0 if no RWQE is available, otherwise return 1.
166 *
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700167 * Can be called from interrupt level.
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800168 */
169int ipath_get_rwqe(struct ipath_qp *qp, int wr_id_only)
170{
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700171 unsigned long flags;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800172 struct ipath_rq *rq;
Ralph Campbell373d9912006-09-22 15:22:26 -0700173 struct ipath_rwq *wq;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800174 struct ipath_srq *srq;
175 struct ipath_rwqe *wqe;
Ralph Campbell373d9912006-09-22 15:22:26 -0700176 void (*handler)(struct ib_event *, void *);
177 u32 tail;
178 int ret;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800179
Ralph Campbell4ee97182007-07-25 11:08:28 -0700180 qp->r_sge.sg_list = qp->r_sg_list;
181
Ralph Campbell373d9912006-09-22 15:22:26 -0700182 if (qp->ibqp.srq) {
183 srq = to_isrq(qp->ibqp.srq);
184 handler = srq->ibsrq.event_handler;
185 rq = &srq->rq;
186 } else {
187 srq = NULL;
188 handler = NULL;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800189 rq = &qp->r_rq;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800190 }
191
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700192 spin_lock_irqsave(&rq->lock, flags);
Ralph Campbell373d9912006-09-22 15:22:26 -0700193 wq = rq->wq;
194 tail = wq->tail;
195 /* Validate tail before using it since it is user writable. */
196 if (tail >= rq->size)
197 tail = 0;
198 do {
199 if (unlikely(tail == wq->head)) {
200 spin_unlock_irqrestore(&rq->lock, flags);
201 ret = 0;
202 goto bail;
203 }
Ralph Campbell4fc570b2007-07-06 12:48:23 -0700204 /* Make sure entry is read after head index is read. */
205 smp_rmb();
Ralph Campbell373d9912006-09-22 15:22:26 -0700206 wqe = get_rwqe_ptr(rq, tail);
207 if (++tail >= rq->size)
208 tail = 0;
Ralph Campbell4ee97182007-07-25 11:08:28 -0700209 } while (!wr_id_only && !ipath_init_sge(qp, wqe, &qp->r_len,
210 &qp->r_sge));
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800211 qp->r_wr_id = wqe->wr_id;
Ralph Campbell373d9912006-09-22 15:22:26 -0700212 wq->tail = tail;
213
214 ret = 1;
Ralph Campbell9f9630d2007-03-15 14:44:49 -0700215 qp->r_wrid_valid = 1;
Ralph Campbell373d9912006-09-22 15:22:26 -0700216 if (handler) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800217 u32 n;
218
Ralph Campbell373d9912006-09-22 15:22:26 -0700219 /*
220 * validate head pointer value and compute
221 * the number of remaining WQEs.
222 */
223 n = wq->head;
224 if (n >= rq->size)
225 n = 0;
226 if (n < tail)
227 n += rq->size - tail;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800228 else
Ralph Campbell373d9912006-09-22 15:22:26 -0700229 n -= tail;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800230 if (n < srq->limit) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700231 struct ib_event ev;
232
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800233 srq->limit = 0;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700234 spin_unlock_irqrestore(&rq->lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800235 ev.device = qp->ibqp.device;
236 ev.element.srq = qp->ibqp.srq;
237 ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
Ralph Campbell373d9912006-09-22 15:22:26 -0700238 handler(&ev, srq->ibsrq.srq_context);
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700239 goto bail;
240 }
241 }
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700242 spin_unlock_irqrestore(&rq->lock, flags);
Ralph Campbell373d9912006-09-22 15:22:26 -0700243
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800244bail:
245 return ret;
246}
247
248/**
249 * ipath_ruc_loopback - handle UC and RC lookback requests
Ralph Campbell4ee97182007-07-25 11:08:28 -0700250 * @sqp: the sending QP
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800251 *
Ralph Campbell4ee97182007-07-25 11:08:28 -0700252 * This is called from ipath_do_send() to
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800253 * forward a WQE addressed to the same HCA.
254 * Note that although we are single threaded due to the tasklet, we still
255 * have to protect against post_send(). We don't have to worry about
256 * receive interrupts since this is a connected protocol and all packets
257 * will pass through here.
258 */
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700259static void ipath_ruc_loopback(struct ipath_qp *sqp)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800260{
261 struct ipath_ibdev *dev = to_idev(sqp->ibqp.device);
262 struct ipath_qp *qp;
263 struct ipath_swqe *wqe;
264 struct ipath_sge *sge;
265 unsigned long flags;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700266 struct ib_wc wc;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800267 u64 sdata;
Ralph Campbell3859e392007-03-15 14:44:51 -0700268 atomic64_t *maddr;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800269
270 qp = ipath_lookup_qpn(&dev->qp_table, sqp->remote_qpn);
271 if (!qp) {
272 dev->n_pkt_drops++;
273 return;
274 }
275
276again:
277 spin_lock_irqsave(&sqp->s_lock, flags);
278
Ralph Campbell7b21d262007-03-15 14:44:50 -0700279 if (!(ib_ipath_state_ops[sqp->state] & IPATH_PROCESS_SEND_OK) ||
Ralph Campbell6d2fad02007-06-18 14:24:38 -0700280 sqp->s_rnr_timeout) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800281 spin_unlock_irqrestore(&sqp->s_lock, flags);
282 goto done;
283 }
284
285 /* Get the next send request. */
286 if (sqp->s_last == sqp->s_head) {
287 /* Send work queue is empty. */
288 spin_unlock_irqrestore(&sqp->s_lock, flags);
289 goto done;
290 }
291
292 /*
293 * We can rely on the entry not changing without the s_lock
294 * being held until we update s_last.
295 */
296 wqe = get_swqe_ptr(sqp, sqp->s_last);
297 spin_unlock_irqrestore(&sqp->s_lock, flags);
298
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700299 wc.wc_flags = 0;
300 wc.imm_data = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800301
302 sqp->s_sge.sge = wqe->sg_list[0];
303 sqp->s_sge.sg_list = wqe->sg_list + 1;
304 sqp->s_sge.num_sge = wqe->wr.num_sge;
305 sqp->s_len = wqe->length;
306 switch (wqe->wr.opcode) {
307 case IB_WR_SEND_WITH_IMM:
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700308 wc.wc_flags = IB_WC_WITH_IMM;
309 wc.imm_data = wqe->wr.imm_data;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800310 /* FALLTHROUGH */
311 case IB_WR_SEND:
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800312 if (!ipath_get_rwqe(qp, 0)) {
313 rnr_nak:
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800314 /* Handle RNR NAK */
315 if (qp->ibqp.qp_type == IB_QPT_UC)
316 goto send_comp;
317 if (sqp->s_rnr_retry == 0) {
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700318 wc.status = IB_WC_RNR_RETRY_EXC_ERR;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800319 goto err;
320 }
321 if (sqp->s_rnr_retry_cnt < 7)
322 sqp->s_rnr_retry--;
323 dev->n_rnr_naks++;
324 sqp->s_rnr_timeout =
Ralph Campbell3859e392007-03-15 14:44:51 -0700325 ib_ipath_rnr_table[qp->r_min_rnr_timer];
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800326 ipath_insert_rnr_queue(sqp);
327 goto done;
328 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800329 break;
330
331 case IB_WR_RDMA_WRITE_WITH_IMM:
Robert Walshb506e1d2007-06-18 14:24:48 -0700332 if (unlikely(!(qp->qp_access_flags &
333 IB_ACCESS_REMOTE_WRITE))) {
334 wc.status = IB_WC_REM_INV_REQ_ERR;
335 goto err;
336 }
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700337 wc.wc_flags = IB_WC_WITH_IMM;
338 wc.imm_data = wqe->wr.imm_data;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800339 if (!ipath_get_rwqe(qp, 1))
340 goto rnr_nak;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800341 /* FALLTHROUGH */
342 case IB_WR_RDMA_WRITE:
Robert Walshb506e1d2007-06-18 14:24:48 -0700343 if (unlikely(!(qp->qp_access_flags &
344 IB_ACCESS_REMOTE_WRITE))) {
345 wc.status = IB_WC_REM_INV_REQ_ERR;
346 goto err;
347 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800348 if (wqe->length == 0)
349 break;
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700350 if (unlikely(!ipath_rkey_ok(qp, &qp->r_sge, wqe->length,
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800351 wqe->wr.wr.rdma.remote_addr,
352 wqe->wr.wr.rdma.rkey,
353 IB_ACCESS_REMOTE_WRITE))) {
354 acc_err:
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700355 wc.status = IB_WC_REM_ACCESS_ERR;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800356 err:
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700357 wc.wr_id = wqe->wr.wr_id;
358 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
359 wc.vendor_err = 0;
360 wc.byte_len = 0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200361 wc.qp = &sqp->ibqp;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700362 wc.src_qp = sqp->remote_qpn;
363 wc.pkey_index = 0;
364 wc.slid = sqp->remote_ah_attr.dlid;
365 wc.sl = sqp->remote_ah_attr.sl;
366 wc.dlid_path_bits = 0;
367 wc.port_num = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -0700368 spin_lock_irqsave(&sqp->s_lock, flags);
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700369 ipath_sqerror_qp(sqp, &wc);
Ralph Campbell3859e392007-03-15 14:44:51 -0700370 spin_unlock_irqrestore(&sqp->s_lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800371 goto done;
372 }
373 break;
374
375 case IB_WR_RDMA_READ:
Ralph Campbell3859e392007-03-15 14:44:51 -0700376 if (unlikely(!(qp->qp_access_flags &
Robert Walshb506e1d2007-06-18 14:24:48 -0700377 IB_ACCESS_REMOTE_READ))) {
378 wc.status = IB_WC_REM_INV_REQ_ERR;
379 goto err;
380 }
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700381 if (unlikely(!ipath_rkey_ok(qp, &sqp->s_sge, wqe->length,
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800382 wqe->wr.wr.rdma.remote_addr,
383 wqe->wr.wr.rdma.rkey,
384 IB_ACCESS_REMOTE_READ)))
385 goto acc_err;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800386 qp->r_sge.sge = wqe->sg_list[0];
387 qp->r_sge.sg_list = wqe->sg_list + 1;
388 qp->r_sge.num_sge = wqe->wr.num_sge;
389 break;
390
391 case IB_WR_ATOMIC_CMP_AND_SWP:
392 case IB_WR_ATOMIC_FETCH_AND_ADD:
Ralph Campbell3859e392007-03-15 14:44:51 -0700393 if (unlikely(!(qp->qp_access_flags &
Robert Walshb506e1d2007-06-18 14:24:48 -0700394 IB_ACCESS_REMOTE_ATOMIC))) {
395 wc.status = IB_WC_REM_INV_REQ_ERR;
396 goto err;
397 }
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700398 if (unlikely(!ipath_rkey_ok(qp, &qp->r_sge, sizeof(u64),
Ralph Campbell3859e392007-03-15 14:44:51 -0700399 wqe->wr.wr.atomic.remote_addr,
400 wqe->wr.wr.atomic.rkey,
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800401 IB_ACCESS_REMOTE_ATOMIC)))
402 goto acc_err;
403 /* Perform atomic OP and save result. */
Ralph Campbell3859e392007-03-15 14:44:51 -0700404 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
405 sdata = wqe->wr.wr.atomic.compare_add;
406 *(u64 *) sqp->s_sge.sge.vaddr =
407 (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
408 (u64) atomic64_add_return(sdata, maddr) - sdata :
409 (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
410 sdata, wqe->wr.wr.atomic.swap);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800411 goto send_comp;
412
413 default:
414 goto done;
415 }
416
417 sge = &sqp->s_sge.sge;
418 while (sqp->s_len) {
419 u32 len = sqp->s_len;
420
421 if (len > sge->length)
422 len = sge->length;
Ralph Campbell30d149a2007-06-18 14:24:44 -0700423 if (len > sge->sge_length)
424 len = sge->sge_length;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800425 BUG_ON(len == 0);
426 ipath_copy_sge(&qp->r_sge, sge->vaddr, len);
427 sge->vaddr += len;
428 sge->length -= len;
429 sge->sge_length -= len;
430 if (sge->sge_length == 0) {
431 if (--sqp->s_sge.num_sge)
432 *sge = *sqp->s_sge.sg_list++;
433 } else if (sge->length == 0 && sge->mr != NULL) {
434 if (++sge->n >= IPATH_SEGSZ) {
435 if (++sge->m >= sge->mr->mapsz)
436 break;
437 sge->n = 0;
438 }
439 sge->vaddr =
440 sge->mr->map[sge->m]->segs[sge->n].vaddr;
441 sge->length =
442 sge->mr->map[sge->m]->segs[sge->n].length;
443 }
444 sqp->s_len -= len;
445 }
446
447 if (wqe->wr.opcode == IB_WR_RDMA_WRITE ||
448 wqe->wr.opcode == IB_WR_RDMA_READ)
449 goto send_comp;
450
451 if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700452 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800453 else
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700454 wc.opcode = IB_WC_RECV;
455 wc.wr_id = qp->r_wr_id;
456 wc.status = IB_WC_SUCCESS;
457 wc.vendor_err = 0;
458 wc.byte_len = wqe->length;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200459 wc.qp = &qp->ibqp;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700460 wc.src_qp = qp->remote_qpn;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700461 wc.pkey_index = 0;
462 wc.slid = qp->remote_ah_attr.dlid;
463 wc.sl = qp->remote_ah_attr.sl;
464 wc.dlid_path_bits = 0;
Ralph Campbell4ee97182007-07-25 11:08:28 -0700465 wc.port_num = 1;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800466 /* Signal completion event if the solicited bit is set. */
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700467 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800468 wqe->wr.send_flags & IB_SEND_SOLICITED);
469
470send_comp:
471 sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
Ralph Campbell4ee97182007-07-25 11:08:28 -0700472 ipath_send_complete(sqp, wqe, IB_WC_SUCCESS);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800473 goto again;
474
475done:
476 if (atomic_dec_and_test(&qp->refcount))
477 wake_up(&qp->wait);
478}
479
Ralph Campbell4ee97182007-07-25 11:08:28 -0700480static void want_buffer(struct ipath_devdata *dd)
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700481{
482 set_bit(IPATH_S_PIOINTBUFAVAIL, &dd->ipath_sendctrl);
483 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
484 dd->ipath_sendctrl);
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700485}
486
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800487/**
488 * ipath_no_bufs_available - tell the layer driver we need buffers
489 * @qp: the QP that caused the problem
490 * @dev: the device we ran out of buffers on
491 *
492 * Called when we run out of PIO buffers.
493 */
Ralph Campbell4ee97182007-07-25 11:08:28 -0700494static void ipath_no_bufs_available(struct ipath_qp *qp,
495 struct ipath_ibdev *dev)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800496{
497 unsigned long flags;
498
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800499 /*
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700500 * Note that as soon as want_buffer() is called and
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800501 * possibly before it returns, ipath_ib_piobufavail()
502 * could be called. If we are still in the tasklet function,
503 * tasklet_hi_schedule() will not call us until the next time
504 * tasklet_hi_schedule() is called.
Ralph Campbelldb5518c2007-06-18 14:24:43 -0700505 * We leave the busy flag set so that another post send doesn't
506 * try to put the same QP on the piowait list again.
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800507 */
Ralph Campbell4ee97182007-07-25 11:08:28 -0700508 spin_lock_irqsave(&dev->pending_lock, flags);
509 list_add_tail(&qp->piowait, &dev->piowait);
510 spin_unlock_irqrestore(&dev->pending_lock, flags);
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700511 want_buffer(dev->dd);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800512 dev->n_piowait++;
513}
514
515/**
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700516 * ipath_make_grh - construct a GRH header
517 * @dev: a pointer to the ipath device
518 * @hdr: a pointer to the GRH header being constructed
519 * @grh: the global route address to send to
520 * @hwords: the number of 32 bit words of header being sent
521 * @nwords: the number of 32 bit words of data being sent
522 *
523 * Return the size of the header in 32 bit words.
524 */
525u32 ipath_make_grh(struct ipath_ibdev *dev, struct ib_grh *hdr,
526 struct ib_global_route *grh, u32 hwords, u32 nwords)
527{
528 hdr->version_tclass_flow =
529 cpu_to_be32((6 << 28) |
530 (grh->traffic_class << 20) |
531 grh->flow_label);
532 hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
533 /* next_hdr is defined by C8-7 in ch. 8.4.1 */
534 hdr->next_hdr = 0x1B;
535 hdr->hop_limit = grh->hop_limit;
536 /* The SGID is 32-bit aligned. */
537 hdr->sgid.global.subnet_prefix = dev->gid_prefix;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700538 hdr->sgid.global.interface_id = dev->dd->ipath_guid;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700539 hdr->dgid = grh->dgid;
540
541 /* GRH header size in 32-bit words. */
542 return sizeof(struct ib_grh) / sizeof(u32);
543}
544
Ralph Campbell4ee97182007-07-25 11:08:28 -0700545void ipath_make_ruc_header(struct ipath_ibdev *dev, struct ipath_qp *qp,
546 struct ipath_other_headers *ohdr,
547 u32 bth0, u32 bth2)
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700548{
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700549 u16 lrh0;
550 u32 nwords;
551 u32 extra_bytes;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700552
553 /* Construct the header. */
Ralph Campbell4ee97182007-07-25 11:08:28 -0700554 extra_bytes = -qp->s_cur_size & 3;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700555 nwords = (qp->s_cur_size + extra_bytes) >> 2;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700556 lrh0 = IPATH_LRH_BTH;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700557 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
558 qp->s_hdrwords += ipath_make_grh(dev, &qp->s_hdr.u.l.grh,
559 &qp->remote_ah_attr.grh,
560 qp->s_hdrwords, nwords);
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700561 lrh0 = IPATH_LRH_GRH;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700562 }
563 lrh0 |= qp->remote_ah_attr.sl << 4;
564 qp->s_hdr.lrh[0] = cpu_to_be16(lrh0);
565 qp->s_hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
Ralph Campbell4ee97182007-07-25 11:08:28 -0700566 qp->s_hdr.lrh[2] = cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700567 qp->s_hdr.lrh[3] = cpu_to_be16(dev->dd->ipath_lid);
568 bth0 |= ipath_get_pkey(dev->dd, qp->s_pkey_index);
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700569 bth0 |= extra_bytes << 20;
Ralph Campbell4ee97182007-07-25 11:08:28 -0700570 ohdr->bth[0] = cpu_to_be32(bth0 | (1 << 22));
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700571 ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
572 ohdr->bth[2] = cpu_to_be32(bth2);
Ralph Campbell4ee97182007-07-25 11:08:28 -0700573}
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700574
Ralph Campbell4ee97182007-07-25 11:08:28 -0700575/**
576 * ipath_do_send - perform a send on a QP
577 * @data: contains a pointer to the QP
578 *
579 * Process entries in the send work queue until credit or queue is
580 * exhausted. Only allow one CPU to send a packet per QP (tasklet).
581 * Otherwise, two threads could send packets out of order.
582 */
583void ipath_do_send(unsigned long data)
584{
585 struct ipath_qp *qp = (struct ipath_qp *)data;
586 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
587 int (*make_req)(struct ipath_qp *qp);
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700588
Ralph Campbell4ee97182007-07-25 11:08:28 -0700589 if (test_and_set_bit(IPATH_S_BUSY, &qp->s_busy))
590 goto bail;
591
592 if ((qp->ibqp.qp_type == IB_QPT_RC ||
593 qp->ibqp.qp_type == IB_QPT_UC) &&
594 qp->remote_ah_attr.dlid == dev->dd->ipath_lid) {
595 ipath_ruc_loopback(qp);
596 goto clear;
597 }
598
599 if (qp->ibqp.qp_type == IB_QPT_RC)
600 make_req = ipath_make_rc_req;
601 else if (qp->ibqp.qp_type == IB_QPT_UC)
602 make_req = ipath_make_uc_req;
603 else
604 make_req = ipath_make_ud_req;
605
606again:
607 /* Check for a constructed packet to be sent. */
608 if (qp->s_hdrwords != 0) {
609 /*
610 * If no PIO bufs are available, return. An interrupt will
611 * call ipath_ib_piobufavail() when one is available.
612 */
613 if (ipath_verbs_send(qp, &qp->s_hdr, qp->s_hdrwords,
614 qp->s_cur_sge, qp->s_cur_size)) {
615 ipath_no_bufs_available(qp, dev);
616 goto bail;
617 }
618 dev->n_unicast_xmit++;
619 /* Record that we sent the packet and s_hdr is empty. */
620 qp->s_hdrwords = 0;
621 }
622
623 if (make_req(qp))
624 goto again;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700625clear:
Ralph Campbell3859e392007-03-15 14:44:51 -0700626 clear_bit(IPATH_S_BUSY, &qp->s_busy);
Ralph Campbell4ee97182007-07-25 11:08:28 -0700627bail:;
628}
629
630void ipath_send_complete(struct ipath_qp *qp, struct ipath_swqe *wqe,
631 enum ib_wc_status status)
632{
633 u32 last = qp->s_last;
634
635 if (++last == qp->s_size)
636 last = 0;
637 qp->s_last = last;
638
639 /* See ch. 11.2.4.1 and 10.7.3.1 */
640 if (!(qp->s_flags & IPATH_S_SIGNAL_REQ_WR) ||
641 (wqe->wr.send_flags & IB_SEND_SIGNALED) ||
642 status != IB_WC_SUCCESS) {
643 struct ib_wc wc;
644
645 wc.wr_id = wqe->wr.wr_id;
646 wc.status = status;
647 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
648 wc.vendor_err = 0;
649 wc.byte_len = wqe->length;
650 wc.imm_data = 0;
651 wc.qp = &qp->ibqp;
652 wc.src_qp = 0;
653 wc.wc_flags = 0;
654 wc.pkey_index = 0;
655 wc.slid = 0;
656 wc.sl = 0;
657 wc.dlid_path_bits = 0;
658 wc.port_num = 0;
659 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 0);
660 }
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700661}