]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_tcam.c
Merge tag 'io_uring-5.7-2020-05-22' of git://git.kernel.dk/linux-block
[thirdparty/linux.git] / drivers / net / ethernet / mellanox / mlxsw / spectrum_acl_tcam.c
CommitLineData
9948a064
JP
1// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2/* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
22a67766
JP
3
4#include <linux/kernel.h>
5#include <linux/slab.h>
6#include <linux/errno.h>
7#include <linux/bitops.h>
8#include <linux/list.h>
9#include <linux/rhashtable.h>
10#include <linux/netdevice.h>
5ec2ee28 11#include <linux/mutex.h>
3985de72 12#include <trace/events/mlxsw.h>
22a67766
JP
13
14#include "reg.h"
15#include "core.h"
16#include "resources.h"
17#include "spectrum.h"
64eccd00 18#include "spectrum_acl_tcam.h"
22a67766
JP
19#include "core_acl_flex_keys.h"
20
bab5c1cf
JP
21size_t mlxsw_sp_acl_tcam_priv_size(struct mlxsw_sp *mlxsw_sp)
22{
23 const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
24
25 return ops->priv_size;
26}
22a67766 27
e5e7962e 28#define MLXSW_SP_ACL_TCAM_VREGION_REHASH_INTRVL_DFLT 5000 /* ms */
98bbf70c 29#define MLXSW_SP_ACL_TCAM_VREGION_REHASH_INTRVL_MIN 3000 /* ms */
c9c9af91 30#define MLXSW_SP_ACL_TCAM_VREGION_REHASH_CREDITS 100 /* number of entries */
e5e7962e 31
bab5c1cf
JP
32int mlxsw_sp_acl_tcam_init(struct mlxsw_sp *mlxsw_sp,
33 struct mlxsw_sp_acl_tcam *tcam)
22a67766 34{
bab5c1cf 35 const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
22a67766
JP
36 u64 max_tcam_regions;
37 u64 max_regions;
38 u64 max_groups;
39 size_t alloc_size;
40 int err;
41
7b0f62ee 42 mutex_init(&tcam->lock);
e5e7962e
JP
43 tcam->vregion_rehash_intrvl =
44 MLXSW_SP_ACL_TCAM_VREGION_REHASH_INTRVL_DFLT;
45 INIT_LIST_HEAD(&tcam->vregion_list);
46
22a67766
JP
47 max_tcam_regions = MLXSW_CORE_RES_GET(mlxsw_sp->core,
48 ACL_MAX_TCAM_REGIONS);
49 max_regions = MLXSW_CORE_RES_GET(mlxsw_sp->core, ACL_MAX_REGIONS);
50
51 /* Use 1:1 mapping between ACL region and TCAM region */
52 if (max_tcam_regions < max_regions)
53 max_regions = max_tcam_regions;
54
55 alloc_size = sizeof(tcam->used_regions[0]) * BITS_TO_LONGS(max_regions);
56 tcam->used_regions = kzalloc(alloc_size, GFP_KERNEL);
57 if (!tcam->used_regions)
58 return -ENOMEM;
59 tcam->max_regions = max_regions;
60
61 max_groups = MLXSW_CORE_RES_GET(mlxsw_sp->core, ACL_MAX_GROUPS);
62 alloc_size = sizeof(tcam->used_groups[0]) * BITS_TO_LONGS(max_groups);
63 tcam->used_groups = kzalloc(alloc_size, GFP_KERNEL);
64 if (!tcam->used_groups) {
65 err = -ENOMEM;
66 goto err_alloc_used_groups;
67 }
68 tcam->max_groups = max_groups;
69 tcam->max_group_size = MLXSW_CORE_RES_GET(mlxsw_sp->core,
70 ACL_MAX_GROUP_SIZE);
bab5c1cf
JP
71
72 err = ops->init(mlxsw_sp, tcam->priv, tcam);
73 if (err)
74 goto err_tcam_init;
75
22a67766
JP
76 return 0;
77
bab5c1cf
JP
78err_tcam_init:
79 kfree(tcam->used_groups);
22a67766
JP
80err_alloc_used_groups:
81 kfree(tcam->used_regions);
82 return err;
83}
84
bab5c1cf
JP
85void mlxsw_sp_acl_tcam_fini(struct mlxsw_sp *mlxsw_sp,
86 struct mlxsw_sp_acl_tcam *tcam)
22a67766 87{
bab5c1cf 88 const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
22a67766 89
7b0f62ee 90 mutex_destroy(&tcam->lock);
bab5c1cf 91 ops->fini(mlxsw_sp, tcam->priv);
22a67766
JP
92 kfree(tcam->used_groups);
93 kfree(tcam->used_regions);
94}
95
ea8b2e28
JP
96int mlxsw_sp_acl_tcam_priority_get(struct mlxsw_sp *mlxsw_sp,
97 struct mlxsw_sp_acl_rule_info *rulei,
98 u32 *priority, bool fillup_priority)
99{
100 u64 max_priority;
101
102 if (!fillup_priority) {
103 *priority = 0;
104 return 0;
105 }
106
107 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, KVD_SIZE))
108 return -EIO;
109
d7263ab3
ND
110 /* Priority range is 1..cap_kvd_size-1. */
111 max_priority = MLXSW_CORE_RES_GET(mlxsw_sp->core, KVD_SIZE) - 1;
112 if (rulei->priority >= max_priority)
ea8b2e28
JP
113 return -EINVAL;
114
115 /* Unlike in TC, in HW, higher number means higher priority. */
116 *priority = max_priority - rulei->priority;
117 return 0;
118}
119
22a67766
JP
120static int mlxsw_sp_acl_tcam_region_id_get(struct mlxsw_sp_acl_tcam *tcam,
121 u16 *p_id)
122{
123 u16 id;
124
125 id = find_first_zero_bit(tcam->used_regions, tcam->max_regions);
126 if (id < tcam->max_regions) {
127 __set_bit(id, tcam->used_regions);
128 *p_id = id;
129 return 0;
130 }
131 return -ENOBUFS;
132}
133
134static void mlxsw_sp_acl_tcam_region_id_put(struct mlxsw_sp_acl_tcam *tcam,
135 u16 id)
136{
137 __clear_bit(id, tcam->used_regions);
138}
139
140static int mlxsw_sp_acl_tcam_group_id_get(struct mlxsw_sp_acl_tcam *tcam,
141 u16 *p_id)
142{
143 u16 id;
144
145 id = find_first_zero_bit(tcam->used_groups, tcam->max_groups);
146 if (id < tcam->max_groups) {
147 __set_bit(id, tcam->used_groups);
148 *p_id = id;
149 return 0;
150 }
151 return -ENOBUFS;
152}
153
154static void mlxsw_sp_acl_tcam_group_id_put(struct mlxsw_sp_acl_tcam *tcam,
155 u16 id)
156{
157 __clear_bit(id, tcam->used_groups);
158}
159
160struct mlxsw_sp_acl_tcam_pattern {
161 const enum mlxsw_afk_element *elements;
162 unsigned int elements_count;
163};
164
165struct mlxsw_sp_acl_tcam_group {
166 struct mlxsw_sp_acl_tcam *tcam;
167 u16 id;
5ec2ee28 168 struct mutex lock; /* guards region list updates */
2802aadf 169 struct list_head region_list;
22a67766 170 unsigned int region_count;
2802aadf
JP
171};
172
173struct mlxsw_sp_acl_tcam_vgroup {
174 struct mlxsw_sp_acl_tcam_group group;
175 struct list_head vregion_list;
b2d6b4d2 176 struct rhashtable vchunk_ht;
22a67766
JP
177 const struct mlxsw_sp_acl_tcam_pattern *patterns;
178 unsigned int patterns_count;
e2f2a1fd
JP
179 bool tmplt_elusage_set;
180 struct mlxsw_afk_element_usage tmplt_elusage;
6b861682 181 bool vregion_rehash_enabled;
22a67766
JP
182};
183
559c2768
JP
184struct mlxsw_sp_acl_tcam_rehash_ctx {
185 void *hints_priv;
220f4fba 186 bool this_is_rollback;
6f9579d4
JP
187 struct mlxsw_sp_acl_tcam_vchunk *current_vchunk; /* vchunk being
188 * currently migrated.
189 */
190 struct mlxsw_sp_acl_tcam_ventry *start_ventry; /* ventry to start
191 * migration from in
192 * a vchunk being
193 * currently migrated.
194 */
195 struct mlxsw_sp_acl_tcam_ventry *stop_ventry; /* ventry to stop
196 * migration at
197 * a vchunk being
198 * currently migrated.
199 */
559c2768
JP
200};
201
0f54236d 202struct mlxsw_sp_acl_tcam_vregion {
1263a9ab
JP
203 struct mutex lock; /* Protects consistency of region, region2 pointers
204 * and vchunk_list.
205 */
0f54236d 206 struct mlxsw_sp_acl_tcam_region *region;
e5e7962e 207 struct mlxsw_sp_acl_tcam_region *region2; /* Used during migration */
0f54236d 208 struct list_head list; /* Member of a TCAM group */
e5e7962e 209 struct list_head tlist; /* Member of a TCAM */
b2d6b4d2 210 struct list_head vchunk_list; /* List of vchunks under this vregion */
0f54236d 211 struct mlxsw_afk_key_info *key_info;
e5e7962e 212 struct mlxsw_sp_acl_tcam *tcam;
6b861682 213 struct mlxsw_sp_acl_tcam_vgroup *vgroup;
f9b274ce
JP
214 struct {
215 struct delayed_work dw;
559c2768 216 struct mlxsw_sp_acl_tcam_rehash_ctx ctx;
f9b274ce 217 } rehash;
e5e7962e 218 struct mlxsw_sp *mlxsw_sp;
79604b6e 219 unsigned int ref_count;
0f54236d
JP
220};
221
b2d6b4d2
JP
222struct mlxsw_sp_acl_tcam_vchunk;
223
22a67766 224struct mlxsw_sp_acl_tcam_chunk {
b2d6b4d2 225 struct mlxsw_sp_acl_tcam_vchunk *vchunk;
e5e7962e 226 struct mlxsw_sp_acl_tcam_region *region;
e99f8e7f 227 unsigned long priv[];
b2d6b4d2
JP
228 /* priv has to be always the last item */
229};
230
231struct mlxsw_sp_acl_tcam_vchunk {
232 struct mlxsw_sp_acl_tcam_chunk *chunk;
e5e7962e 233 struct mlxsw_sp_acl_tcam_chunk *chunk2; /* Used during migration */
0f54236d 234 struct list_head list; /* Member of a TCAM vregion */
22a67766 235 struct rhash_head ht_node; /* Member of a chunk HT */
e5e7962e 236 struct list_head ventry_list;
0f54236d 237 unsigned int priority; /* Priority within the vregion and group */
2802aadf 238 struct mlxsw_sp_acl_tcam_vgroup *vgroup;
0f54236d 239 struct mlxsw_sp_acl_tcam_vregion *vregion;
22a67766
JP
240 unsigned int ref_count;
241};
242
243struct mlxsw_sp_acl_tcam_entry {
c4c2dc54 244 struct mlxsw_sp_acl_tcam_ventry *ventry;
e5e7962e 245 struct mlxsw_sp_acl_tcam_chunk *chunk;
e99f8e7f 246 unsigned long priv[];
64eccd00 247 /* priv has to be always the last item */
22a67766
JP
248};
249
c4c2dc54
JP
250struct mlxsw_sp_acl_tcam_ventry {
251 struct mlxsw_sp_acl_tcam_entry *entry;
e5e7962e 252 struct list_head list; /* Member of a TCAM vchunk */
c4c2dc54 253 struct mlxsw_sp_acl_tcam_vchunk *vchunk;
e5e7962e 254 struct mlxsw_sp_acl_rule_info *rulei;
c4c2dc54
JP
255};
256
b2d6b4d2 257static const struct rhashtable_params mlxsw_sp_acl_tcam_vchunk_ht_params = {
22a67766 258 .key_len = sizeof(unsigned int),
b2d6b4d2
JP
259 .key_offset = offsetof(struct mlxsw_sp_acl_tcam_vchunk, priority),
260 .head_offset = offsetof(struct mlxsw_sp_acl_tcam_vchunk, ht_node),
22a67766
JP
261 .automatic_shrinking = true,
262};
263
264static int mlxsw_sp_acl_tcam_group_update(struct mlxsw_sp *mlxsw_sp,
265 struct mlxsw_sp_acl_tcam_group *group)
266{
2802aadf 267 struct mlxsw_sp_acl_tcam_region *region;
22a67766
JP
268 char pagt_pl[MLXSW_REG_PAGT_LEN];
269 int acl_index = 0;
270
271 mlxsw_reg_pagt_pack(pagt_pl, group->id);
2802aadf
JP
272 list_for_each_entry(region, &group->region_list, list) {
273 bool multi = false;
274
275 /* Check if the next entry in the list has the same vregion. */
276 if (region->list.next != &group->region_list &&
277 list_next_entry(region, list)->vregion == region->vregion)
278 multi = true;
0f54236d 279 mlxsw_reg_pagt_acl_id_pack(pagt_pl, acl_index++,
2802aadf 280 region->id, multi);
e5e7962e 281 }
22a67766
JP
282 mlxsw_reg_pagt_size_set(pagt_pl, acl_index);
283 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pagt), pagt_pl);
284}
285
286static int
2802aadf
JP
287mlxsw_sp_acl_tcam_group_add(struct mlxsw_sp_acl_tcam *tcam,
288 struct mlxsw_sp_acl_tcam_group *group)
22a67766
JP
289{
290 int err;
291
292 group->tcam = tcam;
5ec2ee28 293 mutex_init(&group->lock);
2802aadf
JP
294 INIT_LIST_HEAD(&group->region_list);
295
296 err = mlxsw_sp_acl_tcam_group_id_get(tcam, &group->id);
297 if (err)
298 return err;
299
300 return 0;
301}
302
303static void mlxsw_sp_acl_tcam_group_del(struct mlxsw_sp_acl_tcam_group *group)
304{
305 struct mlxsw_sp_acl_tcam *tcam = group->tcam;
306
5ec2ee28 307 mutex_destroy(&group->lock);
2802aadf
JP
308 mlxsw_sp_acl_tcam_group_id_put(tcam, group->id);
309 WARN_ON(!list_empty(&group->region_list));
310}
311
312static int
313mlxsw_sp_acl_tcam_vgroup_add(struct mlxsw_sp *mlxsw_sp,
314 struct mlxsw_sp_acl_tcam *tcam,
315 struct mlxsw_sp_acl_tcam_vgroup *vgroup,
316 const struct mlxsw_sp_acl_tcam_pattern *patterns,
317 unsigned int patterns_count,
6b861682
JP
318 struct mlxsw_afk_element_usage *tmplt_elusage,
319 bool vregion_rehash_enabled)
2802aadf
JP
320{
321 int err;
322
323 vgroup->patterns = patterns;
324 vgroup->patterns_count = patterns_count;
6b861682
JP
325 vgroup->vregion_rehash_enabled = vregion_rehash_enabled;
326
e2f2a1fd 327 if (tmplt_elusage) {
2802aadf
JP
328 vgroup->tmplt_elusage_set = true;
329 memcpy(&vgroup->tmplt_elusage, tmplt_elusage,
330 sizeof(vgroup->tmplt_elusage));
e2f2a1fd 331 }
2802aadf
JP
332 INIT_LIST_HEAD(&vgroup->vregion_list);
333
334 err = mlxsw_sp_acl_tcam_group_add(tcam, &vgroup->group);
22a67766
JP
335 if (err)
336 return err;
337
2802aadf 338 err = rhashtable_init(&vgroup->vchunk_ht,
b2d6b4d2 339 &mlxsw_sp_acl_tcam_vchunk_ht_params);
22a67766
JP
340 if (err)
341 goto err_rhashtable_init;
342
343 return 0;
344
345err_rhashtable_init:
2802aadf 346 mlxsw_sp_acl_tcam_group_del(&vgroup->group);
22a67766
JP
347 return err;
348}
349
2802aadf
JP
350static void
351mlxsw_sp_acl_tcam_vgroup_del(struct mlxsw_sp_acl_tcam_vgroup *vgroup)
22a67766 352{
2802aadf
JP
353 rhashtable_destroy(&vgroup->vchunk_ht);
354 mlxsw_sp_acl_tcam_group_del(&vgroup->group);
355 WARN_ON(!list_empty(&vgroup->vregion_list));
22a67766
JP
356}
357
358static int
359mlxsw_sp_acl_tcam_group_bind(struct mlxsw_sp *mlxsw_sp,
360 struct mlxsw_sp_acl_tcam_group *group,
4b23258d
JP
361 struct mlxsw_sp_port *mlxsw_sp_port,
362 bool ingress)
22a67766 363{
22a67766
JP
364 char ppbt_pl[MLXSW_REG_PPBT_LEN];
365
02caf499
JP
366 mlxsw_reg_ppbt_pack(ppbt_pl, ingress ? MLXSW_REG_PXBT_E_IACL :
367 MLXSW_REG_PXBT_E_EACL,
368 MLXSW_REG_PXBT_OP_BIND, mlxsw_sp_port->local_port,
22a67766
JP
369 group->id);
370 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppbt), ppbt_pl);
371}
372
373static void
374mlxsw_sp_acl_tcam_group_unbind(struct mlxsw_sp *mlxsw_sp,
02caf499 375 struct mlxsw_sp_acl_tcam_group *group,
4b23258d
JP
376 struct mlxsw_sp_port *mlxsw_sp_port,
377 bool ingress)
22a67766
JP
378{
379 char ppbt_pl[MLXSW_REG_PPBT_LEN];
380
02caf499
JP
381 mlxsw_reg_ppbt_pack(ppbt_pl, ingress ? MLXSW_REG_PXBT_E_IACL :
382 MLXSW_REG_PXBT_E_EACL,
383 MLXSW_REG_PXBT_OP_UNBIND, mlxsw_sp_port->local_port,
22a67766
JP
384 group->id);
385 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppbt), ppbt_pl);
386}
387
0ade3b64
JP
388static u16
389mlxsw_sp_acl_tcam_group_id(struct mlxsw_sp_acl_tcam_group *group)
390{
391 return group->id;
392}
393
22a67766 394static unsigned int
0f54236d 395mlxsw_sp_acl_tcam_vregion_prio(struct mlxsw_sp_acl_tcam_vregion *vregion)
22a67766 396{
b2d6b4d2 397 struct mlxsw_sp_acl_tcam_vchunk *vchunk;
22a67766 398
b2d6b4d2 399 if (list_empty(&vregion->vchunk_list))
22a67766 400 return 0;
b2d6b4d2
JP
401 /* As a priority of a vregion, return priority of the first vchunk */
402 vchunk = list_first_entry(&vregion->vchunk_list,
403 typeof(*vchunk), list);
404 return vchunk->priority;
22a67766
JP
405}
406
407static unsigned int
0f54236d 408mlxsw_sp_acl_tcam_vregion_max_prio(struct mlxsw_sp_acl_tcam_vregion *vregion)
22a67766 409{
b2d6b4d2 410 struct mlxsw_sp_acl_tcam_vchunk *vchunk;
22a67766 411
b2d6b4d2 412 if (list_empty(&vregion->vchunk_list))
22a67766 413 return 0;
b2d6b4d2
JP
414 vchunk = list_last_entry(&vregion->vchunk_list,
415 typeof(*vchunk), list);
416 return vchunk->priority;
22a67766
JP
417}
418
0f54236d
JP
419static int
420mlxsw_sp_acl_tcam_group_region_attach(struct mlxsw_sp *mlxsw_sp,
2802aadf
JP
421 struct mlxsw_sp_acl_tcam_group *group,
422 struct mlxsw_sp_acl_tcam_region *region,
79604b6e 423 unsigned int priority,
2802aadf 424 struct mlxsw_sp_acl_tcam_region *next_region)
22a67766 425{
2802aadf
JP
426 struct mlxsw_sp_acl_tcam_region *region2;
427 struct list_head *pos;
0f54236d
JP
428 int err;
429
5ec2ee28
JP
430 mutex_lock(&group->lock);
431 if (group->region_count == group->tcam->max_group_size) {
432 err = -ENOBUFS;
433 goto err_region_count_check;
434 }
0f54236d 435
2802aadf
JP
436 if (next_region) {
437 /* If the next region is defined, place the new one
438 * before it. The next one is a sibling.
439 */
440 pos = &next_region->list;
441 } else {
442 /* Position the region inside the list according to priority */
443 list_for_each(pos, &group->region_list) {
444 region2 = list_entry(pos, typeof(*region2), list);
445 if (mlxsw_sp_acl_tcam_vregion_prio(region2->vregion) >
79604b6e 446 priority)
2802aadf
JP
447 break;
448 }
449 }
450 list_add_tail(&region->list, pos);
451 region->group = group;
452
0f54236d
JP
453 err = mlxsw_sp_acl_tcam_group_update(mlxsw_sp, group);
454 if (err)
2802aadf 455 goto err_group_update;
22a67766 456
22a67766 457 group->region_count++;
5ec2ee28 458 mutex_unlock(&group->lock);
0f54236d 459 return 0;
2802aadf
JP
460
461err_group_update:
462 list_del(&region->list);
5ec2ee28
JP
463err_region_count_check:
464 mutex_unlock(&group->lock);
2802aadf 465 return err;
22a67766
JP
466}
467
468static void
0f54236d
JP
469mlxsw_sp_acl_tcam_group_region_detach(struct mlxsw_sp *mlxsw_sp,
470 struct mlxsw_sp_acl_tcam_region *region)
22a67766 471{
2802aadf 472 struct mlxsw_sp_acl_tcam_group *group = region->group;
0f54236d 473
5ec2ee28 474 mutex_lock(&group->lock);
2802aadf 475 list_del(&region->list);
22a67766 476 group->region_count--;
0f54236d 477 mlxsw_sp_acl_tcam_group_update(mlxsw_sp, group);
5ec2ee28 478 mutex_unlock(&group->lock);
22a67766
JP
479}
480
481static int
2802aadf
JP
482mlxsw_sp_acl_tcam_vgroup_vregion_attach(struct mlxsw_sp *mlxsw_sp,
483 struct mlxsw_sp_acl_tcam_vgroup *vgroup,
79604b6e
JP
484 struct mlxsw_sp_acl_tcam_vregion *vregion,
485 unsigned int priority)
22a67766 486{
0f54236d
JP
487 struct mlxsw_sp_acl_tcam_vregion *vregion2;
488 struct list_head *pos;
22a67766
JP
489 int err;
490
0f54236d 491 /* Position the vregion inside the list according to priority */
2802aadf 492 list_for_each(pos, &vgroup->vregion_list) {
0f54236d 493 vregion2 = list_entry(pos, typeof(*vregion2), list);
79604b6e 494 if (mlxsw_sp_acl_tcam_vregion_prio(vregion2) > priority)
0f54236d
JP
495 break;
496 }
497 list_add_tail(&vregion->list, pos);
22a67766 498
2802aadf 499 err = mlxsw_sp_acl_tcam_group_region_attach(mlxsw_sp, &vgroup->group,
79604b6e
JP
500 vregion->region,
501 priority, NULL);
22a67766 502 if (err)
0f54236d 503 goto err_region_attach;
22a67766
JP
504
505 return 0;
506
0f54236d
JP
507err_region_attach:
508 list_del(&vregion->list);
22a67766
JP
509 return err;
510}
511
512static void
2802aadf
JP
513mlxsw_sp_acl_tcam_vgroup_vregion_detach(struct mlxsw_sp *mlxsw_sp,
514 struct mlxsw_sp_acl_tcam_vregion *vregion)
22a67766 515{
0f54236d 516 list_del(&vregion->list);
e5e7962e
JP
517 if (vregion->region2)
518 mlxsw_sp_acl_tcam_group_region_detach(mlxsw_sp,
519 vregion->region2);
0f54236d 520 mlxsw_sp_acl_tcam_group_region_detach(mlxsw_sp, vregion->region);
22a67766
JP
521}
522
0f54236d 523static struct mlxsw_sp_acl_tcam_vregion *
2802aadf
JP
524mlxsw_sp_acl_tcam_vgroup_vregion_find(struct mlxsw_sp_acl_tcam_vgroup *vgroup,
525 unsigned int priority,
526 struct mlxsw_afk_element_usage *elusage,
527 bool *p_need_split)
22a67766 528{
0f54236d 529 struct mlxsw_sp_acl_tcam_vregion *vregion, *vregion2;
22a67766
JP
530 struct list_head *pos;
531 bool issubset;
532
2802aadf 533 list_for_each(pos, &vgroup->vregion_list) {
0f54236d 534 vregion = list_entry(pos, typeof(*vregion), list);
22a67766
JP
535
536 /* First, check if the requested priority does not rather belong
0f54236d 537 * under some of the next vregions.
22a67766 538 */
2802aadf 539 if (pos->next != &vgroup->vregion_list) { /* not last */
0f54236d
JP
540 vregion2 = list_entry(pos->next, typeof(*vregion2),
541 list);
542 if (priority >=
543 mlxsw_sp_acl_tcam_vregion_prio(vregion2))
22a67766
JP
544 continue;
545 }
546
0f54236d
JP
547 issubset = mlxsw_afk_key_info_subset(vregion->key_info,
548 elusage);
22a67766
JP
549
550 /* If requested element usage would not fit and the priority
0f54236d
JP
551 * is lower than the currently inspected vregion we cannot
552 * use this region, so return NULL to indicate new vregion has
22a67766
JP
553 * to be created.
554 */
555 if (!issubset &&
0f54236d 556 priority < mlxsw_sp_acl_tcam_vregion_prio(vregion))
22a67766
JP
557 return NULL;
558
559 /* If requested element usage would not fit and the priority
0f54236d
JP
560 * is higher than the currently inspected vregion we cannot
561 * use this vregion. There is still some hope that the next
562 * vregion would be the fit. So let it be processed and
22a67766
JP
563 * eventually break at the check right above this.
564 */
565 if (!issubset &&
0f54236d 566 priority > mlxsw_sp_acl_tcam_vregion_max_prio(vregion))
22a67766
JP
567 continue;
568
0f54236d 569 /* Indicate if the vregion needs to be split in order to add
22a67766 570 * the requested priority. Split is needed when requested
0f54236d 571 * element usage won't fit into the found vregion.
22a67766
JP
572 */
573 *p_need_split = !issubset;
0f54236d 574 return vregion;
22a67766 575 }
0f54236d 576 return NULL; /* New vregion has to be created. */
22a67766
JP
577}
578
579static void
2802aadf
JP
580mlxsw_sp_acl_tcam_vgroup_use_patterns(struct mlxsw_sp_acl_tcam_vgroup *vgroup,
581 struct mlxsw_afk_element_usage *elusage,
582 struct mlxsw_afk_element_usage *out)
22a67766
JP
583{
584 const struct mlxsw_sp_acl_tcam_pattern *pattern;
585 int i;
586
e2f2a1fd
JP
587 /* In case the template is set, we don't have to look up the pattern
588 * and just use the template.
589 */
2802aadf
JP
590 if (vgroup->tmplt_elusage_set) {
591 memcpy(out, &vgroup->tmplt_elusage, sizeof(*out));
e2f2a1fd
JP
592 WARN_ON(!mlxsw_afk_element_usage_subset(elusage, out));
593 return;
594 }
595
2802aadf
JP
596 for (i = 0; i < vgroup->patterns_count; i++) {
597 pattern = &vgroup->patterns[i];
22a67766
JP
598 mlxsw_afk_element_usage_fill(out, pattern->elements,
599 pattern->elements_count);
600 if (mlxsw_afk_element_usage_subset(elusage, out))
601 return;
602 }
603 memcpy(out, elusage, sizeof(*out));
604}
605
22a67766
JP
606static int
607mlxsw_sp_acl_tcam_region_alloc(struct mlxsw_sp *mlxsw_sp,
608 struct mlxsw_sp_acl_tcam_region *region)
609{
610 struct mlxsw_afk_key_info *key_info = region->key_info;
611 char ptar_pl[MLXSW_REG_PTAR_LEN];
612 unsigned int encodings_count;
613 int i;
614 int err;
615
616 mlxsw_reg_ptar_pack(ptar_pl, MLXSW_REG_PTAR_OP_ALLOC,
45e0620d 617 region->key_type,
22a67766
JP
618 MLXSW_SP_ACL_TCAM_REGION_BASE_COUNT,
619 region->id, region->tcam_region_info);
620 encodings_count = mlxsw_afk_key_info_blocks_count_get(key_info);
621 for (i = 0; i < encodings_count; i++) {
622 u16 encoding;
623
624 encoding = mlxsw_afk_key_info_block_encoding_get(key_info, i);
625 mlxsw_reg_ptar_key_id_pack(ptar_pl, i, encoding);
626 }
627 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptar), ptar_pl);
628 if (err)
629 return err;
630 mlxsw_reg_ptar_unpack(ptar_pl, region->tcam_region_info);
631 return 0;
632}
633
634static void
635mlxsw_sp_acl_tcam_region_free(struct mlxsw_sp *mlxsw_sp,
636 struct mlxsw_sp_acl_tcam_region *region)
637{
638 char ptar_pl[MLXSW_REG_PTAR_LEN];
639
45e0620d
JP
640 mlxsw_reg_ptar_pack(ptar_pl, MLXSW_REG_PTAR_OP_FREE,
641 region->key_type, 0, region->id,
22a67766
JP
642 region->tcam_region_info);
643 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptar), ptar_pl);
644}
645
22a67766
JP
646static int
647mlxsw_sp_acl_tcam_region_enable(struct mlxsw_sp *mlxsw_sp,
648 struct mlxsw_sp_acl_tcam_region *region)
649{
650 char pacl_pl[MLXSW_REG_PACL_LEN];
651
652 mlxsw_reg_pacl_pack(pacl_pl, region->id, true,
653 region->tcam_region_info);
654 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pacl), pacl_pl);
655}
656
657static void
658mlxsw_sp_acl_tcam_region_disable(struct mlxsw_sp *mlxsw_sp,
659 struct mlxsw_sp_acl_tcam_region *region)
660{
661 char pacl_pl[MLXSW_REG_PACL_LEN];
662
663 mlxsw_reg_pacl_pack(pacl_pl, region->id, false,
664 region->tcam_region_info);
665 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pacl), pacl_pl);
666}
667
22a67766
JP
668static struct mlxsw_sp_acl_tcam_region *
669mlxsw_sp_acl_tcam_region_create(struct mlxsw_sp *mlxsw_sp,
670 struct mlxsw_sp_acl_tcam *tcam,
e5e7962e
JP
671 struct mlxsw_sp_acl_tcam_vregion *vregion,
672 void *hints_priv)
22a67766 673{
64eccd00 674 const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
22a67766
JP
675 struct mlxsw_sp_acl_tcam_region *region;
676 int err;
677
64eccd00 678 region = kzalloc(sizeof(*region) + ops->region_priv_size, GFP_KERNEL);
22a67766
JP
679 if (!region)
680 return ERR_PTR(-ENOMEM);
22a67766 681 region->mlxsw_sp = mlxsw_sp;
0f54236d
JP
682 region->vregion = vregion;
683 region->key_info = vregion->key_info;
22a67766
JP
684
685 err = mlxsw_sp_acl_tcam_region_id_get(tcam, &region->id);
686 if (err)
687 goto err_region_id_get;
688
a6b9c87d
IS
689 err = ops->region_associate(mlxsw_sp, region);
690 if (err)
691 goto err_tcam_region_associate;
692
64eccd00 693 region->key_type = ops->key_type;
22a67766
JP
694 err = mlxsw_sp_acl_tcam_region_alloc(mlxsw_sp, region);
695 if (err)
696 goto err_tcam_region_alloc;
697
698 err = mlxsw_sp_acl_tcam_region_enable(mlxsw_sp, region);
699 if (err)
700 goto err_tcam_region_enable;
701
a339bf8a 702 err = ops->region_init(mlxsw_sp, region->priv, tcam->priv,
e5e7962e 703 region, hints_priv);
22a67766 704 if (err)
64eccd00 705 goto err_tcam_region_init;
22a67766
JP
706
707 return region;
708
64eccd00 709err_tcam_region_init:
22a67766
JP
710 mlxsw_sp_acl_tcam_region_disable(mlxsw_sp, region);
711err_tcam_region_enable:
712 mlxsw_sp_acl_tcam_region_free(mlxsw_sp, region);
713err_tcam_region_alloc:
a6b9c87d 714err_tcam_region_associate:
22a67766
JP
715 mlxsw_sp_acl_tcam_region_id_put(tcam, region->id);
716err_region_id_get:
22a67766
JP
717 kfree(region);
718 return ERR_PTR(err);
719}
720
721static void
722mlxsw_sp_acl_tcam_region_destroy(struct mlxsw_sp *mlxsw_sp,
723 struct mlxsw_sp_acl_tcam_region *region)
724{
64eccd00
JP
725 const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
726
727 ops->region_fini(mlxsw_sp, region->priv);
22a67766
JP
728 mlxsw_sp_acl_tcam_region_disable(mlxsw_sp, region);
729 mlxsw_sp_acl_tcam_region_free(mlxsw_sp, region);
2802aadf 730 mlxsw_sp_acl_tcam_region_id_put(region->group->tcam,
0f54236d 731 region->id);
22a67766
JP
732 kfree(region);
733}
734
e5e7962e
JP
735static void
736mlxsw_sp_acl_tcam_vregion_rehash_work_schedule(struct mlxsw_sp_acl_tcam_vregion *vregion)
737{
738 unsigned long interval = vregion->tcam->vregion_rehash_intrvl;
739
740 if (!interval)
741 return;
f9b274ce 742 mlxsw_core_schedule_dw(&vregion->rehash.dw,
e5e7962e
JP
743 msecs_to_jiffies(interval));
744}
745
b2c091ce 746static void
e5e7962e 747mlxsw_sp_acl_tcam_vregion_rehash(struct mlxsw_sp *mlxsw_sp,
c9c9af91
JP
748 struct mlxsw_sp_acl_tcam_vregion *vregion,
749 int *credits);
e5e7962e
JP
750
751static void mlxsw_sp_acl_tcam_vregion_rehash_work(struct work_struct *work)
752{
753 struct mlxsw_sp_acl_tcam_vregion *vregion =
754 container_of(work, struct mlxsw_sp_acl_tcam_vregion,
f9b274ce 755 rehash.dw.work);
c9c9af91 756 int credits = MLXSW_SP_ACL_TCAM_VREGION_REHASH_CREDITS;
e5e7962e 757
b2c091ce 758 mlxsw_sp_acl_tcam_vregion_rehash(vregion->mlxsw_sp, vregion, &credits);
c9c9af91
JP
759 if (credits < 0)
760 /* Rehash gone out of credits so it was interrupted.
761 * Schedule the work as soon as possible to continue.
762 */
763 mlxsw_core_schedule_dw(&vregion->rehash.dw, 0);
764 else
765 mlxsw_sp_acl_tcam_vregion_rehash_work_schedule(vregion);
e5e7962e
JP
766}
767
6f9579d4
JP
768static void
769mlxsw_sp_acl_tcam_rehash_ctx_vchunk_changed(struct mlxsw_sp_acl_tcam_vchunk *vchunk)
770{
771 struct mlxsw_sp_acl_tcam_vregion *vregion = vchunk->vregion;
772
773 /* If a rule was added or deleted from vchunk which is currently
774 * under rehash migration, we have to reset the ventry pointers
775 * to make sure all rules are properly migrated.
776 */
777 if (vregion->rehash.ctx.current_vchunk == vchunk) {
778 vregion->rehash.ctx.start_ventry = NULL;
779 vregion->rehash.ctx.stop_ventry = NULL;
780 }
781}
782
783static void
784mlxsw_sp_acl_tcam_rehash_ctx_vregion_changed(struct mlxsw_sp_acl_tcam_vregion *vregion)
785{
786 /* If a chunk was added or deleted from vregion we have to reset
787 * the current chunk pointer to make sure all chunks
788 * are properly migrated.
789 */
790 vregion->rehash.ctx.current_vchunk = NULL;
791}
792
0f54236d
JP
793static struct mlxsw_sp_acl_tcam_vregion *
794mlxsw_sp_acl_tcam_vregion_create(struct mlxsw_sp *mlxsw_sp,
79604b6e
JP
795 struct mlxsw_sp_acl_tcam_vgroup *vgroup,
796 unsigned int priority,
0f54236d
JP
797 struct mlxsw_afk_element_usage *elusage)
798{
e5e7962e 799 const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
0f54236d 800 struct mlxsw_afk *afk = mlxsw_sp_acl_afk(mlxsw_sp->acl);
79604b6e 801 struct mlxsw_sp_acl_tcam *tcam = vgroup->group.tcam;
0f54236d
JP
802 struct mlxsw_sp_acl_tcam_vregion *vregion;
803 int err;
804
805 vregion = kzalloc(sizeof(*vregion), GFP_KERNEL);
806 if (!vregion)
807 return ERR_PTR(-ENOMEM);
b2d6b4d2 808 INIT_LIST_HEAD(&vregion->vchunk_list);
1263a9ab 809 mutex_init(&vregion->lock);
e5e7962e
JP
810 vregion->tcam = tcam;
811 vregion->mlxsw_sp = mlxsw_sp;
6b861682 812 vregion->vgroup = vgroup;
79604b6e 813 vregion->ref_count = 1;
0f54236d
JP
814
815 vregion->key_info = mlxsw_afk_key_info_get(afk, elusage);
816 if (IS_ERR(vregion->key_info)) {
817 err = PTR_ERR(vregion->key_info);
818 goto err_key_info_get;
819 }
820
821 vregion->region = mlxsw_sp_acl_tcam_region_create(mlxsw_sp, tcam,
e5e7962e 822 vregion, NULL);
0f54236d
JP
823 if (IS_ERR(vregion->region)) {
824 err = PTR_ERR(vregion->region);
825 goto err_region_create;
826 }
827
79604b6e
JP
828 err = mlxsw_sp_acl_tcam_vgroup_vregion_attach(mlxsw_sp, vgroup, vregion,
829 priority);
830 if (err)
831 goto err_vgroup_vregion_attach;
832
6b861682 833 if (vgroup->vregion_rehash_enabled && ops->region_rehash_hints_get) {
e5e7962e 834 /* Create the delayed work for vregion periodic rehash */
f9b274ce 835 INIT_DELAYED_WORK(&vregion->rehash.dw,
e5e7962e
JP
836 mlxsw_sp_acl_tcam_vregion_rehash_work);
837 mlxsw_sp_acl_tcam_vregion_rehash_work_schedule(vregion);
7b0f62ee 838 mutex_lock(&tcam->lock);
6b861682 839 list_add_tail(&vregion->tlist, &tcam->vregion_list);
7b0f62ee 840 mutex_unlock(&tcam->lock);
e5e7962e
JP
841 }
842
0f54236d
JP
843 return vregion;
844
79604b6e
JP
845err_vgroup_vregion_attach:
846 mlxsw_sp_acl_tcam_region_destroy(mlxsw_sp, vregion->region);
0f54236d
JP
847err_region_create:
848 mlxsw_afk_key_info_put(vregion->key_info);
849err_key_info_get:
850 kfree(vregion);
851 return ERR_PTR(err);
852}
853
854static void
855mlxsw_sp_acl_tcam_vregion_destroy(struct mlxsw_sp *mlxsw_sp,
856 struct mlxsw_sp_acl_tcam_vregion *vregion)
857{
e5e7962e 858 const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
6b861682 859 struct mlxsw_sp_acl_tcam_vgroup *vgroup = vregion->vgroup;
7b0f62ee 860 struct mlxsw_sp_acl_tcam *tcam = vregion->tcam;
e5e7962e 861
6b861682 862 if (vgroup->vregion_rehash_enabled && ops->region_rehash_hints_get) {
7b0f62ee 863 mutex_lock(&tcam->lock);
6b861682 864 list_del(&vregion->tlist);
7b0f62ee 865 mutex_unlock(&tcam->lock);
f9b274ce 866 cancel_delayed_work_sync(&vregion->rehash.dw);
6b861682 867 }
79604b6e 868 mlxsw_sp_acl_tcam_vgroup_vregion_detach(mlxsw_sp, vregion);
e5e7962e
JP
869 if (vregion->region2)
870 mlxsw_sp_acl_tcam_region_destroy(mlxsw_sp, vregion->region2);
0f54236d
JP
871 mlxsw_sp_acl_tcam_region_destroy(mlxsw_sp, vregion->region);
872 mlxsw_afk_key_info_put(vregion->key_info);
1263a9ab 873 mutex_destroy(&vregion->lock);
0f54236d
JP
874 kfree(vregion);
875}
876
98bbf70c
JP
877u32 mlxsw_sp_acl_tcam_vregion_rehash_intrvl_get(struct mlxsw_sp *mlxsw_sp,
878 struct mlxsw_sp_acl_tcam *tcam)
879{
880 const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
881 u32 vregion_rehash_intrvl;
882
883 if (WARN_ON(!ops->region_rehash_hints_get))
884 return 0;
885 vregion_rehash_intrvl = tcam->vregion_rehash_intrvl;
886 return vregion_rehash_intrvl;
887}
888
889int mlxsw_sp_acl_tcam_vregion_rehash_intrvl_set(struct mlxsw_sp *mlxsw_sp,
890 struct mlxsw_sp_acl_tcam *tcam,
891 u32 val)
892{
893 const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
894 struct mlxsw_sp_acl_tcam_vregion *vregion;
895
896 if (val < MLXSW_SP_ACL_TCAM_VREGION_REHASH_INTRVL_MIN && val)
897 return -EINVAL;
898 if (WARN_ON(!ops->region_rehash_hints_get))
899 return -EOPNOTSUPP;
900 tcam->vregion_rehash_intrvl = val;
7b0f62ee 901 mutex_lock(&tcam->lock);
98bbf70c
JP
902 list_for_each_entry(vregion, &tcam->vregion_list, tlist) {
903 if (val)
f9b274ce 904 mlxsw_core_schedule_dw(&vregion->rehash.dw, 0);
98bbf70c 905 else
f9b274ce 906 cancel_delayed_work_sync(&vregion->rehash.dw);
98bbf70c 907 }
7b0f62ee 908 mutex_unlock(&tcam->lock);
98bbf70c
JP
909 return 0;
910}
911
79604b6e
JP
912static struct mlxsw_sp_acl_tcam_vregion *
913mlxsw_sp_acl_tcam_vregion_get(struct mlxsw_sp *mlxsw_sp,
914 struct mlxsw_sp_acl_tcam_vgroup *vgroup,
915 unsigned int priority,
916 struct mlxsw_afk_element_usage *elusage)
22a67766 917{
79604b6e 918 struct mlxsw_afk_element_usage vregion_elusage;
0f54236d 919 struct mlxsw_sp_acl_tcam_vregion *vregion;
22a67766 920 bool need_split;
22a67766 921
2802aadf
JP
922 vregion = mlxsw_sp_acl_tcam_vgroup_vregion_find(vgroup, priority,
923 elusage, &need_split);
79604b6e
JP
924 if (vregion) {
925 if (need_split) {
926 /* According to priority, new vchunk should belong to
927 * an existing vregion. However, this vchunk needs
928 * elements that vregion does not contain. We need
929 * to split the existing vregion into two and create
930 * a new vregion for the new vchunk in between.
931 * This is not supported now.
932 */
933 return ERR_PTR(-EOPNOTSUPP);
934 }
935 vregion->ref_count++;
936 return vregion;
22a67766
JP
937 }
938
79604b6e
JP
939 mlxsw_sp_acl_tcam_vgroup_use_patterns(vgroup, elusage,
940 &vregion_elusage);
22a67766 941
79604b6e
JP
942 return mlxsw_sp_acl_tcam_vregion_create(mlxsw_sp, vgroup, priority,
943 &vregion_elusage);
22a67766
JP
944}
945
946static void
79604b6e
JP
947mlxsw_sp_acl_tcam_vregion_put(struct mlxsw_sp *mlxsw_sp,
948 struct mlxsw_sp_acl_tcam_vregion *vregion)
22a67766 949{
79604b6e
JP
950 if (--vregion->ref_count)
951 return;
952 mlxsw_sp_acl_tcam_vregion_destroy(mlxsw_sp, vregion);
22a67766
JP
953}
954
955static struct mlxsw_sp_acl_tcam_chunk *
956mlxsw_sp_acl_tcam_chunk_create(struct mlxsw_sp *mlxsw_sp,
b2d6b4d2
JP
957 struct mlxsw_sp_acl_tcam_vchunk *vchunk,
958 struct mlxsw_sp_acl_tcam_region *region)
22a67766 959{
64eccd00 960 const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
22a67766 961 struct mlxsw_sp_acl_tcam_chunk *chunk;
b2d6b4d2
JP
962
963 chunk = kzalloc(sizeof(*chunk) + ops->chunk_priv_size, GFP_KERNEL);
964 if (!chunk)
965 return ERR_PTR(-ENOMEM);
966 chunk->vchunk = vchunk;
e5e7962e 967 chunk->region = region;
b2d6b4d2
JP
968
969 ops->chunk_init(region->priv, chunk->priv, vchunk->priority);
970 return chunk;
971}
972
973static void
974mlxsw_sp_acl_tcam_chunk_destroy(struct mlxsw_sp *mlxsw_sp,
975 struct mlxsw_sp_acl_tcam_chunk *chunk)
976{
977 const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
978
979 ops->chunk_fini(chunk->priv);
980 kfree(chunk);
981}
982
983static struct mlxsw_sp_acl_tcam_vchunk *
984mlxsw_sp_acl_tcam_vchunk_create(struct mlxsw_sp *mlxsw_sp,
2802aadf 985 struct mlxsw_sp_acl_tcam_vgroup *vgroup,
b2d6b4d2
JP
986 unsigned int priority,
987 struct mlxsw_afk_element_usage *elusage)
988{
6ef4889f 989 struct mlxsw_sp_acl_tcam_vchunk *vchunk, *vchunk2;
79604b6e 990 struct mlxsw_sp_acl_tcam_vregion *vregion;
6ef4889f 991 struct list_head *pos;
22a67766
JP
992 int err;
993
994 if (priority == MLXSW_SP_ACL_TCAM_CATCHALL_PRIO)
995 return ERR_PTR(-EINVAL);
996
b2d6b4d2
JP
997 vchunk = kzalloc(sizeof(*vchunk), GFP_KERNEL);
998 if (!vchunk)
22a67766 999 return ERR_PTR(-ENOMEM);
e5e7962e 1000 INIT_LIST_HEAD(&vchunk->ventry_list);
b2d6b4d2 1001 vchunk->priority = priority;
2802aadf 1002 vchunk->vgroup = vgroup;
b2d6b4d2 1003 vchunk->ref_count = 1;
22a67766 1004
79604b6e
JP
1005 vregion = mlxsw_sp_acl_tcam_vregion_get(mlxsw_sp, vgroup,
1006 priority, elusage);
1007 if (IS_ERR(vregion)) {
1008 err = PTR_ERR(vregion);
1009 goto err_vregion_get;
1010 }
1011
1012 vchunk->vregion = vregion;
22a67766 1013
2802aadf 1014 err = rhashtable_insert_fast(&vgroup->vchunk_ht, &vchunk->ht_node,
b2d6b4d2 1015 mlxsw_sp_acl_tcam_vchunk_ht_params);
22a67766
JP
1016 if (err)
1017 goto err_rhashtable_insert;
1018
1263a9ab 1019 mutex_lock(&vregion->lock);
b2d6b4d2
JP
1020 vchunk->chunk = mlxsw_sp_acl_tcam_chunk_create(mlxsw_sp, vchunk,
1021 vchunk->vregion->region);
1022 if (IS_ERR(vchunk->chunk)) {
1263a9ab 1023 mutex_unlock(&vregion->lock);
b2d6b4d2
JP
1024 err = PTR_ERR(vchunk->chunk);
1025 goto err_chunk_create;
1026 }
1027
6f9579d4 1028 mlxsw_sp_acl_tcam_rehash_ctx_vregion_changed(vregion);
6ef4889f
JP
1029
1030 /* Position the vchunk inside the list according to priority */
1031 list_for_each(pos, &vregion->vchunk_list) {
1032 vchunk2 = list_entry(pos, typeof(*vchunk2), list);
1033 if (vchunk2->priority > priority)
1034 break;
1035 }
1036 list_add_tail(&vchunk->list, pos);
1263a9ab 1037 mutex_unlock(&vregion->lock);
79604b6e 1038
b2d6b4d2 1039 return vchunk;
22a67766 1040
b2d6b4d2 1041err_chunk_create:
2802aadf 1042 rhashtable_remove_fast(&vgroup->vchunk_ht, &vchunk->ht_node,
b2d6b4d2 1043 mlxsw_sp_acl_tcam_vchunk_ht_params);
22a67766 1044err_rhashtable_insert:
79604b6e
JP
1045 mlxsw_sp_acl_tcam_vregion_put(mlxsw_sp, vregion);
1046err_vregion_get:
b2d6b4d2 1047 kfree(vchunk);
22a67766
JP
1048 return ERR_PTR(err);
1049}
1050
1051static void
b2d6b4d2
JP
1052mlxsw_sp_acl_tcam_vchunk_destroy(struct mlxsw_sp *mlxsw_sp,
1053 struct mlxsw_sp_acl_tcam_vchunk *vchunk)
22a67766 1054{
1263a9ab 1055 struct mlxsw_sp_acl_tcam_vregion *vregion = vchunk->vregion;
2802aadf 1056 struct mlxsw_sp_acl_tcam_vgroup *vgroup = vchunk->vgroup;
22a67766 1057
1263a9ab 1058 mutex_lock(&vregion->lock);
6f9579d4 1059 mlxsw_sp_acl_tcam_rehash_ctx_vregion_changed(vregion);
79604b6e 1060 list_del(&vchunk->list);
e5e7962e
JP
1061 if (vchunk->chunk2)
1062 mlxsw_sp_acl_tcam_chunk_destroy(mlxsw_sp, vchunk->chunk2);
b2d6b4d2 1063 mlxsw_sp_acl_tcam_chunk_destroy(mlxsw_sp, vchunk->chunk);
1263a9ab 1064 mutex_unlock(&vregion->lock);
2802aadf 1065 rhashtable_remove_fast(&vgroup->vchunk_ht, &vchunk->ht_node,
b2d6b4d2 1066 mlxsw_sp_acl_tcam_vchunk_ht_params);
79604b6e 1067 mlxsw_sp_acl_tcam_vregion_put(mlxsw_sp, vchunk->vregion);
b2d6b4d2 1068 kfree(vchunk);
22a67766
JP
1069}
1070
b2d6b4d2
JP
1071static struct mlxsw_sp_acl_tcam_vchunk *
1072mlxsw_sp_acl_tcam_vchunk_get(struct mlxsw_sp *mlxsw_sp,
2802aadf 1073 struct mlxsw_sp_acl_tcam_vgroup *vgroup,
b2d6b4d2
JP
1074 unsigned int priority,
1075 struct mlxsw_afk_element_usage *elusage)
22a67766 1076{
b2d6b4d2 1077 struct mlxsw_sp_acl_tcam_vchunk *vchunk;
22a67766 1078
2802aadf 1079 vchunk = rhashtable_lookup_fast(&vgroup->vchunk_ht, &priority,
b2d6b4d2
JP
1080 mlxsw_sp_acl_tcam_vchunk_ht_params);
1081 if (vchunk) {
1082 if (WARN_ON(!mlxsw_afk_key_info_subset(vchunk->vregion->key_info,
22a67766
JP
1083 elusage)))
1084 return ERR_PTR(-EINVAL);
b2d6b4d2
JP
1085 vchunk->ref_count++;
1086 return vchunk;
22a67766 1087 }
2802aadf 1088 return mlxsw_sp_acl_tcam_vchunk_create(mlxsw_sp, vgroup,
b2d6b4d2 1089 priority, elusage);
22a67766
JP
1090}
1091
b2d6b4d2
JP
1092static void
1093mlxsw_sp_acl_tcam_vchunk_put(struct mlxsw_sp *mlxsw_sp,
1094 struct mlxsw_sp_acl_tcam_vchunk *vchunk)
22a67766 1095{
b2d6b4d2 1096 if (--vchunk->ref_count)
22a67766 1097 return;
b2d6b4d2 1098 mlxsw_sp_acl_tcam_vchunk_destroy(mlxsw_sp, vchunk);
22a67766
JP
1099}
1100
c4c2dc54
JP
1101static struct mlxsw_sp_acl_tcam_entry *
1102mlxsw_sp_acl_tcam_entry_create(struct mlxsw_sp *mlxsw_sp,
1103 struct mlxsw_sp_acl_tcam_ventry *ventry,
e5e7962e 1104 struct mlxsw_sp_acl_tcam_chunk *chunk)
22a67766 1105{
64eccd00 1106 const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
c4c2dc54 1107 struct mlxsw_sp_acl_tcam_entry *entry;
22a67766
JP
1108 int err;
1109
c4c2dc54
JP
1110 entry = kzalloc(sizeof(*entry) + ops->entry_priv_size, GFP_KERNEL);
1111 if (!entry)
1112 return ERR_PTR(-ENOMEM);
1113 entry->ventry = ventry;
e5e7962e 1114 entry->chunk = chunk;
22a67766 1115
e5e7962e
JP
1116 err = ops->entry_add(mlxsw_sp, chunk->region->priv, chunk->priv,
1117 entry->priv, ventry->rulei);
22a67766 1118 if (err)
64eccd00 1119 goto err_entry_add;
22a67766 1120
c4c2dc54 1121 return entry;
22a67766 1122
64eccd00 1123err_entry_add:
c4c2dc54
JP
1124 kfree(entry);
1125 return ERR_PTR(err);
22a67766
JP
1126}
1127
c4c2dc54 1128static void mlxsw_sp_acl_tcam_entry_destroy(struct mlxsw_sp *mlxsw_sp,
c4c2dc54 1129 struct mlxsw_sp_acl_tcam_entry *entry)
22a67766 1130{
64eccd00 1131 const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
22a67766 1132
e5e7962e
JP
1133 ops->entry_del(mlxsw_sp, entry->chunk->region->priv,
1134 entry->chunk->priv, entry->priv);
c4c2dc54 1135 kfree(entry);
22a67766
JP
1136}
1137
2507a64c
ND
1138static int
1139mlxsw_sp_acl_tcam_entry_action_replace(struct mlxsw_sp *mlxsw_sp,
c4c2dc54 1140 struct mlxsw_sp_acl_tcam_region *region,
2507a64c
ND
1141 struct mlxsw_sp_acl_tcam_entry *entry,
1142 struct mlxsw_sp_acl_rule_info *rulei)
1143{
1144 const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
2507a64c 1145
42d704e0 1146 return ops->entry_action_replace(mlxsw_sp, region->priv,
2507a64c
ND
1147 entry->priv, rulei);
1148}
1149
7fd056c2
AS
1150static int
1151mlxsw_sp_acl_tcam_entry_activity_get(struct mlxsw_sp *mlxsw_sp,
1152 struct mlxsw_sp_acl_tcam_entry *entry,
1153 bool *activity)
1154{
64eccd00 1155 const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
7fd056c2 1156
e5e7962e 1157 return ops->entry_activity_get(mlxsw_sp, entry->chunk->region->priv,
64eccd00 1158 entry->priv, activity);
7fd056c2
AS
1159}
1160
c4c2dc54 1161static int mlxsw_sp_acl_tcam_ventry_add(struct mlxsw_sp *mlxsw_sp,
2802aadf 1162 struct mlxsw_sp_acl_tcam_vgroup *vgroup,
c4c2dc54
JP
1163 struct mlxsw_sp_acl_tcam_ventry *ventry,
1164 struct mlxsw_sp_acl_rule_info *rulei)
1165{
1263a9ab 1166 struct mlxsw_sp_acl_tcam_vregion *vregion;
c4c2dc54
JP
1167 struct mlxsw_sp_acl_tcam_vchunk *vchunk;
1168 int err;
1169
2802aadf 1170 vchunk = mlxsw_sp_acl_tcam_vchunk_get(mlxsw_sp, vgroup, rulei->priority,
c4c2dc54
JP
1171 &rulei->values.elusage);
1172 if (IS_ERR(vchunk))
1173 return PTR_ERR(vchunk);
1174
1175 ventry->vchunk = vchunk;
e5e7962e 1176 ventry->rulei = rulei;
1263a9ab
JP
1177 vregion = vchunk->vregion;
1178
1179 mutex_lock(&vregion->lock);
c4c2dc54 1180 ventry->entry = mlxsw_sp_acl_tcam_entry_create(mlxsw_sp, ventry,
e5e7962e 1181 vchunk->chunk);
c4c2dc54 1182 if (IS_ERR(ventry->entry)) {
1263a9ab 1183 mutex_unlock(&vregion->lock);
c4c2dc54
JP
1184 err = PTR_ERR(ventry->entry);
1185 goto err_entry_create;
1186 }
1187
e5e7962e 1188 list_add_tail(&ventry->list, &vchunk->ventry_list);
6f9579d4 1189 mlxsw_sp_acl_tcam_rehash_ctx_vchunk_changed(vchunk);
1263a9ab 1190 mutex_unlock(&vregion->lock);
e5e7962e 1191
c4c2dc54
JP
1192 return 0;
1193
1194err_entry_create:
1195 mlxsw_sp_acl_tcam_vchunk_put(mlxsw_sp, vchunk);
1196 return err;
1197}
1198
1199static void mlxsw_sp_acl_tcam_ventry_del(struct mlxsw_sp *mlxsw_sp,
1200 struct mlxsw_sp_acl_tcam_ventry *ventry)
1201{
1202 struct mlxsw_sp_acl_tcam_vchunk *vchunk = ventry->vchunk;
1263a9ab 1203 struct mlxsw_sp_acl_tcam_vregion *vregion = vchunk->vregion;
c4c2dc54 1204
1263a9ab 1205 mutex_lock(&vregion->lock);
6f9579d4 1206 mlxsw_sp_acl_tcam_rehash_ctx_vchunk_changed(vchunk);
e5e7962e
JP
1207 list_del(&ventry->list);
1208 mlxsw_sp_acl_tcam_entry_destroy(mlxsw_sp, ventry->entry);
1263a9ab 1209 mutex_unlock(&vregion->lock);
c4c2dc54
JP
1210 mlxsw_sp_acl_tcam_vchunk_put(mlxsw_sp, vchunk);
1211}
1212
1213static int
1214mlxsw_sp_acl_tcam_ventry_action_replace(struct mlxsw_sp *mlxsw_sp,
1215 struct mlxsw_sp_acl_tcam_ventry *ventry,
1216 struct mlxsw_sp_acl_rule_info *rulei)
1217{
1218 struct mlxsw_sp_acl_tcam_vchunk *vchunk = ventry->vchunk;
1219
1220 return mlxsw_sp_acl_tcam_entry_action_replace(mlxsw_sp,
1221 vchunk->vregion->region,
1222 ventry->entry, rulei);
1223}
1224
1225static int
1226mlxsw_sp_acl_tcam_ventry_activity_get(struct mlxsw_sp *mlxsw_sp,
1227 struct mlxsw_sp_acl_tcam_ventry *ventry,
1228 bool *activity)
1229{
c4c2dc54 1230 return mlxsw_sp_acl_tcam_entry_activity_get(mlxsw_sp,
c4c2dc54
JP
1231 ventry->entry, activity);
1232}
1233
e5e7962e
JP
1234static int
1235mlxsw_sp_acl_tcam_ventry_migrate(struct mlxsw_sp *mlxsw_sp,
1236 struct mlxsw_sp_acl_tcam_ventry *ventry,
c9c9af91
JP
1237 struct mlxsw_sp_acl_tcam_chunk *chunk,
1238 int *credits)
e5e7962e 1239{
2c331593 1240 struct mlxsw_sp_acl_tcam_entry *new_entry;
e5e7962e 1241
6ca219e7 1242 /* First check if the entry is not already where we want it to be. */
2c331593 1243 if (ventry->entry->chunk == chunk)
6ca219e7
JP
1244 return 0;
1245
c9c9af91
JP
1246 if (--(*credits) < 0)
1247 return 0;
1248
2c331593
JP
1249 new_entry = mlxsw_sp_acl_tcam_entry_create(mlxsw_sp, ventry, chunk);
1250 if (IS_ERR(new_entry))
1251 return PTR_ERR(new_entry);
e5e7962e 1252 mlxsw_sp_acl_tcam_entry_destroy(mlxsw_sp, ventry->entry);
2c331593 1253 ventry->entry = new_entry;
e5e7962e
JP
1254 return 0;
1255}
1256
1257static int
844f01da
JP
1258mlxsw_sp_acl_tcam_vchunk_migrate_start(struct mlxsw_sp *mlxsw_sp,
1259 struct mlxsw_sp_acl_tcam_vchunk *vchunk,
1260 struct mlxsw_sp_acl_tcam_region *region,
1261 struct mlxsw_sp_acl_tcam_rehash_ctx *ctx)
e5e7962e 1262{
e1d2f7a9 1263 struct mlxsw_sp_acl_tcam_chunk *new_chunk;
e5e7962e 1264
e1d2f7a9 1265 new_chunk = mlxsw_sp_acl_tcam_chunk_create(mlxsw_sp, vchunk, region);
44fd86cb 1266 if (IS_ERR(new_chunk))
e1d2f7a9 1267 return PTR_ERR(new_chunk);
e1d2f7a9
JP
1268 vchunk->chunk2 = vchunk->chunk;
1269 vchunk->chunk = new_chunk;
6f9579d4
JP
1270 ctx->current_vchunk = vchunk;
1271 ctx->start_ventry = NULL;
1272 ctx->stop_ventry = NULL;
844f01da
JP
1273 return 0;
1274}
1275
1276static void
1277mlxsw_sp_acl_tcam_vchunk_migrate_end(struct mlxsw_sp *mlxsw_sp,
6f9579d4
JP
1278 struct mlxsw_sp_acl_tcam_vchunk *vchunk,
1279 struct mlxsw_sp_acl_tcam_rehash_ctx *ctx)
844f01da
JP
1280{
1281 mlxsw_sp_acl_tcam_chunk_destroy(mlxsw_sp, vchunk->chunk2);
1282 vchunk->chunk2 = NULL;
6f9579d4 1283 ctx->current_vchunk = NULL;
844f01da
JP
1284}
1285
1286static int
1287mlxsw_sp_acl_tcam_vchunk_migrate_one(struct mlxsw_sp *mlxsw_sp,
1288 struct mlxsw_sp_acl_tcam_vchunk *vchunk,
1289 struct mlxsw_sp_acl_tcam_region *region,
c9c9af91
JP
1290 struct mlxsw_sp_acl_tcam_rehash_ctx *ctx,
1291 int *credits)
844f01da
JP
1292{
1293 struct mlxsw_sp_acl_tcam_ventry *ventry;
1294 int err;
844f01da 1295
84350051
JP
1296 if (vchunk->chunk->region != region) {
1297 err = mlxsw_sp_acl_tcam_vchunk_migrate_start(mlxsw_sp, vchunk,
1298 region, ctx);
1299 if (err)
1300 return err;
1301 } else if (!vchunk->chunk2) {
1302 /* The chunk is already as it should be, nothing to do. */
1303 return 0;
1304 }
e1d2f7a9 1305
6f9579d4
JP
1306 /* If the migration got interrupted, we have the ventry to start from
1307 * stored in context.
1308 */
1309 if (ctx->start_ventry)
1310 ventry = ctx->start_ventry;
1311 else
1312 ventry = list_first_entry(&vchunk->ventry_list,
1313 typeof(*ventry), list);
1314
1315 list_for_each_entry_from(ventry, &vchunk->ventry_list, list) {
1316 /* During rollback, once we reach the ventry that failed
1317 * to migrate, we are done.
1318 */
1319 if (ventry == ctx->stop_ventry)
1320 break;
1321
e5e7962e 1322 err = mlxsw_sp_acl_tcam_ventry_migrate(mlxsw_sp, ventry,
c9c9af91 1323 vchunk->chunk, credits);
e5e7962e 1324 if (err) {
7c33c72b
JP
1325 if (ctx->this_is_rollback) {
1326 /* Save the ventry which we ended with and try
1327 * to continue later on.
1328 */
1329 ctx->start_ventry = ventry;
e5e7962e 1330 return err;
7c33c72b 1331 }
84350051
JP
1332 /* Swap the chunk and chunk2 pointers so the follow-up
1333 * rollback call will see the original chunk pointer
1334 * in vchunk->chunk.
1335 */
1336 swap(vchunk->chunk, vchunk->chunk2);
6f9579d4
JP
1337 /* The rollback has to be done from beginning of the
1338 * chunk, that is why we have to null the start_ventry.
1339 * However, we know where to stop the rollback,
1340 * at the current ventry.
1341 */
1342 ctx->start_ventry = NULL;
1343 ctx->stop_ventry = ventry;
84350051 1344 return err;
c9c9af91
JP
1345 } else if (*credits < 0) {
1346 /* We are out of credits, the rest of the ventries
6f9579d4
JP
1347 * will be migrated later. Save the ventry
1348 * which we ended with.
c9c9af91 1349 */
6f9579d4 1350 ctx->start_ventry = ventry;
c9c9af91 1351 return 0;
e5e7962e
JP
1352 }
1353 }
844f01da 1354
6f9579d4 1355 mlxsw_sp_acl_tcam_vchunk_migrate_end(mlxsw_sp, vchunk, ctx);
e5e7962e 1356 return 0;
e5e7962e
JP
1357}
1358
1359static int
1360mlxsw_sp_acl_tcam_vchunk_migrate_all(struct mlxsw_sp *mlxsw_sp,
220f4fba 1361 struct mlxsw_sp_acl_tcam_vregion *vregion,
c9c9af91
JP
1362 struct mlxsw_sp_acl_tcam_rehash_ctx *ctx,
1363 int *credits)
e5e7962e
JP
1364{
1365 struct mlxsw_sp_acl_tcam_vchunk *vchunk;
1366 int err;
1367
6f9579d4
JP
1368 /* If the migration got interrupted, we have the vchunk
1369 * we are working on stored in context.
1370 */
1371 if (ctx->current_vchunk)
1372 vchunk = ctx->current_vchunk;
1373 else
1374 vchunk = list_first_entry(&vregion->vchunk_list,
1375 typeof(*vchunk), list);
1376
1377 list_for_each_entry_from(vchunk, &vregion->vchunk_list, list) {
e5e7962e 1378 err = mlxsw_sp_acl_tcam_vchunk_migrate_one(mlxsw_sp, vchunk,
a86838e4 1379 vregion->region,
c9c9af91
JP
1380 ctx, credits);
1381 if (err || *credits < 0)
84350051 1382 return err;
e5e7962e
JP
1383 }
1384 return 0;
e5e7962e
JP
1385}
1386
1387static int
1388mlxsw_sp_acl_tcam_vregion_migrate(struct mlxsw_sp *mlxsw_sp,
1389 struct mlxsw_sp_acl_tcam_vregion *vregion,
c9c9af91
JP
1390 struct mlxsw_sp_acl_tcam_rehash_ctx *ctx,
1391 int *credits)
e5e7962e 1392{
84350051 1393 int err, err2;
e5e7962e 1394
3985de72 1395 trace_mlxsw_sp_acl_tcam_vregion_migrate(mlxsw_sp, vregion);
1263a9ab 1396 mutex_lock(&vregion->lock);
c9c9af91
JP
1397 err = mlxsw_sp_acl_tcam_vchunk_migrate_all(mlxsw_sp, vregion,
1398 ctx, credits);
84350051
JP
1399 if (err) {
1400 /* In case migration was not successful, we need to swap
1401 * so the original region pointer is assigned again
1402 * to vregion->region.
1403 */
1404 swap(vregion->region, vregion->region2);
6f9579d4 1405 ctx->current_vchunk = NULL;
84350051 1406 ctx->this_is_rollback = true;
c9c9af91
JP
1407 err2 = mlxsw_sp_acl_tcam_vchunk_migrate_all(mlxsw_sp, vregion,
1408 ctx, credits);
f3d4ef1a 1409 if (err2) {
a4e76ba6
JP
1410 trace_mlxsw_sp_acl_tcam_vregion_rehash_rollback_failed(mlxsw_sp,
1411 vregion);
f3d4ef1a 1412 dev_err(mlxsw_sp->bus_info->dev, "Failed to rollback during vregion migration fail\n");
7c33c72b 1413 /* Let the rollback to be continued later on. */
f3d4ef1a 1414 }
84350051 1415 }
a9550d0f 1416 mutex_unlock(&vregion->lock);
6375da3d 1417 trace_mlxsw_sp_acl_tcam_vregion_migrate_end(mlxsw_sp, vregion);
e5e7962e
JP
1418 return err;
1419}
1420
c9c9af91
JP
1421static bool
1422mlxsw_sp_acl_tcam_vregion_rehash_in_progress(const struct mlxsw_sp_acl_tcam_rehash_ctx *ctx)
1423{
1424 return ctx->hints_priv;
1425}
1426
e5e7962e 1427static int
1667f766
JP
1428mlxsw_sp_acl_tcam_vregion_rehash_start(struct mlxsw_sp *mlxsw_sp,
1429 struct mlxsw_sp_acl_tcam_vregion *vregion,
1430 struct mlxsw_sp_acl_tcam_rehash_ctx *ctx)
e5e7962e
JP
1431{
1432 const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
a9550d0f 1433 unsigned int priority = mlxsw_sp_acl_tcam_vregion_prio(vregion);
a86838e4 1434 struct mlxsw_sp_acl_tcam_region *new_region;
e5e7962e 1435 void *hints_priv;
a9550d0f 1436 int err;
e5e7962e 1437
3985de72 1438 trace_mlxsw_sp_acl_tcam_vregion_rehash(mlxsw_sp, vregion);
e5e7962e
JP
1439
1440 hints_priv = ops->region_rehash_hints_get(vregion->region->priv);
1667f766
JP
1441 if (IS_ERR(hints_priv))
1442 return PTR_ERR(hints_priv);
1443
a86838e4
JP
1444 new_region = mlxsw_sp_acl_tcam_region_create(mlxsw_sp, vregion->tcam,
1445 vregion, hints_priv);
1446 if (IS_ERR(new_region)) {
1447 err = PTR_ERR(new_region);
a9550d0f
JP
1448 goto err_region_create;
1449 }
1450
a86838e4
JP
1451 /* vregion->region contains the pointer to the new region
1452 * we are going to migrate to.
1453 */
1454 vregion->region2 = vregion->region;
1455 vregion->region = new_region;
a9550d0f 1456 err = mlxsw_sp_acl_tcam_group_region_attach(mlxsw_sp,
a86838e4
JP
1457 vregion->region2->group,
1458 new_region, priority,
1459 vregion->region2);
a9550d0f
JP
1460 if (err)
1461 goto err_group_region_attach;
1462
1667f766 1463 ctx->hints_priv = hints_priv;
220f4fba 1464 ctx->this_is_rollback = false;
1667f766
JP
1465
1466 return 0;
a9550d0f
JP
1467
1468err_group_region_attach:
a86838e4 1469 vregion->region = vregion->region2;
a9550d0f 1470 vregion->region2 = NULL;
a86838e4 1471 mlxsw_sp_acl_tcam_region_destroy(mlxsw_sp, new_region);
a9550d0f
JP
1472err_region_create:
1473 ops->region_rehash_hints_put(hints_priv);
1474 return err;
1667f766
JP
1475}
1476
1477static void
1478mlxsw_sp_acl_tcam_vregion_rehash_end(struct mlxsw_sp *mlxsw_sp,
1479 struct mlxsw_sp_acl_tcam_vregion *vregion,
1480 struct mlxsw_sp_acl_tcam_rehash_ctx *ctx)
1481{
a9550d0f 1482 struct mlxsw_sp_acl_tcam_region *unused_region = vregion->region2;
1667f766
JP
1483 const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
1484
7c33c72b
JP
1485 vregion->region2 = NULL;
1486 mlxsw_sp_acl_tcam_group_region_detach(mlxsw_sp, unused_region);
1487 mlxsw_sp_acl_tcam_region_destroy(mlxsw_sp, unused_region);
1667f766
JP
1488 ops->region_rehash_hints_put(ctx->hints_priv);
1489 ctx->hints_priv = NULL;
1490}
1491
b2c091ce 1492static void
1667f766 1493mlxsw_sp_acl_tcam_vregion_rehash(struct mlxsw_sp *mlxsw_sp,
c9c9af91
JP
1494 struct mlxsw_sp_acl_tcam_vregion *vregion,
1495 int *credits)
1667f766
JP
1496{
1497 struct mlxsw_sp_acl_tcam_rehash_ctx *ctx = &vregion->rehash.ctx;
1498 int err;
1499
c9c9af91
JP
1500 /* Check if the previous rehash work was interrupted
1501 * which means we have to continue it now.
1502 * If not, start a new rehash.
1503 */
1504 if (!mlxsw_sp_acl_tcam_vregion_rehash_in_progress(ctx)) {
1505 err = mlxsw_sp_acl_tcam_vregion_rehash_start(mlxsw_sp,
1506 vregion, ctx);
1507 if (err) {
1508 if (err != -EAGAIN)
1509 dev_err(mlxsw_sp->bus_info->dev, "Failed get rehash hints\n");
b2c091ce 1510 return;
c9c9af91 1511 }
e5e7962e
JP
1512 }
1513
c9c9af91
JP
1514 err = mlxsw_sp_acl_tcam_vregion_migrate(mlxsw_sp, vregion,
1515 ctx, credits);
e5e7962e
JP
1516 if (err) {
1517 dev_err(mlxsw_sp->bus_info->dev, "Failed to migrate vregion\n");
e5e7962e
JP
1518 }
1519
c9c9af91
JP
1520 if (*credits >= 0)
1521 mlxsw_sp_acl_tcam_vregion_rehash_end(mlxsw_sp, vregion, ctx);
e5e7962e
JP
1522}
1523
22a67766
JP
1524static const enum mlxsw_afk_element mlxsw_sp_acl_tcam_pattern_ipv4[] = {
1525 MLXSW_AFK_ELEMENT_SRC_SYS_PORT,
c43ea06d
JP
1526 MLXSW_AFK_ELEMENT_DMAC_32_47,
1527 MLXSW_AFK_ELEMENT_DMAC_0_31,
1528 MLXSW_AFK_ELEMENT_SMAC_32_47,
1529 MLXSW_AFK_ELEMENT_SMAC_0_31,
22a67766
JP
1530 MLXSW_AFK_ELEMENT_ETHERTYPE,
1531 MLXSW_AFK_ELEMENT_IP_PROTO,
c43ea06d
JP
1532 MLXSW_AFK_ELEMENT_SRC_IP_0_31,
1533 MLXSW_AFK_ELEMENT_DST_IP_0_31,
22a67766
JP
1534 MLXSW_AFK_ELEMENT_DST_L4_PORT,
1535 MLXSW_AFK_ELEMENT_SRC_L4_PORT,
9caab08a
PM
1536 MLXSW_AFK_ELEMENT_VID,
1537 MLXSW_AFK_ELEMENT_PCP,
8a41d845 1538 MLXSW_AFK_ELEMENT_TCP_FLAGS,
046759a6 1539 MLXSW_AFK_ELEMENT_IP_TTL_,
abac7b01
OG
1540 MLXSW_AFK_ELEMENT_IP_ECN,
1541 MLXSW_AFK_ELEMENT_IP_DSCP,
22a67766
JP
1542};
1543
1544static const enum mlxsw_afk_element mlxsw_sp_acl_tcam_pattern_ipv6[] = {
1545 MLXSW_AFK_ELEMENT_ETHERTYPE,
1546 MLXSW_AFK_ELEMENT_IP_PROTO,
c43ea06d
JP
1547 MLXSW_AFK_ELEMENT_SRC_IP_96_127,
1548 MLXSW_AFK_ELEMENT_SRC_IP_64_95,
1549 MLXSW_AFK_ELEMENT_SRC_IP_32_63,
1550 MLXSW_AFK_ELEMENT_SRC_IP_0_31,
1551 MLXSW_AFK_ELEMENT_DST_IP_96_127,
1552 MLXSW_AFK_ELEMENT_DST_IP_64_95,
1553 MLXSW_AFK_ELEMENT_DST_IP_32_63,
1554 MLXSW_AFK_ELEMENT_DST_IP_0_31,
22a67766
JP
1555 MLXSW_AFK_ELEMENT_DST_L4_PORT,
1556 MLXSW_AFK_ELEMENT_SRC_L4_PORT,
1557};
1558
1559static const struct mlxsw_sp_acl_tcam_pattern mlxsw_sp_acl_tcam_patterns[] = {
1560 {
1561 .elements = mlxsw_sp_acl_tcam_pattern_ipv4,
1562 .elements_count = ARRAY_SIZE(mlxsw_sp_acl_tcam_pattern_ipv4),
1563 },
1564 {
1565 .elements = mlxsw_sp_acl_tcam_pattern_ipv6,
1566 .elements_count = ARRAY_SIZE(mlxsw_sp_acl_tcam_pattern_ipv6),
1567 },
1568};
1569
1570#define MLXSW_SP_ACL_TCAM_PATTERNS_COUNT \
1571 ARRAY_SIZE(mlxsw_sp_acl_tcam_patterns)
1572
1573struct mlxsw_sp_acl_tcam_flower_ruleset {
2802aadf 1574 struct mlxsw_sp_acl_tcam_vgroup vgroup;
22a67766
JP
1575};
1576
1577struct mlxsw_sp_acl_tcam_flower_rule {
c4c2dc54 1578 struct mlxsw_sp_acl_tcam_ventry ventry;
22a67766
JP
1579};
1580
1581static int
1582mlxsw_sp_acl_tcam_flower_ruleset_add(struct mlxsw_sp *mlxsw_sp,
bab5c1cf 1583 struct mlxsw_sp_acl_tcam *tcam,
e2f2a1fd
JP
1584 void *ruleset_priv,
1585 struct mlxsw_afk_element_usage *tmplt_elusage)
22a67766
JP
1586{
1587 struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
22a67766 1588
2802aadf
JP
1589 return mlxsw_sp_acl_tcam_vgroup_add(mlxsw_sp, tcam, &ruleset->vgroup,
1590 mlxsw_sp_acl_tcam_patterns,
1591 MLXSW_SP_ACL_TCAM_PATTERNS_COUNT,
6b861682 1592 tmplt_elusage, true);
22a67766
JP
1593}
1594
1595static void
1596mlxsw_sp_acl_tcam_flower_ruleset_del(struct mlxsw_sp *mlxsw_sp,
1597 void *ruleset_priv)
1598{
1599 struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
1600
2802aadf 1601 mlxsw_sp_acl_tcam_vgroup_del(&ruleset->vgroup);
22a67766
JP
1602}
1603
1604static int
1605mlxsw_sp_acl_tcam_flower_ruleset_bind(struct mlxsw_sp *mlxsw_sp,
1606 void *ruleset_priv,
4b23258d
JP
1607 struct mlxsw_sp_port *mlxsw_sp_port,
1608 bool ingress)
22a67766
JP
1609{
1610 struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
1611
2802aadf 1612 return mlxsw_sp_acl_tcam_group_bind(mlxsw_sp, &ruleset->vgroup.group,
4b23258d 1613 mlxsw_sp_port, ingress);
22a67766
JP
1614}
1615
1616static void
1617mlxsw_sp_acl_tcam_flower_ruleset_unbind(struct mlxsw_sp *mlxsw_sp,
02caf499 1618 void *ruleset_priv,
4b23258d
JP
1619 struct mlxsw_sp_port *mlxsw_sp_port,
1620 bool ingress)
22a67766
JP
1621{
1622 struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
1623
2802aadf 1624 mlxsw_sp_acl_tcam_group_unbind(mlxsw_sp, &ruleset->vgroup.group,
4b23258d 1625 mlxsw_sp_port, ingress);
22a67766
JP
1626}
1627
0ade3b64
JP
1628static u16
1629mlxsw_sp_acl_tcam_flower_ruleset_group_id(void *ruleset_priv)
1630{
1631 struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
1632
2802aadf 1633 return mlxsw_sp_acl_tcam_group_id(&ruleset->vgroup.group);
0ade3b64
JP
1634}
1635
22a67766
JP
1636static int
1637mlxsw_sp_acl_tcam_flower_rule_add(struct mlxsw_sp *mlxsw_sp,
1638 void *ruleset_priv, void *rule_priv,
1639 struct mlxsw_sp_acl_rule_info *rulei)
1640{
1641 struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
1642 struct mlxsw_sp_acl_tcam_flower_rule *rule = rule_priv;
1643
2802aadf 1644 return mlxsw_sp_acl_tcam_ventry_add(mlxsw_sp, &ruleset->vgroup,
c4c2dc54 1645 &rule->ventry, rulei);
22a67766
JP
1646}
1647
1648static void
1649mlxsw_sp_acl_tcam_flower_rule_del(struct mlxsw_sp *mlxsw_sp, void *rule_priv)
1650{
1651 struct mlxsw_sp_acl_tcam_flower_rule *rule = rule_priv;
1652
c4c2dc54 1653 mlxsw_sp_acl_tcam_ventry_del(mlxsw_sp, &rule->ventry);
22a67766
JP
1654}
1655
2507a64c
ND
1656static int
1657mlxsw_sp_acl_tcam_flower_rule_action_replace(struct mlxsw_sp *mlxsw_sp,
2507a64c
ND
1658 void *rule_priv,
1659 struct mlxsw_sp_acl_rule_info *rulei)
1660{
1661 return -EOPNOTSUPP;
1662}
1663
7fd056c2
AS
1664static int
1665mlxsw_sp_acl_tcam_flower_rule_activity_get(struct mlxsw_sp *mlxsw_sp,
1666 void *rule_priv, bool *activity)
1667{
1668 struct mlxsw_sp_acl_tcam_flower_rule *rule = rule_priv;
1669
c4c2dc54
JP
1670 return mlxsw_sp_acl_tcam_ventry_activity_get(mlxsw_sp, &rule->ventry,
1671 activity);
7fd056c2
AS
1672}
1673
22a67766
JP
1674static const struct mlxsw_sp_acl_profile_ops mlxsw_sp_acl_tcam_flower_ops = {
1675 .ruleset_priv_size = sizeof(struct mlxsw_sp_acl_tcam_flower_ruleset),
1676 .ruleset_add = mlxsw_sp_acl_tcam_flower_ruleset_add,
1677 .ruleset_del = mlxsw_sp_acl_tcam_flower_ruleset_del,
1678 .ruleset_bind = mlxsw_sp_acl_tcam_flower_ruleset_bind,
1679 .ruleset_unbind = mlxsw_sp_acl_tcam_flower_ruleset_unbind,
0ade3b64 1680 .ruleset_group_id = mlxsw_sp_acl_tcam_flower_ruleset_group_id,
c4c2dc54 1681 .rule_priv_size = sizeof(struct mlxsw_sp_acl_tcam_flower_rule),
22a67766
JP
1682 .rule_add = mlxsw_sp_acl_tcam_flower_rule_add,
1683 .rule_del = mlxsw_sp_acl_tcam_flower_rule_del,
2507a64c 1684 .rule_action_replace = mlxsw_sp_acl_tcam_flower_rule_action_replace,
7fd056c2 1685 .rule_activity_get = mlxsw_sp_acl_tcam_flower_rule_activity_get,
22a67766
JP
1686};
1687
038418ee 1688struct mlxsw_sp_acl_tcam_mr_ruleset {
b2d6b4d2 1689 struct mlxsw_sp_acl_tcam_vchunk *vchunk;
2802aadf 1690 struct mlxsw_sp_acl_tcam_vgroup vgroup;
038418ee
JP
1691};
1692
1693struct mlxsw_sp_acl_tcam_mr_rule {
c4c2dc54 1694 struct mlxsw_sp_acl_tcam_ventry ventry;
038418ee
JP
1695};
1696
1a29d293
ND
1697static int
1698mlxsw_sp_acl_tcam_mr_ruleset_add(struct mlxsw_sp *mlxsw_sp,
1699 struct mlxsw_sp_acl_tcam *tcam,
1700 void *ruleset_priv,
1701 struct mlxsw_afk_element_usage *tmplt_elusage)
1702{
1703 struct mlxsw_sp_acl_tcam_mr_ruleset *ruleset = ruleset_priv;
1704 int err;
1705
2802aadf
JP
1706 err = mlxsw_sp_acl_tcam_vgroup_add(mlxsw_sp, tcam, &ruleset->vgroup,
1707 mlxsw_sp_acl_tcam_patterns,
1708 MLXSW_SP_ACL_TCAM_PATTERNS_COUNT,
6b861682 1709 tmplt_elusage, false);
1a29d293
ND
1710 if (err)
1711 return err;
1712
1713 /* For most of the TCAM clients it would make sense to take a tcam chunk
1714 * only when the first rule is written. This is not the case for
1715 * multicast router as it is required to bind the multicast router to a
1716 * specific ACL Group ID which must exist in HW before multicast router
1717 * is initialized.
1718 */
b2d6b4d2 1719 ruleset->vchunk = mlxsw_sp_acl_tcam_vchunk_get(mlxsw_sp,
2802aadf 1720 &ruleset->vgroup, 1,
b2d6b4d2
JP
1721 tmplt_elusage);
1722 if (IS_ERR(ruleset->vchunk)) {
1723 err = PTR_ERR(ruleset->vchunk);
1a29d293
ND
1724 goto err_chunk_get;
1725 }
1726
1727 return 0;
1728
1729err_chunk_get:
2802aadf 1730 mlxsw_sp_acl_tcam_vgroup_del(&ruleset->vgroup);
1a29d293
ND
1731 return err;
1732}
1733
1734static void
1735mlxsw_sp_acl_tcam_mr_ruleset_del(struct mlxsw_sp *mlxsw_sp, void *ruleset_priv)
1736{
1737 struct mlxsw_sp_acl_tcam_mr_ruleset *ruleset = ruleset_priv;
1738
b2d6b4d2 1739 mlxsw_sp_acl_tcam_vchunk_put(mlxsw_sp, ruleset->vchunk);
2802aadf 1740 mlxsw_sp_acl_tcam_vgroup_del(&ruleset->vgroup);
1a29d293
ND
1741}
1742
1743static int
1744mlxsw_sp_acl_tcam_mr_ruleset_bind(struct mlxsw_sp *mlxsw_sp, void *ruleset_priv,
1745 struct mlxsw_sp_port *mlxsw_sp_port,
1746 bool ingress)
1747{
1748 /* Binding is done when initializing multicast router */
1749 return 0;
1750}
1751
1752static void
1753mlxsw_sp_acl_tcam_mr_ruleset_unbind(struct mlxsw_sp *mlxsw_sp,
1754 void *ruleset_priv,
1755 struct mlxsw_sp_port *mlxsw_sp_port,
1756 bool ingress)
1757{
1758}
1759
1760static u16
1761mlxsw_sp_acl_tcam_mr_ruleset_group_id(void *ruleset_priv)
1762{
1763 struct mlxsw_sp_acl_tcam_mr_ruleset *ruleset = ruleset_priv;
1764
2802aadf 1765 return mlxsw_sp_acl_tcam_group_id(&ruleset->vgroup.group);
1a29d293
ND
1766}
1767
1a29d293
ND
1768static int
1769mlxsw_sp_acl_tcam_mr_rule_add(struct mlxsw_sp *mlxsw_sp, void *ruleset_priv,
1770 void *rule_priv,
1771 struct mlxsw_sp_acl_rule_info *rulei)
1772{
1773 struct mlxsw_sp_acl_tcam_mr_ruleset *ruleset = ruleset_priv;
1774 struct mlxsw_sp_acl_tcam_mr_rule *rule = rule_priv;
1775
2802aadf 1776 return mlxsw_sp_acl_tcam_ventry_add(mlxsw_sp, &ruleset->vgroup,
c4c2dc54 1777 &rule->ventry, rulei);
1a29d293
ND
1778}
1779
1780static void
1781mlxsw_sp_acl_tcam_mr_rule_del(struct mlxsw_sp *mlxsw_sp, void *rule_priv)
1782{
1783 struct mlxsw_sp_acl_tcam_mr_rule *rule = rule_priv;
1784
c4c2dc54 1785 mlxsw_sp_acl_tcam_ventry_del(mlxsw_sp, &rule->ventry);
1a29d293
ND
1786}
1787
2507a64c
ND
1788static int
1789mlxsw_sp_acl_tcam_mr_rule_action_replace(struct mlxsw_sp *mlxsw_sp,
42d704e0 1790 void *rule_priv,
2507a64c
ND
1791 struct mlxsw_sp_acl_rule_info *rulei)
1792{
2507a64c
ND
1793 struct mlxsw_sp_acl_tcam_mr_rule *rule = rule_priv;
1794
c4c2dc54
JP
1795 return mlxsw_sp_acl_tcam_ventry_action_replace(mlxsw_sp, &rule->ventry,
1796 rulei);
2507a64c
ND
1797}
1798
1a29d293
ND
1799static int
1800mlxsw_sp_acl_tcam_mr_rule_activity_get(struct mlxsw_sp *mlxsw_sp,
1801 void *rule_priv, bool *activity)
1802{
1803 struct mlxsw_sp_acl_tcam_mr_rule *rule = rule_priv;
1804
c4c2dc54
JP
1805 return mlxsw_sp_acl_tcam_ventry_activity_get(mlxsw_sp, &rule->ventry,
1806 activity);
1a29d293
ND
1807}
1808
1809static const struct mlxsw_sp_acl_profile_ops mlxsw_sp_acl_tcam_mr_ops = {
1810 .ruleset_priv_size = sizeof(struct mlxsw_sp_acl_tcam_mr_ruleset),
1811 .ruleset_add = mlxsw_sp_acl_tcam_mr_ruleset_add,
1812 .ruleset_del = mlxsw_sp_acl_tcam_mr_ruleset_del,
1813 .ruleset_bind = mlxsw_sp_acl_tcam_mr_ruleset_bind,
1814 .ruleset_unbind = mlxsw_sp_acl_tcam_mr_ruleset_unbind,
1815 .ruleset_group_id = mlxsw_sp_acl_tcam_mr_ruleset_group_id,
c4c2dc54 1816 .rule_priv_size = sizeof(struct mlxsw_sp_acl_tcam_mr_rule),
1a29d293
ND
1817 .rule_add = mlxsw_sp_acl_tcam_mr_rule_add,
1818 .rule_del = mlxsw_sp_acl_tcam_mr_rule_del,
2507a64c 1819 .rule_action_replace = mlxsw_sp_acl_tcam_mr_rule_action_replace,
1a29d293
ND
1820 .rule_activity_get = mlxsw_sp_acl_tcam_mr_rule_activity_get,
1821};
1822
22a67766
JP
1823static const struct mlxsw_sp_acl_profile_ops *
1824mlxsw_sp_acl_tcam_profile_ops_arr[] = {
1825 [MLXSW_SP_ACL_PROFILE_FLOWER] = &mlxsw_sp_acl_tcam_flower_ops,
1a29d293 1826 [MLXSW_SP_ACL_PROFILE_MR] = &mlxsw_sp_acl_tcam_mr_ops,
22a67766
JP
1827};
1828
bab5c1cf 1829const struct mlxsw_sp_acl_profile_ops *
22a67766
JP
1830mlxsw_sp_acl_tcam_profile_ops(struct mlxsw_sp *mlxsw_sp,
1831 enum mlxsw_sp_acl_profile profile)
1832{
1833 const struct mlxsw_sp_acl_profile_ops *ops;
1834
1835 if (WARN_ON(profile >= ARRAY_SIZE(mlxsw_sp_acl_tcam_profile_ops_arr)))
1836 return NULL;
1837 ops = mlxsw_sp_acl_tcam_profile_ops_arr[profile];
1838 if (WARN_ON(!ops))
1839 return NULL;
1840 return ops;
1841}