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