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

Change-Id: Ibfbe4db64c11b89107258cb8f3eefe7f3ee41f59
---
 .../mellanox/mlx5/core/eswitch_offloads.c     | 152 +++++++++++++++---
 1 file changed, 134 insertions(+), 18 deletions(-)

--- a/drivers/net/ethernet/mellanox/mlx5/core/eswitch_offloads.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/eswitch_offloads.c
@@ -2569,8 +2569,14 @@ err:
 	return err;
 }
 
+#if defined(HAVE_DEVLINK_PARAM_REGISTER) || defined(HAVE_DEVL_PARAM_DRIVERINIT_VALUE_GET)
 static int esw_port_metadata_set(struct devlink *devlink, u32 id,
+#ifdef HAVE_DEVLINK_PARAM_SET_FUNCTION_POINTER_HAS_EXTACK
+				 struct devlink_param_gset_ctx *ctx,
+				 struct netlink_ext_ack *extack)
+#else
 				 struct devlink_param_gset_ctx *ctx)
+#endif
 {
 	struct mlx5_core_dev *dev = devlink_priv(devlink);
 	struct mlx5_eswitch *esw = dev->priv.eswitch;
@@ -2627,6 +2633,7 @@ static const struct devlink_param esw_de
 			     esw_port_metadata_set,
 			     esw_port_metadata_validate),
 };
