1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2018 Mellanox Technologies. All rights reserved */
3 
4 #include <linux/kernel.h>
5 
6 #include "spectrum.h"
7 #include "spectrum_acl_tcam.h"
8 #include "core_acl_flex_actions.h"
9 
10 struct mlxsw_sp2_acl_tcam {
11 	struct mlxsw_sp_acl_atcam atcam;
12 	u32 kvdl_index;
13 	unsigned int kvdl_count;
14 };
15 
16 struct mlxsw_sp2_acl_tcam_region {
17 	struct mlxsw_sp_acl_atcam_region aregion;
18 	struct mlxsw_sp_acl_tcam_region *region;
19 };
20 
21 struct mlxsw_sp2_acl_tcam_chunk {
22 	struct mlxsw_sp_acl_atcam_chunk achunk;
23 };
24 
25 struct mlxsw_sp2_acl_tcam_entry {
26 	struct mlxsw_sp_acl_atcam_entry aentry;
27 	struct mlxsw_afa_block *act_block;
28 };
29 
30 static int
mlxsw_sp2_acl_ctcam_region_entry_insert(struct mlxsw_sp_acl_ctcam_region * cregion,struct mlxsw_sp_acl_ctcam_entry * centry,const char * mask)31 mlxsw_sp2_acl_ctcam_region_entry_insert(struct mlxsw_sp_acl_ctcam_region *cregion,
32 					struct mlxsw_sp_acl_ctcam_entry *centry,
33 					const char *mask)
34 {
35 	struct mlxsw_sp_acl_atcam_region *aregion;
36 	struct mlxsw_sp_acl_atcam_entry *aentry;
37 	struct mlxsw_sp_acl_erp *erp;
38 
39 	aregion = mlxsw_sp_acl_tcam_cregion_aregion(cregion);
40 	aentry = mlxsw_sp_acl_tcam_centry_aentry(centry);
41 
42 	erp = mlxsw_sp_acl_erp_get(aregion, mask, true);
43 	if (IS_ERR(erp))
44 		return PTR_ERR(erp);
45 	aentry->erp = erp;
46 
47 	return 0;
48 }
49 
50 static void
mlxsw_sp2_acl_ctcam_region_entry_remove(struct mlxsw_sp_acl_ctcam_region * cregion,struct mlxsw_sp_acl_ctcam_entry * centry)51 mlxsw_sp2_acl_ctcam_region_entry_remove(struct mlxsw_sp_acl_ctcam_region *cregion,
52 					struct mlxsw_sp_acl_ctcam_entry *centry)
53 {
54 	struct mlxsw_sp_acl_atcam_region *aregion;
55 	struct mlxsw_sp_acl_atcam_entry *aentry;
56 
57 	aregion = mlxsw_sp_acl_tcam_cregion_aregion(cregion);
58 	aentry = mlxsw_sp_acl_tcam_centry_aentry(centry);
59 
60 	mlxsw_sp_acl_erp_put(aregion, aentry->erp);
61 }
62 
63 static const struct mlxsw_sp_acl_ctcam_region_ops
64 mlxsw_sp2_acl_ctcam_region_ops = {
65 	.entry_insert = mlxsw_sp2_acl_ctcam_region_entry_insert,
66 	.entry_remove = mlxsw_sp2_acl_ctcam_region_entry_remove,
67 };
68 
mlxsw_sp2_acl_tcam_init(struct mlxsw_sp * mlxsw_sp,void * priv,struct mlxsw_sp_acl_tcam * _tcam)69 static int mlxsw_sp2_acl_tcam_init(struct mlxsw_sp *mlxsw_sp, void *priv,
70 				   struct mlxsw_sp_acl_tcam *_tcam)
71 {
72 	struct mlxsw_sp2_acl_tcam *tcam = priv;
73 	struct mlxsw_afa_block *afa_block;
74 	char pefa_pl[MLXSW_REG_PEFA_LEN];
75 	char pgcr_pl[MLXSW_REG_PGCR_LEN];
76 	char *enc_actions;
77 	int i;
78 	int err;
79 
80 	tcam->kvdl_count = _tcam->max_regions;
81 	err = mlxsw_sp_kvdl_alloc(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ACTSET,
82 				  tcam->kvdl_count, &tcam->kvdl_index);
83 	if (err)
84 		return err;
85 
86 	/* Create flex action block, set default action (continue)
87 	 * but don't commit. We need just the current set encoding
88 	 * to be written using PEFA register to all indexes for all regions.
89 	 */
90 	afa_block = mlxsw_afa_block_create(mlxsw_sp->afa);
91 	if (!afa_block) {
92 		err = -ENOMEM;
93 		goto err_afa_block;
94 	}
95 	err = mlxsw_afa_block_continue(afa_block);
96 	if (WARN_ON(err))
97 		goto err_afa_block_continue;
98 	enc_actions = mlxsw_afa_block_cur_set(afa_block);
99 
100 	for (i = 0; i < tcam->kvdl_count; i++) {
101 		mlxsw_reg_pefa_pack(pefa_pl, tcam->kvdl_index + i,
102 				    true, enc_actions);
103 		err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pefa), pefa_pl);
104 		if (err)
105 			goto err_pefa_write;
106 	}
107 	mlxsw_reg_pgcr_pack(pgcr_pl, tcam->kvdl_index);
108 	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pgcr), pgcr_pl);
109 	if (err)
110 		goto err_pgcr_write;
111 
112 	err = mlxsw_sp_acl_atcam_init(mlxsw_sp, &tcam->atcam);
113 	if (err)
114 		goto err_atcam_init;
115 
116 	mlxsw_afa_block_destroy(afa_block);
117 	return 0;
118 
119 err_atcam_init:
120 err_pgcr_write:
121 err_pefa_write:
122 err_afa_block_continue:
123 	mlxsw_afa_block_destroy(afa_block);
124 err_afa_block:
125 	mlxsw_sp_kvdl_free(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ACTSET,
126 			   tcam->kvdl_count, tcam->kvdl_index);
127 	return err;
128 }
129 
mlxsw_sp2_acl_tcam_fini(struct mlxsw_sp * mlxsw_sp,void * priv)130 static void mlxsw_sp2_acl_tcam_fini(struct mlxsw_sp *mlxsw_sp, void *priv)
131 {
132 	struct mlxsw_sp2_acl_tcam *tcam = priv;
133 
134 	mlxsw_sp_acl_atcam_fini(mlxsw_sp, &tcam->atcam);
135 	mlxsw_sp_kvdl_free(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ACTSET,
136 			   tcam->kvdl_count, tcam->kvdl_index);
137 }
138 
139 static int
mlxsw_sp2_acl_tcam_region_init(struct mlxsw_sp * mlxsw_sp,void * region_priv,void * tcam_priv,struct mlxsw_sp_acl_tcam_region * _region)140 mlxsw_sp2_acl_tcam_region_init(struct mlxsw_sp *mlxsw_sp, void *region_priv,
141 			       void *tcam_priv,
142 			       struct mlxsw_sp_acl_tcam_region *_region)
143 {
144 	struct mlxsw_sp2_acl_tcam_region *region = region_priv;
145 	struct mlxsw_sp2_acl_tcam *tcam = tcam_priv;
146 
147 	region->region = _region;
148 
149 	return mlxsw_sp_acl_atcam_region_init(mlxsw_sp, &tcam->atcam,
150 					      &region->aregion, _region,
151 					      &mlxsw_sp2_acl_ctcam_region_ops);
152 }
153 
154 static void
mlxsw_sp2_acl_tcam_region_fini(struct mlxsw_sp * mlxsw_sp,void * region_priv)155 mlxsw_sp2_acl_tcam_region_fini(struct mlxsw_sp *mlxsw_sp, void *region_priv)
156 {
157 	struct mlxsw_sp2_acl_tcam_region *region = region_priv;
158 
159 	mlxsw_sp_acl_atcam_region_fini(&region->aregion);
160 }
161 
162 static int
mlxsw_sp2_acl_tcam_region_associate(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_tcam_region * region)163 mlxsw_sp2_acl_tcam_region_associate(struct mlxsw_sp *mlxsw_sp,
164 				    struct mlxsw_sp_acl_tcam_region *region)
165 {
166 	return mlxsw_sp_acl_atcam_region_associate(mlxsw_sp, region->id);
167 }
168 
mlxsw_sp2_acl_tcam_chunk_init(void * region_priv,void * chunk_priv,unsigned int priority)169 static void mlxsw_sp2_acl_tcam_chunk_init(void *region_priv, void *chunk_priv,
170 					  unsigned int priority)
171 {
172 	struct mlxsw_sp2_acl_tcam_region *region = region_priv;
173 	struct mlxsw_sp2_acl_tcam_chunk *chunk = chunk_priv;
174 
175 	mlxsw_sp_acl_atcam_chunk_init(&region->aregion, &chunk->achunk,
176 				      priority);
177 }
178 
mlxsw_sp2_acl_tcam_chunk_fini(void * chunk_priv)179 static void mlxsw_sp2_acl_tcam_chunk_fini(void *chunk_priv)
180 {
181 	struct mlxsw_sp2_acl_tcam_chunk *chunk = chunk_priv;
182 
183 	mlxsw_sp_acl_atcam_chunk_fini(&chunk->achunk);
184 }
185 
mlxsw_sp2_acl_tcam_entry_add(struct mlxsw_sp * mlxsw_sp,void * region_priv,void * chunk_priv,void * entry_priv,struct mlxsw_sp_acl_rule_info * rulei)186 static int mlxsw_sp2_acl_tcam_entry_add(struct mlxsw_sp *mlxsw_sp,
187 					void *region_priv, void *chunk_priv,
188 					void *entry_priv,
189 					struct mlxsw_sp_acl_rule_info *rulei)
190 {
191 	struct mlxsw_sp2_acl_tcam_region *region = region_priv;
192 	struct mlxsw_sp2_acl_tcam_chunk *chunk = chunk_priv;
193 	struct mlxsw_sp2_acl_tcam_entry *entry = entry_priv;
194 
195 	entry->act_block = rulei->act_block;
196 	return mlxsw_sp_acl_atcam_entry_add(mlxsw_sp, &region->aregion,
197 					    &chunk->achunk, &entry->aentry,
198 					    rulei);
199 }
200 
mlxsw_sp2_acl_tcam_entry_del(struct mlxsw_sp * mlxsw_sp,void * region_priv,void * chunk_priv,void * entry_priv)201 static void mlxsw_sp2_acl_tcam_entry_del(struct mlxsw_sp *mlxsw_sp,
202 					 void *region_priv, void *chunk_priv,
203 					 void *entry_priv)
204 {
205 	struct mlxsw_sp2_acl_tcam_region *region = region_priv;
206 	struct mlxsw_sp2_acl_tcam_chunk *chunk = chunk_priv;
207 	struct mlxsw_sp2_acl_tcam_entry *entry = entry_priv;
208 
209 	mlxsw_sp_acl_atcam_entry_del(mlxsw_sp, &region->aregion, &chunk->achunk,
210 				     &entry->aentry);
211 }
212 
213 static int
mlxsw_sp2_acl_tcam_entry_activity_get(struct mlxsw_sp * mlxsw_sp,void * region_priv,void * entry_priv,bool * activity)214 mlxsw_sp2_acl_tcam_entry_activity_get(struct mlxsw_sp *mlxsw_sp,
215 				      void *region_priv, void *entry_priv,
216 				      bool *activity)
217 {
218 	struct mlxsw_sp2_acl_tcam_entry *entry = entry_priv;
219 
220 	return mlxsw_afa_block_activity_get(entry->act_block, activity);
221 }
222 
223 const struct mlxsw_sp_acl_tcam_ops mlxsw_sp2_acl_tcam_ops = {
224 	.key_type		= MLXSW_REG_PTAR_KEY_TYPE_FLEX2,
225 	.priv_size		= sizeof(struct mlxsw_sp2_acl_tcam),
226 	.init			= mlxsw_sp2_acl_tcam_init,
227 	.fini			= mlxsw_sp2_acl_tcam_fini,
228 	.region_priv_size	= sizeof(struct mlxsw_sp2_acl_tcam_region),
229 	.region_init		= mlxsw_sp2_acl_tcam_region_init,
230 	.region_fini		= mlxsw_sp2_acl_tcam_region_fini,
231 	.region_associate	= mlxsw_sp2_acl_tcam_region_associate,
232 	.chunk_priv_size	= sizeof(struct mlxsw_sp2_acl_tcam_chunk),
233 	.chunk_init		= mlxsw_sp2_acl_tcam_chunk_init,
234 	.chunk_fini		= mlxsw_sp2_acl_tcam_chunk_fini,
235 	.entry_priv_size	= sizeof(struct mlxsw_sp2_acl_tcam_entry),
236 	.entry_add		= mlxsw_sp2_acl_tcam_entry_add,
237 	.entry_del		= mlxsw_sp2_acl_tcam_entry_del,
238 	.entry_activity_get	= mlxsw_sp2_acl_tcam_entry_activity_get,
239 };
240