// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
/* Copyright (c) 2020 Mellanox Technologies Ltd. */

#include <linux/mlx5/driver.h>
#ifdef HAVE_BASECODE_EXTRAS
#include <net/devlink.h>
#endif
#include "eswitch.h"
#include "mlx5_esw_devm.h"

#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
static void
mlx5_esw_get_port_parent_id(struct mlx5_core_dev *dev, struct netdev_phys_item_id *ppid)
{
	u64 parent_id;

	parent_id = mlx5_query_nic_system_image_guid(dev);
	ppid->id_len = sizeof(parent_id);
	memcpy(ppid->id, &parent_id, sizeof(parent_id));
}
#endif

#ifdef HAVE_DEVLINK_PORT_TYPE_ETH_SET
static bool mlx5_esw_devlink_port_supported(struct mlx5_eswitch *esw, u16 vport_num)
{
	return vport_num == MLX5_VPORT_UPLINK ||
	       (mlx5_core_is_ecpf(esw->dev) && vport_num == MLX5_VPORT_PF) ||
	       mlx5_eswitch_is_vf_vport(esw, vport_num) ||
	       mlx5_core_is_ec_vf_vport(esw->dev, vport_num);
}

static struct devlink_port *mlx5_esw_dl_port_alloc(struct mlx5_eswitch *esw, u16 vport_num)
{
#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
	struct mlx5_core_dev *dev = esw->dev;
#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_2_PARAMS
	struct devlink_port_attrs attrs = {};
#endif
	struct netdev_phys_item_id ppid = {};
	struct devlink_port *dl_port;
#if defined(HAVE_DEVLINK_PORT_ATTRS_PCI_PF_SET_CONTROLLER_NUM)
	u32 controller_num = 0;
	bool external;
#endif
	u16 pfnum;

	dl_port = kzalloc(sizeof(*dl_port), GFP_KERNEL);
	if (!dl_port)
		return NULL;

	mlx5_esw_get_port_parent_id(dev, &ppid);
	pfnum = mlx5_get_dev_index(dev);
#if defined(HAVE_DEVLINK_PORT_ATTRS_PCI_PF_SET_CONTROLLER_NUM)
	external = mlx5_core_is_ecpf_esw_manager(dev);
	if (external)
		controller_num = dev->priv.eswitch->offloads.host_number + 1;
#endif

