18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/kernel.h>
58c2ecf20Sopenharmony_ci#include <linux/mutex.h>
68c2ecf20Sopenharmony_ci#include <net/devlink.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include "spectrum.h"
98c2ecf20Sopenharmony_ci#include "spectrum_dpipe.h"
108c2ecf20Sopenharmony_ci#include "spectrum_router.h"
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cienum mlxsw_sp_field_metadata_id {
138c2ecf20Sopenharmony_ci	MLXSW_SP_DPIPE_FIELD_METADATA_ERIF_PORT,
148c2ecf20Sopenharmony_ci	MLXSW_SP_DPIPE_FIELD_METADATA_L3_FORWARD,
158c2ecf20Sopenharmony_ci	MLXSW_SP_DPIPE_FIELD_METADATA_L3_DROP,
168c2ecf20Sopenharmony_ci	MLXSW_SP_DPIPE_FIELD_METADATA_ADJ_INDEX,
178c2ecf20Sopenharmony_ci	MLXSW_SP_DPIPE_FIELD_METADATA_ADJ_SIZE,
188c2ecf20Sopenharmony_ci	MLXSW_SP_DPIPE_FIELD_METADATA_ADJ_HASH_INDEX,
198c2ecf20Sopenharmony_ci};
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic struct devlink_dpipe_field mlxsw_sp_dpipe_fields_metadata[] = {
228c2ecf20Sopenharmony_ci	{
238c2ecf20Sopenharmony_ci		.name = "erif_port",
248c2ecf20Sopenharmony_ci		.id = MLXSW_SP_DPIPE_FIELD_METADATA_ERIF_PORT,
258c2ecf20Sopenharmony_ci		.bitwidth = 32,
268c2ecf20Sopenharmony_ci		.mapping_type = DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX,
278c2ecf20Sopenharmony_ci	},
288c2ecf20Sopenharmony_ci	{
298c2ecf20Sopenharmony_ci		.name = "l3_forward",
308c2ecf20Sopenharmony_ci		.id = MLXSW_SP_DPIPE_FIELD_METADATA_L3_FORWARD,
318c2ecf20Sopenharmony_ci		.bitwidth = 1,
328c2ecf20Sopenharmony_ci	},
338c2ecf20Sopenharmony_ci	{
348c2ecf20Sopenharmony_ci		.name = "l3_drop",
358c2ecf20Sopenharmony_ci		.id = MLXSW_SP_DPIPE_FIELD_METADATA_L3_DROP,
368c2ecf20Sopenharmony_ci		.bitwidth = 1,
378c2ecf20Sopenharmony_ci	},
388c2ecf20Sopenharmony_ci	{
398c2ecf20Sopenharmony_ci		.name = "adj_index",
408c2ecf20Sopenharmony_ci		.id = MLXSW_SP_DPIPE_FIELD_METADATA_ADJ_INDEX,
418c2ecf20Sopenharmony_ci		.bitwidth = 32,
428c2ecf20Sopenharmony_ci	},
438c2ecf20Sopenharmony_ci	{
448c2ecf20Sopenharmony_ci		.name = "adj_size",
458c2ecf20Sopenharmony_ci		.id = MLXSW_SP_DPIPE_FIELD_METADATA_ADJ_SIZE,
468c2ecf20Sopenharmony_ci		.bitwidth = 32,
478c2ecf20Sopenharmony_ci	},
488c2ecf20Sopenharmony_ci	{
498c2ecf20Sopenharmony_ci		.name = "adj_hash_index",
508c2ecf20Sopenharmony_ci		.id = MLXSW_SP_DPIPE_FIELD_METADATA_ADJ_HASH_INDEX,
518c2ecf20Sopenharmony_ci		.bitwidth = 32,
528c2ecf20Sopenharmony_ci	},
538c2ecf20Sopenharmony_ci};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cienum mlxsw_sp_dpipe_header_id {
568c2ecf20Sopenharmony_ci	MLXSW_SP_DPIPE_HEADER_METADATA,
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic struct devlink_dpipe_header mlxsw_sp_dpipe_header_metadata = {
608c2ecf20Sopenharmony_ci	.name = "mlxsw_meta",
618c2ecf20Sopenharmony_ci	.id = MLXSW_SP_DPIPE_HEADER_METADATA,
628c2ecf20Sopenharmony_ci	.fields = mlxsw_sp_dpipe_fields_metadata,
638c2ecf20Sopenharmony_ci	.fields_count = ARRAY_SIZE(mlxsw_sp_dpipe_fields_metadata),
648c2ecf20Sopenharmony_ci};
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic struct devlink_dpipe_header *mlxsw_dpipe_headers[] = {
678c2ecf20Sopenharmony_ci	&mlxsw_sp_dpipe_header_metadata,
688c2ecf20Sopenharmony_ci	&devlink_dpipe_header_ethernet,
698c2ecf20Sopenharmony_ci	&devlink_dpipe_header_ipv4,
708c2ecf20Sopenharmony_ci	&devlink_dpipe_header_ipv6,
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic struct devlink_dpipe_headers mlxsw_sp_dpipe_headers = {
748c2ecf20Sopenharmony_ci	.headers = mlxsw_dpipe_headers,
758c2ecf20Sopenharmony_ci	.headers_count = ARRAY_SIZE(mlxsw_dpipe_headers),
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic int mlxsw_sp_dpipe_table_erif_actions_dump(void *priv,
798c2ecf20Sopenharmony_ci						  struct sk_buff *skb)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	struct devlink_dpipe_action action = {0};
828c2ecf20Sopenharmony_ci	int err;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	action.type = DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY;
858c2ecf20Sopenharmony_ci	action.header = &mlxsw_sp_dpipe_header_metadata;
868c2ecf20Sopenharmony_ci	action.field_id = MLXSW_SP_DPIPE_FIELD_METADATA_L3_FORWARD;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	err = devlink_dpipe_action_put(skb, &action);
898c2ecf20Sopenharmony_ci	if (err)
908c2ecf20Sopenharmony_ci		return err;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	action.type = DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY;
938c2ecf20Sopenharmony_ci	action.header = &mlxsw_sp_dpipe_header_metadata;
948c2ecf20Sopenharmony_ci	action.field_id = MLXSW_SP_DPIPE_FIELD_METADATA_L3_DROP;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	return devlink_dpipe_action_put(skb, &action);
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic int mlxsw_sp_dpipe_table_erif_matches_dump(void *priv,
1008c2ecf20Sopenharmony_ci						  struct sk_buff *skb)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	struct devlink_dpipe_match match = {0};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	match.type = DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT;
1058c2ecf20Sopenharmony_ci	match.header = &mlxsw_sp_dpipe_header_metadata;
1068c2ecf20Sopenharmony_ci	match.field_id = MLXSW_SP_DPIPE_FIELD_METADATA_ERIF_PORT;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	return devlink_dpipe_match_put(skb, &match);
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic void
1128c2ecf20Sopenharmony_cimlxsw_sp_erif_match_action_prepare(struct devlink_dpipe_match *match,
1138c2ecf20Sopenharmony_ci				   struct devlink_dpipe_action *action)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	action->type = DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY;
1168c2ecf20Sopenharmony_ci	action->header = &mlxsw_sp_dpipe_header_metadata;
1178c2ecf20Sopenharmony_ci	action->field_id = MLXSW_SP_DPIPE_FIELD_METADATA_L3_FORWARD;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	match->type = DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT;
1208c2ecf20Sopenharmony_ci	match->header = &mlxsw_sp_dpipe_header_metadata;
1218c2ecf20Sopenharmony_ci	match->field_id = MLXSW_SP_DPIPE_FIELD_METADATA_ERIF_PORT;
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic int mlxsw_sp_erif_entry_prepare(struct devlink_dpipe_entry *entry,
1258c2ecf20Sopenharmony_ci				       struct devlink_dpipe_value *match_value,
1268c2ecf20Sopenharmony_ci				       struct devlink_dpipe_match *match,
1278c2ecf20Sopenharmony_ci				       struct devlink_dpipe_value *action_value,
1288c2ecf20Sopenharmony_ci				       struct devlink_dpipe_action *action)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	entry->match_values = match_value;
1318c2ecf20Sopenharmony_ci	entry->match_values_count = 1;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	entry->action_values = action_value;
1348c2ecf20Sopenharmony_ci	entry->action_values_count = 1;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	match_value->match = match;
1378c2ecf20Sopenharmony_ci	match_value->value_size = sizeof(u32);
1388c2ecf20Sopenharmony_ci	match_value->value = kmalloc(match_value->value_size, GFP_KERNEL);
1398c2ecf20Sopenharmony_ci	if (!match_value->value)
1408c2ecf20Sopenharmony_ci		return -ENOMEM;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	action_value->action = action;
1438c2ecf20Sopenharmony_ci	action_value->value_size = sizeof(u32);
1448c2ecf20Sopenharmony_ci	action_value->value = kmalloc(action_value->value_size, GFP_KERNEL);
1458c2ecf20Sopenharmony_ci	if (!action_value->value)
1468c2ecf20Sopenharmony_ci		goto err_action_alloc;
1478c2ecf20Sopenharmony_ci	return 0;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cierr_action_alloc:
1508c2ecf20Sopenharmony_ci	kfree(match_value->value);
1518c2ecf20Sopenharmony_ci	return -ENOMEM;
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic int mlxsw_sp_erif_entry_get(struct mlxsw_sp *mlxsw_sp,
1558c2ecf20Sopenharmony_ci				   struct devlink_dpipe_entry *entry,
1568c2ecf20Sopenharmony_ci				   struct mlxsw_sp_rif *rif,
1578c2ecf20Sopenharmony_ci				   bool counters_enabled)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	u32 *action_value;
1608c2ecf20Sopenharmony_ci	u32 *rif_value;
1618c2ecf20Sopenharmony_ci	u64 cnt;
1628c2ecf20Sopenharmony_ci	int err;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	/* Set Match RIF index */
1658c2ecf20Sopenharmony_ci	rif_value = entry->match_values->value;
1668c2ecf20Sopenharmony_ci	*rif_value = mlxsw_sp_rif_index(rif);
1678c2ecf20Sopenharmony_ci	entry->match_values->mapping_value = mlxsw_sp_rif_dev_ifindex(rif);
1688c2ecf20Sopenharmony_ci	entry->match_values->mapping_valid = true;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	/* Set Action Forwarding */
1718c2ecf20Sopenharmony_ci	action_value = entry->action_values->value;
1728c2ecf20Sopenharmony_ci	*action_value = 1;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	entry->counter_valid = false;
1758c2ecf20Sopenharmony_ci	entry->counter = 0;
1768c2ecf20Sopenharmony_ci	entry->index = mlxsw_sp_rif_index(rif);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	if (!counters_enabled)
1798c2ecf20Sopenharmony_ci		return 0;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	err = mlxsw_sp_rif_counter_value_get(mlxsw_sp, rif,
1828c2ecf20Sopenharmony_ci					     MLXSW_SP_RIF_COUNTER_EGRESS,
1838c2ecf20Sopenharmony_ci					     &cnt);
1848c2ecf20Sopenharmony_ci	if (!err) {
1858c2ecf20Sopenharmony_ci		entry->counter = cnt;
1868c2ecf20Sopenharmony_ci		entry->counter_valid = true;
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci	return 0;
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic int
1928c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_erif_entries_dump(void *priv, bool counters_enabled,
1938c2ecf20Sopenharmony_ci				       struct devlink_dpipe_dump_ctx *dump_ctx)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	struct devlink_dpipe_value match_value, action_value;
1968c2ecf20Sopenharmony_ci	struct devlink_dpipe_action action = {0};
1978c2ecf20Sopenharmony_ci	struct devlink_dpipe_match match = {0};
1988c2ecf20Sopenharmony_ci	struct devlink_dpipe_entry entry = {0};
1998c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = priv;
2008c2ecf20Sopenharmony_ci	unsigned int rif_count;
2018c2ecf20Sopenharmony_ci	int i, j;
2028c2ecf20Sopenharmony_ci	int err;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	memset(&match_value, 0, sizeof(match_value));
2058c2ecf20Sopenharmony_ci	memset(&action_value, 0, sizeof(action_value));
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	mlxsw_sp_erif_match_action_prepare(&match, &action);
2088c2ecf20Sopenharmony_ci	err = mlxsw_sp_erif_entry_prepare(&entry, &match_value, &match,
2098c2ecf20Sopenharmony_ci					  &action_value, &action);
2108c2ecf20Sopenharmony_ci	if (err)
2118c2ecf20Sopenharmony_ci		return err;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	rif_count = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
2148c2ecf20Sopenharmony_ci	mutex_lock(&mlxsw_sp->router->lock);
2158c2ecf20Sopenharmony_ci	i = 0;
2168c2ecf20Sopenharmony_cistart_again:
2178c2ecf20Sopenharmony_ci	err = devlink_dpipe_entry_ctx_prepare(dump_ctx);
2188c2ecf20Sopenharmony_ci	if (err)
2198c2ecf20Sopenharmony_ci		goto err_ctx_prepare;
2208c2ecf20Sopenharmony_ci	j = 0;
2218c2ecf20Sopenharmony_ci	for (; i < rif_count; i++) {
2228c2ecf20Sopenharmony_ci		struct mlxsw_sp_rif *rif = mlxsw_sp_rif_by_index(mlxsw_sp, i);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci		if (!rif || !mlxsw_sp_rif_dev(rif))
2258c2ecf20Sopenharmony_ci			continue;
2268c2ecf20Sopenharmony_ci		err = mlxsw_sp_erif_entry_get(mlxsw_sp, &entry, rif,
2278c2ecf20Sopenharmony_ci					      counters_enabled);
2288c2ecf20Sopenharmony_ci		if (err)
2298c2ecf20Sopenharmony_ci			goto err_entry_get;
2308c2ecf20Sopenharmony_ci		err = devlink_dpipe_entry_ctx_append(dump_ctx, &entry);
2318c2ecf20Sopenharmony_ci		if (err) {
2328c2ecf20Sopenharmony_ci			if (err == -EMSGSIZE) {
2338c2ecf20Sopenharmony_ci				if (!j)
2348c2ecf20Sopenharmony_ci					goto err_entry_append;
2358c2ecf20Sopenharmony_ci				break;
2368c2ecf20Sopenharmony_ci			}
2378c2ecf20Sopenharmony_ci			goto err_entry_append;
2388c2ecf20Sopenharmony_ci		}
2398c2ecf20Sopenharmony_ci		j++;
2408c2ecf20Sopenharmony_ci	}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	devlink_dpipe_entry_ctx_close(dump_ctx);
2438c2ecf20Sopenharmony_ci	if (i != rif_count)
2448c2ecf20Sopenharmony_ci		goto start_again;
2458c2ecf20Sopenharmony_ci	mutex_unlock(&mlxsw_sp->router->lock);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	devlink_dpipe_entry_clear(&entry);
2488c2ecf20Sopenharmony_ci	return 0;
2498c2ecf20Sopenharmony_cierr_entry_append:
2508c2ecf20Sopenharmony_cierr_entry_get:
2518c2ecf20Sopenharmony_cierr_ctx_prepare:
2528c2ecf20Sopenharmony_ci	mutex_unlock(&mlxsw_sp->router->lock);
2538c2ecf20Sopenharmony_ci	devlink_dpipe_entry_clear(&entry);
2548c2ecf20Sopenharmony_ci	return err;
2558c2ecf20Sopenharmony_ci}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic int mlxsw_sp_dpipe_table_erif_counters_update(void *priv, bool enable)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = priv;
2608c2ecf20Sopenharmony_ci	int i;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	mutex_lock(&mlxsw_sp->router->lock);
2638c2ecf20Sopenharmony_ci	for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++) {
2648c2ecf20Sopenharmony_ci		struct mlxsw_sp_rif *rif = mlxsw_sp_rif_by_index(mlxsw_sp, i);
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci		if (!rif)
2678c2ecf20Sopenharmony_ci			continue;
2688c2ecf20Sopenharmony_ci		if (enable)
2698c2ecf20Sopenharmony_ci			mlxsw_sp_rif_counter_alloc(mlxsw_sp, rif,
2708c2ecf20Sopenharmony_ci						   MLXSW_SP_RIF_COUNTER_EGRESS);
2718c2ecf20Sopenharmony_ci		else
2728c2ecf20Sopenharmony_ci			mlxsw_sp_rif_counter_free(mlxsw_sp, rif,
2738c2ecf20Sopenharmony_ci						  MLXSW_SP_RIF_COUNTER_EGRESS);
2748c2ecf20Sopenharmony_ci	}
2758c2ecf20Sopenharmony_ci	mutex_unlock(&mlxsw_sp->router->lock);
2768c2ecf20Sopenharmony_ci	return 0;
2778c2ecf20Sopenharmony_ci}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_cistatic u64 mlxsw_sp_dpipe_table_erif_size_get(void *priv)
2808c2ecf20Sopenharmony_ci{
2818c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = priv;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	return MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistatic struct devlink_dpipe_table_ops mlxsw_sp_erif_ops = {
2878c2ecf20Sopenharmony_ci	.matches_dump = mlxsw_sp_dpipe_table_erif_matches_dump,
2888c2ecf20Sopenharmony_ci	.actions_dump = mlxsw_sp_dpipe_table_erif_actions_dump,
2898c2ecf20Sopenharmony_ci	.entries_dump = mlxsw_sp_dpipe_table_erif_entries_dump,
2908c2ecf20Sopenharmony_ci	.counters_set_update = mlxsw_sp_dpipe_table_erif_counters_update,
2918c2ecf20Sopenharmony_ci	.size_get = mlxsw_sp_dpipe_table_erif_size_get,
2928c2ecf20Sopenharmony_ci};
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_cistatic int mlxsw_sp_dpipe_erif_table_init(struct mlxsw_sp *mlxsw_sp)
2958c2ecf20Sopenharmony_ci{
2968c2ecf20Sopenharmony_ci	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	return devlink_dpipe_table_register(devlink,
2998c2ecf20Sopenharmony_ci					    MLXSW_SP_DPIPE_TABLE_NAME_ERIF,
3008c2ecf20Sopenharmony_ci					    &mlxsw_sp_erif_ops,
3018c2ecf20Sopenharmony_ci					    mlxsw_sp, false);
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic void mlxsw_sp_dpipe_erif_table_fini(struct mlxsw_sp *mlxsw_sp)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	devlink_dpipe_table_unregister(devlink, MLXSW_SP_DPIPE_TABLE_NAME_ERIF);
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_cistatic int mlxsw_sp_dpipe_table_host_matches_dump(struct sk_buff *skb, int type)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	struct devlink_dpipe_match match = {0};
3148c2ecf20Sopenharmony_ci	int err;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	match.type = DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT;
3178c2ecf20Sopenharmony_ci	match.header = &mlxsw_sp_dpipe_header_metadata;
3188c2ecf20Sopenharmony_ci	match.field_id = MLXSW_SP_DPIPE_FIELD_METADATA_ERIF_PORT;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	err = devlink_dpipe_match_put(skb, &match);
3218c2ecf20Sopenharmony_ci	if (err)
3228c2ecf20Sopenharmony_ci		return err;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	switch (type) {
3258c2ecf20Sopenharmony_ci	case AF_INET:
3268c2ecf20Sopenharmony_ci		match.type = DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT;
3278c2ecf20Sopenharmony_ci		match.header = &devlink_dpipe_header_ipv4;
3288c2ecf20Sopenharmony_ci		match.field_id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP;
3298c2ecf20Sopenharmony_ci		break;
3308c2ecf20Sopenharmony_ci	case AF_INET6:
3318c2ecf20Sopenharmony_ci		match.type = DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT;
3328c2ecf20Sopenharmony_ci		match.header = &devlink_dpipe_header_ipv6;
3338c2ecf20Sopenharmony_ci		match.field_id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP;
3348c2ecf20Sopenharmony_ci		break;
3358c2ecf20Sopenharmony_ci	default:
3368c2ecf20Sopenharmony_ci		WARN_ON(1);
3378c2ecf20Sopenharmony_ci		return -EINVAL;
3388c2ecf20Sopenharmony_ci	}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	return devlink_dpipe_match_put(skb, &match);
3418c2ecf20Sopenharmony_ci}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_cistatic int
3448c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_host4_matches_dump(void *priv, struct sk_buff *skb)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	return mlxsw_sp_dpipe_table_host_matches_dump(skb, AF_INET);
3478c2ecf20Sopenharmony_ci}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_cistatic int
3508c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_host_actions_dump(void *priv, struct sk_buff *skb)
3518c2ecf20Sopenharmony_ci{
3528c2ecf20Sopenharmony_ci	struct devlink_dpipe_action action = {0};
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	action.type = DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY;
3558c2ecf20Sopenharmony_ci	action.header = &devlink_dpipe_header_ethernet;
3568c2ecf20Sopenharmony_ci	action.field_id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	return devlink_dpipe_action_put(skb, &action);
3598c2ecf20Sopenharmony_ci}
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_cienum mlxsw_sp_dpipe_table_host_match {
3628c2ecf20Sopenharmony_ci	MLXSW_SP_DPIPE_TABLE_HOST_MATCH_RIF,
3638c2ecf20Sopenharmony_ci	MLXSW_SP_DPIPE_TABLE_HOST_MATCH_DIP,
3648c2ecf20Sopenharmony_ci	MLXSW_SP_DPIPE_TABLE_HOST_MATCH_COUNT,
3658c2ecf20Sopenharmony_ci};
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_cistatic void
3688c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_host_match_action_prepare(struct devlink_dpipe_match *matches,
3698c2ecf20Sopenharmony_ci					       struct devlink_dpipe_action *action,
3708c2ecf20Sopenharmony_ci					       int type)
3718c2ecf20Sopenharmony_ci{
3728c2ecf20Sopenharmony_ci	struct devlink_dpipe_match *match;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	match = &matches[MLXSW_SP_DPIPE_TABLE_HOST_MATCH_RIF];
3758c2ecf20Sopenharmony_ci	match->type = DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT;
3768c2ecf20Sopenharmony_ci	match->header = &mlxsw_sp_dpipe_header_metadata;
3778c2ecf20Sopenharmony_ci	match->field_id = MLXSW_SP_DPIPE_FIELD_METADATA_ERIF_PORT;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	match = &matches[MLXSW_SP_DPIPE_TABLE_HOST_MATCH_DIP];
3808c2ecf20Sopenharmony_ci	match->type = DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT;
3818c2ecf20Sopenharmony_ci	switch (type) {
3828c2ecf20Sopenharmony_ci	case AF_INET:
3838c2ecf20Sopenharmony_ci		match->header = &devlink_dpipe_header_ipv4;
3848c2ecf20Sopenharmony_ci		match->field_id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP;
3858c2ecf20Sopenharmony_ci		break;
3868c2ecf20Sopenharmony_ci	case AF_INET6:
3878c2ecf20Sopenharmony_ci		match->header = &devlink_dpipe_header_ipv6;
3888c2ecf20Sopenharmony_ci		match->field_id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP;
3898c2ecf20Sopenharmony_ci		break;
3908c2ecf20Sopenharmony_ci	default:
3918c2ecf20Sopenharmony_ci		WARN_ON(1);
3928c2ecf20Sopenharmony_ci		return;
3938c2ecf20Sopenharmony_ci	}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	action->type = DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY;
3968c2ecf20Sopenharmony_ci	action->header = &devlink_dpipe_header_ethernet;
3978c2ecf20Sopenharmony_ci	action->field_id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC;
3988c2ecf20Sopenharmony_ci}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_cistatic int
4018c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_host_entry_prepare(struct devlink_dpipe_entry *entry,
4028c2ecf20Sopenharmony_ci					struct devlink_dpipe_value *match_values,
4038c2ecf20Sopenharmony_ci					struct devlink_dpipe_match *matches,
4048c2ecf20Sopenharmony_ci					struct devlink_dpipe_value *action_value,
4058c2ecf20Sopenharmony_ci					struct devlink_dpipe_action *action,
4068c2ecf20Sopenharmony_ci					int type)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	struct devlink_dpipe_value *match_value;
4098c2ecf20Sopenharmony_ci	struct devlink_dpipe_match *match;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	entry->match_values = match_values;
4128c2ecf20Sopenharmony_ci	entry->match_values_count = MLXSW_SP_DPIPE_TABLE_HOST_MATCH_COUNT;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	entry->action_values = action_value;
4158c2ecf20Sopenharmony_ci	entry->action_values_count = 1;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	match = &matches[MLXSW_SP_DPIPE_TABLE_HOST_MATCH_RIF];
4188c2ecf20Sopenharmony_ci	match_value = &match_values[MLXSW_SP_DPIPE_TABLE_HOST_MATCH_RIF];
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	match_value->match = match;
4218c2ecf20Sopenharmony_ci	match_value->value_size = sizeof(u32);
4228c2ecf20Sopenharmony_ci	match_value->value = kmalloc(match_value->value_size, GFP_KERNEL);
4238c2ecf20Sopenharmony_ci	if (!match_value->value)
4248c2ecf20Sopenharmony_ci		return -ENOMEM;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	match = &matches[MLXSW_SP_DPIPE_TABLE_HOST_MATCH_DIP];
4278c2ecf20Sopenharmony_ci	match_value = &match_values[MLXSW_SP_DPIPE_TABLE_HOST_MATCH_DIP];
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	match_value->match = match;
4308c2ecf20Sopenharmony_ci	switch (type) {
4318c2ecf20Sopenharmony_ci	case AF_INET:
4328c2ecf20Sopenharmony_ci		match_value->value_size = sizeof(u32);
4338c2ecf20Sopenharmony_ci		break;
4348c2ecf20Sopenharmony_ci	case AF_INET6:
4358c2ecf20Sopenharmony_ci		match_value->value_size = sizeof(struct in6_addr);
4368c2ecf20Sopenharmony_ci		break;
4378c2ecf20Sopenharmony_ci	default:
4388c2ecf20Sopenharmony_ci		WARN_ON(1);
4398c2ecf20Sopenharmony_ci		return -EINVAL;
4408c2ecf20Sopenharmony_ci	}
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	match_value->value = kmalloc(match_value->value_size, GFP_KERNEL);
4438c2ecf20Sopenharmony_ci	if (!match_value->value)
4448c2ecf20Sopenharmony_ci		return -ENOMEM;
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	action_value->action = action;
4478c2ecf20Sopenharmony_ci	action_value->value_size = sizeof(u64);
4488c2ecf20Sopenharmony_ci	action_value->value = kmalloc(action_value->value_size, GFP_KERNEL);
4498c2ecf20Sopenharmony_ci	if (!action_value->value)
4508c2ecf20Sopenharmony_ci		return -ENOMEM;
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	return 0;
4538c2ecf20Sopenharmony_ci}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_cistatic void
4568c2ecf20Sopenharmony_ci__mlxsw_sp_dpipe_table_host_entry_fill(struct devlink_dpipe_entry *entry,
4578c2ecf20Sopenharmony_ci				       struct mlxsw_sp_rif *rif,
4588c2ecf20Sopenharmony_ci				       unsigned char *ha, void *dip)
4598c2ecf20Sopenharmony_ci{
4608c2ecf20Sopenharmony_ci	struct devlink_dpipe_value *value;
4618c2ecf20Sopenharmony_ci	u32 *rif_value;
4628c2ecf20Sopenharmony_ci	u8 *ha_value;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	/* Set Match RIF index */
4658c2ecf20Sopenharmony_ci	value = &entry->match_values[MLXSW_SP_DPIPE_TABLE_HOST_MATCH_RIF];
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	rif_value = value->value;
4688c2ecf20Sopenharmony_ci	*rif_value = mlxsw_sp_rif_index(rif);
4698c2ecf20Sopenharmony_ci	value->mapping_value = mlxsw_sp_rif_dev_ifindex(rif);
4708c2ecf20Sopenharmony_ci	value->mapping_valid = true;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	/* Set Match DIP */
4738c2ecf20Sopenharmony_ci	value = &entry->match_values[MLXSW_SP_DPIPE_TABLE_HOST_MATCH_DIP];
4748c2ecf20Sopenharmony_ci	memcpy(value->value, dip, value->value_size);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	/* Set Action DMAC */
4778c2ecf20Sopenharmony_ci	value = entry->action_values;
4788c2ecf20Sopenharmony_ci	ha_value = value->value;
4798c2ecf20Sopenharmony_ci	ether_addr_copy(ha_value, ha);
4808c2ecf20Sopenharmony_ci}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_cistatic void
4838c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_host4_entry_fill(struct devlink_dpipe_entry *entry,
4848c2ecf20Sopenharmony_ci				      struct mlxsw_sp_neigh_entry *neigh_entry,
4858c2ecf20Sopenharmony_ci				      struct mlxsw_sp_rif *rif)
4868c2ecf20Sopenharmony_ci{
4878c2ecf20Sopenharmony_ci	unsigned char *ha;
4888c2ecf20Sopenharmony_ci	u32 dip;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	ha = mlxsw_sp_neigh_entry_ha(neigh_entry);
4918c2ecf20Sopenharmony_ci	dip = mlxsw_sp_neigh4_entry_dip(neigh_entry);
4928c2ecf20Sopenharmony_ci	__mlxsw_sp_dpipe_table_host_entry_fill(entry, rif, ha, &dip);
4938c2ecf20Sopenharmony_ci}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_cistatic void
4968c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_host6_entry_fill(struct devlink_dpipe_entry *entry,
4978c2ecf20Sopenharmony_ci				      struct mlxsw_sp_neigh_entry *neigh_entry,
4988c2ecf20Sopenharmony_ci				      struct mlxsw_sp_rif *rif)
4998c2ecf20Sopenharmony_ci{
5008c2ecf20Sopenharmony_ci	struct in6_addr *dip;
5018c2ecf20Sopenharmony_ci	unsigned char *ha;
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	ha = mlxsw_sp_neigh_entry_ha(neigh_entry);
5048c2ecf20Sopenharmony_ci	dip = mlxsw_sp_neigh6_entry_dip(neigh_entry);
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	__mlxsw_sp_dpipe_table_host_entry_fill(entry, rif, ha, dip);
5078c2ecf20Sopenharmony_ci}
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_cistatic void
5108c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_host_entry_fill(struct mlxsw_sp *mlxsw_sp,
5118c2ecf20Sopenharmony_ci				     struct devlink_dpipe_entry *entry,
5128c2ecf20Sopenharmony_ci				     struct mlxsw_sp_neigh_entry *neigh_entry,
5138c2ecf20Sopenharmony_ci				     struct mlxsw_sp_rif *rif,
5148c2ecf20Sopenharmony_ci				     int type)
5158c2ecf20Sopenharmony_ci{
5168c2ecf20Sopenharmony_ci	int err;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	switch (type) {
5198c2ecf20Sopenharmony_ci	case AF_INET:
5208c2ecf20Sopenharmony_ci		mlxsw_sp_dpipe_table_host4_entry_fill(entry, neigh_entry, rif);
5218c2ecf20Sopenharmony_ci		break;
5228c2ecf20Sopenharmony_ci	case AF_INET6:
5238c2ecf20Sopenharmony_ci		mlxsw_sp_dpipe_table_host6_entry_fill(entry, neigh_entry, rif);
5248c2ecf20Sopenharmony_ci		break;
5258c2ecf20Sopenharmony_ci	default:
5268c2ecf20Sopenharmony_ci		WARN_ON(1);
5278c2ecf20Sopenharmony_ci		return;
5288c2ecf20Sopenharmony_ci	}
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	err = mlxsw_sp_neigh_counter_get(mlxsw_sp, neigh_entry,
5318c2ecf20Sopenharmony_ci					 &entry->counter);
5328c2ecf20Sopenharmony_ci	if (!err)
5338c2ecf20Sopenharmony_ci		entry->counter_valid = true;
5348c2ecf20Sopenharmony_ci}
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_cistatic int
5378c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_host_entries_get(struct mlxsw_sp *mlxsw_sp,
5388c2ecf20Sopenharmony_ci				      struct devlink_dpipe_entry *entry,
5398c2ecf20Sopenharmony_ci				      bool counters_enabled,
5408c2ecf20Sopenharmony_ci				      struct devlink_dpipe_dump_ctx *dump_ctx,
5418c2ecf20Sopenharmony_ci				      int type)
5428c2ecf20Sopenharmony_ci{
5438c2ecf20Sopenharmony_ci	int rif_neigh_count = 0;
5448c2ecf20Sopenharmony_ci	int rif_neigh_skip = 0;
5458c2ecf20Sopenharmony_ci	int neigh_count = 0;
5468c2ecf20Sopenharmony_ci	int rif_count;
5478c2ecf20Sopenharmony_ci	int i, j;
5488c2ecf20Sopenharmony_ci	int err;
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	mutex_lock(&mlxsw_sp->router->lock);
5518c2ecf20Sopenharmony_ci	i = 0;
5528c2ecf20Sopenharmony_ci	rif_count = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
5538c2ecf20Sopenharmony_cistart_again:
5548c2ecf20Sopenharmony_ci	err = devlink_dpipe_entry_ctx_prepare(dump_ctx);
5558c2ecf20Sopenharmony_ci	if (err)
5568c2ecf20Sopenharmony_ci		goto err_ctx_prepare;
5578c2ecf20Sopenharmony_ci	j = 0;
5588c2ecf20Sopenharmony_ci	rif_neigh_skip = rif_neigh_count;
5598c2ecf20Sopenharmony_ci	for (; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++) {
5608c2ecf20Sopenharmony_ci		struct mlxsw_sp_rif *rif = mlxsw_sp_rif_by_index(mlxsw_sp, i);
5618c2ecf20Sopenharmony_ci		struct mlxsw_sp_neigh_entry *neigh_entry;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci		if (!rif)
5648c2ecf20Sopenharmony_ci			continue;
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci		rif_neigh_count = 0;
5678c2ecf20Sopenharmony_ci		mlxsw_sp_rif_neigh_for_each(neigh_entry, rif) {
5688c2ecf20Sopenharmony_ci			int neigh_type = mlxsw_sp_neigh_entry_type(neigh_entry);
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci			if (neigh_type != type)
5718c2ecf20Sopenharmony_ci				continue;
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci			if (neigh_type == AF_INET6 &&
5748c2ecf20Sopenharmony_ci			    mlxsw_sp_neigh_ipv6_ignore(neigh_entry))
5758c2ecf20Sopenharmony_ci				continue;
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci			if (rif_neigh_count < rif_neigh_skip)
5788c2ecf20Sopenharmony_ci				goto skip;
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci			mlxsw_sp_dpipe_table_host_entry_fill(mlxsw_sp, entry,
5818c2ecf20Sopenharmony_ci							     neigh_entry, rif,
5828c2ecf20Sopenharmony_ci							     type);
5838c2ecf20Sopenharmony_ci			entry->index = neigh_count;
5848c2ecf20Sopenharmony_ci			err = devlink_dpipe_entry_ctx_append(dump_ctx, entry);
5858c2ecf20Sopenharmony_ci			if (err) {
5868c2ecf20Sopenharmony_ci				if (err == -EMSGSIZE) {
5878c2ecf20Sopenharmony_ci					if (!j)
5888c2ecf20Sopenharmony_ci						goto err_entry_append;
5898c2ecf20Sopenharmony_ci					else
5908c2ecf20Sopenharmony_ci						goto out;
5918c2ecf20Sopenharmony_ci				}
5928c2ecf20Sopenharmony_ci				goto err_entry_append;
5938c2ecf20Sopenharmony_ci			}
5948c2ecf20Sopenharmony_ci			neigh_count++;
5958c2ecf20Sopenharmony_ci			j++;
5968c2ecf20Sopenharmony_ciskip:
5978c2ecf20Sopenharmony_ci			rif_neigh_count++;
5988c2ecf20Sopenharmony_ci		}
5998c2ecf20Sopenharmony_ci		rif_neigh_skip = 0;
6008c2ecf20Sopenharmony_ci	}
6018c2ecf20Sopenharmony_ciout:
6028c2ecf20Sopenharmony_ci	devlink_dpipe_entry_ctx_close(dump_ctx);
6038c2ecf20Sopenharmony_ci	if (i != rif_count)
6048c2ecf20Sopenharmony_ci		goto start_again;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	mutex_unlock(&mlxsw_sp->router->lock);
6078c2ecf20Sopenharmony_ci	return 0;
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_cierr_ctx_prepare:
6108c2ecf20Sopenharmony_cierr_entry_append:
6118c2ecf20Sopenharmony_ci	mutex_unlock(&mlxsw_sp->router->lock);
6128c2ecf20Sopenharmony_ci	return err;
6138c2ecf20Sopenharmony_ci}
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_cistatic int
6168c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_host_entries_dump(struct mlxsw_sp *mlxsw_sp,
6178c2ecf20Sopenharmony_ci				       bool counters_enabled,
6188c2ecf20Sopenharmony_ci				       struct devlink_dpipe_dump_ctx *dump_ctx,
6198c2ecf20Sopenharmony_ci				       int type)
6208c2ecf20Sopenharmony_ci{
6218c2ecf20Sopenharmony_ci	struct devlink_dpipe_value match_values[MLXSW_SP_DPIPE_TABLE_HOST_MATCH_COUNT];
6228c2ecf20Sopenharmony_ci	struct devlink_dpipe_match matches[MLXSW_SP_DPIPE_TABLE_HOST_MATCH_COUNT];
6238c2ecf20Sopenharmony_ci	struct devlink_dpipe_value action_value;
6248c2ecf20Sopenharmony_ci	struct devlink_dpipe_action action = {0};
6258c2ecf20Sopenharmony_ci	struct devlink_dpipe_entry entry = {0};
6268c2ecf20Sopenharmony_ci	int err;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	memset(matches, 0, MLXSW_SP_DPIPE_TABLE_HOST_MATCH_COUNT *
6298c2ecf20Sopenharmony_ci			   sizeof(matches[0]));
6308c2ecf20Sopenharmony_ci	memset(match_values, 0, MLXSW_SP_DPIPE_TABLE_HOST_MATCH_COUNT *
6318c2ecf20Sopenharmony_ci				sizeof(match_values[0]));
6328c2ecf20Sopenharmony_ci	memset(&action_value, 0, sizeof(action_value));
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	mlxsw_sp_dpipe_table_host_match_action_prepare(matches, &action, type);
6358c2ecf20Sopenharmony_ci	err = mlxsw_sp_dpipe_table_host_entry_prepare(&entry, match_values,
6368c2ecf20Sopenharmony_ci						      matches, &action_value,
6378c2ecf20Sopenharmony_ci						      &action, type);
6388c2ecf20Sopenharmony_ci	if (err)
6398c2ecf20Sopenharmony_ci		goto out;
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	err = mlxsw_sp_dpipe_table_host_entries_get(mlxsw_sp, &entry,
6428c2ecf20Sopenharmony_ci						    counters_enabled, dump_ctx,
6438c2ecf20Sopenharmony_ci						    type);
6448c2ecf20Sopenharmony_ciout:
6458c2ecf20Sopenharmony_ci	devlink_dpipe_entry_clear(&entry);
6468c2ecf20Sopenharmony_ci	return err;
6478c2ecf20Sopenharmony_ci}
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_cistatic int
6508c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_host4_entries_dump(void *priv, bool counters_enabled,
6518c2ecf20Sopenharmony_ci					struct devlink_dpipe_dump_ctx *dump_ctx)
6528c2ecf20Sopenharmony_ci{
6538c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = priv;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	return mlxsw_sp_dpipe_table_host_entries_dump(mlxsw_sp,
6568c2ecf20Sopenharmony_ci						      counters_enabled,
6578c2ecf20Sopenharmony_ci						      dump_ctx, AF_INET);
6588c2ecf20Sopenharmony_ci}
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_cistatic void
6618c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_host_counters_update(struct mlxsw_sp *mlxsw_sp,
6628c2ecf20Sopenharmony_ci					  bool enable, int type)
6638c2ecf20Sopenharmony_ci{
6648c2ecf20Sopenharmony_ci	int i;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	mutex_lock(&mlxsw_sp->router->lock);
6678c2ecf20Sopenharmony_ci	for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++) {
6688c2ecf20Sopenharmony_ci		struct mlxsw_sp_rif *rif = mlxsw_sp_rif_by_index(mlxsw_sp, i);
6698c2ecf20Sopenharmony_ci		struct mlxsw_sp_neigh_entry *neigh_entry;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci		if (!rif)
6728c2ecf20Sopenharmony_ci			continue;
6738c2ecf20Sopenharmony_ci		mlxsw_sp_rif_neigh_for_each(neigh_entry, rif) {
6748c2ecf20Sopenharmony_ci			int neigh_type = mlxsw_sp_neigh_entry_type(neigh_entry);
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci			if (neigh_type != type)
6778c2ecf20Sopenharmony_ci				continue;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci			if (neigh_type == AF_INET6 &&
6808c2ecf20Sopenharmony_ci			    mlxsw_sp_neigh_ipv6_ignore(neigh_entry))
6818c2ecf20Sopenharmony_ci				continue;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci			mlxsw_sp_neigh_entry_counter_update(mlxsw_sp,
6848c2ecf20Sopenharmony_ci							    neigh_entry,
6858c2ecf20Sopenharmony_ci							    enable);
6868c2ecf20Sopenharmony_ci		}
6878c2ecf20Sopenharmony_ci	}
6888c2ecf20Sopenharmony_ci	mutex_unlock(&mlxsw_sp->router->lock);
6898c2ecf20Sopenharmony_ci}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_cistatic int mlxsw_sp_dpipe_table_host4_counters_update(void *priv, bool enable)
6928c2ecf20Sopenharmony_ci{
6938c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = priv;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	mlxsw_sp_dpipe_table_host_counters_update(mlxsw_sp, enable, AF_INET);
6968c2ecf20Sopenharmony_ci	return 0;
6978c2ecf20Sopenharmony_ci}
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_cistatic u64
7008c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_host_size_get(struct mlxsw_sp *mlxsw_sp, int type)
7018c2ecf20Sopenharmony_ci{
7028c2ecf20Sopenharmony_ci	u64 size = 0;
7038c2ecf20Sopenharmony_ci	int i;
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	mutex_lock(&mlxsw_sp->router->lock);
7068c2ecf20Sopenharmony_ci	for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++) {
7078c2ecf20Sopenharmony_ci		struct mlxsw_sp_rif *rif = mlxsw_sp_rif_by_index(mlxsw_sp, i);
7088c2ecf20Sopenharmony_ci		struct mlxsw_sp_neigh_entry *neigh_entry;
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci		if (!rif)
7118c2ecf20Sopenharmony_ci			continue;
7128c2ecf20Sopenharmony_ci		mlxsw_sp_rif_neigh_for_each(neigh_entry, rif) {
7138c2ecf20Sopenharmony_ci			int neigh_type = mlxsw_sp_neigh_entry_type(neigh_entry);
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci			if (neigh_type != type)
7168c2ecf20Sopenharmony_ci				continue;
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci			if (neigh_type == AF_INET6 &&
7198c2ecf20Sopenharmony_ci			    mlxsw_sp_neigh_ipv6_ignore(neigh_entry))
7208c2ecf20Sopenharmony_ci				continue;
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci			size++;
7238c2ecf20Sopenharmony_ci		}
7248c2ecf20Sopenharmony_ci	}
7258c2ecf20Sopenharmony_ci	mutex_unlock(&mlxsw_sp->router->lock);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	return size;
7288c2ecf20Sopenharmony_ci}
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_cistatic u64 mlxsw_sp_dpipe_table_host4_size_get(void *priv)
7318c2ecf20Sopenharmony_ci{
7328c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = priv;
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	return mlxsw_sp_dpipe_table_host_size_get(mlxsw_sp, AF_INET);
7358c2ecf20Sopenharmony_ci}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_cistatic struct devlink_dpipe_table_ops mlxsw_sp_host4_ops = {
7388c2ecf20Sopenharmony_ci	.matches_dump = mlxsw_sp_dpipe_table_host4_matches_dump,
7398c2ecf20Sopenharmony_ci	.actions_dump = mlxsw_sp_dpipe_table_host_actions_dump,
7408c2ecf20Sopenharmony_ci	.entries_dump = mlxsw_sp_dpipe_table_host4_entries_dump,
7418c2ecf20Sopenharmony_ci	.counters_set_update = mlxsw_sp_dpipe_table_host4_counters_update,
7428c2ecf20Sopenharmony_ci	.size_get = mlxsw_sp_dpipe_table_host4_size_get,
7438c2ecf20Sopenharmony_ci};
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci#define MLXSW_SP_DPIPE_TABLE_RESOURCE_UNIT_HOST4 1
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_cistatic int mlxsw_sp_dpipe_host4_table_init(struct mlxsw_sp *mlxsw_sp)
7488c2ecf20Sopenharmony_ci{
7498c2ecf20Sopenharmony_ci	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
7508c2ecf20Sopenharmony_ci	int err;
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	err = devlink_dpipe_table_register(devlink,
7538c2ecf20Sopenharmony_ci					   MLXSW_SP_DPIPE_TABLE_NAME_HOST4,
7548c2ecf20Sopenharmony_ci					   &mlxsw_sp_host4_ops,
7558c2ecf20Sopenharmony_ci					   mlxsw_sp, false);
7568c2ecf20Sopenharmony_ci	if (err)
7578c2ecf20Sopenharmony_ci		return err;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	err = devlink_dpipe_table_resource_set(devlink,
7608c2ecf20Sopenharmony_ci					       MLXSW_SP_DPIPE_TABLE_NAME_HOST4,
7618c2ecf20Sopenharmony_ci					       MLXSW_SP_RESOURCE_KVD_HASH_SINGLE,
7628c2ecf20Sopenharmony_ci					       MLXSW_SP_DPIPE_TABLE_RESOURCE_UNIT_HOST4);
7638c2ecf20Sopenharmony_ci	if (err)
7648c2ecf20Sopenharmony_ci		goto err_resource_set;
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	return 0;
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_cierr_resource_set:
7698c2ecf20Sopenharmony_ci	devlink_dpipe_table_unregister(devlink,
7708c2ecf20Sopenharmony_ci				       MLXSW_SP_DPIPE_TABLE_NAME_HOST4);
7718c2ecf20Sopenharmony_ci	return err;
7728c2ecf20Sopenharmony_ci}
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_cistatic void mlxsw_sp_dpipe_host4_table_fini(struct mlxsw_sp *mlxsw_sp)
7758c2ecf20Sopenharmony_ci{
7768c2ecf20Sopenharmony_ci	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci	devlink_dpipe_table_unregister(devlink,
7798c2ecf20Sopenharmony_ci				       MLXSW_SP_DPIPE_TABLE_NAME_HOST4);
7808c2ecf20Sopenharmony_ci}
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_cistatic int
7838c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_host6_matches_dump(void *priv, struct sk_buff *skb)
7848c2ecf20Sopenharmony_ci{
7858c2ecf20Sopenharmony_ci	return mlxsw_sp_dpipe_table_host_matches_dump(skb, AF_INET6);
7868c2ecf20Sopenharmony_ci}
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_cistatic int
7898c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_host6_entries_dump(void *priv, bool counters_enabled,
7908c2ecf20Sopenharmony_ci					struct devlink_dpipe_dump_ctx *dump_ctx)
7918c2ecf20Sopenharmony_ci{
7928c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = priv;
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	return mlxsw_sp_dpipe_table_host_entries_dump(mlxsw_sp,
7958c2ecf20Sopenharmony_ci						      counters_enabled,
7968c2ecf20Sopenharmony_ci						      dump_ctx, AF_INET6);
7978c2ecf20Sopenharmony_ci}
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_cistatic int mlxsw_sp_dpipe_table_host6_counters_update(void *priv, bool enable)
8008c2ecf20Sopenharmony_ci{
8018c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = priv;
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	mlxsw_sp_dpipe_table_host_counters_update(mlxsw_sp, enable, AF_INET6);
8048c2ecf20Sopenharmony_ci	return 0;
8058c2ecf20Sopenharmony_ci}
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_cistatic u64 mlxsw_sp_dpipe_table_host6_size_get(void *priv)
8088c2ecf20Sopenharmony_ci{
8098c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = priv;
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	return mlxsw_sp_dpipe_table_host_size_get(mlxsw_sp, AF_INET6);
8128c2ecf20Sopenharmony_ci}
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_cistatic struct devlink_dpipe_table_ops mlxsw_sp_host6_ops = {
8158c2ecf20Sopenharmony_ci	.matches_dump = mlxsw_sp_dpipe_table_host6_matches_dump,
8168c2ecf20Sopenharmony_ci	.actions_dump = mlxsw_sp_dpipe_table_host_actions_dump,
8178c2ecf20Sopenharmony_ci	.entries_dump = mlxsw_sp_dpipe_table_host6_entries_dump,
8188c2ecf20Sopenharmony_ci	.counters_set_update = mlxsw_sp_dpipe_table_host6_counters_update,
8198c2ecf20Sopenharmony_ci	.size_get = mlxsw_sp_dpipe_table_host6_size_get,
8208c2ecf20Sopenharmony_ci};
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci#define MLXSW_SP_DPIPE_TABLE_RESOURCE_UNIT_HOST6 2
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_cistatic int mlxsw_sp_dpipe_host6_table_init(struct mlxsw_sp *mlxsw_sp)
8258c2ecf20Sopenharmony_ci{
8268c2ecf20Sopenharmony_ci	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
8278c2ecf20Sopenharmony_ci	int err;
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	err = devlink_dpipe_table_register(devlink,
8308c2ecf20Sopenharmony_ci					   MLXSW_SP_DPIPE_TABLE_NAME_HOST6,
8318c2ecf20Sopenharmony_ci					   &mlxsw_sp_host6_ops,
8328c2ecf20Sopenharmony_ci					   mlxsw_sp, false);
8338c2ecf20Sopenharmony_ci	if (err)
8348c2ecf20Sopenharmony_ci		return err;
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	err = devlink_dpipe_table_resource_set(devlink,
8378c2ecf20Sopenharmony_ci					       MLXSW_SP_DPIPE_TABLE_NAME_HOST6,
8388c2ecf20Sopenharmony_ci					       MLXSW_SP_RESOURCE_KVD_HASH_DOUBLE,
8398c2ecf20Sopenharmony_ci					       MLXSW_SP_DPIPE_TABLE_RESOURCE_UNIT_HOST6);
8408c2ecf20Sopenharmony_ci	if (err)
8418c2ecf20Sopenharmony_ci		goto err_resource_set;
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	return 0;
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_cierr_resource_set:
8468c2ecf20Sopenharmony_ci	devlink_dpipe_table_unregister(devlink,
8478c2ecf20Sopenharmony_ci				       MLXSW_SP_DPIPE_TABLE_NAME_HOST6);
8488c2ecf20Sopenharmony_ci	return err;
8498c2ecf20Sopenharmony_ci}
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_cistatic void mlxsw_sp_dpipe_host6_table_fini(struct mlxsw_sp *mlxsw_sp)
8528c2ecf20Sopenharmony_ci{
8538c2ecf20Sopenharmony_ci	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci	devlink_dpipe_table_unregister(devlink,
8568c2ecf20Sopenharmony_ci				       MLXSW_SP_DPIPE_TABLE_NAME_HOST6);
8578c2ecf20Sopenharmony_ci}
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_cistatic int mlxsw_sp_dpipe_table_adj_matches_dump(void *priv,
8608c2ecf20Sopenharmony_ci						 struct sk_buff *skb)
8618c2ecf20Sopenharmony_ci{
8628c2ecf20Sopenharmony_ci	struct devlink_dpipe_match match = {0};
8638c2ecf20Sopenharmony_ci	int err;
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	match.type = DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT;
8668c2ecf20Sopenharmony_ci	match.header = &mlxsw_sp_dpipe_header_metadata;
8678c2ecf20Sopenharmony_ci	match.field_id = MLXSW_SP_DPIPE_FIELD_METADATA_ADJ_INDEX;
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	err = devlink_dpipe_match_put(skb, &match);
8708c2ecf20Sopenharmony_ci	if (err)
8718c2ecf20Sopenharmony_ci		return err;
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	match.type = DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT;
8748c2ecf20Sopenharmony_ci	match.header = &mlxsw_sp_dpipe_header_metadata;
8758c2ecf20Sopenharmony_ci	match.field_id = MLXSW_SP_DPIPE_FIELD_METADATA_ADJ_SIZE;
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci	err = devlink_dpipe_match_put(skb, &match);
8788c2ecf20Sopenharmony_ci	if (err)
8798c2ecf20Sopenharmony_ci		return err;
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	match.type = DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT;
8828c2ecf20Sopenharmony_ci	match.header = &mlxsw_sp_dpipe_header_metadata;
8838c2ecf20Sopenharmony_ci	match.field_id = MLXSW_SP_DPIPE_FIELD_METADATA_ADJ_HASH_INDEX;
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	return devlink_dpipe_match_put(skb, &match);
8868c2ecf20Sopenharmony_ci}
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_cistatic int mlxsw_sp_dpipe_table_adj_actions_dump(void *priv,
8898c2ecf20Sopenharmony_ci						 struct sk_buff *skb)
8908c2ecf20Sopenharmony_ci{
8918c2ecf20Sopenharmony_ci	struct devlink_dpipe_action action = {0};
8928c2ecf20Sopenharmony_ci	int err;
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	action.type = DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY;
8958c2ecf20Sopenharmony_ci	action.header = &devlink_dpipe_header_ethernet;
8968c2ecf20Sopenharmony_ci	action.field_id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC;
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	err = devlink_dpipe_action_put(skb, &action);
8998c2ecf20Sopenharmony_ci	if (err)
9008c2ecf20Sopenharmony_ci		return err;
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci	action.type = DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY;
9038c2ecf20Sopenharmony_ci	action.header = &mlxsw_sp_dpipe_header_metadata;
9048c2ecf20Sopenharmony_ci	action.field_id = MLXSW_SP_DPIPE_FIELD_METADATA_ERIF_PORT;
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	return devlink_dpipe_action_put(skb, &action);
9078c2ecf20Sopenharmony_ci}
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_cistatic u64 mlxsw_sp_dpipe_table_adj_size(struct mlxsw_sp *mlxsw_sp)
9108c2ecf20Sopenharmony_ci{
9118c2ecf20Sopenharmony_ci	struct mlxsw_sp_nexthop *nh;
9128c2ecf20Sopenharmony_ci	u64 size = 0;
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	mlxsw_sp_nexthop_for_each(nh, mlxsw_sp->router)
9158c2ecf20Sopenharmony_ci		if (mlxsw_sp_nexthop_offload(nh) &&
9168c2ecf20Sopenharmony_ci		    !mlxsw_sp_nexthop_group_has_ipip(nh))
9178c2ecf20Sopenharmony_ci			size++;
9188c2ecf20Sopenharmony_ci	return size;
9198c2ecf20Sopenharmony_ci}
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_cienum mlxsw_sp_dpipe_table_adj_match {
9228c2ecf20Sopenharmony_ci	MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_INDEX,
9238c2ecf20Sopenharmony_ci	MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_SIZE,
9248c2ecf20Sopenharmony_ci	MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_HASH_INDEX,
9258c2ecf20Sopenharmony_ci	MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_COUNT,
9268c2ecf20Sopenharmony_ci};
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_cienum mlxsw_sp_dpipe_table_adj_action {
9298c2ecf20Sopenharmony_ci	MLXSW_SP_DPIPE_TABLE_ADJ_ACTION_DST_MAC,
9308c2ecf20Sopenharmony_ci	MLXSW_SP_DPIPE_TABLE_ADJ_ACTION_ERIF_PORT,
9318c2ecf20Sopenharmony_ci	MLXSW_SP_DPIPE_TABLE_ADJ_ACTION_COUNT,
9328c2ecf20Sopenharmony_ci};
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_cistatic void
9358c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_adj_match_action_prepare(struct devlink_dpipe_match *matches,
9368c2ecf20Sopenharmony_ci					      struct devlink_dpipe_action *actions)
9378c2ecf20Sopenharmony_ci{
9388c2ecf20Sopenharmony_ci	struct devlink_dpipe_action *action;
9398c2ecf20Sopenharmony_ci	struct devlink_dpipe_match *match;
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	match = &matches[MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_INDEX];
9428c2ecf20Sopenharmony_ci	match->type = DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT;
9438c2ecf20Sopenharmony_ci	match->header = &mlxsw_sp_dpipe_header_metadata;
9448c2ecf20Sopenharmony_ci	match->field_id = MLXSW_SP_DPIPE_FIELD_METADATA_ADJ_INDEX;
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	match = &matches[MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_SIZE];
9478c2ecf20Sopenharmony_ci	match->type = DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT;
9488c2ecf20Sopenharmony_ci	match->header = &mlxsw_sp_dpipe_header_metadata;
9498c2ecf20Sopenharmony_ci	match->field_id = MLXSW_SP_DPIPE_FIELD_METADATA_ADJ_SIZE;
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	match = &matches[MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_HASH_INDEX];
9528c2ecf20Sopenharmony_ci	match->type = DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT;
9538c2ecf20Sopenharmony_ci	match->header = &mlxsw_sp_dpipe_header_metadata;
9548c2ecf20Sopenharmony_ci	match->field_id = MLXSW_SP_DPIPE_FIELD_METADATA_ADJ_HASH_INDEX;
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci	action = &actions[MLXSW_SP_DPIPE_TABLE_ADJ_ACTION_DST_MAC];
9578c2ecf20Sopenharmony_ci	action->type = DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY;
9588c2ecf20Sopenharmony_ci	action->header = &devlink_dpipe_header_ethernet;
9598c2ecf20Sopenharmony_ci	action->field_id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC;
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci	action = &actions[MLXSW_SP_DPIPE_TABLE_ADJ_ACTION_ERIF_PORT];
9628c2ecf20Sopenharmony_ci	action->type = DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY;
9638c2ecf20Sopenharmony_ci	action->header = &mlxsw_sp_dpipe_header_metadata;
9648c2ecf20Sopenharmony_ci	action->field_id = MLXSW_SP_DPIPE_FIELD_METADATA_ERIF_PORT;
9658c2ecf20Sopenharmony_ci}
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_cistatic int
9688c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_adj_entry_prepare(struct devlink_dpipe_entry *entry,
9698c2ecf20Sopenharmony_ci				       struct devlink_dpipe_value *match_values,
9708c2ecf20Sopenharmony_ci				       struct devlink_dpipe_match *matches,
9718c2ecf20Sopenharmony_ci				       struct devlink_dpipe_value *action_values,
9728c2ecf20Sopenharmony_ci				       struct devlink_dpipe_action *actions)
9738c2ecf20Sopenharmony_ci{	struct devlink_dpipe_value *action_value;
9748c2ecf20Sopenharmony_ci	struct devlink_dpipe_value *match_value;
9758c2ecf20Sopenharmony_ci	struct devlink_dpipe_action *action;
9768c2ecf20Sopenharmony_ci	struct devlink_dpipe_match *match;
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci	entry->match_values = match_values;
9798c2ecf20Sopenharmony_ci	entry->match_values_count = MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_COUNT;
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci	entry->action_values = action_values;
9828c2ecf20Sopenharmony_ci	entry->action_values_count = MLXSW_SP_DPIPE_TABLE_ADJ_ACTION_COUNT;
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci	match = &matches[MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_INDEX];
9858c2ecf20Sopenharmony_ci	match_value = &match_values[MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_INDEX];
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	match_value->match = match;
9888c2ecf20Sopenharmony_ci	match_value->value_size = sizeof(u32);
9898c2ecf20Sopenharmony_ci	match_value->value = kmalloc(match_value->value_size, GFP_KERNEL);
9908c2ecf20Sopenharmony_ci	if (!match_value->value)
9918c2ecf20Sopenharmony_ci		return -ENOMEM;
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci	match = &matches[MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_SIZE];
9948c2ecf20Sopenharmony_ci	match_value = &match_values[MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_SIZE];
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci	match_value->match = match;
9978c2ecf20Sopenharmony_ci	match_value->value_size = sizeof(u32);
9988c2ecf20Sopenharmony_ci	match_value->value = kmalloc(match_value->value_size, GFP_KERNEL);
9998c2ecf20Sopenharmony_ci	if (!match_value->value)
10008c2ecf20Sopenharmony_ci		return -ENOMEM;
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_ci	match = &matches[MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_HASH_INDEX];
10038c2ecf20Sopenharmony_ci	match_value = &match_values[MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_HASH_INDEX];
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci	match_value->match = match;
10068c2ecf20Sopenharmony_ci	match_value->value_size = sizeof(u32);
10078c2ecf20Sopenharmony_ci	match_value->value = kmalloc(match_value->value_size, GFP_KERNEL);
10088c2ecf20Sopenharmony_ci	if (!match_value->value)
10098c2ecf20Sopenharmony_ci		return -ENOMEM;
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_ci	action = &actions[MLXSW_SP_DPIPE_TABLE_ADJ_ACTION_DST_MAC];
10128c2ecf20Sopenharmony_ci	action_value = &action_values[MLXSW_SP_DPIPE_TABLE_ADJ_ACTION_DST_MAC];
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci	action_value->action = action;
10158c2ecf20Sopenharmony_ci	action_value->value_size = sizeof(u64);
10168c2ecf20Sopenharmony_ci	action_value->value = kmalloc(action_value->value_size, GFP_KERNEL);
10178c2ecf20Sopenharmony_ci	if (!action_value->value)
10188c2ecf20Sopenharmony_ci		return -ENOMEM;
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_ci	action = &actions[MLXSW_SP_DPIPE_TABLE_ADJ_ACTION_ERIF_PORT];
10218c2ecf20Sopenharmony_ci	action_value = &action_values[MLXSW_SP_DPIPE_TABLE_ADJ_ACTION_ERIF_PORT];
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	action_value->action = action;
10248c2ecf20Sopenharmony_ci	action_value->value_size = sizeof(u32);
10258c2ecf20Sopenharmony_ci	action_value->value = kmalloc(action_value->value_size, GFP_KERNEL);
10268c2ecf20Sopenharmony_ci	if (!action_value->value)
10278c2ecf20Sopenharmony_ci		return -ENOMEM;
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	return 0;
10308c2ecf20Sopenharmony_ci}
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_cistatic void
10338c2ecf20Sopenharmony_ci__mlxsw_sp_dpipe_table_adj_entry_fill(struct devlink_dpipe_entry *entry,
10348c2ecf20Sopenharmony_ci				      u32 adj_index, u32 adj_size,
10358c2ecf20Sopenharmony_ci				      u32 adj_hash_index, unsigned char *ha,
10368c2ecf20Sopenharmony_ci				      struct mlxsw_sp_rif *rif)
10378c2ecf20Sopenharmony_ci{
10388c2ecf20Sopenharmony_ci	struct devlink_dpipe_value *value;
10398c2ecf20Sopenharmony_ci	u32 *p_rif_value;
10408c2ecf20Sopenharmony_ci	u32 *p_index;
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci	value = &entry->match_values[MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_INDEX];
10438c2ecf20Sopenharmony_ci	p_index = value->value;
10448c2ecf20Sopenharmony_ci	*p_index = adj_index;
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_ci	value = &entry->match_values[MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_SIZE];
10478c2ecf20Sopenharmony_ci	p_index = value->value;
10488c2ecf20Sopenharmony_ci	*p_index = adj_size;
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci	value = &entry->match_values[MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_HASH_INDEX];
10518c2ecf20Sopenharmony_ci	p_index = value->value;
10528c2ecf20Sopenharmony_ci	*p_index = adj_hash_index;
10538c2ecf20Sopenharmony_ci
10548c2ecf20Sopenharmony_ci	value = &entry->action_values[MLXSW_SP_DPIPE_TABLE_ADJ_ACTION_DST_MAC];
10558c2ecf20Sopenharmony_ci	ether_addr_copy(value->value, ha);
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	value = &entry->action_values[MLXSW_SP_DPIPE_TABLE_ADJ_ACTION_ERIF_PORT];
10588c2ecf20Sopenharmony_ci	p_rif_value = value->value;
10598c2ecf20Sopenharmony_ci	*p_rif_value = mlxsw_sp_rif_index(rif);
10608c2ecf20Sopenharmony_ci	value->mapping_value = mlxsw_sp_rif_dev_ifindex(rif);
10618c2ecf20Sopenharmony_ci	value->mapping_valid = true;
10628c2ecf20Sopenharmony_ci}
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_cistatic void mlxsw_sp_dpipe_table_adj_entry_fill(struct mlxsw_sp *mlxsw_sp,
10658c2ecf20Sopenharmony_ci						struct mlxsw_sp_nexthop *nh,
10668c2ecf20Sopenharmony_ci						struct devlink_dpipe_entry *entry)
10678c2ecf20Sopenharmony_ci{
10688c2ecf20Sopenharmony_ci	struct mlxsw_sp_rif *rif = mlxsw_sp_nexthop_rif(nh);
10698c2ecf20Sopenharmony_ci	unsigned char *ha = mlxsw_sp_nexthop_ha(nh);
10708c2ecf20Sopenharmony_ci	u32 adj_hash_index = 0;
10718c2ecf20Sopenharmony_ci	u32 adj_index = 0;
10728c2ecf20Sopenharmony_ci	u32 adj_size = 0;
10738c2ecf20Sopenharmony_ci	int err;
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci	mlxsw_sp_nexthop_indexes(nh, &adj_index, &adj_size, &adj_hash_index);
10768c2ecf20Sopenharmony_ci	__mlxsw_sp_dpipe_table_adj_entry_fill(entry, adj_index, adj_size,
10778c2ecf20Sopenharmony_ci					      adj_hash_index, ha, rif);
10788c2ecf20Sopenharmony_ci	err = mlxsw_sp_nexthop_counter_get(mlxsw_sp, nh, &entry->counter);
10798c2ecf20Sopenharmony_ci	if (!err)
10808c2ecf20Sopenharmony_ci		entry->counter_valid = true;
10818c2ecf20Sopenharmony_ci}
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_cistatic int
10848c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_adj_entries_get(struct mlxsw_sp *mlxsw_sp,
10858c2ecf20Sopenharmony_ci				     struct devlink_dpipe_entry *entry,
10868c2ecf20Sopenharmony_ci				     bool counters_enabled,
10878c2ecf20Sopenharmony_ci				     struct devlink_dpipe_dump_ctx *dump_ctx)
10888c2ecf20Sopenharmony_ci{
10898c2ecf20Sopenharmony_ci	struct mlxsw_sp_nexthop *nh;
10908c2ecf20Sopenharmony_ci	int entry_index = 0;
10918c2ecf20Sopenharmony_ci	int nh_count_max;
10928c2ecf20Sopenharmony_ci	int nh_count = 0;
10938c2ecf20Sopenharmony_ci	int nh_skip;
10948c2ecf20Sopenharmony_ci	int j;
10958c2ecf20Sopenharmony_ci	int err;
10968c2ecf20Sopenharmony_ci
10978c2ecf20Sopenharmony_ci	mutex_lock(&mlxsw_sp->router->lock);
10988c2ecf20Sopenharmony_ci	nh_count_max = mlxsw_sp_dpipe_table_adj_size(mlxsw_sp);
10998c2ecf20Sopenharmony_cistart_again:
11008c2ecf20Sopenharmony_ci	err = devlink_dpipe_entry_ctx_prepare(dump_ctx);
11018c2ecf20Sopenharmony_ci	if (err)
11028c2ecf20Sopenharmony_ci		goto err_ctx_prepare;
11038c2ecf20Sopenharmony_ci	j = 0;
11048c2ecf20Sopenharmony_ci	nh_skip = nh_count;
11058c2ecf20Sopenharmony_ci	nh_count = 0;
11068c2ecf20Sopenharmony_ci	mlxsw_sp_nexthop_for_each(nh, mlxsw_sp->router) {
11078c2ecf20Sopenharmony_ci		if (!mlxsw_sp_nexthop_offload(nh) ||
11088c2ecf20Sopenharmony_ci		    mlxsw_sp_nexthop_group_has_ipip(nh))
11098c2ecf20Sopenharmony_ci			continue;
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci		if (nh_count < nh_skip)
11128c2ecf20Sopenharmony_ci			goto skip;
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci		mlxsw_sp_dpipe_table_adj_entry_fill(mlxsw_sp, nh, entry);
11158c2ecf20Sopenharmony_ci		entry->index = entry_index;
11168c2ecf20Sopenharmony_ci		err = devlink_dpipe_entry_ctx_append(dump_ctx, entry);
11178c2ecf20Sopenharmony_ci		if (err) {
11188c2ecf20Sopenharmony_ci			if (err == -EMSGSIZE) {
11198c2ecf20Sopenharmony_ci				if (!j)
11208c2ecf20Sopenharmony_ci					goto err_entry_append;
11218c2ecf20Sopenharmony_ci				break;
11228c2ecf20Sopenharmony_ci			}
11238c2ecf20Sopenharmony_ci			goto err_entry_append;
11248c2ecf20Sopenharmony_ci		}
11258c2ecf20Sopenharmony_ci		entry_index++;
11268c2ecf20Sopenharmony_ci		j++;
11278c2ecf20Sopenharmony_ciskip:
11288c2ecf20Sopenharmony_ci		nh_count++;
11298c2ecf20Sopenharmony_ci	}
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_ci	devlink_dpipe_entry_ctx_close(dump_ctx);
11328c2ecf20Sopenharmony_ci	if (nh_count != nh_count_max)
11338c2ecf20Sopenharmony_ci		goto start_again;
11348c2ecf20Sopenharmony_ci	mutex_unlock(&mlxsw_sp->router->lock);
11358c2ecf20Sopenharmony_ci
11368c2ecf20Sopenharmony_ci	return 0;
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_cierr_ctx_prepare:
11398c2ecf20Sopenharmony_cierr_entry_append:
11408c2ecf20Sopenharmony_ci	mutex_unlock(&mlxsw_sp->router->lock);
11418c2ecf20Sopenharmony_ci	return err;
11428c2ecf20Sopenharmony_ci}
11438c2ecf20Sopenharmony_ci
11448c2ecf20Sopenharmony_cistatic int
11458c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_adj_entries_dump(void *priv, bool counters_enabled,
11468c2ecf20Sopenharmony_ci				      struct devlink_dpipe_dump_ctx *dump_ctx)
11478c2ecf20Sopenharmony_ci{
11488c2ecf20Sopenharmony_ci	struct devlink_dpipe_value action_values[MLXSW_SP_DPIPE_TABLE_ADJ_ACTION_COUNT];
11498c2ecf20Sopenharmony_ci	struct devlink_dpipe_value match_values[MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_COUNT];
11508c2ecf20Sopenharmony_ci	struct devlink_dpipe_action actions[MLXSW_SP_DPIPE_TABLE_ADJ_ACTION_COUNT];
11518c2ecf20Sopenharmony_ci	struct devlink_dpipe_match matches[MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_COUNT];
11528c2ecf20Sopenharmony_ci	struct devlink_dpipe_entry entry = {0};
11538c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = priv;
11548c2ecf20Sopenharmony_ci	int err;
11558c2ecf20Sopenharmony_ci
11568c2ecf20Sopenharmony_ci	memset(matches, 0, MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_COUNT *
11578c2ecf20Sopenharmony_ci			   sizeof(matches[0]));
11588c2ecf20Sopenharmony_ci	memset(match_values, 0, MLXSW_SP_DPIPE_TABLE_ADJ_MATCH_COUNT *
11598c2ecf20Sopenharmony_ci				sizeof(match_values[0]));
11608c2ecf20Sopenharmony_ci	memset(actions, 0, MLXSW_SP_DPIPE_TABLE_ADJ_ACTION_COUNT *
11618c2ecf20Sopenharmony_ci			   sizeof(actions[0]));
11628c2ecf20Sopenharmony_ci	memset(action_values, 0, MLXSW_SP_DPIPE_TABLE_ADJ_ACTION_COUNT *
11638c2ecf20Sopenharmony_ci				 sizeof(action_values[0]));
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_ci	mlxsw_sp_dpipe_table_adj_match_action_prepare(matches, actions);
11668c2ecf20Sopenharmony_ci	err = mlxsw_sp_dpipe_table_adj_entry_prepare(&entry,
11678c2ecf20Sopenharmony_ci						     match_values, matches,
11688c2ecf20Sopenharmony_ci						     action_values, actions);
11698c2ecf20Sopenharmony_ci	if (err)
11708c2ecf20Sopenharmony_ci		goto out;
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci	err = mlxsw_sp_dpipe_table_adj_entries_get(mlxsw_sp, &entry,
11738c2ecf20Sopenharmony_ci						   counters_enabled, dump_ctx);
11748c2ecf20Sopenharmony_ciout:
11758c2ecf20Sopenharmony_ci	devlink_dpipe_entry_clear(&entry);
11768c2ecf20Sopenharmony_ci	return err;
11778c2ecf20Sopenharmony_ci}
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_cistatic int mlxsw_sp_dpipe_table_adj_counters_update(void *priv, bool enable)
11808c2ecf20Sopenharmony_ci{
11818c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = priv;
11828c2ecf20Sopenharmony_ci	struct mlxsw_sp_nexthop *nh;
11838c2ecf20Sopenharmony_ci	u32 adj_hash_index = 0;
11848c2ecf20Sopenharmony_ci	u32 adj_index = 0;
11858c2ecf20Sopenharmony_ci	u32 adj_size = 0;
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_ci	mlxsw_sp_nexthop_for_each(nh, mlxsw_sp->router) {
11888c2ecf20Sopenharmony_ci		if (!mlxsw_sp_nexthop_offload(nh) ||
11898c2ecf20Sopenharmony_ci		    mlxsw_sp_nexthop_group_has_ipip(nh))
11908c2ecf20Sopenharmony_ci			continue;
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci		mlxsw_sp_nexthop_indexes(nh, &adj_index, &adj_size,
11938c2ecf20Sopenharmony_ci					 &adj_hash_index);
11948c2ecf20Sopenharmony_ci		if (enable)
11958c2ecf20Sopenharmony_ci			mlxsw_sp_nexthop_counter_alloc(mlxsw_sp, nh);
11968c2ecf20Sopenharmony_ci		else
11978c2ecf20Sopenharmony_ci			mlxsw_sp_nexthop_counter_free(mlxsw_sp, nh);
11988c2ecf20Sopenharmony_ci		mlxsw_sp_nexthop_update(mlxsw_sp,
11998c2ecf20Sopenharmony_ci					adj_index + adj_hash_index, nh);
12008c2ecf20Sopenharmony_ci	}
12018c2ecf20Sopenharmony_ci	return 0;
12028c2ecf20Sopenharmony_ci}
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_cistatic u64
12058c2ecf20Sopenharmony_cimlxsw_sp_dpipe_table_adj_size_get(void *priv)
12068c2ecf20Sopenharmony_ci{
12078c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = priv;
12088c2ecf20Sopenharmony_ci	u64 size;
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_ci	mutex_lock(&mlxsw_sp->router->lock);
12118c2ecf20Sopenharmony_ci	size = mlxsw_sp_dpipe_table_adj_size(mlxsw_sp);
12128c2ecf20Sopenharmony_ci	mutex_unlock(&mlxsw_sp->router->lock);
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci	return size;
12158c2ecf20Sopenharmony_ci}
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_cistatic struct devlink_dpipe_table_ops mlxsw_sp_dpipe_table_adj_ops = {
12188c2ecf20Sopenharmony_ci	.matches_dump = mlxsw_sp_dpipe_table_adj_matches_dump,
12198c2ecf20Sopenharmony_ci	.actions_dump = mlxsw_sp_dpipe_table_adj_actions_dump,
12208c2ecf20Sopenharmony_ci	.entries_dump = mlxsw_sp_dpipe_table_adj_entries_dump,
12218c2ecf20Sopenharmony_ci	.counters_set_update = mlxsw_sp_dpipe_table_adj_counters_update,
12228c2ecf20Sopenharmony_ci	.size_get = mlxsw_sp_dpipe_table_adj_size_get,
12238c2ecf20Sopenharmony_ci};
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_ci#define MLXSW_SP_DPIPE_TABLE_RESOURCE_UNIT_ADJ 1
12268c2ecf20Sopenharmony_ci
12278c2ecf20Sopenharmony_cistatic int mlxsw_sp_dpipe_adj_table_init(struct mlxsw_sp *mlxsw_sp)
12288c2ecf20Sopenharmony_ci{
12298c2ecf20Sopenharmony_ci	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
12308c2ecf20Sopenharmony_ci	int err;
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci	err = devlink_dpipe_table_register(devlink,
12338c2ecf20Sopenharmony_ci					   MLXSW_SP_DPIPE_TABLE_NAME_ADJ,
12348c2ecf20Sopenharmony_ci					   &mlxsw_sp_dpipe_table_adj_ops,
12358c2ecf20Sopenharmony_ci					   mlxsw_sp, false);
12368c2ecf20Sopenharmony_ci	if (err)
12378c2ecf20Sopenharmony_ci		return err;
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci	err = devlink_dpipe_table_resource_set(devlink,
12408c2ecf20Sopenharmony_ci					       MLXSW_SP_DPIPE_TABLE_NAME_ADJ,
12418c2ecf20Sopenharmony_ci					       MLXSW_SP_RESOURCE_KVD_LINEAR,
12428c2ecf20Sopenharmony_ci					       MLXSW_SP_DPIPE_TABLE_RESOURCE_UNIT_ADJ);
12438c2ecf20Sopenharmony_ci	if (err)
12448c2ecf20Sopenharmony_ci		goto err_resource_set;
12458c2ecf20Sopenharmony_ci
12468c2ecf20Sopenharmony_ci	return 0;
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_cierr_resource_set:
12498c2ecf20Sopenharmony_ci	devlink_dpipe_table_unregister(devlink,
12508c2ecf20Sopenharmony_ci				       MLXSW_SP_DPIPE_TABLE_NAME_ADJ);
12518c2ecf20Sopenharmony_ci	return err;
12528c2ecf20Sopenharmony_ci}
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_cistatic void mlxsw_sp_dpipe_adj_table_fini(struct mlxsw_sp *mlxsw_sp)
12558c2ecf20Sopenharmony_ci{
12568c2ecf20Sopenharmony_ci	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
12578c2ecf20Sopenharmony_ci
12588c2ecf20Sopenharmony_ci	devlink_dpipe_table_unregister(devlink,
12598c2ecf20Sopenharmony_ci				       MLXSW_SP_DPIPE_TABLE_NAME_ADJ);
12608c2ecf20Sopenharmony_ci}
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ciint mlxsw_sp_dpipe_init(struct mlxsw_sp *mlxsw_sp)
12638c2ecf20Sopenharmony_ci{
12648c2ecf20Sopenharmony_ci	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
12658c2ecf20Sopenharmony_ci	int err;
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_ci	err = devlink_dpipe_headers_register(devlink,
12688c2ecf20Sopenharmony_ci					     &mlxsw_sp_dpipe_headers);
12698c2ecf20Sopenharmony_ci	if (err)
12708c2ecf20Sopenharmony_ci		return err;
12718c2ecf20Sopenharmony_ci	err = mlxsw_sp_dpipe_erif_table_init(mlxsw_sp);
12728c2ecf20Sopenharmony_ci	if (err)
12738c2ecf20Sopenharmony_ci		goto err_erif_table_init;
12748c2ecf20Sopenharmony_ci
12758c2ecf20Sopenharmony_ci	err = mlxsw_sp_dpipe_host4_table_init(mlxsw_sp);
12768c2ecf20Sopenharmony_ci	if (err)
12778c2ecf20Sopenharmony_ci		goto err_host4_table_init;
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_ci	err = mlxsw_sp_dpipe_host6_table_init(mlxsw_sp);
12808c2ecf20Sopenharmony_ci	if (err)
12818c2ecf20Sopenharmony_ci		goto err_host6_table_init;
12828c2ecf20Sopenharmony_ci
12838c2ecf20Sopenharmony_ci	err = mlxsw_sp_dpipe_adj_table_init(mlxsw_sp);
12848c2ecf20Sopenharmony_ci	if (err)
12858c2ecf20Sopenharmony_ci		goto err_adj_table_init;
12868c2ecf20Sopenharmony_ci
12878c2ecf20Sopenharmony_ci	return 0;
12888c2ecf20Sopenharmony_cierr_adj_table_init:
12898c2ecf20Sopenharmony_ci	mlxsw_sp_dpipe_host6_table_fini(mlxsw_sp);
12908c2ecf20Sopenharmony_cierr_host6_table_init:
12918c2ecf20Sopenharmony_ci	mlxsw_sp_dpipe_host4_table_fini(mlxsw_sp);
12928c2ecf20Sopenharmony_cierr_host4_table_init:
12938c2ecf20Sopenharmony_ci	mlxsw_sp_dpipe_erif_table_fini(mlxsw_sp);
12948c2ecf20Sopenharmony_cierr_erif_table_init:
12958c2ecf20Sopenharmony_ci	devlink_dpipe_headers_unregister(priv_to_devlink(mlxsw_sp->core));
12968c2ecf20Sopenharmony_ci	return err;
12978c2ecf20Sopenharmony_ci}
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_civoid mlxsw_sp_dpipe_fini(struct mlxsw_sp *mlxsw_sp)
13008c2ecf20Sopenharmony_ci{
13018c2ecf20Sopenharmony_ci	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
13028c2ecf20Sopenharmony_ci
13038c2ecf20Sopenharmony_ci	mlxsw_sp_dpipe_adj_table_fini(mlxsw_sp);
13048c2ecf20Sopenharmony_ci	mlxsw_sp_dpipe_host6_table_fini(mlxsw_sp);
13058c2ecf20Sopenharmony_ci	mlxsw_sp_dpipe_host4_table_fini(mlxsw_sp);
13068c2ecf20Sopenharmony_ci	mlxsw_sp_dpipe_erif_table_fini(mlxsw_sp);
13078c2ecf20Sopenharmony_ci	devlink_dpipe_headers_unregister(devlink);
13088c2ecf20Sopenharmony_ci}
1309