Re: [PATCH net-next v3 1/7] dsa: move call to driver port_setup after creation of netdev.

Vladimir Oltean
2024-04-01T15:53:33Z

[-- Attachment #1: Type: text/plain, Size: 3137 bytes --]

Title: "net: dsa:" prefix, no "."

On Mon, Apr 01, 2024 at 08:35:46AM -0500, Andrew Lunn wrote:
> The drivers call port_setup() is a good place to add the LEDs of a
> port to the netdev representing the port. However, when port_setup()
> is called in dsa_port_devlink_setup() the netdev does not exist
> yet. That only happens in dsa_user_create() which is latter in

later

> dsa_port_setup().
>
> Move the call to port_setup() out of dsa_port_devlink_setup() and to
> the end of dsa_port_setup() where the netdev will exist.
>
> Signed-off-by: Andrew Lunn <andrew@lunn.ch>
> ---
> net/dsa/devlink.c | 17 +----------------
> net/dsa/dsa.c | 3 +++
> 2 files changed, 4 insertions(+), 16 deletions(-)
>
> diff --git a/net/dsa/devlink.c b/net/dsa/devlink.c
> index 431bf52290a1..9c3dc6319269 100644
> --- a/net/dsa/devlink.c
> +++ b/net/dsa/devlink.c
> @@ -294,20 +294,12 @@ int dsa_port_devlink_setup(struct dsa_port *dp)
> struct dsa_switch_tree *dst = dp->ds->dst;
> struct devlink_port_attrs attrs = {};
> struct devlink *dl = dp->ds->devlink;
> - struct dsa_switch *ds = dp->ds;
> const unsigned char *id;
> unsigned char len;
> - int err;
>
> memset(dlp, 0, sizeof(*dlp));
> devlink_port_init(dl, dlp);
>
> - if (ds->ops->port_setup) {
> - err = ds->ops->port_setup(ds, dp->index);
> - if (err)
> - return err;
> - }
> -
> id = (const unsigned char *)&dst->index;
> len = sizeof(dst->index);
>
> @@ -331,14 +323,7 @@ int dsa_port_devlink_setup(struct dsa_port *dp)
> }
>
> devlink_port_attrs_set(dlp, &attrs);
> - err = devlink_port_register(dl, dlp, dp->index);
> - if (err) {
> - if (ds->ops->port_teardown)
> - ds->ops->port_teardown(ds, dp->index);
> - return err;
> - }
> -
> - return 0;
> + return devlink_port_register(dl, dlp, dp->index);
> }
>
> void dsa_port_devlink_teardown(struct dsa_port *dp)
> diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c
> index 09d2f5d4b3dd..6ffee2a7de94 100644
> --- a/net/dsa/dsa.c
> +++ b/net/dsa/dsa.c
> @@ -520,6 +520,9 @@ static int dsa_port_setup(struct dsa_port *dp)
> break;
> }
>
> + if (ds->ops->port_setup)
> + err = ds->ops->port_setup(ds, dp->index);
> +

This overwrites the not-yet-checked "err", masking the dsa_user_create()
return code, and breaking the error handling logic below. Not to
mention, if ds->ops->port_setup() fails for a user port, we should call
dsa_user_destroy().

> if (err && dsa_port_enabled)
> dsa_port_disable(dp);
> if (err && dsa_port_link_registered)
>
> --
> 2.43.0
>

It would have been good for the API, if we want the netdev to be
available for user ports at port_setup() time, for it to be available at
port_teardown() time as well. So dsa_port_devlink_teardown() needs
changing too.

Additionally, for CPU and DSA ports, this change will make
ds->ops->port_enable() be visible from the driver API earlier than
ds->ops->port_setup(), which isn't exactly intuitive or great or better
than before.

In fact, I think it's very difficult not to make mistakes changing the
code in its current form. These 3 patches I've prepared - which replace
this patch - should help (see attached).