	if (vport_num == MLX5_VPORT_UPLINK) {
#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_2_PARAMS
		attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
		attrs.phys.port_number = pfnum;
		memcpy(attrs.switch_id.id, ppid.id, ppid.id_len);
		attrs.switch_id.id_len = ppid.id_len;
		devlink_port_attrs_set(dl_port, &attrs);
#else
		devlink_port_attrs_set(dl_port,
				DEVLINK_PORT_FLAVOUR_PHYSICAL,
				mlx5_get_dev_index(dev),
				false, 0
#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_7_PARAMS
				,NULL, 0
#endif
		);
#endif
	} 
#ifdef HAVE_DEVLINK_PORT_ATTRS_PCI_PF_SET
	else if (vport_num == MLX5_VPORT_PF) {
		memcpy(dl_port->attrs.switch_id.id, ppid.id, ppid.id_len);
		dl_port->attrs.switch_id.id_len = ppid.id_len;
#if defined(HAVE_DEVLINK_PORT_ATTRS_PCI_PF_SET_CONTROLLER_NUM)
		devlink_port_attrs_pci_pf_set(dl_port, controller_num, pfnum, external);
#elif defined(HAVE_DEVLINK_PORT_ATTRS_PCI_PF_SET_GET_2_PARAMS)
		devlink_port_attrs_pci_pf_set(dl_port, pfnum); 
#else 
		devlink_port_attrs_pci_pf_set(dl_port,
				&ppid.id[0], ppid.id_len,
				pfnum);
#endif
	} else if (mlx5_eswitch_is_vf_vport(esw, vport_num)) {
		memcpy(dl_port->attrs.switch_id.id, ppid.id, ppid.id_len);
		dl_port->attrs.switch_id.id_len = ppid.id_len;
#if defined(HAVE_DEVLINK_PORT_ATTRS_PCI_VF_SET_GET_CONTROLLER_NUM)
		devlink_port_attrs_pci_vf_set(dl_port, controller_num, pfnum,
					      vport_num - 1, external);
#elif defined(HAVE_DEVLINK_PORT_ATTRS_PCI_VF_SET_GET_3_PARAMS)
		devlink_port_attrs_pci_vf_set(dl_port, pfnum, vport_num - 1);
#elif defined(HAVE_DEVLINK_PORT_ATTRS_PCI_VF_SET_GET_5_PARAMS)
		devlink_port_attrs_pci_vf_set(dl_port,
				&ppid.id[0], ppid.id_len,
				pfnum, vport_num - 1);
#endif
	}  else if (mlx5_core_is_ec_vf_vport(esw->dev, vport_num)) {
		memcpy(dl_port->attrs.switch_id.id, ppid.id, ppid.id_len);
		dl_port->attrs.switch_id.id_len = ppid.id_len;
#if defined(HAVE_DEVLINK_PORT_ATTRS_PCI_VF_SET_GET_CONTROLLER_NUM)
		devlink_port_attrs_pci_vf_set(dl_port, 0, pfnum,
					      vport_num - 1, false);
#elif defined(HAVE_DEVLINK_PORT_ATTRS_PCI_VF_SET_GET_3_PARAMS)
		devlink_port_attrs_pci_vf_set(dl_port, pfnum, vport_num - 1);
#elif defined(HAVE_DEVLINK_PORT_ATTRS_PCI_VF_SET_GET_5_PARAMS)
		devlink_port_attrs_pci_vf_set(dl_port,
				&ppid.id[0], ppid.id_len,
				pfnum, vport_num - 1);
#endif
	}
#else
	else
                devlink_port_attrs_set(dl_port,
                                DEVLINK_PORT_FLAVOUR_VIRTUAL,
                                0, false , 0
#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_7_PARAMS
                                ,NULL, 0
#endif
		);
#endif /* HAVE_DEVLINK_PORT_ATTRS_PCI_PF_SET */
	return dl_port;
#else
	return NULL;
#endif /* HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT */
}

static void mlx5_esw_dl_port_free(struct devlink_port *dl_port)
{
	kfree(dl_port);
}
#endif

#ifdef HAVE_DEVLINK_PORT_OPS
static const struct devlink_port_ops mlx5_esw_dl_port_ops = {
	.port_fn_hw_addr_get = mlx5_devlink_port_function_hw_addr_get,
	.port_fn_hw_addr_set = mlx5_devlink_port_function_hw_addr_set,
	.port_fn_roce_get = mlx5_devlink_port_fn_roce_get,
	.port_fn_roce_set = mlx5_devlink_port_fn_roce_set,
	.port_fn_migratable_get = mlx5_devlink_port_fn_migratable_get,
	.port_fn_migratable_set = mlx5_devlink_port_fn_migratable_set,
#ifdef CONFIG_XFRM_OFFLOAD
#ifdef HAVE_DEVLINK_IPSEC_CRYPTO
	.port_fn_ipsec_crypto_get = mlx5_devlink_port_fn_ipsec_crypto_get,
	.port_fn_ipsec_crypto_set = mlx5_devlink_port_fn_ipsec_crypto_set,
#endif /* HAVE_DEVLINK_IPSEC_CRYPTO */
#ifdef HAVE_DEVLINK_IPSEC_PACKET
	.port_fn_ipsec_packet_get = mlx5_devlink_port_fn_ipsec_packet_get,
	.port_fn_ipsec_packet_set = mlx5_devlink_port_fn_ipsec_packet_set,
#endif /* HAVE_DEVLINK_IPSEC_PACKET */
#endif /* CONFIG_XFRM_OFFLOAD */
};
#endif

int mlx5_esw_offloads_devlink_port_register(struct mlx5_eswitch *esw, u16 vport_num)
{
#ifdef HAVE_DEVLINK_PORT_TYPE_ETH_SET
	struct mlx5_core_dev *dev = esw->dev;
	struct devlink_port *dl_port;
	unsigned int dl_port_index;
	struct mlx5_vport *vport;
	struct devlink *devlink;
	int err;

	if (!mlx5_esw_devlink_port_supported(esw, vport_num))
		return 0;

	vport = mlx5_eswitch_get_vport(esw, vport_num);
	if (IS_ERR(vport))
		return PTR_ERR(vport);

	dl_port = mlx5_esw_dl_port_alloc(esw, vport_num);
	if (!dl_port)
		return -ENOMEM;

	devlink = priv_to_devlink(dev);
	dl_port_index = mlx5_esw_vport_to_devlink_port_index(dev, vport_num);
#ifdef HAVE_DEVLINK_PORT_OPS
	err = devl_port_register_with_ops(devlink, dl_port, dl_port_index,
					  &mlx5_esw_dl_port_ops);
#elif defined(HAVE_DEVL_PORT_REGISTER)
	err = devl_port_register(devlink, dl_port, dl_port_index);
#else
	err = devlink_port_register(devlink, dl_port, dl_port_index);
#endif
	if (err)
		goto reg_err;

#ifdef HAVE_DEVLINK_HAS_RATE_FUNCTIONS
#ifdef HAVE_DEVL_PORT_REGISTER
#ifdef HAVE_DEVL_RATE_LEAF_CREATE_GET_3_PARAMS
	err = devl_rate_leaf_create(dl_port, vport, NULL);
#else
	err = devl_rate_leaf_create(dl_port, vport);
#endif /* HAVE_DEVL_RATE_LEAF_CREATE_GET_3_PARAMS */ 
#else
	err = devlink_rate_leaf_create(dl_port, vport);
#endif
	if (err)
		goto rate_err;
#endif

	vport->dl_port = dl_port;
	return 0;

#ifdef HAVE_DEVLINK_HAS_RATE_FUNCTIONS
rate_err:
#ifdef HAVE_DEVL_PORT_REGISTER
	devl_port_unregister(dl_port);
#else
	devlink_port_unregister(dl_port);
#endif
#endif
reg_err:
	mlx5_esw_dl_port_free(dl_port);
	return err;
#else
	return 0;
#endif
}

void mlx5_esw_offloads_devlink_port_unregister(struct mlx5_eswitch *esw, u16 vport_num)
{
#ifdef HAVE_DEVLINK_PORT_TYPE_ETH_SET
	struct mlx5_vport *vport;

	if (!mlx5_esw_devlink_port_supported(esw, vport_num))
		return;

	vport = mlx5_eswitch_get_vport(esw, vport_num);
	if (IS_ERR(vport))
		return;

#ifdef HAVE_DEVLINK_HAS_RATE_FUNCTIONS
	if (vport->dl_port->devlink_rate) {
		if (!test_bit(MLX5_BREAK_FW_WAIT, &esw->dev->intf_state))
			mlx5_esw_qos_vport_update_group(esw, vport, NULL, NULL);
#ifdef HAVE_DEVL_PORT_REGISTER
		devl_rate_leaf_destroy(vport->dl_port);
#else
 		devlink_rate_leaf_destroy(vport->dl_port);
#endif
	}
#endif

#ifdef HAVE_DEVL_PORT_REGISTER
	devl_port_unregister(vport->dl_port);
#else
 	devlink_port_unregister(vport->dl_port);
#endif
 	mlx5_esw_dl_port_free(vport->dl_port);
 	vport->dl_port = NULL;
#endif
}

