net: dsa: mt7628 embedded switch initial support
Subsystems (6)
v2 cover net: dsa: mt7628 embedded switch initial support
Hello,
This patch series adds initial support for the MediaTek MT7628 Embedded Switch.
The driver implements the basic functionality required to operate the switch using DSA. The hardware provides five internal Fast Ethernet user ports and one Gigabit port connected internally to the CPU MAC.
Bridge offloading is not yet supported.
Tested on an MT7628NN-based board.
changes since v1:
- changed port 6 phy-mode to internal
- cleaned up tag_mt7628 rcv function and mask defines
- fixed sorting error in drivers/net/dsa/ Kconfig and Makefile
- fixed sorting error in net/dsa/ Kconfig and Makefile
- fixed mt7628_mii_read/write return values on error
Thanks,
Joris
Joris Vaisvila (4):
dt-bindings: net: dsa: add MT7628 ESW
net: phy: mediatek: add phy driver for MT7628 built-in Fast Ethernet
PHYs
net: dsa: initial MT7628 tagging driver
net: dsa: initial support for MT7628 embedded switch
.../bindings/net/dsa/mediatek,mt7628-esw.yaml | 101 +++ drivers/net/dsa/Kconfig | 8 + drivers/net/dsa/Makefile | 1 + drivers/net/dsa/mt7628.c | 627 ++++++++++++++++++ drivers/net/phy/mediatek/Kconfig | 10 +- drivers/net/phy/mediatek/Makefile | 1 + drivers/net/phy/mediatek/mtk-fe-soc.c | 50 ++ include/net/dsa.h | 2 + net/dsa/Kconfig | 6 + net/dsa/Makefile | 1 + net/dsa/tag_mt7628.c | 89 +++ 11 files changed, 895 insertions(+), 1 deletion(-) create mode 100644 Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml create mode 100644 drivers/net/dsa/mt7628.c create mode 100644 drivers/net/phy/mediatek/mtk-fe-soc.c create mode 100644 net/dsa/tag_mt7628.c
Signature · 1 line
2.53.0
PATCH v2 1/4 dt-bindings: net: dsa: add MT7628 ESW
Add bindings for MT7628 SoC's Embedded Switch.
--- .../bindings/net/dsa/mediatek,mt7628-esw.yaml | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml
Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml new file +101 -0
@@ -0,0 +1,101 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/net/dsa/mediatek,mt7628-esw.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Mediatek MT7628 Embedded Ethernet Switch + +maintainers: + - Joris Vaisvila <[email protected]> + +description: + The MT7628 SoC's built-in Ethernet Switch is a five port switch with + integrated 10/100 PHYs. The switch registers are directly mapped in the SoC's + memory. The switch has an internally connected 1G CPU port and 5 user ports + connected to the built-in Fast Ethernet PHYs. + +unevaluatedProperties: false + +allOf: + - $ref: dsa.yaml#/$defs/ethernet-ports + +properties: + compatible: + const: mediatek,mt7628-esw + + reg: + maxItems: 1 + description: MMIO address of the switch + + resets: + items: + - description: Phandle of system reset controller with ESW reset index + - description: Phandle of system reset controller with EPHY reset index + + reset-names: + items: + - const: esw + - const: ephy + + mdio: + $ref: /schemas/net/mdio.yaml# + unevaluatedProperties: false + +required: + - compatible + - reg + - resets + - reset-names + +examples: + - | + switch0: switch@10110000 { + reg = <0x10110000 0x8000>; + + resets = <&sysc 23>, <&sysc 24>; + reset-names = "esw", "ephy"; + + compatible = "mediatek,mt7628-esw"; + + ports { + #address-cells = <1>; + #size-cells = <0>; + + port@0 { + reg = <0>; + phy-mode = "internal"; + }; + + port@1 { + reg = <1>; + phy-mode = "internal"; + }; + + port@2 { + reg = <2>; + phy-mode = "internal"; + }; + + port@3 { + reg = <3>; + phy-mode = "internal"; + }; + + port@4 { + reg = <4>; + phy-mode = "internal"; + }; + + port@6 { + reg = <6>; + phy-mode = "internal"; + ethernet = <ðernet>; + + fixed-link { + speed = <1000>; + full-duplex; + }; + }; + }; + };
Signature · 1 line
2.53.0
Rob Herring
maintainerRe: [PATCH net-next v2 1/4] dt-bindings: net: dsa: add MT7628 ESW
Quoted 33 lines · Joris Vaisvila
Add bindings for MT7628 SoC's Embedded Switch.
Signed-off-by Joris Vaisvila <[email protected]>--- .../bindings/net/dsa/mediatek,mt7628-esw.yaml | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yamlDocumentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml new file +19
-0@@ -0,0 +1,101 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/net/dsa/mediatek,mt7628-esw.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Mediatek MT7628 Embedded Ethernet Switch + +maintainers: + - Joris Vaisvila <[email protected]> + +description: + The MT7628 SoC's built-in Ethernet Switch is a five port switch with + integrated 10/100 PHYs. The switch registers are directly mapped in the SoC's + memory. The switch has an internally connected 1G CPU port and 5 user ports + connected to the built-in Fast Ethernet PHYs. + +unevaluatedProperties: false +
Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml line 29
Quoted 10 lines
Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml new file +10
-0+allOf: + - $ref: dsa.yaml#/$defs/ethernet-ports + +properties: + compatible: + const: mediatek,mt7628-esw + + reg: + maxItems: 1 + description: MMIO address of the switch
Drop the description.
Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml line 34
Quoted 5 lines
Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml new file +5
-0+ + resets: + items: + - description: Phandle of system reset controller with ESW reset index + - description: Phandle of system reset controller with EPHY reset index
Just describe these in terms of what they reset, not the provider.
Quoted 5 lines
Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml new file +5
-0+ + reset-names: + items: + - const: esw + - const: ephy
Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml line 49
Quoted 10 lines
Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml new file +10
-0+ + mdio: + $ref: /schemas/net/mdio.yaml# + unevaluatedProperties: false + +required: + - compatible + - reg + - resets + - reset-names
Surely 'ethernet-ports' is required?
Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml line 59
Quoted 10 lines
Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml new file +10
-0+ +examples: + - | + switch0: switch@10110000 { + reg = <0x10110000 0x8000>; + + resets = <&sysc 23>, <&sysc 24>; + reset-names = "esw", "ephy"; + + compatible = "mediatek,mt7628-esw";
compatible goes before reg.
Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml line 61
Quoted 2 lines
Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml new file +2
-0+ + ports {
"ethernet-ports" for new bindings.
Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml line 65
Quoted 4 lines
Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml new file +4
-0+ #address-cells = <1>; + #size-cells = <0>; + + port@0 {
And ethernet-port
Quoted 39 lines
Documentation/devicetree/bindings/net/dsa/mediatek,mt7628-esw.yaml new file +36
-0+ reg = <0>; + phy-mode = "internal"; + }; + + port@1 { + reg = <1>; + phy-mode = "internal"; + }; + + port@2 { + reg = <2>; + phy-mode = "internal"; + }; + + port@3 { + reg = <3>; + phy-mode = "internal"; + }; + + port@4 { + reg = <4>; + phy-mode = "internal"; + }; + + port@6 { + reg = <6>; + phy-mode = "internal"; + ethernet = <ðernet>; + + fixed-link { + speed = <1000>; + full-duplex; + }; + }; + }; + };Signature · 1 line
2.53.0
PATCH v2 2/4 net: phy: mediatek: add phy driver for MT7628 built-in Fast Ethernet PHYs
In reply to Joris VaisvilaThe Fast Ethernet PHYs present in the MT7628 SoCs require an undocumented bit to be set before they can establish 100mbps links.
This commit adds the Kconfig option MEDIATEK_FE_SOC_PHY and the corresponding driver mtk-fe-soc.c.
--- drivers/net/phy/mediatek/Kconfig | 10 +++++- drivers/net/phy/mediatek/Makefile | 1 + drivers/net/phy/mediatek/mtk-fe-soc.c | 50 +++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 drivers/net/phy/mediatek/mtk-fe-soc.c
drivers/net/phy/mediatek/Kconfig +9 -1
@@ -21,8 +21,16 @@ config MEDIATEK_GE_PHY common operations with MediaTek SoC built-in Gigabit Ethernet PHYs. +config MEDIATEK_FE_SOC_PHY + tristate "MediaTek SoC Fast Ethernet PHYs" + help + Support for MediaTek MT7628 built-in Fast Ethernet PHYs. + This driver only sets an initialization bit required for the PHY + to establish 100 Mbps links. All other PHY operations are handled + by the kernel's generic PHY code. + config MEDIATEK_GE_SOC_PHY - tristate "MediaTek SoC Ethernet PHYs" + tristate "MediaTek SoC Gigabit Ethernet PHYs" depends on ARM64 || COMPILE_TEST depends on ARCH_AIROHA || (ARCH_MEDIATEK && NVMEM_MTK_EFUSE) || \ COMPILE_TEST
drivers/net/phy/mediatek/Makefile +1 -0
@@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_MEDIATEK_2P5GE_PHY) += mtk-2p5ge.o +obj-$(CONFIG_MEDIATEK_FE_SOC_PHY) += mtk-fe-soc.o obj-$(CONFIG_MEDIATEK_GE_PHY) += mtk-ge.o obj-$(CONFIG_MEDIATEK_GE_SOC_PHY) += mtk-ge-soc.o obj-$(CONFIG_MTK_NET_PHYLIB) += mtk-phy-lib.o
drivers/net/phy/mediatek/mtk-fe-soc.c new file +50 -0
50 lines not on this page · raw message
Signature · 1 line
2.53.0
PATCH v2 3/4 net: dsa: initial MT7628 tagging driver
In reply to Joris VaisvilaAdd support for the MT7628 embedded switch's tag.
The MT7628 tag is merged with the VLAN TPID field when a VLAN is appended by the switch hardware. It is not installed if the VLAN tag is already there on ingress. Due to this hardware quirk the tag cannot be trusted for port 0 if we don't know that the VLAN was added by the hardware. As a workaround for this the switch is configured to always append the port PVID tag even if the incoming packet is already tagged. The tagging driver can then trust that the tag is always accurate and the whole VLAN tag can be removed on ingress as it's only metadata for the tagger.
On egress the MT7628 tag allows precise TX, but the correct VLAN tag from tag_8021q is still appended or the switch will not forward the packet.
--- include/net/dsa.h | 2 + net/dsa/Kconfig | 6 +++ net/dsa/Makefile | 1 + net/dsa/tag_mt7628.c | 89 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 net/dsa/tag_mt7628.c
include/net/dsa.h +2 -0
@@ -58,6 +58,7 @@ struct tc_action;#define DSA_TAG_PROTO_YT921X_VALUE 30#define DSA_TAG_PROTO_MXL_GSW1XX_VALUE 31#define DSA_TAG_PROTO_MXL862_VALUE 32 +#define DSA_TAG_PROTO_MT7628_VALUE 33enum dsa_tag_protocol {DSA_TAG_PROTO_NONE =DSA_TAG_PROTO_NONE_VALUE ,
@@ -93,6 +94,7 @@ enum dsa_tag_protocol {DSA_TAG_PROTO_YT921X =DSA_TAG_PROTO_YT921X_VALUE ,DSA_TAG_PROTO_MXL_GSW1XX =DSA_TAG_PROTO_MXL_GSW1XX_VALUE ,DSA_TAG_PROTO_MXL862 =DSA_TAG_PROTO_MXL862_VALUE , + DSA_TAG_PROTO_MT7628 = DSA_TAG_PROTO_MT7628_VALUE, };struct dsa_switch ;
net/dsa/Kconfig +6 -0
@@ -98,6 +98,12 @@ config NET_DSA_TAG_EDSA Say Y or M if you want to enable support for tagging frames for the Marvell switches which use EtherType DSA headers. +config NET_DSA_TAG_MT7628 + tristate "Tag driver for the MT7628 embedded switch" + help + Say Y or M if you want to enable support for tagging frames for the + switch embedded in the MT7628 SoC. + config NET_DSA_TAG_MTK tristate "Tag driver for Mediatek switches" help
net/dsa/Makefile +1 -0
@@ -27,6 +27,7 @@ obj-$(CONFIG_NET_DSA_TAG_GSWIP) += tag_gswip.o obj-$(CONFIG_NET_DSA_TAG_HELLCREEK) += tag_hellcreek.o obj-$(CONFIG_NET_DSA_TAG_KSZ) += tag_ksz.o obj-$(CONFIG_NET_DSA_TAG_LAN9303) += tag_lan9303.o +obj-$(CONFIG_NET_DSA_TAG_MT7628) += tag_mt7628.o obj-$(CONFIG_NET_DSA_TAG_MTK) += tag_mtk.o obj-$(CONFIG_NET_DSA_TAG_MXL_862XX) += tag_mxl862xx.o obj-$(CONFIG_NET_DSA_TAG_MXL_GSW1XX) += tag_mxl-gsw1xx.o
net/dsa/tag_mt7628.c new file +89 -0
89 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [PATCH net-next v2 3/4] net: dsa: initial MT7628 tagging driver
Quoted 12 lines · Joris Vaisvila
Add support for the MT7628 embedded switch's tag.
The MT7628 tag is merged with the VLAN TPID field when a VLAN is appended by the switch hardware. It is not installed if the VLAN tag is already there on ingress. Due to this hardware quirk the tag cannot be trusted for port 0 if we don't know that the VLAN was added by the hardware. As a workaround for this the switch is configured to always append the port PVID tag even if the incoming packet is already tagged. The tagging driver can then trust that the tag is always accurate and the whole VLAN tag can be removed on ingress as it's only metadata for the tagger.
Quoted 5 lines
On egress the MT7628 tag allows precise TX, but the correct VLAN tag from
tag_8021qis still appended or the switch will not forward the packet.Signed-off-by Joris Vaisvila <[email protected]>
Andrew
PATCH v2 4/4 net: dsa: initial support for MT7628 embedded switch
In reply to Joris VaisvilaAdd support for the MT7628 embedded switch.
The switch has 5 built-in 100Mbps user ports (ports 0-4) and one 1Gbps port that is internally attached to the SoCs CPU MAC and serves as the CPU port.
The switch hardware has a very limited 16 entry VLAN table. Configuring VLANs is the only way to control switch forwarding. Currently 6 entries are used by tag_8021q to isolate the ports. Double tag feature is enabled to force the switch to append the VLAN tag even if the incoming packet is already tagged, this simulates VLAN-unaware functionality and simplifies the tagger implementation.
--- drivers/net/dsa/Kconfig | 8 + drivers/net/dsa/Makefile | 1 + drivers/net/dsa/mt7628.c | 627 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 636 insertions(+) create mode 100644 drivers/net/dsa/mt7628.c
drivers/net/dsa/Kconfig +8 -0
@@ -28,6 +28,14 @@ source "drivers/net/dsa/hirschmann/Kconfig" source "drivers/net/dsa/lantiq/Kconfig" +config NET_DSA_MT7628 + tristate "MediaTek MT7628 Embedded Ethernet switch support" + select NET_DSA_TAG_MT7628 + select MEDIATEK_FE_SOC_PHY + help + This enables support for the built-in Ethernet switch found + in the MT7628 SoC. + config NET_DSA_MT7530 tristate "MediaTek MT7530 and MT7531 Ethernet switch support" select NET_DSA_TAG_MTK
drivers/net/dsa/Makefile +1 -0
@@ -6,6 +6,7 @@ obj-$(CONFIG_NET_DSA_KS8995) += ks8995.o obj-$(CONFIG_NET_DSA_MT7530) += mt7530.o obj-$(CONFIG_NET_DSA_MT7530_MDIO) += mt7530-mdio.o obj-$(CONFIG_NET_DSA_MT7530_MMIO) += mt7530-mmio.o +obj-$(CONFIG_NET_DSA_MT7628) += mt7628.o obj-$(CONFIG_NET_DSA_MV88E6060) += mv88e6060.o obj-$(CONFIG_NET_DSA_RZN1_A5PSW) += rzn1_a5psw.o obj-$(CONFIG_NET_DSA_SMSC_LAN9303) += lan9303-core.o
drivers/net/dsa/mt7628.c new file +627 -0
627 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [PATCH net-next v2 4/4] net: dsa: initial support for MT7628 embedded switch
Quoted 6 lines · Joris Vaisvila
Add support for the MT7628 embedded switch.
The switch has 5 built-in 100Mbps user ports (ports 0-4) and one 1Gbps port that is internally attached to the SoCs CPU MAC and serves as the CPU port.
Quoted 8 lines
The switch hardware has a very limited 16 entry VLAN table. Configuring VLANs is the only way to control switch forwarding. Currently 6 entries are used by
tag_8021qto isolate the ports. Double tag feature is enabled to force the switch to append the VLAN tag even if the incoming packet is already tagged, this simulates VLAN-unaware functionality and simplifies the tagger implementation.Signed-off-by Joris Vaisvila <[email protected]>
Andrew
Jakub Kicinski
maintainerRe: [PATCH net-next v2 4/4] net: dsa: initial support for MT7628 embedded switch
In reply to Joris VaisvilaThis is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: dsa: initial support for MT7628 embedded switch
Add support for the MT7628 embedded switch.
The switch has 5 built-in 100Mbps user ports (ports 0-4) and one 1Gbps port that is internally attached to the SoCs CPU MAC and serves as the CPU port.
drivers/net/dsa/Kconfig source "drivers/net/dsa/hirschmann/Kconfig" · line 34
Quoted 11 lines
drivers/net/dsa/Kconfig +4
-0@@ -28,6 +28,14 @@ source "drivers/net/dsa/hirschmann/Kconfig" source "drivers/net/dsa/lantiq/Kconfig" +config NET_DSA_MT7628 + tristate "MediaTek MT7628 Embedded Ethernet switch support" + select NET_DSA_TAG_MT7628 + select MEDIATEK_FE_SOC_PHY
Does this configuration need to select NET_DSA_TAG_8021Q?
The driver relies on the tag_8021q mechanism for switch port isolation. If the kernel is configured without NET_DSA_TAG_8021Q, dsa_tag_8021q_register() will return -EOPNOTSUPP, causing the switch to boot with broken port isolation.
Quoted 4 lines
drivers/net/dsa/Kconfig +4
-0+ help + This enables support for the built-in Ethernet switch found + in the MT7628 SoC. +
[ ... ]
Quoted 3 lines
drivers/net/dsa/mt7628.c new file +0
-0
[ ... ]
drivers/net/dsa/mt7628.c line 180
Quoted 9 lines
drivers/net/dsa/mt7628.c new file +9
-0+static int mt7628_mii_read (struct mii_bus *bus,int port,int regnum) +{ +struct mt7628_esw *esw = bus->priv; +int ret; +u32 val; + + ret =regmap_read_poll_timeout (esw->regmap,MT7628_ESW_REG_PCR1 , val, + !(val &MT7628_ESW_PCR1_RD_DONE ),10 , +5000 );
Will this polling logic cause an MDIO timeout?
If RD_DONE is a completion status bit that remains 1 after a successful operation, this first poll will wait indefinitely for the bit to become 0.
If RD_DONE is an active-low busy bit, the second poll below waits for the controller to become busy instead of waiting for it to be idle.
Quoted 36 lines
drivers/net/dsa/mt7628.c new file +36
-0+if (ret) + goto out; + + ret =regmap_write (esw->regmap,MT7628_ESW_REG_PCR0 , +FIELD_PREP (MT7628_ESW_PCR0_CPU_PHY_REG , + regnum) | +FIELD_PREP (MT7628_ESW_PCR0_CPU_PHY_ADDR , + port) |MT7628_ESW_PCR0_RD_PHY_CMD ); +if (ret) + goto out; + + ret =regmap_read_poll_timeout (esw->regmap,MT7628_ESW_REG_PCR1 , val, + (val &MT7628_ESW_PCR1_RD_DONE ),10 , +5000 ); +out: +if (ret) { +dev_err (&bus->dev,"read failed. MDIO timeout?\n" ); +return ret; + } +return FIELD_GET (MT7628_ESW_PCR1_RD_DATA , val); +} + +static int mt7628_mii_write (struct mii_bus *bus,int port,int regnum,u16 dat) +{ +struct mt7628_esw *esw = bus->priv; +u32 val; +int ret; + + ret =regmap_read_poll_timeout (esw->regmap,MT7628_ESW_REG_PCR1 , val, + !(val &MT7628_ESW_PCR1_WT_DONE ),10 , +5000 ); +if (ret) + goto out; + + ret = regmap_write(esw->regmap, MT7628_ESW_REG_PCR0, + FIELD_PREP(MT7628_ESW_PCR0_WT_NWAY_DATA, dat) |
drivers/net/dsa/mt7628.c line 226
Quoted 10 lines
drivers/net/dsa/mt7628.c new file +10
-0+FIELD_PREP (MT7628_ESW_PCR0_CPU_PHY_REG , + regnum) | +FIELD_PREP (MT7628_ESW_PCR0_CPU_PHY_ADDR , + port) | MT7628_ESW_PCR0_WT_PHY_CMD); +if (ret) + goto out; + + ret =regmap_read_poll_timeout (esw->regmap,MT7628_ESW_REG_PCR1 , val, + (val &MT7628_ESW_PCR1_WT_DONE ),10 , +5000 );
Is this second poll waiting for the controller to become busy instead of idle, similar to the read function?
Quoted 8 lines
drivers/net/dsa/mt7628.c new file +8
-0+out: +if (ret) { +dev_err (&bus->dev,"write failed. MDIO timeout?\n" ); +return ret; + } +return 0 ; +} +
drivers/net/dsa/mt7628.c line 244
Quoted 10 lines
drivers/net/dsa/mt7628.c new file +10
-0+static int mt7628_setup_internal_mdio (struct dsa_switch *ds) +{ +struct mt7628_esw *esw = ds->priv; +struct device_node *mdio; +struct mii_bus *bus; +int ret =0 ; + + mdio =of_get_available_child_by_name (ds->dev->of_node,"mdio" ); + + bus =devm_mdiobus_alloc (esw->dev);
Can allocating devm_ resources in the DSA .setup callback cause a sysfs name collision?
The .setup callback is executed whenever the DSA tree binds. Since devm_ resources are tied to the platform device (esw->dev) rather than the DSA tree lifecycle, they are not released when the DSA tree unbinds. On a subsequent re-bind, allocating a new MDIO bus with the same ID will fail because the original bus was never unregistered.
Quoted 22 lines
drivers/net/dsa/mt7628.c new file +22
-0+if (!bus) { + ret = -ENOMEM ; + goto out_put_node; + } + + bus->name ="MT7628 internal MDIO bus" ; +snprintf (bus->id,MII_BUS_ID_SIZE ,"%s-mii" ,dev_name (ds->dev)); + bus->priv = esw; + bus->read = mt7628_mii_read; + bus->write = mt7628_mii_write; + bus->parent = esw->dev; +if (!mdio) { + ds->user_mii_bus = bus; + bus->phy_mask = ~ds->phys_mii_mask; + } + + ret =devm_of_mdiobus_register (esw->dev, bus, mdio); + +out_put_node: +of_node_put (mdio); +return ret; +}
[ ... ]
Quoted 6 lines
drivers/net/dsa/mt7628.c new file +6
-0+static int mt7628_setup (struct dsa_switch *ds) +{ +struct mt7628_esw *esw = ds->priv; +int ret; + +reset_control_reset (esw->rst_esw);
drivers/net/dsa/mt7628.c line 421
Quoted 10 lines
drivers/net/dsa/mt7628.c new file +10
-0+usleep_range (1000 ,2000 ); +reset_control_reset (esw->rst_ephy); +usleep_range (1000 ,2000 ); +/* +* all MMIO reads hang if esw is not out of reset +* ephy needs extra time to get out of reset or it ends up misconfigured +*/ +mt7628_switch_init (ds); +rtnl_lock (); +dsa_tag_8021q_register (ds,htons (ETH_P_8021Q ));
Is it safe to ignore the return value of dsa_tag_8021q_register()?
If the registration fails, the core function frees the allocated context but does not clear ds->tag_8021q_ctx. The setup will continue, and the DSA core will later dereference the non-NULL ds->tag_8021q_ctx, leading to a use-after-free crash.
Also, if mt7628_setup_internal_mdio() below fails, the setup returns an error without unregistering the tagger, which leaks the tag_8021q context.
Quoted 5 lines
drivers/net/dsa/mt7628.c new file +5
-0+rtnl_unlock (); + + ret =mt7628_setup_internal_mdio (ds); +return ret; +}
[ ... ]
drivers/net/dsa/mt7628.c line 533
Quoted 3 lines
drivers/net/dsa/mt7628.c new file +3
-0+static struct dsa_switch_ops mt7628_switch_ops = { + .get_tag_protocol = mt7628_get_tag_proto, + .setup = mt7628_setup,
Does this switch ops structure need a .teardown callback?
Without calling dsa_tag_8021q_unregister() in a .teardown callback, the tag_8021q context memory allocated during .setup will be permanently leaked whenever the DSA switch is unbound or the module is removed.
Quoted 6 lines
drivers/net/dsa/mt7628.c new file +6
-0+ .port_enable = mt7628_port_enable, + .port_disable = mt7628_port_disable, + .phylink_get_caps = mt7628_phylink_get_caps, + .tag_8021q_vlan_add = mt7628_dsa_8021q_vlan_add, + .tag_8021q_vlan_del = mt7628_dsa_8021q_vlan_del, +};
Signature · 1 line
pw-bot: cr
Re: [PATCH net-next v2 4/4] net: dsa: initial support for MT7628 embedded switch
Hi Jakub, thank you for the review.
Quoted 24 lines · Jakub Kicinski
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: dsa: initial support for MT7628 embedded switchAdd support for the MT7628 embedded switch.
The switch has 5 built-in 100Mbps user ports (ports 0-4) and one 1Gbps port that is internally attached to the SoCs CPU MAC and serves as the CPU port.drivers/net/dsa/Kconfig source "drivers/net/dsa/hirschmann/Kconfig" · line 34
Quoted 11 lines
drivers/net/dsa/Kconfig +4
-0@@ -28,6 +28,14 @@ source "drivers/net/dsa/hirschmann/Kconfig" source "drivers/net/dsa/lantiq/Kconfig" +config NET_DSA_MT7628 + tristate "MediaTek MT7628 Embedded Ethernet switch support" + select NET_DSA_TAG_MT7628 + select MEDIATEK_FE_SOC_PHYDoes this configuration need to select
NET_DSA_TAG_8021Q?
Quoted 5 lines
The driver relies on the
tag_8021qmechanism for switch port isolation. If the kernel is configured withoutNET_DSA_TAG_8021Q,dsa_tag_8021q_register()will return-EOPNOTSUPP, causing the switch to boot with broken port isolation.
tag_8021q.o is in DSA core, there's no option to enable or disable it.
Quoted 13 lines
[ ... ]
drivers/net/dsa/Kconfig
Quoted 9 lines
drivers/net/dsa/Kconfig +9
-0+static int mt7628_mii_read(struct mii_bus *bus, int port, int regnum) +{ + struct mt7628_esw *esw = bus->priv; + int ret; + u32 val; + + ret = regmap_read_poll_timeout(esw->regmap, MT7628_ESW_REG_PCR1, val, + !(val & MT7628_ESW_PCR1_RD_DONE), 10, + 5000);Will this polling logic cause an MDIO timeout?
Quoted 6 lines
If
RD_DONEis a completion status bit that remains 1 after a successful operation, this first poll will wait indefinitely for the bit to become 0.If
RD_DONEis an active-low busy bit, the second poll below waits for the controller to become busy instead of waiting for it to be idle.
RD_DONE gets set when the read operations completes and is reset on register read, so this will not cause a timeout.
Quoted 41 lines
Quoted 41 lines
drivers/net/dsa/Kconfig +41
-0+ if (ret) + goto out; + + ret = regmap_write(esw->regmap, MT7628_ESW_REG_PCR0, + FIELD_PREP(MT7628_ESW_PCR0_CPU_PHY_REG, + regnum) | + FIELD_PREP(MT7628_ESW_PCR0_CPU_PHY_ADDR, + port) | MT7628_ESW_PCR0_RD_PHY_CMD); + if (ret) + goto out; + + ret = regmap_read_poll_timeout(esw->regmap, MT7628_ESW_REG_PCR1, val, + (val & MT7628_ESW_PCR1_RD_DONE), 10, + 5000); +out: + if (ret) { + dev_err(&bus->dev, "read failed. MDIO timeout?\n"); + return ret; + } + return FIELD_GET(MT7628_ESW_PCR1_RD_DATA, val); +} + +static int mt7628_mii_write(struct mii_bus *bus, int port, int regnum, u16 dat) +{ + struct mt7628_esw *esw = bus->priv; + u32 val; + int ret; + + ret = regmap_read_poll_timeout(esw->regmap, MT7628_ESW_REG_PCR1, val, + !(val & MT7628_ESW_PCR1_WT_DONE), 10, + 5000); + if (ret) + goto out; + + ret = regmap_write(esw->regmap, MT7628_ESW_REG_PCR0, + FIELD_PREP(MT7628_ESW_PCR0_WT_NWAY_DATA, dat) | + FIELD_PREP(MT7628_ESW_PCR0_CPU_PHY_REG, + regnum) | + FIELD_PREP(MT7628_ESW_PCR0_CPU_PHY_ADDR, + port) | MT7628_ESW_PCR0_WT_PHY_CMD); + if (ret)
Quoted 9 lines
drivers/net/dsa/Kconfig
Quoted 5 lines
drivers/net/dsa/Kconfig +5
-0+ goto out; + + ret = regmap_read_poll_timeout(esw->regmap, MT7628_ESW_REG_PCR1, val, + (val & MT7628_ESW_PCR1_WT_DONE), 10, + 5000);Is this second poll waiting for the controller to become busy instead of idle, similar to the read function?
WT_DONE is set when the data is ready and reset on read, same as RD_DONE.
Quoted 22 lines
Quoted 8 lines
drivers/net/dsa/Kconfig +8
-0+out: + if (ret) { + dev_err(&bus->dev, "write failed. MDIO timeout?\n"); + return ret; + } + return 0; +} +drivers/net/dsa/Kconfig
Quoted 10 lines
drivers/net/dsa/Kconfig +10
-0+static int mt7628_setup_internal_mdio(struct dsa_switch *ds) +{ + struct mt7628_esw *esw = ds->priv; + struct device_node *mdio; + struct mii_bus *bus; + int ret = 0; + + mdio = of_get_available_child_by_name(ds->dev->of_node, "mdio"); + + bus = devm_mdiobus_alloc(esw->dev);Can allocating
devm_resources in the DSA .setup callback cause a sysfs name collision?
Quoted 6 lines
The .setup callback is executed whenever the DSA tree binds. Since
devm_resources are tied to the platform device (esw->dev) rather than the DSA tree lifecycle, they are not released when the DSA tree unbinds. On a subsequent re-bind, allocating a new MDIO bus with the same ID will fail because the original bus was never unregistered.
I'm not sure how this would work. Other switches appear to be doing the same. For example mt7530-mmio.c does priv->dev = &pdev->dev and later uses priv->dev to register the mdio bus. Is this a real issue?
Quoted 20 lines
[ ... ]
Quoted 6 lines
drivers/net/dsa/Kconfig +6
-0+static int mt7628_setup(struct dsa_switch *ds) +{ + struct mt7628_esw *esw = ds->priv; + int ret; + + reset_control_reset(esw->rst_esw);drivers/net/dsa/Kconfig
Quoted 10 lines
drivers/net/dsa/Kconfig +10
-0+ usleep_range(1000, 2000); + reset_control_reset(esw->rst_ephy); + usleep_range(1000, 2000); + /* + * all MMIO reads hang if esw is not out of reset + * ephy needs extra time to get out of reset or it ends up misconfigured + */ + mt7628_switch_init(ds); + rtnl_lock(); + dsa_tag_8021q_register(ds, htons(ETH_P_8021Q));Is it safe to ignore the return value of
dsa_tag_8021q_register()?
Quoted 8 lines
If the registration fails, the core function frees the allocated context but does not clear
ds->tag_8021q_ctx. The setup will continue, and the DSA core will later dereference the non-NULLds->tag_8021q_ctx, leading to a use-after-free crash.Also, if
mt7628_setup_internal_mdio()below fails, the setup returns an error without unregistering the tagger, which leaks thetag_8021qcontext.
No, will fix in v3.
Quoted 12 lines
Quoted 5 lines
drivers/net/dsa/Kconfig +5
-0+ rtnl_unlock(); + + ret = mt7628_setup_internal_mdio(ds); + return ret; +}[ ... ]
drivers/net/dsa/Kconfig
Quoted 3 lines
drivers/net/dsa/Kconfig +3
-0+static struct dsa_switch_ops mt7628_switch_ops = { + .get_tag_protocol = mt7628_get_tag_proto, + .setup = mt7628_setup,Does this switch ops structure need a .teardown callback?
Quoted 4 lines
Without calling
dsa_tag_8021q_unregister()in a .teardown callback, thetag_8021qcontext memory allocated during .setup will be permanently leaked whenever the DSA switch is unbound or the module is removed.
Yes, will fix in V3.
Thanks,
Joris