xref: /openbmc/linux/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_tcam.c (revision c1e01cdbe0312d95b8c1542abd67fe786b534f57)
19948a064SJiri Pirko // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
29948a064SJiri Pirko /* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
322a67766SJiri Pirko 
422a67766SJiri Pirko #include <linux/kernel.h>
522a67766SJiri Pirko #include <linux/slab.h>
622a67766SJiri Pirko #include <linux/errno.h>
722a67766SJiri Pirko #include <linux/bitops.h>
822a67766SJiri Pirko #include <linux/list.h>
922a67766SJiri Pirko #include <linux/rhashtable.h>
1022a67766SJiri Pirko #include <linux/netdevice.h>
115ec2ee28SJiri Pirko #include <linux/mutex.h>
1299a9e7fbSAmit Cohen #include <linux/refcount.h>
1379736f57SIdo Schimmel #include <linux/idr.h>
1474cbc3c0SIdo Schimmel #include <net/devlink.h>
153985de72SJiri Pirko #include <trace/events/mlxsw.h>
1622a67766SJiri Pirko 
1722a67766SJiri Pirko #include "reg.h"
1822a67766SJiri Pirko #include "core.h"
1922a67766SJiri Pirko #include "resources.h"
2022a67766SJiri Pirko #include "spectrum.h"
2164eccd00SJiri Pirko #include "spectrum_acl_tcam.h"
2222a67766SJiri Pirko #include "core_acl_flex_keys.h"
2322a67766SJiri Pirko 
mlxsw_sp_acl_tcam_priv_size(struct mlxsw_sp * mlxsw_sp)24bab5c1cfSJiri Pirko size_t mlxsw_sp_acl_tcam_priv_size(struct mlxsw_sp *mlxsw_sp)
2522a67766SJiri Pirko {
26bab5c1cfSJiri Pirko 	const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
27bab5c1cfSJiri Pirko 
28bab5c1cfSJiri Pirko 	return ops->priv_size;
29bab5c1cfSJiri Pirko }
30bab5c1cfSJiri Pirko 
31e5e7962eSJiri Pirko #define MLXSW_SP_ACL_TCAM_VREGION_REHASH_INTRVL_DFLT 5000 /* ms */
3298bbf70cSJiri Pirko #define MLXSW_SP_ACL_TCAM_VREGION_REHASH_INTRVL_MIN 3000 /* ms */
33c9c9af91SJiri Pirko #define MLXSW_SP_ACL_TCAM_VREGION_REHASH_CREDITS 100 /* number of entries */
34e5e7962eSJiri Pirko 
mlxsw_sp_acl_tcam_priority_get(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_rule_info * rulei,u32 * priority,bool fillup_priority)35ea8b2e28SJiri Pirko int mlxsw_sp_acl_tcam_priority_get(struct mlxsw_sp *mlxsw_sp,
36ea8b2e28SJiri Pirko 				   struct mlxsw_sp_acl_rule_info *rulei,
37ea8b2e28SJiri Pirko 				   u32 *priority, bool fillup_priority)
38ea8b2e28SJiri Pirko {
39ea8b2e28SJiri Pirko 	u64 max_priority;
40ea8b2e28SJiri Pirko 
41ea8b2e28SJiri Pirko 	if (!fillup_priority) {
42ea8b2e28SJiri Pirko 		*priority = 0;
43ea8b2e28SJiri Pirko 		return 0;
44ea8b2e28SJiri Pirko 	}
45ea8b2e28SJiri Pirko 
46ea8b2e28SJiri Pirko 	if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, KVD_SIZE))
47ea8b2e28SJiri Pirko 		return -EIO;
48ea8b2e28SJiri Pirko 
49d7263ab3SNir Dotan 	/* Priority range is 1..cap_kvd_size-1. */
50d7263ab3SNir Dotan 	max_priority = MLXSW_CORE_RES_GET(mlxsw_sp->core, KVD_SIZE) - 1;
51d7263ab3SNir Dotan 	if (rulei->priority >= max_priority)
52ea8b2e28SJiri Pirko 		return -EINVAL;
53ea8b2e28SJiri Pirko 
54ea8b2e28SJiri Pirko 	/* Unlike in TC, in HW, higher number means higher priority. */
55ea8b2e28SJiri Pirko 	*priority = max_priority - rulei->priority;
56ea8b2e28SJiri Pirko 	return 0;
57ea8b2e28SJiri Pirko }
58ea8b2e28SJiri Pirko 
mlxsw_sp_acl_tcam_region_id_get(struct mlxsw_sp_acl_tcam * tcam,u16 * p_id)5922a67766SJiri Pirko static int mlxsw_sp_acl_tcam_region_id_get(struct mlxsw_sp_acl_tcam *tcam,
6022a67766SJiri Pirko 					   u16 *p_id)
6122a67766SJiri Pirko {
6279736f57SIdo Schimmel 	int id;
6322a67766SJiri Pirko 
6479736f57SIdo Schimmel 	id = ida_alloc_max(&tcam->used_regions, tcam->max_regions - 1,
6579736f57SIdo Schimmel 			   GFP_KERNEL);
6679736f57SIdo Schimmel 	if (id < 0)
6779736f57SIdo Schimmel 		return id;
6879736f57SIdo Schimmel 
6922a67766SJiri Pirko 	*p_id = id;
7079736f57SIdo Schimmel 
7122a67766SJiri Pirko 	return 0;
7222a67766SJiri Pirko }
7322a67766SJiri Pirko 
mlxsw_sp_acl_tcam_region_id_put(struct mlxsw_sp_acl_tcam * tcam,u16 id)7422a67766SJiri Pirko static void mlxsw_sp_acl_tcam_region_id_put(struct mlxsw_sp_acl_tcam *tcam,
7522a67766SJiri Pirko 					    u16 id)
7622a67766SJiri Pirko {
7779736f57SIdo Schimmel 	ida_free(&tcam->used_regions, id);
7822a67766SJiri Pirko }
7922a67766SJiri Pirko 
mlxsw_sp_acl_tcam_group_id_get(struct mlxsw_sp_acl_tcam * tcam,u16 * p_id)8022a67766SJiri Pirko static int mlxsw_sp_acl_tcam_group_id_get(struct mlxsw_sp_acl_tcam *tcam,
8122a67766SJiri Pirko 					  u16 *p_id)
8222a67766SJiri Pirko {
8379736f57SIdo Schimmel 	int id;
8422a67766SJiri Pirko 
8579736f57SIdo Schimmel 	id = ida_alloc_max(&tcam->used_groups, tcam->max_groups - 1,
8679736f57SIdo Schimmel 			   GFP_KERNEL);
8779736f57SIdo Schimmel 	if (id < 0)
8879736f57SIdo Schimmel 		return id;
8979736f57SIdo Schimmel 
9022a67766SJiri Pirko 	*p_id = id;
9179736f57SIdo Schimmel 
9222a67766SJiri Pirko 	return 0;
9322a67766SJiri Pirko }
9422a67766SJiri Pirko 
mlxsw_sp_acl_tcam_group_id_put(struct mlxsw_sp_acl_tcam * tcam,u16 id)9522a67766SJiri Pirko static void mlxsw_sp_acl_tcam_group_id_put(struct mlxsw_sp_acl_tcam *tcam,
9622a67766SJiri Pirko 					   u16 id)
9722a67766SJiri Pirko {
9879736f57SIdo Schimmel 	ida_free(&tcam->used_groups, id);
9922a67766SJiri Pirko }
10022a67766SJiri Pirko 
10122a67766SJiri Pirko struct mlxsw_sp_acl_tcam_pattern {
10222a67766SJiri Pirko 	const enum mlxsw_afk_element *elements;
10322a67766SJiri Pirko 	unsigned int elements_count;
10422a67766SJiri Pirko };
10522a67766SJiri Pirko 
10622a67766SJiri Pirko struct mlxsw_sp_acl_tcam_group {
10722a67766SJiri Pirko 	struct mlxsw_sp_acl_tcam *tcam;
10822a67766SJiri Pirko 	u16 id;
1095ec2ee28SJiri Pirko 	struct mutex lock; /* guards region list updates */
1102802aadfSJiri Pirko 	struct list_head region_list;
11122a67766SJiri Pirko 	unsigned int region_count;
1122802aadfSJiri Pirko };
1132802aadfSJiri Pirko 
1142802aadfSJiri Pirko struct mlxsw_sp_acl_tcam_vgroup {
1152802aadfSJiri Pirko 	struct mlxsw_sp_acl_tcam_group group;
1162802aadfSJiri Pirko 	struct list_head vregion_list;
117b2d6b4d2SJiri Pirko 	struct rhashtable vchunk_ht;
11822a67766SJiri Pirko 	const struct mlxsw_sp_acl_tcam_pattern *patterns;
11922a67766SJiri Pirko 	unsigned int patterns_count;
120e2f2a1fdSJiri Pirko 	bool tmplt_elusage_set;
121e2f2a1fdSJiri Pirko 	struct mlxsw_afk_element_usage tmplt_elusage;
1226b861682SJiri Pirko 	bool vregion_rehash_enabled;
123593bb843SJiri Pirko 	unsigned int *p_min_prio;
124593bb843SJiri Pirko 	unsigned int *p_max_prio;
12522a67766SJiri Pirko };
12622a67766SJiri Pirko 
127559c2768SJiri Pirko struct mlxsw_sp_acl_tcam_rehash_ctx {
128559c2768SJiri Pirko 	void *hints_priv;
129220f4fbaSJiri Pirko 	bool this_is_rollback;
1306f9579d4SJiri Pirko 	struct mlxsw_sp_acl_tcam_vchunk *current_vchunk; /* vchunk being
1316f9579d4SJiri Pirko 							  * currently migrated.
1326f9579d4SJiri Pirko 							  */
1336f9579d4SJiri Pirko 	struct mlxsw_sp_acl_tcam_ventry *start_ventry; /* ventry to start
1346f9579d4SJiri Pirko 							* migration from in
1356f9579d4SJiri Pirko 							* a vchunk being
1366f9579d4SJiri Pirko 							* currently migrated.
1376f9579d4SJiri Pirko 							*/
1386f9579d4SJiri Pirko 	struct mlxsw_sp_acl_tcam_ventry *stop_ventry; /* ventry to stop
1396f9579d4SJiri Pirko 						       * migration at
1406f9579d4SJiri Pirko 						       * a vchunk being
1416f9579d4SJiri Pirko 						       * currently migrated.
1426f9579d4SJiri Pirko 						       */
143559c2768SJiri Pirko };
144559c2768SJiri Pirko 
1450f54236dSJiri Pirko struct mlxsw_sp_acl_tcam_vregion {
1461263a9abSJiri Pirko 	struct mutex lock; /* Protects consistency of region, region2 pointers
1471263a9abSJiri Pirko 			    * and vchunk_list.
1481263a9abSJiri Pirko 			    */
14922a67766SJiri Pirko 	struct mlxsw_sp_acl_tcam_region *region;
150e5e7962eSJiri Pirko 	struct mlxsw_sp_acl_tcam_region *region2; /* Used during migration */
1510f54236dSJiri Pirko 	struct list_head list; /* Member of a TCAM group */
152e5e7962eSJiri Pirko 	struct list_head tlist; /* Member of a TCAM */
153b2d6b4d2SJiri Pirko 	struct list_head vchunk_list; /* List of vchunks under this vregion */
1540f54236dSJiri Pirko 	struct mlxsw_afk_key_info *key_info;
155e5e7962eSJiri Pirko 	struct mlxsw_sp_acl_tcam *tcam;
1566b861682SJiri Pirko 	struct mlxsw_sp_acl_tcam_vgroup *vgroup;
157f9b274ceSJiri Pirko 	struct {
158f9b274ceSJiri Pirko 		struct delayed_work dw;
159559c2768SJiri Pirko 		struct mlxsw_sp_acl_tcam_rehash_ctx ctx;
160f9b274ceSJiri Pirko 	} rehash;
161e5e7962eSJiri Pirko 	struct mlxsw_sp *mlxsw_sp;
16299a9e7fbSAmit Cohen 	refcount_t ref_count;
1630f54236dSJiri Pirko };
1640f54236dSJiri Pirko 
165b2d6b4d2SJiri Pirko struct mlxsw_sp_acl_tcam_vchunk;
166b2d6b4d2SJiri Pirko 
1670f54236dSJiri Pirko struct mlxsw_sp_acl_tcam_chunk {
168b2d6b4d2SJiri Pirko 	struct mlxsw_sp_acl_tcam_vchunk *vchunk;
169e5e7962eSJiri Pirko 	struct mlxsw_sp_acl_tcam_region *region;
170e99f8e7fSGustavo A. R. Silva 	unsigned long priv[];
171b2d6b4d2SJiri Pirko 	/* priv has to be always the last item */
172b2d6b4d2SJiri Pirko };
173b2d6b4d2SJiri Pirko 
174b2d6b4d2SJiri Pirko struct mlxsw_sp_acl_tcam_vchunk {
175b2d6b4d2SJiri Pirko 	struct mlxsw_sp_acl_tcam_chunk *chunk;
176e5e7962eSJiri Pirko 	struct mlxsw_sp_acl_tcam_chunk *chunk2; /* Used during migration */
1770f54236dSJiri Pirko 	struct list_head list; /* Member of a TCAM vregion */
1780f54236dSJiri Pirko 	struct rhash_head ht_node; /* Member of a chunk HT */
179e5e7962eSJiri Pirko 	struct list_head ventry_list;
1800f54236dSJiri Pirko 	unsigned int priority; /* Priority within the vregion and group */
1812802aadfSJiri Pirko 	struct mlxsw_sp_acl_tcam_vgroup *vgroup;
1820f54236dSJiri Pirko 	struct mlxsw_sp_acl_tcam_vregion *vregion;
18399a9e7fbSAmit Cohen 	refcount_t ref_count;
18422a67766SJiri Pirko };
18522a67766SJiri Pirko 
18622a67766SJiri Pirko struct mlxsw_sp_acl_tcam_entry {
187c4c2dc54SJiri Pirko 	struct mlxsw_sp_acl_tcam_ventry *ventry;
188e5e7962eSJiri Pirko 	struct mlxsw_sp_acl_tcam_chunk *chunk;
189e99f8e7fSGustavo A. R. Silva 	unsigned long priv[];
19064eccd00SJiri Pirko 	/* priv has to be always the last item */
19122a67766SJiri Pirko };
19222a67766SJiri Pirko 
193c4c2dc54SJiri Pirko struct mlxsw_sp_acl_tcam_ventry {
194c4c2dc54SJiri Pirko 	struct mlxsw_sp_acl_tcam_entry *entry;
195e5e7962eSJiri Pirko 	struct list_head list; /* Member of a TCAM vchunk */
196c4c2dc54SJiri Pirko 	struct mlxsw_sp_acl_tcam_vchunk *vchunk;
197e5e7962eSJiri Pirko 	struct mlxsw_sp_acl_rule_info *rulei;
198c4c2dc54SJiri Pirko };
199c4c2dc54SJiri Pirko 
200b2d6b4d2SJiri Pirko static const struct rhashtable_params mlxsw_sp_acl_tcam_vchunk_ht_params = {
20122a67766SJiri Pirko 	.key_len = sizeof(unsigned int),
202b2d6b4d2SJiri Pirko 	.key_offset = offsetof(struct mlxsw_sp_acl_tcam_vchunk, priority),
203b2d6b4d2SJiri Pirko 	.head_offset = offsetof(struct mlxsw_sp_acl_tcam_vchunk, ht_node),
20422a67766SJiri Pirko 	.automatic_shrinking = true,
20522a67766SJiri Pirko };
20622a67766SJiri Pirko 
mlxsw_sp_acl_tcam_group_update(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_group * group)20722a67766SJiri Pirko static int mlxsw_sp_acl_tcam_group_update(struct mlxsw_sp *mlxsw_sp,
20822a67766SJiri Pirko 					  struct mlxsw_sp_acl_tcam_group *group)
20922a67766SJiri Pirko {
2102802aadfSJiri Pirko 	struct mlxsw_sp_acl_tcam_region *region;
21122a67766SJiri Pirko 	char pagt_pl[MLXSW_REG_PAGT_LEN];
21222a67766SJiri Pirko 	int acl_index = 0;
21322a67766SJiri Pirko 
21422a67766SJiri Pirko 	mlxsw_reg_pagt_pack(pagt_pl, group->id);
2152802aadfSJiri Pirko 	list_for_each_entry(region, &group->region_list, list) {
2162802aadfSJiri Pirko 		bool multi = false;
2172802aadfSJiri Pirko 
2182802aadfSJiri Pirko 		/* Check if the next entry in the list has the same vregion. */
2192802aadfSJiri Pirko 		if (region->list.next != &group->region_list &&
2202802aadfSJiri Pirko 		    list_next_entry(region, list)->vregion == region->vregion)
2212802aadfSJiri Pirko 			multi = true;
222e5e7962eSJiri Pirko 		mlxsw_reg_pagt_acl_id_pack(pagt_pl, acl_index++,
2232802aadfSJiri Pirko 					   region->id, multi);
224e5e7962eSJiri Pirko 	}
22522a67766SJiri Pirko 	mlxsw_reg_pagt_size_set(pagt_pl, acl_index);
22622a67766SJiri Pirko 	return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pagt), pagt_pl);
22722a67766SJiri Pirko }
22822a67766SJiri Pirko 
22922a67766SJiri Pirko static int
mlxsw_sp_acl_tcam_group_add(struct mlxsw_sp_acl_tcam * tcam,struct mlxsw_sp_acl_tcam_group * group)2302802aadfSJiri Pirko mlxsw_sp_acl_tcam_group_add(struct mlxsw_sp_acl_tcam *tcam,
2312802aadfSJiri Pirko 			    struct mlxsw_sp_acl_tcam_group *group)
2322802aadfSJiri Pirko {
2332802aadfSJiri Pirko 	int err;
2342802aadfSJiri Pirko 
2352802aadfSJiri Pirko 	group->tcam = tcam;
2362802aadfSJiri Pirko 	INIT_LIST_HEAD(&group->region_list);
2372802aadfSJiri Pirko 
2382802aadfSJiri Pirko 	err = mlxsw_sp_acl_tcam_group_id_get(tcam, &group->id);
2392802aadfSJiri Pirko 	if (err)
2402802aadfSJiri Pirko 		return err;
2412802aadfSJiri Pirko 
24272865028SIdo Schimmel 	mutex_init(&group->lock);
24372865028SIdo Schimmel 
2442802aadfSJiri Pirko 	return 0;
2452802aadfSJiri Pirko }
2462802aadfSJiri Pirko 
mlxsw_sp_acl_tcam_group_del(struct mlxsw_sp_acl_tcam_group * group)2472802aadfSJiri Pirko static void mlxsw_sp_acl_tcam_group_del(struct mlxsw_sp_acl_tcam_group *group)
2482802aadfSJiri Pirko {
2492802aadfSJiri Pirko 	struct mlxsw_sp_acl_tcam *tcam = group->tcam;
2502802aadfSJiri Pirko 
2515ec2ee28SJiri Pirko 	mutex_destroy(&group->lock);
2522802aadfSJiri Pirko 	mlxsw_sp_acl_tcam_group_id_put(tcam, group->id);
2532802aadfSJiri Pirko 	WARN_ON(!list_empty(&group->region_list));
2542802aadfSJiri Pirko }
2552802aadfSJiri Pirko 
2562802aadfSJiri Pirko static int
mlxsw_sp_acl_tcam_vgroup_add(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam * tcam,struct mlxsw_sp_acl_tcam_vgroup * vgroup,const struct mlxsw_sp_acl_tcam_pattern * patterns,unsigned int patterns_count,struct mlxsw_afk_element_usage * tmplt_elusage,bool vregion_rehash_enabled,unsigned int * p_min_prio,unsigned int * p_max_prio)2572802aadfSJiri Pirko mlxsw_sp_acl_tcam_vgroup_add(struct mlxsw_sp *mlxsw_sp,
25822a67766SJiri Pirko 			     struct mlxsw_sp_acl_tcam *tcam,
2592802aadfSJiri Pirko 			     struct mlxsw_sp_acl_tcam_vgroup *vgroup,
26022a67766SJiri Pirko 			     const struct mlxsw_sp_acl_tcam_pattern *patterns,
261e2f2a1fdSJiri Pirko 			     unsigned int patterns_count,
2626b861682SJiri Pirko 			     struct mlxsw_afk_element_usage *tmplt_elusage,
263593bb843SJiri Pirko 			     bool vregion_rehash_enabled,
264593bb843SJiri Pirko 			     unsigned int *p_min_prio,
265593bb843SJiri Pirko 			     unsigned int *p_max_prio)
26622a67766SJiri Pirko {
26722a67766SJiri Pirko 	int err;
26822a67766SJiri Pirko 
2692802aadfSJiri Pirko 	vgroup->patterns = patterns;
2702802aadfSJiri Pirko 	vgroup->patterns_count = patterns_count;
2716b861682SJiri Pirko 	vgroup->vregion_rehash_enabled = vregion_rehash_enabled;
272593bb843SJiri Pirko 	vgroup->p_min_prio = p_min_prio;
273593bb843SJiri Pirko 	vgroup->p_max_prio = p_max_prio;
2746b861682SJiri Pirko 
275e2f2a1fdSJiri Pirko 	if (tmplt_elusage) {
2762802aadfSJiri Pirko 		vgroup->tmplt_elusage_set = true;
2772802aadfSJiri Pirko 		memcpy(&vgroup->tmplt_elusage, tmplt_elusage,
2782802aadfSJiri Pirko 		       sizeof(vgroup->tmplt_elusage));
279e2f2a1fdSJiri Pirko 	}
2802802aadfSJiri Pirko 	INIT_LIST_HEAD(&vgroup->vregion_list);
2812802aadfSJiri Pirko 
2822802aadfSJiri Pirko 	err = mlxsw_sp_acl_tcam_group_add(tcam, &vgroup->group);
28322a67766SJiri Pirko 	if (err)
28422a67766SJiri Pirko 		return err;
28522a67766SJiri Pirko 
2862802aadfSJiri Pirko 	err = rhashtable_init(&vgroup->vchunk_ht,
287b2d6b4d2SJiri Pirko 			      &mlxsw_sp_acl_tcam_vchunk_ht_params);
28822a67766SJiri Pirko 	if (err)
28922a67766SJiri Pirko 		goto err_rhashtable_init;
29022a67766SJiri Pirko 
29122a67766SJiri Pirko 	return 0;
29222a67766SJiri Pirko 
29322a67766SJiri Pirko err_rhashtable_init:
2942802aadfSJiri Pirko 	mlxsw_sp_acl_tcam_group_del(&vgroup->group);
29522a67766SJiri Pirko 	return err;
29622a67766SJiri Pirko }
29722a67766SJiri Pirko 
2982802aadfSJiri Pirko static void
mlxsw_sp_acl_tcam_vgroup_del(struct mlxsw_sp_acl_tcam_vgroup * vgroup)2992802aadfSJiri Pirko mlxsw_sp_acl_tcam_vgroup_del(struct mlxsw_sp_acl_tcam_vgroup *vgroup)
30022a67766SJiri Pirko {
3012802aadfSJiri Pirko 	rhashtable_destroy(&vgroup->vchunk_ht);
3022802aadfSJiri Pirko 	mlxsw_sp_acl_tcam_group_del(&vgroup->group);
3032802aadfSJiri Pirko 	WARN_ON(!list_empty(&vgroup->vregion_list));
30422a67766SJiri Pirko }
30522a67766SJiri Pirko 
30622a67766SJiri Pirko static int
mlxsw_sp_acl_tcam_group_bind(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_group * group,struct mlxsw_sp_port * mlxsw_sp_port,bool ingress)30722a67766SJiri Pirko mlxsw_sp_acl_tcam_group_bind(struct mlxsw_sp *mlxsw_sp,
30822a67766SJiri Pirko 			     struct mlxsw_sp_acl_tcam_group *group,
3094b23258dSJiri Pirko 			     struct mlxsw_sp_port *mlxsw_sp_port,
3104b23258dSJiri Pirko 			     bool ingress)
31122a67766SJiri Pirko {
31222a67766SJiri Pirko 	char ppbt_pl[MLXSW_REG_PPBT_LEN];
31322a67766SJiri Pirko 
31402caf499SJiri Pirko 	mlxsw_reg_ppbt_pack(ppbt_pl, ingress ? MLXSW_REG_PXBT_E_IACL :
31522a67766SJiri Pirko 					       MLXSW_REG_PXBT_E_EACL,
31602caf499SJiri Pirko 			    MLXSW_REG_PXBT_OP_BIND, mlxsw_sp_port->local_port,
31722a67766SJiri Pirko 			    group->id);
31822a67766SJiri Pirko 	return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppbt), ppbt_pl);
31922a67766SJiri Pirko }
32022a67766SJiri Pirko 
32122a67766SJiri Pirko static void
mlxsw_sp_acl_tcam_group_unbind(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_group * group,struct mlxsw_sp_port * mlxsw_sp_port,bool ingress)32222a67766SJiri Pirko mlxsw_sp_acl_tcam_group_unbind(struct mlxsw_sp *mlxsw_sp,
32302caf499SJiri Pirko 			       struct mlxsw_sp_acl_tcam_group *group,
3244b23258dSJiri Pirko 			       struct mlxsw_sp_port *mlxsw_sp_port,
3254b23258dSJiri Pirko 			       bool ingress)
32622a67766SJiri Pirko {
32722a67766SJiri Pirko 	char ppbt_pl[MLXSW_REG_PPBT_LEN];
32822a67766SJiri Pirko 
32902caf499SJiri Pirko 	mlxsw_reg_ppbt_pack(ppbt_pl, ingress ? MLXSW_REG_PXBT_E_IACL :
33022a67766SJiri Pirko 					       MLXSW_REG_PXBT_E_EACL,
33102caf499SJiri Pirko 			    MLXSW_REG_PXBT_OP_UNBIND, mlxsw_sp_port->local_port,
33222a67766SJiri Pirko 			    group->id);
33322a67766SJiri Pirko 	mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppbt), ppbt_pl);
33422a67766SJiri Pirko }
33522a67766SJiri Pirko 
3360ade3b64SJiri Pirko static u16
mlxsw_sp_acl_tcam_group_id(struct mlxsw_sp_acl_tcam_group * group)3370ade3b64SJiri Pirko mlxsw_sp_acl_tcam_group_id(struct mlxsw_sp_acl_tcam_group *group)
3380ade3b64SJiri Pirko {
3390ade3b64SJiri Pirko 	return group->id;
3400ade3b64SJiri Pirko }
3410ade3b64SJiri Pirko 
34222a67766SJiri Pirko static unsigned int
mlxsw_sp_acl_tcam_vregion_prio(struct mlxsw_sp_acl_tcam_vregion * vregion)3430f54236dSJiri Pirko mlxsw_sp_acl_tcam_vregion_prio(struct mlxsw_sp_acl_tcam_vregion *vregion)
34422a67766SJiri Pirko {
345b2d6b4d2SJiri Pirko 	struct mlxsw_sp_acl_tcam_vchunk *vchunk;
34622a67766SJiri Pirko 
347b2d6b4d2SJiri Pirko 	if (list_empty(&vregion->vchunk_list))
34822a67766SJiri Pirko 		return 0;
349b2d6b4d2SJiri Pirko 	/* As a priority of a vregion, return priority of the first vchunk */
350b2d6b4d2SJiri Pirko 	vchunk = list_first_entry(&vregion->vchunk_list,
351b2d6b4d2SJiri Pirko 				  typeof(*vchunk), list);
352b2d6b4d2SJiri Pirko 	return vchunk->priority;
35322a67766SJiri Pirko }
35422a67766SJiri Pirko 
35522a67766SJiri Pirko static unsigned int
mlxsw_sp_acl_tcam_vregion_max_prio(struct mlxsw_sp_acl_tcam_vregion * vregion)3560f54236dSJiri Pirko mlxsw_sp_acl_tcam_vregion_max_prio(struct mlxsw_sp_acl_tcam_vregion *vregion)
35722a67766SJiri Pirko {
358b2d6b4d2SJiri Pirko 	struct mlxsw_sp_acl_tcam_vchunk *vchunk;
35922a67766SJiri Pirko 
360b2d6b4d2SJiri Pirko 	if (list_empty(&vregion->vchunk_list))
36122a67766SJiri Pirko 		return 0;
362b2d6b4d2SJiri Pirko 	vchunk = list_last_entry(&vregion->vchunk_list,
363b2d6b4d2SJiri Pirko 				 typeof(*vchunk), list);
364b2d6b4d2SJiri Pirko 	return vchunk->priority;
36522a67766SJiri Pirko }
36622a67766SJiri Pirko 
367593bb843SJiri Pirko static void
mlxsw_sp_acl_tcam_vgroup_prio_update(struct mlxsw_sp_acl_tcam_vgroup * vgroup)368593bb843SJiri Pirko mlxsw_sp_acl_tcam_vgroup_prio_update(struct mlxsw_sp_acl_tcam_vgroup *vgroup)
369593bb843SJiri Pirko {
370593bb843SJiri Pirko 	struct mlxsw_sp_acl_tcam_vregion *vregion;
371593bb843SJiri Pirko 
372593bb843SJiri Pirko 	if (list_empty(&vgroup->vregion_list))
373593bb843SJiri Pirko 		return;
374593bb843SJiri Pirko 	vregion = list_first_entry(&vgroup->vregion_list,
375593bb843SJiri Pirko 				   typeof(*vregion), list);
376593bb843SJiri Pirko 	*vgroup->p_min_prio = mlxsw_sp_acl_tcam_vregion_prio(vregion);
377593bb843SJiri Pirko 	vregion = list_last_entry(&vgroup->vregion_list,
378593bb843SJiri Pirko 				  typeof(*vregion), list);
379593bb843SJiri Pirko 	*vgroup->p_max_prio = mlxsw_sp_acl_tcam_vregion_max_prio(vregion);
380593bb843SJiri Pirko }
381593bb843SJiri Pirko 
38222a67766SJiri Pirko static int
mlxsw_sp_acl_tcam_group_region_attach(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_group * group,struct mlxsw_sp_acl_tcam_region * region,unsigned int priority,struct mlxsw_sp_acl_tcam_region * next_region)38322a67766SJiri Pirko mlxsw_sp_acl_tcam_group_region_attach(struct mlxsw_sp *mlxsw_sp,
3842802aadfSJiri Pirko 				      struct mlxsw_sp_acl_tcam_group *group,
3852802aadfSJiri Pirko 				      struct mlxsw_sp_acl_tcam_region *region,
38679604b6eSJiri Pirko 				      unsigned int priority,
3872802aadfSJiri Pirko 				      struct mlxsw_sp_acl_tcam_region *next_region)
38822a67766SJiri Pirko {
3892802aadfSJiri Pirko 	struct mlxsw_sp_acl_tcam_region *region2;
3902802aadfSJiri Pirko 	struct list_head *pos;
39122a67766SJiri Pirko 	int err;
39222a67766SJiri Pirko 
3935ec2ee28SJiri Pirko 	mutex_lock(&group->lock);
3945ec2ee28SJiri Pirko 	if (group->region_count == group->tcam->max_group_size) {
3955ec2ee28SJiri Pirko 		err = -ENOBUFS;
3965ec2ee28SJiri Pirko 		goto err_region_count_check;
3975ec2ee28SJiri Pirko 	}
39822a67766SJiri Pirko 
3992802aadfSJiri Pirko 	if (next_region) {
4002802aadfSJiri Pirko 		/* If the next region is defined, place the new one
4012802aadfSJiri Pirko 		 * before it. The next one is a sibling.
4022802aadfSJiri Pirko 		 */
4032802aadfSJiri Pirko 		pos = &next_region->list;
4042802aadfSJiri Pirko 	} else {
4052802aadfSJiri Pirko 		/* Position the region inside the list according to priority */
4062802aadfSJiri Pirko 		list_for_each(pos, &group->region_list) {
4072802aadfSJiri Pirko 			region2 = list_entry(pos, typeof(*region2), list);
4082802aadfSJiri Pirko 			if (mlxsw_sp_acl_tcam_vregion_prio(region2->vregion) >
40979604b6eSJiri Pirko 			    priority)
4102802aadfSJiri Pirko 				break;
4112802aadfSJiri Pirko 		}
4122802aadfSJiri Pirko 	}
4132802aadfSJiri Pirko 	list_add_tail(&region->list, pos);
4142802aadfSJiri Pirko 	region->group = group;
4152802aadfSJiri Pirko 
41622a67766SJiri Pirko 	err = mlxsw_sp_acl_tcam_group_update(mlxsw_sp, group);
41722a67766SJiri Pirko 	if (err)
4182802aadfSJiri Pirko 		goto err_group_update;
4190f54236dSJiri Pirko 
4200f54236dSJiri Pirko 	group->region_count++;
4215ec2ee28SJiri Pirko 	mutex_unlock(&group->lock);
4220f54236dSJiri Pirko 	return 0;
4232802aadfSJiri Pirko 
4242802aadfSJiri Pirko err_group_update:
4252802aadfSJiri Pirko 	list_del(&region->list);
4265ec2ee28SJiri Pirko err_region_count_check:
4275ec2ee28SJiri Pirko 	mutex_unlock(&group->lock);
4282802aadfSJiri Pirko 	return err;
42922a67766SJiri Pirko }
43022a67766SJiri Pirko 
43122a67766SJiri Pirko static void
mlxsw_sp_acl_tcam_group_region_detach(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_region * region)43222a67766SJiri Pirko mlxsw_sp_acl_tcam_group_region_detach(struct mlxsw_sp *mlxsw_sp,
43322a67766SJiri Pirko 				      struct mlxsw_sp_acl_tcam_region *region)
43422a67766SJiri Pirko {
4352802aadfSJiri Pirko 	struct mlxsw_sp_acl_tcam_group *group = region->group;
43622a67766SJiri Pirko 
4375ec2ee28SJiri Pirko 	mutex_lock(&group->lock);
4382802aadfSJiri Pirko 	list_del(&region->list);
4390f54236dSJiri Pirko 	group->region_count--;
44022a67766SJiri Pirko 	mlxsw_sp_acl_tcam_group_update(mlxsw_sp, group);
4415ec2ee28SJiri Pirko 	mutex_unlock(&group->lock);
44222a67766SJiri Pirko }
44322a67766SJiri Pirko 
4440f54236dSJiri Pirko static int
mlxsw_sp_acl_tcam_vgroup_vregion_attach(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vgroup * vgroup,struct mlxsw_sp_acl_tcam_vregion * vregion,unsigned int priority)4452802aadfSJiri Pirko mlxsw_sp_acl_tcam_vgroup_vregion_attach(struct mlxsw_sp *mlxsw_sp,
4462802aadfSJiri Pirko 					struct mlxsw_sp_acl_tcam_vgroup *vgroup,
44779604b6eSJiri Pirko 					struct mlxsw_sp_acl_tcam_vregion *vregion,
44879604b6eSJiri Pirko 					unsigned int priority)
4490f54236dSJiri Pirko {
4500f54236dSJiri Pirko 	struct mlxsw_sp_acl_tcam_vregion *vregion2;
4510f54236dSJiri Pirko 	struct list_head *pos;
4520f54236dSJiri Pirko 	int err;
4530f54236dSJiri Pirko 
4540f54236dSJiri Pirko 	/* Position the vregion inside the list according to priority */
4552802aadfSJiri Pirko 	list_for_each(pos, &vgroup->vregion_list) {
4560f54236dSJiri Pirko 		vregion2 = list_entry(pos, typeof(*vregion2), list);
45779604b6eSJiri Pirko 		if (mlxsw_sp_acl_tcam_vregion_prio(vregion2) > priority)
4580f54236dSJiri Pirko 			break;
4590f54236dSJiri Pirko 	}
4600f54236dSJiri Pirko 	list_add_tail(&vregion->list, pos);
4610f54236dSJiri Pirko 
4622802aadfSJiri Pirko 	err = mlxsw_sp_acl_tcam_group_region_attach(mlxsw_sp, &vgroup->group,
46379604b6eSJiri Pirko 						    vregion->region,
46479604b6eSJiri Pirko 						    priority, NULL);
4650f54236dSJiri Pirko 	if (err)
4660f54236dSJiri Pirko 		goto err_region_attach;
4670f54236dSJiri Pirko 
4680f54236dSJiri Pirko 	return 0;
4690f54236dSJiri Pirko 
4700f54236dSJiri Pirko err_region_attach:
4710f54236dSJiri Pirko 	list_del(&vregion->list);
4720f54236dSJiri Pirko 	return err;
4730f54236dSJiri Pirko }
4740f54236dSJiri Pirko 
4750f54236dSJiri Pirko static void
mlxsw_sp_acl_tcam_vgroup_vregion_detach(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vregion * vregion)4762802aadfSJiri Pirko mlxsw_sp_acl_tcam_vgroup_vregion_detach(struct mlxsw_sp *mlxsw_sp,
4770f54236dSJiri Pirko 					struct mlxsw_sp_acl_tcam_vregion *vregion)
4780f54236dSJiri Pirko {
4790f54236dSJiri Pirko 	list_del(&vregion->list);
480e5e7962eSJiri Pirko 	if (vregion->region2)
481e5e7962eSJiri Pirko 		mlxsw_sp_acl_tcam_group_region_detach(mlxsw_sp,
482e5e7962eSJiri Pirko 						      vregion->region2);
4830f54236dSJiri Pirko 	mlxsw_sp_acl_tcam_group_region_detach(mlxsw_sp, vregion->region);
4840f54236dSJiri Pirko }
4850f54236dSJiri Pirko 
4860f54236dSJiri Pirko static struct mlxsw_sp_acl_tcam_vregion *
mlxsw_sp_acl_tcam_vgroup_vregion_find(struct mlxsw_sp_acl_tcam_vgroup * vgroup,unsigned int priority,struct mlxsw_afk_element_usage * elusage,bool * p_need_split)4872802aadfSJiri Pirko mlxsw_sp_acl_tcam_vgroup_vregion_find(struct mlxsw_sp_acl_tcam_vgroup *vgroup,
48822a67766SJiri Pirko 				      unsigned int priority,
48922a67766SJiri Pirko 				      struct mlxsw_afk_element_usage *elusage,
49022a67766SJiri Pirko 				      bool *p_need_split)
49122a67766SJiri Pirko {
4920f54236dSJiri Pirko 	struct mlxsw_sp_acl_tcam_vregion *vregion, *vregion2;
49322a67766SJiri Pirko 	struct list_head *pos;
49422a67766SJiri Pirko 	bool issubset;
49522a67766SJiri Pirko 
4962802aadfSJiri Pirko 	list_for_each(pos, &vgroup->vregion_list) {
4970f54236dSJiri Pirko 		vregion = list_entry(pos, typeof(*vregion), list);
49822a67766SJiri Pirko 
49922a67766SJiri Pirko 		/* First, check if the requested priority does not rather belong
5000f54236dSJiri Pirko 		 * under some of the next vregions.
50122a67766SJiri Pirko 		 */
5022802aadfSJiri Pirko 		if (pos->next != &vgroup->vregion_list) { /* not last */
5030f54236dSJiri Pirko 			vregion2 = list_entry(pos->next, typeof(*vregion2),
5040f54236dSJiri Pirko 					      list);
5050f54236dSJiri Pirko 			if (priority >=
5060f54236dSJiri Pirko 			    mlxsw_sp_acl_tcam_vregion_prio(vregion2))
50722a67766SJiri Pirko 				continue;
50822a67766SJiri Pirko 		}
50922a67766SJiri Pirko 
5100f54236dSJiri Pirko 		issubset = mlxsw_afk_key_info_subset(vregion->key_info,
5110f54236dSJiri Pirko 						     elusage);
51222a67766SJiri Pirko 
51322a67766SJiri Pirko 		/* If requested element usage would not fit and the priority
5140f54236dSJiri Pirko 		 * is lower than the currently inspected vregion we cannot
5150f54236dSJiri Pirko 		 * use this region, so return NULL to indicate new vregion has
51622a67766SJiri Pirko 		 * to be created.
51722a67766SJiri Pirko 		 */
51822a67766SJiri Pirko 		if (!issubset &&
5190f54236dSJiri Pirko 		    priority < mlxsw_sp_acl_tcam_vregion_prio(vregion))
52022a67766SJiri Pirko 			return NULL;
52122a67766SJiri Pirko 
52222a67766SJiri Pirko 		/* If requested element usage would not fit and the priority
5230f54236dSJiri Pirko 		 * is higher than the currently inspected vregion we cannot
5240f54236dSJiri Pirko 		 * use this vregion. There is still some hope that the next
5250f54236dSJiri Pirko 		 * vregion would be the fit. So let it be processed and
52622a67766SJiri Pirko 		 * eventually break at the check right above this.
52722a67766SJiri Pirko 		 */
52822a67766SJiri Pirko 		if (!issubset &&
5290f54236dSJiri Pirko 		    priority > mlxsw_sp_acl_tcam_vregion_max_prio(vregion))
53022a67766SJiri Pirko 			continue;
53122a67766SJiri Pirko 
5320f54236dSJiri Pirko 		/* Indicate if the vregion needs to be split in order to add
53322a67766SJiri Pirko 		 * the requested priority. Split is needed when requested
5340f54236dSJiri Pirko 		 * element usage won't fit into the found vregion.
53522a67766SJiri Pirko 		 */
53622a67766SJiri Pirko 		*p_need_split = !issubset;
5370f54236dSJiri Pirko 		return vregion;
53822a67766SJiri Pirko 	}
5390f54236dSJiri Pirko 	return NULL; /* New vregion has to be created. */
54022a67766SJiri Pirko }
54122a67766SJiri Pirko 
54222a67766SJiri Pirko static void
mlxsw_sp_acl_tcam_vgroup_use_patterns(struct mlxsw_sp_acl_tcam_vgroup * vgroup,struct mlxsw_afk_element_usage * elusage,struct mlxsw_afk_element_usage * out)5432802aadfSJiri Pirko mlxsw_sp_acl_tcam_vgroup_use_patterns(struct mlxsw_sp_acl_tcam_vgroup *vgroup,
54422a67766SJiri Pirko 				      struct mlxsw_afk_element_usage *elusage,
54522a67766SJiri Pirko 				      struct mlxsw_afk_element_usage *out)
54622a67766SJiri Pirko {
54722a67766SJiri Pirko 	const struct mlxsw_sp_acl_tcam_pattern *pattern;
54822a67766SJiri Pirko 	int i;
54922a67766SJiri Pirko 
550e2f2a1fdSJiri Pirko 	/* In case the template is set, we don't have to look up the pattern
551e2f2a1fdSJiri Pirko 	 * and just use the template.
552e2f2a1fdSJiri Pirko 	 */
5532802aadfSJiri Pirko 	if (vgroup->tmplt_elusage_set) {
5542802aadfSJiri Pirko 		memcpy(out, &vgroup->tmplt_elusage, sizeof(*out));
555e2f2a1fdSJiri Pirko 		WARN_ON(!mlxsw_afk_element_usage_subset(elusage, out));
556e2f2a1fdSJiri Pirko 		return;
557e2f2a1fdSJiri Pirko 	}
558e2f2a1fdSJiri Pirko 
5592802aadfSJiri Pirko 	for (i = 0; i < vgroup->patterns_count; i++) {
5602802aadfSJiri Pirko 		pattern = &vgroup->patterns[i];
56122a67766SJiri Pirko 		mlxsw_afk_element_usage_fill(out, pattern->elements,
56222a67766SJiri Pirko 					     pattern->elements_count);
56322a67766SJiri Pirko 		if (mlxsw_afk_element_usage_subset(elusage, out))
56422a67766SJiri Pirko 			return;
56522a67766SJiri Pirko 	}
56622a67766SJiri Pirko 	memcpy(out, elusage, sizeof(*out));
56722a67766SJiri Pirko }
56822a67766SJiri Pirko 
56922a67766SJiri Pirko static int
mlxsw_sp_acl_tcam_region_alloc(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_region * region)57022a67766SJiri Pirko mlxsw_sp_acl_tcam_region_alloc(struct mlxsw_sp *mlxsw_sp,
57122a67766SJiri Pirko 			       struct mlxsw_sp_acl_tcam_region *region)
57222a67766SJiri Pirko {
57322a67766SJiri Pirko 	struct mlxsw_afk_key_info *key_info = region->key_info;
57422a67766SJiri Pirko 	char ptar_pl[MLXSW_REG_PTAR_LEN];
57522a67766SJiri Pirko 	unsigned int encodings_count;
57622a67766SJiri Pirko 	int i;
57722a67766SJiri Pirko 	int err;
57822a67766SJiri Pirko 
57922a67766SJiri Pirko 	mlxsw_reg_ptar_pack(ptar_pl, MLXSW_REG_PTAR_OP_ALLOC,
58045e0620dSJiri Pirko 			    region->key_type,
58122a67766SJiri Pirko 			    MLXSW_SP_ACL_TCAM_REGION_BASE_COUNT,
58222a67766SJiri Pirko 			    region->id, region->tcam_region_info);
58322a67766SJiri Pirko 	encodings_count = mlxsw_afk_key_info_blocks_count_get(key_info);
58422a67766SJiri Pirko 	for (i = 0; i < encodings_count; i++) {
58522a67766SJiri Pirko 		u16 encoding;
58622a67766SJiri Pirko 
58722a67766SJiri Pirko 		encoding = mlxsw_afk_key_info_block_encoding_get(key_info, i);
58822a67766SJiri Pirko 		mlxsw_reg_ptar_key_id_pack(ptar_pl, i, encoding);
58922a67766SJiri Pirko 	}
59022a67766SJiri Pirko 	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptar), ptar_pl);
59122a67766SJiri Pirko 	if (err)
59222a67766SJiri Pirko 		return err;
59322a67766SJiri Pirko 	mlxsw_reg_ptar_unpack(ptar_pl, region->tcam_region_info);
59422a67766SJiri Pirko 	return 0;
59522a67766SJiri Pirko }
59622a67766SJiri Pirko 
59722a67766SJiri Pirko static void
mlxsw_sp_acl_tcam_region_free(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_region * region)59822a67766SJiri Pirko mlxsw_sp_acl_tcam_region_free(struct mlxsw_sp *mlxsw_sp,
59922a67766SJiri Pirko 			      struct mlxsw_sp_acl_tcam_region *region)
60022a67766SJiri Pirko {
60122a67766SJiri Pirko 	char ptar_pl[MLXSW_REG_PTAR_LEN];
60222a67766SJiri Pirko 
60345e0620dSJiri Pirko 	mlxsw_reg_ptar_pack(ptar_pl, MLXSW_REG_PTAR_OP_FREE,
60445e0620dSJiri Pirko 			    region->key_type, 0, region->id,
60522a67766SJiri Pirko 			    region->tcam_region_info);
60622a67766SJiri Pirko 	mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptar), ptar_pl);
60722a67766SJiri Pirko }
60822a67766SJiri Pirko 
60922a67766SJiri Pirko static int
mlxsw_sp_acl_tcam_region_enable(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_region * region)61022a67766SJiri Pirko mlxsw_sp_acl_tcam_region_enable(struct mlxsw_sp *mlxsw_sp,
61122a67766SJiri Pirko 				struct mlxsw_sp_acl_tcam_region *region)
61222a67766SJiri Pirko {
61322a67766SJiri Pirko 	char pacl_pl[MLXSW_REG_PACL_LEN];
61422a67766SJiri Pirko 
61522a67766SJiri Pirko 	mlxsw_reg_pacl_pack(pacl_pl, region->id, true,
61622a67766SJiri Pirko 			    region->tcam_region_info);
61722a67766SJiri Pirko 	return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pacl), pacl_pl);
61822a67766SJiri Pirko }
61922a67766SJiri Pirko 
62022a67766SJiri Pirko static void
mlxsw_sp_acl_tcam_region_disable(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_region * region)62122a67766SJiri Pirko mlxsw_sp_acl_tcam_region_disable(struct mlxsw_sp *mlxsw_sp,
62222a67766SJiri Pirko 				 struct mlxsw_sp_acl_tcam_region *region)
62322a67766SJiri Pirko {
62422a67766SJiri Pirko 	char pacl_pl[MLXSW_REG_PACL_LEN];
62522a67766SJiri Pirko 
62622a67766SJiri Pirko 	mlxsw_reg_pacl_pack(pacl_pl, region->id, false,
62722a67766SJiri Pirko 			    region->tcam_region_info);
62822a67766SJiri Pirko 	mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pacl), pacl_pl);
62922a67766SJiri Pirko }
63022a67766SJiri Pirko 
63122a67766SJiri Pirko static struct mlxsw_sp_acl_tcam_region *
mlxsw_sp_acl_tcam_region_create(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam * tcam,struct mlxsw_sp_acl_tcam_vregion * vregion,void * hints_priv)63222a67766SJiri Pirko mlxsw_sp_acl_tcam_region_create(struct mlxsw_sp *mlxsw_sp,
63322a67766SJiri Pirko 				struct mlxsw_sp_acl_tcam *tcam,
634e5e7962eSJiri Pirko 				struct mlxsw_sp_acl_tcam_vregion *vregion,
635e5e7962eSJiri Pirko 				void *hints_priv)
63622a67766SJiri Pirko {
63764eccd00SJiri Pirko 	const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
63822a67766SJiri Pirko 	struct mlxsw_sp_acl_tcam_region *region;
63922a67766SJiri Pirko 	int err;
64022a67766SJiri Pirko 
64164eccd00SJiri Pirko 	region = kzalloc(sizeof(*region) + ops->region_priv_size, GFP_KERNEL);
64222a67766SJiri Pirko 	if (!region)
64322a67766SJiri Pirko 		return ERR_PTR(-ENOMEM);
64422a67766SJiri Pirko 	region->mlxsw_sp = mlxsw_sp;
6450f54236dSJiri Pirko 	region->vregion = vregion;
6460f54236dSJiri Pirko 	region->key_info = vregion->key_info;
64722a67766SJiri Pirko 
64822a67766SJiri Pirko 	err = mlxsw_sp_acl_tcam_region_id_get(tcam, &region->id);
64922a67766SJiri Pirko 	if (err)
65022a67766SJiri Pirko 		goto err_region_id_get;
65122a67766SJiri Pirko 
652a6b9c87dSIdo Schimmel 	err = ops->region_associate(mlxsw_sp, region);
653a6b9c87dSIdo Schimmel 	if (err)
654a6b9c87dSIdo Schimmel 		goto err_tcam_region_associate;
655a6b9c87dSIdo Schimmel 
65664eccd00SJiri Pirko 	region->key_type = ops->key_type;
65722a67766SJiri Pirko 	err = mlxsw_sp_acl_tcam_region_alloc(mlxsw_sp, region);
65822a67766SJiri Pirko 	if (err)
65922a67766SJiri Pirko 		goto err_tcam_region_alloc;
66022a67766SJiri Pirko 
66122a67766SJiri Pirko 	err = mlxsw_sp_acl_tcam_region_enable(mlxsw_sp, region);
66222a67766SJiri Pirko 	if (err)
66322a67766SJiri Pirko 		goto err_tcam_region_enable;
66422a67766SJiri Pirko 
665a339bf8aSJiri Pirko 	err = ops->region_init(mlxsw_sp, region->priv, tcam->priv,
666e5e7962eSJiri Pirko 			       region, hints_priv);
66722a67766SJiri Pirko 	if (err)
66864eccd00SJiri Pirko 		goto err_tcam_region_init;
66922a67766SJiri Pirko 
67022a67766SJiri Pirko 	return region;
67122a67766SJiri Pirko 
67264eccd00SJiri Pirko err_tcam_region_init:
67322a67766SJiri Pirko 	mlxsw_sp_acl_tcam_region_disable(mlxsw_sp, region);
67422a67766SJiri Pirko err_tcam_region_enable:
67522a67766SJiri Pirko 	mlxsw_sp_acl_tcam_region_free(mlxsw_sp, region);
67622a67766SJiri Pirko err_tcam_region_alloc:
677a6b9c87dSIdo Schimmel err_tcam_region_associate:
67822a67766SJiri Pirko 	mlxsw_sp_acl_tcam_region_id_put(tcam, region->id);
67922a67766SJiri Pirko err_region_id_get:
68022a67766SJiri Pirko 	kfree(region);
68122a67766SJiri Pirko 	return ERR_PTR(err);
68222a67766SJiri Pirko }
68322a67766SJiri Pirko 
68422a67766SJiri Pirko static void
mlxsw_sp_acl_tcam_region_destroy(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_region * region)68522a67766SJiri Pirko mlxsw_sp_acl_tcam_region_destroy(struct mlxsw_sp *mlxsw_sp,
68622a67766SJiri Pirko 				 struct mlxsw_sp_acl_tcam_region *region)
68722a67766SJiri Pirko {
688817840d1SIdo Schimmel 	struct mlxsw_sp_acl_tcam *tcam = mlxsw_sp_acl_to_tcam(mlxsw_sp->acl);
68964eccd00SJiri Pirko 	const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
69064eccd00SJiri Pirko 
69164eccd00SJiri Pirko 	ops->region_fini(mlxsw_sp, region->priv);
69222a67766SJiri Pirko 	mlxsw_sp_acl_tcam_region_disable(mlxsw_sp, region);
69322a67766SJiri Pirko 	mlxsw_sp_acl_tcam_region_free(mlxsw_sp, region);
694817840d1SIdo Schimmel 	mlxsw_sp_acl_tcam_region_id_put(tcam, region->id);
69522a67766SJiri Pirko 	kfree(region);
69622a67766SJiri Pirko }
69722a67766SJiri Pirko 
698e5e7962eSJiri Pirko static void
mlxsw_sp_acl_tcam_vregion_rehash_work_schedule(struct mlxsw_sp_acl_tcam_vregion * vregion)699e5e7962eSJiri Pirko mlxsw_sp_acl_tcam_vregion_rehash_work_schedule(struct mlxsw_sp_acl_tcam_vregion *vregion)
700e5e7962eSJiri Pirko {
701e5e7962eSJiri Pirko 	unsigned long interval = vregion->tcam->vregion_rehash_intrvl;
702e5e7962eSJiri Pirko 
703e5e7962eSJiri Pirko 	if (!interval)
704e5e7962eSJiri Pirko 		return;
705f9b274ceSJiri Pirko 	mlxsw_core_schedule_dw(&vregion->rehash.dw,
706e5e7962eSJiri Pirko 			       msecs_to_jiffies(interval));
707e5e7962eSJiri Pirko }
708e5e7962eSJiri Pirko 
709b2c091ceSJiri Pirko static void
710e5e7962eSJiri Pirko mlxsw_sp_acl_tcam_vregion_rehash(struct mlxsw_sp *mlxsw_sp,
711c9c9af91SJiri Pirko 				 struct mlxsw_sp_acl_tcam_vregion *vregion,
712c9c9af91SJiri Pirko 				 int *credits);
713e5e7962eSJiri Pirko 
mlxsw_sp_acl_tcam_vregion_rehash_work(struct work_struct * work)714e5e7962eSJiri Pirko static void mlxsw_sp_acl_tcam_vregion_rehash_work(struct work_struct *work)
715e5e7962eSJiri Pirko {
716e5e7962eSJiri Pirko 	struct mlxsw_sp_acl_tcam_vregion *vregion =
717e5e7962eSJiri Pirko 		container_of(work, struct mlxsw_sp_acl_tcam_vregion,
718f9b274ceSJiri Pirko 			     rehash.dw.work);
719c9c9af91SJiri Pirko 	int credits = MLXSW_SP_ACL_TCAM_VREGION_REHASH_CREDITS;
720e5e7962eSJiri Pirko 
721136fc524SIdo Schimmel 	mutex_lock(&vregion->lock);
722b2c091ceSJiri Pirko 	mlxsw_sp_acl_tcam_vregion_rehash(vregion->mlxsw_sp, vregion, &credits);
723136fc524SIdo Schimmel 	mutex_unlock(&vregion->lock);
724c9c9af91SJiri Pirko 	if (credits < 0)
725c9c9af91SJiri Pirko 		/* Rehash gone out of credits so it was interrupted.
726c9c9af91SJiri Pirko 		 * Schedule the work as soon as possible to continue.
727c9c9af91SJiri Pirko 		 */
728c9c9af91SJiri Pirko 		mlxsw_core_schedule_dw(&vregion->rehash.dw, 0);
729c9c9af91SJiri Pirko 	else
730e5e7962eSJiri Pirko 		mlxsw_sp_acl_tcam_vregion_rehash_work_schedule(vregion);
731e5e7962eSJiri Pirko }
732e5e7962eSJiri Pirko 
7336f9579d4SJiri Pirko static void
mlxsw_sp_acl_tcam_rehash_ctx_vchunk_reset(struct mlxsw_sp_acl_tcam_rehash_ctx * ctx)734e8904560SIdo Schimmel mlxsw_sp_acl_tcam_rehash_ctx_vchunk_reset(struct mlxsw_sp_acl_tcam_rehash_ctx *ctx)
735e8904560SIdo Schimmel {
736e8904560SIdo Schimmel 	/* The entry markers are relative to the current chunk and therefore
737e8904560SIdo Schimmel 	 * needs to be reset together with the chunk marker.
738e8904560SIdo Schimmel 	 */
739e8904560SIdo Schimmel 	ctx->current_vchunk = NULL;
740e8904560SIdo Schimmel 	ctx->start_ventry = NULL;
741e8904560SIdo Schimmel 	ctx->stop_ventry = NULL;
742e8904560SIdo Schimmel }
743e8904560SIdo Schimmel 
744e8904560SIdo Schimmel static void
mlxsw_sp_acl_tcam_rehash_ctx_vchunk_changed(struct mlxsw_sp_acl_tcam_vchunk * vchunk)7456f9579d4SJiri Pirko mlxsw_sp_acl_tcam_rehash_ctx_vchunk_changed(struct mlxsw_sp_acl_tcam_vchunk *vchunk)
7466f9579d4SJiri Pirko {
7476f9579d4SJiri Pirko 	struct mlxsw_sp_acl_tcam_vregion *vregion = vchunk->vregion;
7486f9579d4SJiri Pirko 
7496f9579d4SJiri Pirko 	/* If a rule was added or deleted from vchunk which is currently
7506f9579d4SJiri Pirko 	 * under rehash migration, we have to reset the ventry pointers
7516f9579d4SJiri Pirko 	 * to make sure all rules are properly migrated.
7526f9579d4SJiri Pirko 	 */
7536f9579d4SJiri Pirko 	if (vregion->rehash.ctx.current_vchunk == vchunk) {
7546f9579d4SJiri Pirko 		vregion->rehash.ctx.start_ventry = NULL;
7556f9579d4SJiri Pirko 		vregion->rehash.ctx.stop_ventry = NULL;
7566f9579d4SJiri Pirko 	}
7576f9579d4SJiri Pirko }
7586f9579d4SJiri Pirko 
7596f9579d4SJiri Pirko static void
mlxsw_sp_acl_tcam_rehash_ctx_vregion_changed(struct mlxsw_sp_acl_tcam_vregion * vregion)7606f9579d4SJiri Pirko mlxsw_sp_acl_tcam_rehash_ctx_vregion_changed(struct mlxsw_sp_acl_tcam_vregion *vregion)
7616f9579d4SJiri Pirko {
7626f9579d4SJiri Pirko 	/* If a chunk was added or deleted from vregion we have to reset
7636f9579d4SJiri Pirko 	 * the current chunk pointer to make sure all chunks
7646f9579d4SJiri Pirko 	 * are properly migrated.
7656f9579d4SJiri Pirko 	 */
766e8904560SIdo Schimmel 	mlxsw_sp_acl_tcam_rehash_ctx_vchunk_reset(&vregion->rehash.ctx);
7676f9579d4SJiri Pirko }
7686f9579d4SJiri Pirko 
7690f54236dSJiri Pirko static struct mlxsw_sp_acl_tcam_vregion *
mlxsw_sp_acl_tcam_vregion_create(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vgroup * vgroup,unsigned int priority,struct mlxsw_afk_element_usage * elusage)7700f54236dSJiri Pirko mlxsw_sp_acl_tcam_vregion_create(struct mlxsw_sp *mlxsw_sp,
77179604b6eSJiri Pirko 				 struct mlxsw_sp_acl_tcam_vgroup *vgroup,
77279604b6eSJiri Pirko 				 unsigned int priority,
7730f54236dSJiri Pirko 				 struct mlxsw_afk_element_usage *elusage)
7740f54236dSJiri Pirko {
775e5e7962eSJiri Pirko 	const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
7760f54236dSJiri Pirko 	struct mlxsw_afk *afk = mlxsw_sp_acl_afk(mlxsw_sp->acl);
77779604b6eSJiri Pirko 	struct mlxsw_sp_acl_tcam *tcam = vgroup->group.tcam;
7780f54236dSJiri Pirko 	struct mlxsw_sp_acl_tcam_vregion *vregion;
7790f54236dSJiri Pirko 	int err;
7800f54236dSJiri Pirko 
7810f54236dSJiri Pirko 	vregion = kzalloc(sizeof(*vregion), GFP_KERNEL);
7820f54236dSJiri Pirko 	if (!vregion)
7830f54236dSJiri Pirko 		return ERR_PTR(-ENOMEM);
784b2d6b4d2SJiri Pirko 	INIT_LIST_HEAD(&vregion->vchunk_list);
7851263a9abSJiri Pirko 	mutex_init(&vregion->lock);
786e5e7962eSJiri Pirko 	vregion->tcam = tcam;
787e5e7962eSJiri Pirko 	vregion->mlxsw_sp = mlxsw_sp;
7886b861682SJiri Pirko 	vregion->vgroup = vgroup;
78999a9e7fbSAmit Cohen 	refcount_set(&vregion->ref_count, 1);
7900f54236dSJiri Pirko 
7910f54236dSJiri Pirko 	vregion->key_info = mlxsw_afk_key_info_get(afk, elusage);
7920f54236dSJiri Pirko 	if (IS_ERR(vregion->key_info)) {
7930f54236dSJiri Pirko 		err = PTR_ERR(vregion->key_info);
7940f54236dSJiri Pirko 		goto err_key_info_get;
7950f54236dSJiri Pirko 	}
7960f54236dSJiri Pirko 
7970f54236dSJiri Pirko 	vregion->region = mlxsw_sp_acl_tcam_region_create(mlxsw_sp, tcam,
798e5e7962eSJiri Pirko 							  vregion, NULL);
7990f54236dSJiri Pirko 	if (IS_ERR(vregion->region)) {
8000f54236dSJiri Pirko 		err = PTR_ERR(vregion->region);
8010f54236dSJiri Pirko 		goto err_region_create;
8020f54236dSJiri Pirko 	}
8030f54236dSJiri Pirko 
80479604b6eSJiri Pirko 	err = mlxsw_sp_acl_tcam_vgroup_vregion_attach(mlxsw_sp, vgroup, vregion,
80579604b6eSJiri Pirko 						      priority);
80679604b6eSJiri Pirko 	if (err)
80779604b6eSJiri Pirko 		goto err_vgroup_vregion_attach;
80879604b6eSJiri Pirko 
8096b861682SJiri Pirko 	if (vgroup->vregion_rehash_enabled && ops->region_rehash_hints_get) {
810e5e7962eSJiri Pirko 		/* Create the delayed work for vregion periodic rehash */
811f9b274ceSJiri Pirko 		INIT_DELAYED_WORK(&vregion->rehash.dw,
812e5e7962eSJiri Pirko 				  mlxsw_sp_acl_tcam_vregion_rehash_work);
813e5e7962eSJiri Pirko 		mlxsw_sp_acl_tcam_vregion_rehash_work_schedule(vregion);
8147b0f62eeSJiri Pirko 		mutex_lock(&tcam->lock);
8156b861682SJiri Pirko 		list_add_tail(&vregion->tlist, &tcam->vregion_list);
8167b0f62eeSJiri Pirko 		mutex_unlock(&tcam->lock);
817e5e7962eSJiri Pirko 	}
818e5e7962eSJiri Pirko 
8190f54236dSJiri Pirko 	return vregion;
8200f54236dSJiri Pirko 
82179604b6eSJiri Pirko err_vgroup_vregion_attach:
82279604b6eSJiri Pirko 	mlxsw_sp_acl_tcam_region_destroy(mlxsw_sp, vregion->region);
8230f54236dSJiri Pirko err_region_create:
8240f54236dSJiri Pirko 	mlxsw_afk_key_info_put(vregion->key_info);
8250f54236dSJiri Pirko err_key_info_get:
8260f54236dSJiri Pirko 	kfree(vregion);
8270f54236dSJiri Pirko 	return ERR_PTR(err);
8280f54236dSJiri Pirko }
8290f54236dSJiri Pirko 
8300f54236dSJiri Pirko static void
mlxsw_sp_acl_tcam_vregion_destroy(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vregion * vregion)8310f54236dSJiri Pirko mlxsw_sp_acl_tcam_vregion_destroy(struct mlxsw_sp *mlxsw_sp,
8320f54236dSJiri Pirko 				  struct mlxsw_sp_acl_tcam_vregion *vregion)
8330f54236dSJiri Pirko {
834e5e7962eSJiri Pirko 	const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
8356b861682SJiri Pirko 	struct mlxsw_sp_acl_tcam_vgroup *vgroup = vregion->vgroup;
8367b0f62eeSJiri Pirko 	struct mlxsw_sp_acl_tcam *tcam = vregion->tcam;
837e5e7962eSJiri Pirko 
8386b861682SJiri Pirko 	if (vgroup->vregion_rehash_enabled && ops->region_rehash_hints_get) {
839*de1aaefaSIdo Schimmel 		struct mlxsw_sp_acl_tcam_rehash_ctx *ctx = &vregion->rehash.ctx;
840*de1aaefaSIdo Schimmel 
8417b0f62eeSJiri Pirko 		mutex_lock(&tcam->lock);
842e5e7962eSJiri Pirko 		list_del(&vregion->tlist);
8437b0f62eeSJiri Pirko 		mutex_unlock(&tcam->lock);
844*de1aaefaSIdo Schimmel 		if (cancel_delayed_work_sync(&vregion->rehash.dw) &&
845*de1aaefaSIdo Schimmel 		    ctx->hints_priv)
846*de1aaefaSIdo Schimmel 			ops->region_rehash_hints_put(ctx->hints_priv);
8476b861682SJiri Pirko 	}
84879604b6eSJiri Pirko 	mlxsw_sp_acl_tcam_vgroup_vregion_detach(mlxsw_sp, vregion);
849e5e7962eSJiri Pirko 	if (vregion->region2)
850e5e7962eSJiri Pirko 		mlxsw_sp_acl_tcam_region_destroy(mlxsw_sp, vregion->region2);
8510f54236dSJiri Pirko 	mlxsw_sp_acl_tcam_region_destroy(mlxsw_sp, vregion->region);
8520f54236dSJiri Pirko 	mlxsw_afk_key_info_put(vregion->key_info);
8531263a9abSJiri Pirko 	mutex_destroy(&vregion->lock);
8540f54236dSJiri Pirko 	kfree(vregion);
8550f54236dSJiri Pirko }
8560f54236dSJiri Pirko 
85779604b6eSJiri Pirko static struct mlxsw_sp_acl_tcam_vregion *
mlxsw_sp_acl_tcam_vregion_get(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vgroup * vgroup,unsigned int priority,struct mlxsw_afk_element_usage * elusage)85879604b6eSJiri Pirko mlxsw_sp_acl_tcam_vregion_get(struct mlxsw_sp *mlxsw_sp,
8592802aadfSJiri Pirko 			      struct mlxsw_sp_acl_tcam_vgroup *vgroup,
86022a67766SJiri Pirko 			      unsigned int priority,
86179604b6eSJiri Pirko 			      struct mlxsw_afk_element_usage *elusage)
86222a67766SJiri Pirko {
86379604b6eSJiri Pirko 	struct mlxsw_afk_element_usage vregion_elusage;
8640f54236dSJiri Pirko 	struct mlxsw_sp_acl_tcam_vregion *vregion;
86522a67766SJiri Pirko 	bool need_split;
86622a67766SJiri Pirko 
8672802aadfSJiri Pirko 	vregion = mlxsw_sp_acl_tcam_vgroup_vregion_find(vgroup, priority,
8682802aadfSJiri Pirko 							elusage, &need_split);
86979604b6eSJiri Pirko 	if (vregion) {
87079604b6eSJiri Pirko 		if (need_split) {
87179604b6eSJiri Pirko 			/* According to priority, new vchunk should belong to
87279604b6eSJiri Pirko 			 * an existing vregion. However, this vchunk needs
87379604b6eSJiri Pirko 			 * elements that vregion does not contain. We need
87479604b6eSJiri Pirko 			 * to split the existing vregion into two and create
87579604b6eSJiri Pirko 			 * a new vregion for the new vchunk in between.
87679604b6eSJiri Pirko 			 * This is not supported now.
87722a67766SJiri Pirko 			 */
87879604b6eSJiri Pirko 			return ERR_PTR(-EOPNOTSUPP);
87922a67766SJiri Pirko 		}
88099a9e7fbSAmit Cohen 		refcount_inc(&vregion->ref_count);
88179604b6eSJiri Pirko 		return vregion;
88279604b6eSJiri Pirko 	}
88322a67766SJiri Pirko 
8842802aadfSJiri Pirko 	mlxsw_sp_acl_tcam_vgroup_use_patterns(vgroup, elusage,
8850f54236dSJiri Pirko 					      &vregion_elusage);
88679604b6eSJiri Pirko 
88779604b6eSJiri Pirko 	return mlxsw_sp_acl_tcam_vregion_create(mlxsw_sp, vgroup, priority,
8880f54236dSJiri Pirko 						&vregion_elusage);
88922a67766SJiri Pirko }
89022a67766SJiri Pirko 
89122a67766SJiri Pirko static void
mlxsw_sp_acl_tcam_vregion_put(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vregion * vregion)89279604b6eSJiri Pirko mlxsw_sp_acl_tcam_vregion_put(struct mlxsw_sp *mlxsw_sp,
89379604b6eSJiri Pirko 			      struct mlxsw_sp_acl_tcam_vregion *vregion)
89422a67766SJiri Pirko {
89599a9e7fbSAmit Cohen 	if (!refcount_dec_and_test(&vregion->ref_count))
89679604b6eSJiri Pirko 		return;
8970f54236dSJiri Pirko 	mlxsw_sp_acl_tcam_vregion_destroy(mlxsw_sp, vregion);
89822a67766SJiri Pirko }
89922a67766SJiri Pirko 
90022a67766SJiri Pirko static struct mlxsw_sp_acl_tcam_chunk *
mlxsw_sp_acl_tcam_chunk_create(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vchunk * vchunk,struct mlxsw_sp_acl_tcam_region * region)90122a67766SJiri Pirko mlxsw_sp_acl_tcam_chunk_create(struct mlxsw_sp *mlxsw_sp,
902b2d6b4d2SJiri Pirko 			       struct mlxsw_sp_acl_tcam_vchunk *vchunk,
903b2d6b4d2SJiri Pirko 			       struct mlxsw_sp_acl_tcam_region *region)
90422a67766SJiri Pirko {
90564eccd00SJiri Pirko 	const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
90622a67766SJiri Pirko 	struct mlxsw_sp_acl_tcam_chunk *chunk;
90722a67766SJiri Pirko 
90864eccd00SJiri Pirko 	chunk = kzalloc(sizeof(*chunk) + ops->chunk_priv_size, GFP_KERNEL);
90922a67766SJiri Pirko 	if (!chunk)
91022a67766SJiri Pirko 		return ERR_PTR(-ENOMEM);
911b2d6b4d2SJiri Pirko 	chunk->vchunk = vchunk;
912e5e7962eSJiri Pirko 	chunk->region = region;
91322a67766SJiri Pirko 
914b2d6b4d2SJiri Pirko 	ops->chunk_init(region->priv, chunk->priv, vchunk->priority);
91522a67766SJiri Pirko 	return chunk;
91622a67766SJiri Pirko }
91722a67766SJiri Pirko 
91822a67766SJiri Pirko static void
mlxsw_sp_acl_tcam_chunk_destroy(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_chunk * chunk)91922a67766SJiri Pirko mlxsw_sp_acl_tcam_chunk_destroy(struct mlxsw_sp *mlxsw_sp,
92022a67766SJiri Pirko 				struct mlxsw_sp_acl_tcam_chunk *chunk)
92122a67766SJiri Pirko {
92264eccd00SJiri Pirko 	const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
92322a67766SJiri Pirko 
92464eccd00SJiri Pirko 	ops->chunk_fini(chunk->priv);
92522a67766SJiri Pirko 	kfree(chunk);
92622a67766SJiri Pirko }
92722a67766SJiri Pirko 
928b2d6b4d2SJiri Pirko static struct mlxsw_sp_acl_tcam_vchunk *
mlxsw_sp_acl_tcam_vchunk_create(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vgroup * vgroup,unsigned int priority,struct mlxsw_afk_element_usage * elusage)929b2d6b4d2SJiri Pirko mlxsw_sp_acl_tcam_vchunk_create(struct mlxsw_sp *mlxsw_sp,
9302802aadfSJiri Pirko 				struct mlxsw_sp_acl_tcam_vgroup *vgroup,
93122a67766SJiri Pirko 				unsigned int priority,
93222a67766SJiri Pirko 				struct mlxsw_afk_element_usage *elusage)
93322a67766SJiri Pirko {
9346ef4889fSJiri Pirko 	struct mlxsw_sp_acl_tcam_vchunk *vchunk, *vchunk2;
93579604b6eSJiri Pirko 	struct mlxsw_sp_acl_tcam_vregion *vregion;
9366ef4889fSJiri Pirko 	struct list_head *pos;
937b2d6b4d2SJiri Pirko 	int err;
93822a67766SJiri Pirko 
939b2d6b4d2SJiri Pirko 	if (priority == MLXSW_SP_ACL_TCAM_CATCHALL_PRIO)
940b2d6b4d2SJiri Pirko 		return ERR_PTR(-EINVAL);
941b2d6b4d2SJiri Pirko 
942b2d6b4d2SJiri Pirko 	vchunk = kzalloc(sizeof(*vchunk), GFP_KERNEL);
943b2d6b4d2SJiri Pirko 	if (!vchunk)
944b2d6b4d2SJiri Pirko 		return ERR_PTR(-ENOMEM);
945e5e7962eSJiri Pirko 	INIT_LIST_HEAD(&vchunk->ventry_list);
946b2d6b4d2SJiri Pirko 	vchunk->priority = priority;
9472802aadfSJiri Pirko 	vchunk->vgroup = vgroup;
94899a9e7fbSAmit Cohen 	refcount_set(&vchunk->ref_count, 1);
949b2d6b4d2SJiri Pirko 
95079604b6eSJiri Pirko 	vregion = mlxsw_sp_acl_tcam_vregion_get(mlxsw_sp, vgroup,
95179604b6eSJiri Pirko 						priority, elusage);
95279604b6eSJiri Pirko 	if (IS_ERR(vregion)) {
95379604b6eSJiri Pirko 		err = PTR_ERR(vregion);
95479604b6eSJiri Pirko 		goto err_vregion_get;
95579604b6eSJiri Pirko 	}
95679604b6eSJiri Pirko 
95779604b6eSJiri Pirko 	vchunk->vregion = vregion;
958b2d6b4d2SJiri Pirko 
9592802aadfSJiri Pirko 	err = rhashtable_insert_fast(&vgroup->vchunk_ht, &vchunk->ht_node,
960b2d6b4d2SJiri Pirko 				     mlxsw_sp_acl_tcam_vchunk_ht_params);
961b2d6b4d2SJiri Pirko 	if (err)
962b2d6b4d2SJiri Pirko 		goto err_rhashtable_insert;
963b2d6b4d2SJiri Pirko 
9641263a9abSJiri Pirko 	mutex_lock(&vregion->lock);
965b2d6b4d2SJiri Pirko 	vchunk->chunk = mlxsw_sp_acl_tcam_chunk_create(mlxsw_sp, vchunk,
966b2d6b4d2SJiri Pirko 						       vchunk->vregion->region);
967b2d6b4d2SJiri Pirko 	if (IS_ERR(vchunk->chunk)) {
9681263a9abSJiri Pirko 		mutex_unlock(&vregion->lock);
969b2d6b4d2SJiri Pirko 		err = PTR_ERR(vchunk->chunk);
970b2d6b4d2SJiri Pirko 		goto err_chunk_create;
971b2d6b4d2SJiri Pirko 	}
972b2d6b4d2SJiri Pirko 
9736f9579d4SJiri Pirko 	mlxsw_sp_acl_tcam_rehash_ctx_vregion_changed(vregion);
9746ef4889fSJiri Pirko 
9756ef4889fSJiri Pirko 	/* Position the vchunk inside the list according to priority */
9766ef4889fSJiri Pirko 	list_for_each(pos, &vregion->vchunk_list) {
9776ef4889fSJiri Pirko 		vchunk2 = list_entry(pos, typeof(*vchunk2), list);
9786ef4889fSJiri Pirko 		if (vchunk2->priority > priority)
9796ef4889fSJiri Pirko 			break;
9806ef4889fSJiri Pirko 	}
9816ef4889fSJiri Pirko 	list_add_tail(&vchunk->list, pos);
9821263a9abSJiri Pirko 	mutex_unlock(&vregion->lock);
983593bb843SJiri Pirko 	mlxsw_sp_acl_tcam_vgroup_prio_update(vgroup);
98479604b6eSJiri Pirko 
985b2d6b4d2SJiri Pirko 	return vchunk;
986b2d6b4d2SJiri Pirko 
987b2d6b4d2SJiri Pirko err_chunk_create:
9882802aadfSJiri Pirko 	rhashtable_remove_fast(&vgroup->vchunk_ht, &vchunk->ht_node,
989b2d6b4d2SJiri Pirko 			       mlxsw_sp_acl_tcam_vchunk_ht_params);
990b2d6b4d2SJiri Pirko err_rhashtable_insert:
99179604b6eSJiri Pirko 	mlxsw_sp_acl_tcam_vregion_put(mlxsw_sp, vregion);
99279604b6eSJiri Pirko err_vregion_get:
993b2d6b4d2SJiri Pirko 	kfree(vchunk);
994b2d6b4d2SJiri Pirko 	return ERR_PTR(err);
995b2d6b4d2SJiri Pirko }
996b2d6b4d2SJiri Pirko 
997b2d6b4d2SJiri Pirko static void
mlxsw_sp_acl_tcam_vchunk_destroy(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vchunk * vchunk)998b2d6b4d2SJiri Pirko mlxsw_sp_acl_tcam_vchunk_destroy(struct mlxsw_sp *mlxsw_sp,
999b2d6b4d2SJiri Pirko 				 struct mlxsw_sp_acl_tcam_vchunk *vchunk)
1000b2d6b4d2SJiri Pirko {
10011263a9abSJiri Pirko 	struct mlxsw_sp_acl_tcam_vregion *vregion = vchunk->vregion;
10022802aadfSJiri Pirko 	struct mlxsw_sp_acl_tcam_vgroup *vgroup = vchunk->vgroup;
1003b2d6b4d2SJiri Pirko 
10041263a9abSJiri Pirko 	mutex_lock(&vregion->lock);
10056f9579d4SJiri Pirko 	mlxsw_sp_acl_tcam_rehash_ctx_vregion_changed(vregion);
100679604b6eSJiri Pirko 	list_del(&vchunk->list);
1007e5e7962eSJiri Pirko 	if (vchunk->chunk2)
1008e5e7962eSJiri Pirko 		mlxsw_sp_acl_tcam_chunk_destroy(mlxsw_sp, vchunk->chunk2);
1009b2d6b4d2SJiri Pirko 	mlxsw_sp_acl_tcam_chunk_destroy(mlxsw_sp, vchunk->chunk);
10101263a9abSJiri Pirko 	mutex_unlock(&vregion->lock);
10112802aadfSJiri Pirko 	rhashtable_remove_fast(&vgroup->vchunk_ht, &vchunk->ht_node,
1012b2d6b4d2SJiri Pirko 			       mlxsw_sp_acl_tcam_vchunk_ht_params);
101379604b6eSJiri Pirko 	mlxsw_sp_acl_tcam_vregion_put(mlxsw_sp, vchunk->vregion);
1014b2d6b4d2SJiri Pirko 	kfree(vchunk);
1015593bb843SJiri Pirko 	mlxsw_sp_acl_tcam_vgroup_prio_update(vgroup);
1016b2d6b4d2SJiri Pirko }
1017b2d6b4d2SJiri Pirko 
1018b2d6b4d2SJiri Pirko static struct mlxsw_sp_acl_tcam_vchunk *
mlxsw_sp_acl_tcam_vchunk_get(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vgroup * vgroup,unsigned int priority,struct mlxsw_afk_element_usage * elusage)1019b2d6b4d2SJiri Pirko mlxsw_sp_acl_tcam_vchunk_get(struct mlxsw_sp *mlxsw_sp,
10202802aadfSJiri Pirko 			     struct mlxsw_sp_acl_tcam_vgroup *vgroup,
1021b2d6b4d2SJiri Pirko 			     unsigned int priority,
1022b2d6b4d2SJiri Pirko 			     struct mlxsw_afk_element_usage *elusage)
1023b2d6b4d2SJiri Pirko {
1024b2d6b4d2SJiri Pirko 	struct mlxsw_sp_acl_tcam_vchunk *vchunk;
1025b2d6b4d2SJiri Pirko 
10262802aadfSJiri Pirko 	vchunk = rhashtable_lookup_fast(&vgroup->vchunk_ht, &priority,
1027b2d6b4d2SJiri Pirko 					mlxsw_sp_acl_tcam_vchunk_ht_params);
1028b2d6b4d2SJiri Pirko 	if (vchunk) {
1029b2d6b4d2SJiri Pirko 		if (WARN_ON(!mlxsw_afk_key_info_subset(vchunk->vregion->key_info,
103022a67766SJiri Pirko 						       elusage)))
103122a67766SJiri Pirko 			return ERR_PTR(-EINVAL);
103299a9e7fbSAmit Cohen 		refcount_inc(&vchunk->ref_count);
1033b2d6b4d2SJiri Pirko 		return vchunk;
103422a67766SJiri Pirko 	}
10352802aadfSJiri Pirko 	return mlxsw_sp_acl_tcam_vchunk_create(mlxsw_sp, vgroup,
103622a67766SJiri Pirko 					       priority, elusage);
103722a67766SJiri Pirko }
103822a67766SJiri Pirko 
1039b2d6b4d2SJiri Pirko static void
mlxsw_sp_acl_tcam_vchunk_put(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vchunk * vchunk)1040b2d6b4d2SJiri Pirko mlxsw_sp_acl_tcam_vchunk_put(struct mlxsw_sp *mlxsw_sp,
1041b2d6b4d2SJiri Pirko 			     struct mlxsw_sp_acl_tcam_vchunk *vchunk)
104222a67766SJiri Pirko {
104399a9e7fbSAmit Cohen 	if (!refcount_dec_and_test(&vchunk->ref_count))
104422a67766SJiri Pirko 		return;
1045b2d6b4d2SJiri Pirko 	mlxsw_sp_acl_tcam_vchunk_destroy(mlxsw_sp, vchunk);
104622a67766SJiri Pirko }
104722a67766SJiri Pirko 
1048c4c2dc54SJiri Pirko static struct mlxsw_sp_acl_tcam_entry *
mlxsw_sp_acl_tcam_entry_create(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_ventry * ventry,struct mlxsw_sp_acl_tcam_chunk * chunk)1049c4c2dc54SJiri Pirko mlxsw_sp_acl_tcam_entry_create(struct mlxsw_sp *mlxsw_sp,
1050c4c2dc54SJiri Pirko 			       struct mlxsw_sp_acl_tcam_ventry *ventry,
1051e5e7962eSJiri Pirko 			       struct mlxsw_sp_acl_tcam_chunk *chunk)
105222a67766SJiri Pirko {
105364eccd00SJiri Pirko 	const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
1054c4c2dc54SJiri Pirko 	struct mlxsw_sp_acl_tcam_entry *entry;
105522a67766SJiri Pirko 	int err;
105622a67766SJiri Pirko 
1057c4c2dc54SJiri Pirko 	entry = kzalloc(sizeof(*entry) + ops->entry_priv_size, GFP_KERNEL);
1058c4c2dc54SJiri Pirko 	if (!entry)
1059c4c2dc54SJiri Pirko 		return ERR_PTR(-ENOMEM);
1060c4c2dc54SJiri Pirko 	entry->ventry = ventry;
1061e5e7962eSJiri Pirko 	entry->chunk = chunk;
106222a67766SJiri Pirko 
1063e5e7962eSJiri Pirko 	err = ops->entry_add(mlxsw_sp, chunk->region->priv, chunk->priv,
1064e5e7962eSJiri Pirko 			     entry->priv, ventry->rulei);
106522a67766SJiri Pirko 	if (err)
106664eccd00SJiri Pirko 		goto err_entry_add;
106722a67766SJiri Pirko 
1068c4c2dc54SJiri Pirko 	return entry;
106922a67766SJiri Pirko 
107064eccd00SJiri Pirko err_entry_add:
1071c4c2dc54SJiri Pirko 	kfree(entry);
1072c4c2dc54SJiri Pirko 	return ERR_PTR(err);
107322a67766SJiri Pirko }
107422a67766SJiri Pirko 
mlxsw_sp_acl_tcam_entry_destroy(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_entry * entry)1075c4c2dc54SJiri Pirko static void mlxsw_sp_acl_tcam_entry_destroy(struct mlxsw_sp *mlxsw_sp,
107622a67766SJiri Pirko 					    struct mlxsw_sp_acl_tcam_entry *entry)
107722a67766SJiri Pirko {
107864eccd00SJiri Pirko 	const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
107922a67766SJiri Pirko 
1080e5e7962eSJiri Pirko 	ops->entry_del(mlxsw_sp, entry->chunk->region->priv,
1081e5e7962eSJiri Pirko 		       entry->chunk->priv, entry->priv);
1082c4c2dc54SJiri Pirko 	kfree(entry);
108322a67766SJiri Pirko }
108422a67766SJiri Pirko 
10857fd056c2SArkadi Sharshevsky static int
mlxsw_sp_acl_tcam_entry_action_replace(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_region * region,struct mlxsw_sp_acl_tcam_entry * entry,struct mlxsw_sp_acl_rule_info * rulei)10862507a64cSNir Dotan mlxsw_sp_acl_tcam_entry_action_replace(struct mlxsw_sp *mlxsw_sp,
1087c4c2dc54SJiri Pirko 				       struct mlxsw_sp_acl_tcam_region *region,
10882507a64cSNir Dotan 				       struct mlxsw_sp_acl_tcam_entry *entry,
10892507a64cSNir Dotan 				       struct mlxsw_sp_acl_rule_info *rulei)
10902507a64cSNir Dotan {
10912507a64cSNir Dotan 	const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
10922507a64cSNir Dotan 
109342d704e0SJiri Pirko 	return ops->entry_action_replace(mlxsw_sp, region->priv,
10942507a64cSNir Dotan 					 entry->priv, rulei);
10952507a64cSNir Dotan }
10962507a64cSNir Dotan 
10972507a64cSNir Dotan static int
mlxsw_sp_acl_tcam_entry_activity_get(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_entry * entry,bool * activity)10987fd056c2SArkadi Sharshevsky mlxsw_sp_acl_tcam_entry_activity_get(struct mlxsw_sp *mlxsw_sp,
10997fd056c2SArkadi Sharshevsky 				     struct mlxsw_sp_acl_tcam_entry *entry,
11007fd056c2SArkadi Sharshevsky 				     bool *activity)
11017fd056c2SArkadi Sharshevsky {
110264eccd00SJiri Pirko 	const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
11037fd056c2SArkadi Sharshevsky 
1104e5e7962eSJiri Pirko 	return ops->entry_activity_get(mlxsw_sp, entry->chunk->region->priv,
110564eccd00SJiri Pirko 				       entry->priv, activity);
11067fd056c2SArkadi Sharshevsky }
11077fd056c2SArkadi Sharshevsky 
mlxsw_sp_acl_tcam_ventry_add(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vgroup * vgroup,struct mlxsw_sp_acl_tcam_ventry * ventry,struct mlxsw_sp_acl_rule_info * rulei)1108c4c2dc54SJiri Pirko static int mlxsw_sp_acl_tcam_ventry_add(struct mlxsw_sp *mlxsw_sp,
11092802aadfSJiri Pirko 					struct mlxsw_sp_acl_tcam_vgroup *vgroup,
1110c4c2dc54SJiri Pirko 					struct mlxsw_sp_acl_tcam_ventry *ventry,
1111c4c2dc54SJiri Pirko 					struct mlxsw_sp_acl_rule_info *rulei)
1112c4c2dc54SJiri Pirko {
11131263a9abSJiri Pirko 	struct mlxsw_sp_acl_tcam_vregion *vregion;
1114c4c2dc54SJiri Pirko 	struct mlxsw_sp_acl_tcam_vchunk *vchunk;
1115c4c2dc54SJiri Pirko 	int err;
1116c4c2dc54SJiri Pirko 
11172802aadfSJiri Pirko 	vchunk = mlxsw_sp_acl_tcam_vchunk_get(mlxsw_sp, vgroup, rulei->priority,
1118c4c2dc54SJiri Pirko 					      &rulei->values.elusage);
1119c4c2dc54SJiri Pirko 	if (IS_ERR(vchunk))
1120c4c2dc54SJiri Pirko 		return PTR_ERR(vchunk);
1121c4c2dc54SJiri Pirko 
1122c4c2dc54SJiri Pirko 	ventry->vchunk = vchunk;
1123e5e7962eSJiri Pirko 	ventry->rulei = rulei;
11241263a9abSJiri Pirko 	vregion = vchunk->vregion;
11251263a9abSJiri Pirko 
11261263a9abSJiri Pirko 	mutex_lock(&vregion->lock);
1127c4c2dc54SJiri Pirko 	ventry->entry = mlxsw_sp_acl_tcam_entry_create(mlxsw_sp, ventry,
1128e5e7962eSJiri Pirko 						       vchunk->chunk);
1129c4c2dc54SJiri Pirko 	if (IS_ERR(ventry->entry)) {
11301263a9abSJiri Pirko 		mutex_unlock(&vregion->lock);
1131c4c2dc54SJiri Pirko 		err = PTR_ERR(ventry->entry);
1132c4c2dc54SJiri Pirko 		goto err_entry_create;
1133c4c2dc54SJiri Pirko 	}
1134c4c2dc54SJiri Pirko 
1135e5e7962eSJiri Pirko 	list_add_tail(&ventry->list, &vchunk->ventry_list);
11366f9579d4SJiri Pirko 	mlxsw_sp_acl_tcam_rehash_ctx_vchunk_changed(vchunk);
11371263a9abSJiri Pirko 	mutex_unlock(&vregion->lock);
1138e5e7962eSJiri Pirko 
1139c4c2dc54SJiri Pirko 	return 0;
1140c4c2dc54SJiri Pirko 
1141c4c2dc54SJiri Pirko err_entry_create:
1142c4c2dc54SJiri Pirko 	mlxsw_sp_acl_tcam_vchunk_put(mlxsw_sp, vchunk);
1143c4c2dc54SJiri Pirko 	return err;
1144c4c2dc54SJiri Pirko }
1145c4c2dc54SJiri Pirko 
mlxsw_sp_acl_tcam_ventry_del(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_ventry * ventry)1146c4c2dc54SJiri Pirko static void mlxsw_sp_acl_tcam_ventry_del(struct mlxsw_sp *mlxsw_sp,
1147c4c2dc54SJiri Pirko 					 struct mlxsw_sp_acl_tcam_ventry *ventry)
1148c4c2dc54SJiri Pirko {
1149c4c2dc54SJiri Pirko 	struct mlxsw_sp_acl_tcam_vchunk *vchunk = ventry->vchunk;
11501263a9abSJiri Pirko 	struct mlxsw_sp_acl_tcam_vregion *vregion = vchunk->vregion;
1151c4c2dc54SJiri Pirko 
11521263a9abSJiri Pirko 	mutex_lock(&vregion->lock);
11536f9579d4SJiri Pirko 	mlxsw_sp_acl_tcam_rehash_ctx_vchunk_changed(vchunk);
1154e5e7962eSJiri Pirko 	list_del(&ventry->list);
1155e5e7962eSJiri Pirko 	mlxsw_sp_acl_tcam_entry_destroy(mlxsw_sp, ventry->entry);
11561263a9abSJiri Pirko 	mutex_unlock(&vregion->lock);
1157c4c2dc54SJiri Pirko 	mlxsw_sp_acl_tcam_vchunk_put(mlxsw_sp, vchunk);
1158c4c2dc54SJiri Pirko }
1159c4c2dc54SJiri Pirko 
1160c4c2dc54SJiri Pirko static int
mlxsw_sp_acl_tcam_ventry_action_replace(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_ventry * ventry,struct mlxsw_sp_acl_rule_info * rulei)1161c4c2dc54SJiri Pirko mlxsw_sp_acl_tcam_ventry_action_replace(struct mlxsw_sp *mlxsw_sp,
1162c4c2dc54SJiri Pirko 					struct mlxsw_sp_acl_tcam_ventry *ventry,
1163c4c2dc54SJiri Pirko 					struct mlxsw_sp_acl_rule_info *rulei)
1164c4c2dc54SJiri Pirko {
1165c4c2dc54SJiri Pirko 	struct mlxsw_sp_acl_tcam_vchunk *vchunk = ventry->vchunk;
1166c4c2dc54SJiri Pirko 
1167c4c2dc54SJiri Pirko 	return mlxsw_sp_acl_tcam_entry_action_replace(mlxsw_sp,
1168c4c2dc54SJiri Pirko 						      vchunk->vregion->region,
1169c4c2dc54SJiri Pirko 						      ventry->entry, rulei);
1170c4c2dc54SJiri Pirko }
1171c4c2dc54SJiri Pirko 
1172c4c2dc54SJiri Pirko static int
mlxsw_sp_acl_tcam_ventry_activity_get(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_ventry * ventry,bool * activity)1173c4c2dc54SJiri Pirko mlxsw_sp_acl_tcam_ventry_activity_get(struct mlxsw_sp *mlxsw_sp,
1174c4c2dc54SJiri Pirko 				      struct mlxsw_sp_acl_tcam_ventry *ventry,
1175c4c2dc54SJiri Pirko 				      bool *activity)
1176c4c2dc54SJiri Pirko {
1177feabdac2SIdo Schimmel 	struct mlxsw_sp_acl_tcam_vregion *vregion = ventry->vchunk->vregion;
1178feabdac2SIdo Schimmel 	int err;
1179feabdac2SIdo Schimmel 
1180feabdac2SIdo Schimmel 	mutex_lock(&vregion->lock);
1181feabdac2SIdo Schimmel 	err = mlxsw_sp_acl_tcam_entry_activity_get(mlxsw_sp, ventry->entry,
1182feabdac2SIdo Schimmel 						   activity);
1183feabdac2SIdo Schimmel 	mutex_unlock(&vregion->lock);
1184feabdac2SIdo Schimmel 	return err;
1185c4c2dc54SJiri Pirko }
1186c4c2dc54SJiri Pirko 
1187e5e7962eSJiri Pirko static int
mlxsw_sp_acl_tcam_ventry_migrate(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_ventry * ventry,struct mlxsw_sp_acl_tcam_chunk * chunk,int * credits)1188e5e7962eSJiri Pirko mlxsw_sp_acl_tcam_ventry_migrate(struct mlxsw_sp *mlxsw_sp,
1189e5e7962eSJiri Pirko 				 struct mlxsw_sp_acl_tcam_ventry *ventry,
1190c9c9af91SJiri Pirko 				 struct mlxsw_sp_acl_tcam_chunk *chunk,
1191c9c9af91SJiri Pirko 				 int *credits)
1192e5e7962eSJiri Pirko {
11932c331593SJiri Pirko 	struct mlxsw_sp_acl_tcam_entry *new_entry;
1194e5e7962eSJiri Pirko 
11956ca219e7SJiri Pirko 	/* First check if the entry is not already where we want it to be. */
11962c331593SJiri Pirko 	if (ventry->entry->chunk == chunk)
11976ca219e7SJiri Pirko 		return 0;
11986ca219e7SJiri Pirko 
1199c9c9af91SJiri Pirko 	if (--(*credits) < 0)
1200c9c9af91SJiri Pirko 		return 0;
1201c9c9af91SJiri Pirko 
12022c331593SJiri Pirko 	new_entry = mlxsw_sp_acl_tcam_entry_create(mlxsw_sp, ventry, chunk);
12032c331593SJiri Pirko 	if (IS_ERR(new_entry))
12042c331593SJiri Pirko 		return PTR_ERR(new_entry);
1205e5e7962eSJiri Pirko 	mlxsw_sp_acl_tcam_entry_destroy(mlxsw_sp, ventry->entry);
12062c331593SJiri Pirko 	ventry->entry = new_entry;
1207e5e7962eSJiri Pirko 	return 0;
1208e5e7962eSJiri Pirko }
1209e5e7962eSJiri Pirko 
1210e5e7962eSJiri Pirko static int
mlxsw_sp_acl_tcam_vchunk_migrate_start(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vchunk * vchunk,struct mlxsw_sp_acl_tcam_region * region,struct mlxsw_sp_acl_tcam_rehash_ctx * ctx)1211844f01daSJiri Pirko mlxsw_sp_acl_tcam_vchunk_migrate_start(struct mlxsw_sp *mlxsw_sp,
1212e5e7962eSJiri Pirko 				       struct mlxsw_sp_acl_tcam_vchunk *vchunk,
1213e5e7962eSJiri Pirko 				       struct mlxsw_sp_acl_tcam_region *region,
1214220f4fbaSJiri Pirko 				       struct mlxsw_sp_acl_tcam_rehash_ctx *ctx)
1215e5e7962eSJiri Pirko {
1216e1d2f7a9SJiri Pirko 	struct mlxsw_sp_acl_tcam_chunk *new_chunk;
1217e5e7962eSJiri Pirko 
12180ae8ff7bSIdo Schimmel 	WARN_ON(vchunk->chunk2);
12190ae8ff7bSIdo Schimmel 
1220e1d2f7a9SJiri Pirko 	new_chunk = mlxsw_sp_acl_tcam_chunk_create(mlxsw_sp, vchunk, region);
122144fd86cbSJiri Pirko 	if (IS_ERR(new_chunk))
1222e1d2f7a9SJiri Pirko 		return PTR_ERR(new_chunk);
1223e1d2f7a9SJiri Pirko 	vchunk->chunk2 = vchunk->chunk;
1224e1d2f7a9SJiri Pirko 	vchunk->chunk = new_chunk;
12256f9579d4SJiri Pirko 	ctx->current_vchunk = vchunk;
12266f9579d4SJiri Pirko 	ctx->start_ventry = NULL;
12276f9579d4SJiri Pirko 	ctx->stop_ventry = NULL;
1228844f01daSJiri Pirko 	return 0;
1229844f01daSJiri Pirko }
1230844f01daSJiri Pirko 
1231844f01daSJiri Pirko static void
mlxsw_sp_acl_tcam_vchunk_migrate_end(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vchunk * vchunk,struct mlxsw_sp_acl_tcam_rehash_ctx * ctx)1232844f01daSJiri Pirko mlxsw_sp_acl_tcam_vchunk_migrate_end(struct mlxsw_sp *mlxsw_sp,
12336f9579d4SJiri Pirko 				     struct mlxsw_sp_acl_tcam_vchunk *vchunk,
12346f9579d4SJiri Pirko 				     struct mlxsw_sp_acl_tcam_rehash_ctx *ctx)
1235844f01daSJiri Pirko {
1236844f01daSJiri Pirko 	mlxsw_sp_acl_tcam_chunk_destroy(mlxsw_sp, vchunk->chunk2);
1237844f01daSJiri Pirko 	vchunk->chunk2 = NULL;
1238e8904560SIdo Schimmel 	mlxsw_sp_acl_tcam_rehash_ctx_vchunk_reset(ctx);
1239844f01daSJiri Pirko }
1240844f01daSJiri Pirko 
1241844f01daSJiri Pirko static int
mlxsw_sp_acl_tcam_vchunk_migrate_one(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vchunk * vchunk,struct mlxsw_sp_acl_tcam_region * region,struct mlxsw_sp_acl_tcam_rehash_ctx * ctx,int * credits)1242844f01daSJiri Pirko mlxsw_sp_acl_tcam_vchunk_migrate_one(struct mlxsw_sp *mlxsw_sp,
1243844f01daSJiri Pirko 				     struct mlxsw_sp_acl_tcam_vchunk *vchunk,
1244844f01daSJiri Pirko 				     struct mlxsw_sp_acl_tcam_region *region,
1245c9c9af91SJiri Pirko 				     struct mlxsw_sp_acl_tcam_rehash_ctx *ctx,
1246c9c9af91SJiri Pirko 				     int *credits)
1247844f01daSJiri Pirko {
1248844f01daSJiri Pirko 	struct mlxsw_sp_acl_tcam_ventry *ventry;
1249844f01daSJiri Pirko 	int err;
1250844f01daSJiri Pirko 
125184350051SJiri Pirko 	if (vchunk->chunk->region != region) {
1252844f01daSJiri Pirko 		err = mlxsw_sp_acl_tcam_vchunk_migrate_start(mlxsw_sp, vchunk,
1253844f01daSJiri Pirko 							     region, ctx);
1254844f01daSJiri Pirko 		if (err)
1255844f01daSJiri Pirko 			return err;
125684350051SJiri Pirko 	} else if (!vchunk->chunk2) {
125784350051SJiri Pirko 		/* The chunk is already as it should be, nothing to do. */
125884350051SJiri Pirko 		return 0;
125984350051SJiri Pirko 	}
1260e1d2f7a9SJiri Pirko 
1261ab4ecfb6SIdo Schimmel 	if (list_empty(&vchunk->ventry_list))
1262ab4ecfb6SIdo Schimmel 		goto out;
1263ab4ecfb6SIdo Schimmel 
12646f9579d4SJiri Pirko 	/* If the migration got interrupted, we have the ventry to start from
12656f9579d4SJiri Pirko 	 * stored in context.
12666f9579d4SJiri Pirko 	 */
12676f9579d4SJiri Pirko 	if (ctx->start_ventry)
12686f9579d4SJiri Pirko 		ventry = ctx->start_ventry;
12696f9579d4SJiri Pirko 	else
12706f9579d4SJiri Pirko 		ventry = list_first_entry(&vchunk->ventry_list,
12716f9579d4SJiri Pirko 					  typeof(*ventry), list);
12726f9579d4SJiri Pirko 
1273e8904560SIdo Schimmel 	WARN_ON(ventry->vchunk != vchunk);
1274e8904560SIdo Schimmel 
12756f9579d4SJiri Pirko 	list_for_each_entry_from(ventry, &vchunk->ventry_list, list) {
12766f9579d4SJiri Pirko 		/* During rollback, once we reach the ventry that failed
12776f9579d4SJiri Pirko 		 * to migrate, we are done.
12786f9579d4SJiri Pirko 		 */
12796f9579d4SJiri Pirko 		if (ventry == ctx->stop_ventry)
12806f9579d4SJiri Pirko 			break;
12816f9579d4SJiri Pirko 
1282e5e7962eSJiri Pirko 		err = mlxsw_sp_acl_tcam_ventry_migrate(mlxsw_sp, ventry,
1283c9c9af91SJiri Pirko 						       vchunk->chunk, credits);
1284e5e7962eSJiri Pirko 		if (err) {
12857c33c72bSJiri Pirko 			if (ctx->this_is_rollback) {
12867c33c72bSJiri Pirko 				/* Save the ventry which we ended with and try
12877c33c72bSJiri Pirko 				 * to continue later on.
12887c33c72bSJiri Pirko 				 */
12897c33c72bSJiri Pirko 				ctx->start_ventry = ventry;
1290e5e7962eSJiri Pirko 				return err;
12917c33c72bSJiri Pirko 			}
129284350051SJiri Pirko 			/* Swap the chunk and chunk2 pointers so the follow-up
129384350051SJiri Pirko 			 * rollback call will see the original chunk pointer
129484350051SJiri Pirko 			 * in vchunk->chunk.
129584350051SJiri Pirko 			 */
129684350051SJiri Pirko 			swap(vchunk->chunk, vchunk->chunk2);
12976f9579d4SJiri Pirko 			/* The rollback has to be done from beginning of the
12986f9579d4SJiri Pirko 			 * chunk, that is why we have to null the start_ventry.
12996f9579d4SJiri Pirko 			 * However, we know where to stop the rollback,
13006f9579d4SJiri Pirko 			 * at the current ventry.
13016f9579d4SJiri Pirko 			 */
13026f9579d4SJiri Pirko 			ctx->start_ventry = NULL;
13036f9579d4SJiri Pirko 			ctx->stop_ventry = ventry;
130484350051SJiri Pirko 			return err;
1305c9c9af91SJiri Pirko 		} else if (*credits < 0) {
1306c9c9af91SJiri Pirko 			/* We are out of credits, the rest of the ventries
13076f9579d4SJiri Pirko 			 * will be migrated later. Save the ventry
13086f9579d4SJiri Pirko 			 * which we ended with.
1309c9c9af91SJiri Pirko 			 */
13106f9579d4SJiri Pirko 			ctx->start_ventry = ventry;
1311c9c9af91SJiri Pirko 			return 0;
1312e5e7962eSJiri Pirko 		}
1313e5e7962eSJiri Pirko 	}
1314844f01daSJiri Pirko 
1315ab4ecfb6SIdo Schimmel out:
13166f9579d4SJiri Pirko 	mlxsw_sp_acl_tcam_vchunk_migrate_end(mlxsw_sp, vchunk, ctx);
1317e5e7962eSJiri Pirko 	return 0;
1318e5e7962eSJiri Pirko }
1319e5e7962eSJiri Pirko 
1320e5e7962eSJiri Pirko static int
mlxsw_sp_acl_tcam_vchunk_migrate_all(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vregion * vregion,struct mlxsw_sp_acl_tcam_rehash_ctx * ctx,int * credits)1321e5e7962eSJiri Pirko mlxsw_sp_acl_tcam_vchunk_migrate_all(struct mlxsw_sp *mlxsw_sp,
1322220f4fbaSJiri Pirko 				     struct mlxsw_sp_acl_tcam_vregion *vregion,
1323c9c9af91SJiri Pirko 				     struct mlxsw_sp_acl_tcam_rehash_ctx *ctx,
1324c9c9af91SJiri Pirko 				     int *credits)
1325e5e7962eSJiri Pirko {
1326e5e7962eSJiri Pirko 	struct mlxsw_sp_acl_tcam_vchunk *vchunk;
1327e5e7962eSJiri Pirko 	int err;
1328e5e7962eSJiri Pirko 
1329ab4ecfb6SIdo Schimmel 	if (list_empty(&vregion->vchunk_list))
1330ab4ecfb6SIdo Schimmel 		return 0;
1331ab4ecfb6SIdo Schimmel 
13326f9579d4SJiri Pirko 	/* If the migration got interrupted, we have the vchunk
13336f9579d4SJiri Pirko 	 * we are working on stored in context.
13346f9579d4SJiri Pirko 	 */
13356f9579d4SJiri Pirko 	if (ctx->current_vchunk)
13366f9579d4SJiri Pirko 		vchunk = ctx->current_vchunk;
13376f9579d4SJiri Pirko 	else
13386f9579d4SJiri Pirko 		vchunk = list_first_entry(&vregion->vchunk_list,
13396f9579d4SJiri Pirko 					  typeof(*vchunk), list);
13406f9579d4SJiri Pirko 
13416f9579d4SJiri Pirko 	list_for_each_entry_from(vchunk, &vregion->vchunk_list, list) {
1342e5e7962eSJiri Pirko 		err = mlxsw_sp_acl_tcam_vchunk_migrate_one(mlxsw_sp, vchunk,
1343a86838e4SJiri Pirko 							   vregion->region,
1344c9c9af91SJiri Pirko 							   ctx, credits);
1345c9c9af91SJiri Pirko 		if (err || *credits < 0)
134684350051SJiri Pirko 			return err;
1347e5e7962eSJiri Pirko 	}
1348e5e7962eSJiri Pirko 	return 0;
1349e5e7962eSJiri Pirko }
1350e5e7962eSJiri Pirko 
1351e5e7962eSJiri Pirko static int
mlxsw_sp_acl_tcam_vregion_migrate(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vregion * vregion,struct mlxsw_sp_acl_tcam_rehash_ctx * ctx,int * credits)1352e5e7962eSJiri Pirko mlxsw_sp_acl_tcam_vregion_migrate(struct mlxsw_sp *mlxsw_sp,
1353e5e7962eSJiri Pirko 				  struct mlxsw_sp_acl_tcam_vregion *vregion,
1354c9c9af91SJiri Pirko 				  struct mlxsw_sp_acl_tcam_rehash_ctx *ctx,
1355c9c9af91SJiri Pirko 				  int *credits)
1356e5e7962eSJiri Pirko {
135784350051SJiri Pirko 	int err, err2;
1358e5e7962eSJiri Pirko 
13593985de72SJiri Pirko 	trace_mlxsw_sp_acl_tcam_vregion_migrate(mlxsw_sp, vregion);
1360c9c9af91SJiri Pirko 	err = mlxsw_sp_acl_tcam_vchunk_migrate_all(mlxsw_sp, vregion,
1361c9c9af91SJiri Pirko 						   ctx, credits);
136284350051SJiri Pirko 	if (err) {
13630ae8ff7bSIdo Schimmel 		if (ctx->this_is_rollback)
13640ae8ff7bSIdo Schimmel 			return err;
136584350051SJiri Pirko 		/* In case migration was not successful, we need to swap
136684350051SJiri Pirko 		 * so the original region pointer is assigned again
136784350051SJiri Pirko 		 * to vregion->region.
136884350051SJiri Pirko 		 */
136984350051SJiri Pirko 		swap(vregion->region, vregion->region2);
1370e8904560SIdo Schimmel 		mlxsw_sp_acl_tcam_rehash_ctx_vchunk_reset(ctx);
137184350051SJiri Pirko 		ctx->this_is_rollback = true;
1372c9c9af91SJiri Pirko 		err2 = mlxsw_sp_acl_tcam_vchunk_migrate_all(mlxsw_sp, vregion,
1373c9c9af91SJiri Pirko 							    ctx, credits);
1374f3d4ef1aSJiri Pirko 		if (err2) {
1375a4e76ba6SJiri Pirko 			trace_mlxsw_sp_acl_tcam_vregion_rehash_rollback_failed(mlxsw_sp,
1376f3d4ef1aSJiri Pirko 									       vregion);
1377f3d4ef1aSJiri Pirko 			dev_err(mlxsw_sp->bus_info->dev, "Failed to rollback during vregion migration fail\n");
13787c33c72bSJiri Pirko 			/* Let the rollback to be continued later on. */
1379f3d4ef1aSJiri Pirko 		}
138084350051SJiri Pirko 	}
1381a9550d0fSJiri Pirko 	trace_mlxsw_sp_acl_tcam_vregion_migrate_end(mlxsw_sp, vregion);
1382a9550d0fSJiri Pirko 	return err;
1383a9550d0fSJiri Pirko }
1384a9550d0fSJiri Pirko 
1385c9c9af91SJiri Pirko static bool
mlxsw_sp_acl_tcam_vregion_rehash_in_progress(const struct mlxsw_sp_acl_tcam_rehash_ctx * ctx)1386c9c9af91SJiri Pirko mlxsw_sp_acl_tcam_vregion_rehash_in_progress(const struct mlxsw_sp_acl_tcam_rehash_ctx *ctx)
1387c9c9af91SJiri Pirko {
1388c9c9af91SJiri Pirko 	return ctx->hints_priv;
1389c9c9af91SJiri Pirko }
1390c9c9af91SJiri Pirko 
1391a9550d0fSJiri Pirko static int
mlxsw_sp_acl_tcam_vregion_rehash_start(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vregion * vregion,struct mlxsw_sp_acl_tcam_rehash_ctx * ctx)1392a9550d0fSJiri Pirko mlxsw_sp_acl_tcam_vregion_rehash_start(struct mlxsw_sp *mlxsw_sp,
1393a9550d0fSJiri Pirko 				       struct mlxsw_sp_acl_tcam_vregion *vregion,
1394a9550d0fSJiri Pirko 				       struct mlxsw_sp_acl_tcam_rehash_ctx *ctx)
1395a9550d0fSJiri Pirko {
1396a9550d0fSJiri Pirko 	const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
1397a9550d0fSJiri Pirko 	unsigned int priority = mlxsw_sp_acl_tcam_vregion_prio(vregion);
1398a86838e4SJiri Pirko 	struct mlxsw_sp_acl_tcam_region *new_region;
1399a9550d0fSJiri Pirko 	void *hints_priv;
1400a9550d0fSJiri Pirko 	int err;
1401a9550d0fSJiri Pirko 
1402a9550d0fSJiri Pirko 	trace_mlxsw_sp_acl_tcam_vregion_rehash(mlxsw_sp, vregion);
1403a9550d0fSJiri Pirko 
1404a9550d0fSJiri Pirko 	hints_priv = ops->region_rehash_hints_get(vregion->region->priv);
1405a9550d0fSJiri Pirko 	if (IS_ERR(hints_priv))
1406a9550d0fSJiri Pirko 		return PTR_ERR(hints_priv);
14073985de72SJiri Pirko 
1408a86838e4SJiri Pirko 	new_region = mlxsw_sp_acl_tcam_region_create(mlxsw_sp, vregion->tcam,
1409a9550d0fSJiri Pirko 						     vregion, hints_priv);
1410a86838e4SJiri Pirko 	if (IS_ERR(new_region)) {
1411a86838e4SJiri Pirko 		err = PTR_ERR(new_region);
1412a9550d0fSJiri Pirko 		goto err_region_create;
14136375da3dSJiri Pirko 	}
1414e5e7962eSJiri Pirko 
1415a86838e4SJiri Pirko 	/* vregion->region contains the pointer to the new region
1416a86838e4SJiri Pirko 	 * we are going to migrate to.
1417a86838e4SJiri Pirko 	 */
1418a86838e4SJiri Pirko 	vregion->region2 = vregion->region;
1419a86838e4SJiri Pirko 	vregion->region = new_region;
14202802aadfSJiri Pirko 	err = mlxsw_sp_acl_tcam_group_region_attach(mlxsw_sp,
1421a86838e4SJiri Pirko 						    vregion->region2->group,
1422a86838e4SJiri Pirko 						    new_region, priority,
1423a86838e4SJiri Pirko 						    vregion->region2);
1424e5e7962eSJiri Pirko 	if (err)
1425e5e7962eSJiri Pirko 		goto err_group_region_attach;
1426e5e7962eSJiri Pirko 
1427a9550d0fSJiri Pirko 	ctx->hints_priv = hints_priv;
1428220f4fbaSJiri Pirko 	ctx->this_is_rollback = false;
1429e8904560SIdo Schimmel 	mlxsw_sp_acl_tcam_rehash_ctx_vchunk_reset(ctx);
14301263a9abSJiri Pirko 
1431a9550d0fSJiri Pirko 	return 0;
1432e5e7962eSJiri Pirko 
1433e5e7962eSJiri Pirko err_group_region_attach:
1434a86838e4SJiri Pirko 	vregion->region = vregion->region2;
1435e5e7962eSJiri Pirko 	vregion->region2 = NULL;
1436a86838e4SJiri Pirko 	mlxsw_sp_acl_tcam_region_destroy(mlxsw_sp, new_region);
1437a9550d0fSJiri Pirko err_region_create:
1438a9550d0fSJiri Pirko 	ops->region_rehash_hints_put(hints_priv);
1439e5e7962eSJiri Pirko 	return err;
1440e5e7962eSJiri Pirko }
1441e5e7962eSJiri Pirko 
14421667f766SJiri Pirko static void
mlxsw_sp_acl_tcam_vregion_rehash_end(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vregion * vregion,struct mlxsw_sp_acl_tcam_rehash_ctx * ctx)14431667f766SJiri Pirko mlxsw_sp_acl_tcam_vregion_rehash_end(struct mlxsw_sp *mlxsw_sp,
14441667f766SJiri Pirko 				     struct mlxsw_sp_acl_tcam_vregion *vregion,
14451667f766SJiri Pirko 				     struct mlxsw_sp_acl_tcam_rehash_ctx *ctx)
14461667f766SJiri Pirko {
1447a9550d0fSJiri Pirko 	struct mlxsw_sp_acl_tcam_region *unused_region = vregion->region2;
14481667f766SJiri Pirko 	const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
14491667f766SJiri Pirko 
1450a9550d0fSJiri Pirko 	vregion->region2 = NULL;
1451a9550d0fSJiri Pirko 	mlxsw_sp_acl_tcam_group_region_detach(mlxsw_sp, unused_region);
1452a9550d0fSJiri Pirko 	mlxsw_sp_acl_tcam_region_destroy(mlxsw_sp, unused_region);
14531667f766SJiri Pirko 	ops->region_rehash_hints_put(ctx->hints_priv);
14541667f766SJiri Pirko 	ctx->hints_priv = NULL;
14551667f766SJiri Pirko }
14561667f766SJiri Pirko 
1457b2c091ceSJiri Pirko static void
mlxsw_sp_acl_tcam_vregion_rehash(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_vregion * vregion,int * credits)14581667f766SJiri Pirko mlxsw_sp_acl_tcam_vregion_rehash(struct mlxsw_sp *mlxsw_sp,
1459c9c9af91SJiri Pirko 				 struct mlxsw_sp_acl_tcam_vregion *vregion,
1460c9c9af91SJiri Pirko 				 int *credits)
14611667f766SJiri Pirko {
14621667f766SJiri Pirko 	struct mlxsw_sp_acl_tcam_rehash_ctx *ctx = &vregion->rehash.ctx;
14631667f766SJiri Pirko 	int err;
14641667f766SJiri Pirko 
1465c9c9af91SJiri Pirko 	/* Check if the previous rehash work was interrupted
1466c9c9af91SJiri Pirko 	 * which means we have to continue it now.
1467c9c9af91SJiri Pirko 	 * If not, start a new rehash.
1468c9c9af91SJiri Pirko 	 */
1469c9c9af91SJiri Pirko 	if (!mlxsw_sp_acl_tcam_vregion_rehash_in_progress(ctx)) {
1470c9c9af91SJiri Pirko 		err = mlxsw_sp_acl_tcam_vregion_rehash_start(mlxsw_sp,
1471c9c9af91SJiri Pirko 							     vregion, ctx);
14721667f766SJiri Pirko 		if (err) {
1473e5e7962eSJiri Pirko 			if (err != -EAGAIN)
1474e5e7962eSJiri Pirko 				dev_err(mlxsw_sp->bus_info->dev, "Failed get rehash hints\n");
1475b2c091ceSJiri Pirko 			return;
1476e5e7962eSJiri Pirko 		}
1477c9c9af91SJiri Pirko 	}
1478e5e7962eSJiri Pirko 
1479c9c9af91SJiri Pirko 	err = mlxsw_sp_acl_tcam_vregion_migrate(mlxsw_sp, vregion,
1480c9c9af91SJiri Pirko 						ctx, credits);
1481e5e7962eSJiri Pirko 	if (err) {
14823da432f3SIdo Schimmel 		dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to migrate vregion\n");
1483311eeaa7SIdo Schimmel 		return;
14843985de72SJiri Pirko 	}
1485e5e7962eSJiri Pirko 
1486c9c9af91SJiri Pirko 	if (*credits >= 0)
14871667f766SJiri Pirko 		mlxsw_sp_acl_tcam_vregion_rehash_end(mlxsw_sp, vregion, ctx);
1488e5e7962eSJiri Pirko }
1489e5e7962eSJiri Pirko 
149074cbc3c0SIdo Schimmel static int
mlxsw_sp_acl_tcam_region_rehash_intrvl_get(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)149174cbc3c0SIdo Schimmel mlxsw_sp_acl_tcam_region_rehash_intrvl_get(struct devlink *devlink, u32 id,
149274cbc3c0SIdo Schimmel 					   struct devlink_param_gset_ctx *ctx)
149374cbc3c0SIdo Schimmel {
149474cbc3c0SIdo Schimmel 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
149574cbc3c0SIdo Schimmel 	struct mlxsw_sp_acl_tcam *tcam;
149674cbc3c0SIdo Schimmel 	struct mlxsw_sp *mlxsw_sp;
149774cbc3c0SIdo Schimmel 
149874cbc3c0SIdo Schimmel 	mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
149974cbc3c0SIdo Schimmel 	tcam = mlxsw_sp_acl_to_tcam(mlxsw_sp->acl);
150074cbc3c0SIdo Schimmel 	ctx->val.vu32 = tcam->vregion_rehash_intrvl;
150174cbc3c0SIdo Schimmel 
150274cbc3c0SIdo Schimmel 	return 0;
150374cbc3c0SIdo Schimmel }
150474cbc3c0SIdo Schimmel 
150574cbc3c0SIdo Schimmel static int
mlxsw_sp_acl_tcam_region_rehash_intrvl_set(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)150674cbc3c0SIdo Schimmel mlxsw_sp_acl_tcam_region_rehash_intrvl_set(struct devlink *devlink, u32 id,
150774cbc3c0SIdo Schimmel 					   struct devlink_param_gset_ctx *ctx)
150874cbc3c0SIdo Schimmel {
150974cbc3c0SIdo Schimmel 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
151074cbc3c0SIdo Schimmel 	struct mlxsw_sp_acl_tcam_vregion *vregion;
151174cbc3c0SIdo Schimmel 	struct mlxsw_sp_acl_tcam *tcam;
151274cbc3c0SIdo Schimmel 	struct mlxsw_sp *mlxsw_sp;
151374cbc3c0SIdo Schimmel 	u32 val = ctx->val.vu32;
151474cbc3c0SIdo Schimmel 
151574cbc3c0SIdo Schimmel 	if (val < MLXSW_SP_ACL_TCAM_VREGION_REHASH_INTRVL_MIN && val)
151674cbc3c0SIdo Schimmel 		return -EINVAL;
151774cbc3c0SIdo Schimmel 
151874cbc3c0SIdo Schimmel 	mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
151974cbc3c0SIdo Schimmel 	tcam = mlxsw_sp_acl_to_tcam(mlxsw_sp->acl);
152074cbc3c0SIdo Schimmel 	tcam->vregion_rehash_intrvl = val;
152174cbc3c0SIdo Schimmel 	mutex_lock(&tcam->lock);
152274cbc3c0SIdo Schimmel 	list_for_each_entry(vregion, &tcam->vregion_list, tlist) {
152374cbc3c0SIdo Schimmel 		if (val)
152474cbc3c0SIdo Schimmel 			mlxsw_core_schedule_dw(&vregion->rehash.dw, 0);
152574cbc3c0SIdo Schimmel 		else
152674cbc3c0SIdo Schimmel 			cancel_delayed_work_sync(&vregion->rehash.dw);
152774cbc3c0SIdo Schimmel 	}
152874cbc3c0SIdo Schimmel 	mutex_unlock(&tcam->lock);
152974cbc3c0SIdo Schimmel 	return 0;
153074cbc3c0SIdo Schimmel }
153174cbc3c0SIdo Schimmel 
153274cbc3c0SIdo Schimmel static const struct devlink_param mlxsw_sp_acl_tcam_rehash_params[] = {
153374cbc3c0SIdo Schimmel 	DEVLINK_PARAM_DRIVER(MLXSW_DEVLINK_PARAM_ID_ACL_REGION_REHASH_INTERVAL,
153474cbc3c0SIdo Schimmel 			     "acl_region_rehash_interval",
153574cbc3c0SIdo Schimmel 			     DEVLINK_PARAM_TYPE_U32,
153674cbc3c0SIdo Schimmel 			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
153774cbc3c0SIdo Schimmel 			     mlxsw_sp_acl_tcam_region_rehash_intrvl_get,
153874cbc3c0SIdo Schimmel 			     mlxsw_sp_acl_tcam_region_rehash_intrvl_set,
153974cbc3c0SIdo Schimmel 			     NULL),
154074cbc3c0SIdo Schimmel };
154174cbc3c0SIdo Schimmel 
mlxsw_sp_acl_tcam_rehash_params_register(struct mlxsw_sp * mlxsw_sp)154274cbc3c0SIdo Schimmel static int mlxsw_sp_acl_tcam_rehash_params_register(struct mlxsw_sp *mlxsw_sp)
154374cbc3c0SIdo Schimmel {
154474cbc3c0SIdo Schimmel 	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
154574cbc3c0SIdo Schimmel 
154674cbc3c0SIdo Schimmel 	if (!mlxsw_sp->acl_tcam_ops->region_rehash_hints_get)
154774cbc3c0SIdo Schimmel 		return 0;
154874cbc3c0SIdo Schimmel 
154974cbc3c0SIdo Schimmel 	return devl_params_register(devlink, mlxsw_sp_acl_tcam_rehash_params,
155074cbc3c0SIdo Schimmel 				    ARRAY_SIZE(mlxsw_sp_acl_tcam_rehash_params));
155174cbc3c0SIdo Schimmel }
155274cbc3c0SIdo Schimmel 
155374cbc3c0SIdo Schimmel static void
mlxsw_sp_acl_tcam_rehash_params_unregister(struct mlxsw_sp * mlxsw_sp)155474cbc3c0SIdo Schimmel mlxsw_sp_acl_tcam_rehash_params_unregister(struct mlxsw_sp *mlxsw_sp)
155574cbc3c0SIdo Schimmel {
155674cbc3c0SIdo Schimmel 	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
155774cbc3c0SIdo Schimmel 
155874cbc3c0SIdo Schimmel 	if (!mlxsw_sp->acl_tcam_ops->region_rehash_hints_get)
155974cbc3c0SIdo Schimmel 		return;
156074cbc3c0SIdo Schimmel 
156174cbc3c0SIdo Schimmel 	devl_params_unregister(devlink, mlxsw_sp_acl_tcam_rehash_params,
156274cbc3c0SIdo Schimmel 			       ARRAY_SIZE(mlxsw_sp_acl_tcam_rehash_params));
156374cbc3c0SIdo Schimmel }
156474cbc3c0SIdo Schimmel 
mlxsw_sp_acl_tcam_init(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam * tcam)1565194ab947SIdo Schimmel int mlxsw_sp_acl_tcam_init(struct mlxsw_sp *mlxsw_sp,
1566194ab947SIdo Schimmel 			   struct mlxsw_sp_acl_tcam *tcam)
1567194ab947SIdo Schimmel {
1568194ab947SIdo Schimmel 	const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
1569194ab947SIdo Schimmel 	u64 max_tcam_regions;
1570194ab947SIdo Schimmel 	u64 max_regions;
1571194ab947SIdo Schimmel 	u64 max_groups;
1572194ab947SIdo Schimmel 	int err;
1573194ab947SIdo Schimmel 
1574194ab947SIdo Schimmel 	mutex_init(&tcam->lock);
1575194ab947SIdo Schimmel 	tcam->vregion_rehash_intrvl =
1576194ab947SIdo Schimmel 			MLXSW_SP_ACL_TCAM_VREGION_REHASH_INTRVL_DFLT;
1577194ab947SIdo Schimmel 	INIT_LIST_HEAD(&tcam->vregion_list);
1578194ab947SIdo Schimmel 
157974cbc3c0SIdo Schimmel 	err = mlxsw_sp_acl_tcam_rehash_params_register(mlxsw_sp);
158074cbc3c0SIdo Schimmel 	if (err)
158174cbc3c0SIdo Schimmel 		goto err_rehash_params_register;
158274cbc3c0SIdo Schimmel 
1583194ab947SIdo Schimmel 	max_tcam_regions = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1584194ab947SIdo Schimmel 					      ACL_MAX_TCAM_REGIONS);
1585194ab947SIdo Schimmel 	max_regions = MLXSW_CORE_RES_GET(mlxsw_sp->core, ACL_MAX_REGIONS);
1586194ab947SIdo Schimmel 
1587194ab947SIdo Schimmel 	/* Use 1:1 mapping between ACL region and TCAM region */
1588194ab947SIdo Schimmel 	if (max_tcam_regions < max_regions)
1589194ab947SIdo Schimmel 		max_regions = max_tcam_regions;
1590194ab947SIdo Schimmel 
159179736f57SIdo Schimmel 	ida_init(&tcam->used_regions);
1592194ab947SIdo Schimmel 	tcam->max_regions = max_regions;
1593194ab947SIdo Schimmel 
1594194ab947SIdo Schimmel 	max_groups = MLXSW_CORE_RES_GET(mlxsw_sp->core, ACL_MAX_GROUPS);
159579736f57SIdo Schimmel 	ida_init(&tcam->used_groups);
1596194ab947SIdo Schimmel 	tcam->max_groups = max_groups;
1597194ab947SIdo Schimmel 	tcam->max_group_size = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1598194ab947SIdo Schimmel 						  ACL_MAX_GROUP_SIZE);
15992f5e1565SIdo Schimmel 	tcam->max_group_size = min_t(unsigned int, tcam->max_group_size,
16002f5e1565SIdo Schimmel 				     MLXSW_REG_PAGT_ACL_MAX_NUM);
1601194ab947SIdo Schimmel 
1602194ab947SIdo Schimmel 	err = ops->init(mlxsw_sp, tcam->priv, tcam);
1603194ab947SIdo Schimmel 	if (err)
1604194ab947SIdo Schimmel 		goto err_tcam_init;
1605194ab947SIdo Schimmel 
1606194ab947SIdo Schimmel 	return 0;
1607194ab947SIdo Schimmel 
1608194ab947SIdo Schimmel err_tcam_init:
160979736f57SIdo Schimmel 	ida_destroy(&tcam->used_groups);
161079736f57SIdo Schimmel 	ida_destroy(&tcam->used_regions);
161174cbc3c0SIdo Schimmel 	mlxsw_sp_acl_tcam_rehash_params_unregister(mlxsw_sp);
161274cbc3c0SIdo Schimmel err_rehash_params_register:
1613194ab947SIdo Schimmel 	mutex_destroy(&tcam->lock);
1614194ab947SIdo Schimmel 	return err;
1615194ab947SIdo Schimmel }
1616194ab947SIdo Schimmel 
mlxsw_sp_acl_tcam_fini(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam * tcam)1617194ab947SIdo Schimmel void mlxsw_sp_acl_tcam_fini(struct mlxsw_sp *mlxsw_sp,
1618194ab947SIdo Schimmel 			    struct mlxsw_sp_acl_tcam *tcam)
1619194ab947SIdo Schimmel {
1620194ab947SIdo Schimmel 	const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
1621194ab947SIdo Schimmel 
1622194ab947SIdo Schimmel 	ops->fini(mlxsw_sp, tcam->priv);
162379736f57SIdo Schimmel 	ida_destroy(&tcam->used_groups);
162479736f57SIdo Schimmel 	ida_destroy(&tcam->used_regions);
162574cbc3c0SIdo Schimmel 	mlxsw_sp_acl_tcam_rehash_params_unregister(mlxsw_sp);
1626194ab947SIdo Schimmel 	mutex_destroy(&tcam->lock);
1627194ab947SIdo Schimmel }
1628194ab947SIdo Schimmel 
162922a67766SJiri Pirko static const enum mlxsw_afk_element mlxsw_sp_acl_tcam_pattern_ipv4[] = {
163022a67766SJiri Pirko 	MLXSW_AFK_ELEMENT_SRC_SYS_PORT,
1631c43ea06dSJiri Pirko 	MLXSW_AFK_ELEMENT_DMAC_32_47,
1632c43ea06dSJiri Pirko 	MLXSW_AFK_ELEMENT_DMAC_0_31,
1633c43ea06dSJiri Pirko 	MLXSW_AFK_ELEMENT_SMAC_32_47,
1634c43ea06dSJiri Pirko 	MLXSW_AFK_ELEMENT_SMAC_0_31,
163522a67766SJiri Pirko 	MLXSW_AFK_ELEMENT_ETHERTYPE,
163622a67766SJiri Pirko 	MLXSW_AFK_ELEMENT_IP_PROTO,
1637c43ea06dSJiri Pirko 	MLXSW_AFK_ELEMENT_SRC_IP_0_31,
1638c43ea06dSJiri Pirko 	MLXSW_AFK_ELEMENT_DST_IP_0_31,
163922a67766SJiri Pirko 	MLXSW_AFK_ELEMENT_DST_L4_PORT,
164022a67766SJiri Pirko 	MLXSW_AFK_ELEMENT_SRC_L4_PORT,
16419caab08aSPetr Machata 	MLXSW_AFK_ELEMENT_VID,
16429caab08aSPetr Machata 	MLXSW_AFK_ELEMENT_PCP,
16438a41d845SJiri Pirko 	MLXSW_AFK_ELEMENT_TCP_FLAGS,
1644046759a6SOr Gerlitz 	MLXSW_AFK_ELEMENT_IP_TTL_,
1645abac7b01SOr Gerlitz 	MLXSW_AFK_ELEMENT_IP_ECN,
1646abac7b01SOr Gerlitz 	MLXSW_AFK_ELEMENT_IP_DSCP,
164722a67766SJiri Pirko };
164822a67766SJiri Pirko 
164922a67766SJiri Pirko static const enum mlxsw_afk_element mlxsw_sp_acl_tcam_pattern_ipv6[] = {
165022a67766SJiri Pirko 	MLXSW_AFK_ELEMENT_ETHERTYPE,
165122a67766SJiri Pirko 	MLXSW_AFK_ELEMENT_IP_PROTO,
1652c43ea06dSJiri Pirko 	MLXSW_AFK_ELEMENT_SRC_IP_96_127,
1653c43ea06dSJiri Pirko 	MLXSW_AFK_ELEMENT_SRC_IP_64_95,
1654c43ea06dSJiri Pirko 	MLXSW_AFK_ELEMENT_SRC_IP_32_63,
1655c43ea06dSJiri Pirko 	MLXSW_AFK_ELEMENT_SRC_IP_0_31,
1656c43ea06dSJiri Pirko 	MLXSW_AFK_ELEMENT_DST_IP_96_127,
1657c43ea06dSJiri Pirko 	MLXSW_AFK_ELEMENT_DST_IP_64_95,
1658c43ea06dSJiri Pirko 	MLXSW_AFK_ELEMENT_DST_IP_32_63,
1659c43ea06dSJiri Pirko 	MLXSW_AFK_ELEMENT_DST_IP_0_31,
166022a67766SJiri Pirko 	MLXSW_AFK_ELEMENT_DST_L4_PORT,
166122a67766SJiri Pirko 	MLXSW_AFK_ELEMENT_SRC_L4_PORT,
166222a67766SJiri Pirko };
166322a67766SJiri Pirko 
166422a67766SJiri Pirko static const struct mlxsw_sp_acl_tcam_pattern mlxsw_sp_acl_tcam_patterns[] = {
166522a67766SJiri Pirko 	{
166622a67766SJiri Pirko 		.elements = mlxsw_sp_acl_tcam_pattern_ipv4,
166722a67766SJiri Pirko 		.elements_count = ARRAY_SIZE(mlxsw_sp_acl_tcam_pattern_ipv4),
166822a67766SJiri Pirko 	},
166922a67766SJiri Pirko 	{
167022a67766SJiri Pirko 		.elements = mlxsw_sp_acl_tcam_pattern_ipv6,
167122a67766SJiri Pirko 		.elements_count = ARRAY_SIZE(mlxsw_sp_acl_tcam_pattern_ipv6),
167222a67766SJiri Pirko 	},
167322a67766SJiri Pirko };
167422a67766SJiri Pirko 
167522a67766SJiri Pirko #define MLXSW_SP_ACL_TCAM_PATTERNS_COUNT \
167622a67766SJiri Pirko 	ARRAY_SIZE(mlxsw_sp_acl_tcam_patterns)
167722a67766SJiri Pirko 
167822a67766SJiri Pirko struct mlxsw_sp_acl_tcam_flower_ruleset {
16792802aadfSJiri Pirko 	struct mlxsw_sp_acl_tcam_vgroup vgroup;
168022a67766SJiri Pirko };
168122a67766SJiri Pirko 
168222a67766SJiri Pirko struct mlxsw_sp_acl_tcam_flower_rule {
1683c4c2dc54SJiri Pirko 	struct mlxsw_sp_acl_tcam_ventry ventry;
168422a67766SJiri Pirko };
168522a67766SJiri Pirko 
168622a67766SJiri Pirko static int
mlxsw_sp_acl_tcam_flower_ruleset_add(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam * tcam,void * ruleset_priv,struct mlxsw_afk_element_usage * tmplt_elusage,unsigned int * p_min_prio,unsigned int * p_max_prio)168722a67766SJiri Pirko mlxsw_sp_acl_tcam_flower_ruleset_add(struct mlxsw_sp *mlxsw_sp,
1688bab5c1cfSJiri Pirko 				     struct mlxsw_sp_acl_tcam *tcam,
1689e2f2a1fdSJiri Pirko 				     void *ruleset_priv,
1690593bb843SJiri Pirko 				     struct mlxsw_afk_element_usage *tmplt_elusage,
1691593bb843SJiri Pirko 				     unsigned int *p_min_prio,
1692593bb843SJiri Pirko 				     unsigned int *p_max_prio)
169322a67766SJiri Pirko {
169422a67766SJiri Pirko 	struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
169522a67766SJiri Pirko 
16962802aadfSJiri Pirko 	return mlxsw_sp_acl_tcam_vgroup_add(mlxsw_sp, tcam, &ruleset->vgroup,
169722a67766SJiri Pirko 					    mlxsw_sp_acl_tcam_patterns,
1698e2f2a1fdSJiri Pirko 					    MLXSW_SP_ACL_TCAM_PATTERNS_COUNT,
1699593bb843SJiri Pirko 					    tmplt_elusage, true,
1700593bb843SJiri Pirko 					    p_min_prio, p_max_prio);
170122a67766SJiri Pirko }
170222a67766SJiri Pirko 
170322a67766SJiri Pirko static void
mlxsw_sp_acl_tcam_flower_ruleset_del(struct mlxsw_sp * mlxsw_sp,void * ruleset_priv)170422a67766SJiri Pirko mlxsw_sp_acl_tcam_flower_ruleset_del(struct mlxsw_sp *mlxsw_sp,
170522a67766SJiri Pirko 				     void *ruleset_priv)
170622a67766SJiri Pirko {
170722a67766SJiri Pirko 	struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
170822a67766SJiri Pirko 
17092802aadfSJiri Pirko 	mlxsw_sp_acl_tcam_vgroup_del(&ruleset->vgroup);
171022a67766SJiri Pirko }
171122a67766SJiri Pirko 
171222a67766SJiri Pirko static int
mlxsw_sp_acl_tcam_flower_ruleset_bind(struct mlxsw_sp * mlxsw_sp,void * ruleset_priv,struct mlxsw_sp_port * mlxsw_sp_port,bool ingress)171322a67766SJiri Pirko mlxsw_sp_acl_tcam_flower_ruleset_bind(struct mlxsw_sp *mlxsw_sp,
171422a67766SJiri Pirko 				      void *ruleset_priv,
17154b23258dSJiri Pirko 				      struct mlxsw_sp_port *mlxsw_sp_port,
17164b23258dSJiri Pirko 				      bool ingress)
171722a67766SJiri Pirko {
171822a67766SJiri Pirko 	struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
171922a67766SJiri Pirko 
17202802aadfSJiri Pirko 	return mlxsw_sp_acl_tcam_group_bind(mlxsw_sp, &ruleset->vgroup.group,
17214b23258dSJiri Pirko 					    mlxsw_sp_port, ingress);
172222a67766SJiri Pirko }
172322a67766SJiri Pirko 
172422a67766SJiri Pirko static void
mlxsw_sp_acl_tcam_flower_ruleset_unbind(struct mlxsw_sp * mlxsw_sp,void * ruleset_priv,struct mlxsw_sp_port * mlxsw_sp_port,bool ingress)172522a67766SJiri Pirko mlxsw_sp_acl_tcam_flower_ruleset_unbind(struct mlxsw_sp *mlxsw_sp,
172602caf499SJiri Pirko 					void *ruleset_priv,
17274b23258dSJiri Pirko 					struct mlxsw_sp_port *mlxsw_sp_port,
17284b23258dSJiri Pirko 					bool ingress)
172922a67766SJiri Pirko {
173022a67766SJiri Pirko 	struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
173122a67766SJiri Pirko 
17322802aadfSJiri Pirko 	mlxsw_sp_acl_tcam_group_unbind(mlxsw_sp, &ruleset->vgroup.group,
17334b23258dSJiri Pirko 				       mlxsw_sp_port, ingress);
173422a67766SJiri Pirko }
173522a67766SJiri Pirko 
17360ade3b64SJiri Pirko static u16
mlxsw_sp_acl_tcam_flower_ruleset_group_id(void * ruleset_priv)17370ade3b64SJiri Pirko mlxsw_sp_acl_tcam_flower_ruleset_group_id(void *ruleset_priv)
17380ade3b64SJiri Pirko {
17390ade3b64SJiri Pirko 	struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
17400ade3b64SJiri Pirko 
17412802aadfSJiri Pirko 	return mlxsw_sp_acl_tcam_group_id(&ruleset->vgroup.group);
17420ade3b64SJiri Pirko }
17430ade3b64SJiri Pirko 
174422a67766SJiri Pirko static int
mlxsw_sp_acl_tcam_flower_rule_add(struct mlxsw_sp * mlxsw_sp,void * ruleset_priv,void * rule_priv,struct mlxsw_sp_acl_rule_info * rulei)174522a67766SJiri Pirko mlxsw_sp_acl_tcam_flower_rule_add(struct mlxsw_sp *mlxsw_sp,
174622a67766SJiri Pirko 				  void *ruleset_priv, void *rule_priv,
174722a67766SJiri Pirko 				  struct mlxsw_sp_acl_rule_info *rulei)
174822a67766SJiri Pirko {
174922a67766SJiri Pirko 	struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
175022a67766SJiri Pirko 	struct mlxsw_sp_acl_tcam_flower_rule *rule = rule_priv;
175122a67766SJiri Pirko 
17522802aadfSJiri Pirko 	return mlxsw_sp_acl_tcam_ventry_add(mlxsw_sp, &ruleset->vgroup,
1753c4c2dc54SJiri Pirko 					    &rule->ventry, rulei);
175422a67766SJiri Pirko }
175522a67766SJiri Pirko 
175622a67766SJiri Pirko static void
mlxsw_sp_acl_tcam_flower_rule_del(struct mlxsw_sp * mlxsw_sp,void * rule_priv)175722a67766SJiri Pirko mlxsw_sp_acl_tcam_flower_rule_del(struct mlxsw_sp *mlxsw_sp, void *rule_priv)
175822a67766SJiri Pirko {
175922a67766SJiri Pirko 	struct mlxsw_sp_acl_tcam_flower_rule *rule = rule_priv;
176022a67766SJiri Pirko 
1761c4c2dc54SJiri Pirko 	mlxsw_sp_acl_tcam_ventry_del(mlxsw_sp, &rule->ventry);
176222a67766SJiri Pirko }
176322a67766SJiri Pirko 
17647fd056c2SArkadi Sharshevsky static int
mlxsw_sp_acl_tcam_flower_rule_action_replace(struct mlxsw_sp * mlxsw_sp,void * rule_priv,struct mlxsw_sp_acl_rule_info * rulei)17652507a64cSNir Dotan mlxsw_sp_acl_tcam_flower_rule_action_replace(struct mlxsw_sp *mlxsw_sp,
17662507a64cSNir Dotan 					     void *rule_priv,
17672507a64cSNir Dotan 					     struct mlxsw_sp_acl_rule_info *rulei)
17682507a64cSNir Dotan {
17692507a64cSNir Dotan 	return -EOPNOTSUPP;
17702507a64cSNir Dotan }
17712507a64cSNir Dotan 
17722507a64cSNir Dotan static int
mlxsw_sp_acl_tcam_flower_rule_activity_get(struct mlxsw_sp * mlxsw_sp,void * rule_priv,bool * activity)17737fd056c2SArkadi Sharshevsky mlxsw_sp_acl_tcam_flower_rule_activity_get(struct mlxsw_sp *mlxsw_sp,
17747fd056c2SArkadi Sharshevsky 					   void *rule_priv, bool *activity)
17757fd056c2SArkadi Sharshevsky {
17767fd056c2SArkadi Sharshevsky 	struct mlxsw_sp_acl_tcam_flower_rule *rule = rule_priv;
17777fd056c2SArkadi Sharshevsky 
1778c4c2dc54SJiri Pirko 	return mlxsw_sp_acl_tcam_ventry_activity_get(mlxsw_sp, &rule->ventry,
17797fd056c2SArkadi Sharshevsky 						     activity);
17807fd056c2SArkadi Sharshevsky }
17817fd056c2SArkadi Sharshevsky 
178222a67766SJiri Pirko static const struct mlxsw_sp_acl_profile_ops mlxsw_sp_acl_tcam_flower_ops = {
178322a67766SJiri Pirko 	.ruleset_priv_size	= sizeof(struct mlxsw_sp_acl_tcam_flower_ruleset),
178422a67766SJiri Pirko 	.ruleset_add		= mlxsw_sp_acl_tcam_flower_ruleset_add,
178522a67766SJiri Pirko 	.ruleset_del		= mlxsw_sp_acl_tcam_flower_ruleset_del,
178622a67766SJiri Pirko 	.ruleset_bind		= mlxsw_sp_acl_tcam_flower_ruleset_bind,
178722a67766SJiri Pirko 	.ruleset_unbind		= mlxsw_sp_acl_tcam_flower_ruleset_unbind,
17880ade3b64SJiri Pirko 	.ruleset_group_id	= mlxsw_sp_acl_tcam_flower_ruleset_group_id,
1789c4c2dc54SJiri Pirko 	.rule_priv_size		= sizeof(struct mlxsw_sp_acl_tcam_flower_rule),
179022a67766SJiri Pirko 	.rule_add		= mlxsw_sp_acl_tcam_flower_rule_add,
179122a67766SJiri Pirko 	.rule_del		= mlxsw_sp_acl_tcam_flower_rule_del,
17922507a64cSNir Dotan 	.rule_action_replace	= mlxsw_sp_acl_tcam_flower_rule_action_replace,
17937fd056c2SArkadi Sharshevsky 	.rule_activity_get	= mlxsw_sp_acl_tcam_flower_rule_activity_get,
179422a67766SJiri Pirko };
179522a67766SJiri Pirko 
1796038418eeSJiri Pirko struct mlxsw_sp_acl_tcam_mr_ruleset {
1797b2d6b4d2SJiri Pirko 	struct mlxsw_sp_acl_tcam_vchunk *vchunk;
17982802aadfSJiri Pirko 	struct mlxsw_sp_acl_tcam_vgroup vgroup;
1799038418eeSJiri Pirko };
1800038418eeSJiri Pirko 
1801038418eeSJiri Pirko struct mlxsw_sp_acl_tcam_mr_rule {
1802c4c2dc54SJiri Pirko 	struct mlxsw_sp_acl_tcam_ventry ventry;
1803038418eeSJiri Pirko };
1804038418eeSJiri Pirko 
18051a29d293SNir Dotan static int
mlxsw_sp_acl_tcam_mr_ruleset_add(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam * tcam,void * ruleset_priv,struct mlxsw_afk_element_usage * tmplt_elusage,unsigned int * p_min_prio,unsigned int * p_max_prio)18061a29d293SNir Dotan mlxsw_sp_acl_tcam_mr_ruleset_add(struct mlxsw_sp *mlxsw_sp,
18071a29d293SNir Dotan 				 struct mlxsw_sp_acl_tcam *tcam,
18081a29d293SNir Dotan 				 void *ruleset_priv,
1809593bb843SJiri Pirko 				 struct mlxsw_afk_element_usage *tmplt_elusage,
1810593bb843SJiri Pirko 				 unsigned int *p_min_prio,
1811593bb843SJiri Pirko 				 unsigned int *p_max_prio)
18121a29d293SNir Dotan {
18131a29d293SNir Dotan 	struct mlxsw_sp_acl_tcam_mr_ruleset *ruleset = ruleset_priv;
18141a29d293SNir Dotan 	int err;
18151a29d293SNir Dotan 
18162802aadfSJiri Pirko 	err = mlxsw_sp_acl_tcam_vgroup_add(mlxsw_sp, tcam, &ruleset->vgroup,
18171a29d293SNir Dotan 					   mlxsw_sp_acl_tcam_patterns,
18181a29d293SNir Dotan 					   MLXSW_SP_ACL_TCAM_PATTERNS_COUNT,
1819593bb843SJiri Pirko 					   tmplt_elusage, false,
1820593bb843SJiri Pirko 					   p_min_prio, p_max_prio);
18211a29d293SNir Dotan 	if (err)
18221a29d293SNir Dotan 		return err;
18231a29d293SNir Dotan 
18241a29d293SNir Dotan 	/* For most of the TCAM clients it would make sense to take a tcam chunk
18251a29d293SNir Dotan 	 * only when the first rule is written. This is not the case for
18261a29d293SNir Dotan 	 * multicast router as it is required to bind the multicast router to a
18271a29d293SNir Dotan 	 * specific ACL Group ID which must exist in HW before multicast router
18281a29d293SNir Dotan 	 * is initialized.
18291a29d293SNir Dotan 	 */
1830b2d6b4d2SJiri Pirko 	ruleset->vchunk = mlxsw_sp_acl_tcam_vchunk_get(mlxsw_sp,
18312802aadfSJiri Pirko 						       &ruleset->vgroup, 1,
1832b2d6b4d2SJiri Pirko 						       tmplt_elusage);
1833b2d6b4d2SJiri Pirko 	if (IS_ERR(ruleset->vchunk)) {
1834b2d6b4d2SJiri Pirko 		err = PTR_ERR(ruleset->vchunk);
18351a29d293SNir Dotan 		goto err_chunk_get;
18361a29d293SNir Dotan 	}
18371a29d293SNir Dotan 
18381a29d293SNir Dotan 	return 0;
18391a29d293SNir Dotan 
18401a29d293SNir Dotan err_chunk_get:
18412802aadfSJiri Pirko 	mlxsw_sp_acl_tcam_vgroup_del(&ruleset->vgroup);
18421a29d293SNir Dotan 	return err;
18431a29d293SNir Dotan }
18441a29d293SNir Dotan 
18451a29d293SNir Dotan static void
mlxsw_sp_acl_tcam_mr_ruleset_del(struct mlxsw_sp * mlxsw_sp,void * ruleset_priv)18461a29d293SNir Dotan mlxsw_sp_acl_tcam_mr_ruleset_del(struct mlxsw_sp *mlxsw_sp, void *ruleset_priv)
18471a29d293SNir Dotan {
18481a29d293SNir Dotan 	struct mlxsw_sp_acl_tcam_mr_ruleset *ruleset = ruleset_priv;
18491a29d293SNir Dotan 
1850b2d6b4d2SJiri Pirko 	mlxsw_sp_acl_tcam_vchunk_put(mlxsw_sp, ruleset->vchunk);
18512802aadfSJiri Pirko 	mlxsw_sp_acl_tcam_vgroup_del(&ruleset->vgroup);
18521a29d293SNir Dotan }
18531a29d293SNir Dotan 
18541a29d293SNir Dotan static int
mlxsw_sp_acl_tcam_mr_ruleset_bind(struct mlxsw_sp * mlxsw_sp,void * ruleset_priv,struct mlxsw_sp_port * mlxsw_sp_port,bool ingress)18551a29d293SNir Dotan mlxsw_sp_acl_tcam_mr_ruleset_bind(struct mlxsw_sp *mlxsw_sp, void *ruleset_priv,
18561a29d293SNir Dotan 				  struct mlxsw_sp_port *mlxsw_sp_port,
18571a29d293SNir Dotan 				  bool ingress)
18581a29d293SNir Dotan {
18591a29d293SNir Dotan 	/* Binding is done when initializing multicast router */
18601a29d293SNir Dotan 	return 0;
18611a29d293SNir Dotan }
18621a29d293SNir Dotan 
18631a29d293SNir Dotan static void
mlxsw_sp_acl_tcam_mr_ruleset_unbind(struct mlxsw_sp * mlxsw_sp,void * ruleset_priv,struct mlxsw_sp_port * mlxsw_sp_port,bool ingress)18641a29d293SNir Dotan mlxsw_sp_acl_tcam_mr_ruleset_unbind(struct mlxsw_sp *mlxsw_sp,
18651a29d293SNir Dotan 				    void *ruleset_priv,
18661a29d293SNir Dotan 				    struct mlxsw_sp_port *mlxsw_sp_port,
18671a29d293SNir Dotan 				    bool ingress)
18681a29d293SNir Dotan {
18691a29d293SNir Dotan }
18701a29d293SNir Dotan 
18711a29d293SNir Dotan static u16
mlxsw_sp_acl_tcam_mr_ruleset_group_id(void * ruleset_priv)18721a29d293SNir Dotan mlxsw_sp_acl_tcam_mr_ruleset_group_id(void *ruleset_priv)
18731a29d293SNir Dotan {
18741a29d293SNir Dotan 	struct mlxsw_sp_acl_tcam_mr_ruleset *ruleset = ruleset_priv;
18751a29d293SNir Dotan 
18762802aadfSJiri Pirko 	return mlxsw_sp_acl_tcam_group_id(&ruleset->vgroup.group);
18771a29d293SNir Dotan }
18781a29d293SNir Dotan 
18791a29d293SNir Dotan static int
mlxsw_sp_acl_tcam_mr_rule_add(struct mlxsw_sp * mlxsw_sp,void * ruleset_priv,void * rule_priv,struct mlxsw_sp_acl_rule_info * rulei)18801a29d293SNir Dotan mlxsw_sp_acl_tcam_mr_rule_add(struct mlxsw_sp *mlxsw_sp, void *ruleset_priv,
18811a29d293SNir Dotan 			      void *rule_priv,
18821a29d293SNir Dotan 			      struct mlxsw_sp_acl_rule_info *rulei)
18831a29d293SNir Dotan {
18841a29d293SNir Dotan 	struct mlxsw_sp_acl_tcam_mr_ruleset *ruleset = ruleset_priv;
18851a29d293SNir Dotan 	struct mlxsw_sp_acl_tcam_mr_rule *rule = rule_priv;
18861a29d293SNir Dotan 
18872802aadfSJiri Pirko 	return mlxsw_sp_acl_tcam_ventry_add(mlxsw_sp, &ruleset->vgroup,
1888c4c2dc54SJiri Pirko 					   &rule->ventry, rulei);
18891a29d293SNir Dotan }
18901a29d293SNir Dotan 
18911a29d293SNir Dotan static void
mlxsw_sp_acl_tcam_mr_rule_del(struct mlxsw_sp * mlxsw_sp,void * rule_priv)18921a29d293SNir Dotan mlxsw_sp_acl_tcam_mr_rule_del(struct mlxsw_sp *mlxsw_sp, void *rule_priv)
18931a29d293SNir Dotan {
18941a29d293SNir Dotan 	struct mlxsw_sp_acl_tcam_mr_rule *rule = rule_priv;
18951a29d293SNir Dotan 
1896c4c2dc54SJiri Pirko 	mlxsw_sp_acl_tcam_ventry_del(mlxsw_sp, &rule->ventry);
18971a29d293SNir Dotan }
18981a29d293SNir Dotan 
18991a29d293SNir Dotan static int
mlxsw_sp_acl_tcam_mr_rule_action_replace(struct mlxsw_sp * mlxsw_sp,void * rule_priv,struct mlxsw_sp_acl_rule_info * rulei)19002507a64cSNir Dotan mlxsw_sp_acl_tcam_mr_rule_action_replace(struct mlxsw_sp *mlxsw_sp,
190142d704e0SJiri Pirko 					 void *rule_priv,
19022507a64cSNir Dotan 					 struct mlxsw_sp_acl_rule_info *rulei)
19032507a64cSNir Dotan {
19042507a64cSNir Dotan 	struct mlxsw_sp_acl_tcam_mr_rule *rule = rule_priv;
19052507a64cSNir Dotan 
1906c4c2dc54SJiri Pirko 	return mlxsw_sp_acl_tcam_ventry_action_replace(mlxsw_sp, &rule->ventry,
190742d704e0SJiri Pirko 						       rulei);
19082507a64cSNir Dotan }
19092507a64cSNir Dotan 
19102507a64cSNir Dotan static int
mlxsw_sp_acl_tcam_mr_rule_activity_get(struct mlxsw_sp * mlxsw_sp,void * rule_priv,bool * activity)19111a29d293SNir Dotan mlxsw_sp_acl_tcam_mr_rule_activity_get(struct mlxsw_sp *mlxsw_sp,
19121a29d293SNir Dotan 				       void *rule_priv, bool *activity)
19131a29d293SNir Dotan {
1914d1314096SIdo Schimmel 	*activity = false;
19151a29d293SNir Dotan 
1916d1314096SIdo Schimmel 	return 0;
19171a29d293SNir Dotan }
19181a29d293SNir Dotan 
19191a29d293SNir Dotan static const struct mlxsw_sp_acl_profile_ops mlxsw_sp_acl_tcam_mr_ops = {
19201a29d293SNir Dotan 	.ruleset_priv_size	= sizeof(struct mlxsw_sp_acl_tcam_mr_ruleset),
19211a29d293SNir Dotan 	.ruleset_add		= mlxsw_sp_acl_tcam_mr_ruleset_add,
19221a29d293SNir Dotan 	.ruleset_del		= mlxsw_sp_acl_tcam_mr_ruleset_del,
19231a29d293SNir Dotan 	.ruleset_bind		= mlxsw_sp_acl_tcam_mr_ruleset_bind,
19241a29d293SNir Dotan 	.ruleset_unbind		= mlxsw_sp_acl_tcam_mr_ruleset_unbind,
19251a29d293SNir Dotan 	.ruleset_group_id	= mlxsw_sp_acl_tcam_mr_ruleset_group_id,
1926c4c2dc54SJiri Pirko 	.rule_priv_size		= sizeof(struct mlxsw_sp_acl_tcam_mr_rule),
19271a29d293SNir Dotan 	.rule_add		= mlxsw_sp_acl_tcam_mr_rule_add,
19281a29d293SNir Dotan 	.rule_del		= mlxsw_sp_acl_tcam_mr_rule_del,
19292507a64cSNir Dotan 	.rule_action_replace	= mlxsw_sp_acl_tcam_mr_rule_action_replace,
19301a29d293SNir Dotan 	.rule_activity_get	= mlxsw_sp_acl_tcam_mr_rule_activity_get,
19311a29d293SNir Dotan };
19321a29d293SNir Dotan 
193322a67766SJiri Pirko static const struct mlxsw_sp_acl_profile_ops *
193422a67766SJiri Pirko mlxsw_sp_acl_tcam_profile_ops_arr[] = {
193522a67766SJiri Pirko 	[MLXSW_SP_ACL_PROFILE_FLOWER] = &mlxsw_sp_acl_tcam_flower_ops,
19361a29d293SNir Dotan 	[MLXSW_SP_ACL_PROFILE_MR] = &mlxsw_sp_acl_tcam_mr_ops,
193722a67766SJiri Pirko };
193822a67766SJiri Pirko 
1939bab5c1cfSJiri Pirko const struct mlxsw_sp_acl_profile_ops *
mlxsw_sp_acl_tcam_profile_ops(struct mlxsw_sp * mlxsw_sp,enum mlxsw_sp_acl_profile profile)194022a67766SJiri Pirko mlxsw_sp_acl_tcam_profile_ops(struct mlxsw_sp *mlxsw_sp,
194122a67766SJiri Pirko 			      enum mlxsw_sp_acl_profile profile)
194222a67766SJiri Pirko {
194322a67766SJiri Pirko 	const struct mlxsw_sp_acl_profile_ops *ops;
194422a67766SJiri Pirko 
194522a67766SJiri Pirko 	if (WARN_ON(profile >= ARRAY_SIZE(mlxsw_sp_acl_tcam_profile_ops_arr)))
194622a67766SJiri Pirko 		return NULL;
194722a67766SJiri Pirko 	ops = mlxsw_sp_acl_tcam_profile_ops_arr[profile];
194822a67766SJiri Pirko 	if (WARN_ON(!ops))
194922a67766SJiri Pirko 		return NULL;
195022a67766SJiri Pirko 	return ops;
195122a67766SJiri Pirko }
1952