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/errno.h>
6 #include <linux/parman.h>
11 #include "spectrum_acl_tcam.h"
14 mlxsw_sp_acl_ctcam_region_resize(struct mlxsw_sp *mlxsw_sp,
15 struct mlxsw_sp_acl_tcam_region *region,
18 char ptar_pl[MLXSW_REG_PTAR_LEN];
20 mlxsw_reg_ptar_pack(ptar_pl, MLXSW_REG_PTAR_OP_RESIZE,
21 region->key_type, new_size, region->id,
22 region->tcam_region_info);
23 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptar), ptar_pl);
27 mlxsw_sp_acl_ctcam_region_move(struct mlxsw_sp *mlxsw_sp,
28 struct mlxsw_sp_acl_tcam_region *region,
29 u16 src_offset, u16 dst_offset, u16 size)
31 char prcr_pl[MLXSW_REG_PRCR_LEN];
33 mlxsw_reg_prcr_pack(prcr_pl, MLXSW_REG_PRCR_OP_MOVE,
34 region->tcam_region_info, src_offset,
35 region->tcam_region_info, dst_offset, size);
36 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(prcr), prcr_pl);
40 mlxsw_sp_acl_ctcam_region_entry_insert(struct mlxsw_sp *mlxsw_sp,
41 struct mlxsw_sp_acl_ctcam_region *cregion,
42 struct mlxsw_sp_acl_ctcam_entry *centry,
43 struct mlxsw_sp_acl_rule_info *rulei,
46 struct mlxsw_sp_acl_tcam_region *region = cregion->region;
47 struct mlxsw_afk *afk = mlxsw_sp_acl_afk(mlxsw_sp->acl);
48 char ptce2_pl[MLXSW_REG_PTCE2_LEN];
49 unsigned int blocks_count;
56 err = mlxsw_sp_acl_tcam_priority_get(mlxsw_sp, rulei, &priority,
61 mlxsw_reg_ptce2_pack(ptce2_pl, true, MLXSW_REG_PTCE2_OP_WRITE_WRITE,
62 region->tcam_region_info,
63 centry->parman_item.index, priority);
64 key = mlxsw_reg_ptce2_flex_key_blocks_data(ptce2_pl);
65 mask = mlxsw_reg_ptce2_mask_data(ptce2_pl);
66 blocks_count = mlxsw_afk_key_info_blocks_count_get(region->key_info);
67 mlxsw_afk_encode(afk, region->key_info, &rulei->values, key, mask, 0,
70 err = cregion->ops->entry_insert(cregion, centry, mask);
74 /* Only the first action set belongs here, the rest is in KVD */
75 act_set = mlxsw_afa_block_first_set(rulei->act_block);
76 mlxsw_reg_ptce2_flex_action_set_memcpy_to(ptce2_pl, act_set);
78 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
85 cregion->ops->entry_remove(cregion, centry);
90 mlxsw_sp_acl_ctcam_region_entry_remove(struct mlxsw_sp *mlxsw_sp,
91 struct mlxsw_sp_acl_ctcam_region *cregion,
92 struct mlxsw_sp_acl_ctcam_entry *centry)
94 char ptce2_pl[MLXSW_REG_PTCE2_LEN];
96 mlxsw_reg_ptce2_pack(ptce2_pl, false, MLXSW_REG_PTCE2_OP_WRITE_WRITE,
97 cregion->region->tcam_region_info,
98 centry->parman_item.index, 0);
99 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
100 cregion->ops->entry_remove(cregion, centry);
103 static int mlxsw_sp_acl_ctcam_region_parman_resize(void *priv,
104 unsigned long new_count)
106 struct mlxsw_sp_acl_ctcam_region *cregion = priv;
107 struct mlxsw_sp_acl_tcam_region *region = cregion->region;
108 struct mlxsw_sp *mlxsw_sp = region->mlxsw_sp;
111 max_tcam_rules = MLXSW_CORE_RES_GET(mlxsw_sp->core, ACL_MAX_TCAM_RULES);
112 if (new_count > max_tcam_rules)
114 return mlxsw_sp_acl_ctcam_region_resize(mlxsw_sp, region, new_count);
117 static void mlxsw_sp_acl_ctcam_region_parman_move(void *priv,
118 unsigned long from_index,
119 unsigned long to_index,
122 struct mlxsw_sp_acl_ctcam_region *cregion = priv;
123 struct mlxsw_sp_acl_tcam_region *region = cregion->region;
124 struct mlxsw_sp *mlxsw_sp = region->mlxsw_sp;
126 mlxsw_sp_acl_ctcam_region_move(mlxsw_sp, region,
127 from_index, to_index, count);
130 static const struct parman_ops mlxsw_sp_acl_ctcam_region_parman_ops = {
131 .base_count = MLXSW_SP_ACL_TCAM_REGION_BASE_COUNT,
132 .resize_step = MLXSW_SP_ACL_TCAM_REGION_RESIZE_STEP,
133 .resize = mlxsw_sp_acl_ctcam_region_parman_resize,
134 .move = mlxsw_sp_acl_ctcam_region_parman_move,
135 .algo = PARMAN_ALGO_TYPE_LSORT,
139 mlxsw_sp_acl_ctcam_region_init(struct mlxsw_sp *mlxsw_sp,
140 struct mlxsw_sp_acl_ctcam_region *cregion,
141 struct mlxsw_sp_acl_tcam_region *region,
142 const struct mlxsw_sp_acl_ctcam_region_ops *ops)
144 cregion->region = region;
146 cregion->parman = parman_create(&mlxsw_sp_acl_ctcam_region_parman_ops,
148 if (!cregion->parman)
153 void mlxsw_sp_acl_ctcam_region_fini(struct mlxsw_sp_acl_ctcam_region *cregion)
155 parman_destroy(cregion->parman);
158 void mlxsw_sp_acl_ctcam_chunk_init(struct mlxsw_sp_acl_ctcam_region *cregion,
159 struct mlxsw_sp_acl_ctcam_chunk *cchunk,
160 unsigned int priority)
162 parman_prio_init(cregion->parman, &cchunk->parman_prio, priority);
165 void mlxsw_sp_acl_ctcam_chunk_fini(struct mlxsw_sp_acl_ctcam_chunk *cchunk)
167 parman_prio_fini(&cchunk->parman_prio);
170 int mlxsw_sp_acl_ctcam_entry_add(struct mlxsw_sp *mlxsw_sp,
171 struct mlxsw_sp_acl_ctcam_region *cregion,
172 struct mlxsw_sp_acl_ctcam_chunk *cchunk,
173 struct mlxsw_sp_acl_ctcam_entry *centry,
174 struct mlxsw_sp_acl_rule_info *rulei,
175 bool fillup_priority)
179 err = parman_item_add(cregion->parman, &cchunk->parman_prio,
180 ¢ry->parman_item);
184 err = mlxsw_sp_acl_ctcam_region_entry_insert(mlxsw_sp, cregion, centry,
185 rulei, fillup_priority);
187 goto err_rule_insert;
191 parman_item_remove(cregion->parman, &cchunk->parman_prio,
192 ¢ry->parman_item);
196 void mlxsw_sp_acl_ctcam_entry_del(struct mlxsw_sp *mlxsw_sp,
197 struct mlxsw_sp_acl_ctcam_region *cregion,
198 struct mlxsw_sp_acl_ctcam_chunk *cchunk,
199 struct mlxsw_sp_acl_ctcam_entry *centry)
201 mlxsw_sp_acl_ctcam_region_entry_remove(mlxsw_sp, cregion, centry);
202 parman_item_remove(cregion->parman, &cchunk->parman_prio,
203 ¢ry->parman_item);