162306a36Sopenharmony_ci// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
262306a36Sopenharmony_ci/* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#include <linux/mutex.h>
562306a36Sopenharmony_ci#include <linux/rhashtable.h>
662306a36Sopenharmony_ci#include <net/ipv6.h>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include "spectrum_mr.h"
962306a36Sopenharmony_ci#include "spectrum_router.h"
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_cistruct mlxsw_sp_mr {
1262306a36Sopenharmony_ci	const struct mlxsw_sp_mr_ops *mr_ops;
1362306a36Sopenharmony_ci	void *catchall_route_priv;
1462306a36Sopenharmony_ci	struct delayed_work stats_update_dw;
1562306a36Sopenharmony_ci	struct list_head table_list;
1662306a36Sopenharmony_ci	struct mutex table_list_lock; /* Protects table_list */
1762306a36Sopenharmony_ci#define MLXSW_SP_MR_ROUTES_COUNTER_UPDATE_INTERVAL 5000 /* ms */
1862306a36Sopenharmony_ci	unsigned long priv[];
1962306a36Sopenharmony_ci	/* priv has to be always the last item */
2062306a36Sopenharmony_ci};
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cistruct mlxsw_sp_mr_vif;
2362306a36Sopenharmony_cistruct mlxsw_sp_mr_vif_ops {
2462306a36Sopenharmony_ci	bool (*is_regular)(const struct mlxsw_sp_mr_vif *vif);
2562306a36Sopenharmony_ci};
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_cistruct mlxsw_sp_mr_vif {
2862306a36Sopenharmony_ci	struct net_device *dev;
2962306a36Sopenharmony_ci	const struct mlxsw_sp_rif *rif;
3062306a36Sopenharmony_ci	unsigned long vif_flags;
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci	/* A list of route_vif_entry structs that point to routes that the VIF
3362306a36Sopenharmony_ci	 * instance is used as one of the egress VIFs
3462306a36Sopenharmony_ci	 */
3562306a36Sopenharmony_ci	struct list_head route_evif_list;
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	/* A list of route_vif_entry structs that point to routes that the VIF
3862306a36Sopenharmony_ci	 * instance is used as an ingress VIF
3962306a36Sopenharmony_ci	 */
4062306a36Sopenharmony_ci	struct list_head route_ivif_list;
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci	/* Protocol specific operations for a VIF */
4362306a36Sopenharmony_ci	const struct mlxsw_sp_mr_vif_ops *ops;
4462306a36Sopenharmony_ci};
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistruct mlxsw_sp_mr_route_vif_entry {
4762306a36Sopenharmony_ci	struct list_head vif_node;
4862306a36Sopenharmony_ci	struct list_head route_node;
4962306a36Sopenharmony_ci	struct mlxsw_sp_mr_vif *mr_vif;
5062306a36Sopenharmony_ci	struct mlxsw_sp_mr_route *mr_route;
5162306a36Sopenharmony_ci};
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cistruct mlxsw_sp_mr_table;
5462306a36Sopenharmony_cistruct mlxsw_sp_mr_table_ops {
5562306a36Sopenharmony_ci	bool (*is_route_valid)(const struct mlxsw_sp_mr_table *mr_table,
5662306a36Sopenharmony_ci			       const struct mr_mfc *mfc);
5762306a36Sopenharmony_ci	void (*key_create)(struct mlxsw_sp_mr_table *mr_table,
5862306a36Sopenharmony_ci			   struct mlxsw_sp_mr_route_key *key,
5962306a36Sopenharmony_ci			   struct mr_mfc *mfc);
6062306a36Sopenharmony_ci	bool (*is_route_starg)(const struct mlxsw_sp_mr_table *mr_table,
6162306a36Sopenharmony_ci			       const struct mlxsw_sp_mr_route *mr_route);
6262306a36Sopenharmony_ci};
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_cistruct mlxsw_sp_mr_table {
6562306a36Sopenharmony_ci	struct list_head node;
6662306a36Sopenharmony_ci	enum mlxsw_sp_l3proto proto;
6762306a36Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp;
6862306a36Sopenharmony_ci	u32 vr_id;
6962306a36Sopenharmony_ci	struct mlxsw_sp_mr_vif vifs[MAXVIFS];
7062306a36Sopenharmony_ci	struct list_head route_list;
7162306a36Sopenharmony_ci	struct mutex route_list_lock; /* Protects route_list */
7262306a36Sopenharmony_ci	struct rhashtable route_ht;
7362306a36Sopenharmony_ci	const struct mlxsw_sp_mr_table_ops *ops;
7462306a36Sopenharmony_ci	char catchall_route_priv[];
7562306a36Sopenharmony_ci	/* catchall_route_priv has to be always the last item */
7662306a36Sopenharmony_ci};
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_cistruct mlxsw_sp_mr_route {
7962306a36Sopenharmony_ci	struct list_head node;
8062306a36Sopenharmony_ci	struct rhash_head ht_node;
8162306a36Sopenharmony_ci	struct mlxsw_sp_mr_route_key key;
8262306a36Sopenharmony_ci	enum mlxsw_sp_mr_route_action route_action;
8362306a36Sopenharmony_ci	u16 min_mtu;
8462306a36Sopenharmony_ci	struct mr_mfc *mfc;
8562306a36Sopenharmony_ci	void *route_priv;
8662306a36Sopenharmony_ci	const struct mlxsw_sp_mr_table *mr_table;
8762306a36Sopenharmony_ci	/* A list of route_vif_entry structs that point to the egress VIFs */
8862306a36Sopenharmony_ci	struct list_head evif_list;
8962306a36Sopenharmony_ci	/* A route_vif_entry struct that point to the ingress VIF */
9062306a36Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry ivif;
9162306a36Sopenharmony_ci};
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_cistatic const struct rhashtable_params mlxsw_sp_mr_route_ht_params = {
9462306a36Sopenharmony_ci	.key_len = sizeof(struct mlxsw_sp_mr_route_key),
9562306a36Sopenharmony_ci	.key_offset = offsetof(struct mlxsw_sp_mr_route, key),
9662306a36Sopenharmony_ci	.head_offset = offsetof(struct mlxsw_sp_mr_route, ht_node),
9762306a36Sopenharmony_ci	.automatic_shrinking = true,
9862306a36Sopenharmony_ci};
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_cistatic bool mlxsw_sp_mr_vif_valid(const struct mlxsw_sp_mr_vif *vif)
10162306a36Sopenharmony_ci{
10262306a36Sopenharmony_ci	return vif->ops->is_regular(vif) && vif->dev && vif->rif;
10362306a36Sopenharmony_ci}
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_cistatic bool mlxsw_sp_mr_vif_exists(const struct mlxsw_sp_mr_vif *vif)
10662306a36Sopenharmony_ci{
10762306a36Sopenharmony_ci	return vif->dev;
10862306a36Sopenharmony_ci}
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_cistatic bool
11162306a36Sopenharmony_cimlxsw_sp_mr_route_ivif_in_evifs(const struct mlxsw_sp_mr_route *mr_route)
11262306a36Sopenharmony_ci{
11362306a36Sopenharmony_ci	vifi_t ivif = mr_route->mfc->mfc_parent;
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	return mr_route->mfc->mfc_un.res.ttls[ivif] != 255;
11662306a36Sopenharmony_ci}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistatic int
11962306a36Sopenharmony_cimlxsw_sp_mr_route_valid_evifs_num(const struct mlxsw_sp_mr_route *mr_route)
12062306a36Sopenharmony_ci{
12162306a36Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *rve;
12262306a36Sopenharmony_ci	int valid_evifs;
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	valid_evifs = 0;
12562306a36Sopenharmony_ci	list_for_each_entry(rve, &mr_route->evif_list, route_node)
12662306a36Sopenharmony_ci		if (mlxsw_sp_mr_vif_valid(rve->mr_vif))
12762306a36Sopenharmony_ci			valid_evifs++;
12862306a36Sopenharmony_ci	return valid_evifs;
12962306a36Sopenharmony_ci}
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_cistatic enum mlxsw_sp_mr_route_action
13262306a36Sopenharmony_cimlxsw_sp_mr_route_action(const struct mlxsw_sp_mr_route *mr_route)
13362306a36Sopenharmony_ci{
13462306a36Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *rve;
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	/* If the ingress port is not regular and resolved, trap the route */
13762306a36Sopenharmony_ci	if (!mlxsw_sp_mr_vif_valid(mr_route->ivif.mr_vif))
13862306a36Sopenharmony_ci		return MLXSW_SP_MR_ROUTE_ACTION_TRAP;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	/* The kernel does not match a (*,G) route that the ingress interface is
14162306a36Sopenharmony_ci	 * not one of the egress interfaces, so trap these kind of routes.
14262306a36Sopenharmony_ci	 */
14362306a36Sopenharmony_ci	if (mr_route->mr_table->ops->is_route_starg(mr_route->mr_table,
14462306a36Sopenharmony_ci						    mr_route) &&
14562306a36Sopenharmony_ci	    !mlxsw_sp_mr_route_ivif_in_evifs(mr_route))
14662306a36Sopenharmony_ci		return MLXSW_SP_MR_ROUTE_ACTION_TRAP;
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	/* If the route has no valid eVIFs, trap it. */
14962306a36Sopenharmony_ci	if (!mlxsw_sp_mr_route_valid_evifs_num(mr_route))
15062306a36Sopenharmony_ci		return MLXSW_SP_MR_ROUTE_ACTION_TRAP;
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	/* If one of the eVIFs has no RIF, trap-and-forward the route as there
15362306a36Sopenharmony_ci	 * is some more routing to do in software too.
15462306a36Sopenharmony_ci	 */
15562306a36Sopenharmony_ci	list_for_each_entry(rve, &mr_route->evif_list, route_node)
15662306a36Sopenharmony_ci		if (mlxsw_sp_mr_vif_exists(rve->mr_vif) && !rve->mr_vif->rif)
15762306a36Sopenharmony_ci			return MLXSW_SP_MR_ROUTE_ACTION_TRAP_AND_FORWARD;
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	return MLXSW_SP_MR_ROUTE_ACTION_FORWARD;
16062306a36Sopenharmony_ci}
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_cistatic enum mlxsw_sp_mr_route_prio
16362306a36Sopenharmony_cimlxsw_sp_mr_route_prio(const struct mlxsw_sp_mr_route *mr_route)
16462306a36Sopenharmony_ci{
16562306a36Sopenharmony_ci	return mr_route->mr_table->ops->is_route_starg(mr_route->mr_table,
16662306a36Sopenharmony_ci						       mr_route) ?
16762306a36Sopenharmony_ci		MLXSW_SP_MR_ROUTE_PRIO_STARG : MLXSW_SP_MR_ROUTE_PRIO_SG;
16862306a36Sopenharmony_ci}
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_cistatic int mlxsw_sp_mr_route_evif_link(struct mlxsw_sp_mr_route *mr_route,
17162306a36Sopenharmony_ci				       struct mlxsw_sp_mr_vif *mr_vif)
17262306a36Sopenharmony_ci{
17362306a36Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *rve;
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	rve = kzalloc(sizeof(*rve), GFP_KERNEL);
17662306a36Sopenharmony_ci	if (!rve)
17762306a36Sopenharmony_ci		return -ENOMEM;
17862306a36Sopenharmony_ci	rve->mr_route = mr_route;
17962306a36Sopenharmony_ci	rve->mr_vif = mr_vif;
18062306a36Sopenharmony_ci	list_add_tail(&rve->route_node, &mr_route->evif_list);
18162306a36Sopenharmony_ci	list_add_tail(&rve->vif_node, &mr_vif->route_evif_list);
18262306a36Sopenharmony_ci	return 0;
18362306a36Sopenharmony_ci}
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_cistatic void
18662306a36Sopenharmony_cimlxsw_sp_mr_route_evif_unlink(struct mlxsw_sp_mr_route_vif_entry *rve)
18762306a36Sopenharmony_ci{
18862306a36Sopenharmony_ci	list_del(&rve->route_node);
18962306a36Sopenharmony_ci	list_del(&rve->vif_node);
19062306a36Sopenharmony_ci	kfree(rve);
19162306a36Sopenharmony_ci}
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_cistatic void mlxsw_sp_mr_route_ivif_link(struct mlxsw_sp_mr_route *mr_route,
19462306a36Sopenharmony_ci					struct mlxsw_sp_mr_vif *mr_vif)
19562306a36Sopenharmony_ci{
19662306a36Sopenharmony_ci	mr_route->ivif.mr_route = mr_route;
19762306a36Sopenharmony_ci	mr_route->ivif.mr_vif = mr_vif;
19862306a36Sopenharmony_ci	list_add_tail(&mr_route->ivif.vif_node, &mr_vif->route_ivif_list);
19962306a36Sopenharmony_ci}
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_cistatic void mlxsw_sp_mr_route_ivif_unlink(struct mlxsw_sp_mr_route *mr_route)
20262306a36Sopenharmony_ci{
20362306a36Sopenharmony_ci	list_del(&mr_route->ivif.vif_node);
20462306a36Sopenharmony_ci}
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_cistatic int
20762306a36Sopenharmony_cimlxsw_sp_mr_route_info_create(struct mlxsw_sp_mr_table *mr_table,
20862306a36Sopenharmony_ci			      struct mlxsw_sp_mr_route *mr_route,
20962306a36Sopenharmony_ci			      struct mlxsw_sp_mr_route_info *route_info)
21062306a36Sopenharmony_ci{
21162306a36Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *rve;
21262306a36Sopenharmony_ci	u16 *erif_indices;
21362306a36Sopenharmony_ci	u16 irif_index;
21462306a36Sopenharmony_ci	u16 erif = 0;
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	erif_indices = kmalloc_array(MAXVIFS, sizeof(*erif_indices),
21762306a36Sopenharmony_ci				     GFP_KERNEL);
21862306a36Sopenharmony_ci	if (!erif_indices)
21962306a36Sopenharmony_ci		return -ENOMEM;
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	list_for_each_entry(rve, &mr_route->evif_list, route_node) {
22262306a36Sopenharmony_ci		if (mlxsw_sp_mr_vif_valid(rve->mr_vif)) {
22362306a36Sopenharmony_ci			u16 rifi = mlxsw_sp_rif_index(rve->mr_vif->rif);
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci			erif_indices[erif++] = rifi;
22662306a36Sopenharmony_ci		}
22762306a36Sopenharmony_ci	}
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci	if (mlxsw_sp_mr_vif_valid(mr_route->ivif.mr_vif))
23062306a36Sopenharmony_ci		irif_index = mlxsw_sp_rif_index(mr_route->ivif.mr_vif->rif);
23162306a36Sopenharmony_ci	else
23262306a36Sopenharmony_ci		irif_index = 0;
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci	route_info->irif_index = irif_index;
23562306a36Sopenharmony_ci	route_info->erif_indices = erif_indices;
23662306a36Sopenharmony_ci	route_info->min_mtu = mr_route->min_mtu;
23762306a36Sopenharmony_ci	route_info->route_action = mr_route->route_action;
23862306a36Sopenharmony_ci	route_info->erif_num = erif;
23962306a36Sopenharmony_ci	return 0;
24062306a36Sopenharmony_ci}
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_cistatic void
24362306a36Sopenharmony_cimlxsw_sp_mr_route_info_destroy(struct mlxsw_sp_mr_route_info *route_info)
24462306a36Sopenharmony_ci{
24562306a36Sopenharmony_ci	kfree(route_info->erif_indices);
24662306a36Sopenharmony_ci}
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_cistatic int mlxsw_sp_mr_route_write(struct mlxsw_sp_mr_table *mr_table,
24962306a36Sopenharmony_ci				   struct mlxsw_sp_mr_route *mr_route,
25062306a36Sopenharmony_ci				   bool replace)
25162306a36Sopenharmony_ci{
25262306a36Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
25362306a36Sopenharmony_ci	struct mlxsw_sp_mr_route_info route_info;
25462306a36Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
25562306a36Sopenharmony_ci	int err;
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci	err = mlxsw_sp_mr_route_info_create(mr_table, mr_route, &route_info);
25862306a36Sopenharmony_ci	if (err)
25962306a36Sopenharmony_ci		return err;
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci	if (!replace) {
26262306a36Sopenharmony_ci		struct mlxsw_sp_mr_route_params route_params;
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci		mr_route->route_priv = kzalloc(mr->mr_ops->route_priv_size,
26562306a36Sopenharmony_ci					       GFP_KERNEL);
26662306a36Sopenharmony_ci		if (!mr_route->route_priv) {
26762306a36Sopenharmony_ci			err = -ENOMEM;
26862306a36Sopenharmony_ci			goto out;
26962306a36Sopenharmony_ci		}
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci		route_params.key = mr_route->key;
27262306a36Sopenharmony_ci		route_params.value = route_info;
27362306a36Sopenharmony_ci		route_params.prio = mlxsw_sp_mr_route_prio(mr_route);
27462306a36Sopenharmony_ci		err = mr->mr_ops->route_create(mlxsw_sp, mr->priv,
27562306a36Sopenharmony_ci					       mr_route->route_priv,
27662306a36Sopenharmony_ci					       &route_params);
27762306a36Sopenharmony_ci		if (err)
27862306a36Sopenharmony_ci			kfree(mr_route->route_priv);
27962306a36Sopenharmony_ci	} else {
28062306a36Sopenharmony_ci		err = mr->mr_ops->route_update(mlxsw_sp, mr_route->route_priv,
28162306a36Sopenharmony_ci					       &route_info);
28262306a36Sopenharmony_ci	}
28362306a36Sopenharmony_ciout:
28462306a36Sopenharmony_ci	mlxsw_sp_mr_route_info_destroy(&route_info);
28562306a36Sopenharmony_ci	return err;
28662306a36Sopenharmony_ci}
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_cistatic void mlxsw_sp_mr_route_erase(struct mlxsw_sp_mr_table *mr_table,
28962306a36Sopenharmony_ci				    struct mlxsw_sp_mr_route *mr_route)
29062306a36Sopenharmony_ci{
29162306a36Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
29262306a36Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_ci	mr->mr_ops->route_destroy(mlxsw_sp, mr->priv, mr_route->route_priv);
29562306a36Sopenharmony_ci	kfree(mr_route->route_priv);
29662306a36Sopenharmony_ci}
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_cistatic struct mlxsw_sp_mr_route *
29962306a36Sopenharmony_cimlxsw_sp_mr_route_create(struct mlxsw_sp_mr_table *mr_table,
30062306a36Sopenharmony_ci			 struct mr_mfc *mfc)
30162306a36Sopenharmony_ci{
30262306a36Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *rve, *tmp;
30362306a36Sopenharmony_ci	struct mlxsw_sp_mr_route *mr_route;
30462306a36Sopenharmony_ci	int err = 0;
30562306a36Sopenharmony_ci	int i;
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci	/* Allocate and init a new route and fill it with parameters */
30862306a36Sopenharmony_ci	mr_route = kzalloc(sizeof(*mr_route), GFP_KERNEL);
30962306a36Sopenharmony_ci	if (!mr_route)
31062306a36Sopenharmony_ci		return ERR_PTR(-ENOMEM);
31162306a36Sopenharmony_ci	INIT_LIST_HEAD(&mr_route->evif_list);
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ci	/* Find min_mtu and link iVIF and eVIFs */
31462306a36Sopenharmony_ci	mr_route->min_mtu = ETH_MAX_MTU;
31562306a36Sopenharmony_ci	mr_cache_hold(mfc);
31662306a36Sopenharmony_ci	mr_route->mfc = mfc;
31762306a36Sopenharmony_ci	mr_table->ops->key_create(mr_table, &mr_route->key, mr_route->mfc);
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci	mr_route->mr_table = mr_table;
32062306a36Sopenharmony_ci	for (i = 0; i < MAXVIFS; i++) {
32162306a36Sopenharmony_ci		if (mfc->mfc_un.res.ttls[i] != 255) {
32262306a36Sopenharmony_ci			err = mlxsw_sp_mr_route_evif_link(mr_route,
32362306a36Sopenharmony_ci							  &mr_table->vifs[i]);
32462306a36Sopenharmony_ci			if (err)
32562306a36Sopenharmony_ci				goto err;
32662306a36Sopenharmony_ci			if (mr_table->vifs[i].dev &&
32762306a36Sopenharmony_ci			    mr_table->vifs[i].dev->mtu < mr_route->min_mtu)
32862306a36Sopenharmony_ci				mr_route->min_mtu = mr_table->vifs[i].dev->mtu;
32962306a36Sopenharmony_ci		}
33062306a36Sopenharmony_ci	}
33162306a36Sopenharmony_ci	mlxsw_sp_mr_route_ivif_link(mr_route,
33262306a36Sopenharmony_ci				    &mr_table->vifs[mfc->mfc_parent]);
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci	mr_route->route_action = mlxsw_sp_mr_route_action(mr_route);
33562306a36Sopenharmony_ci	return mr_route;
33662306a36Sopenharmony_cierr:
33762306a36Sopenharmony_ci	mr_cache_put(mfc);
33862306a36Sopenharmony_ci	list_for_each_entry_safe(rve, tmp, &mr_route->evif_list, route_node)
33962306a36Sopenharmony_ci		mlxsw_sp_mr_route_evif_unlink(rve);
34062306a36Sopenharmony_ci	kfree(mr_route);
34162306a36Sopenharmony_ci	return ERR_PTR(err);
34262306a36Sopenharmony_ci}
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_cistatic void mlxsw_sp_mr_route_destroy(struct mlxsw_sp_mr_table *mr_table,
34562306a36Sopenharmony_ci				      struct mlxsw_sp_mr_route *mr_route)
34662306a36Sopenharmony_ci{
34762306a36Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *rve, *tmp;
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci	mlxsw_sp_mr_route_ivif_unlink(mr_route);
35062306a36Sopenharmony_ci	mr_cache_put(mr_route->mfc);
35162306a36Sopenharmony_ci	list_for_each_entry_safe(rve, tmp, &mr_route->evif_list, route_node)
35262306a36Sopenharmony_ci		mlxsw_sp_mr_route_evif_unlink(rve);
35362306a36Sopenharmony_ci	kfree(mr_route);
35462306a36Sopenharmony_ci}
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_cistatic void mlxsw_sp_mr_mfc_offload_set(struct mlxsw_sp_mr_route *mr_route,
35762306a36Sopenharmony_ci					bool offload)
35862306a36Sopenharmony_ci{
35962306a36Sopenharmony_ci	if (offload)
36062306a36Sopenharmony_ci		mr_route->mfc->mfc_flags |= MFC_OFFLOAD;
36162306a36Sopenharmony_ci	else
36262306a36Sopenharmony_ci		mr_route->mfc->mfc_flags &= ~MFC_OFFLOAD;
36362306a36Sopenharmony_ci}
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_cistatic void mlxsw_sp_mr_mfc_offload_update(struct mlxsw_sp_mr_route *mr_route)
36662306a36Sopenharmony_ci{
36762306a36Sopenharmony_ci	bool offload;
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ci	offload = mr_route->route_action != MLXSW_SP_MR_ROUTE_ACTION_TRAP;
37062306a36Sopenharmony_ci	mlxsw_sp_mr_mfc_offload_set(mr_route, offload);
37162306a36Sopenharmony_ci}
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_cistatic void __mlxsw_sp_mr_route_del(struct mlxsw_sp_mr_table *mr_table,
37462306a36Sopenharmony_ci				    struct mlxsw_sp_mr_route *mr_route)
37562306a36Sopenharmony_ci{
37662306a36Sopenharmony_ci	WARN_ON_ONCE(!mutex_is_locked(&mr_table->route_list_lock));
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_ci	mlxsw_sp_mr_mfc_offload_set(mr_route, false);
37962306a36Sopenharmony_ci	rhashtable_remove_fast(&mr_table->route_ht, &mr_route->ht_node,
38062306a36Sopenharmony_ci			       mlxsw_sp_mr_route_ht_params);
38162306a36Sopenharmony_ci	list_del(&mr_route->node);
38262306a36Sopenharmony_ci	mlxsw_sp_mr_route_erase(mr_table, mr_route);
38362306a36Sopenharmony_ci	mlxsw_sp_mr_route_destroy(mr_table, mr_route);
38462306a36Sopenharmony_ci}
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ciint mlxsw_sp_mr_route_add(struct mlxsw_sp_mr_table *mr_table,
38762306a36Sopenharmony_ci			  struct mr_mfc *mfc, bool replace)
38862306a36Sopenharmony_ci{
38962306a36Sopenharmony_ci	struct mlxsw_sp_mr_route *mr_orig_route = NULL;
39062306a36Sopenharmony_ci	struct mlxsw_sp_mr_route *mr_route;
39162306a36Sopenharmony_ci	int err;
39262306a36Sopenharmony_ci
39362306a36Sopenharmony_ci	if (!mr_table->ops->is_route_valid(mr_table, mfc))
39462306a36Sopenharmony_ci		return -EINVAL;
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci	/* Create a new route */
39762306a36Sopenharmony_ci	mr_route = mlxsw_sp_mr_route_create(mr_table, mfc);
39862306a36Sopenharmony_ci	if (IS_ERR(mr_route))
39962306a36Sopenharmony_ci		return PTR_ERR(mr_route);
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci	/* Find any route with a matching key */
40262306a36Sopenharmony_ci	mr_orig_route = rhashtable_lookup_fast(&mr_table->route_ht,
40362306a36Sopenharmony_ci					       &mr_route->key,
40462306a36Sopenharmony_ci					       mlxsw_sp_mr_route_ht_params);
40562306a36Sopenharmony_ci	if (replace) {
40662306a36Sopenharmony_ci		/* On replace case, make the route point to the new route_priv.
40762306a36Sopenharmony_ci		 */
40862306a36Sopenharmony_ci		if (WARN_ON(!mr_orig_route)) {
40962306a36Sopenharmony_ci			err = -ENOENT;
41062306a36Sopenharmony_ci			goto err_no_orig_route;
41162306a36Sopenharmony_ci		}
41262306a36Sopenharmony_ci		mr_route->route_priv = mr_orig_route->route_priv;
41362306a36Sopenharmony_ci	} else if (mr_orig_route) {
41462306a36Sopenharmony_ci		/* On non replace case, if another route with the same key was
41562306a36Sopenharmony_ci		 * found, abort, as duplicate routes are used for proxy routes.
41662306a36Sopenharmony_ci		 */
41762306a36Sopenharmony_ci		dev_warn(mr_table->mlxsw_sp->bus_info->dev,
41862306a36Sopenharmony_ci			 "Offloading proxy routes is not supported.\n");
41962306a36Sopenharmony_ci		err = -EINVAL;
42062306a36Sopenharmony_ci		goto err_duplicate_route;
42162306a36Sopenharmony_ci	}
42262306a36Sopenharmony_ci
42362306a36Sopenharmony_ci	/* Write the route to the hardware */
42462306a36Sopenharmony_ci	err = mlxsw_sp_mr_route_write(mr_table, mr_route, replace);
42562306a36Sopenharmony_ci	if (err)
42662306a36Sopenharmony_ci		goto err_mr_route_write;
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_ci	/* Put it in the table data-structures */
42962306a36Sopenharmony_ci	mutex_lock(&mr_table->route_list_lock);
43062306a36Sopenharmony_ci	list_add_tail(&mr_route->node, &mr_table->route_list);
43162306a36Sopenharmony_ci	mutex_unlock(&mr_table->route_list_lock);
43262306a36Sopenharmony_ci	err = rhashtable_insert_fast(&mr_table->route_ht,
43362306a36Sopenharmony_ci				     &mr_route->ht_node,
43462306a36Sopenharmony_ci				     mlxsw_sp_mr_route_ht_params);
43562306a36Sopenharmony_ci	if (err)
43662306a36Sopenharmony_ci		goto err_rhashtable_insert;
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci	/* Destroy the original route */
43962306a36Sopenharmony_ci	if (replace) {
44062306a36Sopenharmony_ci		rhashtable_remove_fast(&mr_table->route_ht,
44162306a36Sopenharmony_ci				       &mr_orig_route->ht_node,
44262306a36Sopenharmony_ci				       mlxsw_sp_mr_route_ht_params);
44362306a36Sopenharmony_ci		list_del(&mr_orig_route->node);
44462306a36Sopenharmony_ci		mlxsw_sp_mr_route_destroy(mr_table, mr_orig_route);
44562306a36Sopenharmony_ci	}
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ci	mlxsw_sp_mr_mfc_offload_update(mr_route);
44862306a36Sopenharmony_ci	return 0;
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_cierr_rhashtable_insert:
45162306a36Sopenharmony_ci	mutex_lock(&mr_table->route_list_lock);
45262306a36Sopenharmony_ci	list_del(&mr_route->node);
45362306a36Sopenharmony_ci	mutex_unlock(&mr_table->route_list_lock);
45462306a36Sopenharmony_ci	mlxsw_sp_mr_route_erase(mr_table, mr_route);
45562306a36Sopenharmony_cierr_mr_route_write:
45662306a36Sopenharmony_cierr_no_orig_route:
45762306a36Sopenharmony_cierr_duplicate_route:
45862306a36Sopenharmony_ci	mlxsw_sp_mr_route_destroy(mr_table, mr_route);
45962306a36Sopenharmony_ci	return err;
46062306a36Sopenharmony_ci}
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_civoid mlxsw_sp_mr_route_del(struct mlxsw_sp_mr_table *mr_table,
46362306a36Sopenharmony_ci			   struct mr_mfc *mfc)
46462306a36Sopenharmony_ci{
46562306a36Sopenharmony_ci	struct mlxsw_sp_mr_route *mr_route;
46662306a36Sopenharmony_ci	struct mlxsw_sp_mr_route_key key;
46762306a36Sopenharmony_ci
46862306a36Sopenharmony_ci	mr_table->ops->key_create(mr_table, &key, mfc);
46962306a36Sopenharmony_ci	mr_route = rhashtable_lookup_fast(&mr_table->route_ht, &key,
47062306a36Sopenharmony_ci					  mlxsw_sp_mr_route_ht_params);
47162306a36Sopenharmony_ci	if (mr_route) {
47262306a36Sopenharmony_ci		mutex_lock(&mr_table->route_list_lock);
47362306a36Sopenharmony_ci		__mlxsw_sp_mr_route_del(mr_table, mr_route);
47462306a36Sopenharmony_ci		mutex_unlock(&mr_table->route_list_lock);
47562306a36Sopenharmony_ci	}
47662306a36Sopenharmony_ci}
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci/* Should be called after the VIF struct is updated */
47962306a36Sopenharmony_cistatic int
48062306a36Sopenharmony_cimlxsw_sp_mr_route_ivif_resolve(struct mlxsw_sp_mr_table *mr_table,
48162306a36Sopenharmony_ci			       struct mlxsw_sp_mr_route_vif_entry *rve)
48262306a36Sopenharmony_ci{
48362306a36Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
48462306a36Sopenharmony_ci	enum mlxsw_sp_mr_route_action route_action;
48562306a36Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
48662306a36Sopenharmony_ci	u16 irif_index;
48762306a36Sopenharmony_ci	int err;
48862306a36Sopenharmony_ci
48962306a36Sopenharmony_ci	route_action = mlxsw_sp_mr_route_action(rve->mr_route);
49062306a36Sopenharmony_ci	if (route_action == MLXSW_SP_MR_ROUTE_ACTION_TRAP)
49162306a36Sopenharmony_ci		return 0;
49262306a36Sopenharmony_ci
49362306a36Sopenharmony_ci	/* rve->mr_vif->rif is guaranteed to be valid at this stage */
49462306a36Sopenharmony_ci	irif_index = mlxsw_sp_rif_index(rve->mr_vif->rif);
49562306a36Sopenharmony_ci	err = mr->mr_ops->route_irif_update(mlxsw_sp, rve->mr_route->route_priv,
49662306a36Sopenharmony_ci					    irif_index);
49762306a36Sopenharmony_ci	if (err)
49862306a36Sopenharmony_ci		return err;
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_ci	err = mr->mr_ops->route_action_update(mlxsw_sp,
50162306a36Sopenharmony_ci					      rve->mr_route->route_priv,
50262306a36Sopenharmony_ci					      route_action);
50362306a36Sopenharmony_ci	if (err)
50462306a36Sopenharmony_ci		/* No need to rollback here because the iRIF change only takes
50562306a36Sopenharmony_ci		 * place after the action has been updated.
50662306a36Sopenharmony_ci		 */
50762306a36Sopenharmony_ci		return err;
50862306a36Sopenharmony_ci
50962306a36Sopenharmony_ci	rve->mr_route->route_action = route_action;
51062306a36Sopenharmony_ci	mlxsw_sp_mr_mfc_offload_update(rve->mr_route);
51162306a36Sopenharmony_ci	return 0;
51262306a36Sopenharmony_ci}
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_cistatic void
51562306a36Sopenharmony_cimlxsw_sp_mr_route_ivif_unresolve(struct mlxsw_sp_mr_table *mr_table,
51662306a36Sopenharmony_ci				 struct mlxsw_sp_mr_route_vif_entry *rve)
51762306a36Sopenharmony_ci{
51862306a36Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
51962306a36Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
52062306a36Sopenharmony_ci
52162306a36Sopenharmony_ci	mr->mr_ops->route_action_update(mlxsw_sp, rve->mr_route->route_priv,
52262306a36Sopenharmony_ci					MLXSW_SP_MR_ROUTE_ACTION_TRAP);
52362306a36Sopenharmony_ci	rve->mr_route->route_action = MLXSW_SP_MR_ROUTE_ACTION_TRAP;
52462306a36Sopenharmony_ci	mlxsw_sp_mr_mfc_offload_update(rve->mr_route);
52562306a36Sopenharmony_ci}
52662306a36Sopenharmony_ci
52762306a36Sopenharmony_ci/* Should be called after the RIF struct is updated */
52862306a36Sopenharmony_cistatic int
52962306a36Sopenharmony_cimlxsw_sp_mr_route_evif_resolve(struct mlxsw_sp_mr_table *mr_table,
53062306a36Sopenharmony_ci			       struct mlxsw_sp_mr_route_vif_entry *rve)
53162306a36Sopenharmony_ci{
53262306a36Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
53362306a36Sopenharmony_ci	enum mlxsw_sp_mr_route_action route_action;
53462306a36Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
53562306a36Sopenharmony_ci	u16 erif_index = 0;
53662306a36Sopenharmony_ci	int err;
53762306a36Sopenharmony_ci
53862306a36Sopenharmony_ci	/* Add the eRIF */
53962306a36Sopenharmony_ci	if (mlxsw_sp_mr_vif_valid(rve->mr_vif)) {
54062306a36Sopenharmony_ci		erif_index = mlxsw_sp_rif_index(rve->mr_vif->rif);
54162306a36Sopenharmony_ci		err = mr->mr_ops->route_erif_add(mlxsw_sp,
54262306a36Sopenharmony_ci						 rve->mr_route->route_priv,
54362306a36Sopenharmony_ci						 erif_index);
54462306a36Sopenharmony_ci		if (err)
54562306a36Sopenharmony_ci			return err;
54662306a36Sopenharmony_ci	}
54762306a36Sopenharmony_ci
54862306a36Sopenharmony_ci	/* Update the route action, as the new eVIF can be a tunnel or a pimreg
54962306a36Sopenharmony_ci	 * device which will require updating the action.
55062306a36Sopenharmony_ci	 */
55162306a36Sopenharmony_ci	route_action = mlxsw_sp_mr_route_action(rve->mr_route);
55262306a36Sopenharmony_ci	if (route_action != rve->mr_route->route_action) {
55362306a36Sopenharmony_ci		err = mr->mr_ops->route_action_update(mlxsw_sp,
55462306a36Sopenharmony_ci						      rve->mr_route->route_priv,
55562306a36Sopenharmony_ci						      route_action);
55662306a36Sopenharmony_ci		if (err)
55762306a36Sopenharmony_ci			goto err_route_action_update;
55862306a36Sopenharmony_ci	}
55962306a36Sopenharmony_ci
56062306a36Sopenharmony_ci	/* Update the minimum MTU */
56162306a36Sopenharmony_ci	if (rve->mr_vif->dev->mtu < rve->mr_route->min_mtu) {
56262306a36Sopenharmony_ci		rve->mr_route->min_mtu = rve->mr_vif->dev->mtu;
56362306a36Sopenharmony_ci		err = mr->mr_ops->route_min_mtu_update(mlxsw_sp,
56462306a36Sopenharmony_ci						       rve->mr_route->route_priv,
56562306a36Sopenharmony_ci						       rve->mr_route->min_mtu);
56662306a36Sopenharmony_ci		if (err)
56762306a36Sopenharmony_ci			goto err_route_min_mtu_update;
56862306a36Sopenharmony_ci	}
56962306a36Sopenharmony_ci
57062306a36Sopenharmony_ci	rve->mr_route->route_action = route_action;
57162306a36Sopenharmony_ci	mlxsw_sp_mr_mfc_offload_update(rve->mr_route);
57262306a36Sopenharmony_ci	return 0;
57362306a36Sopenharmony_ci
57462306a36Sopenharmony_cierr_route_min_mtu_update:
57562306a36Sopenharmony_ci	if (route_action != rve->mr_route->route_action)
57662306a36Sopenharmony_ci		mr->mr_ops->route_action_update(mlxsw_sp,
57762306a36Sopenharmony_ci						rve->mr_route->route_priv,
57862306a36Sopenharmony_ci						rve->mr_route->route_action);
57962306a36Sopenharmony_cierr_route_action_update:
58062306a36Sopenharmony_ci	if (mlxsw_sp_mr_vif_valid(rve->mr_vif))
58162306a36Sopenharmony_ci		mr->mr_ops->route_erif_del(mlxsw_sp, rve->mr_route->route_priv,
58262306a36Sopenharmony_ci					   erif_index);
58362306a36Sopenharmony_ci	return err;
58462306a36Sopenharmony_ci}
58562306a36Sopenharmony_ci
58662306a36Sopenharmony_ci/* Should be called before the RIF struct is updated */
58762306a36Sopenharmony_cistatic void
58862306a36Sopenharmony_cimlxsw_sp_mr_route_evif_unresolve(struct mlxsw_sp_mr_table *mr_table,
58962306a36Sopenharmony_ci				 struct mlxsw_sp_mr_route_vif_entry *rve)
59062306a36Sopenharmony_ci{
59162306a36Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
59262306a36Sopenharmony_ci	enum mlxsw_sp_mr_route_action route_action;
59362306a36Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
59462306a36Sopenharmony_ci	u16 rifi;
59562306a36Sopenharmony_ci
59662306a36Sopenharmony_ci	/* If the unresolved RIF was not valid, no need to delete it */
59762306a36Sopenharmony_ci	if (!mlxsw_sp_mr_vif_valid(rve->mr_vif))
59862306a36Sopenharmony_ci		return;
59962306a36Sopenharmony_ci
60062306a36Sopenharmony_ci	/* Update the route action: if there is only one valid eVIF in the
60162306a36Sopenharmony_ci	 * route, set the action to trap as the VIF deletion will lead to zero
60262306a36Sopenharmony_ci	 * valid eVIFs. On any other case, use the mlxsw_sp_mr_route_action to
60362306a36Sopenharmony_ci	 * determine the route action.
60462306a36Sopenharmony_ci	 */
60562306a36Sopenharmony_ci	if (mlxsw_sp_mr_route_valid_evifs_num(rve->mr_route) == 1)
60662306a36Sopenharmony_ci		route_action = MLXSW_SP_MR_ROUTE_ACTION_TRAP;
60762306a36Sopenharmony_ci	else
60862306a36Sopenharmony_ci		route_action = mlxsw_sp_mr_route_action(rve->mr_route);
60962306a36Sopenharmony_ci	if (route_action != rve->mr_route->route_action)
61062306a36Sopenharmony_ci		mr->mr_ops->route_action_update(mlxsw_sp,
61162306a36Sopenharmony_ci						rve->mr_route->route_priv,
61262306a36Sopenharmony_ci						route_action);
61362306a36Sopenharmony_ci
61462306a36Sopenharmony_ci	/* Delete the erif from the route */
61562306a36Sopenharmony_ci	rifi = mlxsw_sp_rif_index(rve->mr_vif->rif);
61662306a36Sopenharmony_ci	mr->mr_ops->route_erif_del(mlxsw_sp, rve->mr_route->route_priv, rifi);
61762306a36Sopenharmony_ci	rve->mr_route->route_action = route_action;
61862306a36Sopenharmony_ci	mlxsw_sp_mr_mfc_offload_update(rve->mr_route);
61962306a36Sopenharmony_ci}
62062306a36Sopenharmony_ci
62162306a36Sopenharmony_cistatic int mlxsw_sp_mr_vif_resolve(struct mlxsw_sp_mr_table *mr_table,
62262306a36Sopenharmony_ci				   struct net_device *dev,
62362306a36Sopenharmony_ci				   struct mlxsw_sp_mr_vif *mr_vif,
62462306a36Sopenharmony_ci				   unsigned long vif_flags,
62562306a36Sopenharmony_ci				   const struct mlxsw_sp_rif *rif)
62662306a36Sopenharmony_ci{
62762306a36Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *irve, *erve;
62862306a36Sopenharmony_ci	int err;
62962306a36Sopenharmony_ci
63062306a36Sopenharmony_ci	/* Update the VIF */
63162306a36Sopenharmony_ci	mr_vif->dev = dev;
63262306a36Sopenharmony_ci	mr_vif->rif = rif;
63362306a36Sopenharmony_ci	mr_vif->vif_flags = vif_flags;
63462306a36Sopenharmony_ci
63562306a36Sopenharmony_ci	/* Update all routes where this VIF is used as an unresolved iRIF */
63662306a36Sopenharmony_ci	list_for_each_entry(irve, &mr_vif->route_ivif_list, vif_node) {
63762306a36Sopenharmony_ci		err = mlxsw_sp_mr_route_ivif_resolve(mr_table, irve);
63862306a36Sopenharmony_ci		if (err)
63962306a36Sopenharmony_ci			goto err_irif_unresolve;
64062306a36Sopenharmony_ci	}
64162306a36Sopenharmony_ci
64262306a36Sopenharmony_ci	/* Update all routes where this VIF is used as an unresolved eRIF */
64362306a36Sopenharmony_ci	list_for_each_entry(erve, &mr_vif->route_evif_list, vif_node) {
64462306a36Sopenharmony_ci		err = mlxsw_sp_mr_route_evif_resolve(mr_table, erve);
64562306a36Sopenharmony_ci		if (err)
64662306a36Sopenharmony_ci			goto err_erif_unresolve;
64762306a36Sopenharmony_ci	}
64862306a36Sopenharmony_ci	return 0;
64962306a36Sopenharmony_ci
65062306a36Sopenharmony_cierr_erif_unresolve:
65162306a36Sopenharmony_ci	list_for_each_entry_continue_reverse(erve, &mr_vif->route_evif_list,
65262306a36Sopenharmony_ci					     vif_node)
65362306a36Sopenharmony_ci		mlxsw_sp_mr_route_evif_unresolve(mr_table, erve);
65462306a36Sopenharmony_cierr_irif_unresolve:
65562306a36Sopenharmony_ci	list_for_each_entry_continue_reverse(irve, &mr_vif->route_ivif_list,
65662306a36Sopenharmony_ci					     vif_node)
65762306a36Sopenharmony_ci		mlxsw_sp_mr_route_ivif_unresolve(mr_table, irve);
65862306a36Sopenharmony_ci	mr_vif->rif = NULL;
65962306a36Sopenharmony_ci	return err;
66062306a36Sopenharmony_ci}
66162306a36Sopenharmony_ci
66262306a36Sopenharmony_cistatic void mlxsw_sp_mr_vif_unresolve(struct mlxsw_sp_mr_table *mr_table,
66362306a36Sopenharmony_ci				      struct net_device *dev,
66462306a36Sopenharmony_ci				      struct mlxsw_sp_mr_vif *mr_vif)
66562306a36Sopenharmony_ci{
66662306a36Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *rve;
66762306a36Sopenharmony_ci
66862306a36Sopenharmony_ci	/* Update all routes where this VIF is used as an unresolved eRIF */
66962306a36Sopenharmony_ci	list_for_each_entry(rve, &mr_vif->route_evif_list, vif_node)
67062306a36Sopenharmony_ci		mlxsw_sp_mr_route_evif_unresolve(mr_table, rve);
67162306a36Sopenharmony_ci
67262306a36Sopenharmony_ci	/* Update all routes where this VIF is used as an unresolved iRIF */
67362306a36Sopenharmony_ci	list_for_each_entry(rve, &mr_vif->route_ivif_list, vif_node)
67462306a36Sopenharmony_ci		mlxsw_sp_mr_route_ivif_unresolve(mr_table, rve);
67562306a36Sopenharmony_ci
67662306a36Sopenharmony_ci	/* Update the VIF */
67762306a36Sopenharmony_ci	mr_vif->dev = dev;
67862306a36Sopenharmony_ci	mr_vif->rif = NULL;
67962306a36Sopenharmony_ci}
68062306a36Sopenharmony_ci
68162306a36Sopenharmony_ciint mlxsw_sp_mr_vif_add(struct mlxsw_sp_mr_table *mr_table,
68262306a36Sopenharmony_ci			struct net_device *dev, vifi_t vif_index,
68362306a36Sopenharmony_ci			unsigned long vif_flags, const struct mlxsw_sp_rif *rif)
68462306a36Sopenharmony_ci{
68562306a36Sopenharmony_ci	struct mlxsw_sp_mr_vif *mr_vif = &mr_table->vifs[vif_index];
68662306a36Sopenharmony_ci
68762306a36Sopenharmony_ci	if (WARN_ON(vif_index >= MAXVIFS))
68862306a36Sopenharmony_ci		return -EINVAL;
68962306a36Sopenharmony_ci	if (mr_vif->dev)
69062306a36Sopenharmony_ci		return -EEXIST;
69162306a36Sopenharmony_ci	return mlxsw_sp_mr_vif_resolve(mr_table, dev, mr_vif, vif_flags, rif);
69262306a36Sopenharmony_ci}
69362306a36Sopenharmony_ci
69462306a36Sopenharmony_civoid mlxsw_sp_mr_vif_del(struct mlxsw_sp_mr_table *mr_table, vifi_t vif_index)
69562306a36Sopenharmony_ci{
69662306a36Sopenharmony_ci	struct mlxsw_sp_mr_vif *mr_vif = &mr_table->vifs[vif_index];
69762306a36Sopenharmony_ci
69862306a36Sopenharmony_ci	if (WARN_ON(vif_index >= MAXVIFS))
69962306a36Sopenharmony_ci		return;
70062306a36Sopenharmony_ci	if (WARN_ON(!mr_vif->dev))
70162306a36Sopenharmony_ci		return;
70262306a36Sopenharmony_ci	mlxsw_sp_mr_vif_unresolve(mr_table, NULL, mr_vif);
70362306a36Sopenharmony_ci}
70462306a36Sopenharmony_ci
70562306a36Sopenharmony_cistatic struct mlxsw_sp_mr_vif *
70662306a36Sopenharmony_cimlxsw_sp_mr_dev_vif_lookup(struct mlxsw_sp_mr_table *mr_table,
70762306a36Sopenharmony_ci			   const struct mlxsw_sp_rif *rif)
70862306a36Sopenharmony_ci{
70962306a36Sopenharmony_ci	vifi_t vif_index;
71062306a36Sopenharmony_ci
71162306a36Sopenharmony_ci	for (vif_index = 0; vif_index < MAXVIFS; vif_index++)
71262306a36Sopenharmony_ci		if (mlxsw_sp_rif_dev_is(rif, mr_table->vifs[vif_index].dev))
71362306a36Sopenharmony_ci			return &mr_table->vifs[vif_index];
71462306a36Sopenharmony_ci	return NULL;
71562306a36Sopenharmony_ci}
71662306a36Sopenharmony_ci
71762306a36Sopenharmony_ciint mlxsw_sp_mr_rif_add(struct mlxsw_sp_mr_table *mr_table,
71862306a36Sopenharmony_ci			const struct mlxsw_sp_rif *rif)
71962306a36Sopenharmony_ci{
72062306a36Sopenharmony_ci	struct mlxsw_sp_mr_vif *mr_vif;
72162306a36Sopenharmony_ci
72262306a36Sopenharmony_ci	if (!mlxsw_sp_rif_has_dev(rif))
72362306a36Sopenharmony_ci		return 0;
72462306a36Sopenharmony_ci
72562306a36Sopenharmony_ci	mr_vif = mlxsw_sp_mr_dev_vif_lookup(mr_table, rif);
72662306a36Sopenharmony_ci	if (!mr_vif)
72762306a36Sopenharmony_ci		return 0;
72862306a36Sopenharmony_ci	return mlxsw_sp_mr_vif_resolve(mr_table, mr_vif->dev, mr_vif,
72962306a36Sopenharmony_ci				       mr_vif->vif_flags, rif);
73062306a36Sopenharmony_ci}
73162306a36Sopenharmony_ci
73262306a36Sopenharmony_civoid mlxsw_sp_mr_rif_del(struct mlxsw_sp_mr_table *mr_table,
73362306a36Sopenharmony_ci			 const struct mlxsw_sp_rif *rif)
73462306a36Sopenharmony_ci{
73562306a36Sopenharmony_ci	struct mlxsw_sp_mr_vif *mr_vif;
73662306a36Sopenharmony_ci
73762306a36Sopenharmony_ci	if (!mlxsw_sp_rif_has_dev(rif))
73862306a36Sopenharmony_ci		return;
73962306a36Sopenharmony_ci
74062306a36Sopenharmony_ci	mr_vif = mlxsw_sp_mr_dev_vif_lookup(mr_table, rif);
74162306a36Sopenharmony_ci	if (!mr_vif)
74262306a36Sopenharmony_ci		return;
74362306a36Sopenharmony_ci	mlxsw_sp_mr_vif_unresolve(mr_table, mr_vif->dev, mr_vif);
74462306a36Sopenharmony_ci}
74562306a36Sopenharmony_ci
74662306a36Sopenharmony_civoid mlxsw_sp_mr_rif_mtu_update(struct mlxsw_sp_mr_table *mr_table,
74762306a36Sopenharmony_ci				const struct mlxsw_sp_rif *rif, int mtu)
74862306a36Sopenharmony_ci{
74962306a36Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
75062306a36Sopenharmony_ci	struct mlxsw_sp_mr_route_vif_entry *rve;
75162306a36Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
75262306a36Sopenharmony_ci	struct mlxsw_sp_mr_vif *mr_vif;
75362306a36Sopenharmony_ci
75462306a36Sopenharmony_ci	if (!mlxsw_sp_rif_has_dev(rif))
75562306a36Sopenharmony_ci		return;
75662306a36Sopenharmony_ci
75762306a36Sopenharmony_ci	/* Search for a VIF that use that RIF */
75862306a36Sopenharmony_ci	mr_vif = mlxsw_sp_mr_dev_vif_lookup(mr_table, rif);
75962306a36Sopenharmony_ci	if (!mr_vif)
76062306a36Sopenharmony_ci		return;
76162306a36Sopenharmony_ci
76262306a36Sopenharmony_ci	/* Update all the routes that uses that VIF as eVIF */
76362306a36Sopenharmony_ci	list_for_each_entry(rve, &mr_vif->route_evif_list, vif_node) {
76462306a36Sopenharmony_ci		if (mtu < rve->mr_route->min_mtu) {
76562306a36Sopenharmony_ci			rve->mr_route->min_mtu = mtu;
76662306a36Sopenharmony_ci			mr->mr_ops->route_min_mtu_update(mlxsw_sp,
76762306a36Sopenharmony_ci							 rve->mr_route->route_priv,
76862306a36Sopenharmony_ci							 mtu);
76962306a36Sopenharmony_ci		}
77062306a36Sopenharmony_ci	}
77162306a36Sopenharmony_ci}
77262306a36Sopenharmony_ci
77362306a36Sopenharmony_ci/* Protocol specific functions */
77462306a36Sopenharmony_cistatic bool
77562306a36Sopenharmony_cimlxsw_sp_mr_route4_validate(const struct mlxsw_sp_mr_table *mr_table,
77662306a36Sopenharmony_ci			    const struct mr_mfc *c)
77762306a36Sopenharmony_ci{
77862306a36Sopenharmony_ci	struct mfc_cache *mfc = (struct mfc_cache *) c;
77962306a36Sopenharmony_ci
78062306a36Sopenharmony_ci	/* If the route is a (*,*) route, abort, as these kind of routes are
78162306a36Sopenharmony_ci	 * used for proxy routes.
78262306a36Sopenharmony_ci	 */
78362306a36Sopenharmony_ci	if (mfc->mfc_origin == htonl(INADDR_ANY) &&
78462306a36Sopenharmony_ci	    mfc->mfc_mcastgrp == htonl(INADDR_ANY)) {
78562306a36Sopenharmony_ci		dev_warn(mr_table->mlxsw_sp->bus_info->dev,
78662306a36Sopenharmony_ci			 "Offloading proxy routes is not supported.\n");
78762306a36Sopenharmony_ci		return false;
78862306a36Sopenharmony_ci	}
78962306a36Sopenharmony_ci	return true;
79062306a36Sopenharmony_ci}
79162306a36Sopenharmony_ci
79262306a36Sopenharmony_cistatic void mlxsw_sp_mr_route4_key(struct mlxsw_sp_mr_table *mr_table,
79362306a36Sopenharmony_ci				   struct mlxsw_sp_mr_route_key *key,
79462306a36Sopenharmony_ci				   struct mr_mfc *c)
79562306a36Sopenharmony_ci{
79662306a36Sopenharmony_ci	const struct mfc_cache *mfc = (struct mfc_cache *) c;
79762306a36Sopenharmony_ci	bool starg;
79862306a36Sopenharmony_ci
79962306a36Sopenharmony_ci	starg = (mfc->mfc_origin == htonl(INADDR_ANY));
80062306a36Sopenharmony_ci
80162306a36Sopenharmony_ci	memset(key, 0, sizeof(*key));
80262306a36Sopenharmony_ci	key->vrid = mr_table->vr_id;
80362306a36Sopenharmony_ci	key->proto = MLXSW_SP_L3_PROTO_IPV4;
80462306a36Sopenharmony_ci	key->group.addr4 = mfc->mfc_mcastgrp;
80562306a36Sopenharmony_ci	key->group_mask.addr4 = htonl(0xffffffff);
80662306a36Sopenharmony_ci	key->source.addr4 = mfc->mfc_origin;
80762306a36Sopenharmony_ci	key->source_mask.addr4 = htonl(starg ? 0 : 0xffffffff);
80862306a36Sopenharmony_ci}
80962306a36Sopenharmony_ci
81062306a36Sopenharmony_cistatic bool mlxsw_sp_mr_route4_starg(const struct mlxsw_sp_mr_table *mr_table,
81162306a36Sopenharmony_ci				     const struct mlxsw_sp_mr_route *mr_route)
81262306a36Sopenharmony_ci{
81362306a36Sopenharmony_ci	return mr_route->key.source_mask.addr4 == htonl(INADDR_ANY);
81462306a36Sopenharmony_ci}
81562306a36Sopenharmony_ci
81662306a36Sopenharmony_cistatic bool mlxsw_sp_mr_vif4_is_regular(const struct mlxsw_sp_mr_vif *vif)
81762306a36Sopenharmony_ci{
81862306a36Sopenharmony_ci	return !(vif->vif_flags & (VIFF_TUNNEL | VIFF_REGISTER));
81962306a36Sopenharmony_ci}
82062306a36Sopenharmony_ci
82162306a36Sopenharmony_cistatic bool
82262306a36Sopenharmony_cimlxsw_sp_mr_route6_validate(const struct mlxsw_sp_mr_table *mr_table,
82362306a36Sopenharmony_ci			    const struct mr_mfc *c)
82462306a36Sopenharmony_ci{
82562306a36Sopenharmony_ci	struct mfc6_cache *mfc = (struct mfc6_cache *) c;
82662306a36Sopenharmony_ci
82762306a36Sopenharmony_ci	/* If the route is a (*,*) route, abort, as these kind of routes are
82862306a36Sopenharmony_ci	 * used for proxy routes.
82962306a36Sopenharmony_ci	 */
83062306a36Sopenharmony_ci	if (ipv6_addr_any(&mfc->mf6c_origin) &&
83162306a36Sopenharmony_ci	    ipv6_addr_any(&mfc->mf6c_mcastgrp)) {
83262306a36Sopenharmony_ci		dev_warn(mr_table->mlxsw_sp->bus_info->dev,
83362306a36Sopenharmony_ci			 "Offloading proxy routes is not supported.\n");
83462306a36Sopenharmony_ci		return false;
83562306a36Sopenharmony_ci	}
83662306a36Sopenharmony_ci	return true;
83762306a36Sopenharmony_ci}
83862306a36Sopenharmony_ci
83962306a36Sopenharmony_cistatic void mlxsw_sp_mr_route6_key(struct mlxsw_sp_mr_table *mr_table,
84062306a36Sopenharmony_ci				   struct mlxsw_sp_mr_route_key *key,
84162306a36Sopenharmony_ci				   struct mr_mfc *c)
84262306a36Sopenharmony_ci{
84362306a36Sopenharmony_ci	const struct mfc6_cache *mfc = (struct mfc6_cache *) c;
84462306a36Sopenharmony_ci
84562306a36Sopenharmony_ci	memset(key, 0, sizeof(*key));
84662306a36Sopenharmony_ci	key->vrid = mr_table->vr_id;
84762306a36Sopenharmony_ci	key->proto = MLXSW_SP_L3_PROTO_IPV6;
84862306a36Sopenharmony_ci	key->group.addr6 = mfc->mf6c_mcastgrp;
84962306a36Sopenharmony_ci	memset(&key->group_mask.addr6, 0xff, sizeof(key->group_mask.addr6));
85062306a36Sopenharmony_ci	key->source.addr6 = mfc->mf6c_origin;
85162306a36Sopenharmony_ci	if (!ipv6_addr_any(&mfc->mf6c_origin))
85262306a36Sopenharmony_ci		memset(&key->source_mask.addr6, 0xff,
85362306a36Sopenharmony_ci		       sizeof(key->source_mask.addr6));
85462306a36Sopenharmony_ci}
85562306a36Sopenharmony_ci
85662306a36Sopenharmony_cistatic bool mlxsw_sp_mr_route6_starg(const struct mlxsw_sp_mr_table *mr_table,
85762306a36Sopenharmony_ci				     const struct mlxsw_sp_mr_route *mr_route)
85862306a36Sopenharmony_ci{
85962306a36Sopenharmony_ci	return ipv6_addr_any(&mr_route->key.source_mask.addr6);
86062306a36Sopenharmony_ci}
86162306a36Sopenharmony_ci
86262306a36Sopenharmony_cistatic bool mlxsw_sp_mr_vif6_is_regular(const struct mlxsw_sp_mr_vif *vif)
86362306a36Sopenharmony_ci{
86462306a36Sopenharmony_ci	return !(vif->vif_flags & MIFF_REGISTER);
86562306a36Sopenharmony_ci}
86662306a36Sopenharmony_ci
86762306a36Sopenharmony_cistatic struct
86862306a36Sopenharmony_cimlxsw_sp_mr_vif_ops mlxsw_sp_mr_vif_ops_arr[] = {
86962306a36Sopenharmony_ci	{
87062306a36Sopenharmony_ci		.is_regular = mlxsw_sp_mr_vif4_is_regular,
87162306a36Sopenharmony_ci	},
87262306a36Sopenharmony_ci	{
87362306a36Sopenharmony_ci		.is_regular = mlxsw_sp_mr_vif6_is_regular,
87462306a36Sopenharmony_ci	},
87562306a36Sopenharmony_ci};
87662306a36Sopenharmony_ci
87762306a36Sopenharmony_cistatic struct
87862306a36Sopenharmony_cimlxsw_sp_mr_table_ops mlxsw_sp_mr_table_ops_arr[] = {
87962306a36Sopenharmony_ci	{
88062306a36Sopenharmony_ci		.is_route_valid = mlxsw_sp_mr_route4_validate,
88162306a36Sopenharmony_ci		.key_create = mlxsw_sp_mr_route4_key,
88262306a36Sopenharmony_ci		.is_route_starg = mlxsw_sp_mr_route4_starg,
88362306a36Sopenharmony_ci	},
88462306a36Sopenharmony_ci	{
88562306a36Sopenharmony_ci		.is_route_valid = mlxsw_sp_mr_route6_validate,
88662306a36Sopenharmony_ci		.key_create = mlxsw_sp_mr_route6_key,
88762306a36Sopenharmony_ci		.is_route_starg = mlxsw_sp_mr_route6_starg,
88862306a36Sopenharmony_ci	},
88962306a36Sopenharmony_ci
89062306a36Sopenharmony_ci};
89162306a36Sopenharmony_ci
89262306a36Sopenharmony_cistruct mlxsw_sp_mr_table *mlxsw_sp_mr_table_create(struct mlxsw_sp *mlxsw_sp,
89362306a36Sopenharmony_ci						   u32 vr_id,
89462306a36Sopenharmony_ci						   enum mlxsw_sp_l3proto proto)
89562306a36Sopenharmony_ci{
89662306a36Sopenharmony_ci	struct mlxsw_sp_mr_route_params catchall_route_params = {
89762306a36Sopenharmony_ci		.prio = MLXSW_SP_MR_ROUTE_PRIO_CATCHALL,
89862306a36Sopenharmony_ci		.key = {
89962306a36Sopenharmony_ci			.vrid = vr_id,
90062306a36Sopenharmony_ci			.proto = proto,
90162306a36Sopenharmony_ci		},
90262306a36Sopenharmony_ci		.value = {
90362306a36Sopenharmony_ci			.route_action = MLXSW_SP_MR_ROUTE_ACTION_TRAP,
90462306a36Sopenharmony_ci		}
90562306a36Sopenharmony_ci	};
90662306a36Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
90762306a36Sopenharmony_ci	struct mlxsw_sp_mr_table *mr_table;
90862306a36Sopenharmony_ci	int err;
90962306a36Sopenharmony_ci	int i;
91062306a36Sopenharmony_ci
91162306a36Sopenharmony_ci	mr_table = kzalloc(sizeof(*mr_table) + mr->mr_ops->route_priv_size,
91262306a36Sopenharmony_ci			   GFP_KERNEL);
91362306a36Sopenharmony_ci	if (!mr_table)
91462306a36Sopenharmony_ci		return ERR_PTR(-ENOMEM);
91562306a36Sopenharmony_ci
91662306a36Sopenharmony_ci	mr_table->vr_id = vr_id;
91762306a36Sopenharmony_ci	mr_table->mlxsw_sp = mlxsw_sp;
91862306a36Sopenharmony_ci	mr_table->proto = proto;
91962306a36Sopenharmony_ci	mr_table->ops = &mlxsw_sp_mr_table_ops_arr[proto];
92062306a36Sopenharmony_ci	INIT_LIST_HEAD(&mr_table->route_list);
92162306a36Sopenharmony_ci	mutex_init(&mr_table->route_list_lock);
92262306a36Sopenharmony_ci
92362306a36Sopenharmony_ci	err = rhashtable_init(&mr_table->route_ht,
92462306a36Sopenharmony_ci			      &mlxsw_sp_mr_route_ht_params);
92562306a36Sopenharmony_ci	if (err)
92662306a36Sopenharmony_ci		goto err_route_rhashtable_init;
92762306a36Sopenharmony_ci
92862306a36Sopenharmony_ci	for (i = 0; i < MAXVIFS; i++) {
92962306a36Sopenharmony_ci		INIT_LIST_HEAD(&mr_table->vifs[i].route_evif_list);
93062306a36Sopenharmony_ci		INIT_LIST_HEAD(&mr_table->vifs[i].route_ivif_list);
93162306a36Sopenharmony_ci		mr_table->vifs[i].ops = &mlxsw_sp_mr_vif_ops_arr[proto];
93262306a36Sopenharmony_ci	}
93362306a36Sopenharmony_ci
93462306a36Sopenharmony_ci	err = mr->mr_ops->route_create(mlxsw_sp, mr->priv,
93562306a36Sopenharmony_ci				       mr_table->catchall_route_priv,
93662306a36Sopenharmony_ci				       &catchall_route_params);
93762306a36Sopenharmony_ci	if (err)
93862306a36Sopenharmony_ci		goto err_ops_route_create;
93962306a36Sopenharmony_ci	mutex_lock(&mr->table_list_lock);
94062306a36Sopenharmony_ci	list_add_tail(&mr_table->node, &mr->table_list);
94162306a36Sopenharmony_ci	mutex_unlock(&mr->table_list_lock);
94262306a36Sopenharmony_ci	return mr_table;
94362306a36Sopenharmony_ci
94462306a36Sopenharmony_cierr_ops_route_create:
94562306a36Sopenharmony_ci	rhashtable_destroy(&mr_table->route_ht);
94662306a36Sopenharmony_cierr_route_rhashtable_init:
94762306a36Sopenharmony_ci	mutex_destroy(&mr_table->route_list_lock);
94862306a36Sopenharmony_ci	kfree(mr_table);
94962306a36Sopenharmony_ci	return ERR_PTR(err);
95062306a36Sopenharmony_ci}
95162306a36Sopenharmony_ci
95262306a36Sopenharmony_civoid mlxsw_sp_mr_table_destroy(struct mlxsw_sp_mr_table *mr_table)
95362306a36Sopenharmony_ci{
95462306a36Sopenharmony_ci	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
95562306a36Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
95662306a36Sopenharmony_ci
95762306a36Sopenharmony_ci	WARN_ON(!mlxsw_sp_mr_table_empty(mr_table));
95862306a36Sopenharmony_ci	mutex_lock(&mr->table_list_lock);
95962306a36Sopenharmony_ci	list_del(&mr_table->node);
96062306a36Sopenharmony_ci	mutex_unlock(&mr->table_list_lock);
96162306a36Sopenharmony_ci	mr->mr_ops->route_destroy(mlxsw_sp, mr->priv,
96262306a36Sopenharmony_ci				  &mr_table->catchall_route_priv);
96362306a36Sopenharmony_ci	rhashtable_destroy(&mr_table->route_ht);
96462306a36Sopenharmony_ci	mutex_destroy(&mr_table->route_list_lock);
96562306a36Sopenharmony_ci	kfree(mr_table);
96662306a36Sopenharmony_ci}
96762306a36Sopenharmony_ci
96862306a36Sopenharmony_civoid mlxsw_sp_mr_table_flush(struct mlxsw_sp_mr_table *mr_table)
96962306a36Sopenharmony_ci{
97062306a36Sopenharmony_ci	struct mlxsw_sp_mr_route *mr_route, *tmp;
97162306a36Sopenharmony_ci	int i;
97262306a36Sopenharmony_ci
97362306a36Sopenharmony_ci	mutex_lock(&mr_table->route_list_lock);
97462306a36Sopenharmony_ci	list_for_each_entry_safe(mr_route, tmp, &mr_table->route_list, node)
97562306a36Sopenharmony_ci		__mlxsw_sp_mr_route_del(mr_table, mr_route);
97662306a36Sopenharmony_ci	mutex_unlock(&mr_table->route_list_lock);
97762306a36Sopenharmony_ci
97862306a36Sopenharmony_ci	for (i = 0; i < MAXVIFS; i++) {
97962306a36Sopenharmony_ci		mr_table->vifs[i].dev = NULL;
98062306a36Sopenharmony_ci		mr_table->vifs[i].rif = NULL;
98162306a36Sopenharmony_ci	}
98262306a36Sopenharmony_ci}
98362306a36Sopenharmony_ci
98462306a36Sopenharmony_cibool mlxsw_sp_mr_table_empty(const struct mlxsw_sp_mr_table *mr_table)
98562306a36Sopenharmony_ci{
98662306a36Sopenharmony_ci	int i;
98762306a36Sopenharmony_ci
98862306a36Sopenharmony_ci	for (i = 0; i < MAXVIFS; i++)
98962306a36Sopenharmony_ci		if (mr_table->vifs[i].dev)
99062306a36Sopenharmony_ci			return false;
99162306a36Sopenharmony_ci	return list_empty(&mr_table->route_list);
99262306a36Sopenharmony_ci}
99362306a36Sopenharmony_ci
99462306a36Sopenharmony_cistatic void mlxsw_sp_mr_route_stats_update(struct mlxsw_sp *mlxsw_sp,
99562306a36Sopenharmony_ci					   struct mlxsw_sp_mr_route *mr_route)
99662306a36Sopenharmony_ci{
99762306a36Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
99862306a36Sopenharmony_ci	u64 packets, bytes;
99962306a36Sopenharmony_ci
100062306a36Sopenharmony_ci	if (mr_route->route_action == MLXSW_SP_MR_ROUTE_ACTION_TRAP)
100162306a36Sopenharmony_ci		return;
100262306a36Sopenharmony_ci
100362306a36Sopenharmony_ci	mr->mr_ops->route_stats(mlxsw_sp, mr_route->route_priv, &packets,
100462306a36Sopenharmony_ci				&bytes);
100562306a36Sopenharmony_ci
100662306a36Sopenharmony_ci	if (mr_route->mfc->mfc_un.res.pkt != packets)
100762306a36Sopenharmony_ci		mr_route->mfc->mfc_un.res.lastuse = jiffies;
100862306a36Sopenharmony_ci	mr_route->mfc->mfc_un.res.pkt = packets;
100962306a36Sopenharmony_ci	mr_route->mfc->mfc_un.res.bytes = bytes;
101062306a36Sopenharmony_ci}
101162306a36Sopenharmony_ci
101262306a36Sopenharmony_cistatic void mlxsw_sp_mr_stats_update(struct work_struct *work)
101362306a36Sopenharmony_ci{
101462306a36Sopenharmony_ci	struct mlxsw_sp_mr *mr = container_of(work, struct mlxsw_sp_mr,
101562306a36Sopenharmony_ci					      stats_update_dw.work);
101662306a36Sopenharmony_ci	struct mlxsw_sp_mr_table *mr_table;
101762306a36Sopenharmony_ci	struct mlxsw_sp_mr_route *mr_route;
101862306a36Sopenharmony_ci	unsigned long interval;
101962306a36Sopenharmony_ci
102062306a36Sopenharmony_ci	mutex_lock(&mr->table_list_lock);
102162306a36Sopenharmony_ci	list_for_each_entry(mr_table, &mr->table_list, node) {
102262306a36Sopenharmony_ci		mutex_lock(&mr_table->route_list_lock);
102362306a36Sopenharmony_ci		list_for_each_entry(mr_route, &mr_table->route_list, node)
102462306a36Sopenharmony_ci			mlxsw_sp_mr_route_stats_update(mr_table->mlxsw_sp,
102562306a36Sopenharmony_ci						       mr_route);
102662306a36Sopenharmony_ci		mutex_unlock(&mr_table->route_list_lock);
102762306a36Sopenharmony_ci	}
102862306a36Sopenharmony_ci	mutex_unlock(&mr->table_list_lock);
102962306a36Sopenharmony_ci
103062306a36Sopenharmony_ci	interval = msecs_to_jiffies(MLXSW_SP_MR_ROUTES_COUNTER_UPDATE_INTERVAL);
103162306a36Sopenharmony_ci	mlxsw_core_schedule_dw(&mr->stats_update_dw, interval);
103262306a36Sopenharmony_ci}
103362306a36Sopenharmony_ci
103462306a36Sopenharmony_ciint mlxsw_sp_mr_init(struct mlxsw_sp *mlxsw_sp,
103562306a36Sopenharmony_ci		     const struct mlxsw_sp_mr_ops *mr_ops)
103662306a36Sopenharmony_ci{
103762306a36Sopenharmony_ci	struct mlxsw_sp_mr *mr;
103862306a36Sopenharmony_ci	unsigned long interval;
103962306a36Sopenharmony_ci	int err;
104062306a36Sopenharmony_ci
104162306a36Sopenharmony_ci	mr = kzalloc(sizeof(*mr) + mr_ops->priv_size, GFP_KERNEL);
104262306a36Sopenharmony_ci	if (!mr)
104362306a36Sopenharmony_ci		return -ENOMEM;
104462306a36Sopenharmony_ci	mr->mr_ops = mr_ops;
104562306a36Sopenharmony_ci	mlxsw_sp->mr = mr;
104662306a36Sopenharmony_ci	INIT_LIST_HEAD(&mr->table_list);
104762306a36Sopenharmony_ci	mutex_init(&mr->table_list_lock);
104862306a36Sopenharmony_ci
104962306a36Sopenharmony_ci	err = mr_ops->init(mlxsw_sp, mr->priv);
105062306a36Sopenharmony_ci	if (err)
105162306a36Sopenharmony_ci		goto err;
105262306a36Sopenharmony_ci
105362306a36Sopenharmony_ci	/* Create the delayed work for counter updates */
105462306a36Sopenharmony_ci	INIT_DELAYED_WORK(&mr->stats_update_dw, mlxsw_sp_mr_stats_update);
105562306a36Sopenharmony_ci	interval = msecs_to_jiffies(MLXSW_SP_MR_ROUTES_COUNTER_UPDATE_INTERVAL);
105662306a36Sopenharmony_ci	mlxsw_core_schedule_dw(&mr->stats_update_dw, interval);
105762306a36Sopenharmony_ci	return 0;
105862306a36Sopenharmony_cierr:
105962306a36Sopenharmony_ci	mutex_destroy(&mr->table_list_lock);
106062306a36Sopenharmony_ci	kfree(mr);
106162306a36Sopenharmony_ci	return err;
106262306a36Sopenharmony_ci}
106362306a36Sopenharmony_ci
106462306a36Sopenharmony_civoid mlxsw_sp_mr_fini(struct mlxsw_sp *mlxsw_sp)
106562306a36Sopenharmony_ci{
106662306a36Sopenharmony_ci	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
106762306a36Sopenharmony_ci
106862306a36Sopenharmony_ci	cancel_delayed_work_sync(&mr->stats_update_dw);
106962306a36Sopenharmony_ci	mr->mr_ops->fini(mlxsw_sp, mr->priv);
107062306a36Sopenharmony_ci	mutex_destroy(&mr->table_list_lock);
107162306a36Sopenharmony_ci	kfree(mr);
107262306a36Sopenharmony_ci}
1073