18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (c) 2018 Mellanox Technologies. All rights reserved */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/err.h>
58c2ecf20Sopenharmony_ci#include <linux/gfp.h>
68c2ecf20Sopenharmony_ci#include <linux/kernel.h>
78c2ecf20Sopenharmony_ci#include <linux/list.h>
88c2ecf20Sopenharmony_ci#include <linux/netlink.h>
98c2ecf20Sopenharmony_ci#include <linux/rtnetlink.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include <net/inet_ecn.h>
128c2ecf20Sopenharmony_ci#include <net/ipv6.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include "reg.h"
158c2ecf20Sopenharmony_ci#include "spectrum.h"
168c2ecf20Sopenharmony_ci#include "spectrum_nve.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ciconst struct mlxsw_sp_nve_ops *mlxsw_sp1_nve_ops_arr[] = {
198c2ecf20Sopenharmony_ci	[MLXSW_SP_NVE_TYPE_VXLAN]	= &mlxsw_sp1_nve_vxlan_ops,
208c2ecf20Sopenharmony_ci};
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ciconst struct mlxsw_sp_nve_ops *mlxsw_sp2_nve_ops_arr[] = {
238c2ecf20Sopenharmony_ci	[MLXSW_SP_NVE_TYPE_VXLAN]	= &mlxsw_sp2_nve_vxlan_ops,
248c2ecf20Sopenharmony_ci};
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistruct mlxsw_sp_nve_mc_entry;
278c2ecf20Sopenharmony_cistruct mlxsw_sp_nve_mc_record;
288c2ecf20Sopenharmony_cistruct mlxsw_sp_nve_mc_list;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistruct mlxsw_sp_nve_mc_record_ops {
318c2ecf20Sopenharmony_ci	enum mlxsw_reg_tnumt_record_type type;
328c2ecf20Sopenharmony_ci	int (*entry_add)(struct mlxsw_sp_nve_mc_record *mc_record,
338c2ecf20Sopenharmony_ci			 struct mlxsw_sp_nve_mc_entry *mc_entry,
348c2ecf20Sopenharmony_ci			 const union mlxsw_sp_l3addr *addr);
358c2ecf20Sopenharmony_ci	void (*entry_del)(const struct mlxsw_sp_nve_mc_record *mc_record,
368c2ecf20Sopenharmony_ci			  const struct mlxsw_sp_nve_mc_entry *mc_entry);
378c2ecf20Sopenharmony_ci	void (*entry_set)(const struct mlxsw_sp_nve_mc_record *mc_record,
388c2ecf20Sopenharmony_ci			  const struct mlxsw_sp_nve_mc_entry *mc_entry,
398c2ecf20Sopenharmony_ci			  char *tnumt_pl, unsigned int entry_index);
408c2ecf20Sopenharmony_ci	bool (*entry_compare)(const struct mlxsw_sp_nve_mc_record *mc_record,
418c2ecf20Sopenharmony_ci			      const struct mlxsw_sp_nve_mc_entry *mc_entry,
428c2ecf20Sopenharmony_ci			      const union mlxsw_sp_l3addr *addr);
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistruct mlxsw_sp_nve_mc_list_key {
468c2ecf20Sopenharmony_ci	u16 fid_index;
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistruct mlxsw_sp_nve_mc_ipv6_entry {
508c2ecf20Sopenharmony_ci	struct in6_addr addr6;
518c2ecf20Sopenharmony_ci	u32 addr6_kvdl_index;
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistruct mlxsw_sp_nve_mc_entry {
558c2ecf20Sopenharmony_ci	union {
568c2ecf20Sopenharmony_ci		__be32 addr4;
578c2ecf20Sopenharmony_ci		struct mlxsw_sp_nve_mc_ipv6_entry ipv6_entry;
588c2ecf20Sopenharmony_ci	};
598c2ecf20Sopenharmony_ci	u8 valid:1;
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistruct mlxsw_sp_nve_mc_record {
638c2ecf20Sopenharmony_ci	struct list_head list;
648c2ecf20Sopenharmony_ci	enum mlxsw_sp_l3proto proto;
658c2ecf20Sopenharmony_ci	unsigned int num_entries;
668c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp;
678c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_list *mc_list;
688c2ecf20Sopenharmony_ci	const struct mlxsw_sp_nve_mc_record_ops *ops;
698c2ecf20Sopenharmony_ci	u32 kvdl_index;
708c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_entry entries[];
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistruct mlxsw_sp_nve_mc_list {
748c2ecf20Sopenharmony_ci	struct list_head records_list;
758c2ecf20Sopenharmony_ci	struct rhash_head ht_node;
768c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_list_key key;
778c2ecf20Sopenharmony_ci};
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic const struct rhashtable_params mlxsw_sp_nve_mc_list_ht_params = {
808c2ecf20Sopenharmony_ci	.key_len = sizeof(struct mlxsw_sp_nve_mc_list_key),
818c2ecf20Sopenharmony_ci	.key_offset = offsetof(struct mlxsw_sp_nve_mc_list, key),
828c2ecf20Sopenharmony_ci	.head_offset = offsetof(struct mlxsw_sp_nve_mc_list, ht_node),
838c2ecf20Sopenharmony_ci};
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic int
868c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_ipv4_entry_add(struct mlxsw_sp_nve_mc_record *mc_record,
878c2ecf20Sopenharmony_ci				      struct mlxsw_sp_nve_mc_entry *mc_entry,
888c2ecf20Sopenharmony_ci				      const union mlxsw_sp_l3addr *addr)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	mc_entry->addr4 = addr->addr4;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	return 0;
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic void
968c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_ipv4_entry_del(const struct mlxsw_sp_nve_mc_record *mc_record,
978c2ecf20Sopenharmony_ci				      const struct mlxsw_sp_nve_mc_entry *mc_entry)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic void
1028c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_ipv4_entry_set(const struct mlxsw_sp_nve_mc_record *mc_record,
1038c2ecf20Sopenharmony_ci				      const struct mlxsw_sp_nve_mc_entry *mc_entry,
1048c2ecf20Sopenharmony_ci				      char *tnumt_pl, unsigned int entry_index)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	u32 udip = be32_to_cpu(mc_entry->addr4);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	mlxsw_reg_tnumt_udip_set(tnumt_pl, entry_index, udip);
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic bool
1128c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_ipv4_entry_compare(const struct mlxsw_sp_nve_mc_record *mc_record,
1138c2ecf20Sopenharmony_ci					  const struct mlxsw_sp_nve_mc_entry *mc_entry,
1148c2ecf20Sopenharmony_ci					  const union mlxsw_sp_l3addr *addr)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	return mc_entry->addr4 == addr->addr4;
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic const struct mlxsw_sp_nve_mc_record_ops
1208c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_ipv4_ops = {
1218c2ecf20Sopenharmony_ci	.type		= MLXSW_REG_TNUMT_RECORD_TYPE_IPV4,
1228c2ecf20Sopenharmony_ci	.entry_add	= &mlxsw_sp_nve_mc_record_ipv4_entry_add,
1238c2ecf20Sopenharmony_ci	.entry_del	= &mlxsw_sp_nve_mc_record_ipv4_entry_del,
1248c2ecf20Sopenharmony_ci	.entry_set	= &mlxsw_sp_nve_mc_record_ipv4_entry_set,
1258c2ecf20Sopenharmony_ci	.entry_compare	= &mlxsw_sp_nve_mc_record_ipv4_entry_compare,
1268c2ecf20Sopenharmony_ci};
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic int
1298c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_ipv6_entry_add(struct mlxsw_sp_nve_mc_record *mc_record,
1308c2ecf20Sopenharmony_ci				      struct mlxsw_sp_nve_mc_entry *mc_entry,
1318c2ecf20Sopenharmony_ci				      const union mlxsw_sp_l3addr *addr)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	WARN_ON(1);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	return -EINVAL;
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic void
1398c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_ipv6_entry_del(const struct mlxsw_sp_nve_mc_record *mc_record,
1408c2ecf20Sopenharmony_ci				      const struct mlxsw_sp_nve_mc_entry *mc_entry)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic void
1458c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_ipv6_entry_set(const struct mlxsw_sp_nve_mc_record *mc_record,
1468c2ecf20Sopenharmony_ci				      const struct mlxsw_sp_nve_mc_entry *mc_entry,
1478c2ecf20Sopenharmony_ci				      char *tnumt_pl, unsigned int entry_index)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	u32 udip_ptr = mc_entry->ipv6_entry.addr6_kvdl_index;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	mlxsw_reg_tnumt_udip_ptr_set(tnumt_pl, entry_index, udip_ptr);
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic bool
1558c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_ipv6_entry_compare(const struct mlxsw_sp_nve_mc_record *mc_record,
1568c2ecf20Sopenharmony_ci					  const struct mlxsw_sp_nve_mc_entry *mc_entry,
1578c2ecf20Sopenharmony_ci					  const union mlxsw_sp_l3addr *addr)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	return ipv6_addr_equal(&mc_entry->ipv6_entry.addr6, &addr->addr6);
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic const struct mlxsw_sp_nve_mc_record_ops
1638c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_ipv6_ops = {
1648c2ecf20Sopenharmony_ci	.type		= MLXSW_REG_TNUMT_RECORD_TYPE_IPV6,
1658c2ecf20Sopenharmony_ci	.entry_add	= &mlxsw_sp_nve_mc_record_ipv6_entry_add,
1668c2ecf20Sopenharmony_ci	.entry_del	= &mlxsw_sp_nve_mc_record_ipv6_entry_del,
1678c2ecf20Sopenharmony_ci	.entry_set	= &mlxsw_sp_nve_mc_record_ipv6_entry_set,
1688c2ecf20Sopenharmony_ci	.entry_compare	= &mlxsw_sp_nve_mc_record_ipv6_entry_compare,
1698c2ecf20Sopenharmony_ci};
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic const struct mlxsw_sp_nve_mc_record_ops *
1728c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_ops_arr[] = {
1738c2ecf20Sopenharmony_ci	[MLXSW_SP_L3_PROTO_IPV4] = &mlxsw_sp_nve_mc_record_ipv4_ops,
1748c2ecf20Sopenharmony_ci	[MLXSW_SP_L3_PROTO_IPV6] = &mlxsw_sp_nve_mc_record_ipv6_ops,
1758c2ecf20Sopenharmony_ci};
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ciint mlxsw_sp_nve_learned_ip_resolve(struct mlxsw_sp *mlxsw_sp, u32 uip,
1788c2ecf20Sopenharmony_ci				    enum mlxsw_sp_l3proto proto,
1798c2ecf20Sopenharmony_ci				    union mlxsw_sp_l3addr *addr)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	switch (proto) {
1828c2ecf20Sopenharmony_ci	case MLXSW_SP_L3_PROTO_IPV4:
1838c2ecf20Sopenharmony_ci		addr->addr4 = cpu_to_be32(uip);
1848c2ecf20Sopenharmony_ci		return 0;
1858c2ecf20Sopenharmony_ci	default:
1868c2ecf20Sopenharmony_ci		WARN_ON(1);
1878c2ecf20Sopenharmony_ci		return -EINVAL;
1888c2ecf20Sopenharmony_ci	}
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic struct mlxsw_sp_nve_mc_list *
1928c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_list_find(struct mlxsw_sp *mlxsw_sp,
1938c2ecf20Sopenharmony_ci			  const struct mlxsw_sp_nve_mc_list_key *key)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve *nve = mlxsw_sp->nve;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	return rhashtable_lookup_fast(&nve->mc_list_ht, key,
1988c2ecf20Sopenharmony_ci				      mlxsw_sp_nve_mc_list_ht_params);
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic struct mlxsw_sp_nve_mc_list *
2028c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_list_create(struct mlxsw_sp *mlxsw_sp,
2038c2ecf20Sopenharmony_ci			    const struct mlxsw_sp_nve_mc_list_key *key)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve *nve = mlxsw_sp->nve;
2068c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_list *mc_list;
2078c2ecf20Sopenharmony_ci	int err;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	mc_list = kmalloc(sizeof(*mc_list), GFP_KERNEL);
2108c2ecf20Sopenharmony_ci	if (!mc_list)
2118c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&mc_list->records_list);
2148c2ecf20Sopenharmony_ci	mc_list->key = *key;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	err = rhashtable_insert_fast(&nve->mc_list_ht, &mc_list->ht_node,
2178c2ecf20Sopenharmony_ci				     mlxsw_sp_nve_mc_list_ht_params);
2188c2ecf20Sopenharmony_ci	if (err)
2198c2ecf20Sopenharmony_ci		goto err_rhashtable_insert;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	return mc_list;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cierr_rhashtable_insert:
2248c2ecf20Sopenharmony_ci	kfree(mc_list);
2258c2ecf20Sopenharmony_ci	return ERR_PTR(err);
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic void mlxsw_sp_nve_mc_list_destroy(struct mlxsw_sp *mlxsw_sp,
2298c2ecf20Sopenharmony_ci					 struct mlxsw_sp_nve_mc_list *mc_list)
2308c2ecf20Sopenharmony_ci{
2318c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve *nve = mlxsw_sp->nve;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	rhashtable_remove_fast(&nve->mc_list_ht, &mc_list->ht_node,
2348c2ecf20Sopenharmony_ci			       mlxsw_sp_nve_mc_list_ht_params);
2358c2ecf20Sopenharmony_ci	WARN_ON(!list_empty(&mc_list->records_list));
2368c2ecf20Sopenharmony_ci	kfree(mc_list);
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistatic struct mlxsw_sp_nve_mc_list *
2408c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_list_get(struct mlxsw_sp *mlxsw_sp,
2418c2ecf20Sopenharmony_ci			 const struct mlxsw_sp_nve_mc_list_key *key)
2428c2ecf20Sopenharmony_ci{
2438c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_list *mc_list;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	mc_list = mlxsw_sp_nve_mc_list_find(mlxsw_sp, key);
2468c2ecf20Sopenharmony_ci	if (mc_list)
2478c2ecf20Sopenharmony_ci		return mc_list;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	return mlxsw_sp_nve_mc_list_create(mlxsw_sp, key);
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cistatic void
2538c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_list_put(struct mlxsw_sp *mlxsw_sp,
2548c2ecf20Sopenharmony_ci			 struct mlxsw_sp_nve_mc_list *mc_list)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	if (!list_empty(&mc_list->records_list))
2578c2ecf20Sopenharmony_ci		return;
2588c2ecf20Sopenharmony_ci	mlxsw_sp_nve_mc_list_destroy(mlxsw_sp, mc_list);
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic struct mlxsw_sp_nve_mc_record *
2628c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_create(struct mlxsw_sp *mlxsw_sp,
2638c2ecf20Sopenharmony_ci			      struct mlxsw_sp_nve_mc_list *mc_list,
2648c2ecf20Sopenharmony_ci			      enum mlxsw_sp_l3proto proto)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	unsigned int num_max_entries = mlxsw_sp->nve->num_max_mc_entries[proto];
2678c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_record *mc_record;
2688c2ecf20Sopenharmony_ci	int err;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	mc_record = kzalloc(struct_size(mc_record, entries, num_max_entries),
2718c2ecf20Sopenharmony_ci			    GFP_KERNEL);
2728c2ecf20Sopenharmony_ci	if (!mc_record)
2738c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	err = mlxsw_sp_kvdl_alloc(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_TNUMT, 1,
2768c2ecf20Sopenharmony_ci				  &mc_record->kvdl_index);
2778c2ecf20Sopenharmony_ci	if (err)
2788c2ecf20Sopenharmony_ci		goto err_kvdl_alloc;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	mc_record->ops = mlxsw_sp_nve_mc_record_ops_arr[proto];
2818c2ecf20Sopenharmony_ci	mc_record->mlxsw_sp = mlxsw_sp;
2828c2ecf20Sopenharmony_ci	mc_record->mc_list = mc_list;
2838c2ecf20Sopenharmony_ci	mc_record->proto = proto;
2848c2ecf20Sopenharmony_ci	list_add_tail(&mc_record->list, &mc_list->records_list);
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	return mc_record;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_cierr_kvdl_alloc:
2898c2ecf20Sopenharmony_ci	kfree(mc_record);
2908c2ecf20Sopenharmony_ci	return ERR_PTR(err);
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_cistatic void
2948c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_destroy(struct mlxsw_sp_nve_mc_record *mc_record)
2958c2ecf20Sopenharmony_ci{
2968c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mc_record->mlxsw_sp;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	list_del(&mc_record->list);
2998c2ecf20Sopenharmony_ci	mlxsw_sp_kvdl_free(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_TNUMT, 1,
3008c2ecf20Sopenharmony_ci			   mc_record->kvdl_index);
3018c2ecf20Sopenharmony_ci	WARN_ON(mc_record->num_entries);
3028c2ecf20Sopenharmony_ci	kfree(mc_record);
3038c2ecf20Sopenharmony_ci}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cistatic struct mlxsw_sp_nve_mc_record *
3068c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_get(struct mlxsw_sp *mlxsw_sp,
3078c2ecf20Sopenharmony_ci			   struct mlxsw_sp_nve_mc_list *mc_list,
3088c2ecf20Sopenharmony_ci			   enum mlxsw_sp_l3proto proto)
3098c2ecf20Sopenharmony_ci{
3108c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_record *mc_record;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	list_for_each_entry_reverse(mc_record, &mc_list->records_list, list) {
3138c2ecf20Sopenharmony_ci		unsigned int num_entries = mc_record->num_entries;
3148c2ecf20Sopenharmony_ci		struct mlxsw_sp_nve *nve = mlxsw_sp->nve;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci		if (mc_record->proto == proto &&
3178c2ecf20Sopenharmony_ci		    num_entries < nve->num_max_mc_entries[proto])
3188c2ecf20Sopenharmony_ci			return mc_record;
3198c2ecf20Sopenharmony_ci	}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	return mlxsw_sp_nve_mc_record_create(mlxsw_sp, mc_list, proto);
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_cistatic void
3258c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_put(struct mlxsw_sp_nve_mc_record *mc_record)
3268c2ecf20Sopenharmony_ci{
3278c2ecf20Sopenharmony_ci	if (mc_record->num_entries != 0)
3288c2ecf20Sopenharmony_ci		return;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	mlxsw_sp_nve_mc_record_destroy(mc_record);
3318c2ecf20Sopenharmony_ci}
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_cistatic struct mlxsw_sp_nve_mc_entry *
3348c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_free_entry_find(struct mlxsw_sp_nve_mc_record *mc_record)
3358c2ecf20Sopenharmony_ci{
3368c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve *nve = mc_record->mlxsw_sp->nve;
3378c2ecf20Sopenharmony_ci	unsigned int num_max_entries;
3388c2ecf20Sopenharmony_ci	int i;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	num_max_entries = nve->num_max_mc_entries[mc_record->proto];
3418c2ecf20Sopenharmony_ci	for (i = 0; i < num_max_entries; i++) {
3428c2ecf20Sopenharmony_ci		if (mc_record->entries[i].valid)
3438c2ecf20Sopenharmony_ci			continue;
3448c2ecf20Sopenharmony_ci		return &mc_record->entries[i];
3458c2ecf20Sopenharmony_ci	}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	return NULL;
3488c2ecf20Sopenharmony_ci}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_cistatic int
3518c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_refresh(struct mlxsw_sp_nve_mc_record *mc_record)
3528c2ecf20Sopenharmony_ci{
3538c2ecf20Sopenharmony_ci	enum mlxsw_reg_tnumt_record_type type = mc_record->ops->type;
3548c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_list *mc_list = mc_record->mc_list;
3558c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mc_record->mlxsw_sp;
3568c2ecf20Sopenharmony_ci	char tnumt_pl[MLXSW_REG_TNUMT_LEN];
3578c2ecf20Sopenharmony_ci	unsigned int num_max_entries;
3588c2ecf20Sopenharmony_ci	unsigned int num_entries = 0;
3598c2ecf20Sopenharmony_ci	u32 next_kvdl_index = 0;
3608c2ecf20Sopenharmony_ci	bool next_valid = false;
3618c2ecf20Sopenharmony_ci	int i;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	if (!list_is_last(&mc_record->list, &mc_list->records_list)) {
3648c2ecf20Sopenharmony_ci		struct mlxsw_sp_nve_mc_record *next_record;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci		next_record = list_next_entry(mc_record, list);
3678c2ecf20Sopenharmony_ci		next_kvdl_index = next_record->kvdl_index;
3688c2ecf20Sopenharmony_ci		next_valid = true;
3698c2ecf20Sopenharmony_ci	}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	mlxsw_reg_tnumt_pack(tnumt_pl, type, MLXSW_REG_TNUMT_TUNNEL_PORT_NVE,
3728c2ecf20Sopenharmony_ci			     mc_record->kvdl_index, next_valid,
3738c2ecf20Sopenharmony_ci			     next_kvdl_index, mc_record->num_entries);
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	num_max_entries = mlxsw_sp->nve->num_max_mc_entries[mc_record->proto];
3768c2ecf20Sopenharmony_ci	for (i = 0; i < num_max_entries; i++) {
3778c2ecf20Sopenharmony_ci		struct mlxsw_sp_nve_mc_entry *mc_entry;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci		mc_entry = &mc_record->entries[i];
3808c2ecf20Sopenharmony_ci		if (!mc_entry->valid)
3818c2ecf20Sopenharmony_ci			continue;
3828c2ecf20Sopenharmony_ci		mc_record->ops->entry_set(mc_record, mc_entry, tnumt_pl,
3838c2ecf20Sopenharmony_ci					  num_entries++);
3848c2ecf20Sopenharmony_ci	}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	WARN_ON(num_entries != mc_record->num_entries);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(tnumt), tnumt_pl);
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic bool
3928c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_is_first(struct mlxsw_sp_nve_mc_record *mc_record)
3938c2ecf20Sopenharmony_ci{
3948c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_list *mc_list = mc_record->mc_list;
3958c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_record *first_record;
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	first_record = list_first_entry(&mc_list->records_list,
3988c2ecf20Sopenharmony_ci					struct mlxsw_sp_nve_mc_record, list);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	return mc_record == first_record;
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_cistatic struct mlxsw_sp_nve_mc_entry *
4048c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_entry_find(struct mlxsw_sp_nve_mc_record *mc_record,
4058c2ecf20Sopenharmony_ci			   union mlxsw_sp_l3addr *addr)
4068c2ecf20Sopenharmony_ci{
4078c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve *nve = mc_record->mlxsw_sp->nve;
4088c2ecf20Sopenharmony_ci	unsigned int num_max_entries;
4098c2ecf20Sopenharmony_ci	int i;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	num_max_entries = nve->num_max_mc_entries[mc_record->proto];
4128c2ecf20Sopenharmony_ci	for (i = 0; i < num_max_entries; i++) {
4138c2ecf20Sopenharmony_ci		struct mlxsw_sp_nve_mc_entry *mc_entry;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci		mc_entry = &mc_record->entries[i];
4168c2ecf20Sopenharmony_ci		if (!mc_entry->valid)
4178c2ecf20Sopenharmony_ci			continue;
4188c2ecf20Sopenharmony_ci		if (mc_record->ops->entry_compare(mc_record, mc_entry, addr))
4198c2ecf20Sopenharmony_ci			return mc_entry;
4208c2ecf20Sopenharmony_ci	}
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	return NULL;
4238c2ecf20Sopenharmony_ci}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_cistatic int
4268c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_ip_add(struct mlxsw_sp_nve_mc_record *mc_record,
4278c2ecf20Sopenharmony_ci			      union mlxsw_sp_l3addr *addr)
4288c2ecf20Sopenharmony_ci{
4298c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_entry *mc_entry = NULL;
4308c2ecf20Sopenharmony_ci	int err;
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	mc_entry = mlxsw_sp_nve_mc_free_entry_find(mc_record);
4338c2ecf20Sopenharmony_ci	if (WARN_ON(!mc_entry))
4348c2ecf20Sopenharmony_ci		return -EINVAL;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	err = mc_record->ops->entry_add(mc_record, mc_entry, addr);
4378c2ecf20Sopenharmony_ci	if (err)
4388c2ecf20Sopenharmony_ci		return err;
4398c2ecf20Sopenharmony_ci	mc_record->num_entries++;
4408c2ecf20Sopenharmony_ci	mc_entry->valid = true;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	err = mlxsw_sp_nve_mc_record_refresh(mc_record);
4438c2ecf20Sopenharmony_ci	if (err)
4448c2ecf20Sopenharmony_ci		goto err_record_refresh;
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	/* If this is a new record and not the first one, then we need to
4478c2ecf20Sopenharmony_ci	 * update the next pointer of the previous entry
4488c2ecf20Sopenharmony_ci	 */
4498c2ecf20Sopenharmony_ci	if (mc_record->num_entries != 1 ||
4508c2ecf20Sopenharmony_ci	    mlxsw_sp_nve_mc_record_is_first(mc_record))
4518c2ecf20Sopenharmony_ci		return 0;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	err = mlxsw_sp_nve_mc_record_refresh(list_prev_entry(mc_record, list));
4548c2ecf20Sopenharmony_ci	if (err)
4558c2ecf20Sopenharmony_ci		goto err_prev_record_refresh;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	return 0;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_cierr_prev_record_refresh:
4608c2ecf20Sopenharmony_cierr_record_refresh:
4618c2ecf20Sopenharmony_ci	mc_entry->valid = false;
4628c2ecf20Sopenharmony_ci	mc_record->num_entries--;
4638c2ecf20Sopenharmony_ci	mc_record->ops->entry_del(mc_record, mc_entry);
4648c2ecf20Sopenharmony_ci	return err;
4658c2ecf20Sopenharmony_ci}
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_cistatic void
4688c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_entry_del(struct mlxsw_sp_nve_mc_record *mc_record,
4698c2ecf20Sopenharmony_ci				 struct mlxsw_sp_nve_mc_entry *mc_entry)
4708c2ecf20Sopenharmony_ci{
4718c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_list *mc_list = mc_record->mc_list;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	mc_entry->valid = false;
4748c2ecf20Sopenharmony_ci	mc_record->num_entries--;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	/* When the record continues to exist we only need to invalidate
4778c2ecf20Sopenharmony_ci	 * the requested entry
4788c2ecf20Sopenharmony_ci	 */
4798c2ecf20Sopenharmony_ci	if (mc_record->num_entries != 0) {
4808c2ecf20Sopenharmony_ci		mlxsw_sp_nve_mc_record_refresh(mc_record);
4818c2ecf20Sopenharmony_ci		mc_record->ops->entry_del(mc_record, mc_entry);
4828c2ecf20Sopenharmony_ci		return;
4838c2ecf20Sopenharmony_ci	}
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	/* If the record needs to be deleted, but it is not the first,
4868c2ecf20Sopenharmony_ci	 * then we need to make sure that the previous record no longer
4878c2ecf20Sopenharmony_ci	 * points to it. Remove deleted record from the list to reflect
4888c2ecf20Sopenharmony_ci	 * that and then re-add it at the end, so that it could be
4898c2ecf20Sopenharmony_ci	 * properly removed by the record destruction code
4908c2ecf20Sopenharmony_ci	 */
4918c2ecf20Sopenharmony_ci	if (!mlxsw_sp_nve_mc_record_is_first(mc_record)) {
4928c2ecf20Sopenharmony_ci		struct mlxsw_sp_nve_mc_record *prev_record;
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci		prev_record = list_prev_entry(mc_record, list);
4958c2ecf20Sopenharmony_ci		list_del(&mc_record->list);
4968c2ecf20Sopenharmony_ci		mlxsw_sp_nve_mc_record_refresh(prev_record);
4978c2ecf20Sopenharmony_ci		list_add_tail(&mc_record->list, &mc_list->records_list);
4988c2ecf20Sopenharmony_ci		mc_record->ops->entry_del(mc_record, mc_entry);
4998c2ecf20Sopenharmony_ci		return;
5008c2ecf20Sopenharmony_ci	}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	/* If the first record needs to be deleted, but the list is not
5038c2ecf20Sopenharmony_ci	 * singular, then the second record needs to be written in the
5048c2ecf20Sopenharmony_ci	 * first record's address, as this address is stored as a property
5058c2ecf20Sopenharmony_ci	 * of the FID
5068c2ecf20Sopenharmony_ci	 */
5078c2ecf20Sopenharmony_ci	if (mlxsw_sp_nve_mc_record_is_first(mc_record) &&
5088c2ecf20Sopenharmony_ci	    !list_is_singular(&mc_list->records_list)) {
5098c2ecf20Sopenharmony_ci		struct mlxsw_sp_nve_mc_record *next_record;
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci		next_record = list_next_entry(mc_record, list);
5128c2ecf20Sopenharmony_ci		swap(mc_record->kvdl_index, next_record->kvdl_index);
5138c2ecf20Sopenharmony_ci		mlxsw_sp_nve_mc_record_refresh(next_record);
5148c2ecf20Sopenharmony_ci		mc_record->ops->entry_del(mc_record, mc_entry);
5158c2ecf20Sopenharmony_ci		return;
5168c2ecf20Sopenharmony_ci	}
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	/* This is the last case where the last remaining record needs to
5198c2ecf20Sopenharmony_ci	 * be deleted. Simply delete the entry
5208c2ecf20Sopenharmony_ci	 */
5218c2ecf20Sopenharmony_ci	mc_record->ops->entry_del(mc_record, mc_entry);
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_cistatic struct mlxsw_sp_nve_mc_record *
5258c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_find(struct mlxsw_sp_nve_mc_list *mc_list,
5268c2ecf20Sopenharmony_ci			    enum mlxsw_sp_l3proto proto,
5278c2ecf20Sopenharmony_ci			    union mlxsw_sp_l3addr *addr,
5288c2ecf20Sopenharmony_ci			    struct mlxsw_sp_nve_mc_entry **mc_entry)
5298c2ecf20Sopenharmony_ci{
5308c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_record *mc_record;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	list_for_each_entry(mc_record, &mc_list->records_list, list) {
5338c2ecf20Sopenharmony_ci		if (mc_record->proto != proto)
5348c2ecf20Sopenharmony_ci			continue;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci		*mc_entry = mlxsw_sp_nve_mc_entry_find(mc_record, addr);
5378c2ecf20Sopenharmony_ci		if (*mc_entry)
5388c2ecf20Sopenharmony_ci			return mc_record;
5398c2ecf20Sopenharmony_ci	}
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	return NULL;
5428c2ecf20Sopenharmony_ci}
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_cistatic int mlxsw_sp_nve_mc_list_ip_add(struct mlxsw_sp *mlxsw_sp,
5458c2ecf20Sopenharmony_ci				       struct mlxsw_sp_nve_mc_list *mc_list,
5468c2ecf20Sopenharmony_ci				       enum mlxsw_sp_l3proto proto,
5478c2ecf20Sopenharmony_ci				       union mlxsw_sp_l3addr *addr)
5488c2ecf20Sopenharmony_ci{
5498c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_record *mc_record;
5508c2ecf20Sopenharmony_ci	int err;
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	mc_record = mlxsw_sp_nve_mc_record_get(mlxsw_sp, mc_list, proto);
5538c2ecf20Sopenharmony_ci	if (IS_ERR(mc_record))
5548c2ecf20Sopenharmony_ci		return PTR_ERR(mc_record);
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	err = mlxsw_sp_nve_mc_record_ip_add(mc_record, addr);
5578c2ecf20Sopenharmony_ci	if (err)
5588c2ecf20Sopenharmony_ci		goto err_ip_add;
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	return 0;
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_cierr_ip_add:
5638c2ecf20Sopenharmony_ci	mlxsw_sp_nve_mc_record_put(mc_record);
5648c2ecf20Sopenharmony_ci	return err;
5658c2ecf20Sopenharmony_ci}
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_cistatic void mlxsw_sp_nve_mc_list_ip_del(struct mlxsw_sp *mlxsw_sp,
5688c2ecf20Sopenharmony_ci					struct mlxsw_sp_nve_mc_list *mc_list,
5698c2ecf20Sopenharmony_ci					enum mlxsw_sp_l3proto proto,
5708c2ecf20Sopenharmony_ci					union mlxsw_sp_l3addr *addr)
5718c2ecf20Sopenharmony_ci{
5728c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_record *mc_record;
5738c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_entry *mc_entry;
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	mc_record = mlxsw_sp_nve_mc_record_find(mc_list, proto, addr,
5768c2ecf20Sopenharmony_ci						&mc_entry);
5778c2ecf20Sopenharmony_ci	if (!mc_record)
5788c2ecf20Sopenharmony_ci		return;
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	mlxsw_sp_nve_mc_record_entry_del(mc_record, mc_entry);
5818c2ecf20Sopenharmony_ci	mlxsw_sp_nve_mc_record_put(mc_record);
5828c2ecf20Sopenharmony_ci}
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_cistatic int
5858c2ecf20Sopenharmony_cimlxsw_sp_nve_fid_flood_index_set(struct mlxsw_sp_fid *fid,
5868c2ecf20Sopenharmony_ci				 struct mlxsw_sp_nve_mc_list *mc_list)
5878c2ecf20Sopenharmony_ci{
5888c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_record *mc_record;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	/* The address of the first record in the list is a property of
5918c2ecf20Sopenharmony_ci	 * the FID and we never change it. It only needs to be set when
5928c2ecf20Sopenharmony_ci	 * a new list is created
5938c2ecf20Sopenharmony_ci	 */
5948c2ecf20Sopenharmony_ci	if (mlxsw_sp_fid_nve_flood_index_is_set(fid))
5958c2ecf20Sopenharmony_ci		return 0;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	mc_record = list_first_entry(&mc_list->records_list,
5988c2ecf20Sopenharmony_ci				     struct mlxsw_sp_nve_mc_record, list);
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	return mlxsw_sp_fid_nve_flood_index_set(fid, mc_record->kvdl_index);
6018c2ecf20Sopenharmony_ci}
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_cistatic void
6048c2ecf20Sopenharmony_cimlxsw_sp_nve_fid_flood_index_clear(struct mlxsw_sp_fid *fid,
6058c2ecf20Sopenharmony_ci				   struct mlxsw_sp_nve_mc_list *mc_list)
6068c2ecf20Sopenharmony_ci{
6078c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_record *mc_record;
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	/* The address of the first record needs to be invalidated only when
6108c2ecf20Sopenharmony_ci	 * the last record is about to be removed
6118c2ecf20Sopenharmony_ci	 */
6128c2ecf20Sopenharmony_ci	if (!list_is_singular(&mc_list->records_list))
6138c2ecf20Sopenharmony_ci		return;
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	mc_record = list_first_entry(&mc_list->records_list,
6168c2ecf20Sopenharmony_ci				     struct mlxsw_sp_nve_mc_record, list);
6178c2ecf20Sopenharmony_ci	if (mc_record->num_entries != 1)
6188c2ecf20Sopenharmony_ci		return;
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	return mlxsw_sp_fid_nve_flood_index_clear(fid);
6218c2ecf20Sopenharmony_ci}
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ciint mlxsw_sp_nve_flood_ip_add(struct mlxsw_sp *mlxsw_sp,
6248c2ecf20Sopenharmony_ci			      struct mlxsw_sp_fid *fid,
6258c2ecf20Sopenharmony_ci			      enum mlxsw_sp_l3proto proto,
6268c2ecf20Sopenharmony_ci			      union mlxsw_sp_l3addr *addr)
6278c2ecf20Sopenharmony_ci{
6288c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_list_key key = { 0 };
6298c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_list *mc_list;
6308c2ecf20Sopenharmony_ci	int err;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	key.fid_index = mlxsw_sp_fid_index(fid);
6338c2ecf20Sopenharmony_ci	mc_list = mlxsw_sp_nve_mc_list_get(mlxsw_sp, &key);
6348c2ecf20Sopenharmony_ci	if (IS_ERR(mc_list))
6358c2ecf20Sopenharmony_ci		return PTR_ERR(mc_list);
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	err = mlxsw_sp_nve_mc_list_ip_add(mlxsw_sp, mc_list, proto, addr);
6388c2ecf20Sopenharmony_ci	if (err)
6398c2ecf20Sopenharmony_ci		goto err_add_ip;
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	err = mlxsw_sp_nve_fid_flood_index_set(fid, mc_list);
6428c2ecf20Sopenharmony_ci	if (err)
6438c2ecf20Sopenharmony_ci		goto err_fid_flood_index_set;
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	return 0;
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_cierr_fid_flood_index_set:
6488c2ecf20Sopenharmony_ci	mlxsw_sp_nve_mc_list_ip_del(mlxsw_sp, mc_list, proto, addr);
6498c2ecf20Sopenharmony_cierr_add_ip:
6508c2ecf20Sopenharmony_ci	mlxsw_sp_nve_mc_list_put(mlxsw_sp, mc_list);
6518c2ecf20Sopenharmony_ci	return err;
6528c2ecf20Sopenharmony_ci}
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_civoid mlxsw_sp_nve_flood_ip_del(struct mlxsw_sp *mlxsw_sp,
6558c2ecf20Sopenharmony_ci			       struct mlxsw_sp_fid *fid,
6568c2ecf20Sopenharmony_ci			       enum mlxsw_sp_l3proto proto,
6578c2ecf20Sopenharmony_ci			       union mlxsw_sp_l3addr *addr)
6588c2ecf20Sopenharmony_ci{
6598c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_list_key key = { 0 };
6608c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_list *mc_list;
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	key.fid_index = mlxsw_sp_fid_index(fid);
6638c2ecf20Sopenharmony_ci	mc_list = mlxsw_sp_nve_mc_list_find(mlxsw_sp, &key);
6648c2ecf20Sopenharmony_ci	if (!mc_list)
6658c2ecf20Sopenharmony_ci		return;
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	mlxsw_sp_nve_fid_flood_index_clear(fid, mc_list);
6688c2ecf20Sopenharmony_ci	mlxsw_sp_nve_mc_list_ip_del(mlxsw_sp, mc_list, proto, addr);
6698c2ecf20Sopenharmony_ci	mlxsw_sp_nve_mc_list_put(mlxsw_sp, mc_list);
6708c2ecf20Sopenharmony_ci}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_cistatic void
6738c2ecf20Sopenharmony_cimlxsw_sp_nve_mc_record_delete(struct mlxsw_sp_nve_mc_record *mc_record)
6748c2ecf20Sopenharmony_ci{
6758c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve *nve = mc_record->mlxsw_sp->nve;
6768c2ecf20Sopenharmony_ci	unsigned int num_max_entries;
6778c2ecf20Sopenharmony_ci	int i;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	num_max_entries = nve->num_max_mc_entries[mc_record->proto];
6808c2ecf20Sopenharmony_ci	for (i = 0; i < num_max_entries; i++) {
6818c2ecf20Sopenharmony_ci		struct mlxsw_sp_nve_mc_entry *mc_entry = &mc_record->entries[i];
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci		if (!mc_entry->valid)
6848c2ecf20Sopenharmony_ci			continue;
6858c2ecf20Sopenharmony_ci		mlxsw_sp_nve_mc_record_entry_del(mc_record, mc_entry);
6868c2ecf20Sopenharmony_ci	}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	WARN_ON(mc_record->num_entries);
6898c2ecf20Sopenharmony_ci	mlxsw_sp_nve_mc_record_put(mc_record);
6908c2ecf20Sopenharmony_ci}
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_cistatic void mlxsw_sp_nve_flood_ip_flush(struct mlxsw_sp *mlxsw_sp,
6938c2ecf20Sopenharmony_ci					struct mlxsw_sp_fid *fid)
6948c2ecf20Sopenharmony_ci{
6958c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_record *mc_record, *tmp;
6968c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_list_key key = { 0 };
6978c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_mc_list *mc_list;
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	if (!mlxsw_sp_fid_nve_flood_index_is_set(fid))
7008c2ecf20Sopenharmony_ci		return;
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	mlxsw_sp_fid_nve_flood_index_clear(fid);
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	key.fid_index = mlxsw_sp_fid_index(fid);
7058c2ecf20Sopenharmony_ci	mc_list = mlxsw_sp_nve_mc_list_find(mlxsw_sp, &key);
7068c2ecf20Sopenharmony_ci	if (WARN_ON(!mc_list))
7078c2ecf20Sopenharmony_ci		return;
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	list_for_each_entry_safe(mc_record, tmp, &mc_list->records_list, list)
7108c2ecf20Sopenharmony_ci		mlxsw_sp_nve_mc_record_delete(mc_record);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	WARN_ON(!list_empty(&mc_list->records_list));
7138c2ecf20Sopenharmony_ci	mlxsw_sp_nve_mc_list_put(mlxsw_sp, mc_list);
7148c2ecf20Sopenharmony_ci}
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_cistatic int mlxsw_sp_nve_tunnel_init(struct mlxsw_sp *mlxsw_sp,
7178c2ecf20Sopenharmony_ci				    struct mlxsw_sp_nve_config *config)
7188c2ecf20Sopenharmony_ci{
7198c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve *nve = mlxsw_sp->nve;
7208c2ecf20Sopenharmony_ci	const struct mlxsw_sp_nve_ops *ops;
7218c2ecf20Sopenharmony_ci	int err;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	if (nve->num_nve_tunnels++ != 0)
7248c2ecf20Sopenharmony_ci		return 0;
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	nve->config = *config;
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	err = mlxsw_sp_kvdl_alloc(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ, 1,
7298c2ecf20Sopenharmony_ci				  &nve->tunnel_index);
7308c2ecf20Sopenharmony_ci	if (err)
7318c2ecf20Sopenharmony_ci		goto err_kvdl_alloc;
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	ops = nve->nve_ops_arr[config->type];
7348c2ecf20Sopenharmony_ci	err = ops->init(nve, config);
7358c2ecf20Sopenharmony_ci	if (err)
7368c2ecf20Sopenharmony_ci		goto err_ops_init;
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	return 0;
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_cierr_ops_init:
7418c2ecf20Sopenharmony_ci	mlxsw_sp_kvdl_free(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ, 1,
7428c2ecf20Sopenharmony_ci			   nve->tunnel_index);
7438c2ecf20Sopenharmony_cierr_kvdl_alloc:
7448c2ecf20Sopenharmony_ci	memset(&nve->config, 0, sizeof(nve->config));
7458c2ecf20Sopenharmony_ci	nve->num_nve_tunnels--;
7468c2ecf20Sopenharmony_ci	return err;
7478c2ecf20Sopenharmony_ci}
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_cistatic void mlxsw_sp_nve_tunnel_fini(struct mlxsw_sp *mlxsw_sp)
7508c2ecf20Sopenharmony_ci{
7518c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve *nve = mlxsw_sp->nve;
7528c2ecf20Sopenharmony_ci	const struct mlxsw_sp_nve_ops *ops;
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	ops = nve->nve_ops_arr[nve->config.type];
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	if (mlxsw_sp->nve->num_nve_tunnels == 1) {
7578c2ecf20Sopenharmony_ci		ops->fini(nve);
7588c2ecf20Sopenharmony_ci		mlxsw_sp_kvdl_free(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ, 1,
7598c2ecf20Sopenharmony_ci				   nve->tunnel_index);
7608c2ecf20Sopenharmony_ci		memset(&nve->config, 0, sizeof(nve->config));
7618c2ecf20Sopenharmony_ci	}
7628c2ecf20Sopenharmony_ci	nve->num_nve_tunnels--;
7638c2ecf20Sopenharmony_ci}
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_cistatic void mlxsw_sp_nve_fdb_flush_by_fid(struct mlxsw_sp *mlxsw_sp,
7668c2ecf20Sopenharmony_ci					  u16 fid_index)
7678c2ecf20Sopenharmony_ci{
7688c2ecf20Sopenharmony_ci	char sfdf_pl[MLXSW_REG_SFDF_LEN];
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	mlxsw_reg_sfdf_pack(sfdf_pl, MLXSW_REG_SFDF_FLUSH_PER_NVE_AND_FID);
7718c2ecf20Sopenharmony_ci	mlxsw_reg_sfdf_fid_set(sfdf_pl, fid_index);
7728c2ecf20Sopenharmony_ci	mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
7738c2ecf20Sopenharmony_ci}
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_cistatic void mlxsw_sp_nve_fdb_clear_offload(struct mlxsw_sp *mlxsw_sp,
7768c2ecf20Sopenharmony_ci					   const struct mlxsw_sp_fid *fid,
7778c2ecf20Sopenharmony_ci					   const struct net_device *nve_dev,
7788c2ecf20Sopenharmony_ci					   __be32 vni)
7798c2ecf20Sopenharmony_ci{
7808c2ecf20Sopenharmony_ci	const struct mlxsw_sp_nve_ops *ops;
7818c2ecf20Sopenharmony_ci	enum mlxsw_sp_nve_type type;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	if (WARN_ON(mlxsw_sp_fid_nve_type(fid, &type)))
7848c2ecf20Sopenharmony_ci		return;
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	ops = mlxsw_sp->nve->nve_ops_arr[type];
7878c2ecf20Sopenharmony_ci	ops->fdb_clear_offload(nve_dev, vni);
7888c2ecf20Sopenharmony_ci}
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ciint mlxsw_sp_nve_fid_enable(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_fid *fid,
7918c2ecf20Sopenharmony_ci			    struct mlxsw_sp_nve_params *params,
7928c2ecf20Sopenharmony_ci			    struct netlink_ext_ack *extack)
7938c2ecf20Sopenharmony_ci{
7948c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve *nve = mlxsw_sp->nve;
7958c2ecf20Sopenharmony_ci	const struct mlxsw_sp_nve_ops *ops;
7968c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve_config config;
7978c2ecf20Sopenharmony_ci	int err;
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci	ops = nve->nve_ops_arr[params->type];
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	if (!ops->can_offload(nve, params->dev, extack))
8028c2ecf20Sopenharmony_ci		return -EINVAL;
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	memset(&config, 0, sizeof(config));
8058c2ecf20Sopenharmony_ci	ops->nve_config(nve, params->dev, &config);
8068c2ecf20Sopenharmony_ci	if (nve->num_nve_tunnels &&
8078c2ecf20Sopenharmony_ci	    memcmp(&config, &nve->config, sizeof(config))) {
8088c2ecf20Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Conflicting NVE tunnels configuration");
8098c2ecf20Sopenharmony_ci		return -EINVAL;
8108c2ecf20Sopenharmony_ci	}
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	err = mlxsw_sp_nve_tunnel_init(mlxsw_sp, &config);
8138c2ecf20Sopenharmony_ci	if (err) {
8148c2ecf20Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Failed to initialize NVE tunnel");
8158c2ecf20Sopenharmony_ci		return err;
8168c2ecf20Sopenharmony_ci	}
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	err = mlxsw_sp_fid_vni_set(fid, params->type, params->vni,
8198c2ecf20Sopenharmony_ci				   params->dev->ifindex);
8208c2ecf20Sopenharmony_ci	if (err) {
8218c2ecf20Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "Failed to set VNI on FID");
8228c2ecf20Sopenharmony_ci		goto err_fid_vni_set;
8238c2ecf20Sopenharmony_ci	}
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	err = ops->fdb_replay(params->dev, params->vni, extack);
8268c2ecf20Sopenharmony_ci	if (err)
8278c2ecf20Sopenharmony_ci		goto err_fdb_replay;
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	return 0;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_cierr_fdb_replay:
8328c2ecf20Sopenharmony_ci	mlxsw_sp_fid_vni_clear(fid);
8338c2ecf20Sopenharmony_cierr_fid_vni_set:
8348c2ecf20Sopenharmony_ci	mlxsw_sp_nve_tunnel_fini(mlxsw_sp);
8358c2ecf20Sopenharmony_ci	return err;
8368c2ecf20Sopenharmony_ci}
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_civoid mlxsw_sp_nve_fid_disable(struct mlxsw_sp *mlxsw_sp,
8398c2ecf20Sopenharmony_ci			      struct mlxsw_sp_fid *fid)
8408c2ecf20Sopenharmony_ci{
8418c2ecf20Sopenharmony_ci	u16 fid_index = mlxsw_sp_fid_index(fid);
8428c2ecf20Sopenharmony_ci	struct net_device *nve_dev;
8438c2ecf20Sopenharmony_ci	int nve_ifindex;
8448c2ecf20Sopenharmony_ci	__be32 vni;
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	mlxsw_sp_nve_flood_ip_flush(mlxsw_sp, fid);
8478c2ecf20Sopenharmony_ci	mlxsw_sp_nve_fdb_flush_by_fid(mlxsw_sp, fid_index);
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	if (WARN_ON(mlxsw_sp_fid_nve_ifindex(fid, &nve_ifindex) ||
8508c2ecf20Sopenharmony_ci		    mlxsw_sp_fid_vni(fid, &vni)))
8518c2ecf20Sopenharmony_ci		goto out;
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	nve_dev = dev_get_by_index(mlxsw_sp_net(mlxsw_sp), nve_ifindex);
8548c2ecf20Sopenharmony_ci	if (!nve_dev)
8558c2ecf20Sopenharmony_ci		goto out;
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	mlxsw_sp_nve_fdb_clear_offload(mlxsw_sp, fid, nve_dev, vni);
8588c2ecf20Sopenharmony_ci	mlxsw_sp_fid_fdb_clear_offload(fid, nve_dev);
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	dev_put(nve_dev);
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ciout:
8638c2ecf20Sopenharmony_ci	mlxsw_sp_fid_vni_clear(fid);
8648c2ecf20Sopenharmony_ci	mlxsw_sp_nve_tunnel_fini(mlxsw_sp);
8658c2ecf20Sopenharmony_ci}
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ciint mlxsw_sp_port_nve_init(struct mlxsw_sp_port *mlxsw_sp_port)
8688c2ecf20Sopenharmony_ci{
8698c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
8708c2ecf20Sopenharmony_ci	char tnqdr_pl[MLXSW_REG_TNQDR_LEN];
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci	mlxsw_reg_tnqdr_pack(tnqdr_pl, mlxsw_sp_port->local_port);
8738c2ecf20Sopenharmony_ci	return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(tnqdr), tnqdr_pl);
8748c2ecf20Sopenharmony_ci}
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_civoid mlxsw_sp_port_nve_fini(struct mlxsw_sp_port *mlxsw_sp_port)
8778c2ecf20Sopenharmony_ci{
8788c2ecf20Sopenharmony_ci}
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_cistatic int mlxsw_sp_nve_qos_init(struct mlxsw_sp *mlxsw_sp)
8818c2ecf20Sopenharmony_ci{
8828c2ecf20Sopenharmony_ci	char tnqcr_pl[MLXSW_REG_TNQCR_LEN];
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	mlxsw_reg_tnqcr_pack(tnqcr_pl);
8858c2ecf20Sopenharmony_ci	return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(tnqcr), tnqcr_pl);
8868c2ecf20Sopenharmony_ci}
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_cistatic int mlxsw_sp_nve_ecn_encap_init(struct mlxsw_sp *mlxsw_sp)
8898c2ecf20Sopenharmony_ci{
8908c2ecf20Sopenharmony_ci	int i;
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	/* Iterate over inner ECN values */
8938c2ecf20Sopenharmony_ci	for (i = INET_ECN_NOT_ECT; i <= INET_ECN_CE; i++) {
8948c2ecf20Sopenharmony_ci		u8 outer_ecn = INET_ECN_encapsulate(0, i);
8958c2ecf20Sopenharmony_ci		char tneem_pl[MLXSW_REG_TNEEM_LEN];
8968c2ecf20Sopenharmony_ci		int err;
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci		mlxsw_reg_tneem_pack(tneem_pl, i, outer_ecn);
8998c2ecf20Sopenharmony_ci		err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(tneem),
9008c2ecf20Sopenharmony_ci				      tneem_pl);
9018c2ecf20Sopenharmony_ci		if (err)
9028c2ecf20Sopenharmony_ci			return err;
9038c2ecf20Sopenharmony_ci	}
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	return 0;
9068c2ecf20Sopenharmony_ci}
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_cistatic int __mlxsw_sp_nve_ecn_decap_init(struct mlxsw_sp *mlxsw_sp,
9098c2ecf20Sopenharmony_ci					 u8 inner_ecn, u8 outer_ecn)
9108c2ecf20Sopenharmony_ci{
9118c2ecf20Sopenharmony_ci	char tndem_pl[MLXSW_REG_TNDEM_LEN];
9128c2ecf20Sopenharmony_ci	u8 new_inner_ecn;
9138c2ecf20Sopenharmony_ci	bool trap_en;
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	new_inner_ecn = mlxsw_sp_tunnel_ecn_decap(outer_ecn, inner_ecn,
9168c2ecf20Sopenharmony_ci						  &trap_en);
9178c2ecf20Sopenharmony_ci	mlxsw_reg_tndem_pack(tndem_pl, outer_ecn, inner_ecn, new_inner_ecn,
9188c2ecf20Sopenharmony_ci			     trap_en, trap_en ? MLXSW_TRAP_ID_DECAP_ECN0 : 0);
9198c2ecf20Sopenharmony_ci	return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(tndem), tndem_pl);
9208c2ecf20Sopenharmony_ci}
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_cistatic int mlxsw_sp_nve_ecn_decap_init(struct mlxsw_sp *mlxsw_sp)
9238c2ecf20Sopenharmony_ci{
9248c2ecf20Sopenharmony_ci	int i;
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci	/* Iterate over inner ECN values */
9278c2ecf20Sopenharmony_ci	for (i = INET_ECN_NOT_ECT; i <= INET_ECN_CE; i++) {
9288c2ecf20Sopenharmony_ci		int j;
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ci		/* Iterate over outer ECN values */
9318c2ecf20Sopenharmony_ci		for (j = INET_ECN_NOT_ECT; j <= INET_ECN_CE; j++) {
9328c2ecf20Sopenharmony_ci			int err;
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci			err = __mlxsw_sp_nve_ecn_decap_init(mlxsw_sp, i, j);
9358c2ecf20Sopenharmony_ci			if (err)
9368c2ecf20Sopenharmony_ci				return err;
9378c2ecf20Sopenharmony_ci		}
9388c2ecf20Sopenharmony_ci	}
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	return 0;
9418c2ecf20Sopenharmony_ci}
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_cistatic int mlxsw_sp_nve_ecn_init(struct mlxsw_sp *mlxsw_sp)
9448c2ecf20Sopenharmony_ci{
9458c2ecf20Sopenharmony_ci	int err;
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci	err = mlxsw_sp_nve_ecn_encap_init(mlxsw_sp);
9488c2ecf20Sopenharmony_ci	if (err)
9498c2ecf20Sopenharmony_ci		return err;
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	return mlxsw_sp_nve_ecn_decap_init(mlxsw_sp);
9528c2ecf20Sopenharmony_ci}
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_cistatic int mlxsw_sp_nve_resources_query(struct mlxsw_sp *mlxsw_sp)
9558c2ecf20Sopenharmony_ci{
9568c2ecf20Sopenharmony_ci	unsigned int max;
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_NVE_MC_ENTRIES_IPV4) ||
9598c2ecf20Sopenharmony_ci	    !MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_NVE_MC_ENTRIES_IPV6))
9608c2ecf20Sopenharmony_ci		return -EIO;
9618c2ecf20Sopenharmony_ci	max = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_NVE_MC_ENTRIES_IPV4);
9628c2ecf20Sopenharmony_ci	mlxsw_sp->nve->num_max_mc_entries[MLXSW_SP_L3_PROTO_IPV4] = max;
9638c2ecf20Sopenharmony_ci	max = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_NVE_MC_ENTRIES_IPV6);
9648c2ecf20Sopenharmony_ci	mlxsw_sp->nve->num_max_mc_entries[MLXSW_SP_L3_PROTO_IPV6] = max;
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci	return 0;
9678c2ecf20Sopenharmony_ci}
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ciint mlxsw_sp_nve_init(struct mlxsw_sp *mlxsw_sp)
9708c2ecf20Sopenharmony_ci{
9718c2ecf20Sopenharmony_ci	struct mlxsw_sp_nve *nve;
9728c2ecf20Sopenharmony_ci	int err;
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	nve = kzalloc(sizeof(*mlxsw_sp->nve), GFP_KERNEL);
9758c2ecf20Sopenharmony_ci	if (!nve)
9768c2ecf20Sopenharmony_ci		return -ENOMEM;
9778c2ecf20Sopenharmony_ci	mlxsw_sp->nve = nve;
9788c2ecf20Sopenharmony_ci	nve->mlxsw_sp = mlxsw_sp;
9798c2ecf20Sopenharmony_ci	nve->nve_ops_arr = mlxsw_sp->nve_ops_arr;
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci	err = rhashtable_init(&nve->mc_list_ht,
9828c2ecf20Sopenharmony_ci			      &mlxsw_sp_nve_mc_list_ht_params);
9838c2ecf20Sopenharmony_ci	if (err)
9848c2ecf20Sopenharmony_ci		goto err_rhashtable_init;
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	err = mlxsw_sp_nve_qos_init(mlxsw_sp);
9878c2ecf20Sopenharmony_ci	if (err)
9888c2ecf20Sopenharmony_ci		goto err_nve_qos_init;
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci	err = mlxsw_sp_nve_ecn_init(mlxsw_sp);
9918c2ecf20Sopenharmony_ci	if (err)
9928c2ecf20Sopenharmony_ci		goto err_nve_ecn_init;
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci	err = mlxsw_sp_nve_resources_query(mlxsw_sp);
9958c2ecf20Sopenharmony_ci	if (err)
9968c2ecf20Sopenharmony_ci		goto err_nve_resources_query;
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci	return 0;
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_cierr_nve_resources_query:
10018c2ecf20Sopenharmony_cierr_nve_ecn_init:
10028c2ecf20Sopenharmony_cierr_nve_qos_init:
10038c2ecf20Sopenharmony_ci	rhashtable_destroy(&nve->mc_list_ht);
10048c2ecf20Sopenharmony_cierr_rhashtable_init:
10058c2ecf20Sopenharmony_ci	mlxsw_sp->nve = NULL;
10068c2ecf20Sopenharmony_ci	kfree(nve);
10078c2ecf20Sopenharmony_ci	return err;
10088c2ecf20Sopenharmony_ci}
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_civoid mlxsw_sp_nve_fini(struct mlxsw_sp *mlxsw_sp)
10118c2ecf20Sopenharmony_ci{
10128c2ecf20Sopenharmony_ci	WARN_ON(mlxsw_sp->nve->num_nve_tunnels);
10138c2ecf20Sopenharmony_ci	rhashtable_destroy(&mlxsw_sp->nve->mc_list_ht);
10148c2ecf20Sopenharmony_ci	kfree(mlxsw_sp->nve);
10158c2ecf20Sopenharmony_ci	mlxsw_sp->nve = NULL;
10168c2ecf20Sopenharmony_ci}
1017