d47ad7c8a0d25d49b59d3e44ff9dfa3270f78c83
[safe/jmp/linux-2.6] / drivers / infiniband / hw / ipath / ipath_ruc.c
1 /*
2  * Copyright (c) 2006 QLogic, Inc. All rights reserved.
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
34 #include "ipath_verbs.h"
35 #include "ipath_kernel.h"
36
37 /*
38  * Convert the AETH RNR timeout code into the number of milliseconds.
39  */
40 const 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  */
83 void 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
109 static int init_sge(struct ipath_qp *qp, struct ipath_rwqe *wqe)
110 {
111         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) ||
121                     !ipath_lkey_ok(qp, &qp->r_sg_list[j], &wqe->sg_list[i],
122                                    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
133 bad_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;
140         wc.qp = &qp->ibqp;
141         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;
151 bail:
152         return ret;
153 }
154
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  *
162  * Can be called from interrupt level.
163  */
164 int ipath_get_rwqe(struct ipath_qp *qp, int wr_id_only)
165 {
166         unsigned long flags;
167         struct ipath_rq *rq;
168         struct ipath_rwq *wq;
169         struct ipath_srq *srq;
170         struct ipath_rwqe *wqe;
171         void (*handler)(struct ib_event *, void *);
172         u32 tail;
173         int ret;
174
175         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;
182                 rq = &qp->r_rq;
183         }
184
185         spin_lock_irqsave(&rq->lock, flags);
186         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                 }
197                 wqe = get_rwqe_ptr(rq, tail);
198                 if (++tail >= rq->size)
199                         tail = 0;
200         } while (!wr_id_only && !init_sge(qp, wqe));
201         qp->r_wr_id = wqe->wr_id;
202         wq->tail = tail;
203
204         ret = 1;
205         qp->r_wrid_valid = 1;
206         if (handler) {
207                 u32 n;
208
209                 /*
210                  * validate head pointer value and compute
211                  * the number of remaining WQEs.
212                  */
213                 n = wq->head;
214                 if (n >= rq->size)
215                         n = 0;
216                 if (n < tail)
217                         n += rq->size - tail;
218                 else
219                         n -= tail;
220                 if (n < srq->limit) {
221                         struct ib_event ev;
222
223                         srq->limit = 0;
224                         spin_unlock_irqrestore(&rq->lock, flags);
225                         ev.device = qp->ibqp.device;
226                         ev.element.srq = qp->ibqp.srq;
227                         ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
228                         handler(&ev, srq->ibsrq.srq_context);
229                         goto bail;
230                 }
231         }
232         spin_unlock_irqrestore(&rq->lock, flags);
233
234 bail:
235         return ret;
236 }
237
238 /**
239  * ipath_ruc_loopback - handle UC and RC lookback requests
240  * @sqp: the loopback QP
241  *
242  * This is called from ipath_do_uc_send() or ipath_do_rc_send() to
243  * forward a WQE addressed to the same HCA.
244  * Note that although we are single threaded due to the tasklet, we still
245  * have to protect against post_send().  We don't have to worry about
246  * receive interrupts since this is a connected protocol and all packets
247  * will pass through here.
248  */
249 static void ipath_ruc_loopback(struct ipath_qp *sqp)
250 {
251         struct ipath_ibdev *dev = to_idev(sqp->ibqp.device);
252         struct ipath_qp *qp;
253         struct ipath_swqe *wqe;
254         struct ipath_sge *sge;
255         unsigned long flags;
256         struct ib_wc wc;
257         u64 sdata;
258         atomic64_t *maddr;
259
260         qp = ipath_lookup_qpn(&dev->qp_table, sqp->remote_qpn);
261         if (!qp) {
262                 dev->n_pkt_drops++;
263                 return;
264         }
265
266 again:
267         spin_lock_irqsave(&sqp->s_lock, flags);
268
269         if (!(ib_ipath_state_ops[sqp->state] & IPATH_PROCESS_SEND_OK) ||
270             sqp->s_rnr_timeout) {
271                 spin_unlock_irqrestore(&sqp->s_lock, flags);
272                 goto done;
273         }
274
275         /* Get the next send request. */
276         if (sqp->s_last == sqp->s_head) {
277                 /* Send work queue is empty. */
278                 spin_unlock_irqrestore(&sqp->s_lock, flags);
279                 goto done;
280         }
281
282         /*
283          * We can rely on the entry not changing without the s_lock
284          * being held until we update s_last.
285          */
286         wqe = get_swqe_ptr(sqp, sqp->s_last);
287         spin_unlock_irqrestore(&sqp->s_lock, flags);
288
289         wc.wc_flags = 0;
290         wc.imm_data = 0;
291
292         sqp->s_sge.sge = wqe->sg_list[0];
293         sqp->s_sge.sg_list = wqe->sg_list + 1;
294         sqp->s_sge.num_sge = wqe->wr.num_sge;
295         sqp->s_len = wqe->length;
296         switch (wqe->wr.opcode) {
297         case IB_WR_SEND_WITH_IMM:
298                 wc.wc_flags = IB_WC_WITH_IMM;
299                 wc.imm_data = wqe->wr.imm_data;
300                 /* FALLTHROUGH */
301         case IB_WR_SEND:
302                 if (!ipath_get_rwqe(qp, 0)) {
303                 rnr_nak:
304                         /* Handle RNR NAK */
305                         if (qp->ibqp.qp_type == IB_QPT_UC)
306                                 goto send_comp;
307                         if (sqp->s_rnr_retry == 0) {
308                                 wc.status = IB_WC_RNR_RETRY_EXC_ERR;
309                                 goto err;
310                         }
311                         if (sqp->s_rnr_retry_cnt < 7)
312                                 sqp->s_rnr_retry--;
313                         dev->n_rnr_naks++;
314                         sqp->s_rnr_timeout =
315                                 ib_ipath_rnr_table[qp->r_min_rnr_timer];
316                         ipath_insert_rnr_queue(sqp);
317                         goto done;
318                 }
319                 break;
320
321         case IB_WR_RDMA_WRITE_WITH_IMM:
322                 if (unlikely(!(qp->qp_access_flags &
323                                IB_ACCESS_REMOTE_WRITE))) {
324                         wc.status = IB_WC_REM_INV_REQ_ERR;
325                         goto err;
326                 }
327                 wc.wc_flags = IB_WC_WITH_IMM;
328                 wc.imm_data = wqe->wr.imm_data;
329                 if (!ipath_get_rwqe(qp, 1))
330                         goto rnr_nak;
331                 /* FALLTHROUGH */
332         case IB_WR_RDMA_WRITE:
333                 if (unlikely(!(qp->qp_access_flags &
334                                IB_ACCESS_REMOTE_WRITE))) {
335                         wc.status = IB_WC_REM_INV_REQ_ERR;
336                         goto err;
337                 }
338                 if (wqe->length == 0)
339                         break;
340                 if (unlikely(!ipath_rkey_ok(qp, &qp->r_sge, wqe->length,
341                                             wqe->wr.wr.rdma.remote_addr,
342                                             wqe->wr.wr.rdma.rkey,
343                                             IB_ACCESS_REMOTE_WRITE))) {
344                 acc_err:
345                         wc.status = IB_WC_REM_ACCESS_ERR;
346                 err:
347                         wc.wr_id = wqe->wr.wr_id;
348                         wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
349                         wc.vendor_err = 0;
350                         wc.byte_len = 0;
351                         wc.qp = &sqp->ibqp;
352                         wc.src_qp = sqp->remote_qpn;
353                         wc.pkey_index = 0;
354                         wc.slid = sqp->remote_ah_attr.dlid;
355                         wc.sl = sqp->remote_ah_attr.sl;
356                         wc.dlid_path_bits = 0;
357                         wc.port_num = 0;
358                         spin_lock_irqsave(&sqp->s_lock, flags);
359                         ipath_sqerror_qp(sqp, &wc);
360                         spin_unlock_irqrestore(&sqp->s_lock, flags);
361                         goto done;
362                 }
363                 break;
364
365         case IB_WR_RDMA_READ:
366                 if (unlikely(!(qp->qp_access_flags &
367                                IB_ACCESS_REMOTE_READ))) {
368                         wc.status = IB_WC_REM_INV_REQ_ERR;
369                         goto err;
370                 }
371                 if (unlikely(!ipath_rkey_ok(qp, &sqp->s_sge, wqe->length,
372                                             wqe->wr.wr.rdma.remote_addr,
373                                             wqe->wr.wr.rdma.rkey,
374                                             IB_ACCESS_REMOTE_READ)))
375                         goto acc_err;
376                 qp->r_sge.sge = wqe->sg_list[0];
377                 qp->r_sge.sg_list = wqe->sg_list + 1;
378                 qp->r_sge.num_sge = wqe->wr.num_sge;
379                 break;
380
381         case IB_WR_ATOMIC_CMP_AND_SWP:
382         case IB_WR_ATOMIC_FETCH_AND_ADD:
383                 if (unlikely(!(qp->qp_access_flags &
384                                IB_ACCESS_REMOTE_ATOMIC))) {
385                         wc.status = IB_WC_REM_INV_REQ_ERR;
386                         goto err;
387                 }
388                 if (unlikely(!ipath_rkey_ok(qp, &qp->r_sge, sizeof(u64),
389                                             wqe->wr.wr.atomic.remote_addr,
390                                             wqe->wr.wr.atomic.rkey,
391                                             IB_ACCESS_REMOTE_ATOMIC)))
392                         goto acc_err;
393                 /* Perform atomic OP and save result. */
394                 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
395                 sdata = wqe->wr.wr.atomic.compare_add;
396                 *(u64 *) sqp->s_sge.sge.vaddr =
397                         (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
398                         (u64) atomic64_add_return(sdata, maddr) - sdata :
399                         (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
400                                       sdata, wqe->wr.wr.atomic.swap);
401                 goto send_comp;
402
403         default:
404                 goto done;
405         }
406
407         sge = &sqp->s_sge.sge;
408         while (sqp->s_len) {
409                 u32 len = sqp->s_len;
410
411                 if (len > sge->length)
412                         len = sge->length;
413                 if (len > sge->sge_length)
414                         len = sge->sge_length;
415                 BUG_ON(len == 0);
416                 ipath_copy_sge(&qp->r_sge, sge->vaddr, len);
417                 sge->vaddr += len;
418                 sge->length -= len;
419                 sge->sge_length -= len;
420                 if (sge->sge_length == 0) {
421                         if (--sqp->s_sge.num_sge)
422                                 *sge = *sqp->s_sge.sg_list++;
423                 } else if (sge->length == 0 && sge->mr != NULL) {
424                         if (++sge->n >= IPATH_SEGSZ) {
425                                 if (++sge->m >= sge->mr->mapsz)
426                                         break;
427                                 sge->n = 0;
428                         }
429                         sge->vaddr =
430                                 sge->mr->map[sge->m]->segs[sge->n].vaddr;
431                         sge->length =
432                                 sge->mr->map[sge->m]->segs[sge->n].length;
433                 }
434                 sqp->s_len -= len;
435         }
436
437         if (wqe->wr.opcode == IB_WR_RDMA_WRITE ||
438             wqe->wr.opcode == IB_WR_RDMA_READ)
439                 goto send_comp;
440
441         if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
442                 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
443         else
444                 wc.opcode = IB_WC_RECV;
445         wc.wr_id = qp->r_wr_id;
446         wc.status = IB_WC_SUCCESS;
447         wc.vendor_err = 0;
448         wc.byte_len = wqe->length;
449         wc.qp = &qp->ibqp;
450         wc.src_qp = qp->remote_qpn;
451         /* XXX do we know which pkey matched? Only needed for GSI. */
452         wc.pkey_index = 0;
453         wc.slid = qp->remote_ah_attr.dlid;
454         wc.sl = qp->remote_ah_attr.sl;
455         wc.dlid_path_bits = 0;
456         /* Signal completion event if the solicited bit is set. */
457         ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
458                        wqe->wr.send_flags & IB_SEND_SOLICITED);
459
460 send_comp:
461         sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
462
463         if (!(sqp->s_flags & IPATH_S_SIGNAL_REQ_WR) ||
464             (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
465                 wc.wr_id = wqe->wr.wr_id;
466                 wc.status = IB_WC_SUCCESS;
467                 wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
468                 wc.vendor_err = 0;
469                 wc.byte_len = wqe->length;
470                 wc.qp = &sqp->ibqp;
471                 wc.src_qp = 0;
472                 wc.pkey_index = 0;
473                 wc.slid = 0;
474                 wc.sl = 0;
475                 wc.dlid_path_bits = 0;
476                 wc.port_num = 0;
477                 ipath_cq_enter(to_icq(sqp->ibqp.send_cq), &wc, 0);
478         }
479
480         /* Update s_last now that we are finished with the SWQE */
481         spin_lock_irqsave(&sqp->s_lock, flags);
482         if (++sqp->s_last >= sqp->s_size)
483                 sqp->s_last = 0;
484         spin_unlock_irqrestore(&sqp->s_lock, flags);
485         goto again;
486
487 done:
488         if (atomic_dec_and_test(&qp->refcount))
489                 wake_up(&qp->wait);
490 }
491
492 static int want_buffer(struct ipath_devdata *dd)
493 {
494         set_bit(IPATH_S_PIOINTBUFAVAIL, &dd->ipath_sendctrl);
495         ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
496                          dd->ipath_sendctrl);
497
498         return 0;
499 }
500
501 /**
502  * ipath_no_bufs_available - tell the layer driver we need buffers
503  * @qp: the QP that caused the problem
504  * @dev: the device we ran out of buffers on
505  *
506  * Called when we run out of PIO buffers.
507  */
508 void ipath_no_bufs_available(struct ipath_qp *qp, struct ipath_ibdev *dev)
509 {
510         unsigned long flags;
511
512         spin_lock_irqsave(&dev->pending_lock, flags);
513         if (list_empty(&qp->piowait))
514                 list_add_tail(&qp->piowait, &dev->piowait);
515         spin_unlock_irqrestore(&dev->pending_lock, flags);
516         /*
517          * Note that as soon as want_buffer() is called and
518          * possibly before it returns, ipath_ib_piobufavail()
519          * could be called.  If we are still in the tasklet function,
520          * tasklet_hi_schedule() will not call us until the next time
521          * tasklet_hi_schedule() is called.
522          * We leave the busy flag set so that another post send doesn't
523          * try to put the same QP on the piowait list again.
524          */
525         want_buffer(dev->dd);
526         dev->n_piowait++;
527 }
528
529 /**
530  * ipath_post_ruc_send - post RC and UC sends
531  * @qp: the QP to post on
532  * @wr: the work request to send
533  */
534 int ipath_post_ruc_send(struct ipath_qp *qp, struct ib_send_wr *wr)
535 {
536         struct ipath_swqe *wqe;
537         unsigned long flags;
538         u32 next;
539         int i, j;
540         int acc;
541         int ret;
542
543         /*
544          * Don't allow RDMA reads or atomic operations on UC or
545          * undefined operations.
546          * Make sure buffer is large enough to hold the result for atomics.
547          */
548         if (qp->ibqp.qp_type == IB_QPT_UC) {
549                 if ((unsigned) wr->opcode >= IB_WR_RDMA_READ) {
550                         ret = -EINVAL;
551                         goto bail;
552                 }
553         } else if ((unsigned) wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD) {
554                 ret = -EINVAL;
555                 goto bail;
556         } else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
557                    (wr->num_sge == 0 ||
558                     wr->sg_list[0].length < sizeof(u64) ||
559                     wr->sg_list[0].addr & (sizeof(u64) - 1))) {
560                 ret = -EINVAL;
561                 goto bail;
562         } else if (wr->opcode >= IB_WR_RDMA_READ && !qp->s_max_rd_atomic) {
563                 ret = -EINVAL;
564                 goto bail;
565         }
566         /* IB spec says that num_sge == 0 is OK. */
567         if (wr->num_sge > qp->s_max_sge) {
568                 ret = -ENOMEM;
569                 goto bail;
570         }
571         spin_lock_irqsave(&qp->s_lock, flags);
572         next = qp->s_head + 1;
573         if (next >= qp->s_size)
574                 next = 0;
575         if (next == qp->s_last) {
576                 spin_unlock_irqrestore(&qp->s_lock, flags);
577                 ret = -EINVAL;
578                 goto bail;
579         }
580
581         wqe = get_swqe_ptr(qp, qp->s_head);
582         wqe->wr = *wr;
583         wqe->ssn = qp->s_ssn++;
584         wqe->sg_list[0].mr = NULL;
585         wqe->sg_list[0].vaddr = NULL;
586         wqe->sg_list[0].length = 0;
587         wqe->sg_list[0].sge_length = 0;
588         wqe->length = 0;
589         acc = wr->opcode >= IB_WR_RDMA_READ ? IB_ACCESS_LOCAL_WRITE : 0;
590         for (i = 0, j = 0; i < wr->num_sge; i++) {
591                 if (to_ipd(qp->ibqp.pd)->user && wr->sg_list[i].lkey == 0) {
592                         spin_unlock_irqrestore(&qp->s_lock, flags);
593                         ret = -EINVAL;
594                         goto bail;
595                 }
596                 if (wr->sg_list[i].length == 0)
597                         continue;
598                 if (!ipath_lkey_ok(qp, &wqe->sg_list[j], &wr->sg_list[i],
599                                    acc)) {
600                         spin_unlock_irqrestore(&qp->s_lock, flags);
601                         ret = -EINVAL;
602                         goto bail;
603                 }
604                 wqe->length += wr->sg_list[i].length;
605                 j++;
606         }
607         wqe->wr.num_sge = j;
608         qp->s_head = next;
609         spin_unlock_irqrestore(&qp->s_lock, flags);
610
611         ipath_do_ruc_send((unsigned long) qp);
612
613         ret = 0;
614
615 bail:
616         return ret;
617 }
618
619 /**
620  * ipath_make_grh - construct a GRH header
621  * @dev: a pointer to the ipath device
622  * @hdr: a pointer to the GRH header being constructed
623  * @grh: the global route address to send to
624  * @hwords: the number of 32 bit words of header being sent
625  * @nwords: the number of 32 bit words of data being sent
626  *
627  * Return the size of the header in 32 bit words.
628  */
629 u32 ipath_make_grh(struct ipath_ibdev *dev, struct ib_grh *hdr,
630                    struct ib_global_route *grh, u32 hwords, u32 nwords)
631 {
632         hdr->version_tclass_flow =
633                 cpu_to_be32((6 << 28) |
634                             (grh->traffic_class << 20) |
635                             grh->flow_label);
636         hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
637         /* next_hdr is defined by C8-7 in ch. 8.4.1 */
638         hdr->next_hdr = 0x1B;
639         hdr->hop_limit = grh->hop_limit;
640         /* The SGID is 32-bit aligned. */
641         hdr->sgid.global.subnet_prefix = dev->gid_prefix;
642         hdr->sgid.global.interface_id = dev->dd->ipath_guid;
643         hdr->dgid = grh->dgid;
644
645         /* GRH header size in 32-bit words. */
646         return sizeof(struct ib_grh) / sizeof(u32);
647 }
648
649 /**
650  * ipath_do_ruc_send - perform a send on an RC or UC QP
651  * @data: contains a pointer to the QP
652  *
653  * Process entries in the send work queue until credit or queue is
654  * exhausted.  Only allow one CPU to send a packet per QP (tasklet).
655  * Otherwise, after we drop the QP s_lock, two threads could send
656  * packets out of order.
657  */
658 void ipath_do_ruc_send(unsigned long data)
659 {
660         struct ipath_qp *qp = (struct ipath_qp *)data;
661         struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
662         unsigned long flags;
663         u16 lrh0;
664         u32 nwords;
665         u32 extra_bytes;
666         u32 bth0;
667         u32 bth2;
668         u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
669         struct ipath_other_headers *ohdr;
670
671         if (test_and_set_bit(IPATH_S_BUSY, &qp->s_busy))
672                 goto bail;
673
674         if (unlikely(qp->remote_ah_attr.dlid == dev->dd->ipath_lid)) {
675                 ipath_ruc_loopback(qp);
676                 goto clear;
677         }
678
679         ohdr = &qp->s_hdr.u.oth;
680         if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
681                 ohdr = &qp->s_hdr.u.l.oth;
682
683 again:
684         /* Check for a constructed packet to be sent. */
685         if (qp->s_hdrwords != 0) {
686                 /*
687                  * If no PIO bufs are available, return.  An interrupt will
688                  * call ipath_ib_piobufavail() when one is available.
689                  */
690                 if (ipath_verbs_send(dev->dd, qp->s_hdrwords,
691                                      (u32 *) &qp->s_hdr, qp->s_cur_size,
692                                      qp->s_cur_sge)) {
693                         ipath_no_bufs_available(qp, dev);
694                         goto bail;
695                 }
696                 dev->n_unicast_xmit++;
697                 /* Record that we sent the packet and s_hdr is empty. */
698                 qp->s_hdrwords = 0;
699         }
700
701         /*
702          * The lock is needed to synchronize between setting
703          * qp->s_ack_state, resend timer, and post_send().
704          */
705         spin_lock_irqsave(&qp->s_lock, flags);
706
707         if (!((qp->ibqp.qp_type == IB_QPT_RC) ?
708                ipath_make_rc_req(qp, ohdr, pmtu, &bth0, &bth2) :
709                ipath_make_uc_req(qp, ohdr, pmtu, &bth0, &bth2))) {
710                 /*
711                  * Clear the busy bit before unlocking to avoid races with
712                  * adding new work queue items and then failing to process
713                  * them.
714                  */
715                 clear_bit(IPATH_S_BUSY, &qp->s_busy);
716                 spin_unlock_irqrestore(&qp->s_lock, flags);
717                 goto bail;
718         }
719
720         spin_unlock_irqrestore(&qp->s_lock, flags);
721
722         /* Construct the header. */
723         extra_bytes = (4 - qp->s_cur_size) & 3;
724         nwords = (qp->s_cur_size + extra_bytes) >> 2;
725         lrh0 = IPATH_LRH_BTH;
726         if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
727                 qp->s_hdrwords += ipath_make_grh(dev, &qp->s_hdr.u.l.grh,
728                                                  &qp->remote_ah_attr.grh,
729                                                  qp->s_hdrwords, nwords);
730                 lrh0 = IPATH_LRH_GRH;
731         }
732         lrh0 |= qp->remote_ah_attr.sl << 4;
733         qp->s_hdr.lrh[0] = cpu_to_be16(lrh0);
734         qp->s_hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
735         qp->s_hdr.lrh[2] = cpu_to_be16(qp->s_hdrwords + nwords +
736                                        SIZE_OF_CRC);
737         qp->s_hdr.lrh[3] = cpu_to_be16(dev->dd->ipath_lid);
738         bth0 |= ipath_get_pkey(dev->dd, qp->s_pkey_index);
739         bth0 |= extra_bytes << 20;
740         ohdr->bth[0] = cpu_to_be32(bth0);
741         ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
742         ohdr->bth[2] = cpu_to_be32(bth2);
743
744         /* Check for more work to do. */
745         goto again;
746
747 clear:
748         clear_bit(IPATH_S_BUSY, &qp->s_busy);
749 bail:
750         return;
751 }