All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net-next 00/12] net: phy: Introduce MII multiplexers
@ 2026-12-04 11:06 Maxime Chevallier
  2026-12-04 11:06 ` [PATCH net-next 01/12] dt-bindings: net: Add Ethernet " Maxime Chevallier
                   ` (11 more replies)
  0 siblings, 12 replies; 31+ messages in thread
From: Maxime Chevallier @ 2026-12-04 11:06 UTC (permalink / raw)
  To: davem, Andrew Lunn, Jakub Kicinski, Eric Dumazet,
	Willem de Bruijn, Paolo Abeni, Russell King, Marek Behún,
	Sebastian Hesselbarth, Gregory Clement, Heiner Kallweit
  Cc: Maxime Chevallier, netdev, linux-kernel, linux-arm-kernel,
	thomas.petazzoni, devicetree, Conor Dooley, Krzysztof Kozlowski,
	Rob Herring, Christophe Leroy, Herve Codina, Florian Fainelli,
	Vladimir Oltean, Köry Maincent, Oleksij Rempel,
	Nicolò Veronese, Simon Horman, marcin.s.wojtas, Daniel Golle,
	Dimitri Fedrau, Frank Wunderlich, Pietro Ameruoso,
	Aleksei Sviridkin

Hi everyone,

3 years after pitching the idea at Netdevconf Vancouver, this is V1 of a
framework to deal with multiplexed MII interfaces.

 - What we want to do:

The end-goal is to support multi-port ethernet interfaces. There are
multiple use-cases for that, the most common one is combo-port:

        /-- RJ45
MAC ---X
        \-- SFP

To achieve this, we can use several HW designs:

1 : Use a physical switch on the MII path:

           PHY
    xMII  /
MAC --- Mux
          \
	   SFP

This is what we find on Turris Omnia for instance. Here, the switch is
driven directly by the onboard electronics, it's connected to the SFP's
mod_def0 signal, and we can't switch it at all from Linux.

2 : Use a multiplexer in the SoC:

         /-- xMII -- PHY
MAC -- MUX
         \-- xMII -- SFP

This is what we have on Banana Pi R4 I believe.

3 : Use a PHY that contains a Mux:

    xMII   /---SFP
MAC ---- PHY
           \-- RJ45

This is what we have on Machhiatobin for example.

- What we do here:

To make this "digestable" (well, it's already 12 patches, here goes my
netdev budget), this series adds the easiest case: The first one.

This allows adding the core mux infrastructure, the bindings, a small
driver and demonstrating that on upstream supported HW. Because the HW
does the switching, this series does NOT include the logic to drive a
mux from userspace. This will come later.

With this series, we can now use both WAN ports on omnia :)

 - What we don't do here, but will do next:

Next step is to add some uAPI path to drive that, this will be done with
the phy_port API. end users don't care about the HW design, they want to
select the port to use, and phy_port represents them already.

The example for that will be mcbin.

Finally, we'll add driveable MII muxes (for bpi r4 and others).

This was tested on Omnia.

Note that patch 12 should likely go through the mvebu tree.

Thanks,

Maxime



Maxime Chevallier (12):
  dt-bindings: net: Add Ethernet MII multiplexers
  net: phy: Introduce phy_mux, a MII multiplexer representation
  net: phy: Don't create the sysfs links for PHYs behind a mux
  net: phy: phy_mux: Allow attaching a mux to a net_device
  net: phy: phy_mux: Add port selection logic
  net: phy: phy_mux: Support SFP cage ports
  net: phylink: Factor out phylink_clear_phydev()
  net: phy: phy_mux: Add phy_mux_set_mac_managed_pm()
  net: phylink: Support PHYs reached through a phy_mux
  net: phylink: Support an SFP cage behind a phy_mux
  net: phy: mux: Add the mii-mux-auto driver
  ARM: dts: turris-omnia: Describe the WAN port multiplexer

 .../bindings/net/ethernet-controller.yaml     |   6 +
 .../devicetree/bindings/net/mii-mux-auto.yaml |  89 +++
 .../devicetree/bindings/net/mii-mux.yaml      |  74 ++
 MAINTAINERS                                   |   9 +
 .../dts/marvell/armada-385-turris-omnia.dts   |  29 +-
 drivers/net/phy/Kconfig                       |  10 +
 drivers/net/phy/Makefile                      |   2 +
 drivers/net/phy/mux/Kconfig                   |  18 +
 drivers/net/phy/mux/Makefile                  |   3 +
 drivers/net/phy/mux/mii_mux_auto.c            |  99 +++
 drivers/net/phy/phy_device.c                  |   3 +-
 drivers/net/phy/phy_mux.c                     | 698 ++++++++++++++++++
 drivers/net/phy/phylink.c                     | 264 ++++++-
 include/linux/phy.h                           |   3 +
 include/linux/phy_mux.h                       | 147 ++++
 15 files changed, 1423 insertions(+), 31 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/net/mii-mux-auto.yaml
 create mode 100644 Documentation/devicetree/bindings/net/mii-mux.yaml
 create mode 100644 drivers/net/phy/mux/Kconfig
 create mode 100644 drivers/net/phy/mux/Makefile
 create mode 100644 drivers/net/phy/mux/mii_mux_auto.c
 create mode 100644 drivers/net/phy/phy_mux.c
 create mode 100644 include/linux/phy_mux.h

-- 
2.56.1


^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH net-next 01/12] dt-bindings: net: Add Ethernet MII multiplexers
  2026-12-04 11:06 [PATCH net-next 00/12] net: phy: Introduce MII multiplexers Maxime Chevallier
@ 2026-12-04 11:06 ` Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  2026-12-04 11:06 ` [PATCH net-next 02/12] net: phy: Introduce phy_mux, a MII multiplexer representation Maxime Chevallier
                   ` (10 subsequent siblings)
  11 siblings, 2 replies; 31+ messages in thread
From: Maxime Chevallier @ 2026-12-04 11:06 UTC (permalink / raw)
  To: davem, Andrew Lunn, Jakub Kicinski, Eric Dumazet,
	Willem de Bruijn, Paolo Abeni, Russell King, Marek Behún,
	Sebastian Hesselbarth, Gregory Clement, Heiner Kallweit
  Cc: Maxime Chevallier, netdev, linux-kernel, linux-arm-kernel,
	thomas.petazzoni, devicetree, Conor Dooley, Krzysztof Kozlowski,
	Rob Herring, Christophe Leroy, Herve Codina, Florian Fainelli,
	Vladimir Oltean, Köry Maincent, Oleksij Rempel,
	Nicolò Veronese, Simon Horman, marcin.s.wojtas, Daniel Golle,
	Dimitri Fedrau, Frank Wunderlich, Pietro Ameruoso,
	Aleksei Sviridkin

It's not unusual for network equipment to have some flavors of "combo
ports", where multiple physical connectors are available for one single
network interface.

Usually it's for SFP and RJ45 ports and in such a case, only one port
can be used at any point in time.

There are numerous ways to implement this in hardware. It can be a
gpio-driven multiplexer, a small glue in the SoC, inside an ethernet
PHY, etc.

The simplest form of that multiplexer lets the hardware select the
port in use, without any software way to control it. This is the case
on the Turris Omnia, where the mux is controlled by a GPIO directly
connected to the SFP cage's mod_def0 signal. When a module is inserted,
the mux automatically switches to the SFP cage.

Introduce a binding for MII muxes, for now only for the automatic
muxes. The Ethernet controller has a mii-mux-handle property, pointing
to the new mux node.

This node has a compatible, as it may use dedicated drivers to control
it, and lists all the ports of the mux.

Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 .../bindings/net/ethernet-controller.yaml     |  6 ++
 .../devicetree/bindings/net/mii-mux-auto.yaml | 89 +++++++++++++++++++
 .../devicetree/bindings/net/mii-mux.yaml      | 74 +++++++++++++++
 MAINTAINERS                                   |  6 ++
 4 files changed, 175 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/mii-mux-auto.yaml
 create mode 100644 Documentation/devicetree/bindings/net/mii-mux.yaml

diff --git a/Documentation/devicetree/bindings/net/ethernet-controller.yaml b/Documentation/devicetree/bindings/net/ethernet-controller.yaml
index 30b2a335116e..e2836351cd7a 100644
--- a/Documentation/devicetree/bindings/net/ethernet-controller.yaml
+++ b/Documentation/devicetree/bindings/net/ethernet-controller.yaml
@@ -100,6 +100,12 @@ properties:
     description:
       Specifies a reference to a node representing a PHY device.
 
+  mii-mux-handle:
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description:
+      Specifies a reference to a node representing an Ethernet MII
+      multiplexer.
+
   phy:
     $ref: "#/properties/phy-handle"
     deprecated: true
diff --git a/Documentation/devicetree/bindings/net/mii-mux-auto.yaml b/Documentation/devicetree/bindings/net/mii-mux-auto.yaml
new file mode 100644
index 000000000000..7a965f6e8dcc
--- /dev/null
+++ b/Documentation/devicetree/bindings/net/mii-mux-auto.yaml
@@ -0,0 +1,89 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/net/mii-mux-auto.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Ethernet MII multiplexer switched by the hardware
+
+maintainers:
+  - Maxime Chevallier <maxime.chevallier@bootlin.com>
+
+description:
+  A two-port Ethernet MII multiplexer that the hardware switches by itself,
+  without any software control.
+
+allOf:
+  - $ref: mii-mux.yaml#
+
+properties:
+  compatible:
+    const: mii-mux-auto
+
+patternProperties:
+  "^port@[0-9a-f]+$":
+    $ref: mii-mux.yaml#/$defs/port
+    unevaluatedProperties: false
+
+    properties:
+      takes-precedence:
+        type: boolean
+        description:
+          The multiplexer switches to this port by itself as soon as the
+          port gets active, whatever the state of the other port.
+
+required:
+  - compatible
+  - port@0
+  - port@1
+
+oneOf:
+  - properties:
+      port@0:
+        required:
+          - takes-precedence
+  - properties:
+      port@1:
+        required:
+          - takes-precedence
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    ethernet {
+        phy-mode = "sgmii";
+        mii-mux-handle = <&mii_mux>;
+
+        mdio {
+            #address-cells = <1>;
+            #size-cells = <0>;
+
+            phy0: ethernet-phy@0 {
+                reg = <0>;
+            };
+        };
+    };
+
+    mii_mux: mii-mux {
+        compatible = "mii-mux-auto";
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        port@0 {
+            reg = <0>;
+            sfp = <&sfp0>;
+            takes-precedence;
+        };
+
+        port@1 {
+            reg = <1>;
+            phy-handle = <&phy0>;
+            phy-mode = "sgmii";
+        };
+    };
+
+    sfp0: sfp {
+        compatible = "sff,sfp";
+        i2c-bus = <&i2c0>;
+    };
diff --git a/Documentation/devicetree/bindings/net/mii-mux.yaml b/Documentation/devicetree/bindings/net/mii-mux.yaml
new file mode 100644
index 000000000000..cbd99251f460
--- /dev/null
+++ b/Documentation/devicetree/bindings/net/mii-mux.yaml
@@ -0,0 +1,74 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/net/mii-mux.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Ethernet MII multiplexer
+
+maintainers:
+  - Maxime Chevallier <maxime.chevallier@bootlin.com>
+
+description: |
+  An Ethernet MII multiplexer sits between one Ethernet MAC and several
+  Ethernet PHYs or SFP cages that share the MAC's MII, only one of them
+  being usable at a time.
+
+select: false
+
+properties:
+  "#address-cells":
+    const: 1
+
+  "#size-cells":
+    const: 0
+
+patternProperties:
+  "^port@[0-9a-f]+$":
+    $ref: "#/$defs/port"
+
+required:
+  - "#address-cells"
+  - "#size-cells"
+
+additionalProperties: true
+
+$defs:
+  port:
+    type: object
+
+    properties:
+      reg:
+        maxItems: 1
+        description:
+          The port number.
+
+      phy-handle:
+        $ref: /schemas/types.yaml#/definitions/phandle
+        description:
+          The Ethernet PHY reached through this port.
+
+      phy-mode:
+        $ref: ethernet-controller.yaml#/properties/phy-connection-type
+        description:
+          The MII mode used between the MAC and this PHY.
+
+      sfp:
+        $ref: /schemas/types.yaml#/definitions/phandle
+        description:
+          The SFP cage reached through this port.
+
+    required:
+      - reg
+
+    oneOf:
+      - required:
+          - phy-handle
+          - phy-mode
+        properties:
+          sfp: false
+      - required:
+          - sfp
+        properties:
+          phy-handle: false
+          phy-mode: false
diff --git a/MAINTAINERS b/MAINTAINERS
index bfa5a7248973..8685ed44840a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10081,6 +10081,12 @@ S:	Maintained
 F:	rust/kernel/net/phy.rs
 F:	rust/kernel/net/phy/reg.rs
 
+ETHERNET PHY MUX
+M:	Maxime Chevallier <maxime.chevallier@bootlin.com>
+L:	netdev@vger.kernel.org
+S:	Maintained
+F:	Documentation/devicetree/bindings/net/mii-mux*.yaml
+
 EXEC & BINFMT API, ELF
 M:	Kees Cook <kees@kernel.org>
 L:	linux-mm@kvack.org
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH net-next 02/12] net: phy: Introduce phy_mux, a MII multiplexer representation
  2026-12-04 11:06 [PATCH net-next 00/12] net: phy: Introduce MII multiplexers Maxime Chevallier
  2026-12-04 11:06 ` [PATCH net-next 01/12] dt-bindings: net: Add Ethernet " Maxime Chevallier
@ 2026-12-04 11:06 ` Maxime Chevallier
  2026-12-04 11:06 ` [PATCH net-next 03/12] net: phy: Don't create the sysfs links for PHYs behind a mux Maxime Chevallier
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 31+ messages in thread
From: Maxime Chevallier @ 2026-12-04 11:06 UTC (permalink / raw)
  To: davem, Andrew Lunn, Jakub Kicinski, Eric Dumazet,
	Willem de Bruijn, Paolo Abeni, Russell King, Marek Behún,
	Sebastian Hesselbarth, Gregory Clement, Heiner Kallweit
  Cc: Maxime Chevallier, netdev, linux-kernel, linux-arm-kernel,
	thomas.petazzoni, devicetree, Conor Dooley, Krzysztof Kozlowski,
	Rob Herring, Christophe Leroy, Herve Codina, Florian Fainelli,
	Vladimir Oltean, Köry Maincent, Oleksij Rempel,
	Nicolò Veronese, Simon Horman, marcin.s.wojtas, Daniel Golle,
	Dimitri Fedrau, Frank Wunderlich, Pietro Ameruoso,
	Aleksei Sviridkin

The implementation of combo-port and multi-PHY interfaces can be done
either by having the MAC's MII interface muxed to 2 different
destinations, or by having a PHY with 2 outputs.

Let's introduce a framework to deal with the first case: muxing the MII
bus. Here again, we can mux it with different techniques:

 - A Mux block in the SoC, after the MAC, MMIO-mapped like we have on
   banana Pi R4 for example

 - A gpio-driven Mux, can be some high speed analog switch for example.
   Here again, we have 2 options:
    - The switch can be controlled by Linux
    - The switch is controlled through another means, that's the case
      for Turris Omnia for example, the control pin is tied to the SFP
      cage's mod_def0 pin that indicates if a module is present

 - No mux at all, the mux is just the MII going to 2 PHYs, one of them
   needing to isolate its MII.

The plan is to support all these cases, with the uAPI interface being
phy_port as it'll also allow supporting phy-driven muxes.

Introduce the framework for the muxes, named phy_mux, for now without
users, they'll be added in the next commits.

The mux is represented in DT, and is parsed using the fwnode logic.

A mux has multiple "ports" represented by "struct phy_mux_port", that
correspond to the PHY-side MII busses, only one of these can be active
at any given time.

Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 MAINTAINERS               |   2 +
 drivers/net/phy/Kconfig   |   8 ++
 drivers/net/phy/Makefile  |   1 +
 drivers/net/phy/phy_mux.c | 189 ++++++++++++++++++++++++++++++++++++++
 include/linux/phy_mux.h   |  44 +++++++++
 5 files changed, 244 insertions(+)
 create mode 100644 drivers/net/phy/phy_mux.c
 create mode 100644 include/linux/phy_mux.h

diff --git a/MAINTAINERS b/MAINTAINERS
index 8685ed44840a..b2b02a1dbd3a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10086,6 +10086,8 @@ M:	Maxime Chevallier <maxime.chevallier@bootlin.com>
 L:	netdev@vger.kernel.org
 S:	Maintained
 F:	Documentation/devicetree/bindings/net/mii-mux*.yaml
+F:	drivers/net/phy/phy_mux.c
+F:	include/linux/phy_mux.h
 
 EXEC & BINFMT API, ELF
 M:	Kees Cook <kees@kernel.org>
diff --git a/drivers/net/phy/Kconfig b/drivers/net/phy/Kconfig
index 30e9d22c8d1a..d8a0131d2aa2 100644
--- a/drivers/net/phy/Kconfig
+++ b/drivers/net/phy/Kconfig
@@ -21,6 +21,14 @@ menuconfig PHYLIB
 
 if PHYLIB
 
+config PHY_MUX
+	bool "PHY multiplexing support"
+	help
+	  Some boards route several PHYs or SFP cages to the same MAC
+	  through a multiplexer, only one of them being usable at a
+	  time. This adds the infrastructure to represent such multiplexers
+	  and to select which port carries the link.
+
 config SWPHY
 	bool
 
diff --git a/drivers/net/phy/Makefile b/drivers/net/phy/Makefile
index 579066a638f0..b3ec2933fec4 100644
--- a/drivers/net/phy/Makefile
+++ b/drivers/net/phy/Makefile
@@ -5,6 +5,7 @@ libphy-y			:= phy.o phy-c45.o phy-core.o phy_device.o \
 				   linkmode.o phy_link_topology.o \
 				   phy_caps.o mdio_bus_provider.o phy_port.o \
 				   mdio_device.o
+libphy-$(CONFIG_PHY_MUX)	+= phy_mux.o
 
 ifdef CONFIG_PHYLIB
 # built-in whenever PHYLIB is built-in or module
