1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright(c) 2016 Intel Corporation.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * This file is provided under a dual BSD/GPLv2 license. When using or
5*4882a593Smuzhiyun * redistributing this file, you may do so under either license.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * GPL LICENSE SUMMARY
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun * it under the terms of version 2 of the GNU General Public License as
11*4882a593Smuzhiyun * published by the Free Software Foundation.
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * This program is distributed in the hope that it will be useful, but
14*4882a593Smuzhiyun * WITHOUT ANY WARRANTY; without even the implied warranty of
15*4882a593Smuzhiyun * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16*4882a593Smuzhiyun * General Public License for more details.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * BSD LICENSE
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
21*4882a593Smuzhiyun * modification, are permitted provided that the following conditions
22*4882a593Smuzhiyun * are met:
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * - Redistributions of source code must retain the above copyright
25*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer.
26*4882a593Smuzhiyun * - Redistributions in binary form must reproduce the above copyright
27*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer in
28*4882a593Smuzhiyun * the documentation and/or other materials provided with the
29*4882a593Smuzhiyun * distribution.
30*4882a593Smuzhiyun * - Neither the name of Intel Corporation nor the names of its
31*4882a593Smuzhiyun * contributors may be used to endorse or promote products derived
32*4882a593Smuzhiyun * from this software without specific prior written permission.
33*4882a593Smuzhiyun *
34*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35*4882a593Smuzhiyun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36*4882a593Smuzhiyun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37*4882a593Smuzhiyun * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38*4882a593Smuzhiyun * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39*4882a593Smuzhiyun * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40*4882a593Smuzhiyun * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41*4882a593Smuzhiyun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42*4882a593Smuzhiyun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44*4882a593Smuzhiyun * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun */
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun #include <rdma/rdmavt_qp.h>
49*4882a593Smuzhiyun #include <rdma/ib_hdrs.h>
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun /*
52*4882a593Smuzhiyun * Convert the AETH credit code into the number of credits.
53*4882a593Smuzhiyun */
54*4882a593Smuzhiyun static const u16 credit_table[31] = {
55*4882a593Smuzhiyun 0, /* 0 */
56*4882a593Smuzhiyun 1, /* 1 */
57*4882a593Smuzhiyun 2, /* 2 */
58*4882a593Smuzhiyun 3, /* 3 */
59*4882a593Smuzhiyun 4, /* 4 */
60*4882a593Smuzhiyun 6, /* 5 */
61*4882a593Smuzhiyun 8, /* 6 */
62*4882a593Smuzhiyun 12, /* 7 */
63*4882a593Smuzhiyun 16, /* 8 */
64*4882a593Smuzhiyun 24, /* 9 */
65*4882a593Smuzhiyun 32, /* A */
66*4882a593Smuzhiyun 48, /* B */
67*4882a593Smuzhiyun 64, /* C */
68*4882a593Smuzhiyun 96, /* D */
69*4882a593Smuzhiyun 128, /* E */
70*4882a593Smuzhiyun 192, /* F */
71*4882a593Smuzhiyun 256, /* 10 */
72*4882a593Smuzhiyun 384, /* 11 */
73*4882a593Smuzhiyun 512, /* 12 */
74*4882a593Smuzhiyun 768, /* 13 */
75*4882a593Smuzhiyun 1024, /* 14 */
76*4882a593Smuzhiyun 1536, /* 15 */
77*4882a593Smuzhiyun 2048, /* 16 */
78*4882a593Smuzhiyun 3072, /* 17 */
79*4882a593Smuzhiyun 4096, /* 18 */
80*4882a593Smuzhiyun 6144, /* 19 */
81*4882a593Smuzhiyun 8192, /* 1A */
82*4882a593Smuzhiyun 12288, /* 1B */
83*4882a593Smuzhiyun 16384, /* 1C */
84*4882a593Smuzhiyun 24576, /* 1D */
85*4882a593Smuzhiyun 32768 /* 1E */
86*4882a593Smuzhiyun };
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun /**
89*4882a593Smuzhiyun * rvt_compute_aeth - compute the AETH (syndrome + MSN)
90*4882a593Smuzhiyun * @qp: the queue pair to compute the AETH for
91*4882a593Smuzhiyun *
92*4882a593Smuzhiyun * Returns the AETH.
93*4882a593Smuzhiyun */
rvt_compute_aeth(struct rvt_qp * qp)94*4882a593Smuzhiyun __be32 rvt_compute_aeth(struct rvt_qp *qp)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun u32 aeth = qp->r_msn & IB_MSN_MASK;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun if (qp->ibqp.srq) {
99*4882a593Smuzhiyun /*
100*4882a593Smuzhiyun * Shared receive queues don't generate credits.
101*4882a593Smuzhiyun * Set the credit field to the invalid value.
102*4882a593Smuzhiyun */
103*4882a593Smuzhiyun aeth |= IB_AETH_CREDIT_INVAL << IB_AETH_CREDIT_SHIFT;
104*4882a593Smuzhiyun } else {
105*4882a593Smuzhiyun u32 min, max, x;
106*4882a593Smuzhiyun u32 credits;
107*4882a593Smuzhiyun u32 head;
108*4882a593Smuzhiyun u32 tail;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun credits = READ_ONCE(qp->r_rq.kwq->count);
111*4882a593Smuzhiyun if (credits == 0) {
112*4882a593Smuzhiyun /* sanity check pointers before trusting them */
113*4882a593Smuzhiyun if (qp->ip) {
114*4882a593Smuzhiyun head = RDMA_READ_UAPI_ATOMIC(qp->r_rq.wq->head);
115*4882a593Smuzhiyun tail = RDMA_READ_UAPI_ATOMIC(qp->r_rq.wq->tail);
116*4882a593Smuzhiyun } else {
117*4882a593Smuzhiyun head = READ_ONCE(qp->r_rq.kwq->head);
118*4882a593Smuzhiyun tail = READ_ONCE(qp->r_rq.kwq->tail);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun if (head >= qp->r_rq.size)
121*4882a593Smuzhiyun head = 0;
122*4882a593Smuzhiyun if (tail >= qp->r_rq.size)
123*4882a593Smuzhiyun tail = 0;
124*4882a593Smuzhiyun /*
125*4882a593Smuzhiyun * Compute the number of credits available (RWQEs).
126*4882a593Smuzhiyun * There is a small chance that the pair of reads are
127*4882a593Smuzhiyun * not atomic, which is OK, since the fuzziness is
128*4882a593Smuzhiyun * resolved as further ACKs go out.
129*4882a593Smuzhiyun */
130*4882a593Smuzhiyun credits = rvt_get_rq_count(&qp->r_rq, head, tail);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun /*
133*4882a593Smuzhiyun * Binary search the credit table to find the code to
134*4882a593Smuzhiyun * use.
135*4882a593Smuzhiyun */
136*4882a593Smuzhiyun min = 0;
137*4882a593Smuzhiyun max = 31;
138*4882a593Smuzhiyun for (;;) {
139*4882a593Smuzhiyun x = (min + max) / 2;
140*4882a593Smuzhiyun if (credit_table[x] == credits)
141*4882a593Smuzhiyun break;
142*4882a593Smuzhiyun if (credit_table[x] > credits) {
143*4882a593Smuzhiyun max = x;
144*4882a593Smuzhiyun } else {
145*4882a593Smuzhiyun if (min == x)
146*4882a593Smuzhiyun break;
147*4882a593Smuzhiyun min = x;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun aeth |= x << IB_AETH_CREDIT_SHIFT;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun return cpu_to_be32(aeth);
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun EXPORT_SYMBOL(rvt_compute_aeth);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun /**
157*4882a593Smuzhiyun * rvt_get_credit - flush the send work queue of a QP
158*4882a593Smuzhiyun * @qp: the qp who's send work queue to flush
159*4882a593Smuzhiyun * @aeth: the Acknowledge Extended Transport Header
160*4882a593Smuzhiyun *
161*4882a593Smuzhiyun * The QP s_lock should be held.
162*4882a593Smuzhiyun */
rvt_get_credit(struct rvt_qp * qp,u32 aeth)163*4882a593Smuzhiyun void rvt_get_credit(struct rvt_qp *qp, u32 aeth)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
166*4882a593Smuzhiyun u32 credit = (aeth >> IB_AETH_CREDIT_SHIFT) & IB_AETH_CREDIT_MASK;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun lockdep_assert_held(&qp->s_lock);
169*4882a593Smuzhiyun /*
170*4882a593Smuzhiyun * If the credit is invalid, we can send
171*4882a593Smuzhiyun * as many packets as we like. Otherwise, we have to
172*4882a593Smuzhiyun * honor the credit field.
173*4882a593Smuzhiyun */
174*4882a593Smuzhiyun if (credit == IB_AETH_CREDIT_INVAL) {
175*4882a593Smuzhiyun if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT)) {
176*4882a593Smuzhiyun qp->s_flags |= RVT_S_UNLIMITED_CREDIT;
177*4882a593Smuzhiyun if (qp->s_flags & RVT_S_WAIT_SSN_CREDIT) {
178*4882a593Smuzhiyun qp->s_flags &= ~RVT_S_WAIT_SSN_CREDIT;
179*4882a593Smuzhiyun rdi->driver_f.schedule_send(qp);
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun } else if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT)) {
183*4882a593Smuzhiyun /* Compute new LSN (i.e., MSN + credit) */
184*4882a593Smuzhiyun credit = (aeth + credit_table[credit]) & IB_MSN_MASK;
185*4882a593Smuzhiyun if (rvt_cmp_msn(credit, qp->s_lsn) > 0) {
186*4882a593Smuzhiyun qp->s_lsn = credit;
187*4882a593Smuzhiyun if (qp->s_flags & RVT_S_WAIT_SSN_CREDIT) {
188*4882a593Smuzhiyun qp->s_flags &= ~RVT_S_WAIT_SSN_CREDIT;
189*4882a593Smuzhiyun rdi->driver_f.schedule_send(qp);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun EXPORT_SYMBOL(rvt_get_credit);
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun /**
197*4882a593Smuzhiyun * rvt_restart_sge - rewind the sge state for a wqe
198*4882a593Smuzhiyun * @ss: the sge state pointer
199*4882a593Smuzhiyun * @wqe: the wqe to rewind
200*4882a593Smuzhiyun * @len: the data length from the start of the wqe in bytes
201*4882a593Smuzhiyun *
202*4882a593Smuzhiyun * Returns the remaining data length.
203*4882a593Smuzhiyun */
rvt_restart_sge(struct rvt_sge_state * ss,struct rvt_swqe * wqe,u32 len)204*4882a593Smuzhiyun u32 rvt_restart_sge(struct rvt_sge_state *ss, struct rvt_swqe *wqe, u32 len)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun ss->sge = wqe->sg_list[0];
207*4882a593Smuzhiyun ss->sg_list = wqe->sg_list + 1;
208*4882a593Smuzhiyun ss->num_sge = wqe->wr.num_sge;
209*4882a593Smuzhiyun ss->total_len = wqe->length;
210*4882a593Smuzhiyun rvt_skip_sge(ss, len, false);
211*4882a593Smuzhiyun return wqe->length - len;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun EXPORT_SYMBOL(rvt_restart_sge);
214*4882a593Smuzhiyun
215