net: dsa: dsa2: Add basic support of devlink

Register the switch and its ports with devlink.

Signed-off-by: Andrew Lunn <andrew@lunn.ch>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Tested-by: Florian Fainelli <f.fainelli@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Andrew Lunn 2017-03-28 23:45:07 +02:00 committed by David S. Miller
parent c6e970a04b
commit 96567d5dac
2 changed files with 50 additions and 2 deletions

View File

@ -19,6 +19,7 @@
#include <linux/workqueue.h> #include <linux/workqueue.h>
#include <linux/of.h> #include <linux/of.h>
#include <linux/ethtool.h> #include <linux/ethtool.h>
#include <net/devlink.h>
struct tc_action; struct tc_action;
struct phy_device; struct phy_device;
@ -182,6 +183,7 @@ struct dsa_port {
unsigned int ageing_time; unsigned int ageing_time;
u8 stp_state; u8 stp_state;
struct net_device *bridge_dev; struct net_device *bridge_dev;
struct devlink_port devlink_port;
}; };
struct dsa_switch { struct dsa_switch {
@ -237,6 +239,9 @@ struct dsa_switch {
unsigned int ageing_time_min; unsigned int ageing_time_min;
unsigned int ageing_time_max; unsigned int ageing_time_max;
/* devlink used to represent this switch device */
struct devlink *devlink;
/* Dynamically allocated ports, keep last */ /* Dynamically allocated ports, keep last */
size_t num_ports; size_t num_ports;
struct dsa_port ports[]; struct dsa_port ports[];

View File

@ -24,6 +24,9 @@
static LIST_HEAD(dsa_switch_trees); static LIST_HEAD(dsa_switch_trees);
static DEFINE_MUTEX(dsa2_mutex); static DEFINE_MUTEX(dsa2_mutex);
static const struct devlink_ops dsa_devlink_ops = {
};
static struct dsa_switch_tree *dsa_get_dst(u32 tree) static struct dsa_switch_tree *dsa_get_dst(u32 tree)
{ {
struct dsa_switch_tree *dst; struct dsa_switch_tree *dst;
@ -223,12 +226,18 @@ static int dsa_dsa_port_apply(struct dsa_port *port, u32 index,
return err; return err;
} }
return 0; memset(&ds->ports[index].devlink_port, 0,
sizeof(ds->ports[index].devlink_port));
return devlink_port_register(ds->devlink,
&ds->ports[index].devlink_port,
index);
} }
static void dsa_dsa_port_unapply(struct dsa_port *port, u32 index, static void dsa_dsa_port_unapply(struct dsa_port *port, u32 index,
struct dsa_switch *ds) struct dsa_switch *ds)
{ {
devlink_port_unregister(&ds->ports[index].devlink_port);
dsa_cpu_dsa_destroy(port); dsa_cpu_dsa_destroy(port);
} }
@ -246,12 +255,17 @@ static int dsa_cpu_port_apply(struct dsa_port *port, u32 index,
ds->cpu_port_mask |= BIT(index); ds->cpu_port_mask |= BIT(index);
return 0; memset(&ds->ports[index].devlink_port, 0,
sizeof(ds->ports[index].devlink_port));
err = devlink_port_register(ds->devlink, &ds->ports[index].devlink_port,
index);
return err;
} }
static void dsa_cpu_port_unapply(struct dsa_port *port, u32 index, static void dsa_cpu_port_unapply(struct dsa_port *port, u32 index,
struct dsa_switch *ds) struct dsa_switch *ds)
{ {
devlink_port_unregister(&ds->ports[index].devlink_port);
dsa_cpu_dsa_destroy(port); dsa_cpu_dsa_destroy(port);
ds->cpu_port_mask &= ~BIT(index); ds->cpu_port_mask &= ~BIT(index);
@ -276,12 +290,23 @@ static int dsa_user_port_apply(struct dsa_port *port, u32 index,
return err; return err;
} }
memset(&ds->ports[index].devlink_port, 0,
sizeof(ds->ports[index].devlink_port));
err = devlink_port_register(ds->devlink, &ds->ports[index].devlink_port,
index);
if (err)
return err;
devlink_port_type_eth_set(&ds->ports[index].devlink_port,
ds->ports[index].netdev);
return 0; return 0;
} }
static void dsa_user_port_unapply(struct dsa_port *port, u32 index, static void dsa_user_port_unapply(struct dsa_port *port, u32 index,
struct dsa_switch *ds) struct dsa_switch *ds)
{ {
devlink_port_unregister(&ds->ports[index].devlink_port);
if (ds->ports[index].netdev) { if (ds->ports[index].netdev) {
dsa_slave_destroy(ds->ports[index].netdev); dsa_slave_destroy(ds->ports[index].netdev);
ds->ports[index].netdev = NULL; ds->ports[index].netdev = NULL;
@ -302,6 +327,17 @@ static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
*/ */
ds->phys_mii_mask = ds->enabled_port_mask; ds->phys_mii_mask = ds->enabled_port_mask;
/* Add the switch to devlink before calling setup, so that setup can
* add dpipe tables
*/
ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
if (!ds->devlink)
return -ENOMEM;
err = devlink_register(ds->devlink, ds->dev);
if (err)
return err;
err = ds->ops->setup(ds); err = ds->ops->setup(ds);
if (err < 0) if (err < 0)
return err; return err;
@ -382,6 +418,13 @@ static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
mdiobus_unregister(ds->slave_mii_bus); mdiobus_unregister(ds->slave_mii_bus);
dsa_switch_unregister_notifier(ds); dsa_switch_unregister_notifier(ds);
if (ds->devlink) {
devlink_unregister(ds->devlink);
devlink_free(ds->devlink);
ds->devlink = NULL;
}
} }
static int dsa_dst_apply(struct dsa_switch_tree *dst) static int dsa_dst_apply(struct dsa_switch_tree *dst)