+#endif
 
 int esw_offloads_init(struct mlx5_eswitch *esw)
 {
@@ -2636,24 +2643,38 @@ int esw_offloads_init(struct mlx5_eswitc
 	if (err)
 		return err;
 
+#if defined(HAVE_DEVLINK_PARAM_REGISTER) || defined(HAVE_DEVL_PARAM_DRIVERINIT_VALUE_GET)
+#ifdef HAVE_DEVL_PARAM_DRIVERINIT_VALUE_GET
 	err = devl_params_register(priv_to_devlink(esw->dev),
+#else
+	err = devlink_params_register(priv_to_devlink(esw->dev),
+#endif
 				   esw_devlink_params,
 				   ARRAY_SIZE(esw_devlink_params));
 	if (err)
 		goto err_params;
+#endif
 
 	return 0;
 
+#if defined(HAVE_DEVLINK_PARAM_REGISTER) || defined(HAVE_DEVL_PARAM_DRIVERINIT_VALUE_GET)
 err_params:
 	esw_offloads_cleanup_reps(esw);
 	return err;
+#endif
 }
 
 void esw_offloads_cleanup(struct mlx5_eswitch *esw)
 {
+#if defined(HAVE_DEVLINK_PARAM_REGISTER) || defined(HAVE_DEVL_PARAM_DRIVERINIT_VALUE_GET)
+#ifdef HAVE_DEVL_PARAM_DRIVERINIT_VALUE_GET
 	devl_params_unregister(priv_to_devlink(esw->dev),
+#else
+	devlink_params_unregister(priv_to_devlink(esw->dev),
+#endif
 			       esw_devlink_params,
 			       ARRAY_SIZE(esw_devlink_params));
+#endif
 	esw_offloads_cleanup_reps(esw);
 }
 
@@ -2757,9 +2778,11 @@ int mlx5_esw_offloads_load_rep(struct ml
 	if (esw->mode != MLX5_ESWITCH_OFFLOADS)
 		return 0;
 
+#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
 	err = mlx5_esw_offloads_devlink_port_register(esw, vport);
 	if (err)
 		return err;
+#endif
 
 	err = mlx5_esw_offloads_rep_load(esw, vport->vport);
 	if (err)
@@ -2767,7 +2790,9 @@ int mlx5_esw_offloads_load_rep(struct ml
 	return err;
 
 load_err:
+#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
 	mlx5_esw_offloads_devlink_port_unregister(esw, vport);
+#endif
 	return err;
 }
 
@@ -2778,7 +2803,9 @@ void mlx5_esw_offloads_unload_rep(struct
 
 	mlx5_esw_offloads_rep_unload(esw, vport->vport);
 
+#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
 	mlx5_esw_offloads_devlink_port_unregister(esw, vport);
+#endif
 }
 
 static int esw_set_slave_root_fdb(struct mlx5_core_dev *master,
@@ -3298,7 +3325,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)
@@ -3312,7 +3343,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,
@@ -3553,7 +3588,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
 	struct devlink *devlink;
+#endif
 	bool host_pf_disabled;
 	u16 new_num_vfs;
 
@@ -3565,8 +3602,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
 	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);
@@ -3576,12 +3615,16 @@ esw_vfs_changed_event_handler(struct mlx
 		err = mlx5_eswitch_load_vf_vports(esw, new_num_vfs,
 						  MLX5_VPORT_UC_ADDR_CHANGE);
 		if (err) {
+#ifdef HAVE_DEVL_PORT_REGISTER
 			devl_unlock(devlink);
+#endif
 			return;
 		}
 	}
 	esw->esw_funcs.num_vfs = new_num_vfs;
+#ifdef HAVE_DEVL_PORT_REGISTER
 	devl_unlock(devlink);
+#endif
 }
 
 static void esw_functions_changed_event_handler(struct work_struct *work)
@@ -3885,17 +3928,25 @@ bool mlx5_eswitch_mode_is_blocked(struct
 	return blocked;
 }
 
-int mlx5_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode,
-				  struct netlink_ext_ack *extack)
+int mlx5_devlink_eswitch_mode_set(struct devlink *devlink, u16 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 = NULL;
+#endif
 	u16 cur_mlx5_mode, mlx5_mode = 0;
 	struct mlx5_eswitch *esw;
 	int err = 0;
 
+#if defined(HAVE_LIGHT_SFS)
 	if (mlx5_dev_is_lightweight(devlink_priv(devlink))) {
 		NL_SET_ERR_MSG_MOD(extack, "Function doesn't fully probe.");
 		return -EOPNOTSUPP;
 	}
+#endif
 
 	esw = mlx5_devlink_eswitch_get(devlink);
 	if (IS_ERR(esw))
@@ -3934,12 +3985,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 skip;
 		}
+#endif
 		err = esw_offloads_start(esw, extack);
 	} else if (mode == DEVLINK_ESWITCH_MODE_LEGACY) {
 		err = esw_offloads_stop(esw, extack);
@@ -3947,8 +4000,9 @@ int mlx5_devlink_eswitch_mode_set(struct
 	} else {
 		err = -EINVAL;
 	}
-
+#ifdef HAVE_DEVLINK_TRAP_SUPPORT
 skip:
+#endif
 	down_write(&esw->mode_lock);
 	esw->eswitch_operation_in_progress = false;
 unlock:
@@ -4019,9 +4073,15 @@ 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 = NULL;
+#endif
 	struct mlx5_core_dev *dev = devlink_priv(devlink);
 	struct mlx5_eswitch *esw;
 	u8 mlx5_mode;
@@ -4120,9 +4180,19 @@ void mlx5_eswitch_unblock_encap(struct m
 }
 
 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 = NULL;
+#endif
 	struct mlx5_core_dev *dev = devlink_priv(devlink);
 	struct mlx5_eswitch *esw;
 	int err = 0;
@@ -4192,7 +4262,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;
 
@@ -4221,14 +4295,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))
@@ -4652,13 +4736,29 @@ u32 mlx5_eswitch_get_vport_metadata_for_
 }
 EXPORT_SYMBOL(mlx5_eswitch_get_vport_metadata_for_set);
 
