mlxsw: spectrum_acl: Split TCAM handling 3 ways
authorJiri Pirko <jiri@mellanox.com>
Sun, 8 Jul 2018 20:51:20 +0000 (23:51 +0300)
committerDavid S. Miller <davem@davemloft.net>
Mon, 9 Jul 2018 23:24:17 +0000 (16:24 -0700)
To allow easy and clean Spectrum-2 implementation for things that differ
from Spectrum, split the existing ACL TCAM code 3 ways:
1) common code that calls Spectrum/Spectrum-2 specific ops
2) Spectrum ops implementations
3) common C-TCAM code that is going to be shared between Spectrum and
   Spectrum-2 implementations

Signed-off-by: Jiri Pirko <jiri@mellanox.com>
Signed-off-by: Ido Schimmel <idosch@mellanox.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/mellanox/mlxsw/Makefile
drivers/net/ethernet/mellanox/mlxsw/spectrum.c
drivers/net/ethernet/mellanox/mlxsw/spectrum.h
drivers/net/ethernet/mellanox/mlxsw/spectrum1_acl_tcam.c [new file with mode: 0644]
drivers/net/ethernet/mellanox/mlxsw/spectrum_acl.c
drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_ctcam.c [new file with mode: 0644]
drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_tcam.c
drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_tcam.h [new file with mode: 0644]

index cd9a281e0395441d19d7ec827eebaff875988830..54f1de4674ce6457b26a7859a4488e1f65780c29 100644 (file)
@@ -16,7 +16,9 @@ obj-$(CONFIG_MLXSW_SPECTRUM)  += mlxsw_spectrum.o
 mlxsw_spectrum-objs            := spectrum.o spectrum_buffers.o \
                                   spectrum_switchdev.o spectrum_router.o \
                                   spectrum1_kvdl.o spectrum_kvdl.o \
-                                  spectrum_acl_tcam.o spectrum_acl.o \
+                                  spectrum_acl_tcam.o spectrum_acl_ctcam.o \
+                                  spectrum1_acl_tcam.o \
+                                  spectrum_acl.o \
                                   spectrum_flower.o spectrum_cnt.o \
                                   spectrum_fid.o spectrum_ipip.o \
                                   spectrum_acl_flex_actions.o \
