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
];
55 err
= mlxsw_sp_acl_tcam_priority_get(mlxsw_sp
, rulei
, &priority
,
60 mlxsw_reg_ptce2_pack(ptce2_pl
, true, MLXSW_REG_PTCE2_OP_WRITE_WRITE
,
61 region
->tcam_region_info
,
62 centry
->parman_item
.index
, priority
);
63 key
= mlxsw_reg_ptce2_flex_key_blocks_data(ptce2_pl
);
64 mask
= mlxsw_reg_ptce2_mask_data(ptce2_pl
);
65 mlxsw_afk_encode(afk
, region
->key_info
, &rulei
->values
, key
, mask
);
67 err
= cregion
->ops
->entry_insert(cregion
, centry
, mask
);
71 /* Only the first action set belongs here, the rest is in KVD */
72 act_set
= mlxsw_afa_block_first_set(rulei
->act_block
);
73 mlxsw_reg_ptce2_flex_action_set_memcpy_to(ptce2_pl
, act_set
);
75 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ptce2
), ptce2_pl
);
79 mlxsw_sp_acl_ctcam_region_entry_remove(struct mlxsw_sp
*mlxsw_sp
,
80 struct mlxsw_sp_acl_ctcam_region
*cregion
,
81 struct mlxsw_sp_acl_ctcam_entry
*centry
)
83 char ptce2_pl
[MLXSW_REG_PTCE2_LEN
];
85 mlxsw_reg_ptce2_pack(ptce2_pl
, false, MLXSW_REG_PTCE2_OP_WRITE_WRITE
,
86 cregion
->region
->tcam_region_info
,
87 centry
->parman_item
.index
, 0);
88 mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ptce2
), ptce2_pl
);
89 cregion
->ops
->entry_remove(cregion
, centry
);
93 mlxsw_sp_acl_ctcam_region_entry_action_replace(struct mlxsw_sp
*mlxsw_sp
,
94 struct mlxsw_sp_acl_ctcam_region
*cregion
,
95 struct mlxsw_sp_acl_ctcam_entry
*centry
,
96 struct mlxsw_afa_block
*afa_block
,
97 unsigned int priority
)
99 char ptce2_pl
[MLXSW_REG_PTCE2_LEN
];
102 mlxsw_reg_ptce2_pack(ptce2_pl
, true, MLXSW_REG_PTCE2_OP_WRITE_UPDATE
,
103 cregion
->region
->tcam_region_info
,
104 centry
->parman_item
.index
, priority
);
106 act_set
= mlxsw_afa_block_first_set(afa_block
);
107 mlxsw_reg_ptce2_flex_action_set_memcpy_to(ptce2_pl
, act_set
);
109 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ptce2
), ptce2_pl
);
113 static int mlxsw_sp_acl_ctcam_region_parman_resize(void *priv
,
114 unsigned long new_count
)
116 struct mlxsw_sp_acl_ctcam_region
*cregion
= priv
;
117 struct mlxsw_sp_acl_tcam_region
*region
= cregion
->region
;
118 struct mlxsw_sp
*mlxsw_sp
= region
->mlxsw_sp
;
121 max_tcam_rules
= MLXSW_CORE_RES_GET(mlxsw_sp
->core
, ACL_MAX_TCAM_RULES
);
122 if (new_count
> max_tcam_rules
)
124 return mlxsw_sp_acl_ctcam_region_resize(mlxsw_sp
, region
, new_count
);
127 static void mlxsw_sp_acl_ctcam_region_parman_move(void *priv
,
128 unsigned long from_index
,
129 unsigned long to_index
,
132 struct mlxsw_sp_acl_ctcam_region
*cregion
= priv
;
133 struct mlxsw_sp_acl_tcam_region
*region
= cregion
->region
;
134 struct mlxsw_sp
*mlxsw_sp
= region
->mlxsw_sp
;
136 mlxsw_sp_acl_ctcam_region_move(mlxsw_sp
, region
,
137 from_index
, to_index
, count
);
140 static const struct parman_ops mlxsw_sp_acl_ctcam_region_parman_ops
= {
141 .base_count
= MLXSW_SP_ACL_TCAM_REGION_BASE_COUNT
,
142 .resize_step
= MLXSW_SP_ACL_TCAM_REGION_RESIZE_STEP
,
143 .resize
= mlxsw_sp_acl_ctcam_region_parman_resize
,
144 .move
= mlxsw_sp_acl_ctcam_region_parman_move
,
145 .algo
= PARMAN_ALGO_TYPE_LSORT
,
149 mlxsw_sp_acl_ctcam_region_init(struct mlxsw_sp
*mlxsw_sp
,
150 struct mlxsw_sp_acl_ctcam_region
*cregion
,
151 struct mlxsw_sp_acl_tcam_region
*region
,
152 const struct mlxsw_sp_acl_ctcam_region_ops
*ops
)
154 cregion
->region
= region
;
156 cregion
->parman
= parman_create(&mlxsw_sp_acl_ctcam_region_parman_ops
,
158 if (!cregion
->parman
)
163 void mlxsw_sp_acl_ctcam_region_fini(struct mlxsw_sp_acl_ctcam_region
*cregion
)
165 parman_destroy(cregion
->parman
);
168 void mlxsw_sp_acl_ctcam_chunk_init(struct mlxsw_sp_acl_ctcam_region
*cregion
,
169 struct mlxsw_sp_acl_ctcam_chunk
*cchunk
,
170 unsigned int priority
)
172 parman_prio_init(cregion
->parman
, &cchunk
->parman_prio
, priority
);
175 void mlxsw_sp_acl_ctcam_chunk_fini(struct mlxsw_sp_acl_ctcam_chunk
*cchunk
)
177 parman_prio_fini(&cchunk
->parman_prio
);
180 int mlxsw_sp_acl_ctcam_entry_add(struct mlxsw_sp
*mlxsw_sp
,
181 struct mlxsw_sp_acl_ctcam_region
*cregion
,
182 struct mlxsw_sp_acl_ctcam_chunk
*cchunk
,
183 struct mlxsw_sp_acl_ctcam_entry
*centry
,
184 struct mlxsw_sp_acl_rule_info
*rulei
,
185 bool fillup_priority
)
189 err
= parman_item_add(cregion
->parman
, &cchunk
->parman_prio
,
190 ¢ry
->parman_item
);
194 err
= mlxsw_sp_acl_ctcam_region_entry_insert(mlxsw_sp
, cregion
, centry
,
195 rulei
, fillup_priority
);
197 goto err_rule_insert
;
201 parman_item_remove(cregion
->parman
, &cchunk
->parman_prio
,
202 ¢ry
->parman_item
);
206 void mlxsw_sp_acl_ctcam_entry_del(struct mlxsw_sp
*mlxsw_sp
,
207 struct mlxsw_sp_acl_ctcam_region
*cregion
,
208 struct mlxsw_sp_acl_ctcam_chunk
*cchunk
,
209 struct mlxsw_sp_acl_ctcam_entry
*centry
)
211 mlxsw_sp_acl_ctcam_region_entry_remove(mlxsw_sp
, cregion
, centry
);
212 parman_item_remove(cregion
->parman
, &cchunk
->parman_prio
,
213 ¢ry
->parman_item
);
216 int mlxsw_sp_acl_ctcam_entry_action_replace(struct mlxsw_sp
*mlxsw_sp
,
217 struct mlxsw_sp_acl_ctcam_region
*cregion
,
218 struct mlxsw_sp_acl_ctcam_chunk
*cchunk
,
219 struct mlxsw_sp_acl_ctcam_entry
*centry
,
220 struct mlxsw_sp_acl_rule_info
*rulei
)
222 return mlxsw_sp_acl_ctcam_region_entry_action_replace(mlxsw_sp
, cregion
,