2 * drivers/net/ethernet/mellanox/mlxsw/spectrum_qdisc.c
3 * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2017 Nogah Frankel <nogahf@mellanox.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the names of the copyright holders nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * Alternatively, this software may be distributed under the terms of the
19 * GNU General Public License ("GPL") version 2 as published by the Free
20 * Software Foundation.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/kernel.h>
36 #include <linux/errno.h>
37 #include <linux/netdevice.h>
38 #include <net/pkt_cls.h>
45 mlxsw_sp_tclass_congestion_enable(struct mlxsw_sp_port *mlxsw_sp_port,
46 int tclass_num, u32 min, u32 max,
47 u32 probability, bool is_ecn)
49 char cwtp_cmd[max_t(u8, MLXSW_REG_CWTP_LEN, MLXSW_REG_CWTPM_LEN)];
50 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
53 mlxsw_reg_cwtp_pack(cwtp_cmd, mlxsw_sp_port->local_port, tclass_num);
54 mlxsw_reg_cwtp_profile_pack(cwtp_cmd, MLXSW_REG_CWTP_DEFAULT_PROFILE,
55 roundup(min, MLXSW_REG_CWTP_MIN_VALUE),
56 roundup(max, MLXSW_REG_CWTP_MIN_VALUE),
59 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(cwtp), cwtp_cmd);
63 mlxsw_reg_cwtpm_pack(cwtp_cmd, mlxsw_sp_port->local_port, tclass_num,
64 MLXSW_REG_CWTP_DEFAULT_PROFILE, true, is_ecn);
66 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(cwtpm), cwtp_cmd);
70 mlxsw_sp_tclass_congestion_disable(struct mlxsw_sp_port *mlxsw_sp_port,
73 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
74 char cwtpm_cmd[MLXSW_REG_CWTPM_LEN];
76 mlxsw_reg_cwtpm_pack(cwtpm_cmd, mlxsw_sp_port->local_port, tclass_num,
77 MLXSW_REG_CWTPM_RESET_PROFILE, false, false);
78 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(cwtpm), cwtpm_cmd);
82 mlxsw_sp_setup_tc_qdisc_clean_stats(struct mlxsw_sp_port *mlxsw_sp_port,
83 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
86 struct red_stats *xstats_base = &mlxsw_sp_qdisc->xstats_base;
87 struct mlxsw_sp_port_xstats *xstats;
88 struct rtnl_link_stats64 *stats;
90 xstats = &mlxsw_sp_port->periodic_hw_stats.xstats;
91 stats = &mlxsw_sp_port->periodic_hw_stats.stats;
93 mlxsw_sp_qdisc->tx_packets = stats->tx_packets;
94 mlxsw_sp_qdisc->tx_bytes = stats->tx_bytes;
96 switch (mlxsw_sp_qdisc->type) {
97 case MLXSW_SP_QDISC_RED:
98 xstats_base->prob_mark = xstats->ecn;
99 xstats_base->prob_drop = xstats->wred_drop[tclass_num];
100 xstats_base->pdrop = xstats->tail_drop[tclass_num];
102 mlxsw_sp_qdisc->overlimits = xstats_base->prob_drop +
103 xstats_base->prob_mark;
104 mlxsw_sp_qdisc->drops = xstats_base->prob_drop +
113 mlxsw_sp_qdisc_red_destroy(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle,
114 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
119 if (mlxsw_sp_qdisc->handle != handle)
122 err = mlxsw_sp_tclass_congestion_disable(mlxsw_sp_port, tclass_num);
123 mlxsw_sp_qdisc->handle = TC_H_UNSPEC;
124 mlxsw_sp_qdisc->type = MLXSW_SP_QDISC_NO_QDISC;
130 mlxsw_sp_qdisc_red_replace(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle,
131 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
133 struct tc_red_qopt_offload_params *p)
135 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
140 if (p->min > p->max) {
141 dev_err(mlxsw_sp->bus_info->dev,
142 "spectrum: RED: min %u is bigger then max %u\n", p->min,
146 if (p->max > MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_BUFFER_SIZE)) {
147 dev_err(mlxsw_sp->bus_info->dev,
148 "spectrum: RED: max value %u is too big\n", p->max);
151 if (p->min == 0 || p->max == 0) {
152 dev_err(mlxsw_sp->bus_info->dev,
153 "spectrum: RED: 0 value is illegal for min and max\n");
157 /* calculate probability in percentage */
158 prob = p->probability;
160 prob = DIV_ROUND_UP(prob, 1 << 16);
161 prob = DIV_ROUND_UP(prob, 1 << 16);
162 min = mlxsw_sp_bytes_cells(mlxsw_sp, p->min);
163 max = mlxsw_sp_bytes_cells(mlxsw_sp, p->max);
164 err = mlxsw_sp_tclass_congestion_enable(mlxsw_sp_port, tclass_num, min,
165 max, prob, p->is_ecn);
169 mlxsw_sp_qdisc->type = MLXSW_SP_QDISC_RED;
170 if (mlxsw_sp_qdisc->handle != handle)
171 mlxsw_sp_setup_tc_qdisc_clean_stats(mlxsw_sp_port,
175 mlxsw_sp_qdisc->handle = handle;
181 mlxsw_sp_qdisc_red_destroy(mlxsw_sp_port, mlxsw_sp_qdisc->handle,
182 mlxsw_sp_qdisc, tclass_num);
187 mlxsw_sp_qdisc_get_red_xstats(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle,
188 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
189 int tclass_num, struct red_stats *res)
191 struct red_stats *xstats_base = &mlxsw_sp_qdisc->xstats_base;
192 struct mlxsw_sp_port_xstats *xstats;
194 if (mlxsw_sp_qdisc->handle != handle ||
195 mlxsw_sp_qdisc->type != MLXSW_SP_QDISC_RED)
198 xstats = &mlxsw_sp_port->periodic_hw_stats.xstats;
200 res->prob_drop = xstats->wred_drop[tclass_num] - xstats_base->prob_drop;
201 res->prob_mark = xstats->ecn - xstats_base->prob_mark;
202 res->pdrop = xstats->tail_drop[tclass_num] - xstats_base->pdrop;
207 mlxsw_sp_qdisc_get_red_stats(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle,
208 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
210 struct tc_red_qopt_offload_stats *res)
212 u64 tx_bytes, tx_packets, overlimits, drops;
213 struct mlxsw_sp_port_xstats *xstats;
214 struct rtnl_link_stats64 *stats;
216 if (mlxsw_sp_qdisc->handle != handle ||
217 mlxsw_sp_qdisc->type != MLXSW_SP_QDISC_RED)
220 xstats = &mlxsw_sp_port->periodic_hw_stats.xstats;
221 stats = &mlxsw_sp_port->periodic_hw_stats.stats;
223 tx_bytes = stats->tx_bytes - mlxsw_sp_qdisc->tx_bytes;
224 tx_packets = stats->tx_packets - mlxsw_sp_qdisc->tx_packets;
225 overlimits = xstats->wred_drop[tclass_num] + xstats->ecn -
226 mlxsw_sp_qdisc->overlimits;
227 drops = xstats->wred_drop[tclass_num] + xstats->tail_drop[tclass_num] -
228 mlxsw_sp_qdisc->drops;
230 _bstats_update(res->bstats, tx_bytes, tx_packets);
231 res->qstats->overlimits += overlimits;
232 res->qstats->drops += drops;
233 res->qstats->backlog += mlxsw_sp_cells_bytes(mlxsw_sp_port->mlxsw_sp,
234 xstats->backlog[tclass_num]);
236 mlxsw_sp_qdisc->drops += drops;
237 mlxsw_sp_qdisc->overlimits += overlimits;
238 mlxsw_sp_qdisc->tx_bytes += tx_bytes;
239 mlxsw_sp_qdisc->tx_packets += tx_packets;
243 #define MLXSW_SP_PORT_DEFAULT_TCLASS 0
245 int mlxsw_sp_setup_tc_red(struct mlxsw_sp_port *mlxsw_sp_port,
246 struct tc_red_qopt_offload *p)
248 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc;
251 if (p->parent != TC_H_ROOT)
254 mlxsw_sp_qdisc = &mlxsw_sp_port->root_qdisc;
255 tclass_num = MLXSW_SP_PORT_DEFAULT_TCLASS;
257 switch (p->command) {
259 return mlxsw_sp_qdisc_red_replace(mlxsw_sp_port, p->handle,
260 mlxsw_sp_qdisc, tclass_num,
263 return mlxsw_sp_qdisc_red_destroy(mlxsw_sp_port, p->handle,
264 mlxsw_sp_qdisc, tclass_num);
266 return mlxsw_sp_qdisc_get_red_xstats(mlxsw_sp_port, p->handle,
267 mlxsw_sp_qdisc, tclass_num,
270 return mlxsw_sp_qdisc_get_red_stats(mlxsw_sp_port, p->handle,
271 mlxsw_sp_qdisc, tclass_num,