diff --git a/drivers/net/phy/phy_mux.c b/drivers/net/phy/phy_mux.c
new file mode 100644
index 000000000000..e259eb4e271a
--- /dev/null
+++ b/drivers/net/phy/phy_mux.c
@@ -0,0 +1,189 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+#include <linux/device.h>
+#include <linux/list.h>
+#include <linux/mutex.h>
+#include <linux/phy.h>
+#include <linux/phy_mux.h>
+#include <linux/property.h>
+#include <linux/slab.h>
+#include <linux/xarray.h>
+
+static LIST_HEAD(phy_muxes);
+static DEFINE_MUTEX(phy_mux_mutex);
+
+struct phy_mux {
+	struct list_head node;
+	struct device *dev;
+
+	struct xarray ports;
+	int n_ports;
+	int max_n_ports;
+
+};
+
+/**
+ * phy_mux_alloc() - Allocate a mux
+ * @dev: The mux device
+ * @max_n_ports: How many ports the mux accepts
+ *
+ * The mux is freed with phy_mux_free().
+ *
+ * Returns: the mux, or an ERR_PTR.
+ */
+struct phy_mux *phy_mux_alloc(struct device *dev, int max_n_ports)
+{
+	struct phy_mux *mux;
+
+	mux = kzalloc_obj(*mux);
+	if (!mux)
+		return ERR_PTR(-ENOMEM);
+
+	xa_init_flags(&mux->ports, XA_FLAGS_ALLOC1);
+
+	mux->dev = dev;
+	mux->max_n_ports = max_n_ports;
+
+	return mux;
+}
+EXPORT_SYMBOL_GPL(phy_mux_alloc);
+
+static void phy_mux_port_destroy(struct phy_mux_port *mux_port)
+{
+	switch (mux_port->type) {
+	case PHY_MUX_PORT_PHY:
+		fwnode_handle_put(mux_port->fwnode);
+		break;
+	}
+
+	kfree(mux_port);
+}
+
+/**
+ * phy_mux_register_port() - Add a port to a mux
+ * @mux: The mux, not registered yet
+ * @port: The port, allocated with kzalloc(). The mux owns it from then on
+ *	  and frees it in phy_mux_free().
+ *
+ * Returns: 0 on success, -EBUSY if the mux is full, or a negative error.
+ */
+static int phy_mux_register_port(struct phy_mux *mux,
+				 struct phy_mux_port *port)
+{
+	int ret;
+
+	if (mux->n_ports >= mux->max_n_ports)
+		return -EBUSY;
+
+	ret = xa_alloc(&mux->ports, &port->id, port, xa_limit_16b, GFP_KERNEL);
+	if (ret)
+		return ret;
+
+	mux->n_ports++;
+
+	return 0;
+}
+
+/**
+ * phy_mux_fwnode_add_ports() - Create the ports described under a fwnode
+ * @mux: The mux
+ * @fwnode: The mux device's fwnode, each child being a port with a
+ *	    phy-handle and a phy-mode
+ *
+ * The PHYs themselves are only looked up when the mux gets attached.
+ *
+ * Returns: 0 on success or a negative error.
+ */
+int phy_mux_fwnode_add_ports(struct phy_mux *mux,
+			     const struct fwnode_handle *fwnode)
+{
+	struct fwnode_handle *child, *phy_fwnode;
+	struct phy_mux_port *port;
+	int ret;
+
+	fwnode_for_each_available_child_node(fwnode, child) {
+		phy_fwnode = fwnode_get_phy_node(child);
+		if (IS_ERR(phy_fwnode)) {
+			ret = PTR_ERR(phy_fwnode);
+			goto err;
+		}
+
+		port = kzalloc_obj(*port);
+		if (!port) {
+			fwnode_handle_put(phy_fwnode);
+			ret = -ENOMEM;
+			goto err;
+		}
+
+		port->type = PHY_MUX_PORT_PHY;
+		port->fwnode = phy_fwnode;
+		port->mux = mux;
+
+		ret = fwnode_get_phy_mode(child);
+		if (ret < 0) {
+			phy_mux_port_destroy(port);
+			goto err;
+		}
+		port->interface = ret;
+
+		ret = phy_mux_register_port(mux, port);
+		if (ret) {
+			phy_mux_port_destroy(port);
+			goto err;
+		}
+	}
+
+	return 0;
+
+err:
+	fwnode_handle_put(child);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(phy_mux_fwnode_add_ports);
+
+/**
+ * phy_mux_register() - Make the mux available to its parent
+ * @mux: The mux, with all its ports added
+ *
+ * Returns: 0 on success or a negative error.
+ */
+int phy_mux_register(struct phy_mux *mux)
+{
+	mutex_lock(&phy_mux_mutex);
+	list_add(&mux->node, &phy_muxes);
+	mutex_unlock(&phy_mux_mutex);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(phy_mux_register);
+
+/**
+ * phy_mux_unregister() - Make the mux unavailable to its parent
+ * @mux: The mux, registered
+ *
+ * The mux still has to be freed with phy_mux_free().
+ */
+void phy_mux_unregister(struct phy_mux *mux)
+{
+	mutex_lock(&phy_mux_mutex);
+	list_del(&mux->node);
+	mutex_unlock(&phy_mux_mutex);
+}
+EXPORT_SYMBOL_GPL(phy_mux_unregister);
+
+/**
+ * phy_mux_free() - Free a mux and its ports
+ * @mux: The mux, not registered
+ */
+void phy_mux_free(struct phy_mux *mux)
+{
+	struct phy_mux_port *port;
+	unsigned long id;
+
+	xa_for_each(&mux->ports, id, port)
+		phy_mux_port_destroy(port);
+
+	xa_destroy(&mux->ports);
+	kfree(mux);
+}
+EXPORT_SYMBOL_GPL(phy_mux_free);
diff --git a/include/linux/phy_mux.h b/include/linux/phy_mux.h
new file mode 100644
index 000000000000..3dde6dd57faf
--- /dev/null
+++ b/include/linux/phy_mux.h
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef __PHY_MUX_H
+#define __PHY_MUX_H
+
+#include <linux/phy.h>
+
+struct device;
+struct fwnode_handle;
+struct phy_mux;
+struct phy_mux_port;
+
+enum phy_mux_port_type {
+	PHY_MUX_PORT_PHY,
+};
+
+/**
+ * struct phy_mux_port - One of the multiplexed channels of a MII multiplexer
+ * @id: Identifier of the port, unique within the mux
+ * @mux: The mux this port belongs to
+ * @type: What sits behind that port
+ * @phydev: For PHY_MUX_PORT_PHY, the PHY device behind the port, while the
+ *	    mux is attached
+ * @fwnode: For PHY_MUX_PORT_PHY, the firmware node of that PHY
+ * @interface: The MII mode the link runs in when this port is selected
+ */
+struct phy_mux_port {
+	u32 id;
+	struct phy_mux *mux;
+	enum phy_mux_port_type type;
+	struct phy_device *phydev;
+	struct fwnode_handle *fwnode;
+
+	phy_interface_t interface;
+};
+
+struct phy_mux *phy_mux_alloc(struct device *dev, int max_n_ports);
+int phy_mux_fwnode_add_ports(struct phy_mux *mux,
+			     const struct fwnode_handle *fwnode);
+int phy_mux_register(struct phy_mux *mux);
+void phy_mux_unregister(struct phy_mux *mux);
+void phy_mux_free(struct phy_mux *mux);
+
+#endif /* __PHY_MUX_H */
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH net-next 03/12] net: phy: Don't create the sysfs links for PHYs behind a mux
  2026-12-04 11:06 [PATCH net-next 00/12] net: phy: Introduce MII multiplexers Maxime Chevallier
  2026-12-04 11:06 ` [PATCH net-next 01/12] dt-bindings: net: Add Ethernet " Maxime Chevallier
  2026-12-04 11:06 ` [PATCH net-next 02/12] net: phy: Introduce phy_mux, a MII multiplexer representation Maxime Chevallier
@ 2026-12-04 11:06 ` Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  2026-12-04 11:06 ` [PATCH net-next 04/12] net: phy: phy_mux: Allow attaching a mux to a net_device Maxime Chevallier
                   ` (8 subsequent siblings)
  11 siblings, 2 replies; 31+ messages in thread
From: Maxime Chevallier @ 2026-12-04 11:06 UTC (permalink / raw)
  To: davem, Andrew Lunn, Jakub Kicinski, Eric Dumazet,
	Willem de Bruijn, Paolo Abeni, Russell King, Marek Behún,
	Sebastian Hesselbarth, Gregory Clement, Heiner Kallweit
  Cc: Maxime Chevallier, netdev, linux-kernel, linux-arm-kernel,
	thomas.petazzoni, devicetree, Conor Dooley, Krzysztof Kozlowski,
	Rob Herring, Christophe Leroy, Herve Codina, Florian Fainelli,
	Vladimir Oltean, Köry Maincent, Oleksij Rempel,
	Nicolò Veronese, Simon Horman, marcin.s.wojtas, Daniel Golle,
	Dimitri Fedrau, Frank Wunderlich, Pietro Ameruoso,
	Aleksei Sviridkin

The phy_mux framework allows attaching multiple PHYs to the same netdev,
one of them being actively used at any given time.

Attached phys create sysfs links in the netdev entry, pointing to the
PHY. We can't have two of them (otherwise they'd overwrite one another),
so let's just not create these. PHYs are still visible through the
phy_link_topology API.

Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 drivers/net/phy/phy_device.c | 3 ++-
 include/linux/phy.h          | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 60440b62e6c7..9630578a6a5c 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -2019,7 +2019,8 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
 	 */
 	phydev->sysfs_links = false;
 
-	phy_sysfs_create_links(phydev);
+	if (!phydev->mux_port)
+		phy_sysfs_create_links(phydev);
 
 	if (!phydev->attached_dev) {
 		err = sysfs_create_file(&phydev->mdio.dev.kobj,
diff --git a/include/linux/phy.h b/include/linux/phy.h
index f1b1ad1dd82f..a6cd2fc642ad 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -328,6 +328,7 @@ static inline long rgmii_clock(int speed)
 struct device;
 struct kernel_hwtstamp_config;
 struct phylink;
+struct phy_mux_port;
 struct phy_port;
 struct sfp_bus;
 struct sfp_upstream_ops;
@@ -618,6 +619,7 @@ struct phy_oatc14_sqi_capability {
  * @sfp_cage_port: The phy_port connected to the downstream SFP cage
  * @mod_port: phy_port representing the SFP module, if it is phy-less
  * @upstream_port: phy_port this PHY's MII attaches to, if any
+ * @mux_port: The mux port this PHY sits behind, if any
  * @attached_dev: The attached enet driver's device instance ptr
  * @adjust_link: Callback for the enet controller to respond to changes: in the
  *               link state.
@@ -814,6 +816,7 @@ struct phy_device {
 	struct phy_port *sfp_cage_port;
 	struct phy_port *mod_port;
 	struct phy_port *upstream_port;
+	struct phy_mux_port *mux_port;
 	struct phylink *phylink;
 	struct net_device *attached_dev;
 	struct mii_timestamper *mii_ts;
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH net-next 04/12] net: phy: phy_mux: Allow attaching a mux to a net_device
  2026-12-04 11:06 [PATCH net-next 00/12] net: phy: Introduce MII multiplexers Maxime Chevallier
                   ` (2 preceding siblings ...)
  2026-12-04 11:06 ` [PATCH net-next 03/12] net: phy: Don't create the sysfs links for PHYs behind a mux Maxime Chevallier
@ 2026-12-04 11:06 ` Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  2026-12-04 11:06 ` [PATCH net-next 05/12] net: phy: phy_mux: Add port selection logic Maxime Chevallier
                   ` (7 subsequent siblings)
  11 siblings, 2 replies; 31+ messages in thread
From: Maxime Chevallier @ 2026-12-04 11:06 UTC (permalink / raw)
  To: davem, Andrew Lunn, Jakub Kicinski, Eric Dumazet,
	Willem de Bruijn, Paolo Abeni, Russell King, Marek Behún,
	Sebastian Hesselbarth, Gregory Clement, Heiner Kallweit
  Cc: Maxime Chevallier, netdev, linux-kernel, linux-arm-kernel,
	thomas.petazzoni, devicetree, Conor Dooley, Krzysztof Kozlowski,
	Rob Herring, Christophe Leroy, Herve Codina, Florian Fainelli,
	Vladimir Oltean, Köry Maincent, Oleksij Rempel,
	Nicolò Veronese, Simon Horman, marcin.s.wojtas, Daniel Golle,
	Dimitri Fedrau, Frank Wunderlich, Pietro Ameruoso,
	Aleksei Sviridkin

Like any other MII link, muxed MII's need all the involved components
(MAC, PCS, PHY, etc) to be involved for everything to work smoothly.

When switching from one port to another, we need to re-construct the
link and set all parameters, as both ports may be using different kinds
of technologies (think SGMII PHY on one side, 1000BaseX SFP on the
other).

Setting-up a mux therefore requires attaching the mux to its parent
(i.e. the MAC), and let its parent register callbacks to be notified
when switching occurs:

      /--- parent_ops
      |
      v     +-----+
+-----+     |     |------ PHY
| MAC | ----| MUX |
+-----+     |     |------ SFP
            +-----+

In practice, these parent_ops will be implemented by phylink.

The mux needs the netdev reference too as it's required when adding the
devlink dependency.

All PHYs in the mux are attached to the netdev, meaning we end-up with
more than one PHY attached to the netdev. We override the link
notification callback from a mux provided one.

Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 drivers/net/phy/phy_mux.c | 196 +++++++++++++++++++++++++++++++++++++-
 include/linux/phy_mux.h   |  39 ++++++++
 2 files changed, 232 insertions(+), 3 deletions(-)

diff --git a/drivers/net/phy/phy_mux.c b/drivers/net/phy/phy_mux.c
index e259eb4e271a..691bca09e0af 100644
--- a/drivers/net/phy/phy_mux.c
+++ b/drivers/net/phy/phy_mux.c
@@ -3,9 +3,11 @@
 #include <linux/device.h>
 #include <linux/list.h>
 #include <linux/mutex.h>
+#include <linux/netdevice.h>
 #include <linux/phy.h>
 #include <linux/phy_mux.h>
 #include <linux/property.h>
+#include <linux/rtnetlink.h>
 #include <linux/slab.h>
 #include <linux/xarray.h>
 
@@ -16,12 +18,196 @@ struct phy_mux {
 	struct list_head node;
 	struct device *dev;
 
+	struct net_device *netdev;
+	struct device_link *devlink;
+	const struct phy_mux_parent_ops *pops;
+	void *parent_priv;
+
 	struct xarray ports;
 	int n_ports;
 	int max_n_ports;
-
 };
 
+static void phy_mux_phy_link_change(struct phy_device *phydev, bool up)
+{
+}
+
+static int phy_mux_port_bind_phy(struct phy_mux_port *mux_port)
+{
+	struct phy_device *phydev;
+
+	phydev = fwnode_phy_find_device(mux_port->fwnode);
+	if (!phydev)
+		return -EPROBE_DEFER;
+
+	if (phydev->mux_port) {
+		put_device(&phydev->mdio.dev);
+		return -EBUSY;
+	}
+
+	mux_port->phydev = phydev;
+	phydev->mux_port = mux_port;
+
+	return 0;
+}
+
+static void phy_mux_port_unbind_phy(struct phy_mux_port *mux_port)
+{
+	struct phy_device *phydev = mux_port->phydev;
+
+	if (!phydev)
+		return;
+
+	phydev->mux_port = NULL;
+	mux_port->phydev = NULL;
+
+	put_device(&phydev->mdio.dev);
+}
+
+static void phy_mux_detach_phys(struct phy_mux *mux)
+{
+	struct phy_mux_port *port;
+	unsigned long id;
+
+	xa_for_each(&mux->ports, id, port) {
+		if (!port->attached)
+			continue;
+
+		phy_detach(port->phydev);
+		phy_mux_port_unbind_phy(port);
+		port->attached = false;
+	}
+}
+
+/**
+ * phy_mux_attach() - Attach the mux to its parent
+ * @mux: The mux
+ * @dev: The net_device the mux leads to
+ * @phy_flags: PHY device flags, for the PHYs sitting behind the mux
+ * @pops: The parent's callbacks
+ * @priv: Passed back to @pops
+ *
+ * All the PHYs behind the mux are looked up and attached to @dev, and stay
+ * attached until phy_mux_detach(). The parent only gets to use the one that
+ * is selected.
+ *
+ * Returns: 0 on success, -EBUSY if already attached, -EPROBE_DEFER if a PHY
+ * isn't there yet, or a negative error.
+ */
+static int phy_mux_attach(struct phy_mux *mux, struct net_device *dev,
+			  u32 phy_flags,
+			  const struct phy_mux_parent_ops *pops, void *priv)
+{
+	struct phy_mux_port *port;
+	bool has_phys = false;
+	unsigned long id;
+	int ret;
+
+	if (mux->netdev)
+		return -EBUSY;
+
+	xa_for_each(&mux->ports, id, port) {
+		if (port->type != PHY_MUX_PORT_PHY)
+			continue;
+
+		ret = phy_mux_port_bind_phy(port);
+		if (ret)
+			goto err_detach;
+
+		ret = phy_attach_direct(dev, port->phydev, phy_flags,
+					port->interface);
+		if (ret) {
+			phy_mux_port_unbind_phy(port);
+			goto err_detach;
+		}
+
+		port->phydev->phy_link_change = phy_mux_phy_link_change;
+		port->attached = true;
+		has_phys = true;
+	}
+
+	if (has_phys)
+		dev->phydev = NULL;
+
+	if (dev->dev.parent)
+		mux->devlink = device_link_add(dev->dev.parent, mux->dev,
+					       DL_FLAG_STATELESS);
+
+	mux->pops = pops;
+	mux->parent_priv = priv;
+	mux->netdev = dev;
+
+	if (pops && pops->attach)
+		pops->attach(mux, priv);
+
+	return 0;
+
+err_detach:
+	phy_mux_detach_phys(mux);
+
+	return ret;
+}
+
+/**
+ * fwnode_phy_mux_attach() - Find a mux from its firmware node and attach it
+ * @fwnode: The mux device's fwnode
+ * @dev: The net_device the mux leads to
+ * @phy_flags: PHY device flags, for the PHYs sitting behind the mux
+ * @pops: The parent's callbacks
+ * @priv: Passed back to @pops
+ *
+ * The parent gets the mux through its attach callback, called before the
+ * mux can be unregistered.
+ *
+ * Returns: 0 on success, -EPROBE_DEFER if no mux is registered yet for that
+ * node, or a negative error.
+ */
+int
+fwnode_phy_mux_attach(const struct fwnode_handle *fwnode,
+		      struct net_device *dev, u32 phy_flags,
+		      const struct phy_mux_parent_ops *pops, void *priv)
+{
+	int ret = -EPROBE_DEFER;
+	struct phy_mux *mux;
+
+	mutex_lock(&phy_mux_mutex);
+	list_for_each_entry(mux, &phy_muxes, node) {
+		if (dev_fwnode(mux->dev) != fwnode)
+			continue;
+
+		ret = phy_mux_attach(mux, dev, phy_flags, pops, priv);
+		break;
+	}
+	mutex_unlock(&phy_mux_mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(fwnode_phy_mux_attach);
+
+/**
+ * phy_mux_detach() - Detach the mux from its parent
+ * @mux: The mux
+ *
+ * The PHYs behind the mux are detached.
+ */
+void phy_mux_detach(struct phy_mux *mux)
+{
+	if (!mux->netdev)
+		return;
+
+	phy_mux_detach_phys(mux);
+
+	if (mux->devlink) {
+		device_link_del(mux->devlink);
+		mux->devlink = NULL;
+	}
+
+	mux->pops = NULL;
+	mux->parent_priv = NULL;
+	mux->netdev = NULL;
+}
+EXPORT_SYMBOL_GPL(phy_mux_detach);
+
 /**
  * phy_mux_alloc() - Allocate a mux
  * @dev: The mux device
@@ -52,6 +238,7 @@ static void phy_mux_port_destroy(struct phy_mux_port *mux_port)
 {
 	switch (mux_port->type) {
 	case PHY_MUX_PORT_PHY:
+		phy_mux_port_unbind_phy(mux_port);
 		fwnode_handle_put(mux_port->fwnode);
 		break;
 	}
@@ -160,14 +347,17 @@ EXPORT_SYMBOL_GPL(phy_mux_register);
 /**
  * phy_mux_unregister() - Make the mux unavailable to its parent
  * @mux: The mux, registered
- *
- * The mux still has to be freed with phy_mux_free().
  */
 void phy_mux_unregister(struct phy_mux *mux)
 {
 	mutex_lock(&phy_mux_mutex);
 	list_del(&mux->node);
 	mutex_unlock(&phy_mux_mutex);
+
+	rtnl_lock();
+	if (mux->netdev && mux->pops && mux->pops->detach)
+		mux->pops->detach(mux, mux->parent_priv);
+	rtnl_unlock();
 }
 EXPORT_SYMBOL_GPL(phy_mux_unregister);
 
diff --git a/include/linux/phy_mux.h b/include/linux/phy_mux.h
index 3dde6dd57faf..cdc58b56a3b4 100644
--- a/include/linux/phy_mux.h
+++ b/include/linux/phy_mux.h
@@ -7,6 +7,7 @@
 
 struct device;
 struct fwnode_handle;
+struct net_device;
 struct phy_mux;
 struct phy_mux_port;
 
@@ -23,6 +24,7 @@ enum phy_mux_port_type {
  *	    mux is attached
  * @fwnode: For PHY_MUX_PORT_PHY, the firmware node of that PHY
  * @interface: The MII mode the link runs in when this port is selected
+ * @attached: For PHY_MUX_PORT_PHY, the PHY is attached to the mux's netdev
  */
 struct phy_mux_port {
 	u32 id;
@@ -32,8 +34,24 @@ struct phy_mux_port {
 	struct fwnode_handle *fwnode;
 
 	phy_interface_t interface;
+
+	bool attached;
+};
+
+/**
+ * struct phy_mux_parent_ops - Ops for the device providing the single MII
+ *				that's multiplexed, usually the MAC
+ * @attach: The mux is attached to the parent.
+ * @detach: The mux is going away, the parent must detach from it. Called with
+ *	    rtnl held.
+ */
+struct phy_mux_parent_ops {
+	void (*attach)(struct phy_mux *mux, void *priv);
+	void (*detach)(struct phy_mux *mux, void *priv);
 };
 
+#if IS_ENABLED(CONFIG_PHY_MUX)
+
 struct phy_mux *phy_mux_alloc(struct device *dev, int max_n_ports);
 int phy_mux_fwnode_add_ports(struct phy_mux *mux,
 			     const struct fwnode_handle *fwnode);
@@ -41,4 +59,25 @@ int phy_mux_register(struct phy_mux *mux);
 void phy_mux_unregister(struct phy_mux *mux);
 void phy_mux_free(struct phy_mux *mux);
 
+int fwnode_phy_mux_attach(const struct fwnode_handle *fwnode,
+			  struct net_device *dev, u32 phy_flags,
+			  const struct phy_mux_parent_ops *pops, void *priv);
+void phy_mux_detach(struct phy_mux *mux);
+
+#else
+
+static inline int
+fwnode_phy_mux_attach(const struct fwnode_handle *fwnode,
+		      struct net_device *dev, u32 phy_flags,
+		      const struct phy_mux_parent_ops *pops, void *priv)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void phy_mux_detach(struct phy_mux *mux)
+{
+}
+
+#endif /* CONFIG_PHY_MUX */
+
 #endif /* __PHY_MUX_H */
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH net-next 05/12] net: phy: phy_mux: Add port selection logic
  2026-12-04 11:06 [PATCH net-next 00/12] net: phy: Introduce MII multiplexers Maxime Chevallier
                   ` (3 preceding siblings ...)
  2026-12-04 11:06 ` [PATCH net-next 04/12] net: phy: phy_mux: Allow attaching a mux to a net_device Maxime Chevallier
@ 2026-12-04 11:06 ` Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  2026-12-04 11:06 ` [PATCH net-next 06/12] net: phy: phy_mux: Support SFP cage ports Maxime Chevallier
                   ` (6 subsequent siblings)
  11 siblings, 2 replies; 31+ messages in thread
From: Maxime Chevallier @ 2026-12-04 11:06 UTC (permalink / raw)
  To: davem, Andrew Lunn, Jakub Kicinski, Eric Dumazet,
	Willem de Bruijn, Paolo Abeni, Russell King, Marek Behún,
	Sebastian Hesselbarth, Gregory Clement, Heiner Kallweit
  Cc: Maxime Chevallier, netdev, linux-kernel, linux-arm-kernel,
	thomas.petazzoni, devicetree, Conor Dooley, Krzysztof Kozlowski,
	Rob Herring, Christophe Leroy, Herve Codina, Florian Fainelli,
	Vladimir Oltean, Köry Maincent, Oleksij Rempel,
	Nicolò Veronese, Simon Horman, marcin.s.wojtas, Daniel Golle,
	Dimitri Fedrau, Frank Wunderlich, Pietro Ameruoso,
	Aleksei Sviridkin

As Muxes drive multiple MII ports but only one can be used at a time, we
need logic to select which one to use, and notify the parent (i.e. the
netdev).

Selecting a port tells the MAC to start using what's at the end of the
MII line (a PHY, an SFP cage, etc.), so that it can start configuring
its MAC, PCS, Serdes and so on accordingly.

Deselecting it tells the parent to stop using it.

All the ports are deselected when the mux gets attached, and nothing is
selected until the parent starts the mux with phy_mux_start(), which it
is expected to do when the interface is brought up.

The selection policy is as simple as it gets for now, the first port is
selected.

For that, we introduce mux_port_ops:

      /--- parent_ops
      |
      |                    / phy_mux_port_ops
      v     +-----+        v
+-----+     |     |------ PHY
| MAC | ----| MUX |
+-----+     |     |------ SFP
            +-----+        ^
                           \ phy_mux_port_ops

They allow notifying what's at the end of a mux'd MII line (a PHY, an SFP)
that it has been selected or deselected. That way it can act accordingly
to stop trying to reach the MAC.

For now, only deselect is implemented, the rest will come later.

Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 drivers/net/phy/phy_mux.c | 134 +++++++++++++++++++++++++++++++++++++-
 include/linux/phy_mux.h   |  29 +++++++++
 2 files changed, 162 insertions(+), 1 deletion(-)

diff --git a/drivers/net/phy/phy_mux.c b/drivers/net/phy/phy_mux.c
index 691bca09e0af..407269980edc 100644
--- a/drivers/net/phy/phy_mux.c
+++ b/drivers/net/phy/phy_mux.c
@@ -14,6 +14,11 @@
 static LIST_HEAD(phy_muxes);
 static DEFINE_MUTEX(phy_mux_mutex);
 
+enum phy_mux_state {
+	PHY_MUX_STOPPED,
+	PHY_MUX_STARTED,
+};
+
 struct phy_mux {
 	struct list_head node;
 	struct device *dev;
@@ -26,12 +31,97 @@ struct phy_mux {
 	struct xarray ports;
 	int n_ports;
 	int max_n_ports;
+
+	struct phy_mux_port *current_port;
+	enum phy_mux_state state;
 };
 
+static void phy_mux_port_deselect(struct phy_mux_port *port)
+{
+	if (port->ops && port->ops->deselect)
+		port->ops->deselect(port);
+}
+
 static void phy_mux_phy_link_change(struct phy_device *phydev, bool up)
 {
 }
 
+static void phy_mux_port_set_parent_phy(struct phy_mux *mux,
+					struct phy_mux_port *port, bool used)
+{
+	if (port->type != PHY_MUX_PORT_PHY)
+		return;
+
+	if (!used)
+		port->phydev->phy_link_change = phy_mux_phy_link_change;
+
+	if (mux->netdev)
+		mux->netdev->phydev = used ? port->phydev : NULL;
+}
+
+static void __phy_mux_deselect_port(struct phy_mux *mux,
+				    struct phy_mux_port *port)
+{
+	if (mux->pops && mux->pops->deselect) {
+		mux->pops->deselect(mux, port, mux->parent_priv);
+		phy_mux_port_set_parent_phy(mux, port, false);
+	}
+
+	phy_mux_port_deselect(port);
+
+	port->selected = false;
+}
+
+static int __phy_mux_select_port(struct phy_mux *mux,
+				 struct phy_mux_port *port)
+{
+	int ret;
+
+	if (mux->pops && mux->pops->select) {
+		ret = mux->pops->select(mux, port, mux->parent_priv);
+		phy_mux_port_set_parent_phy(mux, port, !ret);
+		if (ret)
+			return ret;
+	}
+
+	port->selected = true;
+
+	return 0;
+}
+
+static int phy_mux_select(struct phy_mux *mux, struct phy_mux_port *port)
+{
+	int ret;
+
+	if (mux->current_port == port)
+		return 0;
+
+	if (mux->current_port) {
+		__phy_mux_deselect_port(mux, mux->current_port);
+		mux->current_port = NULL;
+	}
+
+	if (!port)
+		return 0;
+
+	ret = __phy_mux_select_port(mux, port);
+	if (ret)
+		return ret;
+
+	mux->current_port = port;
+
+	return 0;
+}
+
+static void phy_mux_phy_power_down(struct phy_mux_port *port)
+{
+	phy_suspend(port->phydev);
+}
+
+static const struct phy_mux_port_ops phy_mux_phy_power_ops = {
+	.deselect = phy_mux_phy_power_down,
+};
+
 static int phy_mux_port_bind_phy(struct phy_mux_port *mux_port)
 {
 	struct phy_device *phydev;
@@ -129,6 +219,9 @@ static int phy_mux_attach(struct phy_mux *mux, struct net_device *dev,
 	if (has_phys)
 		dev->phydev = NULL;
 
+	xa_for_each(&mux->ports, id, port)
+		phy_mux_port_deselect(port);
+
 	if (dev->dev.parent)
 		mux->devlink = device_link_add(dev->dev.parent, mux->dev,
 					       DL_FLAG_STATELESS);
@@ -188,13 +281,15 @@ EXPORT_SYMBOL_GPL(fwnode_phy_mux_attach);
  * phy_mux_detach() - Detach the mux from its parent
  * @mux: The mux
  *
- * The PHYs behind the mux are detached.
+ * The mux is stopped and its PHYs are detached. Caller must hold rtnl when
+ * the mux was started.
  */
 void phy_mux_detach(struct phy_mux *mux)
 {
 	if (!mux->netdev)
 		return;
 
+	phy_mux_stop(mux);
 	phy_mux_detach_phys(mux);
 
 	if (mux->devlink) {
@@ -208,6 +303,41 @@ void phy_mux_detach(struct phy_mux *mux)
 }
 EXPORT_SYMBOL_GPL(phy_mux_detach);
 
+/**
+ * phy_mux_start() - Start selecting ports
+ * @mux: The mux, attached
+ *
+ * To be called when the parent is ready to use the PHY of the selected
+ * port, typically when the interface is brought up. Caller must hold rtnl.
+ */
+void phy_mux_start(struct phy_mux *mux)
+{
+	struct phy_mux_port *port;
+	unsigned long id = 0;
+
+	mux->state = PHY_MUX_STARTED;
+
+	port = xa_find(&mux->ports, &id, ULONG_MAX, XA_PRESENT);
+	phy_mux_select(mux, port);
+}
+EXPORT_SYMBOL_GPL(phy_mux_start);
+
+/**
+ * phy_mux_stop() - Deselect the port in use and stop selecting ports
+ * @mux: The mux
+ *
+ * Does nothing when the mux isn't started. Caller must hold rtnl otherwise.
+ */
+void phy_mux_stop(struct phy_mux *mux)
+{
+	if (mux->state < PHY_MUX_STARTED)
+		return;
+
+	mux->state = PHY_MUX_STOPPED;
+	phy_mux_select(mux, NULL);
+}
+EXPORT_SYMBOL_GPL(phy_mux_stop);
+
 /**
  * phy_mux_alloc() - Allocate a mux
  * @dev: The mux device
@@ -305,6 +435,7 @@ int phy_mux_fwnode_add_ports(struct phy_mux *mux,
 		port->type = PHY_MUX_PORT_PHY;
 		port->fwnode = phy_fwnode;
 		port->mux = mux;
+		port->ops = &phy_mux_phy_power_ops;
 
 		ret = fwnode_get_phy_mode(child);
 		if (ret < 0) {
@@ -357,6 +488,7 @@ void phy_mux_unregister(struct phy_mux *mux)
 	rtnl_lock();
 	if (mux->netdev && mux->pops && mux->pops->detach)
 		mux->pops->detach(mux, mux->parent_priv);
+	phy_mux_stop(mux);
 	rtnl_unlock();
 }
 EXPORT_SYMBOL_GPL(phy_mux_unregister);
diff --git a/include/linux/phy_mux.h b/include/linux/phy_mux.h
index cdc58b56a3b4..6a42ffff02b7 100644
--- a/include/linux/phy_mux.h
+++ b/include/linux/phy_mux.h
@@ -11,6 +11,14 @@ struct net_device;
 struct phy_mux;
 struct phy_mux_port;
 
+/**
+ * struct phy_mux_port_ops - ops for the multiplexed mii ports of the mux
+ * @deselect: Disconnect the port from the MII
+ */
+struct phy_mux_port_ops {
+	void (*deselect)(struct phy_mux_port *port);
+};
+
 enum phy_mux_port_type {
 	PHY_MUX_PORT_PHY,
 };
@@ -23,8 +31,10 @@ enum phy_mux_port_type {
  * @phydev: For PHY_MUX_PORT_PHY, the PHY device behind the port, while the
  *	    mux is attached
  * @fwnode: For PHY_MUX_PORT_PHY, the firmware node of that PHY
+ * @ops: How to drive that port
  * @interface: The MII mode the link runs in when this port is selected
  * @attached: For PHY_MUX_PORT_PHY, the PHY is attached to the mux's netdev
+ * @selected: The port currently drives the MII
  */
 struct phy_mux_port {
 	u32 id;
@@ -33,20 +43,29 @@ struct phy_mux_port {
 	struct phy_device *phydev;
 	struct fwnode_handle *fwnode;
 
+	const struct phy_mux_port_ops *ops;
 	phy_interface_t interface;
 
 	bool attached;
+	bool selected;
 };
 
 /**
  * struct phy_mux_parent_ops - Ops for the device providing the single MII
  *				that's multiplexed, usually the MAC
  * @attach: The mux is attached to the parent.
+ * @select: A port was selected, the parent must now use it (e.g. connect
+ *	    to its PHY)
+ * @deselect: The port is no longer in use
  * @detach: The mux is going away, the parent must detach from it. Called with
  *	    rtnl held.
  */
 struct phy_mux_parent_ops {
 	void (*attach)(struct phy_mux *mux, void *priv);
+	int (*select)(struct phy_mux *mux, struct phy_mux_port *port,
+		      void *priv);
+	void (*deselect)(struct phy_mux *mux, struct phy_mux_port *port,
+			 void *priv);
 	void (*detach)(struct phy_mux *mux, void *priv);
 };
 
@@ -63,6 +82,8 @@ int fwnode_phy_mux_attach(const struct fwnode_handle *fwnode,
 			  struct net_device *dev, u32 phy_flags,
 			  const struct phy_mux_parent_ops *pops, void *priv);
 void phy_mux_detach(struct phy_mux *mux);
+void phy_mux_start(struct phy_mux *mux);
+void phy_mux_stop(struct phy_mux *mux);
 
 #else
 
@@ -78,6 +99,14 @@ static inline void phy_mux_detach(struct phy_mux *mux)
 {
 }
 
+static inline void phy_mux_start(struct phy_mux *mux)
+{
+}
+
+static inline void phy_mux_stop(struct phy_mux *mux)
+{
+}
+
 #endif /* CONFIG_PHY_MUX */
 
 #endif /* __PHY_MUX_H */
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH net-next 06/12] net: phy: phy_mux: Support SFP cage ports
  2026-12-04 11:06 [PATCH net-next 00/12] net: phy: Introduce MII multiplexers Maxime Chevallier
                   ` (4 preceding siblings ...)
  2026-12-04 11:06 ` [PATCH net-next 05/12] net: phy: phy_mux: Add port selection logic Maxime Chevallier
@ 2026-12-04 11:06 ` Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  2026-12-04 11:06 ` [PATCH net-next 07/12] net: phylink: Factor out phylink_clear_phydev() Maxime Chevallier
                   ` (5 subsequent siblings)
  11 siblings, 2 replies; 31+ messages in thread
From: Maxime Chevallier @ 2026-12-04 11:06 UTC (permalink / raw)
  To: davem, Andrew Lunn, Jakub Kicinski, Eric Dumazet,
	Willem de Bruijn, Paolo Abeni, Russell King, Marek Behún,
	Sebastian Hesselbarth, Gregory Clement, Heiner Kallweit
  Cc: Maxime Chevallier, netdev, linux-kernel, linux-arm-kernel,
	thomas.petazzoni, devicetree, Conor Dooley, Krzysztof Kozlowski,
	Rob Herring, Christophe Leroy, Herve Codina, Florian Fainelli,
	Vladimir Oltean, Köry Maincent, Oleksij Rempel,
	Nicolò Veronese, Simon Horman, marcin.s.wojtas, Daniel Golle,
	Dimitri Fedrau, Frank Wunderlich, Pietro Ameruoso,
	Aleksei Sviridkin

It's quite common to have combo ports where one of the ports is an SFP
cage, let's allow adding one on the mux.

To keep things realistic, we only allow one SFP cage per combo port.

When an SFP cage doesn't have any module inserted, it doesn't really
make sense to allow using it as the active port, so we need to keep
track of whether there's a module or not.

There's a simple way to implement a combo port that we find on Turris
Omnia for example, that is to wire the SFP cage's mod_def0 to the
physical multiplexer. In this case, we can't select the port to use from
the host, but we can report that the port is in use and shutdown the
unused port. For that, we however need to indicate that the SFP cage
always takes precedence, as represented in the auto-mux binding.

So, add support for attaching SFP cages to muxes, and implement the
automatic mux.

Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 drivers/net/phy/phy_mux.c | 190 ++++++++++++++++++++++++++++++++++++--
 include/linux/phy_mux.h   |  34 ++++++-
 2 files changed, 213 insertions(+), 11 deletions(-)

diff --git a/drivers/net/phy/phy_mux.c b/drivers/net/phy/phy_mux.c
index 407269980edc..de3336970bd9 100644
--- a/drivers/net/phy/phy_mux.c
+++ b/drivers/net/phy/phy_mux.c
@@ -9,6 +9,7 @@
 #include <linux/property.h>
 #include <linux/rtnetlink.h>
 #include <linux/slab.h>
+#include <linux/workqueue.h>
 #include <linux/xarray.h>
 
 static LIST_HEAD(phy_muxes);
@@ -33,9 +34,15 @@ struct phy_mux {
 	int max_n_ports;
 
 	struct phy_mux_port *current_port;
+	struct work_struct state_work;
 	enum phy_mux_state state;
 };
 
+static void phy_mux_trigger(struct phy_mux *mux)
+{
+	queue_work(system_freezable_power_efficient_wq, &mux->state_work);
+}
+
 static void phy_mux_port_deselect(struct phy_mux_port *port)
 {
 	if (port->ops && port->ops->deselect)
@@ -122,6 +129,109 @@ static const struct phy_mux_port_ops phy_mux_phy_power_ops = {
 	.deselect = phy_mux_phy_power_down,
 };
 
+static struct phy_mux_port *phy_mux_choose_port(struct phy_mux *mux)
+{
+	struct phy_mux_port *port;
+	unsigned long id;
+
+	xa_for_each(&mux->ports, id, port)
+		if (port->has_precedence && port->available)
+			return port;
+
+	xa_for_each(&mux->ports, id, port)
+		if (port->available)
+			return port;
+
+	return NULL;
+}
+
+static void phy_mux_state_machine(struct work_struct *work)
+{
+	struct phy_mux *mux = container_of(work, struct phy_mux, state_work);
+
+	rtnl_lock();
+
+	switch (mux->state) {
+	case PHY_MUX_STOPPED:
+		break;
+
+	case PHY_MUX_STARTED:
+		phy_mux_select(mux, phy_mux_choose_port(mux));
+		break;
+	}
+
+	rtnl_unlock();
+}
+
+/**
+ * phy_mux_port_set_available() - Report whether a port has a medium to use
+ * @port: The mux port
+ * @available: true when there is something to link with on that port
+ *
+ * The parent of the mux calls this for the SFP cage port, when a module gets
+ * plugged or removed. Caller must hold rtnl.
+ */
+void phy_mux_port_set_available(struct phy_mux_port *port, bool available)
+{
+	struct phy_mux *mux = port->mux;
+
+	ASSERT_RTNL();
+
+	if (port->available == available)
+		return;
+
+	port->available = available;
+
+	if (available && mux->state == PHY_MUX_STARTED) {
+		phy_mux_select(mux, phy_mux_choose_port(mux));
+		return;
+	}
+
+	phy_mux_trigger(mux);
+}
+EXPORT_SYMBOL_GPL(phy_mux_port_set_available);
+
+/**
+ * phy_mux_get_sfp_port() - Get the SFP cage port of a mux
+ * @mux: The mux
+ *
+ * A mux has at most one SFP cage port.
+ *
+ * Returns: the SFP cage port, or NULL when the mux has none.
+ */
+struct phy_mux_port *phy_mux_get_sfp_port(struct phy_mux *mux)
+{
+	struct phy_mux_port *port;
+	unsigned long id;
+
+	xa_for_each(&mux->ports, id, port)
+		if (port->type == PHY_MUX_PORT_SFP)
+			return port;
+
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(phy_mux_get_sfp_port);
+
+/**
+ * fwnode_phy_mux_find_sfp_port() - Find the SFP cage port of a mux
+ * @fwnode: The mux device's fwnode
+ *
+ * Returns: the fwnode of the port carrying an sfp property, with a reference
+ * held, or NULL.
+ */
+struct fwnode_handle *
+fwnode_phy_mux_find_sfp_port(const struct fwnode_handle *fwnode)
+{
+	struct fwnode_handle *child;
+
+	fwnode_for_each_available_child_node(fwnode, child)
+		if (fwnode_property_present(child, "sfp"))
+			return child;
+
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(fwnode_phy_mux_find_sfp_port);
+
 static int phy_mux_port_bind_phy(struct phy_mux_port *mux_port)
 {
 	struct phy_device *phydev;
@@ -312,13 +422,8 @@ EXPORT_SYMBOL_GPL(phy_mux_detach);
  */
 void phy_mux_start(struct phy_mux *mux)
 {
-	struct phy_mux_port *port;
-	unsigned long id = 0;
-
 	mux->state = PHY_MUX_STARTED;
-
-	port = xa_find(&mux->ports, &id, ULONG_MAX, XA_PRESENT);
-	phy_mux_select(mux, port);
+	phy_mux_trigger(mux);
 }
 EXPORT_SYMBOL_GPL(phy_mux_start);
 
@@ -335,6 +440,8 @@ void phy_mux_stop(struct phy_mux *mux)
 
 	mux->state = PHY_MUX_STOPPED;
 	phy_mux_select(mux, NULL);
+
+	cancel_work(&mux->state_work);
 }
 EXPORT_SYMBOL_GPL(phy_mux_stop);
 
@@ -356,6 +463,7 @@ struct phy_mux *phy_mux_alloc(struct device *dev, int max_n_ports)
 		return ERR_PTR(-ENOMEM);
 
 	xa_init_flags(&mux->ports, XA_FLAGS_ALLOC1);
+	INIT_WORK(&mux->state_work, phy_mux_state_machine);
 
 	mux->dev = dev;
 	mux->max_n_ports = max_n_ports;
@@ -371,6 +479,9 @@ static void phy_mux_port_destroy(struct phy_mux_port *mux_port)
 		phy_mux_port_unbind_phy(mux_port);
 		fwnode_handle_put(mux_port->fwnode);
 		break;
+	case PHY_MUX_PORT_SFP:
+		fwnode_handle_put(mux_port->fwnode);
+		break;
 	}
 
 	kfree(mux_port);
@@ -396,29 +507,80 @@ static int phy_mux_register_port(struct phy_mux *mux,
 	if (ret)
 		return ret;
 
+	port->available = true;
+
 	mux->n_ports++;
 
 	return 0;
 }
 
+static int phy_mux_add_sfp_port(struct phy_mux *mux,
+				struct fwnode_handle *child,
+				int (*init_port)(struct phy_mux_port *port,
+						 struct fwnode_handle *child))
+{
+	struct phy_mux_port *port;
+	int ret;
+
+	if (phy_mux_get_sfp_port(mux))
+		return -EBUSY;
+
+	port = kzalloc_obj(*port);
+	if (!port)
+		return -ENOMEM;
+
+	port->type = PHY_MUX_PORT_SFP;
+	port->fwnode = fwnode_handle_get(child);
+	port->mux = mux;
+
+	if (init_port) {
+		ret = init_port(port, child);
+		if (ret) {
+			phy_mux_port_destroy(port);
+			return ret;
+		}
+	}
+
+	ret = phy_mux_register_port(mux, port);
+	if (ret) {
+		phy_mux_port_destroy(port);
+		return ret;
+	}
+
+	port->available = false;
+
+	return 0;
+}
+
 /**
  * phy_mux_fwnode_add_ports() - Create the ports described under a fwnode
  * @mux: The mux
- * @fwnode: The mux device's fwnode, each child being a port with a
- *	    phy-handle and a phy-mode
+ * @fwnode: The mux device's fwnode, each child being a port with either a
+ *	    phy-handle and a phy-mode, or an sfp property
+ * @init_port: Optional driver callback to parse the rest of a port's node
  *
  * The PHYs themselves are only looked up when the mux gets attached.
  *
  * Returns: 0 on success or a negative error.
  */
 int phy_mux_fwnode_add_ports(struct phy_mux *mux,
-			     const struct fwnode_handle *fwnode)
+			     const struct fwnode_handle *fwnode,
+			     int (*init_port)(struct phy_mux_port *port,
+					      struct fwnode_handle *child))
 {
 	struct fwnode_handle *child, *phy_fwnode;
 	struct phy_mux_port *port;
 	int ret;
 
 	fwnode_for_each_available_child_node(fwnode, child) {
+		if (fwnode_property_present(child, "sfp")) {
+			ret = phy_mux_add_sfp_port(mux, child, init_port);
+			if (ret)
+				goto err;
+
+			continue;
+		}
+
 		phy_fwnode = fwnode_get_phy_node(child);
 		if (IS_ERR(phy_fwnode)) {
 			ret = PTR_ERR(phy_fwnode);
@@ -444,6 +606,14 @@ int phy_mux_fwnode_add_ports(struct phy_mux *mux,
 		}
 		port->interface = ret;
 
+		if (init_port) {
+			ret = init_port(port, child);
+			if (ret) {
+				phy_mux_port_destroy(port);
+				goto err;
+			}
+		}
+
 		ret = phy_mux_register_port(mux, port);
 		if (ret) {
 			phy_mux_port_destroy(port);
@@ -502,6 +672,8 @@ void phy_mux_free(struct phy_mux *mux)
 	struct phy_mux_port *port;
 	unsigned long id;
 
+	disable_work_sync(&mux->state_work);
+
 	xa_for_each(&mux->ports, id, port)
 		phy_mux_port_destroy(port);
 
diff --git a/include/linux/phy_mux.h b/include/linux/phy_mux.h
index 6a42ffff02b7..77cf389b8c77 100644
--- a/include/linux/phy_mux.h
+++ b/include/linux/phy_mux.h
@@ -21,6 +21,7 @@ struct phy_mux_port_ops {
 
 enum phy_mux_port_type {
 	PHY_MUX_PORT_PHY,
+	PHY_MUX_PORT_SFP,
 };
 
 /**
@@ -30,9 +31,14 @@ enum phy_mux_port_type {
  * @type: What sits behind that port
  * @phydev: For PHY_MUX_PORT_PHY, the PHY device behind the port, while the
  *	    mux is attached
- * @fwnode: For PHY_MUX_PORT_PHY, the firmware node of that PHY
+ * @fwnode: For PHY_MUX_PORT_PHY, the firmware node of that PHY. For
+ *	    PHY_MUX_PORT_SFP, the port's own firmware node, carrying the sfp
+ *	    property.
  * @ops: How to drive that port
  * @interface: The MII mode the link runs in when this port is selected
+ * @available: The port can be used (e.g. an SFP module is plugged)
+ * @has_precedence: The port is selected as soon as it is available, whatever
+ *		  the other ports do.
  * @attached: For PHY_MUX_PORT_PHY, the PHY is attached to the mux's netdev
  * @selected: The port currently drives the MII
  */
@@ -46,6 +52,8 @@ struct phy_mux_port {
 	const struct phy_mux_port_ops *ops;
 	phy_interface_t interface;
 
+	bool available;
+	bool has_precedence;
 	bool attached;
 	bool selected;
 };
@@ -73,7 +81,9 @@ struct phy_mux_parent_ops {
 
 struct phy_mux *phy_mux_alloc(struct device *dev, int max_n_ports);
 int phy_mux_fwnode_add_ports(struct phy_mux *mux,
-			     const struct fwnode_handle *fwnode);
+			     const struct fwnode_handle *fwnode,
+			     int (*init_port)(struct phy_mux_port *port,
+					      struct fwnode_handle *child));
 int phy_mux_register(struct phy_mux *mux);
 void phy_mux_unregister(struct phy_mux *mux);
 void phy_mux_free(struct phy_mux *mux);
@@ -84,6 +94,10 @@ int fwnode_phy_mux_attach(const struct fwnode_handle *fwnode,
 void phy_mux_detach(struct phy_mux *mux);
 void phy_mux_start(struct phy_mux *mux);
 void phy_mux_stop(struct phy_mux *mux);
+struct fwnode_handle *
+fwnode_phy_mux_find_sfp_port(const struct fwnode_handle *fwnode);
+struct phy_mux_port *phy_mux_get_sfp_port(struct phy_mux *mux);
+void phy_mux_port_set_available(struct phy_mux_port *port, bool available);
 
 #else
 
@@ -107,6 +121,22 @@ static inline void phy_mux_stop(struct phy_mux *mux)
 {
 }
 
+static inline struct fwnode_handle *
+fwnode_phy_mux_find_sfp_port(const struct fwnode_handle *fwnode)
+{
+	return NULL;
+}
+
+static inline struct phy_mux_port *phy_mux_get_sfp_port(struct phy_mux *mux)
+{
+	return NULL;
+}
+
+static inline void phy_mux_port_set_available(struct phy_mux_port *port,
+					      bool available)
+{
+}
+
 #endif /* CONFIG_PHY_MUX */
 
 #endif /* __PHY_MUX_H */
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH net-next 07/12] net: phylink: Factor out phylink_clear_phydev()
  2026-12-04 11:06 [PATCH net-next 00/12] net: phy: Introduce MII multiplexers Maxime Chevallier
                   ` (5 preceding siblings ...)
  2026-12-04 11:06 ` [PATCH net-next 06/12] net: phy: phy_mux: Support SFP cage ports Maxime Chevallier
@ 2026-12-04 11:06 ` Maxime Chevallier
  2026-12-04 11:06 ` [PATCH net-next 08/12] net: phy: phy_mux: Add phy_mux_set_mac_managed_pm() Maxime Chevallier
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 31+ messages in thread
From: Maxime Chevallier @ 2026-12-04 11:06 UTC (permalink / raw)
  To: davem, Andrew Lunn, Jakub Kicinski, Eric Dumazet,
	Willem de Bruijn, Paolo Abeni, Russell King, Marek Behún,
	Sebastian Hesselbarth, Gregory Clement, Heiner Kallweit
  Cc: Maxime Chevallier, netdev, linux-kernel, linux-arm-kernel,
	thomas.petazzoni, devicetree, Conor Dooley, Krzysztof Kozlowski,
	Rob Herring, Christophe Leroy, Herve Codina, Florian Fainelli,
	Vladimir Oltean, Köry Maincent, Oleksij Rempel,
	Nicolò Veronese, Simon Horman, marcin.s.wojtas, Daniel Golle,
	Dimitri Fedrau, Frank Wunderlich, Pietro Ameruoso,
	Aleksei Sviridkin

When disconnecting from a PHY, phylink first clears its internal
references to it, then calls phy_disconnect().

For PHYs that sit behind a mux, we'll only need the first part, as the
mux is the one that attaches and detaches the PHYs from the netdev.

Split that out in phylink_clear_phydev(), in preparation for the phy_mux
support. No functional change intended.

Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 drivers/net/phy/phylink.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
index 6f5af388210d..03e255f5ceb1 100644
--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -2553,14 +2553,7 @@ int phylink_fwnode_phy_connect(struct phylink *pl,
 }
 EXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect);
 
-/**
- * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
- *   instance.
- * @pl: a pointer to a &struct phylink returned from phylink_create()
- *
- * Disconnect any current PHY from the phylink instance described by @pl.
- */
-void phylink_disconnect_phy(struct phylink *pl)
+static struct phy_device *phylink_clear_phydev(struct phylink *pl)
 {
 	struct phy_device *phy;
 
@@ -2579,10 +2572,25 @@ void phylink_disconnect_phy(struct phylink *pl)
 	}
 	mutex_unlock(&pl->phydev_mutex);
 
-	if (phy) {
+	if (phy)
 		flush_work(&pl->resolve);
+
+	return phy;
+}
+
+/**
+ * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
+ *   instance.
+ * @pl: a pointer to a &struct phylink returned from phylink_create()
+ *
+ * Disconnect any current PHY from the phylink instance described by @pl.
+ */
+void phylink_disconnect_phy(struct phylink *pl)
+{
+	struct phy_device *phy = phylink_clear_phydev(pl);
+
+	if (phy)
 		phy_disconnect(phy);
-	}
 }
 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
 
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH net-next 08/12] net: phy: phy_mux: Add phy_mux_set_mac_managed_pm()
  2026-12-04 11:06 [PATCH net-next 00/12] net: phy: Introduce MII multiplexers Maxime Chevallier
                   ` (6 preceding siblings ...)
  2026-12-04 11:06 ` [PATCH net-next 07/12] net: phylink: Factor out phylink_clear_phydev() Maxime Chevallier
@ 2026-12-04 11:06 ` Maxime Chevallier
  2026-12-04 11:06 ` [PATCH net-next 09/12] net: phylink: Support PHYs reached through a phy_mux Maxime Chevallier
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 31+ messages in thread
From: Maxime Chevallier @ 2026-12-04 11:06 UTC (permalink / raw)
  To: davem, Andrew Lunn, Jakub Kicinski, Eric Dumazet,
	Willem de Bruijn, Paolo Abeni, Russell King, Marek Behún,
	Sebastian Hesselbarth, Gregory Clement, Heiner Kallweit
  Cc: Maxime Chevallier, netdev, linux-kernel, linux-arm-kernel,
	thomas.petazzoni, devicetree, Conor Dooley, Krzysztof Kozlowski,
	Rob Herring, Christophe Leroy, Herve Codina, Florian Fainelli,
	Vladimir Oltean, Köry Maincent, Oleksij Rempel,
	Nicolò Veronese, Simon Horman, marcin.s.wojtas, Daniel Golle,
	Dimitri Fedrau, Frank Wunderlich, Pietro Ameruoso,
	Aleksei Sviridkin

MAC drivers that deal with the suspend/resume of their PHY on their own
say so with the mac_managed_pm flag, that ends-up being set on the PHY
when the MAC connects to it.

With a mux, the PHYs are attached by the mux and the parent only gets to
see the PHY of the selected port. The flag is needed on all of them
though, as they are all attached to the netdev.

Add a helper for the mux's parent to set the flag on all the PHYs behind
the mux.

Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 drivers/net/phy/phy_mux.c | 15 +++++++++++++++
 include/linux/phy_mux.h   |  5 +++++
 2 files changed, 20 insertions(+)

diff --git a/drivers/net/phy/phy_mux.c b/drivers/net/phy/phy_mux.c
index de3336970bd9..ed0bb8a3f429 100644
--- a/drivers/net/phy/phy_mux.c
+++ b/drivers/net/phy/phy_mux.c
@@ -413,6 +413,21 @@ void phy_mux_detach(struct phy_mux *mux)
 }
 EXPORT_SYMBOL_GPL(phy_mux_detach);
 
+/**
+ * phy_mux_set_mac_managed_pm() - Leave the PM of the PHYs to the MAC driver
+ * @mux: The mux, attached
+ */
+void phy_mux_set_mac_managed_pm(struct phy_mux *mux)
+{
+	struct phy_mux_port *port;
+	unsigned long id;
+
+	xa_for_each(&mux->ports, id, port)
+		if (port->attached)
+			port->phydev->mac_managed_pm = true;
+}
+EXPORT_SYMBOL_GPL(phy_mux_set_mac_managed_pm);
+
 /**
  * phy_mux_start() - Start selecting ports
  * @mux: The mux, attached
diff --git a/include/linux/phy_mux.h b/include/linux/phy_mux.h
index 77cf389b8c77..00703371eea8 100644
--- a/include/linux/phy_mux.h
+++ b/include/linux/phy_mux.h
@@ -94,6 +94,7 @@ int fwnode_phy_mux_attach(const struct fwnode_handle *fwnode,
 void phy_mux_detach(struct phy_mux *mux);
 void phy_mux_start(struct phy_mux *mux);
 void phy_mux_stop(struct phy_mux *mux);
+void phy_mux_set_mac_managed_pm(struct phy_mux *mux);
 struct fwnode_handle *
 fwnode_phy_mux_find_sfp_port(const struct fwnode_handle *fwnode);
 struct phy_mux_port *phy_mux_get_sfp_port(struct phy_mux *mux);
@@ -121,6 +122,10 @@ static inline void phy_mux_stop(struct phy_mux *mux)
 {
 }
 
+static inline void phy_mux_set_mac_managed_pm(struct phy_mux *mux)
+{
+}
+
 static inline struct fwnode_handle *
 fwnode_phy_mux_find_sfp_port(const struct fwnode_handle *fwnode)
 {
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH net-next 09/12] net: phylink: Support PHYs reached through a phy_mux
  2026-12-04 11:06 [PATCH net-next 00/12] net: phy: Introduce MII multiplexers Maxime Chevallier
                   ` (7 preceding siblings ...)
  2026-12-04 11:06 ` [PATCH net-next 08/12] net: phy: phy_mux: Add phy_mux_set_mac_managed_pm() Maxime Chevallier
@ 2026-12-04 11:06 ` Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  2026-12-04 11:06 ` [PATCH net-next 10/12] net: phylink: Support an SFP cage behind " Maxime Chevallier
                   ` (2 subsequent siblings)
  11 siblings, 2 replies; 31+ messages in thread
From: Maxime Chevallier @ 2026-12-04 11:06 UTC (permalink / raw)
  To: davem, Andrew Lunn, Jakub Kicinski, Eric Dumazet,
	Willem de Bruijn, Paolo Abeni, Russell King, Marek Behún,
	Sebastian Hesselbarth, Gregory Clement, Heiner Kallweit
  Cc: Maxime Chevallier, netdev, linux-kernel, linux-arm-kernel,
	thomas.petazzoni, devicetree, Conor Dooley, Krzysztof Kozlowski,
	Rob Herring, Christophe Leroy, Herve Codina, Florian Fainelli,
	Vladimir Oltean, Köry Maincent, Oleksij Rempel,
	Nicolò Veronese, Simon Horman, marcin.s.wojtas, Daniel Golle,
	Dimitri Fedrau, Frank Wunderlich, Pietro Ameruoso,
	Aleksei Sviridkin

Now that we have phy_mux, we need something to act as its parent, i.e.
something that reconfigures the MAC side each time the mux switches.
phylink is the right place for that, it already knows how to bring a
PHY up and how to configure the MAC and PCS for it.

When the MAC's node has a mii-mux-handle property,
phylink_fwnode_phy_connect() now attaches to the mux instead of
connecting to a PHY. phylink starts and stops the mux along with the
interface, and implements the parent_ops:

 - select: bring up the port's PHY, configure the MAC and start the PHY

 - deselect: stop the PHY

The PHYs are attached to the netdev by the mux, so phylink doesn't
attach nor detach them.

One side-effect is that phylink has no PHY between the time it attaches
to the mux and the first selection, which only happens after
phylink_start(). When a mux is used, MAC drivers can't assume there's a
PHY right after phylink_fwnode_phy_connect() anymore.

Only PHY ports are handled for now, SFP ports come next.

Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 drivers/net/phy/phylink.c | 154 +++++++++++++++++++++++++++++++++++---
 1 file changed, 144 insertions(+), 10 deletions(-)

diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
index 03e255f5ceb1..702614adcb94 100644
--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -15,6 +15,7 @@
 #include <linux/phy.h>
 #include <linux/phy_fixed.h>
 #include <linux/phy_link_topology.h>
+#include <linux/phy_mux.h>
 #include <linux/phy_port.h>
 #include <linux/phylink.h>
 #include <linux/rtnetlink.h>
@@ -97,6 +98,7 @@ struct phylink {
 	u8 sfp_port;
 	struct phy_port *sfp_cage_port;
 	struct phy_port *mod_port;
+	struct phy_mux *mux;
 
 	struct eee_config eee_cfg;
 
@@ -2141,6 +2143,15 @@ struct phylink *phylink_create(struct phylink_config *config,
 }
 EXPORT_SYMBOL_GPL(phylink_create);
 
+static void phylink_detach_mux(struct phylink *pl)
+{
+	if (!pl->mux)
+		return;
+
+	phy_mux_detach(pl->mux);
+	pl->mux = NULL;
+}
+
 /**
  * phylink_destroy() - cleanup and destroy the phylink instance
  * @pl: a pointer to a &struct phylink returned from phylink_create()
@@ -2153,6 +2164,7 @@ EXPORT_SYMBOL_GPL(phylink_create);
 void phylink_destroy(struct phylink *pl)
 {
 	sfp_bus_del_upstream(pl->sfp_bus);
+	phylink_detach_mux(pl);
 	phylink_destroy_sfp_cage_port(pl);
 	if (pl->link_gpio)
 		gpiod_put(pl->link_gpio);
@@ -2326,9 +2338,13 @@ static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
 	phy->phy_link_change = phylink_phy_change;
 
 	irq_str = phy_attached_info_irq(phy);
-	phylink_info(pl,
-		     "PHY [%s] driver [%s] (irq=%s)\n",
-		     dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
+	if (phy->mux_port)
+		phylink_dbg(pl, "PHY [%s] driver [%s] (irq=%s)\n",
+			    dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
+	else
+		phylink_info(pl,
+			     "PHY [%s] driver [%s] (irq=%s)\n",
+			     dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
 	kfree(irq_str);
 
 	mutex_lock(&pl->phydev_mutex);
@@ -2494,6 +2510,34 @@ int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
 }
 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
 
+static const struct phy_mux_parent_ops phylink_mux_ops;
+
+static int phylink_attach_mux(struct phylink *pl,
+			      const struct fwnode_handle *fwnode, u32 flags)
+{
+	struct fwnode_handle *mux_fwnode;
+	int ret;
+
+	if (pl->mux || !IS_ENABLED(CONFIG_PHY_MUX))
+		return 0;
+
+	if (pl->config->type != PHYLINK_NETDEV)
+		return 0;
+
+	mux_fwnode = fwnode_find_reference(fwnode, "mii-mux-handle", 0);
+	if (IS_ERR(mux_fwnode))
+		return 0;
+
+	if (pl->config->mac_requires_rxc)
+		flags |= PHY_F_RXC_ALWAYS_ON;
+
+	ret = fwnode_phy_mux_attach(mux_fwnode, pl->netdev, flags,
+				    &phylink_mux_ops, pl);
+	fwnode_handle_put(mux_fwnode);
+
+	return ret == -EPROBE_DEFER ? -ENODEV : ret;
+}
+
 /**
  * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
  * @pl: a pointer to a &struct phylink returned from phylink_create()
@@ -2501,7 +2545,7 @@ EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
  * @flags: PHY-specific flags to communicate to the PHY device driver
  *
  * Connect the phy specified @fwnode to the phylink instance specified
- * by @pl.
+ * by @pl, or attach to the PHY multiplexer it refers to.
  *
  * Returns 0 on success or a negative errno.
  */
@@ -2516,6 +2560,13 @@ int phylink_fwnode_phy_connect(struct phylink *pl,
 	if (!phylink_expects_phy(pl))
 		return 0;
 
+	ret = phylink_attach_mux(pl, fwnode, flags);
+	if (ret)
+		return ret;
+
+	if (pl->mux)
+		return 0;
+
 	phy_fwnode = fwnode_get_phy_node(fwnode);
 	if (IS_ERR(phy_fwnode)) {
 		/* PHY mode requires a PHY to be specified. */
@@ -2578,19 +2629,26 @@ static struct phy_device *phylink_clear_phydev(struct phylink *pl)
 	return phy;
 }
 
+static void phylink_disconnect_current_phy(struct phylink *pl)
+{
+	struct phy_device *phy = phylink_clear_phydev(pl);
+
+	if (phy)
+		phy_disconnect(phy);
+}
+
 /**
  * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
  *   instance.
  * @pl: a pointer to a &struct phylink returned from phylink_create()
  *
- * Disconnect any current PHY from the phylink instance described by @pl.
+ * Disconnect any current PHY from the phylink instance described by @pl,
+ * and detach from its PHY multiplexer if it has one.
  */
 void phylink_disconnect_phy(struct phylink *pl)
 {
-	struct phy_device *phy = phylink_clear_phydev(pl);
-
-	if (phy)
-		phy_disconnect(phy);
+	phylink_detach_mux(pl);
+	phylink_disconnect_current_phy(pl);
 }
 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
 
@@ -2708,6 +2766,8 @@ void phylink_start(struct phylink *pl)
 		phy_start(pl->phydev);
 	if (pl->sfp_bus)
 		sfp_upstream_start(pl->sfp_bus);
+	if (pl->mux)
+		phy_mux_start(pl->mux);
 }
 EXPORT_SYMBOL_GPL(phylink_start);
 
@@ -2727,6 +2787,8 @@ void phylink_stop(struct phylink *pl)
 {
 	ASSERT_RTNL();
 
+	if (pl->mux)
+		phy_mux_stop(pl->mux);
 	if (pl->sfp_bus)
 		sfp_upstream_stop(pl->sfp_bus);
 	if (pl->phydev)
@@ -4194,10 +4256,82 @@ static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
 static void phylink_sfp_disconnect_phy(void *upstream,
 				       struct phy_device *phydev)
 {
-	phylink_disconnect_phy(upstream);
+	phylink_disconnect_current_phy(upstream);
 	phy_set_upstream_port(phydev, NULL);
 }
 
+static void phylink_mux_attach(struct phy_mux *mux, void *priv)
+{
+	struct phylink *pl = priv;
+
+	if (pl->config->mac_managed_pm)
+		phy_mux_set_mac_managed_pm(mux);
+
+	pl->mux = mux;
+}
+
+static int phylink_mux_select(struct phy_mux *mux, struct phy_mux_port *port,
+			      void *priv)
+{
+	struct phylink *pl = priv;
+	struct phy_device *phy;
+	int ret;
+
+	if (port->type != PHY_MUX_PORT_PHY)
+		return -EOPNOTSUPP;
+
+	if (pl->phydev)
+		return -EBUSY;
+
+	phy = port->phydev;
+
+	ret = phylink_bringup_phy(pl, phy, port->interface);
+	if (ret) {
+		phy->phylink = NULL;
+		return ret;
+	}
+
+	pl->link_config.interface = port->interface;
+	phylink_mac_initial_config(pl, false);
+	phy_start(phy);
+
+	return 0;
+}
+
+static void phylink_mux_deselect(struct phy_mux *mux,
+				 struct phy_mux_port *port, void *priv)
+{
+	struct phylink *pl = priv;
+	struct phy_device *phy;
+
+	if (pl->phydev != port->phydev)
+		return;
+
+	phy = port->phydev;
+
+	if (phy_is_started(phy) || phy->state == PHY_ERROR)
+		phy_stop(phy);
+
+	phylink_clear_phydev(pl);
+
+	if (phy_interrupt_is_valid(phy))
+		phy_free_interrupt(phy);
+
+	phy->phylink = NULL;
+}
+
+static void phylink_mux_detach(struct phy_mux *mux, void *priv)
+{
+	phylink_detach_mux(priv);
+}
+
+static const struct phy_mux_parent_ops phylink_mux_ops = {
+	.attach = phylink_mux_attach,
+	.select = phylink_mux_select,
+	.deselect = phylink_mux_deselect,
+	.detach = phylink_mux_detach,
+};
+
 static const struct sfp_upstream_ops sfp_phylink_ops = {
 	.attach = phylink_sfp_attach,
 	.detach = phylink_sfp_detach,
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH net-next 10/12] net: phylink: Support an SFP cage behind a phy_mux
  2026-12-04 11:06 [PATCH net-next 00/12] net: phy: Introduce MII multiplexers Maxime Chevallier
                   ` (8 preceding siblings ...)
  2026-12-04 11:06 ` [PATCH net-next 09/12] net: phylink: Support PHYs reached through a phy_mux Maxime Chevallier
@ 2026-12-04 11:06 ` Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  2026-12-04 11:06 ` [PATCH net-next 11/12] net: phy: mux: Add the mii-mux-auto driver Maxime Chevallier
  2026-12-04 11:06 ` [PATCH net-next 12/12] ARM: dts: turris-omnia: Describe the WAN port multiplexer Maxime Chevallier
  11 siblings, 2 replies; 31+ messages in thread
From: Maxime Chevallier @ 2026-12-04 11:06 UTC (permalink / raw)
  To: davem, Andrew Lunn, Jakub Kicinski, Eric Dumazet,
	Willem de Bruijn, Paolo Abeni, Russell King, Marek Behún,
	Sebastian Hesselbarth, Gregory Clement, Heiner Kallweit
  Cc: Maxime Chevallier, netdev, linux-kernel, linux-arm-kernel,
	thomas.petazzoni, devicetree, Conor Dooley, Krzysztof Kozlowski,
	Rob Herring, Christophe Leroy, Herve Codina, Florian Fainelli,
	Vladimir Oltean, Köry Maincent, Oleksij Rempel,
	Nicolò Veronese, Simon Horman, marcin.s.wojtas, Daniel Golle,
	Dimitri Fedrau, Frank Wunderlich, Pietro Ameruoso,
	Aleksei Sviridkin

With a mux, the SFP cage isn't described in the MAC's node but in one of
the mux ports. phylink is still the upstream of that cage, so let's look
for it there as well.

From there, modules are handled the same way as with any other
MAC-driven cage. What phylink needs to do on top of that is tell the mux
when a module is there, with phy_mux_port_set_available(), so that the
mux can select the SFP port, and go back to the PHY port when the module
is removed.

There's one limitation for now, we only report the modules that phylink
accepted. On the Turris Omnia the mux switches for any module though, so
with an unsupported module we're left with the PHY port selected while
the MAC is wired to the cage. Handling that needs a new SFP upstream
callback to follow the raw module presence, this is left for later.

Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 drivers/net/phy/phylink.c | 94 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 89 insertions(+), 5 deletions(-)

diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
index 702614adcb94..6d62bd37f95a 100644
--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -93,12 +93,17 @@ struct phylink {
 
 	struct sfp_bus *sfp_bus;
 	bool sfp_may_have_phy;
+	bool sfp_mod_present;
+	bool sfp_behind_mux;
+	bool sfp_selected;
+	bool sfp_link_down;
 	DECLARE_PHY_INTERFACE_MASK(sfp_interfaces);
 	__ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
 	u8 sfp_port;
 	struct phy_port *sfp_cage_port;
 	struct phy_port *mod_port;
 	struct phy_mux *mux;
+	struct phy_mux_port *mux_sfp_port;
 
 	struct eee_config eee_cfg;
 
@@ -1876,6 +1881,30 @@ static int phylink_register_sfp(struct phylink *pl,
 	return ret;
 }
 
+static int phylink_register_mux_sfp(struct phylink *pl,
+				    const struct fwnode_handle *fwnode)
+{
+	struct fwnode_handle *mux_fwnode, *sfp_fwnode;
+	int ret;
+
+	mux_fwnode = fwnode_find_reference(fwnode, "mii-mux-handle", 0);
+	if (IS_ERR(mux_fwnode))
+		return phylink_register_sfp(pl, fwnode);
+
+	sfp_fwnode = fwnode_phy_mux_find_sfp_port(mux_fwnode);
+	fwnode_handle_put(mux_fwnode);
+	if (!sfp_fwnode)
+		return phylink_register_sfp(pl, fwnode);
+
+	pl->sfp_behind_mux = true;
+	pl->sfp_link_down = true;
+
+	ret = phylink_register_sfp(pl, sfp_fwnode);
+	fwnode_handle_put(sfp_fwnode);
+
+	return ret;
+}
+
 /**
  * phylink_set_fixed_link() - set the fixed link
  * @pl: a pointer to a &struct phylink returned from phylink_create()
@@ -2128,7 +2157,7 @@ struct phylink *phylink_create(struct phylink_config *config,
 
 	pl->req_link_an_mode = pl->cfg_link_an_mode;
 
-	ret = phylink_register_sfp(pl, fwnode);
+	ret = phylink_register_mux_sfp(pl, fwnode);
 	if (ret < 0)
 		goto release_link_gpio;
 
@@ -2150,6 +2179,7 @@ static void phylink_detach_mux(struct phylink *pl)
 
 	phy_mux_detach(pl->mux);
 	pl->mux = NULL;
+	pl->mux_sfp_port = NULL;
 }
 
 /**
@@ -2766,6 +2796,9 @@ void phylink_start(struct phylink *pl)
 		phy_start(pl->phydev);
 	if (pl->sfp_bus)
 		sfp_upstream_start(pl->sfp_bus);
+	if (pl->mux_sfp_port)
+		phy_mux_port_set_available(pl->mux_sfp_port,
+					   pl->sfp_mod_present);
 	if (pl->mux)
 		phy_mux_start(pl->mux);
 }
@@ -3842,7 +3875,8 @@ int phylink_speed_down(struct phylink *pl, bool sync)
 
 	ASSERT_RTNL();
 
-	if (!pl->sfp_bus && pl->phydev)
+	if (pl->phydev &&
+	    (!pl->sfp_bus || (pl->sfp_behind_mux && !pl->sfp_selected)))
 		ret = phy_speed_down(pl->phydev, sync);
 
 	return ret;
@@ -3865,7 +3899,8 @@ int phylink_speed_up(struct phylink *pl)
 
 	ASSERT_RTNL();
 
-	if (!pl->sfp_bus && pl->phydev)
+	if (pl->phydev &&
+	    (!pl->sfp_bus || (pl->sfp_behind_mux && !pl->sfp_selected)))
 		ret = phy_speed_up(pl->phydev);
 
 	return ret;
@@ -3884,6 +3919,10 @@ static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
 	struct phylink *pl = upstream;
 
 	pl->netdev->sfp_bus = NULL;
+
+	pl->sfp_mod_present = false;
+	if (pl->mux_sfp_port)
+		phy_mux_port_set_available(pl->mux_sfp_port, false);
 }
 
 static phy_interface_t phylink_choose_sfp_interface(struct phylink *pl,
@@ -4082,11 +4121,19 @@ static int phylink_sfp_config_optical(struct phylink *pl)
 	return 0;
 }
 
+static void phylink_sfp_set_present(struct phylink *pl, bool present)
+{
+	pl->sfp_mod_present = present;
+	if (pl->mux_sfp_port)
+		phy_mux_port_set_available(pl->mux_sfp_port, present);
+}
+
 static int phylink_sfp_module_insert(void *upstream,
 				     const struct sfp_eeprom_id *id)
 {
 	const struct sfp_module_caps *caps;
 	struct phylink *pl = upstream;
+	int ret;
 
 	ASSERT_RTNL();
 
@@ -4096,18 +4143,27 @@ static int phylink_sfp_module_insert(void *upstream,
 	pl->sfp_may_have_phy = caps->may_have_phy;
 	pl->sfp_port = caps->port;
 
+	phylink_sfp_set_present(pl, true);
+
 	/* If this module may have a PHY connecting later, defer until later */
 	if (pl->sfp_may_have_phy)
 		return 0;
 
-	return phylink_sfp_config_optical(pl);
+	ret = phylink_sfp_config_optical(pl);
+	if (ret)
+		phylink_sfp_set_present(pl, false);
+
+	return ret;
 }
 
 static void phylink_sfp_module_remove(void *upstream)
 {
 	struct phylink *pl = upstream;
 
+	ASSERT_RTNL();
+
 	phy_interface_zero(pl->sfp_interfaces);
+	phylink_sfp_set_present(pl, false);
 }
 
 static int phylink_add_sfp_mod_port(struct phylink *pl)
@@ -4211,6 +4267,10 @@ static void phylink_sfp_link_down(void *upstream)
 
 	ASSERT_RTNL();
 
+	pl->sfp_link_down = true;
+	if (pl->sfp_behind_mux && !pl->sfp_selected)
+		return;
+
 	phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
 }
 
@@ -4220,6 +4280,10 @@ static void phylink_sfp_link_up(void *upstream)
 
 	ASSERT_RTNL();
 
+	pl->sfp_link_down = false;
+	if (pl->sfp_behind_mux && !pl->sfp_selected)
+		return;
+
 	phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK);
 }
 
@@ -4268,6 +4332,7 @@ static void phylink_mux_attach(struct phy_mux *mux, void *priv)
 		phy_mux_set_mac_managed_pm(mux);
 
 	pl->mux = mux;
+	pl->mux_sfp_port = phy_mux_get_sfp_port(mux);
 }
 
 static int phylink_mux_select(struct phy_mux *mux, struct phy_mux_port *port,
@@ -4277,6 +4342,19 @@ static int phylink_mux_select(struct phy_mux *mux, struct phy_mux_port *port,
 	struct phy_device *phy;
 	int ret;
 
+	if (port->type == PHY_MUX_PORT_SFP) {
+		if (!pl->sfp_bus)
+			return -ENODEV;
+
+		pl->sfp_selected = true;
+		if (pl->sfp_link_down)
+			phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
+		else
+			phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK);
+
+		return 0;
+	}
+
 	if (port->type != PHY_MUX_PORT_PHY)
 		return -EOPNOTSUPP;
 
@@ -4291,7 +4369,10 @@ static int phylink_mux_select(struct phy_mux *mux, struct phy_mux_port *port,
 		return ret;
 	}
 
+	clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
+
 	pl->link_config.interface = port->interface;
+	pl->req_link_an_mode = pl->cfg_link_an_mode;
 	phylink_mac_initial_config(pl, false);
 	phy_start(phy);
 
@@ -4304,7 +4385,10 @@ static void phylink_mux_deselect(struct phy_mux *mux,
 	struct phylink *pl = priv;
 	struct phy_device *phy;
 
-	if (pl->phydev != port->phydev)
+	if (port->type == PHY_MUX_PORT_SFP)
+		pl->sfp_selected = false;
+
+	if (port->type != PHY_MUX_PORT_PHY || pl->phydev != port->phydev)
 		return;
 
 	phy = port->phydev;
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH net-next 11/12] net: phy: mux: Add the mii-mux-auto driver
  2026-12-04 11:06 [PATCH net-next 00/12] net: phy: Introduce MII multiplexers Maxime Chevallier
                   ` (9 preceding siblings ...)
  2026-12-04 11:06 ` [PATCH net-next 10/12] net: phylink: Support an SFP cage behind " Maxime Chevallier
@ 2026-12-04 11:06 ` Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  2026-12-04 11:06 ` [PATCH net-next 12/12] ARM: dts: turris-omnia: Describe the WAN port multiplexer Maxime Chevallier
  11 siblings, 2 replies; 31+ messages in thread
From: Maxime Chevallier @ 2026-12-04 11:06 UTC (permalink / raw)
  To: davem, Andrew Lunn, Jakub Kicinski, Eric Dumazet,
	Willem de Bruijn, Paolo Abeni, Russell King, Marek Behún,
	Sebastian Hesselbarth, Gregory Clement, Heiner Kallweit
  Cc: Maxime Chevallier, netdev, linux-kernel, linux-arm-kernel,
	thomas.petazzoni, devicetree, Conor Dooley, Krzysztof Kozlowski,
	Rob Herring, Christophe Leroy, Herve Codina, Florian Fainelli,
	Vladimir Oltean, Köry Maincent, Oleksij Rempel,
	Nicolò Veronese, Simon Horman, marcin.s.wojtas, Daniel Golle,
	Dimitri Fedrau, Frank Wunderlich, Pietro Ameruoso,
	Aleksei Sviridkin

Add a driver for the simplest kind of mux, the ones that are switched by
the hardware without any control from software, described with the
mii-mux-auto binding.

That's what we have on the Turris Omnia's WAN port, where the mux's
select line is tied to the SFP cage's mod_def0 signal.

There's not much to do in the driver: it registers the ports, and looks
for the takes-precedence property to tell the core which port the
hardware is going to switch to.

Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 MAINTAINERS                        |  1 +
 drivers/net/phy/Kconfig            |  2 +
 drivers/net/phy/Makefile           |  1 +
 drivers/net/phy/mux/Kconfig        | 18 ++++++
 drivers/net/phy/mux/Makefile       |  3 +
 drivers/net/phy/mux/mii_mux_auto.c | 99 ++++++++++++++++++++++++++++++
 6 files changed, 124 insertions(+)
 create mode 100644 drivers/net/phy/mux/Kconfig
 create mode 100644 drivers/net/phy/mux/Makefile
 create mode 100644 drivers/net/phy/mux/mii_mux_auto.c

diff --git a/MAINTAINERS b/MAINTAINERS
index b2b02a1dbd3a..59017b0f9f86 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10086,6 +10086,7 @@ M:	Maxime Chevallier <maxime.chevallier@bootlin.com>
 L:	netdev@vger.kernel.org
 S:	Maintained
 F:	Documentation/devicetree/bindings/net/mii-mux*.yaml
+F:	drivers/net/phy/mux/
 F:	drivers/net/phy/phy_mux.c
 F:	include/linux/phy_mux.h
 
diff --git a/drivers/net/phy/Kconfig b/drivers/net/phy/Kconfig
index d8a0131d2aa2..f6397dab2304 100644
--- a/drivers/net/phy/Kconfig
+++ b/drivers/net/phy/Kconfig
@@ -29,6 +29,8 @@ config PHY_MUX
 	  time. This adds the infrastructure to represent such multiplexers
 	  and to select which port carries the link.
 
+source "drivers/net/phy/mux/Kconfig"
+
 config SWPHY
 	bool
 
diff --git a/drivers/net/phy/Makefile b/drivers/net/phy/Makefile
index b3ec2933fec4..b26b34c93dbc 100644
--- a/drivers/net/phy/Makefile
+++ b/drivers/net/phy/Makefile
@@ -21,6 +21,7 @@ obj-$(CONFIG_PHYLINK)		+= phylink.o
 obj-$(CONFIG_PHYLIB)		+= libphy.o
 obj-$(CONFIG_PHYLIB)		+= mdio_devres.o
 obj-$(CONFIG_PHY_PACKAGE)	+= phy_package.o
+obj-$(CONFIG_PHY_MUX)		+= mux/
 
 obj-$(CONFIG_NETWORK_PHY_TIMESTAMPING) += mii_timestamper.o
 
diff --git a/drivers/net/phy/mux/Kconfig b/drivers/net/phy/mux/Kconfig
new file mode 100644
index 000000000000..cbfd49fab2ea
--- /dev/null
+++ b/drivers/net/phy/mux/Kconfig
@@ -0,0 +1,18 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+if PHY_MUX
+
+config MII_MUX_AUTO
+	tristate "MII multiplexer switched by the hardware"
+	help
+	  Support for the two-port MII multiplexers that the hardware
+	  switches by itself, without any software control.
+
+	  This is typically found on combo ports, where a MAC is wired to
+	  either a PHY or an SFP cage depending on whether a module is
+	  plugged in the cage.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called mii_mux_auto.
+
+endif # PHY_MUX
diff --git a/drivers/net/phy/mux/Makefile b/drivers/net/phy/mux/Makefile
new file mode 100644
index 000000000000..23e8c5029507
--- /dev/null
+++ b/drivers/net/phy/mux/Makefile
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+obj-$(CONFIG_MII_MUX_AUTO)	+= mii_mux_auto.o
diff --git a/drivers/net/phy/mux/mii_mux_auto.c b/drivers/net/phy/mux/mii_mux_auto.c
new file mode 100644
index 000000000000..980cc2c242ba
--- /dev/null
+++ b/drivers/net/phy/mux/mii_mux_auto.c
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+#include <linux/module.h>
+#include <linux/phy_mux.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+
+static int mii_mux_auto_init_port(struct phy_mux_port *port,
+				  struct fwnode_handle *child)
+{
+	port->has_precedence = fwnode_property_read_bool(child,
+							 "takes-precedence");
+
+	return 0;
+}
+
+static int mii_mux_auto_check_ports(struct fwnode_handle *fwnode)
+{
+	struct fwnode_handle *child;
+	int n_sfp = 0, n_phy = 0, n_prec = 0, n_sfp_prec = 0;
+
+	fwnode_for_each_available_child_node(fwnode, child) {
+		if (fwnode_property_read_bool(child, "takes-precedence"))
+			n_prec++;
+
+		if (!fwnode_property_present(child, "sfp")) {
+			n_phy++;
+			continue;
+		}
+
+		n_sfp++;
+		if (fwnode_property_read_bool(child, "takes-precedence"))
+			n_sfp_prec++;
+	}
+
+	if (n_sfp != 1 || n_phy != 1 || n_prec != 1 || n_sfp_prec != 1)
+		return -EOPNOTSUPP;
+
+	return 0;
+}
+
+static int mii_mux_auto_probe(struct platform_device *pdev)
+{
+	struct fwnode_handle *fwnode = dev_fwnode(&pdev->dev);
+	struct phy_mux *mux;
+	int ret;
+
+	ret = mii_mux_auto_check_ports(fwnode);
+	if (ret)
+		return ret;
+
+	mux = phy_mux_alloc(&pdev->dev, 2);
+	if (IS_ERR(mux))
+		return PTR_ERR(mux);
+
+	ret = phy_mux_fwnode_add_ports(mux, fwnode, mii_mux_auto_init_port);
+	if (ret)
+		goto err_free;
+
+	ret = phy_mux_register(mux);
+	if (ret)
+		goto err_free;
+
+	platform_set_drvdata(pdev, mux);
+
+	return 0;
+
+err_free:
+	phy_mux_free(mux);
+	return ret;
+}
+
+static void mii_mux_auto_remove(struct platform_device *pdev)
+{
+	struct phy_mux *mux = platform_get_drvdata(pdev);
+
+	phy_mux_unregister(mux);
+	phy_mux_free(mux);
+}
+
+static const struct of_device_id mii_mux_auto_of_match[] = {
+	{ .compatible = "mii-mux-auto" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, mii_mux_auto_of_match);
+
+static struct platform_driver mii_mux_auto_driver = {
+	.probe = mii_mux_auto_probe,
+	.remove = mii_mux_auto_remove,
+	.driver = {
+		.name = "mii-mux-auto",
+		.of_match_table = mii_mux_auto_of_match,
+	},
+};
+module_platform_driver(mii_mux_auto_driver);
+
+MODULE_DESCRIPTION("Ethernet MII multiplexer switched by the hardware");
+MODULE_AUTHOR("Maxime Chevallier <maxime.chevallier@bootlin.com>");
+MODULE_LICENSE("GPL");
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH net-next 12/12] ARM: dts: turris-omnia: Describe the WAN port multiplexer
  2026-12-04 11:06 [PATCH net-next 00/12] net: phy: Introduce MII multiplexers Maxime Chevallier
                   ` (10 preceding siblings ...)
  2026-12-04 11:06 ` [PATCH net-next 11/12] net: phy: mux: Add the mii-mux-auto driver Maxime Chevallier
@ 2026-12-04 11:06 ` Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  11 siblings, 2 replies; 31+ messages in thread
From: Maxime Chevallier @ 2026-12-04 11:06 UTC (permalink / raw)
  To: davem, Andrew Lunn, Jakub Kicinski, Eric Dumazet,
	Willem de Bruijn, Paolo Abeni, Russell King, Marek Behún,
	Sebastian Hesselbarth, Gregory Clement, Heiner Kallweit
  Cc: Maxime Chevallier, netdev, linux-kernel, linux-arm-kernel,
	thomas.petazzoni, devicetree, Conor Dooley, Krzysztof Kozlowski,
	Rob Herring, Christophe Leroy, Herve Codina, Florian Fainelli,
	Vladimir Oltean, Köry Maincent, Oleksij Rempel,
	Nicolò Veronese, Simon Horman, marcin.s.wojtas, Daniel Golle,
	Dimitri Fedrau, Frank Wunderlich, Pietro Ameruoso,
	Aleksei Sviridkin

The Turris Omnia's WAN port has a mux between the MAC and both an SFP
cage and a 88E1514 PHY. The mux is a high-speed analog switch (NXP
CBTL02042A), whose select line is driven by the cage's mod_def0 signal:
it switches to the cage as soon as a module is plugged.

We couldn't deal with that so far. The devicetree only describes the
PHY, and relies on the bootloader to enable the sfp node and drop the
phy-handle when a module is found at boot. Modules plugged or removed
later on aren't taken into account.

Now that we have a binding for these muxes, let's describe that one. The
sfp node is always enabled, and we follow the module at runtime.

Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 .../dts/marvell/armada-385-turris-omnia.dts   | 29 ++++++++++++-------
 1 file changed, 18 insertions(+), 11 deletions(-)

diff --git a/arch/arm/boot/dts/marvell/armada-385-turris-omnia.dts b/arch/arm/boot/dts/marvell/armada-385-turris-omnia.dts
index 83fe00abd652..0501686e2642 100644
--- a/arch/arm/boot/dts/marvell/armada-385-turris-omnia.dts
+++ b/arch/arm/boot/dts/marvell/armada-385-turris-omnia.dts
@@ -103,13 +103,24 @@ sfp: sfp {
 		los-gpios = <&pcawan 3 GPIO_ACTIVE_HIGH>;
 		mod-def0-gpios = <&pcawan 4 GPIO_ACTIVE_LOW>;
 		maximum-power-milliwatt = <3000>;
+	};
 
-		/*
-		 * For now this has to be enabled at boot time by U-Boot when
-		 * a SFP module is present. Read more in the comment in the
-		 * eth2 node below.
-		 */
-		status = "disabled";
+	wan_mux: mii-mux {
+		compatible = "mii-mux-auto";
+		#address-cells = <1>;
+		#size-cells = <0>;
+
+		port@0 {
+			reg = <0>;
+			sfp = <&sfp>;
+			takes-precedence;
+		};
+
+		port@1 {
+			reg = <1>;
+			phy-handle = <&phy1>;
+			phy-mode = "sgmii";
+		};
 	};
 
 	gpio-keys {
@@ -199,16 +210,12 @@ &eth2 {
 	 * eth2 is connected via a multiplexor to both the SFP cage and to
 	 * ethernet-phy@1. The multiplexor switches the signal to SFP cage when
 	 * a SFP module is present, as determined by the mode-def0 GPIO.
-	 *
-	 * Until kernel supports this configuration properly, in case SFP module
-	 * is present, U-Boot has to enable the sfp node above, remove phy
-	 * handle and add managed = "in-band-status" property.
 	 */
 	status = "okay";
 	phy-mode = "sgmii";
 	phy-handle = <&phy1>;
+	mii-mux-handle = <&wan_mux>;
 	phys = <&comphy5 2>;
-	sfp = <&sfp>;
 	buffer-manager = <&bm>;
 	bm,pool-long = <2>;
 	bm,pool-short = <3>;
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 01/12] dt-bindings: net: Add Ethernet MII multiplexers
  2026-12-04 11:06 ` [PATCH net-next 01/12] dt-bindings: net: Add Ethernet " Maxime Chevallier
@ 2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: sashiko-bot @ 2026-12-05 11:08 UTC (permalink / raw)
  To: Maxime Chevallier; +Cc: devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] dt-bindings: net: mii-mux-auto allows >2 ports and extra
  takes-precedence
- [Low] dt-bindings: net: mii-mux-auto leaves takes-precedence trigger
  unspecified
--

commit 15b0b40dd7397c1fe9390dd7da13fe9a18c062a8
Author: Maxime Chevallier <maxime.chevallier@bootlin.com>

dt-bindings: net: Add Ethernet MII multiplexers

This commit adds generic and hardware-switched Ethernet MII multiplexer
devicetree bindings, adds a mii-mux-handle property to
ethernet-controller.yaml, and adds a MAINTAINERS entry.

> diff --git a/Documentation/devicetree/bindings/net/mii-mux-auto.yaml b/Documentation/devicetree/bindings/net/mii-mux-auto.yaml
> new file mode 100644
> index 0000000000000..7a965f6e8dcce
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/net/mii-mux-auto.yaml
> @@ -0,0 +1,89 @@

[ ... ]

> +allOf:
> +  - $ref: mii-mux.yaml#
> +
> +properties:
> +  compatible:
> +    const: mii-mux-auto
> +
> +patternProperties:
> +  "^port@[0-9a-f]+$":
> +    $ref: mii-mux.yaml#/$defs/port
> +    unevaluatedProperties: false
> +
> +    properties:
> +      takes-precedence:
> +        type: boolean
> +        description:
> +          The multiplexer switches to this port by itself as soon as the
> +          port gets active, whatever the state of the other port.

[Severity: Low]
Should the takes-precedence description specify what physical event counts
as "active"?

Without software control or state readback, drivers must infer when the
hardware switches.  "Active" could mean SFP module presence (mod_def0),
optical signal detect (RX_LOS), or link-up, letting incompatible hardware
share the same DT representation.

It also leaves takes-precedence permitted on phy-handle ports even though
no activation trigger is defined for a PHY port.

> +
> +required:
> +  - compatible
> +  - port@0
> +  - port@1
> +
> +oneOf:
> +  - properties:
> +      port@0:
> +        required:
> +          - takes-precedence
> +  - properties:
> +      port@1:
> +        required:
> +          - takes-precedence
> +
> +unevaluatedProperties: false

[Severity: Medium]
Does this schema still allow a mii-mux-auto node with port@2 or higher?

Both this file and mii-mux.yaml match "^port@[0-9a-f]+$" without bounding
reg, so port@2+ children pass unevaluatedProperties: false.  Because oneOf
only checks port@0 and port@1, an extra port can also set takes-precedence
and still pass dtbs_check.

Narrowing the pattern here to "^port@[01]$" alone would not reject port@2+
because mii-mux.yaml still evaluates "^port@[0-9a-f]+$" via allOf;
constraining reg to [0, 1] (or rejecting higher ports explicitly) would
enforce both invariants.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204110622.1106492-1-maxime.chevallier@bootlin.com?part=1

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 03/12] net: phy: Don't create the sysfs links for PHYs behind a mux
  2026-12-04 11:06 ` [PATCH net-next 03/12] net: phy: Don't create the sysfs links for PHYs behind a mux Maxime Chevallier
@ 2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: sashiko-bot @ 2026-12-05 11:08 UTC (permalink / raw)
  To: Maxime Chevallier; +Cc: devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] net: phy: skipping phy_sysfs_create_links() drops per-PHY
  attached_dev link
--

commit a16b08e2d216e5687da38893c4bd94d7b180213f
Author: Maxime Chevallier <maxime.chevallier@bootlin.com>

net: phy: Don't create the sysfs links for PHYs behind a mux

This commit adds a mux_port pointer to struct phy_device and skips calling
phy_sysfs_create_links() in phy_attach_direct() when phydev->mux_port is set.

> diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
> index 60440b62e6c79..9630578a6a5c5 100644
> --- a/drivers/net/phy/phy_device.c
> +++ b/drivers/net/phy/phy_device.c
> @@ -2019,7 +2019,8 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
>  	 */
>  	phydev->sysfs_links = false;
>  
> -	phy_sysfs_create_links(phydev);
> +	if (!phydev->mux_port)
> +		phy_sysfs_create_links(phydev);
>  
>  	if (!phydev->attached_dev) {
>  		err = sysfs_create_file(&phydev->mdio.dev.kobj,

[Severity: Low]
Should muxed PHYs still create the per-PHY attached_dev symlink?

phy_sysfs_create_links() creates two symlinks: attached_dev under
phydev->mdio.dev.kobj and phydev under dev->dev.kobj.  Only the netdev-side
phydev link collides across multiple PHYs (failing with -EEXIST rather than
overwriting), whereas each PHY has its own kobject for attached_dev.

Skipping phy_sysfs_create_links() entirely while phydev->attached_dev is
still set leaves muxed PHYs with neither attached_dev nor phy_standalone in
sysfs, despite Documentation/ABI/testing/sysfs-class-net-phydev documenting
attached_dev for attached PHYs.

If omitting attached_dev is intentional, the commit message and ABI doc
should note that; otherwise, only the netdev-side phydev link needs to be
skipped (with phy_detach_internal() updated to match).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204110622.1106492-1-maxime.chevallier@bootlin.com?part=3

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 04/12] net: phy: phy_mux: Allow attaching a mux to a net_device
  2026-12-04 11:06 ` [PATCH net-next 04/12] net: phy: phy_mux: Allow attaching a mux to a net_device Maxime Chevallier
@ 2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: sashiko-bot @ 2026-12-05 11:08 UTC (permalink / raw)
  To: Maxime Chevallier; +Cc: devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] net: phy: unsynchronized phy_mux_detach() races with
  unregister and attach
- [Medium] net: phy: phy_mux: phy_mux_attach() clobbers dev->sfp_bus
- [Medium] net: phy: phy_mux_detach_phys() omits phy_stop_machine()
  before phy_detach()
--

commit 5ec0ab0887a678472a4f565b6b693b7e69aef8df
Author: Maxime Chevallier <maxime.chevallier@bootlin.com>

net: phy: phy_mux: Allow attaching a mux to a net_device

This commit adds support for attaching a phy_mux to a net_device and its
parent, binding and attaching the PHYs behind the mux and introducing
parent_ops attach and detach callbacks.

> diff --git a/drivers/net/phy/phy_mux.c b/drivers/net/phy/phy_mux.c
> index e259eb4e271a0..691bca09e0af1 100644
> --- a/drivers/net/phy/phy_mux.c
> +++ b/drivers/net/phy/phy_mux.c

[ ... ]

> +static void phy_mux_detach_phys(struct phy_mux *mux)
> +{
> +	struct phy_mux_port *port;
> +	unsigned long id;
> +
> +	xa_for_each(&mux->ports, id, port) {
> +		if (!port->attached)
> +			continue;
> +
> +		phy_detach(port->phydev);
> +		phy_mux_port_unbind_phy(port);
> +		port->attached = false;
> +	}
> +}

[Severity: Medium]
Should phy_mux_detach_phys() call phy_stop_machine() before phy_detach()?

phy_mux_attach() sets port->phydev->phy_link_change to
phy_mux_phy_link_change, which causes phy_uses_state_machine() to return
true even for unselected mux PHYs.  On MAC drivers without mac_managed_pm
(such as mvneta), mdio_bus_phy_resume() then calls phy_start_machine() and
queues phydev->state_queue.  For non-genphy drivers, phy_detach() does not
cancel state_queue.

If a deselected PHY is in PHY_HALTED with phydev->suspended still false
(for example, with WoL enabled), a queued phy_state_machine() work item
can race with detach and netdev teardown:

CPU0                              CPU1 (kworker)
                                  phy_state_machine()
                                    _phy_state_machine()  /* PHY_HALTED */
                                    mutex_unlock(&phydev->lock)
                                    phy_suspend()
                                      netdev = phydev->attached_dev
phy_mux_detach()
  phy_mux_detach_phys()
    phy_detach()
      phydev->attached_dev = NULL
      phy_device_reset(phydev, 1)
free_netdev(netdev)
                                      netdev->ethtool->wol_enabled
                                        <-- use-after-free

Even without WoL, a pending phy_suspend() can run after
phy_device_reset(phydev, 1) or race with a subsequent re-attach and power
down an active PHY.

[ ... ]

> +		ret = phy_attach_direct(dev, port->phydev, phy_flags,
> +					port->interface);
> +		if (ret) {
> +			phy_mux_port_unbind_phy(port);
> +			goto err_detach;
> +		}
> +
> +		port->phydev->phy_link_change = phy_mux_phy_link_change;
> +		port->attached = true;
> +		has_phys = true;
> +	}
> +
> +	if (has_phys)
> +		dev->phydev = NULL;

[Severity: Medium]
Can attaching a muxed PHY here clobber dev->sfp_bus?

Unlike phy_sysfs_create_links(), phy_attach_direct() unconditionally sets
dev->sfp_bus = phydev->sfp_bus when phydev->sfp_bus_attached is true, even
when phydev->mux_port is set.  Because deselected mux PHYs keep
phydev->attached_dev set to dev, later phy_sfp_attach() and
phy_sfp_detach() calls also overwrite or clear dev->sfp_bus.

On a mux where a PHY has a downstream SFP cage, this overwrites any
existing dev->sfp_bus (such as an SFP cage on another mux port), causing
ethtool -m to query the wrong cage.  In addition, phy_detach_internal()
clears phydev->attached_dev without clearing dev->sfp_bus, leaving a
dangling dev->sfp_bus pointer if the PHY and its SFP socket are later
unbound while dev stays registered.

[ ... ]

> +void phy_mux_detach(struct phy_mux *mux)
> +{
> +	if (!mux->netdev)
> +		return;
> +
> +	phy_mux_detach_phys(mux);
> +
> +	if (mux->devlink) {
> +		device_link_del(mux->devlink);
> +		mux->devlink = NULL;
> +	}
> +
> +	mux->pops = NULL;
> +	mux->parent_priv = NULL;
> +	mux->netdev = NULL;
> +}
> +EXPORT_SYMBOL_GPL(phy_mux_detach);

[ ... ]

>  void phy_mux_unregister(struct phy_mux *mux)
>  {
>  	mutex_lock(&phy_mux_mutex);
>  	list_del(&mux->node);
>  	mutex_unlock(&phy_mux_mutex);
> +
> +	rtnl_lock();
> +	if (mux->netdev && mux->pops && mux->pops->detach)
> +		mux->pops->detach(mux, mux->parent_priv);
> +	rtnl_unlock();
>  }

[Severity: Medium]
Can phy_mux_detach() race with phy_mux_unregister() and phy_mux_free()?

phy_mux_attach() adds mux->devlink with DL_FLAG_STATELESS, so unbinding
the mux does not unbind the MAC first.  phy_mux_unregister() drops
phy_mux_mutex and relies on rtnl_lock() to serialize mux->pops->detach()
before the mux driver calls phy_mux_free(mux), whereas phy_mux_detach()
acquires neither phy_mux_mutex nor rtnl_lock().

When phylink_destroy() calls phy_mux_detach() without RTNL (for instance
in mvpp2_port_remove() or ucc_geth_remove() after an open-time failure
leaves the mux attached), it can run concurrently with mux removal:

CPU0                              CPU1
phylink_destroy()  /* no RTNL */  phy_mux_unregister()
  phy_mux_detach(mux)               mutex_lock(&phy_mux_mutex)
    if (!mux->netdev) /* false */   list_del(&mux->node)
                                    mutex_unlock(&phy_mux_mutex)
                                    rtnl_lock()
                                    if (mux->netdev && mux->pops->detach)
                                      mux->pops->detach(mux, priv)
                                        phy_mux_detach(mux)
                                    rtnl_unlock()
                                  phy_mux_free(mux)
                                    kfree(mux)
    phy_mux_detach_phys(mux)
      <-- use-after-free / double detach

If CPU0 finishes phy_mux_detach() and frees phylink while CPU1 is past the
mux->netdev check, CPU1 can likewise dereference NULL mux->pops or call
pops->detach() on freed parent state.

Similarly, fwnode_phy_mux_attach() holds only phy_mux_mutex and is called
without RTNL by drivers such as dsa_user_create() and ocelot_probe_port(),
sharing no common lock with phy_mux_detach().  Calling phy_mux_detach()
without RTNL while dev is still registered (as in ucc_geth_remove()) also
violates the rcu_dereference_protected() check on dev->hwprov in
phy_detach_internal().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204110622.1106492-1-maxime.chevallier@bootlin.com?part=4

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 05/12] net: phy: phy_mux: Add port selection logic
  2026-12-04 11:06 ` [PATCH net-next 05/12] net: phy: phy_mux: Add port selection logic Maxime Chevallier
@ 2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: sashiko-bot @ 2026-12-05 11:08 UTC (permalink / raw)
  To: Maxime Chevallier; +Cc: devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] net: phy: phy_mux: selected PHY is not resumed before
  pops->select()
- [Medium] net: phy: unlocked phy_mux_detach() races with unregister and
  ethtool
--

commit dc38119185f1b5ebf431eebfe3a9d57a7ea7cf22
Author: Maxime Chevallier <maxime.chevallier@bootlin.com>

net: phy: phy_mux: Add port selection logic

This patch adds port selection and deselection logic to phy_mux, along with
phy_mux_start(), phy_mux_stop(), and phy_mux_port_ops to power down
deselected PHYs.

> diff --git a/drivers/net/phy/phy_mux.c b/drivers/net/phy/phy_mux.c
> index 691bca09e0af1..407269980edc1 100644
> --- a/drivers/net/phy/phy_mux.c
> +++ b/drivers/net/phy/phy_mux.c

[ ... ]

> +static int __phy_mux_select_port(struct phy_mux *mux,
> +				 struct phy_mux_port *port)
> +{
> +	int ret;
> +
> +	if (mux->pops && mux->pops->select) {
> +		ret = mux->pops->select(mux, port, mux->parent_priv);
> +		phy_mux_port_set_parent_phy(mux, port, !ret);
> +		if (ret)
> +			return ret;
> +	}
> +
> +	port->selected = true;
> +
> +	return 0;
> +}

[Severity: High]
Should port->phydev be resumed before calling mux->pops->select()?

phy_mux_attach() and __phy_mux_deselect_port() suspend every PHY via
phy_mux_phy_power_down() -> phy_suspend(), so mux->pops->select() runs
against a powered-down PHY.  When a parent like phylink runs
phylink_bringup_phy(), EEE clock-stop configuration and
phy_request_interrupt() execute before phy_start() calls __phy_resume().

On PHYs such as Broadcom BCM54xx, non-BMCR register writes are ignored while
BMCR_PDOWN is set and bcm54xx_resume() re-runs bcm54xx_config_init(), leaving
hardware interrupts disabled and EEE settings lost.

If pops->select is NULL or does not call phy_start() or phy_resume(), the
selected PHY also remains suspended while port->selected is true.  Adding a
select callback in phy_mux_port_ops to call phy_resume() before
mux->pops->select() would restore symmetry with phy_mux_port_deselect().

[ ... ]

> @@ -188,13 +281,15 @@ EXPORT_SYMBOL_GPL(fwnode_phy_mux_attach);
>   * phy_mux_detach() - Detach the mux from its parent
>   * @mux: The mux
>   *
> - * The PHYs behind the mux are detached.
> + * The mux is stopped and its PHYs are detached. Caller must hold rtnl when
> + * the mux was started.
>   */
>  void phy_mux_detach(struct phy_mux *mux)
>  {
>  	if (!mux->netdev)
>  		return;
>  
> +	phy_mux_stop(mux);
>  	phy_mux_detach_phys(mux);

[Severity: Medium]
Can calling phy_mux_detach() without rtnl_lock() race with
phy_mux_unregister() and ethtool?

phy_mux_attach() creates mux->devlink with DL_FLAG_STATELESS, which does not
prevent the mux driver from unbinding while the parent MAC driver is bound.
Because phy_mux_unregister() drops phy_mux_mutex before taking rtnl_lock(), a
caller that runs phy_mux_detach() without rtnl_lock() when the mux is stopped
(such as phylink_destroy() in mvpp2_port_remove() after an ndo_open()
failure) has no lock in common with phy_mux_unregister():

CPU0                              CPU1
phylink_destroy()
  phy_mux_detach(mux)
    if (!mux->netdev) // false
                                  phy_mux_unregister()
                                    rtnl_lock()
                                    mux->pops->detach(...)
                                      phy_mux_detach(mux)
                                        phy_mux_detach_phys(mux)
    phy_mux_detach_phys(mux)
      <-- double phy_detach()
                                    rtnl_unlock()
                                  phy_mux_free(mux)
    mux->netdev = NULL
      <-- use-after-free

In addition, if the netdev is still NETREG_REGISTERED when phy_mux_detach()
runs without rtnl_lock() (such as ucc_geth_remove() with
devm_register_netdev()), phy_detach_internal() triggers a lockdep splat on
dev->hwprov and phy_link_topo_del_phy() frees struct phy_device_node without
RCU while concurrent ethtool queries in ethnl_req_get_phydev() expect
rtnl_lock().

fwnode_phy_mux_attach() also serializes only on phy_mux_mutex and can be
called without rtnl_lock() (for example from dsa_user_phy_setup()), leaving
mux->netdev, mux->pops, and mux->parent_priv without a common lock between
attach and detach.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204110622.1106492-1-maxime.chevallier@bootlin.com?part=5

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 06/12] net: phy: phy_mux: Support SFP cage ports
  2026-12-04 11:06 ` [PATCH net-next 06/12] net: phy: phy_mux: Support SFP cage ports Maxime Chevallier
@ 2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: sashiko-bot @ 2026-12-05 11:08 UTC (permalink / raw)
  To: Maxime Chevallier; +Cc: devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] net: phy: phy_mux_detach() races with unregister, free, and
  attach
- [Medium] net: phy: phy_mux_select() errors silently ignored by callers
- [Medium] net: phy: phy_mux_start() defers port selection past
  ndo_open() and RTNL
- [Low] net: phy: phy_mux_port_set_available() queues work when stopped
- [Low] net: phy: commit message overstates auto-mux scope and omits
  design
--

commit 7f7c13dd3f5d9a4be97b36f9ec7d719e76abf8ad
Author: Maxime Chevallier <maxime.chevallier@bootlin.com>

> net: phy: phy_mux: Support SFP cage ports
>
> It's quite common to have combo ports where one of the ports is an SFP
> cage, let's allow adding one on the mux.
>
> To keep things realistic, we only allow one SFP cage per combo port.
>
> When an SFP cage doesn't have any module inserted, it doesn't really
> make sense to allow using it as the active port, so we need to keep
> track of whether there's a module or not.
>
> There's a simple way to implement a combo port that we find on Turris
> Omnia for example, that is to wire the SFP cage's mod_def0 to the
> physical multiplexer. In this case, we can't select the port to use from
> the host, but we can report that the port is in use and shutdown the
> unused port. For that, we however need to indicate that the SFP cage
> always takes precedence, as represented in the auto-mux binding.
>
> So, add support for attaching SFP cages to muxes, and implement the
> automatic mux.
>
> Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>

[Severity: Low]
This isn't a bug, but the closing sentence states that this patch
implements the automatic mux, whereas it only adds core phy_mux support
for SFP ports and port precedence (not the mii-mux-auto driver or its DT
property parsing).

The commit message also omits explaining the new init_port callback in
phy_mux_fwnode_add_ports(), changing phy_mux_start() to defer selection
to state_work, and why phy_mux_port_set_available() selects synchronously
when available is true but defers to state_work when false.

> diff --git a/drivers/net/phy/phy_mux.c b/drivers/net/phy/phy_mux.c
> index 407269980edc1..de3336970bd9b 100644
> --- a/drivers/net/phy/phy_mux.c
> +++ b/drivers/net/phy/phy_mux.c

[ ... ]

> @@ -122,6 +129,109 @@ static const struct phy_mux_port_ops phy_mux_phy_power_ops = {
>  	.deselect = phy_mux_phy_power_down,
>  };
>  
> +static struct phy_mux_port *phy_mux_choose_port(struct phy_mux *mux)
> +{
> +	struct phy_mux_port *port;
> +	unsigned long id;
> +
> +	xa_for_each(&mux->ports, id, port)
> +		if (port->has_precedence && port->available)
> +			return port;
> +
> +	xa_for_each(&mux->ports, id, port)
> +		if (port->available)
> +			return port;
> +
> +	return NULL;
> +}
> +
> +static void phy_mux_state_machine(struct work_struct *work)
> +{
> +	struct phy_mux *mux = container_of(work, struct phy_mux, state_work);
> +
> +	rtnl_lock();
> +
> +	switch (mux->state) {
> +	case PHY_MUX_STOPPED:
> +		break;
> +
> +	case PHY_MUX_STARTED:
> +		phy_mux_select(mux, phy_mux_choose_port(mux));
> +		break;
> +	}
> +
> +	rtnl_unlock();
> +}

[Severity: Medium]
Should phy_mux_state_machine() and phy_mux_port_set_available() check the
return value of phy_mux_select()?

phy_mux_select() deselects any active mux->current_port and sets it to
NULL before calling __phy_mux_select_port().  If mux->pops->select() fails
(for example, phylink_bringup_phy() failing in phy_eee_rx_clock_stop() or
phylink_validate_phy()), ignoring the error leaves mux->current_port NULL
and the interface up with no active port or link, without logging any
error.

> +void phy_mux_port_set_available(struct phy_mux_port *port, bool available)
> +{
> +	struct phy_mux *mux = port->mux;
> +
> +	ASSERT_RTNL();
> +
> +	if (port->available == available)
> +		return;
> +
> +	port->available = available;
> +
> +	if (available && mux->state == PHY_MUX_STARTED) {
> +		phy_mux_select(mux, phy_mux_choose_port(mux));
> +		return;
> +	}
> +
> +	phy_mux_trigger(mux);
> +}

[Severity: Low]
Should phy_mux_port_set_available() return early when
mux->state != PHY_MUX_STARTED?

When the mux is stopped, falling through to phy_mux_trigger() wakes a
kworker and acquires rtnl_lock() in phy_mux_state_machine() only to break
out of the switch.  phy_mux_stop() also returns early when already
stopped, so work queued while stopped is not cancelled there either.

[ ... ]

> @@ -312,13 +422,8 @@ EXPORT_SYMBOL_GPL(phy_mux_detach);
>   */
>  void phy_mux_start(struct phy_mux *mux)
>  {
> -	struct phy_mux_port *port;
> -	unsigned long id = 0;
> -
>  	mux->state = PHY_MUX_STARTED;
> -
> -	port = xa_find(&mux->ports, &id, ULONG_MAX, XA_PRESENT);
> -	phy_mux_select(mux, port);
> +	phy_mux_trigger(mux);
>  }
>  EXPORT_SYMBOL_GPL(phy_mux_start);

[Severity: Medium]
Does deferring initial port selection to state_work break callers that
expect a PHY to be selected when phy_mux_start() returns?

Because phy_mux_state_machine() blocks on rtnl_lock(), port selection
cannot run until ndo_open() releases RTNL.  Drivers such as stmmac_open()
and mvneta_start_dev() call phylink_speed_up() right after phylink_start()
under the same RTNL lock, where pl->phydev is now guaranteed to be NULL;
phy_speed_up() is silently skipped and a PHY downgraded by
phylink_speed_down() remains stuck at its lowest advertised speed.

Userspace ethtool calls issued immediately after ip link set up can also
race ahead of state_work and see pl->phydev == NULL.  Calling
phy_mux_select(mux, phy_mux_choose_port(mux)) synchronously in
phy_mux_start() under the caller's held RTNL lock avoids both issues and
the extra kworker wakeup.

[ ... ]

> @@ -502,6 +672,8 @@ void phy_mux_free(struct phy_mux *mux)
>  	struct phy_mux_port *port;
>  	unsigned long id;
>  
> +	disable_work_sync(&mux->state_work);
> +
>  	xa_for_each(&mux->ports, id, port)
>  		phy_mux_port_destroy(port);

[Severity: Medium]
Can phy_mux_detach() race with phy_mux_unregister(), phy_mux_free(), and
fwnode_phy_mux_attach()?

fwnode_phy_mux_attach() holds phy_mux_mutex without requiring rtnl_lock(),
phy_mux_unregister() drops phy_mux_mutex and holds only rtnl_lock(), and
phy_mux_detach() acquires neither lock (and is permitted without RTNL when
the mux is stopped, as in phylink_destroy()).  Because mux->devlink uses
DL_FLAG_STATELESS and struct phy_mux has no reference count, unbinding the
MAC device and mux device concurrently can interleave:

CPU0                              CPU1
phy_mux_detach(mux)
  if (!mux->netdev) -> false
  ...
                                  phy_mux_unregister(mux)
                                    rtnl_lock()
                                    mux->pops->detach(...)
                                    rtnl_unlock()
                                  phy_mux_free(mux)
                                    kfree(mux)
  mux->netdev = NULL  <-- use-after-free

Depending on timing, CPU1 can also trigger a double detach (double
phy_detach() and device_link_del()), or dereference a NULL mux->pops or
freed parent_priv if CPU0 clears them after CPU1 checks them.  Concurrent
fwnode_phy_mux_attach() and phy_mux_detach() are similarly unsynchronized
on mux->netdev and port PHY bindings.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204110622.1106492-1-maxime.chevallier@bootlin.com?part=6

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 09/12] net: phylink: Support PHYs reached through a phy_mux
  2026-12-04 11:06 ` [PATCH net-next 09/12] net: phylink: Support PHYs reached through a phy_mux Maxime Chevallier
@ 2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: sashiko-bot @ 2026-12-05 11:08 UTC (permalink / raw)
  To: Maxime Chevallier; +Cc: devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] net: phylink: phylink_mux_select() configures suspended PHY
- [High] net: phylink: phylink_resolve() left enabled across phy_mux
  port transitions
- [Medium] net: phylink: missing rtnl_lock() in phylink_destroy() mux
  detach
- [Medium] net: phylink: phy_mux_stop() in phylink_stop() breaks PM WoL
  and RXC resume
- [Medium] net: phylink: unlocked pl->mux read in
  phylink_fwnode_phy_connect()
- [Medium] net: phylink: armed PHY IRQ in PHY_HALTED races with mux
  select/deselect
--

commit a4afcb522e18328b8fbf2ddefbad3fcd4d9619af
Author: Maxime Chevallier <maxime.chevallier@bootlin.com>

net: phylink: Support PHYs reached through a phy_mux

This commit adds phy_mux parent support to phylink so that MACs referencing a
mii-mux-handle attach to the mux in phylink_fwnode_phy_connect() and manage
PHY port selection through phy_mux_parent_ops.

> diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
> index 03e255f5ceb17..702614adcb945 100644
> --- a/drivers/net/phy/phylink.c
> +++ b/drivers/net/phy/phylink.c

[ ... ]

> @@ -2141,6 +2143,15 @@ struct phylink *phylink_create(struct phylink_config *config,
>  }
>  EXPORT_SYMBOL_GPL(phylink_create);
>  
> +static void phylink_detach_mux(struct phylink *pl)
> +{
> +	if (!pl->mux)
> +		return;
> +
> +	phy_mux_detach(pl->mux);
> +	pl->mux = NULL;
> +}
> +
>  /**
>   * phylink_destroy() - cleanup and destroy the phylink instance
>   * @pl: a pointer to a &struct phylink returned from phylink_create()
> @@ -2153,6 +2164,7 @@ EXPORT_SYMBOL_GPL(phylink_create);
>  void phylink_destroy(struct phylink *pl)
>  {
>  	sfp_bus_del_upstream(pl->sfp_bus);
> +	phylink_detach_mux(pl);
>  	phylink_destroy_sfp_cage_port(pl);
>  	if (pl->link_gpio)
>  		gpiod_put(pl->link_gpio);

[Severity: Medium]
Can calling phylink_detach_mux() here without rtnl_lock() race with
phy_mux_unregister() or phy_mux_state_machine()?

phylink_destroy() is called without rtnl_lock() held, unlike both neighboring
calls (sfp_bus_del_upstream() and phylink_destroy_sfp_cage_port()), which
acquire it internally.  In drivers/net/phy/phy_mux.c, phy_mux_unregister() and
phy_mux_state_machine() rely on rtnl_lock() to serialize mux teardown (and
phy_mux_stop() only calls non-synchronous cancel_work(&mux->state_work)).

CPU0                              CPU1
phylink_destroy()                 phy_mux_unregister()
  phylink_detach_mux()              rtnl_lock()
    if (!pl->mux)                     mux->pops->detach()
                                        phylink_detach_mux()
                                          phy_mux_detach(mux)
                                          pl->mux = NULL
    phy_mux_detach(pl->mux)         rtnl_unlock()
      <-- double detach / UAF       phy_mux_free(mux)

If phylink_destroy() runs while pl->mux is still attached, this can double
detach or use-after-free pl or mux, and if the mux is still started,
phy_mux_detach() -> phy_mux_stop() -> phylink_mux_deselect() ->
phylink_clear_phydev() also hits ASSERT_RTNL().  Acquiring rtnl_lock() around
phylink_detach_mux(pl) in phylink_destroy() (and asserting ASSERT_RTNL() in
phylink_detach_mux()) would close this.

[ ... ]

> @@ -2516,6 +2560,13 @@ int phylink_fwnode_phy_connect(struct phylink *pl,
>  	if (!phylink_expects_phy(pl))
>  		return 0;
>  
> +	ret = phylink_attach_mux(pl, fwnode, flags);
> +	if (ret)
> +		return ret;
> +
> +	if (pl->mux)
> +		return 0;
> +
>  	phy_fwnode = fwnode_get_phy_node(fwnode);
>  	if (IS_ERR(phy_fwnode)) {
>  		/* PHY mode requires a PHY to be specified. */

[Severity: Medium]
Can this unlocked read of pl->mux race with a concurrent phy_mux_unregister()?

Callers such as dsa_user_create() invoke phylink_fwnode_phy_connect() without
rtnl_lock().  fwnode_phy_mux_attach() releases phy_mux_mutex before returning:

CPU0                              CPU1
phylink_fwnode_phy_connect()
  phylink_attach_mux()
    fwnode_phy_mux_attach()
      mutex_lock(&phy_mux_mutex)
      pl->mux = mux
      mutex_unlock(&phy_mux_mutex)
                                  phy_mux_unregister()
                                    rtnl_lock()
                                    phylink_mux_detach()
                                      pl->mux = NULL
                                    rtnl_unlock()
  if (pl->mux)  <-- sees NULL
    return 0;
  fwnode_get_phy_node()

If phy_mux_unregister() clears pl->mux in that window, this falls through to
fwnode_get_phy_node() as if no mux existed, returning -ENODEV in MLO_AN_PHY
mode or returning 0 with neither mux nor PHY in MLO_AN_INBAND mode.

[ ... ]

> @@ -2708,6 +2766,8 @@ void phylink_start(struct phylink *pl)
>  		phy_start(pl->phydev);
>  	if (pl->sfp_bus)
>  		sfp_upstream_start(pl->sfp_bus);
> +	if (pl->mux)
> +		phy_mux_start(pl->mux);
>  }
>  EXPORT_SYMBOL_GPL(phylink_start);

[Severity: High]
Does leaving phylink_resolve() enabled while no mux port is selected allow
spurious link-up and unlocked reconfiguration in MLO_AN_INBAND mode?

phylink_start() clears PHYLINK_DISABLE_STOPPED and queues pl->resolve before
phy_mux_start() schedules mux->state_work (which blocks on rtnl_lock()).
phylink_mux_deselect() also clears pl->phydev without setting a
PHYLINK_DISABLE_* bit, so the retriggered resolve work runs with pl->phydev
NULL.

CPU0                              kworker
phylink_start()
  phylink_enable_and_run_resolve()
    queue_work(pl->resolve)
  phy_mux_start(pl->mux)
    schedule_work(mux->state_work)
                                  phylink_resolve()
                                    /* pl->phydev == NULL */
                                    phylink_mac_pcs_get_state()
                                    phylink_link_up()  <-- no port selected

In MLO_AN_INBAND mode, phylink_resolve() skips gating on pl->phy_state.link
when pl->phydev is NULL, so a live PCS link triggers phylink_link_up() with
no mux port selected.

When phylink_mux_select() then runs, calling phylink_mac_initial_config() ->
phylink_major_config() while pl->old_link_state is true reconfigures the MAC
and PCS without a preceding phylink_link_down().

Because phylink_resolve() is still enabled, phylink_mux_select() also writes
pl->link_config.interface without pl->state_mutex or phy->lock, and
phylink_mac_initial_config() reads pl->req_link_an_mode, pl->phy_state, and
pl->link_config before taking phy->lock, racing with a concurrent resolve.

> @@ -2727,6 +2787,8 @@ void phylink_stop(struct phylink *pl)
>  {
>  	ASSERT_RTNL();
>  
> +	if (pl->mux)
> +		phy_mux_stop(pl->mux);
>  	if (pl->sfp_bus)
>  		sfp_upstream_stop(pl->sfp_bus);
>  	if (pl->phydev)

[Severity: Medium]
Does calling phy_mux_stop() in phylink_stop() break PHY Wake-on-LAN and
mac_requires_rxc resume?

phy_mux_stop() deselects the active port via phylink_mux_deselect(), which
clears pl->phydev to NULL and frees the PHY interrupt.  In
phylink_suspend(pl, false), phylink_stop() runs before
phylink_phy_pm_speed_ctrl() checks pl->phydev, so phylink_speed_down() (and
phylink_speed_up() on resume) is always skipped, and freeing the PHY IRQ
breaks IRQ-based PHY Wake-on-LAN wakeups.

Clearing pl->phydev also turns phylink_prepare_resume() into a no-op, so MACs
with mac_requires_rxc (such as stmmac in __stmmac_open()) cannot wake the
suspended PHY to restore RXC before DMA reset.

[ ... ]

> +static int phylink_mux_select(struct phy_mux *mux, struct phy_mux_port *port,
> +			      void *priv)
> +{
> +	struct phylink *pl = priv;
> +	struct phy_device *phy;
> +	int ret;
> +
> +	if (port->type != PHY_MUX_PORT_PHY)
> +		return -EOPNOTSUPP;
> +
> +	if (pl->phydev)
> +		return -EBUSY;
> +
> +	phy = port->phydev;
> +
> +	ret = phylink_bringup_phy(pl, phy, port->interface);
> +	if (ret) {
> +		phy->phylink = NULL;
> +		return ret;
> +	}
> +
> +	pl->link_config.interface = port->interface;
> +	phylink_mac_initial_config(pl, false);
> +	phy_start(phy);
> +
> +	return 0;
> +}

[Severity: High]
Isn't port->phydev still suspended when phylink_bringup_phy() and
phylink_mac_initial_config() configure it here?

phy_mux_attach() and port deselection both suspend PHY ports via
phy_mux_phy_power_down() -> phy_suspend(), and phy_mux does not call
phy_resume() on selection.  phylink_bringup_phy() performs MDIO accesses for
EEE and enables interrupts via phy_request_interrupt(), and
phylink_major_config() calls phy_config_inband(), all before phy_start(phy)
resumes the PHY.

On PHY drivers that ignore non-BMCR writes in power-down or reset registers on
resume (such as bcm54xx_resume() in drivers/net/phy/broadcom.c or
kszphy_resume() in drivers/net/phy/micrel.c), interrupt and EEE configuration
can be silently discarded or fail, leaving hardware IRQs masked while polling
is disabled so the link never comes up.  Resuming the PHY before
phylink_bringup_phy() (and re-suspending on error), or resuming the port in
phy_mux before pops->select(), would avoid this.

> +static void phylink_mux_deselect(struct phy_mux *mux,
> +				 struct phy_mux_port *port, void *priv)
> +{
> +	struct phylink *pl = priv;
> +	struct phy_device *phy;
> +
> +	if (pl->phydev != port->phydev)
> +		return;
> +
> +	phy = port->phydev;
> +
> +	if (phy_is_started(phy) || phy->state == PHY_ERROR)
> +		phy_stop(phy);
> +
> +	phylink_clear_phydev(pl);
> +
> +	if (phy_interrupt_is_valid(phy))
> +		phy_free_interrupt(phy);
> +
> +	phy->phylink = NULL;
> +}

[Severity: Medium]
Can a PHY or shared interrupt firing while phy->state is PHY_HALTED re-suspend
the PHY after phy_start() or race with teardown?

Because a muxed PHY stays attached across port switches, phy_stop() leaves it
in PHY_HALTED rather than PHY_READY.  In phylink_mux_deselect(), phy_stop()
cancels phy->state_queue before phylink_clear_phydev() flushes pl->resolve and
before phy_free_interrupt() frees the IRQ, without canceling phy->state_queue
afterward.  On re-selection in phylink_mux_select(), phylink_bringup_phy()
also calls phy_request_interrupt() while phy->state is still PHY_HALTED.

CPU0                              kworker
phylink_mux_select()
  phylink_bringup_phy()
    phy_request_interrupt(phy)
    /* IRQ fires in PHY_HALTED */
                                  phy_state_machine()
                                    mutex_lock(&phy->lock)
                                    _phy_state_machine() -> WORK_SUSPEND
                                    mutex_unlock(&phy->lock)
  phy_start(phy)
    mutex_lock(&phy->lock)
    __phy_resume(phy)
    phy->state = PHY_UP
    mutex_unlock(&phy->lock)
                                    phy_suspend(phy)  <-- re-suspends PHY

Because phy_state_machine() drops phy->lock before
_phy_state_machine_post_work() calls phy_suspend(), a stale PHY_HALTED work
item can re-set BMCR_PDOWN and phy->suspended after phy_start() resumes the
PHY, leaving the link stuck down until another stop/start cycle.  During
deselection, the queued work can also race with phy_mux_phy_power_down() or
phy_detach().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204110622.1106492-1-maxime.chevallier@bootlin.com?part=9

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 10/12] net: phylink: Support an SFP cage behind a phy_mux
  2026-12-04 11:06 ` [PATCH net-next 10/12] net: phylink: Support an SFP cage behind " Maxime Chevallier
@ 2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: sashiko-bot @ 2026-12-05 11:08 UTC (permalink / raw)
  To: Maxime Chevallier; +Cc: devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Medium] net: phylink: stale sfp_link_down after stop/remove/detach
  breaks LOS gating
- [Medium] net: phylink: phylink_start() runs resolve before mux selects
  SFP port
- [Medium] net: phylink: phylink_mux_select() clears DISABLE_LINK before
  reconfiguring
- [Medium] net: phylink: missing sfp_mod_present rollback on
  may_have_phy module failures
- [Medium] net: phylink: phylink_mux_deselect() does not bring down
  active SFP link
- [Medium] net: phylink: data race on mux_sfp_port in
  phylink_mux_attach()
- [Medium] net: phylink: missing rtnl_lock() in phylink_destroy() mux
  detach
--

commit 0d6f64bddfb5d3b2e43faf1b89e62b3654b6ad2b
Author: Maxime Chevallier <maxime.chevallier@bootlin.com>

net: phylink: Support an SFP cage behind a phy_mux

This commit teaches phylink to discover an SFP cage described on a
phy_mux port, report module availability to the mux, and handle
selecting and deselecting the SFP port.

> diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
> index 702614adcb945..6d62bd37f95a6 100644
> --- a/drivers/net/phy/phylink.c
> +++ b/drivers/net/phy/phylink.c

[ ... ]

> @@ -2150,6 +2179,7 @@ static void phylink_detach_mux(struct phylink *pl)
>  
>  	phy_mux_detach(pl->mux);
>  	pl->mux = NULL;
> +	pl->mux_sfp_port = NULL;
>  }

[Severity: Medium]
Can phylink_destroy() race with phy_mux_state_machine() and cause a
use-after-free of pl when calling phylink_detach_mux(pl) without
rtnl_lock()?

phylink_destroy() runs without rtnl_lock() and calls
sfp_bus_del_upstream(pl->sfp_bus) followed by phylink_detach_mux(pl)
and kfree(pl).  With this patch, phylink_sfp_detach() calls
phy_mux_port_set_available(pl->mux_sfp_port, false), which queues
mux->state_work right before sfp_bus_del_upstream() drops rtnl_lock().
Because phy_mux_stop() inside phy_mux_detach() uses cancel_work()
rather than cancel_work_sync(), phylink_destroy() can free pl while
the worker is still running:

CPU0                                CPU1 (kworker)
phylink_destroy()
  sfp_bus_del_upstream()
    rtnl_lock()
    phylink_sfp_detach()
      phy_mux_port_set_available(..., false)
        phy_mux_trigger(mux)
    rtnl_unlock()
                                    phy_mux_state_machine()
                                      rtnl_lock()
  phylink_detach_mux(pl)              phy_mux_select(...)
    phy_mux_detach(pl->mux)
      phy_mux_stop(mux)
        cancel_work(&mux->state_work)
  kfree(pl)
                                        mux->pops->select(..., pl)
                                          <-- use-after-free of pl

Drivers such as ucc_geth (ucc_geth_remove()) or lan743x (error path in
lan743x_netdev_open()) can reach phylink_destroy() with pl->mux still
attached.  Independently, phy_mux_unregister() can also run
phylink_detach_mux(pl) concurrently under rtnl_lock(), risking double
phy_detach() / device_link_del() or use-after-free.  Wrapping
phylink_detach_mux(pl) in rtnl_lock() / rtnl_unlock() inside
phylink_destroy() would serialize both paths before kfree(pl).

[ ... ]

> @@ -2766,6 +2796,9 @@ void phylink_start(struct phylink *pl)
>  		phy_start(pl->phydev);
>  	if (pl->sfp_bus)
>  		sfp_upstream_start(pl->sfp_bus);
> +	if (pl->mux_sfp_port)
> +		phy_mux_port_set_available(pl->mux_sfp_port,
> +					   pl->sfp_mod_present);
>  	if (pl->mux)
>  		phy_mux_start(pl->mux);
>  }

[Severity: Medium]
Can phylink_resolve() bring the link up prematurely here before the mux
selects the SFP port?

Earlier in phylink_start(), phylink_enable_and_run_resolve(pl,
PHYLINK_DISABLE_STOPPED) clears PHYLINK_DISABLE_STOPPED and schedules
pl->resolve.  Because phylink_sfp_link_down() returns early without
setting PHYLINK_DISABLE_LINK while !pl->sfp_selected,
pl->phylink_disable_state becomes 0.

When an SFP module is already inserted with pl->sfp_link_down true,
phy_mux_port_set_available() runs while mux->state is still
PHY_MUX_STOPPED, so phy_mux_start() defers port selection to
phy_mux_state_machine().  That worker must wait for rtnl_lock(),
whereas phylink_resolve() does not:

CPU0 (under rtnl_lock)              CPU1 (kworker)
phylink_start()
  phylink_enable_and_run_resolve(
      pl, PHYLINK_DISABLE_STOPPED)
  phy_mux_port_set_available(...)
  phy_mux_start(pl->mux)
    phy_mux_trigger(mux)
                                    phylink_resolve()
                                      phylink_disable_state == 0
                                      <-- premature link-up
rtnl_unlock()
                                    phy_mux_state_machine()
                                      rtnl_lock()
                                      phylink_mux_select(SFP)
                                        set PHYLINK_DISABLE_LINK

This allows a spurious carrier-up and flap before SFP startup and LOS
checks complete.

[ ... ]

> @@ -4096,18 +4143,27 @@ static int phylink_sfp_module_insert(void *upstream,
>  	pl->sfp_may_have_phy = caps->may_have_phy;
>  	pl->sfp_port = caps->port;
>  
> +	phylink_sfp_set_present(pl, true);
> +
>  	/* If this module may have a PHY connecting later, defer until later */
>  	if (pl->sfp_may_have_phy)
>  		return 0;
>  
> -	return phylink_sfp_config_optical(pl);
> +	ret = phylink_sfp_config_optical(pl);
> +	if (ret)
> +		phylink_sfp_set_present(pl, false);
> +
> +	return ret;
>  }

[Severity: Medium]
Does this leave pl->sfp_mod_present and pl->mux_sfp_port->available
stuck true when a may_have_phy module fails deferred setup?

For copper or other may_have_phy modules, validation is deferred to
phylink_sfp_connect_phy() or phylink_sfp_module_start().  Neither
function calls phylink_sfp_set_present(pl, false) on failure (for
example when !phy->drv or phylink_sfp_config_optical() fails), and
sfp_sm_main() transitions straight to SFP_S_FAIL without calling
sfp_module_remove().  That leaves the mux stuck on the rejected SFP
port until the module is physically removed.

Also, calling phylink_sfp_set_present(pl, true) before
phylink_sfp_config_optical() synchronously switches the mux and stops
an active PHY port before discovering that an optical module is
unsupported, causing an unnecessary link bounce when it rolls back.

[ ... ]

> @@ -4268,6 +4332,7 @@ static void phylink_mux_attach(struct phy_mux *mux, void *priv)
>  		phy_mux_set_mac_managed_pm(mux);
>  
>  	pl->mux = mux;
> +	pl->mux_sfp_port = phy_mux_get_sfp_port(mux);
>  }

[Severity: Medium]
Is there a data race on pl->mux_sfp_port between phylink_mux_attach()
and the SFP callbacks?

phylink_create() registers the SFP upstream before
phylink_of_phy_connect() attaches the mux, and probe-time callers such
as dsa_user_create() and ocelot_port_phylink_create() call
phylink_of_phy_connect() without holding rtnl_lock().  In that window,
phylink_mux_attach() writes pl->mux_sfp_port under phy_mux_mutex while
phylink_sfp_set_present() and phylink_sfp_detach() read it under
rtnl_lock():

CPU0 (probe, phy_mux_mutex)         CPU1 (SFP work, rtnl_lock)
phylink_of_phy_connect()
  phylink_attach_mux()
    phylink_mux_attach()
      pl->mux_sfp_port = ...        phylink_sfp_module_insert()
                                      phylink_sfp_set_present()
                                        if (pl->mux_sfp_port)
                                          <-- data race

> @@ -4277,6 +4342,19 @@ static int phylink_mux_select(struct phy_mux *mux, struct phy_mux_port *port,
>  	struct phy_device *phy;
>  	int ret;
>  
> +	if (port->type == PHY_MUX_PORT_SFP) {
> +		if (!pl->sfp_bus)
> +			return -ENODEV;
> +
> +		pl->sfp_selected = true;
> +		if (pl->sfp_link_down)
> +			phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
> +		else
> +			phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK);
> +
> +		return 0;
> +	}
> +
>  	if (port->type != PHY_MUX_PORT_PHY)
>  		return -EOPNOTSUPP;

[Severity: Medium]
Can pl->sfp_link_down stay stale false after phylink_stop(),
phylink_sfp_module_remove(), or phylink_sfp_detach(), bypassing SFP
LOS link gating?

Once an SFP link comes up, phylink_sfp_link_up() clears
pl->sfp_link_down.  When phylink_stop() calls sfp_upstream_stop(),
sfp_sm_device() changes sm_dev_state to SFP_DEV_DOWN before
sfp_sm_main() runs, so sfp_sm_main() skips sfp_sm_link_down() (which
is gated on sm_dev_state == SFP_DEV_UP) while resetting sm_state to
SFP_S_DOWN.  Neither phylink_stop(), phylink_sfp_module_remove(), nor
phylink_sfp_detach() resets pl->sfp_link_down = true.

If the interface is later reopened with RX_LOS asserted or a new
module inserted, phylink_mux_select() sees stale pl->sfp_link_down ==
false and clears PHYLINK_DISABLE_LINK.  Because
sfp_sm_link_check_los() moves straight to SFP_S_WAIT_LOS without
calling sfp_sm_link_down(), PHYLINK_DISABLE_LINK remains cleared and
carrier can come up while LOS is asserted.

> @@ -4291,7 +4369,10 @@ static int phylink_mux_select(struct phy_mux *mux, struct phy_mux_port *port,
>  		return ret;
>  	}
>  
> +	clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
> +
>  	pl->link_config.interface = port->interface;
> +	pl->req_link_an_mode = pl->cfg_link_an_mode;
>  	phylink_mac_initial_config(pl, false);
>  	phy_start(phy);

[Severity: Medium]
Should PHYLINK_DISABLE_LINK be cleared after
phylink_mac_initial_config() rather than before it?

Once PHYLINK_DISABLE_LINK is cleared here, any MAC, PCS, or PHY
interrupt can schedule phylink_resolve() on another CPU without
rtnl_lock().  Because pl->link_config.interface and
pl->req_link_an_mode are updated without pl->state_mutex,
phylink_resolve() can race with those writes and poll the PCS under
stale SFP act_link_an_mode and interface settings before
phylink_mac_initial_config() reconfigures the MAC/PCS.  Updating the
config under pl->state_mutex and clearing PHYLINK_DISABLE_LINK after
phylink_mac_initial_config(pl, false) would close this window.

> @@ -4304,7 +4385,10 @@ static void phylink_mux_deselect(struct phy_mux *mux,
>  	struct phy_device *phy;
>  
> -	if (pl->phydev != port->phydev)
> +	if (port->type == PHY_MUX_PORT_SFP)
> +		pl->sfp_selected = false;
> +
> +	if (port->type != PHY_MUX_PORT_PHY || pl->phydev != port->phydev)
>  		return;

[Severity: Medium]
Should phylink_mux_deselect() bring the link down when deselecting
PHY_MUX_PORT_SFP?

Unbinding the phy_mux driver while the SFP link is up calls
phy_mux_unregister() -> phylink_mux_detach() -> phy_mux_stop() ->
phylink_mux_deselect() without stopping the netdev.  Because
phylink_mux_deselect() only clears pl->sfp_selected without calling
phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK), the link
stays up and later phylink_sfp_link_down() calls return early while
pl->sfp_behind_mux && !pl->sfp_selected, leaving carrier stuck up.

Unbinding the sfp driver while an optical link is up similarly leaves
the link up across the mux switch, so phylink_mux_select() calls
phylink_mac_initial_config() without mac_link_down() first.  Also, if
a copper SFP PHY is still attached when deselected, pl->phydev remains
populated and selecting a PHY port fails with -EBUSY.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204110622.1106492-1-maxime.chevallier@bootlin.com?part=10

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 11/12] net: phy: mux: Add the mii-mux-auto driver
  2026-12-04 11:06 ` [PATCH net-next 11/12] net: phy: mux: Add the mii-mux-auto driver Maxime Chevallier
@ 2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: sashiko-bot @ 2026-12-05 11:08 UTC (permalink / raw)
  To: Maxime Chevallier; +Cc: devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] net: phy: mux: unsynchronized mii_mux_auto_remove() vs
  phy_mux_detach()
- [Medium] net: phy: unsynchronized pl->mux access and detach vs
  phy_mux_unregister()
--

commit 3ddf01a7f8c108024edc40420dc253bccef488c1
Author: Maxime Chevallier <maxime.chevallier@bootlin.com>

net: phy: mux: Add the mii-mux-auto driver

This commit adds the mii-mux-auto platform driver for hardware-switched
two-port MII multiplexers, validating the port fwnodes, registering the mux
with the phy_mux core, and unregistering and freeing it on removal.

> diff --git a/drivers/net/phy/mux/mii_mux_auto.c b/drivers/net/phy/mux/mii_mux_auto.c
> new file mode 100644
> index 0000000000000..980cc2c242ba7
> --- /dev/null
> +++ b/drivers/net/phy/mux/mii_mux_auto.c
> @@ -0,0 +1,99 @@

[ ... ]

> +static void mii_mux_auto_remove(struct platform_device *pdev)
> +{
> +	struct phy_mux *mux = platform_get_drvdata(pdev);
> +
> +	phy_mux_unregister(mux);
> +	phy_mux_free(mux);
> +}

[Severity: Medium]
Can mii_mux_auto_remove() race with an unlocked phy_mux_detach() and cause a
use-after-free on struct phy_mux or struct phylink?

phy_mux_attach() adds a DL_FLAG_STATELESS device link, which does not force
consumers to unbind when mii-mux-auto is removed.  phy_mux_unregister() only
holds rtnl_lock() around mux->pops->detach(), and phy_mux_free() immediately
destroys the ports and frees mux.

phy_mux_detach() takes neither rtnl_lock() nor phy_mux_mutex.
phylink_destroy() calls phylink_detach_mux() -> phy_mux_detach() unlocked
before freeing pl, which is reachable with pl->mux set if mvpp2_open() fails
in request_irq() and mvpp2_port_remove() later runs:

CPU0                                CPU1
mii_mux_auto_remove()               mvpp2_port_remove()
  phy_mux_unregister(mux)             phylink_destroy(pl)
    rtnl_lock()                         phylink_detach_mux(pl)
    mux->pops->detach(mux, pl)            phy_mux_detach(mux)
    phy_mux_stop(mux)
    rtnl_unlock()
  phy_mux_free(mux)
    phy_mux_port_destroy(port)
    kfree(mux)
                                          phy_mux_detach_phys(mux)
                                            <-- use-after-free

In the reverse ordering, phylink_destroy() can free pl while
phy_mux_unregister() calls mux->pops->detach(mux, pl), or both paths can
double-detach the PHYs and devlink.  Using a managed device link or
serializing teardown with phy_mux_mutex would prevent this.

[Severity: Medium]
Can mii_mux_auto_remove() also race with the unlocked pl->mux check in
phylink_fwnode_phy_connect()?

Callers such as ocelot_port_phylink_create() and dsa_user_phy_setup() invoke
phylink_of_phy_connect() -> phylink_fwnode_phy_connect() without holding
rtnl_lock().  phylink_attach_mux() sets pl->mux under phy_mux_mutex and then
drops it before phylink_fwnode_phy_connect() checks if (pl->mux):

CPU0                                CPU1
phylink_fwnode_phy_connect()        mii_mux_auto_remove()
  phylink_attach_mux()                phy_mux_unregister(mux)
    mutex_lock(&phy_mux_mutex)
    pl->mux = mux
    mutex_unlock(&phy_mux_mutex)
                                        mutex_lock(&phy_mux_mutex)
                                        list_del(&mux->node)
                                        mutex_unlock(&phy_mux_mutex)
                                        rtnl_lock()
                                        phylink_mux_detach()
                                          pl->mux = NULL
                                        rtnl_unlock()
  if (pl->mux)  <-- reads NULL
  fwnode_get_phy_node(fwnode)

Because pl->mux is read as NULL, phylink_fwnode_phy_connect() falls through
to fwnode_get_phy_node() and can fail probe with -ENODEV, return 0 without
an attached mux or PHY, or attach a fallback phy-handle while
pl->sfp_behind_mux remains set.

Separately, phylink_destroy() calls phylink_detach_mux() unlocked before
kfree(pl), which can race with mii_mux_auto_remove() -> phy_mux_unregister()
and phy_mux_free() to cause a use-after-free on mux or pl.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204110622.1106492-1-maxime.chevallier@bootlin.com?part=11

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 12/12] ARM: dts: turris-omnia: Describe the WAN port multiplexer
  2026-12-04 11:06 ` [PATCH net-next 12/12] ARM: dts: turris-omnia: Describe the WAN port multiplexer Maxime Chevallier
@ 2026-12-05 11:08   ` sashiko-bot
  2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: sashiko-bot @ 2026-12-05 11:08 UTC (permalink / raw)
  To: Maxime Chevallier; +Cc: devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] ARM: dts: turris-omnia: removing eth2 sfp breaks WAN SFP when
  PHY_MUX=n
- [Medium] ARM: dts: turris-omnia: mii-mux-handle fails mvneta_open() if
  mux absent
- [Medium] ARM: dts: turris-omnia: wan_mux breaks mvneta_mdio_probe()
  WoL and speed_up
--

commit 6cfa2117998086601c4b50faf6694fb1b3114fac
Author: Maxime Chevallier <maxime.chevallier@bootlin.com>

ARM: dts: turris-omnia: Describe the WAN port multiplexer

This commit describes the Turris Omnia WAN port MII multiplexer in
devicetree, enabling the SFP node unconditionally and wiring eth2 to
the mux instead of referencing the SFP node directly.

> diff --git a/arch/arm/boot/dts/marvell/armada-385-turris-omnia.dts b/arch/arm/boot/dts/marvell/armada-385-turris-omnia.dts
> index 83fe00abd6523..0501686e26427 100644
> --- a/arch/arm/boot/dts/marvell/armada-385-turris-omnia.dts
> +++ b/arch/arm/boot/dts/marvell/armada-385-turris-omnia.dts
> @@ -103,13 +103,24 @@ sfp: sfp {

[ ... ]

> +	wan_mux: mii-mux {
> +		compatible = "mii-mux-auto";

[Severity: Medium]
Can adding this mii-mux-auto node break eth2 bring-up when
CONFIG_MII_MUX_AUTO is disabled or built as a module?

With CONFIG_PHY_MUX=y, mvneta_open() -> phylink_of_phy_connect() ->
phylink_attach_mux() calls fwnode_phy_mux_attach().  If mii_mux_auto is
not built or not yet probed (such as during IP_PNP DHCP with
CONFIG_MII_MUX_AUTO=m), fwnode_phy_mux_attach() returns -EPROBE_DEFER.

phylink_attach_mux() turns -EPROBE_DEFER into -ENODEV without falling
back to phy-handle, and drivers/of/property.c has no
of_supplier_bindings[] entry for mii-mux-handle to order probing via
fw_devlink.  As a result, mvneta_open() fails even on copper RJ45.

[ ... ]

> @@ -199,16 +210,12 @@ &eth2 {

[ ... ]

>  	status = "okay";
>  	phy-mode = "sgmii";
>  	phy-handle = <&phy1>;
> +	mii-mux-handle = <&wan_mux>;

[Severity: Medium]
Does attaching eth2 through wan_mux break Wake-on-LAN and
phylink_speed_up() in mvneta?

With mii-mux-handle, pl->phydev stays NULL after phylink_of_phy_connect()
until phy_mux_state_machine() asynchronously selects port@1 after
phylink_start().  mvneta_mdio_probe() calls phylink_ethtool_get_wol()
before phylink_start(), so wol.supported is 0 and can_wakeup is cleared.

Because mvneta never re-enables can_wakeup after port selection,
mvneta_ethtool_set_wol() -> device_set_wakeup_enable() silently fails
with -EINVAL.  This leaves device_may_wakeup() permanently false and
prevents mvneta_stop_dev() from calling phylink_speed_down().

In addition, mvneta_start_dev() calls phylink_speed_up() right after
phylink_start(), before mux->state_work populates pl->phydev, turning
phylink_speed_up() into a no-op.

>  	phys = <&comphy5 2>;
> -	sfp = <&sfp>;

[Severity: High]
Does removing sfp = <&sfp> from eth2 regress Turris Omnia WAN SFP
networking when CONFIG_PHY_MUX=n or when an older kernel boots this DTB?

Neither CONFIG_PHY_MUX nor CONFIG_MII_MUX_AUTO defaults to y or is
enabled in mvebu_v7_defconfig or multi_v7_defconfig.  When
CONFIG_PHY_MUX=n, phylink_register_mux_sfp() gets NULL from the stub
fwnode_phy_mux_find_sfp_port() and falls back to phylink_register_sfp().

With sfp = <&sfp> deleted from eth2, pl->sfp_bus stays NULL while the
enabled sfp node probes unattached with TX_DISABLE asserted, breaking
WAN SFP networking even with U-Boot's boot-time FDT fixup.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204110622.1106492-1-maxime.chevallier@bootlin.com?part=12

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 01/12] dt-bindings: net: Add Ethernet MII multiplexers
  2026-12-04 11:06 ` [PATCH net-next 01/12] dt-bindings: net: Add Ethernet " Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
@ 2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 12:18 UTC (permalink / raw)
  To: maxime.chevallier
  Cc: davem, andrew+netdev, kuba, edumazet, willemb, pabeni, linux,
	kabel, sebastian.hesselbarth, gregory.clement, hkallweit1, netdev,
	linux-kernel, linux-arm-kernel, thomas.petazzoni, devicetree,
	conor+dt, krzk+dt, robh, christophe.leroy, herve.codina,
	f.fainelli, vladimir.oltean, kory.maincent, o.rempel, nicveronese,
	horms, marcin.s.wojtas, daniel, dimitri.fedrau, frank.wunderlich,
	p.ameruoso, f

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] In mii-mux-auto.yaml, the description says the mux has two ports
  and that exactly one of them takes precedence, but the schema enforces…
- [Low] ethernet-controller.yaml adds `mii-mux-handle` (lines 103-107) as
  a bare phandle.
- [Low] mii-mux-auto.yaml defines `takes-precedence` (lines 29-33) as
  "The multiplexer switches to this port by itself as soon as the port…

--

dt-bindings: net: Add Ethernet MII multiplexers

This adds mii-mux.yaml, a generic binding for an Ethernet MII
multiplexer and its ports. It also adds mii-mux-auto.yaml for two-port
muxes that the hardware switches by itself. A new mii-mux-handle
property in ethernet-controller.yaml lets a MAC point to the mux node.

> diff --git a/Documentation/devicetree/bindings/net/ethernet-controller.yaml b/Documentation/devicetree/bindings/net/ethernet-controller.yaml
> index 30b2a335116e9..e2836351cd7ad 100644
> --- a/Documentation/devicetree/bindings/net/ethernet-controller.yaml
> +++ b/Documentation/devicetree/bindings/net/ethernet-controller.yaml
> @@ -100,6 +100,12 @@ properties:
>      description:
>        Specifies a reference to a node representing a PHY device.
>  
> +  mii-mux-handle:
> +    $ref: /schemas/types.yaml#/definitions/phandle
> +    description:
> +      Specifies a reference to a node representing an Ethernet MII
> +      multiplexer.
> +

[Severity: Low]
How should mii-mux-handle interact with the MAC-level phy-handle, sfp,
fixed-link, managed and phy-mode properties?

Nothing here stops those properties from appearing next to
mii-mux-handle. The description also doesn't say which one wins. The mux
ports in mii-mux.yaml then define phy-handle, phy-mode and sfp again for
the same MAC-to-PHY link.

The mii-mux-auto.yaml example sets phy-mode = "sgmii" on both the MAC
and port@1. What does the MAC-level mode mean when the SFP port@0 is
selected? The port oneOf in mii-mux.yaml forbids phy-mode on that port.

Later in the series, phylink uses a precedence that this binding doesn't
describe. phylink_fwnode_phy_connect() returns early once a mux is
attached:

	if (pl->mux)
		return 0;

	phy_fwnode = fwnode_get_phy_node(fwnode);

But phylink_attach_mux() returns without attaching a mux when the option
is disabled:

	if (pl->mux || !IS_ENABLED(CONFIG_PHY_MUX))
		return 0;

In that case the MAC-level phy-handle is used. The Turris Omnia DTS in
this series depends on this and keeps both on &eth2:

	phy-mode = "sgmii";
	phy-handle = <&phy1>;
	mii-mux-handle = <&wan_mux>;

Could the binding either forbid the conflicting MAC-level link
properties when mii-mux-handle is present, or say which level takes
precedence?

Separately, the RGMII rx/tx-internal-delay-ps conditional in the allOf
of ethernet-controller.yaml only checks the MAC-level phy-mode. Is there
a way to express delays for a port-level rgmii-* mode?

None of the later patches in the series change the binding files.

> diff --git a/Documentation/devicetree/bindings/net/mii-mux-auto.yaml b/Documentation/devicetree/bindings/net/mii-mux-auto.yaml
> new file mode 100644
> index 0000000000000..7a965f6e8dcce
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/net/mii-mux-auto.yaml

[ ... ]

> +patternProperties:
> +  "^port@[0-9a-f]+$":
> +    $ref: mii-mux.yaml#/$defs/port
> +    unevaluatedProperties: false
> +
> +    properties:
> +      takes-precedence:
> +        type: boolean
> +        description:
> +          The multiplexer switches to this port by itself as soon as the
> +          port gets active, whatever the state of the other port.

[Severity: Low]
What does "gets active" mean here?

On the Turris Omnia, which the commit message uses as the example, the
select line is the SFP cage's mod_def0 signal. The hardware therefore
switches on module presence, not on link or LOS state.

For a PHY port, the binding gives "active" no defined meaning. Even so,
the schema accepts takes-precedence on either port, whether that port
has sfp or phy-handle.

The only driver is mii_mux_auto_check_ports() in
drivers/net/phy/mux/mii_mux_auto.c, added later in the series. It only
accepts the flag on the SFP port:

	if (n_sfp != 1 || n_phy != 1 || n_prec != 1 || n_sfp_prec != 1)
		return -EOPNOTSUPP;

So a DT with takes-precedence on the PHY port passes dt_binding_check
but then fails to probe.

Could the binding describe the trigger as module presence on the SFP
port, and only allow takes-precedence on the port that has sfp?

> +
> +required:
> +  - compatible
> +  - port@0
> +  - port@1
> +
> +oneOf:
> +  - properties:
> +      port@0:
> +        required:
> +          - takes-precedence
> +  - properties:
> +      port@1:
> +        required:
> +          - takes-precedence

[Severity: Low]
The description calls this a two-port mux where exactly one port takes
precedence. Does the schema enforce either of those?

The "^port@[0-9a-f]+$" pattern accepts any number of ports. The required
list only asks for port@0 and port@1, and this oneOf only checks
takes-precedence on those two ports.

For example, this node passes dt_binding_check:

    port@0 { reg = <0>; sfp = <&sfp0>; takes-precedence; };
    port@1 { reg = <1>; phy-handle = <&phy0>; phy-mode = "sgmii"; };
    port@2 { reg = <2>; sfp = <&sfp1>; takes-precedence; };

The port@0 branch matches and the port@1 branch fails, so oneOf is
satisfied. port@2 matches patternProperties, so unevaluatedProperties
never triggers.

Later in the series, mii_mux_auto_check_ports() rejects this layout at
probe time with -EOPNOTSUPP. The schema and the driver therefore
disagree.

Would restricting the pattern to "^port@[01]$" for this compatible make
the required list and the oneOf complete?

None of the later patches in the series change the binding files.

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204110622.1106492-1-maxime.chevallier%40bootlin.com

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 03/12] net: phy: Don't create the sysfs links for PHYs behind a mux
  2026-12-04 11:06 ` [PATCH net-next 03/12] net: phy: Don't create the sysfs links for PHYs behind a mux Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
@ 2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 12:18 UTC (permalink / raw)
  To: maxime.chevallier
  Cc: davem, andrew+netdev, kuba, edumazet, willemb, pabeni, linux,
	kabel, sebastian.hesselbarth, gregory.clement, hkallweit1, netdev,
	linux-kernel, linux-arm-kernel, thomas.petazzoni, devicetree,
	conor+dt, krzk+dt, robh, christophe.leroy, herve.codina,
	f.fainelli, vladimir.oltean, kory.maincent, o.rempel, nicveronese,
	horms, marcin.s.wojtas, daniel, dimitri.fedrau, frank.wunderlich,
	p.ameruoso, f

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] For a PHY behind a mux, phy_attach_direct()
  (drivers/net/phy/phy_device.c:2022) now skips phy_sysfs_create_links()…
- [Low] The commit message says that two PHYs' sysfs links in the netdev
  entry would 'overwrite one another'.

--

net: phy: Don't create the sysfs links for PHYs behind a mux

This commit adds a mux_port pointer to struct phy_device. When that
pointer is set, phy_attach_direct() no longer calls
phy_sysfs_create_links(). The reason given is that a mux can attach
several PHYs to one netdev, and their sysfs links would conflict.

> Attached phys create sysfs links in the netdev entry, pointing to the
> PHY. We can't have two of them (otherwise they'd overwrite one another),
> so let's just not create these.

[Severity: Low]
Is "overwrite one another" accurate here? Sysfs links aren't overwritten.
When the name already exists, sysfs_do_create_link_sd() returns -EEXIST
from kernfs_create_link().

For the netdev side "phydev" link, phy_sysfs_create_links() logs that
failure, treats it as non-fatal, and still sets sysfs_links:

    err = sysfs_create_link_nowarn(&dev->dev.kobj,
                                   &phydev->mdio.dev.kobj,
                                   "phydev");
    if (err) {
        dev_err(...);
        /* non-fatal - some net drivers can use one netdevice
         * with more then one phy
         */
    }

    phydev->sysfs_links = true;

So the real problem looks different. An error is logged for each
additional PHY, and phy_detach_internal() removes the "phydev" link by
name:

    if (phydev->sysfs_links) {
        if (dev)
            sysfs_remove_link(&dev->dev.kobj, "phydev");
        sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
    }

Detaching one PHY could then remove a link that belongs to another PHY
that is still attached.

The same collision and remove-by-name behavior already exists for non-mux
drivers that use one netdev with several PHYs, as the in-tree comment
says.

Could the commit message describe the failure more precisely, and say
that only the netdev side link can collide?

> diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
> index 60440b62e6c79..9630578a6a5c5 100644
> --- a/drivers/net/phy/phy_device.c
> +++ b/drivers/net/phy/phy_device.c
> @@ -2019,7 +2019,8 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
>  	 */
>  	phydev->sysfs_links = false;
>  
> -	phy_sysfs_create_links(phydev);
> +	if (!phydev->mux_port)
> +		phy_sysfs_create_links(phydev);

[Severity: Medium]
Does skipping the whole helper also drop the attached_dev link for mux
PHYs? phy_sysfs_create_links() creates two links:

    err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
                            "attached_dev");
    ...
    err = sysfs_create_link_nowarn(&dev->dev.kobj,
                                   &phydev->mdio.dev.kobj,
                                   "phydev");

The attached_dev link is created under each PHY's own mdio device kobject.
Several PHYs behind the same mux therefore can't collide on it. Only the
netdev side "phydev" link can conflict.

Later in the series, phy_mux_attach() calls phy_mux_port_bind_phy(), which
sets phydev->mux_port, and then calls phy_attach_direct():

    ret = phy_mux_port_bind_phy(port);
    ...
    ret = phy_attach_direct(dev, port->phydev, phy_flags,
                            port->interface);

phy_attach_direct() still sets phydev->attached_dev = dev and adds the PHY
to the link topology. Every mux PHY is attached to the netdev, but
/sys/class/mdio_bus/<bus>/<device>/attached_dev is never created for any
of them.

Documentation/ABI/testing/sysfs-class-net-phydev describes attached_dev as
a symbolic link to the network device this PHY device is attached to. It
makes no exception for muxes. The phydev entry in
Documentation/ABI/testing/sysfs-class-net doesn't mention the mux case
either.

Skipping the netdev side phydev link seems fine, because phy_mux_attach()
sets dev->phydev = NULL anyway.

Would it make sense to keep creating attached_dev for mux PHYs and only
skip the netdev side link? If not, could the exception be documented in
the ABI files?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204110622.1106492-1-maxime.chevallier%40bootlin.com

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 04/12] net: phy: phy_mux: Allow attaching a mux to a net_device
  2026-12-04 11:06 ` [PATCH net-next 04/12] net: phy: phy_mux: Allow attaching a mux to a net_device Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
@ 2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 12:18 UTC (permalink / raw)
  To: maxime.chevallier
  Cc: davem, andrew+netdev, kuba, edumazet, willemb, pabeni, linux,
	kabel, sebastian.hesselbarth, gregory.clement, hkallweit1, netdev,
	linux-kernel, linux-arm-kernel, thomas.petazzoni, devicetree,
	conor+dt, krzk+dt, robh, christophe.leroy, herve.codina,
	f.fainelli, vladimir.oltean, kory.maincent, o.rempel, nicveronese,
	horms, marcin.s.wojtas, daniel, dimitri.fedrau, frank.wunderlich,
	p.ameruoso, f

Thank you for your contribution! Sashiko AI review found 6 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 3 · Low: 3

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] Only the parent's optional pops->detach can undo an attach.
- [Medium] At this commit phy_mux_detach() takes no lock and asserts
  none, yet it clears mux->pops, mux->parent_priv and mux->netdev and…
- [Medium] net: phy: phy_mux: phy_mux_attach() clobbers dev->sfp_bus
- [Low] The commit message and kernel-doc describe things this commit
  does not contain.
- [Low] The patch deletes the still-accurate kernel-doc line "The mux
  still has to be freed with phy_mux_free()" from phy_mux_unregister().
- [Low] fwnode_phy_mux_attach() holds the global phy_mux_mutex across
  phy_attach_direct() for every muxed PHY, across device_link_add(), and…

--

net: phy: phy_mux: Allow attaching a mux to a net_device

This commit adds fwnode_phy_mux_attach() and phy_mux_detach(), so a mux
can be attached to a net_device. A parent, later phylink, can register
attach and detach callbacks. Every PHY behind the mux is attached to the
netdev with its link change callback overridden, and a device link is
added from the netdev's parent to the mux device.

> diff --git a/drivers/net/phy/phy_mux.c b/drivers/net/phy/phy_mux.c
> index e259eb4e271a0..691bca09e0af1 100644
> --- a/drivers/net/phy/phy_mux.c
> +++ b/drivers/net/phy/phy_mux.c

[ ... ]

> @@ -16,12 +18,196 @@ struct phy_mux {

[ ... ]

> +static void phy_mux_phy_link_change(struct phy_device *phydev, bool up)
> +{
> +}

[ ... ]

> + * All the PHYs behind the mux are looked up and attached to @dev, and stay
> + * attached until phy_mux_detach(). The parent only gets to use the one that
> + * is selected.

[ ... ]

> +		ret = phy_attach_direct(dev, port->phydev, phy_flags,
> +					port->interface);
> +		if (ret) {
> +			phy_mux_port_unbind_phy(port);
> +			goto err_detach;
> +		}
> +
> +		port->phydev->phy_link_change = phy_mux_phy_link_change;
> +		port->attached = true;
> +		has_phys = true;
> +	}

[Severity: Medium]
phy_attach_direct() does more than set dev->phydev. When the PHY has a
downstream SFP cage, it also does this:

drivers/net/phy/phy_device.c:phy_attach_direct() {
    ...
	if (dev) {
		phydev->attached_dev = dev;
		dev->phydev = phydev;

		if (phydev->sfp_bus_attached)
			dev->sfp_bus = phydev->sfp_bus;
    ...
}

The sysfs link creation is skipped when phydev->mux_port is set, but
this assignment is not. phy_mux_attach() then resets dev->phydev to
NULL and leaves dev->sfp_bus as it is.

Suppose one of the muxed PHYs has its own SFP cage, through an sfp
property handled by phy_sfp_probe() as on 88E1510, 88X3310 or QCA807x.
Attaching the mux then overwrites whatever dev->sfp_bus already held.
That could be the mux SFP cage that phylink_register_mux_sfp() sets up
later in the series, or another muxed PHY's cage.

dev->sfp_bus would then point at the SFP bus of a PHY that may not be
selected. ethtool -m would read the wrong module, and
phy_link_topo_add_phy() would record the wrong parent_sfp_bus for a
copper module plugged into the mux cage.

Deselected PHYs stay attached with attached_dev == dev. So
phy_sfp_attach() and phy_sfp_detach() on any muxed PHY keep rewriting
or clearing dev->sfp_bus, whichever port is selected.

On the way out, neither the err_detach unwind in phy_mux_attach() nor
phy_mux_detach() restores dev->sfp_bus, so the mux cage's entry is
lost. Once phy_detach() has cleared attached_dev, phy_sfp_detach() can
no longer clear dev->sfp_bus either. If the PHY and its SFP socket are
then unbound while the netdev stays registered, dev->sfp_bus is left
dangling for the next ethtool -m.

The Turris Omnia description later in the series has no SFP cage
behind the muxed 88E1514, so it doesn't hit this. Nothing in the
series prevents such a topology, though.

Should phy_attach_direct() and the phy_sfp_attach()/phy_sfp_detach()
upstream ops skip dev->sfp_bus when phydev->mux_port is set? The mux
selection path could then own dev->sfp_bus for the selected port.
Alternatively, should phy_mux_attach() reject muxed PHYs that have a
downstream SFP bus until that is handled?

[Severity: Low]
The commit message says the parent registers callbacks "to be notified
when switching occurs". The phy_mux_attach() kernel-doc says "The parent
only gets to use the one that is selected".

In this patch, struct phy_mux_parent_ops only has attach and detach, and
the mux has no selection state. Every muxed PHY also gets the empty
phy_mux_phy_link_change() stub in place of the phy_link_change() handler
that phy_attach_direct() installed. As a result, link events from all
muxed PHYs are dropped.

Later in the series, "net: phy: phy_mux: Add port selection logic" adds
select/deselect to phy_mux_parent_ops, along with current_port,
phy_mux_select() and phy_mux_start()/stop(). Then "net: phylink: Support
PHYs reached through a phy_mux" points the selected PHY's phy_link_change
at phylink through phylink_bringup_phy(). Nothing calls
fwnode_phy_mux_attach() at this commit, so there is no runtime effect.

Could the commit message and kernel-doc describe only what this patch
provides, or say that port selection arrives in a follow-up patch?

[ ... ]

> +int
> +fwnode_phy_mux_attach(const struct fwnode_handle *fwnode,
> +		      struct net_device *dev, u32 phy_flags,
> +		      const struct phy_mux_parent_ops *pops, void *priv)
> +{
> +	int ret = -EPROBE_DEFER;
> +	struct phy_mux *mux;
> +
> +	mutex_lock(&phy_mux_mutex);
> +	list_for_each_entry(mux, &phy_muxes, node) {
> +		if (dev_fwnode(mux->dev) != fwnode)
> +			continue;
> +
> +		ret = phy_mux_attach(mux, dev, phy_flags, pops, priv);
> +		break;
> +	}
> +	mutex_unlock(&phy_mux_mutex);

[Severity: Low]
Here the global phy_mux_mutex stays held across phy_attach_direct() for
each muxed PHY, across device_link_add(), and across the parent's
pops->attach() callback.

I couldn't find an in-tree path that reverses the rtnl -> phy_mux_mutex
order:

  - phy_mux_unregister() releases phy_mux_mutex before rtnl_lock().
  - phy_attach_direct() never reaches sfp_bus_add_upstream() for these
    PHYs.
  - phylink_mux_attach(), added later in the series, only sets flags and
    pointers.

Would it help to note in the struct phy_mux_parent_ops kernel-doc that
@attach runs with phy_mux_mutex held? That would warn a future parent not
to take rtnl or re-enter the mux API from that callback.

[ ... ]

> +void phy_mux_detach(struct phy_mux *mux)
> +{
> +	if (!mux->netdev)
> +		return;
> +
> +	phy_mux_detach_phys(mux);
> +
> +	if (mux->devlink) {
> +		device_link_del(mux->devlink);
> +		mux->devlink = NULL;
> +	}
> +
> +	mux->pops = NULL;
> +	mux->parent_priv = NULL;
> +	mux->netdev = NULL;
> +}

[Severity: Medium]
phy_mux_detach() clears mux->pops, mux->parent_priv, mux->netdev and the
per-port attached state without taking or asserting any lock.
phy_mux_attach() writes these fields under phy_mux_mutex. The
check-then-call on them in phy_mux_unregister() below holds only rtnl.

Suppose a parent calls phy_mux_detach() without rtnl while the mux driver
is being unbound. Could that race with phy_mux_unregister() and cause a
NULL mux->pops dereference, or a double phy_detach()/put_device() on the
muxed PHYs?

"net: phy: phy_mux: Add port selection logic" later adds "Caller must
hold rtnl when the mux was started" to the phy_mux_detach() kernel-doc.
The in-tree phylink paths hold rtnl, with one exception:
phylink_destroy()->phylink_detach_mux(). That call is a no-op if
phylink_disconnect_phy() has already run, as phylink_destroy() requires.
The stateless device_link does not order the two unbinds, though.

Would an ASSERT_RTNL() in phy_mux_detach() make the requirement explicit?

[ ... ]

> @@ -160,14 +347,17 @@ EXPORT_SYMBOL_GPL(phy_mux_register);
>  /**
>   * phy_mux_unregister() - Make the mux unavailable to its parent
>   * @mux: The mux, registered
> - *
> - * The mux still has to be freed with phy_mux_free().
>   */

[Severity: Low]
This isn't a bug, but the removed sentence "The mux still has to be freed
with phy_mux_free()." still looks accurate. phy_mux_free() is still
required and still exported.

phy_mux_unregister() also now takes rtnl_lock() and calls the parent's
pops->detach() under it. A mux provider that calls it with rtnl held
would deadlock on rtnl_mutex.

Could the kernel-doc keep the phy_mux_free() note and say that the
function must be called without rtnl held?

>  void phy_mux_unregister(struct phy_mux *mux)
>  {
>  	mutex_lock(&phy_mux_mutex);
>  	list_del(&mux->node);
>  	mutex_unlock(&phy_mux_mutex);
> +
> +	rtnl_lock();
> +	if (mux->netdev && mux->pops && mux->pops->detach)
> +		mux->pops->detach(mux, mux->parent_priv);
> +	rtnl_unlock();
>  }

[Severity: Medium]
Is the optional pops->detach() the only thing that can undo an attach?
phy_mux_attach() treats the ops as optional:

    if (pops && pops->attach)
        pops->attach(mux, priv);

fwnode_phy_mux_attach() only returns an int. The parent can therefore
only get the mux pointer that phy_mux_detach() needs through
pops->attach.

If pops or pops->detach is NULL, or the detach callback does not call
phy_mux_detach(), nothing here detaches the PHYs. phy_mux_free() then
calls phy_mux_port_destroy() on every port. For ports that still have
port->attached set, that only unbinds the PHY and never calls
phy_detach():

drivers/net/phy/phy_mux.c:phy_mux_port_destroy() {
    ...
	case PHY_MUX_PORT_PHY:
		phy_mux_port_unbind_phy(mux_port);
		fwnode_handle_put(mux_port->fwnode);
		break;
    ...
}

Would this leak the references taken by phy_attach_direct(), the
link_topo entry and mux->devlink? It would also leave
phydev->attached_dev pointing at a netdev that may be freed later.

At the end of the series, the only in-tree parent is phylink. It always
passes phylink_mux_ops with all callbacks, and phylink_mux_detach()
calls phy_mux_detach(). Triggering this would take a caller that misuses
the API.

Could phy_mux_attach() reject NULL attach/detach ops with -EINVAL? As an
alternative, phy_mux_unregister() could call phy_mux_detach() itself when
mux->netdev is still set after the callback.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204110622.1106492-1-maxime.chevallier%40bootlin.com

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 05/12] net: phy: phy_mux: Add port selection logic
  2026-12-04 11:06 ` [PATCH net-next 05/12] net: phy: phy_mux: Add port selection logic Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
