Commit 160dc373 authored by Dmytro Linkin's avatar Dmytro Linkin Committed by David S. Miller

netdevsim: Implement legacy/switchdev mode for VFs

Implement callbacks to set/get eswitch mode value. Add helpers to check
current mode.

Instantiate VFs' net devices and devlink ports on switchdev enabling and
remove them on legacy enabling. Changing number of VFs while in
switchdev mode triggers VFs creation/deletion.

Also disable NDO API callback to set VF rate, since it's legacy API.
Switchdev API to set VF rate will be implemented in one of the next
patches.
Signed-off-by: default avatarDmytro Linkin <dlinkin@nvidia.com>
Reviewed-by: default avatarJiri Pirko <jiri@nvidia.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 92ba1f29
...@@ -27,6 +27,9 @@ static struct nsim_bus_dev *to_nsim_bus_dev(struct device *dev) ...@@ -27,6 +27,9 @@ static struct nsim_bus_dev *to_nsim_bus_dev(struct device *dev)
static int nsim_bus_dev_vfs_enable(struct nsim_bus_dev *nsim_bus_dev, static int nsim_bus_dev_vfs_enable(struct nsim_bus_dev *nsim_bus_dev,
unsigned int num_vfs) unsigned int num_vfs)
{ {
struct nsim_dev *nsim_dev;
int err = 0;
if (nsim_bus_dev->max_vfs < num_vfs) if (nsim_bus_dev->max_vfs < num_vfs)
return -ENOMEM; return -ENOMEM;
...@@ -34,12 +37,24 @@ static int nsim_bus_dev_vfs_enable(struct nsim_bus_dev *nsim_bus_dev, ...@@ -34,12 +37,24 @@ static int nsim_bus_dev_vfs_enable(struct nsim_bus_dev *nsim_bus_dev,
return -ENOMEM; return -ENOMEM;
nsim_bus_dev->num_vfs = num_vfs; nsim_bus_dev->num_vfs = num_vfs;
return 0; nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
if (nsim_esw_mode_is_switchdev(nsim_dev)) {
err = nsim_esw_switchdev_enable(nsim_dev, NULL);
if (err)
nsim_bus_dev->num_vfs = 0;
}
return err;
} }
void nsim_bus_dev_vfs_disable(struct nsim_bus_dev *nsim_bus_dev) void nsim_bus_dev_vfs_disable(struct nsim_bus_dev *nsim_bus_dev)
{ {
struct nsim_dev *nsim_dev;
nsim_bus_dev->num_vfs = 0; nsim_bus_dev->num_vfs = 0;
nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
if (nsim_esw_mode_is_switchdev(nsim_dev))
nsim_esw_legacy_enable(nsim_dev, NULL);
} }
static ssize_t static ssize_t
......
...@@ -439,6 +439,72 @@ static void nsim_dev_dummy_region_exit(struct nsim_dev *nsim_dev) ...@@ -439,6 +439,72 @@ static void nsim_dev_dummy_region_exit(struct nsim_dev *nsim_dev)
devlink_region_destroy(nsim_dev->dummy_region); devlink_region_destroy(nsim_dev->dummy_region);
} }
static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port);
int nsim_esw_legacy_enable(struct nsim_dev *nsim_dev, struct netlink_ext_ack *extack)
{
struct nsim_dev_port *nsim_dev_port, *tmp;
mutex_lock(&nsim_dev->port_list_lock);
list_for_each_entry_safe(nsim_dev_port, tmp, &nsim_dev->port_list, list)
if (nsim_dev_port_is_vf(nsim_dev_port))
__nsim_dev_port_del(nsim_dev_port);
mutex_unlock(&nsim_dev->port_list_lock);
nsim_dev->esw_mode = DEVLINK_ESWITCH_MODE_LEGACY;
return 0;
}
int nsim_esw_switchdev_enable(struct nsim_dev *nsim_dev, struct netlink_ext_ack *extack)
{
struct nsim_bus_dev *nsim_bus_dev = nsim_dev->nsim_bus_dev;
int i, err;
for (i = 0; i < nsim_bus_dev->num_vfs; i++) {
err = nsim_dev_port_add(nsim_bus_dev, NSIM_DEV_PORT_TYPE_VF, i);
if (err) {
NL_SET_ERR_MSG_MOD(extack, "Failed to initialize VFs' netdevsim ports");
pr_err("Failed to initialize VF id=%d. %d.\n", i, err);
goto err_port_add_vfs;
}
}
nsim_dev->esw_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
return 0;
err_port_add_vfs:
for (i--; i >= 0; i--)
nsim_dev_port_del(nsim_bus_dev, NSIM_DEV_PORT_TYPE_VF, i);
return err;
}
static int nsim_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode,
struct netlink_ext_ack *extack)
{
struct nsim_dev *nsim_dev = devlink_priv(devlink);
int err = 0;
mutex_lock(&nsim_dev->nsim_bus_dev->vfs_lock);
if (mode == nsim_dev->esw_mode)
goto unlock;
if (mode == DEVLINK_ESWITCH_MODE_LEGACY)
err = nsim_esw_legacy_enable(nsim_dev, extack);
else if (mode == DEVLINK_ESWITCH_MODE_SWITCHDEV)
err = nsim_esw_switchdev_enable(nsim_dev, extack);
else
err = -EINVAL;
unlock:
mutex_unlock(&nsim_dev->nsim_bus_dev->vfs_lock);
return err;
}
static int nsim_devlink_eswitch_mode_get(struct devlink *devlink, u16 *mode)
{
struct nsim_dev *nsim_dev = devlink_priv(devlink);
*mode = nsim_dev->esw_mode;
return 0;
}
struct nsim_trap_item { struct nsim_trap_item {
void *trap_ctx; void *trap_ctx;
enum devlink_trap_action action; enum devlink_trap_action action;
...@@ -925,6 +991,8 @@ nsim_dev_devlink_trap_policer_counter_get(struct devlink *devlink, ...@@ -925,6 +991,8 @@ nsim_dev_devlink_trap_policer_counter_get(struct devlink *devlink,
} }
static const struct devlink_ops nsim_dev_devlink_ops = { static const struct devlink_ops nsim_dev_devlink_ops = {
.eswitch_mode_set = nsim_devlink_eswitch_mode_set,
.eswitch_mode_get = nsim_devlink_eswitch_mode_get,
.supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_COMPONENT | .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_COMPONENT |
DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK, DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
.reload_actions = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT), .reload_actions = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT),
...@@ -1177,6 +1245,7 @@ int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev) ...@@ -1177,6 +1245,7 @@ int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev)
devlink_params_publish(devlink); devlink_params_publish(devlink);
devlink_reload_enable(devlink); devlink_reload_enable(devlink);
nsim_dev->esw_mode = DEVLINK_ESWITCH_MODE_LEGACY;
return 0; return 0;
err_psample_exit: err_psample_exit:
......
...@@ -113,6 +113,11 @@ static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max) ...@@ -113,6 +113,11 @@ static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
struct netdevsim *ns = netdev_priv(dev); struct netdevsim *ns = netdev_priv(dev);
struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev; struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
if (nsim_esw_mode_is_switchdev(ns->nsim_dev)) {
pr_err("Not supported in switchdev mode. Please use devlink API.\n");
return -EOPNOTSUPP;
}
if (vf >= nsim_bus_dev->num_vfs) if (vf >= nsim_bus_dev->num_vfs)
return -EINVAL; return -EINVAL;
......
...@@ -257,8 +257,22 @@ struct nsim_dev { ...@@ -257,8 +257,22 @@ struct nsim_dev {
u32 sleep; u32 sleep;
} udp_ports; } udp_ports;
struct nsim_dev_psample *psample; struct nsim_dev_psample *psample;
u16 esw_mode;
}; };
int nsim_esw_legacy_enable(struct nsim_dev *nsim_dev, struct netlink_ext_ack *extack);
int nsim_esw_switchdev_enable(struct nsim_dev *nsim_dev, struct netlink_ext_ack *extack);
static inline bool nsim_esw_mode_is_legacy(struct nsim_dev *nsim_dev)
{
return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_LEGACY;
}
static inline bool nsim_esw_mode_is_switchdev(struct nsim_dev *nsim_dev)
{
return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV;
}
static inline struct net *nsim_dev_net(struct nsim_dev *nsim_dev) static inline struct net *nsim_dev_net(struct nsim_dev *nsim_dev)
{ {
return devlink_net(priv_to_devlink(nsim_dev)); return devlink_net(priv_to_devlink(nsim_dev));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment