spectrum_buffers.c 29.0 KB
Newer Older
1 2
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
/* Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved */
3 4 5

#include <linux/kernel.h>
#include <linux/types.h>
6
#include <linux/dcbnl.h>
7
#include <linux/if_ether.h>
8
#include <linux/list.h>
9 10 11 12 13 14

#include "spectrum.h"
#include "core.h"
#include "port.h"
#include "reg.h"

15 16 17 18 19 20 21 22 23 24 25 26 27
struct mlxsw_sp_sb_pr {
	enum mlxsw_reg_sbpr_mode mode;
	u32 size;
};

struct mlxsw_cp_sb_occ {
	u32 cur;
	u32 max;
};

struct mlxsw_sp_sb_cm {
	u32 min_buff;
	u32 max_buff;
28
	u16 pool_index;
29 30 31
	struct mlxsw_cp_sb_occ occ;
};

32 33
#define MLXSW_SP_SB_INFI -1U

34 35 36 37 38 39
struct mlxsw_sp_sb_pm {
	u32 min_buff;
	u32 max_buff;
	struct mlxsw_cp_sb_occ occ;
};

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
struct mlxsw_sp_sb_pool_des {
	enum mlxsw_reg_sbxx_dir dir;
	u8 pool;
};

/* Order ingress pools before egress pools. */
static const struct mlxsw_sp_sb_pool_des mlxsw_sp_sb_pool_dess[] = {
	{MLXSW_REG_SBXX_DIR_INGRESS, 0},
	{MLXSW_REG_SBXX_DIR_INGRESS, 1},
	{MLXSW_REG_SBXX_DIR_INGRESS, 2},
	{MLXSW_REG_SBXX_DIR_INGRESS, 3},
	{MLXSW_REG_SBXX_DIR_EGRESS, 0},
	{MLXSW_REG_SBXX_DIR_EGRESS, 1},
	{MLXSW_REG_SBXX_DIR_EGRESS, 2},
	{MLXSW_REG_SBXX_DIR_EGRESS, 3},
	{MLXSW_REG_SBXX_DIR_EGRESS, 15},
};

#define MLXSW_SP_SB_POOL_DESS_LEN ARRAY_SIZE(mlxsw_sp_sb_pool_dess)

60 61
#define MLXSW_SP_SB_ING_TC_COUNT 8
#define MLXSW_SP_SB_EG_TC_COUNT 16
62 63

struct mlxsw_sp_sb_port {
64 65
	struct mlxsw_sp_sb_cm ing_cms[MLXSW_SP_SB_ING_TC_COUNT];
	struct mlxsw_sp_sb_cm eg_cms[MLXSW_SP_SB_EG_TC_COUNT];
66
	struct mlxsw_sp_sb_pm pms[MLXSW_SP_SB_POOL_DESS_LEN];
67 68 69
};

struct mlxsw_sp_sb {
70
	struct mlxsw_sp_sb_pr prs[MLXSW_SP_SB_POOL_DESS_LEN];
71 72
	struct mlxsw_sp_sb_port *ports;
	u32 cell_size;
73
	u64 sb_size;
74 75
};

76 77 78
struct mlxsw_sp_sb_vals {
};

79 80 81 82 83 84 85 86 87 88
u32 mlxsw_sp_cells_bytes(const struct mlxsw_sp *mlxsw_sp, u32 cells)
{
	return mlxsw_sp->sb->cell_size * cells;
}

u32 mlxsw_sp_bytes_cells(const struct mlxsw_sp *mlxsw_sp, u32 bytes)
{
	return DIV_ROUND_UP(bytes, mlxsw_sp->sb->cell_size);
}

89
static struct mlxsw_sp_sb_pr *mlxsw_sp_sb_pr_get(struct mlxsw_sp *mlxsw_sp,
90
						 u16 pool_index)
91
{
92
	return &mlxsw_sp->sb->prs[pool_index];
93 94
}

95 96 97 98 99 100 101 102
static bool mlxsw_sp_sb_cm_exists(u8 pg_buff, enum mlxsw_reg_sbxx_dir dir)
{
	if (dir == MLXSW_REG_SBXX_DIR_INGRESS)
		return pg_buff < MLXSW_SP_SB_ING_TC_COUNT;
	else
		return pg_buff < MLXSW_SP_SB_EG_TC_COUNT;
}

103 104 105 106
static struct mlxsw_sp_sb_cm *mlxsw_sp_sb_cm_get(struct mlxsw_sp *mlxsw_sp,
						 u8 local_port, u8 pg_buff,
						 enum mlxsw_reg_sbxx_dir dir)
{
107 108 109 110 111 112 113
	struct mlxsw_sp_sb_port *sb_port = &mlxsw_sp->sb->ports[local_port];

	WARN_ON(!mlxsw_sp_sb_cm_exists(pg_buff, dir));
	if (dir == MLXSW_REG_SBXX_DIR_INGRESS)
		return &sb_port->ing_cms[pg_buff];
	else
		return &sb_port->eg_cms[pg_buff];
114 115 116
}

static struct mlxsw_sp_sb_pm *mlxsw_sp_sb_pm_get(struct mlxsw_sp *mlxsw_sp,
117
						 u8 local_port, u16 pool_index)
118
{
119
	return &mlxsw_sp->sb->ports[local_port].pms[pool_index];
120 121
}

122
static int mlxsw_sp_sb_pr_write(struct mlxsw_sp *mlxsw_sp, u16 pool_index,
123 124
				enum mlxsw_reg_sbpr_mode mode,
				u32 size, bool infi_size)
125
{
126 127
	const struct mlxsw_sp_sb_pool_des *des =
		&mlxsw_sp_sb_pool_dess[pool_index];
128
	char sbpr_pl[MLXSW_REG_SBPR_LEN];
129 130
	struct mlxsw_sp_sb_pr *pr;
	int err;
131

132 133
	mlxsw_reg_sbpr_pack(sbpr_pl, des->pool, des->dir, mode,
			    size, infi_size);
134 135 136 137
	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbpr), sbpr_pl);
	if (err)
		return err;

