Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
0002 /* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
0003 
0004 #include <linux/kernel.h>
0005 #include <linux/slab.h>
0006 #include <linux/errno.h>
0007 #include <linux/bitops.h>
0008 #include <linux/list.h>
0009 #include <linux/rhashtable.h>
0010 #include <linux/netdevice.h>
0011 #include <linux/mutex.h>
0012 #include <trace/events/mlxsw.h>
0013 
0014 #include "reg.h"
0015 #include "core.h"
0016 #include "resources.h"
0017 #include "spectrum.h"
0018 #include "spectrum_acl_tcam.h"
0019 #include "core_acl_flex_keys.h"
0020 
0021 size_t mlxsw_sp_acl_tcam_priv_size(struct mlxsw_sp *mlxsw_sp)
0022 {
0023     const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
0024 
0025     return ops->priv_size;
0026 }
0027 
0028 #define MLXSW_SP_ACL_TCAM_VREGION_REHASH_INTRVL_DFLT 5000 /* ms */
0029 #define MLXSW_SP_ACL_TCAM_VREGION_REHASH_INTRVL_MIN 3000 /* ms */
0030 #define MLXSW_SP_ACL_TCAM_VREGION_REHASH_CREDITS 100 /* number of entries */
0031 
0032 int mlxsw_sp_acl_tcam_init(struct mlxsw_sp *mlxsw_sp,
0033                struct mlxsw_sp_acl_tcam *tcam)
0034 {
0035     const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
0036     u64 max_tcam_regions;
0037     u64 max_regions;
0038     u64 max_groups;
0039     int err;
0040 
0041     mutex_init(&tcam->lock);
0042     tcam->vregion_rehash_intrvl =
0043             MLXSW_SP_ACL_TCAM_VREGION_REHASH_INTRVL_DFLT;
0044     INIT_LIST_HEAD(&tcam->vregion_list);
0045 
0046     max_tcam_regions = MLXSW_CORE_RES_GET(mlxsw_sp->core,
0047                           ACL_MAX_TCAM_REGIONS);
0048     max_regions = MLXSW_CORE_RES_GET(mlxsw_sp->core, ACL_MAX_REGIONS);
0049 
0050     /* Use 1:1 mapping between ACL region and TCAM region */
0051     if (max_tcam_regions < max_regions)
0052         max_regions = max_tcam_regions;
0053 
0054     tcam->used_regions = bitmap_zalloc(max_regions, GFP_KERNEL);
0055     if (!tcam->used_regions)
0056         return -ENOMEM;
0057     tcam->max_regions = max_regions;
0058 
0059     max_groups = MLXSW_CORE_RES_GET(mlxsw_sp->core, ACL_MAX_GROUPS);
0060     tcam->used_groups = bitmap_zalloc(max_groups, GFP_KERNEL);
0061     if (!tcam->used_groups) {
0062         err = -ENOMEM;
0063         goto err_alloc_used_groups;
0064     }
0065     tcam->max_groups = max_groups;
0066     tcam->max_group_size = MLXSW_CORE_RES_GET(mlxsw_sp->core,
0067                          ACL_MAX_GROUP_SIZE);
0068 
0069     err = ops->init(mlxsw_sp, tcam->priv, tcam);
0070     if (err)
0071         goto err_tcam_init;
0072 
0073     return 0;
0074 
0075 err_tcam_init:
0076     bitmap_free(tcam->used_groups);
0077 err_alloc_used_groups:
0078     bitmap_free(tcam->used_regions);
0079     return err;
0080 }
0081 
0082 void mlxsw_sp_acl_tcam_fini(struct mlxsw_sp *mlxsw_sp,
0083                 struct mlxsw_sp_acl_tcam *tcam)
0084 {
0085     const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
0086 
0087     mutex_destroy(&tcam->lock);
0088     ops->fini(mlxsw_sp, tcam->priv);
0089     bitmap_free(tcam->used_groups);
0090     bitmap_free(tcam->used_regions);
0091 }
0092 
0093 int mlxsw_sp_acl_tcam_priority_get(struct mlxsw_sp *mlxsw_sp,
0094                    struct mlxsw_sp_acl_rule_info *rulei,
0095                    u32 *priority, bool fillup_priority)
0096 {
0097     u64 max_priority;
0098 
0099     if (!fillup_priority) {
0100         *priority = 0;
0101         return 0;
0102     }
0103 
0104     if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, KVD_SIZE))
0105         return -EIO;
0106 
0107     /* Priority range is 1..cap_kvd_size-1. */
0108     max_priority = MLXSW_CORE_RES_GET(mlxsw_sp->core, KVD_SIZE) - 1;
0109     if (rulei->priority >= max_priority)
0110         return -EINVAL;
0111 
0112     /* Unlike in TC, in HW, higher number means higher priority. */
0113     *priority = max_priority - rulei->priority;
0114     return 0;
0115 }
0116 
0117 static int mlxsw_sp_acl_tcam_region_id_get(struct mlxsw_sp_acl_tcam *tcam,
0118                        u16 *p_id)
0119 {
0120     u16 id;
0121 
0122     id = find_first_zero_bit(tcam->used_regions, tcam->max_regions);
0123     if (id < tcam->max_regions) {
0124         __set_bit(id, tcam->used_regions);
0125         *p_id = id;
0126         return 0;
0127     }
0128     return -ENOBUFS;
0129 }
0130 
0131 static void mlxsw_sp_acl_tcam_region_id_put(struct mlxsw_sp_acl_tcam *tcam,
0132                         u16 id)
0133 {
0134     __clear_bit(id, tcam->used_regions);
0135 }
0136 
0137 static int mlxsw_sp_acl_tcam_group_id_get(struct mlxsw_sp_acl_tcam *tcam,
0138                       u16 *p_id)
0139 {
0140     u16 id;
0141 
0142     id = find_first_zero_bit(tcam->used_groups, tcam->max_groups);
0143     if (id < tcam->max_groups) {
0144         __set_bit(id, tcam->used_groups);
0145         *p_id = id;
0146         return 0;
0147     }
0148     return -ENOBUFS;
0149 }
0150 
0151 static void mlxsw_sp_acl_tcam_group_id_put(struct mlxsw_sp_acl_tcam *tcam,
0152                        u16 id)
0153 {
0154     __clear_bit(id, tcam->used_groups);
0155 }
0156 
0157 struct mlxsw_sp_acl_tcam_pattern {
0158     const enum mlxsw_afk_element *elements;
0159     unsigned int elements_count;
0160 };
0161 
0162 struct mlxsw_sp_acl_tcam_group {
0163     struct mlxsw_sp_acl_tcam *tcam;
0164     u16 id;
0165     struct mutex lock; /* guards region list updates */
0166     struct list_head region_list;
0167     unsigned int region_count;
0168 };
0169 
0170 struct mlxsw_sp_acl_tcam_vgroup {
0171     struct mlxsw_sp_acl_tcam_group group;
0172     struct list_head vregion_list;
0173     struct rhashtable vchunk_ht;
0174     const struct mlxsw_sp_acl_tcam_pattern *patterns;
0175     unsigned int patterns_count;
0176     bool tmplt_elusage_set;
0177     struct mlxsw_afk_element_usage tmplt_elusage;
0178     bool vregion_rehash_enabled;
0179     unsigned int *p_min_prio;
0180     unsigned int *p_max_prio;
0181 };
0182 
0183 struct mlxsw_sp_acl_tcam_rehash_ctx {
0184     void *hints_priv;
0185     bool this_is_rollback;
0186     struct mlxsw_sp_acl_tcam_vchunk *current_vchunk; /* vchunk being
0187                               * currently migrated.
0188                               */
0189     struct mlxsw_sp_acl_tcam_ventry *start_ventry; /* ventry to start
0190                             * migration from in
0191                             * a vchunk being
0192                             * currently migrated.
0193                             */
0194     struct mlxsw_sp_acl_tcam_ventry *stop_ventry; /* ventry to stop
0195                                * migration at
0196                                * a vchunk being
0197                                * currently migrated.
0198                                */
0199 };
0200 
0201 struct mlxsw_sp_acl_tcam_vregion {
0202     struct mutex lock; /* Protects consistency of region, region2 pointers
0203                 * and vchunk_list.
0204                 */
0205     struct mlxsw_sp_acl_tcam_region *region;
0206     struct mlxsw_sp_acl_tcam_region *region2; /* Used during migration */
0207     struct list_head list; /* Member of a TCAM group */
0208     struct list_head tlist; /* Member of a TCAM */
0209     struct list_head vchunk_list; /* List of vchunks under this vregion */
0210     struct mlxsw_afk_key_info *key_info;
0211     struct mlxsw_sp_acl_tcam *tcam;
0212     struct mlxsw_sp_acl_tcam_vgroup *vgroup;
0213     struct {
0214         struct delayed_work dw;
0215         struct mlxsw_sp_acl_tcam_rehash_ctx ctx;
0216     } rehash;
0217     struct mlxsw_sp *mlxsw_sp;
0218     unsigned int ref_count;
0219 };
0220 
0221 struct mlxsw_sp_acl_tcam_vchunk;
0222 
0223 struct mlxsw_sp_acl_tcam_chunk {
0224     struct mlxsw_sp_acl_tcam_vchunk *vchunk;
0225     struct mlxsw_sp_acl_tcam_region *region;
0226     unsigned long priv[];
0227     /* priv has to be always the last item */
0228 };
0229 
0230 struct mlxsw_sp_acl_tcam_vchunk {
0231     struct mlxsw_sp_acl_tcam_chunk *chunk;
0232     struct mlxsw_sp_acl_tcam_chunk *chunk2; /* Used during migration */
0233     struct list_head list; /* Member of a TCAM vregion */
0234     struct rhash_head ht_node; /* Member of a chunk HT */
0235     struct list_head ventry_list;
0236     unsigned int priority; /* Priority within the vregion and group */
0237     struct mlxsw_sp_acl_tcam_vgroup *vgroup;
0238     struct mlxsw_sp_acl_tcam_vregion *vregion;
0239     unsigned int ref_count;
0240 };
0241 
0242 struct mlxsw_sp_acl_tcam_entry {
0243     struct mlxsw_sp_acl_tcam_ventry *ventry;
0244     struct mlxsw_sp_acl_tcam_chunk *chunk;
0245     unsigned long priv[];
0246     /* priv has to be always the last item */
0247 };
0248 
0249 struct mlxsw_sp_acl_tcam_ventry {
0250     struct mlxsw_sp_acl_tcam_entry *entry;
0251     struct list_head list; /* Member of a TCAM vchunk */
0252     struct mlxsw_sp_acl_tcam_vchunk *vchunk;
0253     struct mlxsw_sp_acl_rule_info *rulei;
0254 };
0255 
0256 static const struct rhashtable_params mlxsw_sp_acl_tcam_vchunk_ht_params = {
0257     .key_len = sizeof(unsigned int),
0258     .key_offset = offsetof(struct mlxsw_sp_acl_tcam_vchunk, priority),
0259     .head_offset = offsetof(struct mlxsw_sp_acl_tcam_vchunk, ht_node),
0260     .automatic_shrinking = true,
0261 };
0262 
0263 static int mlxsw_sp_acl_tcam_group_update(struct mlxsw_sp *mlxsw_sp,
0264                       struct mlxsw_sp_acl_tcam_group *group)
0265 {
0266     struct mlxsw_sp_acl_tcam_region *region;
0267     char pagt_pl[MLXSW_REG_PAGT_LEN];
0268     int acl_index = 0;
0269 
0270     mlxsw_reg_pagt_pack(pagt_pl, group->id);
0271     list_for_each_entry(region, &group->region_list, list) {
0272         bool multi = false;
0273 
0274         /* Check if the next entry in the list has the same vregion. */
0275         if (region->list.next != &group->region_list &&
0276             list_next_entry(region, list)->vregion == region->vregion)
0277             multi = true;
0278         mlxsw_reg_pagt_acl_id_pack(pagt_pl, acl_index++,
0279                        region->id, multi);
0280     }
0281     mlxsw_reg_pagt_size_set(pagt_pl, acl_index);
0282     return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pagt), pagt_pl);
0283 }
0284 
0285 static int
0286 mlxsw_sp_acl_tcam_group_add(struct mlxsw_sp_acl_tcam *tcam,
0287                 struct mlxsw_sp_acl_tcam_group *group)
0288 {
0289     int err;
0290 
0291     group->tcam = tcam;
0292     INIT_LIST_HEAD(&group->region_list);
0293 
0294     err = mlxsw_sp_acl_tcam_group_id_get(tcam, &group->id);
0295     if (err)
0296         return err;
0297 
0298     mutex_init(&group->lock);
0299 
0300     return 0;
0301 }
0302 
0303 static void mlxsw_sp_acl_tcam_group_del(struct mlxsw_sp_acl_tcam_group *group)
0304 {
0305     struct mlxsw_sp_acl_tcam *tcam = group->tcam;
0306 
0307     mutex_destroy(&group->lock);
0308     mlxsw_sp_acl_tcam_group_id_put(tcam, group->id);
0309     WARN_ON(!list_empty(&group->region_list));
0310 }
0311 
0312 static int
0313 mlxsw_sp_acl_tcam_vgroup_add(struct mlxsw_sp *mlxsw_sp,
0314                  struct mlxsw_sp_acl_tcam *tcam,
0315                  struct mlxsw_sp_acl_tcam_vgroup *vgroup,
0316                  const struct mlxsw_sp_acl_tcam_pattern *patterns,
0317                  unsigned int patterns_count,
0318                  struct mlxsw_afk_element_usage *tmplt_elusage,
0319                  bool vregion_rehash_enabled,
0320                  unsigned int *p_min_prio,
0321                  unsigned int *p_max_prio)
0322 {
0323     int err;
0324 
0325     vgroup->patterns = patterns;
0326     vgroup->patterns_count = patterns_count;
0327     vgroup->vregion_rehash_enabled = vregion_rehash_enabled;
0328     vgroup->p_min_prio = p_min_prio;
0329     vgroup->p_max_prio = p_max_prio;
0330 
0331     if (tmplt_elusage) {
0332         vgroup->tmplt_elusage_set = true;
0333         memcpy(&vgroup->tmplt_elusage, tmplt_elusage,
0334                sizeof(vgroup->tmplt_elusage));
0335     }
0336     INIT_LIST_HEAD(&vgroup->vregion_list);
0337 
0338     err = mlxsw_sp_acl_tcam_group_add(tcam, &vgroup->group);
0339     if (err)
0340         return err;
0341 
0342     err = rhashtable_init(&vgroup->vchunk_ht,
0343                   &mlxsw_sp_acl_tcam_vchunk_ht_params);
0344     if (err)
0345         goto err_rhashtable_init;
0346 
0347     return 0;
0348 
0349 err_rhashtable_init:
0350     mlxsw_sp_acl_tcam_group_del(&vgroup->group);
0351     return err;
0352 }
0353 
0354 static void
0355 mlxsw_sp_acl_tcam_vgroup_del(struct mlxsw_sp_acl_tcam_vgroup *vgroup)
0356 {
0357     rhashtable_destroy(&vgroup->vchunk_ht);
0358     mlxsw_sp_acl_tcam_group_del(&vgroup->group);
0359     WARN_ON(!list_empty(&vgroup->vregion_list));
0360 }
0361 
0362 static int
0363 mlxsw_sp_acl_tcam_group_bind(struct mlxsw_sp *mlxsw_sp,
0364                  struct mlxsw_sp_acl_tcam_group *group,
0365                  struct mlxsw_sp_port *mlxsw_sp_port,
0366                  bool ingress)
0367 {
0368     char ppbt_pl[MLXSW_REG_PPBT_LEN];
0369 
0370     mlxsw_reg_ppbt_pack(ppbt_pl, ingress ? MLXSW_REG_PXBT_E_IACL :
0371                            MLXSW_REG_PXBT_E_EACL,
0372                 MLXSW_REG_PXBT_OP_BIND, mlxsw_sp_port->local_port,
0373                 group->id);
0374     return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppbt), ppbt_pl);
0375 }
0376 
0377 static void
0378 mlxsw_sp_acl_tcam_group_unbind(struct mlxsw_sp *mlxsw_sp,
0379                    struct mlxsw_sp_acl_tcam_group *group,
0380                    struct mlxsw_sp_port *mlxsw_sp_port,
0381                    bool ingress)
0382 {
0383     char ppbt_pl[MLXSW_REG_PPBT_LEN];
0384 
0385     mlxsw_reg_ppbt_pack(ppbt_pl, ingress ? MLXSW_REG_PXBT_E_IACL :
0386                            MLXSW_REG_PXBT_E_EACL,
0387                 MLXSW_REG_PXBT_OP_UNBIND, mlxsw_sp_port->local_port,
0388                 group->id);
0389     mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppbt), ppbt_pl);
0390 }
0391 
0392 static u16
0393 mlxsw_sp_acl_tcam_group_id(struct mlxsw_sp_acl_tcam_group *group)
0394 {
0395     return group->id;
0396 }
0397 
0398 static unsigned int
0399 mlxsw_sp_acl_tcam_vregion_prio(struct mlxsw_sp_acl_tcam_vregion *vregion)
0400 {
0401     struct mlxsw_sp_acl_tcam_vchunk *vchunk;
0402 
0403     if (list_empty(&vregion->vchunk_list))
0404         return 0;
0405     /* As a priority of a vregion, return priority of the first vchunk */
0406     vchunk = list_first_entry(&vregion->vchunk_list,
0407                   typeof(*vchunk), list);
0408     return vchunk->priority;
0409 }
0410 
0411 static unsigned int
0412 mlxsw_sp_acl_tcam_vregion_max_prio(struct mlxsw_sp_acl_tcam_vregion *vregion)
0413 {
0414     struct mlxsw_sp_acl_tcam_vchunk *vchunk;
0415 
0416     if (list_empty(&vregion->vchunk_list))
0417         return 0;
0418     vchunk = list_last_entry(&vregion->vchunk_list,
0419                  typeof(*vchunk), list);
0420     return vchunk->priority;
0421 }
0422 
0423 static void
0424 mlxsw_sp_acl_tcam_vgroup_prio_update(struct mlxsw_sp_acl_tcam_vgroup *vgroup)
0425 {
0426     struct mlxsw_sp_acl_tcam_vregion *vregion;
0427 
0428     if (list_empty(&vgroup->vregion_list))
0429         return;
0430     vregion = list_first_entry(&vgroup->vregion_list,
0431                    typeof(*vregion), list);
0432     *vgroup->p_min_prio = mlxsw_sp_acl_tcam_vregion_prio(vregion);
0433     vregion = list_last_entry(&vgroup->vregion_list,
0434                   typeof(*vregion), list);
0435     *vgroup->p_max_prio = mlxsw_sp_acl_tcam_vregion_max_prio(vregion);
0436 }
0437 
0438 static int
0439 mlxsw_sp_acl_tcam_group_region_attach(struct mlxsw_sp *mlxsw_sp,
0440                       struct mlxsw_sp_acl_tcam_group *group,
0441                       struct mlxsw_sp_acl_tcam_region *region,
0442                       unsigned int priority,
0443                       struct mlxsw_sp_acl_tcam_region *next_region)
0444 {
0445     struct mlxsw_sp_acl_tcam_region *region2;
0446     struct list_head *pos;
0447     int err;
0448 
0449     mutex_lock(&group->lock);
0450     if (group->region_count == group->tcam->max_group_size) {
0451         err = -ENOBUFS;
0452         goto err_region_count_check;
0453     }
0454 
0455     if (next_region) {
0456         /* If the next region is defined, place the new one
0457          * before it. The next one is a sibling.
0458          */
0459         pos = &next_region->list;
0460     } else {
0461         /* Position the region inside the list according to priority */
0462         list_for_each(pos, &group->region_list) {
0463             region2 = list_entry(pos, typeof(*region2), list);
0464             if (mlxsw_sp_acl_tcam_vregion_prio(region2->vregion) >
0465                 priority)
0466                 break;
0467         }
0468     }
0469     list_add_tail(&region->list, pos);
0470     region->group = group;
0471 
0472     err = mlxsw_sp_acl_tcam_group_update(mlxsw_sp, group);
0473     if (err)
0474         goto err_group_update;
0475 
0476     group->region_count++;
0477     mutex_unlock(&group->lock);
0478     return 0;
0479 
0480 err_group_update:
0481     list_del(&region->list);
0482 err_region_count_check:
0483     mutex_unlock(&group->lock);
0484     return err;
0485 }
0486 
0487 static void
0488 mlxsw_sp_acl_tcam_group_region_detach(struct mlxsw_sp *mlxsw_sp,
0489                       struct mlxsw_sp_acl_tcam_region *region)
0490 {
0491     struct mlxsw_sp_acl_tcam_group *group = region->group;
0492 
0493     mutex_lock(&group->lock);
0494     list_del(&region->list);
0495     group->region_count--;
0496     mlxsw_sp_acl_tcam_group_update(mlxsw_sp, group);
0497     mutex_unlock(&group->lock);
0498 }
0499 
0500 static int
0501 mlxsw_sp_acl_tcam_vgroup_vregion_attach(struct mlxsw_sp *mlxsw_sp,
0502                     struct mlxsw_sp_acl_tcam_vgroup *vgroup,
0503                     struct mlxsw_sp_acl_tcam_vregion *vregion,
0504                     unsigned int priority)
0505 {
0506     struct mlxsw_sp_acl_tcam_vregion *vregion2;
0507     struct list_head *pos;
0508     int err;
0509 
0510     /* Position the vregion inside the list according to priority */
0511     list_for_each(pos, &vgroup->vregion_list) {
0512         vregion2 = list_entry(pos, typeof(*vregion2), list);
0513         if (mlxsw_sp_acl_tcam_vregion_prio(vregion2) > priority)
0514             break;
0515     }
0516     list_add_tail(&vregion->list, pos);
0517 
0518     err = mlxsw_sp_acl_tcam_group_region_attach(mlxsw_sp, &vgroup->group,
0519                             vregion->region,
0520                             priority, NULL);
0521     if (err)
0522         goto err_region_attach;
0523 
0524     return 0;
0525 
0526 err_region_attach:
0527     list_del(&vregion->list);
0528     return err;
0529 }
0530 
0531 static void
0532 mlxsw_sp_acl_tcam_vgroup_vregion_detach(struct mlxsw_sp *mlxsw_sp,
0533                     struct mlxsw_sp_acl_tcam_vregion *vregion)
0534 {
0535     list_del(&vregion->list);
0536     if (vregion->region2)
0537         mlxsw_sp_acl_tcam_group_region_detach(mlxsw_sp,
0538                               vregion->region2);
0539     mlxsw_sp_acl_tcam_group_region_detach(mlxsw_sp, vregion->region);
0540 }
0541 
0542 static struct mlxsw_sp_acl_tcam_vregion *
0543 mlxsw_sp_acl_tcam_vgroup_vregion_find(struct mlxsw_sp_acl_tcam_vgroup *vgroup,
0544                       unsigned int priority,
0545                       struct mlxsw_afk_element_usage *elusage,
0546                       bool *p_need_split)
0547 {
0548     struct mlxsw_sp_acl_tcam_vregion *vregion, *vregion2;
0549     struct list_head *pos;
0550     bool issubset;
0551 
0552     list_for_each(pos, &vgroup->vregion_list) {
0553         vregion = list_entry(pos, typeof(*vregion), list);
0554 
0555         /* First, check if the requested priority does not rather belong
0556          * under some of the next vregions.
0557          */
0558         if (pos->next != &vgroup->vregion_list) { /* not last */
0559             vregion2 = list_entry(pos->next, typeof(*vregion2),
0560                           list);
0561             if (priority >=
0562                 mlxsw_sp_acl_tcam_vregion_prio(vregion2))
0563                 continue;
0564         }
0565 
0566         issubset = mlxsw_afk_key_info_subset(vregion->key_info,
0567                              elusage);
0568 
0569         /* If requested element usage would not fit and the priority
0570          * is lower than the currently inspected vregion we cannot
0571          * use this region, so return NULL to indicate new vregion has
0572          * to be created.
0573          */
0574         if (!issubset &&
0575             priority < mlxsw_sp_acl_tcam_vregion_prio(vregion))
0576             return NULL;
0577 
0578         /* If requested element usage would not fit and the priority
0579          * is higher than the currently inspected vregion we cannot
0580          * use this vregion. There is still some hope that the next
0581          * vregion would be the fit. So let it be processed and
0582          * eventually break at the check right above this.
0583          */
0584         if (!issubset &&
0585             priority > mlxsw_sp_acl_tcam_vregion_max_prio(vregion))
0586             continue;
0587 
0588         /* Indicate if the vregion needs to be split in order to add
0589          * the requested priority. Split is needed when requested
0590          * element usage won't fit into the found vregion.
0591          */
0592         *p_need_split = !issubset;
0593         return vregion;
0594     }
0595     return NULL; /* New vregion has to be created. */
0596 }
0597 
0598 static void
0599 mlxsw_sp_acl_tcam_vgroup_use_patterns(struct mlxsw_sp_acl_tcam_vgroup *vgroup,
0600                       struct mlxsw_afk_element_usage *elusage,
0601                       struct mlxsw_afk_element_usage *out)
0602 {
0603     const struct mlxsw_sp_acl_tcam_pattern *pattern;
0604     int i;
0605 
0606     /* In case the template is set, we don't have to look up the pattern
0607      * and just use the template.
0608      */
0609     if (vgroup->tmplt_elusage_set) {
0610         memcpy(out, &vgroup->tmplt_elusage, sizeof(*out));
0611         WARN_ON(!mlxsw_afk_element_usage_subset(elusage, out));
0612         return;
0613     }
0614 
0615     for (i = 0; i < vgroup->patterns_count; i++) {
0616         pattern = &vgroup->patterns[i];
0617         mlxsw_afk_element_usage_fill(out, pattern->elements,
0618                          pattern->elements_count);
0619         if (mlxsw_afk_element_usage_subset(elusage, out))
0620             return;
0621     }
0622     memcpy(out, elusage, sizeof(*out));
0623 }
0624 
0625 static int
0626 mlxsw_sp_acl_tcam_region_alloc(struct mlxsw_sp *mlxsw_sp,
0627                    struct mlxsw_sp_acl_tcam_region *region)
0628 {
0629     struct mlxsw_afk_key_info *key_info = region->key_info;
0630     char ptar_pl[MLXSW_REG_PTAR_LEN];
0631     unsigned int encodings_count;
0632     int i;
0633     int err;
0634 
0635     mlxsw_reg_ptar_pack(ptar_pl, MLXSW_REG_PTAR_OP_ALLOC,
0636                 region->key_type,
0637                 MLXSW_SP_ACL_TCAM_REGION_BASE_COUNT,
0638                 region->id, region->tcam_region_info);
0639     encodings_count = mlxsw_afk_key_info_blocks_count_get(key_info);
0640     for (i = 0; i < encodings_count; i++) {
0641         u16 encoding;
0642 
0643         encoding = mlxsw_afk_key_info_block_encoding_get(key_info, i);
0644         mlxsw_reg_ptar_key_id_pack(ptar_pl, i, encoding);
0645     }
0646     err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptar), ptar_pl);
0647     if (err)
0648         return err;
0649     mlxsw_reg_ptar_unpack(ptar_pl, region->tcam_region_info);
0650     return 0;
0651 }
0652 
0653 static void
0654 mlxsw_sp_acl_tcam_region_free(struct mlxsw_sp *mlxsw_sp,
0655                   struct mlxsw_sp_acl_tcam_region *region)
0656 {
0657     char ptar_pl[MLXSW_REG_PTAR_LEN];
0658 
0659     mlxsw_reg_ptar_pack(ptar_pl, MLXSW_REG_PTAR_OP_FREE,
0660                 region->key_type, 0, region->id,
0661                 region->tcam_region_info);
0662     mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptar), ptar_pl);
0663 }
0664 
0665 static int
0666 mlxsw_sp_acl_tcam_region_enable(struct mlxsw_sp *mlxsw_sp,
0667                 struct mlxsw_sp_acl_tcam_region *region)
0668 {
0669     char pacl_pl[MLXSW_REG_PACL_LEN];
0670 
0671     mlxsw_reg_pacl_pack(pacl_pl, region->id, true,
0672                 region->tcam_region_info);
0673     return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pacl), pacl_pl);
0674 }
0675 
0676 static void
0677 mlxsw_sp_acl_tcam_region_disable(struct mlxsw_sp *mlxsw_sp,
0678                  struct mlxsw_sp_acl_tcam_region *region)
0679 {
0680     char pacl_pl[MLXSW_REG_PACL_LEN];
0681 
0682     mlxsw_reg_pacl_pack(pacl_pl, region->id, false,
0683                 region->tcam_region_info);
0684     mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pacl), pacl_pl);
0685 }
0686 
0687 static struct mlxsw_sp_acl_tcam_region *
0688 mlxsw_sp_acl_tcam_region_create(struct mlxsw_sp *mlxsw_sp,
0689                 struct mlxsw_sp_acl_tcam *tcam,
0690                 struct mlxsw_sp_acl_tcam_vregion *vregion,
0691                 void *hints_priv)
0692 {
0693     const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
0694     struct mlxsw_sp_acl_tcam_region *region;
0695     int err;
0696 
0697     region = kzalloc(sizeof(*region) + ops->region_priv_size, GFP_KERNEL);
0698     if (!region)
0699         return ERR_PTR(-ENOMEM);
0700     region->mlxsw_sp = mlxsw_sp;
0701     region->vregion = vregion;
0702     region->key_info = vregion->key_info;
0703 
0704     err = mlxsw_sp_acl_tcam_region_id_get(tcam, &region->id);
0705     if (err)
0706         goto err_region_id_get;
0707 
0708     err = ops->region_associate(mlxsw_sp, region);
0709     if (err)
0710         goto err_tcam_region_associate;
0711 
0712     region->key_type = ops->key_type;
0713     err = mlxsw_sp_acl_tcam_region_alloc(mlxsw_sp, region);
0714     if (err)
0715         goto err_tcam_region_alloc;
0716 
0717     err = mlxsw_sp_acl_tcam_region_enable(mlxsw_sp, region);
0718     if (err)
0719         goto err_tcam_region_enable;
0720 
0721     err = ops->region_init(mlxsw_sp, region->priv, tcam->priv,
0722                    region, hints_priv);
0723     if (err)
0724         goto err_tcam_region_init;
0725 
0726     return region;
0727 
0728 err_tcam_region_init:
0729     mlxsw_sp_acl_tcam_region_disable(mlxsw_sp, region);
0730 err_tcam_region_enable:
0731     mlxsw_sp_acl_tcam_region_free(mlxsw_sp, region);
0732 err_tcam_region_alloc:
0733 err_tcam_region_associate:
0734     mlxsw_sp_acl_tcam_region_id_put(tcam, region->id);
0735 err_region_id_get:
0736     kfree(region);
0737     return ERR_PTR(err);
0738 }
0739 
0740 static void
0741 mlxsw_sp_acl_tcam_region_destroy(struct mlxsw_sp *mlxsw_sp,
0742                  struct mlxsw_sp_acl_tcam_region *region)
0743 {
0744     const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
0745 
0746     ops->region_fini(mlxsw_sp, region->priv);
0747     mlxsw_sp_acl_tcam_region_disable(mlxsw_sp, region);
0748     mlxsw_sp_acl_tcam_region_free(mlxsw_sp, region);
0749     mlxsw_sp_acl_tcam_region_id_put(region->group->tcam,
0750                     region->id);
0751     kfree(region);
0752 }
0753 
0754 static void
0755 mlxsw_sp_acl_tcam_vregion_rehash_work_schedule(struct mlxsw_sp_acl_tcam_vregion *vregion)
0756 {
0757     unsigned long interval = vregion->tcam->vregion_rehash_intrvl;
0758 
0759     if (!interval)
0760         return;
0761     mlxsw_core_schedule_dw(&vregion->rehash.dw,
0762                    msecs_to_jiffies(interval));
0763 }
0764 
0765 static void
0766 mlxsw_sp_acl_tcam_vregion_rehash(struct mlxsw_sp *mlxsw_sp,
0767                  struct mlxsw_sp_acl_tcam_vregion *vregion,
0768                  int *credits);
0769 
0770 static void mlxsw_sp_acl_tcam_vregion_rehash_work(struct work_struct *work)
0771 {
0772     struct mlxsw_sp_acl_tcam_vregion *vregion =
0773         container_of(work, struct mlxsw_sp_acl_tcam_vregion,
0774                  rehash.dw.work);
0775     int credits = MLXSW_SP_ACL_TCAM_VREGION_REHASH_CREDITS;
0776 
0777     mlxsw_sp_acl_tcam_vregion_rehash(vregion->mlxsw_sp, vregion, &credits);
0778     if (credits < 0)
0779         /* Rehash gone out of credits so it was interrupted.
0780          * Schedule the work as soon as possible to continue.
0781          */
0782         mlxsw_core_schedule_dw(&vregion->rehash.dw, 0);
0783     else
0784         mlxsw_sp_acl_tcam_vregion_rehash_work_schedule(vregion);
0785 }
0786 
0787 static void
0788 mlxsw_sp_acl_tcam_rehash_ctx_vchunk_changed(struct mlxsw_sp_acl_tcam_vchunk *vchunk)
0789 {
0790     struct mlxsw_sp_acl_tcam_vregion *vregion = vchunk->vregion;
0791 
0792     /* If a rule was added or deleted from vchunk which is currently
0793      * under rehash migration, we have to reset the ventry pointers
0794      * to make sure all rules are properly migrated.
0795      */
0796     if (vregion->rehash.ctx.current_vchunk == vchunk) {
0797         vregion->rehash.ctx.start_ventry = NULL;
0798         vregion->rehash.ctx.stop_ventry = NULL;
0799     }
0800 }
0801 
0802 static void
0803 mlxsw_sp_acl_tcam_rehash_ctx_vregion_changed(struct mlxsw_sp_acl_tcam_vregion *vregion)
0804 {
0805     /* If a chunk was added or deleted from vregion we have to reset
0806      * the current chunk pointer to make sure all chunks
0807      * are properly migrated.
0808      */
0809     vregion->rehash.ctx.current_vchunk = NULL;
0810 }
0811 
0812 static struct mlxsw_sp_acl_tcam_vregion *
0813 mlxsw_sp_acl_tcam_vregion_create(struct mlxsw_sp *mlxsw_sp,
0814                  struct mlxsw_sp_acl_tcam_vgroup *vgroup,
0815                  unsigned int priority,
0816                  struct mlxsw_afk_element_usage *elusage)
0817 {
0818     const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
0819     struct mlxsw_afk *afk = mlxsw_sp_acl_afk(mlxsw_sp->acl);
0820     struct mlxsw_sp_acl_tcam *tcam = vgroup->group.tcam;
0821     struct mlxsw_sp_acl_tcam_vregion *vregion;
0822     int err;
0823 
0824     vregion = kzalloc(sizeof(*vregion), GFP_KERNEL);
0825     if (!vregion)
0826         return ERR_PTR(-ENOMEM);
0827     INIT_LIST_HEAD(&vregion->vchunk_list);
0828     mutex_init(&vregion->lock);
0829     vregion->tcam = tcam;
0830     vregion->mlxsw_sp = mlxsw_sp;
0831     vregion->vgroup = vgroup;
0832     vregion->ref_count = 1;
0833 
0834     vregion->key_info = mlxsw_afk_key_info_get(afk, elusage);
0835     if (IS_ERR(vregion->key_info)) {
0836         err = PTR_ERR(vregion->key_info);
0837         goto err_key_info_get;
0838     }
0839 
0840     vregion->region = mlxsw_sp_acl_tcam_region_create(mlxsw_sp, tcam,
0841                               vregion, NULL);
0842     if (IS_ERR(vregion->region)) {
0843         err = PTR_ERR(vregion->region);
0844         goto err_region_create;
0845     }
0846 
0847     err = mlxsw_sp_acl_tcam_vgroup_vregion_attach(mlxsw_sp, vgroup, vregion,
0848                               priority);
0849     if (err)
0850         goto err_vgroup_vregion_attach;
0851 
0852     if (vgroup->vregion_rehash_enabled && ops->region_rehash_hints_get) {
0853         /* Create the delayed work for vregion periodic rehash */
0854         INIT_DELAYED_WORK(&vregion->rehash.dw,
0855                   mlxsw_sp_acl_tcam_vregion_rehash_work);
0856         mlxsw_sp_acl_tcam_vregion_rehash_work_schedule(vregion);
0857         mutex_lock(&tcam->lock);
0858         list_add_tail(&vregion->tlist, &tcam->vregion_list);
0859         mutex_unlock(&tcam->lock);
0860     }
0861 
0862     return vregion;
0863 
0864 err_vgroup_vregion_attach:
0865     mlxsw_sp_acl_tcam_region_destroy(mlxsw_sp, vregion->region);
0866 err_region_create:
0867     mlxsw_afk_key_info_put(vregion->key_info);
0868 err_key_info_get:
0869     kfree(vregion);
0870     return ERR_PTR(err);
0871 }
0872 
0873 static void
0874 mlxsw_sp_acl_tcam_vregion_destroy(struct mlxsw_sp *mlxsw_sp,
0875                   struct mlxsw_sp_acl_tcam_vregion *vregion)
0876 {
0877     const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
0878     struct mlxsw_sp_acl_tcam_vgroup *vgroup = vregion->vgroup;
0879     struct mlxsw_sp_acl_tcam *tcam = vregion->tcam;
0880 
0881     if (vgroup->vregion_rehash_enabled && ops->region_rehash_hints_get) {
0882         mutex_lock(&tcam->lock);
0883         list_del(&vregion->tlist);
0884         mutex_unlock(&tcam->lock);
0885         cancel_delayed_work_sync(&vregion->rehash.dw);
0886     }
0887     mlxsw_sp_acl_tcam_vgroup_vregion_detach(mlxsw_sp, vregion);
0888     if (vregion->region2)
0889         mlxsw_sp_acl_tcam_region_destroy(mlxsw_sp, vregion->region2);
0890     mlxsw_sp_acl_tcam_region_destroy(mlxsw_sp, vregion->region);
0891     mlxsw_afk_key_info_put(vregion->key_info);
0892     mutex_destroy(&vregion->lock);
0893     kfree(vregion);
0894 }
0895 
0896 u32 mlxsw_sp_acl_tcam_vregion_rehash_intrvl_get(struct mlxsw_sp *mlxsw_sp,
0897                         struct mlxsw_sp_acl_tcam *tcam)
0898 {
0899     const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
0900     u32 vregion_rehash_intrvl;
0901 
0902     if (WARN_ON(!ops->region_rehash_hints_get))
0903         return 0;
0904     vregion_rehash_intrvl = tcam->vregion_rehash_intrvl;
0905     return vregion_rehash_intrvl;
0906 }
0907 
0908 int mlxsw_sp_acl_tcam_vregion_rehash_intrvl_set(struct mlxsw_sp *mlxsw_sp,
0909                         struct mlxsw_sp_acl_tcam *tcam,
0910                         u32 val)
0911 {
0912     const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
0913     struct mlxsw_sp_acl_tcam_vregion *vregion;
0914 
0915     if (val < MLXSW_SP_ACL_TCAM_VREGION_REHASH_INTRVL_MIN && val)
0916         return -EINVAL;
0917     if (WARN_ON(!ops->region_rehash_hints_get))
0918         return -EOPNOTSUPP;
0919     tcam->vregion_rehash_intrvl = val;
0920     mutex_lock(&tcam->lock);
0921     list_for_each_entry(vregion, &tcam->vregion_list, tlist) {
0922         if (val)
0923             mlxsw_core_schedule_dw(&vregion->rehash.dw, 0);
0924         else
0925             cancel_delayed_work_sync(&vregion->rehash.dw);
0926     }
0927     mutex_unlock(&tcam->lock);
0928     return 0;
0929 }
0930 
0931 static struct mlxsw_sp_acl_tcam_vregion *
0932 mlxsw_sp_acl_tcam_vregion_get(struct mlxsw_sp *mlxsw_sp,
0933                   struct mlxsw_sp_acl_tcam_vgroup *vgroup,
0934                   unsigned int priority,
0935                   struct mlxsw_afk_element_usage *elusage)
0936 {
0937     struct mlxsw_afk_element_usage vregion_elusage;
0938     struct mlxsw_sp_acl_tcam_vregion *vregion;
0939     bool need_split;
0940 
0941     vregion = mlxsw_sp_acl_tcam_vgroup_vregion_find(vgroup, priority,
0942                             elusage, &need_split);
0943     if (vregion) {
0944         if (need_split) {
0945             /* According to priority, new vchunk should belong to
0946              * an existing vregion. However, this vchunk needs
0947              * elements that vregion does not contain. We need
0948              * to split the existing vregion into two and create
0949              * a new vregion for the new vchunk in between.
0950              * This is not supported now.
0951              */
0952             return ERR_PTR(-EOPNOTSUPP);
0953         }
0954         vregion->ref_count++;
0955         return vregion;
0956     }
0957 
0958     mlxsw_sp_acl_tcam_vgroup_use_patterns(vgroup, elusage,
0959                           &vregion_elusage);
0960 
0961     return mlxsw_sp_acl_tcam_vregion_create(mlxsw_sp, vgroup, priority,
0962                         &vregion_elusage);
0963 }
0964 
0965 static void
0966 mlxsw_sp_acl_tcam_vregion_put(struct mlxsw_sp *mlxsw_sp,
0967                   struct mlxsw_sp_acl_tcam_vregion *vregion)
0968 {
0969     if (--vregion->ref_count)
0970         return;
0971     mlxsw_sp_acl_tcam_vregion_destroy(mlxsw_sp, vregion);
0972 }
0973 
0974 static struct mlxsw_sp_acl_tcam_chunk *
0975 mlxsw_sp_acl_tcam_chunk_create(struct mlxsw_sp *mlxsw_sp,
0976                    struct mlxsw_sp_acl_tcam_vchunk *vchunk,
0977                    struct mlxsw_sp_acl_tcam_region *region)
0978 {
0979     const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
0980     struct mlxsw_sp_acl_tcam_chunk *chunk;
0981 
0982     chunk = kzalloc(sizeof(*chunk) + ops->chunk_priv_size, GFP_KERNEL);
0983     if (!chunk)
0984         return ERR_PTR(-ENOMEM);
0985     chunk->vchunk = vchunk;
0986     chunk->region = region;
0987 
0988     ops->chunk_init(region->priv, chunk->priv, vchunk->priority);
0989     return chunk;
0990 }
0991 
0992 static void
0993 mlxsw_sp_acl_tcam_chunk_destroy(struct mlxsw_sp *mlxsw_sp,
0994                 struct mlxsw_sp_acl_tcam_chunk *chunk)
0995 {
0996     const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
0997 
0998     ops->chunk_fini(chunk->priv);
0999     kfree(chunk);
1000 }
1001 
1002 static struct mlxsw_sp_acl_tcam_vchunk *
1003 mlxsw_sp_acl_tcam_vchunk_create(struct mlxsw_sp *mlxsw_sp,
1004                 struct mlxsw_sp_acl_tcam_vgroup *vgroup,
1005                 unsigned int priority,
1006                 struct mlxsw_afk_element_usage *elusage)
1007 {
1008     struct mlxsw_sp_acl_tcam_vchunk *vchunk, *vchunk2;
1009     struct mlxsw_sp_acl_tcam_vregion *vregion;
1010     struct list_head *pos;
1011     int err;
1012 
1013     if (priority == MLXSW_SP_ACL_TCAM_CATCHALL_PRIO)
1014         return ERR_PTR(-EINVAL);
1015 
1016     vchunk = kzalloc(sizeof(*vchunk), GFP_KERNEL);
1017     if (!vchunk)
1018         return ERR_PTR(-ENOMEM);
1019     INIT_LIST_HEAD(&vchunk->ventry_list);
1020     vchunk->priority = priority;
1021     vchunk->vgroup = vgroup;
1022     vchunk->ref_count = 1;
1023 
1024     vregion = mlxsw_sp_acl_tcam_vregion_get(mlxsw_sp, vgroup,
1025                         priority, elusage);
1026     if (IS_ERR(vregion)) {
1027         err = PTR_ERR(vregion);
1028         goto err_vregion_get;
1029     }
1030 
1031     vchunk->vregion = vregion;
1032 
1033     err = rhashtable_insert_fast(&vgroup->vchunk_ht, &vchunk->ht_node,
1034                      mlxsw_sp_acl_tcam_vchunk_ht_params);
1035     if (err)
1036         goto err_rhashtable_insert;
1037 
1038     mutex_lock(&vregion->lock);
1039     vchunk->chunk = mlxsw_sp_acl_tcam_chunk_create(mlxsw_sp, vchunk,
1040                                vchunk->vregion->region);
1041     if (IS_ERR(vchunk->chunk)) {
1042         mutex_unlock(&vregion->lock);
1043         err = PTR_ERR(vchunk->chunk);
1044         goto err_chunk_create;
1045     }
1046 
1047     mlxsw_sp_acl_tcam_rehash_ctx_vregion_changed(vregion);
1048 
1049     /* Position the vchunk inside the list according to priority */
1050     list_for_each(pos, &vregion->vchunk_list) {
1051         vchunk2 = list_entry(pos, typeof(*vchunk2), list);
1052         if (vchunk2->priority > priority)
1053             break;
1054     }
1055     list_add_tail(&vchunk->list, pos);
1056     mutex_unlock(&vregion->lock);
1057     mlxsw_sp_acl_tcam_vgroup_prio_update(vgroup);
1058 
1059     return vchunk;
1060 
1061 err_chunk_create:
1062     rhashtable_remove_fast(&vgroup->vchunk_ht, &vchunk->ht_node,
1063                    mlxsw_sp_acl_tcam_vchunk_ht_params);
1064 err_rhashtable_insert:
1065     mlxsw_sp_acl_tcam_vregion_put(mlxsw_sp, vregion);
1066 err_vregion_get:
1067     kfree(vchunk);
1068     return ERR_PTR(err);
1069 }
1070 
1071 static void
1072 mlxsw_sp_acl_tcam_vchunk_destroy(struct mlxsw_sp *mlxsw_sp,
1073                  struct mlxsw_sp_acl_tcam_vchunk *vchunk)
1074 {
1075     struct mlxsw_sp_acl_tcam_vregion *vregion = vchunk->vregion;
1076     struct mlxsw_sp_acl_tcam_vgroup *vgroup = vchunk->vgroup;
1077 
1078     mutex_lock(&vregion->lock);
1079     mlxsw_sp_acl_tcam_rehash_ctx_vregion_changed(vregion);
1080     list_del(&vchunk->list);
1081     if (vchunk->chunk2)
1082         mlxsw_sp_acl_tcam_chunk_destroy(mlxsw_sp, vchunk->chunk2);
1083     mlxsw_sp_acl_tcam_chunk_destroy(mlxsw_sp, vchunk->chunk);
1084     mutex_unlock(&vregion->lock);
1085     rhashtable_remove_fast(&vgroup->vchunk_ht, &vchunk->ht_node,
1086                    mlxsw_sp_acl_tcam_vchunk_ht_params);
1087     mlxsw_sp_acl_tcam_vregion_put(mlxsw_sp, vchunk->vregion);
1088     kfree(vchunk);
1089     mlxsw_sp_acl_tcam_vgroup_prio_update(vgroup);
1090 }
1091 
1092 static struct mlxsw_sp_acl_tcam_vchunk *
1093 mlxsw_sp_acl_tcam_vchunk_get(struct mlxsw_sp *mlxsw_sp,
1094                  struct mlxsw_sp_acl_tcam_vgroup *vgroup,
1095                  unsigned int priority,
1096                  struct mlxsw_afk_element_usage *elusage)
1097 {
1098     struct mlxsw_sp_acl_tcam_vchunk *vchunk;
1099 
1100     vchunk = rhashtable_lookup_fast(&vgroup->vchunk_ht, &priority,
1101                     mlxsw_sp_acl_tcam_vchunk_ht_params);
1102     if (vchunk) {
1103         if (WARN_ON(!mlxsw_afk_key_info_subset(vchunk->vregion->key_info,
1104                                elusage)))
1105             return ERR_PTR(-EINVAL);
1106         vchunk->ref_count++;
1107         return vchunk;
1108     }
1109     return mlxsw_sp_acl_tcam_vchunk_create(mlxsw_sp, vgroup,
1110                            priority, elusage);
1111 }
1112 
1113 static void
1114 mlxsw_sp_acl_tcam_vchunk_put(struct mlxsw_sp *mlxsw_sp,
1115                  struct mlxsw_sp_acl_tcam_vchunk *vchunk)
1116 {
1117     if (--vchunk->ref_count)
1118         return;
1119     mlxsw_sp_acl_tcam_vchunk_destroy(mlxsw_sp, vchunk);
1120 }
1121 
1122 static struct mlxsw_sp_acl_tcam_entry *
1123 mlxsw_sp_acl_tcam_entry_create(struct mlxsw_sp *mlxsw_sp,
1124                    struct mlxsw_sp_acl_tcam_ventry *ventry,
1125                    struct mlxsw_sp_acl_tcam_chunk *chunk)
1126 {
1127     const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
1128     struct mlxsw_sp_acl_tcam_entry *entry;
1129     int err;
1130 
1131     entry = kzalloc(sizeof(*entry) + ops->entry_priv_size, GFP_KERNEL);
1132     if (!entry)
1133         return ERR_PTR(-ENOMEM);
1134     entry->ventry = ventry;
1135     entry->chunk = chunk;
1136 
1137     err = ops->entry_add(mlxsw_sp, chunk->region->priv, chunk->priv,
1138                  entry->priv, ventry->rulei);
1139     if (err)
1140         goto err_entry_add;
1141 
1142     return entry;
1143 
1144 err_entry_add:
1145     kfree(entry);
1146     return ERR_PTR(err);
1147 }
1148 
1149 static void mlxsw_sp_acl_tcam_entry_destroy(struct mlxsw_sp *mlxsw_sp,
1150                         struct mlxsw_sp_acl_tcam_entry *entry)
1151 {
1152     const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
1153 
1154     ops->entry_del(mlxsw_sp, entry->chunk->region->priv,
1155                entry->chunk->priv, entry->priv);
1156     kfree(entry);
1157 }
1158 
1159 static int
1160 mlxsw_sp_acl_tcam_entry_action_replace(struct mlxsw_sp *mlxsw_sp,
1161                        struct mlxsw_sp_acl_tcam_region *region,
1162                        struct mlxsw_sp_acl_tcam_entry *entry,
1163                        struct mlxsw_sp_acl_rule_info *rulei)
1164 {
1165     const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
1166 
1167     return ops->entry_action_replace(mlxsw_sp, region->priv,
1168                      entry->priv, rulei);
1169 }
1170 
1171 static int
1172 mlxsw_sp_acl_tcam_entry_activity_get(struct mlxsw_sp *mlxsw_sp,
1173                      struct mlxsw_sp_acl_tcam_entry *entry,
1174                      bool *activity)
1175 {
1176     const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
1177 
1178     return ops->entry_activity_get(mlxsw_sp, entry->chunk->region->priv,
1179                        entry->priv, activity);
1180 }
1181 
1182 static int mlxsw_sp_acl_tcam_ventry_add(struct mlxsw_sp *mlxsw_sp,
1183                     struct mlxsw_sp_acl_tcam_vgroup *vgroup,
1184                     struct mlxsw_sp_acl_tcam_ventry *ventry,
1185                     struct mlxsw_sp_acl_rule_info *rulei)
1186 {
1187     struct mlxsw_sp_acl_tcam_vregion *vregion;
1188     struct mlxsw_sp_acl_tcam_vchunk *vchunk;
1189     int err;
1190 
1191     vchunk = mlxsw_sp_acl_tcam_vchunk_get(mlxsw_sp, vgroup, rulei->priority,
1192                           &rulei->values.elusage);
1193     if (IS_ERR(vchunk))
1194         return PTR_ERR(vchunk);
1195 
1196     ventry->vchunk = vchunk;
1197     ventry->rulei = rulei;
1198     vregion = vchunk->vregion;
1199 
1200     mutex_lock(&vregion->lock);
1201     ventry->entry = mlxsw_sp_acl_tcam_entry_create(mlxsw_sp, ventry,
1202                                vchunk->chunk);
1203     if (IS_ERR(ventry->entry)) {
1204         mutex_unlock(&vregion->lock);
1205         err = PTR_ERR(ventry->entry);
1206         goto err_entry_create;
1207     }
1208 
1209     list_add_tail(&ventry->list, &vchunk->ventry_list);
1210     mlxsw_sp_acl_tcam_rehash_ctx_vchunk_changed(vchunk);
1211     mutex_unlock(&vregion->lock);
1212 
1213     return 0;
1214 
1215 err_entry_create:
1216     mlxsw_sp_acl_tcam_vchunk_put(mlxsw_sp, vchunk);
1217     return err;
1218 }
1219 
1220 static void mlxsw_sp_acl_tcam_ventry_del(struct mlxsw_sp *mlxsw_sp,
1221                      struct mlxsw_sp_acl_tcam_ventry *ventry)
1222 {
1223     struct mlxsw_sp_acl_tcam_vchunk *vchunk = ventry->vchunk;
1224     struct mlxsw_sp_acl_tcam_vregion *vregion = vchunk->vregion;
1225 
1226     mutex_lock(&vregion->lock);
1227     mlxsw_sp_acl_tcam_rehash_ctx_vchunk_changed(vchunk);
1228     list_del(&ventry->list);
1229     mlxsw_sp_acl_tcam_entry_destroy(mlxsw_sp, ventry->entry);
1230     mutex_unlock(&vregion->lock);
1231     mlxsw_sp_acl_tcam_vchunk_put(mlxsw_sp, vchunk);
1232 }
1233 
1234 static int
1235 mlxsw_sp_acl_tcam_ventry_action_replace(struct mlxsw_sp *mlxsw_sp,
1236                     struct mlxsw_sp_acl_tcam_ventry *ventry,
1237                     struct mlxsw_sp_acl_rule_info *rulei)
1238 {
1239     struct mlxsw_sp_acl_tcam_vchunk *vchunk = ventry->vchunk;
1240 
1241     return mlxsw_sp_acl_tcam_entry_action_replace(mlxsw_sp,
1242                               vchunk->vregion->region,
1243                               ventry->entry, rulei);
1244 }
1245 
1246 static int
1247 mlxsw_sp_acl_tcam_ventry_activity_get(struct mlxsw_sp *mlxsw_sp,
1248                       struct mlxsw_sp_acl_tcam_ventry *ventry,
1249                       bool *activity)
1250 {
1251     return mlxsw_sp_acl_tcam_entry_activity_get(mlxsw_sp,
1252                             ventry->entry, activity);
1253 }
1254 
1255 static int
1256 mlxsw_sp_acl_tcam_ventry_migrate(struct mlxsw_sp *mlxsw_sp,
1257                  struct mlxsw_sp_acl_tcam_ventry *ventry,
1258                  struct mlxsw_sp_acl_tcam_chunk *chunk,
1259                  int *credits)
1260 {
1261     struct mlxsw_sp_acl_tcam_entry *new_entry;
1262 
1263     /* First check if the entry is not already where we want it to be. */
1264     if (ventry->entry->chunk == chunk)
1265         return 0;
1266 
1267     if (--(*credits) < 0)
1268         return 0;
1269 
1270     new_entry = mlxsw_sp_acl_tcam_entry_create(mlxsw_sp, ventry, chunk);
1271     if (IS_ERR(new_entry))
1272         return PTR_ERR(new_entry);
1273     mlxsw_sp_acl_tcam_entry_destroy(mlxsw_sp, ventry->entry);
1274     ventry->entry = new_entry;
1275     return 0;
1276 }
1277 
1278 static int
1279 mlxsw_sp_acl_tcam_vchunk_migrate_start(struct mlxsw_sp *mlxsw_sp,
1280                        struct mlxsw_sp_acl_tcam_vchunk *vchunk,
1281                        struct mlxsw_sp_acl_tcam_region *region,
1282                        struct mlxsw_sp_acl_tcam_rehash_ctx *ctx)
1283 {
1284     struct mlxsw_sp_acl_tcam_chunk *new_chunk;
1285 
1286     new_chunk = mlxsw_sp_acl_tcam_chunk_create(mlxsw_sp, vchunk, region);
1287     if (IS_ERR(new_chunk))
1288         return PTR_ERR(new_chunk);
1289     vchunk->chunk2 = vchunk->chunk;
1290     vchunk->chunk = new_chunk;
1291     ctx->current_vchunk = vchunk;
1292     ctx->start_ventry = NULL;
1293     ctx->stop_ventry = NULL;
1294     return 0;
1295 }
1296 
1297 static void
1298 mlxsw_sp_acl_tcam_vchunk_migrate_end(struct mlxsw_sp *mlxsw_sp,
1299                      struct mlxsw_sp_acl_tcam_vchunk *vchunk,
1300                      struct mlxsw_sp_acl_tcam_rehash_ctx *ctx)
1301 {
1302     mlxsw_sp_acl_tcam_chunk_destroy(mlxsw_sp, vchunk->chunk2);
1303     vchunk->chunk2 = NULL;
1304     ctx->current_vchunk = NULL;
1305 }
1306 
1307 static int
1308 mlxsw_sp_acl_tcam_vchunk_migrate_one(struct mlxsw_sp *mlxsw_sp,
1309                      struct mlxsw_sp_acl_tcam_vchunk *vchunk,
1310                      struct mlxsw_sp_acl_tcam_region *region,
1311                      struct mlxsw_sp_acl_tcam_rehash_ctx *ctx,
1312                      int *credits)
1313 {
1314     struct mlxsw_sp_acl_tcam_ventry *ventry;
1315     int err;
1316 
1317     if (vchunk->chunk->region != region) {
1318         err = mlxsw_sp_acl_tcam_vchunk_migrate_start(mlxsw_sp, vchunk,
1319                                  region, ctx);
1320         if (err)
1321             return err;
1322     } else if (!vchunk->chunk2) {
1323         /* The chunk is already as it should be, nothing to do. */
1324         return 0;
1325     }
1326 
1327     /* If the migration got interrupted, we have the ventry to start from
1328      * stored in context.
1329      */
1330     if (ctx->start_ventry)
1331         ventry = ctx->start_ventry;
1332     else
1333         ventry = list_first_entry(&vchunk->ventry_list,
1334                       typeof(*ventry), list);
1335 
1336     list_for_each_entry_from(ventry, &vchunk->ventry_list, list) {
1337         /* During rollback, once we reach the ventry that failed
1338          * to migrate, we are done.
1339          */
1340         if (ventry == ctx->stop_ventry)
1341             break;
1342 
1343         err = mlxsw_sp_acl_tcam_ventry_migrate(mlxsw_sp, ventry,
1344                                vchunk->chunk, credits);
1345         if (err) {
1346             if (ctx->this_is_rollback) {
1347                 /* Save the ventry which we ended with and try
1348                  * to continue later on.
1349                  */
1350                 ctx->start_ventry = ventry;
1351                 return err;
1352             }
1353             /* Swap the chunk and chunk2 pointers so the follow-up
1354              * rollback call will see the original chunk pointer
1355              * in vchunk->chunk.
1356              */
1357             swap(vchunk->chunk, vchunk->chunk2);
1358             /* The rollback has to be done from beginning of the
1359              * chunk, that is why we have to null the start_ventry.
1360              * However, we know where to stop the rollback,
1361              * at the current ventry.
1362              */
1363             ctx->start_ventry = NULL;
1364             ctx->stop_ventry = ventry;
1365             return err;
1366         } else if (*credits < 0) {
1367             /* We are out of credits, the rest of the ventries
1368              * will be migrated later. Save the ventry
1369              * which we ended with.
1370              */
1371             ctx->start_ventry = ventry;
1372             return 0;
1373         }
1374     }
1375 
1376     mlxsw_sp_acl_tcam_vchunk_migrate_end(mlxsw_sp, vchunk, ctx);
1377     return 0;
1378 }
1379 
1380 static int
1381 mlxsw_sp_acl_tcam_vchunk_migrate_all(struct mlxsw_sp *mlxsw_sp,
1382                      struct mlxsw_sp_acl_tcam_vregion *vregion,
1383                      struct mlxsw_sp_acl_tcam_rehash_ctx *ctx,
1384                      int *credits)
1385 {
1386     struct mlxsw_sp_acl_tcam_vchunk *vchunk;
1387     int err;
1388 
1389     /* If the migration got interrupted, we have the vchunk
1390      * we are working on stored in context.
1391      */
1392     if (ctx->current_vchunk)
1393         vchunk = ctx->current_vchunk;
1394     else
1395         vchunk = list_first_entry(&vregion->vchunk_list,
1396                       typeof(*vchunk), list);
1397 
1398     list_for_each_entry_from(vchunk, &vregion->vchunk_list, list) {
1399         err = mlxsw_sp_acl_tcam_vchunk_migrate_one(mlxsw_sp, vchunk,
1400                                vregion->region,
1401                                ctx, credits);
1402         if (err || *credits < 0)
1403             return err;
1404     }
1405     return 0;
1406 }
1407 
1408 static int
1409 mlxsw_sp_acl_tcam_vregion_migrate(struct mlxsw_sp *mlxsw_sp,
1410                   struct mlxsw_sp_acl_tcam_vregion *vregion,
1411                   struct mlxsw_sp_acl_tcam_rehash_ctx *ctx,
1412                   int *credits)
1413 {
1414     int err, err2;
1415 
1416     trace_mlxsw_sp_acl_tcam_vregion_migrate(mlxsw_sp, vregion);
1417     mutex_lock(&vregion->lock);
1418     err = mlxsw_sp_acl_tcam_vchunk_migrate_all(mlxsw_sp, vregion,
1419                            ctx, credits);
1420     if (err) {
1421         /* In case migration was not successful, we need to swap
1422          * so the original region pointer is assigned again
1423          * to vregion->region.
1424          */
1425         swap(vregion->region, vregion->region2);
1426         ctx->current_vchunk = NULL;
1427         ctx->this_is_rollback = true;
1428         err2 = mlxsw_sp_acl_tcam_vchunk_migrate_all(mlxsw_sp, vregion,
1429                                 ctx, credits);
1430         if (err2) {
1431             trace_mlxsw_sp_acl_tcam_vregion_rehash_rollback_failed(mlxsw_sp,
1432                                            vregion);
1433             dev_err(mlxsw_sp->bus_info->dev, "Failed to rollback during vregion migration fail\n");
1434             /* Let the rollback to be continued later on. */
1435         }
1436     }
1437     mutex_unlock(&vregion->lock);
1438     trace_mlxsw_sp_acl_tcam_vregion_migrate_end(mlxsw_sp, vregion);
1439     return err;
1440 }
1441 
1442 static bool
1443 mlxsw_sp_acl_tcam_vregion_rehash_in_progress(const struct mlxsw_sp_acl_tcam_rehash_ctx *ctx)
1444 {
1445     return ctx->hints_priv;
1446 }
1447 
1448 static int
1449 mlxsw_sp_acl_tcam_vregion_rehash_start(struct mlxsw_sp *mlxsw_sp,
1450                        struct mlxsw_sp_acl_tcam_vregion *vregion,
1451                        struct mlxsw_sp_acl_tcam_rehash_ctx *ctx)
1452 {
1453     const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
1454     unsigned int priority = mlxsw_sp_acl_tcam_vregion_prio(vregion);
1455     struct mlxsw_sp_acl_tcam_region *new_region;
1456     void *hints_priv;
1457     int err;
1458 
1459     trace_mlxsw_sp_acl_tcam_vregion_rehash(mlxsw_sp, vregion);
1460 
1461     hints_priv = ops->region_rehash_hints_get(vregion->region->priv);
1462     if (IS_ERR(hints_priv))
1463         return PTR_ERR(hints_priv);
1464 
1465     new_region = mlxsw_sp_acl_tcam_region_create(mlxsw_sp, vregion->tcam,
1466                              vregion, hints_priv);
1467     if (IS_ERR(new_region)) {
1468         err = PTR_ERR(new_region);
1469         goto err_region_create;
1470     }
1471 
1472     /* vregion->region contains the pointer to the new region
1473      * we are going to migrate to.
1474      */
1475     vregion->region2 = vregion->region;
1476     vregion->region = new_region;
1477     err = mlxsw_sp_acl_tcam_group_region_attach(mlxsw_sp,
1478                             vregion->region2->group,
1479                             new_region, priority,
1480                             vregion->region2);
1481     if (err)
1482         goto err_group_region_attach;
1483 
1484     ctx->hints_priv = hints_priv;
1485     ctx->this_is_rollback = false;
1486 
1487     return 0;
1488 
1489 err_group_region_attach:
1490     vregion->region = vregion->region2;
1491     vregion->region2 = NULL;
1492     mlxsw_sp_acl_tcam_region_destroy(mlxsw_sp, new_region);
1493 err_region_create:
1494     ops->region_rehash_hints_put(hints_priv);
1495     return err;
1496 }
1497 
1498 static void
1499 mlxsw_sp_acl_tcam_vregion_rehash_end(struct mlxsw_sp *mlxsw_sp,
1500                      struct mlxsw_sp_acl_tcam_vregion *vregion,
1501                      struct mlxsw_sp_acl_tcam_rehash_ctx *ctx)
1502 {
1503     struct mlxsw_sp_acl_tcam_region *unused_region = vregion->region2;
1504     const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
1505 
1506     vregion->region2 = NULL;
1507     mlxsw_sp_acl_tcam_group_region_detach(mlxsw_sp, unused_region);
1508     mlxsw_sp_acl_tcam_region_destroy(mlxsw_sp, unused_region);
1509     ops->region_rehash_hints_put(ctx->hints_priv);
1510     ctx->hints_priv = NULL;
1511 }
1512 
1513 static void
1514 mlxsw_sp_acl_tcam_vregion_rehash(struct mlxsw_sp *mlxsw_sp,
1515                  struct mlxsw_sp_acl_tcam_vregion *vregion,
1516                  int *credits)
1517 {
1518     struct mlxsw_sp_acl_tcam_rehash_ctx *ctx = &vregion->rehash.ctx;
1519     int err;
1520 
1521     /* Check if the previous rehash work was interrupted
1522      * which means we have to continue it now.
1523      * If not, start a new rehash.
1524      */
1525     if (!mlxsw_sp_acl_tcam_vregion_rehash_in_progress(ctx)) {
1526         err = mlxsw_sp_acl_tcam_vregion_rehash_start(mlxsw_sp,
1527                                  vregion, ctx);
1528         if (err) {
1529             if (err != -EAGAIN)
1530                 dev_err(mlxsw_sp->bus_info->dev, "Failed get rehash hints\n");
1531             return;
1532         }
1533     }
1534 
1535     err = mlxsw_sp_acl_tcam_vregion_migrate(mlxsw_sp, vregion,
1536                         ctx, credits);
1537     if (err) {
1538         dev_err(mlxsw_sp->bus_info->dev, "Failed to migrate vregion\n");
1539     }
1540 
1541     if (*credits >= 0)
1542         mlxsw_sp_acl_tcam_vregion_rehash_end(mlxsw_sp, vregion, ctx);
1543 }
1544 
1545 static const enum mlxsw_afk_element mlxsw_sp_acl_tcam_pattern_ipv4[] = {
1546     MLXSW_AFK_ELEMENT_SRC_SYS_PORT,
1547     MLXSW_AFK_ELEMENT_DMAC_32_47,
1548     MLXSW_AFK_ELEMENT_DMAC_0_31,
1549     MLXSW_AFK_ELEMENT_SMAC_32_47,
1550     MLXSW_AFK_ELEMENT_SMAC_0_31,
1551     MLXSW_AFK_ELEMENT_ETHERTYPE,
1552     MLXSW_AFK_ELEMENT_IP_PROTO,
1553     MLXSW_AFK_ELEMENT_SRC_IP_0_31,
1554     MLXSW_AFK_ELEMENT_DST_IP_0_31,
1555     MLXSW_AFK_ELEMENT_DST_L4_PORT,
1556     MLXSW_AFK_ELEMENT_SRC_L4_PORT,
1557     MLXSW_AFK_ELEMENT_VID,
1558     MLXSW_AFK_ELEMENT_PCP,
1559     MLXSW_AFK_ELEMENT_TCP_FLAGS,
1560     MLXSW_AFK_ELEMENT_IP_TTL_,
1561     MLXSW_AFK_ELEMENT_IP_ECN,
1562     MLXSW_AFK_ELEMENT_IP_DSCP,
1563 };
1564 
1565 static const enum mlxsw_afk_element mlxsw_sp_acl_tcam_pattern_ipv6[] = {
1566     MLXSW_AFK_ELEMENT_ETHERTYPE,
1567     MLXSW_AFK_ELEMENT_IP_PROTO,
1568     MLXSW_AFK_ELEMENT_SRC_IP_96_127,
1569     MLXSW_AFK_ELEMENT_SRC_IP_64_95,
1570     MLXSW_AFK_ELEMENT_SRC_IP_32_63,
1571     MLXSW_AFK_ELEMENT_SRC_IP_0_31,
1572     MLXSW_AFK_ELEMENT_DST_IP_96_127,
1573     MLXSW_AFK_ELEMENT_DST_IP_64_95,
1574     MLXSW_AFK_ELEMENT_DST_IP_32_63,
1575     MLXSW_AFK_ELEMENT_DST_IP_0_31,
1576     MLXSW_AFK_ELEMENT_DST_L4_PORT,
1577     MLXSW_AFK_ELEMENT_SRC_L4_PORT,
1578 };
1579 
1580 static const struct mlxsw_sp_acl_tcam_pattern mlxsw_sp_acl_tcam_patterns[] = {
1581     {
1582         .elements = mlxsw_sp_acl_tcam_pattern_ipv4,
1583         .elements_count = ARRAY_SIZE(mlxsw_sp_acl_tcam_pattern_ipv4),
1584     },
1585     {
1586         .elements = mlxsw_sp_acl_tcam_pattern_ipv6,
1587         .elements_count = ARRAY_SIZE(mlxsw_sp_acl_tcam_pattern_ipv6),
1588     },
1589 };
1590 
1591 #define MLXSW_SP_ACL_TCAM_PATTERNS_COUNT \
1592     ARRAY_SIZE(mlxsw_sp_acl_tcam_patterns)
1593 
1594 struct mlxsw_sp_acl_tcam_flower_ruleset {
1595     struct mlxsw_sp_acl_tcam_vgroup vgroup;
1596 };
1597 
1598 struct mlxsw_sp_acl_tcam_flower_rule {
1599     struct mlxsw_sp_acl_tcam_ventry ventry;
1600 };
1601 
1602 static int
1603 mlxsw_sp_acl_tcam_flower_ruleset_add(struct mlxsw_sp *mlxsw_sp,
1604                      struct mlxsw_sp_acl_tcam *tcam,
1605                      void *ruleset_priv,
1606                      struct mlxsw_afk_element_usage *tmplt_elusage,
1607                      unsigned int *p_min_prio,
1608                      unsigned int *p_max_prio)
1609 {
1610     struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
1611 
1612     return mlxsw_sp_acl_tcam_vgroup_add(mlxsw_sp, tcam, &ruleset->vgroup,
1613                         mlxsw_sp_acl_tcam_patterns,
1614                         MLXSW_SP_ACL_TCAM_PATTERNS_COUNT,
1615                         tmplt_elusage, true,
1616                         p_min_prio, p_max_prio);
1617 }
1618 
1619 static void
1620 mlxsw_sp_acl_tcam_flower_ruleset_del(struct mlxsw_sp *mlxsw_sp,
1621                      void *ruleset_priv)
1622 {
1623     struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
1624 
1625     mlxsw_sp_acl_tcam_vgroup_del(&ruleset->vgroup);
1626 }
1627 
1628 static int
1629 mlxsw_sp_acl_tcam_flower_ruleset_bind(struct mlxsw_sp *mlxsw_sp,
1630                       void *ruleset_priv,
1631                       struct mlxsw_sp_port *mlxsw_sp_port,
1632                       bool ingress)
1633 {
1634     struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
1635 
1636     return mlxsw_sp_acl_tcam_group_bind(mlxsw_sp, &ruleset->vgroup.group,
1637                         mlxsw_sp_port, ingress);
1638 }
1639 
1640 static void
1641 mlxsw_sp_acl_tcam_flower_ruleset_unbind(struct mlxsw_sp *mlxsw_sp,
1642                     void *ruleset_priv,
1643                     struct mlxsw_sp_port *mlxsw_sp_port,
1644                     bool ingress)
1645 {
1646     struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
1647 
1648     mlxsw_sp_acl_tcam_group_unbind(mlxsw_sp, &ruleset->vgroup.group,
1649                        mlxsw_sp_port, ingress);
1650 }
1651 
1652 static u16
1653 mlxsw_sp_acl_tcam_flower_ruleset_group_id(void *ruleset_priv)
1654 {
1655     struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
1656 
1657     return mlxsw_sp_acl_tcam_group_id(&ruleset->vgroup.group);
1658 }
1659 
1660 static int
1661 mlxsw_sp_acl_tcam_flower_rule_add(struct mlxsw_sp *mlxsw_sp,
1662                   void *ruleset_priv, void *rule_priv,
1663                   struct mlxsw_sp_acl_rule_info *rulei)
1664 {
1665     struct mlxsw_sp_acl_tcam_flower_ruleset *ruleset = ruleset_priv;
1666     struct mlxsw_sp_acl_tcam_flower_rule *rule = rule_priv;
1667 
1668     return mlxsw_sp_acl_tcam_ventry_add(mlxsw_sp, &ruleset->vgroup,
1669                         &rule->ventry, rulei);
1670 }
1671 
1672 static void
1673 mlxsw_sp_acl_tcam_flower_rule_del(struct mlxsw_sp *mlxsw_sp, void *rule_priv)
1674 {
1675     struct mlxsw_sp_acl_tcam_flower_rule *rule = rule_priv;
1676 
1677     mlxsw_sp_acl_tcam_ventry_del(mlxsw_sp, &rule->ventry);
1678 }
1679 
1680 static int
1681 mlxsw_sp_acl_tcam_flower_rule_action_replace(struct mlxsw_sp *mlxsw_sp,
1682                          void *rule_priv,
1683                          struct mlxsw_sp_acl_rule_info *rulei)
1684 {
1685     return -EOPNOTSUPP;
1686 }
1687 
1688 static int
1689 mlxsw_sp_acl_tcam_flower_rule_activity_get(struct mlxsw_sp *mlxsw_sp,
1690                        void *rule_priv, bool *activity)
1691 {
1692     struct mlxsw_sp_acl_tcam_flower_rule *rule = rule_priv;
1693 
1694     return mlxsw_sp_acl_tcam_ventry_activity_get(mlxsw_sp, &rule->ventry,
1695                              activity);
1696 }
1697 
1698 static const struct mlxsw_sp_acl_profile_ops mlxsw_sp_acl_tcam_flower_ops = {
1699     .ruleset_priv_size  = sizeof(struct mlxsw_sp_acl_tcam_flower_ruleset),
1700     .ruleset_add        = mlxsw_sp_acl_tcam_flower_ruleset_add,
1701     .ruleset_del        = mlxsw_sp_acl_tcam_flower_ruleset_del,
1702     .ruleset_bind       = mlxsw_sp_acl_tcam_flower_ruleset_bind,
1703     .ruleset_unbind     = mlxsw_sp_acl_tcam_flower_ruleset_unbind,
1704     .ruleset_group_id   = mlxsw_sp_acl_tcam_flower_ruleset_group_id,
1705     .rule_priv_size     = sizeof(struct mlxsw_sp_acl_tcam_flower_rule),
1706     .rule_add       = mlxsw_sp_acl_tcam_flower_rule_add,
1707     .rule_del       = mlxsw_sp_acl_tcam_flower_rule_del,
1708     .rule_action_replace    = mlxsw_sp_acl_tcam_flower_rule_action_replace,
1709     .rule_activity_get  = mlxsw_sp_acl_tcam_flower_rule_activity_get,
1710 };
1711 
1712 struct mlxsw_sp_acl_tcam_mr_ruleset {
1713     struct mlxsw_sp_acl_tcam_vchunk *vchunk;
1714     struct mlxsw_sp_acl_tcam_vgroup vgroup;
1715 };
1716 
1717 struct mlxsw_sp_acl_tcam_mr_rule {
1718     struct mlxsw_sp_acl_tcam_ventry ventry;
1719 };
1720 
1721 static int
1722 mlxsw_sp_acl_tcam_mr_ruleset_add(struct mlxsw_sp *mlxsw_sp,
1723                  struct mlxsw_sp_acl_tcam *tcam,
1724                  void *ruleset_priv,
1725                  struct mlxsw_afk_element_usage *tmplt_elusage,
1726                  unsigned int *p_min_prio,
1727                  unsigned int *p_max_prio)
1728 {
1729     struct mlxsw_sp_acl_tcam_mr_ruleset *ruleset = ruleset_priv;
1730     int err;
1731 
1732     err = mlxsw_sp_acl_tcam_vgroup_add(mlxsw_sp, tcam, &ruleset->vgroup,
1733                        mlxsw_sp_acl_tcam_patterns,
1734                        MLXSW_SP_ACL_TCAM_PATTERNS_COUNT,
1735                        tmplt_elusage, false,
1736                        p_min_prio, p_max_prio);
1737     if (err)
1738         return err;
1739 
1740     /* For most of the TCAM clients it would make sense to take a tcam chunk
1741      * only when the first rule is written. This is not the case for
1742      * multicast router as it is required to bind the multicast router to a
1743      * specific ACL Group ID which must exist in HW before multicast router
1744      * is initialized.
1745      */
1746     ruleset->vchunk = mlxsw_sp_acl_tcam_vchunk_get(mlxsw_sp,
1747                                &ruleset->vgroup, 1,
1748                                tmplt_elusage);
1749     if (IS_ERR(ruleset->vchunk)) {
1750         err = PTR_ERR(ruleset->vchunk);
1751         goto err_chunk_get;
1752     }
1753 
1754     return 0;
1755 
1756 err_chunk_get:
1757     mlxsw_sp_acl_tcam_vgroup_del(&ruleset->vgroup);
1758     return err;
1759 }
1760 
1761 static void
1762 mlxsw_sp_acl_tcam_mr_ruleset_del(struct mlxsw_sp *mlxsw_sp, void *ruleset_priv)
1763 {
1764     struct mlxsw_sp_acl_tcam_mr_ruleset *ruleset = ruleset_priv;
1765 
1766     mlxsw_sp_acl_tcam_vchunk_put(mlxsw_sp, ruleset->vchunk);
1767     mlxsw_sp_acl_tcam_vgroup_del(&ruleset->vgroup);
1768 }
1769 
1770 static int
1771 mlxsw_sp_acl_tcam_mr_ruleset_bind(struct mlxsw_sp *mlxsw_sp, void *ruleset_priv,
1772                   struct mlxsw_sp_port *mlxsw_sp_port,
1773                   bool ingress)
1774 {
1775     /* Binding is done when initializing multicast router */
1776     return 0;
1777 }
1778 
1779 static void
1780 mlxsw_sp_acl_tcam_mr_ruleset_unbind(struct mlxsw_sp *mlxsw_sp,
1781                     void *ruleset_priv,
1782                     struct mlxsw_sp_port *mlxsw_sp_port,
1783                     bool ingress)
1784 {
1785 }
1786 
1787 static u16
1788 mlxsw_sp_acl_tcam_mr_ruleset_group_id(void *ruleset_priv)
1789 {
1790     struct mlxsw_sp_acl_tcam_mr_ruleset *ruleset = ruleset_priv;
1791 
1792     return mlxsw_sp_acl_tcam_group_id(&ruleset->vgroup.group);
1793 }
1794 
1795 static int
1796 mlxsw_sp_acl_tcam_mr_rule_add(struct mlxsw_sp *mlxsw_sp, void *ruleset_priv,
1797                   void *rule_priv,
1798                   struct mlxsw_sp_acl_rule_info *rulei)
1799 {
1800     struct mlxsw_sp_acl_tcam_mr_ruleset *ruleset = ruleset_priv;
1801     struct mlxsw_sp_acl_tcam_mr_rule *rule = rule_priv;
1802 
1803     return mlxsw_sp_acl_tcam_ventry_add(mlxsw_sp, &ruleset->vgroup,
1804                        &rule->ventry, rulei);
1805 }
1806 
1807 static void
1808 mlxsw_sp_acl_tcam_mr_rule_del(struct mlxsw_sp *mlxsw_sp, void *rule_priv)
1809 {
1810     struct mlxsw_sp_acl_tcam_mr_rule *rule = rule_priv;
1811 
1812     mlxsw_sp_acl_tcam_ventry_del(mlxsw_sp, &rule->ventry);
1813 }
1814 
1815 static int
1816 mlxsw_sp_acl_tcam_mr_rule_action_replace(struct mlxsw_sp *mlxsw_sp,
1817                      void *rule_priv,
1818                      struct mlxsw_sp_acl_rule_info *rulei)
1819 {
1820     struct mlxsw_sp_acl_tcam_mr_rule *rule = rule_priv;
1821 
1822     return mlxsw_sp_acl_tcam_ventry_action_replace(mlxsw_sp, &rule->ventry,
1823                                rulei);
1824 }
1825 
1826 static int
1827 mlxsw_sp_acl_tcam_mr_rule_activity_get(struct mlxsw_sp *mlxsw_sp,
1828                        void *rule_priv, bool *activity)
1829 {
1830     *activity = false;
1831 
1832     return 0;
1833 }
1834 
1835 static const struct mlxsw_sp_acl_profile_ops mlxsw_sp_acl_tcam_mr_ops = {
1836     .ruleset_priv_size  = sizeof(struct mlxsw_sp_acl_tcam_mr_ruleset),
1837     .ruleset_add        = mlxsw_sp_acl_tcam_mr_ruleset_add,
1838     .ruleset_del        = mlxsw_sp_acl_tcam_mr_ruleset_del,
1839     .ruleset_bind       = mlxsw_sp_acl_tcam_mr_ruleset_bind,
1840     .ruleset_unbind     = mlxsw_sp_acl_tcam_mr_ruleset_unbind,
1841     .ruleset_group_id   = mlxsw_sp_acl_tcam_mr_ruleset_group_id,
1842     .rule_priv_size     = sizeof(struct mlxsw_sp_acl_tcam_mr_rule),
1843     .rule_add       = mlxsw_sp_acl_tcam_mr_rule_add,
1844     .rule_del       = mlxsw_sp_acl_tcam_mr_rule_del,
1845     .rule_action_replace    = mlxsw_sp_acl_tcam_mr_rule_action_replace,
1846     .rule_activity_get  = mlxsw_sp_acl_tcam_mr_rule_activity_get,
1847 };
1848 
1849 static const struct mlxsw_sp_acl_profile_ops *
1850 mlxsw_sp_acl_tcam_profile_ops_arr[] = {
1851     [MLXSW_SP_ACL_PROFILE_FLOWER] = &mlxsw_sp_acl_tcam_flower_ops,
1852     [MLXSW_SP_ACL_PROFILE_MR] = &mlxsw_sp_acl_tcam_mr_ops,
1853 };
1854 
1855 const struct mlxsw_sp_acl_profile_ops *
1856 mlxsw_sp_acl_tcam_profile_ops(struct mlxsw_sp *mlxsw_sp,
1857                   enum mlxsw_sp_acl_profile profile)
1858 {
1859     const struct mlxsw_sp_acl_profile_ops *ops;
1860 
1861     if (WARN_ON(profile >= ARRAY_SIZE(mlxsw_sp_acl_tcam_profile_ops_arr)))
1862         return NULL;
1863     ops = mlxsw_sp_acl_tcam_profile_ops_arr[profile];
1864     if (WARN_ON(!ops))
1865         return NULL;
1866     return ops;
1867 }