#include "diag/fs_tracepoint.h"
 #include "accel/ipsec.h"
 #include "fpga/ipsec.h"
+#include "eswitch.h"
 
 #define INIT_TREE_NODE_ARRAY_SIZE(...) (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
                                         sizeof(struct init_tree_node))
        struct fs_node *iter = list_entry(start, struct fs_node, list);
        struct mlx5_flow_table *ft = NULL;
 
-       if (!root)
+       if (!root || root->type == FS_TYPE_PRIO_CHAINS)
                return NULL;
 
        list_for_each_advance_continue(iter, &root->children, reverse) {
                               fg->id);
 }
 
+struct mlx5_flow_namespace *mlx5_get_fdb_sub_ns(struct mlx5_core_dev *dev,
+                                               int n)
+{
+       struct mlx5_flow_steering *steering = dev->priv.steering;
+
+       if (!steering || !steering->fdb_sub_ns)
+               return NULL;
+
+       return steering->fdb_sub_ns[n];
+}
+EXPORT_SYMBOL(mlx5_get_fdb_sub_ns);
+
 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
                                                    enum mlx5_flow_namespace_type type)
 {
        }
 }
 
-static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
-                                     unsigned int prio, int num_levels)
+static struct fs_prio *_fs_create_prio(struct mlx5_flow_namespace *ns,
+                                      unsigned int prio,
+                                      int num_levels,
+                                      enum fs_node_type type)
 {
        struct fs_prio *fs_prio;
 
        if (!fs_prio)
                return ERR_PTR(-ENOMEM);
 
-       fs_prio->node.type = FS_TYPE_PRIO;
+       fs_prio->node.type = type;
        tree_init_node(&fs_prio->node, NULL, del_sw_prio);
        tree_add_node(&fs_prio->node, &ns->node);
        fs_prio->num_levels = num_levels;
        return fs_prio;
 }
 
+static struct fs_prio *fs_create_prio_chained(struct mlx5_flow_namespace *ns,
+                                             unsigned int prio,
+                                             int num_levels)
+{
+       return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO_CHAINS);
+}
+
+static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
+                                     unsigned int prio, int num_levels)
+{
+       return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO);
+}
+
 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
                                                     *ns)
 {
        cleanup_egress_acls_root_ns(dev);
        cleanup_ingress_acls_root_ns(dev);
        cleanup_root_ns(steering->fdb_root_ns);
+       steering->fdb_root_ns = NULL;
+       kfree(steering->fdb_sub_ns);
+       steering->fdb_sub_ns = NULL;
        cleanup_root_ns(steering->sniffer_rx_root_ns);
        cleanup_root_ns(steering->sniffer_tx_root_ns);
        cleanup_root_ns(steering->egress_root_ns);
 
 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
 {
-       struct fs_prio *prio;
+       struct mlx5_flow_namespace *ns;
+       struct fs_prio *maj_prio;
+       struct fs_prio *min_prio;
+       int levels;
+       int chain;
+       int prio;
+       int err;
 
        steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
        if (!steering->fdb_root_ns)
                return -ENOMEM;
 
-       prio = fs_create_prio(&steering->fdb_root_ns->ns, 0, 2);
-       if (IS_ERR(prio))
+       steering->fdb_sub_ns = kzalloc(sizeof(steering->fdb_sub_ns) *
+                                      FDB_MAX_CHAIN + 1, GFP_KERNEL);
+       if (!steering->fdb_sub_ns)
+               return -ENOMEM;
+
+       levels = 2 * FDB_MAX_PRIO * (FDB_MAX_CHAIN + 1);
+       maj_prio = fs_create_prio_chained(&steering->fdb_root_ns->ns, 0,
+                                         levels);
+       if (IS_ERR(maj_prio)) {
+               err = PTR_ERR(maj_prio);
                goto out_err;
+       }
 
-       prio = fs_create_prio(&steering->fdb_root_ns->ns, 1, 1);
-       if (IS_ERR(prio))
+       for (chain = 0; chain <= FDB_MAX_CHAIN; chain++) {
+               ns = fs_create_namespace(maj_prio);
+               if (IS_ERR(ns)) {
+                       err = PTR_ERR(ns);
+                       goto out_err;
+               }
+
+               for (prio = 0; prio < FDB_MAX_PRIO * (chain + 1); prio++) {
+                       min_prio = fs_create_prio(ns, prio, 2);
+                       if (IS_ERR(min_prio)) {
+                               err = PTR_ERR(min_prio);
+                               goto out_err;
+                       }
+               }
+
+               steering->fdb_sub_ns[chain] = ns;
+       }
+
+       maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, 1, 1);
+       if (IS_ERR(maj_prio)) {
+               err = PTR_ERR(maj_prio);
                goto out_err;
+       }
 
        set_prio_attrs(steering->fdb_root_ns);
        return 0;
 
 out_err:
        cleanup_root_ns(steering->fdb_root_ns);
+       kfree(steering->fdb_sub_ns);
+       steering->fdb_sub_ns = NULL;
        steering->fdb_root_ns = NULL;
-       return PTR_ERR(prio);
+       return err;
 }
 
 static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
 
 #include <linux/rhashtable.h>
 #include <linux/llist.h>
 
+/* FS_TYPE_PRIO_CHAINS is a PRIO that will have namespaces only,
+ * and those are in parallel to one another when going over them to connect
+ * a new flow table. Meaning the last flow table in a TYPE_PRIO prio in one
+ * parallel namespace will not automatically connect to the first flow table
+ * found in any prio in any next namespace, but skip the entire containing
+ * TYPE_PRIO_CHAINS prio.
+ *
+ * This is used to implement tc chains, each chain of prios is a different
+ * namespace inside a containing TYPE_PRIO_CHAINS prio.
+ */
+
 enum fs_node_type {
        FS_TYPE_NAMESPACE,
        FS_TYPE_PRIO,
+       FS_TYPE_PRIO_CHAINS,
        FS_TYPE_FLOW_TABLE,
        FS_TYPE_FLOW_GROUP,
        FS_TYPE_FLOW_ENTRY,
        struct kmem_cache               *ftes_cache;
        struct mlx5_flow_root_namespace *root_ns;
        struct mlx5_flow_root_namespace *fdb_root_ns;
+       struct mlx5_flow_namespace      **fdb_sub_ns;
        struct mlx5_flow_root_namespace **esw_egress_root_ns;
        struct mlx5_flow_root_namespace **esw_ingress_root_ns;
        struct mlx5_flow_root_namespace *sniffer_tx_root_ns;