138 139
	if (infi_size)
		size = mlxsw_sp_bytes_cells(mlxsw_sp, mlxsw_sp->sb->sb_size);
140
	pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool_index);
141 142 143
	pr->mode = mode;
	pr->size = size;
	return 0;
144 145 146
}

static int mlxsw_sp_sb_cm_write(struct mlxsw_sp *mlxsw_sp, u8 local_port,
147
				u8 pg_buff, u32 min_buff, u32 max_buff,
148
				bool infi_max, u16 pool_index)
149
{
150 151
	const struct mlxsw_sp_sb_pool_des *des =
		&mlxsw_sp_sb_pool_dess[pool_index];
152
	char sbcm_pl[MLXSW_REG_SBCM_LEN];
153
	struct mlxsw_sp_sb_cm *cm;
154
	int err;
155

156
	mlxsw_reg_sbcm_pack(sbcm_pl, local_port, pg_buff, des->dir,
157
			    min_buff, max_buff, infi_max, des->pool);
158 159 160
	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbcm), sbcm_pl);
	if (err)
		return err;
161

162
	if (mlxsw_sp_sb_cm_exists(pg_buff, des->dir)) {
163 164 165
		if (infi_max)
			max_buff = mlxsw_sp_bytes_cells(mlxsw_sp,
							mlxsw_sp->sb->sb_size);
166

167 168
		cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port, pg_buff,
					des->dir);
169 170
		cm->min_buff = min_buff;
		cm->max_buff = max_buff;
171
		cm->pool_index = pool_index;
172 173
	}
	return 0;
174 175 176
}

static int mlxsw_sp_sb_pm_write(struct mlxsw_sp *mlxsw_sp, u8 local_port,
177
				u16 pool_index, u32 min_buff, u32 max_buff)
178
{
179 180
	const struct mlxsw_sp_sb_pool_des *des =
		&mlxsw_sp_sb_pool_dess[pool_index];
181
	char sbpm_pl[MLXSW_REG_SBPM_LEN];
182 183
	struct mlxsw_sp_sb_pm *pm;
	int err;
184

185
	mlxsw_reg_sbpm_pack(sbpm_pl, local_port, des->pool, des->dir, false,
186
			    min_buff, max_buff);
187 188 189 190
	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbpm), sbpm_pl);
	if (err)
		return err;

191
	pm = mlxsw_sp_sb_pm_get(mlxsw_sp, local_port, pool_index);
192 193 194
	pm->min_buff = min_buff;
	pm->max_buff = max_buff;
	return 0;
195 196
}

197
static int mlxsw_sp_sb_pm_occ_clear(struct mlxsw_sp *mlxsw_sp, u8 local_port,
198
				    u16 pool_index, struct list_head *bulk_list)
199
{
200 201
	const struct mlxsw_sp_sb_pool_des *des =
		&mlxsw_sp_sb_pool_dess[pool_index];
202 203
	char sbpm_pl[MLXSW_REG_SBPM_LEN];

204 205
	mlxsw_reg_sbpm_pack(sbpm_pl, local_port, des->pool, des->dir,
			    true, 0, 0);
206 207 208 209 210 211 212 213 214 215 216 217 218 219
	return mlxsw_reg_trans_query(mlxsw_sp->core, MLXSW_REG(sbpm), sbpm_pl,
				     bulk_list, NULL, 0);
}

static void mlxsw_sp_sb_pm_occ_query_cb(struct mlxsw_core *mlxsw_core,
					char *sbpm_pl, size_t sbpm_pl_len,
					unsigned long cb_priv)
{
	struct mlxsw_sp_sb_pm *pm = (struct mlxsw_sp_sb_pm *) cb_priv;

	mlxsw_reg_sbpm_unpack(sbpm_pl, &pm->occ.cur, &pm->occ.max);
}

static int mlxsw_sp_sb_pm_occ_query(struct mlxsw_sp *mlxsw_sp, u8 local_port,
220
				    u16 pool_index, struct list_head *bulk_list)
221
{
222 223
	const struct mlxsw_sp_sb_pool_des *des =
		&mlxsw_sp_sb_pool_dess[pool_index];
224 225 226
	char sbpm_pl[MLXSW_REG_SBPM_LEN];
	struct mlxsw_sp_sb_pm *pm;

227 228 229
	pm = mlxsw_sp_sb_pm_get(mlxsw_sp, local_port, pool_index);
	mlxsw_reg_sbpm_pack(sbpm_pl, local_port, des->pool, des->dir,
			    false, 0, 0);
230 231 232 233 234 235
	return mlxsw_reg_trans_query(mlxsw_sp->core, MLXSW_REG(sbpm), sbpm_pl,
				     bulk_list,
				     mlxsw_sp_sb_pm_occ_query_cb,
				     (unsigned long) pm);
}

236
static const u16 mlxsw_sp_pbs[] = {
237 238
	[0] = 2 * ETH_FRAME_LEN,
	[9] = 2 * MLXSW_PORT_MAX_MTU,
239 240 241
};

#define MLXSW_SP_PBS_LEN ARRAY_SIZE(mlxsw_sp_pbs)
242
#define MLXSW_SP_PB_UNUSED 8
243 244 245

static int mlxsw_sp_port_pb_init(struct mlxsw_sp_port *mlxsw_sp_port)
{
246
	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
247 248 249 250 251 252
	char pbmc_pl[MLXSW_REG_PBMC_LEN];
	int i;

	mlxsw_reg_pbmc_pack(pbmc_pl, mlxsw_sp_port->local_port,
			    0xffff, 0xffff / 2);
	for (i = 0; i < MLXSW_SP_PBS_LEN; i++) {
253 254
		u16 size = mlxsw_sp_bytes_cells(mlxsw_sp, mlxsw_sp_pbs[i]);

255
		if (i == MLXSW_SP_PB_UNUSED)
256
			continue;
257
		mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, i, size);
258
	}
