Skip to content

Commit

Permalink
netdevsim: move details of vf config to dev
Browse files Browse the repository at this point in the history
Since "eswitch" configuration was added bus.c contains
a lot of device details which really belong to dev.c.

Restructure the code while moving it.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Jakub Kicinski authored and David S. Miller committed Nov 1, 2021
1 parent 5e388f3 commit 1c40107
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 73 deletions.
69 changes: 4 additions & 65 deletions drivers/net/netdevsim/bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/rtnetlink.h>
#include <linux/slab.h>
#include <linux/sysfs.h>

Expand All @@ -24,50 +23,11 @@ static struct nsim_bus_dev *to_nsim_bus_dev(struct device *dev)
return container_of(dev, struct nsim_bus_dev, dev);
}

static void
nsim_bus_dev_set_vfs(struct nsim_bus_dev *nsim_bus_dev, unsigned int num_vfs)
{
rtnl_lock();
nsim_bus_dev->num_vfs = num_vfs;
rtnl_unlock();
}

static int nsim_bus_dev_vfs_enable(struct nsim_bus_dev *nsim_bus_dev,
unsigned int num_vfs)
{
struct nsim_dev *nsim_dev;
int err = 0;

if (nsim_bus_dev->max_vfs < num_vfs)
return -ENOMEM;
nsim_bus_dev_set_vfs(nsim_bus_dev, num_vfs);

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_set_vfs(nsim_bus_dev, 0);
}

return err;
}

void nsim_bus_dev_vfs_disable(struct nsim_bus_dev *nsim_bus_dev)
{
struct nsim_dev *nsim_dev;

nsim_bus_dev_set_vfs(nsim_bus_dev, 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
nsim_bus_dev_numvfs_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
struct nsim_dev *nsim_dev = dev_get_drvdata(dev);
unsigned int num_vfs;
int ret;

Expand All @@ -76,33 +36,12 @@ nsim_bus_dev_numvfs_store(struct device *dev, struct device_attribute *attr,
return ret;

device_lock(dev);
if (!nsim_dev) {
ret = -ENOENT;
goto exit_unlock;
}

mutex_lock(&nsim_dev->vfs_lock);
if (nsim_bus_dev->num_vfs == num_vfs)
goto exit_good;
if (nsim_bus_dev->num_vfs && num_vfs) {
ret = -EBUSY;
goto exit_unlock;
}

if (num_vfs) {
ret = nsim_bus_dev_vfs_enable(nsim_bus_dev, num_vfs);
if (ret)
goto exit_unlock;
} else {
nsim_bus_dev_vfs_disable(nsim_bus_dev);
}
exit_good:
ret = count;
exit_unlock:
mutex_unlock(&nsim_dev->vfs_lock);
ret = -ENOENT;
if (dev_get_drvdata(dev))
ret = nsim_drv_configure_vfs(nsim_bus_dev, num_vfs);
device_unlock(dev);

return ret;
return ret ? ret : count;
}

static ssize_t
Expand Down
61 changes: 57 additions & 4 deletions drivers/net/netdevsim/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ unsigned int nsim_dev_get_vfs(struct nsim_dev *nsim_dev)
return nsim_dev->nsim_bus_dev->num_vfs;
}

static void
nsim_bus_dev_set_vfs(struct nsim_bus_dev *nsim_bus_dev, unsigned int num_vfs)
{
rtnl_lock();
nsim_bus_dev->num_vfs = num_vfs;
rtnl_unlock();
}

#define NSIM_DEV_DUMMY_REGION_SIZE (1024 * 32)

static int
Expand Down Expand Up @@ -496,7 +504,9 @@ static void nsim_dev_dummy_region_exit(struct nsim_dev *nsim_dev)
}

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)

static int nsim_esw_legacy_enable(struct nsim_dev *nsim_dev,
struct netlink_ext_ack *extack)
{
struct devlink *devlink = priv_to_devlink(nsim_dev);
struct nsim_dev_port *nsim_dev_port, *tmp;
Expand All @@ -511,7 +521,8 @@ int nsim_esw_legacy_enable(struct nsim_dev *nsim_dev, struct netlink_ext_ack *ex
return 0;
}

int nsim_esw_switchdev_enable(struct nsim_dev *nsim_dev, struct netlink_ext_ack *extack)
static 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;
Expand Down Expand Up @@ -1565,8 +1576,11 @@ static void nsim_dev_reload_destroy(struct nsim_dev *nsim_dev)
debugfs_remove(nsim_dev->take_snapshot);

mutex_lock(&nsim_dev->vfs_lock);
if (nsim_dev_get_vfs(nsim_dev))
nsim_bus_dev_vfs_disable(nsim_dev->nsim_bus_dev);
if (nsim_dev_get_vfs(nsim_dev)) {
nsim_bus_dev_set_vfs(nsim_dev->nsim_bus_dev, 0);
if (nsim_esw_mode_is_switchdev(nsim_dev))
nsim_esw_legacy_enable(nsim_dev, NULL);
}
mutex_unlock(&nsim_dev->vfs_lock);

nsim_dev_port_del_all(nsim_dev);
Expand Down Expand Up @@ -1641,6 +1655,45 @@ int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev, enum nsim_dev_port_type
return err;
}

int nsim_drv_configure_vfs(struct nsim_bus_dev *nsim_bus_dev,
unsigned int num_vfs)
{
struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
int ret;

mutex_lock(&nsim_dev->vfs_lock);
if (nsim_bus_dev->num_vfs == num_vfs) {
ret = 0;
goto exit_unlock;
}
if (nsim_bus_dev->num_vfs && num_vfs) {
ret = -EBUSY;
goto exit_unlock;
}
if (nsim_bus_dev->max_vfs < num_vfs) {
ret = -ENOMEM;
goto exit_unlock;
}

nsim_bus_dev_set_vfs(nsim_bus_dev, num_vfs);
if (nsim_esw_mode_is_switchdev(nsim_dev)) {
if (num_vfs) {
ret = nsim_esw_switchdev_enable(nsim_dev, NULL);
if (ret) {
nsim_bus_dev_set_vfs(nsim_bus_dev, 0);
goto exit_unlock;
}
} else {
nsim_esw_legacy_enable(nsim_dev, NULL);
}
}

exit_unlock:
mutex_unlock(&nsim_dev->vfs_lock);

return ret;
}

int nsim_dev_init(void)
{
nsim_dev_ddir = debugfs_create_dir(DRV_NAME, NULL);
Expand Down
6 changes: 2 additions & 4 deletions drivers/net/netdevsim/netdevsim.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,6 @@ struct nsim_dev {
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;
Expand All @@ -309,6 +306,8 @@ int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev,
int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev,
enum nsim_dev_port_type type,
unsigned int port_index);
int nsim_drv_configure_vfs(struct nsim_bus_dev *nsim_bus_dev,
unsigned int num_vfs);

unsigned int nsim_dev_get_vfs(struct nsim_dev *nsim_dev);

Expand All @@ -324,7 +323,6 @@ ssize_t nsim_bus_dev_max_vfs_read(struct file *file,
ssize_t nsim_bus_dev_max_vfs_write(struct file *file,
const char __user *data,
size_t count, loff_t *ppos);
void nsim_bus_dev_vfs_disable(struct nsim_bus_dev *nsim_bus_dev);

static inline bool nsim_dev_port_is_pf(struct nsim_dev_port *nsim_dev_port)
{
Expand Down

0 comments on commit 1c40107

Please sign in to comment.