struct devlink_port *mlx5_esw_offloads_devlink_port(struct mlx5_eswitch *esw, u16 vport_num)
{
	struct mlx5_vport *vport;

	vport = mlx5_eswitch_get_vport(esw, vport_num);
	return IS_ERR(vport) ? ERR_CAST(vport) : vport->dl_port;
}

#ifdef HAVE_DEVLINK_PORT_OPS
static const struct devlink_port_ops mlx5_esw_dl_sf_port_ops = {
#ifdef CONFIG_MLX5_SF_MANAGER
	.port_del = mlx5_devlink_sf_port_del,
#endif
	.port_fn_hw_addr_get = mlx5_devlink_port_function_hw_addr_get,
	.port_fn_hw_addr_set = mlx5_devlink_port_function_hw_addr_set,
	.port_fn_roce_get = mlx5_devlink_port_fn_roce_get,
	.port_fn_roce_set = mlx5_devlink_port_fn_roce_set,
#ifdef CONFIG_MLX5_SF_MANAGER
	.port_fn_state_get = mlx5_devlink_sf_port_fn_state_get,
	.port_fn_state_set = mlx5_devlink_sf_port_fn_state_set,
#endif
};
#endif /* HAVE_DEVLINK_PORT_OPS */


int _mlx5_esw_devlink_sf_port_register(struct mlx5_eswitch *esw, struct devlink_port *dl_port,
				      u16 vport_num, u32 controller, u32 sfnum)
{
#if defined(HAVE_DEVLINK_PORT_ATTRS_PCI_SF_SET_GET_4_PARAMS) || defined(HAVE_DEVLINK_PORT_ATTRS_PCI_SF_SET_GET_5_PARAMS)
	struct mlx5_core_dev *dev = esw->dev;
	struct netdev_phys_item_id ppid = {};
	unsigned int dl_port_index;
	struct mlx5_vport *vport;
	struct devlink *devlink;
	u16 pfnum;
	int err;

	vport = mlx5_eswitch_get_vport(esw, vport_num);
	if (IS_ERR(vport))
		return PTR_ERR(vport);

	pfnum = mlx5_get_dev_index(dev);
	mlx5_esw_get_port_parent_id(dev, &ppid);
	memcpy(dl_port->attrs.switch_id.id, &ppid.id[0], ppid.id_len);
	dl_port->attrs.switch_id.id_len = ppid.id_len;
#if defined(HAVE_DEVLINK_PORT_ATTRS_PCI_SF_SET_GET_5_PARAMS)
	devlink_port_attrs_pci_sf_set(dl_port, controller, pfnum, sfnum, !!controller);
#elif defined(HAVE_DEVLINK_PORT_ATTRS_PCI_SF_SET_GET_4_PARAMS)
	devlink_port_attrs_pci_sf_set(dl_port, controller, pfnum, sfnum);
#endif
	devlink = priv_to_devlink(dev);
	dl_port_index = mlx5_esw_vport_to_devlink_port_index(dev, vport_num);
#ifdef HAVE_DEVLINK_PORT_OPS
	err = devl_port_register_with_ops(devlink, dl_port, dl_port_index,
					  &mlx5_esw_dl_sf_port_ops);
#elif defined(HAVE_DEVL_PORT_REGISTER)
	err = devl_port_register(devlink, dl_port, dl_port_index);
#else
	err = devlink_port_register(devlink, dl_port, dl_port_index);
#endif
	if (err)
		return err;

#ifdef HAVE_DEVLINK_HAS_RATE_FUNCTIONS
#ifdef HAVE_DEVL_PORT_REGISTER
#ifdef HAVE_DEVL_RATE_LEAF_CREATE_GET_3_PARAMS
	err = devl_rate_leaf_create(dl_port, vport, NULL);
#else
	err = devl_rate_leaf_create(dl_port, vport);
#endif /* HAVE_DEVL_RATE_LEAF_CREATE_GET_3_PARAMS */ 
#else
	err = devlink_rate_leaf_create(dl_port, vport);
#endif
	if (err)
		goto rate_err;
#endif

	vport->dl_port = dl_port;
	return 0;

#ifdef HAVE_DEVLINK_HAS_RATE_FUNCTIONS
rate_err:
#ifdef HAVE_DEVL_PORT_REGISTER
	devl_port_unregister(dl_port);
#else
 	devlink_port_unregister(dl_port);
#endif
	return err;
#endif
#else /* defined(HAVE_DEVLINK_PORT_ATTRS_PCI_SF_SET_GET_4_PARAMS) || defined(HAVE_DEVLINK_PORT_ATTRS_PCI_SF_SET_GET_5_PARAMS) */
 
	return -EOPNOTSUPP;
#endif /* defined(HAVE_DEVLINK_PORT_ATTRS_PCI_SF_SET_GET_4_PARAMS) || defined(HAVE_DEVLINK_PORT_ATTRS_PCI_SF_SET_GET_5_PARAMS) */
}

int mlx5_esw_devlink_sf_port_register(struct mlx5_eswitch *esw,
				      struct devlink_port *dl_port,
				      u16 vport_num, u32 controller,
				      u32 sfnum)
{
	int err = 0;

#if defined(HAVE_DEVLINK_PORT_ATTRS_PCI_SF_SET_GET_4_PARAMS) || defined(HAVE_DEVLINK_PORT_ATTRS_PCI_SF_SET_GET_5_PARAMS)
	err = _mlx5_esw_devlink_sf_port_register(esw, dl_port, vport_num,
						 controller, sfnum);
	if (err)
		return err;
#endif

#if IS_ENABLED(CONFIG_MLXDEVM)
	err = mlx5_devm_sf_port_register(esw->dev, vport_num, controller, sfnum, dl_port);
#endif
	return err;
}

void _mlx5_esw_devlink_sf_port_unregister(struct mlx5_eswitch *esw, u16 vport_num)
{
#if defined(HAVE_DEVLINK_PORT_ATTRS_PCI_SF_SET_GET_4_PARAMS) || defined(HAVE_DEVLINK_PORT_ATTRS_PCI_SF_SET_GET_5_PARAMS)
	struct mlx5_vport *vport;

	vport = mlx5_eswitch_get_vport(esw, vport_num);
	if (IS_ERR(vport))
		return;

#ifdef HAVE_DEVLINK_HAS_RATE_FUNCTIONS
	if (vport->dl_port->devlink_rate) {
#if !IS_ENABLED(CONFIG_MLXDEVM)
		mlx5_esw_qos_vport_update_group(esw, vport, NULL, NULL);
#endif
#ifdef HAVE_DEVL_PORT_REGISTER
		devl_rate_leaf_destroy(vport->dl_port);
#else
		devlink_rate_leaf_destroy(vport->dl_port);
#endif
	}
#endif

#ifdef HAVE_DEVL_PORT_REGISTER
	devl_port_unregister(vport->dl_port);
#else
	devlink_port_unregister(vport->dl_port);
#endif
	vport->dl_port = NULL;
#endif /* defined(HAVE_DEVLINK_PORT_ATTRS_PCI_SF_SET_GET_4_PARAMS) || defined(HAVE_DEVLINK_PORT_ATTRS_PCI_SF_SET_GET_5_PARAMS) */
}

void mlx5_esw_devlink_sf_port_unregister(struct mlx5_eswitch *esw,
					 u16 vport_num)
{
#if defined(HAVE_DEVLINK_PORT_ATTRS_PCI_SF_SET_GET_4_PARAMS) || defined(HAVE_DEVLINK_PORT_ATTRS_PCI_SF_SET_GET_5_PARAMS)
	_mlx5_esw_devlink_sf_port_unregister(esw, vport_num);
#endif
#if IS_ENABLED(CONFIG_MLXDEVM)
	mlx5_devm_sf_port_unregister(esw->dev, vport_num);
#endif
}
