18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/kernel.h>
58c2ecf20Sopenharmony_ci#include <linux/types.h>
68c2ecf20Sopenharmony_ci#include <linux/dcbnl.h>
78c2ecf20Sopenharmony_ci#include <linux/if_ether.h>
88c2ecf20Sopenharmony_ci#include <linux/list.h>
98c2ecf20Sopenharmony_ci#include <linux/netlink.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include "spectrum.h"
128c2ecf20Sopenharmony_ci#include "core.h"
138c2ecf20Sopenharmony_ci#include "port.h"
148c2ecf20Sopenharmony_ci#include "reg.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistruct mlxsw_sp_sb_pr {
178c2ecf20Sopenharmony_ci	enum mlxsw_reg_sbpr_mode mode;
188c2ecf20Sopenharmony_ci	u32 size;
198c2ecf20Sopenharmony_ci	u8 freeze_mode:1,
208c2ecf20Sopenharmony_ci	   freeze_size:1;
218c2ecf20Sopenharmony_ci};
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistruct mlxsw_cp_sb_occ {
248c2ecf20Sopenharmony_ci	u32 cur;
258c2ecf20Sopenharmony_ci	u32 max;
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistruct mlxsw_sp_sb_cm {
298c2ecf20Sopenharmony_ci	u32 min_buff;
308c2ecf20Sopenharmony_ci	u32 max_buff;
318c2ecf20Sopenharmony_ci	u16 pool_index;
328c2ecf20Sopenharmony_ci	struct mlxsw_cp_sb_occ occ;
338c2ecf20Sopenharmony_ci	u8 freeze_pool:1,
348c2ecf20Sopenharmony_ci	   freeze_thresh:1;
358c2ecf20Sopenharmony_ci};
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_INFI -1U
388c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_REST -2U
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistruct mlxsw_sp_sb_pm {
418c2ecf20Sopenharmony_ci	u32 min_buff;
428c2ecf20Sopenharmony_ci	u32 max_buff;
438c2ecf20Sopenharmony_ci	struct mlxsw_cp_sb_occ occ;
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistruct mlxsw_sp_sb_mm {
478c2ecf20Sopenharmony_ci	u32 min_buff;
488c2ecf20Sopenharmony_ci	u32 max_buff;
498c2ecf20Sopenharmony_ci	u16 pool_index;
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistruct mlxsw_sp_sb_pool_des {
538c2ecf20Sopenharmony_ci	enum mlxsw_reg_sbxx_dir dir;
548c2ecf20Sopenharmony_ci	u8 pool;
558c2ecf20Sopenharmony_ci};
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_POOL_ING		0
588c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_POOL_EGR		4
598c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_POOL_EGR_MC		8
608c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_POOL_ING_CPU	9
618c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_POOL_EGR_CPU	10
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic const struct mlxsw_sp_sb_pool_des mlxsw_sp1_sb_pool_dess[] = {
648c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_INGRESS, 0},
658c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_INGRESS, 1},
668c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_INGRESS, 2},
678c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_INGRESS, 3},
688c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_EGRESS, 0},
698c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_EGRESS, 1},
708c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_EGRESS, 2},
718c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_EGRESS, 3},
728c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_EGRESS, 15},
738c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_INGRESS, 4},
748c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_EGRESS, 4},
758c2ecf20Sopenharmony_ci};
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic const struct mlxsw_sp_sb_pool_des mlxsw_sp2_sb_pool_dess[] = {
788c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_INGRESS, 0},
798c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_INGRESS, 1},
808c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_INGRESS, 2},
818c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_INGRESS, 3},
828c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_EGRESS, 0},
838c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_EGRESS, 1},
848c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_EGRESS, 2},
858c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_EGRESS, 3},
868c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_EGRESS, 15},
878c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_INGRESS, 4},
888c2ecf20Sopenharmony_ci	{MLXSW_REG_SBXX_DIR_EGRESS, 4},
898c2ecf20Sopenharmony_ci};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_ING_TC_COUNT 8
928c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_EG_TC_COUNT 16
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistruct mlxsw_sp_sb_port {
958c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_cm ing_cms[MLXSW_SP_SB_ING_TC_COUNT];
968c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_cm eg_cms[MLXSW_SP_SB_EG_TC_COUNT];
978c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_pm *pms;
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistruct mlxsw_sp_sb {
1018c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_pr *prs;
1028c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_port *ports;
1038c2ecf20Sopenharmony_ci	u32 cell_size;
1048c2ecf20Sopenharmony_ci	u32 max_headroom_cells;
1058c2ecf20Sopenharmony_ci	u64 sb_size;
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistruct mlxsw_sp_sb_vals {
1098c2ecf20Sopenharmony_ci	unsigned int pool_count;
1108c2ecf20Sopenharmony_ci	const struct mlxsw_sp_sb_pool_des *pool_dess;
1118c2ecf20Sopenharmony_ci	const struct mlxsw_sp_sb_pm *pms;
1128c2ecf20Sopenharmony_ci	const struct mlxsw_sp_sb_pm *pms_cpu;
1138c2ecf20Sopenharmony_ci	const struct mlxsw_sp_sb_pr *prs;
1148c2ecf20Sopenharmony_ci	const struct mlxsw_sp_sb_mm *mms;
1158c2ecf20Sopenharmony_ci	const struct mlxsw_sp_sb_cm *cms_ingress;
1168c2ecf20Sopenharmony_ci	const struct mlxsw_sp_sb_cm *cms_egress;
1178c2ecf20Sopenharmony_ci	const struct mlxsw_sp_sb_cm *cms_cpu;
1188c2ecf20Sopenharmony_ci	unsigned int mms_count;
1198c2ecf20Sopenharmony_ci	unsigned int cms_ingress_count;
1208c2ecf20Sopenharmony_ci	unsigned int cms_egress_count;
1218c2ecf20Sopenharmony_ci	unsigned int cms_cpu_count;
1228c2ecf20Sopenharmony_ci};
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistruct mlxsw_sp_sb_ops {
1258c2ecf20Sopenharmony_ci	u32 (*int_buf_size_get)(int mtu, u32 speed);
1268c2ecf20Sopenharmony_ci};
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ciu32 mlxsw_sp_cells_bytes(const struct mlxsw_sp *mlxsw_sp, u32 cells)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	return mlxsw_sp->sb->cell_size * cells;
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ciu32 mlxsw_sp_bytes_cells(const struct mlxsw_sp *mlxsw_sp, u32 bytes)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	return DIV_ROUND_UP(bytes, mlxsw_sp->sb->cell_size);
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic u32 mlxsw_sp_port_headroom_8x_adjust(const struct mlxsw_sp_port *mlxsw_sp_port,
1398c2ecf20Sopenharmony_ci					    u32 size_cells)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	/* Ports with eight lanes use two headroom buffers between which the
1428c2ecf20Sopenharmony_ci	 * configured headroom size is split. Therefore, multiply the calculated
1438c2ecf20Sopenharmony_ci	 * headroom size by two.
1448c2ecf20Sopenharmony_ci	 */
1458c2ecf20Sopenharmony_ci	return mlxsw_sp_port->mapping.width == 8 ? 2 * size_cells : size_cells;
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic struct mlxsw_sp_sb_pr *mlxsw_sp_sb_pr_get(struct mlxsw_sp *mlxsw_sp,
1498c2ecf20Sopenharmony_ci						 u16 pool_index)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	return &mlxsw_sp->sb->prs[pool_index];
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic bool mlxsw_sp_sb_cm_exists(u8 pg_buff, enum mlxsw_reg_sbxx_dir dir)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	if (dir == MLXSW_REG_SBXX_DIR_INGRESS)
1578c2ecf20Sopenharmony_ci		return pg_buff < MLXSW_SP_SB_ING_TC_COUNT;
1588c2ecf20Sopenharmony_ci	else
1598c2ecf20Sopenharmony_ci		return pg_buff < MLXSW_SP_SB_EG_TC_COUNT;
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic struct mlxsw_sp_sb_cm *mlxsw_sp_sb_cm_get(struct mlxsw_sp *mlxsw_sp,
1638c2ecf20Sopenharmony_ci						 u8 local_port, u8 pg_buff,
1648c2ecf20Sopenharmony_ci						 enum mlxsw_reg_sbxx_dir dir)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_port *sb_port = &mlxsw_sp->sb->ports[local_port];
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	WARN_ON(!mlxsw_sp_sb_cm_exists(pg_buff, dir));
1698c2ecf20Sopenharmony_ci	if (dir == MLXSW_REG_SBXX_DIR_INGRESS)
1708c2ecf20Sopenharmony_ci		return &sb_port->ing_cms[pg_buff];
1718c2ecf20Sopenharmony_ci	else
1728c2ecf20Sopenharmony_ci		return &sb_port->eg_cms[pg_buff];
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic struct mlxsw_sp_sb_pm *mlxsw_sp_sb_pm_get(struct mlxsw_sp *mlxsw_sp,
1768c2ecf20Sopenharmony_ci						 u8 local_port, u16 pool_index)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	return &mlxsw_sp->sb->ports[local_port].pms[pool_index];
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic int mlxsw_sp_sb_pr_write(struct mlxsw_sp *mlxsw_sp, u16 pool_index,
1828c2ecf20Sopenharmony_ci				enum mlxsw_reg_sbpr_mode mode,
1838c2ecf20Sopenharmony_ci				u32 size, bool infi_size)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	const struct mlxsw_sp_sb_pool_des *des =
1868c2ecf20Sopenharmony_ci		&mlxsw_sp->sb_vals->pool_dess[pool_index];
1878c2ecf20Sopenharmony_ci	char sbpr_pl[MLXSW_REG_SBPR_LEN];
1888c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_pr *pr;
1898c2ecf20Sopenharmony_ci	int err;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	mlxsw_reg_sbpr_pack(sbpr_pl, des->pool, des->dir, mode,
1928c2ecf20Sopenharmony_ci			    size, infi_size);
1938c2ecf20Sopenharmony_ci	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbpr), sbpr_pl);
1948c2ecf20Sopenharmony_ci	if (err)
1958c2ecf20Sopenharmony_ci		return err;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	if (infi_size)
1988c2ecf20Sopenharmony_ci		size = mlxsw_sp_bytes_cells(mlxsw_sp, mlxsw_sp->sb->sb_size);
1998c2ecf20Sopenharmony_ci	pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool_index);
2008c2ecf20Sopenharmony_ci	pr->mode = mode;
2018c2ecf20Sopenharmony_ci	pr->size = size;
2028c2ecf20Sopenharmony_ci	return 0;
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cistatic int mlxsw_sp_sb_cm_write(struct mlxsw_sp *mlxsw_sp, u8 local_port,
2068c2ecf20Sopenharmony_ci				u8 pg_buff, u32 min_buff, u32 max_buff,
2078c2ecf20Sopenharmony_ci				bool infi_max, u16 pool_index)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	const struct mlxsw_sp_sb_pool_des *des =
2108c2ecf20Sopenharmony_ci		&mlxsw_sp->sb_vals->pool_dess[pool_index];
2118c2ecf20Sopenharmony_ci	char sbcm_pl[MLXSW_REG_SBCM_LEN];
2128c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_cm *cm;
2138c2ecf20Sopenharmony_ci	int err;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	mlxsw_reg_sbcm_pack(sbcm_pl, local_port, pg_buff, des->dir,
2168c2ecf20Sopenharmony_ci			    min_buff, max_buff, infi_max, des->pool);
2178c2ecf20Sopenharmony_ci	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbcm), sbcm_pl);
2188c2ecf20Sopenharmony_ci	if (err)
2198c2ecf20Sopenharmony_ci		return err;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	if (mlxsw_sp_sb_cm_exists(pg_buff, des->dir)) {
2228c2ecf20Sopenharmony_ci		if (infi_max)
2238c2ecf20Sopenharmony_ci			max_buff = mlxsw_sp_bytes_cells(mlxsw_sp,
2248c2ecf20Sopenharmony_ci							mlxsw_sp->sb->sb_size);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci		cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port, pg_buff,
2278c2ecf20Sopenharmony_ci					des->dir);
2288c2ecf20Sopenharmony_ci		cm->min_buff = min_buff;
2298c2ecf20Sopenharmony_ci		cm->max_buff = max_buff;
2308c2ecf20Sopenharmony_ci		cm->pool_index = pool_index;
2318c2ecf20Sopenharmony_ci	}
2328c2ecf20Sopenharmony_ci	return 0;
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic int mlxsw_sp_sb_pm_write(struct mlxsw_sp *mlxsw_sp, u8 local_port,
2368c2ecf20Sopenharmony_ci				u16 pool_index, u32 min_buff, u32 max_buff)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	const struct mlxsw_sp_sb_pool_des *des =
2398c2ecf20Sopenharmony_ci		&mlxsw_sp->sb_vals->pool_dess[pool_index];
2408c2ecf20Sopenharmony_ci	char sbpm_pl[MLXSW_REG_SBPM_LEN];
2418c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_pm *pm;
2428c2ecf20Sopenharmony_ci	int err;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	mlxsw_reg_sbpm_pack(sbpm_pl, local_port, des->pool, des->dir, false,
2458c2ecf20Sopenharmony_ci			    min_buff, max_buff);
2468c2ecf20Sopenharmony_ci	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbpm), sbpm_pl);
2478c2ecf20Sopenharmony_ci	if (err)
2488c2ecf20Sopenharmony_ci		return err;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	pm = mlxsw_sp_sb_pm_get(mlxsw_sp, local_port, pool_index);
2518c2ecf20Sopenharmony_ci	pm->min_buff = min_buff;
2528c2ecf20Sopenharmony_ci	pm->max_buff = max_buff;
2538c2ecf20Sopenharmony_ci	return 0;
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_cistatic int mlxsw_sp_sb_pm_occ_clear(struct mlxsw_sp *mlxsw_sp, u8 local_port,
2578c2ecf20Sopenharmony_ci				    u16 pool_index, struct list_head *bulk_list)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	const struct mlxsw_sp_sb_pool_des *des =
2608c2ecf20Sopenharmony_ci		&mlxsw_sp->sb_vals->pool_dess[pool_index];
2618c2ecf20Sopenharmony_ci	char sbpm_pl[MLXSW_REG_SBPM_LEN];
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	if (local_port == MLXSW_PORT_CPU_PORT &&
2648c2ecf20Sopenharmony_ci	    des->dir == MLXSW_REG_SBXX_DIR_INGRESS)
2658c2ecf20Sopenharmony_ci		return 0;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	mlxsw_reg_sbpm_pack(sbpm_pl, local_port, des->pool, des->dir,
2688c2ecf20Sopenharmony_ci			    true, 0, 0);
2698c2ecf20Sopenharmony_ci	return mlxsw_reg_trans_query(mlxsw_sp->core, MLXSW_REG(sbpm), sbpm_pl,
2708c2ecf20Sopenharmony_ci				     bulk_list, NULL, 0);
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_cistatic void mlxsw_sp_sb_pm_occ_query_cb(struct mlxsw_core *mlxsw_core,
2748c2ecf20Sopenharmony_ci					char *sbpm_pl, size_t sbpm_pl_len,
2758c2ecf20Sopenharmony_ci					unsigned long cb_priv)
2768c2ecf20Sopenharmony_ci{
2778c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_pm *pm = (struct mlxsw_sp_sb_pm *) cb_priv;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	mlxsw_reg_sbpm_unpack(sbpm_pl, &pm->occ.cur, &pm->occ.max);
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_cistatic int mlxsw_sp_sb_pm_occ_query(struct mlxsw_sp *mlxsw_sp, u8 local_port,
2838c2ecf20Sopenharmony_ci				    u16 pool_index, struct list_head *bulk_list)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	const struct mlxsw_sp_sb_pool_des *des =
2868c2ecf20Sopenharmony_ci		&mlxsw_sp->sb_vals->pool_dess[pool_index];
2878c2ecf20Sopenharmony_ci	char sbpm_pl[MLXSW_REG_SBPM_LEN];
2888c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_pm *pm;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	if (local_port == MLXSW_PORT_CPU_PORT &&
2918c2ecf20Sopenharmony_ci	    des->dir == MLXSW_REG_SBXX_DIR_INGRESS)
2928c2ecf20Sopenharmony_ci		return 0;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	pm = mlxsw_sp_sb_pm_get(mlxsw_sp, local_port, pool_index);
2958c2ecf20Sopenharmony_ci	mlxsw_reg_sbpm_pack(sbpm_pl, local_port, des->pool, des->dir,
2968c2ecf20Sopenharmony_ci			    false, 0, 0);
2978c2ecf20Sopenharmony_ci	return mlxsw_reg_trans_query(mlxsw_sp->core, MLXSW_REG(sbpm), sbpm_pl,
2988c2ecf20Sopenharmony_ci				     bulk_list,
2998c2ecf20Sopenharmony_ci				     mlxsw_sp_sb_pm_occ_query_cb,
3008c2ecf20Sopenharmony_ci				     (unsigned long) pm);
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_civoid mlxsw_sp_hdroom_prios_reset_buf_idx(struct mlxsw_sp_hdroom *hdroom)
3048c2ecf20Sopenharmony_ci{
3058c2ecf20Sopenharmony_ci	int prio;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
3088c2ecf20Sopenharmony_ci		switch (hdroom->mode) {
3098c2ecf20Sopenharmony_ci		case MLXSW_SP_HDROOM_MODE_DCB:
3108c2ecf20Sopenharmony_ci			hdroom->prios.prio[prio].buf_idx = hdroom->prios.prio[prio].ets_buf_idx;
3118c2ecf20Sopenharmony_ci			break;
3128c2ecf20Sopenharmony_ci		case MLXSW_SP_HDROOM_MODE_TC:
3138c2ecf20Sopenharmony_ci			hdroom->prios.prio[prio].buf_idx = hdroom->prios.prio[prio].set_buf_idx;
3148c2ecf20Sopenharmony_ci			break;
3158c2ecf20Sopenharmony_ci		}
3168c2ecf20Sopenharmony_ci	}
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_civoid mlxsw_sp_hdroom_bufs_reset_lossiness(struct mlxsw_sp_hdroom *hdroom)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	int prio;
3228c2ecf20Sopenharmony_ci	int i;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	for (i = 0; i < DCBX_MAX_BUFFERS; i++)
3258c2ecf20Sopenharmony_ci		hdroom->bufs.buf[i].lossy = true;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	for (prio = 0; prio < IEEE_8021Q_MAX_PRIORITIES; prio++) {
3288c2ecf20Sopenharmony_ci		if (!hdroom->prios.prio[prio].lossy)
3298c2ecf20Sopenharmony_ci			hdroom->bufs.buf[hdroom->prios.prio[prio].buf_idx].lossy = false;
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci}
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_cistatic u16 mlxsw_sp_hdroom_buf_threshold_get(const struct mlxsw_sp *mlxsw_sp, int mtu)
3348c2ecf20Sopenharmony_ci{
3358c2ecf20Sopenharmony_ci	return 2 * mlxsw_sp_bytes_cells(mlxsw_sp, mtu);
3368c2ecf20Sopenharmony_ci}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_cistatic void mlxsw_sp_hdroom_buf_pack(char *pbmc_pl, int index, u16 size, u16 thres, bool lossy)
3398c2ecf20Sopenharmony_ci{
3408c2ecf20Sopenharmony_ci	if (lossy)
3418c2ecf20Sopenharmony_ci		mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, index, size);
3428c2ecf20Sopenharmony_ci	else
3438c2ecf20Sopenharmony_ci		mlxsw_reg_pbmc_lossless_buffer_pack(pbmc_pl, index, size,
3448c2ecf20Sopenharmony_ci						    thres);
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistatic u16 mlxsw_sp_hdroom_buf_delay_get(const struct mlxsw_sp *mlxsw_sp,
3488c2ecf20Sopenharmony_ci					 const struct mlxsw_sp_hdroom *hdroom)
3498c2ecf20Sopenharmony_ci{
3508c2ecf20Sopenharmony_ci	u16 delay_cells;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	delay_cells = mlxsw_sp_bytes_cells(mlxsw_sp, hdroom->delay_bytes);
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	/* In the worst case scenario the delay will be made up of packets that
3558c2ecf20Sopenharmony_ci	 * are all of size CELL_SIZE + 1, which means each packet will require
3568c2ecf20Sopenharmony_ci	 * almost twice its true size when buffered in the switch. We therefore
3578c2ecf20Sopenharmony_ci	 * multiply this value by the "cell factor", which is close to 2.
3588c2ecf20Sopenharmony_ci	 *
3598c2ecf20Sopenharmony_ci	 * Another MTU is added in case the transmitting host already started
3608c2ecf20Sopenharmony_ci	 * transmitting a maximum length frame when the PFC packet was received.
3618c2ecf20Sopenharmony_ci	 */
3628c2ecf20Sopenharmony_ci	return 2 * delay_cells + mlxsw_sp_bytes_cells(mlxsw_sp, hdroom->mtu);
3638c2ecf20Sopenharmony_ci}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_cistatic u32 mlxsw_sp_hdroom_int_buf_size_get(struct mlxsw_sp *mlxsw_sp, int mtu, u32 speed)
3668c2ecf20Sopenharmony_ci{
3678c2ecf20Sopenharmony_ci	u32 buffsize = mlxsw_sp->sb_ops->int_buf_size_get(speed, mtu);
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	return mlxsw_sp_bytes_cells(mlxsw_sp, buffsize) + 1;
3708c2ecf20Sopenharmony_ci}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_cistatic bool mlxsw_sp_hdroom_buf_is_used(const struct mlxsw_sp_hdroom *hdroom, int buf)
3738c2ecf20Sopenharmony_ci{
3748c2ecf20Sopenharmony_ci	int prio;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
3778c2ecf20Sopenharmony_ci		if (hdroom->prios.prio[prio].buf_idx == buf)
3788c2ecf20Sopenharmony_ci			return true;
3798c2ecf20Sopenharmony_ci	}
3808c2ecf20Sopenharmony_ci	return false;
3818c2ecf20Sopenharmony_ci}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_civoid mlxsw_sp_hdroom_bufs_reset_sizes(struct mlxsw_sp_port *mlxsw_sp_port,
3848c2ecf20Sopenharmony_ci				      struct mlxsw_sp_hdroom *hdroom)
3858c2ecf20Sopenharmony_ci{
3868c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3878c2ecf20Sopenharmony_ci	u16 reserve_cells;
3888c2ecf20Sopenharmony_ci	int i;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	/* Internal buffer. */
3918c2ecf20Sopenharmony_ci	reserve_cells = mlxsw_sp_hdroom_int_buf_size_get(mlxsw_sp, mlxsw_sp_port->max_speed,
3928c2ecf20Sopenharmony_ci							 mlxsw_sp_port->max_mtu);
3938c2ecf20Sopenharmony_ci	reserve_cells = mlxsw_sp_port_headroom_8x_adjust(mlxsw_sp_port, reserve_cells);
3948c2ecf20Sopenharmony_ci	hdroom->int_buf.reserve_cells = reserve_cells;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	if (hdroom->int_buf.enable)
3978c2ecf20Sopenharmony_ci		hdroom->int_buf.size_cells = reserve_cells;
3988c2ecf20Sopenharmony_ci	else
3998c2ecf20Sopenharmony_ci		hdroom->int_buf.size_cells = 0;
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	/* PG buffers. */
4028c2ecf20Sopenharmony_ci	for (i = 0; i < DCBX_MAX_BUFFERS; i++) {
4038c2ecf20Sopenharmony_ci		struct mlxsw_sp_hdroom_buf *buf = &hdroom->bufs.buf[i];
4048c2ecf20Sopenharmony_ci		u16 thres_cells;
4058c2ecf20Sopenharmony_ci		u16 delay_cells;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci		if (!mlxsw_sp_hdroom_buf_is_used(hdroom, i)) {
4088c2ecf20Sopenharmony_ci			thres_cells = 0;
4098c2ecf20Sopenharmony_ci			delay_cells = 0;
4108c2ecf20Sopenharmony_ci		} else if (buf->lossy) {
4118c2ecf20Sopenharmony_ci			thres_cells = mlxsw_sp_hdroom_buf_threshold_get(mlxsw_sp, hdroom->mtu);
4128c2ecf20Sopenharmony_ci			delay_cells = 0;
4138c2ecf20Sopenharmony_ci		} else {
4148c2ecf20Sopenharmony_ci			thres_cells = mlxsw_sp_hdroom_buf_threshold_get(mlxsw_sp, hdroom->mtu);
4158c2ecf20Sopenharmony_ci			delay_cells = mlxsw_sp_hdroom_buf_delay_get(mlxsw_sp, hdroom);
4168c2ecf20Sopenharmony_ci		}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci		thres_cells = mlxsw_sp_port_headroom_8x_adjust(mlxsw_sp_port, thres_cells);
4198c2ecf20Sopenharmony_ci		delay_cells = mlxsw_sp_port_headroom_8x_adjust(mlxsw_sp_port, delay_cells);
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci		buf->thres_cells = thres_cells;
4228c2ecf20Sopenharmony_ci		if (hdroom->mode == MLXSW_SP_HDROOM_MODE_DCB) {
4238c2ecf20Sopenharmony_ci			buf->size_cells = thres_cells + delay_cells;
4248c2ecf20Sopenharmony_ci		} else {
4258c2ecf20Sopenharmony_ci			/* Do not allow going below the minimum size, even if
4268c2ecf20Sopenharmony_ci			 * the user requested it.
4278c2ecf20Sopenharmony_ci			 */
4288c2ecf20Sopenharmony_ci			buf->size_cells = max(buf->set_size_cells, buf->thres_cells);
4298c2ecf20Sopenharmony_ci		}
4308c2ecf20Sopenharmony_ci	}
4318c2ecf20Sopenharmony_ci}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci#define MLXSW_SP_PB_UNUSED 8
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_cistatic int mlxsw_sp_hdroom_configure_buffers(struct mlxsw_sp_port *mlxsw_sp_port,
4368c2ecf20Sopenharmony_ci					     const struct mlxsw_sp_hdroom *hdroom, bool force)
4378c2ecf20Sopenharmony_ci{
4388c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4398c2ecf20Sopenharmony_ci	char pbmc_pl[MLXSW_REG_PBMC_LEN];
4408c2ecf20Sopenharmony_ci	bool dirty;
4418c2ecf20Sopenharmony_ci	int err;
4428c2ecf20Sopenharmony_ci	int i;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	dirty = memcmp(&mlxsw_sp_port->hdroom->bufs, &hdroom->bufs, sizeof(hdroom->bufs));
4458c2ecf20Sopenharmony_ci	if (!dirty && !force)
4468c2ecf20Sopenharmony_ci		return 0;
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	mlxsw_reg_pbmc_pack(pbmc_pl, mlxsw_sp_port->local_port, 0xffff, 0xffff / 2);
4498c2ecf20Sopenharmony_ci	for (i = 0; i < MLXSW_SP_PB_COUNT; i++) {
4508c2ecf20Sopenharmony_ci		const struct mlxsw_sp_hdroom_buf *buf = &hdroom->bufs.buf[i];
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci		if (i == MLXSW_SP_PB_UNUSED)
4538c2ecf20Sopenharmony_ci			continue;
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci		mlxsw_sp_hdroom_buf_pack(pbmc_pl, i, buf->size_cells, buf->thres_cells, buf->lossy);
4568c2ecf20Sopenharmony_ci	}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, MLXSW_REG_PBMC_PORT_SHARED_BUF_IDX, 0);
4598c2ecf20Sopenharmony_ci	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
4608c2ecf20Sopenharmony_ci	if (err)
4618c2ecf20Sopenharmony_ci		return err;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	mlxsw_sp_port->hdroom->bufs = hdroom->bufs;
4648c2ecf20Sopenharmony_ci	return 0;
4658c2ecf20Sopenharmony_ci}
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_cistatic int mlxsw_sp_hdroom_configure_priomap(struct mlxsw_sp_port *mlxsw_sp_port,
4688c2ecf20Sopenharmony_ci					     const struct mlxsw_sp_hdroom *hdroom, bool force)
4698c2ecf20Sopenharmony_ci{
4708c2ecf20Sopenharmony_ci	char pptb_pl[MLXSW_REG_PPTB_LEN];
4718c2ecf20Sopenharmony_ci	bool dirty;
4728c2ecf20Sopenharmony_ci	int prio;
4738c2ecf20Sopenharmony_ci	int err;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	dirty = memcmp(&mlxsw_sp_port->hdroom->prios, &hdroom->prios, sizeof(hdroom->prios));
4768c2ecf20Sopenharmony_ci	if (!dirty && !force)
4778c2ecf20Sopenharmony_ci		return 0;
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	mlxsw_reg_pptb_pack(pptb_pl, mlxsw_sp_port->local_port);
4808c2ecf20Sopenharmony_ci	for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++)
4818c2ecf20Sopenharmony_ci		mlxsw_reg_pptb_prio_to_buff_pack(pptb_pl, prio, hdroom->prios.prio[prio].buf_idx);
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	err = mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pptb), pptb_pl);
4848c2ecf20Sopenharmony_ci	if (err)
4858c2ecf20Sopenharmony_ci		return err;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	mlxsw_sp_port->hdroom->prios = hdroom->prios;
4888c2ecf20Sopenharmony_ci	return 0;
4898c2ecf20Sopenharmony_ci}
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_cistatic int mlxsw_sp_hdroom_configure_int_buf(struct mlxsw_sp_port *mlxsw_sp_port,
4928c2ecf20Sopenharmony_ci					     const struct mlxsw_sp_hdroom *hdroom, bool force)
4938c2ecf20Sopenharmony_ci{
4948c2ecf20Sopenharmony_ci	char sbib_pl[MLXSW_REG_SBIB_LEN];
4958c2ecf20Sopenharmony_ci	bool dirty;
4968c2ecf20Sopenharmony_ci	int err;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	dirty = memcmp(&mlxsw_sp_port->hdroom->int_buf, &hdroom->int_buf, sizeof(hdroom->int_buf));
4998c2ecf20Sopenharmony_ci	if (!dirty && !force)
5008c2ecf20Sopenharmony_ci		return 0;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	mlxsw_reg_sbib_pack(sbib_pl, mlxsw_sp_port->local_port, hdroom->int_buf.size_cells);
5038c2ecf20Sopenharmony_ci	err = mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl);
5048c2ecf20Sopenharmony_ci	if (err)
5058c2ecf20Sopenharmony_ci		return err;
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	mlxsw_sp_port->hdroom->int_buf = hdroom->int_buf;
5088c2ecf20Sopenharmony_ci	return 0;
5098c2ecf20Sopenharmony_ci}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_cistatic bool mlxsw_sp_hdroom_bufs_fit(struct mlxsw_sp *mlxsw_sp,
5128c2ecf20Sopenharmony_ci				     const struct mlxsw_sp_hdroom *hdroom)
5138c2ecf20Sopenharmony_ci{
5148c2ecf20Sopenharmony_ci	u32 taken_headroom_cells = 0;
5158c2ecf20Sopenharmony_ci	int i;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	for (i = 0; i < MLXSW_SP_PB_COUNT; i++)
5188c2ecf20Sopenharmony_ci		taken_headroom_cells += hdroom->bufs.buf[i].size_cells;
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	taken_headroom_cells += hdroom->int_buf.reserve_cells;
5218c2ecf20Sopenharmony_ci	return taken_headroom_cells <= mlxsw_sp->sb->max_headroom_cells;
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_cistatic int __mlxsw_sp_hdroom_configure(struct mlxsw_sp_port *mlxsw_sp_port,
5258c2ecf20Sopenharmony_ci				       const struct mlxsw_sp_hdroom *hdroom, bool force)
5268c2ecf20Sopenharmony_ci{
5278c2ecf20Sopenharmony_ci	struct mlxsw_sp_hdroom orig_hdroom;
5288c2ecf20Sopenharmony_ci	struct mlxsw_sp_hdroom tmp_hdroom;
5298c2ecf20Sopenharmony_ci	int err;
5308c2ecf20Sopenharmony_ci	int i;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	/* Port buffers need to be configured in three steps. First, all buffers
5338c2ecf20Sopenharmony_ci	 * with non-zero size are configured. Then, prio-to-buffer map is
5348c2ecf20Sopenharmony_ci	 * updated, allowing traffic to flow to the now non-zero buffers.
5358c2ecf20Sopenharmony_ci	 * Finally, zero-sized buffers are configured, because now no traffic
5368c2ecf20Sopenharmony_ci	 * should be directed to them anymore. This way, in a non-congested
5378c2ecf20Sopenharmony_ci	 * system, no packet drops are introduced by the reconfiguration.
5388c2ecf20Sopenharmony_ci	 */
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	orig_hdroom = *mlxsw_sp_port->hdroom;
5418c2ecf20Sopenharmony_ci	tmp_hdroom = orig_hdroom;
5428c2ecf20Sopenharmony_ci	for (i = 0; i < MLXSW_SP_PB_COUNT; i++) {
5438c2ecf20Sopenharmony_ci		if (hdroom->bufs.buf[i].size_cells)
5448c2ecf20Sopenharmony_ci			tmp_hdroom.bufs.buf[i] = hdroom->bufs.buf[i];
5458c2ecf20Sopenharmony_ci	}
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	if (!mlxsw_sp_hdroom_bufs_fit(mlxsw_sp_port->mlxsw_sp, &tmp_hdroom) ||
5488c2ecf20Sopenharmony_ci	    !mlxsw_sp_hdroom_bufs_fit(mlxsw_sp_port->mlxsw_sp, hdroom))
5498c2ecf20Sopenharmony_ci		return -ENOBUFS;
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	err = mlxsw_sp_hdroom_configure_buffers(mlxsw_sp_port, &tmp_hdroom, force);
5528c2ecf20Sopenharmony_ci	if (err)
5538c2ecf20Sopenharmony_ci		return err;
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	err = mlxsw_sp_hdroom_configure_priomap(mlxsw_sp_port, hdroom, force);
5568c2ecf20Sopenharmony_ci	if (err)
5578c2ecf20Sopenharmony_ci		goto err_configure_priomap;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	err = mlxsw_sp_hdroom_configure_buffers(mlxsw_sp_port, hdroom, false);
5608c2ecf20Sopenharmony_ci	if (err)
5618c2ecf20Sopenharmony_ci		goto err_configure_buffers;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	err = mlxsw_sp_hdroom_configure_int_buf(mlxsw_sp_port, hdroom, false);
5648c2ecf20Sopenharmony_ci	if (err)
5658c2ecf20Sopenharmony_ci		goto err_configure_int_buf;
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	*mlxsw_sp_port->hdroom = *hdroom;
5688c2ecf20Sopenharmony_ci	return 0;
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_cierr_configure_int_buf:
5718c2ecf20Sopenharmony_ci	mlxsw_sp_hdroom_configure_buffers(mlxsw_sp_port, &tmp_hdroom, false);
5728c2ecf20Sopenharmony_cierr_configure_buffers:
5738c2ecf20Sopenharmony_ci	mlxsw_sp_hdroom_configure_priomap(mlxsw_sp_port, &tmp_hdroom, false);
5748c2ecf20Sopenharmony_cierr_configure_priomap:
5758c2ecf20Sopenharmony_ci	mlxsw_sp_hdroom_configure_buffers(mlxsw_sp_port, &orig_hdroom, false);
5768c2ecf20Sopenharmony_ci	return err;
5778c2ecf20Sopenharmony_ci}
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ciint mlxsw_sp_hdroom_configure(struct mlxsw_sp_port *mlxsw_sp_port,
5808c2ecf20Sopenharmony_ci			      const struct mlxsw_sp_hdroom *hdroom)
5818c2ecf20Sopenharmony_ci{
5828c2ecf20Sopenharmony_ci	return __mlxsw_sp_hdroom_configure(mlxsw_sp_port, hdroom, false);
5838c2ecf20Sopenharmony_ci}
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_cistatic int mlxsw_sp_port_headroom_init(struct mlxsw_sp_port *mlxsw_sp_port)
5868c2ecf20Sopenharmony_ci{
5878c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5888c2ecf20Sopenharmony_ci	struct mlxsw_sp_hdroom hdroom = {};
5898c2ecf20Sopenharmony_ci	u32 size9;
5908c2ecf20Sopenharmony_ci	int prio;
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	hdroom.mtu = mlxsw_sp_port->dev->mtu;
5938c2ecf20Sopenharmony_ci	hdroom.mode = MLXSW_SP_HDROOM_MODE_DCB;
5948c2ecf20Sopenharmony_ci	for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++)
5958c2ecf20Sopenharmony_ci		hdroom.prios.prio[prio].lossy = true;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	mlxsw_sp_hdroom_bufs_reset_lossiness(&hdroom);
5988c2ecf20Sopenharmony_ci	mlxsw_sp_hdroom_bufs_reset_sizes(mlxsw_sp_port, &hdroom);
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	/* Buffer 9 is used for control traffic. */
6018c2ecf20Sopenharmony_ci	size9 = mlxsw_sp_port_headroom_8x_adjust(mlxsw_sp_port, mlxsw_sp_port->max_mtu);
6028c2ecf20Sopenharmony_ci	hdroom.bufs.buf[9].size_cells = mlxsw_sp_bytes_cells(mlxsw_sp, size9);
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	return __mlxsw_sp_hdroom_configure(mlxsw_sp_port, &hdroom, true);
6058c2ecf20Sopenharmony_ci}
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_cistatic int mlxsw_sp_sb_port_init(struct mlxsw_sp *mlxsw_sp,
6088c2ecf20Sopenharmony_ci				 struct mlxsw_sp_sb_port *sb_port)
6098c2ecf20Sopenharmony_ci{
6108c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_pm *pms;
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	pms = kcalloc(mlxsw_sp->sb_vals->pool_count, sizeof(*pms),
6138c2ecf20Sopenharmony_ci		      GFP_KERNEL);
6148c2ecf20Sopenharmony_ci	if (!pms)
6158c2ecf20Sopenharmony_ci		return -ENOMEM;
6168c2ecf20Sopenharmony_ci	sb_port->pms = pms;
6178c2ecf20Sopenharmony_ci	return 0;
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_cistatic void mlxsw_sp_sb_port_fini(struct mlxsw_sp_sb_port *sb_port)
6218c2ecf20Sopenharmony_ci{
6228c2ecf20Sopenharmony_ci	kfree(sb_port->pms);
6238c2ecf20Sopenharmony_ci}
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_cistatic int mlxsw_sp_sb_ports_init(struct mlxsw_sp *mlxsw_sp)
6268c2ecf20Sopenharmony_ci{
6278c2ecf20Sopenharmony_ci	unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
6288c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_pr *prs;
6298c2ecf20Sopenharmony_ci	int i;
6308c2ecf20Sopenharmony_ci	int err;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	mlxsw_sp->sb->ports = kcalloc(max_ports,
6338c2ecf20Sopenharmony_ci				      sizeof(struct mlxsw_sp_sb_port),
6348c2ecf20Sopenharmony_ci				      GFP_KERNEL);
6358c2ecf20Sopenharmony_ci	if (!mlxsw_sp->sb->ports)
6368c2ecf20Sopenharmony_ci		return -ENOMEM;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	prs = kcalloc(mlxsw_sp->sb_vals->pool_count, sizeof(*prs),
6398c2ecf20Sopenharmony_ci		      GFP_KERNEL);
6408c2ecf20Sopenharmony_ci	if (!prs) {
6418c2ecf20Sopenharmony_ci		err = -ENOMEM;
6428c2ecf20Sopenharmony_ci		goto err_alloc_prs;
6438c2ecf20Sopenharmony_ci	}
6448c2ecf20Sopenharmony_ci	mlxsw_sp->sb->prs = prs;
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	for (i = 0; i < max_ports; i++) {
6478c2ecf20Sopenharmony_ci		err = mlxsw_sp_sb_port_init(mlxsw_sp, &mlxsw_sp->sb->ports[i]);
6488c2ecf20Sopenharmony_ci		if (err)
6498c2ecf20Sopenharmony_ci			goto err_sb_port_init;
6508c2ecf20Sopenharmony_ci	}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	return 0;
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_cierr_sb_port_init:
6558c2ecf20Sopenharmony_ci	for (i--; i >= 0; i--)
6568c2ecf20Sopenharmony_ci		mlxsw_sp_sb_port_fini(&mlxsw_sp->sb->ports[i]);
6578c2ecf20Sopenharmony_ci	kfree(mlxsw_sp->sb->prs);
6588c2ecf20Sopenharmony_cierr_alloc_prs:
6598c2ecf20Sopenharmony_ci	kfree(mlxsw_sp->sb->ports);
6608c2ecf20Sopenharmony_ci	return err;
6618c2ecf20Sopenharmony_ci}
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_cistatic void mlxsw_sp_sb_ports_fini(struct mlxsw_sp *mlxsw_sp)
6648c2ecf20Sopenharmony_ci{
6658c2ecf20Sopenharmony_ci	int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
6668c2ecf20Sopenharmony_ci	int i;
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	for (i = max_ports - 1; i >= 0; i--)
6698c2ecf20Sopenharmony_ci		mlxsw_sp_sb_port_fini(&mlxsw_sp->sb->ports[i]);
6708c2ecf20Sopenharmony_ci	kfree(mlxsw_sp->sb->prs);
6718c2ecf20Sopenharmony_ci	kfree(mlxsw_sp->sb->ports);
6728c2ecf20Sopenharmony_ci}
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_PR(_mode, _size)	\
6758c2ecf20Sopenharmony_ci	{				\
6768c2ecf20Sopenharmony_ci		.mode = _mode,		\
6778c2ecf20Sopenharmony_ci		.size = _size,		\
6788c2ecf20Sopenharmony_ci	}
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_PR_EXT(_mode, _size, _freeze_mode, _freeze_size)	\
6818c2ecf20Sopenharmony_ci	{								\
6828c2ecf20Sopenharmony_ci		.mode = _mode,						\
6838c2ecf20Sopenharmony_ci		.size = _size,						\
6848c2ecf20Sopenharmony_ci		.freeze_mode = _freeze_mode,				\
6858c2ecf20Sopenharmony_ci		.freeze_size = _freeze_size,				\
6868c2ecf20Sopenharmony_ci	}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci#define MLXSW_SP1_SB_PR_CPU_SIZE	(256 * 1000)
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci/* Order according to mlxsw_sp1_sb_pool_dess */
6918c2ecf20Sopenharmony_cistatic const struct mlxsw_sp_sb_pr mlxsw_sp1_sb_prs[] = {
6928c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, MLXSW_SP_SB_REST),
6938c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
6948c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
6958c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
6968c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC, MLXSW_SP_SB_REST,
6978c2ecf20Sopenharmony_ci			   true, false),
6988c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
6998c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
7008c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
7018c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_STATIC, MLXSW_SP_SB_INFI,
7028c2ecf20Sopenharmony_ci			   true, true),
7038c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC,
7048c2ecf20Sopenharmony_ci			   MLXSW_SP1_SB_PR_CPU_SIZE, true, false),
7058c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC,
7068c2ecf20Sopenharmony_ci			   MLXSW_SP1_SB_PR_CPU_SIZE, true, false),
7078c2ecf20Sopenharmony_ci};
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci#define MLXSW_SP2_SB_PR_CPU_SIZE	(256 * 1000)
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci/* Order according to mlxsw_sp2_sb_pool_dess */
7128c2ecf20Sopenharmony_cistatic const struct mlxsw_sp_sb_pr mlxsw_sp2_sb_prs[] = {
7138c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, MLXSW_SP_SB_REST),
7148c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC, 0),
7158c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC, 0),
7168c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC, 0),
7178c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC, MLXSW_SP_SB_REST,
7188c2ecf20Sopenharmony_ci			   true, false),
7198c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC, 0),
7208c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC, 0),
7218c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC, 0),
7228c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_STATIC, MLXSW_SP_SB_INFI,
7238c2ecf20Sopenharmony_ci			   true, true),
7248c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC,
7258c2ecf20Sopenharmony_ci			   MLXSW_SP2_SB_PR_CPU_SIZE, true, false),
7268c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC,
7278c2ecf20Sopenharmony_ci			   MLXSW_SP2_SB_PR_CPU_SIZE, true, false),
7288c2ecf20Sopenharmony_ci};
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_cistatic int mlxsw_sp_sb_prs_init(struct mlxsw_sp *mlxsw_sp,
7318c2ecf20Sopenharmony_ci				const struct mlxsw_sp_sb_pr *prs,
7328c2ecf20Sopenharmony_ci				const struct mlxsw_sp_sb_pool_des *pool_dess,
7338c2ecf20Sopenharmony_ci				size_t prs_len)
7348c2ecf20Sopenharmony_ci{
7358c2ecf20Sopenharmony_ci	/* Round down, unlike mlxsw_sp_bytes_cells(). */
7368c2ecf20Sopenharmony_ci	u32 sb_cells = div_u64(mlxsw_sp->sb->sb_size, mlxsw_sp->sb->cell_size);
7378c2ecf20Sopenharmony_ci	u32 rest_cells[2] = {sb_cells, sb_cells};
7388c2ecf20Sopenharmony_ci	int i;
7398c2ecf20Sopenharmony_ci	int err;
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci	/* Calculate how much space to give to the "REST" pools in either
7428c2ecf20Sopenharmony_ci	 * direction.
7438c2ecf20Sopenharmony_ci	 */
7448c2ecf20Sopenharmony_ci	for (i = 0; i < prs_len; i++) {
7458c2ecf20Sopenharmony_ci		enum mlxsw_reg_sbxx_dir dir = pool_dess[i].dir;
7468c2ecf20Sopenharmony_ci		u32 size = prs[i].size;
7478c2ecf20Sopenharmony_ci		u32 size_cells;
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci		if (size == MLXSW_SP_SB_INFI || size == MLXSW_SP_SB_REST)
7508c2ecf20Sopenharmony_ci			continue;
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci		size_cells = mlxsw_sp_bytes_cells(mlxsw_sp, size);
7538c2ecf20Sopenharmony_ci		if (WARN_ON_ONCE(size_cells > rest_cells[dir]))
7548c2ecf20Sopenharmony_ci			continue;
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci		rest_cells[dir] -= size_cells;
7578c2ecf20Sopenharmony_ci	}
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	for (i = 0; i < prs_len; i++) {
7608c2ecf20Sopenharmony_ci		u32 size = prs[i].size;
7618c2ecf20Sopenharmony_ci		u32 size_cells;
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci		if (size == MLXSW_SP_SB_INFI) {
7648c2ecf20Sopenharmony_ci			err = mlxsw_sp_sb_pr_write(mlxsw_sp, i, prs[i].mode,
7658c2ecf20Sopenharmony_ci						   0, true);
7668c2ecf20Sopenharmony_ci		} else if (size == MLXSW_SP_SB_REST) {
7678c2ecf20Sopenharmony_ci			size_cells = rest_cells[pool_dess[i].dir];
7688c2ecf20Sopenharmony_ci			err = mlxsw_sp_sb_pr_write(mlxsw_sp, i, prs[i].mode,
7698c2ecf20Sopenharmony_ci						   size_cells, false);
7708c2ecf20Sopenharmony_ci		} else {
7718c2ecf20Sopenharmony_ci			size_cells = mlxsw_sp_bytes_cells(mlxsw_sp, size);
7728c2ecf20Sopenharmony_ci			err = mlxsw_sp_sb_pr_write(mlxsw_sp, i, prs[i].mode,
7738c2ecf20Sopenharmony_ci						   size_cells, false);
7748c2ecf20Sopenharmony_ci		}
7758c2ecf20Sopenharmony_ci		if (err)
7768c2ecf20Sopenharmony_ci			return err;
7778c2ecf20Sopenharmony_ci	}
7788c2ecf20Sopenharmony_ci	return 0;
7798c2ecf20Sopenharmony_ci}
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_CM(_min_buff, _max_buff, _pool)	\
7828c2ecf20Sopenharmony_ci	{						\
7838c2ecf20Sopenharmony_ci		.min_buff = _min_buff,			\
7848c2ecf20Sopenharmony_ci		.max_buff = _max_buff,			\
7858c2ecf20Sopenharmony_ci		.pool_index = _pool,			\
7868c2ecf20Sopenharmony_ci	}
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_CM_ING(_min_buff, _max_buff)	\
7898c2ecf20Sopenharmony_ci	{						\
7908c2ecf20Sopenharmony_ci		.min_buff = _min_buff,			\
7918c2ecf20Sopenharmony_ci		.max_buff = _max_buff,			\
7928c2ecf20Sopenharmony_ci		.pool_index = MLXSW_SP_SB_POOL_ING,	\
7938c2ecf20Sopenharmony_ci	}
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_CM_EGR(_min_buff, _max_buff)	\
7968c2ecf20Sopenharmony_ci	{						\
7978c2ecf20Sopenharmony_ci		.min_buff = _min_buff,			\
7988c2ecf20Sopenharmony_ci		.max_buff = _max_buff,			\
7998c2ecf20Sopenharmony_ci		.pool_index = MLXSW_SP_SB_POOL_EGR,	\
8008c2ecf20Sopenharmony_ci	}
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_CM_EGR_MC(_min_buff, _max_buff)	\
8038c2ecf20Sopenharmony_ci	{						\
8048c2ecf20Sopenharmony_ci		.min_buff = _min_buff,			\
8058c2ecf20Sopenharmony_ci		.max_buff = _max_buff,			\
8068c2ecf20Sopenharmony_ci		.pool_index = MLXSW_SP_SB_POOL_EGR_MC,	\
8078c2ecf20Sopenharmony_ci		.freeze_pool = true,			\
8088c2ecf20Sopenharmony_ci		.freeze_thresh = true,			\
8098c2ecf20Sopenharmony_ci	}
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_cistatic const struct mlxsw_sp_sb_cm mlxsw_sp1_sb_cms_ingress[] = {
8128c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(10000, 8),
8138c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
8148c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
8158c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
8168c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
8178c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
8188c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
8198c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
8208c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(0, 0), /* dummy, this PG does not exist */
8218c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM(10000, 8, MLXSW_SP_SB_POOL_ING_CPU),
8228c2ecf20Sopenharmony_ci};
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_cistatic const struct mlxsw_sp_sb_cm mlxsw_sp2_sb_cms_ingress[] = {
8258c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(0, 7),
8268c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
8278c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
8288c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
8298c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
8308c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
8318c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
8328c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
8338c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_ING(0, 0), /* dummy, this PG does not exist */
8348c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM(10000, 8, MLXSW_SP_SB_POOL_ING_CPU),
8358c2ecf20Sopenharmony_ci};
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_cistatic const struct mlxsw_sp_sb_cm mlxsw_sp1_sb_cms_egress[] = {
8388c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(1500, 9),
8398c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(1500, 9),
8408c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(1500, 9),
8418c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(1500, 9),
8428c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(1500, 9),
8438c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(1500, 9),
8448c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(1500, 9),
8458c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(1500, 9),
8468c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
8478c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
8488c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
8498c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
8508c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
8518c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
8528c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
8538c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
8548c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(1, 0xff),
8558c2ecf20Sopenharmony_ci};
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_cistatic const struct mlxsw_sp_sb_cm mlxsw_sp2_sb_cms_egress[] = {
8588c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(0, 7),
8598c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(0, 7),
8608c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(0, 7),
8618c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(0, 7),
8628c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(0, 7),
8638c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(0, 7),
8648c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(0, 7),
8658c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(0, 7),
8668c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
8678c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
8688c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
8698c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
8708c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
8718c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
8728c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
8738c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
8748c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM_EGR(1, 0xff),
8758c2ecf20Sopenharmony_ci};
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci#define MLXSW_SP_CPU_PORT_SB_CM MLXSW_SP_SB_CM(0, 0, MLXSW_SP_SB_POOL_EGR_CPU)
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_cistatic const struct mlxsw_sp_sb_cm mlxsw_sp_cpu_port_sb_cms[] = {
8808c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU),
8818c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU),
8828c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU),
8838c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU),
8848c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU),
8858c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU),
8868c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
8878c2ecf20Sopenharmony_ci	MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU),
8888c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
8898c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
8908c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
8918c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
8928c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
8938c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
8948c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
8958c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
8968c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
8978c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
8988c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
8998c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
9008c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
9018c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
9028c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
9038c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
9048c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
9058c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
9068c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
9078c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
9088c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
9098c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
9108c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
9118c2ecf20Sopenharmony_ci	MLXSW_SP_CPU_PORT_SB_CM,
9128c2ecf20Sopenharmony_ci};
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_cistatic bool
9158c2ecf20Sopenharmony_cimlxsw_sp_sb_pool_is_static(struct mlxsw_sp *mlxsw_sp, u16 pool_index)
9168c2ecf20Sopenharmony_ci{
9178c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_pr *pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool_index);
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	return pr->mode == MLXSW_REG_SBPR_MODE_STATIC;
9208c2ecf20Sopenharmony_ci}
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_cistatic int __mlxsw_sp_sb_cms_init(struct mlxsw_sp *mlxsw_sp, u8 local_port,
9238c2ecf20Sopenharmony_ci				  enum mlxsw_reg_sbxx_dir dir,
9248c2ecf20Sopenharmony_ci				  const struct mlxsw_sp_sb_cm *cms,
9258c2ecf20Sopenharmony_ci				  size_t cms_len)
9268c2ecf20Sopenharmony_ci{
9278c2ecf20Sopenharmony_ci	const struct mlxsw_sp_sb_vals *sb_vals = mlxsw_sp->sb_vals;
9288c2ecf20Sopenharmony_ci	int i;
9298c2ecf20Sopenharmony_ci	int err;
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_ci	for (i = 0; i < cms_len; i++) {
9328c2ecf20Sopenharmony_ci		const struct mlxsw_sp_sb_cm *cm;
9338c2ecf20Sopenharmony_ci		u32 min_buff;
9348c2ecf20Sopenharmony_ci		u32 max_buff;
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci		if (i == 8 && dir == MLXSW_REG_SBXX_DIR_INGRESS)
9378c2ecf20Sopenharmony_ci			continue; /* PG number 8 does not exist, skip it */
9388c2ecf20Sopenharmony_ci		cm = &cms[i];
9398c2ecf20Sopenharmony_ci		if (WARN_ON(sb_vals->pool_dess[cm->pool_index].dir != dir))
9408c2ecf20Sopenharmony_ci			continue;
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci		min_buff = mlxsw_sp_bytes_cells(mlxsw_sp, cm->min_buff);
9438c2ecf20Sopenharmony_ci		max_buff = cm->max_buff;
9448c2ecf20Sopenharmony_ci		if (max_buff == MLXSW_SP_SB_INFI) {
9458c2ecf20Sopenharmony_ci			err = mlxsw_sp_sb_cm_write(mlxsw_sp, local_port, i,
9468c2ecf20Sopenharmony_ci						   min_buff, 0,
9478c2ecf20Sopenharmony_ci						   true, cm->pool_index);
9488c2ecf20Sopenharmony_ci		} else {
9498c2ecf20Sopenharmony_ci			if (mlxsw_sp_sb_pool_is_static(mlxsw_sp,
9508c2ecf20Sopenharmony_ci						       cm->pool_index))
9518c2ecf20Sopenharmony_ci				max_buff = mlxsw_sp_bytes_cells(mlxsw_sp,
9528c2ecf20Sopenharmony_ci								max_buff);
9538c2ecf20Sopenharmony_ci			err = mlxsw_sp_sb_cm_write(mlxsw_sp, local_port, i,
9548c2ecf20Sopenharmony_ci						   min_buff, max_buff,
9558c2ecf20Sopenharmony_ci						   false, cm->pool_index);
9568c2ecf20Sopenharmony_ci		}
9578c2ecf20Sopenharmony_ci		if (err)
9588c2ecf20Sopenharmony_ci			return err;
9598c2ecf20Sopenharmony_ci	}
9608c2ecf20Sopenharmony_ci	return 0;
9618c2ecf20Sopenharmony_ci}
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_cistatic int mlxsw_sp_port_sb_cms_init(struct mlxsw_sp_port *mlxsw_sp_port)
9648c2ecf20Sopenharmony_ci{
9658c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
9668c2ecf20Sopenharmony_ci	int err;
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci	err = __mlxsw_sp_sb_cms_init(mlxsw_sp,
9698c2ecf20Sopenharmony_ci				     mlxsw_sp_port->local_port,
9708c2ecf20Sopenharmony_ci				     MLXSW_REG_SBXX_DIR_INGRESS,
9718c2ecf20Sopenharmony_ci				     mlxsw_sp->sb_vals->cms_ingress,
9728c2ecf20Sopenharmony_ci				     mlxsw_sp->sb_vals->cms_ingress_count);
9738c2ecf20Sopenharmony_ci	if (err)
9748c2ecf20Sopenharmony_ci		return err;
9758c2ecf20Sopenharmony_ci	return __mlxsw_sp_sb_cms_init(mlxsw_sp_port->mlxsw_sp,
9768c2ecf20Sopenharmony_ci				      mlxsw_sp_port->local_port,
9778c2ecf20Sopenharmony_ci				      MLXSW_REG_SBXX_DIR_EGRESS,
9788c2ecf20Sopenharmony_ci				      mlxsw_sp->sb_vals->cms_egress,
9798c2ecf20Sopenharmony_ci				      mlxsw_sp->sb_vals->cms_egress_count);
9808c2ecf20Sopenharmony_ci}
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_cistatic int mlxsw_sp_cpu_port_sb_cms_init(struct mlxsw_sp *mlxsw_sp)
9838c2ecf20Sopenharmony_ci{
9848c2ecf20Sopenharmony_ci	return __mlxsw_sp_sb_cms_init(mlxsw_sp, 0, MLXSW_REG_SBXX_DIR_EGRESS,
9858c2ecf20Sopenharmony_ci				      mlxsw_sp->sb_vals->cms_cpu,
9868c2ecf20Sopenharmony_ci				      mlxsw_sp->sb_vals->cms_cpu_count);
9878c2ecf20Sopenharmony_ci}
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_PM(_min_buff, _max_buff)	\
9908c2ecf20Sopenharmony_ci	{					\
9918c2ecf20Sopenharmony_ci		.min_buff = _min_buff,		\
9928c2ecf20Sopenharmony_ci		.max_buff = _max_buff,		\
9938c2ecf20Sopenharmony_ci	}
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci/* Order according to mlxsw_sp1_sb_pool_dess */
9968c2ecf20Sopenharmony_cistatic const struct mlxsw_sp_sb_pm mlxsw_sp1_sb_pms[] = {
9978c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX),
9988c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
9998c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
10008c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
10018c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 7),
10028c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
10038c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
10048c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
10058c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(10000, 90000),
10068c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 8),	/* 50% occupancy */
10078c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
10088c2ecf20Sopenharmony_ci};
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci/* Order according to mlxsw_sp2_sb_pool_dess */
10118c2ecf20Sopenharmony_cistatic const struct mlxsw_sp_sb_pm mlxsw_sp2_sb_pms[] = {
10128c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 7),
10138c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 0),
10148c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 0),
10158c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 0),
10168c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 7),
10178c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 0),
10188c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 0),
10198c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 0),
10208c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(10000, 90000),
10218c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 8),	/* 50% occupancy */
10228c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
10238c2ecf20Sopenharmony_ci};
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_ci/* Order according to mlxsw_sp*_sb_pool_dess */
10268c2ecf20Sopenharmony_cistatic const struct mlxsw_sp_sb_pm mlxsw_sp_cpu_port_sb_pms[] = {
10278c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 0),
10288c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 0),
10298c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 0),
10308c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 0),
10318c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 0),
10328c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 0),
10338c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 0),
10348c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 0),
10358c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 90000),
10368c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, 0),
10378c2ecf20Sopenharmony_ci	MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX),
10388c2ecf20Sopenharmony_ci};
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_cistatic int mlxsw_sp_sb_pms_init(struct mlxsw_sp *mlxsw_sp, u8 local_port,
10418c2ecf20Sopenharmony_ci				const struct mlxsw_sp_sb_pm *pms,
10428c2ecf20Sopenharmony_ci				bool skip_ingress)
10438c2ecf20Sopenharmony_ci{
10448c2ecf20Sopenharmony_ci	int i, err;
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_ci	for (i = 0; i < mlxsw_sp->sb_vals->pool_count; i++) {
10478c2ecf20Sopenharmony_ci		const struct mlxsw_sp_sb_pm *pm = &pms[i];
10488c2ecf20Sopenharmony_ci		const struct mlxsw_sp_sb_pool_des *des;
10498c2ecf20Sopenharmony_ci		u32 max_buff;
10508c2ecf20Sopenharmony_ci		u32 min_buff;
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci		des = &mlxsw_sp->sb_vals->pool_dess[i];
10538c2ecf20Sopenharmony_ci		if (skip_ingress && des->dir == MLXSW_REG_SBXX_DIR_INGRESS)
10548c2ecf20Sopenharmony_ci			continue;
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci		min_buff = mlxsw_sp_bytes_cells(mlxsw_sp, pm->min_buff);
10578c2ecf20Sopenharmony_ci		max_buff = pm->max_buff;
10588c2ecf20Sopenharmony_ci		if (mlxsw_sp_sb_pool_is_static(mlxsw_sp, i))
10598c2ecf20Sopenharmony_ci			max_buff = mlxsw_sp_bytes_cells(mlxsw_sp, max_buff);
10608c2ecf20Sopenharmony_ci		err = mlxsw_sp_sb_pm_write(mlxsw_sp, local_port, i, min_buff,
10618c2ecf20Sopenharmony_ci					   max_buff);
10628c2ecf20Sopenharmony_ci		if (err)
10638c2ecf20Sopenharmony_ci			return err;
10648c2ecf20Sopenharmony_ci	}
10658c2ecf20Sopenharmony_ci	return 0;
10668c2ecf20Sopenharmony_ci}
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_cistatic int mlxsw_sp_port_sb_pms_init(struct mlxsw_sp_port *mlxsw_sp_port)
10698c2ecf20Sopenharmony_ci{
10708c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	return mlxsw_sp_sb_pms_init(mlxsw_sp, mlxsw_sp_port->local_port,
10738c2ecf20Sopenharmony_ci				    mlxsw_sp->sb_vals->pms, false);
10748c2ecf20Sopenharmony_ci}
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_cistatic int mlxsw_sp_cpu_port_sb_pms_init(struct mlxsw_sp *mlxsw_sp)
10778c2ecf20Sopenharmony_ci{
10788c2ecf20Sopenharmony_ci	return mlxsw_sp_sb_pms_init(mlxsw_sp, 0, mlxsw_sp->sb_vals->pms_cpu,
10798c2ecf20Sopenharmony_ci				    true);
10808c2ecf20Sopenharmony_ci}
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_MM(_min_buff, _max_buff)		\
10838c2ecf20Sopenharmony_ci	{						\
10848c2ecf20Sopenharmony_ci		.min_buff = _min_buff,			\
10858c2ecf20Sopenharmony_ci		.max_buff = _max_buff,			\
10868c2ecf20Sopenharmony_ci		.pool_index = MLXSW_SP_SB_POOL_EGR,	\
10878c2ecf20Sopenharmony_ci	}
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_cistatic const struct mlxsw_sp_sb_mm mlxsw_sp_sb_mms[] = {
10908c2ecf20Sopenharmony_ci	MLXSW_SP_SB_MM(0, 6),
10918c2ecf20Sopenharmony_ci	MLXSW_SP_SB_MM(0, 6),
10928c2ecf20Sopenharmony_ci	MLXSW_SP_SB_MM(0, 6),
10938c2ecf20Sopenharmony_ci	MLXSW_SP_SB_MM(0, 6),
10948c2ecf20Sopenharmony_ci	MLXSW_SP_SB_MM(0, 6),
10958c2ecf20Sopenharmony_ci	MLXSW_SP_SB_MM(0, 6),
10968c2ecf20Sopenharmony_ci	MLXSW_SP_SB_MM(0, 6),
10978c2ecf20Sopenharmony_ci	MLXSW_SP_SB_MM(0, 6),
10988c2ecf20Sopenharmony_ci	MLXSW_SP_SB_MM(0, 6),
10998c2ecf20Sopenharmony_ci	MLXSW_SP_SB_MM(0, 6),
11008c2ecf20Sopenharmony_ci	MLXSW_SP_SB_MM(0, 6),
11018c2ecf20Sopenharmony_ci	MLXSW_SP_SB_MM(0, 6),
11028c2ecf20Sopenharmony_ci	MLXSW_SP_SB_MM(0, 6),
11038c2ecf20Sopenharmony_ci	MLXSW_SP_SB_MM(0, 6),
11048c2ecf20Sopenharmony_ci	MLXSW_SP_SB_MM(0, 6),
11058c2ecf20Sopenharmony_ci};
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_cistatic int mlxsw_sp_sb_mms_init(struct mlxsw_sp *mlxsw_sp)
11088c2ecf20Sopenharmony_ci{
11098c2ecf20Sopenharmony_ci	char sbmm_pl[MLXSW_REG_SBMM_LEN];
11108c2ecf20Sopenharmony_ci	int i;
11118c2ecf20Sopenharmony_ci	int err;
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci	for (i = 0; i < mlxsw_sp->sb_vals->mms_count; i++) {
11148c2ecf20Sopenharmony_ci		const struct mlxsw_sp_sb_pool_des *des;
11158c2ecf20Sopenharmony_ci		const struct mlxsw_sp_sb_mm *mc;
11168c2ecf20Sopenharmony_ci		u32 min_buff;
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci		mc = &mlxsw_sp->sb_vals->mms[i];
11198c2ecf20Sopenharmony_ci		des = &mlxsw_sp->sb_vals->pool_dess[mc->pool_index];
11208c2ecf20Sopenharmony_ci		/* All pools used by sb_mm's are initialized using dynamic
11218c2ecf20Sopenharmony_ci		 * thresholds, therefore 'max_buff' isn't specified in cells.
11228c2ecf20Sopenharmony_ci		 */
11238c2ecf20Sopenharmony_ci		min_buff = mlxsw_sp_bytes_cells(mlxsw_sp, mc->min_buff);
11248c2ecf20Sopenharmony_ci		mlxsw_reg_sbmm_pack(sbmm_pl, i, min_buff, mc->max_buff,
11258c2ecf20Sopenharmony_ci				    des->pool);
11268c2ecf20Sopenharmony_ci		err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbmm), sbmm_pl);
11278c2ecf20Sopenharmony_ci		if (err)
11288c2ecf20Sopenharmony_ci			return err;
11298c2ecf20Sopenharmony_ci	}
11308c2ecf20Sopenharmony_ci	return 0;
11318c2ecf20Sopenharmony_ci}
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_cistatic void mlxsw_sp_pool_count(struct mlxsw_sp *mlxsw_sp,
11348c2ecf20Sopenharmony_ci				u16 *p_ingress_len, u16 *p_egress_len)
11358c2ecf20Sopenharmony_ci{
11368c2ecf20Sopenharmony_ci	int i;
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci	for (i = 0; i < mlxsw_sp->sb_vals->pool_count; ++i) {
11398c2ecf20Sopenharmony_ci		if (mlxsw_sp->sb_vals->pool_dess[i].dir ==
11408c2ecf20Sopenharmony_ci		    MLXSW_REG_SBXX_DIR_INGRESS)
11418c2ecf20Sopenharmony_ci			(*p_ingress_len)++;
11428c2ecf20Sopenharmony_ci		else
11438c2ecf20Sopenharmony_ci			(*p_egress_len)++;
11448c2ecf20Sopenharmony_ci	}
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci	WARN(*p_egress_len == 0, "No egress pools\n");
11478c2ecf20Sopenharmony_ci}
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_ciconst struct mlxsw_sp_sb_vals mlxsw_sp1_sb_vals = {
11508c2ecf20Sopenharmony_ci	.pool_count = ARRAY_SIZE(mlxsw_sp1_sb_pool_dess),
11518c2ecf20Sopenharmony_ci	.pool_dess = mlxsw_sp1_sb_pool_dess,
11528c2ecf20Sopenharmony_ci	.pms = mlxsw_sp1_sb_pms,
11538c2ecf20Sopenharmony_ci	.pms_cpu = mlxsw_sp_cpu_port_sb_pms,
11548c2ecf20Sopenharmony_ci	.prs = mlxsw_sp1_sb_prs,
11558c2ecf20Sopenharmony_ci	.mms = mlxsw_sp_sb_mms,
11568c2ecf20Sopenharmony_ci	.cms_ingress = mlxsw_sp1_sb_cms_ingress,
11578c2ecf20Sopenharmony_ci	.cms_egress = mlxsw_sp1_sb_cms_egress,
11588c2ecf20Sopenharmony_ci	.cms_cpu = mlxsw_sp_cpu_port_sb_cms,
11598c2ecf20Sopenharmony_ci	.mms_count = ARRAY_SIZE(mlxsw_sp_sb_mms),
11608c2ecf20Sopenharmony_ci	.cms_ingress_count = ARRAY_SIZE(mlxsw_sp1_sb_cms_ingress),
11618c2ecf20Sopenharmony_ci	.cms_egress_count = ARRAY_SIZE(mlxsw_sp1_sb_cms_egress),
11628c2ecf20Sopenharmony_ci	.cms_cpu_count = ARRAY_SIZE(mlxsw_sp_cpu_port_sb_cms),
11638c2ecf20Sopenharmony_ci};
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_ciconst struct mlxsw_sp_sb_vals mlxsw_sp2_sb_vals = {
11668c2ecf20Sopenharmony_ci	.pool_count = ARRAY_SIZE(mlxsw_sp2_sb_pool_dess),
11678c2ecf20Sopenharmony_ci	.pool_dess = mlxsw_sp2_sb_pool_dess,
11688c2ecf20Sopenharmony_ci	.pms = mlxsw_sp2_sb_pms,
11698c2ecf20Sopenharmony_ci	.pms_cpu = mlxsw_sp_cpu_port_sb_pms,
11708c2ecf20Sopenharmony_ci	.prs = mlxsw_sp2_sb_prs,
11718c2ecf20Sopenharmony_ci	.mms = mlxsw_sp_sb_mms,
11728c2ecf20Sopenharmony_ci	.cms_ingress = mlxsw_sp2_sb_cms_ingress,
11738c2ecf20Sopenharmony_ci	.cms_egress = mlxsw_sp2_sb_cms_egress,
11748c2ecf20Sopenharmony_ci	.cms_cpu = mlxsw_sp_cpu_port_sb_cms,
11758c2ecf20Sopenharmony_ci	.mms_count = ARRAY_SIZE(mlxsw_sp_sb_mms),
11768c2ecf20Sopenharmony_ci	.cms_ingress_count = ARRAY_SIZE(mlxsw_sp2_sb_cms_ingress),
11778c2ecf20Sopenharmony_ci	.cms_egress_count = ARRAY_SIZE(mlxsw_sp2_sb_cms_egress),
11788c2ecf20Sopenharmony_ci	.cms_cpu_count = ARRAY_SIZE(mlxsw_sp_cpu_port_sb_cms),
11798c2ecf20Sopenharmony_ci};
11808c2ecf20Sopenharmony_ci
11818c2ecf20Sopenharmony_cistatic u32 mlxsw_sp1_pb_int_buf_size_get(int mtu, u32 speed)
11828c2ecf20Sopenharmony_ci{
11838c2ecf20Sopenharmony_ci	return mtu * 5 / 2;
11848c2ecf20Sopenharmony_ci}
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_cistatic u32 __mlxsw_sp_pb_int_buf_size_get(int mtu, u32 speed, u32 buffer_factor)
11878c2ecf20Sopenharmony_ci{
11888c2ecf20Sopenharmony_ci	return 3 * mtu + buffer_factor * speed / 1000;
11898c2ecf20Sopenharmony_ci}
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci#define MLXSW_SP2_SPAN_EG_MIRROR_BUFFER_FACTOR 38
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_cistatic u32 mlxsw_sp2_pb_int_buf_size_get(int mtu, u32 speed)
11948c2ecf20Sopenharmony_ci{
11958c2ecf20Sopenharmony_ci	int factor = MLXSW_SP2_SPAN_EG_MIRROR_BUFFER_FACTOR;
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_ci	return __mlxsw_sp_pb_int_buf_size_get(mtu, speed, factor);
11988c2ecf20Sopenharmony_ci}
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci#define MLXSW_SP3_SPAN_EG_MIRROR_BUFFER_FACTOR 50
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_cistatic u32 mlxsw_sp3_pb_int_buf_size_get(int mtu, u32 speed)
12038c2ecf20Sopenharmony_ci{
12048c2ecf20Sopenharmony_ci	int factor = MLXSW_SP3_SPAN_EG_MIRROR_BUFFER_FACTOR;
12058c2ecf20Sopenharmony_ci
12068c2ecf20Sopenharmony_ci	return __mlxsw_sp_pb_int_buf_size_get(mtu, speed, factor);
12078c2ecf20Sopenharmony_ci}
12088c2ecf20Sopenharmony_ci
12098c2ecf20Sopenharmony_ciconst struct mlxsw_sp_sb_ops mlxsw_sp1_sb_ops = {
12108c2ecf20Sopenharmony_ci	.int_buf_size_get = mlxsw_sp1_pb_int_buf_size_get,
12118c2ecf20Sopenharmony_ci};
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ciconst struct mlxsw_sp_sb_ops mlxsw_sp2_sb_ops = {
12148c2ecf20Sopenharmony_ci	.int_buf_size_get = mlxsw_sp2_pb_int_buf_size_get,
12158c2ecf20Sopenharmony_ci};
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ciconst struct mlxsw_sp_sb_ops mlxsw_sp3_sb_ops = {
12188c2ecf20Sopenharmony_ci	.int_buf_size_get = mlxsw_sp3_pb_int_buf_size_get,
12198c2ecf20Sopenharmony_ci};
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_ciint mlxsw_sp_buffers_init(struct mlxsw_sp *mlxsw_sp)
12228c2ecf20Sopenharmony_ci{
12238c2ecf20Sopenharmony_ci	u32 max_headroom_size;
12248c2ecf20Sopenharmony_ci	u16 ing_pool_count = 0;
12258c2ecf20Sopenharmony_ci	u16 eg_pool_count = 0;
12268c2ecf20Sopenharmony_ci	int err;
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_ci	if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, CELL_SIZE))
12298c2ecf20Sopenharmony_ci		return -EIO;
12308c2ecf20Sopenharmony_ci
12318c2ecf20Sopenharmony_ci	if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, GUARANTEED_SHARED_BUFFER))
12328c2ecf20Sopenharmony_ci		return -EIO;
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_HEADROOM_SIZE))
12358c2ecf20Sopenharmony_ci		return -EIO;
12368c2ecf20Sopenharmony_ci
12378c2ecf20Sopenharmony_ci	mlxsw_sp->sb = kzalloc(sizeof(*mlxsw_sp->sb), GFP_KERNEL);
12388c2ecf20Sopenharmony_ci	if (!mlxsw_sp->sb)
12398c2ecf20Sopenharmony_ci		return -ENOMEM;
12408c2ecf20Sopenharmony_ci	mlxsw_sp->sb->cell_size = MLXSW_CORE_RES_GET(mlxsw_sp->core, CELL_SIZE);
12418c2ecf20Sopenharmony_ci	mlxsw_sp->sb->sb_size = MLXSW_CORE_RES_GET(mlxsw_sp->core,
12428c2ecf20Sopenharmony_ci						   GUARANTEED_SHARED_BUFFER);
12438c2ecf20Sopenharmony_ci	max_headroom_size = MLXSW_CORE_RES_GET(mlxsw_sp->core,
12448c2ecf20Sopenharmony_ci					       MAX_HEADROOM_SIZE);
12458c2ecf20Sopenharmony_ci	/* Round down, because this limit must not be overstepped. */
12468c2ecf20Sopenharmony_ci	mlxsw_sp->sb->max_headroom_cells = max_headroom_size /
12478c2ecf20Sopenharmony_ci						mlxsw_sp->sb->cell_size;
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci	err = mlxsw_sp_sb_ports_init(mlxsw_sp);
12508c2ecf20Sopenharmony_ci	if (err)
12518c2ecf20Sopenharmony_ci		goto err_sb_ports_init;
12528c2ecf20Sopenharmony_ci	err = mlxsw_sp_sb_prs_init(mlxsw_sp, mlxsw_sp->sb_vals->prs,
12538c2ecf20Sopenharmony_ci				   mlxsw_sp->sb_vals->pool_dess,
12548c2ecf20Sopenharmony_ci				   mlxsw_sp->sb_vals->pool_count);
12558c2ecf20Sopenharmony_ci	if (err)
12568c2ecf20Sopenharmony_ci		goto err_sb_prs_init;
12578c2ecf20Sopenharmony_ci	err = mlxsw_sp_cpu_port_sb_cms_init(mlxsw_sp);
12588c2ecf20Sopenharmony_ci	if (err)
12598c2ecf20Sopenharmony_ci		goto err_sb_cpu_port_sb_cms_init;
12608c2ecf20Sopenharmony_ci	err = mlxsw_sp_cpu_port_sb_pms_init(mlxsw_sp);
12618c2ecf20Sopenharmony_ci	if (err)
12628c2ecf20Sopenharmony_ci		goto err_sb_cpu_port_pms_init;
12638c2ecf20Sopenharmony_ci	err = mlxsw_sp_sb_mms_init(mlxsw_sp);
12648c2ecf20Sopenharmony_ci	if (err)
12658c2ecf20Sopenharmony_ci		goto err_sb_mms_init;
12668c2ecf20Sopenharmony_ci	mlxsw_sp_pool_count(mlxsw_sp, &ing_pool_count, &eg_pool_count);
12678c2ecf20Sopenharmony_ci	err = devlink_sb_register(priv_to_devlink(mlxsw_sp->core), 0,
12688c2ecf20Sopenharmony_ci				  mlxsw_sp->sb->sb_size,
12698c2ecf20Sopenharmony_ci				  ing_pool_count,
12708c2ecf20Sopenharmony_ci				  eg_pool_count,
12718c2ecf20Sopenharmony_ci				  MLXSW_SP_SB_ING_TC_COUNT,
12728c2ecf20Sopenharmony_ci				  MLXSW_SP_SB_EG_TC_COUNT);
12738c2ecf20Sopenharmony_ci	if (err)
12748c2ecf20Sopenharmony_ci		goto err_devlink_sb_register;
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci	return 0;
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_cierr_devlink_sb_register:
12798c2ecf20Sopenharmony_cierr_sb_mms_init:
12808c2ecf20Sopenharmony_cierr_sb_cpu_port_pms_init:
12818c2ecf20Sopenharmony_cierr_sb_cpu_port_sb_cms_init:
12828c2ecf20Sopenharmony_cierr_sb_prs_init:
12838c2ecf20Sopenharmony_ci	mlxsw_sp_sb_ports_fini(mlxsw_sp);
12848c2ecf20Sopenharmony_cierr_sb_ports_init:
12858c2ecf20Sopenharmony_ci	kfree(mlxsw_sp->sb);
12868c2ecf20Sopenharmony_ci	return err;
12878c2ecf20Sopenharmony_ci}
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_civoid mlxsw_sp_buffers_fini(struct mlxsw_sp *mlxsw_sp)
12908c2ecf20Sopenharmony_ci{
12918c2ecf20Sopenharmony_ci	devlink_sb_unregister(priv_to_devlink(mlxsw_sp->core), 0);
12928c2ecf20Sopenharmony_ci	mlxsw_sp_sb_ports_fini(mlxsw_sp);
12938c2ecf20Sopenharmony_ci	kfree(mlxsw_sp->sb);
12948c2ecf20Sopenharmony_ci}
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ciint mlxsw_sp_port_buffers_init(struct mlxsw_sp_port *mlxsw_sp_port)
12978c2ecf20Sopenharmony_ci{
12988c2ecf20Sopenharmony_ci	int err;
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ci	mlxsw_sp_port->hdroom = kzalloc(sizeof(*mlxsw_sp_port->hdroom), GFP_KERNEL);
13018c2ecf20Sopenharmony_ci	if (!mlxsw_sp_port->hdroom)
13028c2ecf20Sopenharmony_ci		return -ENOMEM;
13038c2ecf20Sopenharmony_ci	mlxsw_sp_port->hdroom->mtu = mlxsw_sp_port->dev->mtu;
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci	err = mlxsw_sp_port_headroom_init(mlxsw_sp_port);
13068c2ecf20Sopenharmony_ci	if (err)
13078c2ecf20Sopenharmony_ci		goto err_headroom_init;
13088c2ecf20Sopenharmony_ci	err = mlxsw_sp_port_sb_cms_init(mlxsw_sp_port);
13098c2ecf20Sopenharmony_ci	if (err)
13108c2ecf20Sopenharmony_ci		goto err_port_sb_cms_init;
13118c2ecf20Sopenharmony_ci	err = mlxsw_sp_port_sb_pms_init(mlxsw_sp_port);
13128c2ecf20Sopenharmony_ci	if (err)
13138c2ecf20Sopenharmony_ci		goto err_port_sb_pms_init;
13148c2ecf20Sopenharmony_ci	return 0;
13158c2ecf20Sopenharmony_ci
13168c2ecf20Sopenharmony_cierr_port_sb_pms_init:
13178c2ecf20Sopenharmony_cierr_port_sb_cms_init:
13188c2ecf20Sopenharmony_cierr_headroom_init:
13198c2ecf20Sopenharmony_ci	kfree(mlxsw_sp_port->hdroom);
13208c2ecf20Sopenharmony_ci	return err;
13218c2ecf20Sopenharmony_ci}
13228c2ecf20Sopenharmony_ci
13238c2ecf20Sopenharmony_civoid mlxsw_sp_port_buffers_fini(struct mlxsw_sp_port *mlxsw_sp_port)
13248c2ecf20Sopenharmony_ci{
13258c2ecf20Sopenharmony_ci	kfree(mlxsw_sp_port->hdroom);
13268c2ecf20Sopenharmony_ci}
13278c2ecf20Sopenharmony_ci
13288c2ecf20Sopenharmony_ciint mlxsw_sp_sb_pool_get(struct mlxsw_core *mlxsw_core,
13298c2ecf20Sopenharmony_ci			 unsigned int sb_index, u16 pool_index,
13308c2ecf20Sopenharmony_ci			 struct devlink_sb_pool_info *pool_info)
13318c2ecf20Sopenharmony_ci{
13328c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
13338c2ecf20Sopenharmony_ci	enum mlxsw_reg_sbxx_dir dir;
13348c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_pr *pr;
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_ci	dir = mlxsw_sp->sb_vals->pool_dess[pool_index].dir;
13378c2ecf20Sopenharmony_ci	pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool_index);
13388c2ecf20Sopenharmony_ci	pool_info->pool_type = (enum devlink_sb_pool_type) dir;
13398c2ecf20Sopenharmony_ci	pool_info->size = mlxsw_sp_cells_bytes(mlxsw_sp, pr->size);
13408c2ecf20Sopenharmony_ci	pool_info->threshold_type = (enum devlink_sb_threshold_type) pr->mode;
13418c2ecf20Sopenharmony_ci	pool_info->cell_size = mlxsw_sp->sb->cell_size;
13428c2ecf20Sopenharmony_ci	return 0;
13438c2ecf20Sopenharmony_ci}
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_ciint mlxsw_sp_sb_pool_set(struct mlxsw_core *mlxsw_core,
13468c2ecf20Sopenharmony_ci			 unsigned int sb_index, u16 pool_index, u32 size,
13478c2ecf20Sopenharmony_ci			 enum devlink_sb_threshold_type threshold_type,
13488c2ecf20Sopenharmony_ci			 struct netlink_ext_ack *extack)
13498c2ecf20Sopenharmony_ci{
13508c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
13518c2ecf20Sopenharmony_ci	u32 pool_size = mlxsw_sp_bytes_cells(mlxsw_sp, size);
13528c2ecf20Sopenharmony_ci	const struct mlxsw_sp_sb_pr *pr;
13538c2ecf20Sopenharmony_ci	enum mlxsw_reg_sbpr_mode mode;
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_ci	mode = (enum mlxsw_reg_sbpr_mode) threshold_type;
13568c2ecf20Sopenharmony_ci	pr = &mlxsw_sp->sb_vals->prs[pool_index];
13578c2ecf20Sopenharmony_ci
13588c2ecf20Sopenharmony_ci	if (size > MLXSW_CORE_RES_GET(mlxsw_sp->core,
13598c2ecf20Sopenharmony_ci				      GUARANTEED_SHARED_BUFFER)) {
13608c2ecf20Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Exceeded shared buffer size");
13618c2ecf20Sopenharmony_ci		return -EINVAL;
13628c2ecf20Sopenharmony_ci	}
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_ci	if (pr->freeze_mode && pr->mode != mode) {
13658c2ecf20Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Changing this pool's threshold type is forbidden");
13668c2ecf20Sopenharmony_ci		return -EINVAL;
13678c2ecf20Sopenharmony_ci	}
13688c2ecf20Sopenharmony_ci
13698c2ecf20Sopenharmony_ci	if (pr->freeze_size && pr->size != size) {
13708c2ecf20Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Changing this pool's size is forbidden");
13718c2ecf20Sopenharmony_ci		return -EINVAL;
13728c2ecf20Sopenharmony_ci	}
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci	return mlxsw_sp_sb_pr_write(mlxsw_sp, pool_index, mode,
13758c2ecf20Sopenharmony_ci				    pool_size, false);
13768c2ecf20Sopenharmony_ci}
13778c2ecf20Sopenharmony_ci
13788c2ecf20Sopenharmony_ci#define MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET (-2) /* 3->1, 16->14 */
13798c2ecf20Sopenharmony_ci
13808c2ecf20Sopenharmony_cistatic u32 mlxsw_sp_sb_threshold_out(struct mlxsw_sp *mlxsw_sp, u16 pool_index,
13818c2ecf20Sopenharmony_ci				     u32 max_buff)
13828c2ecf20Sopenharmony_ci{
13838c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_pr *pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool_index);
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_ci	if (pr->mode == MLXSW_REG_SBPR_MODE_DYNAMIC)
13868c2ecf20Sopenharmony_ci		return max_buff - MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET;
13878c2ecf20Sopenharmony_ci	return mlxsw_sp_cells_bytes(mlxsw_sp, max_buff);
13888c2ecf20Sopenharmony_ci}
13898c2ecf20Sopenharmony_ci
13908c2ecf20Sopenharmony_cistatic int mlxsw_sp_sb_threshold_in(struct mlxsw_sp *mlxsw_sp, u16 pool_index,
13918c2ecf20Sopenharmony_ci				    u32 threshold, u32 *p_max_buff,
13928c2ecf20Sopenharmony_ci				    struct netlink_ext_ack *extack)
13938c2ecf20Sopenharmony_ci{
13948c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_pr *pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool_index);
13958c2ecf20Sopenharmony_ci
13968c2ecf20Sopenharmony_ci	if (pr->mode == MLXSW_REG_SBPR_MODE_DYNAMIC) {
13978c2ecf20Sopenharmony_ci		int val;
13988c2ecf20Sopenharmony_ci
13998c2ecf20Sopenharmony_ci		val = threshold + MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET;
14008c2ecf20Sopenharmony_ci		if (val < MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN ||
14018c2ecf20Sopenharmony_ci		    val > MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX) {
14028c2ecf20Sopenharmony_ci			NL_SET_ERR_MSG_MOD(extack, "Invalid dynamic threshold value");
14038c2ecf20Sopenharmony_ci			return -EINVAL;
14048c2ecf20Sopenharmony_ci		}
14058c2ecf20Sopenharmony_ci		*p_max_buff = val;
14068c2ecf20Sopenharmony_ci	} else {
14078c2ecf20Sopenharmony_ci		*p_max_buff = mlxsw_sp_bytes_cells(mlxsw_sp, threshold);
14088c2ecf20Sopenharmony_ci	}
14098c2ecf20Sopenharmony_ci	return 0;
14108c2ecf20Sopenharmony_ci}
14118c2ecf20Sopenharmony_ci
14128c2ecf20Sopenharmony_ciint mlxsw_sp_sb_port_pool_get(struct mlxsw_core_port *mlxsw_core_port,
14138c2ecf20Sopenharmony_ci			      unsigned int sb_index, u16 pool_index,
14148c2ecf20Sopenharmony_ci			      u32 *p_threshold)
14158c2ecf20Sopenharmony_ci{
14168c2ecf20Sopenharmony_ci	struct mlxsw_sp_port *mlxsw_sp_port =
14178c2ecf20Sopenharmony_ci			mlxsw_core_port_driver_priv(mlxsw_core_port);
14188c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
14198c2ecf20Sopenharmony_ci	u8 local_port = mlxsw_sp_port->local_port;
14208c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_pm *pm = mlxsw_sp_sb_pm_get(mlxsw_sp, local_port,
14218c2ecf20Sopenharmony_ci						       pool_index);
14228c2ecf20Sopenharmony_ci
14238c2ecf20Sopenharmony_ci	*p_threshold = mlxsw_sp_sb_threshold_out(mlxsw_sp, pool_index,
14248c2ecf20Sopenharmony_ci						 pm->max_buff);
14258c2ecf20Sopenharmony_ci	return 0;
14268c2ecf20Sopenharmony_ci}
14278c2ecf20Sopenharmony_ci
14288c2ecf20Sopenharmony_ciint mlxsw_sp_sb_port_pool_set(struct mlxsw_core_port *mlxsw_core_port,
14298c2ecf20Sopenharmony_ci			      unsigned int sb_index, u16 pool_index,
14308c2ecf20Sopenharmony_ci			      u32 threshold, struct netlink_ext_ack *extack)
14318c2ecf20Sopenharmony_ci{
14328c2ecf20Sopenharmony_ci	struct mlxsw_sp_port *mlxsw_sp_port =
14338c2ecf20Sopenharmony_ci			mlxsw_core_port_driver_priv(mlxsw_core_port);
14348c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
14358c2ecf20Sopenharmony_ci	u8 local_port = mlxsw_sp_port->local_port;
14368c2ecf20Sopenharmony_ci	u32 max_buff;
14378c2ecf20Sopenharmony_ci	int err;
14388c2ecf20Sopenharmony_ci
14398c2ecf20Sopenharmony_ci	if (local_port == MLXSW_PORT_CPU_PORT) {
14408c2ecf20Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Changing CPU port's threshold is forbidden");
14418c2ecf20Sopenharmony_ci		return -EINVAL;
14428c2ecf20Sopenharmony_ci	}
14438c2ecf20Sopenharmony_ci
14448c2ecf20Sopenharmony_ci	err = mlxsw_sp_sb_threshold_in(mlxsw_sp, pool_index,
14458c2ecf20Sopenharmony_ci				       threshold, &max_buff, extack);
14468c2ecf20Sopenharmony_ci	if (err)
14478c2ecf20Sopenharmony_ci		return err;
14488c2ecf20Sopenharmony_ci
14498c2ecf20Sopenharmony_ci	return mlxsw_sp_sb_pm_write(mlxsw_sp, local_port, pool_index,
14508c2ecf20Sopenharmony_ci				    0, max_buff);
14518c2ecf20Sopenharmony_ci}
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ciint mlxsw_sp_sb_tc_pool_bind_get(struct mlxsw_core_port *mlxsw_core_port,
14548c2ecf20Sopenharmony_ci				 unsigned int sb_index, u16 tc_index,
14558c2ecf20Sopenharmony_ci				 enum devlink_sb_pool_type pool_type,
14568c2ecf20Sopenharmony_ci				 u16 *p_pool_index, u32 *p_threshold)
14578c2ecf20Sopenharmony_ci{
14588c2ecf20Sopenharmony_ci	struct mlxsw_sp_port *mlxsw_sp_port =
14598c2ecf20Sopenharmony_ci			mlxsw_core_port_driver_priv(mlxsw_core_port);
14608c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
14618c2ecf20Sopenharmony_ci	u8 local_port = mlxsw_sp_port->local_port;
14628c2ecf20Sopenharmony_ci	u8 pg_buff = tc_index;
14638c2ecf20Sopenharmony_ci	enum mlxsw_reg_sbxx_dir dir = (enum mlxsw_reg_sbxx_dir) pool_type;
14648c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_cm *cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port,
14658c2ecf20Sopenharmony_ci						       pg_buff, dir);
14668c2ecf20Sopenharmony_ci
14678c2ecf20Sopenharmony_ci	*p_threshold = mlxsw_sp_sb_threshold_out(mlxsw_sp, cm->pool_index,
14688c2ecf20Sopenharmony_ci						 cm->max_buff);
14698c2ecf20Sopenharmony_ci	*p_pool_index = cm->pool_index;
14708c2ecf20Sopenharmony_ci	return 0;
14718c2ecf20Sopenharmony_ci}
14728c2ecf20Sopenharmony_ci
14738c2ecf20Sopenharmony_ciint mlxsw_sp_sb_tc_pool_bind_set(struct mlxsw_core_port *mlxsw_core_port,
14748c2ecf20Sopenharmony_ci				 unsigned int sb_index, u16 tc_index,
14758c2ecf20Sopenharmony_ci				 enum devlink_sb_pool_type pool_type,
14768c2ecf20Sopenharmony_ci				 u16 pool_index, u32 threshold,
14778c2ecf20Sopenharmony_ci				 struct netlink_ext_ack *extack)
14788c2ecf20Sopenharmony_ci{
14798c2ecf20Sopenharmony_ci	struct mlxsw_sp_port *mlxsw_sp_port =
14808c2ecf20Sopenharmony_ci			mlxsw_core_port_driver_priv(mlxsw_core_port);
14818c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
14828c2ecf20Sopenharmony_ci	u8 local_port = mlxsw_sp_port->local_port;
14838c2ecf20Sopenharmony_ci	const struct mlxsw_sp_sb_cm *cm;
14848c2ecf20Sopenharmony_ci	u8 pg_buff = tc_index;
14858c2ecf20Sopenharmony_ci	enum mlxsw_reg_sbxx_dir dir = (enum mlxsw_reg_sbxx_dir) pool_type;
14868c2ecf20Sopenharmony_ci	u32 max_buff;
14878c2ecf20Sopenharmony_ci	int err;
14888c2ecf20Sopenharmony_ci
14898c2ecf20Sopenharmony_ci	if (local_port == MLXSW_PORT_CPU_PORT) {
14908c2ecf20Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Changing CPU port's binding is forbidden");
14918c2ecf20Sopenharmony_ci		return -EINVAL;
14928c2ecf20Sopenharmony_ci	}
14938c2ecf20Sopenharmony_ci
14948c2ecf20Sopenharmony_ci	if (dir != mlxsw_sp->sb_vals->pool_dess[pool_index].dir) {
14958c2ecf20Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Binding egress TC to ingress pool and vice versa is forbidden");
14968c2ecf20Sopenharmony_ci		return -EINVAL;
14978c2ecf20Sopenharmony_ci	}
14988c2ecf20Sopenharmony_ci
14998c2ecf20Sopenharmony_ci	if (dir == MLXSW_REG_SBXX_DIR_INGRESS)
15008c2ecf20Sopenharmony_ci		cm = &mlxsw_sp->sb_vals->cms_ingress[tc_index];
15018c2ecf20Sopenharmony_ci	else
15028c2ecf20Sopenharmony_ci		cm = &mlxsw_sp->sb_vals->cms_egress[tc_index];
15038c2ecf20Sopenharmony_ci
15048c2ecf20Sopenharmony_ci	if (cm->freeze_pool && cm->pool_index != pool_index) {
15058c2ecf20Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Binding this TC to a different pool is forbidden");
15068c2ecf20Sopenharmony_ci		return -EINVAL;
15078c2ecf20Sopenharmony_ci	}
15088c2ecf20Sopenharmony_ci
15098c2ecf20Sopenharmony_ci	if (cm->freeze_thresh && cm->max_buff != threshold) {
15108c2ecf20Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Changing this TC's threshold is forbidden");
15118c2ecf20Sopenharmony_ci		return -EINVAL;
15128c2ecf20Sopenharmony_ci	}
15138c2ecf20Sopenharmony_ci
15148c2ecf20Sopenharmony_ci	err = mlxsw_sp_sb_threshold_in(mlxsw_sp, pool_index,
15158c2ecf20Sopenharmony_ci				       threshold, &max_buff, extack);
15168c2ecf20Sopenharmony_ci	if (err)
15178c2ecf20Sopenharmony_ci		return err;
15188c2ecf20Sopenharmony_ci
15198c2ecf20Sopenharmony_ci	return mlxsw_sp_sb_cm_write(mlxsw_sp, local_port, pg_buff,
15208c2ecf20Sopenharmony_ci				    0, max_buff, false, pool_index);
15218c2ecf20Sopenharmony_ci}
15228c2ecf20Sopenharmony_ci
15238c2ecf20Sopenharmony_ci#define MASKED_COUNT_MAX \
15248c2ecf20Sopenharmony_ci	(MLXSW_REG_SBSR_REC_MAX_COUNT / \
15258c2ecf20Sopenharmony_ci	 (MLXSW_SP_SB_ING_TC_COUNT + MLXSW_SP_SB_EG_TC_COUNT))
15268c2ecf20Sopenharmony_ci
15278c2ecf20Sopenharmony_cistruct mlxsw_sp_sb_sr_occ_query_cb_ctx {
15288c2ecf20Sopenharmony_ci	u8 masked_count;
15298c2ecf20Sopenharmony_ci	u8 local_port_1;
15308c2ecf20Sopenharmony_ci};
15318c2ecf20Sopenharmony_ci
15328c2ecf20Sopenharmony_cistatic void mlxsw_sp_sb_sr_occ_query_cb(struct mlxsw_core *mlxsw_core,
15338c2ecf20Sopenharmony_ci					char *sbsr_pl, size_t sbsr_pl_len,
15348c2ecf20Sopenharmony_ci					unsigned long cb_priv)
15358c2ecf20Sopenharmony_ci{
15368c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
15378c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_sr_occ_query_cb_ctx cb_ctx;
15388c2ecf20Sopenharmony_ci	u8 masked_count;
15398c2ecf20Sopenharmony_ci	u8 local_port;
15408c2ecf20Sopenharmony_ci	int rec_index = 0;
15418c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_cm *cm;
15428c2ecf20Sopenharmony_ci	int i;
15438c2ecf20Sopenharmony_ci
15448c2ecf20Sopenharmony_ci	memcpy(&cb_ctx, &cb_priv, sizeof(cb_ctx));
15458c2ecf20Sopenharmony_ci
15468c2ecf20Sopenharmony_ci	masked_count = 0;
15478c2ecf20Sopenharmony_ci	for (local_port = cb_ctx.local_port_1;
15488c2ecf20Sopenharmony_ci	     local_port < mlxsw_core_max_ports(mlxsw_core); local_port++) {
15498c2ecf20Sopenharmony_ci		if (!mlxsw_sp->ports[local_port])
15508c2ecf20Sopenharmony_ci			continue;
15518c2ecf20Sopenharmony_ci		if (local_port == MLXSW_PORT_CPU_PORT) {
15528c2ecf20Sopenharmony_ci			/* Ingress quotas are not supported for the CPU port */
15538c2ecf20Sopenharmony_ci			masked_count++;
15548c2ecf20Sopenharmony_ci			continue;
15558c2ecf20Sopenharmony_ci		}
15568c2ecf20Sopenharmony_ci		for (i = 0; i < MLXSW_SP_SB_ING_TC_COUNT; i++) {
15578c2ecf20Sopenharmony_ci			cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port, i,
15588c2ecf20Sopenharmony_ci						MLXSW_REG_SBXX_DIR_INGRESS);
15598c2ecf20Sopenharmony_ci			mlxsw_reg_sbsr_rec_unpack(sbsr_pl, rec_index++,
15608c2ecf20Sopenharmony_ci						  &cm->occ.cur, &cm->occ.max);
15618c2ecf20Sopenharmony_ci		}
15628c2ecf20Sopenharmony_ci		if (++masked_count == cb_ctx.masked_count)
15638c2ecf20Sopenharmony_ci			break;
15648c2ecf20Sopenharmony_ci	}
15658c2ecf20Sopenharmony_ci	masked_count = 0;
15668c2ecf20Sopenharmony_ci	for (local_port = cb_ctx.local_port_1;
15678c2ecf20Sopenharmony_ci	     local_port < mlxsw_core_max_ports(mlxsw_core); local_port++) {
15688c2ecf20Sopenharmony_ci		if (!mlxsw_sp->ports[local_port])
15698c2ecf20Sopenharmony_ci			continue;
15708c2ecf20Sopenharmony_ci		for (i = 0; i < MLXSW_SP_SB_EG_TC_COUNT; i++) {
15718c2ecf20Sopenharmony_ci			cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port, i,
15728c2ecf20Sopenharmony_ci						MLXSW_REG_SBXX_DIR_EGRESS);
15738c2ecf20Sopenharmony_ci			mlxsw_reg_sbsr_rec_unpack(sbsr_pl, rec_index++,
15748c2ecf20Sopenharmony_ci						  &cm->occ.cur, &cm->occ.max);
15758c2ecf20Sopenharmony_ci		}
15768c2ecf20Sopenharmony_ci		if (++masked_count == cb_ctx.masked_count)
15778c2ecf20Sopenharmony_ci			break;
15788c2ecf20Sopenharmony_ci	}
15798c2ecf20Sopenharmony_ci}
15808c2ecf20Sopenharmony_ci
15818c2ecf20Sopenharmony_ciint mlxsw_sp_sb_occ_snapshot(struct mlxsw_core *mlxsw_core,
15828c2ecf20Sopenharmony_ci			     unsigned int sb_index)
15838c2ecf20Sopenharmony_ci{
15848c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
15858c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_sr_occ_query_cb_ctx cb_ctx;
15868c2ecf20Sopenharmony_ci	unsigned long cb_priv;
15878c2ecf20Sopenharmony_ci	LIST_HEAD(bulk_list);
15888c2ecf20Sopenharmony_ci	char *sbsr_pl;
15898c2ecf20Sopenharmony_ci	u8 masked_count;
15908c2ecf20Sopenharmony_ci	u8 local_port_1;
15918c2ecf20Sopenharmony_ci	u8 local_port;
15928c2ecf20Sopenharmony_ci	int i;
15938c2ecf20Sopenharmony_ci	int err;
15948c2ecf20Sopenharmony_ci	int err2;
15958c2ecf20Sopenharmony_ci
15968c2ecf20Sopenharmony_ci	sbsr_pl = kmalloc(MLXSW_REG_SBSR_LEN, GFP_KERNEL);
15978c2ecf20Sopenharmony_ci	if (!sbsr_pl)
15988c2ecf20Sopenharmony_ci		return -ENOMEM;
15998c2ecf20Sopenharmony_ci
16008c2ecf20Sopenharmony_ci	local_port = MLXSW_PORT_CPU_PORT;
16018c2ecf20Sopenharmony_cinext_batch:
16028c2ecf20Sopenharmony_ci	local_port_1 = local_port;
16038c2ecf20Sopenharmony_ci	masked_count = 0;
16048c2ecf20Sopenharmony_ci	mlxsw_reg_sbsr_pack(sbsr_pl, false);
16058c2ecf20Sopenharmony_ci	for (i = 0; i < MLXSW_SP_SB_ING_TC_COUNT; i++)
16068c2ecf20Sopenharmony_ci		mlxsw_reg_sbsr_pg_buff_mask_set(sbsr_pl, i, 1);
16078c2ecf20Sopenharmony_ci	for (i = 0; i < MLXSW_SP_SB_EG_TC_COUNT; i++)
16088c2ecf20Sopenharmony_ci		mlxsw_reg_sbsr_tclass_mask_set(sbsr_pl, i, 1);
16098c2ecf20Sopenharmony_ci	for (; local_port < mlxsw_core_max_ports(mlxsw_core); local_port++) {
16108c2ecf20Sopenharmony_ci		if (!mlxsw_sp->ports[local_port])
16118c2ecf20Sopenharmony_ci			continue;
16128c2ecf20Sopenharmony_ci		if (local_port != MLXSW_PORT_CPU_PORT) {
16138c2ecf20Sopenharmony_ci			/* Ingress quotas are not supported for the CPU port */
16148c2ecf20Sopenharmony_ci			mlxsw_reg_sbsr_ingress_port_mask_set(sbsr_pl,
16158c2ecf20Sopenharmony_ci							     local_port, 1);
16168c2ecf20Sopenharmony_ci		}
16178c2ecf20Sopenharmony_ci		mlxsw_reg_sbsr_egress_port_mask_set(sbsr_pl, local_port, 1);
16188c2ecf20Sopenharmony_ci		for (i = 0; i < mlxsw_sp->sb_vals->pool_count; i++) {
16198c2ecf20Sopenharmony_ci			err = mlxsw_sp_sb_pm_occ_query(mlxsw_sp, local_port, i,
16208c2ecf20Sopenharmony_ci						       &bulk_list);
16218c2ecf20Sopenharmony_ci			if (err)
16228c2ecf20Sopenharmony_ci				goto out;
16238c2ecf20Sopenharmony_ci		}
16248c2ecf20Sopenharmony_ci		if (++masked_count == MASKED_COUNT_MAX)
16258c2ecf20Sopenharmony_ci			goto do_query;
16268c2ecf20Sopenharmony_ci	}
16278c2ecf20Sopenharmony_ci
16288c2ecf20Sopenharmony_cido_query:
16298c2ecf20Sopenharmony_ci	cb_ctx.masked_count = masked_count;
16308c2ecf20Sopenharmony_ci	cb_ctx.local_port_1 = local_port_1;
16318c2ecf20Sopenharmony_ci	memcpy(&cb_priv, &cb_ctx, sizeof(cb_ctx));
16328c2ecf20Sopenharmony_ci	err = mlxsw_reg_trans_query(mlxsw_core, MLXSW_REG(sbsr), sbsr_pl,
16338c2ecf20Sopenharmony_ci				    &bulk_list, mlxsw_sp_sb_sr_occ_query_cb,
16348c2ecf20Sopenharmony_ci				    cb_priv);
16358c2ecf20Sopenharmony_ci	if (err)
16368c2ecf20Sopenharmony_ci		goto out;
16378c2ecf20Sopenharmony_ci	if (local_port < mlxsw_core_max_ports(mlxsw_core)) {
16388c2ecf20Sopenharmony_ci		local_port++;
16398c2ecf20Sopenharmony_ci		goto next_batch;
16408c2ecf20Sopenharmony_ci	}
16418c2ecf20Sopenharmony_ci
16428c2ecf20Sopenharmony_ciout:
16438c2ecf20Sopenharmony_ci	err2 = mlxsw_reg_trans_bulk_wait(&bulk_list);
16448c2ecf20Sopenharmony_ci	if (!err)
16458c2ecf20Sopenharmony_ci		err = err2;
16468c2ecf20Sopenharmony_ci	kfree(sbsr_pl);
16478c2ecf20Sopenharmony_ci	return err;
16488c2ecf20Sopenharmony_ci}
16498c2ecf20Sopenharmony_ci
16508c2ecf20Sopenharmony_ciint mlxsw_sp_sb_occ_max_clear(struct mlxsw_core *mlxsw_core,
16518c2ecf20Sopenharmony_ci			      unsigned int sb_index)
16528c2ecf20Sopenharmony_ci{
16538c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
16548c2ecf20Sopenharmony_ci	LIST_HEAD(bulk_list);
16558c2ecf20Sopenharmony_ci	char *sbsr_pl;
16568c2ecf20Sopenharmony_ci	unsigned int masked_count;
16578c2ecf20Sopenharmony_ci	u8 local_port;
16588c2ecf20Sopenharmony_ci	int i;
16598c2ecf20Sopenharmony_ci	int err;
16608c2ecf20Sopenharmony_ci	int err2;
16618c2ecf20Sopenharmony_ci
16628c2ecf20Sopenharmony_ci	sbsr_pl = kmalloc(MLXSW_REG_SBSR_LEN, GFP_KERNEL);
16638c2ecf20Sopenharmony_ci	if (!sbsr_pl)
16648c2ecf20Sopenharmony_ci		return -ENOMEM;
16658c2ecf20Sopenharmony_ci
16668c2ecf20Sopenharmony_ci	local_port = MLXSW_PORT_CPU_PORT;
16678c2ecf20Sopenharmony_cinext_batch:
16688c2ecf20Sopenharmony_ci	masked_count = 0;
16698c2ecf20Sopenharmony_ci	mlxsw_reg_sbsr_pack(sbsr_pl, true);
16708c2ecf20Sopenharmony_ci	for (i = 0; i < MLXSW_SP_SB_ING_TC_COUNT; i++)
16718c2ecf20Sopenharmony_ci		mlxsw_reg_sbsr_pg_buff_mask_set(sbsr_pl, i, 1);
16728c2ecf20Sopenharmony_ci	for (i = 0; i < MLXSW_SP_SB_EG_TC_COUNT; i++)
16738c2ecf20Sopenharmony_ci		mlxsw_reg_sbsr_tclass_mask_set(sbsr_pl, i, 1);
16748c2ecf20Sopenharmony_ci	for (; local_port < mlxsw_core_max_ports(mlxsw_core); local_port++) {
16758c2ecf20Sopenharmony_ci		if (!mlxsw_sp->ports[local_port])
16768c2ecf20Sopenharmony_ci			continue;
16778c2ecf20Sopenharmony_ci		if (local_port != MLXSW_PORT_CPU_PORT) {
16788c2ecf20Sopenharmony_ci			/* Ingress quotas are not supported for the CPU port */
16798c2ecf20Sopenharmony_ci			mlxsw_reg_sbsr_ingress_port_mask_set(sbsr_pl,
16808c2ecf20Sopenharmony_ci							     local_port, 1);
16818c2ecf20Sopenharmony_ci		}
16828c2ecf20Sopenharmony_ci		mlxsw_reg_sbsr_egress_port_mask_set(sbsr_pl, local_port, 1);
16838c2ecf20Sopenharmony_ci		for (i = 0; i < mlxsw_sp->sb_vals->pool_count; i++) {
16848c2ecf20Sopenharmony_ci			err = mlxsw_sp_sb_pm_occ_clear(mlxsw_sp, local_port, i,
16858c2ecf20Sopenharmony_ci						       &bulk_list);
16868c2ecf20Sopenharmony_ci			if (err)
16878c2ecf20Sopenharmony_ci				goto out;
16888c2ecf20Sopenharmony_ci		}
16898c2ecf20Sopenharmony_ci		if (++masked_count == MASKED_COUNT_MAX)
16908c2ecf20Sopenharmony_ci			goto do_query;
16918c2ecf20Sopenharmony_ci	}
16928c2ecf20Sopenharmony_ci
16938c2ecf20Sopenharmony_cido_query:
16948c2ecf20Sopenharmony_ci	err = mlxsw_reg_trans_query(mlxsw_core, MLXSW_REG(sbsr), sbsr_pl,
16958c2ecf20Sopenharmony_ci				    &bulk_list, NULL, 0);
16968c2ecf20Sopenharmony_ci	if (err)
16978c2ecf20Sopenharmony_ci		goto out;
16988c2ecf20Sopenharmony_ci	if (local_port < mlxsw_core_max_ports(mlxsw_core)) {
16998c2ecf20Sopenharmony_ci		local_port++;
17008c2ecf20Sopenharmony_ci		goto next_batch;
17018c2ecf20Sopenharmony_ci	}
17028c2ecf20Sopenharmony_ci
17038c2ecf20Sopenharmony_ciout:
17048c2ecf20Sopenharmony_ci	err2 = mlxsw_reg_trans_bulk_wait(&bulk_list);
17058c2ecf20Sopenharmony_ci	if (!err)
17068c2ecf20Sopenharmony_ci		err = err2;
17078c2ecf20Sopenharmony_ci	kfree(sbsr_pl);
17088c2ecf20Sopenharmony_ci	return err;
17098c2ecf20Sopenharmony_ci}
17108c2ecf20Sopenharmony_ci
17118c2ecf20Sopenharmony_ciint mlxsw_sp_sb_occ_port_pool_get(struct mlxsw_core_port *mlxsw_core_port,
17128c2ecf20Sopenharmony_ci				  unsigned int sb_index, u16 pool_index,
17138c2ecf20Sopenharmony_ci				  u32 *p_cur, u32 *p_max)
17148c2ecf20Sopenharmony_ci{
17158c2ecf20Sopenharmony_ci	struct mlxsw_sp_port *mlxsw_sp_port =
17168c2ecf20Sopenharmony_ci			mlxsw_core_port_driver_priv(mlxsw_core_port);
17178c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
17188c2ecf20Sopenharmony_ci	u8 local_port = mlxsw_sp_port->local_port;
17198c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_pm *pm = mlxsw_sp_sb_pm_get(mlxsw_sp, local_port,
17208c2ecf20Sopenharmony_ci						       pool_index);
17218c2ecf20Sopenharmony_ci
17228c2ecf20Sopenharmony_ci	*p_cur = mlxsw_sp_cells_bytes(mlxsw_sp, pm->occ.cur);
17238c2ecf20Sopenharmony_ci	*p_max = mlxsw_sp_cells_bytes(mlxsw_sp, pm->occ.max);
17248c2ecf20Sopenharmony_ci	return 0;
17258c2ecf20Sopenharmony_ci}
17268c2ecf20Sopenharmony_ci
17278c2ecf20Sopenharmony_ciint mlxsw_sp_sb_occ_tc_port_bind_get(struct mlxsw_core_port *mlxsw_core_port,
17288c2ecf20Sopenharmony_ci				     unsigned int sb_index, u16 tc_index,
17298c2ecf20Sopenharmony_ci				     enum devlink_sb_pool_type pool_type,
17308c2ecf20Sopenharmony_ci				     u32 *p_cur, u32 *p_max)
17318c2ecf20Sopenharmony_ci{
17328c2ecf20Sopenharmony_ci	struct mlxsw_sp_port *mlxsw_sp_port =
17338c2ecf20Sopenharmony_ci			mlxsw_core_port_driver_priv(mlxsw_core_port);
17348c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
17358c2ecf20Sopenharmony_ci	u8 local_port = mlxsw_sp_port->local_port;
17368c2ecf20Sopenharmony_ci	u8 pg_buff = tc_index;
17378c2ecf20Sopenharmony_ci	enum mlxsw_reg_sbxx_dir dir = (enum mlxsw_reg_sbxx_dir) pool_type;
17388c2ecf20Sopenharmony_ci	struct mlxsw_sp_sb_cm *cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port,
17398c2ecf20Sopenharmony_ci						       pg_buff, dir);
17408c2ecf20Sopenharmony_ci
17418c2ecf20Sopenharmony_ci	*p_cur = mlxsw_sp_cells_bytes(mlxsw_sp, cm->occ.cur);
17428c2ecf20Sopenharmony_ci	*p_max = mlxsw_sp_cells_bytes(mlxsw_sp, cm->occ.max);
17438c2ecf20Sopenharmony_ci	return 0;
17448c2ecf20Sopenharmony_ci}
1745