blob: 639ba5ae8bbd32a4468bd72ff88ebc03ae2e971d [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
Jiri Pirko42a7f1d2016-04-14 18:19:27 +0200116 mlxsw_reg_sbpm_pack(sbpm_pl, local_port, pool, dir, false,
117 min_buff, max_buff);
Jiri Pirko078f9c72016-04-14 18:19:19 +0200118 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbpm), sbpm_pl);
119 if (err)
120 return err;
121
122 pm = mlxsw_sp_sb_pm_get(mlxsw_sp, local_port, pool, dir);
123 pm->min_buff = min_buff;
124 pm->max_buff = max_buff;
125 return 0;
Jiri Pirko94266e32016-04-14 18:19:16 +0200126}
127
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200128static const u16 mlxsw_sp_pbs[] = {
129 2 * MLXSW_SP_BYTES_TO_CELLS(ETH_FRAME_LEN),
130 0,
131 0,
132 0,
133 0,
134 0,
135 0,
136 0,
137 0, /* Unused */
138 2 * MLXSW_SP_BYTES_TO_CELLS(MLXSW_PORT_MAX_MTU),
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200139};
140
141#define MLXSW_SP_PBS_LEN ARRAY_SIZE(mlxsw_sp_pbs)
142
143static int mlxsw_sp_port_pb_init(struct mlxsw_sp_port *mlxsw_sp_port)
144{
145 char pbmc_pl[MLXSW_REG_PBMC_LEN];
146 int i;
147
148 mlxsw_reg_pbmc_pack(pbmc_pl, mlxsw_sp_port->local_port,
149 0xffff, 0xffff / 2);
150 for (i = 0; i < MLXSW_SP_PBS_LEN; i++) {
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200151 if (i == 8)
152 continue;
153 mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, i, mlxsw_sp_pbs[i]);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200154 }
Ido Schimmeld6b7c132016-04-06 17:10:05 +0200155 mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl,
156 MLXSW_REG_PBMC_PORT_SHARED_BUF_IDX, 0);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200157 return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core,
158 MLXSW_REG(pbmc), pbmc_pl);
159}
160
Ido Schimmeldd6cb0f2016-04-06 17:10:01 +0200161static int mlxsw_sp_port_pb_prio_init(struct mlxsw_sp_port *mlxsw_sp_port)
162{
163 char pptb_pl[MLXSW_REG_PPTB_LEN];
164 int i;
165
166 mlxsw_reg_pptb_pack(pptb_pl, mlxsw_sp_port->local_port);
167 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
168 mlxsw_reg_pptb_prio_to_buff_set(pptb_pl, i, 0);
169 return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pptb),
170 pptb_pl);
171}
172
173static int mlxsw_sp_port_headroom_init(struct mlxsw_sp_port *mlxsw_sp_port)
174{
175 int err;
176
177 err = mlxsw_sp_port_pb_init(mlxsw_sp_port);
178 if (err)
179 return err;
180 return mlxsw_sp_port_pb_prio_init(mlxsw_sp_port);
181}
182
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200183#define MLXSW_SP_SB_PR_INGRESS_SIZE \
Ido Schimmel1a198442016-04-06 17:10:02 +0200184 (15000000 - (2 * 20000 * MLXSW_PORT_MAX_PORTS))
Jiri Pirkobc872502016-04-14 18:19:21 +0200185#define MLXSW_SP_SB_PR_INGRESS_MNG_SIZE (200 * 1000)
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200186#define MLXSW_SP_SB_PR_EGRESS_SIZE \
Ido Schimmel1a198442016-04-06 17:10:02 +0200187 (14000000 - (8 * 1500 * MLXSW_PORT_MAX_PORTS))
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200188
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200189#define MLXSW_SP_SB_PR(_mode, _size) \
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200190 { \
191 .mode = _mode, \
192 .size = _size, \
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200193 }
194
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200195static const struct mlxsw_sp_sb_pr mlxsw_sp_sb_prs_ingress[] = {
196 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC,
197 MLXSW_SP_BYTES_TO_CELLS(MLXSW_SP_SB_PR_INGRESS_SIZE)),
198 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
199 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
Jiri Pirkobc872502016-04-14 18:19:21 +0200200 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC,
201 MLXSW_SP_BYTES_TO_CELLS(MLXSW_SP_SB_PR_INGRESS_MNG_SIZE)),
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200202};
203
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200204#define MLXSW_SP_SB_PRS_INGRESS_LEN ARRAY_SIZE(mlxsw_sp_sb_prs_ingress)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200205
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200206static const struct mlxsw_sp_sb_pr mlxsw_sp_sb_prs_egress[] = {
207 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC,
208 MLXSW_SP_BYTES_TO_CELLS(MLXSW_SP_SB_PR_EGRESS_SIZE)),
209 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
210 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
Jiri Pirko5408f7c2016-04-14 18:19:20 +0200211 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200212};
213
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200214#define MLXSW_SP_SB_PRS_EGRESS_LEN ARRAY_SIZE(mlxsw_sp_sb_prs_egress)
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200215
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200216static int __mlxsw_sp_sb_prs_init(struct mlxsw_sp *mlxsw_sp,
217 enum mlxsw_reg_sbxx_dir dir,
218 const struct mlxsw_sp_sb_pr *prs,
219 size_t prs_len)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200220{
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200221 int i;
222 int err;
223
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200224 for (i = 0; i < prs_len; i++) {
225 const struct mlxsw_sp_sb_pr *pr;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200226
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200227 pr = &prs[i];
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200228 err = mlxsw_sp_sb_pr_write(mlxsw_sp, i, dir,
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200229 pr->mode, pr->size);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200230 if (err)
231 return err;
232 }
233 return 0;
234}
235
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200236static int mlxsw_sp_sb_prs_init(struct mlxsw_sp *mlxsw_sp)
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200237{
238 int err;
239
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200240 err = __mlxsw_sp_sb_prs_init(mlxsw_sp, MLXSW_REG_SBXX_DIR_INGRESS,
241 mlxsw_sp_sb_prs_ingress,
242 MLXSW_SP_SB_PRS_INGRESS_LEN);
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200243 if (err)
244 return err;
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200245 return __mlxsw_sp_sb_prs_init(mlxsw_sp, MLXSW_REG_SBXX_DIR_EGRESS,
246 mlxsw_sp_sb_prs_egress,
247 MLXSW_SP_SB_PRS_EGRESS_LEN);
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200248}
249
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200250#define MLXSW_SP_SB_CM(_min_buff, _max_buff, _pool) \
251 { \
252 .min_buff = _min_buff, \
253 .max_buff = _max_buff, \
254 .pool = _pool, \
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200255 }
256
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200257static const struct mlxsw_sp_sb_cm mlxsw_sp_sb_cms_ingress[] = {
258 MLXSW_SP_SB_CM(MLXSW_SP_BYTES_TO_CELLS(10000), 8, 0),
Jiri Pirkoc30a53c2016-04-14 18:19:22 +0200259 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),
265 MLXSW_SP_SB_CM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN, 0),
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200266 MLXSW_SP_SB_CM(0, 0, 0), /* dummy, this PG does not exist */
Jiri Pirkobc872502016-04-14 18:19:21 +0200267 MLXSW_SP_SB_CM(MLXSW_SP_BYTES_TO_CELLS(20000), 1, 3),
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200268};
269
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200270#define MLXSW_SP_SB_CMS_INGRESS_LEN ARRAY_SIZE(mlxsw_sp_sb_cms_ingress)
271
272static const struct mlxsw_sp_sb_cm mlxsw_sp_sb_cms_egress[] = {
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(MLXSW_SP_BYTES_TO_CELLS(1500), 9, 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(0, 0, 0),
289 MLXSW_SP_SB_CM(1, 0xff, 0),
290};
291
292#define MLXSW_SP_SB_CMS_EGRESS_LEN ARRAY_SIZE(mlxsw_sp_sb_cms_egress)
293
Jiri Pirko5408f7c2016-04-14 18:19:20 +0200294#define MLXSW_SP_CPU_PORT_SB_CM MLXSW_SP_SB_CM(0, 0, 0)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200295
296static const struct mlxsw_sp_sb_cm mlxsw_sp_cpu_port_sb_cms[] = {
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200297 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,
328 MLXSW_SP_CPU_PORT_SB_CM,
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200329};
330
331#define MLXSW_SP_CPU_PORT_SB_MCS_LEN \
332 ARRAY_SIZE(mlxsw_sp_cpu_port_sb_cms)
333
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200334static int __mlxsw_sp_sb_cms_init(struct mlxsw_sp *mlxsw_sp, u8 local_port,
335 enum mlxsw_reg_sbxx_dir dir,
336 const struct mlxsw_sp_sb_cm *cms,
337 size_t cms_len)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200338{
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200339 int i;
340 int err;
341
342 for (i = 0; i < cms_len; i++) {
343 const struct mlxsw_sp_sb_cm *cm;
344
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200345 if (i == 8 && dir == MLXSW_REG_SBXX_DIR_INGRESS)
346 continue; /* PG number 8 does not exist, skip it */
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200347 cm = &cms[i];
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200348 err = mlxsw_sp_sb_cm_write(mlxsw_sp, local_port, i, dir,
349 cm->min_buff, cm->max_buff,
350 cm->pool);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200351 if (err)
352 return err;
353 }
354 return 0;
355}
356
357static int mlxsw_sp_port_sb_cms_init(struct mlxsw_sp_port *mlxsw_sp_port)
358{
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200359 int err;
360
361 err = __mlxsw_sp_sb_cms_init(mlxsw_sp_port->mlxsw_sp,
362 mlxsw_sp_port->local_port,
363 MLXSW_REG_SBXX_DIR_INGRESS,
364 mlxsw_sp_sb_cms_ingress,
365 MLXSW_SP_SB_CMS_INGRESS_LEN);
366 if (err)
367 return err;
368 return __mlxsw_sp_sb_cms_init(mlxsw_sp_port->mlxsw_sp,
369 mlxsw_sp_port->local_port,
370 MLXSW_REG_SBXX_DIR_EGRESS,
371 mlxsw_sp_sb_cms_egress,
372 MLXSW_SP_SB_CMS_EGRESS_LEN);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200373}
374
375static int mlxsw_sp_cpu_port_sb_cms_init(struct mlxsw_sp *mlxsw_sp)
376{
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200377 return __mlxsw_sp_sb_cms_init(mlxsw_sp, 0, MLXSW_REG_SBXX_DIR_EGRESS,
378 mlxsw_sp_cpu_port_sb_cms,
379 MLXSW_SP_CPU_PORT_SB_MCS_LEN);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200380}
381
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200382#define MLXSW_SP_SB_PM(_min_buff, _max_buff) \
383 { \
384 .min_buff = _min_buff, \
385 .max_buff = _max_buff, \
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200386 }
387
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200388static const struct mlxsw_sp_sb_pm mlxsw_sp_sb_pms_ingress[] = {
Jiri Pirkoc30a53c2016-04-14 18:19:22 +0200389 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX),
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_MIN),
392 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX),
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200393};
394
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200395#define MLXSW_SP_SB_PMS_INGRESS_LEN ARRAY_SIZE(mlxsw_sp_sb_pms_ingress)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200396
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200397static const struct mlxsw_sp_sb_pm mlxsw_sp_sb_pms_egress[] = {
398 MLXSW_SP_SB_PM(0, 7),
Jiri Pirkoc30a53c2016-04-14 18:19:22 +0200399 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),
401 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200402};
403
404#define MLXSW_SP_SB_PMS_EGRESS_LEN ARRAY_SIZE(mlxsw_sp_sb_pms_egress)
405
406static int __mlxsw_sp_port_sb_pms_init(struct mlxsw_sp *mlxsw_sp, u8 local_port,
407 enum mlxsw_reg_sbxx_dir dir,
408 const struct mlxsw_sp_sb_pm *pms,
409 size_t pms_len)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200410{
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200411 int i;
412 int err;
413
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200414 for (i = 0; i < pms_len; i++) {
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200415 const struct mlxsw_sp_sb_pm *pm;
416
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200417 pm = &pms[i];
418 err = mlxsw_sp_sb_pm_write(mlxsw_sp, local_port, i, dir,
Jiri Pirko94266e32016-04-14 18:19:16 +0200419 pm->min_buff, pm->max_buff);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200420 if (err)
421 return err;
422 }
423 return 0;
424}
425
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200426static int mlxsw_sp_port_sb_pms_init(struct mlxsw_sp_port *mlxsw_sp_port)
427{
428 int err;
429
430 err = __mlxsw_sp_port_sb_pms_init(mlxsw_sp_port->mlxsw_sp,
431 mlxsw_sp_port->local_port,
432 MLXSW_REG_SBXX_DIR_INGRESS,
433 mlxsw_sp_sb_pms_ingress,
434 MLXSW_SP_SB_PMS_INGRESS_LEN);
435 if (err)
436 return err;
437 return __mlxsw_sp_port_sb_pms_init(mlxsw_sp_port->mlxsw_sp,
438 mlxsw_sp_port->local_port,
439 MLXSW_REG_SBXX_DIR_EGRESS,
440 mlxsw_sp_sb_pms_egress,
441 MLXSW_SP_SB_PMS_EGRESS_LEN);
442}
443
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200444struct mlxsw_sp_sb_mm {
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200445 u32 min_buff;
446 u32 max_buff;
447 u8 pool;
448};
449
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200450#define MLXSW_SP_SB_MM(_min_buff, _max_buff, _pool) \
451 { \
452 .min_buff = _min_buff, \
453 .max_buff = _max_buff, \
454 .pool = _pool, \
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200455 }
456
457static const struct mlxsw_sp_sb_mm mlxsw_sp_sb_mms[] = {
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200458 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),
472 MLXSW_SP_SB_MM(MLXSW_SP_BYTES_TO_CELLS(20000), 0xff, 0),
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200473};
474
475#define MLXSW_SP_SB_MMS_LEN ARRAY_SIZE(mlxsw_sp_sb_mms)
476
477static int mlxsw_sp_sb_mms_init(struct mlxsw_sp *mlxsw_sp)
478{
479 char sbmm_pl[MLXSW_REG_SBMM_LEN];
480 int i;
481 int err;
482
483 for (i = 0; i < MLXSW_SP_SB_MMS_LEN; i++) {
484 const struct mlxsw_sp_sb_mm *mc;
485
486 mc = &mlxsw_sp_sb_mms[i];
Jiri Pirkob11c3b42016-04-14 18:19:17 +0200487 mlxsw_reg_sbmm_pack(sbmm_pl, i, mc->min_buff,
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200488 mc->max_buff, mc->pool);
489 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbmm), sbmm_pl);
490 if (err)
491 return err;
492 }
493 return 0;
494}
495
Jiri Pirko0f433fa2016-04-14 18:19:24 +0200496#define MLXSW_SP_SB_SIZE (16 * 1024 * 1024)
497
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200498int mlxsw_sp_buffers_init(struct mlxsw_sp *mlxsw_sp)
499{
500 int err;
501
Jiri Pirkoaa99bc72016-04-14 18:19:18 +0200502 err = mlxsw_sp_sb_prs_init(mlxsw_sp);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200503 if (err)
504 return err;
505 err = mlxsw_sp_cpu_port_sb_cms_init(mlxsw_sp);
506 if (err)
507 return err;
508 err = mlxsw_sp_sb_mms_init(mlxsw_sp);
Jiri Pirko0f433fa2016-04-14 18:19:24 +0200509 if (err)
510 return err;
511 return devlink_sb_register(priv_to_devlink(mlxsw_sp->core), 0,
512 MLXSW_SP_SB_SIZE,
513 MLXSW_SP_SB_POOL_COUNT,
514 MLXSW_SP_SB_POOL_COUNT,
515 MLXSW_SP_SB_TC_COUNT,
516 MLXSW_SP_SB_TC_COUNT);
517}
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200518
Jiri Pirko0f433fa2016-04-14 18:19:24 +0200519void mlxsw_sp_buffers_fini(struct mlxsw_sp *mlxsw_sp)
520{
521 devlink_sb_unregister(priv_to_devlink(mlxsw_sp->core), 0);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200522}
523
524int mlxsw_sp_port_buffers_init(struct mlxsw_sp_port *mlxsw_sp_port)
525{
526 int err;
527
Ido Schimmeldd6cb0f2016-04-06 17:10:01 +0200528 err = mlxsw_sp_port_headroom_init(mlxsw_sp_port);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200529 if (err)
530 return err;
531 err = mlxsw_sp_port_sb_cms_init(mlxsw_sp_port);
532 if (err)
533 return err;
534 err = mlxsw_sp_port_sb_pms_init(mlxsw_sp_port);
535
536 return err;
537}
Jiri Pirko0f433fa2016-04-14 18:19:24 +0200538
539static u8 pool_get(u16 pool_index)
540{
541 return pool_index % MLXSW_SP_SB_POOL_COUNT;
542}
543
544static u16 pool_index_get(u8 pool, enum mlxsw_reg_sbxx_dir dir)
545{
546 u16 pool_index;
547
548 pool_index = pool;
549 if (dir == MLXSW_REG_SBXX_DIR_EGRESS)
550 pool_index += MLXSW_SP_SB_POOL_COUNT;
551 return pool_index;
552}
553
554static enum mlxsw_reg_sbxx_dir dir_get(u16 pool_index)
555{
556 return pool_index < MLXSW_SP_SB_POOL_COUNT ?
557 MLXSW_REG_SBXX_DIR_INGRESS : MLXSW_REG_SBXX_DIR_EGRESS;
558}
559
560int mlxsw_sp_sb_pool_get(struct mlxsw_core *mlxsw_core,
561 unsigned int sb_index, u16 pool_index,
562 struct devlink_sb_pool_info *pool_info)
563{
564 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
565 u8 pool = pool_get(pool_index);
566 enum mlxsw_reg_sbxx_dir dir = dir_get(pool_index);
567 struct mlxsw_sp_sb_pr *pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool, dir);
568
569 pool_info->pool_type = dir;
570 pool_info->size = MLXSW_SP_CELLS_TO_BYTES(pr->size);
571 pool_info->threshold_type = pr->mode;
572 return 0;
573}
574
575int mlxsw_sp_sb_pool_set(struct mlxsw_core *mlxsw_core,
576 unsigned int sb_index, u16 pool_index, u32 size,
577 enum devlink_sb_threshold_type threshold_type)
578{
579 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
580 u8 pool = pool_get(pool_index);
581 enum mlxsw_reg_sbxx_dir dir = dir_get(pool_index);
582 enum mlxsw_reg_sbpr_mode mode = threshold_type;
583 u32 pool_size = MLXSW_SP_BYTES_TO_CELLS(size);
584
585 return mlxsw_sp_sb_pr_write(mlxsw_sp, pool, dir, mode, pool_size);
586}
587
588#define MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET (-2) /* 3->1, 16->14 */
589
590static u32 mlxsw_sp_sb_threshold_out(struct mlxsw_sp *mlxsw_sp, u8 pool,
591 enum mlxsw_reg_sbxx_dir dir, u32 max_buff)
592{
593 struct mlxsw_sp_sb_pr *pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool, dir);
594
595 if (pr->mode == MLXSW_REG_SBPR_MODE_DYNAMIC)
596 return max_buff - MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET;
597 return MLXSW_SP_CELLS_TO_BYTES(max_buff);
598}
599
600static int mlxsw_sp_sb_threshold_in(struct mlxsw_sp *mlxsw_sp, u8 pool,
601 enum mlxsw_reg_sbxx_dir dir, u32 threshold,
602 u32 *p_max_buff)
603{
604 struct mlxsw_sp_sb_pr *pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool, dir);
605
606 if (pr->mode == MLXSW_REG_SBPR_MODE_DYNAMIC) {
607 int val;
608
609 val = threshold + MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET;
610 if (val < MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN ||
611 val > MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX)
612 return -EINVAL;
613 *p_max_buff = val;
614 } else {
615 *p_max_buff = MLXSW_SP_BYTES_TO_CELLS(threshold);
616 }
617 return 0;
618}
619
620int mlxsw_sp_sb_port_pool_get(struct mlxsw_core_port *mlxsw_core_port,
621 unsigned int sb_index, u16 pool_index,
622 u32 *p_threshold)
623{
624 struct mlxsw_sp_port *mlxsw_sp_port =
625 mlxsw_core_port_driver_priv(mlxsw_core_port);
626 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
627 u8 local_port = mlxsw_sp_port->local_port;
628 u8 pool = pool_get(pool_index);
629 enum mlxsw_reg_sbxx_dir dir = dir_get(pool_index);
630 struct mlxsw_sp_sb_pm *pm = mlxsw_sp_sb_pm_get(mlxsw_sp, local_port,
631 pool, dir);
632
633 *p_threshold = mlxsw_sp_sb_threshold_out(mlxsw_sp, pool, dir,
634 pm->max_buff);
635 return 0;
636}
637
638int mlxsw_sp_sb_port_pool_set(struct mlxsw_core_port *mlxsw_core_port,
639 unsigned int sb_index, u16 pool_index,
640 u32 threshold)
641{
642 struct mlxsw_sp_port *mlxsw_sp_port =
643 mlxsw_core_port_driver_priv(mlxsw_core_port);
644 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
645 u8 local_port = mlxsw_sp_port->local_port;
646 u8 pool = pool_get(pool_index);
647 enum mlxsw_reg_sbxx_dir dir = dir_get(pool_index);
648 u32 max_buff;
649 int err;
650
651 err = mlxsw_sp_sb_threshold_in(mlxsw_sp, pool, dir,
652 threshold, &max_buff);
653 if (err)
654 return err;
655
656 return mlxsw_sp_sb_pm_write(mlxsw_sp, local_port, pool, dir,
657 0, max_buff);
658}
659
660int mlxsw_sp_sb_tc_pool_bind_get(struct mlxsw_core_port *mlxsw_core_port,
661 unsigned int sb_index, u16 tc_index,
662 enum devlink_sb_pool_type pool_type,
663 u16 *p_pool_index, u32 *p_threshold)
664{
665 struct mlxsw_sp_port *mlxsw_sp_port =
666 mlxsw_core_port_driver_priv(mlxsw_core_port);
667 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
668 u8 local_port = mlxsw_sp_port->local_port;
669 u8 pg_buff = tc_index;
670 enum mlxsw_reg_sbxx_dir dir = pool_type;
671 struct mlxsw_sp_sb_cm *cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port,
672 pg_buff, dir);
673
674 *p_threshold = mlxsw_sp_sb_threshold_out(mlxsw_sp, cm->pool, dir,
675 cm->max_buff);
676 *p_pool_index = pool_index_get(cm->pool, pool_type);
677 return 0;
678}
679
680int mlxsw_sp_sb_tc_pool_bind_set(struct mlxsw_core_port *mlxsw_core_port,
681 unsigned int sb_index, u16 tc_index,
682 enum devlink_sb_pool_type pool_type,
683 u16 pool_index, u32 threshold)
684{
685 struct mlxsw_sp_port *mlxsw_sp_port =
686 mlxsw_core_port_driver_priv(mlxsw_core_port);
687 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
688 u8 local_port = mlxsw_sp_port->local_port;
689 u8 pg_buff = tc_index;
690 enum mlxsw_reg_sbxx_dir dir = pool_type;
691 u8 pool = pool_index;
692 u32 max_buff;
693 int err;
694
695 err = mlxsw_sp_sb_threshold_in(mlxsw_sp, pool, dir,
696 threshold, &max_buff);
697 if (err)
698 return err;
699
700 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS) {
701 if (pool < MLXSW_SP_SB_POOL_COUNT)
702 return -EINVAL;
703 pool -= MLXSW_SP_SB_POOL_COUNT;
704 } else if (pool >= MLXSW_SP_SB_POOL_COUNT) {
705 return -EINVAL;
706 }
707 return mlxsw_sp_sb_cm_write(mlxsw_sp, local_port, pg_buff, dir,
708 0, max_buff, pool);
709}