Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/nab/target...
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlxsw / spectrum_qdisc.c
1 /*
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>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
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.
17  *
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.
21  *
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.
33  */
34
35 #include <linux/kernel.h>
36 #include <linux/errno.h>
37 #include <linux/netdevice.h>
38 #include <net/pkt_cls.h>
39 #include <net/red.h>
40
41 #include "spectrum.h"
42 #include "reg.h"
43
44 static int
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)
48 {
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;
51         int err;
52
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),
57                                     probability);
58
59         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(cwtp), cwtp_cmd);
60         if (err)
61                 return err;
62
63         mlxsw_reg_cwtpm_pack(cwtp_cmd, mlxsw_sp_port->local_port, tclass_num,
64                              MLXSW_REG_CWTP_DEFAULT_PROFILE, true, is_ecn);
65
66         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(cwtpm), cwtp_cmd);
67 }
68
69 static int
70 mlxsw_sp_tclass_congestion_disable(struct mlxsw_sp_port *mlxsw_sp_port,
71                                    int tclass_num)
72 {
73         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
74         char cwtpm_cmd[MLXSW_REG_CWTPM_LEN];
75
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);
79 }
80
81 static void
82 mlxsw_sp_setup_tc_qdisc_clean_stats(struct mlxsw_sp_port *mlxsw_sp_port,
83                                     struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
84                                     int tclass_num)
85 {
86         struct red_stats *xstats_base = &mlxsw_sp_qdisc->xstats_base;
87         struct mlxsw_sp_port_xstats *xstats;
88         struct rtnl_link_stats64 *stats;
89
90         xstats = &mlxsw_sp_port->periodic_hw_stats.xstats;
91         stats = &mlxsw_sp_port->periodic_hw_stats.stats;
92
93         mlxsw_sp_qdisc->tx_packets = stats->tx_packets;
94         mlxsw_sp_qdisc->tx_bytes = stats->tx_bytes;
95
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];
101
102                 mlxsw_sp_qdisc->overlimits = xstats_base->prob_drop +
103                                              xstats_base->prob_mark;
104                 mlxsw_sp_qdisc->drops = xstats_base->prob_drop +
105                                         xstats_base->pdrop;
106                 break;
107         default:
108                 break;
109         }
110 }
111
112 static int
113 mlxsw_sp_qdisc_red_destroy(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle,
114                            struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
115                            int tclass_num)
116 {
117         int err;
118
119         if (mlxsw_sp_qdisc->handle != handle)
120                 return 0;
121
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;
125
126         return err;
127 }
128
129 static int
130 mlxsw_sp_qdisc_red_replace(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle,
131                            struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
132                            int tclass_num,
133                            struct tc_red_qopt_offload_params *p)
134 {
135         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
136         u32 min, max;
137         u64 prob;
138         int err = 0;
139
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,
143                         p->max);
144                 goto err_bad_param;
145         }
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);
149                 goto err_bad_param;
150         }
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");
154                 goto err_bad_param;
155         }
156
157         /* calculate probability in percentage */
158         prob = p->probability;
159         prob *= 100;
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);
166         if (err)
167                 goto err_config;
168
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,
172                                                     mlxsw_sp_qdisc,
173                                                     tclass_num);
174
175         mlxsw_sp_qdisc->handle = handle;
176         return 0;
177
178 err_bad_param:
179         err = -EINVAL;
180 err_config:
181         mlxsw_sp_qdisc_red_destroy(mlxsw_sp_port, mlxsw_sp_qdisc->handle,
182                                    mlxsw_sp_qdisc, tclass_num);
183         return err;
184 }
185
186 static int
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)
190 {
191         struct red_stats *xstats_base = &mlxsw_sp_qdisc->xstats_base;
192         struct mlxsw_sp_port_xstats *xstats;
193
194         if (mlxsw_sp_qdisc->handle != handle ||
195             mlxsw_sp_qdisc->type != MLXSW_SP_QDISC_RED)
196                 return -EOPNOTSUPP;
197
198         xstats = &mlxsw_sp_port->periodic_hw_stats.xstats;
199
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;
203         return 0;
204 }
205
206 static int
207 mlxsw_sp_qdisc_get_red_stats(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle,
208                              struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
209                              int tclass_num,
210                              struct tc_red_qopt_offload_stats *res)
211 {
212         u64 tx_bytes, tx_packets, overlimits, drops;
213         struct mlxsw_sp_port_xstats *xstats;
214         struct rtnl_link_stats64 *stats;
215
216         if (mlxsw_sp_qdisc->handle != handle ||
217             mlxsw_sp_qdisc->type != MLXSW_SP_QDISC_RED)
218                 return -EOPNOTSUPP;
219
220         xstats = &mlxsw_sp_port->periodic_hw_stats.xstats;
221         stats = &mlxsw_sp_port->periodic_hw_stats.stats;
222
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;
229
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]);
235
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;
240         return 0;
241 }
242
243 #define MLXSW_SP_PORT_DEFAULT_TCLASS 0
244
245 int mlxsw_sp_setup_tc_red(struct mlxsw_sp_port *mlxsw_sp_port,
246                           struct tc_red_qopt_offload *p)
247 {
248         struct mlxsw_sp_qdisc *mlxsw_sp_qdisc;
249         int tclass_num;
250
251         if (p->parent != TC_H_ROOT)
252                 return -EOPNOTSUPP;
253
254         mlxsw_sp_qdisc = &mlxsw_sp_port->root_qdisc;
255         tclass_num = MLXSW_SP_PORT_DEFAULT_TCLASS;
256
257         switch (p->command) {
258         case TC_RED_REPLACE:
259                 return mlxsw_sp_qdisc_red_replace(mlxsw_sp_port, p->handle,
260                                                   mlxsw_sp_qdisc, tclass_num,
261                                                   &p->set);
262         case TC_RED_DESTROY:
263                 return mlxsw_sp_qdisc_red_destroy(mlxsw_sp_port, p->handle,
264                                                   mlxsw_sp_qdisc, tclass_num);
265         case TC_RED_XSTATS:
266                 return mlxsw_sp_qdisc_get_red_xstats(mlxsw_sp_port, p->handle,
267                                                      mlxsw_sp_qdisc, tclass_num,
268                                                      p->xstats);
269         case TC_RED_STATS:
270                 return mlxsw_sp_qdisc_get_red_stats(mlxsw_sp_port, p->handle,
271                                                     mlxsw_sp_qdisc, tclass_num,
272                                                     &p->stats);
273         default:
274                 return -EOPNOTSUPP;
275         }
276 }