259 260
	mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl,
					 MLXSW_REG_PBMC_PORT_SHARED_BUF_IDX, 0);
261
	return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
262 263
}

264 265 266 267 268 269 270
static int mlxsw_sp_port_pb_prio_init(struct mlxsw_sp_port *mlxsw_sp_port)
{
	char pptb_pl[MLXSW_REG_PPTB_LEN];
	int i;

	mlxsw_reg_pptb_pack(pptb_pl, mlxsw_sp_port->local_port);
	for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
271
		mlxsw_reg_pptb_prio_to_buff_pack(pptb_pl, i, 0);
272 273 274 275 276 277 278 279 280 281 282 283 284 285
	return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pptb),
			       pptb_pl);
}

static int mlxsw_sp_port_headroom_init(struct mlxsw_sp_port *mlxsw_sp_port)
{
	int err;

	err = mlxsw_sp_port_pb_init(mlxsw_sp_port);
	if (err)
		return err;
	return mlxsw_sp_port_pb_prio_init(mlxsw_sp_port);
}

286 287 288 289
static int mlxsw_sp_sb_ports_init(struct mlxsw_sp *mlxsw_sp)
{
	unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);

290 291 292 293
	mlxsw_sp->sb->ports = kcalloc(max_ports,
				      sizeof(struct mlxsw_sp_sb_port),
				      GFP_KERNEL);
	if (!mlxsw_sp->sb->ports)
294 295 296 297 298 299
		return -ENOMEM;
	return 0;
}

static void mlxsw_sp_sb_ports_fini(struct mlxsw_sp *mlxsw_sp)
{
300
	kfree(mlxsw_sp->sb->ports);
301 302 303
}

#define MLXSW_SP_SB_PR_INGRESS_SIZE	12440000
304
#define MLXSW_SP_SB_PR_INGRESS_MNG_SIZE (200 * 1000)
305
#define MLXSW_SP_SB_PR_EGRESS_SIZE	13232000
306

307
#define MLXSW_SP_SB_PR(_mode, _size)	\
308 309 310
	{				\
		.mode = _mode,		\
		.size = _size,		\
311 312
	}

313 314
static const struct mlxsw_sp_sb_pr mlxsw_sp_sb_prs[] = {
	/* Ingress pools. */
315
	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC,
316
		       MLXSW_SP_SB_PR_INGRESS_SIZE),
317 318
	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
319
	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC,
320
		       MLXSW_SP_SB_PR_INGRESS_MNG_SIZE),
321
	/* Egress pools. */
322
	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, MLXSW_SP_SB_PR_EGRESS_SIZE),
323 324
	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
325
	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
326
	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC, MLXSW_SP_SB_INFI),
327 328
};

329
#define MLXSW_SP_SB_PRS_LEN ARRAY_SIZE(mlxsw_sp_sb_prs)
330

331 332 333
static int mlxsw_sp_sb_prs_init(struct mlxsw_sp *mlxsw_sp,
				const struct mlxsw_sp_sb_pr *prs,
				size_t prs_len)
334 335 336 337
{
	int i;
	int err;

338
	for (i = 0; i < prs_len; i++) {
339 340 341 342 343 344 345 346 347 348 349
		u32 size = prs[i].size;
		u32 size_cells;

		if (size == MLXSW_SP_SB_INFI) {
			err = mlxsw_sp_sb_pr_write(mlxsw_sp, i, prs[i].mode,
						   0, true);
		} else {
			size_cells = mlxsw_sp_bytes_cells(mlxsw_sp, size);
			err = mlxsw_sp_sb_pr_write(mlxsw_sp, i, prs[i].mode,
						   size_cells, false);
		}
350 351 352 353 354 355
		if (err)
			return err;
	}
	return 0;
}

356 357 358 359
#define MLXSW_SP_SB_CM(_min_buff, _max_buff, _pool)	\
	{						\
		.min_buff = _min_buff,			\
		.max_buff = _max_buff,			\
360
		.pool_index = _pool,			\
361 362
	}

363
static const struct mlxsw_sp_sb_cm mlxsw_sp_sb_cms_ingress[] = {
364
	MLXSW_SP_SB_CM(10000, 8, 0),
365 366 367 368 369 370 371
	MLXSW_SP_SB_CM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN, 0),
	MLXSW_SP_SB_CM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN, 0),
	MLXSW_SP_SB_CM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN, 0),
	MLXSW_SP_SB_CM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN, 0),
	MLXSW_SP_SB_CM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN, 0),
	MLXSW_SP_SB_CM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN, 0),
	MLXSW_SP_SB_CM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN, 0),
372
	MLXSW_SP_SB_CM(0, 0, 0), /* dummy, this PG does not exist */
373
	MLXSW_SP_SB_CM(20000, 1, 3),
374 375
};

376 377 378
#define MLXSW_SP_SB_CMS_INGRESS_LEN ARRAY_SIZE(mlxsw_sp_sb_cms_ingress)

static const struct mlxsw_sp_sb_cm mlxsw_sp_sb_cms_egress[] = {
379 380 381 382 383 384 385 386
	MLXSW_SP_SB_CM(1500, 9, 4),
	MLXSW_SP_SB_CM(1500, 9, 4),
	MLXSW_SP_SB_CM(1500, 9, 4),
	MLXSW_SP_SB_CM(1500, 9, 4),
	MLXSW_SP_SB_CM(1500, 9, 4),
	MLXSW_SP_SB_CM(1500, 9, 4),
	MLXSW_SP_SB_CM(1500, 9, 4),
	MLXSW_SP_SB_CM(1500, 9, 4),
387 388 389 390 391 392 393 394
	MLXSW_SP_SB_CM(0, MLXSW_SP_SB_INFI, 8),
	MLXSW_SP_SB_CM(0, MLXSW_SP_SB_INFI, 8),
	MLXSW_SP_SB_CM(0, MLXSW_SP_SB_INFI, 8),
	MLXSW_SP_SB_CM(0, MLXSW_SP_SB_INFI, 8),
	MLXSW_SP_SB_CM(0, MLXSW_SP_SB_INFI, 8),
	MLXSW_SP_SB_CM(0, MLXSW_SP_SB_INFI, 8),
	MLXSW_SP_SB_CM(0, MLXSW_SP_SB_INFI, 8),
	MLXSW_SP_SB_CM(0, MLXSW_SP_SB_INFI, 8),
395
	MLXSW_SP_SB_CM(1, 0xff, 4),
396 397 398 399
};

