* [PATCH net-next 0/2] net: ethernet: realtek: Add r8169soc Ethernet driver for Realtek STB SoCs
@ 2026-12-04 10:07 Eric Wang
2026-12-04 10:07 ` [PATCH net-next 1/2] dt-bindings: net: Add Realtek STB SoC r8169soc Ethernet controller Eric Wang
2026-12-04 10:07 ` [PATCH net-next 2/2] net: ethernet: realtek: Add r8169soc Ethernet driver for Realtek STB SoCs Eric Wang
0 siblings, 2 replies; 5+ messages in thread
From: Eric Wang @ 2026-12-04 10:07 UTC (permalink / raw)
To: Andrew Lunn, David S . Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
Philipp Zabel
Cc: netdev, devicetree, linux-kernel, Eric Wang, Eleanor Lin
This series adds the Devicetree binding and Ethernet driver for the
embedded r8169soc Ethernet controller on Realtek STB SoCs, starting
with the RTD1625 SoC ("realtek,rtd1625-r8169soc").
Why a separate r8169soc driver instead of extending r8169:
Although the r8169soc MAC originally evolved from the RTL8168 descriptor
ring architecture, the hardware IP and software driver are developed and
maintained by a separate SoC division within Realtek, and the hardware
integration differs fundamentally from the PCIe r8169 NICs:
- Bus interface: r8169 is a PCIe driver relying on PCI configuration
space, BAR mapping, MSI/MSI-X, and PCIE ASPM, whereas r8169soc is an
on-chip platform device integrated into the SoC internal bus (RBUS/DBUS)
with no PCIe interface or PCI configuration registers.
- SoC clocks, resets, and syscon integration: r8169soc requires coordinating
multiple SoC clocks (etn_250m, etn_sys, misc), reset lines (gmac, gphy,
misc), and SoC system controller regmaps (realtek,iso and realtek,m2tmx)
for PHY bypass mode, DBUS clock gating, power-cut, and hardware autoload
handshaking.
- Register and feature divergence: SoC generations introduce custom MMIO
and OCP register layouts (such as dedicated OCP MDIO access at
0xde08..0xde0c and remapped TX no-close descriptor tail/close index
registers at 0x20/0x22). Keeping r8169soc as a dedicated platform driver
avoids polluting the PCIe-centric r8169 driver with SoC-specific
platform, clock, reset, and syscon logic.
Patch 1 adds the DT binding schema (realtek,r8169soc.yaml) and MAINTAINERS
entry.
Patch 2 adds the r8169soc driver supporting RTD1625 and its internal Fast
Ethernet PHY (via CONFIG_REALTEK_PHY).
Eric Wang (2):
dt-bindings: net: Add Realtek STB SoC r8169soc Ethernet controller
net: ethernet: realtek: Add r8169soc Ethernet driver for Realtek STB
SoCs
.../bindings/net/realtek,r8169soc.yaml | 102 +
MAINTAINERS | 8 +
drivers/net/ethernet/realtek/Kconfig | 19 +-
drivers/net/ethernet/realtek/Makefile | 1 +
.../net/ethernet/realtek/r8169soc/Makefile | 3 +
.../ethernet/realtek/r8169soc/rtl8169soc.c | 2375 +++++++++++++++++
.../ethernet/realtek/r8169soc/rtl8169soc.h | 513 ++++
7 files changed, 3020 insertions(+), 1 deletion(-)
create mode 100644 Documentation/devicetree/bindings/net/realtek,r8169soc.yaml
create mode 100644 drivers/net/ethernet/realtek/r8169soc/Makefile
create mode 100644 drivers/net/ethernet/realtek/r8169soc/rtl8169soc.c
create mode 100644 drivers/net/ethernet/realtek/r8169soc/rtl8169soc.h
--
2.49.0
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH net-next 1/2] dt-bindings: net: Add Realtek STB SoC r8169soc Ethernet controller 2026-12-04 10:07 [PATCH net-next 0/2] net: ethernet: realtek: Add r8169soc Ethernet driver for Realtek STB SoCs Eric Wang @ 2026-12-04 10:07 ` Eric Wang 2026-12-05 10:32 ` netdev-bot+sashiko 2026-12-04 10:07 ` [PATCH net-next 2/2] net: ethernet: realtek: Add r8169soc Ethernet driver for Realtek STB SoCs Eric Wang 1 sibling, 1 reply; 5+ messages in thread From: Eric Wang @ 2026-12-04 10:07 UTC (permalink / raw) To: Andrew Lunn, David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Rob Herring, Krzysztof Kozlowski, Conor Dooley, Philipp Zabel Cc: netdev, devicetree, linux-kernel, Eric Wang, Eleanor Lin Add Devicetree binding documentation for the embedded r8169soc Ethernet controller found on Realtek STB SoCs, starting with the RTD1625 SoC ("realtek,rtd1625-r8169soc"). The schema is named realtek,r8169soc.yaml so additional Realtek STB SoC variants sharing this controller architecture can be documented in the same binding. Signed-off-by: Eric Wang <ericwang@realtek.com> --- .../bindings/net/realtek,r8169soc.yaml | 102 ++++++++++++++++++ MAINTAINERS | 6 ++ 2 files changed, 108 insertions(+) create mode 100644 Documentation/devicetree/bindings/net/realtek,r8169soc.yaml diff --git a/Documentation/devicetree/bindings/net/realtek,r8169soc.yaml b/Documentation/devicetree/bindings/net/realtek,r8169soc.yaml new file mode 100644 index 000000000000..604ff1ddfb0a --- /dev/null +++ b/Documentation/devicetree/bindings/net/realtek,r8169soc.yaml @@ -0,0 +1,102 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/net/realtek,r8169soc.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Realtek STB SoC r8169soc Ethernet Controller + +maintainers: + - Eric Wang <ericwang@realtek.com> + +description: + The embedded Realtek STB SoC r8169soc Ethernet controller integrates a + Fast Ethernet MAC with an internal Fast Ethernet PHY, using ISO and + MAIN2 system controller regmaps for SoC-specific PHY and bus control. + Since the controller connects directly to the internal PHY, external + phy-mode and phy-handle properties are not required. + +allOf: + - $ref: ethernet-controller.yaml# + +properties: + compatible: + const: realtek,rtd1625-r8169soc + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + clocks: + items: + - description: 250MHz reference clock + - description: Ethernet system clock + - description: ISO misc clock + + clock-names: + items: + - const: etn_250m + - const: etn_sys + - const: misc + + resets: + items: + - description: GMAC reset + - description: GPHY reset + - description: Misc reset + + reset-names: + items: + - const: gmac + - const: gphy + - const: misc + + power-domains: + maxItems: 1 + + realtek,iso: + $ref: /schemas/types.yaml#/definitions/phandle + description: + Phandle to the ISO system controller syscon node used for Ethernet + PHY interrupt clearing, bypass mode control, and autoload status. + + realtek,m2tmx: + $ref: /schemas/types.yaml#/definitions/phandle + description: + Phandle to the MAIN2 misc syscon node used for Ethernet DBUS clock + gating and power-cut configuration. + +required: + - compatible + - reg + - interrupts + - clocks + - clock-names + - resets + - reset-names + - realtek,iso + - realtek,m2tmx + +unevaluatedProperties: false + +examples: + - | + #include <dt-bindings/interrupt-controller/arm-gic.h> + + ethernet@16000 { + compatible = "realtek,rtd1625-r8169soc"; + reg = <0x16000 0x1000>; + interrupts = <GIC_SPI 22 IRQ_TYPE_LEVEL_HIGH>; + clocks = <&ic 11>, <&ic 12>, <&cc 5>; + clock-names = "etn_250m", "etn_sys", "misc"; + resets = <&ic 7>, <&ic 8>, <&cc 0>; + reset-names = "gmac", "gphy", "misc"; + pinctrl-names = "default"; + pinctrl-0 = <&etn_led_loc0_pins>; + power-domains = <&pd>; + local-mac-address = [00 10 20 30 40 50]; + realtek,iso = <&iso>; + realtek,m2tmx = <&m2tmx>; + }; diff --git a/MAINTAINERS b/MAINTAINERS index 05b2d9e36b6d..319ec17b5ce6 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -23693,6 +23693,12 @@ S: Maintained F: Documentation/devicetree/bindings/net/pse-pd/realtek,pse-mcu-gen1.yaml F: drivers/net/pse-pd/realtek-pse-mcu* +REALTEK R8169SOC ETHERNET DRIVER +M: Eric Wang <ericwang@realtek.com> +L: netdev@vger.kernel.org +S: Maintained +F: Documentation/devicetree/bindings/net/realtek,r8169soc.yaml + REALTEK RTL83xx SMI DSA ROUTER CHIPS M: Linus Walleij <linusw@kernel.org> M: Luiz Angelo Daros de Luca <luizluca@gmail.com> -- 2.49.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH net-next 1/2] dt-bindings: net: Add Realtek STB SoC r8169soc Ethernet controller 2026-12-04 10:07 ` [PATCH net-next 1/2] dt-bindings: net: Add Realtek STB SoC r8169soc Ethernet controller Eric Wang @ 2026-12-05 10:32 ` netdev-bot+sashiko 0 siblings, 0 replies; 5+ messages in thread From: netdev-bot+sashiko @ 2026-12-05 10:32 UTC (permalink / raw) To: ericwang Cc: andrew+netdev, davem, edumazet, kuba, pabeni, robh, krzk+dt, conor+dt, p.zabel, netdev, devicetree, linux-kernel, eleanor.lin Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin. Low priority / nit picks for your consideration: - [Low] Two required properties in the new binding, `realtek,iso` and `realtek,m2tmx` (realtek,r8169soc.yaml lines 59-69 and 80-81), are… - [Low] The binding file is named realtek,r8169soc.yaml, but its only compatible is "realtek,rtd1625-r8169soc" (line 24), and `$id` (line 4)… - [Low] The description (lines 12-17) says the MAC is wired directly to an internal PHY and that external phy-mode/phy-handle are not needed. - [Low] dt-bindings: net: pinctrl-names precedes pinctrl-0 in example DTS -- dt-bindings: net: Add Realtek STB SoC r8169soc Ethernet controller This adds a devicetree binding for the embedded r8169soc Ethernet controller found on Realtek STB SoCs, starting with the RTD1625 ("realtek,rtd1625-r8169soc"). It also adds a MAINTAINERS entry for the new binding file. > diff --git a/Documentation/devicetree/bindings/net/realtek,r8169soc.yaml b/Documentation/devicetree/bindings/net/realtek,r8169soc.yaml > new file mode 100644 > index 0000000000000..604ff1ddfb0af > --- /dev/null > +++ b/Documentation/devicetree/bindings/net/realtek,r8169soc.yaml > @@ -0,0 +1,102 @@ > +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) > +%YAML 1.2 > +--- > +$id: http://devicetree.org/schemas/net/realtek,r8169soc.yaml# > +$schema: http://devicetree.org/meta-schemas/core.yaml# [ ... ] > +description: > + The embedded Realtek STB SoC r8169soc Ethernet controller integrates a > + Fast Ethernet MAC with an internal Fast Ethernet PHY, using ISO and > + MAIN2 system controller regmaps for SoC-specific PHY and bus control. > + Since the controller connects directly to the internal PHY, external > + phy-mode and phy-handle properties are not required. > + > +allOf: > + - $ref: ethernet-controller.yaml# [Severity: Low] The description says the MAC is wired only to the internal PHY. Should the schema enforce that? The $ref to ethernet-controller.yaml has no restrictions, and unevaluatedProperties is false, so phy-mode, phy-connection-type, phy-handle, fixed-link and managed are all accepted. A board DT that describes an external RGMII PHY through phy-handle, or that uses a fixed-link, would pass dt_binding_check and dtbs_check. The driver in the next patch ignores these properties. It hard-codes the interface mode in its PHY connect path in rtl8169soc.c: phy_mode = tp->chip->features & RTL_FEATURE_GMII ? PHY_INTERFACE_MODE_GMII : PHY_INTERFACE_MODE_MII; That means an incorrect DT is silently ignored instead of being rejected. Could the binding constrain these properties? For example, it could use phy-mode: const: internal, and set phy-handle and fixed-link to false. The binding also doesn't describe an mdio subnode or a PHY node for the internal PHY. > + > +properties: > + compatible: > + const: realtek,rtd1625-r8169soc [Severity: Low] Should the file be named realtek,rtd1625-r8169soc.yaml, with the $id updated to match? writing-bindings.rst says: Bindings files should be named like compatible: vendor,device.yaml. In case of multiple compatibles in the binding, use one of the fallbacks or a more generic name, yet still matching compatible style. The commit message says: The schema is named realtek,r8169soc.yaml so additional Realtek STB SoC variants sharing this controller architecture can be documented in the same binding. There is only one compatible here, though. The other RTD1625 bindings follow the compatible-based pattern (realtek,rtd1625-gpio.yaml, realtek,rtd1625-pinctrl.yaml, realtek,rtd1625-nor.yaml, realtek,rtd1625-systimer.yaml). Later variants can still go into a file named after the first compatible. Also, r8169 is the name of the Linux PCIe driver, not of a hardware block (the chips are RTL8169/RTL8168). writing-bindings.rst advises against referring to Linux or "device driver" in bindings. Compatible strings become ABI once merged. Would a name based on the hardware work better than the -r8169soc suffix? [ ... ] > + realtek,iso: > + $ref: /schemas/types.yaml#/definitions/phandle > + description: > + Phandle to the ISO system controller syscon node used for Ethernet > + PHY interrupt clearing, bypass mode control, and autoload status. > + > + realtek,m2tmx: > + $ref: /schemas/types.yaml#/definitions/phandle > + description: > + Phandle to the MAIN2 misc syscon node used for Ethernet DBUS clock > + gating and power-cut configuration. [Severity: Low] Where are the ISO and MAIN2 syscon nodes that these phandles point to documented? Both properties are required, but neither block has a binding in the tree, even at the end of the series. Searching Documentation/devicetree/bindings for rtd1625, realtek,iso and m2tmx finds only the gpio, pinctrl, spi-nor and timer bindings, plus this file. The RTD1625 SoC dtsi (arch/arm64/boot/dts/realtek/kent.dtsi) has no ISO or MAIN2 syscon node either. It also has no clock or reset controller node for the required clocks and resets. Older Realtek dtsi files (rtd16xx.dtsi, rtd139x.dtsi, rtd129x.dtsi) use this pattern: iso: syscon@7000 { compatible = "syscon", "simple-mfd"; writing-bindings.rst asks that a syscon block not use "syscon" alone. It should have a compatible specific enough to infer the register layout. The binding doesn't constrain the phandle targets, so dtschema can't check that &iso and &m2tmx point at the right blocks. The driver in the series reaches registers through these regmaps: RTD1625_M2TMX_ETN_DBUS_CTRL, RTD1625_M2TMX_PWRCUT_ETN, and the ISO PHY interrupt, bypass and autoload registers. That layout is hard-coded in rtl8169soc.c and isn't described anywhere in DT. Could bindings with specific compatibles be added for these syscon blocks? [ ... ] > @@ -0,0 +1,102 @@ > +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) > +%YAML 1.2 > +--- > +$id: http://devicetree.org/schemas/net/realtek,r8169soc.yaml# > +$schema: http://devicetree.org/meta-schemas/core.yaml# > + > +title: Realtek STB SoC r8169soc Ethernet Controller > + > +maintainers: > + - Eric Wang <ericwang@realtek.com> > + > +description: > + The embedded Realtek STB SoC r8169soc Ethernet controller integrates a > + Fast Ethernet MAC with an internal Fast Ethernet PHY, using ISO and > + MAIN2 system controller regmaps for SoC-specific PHY and bus control. > + Since the controller connects directly to the internal PHY, external > + phy-mode and phy-handle properties are not required. > + > +allOf: > + - $ref: ethernet-controller.yaml# > + > +properties: > + compatible: > + const: realtek,rtd1625-r8169soc > + > + reg: > + maxItems: 1 > + > + interrupts: > + maxItems: 1 > + > + clocks: > + items: > + - description: 250MHz reference clock > + - description: Ethernet system clock > + - description: ISO misc clock > + > + clock-names: > + items: > + - const: etn_250m > + - const: etn_sys > + - const: misc > + > + resets: > + items: > + - description: GMAC reset > + - description: GPHY reset > + - description: Misc reset > + > + reset-names: > + items: > + - const: gmac > + - const: gphy > + - const: misc > + > + power-domains: > + maxItems: 1 > + > + realtek,iso: > + $ref: /schemas/types.yaml#/definitions/phandle > + description: > + Phandle to the ISO system controller syscon node used for Ethernet > + PHY interrupt clearing, bypass mode control, and autoload status. > + > + realtek,m2tmx: > + $ref: /schemas/types.yaml#/definitions/phandle > + description: > + Phandle to the MAIN2 misc syscon node used for Ethernet DBUS clock > + gating and power-cut configuration. > + > +required: > + - compatible > + - reg > + - interrupts > + - clocks > + - clock-names > + - resets > + - reset-names > + - realtek,iso > + - realtek,m2tmx > + > +unevaluatedProperties: false > + > +examples: > + - | > + #include <dt-bindings/interrupt-controller/arm-gic.h> > + > + ethernet@16000 { > + compatible = "realtek,rtd1625-r8169soc"; > + reg = <0x16000 0x1000>; > + interrupts = <GIC_SPI 22 IRQ_TYPE_LEVEL_HIGH>; > + clocks = <&ic 11>, <&ic 12>, <&cc 5>; > + clock-names = "etn_250m", "etn_sys", "misc"; > + resets = <&ic 7>, <&ic 8>, <&cc 0>; > + reset-names = "gmac", "gphy", "misc"; > + pinctrl-names = "default"; > + pinctrl-0 = <&etn_led_loc0_pins>; > + power-domains = <&pd>; > + local-mac-address = [00 10 20 30 40 50]; > + realtek,iso = <&iso>; > + realtek,m2tmx = <&m2tmx>; > + }; [Severity: Low] In the example node, pinctrl-names is placed before pinctrl-0. Will this example pass scripts/dtc/dt-check-style in strict mode? submitting-patches.rst asks that examples in new bindings pass scripts/dtc/dt-check-style in strict mode without warnings. The strict property-order rule (check_property_order(), through _pair_pinctrl_names()) requires pinctrl-names to follow the highest-numbered pinctrl-N property in the same node. With the current order, the example will warn: property 'pinctrl-names' must be placed after 'pinctrl-0' The second patch in the series doesn't touch the example, so the warning remains at the end of the series. There is no runtime impact. Could you swap the two lines so that pinctrl-0 comes first and pinctrl-names follows it? -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204100746.1165612-1-ericwang%40realtek.com ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH net-next 2/2] net: ethernet: realtek: Add r8169soc Ethernet driver for Realtek STB SoCs 2026-12-04 10:07 [PATCH net-next 0/2] net: ethernet: realtek: Add r8169soc Ethernet driver for Realtek STB SoCs Eric Wang 2026-12-04 10:07 ` [PATCH net-next 1/2] dt-bindings: net: Add Realtek STB SoC r8169soc Ethernet controller Eric Wang @ 2026-12-04 10:07 ` Eric Wang 2026-12-05 10:32 ` netdev-bot+sashiko 1 sibling, 1 reply; 5+ messages in thread From: Eric Wang @ 2026-12-04 10:07 UTC (permalink / raw) To: Andrew Lunn, David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Rob Herring, Krzysztof Kozlowski, Conor Dooley, Philipp Zabel Cc: netdev, devicetree, linux-kernel, Eric Wang, Eleanor Lin Add initial support for the embedded r8169soc Ethernet controller on Realtek STB SoCs, starting with RTD1625 ("realtek,rtd1625-r8169soc"). The r8169soc controller shares its core descriptor ring design with RTL8168/8169 MACs, but integrates directly into the SoC bus without PCIe registers and pairs with an embedded Fast Ethernet PHY (PHY ID 0x001cc800, supported via CONFIG_REALTEK_PHY) and SoC-specific clock, reset, and syscon interfaces. Update Realtek Kconfig to allow building on ARCH_REALTEK or COMPILE_TEST. Signed-off-by: Eric Wang <ericwang@realtek.com> --- MAINTAINERS | 2 + drivers/net/ethernet/realtek/Kconfig | 19 +- drivers/net/ethernet/realtek/Makefile | 1 + .../net/ethernet/realtek/r8169soc/Makefile | 3 + .../ethernet/realtek/r8169soc/rtl8169soc.c | 2375 +++++++++++++++++ .../ethernet/realtek/r8169soc/rtl8169soc.h | 513 ++++ 6 files changed, 2912 insertions(+), 1 deletion(-) create mode 100644 drivers/net/ethernet/realtek/r8169soc/Makefile create mode 100644 drivers/net/ethernet/realtek/r8169soc/rtl8169soc.c create mode 100644 drivers/net/ethernet/realtek/r8169soc/rtl8169soc.h diff --git a/MAINTAINERS b/MAINTAINERS index 319ec17b5ce6..f3419cae00af 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -136,6 +136,7 @@ L: netdev@vger.kernel.org S: Maintained F: Documentation/devicetree/bindings/net/realtek,rtl8125.yaml F: drivers/net/ethernet/realtek/r8169* +X: drivers/net/ethernet/realtek/r8169soc/ 8250/16?50 (AND CLONE UARTS) SERIAL DRIVER M: Greg Kroah-Hartman <gregkh@linuxfoundation.org> @@ -23698,6 +23699,7 @@ M: Eric Wang <ericwang@realtek.com> L: netdev@vger.kernel.org S: Maintained F: Documentation/devicetree/bindings/net/realtek,r8169soc.yaml +F: drivers/net/ethernet/realtek/r8169soc/ REALTEK RTL83xx SMI DSA ROUTER CHIPS M: Linus Walleij <linusw@kernel.org> diff --git a/drivers/net/ethernet/realtek/Kconfig b/drivers/net/ethernet/realtek/Kconfig index 5818a9a500a8..4cc1a0f955a2 100644 --- a/drivers/net/ethernet/realtek/Kconfig +++ b/drivers/net/ethernet/realtek/Kconfig @@ -6,7 +6,7 @@ config NET_VENDOR_REALTEK bool "Realtek devices" default y - depends on PCI + depends on PCI || ARCH_REALTEK || COMPILE_TEST help If you have a network (Ethernet) card belonging to this class, say Y. @@ -126,4 +126,21 @@ config RTASE To compile this driver as a module, choose M here: the module will be called rtase. This is recommended. +config R8169SOC + tristate "Realtek STB SoC 8169soc ethernet support" + depends on ARCH_REALTEK || COMPILE_TEST + depends on OF + depends on HAS_IOMEM + depends on HAS_DMA + select CRC32 + select MFD_SYSCON + select PHYLIB + select REALTEK_PHY + help + Say Y here if you have an embedded Realtek STB SoC Ethernet + interface. + + To compile this driver as a module, choose M here: the module + will be called r8169soc. This is recommended. + endif # NET_VENDOR_REALTEK diff --git a/drivers/net/ethernet/realtek/Makefile b/drivers/net/ethernet/realtek/Makefile index 12a9c399f40c..f49cb5cc3cab 100644 --- a/drivers/net/ethernet/realtek/Makefile +++ b/drivers/net/ethernet/realtek/Makefile @@ -8,4 +8,5 @@ obj-$(CONFIG_8139TOO) += 8139too.o r8169-y += r8169_main.o r8169_firmware.o r8169_phy_config.o r8169-$(CONFIG_R8169_LEDS) += r8169_leds.o obj-$(CONFIG_R8169) += r8169.o +obj-$(CONFIG_R8169SOC) += r8169soc/ obj-$(CONFIG_RTASE) += rtase/ diff --git a/drivers/net/ethernet/realtek/r8169soc/Makefile b/drivers/net/ethernet/realtek/r8169soc/Makefile new file mode 100644 index 000000000000..aecf88cfe7df --- /dev/null +++ b/drivers/net/ethernet/realtek/r8169soc/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only +r8169soc-y += rtl8169soc.o +obj-$(CONFIG_R8169SOC) += r8169soc.o diff --git a/drivers/net/ethernet/realtek/r8169soc/rtl8169soc.c b/drivers/net/ethernet/realtek/r8169soc/rtl8169soc.c new file mode 100644 index 000000000000..0d3e68bd7512 --- /dev/null +++ b/drivers/net/ethernet/realtek/r8169soc/rtl8169soc.c @@ -0,0 +1,2375 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* rtl8169soc.c: the embedded Realtek STB SoC 8169soc Ethernet driver. + * + * Copyright (c) 2002 Realtek Semiconductor Corp. + * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw> + * Copyright (c) 2003-2007 Francois Romieu <romieu@fr.zoreil.com> + * Copyright (c) 2014 YuKuen Wu <yukuen@realtek.com> + * Copyright (c) 2015-2026 Eric Wang <ericwang@realtek.com> + */ + +#include <linux/clk.h> +#include <linux/delay.h> +#include <linux/dma-mapping.h> +#include <linux/etherdevice.h> +#include <linux/ethtool.h> +#include <linux/if_vlan.h> +#include <linux/interrupt.h> +#include <linux/io.h> +#include <linux/ip.h> +#include <linux/mfd/syscon.h> +#include <linux/module.h> +#include <linux/netdevice.h> +#include <linux/of.h> +#include <linux/phy.h> +#include <linux/platform_device.h> +#include <linux/prefetch.h> +#include <linux/regmap.h> +#include <linux/reset.h> +#include <linux/unaligned.h> +#include <net/netdev_queues.h> + +#include "rtl8169soc.h" + +MODULE_AUTHOR("Eric Wang <ericwang@realtek.com>"); +MODULE_DESCRIPTION("RealTek STB SoC r8169soc Ethernet driver"); +MODULE_SOFTDEP("pre: realtek"); +MODULE_LICENSE("GPL"); + +static void rtl_lock_work(struct rtl8169_private *tp) +{ + mutex_lock(&tp->wk.mutex); +} + +static void rtl_unlock_work(struct rtl8169_private *tp) +{ + mutex_unlock(&tp->wk.mutex); +} + +static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c, + unsigned long usecs, int n, bool high) +{ + int i; + + for (i = 0; i < n; i++) { + if (c->check(tp) == high) + return true; + fsleep(usecs); + } + + if (net_ratelimit()) + netdev_err(tp->dev, "%s == %d (loop: %d, delay: %lu).\n", + c->msg, !high, n, usecs); + return false; +} + +static bool rtl_loop_wait_high(struct rtl8169_private *tp, + const struct rtl_cond *c, + unsigned long d, int n) +{ + return rtl_loop_wait(tp, c, d, n, true); +} + +static bool rtl_loop_wait_low(struct rtl8169_private *tp, + const struct rtl_cond *c, + unsigned long d, int n) +{ + return rtl_loop_wait(tp, c, d, n, false); +} + +DECLARE_RTL_COND(rtl_eriar_cond) +{ + return RTL_R32(tp, ERIAR) & ERIAR_FLAG; +} + +DECLARE_RTL_COND(rtl_link_list_ready_cond) +{ + return RTL_R8(tp, MCU) & LINK_LIST_RDY; +} + +static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg) +{ + if (reg & 0xffff0001) { + if (net_ratelimit()) + netdev_err(tp->dev, "Invalid ocp reg %x!\n", reg); + return true; + } + return false; +} + +static void rtl_ocp_write(struct rtl8169_private *tp, u32 reg, u32 value) +{ + unsigned long flags; + unsigned int wdata; + + if (rtl_ocp_reg_failure(tp, reg)) + return; + + wdata = OCPDR_WRITE_CMD | + (((reg >> 1) & OCPDR_REG_MASK) << OCPDR_REG_SHIFT) | + (value & OCPDR_DATA_MASK); + + raw_spin_lock_irqsave(&tp->mac_ocp_lock, flags); + RTL_W32(tp, OCPDR, wdata); + raw_spin_unlock_irqrestore(&tp->mac_ocp_lock, flags); +} + +static u32 rtl_ocp_read(struct rtl8169_private *tp, u32 reg) +{ + unsigned long flags; + unsigned int wdata; + unsigned int rdata; + + if (rtl_ocp_reg_failure(tp, reg)) + return 0; + + wdata = OCPDR_READ_CMD | + (((reg >> 1) & OCPDR_REG_MASK) << OCPDR_REG_SHIFT); + + raw_spin_lock_irqsave(&tp->mac_ocp_lock, flags); + RTL_W32(tp, OCPDR, wdata); + rdata = RTL_R32(tp, OCPDR); + raw_spin_unlock_irqrestore(&tp->mac_ocp_lock, flags); + + return rdata & OCPDR_DATA_MASK; +} + +DECLARE_RTL_COND(rtl_int_phyar_cond) +{ + return rtl_ocp_read(tp, OCP_SW_GPHY_MDCMDIO_CMD) & SW_GPHY_MDCMDIO_BUSY; +} + +DECLARE_RTL_COND(rtl_phyar_cond) +{ + return RTL_R32(tp, PHYAR) & PHYAR_FLAG; +} + +static int __int_mdio_read(struct rtl8169_private *tp, int reg) +{ + int value; + + RTL_W32(tp, PHYAR, + PHYAR_READ_CMD | ((reg & PHYAR_REG_MASK) << PHYAR_REG_SHIFT)); + + value = rtl_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ? + RTL_R32(tp, PHYAR) & PHY_DATA_MASK : -ETIMEDOUT; + + /* According to hardware specs a 20us delay is required after read + * complete indication, but before sending next command. + */ + fsleep(20); + + return value; +} + +static int __int_mdio_write(struct rtl8169_private *tp, int reg, int value) +{ + int ret; + + RTL_W32(tp, PHYAR, PHYAR_WRITE_CMD | + ((reg & PHYAR_REG_MASK) << PHYAR_REG_SHIFT) | + (value & PHY_DATA_MASK)); + + ret = rtl_loop_wait_low(tp, &rtl_phyar_cond, 25, 20) ? 0 : -ETIMEDOUT; + /* According to hardware specs a 20us delay is required after write + * complete indication, but before sending next command. + */ + fsleep(20); + + return ret; +} + +static int int_mdio_read(struct rtl8169_private *tp, int page, int reg) +{ + int value; + + tp->chip->mdio_lock(tp); + + if (page != CURRENT_MDIO_PAGE) { + value = __int_mdio_write(tp, PHY_PAGSR_REG, page); + if (value) + goto out_unlock; + } + + value = __int_mdio_read(tp, reg); + +out_unlock: + tp->chip->mdio_unlock(tp); + return value; +} + +static int int_mdio_write(struct rtl8169_private *tp, int page, int reg, + int value) +{ + int ret; + + tp->chip->mdio_lock(tp); + + if (page != CURRENT_MDIO_PAGE) { + ret = __int_mdio_write(tp, PHY_PAGSR_REG, page); + if (ret) + goto out_unlock; + } + + ret = __int_mdio_write(tp, reg, value); + +out_unlock: + tp->chip->mdio_unlock(tp); + return ret; +} + +static int int_ocp_mdio_read(struct rtl8169_private *tp, int page, int reg) +{ + if (page != CURRENT_MDIO_PAGE) + rtl_ocp_write(tp, OCP_SW_GPHY_MDCMDIO_PAGE, page); + rtl_ocp_write(tp, OCP_SW_GPHY_MDCMDIO_CMD, reg & PHYAR_REG_MASK); + + return rtl_loop_wait_low(tp, &rtl_int_phyar_cond, 25, 20) ? + rtl_ocp_read(tp, OCP_SW_GPHY_MDCMDIO_DATA) & PHY_DATA_MASK : + -ETIMEDOUT; +} + +static int int_ocp_mdio_write(struct rtl8169_private *tp, int page, int reg, + int value) +{ + if (page != CURRENT_MDIO_PAGE) + rtl_ocp_write(tp, OCP_SW_GPHY_MDCMDIO_PAGE, page); + rtl_ocp_write(tp, OCP_SW_GPHY_MDCMDIO_DATA, value); + rtl_ocp_write(tp, OCP_SW_GPHY_MDCMDIO_CMD, + SW_GPHY_MDCMDIO_WRITE_CMD | (reg & PHYAR_REG_MASK)); + + return rtl_loop_wait_low(tp, &rtl_int_phyar_cond, 25, 20) ? + 0 : -ETIMEDOUT; +} + +static void dummy_mdio_lock(struct rtl8169_private *tp) +{ +} + +static void dummy_mdio_unlock(struct rtl8169_private *tp) +{ +} + +/* Chips with RTL_FEATURE_OCP_MDIO access PHY registers via dedicated OCP + * registers (0xde08..0xde0c) that do not conflict with hardware MAC polling, + * while chip->mdio_lock is used by int_mdio_read/write (PHYAR) during init. + */ +static void rtl_init_mdio_ops(struct rtl8169_private *tp) +{ + if (tp->chip->features & RTL_FEATURE_OCP_MDIO) { + tp->chip->mdio_write = int_ocp_mdio_write; + tp->chip->mdio_read = int_ocp_mdio_read; + } else { + tp->chip->mdio_write = int_mdio_write; + tp->chip->mdio_read = int_mdio_read; + } +} + +static void __rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask, + u32 val, int type) +{ + WARN_ON((addr & 3) || (mask == 0)); + RTL_W32(tp, ERIDR, val); + RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr); + + rtl_loop_wait_low(tp, &rtl_eriar_cond, 100, 100); +} + +static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask, + u32 val) +{ + __rtl_eri_write(tp, addr, mask, val, ERIAR_EXGMAC); +} + +static u32 __rtl_eri_read(struct rtl8169_private *tp, int addr, int type) +{ + RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr); + + return rtl_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ? + RTL_R32(tp, ERIDR) : ~0; +} + +static u32 rtl_eri_read(struct rtl8169_private *tp, int addr) +{ + return __rtl_eri_read(tp, addr, ERIAR_EXGMAC); +} + +static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 p, u32 m) +{ + u32 val = rtl_eri_read(tp, addr); + + if (val == ~0U) { + dev_dbg(tp_to_dev(tp), "failed to read ERI addr 0x%x\n", addr); + return; + } + + rtl_eri_write(tp, addr, ERIAR_MASK_1111, (val & ~m) | p); +} + +static void rtl_eri_set_bits(struct rtl8169_private *tp, int addr, u32 p) +{ + rtl_w0w1_eri(tp, addr, p, 0); +} + +static void rtl_eri_clear_bits(struct rtl8169_private *tp, int addr, u32 m) +{ + rtl_w0w1_eri(tp, addr, 0, m); +} + +static void rtl_reset_packet_filter(struct rtl8169_private *tp) +{ + rtl_eri_clear_bits(tp, ERI_FTR_MCU_CTRL, ERI_FTR_MCU_EN); + rtl_eri_set_bits(tp, ERI_FTR_MCU_CTRL, ERI_FTR_MCU_EN); +} + +static u16 rtl_get_events(struct rtl8169_private *tp) +{ + return RTL_R16(tp, INTR_STATUS); +} + +static void rtl_ack_events(struct rtl8169_private *tp, u16 bits) +{ + RTL_W16(tp, INTR_STATUS, bits); +} + +static void rtl_irq_disable(struct rtl8169_private *tp) +{ + RTL_W16(tp, INTR_MASK, 0); +} + +static void rtl_irq_enable(struct rtl8169_private *tp) +{ + RTL_W16(tp, INTR_MASK, tp->irq_mask); +} + +static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp) +{ + rtl_irq_disable(tp); + rtl_ack_events(tp, 0xffff); + RTL_R8(tp, CHIP_CMD); +} + +static netdev_features_t rtl8169_fix_features(struct net_device *dev, + netdev_features_t features) +{ + struct rtl8169_private *tp = netdev_priv(dev); + + if (dev->mtu > TD_MSS_MAX) + features &= ~NETIF_F_ALL_TSO; + + if (dev->mtu > JUMBO_1K && !tp->chip->jumbo_tx_csum) + features &= ~NETIF_F_IP_CSUM; + + return features; +} + +static void rtl_set_rx_config_features(struct rtl8169_private *tp, + netdev_features_t features) +{ + u32 rx_config = RTL_R32(tp, RX_CONFIG); + + if (features & NETIF_F_RXALL) + rx_config |= RX_CONFIG_ACCEPT_ERR_MASK; + else + rx_config &= ~RX_CONFIG_ACCEPT_ERR_MASK; + + RTL_W32(tp, RX_CONFIG, rx_config); +} + +static void __rtl8169_set_features(struct rtl8169_private *tp, + netdev_features_t features) +{ + netif_addr_lock_bh(tp->dev); + rtl_set_rx_config_features(tp, features); + netif_addr_unlock_bh(tp->dev); + + if (features & NETIF_F_RXCSUM) + tp->cp_cmd |= RX_CHK_SUM; + else + tp->cp_cmd &= ~RX_CHK_SUM; + + if (features & NETIF_F_HW_VLAN_CTAG_RX) + tp->cp_cmd |= RX_VLAN; + else + tp->cp_cmd &= ~RX_VLAN; + + RTL_W16(tp, C_PLUS_CMD, tp->cp_cmd); + RTL_R16(tp, C_PLUS_CMD); +} + +static int rtl8169_set_features(struct net_device *dev, + netdev_features_t features) +{ + struct rtl8169_private *tp = netdev_priv(dev); + + rtl_lock_work(tp); + if (test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status)) + __rtl8169_set_features(tp, features); + rtl_unlock_work(tp); + + return 0; +} + +static u32 rtl8169_tx_vlan_tag(struct sk_buff *skb) +{ + return skb_vlan_tag_present(skb) ? + TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb)) : 0x00; +} + +static void rtl8169_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb) +{ + u32 opts2 = le32_to_cpu(desc->opts2); + + if (opts2 & RX_VLAN_TAG) + __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), + swab16(opts2 & 0xffff)); +} + +static void rtl_hw_phy_config(struct rtl8169_private *tp) +{ + /* power down PHY; genphy_soft_reset() in rtl8169_init_phy() will clear BMCR_PDOWN */ + phy_modify(tp->phydev, MII_BMCR, 0, BMCR_PDOWN); + + tp->chip->hw_phy_config(tp); +} + +static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag) +{ + if (!test_and_set_bit(flag, tp->wk.flags)) + schedule_work(&tp->wk.work); +} + +static void __rtl_rar_set(struct rtl8169_private *tp, const u8 *addr) +{ + rtl_unlock_config_regs(tp); + + RTL_W32(tp, MAC4, get_unaligned_le16(addr + 4)); + RTL_R32(tp, MAC4); + + RTL_W32(tp, MAC0, get_unaligned_le32(addr)); + RTL_R32(tp, MAC0); + + rtl_lock_config_regs(tp); +} + +static void rtl_rar_set(struct rtl8169_private *tp, const u8 *addr) +{ + rtl_lock_work(tp); + if (test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status)) + __rtl_rar_set(tp, addr); + rtl_unlock_work(tp); +} + +static void rtl8169_init_phy(struct rtl8169_private *tp) +{ + rtl_hw_phy_config(tp); + + /* disable now_is_oob */ + RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB); + + /* We may have called phy_speed_down before */ + phy_speed_up(tp->phydev); + + genphy_soft_reset(tp->phydev); +} + +static int rtl_set_mac_address(struct net_device *dev, void *p) +{ + struct rtl8169_private *tp = netdev_priv(dev); + struct sockaddr *addr = p; + + if (!is_valid_ether_addr(addr->sa_data)) { + dev_dbg(tp_to_dev(tp), "invalid MAC address %pM\n", + addr->sa_data); + return -EADDRNOTAVAIL; + } + + eth_hw_addr_set(dev, addr->sa_data); + + ether_addr_copy(tp->mac_addr, addr->sa_data); + + rtl_rar_set(tp, addr->sa_data); + + return 0; +} + +static void rtl_init_rxcfg(struct rtl8169_private *tp) +{ + /* disable RX128_INT_EN to reduce CPU loading */ + RTL_W32(tp, RX_CONFIG, RX_DMA_BURST | RX_EARLY_OFF); +} + +static void rtl8169_init_ring_indexes(struct rtl8169_private *tp) +{ + tp->dirty_tx = 0; + tp->cur_tx = 0; + tp->cur_rx = 0; +} + +DECLARE_RTL_COND(rtl_chipcmd_cond) +{ + return RTL_R8(tp, CHIP_CMD) & CMD_RESET; +} + +static void rtl_hw_reset(struct rtl8169_private *tp) +{ + RTL_W8(tp, CHIP_CMD, CMD_RESET); + + rtl_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100); +} + +static void rtl_rx_close(struct rtl8169_private *tp) +{ + RTL_W32(tp, RX_CONFIG, RTL_R32(tp, RX_CONFIG) & ~RX_CONFIG_ACCEPT_MASK); +} + +DECLARE_RTL_COND(rtl_txcfg_empty_cond) +{ + return RTL_R32(tp, TX_CONFIG) & TXCFG_EMPTY; +} + +DECLARE_RTL_COND(rtl_rxtx_empty_cond) +{ + return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY; +} + +static void rtl_wait_txrx_fifo_empty(struct rtl8169_private *tp) +{ + rtl_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42); + rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42); +} + +static void rtl_disable_rxdvgate(struct rtl8169_private *tp) +{ + RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN); +} + +static void rtl_enable_rxdvgate(struct rtl8169_private *tp) +{ + RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN); + fsleep(2000); + rtl_wait_txrx_fifo_empty(tp); +} + +static void rtl_set_tx_config_registers(struct rtl8169_private *tp) +{ + /* Set DMA burst size and Interframe Gap Time */ + RTL_W32(tp, TX_CONFIG, (TX_DMA_BURST << TX_DMA_SHIFT) | + (INTER_FRAME_GAP << TX_INTER_FRAME_GAP_SHIFT)); +} + +static void rtl_set_rx_max_size(struct rtl8169_private *tp) +{ + /* Low hurts. Let's disable the filtering. */ + RTL_W16(tp, RX_MAX_SIZE, RX_BUF_SIZE); +} + +static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp) +{ + RTL_W32(tp, TX_DESC_START_ADDR_HIGH, ((u64)tp->tx_phy_addr) >> 32); + RTL_W32(tp, TX_DESC_START_ADDR_LOW, + ((u64)tp->tx_phy_addr) & DMA_BIT_MASK(32)); + RTL_W32(tp, RX_DESC_ADDR_HIGH, ((u64)tp->rx_phy_addr) >> 32); + RTL_W32(tp, RX_DESC_ADDR_LOW, + ((u64)tp->rx_phy_addr) & DMA_BIT_MASK(32)); +} + +static void rtl_set_rx_mode(struct net_device *dev) +{ + u32 rx_mode = ACCEPT_BROADCAST | ACCEPT_MY_PHYS | ACCEPT_MULTICAST; + /* Multicast hash filter */ + u32 mc_filter[2] = { 0xffffffff, 0xffffffff }; + struct rtl8169_private *tp = netdev_priv(dev); + u32 tmp; + + if (!test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status)) + return; + + if (dev->flags & IFF_PROMISC) { + rx_mode |= ACCEPT_ALL_PHYS; + } else if (!(dev->flags & IFF_MULTICAST)) { + rx_mode &= ~ACCEPT_MULTICAST; + } else if (netdev_mc_count(dev) > MC_FILTER_LIMIT || + (dev->flags & IFF_ALLMULTI)) { + /* Too many to filter perfectly -- accept all multicasts. */ + } else { + struct netdev_hw_addr *ha; + + mc_filter[1] = 0; + mc_filter[0] = 0; + netdev_for_each_mc_addr(ha, dev) { + u32 bit_nr = eth_hw_addr_crc(ha) >> 26; + + mc_filter[bit_nr >> 5] |= BIT(bit_nr & 31); + } + } + + tmp = mc_filter[0]; + + mc_filter[0] = swab32(mc_filter[1]); + mc_filter[1] = swab32(tmp); + + RTL_W32(tp, MAR0 + 4, mc_filter[1]); + RTL_W32(tp, MAR0 + 0, mc_filter[0]); + + tmp = RTL_R32(tp, RX_CONFIG); + RTL_W32(tp, RX_CONFIG, (tmp & ~RX_CONFIG_ACCEPT_OK_MASK) | rx_mode); +} + +static void rtl_led_set(struct rtl8169_private *tp) +{ + RTL_W32(tp, LEDSEL, tp->chip->led_cfg); +} + +static void rtl_hw_start(struct rtl8169_private *tp) +{ + u32 phy_status; + + rtl_unlock_config_regs(tp); + + rtl_set_rx_max_size(tp); + + tp->cp_cmd |= RTL_R16(tp, C_PLUS_CMD) | PKT_CNTR_DISABLE | INTT_3; + + RTL_W16(tp, C_PLUS_CMD, tp->cp_cmd); + + RTL_W16(tp, INTR_MITIGATE, 0x5151); + + rtl_set_rx_tx_desc_registers(tp); + + rtl_set_tx_config_registers(tp); + + if (tp->chip->features & RTL_FEATURE_TX_NO_CLOSE) { + /* enable tx no close mode */ + rtl_ocp_write(tp, OCP_TCR_0, + rtl_ocp_read(tp, OCP_TCR_0) | + (SEL_PAGE_FLAG | TX_CLOSE_DISABLE)); + } + + /* TX FIFO threshold */ + rtl_ocp_write(tp, OCP_TFIFOFULL_TH_0, 0x0006); + rtl_ocp_write(tp, OCP_TFIFOFULL_TH_1, 0x0010); + + /* RX FIFO threshold */ + rtl_ocp_write(tp, OCP_RFIFOFULL_TH_0, 0x0002); + rtl_ocp_write(tp, OCP_RFIFOFULL_TH_1, 0x0008); + + if (tp->chip->features & RTL_FEATURE_ADJUST_FIFO) { + phy_status = rtl_ocp_read(tp, OCP_FPGA_GPHY0); + if ((phy_status & GPHY0_SPEED_MASK) == GPHY0_SPEED_1000) { + /* 1000 Mbps */ + rtl_ocp_write(tp, OCP_RFIFONFULL_TH_0, 0x0088); + rtl_ocp_write(tp, OCP_RFIFOEMPTY_TH_0, 0x00a8); + } else { + /* 10/100 Mbps */ + rtl_ocp_write(tp, OCP_RFIFONFULL_TH_0, 0x0038); + rtl_ocp_write(tp, OCP_RFIFOEMPTY_TH_0, 0x0048); + } + } else { + rtl_ocp_write(tp, OCP_RFIFONFULL_TH_0, 0x0038); + rtl_ocp_write(tp, OCP_RFIFOEMPTY_TH_0, 0x0048); + } + + RTL_W8(tp, CONFIG5, RTL_R8(tp, CONFIG5) & ~LAN_WAKE); + RTL_W8(tp, CONFIG3, RTL_R8(tp, CONFIG3) & ~MAGIC_PKT); + + RTL_W32(tp, TX_CONFIG, RTL_R32(tp, TX_CONFIG) | TXCFG_AUTO_FIFO); + + rtl_reset_packet_filter(tp); + rtl_eri_write(tp, ERI_PLA_CKCTRL_0, ERIAR_MASK_0011, 0x1d8f); + + rtl_disable_rxdvgate(tp); + + RTL_W8(tp, MAX_TX_PACKET_SIZE, EARLY_SIZE); + + rtl_eri_write(tp, ERI_VMQ_CONTROL, ERIAR_MASK_0011, 0x0000); + rtl_eri_write(tp, ERI_RSS_CONTROL_0, ERIAR_MASK_0011, 0x0000); + + rtl_w0w1_eri(tp, ERI_MISC_2, 0x01, 0x06); + rtl_eri_clear_bits(tp, ERI_EEE_CR, ERI_PAUSE_TX_EN); + + /* disable aspm and clock request before access ephy */ + RTL_W8(tp, CONFIG2, RTL_R8(tp, CONFIG2) & ~CLKREQ_EN_LOCK); + RTL_W8(tp, CONFIG5, RTL_R8(tp, CONFIG5) & ~ASPM_EN); + + rtl_lock_config_regs(tp); + + RTL_W8(tp, CHIP_CMD, CMD_TX_ENB | CMD_RX_ENB); + + netif_addr_lock_bh(tp->dev); + rtl_set_rx_config_features(tp, tp->dev->features); + rtl_set_rx_mode(tp->dev); + netif_addr_unlock_bh(tp->dev); + + RTL_W16(tp, MULTI_INTR, RTL_R16(tp, MULTI_INTR) & 0xf000); + + rtl_led_set(tp); + + rtl_irq_enable(tp); +} + +static int rtl8169_change_mtu(struct net_device *dev, int new_mtu) +{ + WRITE_ONCE(dev->mtu, new_mtu); + netdev_update_features(dev); + + return 0; +} + +static void rtl8169_mark_to_asic(struct rx_desc *desc) +{ + u32 eor = le32_to_cpu(desc->opts1) & RING_END; + + desc->opts2 = 0; + /* Force memory writes to complete before releasing descriptor */ + dma_wmb(); + WRITE_ONCE(desc->opts1, cpu_to_le32(DESC_OWN | eor | RX_BUF_SIZE)); +} + +static struct page *rtl8169_alloc_rx_data(struct rtl8169_private *tp, + struct rx_desc *desc) +{ + struct device *d = tp_to_dev(tp); + int node = dev_to_node(d); + dma_addr_t mapping; + struct page *data; + + data = alloc_pages_node(node, GFP_KERNEL, get_order(RX_BUF_SIZE)); + if (!data) + return NULL; + + mapping = dma_map_page(d, data, 0, RX_BUF_SIZE, DMA_FROM_DEVICE); + if (unlikely(dma_mapping_error(d, mapping))) { + if (net_ratelimit()) + netdev_err(tp->dev, "Failed to map RX DMA!\n"); + __free_pages(data, get_order(RX_BUF_SIZE)); + return NULL; + } + + desc->addr = cpu_to_le64(mapping); + rtl8169_mark_to_asic(desc); + + return data; +} + +static void rtl8169_rx_clear(struct rtl8169_private *tp) +{ + int i; + + for (i = 0; i < NUM_RX_DESC && tp->rx_databuff[i]; i++) { + dma_unmap_page(tp_to_dev(tp), + le64_to_cpu(tp->rx_desc_array[i].addr), + RX_BUF_SIZE, DMA_FROM_DEVICE); + __free_pages(tp->rx_databuff[i], get_order(RX_BUF_SIZE)); + tp->rx_databuff[i] = NULL; + tp->rx_desc_array[i].addr = 0; + tp->rx_desc_array[i].opts1 = 0; + } +} + +static int rtl8169_rx_fill(struct rtl8169_private *tp) +{ + int i; + + for (i = 0; i < NUM_RX_DESC; i++) { + struct page *data; + + data = rtl8169_alloc_rx_data(tp, tp->rx_desc_array + i); + if (!data) { + rtl8169_rx_clear(tp); + return -ENOMEM; + } + tp->rx_databuff[i] = data; + } + + /* mark as last descriptor in the ring */ + tp->rx_desc_array[NUM_RX_DESC - 1].opts1 |= cpu_to_le32(RING_END); + + return 0; +} + +static int rtl8169_init_ring(struct rtl8169_private *tp) +{ + rtl8169_init_ring_indexes(tp); + + memset(tp->tx_skb, 0x0, sizeof(tp->tx_skb)); + memset(tp->rx_databuff, 0x0, sizeof(tp->rx_databuff)); + + return rtl8169_rx_fill(tp); +} + +static void rtl8169_unmap_tx_skb(struct rtl8169_private *tp, unsigned int entry) +{ + struct tx_desc *desc = tp->tx_desc_array + entry; + struct ring_info *tx_skb = tp->tx_skb + entry; + + dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr), tx_skb->len, + DMA_TO_DEVICE); + + memset(desc, 0, sizeof(*desc)); + memset(tx_skb, 0, sizeof(*tx_skb)); +} + +static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start, + unsigned int n) +{ + unsigned int i; + + for (i = 0; i < n; i++) { + unsigned int entry = (start + i) % NUM_TX_DESC; + struct ring_info *tx_skb = tp->tx_skb + entry; + unsigned int len = tx_skb->len; + + if (len) { + struct sk_buff *skb = tx_skb->skb; + + rtl8169_unmap_tx_skb(tp, entry); + if (skb) { + tp->dev->stats.tx_dropped++; + dev_kfree_skb_any(skb); + } + } + } +} + +static void rtl8169_tx_clear(struct rtl8169_private *tp) +{ + rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC); + netdev_reset_queue(tp->dev); + tp->cur_tx = 0; + tp->dirty_tx = 0; +} + +static void rtl8169_cleanup(struct rtl8169_private *tp) +{ + napi_disable(&tp->napi); + + /* Give a racing hard_start_xmit a few cycles to complete. */ + synchronize_net(); + + rtl8169_irq_mask_and_ack(tp); + + rtl_rx_close(tp); + + rtl_enable_rxdvgate(tp); + /* Wait for MAC DMA to settle before reset */ + fsleep(2000); + + rtl_hw_reset(tp); + + rtl8169_tx_clear(tp); + rtl8169_init_ring_indexes(tp); + + if (tp->chip->features & RTL_FEATURE_TX_NO_CLOSE) { + /* disable tx no close mode */ + rtl_ocp_write(tp, OCP_TCR_0, + rtl_ocp_read(tp, OCP_TCR_0) & + ~(SEL_PAGE_FLAG | TX_CLOSE_DISABLE)); + } +} + +static void rtl_reset_work(struct rtl8169_private *tp) +{ + struct net_device *dev = tp->dev; + int i; + + netif_stop_queue(dev); + + rtl8169_cleanup(tp); + + for (i = 0; i < NUM_RX_DESC; i++) + rtl8169_mark_to_asic(tp->rx_desc_array + i); + + napi_enable(&tp->napi); + rtl_hw_start(tp); + + netif_wake_queue(dev); +} + +static void rtl8169_tx_timeout(struct net_device *dev, unsigned int txqueue) +{ + struct rtl8169_private *tp = netdev_priv(dev); + + netdev_warn(dev, "TX timeout, resetting MAC\n"); + rtl_schedule_task(tp, RTL_FLAG_TASK_TX_TIMEOUT); +} + +static int rtl8169_tx_map(struct rtl8169_private *tp, const u32 *opts, u32 len, + void *addr, unsigned int entry, bool desc_own) +{ + struct tx_desc *txd = tp->tx_desc_array + entry; + struct device *d = tp_to_dev(tp); + dma_addr_t mapping; + u32 opts1; + int ret; + + mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE); + ret = dma_mapping_error(d, mapping); + if (unlikely(ret)) { + if (net_ratelimit()) + netdev_err(tp->dev, "Failed to map TX data!\n"); + return ret; + } + + txd->addr = cpu_to_le64(mapping); + txd->opts2 = cpu_to_le32(opts[1]); + + opts1 = opts[0] | len; + if (entry == NUM_TX_DESC - 1) + opts1 |= RING_END; + if (desc_own) + opts1 |= DESC_OWN; + txd->opts1 = cpu_to_le32(opts1); + + tp->tx_skb[entry].len = len; + + return 0; +} + +static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb, + const u32 *opts, unsigned int entry) +{ + struct skb_shared_info *info = skb_shinfo(skb); + unsigned int cur_frag; + + for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) { + const skb_frag_t *frag = info->frags + cur_frag; + void *addr = skb_frag_address(frag); + u32 len = skb_frag_size(frag); + + entry = (entry + 1) % NUM_TX_DESC; + + if (unlikely(rtl8169_tx_map(tp, opts, len, addr, entry, true))) + goto err_out; + } + + return 0; + +err_out: + rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag); + return -EIO; +} + +static bool rtl8169_tso_csum(struct sk_buff *skb, u32 *opts) +{ + u32 mss = skb_shinfo(skb)->gso_size; + + if (mss) { + /* SoC GMAC LSO descriptor uses TD_LSO in opts[0] and MSS in opts[1] */ + opts[0] |= TD_LSO; + opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT; + } else if (skb->ip_summed == CHECKSUM_PARTIAL) { + const struct iphdr *ip; + + if (vlan_get_protocol(skb) != htons(ETH_P_IP)) + return !skb_checksum_help(skb); + + ip = ip_hdr(skb); + if (ip->protocol == IPPROTO_TCP) + opts[1] |= TD1_IP_CS | TD1_TCP_CS; + else if (ip->protocol == IPPROTO_UDP) + opts[1] |= TD1_IP_CS | TD1_UDP_CS; + else + return !skb_checksum_help(skb); + } + + return true; +} + +static unsigned int rtl_tx_slots_avail(struct rtl8169_private *tp) +{ + return READ_ONCE(tp->dirty_tx) + NUM_TX_DESC - READ_ONCE(tp->cur_tx); +} + +static void rtl8169_doorbell(struct rtl8169_private *tp) +{ + RTL_W8(tp, TX_POLL, NPQ); +} + +static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb, + struct net_device *dev) +{ + unsigned int frags = skb_shinfo(skb)->nr_frags; + struct rtl8169_private *tp = netdev_priv(dev); + unsigned int entry = tp->cur_tx % NUM_TX_DESC; + struct tx_desc *txd_first, *txd_last; + bool stop_queue, door_bell; + u16 close_idx; + u16 tail_idx; + u32 opts[2]; + + if (unlikely(rtl_tx_slots_avail(tp) < R8169_TX_STOP_THRS)) { + if (net_ratelimit()) + netdev_err(dev, "BUG! Tx Ring full when queue awake!\n"); + goto err_stop_0; + } + + txd_first = tp->tx_desc_array + entry; + + if (tp->chip->features & RTL_FEATURE_TX_NO_CLOSE) { + close_idx = RTL_R16(tp, TX_DESC_CLOSE_IDX) & TX_DESC_CNT_MASK; + tail_idx = READ_ONCE(tp->cur_tx) & TX_DESC_CNT_MASK; + if ((tail_idx > close_idx && + (tail_idx - close_idx == NUM_TX_DESC)) || + (tail_idx < close_idx && + (TX_DESC_CNT_SIZE - close_idx + tail_idx == NUM_TX_DESC))) + goto err_stop_0; + } else { + if (unlikely(le32_to_cpu(txd_first->opts1) & DESC_OWN)) + goto err_stop_0; + } + + opts[1] = rtl8169_tx_vlan_tag(skb); + opts[0] = 0; + + if (unlikely(!rtl8169_tso_csum(skb, opts))) + goto err_dma_0; + + if (unlikely(rtl8169_tx_map(tp, opts, skb_headlen(skb), skb->data, + entry, false))) + goto err_dma_0; + + if (frags) { + if (rtl8169_xmit_frags(tp, skb, opts, entry)) + goto err_dma_1; + entry = (entry + frags) % NUM_TX_DESC; + } + + txd_last = tp->tx_desc_array + entry; + txd_last->opts1 |= cpu_to_le32(LAST_FRAG); + tp->tx_skb[entry].skb = skb; + + skb_tx_timestamp(skb); + + /* Force memory writes to complete before releasing descriptor */ + dma_wmb(); + + door_bell = __netdev_sent_queue(dev, skb->len, netdev_xmit_more()); + + txd_first->opts1 |= cpu_to_le32(DESC_OWN | FIRST_FRAG); + + /* rtl_tx needs to see descriptor changes before updated tp->cur_tx */ + smp_wmb(); + + WRITE_ONCE(tp->cur_tx, tp->cur_tx + frags + 1); + + if (tp->chip->features & RTL_FEATURE_TX_NO_CLOSE) + RTL_W16(tp, TX_DESC_TAIL_IDX, + READ_ONCE(tp->cur_tx) & TX_DESC_CNT_MASK); + + stop_queue = !netif_subqueue_maybe_stop(dev, 0, rtl_tx_slots_avail(tp), + R8169_TX_STOP_THRS, + R8169_TX_START_THRS); + if (door_bell || stop_queue) + rtl8169_doorbell(tp); + + return NETDEV_TX_OK; + +err_dma_1: + rtl8169_unmap_tx_skb(tp, entry); +err_dma_0: + dev_kfree_skb_any(skb); + dev->stats.tx_dropped++; + return NETDEV_TX_OK; + +err_stop_0: + netif_stop_queue(dev); + return NETDEV_TX_BUSY; +} + +static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp, + int budget) +{ + unsigned int dirty_tx, tx_left, bytes_compl = 0, pkts_compl = 0; + struct sk_buff *skb = NULL; + u16 dirty_tx_idx; + u16 close_idx; + + dirty_tx = tp->dirty_tx; + if (tp->chip->features & RTL_FEATURE_TX_NO_CLOSE) { + close_idx = RTL_R16(tp, TX_DESC_CLOSE_IDX) & TX_DESC_CNT_MASK; + dirty_tx_idx = dirty_tx & TX_DESC_CNT_MASK; + smp_rmb(); /* make sure dirty_tx and close_idx are updated */ + + if (dirty_tx_idx <= close_idx) + tx_left = close_idx - dirty_tx_idx; + else + tx_left = close_idx + TX_DESC_CNT_SIZE - dirty_tx_idx; + } else { + smp_rmb(); /* make sure dirty_tx is updated */ + tx_left = READ_ONCE(tp->cur_tx) - dirty_tx; + } + + while (tx_left > 0) { + unsigned int entry = dirty_tx % NUM_TX_DESC; + u32 status; + + status = le32_to_cpu(READ_ONCE(tp->tx_desc_array[entry].opts1)); + if (!(tp->chip->features & RTL_FEATURE_TX_NO_CLOSE) && + (status & DESC_OWN)) + break; + + skb = tp->tx_skb[entry].skb; + rtl8169_unmap_tx_skb(tp, entry); + + if (skb) { + pkts_compl++; + bytes_compl += skb->len; + napi_consume_skb(skb, budget); + } + dirty_tx++; + tx_left--; + } + + if (tp->dirty_tx != dirty_tx) { + WRITE_ONCE(tp->dirty_tx, dirty_tx); + + netif_subqueue_completed_wake(dev, 0, pkts_compl, bytes_compl, + rtl_tx_slots_avail(tp), + R8169_TX_START_THRS); + + dev_sw_netstats_tx_add(dev, pkts_compl, bytes_compl); + + /* Kick an extra TX_POLL request when a burst of start_xmit + * activity is detected. + */ + if (READ_ONCE(tp->cur_tx) != dirty_tx && skb) + rtl8169_doorbell(tp); + } +} + +static int rtl8169_fragmented_frame(u32 status) +{ + return (status & (FIRST_FRAG | LAST_FRAG)) != (FIRST_FRAG | LAST_FRAG); +} + +static void rtl8169_rx_csum(struct net_device *dev, struct sk_buff *skb, + u32 opts1) +{ + u32 status = opts1 & (RX_PROTO_MASK | RX_CS_FAIL_MASK); + + if ((dev->features & NETIF_F_RXCSUM) && + (status == RX_PROTO_TCP || status == RX_PROTO_UDP)) + skb->ip_summed = CHECKSUM_UNNECESSARY; + else + skb_checksum_none_assert(skb); +} + +static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, + int budget) +{ + struct device *d = tp_to_dev(tp); + int count; + + for (count = 0; count < budget; count++, tp->cur_rx++) { + unsigned int pkt_size, entry = tp->cur_rx % NUM_RX_DESC; + struct rx_desc *desc = tp->rx_desc_array + entry; + struct sk_buff *skb; + const void *rx_buf; + dma_addr_t addr; + u32 status; + + status = le32_to_cpu(READ_ONCE(desc->opts1)); + if (status & DESC_OWN) + break; + + /* This barrier is needed to keep us from reading + * any other fields out of the Rx descriptor until + * we know the status of DescOwn + */ + dma_rmb(); + + if (unlikely(status & RX_RES)) { + if (net_ratelimit()) + netdev_warn(dev, "Rx ERROR. status = %08x\n", + status); + dev->stats.rx_errors++; + if (status & (RX_RWT | RX_RUNT)) + dev->stats.rx_length_errors++; + if (status & RX_CRC) + dev->stats.rx_crc_errors++; + if (!(dev->features & NETIF_F_RXALL)) + goto release_descriptor; + else if ((status & RX_RWT) || + !(status & (RX_RUNT | RX_CRC))) + goto release_descriptor; + } + + pkt_size = status & GENMASK(13, 0); + if (unlikely(pkt_size < ETH_HLEN + ETH_FCS_LEN)) { + dev->stats.rx_dropped++; + dev->stats.rx_length_errors++; + goto release_descriptor; + } + + if (likely(!(dev->features & NETIF_F_RXFCS))) + pkt_size -= ETH_FCS_LEN; + + if (unlikely(pkt_size > RX_BUF_SIZE)) { + dev->stats.rx_dropped++; + dev->stats.rx_length_errors++; + goto release_descriptor; + } + + /* The driver does not support incoming fragmented frames. + * They are seen as a symptom of over-mtu sized frames. + */ + if (unlikely(rtl8169_fragmented_frame(status))) { + dev->stats.rx_dropped++; + dev->stats.rx_length_errors++; + goto release_descriptor; + } + + skb = napi_alloc_skb(&tp->napi, pkt_size); + if (unlikely(!skb)) { + dev->stats.rx_dropped++; + goto release_descriptor; + } + + addr = le64_to_cpu(desc->addr); + rx_buf = page_address(tp->rx_databuff[entry]); + + dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE); + prefetch(rx_buf); + skb_copy_to_linear_data(skb, rx_buf, pkt_size); + skb_put(skb, pkt_size); + dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE); + + rtl8169_rx_csum(dev, skb, status); + skb->protocol = eth_type_trans(skb, dev); + + rtl8169_rx_vlan_tag(desc, skb); + + if (skb->pkt_type == PACKET_MULTICAST) + dev->stats.multicast++; + + napi_gro_receive(&tp->napi, skb); + + dev_sw_netstats_rx_add(dev, pkt_size); +release_descriptor: + rtl8169_mark_to_asic(desc); + } + + return count; +} + +static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance) +{ + struct rtl8169_private *tp = dev_instance; + u16 status; + + if (unlikely(!test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status))) + return IRQ_NONE; + + status = rtl_get_events(tp); + if (status == 0xffff || !(status & tp->irq_mask)) + return IRQ_NONE; + + if (status & LINK_CHG) + phy_mac_interrupt(tp->phydev); + + rtl_irq_disable(tp); + napi_schedule(&tp->napi); + + return IRQ_HANDLED; +} + +static void rtl_mac_reinit(struct rtl8169_private *tp); + +/* Workqueue context. */ +static void rtl_task(struct work_struct *work) +{ + static const struct { + void (*action)(struct rtl8169_private *tp); + int bitnr; + } rtl_work[] = { + { rtl_reset_work, RTL_FLAG_TASK_RESET_PENDING }, + { rtl_mac_reinit, RTL_FLAG_TASK_TX_TIMEOUT }, + }; + struct rtl8169_private *tp = + container_of(work, struct rtl8169_private, wk.work); + struct net_device *dev = tp->dev; + int i; + + rtl_lock_work(tp); + + if (!netif_running(dev) || + !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags)) + goto out_unlock; + + for (i = 0; i < ARRAY_SIZE(rtl_work); i++) { + bool pending; + + pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags); + if (pending) + rtl_work[i].action(tp); + } + +out_unlock: + rtl_unlock_work(tp); +} + +static int rtl8169_poll(struct napi_struct *napi, int budget) +{ + struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, + napi); + struct net_device *dev = tp->dev; + u16 status = rtl_get_events(tp); + int work_done = 0; + + rtl_ack_events(tp, status & ~RX_OVERFLOW); + + rtl_tx(dev, tp, budget); + + /* always check rx queue */ + work_done = rtl_rx(dev, tp, budget); + + if ((status & RX_OVERFLOW) && work_done > 0) + rtl_ack_events(tp, RX_OVERFLOW); + + if (work_done < budget && napi_complete_done(napi, work_done)) + rtl_irq_enable(tp); + + return work_done; +} + +static void r8169soc_adjust_link(struct net_device *ndev) +{ + struct rtl8169_private *tp = netdev_priv(ndev); + + if (netif_carrier_ok(ndev)) { + /* prevent ALDPS enter MAC Powercut Tx/Rx disable */ + /* use MAC reset to set counter to offset 0 */ + rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING); + } + + phy_print_status(tp->phydev); +} + +static int r8169_phy_connect(struct rtl8169_private *tp) +{ + struct phy_device *phydev = tp->phydev; + phy_interface_t phy_mode; + int ret; + + phy_mode = tp->chip->features & RTL_FEATURE_GMII ? + PHY_INTERFACE_MODE_GMII : PHY_INTERFACE_MODE_MII; + + ret = phy_connect_direct(tp->dev, phydev, r8169soc_adjust_link, + phy_mode); + if (ret) { + netdev_err(tp->dev, "failed to connect PHY: %d\n", ret); + return ret; + } + + if (!(tp->chip->features & RTL_FEATURE_GMII)) + phy_set_max_speed(phydev, SPEED_100); + + phy_attached_info(phydev); + + return 0; +} + +static void rtl8169_down(struct rtl8169_private *tp) +{ + if (test_and_set_bit(RTL_STATUS_DOWN, tp->status)) + return; + + phy_stop(tp->phydev); + + /* Clear all task flags after stopping PHY state machine */ + bitmap_zero(tp->wk.flags, RTL_FLAG_MAX); + + rtl8169_cleanup(tp); +} + +static void rtl8169_up(struct rtl8169_private *tp) +{ + phy_init_hw(tp->phydev); + phy_resume(tp->phydev); + if (test_and_clear_bit(RTL_STATUS_DOWN, tp->status)) + rtl8169_init_phy(tp); + napi_enable(&tp->napi); + set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags); + rtl_reset_work(tp); + + phy_start(tp->phydev); +} + +static int __rtl8169_close(struct net_device *dev) +{ + struct rtl8169_private *tp = netdev_priv(dev); + struct platform_device *pdev = tp->pdev; + + netif_stop_queue(dev); + + rtl8169_down(tp); + + rtl8169_rx_clear(tp); + + cancel_work(&tp->wk.work); + + free_irq(dev->irq, tp); + + phy_disconnect(tp->phydev); + + dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, + tp->rx_desc_array, tp->rx_phy_addr); + dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, + tp->tx_desc_array, tp->tx_phy_addr); + tp->tx_desc_array = NULL; + tp->rx_desc_array = NULL; + + return 0; +} + +static int rtl8169_close(struct net_device *dev) +{ + struct rtl8169_private *tp = netdev_priv(dev); + int ret; + + rtl_lock_work(tp); + ret = __rtl8169_close(dev); + rtl_unlock_work(tp); + return ret; +} + +#ifdef CONFIG_NET_POLL_CONTROLLER +static void rtl8169_netpoll(struct net_device *dev) +{ + struct rtl8169_private *tp = netdev_priv(dev); + + rtl8169_interrupt(dev->irq, tp); +} +#endif + +static void rtl_hw_initialize(struct rtl8169_private *tp) +{ + u32 data; + + rtl_enable_rxdvgate(tp); + + RTL_W8(tp, CHIP_CMD, + RTL_R8(tp, CHIP_CMD) & ~(CMD_TX_ENB | CMD_RX_ENB)); + fsleep(1000); + RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB); + + data = rtl_ocp_read(tp, OCP_SFF_STS_7); + data &= ~SFF_MCU_BORW_EN; + rtl_ocp_write(tp, OCP_SFF_STS_7, data); + + rtl_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42); + + data = rtl_ocp_read(tp, OCP_SFF_STS_7); + data |= SFF_RE_INIT_LL; + rtl_ocp_write(tp, OCP_SFF_STS_7, data); + + rtl_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42); +} + +static int __rtl_reinit_mac_phy(struct rtl8169_private *tp) +{ + struct device *d = tp_to_dev(tp); + int ret; + + ret = tp->chip->pll_clock_init(tp); + if (ret) { + dev_dbg(d, "failed to initialize PLL clock: %d\n", ret); + return ret; + } + + ret = tp->chip->mdio_init(tp); + if (ret) { + dev_dbg(d, "failed to initialize MDIO: %d\n", ret); + return ret; + } + + /* Enable ALDPS */ + ret = rtl_phy_modify(tp, PHY_PAGE_A43, PHY_PHYCR1_REG, 0, + PHYCR1_ALDPS_EN); + if (ret) { + dev_dbg(d, "failed to enable ALDPS: %d\n", ret); + return ret; + } + + rtl_init_rxcfg(tp); + + rtl8169_irq_mask_and_ack(tp); + + rtl_hw_initialize(tp); + + rtl_hw_reset(tp); + + rtl_unlock_config_regs(tp); + RTL_W8(tp, CONFIG1, RTL_R8(tp, CONFIG1) | PM_ENABLE); + /* Clear CONFIG5[7:1] wakeup bits and keep bit 0 */ + RTL_W8(tp, CONFIG5, RTL_R8(tp, CONFIG5) & ASPM_EN); + + /* disable magic packet WOL */ + RTL_W8(tp, CONFIG3, RTL_R8(tp, CONFIG3) & ~MAGIC_PKT); + rtl_lock_config_regs(tp); + + if (test_bit(RTL_STATUS_REINIT, tp->status)) + __rtl_rar_set(tp, tp->mac_addr); + + rtl_led_set(tp); + + return 0; +} + +static int rtl_reinit_mac_phy(struct rtl8169_private *tp) +{ + int ret; + + phy_lock_mdio_bus(tp->phydev); + set_bit(RTL_STATUS_REINIT, tp->status); + + ret = tp->chip->reset_phy_gmac(tp); + if (ret) + dev_dbg(tp_to_dev(tp), "failed to reset PHY/GMAC: %d\n", ret); + else + ret = __rtl_reinit_mac_phy(tp); + + clear_bit(RTL_STATUS_REINIT, tp->status); + phy_unlock_mdio_bus(tp->phydev); + return ret; +} + +static int rtl_open(struct net_device *dev) +{ + struct rtl8169_private *tp = netdev_priv(dev); + struct platform_device *pdev = tp->pdev; + int retval = -ENOMEM; + + /* Rx and Tx descriptors needs 256 bytes alignment. + * dma_alloc_coherent provides more. + */ + tp->tx_desc_array = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES, + &tp->tx_phy_addr, GFP_KERNEL); + if (!tp->tx_desc_array) + goto out; + + tp->rx_desc_array = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES, + &tp->rx_phy_addr, GFP_KERNEL); + if (!tp->rx_desc_array) + goto err_free_tx_0; + + retval = rtl8169_init_ring(tp); + if (retval < 0) { + dev_dbg(&pdev->dev, "failed to initialize RX ring: %d\n", + retval); + goto err_free_rx_1; + } + + rtl_lock_work(tp); + + if (!test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status)) { + retval = rtl_reinit_mac_phy(tp); + if (retval) + goto err_free_rx_2; + } + + smp_mb(); /* make sure TX/RX rings are ready */ + + retval = request_irq(dev->irq, rtl8169_interrupt, 0, dev->name, tp); + if (retval < 0) { + netdev_err(dev, "failed to request IRQ %d: %d\n", + dev->irq, retval); + goto err_free_rx_2; + } + + retval = r8169_phy_connect(tp); + if (retval) + goto err_free_irq; + + rtl8169_up(tp); + + rtl_unlock_work(tp); + + netif_start_queue(dev); + +out: + return retval; + +err_free_irq: + free_irq(dev->irq, tp); +err_free_rx_2: + rtl_unlock_work(tp); + rtl8169_rx_clear(tp); +err_free_rx_1: + dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, + tp->rx_desc_array, tp->rx_phy_addr); + tp->rx_desc_array = NULL; +err_free_tx_0: + dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, + tp->tx_desc_array, tp->tx_phy_addr); + tp->tx_desc_array = NULL; + goto out; +} + +static void rtl8169_get_stats64(struct net_device *dev, + struct rtnl_link_stats64 *stats) +{ + netdev_stats_to_stats64(stats, &dev->stats); + dev_fetch_sw_netstats(stats, dev->tstats); +} + +static void rtl_mac_reinit(struct rtl8169_private *tp) +{ + int ret; + + netif_stop_queue(tp->dev); + rtl8169_down(tp); + + ret = rtl_reinit_mac_phy(tp); + if (ret) { + netdev_err(tp->dev, "hardware re-initialization failed: %d\n", + ret); + netif_carrier_off(tp->dev); + return; + } + + rtl8169_up(tp); + netif_wake_queue(tp->dev); +} + +static void rtl_shutdown(struct platform_device *pdev) +{ + struct rtl8169_private *tp = platform_get_drvdata(pdev); + + rtl_lock_work(tp); + netif_device_detach(tp->dev); + if (netif_running(tp->dev)) + rtl8169_down(tp); + + /* Restore permanent MAC address */ + if (test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status) && + is_valid_ether_addr(tp->dev->perm_addr)) + __rtl_rar_set(tp, tp->dev->perm_addr); + rtl_unlock_work(tp); +} + +static void rtl_hw_uninit(void *data) +{ + struct rtl8169_private *tp = data; + + if (test_and_clear_bit(RTL_STATUS_CLK_ETN_250M_EN, tp->status)) + clk_disable_unprepare(tp->clk_etn_250m); + if (test_and_clear_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status)) + clk_disable_unprepare(tp->clk_etn_sys); + reset_control_assert(tp->rstc_gmac); + reset_control_assert(tp->rstc_gphy); + if (test_and_clear_bit(RTL_STATUS_CLK_MISC_EN, tp->status)) + clk_disable_unprepare(tp->clk_misc); + reset_control_assert(tp->rstc_misc); +} + +static void rtl_remove_one(struct platform_device *pdev) +{ + struct rtl8169_private *tp = platform_get_drvdata(pdev); + + unregister_netdev(tp->dev); + + cancel_work_sync(&tp->wk.work); + + netif_napi_del(&tp->napi); + + /* restore permanent MAC address */ + if (test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status) && + is_valid_ether_addr(tp->dev->perm_addr)) + __rtl_rar_set(tp, tp->dev->perm_addr); +} + +static void rtl8169soc_get_drvinfo(struct net_device *dev, + struct ethtool_drvinfo *info) +{ + strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); +} + +static const struct ethtool_ops rtl8169soc_ethtool_ops = { + .get_drvinfo = rtl8169soc_get_drvinfo, + .nway_reset = phy_ethtool_nway_reset, + .get_link = ethtool_op_get_link, + .get_ts_info = ethtool_op_get_ts_info, + .get_link_ksettings = phy_ethtool_get_link_ksettings, + .set_link_ksettings = phy_ethtool_set_link_ksettings, +}; + +static const struct net_device_ops rtl_netdev_ops = { + .ndo_open = rtl_open, + .ndo_stop = rtl8169_close, + .ndo_get_stats64 = rtl8169_get_stats64, + .ndo_start_xmit = rtl8169_start_xmit, + .ndo_tx_timeout = rtl8169_tx_timeout, + .ndo_validate_addr = eth_validate_addr, + .ndo_change_mtu = rtl8169_change_mtu, + .ndo_fix_features = rtl8169_fix_features, + .ndo_set_features = rtl8169_set_features, + .ndo_set_mac_address = rtl_set_mac_address, + .ndo_eth_ioctl = phy_do_ioctl_running, + .ndo_set_rx_mode = rtl_set_rx_mode, +#ifdef CONFIG_NET_POLL_CONTROLLER + .ndo_poll_controller = rtl8169_netpoll, +#endif +}; + +static void rtl_set_irq_mask(struct rtl8169_private *tp) +{ + tp->irq_mask = RX_OK | RX_ERR | RX_OVERFLOW | TX_OK | TX_ERR | + LINK_CHG; +} + +static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg) +{ + struct rtl8169_private *tp = mii_bus->priv; + + if (phyaddr > 0) + return -ENODEV; + + return rtl_phy_read(tp, CURRENT_MDIO_PAGE, phyreg); +} + +static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr, + int phyreg, u16 val) +{ + struct rtl8169_private *tp = mii_bus->priv; + + if (phyaddr > 0) + return -ENODEV; + + return rtl_phy_write(tp, CURRENT_MDIO_PAGE, phyreg, val); +} + +static int r8169_mdio_register(struct rtl8169_private *tp) +{ + struct platform_device *pdev = tp->pdev; + struct mii_bus *new_bus; + int ret; + + new_bus = devm_mdiobus_alloc(&pdev->dev); + if (!new_bus) + return -ENOMEM; + + new_bus->name = KBUILD_MODNAME; + new_bus->priv = tp; + new_bus->parent = &pdev->dev; + new_bus->irq[0] = PHY_MAC_INTERRUPT; + new_bus->phy_mask = GENMASK(31, 1); + snprintf(new_bus->id, MII_BUS_ID_SIZE, "%s-0", KBUILD_MODNAME); + + new_bus->read = r8169_mdio_read_reg; + new_bus->write = r8169_mdio_write_reg; + + ret = devm_mdiobus_register(&pdev->dev, new_bus); + if (ret) + return dev_err_probe(&pdev->dev, ret, + "failed to register MDIO bus\n"); + + tp->phydev = mdiobus_get_phy(new_bus, 0); + if (!tp->phydev) + return dev_err_probe(&pdev->dev, -ENODEV, "no phydev found\n"); + + if (!tp->phydev->drv) + return dev_err_probe(&pdev->dev, -EUNATCH, + "no dedicated PHY driver found for PHY ID 0x%08x\n", + tp->phydev->phy_id); + + /* PHY will be woken up in rtl_open() */ + phy_suspend(tp->phydev); + set_bit(RTL_STATUS_DOWN, tp->status); + + return 0; +} + +static int dummy_reset_phy_gmac(struct rtl8169_private *tp) +{ + return 0; +} + +static int dummy_pll_clock_init(struct rtl8169_private *tp) +{ + return 0; +} + +static int dummy_mdio_init(struct rtl8169_private *tp) +{ + return 0; +} + +static void dummy_hw_phy_config(struct rtl8169_private *tp) +{ +} + +/* RTD1625 */ +static void rtd1625_mdio_lock(struct rtl8169_private *tp) +{ + u32 wait_cnt = 0; + u32 log_de4e; + + /* disable EEE IMR */ + rtl_ocp_write(tp, OCP_EEE_IMR, + rtl_ocp_read(tp, OCP_EEE_IMR) & ~EEE_IMR_MASK); + /* disable timer 2 */ + rtl_ocp_write(tp, OCP_TIMER2_CTRL, + rtl_ocp_read(tp, OCP_TIMER2_CTRL) | TIMER2_DISABLE); + /* wait MDIO channel is free */ + log_de4e = rtl_ocp_read(tp, OCP_MDIO_NON_IDLE) & MDIO_NON_IDLE_FLAG; + log_de4e = (log_de4e << 1) | + (rtl_ocp_read(tp, OCP_MDIO_NON_IDLE) & MDIO_NON_IDLE_FLAG); + /* check if 0 for continuous 2 times */ + while (log_de4e & GENMASK(1, 0)) { + wait_cnt++; + udelay(1); + log_de4e = (log_de4e << 1) | + (rtl_ocp_read(tp, OCP_MDIO_NON_IDLE) & + MDIO_NON_IDLE_FLAG); + if (wait_cnt > MDIO_WAIT_TIMEOUT) + break; + } + /* enter driver mode */ + rtl_ocp_write(tp, OCP_FPGA_GPHY1, + rtl_ocp_read(tp, OCP_FPGA_GPHY1) | GPHY1_DRV_MODE); + if (wait_cnt > MDIO_WAIT_TIMEOUT) + dev_err(tp_to_dev(tp), "MDIO lock timeout\n"); +} + +static void rtd1625_mdio_unlock(struct rtl8169_private *tp) +{ + /* exit driver mode */ + rtl_ocp_write(tp, OCP_FPGA_GPHY1, + rtl_ocp_read(tp, OCP_FPGA_GPHY1) & ~GPHY1_DRV_MODE); + /* enable timer 2 */ + rtl_ocp_write(tp, OCP_TIMER2_CTRL, + rtl_ocp_read(tp, OCP_TIMER2_CTRL) & ~TIMER2_DISABLE); + /* enable EEE IMR */ + rtl_ocp_write(tp, OCP_EEE_IMR, + rtl_ocp_read(tp, OCP_EEE_IMR) | EEE_IMR_MASK); +} + +static int rtd1625_reset_phy_gmac(struct rtl8169_private *tp) +{ + struct device *d = tp_to_dev(tp); + bool clk_en; + int ret; + + if (!test_bit(RTL_STATUS_REINIT, tp->status)) { + if (!test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status)) { + ret = clk_prepare_enable(tp->clk_etn_sys); + if (ret) { + dev_dbg(d, "failed to enable etn_sys clk: %d\n", + ret); + return ret; + } + set_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status); + } + + if (!test_bit(RTL_STATUS_CLK_ETN_250M_EN, tp->status)) { + ret = clk_prepare_enable(tp->clk_etn_250m); + if (ret) { + dev_dbg(d, "failed to enable etn_250m clk: %d\n", + ret); + return ret; + } + set_bit(RTL_STATUS_CLK_ETN_250M_EN, tp->status); + } + + ret = reset_control_deassert(tp->rstc_misc); + if (ret) { + dev_dbg(d, "failed to deassert misc reset: %d\n", ret); + return ret; + } + + if (!test_bit(RTL_STATUS_CLK_MISC_EN, tp->status)) { + ret = clk_prepare_enable(tp->clk_misc); + if (ret) { + dev_dbg(d, "failed to enable misc clk: %d\n", + ret); + return ret; + } + set_bit(RTL_STATUS_CLK_MISC_EN, tp->status); + } + } else { + ret = reset_control_deassert(tp->rstc_misc); + if (ret) { + dev_dbg(d, "failed to deassert misc reset: %d\n", ret); + return ret; + } + } + + ret = reset_control_deassert(tp->rstc_gphy); + if (ret) { + dev_dbg(d, "failed to deassert gphy reset: %d\n", ret); + return ret; + } + + /* Disable DBUS clock gating */ + ret = regmap_update_bits_base(tp->m2tmx_base, + RTD1625_M2TMX_ETN_DBUS_CTRL, + BIT(0), 0, NULL, false, true); + if (ret) { + dev_dbg(d, "failed to disable DBUS clock gating: %d\n", ret); + return ret; + } + + /* Serialize against rtl_set_rx_mode() and ISR before disabling MAC clock */ + netif_addr_lock_bh(tp->dev); + clk_en = test_and_clear_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status); + netif_addr_unlock_bh(tp->dev); + if (clk_en) { + synchronize_irq(tp->dev->irq); + clk_disable_unprepare(tp->clk_etn_sys); + } + if (test_and_clear_bit(RTL_STATUS_CLK_ETN_250M_EN, tp->status)) + clk_disable_unprepare(tp->clk_etn_250m); + + ret = reset_control_assert(tp->rstc_gphy); + if (ret) { + dev_dbg(d, "failed to assert gphy reset: %d\n", ret); + return ret; + } + + ret = reset_control_assert(tp->rstc_gmac); + if (ret) { + dev_dbg(d, "failed to assert gmac reset: %d\n", ret); + return ret; + } + + /* Enable bypass mode */ + ret = regmap_update_bits_base(tp->iso_base, ISO_ETN_TESTIO, + BIT(1), BIT(1), NULL, false, true); + if (ret) { + dev_dbg(d, "failed to enable bypass mode: %d\n", ret); + return ret; + } + + /* Set embedded PHY address to 0 */ + ret = regmap_write(tp->m2tmx_base, RTD1625_M2TMX_PWRCUT_ETN, + RTD1625_PWRCUT_ETN_DEFAULT); + if (ret) { + dev_dbg(d, "failed to set PWRCUT_ETN: %d\n", ret); + return ret; + } + + fsleep(1000); + return 0; +} + +static int rtd1625_pll_clock_init(struct rtl8169_private *tp) +{ + struct device *d = tp_to_dev(tp); + u32 val = 0; + u32 tmp; + int ret; + + /* Clear ETN PHY interrupt for bypass mode */ + ret = regmap_write(tp->iso_base, ISO_UMSK_ISR, BIT(27)); + if (ret) { + dev_dbg(d, "failed to clear ETN PHY interrupt: %d\n", ret); + return ret; + } + + ret = reset_control_deassert(tp->rstc_gphy); + if (ret) { + dev_dbg(d, "failed to deassert gphy reset: %d\n", ret); + return ret; + } + + fsleep(1000); /* wait 1ms for PHY PLL stable */ + + /* Enable bypass mode */ + ret = regmap_update_bits_base(tp->iso_base, ISO_ETN_TESTIO, + BIT(1), BIT(1), NULL, false, true); + if (ret) { + dev_dbg(d, "failed to enable bypass mode: %d\n", ret); + return ret; + } + + ret = reset_control_deassert(tp->rstc_gmac); + if (ret) { + dev_dbg(d, "failed to deassert gmac reset: %d\n", ret); + return ret; + } + + if (!test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status)) { + ret = clk_prepare_enable(tp->clk_etn_sys); + if (ret) { + dev_dbg(d, "failed to enable etn_sys clk: %d\n", ret); + return ret; + } + set_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status); + } + + if (!test_bit(RTL_STATUS_CLK_ETN_250M_EN, tp->status)) { + ret = clk_prepare_enable(tp->clk_etn_250m); + if (ret) { + dev_dbg(d, "failed to enable etn_250m clk: %d\n", ret); + return ret; + } + set_bit(RTL_STATUS_CLK_ETN_250M_EN, tp->status); + } + + /* Bit 0 of ISO_PLL_WDOUT indicates init_autoload_done (ETN RBUS valid) */ + ret = regmap_read(tp->iso_base, ISO_PLL_WDOUT, &val); + if (ret) { + dev_dbg(d, "failed to read ISO_PLL_WDOUT: %d\n", ret); + return ret; + } + + tmp = 0; + while ((val & BIT(0)) != 0x1) { + tmp += 1; + fsleep(1000); + if (tmp >= MAC_INIT_TIMEOUT) { + dev_err(d, "GMAC init timeout\n"); + return -ETIMEDOUT; + } + ret = regmap_read(tp->iso_base, ISO_PLL_WDOUT, &val); + if (ret) { + dev_dbg(d, "failed to read ISO_PLL_WDOUT: %d\n", ret); + return ret; + } + } + dev_dbg(d, "GMAC wait %d ms for init_autoload_done\n", tmp); + return 0; +} + +static int rtd1625_mdio_init(struct rtl8169_private *tp) +{ + struct device *d = tp_to_dev(tp); + u32 reg; + int ret; + int val; + u32 i; + + fsleep(10000); /* wait for MDIO ready */ + + /* PHY will stay in state 1 mode */ + for (i = 0; i < MDIO_INIT_TIMEOUT; i++) { + val = int_mdio_read(tp, PHY_PAGE_A42, PHY_STATUS_A42_REG); + if (val < 0) { + dev_dbg(d, "failed to read PHY bypass status: %d\n", + val); + return val; + } + if ((val & PHY_STATUS_A42_MASK) == PHY_STATUS_BYPASS) + break; + fsleep(1000); + } + if (i >= MDIO_INIT_TIMEOUT) { + dev_err(d, "PHY status is not 0x1 in bypass mode, current = 0x%02x\n", + val & PHY_STATUS_A42_MASK); + return -ETIMEDOUT; + } + + /* ETN spec. set MDC freq = 8.9MHZ */ + reg = rtl_ocp_read(tp, OCP_GPHY_MDCMDIO_CFG) & ~MDCMDIO_PERIOD_MASK; + reg |= MDCMDIO_PERIOD_8_9MHZ; + rtl_ocp_write(tp, OCP_GPHY_MDCMDIO_CFG, reg); + reg = rtl_ocp_read(tp, OCP_EPHY_MDCMDIO_CFG) & ~MDCMDIO_PERIOD_MASK; + reg |= MDCMDIO_PERIOD_8_9MHZ; + rtl_ocp_write(tp, OCP_EPHY_MDCMDIO_CFG, reg); + + /* fill fuse_rdy & rg_ext_ini_done */ + val = int_mdio_read(tp, PHY_PAGE_A46, PHY_PHYSCR_REG); + if (val < 0) { + dev_dbg(d, "failed to read PHY_PHYSCR_REG: %d\n", val); + return val; + } + ret = int_mdio_write(tp, PHY_PAGE_A46, PHY_PHYSCR_REG, + val | PHYSCR_CFG_DONE); + if (ret) { + dev_dbg(d, "failed to write PHY_PHYSCR_REG: %d\n", ret); + return ret; + } + + /* init_autoload_done = 1 */ + reg = rtl_ocp_read(tp, OCP_BOOT_CTRL_REG); + reg |= INIT_AUTOLOAD_DONE; + rtl_ocp_write(tp, OCP_BOOT_CTRL_REG, reg); + + /* Set ee_mode = 3 to unlock config registers */ + rtl_unlock_config_regs(tp); + + /* wait LAN-ON */ + for (i = 0; i < MDIO_INIT_TIMEOUT; i++) { + fsleep(1000); + val = int_mdio_read(tp, PHY_PAGE_A42, PHY_STATUS_A42_REG); + if (val < 0) { + rtl_lock_config_regs(tp); + dev_dbg(d, "failed to read PHY LAN-ON status: %d\n", + val); + return val; + } + if ((val & PHY_STATUS_A42_MASK) == PHY_STATUS_LAN_ON) + break; + } + if (i >= MDIO_INIT_TIMEOUT) { + dev_err(d, "PHY status is not 0x3, current = 0x%02x\n", + val & PHY_STATUS_A42_MASK); + rtl_lock_config_regs(tp); + return -ETIMEDOUT; + } + rtl_lock_config_regs(tp); + dev_dbg(d, "wait %d ms for PHY ready, current = 0x%x\n", i + 1, val); + + /* ISO spec, set data path to FEPHY */ + reg = rtl_ocp_read(tp, OCP_RMII_REG); + reg &= ~RMII_ENABLE; + rtl_ocp_write(tp, OCP_RMII_REG, reg); + + reg = rtl_ocp_read(tp, OCP_RGMII_REG); + reg &= ~(MII_ENABLE | RGMII_ENABLE); + reg |= MII_ENABLE; /* FEPHY */ + rtl_ocp_write(tp, OCP_RGMII_REG, reg); + + return 0; +} + +static const struct r8169soc_chip_info rtd1625_info = { + .name = "RTD1625", + .mdio_lock = rtd1625_mdio_lock, + .mdio_unlock = rtd1625_mdio_unlock, + .reset_phy_gmac = rtd1625_reset_phy_gmac, + .pll_clock_init = rtd1625_pll_clock_init, + .mdio_init = rtd1625_mdio_init, + .jumbo_max = JUMBO_1K, + .jumbo_tx_csum = false, + .led_cfg = 0x170000a9, + .features = RTL_FEATURE_TX_NO_CLOSE | + RTL_FEATURE_ADJUST_FIFO | + RTL_FEATURE_OCP_MDIO, +}; + +static const struct of_device_id r8169soc_dt_ids[] = { + { + .compatible = "realtek,rtd1625-r8169soc", + .data = &rtd1625_info, + }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, r8169soc_dt_ids); + +static void rtk_chip_info_check(struct r8169soc_chip_info *chip) +{ + if (!chip->mdio_lock) + chip->mdio_lock = dummy_mdio_lock; + if (!chip->mdio_unlock) + chip->mdio_unlock = dummy_mdio_unlock; + if (!chip->reset_phy_gmac) + chip->reset_phy_gmac = dummy_reset_phy_gmac; + if (!chip->pll_clock_init) + chip->pll_clock_init = dummy_pll_clock_init; + if (!chip->mdio_init) + chip->mdio_init = dummy_mdio_init; + if (!chip->hw_phy_config) + chip->hw_phy_config = dummy_hw_phy_config; +} + +static void rtl_init_mac_address(struct rtl8169_private *tp) +{ + /* Default placeholder MAC in DTB when factory MAC is not programmed + * by the bootloader; treat it as unconfigured and use a random MAC. + */ + static const u8 default_mac[ETH_ALEN] = { + 0x00, 0x10, 0x20, 0x30, 0x40, 0x50 + }; + struct net_device *dev = tp->dev; + u8 mac_addr[ETH_ALEN] = {}; + int rc; + + rc = eth_platform_get_mac_address(tp_to_dev(tp), mac_addr); + if (!rc && is_valid_ether_addr(mac_addr) && + !ether_addr_equal(mac_addr, default_mac)) { + ether_addr_copy(dev->perm_addr, mac_addr); + goto done; + } + + eth_random_addr(mac_addr); + dev->addr_assign_type = NET_ADDR_RANDOM; + dev_warn(tp_to_dev(tp), "using random MAC address %pM\n", mac_addr); +done: + eth_hw_addr_set(dev, mac_addr); + ether_addr_copy(tp->mac_addr, mac_addr); + __rtl_rar_set(tp, mac_addr); +} + +static int rtl_init_clks_and_resets(struct rtl8169_private *tp) +{ + struct device *dev = &tp->pdev->dev; + + tp->clk_etn_sys = devm_clk_get(dev, "etn_sys"); + if (IS_ERR(tp->clk_etn_sys)) + return dev_err_probe(dev, PTR_ERR(tp->clk_etn_sys), + "failed to get etn_sys clk\n"); + + tp->clk_etn_250m = devm_clk_get(dev, "etn_250m"); + if (IS_ERR(tp->clk_etn_250m)) + return dev_err_probe(dev, PTR_ERR(tp->clk_etn_250m), + "failed to get etn_250m clk\n"); + + tp->clk_misc = devm_clk_get(dev, "misc"); + if (IS_ERR(tp->clk_misc)) + return dev_err_probe(dev, PTR_ERR(tp->clk_misc), + "failed to get misc clk\n"); + + tp->rstc_gphy = devm_reset_control_get_exclusive(dev, "gphy"); + if (IS_ERR(tp->rstc_gphy)) + return dev_err_probe(dev, PTR_ERR(tp->rstc_gphy), + "failed to get gphy reset\n"); + + tp->rstc_gmac = devm_reset_control_get_exclusive(dev, "gmac"); + if (IS_ERR(tp->rstc_gmac)) + return dev_err_probe(dev, PTR_ERR(tp->rstc_gmac), + "failed to get gmac reset\n"); + + tp->rstc_misc = devm_reset_control_get_exclusive(dev, "misc"); + if (IS_ERR(tp->rstc_misc)) + return dev_err_probe(dev, PTR_ERR(tp->rstc_misc), + "failed to get misc reset\n"); + + return 0; +} + +static int rtl_init_one(struct platform_device *pdev) +{ + const struct r8169soc_chip_info *chip; + struct rtl8169_private *tp; + struct net_device *ndev; + void __iomem *ioaddr; + int irq; + int rc; + + chip = of_device_get_match_data(&pdev->dev); + if (!chip) + return dev_err_probe(&pdev->dev, -EINVAL, + "no proper chip matched\n"); + + irq = platform_get_irq(pdev, 0); + if (irq < 0) + return irq; + + rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); + if (rc) + return dev_err_probe(&pdev->dev, rc, "failed to set DMA mask\n"); + + ioaddr = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(ioaddr)) + return PTR_ERR(ioaddr); + + ndev = devm_alloc_etherdev(&pdev->dev, sizeof(*tp)); + if (!ndev) + return -ENOMEM; + + SET_NETDEV_DEV(ndev, &pdev->dev); + ndev->irq = irq; + ndev->netdev_ops = &rtl_netdev_ops; + ndev->ethtool_ops = &rtl8169soc_ethtool_ops; + tp = netdev_priv(ndev); + tp->dev = ndev; + tp->pdev = pdev; + tp->chip_info = *chip; + tp->chip = &tp->chip_info; + rtk_chip_info_check(tp->chip); + tp->mmio_addr = ioaddr; + + ndev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS; + + tp->iso_base = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, + "realtek,iso"); + if (IS_ERR(tp->iso_base)) + return dev_err_probe(&pdev->dev, PTR_ERR(tp->iso_base), + "Fail to get iso_base address\n"); + + tp->m2tmx_base = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, + "realtek,m2tmx"); + if (IS_ERR(tp->m2tmx_base)) + return dev_err_probe(&pdev->dev, PTR_ERR(tp->m2tmx_base), + "Fail to get m2tmx_base address\n"); + + rc = rtl_init_clks_and_resets(tp); + if (rc) + return rc; + + rc = devm_add_action_or_reset(&pdev->dev, rtl_hw_uninit, tp); + if (rc) + return rc; + + raw_spin_lock_init(&tp->mac_ocp_lock); + + rtl_init_mdio_ops(tp); + rc = tp->chip->reset_phy_gmac(tp); + if (rc) + return dev_err_probe(&pdev->dev, rc, + "failed to reset PHY/GMAC\n"); + + rc = __rtl_reinit_mac_phy(tp); + if (rc) + return dev_err_probe(&pdev->dev, rc, + "failed to initialize MAC/PHY\n"); + + mutex_init(&tp->wk.mutex); + + INIT_WORK(&tp->wk.work, rtl_task); + + rtl_init_mac_address(tp); + + rtl_set_irq_mask(tp); + + rc = r8169_mdio_register(tp); + if (rc) + return rc; + + ndev->watchdog_timeo = RTL8169_TX_TIMEOUT; + + netif_napi_add(ndev, &tp->napi, rtl8169_poll); + + ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO | + NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX | + NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_GRO_UDP_FWD; + + ndev->features |= ndev->hw_features; + + ndev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO; + ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE; + + netif_set_tso_max_size(ndev, RTL_GSO_MAX_SIZE_V1); + netif_set_tso_max_segs(ndev, RTL_GSO_MAX_SEGS_V1); + + ndev->hw_features |= NETIF_F_RXALL; + ndev->hw_features |= NETIF_F_RXFCS; + + netdev_sw_irq_coalesce_default_on(ndev); + + /* configure chip for default features */ + __rtl8169_set_features(tp, ndev->features); + + ndev->min_mtu = ETH_ZLEN; + ndev->max_mtu = tp->chip->jumbo_max; + + platform_set_drvdata(pdev, tp); + + netif_carrier_off(ndev); + + rc = register_netdev(ndev); + if (rc < 0) { + netif_napi_del(&tp->napi); + return dev_err_probe(&pdev->dev, rc, + "failed to register netdev\n"); + } + + netdev_info(ndev, "%s, %pM, IRQ %d\n", + tp->chip->name, ndev->dev_addr, ndev->irq); + + return 0; +} + +static struct platform_driver rtl8169_soc_driver = { + .probe = rtl_init_one, + .remove = rtl_remove_one, + .shutdown = rtl_shutdown, + .driver = { + .name = KBUILD_MODNAME, + .of_match_table = r8169soc_dt_ids, + }, +}; +module_platform_driver(rtl8169_soc_driver); diff --git a/drivers/net/ethernet/realtek/r8169soc/rtl8169soc.h b/drivers/net/ethernet/realtek/r8169soc/rtl8169soc.h new file mode 100644 index 000000000000..ba099b13f7e8 --- /dev/null +++ b/drivers/net/ethernet/realtek/r8169soc/rtl8169soc.h @@ -0,0 +1,513 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* rtl8169soc.h: the embedded Realtek STB SoC 8169soc Ethernet driver. + * + * Copyright (c) 2002 Realtek Semiconductor Corp. + * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw> + * Copyright (c) 2003-2007 Francois Romieu <romieu@fr.zoreil.com> + * Copyright (c) 2014 YuKuen Wu <yukuen@realtek.com> + * Copyright (c) 2015-2026 Eric Wang <ericwang@realtek.com> + */ + +#ifndef __RTL8169SOC_H__ +#define __RTL8169SOC_H__ + +#include <linux/bitops.h> +#include <linux/bits.h> +#include <linux/clk.h> +#include <linux/if_ether.h> +#include <linux/io.h> +#include <linux/mutex.h> +#include <linux/netdevice.h> +#include <linux/phy.h> +#include <linux/platform_device.h> +#include <linux/regmap.h> +#include <linux/reset.h> +#include <linux/spinlock.h> +#include <linux/types.h> +#include <linux/workqueue.h> + +#define CURRENT_MDIO_PAGE 0xffffffff + +/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast). + * The RTL chips use a 64 element hash table based on the Ethernet CRC. + */ +#define MC_FILTER_LIMIT 32 + +#define TX_DMA_BURST 4 /* Maximum DMA burst, '7' is unlimited */ +#define INTER_FRAME_GAP 0x03 /* 3 means INTER_FRAME_GAP = the shortest one */ + +#define NUM_TX_DESC 1024U /* Number of Tx descriptor registers */ +#define NUM_RX_DESC 1024U /* Number of Rx descriptor registers */ +#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct tx_desc)) +#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct rx_desc)) +#define R8169_TX_STOP_THRS (MAX_SKB_FRAGS + 1) +#define R8169_TX_START_THRS (2 * R8169_TX_STOP_THRS) + +#define RTL_GSO_MAX_SIZE_V1 32000 +#define RTL_GSO_MAX_SEGS_V1 24 + +#define RTL8169_TX_TIMEOUT (6 * HZ) +#define MAC_INIT_TIMEOUT 20 +#define MDIO_INIT_TIMEOUT 2000 +#define MDIO_WAIT_TIMEOUT 100 + +#define RX_BUF_SIZE 0x05f3 /* 0x05f3 = 1522 bytes + 1 */ + +/* write/read MMIO register */ +#define RTL_W8(tp, reg, val8) writeb((val8), (tp)->mmio_addr + (reg)) +#define RTL_W16(tp, reg, val16) writew((val16), (tp)->mmio_addr + (reg)) +#define RTL_W32(tp, reg, val32) writel((val32), (tp)->mmio_addr + (reg)) +#define RTL_R8(tp, reg) readb((tp)->mmio_addr + (reg)) +#define RTL_R16(tp, reg) readw((tp)->mmio_addr + (reg)) +#define RTL_R32(tp, reg) readl((tp)->mmio_addr + (reg)) + +#define JUMBO_1K ETH_DATA_LEN + +enum rtl_registers { + MAC0 = 0, /* Ethernet hardware address. */ + MAC4 = 4, + MAR0 = 8, /* Multicast filter. */ + LEDSEL = 0x18, + TX_DESC_START_ADDR_LOW = 0x20, + TX_DESC_START_ADDR_HIGH = 0x24, + CHIP_CMD = 0x37, + TX_POLL = 0x38, + INTR_MASK = 0x3c, + INTR_STATUS = 0x3e, + + TX_CONFIG = 0x40, +#define TXCFG_AUTO_FIFO BIT(7) +#define TXCFG_EMPTY BIT(11) + + RX_CONFIG = 0x44, +#define RX_EARLY_OFF BIT(11) +#define RXCFG_DMA_SHIFT 8 +#define RX_DMA_BURST (3 << RXCFG_DMA_SHIFT) /* 128 bytes */ + + CFG9346 = 0x50, + CONFIG1 = 0x52, + CONFIG2 = 0x53, + CONFIG3 = 0x54, + CONFIG5 = 0x56, + MULTI_INTR = 0x5c, + PHYAR = 0x60, +#define PHYAR_FLAG 0x80000000 +#define PHYAR_WRITE_CMD 0x80000000 +#define PHYAR_READ_CMD 0x00000000 +#define PHYAR_REG_MASK 0x1f +#define PHYAR_REG_SHIFT 16 +#define PHY_DATA_MASK 0xffff + ERIDR = 0x70, + ERIAR = 0x74, +#define ERIAR_FLAG 0x80000000 +#define ERIAR_WRITE_CMD 0x80000000 +#define ERIAR_READ_CMD 0x00000000 +#define ERIAR_TYPE_SHIFT 16 +#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT) +#define ERIAR_MASK_SHIFT 12 +#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT) +#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT) + OCPDR = 0xb0, /* OCP GPHY access */ +#define OCPDR_WRITE_CMD 0x80000000 +#define OCPDR_READ_CMD 0x00000000 +#define OCPDR_REG_MASK 0x7fff +#define OCPDR_REG_SHIFT 16 +#define OCPDR_DATA_MASK 0xffff + MCU = 0xd3, +#define NOW_IS_OOB BIT(7) +#define TX_EMPTY BIT(5) +#define RX_EMPTY BIT(4) +#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY) +#define LINK_LIST_RDY BIT(1) + RX_MAX_SIZE = 0xda, + C_PLUS_CMD = 0xe0, + INTR_MITIGATE = 0xe2, + RX_DESC_ADDR_LOW = 0xe4, + RX_DESC_ADDR_HIGH = 0xe8, + MAX_TX_PACKET_SIZE = 0xec, /* Unit of 128 bytes. */ +#define EARLY_SIZE 0x27 + MISC = 0xf0, +#define RXDV_GATED_EN BIT(19) +}; + +/* When TX_CLOSE_DISABLE and SEL_PAGE_FLAG are set in OCP_TCR_0, MMIO offsets + * 0x20 and 0x22 are remapped from TX_DESC_START_ADDR_LOW to 16-bit TX no-close + * tail and close descriptor index registers. + */ +enum r8169soc_registers { + TX_DESC_TAIL_IDX = 0x20, /* the last descriptor index */ + TX_DESC_CLOSE_IDX = 0x22, /* the closed descriptor index */ +#define TX_DESC_CNT_MASK 0x3fff +#define TX_DESC_CNT_SIZE 0x4000 +}; + +/* MAC OCP indirect access registers (via OCPDR) */ +enum rtl_ocp_registers { + /* RX_MEM_REG (0xc000 ~ 0xd3ff) */ + OCP_RFIFOFULL_TH_0 = 0xc0a0, + OCP_RFIFOFULL_TH_1 = 0xc0a2, + OCP_RFIFONFULL_TH_0 = 0xc0a4, + OCP_RFIFOEMPTY_TH_0 = 0xc0a8, + + /* PICO (0xdc00 ~ 0xdfff) */ + OCP_SW_GPHY_MDCMDIO_DATA = 0xde08, + OCP_SW_GPHY_MDCMDIO_CMD = 0xde0a, +#define SW_GPHY_MDCMDIO_WRITE_CMD BIT(15) +#define SW_GPHY_MDCMDIO_BUSY BIT(14) + OCP_SW_GPHY_MDCMDIO_PAGE = 0xde0c, + OCP_GPHY_MDCMDIO_CFG = 0xde10, + OCP_EPHY_MDCMDIO_CFG = 0xde30, +#define MDCMDIO_PERIOD_MASK (BIT(7) | BIT(6)) +#define MDCMDIO_PERIOD_8_9MHZ BIT(6) + OCP_FPGA_GPHY0 = 0xde40, +#define GPHY0_SPEED_MASK 0x0030 +#define GPHY0_SPEED_1000 0x0020 + OCP_FPGA_GPHY1 = 0xde42, +#define GPHY1_DRV_MODE BIT(0) + OCP_MDIO_NON_IDLE = 0xde4e, +#define MDIO_NON_IDLE_FLAG BIT(0) + + /* PME (0xe000 ~ 0xe3ff) */ + OCP_BOOT_CTRL_REG = 0xe004, +#define INIT_AUTOLOAD_DONE BIT(7) + OCP_EEE_IMR = 0xe044, +#define EEE_IMR_MASK GENMASK(3, 0) + OCP_TIMER2_CTRL = 0xe404, +#define TIMER2_DISABLE BIT(9) + + /* TX_MEM_REG (0xe600 ~ 0xe7ff) */ + OCP_TCR_0 = 0xe610, +#define TX_CLOSE_DISABLE BIT(6) +#define SEL_PAGE_FLAG BIT(4) + OCP_TFIFOFULL_TH_0 = 0xe618, + OCP_TFIFOFULL_TH_1 = 0xe61a, + + /* MISC (0xe800 ~ 0xebff) */ + OCP_SFF_STS_7 = 0xe8de, +#define SFF_RE_INIT_LL BIT(15) +#define SFF_MCU_BORW_EN BIT(14) + OCP_RMII_REG = 0xea30, +#define RMII_ENABLE BIT(0) + OCP_RGMII_REG = 0xea34, +#define MII_ENABLE BIT(1) +#define RGMII_ENABLE BIT(0) +}; + +/* MAC ERI indirect access registers (via ERIDR / ERIAR) */ +enum rtl_eri_registers { + ERI_RSS_CONTROL_0 = 0x0b8, + ERI_VMQ_CONTROL = 0x0c0, + ERI_FTR_MCU_CTRL = 0x0dc, +#define ERI_FTR_MCU_EN BIT(0) + ERI_EEE_CR = 0x1b0, +#define ERI_PAUSE_TX_EN BIT(12) + ERI_PLA_CKCTRL_0 = 0x2f8, + ERI_MISC_2 = 0x2fc, +}; + +/* Realtek PHY pages and registers (via PHYAR / OCP MDIO) */ +enum rtl_phy_pages { + PHY_PAGE_A42 = 0x0a42, + PHY_PAGE_A43 = 0x0a43, + PHY_PAGE_A46 = 0x0a46, +}; + +enum rtl_phy_registers { + /* Page 0xa42 */ + PHY_STATUS_A42_REG = 16, +#define PHY_STATUS_A42_MASK 0x07 +#define PHY_STATUS_BYPASS 0x01 +#define PHY_STATUS_LAN_ON 0x03 + + /* Page 0xa43 */ + PHY_PHYCR1_REG = 24, +#define PHYCR1_ALDPS_EN BIT(2) + PHY_PAGSR_REG = 31, + + /* Page 0xa46 */ + PHY_PHYSCR_REG = 20, +#define PHYSCR_CFG_DONE (BIT(1) | BIT(0)) +}; + +enum rtl_register_content { + /* InterruptStatusBits */ + LINK_CHG = 0x0020, + RX_OVERFLOW = 0x0010, + TX_ERR = 0x0008, + TX_OK = 0x0004, + RX_ERR = 0x0002, + RX_OK = 0x0001, + + /* RxStatusDesc */ + RX_RWT = BIT(22), + RX_RES = BIT(21), + RX_RUNT = BIT(20), + RX_CRC = BIT(19), + + /* ChipCmdBits */ + CMD_RESET = 0x10, + CMD_RX_ENB = 0x08, + CMD_TX_ENB = 0x04, + + /* TXPoll register */ + NPQ = 0x40, /* Poll cmd on the low prio queue */ + + /* Cfg9346Bits */ + CFG9346_LOCK = 0x00, + CFG9346_UNLOCK = 0xc0, + + /* rx_mode_bits */ + ACCEPT_ERR = 0x20, + ACCEPT_RUNT = 0x10, +#define RX_CONFIG_ACCEPT_ERR_MASK (ACCEPT_ERR | ACCEPT_RUNT) + ACCEPT_BROADCAST = 0x08, + ACCEPT_MULTICAST = 0x04, + ACCEPT_MY_PHYS = 0x02, + ACCEPT_ALL_PHYS = 0x01, +#define RX_CONFIG_ACCEPT_OK_MASK (ACCEPT_BROADCAST | ACCEPT_MULTICAST | \ + ACCEPT_MY_PHYS | ACCEPT_ALL_PHYS) +#define RX_CONFIG_ACCEPT_MASK (RX_CONFIG_ACCEPT_ERR_MASK | \ + RX_CONFIG_ACCEPT_OK_MASK) + + /* TxConfigBits */ + TX_INTER_FRAME_GAP_SHIFT = 24, + TX_DMA_SHIFT = 8, /* DMA burst value (0-7) + * is shift this many bits + */ + + /* CONFIG1 register (0x52) */ + PM_ENABLE = BIT(0), /* Power Management Enable */ + + /* CONFIG2 register (0x53) */ + CLKREQ_EN_LOCK = BIT(7), /* clk request enable */ + + /* CONFIG3 register (0x54) */ + MAGIC_PKT = BIT(5), /* Wake up when receives a Magic Pkt */ + + /* CONFIG5 register (0x56) */ + LAN_WAKE = BIT(1), /* LAN_WAKE enable/disable */ + ASPM_EN = BIT(0), /* ASPM enable */ + + /* C_PLUS_CMD */ + PKT_CNTR_DISABLE = BIT(7), + RX_VLAN = BIT(6), + RX_CHK_SUM = BIT(5), + INTT_3 = 0x0003, +}; + +enum rtl_desc_bit { + /* First doubleword. */ + DESC_OWN = BIT(31), /* Descriptor is owned by NIC */ + RING_END = BIT(30), /* End of descriptor ring */ + FIRST_FRAG = BIT(29), /* First segment of a packet */ + LAST_FRAG = BIT(28), /* Final segment of a packet */ +}; + +/* Generic case. */ +enum rtl_tx_desc_bit { + /* First doubleword. */ + TD_LSO = BIT(27), /* Large Send Offload */ +#define TD_MSS_MAX 0x07ffu /* MSS value */ + + /* Second doubleword. */ + TX_VLAN_TAG = BIT(17), /* Add VLAN tag */ +}; + +enum rtl_tx_desc_bit_1 { + /* Second doubleword. */ +#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */ + TD1_IP_CS = BIT(29), /* Calculate IP checksum */ + TD1_TCP_CS = BIT(30), /* Calculate TCP/IP checksum */ + TD1_UDP_CS = BIT(31), /* Calculate UDP/IP checksum */ +}; + +enum rtl_rx_desc_bit { + /* Rx private */ + PID1 = BIT(18), /* Protocol ID bit 1/2 */ + PID0 = BIT(17), /* Protocol ID bit 2/2 */ + +#define RX_PROTO_UDP (PID1) +#define RX_PROTO_TCP (PID0) +#define RX_PROTO_IP (PID1 | PID0) +#define RX_PROTO_MASK RX_PROTO_IP + + IP_FAIL = BIT(16), /* IP checksum failed */ + UDP_FAIL = BIT(15), /* UDP/IP checksum failed */ + TCP_FAIL = BIT(14), /* TCP/IP checksum failed */ + +#define RX_CS_FAIL_MASK (IP_FAIL | UDP_FAIL | TCP_FAIL) + + RX_VLAN_TAG = BIT(16), /* VLAN tag available */ +}; + +struct tx_desc { + __le32 opts1; + __le32 opts2; + __le64 addr; +}; + +struct rx_desc { + __le32 opts1; + __le32 opts2; + __le64 addr; +}; + +struct ring_info { + struct sk_buff *skb; + u32 len; +}; + +enum features { + RTL_FEATURE_GMII = BIT(2), + RTL_FEATURE_TX_NO_CLOSE = BIT(3), + RTL_FEATURE_ADJUST_FIFO = BIT(5), + RTL_FEATURE_OCP_MDIO = BIT(8), +}; + +enum rtl_flag { + RTL_FLAG_TASK_ENABLED = 0, + RTL_FLAG_TASK_RESET_PENDING, + RTL_FLAG_TASK_TX_TIMEOUT, + RTL_FLAG_MAX +}; + +enum drv_status { + RTL_STATUS_DOWN = 0, + RTL_STATUS_REINIT, + RTL_STATUS_CLK_ETN_SYS_EN, + RTL_STATUS_CLK_ETN_250M_EN, + RTL_STATUS_CLK_MISC_EN, + RTL_STATUS_MAX +}; + +/* ISO system controller registers */ +enum common_iso_registers { + ISO_UMSK_ISR = 0x0004, + ISO_ETN_TESTIO = 0x0060, + ISO_PLL_WDOUT = 0x0070, +}; + +/* RTD1625 MAIN2 misc system controller registers */ +enum rtd1625_m2tmx_registers { + RTD1625_M2TMX_PWRCUT_ETN = 0x0180, +#define RTD1625_PWRCUT_ETN_DEFAULT 0x04012713 + RTD1625_M2TMX_ETN_DBUS_CTRL = 0x0188, /* DBUS clock gating */ +}; + +struct rtl8169_private; + +struct r8169soc_chip_info { + const char *name; + int (*mdio_write)(struct rtl8169_private *tp, int page, int reg, int value); + int (*mdio_read)(struct rtl8169_private *tp, int page, int reg); + void (*mdio_lock)(struct rtl8169_private *tp); + void (*mdio_unlock)(struct rtl8169_private *tp); + int (*reset_phy_gmac)(struct rtl8169_private *tp); + int (*pll_clock_init)(struct rtl8169_private *tp); + int (*mdio_init)(struct rtl8169_private *tp); + void (*hw_phy_config)(struct rtl8169_private *tp); + + u32 led_cfg; + u32 features; + u16 jumbo_max; + bool jumbo_tx_csum; +}; + +struct rtl8169_private { + void __iomem *mmio_addr; /* memory map physical address */ + struct regmap *iso_base; + struct regmap *m2tmx_base; + + struct clk *clk_etn_250m; + struct clk *clk_etn_sys; + struct clk *clk_misc; + struct reset_control *rstc_gmac; + struct reset_control *rstc_gphy; + struct reset_control *rstc_misc; + + struct platform_device *pdev; + struct net_device *dev; + struct phy_device *phydev; + struct napi_struct napi; + u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */ + u32 cur_tx; /* Index into the Tx descriptor buffer of next Tx pkt. */ + u32 dirty_tx; + + struct tx_desc *tx_desc_array; /* 256-aligned Tx descriptor ring */ + struct rx_desc *rx_desc_array; /* 256-aligned Rx descriptor ring */ + dma_addr_t tx_phy_addr; + dma_addr_t rx_phy_addr; + + struct page *rx_databuff[NUM_RX_DESC]; /* Rx data buffers */ + struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */ + u16 cp_cmd; + u16 irq_mask; + + struct { + DECLARE_BITMAP(flags, RTL_FLAG_MAX); + struct mutex mutex; /* mutex for work */ + struct work_struct work; + } wk; + + raw_spinlock_t mac_ocp_lock; + + u8 mac_addr[ETH_ALEN]; + + struct r8169soc_chip_info chip_info; + struct r8169soc_chip_info *chip; + + DECLARE_BITMAP(status, RTL_STATUS_MAX); +}; + +struct rtl_cond { + bool (*check)(struct rtl8169_private *tp); + const char *msg; +}; + +#define DECLARE_RTL_COND(name) \ +static bool name ## _check(struct rtl8169_private *tp); \ + \ +static const struct rtl_cond name = { \ + .check = name ## _check, \ + .msg = #name \ +}; \ + \ +static bool name ## _check(struct rtl8169_private *tp) + +static inline struct device *tp_to_dev(struct rtl8169_private *tp) +{ + return &tp->pdev->dev; +} + +static inline void rtl_lock_config_regs(struct rtl8169_private *tp) +{ + RTL_W8(tp, CFG9346, CFG9346_LOCK); +} + +static inline void rtl_unlock_config_regs(struct rtl8169_private *tp) +{ + RTL_W8(tp, CFG9346, CFG9346_UNLOCK); +} + +static inline int rtl_phy_write(struct rtl8169_private *tp, int page, int reg, u32 val) +{ + return tp->chip->mdio_write(tp, page, reg, val); +} + +static inline int rtl_phy_read(struct rtl8169_private *tp, int page, int reg) +{ + return tp->chip->mdio_read(tp, page, reg); +} + +static inline int rtl_phy_modify(struct rtl8169_private *tp, int page, int reg, + u16 mask, u16 set) +{ + int val = rtl_phy_read(tp, page, reg); + + if (val < 0) + return val; + + return rtl_phy_write(tp, page, reg, (val & ~mask) | set); +} + +#endif /* __RTL8169SOC_H__ */ -- 2.49.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH net-next 2/2] net: ethernet: realtek: Add r8169soc Ethernet driver for Realtek STB SoCs 2026-12-04 10:07 ` [PATCH net-next 2/2] net: ethernet: realtek: Add r8169soc Ethernet driver for Realtek STB SoCs Eric Wang @ 2026-12-05 10:32 ` netdev-bot+sashiko 0 siblings, 0 replies; 5+ messages in thread From: netdev-bot+sashiko @ 2026-12-05 10:32 UTC (permalink / raw) To: ericwang Cc: andrew+netdev, davem, edumazet, kuba, pabeni, robh, krzk+dt, conor+dt, p.zabel, netdev, devicetree, linux-kernel, eleanor.lin Thank you for your contribution! Sashiko AI review found 21 potential issue(s) to consider. Critical: 0 · High: 4 · Medium: 12 · Low: 5 If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough). - [High] net: r8169soc: rtl_tx() can wake TX queue before napi_disable() in reset - [High] net: r8169soc: LINK_CHG acked in rtl8169_poll() without phy_mac_interrupt() - [High] net: r8169soc: stale nr_frags after skb linearization in rtl8169_start_xmit() - [High] net: r8169soc: unacked RX_OVERFLOW causes IRQ storm in rtl8169_poll() - [Medium] rtl_init_one() puts NETIF_F_GRO_UDP_FWD into ndev->hw_features and then does `ndev->features |= ndev->hw_features`. - [Medium] On RTD1625 (RTL_FEATURE_OCP_MDIO), the mii_bus callbacks r8169_mdio_read_reg()/r8169_mdio_write_reg() call… - [Medium] r8169_mdio_register() builds the MDIO bus id from a fixed string, `snprintf(new_bus->id, MII_BUS_ID_SIZE, "%s-0",… - [Medium] rtl_open() re-initializes the MAC/PHY only when RTL_STATUS_CLK_ETN_SYS_EN is clear (`if… - [Medium] r8169_mdio_read_reg()/r8169_mdio_write_reg() lead to int_ocp_mdio_read()/write(), then rtl_ocp_read()/write(), which do… - [Medium] Probe takes dev->addr_list_lock before it has been initialized. - [Medium] The driver uses RTL_STATUS_CLK_ETN_SYS_EN as its 'MAC registers are accessible' gate in rtl_set_rx_mode(), the ISR and… - [Medium] rtd1625_reset_phy_gmac() relies on synchronize_irq() to make sure no rtl8169_interrupt() is still running before it disables… - [Medium] In TX_NO_CLOSE mode (on by default for RTD1625), rtl_tx() takes tx_left only from the hardware TX_DESC_CLOSE_IDX register:… - [Medium] rtl8169_tso_csum() requests hardware IP/TCP/UDP checksum insertion (TD1_IP_CS/TD1_TCP_CS/TD1_UDP_CS) for CHECKSUM_PARTIAL… - [Medium] net: r8169soc: missing rtnl_lock() in rtl_shutdown() races with rtl_open() - [Medium] net: r8169soc: unlocked RX_CONFIG access races with rtl_set_rx_mode() - [Low] The only example in Documentation/devicetree/bindings/net/realtek,r8169soc.yaml (line 99)… - [Low] rtl8169_init_phy() keeps the comment "We may have called phy_speed_down before" (rtl8169soc.c:470), copied from r8169_main. - [Low] rtl8169_set_features() calls __rtl8169_set_features() only when RTL_STATUS_CLK_ETN_SYS_EN is set, but returns 0 either way, so the… - [Low] rtl_set_mac_address() writes tp->mac_addr with ether_addr_copy() (line 489) holding only rtnl, before rtl_rar_set() takes wk.mutex. - [Low] net: r8169soc: smp_rmb() placed before READ_ONCE(tp->cur_tx) in rtl_tx() -- net: ethernet: realtek: Add r8169soc Ethernet driver for Realtek STB SoCs This adds a new platform driver for the embedded r8169soc Ethernet controller on Realtek STB SoCs, starting with RTD1625. The MAC uses the same descriptor ring design as RTL8168/8169. It is wired up through SoC clocks, resets and syscon regmaps, and pairs with an embedded FE PHY. NET_VENDOR_REALTEK can now be enabled on ARCH_REALTEK or COMPILE_TEST. > diff --git a/drivers/net/ethernet/realtek/r8169soc/rtl8169soc.c b/drivers/net/ethernet/realtek/r8169soc/rtl8169soc.c > new file mode 100644 > index 0000000000000..0d3e68bd75123 > --- /dev/null > +++ b/drivers/net/ethernet/realtek/r8169soc/rtl8169soc.c > @@ -0,0 +1,2375 @@ [ ... ] > +static int int_ocp_mdio_read(struct rtl8169_private *tp, int page, int reg) > +{ > + if (page != CURRENT_MDIO_PAGE) > + rtl_ocp_write(tp, OCP_SW_GPHY_MDCMDIO_PAGE, page); > + rtl_ocp_write(tp, OCP_SW_GPHY_MDCMDIO_CMD, reg & PHYAR_REG_MASK); [Severity: Medium] The mii_bus ops r8169_mdio_read_reg() and r8169_mdio_write_reg() reach this with CURRENT_MDIO_PAGE. In that case OCP_SW_GPHY_MDCMDIO_PAGE is never written, and register 0x1f is passed straight through as reg & 0x1f. The Realtek PHY driver picks pages by writing register 0x1f over the bus. For example, rtlgen_supports_2_5gbps() does: phy_write(phydev, RTL821x_PAGE_SELECT, 0xa61); val = phy_read(phydev, 0x13); phy_write(phydev, RTL821x_PAGE_SELECT, 0); Also, before devm_mdiobus_register() probes the PHY, __rtl_reinit_mac_phy() calls: rtl_phy_modify(tp, PHY_PAGE_A43, PHY_PHYCR1_REG, 0, PHYCR1_ALDPS_EN); That leaves OCP_SW_GPHY_MDCMDIO_PAGE at 0xa43. rtd1625_mdio_init() also leaves PHY reg 31 at 0xa42 through the PHYAR path. Suppose the OCP MDIO engine applies the PAGE register to every command. Would phylib accesses that assume page 0, and the paged accesses made by the PHY driver, then hit the wrong registers? For OCP based PHY access, r8169_main.c emulates register 0x1f in software (r8168g_mdio_write() / r8168g_mdio_read()). Is something similar needed here? [ ... ] > +static int rtl8169_set_features(struct net_device *dev, > + netdev_features_t features) > +{ > + struct rtl8169_private *tp = netdev_priv(dev); > + > + rtl_lock_work(tp); > + if (test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status)) > + __rtl8169_set_features(tp, features); > + rtl_unlock_work(tp); > + > + return 0; > +} [Severity: Low] When RTL_STATUS_CLK_ETN_SYS_EN is clear (for example after a failed re-init), this skips the change but still returns 0. The core then commits the new dev->features while tp->cp_cmd keeps the old RX_VLAN and RX_CHK_SUM bits. rtl_hw_start() only ORs bits into cp_cmd and never rebuilds it from dev->features: tp->cp_cmd |= RTL_R16(tp, C_PLUS_CMD) | PKT_CNTR_DISABLE | INTT_3; If rx-vlan-offload is turned off in that state, will the hardware keep stripping VLAN tags after the next open? rtl8169_rx_vlan_tag() also reports them as hwaccel tags without checking NETIF_F_HW_VLAN_CTAG_RX. [ ... ] > +static void rtl8169_init_phy(struct rtl8169_private *tp) > +{ > + rtl_hw_phy_config(tp); > + > + /* disable now_is_oob */ > + RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB); > + > + /* We may have called phy_speed_down before */ > + phy_speed_up(tp->phydev); [Severity: Low] This isn't a bug, but this driver never calls phy_speed_down(). The comment looks like it came over from r8169_main.c. Should it be dropped? > + > + genphy_soft_reset(tp->phydev); > +} > + > +static int rtl_set_mac_address(struct net_device *dev, void *p) > +{ [ ... ] > + eth_hw_addr_set(dev, addr->sa_data); > + > + ether_addr_copy(tp->mac_addr, addr->sa_data); > + > + rtl_rar_set(tp, addr->sa_data); [Severity: Low] tp->mac_addr is written here with only rtnl held, before rtl_rar_set() takes wk.mutex. rtl_task() holds wk.mutex but not rtnl, and it can be running this path at the same time: rtl_task()->rtl_mac_reinit()->rtl_reinit_mac_phy()->__rtl_reinit_mac_phy() if (test_bit(RTL_STATUS_REINIT, tp->status)) __rtl_rar_set(tp, tp->mac_addr); Could this race program MAC0/MAC4 with a mix of old and new address bytes? The later rtl_rar_set() call does rewrite it, but should the copy into tp->mac_addr be done under wk.mutex? [ ... ] > +static bool rtl8169_tso_csum(struct sk_buff *skb, u32 *opts) > +{ [ ... ] > + } else if (skb->ip_summed == CHECKSUM_PARTIAL) { > + const struct iphdr *ip; > + > + if (vlan_get_protocol(skb) != htons(ETH_P_IP)) > + return !skb_checksum_help(skb); > + > + ip = ip_hdr(skb); > + if (ip->protocol == IPPROTO_TCP) > + opts[1] |= TD1_IP_CS | TD1_TCP_CS; > + else if (ip->protocol == IPPROTO_UDP) > + opts[1] |= TD1_IP_CS | TD1_UDP_CS; [Severity: Medium] This requests checksum offload for frames of any length, and the driver has no .ndo_features_check. The r8169 driver for the same MAC family does this for all chips in rtl8169_features_check(): /* work around hw bug on some chip versions */ if (skb->len < ETH_ZLEN) features &= ~NETIF_F_CSUM_MASK; Is this SoC MAC known not to have that erratum? If it does have it, would short frames, such as 54 byte TCP ACKs without timestamps, go out with bad L4 checksums? [ ... ] > +static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp, > + int budget) > +{ [ ... ] > + dirty_tx = tp->dirty_tx; > + if (tp->chip->features & RTL_FEATURE_TX_NO_CLOSE) { > + close_idx = RTL_R16(tp, TX_DESC_CLOSE_IDX) & TX_DESC_CNT_MASK; > + dirty_tx_idx = dirty_tx & TX_DESC_CNT_MASK; > + smp_rmb(); /* make sure dirty_tx and close_idx are updated */ > + > + if (dirty_tx_idx <= close_idx) > + tx_left = close_idx - dirty_tx_idx; > + else > + tx_left = close_idx + TX_DESC_CNT_SIZE - dirty_tx_idx; > + } else { > + smp_rmb(); /* make sure dirty_tx is updated */ > + tx_left = READ_ONCE(tp->cur_tx) - dirty_tx; > + } > + > + while (tx_left > 0) { > + unsigned int entry = dirty_tx % NUM_TX_DESC; > + u32 status; > + > + status = le32_to_cpu(READ_ONCE(tp->tx_desc_array[entry].opts1)); > + if (!(tp->chip->features & RTL_FEATURE_TX_NO_CLOSE) && > + (status & DESC_OWN)) > + break; [Severity: Low] In the !RTL_FEATURE_TX_NO_CLOSE branch, the smp_rmb() comes before READ_ONCE(tp->cur_tx), so it does not order anything that matters here. rtl8169_start_xmit() writes the descriptors and tx_skb[], does smp_wmb() and then publishes tp->cur_tx. For that to pair, the read side needs the barrier after the cur_tx load and before the loads of tx_desc_array[entry].opts1 and tx_skb[entry]. As written, on a weakly ordered CPU such as arm64, rtl_tx() could see the new cur_tx but stale descriptor contents. One example is an opts1 that an earlier rtl8169_unmap_tx_skb() zeroed, so DESC_OWN reads as clear. rtl_tx() would then unmap and complete a buffer that the hardware has not transmitted yet. RTD1625 sets RTL_FEATURE_TX_NO_CLOSE, so this branch is not reachable today. It is still the path any future chip without that feature will use. Could this use smp_load_acquire(&tp->cur_tx), or move the smp_rmb() after the read? The comment about dirty_tx also seems to describe the wrong variable. [Severity: Medium] In TX_NO_CLOSE mode, tx_left comes only from the hardware TX_DESC_CLOSE_IDX register. It can be as large as 0x3fff, while the ring has 1024 entries. It is not limited to cur_tx - dirty_tx, and the loop skips the DESC_OWN check in this mode. What happens if close_idx runs ahead of cur_tx? Two ways that could happen: - an all-ones read, which rtl8169_interrupt() already handles as 0xffff - the hardware close index not being reset when rtl8169_cleanup() zeroes the software indices Wouldn't this then unmap and free tx_skb entries that are still in flight, call dma_unmap_single() on unused slots, and push dirty_tx past cur_tx? After that, rtl_tx_slots_avail() would report more than NUM_TX_DESC free slots, and rtl8169_start_xmit() could overwrite descriptors the hardware still owns. Would clamping tx_left to READ_ONCE(tp->cur_tx) - dirty_tx be safer? [ ... ] > +#ifdef CONFIG_NET_POLL_CONTROLLER > +static void rtl8169_netpoll(struct net_device *dev) > +{ > + struct rtl8169_private *tp = netdev_priv(dev); > + > + rtl8169_interrupt(dev->irq, tp); > +} > +#endif [Severity: Medium] rtd1625_reset_phy_gmac() relies on synchronize_irq() before it gates etn_sys: if (clk_en) { synchronize_irq(tp->dev->irq); clk_disable_unprepare(tp->clk_etn_sys); } synchronize_irq() does not wait for this direct call from netpoll_poll_dev(), and rtl_mac_reinit() does not call netpoll_poll_disable(). Can this sequence happen? CPU1 (netconsole) netpoll_poll_dev()->rtl8169_netpoll()->rtl8169_interrupt() test_bit(RTL_STATUS_CLK_ETN_SYS_EN) passes CPU0 (rtl_task) rtl_mac_reinit()->rtl_reinit_mac_phy()->rtd1625_reset_phy_gmac() test_and_clear_bit(RTL_STATUS_CLK_ETN_SYS_EN) synchronize_irq() /* returns immediately */ clk_disable_unprepare(tp->clk_etn_sys) reset_control_assert(tp->rstc_gmac) CPU1 rtl_get_events() / rtl_irq_disable() /* MMIO on gated MAC */ At that point, the "TX timeout, resetting MAC" warning itself generates netconsole traffic. [ ... ] > + rtl_lock_work(tp); > + > + if (!test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status)) { > + retval = rtl_reinit_mac_phy(tp); > + if (retval) > + goto err_free_rx_2; > + } [Severity: Medium] rtl_open() skips the re-init whenever RTL_STATUS_CLK_ETN_SYS_EN is set. But rtd1625_pll_clock_init() sets that bit right after enabling etn_sys, before steps that can still fail: enabling etn_250m and the ISO_PLL_WDOUT autoload poll. rtd1625_mdio_init() and the ALDPS rtl_phy_modify() in __rtl_reinit_mac_phy() can also fail after that without clearing the bit. Consider: rtl8169_tx_timeout()->rtl_task()->rtl_mac_reinit() rtl8169_down() /* sets RTL_STATUS_DOWN */ rtl_reinit_mac_phy() /* fails after set_bit(CLK_ETN_SYS_EN) */ netif_carrier_off(); return; ifdown: rtl8169_close()->rtl8169_down() returns early since DOWN is set ifup: rtl_open() sees CLK_ETN_SYS_EN set and skips rtl_reinit_mac_phy() Would open then report success on a GMAC whose autoload or PHY LAN-ON and FEPHY setup never finished? If so, the interface can't be recovered without rebinding the driver. The same applies when rtl_open()'s own rtl_reinit_mac_phy() fails after the bit was set. [ ... ] > +static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg) > +{ > + struct rtl8169_private *tp = mii_bus->priv; > + > + if (phyaddr > 0) > + return -ENODEV; > + > + return rtl_phy_read(tp, CURRENT_MDIO_PAGE, phyreg); > +} [Severity: Medium] This path ends up in rtl_ocp_read() and rtl_ocp_write(), which do MMIO on OCPDR. Unlike set_features, rar_set, set_rx_mode and the ISR, there is no RTL_STATUS_CLK_ETN_SYS_EN check here. rtl_mac_reinit() can fail after rtd1625_reset_phy_gmac() has gated etn_sys and asserted the gmac/gphy resets, for example if pll_clock_init() fails early. It then just calls netif_carrier_off() and returns, so the netdev stays running with the PHY attached. Can a later SIOCGMIIREG via phy_do_ioctl_running(), or ethtool nway_reset or set_link_ksettings, then do MMIO on a clock-gated MAC that is held in reset? [ ... ] > + new_bus->irq[0] = PHY_MAC_INTERRUPT; > + new_bus->phy_mask = GENMASK(31, 1); > + snprintf(new_bus->id, MII_BUS_ID_SIZE, "%s-0", KBUILD_MODNAME); [Severity: Medium] This gives "r8169soc-0" for every instance. Would a second r8169soc controller fail devm_mdiobus_register() because of the name collision? Using something like dev_name(&pdev->dev) would make the id unique per device. [ ... ] > + /* Serialize against rtl_set_rx_mode() and ISR before disabling MAC clock */ > + netif_addr_lock_bh(tp->dev); > + clk_en = test_and_clear_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status); > + netif_addr_unlock_bh(tp->dev); [Severity: Medium] rtl_init_one() reaches this through tp->chip->reset_phy_gmac(tp). Later it calls __rtl8169_set_features(), which also takes netif_addr_lock_bh(). Both happen before register_netdev(). dev->addr_list_lock is only initialized in register_netdevice(): spin_lock_init(&dev->addr_list_lock); netdev_set_addr_lockdep_class(dev); Doesn't probe take an uninitialized spinlock here? With CONFIG_DEBUG_SPINLOCK that would give a bad magic report. Lockdep would complain about a non-static key and turn itself off. [ ... ] > + if (!test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status)) { > + ret = clk_prepare_enable(tp->clk_etn_sys); > + if (ret) { > + dev_dbg(d, "failed to enable etn_sys clk: %d\n", ret); > + return ret; > + } > + set_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status); > + } [Severity: Medium] rtl_set_rx_mode(), the ISR and netpoll treat RTL_STATUS_CLK_ETN_SYS_EN as the sign that MAC registers are accessible. Here it is set before etn_250m is enabled, and before the ISO_PLL_WDOUT init_autoload_done ("ETN RBUS valid") poll completes. rtl_set_rx_mode() runs under netif_addr_lock_bh() only, not wk.mutex. During rtl_task()->rtl_mac_reinit(), could a concurrent multicast or promisc change see the bit set and write MAR0 and RX_CONFIG before the MAC bus is valid? Would it be better to set the bit only after the init sequence has completed? [ ... ] > +static void rtl_init_mac_address(struct rtl8169_private *tp) > +{ > + /* Default placeholder MAC in DTB when factory MAC is not programmed > + * by the bootloader; treat it as unconfigured and use a random MAC. > + */ > + static const u8 default_mac[ETH_ALEN] = { > + 0x00, 0x10, 0x20, 0x30, 0x40, 0x50 > + }; > + struct net_device *dev = tp->dev; > + u8 mac_addr[ETH_ALEN] = {}; > + int rc; > + > + rc = eth_platform_get_mac_address(tp_to_dev(tp), mac_addr); > + if (!rc && is_valid_ether_addr(mac_addr) && > + !ether_addr_equal(mac_addr, default_mac)) { [Severity: Low] The only example in Documentation/devicetree/bindings/net/realtek,r8169soc.yaml uses: local-mac-address = [00 10 20 30 40 50]; That is exactly the address treated as unset here, so a DT copied from the binding example gets a random MAC on every boot. Should the binding either document this value as a placeholder or use a different example address? [ ... ] > + ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO | > + NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX | > + NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_GRO_UDP_FWD; > + > + ndev->features |= ndev->hw_features; [Severity: Medium] NETIF_F_GRO_UDP_FWD is part of NETIF_F_SOFT_FEATURES_OFF. register_netdevice() already adds that set to hw_features, but leaves it out of features on purpose: dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF); dev->features |= NETIF_F_SOFT_FEATURES; Adding it here turns rx-udp-gro-forwarding on by default, which no other NIC does. Was that intended? If not, can it be dropped from hw_features? [ ... ] > @@ -0,0 +1,2375 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* rtl8169soc.c: the embedded Realtek STB SoC 8169soc Ethernet driver. > + * > + * Copyright (c) 2002 Realtek Semiconductor Corp. > + * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw> > + * Copyright (c) 2003-2007 Francois Romieu <romieu@fr.zoreil.com> > + * Copyright (c) 2014 YuKuen Wu <yukuen@realtek.com> > + * Copyright (c) 2015-2026 Eric Wang <ericwang@realtek.com> > + */ > + > +#include <linux/clk.h> > +#include <linux/delay.h> > +#include <linux/dma-mapping.h> > +#include <linux/etherdevice.h> > +#include <linux/ethtool.h> > +#include <linux/if_vlan.h> > +#include <linux/interrupt.h> > +#include <linux/io.h> > +#include <linux/ip.h> > +#include <linux/mfd/syscon.h> > +#include <linux/module.h> > +#include <linux/netdevice.h> > +#include <linux/of.h> > +#include <linux/phy.h> > +#include <linux/platform_device.h> > +#include <linux/prefetch.h> > +#include <linux/regmap.h> > +#include <linux/reset.h> > +#include <linux/unaligned.h> > +#include <net/netdev_queues.h> > + > +#include "rtl8169soc.h" > + > +MODULE_AUTHOR("Eric Wang <ericwang@realtek.com>"); > +MODULE_DESCRIPTION("RealTek STB SoC r8169soc Ethernet driver"); > +MODULE_SOFTDEP("pre: realtek"); > +MODULE_LICENSE("GPL"); > + > +static void rtl_lock_work(struct rtl8169_private *tp) > +{ > + mutex_lock(&tp->wk.mutex); > +} > + > +static void rtl_unlock_work(struct rtl8169_private *tp) > +{ > + mutex_unlock(&tp->wk.mutex); > +} > + > +static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c, > + unsigned long usecs, int n, bool high) > +{ > + int i; > + > + for (i = 0; i < n; i++) { > + if (c->check(tp) == high) > + return true; > + fsleep(usecs); > + } > + > + if (net_ratelimit()) > + netdev_err(tp->dev, "%s == %d (loop: %d, delay: %lu).\n", > + c->msg, !high, n, usecs); > + return false; > +} > + > +static bool rtl_loop_wait_high(struct rtl8169_private *tp, > + const struct rtl_cond *c, > + unsigned long d, int n) > +{ > + return rtl_loop_wait(tp, c, d, n, true); > +} > + > +static bool rtl_loop_wait_low(struct rtl8169_private *tp, > + const struct rtl_cond *c, > + unsigned long d, int n) > +{ > + return rtl_loop_wait(tp, c, d, n, false); > +} > + > +DECLARE_RTL_COND(rtl_eriar_cond) > +{ > + return RTL_R32(tp, ERIAR) & ERIAR_FLAG; > +} > + > +DECLARE_RTL_COND(rtl_link_list_ready_cond) > +{ > + return RTL_R8(tp, MCU) & LINK_LIST_RDY; > +} > + > +static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg) > +{ > + if (reg & 0xffff0001) { > + if (net_ratelimit()) > + netdev_err(tp->dev, "Invalid ocp reg %x!\n", reg); > + return true; > + } > + return false; > +} > + > +static void rtl_ocp_write(struct rtl8169_private *tp, u32 reg, u32 value) > +{ > + unsigned long flags; > + unsigned int wdata; > + > + if (rtl_ocp_reg_failure(tp, reg)) > + return; > + > + wdata = OCPDR_WRITE_CMD | > + (((reg >> 1) & OCPDR_REG_MASK) << OCPDR_REG_SHIFT) | > + (value & OCPDR_DATA_MASK); > + > + raw_spin_lock_irqsave(&tp->mac_ocp_lock, flags); > + RTL_W32(tp, OCPDR, wdata); > + raw_spin_unlock_irqrestore(&tp->mac_ocp_lock, flags); > +} > + > +static u32 rtl_ocp_read(struct rtl8169_private *tp, u32 reg) > +{ > + unsigned long flags; > + unsigned int wdata; > + unsigned int rdata; > + > + if (rtl_ocp_reg_failure(tp, reg)) > + return 0; > + > + wdata = OCPDR_READ_CMD | > + (((reg >> 1) & OCPDR_REG_MASK) << OCPDR_REG_SHIFT); > + > + raw_spin_lock_irqsave(&tp->mac_ocp_lock, flags); > + RTL_W32(tp, OCPDR, wdata); > + rdata = RTL_R32(tp, OCPDR); > + raw_spin_unlock_irqrestore(&tp->mac_ocp_lock, flags); > + > + return rdata & OCPDR_DATA_MASK; > +} > + > +DECLARE_RTL_COND(rtl_int_phyar_cond) > +{ > + return rtl_ocp_read(tp, OCP_SW_GPHY_MDCMDIO_CMD) & SW_GPHY_MDCMDIO_BUSY; > +} > + > +DECLARE_RTL_COND(rtl_phyar_cond) > +{ > + return RTL_R32(tp, PHYAR) & PHYAR_FLAG; > +} > + > +static int __int_mdio_read(struct rtl8169_private *tp, int reg) > +{ > + int value; > + > + RTL_W32(tp, PHYAR, > + PHYAR_READ_CMD | ((reg & PHYAR_REG_MASK) << PHYAR_REG_SHIFT)); > + > + value = rtl_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ? > + RTL_R32(tp, PHYAR) & PHY_DATA_MASK : -ETIMEDOUT; > + > + /* According to hardware specs a 20us delay is required after read > + * complete indication, but before sending next command. > + */ > + fsleep(20); > + > + return value; > +} > + > +static int __int_mdio_write(struct rtl8169_private *tp, int reg, int value) > +{ > + int ret; > + > + RTL_W32(tp, PHYAR, PHYAR_WRITE_CMD | > + ((reg & PHYAR_REG_MASK) << PHYAR_REG_SHIFT) | > + (value & PHY_DATA_MASK)); > + > + ret = rtl_loop_wait_low(tp, &rtl_phyar_cond, 25, 20) ? 0 : -ETIMEDOUT; > + /* According to hardware specs a 20us delay is required after write > + * complete indication, but before sending next command. > + */ > + fsleep(20); > + > + return ret; > +} > + > +static int int_mdio_read(struct rtl8169_private *tp, int page, int reg) > +{ > + int value; > + > + tp->chip->mdio_lock(tp); > + > + if (page != CURRENT_MDIO_PAGE) { > + value = __int_mdio_write(tp, PHY_PAGSR_REG, page); > + if (value) > + goto out_unlock; > + } > + > + value = __int_mdio_read(tp, reg); > + > +out_unlock: > + tp->chip->mdio_unlock(tp); > + return value; > +} > + > +static int int_mdio_write(struct rtl8169_private *tp, int page, int reg, > + int value) > +{ > + int ret; > + > + tp->chip->mdio_lock(tp); > + > + if (page != CURRENT_MDIO_PAGE) { > + ret = __int_mdio_write(tp, PHY_PAGSR_REG, page); > + if (ret) > + goto out_unlock; > + } > + > + ret = __int_mdio_write(tp, reg, value); > + > +out_unlock: > + tp->chip->mdio_unlock(tp); > + return ret; > +} > + > +static int int_ocp_mdio_read(struct rtl8169_private *tp, int page, int reg) > +{ > + if (page != CURRENT_MDIO_PAGE) > + rtl_ocp_write(tp, OCP_SW_GPHY_MDCMDIO_PAGE, page); > + rtl_ocp_write(tp, OCP_SW_GPHY_MDCMDIO_CMD, reg & PHYAR_REG_MASK); > + > + return rtl_loop_wait_low(tp, &rtl_int_phyar_cond, 25, 20) ? > + rtl_ocp_read(tp, OCP_SW_GPHY_MDCMDIO_DATA) & PHY_DATA_MASK : > + -ETIMEDOUT; > +} > + > +static int int_ocp_mdio_write(struct rtl8169_private *tp, int page, int reg, > + int value) > +{ > + if (page != CURRENT_MDIO_PAGE) > + rtl_ocp_write(tp, OCP_SW_GPHY_MDCMDIO_PAGE, page); > + rtl_ocp_write(tp, OCP_SW_GPHY_MDCMDIO_DATA, value); > + rtl_ocp_write(tp, OCP_SW_GPHY_MDCMDIO_CMD, > + SW_GPHY_MDCMDIO_WRITE_CMD | (reg & PHYAR_REG_MASK)); > + > + return rtl_loop_wait_low(tp, &rtl_int_phyar_cond, 25, 20) ? > + 0 : -ETIMEDOUT; > +} > + > +static void dummy_mdio_lock(struct rtl8169_private *tp) > +{ > +} > + > +static void dummy_mdio_unlock(struct rtl8169_private *tp) > +{ > +} > + > +/* Chips with RTL_FEATURE_OCP_MDIO access PHY registers via dedicated OCP > + * registers (0xde08..0xde0c) that do not conflict with hardware MAC polling, > + * while chip->mdio_lock is used by int_mdio_read/write (PHYAR) during init. > + */ > +static void rtl_init_mdio_ops(struct rtl8169_private *tp) > +{ > + if (tp->chip->features & RTL_FEATURE_OCP_MDIO) { > + tp->chip->mdio_write = int_ocp_mdio_write; > + tp->chip->mdio_read = int_ocp_mdio_read; > + } else { > + tp->chip->mdio_write = int_mdio_write; > + tp->chip->mdio_read = int_mdio_read; > + } > +} > + > +static void __rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask, > + u32 val, int type) > +{ > + WARN_ON((addr & 3) || (mask == 0)); > + RTL_W32(tp, ERIDR, val); > + RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr); > + > + rtl_loop_wait_low(tp, &rtl_eriar_cond, 100, 100); > +} > + > +static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask, > + u32 val) > +{ > + __rtl_eri_write(tp, addr, mask, val, ERIAR_EXGMAC); > +} > + > +static u32 __rtl_eri_read(struct rtl8169_private *tp, int addr, int type) > +{ > + RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr); > + > + return rtl_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ? > + RTL_R32(tp, ERIDR) : ~0; > +} > + > +static u32 rtl_eri_read(struct rtl8169_private *tp, int addr) > +{ > + return __rtl_eri_read(tp, addr, ERIAR_EXGMAC); > +} > + > +static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 p, u32 m) > +{ > + u32 val = rtl_eri_read(tp, addr); > + > + if (val == ~0U) { > + dev_dbg(tp_to_dev(tp), "failed to read ERI addr 0x%x\n", addr); > + return; > + } > + > + rtl_eri_write(tp, addr, ERIAR_MASK_1111, (val & ~m) | p); > +} > + > +static void rtl_eri_set_bits(struct rtl8169_private *tp, int addr, u32 p) > +{ > + rtl_w0w1_eri(tp, addr, p, 0); > +} > + > +static void rtl_eri_clear_bits(struct rtl8169_private *tp, int addr, u32 m) > +{ > + rtl_w0w1_eri(tp, addr, 0, m); > +} > + > +static void rtl_reset_packet_filter(struct rtl8169_private *tp) > +{ > + rtl_eri_clear_bits(tp, ERI_FTR_MCU_CTRL, ERI_FTR_MCU_EN); > + rtl_eri_set_bits(tp, ERI_FTR_MCU_CTRL, ERI_FTR_MCU_EN); > +} > + > +static u16 rtl_get_events(struct rtl8169_private *tp) > +{ > + return RTL_R16(tp, INTR_STATUS); > +} > + > +static void rtl_ack_events(struct rtl8169_private *tp, u16 bits) > +{ > + RTL_W16(tp, INTR_STATUS, bits); > +} > + > +static void rtl_irq_disable(struct rtl8169_private *tp) > +{ > + RTL_W16(tp, INTR_MASK, 0); > +} > + > +static void rtl_irq_enable(struct rtl8169_private *tp) > +{ > + RTL_W16(tp, INTR_MASK, tp->irq_mask); > +} > + > +static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp) > +{ > + rtl_irq_disable(tp); > + rtl_ack_events(tp, 0xffff); > + RTL_R8(tp, CHIP_CMD); > +} > + > +static netdev_features_t rtl8169_fix_features(struct net_device *dev, > + netdev_features_t features) > +{ > + struct rtl8169_private *tp = netdev_priv(dev); > + > + if (dev->mtu > TD_MSS_MAX) > + features &= ~NETIF_F_ALL_TSO; > + > + if (dev->mtu > JUMBO_1K && !tp->chip->jumbo_tx_csum) > + features &= ~NETIF_F_IP_CSUM; > + > + return features; > +} > + > +static void rtl_set_rx_config_features(struct rtl8169_private *tp, > + netdev_features_t features) > +{ > + u32 rx_config = RTL_R32(tp, RX_CONFIG); > + > + if (features & NETIF_F_RXALL) > + rx_config |= RX_CONFIG_ACCEPT_ERR_MASK; > + else > + rx_config &= ~RX_CONFIG_ACCEPT_ERR_MASK; > + > + RTL_W32(tp, RX_CONFIG, rx_config); > +} > + > +static void __rtl8169_set_features(struct rtl8169_private *tp, > + netdev_features_t features) > +{ > + netif_addr_lock_bh(tp->dev); > + rtl_set_rx_config_features(tp, features); > + netif_addr_unlock_bh(tp->dev); > + > + if (features & NETIF_F_RXCSUM) > + tp->cp_cmd |= RX_CHK_SUM; > + else > + tp->cp_cmd &= ~RX_CHK_SUM; > + > + if (features & NETIF_F_HW_VLAN_CTAG_RX) > + tp->cp_cmd |= RX_VLAN; > + else > + tp->cp_cmd &= ~RX_VLAN; > + > + RTL_W16(tp, C_PLUS_CMD, tp->cp_cmd); > + RTL_R16(tp, C_PLUS_CMD); > +} > + > +static int rtl8169_set_features(struct net_device *dev, > + netdev_features_t features) > +{ > + struct rtl8169_private *tp = netdev_priv(dev); > + > + rtl_lock_work(tp); > + if (test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status)) > + __rtl8169_set_features(tp, features); > + rtl_unlock_work(tp); > + > + return 0; > +} > + > +static u32 rtl8169_tx_vlan_tag(struct sk_buff *skb) > +{ > + return skb_vlan_tag_present(skb) ? > + TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb)) : 0x00; > +} > + > +static void rtl8169_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb) > +{ > + u32 opts2 = le32_to_cpu(desc->opts2); > + > + if (opts2 & RX_VLAN_TAG) > + __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), > + swab16(opts2 & 0xffff)); > +} > + > +static void rtl_hw_phy_config(struct rtl8169_private *tp) > +{ > + /* power down PHY; genphy_soft_reset() in rtl8169_init_phy() will clear BMCR_PDOWN */ > + phy_modify(tp->phydev, MII_BMCR, 0, BMCR_PDOWN); > + > + tp->chip->hw_phy_config(tp); > +} > + > +static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag) > +{ > + if (!test_and_set_bit(flag, tp->wk.flags)) > + schedule_work(&tp->wk.work); > +} > + > +static void __rtl_rar_set(struct rtl8169_private *tp, const u8 *addr) > +{ > + rtl_unlock_config_regs(tp); > + > + RTL_W32(tp, MAC4, get_unaligned_le16(addr + 4)); > + RTL_R32(tp, MAC4); > + > + RTL_W32(tp, MAC0, get_unaligned_le32(addr)); > + RTL_R32(tp, MAC0); > + > + rtl_lock_config_regs(tp); > +} > + > +static void rtl_rar_set(struct rtl8169_private *tp, const u8 *addr) > +{ > + rtl_lock_work(tp); > + if (test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status)) > + __rtl_rar_set(tp, addr); > + rtl_unlock_work(tp); > +} > + > +static void rtl8169_init_phy(struct rtl8169_private *tp) > +{ > + rtl_hw_phy_config(tp); > + > + /* disable now_is_oob */ > + RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB); > + > + /* We may have called phy_speed_down before */ > + phy_speed_up(tp->phydev); > + > + genphy_soft_reset(tp->phydev); > +} > + > +static int rtl_set_mac_address(struct net_device *dev, void *p) > +{ > + struct rtl8169_private *tp = netdev_priv(dev); > + struct sockaddr *addr = p; > + > + if (!is_valid_ether_addr(addr->sa_data)) { > + dev_dbg(tp_to_dev(tp), "invalid MAC address %pM\n", > + addr->sa_data); > + return -EADDRNOTAVAIL; > + } > + > + eth_hw_addr_set(dev, addr->sa_data); > + > + ether_addr_copy(tp->mac_addr, addr->sa_data); > + > + rtl_rar_set(tp, addr->sa_data); > + > + return 0; > +} > + > +static void rtl_init_rxcfg(struct rtl8169_private *tp) > +{ > + /* disable RX128_INT_EN to reduce CPU loading */ > + RTL_W32(tp, RX_CONFIG, RX_DMA_BURST | RX_EARLY_OFF); > +} > + > +static void rtl8169_init_ring_indexes(struct rtl8169_private *tp) > +{ > + tp->dirty_tx = 0; > + tp->cur_tx = 0; > + tp->cur_rx = 0; > +} > + > +DECLARE_RTL_COND(rtl_chipcmd_cond) > +{ > + return RTL_R8(tp, CHIP_CMD) & CMD_RESET; > +} > + > +static void rtl_hw_reset(struct rtl8169_private *tp) > +{ > + RTL_W8(tp, CHIP_CMD, CMD_RESET); > + > + rtl_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100); > +} > + > +static void rtl_rx_close(struct rtl8169_private *tp) > +{ > + RTL_W32(tp, RX_CONFIG, RTL_R32(tp, RX_CONFIG) & ~RX_CONFIG_ACCEPT_MASK); [Severity: Medium] rtl_set_rx_mode() is called by the core under netif_addr_lock_bh() only, not wk.mutex. It does a read-modify-write of RX_CONFIG whenever RTL_STATUS_CLK_ETN_SYS_EN is set. __rtl8169_set_features() and rtl_hw_start() take netif_addr_lock_bh() around their RX_CONFIG updates, but rtl_rx_close() and rtl_init_rxcfg() do not. rtl_rx_close() runs from rtl8169_cleanup() in rtl_reset_work() and rtl_mac_reinit(). rtl_init_rxcfg() runs from __rtl_reinit_mac_phy() after rtd1625_pll_clock_init() has set RTL_STATUS_CLK_ETN_SYS_EN again. A multicast join or promisc change at those points can lose one of the updates. In particular, rtl_set_rx_mode() can set the ACCEPT bits again right after rtl_rx_close() cleared them. That can happen during rtl_enable_rxdvgate() and the 2ms wait before rtl_hw_reset(), while the driver expects the RX FIFO to drain. It can also write a stale RX_CONFIG back over the RX_DMA_BURST and RX_EARLY_OFF setup done by rtl_init_rxcfg(). Should these paths take netif_addr_lock_bh() as well? Or should rtl_set_rx_mode() be kept from touching RX_CONFIG while a reset is in progress? > +} > + > +DECLARE_RTL_COND(rtl_txcfg_empty_cond) > +{ > + return RTL_R32(tp, TX_CONFIG) & TXCFG_EMPTY; > +} > + > +DECLARE_RTL_COND(rtl_rxtx_empty_cond) > +{ > + return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY; > +} > + > +static void rtl_wait_txrx_fifo_empty(struct rtl8169_private *tp) > +{ > + rtl_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42); > + rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42); > +} > + > +static void rtl_disable_rxdvgate(struct rtl8169_private *tp) > +{ > + RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN); > +} > + > +static void rtl_enable_rxdvgate(struct rtl8169_private *tp) > +{ > + RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN); > + fsleep(2000); > + rtl_wait_txrx_fifo_empty(tp); > +} > + > +static void rtl_set_tx_config_registers(struct rtl8169_private *tp) > +{ > + /* Set DMA burst size and Interframe Gap Time */ > + RTL_W32(tp, TX_CONFIG, (TX_DMA_BURST << TX_DMA_SHIFT) | > + (INTER_FRAME_GAP << TX_INTER_FRAME_GAP_SHIFT)); > +} > + > +static void rtl_set_rx_max_size(struct rtl8169_private *tp) > +{ > + /* Low hurts. Let's disable the filtering. */ > + RTL_W16(tp, RX_MAX_SIZE, RX_BUF_SIZE); > +} > + > +static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp) > +{ > + RTL_W32(tp, TX_DESC_START_ADDR_HIGH, ((u64)tp->tx_phy_addr) >> 32); > + RTL_W32(tp, TX_DESC_START_ADDR_LOW, > + ((u64)tp->tx_phy_addr) & DMA_BIT_MASK(32)); > + RTL_W32(tp, RX_DESC_ADDR_HIGH, ((u64)tp->rx_phy_addr) >> 32); > + RTL_W32(tp, RX_DESC_ADDR_LOW, > + ((u64)tp->rx_phy_addr) & DMA_BIT_MASK(32)); > +} > + > +static void rtl_set_rx_mode(struct net_device *dev) > +{ > + u32 rx_mode = ACCEPT_BROADCAST | ACCEPT_MY_PHYS | ACCEPT_MULTICAST; > + /* Multicast hash filter */ > + u32 mc_filter[2] = { 0xffffffff, 0xffffffff }; > + struct rtl8169_private *tp = netdev_priv(dev); > + u32 tmp; > + > + if (!test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status)) > + return; > + > + if (dev->flags & IFF_PROMISC) { > + rx_mode |= ACCEPT_ALL_PHYS; > + } else if (!(dev->flags & IFF_MULTICAST)) { > + rx_mode &= ~ACCEPT_MULTICAST; > + } else if (netdev_mc_count(dev) > MC_FILTER_LIMIT || > + (dev->flags & IFF_ALLMULTI)) { > + /* Too many to filter perfectly -- accept all multicasts. */ > + } else { > + struct netdev_hw_addr *ha; > + > + mc_filter[1] = 0; > + mc_filter[0] = 0; > + netdev_for_each_mc_addr(ha, dev) { > + u32 bit_nr = eth_hw_addr_crc(ha) >> 26; > + > + mc_filter[bit_nr >> 5] |= BIT(bit_nr & 31); > + } > + } > + > + tmp = mc_filter[0]; > + > + mc_filter[0] = swab32(mc_filter[1]); > + mc_filter[1] = swab32(tmp); > + > + RTL_W32(tp, MAR0 + 4, mc_filter[1]); > + RTL_W32(tp, MAR0 + 0, mc_filter[0]); > + > + tmp = RTL_R32(tp, RX_CONFIG); > + RTL_W32(tp, RX_CONFIG, (tmp & ~RX_CONFIG_ACCEPT_OK_MASK) | rx_mode); > +} > + > +static void rtl_led_set(struct rtl8169_private *tp) > +{ > + RTL_W32(tp, LEDSEL, tp->chip->led_cfg); > +} > + > +static void rtl_hw_start(struct rtl8169_private *tp) > +{ > + u32 phy_status; > + > + rtl_unlock_config_regs(tp); > + > + rtl_set_rx_max_size(tp); > + > + tp->cp_cmd |= RTL_R16(tp, C_PLUS_CMD) | PKT_CNTR_DISABLE | INTT_3; > + > + RTL_W16(tp, C_PLUS_CMD, tp->cp_cmd); > + > + RTL_W16(tp, INTR_MITIGATE, 0x5151); > + > + rtl_set_rx_tx_desc_registers(tp); > + > + rtl_set_tx_config_registers(tp); > + > + if (tp->chip->features & RTL_FEATURE_TX_NO_CLOSE) { > + /* enable tx no close mode */ > + rtl_ocp_write(tp, OCP_TCR_0, > + rtl_ocp_read(tp, OCP_TCR_0) | > + (SEL_PAGE_FLAG | TX_CLOSE_DISABLE)); > + } > + > + /* TX FIFO threshold */ > + rtl_ocp_write(tp, OCP_TFIFOFULL_TH_0, 0x0006); > + rtl_ocp_write(tp, OCP_TFIFOFULL_TH_1, 0x0010); > + > + /* RX FIFO threshold */ > + rtl_ocp_write(tp, OCP_RFIFOFULL_TH_0, 0x0002); > + rtl_ocp_write(tp, OCP_RFIFOFULL_TH_1, 0x0008); > + > + if (tp->chip->features & RTL_FEATURE_ADJUST_FIFO) { > + phy_status = rtl_ocp_read(tp, OCP_FPGA_GPHY0); > + if ((phy_status & GPHY0_SPEED_MASK) == GPHY0_SPEED_1000) { > + /* 1000 Mbps */ > + rtl_ocp_write(tp, OCP_RFIFONFULL_TH_0, 0x0088); > + rtl_ocp_write(tp, OCP_RFIFOEMPTY_TH_0, 0x00a8); > + } else { > + /* 10/100 Mbps */ > + rtl_ocp_write(tp, OCP_RFIFONFULL_TH_0, 0x0038); > + rtl_ocp_write(tp, OCP_RFIFOEMPTY_TH_0, 0x0048); > + } > + } else { > + rtl_ocp_write(tp, OCP_RFIFONFULL_TH_0, 0x0038); > + rtl_ocp_write(tp, OCP_RFIFOEMPTY_TH_0, 0x0048); > + } > + > + RTL_W8(tp, CONFIG5, RTL_R8(tp, CONFIG5) & ~LAN_WAKE); > + RTL_W8(tp, CONFIG3, RTL_R8(tp, CONFIG3) & ~MAGIC_PKT); > + > + RTL_W32(tp, TX_CONFIG, RTL_R32(tp, TX_CONFIG) | TXCFG_AUTO_FIFO); > + > + rtl_reset_packet_filter(tp); > + rtl_eri_write(tp, ERI_PLA_CKCTRL_0, ERIAR_MASK_0011, 0x1d8f); > + > + rtl_disable_rxdvgate(tp); > + > + RTL_W8(tp, MAX_TX_PACKET_SIZE, EARLY_SIZE); > + > + rtl_eri_write(tp, ERI_VMQ_CONTROL, ERIAR_MASK_0011, 0x0000); > + rtl_eri_write(tp, ERI_RSS_CONTROL_0, ERIAR_MASK_0011, 0x0000); > + > + rtl_w0w1_eri(tp, ERI_MISC_2, 0x01, 0x06); > + rtl_eri_clear_bits(tp, ERI_EEE_CR, ERI_PAUSE_TX_EN); > + > + /* disable aspm and clock request before access ephy */ > + RTL_W8(tp, CONFIG2, RTL_R8(tp, CONFIG2) & ~CLKREQ_EN_LOCK); > + RTL_W8(tp, CONFIG5, RTL_R8(tp, CONFIG5) & ~ASPM_EN); > + > + rtl_lock_config_regs(tp); > + > + RTL_W8(tp, CHIP_CMD, CMD_TX_ENB | CMD_RX_ENB); > + > + netif_addr_lock_bh(tp->dev); > + rtl_set_rx_config_features(tp, tp->dev->features); > + rtl_set_rx_mode(tp->dev); > + netif_addr_unlock_bh(tp->dev); > + > + RTL_W16(tp, MULTI_INTR, RTL_R16(tp, MULTI_INTR) & 0xf000); > + > + rtl_led_set(tp); > + > + rtl_irq_enable(tp); > +} > + > +static int rtl8169_change_mtu(struct net_device *dev, int new_mtu) > +{ > + WRITE_ONCE(dev->mtu, new_mtu); > + netdev_update_features(dev); > + > + return 0; > +} > + > +static void rtl8169_mark_to_asic(struct rx_desc *desc) > +{ > + u32 eor = le32_to_cpu(desc->opts1) & RING_END; > + > + desc->opts2 = 0; > + /* Force memory writes to complete before releasing descriptor */ > + dma_wmb(); > + WRITE_ONCE(desc->opts1, cpu_to_le32(DESC_OWN | eor | RX_BUF_SIZE)); > +} > + > +static struct page *rtl8169_alloc_rx_data(struct rtl8169_private *tp, > + struct rx_desc *desc) > +{ > + struct device *d = tp_to_dev(tp); > + int node = dev_to_node(d); > + dma_addr_t mapping; > + struct page *data; > + > + data = alloc_pages_node(node, GFP_KERNEL, get_order(RX_BUF_SIZE)); > + if (!data) > + return NULL; > + > + mapping = dma_map_page(d, data, 0, RX_BUF_SIZE, DMA_FROM_DEVICE); > + if (unlikely(dma_mapping_error(d, mapping))) { > + if (net_ratelimit()) > + netdev_err(tp->dev, "Failed to map RX DMA!\n"); > + __free_pages(data, get_order(RX_BUF_SIZE)); > + return NULL; > + } > + > + desc->addr = cpu_to_le64(mapping); > + rtl8169_mark_to_asic(desc); > + > + return data; > +} > + > +static void rtl8169_rx_clear(struct rtl8169_private *tp) > +{ > + int i; > + > + for (i = 0; i < NUM_RX_DESC && tp->rx_databuff[i]; i++) { > + dma_unmap_page(tp_to_dev(tp), > + le64_to_cpu(tp->rx_desc_array[i].addr), > + RX_BUF_SIZE, DMA_FROM_DEVICE); > + __free_pages(tp->rx_databuff[i], get_order(RX_BUF_SIZE)); > + tp->rx_databuff[i] = NULL; > + tp->rx_desc_array[i].addr = 0; > + tp->rx_desc_array[i].opts1 = 0; > + } > +} > + > +static int rtl8169_rx_fill(struct rtl8169_private *tp) > +{ > + int i; > + > + for (i = 0; i < NUM_RX_DESC; i++) { > + struct page *data; > + > + data = rtl8169_alloc_rx_data(tp, tp->rx_desc_array + i); > + if (!data) { > + rtl8169_rx_clear(tp); > + return -ENOMEM; > + } > + tp->rx_databuff[i] = data; > + } > + > + /* mark as last descriptor in the ring */ > + tp->rx_desc_array[NUM_RX_DESC - 1].opts1 |= cpu_to_le32(RING_END); > + > + return 0; > +} > + > +static int rtl8169_init_ring(struct rtl8169_private *tp) > +{ > + rtl8169_init_ring_indexes(tp); > + > + memset(tp->tx_skb, 0x0, sizeof(tp->tx_skb)); > + memset(tp->rx_databuff, 0x0, sizeof(tp->rx_databuff)); > + > + return rtl8169_rx_fill(tp); > +} > + > +static void rtl8169_unmap_tx_skb(struct rtl8169_private *tp, unsigned int entry) > +{ > + struct tx_desc *desc = tp->tx_desc_array + entry; > + struct ring_info *tx_skb = tp->tx_skb + entry; > + > + dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr), tx_skb->len, > + DMA_TO_DEVICE); > + > + memset(desc, 0, sizeof(*desc)); > + memset(tx_skb, 0, sizeof(*tx_skb)); > +} > + > +static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start, > + unsigned int n) > +{ > + unsigned int i; > + > + for (i = 0; i < n; i++) { > + unsigned int entry = (start + i) % NUM_TX_DESC; > + struct ring_info *tx_skb = tp->tx_skb + entry; > + unsigned int len = tx_skb->len; > + > + if (len) { > + struct sk_buff *skb = tx_skb->skb; > + > + rtl8169_unmap_tx_skb(tp, entry); > + if (skb) { > + tp->dev->stats.tx_dropped++; > + dev_kfree_skb_any(skb); > + } > + } > + } > +} > + > +static void rtl8169_tx_clear(struct rtl8169_private *tp) > +{ > + rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC); > + netdev_reset_queue(tp->dev); > + tp->cur_tx = 0; > + tp->dirty_tx = 0; > +} > + > +static void rtl8169_cleanup(struct rtl8169_private *tp) > +{ > + napi_disable(&tp->napi); > + > + /* Give a racing hard_start_xmit a few cycles to complete. */ > + synchronize_net(); > + > + rtl8169_irq_mask_and_ack(tp); > + > + rtl_rx_close(tp); > + > + rtl_enable_rxdvgate(tp); > + /* Wait for MAC DMA to settle before reset */ > + fsleep(2000); > + > + rtl_hw_reset(tp); > + > + rtl8169_tx_clear(tp); > + rtl8169_init_ring_indexes(tp); > + > + if (tp->chip->features & RTL_FEATURE_TX_NO_CLOSE) { > + /* disable tx no close mode */ > + rtl_ocp_write(tp, OCP_TCR_0, > + rtl_ocp_read(tp, OCP_TCR_0) & > + ~(SEL_PAGE_FLAG | TX_CLOSE_DISABLE)); > + } > +} > + > +static void rtl_reset_work(struct rtl8169_private *tp) > +{ > + struct net_device *dev = tp->dev; > + int i; > + > + netif_stop_queue(dev); > + > + rtl8169_cleanup(tp); > + > + for (i = 0; i < NUM_RX_DESC; i++) > + rtl8169_mark_to_asic(tp->rx_desc_array + i); > + > + napi_enable(&tp->napi); > + rtl_hw_start(tp); > + > + netif_wake_queue(dev); > +} > + > +static void rtl8169_tx_timeout(struct net_device *dev, unsigned int txqueue) > +{ > + struct rtl8169_private *tp = netdev_priv(dev); > + > + netdev_warn(dev, "TX timeout, resetting MAC\n"); > + rtl_schedule_task(tp, RTL_FLAG_TASK_TX_TIMEOUT); > +} > + > +static int rtl8169_tx_map(struct rtl8169_private *tp, const u32 *opts, u32 len, > + void *addr, unsigned int entry, bool desc_own) > +{ > + struct tx_desc *txd = tp->tx_desc_array + entry; > + struct device *d = tp_to_dev(tp); > + dma_addr_t mapping; > + u32 opts1; > + int ret; > + > + mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE); > + ret = dma_mapping_error(d, mapping); > + if (unlikely(ret)) { > + if (net_ratelimit()) > + netdev_err(tp->dev, "Failed to map TX data!\n"); > + return ret; > + } > + > + txd->addr = cpu_to_le64(mapping); > + txd->opts2 = cpu_to_le32(opts[1]); > + > + opts1 = opts[0] | len; > + if (entry == NUM_TX_DESC - 1) > + opts1 |= RING_END; > + if (desc_own) > + opts1 |= DESC_OWN; > + txd->opts1 = cpu_to_le32(opts1); > + > + tp->tx_skb[entry].len = len; > + > + return 0; > +} > + > +static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb, > + const u32 *opts, unsigned int entry) > +{ > + struct skb_shared_info *info = skb_shinfo(skb); > + unsigned int cur_frag; > + > + for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) { > + const skb_frag_t *frag = info->frags + cur_frag; > + void *addr = skb_frag_address(frag); > + u32 len = skb_frag_size(frag); > + > + entry = (entry + 1) % NUM_TX_DESC; > + > + if (unlikely(rtl8169_tx_map(tp, opts, len, addr, entry, true))) > + goto err_out; > + } > + > + return 0; > + > +err_out: > + rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag); > + return -EIO; > +} > + > +static bool rtl8169_tso_csum(struct sk_buff *skb, u32 *opts) > +{ > + u32 mss = skb_shinfo(skb)->gso_size; > + > + if (mss) { > + /* SoC GMAC LSO descriptor uses TD_LSO in opts[0] and MSS in opts[1] */ > + opts[0] |= TD_LSO; > + opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT; > + } else if (skb->ip_summed == CHECKSUM_PARTIAL) { > + const struct iphdr *ip; > + > + if (vlan_get_protocol(skb) != htons(ETH_P_IP)) > + return !skb_checksum_help(skb); > + > + ip = ip_hdr(skb); > + if (ip->protocol == IPPROTO_TCP) > + opts[1] |= TD1_IP_CS | TD1_TCP_CS; > + else if (ip->protocol == IPPROTO_UDP) > + opts[1] |= TD1_IP_CS | TD1_UDP_CS; > + else > + return !skb_checksum_help(skb); > + } > + > + return true; > +} > + > +static unsigned int rtl_tx_slots_avail(struct rtl8169_private *tp) > +{ > + return READ_ONCE(tp->dirty_tx) + NUM_TX_DESC - READ_ONCE(tp->cur_tx); > +} > + > +static void rtl8169_doorbell(struct rtl8169_private *tp) > +{ > + RTL_W8(tp, TX_POLL, NPQ); > +} > + > +static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb, > + struct net_device *dev) > +{ > + unsigned int frags = skb_shinfo(skb)->nr_frags; [Severity: High] frags is sampled before rtl8169_tso_csum(), but rtl8169_tso_csum() can fall back to skb_checksum_help(). That calls __skb_linearize() when skb_has_shared_frag() is true, which pulls all fragments into the linear area and sets nr_frags to 0. As far as I can tell, this is reachable. The driver advertises NETIF_F_IP_CSUM, and skb_csum_hwoffload_help() only checks csum_offset. So an IPv4 CHECKSUM_PARTIAL skb whose protocol is neither TCP nor UDP, for example one built through a virtio-net or AF_PACKET vnet header, ends up in this fallback if it carries shared frags. After linearization, rtl8169_tx_map() maps the whole packet into the first slot. rtl8169_xmit_frags() then maps nothing because it iterates over the new nr_frags, but entry is still advanced by the old frags value. LAST_FRAG and tx_skb[entry].skb end up on a zeroed slot that was never mapped, and the first descriptor never gets LAST_FRAG. Both cur_tx and TX_DESC_TAIL_IDX still move forward by frags + 1. That looks like it would stall TX and make rtl_tx() call dma_unmap_single() on never-mapped slots in TX_NO_CLOSE mode. It would also leak the skb in rtl8169_tx_clear_range(), because tx_skb[entry].len is 0 for the slot that holds it. Can frags be read after rtl8169_tso_csum() returns instead? > + struct rtl8169_private *tp = netdev_priv(dev); > + unsigned int entry = tp->cur_tx % NUM_TX_DESC; > + struct tx_desc *txd_first, *txd_last; > + bool stop_queue, door_bell; > + u16 close_idx; > + u16 tail_idx; > + u32 opts[2]; > + > + if (unlikely(rtl_tx_slots_avail(tp) < R8169_TX_STOP_THRS)) { > + if (net_ratelimit()) > + netdev_err(dev, "BUG! Tx Ring full when queue awake!\n"); > + goto err_stop_0; > + } > + > + txd_first = tp->tx_desc_array + entry; > + > + if (tp->chip->features & RTL_FEATURE_TX_NO_CLOSE) { > + close_idx = RTL_R16(tp, TX_DESC_CLOSE_IDX) & TX_DESC_CNT_MASK; > + tail_idx = READ_ONCE(tp->cur_tx) & TX_DESC_CNT_MASK; > + if ((tail_idx > close_idx && > + (tail_idx - close_idx == NUM_TX_DESC)) || > + (tail_idx < close_idx && > + (TX_DESC_CNT_SIZE - close_idx + tail_idx == NUM_TX_DESC))) > + goto err_stop_0; > + } else { > + if (unlikely(le32_to_cpu(txd_first->opts1) & DESC_OWN)) > + goto err_stop_0; > + } > + > + opts[1] = rtl8169_tx_vlan_tag(skb); > + opts[0] = 0; > + > + if (unlikely(!rtl8169_tso_csum(skb, opts))) > + goto err_dma_0; > + > + if (unlikely(rtl8169_tx_map(tp, opts, skb_headlen(skb), skb->data, > + entry, false))) > + goto err_dma_0; > + > + if (frags) { > + if (rtl8169_xmit_frags(tp, skb, opts, entry)) > + goto err_dma_1; > + entry = (entry + frags) % NUM_TX_DESC; > + } > + > + txd_last = tp->tx_desc_array + entry; > + txd_last->opts1 |= cpu_to_le32(LAST_FRAG); > + tp->tx_skb[entry].skb = skb; > + > + skb_tx_timestamp(skb); > + > + /* Force memory writes to complete before releasing descriptor */ > + dma_wmb(); > + > + door_bell = __netdev_sent_queue(dev, skb->len, netdev_xmit_more()); > + > + txd_first->opts1 |= cpu_to_le32(DESC_OWN | FIRST_FRAG); > + > + /* rtl_tx needs to see descriptor changes before updated tp->cur_tx */ > + smp_wmb(); > + > + WRITE_ONCE(tp->cur_tx, tp->cur_tx + frags + 1); > + > + if (tp->chip->features & RTL_FEATURE_TX_NO_CLOSE) > + RTL_W16(tp, TX_DESC_TAIL_IDX, > + READ_ONCE(tp->cur_tx) & TX_DESC_CNT_MASK); > + > + stop_queue = !netif_subqueue_maybe_stop(dev, 0, rtl_tx_slots_avail(tp), > + R8169_TX_STOP_THRS, > + R8169_TX_START_THRS); > + if (door_bell || stop_queue) > + rtl8169_doorbell(tp); > + > + return NETDEV_TX_OK; > + > +err_dma_1: > + rtl8169_unmap_tx_skb(tp, entry); > +err_dma_0: > + dev_kfree_skb_any(skb); > + dev->stats.tx_dropped++; > + return NETDEV_TX_OK; > + > +err_stop_0: > + netif_stop_queue(dev); > + return NETDEV_TX_BUSY; > +} > + > +static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp, > + int budget) > +{ > + unsigned int dirty_tx, tx_left, bytes_compl = 0, pkts_compl = 0; > + struct sk_buff *skb = NULL; > + u16 dirty_tx_idx; > + u16 close_idx; > + > + dirty_tx = tp->dirty_tx; > + if (tp->chip->features & RTL_FEATURE_TX_NO_CLOSE) { > + close_idx = RTL_R16(tp, TX_DESC_CLOSE_IDX) & TX_DESC_CNT_MASK; > + dirty_tx_idx = dirty_tx & TX_DESC_CNT_MASK; > + smp_rmb(); /* make sure dirty_tx and close_idx are updated */ > + > + if (dirty_tx_idx <= close_idx) > + tx_left = close_idx - dirty_tx_idx; > + else > + tx_left = close_idx + TX_DESC_CNT_SIZE - dirty_tx_idx; > + } else { > + smp_rmb(); /* make sure dirty_tx is updated */ > + tx_left = READ_ONCE(tp->cur_tx) - dirty_tx; > + } > + > + while (tx_left > 0) { > + unsigned int entry = dirty_tx % NUM_TX_DESC; > + u32 status; > + > + status = le32_to_cpu(READ_ONCE(tp->tx_desc_array[entry].opts1)); > + if (!(tp->chip->features & RTL_FEATURE_TX_NO_CLOSE) && > + (status & DESC_OWN)) > + break; > + > + skb = tp->tx_skb[entry].skb; > + rtl8169_unmap_tx_skb(tp, entry); > + > + if (skb) { > + pkts_compl++; > + bytes_compl += skb->len; > + napi_consume_skb(skb, budget); > + } > + dirty_tx++; > + tx_left--; > + } > + > + if (tp->dirty_tx != dirty_tx) { > + WRITE_ONCE(tp->dirty_tx, dirty_tx); > + > + netif_subqueue_completed_wake(dev, 0, pkts_compl, bytes_compl, > + rtl_tx_slots_avail(tp), > + R8169_TX_START_THRS); > + > + dev_sw_netstats_tx_add(dev, pkts_compl, bytes_compl); > + > + /* Kick an extra TX_POLL request when a burst of start_xmit > + * activity is detected. > + */ > + if (READ_ONCE(tp->cur_tx) != dirty_tx && skb) > + rtl8169_doorbell(tp); > + } > +} > + > +static int rtl8169_fragmented_frame(u32 status) > +{ > + return (status & (FIRST_FRAG | LAST_FRAG)) != (FIRST_FRAG | LAST_FRAG); > +} > + > +static void rtl8169_rx_csum(struct net_device *dev, struct sk_buff *skb, > + u32 opts1) > +{ > + u32 status = opts1 & (RX_PROTO_MASK | RX_CS_FAIL_MASK); > + > + if ((dev->features & NETIF_F_RXCSUM) && > + (status == RX_PROTO_TCP || status == RX_PROTO_UDP)) > + skb->ip_summed = CHECKSUM_UNNECESSARY; > + else > + skb_checksum_none_assert(skb); > +} > + > +static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, > + int budget) > +{ > + struct device *d = tp_to_dev(tp); > + int count; > + > + for (count = 0; count < budget; count++, tp->cur_rx++) { > + unsigned int pkt_size, entry = tp->cur_rx % NUM_RX_DESC; > + struct rx_desc *desc = tp->rx_desc_array + entry; > + struct sk_buff *skb; > + const void *rx_buf; > + dma_addr_t addr; > + u32 status; > + > + status = le32_to_cpu(READ_ONCE(desc->opts1)); > + if (status & DESC_OWN) > + break; > + > + /* This barrier is needed to keep us from reading > + * any other fields out of the Rx descriptor until > + * we know the status of DescOwn > + */ > + dma_rmb(); > + > + if (unlikely(status & RX_RES)) { > + if (net_ratelimit()) > + netdev_warn(dev, "Rx ERROR. status = %08x\n", > + status); > + dev->stats.rx_errors++; > + if (status & (RX_RWT | RX_RUNT)) > + dev->stats.rx_length_errors++; > + if (status & RX_CRC) > + dev->stats.rx_crc_errors++; > + if (!(dev->features & NETIF_F_RXALL)) > + goto release_descriptor; > + else if ((status & RX_RWT) || > + !(status & (RX_RUNT | RX_CRC))) > + goto release_descriptor; > + } > + > + pkt_size = status & GENMASK(13, 0); > + if (unlikely(pkt_size < ETH_HLEN + ETH_FCS_LEN)) { > + dev->stats.rx_dropped++; > + dev->stats.rx_length_errors++; > + goto release_descriptor; > + } > + > + if (likely(!(dev->features & NETIF_F_RXFCS))) > + pkt_size -= ETH_FCS_LEN; > + > + if (unlikely(pkt_size > RX_BUF_SIZE)) { > + dev->stats.rx_dropped++; > + dev->stats.rx_length_errors++; > + goto release_descriptor; > + } > + > + /* The driver does not support incoming fragmented frames. > + * They are seen as a symptom of over-mtu sized frames. > + */ > + if (unlikely(rtl8169_fragmented_frame(status))) { > + dev->stats.rx_dropped++; > + dev->stats.rx_length_errors++; > + goto release_descriptor; > + } > + > + skb = napi_alloc_skb(&tp->napi, pkt_size); > + if (unlikely(!skb)) { > + dev->stats.rx_dropped++; > + goto release_descriptor; > + } > + > + addr = le64_to_cpu(desc->addr); > + rx_buf = page_address(tp->rx_databuff[entry]); > + > + dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE); > + prefetch(rx_buf); > + skb_copy_to_linear_data(skb, rx_buf, pkt_size); > + skb_put(skb, pkt_size); > + dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE); > + > + rtl8169_rx_csum(dev, skb, status); > + skb->protocol = eth_type_trans(skb, dev); > + > + rtl8169_rx_vlan_tag(desc, skb); > + > + if (skb->pkt_type == PACKET_MULTICAST) > + dev->stats.multicast++; > + > + napi_gro_receive(&tp->napi, skb); > + > + dev_sw_netstats_rx_add(dev, pkt_size); > +release_descriptor: > + rtl8169_mark_to_asic(desc); > + } > + > + return count; > +} > + > +static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance) > +{ > + struct rtl8169_private *tp = dev_instance; > + u16 status; > + > + if (unlikely(!test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status))) > + return IRQ_NONE; > + > + status = rtl_get_events(tp); > + if (status == 0xffff || !(status & tp->irq_mask)) > + return IRQ_NONE; > + > + if (status & LINK_CHG) > + phy_mac_interrupt(tp->phydev); > + > + rtl_irq_disable(tp); > + napi_schedule(&tp->napi); > + > + return IRQ_HANDLED; > +} > + > +static void rtl_mac_reinit(struct rtl8169_private *tp); > + > +/* Workqueue context. */ > +static void rtl_task(struct work_struct *work) > +{ > + static const struct { > + void (*action)(struct rtl8169_private *tp); > + int bitnr; > + } rtl_work[] = { > + { rtl_reset_work, RTL_FLAG_TASK_RESET_PENDING }, > + { rtl_mac_reinit, RTL_FLAG_TASK_TX_TIMEOUT }, > + }; > + struct rtl8169_private *tp = > + container_of(work, struct rtl8169_private, wk.work); > + struct net_device *dev = tp->dev; > + int i; > + > + rtl_lock_work(tp); > + > + if (!netif_running(dev) || > + !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags)) > + goto out_unlock; > + > + for (i = 0; i < ARRAY_SIZE(rtl_work); i++) { > + bool pending; > + > + pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags); > + if (pending) > + rtl_work[i].action(tp); > + } > + > +out_unlock: > + rtl_unlock_work(tp); > +} > + > +static int rtl8169_poll(struct napi_struct *napi, int budget) > +{ > + struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, > + napi); > + struct net_device *dev = tp->dev; > + u16 status = rtl_get_events(tp); > + int work_done = 0; > + > + rtl_ack_events(tp, status & ~RX_OVERFLOW); [Severity: High] r8169_mdio_register() configures the PHY with PHY_MAC_INTERRUPT, so phylib does not poll it. It only learns about link changes through phy_mac_interrupt(). rtl8169_interrupt() calls phy_mac_interrupt() only when LINK_CHG is set in the INTR_STATUS value it read. It then masks interrupts and schedules NAPI without acking anything. rtl8169_poll() reads INTR_STATUS again and acks everything except RX_OVERFLOW, but never looks at LINK_CHG. Suppose the interrupt was raised by RX_OK or TX_OK, and the link changes after the ISR read INTR_STATUS. LINK_CHG then latches while interrupts are masked. The same happens between polls when NAPI keeps repolling with work_done == budget. rtl8169_poll() then clears LINK_CHG and nobody calls phy_mac_interrupt(). Doesn't that lose the link event for good, leaving the carrier state and r8169soc_adjust_link() stuck until the next link transition? r8169_main.c avoids this by acking exactly the status bits it handled in rtl8169_interrupt() and not acking anything in its poll routine. Should this driver do the same, or at least call phy_mac_interrupt() from rtl8169_poll() when it sees LINK_CHG? > + > + rtl_tx(dev, tp, budget); > + > + /* always check rx queue */ > + work_done = rtl_rx(dev, tp, budget); > + > + if ((status & RX_OVERFLOW) && work_done > 0) [Severity: High] RX_OVERFLOW is part of tp->irq_mask, and the binding example uses a level-triggered interrupt. Here it is only acked when two things hold: it was already set in the status read at the start of rtl8169_poll(), and rtl_rx() returned some work. If RX_OVERFLOW latches after that read, the bit stays set when napi_complete_done() and rtl_irq_enable() unmask it again. The same happens if a previous poll already drained the ring, so this one returns 0. rtl8169_interrupt() then fires immediately, returns IRQ_HANDLED and schedules NAPI. The next rtl8169_poll() again finds no completed descriptors, so work_done is 0 and RX_OVERFLOW is again not acked. Doesn't that turn into an interrupt and NAPI livelock that pins a CPU? RX would also stall for good if the MAC holds off reception until RX_OVERFLOW is cleared. A remote peer can trigger the overflow just by sending a burst of traffic. What is the work_done > 0 condition meant to protect against? Would acking RX_OVERFLOW whenever the ring has been drained, that is whenever work_done < budget, be correct for this MAC? > + rtl_ack_events(tp, RX_OVERFLOW); > + > + if (work_done < budget && napi_complete_done(napi, work_done)) > + rtl_irq_enable(tp); > + > + return work_done; > +} > + > +static void r8169soc_adjust_link(struct net_device *ndev) > +{ > + struct rtl8169_private *tp = netdev_priv(ndev); > + > + if (netif_carrier_ok(ndev)) { > + /* prevent ALDPS enter MAC Powercut Tx/Rx disable */ > + /* use MAC reset to set counter to offset 0 */ > + rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING); > + } > + > + phy_print_status(tp->phydev); > +} > + > +static int r8169_phy_connect(struct rtl8169_private *tp) > +{ > + struct phy_device *phydev = tp->phydev; > + phy_interface_t phy_mode; > + int ret; > + > + phy_mode = tp->chip->features & RTL_FEATURE_GMII ? > + PHY_INTERFACE_MODE_GMII : PHY_INTERFACE_MODE_MII; > + > + ret = phy_connect_direct(tp->dev, phydev, r8169soc_adjust_link, > + phy_mode); > + if (ret) { > + netdev_err(tp->dev, "failed to connect PHY: %d\n", ret); > + return ret; > + } > + > + if (!(tp->chip->features & RTL_FEATURE_GMII)) > + phy_set_max_speed(phydev, SPEED_100); > + > + phy_attached_info(phydev); > + > + return 0; > +} > + > +static void rtl8169_down(struct rtl8169_private *tp) > +{ > + if (test_and_set_bit(RTL_STATUS_DOWN, tp->status)) > + return; > + > + phy_stop(tp->phydev); > + > + /* Clear all task flags after stopping PHY state machine */ > + bitmap_zero(tp->wk.flags, RTL_FLAG_MAX); > + > + rtl8169_cleanup(tp); > +} > + > +static void rtl8169_up(struct rtl8169_private *tp) > +{ > + phy_init_hw(tp->phydev); > + phy_resume(tp->phydev); > + if (test_and_clear_bit(RTL_STATUS_DOWN, tp->status)) > + rtl8169_init_phy(tp); > + napi_enable(&tp->napi); > + set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags); > + rtl_reset_work(tp); > + > + phy_start(tp->phydev); > +} > + > +static int __rtl8169_close(struct net_device *dev) > +{ > + struct rtl8169_private *tp = netdev_priv(dev); > + struct platform_device *pdev = tp->pdev; > + > + netif_stop_queue(dev); > + > + rtl8169_down(tp); > + > + rtl8169_rx_clear(tp); > + > + cancel_work(&tp->wk.work); > + > + free_irq(dev->irq, tp); > + > + phy_disconnect(tp->phydev); > + > + dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, > + tp->rx_desc_array, tp->rx_phy_addr); > + dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, > + tp->tx_desc_array, tp->tx_phy_addr); > + tp->tx_desc_array = NULL; > + tp->rx_desc_array = NULL; > + > + return 0; > +} > + > +static int rtl8169_close(struct net_device *dev) > +{ > + struct rtl8169_private *tp = netdev_priv(dev); > + int ret; > + > + rtl_lock_work(tp); > + ret = __rtl8169_close(dev); > + rtl_unlock_work(tp); > + return ret; > +} > + > +#ifdef CONFIG_NET_POLL_CONTROLLER > +static void rtl8169_netpoll(struct net_device *dev) > +{ > + struct rtl8169_private *tp = netdev_priv(dev); > + > + rtl8169_interrupt(dev->irq, tp); > +} > +#endif > + > +static void rtl_hw_initialize(struct rtl8169_private *tp) > +{ > + u32 data; > + > + rtl_enable_rxdvgate(tp); > + > + RTL_W8(tp, CHIP_CMD, > + RTL_R8(tp, CHIP_CMD) & ~(CMD_TX_ENB | CMD_RX_ENB)); > + fsleep(1000); > + RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB); > + > + data = rtl_ocp_read(tp, OCP_SFF_STS_7); > + data &= ~SFF_MCU_BORW_EN; > + rtl_ocp_write(tp, OCP_SFF_STS_7, data); > + > + rtl_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42); > + > + data = rtl_ocp_read(tp, OCP_SFF_STS_7); > + data |= SFF_RE_INIT_LL; > + rtl_ocp_write(tp, OCP_SFF_STS_7, data); > + > + rtl_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42); > +} > + > +static int __rtl_reinit_mac_phy(struct rtl8169_private *tp) > +{ > + struct device *d = tp_to_dev(tp); > + int ret; > + > + ret = tp->chip->pll_clock_init(tp); > + if (ret) { > + dev_dbg(d, "failed to initialize PLL clock: %d\n", ret); > + return ret; > + } > + > + ret = tp->chip->mdio_init(tp); > + if (ret) { > + dev_dbg(d, "failed to initialize MDIO: %d\n", ret); > + return ret; > + } > + > + /* Enable ALDPS */ > + ret = rtl_phy_modify(tp, PHY_PAGE_A43, PHY_PHYCR1_REG, 0, > + PHYCR1_ALDPS_EN); > + if (ret) { > + dev_dbg(d, "failed to enable ALDPS: %d\n", ret); > + return ret; > + } > + > + rtl_init_rxcfg(tp); > + > + rtl8169_irq_mask_and_ack(tp); > + > + rtl_hw_initialize(tp); > + > + rtl_hw_reset(tp); > + > + rtl_unlock_config_regs(tp); > + RTL_W8(tp, CONFIG1, RTL_R8(tp, CONFIG1) | PM_ENABLE); > + /* Clear CONFIG5[7:1] wakeup bits and keep bit 0 */ > + RTL_W8(tp, CONFIG5, RTL_R8(tp, CONFIG5) & ASPM_EN); > + > + /* disable magic packet WOL */ > + RTL_W8(tp, CONFIG3, RTL_R8(tp, CONFIG3) & ~MAGIC_PKT); > + rtl_lock_config_regs(tp); > + > + if (test_bit(RTL_STATUS_REINIT, tp->status)) > + __rtl_rar_set(tp, tp->mac_addr); > + > + rtl_led_set(tp); > + > + return 0; > +} > + > +static int rtl_reinit_mac_phy(struct rtl8169_private *tp) > +{ > + int ret; > + > + phy_lock_mdio_bus(tp->phydev); > + set_bit(RTL_STATUS_REINIT, tp->status); > + > + ret = tp->chip->reset_phy_gmac(tp); > + if (ret) > + dev_dbg(tp_to_dev(tp), "failed to reset PHY/GMAC: %d\n", ret); > + else > + ret = __rtl_reinit_mac_phy(tp); > + > + clear_bit(RTL_STATUS_REINIT, tp->status); > + phy_unlock_mdio_bus(tp->phydev); > + return ret; > +} > + > +static int rtl_open(struct net_device *dev) > +{ > + struct rtl8169_private *tp = netdev_priv(dev); > + struct platform_device *pdev = tp->pdev; > + int retval = -ENOMEM; > + > + /* Rx and Tx descriptors needs 256 bytes alignment. > + * dma_alloc_coherent provides more. > + */ > + tp->tx_desc_array = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES, > + &tp->tx_phy_addr, GFP_KERNEL); > + if (!tp->tx_desc_array) > + goto out; > + > + tp->rx_desc_array = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES, > + &tp->rx_phy_addr, GFP_KERNEL); > + if (!tp->rx_desc_array) > + goto err_free_tx_0; > + > + retval = rtl8169_init_ring(tp); > + if (retval < 0) { > + dev_dbg(&pdev->dev, "failed to initialize RX ring: %d\n", > + retval); > + goto err_free_rx_1; > + } > + > + rtl_lock_work(tp); > + > + if (!test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status)) { > + retval = rtl_reinit_mac_phy(tp); > + if (retval) > + goto err_free_rx_2; > + } > + > + smp_mb(); /* make sure TX/RX rings are ready */ > + > + retval = request_irq(dev->irq, rtl8169_interrupt, 0, dev->name, tp); > + if (retval < 0) { > + netdev_err(dev, "failed to request IRQ %d: %d\n", > + dev->irq, retval); > + goto err_free_rx_2; > + } > + > + retval = r8169_phy_connect(tp); > + if (retval) > + goto err_free_irq; > + > + rtl8169_up(tp); > + > + rtl_unlock_work(tp); > + > + netif_start_queue(dev); > + > +out: > + return retval; > + > +err_free_irq: > + free_irq(dev->irq, tp); > +err_free_rx_2: > + rtl_unlock_work(tp); > + rtl8169_rx_clear(tp); > +err_free_rx_1: > + dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, > + tp->rx_desc_array, tp->rx_phy_addr); > + tp->rx_desc_array = NULL; > +err_free_tx_0: > + dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, > + tp->tx_desc_array, tp->tx_phy_addr); > + tp->tx_desc_array = NULL; > + goto out; > +} > + > +static void rtl8169_get_stats64(struct net_device *dev, > + struct rtnl_link_stats64 *stats) > +{ > + netdev_stats_to_stats64(stats, &dev->stats); > + dev_fetch_sw_netstats(stats, dev->tstats); > +} > + > +static void rtl_mac_reinit(struct rtl8169_private *tp) > +{ > + int ret; > + > + netif_stop_queue(tp->dev); > + rtl8169_down(tp); > + > + ret = rtl_reinit_mac_phy(tp); > + if (ret) { > + netdev_err(tp->dev, "hardware re-initialization failed: %d\n", > + ret); > + netif_carrier_off(tp->dev); > + return; > + } > + > + rtl8169_up(tp); > + netif_wake_queue(tp->dev); > +} > + > +static void rtl_shutdown(struct platform_device *pdev) > +{ > + struct rtl8169_private *tp = platform_get_drvdata(pdev); > + > + rtl_lock_work(tp); > + netif_device_detach(tp->dev); [Severity: Medium] rtl_shutdown() only takes wk.mutex, while ndo_open, ndo_stop and ndo_set_mac_address are serialized by rtnl_lock. rtl_open() also does the ring allocation and rtl8169_init_ring() before it takes wk.mutex, and never rechecks netif_device_present(). Can this sequence happen? CPU0 CPU1 __dev_open() netif_device_present() passes set __LINK_STATE_START rtl_open() allocates rings rtl_shutdown() rtl_lock_work() netif_device_detach() netif_running() is true rtl8169_down() /* returns, DOWN still set */ rtl_unlock_work() rtl_lock_work() request_irq() rtl8169_up() The MAC would then be left with DMA, the IRQ, NAPI and the PHY running after shutdown has returned. That is a problem for kexec in particular. A concurrent rtl_set_mac_address() can likewise overwrite the permanent address that rtl_shutdown() just restored. r8169_main.c takes rtnl_lock() in its rtl_shutdown(). Should this driver do the same around the wk.mutex section? > + if (netif_running(tp->dev)) > + rtl8169_down(tp); > + > + /* Restore permanent MAC address */ > + if (test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status) && > + is_valid_ether_addr(tp->dev->perm_addr)) > + __rtl_rar_set(tp, tp->dev->perm_addr); > + rtl_unlock_work(tp); > +} > + > +static void rtl_hw_uninit(void *data) > +{ > + struct rtl8169_private *tp = data; > + > + if (test_and_clear_bit(RTL_STATUS_CLK_ETN_250M_EN, tp->status)) > + clk_disable_unprepare(tp->clk_etn_250m); > + if (test_and_clear_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status)) > + clk_disable_unprepare(tp->clk_etn_sys); > + reset_control_assert(tp->rstc_gmac); > + reset_control_assert(tp->rstc_gphy); > + if (test_and_clear_bit(RTL_STATUS_CLK_MISC_EN, tp->status)) > + clk_disable_unprepare(tp->clk_misc); > + reset_control_assert(tp->rstc_misc); > +} > + > +static void rtl_remove_one(struct platform_device *pdev) > +{ > + struct rtl8169_private *tp = platform_get_drvdata(pdev); > + > + unregister_netdev(tp->dev); > + > + cancel_work_sync(&tp->wk.work); > + > + netif_napi_del(&tp->napi); > + > + /* restore permanent MAC address */ > + if (test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status) && > + is_valid_ether_addr(tp->dev->perm_addr)) > + __rtl_rar_set(tp, tp->dev->perm_addr); > +} > + > +static void rtl8169soc_get_drvinfo(struct net_device *dev, > + struct ethtool_drvinfo *info) > +{ > + strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); > +} > + > +static const struct ethtool_ops rtl8169soc_ethtool_ops = { > + .get_drvinfo = rtl8169soc_get_drvinfo, > + .nway_reset = phy_ethtool_nway_reset, > + .get_link = ethtool_op_get_link, > + .get_ts_info = ethtool_op_get_ts_info, > + .get_link_ksettings = phy_ethtool_get_link_ksettings, > + .set_link_ksettings = phy_ethtool_set_link_ksettings, > +}; > + > +static const struct net_device_ops rtl_netdev_ops = { > + .ndo_open = rtl_open, > + .ndo_stop = rtl8169_close, > + .ndo_get_stats64 = rtl8169_get_stats64, > + .ndo_start_xmit = rtl8169_start_xmit, > + .ndo_tx_timeout = rtl8169_tx_timeout, > + .ndo_validate_addr = eth_validate_addr, > + .ndo_change_mtu = rtl8169_change_mtu, > + .ndo_fix_features = rtl8169_fix_features, > + .ndo_set_features = rtl8169_set_features, > + .ndo_set_mac_address = rtl_set_mac_address, > + .ndo_eth_ioctl = phy_do_ioctl_running, > + .ndo_set_rx_mode = rtl_set_rx_mode, > +#ifdef CONFIG_NET_POLL_CONTROLLER > + .ndo_poll_controller = rtl8169_netpoll, > +#endif > +}; > + > +static void rtl_set_irq_mask(struct rtl8169_private *tp) > +{ > + tp->irq_mask = RX_OK | RX_ERR | RX_OVERFLOW | TX_OK | TX_ERR | > + LINK_CHG; > +} > + > +static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg) > +{ > + struct rtl8169_private *tp = mii_bus->priv; > + > + if (phyaddr > 0) > + return -ENODEV; > + > + return rtl_phy_read(tp, CURRENT_MDIO_PAGE, phyreg); > +} > + > +static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr, > + int phyreg, u16 val) > +{ > + struct rtl8169_private *tp = mii_bus->priv; > + > + if (phyaddr > 0) > + return -ENODEV; > + > + return rtl_phy_write(tp, CURRENT_MDIO_PAGE, phyreg, val); > +} > + > +static int r8169_mdio_register(struct rtl8169_private *tp) > +{ > + struct platform_device *pdev = tp->pdev; > + struct mii_bus *new_bus; > + int ret; > + > + new_bus = devm_mdiobus_alloc(&pdev->dev); > + if (!new_bus) > + return -ENOMEM; > + > + new_bus->name = KBUILD_MODNAME; > + new_bus->priv = tp; > + new_bus->parent = &pdev->dev; > + new_bus->irq[0] = PHY_MAC_INTERRUPT; > + new_bus->phy_mask = GENMASK(31, 1); > + snprintf(new_bus->id, MII_BUS_ID_SIZE, "%s-0", KBUILD_MODNAME); > + > + new_bus->read = r8169_mdio_read_reg; > + new_bus->write = r8169_mdio_write_reg; > + > + ret = devm_mdiobus_register(&pdev->dev, new_bus); > + if (ret) > + return dev_err_probe(&pdev->dev, ret, > + "failed to register MDIO bus\n"); > + > + tp->phydev = mdiobus_get_phy(new_bus, 0); > + if (!tp->phydev) > + return dev_err_probe(&pdev->dev, -ENODEV, "no phydev found\n"); > + > + if (!tp->phydev->drv) > + return dev_err_probe(&pdev->dev, -EUNATCH, > + "no dedicated PHY driver found for PHY ID 0x%08x\n", > + tp->phydev->phy_id); > + > + /* PHY will be woken up in rtl_open() */ > + phy_suspend(tp->phydev); > + set_bit(RTL_STATUS_DOWN, tp->status); > + > + return 0; > +} > + > +static int dummy_reset_phy_gmac(struct rtl8169_private *tp) > +{ > + return 0; > +} > + > +static int dummy_pll_clock_init(struct rtl8169_private *tp) > +{ > + return 0; > +} > + > +static int dummy_mdio_init(struct rtl8169_private *tp) > +{ > + return 0; > +} > + > +static void dummy_hw_phy_config(struct rtl8169_private *tp) > +{ > +} > + > +/* RTD1625 */ > +static void rtd1625_mdio_lock(struct rtl8169_private *tp) > +{ > + u32 wait_cnt = 0; > + u32 log_de4e; > + > + /* disable EEE IMR */ > + rtl_ocp_write(tp, OCP_EEE_IMR, > + rtl_ocp_read(tp, OCP_EEE_IMR) & ~EEE_IMR_MASK); > + /* disable timer 2 */ > + rtl_ocp_write(tp, OCP_TIMER2_CTRL, > + rtl_ocp_read(tp, OCP_TIMER2_CTRL) | TIMER2_DISABLE); > + /* wait MDIO channel is free */ > + log_de4e = rtl_ocp_read(tp, OCP_MDIO_NON_IDLE) & MDIO_NON_IDLE_FLAG; > + log_de4e = (log_de4e << 1) | > + (rtl_ocp_read(tp, OCP_MDIO_NON_IDLE) & MDIO_NON_IDLE_FLAG); > + /* check if 0 for continuous 2 times */ > + while (log_de4e & GENMASK(1, 0)) { > + wait_cnt++; > + udelay(1); > + log_de4e = (log_de4e << 1) | > + (rtl_ocp_read(tp, OCP_MDIO_NON_IDLE) & > + MDIO_NON_IDLE_FLAG); > + if (wait_cnt > MDIO_WAIT_TIMEOUT) > + break; > + } > + /* enter driver mode */ > + rtl_ocp_write(tp, OCP_FPGA_GPHY1, > + rtl_ocp_read(tp, OCP_FPGA_GPHY1) | GPHY1_DRV_MODE); > + if (wait_cnt > MDIO_WAIT_TIMEOUT) > + dev_err(tp_to_dev(tp), "MDIO lock timeout\n"); > +} > + > +static void rtd1625_mdio_unlock(struct rtl8169_private *tp) > +{ > + /* exit driver mode */ > + rtl_ocp_write(tp, OCP_FPGA_GPHY1, > + rtl_ocp_read(tp, OCP_FPGA_GPHY1) & ~GPHY1_DRV_MODE); > + /* enable timer 2 */ > + rtl_ocp_write(tp, OCP_TIMER2_CTRL, > + rtl_ocp_read(tp, OCP_TIMER2_CTRL) & ~TIMER2_DISABLE); > + /* enable EEE IMR */ > + rtl_ocp_write(tp, OCP_EEE_IMR, > + rtl_ocp_read(tp, OCP_EEE_IMR) | EEE_IMR_MASK); > +} > + > +static int rtd1625_reset_phy_gmac(struct rtl8169_private *tp) > +{ > + struct device *d = tp_to_dev(tp); > + bool clk_en; > + int ret; > + > + if (!test_bit(RTL_STATUS_REINIT, tp->status)) { > + if (!test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status)) { > + ret = clk_prepare_enable(tp->clk_etn_sys); > + if (ret) { > + dev_dbg(d, "failed to enable etn_sys clk: %d\n", > + ret); > + return ret; > + } > + set_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status); > + } > + > + if (!test_bit(RTL_STATUS_CLK_ETN_250M_EN, tp->status)) { > + ret = clk_prepare_enable(tp->clk_etn_250m); > + if (ret) { > + dev_dbg(d, "failed to enable etn_250m clk: %d\n", > + ret); > + return ret; > + } > + set_bit(RTL_STATUS_CLK_ETN_250M_EN, tp->status); > + } > + > + ret = reset_control_deassert(tp->rstc_misc); > + if (ret) { > + dev_dbg(d, "failed to deassert misc reset: %d\n", ret); > + return ret; > + } > + > + if (!test_bit(RTL_STATUS_CLK_MISC_EN, tp->status)) { > + ret = clk_prepare_enable(tp->clk_misc); > + if (ret) { > + dev_dbg(d, "failed to enable misc clk: %d\n", > + ret); > + return ret; > + } > + set_bit(RTL_STATUS_CLK_MISC_EN, tp->status); > + } > + } else { > + ret = reset_control_deassert(tp->rstc_misc); > + if (ret) { > + dev_dbg(d, "failed to deassert misc reset: %d\n", ret); > + return ret; > + } > + } > + > + ret = reset_control_deassert(tp->rstc_gphy); > + if (ret) { > + dev_dbg(d, "failed to deassert gphy reset: %d\n", ret); > + return ret; > + } > + > + /* Disable DBUS clock gating */ > + ret = regmap_update_bits_base(tp->m2tmx_base, > + RTD1625_M2TMX_ETN_DBUS_CTRL, > + BIT(0), 0, NULL, false, true); > + if (ret) { > + dev_dbg(d, "failed to disable DBUS clock gating: %d\n", ret); > + return ret; > + } > + > + /* Serialize against rtl_set_rx_mode() and ISR before disabling MAC clock */ > + netif_addr_lock_bh(tp->dev); > + clk_en = test_and_clear_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status); > + netif_addr_unlock_bh(tp->dev); > + if (clk_en) { > + synchronize_irq(tp->dev->irq); > + clk_disable_unprepare(tp->clk_etn_sys); > + } > + if (test_and_clear_bit(RTL_STATUS_CLK_ETN_250M_EN, tp->status)) > + clk_disable_unprepare(tp->clk_etn_250m); > + > + ret = reset_control_assert(tp->rstc_gphy); > + if (ret) { > + dev_dbg(d, "failed to assert gphy reset: %d\n", ret); > + return ret; > + } > + > + ret = reset_control_assert(tp->rstc_gmac); > + if (ret) { > + dev_dbg(d, "failed to assert gmac reset: %d\n", ret); > + return ret; > + } > + > + /* Enable bypass mode */ > + ret = regmap_update_bits_base(tp->iso_base, ISO_ETN_TESTIO, > + BIT(1), BIT(1), NULL, false, true); > + if (ret) { > + dev_dbg(d, "failed to enable bypass mode: %d\n", ret); > + return ret; > + } > + > + /* Set embedded PHY address to 0 */ > + ret = regmap_write(tp->m2tmx_base, RTD1625_M2TMX_PWRCUT_ETN, > + RTD1625_PWRCUT_ETN_DEFAULT); > + if (ret) { > + dev_dbg(d, "failed to set PWRCUT_ETN: %d\n", ret); > + return ret; > + } > + > + fsleep(1000); > + return 0; > +} > + > +static int rtd1625_pll_clock_init(struct rtl8169_private *tp) > +{ > + struct device *d = tp_to_dev(tp); > + u32 val = 0; > + u32 tmp; > + int ret; > + > + /* Clear ETN PHY interrupt for bypass mode */ > + ret = regmap_write(tp->iso_base, ISO_UMSK_ISR, BIT(27)); > + if (ret) { > + dev_dbg(d, "failed to clear ETN PHY interrupt: %d\n", ret); > + return ret; > + } > + > + ret = reset_control_deassert(tp->rstc_gphy); > + if (ret) { > + dev_dbg(d, "failed to deassert gphy reset: %d\n", ret); > + return ret; > + } > + > + fsleep(1000); /* wait 1ms for PHY PLL stable */ > + > + /* Enable bypass mode */ > + ret = regmap_update_bits_base(tp->iso_base, ISO_ETN_TESTIO, > + BIT(1), BIT(1), NULL, false, true); > + if (ret) { > + dev_dbg(d, "failed to enable bypass mode: %d\n", ret); > + return ret; > + } > + > + ret = reset_control_deassert(tp->rstc_gmac); > + if (ret) { > + dev_dbg(d, "failed to deassert gmac reset: %d\n", ret); > + return ret; > + } > + > + if (!test_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status)) { > + ret = clk_prepare_enable(tp->clk_etn_sys); > + if (ret) { > + dev_dbg(d, "failed to enable etn_sys clk: %d\n", ret); > + return ret; > + } > + set_bit(RTL_STATUS_CLK_ETN_SYS_EN, tp->status); > + } > + > + if (!test_bit(RTL_STATUS_CLK_ETN_250M_EN, tp->status)) { > + ret = clk_prepare_enable(tp->clk_etn_250m); > + if (ret) { > + dev_dbg(d, "failed to enable etn_250m clk: %d\n", ret); > + return ret; > + } > + set_bit(RTL_STATUS_CLK_ETN_250M_EN, tp->status); > + } > + > + /* Bit 0 of ISO_PLL_WDOUT indicates init_autoload_done (ETN RBUS valid) */ > + ret = regmap_read(tp->iso_base, ISO_PLL_WDOUT, &val); > + if (ret) { > + dev_dbg(d, "failed to read ISO_PLL_WDOUT: %d\n", ret); > + return ret; > + } > + > + tmp = 0; > + while ((val & BIT(0)) != 0x1) { > + tmp += 1; > + fsleep(1000); > + if (tmp >= MAC_INIT_TIMEOUT) { > + dev_err(d, "GMAC init timeout\n"); > + return -ETIMEDOUT; > + } > + ret = regmap_read(tp->iso_base, ISO_PLL_WDOUT, &val); > + if (ret) { > + dev_dbg(d, "failed to read ISO_PLL_WDOUT: %d\n", ret); > + return ret; > + } > + } > + dev_dbg(d, "GMAC wait %d ms for init_autoload_done\n", tmp); > + return 0; > +} > + > +static int rtd1625_mdio_init(struct rtl8169_private *tp) > +{ > + struct device *d = tp_to_dev(tp); > + u32 reg; > + int ret; > + int val; > + u32 i; > + > + fsleep(10000); /* wait for MDIO ready */ > + > + /* PHY will stay in state 1 mode */ > + for (i = 0; i < MDIO_INIT_TIMEOUT; i++) { > + val = int_mdio_read(tp, PHY_PAGE_A42, PHY_STATUS_A42_REG); > + if (val < 0) { > + dev_dbg(d, "failed to read PHY bypass status: %d\n", > + val); > + return val; > + } > + if ((val & PHY_STATUS_A42_MASK) == PHY_STATUS_BYPASS) > + break; > + fsleep(1000); > + } > + if (i >= MDIO_INIT_TIMEOUT) { > + dev_err(d, "PHY status is not 0x1 in bypass mode, current = 0x%02x\n", > + val & PHY_STATUS_A42_MASK); > + return -ETIMEDOUT; > + } > + > + /* ETN spec. set MDC freq = 8.9MHZ */ > + reg = rtl_ocp_read(tp, OCP_GPHY_MDCMDIO_CFG) & ~MDCMDIO_PERIOD_MASK; > + reg |= MDCMDIO_PERIOD_8_9MHZ; > + rtl_ocp_write(tp, OCP_GPHY_MDCMDIO_CFG, reg); > + reg = rtl_ocp_read(tp, OCP_EPHY_MDCMDIO_CFG) & ~MDCMDIO_PERIOD_MASK; > + reg |= MDCMDIO_PERIOD_8_9MHZ; > + rtl_ocp_write(tp, OCP_EPHY_MDCMDIO_CFG, reg); > + > + /* fill fuse_rdy & rg_ext_ini_done */ > + val = int_mdio_read(tp, PHY_PAGE_A46, PHY_PHYSCR_REG); > + if (val < 0) { > + dev_dbg(d, "failed to read PHY_PHYSCR_REG: %d\n", val); > + return val; > + } > + ret = int_mdio_write(tp, PHY_PAGE_A46, PHY_PHYSCR_REG, > + val | PHYSCR_CFG_DONE); > + if (ret) { > + dev_dbg(d, "failed to write PHY_PHYSCR_REG: %d\n", ret); > + return ret; > + } > + > + /* init_autoload_done = 1 */ > + reg = rtl_ocp_read(tp, OCP_BOOT_CTRL_REG); > + reg |= INIT_AUTOLOAD_DONE; > + rtl_ocp_write(tp, OCP_BOOT_CTRL_REG, reg); > + > + /* Set ee_mode = 3 to unlock config registers */ > + rtl_unlock_config_regs(tp); > + > + /* wait LAN-ON */ > + for (i = 0; i < MDIO_INIT_TIMEOUT; i++) { > + fsleep(1000); > + val = int_mdio_read(tp, PHY_PAGE_A42, PHY_STATUS_A42_REG); > + if (val < 0) { > + rtl_lock_config_regs(tp); > + dev_dbg(d, "failed to read PHY LAN-ON status: %d\n", > + val); > + return val; > + } > + if ((val & PHY_STATUS_A42_MASK) == PHY_STATUS_LAN_ON) > + break; > + } > + if (i >= MDIO_INIT_TIMEOUT) { > + dev_err(d, "PHY status is not 0x3, current = 0x%02x\n", > + val & PHY_STATUS_A42_MASK); > + rtl_lock_config_regs(tp); > + return -ETIMEDOUT; > + } > + rtl_lock_config_regs(tp); > + dev_dbg(d, "wait %d ms for PHY ready, current = 0x%x\n", i + 1, val); > + > + /* ISO spec, set data path to FEPHY */ > + reg = rtl_ocp_read(tp, OCP_RMII_REG); > + reg &= ~RMII_ENABLE; > + rtl_ocp_write(tp, OCP_RMII_REG, reg); > + > + reg = rtl_ocp_read(tp, OCP_RGMII_REG); > + reg &= ~(MII_ENABLE | RGMII_ENABLE); > + reg |= MII_ENABLE; /* FEPHY */ > + rtl_ocp_write(tp, OCP_RGMII_REG, reg); > + > + return 0; > +} > + > +static const struct r8169soc_chip_info rtd1625_info = { > + .name = "RTD1625", > + .mdio_lock = rtd1625_mdio_lock, > + .mdio_unlock = rtd1625_mdio_unlock, > + .reset_phy_gmac = rtd1625_reset_phy_gmac, > + .pll_clock_init = rtd1625_pll_clock_init, > + .mdio_init = rtd1625_mdio_init, > + .jumbo_max = JUMBO_1K, > + .jumbo_tx_csum = false, > + .led_cfg = 0x170000a9, > + .features = RTL_FEATURE_TX_NO_CLOSE | > + RTL_FEATURE_ADJUST_FIFO | > + RTL_FEATURE_OCP_MDIO, > +}; > + > +static const struct of_device_id r8169soc_dt_ids[] = { > + { > + .compatible = "realtek,rtd1625-r8169soc", > + .data = &rtd1625_info, > + }, > + { /* sentinel */ } > +}; > +MODULE_DEVICE_TABLE(of, r8169soc_dt_ids); > + > +static void rtk_chip_info_check(struct r8169soc_chip_info *chip) > +{ > + if (!chip->mdio_lock) > + chip->mdio_lock = dummy_mdio_lock; > + if (!chip->mdio_unlock) > + chip->mdio_unlock = dummy_mdio_unlock; > + if (!chip->reset_phy_gmac) > + chip->reset_phy_gmac = dummy_reset_phy_gmac; > + if (!chip->pll_clock_init) > + chip->pll_clock_init = dummy_pll_clock_init; > + if (!chip->mdio_init) > + chip->mdio_init = dummy_mdio_init; > + if (!chip->hw_phy_config) > + chip->hw_phy_config = dummy_hw_phy_config; > +} > + > +static void rtl_init_mac_address(struct rtl8169_private *tp) > +{ > + /* Default placeholder MAC in DTB when factory MAC is not programmed > + * by the bootloader; treat it as unconfigured and use a random MAC. > + */ > + static const u8 default_mac[ETH_ALEN] = { > + 0x00, 0x10, 0x20, 0x30, 0x40, 0x50 > + }; > + struct net_device *dev = tp->dev; > + u8 mac_addr[ETH_ALEN] = {}; > + int rc; > + > + rc = eth_platform_get_mac_address(tp_to_dev(tp), mac_addr); > + if (!rc && is_valid_ether_addr(mac_addr) && > + !ether_addr_equal(mac_addr, default_mac)) { > + ether_addr_copy(dev->perm_addr, mac_addr); > + goto done; > + } > + > + eth_random_addr(mac_addr); > + dev->addr_assign_type = NET_ADDR_RANDOM; > + dev_warn(tp_to_dev(tp), "using random MAC address %pM\n", mac_addr); > +done: > + eth_hw_addr_set(dev, mac_addr); > + ether_addr_copy(tp->mac_addr, mac_addr); > + __rtl_rar_set(tp, mac_addr); > +} > + > +static int rtl_init_clks_and_resets(struct rtl8169_private *tp) > +{ > + struct device *dev = &tp->pdev->dev; > + > + tp->clk_etn_sys = devm_clk_get(dev, "etn_sys"); > + if (IS_ERR(tp->clk_etn_sys)) > + return dev_err_probe(dev, PTR_ERR(tp->clk_etn_sys), > + "failed to get etn_sys clk\n"); > + > + tp->clk_etn_250m = devm_clk_get(dev, "etn_250m"); > + if (IS_ERR(tp->clk_etn_250m)) > + return dev_err_probe(dev, PTR_ERR(tp->clk_etn_250m), > + "failed to get etn_250m clk\n"); > + > + tp->clk_misc = devm_clk_get(dev, "misc"); > + if (IS_ERR(tp->clk_misc)) > + return dev_err_probe(dev, PTR_ERR(tp->clk_misc), > + "failed to get misc clk\n"); > + > + tp->rstc_gphy = devm_reset_control_get_exclusive(dev, "gphy"); > + if (IS_ERR(tp->rstc_gphy)) > + return dev_err_probe(dev, PTR_ERR(tp->rstc_gphy), > + "failed to get gphy reset\n"); > + > + tp->rstc_gmac = devm_reset_control_get_exclusive(dev, "gmac"); > + if (IS_ERR(tp->rstc_gmac)) > + return dev_err_probe(dev, PTR_ERR(tp->rstc_gmac), > + "failed to get gmac reset\n"); > + > + tp->rstc_misc = devm_reset_control_get_exclusive(dev, "misc"); > + if (IS_ERR(tp->rstc_misc)) > + return dev_err_probe(dev, PTR_ERR(tp->rstc_misc), > + "failed to get misc reset\n"); > + > + return 0; > +} > + > +static int rtl_init_one(struct platform_device *pdev) > +{ > + const struct r8169soc_chip_info *chip; > + struct rtl8169_private *tp; > + struct net_device *ndev; > + void __iomem *ioaddr; > + int irq; > + int rc; > + > + chip = of_device_get_match_data(&pdev->dev); > + if (!chip) > + return dev_err_probe(&pdev->dev, -EINVAL, > + "no proper chip matched\n"); > + > + irq = platform_get_irq(pdev, 0); > + if (irq < 0) > + return irq; > + > + rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); > + if (rc) > + return dev_err_probe(&pdev->dev, rc, "failed to set DMA mask\n"); > + > + ioaddr = devm_platform_ioremap_resource(pdev, 0); > + if (IS_ERR(ioaddr)) > + return PTR_ERR(ioaddr); > + > + ndev = devm_alloc_etherdev(&pdev->dev, sizeof(*tp)); > + if (!ndev) > + return -ENOMEM; > + > + SET_NETDEV_DEV(ndev, &pdev->dev); > + ndev->irq = irq; > + ndev->netdev_ops = &rtl_netdev_ops; > + ndev->ethtool_ops = &rtl8169soc_ethtool_ops; > + tp = netdev_priv(ndev); > + tp->dev = ndev; > + tp->pdev = pdev; > + tp->chip_info = *chip; > + tp->chip = &tp->chip_info; > + rtk_chip_info_check(tp->chip); > + tp->mmio_addr = ioaddr; > + > + ndev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS; > + > + tp->iso_base = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, > + "realtek,iso"); > + if (IS_ERR(tp->iso_base)) > + return dev_err_probe(&pdev->dev, PTR_ERR(tp->iso_base), > + "Fail to get iso_base address\n"); > + > + tp->m2tmx_base = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, > + "realtek,m2tmx"); > + if (IS_ERR(tp->m2tmx_base)) > + return dev_err_probe(&pdev->dev, PTR_ERR(tp->m2tmx_base), > + "Fail to get m2tmx_base address\n"); > + > + rc = rtl_init_clks_and_resets(tp); > + if (rc) > + return rc; > + > + rc = devm_add_action_or_reset(&pdev->dev, rtl_hw_uninit, tp); > + if (rc) > + return rc; > + > + raw_spin_lock_init(&tp->mac_ocp_lock); > + > + rtl_init_mdio_ops(tp); > + rc = tp->chip->reset_phy_gmac(tp); > + if (rc) > + return dev_err_probe(&pdev->dev, rc, > + "failed to reset PHY/GMAC\n"); > + > + rc = __rtl_reinit_mac_phy(tp); > + if (rc) > + return dev_err_probe(&pdev->dev, rc, > + "failed to initialize MAC/PHY\n"); > + > + mutex_init(&tp->wk.mutex); > + > + INIT_WORK(&tp->wk.work, rtl_task); > + > + rtl_init_mac_address(tp); > + > + rtl_set_irq_mask(tp); > + > + rc = r8169_mdio_register(tp); > + if (rc) > + return rc; > + > + ndev->watchdog_timeo = RTL8169_TX_TIMEOUT; > + > + netif_napi_add(ndev, &tp->napi, rtl8169_poll); > + > + ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO | > + NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX | > + NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_GRO_UDP_FWD; > + > + ndev->features |= ndev->hw_features; > + > + ndev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO; > + ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE; > + > + netif_set_tso_max_size(ndev, RTL_GSO_MAX_SIZE_V1); > + netif_set_tso_max_segs(ndev, RTL_GSO_MAX_SEGS_V1); > + > + ndev->hw_features |= NETIF_F_RXALL; > + ndev->hw_features |= NETIF_F_RXFCS; > + > + netdev_sw_irq_coalesce_default_on(ndev); > + > + /* configure chip for default features */ > + __rtl8169_set_features(tp, ndev->features); > + > + ndev->min_mtu = ETH_ZLEN; > + ndev->max_mtu = tp->chip->jumbo_max; > + > + platform_set_drvdata(pdev, tp); > + > + netif_carrier_off(ndev); > + > + rc = register_netdev(ndev); > + if (rc < 0) { > + netif_napi_del(&tp->napi); > + return dev_err_probe(&pdev->dev, rc, > + "failed to register netdev\n"); > + } > + > + netdev_info(ndev, "%s, %pM, IRQ %d\n", > + tp->chip->name, ndev->dev_addr, ndev->irq); > + > + return 0; > +} > + > +static struct platform_driver rtl8169_soc_driver = { > + .probe = rtl_init_one, > + .remove = rtl_remove_one, > + .shutdown = rtl_shutdown, > + .driver = { > + .name = KBUILD_MODNAME, > + .of_match_table = r8169soc_dt_ids, > + }, > +}; > +module_platform_driver(rtl8169_soc_driver); [Severity: High] rtl_reset_work() stops the TX queue with netif_stop_queue() and only then calls rtl8169_cleanup(), which is where NAPI gets disabled. rtl_mac_reinit() has the same ordering, and the window is wider there because rtl8169_down() first sleeps in phy_stop() doing MDIO accesses. While NAPI is still running, rtl8169_poll()->rtl_tx() calls netif_subqueue_completed_wake(). Once completions free up R8169_TX_START_THRS slots, it wakes the queue again and clears the DRV_XOFF state that was just set. Can this sequence happen? CPU0 (rtl_task) CPU1 rtl_reset_work() netif_stop_queue() rtl8169_poll()->rtl_tx() netif_subqueue_completed_wake() /* queue awake again */ rtl8169_cleanup() napi_disable() synchronize_net() rtl8169_start_xmit() rtl_hw_reset() rtl8169_tx_clear() rtl8169_start_xmit() would then fill descriptors and tx_skb entries while rtl8169_tx_clear() is unmapping and freeing them and resetting cur_tx and dirty_tx. That looks like it can lead to double DMA unmaps, double frees of skbs and a corrupted ring. In the rtl_mac_reinit() case, xmit would also do MMIO on TX_DESC_CLOSE_IDX, TX_DESC_TAIL_IDX and TX_POLL after rtd1625_reset_phy_gmac() has gated etn_sys and asserted rstc_gmac. If rtl_reinit_mac_phy() then fails, the queue stays awake on a MAC that is held in reset. Would it be better to disable NAPI before stopping the queue, or to call netif_tx_disable() after napi_disable() in rtl8169_cleanup()? -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204100746.1165612-1-ericwang%40realtek.com ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-05 10:32 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-12-04 10:07 [PATCH net-next 0/2] net: ethernet: realtek: Add r8169soc Ethernet driver for Realtek STB SoCs Eric Wang 2026-12-04 10:07 ` [PATCH net-next 1/2] dt-bindings: net: Add Realtek STB SoC r8169soc Ethernet controller Eric Wang 2026-12-05 10:32 ` netdev-bot+sashiko 2026-12-04 10:07 ` [PATCH net-next 2/2] net: ethernet: realtek: Add r8169soc Ethernet driver for Realtek STB SoCs Eric Wang 2026-12-05 10:32 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox