blob: c69c2523944339eec85a832edc6a0f596267f9ec [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
34#include "ipath_verbs.h"
Ralph Campbell373d9912006-09-22 15:22:26 -070035#include "ipath_kernel.h"
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -080036
37/*
38 * Convert the AETH RNR timeout code into the number of milliseconds.
39 */
40const u32 ib_ipath_rnr_table[32] = {
41 656, /* 0 */
42 1, /* 1 */
43 1, /* 2 */
44 1, /* 3 */
45 1, /* 4 */
46 1, /* 5 */
47 1, /* 6 */
48 1, /* 7 */
49 1, /* 8 */
50 1, /* 9 */
51 1, /* A */
52 1, /* B */
53 1, /* C */
54 1, /* D */
55 2, /* E */
56 2, /* F */
57 3, /* 10 */
58 4, /* 11 */
59 6, /* 12 */
60 8, /* 13 */
61 11, /* 14 */
62 16, /* 15 */
63 21, /* 16 */
64 31, /* 17 */
65 41, /* 18 */
66 62, /* 19 */
67 82, /* 1A */
68 123, /* 1B */
69 164, /* 1C */
70 246, /* 1D */
71 328, /* 1E */
72 492 /* 1F */
73};
74
75/**
76 * ipath_insert_rnr_queue - put QP on the RNR timeout list for the device
77 * @qp: the QP
78 *
79 * XXX Use a simple list for now. We might need a priority
80 * queue if we have lots of QPs waiting for RNR timeouts
81 * but that should be rare.
82 */
83void ipath_insert_rnr_queue(struct ipath_qp *qp)
84{
85 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
86 unsigned long flags;
87
88 spin_lock_irqsave(&dev->pending_lock, flags);
89 if (list_empty(&dev->rnrwait))
90 list_add(&qp->timerwait, &dev->rnrwait);
91 else {
92 struct list_head *l = &dev->rnrwait;
93 struct ipath_qp *nqp = list_entry(l->next, struct ipath_qp,
94 timerwait);
95
96 while (qp->s_rnr_timeout >= nqp->s_rnr_timeout) {
97 qp->s_rnr_timeout -= nqp->s_rnr_timeout;
98 l = l->next;
99 if (l->next == &dev->rnrwait)
100 break;
101 nqp = list_entry(l->next, struct ipath_qp,
102 timerwait);
103 }
104 list_add(&qp->timerwait, l);
105 }
106 spin_unlock_irqrestore(&dev->pending_lock, flags);
107}
108
Ralph Campbell373d9912006-09-22 15:22:26 -0700109static int init_sge(struct ipath_qp *qp, struct ipath_rwqe *wqe)
110{
Ralph Campbell373d9912006-09-22 15:22:26 -0700111 int user = to_ipd(qp->ibqp.pd)->user;
112 int i, j, ret;
113 struct ib_wc wc;
114
115 qp->r_len = 0;
116 for (i = j = 0; i < wqe->num_sge; i++) {
117 if (wqe->sg_list[i].length == 0)
118 continue;
119 /* Check LKEY */
120 if ((user && wqe->sg_list[i].lkey == 0) ||
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700121 !ipath_lkey_ok(qp, &qp->r_sg_list[j], &wqe->sg_list[i],
Ralph Campbell373d9912006-09-22 15:22:26 -0700122 IB_ACCESS_LOCAL_WRITE))
123 goto bad_lkey;
124 qp->r_len += wqe->sg_list[i].length;
125 j++;
126 }
127 qp->r_sge.sge = qp->r_sg_list[0];
128 qp->r_sge.sg_list = qp->r_sg_list + 1;
129 qp->r_sge.num_sge = j;
130 ret = 1;
131 goto bail;
132
133bad_lkey:
134 wc.wr_id = wqe->wr_id;
135 wc.status = IB_WC_LOC_PROT_ERR;
136 wc.opcode = IB_WC_RECV;
137 wc.vendor_err = 0;
138 wc.byte_len = 0;
139 wc.imm_data = 0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200140 wc.qp = &qp->ibqp;
Ralph Campbell373d9912006-09-22 15:22:26 -0700141 wc.src_qp = 0;
142 wc.wc_flags = 0;
143 wc.pkey_index = 0;
144 wc.slid = 0;
145 wc.sl = 0;
146 wc.dlid_path_bits = 0;
147 wc.port_num = 0;
148 /* Signal solicited completion event. */
149 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
150 ret = 0;
151bail:
152 return ret;
153}
154
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800155/**
156 * ipath_get_rwqe - copy the next RWQE into the QP's RWQE
157 * @qp: the QP
158 * @wr_id_only: update wr_id only, not SGEs
159 *
160 * Return 0 if no RWQE is available, otherwise return 1.
161 *
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700162 * Can be called from interrupt level.
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800163 */
164int ipath_get_rwqe(struct ipath_qp *qp, int wr_id_only)
165{
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700166 unsigned long flags;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800167 struct ipath_rq *rq;
Ralph Campbell373d9912006-09-22 15:22:26 -0700168 struct ipath_rwq *wq;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800169 struct ipath_srq *srq;
170 struct ipath_rwqe *wqe;
Ralph Campbell373d9912006-09-22 15:22:26 -0700171 void (*handler)(struct ib_event *, void *);
172 u32 tail;
173 int ret;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800174
Ralph Campbell373d9912006-09-22 15:22:26 -0700175 if (qp->ibqp.srq) {
176 srq = to_isrq(qp->ibqp.srq);
177 handler = srq->ibsrq.event_handler;
178 rq = &srq->rq;
179 } else {
180 srq = NULL;
181 handler = NULL;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800182 rq = &qp->r_rq;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800183 }
184
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700185 spin_lock_irqsave(&rq->lock, flags);
Ralph Campbell373d9912006-09-22 15:22:26 -0700186 wq = rq->wq;
187 tail = wq->tail;
188 /* Validate tail before using it since it is user writable. */
189 if (tail >= rq->size)
190 tail = 0;
191 do {
192 if (unlikely(tail == wq->head)) {
193 spin_unlock_irqrestore(&rq->lock, flags);
194 ret = 0;
195 goto bail;
196 }
Ralph Campbell4fc570b2007-07-06 12:48:23 -0700197 /* Make sure entry is read after head index is read. */
198 smp_rmb();
Ralph Campbell373d9912006-09-22 15:22:26 -0700199 wqe = get_rwqe_ptr(rq, tail);
200 if (++tail >= rq->size)
201 tail = 0;
202 } while (!wr_id_only && !init_sge(qp, wqe));
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800203 qp->r_wr_id = wqe->wr_id;
Ralph Campbell373d9912006-09-22 15:22:26 -0700204 wq->tail = tail;
205
206 ret = 1;
Ralph Campbell9f9630d2007-03-15 14:44:49 -0700207 qp->r_wrid_valid = 1;
Ralph Campbell373d9912006-09-22 15:22:26 -0700208 if (handler) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800209 u32 n;
210
Ralph Campbell373d9912006-09-22 15:22:26 -0700211 /*
212 * validate head pointer value and compute
213 * the number of remaining WQEs.
214 */
215 n = wq->head;
216 if (n >= rq->size)
217 n = 0;
218 if (n < tail)
219 n += rq->size - tail;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800220 else
Ralph Campbell373d9912006-09-22 15:22:26 -0700221 n -= tail;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800222 if (n < srq->limit) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700223 struct ib_event ev;
224
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800225 srq->limit = 0;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700226 spin_unlock_irqrestore(&rq->lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800227 ev.device = qp->ibqp.device;
228 ev.element.srq = qp->ibqp.srq;
229 ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
Ralph Campbell373d9912006-09-22 15:22:26 -0700230 handler(&ev, srq->ibsrq.srq_context);
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700231 goto bail;
232 }
233 }
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700234 spin_unlock_irqrestore(&rq->lock, flags);
Ralph Campbell373d9912006-09-22 15:22:26 -0700235
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800236bail:
237 return ret;
238}
239
240/**
241 * ipath_ruc_loopback - handle UC and RC lookback requests
242 * @sqp: the loopback QP
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800243 *
244 * This is called from ipath_do_uc_send() or ipath_do_rc_send() to
245 * forward a WQE addressed to the same HCA.
246 * Note that although we are single threaded due to the tasklet, we still
247 * have to protect against post_send(). We don't have to worry about
248 * receive interrupts since this is a connected protocol and all packets
249 * will pass through here.
250 */
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700251static void ipath_ruc_loopback(struct ipath_qp *sqp)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800252{
253 struct ipath_ibdev *dev = to_idev(sqp->ibqp.device);
254 struct ipath_qp *qp;
255 struct ipath_swqe *wqe;
256 struct ipath_sge *sge;
257 unsigned long flags;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700258 struct ib_wc wc;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800259 u64 sdata;
Ralph Campbell3859e392007-03-15 14:44:51 -0700260 atomic64_t *maddr;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800261
262 qp = ipath_lookup_qpn(&dev->qp_table, sqp->remote_qpn);
263 if (!qp) {
264 dev->n_pkt_drops++;
265 return;
266 }
267
268again:
269 spin_lock_irqsave(&sqp->s_lock, flags);
270
Ralph Campbell7b21d262007-03-15 14:44:50 -0700271 if (!(ib_ipath_state_ops[sqp->state] & IPATH_PROCESS_SEND_OK) ||
Ralph Campbell6d2fad02007-06-18 14:24:38 -0700272 sqp->s_rnr_timeout) {
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800273 spin_unlock_irqrestore(&sqp->s_lock, flags);
274 goto done;
275 }
276
277 /* Get the next send request. */
278 if (sqp->s_last == sqp->s_head) {
279 /* Send work queue is empty. */
280 spin_unlock_irqrestore(&sqp->s_lock, flags);
281 goto done;
282 }
283
284 /*
285 * We can rely on the entry not changing without the s_lock
286 * being held until we update s_last.
287 */
288 wqe = get_swqe_ptr(sqp, sqp->s_last);
289 spin_unlock_irqrestore(&sqp->s_lock, flags);
290
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700291 wc.wc_flags = 0;
292 wc.imm_data = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800293
294 sqp->s_sge.sge = wqe->sg_list[0];
295 sqp->s_sge.sg_list = wqe->sg_list + 1;
296 sqp->s_sge.num_sge = wqe->wr.num_sge;
297 sqp->s_len = wqe->length;
298 switch (wqe->wr.opcode) {
299 case IB_WR_SEND_WITH_IMM:
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700300 wc.wc_flags = IB_WC_WITH_IMM;
301 wc.imm_data = wqe->wr.imm_data;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800302 /* FALLTHROUGH */
303 case IB_WR_SEND:
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800304 if (!ipath_get_rwqe(qp, 0)) {
305 rnr_nak:
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800306 /* Handle RNR NAK */
307 if (qp->ibqp.qp_type == IB_QPT_UC)
308 goto send_comp;
309 if (sqp->s_rnr_retry == 0) {
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700310 wc.status = IB_WC_RNR_RETRY_EXC_ERR;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800311 goto err;
312 }
313 if (sqp->s_rnr_retry_cnt < 7)
314 sqp->s_rnr_retry--;
315 dev->n_rnr_naks++;
316 sqp->s_rnr_timeout =
Ralph Campbell3859e392007-03-15 14:44:51 -0700317 ib_ipath_rnr_table[qp->r_min_rnr_timer];
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800318 ipath_insert_rnr_queue(sqp);
319 goto done;
320 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800321 break;
322
323 case IB_WR_RDMA_WRITE_WITH_IMM:
Robert Walshb506e1d2007-06-18 14:24:48 -0700324 if (unlikely(!(qp->qp_access_flags &
325 IB_ACCESS_REMOTE_WRITE))) {
326 wc.status = IB_WC_REM_INV_REQ_ERR;
327 goto err;
328 }
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700329 wc.wc_flags = IB_WC_WITH_IMM;
330 wc.imm_data = wqe->wr.imm_data;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800331 if (!ipath_get_rwqe(qp, 1))
332 goto rnr_nak;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800333 /* FALLTHROUGH */
334 case IB_WR_RDMA_WRITE:
Robert Walshb506e1d2007-06-18 14:24:48 -0700335 if (unlikely(!(qp->qp_access_flags &
336 IB_ACCESS_REMOTE_WRITE))) {
337 wc.status = IB_WC_REM_INV_REQ_ERR;
338 goto err;
339 }
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800340 if (wqe->length == 0)
341 break;
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700342 if (unlikely(!ipath_rkey_ok(qp, &qp->r_sge, wqe->length,
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800343 wqe->wr.wr.rdma.remote_addr,
344 wqe->wr.wr.rdma.rkey,
345 IB_ACCESS_REMOTE_WRITE))) {
346 acc_err:
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700347 wc.status = IB_WC_REM_ACCESS_ERR;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800348 err:
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700349 wc.wr_id = wqe->wr.wr_id;
350 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
351 wc.vendor_err = 0;
352 wc.byte_len = 0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200353 wc.qp = &sqp->ibqp;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700354 wc.src_qp = sqp->remote_qpn;
355 wc.pkey_index = 0;
356 wc.slid = sqp->remote_ah_attr.dlid;
357 wc.sl = sqp->remote_ah_attr.sl;
358 wc.dlid_path_bits = 0;
359 wc.port_num = 0;
Ralph Campbell3859e392007-03-15 14:44:51 -0700360 spin_lock_irqsave(&sqp->s_lock, flags);
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700361 ipath_sqerror_qp(sqp, &wc);
Ralph Campbell3859e392007-03-15 14:44:51 -0700362 spin_unlock_irqrestore(&sqp->s_lock, flags);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800363 goto done;
364 }
365 break;
366
367 case IB_WR_RDMA_READ:
Ralph Campbell3859e392007-03-15 14:44:51 -0700368 if (unlikely(!(qp->qp_access_flags &
Robert Walshb506e1d2007-06-18 14:24:48 -0700369 IB_ACCESS_REMOTE_READ))) {
370 wc.status = IB_WC_REM_INV_REQ_ERR;
371 goto err;
372 }
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700373 if (unlikely(!ipath_rkey_ok(qp, &sqp->s_sge, wqe->length,
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800374 wqe->wr.wr.rdma.remote_addr,
375 wqe->wr.wr.rdma.rkey,
376 IB_ACCESS_REMOTE_READ)))
377 goto acc_err;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800378 qp->r_sge.sge = wqe->sg_list[0];
379 qp->r_sge.sg_list = wqe->sg_list + 1;
380 qp->r_sge.num_sge = wqe->wr.num_sge;
381 break;
382
383 case IB_WR_ATOMIC_CMP_AND_SWP:
384 case IB_WR_ATOMIC_FETCH_AND_ADD:
Ralph Campbell3859e392007-03-15 14:44:51 -0700385 if (unlikely(!(qp->qp_access_flags &
Robert Walshb506e1d2007-06-18 14:24:48 -0700386 IB_ACCESS_REMOTE_ATOMIC))) {
387 wc.status = IB_WC_REM_INV_REQ_ERR;
388 goto err;
389 }
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700390 if (unlikely(!ipath_rkey_ok(qp, &qp->r_sge, sizeof(u64),
Ralph Campbell3859e392007-03-15 14:44:51 -0700391 wqe->wr.wr.atomic.remote_addr,
392 wqe->wr.wr.atomic.rkey,
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800393 IB_ACCESS_REMOTE_ATOMIC)))
394 goto acc_err;
395 /* Perform atomic OP and save result. */
Ralph Campbell3859e392007-03-15 14:44:51 -0700396 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
397 sdata = wqe->wr.wr.atomic.compare_add;
398 *(u64 *) sqp->s_sge.sge.vaddr =
399 (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
400 (u64) atomic64_add_return(sdata, maddr) - sdata :
401 (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
402 sdata, wqe->wr.wr.atomic.swap);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800403 goto send_comp;
404
405 default:
406 goto done;
407 }
408
409 sge = &sqp->s_sge.sge;
410 while (sqp->s_len) {
411 u32 len = sqp->s_len;
412
413 if (len > sge->length)
414 len = sge->length;
Ralph Campbell30d149a2007-06-18 14:24:44 -0700415 if (len > sge->sge_length)
416 len = sge->sge_length;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800417 BUG_ON(len == 0);
418 ipath_copy_sge(&qp->r_sge, sge->vaddr, len);
419 sge->vaddr += len;
420 sge->length -= len;
421 sge->sge_length -= len;
422 if (sge->sge_length == 0) {
423 if (--sqp->s_sge.num_sge)
424 *sge = *sqp->s_sge.sg_list++;
425 } else if (sge->length == 0 && sge->mr != NULL) {
426 if (++sge->n >= IPATH_SEGSZ) {
427 if (++sge->m >= sge->mr->mapsz)
428 break;
429 sge->n = 0;
430 }
431 sge->vaddr =
432 sge->mr->map[sge->m]->segs[sge->n].vaddr;
433 sge->length =
434 sge->mr->map[sge->m]->segs[sge->n].length;
435 }
436 sqp->s_len -= len;
437 }
438
439 if (wqe->wr.opcode == IB_WR_RDMA_WRITE ||
440 wqe->wr.opcode == IB_WR_RDMA_READ)
441 goto send_comp;
442
443 if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700444 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800445 else
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700446 wc.opcode = IB_WC_RECV;
447 wc.wr_id = qp->r_wr_id;
448 wc.status = IB_WC_SUCCESS;
449 wc.vendor_err = 0;
450 wc.byte_len = wqe->length;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200451 wc.qp = &qp->ibqp;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700452 wc.src_qp = qp->remote_qpn;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800453 /* XXX do we know which pkey matched? Only needed for GSI. */
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700454 wc.pkey_index = 0;
455 wc.slid = qp->remote_ah_attr.dlid;
456 wc.sl = qp->remote_ah_attr.sl;
457 wc.dlid_path_bits = 0;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800458 /* Signal completion event if the solicited bit is set. */
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700459 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800460 wqe->wr.send_flags & IB_SEND_SOLICITED);
461
462send_comp:
463 sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
464
Ralph Campbell3859e392007-03-15 14:44:51 -0700465 if (!(sqp->s_flags & IPATH_S_SIGNAL_REQ_WR) ||
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800466 (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700467 wc.wr_id = wqe->wr.wr_id;
468 wc.status = IB_WC_SUCCESS;
469 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
470 wc.vendor_err = 0;
471 wc.byte_len = wqe->length;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200472 wc.qp = &sqp->ibqp;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700473 wc.src_qp = 0;
474 wc.pkey_index = 0;
475 wc.slid = 0;
476 wc.sl = 0;
477 wc.dlid_path_bits = 0;
478 wc.port_num = 0;
479 ipath_cq_enter(to_icq(sqp->ibqp.send_cq), &wc, 0);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800480 }
481
482 /* Update s_last now that we are finished with the SWQE */
483 spin_lock_irqsave(&sqp->s_lock, flags);
484 if (++sqp->s_last >= sqp->s_size)
485 sqp->s_last = 0;
486 spin_unlock_irqrestore(&sqp->s_lock, flags);
487 goto again;
488
489done:
490 if (atomic_dec_and_test(&qp->refcount))
491 wake_up(&qp->wait);
492}
493
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700494static int want_buffer(struct ipath_devdata *dd)
495{
496 set_bit(IPATH_S_PIOINTBUFAVAIL, &dd->ipath_sendctrl);
497 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
498 dd->ipath_sendctrl);
499
500 return 0;
501}
502
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800503/**
504 * ipath_no_bufs_available - tell the layer driver we need buffers
505 * @qp: the QP that caused the problem
506 * @dev: the device we ran out of buffers on
507 *
508 * Called when we run out of PIO buffers.
509 */
Roland Dreierda9aec72007-07-17 18:37:43 -0700510static void ipath_no_bufs_available(struct ipath_qp *qp, struct ipath_ibdev *dev)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800511{
512 unsigned long flags;
513
514 spin_lock_irqsave(&dev->pending_lock, flags);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700515 if (list_empty(&qp->piowait))
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800516 list_add_tail(&qp->piowait, &dev->piowait);
517 spin_unlock_irqrestore(&dev->pending_lock, flags);
518 /*
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700519 * Note that as soon as want_buffer() is called and
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800520 * possibly before it returns, ipath_ib_piobufavail()
521 * could be called. If we are still in the tasklet function,
522 * tasklet_hi_schedule() will not call us until the next time
523 * tasklet_hi_schedule() is called.
Ralph Campbelldb5518c2007-06-18 14:24:43 -0700524 * We leave the busy flag set so that another post send doesn't
525 * try to put the same QP on the piowait list again.
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800526 */
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700527 want_buffer(dev->dd);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800528 dev->n_piowait++;
529}
530
531/**
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700532 * ipath_post_ruc_send - post RC and UC sends
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800533 * @qp: the QP to post on
534 * @wr: the work request to send
535 */
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700536int ipath_post_ruc_send(struct ipath_qp *qp, struct ib_send_wr *wr)
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800537{
538 struct ipath_swqe *wqe;
539 unsigned long flags;
540 u32 next;
541 int i, j;
542 int acc;
543 int ret;
544
545 /*
546 * Don't allow RDMA reads or atomic operations on UC or
547 * undefined operations.
548 * Make sure buffer is large enough to hold the result for atomics.
549 */
550 if (qp->ibqp.qp_type == IB_QPT_UC) {
551 if ((unsigned) wr->opcode >= IB_WR_RDMA_READ) {
552 ret = -EINVAL;
553 goto bail;
554 }
555 } else if ((unsigned) wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD) {
556 ret = -EINVAL;
557 goto bail;
558 } else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
559 (wr->num_sge == 0 ||
560 wr->sg_list[0].length < sizeof(u64) ||
561 wr->sg_list[0].addr & (sizeof(u64) - 1))) {
562 ret = -EINVAL;
563 goto bail;
Ralph Campbell3859e392007-03-15 14:44:51 -0700564 } else if (wr->opcode >= IB_WR_RDMA_READ && !qp->s_max_rd_atomic) {
565 ret = -EINVAL;
566 goto bail;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800567 }
568 /* IB spec says that num_sge == 0 is OK. */
569 if (wr->num_sge > qp->s_max_sge) {
570 ret = -ENOMEM;
571 goto bail;
572 }
573 spin_lock_irqsave(&qp->s_lock, flags);
574 next = qp->s_head + 1;
575 if (next >= qp->s_size)
576 next = 0;
577 if (next == qp->s_last) {
578 spin_unlock_irqrestore(&qp->s_lock, flags);
579 ret = -EINVAL;
580 goto bail;
581 }
582
583 wqe = get_swqe_ptr(qp, qp->s_head);
584 wqe->wr = *wr;
585 wqe->ssn = qp->s_ssn++;
586 wqe->sg_list[0].mr = NULL;
587 wqe->sg_list[0].vaddr = NULL;
588 wqe->sg_list[0].length = 0;
589 wqe->sg_list[0].sge_length = 0;
590 wqe->length = 0;
591 acc = wr->opcode >= IB_WR_RDMA_READ ? IB_ACCESS_LOCAL_WRITE : 0;
592 for (i = 0, j = 0; i < wr->num_sge; i++) {
593 if (to_ipd(qp->ibqp.pd)->user && wr->sg_list[i].lkey == 0) {
594 spin_unlock_irqrestore(&qp->s_lock, flags);
595 ret = -EINVAL;
596 goto bail;
597 }
598 if (wr->sg_list[i].length == 0)
599 continue;
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700600 if (!ipath_lkey_ok(qp, &wqe->sg_list[j], &wr->sg_list[i],
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800601 acc)) {
602 spin_unlock_irqrestore(&qp->s_lock, flags);
603 ret = -EINVAL;
604 goto bail;
605 }
606 wqe->length += wr->sg_list[i].length;
607 j++;
608 }
609 wqe->wr.num_sge = j;
610 qp->s_head = next;
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800611 spin_unlock_irqrestore(&qp->s_lock, flags);
612
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700613 ipath_do_ruc_send((unsigned long) qp);
Bryan O'Sullivane28c00a2006-03-29 15:23:37 -0800614
615 ret = 0;
616
617bail:
618 return ret;
619}
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700620
621/**
622 * ipath_make_grh - construct a GRH header
623 * @dev: a pointer to the ipath device
624 * @hdr: a pointer to the GRH header being constructed
625 * @grh: the global route address to send to
626 * @hwords: the number of 32 bit words of header being sent
627 * @nwords: the number of 32 bit words of data being sent
628 *
629 * Return the size of the header in 32 bit words.
630 */
631u32 ipath_make_grh(struct ipath_ibdev *dev, struct ib_grh *hdr,
632 struct ib_global_route *grh, u32 hwords, u32 nwords)
633{
634 hdr->version_tclass_flow =
635 cpu_to_be32((6 << 28) |
636 (grh->traffic_class << 20) |
637 grh->flow_label);
638 hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
639 /* next_hdr is defined by C8-7 in ch. 8.4.1 */
640 hdr->next_hdr = 0x1B;
641 hdr->hop_limit = grh->hop_limit;
642 /* The SGID is 32-bit aligned. */
643 hdr->sgid.global.subnet_prefix = dev->gid_prefix;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700644 hdr->sgid.global.interface_id = dev->dd->ipath_guid;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700645 hdr->dgid = grh->dgid;
646
647 /* GRH header size in 32-bit words. */
648 return sizeof(struct ib_grh) / sizeof(u32);
649}
650
651/**
652 * ipath_do_ruc_send - perform a send on an RC or UC QP
653 * @data: contains a pointer to the QP
654 *
655 * Process entries in the send work queue until credit or queue is
656 * exhausted. Only allow one CPU to send a packet per QP (tasklet).
657 * Otherwise, after we drop the QP s_lock, two threads could send
658 * packets out of order.
659 */
660void ipath_do_ruc_send(unsigned long data)
661{
662 struct ipath_qp *qp = (struct ipath_qp *)data;
663 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
664 unsigned long flags;
665 u16 lrh0;
666 u32 nwords;
667 u32 extra_bytes;
668 u32 bth0;
669 u32 bth2;
670 u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
671 struct ipath_other_headers *ohdr;
672
Ralph Campbell3859e392007-03-15 14:44:51 -0700673 if (test_and_set_bit(IPATH_S_BUSY, &qp->s_busy))
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700674 goto bail;
675
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700676 if (unlikely(qp->remote_ah_attr.dlid == dev->dd->ipath_lid)) {
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700677 ipath_ruc_loopback(qp);
678 goto clear;
679 }
680
681 ohdr = &qp->s_hdr.u.oth;
682 if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
683 ohdr = &qp->s_hdr.u.l.oth;
684
685again:
686 /* Check for a constructed packet to be sent. */
687 if (qp->s_hdrwords != 0) {
688 /*
689 * If no PIO bufs are available, return. An interrupt will
690 * call ipath_ib_piobufavail() when one is available.
691 */
692 if (ipath_verbs_send(dev->dd, qp->s_hdrwords,
693 (u32 *) &qp->s_hdr, qp->s_cur_size,
694 qp->s_cur_sge)) {
695 ipath_no_bufs_available(qp, dev);
696 goto bail;
697 }
698 dev->n_unicast_xmit++;
699 /* Record that we sent the packet and s_hdr is empty. */
700 qp->s_hdrwords = 0;
701 }
702
703 /*
704 * The lock is needed to synchronize between setting
705 * qp->s_ack_state, resend timer, and post_send().
706 */
707 spin_lock_irqsave(&qp->s_lock, flags);
708
Ralph Campbell3859e392007-03-15 14:44:51 -0700709 if (!((qp->ibqp.qp_type == IB_QPT_RC) ?
710 ipath_make_rc_req(qp, ohdr, pmtu, &bth0, &bth2) :
711 ipath_make_uc_req(qp, ohdr, pmtu, &bth0, &bth2))) {
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700712 /*
713 * Clear the busy bit before unlocking to avoid races with
714 * adding new work queue items and then failing to process
715 * them.
716 */
Ralph Campbell3859e392007-03-15 14:44:51 -0700717 clear_bit(IPATH_S_BUSY, &qp->s_busy);
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700718 spin_unlock_irqrestore(&qp->s_lock, flags);
719 goto bail;
720 }
721
722 spin_unlock_irqrestore(&qp->s_lock, flags);
723
724 /* Construct the header. */
725 extra_bytes = (4 - qp->s_cur_size) & 3;
726 nwords = (qp->s_cur_size + extra_bytes) >> 2;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700727 lrh0 = IPATH_LRH_BTH;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700728 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
729 qp->s_hdrwords += ipath_make_grh(dev, &qp->s_hdr.u.l.grh,
730 &qp->remote_ah_attr.grh,
731 qp->s_hdrwords, nwords);
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700732 lrh0 = IPATH_LRH_GRH;
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700733 }
734 lrh0 |= qp->remote_ah_attr.sl << 4;
735 qp->s_hdr.lrh[0] = cpu_to_be16(lrh0);
736 qp->s_hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
737 qp->s_hdr.lrh[2] = cpu_to_be16(qp->s_hdrwords + nwords +
738 SIZE_OF_CRC);
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700739 qp->s_hdr.lrh[3] = cpu_to_be16(dev->dd->ipath_lid);
740 bth0 |= ipath_get_pkey(dev->dd, qp->s_pkey_index);
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700741 bth0 |= extra_bytes << 20;
742 ohdr->bth[0] = cpu_to_be32(bth0);
743 ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
744 ohdr->bth[2] = cpu_to_be32(bth2);
745
746 /* Check for more work to do. */
747 goto again;
748
749clear:
Ralph Campbell3859e392007-03-15 14:44:51 -0700750 clear_bit(IPATH_S_BUSY, &qp->s_busy);
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700751bail:
752 return;
753}