98a4798a0ead7676fd6aefe8d2717ad3c36bb1c4
[cascardo/linux.git] / drivers / staging / rdma / hfi1 / ruc.c
1 /*
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2015 Intel Corporation.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * BSD LICENSE
20  *
21  * Copyright(c) 2015 Intel Corporation.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  *
27  *  - Redistributions of source code must retain the above copyright
28  *    notice, this list of conditions and the following disclaimer.
29  *  - Redistributions in binary form must reproduce the above copyright
30  *    notice, this list of conditions and the following disclaimer in
31  *    the documentation and/or other materials provided with the
32  *    distribution.
33  *  - Neither the name of Intel Corporation nor the names of its
34  *    contributors may be used to endorse or promote products derived
35  *    from this software without specific prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  *
49  */
50
51 #include <linux/spinlock.h>
52
53 #include "hfi.h"
54 #include "mad.h"
55 #include "qp.h"
56 #include "sdma.h"
57
58 /*
59  * Convert the AETH RNR timeout code into the number of microseconds.
60  */
61 const u32 ib_hfi1_rnr_table[32] = {
62         655360, /* 00: 655.36 */
63         10,     /* 01:    .01 */
64         20,     /* 02     .02 */
65         30,     /* 03:    .03 */
66         40,     /* 04:    .04 */
67         60,     /* 05:    .06 */
68         80,     /* 06:    .08 */
69         120,    /* 07:    .12 */
70         160,    /* 08:    .16 */
71         240,    /* 09:    .24 */
72         320,    /* 0A:    .32 */
73         480,    /* 0B:    .48 */
74         640,    /* 0C:    .64 */
75         960,    /* 0D:    .96 */
76         1280,   /* 0E:   1.28 */
77         1920,   /* 0F:   1.92 */
78         2560,   /* 10:   2.56 */
79         3840,   /* 11:   3.84 */
80         5120,   /* 12:   5.12 */
81         7680,   /* 13:   7.68 */
82         10240,  /* 14:  10.24 */
83         15360,  /* 15:  15.36 */
84         20480,  /* 16:  20.48 */
85         30720,  /* 17:  30.72 */
86         40960,  /* 18:  40.96 */
87         61440,  /* 19:  61.44 */
88         81920,  /* 1A:  81.92 */
89         122880, /* 1B: 122.88 */
90         163840, /* 1C: 163.84 */
91         245760, /* 1D: 245.76 */
92         327680, /* 1E: 327.68 */
93         491520  /* 1F: 491.52 */
94 };
95
96 /*
97  * Validate a RWQE and fill in the SGE state.
98  * Return 1 if OK.
99  */
100 static int init_sge(struct rvt_qp *qp, struct rvt_rwqe *wqe)
101 {
102         int i, j, ret;
103         struct ib_wc wc;
104         struct rvt_lkey_table *rkt;
105         struct rvt_pd *pd;
106         struct rvt_sge_state *ss;
107
108         rkt = &to_idev(qp->ibqp.device)->rdi.lkey_table;
109         pd = ibpd_to_rvtpd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd);
110         ss = &qp->r_sge;
111         ss->sg_list = qp->r_sg_list;
112         qp->r_len = 0;
113         for (i = j = 0; i < wqe->num_sge; i++) {
114                 if (wqe->sg_list[i].length == 0)
115                         continue;
116                 /* Check LKEY */
117                 if (!rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,
118                                  &wqe->sg_list[i], IB_ACCESS_LOCAL_WRITE))
119                         goto bad_lkey;
120                 qp->r_len += wqe->sg_list[i].length;
121                 j++;
122         }
123         ss->num_sge = j;
124         ss->total_len = qp->r_len;
125         ret = 1;
126         goto bail;
127
128 bad_lkey:
129         while (j) {
130                 struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;
131
132                 rvt_put_mr(sge->mr);
133         }
134         ss->num_sge = 0;
135         memset(&wc, 0, sizeof(wc));
136         wc.wr_id = wqe->wr_id;
137         wc.status = IB_WC_LOC_PROT_ERR;
138         wc.opcode = IB_WC_RECV;
139         wc.qp = &qp->ibqp;
140         /* Signal solicited completion event. */
141         hfi1_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
142         ret = 0;
143 bail:
144         return ret;
145 }
146
147 /**
148  * hfi1_get_rwqe - copy the next RWQE into the QP's RWQE
149  * @qp: the QP
150  * @wr_id_only: update qp->r_wr_id only, not qp->r_sge
151  *
152  * Return -1 if there is a local error, 0 if no RWQE is available,
153  * otherwise return 1.
154  *
155  * Can be called from interrupt level.
156  */
157 int hfi1_get_rwqe(struct rvt_qp *qp, int wr_id_only)
158 {
159         unsigned long flags;
160         struct rvt_rq *rq;
161         struct rvt_rwq *wq;
162         struct rvt_srq *srq;
163         struct rvt_rwqe *wqe;
164         void (*handler)(struct ib_event *, void *);
165         u32 tail;
166         int ret;
167
168         if (qp->ibqp.srq) {
169                 srq = ibsrq_to_rvtsrq(qp->ibqp.srq);
170                 handler = srq->ibsrq.event_handler;
171                 rq = &srq->rq;
172         } else {
173                 srq = NULL;
174                 handler = NULL;
175                 rq = &qp->r_rq;
176         }
177
178         spin_lock_irqsave(&rq->lock, flags);
179         if (!(ib_hfi1_state_ops[qp->state] & HFI1_PROCESS_RECV_OK)) {
180                 ret = 0;
181                 goto unlock;
182         }
183
184         wq = rq->wq;
185         tail = wq->tail;
186         /* Validate tail before using it since it is user writable. */
187         if (tail >= rq->size)
188                 tail = 0;
189         if (unlikely(tail == wq->head)) {
190                 ret = 0;
191                 goto unlock;
192         }
193         /* Make sure entry is read after head index is read. */
194         smp_rmb();
195         wqe = get_rwqe_ptr(rq, tail);
196         /*
197          * Even though we update the tail index in memory, the verbs
198          * consumer is not supposed to post more entries until a
199          * completion is generated.
200          */
201         if (++tail >= rq->size)
202                 tail = 0;
203         wq->tail = tail;
204         if (!wr_id_only && !init_sge(qp, wqe)) {
205                 ret = -1;
206                 goto unlock;
207         }
208         qp->r_wr_id = wqe->wr_id;
209
210         ret = 1;
211         set_bit(HFI1_R_WRID_VALID, &qp->r_aflags);
212         if (handler) {
213                 u32 n;
214
215                 /*
216                  * Validate head pointer value and compute
217                  * the number of remaining WQEs.
218                  */
219                 n = wq->head;
220                 if (n >= rq->size)
221                         n = 0;
222                 if (n < tail)
223                         n += rq->size - tail;
224                 else
225                         n -= tail;
226                 if (n < srq->limit) {
227                         struct ib_event ev;
228
229                         srq->limit = 0;
230                         spin_unlock_irqrestore(&rq->lock, flags);
231                         ev.device = qp->ibqp.device;
232                         ev.element.srq = qp->ibqp.srq;
233                         ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
234                         handler(&ev, srq->ibsrq.srq_context);
235                         goto bail;
236                 }
237         }
238 unlock:
239         spin_unlock_irqrestore(&rq->lock, flags);
240 bail:
241         return ret;
242 }
243
244 static __be64 get_sguid(struct hfi1_ibport *ibp, unsigned index)
245 {
246         if (!index) {
247                 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
248
249                 return cpu_to_be64(ppd->guid);
250         }
251         return ibp->guids[index - 1];
252 }
253
254 static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)
255 {
256         return (gid->global.interface_id == id &&
257                 (gid->global.subnet_prefix == gid_prefix ||
258                  gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));
259 }
260
261 /*
262  *
263  * This should be called with the QP r_lock held.
264  *
265  * The s_lock will be acquired around the hfi1_migrate_qp() call.
266  */
267 int hfi1_ruc_check_hdr(struct hfi1_ibport *ibp, struct hfi1_ib_header *hdr,
268                        int has_grh, struct rvt_qp *qp, u32 bth0)
269 {
270         __be64 guid;
271         unsigned long flags;
272         u8 sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl];
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, ibp->rvp.gid_prefix,
283                                     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 (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), (u16)bth0,
291                                             sc5, be16_to_cpu(hdr->lrh[3])))) {
292                         hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY,
293                                        (u16)bth0,
294                                        (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
295                                        0, qp->ibqp.qp_num,
296                                        be16_to_cpu(hdr->lrh[3]),
297                                        be16_to_cpu(hdr->lrh[1]));
298                         goto err;
299                 }
300                 /* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
301                 if (be16_to_cpu(hdr->lrh[3]) != qp->alt_ah_attr.dlid ||
302                     ppd_from_ibp(ibp)->port != qp->alt_ah_attr.port_num)
303                         goto err;
304                 spin_lock_irqsave(&qp->s_lock, flags);
305                 hfi1_migrate_qp(qp);
306                 spin_unlock_irqrestore(&qp->s_lock, flags);
307         } else {
308                 if (!has_grh) {
309                         if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
310                                 goto err;
311                 } else {
312                         if (!(qp->remote_ah_attr.ah_flags & IB_AH_GRH))
313                                 goto err;
314                         guid = get_sguid(ibp,
315                                          qp->remote_ah_attr.grh.sgid_index);
316                         if (!gid_ok(&hdr->u.l.grh.dgid, ibp->rvp.gid_prefix,
317                                     guid))
318                                 goto err;
319                         if (!gid_ok(&hdr->u.l.grh.sgid,
320                             qp->remote_ah_attr.grh.dgid.global.subnet_prefix,
321                             qp->remote_ah_attr.grh.dgid.global.interface_id))
322                                 goto err;
323                 }
324                 if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), (u16)bth0,
325                                             sc5, be16_to_cpu(hdr->lrh[3])))) {
326                         hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY,
327                                        (u16)bth0,
328                                        (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
329                                        0, qp->ibqp.qp_num,
330                                        be16_to_cpu(hdr->lrh[3]),
331                                        be16_to_cpu(hdr->lrh[1]));
332                         goto err;
333                 }
334                 /* Validate the SLID. See Ch. 9.6.1.5 */
335                 if (be16_to_cpu(hdr->lrh[3]) != qp->remote_ah_attr.dlid ||
336                     ppd_from_ibp(ibp)->port != qp->port_num)
337                         goto err;
338                 if (qp->s_mig_state == IB_MIG_REARM &&
339                     !(bth0 & IB_BTH_MIG_REQ))
340                         qp->s_mig_state = IB_MIG_ARMED;
341         }
342
343         return 0;
344
345 err:
346         return 1;
347 }
348
349 /**
350  * ruc_loopback - handle UC and RC loopback requests
351  * @sqp: the sending QP
352  *
353  * This is called from hfi1_do_send() to
354  * forward a WQE addressed to the same HFI.
355  * Note that although we are single threaded due to the tasklet, we still
356  * have to protect against post_send().  We don't have to worry about
357  * receive interrupts since this is a connected protocol and all packets
358  * will pass through here.
359  */
360 static void ruc_loopback(struct rvt_qp *sqp)
361 {
362         struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
363         struct rvt_qp *qp;
364         struct rvt_swqe *wqe;
365         struct rvt_sge *sge;
366         unsigned long flags;
367         struct ib_wc wc;
368         u64 sdata;
369         atomic64_t *maddr;
370         enum ib_wc_status send_status;
371         int release;
372         int ret;
373
374         rcu_read_lock();
375
376         /*
377          * Note that we check the responder QP state after
378          * checking the requester's state.
379          */
380         qp = hfi1_lookup_qpn(ibp, sqp->remote_qpn);
381
382         spin_lock_irqsave(&sqp->s_lock, flags);
383
384         /* Return if we are already busy processing a work request. */
385         if ((sqp->s_flags & (HFI1_S_BUSY | HFI1_S_ANY_WAIT)) ||
386             !(ib_hfi1_state_ops[sqp->state] & HFI1_PROCESS_OR_FLUSH_SEND))
387                 goto unlock;
388
389         sqp->s_flags |= HFI1_S_BUSY;
390
391 again:
392         if (sqp->s_last == sqp->s_head)
393                 goto clr_busy;
394         wqe = get_swqe_ptr(sqp, sqp->s_last);
395
396         /* Return if it is not OK to start a new work request. */
397         if (!(ib_hfi1_state_ops[sqp->state] & HFI1_PROCESS_NEXT_SEND_OK)) {
398                 if (!(ib_hfi1_state_ops[sqp->state] & HFI1_FLUSH_SEND))
399                         goto clr_busy;
400                 /* We are in the error state, flush the work request. */
401                 send_status = IB_WC_WR_FLUSH_ERR;
402                 goto flush_send;
403         }
404
405         /*
406          * We can rely on the entry not changing without the s_lock
407          * being held until we update s_last.
408          * We increment s_cur to indicate s_last is in progress.
409          */
410         if (sqp->s_last == sqp->s_cur) {
411                 if (++sqp->s_cur >= sqp->s_size)
412                         sqp->s_cur = 0;
413         }
414         spin_unlock_irqrestore(&sqp->s_lock, flags);
415
416         if (!qp || !(ib_hfi1_state_ops[qp->state] & HFI1_PROCESS_RECV_OK) ||
417             qp->ibqp.qp_type != sqp->ibqp.qp_type) {
418                 ibp->rvp.n_pkt_drops++;
419                 /*
420                  * For RC, the requester would timeout and retry so
421                  * shortcut the timeouts and just signal too many retries.
422                  */
423                 if (sqp->ibqp.qp_type == IB_QPT_RC)
424                         send_status = IB_WC_RETRY_EXC_ERR;
425                 else
426                         send_status = IB_WC_SUCCESS;
427                 goto serr;
428         }
429
430         memset(&wc, 0, sizeof(wc));
431         send_status = IB_WC_SUCCESS;
432
433         release = 1;
434         sqp->s_sge.sge = wqe->sg_list[0];
435         sqp->s_sge.sg_list = wqe->sg_list + 1;
436         sqp->s_sge.num_sge = wqe->wr.num_sge;
437         sqp->s_len = wqe->length;
438         switch (wqe->wr.opcode) {
439         case IB_WR_SEND_WITH_IMM:
440                 wc.wc_flags = IB_WC_WITH_IMM;
441                 wc.ex.imm_data = wqe->wr.ex.imm_data;
442                 /* FALLTHROUGH */
443         case IB_WR_SEND:
444                 ret = hfi1_get_rwqe(qp, 0);
445                 if (ret < 0)
446                         goto op_err;
447                 if (!ret)
448                         goto rnr_nak;
449                 break;
450
451         case IB_WR_RDMA_WRITE_WITH_IMM:
452                 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
453                         goto inv_err;
454                 wc.wc_flags = IB_WC_WITH_IMM;
455                 wc.ex.imm_data = wqe->wr.ex.imm_data;
456                 ret = hfi1_get_rwqe(qp, 1);
457                 if (ret < 0)
458                         goto op_err;
459                 if (!ret)
460                         goto rnr_nak;
461                 /* FALLTHROUGH */
462         case IB_WR_RDMA_WRITE:
463                 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
464                         goto inv_err;
465                 if (wqe->length == 0)
466                 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
467                                           wqe->rdma_wr.remote_addr,
468                                           wqe->rdma_wr.rkey,
469                                           IB_ACCESS_REMOTE_WRITE)))
470                         goto acc_err;
471                 qp->r_sge.sg_list = NULL;
472                 qp->r_sge.num_sge = 1;
473                 qp->r_sge.total_len = wqe->length;
474                 break;
475
476         case IB_WR_RDMA_READ:
477                 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
478                         goto inv_err;
479                 if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
480                                           wqe->rdma_wr.remote_addr,
481                                           wqe->rdma_wr.rkey,
482                                           IB_ACCESS_REMOTE_READ)))
483                         goto acc_err;
484                 release = 0;
485                 sqp->s_sge.sg_list = NULL;
486                 sqp->s_sge.num_sge = 1;
487                 qp->r_sge.sge = wqe->sg_list[0];
488                 qp->r_sge.sg_list = wqe->sg_list + 1;
489                 qp->r_sge.num_sge = wqe->wr.num_sge;
490                 qp->r_sge.total_len = wqe->length;
491                 break;
492
493         case IB_WR_ATOMIC_CMP_AND_SWP:
494         case IB_WR_ATOMIC_FETCH_AND_ADD:
495                 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
496                         goto inv_err;
497                 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
498                                           wqe->atomic_wr.remote_addr,
499                                           wqe->atomic_wr.rkey,
500                                           IB_ACCESS_REMOTE_ATOMIC)))
501                         goto acc_err;
502                 /* Perform atomic OP and save result. */
503                 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
504                 sdata = wqe->atomic_wr.compare_add;
505                 *(u64 *) sqp->s_sge.sge.vaddr =
506                         (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
507                         (u64) atomic64_add_return(sdata, maddr) - sdata :
508                         (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
509                                       sdata, wqe->atomic_wr.swap);
510                 rvt_put_mr(qp->r_sge.sge.mr);
511                 qp->r_sge.num_sge = 0;
512                 goto send_comp;
513
514         default:
515                 send_status = IB_WC_LOC_QP_OP_ERR;
516                 goto serr;
517         }
518
519         sge = &sqp->s_sge.sge;
520         while (sqp->s_len) {
521                 u32 len = sqp->s_len;
522
523                 if (len > sge->length)
524                         len = sge->length;
525                 if (len > sge->sge_length)
526                         len = sge->sge_length;
527                 WARN_ON_ONCE(len == 0);
528                 hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, release);
529                 sge->vaddr += len;
530                 sge->length -= len;
531                 sge->sge_length -= len;
532                 if (sge->sge_length == 0) {
533                         if (!release)
534                                 rvt_put_mr(sge->mr);
535                         if (--sqp->s_sge.num_sge)
536                                 *sge = *sqp->s_sge.sg_list++;
537                 } else if (sge->length == 0 && sge->mr->lkey) {
538                         if (++sge->n >= RVT_SEGSZ) {
539                                 if (++sge->m >= sge->mr->mapsz)
540                                         break;
541                                 sge->n = 0;
542                         }
543                         sge->vaddr =
544                                 sge->mr->map[sge->m]->segs[sge->n].vaddr;
545                         sge->length =
546                                 sge->mr->map[sge->m]->segs[sge->n].length;
547                 }
548                 sqp->s_len -= len;
549         }
550         if (release)
551                 hfi1_put_ss(&qp->r_sge);
552
553         if (!test_and_clear_bit(HFI1_R_WRID_VALID, &qp->r_aflags))
554                 goto send_comp;
555
556         if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
557                 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
558         else
559                 wc.opcode = IB_WC_RECV;
560         wc.wr_id = qp->r_wr_id;
561         wc.status = IB_WC_SUCCESS;
562         wc.byte_len = wqe->length;
563         wc.qp = &qp->ibqp;
564         wc.src_qp = qp->remote_qpn;
565         wc.slid = qp->remote_ah_attr.dlid;
566         wc.sl = qp->remote_ah_attr.sl;
567         wc.port_num = 1;
568         /* Signal completion event if the solicited bit is set. */
569         hfi1_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
570                       wqe->wr.send_flags & IB_SEND_SOLICITED);
571
572 send_comp:
573         spin_lock_irqsave(&sqp->s_lock, flags);
574         ibp->rvp.n_loop_pkts++;
575 flush_send:
576         sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
577         hfi1_send_complete(sqp, wqe, send_status);
578         goto again;
579
580 rnr_nak:
581         /* Handle RNR NAK */
582         if (qp->ibqp.qp_type == IB_QPT_UC)
583                 goto send_comp;
584         ibp->rvp.n_rnr_naks++;
585         /*
586          * Note: we don't need the s_lock held since the BUSY flag
587          * makes this single threaded.
588          */
589         if (sqp->s_rnr_retry == 0) {
590                 send_status = IB_WC_RNR_RETRY_EXC_ERR;
591                 goto serr;
592         }
593         if (sqp->s_rnr_retry_cnt < 7)
594                 sqp->s_rnr_retry--;
595         spin_lock_irqsave(&sqp->s_lock, flags);
596         if (!(ib_hfi1_state_ops[sqp->state] & HFI1_PROCESS_RECV_OK))
597                 goto clr_busy;
598         sqp->s_flags |= HFI1_S_WAIT_RNR;
599         sqp->s_timer.function = hfi1_rc_rnr_retry;
600         sqp->s_timer.expires = jiffies +
601                 usecs_to_jiffies(ib_hfi1_rnr_table[qp->r_min_rnr_timer]);
602         add_timer(&sqp->s_timer);
603         goto clr_busy;
604
605 op_err:
606         send_status = IB_WC_REM_OP_ERR;
607         wc.status = IB_WC_LOC_QP_OP_ERR;
608         goto err;
609
610 inv_err:
611         send_status = IB_WC_REM_INV_REQ_ERR;
612         wc.status = IB_WC_LOC_QP_OP_ERR;
613         goto err;
614
615 acc_err:
616         send_status = IB_WC_REM_ACCESS_ERR;
617         wc.status = IB_WC_LOC_PROT_ERR;
618 err:
619         /* responder goes to error state */
620         hfi1_rc_error(qp, wc.status);
621
622 serr:
623         spin_lock_irqsave(&sqp->s_lock, flags);
624         hfi1_send_complete(sqp, wqe, send_status);
625         if (sqp->ibqp.qp_type == IB_QPT_RC) {
626                 int lastwqe = hfi1_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
627
628                 sqp->s_flags &= ~HFI1_S_BUSY;
629                 spin_unlock_irqrestore(&sqp->s_lock, flags);
630                 if (lastwqe) {
631                         struct ib_event ev;
632
633                         ev.device = sqp->ibqp.device;
634                         ev.element.qp = &sqp->ibqp;
635                         ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
636                         sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
637                 }
638                 goto done;
639         }
640 clr_busy:
641         sqp->s_flags &= ~HFI1_S_BUSY;
642 unlock:
643         spin_unlock_irqrestore(&sqp->s_lock, flags);
644 done:
645         rcu_read_unlock();
646 }
647
648 /**
649  * hfi1_make_grh - construct a GRH header
650  * @ibp: a pointer to the IB port
651  * @hdr: a pointer to the GRH header being constructed
652  * @grh: the global route address to send to
653  * @hwords: the number of 32 bit words of header being sent
654  * @nwords: the number of 32 bit words of data being sent
655  *
656  * Return the size of the header in 32 bit words.
657  */
658 u32 hfi1_make_grh(struct hfi1_ibport *ibp, struct ib_grh *hdr,
659                   struct ib_global_route *grh, u32 hwords, u32 nwords)
660 {
661         hdr->version_tclass_flow =
662                 cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
663                             (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
664                             (grh->flow_label << IB_GRH_FLOW_SHIFT));
665         hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
666         /* next_hdr is defined by C8-7 in ch. 8.4.1 */
667         hdr->next_hdr = IB_GRH_NEXT_HDR;
668         hdr->hop_limit = grh->hop_limit;
669         /* The SGID is 32-bit aligned. */
670         hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
671         hdr->sgid.global.interface_id =
672                 grh->sgid_index && grh->sgid_index < ARRAY_SIZE(ibp->guids) ?
673                 ibp->guids[grh->sgid_index - 1] :
674                         cpu_to_be64(ppd_from_ibp(ibp)->guid);
675         hdr->dgid = grh->dgid;
676
677         /* GRH header size in 32-bit words. */
678         return sizeof(struct ib_grh) / sizeof(u32);
679 }
680
681 #define BTH2_OFFSET (offsetof(struct hfi1_pio_header, hdr.u.oth.bth[2]) / 4)
682
683 /**
684  * build_ahg - create ahg in s_hdr
685  * @qp: a pointer to QP
686  * @npsn: the next PSN for the request/response
687  *
688  * This routine handles the AHG by allocating an ahg entry and causing the
689  * copy of the first middle.
690  *
691  * Subsequent middles use the copied entry, editing the
692  * PSN with 1 or 2 edits.
693  */
694 static inline void build_ahg(struct rvt_qp *qp, u32 npsn)
695 {
696         struct hfi1_qp_priv *priv = qp->priv;
697         if (unlikely(qp->s_flags & HFI1_S_AHG_CLEAR))
698                 clear_ahg(qp);
699         if (!(qp->s_flags & HFI1_S_AHG_VALID)) {
700                 /* first middle that needs copy  */
701                 if (qp->s_ahgidx < 0)
702                         qp->s_ahgidx = sdma_ahg_alloc(priv->s_sde);
703                 if (qp->s_ahgidx >= 0) {
704                         qp->s_ahgpsn = npsn;
705                         priv->s_hdr->tx_flags |= SDMA_TXREQ_F_AHG_COPY;
706                         /* save to protect a change in another thread */
707                         priv->s_hdr->sde = priv->s_sde;
708                         priv->s_hdr->ahgidx = qp->s_ahgidx;
709                         qp->s_flags |= HFI1_S_AHG_VALID;
710                 }
711         } else {
712                 /* subsequent middle after valid */
713                 if (qp->s_ahgidx >= 0) {
714                         priv->s_hdr->tx_flags |= SDMA_TXREQ_F_USE_AHG;
715                         priv->s_hdr->ahgidx = qp->s_ahgidx;
716                         priv->s_hdr->ahgcount++;
717                         priv->s_hdr->ahgdesc[0] =
718                                 sdma_build_ahg_descriptor(
719                                         (__force u16)cpu_to_be16((u16)npsn),
720                                         BTH2_OFFSET,
721                                         16,
722                                         16);
723                         if ((npsn & 0xffff0000) !=
724                                         (qp->s_ahgpsn & 0xffff0000)) {
725                                 priv->s_hdr->ahgcount++;
726                                 priv->s_hdr->ahgdesc[1] =
727                                         sdma_build_ahg_descriptor(
728                                                 (__force u16)cpu_to_be16(
729                                                         (u16)(npsn >> 16)),
730                                                 BTH2_OFFSET,
731                                                 0,
732                                                 16);
733                         }
734                 }
735         }
736 }
737
738 void hfi1_make_ruc_header(struct rvt_qp *qp, struct hfi1_other_headers *ohdr,
739                           u32 bth0, u32 bth2, int middle)
740 {
741         struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
742         struct hfi1_qp_priv *priv = qp->priv;
743         u16 lrh0;
744         u32 nwords;
745         u32 extra_bytes;
746         u32 bth1;
747
748         /* Construct the header. */
749         extra_bytes = -qp->s_cur_size & 3;
750         nwords = (qp->s_cur_size + extra_bytes) >> 2;
751         lrh0 = HFI1_LRH_BTH;
752         if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
753                 qp->s_hdrwords += hfi1_make_grh(ibp, &priv->s_hdr->ibh.u.l.grh,
754                                                 &qp->remote_ah_attr.grh,
755                                                 qp->s_hdrwords, nwords);
756                 lrh0 = HFI1_LRH_GRH;
757                 middle = 0;
758         }
759         lrh0 |= (priv->s_sc & 0xf) << 12 | (qp->remote_ah_attr.sl & 0xf) << 4;
760         /*
761          * reset s_hdr/AHG fields
762          *
763          * This insures that the ahgentry/ahgcount
764          * are at a non-AHG default to protect
765          * build_verbs_tx_desc() from using
766          * an include ahgidx.
767          *
768          * build_ahg() will modify as appropriate
769          * to use the AHG feature.
770          */
771         priv->s_hdr->tx_flags = 0;
772         priv->s_hdr->ahgcount = 0;
773         priv->s_hdr->ahgidx = 0;
774         priv->s_hdr->sde = NULL;
775         if (qp->s_mig_state == IB_MIG_MIGRATED)
776                 bth0 |= IB_BTH_MIG_REQ;
777         else
778                 middle = 0;
779         if (middle)
780                 build_ahg(qp, bth2);
781         else
782                 qp->s_flags &= ~HFI1_S_AHG_VALID;
783         priv->s_hdr->ibh.lrh[0] = cpu_to_be16(lrh0);
784         priv->s_hdr->ibh.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
785         priv->s_hdr->ibh.lrh[2] =
786                 cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
787         priv->s_hdr->ibh.lrh[3] = cpu_to_be16(ppd_from_ibp(ibp)->lid |
788                                        qp->remote_ah_attr.src_path_bits);
789         bth0 |= hfi1_get_pkey(ibp, qp->s_pkey_index);
790         bth0 |= extra_bytes << 20;
791         ohdr->bth[0] = cpu_to_be32(bth0);
792         bth1 = qp->remote_qpn;
793         if (qp->s_flags & HFI1_S_ECN) {
794                 qp->s_flags &= ~HFI1_S_ECN;
795                 /* we recently received a FECN, so return a BECN */
796                 bth1 |= (HFI1_BECN_MASK << HFI1_BECN_SHIFT);
797         }
798         ohdr->bth[1] = cpu_to_be32(bth1);
799         ohdr->bth[2] = cpu_to_be32(bth2);
800 }
801
802 /* when sending, force a reschedule every one of these periods */
803 #define SEND_RESCHED_TIMEOUT (5 * HZ)  /* 5s in jiffies */
804
805 /**
806  * hfi1_do_send - perform a send on a QP
807  * @work: contains a pointer to the QP
808  *
809  * Process entries in the send work queue until credit or queue is
810  * exhausted.  Only allow one CPU to send a packet per QP (tasklet).
811  * Otherwise, two threads could send packets out of order.
812  */
813 void hfi1_do_send(struct work_struct *work)
814 {
815         struct iowait *wait = container_of(work, struct iowait, iowork);
816         struct rvt_qp *qp = iowait_to_qp(wait);
817         struct hfi1_pkt_state ps;
818         int (*make_req)(struct rvt_qp *qp);
819         unsigned long flags;
820         unsigned long timeout;
821
822         ps.dev = to_idev(qp->ibqp.device);
823         ps.ibp = to_iport(qp->ibqp.device, qp->port_num);
824         ps.ppd = ppd_from_ibp(ps.ibp);
825
826         if ((qp->ibqp.qp_type == IB_QPT_RC ||
827              qp->ibqp.qp_type == IB_QPT_UC) &&
828             !loopback &&
829             (qp->remote_ah_attr.dlid & ~((1 << ps.ppd->lmc) - 1)) ==
830             ps.ppd->lid) {
831                 ruc_loopback(qp);
832                 return;
833         }
834
835         if (qp->ibqp.qp_type == IB_QPT_RC)
836                 make_req = hfi1_make_rc_req;
837         else if (qp->ibqp.qp_type == IB_QPT_UC)
838                 make_req = hfi1_make_uc_req;
839         else
840                 make_req = hfi1_make_ud_req;
841
842         spin_lock_irqsave(&qp->s_lock, flags);
843
844         /* Return if we are already busy processing a work request. */
845         if (!hfi1_send_ok(qp)) {
846                 spin_unlock_irqrestore(&qp->s_lock, flags);
847                 return;
848         }
849
850         qp->s_flags |= HFI1_S_BUSY;
851
852         spin_unlock_irqrestore(&qp->s_lock, flags);
853
854         timeout = jiffies + SEND_RESCHED_TIMEOUT;
855         do {
856                 /* Check for a constructed packet to be sent. */
857                 if (qp->s_hdrwords != 0) {
858                         /*
859                          * If the packet cannot be sent now, return and
860                          * the send tasklet will be woken up later.
861                          */
862                         if (hfi1_verbs_send(qp, &ps))
863                                 break;
864                         /* Record that s_hdr is empty. */
865                         qp->s_hdrwords = 0;
866                 }
867
868                 /* allow other tasks to run */
869                 if (unlikely(time_after(jiffies, timeout))) {
870                         cond_resched();
871                         ps.ppd->dd->verbs_dev.n_send_schedule++;
872                         timeout = jiffies + SEND_RESCHED_TIMEOUT;
873                 }
874         } while (make_req(qp));
875 }
876
877 /*
878  * This should be called with s_lock held.
879  */
880 void hfi1_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
881                         enum ib_wc_status status)
882 {
883         u32 old_last, last;
884         unsigned i;
885
886         if (!(ib_hfi1_state_ops[qp->state] & HFI1_PROCESS_OR_FLUSH_SEND))
887                 return;
888
889         for (i = 0; i < wqe->wr.num_sge; i++) {
890                 struct rvt_sge *sge = &wqe->sg_list[i];
891
892                 rvt_put_mr(sge->mr);
893         }
894         if (qp->ibqp.qp_type == IB_QPT_UD ||
895             qp->ibqp.qp_type == IB_QPT_SMI ||
896             qp->ibqp.qp_type == IB_QPT_GSI)
897                 atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
898
899         /* See ch. 11.2.4.1 and 10.7.3.1 */
900         if (!(qp->s_flags & HFI1_S_SIGNAL_REQ_WR) ||
901             (wqe->wr.send_flags & IB_SEND_SIGNALED) ||
902             status != IB_WC_SUCCESS) {
903                 struct ib_wc wc;
904
905                 memset(&wc, 0, sizeof(wc));
906                 wc.wr_id = wqe->wr.wr_id;
907                 wc.status = status;
908                 wc.opcode = ib_hfi1_wc_opcode[wqe->wr.opcode];
909                 wc.qp = &qp->ibqp;
910                 if (status == IB_WC_SUCCESS)
911                         wc.byte_len = wqe->length;
912                 hfi1_cq_enter(to_icq(qp->ibqp.send_cq), &wc,
913                               status != IB_WC_SUCCESS);
914         }
915
916         last = qp->s_last;
917         old_last = last;
918         if (++last >= qp->s_size)
919                 last = 0;
920         qp->s_last = last;
921         if (qp->s_acked == old_last)
922                 qp->s_acked = last;
923         if (qp->s_cur == old_last)
924                 qp->s_cur = last;
925         if (qp->s_tail == old_last)
926                 qp->s_tail = last;
927         if (qp->state == IB_QPS_SQD && last == qp->s_cur)
928                 qp->s_draining = 0;
929 }