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/mutex.h>
58c2ecf20Sopenharmony_ci#include <linux/rhashtable.h>
68c2ecf20Sopenharmony_ci#include <net/ipv6.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include "spectrum_mr.h"
98c2ecf20Sopenharmony_ci#include "spectrum_router.h"
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cistruct mlxsw_sp_mr {
128c2ecf20Sopenharmony_ci	const struct mlxsw_sp_mr_ops *mr_ops;
138c2ecf20Sopenharmony_ci	void *catchall_route_priv;
148c2ecf20Sopenharmony_ci	struct delayed_work stats_update_dw;
158c2ecf20Sopenharmony_ci	struct list_head table_list;
168c2ecf20Sopenharmony_ci	struct mutex table_list_lock; /* Protects table_list */
178c2ecf20Sopenharmony_ci#define MLXSW_SP_MR_ROUTES_COUNTER_UPDATE_INTERVAL 5000 /* ms */
188c2ecf20Sopenharmony_ci	unsigned long priv[0];
198c2ecf20Sopenharmony_ci	/* priv has to be always the last item */
208c2ecf20Sopenharmony_ci};
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistruct mlxsw_sp_mr_vif;
238c2ecf20Sopenharmony_cistruct mlxsw_sp_mr_vif_ops {
248c2ecf20Sopenharmony_ci	bool (*is_regular)(const struct mlxsw_sp_mr_vif *vif);
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistruct mlxsw_sp_mr_vif {
288c2ecf20Sopenharmony_ci	struct net_device *dev;
298c2ecf20Sopenharmony_ci	const struct mlxsw_sp_rif *rif;
308c2ecf20Sopenharmony_ci	unsigned long vif_flags;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	/* A list of route_vif_entry structs that point to routes that the VIF
338c2ecf20Sopenharmony_ci	 * instance is used as one of the egress VIFs
348c2ecf20Sopenharmony_ci	 */
358c2ecf20Sopenharmony_ci	struct list_head route_evif_list;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	/* A list of route_vif_entry structs that point to routes that the VIF
388c2ecf20Sopenharmony_ci	 * instance is used as an ingress VIF
398c2ecf20Sopenharmony_ci	 */
408c2ecf20Sopenharmony_ci	struct list_head route_ivif_list;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	/* Protocol specific operations for a VIF */
438c2ecf20Sopenharmony_ci	const struct mlxsw_sp_mr_vif_ops *ops;
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistruct mlxsw_sp_mr_route_vif_entry {
478c2ecf20Sopenharmony_ci	struct list_head vif_node;
488c2ecf20Sopenharmony_ci	struct list_head route_node;
498c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_vif *mr_vif;
508c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route *mr_route;
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistruct mlxsw_sp_mr_table;
548c2ecf20Sopenharmony_cistruct mlxsw_sp_mr_table_ops {
558c2ecf20Sopenharmony_ci	bool (*is_route_valid)(const struct mlxsw_sp_mr_table *mr_table,
568c2ecf20Sopenharmony_ci			       const struct mr_mfc *mfc);
578c2ecf20Sopenharmony_ci	void (*key_create)(struct mlxsw_sp_mr_table *mr_table,
588c2ecf20Sopenharmony_ci			   struct mlxsw_sp_mr_route_key *key,
598c2ecf20Sopenharmony_ci			   struct mr_mfc *mfc);
608c2ecf20Sopenharmony_ci	bool (*is_route_starg)(const struct mlxsw_sp_mr_table *mr_table,
618c2ecf20Sopenharmony_ci			       const struct mlxsw_sp_mr_route *mr_route);
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistruct mlxsw_sp_mr_table {
658c2ecf20Sopenharmony_ci	struct list_head node;
668c2ecf20Sopenharmony_ci	enum mlxsw_sp_l3proto proto;
678c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp;
688c2ecf20Sopenharmony_ci	u32 vr_id;
698c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_vif vifs[MAXVIFS];
708c2ecf20Sopenharmony_ci	struct list_head route_list;
718c2ecf20Sopenharmony_ci	struct mutex route_list_lock; /* Protects route_list */
728c2ecf20Sopenharmony_ci	struct rhashtable route_ht;
738c2ecf20Sopenharmony_ci	const struct mlxsw_sp_mr_table_ops *ops;
748c2ecf20Sopenharmony_ci	char catchall_route_priv[];
758c2ecf20Sopenharmony_ci	/* catchall_route_priv has to be always the last item */
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistruct mlxsw_sp_mr_route {
798c2ecf20Sopenharmony_ci	struct list_head node;
808c2ecf20Sopenharmony_ci	struct rhash_head ht_node;
818c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route_key key;
828c2ecf20Sopenharmony_ci	enum mlxsw_sp_mr_route_action route_action;
838c2ecf20Sopenharmony_ci	u16 min_mtu;
848c2ecf20Sopenharmony_ci	struct mr_mfc *mfc;
858c2ecf20Sopenharmony_ci	void *route_priv;
868c2ecf20Sopenharmony_ci	const struct mlxsw_sp_mr_table *mr_table;
878c2ecf20Sopenharmony_ci	/* A list of route_vif_entry structs that point to the egress VIFs */
888c2ecf20Sopenharmony_ci	struct list_head evif_list;
898c2ecf20Sopenharmony_ci	/* A route_vif_entry struct that point to the ingress VIF */
908c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry ivif;
918c2ecf20Sopenharmony_ci};
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic const struct rhashtable_params mlxsw_sp_mr_route_ht_params = {
948c2ecf20Sopenharmony_ci	.key_len = sizeof(struct mlxsw_sp_mr_route_key),
958c2ecf20Sopenharmony_ci	.key_offset = offsetof(struct mlxsw_sp_mr_route, key),
968c2ecf20Sopenharmony_ci	.head_offset = offsetof(struct mlxsw_sp_mr_route, ht_node),
978c2ecf20Sopenharmony_ci	.automatic_shrinking = true,
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic bool mlxsw_sp_mr_vif_valid(const struct mlxsw_sp_mr_vif *vif)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	return vif->ops->is_regular(vif) && vif->dev && vif->rif;
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic bool mlxsw_sp_mr_vif_exists(const struct mlxsw_sp_mr_vif *vif)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	return vif->dev;
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic bool
1118c2ecf20Sopenharmony_cimlxsw_sp_mr_route_ivif_in_evifs(const struct mlxsw_sp_mr_route *mr_route)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	vifi_t ivif = mr_route->mfc->mfc_parent;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	return mr_route->mfc->mfc_un.res.ttls[ivif] != 255;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic int
1198c2ecf20Sopenharmony_cimlxsw_sp_mr_route_valid_evifs_num(const struct mlxsw_sp_mr_route *mr_route)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *rve;
1228c2ecf20Sopenharmony_ci	int valid_evifs;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	valid_evifs = 0;
1258c2ecf20Sopenharmony_ci	list_for_each_entry(rve, &mr_route->evif_list, route_node)
1268c2ecf20Sopenharmony_ci		if (mlxsw_sp_mr_vif_valid(rve->mr_vif))
1278c2ecf20Sopenharmony_ci			valid_evifs++;
1288c2ecf20Sopenharmony_ci	return valid_evifs;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic enum mlxsw_sp_mr_route_action
1328c2ecf20Sopenharmony_cimlxsw_sp_mr_route_action(const struct mlxsw_sp_mr_route *mr_route)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *rve;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	/* If the ingress port is not regular and resolved, trap the route */
1378c2ecf20Sopenharmony_ci	if (!mlxsw_sp_mr_vif_valid(mr_route->ivif.mr_vif))
1388c2ecf20Sopenharmony_ci		return MLXSW_SP_MR_ROUTE_ACTION_TRAP;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	/* The kernel does not match a (*,G) route that the ingress interface is
1418c2ecf20Sopenharmony_ci	 * not one of the egress interfaces, so trap these kind of routes.
1428c2ecf20Sopenharmony_ci	 */
1438c2ecf20Sopenharmony_ci	if (mr_route->mr_table->ops->is_route_starg(mr_route->mr_table,
1448c2ecf20Sopenharmony_ci						    mr_route) &&
1458c2ecf20Sopenharmony_ci	    !mlxsw_sp_mr_route_ivif_in_evifs(mr_route))
1468c2ecf20Sopenharmony_ci		return MLXSW_SP_MR_ROUTE_ACTION_TRAP;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	/* If the route has no valid eVIFs, trap it. */
1498c2ecf20Sopenharmony_ci	if (!mlxsw_sp_mr_route_valid_evifs_num(mr_route))
1508c2ecf20Sopenharmony_ci		return MLXSW_SP_MR_ROUTE_ACTION_TRAP;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	/* If one of the eVIFs has no RIF, trap-and-forward the route as there
1538c2ecf20Sopenharmony_ci	 * is some more routing to do in software too.
1548c2ecf20Sopenharmony_ci	 */
1558c2ecf20Sopenharmony_ci	list_for_each_entry(rve, &mr_route->evif_list, route_node)
1568c2ecf20Sopenharmony_ci		if (mlxsw_sp_mr_vif_exists(rve->mr_vif) && !rve->mr_vif->rif)
1578c2ecf20Sopenharmony_ci			return MLXSW_SP_MR_ROUTE_ACTION_TRAP_AND_FORWARD;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	return MLXSW_SP_MR_ROUTE_ACTION_FORWARD;
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic enum mlxsw_sp_mr_route_prio
1638c2ecf20Sopenharmony_cimlxsw_sp_mr_route_prio(const struct mlxsw_sp_mr_route *mr_route)
1648c2ecf20Sopenharmony_ci{
1658c2ecf20Sopenharmony_ci	return mr_route->mr_table->ops->is_route_starg(mr_route->mr_table,
1668c2ecf20Sopenharmony_ci						       mr_route) ?
1678c2ecf20Sopenharmony_ci		MLXSW_SP_MR_ROUTE_PRIO_STARG : MLXSW_SP_MR_ROUTE_PRIO_SG;
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic int mlxsw_sp_mr_route_evif_link(struct mlxsw_sp_mr_route *mr_route,
1718c2ecf20Sopenharmony_ci				       struct mlxsw_sp_mr_vif *mr_vif)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *rve;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	rve = kzalloc(sizeof(*rve), GFP_KERNEL);
1768c2ecf20Sopenharmony_ci	if (!rve)
1778c2ecf20Sopenharmony_ci		return -ENOMEM;
1788c2ecf20Sopenharmony_ci	rve->mr_route = mr_route;
1798c2ecf20Sopenharmony_ci	rve->mr_vif = mr_vif;
1808c2ecf20Sopenharmony_ci	list_add_tail(&rve->route_node, &mr_route->evif_list);
1818c2ecf20Sopenharmony_ci	list_add_tail(&rve->vif_node, &mr_vif->route_evif_list);
1828c2ecf20Sopenharmony_ci	return 0;
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic void
1868c2ecf20Sopenharmony_cimlxsw_sp_mr_route_evif_unlink(struct mlxsw_sp_mr_route_vif_entry *rve)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	list_del(&rve->route_node);
1898c2ecf20Sopenharmony_ci	list_del(&rve->vif_node);
1908c2ecf20Sopenharmony_ci	kfree(rve);
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic void mlxsw_sp_mr_route_ivif_link(struct mlxsw_sp_mr_route *mr_route,
1948c2ecf20Sopenharmony_ci					struct mlxsw_sp_mr_vif *mr_vif)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	mr_route->ivif.mr_route = mr_route;
1978c2ecf20Sopenharmony_ci	mr_route->ivif.mr_vif = mr_vif;
1988c2ecf20Sopenharmony_ci	list_add_tail(&mr_route->ivif.vif_node, &mr_vif->route_ivif_list);
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic void mlxsw_sp_mr_route_ivif_unlink(struct mlxsw_sp_mr_route *mr_route)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	list_del(&mr_route->ivif.vif_node);
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic int
2078c2ecf20Sopenharmony_cimlxsw_sp_mr_route_info_create(struct mlxsw_sp_mr_table *mr_table,
2088c2ecf20Sopenharmony_ci			      struct mlxsw_sp_mr_route *mr_route,
2098c2ecf20Sopenharmony_ci			      struct mlxsw_sp_mr_route_info *route_info)
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *rve;
2128c2ecf20Sopenharmony_ci	u16 *erif_indices;
2138c2ecf20Sopenharmony_ci	u16 irif_index;
2148c2ecf20Sopenharmony_ci	u16 erif = 0;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	erif_indices = kmalloc_array(MAXVIFS, sizeof(*erif_indices),
2178c2ecf20Sopenharmony_ci				     GFP_KERNEL);
2188c2ecf20Sopenharmony_ci	if (!erif_indices)
2198c2ecf20Sopenharmony_ci		return -ENOMEM;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	list_for_each_entry(rve, &mr_route->evif_list, route_node) {
2228c2ecf20Sopenharmony_ci		if (mlxsw_sp_mr_vif_valid(rve->mr_vif)) {
2238c2ecf20Sopenharmony_ci			u16 rifi = mlxsw_sp_rif_index(rve->mr_vif->rif);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci			erif_indices[erif++] = rifi;
2268c2ecf20Sopenharmony_ci		}
2278c2ecf20Sopenharmony_ci	}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	if (mlxsw_sp_mr_vif_valid(mr_route->ivif.mr_vif))
2308c2ecf20Sopenharmony_ci		irif_index = mlxsw_sp_rif_index(mr_route->ivif.mr_vif->rif);
2318c2ecf20Sopenharmony_ci	else
2328c2ecf20Sopenharmony_ci		irif_index = 0;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	route_info->irif_index = irif_index;
2358c2ecf20Sopenharmony_ci	route_info->erif_indices = erif_indices;
2368c2ecf20Sopenharmony_ci	route_info->min_mtu = mr_route->min_mtu;
2378c2ecf20Sopenharmony_ci	route_info->route_action = mr_route->route_action;
2388c2ecf20Sopenharmony_ci	route_info->erif_num = erif;
2398c2ecf20Sopenharmony_ci	return 0;
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic void
2438c2ecf20Sopenharmony_cimlxsw_sp_mr_route_info_destroy(struct mlxsw_sp_mr_route_info *route_info)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	kfree(route_info->erif_indices);
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic int mlxsw_sp_mr_route_write(struct mlxsw_sp_mr_table *mr_table,
2498c2ecf20Sopenharmony_ci				   struct mlxsw_sp_mr_route *mr_route,
2508c2ecf20Sopenharmony_ci				   bool replace)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
2538c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route_info route_info;
2548c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
2558c2ecf20Sopenharmony_ci	int err;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	err = mlxsw_sp_mr_route_info_create(mr_table, mr_route, &route_info);
2588c2ecf20Sopenharmony_ci	if (err)
2598c2ecf20Sopenharmony_ci		return err;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	if (!replace) {
2628c2ecf20Sopenharmony_ci		struct mlxsw_sp_mr_route_params route_params;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci		mr_route->route_priv = kzalloc(mr->mr_ops->route_priv_size,
2658c2ecf20Sopenharmony_ci					       GFP_KERNEL);
2668c2ecf20Sopenharmony_ci		if (!mr_route->route_priv) {
2678c2ecf20Sopenharmony_ci			err = -ENOMEM;
2688c2ecf20Sopenharmony_ci			goto out;
2698c2ecf20Sopenharmony_ci		}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci		route_params.key = mr_route->key;
2728c2ecf20Sopenharmony_ci		route_params.value = route_info;
2738c2ecf20Sopenharmony_ci		route_params.prio = mlxsw_sp_mr_route_prio(mr_route);
2748c2ecf20Sopenharmony_ci		err = mr->mr_ops->route_create(mlxsw_sp, mr->priv,
2758c2ecf20Sopenharmony_ci					       mr_route->route_priv,
2768c2ecf20Sopenharmony_ci					       &route_params);
2778c2ecf20Sopenharmony_ci		if (err)
2788c2ecf20Sopenharmony_ci			kfree(mr_route->route_priv);
2798c2ecf20Sopenharmony_ci	} else {
2808c2ecf20Sopenharmony_ci		err = mr->mr_ops->route_update(mlxsw_sp, mr_route->route_priv,
2818c2ecf20Sopenharmony_ci					       &route_info);
2828c2ecf20Sopenharmony_ci	}
2838c2ecf20Sopenharmony_ciout:
2848c2ecf20Sopenharmony_ci	mlxsw_sp_mr_route_info_destroy(&route_info);
2858c2ecf20Sopenharmony_ci	return err;
2868c2ecf20Sopenharmony_ci}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_cistatic void mlxsw_sp_mr_route_erase(struct mlxsw_sp_mr_table *mr_table,
2898c2ecf20Sopenharmony_ci				    struct mlxsw_sp_mr_route *mr_route)
2908c2ecf20Sopenharmony_ci{
2918c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
2928c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	mr->mr_ops->route_destroy(mlxsw_sp, mr->priv, mr_route->route_priv);
2958c2ecf20Sopenharmony_ci	kfree(mr_route->route_priv);
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_cistatic struct mlxsw_sp_mr_route *
2998c2ecf20Sopenharmony_cimlxsw_sp_mr_route_create(struct mlxsw_sp_mr_table *mr_table,
3008c2ecf20Sopenharmony_ci			 struct mr_mfc *mfc)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *rve, *tmp;
3038c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route *mr_route;
3048c2ecf20Sopenharmony_ci	int err = 0;
3058c2ecf20Sopenharmony_ci	int i;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	/* Allocate and init a new route and fill it with parameters */
3088c2ecf20Sopenharmony_ci	mr_route = kzalloc(sizeof(*mr_route), GFP_KERNEL);
3098c2ecf20Sopenharmony_ci	if (!mr_route)
3108c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
3118c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&mr_route->evif_list);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	/* Find min_mtu and link iVIF and eVIFs */
3148c2ecf20Sopenharmony_ci	mr_route->min_mtu = ETH_MAX_MTU;
3158c2ecf20Sopenharmony_ci	mr_cache_hold(mfc);
3168c2ecf20Sopenharmony_ci	mr_route->mfc = mfc;
3178c2ecf20Sopenharmony_ci	mr_table->ops->key_create(mr_table, &mr_route->key, mr_route->mfc);
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	mr_route->mr_table = mr_table;
3208c2ecf20Sopenharmony_ci	for (i = 0; i < MAXVIFS; i++) {
3218c2ecf20Sopenharmony_ci		if (mfc->mfc_un.res.ttls[i] != 255) {
3228c2ecf20Sopenharmony_ci			err = mlxsw_sp_mr_route_evif_link(mr_route,
3238c2ecf20Sopenharmony_ci							  &mr_table->vifs[i]);
3248c2ecf20Sopenharmony_ci			if (err)
3258c2ecf20Sopenharmony_ci				goto err;
3268c2ecf20Sopenharmony_ci			if (mr_table->vifs[i].dev &&
3278c2ecf20Sopenharmony_ci			    mr_table->vifs[i].dev->mtu < mr_route->min_mtu)
3288c2ecf20Sopenharmony_ci				mr_route->min_mtu = mr_table->vifs[i].dev->mtu;
3298c2ecf20Sopenharmony_ci		}
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci	mlxsw_sp_mr_route_ivif_link(mr_route,
3328c2ecf20Sopenharmony_ci				    &mr_table->vifs[mfc->mfc_parent]);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	mr_route->route_action = mlxsw_sp_mr_route_action(mr_route);
3358c2ecf20Sopenharmony_ci	return mr_route;
3368c2ecf20Sopenharmony_cierr:
3378c2ecf20Sopenharmony_ci	mr_cache_put(mfc);
3388c2ecf20Sopenharmony_ci	list_for_each_entry_safe(rve, tmp, &mr_route->evif_list, route_node)
3398c2ecf20Sopenharmony_ci		mlxsw_sp_mr_route_evif_unlink(rve);
3408c2ecf20Sopenharmony_ci	kfree(mr_route);
3418c2ecf20Sopenharmony_ci	return ERR_PTR(err);
3428c2ecf20Sopenharmony_ci}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_cistatic void mlxsw_sp_mr_route_destroy(struct mlxsw_sp_mr_table *mr_table,
3458c2ecf20Sopenharmony_ci				      struct mlxsw_sp_mr_route *mr_route)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *rve, *tmp;
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	mlxsw_sp_mr_route_ivif_unlink(mr_route);
3508c2ecf20Sopenharmony_ci	mr_cache_put(mr_route->mfc);
3518c2ecf20Sopenharmony_ci	list_for_each_entry_safe(rve, tmp, &mr_route->evif_list, route_node)
3528c2ecf20Sopenharmony_ci		mlxsw_sp_mr_route_evif_unlink(rve);
3538c2ecf20Sopenharmony_ci	kfree(mr_route);
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic void mlxsw_sp_mr_mfc_offload_set(struct mlxsw_sp_mr_route *mr_route,
3578c2ecf20Sopenharmony_ci					bool offload)
3588c2ecf20Sopenharmony_ci{
3598c2ecf20Sopenharmony_ci	if (offload)
3608c2ecf20Sopenharmony_ci		mr_route->mfc->mfc_flags |= MFC_OFFLOAD;
3618c2ecf20Sopenharmony_ci	else
3628c2ecf20Sopenharmony_ci		mr_route->mfc->mfc_flags &= ~MFC_OFFLOAD;
3638c2ecf20Sopenharmony_ci}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_cistatic void mlxsw_sp_mr_mfc_offload_update(struct mlxsw_sp_mr_route *mr_route)
3668c2ecf20Sopenharmony_ci{
3678c2ecf20Sopenharmony_ci	bool offload;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	offload = mr_route->route_action != MLXSW_SP_MR_ROUTE_ACTION_TRAP;
3708c2ecf20Sopenharmony_ci	mlxsw_sp_mr_mfc_offload_set(mr_route, offload);
3718c2ecf20Sopenharmony_ci}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_cistatic void __mlxsw_sp_mr_route_del(struct mlxsw_sp_mr_table *mr_table,
3748c2ecf20Sopenharmony_ci				    struct mlxsw_sp_mr_route *mr_route)
3758c2ecf20Sopenharmony_ci{
3768c2ecf20Sopenharmony_ci	WARN_ON_ONCE(!mutex_is_locked(&mr_table->route_list_lock));
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	mlxsw_sp_mr_mfc_offload_set(mr_route, false);
3798c2ecf20Sopenharmony_ci	rhashtable_remove_fast(&mr_table->route_ht, &mr_route->ht_node,
3808c2ecf20Sopenharmony_ci			       mlxsw_sp_mr_route_ht_params);
3818c2ecf20Sopenharmony_ci	list_del(&mr_route->node);
3828c2ecf20Sopenharmony_ci	mlxsw_sp_mr_route_erase(mr_table, mr_route);
3838c2ecf20Sopenharmony_ci	mlxsw_sp_mr_route_destroy(mr_table, mr_route);
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ciint mlxsw_sp_mr_route_add(struct mlxsw_sp_mr_table *mr_table,
3878c2ecf20Sopenharmony_ci			  struct mr_mfc *mfc, bool replace)
3888c2ecf20Sopenharmony_ci{
3898c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route *mr_orig_route = NULL;
3908c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route *mr_route;
3918c2ecf20Sopenharmony_ci	int err;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	if (!mr_table->ops->is_route_valid(mr_table, mfc))
3948c2ecf20Sopenharmony_ci		return -EINVAL;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	/* Create a new route */
3978c2ecf20Sopenharmony_ci	mr_route = mlxsw_sp_mr_route_create(mr_table, mfc);
3988c2ecf20Sopenharmony_ci	if (IS_ERR(mr_route))
3998c2ecf20Sopenharmony_ci		return PTR_ERR(mr_route);
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	/* Find any route with a matching key */
4028c2ecf20Sopenharmony_ci	mr_orig_route = rhashtable_lookup_fast(&mr_table->route_ht,
4038c2ecf20Sopenharmony_ci					       &mr_route->key,
4048c2ecf20Sopenharmony_ci					       mlxsw_sp_mr_route_ht_params);
4058c2ecf20Sopenharmony_ci	if (replace) {
4068c2ecf20Sopenharmony_ci		/* On replace case, make the route point to the new route_priv.
4078c2ecf20Sopenharmony_ci		 */
4088c2ecf20Sopenharmony_ci		if (WARN_ON(!mr_orig_route)) {
4098c2ecf20Sopenharmony_ci			err = -ENOENT;
4108c2ecf20Sopenharmony_ci			goto err_no_orig_route;
4118c2ecf20Sopenharmony_ci		}
4128c2ecf20Sopenharmony_ci		mr_route->route_priv = mr_orig_route->route_priv;
4138c2ecf20Sopenharmony_ci	} else if (mr_orig_route) {
4148c2ecf20Sopenharmony_ci		/* On non replace case, if another route with the same key was
4158c2ecf20Sopenharmony_ci		 * found, abort, as duplicate routes are used for proxy routes.
4168c2ecf20Sopenharmony_ci		 */
4178c2ecf20Sopenharmony_ci		dev_warn(mr_table->mlxsw_sp->bus_info->dev,
4188c2ecf20Sopenharmony_ci			 "Offloading proxy routes is not supported.\n");
4198c2ecf20Sopenharmony_ci		err = -EINVAL;
4208c2ecf20Sopenharmony_ci		goto err_duplicate_route;
4218c2ecf20Sopenharmony_ci	}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	/* Write the route to the hardware */
4248c2ecf20Sopenharmony_ci	err = mlxsw_sp_mr_route_write(mr_table, mr_route, replace);
4258c2ecf20Sopenharmony_ci	if (err)
4268c2ecf20Sopenharmony_ci		goto err_mr_route_write;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	/* Put it in the table data-structures */
4298c2ecf20Sopenharmony_ci	mutex_lock(&mr_table->route_list_lock);
4308c2ecf20Sopenharmony_ci	list_add_tail(&mr_route->node, &mr_table->route_list);
4318c2ecf20Sopenharmony_ci	mutex_unlock(&mr_table->route_list_lock);
4328c2ecf20Sopenharmony_ci	err = rhashtable_insert_fast(&mr_table->route_ht,
4338c2ecf20Sopenharmony_ci				     &mr_route->ht_node,
4348c2ecf20Sopenharmony_ci				     mlxsw_sp_mr_route_ht_params);
4358c2ecf20Sopenharmony_ci	if (err)
4368c2ecf20Sopenharmony_ci		goto err_rhashtable_insert;
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	/* Destroy the original route */
4398c2ecf20Sopenharmony_ci	if (replace) {
4408c2ecf20Sopenharmony_ci		rhashtable_remove_fast(&mr_table->route_ht,
4418c2ecf20Sopenharmony_ci				       &mr_orig_route->ht_node,
4428c2ecf20Sopenharmony_ci				       mlxsw_sp_mr_route_ht_params);
4438c2ecf20Sopenharmony_ci		list_del(&mr_orig_route->node);
4448c2ecf20Sopenharmony_ci		mlxsw_sp_mr_route_destroy(mr_table, mr_orig_route);
4458c2ecf20Sopenharmony_ci	}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	mlxsw_sp_mr_mfc_offload_update(mr_route);
4488c2ecf20Sopenharmony_ci	return 0;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_cierr_rhashtable_insert:
4518c2ecf20Sopenharmony_ci	mutex_lock(&mr_table->route_list_lock);
4528c2ecf20Sopenharmony_ci	list_del(&mr_route->node);
4538c2ecf20Sopenharmony_ci	mutex_unlock(&mr_table->route_list_lock);
4548c2ecf20Sopenharmony_ci	mlxsw_sp_mr_route_erase(mr_table, mr_route);
4558c2ecf20Sopenharmony_cierr_mr_route_write:
4568c2ecf20Sopenharmony_cierr_no_orig_route:
4578c2ecf20Sopenharmony_cierr_duplicate_route:
4588c2ecf20Sopenharmony_ci	mlxsw_sp_mr_route_destroy(mr_table, mr_route);
4598c2ecf20Sopenharmony_ci	return err;
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_civoid mlxsw_sp_mr_route_del(struct mlxsw_sp_mr_table *mr_table,
4638c2ecf20Sopenharmony_ci			   struct mr_mfc *mfc)
4648c2ecf20Sopenharmony_ci{
4658c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route *mr_route;
4668c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route_key key;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	mr_table->ops->key_create(mr_table, &key, mfc);
4698c2ecf20Sopenharmony_ci	mr_route = rhashtable_lookup_fast(&mr_table->route_ht, &key,
4708c2ecf20Sopenharmony_ci					  mlxsw_sp_mr_route_ht_params);
4718c2ecf20Sopenharmony_ci	if (mr_route) {
4728c2ecf20Sopenharmony_ci		mutex_lock(&mr_table->route_list_lock);
4738c2ecf20Sopenharmony_ci		__mlxsw_sp_mr_route_del(mr_table, mr_route);
4748c2ecf20Sopenharmony_ci		mutex_unlock(&mr_table->route_list_lock);
4758c2ecf20Sopenharmony_ci	}
4768c2ecf20Sopenharmony_ci}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci/* Should be called after the VIF struct is updated */
4798c2ecf20Sopenharmony_cistatic int
4808c2ecf20Sopenharmony_cimlxsw_sp_mr_route_ivif_resolve(struct mlxsw_sp_mr_table *mr_table,
4818c2ecf20Sopenharmony_ci			       struct mlxsw_sp_mr_route_vif_entry *rve)
4828c2ecf20Sopenharmony_ci{
4838c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
4848c2ecf20Sopenharmony_ci	enum mlxsw_sp_mr_route_action route_action;
4858c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
4868c2ecf20Sopenharmony_ci	u16 irif_index;
4878c2ecf20Sopenharmony_ci	int err;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	route_action = mlxsw_sp_mr_route_action(rve->mr_route);
4908c2ecf20Sopenharmony_ci	if (route_action == MLXSW_SP_MR_ROUTE_ACTION_TRAP)
4918c2ecf20Sopenharmony_ci		return 0;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	/* rve->mr_vif->rif is guaranteed to be valid at this stage */
4948c2ecf20Sopenharmony_ci	irif_index = mlxsw_sp_rif_index(rve->mr_vif->rif);
4958c2ecf20Sopenharmony_ci	err = mr->mr_ops->route_irif_update(mlxsw_sp, rve->mr_route->route_priv,
4968c2ecf20Sopenharmony_ci					    irif_index);
4978c2ecf20Sopenharmony_ci	if (err)
4988c2ecf20Sopenharmony_ci		return err;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	err = mr->mr_ops->route_action_update(mlxsw_sp,
5018c2ecf20Sopenharmony_ci					      rve->mr_route->route_priv,
5028c2ecf20Sopenharmony_ci					      route_action);
5038c2ecf20Sopenharmony_ci	if (err)
5048c2ecf20Sopenharmony_ci		/* No need to rollback here because the iRIF change only takes
5058c2ecf20Sopenharmony_ci		 * place after the action has been updated.
5068c2ecf20Sopenharmony_ci		 */
5078c2ecf20Sopenharmony_ci		return err;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	rve->mr_route->route_action = route_action;
5108c2ecf20Sopenharmony_ci	mlxsw_sp_mr_mfc_offload_update(rve->mr_route);
5118c2ecf20Sopenharmony_ci	return 0;
5128c2ecf20Sopenharmony_ci}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_cistatic void
5158c2ecf20Sopenharmony_cimlxsw_sp_mr_route_ivif_unresolve(struct mlxsw_sp_mr_table *mr_table,
5168c2ecf20Sopenharmony_ci				 struct mlxsw_sp_mr_route_vif_entry *rve)
5178c2ecf20Sopenharmony_ci{
5188c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
5198c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	mr->mr_ops->route_action_update(mlxsw_sp, rve->mr_route->route_priv,
5228c2ecf20Sopenharmony_ci					MLXSW_SP_MR_ROUTE_ACTION_TRAP);
5238c2ecf20Sopenharmony_ci	rve->mr_route->route_action = MLXSW_SP_MR_ROUTE_ACTION_TRAP;
5248c2ecf20Sopenharmony_ci	mlxsw_sp_mr_mfc_offload_update(rve->mr_route);
5258c2ecf20Sopenharmony_ci}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci/* Should be called after the RIF struct is updated */
5288c2ecf20Sopenharmony_cistatic int
5298c2ecf20Sopenharmony_cimlxsw_sp_mr_route_evif_resolve(struct mlxsw_sp_mr_table *mr_table,
5308c2ecf20Sopenharmony_ci			       struct mlxsw_sp_mr_route_vif_entry *rve)
5318c2ecf20Sopenharmony_ci{
5328c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
5338c2ecf20Sopenharmony_ci	enum mlxsw_sp_mr_route_action route_action;
5348c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
5358c2ecf20Sopenharmony_ci	u16 erif_index = 0;
5368c2ecf20Sopenharmony_ci	int err;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	/* Add the eRIF */
5398c2ecf20Sopenharmony_ci	if (mlxsw_sp_mr_vif_valid(rve->mr_vif)) {
5408c2ecf20Sopenharmony_ci		erif_index = mlxsw_sp_rif_index(rve->mr_vif->rif);
5418c2ecf20Sopenharmony_ci		err = mr->mr_ops->route_erif_add(mlxsw_sp,
5428c2ecf20Sopenharmony_ci						 rve->mr_route->route_priv,
5438c2ecf20Sopenharmony_ci						 erif_index);
5448c2ecf20Sopenharmony_ci		if (err)
5458c2ecf20Sopenharmony_ci			return err;
5468c2ecf20Sopenharmony_ci	}
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	/* Update the route action, as the new eVIF can be a tunnel or a pimreg
5498c2ecf20Sopenharmony_ci	 * device which will require updating the action.
5508c2ecf20Sopenharmony_ci	 */
5518c2ecf20Sopenharmony_ci	route_action = mlxsw_sp_mr_route_action(rve->mr_route);
5528c2ecf20Sopenharmony_ci	if (route_action != rve->mr_route->route_action) {
5538c2ecf20Sopenharmony_ci		err = mr->mr_ops->route_action_update(mlxsw_sp,
5548c2ecf20Sopenharmony_ci						      rve->mr_route->route_priv,
5558c2ecf20Sopenharmony_ci						      route_action);
5568c2ecf20Sopenharmony_ci		if (err)
5578c2ecf20Sopenharmony_ci			goto err_route_action_update;
5588c2ecf20Sopenharmony_ci	}
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	/* Update the minimum MTU */
5618c2ecf20Sopenharmony_ci	if (rve->mr_vif->dev->mtu < rve->mr_route->min_mtu) {
5628c2ecf20Sopenharmony_ci		rve->mr_route->min_mtu = rve->mr_vif->dev->mtu;
5638c2ecf20Sopenharmony_ci		err = mr->mr_ops->route_min_mtu_update(mlxsw_sp,
5648c2ecf20Sopenharmony_ci						       rve->mr_route->route_priv,
5658c2ecf20Sopenharmony_ci						       rve->mr_route->min_mtu);
5668c2ecf20Sopenharmony_ci		if (err)
5678c2ecf20Sopenharmony_ci			goto err_route_min_mtu_update;
5688c2ecf20Sopenharmony_ci	}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	rve->mr_route->route_action = route_action;
5718c2ecf20Sopenharmony_ci	mlxsw_sp_mr_mfc_offload_update(rve->mr_route);
5728c2ecf20Sopenharmony_ci	return 0;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_cierr_route_min_mtu_update:
5758c2ecf20Sopenharmony_ci	if (route_action != rve->mr_route->route_action)
5768c2ecf20Sopenharmony_ci		mr->mr_ops->route_action_update(mlxsw_sp,
5778c2ecf20Sopenharmony_ci						rve->mr_route->route_priv,
5788c2ecf20Sopenharmony_ci						rve->mr_route->route_action);
5798c2ecf20Sopenharmony_cierr_route_action_update:
5808c2ecf20Sopenharmony_ci	if (mlxsw_sp_mr_vif_valid(rve->mr_vif))
5818c2ecf20Sopenharmony_ci		mr->mr_ops->route_erif_del(mlxsw_sp, rve->mr_route->route_priv,
5828c2ecf20Sopenharmony_ci					   erif_index);
5838c2ecf20Sopenharmony_ci	return err;
5848c2ecf20Sopenharmony_ci}
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci/* Should be called before the RIF struct is updated */
5878c2ecf20Sopenharmony_cistatic void
5888c2ecf20Sopenharmony_cimlxsw_sp_mr_route_evif_unresolve(struct mlxsw_sp_mr_table *mr_table,
5898c2ecf20Sopenharmony_ci				 struct mlxsw_sp_mr_route_vif_entry *rve)
5908c2ecf20Sopenharmony_ci{
5918c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
5928c2ecf20Sopenharmony_ci	enum mlxsw_sp_mr_route_action route_action;
5938c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
5948c2ecf20Sopenharmony_ci	u16 rifi;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	/* If the unresolved RIF was not valid, no need to delete it */
5978c2ecf20Sopenharmony_ci	if (!mlxsw_sp_mr_vif_valid(rve->mr_vif))
5988c2ecf20Sopenharmony_ci		return;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	/* Update the route action: if there is only one valid eVIF in the
6018c2ecf20Sopenharmony_ci	 * route, set the action to trap as the VIF deletion will lead to zero
6028c2ecf20Sopenharmony_ci	 * valid eVIFs. On any other case, use the mlxsw_sp_mr_route_action to
6038c2ecf20Sopenharmony_ci	 * determine the route action.
6048c2ecf20Sopenharmony_ci	 */
6058c2ecf20Sopenharmony_ci	if (mlxsw_sp_mr_route_valid_evifs_num(rve->mr_route) == 1)
6068c2ecf20Sopenharmony_ci		route_action = MLXSW_SP_MR_ROUTE_ACTION_TRAP;
6078c2ecf20Sopenharmony_ci	else
6088c2ecf20Sopenharmony_ci		route_action = mlxsw_sp_mr_route_action(rve->mr_route);
6098c2ecf20Sopenharmony_ci	if (route_action != rve->mr_route->route_action)
6108c2ecf20Sopenharmony_ci		mr->mr_ops->route_action_update(mlxsw_sp,
6118c2ecf20Sopenharmony_ci						rve->mr_route->route_priv,
6128c2ecf20Sopenharmony_ci						route_action);
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	/* Delete the erif from the route */
6158c2ecf20Sopenharmony_ci	rifi = mlxsw_sp_rif_index(rve->mr_vif->rif);
6168c2ecf20Sopenharmony_ci	mr->mr_ops->route_erif_del(mlxsw_sp, rve->mr_route->route_priv, rifi);
6178c2ecf20Sopenharmony_ci	rve->mr_route->route_action = route_action;
6188c2ecf20Sopenharmony_ci	mlxsw_sp_mr_mfc_offload_update(rve->mr_route);
6198c2ecf20Sopenharmony_ci}
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_cistatic int mlxsw_sp_mr_vif_resolve(struct mlxsw_sp_mr_table *mr_table,
6228c2ecf20Sopenharmony_ci				   struct net_device *dev,
6238c2ecf20Sopenharmony_ci				   struct mlxsw_sp_mr_vif *mr_vif,
6248c2ecf20Sopenharmony_ci				   unsigned long vif_flags,
6258c2ecf20Sopenharmony_ci				   const struct mlxsw_sp_rif *rif)
6268c2ecf20Sopenharmony_ci{
6278c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *irve, *erve;
6288c2ecf20Sopenharmony_ci	int err;
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	/* Update the VIF */
6318c2ecf20Sopenharmony_ci	mr_vif->dev = dev;
6328c2ecf20Sopenharmony_ci	mr_vif->rif = rif;
6338c2ecf20Sopenharmony_ci	mr_vif->vif_flags = vif_flags;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	/* Update all routes where this VIF is used as an unresolved iRIF */
6368c2ecf20Sopenharmony_ci	list_for_each_entry(irve, &mr_vif->route_ivif_list, vif_node) {
6378c2ecf20Sopenharmony_ci		err = mlxsw_sp_mr_route_ivif_resolve(mr_table, irve);
6388c2ecf20Sopenharmony_ci		if (err)
6398c2ecf20Sopenharmony_ci			goto err_irif_unresolve;
6408c2ecf20Sopenharmony_ci	}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	/* Update all routes where this VIF is used as an unresolved eRIF */
6438c2ecf20Sopenharmony_ci	list_for_each_entry(erve, &mr_vif->route_evif_list, vif_node) {
6448c2ecf20Sopenharmony_ci		err = mlxsw_sp_mr_route_evif_resolve(mr_table, erve);
6458c2ecf20Sopenharmony_ci		if (err)
6468c2ecf20Sopenharmony_ci			goto err_erif_unresolve;
6478c2ecf20Sopenharmony_ci	}
6488c2ecf20Sopenharmony_ci	return 0;
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_cierr_erif_unresolve:
6518c2ecf20Sopenharmony_ci	list_for_each_entry_continue_reverse(erve, &mr_vif->route_evif_list,
6528c2ecf20Sopenharmony_ci					     vif_node)
6538c2ecf20Sopenharmony_ci		mlxsw_sp_mr_route_evif_unresolve(mr_table, erve);
6548c2ecf20Sopenharmony_cierr_irif_unresolve:
6558c2ecf20Sopenharmony_ci	list_for_each_entry_continue_reverse(irve, &mr_vif->route_ivif_list,
6568c2ecf20Sopenharmony_ci					     vif_node)
6578c2ecf20Sopenharmony_ci		mlxsw_sp_mr_route_ivif_unresolve(mr_table, irve);
6588c2ecf20Sopenharmony_ci	mr_vif->rif = NULL;
6598c2ecf20Sopenharmony_ci	return err;
6608c2ecf20Sopenharmony_ci}
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_cistatic void mlxsw_sp_mr_vif_unresolve(struct mlxsw_sp_mr_table *mr_table,
6638c2ecf20Sopenharmony_ci				      struct net_device *dev,
6648c2ecf20Sopenharmony_ci				      struct mlxsw_sp_mr_vif *mr_vif)
6658c2ecf20Sopenharmony_ci{
6668c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *rve;
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	/* Update all routes where this VIF is used as an unresolved eRIF */
6698c2ecf20Sopenharmony_ci	list_for_each_entry(rve, &mr_vif->route_evif_list, vif_node)
6708c2ecf20Sopenharmony_ci		mlxsw_sp_mr_route_evif_unresolve(mr_table, rve);
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	/* Update all routes where this VIF is used as an unresolved iRIF */
6738c2ecf20Sopenharmony_ci	list_for_each_entry(rve, &mr_vif->route_ivif_list, vif_node)
6748c2ecf20Sopenharmony_ci		mlxsw_sp_mr_route_ivif_unresolve(mr_table, rve);
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	/* Update the VIF */
6778c2ecf20Sopenharmony_ci	mr_vif->dev = dev;
6788c2ecf20Sopenharmony_ci	mr_vif->rif = NULL;
6798c2ecf20Sopenharmony_ci}
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ciint mlxsw_sp_mr_vif_add(struct mlxsw_sp_mr_table *mr_table,
6828c2ecf20Sopenharmony_ci			struct net_device *dev, vifi_t vif_index,
6838c2ecf20Sopenharmony_ci			unsigned long vif_flags, const struct mlxsw_sp_rif *rif)
6848c2ecf20Sopenharmony_ci{
6858c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_vif *mr_vif = &mr_table->vifs[vif_index];
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci	if (WARN_ON(vif_index >= MAXVIFS))
6888c2ecf20Sopenharmony_ci		return -EINVAL;
6898c2ecf20Sopenharmony_ci	if (mr_vif->dev)
6908c2ecf20Sopenharmony_ci		return -EEXIST;
6918c2ecf20Sopenharmony_ci	return mlxsw_sp_mr_vif_resolve(mr_table, dev, mr_vif, vif_flags, rif);
6928c2ecf20Sopenharmony_ci}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_civoid mlxsw_sp_mr_vif_del(struct mlxsw_sp_mr_table *mr_table, vifi_t vif_index)
6958c2ecf20Sopenharmony_ci{
6968c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_vif *mr_vif = &mr_table->vifs[vif_index];
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	if (WARN_ON(vif_index >= MAXVIFS))
6998c2ecf20Sopenharmony_ci		return;
7008c2ecf20Sopenharmony_ci	if (WARN_ON(!mr_vif->dev))
7018c2ecf20Sopenharmony_ci		return;
7028c2ecf20Sopenharmony_ci	mlxsw_sp_mr_vif_unresolve(mr_table, NULL, mr_vif);
7038c2ecf20Sopenharmony_ci}
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_cistatic struct mlxsw_sp_mr_vif *
7068c2ecf20Sopenharmony_cimlxsw_sp_mr_dev_vif_lookup(struct mlxsw_sp_mr_table *mr_table,
7078c2ecf20Sopenharmony_ci			   const struct net_device *dev)
7088c2ecf20Sopenharmony_ci{
7098c2ecf20Sopenharmony_ci	vifi_t vif_index;
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	for (vif_index = 0; vif_index < MAXVIFS; vif_index++)
7128c2ecf20Sopenharmony_ci		if (mr_table->vifs[vif_index].dev == dev)
7138c2ecf20Sopenharmony_ci			return &mr_table->vifs[vif_index];
7148c2ecf20Sopenharmony_ci	return NULL;
7158c2ecf20Sopenharmony_ci}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ciint mlxsw_sp_mr_rif_add(struct mlxsw_sp_mr_table *mr_table,
7188c2ecf20Sopenharmony_ci			const struct mlxsw_sp_rif *rif)
7198c2ecf20Sopenharmony_ci{
7208c2ecf20Sopenharmony_ci	const struct net_device *rif_dev = mlxsw_sp_rif_dev(rif);
7218c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_vif *mr_vif;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	if (!rif_dev)
7248c2ecf20Sopenharmony_ci		return 0;
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	mr_vif = mlxsw_sp_mr_dev_vif_lookup(mr_table, rif_dev);
7278c2ecf20Sopenharmony_ci	if (!mr_vif)
7288c2ecf20Sopenharmony_ci		return 0;
7298c2ecf20Sopenharmony_ci	return mlxsw_sp_mr_vif_resolve(mr_table, mr_vif->dev, mr_vif,
7308c2ecf20Sopenharmony_ci				       mr_vif->vif_flags, rif);
7318c2ecf20Sopenharmony_ci}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_civoid mlxsw_sp_mr_rif_del(struct mlxsw_sp_mr_table *mr_table,
7348c2ecf20Sopenharmony_ci			 const struct mlxsw_sp_rif *rif)
7358c2ecf20Sopenharmony_ci{
7368c2ecf20Sopenharmony_ci	const struct net_device *rif_dev = mlxsw_sp_rif_dev(rif);
7378c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_vif *mr_vif;
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	if (!rif_dev)
7408c2ecf20Sopenharmony_ci		return;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	mr_vif = mlxsw_sp_mr_dev_vif_lookup(mr_table, rif_dev);
7438c2ecf20Sopenharmony_ci	if (!mr_vif)
7448c2ecf20Sopenharmony_ci		return;
7458c2ecf20Sopenharmony_ci	mlxsw_sp_mr_vif_unresolve(mr_table, mr_vif->dev, mr_vif);
7468c2ecf20Sopenharmony_ci}
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_civoid mlxsw_sp_mr_rif_mtu_update(struct mlxsw_sp_mr_table *mr_table,
7498c2ecf20Sopenharmony_ci				const struct mlxsw_sp_rif *rif, int mtu)
7508c2ecf20Sopenharmony_ci{
7518c2ecf20Sopenharmony_ci	const struct net_device *rif_dev = mlxsw_sp_rif_dev(rif);
7528c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
7538c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *rve;
7548c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
7558c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_vif *mr_vif;
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	if (!rif_dev)
7588c2ecf20Sopenharmony_ci		return;
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	/* Search for a VIF that use that RIF */
7618c2ecf20Sopenharmony_ci	mr_vif = mlxsw_sp_mr_dev_vif_lookup(mr_table, rif_dev);
7628c2ecf20Sopenharmony_ci	if (!mr_vif)
7638c2ecf20Sopenharmony_ci		return;
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	/* Update all the routes that uses that VIF as eVIF */
7668c2ecf20Sopenharmony_ci	list_for_each_entry(rve, &mr_vif->route_evif_list, vif_node) {
7678c2ecf20Sopenharmony_ci		if (mtu < rve->mr_route->min_mtu) {
7688c2ecf20Sopenharmony_ci			rve->mr_route->min_mtu = mtu;
7698c2ecf20Sopenharmony_ci			mr->mr_ops->route_min_mtu_update(mlxsw_sp,
7708c2ecf20Sopenharmony_ci							 rve->mr_route->route_priv,
7718c2ecf20Sopenharmony_ci							 mtu);
7728c2ecf20Sopenharmony_ci		}
7738c2ecf20Sopenharmony_ci	}
7748c2ecf20Sopenharmony_ci}
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci/* Protocol specific functions */
7778c2ecf20Sopenharmony_cistatic bool
7788c2ecf20Sopenharmony_cimlxsw_sp_mr_route4_validate(const struct mlxsw_sp_mr_table *mr_table,
7798c2ecf20Sopenharmony_ci			    const struct mr_mfc *c)
7808c2ecf20Sopenharmony_ci{
7818c2ecf20Sopenharmony_ci	struct mfc_cache *mfc = (struct mfc_cache *) c;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	/* If the route is a (*,*) route, abort, as these kind of routes are
7848c2ecf20Sopenharmony_ci	 * used for proxy routes.
7858c2ecf20Sopenharmony_ci	 */
7868c2ecf20Sopenharmony_ci	if (mfc->mfc_origin == htonl(INADDR_ANY) &&
7878c2ecf20Sopenharmony_ci	    mfc->mfc_mcastgrp == htonl(INADDR_ANY)) {
7888c2ecf20Sopenharmony_ci		dev_warn(mr_table->mlxsw_sp->bus_info->dev,
7898c2ecf20Sopenharmony_ci			 "Offloading proxy routes is not supported.\n");
7908c2ecf20Sopenharmony_ci		return false;
7918c2ecf20Sopenharmony_ci	}
7928c2ecf20Sopenharmony_ci	return true;
7938c2ecf20Sopenharmony_ci}
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_cistatic void mlxsw_sp_mr_route4_key(struct mlxsw_sp_mr_table *mr_table,
7968c2ecf20Sopenharmony_ci				   struct mlxsw_sp_mr_route_key *key,
7978c2ecf20Sopenharmony_ci				   struct mr_mfc *c)
7988c2ecf20Sopenharmony_ci{
7998c2ecf20Sopenharmony_ci	const struct mfc_cache *mfc = (struct mfc_cache *) c;
8008c2ecf20Sopenharmony_ci	bool starg;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	starg = (mfc->mfc_origin == htonl(INADDR_ANY));
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	memset(key, 0, sizeof(*key));
8058c2ecf20Sopenharmony_ci	key->vrid = mr_table->vr_id;
8068c2ecf20Sopenharmony_ci	key->proto = MLXSW_SP_L3_PROTO_IPV4;
8078c2ecf20Sopenharmony_ci	key->group.addr4 = mfc->mfc_mcastgrp;
8088c2ecf20Sopenharmony_ci	key->group_mask.addr4 = htonl(0xffffffff);
8098c2ecf20Sopenharmony_ci	key->source.addr4 = mfc->mfc_origin;
8108c2ecf20Sopenharmony_ci	key->source_mask.addr4 = htonl(starg ? 0 : 0xffffffff);
8118c2ecf20Sopenharmony_ci}
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_cistatic bool mlxsw_sp_mr_route4_starg(const struct mlxsw_sp_mr_table *mr_table,
8148c2ecf20Sopenharmony_ci				     const struct mlxsw_sp_mr_route *mr_route)
8158c2ecf20Sopenharmony_ci{
8168c2ecf20Sopenharmony_ci	return mr_route->key.source_mask.addr4 == htonl(INADDR_ANY);
8178c2ecf20Sopenharmony_ci}
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_cistatic bool mlxsw_sp_mr_vif4_is_regular(const struct mlxsw_sp_mr_vif *vif)
8208c2ecf20Sopenharmony_ci{
8218c2ecf20Sopenharmony_ci	return !(vif->vif_flags & (VIFF_TUNNEL | VIFF_REGISTER));
8228c2ecf20Sopenharmony_ci}
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_cistatic bool
8258c2ecf20Sopenharmony_cimlxsw_sp_mr_route6_validate(const struct mlxsw_sp_mr_table *mr_table,
8268c2ecf20Sopenharmony_ci			    const struct mr_mfc *c)
8278c2ecf20Sopenharmony_ci{
8288c2ecf20Sopenharmony_ci	struct mfc6_cache *mfc = (struct mfc6_cache *) c;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	/* If the route is a (*,*) route, abort, as these kind of routes are
8318c2ecf20Sopenharmony_ci	 * used for proxy routes.
8328c2ecf20Sopenharmony_ci	 */
8338c2ecf20Sopenharmony_ci	if (ipv6_addr_any(&mfc->mf6c_origin) &&
8348c2ecf20Sopenharmony_ci	    ipv6_addr_any(&mfc->mf6c_mcastgrp)) {
8358c2ecf20Sopenharmony_ci		dev_warn(mr_table->mlxsw_sp->bus_info->dev,
8368c2ecf20Sopenharmony_ci			 "Offloading proxy routes is not supported.\n");
8378c2ecf20Sopenharmony_ci		return false;
8388c2ecf20Sopenharmony_ci	}
8398c2ecf20Sopenharmony_ci	return true;
8408c2ecf20Sopenharmony_ci}
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_cistatic void mlxsw_sp_mr_route6_key(struct mlxsw_sp_mr_table *mr_table,
8438c2ecf20Sopenharmony_ci				   struct mlxsw_sp_mr_route_key *key,
8448c2ecf20Sopenharmony_ci				   struct mr_mfc *c)
8458c2ecf20Sopenharmony_ci{
8468c2ecf20Sopenharmony_ci	const struct mfc6_cache *mfc = (struct mfc6_cache *) c;
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	memset(key, 0, sizeof(*key));
8498c2ecf20Sopenharmony_ci	key->vrid = mr_table->vr_id;
8508c2ecf20Sopenharmony_ci	key->proto = MLXSW_SP_L3_PROTO_IPV6;
8518c2ecf20Sopenharmony_ci	key->group.addr6 = mfc->mf6c_mcastgrp;
8528c2ecf20Sopenharmony_ci	memset(&key->group_mask.addr6, 0xff, sizeof(key->group_mask.addr6));
8538c2ecf20Sopenharmony_ci	key->source.addr6 = mfc->mf6c_origin;
8548c2ecf20Sopenharmony_ci	if (!ipv6_addr_any(&mfc->mf6c_origin))
8558c2ecf20Sopenharmony_ci		memset(&key->source_mask.addr6, 0xff,
8568c2ecf20Sopenharmony_ci		       sizeof(key->source_mask.addr6));
8578c2ecf20Sopenharmony_ci}
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_cistatic bool mlxsw_sp_mr_route6_starg(const struct mlxsw_sp_mr_table *mr_table,
8608c2ecf20Sopenharmony_ci				     const struct mlxsw_sp_mr_route *mr_route)
8618c2ecf20Sopenharmony_ci{
8628c2ecf20Sopenharmony_ci	return ipv6_addr_any(&mr_route->key.source_mask.addr6);
8638c2ecf20Sopenharmony_ci}
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_cistatic bool mlxsw_sp_mr_vif6_is_regular(const struct mlxsw_sp_mr_vif *vif)
8668c2ecf20Sopenharmony_ci{
8678c2ecf20Sopenharmony_ci	return !(vif->vif_flags & MIFF_REGISTER);
8688c2ecf20Sopenharmony_ci}
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_cistatic struct
8718c2ecf20Sopenharmony_cimlxsw_sp_mr_vif_ops mlxsw_sp_mr_vif_ops_arr[] = {
8728c2ecf20Sopenharmony_ci	{
8738c2ecf20Sopenharmony_ci		.is_regular = mlxsw_sp_mr_vif4_is_regular,
8748c2ecf20Sopenharmony_ci	},
8758c2ecf20Sopenharmony_ci	{
8768c2ecf20Sopenharmony_ci		.is_regular = mlxsw_sp_mr_vif6_is_regular,
8778c2ecf20Sopenharmony_ci	},
8788c2ecf20Sopenharmony_ci};
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_cistatic struct
8818c2ecf20Sopenharmony_cimlxsw_sp_mr_table_ops mlxsw_sp_mr_table_ops_arr[] = {
8828c2ecf20Sopenharmony_ci	{
8838c2ecf20Sopenharmony_ci		.is_route_valid = mlxsw_sp_mr_route4_validate,
8848c2ecf20Sopenharmony_ci		.key_create = mlxsw_sp_mr_route4_key,
8858c2ecf20Sopenharmony_ci		.is_route_starg = mlxsw_sp_mr_route4_starg,
8868c2ecf20Sopenharmony_ci	},
8878c2ecf20Sopenharmony_ci	{
8888c2ecf20Sopenharmony_ci		.is_route_valid = mlxsw_sp_mr_route6_validate,
8898c2ecf20Sopenharmony_ci		.key_create = mlxsw_sp_mr_route6_key,
8908c2ecf20Sopenharmony_ci		.is_route_starg = mlxsw_sp_mr_route6_starg,
8918c2ecf20Sopenharmony_ci	},
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci};
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_cistruct mlxsw_sp_mr_table *mlxsw_sp_mr_table_create(struct mlxsw_sp *mlxsw_sp,
8968c2ecf20Sopenharmony_ci						   u32 vr_id,
8978c2ecf20Sopenharmony_ci						   enum mlxsw_sp_l3proto proto)
8988c2ecf20Sopenharmony_ci{
8998c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route_params catchall_route_params = {
9008c2ecf20Sopenharmony_ci		.prio = MLXSW_SP_MR_ROUTE_PRIO_CATCHALL,
9018c2ecf20Sopenharmony_ci		.key = {
9028c2ecf20Sopenharmony_ci			.vrid = vr_id,
9038c2ecf20Sopenharmony_ci			.proto = proto,
9048c2ecf20Sopenharmony_ci		},
9058c2ecf20Sopenharmony_ci		.value = {
9068c2ecf20Sopenharmony_ci			.route_action = MLXSW_SP_MR_ROUTE_ACTION_TRAP,
9078c2ecf20Sopenharmony_ci		}
9088c2ecf20Sopenharmony_ci	};
9098c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
9108c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_table *mr_table;
9118c2ecf20Sopenharmony_ci	int err;
9128c2ecf20Sopenharmony_ci	int i;
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	mr_table = kzalloc(sizeof(*mr_table) + mr->mr_ops->route_priv_size,
9158c2ecf20Sopenharmony_ci			   GFP_KERNEL);
9168c2ecf20Sopenharmony_ci	if (!mr_table)
9178c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	mr_table->vr_id = vr_id;
9208c2ecf20Sopenharmony_ci	mr_table->mlxsw_sp = mlxsw_sp;
9218c2ecf20Sopenharmony_ci	mr_table->proto = proto;
9228c2ecf20Sopenharmony_ci	mr_table->ops = &mlxsw_sp_mr_table_ops_arr[proto];
9238c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&mr_table->route_list);
9248c2ecf20Sopenharmony_ci	mutex_init(&mr_table->route_list_lock);
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci	err = rhashtable_init(&mr_table->route_ht,
9278c2ecf20Sopenharmony_ci			      &mlxsw_sp_mr_route_ht_params);
9288c2ecf20Sopenharmony_ci	if (err)
9298c2ecf20Sopenharmony_ci		goto err_route_rhashtable_init;
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_ci	for (i = 0; i < MAXVIFS; i++) {
9328c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&mr_table->vifs[i].route_evif_list);
9338c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&mr_table->vifs[i].route_ivif_list);
9348c2ecf20Sopenharmony_ci		mr_table->vifs[i].ops = &mlxsw_sp_mr_vif_ops_arr[proto];
9358c2ecf20Sopenharmony_ci	}
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	err = mr->mr_ops->route_create(mlxsw_sp, mr->priv,
9388c2ecf20Sopenharmony_ci				       mr_table->catchall_route_priv,
9398c2ecf20Sopenharmony_ci				       &catchall_route_params);
9408c2ecf20Sopenharmony_ci	if (err)
9418c2ecf20Sopenharmony_ci		goto err_ops_route_create;
9428c2ecf20Sopenharmony_ci	mutex_lock(&mr->table_list_lock);
9438c2ecf20Sopenharmony_ci	list_add_tail(&mr_table->node, &mr->table_list);
9448c2ecf20Sopenharmony_ci	mutex_unlock(&mr->table_list_lock);
9458c2ecf20Sopenharmony_ci	return mr_table;
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_cierr_ops_route_create:
9488c2ecf20Sopenharmony_ci	rhashtable_destroy(&mr_table->route_ht);
9498c2ecf20Sopenharmony_cierr_route_rhashtable_init:
9508c2ecf20Sopenharmony_ci	mutex_destroy(&mr_table->route_list_lock);
9518c2ecf20Sopenharmony_ci	kfree(mr_table);
9528c2ecf20Sopenharmony_ci	return ERR_PTR(err);
9538c2ecf20Sopenharmony_ci}
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_civoid mlxsw_sp_mr_table_destroy(struct mlxsw_sp_mr_table *mr_table)
9568c2ecf20Sopenharmony_ci{
9578c2ecf20Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
9588c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci	WARN_ON(!mlxsw_sp_mr_table_empty(mr_table));
9618c2ecf20Sopenharmony_ci	mutex_lock(&mr->table_list_lock);
9628c2ecf20Sopenharmony_ci	list_del(&mr_table->node);
9638c2ecf20Sopenharmony_ci	mutex_unlock(&mr->table_list_lock);
9648c2ecf20Sopenharmony_ci	mr->mr_ops->route_destroy(mlxsw_sp, mr->priv,
9658c2ecf20Sopenharmony_ci				  &mr_table->catchall_route_priv);
9668c2ecf20Sopenharmony_ci	rhashtable_destroy(&mr_table->route_ht);
9678c2ecf20Sopenharmony_ci	mutex_destroy(&mr_table->route_list_lock);
9688c2ecf20Sopenharmony_ci	kfree(mr_table);
9698c2ecf20Sopenharmony_ci}
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_civoid mlxsw_sp_mr_table_flush(struct mlxsw_sp_mr_table *mr_table)
9728c2ecf20Sopenharmony_ci{
9738c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route *mr_route, *tmp;
9748c2ecf20Sopenharmony_ci	int i;
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci	mutex_lock(&mr_table->route_list_lock);
9778c2ecf20Sopenharmony_ci	list_for_each_entry_safe(mr_route, tmp, &mr_table->route_list, node)
9788c2ecf20Sopenharmony_ci		__mlxsw_sp_mr_route_del(mr_table, mr_route);
9798c2ecf20Sopenharmony_ci	mutex_unlock(&mr_table->route_list_lock);
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci	for (i = 0; i < MAXVIFS; i++) {
9828c2ecf20Sopenharmony_ci		mr_table->vifs[i].dev = NULL;
9838c2ecf20Sopenharmony_ci		mr_table->vifs[i].rif = NULL;
9848c2ecf20Sopenharmony_ci	}
9858c2ecf20Sopenharmony_ci}
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_cibool mlxsw_sp_mr_table_empty(const struct mlxsw_sp_mr_table *mr_table)
9888c2ecf20Sopenharmony_ci{
9898c2ecf20Sopenharmony_ci	int i;
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci	for (i = 0; i < MAXVIFS; i++)
9928c2ecf20Sopenharmony_ci		if (mr_table->vifs[i].dev)
9938c2ecf20Sopenharmony_ci			return false;
9948c2ecf20Sopenharmony_ci	return list_empty(&mr_table->route_list);
9958c2ecf20Sopenharmony_ci}
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_cistatic void mlxsw_sp_mr_route_stats_update(struct mlxsw_sp *mlxsw_sp,
9988c2ecf20Sopenharmony_ci					   struct mlxsw_sp_mr_route *mr_route)
9998c2ecf20Sopenharmony_ci{
10008c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
10018c2ecf20Sopenharmony_ci	u64 packets, bytes;
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	if (mr_route->route_action == MLXSW_SP_MR_ROUTE_ACTION_TRAP)
10048c2ecf20Sopenharmony_ci		return;
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci	mr->mr_ops->route_stats(mlxsw_sp, mr_route->route_priv, &packets,
10078c2ecf20Sopenharmony_ci				&bytes);
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci	if (mr_route->mfc->mfc_un.res.pkt != packets)
10108c2ecf20Sopenharmony_ci		mr_route->mfc->mfc_un.res.lastuse = jiffies;
10118c2ecf20Sopenharmony_ci	mr_route->mfc->mfc_un.res.pkt = packets;
10128c2ecf20Sopenharmony_ci	mr_route->mfc->mfc_un.res.bytes = bytes;
10138c2ecf20Sopenharmony_ci}
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_cistatic void mlxsw_sp_mr_stats_update(struct work_struct *work)
10168c2ecf20Sopenharmony_ci{
10178c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr *mr = container_of(work, struct mlxsw_sp_mr,
10188c2ecf20Sopenharmony_ci					      stats_update_dw.work);
10198c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_table *mr_table;
10208c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr_route *mr_route;
10218c2ecf20Sopenharmony_ci	unsigned long interval;
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	mutex_lock(&mr->table_list_lock);
10248c2ecf20Sopenharmony_ci	list_for_each_entry(mr_table, &mr->table_list, node) {
10258c2ecf20Sopenharmony_ci		mutex_lock(&mr_table->route_list_lock);
10268c2ecf20Sopenharmony_ci		list_for_each_entry(mr_route, &mr_table->route_list, node)
10278c2ecf20Sopenharmony_ci			mlxsw_sp_mr_route_stats_update(mr_table->mlxsw_sp,
10288c2ecf20Sopenharmony_ci						       mr_route);
10298c2ecf20Sopenharmony_ci		mutex_unlock(&mr_table->route_list_lock);
10308c2ecf20Sopenharmony_ci	}
10318c2ecf20Sopenharmony_ci	mutex_unlock(&mr->table_list_lock);
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci	interval = msecs_to_jiffies(MLXSW_SP_MR_ROUTES_COUNTER_UPDATE_INTERVAL);
10348c2ecf20Sopenharmony_ci	mlxsw_core_schedule_dw(&mr->stats_update_dw, interval);
10358c2ecf20Sopenharmony_ci}
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ciint mlxsw_sp_mr_init(struct mlxsw_sp *mlxsw_sp,
10388c2ecf20Sopenharmony_ci		     const struct mlxsw_sp_mr_ops *mr_ops)
10398c2ecf20Sopenharmony_ci{
10408c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr *mr;
10418c2ecf20Sopenharmony_ci	unsigned long interval;
10428c2ecf20Sopenharmony_ci	int err;
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci	mr = kzalloc(sizeof(*mr) + mr_ops->priv_size, GFP_KERNEL);
10458c2ecf20Sopenharmony_ci	if (!mr)
10468c2ecf20Sopenharmony_ci		return -ENOMEM;
10478c2ecf20Sopenharmony_ci	mr->mr_ops = mr_ops;
10488c2ecf20Sopenharmony_ci	mlxsw_sp->mr = mr;
10498c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&mr->table_list);
10508c2ecf20Sopenharmony_ci	mutex_init(&mr->table_list_lock);
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci	err = mr_ops->init(mlxsw_sp, mr->priv);
10538c2ecf20Sopenharmony_ci	if (err)
10548c2ecf20Sopenharmony_ci		goto err;
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci	/* Create the delayed work for counter updates */
10578c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&mr->stats_update_dw, mlxsw_sp_mr_stats_update);
10588c2ecf20Sopenharmony_ci	interval = msecs_to_jiffies(MLXSW_SP_MR_ROUTES_COUNTER_UPDATE_INTERVAL);
10598c2ecf20Sopenharmony_ci	mlxsw_core_schedule_dw(&mr->stats_update_dw, interval);
10608c2ecf20Sopenharmony_ci	return 0;
10618c2ecf20Sopenharmony_cierr:
10628c2ecf20Sopenharmony_ci	mutex_destroy(&mr->table_list_lock);
10638c2ecf20Sopenharmony_ci	kfree(mr);
10648c2ecf20Sopenharmony_ci	return err;
10658c2ecf20Sopenharmony_ci}
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_civoid mlxsw_sp_mr_fini(struct mlxsw_sp *mlxsw_sp)
10688c2ecf20Sopenharmony_ci{
10698c2ecf20Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&mr->stats_update_dw);
10728c2ecf20Sopenharmony_ci	mr->mr_ops->fini(mlxsw_sp, mr->priv);
10738c2ecf20Sopenharmony_ci	mutex_destroy(&mr->table_list_lock);
10748c2ecf20Sopenharmony_ci	kfree(mr);
10758c2ecf20Sopenharmony_ci}
1076