From: Valentine Fatiev <valentinef@nvidia.com>
Subject: [PATCH] BACKPORT: drivers/net/ethernet/mellanox/mlx5/core/fs_core.c

Change-Id: I0fa7e6ba70eca6894561bed963aee56e3ae9ef89
---
 .../net/ethernet/mellanox/mlx5/core/fs_core.c | 87 +++++++++++++++++--
 1 file changed, 79 insertions(+), 8 deletions(-)

--- a/drivers/net/ethernet/mellanox/mlx5/core/fs_core.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/fs_core.c
@@ -321,15 +321,27 @@ enum fs_i_lock_class {
 };
 
 static const struct rhashtable_params rhash_fte = {
-	.key_len = sizeof_field(struct fs_fte, val),
+#ifndef FIELD_SIZEOF
+        .key_len = sizeof_field(struct fs_fte, val),
+#else
+        .key_len = FIELD_SIZEOF(struct fs_fte, val),
+#endif
 	.key_offset = offsetof(struct fs_fte, val),
 	.head_offset = offsetof(struct fs_fte, hash),
 	.automatic_shrinking = true,
 	.min_size = 1,
 };
 
+#if !defined(HAVE_RHLTABLE) && defined(HAVE_NETNS_FRAGS_RHASHTABLE)
+static const struct bp_rhashtable_params rhash_fg = {
+#else
 static const struct rhashtable_params rhash_fg = {
-	.key_len = sizeof_field(struct mlx5_flow_group, mask),
+#endif
+#ifndef FIELD_SIZEOF
+        .key_len = sizeof_field(struct mlx5_flow_group, mask),
+#else
+        .key_len = FIELD_SIZEOF(struct mlx5_flow_group, mask),
+#endif
 	.key_offset = offsetof(struct mlx5_flow_group, mask),
 	.head_offset = offsetof(struct mlx5_flow_group, hash),
 	.automatic_shrinking = true,
@@ -543,7 +555,9 @@ static void del_hw_flow_table(struct fs_
 	fs_get_obj(ft, node);
 	dev = get_dev(&ft->node);
 	root = find_root(&ft->node);
+#ifndef MLX_DISABLE_TRACEPOINTS
 	trace_mlx5_fs_del_ft(ft);
+#endif
 
 	if (node->active) {
 		err = root->cmds->destroy_flow_table(root, ft);
@@ -559,7 +573,11 @@ static void del_sw_flow_table(struct fs_
 
 	fs_get_obj(ft, node);
 
+#if !defined(HAVE_RHLTABLE) && defined(HAVE_NETNS_FRAGS_RHASHTABLE)
+	bp_rhltable_destroy(&ft->fgs_hash);
+#else
 	rhltable_destroy(&ft->fgs_hash);
+#endif
 	if (ft->node.parent) {
 		fs_get_obj(prio, ft->node.parent);
 		prio->num_ft--;
@@ -595,7 +613,9 @@ static void del_sw_hw_rule(struct fs_nod
 
 	fs_get_obj(rule, node);
 	fs_get_obj(fte, rule->node.parent);
+#ifndef MLX_DISABLE_TRACEPOINTS
 	trace_mlx5_fs_del_rule(rule);
+#endif
 	if (is_fwd_next_action(rule->sw_action)) {
 		mutex_lock(&rule->dest_attr.ft->lock);
 		list_del(&rule->next_ft);
@@ -640,7 +660,9 @@ static void del_hw_fte(struct fs_node *n
 	fs_get_obj(fg, fte->node.parent);
 	fs_get_obj(ft, fg->node.parent);
 
+#ifndef MLX_DISABLE_TRACEPOINTS
 	trace_mlx5_fs_del_fte(fte);
+#endif
 	dev = get_dev(&ft->node);
 	root = find_root(&ft->node);
 	if (node->active) {
@@ -667,7 +689,11 @@ static void del_sw_fte(struct fs_node *n
 				     &fte->hash,
 				     rhash_fte);
 	WARN_ON(err);
+#ifdef HAVE_IDA_FREE
 	ida_free(&fg->fte_allocator, fte->index - fg->start_index);
+#else
+	ida_simple_remove(&fg->fte_allocator, fte->index - fg->start_index);
+#endif
 	kmem_cache_free(steering->ftes_cache, fte);
 }
 
@@ -681,7 +707,9 @@ static void del_hw_flow_group(struct fs_
 	fs_get_obj(fg, node);
 	fs_get_obj(ft, fg->node.parent);
 	dev = get_dev(&ft->node);
+#ifndef MLX_DISABLE_TRACEPOINTS
 	trace_mlx5_fs_del_fg(fg);
+#endif
 
 	root = find_root(&ft->node);
 	if (fg->node.active && root->cmds->destroy_flow_group(root, ft, fg))
@@ -705,7 +733,11 @@ static void del_sw_flow_group(struct fs_
 	    fg->max_ftes == ft->autogroup.group_size &&
 	    fg->start_index < ft->autogroup.max_fte)
 		ft->autogroup.num_groups--;
+#if !defined(HAVE_RHLTABLE) && defined(HAVE_NETNS_FRAGS_RHASHTABLE)
+	err = bp_rhltable_remove(&ft->fgs_hash,
+#else
 	err = rhltable_remove(&ft->fgs_hash,
+#endif
 			      &fg->hash,
 			      rhash_fg);
 	WARN_ON(err);
@@ -716,8 +748,11 @@ static int insert_fte(struct mlx5_flow_g
 {
 	int index;
 	int ret;
-
+#ifdef HAVE_IDA_ALLOC_MAX
 	index = ida_alloc_max(&fg->fte_allocator, fg->max_ftes - 1, GFP_KERNEL);
+#else
+	index = ida_simple_get(&fg->fte_allocator, 0, fg->max_ftes, GFP_KERNEL);
+#endif
 	if (index < 0)
 		return index;
 
@@ -733,7 +768,11 @@ static int insert_fte(struct mlx5_flow_g
 	return 0;
 
 err_ida_remove:
+#ifdef HAVE_IDA_FREE
 	ida_free(&fg->fte_allocator, index);
+#else
+	ida_simple_remove(&fg->fte_allocator, index);
+#endif
 	return ret;
 }
 
@@ -812,7 +851,11 @@ static struct mlx5_flow_group *alloc_ins
 		return fg;
 
 	/* initialize refcnt, add to parent list */
+#if !defined(HAVE_RHLTABLE) && defined(HAVE_NETNS_FRAGS_RHASHTABLE)
+	ret = bp_rhltable_insert(&ft->fgs_hash,
+#else
 	ret = rhltable_insert(&ft->fgs_hash,
+#endif
 			      &fg->hash,
 			      rhash_fg);
 	if (ret) {
@@ -841,7 +884,11 @@ static struct mlx5_flow_table *alloc_flo
 	if (!ft)
 		return ERR_PTR(-ENOMEM);
 
+#if !defined(HAVE_RHLTABLE) && defined(HAVE_NETNS_FRAGS_RHASHTABLE)
+	ret = bp_rhltable_init(&ft->fgs_hash, &rhash_fg);
+#else
 	ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
+#endif
 	if (ret) {
 		kfree(ft);
 		return ERR_PTR(ret);
@@ -1217,12 +1264,18 @@ static struct mlx5_flow_table *__mlx5_cr
 	fs_prio->num_ft++;
 	up_write_ref_node(&fs_prio->node, false);
 	mutex_unlock(&root->chain_lock);
+#ifndef MLX_DISABLE_TRACEPOINTS
 	trace_mlx5_fs_add_ft(ft);
+#endif
 	return ft;
 destroy_ft:
 	root->cmds->destroy_flow_table(root, ft);
 free_ft:
+#if !defined(HAVE_RHLTABLE) && defined(HAVE_NETNS_FRAGS_RHASHTABLE)
+	bp_rhltable_destroy(&ft->fgs_hash);
+#else
 	rhltable_destroy(&ft->fgs_hash);
+#endif
 	kfree(ft);
 unlock_root:
 	mutex_unlock(&root->chain_lock);
@@ -1357,7 +1410,9 @@ struct mlx5_flow_group *mlx5_create_flow
 		tree_put_node(&fg->node, false);
 		return ERR_PTR(err);
 	}
-	trace_mlx5_fs_add_fg(fg);
+#ifndef MLX_DISABLE_TRACEPOINTS
+       trace_mlx5_fs_add_fg(fg);
+#endif
 	fg->node.active = true;
 
 	return fg;
@@ -1594,7 +1649,9 @@ static int create_auto_flow_group(struct
 	err = root->cmds->create_flow_group(root, ft, in, fg);
 	if (!err) {
 		fg->node.active = true;
+#ifndef MLX_DISABLE_TRACEPOINTS
 		trace_mlx5_fs_add_fg(fg);
+#endif
 	}
 
 	kvfree(in);
@@ -1737,12 +1794,16 @@ static struct mlx5_flow_handle *add_rule
 		fte->action.action = old_action;
 		return handle;
 	}
+#ifndef MLX_DISABLE_TRACEPOINTS
 	trace_mlx5_fs_set_fte(fte, false);
+#endif
 
 	for (i = 0; i < handle->num_rules; i++) {
 		if (refcount_read(&handle->rule[i]->node.refcount) == 1) {
 			tree_add_node(&handle->rule[i]->node, &fte->node);
+#ifndef MLX_DISABLE_TRACEPOINTS
 			trace_mlx5_fs_add_rule(handle->rule[i]);
+#endif
 		}
 	}
 	return handle;
@@ -1808,16 +1869,26 @@ static int build_match_list(struct match
 			    struct mlx5_flow_group *fg,
 			    bool ft_locked)
 {
+#if !defined(HAVE_RHLTABLE) && defined(HAVE_NETNS_FRAGS_RHASHTABLE)
+	struct bp_rhlist_head *tmp, *list;
+#else
 	struct rhlist_head *tmp, *list;
+#endif
 	struct mlx5_flow_group *g;
 	int err = 0;
 
 	rcu_read_lock();
 	INIT_LIST_HEAD(&match_head->list);
 	/* Collect all fgs which has a matching match_criteria */
-	list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
+#if !defined(HAVE_RHLTABLE) && defined(HAVE_NETNS_FRAGS_RHASHTABLE)
+	list = bp_rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
 	/* RCU is atomic, we can't execute FW commands here */
-	rhl_for_each_entry_rcu(g, tmp, list, hash) {
+	bp_rhl_for_each_entry_rcu(g, tmp, list, hash) {
+#else
+       list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
+       /* RCU is atomic, we can't execute FW commands here */
+       rhl_for_each_entry_rcu(g, tmp, list, hash) {
+#endif
 		struct match_list *curr_match;
 
 		if (fg && fg != g)
@@ -3232,8 +3303,8 @@ cleanup:
 int mlx5_init_fs(struct mlx5_core_dev *dev)
 {
 	struct mlx5_flow_steering *steering;
-	char *ftes_cache_name;
-	char *fgs_cache_name;
+	char *ftes_cache_name = NULL;
+	char *fgs_cache_name = NULL;
 	int err = 0;
 
 	err = mlx5_init_fc_stats(dev);