@ 2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 12:18 UTC (permalink / raw)
  To: maxime.chevallier
  Cc: davem, andrew+netdev, kuba, edumazet, willemb, pabeni, linux,
	kabel, sebastian.hesselbarth, gregory.clement, hkallweit1, netdev,
	linux-kernel, linux-arm-kernel, thomas.petazzoni, devicetree,
	conor+dt, krzk+dt, robh, christophe.leroy, herve.codina,
	f.fainelli, vladimir.oltean, kory.maincent, o.rempel, nicveronese,
	horms, marcin.s.wojtas, daniel, dimitri.fedrau, frank.wunderlich,
	p.ameruoso, f

Thank you for your contribution! Sashiko AI review found 7 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 3 · Low: 4

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] phy_mux_start() (drivers/net/phy/phy_mux.c) sets mux->state =
  PHY_MUX_STARTED and then calls phy_mux_select(mux, port), but it…
- [Medium] phy_mux_attach() now runs phy_mux_port_deselect() on every
  port right after phy_attach_direct().
- [Medium] net: phy: unlocked phy_mux_detach() races with unregister and
  ethtool
- [Low] Concern: phy_mux_unregister() calls pops->detach() and only then
  phy_mux_stop(), so a deselect callback could reach a parent that has…
- [Low] The mux's own bookkeeping, phy_mux_port_set_parent_phy(), depends
  on which optional parent callbacks exist, and the two directions check…
