Commit 1d27732f authored by Vivien Didelot's avatar Vivien Didelot Committed by David S. Miller

net: dsa: setup and teardown ports

The dsa_dsa_port_apply and dsa_cpu_port_apply functions do exactly the
same. The dsa_user_port_apply function does not try to register a fixed
link but try to create a slave.

This commit factorizes and scopes all that in two convenient
dsa_port_setup and dsa_port_teardown functions.

It won't hurt to register a devlink_port for unused port as well.
Signed-off-by: default avatarVivien Didelot <vivien.didelot@savoirfairelinux.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 1f08f9e9
...@@ -281,91 +281,65 @@ static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst) ...@@ -281,91 +281,65 @@ static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
dst->cpu_dp = NULL; dst->cpu_dp = NULL;
} }
static int dsa_dsa_port_apply(struct dsa_port *port) static int dsa_port_setup(struct dsa_port *dp)
{ {
struct dsa_switch *ds = port->ds; struct dsa_switch *ds = dp->ds;
int err;
err = dsa_port_fixed_link_register_of(port);
if (err) {
dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
port->index, err);
return err;
}
memset(&port->devlink_port, 0, sizeof(port->devlink_port));
return devlink_port_register(ds->devlink, &port->devlink_port,
port->index);
}
static void dsa_dsa_port_unapply(struct dsa_port *port)
{
devlink_port_unregister(&port->devlink_port);
dsa_port_fixed_link_unregister_of(port);
}
static int dsa_cpu_port_apply(struct dsa_port *port)
{
struct dsa_switch *ds = port->ds;
int err; int err;
err = dsa_port_fixed_link_register_of(port); memset(&dp->devlink_port, 0, sizeof(dp->devlink_port));
if (err) {
dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
port->index, err);
return err;
}
memset(&port->devlink_port, 0, sizeof(port->devlink_port)); err = devlink_port_register(ds->devlink, &dp->devlink_port, dp->index);
err = devlink_port_register(ds->devlink, &port->devlink_port, if (err)
port->index);
return err; return err;
}
static void dsa_cpu_port_unapply(struct dsa_port *port) switch (dp->type) {
{ case DSA_PORT_TYPE_UNUSED:
devlink_port_unregister(&port->devlink_port); break;
dsa_port_fixed_link_unregister_of(port); case DSA_PORT_TYPE_CPU:
} case DSA_PORT_TYPE_DSA:
err = dsa_port_fixed_link_register_of(dp);
static int dsa_user_port_apply(struct dsa_port *port)
{
struct dsa_switch *ds = port->ds;
int err;
err = dsa_slave_create(port);
if (err) { if (err) {
dev_warn(ds->dev, "Failed to create slave %d: %d\n", dev_err(ds->dev, "failed to register fixed link for port %d.%d\n",
port->index, err); ds->index, dp->index);
port->slave = NULL;
return err; return err;
} }
memset(&port->devlink_port, 0, sizeof(port->devlink_port)); break;
err = devlink_port_register(ds->devlink, &port->devlink_port, case DSA_PORT_TYPE_USER:
port->index); err = dsa_slave_create(dp);
if (err) if (err)
return err; dev_err(ds->dev, "failed to create slave for port %d.%d\n",
ds->index, dp->index);
devlink_port_type_eth_set(&port->devlink_port, port->slave); else
devlink_port_type_eth_set(&dp->devlink_port, dp->slave);
break;
}
return 0; return 0;
} }
static void dsa_user_port_unapply(struct dsa_port *port) static void dsa_port_teardown(struct dsa_port *dp)
{ {
devlink_port_unregister(&port->devlink_port); devlink_port_unregister(&dp->devlink_port);
if (port->slave) {
dsa_slave_destroy(port->slave); switch (dp->type) {
port->slave = NULL; case DSA_PORT_TYPE_UNUSED:
break;
case DSA_PORT_TYPE_CPU:
case DSA_PORT_TYPE_DSA:
dsa_port_fixed_link_unregister_of(dp);
break;
case DSA_PORT_TYPE_USER:
if (dp->slave) {
dsa_slave_destroy(dp->slave);
dp->slave = NULL;
}
break;
} }
} }
static int dsa_switch_setup(struct dsa_switch *ds) static int dsa_switch_setup(struct dsa_switch *ds)
{ {
struct dsa_port *port;
u32 index;
int err; int err;
/* Initialize ds->phys_mii_mask before registering the slave MDIO bus /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
...@@ -406,56 +380,11 @@ static int dsa_switch_setup(struct dsa_switch *ds) ...@@ -406,56 +380,11 @@ static int dsa_switch_setup(struct dsa_switch *ds)
return err; return err;
} }
for (index = 0; index < ds->num_ports; index++) {
port = &ds->ports[index];
if (!dsa_port_is_valid(port))
continue;
if (dsa_port_is_dsa(port)) {
err = dsa_dsa_port_apply(port);
if (err)
return err;
continue;
}
if (dsa_port_is_cpu(port)) {
err = dsa_cpu_port_apply(port);
if (err)
return err;
continue;
}
err = dsa_user_port_apply(port);
if (err)
continue;
}
return 0; return 0;
} }
static void dsa_switch_teardown(struct dsa_switch *ds) static void dsa_switch_teardown(struct dsa_switch *ds)
{ {
struct dsa_port *port;
u32 index;
for (index = 0; index < ds->num_ports; index++) {
port = &ds->ports[index];
if (!dsa_port_is_valid(port))
continue;
if (dsa_port_is_dsa(port)) {
dsa_dsa_port_unapply(port);
continue;
}
if (dsa_port_is_cpu(port)) {
dsa_cpu_port_unapply(port);
continue;
}
dsa_user_port_unapply(port);
}
if (ds->slave_mii_bus && ds->ops->phy_read) if (ds->slave_mii_bus && ds->ops->phy_read)
mdiobus_unregister(ds->slave_mii_bus); mdiobus_unregister(ds->slave_mii_bus);
...@@ -472,7 +401,8 @@ static void dsa_switch_teardown(struct dsa_switch *ds) ...@@ -472,7 +401,8 @@ static void dsa_switch_teardown(struct dsa_switch *ds)
static int dsa_tree_setup_switches(struct dsa_switch_tree *dst) static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
{ {
struct dsa_switch *ds; struct dsa_switch *ds;
int device; struct dsa_port *dp;
int device, port;
int err; int err;
for (device = 0; device < DSA_MAX_SWITCHES; device++) { for (device = 0; device < DSA_MAX_SWITCHES; device++) {
...@@ -483,6 +413,14 @@ static int dsa_tree_setup_switches(struct dsa_switch_tree *dst) ...@@ -483,6 +413,14 @@ static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
err = dsa_switch_setup(ds); err = dsa_switch_setup(ds);
if (err) if (err)
return err; return err;
for (port = 0; port < ds->num_ports; port++) {
dp = &ds->ports[port];
err = dsa_port_setup(dp);
if (err)
return err;
}
} }
return 0; return 0;
...@@ -491,13 +429,20 @@ static int dsa_tree_setup_switches(struct dsa_switch_tree *dst) ...@@ -491,13 +429,20 @@ static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst) static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
{ {
struct dsa_switch *ds; struct dsa_switch *ds;
int device; struct dsa_port *dp;
int device, port;
for (device = 0; device < DSA_MAX_SWITCHES; device++) { for (device = 0; device < DSA_MAX_SWITCHES; device++) {
ds = dst->ds[device]; ds = dst->ds[device];
if (!ds) if (!ds)
continue; continue;
for (port = 0; port < ds->num_ports; port++) {
dp = &ds->ports[port];
dsa_port_teardown(dp);
}
dsa_switch_teardown(ds); dsa_switch_teardown(ds);
} }
} }
......
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