#define MLXSW_SP_SB_CMS_EGRESS_LEN ARRAY_SIZE(mlxsw_sp_sb_cms_egress)

400
#define MLXSW_SP_CPU_PORT_SB_CM MLXSW_SP_SB_CM(0, 0, 4)
401 402

static const struct mlxsw_sp_sb_cm mlxsw_sp_cpu_port_sb_cms[] = {
403
	MLXSW_SP_CPU_PORT_SB_CM,
404 405 406 407 408
	MLXSW_SP_SB_CM(MLXSW_PORT_MAX_MTU, 0, 4),
	MLXSW_SP_SB_CM(MLXSW_PORT_MAX_MTU, 0, 4),
	MLXSW_SP_SB_CM(MLXSW_PORT_MAX_MTU, 0, 4),
	MLXSW_SP_SB_CM(MLXSW_PORT_MAX_MTU, 0, 4),
	MLXSW_SP_SB_CM(MLXSW_PORT_MAX_MTU, 0, 4),
409
	MLXSW_SP_CPU_PORT_SB_CM,
410
	MLXSW_SP_SB_CM(MLXSW_PORT_MAX_MTU, 0, 4),
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
	MLXSW_SP_CPU_PORT_SB_CM,
435 436 437 438 439
};

#define MLXSW_SP_CPU_PORT_SB_MCS_LEN \
	ARRAY_SIZE(mlxsw_sp_cpu_port_sb_cms)

440 441 442 443 444 445 446 447
static bool
mlxsw_sp_sb_pool_is_static(struct mlxsw_sp *mlxsw_sp, u16 pool_index)
{
	struct mlxsw_sp_sb_pr *pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool_index);

	return pr->mode == MLXSW_REG_SBPR_MODE_STATIC;
}

448 449 450 451
static int __mlxsw_sp_sb_cms_init(struct mlxsw_sp *mlxsw_sp, u8 local_port,
				  enum mlxsw_reg_sbxx_dir dir,
				  const struct mlxsw_sp_sb_cm *cms,
				  size_t cms_len)
452 453 454 455 456 457
{
	int i;
	int err;

	for (i = 0; i < cms_len; i++) {
		const struct mlxsw_sp_sb_cm *cm;
458
		u32 min_buff;
459
		u32 max_buff;
460

461 462
		if (i == 8 && dir == MLXSW_REG_SBXX_DIR_INGRESS)
			continue; /* PG number 8 does not exist, skip it */
463
		cm = &cms[i];
464 465 466
		if (WARN_ON(mlxsw_sp_sb_pool_dess[cm->pool_index].dir != dir))
			continue;

467
		min_buff = mlxsw_sp_bytes_cells(mlxsw_sp, cm->min_buff);
468 469
		max_buff = cm->max_buff;
		if (max_buff == MLXSW_SP_SB_INFI) {
470 471 472
			err = mlxsw_sp_sb_cm_write(mlxsw_sp, local_port, i,
						   min_buff, 0,
						   true, cm->pool_index);
473 474 475 476 477
		} else {
			if (mlxsw_sp_sb_pool_is_static(mlxsw_sp,
						       cm->pool_index))
				max_buff = mlxsw_sp_bytes_cells(mlxsw_sp,
								max_buff);
478
			err = mlxsw_sp_sb_cm_write(mlxsw_sp, local_port, i,
479
						   min_buff, max_buff,
480
						   false, cm->pool_index);
481
		}
482 483 484 485 486 487 488 489
		if (err)
			return err;
	}
	return 0;
}

static int mlxsw_sp_port_sb_cms_init(struct mlxsw_sp_port *mlxsw_sp_port)
{
490 491 492 493 494 495 496 497 498 499 500 501 502 503
	int err;

	err = __mlxsw_sp_sb_cms_init(mlxsw_sp_port->mlxsw_sp,
				     mlxsw_sp_port->local_port,
				     MLXSW_REG_SBXX_DIR_INGRESS,
				     mlxsw_sp_sb_cms_ingress,
				     MLXSW_SP_SB_CMS_INGRESS_LEN);
	if (err)
		return err;
	return __mlxsw_sp_sb_cms_init(mlxsw_sp_port->mlxsw_sp,
				      mlxsw_sp_port->local_port,
				      MLXSW_REG_SBXX_DIR_EGRESS,
				      mlxsw_sp_sb_cms_egress,
				      MLXSW_SP_SB_CMS_EGRESS_LEN);
504 505 506 507
}

static int mlxsw_sp_cpu_port_sb_cms_init(struct mlxsw_sp *mlxsw_sp)
{
508 509 510
	return __mlxsw_sp_sb_cms_init(mlxsw_sp, 0, MLXSW_REG_SBXX_DIR_EGRESS,
				      mlxsw_sp_cpu_port_sb_cms,
				      MLXSW_SP_CPU_PORT_SB_MCS_LEN);
511 512
}

513 514 515 516
#define MLXSW_SP_SB_PM(_min_buff, _max_buff)	\
	{					\
		.min_buff = _min_buff,		\
		.max_buff = _max_buff,		\
517 518
	}

519 520
static const struct mlxsw_sp_sb_pm mlxsw_sp_sb_pms[] = {
	/* Ingress pools. */
521 522 523 524
	MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX),
	MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
	MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
	MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX),
525
	/* Egress pools. */
526
	MLXSW_SP_SB_PM(0, 7),
527 528 529
	MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
	MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
	MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
