/*
 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <linux/mlx5/fs.h>
#include <net/switchdev.h>
#include <net/pkt_cls.h>
#include <net/act_api.h>
#include <net/devlink.h>
#include <net/ipv6_stubs.h>

#include "eswitch.h"
#include "mlx5_esw_devm.h"
#include "en.h"
#include "en_rep.h"
#include "en/params.h"
#include "en/txrx.h"
#include "en_tc.h"
#include "en/rep/tc.h"
#include "en/rep/neigh.h"
#include "en/rep/bridge.h"
#include "en/rep/sysfs.h"
#include "en/rep/meter.h"
#include "en/devlink.h"
#include "fs_core.h"
#include "ecpf.h"
#include "lib/mlx5.h"
#include "lib/devcom.h"
#include "lib/vxlan.h"
#define CREATE_TRACE_POINTS
#include "diag/en_rep_tracepoint.h"
#include "en_accel/ipsec.h"
#include "en/ptp.h"
#include <generated/utsrelease.h>
#include "compat.h"

#define MLX5E_REP_SF_ENABLED_MAX_NUM_CHANNELS 4
#define MLX5E_REP_PARAMS_DEF_NUM_CHANNELS 1

static const char mlx5e_rep_driver_name[] = "mlx5e_rep";

static void mlx5e_rep_get_drvinfo(struct net_device *dev,
				  struct ethtool_drvinfo *drvinfo)
{
	struct mlx5e_priv *priv = netdev_priv(dev);
	struct mlx5_core_dev *mdev = priv->mdev;

	strlcpy(drvinfo->driver, mlx5e_rep_driver_name,
		sizeof(drvinfo->driver));
	strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
		 "%d.%d.%04d (%.16s)",
		 fw_rev_maj(mdev), fw_rev_min(mdev),
		 fw_rev_sub(mdev), mdev->board_id);
}

static const struct counter_desc sw_rep_stats_desc[] = {
	{ MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_packets) },
	{ MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_bytes) },
	{ MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_packets) },
	{ MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_bytes) },
};

struct vport_stats {
	u64 vport_rx_packets;
	u64 vport_tx_packets;
	u64 vport_rx_bytes;
	u64 vport_tx_bytes;
};

static const struct counter_desc vport_rep_stats_desc[] = {
	{ MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_packets) },
	{ MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_bytes) },
	{ MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_packets) },
	{ MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_bytes) },
};

#define NUM_VPORT_REP_SW_COUNTERS ARRAY_SIZE(sw_rep_stats_desc)
#define NUM_VPORT_REP_HW_COUNTERS ARRAY_SIZE(vport_rep_stats_desc)

static MLX5E_DECLARE_STATS_GRP_OP_NUM_STATS(sw_rep)
{
	return NUM_VPORT_REP_SW_COUNTERS;
}

static MLX5E_DECLARE_STATS_GRP_OP_FILL_STRS(sw_rep)
{
	int i;

	for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
		strcpy(data + (idx++) * ETH_GSTRING_LEN,
		       sw_rep_stats_desc[i].format);
	return idx;
}

static MLX5E_DECLARE_STATS_GRP_OP_FILL_STATS(sw_rep)
{
	int i;

	for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
		data[idx++] = MLX5E_READ_CTR64_CPU(&priv->stats.sw,
						   sw_rep_stats_desc, i);
	return idx;
}

static MLX5E_DECLARE_STATS_GRP_OP_UPDATE_STATS(sw_rep)
{
	struct mlx5e_sw_stats *s = &priv->stats.sw;
	struct rtnl_link_stats64 stats64 = {};

	memset(s, 0, sizeof(*s));
	mlx5e_fold_sw_stats64(priv, &stats64);

	s->rx_packets = stats64.rx_packets;
	s->rx_bytes   = stats64.rx_bytes;
	s->tx_packets = stats64.tx_packets;
	s->tx_bytes   = stats64.tx_bytes;
	s->tx_queue_dropped = stats64.tx_dropped;
}

static MLX5E_DECLARE_STATS_GRP_OP_NUM_STATS(vport_rep)
{
	return NUM_VPORT_REP_HW_COUNTERS;
}

static MLX5E_DECLARE_STATS_GRP_OP_FILL_STRS(vport_rep)
{
	int i;

	for (i = 0; i < NUM_VPORT_REP_HW_COUNTERS; i++)
		strcpy(data + (idx++) * ETH_GSTRING_LEN, vport_rep_stats_desc[i].format);
	return idx;
}

static MLX5E_DECLARE_STATS_GRP_OP_FILL_STATS(vport_rep)
{
	int i;

	for (i = 0; i < NUM_VPORT_REP_HW_COUNTERS; i++)
		data[idx++] = MLX5E_READ_CTR64_CPU(&priv->stats.vf_vport,
						   vport_rep_stats_desc, i);
	return idx;
}

static MLX5E_DECLARE_STATS_GRP_OP_UPDATE_STATS(vport_rep)
{
	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	struct mlx5_eswitch_rep *rep = rpriv->rep;
	struct rtnl_link_stats64 *vport_stats;
	struct ifla_vf_stats vf_stats;
	int err;

	err = mlx5_eswitch_get_vport_stats(esw, rep->vport, &vf_stats);
	if (err) {
		netdev_warn(priv->netdev, "vport %d error %d reading stats\n",
			    rep->vport, err);
		return;
	}

	vport_stats = &priv->stats.vf_vport;
	/* flip tx/rx as we are reporting the counters for the switch vport */
	vport_stats->rx_packets = vf_stats.tx_packets;
	vport_stats->rx_bytes   = vf_stats.tx_bytes;
	vport_stats->tx_packets = vf_stats.rx_packets;
	vport_stats->tx_bytes   = vf_stats.rx_bytes;
}

static void mlx5e_rep_get_strings(struct net_device *dev,
				  u32 stringset, uint8_t *data)
{
	struct mlx5e_priv *priv = netdev_priv(dev);

	switch (stringset) {
	case ETH_SS_STATS:
		mlx5e_stats_fill_strings(priv, data);
		break;
	}
}

static void mlx5e_rep_get_ethtool_stats(struct net_device *dev,
					struct ethtool_stats *stats, u64 *data)
{
	struct mlx5e_priv *priv = netdev_priv(dev);

	mlx5e_ethtool_get_ethtool_stats(priv, stats, data);
}

static int mlx5e_rep_get_sset_count(struct net_device *dev, int sset)
{
	struct mlx5e_priv *priv = netdev_priv(dev);

	switch (sset) {
	case ETH_SS_STATS:
		return mlx5e_stats_total_num(priv);
	default:
		return -EOPNOTSUPP;
	}
}

static void
mlx5e_rep_get_ringparam(struct net_device *dev,
#ifdef HAVE_GET_RINGPARAM_GET_4_PARAMS
			struct ethtool_ringparam *param,
			struct kernel_ethtool_ringparam *kernel_param,
			struct netlink_ext_ack *extack)
#else
			struct ethtool_ringparam *param)
#endif
{
	struct mlx5e_priv *priv = netdev_priv(dev);

	mlx5e_ethtool_get_ringparam(priv, param);
}

static int
mlx5e_rep_set_ringparam(struct net_device *dev,
#ifdef HAVE_GET_RINGPARAM_GET_4_PARAMS
			struct ethtool_ringparam *param,
			struct kernel_ethtool_ringparam *kernel_param,
			struct netlink_ext_ack *extack)
#else
			struct ethtool_ringparam *param)
#endif
{
	struct mlx5e_priv *priv = netdev_priv(dev);

	return mlx5e_ethtool_set_ringparam(priv, param);
}

static void mlx5e_rep_get_channels(struct net_device *dev,
				   struct ethtool_channels *ch)
{
	struct mlx5e_priv *priv = netdev_priv(dev);

	mlx5e_ethtool_get_channels(priv, ch);
}

static int mlx5e_rep_set_channels(struct net_device *dev,
				  struct ethtool_channels *ch)
{
	struct mlx5e_priv *priv = netdev_priv(dev);

	return mlx5e_ethtool_set_channels(priv, ch);
}

static int mlx5e_rep_get_coalesce(struct net_device *netdev,
#ifdef HAVE_NDO_GET_COALESCE_GET_4_PARAMS
			      struct ethtool_coalesce *coal,
			      struct kernel_ethtool_coalesce *kernel_coal,
			      struct netlink_ext_ack *extack)
#else
			      struct ethtool_coalesce *coal)
#endif
{
	struct mlx5e_priv *priv = netdev_priv(netdev);

	return mlx5e_ethtool_get_coalesce(priv,
#ifdef HAVE_NDO_GET_COALESCE_GET_4_PARAMS //forwardport
					  coal, kernel_coal);
#else
					  coal);
#endif
}

