162306a36Sopenharmony_ci// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
262306a36Sopenharmony_ci/* Copyright (c) 2020 Mellanox Technologies. All rights reserved */
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#include <linux/idr.h>
562306a36Sopenharmony_ci#include <linux/log2.h>
662306a36Sopenharmony_ci#include <linux/mutex.h>
762306a36Sopenharmony_ci#include <linux/netlink.h>
862306a36Sopenharmony_ci#include <net/devlink.h>
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include "spectrum.h"
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_cistruct mlxsw_sp_policer_family {
1362306a36Sopenharmony_ci	enum mlxsw_sp_policer_type type;
1462306a36Sopenharmony_ci	enum mlxsw_reg_qpcr_g qpcr_type;
1562306a36Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp;
1662306a36Sopenharmony_ci	u16 start_index; /* Inclusive */
1762306a36Sopenharmony_ci	u16 end_index; /* Exclusive */
1862306a36Sopenharmony_ci	struct idr policer_idr;
1962306a36Sopenharmony_ci	struct mutex lock; /* Protects policer_idr */
2062306a36Sopenharmony_ci	atomic_t policers_count;
2162306a36Sopenharmony_ci	const struct mlxsw_sp_policer_family_ops *ops;
2262306a36Sopenharmony_ci};
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistruct mlxsw_sp_policer {
2562306a36Sopenharmony_ci	struct mlxsw_sp_policer_params params;
2662306a36Sopenharmony_ci	u16 index;
2762306a36Sopenharmony_ci};
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_cistruct mlxsw_sp_policer_family_ops {
3062306a36Sopenharmony_ci	int (*init)(struct mlxsw_sp_policer_family *family);
3162306a36Sopenharmony_ci	void (*fini)(struct mlxsw_sp_policer_family *family);
3262306a36Sopenharmony_ci	int (*policer_index_alloc)(struct mlxsw_sp_policer_family *family,
3362306a36Sopenharmony_ci				   struct mlxsw_sp_policer *policer);
3462306a36Sopenharmony_ci	struct mlxsw_sp_policer * (*policer_index_free)(struct mlxsw_sp_policer_family *family,
3562306a36Sopenharmony_ci							u16 policer_index);
3662306a36Sopenharmony_ci	int (*policer_init)(struct mlxsw_sp_policer_family *family,
3762306a36Sopenharmony_ci			    const struct mlxsw_sp_policer *policer);
3862306a36Sopenharmony_ci	int (*policer_params_check)(const struct mlxsw_sp_policer_family *family,
3962306a36Sopenharmony_ci				    const struct mlxsw_sp_policer_params *params,
4062306a36Sopenharmony_ci				    struct netlink_ext_ack *extack);
4162306a36Sopenharmony_ci};
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistruct mlxsw_sp_policer_core {
4462306a36Sopenharmony_ci	struct mlxsw_sp_policer_family *family_arr[MLXSW_SP_POLICER_TYPE_MAX + 1];
4562306a36Sopenharmony_ci	const struct mlxsw_sp_policer_core_ops *ops;
4662306a36Sopenharmony_ci	u8 lowest_bs_bits;
4762306a36Sopenharmony_ci	u8 highest_bs_bits;
4862306a36Sopenharmony_ci};
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cistruct mlxsw_sp_policer_core_ops {
5162306a36Sopenharmony_ci	int (*init)(struct mlxsw_sp_policer_core *policer_core);
5262306a36Sopenharmony_ci};
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_cistatic u64 mlxsw_sp_policer_rate_bytes_ps_kbps(u64 rate_bytes_ps)
5562306a36Sopenharmony_ci{
5662306a36Sopenharmony_ci	return div_u64(rate_bytes_ps, 1000) * BITS_PER_BYTE;
5762306a36Sopenharmony_ci}
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_cistatic u8 mlxsw_sp_policer_burst_bytes_hw_units(u64 burst_bytes)
6062306a36Sopenharmony_ci{
6162306a36Sopenharmony_ci	/* Provided burst size is in bytes. The ASIC burst size value is
6262306a36Sopenharmony_ci	 * (2 ^ bs) * 512 bits. Convert the provided size to 512-bit units.
6362306a36Sopenharmony_ci	 */
6462306a36Sopenharmony_ci	u64 bs512 = div_u64(burst_bytes, 64);
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	if (!bs512)
6762306a36Sopenharmony_ci		return 0;
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	return fls64(bs512) - 1;
7062306a36Sopenharmony_ci}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_cistatic u64 mlxsw_sp_policer_single_rate_occ_get(void *priv)
7362306a36Sopenharmony_ci{
7462306a36Sopenharmony_ci	struct mlxsw_sp_policer_family *family = priv;
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	return atomic_read(&family->policers_count);
7762306a36Sopenharmony_ci}
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_cistatic int
8062306a36Sopenharmony_cimlxsw_sp_policer_single_rate_family_init(struct mlxsw_sp_policer_family *family)
8162306a36Sopenharmony_ci{
8262306a36Sopenharmony_ci	struct mlxsw_core *core = family->mlxsw_sp->core;
8362306a36Sopenharmony_ci	struct devlink *devlink;
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	/* CPU policers are allocated from the first N policers in the global
8662306a36Sopenharmony_ci	 * range, so skip them.
8762306a36Sopenharmony_ci	 */
8862306a36Sopenharmony_ci	if (!MLXSW_CORE_RES_VALID(core, MAX_GLOBAL_POLICERS) ||
8962306a36Sopenharmony_ci	    !MLXSW_CORE_RES_VALID(core, MAX_CPU_POLICERS))
9062306a36Sopenharmony_ci		return -EIO;
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	family->start_index = MLXSW_CORE_RES_GET(core, MAX_CPU_POLICERS);
9362306a36Sopenharmony_ci	family->end_index = MLXSW_CORE_RES_GET(core, MAX_GLOBAL_POLICERS);
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	atomic_set(&family->policers_count, 0);
9662306a36Sopenharmony_ci	devlink = priv_to_devlink(core);
9762306a36Sopenharmony_ci	devl_resource_occ_get_register(devlink,
9862306a36Sopenharmony_ci				       MLXSW_SP_RESOURCE_SINGLE_RATE_POLICERS,
9962306a36Sopenharmony_ci				       mlxsw_sp_policer_single_rate_occ_get,
10062306a36Sopenharmony_ci				       family);
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	return 0;
10362306a36Sopenharmony_ci}
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_cistatic void
10662306a36Sopenharmony_cimlxsw_sp_policer_single_rate_family_fini(struct mlxsw_sp_policer_family *family)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	struct devlink *devlink = priv_to_devlink(family->mlxsw_sp->core);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	devl_resource_occ_get_unregister(devlink,
11162306a36Sopenharmony_ci					 MLXSW_SP_RESOURCE_SINGLE_RATE_POLICERS);
11262306a36Sopenharmony_ci	WARN_ON(atomic_read(&family->policers_count) != 0);
11362306a36Sopenharmony_ci}
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_cistatic int
11662306a36Sopenharmony_cimlxsw_sp_policer_single_rate_index_alloc(struct mlxsw_sp_policer_family *family,
11762306a36Sopenharmony_ci					 struct mlxsw_sp_policer *policer)
11862306a36Sopenharmony_ci{
11962306a36Sopenharmony_ci	int id;
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	mutex_lock(&family->lock);
12262306a36Sopenharmony_ci	id = idr_alloc(&family->policer_idr, policer, family->start_index,
12362306a36Sopenharmony_ci		       family->end_index, GFP_KERNEL);
12462306a36Sopenharmony_ci	mutex_unlock(&family->lock);
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	if (id < 0)
12762306a36Sopenharmony_ci		return id;
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	atomic_inc(&family->policers_count);
13062306a36Sopenharmony_ci	policer->index = id;
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci	return 0;
13362306a36Sopenharmony_ci}
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_cistatic struct mlxsw_sp_policer *
13662306a36Sopenharmony_cimlxsw_sp_policer_single_rate_index_free(struct mlxsw_sp_policer_family *family,
13762306a36Sopenharmony_ci					u16 policer_index)
13862306a36Sopenharmony_ci{
13962306a36Sopenharmony_ci	struct mlxsw_sp_policer *policer;
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	atomic_dec(&family->policers_count);
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	mutex_lock(&family->lock);
14462306a36Sopenharmony_ci	policer = idr_remove(&family->policer_idr, policer_index);
14562306a36Sopenharmony_ci	mutex_unlock(&family->lock);
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci	WARN_ON(!policer);
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	return policer;
15062306a36Sopenharmony_ci}
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_cistatic int
15362306a36Sopenharmony_cimlxsw_sp_policer_single_rate_init(struct mlxsw_sp_policer_family *family,
15462306a36Sopenharmony_ci				  const struct mlxsw_sp_policer *policer)
15562306a36Sopenharmony_ci{
15662306a36Sopenharmony_ci	u64 rate_kbps = mlxsw_sp_policer_rate_bytes_ps_kbps(policer->params.rate);
15762306a36Sopenharmony_ci	u8 bs = mlxsw_sp_policer_burst_bytes_hw_units(policer->params.burst);
15862306a36Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = family->mlxsw_sp;
15962306a36Sopenharmony_ci	char qpcr_pl[MLXSW_REG_QPCR_LEN];
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci	mlxsw_reg_qpcr_pack(qpcr_pl, policer->index, MLXSW_REG_QPCR_IR_UNITS_K,
16262306a36Sopenharmony_ci			    true, rate_kbps, bs);
16362306a36Sopenharmony_ci	mlxsw_reg_qpcr_clear_counter_set(qpcr_pl, true);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qpcr), qpcr_pl);
16662306a36Sopenharmony_ci}
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_cistatic int
16962306a36Sopenharmony_cimlxsw_sp_policer_single_rate_params_check(const struct mlxsw_sp_policer_family *family,
17062306a36Sopenharmony_ci					  const struct mlxsw_sp_policer_params *params,
17162306a36Sopenharmony_ci					  struct netlink_ext_ack *extack)
17262306a36Sopenharmony_ci{
17362306a36Sopenharmony_ci	struct mlxsw_sp_policer_core *policer_core = family->mlxsw_sp->policer_core;
17462306a36Sopenharmony_ci	u64 rate_bps = params->rate * BITS_PER_BYTE;
17562306a36Sopenharmony_ci	u8 bs;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	if (!params->bytes) {
17862306a36Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Only bandwidth policing is currently supported by single rate policers");
17962306a36Sopenharmony_ci		return -EINVAL;
18062306a36Sopenharmony_ci	}
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	if (!is_power_of_2(params->burst)) {
18362306a36Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Policer burst size is not power of two");
18462306a36Sopenharmony_ci		return -EINVAL;
18562306a36Sopenharmony_ci	}
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	bs = mlxsw_sp_policer_burst_bytes_hw_units(params->burst);
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	if (bs < policer_core->lowest_bs_bits) {
19062306a36Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit");
19162306a36Sopenharmony_ci		return -EINVAL;
19262306a36Sopenharmony_ci	}
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci	if (bs > policer_core->highest_bs_bits) {
19562306a36Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit");
19662306a36Sopenharmony_ci		return -EINVAL;
19762306a36Sopenharmony_ci	}
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci	if (rate_bps < MLXSW_REG_QPCR_LOWEST_CIR_BITS) {
20062306a36Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit");
20162306a36Sopenharmony_ci		return -EINVAL;
20262306a36Sopenharmony_ci	}
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci	if (rate_bps > MLXSW_REG_QPCR_HIGHEST_CIR_BITS) {
20562306a36Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit");
20662306a36Sopenharmony_ci		return -EINVAL;
20762306a36Sopenharmony_ci	}
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci	return 0;
21062306a36Sopenharmony_ci}
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_cistatic const struct mlxsw_sp_policer_family_ops mlxsw_sp_policer_single_rate_ops = {
21362306a36Sopenharmony_ci	.init			= mlxsw_sp_policer_single_rate_family_init,
21462306a36Sopenharmony_ci	.fini			= mlxsw_sp_policer_single_rate_family_fini,
21562306a36Sopenharmony_ci	.policer_index_alloc	= mlxsw_sp_policer_single_rate_index_alloc,
21662306a36Sopenharmony_ci	.policer_index_free	= mlxsw_sp_policer_single_rate_index_free,
21762306a36Sopenharmony_ci	.policer_init		= mlxsw_sp_policer_single_rate_init,
21862306a36Sopenharmony_ci	.policer_params_check	= mlxsw_sp_policer_single_rate_params_check,
21962306a36Sopenharmony_ci};
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_cistatic const struct mlxsw_sp_policer_family mlxsw_sp_policer_single_rate_family = {
22262306a36Sopenharmony_ci	.type		= MLXSW_SP_POLICER_TYPE_SINGLE_RATE,
22362306a36Sopenharmony_ci	.qpcr_type	= MLXSW_REG_QPCR_G_GLOBAL,
22462306a36Sopenharmony_ci	.ops		= &mlxsw_sp_policer_single_rate_ops,
22562306a36Sopenharmony_ci};
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_cistatic const struct mlxsw_sp_policer_family *mlxsw_sp_policer_family_arr[] = {
22862306a36Sopenharmony_ci	[MLXSW_SP_POLICER_TYPE_SINGLE_RATE]	= &mlxsw_sp_policer_single_rate_family,
22962306a36Sopenharmony_ci};
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ciint mlxsw_sp_policer_add(struct mlxsw_sp *mlxsw_sp,
23262306a36Sopenharmony_ci			 enum mlxsw_sp_policer_type type,
23362306a36Sopenharmony_ci			 const struct mlxsw_sp_policer_params *params,
23462306a36Sopenharmony_ci			 struct netlink_ext_ack *extack, u16 *p_policer_index)
23562306a36Sopenharmony_ci{
23662306a36Sopenharmony_ci	struct mlxsw_sp_policer_family *family;
23762306a36Sopenharmony_ci	struct mlxsw_sp_policer *policer;
23862306a36Sopenharmony_ci	int err;
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci	family = mlxsw_sp->policer_core->family_arr[type];
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci	err = family->ops->policer_params_check(family, params, extack);
24362306a36Sopenharmony_ci	if (err)
24462306a36Sopenharmony_ci		return err;
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci	policer = kmalloc(sizeof(*policer), GFP_KERNEL);
24762306a36Sopenharmony_ci	if (!policer)
24862306a36Sopenharmony_ci		return -ENOMEM;
24962306a36Sopenharmony_ci	policer->params = *params;
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	err = family->ops->policer_index_alloc(family, policer);
25262306a36Sopenharmony_ci	if (err) {
25362306a36Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Failed to allocate policer index");
25462306a36Sopenharmony_ci		goto err_policer_index_alloc;
25562306a36Sopenharmony_ci	}
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci	err = family->ops->policer_init(family, policer);
25862306a36Sopenharmony_ci	if (err) {
25962306a36Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Failed to initialize policer");
26062306a36Sopenharmony_ci		goto err_policer_init;
26162306a36Sopenharmony_ci	}
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci	*p_policer_index = policer->index;
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci	return 0;
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_cierr_policer_init:
26862306a36Sopenharmony_ci	family->ops->policer_index_free(family, policer->index);
26962306a36Sopenharmony_cierr_policer_index_alloc:
27062306a36Sopenharmony_ci	kfree(policer);
27162306a36Sopenharmony_ci	return err;
27262306a36Sopenharmony_ci}
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_civoid mlxsw_sp_policer_del(struct mlxsw_sp *mlxsw_sp,
27562306a36Sopenharmony_ci			  enum mlxsw_sp_policer_type type, u16 policer_index)
27662306a36Sopenharmony_ci{
27762306a36Sopenharmony_ci	struct mlxsw_sp_policer_family *family;
27862306a36Sopenharmony_ci	struct mlxsw_sp_policer *policer;
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci	family = mlxsw_sp->policer_core->family_arr[type];
28162306a36Sopenharmony_ci	policer = family->ops->policer_index_free(family, policer_index);
28262306a36Sopenharmony_ci	kfree(policer);
28362306a36Sopenharmony_ci}
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ciint mlxsw_sp_policer_drops_counter_get(struct mlxsw_sp *mlxsw_sp,
28662306a36Sopenharmony_ci				       enum mlxsw_sp_policer_type type,
28762306a36Sopenharmony_ci				       u16 policer_index, u64 *p_drops)
28862306a36Sopenharmony_ci{
28962306a36Sopenharmony_ci	struct mlxsw_sp_policer_family *family;
29062306a36Sopenharmony_ci	char qpcr_pl[MLXSW_REG_QPCR_LEN];
29162306a36Sopenharmony_ci	int err;
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ci	family = mlxsw_sp->policer_core->family_arr[type];
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci	MLXSW_REG_ZERO(qpcr, qpcr_pl);
29662306a36Sopenharmony_ci	mlxsw_reg_qpcr_pid_set(qpcr_pl, policer_index);
29762306a36Sopenharmony_ci	mlxsw_reg_qpcr_g_set(qpcr_pl, family->qpcr_type);
29862306a36Sopenharmony_ci	err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(qpcr), qpcr_pl);
29962306a36Sopenharmony_ci	if (err)
30062306a36Sopenharmony_ci		return err;
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci	*p_drops = mlxsw_reg_qpcr_violate_count_get(qpcr_pl);
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci	return 0;
30562306a36Sopenharmony_ci}
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_cistatic int
30862306a36Sopenharmony_cimlxsw_sp_policer_family_register(struct mlxsw_sp *mlxsw_sp,
30962306a36Sopenharmony_ci				 const struct mlxsw_sp_policer_family *tmpl)
31062306a36Sopenharmony_ci{
31162306a36Sopenharmony_ci	struct mlxsw_sp_policer_family *family;
31262306a36Sopenharmony_ci	int err;
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ci	family = kmemdup(tmpl, sizeof(*family), GFP_KERNEL);
31562306a36Sopenharmony_ci	if (!family)
31662306a36Sopenharmony_ci		return -ENOMEM;
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_ci	family->mlxsw_sp = mlxsw_sp;
31962306a36Sopenharmony_ci	idr_init(&family->policer_idr);
32062306a36Sopenharmony_ci	mutex_init(&family->lock);
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci	err = family->ops->init(family);
32362306a36Sopenharmony_ci	if (err)
32462306a36Sopenharmony_ci		goto err_family_init;
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci	if (WARN_ON(family->start_index >= family->end_index)) {
32762306a36Sopenharmony_ci		err = -EINVAL;
32862306a36Sopenharmony_ci		goto err_index_check;
32962306a36Sopenharmony_ci	}
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ci	mlxsw_sp->policer_core->family_arr[tmpl->type] = family;
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ci	return 0;
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_cierr_index_check:
33662306a36Sopenharmony_ci	family->ops->fini(family);
33762306a36Sopenharmony_cierr_family_init:
33862306a36Sopenharmony_ci	mutex_destroy(&family->lock);
33962306a36Sopenharmony_ci	idr_destroy(&family->policer_idr);
34062306a36Sopenharmony_ci	kfree(family);
34162306a36Sopenharmony_ci	return err;
34262306a36Sopenharmony_ci}
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_cistatic void
34562306a36Sopenharmony_cimlxsw_sp_policer_family_unregister(struct mlxsw_sp *mlxsw_sp,
34662306a36Sopenharmony_ci				   struct mlxsw_sp_policer_family *family)
34762306a36Sopenharmony_ci{
34862306a36Sopenharmony_ci	family->ops->fini(family);
34962306a36Sopenharmony_ci	mutex_destroy(&family->lock);
35062306a36Sopenharmony_ci	WARN_ON(!idr_is_empty(&family->policer_idr));
35162306a36Sopenharmony_ci	idr_destroy(&family->policer_idr);
35262306a36Sopenharmony_ci	kfree(family);
35362306a36Sopenharmony_ci}
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ciint mlxsw_sp_policers_init(struct mlxsw_sp *mlxsw_sp)
35662306a36Sopenharmony_ci{
35762306a36Sopenharmony_ci	struct mlxsw_sp_policer_core *policer_core;
35862306a36Sopenharmony_ci	int i, err;
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci	policer_core = kzalloc(sizeof(*policer_core), GFP_KERNEL);
36162306a36Sopenharmony_ci	if (!policer_core)
36262306a36Sopenharmony_ci		return -ENOMEM;
36362306a36Sopenharmony_ci	mlxsw_sp->policer_core = policer_core;
36462306a36Sopenharmony_ci	policer_core->ops = mlxsw_sp->policer_core_ops;
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci	err = policer_core->ops->init(policer_core);
36762306a36Sopenharmony_ci	if (err)
36862306a36Sopenharmony_ci		goto err_init;
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci	for (i = 0; i < MLXSW_SP_POLICER_TYPE_MAX + 1; i++) {
37162306a36Sopenharmony_ci		err = mlxsw_sp_policer_family_register(mlxsw_sp, mlxsw_sp_policer_family_arr[i]);
37262306a36Sopenharmony_ci		if (err)
37362306a36Sopenharmony_ci			goto err_family_register;
37462306a36Sopenharmony_ci	}
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_ci	return 0;
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_cierr_family_register:
37962306a36Sopenharmony_ci	for (i--; i >= 0; i--) {
38062306a36Sopenharmony_ci		struct mlxsw_sp_policer_family *family;
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci		family = mlxsw_sp->policer_core->family_arr[i];
38362306a36Sopenharmony_ci		mlxsw_sp_policer_family_unregister(mlxsw_sp, family);
38462306a36Sopenharmony_ci	}
38562306a36Sopenharmony_cierr_init:
38662306a36Sopenharmony_ci	kfree(mlxsw_sp->policer_core);
38762306a36Sopenharmony_ci	return err;
38862306a36Sopenharmony_ci}
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_civoid mlxsw_sp_policers_fini(struct mlxsw_sp *mlxsw_sp)
39162306a36Sopenharmony_ci{
39262306a36Sopenharmony_ci	int i;
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci	for (i = MLXSW_SP_POLICER_TYPE_MAX; i >= 0; i--) {
39562306a36Sopenharmony_ci		struct mlxsw_sp_policer_family *family;
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_ci		family = mlxsw_sp->policer_core->family_arr[i];
39862306a36Sopenharmony_ci		mlxsw_sp_policer_family_unregister(mlxsw_sp, family);
39962306a36Sopenharmony_ci	}
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci	kfree(mlxsw_sp->policer_core);
40262306a36Sopenharmony_ci}
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ciint mlxsw_sp_policer_resources_register(struct mlxsw_core *mlxsw_core)
40562306a36Sopenharmony_ci{
40662306a36Sopenharmony_ci	u64 global_policers, cpu_policers, single_rate_policers;
40762306a36Sopenharmony_ci	struct devlink *devlink = priv_to_devlink(mlxsw_core);
40862306a36Sopenharmony_ci	struct devlink_resource_size_params size_params;
40962306a36Sopenharmony_ci	int err;
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci	if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_GLOBAL_POLICERS) ||
41262306a36Sopenharmony_ci	    !MLXSW_CORE_RES_VALID(mlxsw_core, MAX_CPU_POLICERS))
41362306a36Sopenharmony_ci		return -EIO;
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_ci	global_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_GLOBAL_POLICERS);
41662306a36Sopenharmony_ci	cpu_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_CPU_POLICERS);
41762306a36Sopenharmony_ci	single_rate_policers = global_policers - cpu_policers;
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_ci	devlink_resource_size_params_init(&size_params, global_policers,
42062306a36Sopenharmony_ci					  global_policers, 1,
42162306a36Sopenharmony_ci					  DEVLINK_RESOURCE_UNIT_ENTRY);
42262306a36Sopenharmony_ci	err = devl_resource_register(devlink, "global_policers",
42362306a36Sopenharmony_ci				     global_policers,
42462306a36Sopenharmony_ci				     MLXSW_SP_RESOURCE_GLOBAL_POLICERS,
42562306a36Sopenharmony_ci				     DEVLINK_RESOURCE_ID_PARENT_TOP,
42662306a36Sopenharmony_ci				     &size_params);
42762306a36Sopenharmony_ci	if (err)
42862306a36Sopenharmony_ci		return err;
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_ci	devlink_resource_size_params_init(&size_params, single_rate_policers,
43162306a36Sopenharmony_ci					  single_rate_policers, 1,
43262306a36Sopenharmony_ci					  DEVLINK_RESOURCE_UNIT_ENTRY);
43362306a36Sopenharmony_ci	err = devl_resource_register(devlink, "single_rate_policers",
43462306a36Sopenharmony_ci				     single_rate_policers,
43562306a36Sopenharmony_ci				     MLXSW_SP_RESOURCE_SINGLE_RATE_POLICERS,
43662306a36Sopenharmony_ci				     MLXSW_SP_RESOURCE_GLOBAL_POLICERS,
43762306a36Sopenharmony_ci				     &size_params);
43862306a36Sopenharmony_ci	if (err)
43962306a36Sopenharmony_ci		return err;
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci	return 0;
44262306a36Sopenharmony_ci}
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_cistatic int
44562306a36Sopenharmony_cimlxsw_sp1_policer_core_init(struct mlxsw_sp_policer_core *policer_core)
44662306a36Sopenharmony_ci{
44762306a36Sopenharmony_ci	policer_core->lowest_bs_bits = MLXSW_REG_QPCR_LOWEST_CBS_BITS_SP1;
44862306a36Sopenharmony_ci	policer_core->highest_bs_bits = MLXSW_REG_QPCR_HIGHEST_CBS_BITS_SP1;
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_ci	return 0;
45162306a36Sopenharmony_ci}
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ciconst struct mlxsw_sp_policer_core_ops mlxsw_sp1_policer_core_ops = {
45462306a36Sopenharmony_ci	.init = mlxsw_sp1_policer_core_init,
45562306a36Sopenharmony_ci};
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_cistatic int
45862306a36Sopenharmony_cimlxsw_sp2_policer_core_init(struct mlxsw_sp_policer_core *policer_core)
45962306a36Sopenharmony_ci{
46062306a36Sopenharmony_ci	policer_core->lowest_bs_bits = MLXSW_REG_QPCR_LOWEST_CBS_BITS_SP2;
46162306a36Sopenharmony_ci	policer_core->highest_bs_bits = MLXSW_REG_QPCR_HIGHEST_CBS_BITS_SP2;
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ci	return 0;
46462306a36Sopenharmony_ci}
46562306a36Sopenharmony_ci
46662306a36Sopenharmony_ciconst struct mlxsw_sp_policer_core_ops mlxsw_sp2_policer_core_ops = {
46762306a36Sopenharmony_ci	.init = mlxsw_sp2_policer_core_init,
46862306a36Sopenharmony_ci};
469