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

Change-Id: I96cf902e74965fec6d6bc01e2571a1ef6f4df37c
---
 .../mellanox/mlx5/core/eswitch_offloads.c     | 124 +++++++++++++++---
 1 file changed, 109 insertions(+), 15 deletions(-)

--- a/drivers/net/ethernet/mellanox/mlx5/core/eswitch_offloads.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/eswitch_offloads.c
@@ -2675,11 +2675,13 @@ int esw_offloads_load_rep(struct mlx5_es
 	if (esw->mode != MLX5_ESWITCH_OFFLOADS)
 		return 0;
 
+#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
 	if (vport_num != MLX5_VPORT_UPLINK) {
 		err = mlx5_esw_offloads_devlink_port_register(esw, vport_num);
 		if (err)
 			return err;
 	}
+#endif
 
 	err = mlx5_esw_offloads_rep_load(esw, vport_num);
 	if (err)
@@ -2687,8 +2689,10 @@ int esw_offloads_load_rep(struct mlx5_es
 	return err;
 
 load_err:
+#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
 	if (vport_num != MLX5_VPORT_UPLINK)
 		mlx5_esw_offloads_devlink_port_unregister(esw, vport_num);
+#endif
 	return err;
 }
 
@@ -2699,8 +2703,10 @@ void esw_offloads_unload_rep(struct mlx5
 
 	mlx5_esw_offloads_rep_unload(esw, vport_num);
 
+#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
 	if (vport_num != MLX5_VPORT_UPLINK)
 		mlx5_esw_offloads_devlink_port_unregister(esw, vport_num);
+#endif
 }
 
 static int esw_set_slave_root_fdb(struct mlx5_core_dev *master,
@@ -3137,7 +3143,11 @@ u32 mlx5_esw_match_metadata_alloc(struct
 
 	/* Metadata is 4 bits of PFNUM and 12 bits of unique id */
 	/* Use only non-zero vport_id (2-4095) for all PF's */
+#ifdef HAVE_IDA_ALLOC_RANGE
 	id = ida_alloc_range(&esw->offloads.vport_metadata_ida,
+#else
+	id = ida_simple_get(&esw->offloads.vport_metadata_ida,
+#endif
 			     MLX5_ESW_METADATA_RSVD_UPLINK + 1,
 			     vport_end_ida, GFP_KERNEL);
 	if (id < 0)
@@ -3151,7 +3161,11 @@ void mlx5_esw_match_metadata_free(struct
 	u32 vport_bit_mask = (1 << ESW_VPORT_BITS) - 1;
 
 	/* Metadata contains only 12 bits of actual ida id */
-	ida_free(&esw->offloads.vport_metadata_ida, metadata & vport_bit_mask);
+#ifdef HAVE_IDA_FREE
+       ida_free(&esw->offloads.vport_metadata_ida, metadata & vport_bit_mask);
+#else
+	ida_simple_remove(&esw->offloads.vport_metadata_ida, metadata & vport_bit_mask);
+#endif
 }
 
 static int esw_offloads_vport_metadata_setup(struct mlx5_eswitch *esw,
@@ -3414,6 +3428,9 @@ static void esw_offloads_steering_cleanu
 static void
 esw_vfs_changed_event_handler(struct mlx5_eswitch *esw, const u32 *out)
 {
+#ifdef HAVE_DEVL_PORT_REGISTER //forward port
+	struct devlink *devlink;
+#endif
 	bool host_pf_disabled;
 	u16 new_num_vfs;
 
@@ -3425,6 +3442,10 @@ esw_vfs_changed_event_handler(struct mlx
 	if (new_num_vfs == esw->esw_funcs.num_vfs || host_pf_disabled)
 		return;
 
+#ifdef HAVE_DEVL_PORT_REGISTER //forward port
+	devlink = priv_to_devlink(esw->dev);
+	devl_lock(devlink);
+#endif
 	/* Number of VFs can only change from "0 to x" or "x to 0". */
 	if (esw->esw_funcs.num_vfs > 0) {
 		mlx5_eswitch_unload_vf_vports(esw, esw->esw_funcs.num_vfs);
@@ -3433,10 +3454,17 @@ esw_vfs_changed_event_handler(struct mlx
 
 		err = mlx5_eswitch_load_vf_vports(esw, new_num_vfs,
 						  MLX5_VPORT_UC_ADDR_CHANGE);
-		if (err)
+		if (err) {
+#ifdef HAVE_DEVL_PORT_REGISTER //forward port
+			devl_unlock(devlink);
+#endif
 			return;
+		}
 	}
 	esw->esw_funcs.num_vfs = new_num_vfs;
+#ifdef HAVE_DEVL_PORT_REGISTER //forward port
+	devl_unlock(devlink);
+#endif
 }
 
 static void esw_functions_changed_event_handler(struct work_struct *work)
@@ -3686,14 +3714,24 @@ static int esw_inline_mode_to_devlink(u8
 	return 0;
 }
 
+#ifdef HAVE_DEVLINK_ESWITCH_MODE_SET_EXTACK
 int mlx5_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode,
 				  struct netlink_ext_ack *extack)
+#else
+int mlx5_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode)
+#endif
 {
+#ifndef HAVE_DEVLINK_ESWITCH_MODE_SET_EXTACK
+	struct netlink_ext_ack *extack;
+#endif
 	u16 cur_mlx5_mode, mlx5_mode = 0;
 	struct mlx5_eswitch *esw;
-	
 	int err = 0;
 
+#ifndef HAVE_DEVLINK_ESWITCH_MODE_SET_EXTACK
+	extack = NULL;
+#endif
+
 	esw = mlx5_devlink_eswitch_get(devlink);
 	if (IS_ERR(esw))
 		return PTR_ERR(esw);
@@ -3729,12 +3767,14 @@ int mlx5_devlink_eswitch_mode_set(struct
 
 	mlx5_eswitch_disable_locked(esw);
 	if (mode == DEVLINK_ESWITCH_MODE_SWITCHDEV) {
+#ifdef HAVE_DEVLINK_TRAP_SUPPORT
 		if (mlx5_devlink_trap_get_num_active(esw->dev)) {
 			NL_SET_ERR_MSG_MOD(extack,
 					   "Can't change mode while devlink traps are active");
 			err = -EOPNOTSUPP;
 			goto unlock;
 		}
+#endif
 		err = esw_offloads_start(esw, extack);
 	} else if (mode == DEVLINK_ESWITCH_MODE_LEGACY) {
 		err = esw_offloads_stop(esw, extack);
@@ -3800,14 +3840,23 @@ revert_inline_mode:
 	return err;
 }
 
-int mlx5_devlink_eswitch_inline_mode_set(struct devlink *devlink, u8 mode,
-					 struct netlink_ext_ack *extack)
+int mlx5_devlink_eswitch_inline_mode_set(struct devlink *devlink, u8 mode
+#ifdef HAVE_DEVLINK_ESWITCH_MODE_SET_EXTACK
+				 	, struct netlink_ext_ack *extack
+#endif
+				 	)
 {
+#ifndef HAVE_DEVLINK_ESWITCH_MODE_SET_EXTACK
+	struct netlink_ext_ack *extack;
+#endif
 	struct mlx5_core_dev *dev = devlink_priv(devlink);
 	struct mlx5_eswitch *esw;
 	u8 mlx5_mode;
 	int err;
 
+#ifndef HAVE_DEVLINK_ESWITCH_MODE_SET_EXTACK
+	extack = NULL;
+#endif
 	esw = mlx5_devlink_eswitch_get(devlink);
 	if (IS_ERR(esw))
 		return PTR_ERR(esw);
@@ -3872,13 +3921,26 @@ int mlx5_devlink_eswitch_inline_mode_get
 }
 
 int mlx5_devlink_eswitch_encap_mode_set(struct devlink *devlink,
-					enum devlink_eswitch_encap_mode encap,
-					struct netlink_ext_ack *extack)
+#ifdef HAVE_DEVLINK_HAS_ESWITCH_ENCAP_MODE_SET_GET_WITH_ENUM
+					enum devlink_eswitch_encap_mode encap
+#else
+					u8 encap
+#endif
+#ifdef HAVE_DEVLINK_ESWITCH_MODE_SET_EXTACK
+				 	, struct netlink_ext_ack *extack
+#endif
+				 	)
 {
+#ifndef HAVE_DEVLINK_ESWITCH_MODE_SET_EXTACK
+	struct netlink_ext_ack *extack;
+#endif
 	struct mlx5_core_dev *dev = devlink_priv(devlink);
 	struct mlx5_eswitch *esw;
 	int err = 0;
 
+#ifndef HAVE_DEVLINK_ESWITCH_MODE_SET_EXTACK
+	extack = NULL;
+#endif
 	esw = mlx5_devlink_eswitch_get(devlink);
 	if (IS_ERR(esw))
 		return PTR_ERR(esw);
@@ -3931,7 +3993,11 @@ unlock:
 }
 
 int mlx5_devlink_eswitch_encap_mode_get(struct devlink *devlink,
+#ifdef HAVE_DEVLINK_HAS_ESWITCH_ENCAP_MODE_SET_GET_WITH_ENUM
 					enum devlink_eswitch_encap_mode *encap)
+#else
+					u8 *encap)
+#endif
 {
 	struct mlx5_eswitch *esw;
 
@@ -3964,14 +4030,24 @@ mlx5_eswitch_vport_has_rep(const struct
 }
 
 int mlx5_devlink_eswitch_ipsec_mode_set(struct devlink *devlink,
-					enum devlink_eswitch_ipsec_mode ipsec,
-					struct netlink_ext_ack *extack)
+					enum devlink_eswitch_ipsec_mode ipsec
+#ifdef HAVE_DEVLINK_ESWITCH_MODE_SET_EXTACK
+					, struct netlink_ext_ack *extack
+#endif
+)
 {
+#ifndef HAVE_DEVLINK_ESWITCH_MODE_SET_EXTACK
+	struct netlink_ext_ack *extack;
+#endif
 	struct mlx5_core_dev *dev = devlink_priv(devlink);
 	struct mlx5_eswitch *esw = dev->priv.eswitch;
 	int err = 0;
 
+#ifndef HAVE_DEVLINK_ESWITCH_MODE_SET_EXTACK
+	extack = NULL;
+#else
 	memset(extack, 0, sizeof(*extack));
+#endif
 
 	esw = mlx5_devlink_eswitch_get(devlink);
 	if (IS_ERR(esw))
@@ -4419,15 +4495,24 @@ is_port_function_supported(struct mlx5_e
 	       mlx5_esw_is_sf_vport(esw, vport_num);
 }
 
-int mlx5_devlink_port_function_hw_addr_get(struct devlink_port *port,
-					   u8 *hw_addr, int *hw_addr_len,
-					   struct netlink_ext_ack *extack)
+#ifdef HAVE_PORT_FUNCTION_HW_ADDR_GET_GET_4_PARAM
+int mlx5_devlink_port_function_hw_addr_get(
+#else
+int mlx5_devlink_port_function_hw_addr_get(struct devlink *devlink,
+#endif
+ 					   struct devlink_port *port,
+ 					   u8 *hw_addr, int *hw_addr_len,
+ 					   struct netlink_ext_ack *extack)
 {
 	struct mlx5_eswitch *esw;
 	struct mlx5_vport *vport;
 	u16 vport_num;
 
+#ifdef HAVE_PORT_FUNCTION_HW_ADDR_GET_GET_4_PARAM
 	esw = mlx5_devlink_eswitch_get(port->devlink);
+#else
+	esw = mlx5_devlink_eswitch_get(devlink);
+#endif
 	if (IS_ERR(esw))
 		return PTR_ERR(esw);
 
@@ -4448,14 +4533,23 @@ int mlx5_devlink_port_function_hw_addr_g
 	return 0;
 }
 
-int mlx5_devlink_port_function_hw_addr_set(struct devlink_port *port,
-					   const u8 *hw_addr, int hw_addr_len,
-					   struct netlink_ext_ack *extack)
+#ifdef HAVE_PORT_FUNCTION_HW_ADDR_GET_GET_4_PARAM
+int mlx5_devlink_port_function_hw_addr_set(
+#else
+int mlx5_devlink_port_function_hw_addr_set(struct devlink *devlink,
+#endif
+ 					   struct devlink_port *port,
+ 					   const u8 *hw_addr, int hw_addr_len,
+ 					   struct netlink_ext_ack *extack)
 {
 	struct mlx5_eswitch *esw;
 	u16 vport_num;
 
+#ifdef HAVE_PORT_FUNCTION_HW_ADDR_GET_GET_4_PARAM
 	esw = mlx5_devlink_eswitch_get(port->devlink);
+#else
+	esw = mlx5_devlink_eswitch_get(devlink);
+#endif
 	if (IS_ERR(esw)) {
 		NL_SET_ERR_MSG_MOD(extack, "Eswitch doesn't support set hw_addr");
 		return PTR_ERR(esw);