static int mlx5e_rep_set_coalesce(struct net_device *netdev,
#ifdef HAVE_NDO_GET_COALESCE_GET_4_PARAMS
			      struct ethtool_coalesce *coal,
			      struct kernel_ethtool_coalesce *kernel_coal,
			      struct netlink_ext_ack *extack)
#else
			      struct ethtool_coalesce *coal)
#endif
{
	struct mlx5e_priv *priv = netdev_priv(netdev);

	return mlx5e_ethtool_set_coalesce(priv,
#ifdef HAVE_NDO_GET_COALESCE_GET_4_PARAMS //forwardport
					  coal, kernel_coal, extack);
#else
					  coal);
#endif
}

static u32 mlx5e_rep_get_rxfh_key_size(struct net_device *netdev)
{
	struct mlx5e_priv *priv = netdev_priv(netdev);

	return mlx5e_ethtool_get_rxfh_key_size(priv);
}

static u32 mlx5e_rep_get_rxfh_indir_size(struct net_device *netdev)
{
	struct mlx5e_priv *priv = netdev_priv(netdev);

	return mlx5e_ethtool_get_rxfh_indir_size(priv);
}

static const struct ethtool_ops mlx5e_rep_ethtool_ops = {
#ifdef HAVE_SUPPORTED_COALESCE_PARAM
	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
				     ETHTOOL_COALESCE_MAX_FRAMES |
				     ETHTOOL_COALESCE_USE_ADAPTIVE,
#endif
	.get_drvinfo	   = mlx5e_rep_get_drvinfo,
	.get_link	   = ethtool_op_get_link,
	.get_strings       = mlx5e_rep_get_strings,
	.get_sset_count    = mlx5e_rep_get_sset_count,
	.get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
#ifdef HAVE_GET_SET_LINK_KSETTINGS
	.get_link_ksettings  = mlx5e_get_link_ksettings,
	.set_link_ksettings  = mlx5e_set_link_ksettings,
#endif
	.get_ringparam     = mlx5e_rep_get_ringparam,
	.set_ringparam     = mlx5e_rep_set_ringparam,
	.get_channels      = mlx5e_rep_get_channels,
	.set_channels      = mlx5e_rep_set_channels,
	.get_coalesce      = mlx5e_rep_get_coalesce,
	.set_coalesce      = mlx5e_rep_set_coalesce,
	.get_rxfh_key_size   = mlx5e_rep_get_rxfh_key_size,
	.get_rxfh_indir_size = mlx5e_rep_get_rxfh_indir_size,
};

static void mlx5e_sqs2vport_stop(struct mlx5_eswitch *esw,
				 struct mlx5_eswitch_rep *rep)
{
	struct mlx5e_rep_sq *rep_sq, *tmp;
	struct mlx5e_rep_priv *rpriv;

	if (esw->mode != MLX5_ESWITCH_OFFLOADS)
		return;

	rpriv = mlx5e_rep_to_rep_priv(rep);
	list_for_each_entry_safe(rep_sq, tmp, &rpriv->vport_sqs_list, list) {
		mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule);
		if (rep_sq->send_to_vport_rule_peer)
			mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule_peer);
		list_del(&rep_sq->list);
		kfree(rep_sq);
	}
}

static int mlx5e_sqs2vport_start(struct mlx5_eswitch *esw,
				 struct mlx5_eswitch_rep *rep,
				 u32 *sqns_array, int sqns_num)
{
	struct mlx5_eswitch *peer_esw = NULL;
	struct mlx5_flow_handle *flow_rule;
	struct mlx5e_rep_priv *rpriv;
	struct mlx5e_rep_sq *rep_sq;
	int err;
	int i;

	if (esw->mode != MLX5_ESWITCH_OFFLOADS)
		return 0;

	rpriv = mlx5e_rep_to_rep_priv(rep);
	if (mlx5_devcom_is_paired(esw->dev->priv.devcom, MLX5_DEVCOM_ESW_OFFLOADS))
		peer_esw = mlx5_devcom_get_peer_data(esw->dev->priv.devcom,
						     MLX5_DEVCOM_ESW_OFFLOADS);

	for (i = 0; i < sqns_num; i++) {
		rep_sq = kzalloc(sizeof(*rep_sq), GFP_KERNEL);
		if (!rep_sq) {
			err = -ENOMEM;
			goto out_err;
		}

		/* Add re-inject rule to the PF/representor sqs */
		flow_rule = mlx5_eswitch_add_send_to_vport_rule(esw, esw, rep,
								sqns_array[i]);
		if (IS_ERR(flow_rule)) {
			err = PTR_ERR(flow_rule);
			kfree(rep_sq);
			goto out_err;
		}
		rep_sq->send_to_vport_rule = flow_rule;
		rep_sq->sqn = sqns_array[i];

		if (peer_esw) {
			flow_rule = mlx5_eswitch_add_send_to_vport_rule(peer_esw, esw,
									rep, sqns_array[i]);
			if (IS_ERR(flow_rule)) {
				err = PTR_ERR(flow_rule);
				mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule);
				kfree(rep_sq);
				goto out_err;
			}
			rep_sq->send_to_vport_rule_peer = flow_rule;
		}

		list_add(&rep_sq->list, &rpriv->vport_sqs_list);
	}

	if (peer_esw)
		mlx5_devcom_release_peer_data(esw->dev->priv.devcom, MLX5_DEVCOM_ESW_OFFLOADS);

	return 0;

out_err:
	mlx5e_sqs2vport_stop(esw, rep);

	if (peer_esw)
		mlx5_devcom_release_peer_data(esw->dev->priv.devcom, MLX5_DEVCOM_ESW_OFFLOADS);

	return err;
}

int mlx5e_add_sqs_fwd_rules(struct mlx5e_priv *priv)
{
	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	struct mlx5_eswitch_rep *rep = rpriv->rep;
	struct mlx5e_channel *c;
	int n, tc, num_sqs = 0;
	int err = -ENOMEM;
	bool ptp_sq;
	u32 *sqs;
	int num_txqs = priv->channels.params.num_channels * priv->channels.params.num_tc;

#ifdef CONFIG_MLX5_EN_SPECIAL_SQ
	num_txqs += priv->channels.params.num_rl_txqs;
#endif
	ptp_sq = !!(priv->channels.ptp &&
		    MLX5E_GET_PFLAG(&priv->channels.params, MLX5E_PFLAG_TX_PORT_TS));
	num_txqs += ptp_sq ? priv->channels.ptp->num_tc : 0;

	sqs = kcalloc(num_txqs, sizeof(*sqs), GFP_KERNEL);
	if (!sqs)
		goto out;

	for (n = 0; n < priv->channels.num; n++) {
		c = priv->channels.c[n];
		for (tc = 0; tc < c->num_tc; tc++)
			sqs[num_sqs++] = c->sq[tc].sqn;
#ifdef CONFIG_MLX5_EN_SPECIAL_SQ
		for (tc = 0; tc < c->num_special_sq; tc++)
			sqs[num_sqs++] = c->special_sq[tc].sqn;
#endif
	}
	if (ptp_sq) {
		struct mlx5e_ptp *ptp_ch = priv->channels.ptp;

		for (tc = 0; tc < ptp_ch->num_tc; tc++)
			sqs[num_sqs++] = ptp_ch->ptpsq[tc].txqsq.sqn;
	}

	err = mlx5e_sqs2vport_start(esw, rep, sqs, num_sqs);
	kfree(sqs);

out:
	if (err)
		netdev_warn(priv->netdev, "Failed to add SQs FWD rules %d\n", err);
	return err;
}

void mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv)
{
	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	struct mlx5_eswitch_rep *rep = rpriv->rep;

	mlx5e_sqs2vport_stop(esw, rep);
}

static int mlx5e_rep_open(struct net_device *dev)
{
	struct mlx5e_priv *priv = netdev_priv(dev);
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	struct mlx5_eswitch_rep *rep = rpriv->rep;
	int err;

	mutex_lock(&priv->state_lock);
	err = mlx5e_open_locked(dev);
	if (err)
		goto unlock;

	if (!mlx5_modify_vport_admin_state(priv->mdev,
					   MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
					   rep->vport, 1,
					   MLX5_VPORT_ADMIN_STATE_UP))
		netif_carrier_on(dev);

unlock:
	mutex_unlock(&priv->state_lock);
	return err;
}

static int mlx5e_rep_close(struct net_device *dev)
{
	struct mlx5e_priv *priv = netdev_priv(dev);
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	struct mlx5_eswitch_rep *rep = rpriv->rep;
	int ret;

	mutex_lock(&priv->state_lock);
	mlx5_modify_vport_admin_state(priv->mdev,
				      MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
				      rep->vport, 1,
				      MLX5_VPORT_ADMIN_STATE_DOWN);
	ret = mlx5e_close_locked(dev);
	mutex_unlock(&priv->state_lock);
	return ret;
}