530
	MLXSW_SP_SB_PM(10000, 90000),
531 532
};

533
#define MLXSW_SP_SB_PMS_LEN ARRAY_SIZE(mlxsw_sp_sb_pms)
534

535
static int mlxsw_sp_port_sb_pms_init(struct mlxsw_sp_port *mlxsw_sp_port)
536
{
537
	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
538 539 540
	int i;
	int err;

541 542
	for (i = 0; i < MLXSW_SP_SB_PMS_LEN; i++) {
		const struct mlxsw_sp_sb_pm *pm = &mlxsw_sp_sb_pms[i];
543
		u32 max_buff;
544
		u32 min_buff;
545

546
		min_buff = mlxsw_sp_bytes_cells(mlxsw_sp, pm->min_buff);
547 548 549
		max_buff = pm->max_buff;
		if (mlxsw_sp_sb_pool_is_static(mlxsw_sp, i))
			max_buff = mlxsw_sp_bytes_cells(mlxsw_sp, max_buff);
550
		err = mlxsw_sp_sb_pm_write(mlxsw_sp, mlxsw_sp_port->local_port,
551
					   i, min_buff, max_buff);
552 553 554 555 556 557 558 559 560
		if (err)
			return err;
	}
	return 0;
}

struct mlxsw_sp_sb_mm {
	u32 min_buff;
	u32 max_buff;
561
	u16 pool_index;
562 563
};

564 565 566 567
#define MLXSW_SP_SB_MM(_min_buff, _max_buff, _pool)	\
	{						\
		.min_buff = _min_buff,			\
		.max_buff = _max_buff,			\
568
		.pool_index = _pool,			\
569 570 571
	}

static const struct mlxsw_sp_sb_mm mlxsw_sp_sb_mms[] = {
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
	MLXSW_SP_SB_MM(0, 6, 4),
	MLXSW_SP_SB_MM(0, 6, 4),
	MLXSW_SP_SB_MM(0, 6, 4),
	MLXSW_SP_SB_MM(0, 6, 4),
	MLXSW_SP_SB_MM(0, 6, 4),
	MLXSW_SP_SB_MM(0, 6, 4),
	MLXSW_SP_SB_MM(0, 6, 4),
	MLXSW_SP_SB_MM(0, 6, 4),
	MLXSW_SP_SB_MM(0, 6, 4),
	MLXSW_SP_SB_MM(0, 6, 4),
	MLXSW_SP_SB_MM(0, 6, 4),
	MLXSW_SP_SB_MM(0, 6, 4),
	MLXSW_SP_SB_MM(0, 6, 4),
	MLXSW_SP_SB_MM(0, 6, 4),
	MLXSW_SP_SB_MM(0, 6, 4),
587 588 589 590 591 592 593 594 595 596 597
};

#define MLXSW_SP_SB_MMS_LEN ARRAY_SIZE(mlxsw_sp_sb_mms)

static int mlxsw_sp_sb_mms_init(struct mlxsw_sp *mlxsw_sp)
{
	char sbmm_pl[MLXSW_REG_SBMM_LEN];
	int i;
	int err;

	for (i = 0; i < MLXSW_SP_SB_MMS_LEN; i++) {
598
		const struct mlxsw_sp_sb_pool_des *des;
599
		const struct mlxsw_sp_sb_mm *mc;
600
		u32 min_buff;
601 602

		mc = &mlxsw_sp_sb_mms[i];
603
		des = &mlxsw_sp_sb_pool_dess[mc->pool_index];
604 605
		/* All pools used by sb_mm's are initialized using dynamic
		 * thresholds, therefore 'max_buff' isn't specified in cells.
606 607 608
		 */
		min_buff = mlxsw_sp_bytes_cells(mlxsw_sp, mc->min_buff);
		mlxsw_reg_sbmm_pack(sbmm_pl, i, min_buff, mc->max_buff,
609
				    des->pool);
610 611 612 613 614 615 616
		err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbmm), sbmm_pl);
		if (err)
			return err;
	}
	return 0;
}

617 618 619 620 621 622 623 624 625 626 627 628 629 630
static void mlxsw_sp_pool_count(u16 *p_ingress_len, u16 *p_egress_len)
{
	int i;

	for (i = 0; i < MLXSW_SP_SB_POOL_DESS_LEN; ++i)
		if (mlxsw_sp_sb_pool_dess[i].dir == MLXSW_REG_SBXX_DIR_EGRESS)
			goto out;
	WARN(1, "No egress pools\n");

out:
	*p_ingress_len = i;
	*p_egress_len = MLXSW_SP_SB_POOL_DESS_LEN - i;
}

631 632 633 634 635 636
const struct mlxsw_sp_sb_vals mlxsw_sp1_sb_vals = {
};

const struct mlxsw_sp_sb_vals mlxsw_sp2_sb_vals = {
};

637 638
int mlxsw_sp_buffers_init(struct mlxsw_sp *mlxsw_sp)
{
639 640
	u16 ing_pool_count;
	u16 eg_pool_count;
641 642
	int err;

643 644 645
	if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, CELL_SIZE))
		return -EIO;

646 647 648
	if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_BUFFER_SIZE))
		return -EIO;

649 650 651 652
	mlxsw_sp->sb = kzalloc(sizeof(*mlxsw_sp->sb), GFP_KERNEL);
	if (!mlxsw_sp->sb)
		return -ENOMEM;
	mlxsw_sp->sb->cell_size = MLXSW_CORE_RES_GET(mlxsw_sp->core, CELL_SIZE);
653 654
	mlxsw_sp->sb->sb_size = MLXSW_CORE_RES_GET(mlxsw_sp->core,
						   MAX_BUFFER_SIZE);
655

656
	err = mlxsw_sp_sb_ports_init(mlxsw_sp);
657
	if (err)
658
		goto err_sb_ports_init;
659 660
	err = mlxsw_sp_sb_prs_init(mlxsw_sp, mlxsw_sp_sb_prs,
				   MLXSW_SP_SB_PRS_LEN);
661 662
	if (err)
		goto err_sb_prs_init;
663 664
	err = mlxsw_sp_cpu_port_sb_cms_init(mlxsw_sp);
	if (err)
665
		goto err_sb_cpu_port_sb_cms_init;
666
	err = mlxsw_sp_sb_mms_init(mlxsw_sp);
667
	if (err)
668
		goto err_sb_mms_init;
669
	mlxsw_sp_pool_count(&ing_pool_count, &eg_pool_count);
670 671
	err = devlink_sb_register(priv_to_devlink(mlxsw_sp->core), 0,
				  mlxsw_sp->sb->sb_size,
672 673
				  ing_pool_count,
				  eg_pool_count,
674 675
				  MLXSW_SP_SB_ING_TC_COUNT,
				  MLXSW_SP_SB_EG_TC_COUNT);
676 677 678 679 680 681 682 683 684 685
	if (err)
		goto err_devlink_sb_register;

	return 0;

err_devlink_sb_register:
err_sb_mms_init:
err_sb_cpu_port_sb_cms_init:
err_sb_prs_init:
	mlxsw_sp_sb_ports_fini(mlxsw_sp);
686 687
err_sb_ports_init:
	kfree(mlxsw_sp->sb);
688
	return err;
689
}
690

691 692 693
void mlxsw_sp_buffers_fini(struct mlxsw_sp *mlxsw_sp)
{
	devlink_sb_unregister(priv_to_devlink(mlxsw_sp->core), 0);
694
	mlxsw_sp_sb_ports_fini(mlxsw_sp);
695
	kfree(mlxsw_sp->sb);
696 697 698 699 700 701
}

int mlxsw_sp_port_buffers_init(struct mlxsw_sp_port *mlxsw_sp_port)
{
	int err;

702
	err = mlxsw_sp_port_headroom_init(mlxsw_sp_port);
703 704 705 706 707 708 709 710 711
	if (err)
		return err;
	err = mlxsw_sp_port_sb_cms_init(mlxsw_sp_port);
	if (err)
		return err;
	err = mlxsw_sp_port_sb_pms_init(mlxsw_sp_port);

	return err;
}
712 713 714 715 716

int mlxsw_sp_sb_pool_get(struct mlxsw_core *mlxsw_core,
			 unsigned int sb_index, u16 pool_index,
			 struct devlink_sb_pool_info *pool_info)
{
717
	enum mlxsw_reg_sbxx_dir dir = mlxsw_sp_sb_pool_dess[pool_index].dir;
718
	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
719
	struct mlxsw_sp_sb_pr *pr;
720

721
	pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool_index);
722
	pool_info->pool_type = (enum devlink_sb_pool_type) dir;
723
	pool_info->size = mlxsw_sp_cells_bytes(mlxsw_sp, pr->size);
724
	pool_info->threshold_type = (enum devlink_sb_threshold_type) pr->mode;
725
	pool_info->cell_size = mlxsw_sp->sb->cell_size;
726 727 728 729 730 731 732 733
	return 0;
}

int mlxsw_sp_sb_pool_set(struct mlxsw_core *mlxsw_core,
			 unsigned int sb_index, u16 pool_index, u32 size,
			 enum devlink_sb_threshold_type threshold_type)
{
	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
734
	u32 pool_size = mlxsw_sp_bytes_cells(mlxsw_sp, size);
735
	enum mlxsw_reg_sbpr_mode mode;
736

737 738 739
	if (size > MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_BUFFER_SIZE))
		return -EINVAL;

740
	mode = (enum mlxsw_reg_sbpr_mode) threshold_type;
741 742
	return mlxsw_sp_sb_pr_write(mlxsw_sp, pool_index, mode,
				    pool_size, false);
743 744 745 746
}

#define MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET (-2) /* 3->1, 16->14 */

747 748
static u32 mlxsw_sp_sb_threshold_out(struct mlxsw_sp *mlxsw_sp, u16 pool_index,
				     u32 max_buff)
749
{
750
	struct mlxsw_sp_sb_pr *pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool_index);
751 752 753

	if (pr->mode == MLXSW_REG_SBPR_MODE_DYNAMIC)
		return max_buff - MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET;
754
	return mlxsw_sp_cells_bytes(mlxsw_sp, max_buff);
755 756
}

757 758
static int mlxsw_sp_sb_threshold_in(struct mlxsw_sp *mlxsw_sp, u16 pool_index,
				    u32 threshold, u32 *p_max_buff)
759
{
760
	struct mlxsw_sp_sb_pr *pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool_index);
761 762 763 764 765 766 767 768 769 770

	if (pr->mode == MLXSW_REG_SBPR_MODE_DYNAMIC) {
		int val;

		val = threshold + MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET;
		if (val < MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN ||
		    val > MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX)
			return -EINVAL;
		*p_max_buff = val;
	} else {
771
		*p_max_buff = mlxsw_sp_bytes_cells(mlxsw_sp, threshold);
772 773 774 775 776 777 778 779 780 781 782 783 784
	}
	return 0;
}

int mlxsw_sp_sb_port_pool_get(struct mlxsw_core_port *mlxsw_core_port,
			      unsigned int sb_index, u16 pool_index,
			      u32 *p_threshold)
{
	struct mlxsw_sp_port *mlxsw_sp_port =
			mlxsw_core_port_driver_priv(mlxsw_core_port);
	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
	u8 local_port = mlxsw_sp_port->local_port;
	struct mlxsw_sp_sb_pm *pm = mlxsw_sp_sb_pm_get(mlxsw_sp, local_port,
785
						       pool_index);
786

787
	*p_threshold = mlxsw_sp_sb_threshold_out(mlxsw_sp, pool_index,
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
						 pm->max_buff);
	return 0;
}

