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

Change-Id: I5c5eb70c94659b115ea0c99514fa343c43676712
---
 .../net/ethernet/mellanox/mlx5/core/eswitch.c | 91 ++++++++++++++++++-
 1 file changed, 87 insertions(+), 4 deletions(-)

--- a/drivers/net/ethernet/mellanox/mlx5/core/eswitch.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/eswitch.c
@@ -981,7 +981,7 @@ int mlx5_esw_vport_enable(struct mlx5_es
 			  enum mlx5_eswitch_vport_event enabled_events)
 {
 	struct mlx5_vport *vport;
-	int ret;
+	int ret = 0;
 
 	vport = mlx5_eswitch_get_vport(esw, vport_num);
 	if (IS_ERR(vport))
@@ -1326,7 +1326,11 @@ static void mlx5_eswitch_get_devlink_par
 	union devlink_param_value val;
 	int err;
 
-	err = devlink_param_driverinit_value_get(devlink,
+#ifdef HAVE_DEVL_PARAM_DRIVERINIT_VALUE_GET
+	err = devl_param_driverinit_value_get(devlink,
+#else
+        err = devlink_param_driverinit_value_get(devlink,
+#endif
 						 MLX5_DEVLINK_PARAM_ID_ESW_LARGE_GROUP_NUM,
 						 &val);
 	if (!err) {
@@ -1507,17 +1511,29 @@ abort:
  */
 int mlx5_eswitch_enable(struct mlx5_eswitch *esw, int num_vfs)
 {
+#if defined(HAVE_DEVL_PORT_REGISTER) && \
+	!defined(HAVE_DEVL_TRAP_GROUPS_REGISTER)//forward port
+	struct devlink *devlink;
+#endif
 	bool toggle_lag;
 	int ret;
 
 	if (!mlx5_esw_allowed(esw))
 		return 0;
+#ifdef HAVE_DEVL_TRAP_GROUPS_REGISTER //forward port
+	devl_assert_locked(priv_to_devlink(esw->dev));
+#endif
 
 	toggle_lag = !mlx5_sriov_is_enabled(esw->dev) && !is_mdev_switchdev_mode(esw->dev);
 
 	if (toggle_lag)
 		mlx5_lag_disable_change(esw->dev);
 
+#if defined(HAVE_DEVL_PORT_REGISTER) && \
+	!defined(HAVE_DEVL_TRAP_GROUPS_REGISTER)//forward port
+	devlink = priv_to_devlink(esw->dev);
+	devl_lock(devlink);
+#endif
 	down_write(&esw->mode_lock);
 	if (!mlx5_esw_is_fdb_created(esw)) {
 		ret = mlx5_eswitch_enable_locked(esw, num_vfs);
@@ -1531,6 +1547,10 @@ int mlx5_eswitch_enable(struct mlx5_eswi
 			esw->esw_funcs.num_vfs = num_vfs;
 	}
 	up_write(&esw->mode_lock);
+#if defined(HAVE_DEVL_PORT_REGISTER) && \
+	!defined(HAVE_DEVL_TRAP_GROUPS_REGISTER)//forward port
+	devl_unlock(devlink);
+#endif
 
 	if (toggle_lag)
 		mlx5_lag_enable_change(esw->dev);
@@ -1541,9 +1561,19 @@ int mlx5_eswitch_enable(struct mlx5_eswi
 /* When disabling sriov, free driver level resources. */
 void mlx5_eswitch_disable_sriov(struct mlx5_eswitch *esw, bool clear_vf)
 {
+#if defined(HAVE_DEVL_PORT_REGISTER) && \
+	!defined(HAVE_DEVL_TRAP_GROUPS_REGISTER)//forward port
+	struct devlink *devlink;
+#endif
 	if (!mlx5_esw_allowed(esw))
 		return;
 
+#ifdef HAVE_DEVL_TRAP_GROUPS_REGISTER
+	devl_assert_locked(priv_to_devlink(esw->dev));
+#elif defined(HAVE_DEVL_PORT_REGISTER) //forward port
+	devlink = priv_to_devlink(esw->dev);
+	devl_lock(devlink);
+#endif
 	down_write(&esw->mode_lock);
 	/* If driver is unloaded, this function is called twice by remove_one()
 	 * and mlx5_unload(). Prevent the second call.
@@ -1562,9 +1592,17 @@ void mlx5_eswitch_disable_sriov(struct m
 	 * because it depends on num_vfs.
 	 */
 	if (esw->mode == MLX5_ESWITCH_OFFLOADS) {
+#ifdef HAVE_DEVLINK_HAS_RATE_FUNCTIONS
 		struct devlink *devlink = priv_to_devlink(esw->dev);
+#endif
 
-		devlink_rate_nodes_destroy(devlink);
+#ifdef HAVE_DEVLINK_HAS_RATE_FUNCTIONS
+#ifdef HAVE_DEVL_PORT_REGISTER //forward port
+		devl_rate_nodes_destroy(devlink);
+#else
+ 		devlink_rate_nodes_destroy(devlink);
+#endif
+#endif
 #if IS_ENABLED(CONFIG_MLXDEVM)
 		mlx5_devm_rate_nodes_destroy(esw->dev);
 #endif
@@ -1575,6 +1613,10 @@ void mlx5_eswitch_disable_sriov(struct m
 
 unlock:
 	up_write(&esw->mode_lock);
+#if defined(HAVE_DEVL_PORT_REGISTER) && \
+	!defined(HAVE_DEVL_TRAP_GROUPS_REGISTER)//forward port
+	devl_unlock(devlink);
+#endif
 }
 
 /* Free resources for corresponding eswitch mode. It is called by devlink
@@ -1582,9 +1624,15 @@ unlock:
  */
 void mlx5_eswitch_disable_locked(struct mlx5_eswitch *esw)
 {
+#ifdef HAVE_DEVLINK_HAS_RATE_FUNCTIONS
 	struct devlink *devlink = priv_to_devlink(esw->dev);
+#endif
 
+#ifdef HAVE_LOCKUP_ASSERT_HELD_WRITE
 	lockdep_assert_held_write(&esw->mode_lock);
+#else
+	lockdep_assert_held(&esw->mode_lock);
+#endif
 
 	/* Notify eswitch users that it is exiting from current mode.
 	 * So that it can do necessary cleanup before the eswitch is disabled.
@@ -1609,7 +1657,13 @@ void mlx5_eswitch_disable_locked(struct
 	}
 
 	if (esw->mode == MLX5_ESWITCH_OFFLOADS) {
-		devlink_rate_nodes_destroy(devlink);
+#ifdef HAVE_DEVLINK_HAS_RATE_FUNCTIONS
+#ifdef HAVE_DEVL_PORT_REGISTER //forward port
+		devl_rate_nodes_destroy(devlink);
+#else
+ 		devlink_rate_nodes_destroy(devlink);
+#endif
+#endif
 #if IS_ENABLED(CONFIG_MLXDEVM)
 		mlx5_devm_rate_nodes_destroy(esw->dev);
 #endif
@@ -1618,13 +1672,30 @@ void mlx5_eswitch_disable_locked(struct
 
 void mlx5_eswitch_disable(struct mlx5_eswitch *esw)
 {
+#if defined(HAVE_DEVL_PORT_REGISTER) && \
+	!defined(HAVE_DEVL_TRAP_GROUPS_REGISTER)//forward port
+	struct devlink *devlink;
+#endif
+
 	if (!mlx5_esw_allowed(esw))
 		return;
 
+#ifdef HAVE_DEVL_TRAP_GROUPS_REGISTER //forward port
+	devl_assert_locked(priv_to_devlink(esw->dev));
+#endif
 	mlx5_lag_disable_change(esw->dev);
+#if defined(HAVE_DEVL_PORT_REGISTER) && \
+	!defined(HAVE_DEVL_TRAP_GROUPS_REGISTER)//forward port
+	devlink = priv_to_devlink(esw->dev);
+	devl_lock(devlink);
+#endif
 	down_write(&esw->mode_lock);
 	mlx5_eswitch_disable_locked(esw);
 	up_write(&esw->mode_lock);
+#if defined(HAVE_DEVL_PORT_REGISTER) && \
+	!defined(HAVE_DEVL_TRAP_GROUPS_REGISTER)//forward port
+	devl_unlock(devlink);
+#endif
 	mlx5_lag_enable_change(esw->dev);
 }
 
@@ -1927,7 +1998,9 @@ int mlx5_eswitch_init(struct mlx5_core_d
 	ida_init(&esw->offloads.vport_metadata_ida);
 	xa_init_flags(&esw->offloads.vhca_map, XA_FLAGS_ALLOC);
 	mutex_init(&esw->state_lock);
+#ifdef HAVE_LOCKDEP_UNREGISTER_KEY
 	lockdep_register_key(&esw->mode_lock_key);
+#endif
 	init_rwsem(&esw->mode_lock);
 	lockdep_set_class(&esw->mode_lock, &esw->mode_lock_key);
 	refcount_set(&esw->qos.refcnt, 0);
@@ -2014,7 +2087,9 @@ void mlx5_eswitch_cleanup(struct mlx5_es
 	esw->dev->priv.eswitch = NULL;
 	destroy_workqueue(esw->work_queue);
 	WARN_ON(refcount_read(&esw->qos.refcnt));
+#ifdef HAVE_LOCKDEP_UNREGISTER_KEY
 	lockdep_unregister_key(&esw->mode_lock_key);
+#endif
 	mutex_destroy(&esw->state_lock);
 	WARN_ON(!xa_empty(&esw->offloads.vhca_map));
 	xa_destroy(&esw->offloads.vhca_map);
@@ -2392,9 +2467,13 @@ int mlx5_eswitch_get_vport_config(struct
 	ivi->linkstate = evport->info.link_state;
 	ivi->vlan = evport->info.vlan;
 	ivi->qos = evport->info.qos;
+#ifdef HAVE_VF_VLAN_PROTO
 	ivi->vlan_proto = evport->info.vlan_proto;
+#endif
 	ivi->spoofchk = evport->info.spoofchk;
+#ifdef HAVE_VF_INFO_TRUST
 	ivi->trusted = evport->info.trusted;
+#endif
 	if (evport->qos.enabled) {
 		ivi->min_tx_rate = evport->qos.min_rate;
 		ivi->max_tx_rate = evport->qos.max_rate;
@@ -2456,7 +2535,9 @@ int mlx5_eswitch_get_vport_stats(struct
 	struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
 	int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
 	u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)] = {};
+#ifdef HAVE_STRUCT_IFLA_VF_STATS_RX_TX_DROPPED
 	struct mlx5_vport_drop_stats stats = {};
+#endif
 	int err = 0;
 	u32 *out;
 
@@ -2516,11 +2597,13 @@ int mlx5_eswitch_get_vport_stats(struct
 	vf_stats->broadcast =
 		MLX5_GET_CTR(out, received_eth_broadcast.packets);
 
+#ifdef HAVE_STRUCT_IFLA_VF_STATS_RX_TX_DROPPED
 	err = mlx5_esw_query_vport_drop_stats(esw->dev, vport, &stats);
 	if (err)
 		goto free_out;
 	vf_stats->rx_dropped = stats.rx_dropped;
 	vf_stats->tx_dropped = stats.tx_dropped;
+#endif
 
 free_out:
 	kvfree(out);
