netdevsim: move details of vf config to dev
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>
This commit is contained in:
parent
5e388f3dc3
commit
1c401078bc
@ -8,7 +8,6 @@
|
|||||||
#include <linux/kernel.h>
|
#include <linux/kernel.h>
|
||||||
#include <linux/list.h>
|
#include <linux/list.h>
|
||||||
#include <linux/mutex.h>
|
#include <linux/mutex.h>
|
||||||
#include <linux/rtnetlink.h>
|
|
||||||
#include <linux/slab.h>
|
#include <linux/slab.h>
|
||||||
#include <linux/sysfs.h>
|
#include <linux/sysfs.h>
|
||||||
|
|
||||||
@ -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);
|
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
|
static ssize_t
|
||||||
nsim_bus_dev_numvfs_store(struct device *dev, struct device_attribute *attr,
|
nsim_bus_dev_numvfs_store(struct device *dev, struct device_attribute *attr,
|
||||||
const char *buf, size_t count)
|
const char *buf, size_t count)
|
||||||
{
|
{
|
||||||
struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
|
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;
|
unsigned int num_vfs;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
@ -76,33 +36,12 @@ nsim_bus_dev_numvfs_store(struct device *dev, struct device_attribute *attr,
|
|||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
device_lock(dev);
|
device_lock(dev);
|
||||||
if (!nsim_dev) {
|
ret = -ENOENT;
|
||||||
ret = -ENOENT;
|
if (dev_get_drvdata(dev))
|
||||||
goto exit_unlock;
|
ret = nsim_drv_configure_vfs(nsim_bus_dev, num_vfs);
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
device_unlock(dev);
|
device_unlock(dev);
|
||||||
|
|
||||||
return ret;
|
return ret ? ret : count;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t
|
static ssize_t
|
||||||
|
@ -64,6 +64,14 @@ unsigned int nsim_dev_get_vfs(struct nsim_dev *nsim_dev)
|
|||||||
return nsim_dev->nsim_bus_dev->num_vfs;
|
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)
|
#define NSIM_DEV_DUMMY_REGION_SIZE (1024 * 32)
|
||||||
|
|
||||||
static int
|
static int
|
||||||
@ -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);
|
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 devlink *devlink = priv_to_devlink(nsim_dev);
|
||||||
struct nsim_dev_port *nsim_dev_port, *tmp;
|
struct nsim_dev_port *nsim_dev_port, *tmp;
|
||||||
@ -511,7 +521,8 @@ int nsim_esw_legacy_enable(struct nsim_dev *nsim_dev, struct netlink_ext_ack *ex
|
|||||||
return 0;
|
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;
|
struct nsim_bus_dev *nsim_bus_dev = nsim_dev->nsim_bus_dev;
|
||||||
int i, err;
|
int i, err;
|
||||||
@ -1565,8 +1576,11 @@ static void nsim_dev_reload_destroy(struct nsim_dev *nsim_dev)
|
|||||||
debugfs_remove(nsim_dev->take_snapshot);
|
debugfs_remove(nsim_dev->take_snapshot);
|
||||||
|
|
||||||
mutex_lock(&nsim_dev->vfs_lock);
|
mutex_lock(&nsim_dev->vfs_lock);
|
||||||
if (nsim_dev_get_vfs(nsim_dev))
|
if (nsim_dev_get_vfs(nsim_dev)) {
|
||||||
nsim_bus_dev_vfs_disable(nsim_dev->nsim_bus_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);
|
mutex_unlock(&nsim_dev->vfs_lock);
|
||||||
|
|
||||||
nsim_dev_port_del_all(nsim_dev);
|
nsim_dev_port_del_all(nsim_dev);
|
||||||
@ -1641,6 +1655,45 @@ int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev, enum nsim_dev_port_type
|
|||||||
return err;
|
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)
|
int nsim_dev_init(void)
|
||||||
{
|
{
|
||||||
nsim_dev_ddir = debugfs_create_dir(DRV_NAME, NULL);
|
nsim_dev_ddir = debugfs_create_dir(DRV_NAME, NULL);
|
||||||
|
@ -281,9 +281,6 @@ struct nsim_dev {
|
|||||||
u16 esw_mode;
|
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)
|
static inline bool nsim_esw_mode_is_legacy(struct nsim_dev *nsim_dev)
|
||||||
{
|
{
|
||||||
return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_LEGACY;
|
return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_LEGACY;
|
||||||
@ -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,
|
int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev,
|
||||||
enum nsim_dev_port_type type,
|
enum nsim_dev_port_type type,
|
||||||
unsigned int port_index);
|
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);
|
unsigned int nsim_dev_get_vfs(struct nsim_dev *nsim_dev);
|
||||||
|
|
||||||
@ -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,
|
ssize_t nsim_bus_dev_max_vfs_write(struct file *file,
|
||||||
const char __user *data,
|
const char __user *data,
|
||||||
size_t count, loff_t *ppos);
|
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)
|
static inline bool nsim_dev_port_is_pf(struct nsim_dev_port *nsim_dev_port)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user