#ifndef HAVE_DEVLINK_PORT_ATTRS_PCI_PF_SET
int mlx5e_rep_get_phys_port_name(struct net_device *dev,
				 char *buf, size_t len)
{
	struct mlx5e_priv *priv = netdev_priv(dev);
	struct mlx5e_rep_priv *rpriv;
	struct mlx5_eswitch_rep *rep;
	struct mlx5_eswitch *esw;
	int ret = -EOPNOTSUPP;
	unsigned int fn;

	if (!netif_device_present(dev))
		return -EOPNOTSUPP;

	esw = priv->mdev->priv.eswitch;
	if (!esw || esw->mode == MLX5_ESWITCH_NONE)
		return -EOPNOTSUPP;

	fn = mlx5_get_dev_index(priv->mdev);
	if (fn >= MLX5_MAX_PORTS)
		return -EOPNOTSUPP;

	rpriv = priv->ppriv;
	if (!rpriv)
		return -EOPNOTSUPP;
	rep = rpriv->rep;

	if (rep->vport == MLX5_VPORT_UPLINK)
		ret = snprintf(buf, len, "p%d", fn);
	else if (rep->vport == MLX5_VPORT_PF)
		ret = snprintf(buf, len, "pf%d", fn);
	else if (mlx5_eswitch_is_vf_vport(priv->mdev->priv.eswitch, rep->vport))
		ret = snprintf(buf, len, "pf%dvf%d", fn, rep->vport - 1);
	else 
		return -EOPNOTSUPP;

	if (ret >= len)
		return -EOPNOTSUPP;

	return 0;
}
#endif

bool mlx5e_is_uplink_rep(const struct mlx5e_priv *priv)
{
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	struct mlx5_eswitch_rep *rep;

	if (!MLX5_ESWITCH_MANAGER(priv->mdev))
		return false;

	if (!rpriv) /* non vport rep mlx5e instances don't use this field */
		return false;

	rep = rpriv->rep;
	return (rep->vport == MLX5_VPORT_UPLINK);
}

#if defined(HAVE_NDO_HAS_OFFLOAD_STATS_GETS_NET_DEVICE) || defined(HAVE_NDO_HAS_OFFLOAD_STATS_EXTENDED)
bool mlx5e_rep_has_offload_stats(const struct net_device *dev, int attr_id)
{
	switch (attr_id) {
	case IFLA_OFFLOAD_XSTATS_CPU_HIT:
			return true;
	}

	return false;
}
#endif

#if defined(HAVE_NDO_GET_PORT_PARENT_ID) || defined(HAVE_SWITCHDEV_OPS) || defined(HAVE_SWITCHDEV_H_COMPAT)
#ifdef HAVE_DEVLINK_PORT_ATTRS_PCI_PF_SET
void
#else
int
#endif
mlx5e_rep_get_port_parent_id(struct net_device *dev,
			     struct netdev_phys_item_id *ppid)
{
#ifndef HAVE_DEVLINK_PORT_ATTRS_PCI_PF_SET
	struct mlx5_eswitch *esw;
#endif
	struct mlx5e_priv *priv;
	u64 parent_id;

	priv = netdev_priv(dev);
#ifndef HAVE_DEVLINK_PORT_ATTRS_PCI_PF_SET
	esw = priv->mdev->priv.eswitch;

	if (!esw || esw->mode == MLX5_ESWITCH_NONE)
		return -EOPNOTSUPP;
#endif

	parent_id = mlx5_query_nic_system_image_guid(priv->mdev);
	ppid->id_len = sizeof(parent_id);
	memcpy(ppid->id, &parent_id, sizeof(parent_id));
#ifndef HAVE_DEVLINK_PORT_ATTRS_PCI_PF_SET
	return 0;
#endif
}
#endif

#ifdef HAVE_NDO_GET_PORT_PARENT_ID
static int mlx5e_rep_sf_port_parent_id(struct net_device *dev,
				       struct netdev_phys_item_id *ppid)
{
	mlx5e_rep_get_port_parent_id(dev, ppid);
	return 0;
}
#endif

#if defined(HAVE_NDO_GET_PHYS_PORT_NAME) || defined(HAVE_NDO_GET_PHYS_PORT_NAME_EXTENDED)
static int mlx5e_rep_sf_get_phys_port_name(struct net_device *dev,
					   char *buf, size_t len)
{
#ifdef CONFIG_MLXDEVM
	struct mlx5e_priv *priv = netdev_priv(dev);
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	struct mlx5_eswitch_rep *rep = rpriv->rep;
	struct mlx5_eswitch *esw;
	unsigned int fn;
	u32 controller;
	int ret;

	fn = mlx5_get_dev_index(priv->mdev);
	esw = priv->mdev->priv.eswitch;

	controller = mlx5_devm_sf_vport_to_controller(priv->mdev, rep->vport);
	if (controller)
		ret = snprintf(buf, len, "c%dpf%dsf%d",
			       controller, fn,
			       mlx5_devm_sf_vport_to_sfnum(priv->mdev, rep->vport));
	else
		ret = snprintf(buf, len, "pf%dsf%d", fn,
			       mlx5_devm_sf_vport_to_sfnum(priv->mdev, rep->vport));
	if (ret >= len)
		return -EOPNOTSUPP;
#endif
	return 0;
}
#endif

#if defined(HAVE_NDO_GET_OFFLOAD_STATS) || defined(HAVE_NDO_GET_OFFLOAD_STATS_EXTENDED)
static int
mlx5e_get_sw_stats64(const struct net_device *dev,
		     struct rtnl_link_stats64 *stats)
{
	struct mlx5e_priv *priv = netdev_priv(dev);

	mlx5e_fold_sw_stats64(priv, stats);
	return 0;
}

int mlx5e_rep_get_offload_stats(int attr_id, const struct net_device *dev,
				void *sp)
{
	switch (attr_id) {
	case IFLA_OFFLOAD_XSTATS_CPU_HIT:
		return mlx5e_get_sw_stats64(dev, sp);
	}

	return -EINVAL;
}
#endif

static
#ifdef HAVE_NDO_GET_STATS64_RET_VOID
void mlx5e_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
#elif defined(HAVE_NDO_GET_STATS64)
struct rtnl_link_stats64 * mlx5e_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
#else
struct net_device_stats * mlx5e_rep_get_stats(struct net_device *dev)
#endif
{
	struct mlx5e_priv *priv = netdev_priv(dev);
#if !defined(HAVE_NDO_GET_STATS64) && !defined(HAVE_NDO_GET_STATS64_RET_VOID)
	struct net_device_stats *stats = &priv->netdev_stats;
#endif

	/* update HW stats in background for next time */
	mlx5e_queue_update_stats(priv);
	memcpy(stats, &priv->stats.vf_vport, sizeof(*stats));
#ifndef HAVE_NDO_GET_STATS64_RET_VOID
	return stats;
#endif
}

static int mlx5e_rep_change_mtu(struct net_device *netdev, int new_mtu)
{
	return mlx5e_change_mtu(netdev, new_mtu, NULL);
}

#ifdef HAVE_NDO_GET_DEVLINK_PORT
static struct devlink_port *mlx5e_rep_get_devlink_port(struct net_device *netdev)
{
	struct mlx5e_priv *priv = netdev_priv(netdev);
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	struct mlx5_core_dev *dev = priv->mdev;

	return mlx5_esw_offloads_devlink_port(dev->priv.eswitch, rpriv->rep->vport);
}
#endif

static int mlx5e_rep_change_carrier(struct net_device *dev, bool new_carrier)
{
	struct mlx5e_priv *priv = netdev_priv(dev);
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	struct mlx5_eswitch_rep *rep = rpriv->rep;
	int err;

	if (new_carrier) {
		err = mlx5_modify_vport_admin_state(priv->mdev, MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
						    rep->vport, 1, MLX5_VPORT_ADMIN_STATE_UP);
		if (err)
			return err;
		netif_carrier_on(dev);
	} else {
		err = mlx5_modify_vport_admin_state(priv->mdev, MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
						    rep->vport, 1, MLX5_VPORT_ADMIN_STATE_DOWN);
		if (err)
			return err;
		netif_carrier_off(dev);
	}
	return 0;
}

static const struct net_device_ops mlx5e_netdev_ops_rep = {
#ifdef HAVE_NET_DEVICE_OPS_EXTENDED
	.ndo_size = sizeof(struct net_device_ops),
#endif
	.ndo_open                = mlx5e_rep_open,
	.ndo_stop                = mlx5e_rep_close,
	.ndo_start_xmit          = mlx5e_xmit,
#ifdef CONFIG_MLX5_CLS_ACT
#if defined(HAVE_TC_FLOWER_OFFLOAD) || defined(HAVE_FLOW_CLS_OFFLOAD)
#ifdef HAVE_NDO_SETUP_TC_RH_EXTENDED
        .extended.ndo_setup_tc_rh = mlx5e_rep_setup_tc,
#else
        .ndo_setup_tc            = mlx5e_rep_setup_tc,
#endif
#endif
#endif	/* CONFIG_MLX5_CLS_ACT */
#ifdef HAVE_NDO_GET_DEVLINK_PORT
	.ndo_get_devlink_port    = mlx5e_rep_get_devlink_port,
#else
#ifdef HAVE_NDO_GET_PHYS_PORT_NAME
        .ndo_get_phys_port_name  = mlx5e_rep_get_phys_port_name,
#elif defined(HAVE_NDO_GET_PHYS_PORT_NAME_EXTENDED)
        .extended.ndo_get_phys_port_name = mlx5e_rep_get_phys_port_name,
#endif
#ifdef HAVE_NDO_GET_PORT_PARENT_ID
	.ndo_get_port_parent_id  = mlx5e_rep_get_port_parent_id,
#endif
#endif
#if defined(HAVE_NDO_GET_STATS64) || defined(HAVE_NDO_GET_STATS64_RET_VOID)
	.ndo_get_stats64         = mlx5e_rep_get_stats,
#else
	.ndo_get_stats           = mlx5e_rep_get_stats,
#endif
#ifdef HAVE_NDO_HAS_OFFLOAD_STATS_GETS_NET_DEVICE
	.ndo_has_offload_stats	 = mlx5e_rep_has_offload_stats,
#elif defined(HAVE_NDO_HAS_OFFLOAD_STATS_EXTENDED)
	.extended.ndo_has_offload_stats   = mlx5e_rep_has_offload_stats,
#endif
#ifdef HAVE_NDO_GET_OFFLOAD_STATS
	.ndo_get_offload_stats	 = mlx5e_rep_get_offload_stats,
#elif defined(HAVE_NDO_GET_OFFLOAD_STATS_EXTENDED)
	.extended.ndo_get_offload_stats   = mlx5e_rep_get_offload_stats,
#endif
#ifdef HAVE_NDO_CHANGE_MTU_EXTENDED
        .extended.ndo_change_mtu = mlx5e_rep_change_mtu,
#else
        .ndo_change_mtu          = mlx5e_rep_change_mtu,
#endif
	.ndo_change_carrier      = mlx5e_rep_change_carrier,
};

static const struct net_device_ops mlx5e_netdev_ops_rep_sf = {
#ifdef HAVE_NET_DEVICE_OPS_EXTENDED
	.ndo_size = sizeof(struct net_device_ops),
#endif
	.ndo_open                = mlx5e_rep_open,
	.ndo_stop                = mlx5e_rep_close,
	.ndo_start_xmit          = mlx5e_xmit,
#ifdef CONFIG_MLX5_CLS_ACT
#if defined(HAVE_TC_FLOWER_OFFLOAD) || defined(HAVE_FLOW_CLS_OFFLOAD)
#ifdef HAVE_NDO_SETUP_TC_RH_EXTENDED
	.extended.ndo_setup_tc_rh = mlx5e_rep_setup_tc,
#else
	.ndo_setup_tc            = mlx5e_rep_setup_tc,
#endif
#endif
#endif
#ifdef HAVE_NDO_GET_PHYS_PORT_NAME
	.ndo_get_phys_port_name  = mlx5e_rep_sf_get_phys_port_name,
#elif defined(HAVE_NDO_GET_PHYS_PORT_NAME_EXTENDED)
	.extended.ndo_get_phys_port_name = mlx5e_rep_sf_get_phys_port_name,
#endif
#ifdef HAVE_NDO_GET_PORT_PARENT_ID
	.ndo_get_port_parent_id  = mlx5e_rep_sf_port_parent_id,
#endif
#if defined(HAVE_NDO_GET_STATS64) || defined(HAVE_NDO_GET_STATS64_RET_VOID)
	.ndo_get_stats64         = mlx5e_rep_get_stats,
#else
	.ndo_get_stats           = mlx5e_rep_get_stats,
#endif
#ifdef HAVE_NDO_HAS_OFFLOAD_STATS_GETS_NET_DEVICE
	.ndo_has_offload_stats   = mlx5e_rep_has_offload_stats,
#elif defined(HAVE_NDO_HAS_OFFLOAD_STATS_EXTENDED)
	.extended.ndo_has_offload_stats   = mlx5e_rep_has_offload_stats,
#endif
#ifdef HAVE_NDO_GET_OFFLOAD_STATS
	.ndo_get_offload_stats   = mlx5e_rep_get_offload_stats,
#elif defined(HAVE_NDO_GET_OFFLOAD_STATS_EXTENDED)
	.extended.ndo_get_offload_stats   = mlx5e_rep_get_offload_stats,
#endif
#ifdef HAVE_NDO_CHANGE_MTU_EXTENDED
	.extended.ndo_change_mtu = mlx5e_rep_change_mtu,
#else
	.ndo_change_mtu          = mlx5e_rep_change_mtu,
#endif
};

bool mlx5e_eswitch_uplink_rep(const struct net_device *netdev)
{
	return netdev->netdev_ops == &mlx5e_netdev_ops &&
	       mlx5e_is_uplink_rep(netdev_priv(netdev));
}

bool mlx5e_eswitch_vf_rep(const struct net_device *netdev)
{
	return netdev->netdev_ops == &mlx5e_netdev_ops_rep ||
		netdev->netdev_ops == &mlx5e_netdev_ops_rep_sf;
}

static int mlx5e_ul_rep_max_nch(struct mlx5_core_dev *mdev)
{
	return mlx5e_get_max_num_channels(mdev);
}

static int mlx5e_rep_max_nch(struct mlx5_core_dev *mdev)
{
	int max_nch;

	max_nch = mlx5e_get_max_num_channels(mdev);
	/* We limit total number of max channels when SFs are enabled.
	 * MAX channels are used for channel resources such as stats, TIRs and more.
	 * These resources to be used carefully to scale.
	 * Until the code is restructured to always do the allocations based on
	 * current num channels, restrict the maximum channels for scale.
	 */
	if (mlx5_sf_max_functions(mdev))
		return min_t(int, max_nch, MLX5E_REP_SF_ENABLED_MAX_NUM_CHANNELS);
	else
		return max_nch;
}

static void mlx5e_build_rep_params(struct net_device *netdev)
{
	struct mlx5e_priv *priv = netdev_priv(netdev);
	struct mlx5_core_dev *mdev = priv->mdev;
	struct mlx5e_params *params;

	u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
					 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
					 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;

	priv->max_nch = priv->profile->max_nch(mdev);
	params = &priv->channels.params;

	params->num_channels = MLX5E_REP_PARAMS_DEF_NUM_CHANNELS;

	params->hard_mtu    = MLX5E_ETH_HARD_MTU;
	params->sw_mtu      = netdev->mtu;

	/* SQ */
	params->log_sq_size = MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;

	/* RQ */
	mlx5e_build_rq_params(mdev, params);

	/* CQ moderation params */
	params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
	mlx5e_set_rx_cq_mode_params(params, cq_period_mode);

	params->num_tc                = 1;
	params->tunneled_offload_en = false;

	mlx5_query_min_inline(mdev, &params->tx_min_inline_mode);

	MLX5E_SET_PFLAG(params, MLX5E_PFLAG_PER_CH_STATS, true);

	/* RSS */
	mlx5e_build_rss_params(&priv->rss_params, params->num_channels);
}

#ifdef HAVE_SWITCHDEV_OPS
static const struct switchdev_ops mlx5e_rep_switchdev_ops = {
	.switchdev_port_attr_get    = mlx5e_attr_get,
};
#endif

static void mlx5e_build_rep_netdev(struct net_device *netdev,
				   struct mlx5_core_dev *mdev,
				   struct mlx5_eswitch_rep *rep)
{
	SET_NETDEV_DEV(netdev, mdev->device);

	if (mlx5_esw_is_sf_vport(mdev->priv.eswitch, rep->vport))
		netdev->netdev_ops = &mlx5e_netdev_ops_rep_sf;
	else
		netdev->netdev_ops = &mlx5e_netdev_ops_rep;

	eth_hw_addr_random(netdev);
	netdev->ethtool_ops = &mlx5e_rep_ethtool_ops;

#ifdef HAVE_SWITCHDEV_OPS
	netdev->switchdev_ops = &mlx5e_rep_switchdev_ops;
#endif

	netdev->watchdog_timeo    = 15 * HZ;

#if IS_ENABLED(CONFIG_MLX5_CLS_ACT)
	netdev->hw_features    |= NETIF_F_HW_TC;
#endif
	netdev->hw_features    |= NETIF_F_SG;
	netdev->hw_features    |= NETIF_F_IP_CSUM;
	netdev->hw_features    |= NETIF_F_IPV6_CSUM;
	netdev->hw_features    |= NETIF_F_GRO;
	netdev->hw_features    |= NETIF_F_TSO;
	netdev->hw_features    |= NETIF_F_TSO6;
	netdev->hw_features    |= NETIF_F_RXCSUM;

