]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/infiniband/hw/ipath/ipath_ruc.c
IB/ipath: Fix SLID generation for RC/UC QPs when LMC > 0
[net-next-2.6.git] / drivers / infiniband / hw / ipath / ipath_ruc.c
CommitLineData
e28c00ad 1/*
53dc1ca1 2 * Copyright (c) 2006, 2007, 2008 QLogic Corporation. All rights reserved.
e28c00ad
BS
3 * 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
4ee97180
RC
34#include <linux/spinlock.h>
35
e28c00ad 36#include "ipath_verbs.h"
373d9915 37#include "ipath_kernel.h"
e28c00ad
BS
38
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 *
e509be89 81 * Called with the QP s_lock held and interrupts disabled.
e28c00ad
BS
82 * XXX Use a simple list for now. We might need a priority
83 * queue if we have lots of QPs waiting for RNR timeouts
84 * but that should be rare.
85 */
86void ipath_insert_rnr_queue(struct ipath_qp *qp)
87{
88 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
e28c00ad 89
e509be89
RC
90 /* We already did a spin_lock_irqsave(), so just use spin_lock */
91 spin_lock(&dev->pending_lock);
e28c00ad
BS
92 if (list_empty(&dev->rnrwait))
93 list_add(&qp->timerwait, &dev->rnrwait);
94 else {
95 struct list_head *l = &dev->rnrwait;
96 struct ipath_qp *nqp = list_entry(l->next, struct ipath_qp,
97 timerwait);
98
99 while (qp->s_rnr_timeout >= nqp->s_rnr_timeout) {
100 qp->s_rnr_timeout -= nqp->s_rnr_timeout;
101 l = l->next;
cc65edcf
RC
102 if (l->next == &dev->rnrwait) {
103 nqp = NULL;
e28c00ad 104 break;
cc65edcf 105 }
e28c00ad
BS
106 nqp = list_entry(l->next, struct ipath_qp,
107 timerwait);
108 }
cc65edcf
RC
109 if (nqp)
110 nqp->s_rnr_timeout -= qp->s_rnr_timeout;
e28c00ad
BS
111 list_add(&qp->timerwait, l);
112 }
e509be89 113 spin_unlock(&dev->pending_lock);
e28c00ad
BS
114}
115
4ee97180
RC
116/**
117 * ipath_init_sge - Validate a RWQE and fill in the SGE state
118 * @qp: the QP
119 *
120 * Return 1 if OK.
121 */
122int ipath_init_sge(struct ipath_qp *qp, struct ipath_rwqe *wqe,
123 u32 *lengthp, struct ipath_sge_state *ss)
373d9915 124{
373d9915
RC
125 int i, j, ret;
126 struct ib_wc wc;
127
4ee97180 128 *lengthp = 0;
373d9915
RC
129 for (i = j = 0; i < wqe->num_sge; i++) {
130 if (wqe->sg_list[i].length == 0)
131 continue;
132 /* Check LKEY */
4ee97180
RC
133 if (!ipath_lkey_ok(qp, j ? &ss->sg_list[j - 1] : &ss->sge,
134 &wqe->sg_list[i], IB_ACCESS_LOCAL_WRITE))
373d9915 135 goto bad_lkey;
4ee97180 136 *lengthp += wqe->sg_list[i].length;
373d9915
RC
137 j++;
138 }
4ee97180 139 ss->num_sge = j;
373d9915
RC
140 ret = 1;
141 goto bail;
142
143bad_lkey:
53dc1ca1 144 memset(&wc, 0, sizeof(wc));
373d9915
RC
145 wc.wr_id = wqe->wr_id;
146 wc.status = IB_WC_LOC_PROT_ERR;
147 wc.opcode = IB_WC_RECV;
062dbb69 148 wc.qp = &qp->ibqp;
373d9915
RC
149 /* Signal solicited completion event. */
150 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
151 ret = 0;
152bail:
153 return ret;
154}
155
e28c00ad
BS
156/**
157 * ipath_get_rwqe - copy the next RWQE into the QP's RWQE
158 * @qp: the QP
159 * @wr_id_only: update wr_id only, not SGEs
160 *
161 * Return 0 if no RWQE is available, otherwise return 1.
162 *
12eef41f 163 * Can be called from interrupt level.
e28c00ad
BS
164 */
165int ipath_get_rwqe(struct ipath_qp *qp, int wr_id_only)
166{
12eef41f 167 unsigned long flags;
e28c00ad 168 struct ipath_rq *rq;
373d9915 169 struct ipath_rwq *wq;
e28c00ad
BS
170 struct ipath_srq *srq;
171 struct ipath_rwqe *wqe;
373d9915
RC
172 void (*handler)(struct ib_event *, void *);
173 u32 tail;
174 int ret;
e28c00ad 175
4ee97180
RC
176 qp->r_sge.sg_list = qp->r_sg_list;
177
373d9915
RC
178 if (qp->ibqp.srq) {
179 srq = to_isrq(qp->ibqp.srq);
180 handler = srq->ibsrq.event_handler;
181 rq = &srq->rq;
182 } else {
183 srq = NULL;
184 handler = NULL;
e28c00ad 185 rq = &qp->r_rq;
e28c00ad
BS
186 }
187
12eef41f 188 spin_lock_irqsave(&rq->lock, flags);
e509be89
RC
189 if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_RECV_OK)) {
190 ret = 0;
191 goto unlock;
192 }
193
373d9915
RC
194 wq = rq->wq;
195 tail = wq->tail;
196 /* Validate tail before using it since it is user writable. */
197 if (tail >= rq->size)
198 tail = 0;
199 do {
200 if (unlikely(tail == wq->head)) {
373d9915 201 ret = 0;
e509be89 202 goto unlock;
373d9915 203 }
4fc570bc
RC
204 /* Make sure entry is read after head index is read. */
205 smp_rmb();
373d9915
RC
206 wqe = get_rwqe_ptr(rq, tail);
207 if (++tail >= rq->size)
208 tail = 0;
4ee97180
RC
209 } while (!wr_id_only && !ipath_init_sge(qp, wqe, &qp->r_len,
210 &qp->r_sge));
e28c00ad 211 qp->r_wr_id = wqe->wr_id;
373d9915
RC
212 wq->tail = tail;
213
214 ret = 1;
e509be89 215 set_bit(IPATH_R_WRID_VALID, &qp->r_aflags);
373d9915 216 if (handler) {
e28c00ad
BS
217 u32 n;
218
373d9915
RC
219 /*
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;
e28c00ad 228 else
373d9915 229 n -= tail;
e28c00ad 230 if (n < srq->limit) {
373d9915
RC
231 struct ib_event ev;
232
e28c00ad 233 srq->limit = 0;
12eef41f 234 spin_unlock_irqrestore(&rq->lock, flags);
e28c00ad
BS
235 ev.device = qp->ibqp.device;
236 ev.element.srq = qp->ibqp.srq;
237 ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
373d9915 238 handler(&ev, srq->ibsrq.srq_context);
12eef41f
BS
239 goto bail;
240 }
241 }
e509be89 242unlock:
12eef41f 243 spin_unlock_irqrestore(&rq->lock, flags);
e28c00ad
BS
244bail:
245 return ret;
246}
247
248/**
249 * ipath_ruc_loopback - handle UC and RC lookback requests
4ee97180 250 * @sqp: the sending QP
e28c00ad 251 *
4ee97180 252 * This is called from ipath_do_send() to
e28c00ad
BS
253 * 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 */
ddd4bb22 259static void ipath_ruc_loopback(struct ipath_qp *sqp)
e28c00ad
BS
260{
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;
ddd4bb22 266 struct ib_wc wc;
e28c00ad 267 u64 sdata;
3859e39d 268 atomic64_t *maddr;
53dc1ca1 269 enum ib_wc_status send_status;
e28c00ad 270
e509be89
RC
271 /*
272 * Note that we check the responder QP state after
273 * checking the requester's state.
274 */
e28c00ad 275 qp = ipath_lookup_qpn(&dev->qp_table, sqp->remote_qpn);
e28c00ad 276
e28c00ad
BS
277 spin_lock_irqsave(&sqp->s_lock, flags);
278
e509be89
RC
279 /* Return if we are already busy processing a work request. */
280 if ((sqp->s_flags & (IPATH_S_BUSY | IPATH_S_ANY_WAIT)) ||
281 !(ib_ipath_state_ops[sqp->state] & IPATH_PROCESS_OR_FLUSH_SEND))
282 goto unlock;
e28c00ad 283
e509be89
RC
284 sqp->s_flags |= IPATH_S_BUSY;
285
286again:
287 if (sqp->s_last == sqp->s_head)
288 goto clr_busy;
289 wqe = get_swqe_ptr(sqp, sqp->s_last);
290
291 /* Return if it is not OK to start a new work reqeust. */
292 if (!(ib_ipath_state_ops[sqp->state] & IPATH_PROCESS_NEXT_SEND_OK)) {
293 if (!(ib_ipath_state_ops[sqp->state] & IPATH_FLUSH_SEND))
294 goto clr_busy;
295 /* We are in the error state, flush the work request. */
296 send_status = IB_WC_WR_FLUSH_ERR;
297 goto flush_send;
e28c00ad
BS
298 }
299
300 /*
301 * We can rely on the entry not changing without the s_lock
302 * being held until we update s_last.
e509be89 303 * We increment s_cur to indicate s_last is in progress.
e28c00ad 304 */
e509be89
RC
305 if (sqp->s_last == sqp->s_cur) {
306 if (++sqp->s_cur >= sqp->s_size)
307 sqp->s_cur = 0;
308 }
e28c00ad
BS
309 spin_unlock_irqrestore(&sqp->s_lock, flags);
310
e509be89
RC
311 if (!qp || !(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_RECV_OK)) {
312 dev->n_pkt_drops++;
313 /*
314 * For RC, the requester would timeout and retry so
315 * shortcut the timeouts and just signal too many retries.
316 */
317 if (sqp->ibqp.qp_type == IB_QPT_RC)
318 send_status = IB_WC_RETRY_EXC_ERR;
319 else
320 send_status = IB_WC_SUCCESS;
321 goto serr;
322 }
323
53dc1ca1
RC
324 memset(&wc, 0, sizeof wc);
325 send_status = IB_WC_SUCCESS;
e28c00ad
BS
326
327 sqp->s_sge.sge = wqe->sg_list[0];
328 sqp->s_sge.sg_list = wqe->sg_list + 1;
329 sqp->s_sge.num_sge = wqe->wr.num_sge;
330 sqp->s_len = wqe->length;
331 switch (wqe->wr.opcode) {
332 case IB_WR_SEND_WITH_IMM:
ddd4bb22 333 wc.wc_flags = IB_WC_WITH_IMM;
00f7ec36 334 wc.ex.imm_data = wqe->wr.ex.imm_data;
e28c00ad
BS
335 /* FALLTHROUGH */
336 case IB_WR_SEND:
53dc1ca1
RC
337 if (!ipath_get_rwqe(qp, 0))
338 goto rnr_nak;
e28c00ad
BS
339 break;
340
341 case IB_WR_RDMA_WRITE_WITH_IMM:
53dc1ca1
RC
342 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
343 goto inv_err;
ddd4bb22 344 wc.wc_flags = IB_WC_WITH_IMM;
00f7ec36 345 wc.ex.imm_data = wqe->wr.ex.imm_data;
e28c00ad
BS
346 if (!ipath_get_rwqe(qp, 1))
347 goto rnr_nak;
e28c00ad
BS
348 /* FALLTHROUGH */
349 case IB_WR_RDMA_WRITE:
53dc1ca1
RC
350 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
351 goto inv_err;
e28c00ad
BS
352 if (wqe->length == 0)
353 break;
6a553af2 354 if (unlikely(!ipath_rkey_ok(qp, &qp->r_sge, wqe->length,
e28c00ad
BS
355 wqe->wr.wr.rdma.remote_addr,
356 wqe->wr.wr.rdma.rkey,
53dc1ca1
RC
357 IB_ACCESS_REMOTE_WRITE)))
358 goto acc_err;
e28c00ad
BS
359 break;
360
361 case IB_WR_RDMA_READ:
53dc1ca1
RC
362 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
363 goto inv_err;
6a553af2 364 if (unlikely(!ipath_rkey_ok(qp, &sqp->s_sge, wqe->length,
e28c00ad
BS
365 wqe->wr.wr.rdma.remote_addr,
366 wqe->wr.wr.rdma.rkey,
367 IB_ACCESS_REMOTE_READ)))
368 goto acc_err;
e28c00ad
BS
369 qp->r_sge.sge = wqe->sg_list[0];
370 qp->r_sge.sg_list = wqe->sg_list + 1;
371 qp->r_sge.num_sge = wqe->wr.num_sge;
372 break;
373
374 case IB_WR_ATOMIC_CMP_AND_SWP:
375 case IB_WR_ATOMIC_FETCH_AND_ADD:
53dc1ca1
RC
376 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
377 goto inv_err;
6a553af2 378 if (unlikely(!ipath_rkey_ok(qp, &qp->r_sge, sizeof(u64),
3859e39d
RC
379 wqe->wr.wr.atomic.remote_addr,
380 wqe->wr.wr.atomic.rkey,
e28c00ad
BS
381 IB_ACCESS_REMOTE_ATOMIC)))
382 goto acc_err;
383 /* Perform atomic OP and save result. */
3859e39d
RC
384 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
385 sdata = wqe->wr.wr.atomic.compare_add;
386 *(u64 *) sqp->s_sge.sge.vaddr =
387 (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
388 (u64) atomic64_add_return(sdata, maddr) - sdata :
389 (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
390 sdata, wqe->wr.wr.atomic.swap);
e28c00ad
BS
391 goto send_comp;
392
393 default:
53dc1ca1
RC
394 send_status = IB_WC_LOC_QP_OP_ERR;
395 goto serr;
e28c00ad
BS
396 }
397
398 sge = &sqp->s_sge.sge;
399 while (sqp->s_len) {
400 u32 len = sqp->s_len;
401
402 if (len > sge->length)
403 len = sge->length;
30d149ab
RC
404 if (len > sge->sge_length)
405 len = sge->sge_length;
e28c00ad
BS
406 BUG_ON(len == 0);
407 ipath_copy_sge(&qp->r_sge, sge->vaddr, len);
408 sge->vaddr += len;
409 sge->length -= len;
410 sge->sge_length -= len;
411 if (sge->sge_length == 0) {
412 if (--sqp->s_sge.num_sge)
413 *sge = *sqp->s_sge.sg_list++;
414 } else if (sge->length == 0 && sge->mr != NULL) {
415 if (++sge->n >= IPATH_SEGSZ) {
416 if (++sge->m >= sge->mr->mapsz)
417 break;
418 sge->n = 0;
419 }
420 sge->vaddr =
421 sge->mr->map[sge->m]->segs[sge->n].vaddr;
422 sge->length =
423 sge->mr->map[sge->m]->segs[sge->n].length;
424 }
425 sqp->s_len -= len;
426 }
427
e509be89 428 if (!test_and_clear_bit(IPATH_R_WRID_VALID, &qp->r_aflags))
e28c00ad
BS
429 goto send_comp;
430
431 if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
ddd4bb22 432 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
e28c00ad 433 else
ddd4bb22
BS
434 wc.opcode = IB_WC_RECV;
435 wc.wr_id = qp->r_wr_id;
436 wc.status = IB_WC_SUCCESS;
ddd4bb22 437 wc.byte_len = wqe->length;
062dbb69 438 wc.qp = &qp->ibqp;
ddd4bb22 439 wc.src_qp = qp->remote_qpn;
ddd4bb22
BS
440 wc.slid = qp->remote_ah_attr.dlid;
441 wc.sl = qp->remote_ah_attr.sl;
4ee97180 442 wc.port_num = 1;
e28c00ad 443 /* Signal completion event if the solicited bit is set. */
ddd4bb22 444 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
e28c00ad
BS
445 wqe->wr.send_flags & IB_SEND_SOLICITED);
446
447send_comp:
e509be89
RC
448 spin_lock_irqsave(&sqp->s_lock, flags);
449flush_send:
e28c00ad 450 sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
53dc1ca1 451 ipath_send_complete(sqp, wqe, send_status);
e28c00ad
BS
452 goto again;
453
53dc1ca1
RC
454rnr_nak:
455 /* Handle RNR NAK */
456 if (qp->ibqp.qp_type == IB_QPT_UC)
457 goto send_comp;
458 /*
459 * Note: we don't need the s_lock held since the BUSY flag
460 * makes this single threaded.
461 */
462 if (sqp->s_rnr_retry == 0) {
463 send_status = IB_WC_RNR_RETRY_EXC_ERR;
464 goto serr;
465 }
466 if (sqp->s_rnr_retry_cnt < 7)
467 sqp->s_rnr_retry--;
468 spin_lock_irqsave(&sqp->s_lock, flags);
469 if (!(ib_ipath_state_ops[sqp->state] & IPATH_PROCESS_RECV_OK))
e509be89
RC
470 goto clr_busy;
471 sqp->s_flags |= IPATH_S_WAITING;
53dc1ca1
RC
472 dev->n_rnr_naks++;
473 sqp->s_rnr_timeout = ib_ipath_rnr_table[qp->r_min_rnr_timer];
474 ipath_insert_rnr_queue(sqp);
e509be89 475 goto clr_busy;
53dc1ca1
RC
476
477inv_err:
478 send_status = IB_WC_REM_INV_REQ_ERR;
479 wc.status = IB_WC_LOC_QP_OP_ERR;
480 goto err;
481
482acc_err:
483 send_status = IB_WC_REM_ACCESS_ERR;
484 wc.status = IB_WC_LOC_PROT_ERR;
485err:
486 /* responder goes to error state */
487 ipath_rc_error(qp, wc.status);
488
489serr:
490 spin_lock_irqsave(&sqp->s_lock, flags);
491 ipath_send_complete(sqp, wqe, send_status);
492 if (sqp->ibqp.qp_type == IB_QPT_RC) {
493 int lastwqe = ipath_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
494
495 sqp->s_flags &= ~IPATH_S_BUSY;
496 spin_unlock_irqrestore(&sqp->s_lock, flags);
497 if (lastwqe) {
498 struct ib_event ev;
499
500 ev.device = sqp->ibqp.device;
501 ev.element.qp = &sqp->ibqp;
502 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
503 sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
504 }
505 goto done;
506 }
e509be89
RC
507clr_busy:
508 sqp->s_flags &= ~IPATH_S_BUSY;
53dc1ca1
RC
509unlock:
510 spin_unlock_irqrestore(&sqp->s_lock, flags);
e28c00ad 511done:
e509be89 512 if (qp && atomic_dec_and_test(&qp->refcount))
e28c00ad
BS
513 wake_up(&qp->wait);
514}
515
e2ab41ca 516static void want_buffer(struct ipath_devdata *dd, struct ipath_qp *qp)
34b2aafe 517{
e2ab41ca 518 if (!(dd->ipath_flags & IPATH_HAS_SEND_DMA) ||
e509be89 519 qp->ibqp.qp_type == IB_QPT_SMI) {
124b4dcb
DO
520 unsigned long flags;
521
522 spin_lock_irqsave(&dd->ipath_sendctrl_lock, flags);
523 dd->ipath_sendctrl |= INFINIPATH_S_PIOINTBUFAVAIL;
524 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
525 dd->ipath_sendctrl);
526 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
527 spin_unlock_irqrestore(&dd->ipath_sendctrl_lock, flags);
528 }
34b2aafe
BS
529}
530
e28c00ad
BS
531/**
532 * ipath_no_bufs_available - tell the layer driver we need buffers
533 * @qp: the QP that caused the problem
534 * @dev: the device we ran out of buffers on
535 *
536 * Called when we run out of PIO buffers.
e509be89
RC
537 * If we are now in the error state, return zero to flush the
538 * send work request.
e28c00ad 539 */
e509be89 540static int ipath_no_bufs_available(struct ipath_qp *qp,
4ee97180 541 struct ipath_ibdev *dev)
e28c00ad
BS
542{
543 unsigned long flags;
e509be89 544 int ret = 1;
e28c00ad 545
e28c00ad 546 /*
34b2aafe 547 * Note that as soon as want_buffer() is called and
e28c00ad 548 * possibly before it returns, ipath_ib_piobufavail()
e509be89
RC
549 * could be called. Therefore, put QP on the piowait list before
550 * enabling the PIO avail interrupt.
e28c00ad 551 */
e509be89
RC
552 spin_lock_irqsave(&qp->s_lock, flags);
553 if (ib_ipath_state_ops[qp->state] & IPATH_PROCESS_SEND_OK) {
554 dev->n_piowait++;
555 qp->s_flags |= IPATH_S_WAITING;
556 qp->s_flags &= ~IPATH_S_BUSY;
557 spin_lock(&dev->pending_lock);
558 if (list_empty(&qp->piowait))
559 list_add_tail(&qp->piowait, &dev->piowait);
560 spin_unlock(&dev->pending_lock);
561 } else
562 ret = 0;
563 spin_unlock_irqrestore(&qp->s_lock, flags);
564 if (ret)
565 want_buffer(dev->dd, qp);
566 return ret;
e28c00ad
BS
567}
568
ddd4bb22
BS
569/**
570 * ipath_make_grh - construct a GRH header
571 * @dev: a pointer to the ipath device
572 * @hdr: a pointer to the GRH header being constructed
573 * @grh: the global route address to send to
574 * @hwords: the number of 32 bit words of header being sent
575 * @nwords: the number of 32 bit words of data being sent
576 *
577 * Return the size of the header in 32 bit words.
578 */
579u32 ipath_make_grh(struct ipath_ibdev *dev, struct ib_grh *hdr,
580 struct ib_global_route *grh, u32 hwords, u32 nwords)
581{
582 hdr->version_tclass_flow =
583 cpu_to_be32((6 << 28) |
584 (grh->traffic_class << 20) |
585 grh->flow_label);
586 hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
587 /* next_hdr is defined by C8-7 in ch. 8.4.1 */
588 hdr->next_hdr = 0x1B;
589 hdr->hop_limit = grh->hop_limit;
590 /* The SGID is 32-bit aligned. */
591 hdr->sgid.global.subnet_prefix = dev->gid_prefix;
34b2aafe 592 hdr->sgid.global.interface_id = dev->dd->ipath_guid;
ddd4bb22
BS
593 hdr->dgid = grh->dgid;
594
595 /* GRH header size in 32-bit words. */
596 return sizeof(struct ib_grh) / sizeof(u32);
597}
598
4ee97180
RC
599void ipath_make_ruc_header(struct ipath_ibdev *dev, struct ipath_qp *qp,
600 struct ipath_other_headers *ohdr,
601 u32 bth0, u32 bth2)
602{
603 u16 lrh0;
604 u32 nwords;
605 u32 extra_bytes;
606
607 /* Construct the header. */
608 extra_bytes = -qp->s_cur_size & 3;
609 nwords = (qp->s_cur_size + extra_bytes) >> 2;
610 lrh0 = IPATH_LRH_BTH;
611 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
612 qp->s_hdrwords += ipath_make_grh(dev, &qp->s_hdr.u.l.grh,
613 &qp->remote_ah_attr.grh,
614 qp->s_hdrwords, nwords);
615 lrh0 = IPATH_LRH_GRH;
616 }
617 lrh0 |= qp->remote_ah_attr.sl << 4;
618 qp->s_hdr.lrh[0] = cpu_to_be16(lrh0);
619 qp->s_hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
620 qp->s_hdr.lrh[2] = cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
94035896
RC
621 qp->s_hdr.lrh[3] = cpu_to_be16(dev->dd->ipath_lid |
622 qp->remote_ah_attr.src_path_bits);
4ee97180
RC
623 bth0 |= ipath_get_pkey(dev->dd, qp->s_pkey_index);
624 bth0 |= extra_bytes << 20;
625 ohdr->bth[0] = cpu_to_be32(bth0 | (1 << 22));
626 ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
627 ohdr->bth[2] = cpu_to_be32(bth2);
628}
629
ddd4bb22 630/**
4ee97180 631 * ipath_do_send - perform a send on a QP
ddd4bb22
BS
632 * @data: contains a pointer to the QP
633 *
634 * Process entries in the send work queue until credit or queue is
635 * exhausted. Only allow one CPU to send a packet per QP (tasklet).
4ee97180 636 * Otherwise, two threads could send packets out of order.
ddd4bb22 637 */
4ee97180 638void ipath_do_send(unsigned long data)
ddd4bb22
BS
639{
640 struct ipath_qp *qp = (struct ipath_qp *)data;
641 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
4ee97180 642 int (*make_req)(struct ipath_qp *qp);
e509be89 643 unsigned long flags;
ddd4bb22 644
4ee97180
RC
645 if ((qp->ibqp.qp_type == IB_QPT_RC ||
646 qp->ibqp.qp_type == IB_QPT_UC) &&
647 qp->remote_ah_attr.dlid == dev->dd->ipath_lid) {
ddd4bb22 648 ipath_ruc_loopback(qp);
e509be89 649 goto bail;
ddd4bb22
BS
650 }
651
4ee97180
RC
652 if (qp->ibqp.qp_type == IB_QPT_RC)
653 make_req = ipath_make_rc_req;
654 else if (qp->ibqp.qp_type == IB_QPT_UC)
655 make_req = ipath_make_uc_req;
656 else
657 make_req = ipath_make_ud_req;
ddd4bb22 658
e509be89
RC
659 spin_lock_irqsave(&qp->s_lock, flags);
660
661 /* Return if we are already busy processing a work request. */
662 if ((qp->s_flags & (IPATH_S_BUSY | IPATH_S_ANY_WAIT)) ||
663 !(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_OR_FLUSH_SEND)) {
664 spin_unlock_irqrestore(&qp->s_lock, flags);
665 goto bail;
666 }
667
668 qp->s_flags |= IPATH_S_BUSY;
669
670 spin_unlock_irqrestore(&qp->s_lock, flags);
671
ddd4bb22
BS
672again:
673 /* Check for a constructed packet to be sent. */
674 if (qp->s_hdrwords != 0) {
675 /*
676 * If no PIO bufs are available, return. An interrupt will
677 * call ipath_ib_piobufavail() when one is available.
678 */
4ee97180
RC
679 if (ipath_verbs_send(qp, &qp->s_hdr, qp->s_hdrwords,
680 qp->s_cur_sge, qp->s_cur_size)) {
e509be89
RC
681 if (ipath_no_bufs_available(qp, dev))
682 goto bail;
ddd4bb22
BS
683 }
684 dev->n_unicast_xmit++;
685 /* Record that we sent the packet and s_hdr is empty. */
686 qp->s_hdrwords = 0;
687 }
688
4ee97180
RC
689 if (make_req(qp))
690 goto again;
e509be89 691
4ee97180
RC
692bail:;
693}
ddd4bb22 694
e509be89
RC
695/*
696 * This should be called with s_lock held.
697 */
4ee97180
RC
698void ipath_send_complete(struct ipath_qp *qp, struct ipath_swqe *wqe,
699 enum ib_wc_status status)
700{
e509be89
RC
701 u32 old_last, last;
702
703 if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_OR_FLUSH_SEND))
704 return;
ddd4bb22 705
4ee97180
RC
706 /* See ch. 11.2.4.1 and 10.7.3.1 */
707 if (!(qp->s_flags & IPATH_S_SIGNAL_REQ_WR) ||
708 (wqe->wr.send_flags & IB_SEND_SIGNALED) ||
709 status != IB_WC_SUCCESS) {
710 struct ib_wc wc;
ddd4bb22 711
53dc1ca1 712 memset(&wc, 0, sizeof wc);
4ee97180
RC
713 wc.wr_id = wqe->wr.wr_id;
714 wc.status = status;
715 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
4ee97180 716 wc.qp = &qp->ibqp;
53dc1ca1
RC
717 if (status == IB_WC_SUCCESS)
718 wc.byte_len = wqe->length;
719 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc,
720 status != IB_WC_SUCCESS);
4ee97180 721 }
fffbfeaa 722
e509be89 723 old_last = last = qp->s_last;
fffbfeaa
RC
724 if (++last >= qp->s_size)
725 last = 0;
726 qp->s_last = last;
e509be89
RC
727 if (qp->s_cur == old_last)
728 qp->s_cur = last;
729 if (qp->s_tail == old_last)
730 qp->s_tail = last;
731 if (qp->state == IB_QPS_SQD && last == qp->s_cur)
732 qp->s_draining = 0;
ddd4bb22 733}