GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / infiniband / hw / qib / qib_ruc.c
1 /*
2  * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. 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 <linux/spinlock.h>
35 #include <rdma/ib_smi.h>
36
37 #include "qib.h"
38 #include "qib_mad.h"
39
40 /*
41  * Convert the AETH RNR timeout code into the number of microseconds.
42  */
43 const u32 ib_qib_rnr_table[32] = {
44         655360, /* 00: 655.36 */
45         10,     /* 01:    .01 */
46         20,     /* 02     .02 */
47         30,     /* 03:    .03 */
48         40,     /* 04:    .04 */
49         60,     /* 05:    .06 */
50         80,     /* 06:    .08 */
51         120,    /* 07:    .12 */
52         160,    /* 08:    .16 */
53         240,    /* 09:    .24 */
54         320,    /* 0A:    .32 */
55         480,    /* 0B:    .48 */
56         640,    /* 0C:    .64 */
57         960,    /* 0D:    .96 */
58         1280,   /* 0E:   1.28 */
59         1920,   /* 0F:   1.92 */
60         2560,   /* 10:   2.56 */
61         3840,   /* 11:   3.84 */
62         5120,   /* 12:   5.12 */
63         7680,   /* 13:   7.68 */
64         10240,  /* 14:  10.24 */
65         15360,  /* 15:  15.36 */
66         20480,  /* 16:  20.48 */
67         30720,  /* 17:  30.72 */
68         40960,  /* 18:  40.96 */
69         61440,  /* 19:  61.44 */
70         81920,  /* 1A:  81.92 */
71         122880, /* 1B: 122.88 */
72         163840, /* 1C: 163.84 */
73         245760, /* 1D: 245.76 */
74         327680, /* 1E: 327.68 */
75         491520  /* 1F: 491.52 */
76 };
77
78 /*
79  * Validate a RWQE and fill in the SGE state.
80  * Return 1 if OK.
81  */
82 static int qib_init_sge(struct rvt_qp *qp, struct rvt_rwqe *wqe)
83 {
84         int i, j, ret;
85         struct ib_wc wc;
86         struct rvt_lkey_table *rkt;
87         struct rvt_pd *pd;
88         struct rvt_sge_state *ss;
89
90         rkt = &to_idev(qp->ibqp.device)->rdi.lkey_table;
91         pd = ibpd_to_rvtpd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd);
92         ss = &qp->r_sge;
93         ss->sg_list = qp->r_sg_list;
94         qp->r_len = 0;
95         for (i = j = 0; i < wqe->num_sge; i++) {
96                 if (wqe->sg_list[i].length == 0)
97                         continue;
98                 /* Check LKEY */
99                 if (!rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,
100                                  &wqe->sg_list[i], IB_ACCESS_LOCAL_WRITE))
101                         goto bad_lkey;
102                 qp->r_len += wqe->sg_list[i].length;
103                 j++;
104         }
105         ss->num_sge = j;
106         ss->total_len = qp->r_len;
107         ret = 1;
108         goto bail;
109
110 bad_lkey:
111         while (j) {
112                 struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;
113
114                 rvt_put_mr(sge->mr);
115         }
116         ss->num_sge = 0;
117         memset(&wc, 0, sizeof(wc));
118         wc.wr_id = wqe->wr_id;
119         wc.status = IB_WC_LOC_PROT_ERR;
120         wc.opcode = IB_WC_RECV;
121         wc.qp = &qp->ibqp;
122         /* Signal solicited completion event. */
123         rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
124         ret = 0;
125 bail:
126         return ret;
127 }
128
129 /**
130  * qib_get_rwqe - copy the next RWQE into the QP's RWQE
131  * @qp: the QP
132  * @wr_id_only: update qp->r_wr_id only, not qp->r_sge
133  *
134  * Return -1 if there is a local error, 0 if no RWQE is available,
135  * otherwise return 1.
136  *
137  * Can be called from interrupt level.
138  */
139 int qib_get_rwqe(struct rvt_qp *qp, int wr_id_only)
140 {
141         unsigned long flags;
142         struct rvt_rq *rq;
143         struct rvt_rwq *wq;
144         struct rvt_srq *srq;
145         struct rvt_rwqe *wqe;
146         void (*handler)(struct ib_event *, void *);
147         u32 tail;
148         int ret;
149
150         if (qp->ibqp.srq) {
151                 srq = ibsrq_to_rvtsrq(qp->ibqp.srq);
152                 handler = srq->ibsrq.event_handler;
153                 rq = &srq->rq;
154         } else {
155                 srq = NULL;
156                 handler = NULL;
157                 rq = &qp->r_rq;
158         }
159
160         spin_lock_irqsave(&rq->lock, flags);
161         if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
162                 ret = 0;
163                 goto unlock;
164         }
165
166         wq = rq->wq;
167         tail = wq->tail;
168         /* Validate tail before using it since it is user writable. */
169         if (tail >= rq->size)
170                 tail = 0;
171         if (unlikely(tail == wq->head)) {
172                 ret = 0;
173                 goto unlock;
174         }
175         /* Make sure entry is read after head index is read. */
176         smp_rmb();
177         wqe = rvt_get_rwqe_ptr(rq, tail);
178         /*
179          * Even though we update the tail index in memory, the verbs
180          * consumer is not supposed to post more entries until a
181          * completion is generated.
182          */
183         if (++tail >= rq->size)
184                 tail = 0;
185         wq->tail = tail;
186         if (!wr_id_only && !qib_init_sge(qp, wqe)) {
187                 ret = -1;
188                 goto unlock;
189         }
190         qp->r_wr_id = wqe->wr_id;
191
192         ret = 1;
193         set_bit(RVT_R_WRID_VALID, &qp->r_aflags);
194         if (handler) {
195                 u32 n;
196
197                 /*
198                  * Validate head pointer value and compute
199                  * the number of remaining WQEs.
200                  */
201                 n = wq->head;
202                 if (n >= rq->size)
203                         n = 0;
204                 if (n < tail)
205                         n += rq->size - tail;
206                 else
207                         n -= tail;
208                 if (n < srq->limit) {
209                         struct ib_event ev;
210
211                         srq->limit = 0;
212                         spin_unlock_irqrestore(&rq->lock, flags);
213                         ev.device = qp->ibqp.device;
214                         ev.element.srq = qp->ibqp.srq;
215                         ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
216                         handler(&ev, srq->ibsrq.srq_context);
217                         goto bail;
218                 }
219         }
220 unlock:
221         spin_unlock_irqrestore(&rq->lock, flags);
222 bail:
223         return ret;
224 }
225
226 /*
227  * Switch to alternate path.
228  * The QP s_lock should be held and interrupts disabled.
229  */
230 void qib_migrate_qp(struct rvt_qp *qp)
231 {
232         struct ib_event ev;
233
234         qp->s_mig_state = IB_MIG_MIGRATED;
235         qp->remote_ah_attr = qp->alt_ah_attr;
236         qp->port_num = qp->alt_ah_attr.port_num;
237         qp->s_pkey_index = qp->s_alt_pkey_index;
238
239         ev.device = qp->ibqp.device;
240         ev.element.qp = &qp->ibqp;
241         ev.event = IB_EVENT_PATH_MIG;
242         qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
243 }
244
245 static __be64 get_sguid(struct qib_ibport *ibp, unsigned index)
246 {
247         if (!index) {
248                 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
249
250                 return ppd->guid;
251         }
252         return ibp->guids[index - 1];
253 }
254
255 static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)
256 {
257         return (gid->global.interface_id == id &&
258                 (gid->global.subnet_prefix == gid_prefix ||
259                  gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));
260 }
261
262 /*
263  *
264  * This should be called with the QP r_lock held.
265  *
266  * The s_lock will be acquired around the qib_migrate_qp() call.
267  */
268 int qib_ruc_check_hdr(struct qib_ibport *ibp, struct ib_header *hdr,
269                       int has_grh, struct rvt_qp *qp, u32 bth0)
270 {
271         __be64 guid;
272         unsigned long flags;
273
274         if (qp->s_mig_state == IB_MIG_ARMED && (bth0 & IB_BTH_MIG_REQ)) {
275                 if (!has_grh) {
276                         if (qp->alt_ah_attr.ah_flags & IB_AH_GRH)
277                                 goto err;
278                 } else {
279                         if (!(qp->alt_ah_attr.ah_flags & IB_AH_GRH))
280                                 goto err;
281                         guid = get_sguid(ibp, qp->alt_ah_attr.grh.sgid_index);
282                         if (!gid_ok(&hdr->u.l.grh.dgid,
283                                     ibp->rvp.gid_prefix, guid))
284                                 goto err;
285                         if (!gid_ok(&hdr->u.l.grh.sgid,
286                             qp->alt_ah_attr.grh.dgid.global.subnet_prefix,
287                             qp->alt_ah_attr.grh.dgid.global.interface_id))
288                                 goto err;
289                 }
290                 if (!qib_pkey_ok((u16)bth0,
291                                  qib_get_pkey(ibp, qp->s_alt_pkey_index))) {
292                         qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_PKEY,
293                                       (u16)bth0,
294                                       (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
295                                       0, qp->ibqp.qp_num,
296                                       hdr->lrh[3], hdr->lrh[1]);
297                         goto err;
298                 }
299                 /* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
300                 if (be16_to_cpu(hdr->lrh[3]) != qp->alt_ah_attr.dlid ||
301                     ppd_from_ibp(ibp)->port != qp->alt_ah_attr.port_num)
302                         goto err;
303                 spin_lock_irqsave(&qp->s_lock, flags);
304                 qib_migrate_qp(qp);
305                 spin_unlock_irqrestore(&qp->s_lock, flags);
306         } else {
307                 if (!has_grh) {
308                         if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
309                                 goto err;
310                 } else {
311                         if (!(qp->remote_ah_attr.ah_flags & IB_AH_GRH))
312                                 goto err;
313                         guid = get_sguid(ibp,
314                                          qp->remote_ah_attr.grh.sgid_index);
315                         if (!gid_ok(&hdr->u.l.grh.dgid,
316                                     ibp->rvp.gid_prefix, guid))
317                                 goto err;
318                         if (!gid_ok(&hdr->u.l.grh.sgid,
319                             qp->remote_ah_attr.grh.dgid.global.subnet_prefix,
320                             qp->remote_ah_attr.grh.dgid.global.interface_id))
321                                 goto err;
322                 }
323                 if (!qib_pkey_ok((u16)bth0,
324                                  qib_get_pkey(ibp, qp->s_pkey_index))) {
325                         qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_PKEY,
326                                       (u16)bth0,
327                                       (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
328                                       0, qp->ibqp.qp_num,
329                                       hdr->lrh[3], hdr->lrh[1]);
330                         goto err;
331                 }
332                 /* Validate the SLID. See Ch. 9.6.1.5 */
333                 if (be16_to_cpu(hdr->lrh[3]) != qp->remote_ah_attr.dlid ||
334                     ppd_from_ibp(ibp)->port != qp->port_num)
335                         goto err;
336                 if (qp->s_mig_state == IB_MIG_REARM &&
337                     !(bth0 & IB_BTH_MIG_REQ))
338                         qp->s_mig_state = IB_MIG_ARMED;
339         }
340
341         return 0;
342
343 err:
344         return 1;
345 }
346
347 /**
348  * qib_ruc_loopback - handle UC and RC lookback requests
349  * @sqp: the sending QP
350  *
351  * This is called from qib_do_send() to
352  * forward a WQE addressed to the same HCA.
353  * Note that although we are single threaded due to the tasklet, we still
354  * have to protect against post_send().  We don't have to worry about
355  * receive interrupts since this is a connected protocol and all packets
356  * will pass through here.
357  */
358 static void qib_ruc_loopback(struct rvt_qp *sqp)
359 {
360         struct qib_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
361         struct qib_pportdata *ppd = ppd_from_ibp(ibp);
362         struct qib_devdata *dd = ppd->dd;
363         struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
364         struct rvt_qp *qp;
365         struct rvt_swqe *wqe;
366         struct rvt_sge *sge;
367         unsigned long flags;
368         struct ib_wc wc;
369         u64 sdata;
370         atomic64_t *maddr;
371         enum ib_wc_status send_status;
372         int release;
373         int ret;
374
375         rcu_read_lock();
376         /*
377          * Note that we check the responder QP state after
378          * checking the requester's state.
379          */
380         qp = rvt_lookup_qpn(rdi, &ibp->rvp, sqp->remote_qpn);
381         if (!qp)
382                 goto done;
383
384         spin_lock_irqsave(&sqp->s_lock, flags);
385
386         /* Return if we are already busy processing a work request. */
387         if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
388             !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
389                 goto unlock;
390
391         sqp->s_flags |= RVT_S_BUSY;
392
393 again:
394         smp_read_barrier_depends(); /* see post_one_send() */
395         if (sqp->s_last == ACCESS_ONCE(sqp->s_head))
396                 goto clr_busy;
397         wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
398
399         /* Return if it is not OK to start a new work reqeust. */
400         if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
401                 if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
402                         goto clr_busy;
403                 /* We are in the error state, flush the work request. */
404                 send_status = IB_WC_WR_FLUSH_ERR;
405                 goto flush_send;
406         }
407
408         /*
409          * We can rely on the entry not changing without the s_lock
410          * being held until we update s_last.
411          * We increment s_cur to indicate s_last is in progress.
412          */
413         if (sqp->s_last == sqp->s_cur) {
414                 if (++sqp->s_cur >= sqp->s_size)
415                         sqp->s_cur = 0;
416         }
417         spin_unlock_irqrestore(&sqp->s_lock, flags);
418
419         if (!qp || !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
420             qp->ibqp.qp_type != sqp->ibqp.qp_type) {
421                 ibp->rvp.n_pkt_drops++;
422                 /*
423                  * For RC, the requester would timeout and retry so
424                  * shortcut the timeouts and just signal too many retries.
425                  */
426                 if (sqp->ibqp.qp_type == IB_QPT_RC)
427                         send_status = IB_WC_RETRY_EXC_ERR;
428                 else
429                         send_status = IB_WC_SUCCESS;
430                 goto serr;
431         }
432
433         memset(&wc, 0, sizeof(wc));
434         send_status = IB_WC_SUCCESS;
435
436         release = 1;
437         sqp->s_sge.sge = wqe->sg_list[0];
438         sqp->s_sge.sg_list = wqe->sg_list + 1;
439         sqp->s_sge.num_sge = wqe->wr.num_sge;
440         sqp->s_len = wqe->length;
441         switch (wqe->wr.opcode) {
442         case IB_WR_SEND_WITH_IMM:
443                 wc.wc_flags = IB_WC_WITH_IMM;
444                 wc.ex.imm_data = wqe->wr.ex.imm_data;
445                 /* FALLTHROUGH */
446         case IB_WR_SEND:
447                 ret = qib_get_rwqe(qp, 0);
448                 if (ret < 0)
449                         goto op_err;
450                 if (!ret)
451                         goto rnr_nak;
452                 if (wqe->length > qp->r_len)
453                         goto inv_err;
454                 break;
455
456         case IB_WR_RDMA_WRITE_WITH_IMM:
457                 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
458                         goto inv_err;
459                 wc.wc_flags = IB_WC_WITH_IMM;
460                 wc.ex.imm_data = wqe->wr.ex.imm_data;
461                 ret = qib_get_rwqe(qp, 1);
462                 if (ret < 0)
463                         goto op_err;
464                 if (!ret)
465                         goto rnr_nak;
466                 /* FALLTHROUGH */
467         case IB_WR_RDMA_WRITE:
468                 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
469                         goto inv_err;
470                 if (wqe->length == 0)
471                         break;
472                 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
473                                           wqe->rdma_wr.remote_addr,
474                                           wqe->rdma_wr.rkey,
475                                           IB_ACCESS_REMOTE_WRITE)))
476                         goto acc_err;
477                 qp->r_sge.sg_list = NULL;
478                 qp->r_sge.num_sge = 1;
479                 qp->r_sge.total_len = wqe->length;
480                 break;
481
482         case IB_WR_RDMA_READ:
483                 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
484                         goto inv_err;
485                 if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
486                                           wqe->rdma_wr.remote_addr,
487                                           wqe->rdma_wr.rkey,
488                                           IB_ACCESS_REMOTE_READ)))
489                         goto acc_err;
490                 release = 0;
491                 sqp->s_sge.sg_list = NULL;
492                 sqp->s_sge.num_sge = 1;
493                 qp->r_sge.sge = wqe->sg_list[0];
494                 qp->r_sge.sg_list = wqe->sg_list + 1;
495                 qp->r_sge.num_sge = wqe->wr.num_sge;
496                 qp->r_sge.total_len = wqe->length;
497                 break;
498
499         case IB_WR_ATOMIC_CMP_AND_SWP:
500         case IB_WR_ATOMIC_FETCH_AND_ADD:
501                 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
502                         goto inv_err;
503                 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
504                                           wqe->atomic_wr.remote_addr,
505                                           wqe->atomic_wr.rkey,
506                                           IB_ACCESS_REMOTE_ATOMIC)))
507                         goto acc_err;
508                 /* Perform atomic OP and save result. */
509                 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
510                 sdata = wqe->atomic_wr.compare_add;
511                 *(u64 *) sqp->s_sge.sge.vaddr =
512                         (wqe->atomic_wr.wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
513                         (u64) atomic64_add_return(sdata, maddr) - sdata :
514                         (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
515                                       sdata, wqe->atomic_wr.swap);
516                 rvt_put_mr(qp->r_sge.sge.mr);
517                 qp->r_sge.num_sge = 0;
518                 goto send_comp;
519
520         default:
521                 send_status = IB_WC_LOC_QP_OP_ERR;
522                 goto serr;
523         }
524
525         sge = &sqp->s_sge.sge;
526         while (sqp->s_len) {
527                 u32 len = sqp->s_len;
528
529                 if (len > sge->length)
530                         len = sge->length;
531                 if (len > sge->sge_length)
532                         len = sge->sge_length;
533                 BUG_ON(len == 0);
534                 qib_copy_sge(&qp->r_sge, sge->vaddr, len, release);
535                 sge->vaddr += len;
536                 sge->length -= len;
537                 sge->sge_length -= len;
538                 if (sge->sge_length == 0) {
539                         if (!release)
540                                 rvt_put_mr(sge->mr);
541                         if (--sqp->s_sge.num_sge)
542                                 *sge = *sqp->s_sge.sg_list++;
543                 } else if (sge->length == 0 && sge->mr->lkey) {
544                         if (++sge->n >= RVT_SEGSZ) {
545                                 if (++sge->m >= sge->mr->mapsz)
546                                         break;
547                                 sge->n = 0;
548                         }
549                         sge->vaddr =
550                                 sge->mr->map[sge->m]->segs[sge->n].vaddr;
551                         sge->length =
552                                 sge->mr->map[sge->m]->segs[sge->n].length;
553                 }
554                 sqp->s_len -= len;
555         }
556         if (release)
557                 rvt_put_ss(&qp->r_sge);
558
559         if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
560                 goto send_comp;
561
562         if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
563                 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
564         else
565                 wc.opcode = IB_WC_RECV;
566         wc.wr_id = qp->r_wr_id;
567         wc.status = IB_WC_SUCCESS;
568         wc.byte_len = wqe->length;
569         wc.qp = &qp->ibqp;
570         wc.src_qp = qp->remote_qpn;
571         wc.slid = qp->remote_ah_attr.dlid;
572         wc.sl = qp->remote_ah_attr.sl;
573         wc.port_num = 1;
574         /* Signal completion event if the solicited bit is set. */
575         rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
576                      wqe->wr.send_flags & IB_SEND_SOLICITED);
577
578 send_comp:
579         spin_lock_irqsave(&sqp->s_lock, flags);
580         ibp->rvp.n_loop_pkts++;
581 flush_send:
582         sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
583         qib_send_complete(sqp, wqe, send_status);
584         goto again;
585
586 rnr_nak:
587         /* Handle RNR NAK */
588         if (qp->ibqp.qp_type == IB_QPT_UC)
589                 goto send_comp;
590         ibp->rvp.n_rnr_naks++;
591         /*
592          * Note: we don't need the s_lock held since the BUSY flag
593          * makes this single threaded.
594          */
595         if (sqp->s_rnr_retry == 0) {
596                 send_status = IB_WC_RNR_RETRY_EXC_ERR;
597                 goto serr;
598         }
599         if (sqp->s_rnr_retry_cnt < 7)
600                 sqp->s_rnr_retry--;
601         spin_lock_irqsave(&sqp->s_lock, flags);
602         if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
603                 goto clr_busy;
604         sqp->s_flags |= RVT_S_WAIT_RNR;
605         sqp->s_timer.function = qib_rc_rnr_retry;
606         sqp->s_timer.expires = jiffies +
607                 usecs_to_jiffies(ib_qib_rnr_table[qp->r_min_rnr_timer]);
608         add_timer(&sqp->s_timer);
609         goto clr_busy;
610
611 op_err:
612         send_status = IB_WC_REM_OP_ERR;
613         wc.status = IB_WC_LOC_QP_OP_ERR;
614         goto err;
615
616 inv_err:
617         send_status =
618                 sqp->ibqp.qp_type == IB_QPT_RC ?
619                         IB_WC_REM_INV_REQ_ERR :
620                         IB_WC_SUCCESS;
621         wc.status = IB_WC_LOC_QP_OP_ERR;
622         goto err;
623
624 acc_err:
625         send_status = IB_WC_REM_ACCESS_ERR;
626         wc.status = IB_WC_LOC_PROT_ERR;
627 err:
628         /* responder goes to error state */
629         qib_rc_error(qp, wc.status);
630
631 serr:
632         spin_lock_irqsave(&sqp->s_lock, flags);
633         qib_send_complete(sqp, wqe, send_status);
634         if (sqp->ibqp.qp_type == IB_QPT_RC) {
635                 int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
636
637                 sqp->s_flags &= ~RVT_S_BUSY;
638                 spin_unlock_irqrestore(&sqp->s_lock, flags);
639                 if (lastwqe) {
640                         struct ib_event ev;
641
642                         ev.device = sqp->ibqp.device;
643                         ev.element.qp = &sqp->ibqp;
644                         ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
645                         sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
646                 }
647                 goto done;
648         }
649 clr_busy:
650         sqp->s_flags &= ~RVT_S_BUSY;
651 unlock:
652         spin_unlock_irqrestore(&sqp->s_lock, flags);
653 done:
654         rcu_read_unlock();
655 }
656
657 /**
658  * qib_make_grh - construct a GRH header
659  * @ibp: a pointer to the IB port
660  * @hdr: a pointer to the GRH header being constructed
661  * @grh: the global route address to send to
662  * @hwords: the number of 32 bit words of header being sent
663  * @nwords: the number of 32 bit words of data being sent
664  *
665  * Return the size of the header in 32 bit words.
666  */
667 u32 qib_make_grh(struct qib_ibport *ibp, struct ib_grh *hdr,
668                  struct ib_global_route *grh, u32 hwords, u32 nwords)
669 {
670         hdr->version_tclass_flow =
671                 cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
672                             (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
673                             (grh->flow_label << IB_GRH_FLOW_SHIFT));
674         hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
675         /* next_hdr is defined by C8-7 in ch. 8.4.1 */
676         hdr->next_hdr = IB_GRH_NEXT_HDR;
677         hdr->hop_limit = grh->hop_limit;
678         /* The SGID is 32-bit aligned. */
679         hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
680         hdr->sgid.global.interface_id = grh->sgid_index ?
681                 ibp->guids[grh->sgid_index - 1] : ppd_from_ibp(ibp)->guid;
682         hdr->dgid = grh->dgid;
683
684         /* GRH header size in 32-bit words. */
685         return sizeof(struct ib_grh) / sizeof(u32);
686 }
687
688 void qib_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
689                          u32 bth0, u32 bth2)
690 {
691         struct qib_qp_priv *priv = qp->priv;
692         struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
693         u16 lrh0;
694         u32 nwords;
695         u32 extra_bytes;
696
697         /* Construct the header. */
698         extra_bytes = -qp->s_cur_size & 3;
699         nwords = (qp->s_cur_size + extra_bytes) >> 2;
700         lrh0 = QIB_LRH_BTH;
701         if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
702                 qp->s_hdrwords += qib_make_grh(ibp, &priv->s_hdr->u.l.grh,
703                                                &qp->remote_ah_attr.grh,
704                                                qp->s_hdrwords, nwords);
705                 lrh0 = QIB_LRH_GRH;
706         }
707         lrh0 |= ibp->sl_to_vl[qp->remote_ah_attr.sl] << 12 |
708                 qp->remote_ah_attr.sl << 4;
709         priv->s_hdr->lrh[0] = cpu_to_be16(lrh0);
710         priv->s_hdr->lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
711         priv->s_hdr->lrh[2] =
712                         cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
713         priv->s_hdr->lrh[3] = cpu_to_be16(ppd_from_ibp(ibp)->lid |
714                                        qp->remote_ah_attr.src_path_bits);
715         bth0 |= qib_get_pkey(ibp, qp->s_pkey_index);
716         bth0 |= extra_bytes << 20;
717         if (qp->s_mig_state == IB_MIG_MIGRATED)
718                 bth0 |= IB_BTH_MIG_REQ;
719         ohdr->bth[0] = cpu_to_be32(bth0);
720         ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
721         ohdr->bth[2] = cpu_to_be32(bth2);
722         this_cpu_inc(ibp->pmastats->n_unicast_xmit);
723 }
724
725 void _qib_do_send(struct work_struct *work)
726 {
727         struct qib_qp_priv *priv = container_of(work, struct qib_qp_priv,
728                                                 s_work);
729         struct rvt_qp *qp = priv->owner;
730
731         qib_do_send(qp);
732 }
733
734 /**
735  * qib_do_send - perform a send on a QP
736  * @qp: pointer to the QP
737  *
738  * Process entries in the send work queue until credit or queue is
739  * exhausted.  Only allow one CPU to send a packet per QP (tasklet).
740  * Otherwise, two threads could send packets out of order.
741  */
742 void qib_do_send(struct rvt_qp *qp)
743 {
744         struct qib_qp_priv *priv = qp->priv;
745         struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
746         struct qib_pportdata *ppd = ppd_from_ibp(ibp);
747         int (*make_req)(struct rvt_qp *qp, unsigned long *flags);
748         unsigned long flags;
749
750         if ((qp->ibqp.qp_type == IB_QPT_RC ||
751              qp->ibqp.qp_type == IB_QPT_UC) &&
752             (qp->remote_ah_attr.dlid & ~((1 << ppd->lmc) - 1)) == ppd->lid) {
753                 qib_ruc_loopback(qp);
754                 return;
755         }
756
757         if (qp->ibqp.qp_type == IB_QPT_RC)
758                 make_req = qib_make_rc_req;
759         else if (qp->ibqp.qp_type == IB_QPT_UC)
760                 make_req = qib_make_uc_req;
761         else
762                 make_req = qib_make_ud_req;
763
764         spin_lock_irqsave(&qp->s_lock, flags);
765
766         /* Return if we are already busy processing a work request. */
767         if (!qib_send_ok(qp)) {
768                 spin_unlock_irqrestore(&qp->s_lock, flags);
769                 return;
770         }
771
772         qp->s_flags |= RVT_S_BUSY;
773
774         do {
775                 /* Check for a constructed packet to be sent. */
776                 if (qp->s_hdrwords != 0) {
777                         spin_unlock_irqrestore(&qp->s_lock, flags);
778                         /*
779                          * If the packet cannot be sent now, return and
780                          * the send tasklet will be woken up later.
781                          */
782                         if (qib_verbs_send(qp, priv->s_hdr, qp->s_hdrwords,
783                                            qp->s_cur_sge, qp->s_cur_size))
784                                 return;
785                         /* Record that s_hdr is empty. */
786                         qp->s_hdrwords = 0;
787                         spin_lock_irqsave(&qp->s_lock, flags);
788                 }
789         } while (make_req(qp, &flags));
790
791         spin_unlock_irqrestore(&qp->s_lock, flags);
792 }
793
794 /*
795  * This should be called with s_lock held.
796  */
797 void qib_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
798                        enum ib_wc_status status)
799 {
800         u32 old_last, last;
801         unsigned i;
802
803         if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
804                 return;
805
806         last = qp->s_last;
807         old_last = last;
808         if (++last >= qp->s_size)
809                 last = 0;
810         qp->s_last = last;
811         /* See post_send() */
812         barrier();
813         for (i = 0; i < wqe->wr.num_sge; i++) {
814                 struct rvt_sge *sge = &wqe->sg_list[i];
815
816                 rvt_put_mr(sge->mr);
817         }
818         if (qp->ibqp.qp_type == IB_QPT_UD ||
819             qp->ibqp.qp_type == IB_QPT_SMI ||
820             qp->ibqp.qp_type == IB_QPT_GSI)
821                 atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
822
823         /* See ch. 11.2.4.1 and 10.7.3.1 */
824         if (!(qp->s_flags & RVT_S_SIGNAL_REQ_WR) ||
825             (wqe->wr.send_flags & IB_SEND_SIGNALED) ||
826             status != IB_WC_SUCCESS) {
827                 struct ib_wc wc;
828
829                 memset(&wc, 0, sizeof(wc));
830                 wc.wr_id = wqe->wr.wr_id;
831                 wc.status = status;
832                 wc.opcode = ib_qib_wc_opcode[wqe->wr.opcode];
833                 wc.qp = &qp->ibqp;
834                 if (status == IB_WC_SUCCESS)
835                         wc.byte_len = wqe->length;
836                 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.send_cq), &wc,
837                              status != IB_WC_SUCCESS);
838         }
839
840         if (qp->s_acked == old_last)
841                 qp->s_acked = last;
842         if (qp->s_cur == old_last)
843                 qp->s_cur = last;
844         if (qp->s_tail == old_last)
845                 qp->s_tail = last;
846         if (qp->state == IB_QPS_SQD && last == qp->s_cur)
847                 qp->s_draining = 0;
848 }