1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
4 #include <linux/kernel.h>
5 #include <linux/slab.h>
10 #include "spectrum_acl_tcam.h"
12 struct mlxsw_sp1_acl_tcam_region {
13 struct mlxsw_sp_acl_ctcam_region cregion;
14 struct mlxsw_sp_acl_tcam_region *region;
16 struct mlxsw_sp_acl_ctcam_chunk cchunk;
17 struct mlxsw_sp_acl_ctcam_entry centry;
18 struct mlxsw_sp_acl_rule_info *rulei;
22 struct mlxsw_sp1_acl_tcam_chunk {
23 struct mlxsw_sp_acl_ctcam_chunk cchunk;
26 struct mlxsw_sp1_acl_tcam_entry {
27 struct mlxsw_sp_acl_ctcam_entry centry;
31 mlxsw_sp1_acl_ctcam_region_entry_insert(struct mlxsw_sp_acl_ctcam_region *cregion,
32 struct mlxsw_sp_acl_ctcam_entry *centry,
39 mlxsw_sp1_acl_ctcam_region_entry_remove(struct mlxsw_sp_acl_ctcam_region *cregion,
40 struct mlxsw_sp_acl_ctcam_entry *centry)
44 static const struct mlxsw_sp_acl_ctcam_region_ops
45 mlxsw_sp1_acl_ctcam_region_ops = {
46 .entry_insert = mlxsw_sp1_acl_ctcam_region_entry_insert,
47 .entry_remove = mlxsw_sp1_acl_ctcam_region_entry_remove,
50 static int mlxsw_sp1_acl_tcam_init(struct mlxsw_sp *mlxsw_sp, void *priv,
51 struct mlxsw_sp_acl_tcam *tcam)
56 static void mlxsw_sp1_acl_tcam_fini(struct mlxsw_sp *mlxsw_sp, void *priv)
61 mlxsw_sp1_acl_ctcam_region_catchall_add(struct mlxsw_sp *mlxsw_sp,
62 struct mlxsw_sp1_acl_tcam_region *region)
64 struct mlxsw_sp_acl_rule_info *rulei;
67 mlxsw_sp_acl_ctcam_chunk_init(®ion->cregion,
68 ®ion->catchall.cchunk,
69 MLXSW_SP_ACL_TCAM_CATCHALL_PRIO);
70 rulei = mlxsw_sp_acl_rulei_create(mlxsw_sp->acl, NULL);
73 goto err_rulei_create;
75 err = mlxsw_sp_acl_rulei_act_continue(rulei);
77 goto err_rulei_act_continue;
78 err = mlxsw_sp_acl_rulei_commit(rulei);
80 goto err_rulei_commit;
81 err = mlxsw_sp_acl_ctcam_entry_add(mlxsw_sp, ®ion->cregion,
82 ®ion->catchall.cchunk,
83 ®ion->catchall.centry,
87 region->catchall.rulei = rulei;
92 err_rulei_act_continue:
93 mlxsw_sp_acl_rulei_destroy(rulei);
95 mlxsw_sp_acl_ctcam_chunk_fini(®ion->catchall.cchunk);
100 mlxsw_sp1_acl_ctcam_region_catchall_del(struct mlxsw_sp *mlxsw_sp,
101 struct mlxsw_sp1_acl_tcam_region *region)
103 struct mlxsw_sp_acl_rule_info *rulei = region->catchall.rulei;
105 mlxsw_sp_acl_ctcam_entry_del(mlxsw_sp, ®ion->cregion,
106 ®ion->catchall.cchunk,
107 ®ion->catchall.centry);
108 mlxsw_sp_acl_rulei_destroy(rulei);
109 mlxsw_sp_acl_ctcam_chunk_fini(®ion->catchall.cchunk);
113 mlxsw_sp1_acl_tcam_region_init(struct mlxsw_sp *mlxsw_sp, void *region_priv,
115 struct mlxsw_sp_acl_tcam_region *_region,
118 struct mlxsw_sp1_acl_tcam_region *region = region_priv;
121 err = mlxsw_sp_acl_ctcam_region_init(mlxsw_sp, ®ion->cregion,
123 &mlxsw_sp1_acl_ctcam_region_ops);
126 err = mlxsw_sp1_acl_ctcam_region_catchall_add(mlxsw_sp, region);
128 goto err_catchall_add;
129 region->region = _region;
133 mlxsw_sp_acl_ctcam_region_fini(®ion->cregion);
138 mlxsw_sp1_acl_tcam_region_fini(struct mlxsw_sp *mlxsw_sp, void *region_priv)
140 struct mlxsw_sp1_acl_tcam_region *region = region_priv;
142 mlxsw_sp1_acl_ctcam_region_catchall_del(mlxsw_sp, region);
143 mlxsw_sp_acl_ctcam_region_fini(®ion->cregion);
147 mlxsw_sp1_acl_tcam_region_associate(struct mlxsw_sp *mlxsw_sp,
148 struct mlxsw_sp_acl_tcam_region *region)
153 static void mlxsw_sp1_acl_tcam_chunk_init(void *region_priv, void *chunk_priv,
154 unsigned int priority)
156 struct mlxsw_sp1_acl_tcam_region *region = region_priv;
157 struct mlxsw_sp1_acl_tcam_chunk *chunk = chunk_priv;
159 mlxsw_sp_acl_ctcam_chunk_init(®ion->cregion, &chunk->cchunk,
163 static void mlxsw_sp1_acl_tcam_chunk_fini(void *chunk_priv)
165 struct mlxsw_sp1_acl_tcam_chunk *chunk = chunk_priv;
167 mlxsw_sp_acl_ctcam_chunk_fini(&chunk->cchunk);
170 static int mlxsw_sp1_acl_tcam_entry_add(struct mlxsw_sp *mlxsw_sp,
171 void *region_priv, void *chunk_priv,
173 struct mlxsw_sp_acl_rule_info *rulei)
175 struct mlxsw_sp1_acl_tcam_region *region = region_priv;
176 struct mlxsw_sp1_acl_tcam_chunk *chunk = chunk_priv;
177 struct mlxsw_sp1_acl_tcam_entry *entry = entry_priv;
179 return mlxsw_sp_acl_ctcam_entry_add(mlxsw_sp, ®ion->cregion,
180 &chunk->cchunk, &entry->centry,
184 static void mlxsw_sp1_acl_tcam_entry_del(struct mlxsw_sp *mlxsw_sp,
185 void *region_priv, void *chunk_priv,
188 struct mlxsw_sp1_acl_tcam_region *region = region_priv;
189 struct mlxsw_sp1_acl_tcam_chunk *chunk = chunk_priv;
190 struct mlxsw_sp1_acl_tcam_entry *entry = entry_priv;
192 mlxsw_sp_acl_ctcam_entry_del(mlxsw_sp, ®ion->cregion,
193 &chunk->cchunk, &entry->centry);
197 mlxsw_sp1_acl_tcam_entry_action_replace(struct mlxsw_sp *mlxsw_sp,
198 void *region_priv, void *entry_priv,
199 struct mlxsw_sp_acl_rule_info *rulei)
205 mlxsw_sp1_acl_tcam_region_entry_activity_get(struct mlxsw_sp *mlxsw_sp,
206 struct mlxsw_sp_acl_tcam_region *_region,
210 char ptce2_pl[MLXSW_REG_PTCE2_LEN];
213 mlxsw_reg_ptce2_pack(ptce2_pl, true, MLXSW_REG_PTCE2_OP_QUERY_CLEAR_ON_READ,
214 _region->tcam_region_info, offset, 0);
215 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
218 *activity = mlxsw_reg_ptce2_a_get(ptce2_pl);
223 mlxsw_sp1_acl_tcam_entry_activity_get(struct mlxsw_sp *mlxsw_sp,
224 void *region_priv, void *entry_priv,
227 struct mlxsw_sp1_acl_tcam_region *region = region_priv;
228 struct mlxsw_sp1_acl_tcam_entry *entry = entry_priv;
231 offset = mlxsw_sp_acl_ctcam_entry_offset(&entry->centry);
232 return mlxsw_sp1_acl_tcam_region_entry_activity_get(mlxsw_sp,
237 const struct mlxsw_sp_acl_tcam_ops mlxsw_sp1_acl_tcam_ops = {
238 .key_type = MLXSW_REG_PTAR_KEY_TYPE_FLEX,
240 .init = mlxsw_sp1_acl_tcam_init,
241 .fini = mlxsw_sp1_acl_tcam_fini,
242 .region_priv_size = sizeof(struct mlxsw_sp1_acl_tcam_region),
243 .region_init = mlxsw_sp1_acl_tcam_region_init,
244 .region_fini = mlxsw_sp1_acl_tcam_region_fini,
245 .region_associate = mlxsw_sp1_acl_tcam_region_associate,
246 .chunk_priv_size = sizeof(struct mlxsw_sp1_acl_tcam_chunk),
247 .chunk_init = mlxsw_sp1_acl_tcam_chunk_init,
248 .chunk_fini = mlxsw_sp1_acl_tcam_chunk_fini,
249 .entry_priv_size = sizeof(struct mlxsw_sp1_acl_tcam_entry),
250 .entry_add = mlxsw_sp1_acl_tcam_entry_add,
251 .entry_del = mlxsw_sp1_acl_tcam_entry_del,
252 .entry_action_replace = mlxsw_sp1_acl_tcam_entry_action_replace,
253 .entry_activity_get = mlxsw_sp1_acl_tcam_entry_activity_get,