[-- Attachment #2: 0001-net-dsa-consolidate-setup-and-teardown-for-shared-po.patch --]
[-- Type: text/x-diff, Size: 1841 bytes --]

From 205b3503ca15c93ba8e90d42bb2855fa0c8497e3 Mon Sep 17 00:00:00 2001
From: Vladimir Oltean <vladimir.oltean@nxp.com>
Date: Mon, 1 Apr 2024 17:43:29 +0300
Subject: [PATCH 1/3] net: dsa: consolidate setup and teardown for shared ports

CPU and DSA ports have the same port setup and teardown logic, only the
string that gets printed on error differs. Consolidate the code paths.

Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
---
net/dsa/dsa.c | 24 ++----------------------
1 file changed, 2 insertions(+), 22 deletions(-)

diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c
index 09d2f5d4b3dd..64369fa5fd07 100644
--- a/net/dsa/dsa.c
+++ b/net/dsa/dsa.c
@@ -479,23 +479,6 @@ static int dsa_port_setup(struct dsa_port *dp)
dsa_port_disable(dp);
break;
case DSA_PORT_TYPE_CPU:
- if (dp->dn) {
- err = dsa_shared_port_link_register_of(dp);
- if (err)
- break;
- dsa_port_link_registered = true;
- } else {
- dev_warn(ds->dev,
- "skipping link registration for CPU port %d\n",
- dp->index);
- }
-
- err = dsa_port_enable(dp, NULL);
- if (err)
- break;
- dsa_port_enabled = true;
-
- break;
case DSA_PORT_TYPE_DSA:
if (dp->dn) {
err = dsa_shared_port_link_register_of(dp);
@@ -504,7 +487,8 @@ static int dsa_port_setup(struct dsa_port *dp)
dsa_port_link_registered = true;
} else {
dev_warn(ds->dev,
- "skipping link registration for DSA port %d\n",
+ "skipping link registration for %s port %d\n",
+ dsa_port_is_cpu(dp) ? "CPU" : "DSA",
dp->index);
}

@@ -543,10 +527,6 @@ static void dsa_port_teardown(struct dsa_port *dp)
case DSA_PORT_TYPE_UNUSED:
break;
case DSA_PORT_TYPE_CPU:
- dsa_port_disable(dp);
- if (dp->dn)
- dsa_shared_port_link_unregister_of(dp);
- break;
case DSA_PORT_TYPE_DSA:
dsa_port_disable(dp);
if (dp->dn)
--
2.34.1

[-- Attachment #3: 0002-net-dsa-break-out-port-setup-and-teardown-code-per-p.patch --]
[-- Type: text/x-diff, Size: 3823 bytes --]

From b3876b65634004dc16648016820077b21a6270dd Mon Sep 17 00:00:00 2001
From: Vladimir Oltean <vladimir.oltean@nxp.com>
Date: Mon, 1 Apr 2024 18:01:28 +0300
Subject: [PATCH 2/3] net: dsa: break out port setup and teardown code per port
type

It is very hard to make changes to the control flow of dsa_port_setup(),
and this is because the different port types need a different setup
procedure.

By breaking these out into separate functions, it becomes clearer what
needs what, and how the teardown should look like.

Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
---
net/dsa/dsa.c | 102 +++++++++++++++++++++++++++++++++-----------------
1 file changed, 67 insertions(+), 35 deletions(-)

diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c
index 64369fa5fd07..5d65da9a1971 100644
--- a/net/dsa/dsa.c
+++ b/net/dsa/dsa.c
@@ -460,12 +460,69 @@ static void dsa_tree_teardown_cpu_ports(struct dsa_switch_tree *dst)
dp->cpu_dp = NULL;
}

-static int dsa_port_setup(struct dsa_port *dp)
+static int dsa_unused_port_setup(struct dsa_port *dp)
+{
+ dsa_port_disable(dp);
+
+ return 0;
+}
+
+static void dsa_unused_port_teardown(struct dsa_port *dp)
+{
+}
+
+static int dsa_shared_port_setup(struct dsa_port *dp)
{
- bool dsa_port_link_registered = false;
struct dsa_switch *ds = dp->ds;
- bool dsa_port_enabled = false;
- int err = 0;
+ bool link_registered = false;
+ int err;
+
+ if (dp->dn) {
+ err = dsa_shared_port_link_register_of(dp);
+ if (err)
+ return err;
+
+ link_registered = true;
+ } else {
+ dev_warn(ds->dev,
+ "skipping link registration for %s port %d\n",
+ dsa_port_is_cpu(dp) ? "CPU" : "DSA",
+ dp->index);
+ }
+
+ err = dsa_port_enable(dp, NULL);
+ if (err && link_registered)
+ dsa_shared_port_link_unregister_of(dp);
+
+ return err;
+}
+
+static void dsa_shared_port_teardown(struct dsa_port *dp)
+{
+ dsa_port_disable(dp);
+ if (dp->dn)
+ dsa_shared_port_link_unregister_of(dp);
+}
+
+static int dsa_user_port_setup(struct dsa_port *dp)
+{
+ of_get_mac_address(dp->dn, dp->mac);
+
+ return dsa_user_create(dp);
+}
+
+static void dsa_user_port_teardown(struct dsa_port *dp)
+{
+ if (!dp->user)
+ return;
+
+ dsa_user_destroy(dp->user);
+ dp->user = NULL;
+}
+
+static int dsa_port_setup(struct dsa_port *dp)
+{
+ int err;

if (dp->setup)
return 0;
@@ -476,38 +533,17 @@ static int dsa_port_setup(struct dsa_port *dp)

switch (dp->type) {
case DSA_PORT_TYPE_UNUSED:
- dsa_port_disable(dp);
+ err = dsa_unused_port_setup(dp);
break;
case DSA_PORT_TYPE_CPU:
case DSA_PORT_TYPE_DSA:
- if (dp->dn) {
- err = dsa_shared_port_link_register_of(dp);
- if (err)
- break;
- dsa_port_link_registered = true;
- } else {
- dev_warn(ds->dev,
- "skipping link registration for %s port %d\n",
- dsa_port_is_cpu(dp) ? "CPU" : "DSA",
- dp->index);
- }
-
- err = dsa_port_enable(dp, NULL);
- if (err)
- break;
- dsa_port_enabled = true;
-
+ err = dsa_shared_port_setup(dp);
break;
case DSA_PORT_TYPE_USER:
- of_get_mac_address(dp->dn, dp->mac);
- err = dsa_user_create(dp);
+ err = dsa_user_port_setup(dp);
break;
}

- if (err && dsa_port_enabled)
- dsa_port_disable(dp);
- if (err && dsa_port_link_registered)
- dsa_shared_port_link_unregister_of(dp);
if (err) {
dsa_port_devlink_teardown(dp);
return err;
@@ -525,18 +561,14 @@ static void dsa_port_teardown(struct dsa_port *dp)

switch (dp->type) {
case DSA_PORT_TYPE_UNUSED:
+ dsa_unused_port_teardown(dp);
break;
case DSA_PORT_TYPE_CPU:
case DSA_PORT_TYPE_DSA:
- dsa_port_disable(dp);
- if (dp->dn)
- dsa_shared_port_link_unregister_of(dp);
+ dsa_shared_port_teardown(dp);
break;
case DSA_PORT_TYPE_USER:
- if (dp->user) {
- dsa_user_destroy(dp->user);
- dp->user = NULL;
- }
+ dsa_user_port_teardown(dp);
break;
}

--
2.34.1

[-- Attachment #4: 0003-net-dsa-move-call-to-driver-port_setup-after-creatio.patch --]
[-- Type: text/x-diff, Size: 4534 bytes --]

From afebdb6e4b52101e863aa36a2fedb5828d93ed4c Mon Sep 17 00:00:00 2001
From: Vladimir Oltean <vladimir.oltean@nxp.com>
Date: Mon, 1 Apr 2024 18:25:51 +0300
Subject: [PATCH 3/3] net: dsa: move call to driver port_setup after creation
of netdev

The driver-facing method port_setup() is a good place to add the LEDs of
a port to the netdev representing the port. However, when port_setup()
is called in dsa_port_devlink_setup(), the netdev does not exist
yet. That only happens in dsa_user_create(), which is later in
dsa_port_setup().

Move the call to port_setup() out of dsa_port_devlink_setup() and to
the end of dsa_port_setup() where the netdev will exist. For the other
port types, the call to port_setup() and port_teardown() remains where
it was before (functionally speaking), but now it needs to be open-coded
in their respective setup/teardown logic.

Signed-off-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
---
net/dsa/devlink.c | 17 +-------------
net/dsa/dsa.c | 59 +++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 58 insertions(+), 18 deletions(-)

diff --git a/net/dsa/devlink.c b/net/dsa/devlink.c
index 431bf52290a1..9c3dc6319269 100644
--- a/net/dsa/devlink.c
+++ b/net/dsa/devlink.c
@@ -294,20 +294,12 @@ int dsa_port_devlink_setup(struct dsa_port *dp)
struct dsa_switch_tree *dst = dp->ds->dst;
struct devlink_port_attrs attrs = {};
struct devlink *dl = dp->ds->devlink;
- struct dsa_switch *ds = dp->ds;
const unsigned char *id;
unsigned char len;
- int err;

memset(dlp, 0, sizeof(*dlp));
devlink_port_init(dl, dlp);

- if (ds->ops->port_setup) {
- err = ds->ops->port_setup(ds, dp->index);
- if (err)
- return err;
- }
-
id = (const unsigned char *)&dst->index;
len = sizeof(dst->index);

@@ -331,14 +323,7 @@ int dsa_port_devlink_setup(struct dsa_port *dp)
}

devlink_port_attrs_set(dlp, &attrs);
- err = devlink_port_register(dl, dlp, dp->index);
- if (err) {
- if (ds->ops->port_teardown)
- ds->ops->port_teardown(ds, dp->index);
- return err;
- }
-
- return 0;
+ return devlink_port_register(dl, dlp, dp->index);
}

void dsa_port_devlink_teardown(struct dsa_port *dp)
diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c
index 5d65da9a1971..d8aa869e17ba 100644
--- a/net/dsa/dsa.c
+++ b/net/dsa/dsa.c
@@ -462,6 +462,15 @@ static void dsa_tree_teardown_cpu_ports(struct dsa_switch_tree *dst)

static int dsa_unused_port_setup(struct dsa_port *dp)
{
+ struct dsa_switch *ds = dp->ds;
+ int err;
+
+ if (ds->ops->port_setup) {
+ err = ds->ops->port_setup(ds, dp->index);
+ if (err)
+ return err;
+ }
+
dsa_port_disable(dp);

return 0;
@@ -469,6 +478,10 @@ static int dsa_unused_port_setup(struct dsa_port *dp)

static void dsa_unused_port_teardown(struct dsa_port *dp)
{
+ struct dsa_switch *ds = dp->ds;
+
+ if (ds->ops->port_teardown)
+ ds->ops->port_teardown(ds, dp->index);
}

static int dsa_shared_port_setup(struct dsa_port *dp)
@@ -490,8 +503,23 @@ static int dsa_shared_port_setup(struct dsa_port *dp)
dp->index);
}

+ if (ds->ops->port_setup) {
+ err = ds->ops->port_setup(ds, dp->index);
+ if (err)
+ goto unregister_link;
+ }
+
err = dsa_port_enable(dp, NULL);
- if (err && link_registered)
+ if (err)
+ goto port_teardown;
+
+ return 0;
+
+port_teardown:
+ if (ds->ops->port_teardown)
+ ds->ops->port_teardown(ds, dp->index);
+unregister_link:
+ if (link_registered)
dsa_shared_port_link_unregister_of(dp);

return err;
@@ -499,23 +527,50 @@ static int dsa_shared_port_setup(struct dsa_port *dp)

static void dsa_shared_port_teardown(struct dsa_port *dp)
{
+ struct dsa_switch *ds = dp->ds;
+
dsa_port_disable(dp);
+ if (ds->ops->port_teardown)
+ ds->ops->port_teardown(ds, dp->index);
if (dp->dn)
dsa_shared_port_link_unregister_of(dp);
}

static int dsa_user_port_setup(struct dsa_port *dp)
{
+ struct dsa_switch *ds = dp->ds;
+ int err;
+
of_get_mac_address(dp->dn, dp->mac);

- return dsa_user_create(dp);
+ err = dsa_user_create(dp);
+ if (err)
+ return err;
+
+ if (ds->ops->port_setup) {
+ err = ds->ops->port_setup(ds, dp->index);
+ if (err)
+ goto user_destroy;
+ }
+
+ return 0;
+
+user_destroy:
+ dsa_user_destroy(dp->user);
+ dp->user = NULL;
+ return err;
}

static void dsa_user_port_teardown(struct dsa_port *dp)
{
+ struct dsa_switch *ds = dp->ds;
+
if (!dp->user)
return;

+ if (ds->ops->port_teardown)
+ ds->ops->port_teardown(ds, dp->index);
+
dsa_user_destroy(dp->user);
dp->user = NULL;
}
--
2.34.1