	netdev->features |= netdev->hw_features;
	netdev->features |= NETIF_F_NETNS_LOCAL;
}

static int mlx5e_init_rep(struct mlx5_core_dev *mdev,
			  struct net_device *netdev)
{
	struct mlx5e_priv *priv = netdev_priv(netdev);
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	int err;

	if (rpriv->rep->vport == MLX5_VPORT_UPLINK) {
		err = mlx5e_ipsec_init(priv);
		if (err)
			mlx5_core_err(mdev, "Uplink rep IPsec initialization failed, %d\n",
				      err);
	}

	mlx5e_build_rep_params(netdev);
	mlx5e_build_txq_maps(priv);

	mlx5e_timestamp_init(priv);

	return 0;
}

static int mlx5e_init_ul_rep(struct mlx5_core_dev *mdev,
			     struct net_device *netdev)
{
	struct mlx5e_priv *priv = netdev_priv(netdev);

#ifdef HAVE_UDP_TUNNEL_NIC_INFO
	mlx5e_vxlan_set_netdev_info(priv);
#endif
	mutex_init(&priv->aso_lock);
	return mlx5e_init_rep(mdev, netdev);
}

static void mlx5e_cleanup_rep(struct mlx5e_priv *priv)
{
	struct mlx5e_rep_priv *rpriv = priv->ppriv;

	if (!rpriv)
		return;

	if (rpriv->rep->vport == MLX5_VPORT_UPLINK)
		mlx5e_ipsec_cleanup(priv);
}

static int mlx5e_create_rep_ttc_table(struct mlx5e_priv *priv)
{
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	struct mlx5_eswitch_rep *rep = rpriv->rep;
	struct ttc_params ttc_params = {};
	int err;

	priv->fs.ns = mlx5_get_flow_namespace(priv->mdev,
					      MLX5_FLOW_NAMESPACE_KERNEL);

	/* The inner_ttc in the ttc params is intentionally not set */
	mlx5e_set_ttc_params(priv, &ttc_params, false);

	if (rep->vport != MLX5_VPORT_UPLINK)
		/* To give uplik rep TTC a lower level for chaining from root ft */
		ttc_params.ft_attr.level = MLX5E_TTC_FT_LEVEL + 1;

	priv->fs.ttc = mlx5_create_ttc_table(priv->mdev, &ttc_params);
	if (IS_ERR(priv->fs.ttc)) {
		err = PTR_ERR(priv->fs.ttc);
		netdev_err(priv->netdev, "Failed to create rep ttc table, err=%d\n",
			   err);
		return err;
	}
	return 0;
}

static int mlx5e_create_rep_root_ft(struct mlx5e_priv *priv)
{
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	struct mlx5_eswitch_rep *rep = rpriv->rep;
	struct mlx5_flow_table_attr ft_attr = {};
	struct mlx5_flow_namespace *ns;
	int err = 0;

	if (rep->vport != MLX5_VPORT_UPLINK) {
		/* non uplik reps will skip any bypass tables and go directly to
		 * their own ttc
		 */
		rpriv->root_ft = mlx5_get_ttc_flow_table(priv->fs.ttc);
		return 0;
	}

	/* uplink root ft will be used to auto chain, to ethtool or ttc tables */
	ns = mlx5_get_flow_namespace(priv->mdev, MLX5_FLOW_NAMESPACE_OFFLOADS);
	if (!ns) {
		netdev_err(priv->netdev, "Failed to get reps offloads namespace\n");
		return -EOPNOTSUPP;
	}

	ft_attr.max_fte = 0; /* Empty table, miss rule will always point to next table */
	ft_attr.prio = 1;
	ft_attr.level = 1;

	rpriv->root_ft = mlx5_create_flow_table(ns, &ft_attr);
	if (IS_ERR(rpriv->root_ft)) {
		err = PTR_ERR(rpriv->root_ft);
		rpriv->root_ft = NULL;
	}

	return err;
}

static void mlx5e_destroy_rep_root_ft(struct mlx5e_priv *priv)
{
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	struct mlx5_eswitch_rep *rep = rpriv->rep;

	if (rep->vport != MLX5_VPORT_UPLINK)
		return;
	mlx5_destroy_flow_table(rpriv->root_ft);
}

static struct mlx5_flow_table *mlx5e_get_root_ft(struct mlx5e_priv *priv,
						 struct mlx5_eswitch *esw)
{
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	struct mlx5e_rep_priv *uplink_rpriv;

	if (priv->shared_rq) {
		uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
		return uplink_rpriv->root_ft;
	}

	return rpriv->root_ft;
}

static int mlx5e_create_rep_vport_rx_rule(struct mlx5e_priv *priv)
{
	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	struct mlx5_eswitch_rep *rep = rpriv->rep;
	struct mlx5_flow_handle *flow_rule;
	struct mlx5_flow_destination dest;

	dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
	dest.ft = mlx5e_get_root_ft(priv, esw);

	flow_rule = mlx5_eswitch_create_vport_rx_rule(esw, rep->vport, &dest);
	if (IS_ERR(flow_rule))
		return PTR_ERR(flow_rule);
	rpriv->vport_rx_rule = flow_rule;
	return 0;
}

static void rep_vport_rx_rule_destroy(struct mlx5e_priv *priv)
{
	struct mlx5e_rep_priv *rpriv = priv->ppriv;

	if (!rpriv->vport_rx_rule)
		return;

	mlx5_del_flow_rules(rpriv->vport_rx_rule);
	rpriv->vport_rx_rule = NULL;
}

int mlx5e_rep_bond_update(struct mlx5e_priv *priv, bool cleanup)
{
	rep_vport_rx_rule_destroy(priv);

	return cleanup ? 0 : mlx5e_create_rep_vport_rx_rule(priv);
}

static int mlx5e_init_rep_shared_rq(struct mlx5e_priv *priv)
{
	int err;

	err = mlx5e_create_rep_vport_rx_rule(priv);
	if (err)
		mlx5_core_warn(priv->mdev, "create_rep_vport_rx_rule failed with err %d\n", err);

	return err;
}

static int mlx5e_init_rep_dedicated_rq(struct mlx5e_priv *priv)
{
	struct mlx5_core_dev *mdev = priv->mdev;
	u16 max_nch = priv->max_nch;
	int err;

	mlx5e_init_l2_addr(priv);

	err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
	if (err) {
		mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
		return err;
	}

	err = mlx5e_create_indirect_rqt(priv);
	if (err)
		goto err_close_drop_rq;

	err = mlx5e_create_direct_rqts(priv, priv->direct_tir, max_nch);
	if (err)
		goto err_destroy_indirect_rqts;

	err = mlx5e_create_indirect_tirs(priv, false);
	if (err)
		goto err_destroy_direct_rqts;

	err = mlx5e_create_direct_tirs(priv, priv->direct_tir, max_nch);
	if (err)
		goto err_destroy_indirect_tirs;

	err = mlx5e_create_rep_ttc_table(priv);
	if (err)
		goto err_destroy_direct_tirs;

	err = mlx5e_esw_offloads_pet_setup(priv->mdev->priv.eswitch,
					   mlx5_get_ttc_flow_table(priv->fs.ttc));
	if (err)
		goto err_destroy_ttc_table;

	err = mlx5e_create_rep_root_ft(priv);
	if (err)
		goto err_cleanup_pet_setup;

	err = mlx5e_create_rep_vport_rx_rule(priv);
	if (err)
		goto err_destroy_root_ft;

	mlx5e_ethtool_init_steering(priv);

	return 0;

err_destroy_root_ft:
	mlx5e_destroy_rep_root_ft(priv);
err_cleanup_pet_setup:
	mlx5e_esw_offloads_pet_cleanup(priv->mdev->priv.eswitch);
err_destroy_ttc_table:
	mlx5_destroy_ttc_table(priv->fs.ttc);
err_destroy_direct_tirs:
	mlx5e_destroy_direct_tirs(priv, priv->direct_tir, max_nch);
err_destroy_indirect_tirs:
	mlx5e_destroy_indirect_tirs(priv);
err_destroy_direct_rqts:
	mlx5e_destroy_direct_rqts(priv, priv->direct_tir, max_nch);
err_destroy_indirect_rqts:
	mlx5e_destroy_rqt(priv, &priv->indir_rqt);
err_close_drop_rq:
	mlx5e_close_drop_rq(&priv->drop_rq);
	return err;
}

static int mlx5e_init_rep_rx(struct mlx5e_priv *priv)
{
	if (mlx5e_is_rep_shared_rq(priv))
		return mlx5e_init_rep_shared_rq(priv);
	else
		return mlx5e_init_rep_dedicated_rq(priv);
}

static void mlx5e_cleanup_rep_shared_rq(struct mlx5e_priv *priv)
{
	rep_vport_rx_rule_destroy(priv);
}

