2 * Copyright(c) 2016 Intel Corporation.
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.
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.
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.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
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
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.
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.
48 #include <rdma/rdma_vt.h>
49 #include <rdma/ib_hdrs.h>
52 * Convert the AETH credit code into the number of credits.
54 static const u16 credit_table
[31] = {
89 * rvt_compute_aeth - compute the AETH (syndrome + MSN)
90 * @qp: the queue pair to compute the AETH for
94 __be32
rvt_compute_aeth(struct rvt_qp
*qp
)
96 u32 aeth
= qp
->r_msn
& IB_MSN_MASK
;
100 * Shared receive queues don't generate credits.
101 * Set the credit field to the invalid value.
103 aeth
|= IB_AETH_CREDIT_INVAL
<< IB_AETH_CREDIT_SHIFT
;
107 struct rvt_rwq
*wq
= qp
->r_rq
.wq
;
111 /* sanity check pointers before trusting them */
113 if (head
>= qp
->r_rq
.size
)
116 if (tail
>= qp
->r_rq
.size
)
119 * Compute the number of credits available (RWQEs).
120 * There is a small chance that the pair of reads are
121 * not atomic, which is OK, since the fuzziness is
122 * resolved as further ACKs go out.
124 credits
= head
- tail
;
125 if ((int)credits
< 0)
126 credits
+= qp
->r_rq
.size
;
128 * Binary search the credit table to find the code to
135 if (credit_table
[x
] == credits
)
137 if (credit_table
[x
] > credits
) {
145 aeth
|= x
<< IB_AETH_CREDIT_SHIFT
;
147 return cpu_to_be32(aeth
);
149 EXPORT_SYMBOL(rvt_compute_aeth
);
152 * rvt_get_credit - flush the send work queue of a QP
153 * @qp: the qp who's send work queue to flush
154 * @aeth: the Acknowledge Extended Transport Header
156 * The QP s_lock should be held.
158 void rvt_get_credit(struct rvt_qp
*qp
, u32 aeth
)
160 struct rvt_dev_info
*rdi
= ib_to_rvt(qp
->ibqp
.device
);
161 u32 credit
= (aeth
>> IB_AETH_CREDIT_SHIFT
) & IB_AETH_CREDIT_MASK
;
163 lockdep_assert_held(&qp
->s_lock
);
165 * If the credit is invalid, we can send
166 * as many packets as we like. Otherwise, we have to
167 * honor the credit field.
169 if (credit
== IB_AETH_CREDIT_INVAL
) {
170 if (!(qp
->s_flags
& RVT_S_UNLIMITED_CREDIT
)) {
171 qp
->s_flags
|= RVT_S_UNLIMITED_CREDIT
;
172 if (qp
->s_flags
& RVT_S_WAIT_SSN_CREDIT
) {
173 qp
->s_flags
&= ~RVT_S_WAIT_SSN_CREDIT
;
174 rdi
->driver_f
.schedule_send(qp
);
177 } else if (!(qp
->s_flags
& RVT_S_UNLIMITED_CREDIT
)) {
178 /* Compute new LSN (i.e., MSN + credit) */
179 credit
= (aeth
+ credit_table
[credit
]) & IB_MSN_MASK
;
180 if (rvt_cmp_msn(credit
, qp
->s_lsn
) > 0) {
182 if (qp
->s_flags
& RVT_S_WAIT_SSN_CREDIT
) {
183 qp
->s_flags
&= ~RVT_S_WAIT_SSN_CREDIT
;
184 rdi
->driver_f
.schedule_send(qp
);
189 EXPORT_SYMBOL(rvt_get_credit
);