162306a36Sopenharmony_ci/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */
262306a36Sopenharmony_ci/* Copyright (c) 2016-2018 Mellanox Technologies. All rights reserved */
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#ifndef _MLXSW_RESOURCES_H
562306a36Sopenharmony_ci#define _MLXSW_RESOURCES_H
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#include <linux/kernel.h>
862306a36Sopenharmony_ci#include <linux/types.h>
962306a36Sopenharmony_ci
1062306a36Sopenharmony_cienum mlxsw_res_id {
1162306a36Sopenharmony_ci	MLXSW_RES_ID_KVD_SIZE,
1262306a36Sopenharmony_ci	MLXSW_RES_ID_KVD_SINGLE_MIN_SIZE,
1362306a36Sopenharmony_ci	MLXSW_RES_ID_KVD_DOUBLE_MIN_SIZE,
1462306a36Sopenharmony_ci	MLXSW_RES_ID_PGT_SIZE,
1562306a36Sopenharmony_ci	MLXSW_RES_ID_MAX_KVD_LINEAR_RANGE,
1662306a36Sopenharmony_ci	MLXSW_RES_ID_MAX_KVD_ACTION_SETS,
1762306a36Sopenharmony_ci	MLXSW_RES_ID_MAX_TRAP_GROUPS,
1862306a36Sopenharmony_ci	MLXSW_RES_ID_CQE_V0,
1962306a36Sopenharmony_ci	MLXSW_RES_ID_CQE_V1,
2062306a36Sopenharmony_ci	MLXSW_RES_ID_CQE_V2,
2162306a36Sopenharmony_ci	MLXSW_RES_ID_COUNTER_POOL_SIZE,
2262306a36Sopenharmony_ci	MLXSW_RES_ID_COUNTER_BANK_SIZE,
2362306a36Sopenharmony_ci	MLXSW_RES_ID_MAX_SPAN,
2462306a36Sopenharmony_ci	MLXSW_RES_ID_COUNTER_SIZE_PACKETS_BYTES,
2562306a36Sopenharmony_ci	MLXSW_RES_ID_COUNTER_SIZE_ROUTER_BASIC,
2662306a36Sopenharmony_ci	MLXSW_RES_ID_MAX_SYSTEM_PORT,
2762306a36Sopenharmony_ci	MLXSW_RES_ID_FID,
2862306a36Sopenharmony_ci	MLXSW_RES_ID_MAX_LAG,
2962306a36Sopenharmony_ci	MLXSW_RES_ID_MAX_LAG_MEMBERS,
3062306a36Sopenharmony_ci	MLXSW_RES_ID_GUARANTEED_SHARED_BUFFER,
3162306a36Sopenharmony_ci	MLXSW_RES_ID_CELL_SIZE,
3262306a36Sopenharmony_ci	MLXSW_RES_ID_MAX_HEADROOM_SIZE,
3362306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_MAX_TCAM_REGIONS,
3462306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_MAX_TCAM_RULES,
3562306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_MAX_REGIONS,
3662306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_MAX_GROUPS,
3762306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_MAX_GROUP_SIZE,
3862306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_MAX_DEFAULT_ACTIONS,
3962306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_FLEX_KEYS,
4062306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_MAX_ACTION_PER_RULE,
4162306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_ACTIONS_PER_SET,
4262306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_MAX_L4_PORT_RANGE,
4362306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_MAX_ERPT_BANKS,
4462306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_MAX_ERPT_BANK_SIZE,
4562306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_MAX_LARGE_KEY_ID,
4662306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_ERPT_ENTRIES_2KB,
4762306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_ERPT_ENTRIES_4KB,
4862306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_ERPT_ENTRIES_8KB,
4962306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_ERPT_ENTRIES_12KB,
5062306a36Sopenharmony_ci	MLXSW_RES_ID_ACL_MAX_BF_LOG,
5162306a36Sopenharmony_ci	MLXSW_RES_ID_MAX_GLOBAL_POLICERS,
5262306a36Sopenharmony_ci	MLXSW_RES_ID_MAX_CPU_POLICERS,
5362306a36Sopenharmony_ci	MLXSW_RES_ID_MAX_VRS,
5462306a36Sopenharmony_ci	MLXSW_RES_ID_MAX_RIFS,
5562306a36Sopenharmony_ci	MLXSW_RES_ID_MC_ERIF_LIST_ENTRIES,
5662306a36Sopenharmony_ci	MLXSW_RES_ID_MAX_RIF_MAC_PROFILES,
5762306a36Sopenharmony_ci	MLXSW_RES_ID_MAX_LPM_TREES,
5862306a36Sopenharmony_ci	MLXSW_RES_ID_MAX_NVE_MC_ENTRIES_IPV4,
5962306a36Sopenharmony_ci	MLXSW_RES_ID_MAX_NVE_MC_ENTRIES_IPV6,
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci	/* Internal resources.
6262306a36Sopenharmony_ci	 * Determined by the SW, not queried from the HW.
6362306a36Sopenharmony_ci	 */
6462306a36Sopenharmony_ci	MLXSW_RES_ID_KVD_SINGLE_SIZE,
6562306a36Sopenharmony_ci	MLXSW_RES_ID_KVD_DOUBLE_SIZE,
6662306a36Sopenharmony_ci	MLXSW_RES_ID_KVD_LINEAR_SIZE,
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	__MLXSW_RES_ID_MAX,
6962306a36Sopenharmony_ci};
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_cistatic u16 mlxsw_res_ids[] = {
7262306a36Sopenharmony_ci	[MLXSW_RES_ID_KVD_SIZE] = 0x1001,
7362306a36Sopenharmony_ci	[MLXSW_RES_ID_KVD_SINGLE_MIN_SIZE] = 0x1002,
7462306a36Sopenharmony_ci	[MLXSW_RES_ID_KVD_DOUBLE_MIN_SIZE] = 0x1003,
7562306a36Sopenharmony_ci	[MLXSW_RES_ID_PGT_SIZE] = 0x1004,
7662306a36Sopenharmony_ci	[MLXSW_RES_ID_MAX_KVD_LINEAR_RANGE] = 0x1005,
7762306a36Sopenharmony_ci	[MLXSW_RES_ID_MAX_KVD_ACTION_SETS] = 0x1007,
7862306a36Sopenharmony_ci	[MLXSW_RES_ID_MAX_TRAP_GROUPS] = 0x2201,
7962306a36Sopenharmony_ci	[MLXSW_RES_ID_CQE_V0] = 0x2210,
8062306a36Sopenharmony_ci	[MLXSW_RES_ID_CQE_V1] = 0x2211,
8162306a36Sopenharmony_ci	[MLXSW_RES_ID_CQE_V2] = 0x2212,
8262306a36Sopenharmony_ci	[MLXSW_RES_ID_COUNTER_POOL_SIZE] = 0x2410,
8362306a36Sopenharmony_ci	[MLXSW_RES_ID_COUNTER_BANK_SIZE] = 0x2411,
8462306a36Sopenharmony_ci	[MLXSW_RES_ID_MAX_SPAN] = 0x2420,
8562306a36Sopenharmony_ci	[MLXSW_RES_ID_COUNTER_SIZE_PACKETS_BYTES] = 0x2443,
8662306a36Sopenharmony_ci	[MLXSW_RES_ID_COUNTER_SIZE_ROUTER_BASIC] = 0x2449,
8762306a36Sopenharmony_ci	[MLXSW_RES_ID_MAX_SYSTEM_PORT] = 0x2502,
8862306a36Sopenharmony_ci	[MLXSW_RES_ID_FID] = 0x2512,
8962306a36Sopenharmony_ci	[MLXSW_RES_ID_MAX_LAG] = 0x2520,
9062306a36Sopenharmony_ci	[MLXSW_RES_ID_MAX_LAG_MEMBERS] = 0x2521,
9162306a36Sopenharmony_ci	[MLXSW_RES_ID_GUARANTEED_SHARED_BUFFER] = 0x2805,	/* Bytes */
9262306a36Sopenharmony_ci	[MLXSW_RES_ID_CELL_SIZE] = 0x2803,	/* Bytes */
9362306a36Sopenharmony_ci	[MLXSW_RES_ID_MAX_HEADROOM_SIZE] = 0x2811,	/* Bytes */
9462306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_MAX_TCAM_REGIONS] = 0x2901,
9562306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_MAX_TCAM_RULES] = 0x2902,
9662306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_MAX_REGIONS] = 0x2903,
9762306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_MAX_GROUPS] = 0x2904,
9862306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_MAX_GROUP_SIZE] = 0x2905,
9962306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_MAX_DEFAULT_ACTIONS] = 0x2908,
10062306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_FLEX_KEYS] = 0x2910,
10162306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_MAX_ACTION_PER_RULE] = 0x2911,
10262306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_ACTIONS_PER_SET] = 0x2912,
10362306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_MAX_L4_PORT_RANGE] = 0x2920,
10462306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_MAX_ERPT_BANKS] = 0x2940,
10562306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_MAX_ERPT_BANK_SIZE] = 0x2941,
10662306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_MAX_LARGE_KEY_ID] = 0x2942,
10762306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_ERPT_ENTRIES_2KB] = 0x2950,
10862306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_ERPT_ENTRIES_4KB] = 0x2951,
10962306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_ERPT_ENTRIES_8KB] = 0x2952,
11062306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_ERPT_ENTRIES_12KB] = 0x2953,
11162306a36Sopenharmony_ci	[MLXSW_RES_ID_ACL_MAX_BF_LOG] = 0x2960,
11262306a36Sopenharmony_ci	[MLXSW_RES_ID_MAX_GLOBAL_POLICERS] = 0x2A10,
11362306a36Sopenharmony_ci	[MLXSW_RES_ID_MAX_CPU_POLICERS] = 0x2A13,
11462306a36Sopenharmony_ci	[MLXSW_RES_ID_MAX_VRS] = 0x2C01,
11562306a36Sopenharmony_ci	[MLXSW_RES_ID_MAX_RIFS] = 0x2C02,
11662306a36Sopenharmony_ci	[MLXSW_RES_ID_MC_ERIF_LIST_ENTRIES] = 0x2C10,
11762306a36Sopenharmony_ci	[MLXSW_RES_ID_MAX_RIF_MAC_PROFILES] = 0x2C14,
11862306a36Sopenharmony_ci	[MLXSW_RES_ID_MAX_LPM_TREES] = 0x2C30,
11962306a36Sopenharmony_ci	[MLXSW_RES_ID_MAX_NVE_MC_ENTRIES_IPV4] = 0x2E02,
12062306a36Sopenharmony_ci	[MLXSW_RES_ID_MAX_NVE_MC_ENTRIES_IPV6] = 0x2E03,
12162306a36Sopenharmony_ci};
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_cistruct mlxsw_res {
12462306a36Sopenharmony_ci	bool valid[__MLXSW_RES_ID_MAX];
12562306a36Sopenharmony_ci	u64 values[__MLXSW_RES_ID_MAX];
12662306a36Sopenharmony_ci};
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_cistatic inline bool mlxsw_res_valid(struct mlxsw_res *res,
12962306a36Sopenharmony_ci				   enum mlxsw_res_id res_id)
13062306a36Sopenharmony_ci{
13162306a36Sopenharmony_ci	return res->valid[res_id];
13262306a36Sopenharmony_ci}
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci#define MLXSW_RES_VALID(res, short_res_id)			\
13562306a36Sopenharmony_ci	mlxsw_res_valid(res, MLXSW_RES_ID_##short_res_id)
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_cistatic inline u64 mlxsw_res_get(struct mlxsw_res *res,
13862306a36Sopenharmony_ci				enum mlxsw_res_id res_id)
13962306a36Sopenharmony_ci{
14062306a36Sopenharmony_ci	if (WARN_ON(!res->valid[res_id]))
14162306a36Sopenharmony_ci		return 0;
14262306a36Sopenharmony_ci	return res->values[res_id];
14362306a36Sopenharmony_ci}
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci#define MLXSW_RES_GET(res, short_res_id)			\
14662306a36Sopenharmony_ci	mlxsw_res_get(res, MLXSW_RES_ID_##short_res_id)
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_cistatic inline void mlxsw_res_set(struct mlxsw_res *res,
14962306a36Sopenharmony_ci				 enum mlxsw_res_id res_id, u64 value)
15062306a36Sopenharmony_ci{
15162306a36Sopenharmony_ci	res->valid[res_id] = true;
15262306a36Sopenharmony_ci	res->values[res_id] = value;
15362306a36Sopenharmony_ci}
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci#define MLXSW_RES_SET(res, short_res_id, value)			\
15662306a36Sopenharmony_ci	mlxsw_res_set(res, MLXSW_RES_ID_##short_res_id, value)
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_cistatic inline void mlxsw_res_parse(struct mlxsw_res *res, u16 id, u64 value)
15962306a36Sopenharmony_ci{
16062306a36Sopenharmony_ci	int i;
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(mlxsw_res_ids); i++) {
16362306a36Sopenharmony_ci		if (mlxsw_res_ids[i] == id) {
16462306a36Sopenharmony_ci			mlxsw_res_set(res, i, value);
16562306a36Sopenharmony_ci			return;
16662306a36Sopenharmony_ci		}
16762306a36Sopenharmony_ci	}
16862306a36Sopenharmony_ci}
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci#endif
171