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