int mlxsw_sp_sb_port_pool_set(struct mlxsw_core_port *mlxsw_core_port,
			      unsigned int sb_index, u16 pool_index,
			      u32 threshold)
{
	struct mlxsw_sp_port *mlxsw_sp_port =
			mlxsw_core_port_driver_priv(mlxsw_core_port);
	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
	u8 local_port = mlxsw_sp_port->local_port;
	u32 max_buff;
	int err;

803
	err = mlxsw_sp_sb_threshold_in(mlxsw_sp, pool_index,
804 805 806 807
				       threshold, &max_buff);
	if (err)
		return err;

808
	return mlxsw_sp_sb_pm_write(mlxsw_sp, local_port, pool_index,
809 810 811 812 813 814 815 816 817 818 819 820 821
				    0, max_buff);
}

int mlxsw_sp_sb_tc_pool_bind_get(struct mlxsw_core_port *mlxsw_core_port,
				 unsigned int sb_index, u16 tc_index,
				 enum devlink_sb_pool_type pool_type,
				 u16 *p_pool_index, u32 *p_threshold)
{
	struct mlxsw_sp_port *mlxsw_sp_port =
			mlxsw_core_port_driver_priv(mlxsw_core_port);
	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
	u8 local_port = mlxsw_sp_port->local_port;
	u8 pg_buff = tc_index;
822
	enum mlxsw_reg_sbxx_dir dir = (enum mlxsw_reg_sbxx_dir) pool_type;
823 824 825
	struct mlxsw_sp_sb_cm *cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port,
						       pg_buff, dir);

826
	*p_threshold = mlxsw_sp_sb_threshold_out(mlxsw_sp, cm->pool_index,
827
						 cm->max_buff);
828
	*p_pool_index = cm->pool_index;
829 830 831 832 833 834 835 836 837 838 839 840 841
	return 0;
}

int mlxsw_sp_sb_tc_pool_bind_set(struct mlxsw_core_port *mlxsw_core_port,
				 unsigned int sb_index, u16 tc_index,
				 enum devlink_sb_pool_type pool_type,
				 u16 pool_index, u32 threshold)
{
	struct mlxsw_sp_port *mlxsw_sp_port =
			mlxsw_core_port_driver_priv(mlxsw_core_port);
	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
	u8 local_port = mlxsw_sp_port->local_port;
	u8 pg_buff = tc_index;
842
	enum mlxsw_reg_sbxx_dir dir = (enum mlxsw_reg_sbxx_dir) pool_type;
843 844 845
	u32 max_buff;
	int err;

846
	if (dir != mlxsw_sp_sb_pool_dess[pool_index].dir)
847 848
		return -EINVAL;

849
	err = mlxsw_sp_sb_threshold_in(mlxsw_sp, pool_index,
850 851 852 853
				       threshold, &max_buff);
	if (err)
		return err;

854
	return mlxsw_sp_sb_cm_write(mlxsw_sp, local_port, pg_buff,
855
				    0, max_buff, false, pool_index);
856
}
857 858

#define MASKED_COUNT_MAX \
859 860
	(MLXSW_REG_SBSR_REC_MAX_COUNT / \
	 (MLXSW_SP_SB_ING_TC_COUNT + MLXSW_SP_SB_EG_TC_COUNT))
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882

struct mlxsw_sp_sb_sr_occ_query_cb_ctx {
	u8 masked_count;
	u8 local_port_1;
};

static void mlxsw_sp_sb_sr_occ_query_cb(struct mlxsw_core *mlxsw_core,
					char *sbsr_pl, size_t sbsr_pl_len,
					unsigned long cb_priv)
{
	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
	struct mlxsw_sp_sb_sr_occ_query_cb_ctx cb_ctx;
	u8 masked_count;
	u8 local_port;
	int rec_index = 0;
	struct mlxsw_sp_sb_cm *cm;
	int i;

	memcpy(&cb_ctx, &cb_priv, sizeof(cb_ctx));

	masked_count = 0;
	for (local_port = cb_ctx.local_port_1;
883
	     local_port < mlxsw_core_max_ports(mlxsw_core); local_port++) {
884 885
		if (!mlxsw_sp->ports[local_port])
			continue;
886
		for (i = 0; i < MLXSW_SP_SB_ING_TC_COUNT; i++) {
887 888 889 890 891 892 893 894 895 896
			cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port, i,
						MLXSW_REG_SBXX_DIR_INGRESS);
			mlxsw_reg_sbsr_rec_unpack(sbsr_pl, rec_index++,
						  &cm->occ.cur, &cm->occ.max);
		}
		if (++masked_count == cb_ctx.masked_count)
			break;
	}
	masked_count = 0;
	for (local_port = cb_ctx.local_port_1;
897
	     local_port < mlxsw_core_max_ports(mlxsw_core); local_port++) {
898 899
		if (!mlxsw_sp->ports[local_port])
			continue;
900
		for (i = 0; i < MLXSW_SP_SB_EG_TC_COUNT; i++) {
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
			cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port, i,
						MLXSW_REG_SBXX_DIR_EGRESS);
			mlxsw_reg_sbsr_rec_unpack(sbsr_pl, rec_index++,
						  &cm->occ.cur, &cm->occ.max);
		}
		if (++masked_count == cb_ctx.masked_count)
			break;
	}
}