- [Low] Concern: phy_mux_start() does not check that the mux is attached.
- [Low] Concern: phy_mux_detach() and phy_mux_stop() read mux->state with
  no lock under a conditional rtnl contract, so a phy_mux_start() coming…

--

net: phy: phy_mux: Add port selection logic

This patch adds port selection to phy_mux. phy_mux_start() selects the
first port and phy_mux_stop() deselects the current one, and the parent
is told through new select/deselect callbacks. It also adds
phy_mux_port_ops, whose only op for now powers down the PHY of a
deselected port. All ports are deselected when the mux gets attached.

> diff --git a/drivers/net/phy/phy_mux.c b/drivers/net/phy/phy_mux.c
> index 691bca09e0af1..407269980edc1 100644
> --- a/drivers/net/phy/phy_mux.c
> +++ b/drivers/net/phy/phy_mux.c

[ ... ]

> @@ -26,12 +31,97 @@ struct phy_mux {

[ ... ]

> +static void __phy_mux_deselect_port(struct phy_mux *mux,
> +				    struct phy_mux_port *port)
> +{
> +	if (mux->pops && mux->pops->deselect) {
> +		mux->pops->deselect(mux, port, mux->parent_priv);
> +		phy_mux_port_set_parent_phy(mux, port, false);
> +	}
> +
> +	phy_mux_port_deselect(port);
> +
> +	port->selected = false;
> +}
> +
> +static int __phy_mux_select_port(struct phy_mux *mux,
> +				 struct phy_mux_port *port)
> +{
> +	int ret;
> +
> +	if (mux->pops && mux->pops->select) {
> +		ret = mux->pops->select(mux, port, mux->parent_priv);
> +		phy_mux_port_set_parent_phy(mux, port, !ret);
> +		if (ret)
> +			return ret;
> +	}

[Severity: Low]
Should phy_mux_port_set_parent_phy() depend on which optional parent
callbacks are present?

The select side only sets netdev->phydev when pops->select is set. The
deselect side only clears it when pops->deselect is set. The kernel-doc
for struct phy_mux_parent_ops doesn't say the two have to be implemented
together.

Take a parent that provides .select but not .deselect. After
phy_mux_stop(), netdev->phydev would still point at the deselected PHY,
and phy_link_change would not be restored to phy_mux_phy_link_change.
Meanwhile phy_mux_phy_power_down() suspends that PHY. Generic
netdev->phydev users such as the ethtool PHY ops would then act on a
suspended, deselected PHY until phy_mux_detach_phys() runs.

The reverse case, .deselect without .select, would send the parent a
deselect for a port it was never told was selected.

The only in-tree parent (phylink_mux_ops, later in the series) provides
both callbacks, so nothing hits this today.

Could this bookkeeping run unconditionally, or could the kernel-doc state
that the two callbacks must be paired?

[ ... ]

> @@ -129,6 +219,9 @@ static int phy_mux_attach(struct phy_mux *mux, struct net_device *dev,
>  	if (has_phys)
>  		dev->phydev = NULL;
>  
> +	xa_for_each(&mux->ports, id, port)
> +		phy_mux_port_deselect(port);
> +

[Severity: Medium]
This loop calls phy_mux_phy_power_down() -> phy_suspend() on every PHY
right after phy_attach_direct() resumed it with phy_resume(). On genphy
that sets BMCR_PDOWN.

phy_mux_port_ops has no select or resume op, so the mux never powers a
PHY back up itself. Later in the series this only happens in
phylink_mux_select() -> phy_start(), which runs from phylink_start() ->
phy_mux_start() -> state_work.

Also later in the series, phylink_attach_mux() passes PHY_F_RXC_ALWAYS_ON
to fwnode_phy_mux_attach() when pl->config->mac_requires_rxc is set. Does
this power-down cancel out that flag?

phylink_fwnode_phy_connect()
  phylink_attach_mux()          /* flags |= PHY_F_RXC_ALWAYS_ON */
    fwnode_phy_mux_attach()
      phy_mux_attach()
        phy_attach_direct()     /* phy_resume() */
        phy_mux_port_deselect()
          phy_mux_phy_power_down()
            phy_suspend()

Some MACs need the PHY's RX clock between connect and phylink_start(),
for example for a DMA soft reset in ndo_open. Behind a mux, such a MAC
could fail to open.

The only user so far (mvneta on Turris Omnia) doesn't set
mac_requires_rxc. The failure also depends on the PHY stopping RXC while
powered down.

[ ... ]

> +void phy_mux_start(struct phy_mux *mux)
> +{
> +	struct phy_mux_port *port;
> +	unsigned long id = 0;
> +
> +	mux->state = PHY_MUX_STARTED;
> +
> +	port = xa_find(&mux->ports, &id, ULONG_MAX, XA_PRESENT);
> +	phy_mux_select(mux, port);
> +}

[Severity: Medium]
What happens here if the parent's ->select() fails?

__phy_mux_select_port() rolls back netdev->phydev and phy_link_change.
phy_mux_select() then returns the error without setting
mux->current_port. phy_mux_start() drops that return value, and it is
declared void in include/linux/phy_mux.h, so the parent can't see the
failure either.

The mux is left in PHY_MUX_STARTED with current_port == NULL. Every PHY is
still powered down from phy_mux_attach(), and nothing retries the
selection.

The rest of the series doesn't change this. phy_mux_start() later only
queues state_work, and phy_mux_state_machine() also ignores the result of
phy_mux_select(mux, phy_mux_choose_port(mux)). The only later re-trigger
is an SFP availability change.

With phylink as the parent, phylink_mux_select() fails if
phylink_bringup_phy() fails. That can happen when port->interface fails
validation, or on an MDIO error from phy_eee_rx_clock_stop(). The
interface would then be up with no PHY and no link until it is taken down
and brought up again.

Should the error be returned to the caller, or should the selection be
retried?

[ ... ]

> @@ -188,13 +281,15 @@ EXPORT_SYMBOL_GPL(fwnode_phy_mux_attach);
>   * phy_mux_detach() - Detach the mux from its parent
>   * @mux: The mux
>   *
> - * The PHYs behind the mux are detached.
> + * The mux is stopped and its PHYs are detached. Caller must hold rtnl when
> + * the mux was started.
>   */
>  void phy_mux_detach(struct phy_mux *mux)
>  {
>  	if (!mux->netdev)
>  		return;
>  
> +	phy_mux_stop(mux);
>  	phy_mux_detach_phys(mux);
>  
>  	if (mux->devlink) {

[Severity: Medium]
This kernel-doc makes rtnl optional for phy_mux_detach() as long as the
mux is stopped. In that case phy_mux_detach() does all of the following
without holding rtnl or phy_mux_mutex:

- runs phy_mux_detach_phys()
- calls device_link_del(mux->devlink)
- clears mux->pops, mux->parent_priv and mux->netdev

phy_mux_unregister() drops phy_mux_mutex right after list_del(). It
then relies only on rtnl to read mux->netdev and mux->pops and to call
mux->pops->detach(). An unlocked phy_mux_detach() therefore shares no
lock with it.

The two can also run at the same time. The device link added in
phy_mux_attach() is DL_FLAG_STATELESS, so unbinding the mux driver
doesn't unbind the MAC first. mii_mux_auto_remove(), later in the
series, can then run phy_mux_unregister() and phy_mux_free()
concurrently with MAC removal.

Later in the series, phylink_destroy() calls phylink_detach_mux() ->
phy_mux_detach() without rtnl, since phylink_destroy() must not be
called with rtnl held. That path is reachable with a stopped mux, for
example:

mvpp2_open()
  phylink_fwnode_phy_connect()   /* mux attached, PHY_MUX_STOPPED */
  request_irq() fails            /* no phylink_disconnect_phy() */
...
mvpp2_port_remove()
  phylink_destroy()
    phylink_detach_mux()
      phy_mux_detach()           /* no rtnl */

ucc_geth ends up in the same place when ucc_geth_open() fails in
ucc_geth_init_mac() or request_irq() and ucc_geth_remove() runs
later.

If the mux driver is unbound at that point, both CPUs can be inside
phy_mux_detach(). One of them gets there through mux->pops->detach()
-> phylink_mux_detach(). Possible results are:

- a double phy_detach() and device_link_del()
- a NULL dereference of mux->pops
- a use-after-free of the phylink passed as parent_priv
- a use-after-free of the mux and its ports after phy_mux_free()

ucc_geth registers its netdev with devm_register_netdev(), so it is
still NETREG_REGISTERED when phylink_destroy() runs. Two things follow:

- phy_detach() reaches the rcu_dereference_protected() on dev->hwprov
  in phy_detach_internal() without rtnl, and lockdep will complain.
- phy_link_topo_del_phy() kfree()s the phy_device_node while an
  ethtool netlink request in ethnl_req_get_phydev() may be looking it
  up under rtnl.

The attach side doesn't help either. fwnode_phy_mux_attach() only
takes phy_mux_mutex, and callers such as dsa_user_create() and
ocelot_probe_port() don't hold rtnl. phy_mux_detach() takes neither
lock. So mux->netdev, mux->pops, mux->parent_priv and mux->devlink
have no single lock covering attach, detach and unregister.

Triggering this needs a privileged driver unbind or module unload on
top of an ndo_open() error path, so it isn't easy to hit. Nothing
later in the series changes it, though.

Should phy_mux_detach() always require rtnl, with phylink_destroy()
taking it around phylink_detach_mux()? Or should phy_mux_mutex protect
the mux state across attach, detach and unregister?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204110622.1106492-1-maxime.chevallier%40bootlin.com

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 06/12] net: phy: phy_mux: Support SFP cage ports
  2026-12-04 11:06 ` [PATCH net-next 06/12] net: phy: phy_mux: Support SFP cage ports Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
@ 2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 12:18 UTC (permalink / raw)
  To: maxime.chevallier
  Cc: davem, andrew+netdev, kuba, edumazet, willemb, pabeni, linux,
	kabel, sebastian.hesselbarth, gregory.clement, hkallweit1, netdev,
	linux-kernel, linux-arm-kernel, thomas.petazzoni, devicetree,
	conor+dt, krzk+dt, robh, christophe.leroy, herve.codina,
	f.fainelli, vladimir.oltean, kory.maincent, o.rempel, nicveronese,
	horms, marcin.s.wojtas, daniel, dimitri.fedrau, frank.wunderlich,
	p.ameruoso, f

Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 3

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] When a port switch fails, nothing is selected and the mux core
  never retries.
- [Medium] net: phy: phy_mux_detach() races with unregister, free, and
  attach
- [Low] This patch changes phy_mux_start() from synchronous to deferred
  selection without saying so.
- [Low] In include/linux/phy_mux.h, the kernel-doc for struct
  phy_mux_port::interface says "The MII mode the link runs in when this…
- [Low] net: phy: commit message overstates auto-mux scope and omits
  design

--

net: phy: phy_mux: Support SFP cage ports

This adds SFP cage ports to the phy_mux core, with at most one SFP cage
per mux. It tracks whether each port is available and lets a port take
precedence over the others. Port selection moves into a work item, and
phy_mux_port_set_available() switches ports when a module is inserted or
removed.

> diff --git a/drivers/net/phy/phy_mux.c b/drivers/net/phy/phy_mux.c
> index 407269980edc1..de3336970bd9b 100644
> --- a/drivers/net/phy/phy_mux.c
> +++ b/drivers/net/phy/phy_mux.c

[ ... ]

> @@ -122,6 +129,109 @@ static const struct phy_mux_port_ops phy_mux_phy_power_ops = {

[ ... ]

> +static void phy_mux_state_machine(struct work_struct *work)
> +{
> +	struct phy_mux *mux = container_of(work, struct phy_mux, state_work);
> +
> +	rtnl_lock();
> +
> +	switch (mux->state) {
> +	case PHY_MUX_STOPPED:
> +		break;
> +
> +	case PHY_MUX_STARTED:
> +		phy_mux_select(mux, phy_mux_choose_port(mux));
> +		break;
> +	}
> +
> +	rtnl_unlock();
> +}

[ ... ]

> +void phy_mux_port_set_available(struct phy_mux_port *port, bool available)
> +{
> +	struct phy_mux *mux = port->mux;
> +
> +	ASSERT_RTNL();
> +
> +	if (port->available == available)
> +		return;
> +
> +	port->available = available;
> +
> +	if (available && mux->state == PHY_MUX_STARTED) {
> +		phy_mux_select(mux, phy_mux_choose_port(mux));
> +		return;
> +	}
> +
> +	phy_mux_trigger(mux);
> +}

[Severity: Low]
The last sentence of the changelog says this patch implements the
automatic mux, but only the core side is here. The mii-mux-auto driver
and the parsing of the takes-precedence property only arrive later in
the series, in drivers/net/phy/mux/mii_mux_auto.c. Nothing in this
patch sets port->has_precedence either; phy_mux_choose_port() reads
it, and setting it is left to an init_port callback.

Could the changelog say this adds what an auto-mux driver needs,
rather than the auto-mux itself?

It also doesn't explain the split in phy_mux_port_set_available().
When a port becomes available on a started mux, phy_mux_select() runs
synchronously, under the caller's rtnl, and the parent's select and
deselect ops are called from inside phy_mux_port_set_available().
When a port becomes unavailable, the selection is deferred to
state_work, which takes rtnl again later from
system_freezable_power_efficient_wq. So for the SFP port, a module
insertion switches the mux before the call returns, while after a
module removal the SFP port stays current_port until the worker runs.

Is there a reason the two directions need to differ? Either way, could
the commit message and a comment above that branch explain it, so
parent drivers know which of their callbacks can run from within
phy_mux_port_set_available()?

[Severity: Medium]
This isn't introduced here, but since phy_mux_free() is touched: what
serializes phy_mux_detach() against phy_mux_unregister() and
phy_mux_free()?

phy_mux_detach() takes neither phy_mux_mutex nor rtnl, and its
kernel-doc allows calling it without rtnl when the mux is stopped.
phylink_destroy() does that later in this series. phy_mux_unregister()
drops phy_mux_mutex right after list_del(), then only takes rtnl
before testing mux->netdev and mux->pops and calling
mux->pops->detach(mux, mux->parent_priv). fwnode_phy_mux_attach()
serializes phy_mux_attach() with phy_mux_mutex only.

So with the MAC driver and the mux driver unbound at the same time:

CPU0 (MAC unbind)              CPU1 (mux unbind)
phylink_destroy()
  phy_mux_detach()
    mux->netdev is set
                               phy_mux_unregister()
                                 rtnl_lock()
                                 mux->pops->detach()
                                   phy_mux_detach()
                                 rtnl_unlock()
                               phy_mux_free()
                                 kfree(mux)
    phy_mux_detach_phys(mux)
    mux->pops = NULL

Both sides can run phy_mux_detach_phys() and device_link_del(), giving
a double phy_detach()/put_device(). CPU1 can also see mux->pops set and
then call through it after CPU0 has cleared it, or pass a parent_priv
the parent already freed. struct phy_mux has no refcount, so
phy_mux_free() can destroy the ports and kfree(mux) while CPU0 is
still writing to them. The disable_work_sync() added here only waits
for state_work.

The device link from phy_mux_attach() is DL_FLAG_STATELESS, so it does
not make the driver core unbind the MAC before the mux either.

Attach vs detach has the same problem, since the two paths hold
different locks (or none) while updating mux->netdev, mux->pops,
mux->devlink, port->attached, port->phydev and phydev->mux_port.

This needs root to unbind both devices, but nothing later in the
series adds locking or a refcount. Should phy_mux_detach() and
phy_mux_unregister() share a lock held across the whole teardown, or
should struct phy_mux be refcounted while a parent is attached? Would
a managed device link be enough to guarantee the unbind ordering?

[Severity: Medium]
What happens when the switch to the new port fails?

phy_mux_state_machine() and phy_mux_port_set_available() both ignore the
return value of phy_mux_select(). phy_mux_select() deselects the old port
before it tries the new one, and nothing is rolled back:

phy_mux_select() {
    ...
	if (mux->current_port) {
		__phy_mux_deselect_port(mux, mux->current_port);
		mux->current_port = NULL;
	}
    ...
	ret = __phy_mux_select_port(mux, port);
	if (ret)
		return ret;
    ...
}

For a PHY port, __phy_mux_deselect_port() also calls phy_suspend()
through phy_mux_phy_power_down() and clears netdev->phydev.

The phylink parent added later in this series makes the switch back to
copper after an SFP module removal reachable:

phylink_sfp_module_remove()
  phy_mux_port_set_available(sfp, false)
    phy_mux_trigger()
      phy_mux_state_machine()
        phy_mux_select()
          __phy_mux_deselect_port(SFP)
          __phy_mux_select_port(PHY)
            phylink_mux_select()
              phylink_bringup_phy()

phylink_bringup_phy() can fail in phylink_validate_phy(). It can also
fail when phy_eee_rx_clock_stop() hits an MDIO error, or return -EBUSY if
pl->phydev is still set.

A PHY port's available flag is set once in phy_mux_register_port() and
never changes. A repeated phy_mux_port_set_available() call with the same
value returns early.

Can this leave current_port NULL, with no port selected while the copper
cable is still connected, and nothing retrying until ifdown/ifup or the
next SFP event? Should the error at least be logged, or should
phy_mux_choose_port() fall back to another available port?

Before this patch, phy_mux_select() was only called with current_port
NULL from phy_mux_start(), or with port NULL from phy_mux_stop(). A
deselect followed by a failed select could not happen then.

[ ... ]

> @@ -312,13 +422,8 @@ EXPORT_SYMBOL_GPL(phy_mux_detach);
>   */
>  void phy_mux_start(struct phy_mux *mux)
>  {
> -	struct phy_mux_port *port;
> -	unsigned long id = 0;
> -
>  	mux->state = PHY_MUX_STARTED;
> -
> -	port = xa_find(&mux->ports, &id, ULONG_MAX, XA_PRESENT);
> -	phy_mux_select(mux, port);
> +	phy_mux_trigger(mux);
>  }

[Severity: Low]
This changes phy_mux_start() from synchronous to deferred selection for
every mux, PHY-only ones included. The commit message doesn't mention
it.

Before, pops->select() had run and netdev->phydev was set by the time
phy_mux_start() returned. Now the work is queued on
system_freezable_power_efficient_wq, and phy_mux_state_machine() takes
rtnl_lock(). Callers of phy_mux_start() already hold rtnl, so selection
only happens after the caller drops rtnl. On resume paths it also waits
for the freezable workqueue to thaw.

The kernel-doc above phy_mux_start() still says:

 * To be called when the parent is ready to use the PHY of the selected
 * port, typically when the interface is brought up. Caller must hold rtnl.

Could the kernel-doc of phy_mux_start() and struct phy_mux_parent_ops
say that select/deselect may now run later from a worker, or
synchronously from phy_mux_port_set_available()?

The later commit "net: phylink: Support PHYs reached through a phy_mux"
documents this for phylink ("the first selection, which only happens
after phylink_start()"). The core API documentation in this patch says
nothing about it.

The commit message also doesn't mention three other changes:

- the new init_port parameter of the exported phy_mux_fwnode_add_ports()
- the new selection policy: the precedence port first, then the first
  available port
- the deferred start described above

Could those be described as well?

[ ... ]

> diff --git a/include/linux/phy_mux.h b/include/linux/phy_mux.h
> index 6a42ffff02b7b..77cf389b8c776 100644
> --- a/include/linux/phy_mux.h
> +++ b/include/linux/phy_mux.h

[ ... ]

> @@ -30,9 +31,14 @@ enum phy_mux_port_type {
>   * @type: What sits behind that port
>   * @phydev: For PHY_MUX_PORT_PHY, the PHY device behind the port, while the
>   *	    mux is attached
> - * @fwnode: For PHY_MUX_PORT_PHY, the firmware node of that PHY
> + * @fwnode: For PHY_MUX_PORT_PHY, the firmware node of that PHY. For
> + *	    PHY_MUX_PORT_SFP, the port's own firmware node, carrying the sfp
> + *	    property.
>   * @ops: How to drive that port
>   * @interface: The MII mode the link runs in when this port is selected

[Severity: Low]
Is this description still accurate for SFP ports?

phy_mux_add_sfp_port() never sets port->interface, so it stays at the
kzalloc_obj() value of 0 (PHY_INTERFACE_MODE_NA). The mii-mux binding
forbids phy-mode on sfp ports, so there is nothing to parse. The real
mode depends on the inserted module and isn't tracked by the mux.

Should @interface be limited to PHY_MUX_PORT_PHY, like @phydev and
@attached?

> + * @available: The port can be used (e.g. an SFP module is plugged)
> + * @has_precedence: The port is selected as soon as it is available, whatever
> + *		  the other ports do.
>   * @attached: For PHY_MUX_PORT_PHY, the PHY is attached to the mux's netdev
>   * @selected: The port currently drives the MII
>   */

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204110622.1106492-1-maxime.chevallier%40bootlin.com

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 09/12] net: phylink: Support PHYs reached through a phy_mux
  2026-12-04 11:06 ` [PATCH net-next 09/12] net: phylink: Support PHYs reached through a phy_mux Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
@ 2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 12:18 UTC (permalink / raw)
  To: maxime.chevallier
  Cc: davem, andrew+netdev, kuba, edumazet, willemb, pabeni, linux,
	kabel, sebastian.hesselbarth, gregory.clement, hkallweit1, netdev,
	linux-kernel, linux-arm-kernel, thomas.petazzoni, devicetree,
	conor+dt, krzk+dt, robh, christophe.leroy, herve.codina,
	f.fainelli, vladimir.oltean, kory.maincent, o.rempel, nicveronese,
	horms, marcin.s.wojtas, daniel, dimitri.fedrau, frank.wunderlich,
	p.ameruoso, f

Thank you for your contribution! Sashiko AI review found 8 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 5 · Low: 2

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] net: phylink: phylink_mux_select() configures suspended PHY
- [Medium] With a mux, phylink has no PHY every time the interface is
  down, not only before the first selection as the commit message says.
- [Medium] The patch changes what
  phylink_fwnode_phy_connect()/phylink_of_phy_connect() guarantee on…
- [Medium] On MACs that set mac_requires_rxc (stmmac sets it
  unconditionally), the PHYs behind a mux are left in BMCR power-down…
- [Medium] With a mux, phylink_suspend() with PHY-handled WoL breaks the
  wake path.
- [Medium] net: phylink: missing rtnl_lock() in phylink_destroy() mux
  detach
- [Low] The kernel-doc for phylink_of_phy_connect() still says "Actions
  specified in phylink_connect_phy() will be performed": attach the PHY,…
- [Low] phylink_mux_select() writes `pl->link_config.interface =
  port->interface;` with only rtnl held, after phylink_bringup_phy() has…

--

net: phylink: Support PHYs reached through a phy_mux

This makes phylink act as the parent of a phy_mux. When the MAC node has
a mii-mux-handle property, phylink_fwnode_phy_connect() attaches to the
mux instead of connecting a PHY. phylink starts and stops the mux along
with the interface, and the select/deselect parent ops bring up or stop
the selected port's PHY and reconfigure the MAC.

> diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
> index 03e255f5ceb17..702614adcb945 100644
> --- a/drivers/net/phy/phylink.c
> +++ b/drivers/net/phy/phylink.c

[ ... ]

> @@ -2494,6 +2510,34 @@ int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
>  }
>  EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
>  
> +static const struct phy_mux_parent_ops phylink_mux_ops;
> +
> +static int phylink_attach_mux(struct phylink *pl,
> +			      const struct fwnode_handle *fwnode, u32 flags)
> +{
> +	struct fwnode_handle *mux_fwnode;
> +	int ret;
> +
> +	if (pl->mux || !IS_ENABLED(CONFIG_PHY_MUX))
> +		return 0;
> +
> +	if (pl->config->type != PHYLINK_NETDEV)
> +		return 0;
> +
> +	mux_fwnode = fwnode_find_reference(fwnode, "mii-mux-handle", 0);
> +	if (IS_ERR(mux_fwnode))
> +		return 0;
> +
> +	if (pl->config->mac_requires_rxc)
> +		flags |= PHY_F_RXC_ALWAYS_ON;
> +
> +	ret = fwnode_phy_mux_attach(mux_fwnode, pl->netdev, flags,
> +				    &phylink_mux_ops, pl);

[Severity: Medium]
On MACs that set mac_requires_rxc, can the PHYs behind the mux still be
powered down when the MAC runs its DMA reset?

phy_mux_attach() resumes each PHY through phy_attach_direct(), but then
deselects every port:

drivers/net/phy/phy_mux.c:phy_mux_attach() {
    ...
	xa_for_each(&mux->ports, id, port)
		phy_mux_port_deselect(port);
    ...
}

For PHY ports this ends up in phy_mux_phy_power_down() -> phy_suspend().
phylink_fwnode_phy_connect() then returns with pl->phydev == NULL.

phylink_prepare_resume() is the helper that guarantees RXC for these
MACs, and it only looks at pl->phydev:

	if (pl->config->mac_requires_rxc && phydev && phydev->suspended)
		phy_resume(phydev);

stmmac sets mac_requires_rxc unconditionally. __stmmac_open() calls
phylink_prepare_resume() and then stmmac_hw_setup(), both before
phylink_start(), so no PHY gets resumed. On resume, phylink_suspend() ->
phylink_stop() -> phy_mux_stop() has already cleared pl->phydev, so the
same thing seems to happen there.

If the PHY stops RXC in power-down, wouldn't the DMA reset fail, so that
open or resume fails with "Hw setup failed"?

stmmac takes this path when its node has both phy-handle and
mii-mux-handle. No in-tree DT pairs stmmac with a mux today.

> +	fwnode_handle_put(mux_fwnode);
> +
> +	return ret == -EPROBE_DEFER ? -ENODEV : ret;
> +}
> +
>  /**
>   * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
>   * @pl: a pointer to a &struct phylink returned from phylink_create()
> @@ -2501,7 +2545,7 @@ EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
>   * @flags: PHY-specific flags to communicate to the PHY device driver
>   *
>   * Connect the phy specified @fwnode to the phylink instance specified
> - * by @pl.
> + * by @pl, or attach to the PHY multiplexer it refers to.

[Severity: Low]
This isn't a bug, but phylink_of_phy_connect() forwards to this function,
and its kernel-doc still says:

 * Connect the phy specified in the device node @dn to the phylink instance
 * specified by @pl. Actions specified in phylink_connect_phy() will be
 * performed.

With mii-mux-handle, none of those actions (attaching the PHY,
restricting its advertisement, starting it, enabling interrupts, updating
the supported/advertising masks) happen until phylink_mux_select() runs
after phylink_start().

Should that comment be updated as well? It is still unchanged at the end
of the series.

>   *
>   * Returns 0 on success or a negative errno.
>   */
> @@ -2516,6 +2560,13 @@ int phylink_fwnode_phy_connect(struct phylink *pl,
>  	if (!phylink_expects_phy(pl))
>  		return 0;
>  
> +	ret = phylink_attach_mux(pl, fwnode, flags);
> +	if (ret)
> +		return ret;
> +
> +	if (pl->mux)
> +		return 0;

[Severity: Medium]
This changes what a successful phylink_fwnode_phy_connect() or
phylink_of_phy_connect() guarantees. Are the existing callers ready for
pl->phydev to be NULL here?

mvneta_mdio_probe() queries WoL right after connecting:

drivers/net/ethernet/marvell/mvneta.c:mvneta_mdio_probe() {
    ...
	int err = phylink_of_phy_connect(pp->phylink, pp->dn, 0);
    ...
	phylink_ethtool_get_wol(pp->phylink, &wol);
	device_set_wakeup_capable(&pp->dev->dev, !!wol.supported);
    ...
}

The Turris Omnia WAN port is an mvneta, and the DTS change in this series
gives it mii-mux-handle. There, wol.supported would be 0 and the netdev
would be marked not wakeup-capable, even though the 88E1514 behind the
mux supports WoL.

stmmac_init_phy() also calls phylink_ethtool_get_eee()/set_eee() right
after connecting, to program the PHY's tx_lpi_timer. With a mux that
would silently do nothing.

Also, stmmac_init_phy() only calls phylink_fwnode_phy_connect() when
fwnode_get_phy_node() finds a phy-handle:

	if (!phy_fwnode || IS_ERR(phy_fwnode)) {
		int addr = priv->plat->phy_addr;

So a MAC node that only has mii-mux-handle, like the mii-mux-auto.yaml
example, never reaches phylink_attach_mux(). It fails with "no phy found"
when phy_addr < 0.

Should these callers be adapted along with this change?

> +
>  	phy_fwnode = fwnode_get_phy_node(fwnode);
>  	if (IS_ERR(phy_fwnode)) {
>  		/* PHY mode requires a PHY to be specified. */

[ ... ]

> @@ -2727,6 +2787,8 @@ void phylink_stop(struct phylink *pl)
>  {
>  	ASSERT_RTNL();
>  
> +	if (pl->mux)
> +		phy_mux_stop(pl->mux);

[Severity: Medium]
The commit message says phylink has no PHY "between the time it attaches
to the mux and the first selection". Doesn't every phylink_stop() also
drop the PHY?

phylink_stop()
  phy_mux_stop()
    phy_mux_select(mux, NULL)
      __phy_mux_deselect_port()
        phylink_mux_deselect()
          phylink_clear_phydev()

After that, phy_mux_port_set_parent_phy(..., false) sets netdev->phydev
to NULL.

While the interface is down, phylink_ethtool_ksettings_set() with
cfg_link_an_mode == MLO_AN_PHY and no PHY falls through to the branch
commented "All that is left are in-band links". It stores speed, duplex
and advertising in pl->link_config and returns 0.

On the next open, phylink_mux_select() -> phylink_bringup_phy()
overwrites pl->link_config.advertising with phy->advertising. The PHY
itself was never reprogrammed.

So would an ethtool -s, or a systemd .link file applied while the device
is down, be accepted and then silently lost?

In the same state, phylink_ethtool_set_wol() and phylink_ethtool_set_eee()
return -EOPNOTSUPP, and phylink_ethtool_get_wol() reports no support.
ksettings_get reports the leftover pl->supported/advertising of whichever
PHY was selected last.

>  	if (pl->sfp_bus)
>  		sfp_upstream_stop(pl->sfp_bus);
>  	if (pl->phydev)

[ ... ]

> +static int phylink_mux_select(struct phy_mux *mux, struct phy_mux_port *port,
> +			      void *priv)
> +{
> +	struct phylink *pl = priv;
> +	struct phy_device *phy;
> +	int ret;
> +
> +	if (port->type != PHY_MUX_PORT_PHY)
> +		return -EOPNOTSUPP;
> +
> +	if (pl->phydev)
> +		return -EBUSY;
> +
> +	phy = port->phydev;
> +
> +	ret = phylink_bringup_phy(pl, phy, port->interface);
> +	if (ret) {
> +		phy->phylink = NULL;
> +		return ret;
> +	}
> +
> +	pl->link_config.interface = port->interface;

[Severity: High]
Is the PHY still suspended when phylink_mux_select() runs?

phy_mux_attach() deselects every port right after phy_attach_direct().
For PHY ports that ends in phy_mux_phy_power_down() -> phy_suspend(),
and every later deselection suspends the port again. Nothing in
phy_mux.c resumes the PHY before it calls pops->select().

phylink_mux_select() then calls phylink_bringup_phy() and
phylink_mac_initial_config() on that suspended PHY. Only the final
phy_start() goes through __phy_resume(). Before that point:

 - phylink_bringup_phy() does MDIO accesses through phy_support_eee(),
   phy_eee_tx_clock_stop_capable() and phy_eee_rx_clock_stop()

 - phy_request_interrupt() -> phy_enable_interrupts() calls
   config_intr()

 - phylink_major_config() calls phy_config_inband()

Without a mux this ordering doesn't occur, because phy_attach_direct()
resumes the PHY before phylink_bringup_phy() is called.

Several in-tree drivers don't tolerate register accesses in that
state:

 - bcm54xx_resume() notes that writes to registers other than BMCR
   are ignored until PDOWN is cleared, and resume can soft-reset and
   re-run config_init

 - kszphy_resume() says leaving power-down generates an internal
   global reset

 - rtl821x_suspend() gates priv->clk while suspended

On those PHYs, the interrupt enable written by config_intr(), or the
inband and EEE clock-stop settings, could be ignored or wiped by the
resume in phy_start(). phylib would then believe interrupts are
enabled and stop polling while the hardware still has them masked.
The link would never come up. If config_intr() fails outright, the
PHY is silently downgraded to PHY_POLL.

Should the PHY be resumed before phylink_bringup_phy(), either here
(and suspended again if select fails) or by phy_mux before it calls
pops->select()?

[Severity: Low]
Can this store race with phylink_resolve()?

phylink_mux_select() runs from the mux state work after phylink_start()
has cleared PHYLINK_DISABLE_STOPPED. phylink_bringup_phy() has already
published pl->phydev at this point. This line writes
pl->link_config.interface with only rtnl held.

phylink_resolve() can be queued at the same time by phylink_pcs_change(),
phylink_mac_change() or phylink_phy_change(). It reads and writes the
same field under state_mutex:

drivers/net/phy/phylink.c:phylink_resolve() {
    ...
	if ((mac_config && link_state.interface != pl->link_config.interface) ||
	    pl->force_major_config) {
		...
		pl->link_config.interface = link_state.interface;
    ...
}

Could resolve compare against a stale interface and run an extra major
config while select is changing it?

Later in the series, this same unlocked block also gets
clear_bit(PHYLINK_DISABLE_LINK) and a pl->req_link_an_mode store.

> +	phylink_mac_initial_config(pl, false);
> +	phy_start(phy);
> +
> +	return 0;
> +}
> +
> +static void phylink_mux_deselect(struct phy_mux *mux,
> +				 struct phy_mux_port *port, void *priv)
> +{
> +	struct phylink *pl = priv;
> +	struct phy_device *phy;
> +
> +	if (pl->phydev != port->phydev)
> +		return;
> +
> +	phy = port->phydev;
> +
> +	if (phy_is_started(phy) || phy->state == PHY_ERROR)
> +		phy_stop(phy);
> +
> +	phylink_clear_phydev(pl);
> +
> +	if (phy_interrupt_is_valid(phy))
> +		phy_free_interrupt(phy);

[Severity: Medium]
phylink_destroy() is called without rtnl held. Both of its neighbours
here, sfp_bus_del_upstream() and phylink_destroy_sfp_cage_port(),
take rtnl_lock() themselves. phylink_detach_mux() takes no lock at
all, but the paths it can race with rely on rtnl:

 - phy_mux_unregister() takes rtnl_lock() before it checks
   mux->netdev and calls pops->detach(). That ends in
   phylink_mux_detach(), which clears pl->mux.

 - phy_mux_state_machine() runs phy_mux_select() under rtnl.
   phy_mux_stop() only does cancel_work(), not cancel_work_sync(), so
   rtnl is the only thing keeping the work from running alongside
   detach.

If pl->mux is still set when phylink_destroy() runs, could
phylink_destroy() and phy_mux_unregister() both detach the same mux,
so that phy_detach() and device_link_del() run twice? Could a state
work item that is already running still dereference pl after the
kfree(pl) at the end of phylink_destroy()?

If the mux was started, the chain phy_mux_detach() -> phy_mux_stop()
-> phylink_mux_deselect() -> phylink_clear_phydev() also hits
ASSERT_RTNL().

Most drivers call phylink_disconnect_phy() under rtnl before
phylink_destroy(). So this needs a teardown or error path where the
mux is still attached at destroy time.

Should phylink_destroy() take rtnl_lock() around phylink_detach_mux(),
and should phylink_detach_mux() get an ASSERT_RTNL()?

[Severity: Medium]
With a mux, does PHY-based Wake-on-LAN still work across suspend?

When the PHY handles WoL, phylink_suspend() calls phylink_stop(). That
now goes through phy_mux_stop() -> phylink_mux_deselect(), which reaches
this phy_free_interrupt().

phy_free_interrupt() calls config_intr(PHY_INTERRUPT_DISABLED) and then
frees the IRQ. Some PHYs signal WoL through their interrupt enable
register: at803x writes 0 to AT803X_INTR_ENABLE, and Marvell clears the
IMASK register that holds WOL_EIE. On those PHYs this disables the wake
source.

phy_suspend() in the port deselect returns -EBUSY because WoL is enabled.
The PHY therefore stays powered but can no longer raise a wake.

Without a mux, phylink_stop() only calls phy_stop(), which leaves the
interrupt configuration and the IRQ in place.

phylink_suspend() then does:

	} else {
		phylink_stop(pl);
	}

	if (phylink_phy_pm_speed_ctrl(pl))
		phylink_speed_down(pl, false);

phylink_phy_pm_speed_ctrl() needs pl->phydev, and deselect has already
cleared it. When wol_phy_speed_ctrl is set, wouldn't phylink_speed_down()
be skipped, leaving the PHY at full link speed during WoL suspend?

> +
> +	phy->phylink = NULL;
> +}

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204110622.1106492-1-maxime.chevallier%40bootlin.com

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 10/12] net: phylink: Support an SFP cage behind a phy_mux
  2026-12-04 11:06 ` [PATCH net-next 10/12] net: phylink: Support an SFP cage behind " Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
@ 2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 12:18 UTC (permalink / raw)
  To: maxime.chevallier
  Cc: davem, andrew+netdev, kuba, edumazet, willemb, pabeni, linux,
	kabel, sebastian.hesselbarth, gregory.clement, hkallweit1, netdev,
	linux-kernel, linux-arm-kernel, thomas.petazzoni, devicetree,
	conor+dt, krzk+dt, robh, christophe.leroy, herve.codina,
	f.fainelli, vladimir.oltean, kory.maincent, o.rempel, nicveronese,
	horms, marcin.s.wojtas, daniel, dimitri.fedrau, frank.wunderlich,
	p.ameruoso, f

Thank you for your contribution! Sashiko AI review found 7 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 5 · Low: 2

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] phylink_sfp_module_insert() (drivers/net/phy/phylink.c) calls
  phylink_sfp_set_present(pl, true) before it has accepted the module.
- [Medium] phylink_register_mux_sfp() sets pl->sfp_behind_mux = true and
  pl->sfp_link_down = true inside phylink_create().
- [Medium] net: phylink: phylink_start() runs resolve before mux selects
  SFP port
- [Medium] net: phylink: phylink_mux_deselect() does not bring down
  active SFP link
- [Medium] net: phylink: missing rtnl_lock() in phylink_destroy() mux
  detach
- [Low] phylink_speed_down() and phylink_speed_up() change from
  `!pl->sfp_bus && pl->phydev` to `pl->phydev && (!pl->sfp_bus ||…
- [Low] On the PHY-port path, phylink_mux_select() calls
  clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state) first,…

--

net: phylink: Support an SFP cage behind a phy_mux

This patch lets phylink find an SFP cage that is described in one of the
ports of a phy_mux referenced by mii-mux-handle, and register as its
upstream. Module presence is reported to the mux with
phy_mux_port_set_available() so the mux can switch between the SFP and PHY
ports. SFP link events are gated on whether the SFP port is selected.

> diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
> index 702614adcb945..6d62bd37f95a6 100644
> --- a/drivers/net/phy/phylink.c
> +++ b/drivers/net/phy/phylink.c

[ ... ]

> @@ -1876,6 +1881,30 @@ static int phylink_register_sfp(struct phylink *pl,
>  	return ret;
>  }
>  
> +static int phylink_register_mux_sfp(struct phylink *pl,
> +				    const struct fwnode_handle *fwnode)
> +{
> +	struct fwnode_handle *mux_fwnode, *sfp_fwnode;
> +	int ret;
> +
> +	mux_fwnode = fwnode_find_reference(fwnode, "mii-mux-handle", 0);
> +	if (IS_ERR(mux_fwnode))
> +		return phylink_register_sfp(pl, fwnode);
> +
> +	sfp_fwnode = fwnode_phy_mux_find_sfp_port(mux_fwnode);
> +	fwnode_handle_put(mux_fwnode);
> +	if (!sfp_fwnode)
> +		return phylink_register_sfp(pl, fwnode);
> +
> +	pl->sfp_behind_mux = true;
> +	pl->sfp_link_down = true;

[Severity: Medium]
Should deselecting the SFP port also take the link down?

For an SFP port, phylink_mux_deselect() only clears sfp_selected. It
does not set PHYLINK_DISABLE_LINK. It also leaves an SFP module PHY
in pl->phydev if one is attached.

Take a mux driver unbind while the SFP link is up. The device link
created by phy_mux_attach() is DL_FLAG_STATELESS, so the netdev stays
open:

phy_mux_unregister()
  phylink_mux_detach()
    phylink_detach_mux()
      phy_mux_detach()
        phy_mux_stop()
          phy_mux_select(mux, NULL)
            phylink_mux_deselect()

The link is left up. sfp_behind_mux stays true while sfp_selected is
now false, so every later phylink_sfp_link_down() takes the early
return. Wouldn't the carrier then stay up even after the fibre or
the module is pulled?

Removing the SFP socket has a similar problem. sfp_unregister_bus()
calls phylink_sfp_detach() first, which marks the mux port
unavailable and queues mux->state_work.

It then calls socket_ops->stop(), which moves sm_dev_state to
SFP_DEV_DOWN. As a result sfp_sm_main() skips sfp_sm_link_down():

		if (sfp->sm_state == SFP_S_LINK_UP &&
		    sfp->sm_dev_state == SFP_DEV_UP)
			sfp_sm_link_down(sfp);

When the mux work runs, phylink_mux_deselect() leaves the link up.
phylink_mux_select() for the PHY port then calls
phylink_mac_initial_config() -> phylink_major_config(), and
mac_link_down() has never been called. Is the MAC/PCS expected to
cope with a major reconfiguration while the link is up?

Also, a copper module's PHY may still be in pl->phydev when the SFP
port is deselected. In that case the following phylink_mux_select()
for the PHY port would fail with -EBUSY.

Would calling phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK)
from the SFP branch of phylink_mux_deselect() be enough here? And
what should happen to an attached module PHY?

[Severity: Medium]
Can phylink_resolve() bring the link up here before the mux has
selected the SFP port?

While sfp_selected is false, phylink_sfp_link_down() only records
sfp_link_down. It returns without setting PHYLINK_DISABLE_LINK.

Suppose a module is already in the cage when the interface is opened.
By the time phylink_start() runs, the only bit left in
phylink_disable_state is PHYLINK_DISABLE_STOPPED.

phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_STOPPED) clears
that bit. The state drops to zero and pl->resolve is queued.

Only after that does phylink_start() call
phy_mux_port_set_available() and phy_mux_start(). At that point
mux->state is still PHY_MUX_STOPPED, so the port is not selected
synchronously. The selection is deferred to mux->state_work, and
phy_mux_state_machine() has to take rtnl_lock(). The caller of
phylink_start() still holds it:

	rtnl_lock();

	switch (mux->state) {
	...
	case PHY_MUX_STARTED:
		phy_mux_select(mux, phy_mux_choose_port(mux));
		break;
	}

phylink_resolve() does not need RTNL. It can run first, with
phylink_disable_state == 0 even though sfp_link_down is still true.
That is before the SFP state machine has finished its LOS checks.

When phylink_mux_select() finally runs for the SFP port, it calls
phylink_run_resolve_and_disable(). Could the carrier come up and
then drop straight away?

Would it make sense to set PHYLINK_DISABLE_LINK up front when
sfp_behind_mux is set? phylink_mux_select() could then clear it for
whichever port ends up selected. Or should the mux be started before
the resolve is enabled?

[Severity: Medium]
sfp_behind_mux is set here in phylink_create() based only on the firmware
description. The mux itself is attached later, through
phylink_fwnode_phy_connect() -> phylink_attach_mux(). What happens if that
attach never takes place?

phylink_attach_mux() returns 0 without attaching anything for non-netdev
users:

	if (pl->config->type != PHYLINK_NETDEV)
		return 0;

There are other ways to end up without a mux:

- phylink_fwnode_phy_connect() returns early for fixed-link.
- A MAC driver may call phylink_connect_phy() directly when there is no
  phy-handle.
- If the mux driver is unbound, phylink_detach_mux() clears pl->mux and
  pl->mux_sfp_port but leaves sfp_behind_mux set.

In all of these cases sfp_selected never becomes true, so
phylink_sfp_link_up() always takes the new early return:

	pl->sfp_link_down = false;
	if (pl->sfp_behind_mux && !pl->sfp_selected)
		return;

Would the module then be detected and configured while
PHYLINK_DISABLE_LINK is never cleared? That would leave the carrier down
with no error reported.

> +
> +	ret = phylink_register_sfp(pl, sfp_fwnode);
> +	fwnode_handle_put(sfp_fwnode);
> +
> +	return ret;
> +}

[ ... ]

> @@ -3842,7 +3875,8 @@ int phylink_speed_down(struct phylink *pl, bool sync)
>  
>  	ASSERT_RTNL();
>  
> -	if (!pl->sfp_bus && pl->phydev)
> +	if (pl->phydev &&
> +	    (!pl->sfp_bus || (pl->sfp_behind_mux && !pl->sfp_selected)))
>  		ret = phy_speed_down(pl->phydev, sync);

[Severity: Low]
Does !sfp_selected reliably mean that pl->phydev is the mux's copper PHY?

phylink_mux_deselect() clears sfp_selected for an SFP port, but it only
clears pl->phydev for PHY_MUX_PORT_PHY ports:

	if (port->type == PHY_MUX_PORT_SFP)
		pl->sfp_selected = false;

	if (port->type != PHY_MUX_PORT_PHY || pl->phydev != port->phydev)
		return;

Suppose the mux is detached while a copper SFP module's PHY is attached,
for example on mux driver unbind:

phy_mux_unregister()
  phylink_mux_detach()
    phy_mux_detach()
      phy_mux_stop()

pl->phydev is then still the SFP module PHY, and sfp_selected is false.

A later phylink_speed_down() would call phy_speed_down() on that SFP
module PHY. Both the kernel-doc ("set the non-SFP PHY to lowest speed")
and the old check rule that out. The same applies to phylink_speed_up().

The commit message also doesn't mention this change to the WoL/suspend
behaviour.

>  
>  	return ret;

[ ... ]

> @@ -4096,18 +4143,27 @@ static int phylink_sfp_module_insert(void *upstream,
>  	pl->sfp_may_have_phy = caps->may_have_phy;
>  	pl->sfp_port = caps->port;
>  
> +	phylink_sfp_set_present(pl, true);
> +
>  	/* If this module may have a PHY connecting later, defer until later */
>  	if (pl->sfp_may_have_phy)
>  		return 0;

[Severity: Medium]
The port is reported as available here before phylink has accepted the
module. The commit message says:

  "we only report the modules that phylink accepted"

and:

  "with an unsupported module we're left with the PHY port selected"

Does the code match that?

When the mux is started, phy_mux_port_set_available(port, true) selects
the port synchronously:

	if (available && mux->state == PHY_MUX_STARTED) {
		phy_mux_select(mux, phy_mux_choose_port(mux));
		return;
	}

	phy_mux_trigger(mux);

That deselects the copper port before phylink_sfp_config_optical() has
run. phylink_mux_deselect() stops the PHY, clears pl->phydev and frees
the interrupt, and then the mux powers the PHY down.

For an unsupported optical module, the rollback to false only queues
phy_mux_trigger(). Would every such insertion drop and renegotiate the
copper link?

For modules with sfp_may_have_phy set, this returns 0 with the port still
available, and nothing later seems to undo it.

phylink_sfp_connect_phy() can fail:

	if (!phy->drv) {
		...
		return -EINVAL;
	}
	...
	return phylink_sfp_config_phy(pl, phy);

phylink_sfp_module_start() can also fail in phylink_sfp_config_optical(),
and that path only removes the mod port:

	ret = phylink_sfp_config_optical(pl);
	if (ret)
		goto del_mod_port;

In sfp_sm_main(), both failures go to SFP_S_FAIL without calling
module_remove. That would leave the SFP mux port selected, the copper PHY
suspended with pl->phydev NULL, and PHYLINK_DISABLE_LINK set.

Is there then no link on either medium until the module is pulled out?

> -	return phylink_sfp_config_optical(pl);
> +	ret = phylink_sfp_config_optical(pl);
> +	if (ret)
> +		phylink_sfp_set_present(pl, false);
> +
> +	return ret;
>  }

[Severity: Medium]
This gives the mux a new way to be touched while phylink_destroy()
is tearing things down without RTNL.

phylink_destroy() calls sfp_bus_del_upstream(), which takes RTNL and
ends up in phylink_sfp_detach(). With this patch, that now calls
phy_mux_port_set_available(pl->mux_sfp_port, false). If a module was
present, this goes through phy_mux_trigger() and queues
mux->state_work.

As soon as sfp_bus_del_upstream() drops RTNL,
phy_mux_state_machine() can take it. It then calls
phylink_mux_select() or phylink_mux_deselect() with pl as the
private pointer.

Meanwhile phylink_destroy() runs phylink_detach_mux() ->
phy_mux_detach() -> phy_mux_stop() without RTNL, although both of
those expect it to be held.

phy_mux_stop() only does cancel_work() on mux->state_work. Nothing
waits for a running phy_mux_state_machine() before kfree(pl). Can
that lead to a use-after-free of pl?

The same unlocked phylink_detach_mux() can also race with
phy_mux_unregister(). On mux driver unbind, that calls
phylink_mux_detach() under RTNL. Both sides could then run
phy_mux_detach() at the same time, calling phy_detach() and
device_link_del() twice.

This needs phylink_destroy() to be entered with pl->mux still set.
That happens when the MAC driver doesn't call
phylink_disconnect_phy() first:

- ucc_geth_remove() calls phylink_destroy() before devres
  unregisters the netdev.
- The error paths in lan743x_netdev_open() and mvpp2_open() skip the
  disconnect after the PHY was connected.

The unlocked call was added earlier in the series. Should
phylink_destroy() take RTNL around phylink_detach_mux(), or detach
the mux before sfp_bus_del_upstream()?

Note that phy_mux_stop() can't simply switch to cancel_work_sync()
while it is called under RTNL, since the work itself takes
rtnl_lock().

[ ... ]

> @@ -4291,7 +4369,10 @@ static int phylink_mux_select(struct phy_mux *mux, struct phy_mux_port *port,
>  		return ret;
>  	}
>  
> +	clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
> +
>  	pl->link_config.interface = port->interface;
> +	pl->req_link_an_mode = pl->cfg_link_an_mode;
>  	phylink_mac_initial_config(pl, false);

[Severity: Low]
Can clearing PHYLINK_DISABLE_LINK before these stores race with
phylink_resolve()?

When switching back from the SFP port after the module is removed,
phylink_sfp_link_down() has set DISABLE_LINK. This clear_bit() can bring
phylink_disable_state back to 0. A resolve queued by phylink_pcs_change()
or phylink_mac_change() can then run from the workqueue.

phylink_resolve() reads, and may write, pl->link_config while holding
phydev_mutex, phy->lock and state_mutex. Here pl->link_config.interface
and pl->req_link_an_mode are written under RTNL only.

The link should be down during that window, so the practical impact may
be small. Would moving the clear_bit() after phylink_mac_initial_config()
avoid the race?

>  	phy_start(phy);

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204110622.1106492-1-maxime.chevallier%40bootlin.com

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 11/12] net: phy: mux: Add the mii-mux-auto driver
  2026-12-04 11:06 ` [PATCH net-next 11/12] net: phy: mux: Add the mii-mux-auto driver Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
@ 2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 12:18 UTC (permalink / raw)
  To: maxime.chevallier
  Cc: davem, andrew+netdev, kuba, edumazet, willemb, pabeni, linux,
	kabel, sebastian.hesselbarth, gregory.clement, hkallweit1, netdev,
	linux-kernel, linux-arm-kernel, thomas.petazzoni, devicetree,
	conor+dt, krzk+dt, robh, christophe.leroy, herve.codina,
	f.fainelli, vladimir.oltean, kory.maincent, o.rempel, nicveronese,
	horms, marcin.s.wojtas, daniel, dimitri.fedrau, frank.wunderlich,
	p.ameruoso, f

Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 5 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] The mii-mux-auto binding accepts DTs that the mii-mux-auto
  driver refuses to probe, and the driver gives no reason.
- [Medium] On mii-mux-auto hardware (Turris Omnia), the mux select line
  is the SFP cage's MOD_DEF0.
- [Medium] MII_MUX_AUTO is tristate, and nothing orders the MAC after the
  mux.
- [Medium] net: phy: unsynchronized pl->mux access and detach vs
  phy_mux_unregister()
- [Medium] net: phy: mux: unsynchronized mii_mux_auto_remove() vs
  phy_mux_detach()

--

net: phy: mux: Add the mii-mux-auto driver

This adds a platform driver for MII multiplexers that the hardware switches
on its own, described by the mii-mux-auto binding. It registers the two mux
ports with the phy_mux core. It also reads takes-precedence so the core
knows which port the hardware prefers, as on the Turris Omnia WAN port.

> diff --git a/drivers/net/phy/mux/Kconfig b/drivers/net/phy/mux/Kconfig
> new file mode 100644
> index 0000000000000..cbfd49fab2ea2
> --- /dev/null
> +++ b/drivers/net/phy/mux/Kconfig
> @@ -0,0 +1,18 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +
> +if PHY_MUX
> +
> +config MII_MUX_AUTO
> +	tristate "MII multiplexer switched by the hardware"

[Severity: Medium]
With MII_MUX_AUTO built as a module, what happens if the MAC connects
before mii_mux_auto has probed and called phy_mux_register()?

Nothing seems to order the MAC after the mux. drivers/of/property.c has no
fw_devlink supplier binding for mii-mux-handle. The device link in
phy_mux_attach() is only created after a successful attach.

In that window fwnode_phy_mux_attach() returns -EPROBE_DEFER, and
phylink_attach_mux() turns it into -ENODEV:

drivers/net/phy/phylink.c:phylink_attach_mux() {
    ...
	ret = fwnode_phy_mux_attach(mux_fwnode, pl->netdev, flags,
				    &phylink_mux_ops, pl);
	fwnode_handle_put(mux_fwnode);

	return ret == -EPROBE_DEFER ? -ENODEV : ret;
}

phylink_fwnode_phy_connect() then returns -ENODEV and does not fall back to
phy-handle.

On Turris Omnia, mvneta connects from ndo_open:

mvneta_open()
  mvneta_mdio_probe()
    phylink_of_phy_connect()
      phylink_attach_mux()
        fwnode_phy_mux_attach()   <- -EPROBE_DEFER, returned as -ENODEV

So if eth2 is brought up before udev has loaded mii_mux_auto, wouldn't
ndo_open fail with -ENODEV, with no retry once the mux registers? A MAC
that connects at probe time would fail probe for good, since the error is
no longer -EPROBE_DEFER.

Converting to -ENODEV is reasonable in ndo_open context. The conversion
was added earlier in this series in "net: phylink: Support PHYs reached
through a phy_mux". This patch adds the first mux provider and makes it
modular, which makes the path reachable.

Should there be some ordering or deferral for this new dependency?

> +	help
> +	  Support for the two-port MII multiplexers that the hardware
> +	  switches by itself, without any software control.

[ ... ]

> diff --git a/drivers/net/phy/mux/mii_mux_auto.c b/drivers/net/phy/mux/mii_mux_auto.c
> new file mode 100644
> index 0000000000000..980cc2c242ba7
> --- /dev/null
> +++ b/drivers/net/phy/mux/mii_mux_auto.c
> @@ -0,0 +1,99 @@

[ ... ]

> +static int mii_mux_auto_init_port(struct phy_mux_port *port,
> +				  struct fwnode_handle *child)
> +{
> +	port->has_precedence = fwnode_property_read_bool(child,
> +							 "takes-precedence");

[Severity: Medium]
Is has_precedence enough to describe how this hardware switches?

On Turris Omnia the select line is MOD_DEF0, so the hardware routes the
MAC lane to the cage as soon as a module is seated. The core only treats
the SFP port as available once phylink accepts the module. Presence is set
after EEPROM parsing and cleared again if the optical config fails:

drivers/net/phy/phylink.c:phylink_sfp_module_insert() {
    ...
	phylink_sfp_set_present(pl, true);
    ...
	ret = phylink_sfp_config_optical(pl);
	if (ret)
		phylink_sfp_set_present(pl, false);
    ...
}

phy_mux_choose_port() then falls back to the PHY port, which is always
available:

	xa_for_each(&mux->ports, id, port)
		if (port->available)
			return port;

That leaves hardware and software disagreeing in three cases:

  - for a short time, between MOD_DEF0 asserting and sfp_module_insert()
    running
  - permanently, if the EEPROM can't be read and sfp.c ends in
    SFP_MOD_ERROR without ever calling module_insert
  - permanently, for a module that phylink rejects

In each case the PHY stays selected. phylink_mux_select() brings the MAC up
in the PHY's phy-mode and starts the PHY.

With a copper cable also plugged in, can the PHY report link up while the
analog switch has moved the lane to the cage, so no traffic flows?

"net: phylink: Support an SFP cage behind a phy_mux" mentions this ("with
an unsupported module we're left with the PHY port selected while the MAC
is wired to the cage ... left for later"). Neither this commit message nor
the Turris Omnia DTS commit ("we follow the module at runtime") mentions
it.

Should this limitation be documented here? Or should a raw-presence
callback exist before this configuration is enabled on real hardware?

> +
> +	return 0;
> +}
> +
> +static int mii_mux_auto_check_ports(struct fwnode_handle *fwnode)
> +{
> +	struct fwnode_handle *child;
> +	int n_sfp = 0, n_phy = 0, n_prec = 0, n_sfp_prec = 0;
> +
> +	fwnode_for_each_available_child_node(fwnode, child) {
> +		if (fwnode_property_read_bool(child, "takes-precedence"))
> +			n_prec++;
> +
> +		if (!fwnode_property_present(child, "sfp")) {
> +			n_phy++;
> +			continue;
> +		}
> +
> +		n_sfp++;
> +		if (fwnode_property_read_bool(child, "takes-precedence"))
> +			n_sfp_prec++;
> +	}
> +
> +	if (n_sfp != 1 || n_phy != 1 || n_prec != 1 || n_sfp_prec != 1)
> +		return -EOPNOTSUPP;

[Severity: Medium]
This check only accepts one SFP port and one PHY port, with
takes-precedence on the SFP port. Does that match the mii-mux-auto
binding?

In Documentation/devicetree/bindings/net/mii-mux-auto.yaml, each port can
be a PHY port or an SFP port through mii-mux.yaml $defs/port. The oneOf
only requires takes-precedence on exactly one of port@0/port@1, whatever
the port type:

oneOf:
  - properties:
      port@0:
        required:
          - takes-precedence
  - properties:
      port@1:
        required:
          - takes-precedence

So these DTs pass dtbs_check but fail probe here with -EOPNOTSUPP:

  - takes-precedence on the PHY port
  - two PHY ports
  - two SFP ports

There is no dev_err(), so the only output is the driver core's generic
"probe ... failed with error -95".

The restriction seems to come from the core. phy_mux_register_port() sets
port->available = true on PHY ports and nothing clears it. A PHY port with
precedence would always win in phy_mux_choose_port():

	xa_for_each(&mux->ports, id, port)
		if (port->has_precedence && port->available)
			return port;

The commit message says the driver "looks for the takes-precedence property
to tell the core which port the hardware is going to switch to".
mii_mux_auto_init_port() also reads the property on any port type. Yet
only one layout can probe.

Could the binding be narrowed to match the driver? And could the driver
print why it rejects the ports?

> +
> +	return 0;
> +}

[ ... ]

> @@ -0,0 +1,99 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +
> +#include <linux/module.h>
> +#include <linux/phy_mux.h>
> +#include <linux/platform_device.h>
> +#include <linux/property.h>
> +
> +static int mii_mux_auto_init_port(struct phy_mux_port *port,
> +				  struct fwnode_handle *child)
> +{
> +	port->has_precedence = fwnode_property_read_bool(child,
> +							 "takes-precedence");
> +
> +	return 0;
> +}
> +
> +static int mii_mux_auto_check_ports(struct fwnode_handle *fwnode)
> +{
> +	struct fwnode_handle *child;
> +	int n_sfp = 0, n_phy = 0, n_prec = 0, n_sfp_prec = 0;
> +
> +	fwnode_for_each_available_child_node(fwnode, child) {
> +		if (fwnode_property_read_bool(child, "takes-precedence"))
> +			n_prec++;
> +
> +		if (!fwnode_property_present(child, "sfp")) {
> +			n_phy++;
> +			continue;
> +		}
> +
> +		n_sfp++;
> +		if (fwnode_property_read_bool(child, "takes-precedence"))
> +			n_sfp_prec++;
> +	}
> +
> +	if (n_sfp != 1 || n_phy != 1 || n_prec != 1 || n_sfp_prec != 1)
> +		return -EOPNOTSUPP;
> +
> +	return 0;
> +}
> +
> +static int mii_mux_auto_probe(struct platform_device *pdev)
> +{
> +	struct fwnode_handle *fwnode = dev_fwnode(&pdev->dev);
> +	struct phy_mux *mux;
> +	int ret;
> +
> +	ret = mii_mux_auto_check_ports(fwnode);
> +	if (ret)
> +		return ret;
> +
> +	mux = phy_mux_alloc(&pdev->dev, 2);
> +	if (IS_ERR(mux))
> +		return PTR_ERR(mux);
> +
> +	ret = phy_mux_fwnode_add_ports(mux, fwnode, mii_mux_auto_init_port);
> +	if (ret)
> +		goto err_free;
> +
> +	ret = phy_mux_register(mux);
> +	if (ret)
> +		goto err_free;
> +
> +	platform_set_drvdata(pdev, mux);
> +
> +	return 0;
> +
> +err_free:
> +	phy_mux_free(mux);
> +	return ret;
> +}
> +
> +static void mii_mux_auto_remove(struct platform_device *pdev)
> +{
> +	struct phy_mux *mux = platform_get_drvdata(pdev);
> +
> +	phy_mux_unregister(mux);
> +	phy_mux_free(mux);
> +}
> +
> +static const struct of_device_id mii_mux_auto_of_match[] = {
> +	{ .compatible = "mii-mux-auto" },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(of, mii_mux_auto_of_match);
> +
> +static struct platform_driver mii_mux_auto_driver = {
> +	.probe = mii_mux_auto_probe,
> +	.remove = mii_mux_auto_remove,
> +	.driver = {
> +		.name = "mii-mux-auto",
> +		.of_match_table = mii_mux_auto_of_match,
> +	},
> +};
> +module_platform_driver(mii_mux_auto_driver);
> +
> +MODULE_DESCRIPTION("Ethernet MII multiplexer switched by the hardware");
> +MODULE_AUTHOR("Maxime Chevallier <maxime.chevallier@bootlin.com>");
> +MODULE_LICENSE("GPL");

[Severity: Medium]
What keeps the mux alive while a MAC is still attached to it?

phy_mux_attach() adds the device link with DL_FLAG_STATELESS. That
link is not managed, so device_links_unbind_consumers() skips it.
Unbinding mii-mux-auto therefore does not unbind the MAC first. struct
phy_mux and struct phylink are not refcounted either, and
phy_mux_attach() takes no module reference.

So a sysfs unbind, or rmmod mii_mux_auto, runs mii_mux_auto_remove()
while the MAC and its phylink are still bound. As soon as
phy_mux_unregister() returns, phy_mux_free() destroys the ports and
kfree()s the mux.

phy_mux_unregister() relies only on rtnl_lock() to serialize against
the parent. phy_mux_detach() takes neither rtnl_lock() nor
phy_mux_mutex. Some MAC teardown paths reach it without rtnl_lock():

  - phylink_destroy() -> phylink_detach_mux(). pl->mux can still be
    set here after a failed mvpp2_open() or ucc_geth_open(). The call
    comes from mvpp2_port_remove() or ucc_geth_remove().
  - mtk_remove(), which calls mtk_stop() and phylink_disconnect_phy()
    from platform remove context without rtnl_lock(). If the mux was
    started, phy_mux_stop() there uses cancel_work() and can race
    with phy_mux_state_machine().

If one of these runs concurrently with mii_mux_auto_remove():

  - phy_mux_detach() can touch a mux and ports that phy_mux_free() is
    freeing.
  - phy_mux_unregister() can pass its mux->netdev && mux->pops check
    and then find mux->pops cleared. It can also call ->detach() on a
    phylink that phylink_destroy() has already freed.
  - both sides can run phy_mux_detach() at once. That calls
    phy_detach() and put_device(&phydev->mdio.dev) twice, and drops
    the device link twice through device_link_del().

This needs root to trigger, but it ends in a use-after-free.

Should the MAC be unbound before the mux goes away, for example through
a managed device link? Or should the mux be refcounted so that it is
not freed while a phylink still points at it? In either case, should
phy_mux_detach() take the same lock as phy_mux_unregister()?

[Severity: Medium]
mii_mux_auto_remove() is the first in-tree caller of
phy_mux_unregister() and phy_mux_free(). That makes two races in the
phy_mux core reachable. In both, pl->mux is read or torn down without
the lock that phy_mux_unregister() relies on.

phy_mux_unregister() drops phy_mux_mutex right after list_del(). It
then holds only rtnl_lock() around mux->pops->detach(). That call ends
in phylink_detach_mux(), which clears pl->mux.

First, some MACs call phylink_fwnode_phy_connect() without
rtnl_lock(). Two examples:

  - ocelot_port_phylink_create(), at probe time
  - dsa_user_phy_setup(), which runs before dsa_user_create() takes
    rtnl_lock()

fwnode_phy_mux_attach() sets pl->mux under phy_mux_mutex and then
drops the mutex. phylink_fwnode_phy_connect() later tests pl->mux with
no lock held:

drivers/net/phy/phylink.c:phylink_fwnode_phy_connect() {
    ...
	ret = phylink_attach_mux(pl, fwnode, flags);
	if (ret)
		return ret;

	if (pl->mux)
		return 0;
    ...
}

If mii_mux_auto_remove() runs in between, the test can see NULL and
fall through to fwnode_get_phy_node(). The connect then does one of
three things:

  - returns -ENODEV
  - returns 0 with nothing attached
  - attaches a fallback phy-handle while pl->sfp_behind_mux is still
    set

Second, phylink_destroy() must be called without rtnl_lock(). It calls
phylink_detach_mux() -> phy_mux_detach(pl->mux) and then kfree(pl).
It holds neither rtnl_lock() nor phy_mux_mutex while doing so.

pl->mux can still be set at that point if ndo_open failed after the
connect and never called phylink_disconnect_phy(). ucc_geth_open()
does this when ucc_geth_init_mac() or request_irq() fails. mvpp2_open()
does the same when request_irq() fails.

If that races with mii_mux_auto_remove():

  - both sides can run phy_mux_detach() on the same mux. That calls
    phy_detach(), put_device() and device_link_del() twice.
  - phy_mux_free() can kfree() the mux while phylink_destroy() is
    still inside phy_mux_detach().
  - phylink_destroy() can kfree(pl) just before phy_mux_unregister()
    calls mux->pops->detach(mux, mux->parent_priv) with that pointer.

Unbinding the driver or unloading the module needs root. Still, the
result is an oops or memory corruption, not a clean failure.

The locking was added earlier in this series, in "net: phy: phy_mux:
Allow attaching a mux to a net_device" and "net: phylink: Support PHYs
reached through a phy_mux". This patch is what makes it reachable.

Should the pl->mux test in phylink_fwnode_phy_connect() and the detach
in phylink_destroy() be serialized against phy_mux_unregister()? Or
should the mux be pinned while a phylink is attached to it?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204110622.1106492-1-maxime.chevallier%40bootlin.com

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH net-next 12/12] ARM: dts: turris-omnia: Describe the WAN port multiplexer
  2026-12-04 11:06 ` [PATCH net-next 12/12] ARM: dts: turris-omnia: Describe the WAN port multiplexer Maxime Chevallier
  2026-12-05 11:08   ` sashiko-bot
@ 2026-12-05 12:18   ` netdev-bot+sashiko
  1 sibling, 0 replies; 31+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 12:18 UTC (permalink / raw)
  To: maxime.chevallier
  Cc: davem, andrew+netdev, kuba, edumazet, willemb, pabeni, linux,
	kabel, sebastian.hesselbarth, gregory.clement, hkallweit1, netdev,
	linux-kernel, linux-arm-kernel, thomas.petazzoni, devicetree,
	conor+dt, krzk+dt, robh, christophe.leroy, herve.codina,
	f.fainelli, vladimir.oltean, kory.maincent, o.rempel, nicveronese,
	horms, marcin.s.wojtas, daniel, dimitri.fedrau, frank.wunderlich,
	p.ameruoso, f

Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 2 · Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] This DTS removes `sfp = <&sfp>` from &eth2 and adds `mii-mux-
  handle = <&wan_mux>`.
- [Medium] The patch deletes the DTS comment describing the U-Boot fixup
  but does not make the DT robust against that fixup.
- [Medium] ARM: dts: turris-omnia: wan_mux breaks mvneta_mdio_probe() WoL
  and speed_up
- [Low] The commit message says "The sfp node is always enabled, and we
  follow the module at runtime." That is only partly true.

--

ARM: dts: turris-omnia: Describe the WAN port multiplexer

This commit describes the Turris Omnia WAN port mux (NXP CBTL02042A)
with the new mii-mux-auto binding. The sfp node is now always enabled.
The sfp reference moves from eth2 to port@0 of the mux, and eth2 gets a
mii-mux-handle.

> Now that we have a binding for these muxes, let's describe that one. The
> sfp node is always enabled, and we follow the module at runtime.

[Severity: Low]
Is "we follow the module at runtime" fully accurate?

The earlier commit in this series, "net: phylink: Support an SFP cage
behind a phy_mux", notes that phylink only reports modules it has
accepted to the mux, in phylink_sfp_set_present():

	pl->sfp_mod_present = present;
	if (pl->mux_sfp_port)
		phy_mux_port_set_available(pl->mux_sfp_port, present);

The CBTL02042A switches to the cage for any inserted module, though.
Suppose an unsupported module is plugged in. Wouldn't the mux keep port@1
(the 88E1514) selected while the MAC SerDes is physically wired to the
cage? Link state would then come from a PHY that is no longer in the data
path.

Could the commit message mention this limitation? This is the commit that
turns the behaviour on for real hardware.

> diff --git a/arch/arm/boot/dts/marvell/armada-385-turris-omnia.dts b/arch/arm/boot/dts/marvell/armada-385-turris-omnia.dts
> index 83fe00abd6523..0501686e26427 100644
> --- a/arch/arm/boot/dts/marvell/armada-385-turris-omnia.dts
> +++ b/arch/arm/boot/dts/marvell/armada-385-turris-omnia.dts
> @@ -103,13 +103,24 @@ sfp: sfp {
>  		los-gpios = <&pcawan 3 GPIO_ACTIVE_HIGH>;
>  		mod-def0-gpios = <&pcawan 4 GPIO_ACTIVE_LOW>;
>  		maximum-power-milliwatt = <3000>;
> +	};
>  
> -		/*
> -		 * For now this has to be enabled at boot time by U-Boot when
> -		 * a SFP module is present. Read more in the comment in the
> -		 * eth2 node below.
> -		 */
> -		status = "disabled";
> +	wan_mux: mii-mux {
> +		compatible = "mii-mux-auto";
> +		#address-cells = <1>;
> +		#size-cells = <0>;
> +
> +		port@0 {
> +			reg = <0>;
> +			sfp = <&sfp>;
> +			takes-precedence;
> +		};

[ ... ]

> @@ -199,16 +210,12 @@ &eth2 {
>  	 * eth2 is connected via a multiplexor to both the SFP cage and to
>  	 * ethernet-phy@1. The multiplexor switches the signal to SFP cage when
>  	 * a SFP module is present, as determined by the mode-def0 GPIO.
> -	 *
> -	 * Until kernel supports this configuration properly, in case SFP module
> -	 * is present, U-Boot has to enable the sfp node above, remove phy
> -	 * handle and add managed = "in-band-status" property.
>  	 */

[Severity: Medium]
Is the new DT robust against the U-Boot fixup that this comment described?

According to the removed text, U-Boot does the following when a module is
present at boot:

- enables the sfp node
- removes eth2's phy-handle
- adds managed = "in-band-status"

U-Boot builds that are already deployed will keep doing this. This patch
also drops sfp = <&sfp> from eth2, which is what made that fixup work.

Now take a kernel without mux support (CONFIG_PHY_MUX=n, or an older
kernel booted with this DTB). phylink_attach_mux() returns early:

	if (pl->mux || !IS_ENABLED(CONFIG_PHY_MUX))
		return 0;

fwnode_get_phy_node() then finds no phy-handle, because U-Boot deleted it.
phylink_register_mux_sfp() finds no SFP either, because eth2 no longer has
an sfp property.

Doesn't eth2 then end up with neither a PHY nor an SFP upstream? By that
point the analog switch has already routed comphy5 to the cage, and the
sfp driver holds TX_DISABLE asserted. That is the boot-with-module case
that worked before this patch.

On a mux-capable kernel, the injected managed = "in-band-status" leaves
eth2 in MLO_AN_INBAND for the whole uptime. If the module is removed
later, the 88E1514 path runs with a different MAC AN mode than it would
after a boot without a module.

Is it intended that the AN mode depends on whether a module was present at
boot, rather than on the selected mux port?

>  	status = "okay";
>  	phy-mode = "sgmii";
>  	phy-handle = <&phy1>;
> +	mii-mux-handle = <&wan_mux>;
>  	phys = <&comphy5 2>;
> -	sfp = <&sfp>;

[Severity: Medium]
Does mvneta cope with the deferred PHY attachment this enables on eth2?

With PHY_MUX and MII_MUX_AUTO enabled, the phylink phy_mux support
earlier in this series leaves pl->phydev NULL after
phylink_fwnode_phy_connect() for a MAC with a mii-mux-handle. The PHY
only shows up once the mux state machine selects a port, which happens
asynchronously after phylink_start().

mvneta_open() calls mvneta_mdio_probe() before mvneta_start_dev(), and
mvneta_mdio_probe() queries WoL right after connecting:

	phylink_ethtool_get_wol(pp->phylink, &wol);
	device_set_wakeup_capable(&pp->dev->dev, !!wol.supported);

mvneta_phylink_ops has no mac_wol_set, so phylink_ethtool_get_wol()
only consults pl->phydev, and that is still NULL at this point.
wol.supported stays 0 and dev->power.can_wakeup is cleared, even
though the 88E1514 behind port@1 supports WoL through
m88e1318_get_wol() and m88e1318_set_wol().

Nothing sets can_wakeup again once phy_mux_state_machine() selects
port@1. A later WoL request reaches mvneta_ethtool_set_wol(), where
phylink_ethtool_set_wol() can now succeed on the attached PHY.
However, device_set_wakeup_enable() fails with -EINVAL in
device_wakeup_enable() because can_wakeup is false, and
mvneta_ethtool_set_wol() ignores that return value. The PHY ends up
armed for WoL, but device_may_wakeup() stays false, so
mvneta_stop_dev() never calls phylink_speed_down().

mvneta_start_dev() has the same ordering problem:

	phylink_start(pp->phylink);

	/* We may have called phylink_speed_down before */
	phylink_speed_up(pp->phylink);

phy_mux_start() only queues mux->state_work through phy_mux_trigger(),
so pl->phydev is still NULL when phylink_speed_up() runs, and the call
does nothing. Suppose can_wakeup were fixed on its own. Wouldn't the
PHY then stay at whatever speed phylink_speed_down() picked, even after
the next interface up?

This patch is the one that puts a mii-mux-handle on an mvneta port, so
this is where the regression becomes visible. Should mvneta
re-evaluate its wakeup capability, and redo the speed_up, once the mux
attaches a PHY? Or should phylink give MAC drivers a hook when the
selected PHY changes? Either way, it seems that needs to land before or
together with this DT change.

[Severity: High]
Can removing sfp = <&sfp> break the WAN port on kernels built without
CONFIG_PHY_MUX?

Without it, the cage is only reachable through the mux port. PHY_MUX is a
bool with no default, and MII_MUX_AUTO only exists under it.
mvebu_v7_defconfig and multi_v7_defconfig both build this board with
MVNETA and SFP, but neither enables these options.

With PHY_MUX=n, the stub in include/linux/phy_mux.h returns NULL:

fwnode_phy_mux_find_sfp_port(const struct fwnode_handle *fwnode)
{
	return NULL;
}

so phylink_register_mux_sfp() falls back to eth2's own node:

	if (!sfp_fwnode)
		return phylink_register_sfp(pl, fwnode);

That fallback looks for an sfp property on eth2, and this patch removed
it. Because the sfp node is now always enabled, sfp_probe() still binds
and keeps TX_DISABLE asserted:

	sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;

No upstream ever attaches, so SFP modules on the WAN port would stop
working. Before this patch, a module present at boot worked: U-Boot
enabled the sfp node and eth2 had sfp = <&sfp>. Older kernels booted with
this DTB hit the same problem.

A second case: PHY_MUX=y, but the mii-mux-auto driver is not bound when
the interface is opened (MII_MUX_AUTO=n, module not loaded yet, or a
failed probe). What happens then?

fwnode_phy_mux_attach() returns its initial -EPROBE_DEFER when no
registered mux matches. phylink_attach_mux() converts that:

	return ret == -EPROBE_DEFER ? -ENODEV : ret;

phylink_fwnode_phy_connect() returns that error before it tries eth2's
phy-handle:

	ret = phylink_attach_mux(pl, fwnode, flags);
	if (ret)
		return ret;

So mvneta_open()->mvneta_mdio_probe()->phylink_of_phy_connect()->
phylink_fwnode_phy_connect() fails, and mvneta_open() bails out:

	ret = mvneta_mdio_probe(pp);
	if (ret < 0) {
		netdev_err(dev, "cannot probe MDIO bus\n");
		goto err_free_dead_hp;

Wouldn't the WAN interface then fail to open at all, copper included?
Nothing retries after -ENODEV.

Would keeping sfp = <&sfp> on eth2 avoid the non-mux regression?
phylink_register_mux_sfp() prefers the mux's SFP port when one is found,
so the property should be harmless on mux-capable kernels.

Should the defconfigs also enable PHY_MUX and MII_MUX_AUTO?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204110622.1106492-1-maxime.chevallier%40bootlin.com

^ permalink raw reply	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2026-12-05 12:18 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 11:06 [PATCH net-next 00/12] net: phy: Introduce MII multiplexers Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 01/12] dt-bindings: net: Add Ethernet " Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot
2026-12-05 12:18   ` netdev-bot+sashiko
2026-12-04 11:06 ` [PATCH net-next 02/12] net: phy: Introduce phy_mux, a MII multiplexer representation Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 03/12] net: phy: Don't create the sysfs links for PHYs behind a mux Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot
2026-12-05 12:18   ` netdev-bot+sashiko
2026-12-04 11:06 ` [PATCH net-next 04/12] net: phy: phy_mux: Allow attaching a mux to a net_device Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot
2026-12-05 12:18   ` netdev-bot+sashiko
2026-12-04 11:06 ` [PATCH net-next 05/12] net: phy: phy_mux: Add port selection logic Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot
2026-12-05 12:18   ` netdev-bot+sashiko
2026-12-04 11:06 ` [PATCH net-next 06/12] net: phy: phy_mux: Support SFP cage ports Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot
2026-12-05 12:18   ` netdev-bot+sashiko
2026-12-04 11:06 ` [PATCH net-next 07/12] net: phylink: Factor out phylink_clear_phydev() Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 08/12] net: phy: phy_mux: Add phy_mux_set_mac_managed_pm() Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 09/12] net: phylink: Support PHYs reached through a phy_mux Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot
2026-12-05 12:18   ` netdev-bot+sashiko
2026-12-04 11:06 ` [PATCH net-next 10/12] net: phylink: Support an SFP cage behind " Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot
2026-12-05 12:18   ` netdev-bot+sashiko
2026-12-04 11:06 ` [PATCH net-next 11/12] net: phy: mux: Add the mii-mux-auto driver Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot
2026-12-05 12:18   ` netdev-bot+sashiko
2026-12-04 11:06 ` [PATCH net-next 12/12] ARM: dts: turris-omnia: Describe the WAN port multiplexer Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot
2026-12-05 12:18   ` netdev-bot+sashiko

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.