Netdev List
 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; 13+ 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] 13+ 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-04 11:06 ` [PATCH net-next 02/12] net: phy: Introduce phy_mux, a MII multiplexer representation Maxime Chevallier
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ 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] 13+ 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; 13+ 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] 13+ 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-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, 0 replies; 13+ 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] 13+ 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-04 11:06 ` [PATCH net-next 05/12] net: phy: phy_mux: Add port selection logic Maxime Chevallier
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ 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] 13+ 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-04 11:06 ` [PATCH net-next 06/12] net: phy: phy_mux: Support SFP cage ports Maxime Chevallier
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ 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] 13+ 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-04 11:06 ` [PATCH net-next 07/12] net: phylink: Factor out phylink_clear_phydev() Maxime Chevallier
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ 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] 13+ 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; 13+ 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] 13+ 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; 13+ 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] 13+ 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-04 11:06 ` [PATCH net-next 10/12] net: phylink: Support an SFP cage behind " Maxime Chevallier
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ 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] 13+ 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-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, 0 replies; 13+ 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] 13+ 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-04 11:06 ` [PATCH net-next 12/12] ARM: dts: turris-omnia: Describe the WAN port multiplexer Maxime Chevallier
  11 siblings, 0 replies; 13+ 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] 13+ 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
  11 siblings, 0 replies; 13+ 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] 13+ messages in thread

end of thread, other threads:[~2026-12-04 11:08 UTC | newest]

Thread overview: 13+ 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-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-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 ` [PATCH net-next 05/12] net: phy: phy_mux: Add port selection logic Maxime Chevallier
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 ` [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-04 11:06 ` [PATCH net-next 10/12] net: phylink: Support an SFP cage behind " Maxime Chevallier
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox