net: dsa: use ports list to setup switches
authorVivien Didelot <vivien.didelot@gmail.com>
Mon, 21 Oct 2019 20:51:19 +0000 (16:51 -0400)
committerJakub Kicinski <jakub.kicinski@netronome.com>
Tue, 22 Oct 2019 19:37:06 +0000 (12:37 -0700)
Use the new ports list instead of iterating over switches and their
ports when setting up the switches and their ports.

At the same time, provide setup states and messages for ports and
switches as it is done for the trees.

Signed-off-by: Vivien Didelot <vivien.didelot@gmail.com>
Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
include/net/dsa.h
net/dsa/dsa2.c

index d2b7ee2..bd08bde 100644 (file)
@@ -215,9 +215,13 @@ struct dsa_port {
         * Original copy of the master netdev net_device_ops
         */
        const struct net_device_ops *orig_ndo_ops;
+
+       bool setup;
 };
 
 struct dsa_switch {
+       bool setup;
+
        struct device *dev;
 
        /*
index ba27ff8..01b6047 100644 (file)
@@ -267,6 +267,9 @@ static int dsa_port_setup(struct dsa_port *dp)
        bool dsa_port_enabled = false;
        int err = 0;
 
+       if (dp->setup)
+               return 0;
+
        switch (dp->type) {
        case DSA_PORT_TYPE_UNUSED:
                dsa_port_disable(dp);
@@ -335,14 +338,21 @@ static int dsa_port_setup(struct dsa_port *dp)
                dsa_port_link_unregister_of(dp);
        if (err && devlink_port_registered)
                devlink_port_unregister(dlp);
+       if (err)
+               return err;
 
-       return err;
+       dp->setup = true;
+
+       return 0;
 }
 
 static void dsa_port_teardown(struct dsa_port *dp)
 {
        struct devlink_port *dlp = &dp->devlink_port;
 
+       if (!dp->setup)
+               return;
+
        switch (dp->type) {
        case DSA_PORT_TYPE_UNUSED:
                break;
@@ -365,11 +375,16 @@ static void dsa_port_teardown(struct dsa_port *dp)
                }
                break;
        }
+
+       dp->setup = false;
 }
 
 static int dsa_switch_setup(struct dsa_switch *ds)
 {
-       int err = 0;
+       int err;
+
+       if (ds->setup)
+               return 0;
 
        /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
         * driver and before ops->setup() has run, since the switch drivers and
@@ -411,6 +426,8 @@ static int dsa_switch_setup(struct dsa_switch *ds)
                        goto unregister_notifier;
        }
 
+       ds->setup = true;
+
        return 0;
 
 unregister_notifier:
@@ -426,6 +443,9 @@ free_devlink:
 
 static void dsa_switch_teardown(struct dsa_switch *ds)
 {
+       if (!ds->setup)
+               return;
+
        if (ds->slave_mii_bus && ds->ops->phy_read)
                mdiobus_unregister(ds->slave_mii_bus);
 
@@ -440,78 +460,47 @@ static void dsa_switch_teardown(struct dsa_switch *ds)
                ds->devlink = NULL;
        }
 
+       ds->setup = false;
 }
 
 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
 {
-       struct dsa_switch *ds;
        struct dsa_port *dp;
-       int device, port, i;
-       int err = 0;
-
-       for (device = 0; device < DSA_MAX_SWITCHES; device++) {
-               ds = dst->ds[device];
-               if (!ds)
-                       continue;
+       int err;
 
-               err = dsa_switch_setup(ds);
+       list_for_each_entry(dp, &dst->ports, list) {
+               err = dsa_switch_setup(dp->ds);
                if (err)
-                       goto switch_teardown;
-
-               for (port = 0; port < ds->num_ports; port++) {
-                       dp = &ds->ports[port];
+                       goto teardown;
+       }
 
-                       err = dsa_port_setup(dp);
-                       if (err)
-                               goto ports_teardown;
-               }
+       list_for_each_entry(dp, &dst->ports, list) {
+               err = dsa_port_setup(dp);
+               if (err)
+                       goto teardown;
        }
 
        return 0;
 
-ports_teardown:
-       for (i = 0; i < port; i++)
-               dsa_port_teardown(&ds->ports[i]);
-
-       dsa_switch_teardown(ds);
-
-switch_teardown:
-       for (i = 0; i < device; i++) {
-               ds = dst->ds[i];
-               if (!ds)
-                       continue;
-
-               for (port = 0; port < ds->num_ports; port++) {
-                       dp = &ds->ports[port];
-
-                       dsa_port_teardown(dp);
-               }
+teardown:
+       list_for_each_entry(dp, &dst->ports, list)
+               dsa_port_teardown(dp);
 
-               dsa_switch_teardown(ds);
-       }
+       list_for_each_entry(dp, &dst->ports, list)
+               dsa_switch_teardown(dp->ds);
 
        return err;
 }
 
 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
 {
-       struct dsa_switch *ds;
        struct dsa_port *dp;
-       int device, port;
 
-       for (device = 0; device < DSA_MAX_SWITCHES; device++) {
-               ds = dst->ds[device];
-               if (!ds)
-                       continue;
+       list_for_each_entry(dp, &dst->ports, list)
+               dsa_port_teardown(dp);
 
-               for (port = 0; port < ds->num_ports; port++) {
-                       dp = &ds->ports[port];
-
-                       dsa_port_teardown(dp);
-               }
-
-               dsa_switch_teardown(ds);
-       }
+       list_for_each_entry(dp, &dst->ports, list)
+               dsa_switch_teardown(dp->ds);
 }
 
 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)