index ea72556b9e6a51fa62845c9619227331cbf2298a..5f5b476c0235af4e2f68cf699e289bdb44a204f6 100644 (file)
@@ -3624,6 +3624,7 @@ static int mlxsw_sp_init(struct mlxsw_core *mlxsw_core,
        mlxsw_sp->kvdl_ops = &mlxsw_sp1_kvdl_ops;
        mlxsw_sp->afa_ops = &mlxsw_sp1_act_afa_ops;
        mlxsw_sp->mr_tcam_ops = &mlxsw_sp1_mr_tcam_ops;
+       mlxsw_sp->acl_tcam_ops = &mlxsw_sp1_acl_tcam_ops;
 
        mlxsw_sp->core = mlxsw_core;
        mlxsw_sp->bus_info = mlxsw_bus_info;
index a3fff2ba1ccb9064956fb916c84dcef2b7e47d36..fa495f787d2230a067ab45ba1f8b05f19443a5cd 100644 (file)
@@ -147,6 +147,7 @@ struct mlxsw_sp_fid_core;
 struct mlxsw_sp_kvdl;
 struct mlxsw_sp_kvdl_ops;
 struct mlxsw_sp_mr_tcam_ops;
+struct mlxsw_sp_acl_tcam_ops;
 
 struct mlxsw_sp {
        struct mlxsw_sp_port **ports;
@@ -173,6 +174,7 @@ struct mlxsw_sp {
        const struct mlxsw_sp_kvdl_ops *kvdl_ops;
        const struct mlxsw_afa_ops *afa_ops;
        const struct mlxsw_sp_mr_tcam_ops *mr_tcam_ops;
+       const struct mlxsw_sp_acl_tcam_ops *acl_tcam_ops;
 };
 
 static inline struct mlxsw_sp_upper *
@@ -517,7 +519,7 @@ struct mlxsw_sp_acl_profile_ops {
                               struct mlxsw_sp_port *mlxsw_sp_port,
                               bool ingress);
        u16 (*ruleset_group_id)(void *ruleset_priv);
-       size_t rule_priv_size;
+       size_t (*rule_priv_size)(struct mlxsw_sp *mlxsw_sp);
        int (*rule_add)(struct mlxsw_sp *mlxsw_sp,
                        void *ruleset_priv, void *rule_priv,
                        struct mlxsw_sp_acl_rule_info *rulei);
@@ -631,7 +633,33 @@ int mlxsw_sp_acl_init(struct mlxsw_sp *mlxsw_sp);
 void mlxsw_sp_acl_fini(struct mlxsw_sp *mlxsw_sp);
 
 /* spectrum_acl_tcam.c */
-extern const struct mlxsw_sp_acl_ops mlxsw_sp_acl_tcam_ops;
+struct mlxsw_sp_acl_tcam_region;
+
+struct mlxsw_sp_acl_tcam_ops {
+       enum mlxsw_reg_ptar_key_type key_type;
+       size_t region_priv_size;
+       int (*region_init)(struct mlxsw_sp *mlxsw_sp, void *region_priv,
+                          struct mlxsw_sp_acl_tcam_region *region);
+       void (*region_fini)(struct mlxsw_sp *mlxsw_sp, void *region_priv);
+       size_t chunk_priv_size;
+       void (*chunk_init)(void *region_priv, void *chunk_priv,
+                          unsigned int priority);
+       void (*chunk_fini)(void *chunk_priv);
+       size_t entry_priv_size;
+       int (*entry_add)(struct mlxsw_sp *mlxsw_sp,
+                        void *region_priv, void *chunk_priv,
+                        void *entry_priv,
+                        struct mlxsw_sp_acl_rule_info *rulei);
+       void (*entry_del)(struct mlxsw_sp *mlxsw_sp,
+                         void *region_priv, void *chunk_priv,
+                         void *entry_priv);
+       int (*entry_activity_get)(struct mlxsw_sp *mlxsw_sp,
+                                 void *region_priv, void *entry_priv,
+                                 bool *activity);
+};
+
+/* spectrum1_acl_tcam.c */
+extern const struct mlxsw_sp_acl_tcam_ops mlxsw_sp1_acl_tcam_ops;
 
 /* spectrum_acl_flex_actions.c */
 extern const struct mlxsw_afa_ops mlxsw_sp1_act_afa_ops;
diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum1_acl_tcam.c b/drivers/net/ethernet/mellanox/mlxsw/spectrum1_acl_tcam.c
new file mode 100644 (file)
index 0000000..aa7e03b
--- /dev/null
@@ -0,0 +1,231 @@
+/*
+ * drivers/net/ethernet/mellanox/mlxsw/spectrum1_acl_tcam.c
+ * Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.
+ * Copyright (c) 2017-2018 Jiri Pirko <jiri@mellanox.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the names of the copyright holders nor the names of its
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * Alternatively, this software may be distributed under the terms of the
+ * GNU General Public License ("GPL") version 2 as published by the Free
+ * Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <linux/kernel.h>
+#include <linux/slab.h>
+
+#include "reg.h"
+#include "core.h"
+#include "spectrum.h"
+#include "spectrum_acl_tcam.h"
+
+struct mlxsw_sp1_acl_tcam_region {
+       struct mlxsw_sp_acl_ctcam_region cregion;
+       struct mlxsw_sp_acl_tcam_region *region;
+       struct {
+               struct mlxsw_sp_acl_ctcam_chunk cchunk;
+               struct mlxsw_sp_acl_ctcam_entry centry;
+               struct mlxsw_sp_acl_rule_info *rulei;
+       } catchall;
+};
+
+struct mlxsw_sp1_acl_tcam_chunk {
+       struct mlxsw_sp_acl_ctcam_chunk cchunk;
+};
+
+struct mlxsw_sp1_acl_tcam_entry {
+       struct mlxsw_sp_acl_ctcam_entry centry;
+};
+
+static int
+mlxsw_sp1_acl_ctcam_region_catchall_add(struct mlxsw_sp *mlxsw_sp,
+                                       struct mlxsw_sp1_acl_tcam_region *region)
+{
+       struct mlxsw_sp_acl_rule_info *rulei;
+       int err;
+
+       mlxsw_sp_acl_ctcam_chunk_init(&region->cregion,
+                                     &region->catchall.cchunk,
+                                     MLXSW_SP_ACL_TCAM_CATCHALL_PRIO);
+       rulei = mlxsw_sp_acl_rulei_create(mlxsw_sp->acl);
+       if (IS_ERR(rulei)) {
+               err = PTR_ERR(rulei);
+               goto err_rulei_create;
+       }
+       err = mlxsw_sp_acl_rulei_act_continue(rulei);
+       if (WARN_ON(err))
+               goto err_rulei_act_continue;
+       err = mlxsw_sp_acl_rulei_commit(rulei);
+       if (err)
+               goto err_rulei_commit;
+       err = mlxsw_sp_acl_ctcam_entry_add(mlxsw_sp, &region->cregion,
+                                          &region->catchall.cchunk,
+                                          &region->catchall.centry, rulei);
+       if (err)
+               goto err_entry_add;
+       region->catchall.rulei = rulei;
+       return 0;
+
+err_entry_add:
+err_rulei_commit:
+err_rulei_act_continue:
+       mlxsw_sp_acl_rulei_destroy(rulei);
+err_rulei_create:
+       mlxsw_sp_acl_ctcam_chunk_fini(&region->catchall.cchunk);
+       return err;
+}
+
+static void
+mlxsw_sp1_acl_ctcam_region_catchall_del(struct mlxsw_sp *mlxsw_sp,
+                                       struct mlxsw_sp1_acl_tcam_region *region)
+{
+       struct mlxsw_sp_acl_rule_info *rulei = region->catchall.rulei;
+
+       mlxsw_sp_acl_ctcam_entry_del(mlxsw_sp, &region->cregion,
+                                    &region->catchall.cchunk,
+                                    &region->catchall.centry);
+       mlxsw_sp_acl_rulei_destroy(rulei);
+       mlxsw_sp_acl_ctcam_chunk_fini(&region->catchall.cchunk);
+}
+
+static int
+mlxsw_sp1_acl_tcam_region_init(struct mlxsw_sp *mlxsw_sp, void *region_priv,
+                              struct mlxsw_sp_acl_tcam_region *_region)
+{
+       struct mlxsw_sp1_acl_tcam_region *region = region_priv;
+       int err;
+
+       err = mlxsw_sp_acl_ctcam_region_init(mlxsw_sp, &region->cregion,
+                                            _region);
+       if (err)
+               return err;
+       err = mlxsw_sp1_acl_ctcam_region_catchall_add(mlxsw_sp, region);
+       if (err)
+               goto err_catchall_add;
+       region->region = _region;
+       return 0;
+
+err_catchall_add:
+       mlxsw_sp_acl_ctcam_region_fini(&region->cregion);
+       return err;
+}
+
+static void
+mlxsw_sp1_acl_tcam_region_fini(struct mlxsw_sp *mlxsw_sp, void *region_priv)
+{
+       struct mlxsw_sp1_acl_tcam_region *region = region_priv;
+
+       mlxsw_sp1_acl_ctcam_region_catchall_del(mlxsw_sp, region);
+       mlxsw_sp_acl_ctcam_region_fini(&region->cregion);
+}
+
+static void mlxsw_sp1_acl_tcam_chunk_init(void *region_priv, void *chunk_priv,
+                                         unsigned int priority)
+{
+       struct mlxsw_sp1_acl_tcam_region *region = region_priv;
+       struct mlxsw_sp1_acl_tcam_chunk *chunk = chunk_priv;
+
+       mlxsw_sp_acl_ctcam_chunk_init(&region->cregion, &chunk->cchunk,
+                                     priority);
+}
+
+static void mlxsw_sp1_acl_tcam_chunk_fini(void *chunk_priv)
+{
+       struct mlxsw_sp1_acl_tcam_chunk *chunk = chunk_priv;
+
+       mlxsw_sp_acl_ctcam_chunk_fini(&chunk->cchunk);
+}
+
+static int mlxsw_sp1_acl_tcam_entry_add(struct mlxsw_sp *mlxsw_sp,
+                                       void *region_priv, void *chunk_priv,
+                                       void *entry_priv,
+                                       struct mlxsw_sp_acl_rule_info *rulei)
+{
+       struct mlxsw_sp1_acl_tcam_region *region = region_priv;
+       struct mlxsw_sp1_acl_tcam_chunk *chunk = chunk_priv;
+       struct mlxsw_sp1_acl_tcam_entry *entry = entry_priv;
+
+       return mlxsw_sp_acl_ctcam_entry_add(mlxsw_sp, &region->cregion,
+                                           &chunk->cchunk, &entry->centry,
+                                           rulei);
+}
+
+static void mlxsw_sp1_acl_tcam_entry_del(struct mlxsw_sp *mlxsw_sp,
+                                        void *region_priv, void *chunk_priv,
+                                        void *entry_priv)
+{
+       struct mlxsw_sp1_acl_tcam_region *region = region_priv;
+       struct mlxsw_sp1_acl_tcam_chunk *chunk = chunk_priv;
+       struct mlxsw_sp1_acl_tcam_entry *entry = entry_priv;
+
+       mlxsw_sp_acl_ctcam_entry_del(mlxsw_sp, &region->cregion,
+                                    &chunk->cchunk, &entry->centry);
+}
+
+static int
+mlxsw_sp1_acl_tcam_region_entry_activity_get(struct mlxsw_sp *mlxsw_sp,
+                                            struct mlxsw_sp_acl_tcam_region *_region,
+                                            unsigned int offset,
+                                            bool *activity)
+{
+       char ptce2_pl[MLXSW_REG_PTCE2_LEN];
+       int err;
+
+       mlxsw_reg_ptce2_pack(ptce2_pl, true, MLXSW_REG_PTCE2_OP_QUERY_CLEAR_ON_READ,
+                            _region->tcam_region_info, offset);
+       err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
+       if (err)
+               return err;
+       *activity = mlxsw_reg_ptce2_a_get(ptce2_pl);
+       return 0;
+}
+
+static int
+mlxsw_sp1_acl_tcam_entry_activity_get(struct mlxsw_sp *mlxsw_sp,
+                                     void *region_priv, void *entry_priv,
+                                     bool *activity)
+{
+       struct mlxsw_sp1_acl_tcam_region *region = region_priv;
+       struct mlxsw_sp1_acl_tcam_entry *entry = entry_priv;
+       unsigned int offset;
+
+       offset = mlxsw_sp_acl_ctcam_entry_offset(&entry->centry);
+       return mlxsw_sp1_acl_tcam_region_entry_activity_get(mlxsw_sp,
+                                                           region->region,
+                                                           offset, activity);
+}
+
+const struct mlxsw_sp_acl_tcam_ops mlxsw_sp1_acl_tcam_ops = {
+       .key_type               = MLXSW_REG_PTAR_KEY_TYPE_FLEX,
+       .region_priv_size       = sizeof(struct mlxsw_sp1_acl_tcam_region),
+       .region_init            = mlxsw_sp1_acl_tcam_region_init,
+       .region_fini            = mlxsw_sp1_acl_tcam_region_fini,
+       .chunk_priv_size        = sizeof(struct mlxsw_sp1_acl_tcam_chunk),
+       .chunk_init             = mlxsw_sp1_acl_tcam_chunk_init,
+       .chunk_fini             = mlxsw_sp1_acl_tcam_chunk_fini,
+       .entry_priv_size        = sizeof(struct mlxsw_sp1_acl_tcam_entry),
+       .entry_add              = mlxsw_sp1_acl_tcam_entry_add,
+       .entry_del              = mlxsw_sp1_acl_tcam_entry_del,
+       .entry_activity_get     = mlxsw_sp1_acl_tcam_entry_activity_get,
+};
index b40569c67ddfc1e45048d51e4ad38ca1df280397..1bf963cd1f12114835550dc7174c0a1fc14a7664 100644 (file)
@@ -48,6 +48,7 @@
 #include "spectrum.h"
 #include "core_acl_flex_keys.h"
 #include "core_acl_flex_actions.h"
+#include "spectrum_acl_tcam.h"
 #include "spectrum_acl_flex_keys.h"
 
 struct mlxsw_sp_acl {
@@ -634,7 +635,8 @@ mlxsw_sp_acl_rule_create(struct mlxsw_sp *mlxsw_sp,
        int err;
 
        mlxsw_sp_acl_ruleset_ref_inc(ruleset);
-       rule = kzalloc(sizeof(*rule) + ops->rule_priv_size, GFP_KERNEL);
+       rule = kzalloc(sizeof(*rule) + ops->rule_priv_size(mlxsw_sp),
+                      GFP_KERNEL);
        if (!rule) {
                err = -ENOMEM;
                goto err_alloc;
diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_ctcam.c b/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_ctcam.c
new file mode 100644 (file)
index 0000000..c5cbec5
--- /dev/null
@@ -0,0 +1,205 @@
+/*
+ * drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_ctcam.c
+ * Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.
+ * Copyright (c) 2017-2018 Jiri Pirko <jiri@mellanox.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the names of the copyright holders nor the names of its
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * Alternatively, this software may be distributed under the terms of the
+ * GNU General Public License ("GPL") version 2 as published by the Free
+ * Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/parman.h>
+
+#include "reg.h"
+#include "core.h"
+#include "spectrum.h"
+#include "spectrum_acl_tcam.h"
+
+static int
+mlxsw_sp_acl_ctcam_region_resize(struct mlxsw_sp *mlxsw_sp,
+                                struct mlxsw_sp_acl_tcam_region *region,
+                                u16 new_size)
+{
+       char ptar_pl[MLXSW_REG_PTAR_LEN];
+
+       mlxsw_reg_ptar_pack(ptar_pl, MLXSW_REG_PTAR_OP_RESIZE,
+                           region->key_type, new_size, region->id,
+                           region->tcam_region_info);
+       return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptar), ptar_pl);
+}
+
+static void
+mlxsw_sp_acl_ctcam_region_move(struct mlxsw_sp *mlxsw_sp,
+                              struct mlxsw_sp_acl_tcam_region *region,
+                              u16 src_offset, u16 dst_offset, u16 size)
+{
+       char prcr_pl[MLXSW_REG_PRCR_LEN];
+
+       mlxsw_reg_prcr_pack(prcr_pl, MLXSW_REG_PRCR_OP_MOVE,
+                           region->tcam_region_info, src_offset,
+                           region->tcam_region_info, dst_offset, size);
+       mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(prcr), prcr_pl);
+}
+
+static int
+mlxsw_sp_acl_ctcam_region_entry_insert(struct mlxsw_sp *mlxsw_sp,
+                                      struct mlxsw_sp_acl_tcam_region *region,
+                                      unsigned int offset,
+                                      struct mlxsw_sp_acl_rule_info *rulei)
+{
+       char ptce2_pl[MLXSW_REG_PTCE2_LEN];
+       char *act_set;
+       char *mask;
+       char *key;
+
+       mlxsw_reg_ptce2_pack(ptce2_pl, true, MLXSW_REG_PTCE2_OP_WRITE_WRITE,
+                            region->tcam_region_info, offset);
+       key = mlxsw_reg_ptce2_flex_key_blocks_data(ptce2_pl);
+       mask = mlxsw_reg_ptce2_mask_data(ptce2_pl);
+       mlxsw_afk_encode(region->key_info, &rulei->values, key, mask);
+
+       /* Only the first action set belongs here, the rest is in KVD */
+       act_set = mlxsw_afa_block_first_set(rulei->act_block);
+       mlxsw_reg_ptce2_flex_action_set_memcpy_to(ptce2_pl, act_set);
+
+       return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
+}
+
+static void
+mlxsw_sp_acl_ctcam_region_entry_remove(struct mlxsw_sp *mlxsw_sp,
+                                      struct mlxsw_sp_acl_tcam_region *region,
+                                      unsigned int offset)
+{
+       char ptce2_pl[MLXSW_REG_PTCE2_LEN];
+
+       mlxsw_reg_ptce2_pack(ptce2_pl, false, MLXSW_REG_PTCE2_OP_WRITE_WRITE,
+                            region->tcam_region_info, offset);
+       mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
+}
+
+static int mlxsw_sp_acl_ctcam_region_parman_resize(void *priv,
+                                                  unsigned long new_count)
+{
+       struct mlxsw_sp_acl_ctcam_region *cregion = priv;
+       struct mlxsw_sp_acl_tcam_region *region = cregion->region;
+       struct mlxsw_sp *mlxsw_sp = region->mlxsw_sp;
+       u64 max_tcam_rules;
+
+       max_tcam_rules = MLXSW_CORE_RES_GET(mlxsw_sp->core, ACL_MAX_TCAM_RULES);
+       if (new_count > max_tcam_rules)
+               return -EINVAL;
+       return mlxsw_sp_acl_ctcam_region_resize(mlxsw_sp, region, new_count);
+}
+
+static void mlxsw_sp_acl_ctcam_region_parman_move(void *priv,
+                                                 unsigned long from_index,
+                                                 unsigned long to_index,
+                                                 unsigned long count)
+{
+       struct mlxsw_sp_acl_ctcam_region *cregion = priv;
+       struct mlxsw_sp_acl_tcam_region *region = cregion->region;
+       struct mlxsw_sp *mlxsw_sp = region->mlxsw_sp;
+
+       mlxsw_sp_acl_ctcam_region_move(mlxsw_sp, region,
+                                      from_index, to_index, count);
+}
+
+static const struct parman_ops mlxsw_sp_acl_ctcam_region_parman_ops = {
+       .base_count     = MLXSW_SP_ACL_TCAM_REGION_BASE_COUNT,
+       .resize_step    = MLXSW_SP_ACL_TCAM_REGION_RESIZE_STEP,
+       .resize         = mlxsw_sp_acl_ctcam_region_parman_resize,
+       .move           = mlxsw_sp_acl_ctcam_region_parman_move,
+       .algo           = PARMAN_ALGO_TYPE_LSORT,
+};
+
+int mlxsw_sp_acl_ctcam_region_init(struct mlxsw_sp *mlxsw_sp,
+                                  struct mlxsw_sp_acl_ctcam_region *cregion,
+                                  struct mlxsw_sp_acl_tcam_region *region)
+{
+       cregion->region = region;
+       cregion->parman = parman_create(&mlxsw_sp_acl_ctcam_region_parman_ops,
+                                       cregion);
+       if (!cregion->parman)
+               return -ENOMEM;
+       return 0;
+}
+
+void mlxsw_sp_acl_ctcam_region_fini(struct mlxsw_sp_acl_ctcam_region *cregion)
+{
+       parman_destroy(cregion->parman);
+}
+
+void mlxsw_sp_acl_ctcam_chunk_init(struct mlxsw_sp_acl_ctcam_region *cregion,
+                                  struct mlxsw_sp_acl_ctcam_chunk *cchunk,
+                                  unsigned int priority)
+{
+       parman_prio_init(cregion->parman, &cchunk->parman_prio, priority);
+}
+
+void mlxsw_sp_acl_ctcam_chunk_fini(struct mlxsw_sp_acl_ctcam_chunk *cchunk)
+{
+       parman_prio_fini(&cchunk->parman_prio);
+}
+
+int mlxsw_sp_acl_ctcam_entry_add(struct mlxsw_sp *mlxsw_sp,
+                                struct mlxsw_sp_acl_ctcam_region *cregion,
+                                struct mlxsw_sp_acl_ctcam_chunk *cchunk,
+                                struct mlxsw_sp_acl_ctcam_entry *centry,
+                                struct mlxsw_sp_acl_rule_info *rulei)
+{
+       int err;
+
+       err = parman_item_add(cregion->parman, &cchunk->parman_prio,
+                             &centry->parman_item);
+       if (err)
+               return err;
+
+       err = mlxsw_sp_acl_ctcam_region_entry_insert(mlxsw_sp, cregion->region,
+                                                    centry->parman_item.index,
+                                                    rulei);
+       if (err)
+               goto err_rule_insert;
+       return 0;
+
+err_rule_insert:
+       parman_item_remove(cregion->parman, &cchunk->parman_prio,
+                          &centry->parman_item);
+       return err;
+}
+
+void mlxsw_sp_acl_ctcam_entry_del(struct mlxsw_sp *mlxsw_sp,
+                                 struct mlxsw_sp_acl_ctcam_region *cregion,
+                                 struct mlxsw_sp_acl_ctcam_chunk *cchunk,
+                                 struct mlxsw_sp_acl_ctcam_entry *centry)
+{
+       mlxsw_sp_acl_ctcam_region_entry_remove(mlxsw_sp, cregion->region,
+                                              centry->parman_item.index);
+       parman_item_remove(cregion->parman, &cchunk->parman_prio,
+                          &centry->parman_item);
+}
index efbd2062b6ec97f6aecd4801611d70e56f26bf95..d2e3d9cec8057ac9789863ba3316e801d1015526 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_tcam.c
- * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
- * Copyright (c) 2017 Jiri Pirko <jiri@mellanox.com>
+ * Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.
+ * Copyright (c) 2017-2018 Jiri Pirko <jiri@mellanox.com>
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
 #include <linux/list.h>
 #include <linux/rhashtable.h>
 #include <linux/netdevice.h>
-#include <linux/parman.h>
 
 #include "reg.h"
 #include "core.h"
 #include "resources.h"
 #include "spectrum.h"
+#include "spectrum_acl_tcam.h"
 #include "core_acl_flex_keys.h"
 
 struct mlxsw_sp_acl_tcam {
@@ -159,36 +159,21 @@ struct mlxsw_sp_acl_tcam_group {
        unsigned int patterns_count;
 };
 
-struct mlxsw_sp_acl_tcam_region {
-       struct list_head list; /* Member of a TCAM group */
-       struct list_head chunk_list; /* List of chunks under this region */
-       struct parman *parman;
-       struct mlxsw_sp *mlxsw_sp;
-       struct mlxsw_sp_acl_tcam_group *group;
-       enum mlxsw_reg_ptar_key_type key_type;
-       u16 id; /* ACL ID and region ID - they are same */
-       char tcam_region_info[MLXSW_REG_PXXX_TCAM_REGION_INFO_LEN];
-       struct mlxsw_afk_key_info *key_info;
-       struct {
-               struct parman_prio parman_prio;
-               struct parman_item parman_item;
-               struct mlxsw_sp_acl_rule_info *rulei;
-       } catchall;
-};
-
 struct mlxsw_sp_acl_tcam_chunk {
        struct list_head list; /* Member of a TCAM region */
        struct rhash_head ht_node; /* Member of a chunk HT */
        unsigned int priority; /* Priority within the region and group */
-       struct parman_prio parman_prio;
        struct mlxsw_sp_acl_tcam_group *group;
        struct mlxsw_sp_acl_tcam_region *region;
        unsigned int ref_count;
+       unsigned long priv[0];
+       /* priv has to be always the last item */
 };
 
 struct mlxsw_sp_acl_tcam_entry {
-       struct parman_item parman_item;
        struct mlxsw_sp_acl_tcam_chunk *chunk;
+       unsigned long priv[0];
+       /* priv has to be always the last item */
 };
 
 static const struct rhashtable_params mlxsw_sp_acl_tcam_chunk_ht_params = {
@@ -442,9 +427,6 @@ mlxsw_sp_acl_tcam_group_use_patterns(struct mlxsw_sp_acl_tcam_group *group,
        memcpy(out, elusage, sizeof(*out));
 }
 
-#define MLXSW_SP_ACL_TCAM_REGION_BASE_COUNT 16
-#define MLXSW_SP_ACL_TCAM_REGION_RESIZE_STEP 16
-
 static int
 mlxsw_sp_acl_tcam_region_alloc(struct mlxsw_sp *mlxsw_sp,
                               struct mlxsw_sp_acl_tcam_region *region)
@@ -485,19 +467,6 @@ mlxsw_sp_acl_tcam_region_free(struct mlxsw_sp *mlxsw_sp,
        mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptar), ptar_pl);
 }
 
-static int
-mlxsw_sp_acl_tcam_region_resize(struct mlxsw_sp *mlxsw_sp,
-                               struct mlxsw_sp_acl_tcam_region *region,
-                               u16 new_size)
-{
-       char ptar_pl[MLXSW_REG_PTAR_LEN];
-
-       mlxsw_reg_ptar_pack(ptar_pl, MLXSW_REG_PTAR_OP_RESIZE,
-                           region->key_type, new_size, region->id,
-                           region->tcam_region_info);
-       return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptar), ptar_pl);
-}
-
 static int
 mlxsw_sp_acl_tcam_region_enable(struct mlxsw_sp *mlxsw_sp,
                                struct mlxsw_sp_acl_tcam_region *region)