static void mlx5e_cleanup_rep_dedicated_rq(struct mlx5e_priv *priv)
{
	u16 max_nch = priv->max_nch;

	mlx5e_ethtool_cleanup_steering(priv);
	rep_vport_rx_rule_destroy(priv);
	mlx5e_destroy_rep_root_ft(priv);
	mlx5e_esw_offloads_pet_cleanup(priv->mdev->priv.eswitch);
	mlx5_destroy_ttc_table(priv->fs.ttc);
	mlx5e_destroy_direct_tirs(priv, priv->direct_tir, max_nch);
	mlx5e_destroy_indirect_tirs(priv);
	mlx5e_destroy_direct_rqts(priv, priv->direct_tir, max_nch);
	mlx5e_destroy_rqt(priv, &priv->indir_rqt);
	mlx5e_close_drop_rq(&priv->drop_rq);
}

static void mlx5e_cleanup_rep_rx(struct mlx5e_priv *priv)
{
	if (mlx5e_is_rep_shared_rq(priv))
		return mlx5e_cleanup_rep_shared_rq(priv);
	else
		return mlx5e_cleanup_rep_dedicated_rq(priv);
}

static int mlx5e_init_ul_rep_rx(struct mlx5e_priv *priv)
{
	int err;

	mlx5e_create_q_counters(priv);
	err = mlx5e_init_rep_rx(priv);
#if IS_ENABLED(CONFIG_MLX5_CLS_ACT)
	if (!err)
		mlx5_init_rep_int_port_rx(priv->mdev->priv.eswitch);
#endif
	return err;
}

static void mlx5e_cleanup_ul_rep_rx(struct mlx5e_priv *priv)
{
#if IS_ENABLED(CONFIG_MLX5_CLS_ACT)
	mlx5_cleanup_rep_int_port_rx(priv->mdev->priv.eswitch);
#endif
	mlx5e_cleanup_rep_rx(priv);
	mlx5e_destroy_q_counters(priv);
}

static int mlx5e_init_uplink_rep_tx(struct mlx5e_rep_priv *rpriv)
{
	struct mlx5_rep_uplink_priv *uplink_priv;
	struct net_device *netdev;
	struct mlx5e_priv *priv;
	int err;

	netdev = rpriv->netdev;
	priv = netdev_priv(netdev);
	uplink_priv = &rpriv->uplink_priv;

	err = mlx5e_rep_tc_init(rpriv);
	if (err)
		return err;

	mlx5_init_port_tun_entropy(&uplink_priv->tun_entropy, priv->mdev);

	mlx5e_rep_bond_init(rpriv);
#if defined( HAVE_TC_BLOCK_OFFLOAD) || defined(HAVE_FLOW_BLOCK_OFFLOAD)
	err = mlx5e_rep_tc_netdevice_event_register(rpriv);
	if (err) {
		mlx5_core_err(priv->mdev, "Failed to register netdev notifier, err: %d\n",
			      err);
		goto err_event_reg;
	}

	return 0;

err_event_reg:
	mlx5e_rep_bond_cleanup(rpriv);
	mlx5e_rep_tc_cleanup(rpriv);
#endif
	return err;
}

static int mlx5e_init_rep_tx(struct mlx5e_priv *priv)
{
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	int err;

	err = mlx5e_create_tises(priv);
	if (err) {
		mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
		return err;
	}

	err = mlx5e_tc_ht_init(&rpriv->tc_ht);
	if (err)
		goto err_ht_init;

	if (rpriv->rep->vport == MLX5_VPORT_UPLINK) {
		err = mlx5e_init_uplink_rep_tx(rpriv);
		if (err)
			goto err_init_tx;
	}

	return 0;

err_init_tx:
	mlx5e_tc_ht_cleanup(&rpriv->tc_ht);
err_ht_init:
	mlx5e_destroy_tises(priv);
	return err;
}

static void mlx5e_cleanup_uplink_rep_tx(struct mlx5e_rep_priv *rpriv)
{
#if defined( HAVE_TC_BLOCK_OFFLOAD) || defined(HAVE_FLOW_BLOCK_OFFLOAD)
	mlx5e_rep_tc_netdevice_event_unregister(rpriv);
#ifndef HAVE_FLOW_INDR_DEV_REGISTER
	mlx5e_rep_indr_clean_block_privs(rpriv);
#endif
#endif
	mlx5e_rep_bond_cleanup(rpriv);
	mlx5e_rep_tc_cleanup(rpriv);
}

static void mlx5e_cleanup_rep_tx(struct mlx5e_priv *priv)
{
	struct mlx5e_rep_priv *rpriv = priv->ppriv;

	mlx5e_destroy_tises(priv);

	mlx5e_tc_ht_cleanup(&rpriv->tc_ht);

	if (rpriv->rep->vport == MLX5_VPORT_UPLINK)
		mlx5e_cleanup_uplink_rep_tx(rpriv);
}

static void mlx5e_rep_enable(struct mlx5e_priv *priv)
{
	struct mlx5e_rep_priv *rpriv = priv->ppriv;

	mlx5e_set_netdev_mtu_boundaries(priv);
	mlx5e_rep_neigh_init(rpriv);
}

static void mlx5e_rep_disable(struct mlx5e_priv *priv)
{
	struct mlx5e_rep_priv *rpriv = priv->ppriv;

	mlx5e_rep_neigh_cleanup(rpriv);
}

static int mlx5e_update_rep_rx(struct mlx5e_priv *priv)
{
	return 0;
}

static int mlx5e_obj_change_event(struct mlx5e_priv *priv, struct mlx5_eqe *eqe)
{
	struct mlx5_eqe_obj_change *obj_change = &eqe->data.obj_change;
	u16 obj_type = be16_to_cpu(obj_change->obj_type);
	u32 obj_id = be32_to_cpu(obj_change->obj_id);

	if (obj_type == MLX5_GENERAL_OBJECT_TYPES_IPSEC)
		return mlx5e_ipsec_async_event(priv, obj_id);

	return NOTIFY_DONE;
}

static int mlx5e_port_change_event(struct mlx5e_priv *priv, struct mlx5_eqe *eqe)
{
	switch (eqe->sub_type) {
	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
		queue_work(priv->wq, &priv->update_carrier_work);
		break;
	default:
		return NOTIFY_DONE;
	}

	return NOTIFY_OK;
}

static int uplink_rep_async_event(struct notifier_block *nb, unsigned long event, void *data)
{
	struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, events_nb);
	struct mlx5_eqe *eqe = data;

	switch (event) {
	case MLX5_EVENT_TYPE_OBJECT_CHANGE_EVENT:
		return mlx5e_obj_change_event(priv, eqe);
	case MLX5_EVENT_TYPE_PORT_CHANGE:
		return mlx5e_port_change_event(priv, eqe);
	case MLX5_DEV_EVENT_PORT_AFFINITY:
		return mlx5e_rep_tc_event_port_affinity(priv);
	}

	return NOTIFY_DONE;
}

static void mlx5e_uplink_rep_enable(struct mlx5e_priv *priv)
{
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	struct net_device *netdev = priv->netdev;
	struct mlx5_core_dev *mdev = priv->mdev;
#if defined(HAVE_NET_DEVICE_MIN_MAX_MTU) || defined(HAVE_NET_DEVICE_MIN_MAX_MTU_EXTENDED)
	u16 max_mtu;
#endif

#ifdef HAVE_NET_DEVICE_MIN_MAX_MTU
	netdev->min_mtu = ETH_MIN_MTU;
	mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
	netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
#elif defined(HAVE_NET_DEVICE_MIN_MAX_MTU_EXTENDED)
	netdev->extended->min_mtu = ETH_MIN_MTU;
	mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
	netdev->extended->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
#endif
	mlx5e_set_dev_port_mtu(priv);

	mlx5e_rep_tc_enable(priv);

	if (MLX5_CAP_GEN(mdev, uplink_follow))
		mlx5_modify_vport_admin_state(mdev, MLX5_VPORT_STATE_OP_MOD_UPLINK,
					      0, 0, MLX5_VPORT_ADMIN_STATE_AUTO);
	mlx5_lag_add_netdev(mdev, netdev);
	priv->events_nb.notifier_call = uplink_rep_async_event;
	mlx5_notifier_register(mdev, &priv->events_nb);
	mlx5e_dcbnl_initialize(priv);
	mlx5e_dcbnl_init_app(priv);
	mlx5e_rep_neigh_init(rpriv);
	mlx5e_rep_bridge_init(priv);

	netdev->wanted_features |= NETIF_F_HW_TC;

	rtnl_lock();
	if (netif_running(netdev))
		mlx5e_open(netdev);
#ifdef HAVE_UDP_TUNNEL_NIC_INFO
	udp_tunnel_nic_reset_ntf(priv->netdev);
#endif
	netif_device_attach(netdev);
	rtnl_unlock();
}

static void mlx5e_uplink_rep_disable(struct mlx5e_priv *priv)
{
	struct mlx5e_rep_priv *rpriv = priv->ppriv;
	struct mlx5_core_dev *mdev = priv->mdev;

	rtnl_lock();
	if (netif_running(priv->netdev))
		mlx5e_close(priv->netdev);
	netif_device_detach(priv->netdev);
	rtnl_unlock();

	mlx5e_rep_bridge_cleanup(priv);
	mlx5e_rep_neigh_cleanup(rpriv);
	mlx5e_dcbnl_delete_app(priv);
	mlx5_notifier_unregister(mdev, &priv->events_nb);
	mlx5e_rep_tc_disable(priv);
	mlx5_lag_remove_netdev(mdev, priv->netdev);
#ifdef HAVE_UDP_TUNNEL_NIC_INFO
	mlx5_vxlan_reset_to_default(mdev->vxlan);
#endif
}

static MLX5E_DEFINE_STATS_GRP(sw_rep, 0);
static MLX5E_DEFINE_STATS_GRP(vport_rep, MLX5E_NDO_UPDATE_STATS);

/* The stats groups order is opposite to the update_stats() order calls */
static mlx5e_stats_grp_t mlx5e_rep_stats_grps[] = {
	&MLX5E_STATS_GRP(sw_rep),
	&MLX5E_STATS_GRP(vport_rep),
};

static unsigned int mlx5e_rep_stats_grps_num(struct mlx5e_priv *priv)
{
	return ARRAY_SIZE(mlx5e_rep_stats_grps);
}

/* The stats groups order is opposite to the update_stats() order calls */
static mlx5e_stats_grp_t mlx5e_ul_rep_stats_grps[] = {
	&MLX5E_STATS_GRP(sw),
	&MLX5E_STATS_GRP(qcnt),
	&MLX5E_STATS_GRP(vnic_env),
	&MLX5E_STATS_GRP(vport),
	&MLX5E_STATS_GRP(802_3),
	&MLX5E_STATS_GRP(2863),
	&MLX5E_STATS_GRP(2819),
	&MLX5E_STATS_GRP(phy),
	&MLX5E_STATS_GRP(eth_ext),
	&MLX5E_STATS_GRP(pcie),
	&MLX5E_STATS_GRP(per_prio),
	&MLX5E_STATS_GRP(pme),
	&MLX5E_STATS_GRP(channels),
	&MLX5E_STATS_GRP(per_port_buff_congest),
#ifdef CONFIG_MLX5_EN_IPSEC
	&MLX5E_STATS_GRP(ipsec_sw),
	&MLX5E_STATS_GRP(ipsec_hw),
#endif
	&MLX5E_STATS_GRP(ptp),
};

static unsigned int mlx5e_ul_rep_stats_grps_num(struct mlx5e_priv *priv)
{
	return ARRAY_SIZE(mlx5e_ul_rep_stats_grps);
}

static u32 mlx5e_rep_num_rl_queues(struct mlx5_core_dev *mdev)
{
	return 0;
}

static const struct mlx5e_profile mlx5e_rep_profile = {
	.init			= mlx5e_init_rep,
	.cleanup		= mlx5e_cleanup_rep,
	.init_rx		= mlx5e_init_rep_rx,
	.cleanup_rx		= mlx5e_cleanup_rep_rx,
	.init_tx		= mlx5e_init_rep_tx,
	.cleanup_tx		= mlx5e_cleanup_rep_tx,
	.enable		        = mlx5e_rep_enable,
	.disable	        = mlx5e_rep_disable,
	.update_rx		= mlx5e_update_rep_rx,
	.update_stats           = mlx5e_stats_update_ndo_stats,
	.rx_handlers            = &mlx5e_rx_handlers_rep,
	.max_tc			= 1,
	.max_nch		= mlx5e_rep_max_nch,
	.rq_groups		= MLX5E_NUM_RQ_GROUPS(REGULAR),
	.stats_grps		= mlx5e_rep_stats_grps,
	.stats_grps_num		= mlx5e_rep_stats_grps_num,
	.rx_ptp_support		= false,
	.num_rl_queues          = mlx5e_rep_num_rl_queues,
};

static const struct mlx5e_profile mlx5e_uplink_rep_profile = {
	.init			= mlx5e_init_ul_rep,
	.cleanup		= mlx5e_cleanup_rep,
	.init_rx		= mlx5e_init_ul_rep_rx,
	.cleanup_rx		= mlx5e_cleanup_ul_rep_rx,
	.init_tx		= mlx5e_init_rep_tx,
	.cleanup_tx		= mlx5e_cleanup_rep_tx,
	.enable		        = mlx5e_uplink_rep_enable,
	.disable	        = mlx5e_uplink_rep_disable,
	.update_rx		= mlx5e_update_rep_rx,
	.update_stats           = mlx5e_stats_update_ndo_stats,
	.update_carrier	        = mlx5e_update_carrier,
	.rx_handlers            = &mlx5e_rx_handlers_rep,
	.max_tc			= MLX5E_MAX_NUM_TC,
	.max_nch		= mlx5e_ul_rep_max_nch,
	/* XSK is needed so we can replace profile with NIC netdev */
	.rq_groups		= MLX5E_NUM_RQ_GROUPS(XSK),
	.stats_grps		= mlx5e_ul_rep_stats_grps,
	.stats_grps_num		= mlx5e_ul_rep_stats_grps_num,
	.rx_ptp_support		= false,
};

static int mlx5e_rep_metadata_insert(struct mlx5e_priv *priv, struct mlx5_eswitch_rep *rep)
{
	struct mlx5e_rep_priv *rpriv_uplink;
	struct mlx5_eswitch *esw;
	u32 vport_metadata;
	int err;

	esw = priv->mdev->priv.eswitch;
	if (!mlx5e_esw_offloads_pet_enabled(esw))
		return 0;

	rpriv_uplink = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
	vport_metadata = mlx5_eswitch_get_vport_metadata_for_match(esw, rep->vport);
	err = xa_insert(&rpriv_uplink->vport_rep_map, vport_metadata, rep, GFP_KERNEL);
	if (err) {
		esw_warn(esw->dev, "Error %d inserting metadata for vport %d\n", err, rep->vport);
		goto err;
	}
	return 0;
err:
	return err;
}

static void mlx5e_rep_metadata_remove(struct mlx5e_priv *priv, struct mlx5_eswitch_rep *rep)
{
	struct mlx5e_rep_priv *rpriv_uplink;
	struct mlx5_eswitch *esw;
	u32 vport_metadata;

	esw = priv->mdev->priv.eswitch;
	if (!mlx5e_esw_offloads_pet_enabled(esw))
		return;

	rpriv_uplink = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
	vport_metadata = mlx5_eswitch_get_vport_metadata_for_match(esw, rep->vport);
	xa_erase(&rpriv_uplink->vport_rep_map, vport_metadata);
	synchronize_net();
}

/* e-Switch vport representors */
static int
mlx5e_vport_uplink_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
{
	struct mlx5e_priv *priv = netdev_priv(mlx5_uplink_netdev_get(dev));
	struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
	struct devlink_port *dl_port;
#endif
	int err;

	rpriv->netdev = priv->netdev;

	xa_init(&rpriv->vport_rep_map);
	err = mlx5e_rep_metadata_insert(priv, rep);
	if (err)
		return err;

	mlx5e_ipsec_ul_cleanup(priv);

	err = mlx5e_netdev_change_profile(priv, &mlx5e_uplink_rep_profile,
					  rpriv);
	if (err)
		goto err_metadata_insert;

#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
	dl_port = mlx5_esw_offloads_devlink_port(dev->priv.eswitch, rpriv->rep->vport);
	if (dl_port)
		devlink_port_type_eth_set(dl_port, rpriv->netdev);
#endif

	mlx5_smartnic_sysfs_init(rpriv->netdev);
	mlx5_rep_sysfs_init(rpriv);
	mlx5e_ipsec_build_netdev(priv);

	return 0;
err_metadata_insert:
	xa_destroy(&rpriv->vport_rep_map);

	return err;
}

static void
mlx5e_vport_uplink_rep_unload(struct mlx5e_rep_priv *rpriv)
{
	struct net_device *netdev = rpriv->netdev;
#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
	struct devlink_port *dl_port;
#endif
	struct mlx5_core_dev *dev;
	struct mlx5e_priv *priv;

	priv = netdev_priv(netdev);
	dev = priv->mdev;

	mlx5e_rep_metadata_remove(priv, rpriv->rep);
	xa_destroy(&rpriv->vport_rep_map);

	mlx5e_ipsec_ul_cleanup(priv);

#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
	dl_port = mlx5_esw_offloads_devlink_port(dev->priv.eswitch, rpriv->rep->vport);
	if (dl_port)
		devlink_port_type_clear(dl_port);
#endif

	mlx5_smartnic_sysfs_cleanup(netdev);
	if (test_bit(MLX5_INTERFACE_STATE_TEARDOWN, &dev->intf_state)) {
		mlx5e_detach_netdev(priv);
		priv->profile->cleanup(priv);
		mlx5e_destroy_mdev_resources(priv->mdev);
	} else {
		mlx5e_netdev_attach_nic_profile(priv);
	}
}

static int
mlx5e_vport_vf_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
{
	struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
	const struct mlx5e_profile *profile;
#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
	struct devlink_port *dl_port;
#endif
	struct net_device *netdev;
	struct mlx5e_priv *priv;
	int err;

	profile = &mlx5e_rep_profile;
	netdev = mlx5e_create_netdev(dev, profile);
	if (!netdev) {
		mlx5_core_warn(dev,
			       "Failed to create representor netdev for vport %d\n",
			       rep->vport);
		return -EINVAL;
	}

	mlx5e_build_rep_netdev(netdev, dev, rep);
	rpriv->netdev = netdev;

	priv = netdev_priv(netdev);
	priv->profile = profile;
	priv->ppriv = rpriv;
	priv->shared_rq = mlx5e_is_rep_shared_rq(priv);
	err = mlx5e_rep_metadata_insert(netdev_priv(netdev), rep);
	if (err)
		return err;

	err = profile->init(dev, netdev);
	if (err) {
		netdev_warn(netdev, "rep profile init failed, %d\n", err);
		goto err_destroy_netdev;
	}

	err = mlx5e_attach_netdev(netdev_priv(netdev));
	if (err) {
		netdev_warn(netdev,
			    "Failed to attach representor netdev for vport %d\n",
			    rep->vport);
		goto err_cleanup_profile;
	}

	err = mlx5e_vport_rep_load_compat(priv);
	if (err)
		goto err_compat_cleanup;

	err = register_netdev(netdev);
	if (err) {
		netdev_warn(netdev,
			    "Failed to register representor netdev for vport %d\n",
			    rep->vport);
		goto err_detach_netdev;
	}

	if (!mlx5_esw_is_sf_vport(dev->priv.eswitch, rep->vport))
		mlx5_rep_sysfs_init(rpriv);

#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
	dl_port = mlx5_esw_offloads_devlink_port(dev->priv.eswitch, rpriv->rep->vport);
	if (dl_port)
		devlink_port_type_eth_set(dl_port, netdev);
#endif
	mlx5_devm_sf_port_type_eth_set(dev, rpriv->rep->vport, netdev);
	return 0;

err_compat_cleanup:
	mlx5e_vport_rep_unload_compat(priv);

err_detach_netdev:
	mlx5e_detach_netdev(netdev_priv(netdev));

err_cleanup_profile:
	priv->profile->cleanup(priv);

err_destroy_netdev:
	mlx5e_destroy_netdev(netdev_priv(netdev));
	return err;
}

static int
mlx5e_vport_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
{
	struct mlx5e_rep_priv *rpriv;
	int err;

	rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
	if (!rpriv)
		return -ENOMEM;

	/* rpriv->rep to be looked up when profile->init() is called */
	rpriv->rep = rep;
	rep->rep_data[REP_ETH].priv = rpriv;
	INIT_LIST_HEAD(&rpriv->vport_sqs_list);

	if (rep->vport == MLX5_VPORT_UPLINK)
		err = mlx5e_vport_uplink_rep_load(dev, rep);
	else
		err = mlx5e_vport_vf_rep_load(dev, rep);

	if (err)
		kfree(rpriv);

	return err;
}

static void
mlx5e_vport_rep_unload(struct mlx5_eswitch_rep *rep)
{
	struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
	struct net_device *netdev = rpriv->netdev;
	struct mlx5e_priv *priv = netdev_priv(netdev);
	struct mlx5_core_dev *dev = priv->mdev;
#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
	struct devlink_port *dl_port;
#endif
	void *ppriv = priv->ppriv;

	mlx5_rep_destroy_miss_meter(dev, rpriv);
	if (!mlx5_esw_is_sf_vport(dev->priv.eswitch, rep->vport))
		mlx5_rep_sysfs_cleanup(rpriv);

	if (rep->vport == MLX5_VPORT_UPLINK) {
 		mlx5e_vport_uplink_rep_unload(rpriv);
		kfree(ppriv); /* mlx5e_rep_priv */
		priv->ppriv = NULL;
		return;
	}

	mlx5e_rep_metadata_remove(priv, rep);
#ifdef HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
	dl_port = mlx5_esw_offloads_devlink_port(dev->priv.eswitch, rpriv->rep->vport);
	if (dl_port)
		devlink_port_type_clear(dl_port);
#endif
	unregister_netdev(netdev);
	mlx5e_vport_rep_unload_compat(priv);
	mlx5e_detach_netdev(priv);
	priv->profile->cleanup(priv);
	mlx5e_destroy_netdev(priv);
	kfree(ppriv); /* mlx5e_rep_priv */
	priv->ppriv = NULL;
}

static void *mlx5e_vport_rep_get_proto_dev(struct mlx5_eswitch_rep *rep)
{
	struct mlx5e_rep_priv *rpriv;

	rpriv = mlx5e_rep_to_rep_priv(rep);

	return rpriv->netdev;
}

static void mlx5e_vport_rep_event_unpair(struct mlx5_eswitch_rep *rep)
{
	struct mlx5e_rep_priv *rpriv;
	struct mlx5e_rep_sq *rep_sq;

	rpriv = mlx5e_rep_to_rep_priv(rep);
	list_for_each_entry(rep_sq, &rpriv->vport_sqs_list, list) {
		if (!rep_sq->send_to_vport_rule_peer)
			continue;
		mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule_peer);
		rep_sq->send_to_vport_rule_peer = NULL;
	}
}

static int mlx5e_vport_rep_event_pair(struct mlx5_eswitch *esw,
				      struct mlx5_eswitch_rep *rep,
				      struct mlx5_eswitch *peer_esw)
{
	struct mlx5_flow_handle *flow_rule;
	struct mlx5e_rep_priv *rpriv;
	struct mlx5e_rep_sq *rep_sq;

	rpriv = mlx5e_rep_to_rep_priv(rep);
	list_for_each_entry(rep_sq, &rpriv->vport_sqs_list, list) {
		if (rep_sq->send_to_vport_rule_peer)
			continue;
		flow_rule = mlx5_eswitch_add_send_to_vport_rule(peer_esw, esw, rep, rep_sq->sqn);
		if (IS_ERR(flow_rule))
			goto err_out;
		rep_sq->send_to_vport_rule_peer = flow_rule;
	}

	return 0;
err_out:
	mlx5e_vport_rep_event_unpair(rep);
	return PTR_ERR(flow_rule);
}

static int mlx5e_vport_rep_event(struct mlx5_eswitch *esw,
				 struct mlx5_eswitch_rep *rep,
				 enum mlx5_switchdev_event event,
				 void *data)
{
	int err = 0;

	if (event == MLX5_SWITCHDEV_EVENT_PAIR)
		err = mlx5e_vport_rep_event_pair(esw, rep, data);
	else if (event == MLX5_SWITCHDEV_EVENT_UNPAIR)
		mlx5e_vport_rep_event_unpair(rep);

	return err;
}

static const struct mlx5_eswitch_rep_ops rep_ops = {
	.load = mlx5e_vport_rep_load,
	.unload = mlx5e_vport_rep_unload,
	.get_proto_dev = mlx5e_vport_rep_get_proto_dev,
	.event = mlx5e_vport_rep_event,
};

static int mlx5e_rep_probe(struct auxiliary_device *adev,
			   const struct auxiliary_device_id *id)
{
	struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
	struct mlx5_core_dev *mdev = edev->mdev;
	struct mlx5_eswitch *esw;

	esw = mdev->priv.eswitch;
	mlx5_eswitch_register_vport_reps(esw, &rep_ops, REP_ETH);
	return 0;
}

static void mlx5e_rep_remove(struct auxiliary_device *adev)
{
	struct mlx5_adev *vdev = container_of(adev, struct mlx5_adev, adev);
	struct mlx5_core_dev *mdev = vdev->mdev;
	struct mlx5_eswitch *esw;

	esw = mdev->priv.eswitch;
	mlx5_eswitch_unregister_vport_reps(esw, REP_ETH);
}

static const struct auxiliary_device_id mlx5e_rep_id_table[] = {
	{ .name = MLX5_ADEV_NAME ".eth-rep", },
	{},
};

MODULE_DEVICE_TABLE(auxiliary_mlx5e_rep_id_table, mlx5e_rep_id_table);

static struct auxiliary_driver mlx5e_rep_driver = {
	.name = "eth-rep",
	.probe = mlx5e_rep_probe,
	.remove = mlx5e_rep_remove,
	.id_table = mlx5e_rep_id_table,
};

int mlx5e_rep_init(void)
{
	return auxiliary_driver_register(&mlx5e_rep_driver);
}

void mlx5e_rep_cleanup(void)
{
	auxiliary_driver_unregister(&mlx5e_rep_driver);
}