int mlxsw_sp_sb_occ_snapshot(struct mlxsw_core *mlxsw_core,
			     unsigned int sb_index)
{
	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
	struct mlxsw_sp_sb_sr_occ_query_cb_ctx cb_ctx;
	unsigned long cb_priv;
	LIST_HEAD(bulk_list);
	char *sbsr_pl;
	u8 masked_count;
	u8 local_port_1;
	u8 local_port = 0;
	int i;
	int err;
	int err2;

	sbsr_pl = kmalloc(MLXSW_REG_SBSR_LEN, GFP_KERNEL);
	if (!sbsr_pl)
		return -ENOMEM;

next_batch:
	local_port++;
	local_port_1 = local_port;
	masked_count = 0;
	mlxsw_reg_sbsr_pack(sbsr_pl, false);
935
	for (i = 0; i < MLXSW_SP_SB_ING_TC_COUNT; i++)
936
		mlxsw_reg_sbsr_pg_buff_mask_set(sbsr_pl, i, 1);
937
	for (i = 0; i < MLXSW_SP_SB_EG_TC_COUNT; i++)
938
		mlxsw_reg_sbsr_tclass_mask_set(sbsr_pl, i, 1);
939
	for (; local_port < mlxsw_core_max_ports(mlxsw_core); local_port++) {
940 941 942 943
		if (!mlxsw_sp->ports[local_port])
			continue;
		mlxsw_reg_sbsr_ingress_port_mask_set(sbsr_pl, local_port, 1);
		mlxsw_reg_sbsr_egress_port_mask_set(sbsr_pl, local_port, 1);
944
		for (i = 0; i < MLXSW_SP_SB_POOL_DESS_LEN; i++) {
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
			err = mlxsw_sp_sb_pm_occ_query(mlxsw_sp, local_port, i,
						       &bulk_list);
			if (err)
				goto out;
		}
		if (++masked_count == MASKED_COUNT_MAX)
			goto do_query;
	}

do_query:
	cb_ctx.masked_count = masked_count;
	cb_ctx.local_port_1 = local_port_1;
	memcpy(&cb_priv, &cb_ctx, sizeof(cb_ctx));
	err = mlxsw_reg_trans_query(mlxsw_core, MLXSW_REG(sbsr), sbsr_pl,
				    &bulk_list, mlxsw_sp_sb_sr_occ_query_cb,
				    cb_priv);
	if (err)
		goto out;
963
	if (local_port < mlxsw_core_max_ports(mlxsw_core))
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
		goto next_batch;

out:
	err2 = mlxsw_reg_trans_bulk_wait(&bulk_list);
	if (!err)
		err = err2;
	kfree(sbsr_pl);
	return err;
}

int mlxsw_sp_sb_occ_max_clear(struct mlxsw_core *mlxsw_core,
			      unsigned int sb_index)
{
	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
	LIST_HEAD(bulk_list);
	char *sbsr_pl;
	unsigned int masked_count;
	u8 local_port = 0;
	int i;
	int err;
	int err2;

	sbsr_pl = kmalloc(MLXSW_REG_SBSR_LEN, GFP_KERNEL);
	if (!sbsr_pl)
		return -ENOMEM;

next_batch:
	local_port++;
	masked_count = 0;
	mlxsw_reg_sbsr_pack(sbsr_pl, true);
994
	for (i = 0; i < MLXSW_SP_SB_ING_TC_COUNT; i++)
995
		mlxsw_reg_sbsr_pg_buff_mask_set(sbsr_pl, i, 1);
996
	for (i = 0; i < MLXSW_SP_SB_EG_TC_COUNT; i++)
997
		mlxsw_reg_sbsr_tclass_mask_set(sbsr_pl, i, 1);
998
	for (; local_port < mlxsw_core_max_ports(mlxsw_core); local_port++) {
999 1000 1001 1002
		if (!mlxsw_sp->ports[local_port])
			continue;
		mlxsw_reg_sbsr_ingress_port_mask_set(sbsr_pl, local_port, 1);
		mlxsw_reg_sbsr_egress_port_mask_set(sbsr_pl, local_port, 1);
1003
		for (i = 0; i < MLXSW_SP_SB_POOL_DESS_LEN; i++) {
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
			err = mlxsw_sp_sb_pm_occ_clear(mlxsw_sp, local_port, i,
						       &bulk_list);
			if (err)
				goto out;
		}
		if (++masked_count == MASKED_COUNT_MAX)
			goto do_query;
	}

do_query:
	err = mlxsw_reg_trans_query(mlxsw_core, MLXSW_REG(sbsr), sbsr_pl,
				    &bulk_list, NULL, 0);
	if (err)
		goto out;
1018
	if (local_port < mlxsw_core_max_ports(mlxsw_core))
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
		goto next_batch;

out:
	err2 = mlxsw_reg_trans_bulk_wait(&bulk_list);
	if (!err)
		err = err2;
	kfree(sbsr_pl);
	return err;
}

int mlxsw_sp_sb_occ_port_pool_get(struct mlxsw_core_port *mlxsw_core_port,
				  unsigned int sb_index, u16 pool_index,
				  u32 *p_cur, u32 *p_max)
{
	struct mlxsw_sp_port *mlxsw_sp_port =
			mlxsw_core_port_driver_priv(mlxsw_core_port);
	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
	u8 local_port = mlxsw_sp_port->local_port;
	struct mlxsw_sp_sb_pm *pm = mlxsw_sp_sb_pm_get(mlxsw_sp, local_port,
1038
						       pool_index);
1039

1040 1041
	*p_cur = mlxsw_sp_cells_bytes(mlxsw_sp, pm->occ.cur);
	*p_max = mlxsw_sp_cells_bytes(mlxsw_sp, pm->occ.max);
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
	return 0;
}

int mlxsw_sp_sb_occ_tc_port_bind_get(struct mlxsw_core_port *mlxsw_core_port,
				     unsigned int sb_index, u16 tc_index,
				     enum devlink_sb_pool_type pool_type,
				     u32 *p_cur, u32 *p_max)
{
	struct mlxsw_sp_port *mlxsw_sp_port =
			mlxsw_core_port_driver_priv(mlxsw_core_port);
	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
	u8 local_port = mlxsw_sp_port->local_port;
	u8 pg_buff = tc_index;
1055
	enum mlxsw_reg_sbxx_dir dir = (enum mlxsw_reg_sbxx_dir) pool_type;
1056 1057 1058
	struct mlxsw_sp_sb_cm *cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port,
						       pg_buff, dir);

1059 1060
	*p_cur = mlxsw_sp_cells_bytes(mlxsw_sp, cm->occ.cur);
	*p_max = mlxsw_sp_cells_bytes(mlxsw_sp, cm->occ.max);
1061 1062
	return 0;
}