blob: 6042c1741f77fe0522aaf6320a80d20cf605fc17 [file] [log] [blame]
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001/*
2 * drivers/net/ethernet/mellanox/mlxsw/spectrum_buffers.c
3 * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2015 Jiri Pirko <jiri@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/types.h>
Ido Schimmeldd6cb0f2016-04-06 17:10:01 +020037#include <linux/dcbnl.h>
Ido Schimmelff6551e2016-04-06 17:10:03 +020038#include <linux/if_ether.h>
Jiri Pirko56ade8f2015-10-16 14:01:37 +020039
40#include "spectrum.h"
41#include "core.h"
42#include "port.h"
43#include "reg.h"
44
Jiri Pirko078f9c72016-04-14 18:19:19 +020045static struct mlxsw_sp_sb_pr *mlxsw_sp_sb_pr_get(struct mlxsw_sp *mlxsw_sp,
46 u8 pool,
47 enum mlxsw_reg_sbxx_dir dir)
48{
49 return &mlxsw_sp->sb.prs[dir][pool];
50}
51
52static struct mlxsw_sp_sb_cm *mlxsw_sp_sb_cm_get(struct mlxsw_sp *mlxsw_sp,
53 u8 local_port, u8 pg_buff,
54 enum mlxsw_reg_sbxx_dir dir)
55{
56 return &mlxsw_sp->sb.ports[local_port].cms[dir][pg_buff];
57}
58
59static struct mlxsw_sp_sb_pm *mlxsw_sp_sb_pm_get(struct mlxsw_sp *mlxsw_sp,
60 u8 local_port, u8 pool,
61 enum mlxsw_reg_sbxx_dir dir)
62{
63 return &mlxsw_sp->sb.ports[local_port].pms[dir][pool];
64}
65
Jiri Pirko94266e32016-04-14 18:19:16 +020066static int mlxsw_sp_sb_pr_write(struct mlxsw_sp *mlxsw_sp, u8 pool,
67 enum mlxsw_reg_sbxx_dir dir,
68 enum mlxsw_reg_sbpr_mode mode, u32 size)
69{
70 char sbpr_pl[MLXSW_REG_SBPR_LEN];
Jiri Pirko078f9c72016-04-14 18:19:19 +020071 struct mlxsw_sp_sb_pr *pr;
72 int err;
Jiri Pirko94266e32016-04-14 18:19:16 +020073
74 mlxsw_reg_sbpr_pack(sbpr_pl, pool, dir, mode, size);
Jiri Pirko078f9c72016-04-14 18:19:19 +020075 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbpr), sbpr_pl);
76 if (err)
77 return err;
78
79 pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool, dir);
80 pr->mode = mode;
81 pr->size = size;
82 return 0;
Jiri Pirko94266e32016-04-14 18:19:16 +020083}
84
85static int mlxsw_sp_sb_cm_write(struct mlxsw_sp *mlxsw_sp, u8 local_port,
86 u8 pg_buff, enum mlxsw_reg_sbxx_dir dir,
87 u32 min_buff, u32 max_buff, u8 pool)
88{
89 char sbcm_pl[MLXSW_REG_SBCM_LEN];
Jiri Pirko078f9c72016-04-14 18:19:19 +020090 int err;
Jiri Pirko94266e32016-04-14 18:19:16 +020091
92 mlxsw_reg_sbcm_pack(sbcm_pl, local_port, pg_buff, dir,
93 min_buff, max_buff, pool);
Jiri Pirko078f9c72016-04-14 18:19:19 +020094 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbcm), sbcm_pl);
95 if (err)
96 return err;
97 if (pg_buff < MLXSW_SP_SB_TC_COUNT) {
98 struct mlxsw_sp_sb_cm *cm;
99
100 cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port, pg_buff, dir);
101 cm->min_buff = min_buff;
102 cm->max_buff = max_buff;
103 cm->pool = pool;
104 }
105 return 0;
Jiri Pirko94266e32016-04-14 18:19:16 +0200106}
107
108static int mlxsw_sp_sb_pm_write(struct mlxsw_sp *mlxsw_sp, u8 local_port,
109 u8 pool, enum mlxsw_reg_sbxx_dir dir,
110 u32 min_buff, u32 max_buff)
111{
112 char sbpm_pl[MLXSW_REG_SBPM_LEN];
Jiri Pirko078f9c72016-04-14 18:19:19 +0200113 struct mlxsw_sp_sb_pm *pm;
114 int err;
Jiri Pirko94266e32016-04-14 18:19:16 +0200115
116 mlxsw_reg_sbpm_pack(sbpm_pl, local_port, pool, dir, min_buff, max_buff);
Jiri Pirko078f9c72016-04-14 18:19:19 +0200117 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbpm), sbpm_pl);
118 if (err)
119 return err;
120
121 pm = mlxsw_sp_sb_pm_get(mlxsw_sp, local_port, pool, dir);
122 pm->min_buff = min_buff;
123 pm->max_buff = max_buff;
124 return 0;
Jiri Pirko94266e32016-04-14 18:19:16 +0200125}
126
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200127static const u16 mlxsw_sp_pbs[] = {
128 2 * MLXSW_SP_BYTES_TO_CELLS(ETH_FRAME_LEN),
129 0,
130 0,
131 0,
132 0,
133 0,
134 0,
135 0,
136 0, /* Unused */
137 2 * MLXSW_SP_BYTES_TO_CELLS(MLXSW_PORT_MAX_MTU),
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200138};
139
140#define MLXSW_SP_PBS_LEN ARRAY_SIZE(mlxsw_sp_pbs)
141
142static int mlxsw_sp_port_pb_init(struct mlxsw_sp_port *mlxsw_sp_port)
143{
144 char pbmc_pl[MLXSW_REG_PBMC_LEN];
145 int i;
146
147 mlxsw_reg_pbmc_pack(pbmc_pl, mlxsw_sp_port->local_port,
148 0xffff, 0xffff / 2);
149 for (i = 0; i < MLXSW_SP_PBS_LEN; i++) {
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200150 if (i == 8)
151 continue;
152 mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, i, mlxsw_sp_pbs[i]);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200153 }
Ido Schimmeld6b7c132016-04-06 17:10:05 +0200154 mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl,
155 MLXSW_REG_PBMC_PORT_SHARED_BUF_IDX, 0);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200156 return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core,
157 MLXSW_REG(pbmc), pbmc_pl);
158}
159
Ido Schimmeldd6cb0f2016-04-06 17:10:01 +0200160static int mlxsw_sp_port_pb_prio_init(struct mlxsw_sp_port *mlxsw_sp_port)
161{
162 char pptb_pl[MLXSW_REG_PPTB_LEN];
163 int i;
164
165 mlxsw_reg_pptb_pack(pptb_pl, mlxsw_sp_port->local_port);
166 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
167 mlxsw_reg_pptb_prio_to_buff_set(pptb_pl, i, 0);
168 return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pptb),
169 pptb_pl);
170}
171
172static int mlxsw_sp_port_headroom_init(struct mlxsw_sp_port *mlxsw_sp_port)
173{
174 int err;
175
176 err = mlxsw_sp_port_pb_init(mlxsw_sp_port);
177 if (err)
178 return err;
179 return mlxsw_sp_port_pb_prio_init(mlxsw_sp_port);
180}
181
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200182#define MLXSW_SP_SB_PR_INGRESS_SIZE \
Ido Schimmel1a198442016-04-06 17:10:02 +0200183 (15000000 - (2 * 20000 * MLXSW_PORT_MAX_PORTS))
Jiri Pirkobc872502016-04-14 18:19:21 +0200184#define MLXSW_SP_SB_PR_INGRESS_MNG_SIZE (200 * 1000)
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200185#define MLXSW_SP_SB_PR_EGRESS_SIZE \
Ido Schimmel1a198442016-04-06 17:10:02 +0200186 (14000000 - (8 * 1500 * MLXSW_PORT_MAX_PORTS))
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200187
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200188#define MLXSW_SP_SB_PR(_mode, _size) \
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200189 { \
190 .mode = _mode, \
191 .size = _size, \
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200192 }
193
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200194static const struct mlxsw_sp_sb_pr mlxsw_sp_sb_prs_ingress[] = {
195 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC,
196 MLXSW_SP_BYTES_TO_CELLS(MLXSW_SP_SB_PR_INGRESS_SIZE)),
197 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
198 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
Jiri Pirkobc872502016-04-14 18:19:21 +0200199 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC,
200 MLXSW_SP_BYTES_TO_CELLS(MLXSW_SP_SB_PR_INGRESS_MNG_SIZE)),
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200201};
202
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200203#define MLXSW_SP_SB_PRS_INGRESS_LEN ARRAY_SIZE(mlxsw_sp_sb_prs_ingress)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200204
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200205static const struct mlxsw_sp_sb_pr mlxsw_sp_sb_prs_egress[] = {
206 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC,
207 MLXSW_SP_BYTES_TO_CELLS(MLXSW_SP_SB_PR_EGRESS_SIZE)),
208 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
209 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
Jiri Pirko5408f7c2016-04-14 18:19:20 +0200210 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200211};
212
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200213#define MLXSW_SP_SB_PRS_EGRESS_LEN ARRAY_SIZE(mlxsw_sp_sb_prs_egress)
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200214
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200215static int __mlxsw_sp_sb_prs_init(struct mlxsw_sp *mlxsw_sp,
216 enum mlxsw_reg_sbxx_dir dir,
217 const struct mlxsw_sp_sb_pr *prs,
218 size_t prs_len)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200219{
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200220 int i;
221 int err;
222
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200223 for (i = 0; i < prs_len; i++) {
224 const struct mlxsw_sp_sb_pr *pr;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200225
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200226 pr = &prs[i];
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200227 err = mlxsw_sp_sb_pr_write(mlxsw_sp, i, dir,
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200228 pr->mode, pr->size);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200229 if (err)
230 return err;
231 }
232 return 0;
233}
234
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200235static int mlxsw_sp_sb_prs_init(struct mlxsw_sp *mlxsw_sp)
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200236{
237 int err;
238
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200239 err = __mlxsw_sp_sb_prs_init(mlxsw_sp, MLXSW_REG_SBXX_DIR_INGRESS,
240 mlxsw_sp_sb_prs_ingress,
241 MLXSW_SP_SB_PRS_INGRESS_LEN);
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200242 if (err)
243 return err;
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200244 return __mlxsw_sp_sb_prs_init(mlxsw_sp, MLXSW_REG_SBXX_DIR_EGRESS,
245 mlxsw_sp_sb_prs_egress,
246 MLXSW_SP_SB_PRS_EGRESS_LEN);
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200247}
248
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200249#define MLXSW_SP_SB_CM(_min_buff, _max_buff, _pool) \
250 { \
251 .min_buff = _min_buff, \
252 .max_buff = _max_buff, \
253 .pool = _pool, \
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200254 }
255
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200256static const struct mlxsw_sp_sb_cm mlxsw_sp_sb_cms_ingress[] = {
257 MLXSW_SP_SB_CM(MLXSW_SP_BYTES_TO_CELLS(10000), 8, 0),
Jiri Pirkoc30a53c2016-04-14 18:19:22 +0200258 MLXSW_SP_SB_CM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN, 0),
259 MLXSW_SP_SB_CM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN, 0),
260 MLXSW_SP_SB_CM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN, 0),
261 MLXSW_SP_SB_CM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN, 0),
262 MLXSW_SP_SB_CM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN, 0),
263 MLXSW_SP_SB_CM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN, 0),
264 MLXSW_SP_SB_CM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN, 0),
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200265 MLXSW_SP_SB_CM(0, 0, 0), /* dummy, this PG does not exist */
Jiri Pirkobc872502016-04-14 18:19:21 +0200266 MLXSW_SP_SB_CM(MLXSW_SP_BYTES_TO_CELLS(20000), 1, 3),
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200267};
268
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200269#define MLXSW_SP_SB_CMS_INGRESS_LEN ARRAY_SIZE(mlxsw_sp_sb_cms_ingress)
270
271static const struct mlxsw_sp_sb_cm mlxsw_sp_sb_cms_egress[] = {
272 MLXSW_SP_SB_CM(MLXSW_SP_BYTES_TO_CELLS(1500), 9, 0),
273 MLXSW_SP_SB_CM(MLXSW_SP_BYTES_TO_CELLS(1500), 9, 0),
274 MLXSW_SP_SB_CM(MLXSW_SP_BYTES_TO_CELLS(1500), 9, 0),
275 MLXSW_SP_SB_CM(MLXSW_SP_BYTES_TO_CELLS(1500), 9, 0),
276 MLXSW_SP_SB_CM(MLXSW_SP_BYTES_TO_CELLS(1500), 9, 0),
277 MLXSW_SP_SB_CM(MLXSW_SP_BYTES_TO_CELLS(1500), 9, 0),
278 MLXSW_SP_SB_CM(MLXSW_SP_BYTES_TO_CELLS(1500), 9, 0),
279 MLXSW_SP_SB_CM(MLXSW_SP_BYTES_TO_CELLS(1500), 9, 0),
280 MLXSW_SP_SB_CM(0, 0, 0),
281 MLXSW_SP_SB_CM(0, 0, 0),
282 MLXSW_SP_SB_CM(0, 0, 0),
283 MLXSW_SP_SB_CM(0, 0, 0),
284 MLXSW_SP_SB_CM(0, 0, 0),
285 MLXSW_SP_SB_CM(0, 0, 0),
286 MLXSW_SP_SB_CM(0, 0, 0),
287 MLXSW_SP_SB_CM(0, 0, 0),
288 MLXSW_SP_SB_CM(1, 0xff, 0),
289};
290
291#define MLXSW_SP_SB_CMS_EGRESS_LEN ARRAY_SIZE(mlxsw_sp_sb_cms_egress)
292
Jiri Pirko5408f7c2016-04-14 18:19:20 +0200293#define MLXSW_SP_CPU_PORT_SB_CM MLXSW_SP_SB_CM(0, 0, 0)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200294
295static const struct mlxsw_sp_sb_cm mlxsw_sp_cpu_port_sb_cms[] = {
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200296 MLXSW_SP_CPU_PORT_SB_CM,
297 MLXSW_SP_CPU_PORT_SB_CM,
298 MLXSW_SP_CPU_PORT_SB_CM,
299 MLXSW_SP_CPU_PORT_SB_CM,
300 MLXSW_SP_CPU_PORT_SB_CM,
301 MLXSW_SP_CPU_PORT_SB_CM,
302 MLXSW_SP_CPU_PORT_SB_CM,
303 MLXSW_SP_CPU_PORT_SB_CM,
304 MLXSW_SP_CPU_PORT_SB_CM,
305 MLXSW_SP_CPU_PORT_SB_CM,
306 MLXSW_SP_CPU_PORT_SB_CM,
307 MLXSW_SP_CPU_PORT_SB_CM,
308 MLXSW_SP_CPU_PORT_SB_CM,
309 MLXSW_SP_CPU_PORT_SB_CM,
310 MLXSW_SP_CPU_PORT_SB_CM,
311 MLXSW_SP_CPU_PORT_SB_CM,
312 MLXSW_SP_CPU_PORT_SB_CM,
313 MLXSW_SP_CPU_PORT_SB_CM,
314 MLXSW_SP_CPU_PORT_SB_CM,
315 MLXSW_SP_CPU_PORT_SB_CM,
316 MLXSW_SP_CPU_PORT_SB_CM,
317 MLXSW_SP_CPU_PORT_SB_CM,
318 MLXSW_SP_CPU_PORT_SB_CM,
319 MLXSW_SP_CPU_PORT_SB_CM,
320 MLXSW_SP_CPU_PORT_SB_CM,
321 MLXSW_SP_CPU_PORT_SB_CM,
322 MLXSW_SP_CPU_PORT_SB_CM,
323 MLXSW_SP_CPU_PORT_SB_CM,
324 MLXSW_SP_CPU_PORT_SB_CM,
325 MLXSW_SP_CPU_PORT_SB_CM,
326 MLXSW_SP_CPU_PORT_SB_CM,
327 MLXSW_SP_CPU_PORT_SB_CM,
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200328};
329
330#define MLXSW_SP_CPU_PORT_SB_MCS_LEN \
331 ARRAY_SIZE(mlxsw_sp_cpu_port_sb_cms)
332
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200333static int __mlxsw_sp_sb_cms_init(struct mlxsw_sp *mlxsw_sp, u8 local_port,
334 enum mlxsw_reg_sbxx_dir dir,
335 const struct mlxsw_sp_sb_cm *cms,
336 size_t cms_len)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200337{
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200338 int i;
339 int err;
340
341 for (i = 0; i < cms_len; i++) {
342 const struct mlxsw_sp_sb_cm *cm;
343
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200344 if (i == 8 && dir == MLXSW_REG_SBXX_DIR_INGRESS)
345 continue; /* PG number 8 does not exist, skip it */
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200346 cm = &cms[i];
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200347 err = mlxsw_sp_sb_cm_write(mlxsw_sp, local_port, i, dir,
348 cm->min_buff, cm->max_buff,
349 cm->pool);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200350 if (err)
351 return err;
352 }
353 return 0;
354}
355
356static int mlxsw_sp_port_sb_cms_init(struct mlxsw_sp_port *mlxsw_sp_port)
357{
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200358 int err;
359
360 err = __mlxsw_sp_sb_cms_init(mlxsw_sp_port->mlxsw_sp,
361 mlxsw_sp_port->local_port,
362 MLXSW_REG_SBXX_DIR_INGRESS,
363 mlxsw_sp_sb_cms_ingress,
364 MLXSW_SP_SB_CMS_INGRESS_LEN);
365 if (err)
366 return err;
367 return __mlxsw_sp_sb_cms_init(mlxsw_sp_port->mlxsw_sp,
368 mlxsw_sp_port->local_port,
369 MLXSW_REG_SBXX_DIR_EGRESS,
370 mlxsw_sp_sb_cms_egress,
371 MLXSW_SP_SB_CMS_EGRESS_LEN);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200372}
373
374static int mlxsw_sp_cpu_port_sb_cms_init(struct mlxsw_sp *mlxsw_sp)
375{
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200376 return __mlxsw_sp_sb_cms_init(mlxsw_sp, 0, MLXSW_REG_SBXX_DIR_EGRESS,
377 mlxsw_sp_cpu_port_sb_cms,
378 MLXSW_SP_CPU_PORT_SB_MCS_LEN);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200379}
380
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200381#define MLXSW_SP_SB_PM(_min_buff, _max_buff) \
382 { \
383 .min_buff = _min_buff, \
384 .max_buff = _max_buff, \
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200385 }
386
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200387static const struct mlxsw_sp_sb_pm mlxsw_sp_sb_pms_ingress[] = {
Jiri Pirkoc30a53c2016-04-14 18:19:22 +0200388 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX),
389 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
390 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
391 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX),
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200392};
393
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200394#define MLXSW_SP_SB_PMS_INGRESS_LEN ARRAY_SIZE(mlxsw_sp_sb_pms_ingress)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200395
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200396static const struct mlxsw_sp_sb_pm mlxsw_sp_sb_pms_egress[] = {
397 MLXSW_SP_SB_PM(0, 7),
Jiri Pirkoc30a53c2016-04-14 18:19:22 +0200398 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
399 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
400 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200401};
402
403#define MLXSW_SP_SB_PMS_EGRESS_LEN ARRAY_SIZE(mlxsw_sp_sb_pms_egress)
404
405static int __mlxsw_sp_port_sb_pms_init(struct mlxsw_sp *mlxsw_sp, u8 local_port,
406 enum mlxsw_reg_sbxx_dir dir,
407 const struct mlxsw_sp_sb_pm *pms,
408 size_t pms_len)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200409{
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200410 int i;
411 int err;
412
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200413 for (i = 0; i < pms_len; i++) {
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200414 const struct mlxsw_sp_sb_pm *pm;
415
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200416 pm = &pms[i];
417 err = mlxsw_sp_sb_pm_write(mlxsw_sp, local_port, i, dir,
Jiri Pirko94266e32016-04-14 18:19:16 +0200418 pm->min_buff, pm->max_buff);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200419 if (err)
420 return err;
421 }
422 return 0;
423}
424
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200425static int mlxsw_sp_port_sb_pms_init(struct mlxsw_sp_port *mlxsw_sp_port)
426{
427 int err;
428
429 err = __mlxsw_sp_port_sb_pms_init(mlxsw_sp_port->mlxsw_sp,
430 mlxsw_sp_port->local_port,
431 MLXSW_REG_SBXX_DIR_INGRESS,
432 mlxsw_sp_sb_pms_ingress,
433 MLXSW_SP_SB_PMS_INGRESS_LEN);
434 if (err)
435 return err;
436 return __mlxsw_sp_port_sb_pms_init(mlxsw_sp_port->mlxsw_sp,
437 mlxsw_sp_port->local_port,
438 MLXSW_REG_SBXX_DIR_EGRESS,
439 mlxsw_sp_sb_pms_egress,
440 MLXSW_SP_SB_PMS_EGRESS_LEN);
441}
442
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200443struct mlxsw_sp_sb_mm {
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200444 u32 min_buff;
445 u32 max_buff;
446 u8 pool;
447};
448
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200449#define MLXSW_SP_SB_MM(_min_buff, _max_buff, _pool) \
450 { \
451 .min_buff = _min_buff, \
452 .max_buff = _max_buff, \
453 .pool = _pool, \
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200454 }
455
456static const struct mlxsw_sp_sb_mm mlxsw_sp_sb_mms[] = {
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200457 MLXSW_SP_SB_MM(MLXSW_SP_BYTES_TO_CELLS(20000), 0xff, 0),
458 MLXSW_SP_SB_MM(MLXSW_SP_BYTES_TO_CELLS(20000), 0xff, 0),
459 MLXSW_SP_SB_MM(MLXSW_SP_BYTES_TO_CELLS(20000), 0xff, 0),
460 MLXSW_SP_SB_MM(MLXSW_SP_BYTES_TO_CELLS(20000), 0xff, 0),
461 MLXSW_SP_SB_MM(MLXSW_SP_BYTES_TO_CELLS(20000), 0xff, 0),
462 MLXSW_SP_SB_MM(MLXSW_SP_BYTES_TO_CELLS(20000), 0xff, 0),
463 MLXSW_SP_SB_MM(MLXSW_SP_BYTES_TO_CELLS(20000), 0xff, 0),
464 MLXSW_SP_SB_MM(MLXSW_SP_BYTES_TO_CELLS(20000), 0xff, 0),
465 MLXSW_SP_SB_MM(MLXSW_SP_BYTES_TO_CELLS(20000), 0xff, 0),
466 MLXSW_SP_SB_MM(MLXSW_SP_BYTES_TO_CELLS(20000), 0xff, 0),
467 MLXSW_SP_SB_MM(MLXSW_SP_BYTES_TO_CELLS(20000), 0xff, 0),
468 MLXSW_SP_SB_MM(MLXSW_SP_BYTES_TO_CELLS(20000), 0xff, 0),
469 MLXSW_SP_SB_MM(MLXSW_SP_BYTES_TO_CELLS(20000), 0xff, 0),
470 MLXSW_SP_SB_MM(MLXSW_SP_BYTES_TO_CELLS(20000), 0xff, 0),
471 MLXSW_SP_SB_MM(MLXSW_SP_BYTES_TO_CELLS(20000), 0xff, 0),
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200472};
473
474#define MLXSW_SP_SB_MMS_LEN ARRAY_SIZE(mlxsw_sp_sb_mms)
475
476static int mlxsw_sp_sb_mms_init(struct mlxsw_sp *mlxsw_sp)
477{
478 char sbmm_pl[MLXSW_REG_SBMM_LEN];
479 int i;
480 int err;
481
482 for (i = 0; i < MLXSW_SP_SB_MMS_LEN; i++) {
483 const struct mlxsw_sp_sb_mm *mc;
484
485 mc = &mlxsw_sp_sb_mms[i];
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200486 mlxsw_reg_sbmm_pack(sbmm_pl, i, mc->min_buff,
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200487 mc->max_buff, mc->pool);
488 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbmm), sbmm_pl);
489 if (err)
490 return err;
491 }
492 return 0;
493}
494
Jiri Pirko0f433fa2016-04-14 18:19:24 +0200495#define MLXSW_SP_SB_SIZE (16 * 1024 * 1024)
496
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200497int mlxsw_sp_buffers_init(struct mlxsw_sp *mlxsw_sp)
498{
499 int err;
500
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200501 err = mlxsw_sp_sb_prs_init(mlxsw_sp);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200502 if (err)
503 return err;
504 err = mlxsw_sp_cpu_port_sb_cms_init(mlxsw_sp);
505 if (err)
506 return err;
507 err = mlxsw_sp_sb_mms_init(mlxsw_sp);
Jiri Pirko0f433fa2016-04-14 18:19:24 +0200508 if (err)
509 return err;
510 return devlink_sb_register(priv_to_devlink(mlxsw_sp->core), 0,
511 MLXSW_SP_SB_SIZE,
512 MLXSW_SP_SB_POOL_COUNT,
513 MLXSW_SP_SB_POOL_COUNT,
514 MLXSW_SP_SB_TC_COUNT,
515 MLXSW_SP_SB_TC_COUNT);
516}
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200517
Jiri Pirko0f433fa2016-04-14 18:19:24 +0200518void mlxsw_sp_buffers_fini(struct mlxsw_sp *mlxsw_sp)
519{
520 devlink_sb_unregister(priv_to_devlink(mlxsw_sp->core), 0);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200521}
522
523int mlxsw_sp_port_buffers_init(struct mlxsw_sp_port *mlxsw_sp_port)
524{
525 int err;
526
Ido Schimmeldd6cb0f2016-04-06 17:10:01 +0200527 err = mlxsw_sp_port_headroom_init(mlxsw_sp_port);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200528 if (err)
529 return err;
530 err = mlxsw_sp_port_sb_cms_init(mlxsw_sp_port);
531 if (err)
532 return err;
533 err = mlxsw_sp_port_sb_pms_init(mlxsw_sp_port);
534
535 return err;
536}
Jiri Pirko0f433fa2016-04-14 18:19:24 +0200537
538static u8 pool_get(u16 pool_index)
539{
540 return pool_index % MLXSW_SP_SB_POOL_COUNT;
541}
542
543static u16 pool_index_get(u8 pool, enum mlxsw_reg_sbxx_dir dir)
544{
545 u16 pool_index;
546
547 pool_index = pool;
548 if (dir == MLXSW_REG_SBXX_DIR_EGRESS)
549 pool_index += MLXSW_SP_SB_POOL_COUNT;
550 return pool_index;
551}
552
553static enum mlxsw_reg_sbxx_dir dir_get(u16 pool_index)
554{
555 return pool_index < MLXSW_SP_SB_POOL_COUNT ?
556 MLXSW_REG_SBXX_DIR_INGRESS : MLXSW_REG_SBXX_DIR_EGRESS;
557}
558
559int mlxsw_sp_sb_pool_get(struct mlxsw_core *mlxsw_core,
560 unsigned int sb_index, u16 pool_index,
561 struct devlink_sb_pool_info *pool_info)
562{
563 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
564 u8 pool = pool_get(pool_index);
565 enum mlxsw_reg_sbxx_dir dir = dir_get(pool_index);
566 struct mlxsw_sp_sb_pr *pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool, dir);
567
568 pool_info->pool_type = dir;
569 pool_info->size = MLXSW_SP_CELLS_TO_BYTES(pr->size);
570 pool_info->threshold_type = pr->mode;
571 return 0;
572}
573
574int mlxsw_sp_sb_pool_set(struct mlxsw_core *mlxsw_core,
575 unsigned int sb_index, u16 pool_index, u32 size,
576 enum devlink_sb_threshold_type threshold_type)
577{
578 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
579 u8 pool = pool_get(pool_index);
580 enum mlxsw_reg_sbxx_dir dir = dir_get(pool_index);
581 enum mlxsw_reg_sbpr_mode mode = threshold_type;
582 u32 pool_size = MLXSW_SP_BYTES_TO_CELLS(size);
583
584 return mlxsw_sp_sb_pr_write(mlxsw_sp, pool, dir, mode, pool_size);
585}
586
587#define MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET (-2) /* 3->1, 16->14 */
588
589static u32 mlxsw_sp_sb_threshold_out(struct mlxsw_sp *mlxsw_sp, u8 pool,
590 enum mlxsw_reg_sbxx_dir dir, u32 max_buff)
591{
592 struct mlxsw_sp_sb_pr *pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool, dir);
593
594 if (pr->mode == MLXSW_REG_SBPR_MODE_DYNAMIC)
595 return max_buff - MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET;
596 return MLXSW_SP_CELLS_TO_BYTES(max_buff);
597}
598
599static int mlxsw_sp_sb_threshold_in(struct mlxsw_sp *mlxsw_sp, u8 pool,
600 enum mlxsw_reg_sbxx_dir dir, u32 threshold,
601 u32 *p_max_buff)
602{
603 struct mlxsw_sp_sb_pr *pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool, dir);
604
605 if (pr->mode == MLXSW_REG_SBPR_MODE_DYNAMIC) {
606 int val;
607
608 val = threshold + MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET;
609 if (val < MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN ||
610 val > MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX)
611 return -EINVAL;
612 *p_max_buff = val;
613 } else {
614 *p_max_buff = MLXSW_SP_BYTES_TO_CELLS(threshold);
615 }
616 return 0;
617}
618
619int mlxsw_sp_sb_port_pool_get(struct mlxsw_core_port *mlxsw_core_port,
620 unsigned int sb_index, u16 pool_index,
621 u32 *p_threshold)
622{
623 struct mlxsw_sp_port *mlxsw_sp_port =
624 mlxsw_core_port_driver_priv(mlxsw_core_port);
625 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
626 u8 local_port = mlxsw_sp_port->local_port;
627 u8 pool = pool_get(pool_index);
628 enum mlxsw_reg_sbxx_dir dir = dir_get(pool_index);
629 struct mlxsw_sp_sb_pm *pm = mlxsw_sp_sb_pm_get(mlxsw_sp, local_port,
630 pool, dir);
631
632 *p_threshold = mlxsw_sp_sb_threshold_out(mlxsw_sp, pool, dir,
633 pm->max_buff);
634 return 0;
635}
636
637int mlxsw_sp_sb_port_pool_set(struct mlxsw_core_port *mlxsw_core_port,
638 unsigned int sb_index, u16 pool_index,
639 u32 threshold)
640{
641 struct mlxsw_sp_port *mlxsw_sp_port =
642 mlxsw_core_port_driver_priv(mlxsw_core_port);
643 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
644 u8 local_port = mlxsw_sp_port->local_port;
645 u8 pool = pool_get(pool_index);
646 enum mlxsw_reg_sbxx_dir dir = dir_get(pool_index);
647 u32 max_buff;
648 int err;
649
650 err = mlxsw_sp_sb_threshold_in(mlxsw_sp, pool, dir,
651 threshold, &max_buff);
652 if (err)
653 return err;
654
655 return mlxsw_sp_sb_pm_write(mlxsw_sp, local_port, pool, dir,
656 0, max_buff);
657}
658
659int mlxsw_sp_sb_tc_pool_bind_get(struct mlxsw_core_port *mlxsw_core_port,
660 unsigned int sb_index, u16 tc_index,
661 enum devlink_sb_pool_type pool_type,
662 u16 *p_pool_index, u32 *p_threshold)
663{
664 struct mlxsw_sp_port *mlxsw_sp_port =
665 mlxsw_core_port_driver_priv(mlxsw_core_port);
666 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
667 u8 local_port = mlxsw_sp_port->local_port;
668 u8 pg_buff = tc_index;
669 enum mlxsw_reg_sbxx_dir dir = pool_type;
670 struct mlxsw_sp_sb_cm *cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port,
671 pg_buff, dir);
672
673 *p_threshold = mlxsw_sp_sb_threshold_out(mlxsw_sp, cm->pool, dir,
674 cm->max_buff);
675 *p_pool_index = pool_index_get(cm->pool, pool_type);
676 return 0;
677}
678
679int mlxsw_sp_sb_tc_pool_bind_set(struct mlxsw_core_port *mlxsw_core_port,
680 unsigned int sb_index, u16 tc_index,
681 enum devlink_sb_pool_type pool_type,
682 u16 pool_index, u32 threshold)
683{
684 struct mlxsw_sp_port *mlxsw_sp_port =
685 mlxsw_core_port_driver_priv(mlxsw_core_port);
686 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
687 u8 local_port = mlxsw_sp_port->local_port;
688 u8 pg_buff = tc_index;
689 enum mlxsw_reg_sbxx_dir dir = pool_type;
690 u8 pool = pool_index;
691 u32 max_buff;
692 int err;
693
694 err = mlxsw_sp_sb_threshold_in(mlxsw_sp, pool, dir,
695 threshold, &max_buff);
696 if (err)
697 return err;
698
699 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS) {
700 if (pool < MLXSW_SP_SB_POOL_COUNT)
701 return -EINVAL;
702 pool -= MLXSW_SP_SB_POOL_COUNT;
703 } else if (pool >= MLXSW_SP_SB_POOL_COUNT) {
704 return -EINVAL;
705 }
706 return mlxsw_sp_sb_cm_write(mlxsw_sp, local_port, pg_buff, dir,
707 0, max_buff, pool);
708}