-int mlx5_devlink_port_fn_hw_addr_get(struct devlink_port *port,
-				     u8 *hw_addr, int *hw_addr_len,
-				     struct netlink_ext_ack *extack)
+#if defined(HAVE_PORT_FUNCTION_HW_ADDR_GET_GET_4_PARAM) || defined(HAVE_DEVLINK_PORT_OPS)
+int mlx5_devlink_port_fn_hw_addr_get(
+#else
+int mlx5_devlink_port_fn_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 = mlx5_devlink_eswitch_nocheck_get(port->devlink);
+	struct mlx5_eswitch *esw;
 	struct mlx5_vport *vport = mlx5_devlink_port_vport_get(port);
 
+#if defined(HAVE_PORT_FUNCTION_HW_ADDR_GET_GET_4_PARAM) || defined(HAVE_DEVLINK_PORT_OPS)
+	esw = mlx5_devlink_eswitch_nocheck_get(port->devlink);
+#else
+	esw = mlx5_devlink_eswitch_nocheck_get(devlink);
+#endif
+
+#ifdef HAVE_BASECODE_EXTRAS
+	if (!esw || !vport)
+	return -EOPNOTSUPP;
+#endif
+
 	mutex_lock(&esw->state_lock);
 	ether_addr_copy(hw_addr, vport->info.mac);
 	*hw_addr_len = ETH_ALEN;
@@ -4666,16 +4766,27 @@ int mlx5_devlink_port_fn_hw_addr_get(str
 	return 0;
 }
 
-int mlx5_devlink_port_fn_hw_addr_set(struct devlink_port *port,
-				     const u8 *hw_addr, int hw_addr_len,
-				     struct netlink_ext_ack *extack)
+#if defined(HAVE_PORT_FUNCTION_HW_ADDR_GET_GET_4_PARAM) || defined(HAVE_DEVLINK_PORT_OPS)
+int mlx5_devlink_port_fn_hw_addr_set(
+#else
+int mlx5_devlink_port_fn_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 = mlx5_devlink_eswitch_nocheck_get(port->devlink);
+ 	struct mlx5_eswitch *esw;
 	struct mlx5_vport *vport = mlx5_devlink_port_vport_get(port);
 
+#if defined(HAVE_PORT_FUNCTION_HW_ADDR_GET_GET_4_PARAM) || defined(HAVE_DEVLINK_PORT_OPS)
+	esw = mlx5_devlink_eswitch_nocheck_get(port->devlink);
+#else
+	esw = mlx5_devlink_eswitch_nocheck_get(devlink);
+#endif
 	return mlx5_eswitch_set_vport_mac(esw, vport->vport, hw_addr);
 }
 
+#if defined(HAVE_DEVLINK_HAS_PORT_FN_ROCE_MIG) || defined(HAVE_DEVLINK_PORT_OPS)
 int mlx5_devlink_port_fn_migratable_get(struct devlink_port *port, bool *is_enabled,
 					struct netlink_ext_ack *extack)
 {
@@ -4828,6 +4939,7 @@ out:
 	mutex_unlock(&esw->state_lock);
 	return err;
 }
+#endif // HAVE_DEVLINK_HAS_PORT_FN_ROCE_MIG
 
 int
 mlx5_eswitch_restore_ipsec_rule(struct mlx5_eswitch *esw, struct mlx5_flow_handle *rule,
@@ -4846,6 +4958,7 @@ mlx5_eswitch_restore_ipsec_rule(struct m
 }
 
 #ifdef CONFIG_XFRM_OFFLOAD
+#ifdef HAVE_DEVLINK_IPSEC_CRYPTO
 int mlx5_devlink_port_fn_ipsec_crypto_get(struct devlink_port *port, bool *is_enabled,
 					  struct netlink_ext_ack *extack)
 {
@@ -4928,7 +5041,9 @@ unlock:
 	mutex_unlock(&esw->state_lock);
 	return err;
 }
+#endif /* HAVE_DEVLINK_IPSEC_CRYPTO */
 
+#ifdef HAVE_DEVLINK_IPSEC_PACKET
 int mlx5_devlink_port_fn_ipsec_packet_get(struct devlink_port *port, bool *is_enabled,
 					  struct netlink_ext_ack *extack)
 {
@@ -5012,6 +5127,7 @@ unlock:
 	mutex_unlock(&esw->state_lock);
 	return err;
 }
+#endif /* HAVE_DEVLINK_IPSEC_PACKET */
 #endif /* CONFIG_XFRM_OFFLOAD */
 
 int
