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);
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)
117 struct mlxsw_sp1_acl_tcam_region *region = region_priv;
120 err = mlxsw_sp_acl_ctcam_region_init(mlxsw_sp, ®ion->cregion,
122 &mlxsw_sp1_acl_ctcam_region_ops);
125 err = mlxsw_sp1_acl_ctcam_region_catchall_add(mlxsw_sp, region);
127 goto err_catchall_add;
128 region->region = _region;
132 mlxsw_sp_acl_ctcam_region_fini(®ion->cregion);
137 mlxsw_sp1_acl_tcam_region_fini(struct mlxsw_sp *mlxsw_sp, void *region_priv)
139 struct mlxsw_sp1_acl_tcam_region *region = region_priv;
141 mlxsw_sp1_acl_ctcam_region_catchall_del(mlxsw_sp, region);
142 mlxsw_sp_acl_ctcam_region_fini(®ion->cregion);
146 mlxsw_sp1_acl_tcam_region_associate(struct mlxsw_sp *mlxsw_sp,
147 struct mlxsw_sp_acl_tcam_region *region)
152 static void mlxsw_sp1_acl_tcam_chunk_init(void *region_priv, void *chunk_priv,
153 unsigned int priority)
155 struct mlxsw_sp1_acl_tcam_region *region = region_priv;
156 struct mlxsw_sp1_acl_tcam_chunk *chunk = chunk_priv;
158 mlxsw_sp_acl_ctcam_chunk_init(®ion->cregion, &chunk->cchunk,
162 static void mlxsw_sp1_acl_tcam_chunk_fini(void *chunk_priv)
164 struct mlxsw_sp1_acl_tcam_chunk *chunk = chunk_priv;
166 mlxsw_sp_acl_ctcam_chunk_fini(&chunk->cchunk);
169 static int mlxsw_sp1_acl_tcam_entry_add(struct mlxsw_sp *mlxsw_sp,
170 void *region_priv, void *chunk_priv,
172 struct mlxsw_sp_acl_rule_info *rulei)
174 struct mlxsw_sp1_acl_tcam_region *region = region_priv;
175 struct mlxsw_sp1_acl_tcam_chunk *chunk = chunk_priv;
176 struct mlxsw_sp1_acl_tcam_entry *entry = entry_priv;
178 return mlxsw_sp_acl_ctcam_entry_add(mlxsw_sp, ®ion->cregion,
179 &chunk->cchunk, &entry->centry,
183 static void mlxsw_sp1_acl_tcam_entry_del(struct mlxsw_sp *mlxsw_sp,
184 void *region_priv, void *chunk_priv,
187 struct mlxsw_sp1_acl_tcam_region *region = region_priv;
188 struct mlxsw_sp1_acl_tcam_chunk *chunk = chunk_priv;
189 struct mlxsw_sp1_acl_tcam_entry *entry = entry_priv;
191 mlxsw_sp_acl_ctcam_entry_del(mlxsw_sp, ®ion->cregion,
192 &chunk->cchunk, &entry->centry);
196 mlxsw_sp1_acl_tcam_region_entry_activity_get(struct mlxsw_sp *mlxsw_sp,
197 struct mlxsw_sp_acl_tcam_region *_region,
201 char ptce2_pl[MLXSW_REG_PTCE2_LEN];
204 mlxsw_reg_ptce2_pack(ptce2_pl, true, MLXSW_REG_PTCE2_OP_QUERY_CLEAR_ON_READ,
205 _region->tcam_region_info, offset, 0);
206 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
209 *activity = mlxsw_reg_ptce2_a_get(ptce2_pl);
214 mlxsw_sp1_acl_tcam_entry_activity_get(struct mlxsw_sp *mlxsw_sp,
215 void *region_priv, void *entry_priv,
218 struct mlxsw_sp1_acl_tcam_region *region = region_priv;
219 struct mlxsw_sp1_acl_tcam_entry *entry = entry_priv;
222 offset = mlxsw_sp_acl_ctcam_entry_offset(&entry->centry);
223 return mlxsw_sp1_acl_tcam_region_entry_activity_get(mlxsw_sp,
228 const struct mlxsw_sp_acl_tcam_ops mlxsw_sp1_acl_tcam_ops = {
229 .key_type = MLXSW_REG_PTAR_KEY_TYPE_FLEX,
231 .init = mlxsw_sp1_acl_tcam_init,
232 .fini = mlxsw_sp1_acl_tcam_fini,
233 .region_priv_size = sizeof(struct mlxsw_sp1_acl_tcam_region),
234 .region_init = mlxsw_sp1_acl_tcam_region_init,
235 .region_fini = mlxsw_sp1_acl_tcam_region_fini,
236 .region_associate = mlxsw_sp1_acl_tcam_region_associate,
237 .chunk_priv_size = sizeof(struct mlxsw_sp1_acl_tcam_chunk),
238 .chunk_init = mlxsw_sp1_acl_tcam_chunk_init,
239 .chunk_fini = mlxsw_sp1_acl_tcam_chunk_fini,
240 .entry_priv_size = sizeof(struct mlxsw_sp1_acl_tcam_entry),
241 .entry_add = mlxsw_sp1_acl_tcam_entry_add,
242 .entry_del = mlxsw_sp1_acl_tcam_entry_del,
243 .entry_activity_get = mlxsw_sp1_acl_tcam_entry_activity_get,