@@ -520,193 +489,22 @@ mlxsw_sp_acl_tcam_region_disable(struct mlxsw_sp *mlxsw_sp,
        mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pacl), pacl_pl);
 }
 
-static int
-mlxsw_sp_acl_tcam_region_entry_insert(struct mlxsw_sp *mlxsw_sp,
-                                     struct mlxsw_sp_acl_tcam_region *region,
-                                     unsigned int offset,
-                                     struct mlxsw_sp_acl_rule_info *rulei)
-{
-       char ptce2_pl[MLXSW_REG_PTCE2_LEN];
-       char *act_set;
-       char *mask;
-       char *key;
-
-       mlxsw_reg_ptce2_pack(ptce2_pl, true, MLXSW_REG_PTCE2_OP_WRITE_WRITE,
-                            region->tcam_region_info, offset);
-       key = mlxsw_reg_ptce2_flex_key_blocks_data(ptce2_pl);
-       mask = mlxsw_reg_ptce2_mask_data(ptce2_pl);
-       mlxsw_afk_encode(region->key_info, &rulei->values, key, mask);
-
-       /* Only the first action set belongs here, the rest is in KVD */
-       act_set = mlxsw_afa_block_first_set(rulei->act_block);
-       mlxsw_reg_ptce2_flex_action_set_memcpy_to(ptce2_pl, act_set);
-
-       return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
-}
-
-static void
-mlxsw_sp_acl_tcam_region_entry_remove(struct mlxsw_sp *mlxsw_sp,
-                                     struct mlxsw_sp_acl_tcam_region *region,
-                                     unsigned int offset)
-{
-       char ptce2_pl[MLXSW_REG_PTCE2_LEN];
-
-       mlxsw_reg_ptce2_pack(ptce2_pl, false, MLXSW_REG_PTCE2_OP_WRITE_WRITE,
-                            region->tcam_region_info, offset);
-       mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
-}
-
-static int
-mlxsw_sp_acl_tcam_region_entry_activity_get(struct mlxsw_sp *mlxsw_sp,
-                                           struct mlxsw_sp_acl_tcam_region *region,
-                                           unsigned int offset,
-                                           bool *activity)
-{
-       char ptce2_pl[MLXSW_REG_PTCE2_LEN];
-       int err;
-
-       mlxsw_reg_ptce2_pack(ptce2_pl, true, MLXSW_REG_PTCE2_OP_QUERY_CLEAR_ON_READ,
-                            region->tcam_region_info, offset);
-       err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptce2), ptce2_pl);
-       if (err)
-               return err;
-       *activity = mlxsw_reg_ptce2_a_get(ptce2_pl);
-       return 0;
-}
-
-#define MLXSW_SP_ACL_TCAM_CATCHALL_PRIO (~0U)
-
-static int
-mlxsw_sp_acl_tcam_region_catchall_add(struct mlxsw_sp *mlxsw_sp,
-                                     struct mlxsw_sp_acl_tcam_region *region)
-{
-       struct parman_prio *parman_prio = &region->catchall.parman_prio;
-       struct parman_item *parman_item = &region->catchall.parman_item;
-       struct mlxsw_sp_acl_rule_info *rulei;
-       int err;
-
-       parman_prio_init(region->parman, parman_prio,
-                        MLXSW_SP_ACL_TCAM_CATCHALL_PRIO);
-       err = parman_item_add(region->parman, parman_prio, parman_item);
-       if (err)
-               goto err_parman_item_add;
-
-       rulei = mlxsw_sp_acl_rulei_create(mlxsw_sp->acl);
-       if (IS_ERR(rulei)) {
-               err = PTR_ERR(rulei);
-               goto err_rulei_create;
-       }
-
-       err = mlxsw_sp_acl_rulei_act_continue(rulei);
-       if (WARN_ON(err))
-               goto err_rulei_act_continue;
-
-       err = mlxsw_sp_acl_rulei_commit(rulei);
-       if (err)
-               goto err_rulei_commit;
-
-       err = mlxsw_sp_acl_tcam_region_entry_insert(mlxsw_sp, region,
-                                                   parman_item->index, rulei);
-       region->catchall.rulei = rulei;
-       if (err)
-               goto err_rule_insert;
-
-       return 0;
-
-err_rule_insert:
-err_rulei_commit:
-err_rulei_act_continue:
-       mlxsw_sp_acl_rulei_destroy(rulei);
-err_rulei_create:
-       parman_item_remove(region->parman, parman_prio, parman_item);
-err_parman_item_add:
-       parman_prio_fini(parman_prio);
-       return err;
-}
-
-static void
-mlxsw_sp_acl_tcam_region_catchall_del(struct mlxsw_sp *mlxsw_sp,
-                                     struct mlxsw_sp_acl_tcam_region *region)
-{
-       struct parman_prio *parman_prio = &region->catchall.parman_prio;
-       struct parman_item *parman_item = &region->catchall.parman_item;
-       struct mlxsw_sp_acl_rule_info *rulei = region->catchall.rulei;
-
-       mlxsw_sp_acl_tcam_region_entry_remove(mlxsw_sp, region,
-                                             parman_item->index);
-       mlxsw_sp_acl_rulei_destroy(rulei);
-       parman_item_remove(region->parman, parman_prio, parman_item);
-       parman_prio_fini(parman_prio);
-}
-
-static void
-mlxsw_sp_acl_tcam_region_move(struct mlxsw_sp *mlxsw_sp,
-                             struct mlxsw_sp_acl_tcam_region *region,
-                             u16 src_offset, u16 dst_offset, u16 size)
-{
-       char prcr_pl[MLXSW_REG_PRCR_LEN];
-
-       mlxsw_reg_prcr_pack(prcr_pl, MLXSW_REG_PRCR_OP_MOVE,
-                           region->tcam_region_info, src_offset,
-                           region->tcam_region_info, dst_offset, size);
-       mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(prcr), prcr_pl);
-}
-
-static int mlxsw_sp_acl_tcam_region_parman_resize(void *priv,
-                                                 unsigned long new_count)
-{
-       struct mlxsw_sp_acl_tcam_region *region = priv;
-       struct mlxsw_sp *mlxsw_sp = region->mlxsw_sp;
-       u64 max_tcam_rules;
-
-       max_tcam_rules = MLXSW_CORE_RES_GET(mlxsw_sp->core, ACL_MAX_TCAM_RULES);
-       if (new_count > max_tcam_rules)
-               return -EINVAL;
-       return mlxsw_sp_acl_tcam_region_resize(mlxsw_sp, region, new_count);
-}
-
-static void mlxsw_sp_acl_tcam_region_parman_move(void *priv,
-                                                unsigned long from_index,
-                                                unsigned long to_index,
-                                                unsigned long count)
-{
-       struct mlxsw_sp_acl_tcam_region *region = priv;
-       struct mlxsw_sp *mlxsw_sp = region->mlxsw_sp;
-
-       mlxsw_sp_acl_tcam_region_move(mlxsw_sp, region,
-                                     from_index, to_index, count);
-}
-
-static const struct parman_ops mlxsw_sp_acl_tcam_region_parman_ops = {
-       .base_count     = MLXSW_SP_ACL_TCAM_REGION_BASE_COUNT,
-       .resize_step    = MLXSW_SP_ACL_TCAM_REGION_RESIZE_STEP,
-       .resize         = mlxsw_sp_acl_tcam_region_parman_resize,
-       .move           = mlxsw_sp_acl_tcam_region_parman_move,
-       .algo           = PARMAN_ALGO_TYPE_LSORT,
-};
-
 static struct mlxsw_sp_acl_tcam_region *
 mlxsw_sp_acl_tcam_region_create(struct mlxsw_sp *mlxsw_sp,
                                struct mlxsw_sp_acl_tcam *tcam,
                                struct mlxsw_afk_element_usage *elusage)
 {
+       const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
        struct mlxsw_afk *afk = mlxsw_sp_acl_afk(mlxsw_sp->acl);
        struct mlxsw_sp_acl_tcam_region *region;
        int err;
 
-       region = kzalloc(sizeof(*region), GFP_KERNEL);
+       region = kzalloc(sizeof(*region) + ops->region_priv_size, GFP_KERNEL);
        if (!region)
                return ERR_PTR(-ENOMEM);
        INIT_LIST_HEAD(&region->chunk_list);
        region->mlxsw_sp = mlxsw_sp;
 
-       region->parman = parman_create(&mlxsw_sp_acl_tcam_region_parman_ops,
-                                      region);
-       if (!region->parman) {
-               err = -ENOMEM;
-               goto err_parman_create;
-       }
-
        region->key_info = mlxsw_afk_key_info_get(afk, elusage);
        if (IS_ERR(region->key_info)) {
                err = PTR_ERR(region->key_info);
@@ -717,7 +515,7 @@ mlxsw_sp_acl_tcam_region_create(struct mlxsw_sp *mlxsw_sp,
        if (err)
                goto err_region_id_get;
 
-       region->key_type = MLXSW_REG_PTAR_KEY_TYPE_FLEX;
+       region->key_type = ops->key_type;
        err = mlxsw_sp_acl_tcam_region_alloc(mlxsw_sp, region);
        if (err)
                goto err_tcam_region_alloc;
@@ -726,13 +524,13 @@ mlxsw_sp_acl_tcam_region_create(struct mlxsw_sp *mlxsw_sp,
        if (err)
                goto err_tcam_region_enable;
 
-       err = mlxsw_sp_acl_tcam_region_catchall_add(mlxsw_sp, region);
+       err = ops->region_init(mlxsw_sp, region->priv, region);
        if (err)
-               goto err_tcam_region_catchall_add;
+               goto err_tcam_region_init;
 
        return region;
 
-err_tcam_region_catchall_add:
+err_tcam_region_init:
        mlxsw_sp_acl_tcam_region_disable(mlxsw_sp, region);
 err_tcam_region_enable:
        mlxsw_sp_acl_tcam_region_free(mlxsw_sp, region);
@@ -741,8 +539,6 @@ err_tcam_region_alloc:
 err_region_id_get:
        mlxsw_afk_key_info_put(region->key_info);
 err_key_info_get:
-       parman_destroy(region->parman);
-err_parman_create:
        kfree(region);
        return ERR_PTR(err);
 }
@@ -751,12 +547,13 @@ static void
 mlxsw_sp_acl_tcam_region_destroy(struct mlxsw_sp *mlxsw_sp,
                                 struct mlxsw_sp_acl_tcam_region *region)
 {
-       mlxsw_sp_acl_tcam_region_catchall_del(mlxsw_sp, region);
+       const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
+
+       ops->region_fini(mlxsw_sp, region->priv);
        mlxsw_sp_acl_tcam_region_disable(mlxsw_sp, region);
        mlxsw_sp_acl_tcam_region_free(mlxsw_sp, region);
        mlxsw_sp_acl_tcam_region_id_put(region->group->tcam, region->id);
        mlxsw_afk_key_info_put(region->key_info);
-       parman_destroy(region->parman);
        kfree(region);
 }
 
@@ -831,13 +628,14 @@ mlxsw_sp_acl_tcam_chunk_create(struct mlxsw_sp *mlxsw_sp,
                               unsigned int priority,
                               struct mlxsw_afk_element_usage *elusage)
 {
+       const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
        struct mlxsw_sp_acl_tcam_chunk *chunk;
        int err;
 
        if (priority == MLXSW_SP_ACL_TCAM_CATCHALL_PRIO)
                return ERR_PTR(-EINVAL);
 
-       chunk = kzalloc(sizeof(*chunk), GFP_KERNEL);
+       chunk = kzalloc(sizeof(*chunk) + ops->chunk_priv_size, GFP_KERNEL);
        if (!chunk)
                return ERR_PTR(-ENOMEM);
        chunk->priority = priority;
@@ -849,7 +647,7 @@ mlxsw_sp_acl_tcam_chunk_create(struct mlxsw_sp *mlxsw_sp,
        if (err)
                goto err_chunk_assoc;
 
-       parman_prio_init(chunk->region->parman, &chunk->parman_prio, priority);
+       ops->chunk_init(chunk->region->priv, chunk->priv, priority);
 
        err = rhashtable_insert_fast(&group->chunk_ht, &chunk->ht_node,
                                     mlxsw_sp_acl_tcam_chunk_ht_params);
@@ -859,7 +657,7 @@ mlxsw_sp_acl_tcam_chunk_create(struct mlxsw_sp *mlxsw_sp,
        return chunk;
 
 err_rhashtable_insert:
-       parman_prio_fini(&chunk->parman_prio);
+       ops->chunk_fini(chunk->priv);
        mlxsw_sp_acl_tcam_chunk_deassoc(mlxsw_sp, chunk);
 err_chunk_assoc:
        kfree(chunk);
@@ -870,11 +668,12 @@ static void
 mlxsw_sp_acl_tcam_chunk_destroy(struct mlxsw_sp *mlxsw_sp,
                                struct mlxsw_sp_acl_tcam_chunk *chunk)
 {
+       const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
        struct mlxsw_sp_acl_tcam_group *group = chunk->group;
 
        rhashtable_remove_fast(&group->chunk_ht, &chunk->ht_node,
                               mlxsw_sp_acl_tcam_chunk_ht_params);
-       parman_prio_fini(&chunk->parman_prio);
+       ops->chunk_fini(chunk->priv);
        mlxsw_sp_acl_tcam_chunk_deassoc(mlxsw_sp, chunk);
        kfree(chunk);
 }
@@ -908,11 +707,19 @@ static void mlxsw_sp_acl_tcam_chunk_put(struct mlxsw_sp *mlxsw_sp,
        mlxsw_sp_acl_tcam_chunk_destroy(mlxsw_sp, chunk);
 }
 
+static size_t mlxsw_sp_acl_tcam_entry_priv_size(struct mlxsw_sp *mlxsw_sp)
+{
+       const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
+
+       return ops->entry_priv_size;
+}
+
 static int mlxsw_sp_acl_tcam_entry_add(struct mlxsw_sp *mlxsw_sp,
                                       struct mlxsw_sp_acl_tcam_group *group,
                                       struct mlxsw_sp_acl_tcam_entry *entry,
                                       struct mlxsw_sp_acl_rule_info *rulei)
 {
+       const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
        struct mlxsw_sp_acl_tcam_chunk *chunk;
        struct mlxsw_sp_acl_tcam_region *region;
        int err;
@@ -923,24 +730,16 @@ static int mlxsw_sp_acl_tcam_entry_add(struct mlxsw_sp *mlxsw_sp,
                return PTR_ERR(chunk);
 
        region = chunk->region;
-       err = parman_item_add(region->parman, &chunk->parman_prio,
-                             &entry->parman_item);
-       if (err)
-               goto err_parman_item_add;
 
-       err = mlxsw_sp_acl_tcam_region_entry_insert(mlxsw_sp, region,
-                                                   entry->parman_item.index,
-                                                   rulei);
+       err = ops->entry_add(mlxsw_sp, region->priv, chunk->priv,
+                            entry->priv, rulei);
        if (err)
-               goto err_rule_insert;
+               goto err_entry_add;
        entry->chunk = chunk;
 
        return 0;
 
-err_rule_insert:
-       parman_item_remove(region->parman, &chunk->parman_prio,
-                          &entry->parman_item);
-err_parman_item_add:
+err_entry_add:
        mlxsw_sp_acl_tcam_chunk_put(mlxsw_sp, chunk);
        return err;
 }
@@ -948,13 +747,11 @@ err_parman_item_add:
 static void mlxsw_sp_acl_tcam_entry_del(struct mlxsw_sp *mlxsw_sp,
                                        struct mlxsw_sp_acl_tcam_entry *entry)
 {
+       const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
        struct mlxsw_sp_acl_tcam_chunk *chunk = entry->chunk;
        struct mlxsw_sp_acl_tcam_region *region = chunk->region;
 
-       mlxsw_sp_acl_tcam_region_entry_remove(mlxsw_sp, region,
-                                             entry->parman_item.index);
-       parman_item_remove(region->parman, &chunk->parman_prio,
-                          &entry->parman_item);
+       ops->entry_del(mlxsw_sp, region->priv, chunk->priv, entry->priv);
        mlxsw_sp_acl_tcam_chunk_put(mlxsw_sp, chunk);
 }
 
@@ -963,12 +760,12 @@ mlxsw_sp_acl_tcam_entry_activity_get(struct mlxsw_sp *mlxsw_sp,
                                     struct mlxsw_sp_acl_tcam_entry *entry,
                                     bool *activity)
 {
+       const struct mlxsw_sp_acl_tcam_ops *ops = mlxsw_sp->acl_tcam_ops;
        struct mlxsw_sp_acl_tcam_chunk *chunk = entry->chunk;
        struct mlxsw_sp_acl_tcam_region *region = chunk->region;
 
-       return mlxsw_sp_acl_tcam_region_entry_activity_get(mlxsw_sp, region,
-                                                          entry->parman_item.index,
-                                                          activity);
+       return ops->entry_activity_get(mlxsw_sp, region->priv,
+                                      entry->priv, activity);
 }
 
 static const enum mlxsw_afk_element mlxsw_sp_acl_tcam_pattern_ipv4[] = {
@@ -1081,6 +878,12 @@ mlxsw_sp_acl_tcam_flower_ruleset_group_id(void *ruleset_priv)
        return mlxsw_sp_acl_tcam_group_id(&ruleset->group);
 }
 
+static size_t mlxsw_sp_acl_tcam_flower_rule_priv_size(struct mlxsw_sp *mlxsw_sp)
+{
+       return sizeof(struct mlxsw_sp_acl_tcam_flower_rule) +
+              mlxsw_sp_acl_tcam_entry_priv_size(mlxsw_sp);
+}
+
 static int
 mlxsw_sp_acl_tcam_flower_rule_add(struct mlxsw_sp *mlxsw_sp,
                                  void *ruleset_priv, void *rule_priv,
@@ -1118,7 +921,7 @@ static const struct mlxsw_sp_acl_profile_ops mlxsw_sp_acl_tcam_flower_ops = {
        .ruleset_bind           = mlxsw_sp_acl_tcam_flower_ruleset_bind,
        .ruleset_unbind         = mlxsw_sp_acl_tcam_flower_ruleset_unbind,
        .ruleset_group_id       = mlxsw_sp_acl_tcam_flower_ruleset_group_id,
-       .rule_priv_size         = sizeof(struct mlxsw_sp_acl_tcam_flower_rule),
+       .rule_priv_size         = mlxsw_sp_acl_tcam_flower_rule_priv_size,
        .rule_add               = mlxsw_sp_acl_tcam_flower_rule_add,
        .rule_del               = mlxsw_sp_acl_tcam_flower_rule_del,
        .rule_activity_get      = mlxsw_sp_acl_tcam_flower_rule_activity_get,
diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_tcam.h b/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_tcam.h
new file mode 100644 (file)
index 0000000..5d145ab
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_tcam.h
+ * Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.
+ * Copyright (c) 2017-2018 Jiri Pirko <jiri@mellanox.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the names of the copyright holders nor the names of its
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * Alternatively, this software may be distributed under the terms of the
+ * GNU General Public License ("GPL") version 2 as published by the Free
+ * Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _MLXSW_SPECTRUM_ACL_TCAM_H
+#define _MLXSW_SPECTRUM_ACL_TCAM_H
+
+#include <linux/list.h>
+#include <linux/parman.h>
+
+#include "reg.h"
+#include "spectrum.h"
+#include "core_acl_flex_keys.h"
+
+extern const struct mlxsw_sp_acl_ops mlxsw_sp_acl_tcam_ops;
+
+#define MLXSW_SP_ACL_TCAM_REGION_BASE_COUNT 16
+#define MLXSW_SP_ACL_TCAM_REGION_RESIZE_STEP 16
+
+#define MLXSW_SP_ACL_TCAM_CATCHALL_PRIO (~0U)
+
+struct mlxsw_sp_acl_tcam_group;
+
+struct mlxsw_sp_acl_tcam_region {
+       struct list_head list; /* Member of a TCAM group */
+       struct list_head chunk_list; /* List of chunks under this region */
+       struct mlxsw_sp_acl_tcam_group *group;
+       enum mlxsw_reg_ptar_key_type key_type;
+       u16 id; /* ACL ID and region ID - they are same */
+       char tcam_region_info[MLXSW_REG_PXXX_TCAM_REGION_INFO_LEN];
+       struct mlxsw_afk_key_info *key_info;
+       struct mlxsw_sp *mlxsw_sp;
+       unsigned long priv[0];
+       /* priv has to be always the last item */
+};
+
+struct mlxsw_sp_acl_ctcam_region {
+       struct parman *parman;
+       struct mlxsw_sp_acl_tcam_region *region;
+};
+
+struct mlxsw_sp_acl_ctcam_chunk {
+       struct parman_prio parman_prio;
+};
+
+struct mlxsw_sp_acl_ctcam_entry {
+       struct parman_item parman_item;
+};
+
+int mlxsw_sp_acl_ctcam_region_init(struct mlxsw_sp *mlxsw_sp,
+                                  struct mlxsw_sp_acl_ctcam_region *cregion,
+                                  struct mlxsw_sp_acl_tcam_region *region);
+void mlxsw_sp_acl_ctcam_region_fini(struct mlxsw_sp_acl_ctcam_region *cregion);
+void mlxsw_sp_acl_ctcam_chunk_init(struct mlxsw_sp_acl_ctcam_region *cregion,
+                                  struct mlxsw_sp_acl_ctcam_chunk *cchunk,
+                                  unsigned int priority);
+void mlxsw_sp_acl_ctcam_chunk_fini(struct mlxsw_sp_acl_ctcam_chunk *cchunk);
+int mlxsw_sp_acl_ctcam_entry_add(struct mlxsw_sp *mlxsw_sp,
+                                struct mlxsw_sp_acl_ctcam_region *cregion,
+                                struct mlxsw_sp_acl_ctcam_chunk *cchunk,
+                                struct mlxsw_sp_acl_ctcam_entry *centry,
+                                struct mlxsw_sp_acl_rule_info *rulei);
+void mlxsw_sp_acl_ctcam_entry_del(struct mlxsw_sp *mlxsw_sp,
+                                 struct mlxsw_sp_acl_ctcam_region *cregion,
+                                 struct mlxsw_sp_acl_ctcam_chunk *cchunk,
+                                 struct mlxsw_sp_acl_ctcam_entry *centry);
+static inline unsigned int
+mlxsw_sp_acl_ctcam_entry_offset(struct mlxsw_sp_acl_ctcam_entry *centry)
+{
+       return centry->parman_item.index;
+}
+
+#endif