net: dsa: initial support for MaxLinear MxL862xx switches
Subsystems (6)
Daniel Golle
maintainerv11 cover net: dsa: initial support for MaxLinear MxL862xx switches
This series adds very basic DSA support for the MaxLinear MxL86252 (5x 2500Base-T PHYs) and MxL86282 (8x 2500Base-T PHYs) switches. In addition to the 2.5G TP ports both switches also come with two SerDes interfaces which can be used either to connect external PHYs or SFP cages, or as CPU port when using the switch with this DSA driver.
MxL862xx integrates a firmware running on an embedded processor (based on Zephyr RTOS). Host interaction uses a simple netlink-like API transported over MDIO/MMD.
This series includes only what's needed to pass traffic between user ports and the CPU port: relayed MDIO to internal PHYs, basic port enable/disable, and CPU-port special tagging.
The SerDes interface of the CPU port is automatically configured by the switch after reset using a board-specific configuration stored together with the firmware in the flash chip attached to the switch, so no action is needed from the driver to setup the interface mode of the CPU port.
Also MAC settings of the PHY ports are automatically configured, which means the driver works fine with phylink_mac_ops being all no-op stubs.
Multiple follow up series will bring support for setting up the other SerDes PCS interface (ie. not used for the CPU port), bridge, VLAN, ... offloading, and support for using an 802.1Q-based special tag instead of the proprietary 8-byte tag.
---
basic DSA selftests were run, results:
* no_forwarding.sh: all tests PASS
* bridge_vlan_unaware.sh: all tests PASS
* bridge_vlan_mcast.sh: all tests PASS
* bridge_vlan_aware.sh: all tests PASS
* local_termination.sh: all tests PASS or XFAIL, except for
TEST: VLAN over vlan_filtering=1 bridged port: Unicast IPv4 to unknown MAC address [FAIL]
reception succeeded, but should have failed
TEST: VLAN over vlan_filtering=1 bridged port: Unicast IPv4 to unknown MAC address, allmulti [FAIL]
reception succeeded, but should have failed
As obviously this is mostly testing the Linux software bridge at this point so I didn't bother to run any of the FDB or MDB related tests.
Some mystery about the relationship between the sub-interface ID and CTP port assignment remains for now, so I cannot yet fully explain the magic numbers when calculating the two.
(Why is it neccesary to allocate 23 CTPs when using port 9 as CPU port while there are at most 17 phyiscal interfaces in total, and only at most 13 when using one of the two SerDes as CPU interface? Why is there an offset of 16 when calculating the sub-interface ID? How will a total of 17 ports be possibly represented in a 4-bit field in the tag RX path?)
At this point this doesn't impact functionality, and I hope for further clarification discussing all that with MaxLinear engineers.
I believe the initial driver is anyway in good shape now, a follow-up series addressing configuring the SerDes interfaces will also have to take care of CTP assignment and sub-interfaces to not hit the mentioned 4-bit cap in the tag driver.
Changes since v10:
1/4 dt-bindings: net: dsa: add MaxLinear MxL862xx
* no changes
2/4 net: dsa: add tag format for MxL862xx switches
* arrange local variables in reverse xmas tree order (again)
* use MXL862_NAME as .name in struct dsa_device_ops
3/4 net: mdio: add unlocked mdiodev C45 bus accessors
* no changes
4/4 net: dsa: add basic initial driver for MxL862xx switches
* fix fatal last-second changes dsa_port_is_unused vs. dsa_is_unused_port and dsa_port_is_unused vs. dsa_is_unused_port
* use cpu_to_le16 in mxl862xx_configure_ctp_port
* improve formatting of some kerneldoc comments
Changes since v9:
1/4 dt-bindings: net: dsa: add MaxLinear MxL862xx
* add microcontroller port 0
2/4 net: dsa: add tag format for MxL862xx switches
* count ports starting from 0
3/4 net: mdio: add unlocked mdiodev C45 bus accessors
* no changes
4/4 net: dsa: add basic initial driver for MxL862xx switches
* fix indentation in kernel-doc
* remove misleading comment regarding sub-interface IDs
* remove unused macros, set MXL862XX_MAX_PORTS to 17
* count ports starting from 0, 0 being the microcontroller
* improve port setup:
- split tag protocol and CTP port assignment
- use ports cpu_dp instead of relying on single (ie. first) CPU port
- handle CPU port like any other port
- prepare forward DT compatibility with multiple CPU ports
Changes since v8:
1/4 dt-bindings: net: dsa: add MaxLinear MxL862xx
* no changes
2/4 net: dsa: add tag format for MxL862xx switches
* no changes
3/4 net: mdio: add unlocked mdiodev C45 bus accessors
* no changes
4/4 net: dsa: add basic initial driver for MxL862xx switches
* remove practically unused struct hw_info
* lots of kerneldoc improvements in mxl862xx-api.h
* drop .mac_select_pcs() stub
* better handling for firmware error return value
* apply reverse xmas tree in mxl862xx_api_wrap
* guard headers with #ifdef macro
* include net/dsa.h and linux/mdio.h in mxl862xx.h
* call mxl862xx_port_fast_age() only once in .port_setup
* don't create isolation bridges for unused ports
* replace errornous cast with correct range of values denoting firmware errors
Changes since v7
1/4 dt-bindings: net: dsa: add MaxLinear MxL862xx
* no changes
2/4 net: dsa: add tag format for MxL862xx switches
* no changes
3/4 net: mdio: add unlocked mdiodev C45 bus accessors
* no changes
4/4 net: dsa: add basic initial driver for MxL862xx switches
* use little-endian in bridge_port_config API
* remove duplciate assignment of br_port_cfg.bridge_port_id when setting up CPU port
Changes since v6
1/4 dt-bindings: net: dsa: add MaxLinear MxL862xx
* no changes
2/4 net: dsa: add tag format for MxL862xx switches
* no changes
3/4 net: mdio: add unlocked mdiodev C45 bus accessors
* no changes
4/4 net: dsa: add basic initial driver for MxL862xx switches
* fix kerneldoc style
Changes since RFC v5
1/4 dt-bindings: net: dsa: add MaxLinear MxL862xx
* no changes
2/4 net: dsa: add tag format for MxL862xx switches
* remove unnecessary check for skb != NULL
* merge consecutively printed warnings into single dev_warn_ratelimited
3/4 net: mdio: add unlocked mdiodev C45 bus accessors
* no changes
4/4 net: dsa: add basic initial driver for MxL862xx switches
* include bridge and bridgeport API needed to isolate ports
* remove warning in .setup as ports are now isolated
* make ready-after-reset check more robust by adding delay
* sort structs in order of struct definitions
* best effort to sort functions without introducing additional prototypes
* always use enums with kerneldoc comments in mxl862xx-api.h
* remove bogus .phy_read and .phy_write DSA ops as the driver anyway registers a user MDIO bus with Clause-22 and Clause-45 operations
* various small style fixes
Changes since RFC v4
1/4 dt-bindings: net: dsa: add MaxLinear MxL862xx
* no changes
2/4 net: dsa: add tag format for MxL862xx switches
* drop unused precompiler macros
3/4 net: mdio: add unlocked mdiodev C45 bus accessors
* fix indentation
4/4 net: dsa: add basic initial driver for MxL862xx switches
* output warning in .setup regarding unknown pre-configuration
* add comment explaining why CFGGET is used in reset function
Changes since RFC v3
1/4 dt-bindings: net: dsa: add MaxLinear MxL862xx
* remove labels from example
* remove 'bindings for' from commit title
2/4 net: dsa: add tag format for MxL862xx switches
* describe fields and variables with comments
* sub-interface is only 5 bits
* harmonize Kconfig symbol name
* maintain alphabetic order in Kconfig
* fix typo s/beginnig/beginning/
* fix typo s/swtiches/switches/
* arrange local variables in reverse xmas tree order
3/4 net: mdio: add unlocked mdiodev C45 bus accessors
* unchanged
4/4 net: dsa: add basic initial driver for MxL862xx switches
* poll switch readiness after reset
* implement driver shutdown
* added port_fast_aging API call and driver op
* unified port setup in new .port_setup op
* improve comment explaining special handlign for unaligned API read
* various typos and formatting improvements
Changes since RFC v2
1/4, 2/4, 3/4: unchanged
4/4 net: dsa: add basic initial driver for MxL862xx switches
* fix return value being uninitialized on error in mxl862xx_api_wrap()
* add missing description in kerneldoc comment of
struct mxl862xx_ss_sp_tag
Changes since initial RFC
1/4 dt-bindings: net: dsa: add bindings for MaxLinear MxL862xx
* better description in dt-bindings doc
2/4 net: dsa: add tag formats for MxL862xx switches
* make sure all tag fields are initialized
3/4 net: mdio: add unlocked mdiodev C45 bus accessors
* new patch
4/4 net: dsa: add basic initial driver for MxL862xx switches
* make use of struct mdio_device
* add phylink_mac_ops stubs
* drop leftover nonsense from mxl862xx_phylink_get_caps()
* fix endian conversions
* use __le32 instead of enum types in over-the-wire structs
* use existing MDIO_* macros whenever possible
* simplify API constants to be more readable
* use readx_poll_timeout instead of open-coding poll timeout loop
* add mxl862xx_reg_read() and mxl862xx_reg_write() helpers
* demystify error codes returned by the firmware
* add #defines for mxl862xx_ss_sp_tag member values
* move reset to dedicated function, clarify magic number being the reset command ID
Daniel Golle (4):
dt-bindings: net: dsa: add MaxLinear MxL862xx
net: dsa: add tag format for MxL862xx switches
net: mdio: add unlocked mdiodev C45 bus accessors
net: dsa: add basic initial driver for MxL862xx switches
.../bindings/net/dsa/maxlinear,mxl862xx.yaml | 160 +++++ MAINTAINERS | 8 + drivers/net/dsa/Kconfig | 2 + drivers/net/dsa/Makefile | 1 + drivers/net/dsa/mxl862xx/Kconfig | 12 + drivers/net/dsa/mxl862xx/Makefile | 3 + drivers/net/dsa/mxl862xx/mxl862xx-api.h | 675 ++++++++++++++++++ drivers/net/dsa/mxl862xx/mxl862xx-cmd.h | 49 ++ drivers/net/dsa/mxl862xx/mxl862xx-host.c | 245 +++++++ drivers/net/dsa/mxl862xx/mxl862xx-host.h | 12 + drivers/net/dsa/mxl862xx/mxl862xx.c | 476 ++++++++++++ drivers/net/dsa/mxl862xx/mxl862xx.h | 16 + include/linux/mdio.h | 13 + include/net/dsa.h | 2 + net/dsa/Kconfig | 7 + net/dsa/Makefile | 1 + net/dsa/tag_mxl862xx.c | 110 +++ 17 files changed, 1792 insertions(+) create mode 100644 Documentation/devicetree/bindings/net/dsa/maxlinear,mxl862xx.yaml create mode 100644 drivers/net/dsa/mxl862xx/Kconfig create mode 100644 drivers/net/dsa/mxl862xx/Makefile create mode 100644 drivers/net/dsa/mxl862xx/mxl862xx-api.h create mode 100644 drivers/net/dsa/mxl862xx/mxl862xx-cmd.h create mode 100644 drivers/net/dsa/mxl862xx/mxl862xx-host.c create mode 100644 drivers/net/dsa/mxl862xx/mxl862xx-host.h create mode 100644 drivers/net/dsa/mxl862xx/mxl862xx.c create mode 100644 drivers/net/dsa/mxl862xx/mxl862xx.h create mode 100644 net/dsa/tag_mxl862xx.c
Signature · 1 line
2.52.0
Daniel Golle
maintainerPATCH v11 1/4 dt-bindings: net: dsa: add MaxLinear MxL862xx
Add documentation and an example for MaxLinear MxL86282 and MxL86252 switches.
---
v11: no changes
v10: add microcontroller port 0
v9: no changes
v8: no changes
v7: no changes
v6: no changes
v5: no changes
RFC v4:
* remove labels from example
* remove 'bindings for' from commit title
RFC v3: no changes
RFC v2: better description in dt-bindings doc
--- .../bindings/net/dsa/maxlinear,mxl862xx.yaml | 160 ++++++++++++++++++ MAINTAINERS | 6 + 2 files changed, 166 insertions(+) create mode 100644 Documentation/devicetree/bindings/net/dsa/maxlinear,mxl862xx.yaml
Documentation/devicetree/bindings/net/dsa/maxlinear,mxl862xx.yaml new file +160 -0
@@ -0,0 +1,160 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/net/dsa/maxlinear,mxl862xx.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: MaxLinear MxL862xx Ethernet Switch Family + +maintainers: + - Daniel Golle <[email protected]> + +description: + The MaxLinear MxL862xx switch family are multi-port Ethernet switches with + integrated 2.5GE PHYs. The MxL86252 has five PHY ports and the MxL86282 + has eight PHY ports. Both models come with two 10 Gigabit/s SerDes + interfaces to be used to connect external PHYs or SFP cages, or as CPU + port. + +allOf: + - $ref: dsa.yaml#/$defs/ethernet-ports + +properties: + compatible: + enum: + - maxlinear,mxl86252 + - maxlinear,mxl86282 + + reg: + maxItems: 1 + description: MDIO address of the switch + + mdio: + $ref: /schemas/net/mdio.yaml# + unevaluatedProperties: false + +required: + - compatible + - reg + +unevaluatedProperties: false + +examples: + - | + mdio { + #address-cells = <1>; + #size-cells = <0>; + + switch@0 { + compatible = "maxlinear,mxl86282"; + reg = <0>; + + ethernet-ports { + #address-cells = <1>; + #size-cells = <0>; + + /* Microcontroller port */ + port@0 { + reg = <0>; + status = "disabled"; + }; + + port@1 { + reg = <1>; + phy-handle = <&phy0>; + phy-mode = "internal"; + }; + + port@2 { + reg = <2>; + phy-handle = <&phy1>; + phy-mode = "internal"; + }; + + port@3 { + reg = <3>; + phy-handle = <&phy2>; + phy-mode = "internal"; + }; + + port@4 { + reg = <4>; + phy-handle = <&phy3>; + phy-mode = "internal"; + }; + + port@5 { + reg = <5>; + phy-handle = <&phy4>; + phy-mode = "internal"; + }; + + port@6 { + reg = <6>; + phy-handle = <&phy5>; + phy-mode = "internal"; + }; + + port@7 { + reg = <7>; + phy-handle = <&phy6>; + phy-mode = "internal"; + }; + + port@8 { + reg = <8>; + phy-handle = <&phy7>; + phy-mode = "internal"; + }; + + port@9 { + reg = <9>; + label = "cpu"; + ethernet = <&gmac0>; + phy-mode = "usxgmii"; + + fixed-link { + speed = <10000>; + full-duplex; + }; + }; + }; + + mdio { + #address-cells = <1>; + #size-cells = <0>; + + phy0: ethernet-phy@0 { + reg = <0>; + }; + + phy1: ethernet-phy@1 { + reg = <1>; + }; + + phy2: ethernet-phy@2 { + reg = <2>; + }; + + phy3: ethernet-phy@3 { + reg = <3>; + }; + + phy4: ethernet-phy@4 { + reg = <4>; + }; + + phy5: ethernet-phy@5 { + reg = <5>; + }; + + phy6: ethernet-phy@6 { + reg = <6>; + }; + + phy7: ethernet-phy@7 { + reg = <7>; + }; + }; + }; + };
MAINTAINERS +6 -0
@@ -15619,6 +15619,12 @@ S: Supported F: drivers/net/phy/mxl-86110.c F: drivers/net/phy/mxl-gpy.c +MAXLINEAR MXL862XX SWITCH DRIVER +M: Daniel Golle <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/devicetree/bindings/net/dsa/maxlinear,mxl862xx.yaml + MCAN DEVICE DRIVER M: Markus Schneider-Pargmann <[email protected]> L: [email protected]
Signature · 1 line
2.52.0
Daniel Golle
maintainerPATCH v11 2/4 net: dsa: add tag format for MxL862xx switches
In reply to Daniel GolleAdd proprietary special tag format for the MaxLinear MXL862xx family of switches. While using the same Ethertype as MaxLinear's GSW1xx switches, the actual tag format differs significantly, hence we need a dedicated tag driver for that.
---
v11:
* arrange local variables in reverse xmas tree order (again)
* use MXL862_NAME as .name in struct dsa_device_ops
v10: count ports starting from 0
v9: no changes
v8: no changes
v7: no changes
v6:
* remove unnecessary check for skb != NULL
* merge consecutively printed warnings into single dev_warn_ratelimited
v5:
* remove unused macro definitions
RFC v4:
* describe fields and variables with comments
* sub-interface is only 5 bits
* harmonize Kconfig symbol name
* maintain alphabetic order in Kconfig
* fix typo s/beginnig/beginning/
* fix typo s/swtiches/switches/
* arrange local variables in reverse xmas tree order
RFC v3: no changes
RFC v2: make sure all tag fields are initialized
--- MAINTAINERS | 1 + include/net/dsa.h | 2 + net/dsa/Kconfig | 7 +++ net/dsa/Makefile | 1 + net/dsa/tag_mxl862xx.c | 110 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+) create mode 100644 net/dsa/tag_mxl862xx.c
MAINTAINERS +1 -0
@@ -15624,6 +15624,7 @@ M: Daniel Golle <[email protected]> L: [email protected] S: Maintained F: Documentation/devicetree/bindings/net/dsa/maxlinear,mxl862xx.yaml +F: net/dsa/tag_mxl862xx.c MCAN DEVICE DRIVER M: Markus Schneider-Pargmann <[email protected]>
include/net/dsa.h +2 -0
@@ -57,6 +57,7 @@ struct tc_action;#define DSA_TAG_PROTO_BRCM_LEGACY_FCS_VALUE 29#define DSA_TAG_PROTO_YT921X_VALUE 30#define DSA_TAG_PROTO_MXL_GSW1XX_VALUE 31 +#define DSA_TAG_PROTO_MXL862_VALUE 32enum dsa_tag_protocol {DSA_TAG_PROTO_NONE =DSA_TAG_PROTO_NONE_VALUE ,
@@ -91,6 +92,7 @@ enum dsa_tag_protocol {DSA_TAG_PROTO_VSC73XX_8021Q =DSA_TAG_PROTO_VSC73XX_8021Q_VALUE ,DSA_TAG_PROTO_YT921X =DSA_TAG_PROTO_YT921X_VALUE ,DSA_TAG_PROTO_MXL_GSW1XX =DSA_TAG_PROTO_MXL_GSW1XX_VALUE , + DSA_TAG_PROTO_MXL862 = DSA_TAG_PROTO_MXL862_VALUE, };struct dsa_switch ;
net/dsa/Kconfig +7 -0
@@ -104,6 +104,13 @@ config NET_DSA_TAG_MTK Say Y or M if you want to enable support for tagging frames for Mediatek switches. +config NET_DSA_TAG_MXL_862XX + tristate "Tag driver for MxL862xx switches" + help + Say Y or M if you want to enable support for tagging frames for the + Maxlinear MxL86252 and MxL86282 switches using their native 8-byte + tagging protocol. + config NET_DSA_TAG_MXL_GSW1XX tristate "Tag driver for MaxLinear GSW1xx switches" help
net/dsa/Makefile +1 -0
@@ -28,6 +28,7 @@ obj-$(CONFIG_NET_DSA_TAG_HELLCREEK) += tag_hellcreek.o obj-$(CONFIG_NET_DSA_TAG_KSZ) += tag_ksz.o obj-$(CONFIG_NET_DSA_TAG_LAN9303) += tag_lan9303.o obj-$(CONFIG_NET_DSA_TAG_MTK) += tag_mtk.o +obj-$(CONFIG_NET_DSA_TAG_MXL_862XX) += tag_mxl862xx.o obj-$(CONFIG_NET_DSA_TAG_MXL_GSW1XX) += tag_mxl-gsw1xx.o obj-$(CONFIG_NET_DSA_TAG_NONE) += tag_none.o obj-$(CONFIG_NET_DSA_TAG_OCELOT) += tag_ocelot.o
net/dsa/tag_mxl862xx.c new file +110 -0
110 lines not on this page · raw message
Signature · 1 line
2.52.0
Daniel Golle
maintainerPATCH v11 3/4 net: mdio: add unlocked mdiodev C45 bus accessors
In reply to Daniel GolleAdd helper inline functions __mdiodev_c45_read() and __mdiodev_c45_write(), which are the C45 equivalents of the existing __mdiodev_read() and __mdiodev_write() added by commit e6a45700e7e1 ("net: mdio: add unlocked mdiobus and mdiodev bus accessors")
---
v11: no changes
v10: no changes
v9: no changes
v8: no changes
v7: no changes
v6: no changes
v5: fix indentation
RFC v4: no changes
RFC v3: no changes
RFC v2: add this patch, initial submission
--- include/linux/mdio.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)
include/linux/mdio.h +13 -0
@@ -648,6 +648,19 @@ static inline int mdiodev_modify_changed(struct mdio_device *mdiodev, mask, set); } +static inline int __mdiodev_c45_read (struct mdio_device *mdiodev,int devad, +u16 regnum) +{ +return __mdiobus_c45_read (mdiodev->bus, mdiodev->addr, devad, regnum); +} + +static inline int __mdiodev_c45_write (struct mdio_device *mdiodev,u32 devad, +u16 regnum,u16 val) +{ +return __mdiobus_c45_write (mdiodev->bus, mdiodev->addr, devad, regnum, + val); +} +static inline int mdiodev_c45_modify (struct mdio_device *mdiodev,int devad,u32 regnum,u16 mask,u16 set) {
Signature · 1 line
2.52.0
Re: [PATCH net-next v11 3/4] net: mdio: add unlocked mdiodev C45 bus accessors
Quoted 6 lines · Daniel Golle
Add helper inline functions
__mdiodev_c45_read()and__mdiodev_c45_write(), which are the C45 equivalents of the existing__mdiodev_read()and__mdiodev_write()added by commite6a45700e7e1("net: mdio: add unlocked mdiobus and mdiodev bus accessors")Signed-off-by Daniel Golle <[email protected]>
Thanks!
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
Daniel Golle
maintainerPATCH v11 4/4 net: dsa: add basic initial driver for MxL862xx switches
In reply to Daniel GolleAdd very basic DSA driver for MaxLinear's MxL862xx switches.
In contrast to previous MaxLinear switches the MxL862xx has a built-in processor that runs a sophisticated firmware based on Zephyr RTOS. Interaction between the host and the switch hence is organized using a software API of that firmware rather than accessing hardware registers directly.
Add descriptions of the most basic firmware API calls to access the built-in MDIO bus hosting the 2.5GE PHYs, basic port control as well as setting up the CPU port.
Implement a very basic DSA driver using that API which is sufficient to get packets flowing between the user ports and the CPU port.
The firmware offers all features one would expect from a modern switch hardware, they will be added one by one in follow-up patch series.
---
v11:
* fix fatal last-second changes dsa_port_is_unused vs. dsa_is_unused_port and dsa_port_is_unused vs. dsa_is_unused_port
* use cpu_to_le16 in mxl862xx_configure_ctp_port
* improve formatting of some kerneldoc comments
v10:
* fix indentation in kernel-doc
* remove misleading comment regarding sub-interface IDs
* remove unused macros, set MXL862XX_MAX_PORTS to 17
* count ports starting from 0, 0 being the microcontroller
* improve port setup:
- split tag protocol and CTP port assignment
- use ports cpu_dp instead of relying on single (ie. first) CPU port
- handle CPU port like any other port
- prepare forward DT compatibility with multiple CPU ports
v9:
* remove practically unused struct hw_info
* lots of kerneldoc improvements in mxl862xx-api.h
* drop .mac_select_pcs() stub
* better handling for firmware error return value
* apply reverse xmas tree in mxl862xx_api_wrap
* guard headers with #ifdef macro
* include net/dsa.h and linux/mdio.h in mxl862xx.h
* call mxl862xx_port_fast_age() only once in .port_setup
* don't create isolation bridges for unused ports
* replace errornous cast with correct range of values denoting firmware errors
v8:
* use le32 endian in bridge_port_config API
* remove duplciate assignment of br_port_cfg.bridge_port_id when setting up CPU port
v7:
* fix kerneldoc style
v6:
* include bridge and bridgeport API needed to isolate ports
* remove warning in .setup as ports are now isolated
* make ready-after-reset check more robust by adding delay
* sort structs in order of struct definitions
* best effort to sort functions without introducing additional prototypes
* always use enums with kerneldoc comments in mxl862xx-api.h
* remove bogus .phy_read and .phy_write DSA ops as the driver anyway registers a user MDIO bus with Clause-22 and Clause-45 operations
* various small style fixes
v5:
* output warning in .setup regarding unknown pre-configuration
* add comment explaining why CFGGET is used in reset function
RFC v4:
* poll switch readiness after reset
* implement driver shutdown
* added port_fast_aging API call and driver op
* unified port setup in new .port_setup op
* improve comment explaining special handlign for unaligned API read
* various typos
RFC v3:
* fix return value being uninitialized on error in mxl862xx_api_wrap()
* add missing descrition in kerneldoc comment of
struct mxl862xx_ss_sp_tag
RFC v2:
* make use of struct mdio_device
* add phylink_mac_ops stubs
* drop leftover nonsense from mxl862xx_phylink_get_caps()
* use __le32 instead of enum types in over-the-wire structs
* use existing MDIO_* macros whenever possible
* simplify API constants to be more readable
* use readx_poll_timeout instead of open-coding poll timeout loop
* add mxl862xx_reg_read() and mxl862xx_reg_write() helpers
* demystify error codes returned by the firmware
* add #defines for mxl862xx_ss_sp_tag member values
* move reset to dedicated function, clarify magic number being the reset command ID
--- MAINTAINERS | 1 + drivers/net/dsa/Kconfig | 2 + drivers/net/dsa/Makefile | 1 + drivers/net/dsa/mxl862xx/Kconfig | 12 + drivers/net/dsa/mxl862xx/Makefile | 3 + drivers/net/dsa/mxl862xx/mxl862xx-api.h | 675 +++++++++++++++++++++++ drivers/net/dsa/mxl862xx/mxl862xx-cmd.h | 49 ++ drivers/net/dsa/mxl862xx/mxl862xx-host.c | 245 ++++++++ drivers/net/dsa/mxl862xx/mxl862xx-host.h | 12 + drivers/net/dsa/mxl862xx/mxl862xx.c | 476 ++++++++++++++++ drivers/net/dsa/mxl862xx/mxl862xx.h | 16 + 11 files changed, 1492 insertions(+) create mode 100644 drivers/net/dsa/mxl862xx/Kconfig create mode 100644 drivers/net/dsa/mxl862xx/Makefile create mode 100644 drivers/net/dsa/mxl862xx/mxl862xx-api.h create mode 100644 drivers/net/dsa/mxl862xx/mxl862xx-cmd.h create mode 100644 drivers/net/dsa/mxl862xx/mxl862xx-host.c create mode 100644 drivers/net/dsa/mxl862xx/mxl862xx-host.h create mode 100644 drivers/net/dsa/mxl862xx/mxl862xx.c create mode 100644 drivers/net/dsa/mxl862xx/mxl862xx.h
MAINTAINERS +1 -0
@@ -15624,6 +15624,7 @@ M: Daniel Golle <[email protected]> L: [email protected] S: Maintained F: Documentation/devicetree/bindings/net/dsa/maxlinear,mxl862xx.yaml +F: drivers/net/dsa/mxl862xx/ F: net/dsa/tag_mxl862xx.c MCAN DEVICE DRIVER
drivers/net/dsa/Kconfig +2 -0
@@ -74,6 +74,8 @@ source "drivers/net/dsa/microchip/Kconfig" source "drivers/net/dsa/mv88e6xxx/Kconfig" +source "drivers/net/dsa/mxl862xx/Kconfig" + source "drivers/net/dsa/ocelot/Kconfig" source "drivers/net/dsa/qca/Kconfig"
drivers/net/dsa/Makefile +1 -0
@@ -20,6 +20,7 @@ obj-y += hirschmann/ obj-y += lantiq/ obj-y += microchip/ obj-y += mv88e6xxx/ +obj-y += mxl862xx/ obj-y += ocelot/ obj-y += qca/ obj-y += realtek/
drivers/net/dsa/mxl862xx/Kconfig new file +12 -0
@@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0-only +config NET_DSA_MXL862 + tristate "MaxLinear MxL862xx" + depends on NET_DSA + select MAXLINEAR_GPHY + select NET_DSA_TAG_MXL_862XX + help + This enables support for the MaxLinear MxL862xx switch family. + These switches have two 10GE SerDes interfaces, one typically + used as CPU port. + MxL86282 has eight 2.5 Gigabit PHYs + MxL86252 has five 2.5 Gigabit PHYs
drivers/net/dsa/mxl862xx/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_NET_DSA_MXL862) += mxl862xx_dsa.o +mxl862xx_dsa-y := mxl862xx.o mxl862xx-host.o
drivers/net/dsa/mxl862xx/mxl862xx-api.h new file +675 -0
675 lines not on this page · raw message
drivers/net/dsa/mxl862xx/mxl862xx-cmd.h new file +49 -0
49 lines not on this page · raw message
drivers/net/dsa/mxl862xx/mxl862xx-host.c new file +245 -0
245 lines not on this page · raw message
drivers/net/dsa/mxl862xx/mxl862xx-host.h new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef __MXL862XX_HOST_H +#define __MXL862XX_HOST_H + +#include "mxl862xx.h" + +int mxl862xx_api_wrap (struct mxl862xx_priv *priv,u16 cmd,void *data,u16 size, +bool read,bool quiet); +int mxl862xx_reset (struct mxl862xx_priv *priv); + +#endif /* __MXL862XX_HOST_H */
drivers/net/dsa/mxl862xx/mxl862xx.c new file +476 -0
476 lines not on this page · raw message
drivers/net/dsa/mxl862xx/mxl862xx.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef __MXL862XX_H +#define __MXL862XX_H + +#include <linux/mdio.h> +#include <net/dsa.h> + +#define MXL862XX_MAX_PORTS 17 + +struct mxl862xx_priv { +struct dsa_switch *ds; +struct mdio_device *mdiodev; +}; + +#endif /* __MXL862XX_H */
Signature · 1 line
2.52.0
Jakub Kicinski
maintainerRe: [net-next,v11,4/4] net: dsa: add basic initial driver for MxL862xx switches
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
For local reproduction steps see https://netdev-ai.bots.linux.dev/ai-local.html
---
net: dsa: add basic initial driver for MxL862xx switches
Add very basic DSA driver for MaxLinear's MxL862xx switches. In contrast to previous MaxLinear switches the MxL862xx has a built-in processor that runs a sophisticated firmware based on Zephyr RTOS. Interaction between the host and the switch hence is organized using a software API of that firmware rather than accessing hardware registers directly. Add descriptions of the most basic firmware API calls to access the built-in MDIO bus hosting the 2.5GE PHYs, basic port control as well as setting up the CPU port. Implement a very basic DSA driver using that API which is sufficient to get packets flowing between the user ports and the CPU port.
Quoted 8 lines
drivers/net/dsa/mxl862xx/Kconfig new file +2
-0@@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0-only +config NET_DSA_MXL862
drivers/net/dsa/mxl862xx/Kconfig line 12
Quoted 10 lines
drivers/net/dsa/mxl862xx/Kconfig new file +10
-0+ tristate "MaxLinear MxL862xx" + depends on NET_DSA + select MAXLINEAR_GPHY + select NET_DSA_TAG_MXL_862XX + help + This enables support for the MaxLinear MxL862xx switch family. + These switches have two 10GE SerDes interfaces, one typically + used as CPU port. + MxL86282 has eight 2.5 Gigabit PHYs + MxL86252 has five 2.5 Gigabit PHYs
This isn't a bug, but the last two lines have inconsistent indentation. The help text lines describing MxL86282 and MxL86252 have three spaces after the tab while the other help text lines have two spaces.
[Jakub] maybe add a "-" or such in front to make it clear this is a list?
Quoted 5 lines
drivers/net/dsa/mxl862xx/mxl862xx-api.h new file +0
-0
[ ... ]
Quoted 13 lines
drivers/net/dsa/mxl862xx/mxl862xx-api.h new file +13
-0+/** +* struct mxl862xx_bridge_alloc - Bridge Allocation +* @bridge_id: If the bridge allocation is successful, a valid ID will be +* returned in this field. Otherwise, INVALID_HANDLE is +* returned. For bridge free, this field should contain a +* valid ID returned by the bridge allocation. ID 0 is not +* used for historic reasons. +* +* Used by MXL862XX_BRIDGE_ALLOC and MXL862XX_BRIDGE_FREE. +*/ +struct mxl862xx_bridge_alloc { +__le16 bridge_id; +} __packed;
[ ... ]
drivers/net/dsa/mxl862xx/mxl862xx-api.h line 203
Quoted 12 lines
drivers/net/dsa/mxl862xx/mxl862xx-api.h new file +12
-0+/** +* struct mxl862xx_bridge_port_alloc - Bridge Port Allocation +* @bridge_port_id: If the bridge port allocation is successful, a valid ID +* will be returned in this field. Otherwise, INVALID_HANDLE +* is returned. For bridge port free, this field should +* contain a valid ID returned by the bridge port allocation. +* +* Used by MXL862XX_BRIDGE_PORT_ALLOC and MXL862XX_BRIDGE_PORT_FREE. +*/ +struct mxl862xx_bridge_port_alloc { +__le16 bridge_port_id; +};
Is the __packed attribute missing here? The similar struct mxl862xx_bridge_alloc has __packed, and both structures have identical layout with a single __le16 field. Both are used for firmware communication where padding could break the protocol.
[Jakub] please remove the __packed attribute on structs which are
naturally packed according to C rules.
Quoted 5 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +0
-0
[ ... ]
drivers/net/dsa/mxl862xx/mxl862xx.c line 160
Quoted 4 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +4
-0+static void mxl862xx_port_disable (struct dsa_switch *ds,int port) +{ +mxl862xx_port_state (ds, port, false); +}
Should the error return from mxl862xx_port_state() be logged?
The function mxl862xx_port_state() can fail and returns an error code, but mxl862xx_port_disable() ignores this return value. While the DSA framework defines port_disable as returning void so errors cannot be propagated to the caller, silently ignoring errors could leave the port's SDMA or FDMA enabled if the API call fails. The function mxl862xx_port_fast_age() logs errors when its API call fails.
Quoted 13 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +13
-0+static int mxl862xx_add_single_port_bridge (struct dsa_switch *ds,int port) +{ +struct mxl862xx_bridge_port_config br_port_cfg = {}; +struct dsa_port *dp =dsa_to_port (ds, port); +struct mxl862xx_bridge_alloc br_alloc = {}; +int ret; + + ret =MXL862XX_API_READ (ds->priv,MXL862XX_BRIDGE_ALLOC , br_alloc); +if (ret) { +dev_err (ds->dev,"failed to allocate a bridge for port %d\n" , port); +return ret; + } +
drivers/net/dsa/mxl862xx/mxl862xx.c line 195
Quoted 10 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +10
-0+ br_port_cfg.bridge_id = br_alloc.bridge_id; + br_port_cfg.bridge_port_id =cpu_to_le16 (port); + br_port_cfg.mask =cpu_to_le32 (MXL862XX_BRIDGE_PORT_CONFIG_MASK_BRIDGE_ID | +MXL862XX_BRIDGE_PORT_CONFIG_MASK_BRIDGE_PORT_MAP | +MXL862XX_BRIDGE_PORT_CONFIG_MASK_MC_SRC_MAC_LEARNING | +MXL862XX_BRIDGE_PORT_CONFIG_MASK_VLAN_BASED_MAC_LEARNING ); + br_port_cfg.src_mac_learning_disable = true; + br_port_cfg.vlan_src_mac_vid_enable = false; + br_port_cfg.vlan_dst_mac_vid_enable = false; + br_port_cfg.bridge_port_map[0 ] =cpu_to_le16 (BIT (dp->cpu_dp->index));
Can this dereference NULL pointers?
The dsa_to_port() function can return NULL if the port is not found in the switch tree. The code stores the result in dp but doesn't check if dp is NULL before dereferencing dp->cpu_dp->index.
Looking at include/net/dsa.h, dsa_to_port() returns NULL when the port is not found:
static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
{
struct dsa_switch_tree *dst = ds->dst;
struct dsa_port *dp;
list_for_each_entry(dp, &dst->ports, list)
if (dp->ds == ds && dp->index == p)
return dp;
return NULL;
}Additionally, even if dp is non-NULL, dp->cpu_dp could also be NULL if the CPU port hasn't been properly assigned during initialization.
Quoted 7 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +7
-0+static int mxl862xx_setup_mdio (struct dsa_switch *ds) +{ +struct mxl862xx_priv *priv = ds->priv; +struct device *dev = ds->dev; +struct device_node *mdio_np; +struct mii_bus *bus; +static int idx;
drivers/net/dsa/mxl862xx/mxl862xx.c line 216
Quoted 10 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +10
-0+int ret; + + bus =devm_mdiobus_alloc (dev); +if (!bus) +return -ENOMEM ; + + bus->priv = priv; + ds->user_mii_bus = bus; + bus->name =KBUILD_MODNAME "-mii" ; +snprintf (bus->id,MII_BUS_ID_SIZE ,KBUILD_MODNAME "-%d" , idx++);
Can concurrent probes race on the idx variable?
The static int idx is shared across all driver instances and accessed without synchronization. If multiple instances of this driver are probed concurrently on different CPU cores, they could race on the idx++ operation potentially leading to duplicate MII bus IDs. MII bus IDs must be unique system-wide.
[ ... ]
Quoted 11 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +11
-0+static int mxl862xx_setup_cpu_bridge (struct dsa_switch *ds,int port) +{ +struct mxl862xx_bridge_port_config br_port_cfg = {}; +struct mxl862xx_priv *priv = ds->priv; +u16 bridge_port_map =0 ; +struct dsa_port *dp; + +/* CPU port bridge setup */ + br_port_cfg.mask =cpu_to_le32 (MXL862XX_BRIDGE_PORT_CONFIG_MASK_BRIDGE_PORT_MAP | +MXL862XX_BRIDGE_PORT_CONFIG_MASK_MC_SRC_MAC_LEARNING | +MXL862XX_BRIDGE_PORT_CONFIG_MASK_VLAN_BASED_MAC_LEARNING );
drivers/net/dsa/mxl862xx/mxl862xx.c line 293
Quoted 10 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +10
-0+ + br_port_cfg.bridge_port_id =cpu_to_le16 (port); + br_port_cfg.src_mac_learning_disable = false; + br_port_cfg.vlan_src_mac_vid_enable = true; + br_port_cfg.vlan_dst_mac_vid_enable = true; + +/* include all assigned user ports in the CPU portmap */ +dsa_switch_for_each_user_port (dp, ds) { +if (dp->cpu_dp->index != port) +continue ;
Can dp->cpu_dp be dereferenced safely here?
The code dereferences dp->cpu_dp->index without checking if cpu_dp is NULL. While the DSA framework should ensure user ports have a CPU port assigned during initialization, there is no explicit NULL check. If cpu_dp is NULL for any reason such as incomplete initialization or configuration error, this will cause a NULL pointer dereference and kernel crash.
drivers/net/dsa/mxl862xx/mxl862xx.c line 429
Quoted 3 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +3
-0+ ret =dsa_register_switch (ds); +if (ret) +return ret;
Is dev_set_drvdata() called too early?
The function calls dev_set_drvdata(dev, ds) before calling dsa_register_switch(ds). If dsa_register_switch() fails, the function returns the error but leaves the driver data pointing to an unregistered switch.
This creates a problem for cleanup paths. The mxl862xx_remove() function retrieves the switch with dev_get_drvdata() and calls dsa_unregister_switch(ds). If probe failed, ds is not NULL so the check passes but the switch was never successfully registered. Similarly, mxl862xx_shutdown() could be called and would also operate on an unregistered switch. While dsa_switch_shutdown() has a check for ds->setup flag that provides some protection, this is fragile and could lead to inconsistent state or crashes in error scenarios.
[Jakub] AI is a bit pedantic about leaving the drvdata pointer in place
but I guess it's not a bad thing to clean up
Signature · 1 line
pw-bot: cr
Daniel Golle
maintainerRe: [net-next,v11,4/4] net: dsa: add basic initial driver for MxL862xx switches
Quoted 19 lines · Jakub Kicinski
drivers/net/dsa/mxl862xx/mxl862xx-api.h line 203
Quoted 13 lines
[...]
drivers/net/dsa/mxl862xx/mxl862xx-api.h new file +12
-0+/** +* struct mxl862xx_bridge_port_alloc - Bridge Port Allocation +* @bridge_port_id: If the bridge port allocation is successful, a valid ID +* will be returned in this field. Otherwise, INVALID_HANDLE +* is returned. For bridge port free, this field should +* contain a valid ID returned by the bridge port allocation. +* +* Used by MXL862XX_BRIDGE_PORT_ALLOC and MXL862XX_BRIDGE_PORT_FREE. +*/ +struct mxl862xx_bridge_port_alloc { +__le16 bridge_port_id; +};Is the
__packedattribute missing here? The similar structmxl862xx_bridge_allochas__packed, and both structures have identical layout with a single__le16field. Both are used for firmware communication where padding could break the protocol.
Quoted 2 lines
[Jakub] please remove the
__packedattribute on structs which are
naturally packed according to C rules.
I'll do that for the upcoming submission of v12, but wonder if using '#pragma pack(push, 1)' around the complete API header file wouldn't be the better option, as anyway *everything* should always be packed there, as the switch firmware expects data byte-aligned.
Quoted 21 lines
Quoted 8 lines
[...]
drivers/net/dsa/mxl862xx/mxl862xx.c new file +7
-0+static int mxl862xx_setup_mdio (struct dsa_switch *ds) +{ +struct mxl862xx_priv *priv = ds->priv; +struct device *dev = ds->dev; +struct device_node *mdio_np; +struct mii_bus *bus; +static int idx;drivers/net/dsa/mxl862xx/mxl862xx.c line 216
Quoted 10 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +10
-0+int ret; + + bus =devm_mdiobus_alloc (dev); +if (!bus) +return -ENOMEM ; + + bus->priv = priv; + ds->user_mii_bus = bus; + bus->name =KBUILD_MODNAME "-mii" ; +snprintf (bus->id,MII_BUS_ID_SIZE ,KBUILD_MODNAME "-%d" , idx++);Can concurrent probes race on the idx variable?
Quoted 6 lines
The static int idx is shared across all driver instances and accessed without synchronization. If multiple instances of this driver are probed concurrently on different CPU cores, they could race on the idx++ operation potentially leading to duplicate MII bus IDs. MII bus IDs must be unique system-wide.
Do you think it's worth to fix this also in other drivers which follow the same pattern? (bcm_sf2, mt7530 and mv88e6xxx if OF node is NULL -- if there is an OF node it uses %pOF, and I'm not sure that's such a good idea either)
Apart from being racy, this also means the MII bus name depends on probe order which isn't very nice either. Using
snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(priv->dev));
may result in a slightly more ugly-looking, but predictable and non-racy (ie. always unique) name.
Other than that there is also
snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d.%d",
KBUILD_MODNAME, ds->dst->index, ds->index);
The real question here is if we consider MII bus names to be ABI, ie. if they should never change once a driver is part of the kernel.
Russell King (Oracle)
reviewerRe: [net-next,v11,4/4] net: dsa: add basic initial driver for MxL862xx switches
Quoted 4 lines · Daniel Golle
Do you think it's worth to fix this also in other drivers which follow the same pattern? (
bcm_sf2, mt7530 and mv88e6xxx if OF node is NULL -- if there is an OF node it uses %pOF, and I'm not sure that's such a good idea either)
So in v11 here, you ask a question of your reviewer. 2 hours later, you send v12 without waiting for a response.
Slow down. Give your reviewers time to respond before posting your next version.
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
Daniel Golle
maintainerRe: [net-next,v11,4/4] net: dsa: add basic initial driver for MxL862xx switches
Quoted 11 lines · Russell King (Oracle)
Quoted 4 lines · Daniel Golle
Do you think it's worth to fix this also in other drivers which follow the same pattern? (
bcm_sf2, mt7530 and mv88e6xxx if OF node is NULL -- if there is an OF node it uses %pOF, and I'm not sure that's such a good idea either)So in v11 here, you ask a question of your reviewer. 2 hours later, you send v12 without waiting for a response.
Slow down. Give your reviewers time to respond before posting your next version.
Got it, just thought because the question wasn't related to that driver but rather whether the same issue (or non-issue?) should also be fixed in other drivers...
I'm going to wait now until all debates have settled and then post v13 :)
Vladimir Oltean
maintainerRe: [net-next,v11,4/4] net: dsa: add basic initial driver for MxL862xx switches
In reply to Jakub KicinskiQuoted 17 lines · Jakub Kicinski
Quoted 17 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +17
-0+static int mxl862xx_add_single_port_bridge (struct dsa_switch *ds,int port) +{ +struct mxl862xx_bridge_port_config br_port_cfg = {}; +struct dsa_port *dp =dsa_to_port (ds, port); +struct mxl862xx_bridge_alloc br_alloc = {}; +int ret; + + ret =MXL862XX_API_READ (ds->priv,MXL862XX_BRIDGE_ALLOC , br_alloc); +if (ret) { +dev_err (ds->dev,"failed to allocate a bridge for port %d\n" , port); +return ret; + } + + br_port_cfg.bridge_id = br_alloc.bridge_id; + br_port_cfg.bridge_port_id =cpu_to_le16 (port); + br_port_cfg.mask = cpu_to_le32(MXL862XX_BRIDGE_PORT_CONFIG_MASK_BRIDGE_ID | + MXL862XX_BRIDGE_PORT_CONFIG_MASK_BRIDGE_PORT_MAP |
Quoted 8 lines
drivers/net/dsa/mxl862xx/mxl862xx.c line 195
Quoted 6 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +6
-0+MXL862XX_BRIDGE_PORT_CONFIG_MASK_MC_SRC_MAC_LEARNING | + MXL862XX_BRIDGE_PORT_CONFIG_MASK_VLAN_BASED_MAC_LEARNING); + br_port_cfg.src_mac_learning_disable = true; + br_port_cfg.vlan_src_mac_vid_enable = false; + br_port_cfg.vlan_dst_mac_vid_enable = false; + br_port_cfg.bridge_port_map[0 ] =cpu_to_le16 (BIT (dp->cpu_dp->index));Can this dereference NULL pointers?
No. DSA will not make calls to ds->ops->port_setup() with "port" values for which dsa_to_port(ds, port) will not find a struct dsa_port *.
Quoted 19 lines
The
dsa_to_port()function can return NULL if the port is not found in the switch tree. The code stores the result in dp but doesn't check if dp is NULL before dereferencingdp->cpu_dp->index.Looking at
include/net/dsa.h,dsa_to_port()returns NULL when the port is not found:static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p) { struct dsa_switch_tree *dst = ds->dst; struct dsa_port *dp; list_for_each_entry(dp, &dst->ports, list) if (dp->ds == ds && dp->index == p) return dp; return NULL; }
Quoted 2 lines
Additionally, even if dp is non-NULL,
dp->cpu_dpcould also be NULL if the CPU port hasn't been properly assigned during initialization.
mxl862xx_add_single_port_bridge() has been called when all other port types except user ports have been excluded. All user and DSA ports have a non-NULL dp->cpu_dp pointer after dsa_tree_setup_cpu_ports() runs, i.e. also at the time of ds->ops->port_setup().
Quoted 20 lines
Quoted 7 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +7
-0+static int mxl862xx_setup_mdio (struct dsa_switch *ds) +{ +struct mxl862xx_priv *priv = ds->priv; +struct device *dev = ds->dev; +struct device_node *mdio_np; +struct mii_bus *bus; +static int idx;drivers/net/dsa/mxl862xx/mxl862xx.c line 216
Quoted 10 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +10
-0+int ret; + + bus =devm_mdiobus_alloc (dev); +if (!bus) +return -ENOMEM ; + + bus->priv = priv; + ds->user_mii_bus = bus; + bus->name =KBUILD_MODNAME "-mii" ; +snprintf (bus->id,MII_BUS_ID_SIZE ,KBUILD_MODNAME "-%d" , idx++);Can concurrent probes race on the idx variable?
Quoted 7 lines
The static int idx is shared across all driver instances and accessed without synchronization. If multiple instances of this driver are probed concurrently on different CPU cores, they could race on the idx++ operation potentially leading to duplicate MII bus IDs. MII bus IDs must be unique system-wide.
[ ... ]
Technically ds->ops->setup() runs under dsa2_mutex, but the "static int idx" is still not ideal due to the ever-increasing index upon unbinding and rebinding.
Quoted 24 lines
Quoted 11 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +11
-0+static int mxl862xx_setup_cpu_bridge (struct dsa_switch *ds,int port) +{ +struct mxl862xx_bridge_port_config br_port_cfg = {}; +struct mxl862xx_priv *priv = ds->priv; +u16 bridge_port_map =0 ; +struct dsa_port *dp; + +/* CPU port bridge setup */ + br_port_cfg.mask =cpu_to_le32 (MXL862XX_BRIDGE_PORT_CONFIG_MASK_BRIDGE_PORT_MAP | +MXL862XX_BRIDGE_PORT_CONFIG_MASK_MC_SRC_MAC_LEARNING | +MXL862XX_BRIDGE_PORT_CONFIG_MASK_VLAN_BASED_MAC_LEARNING );drivers/net/dsa/mxl862xx/mxl862xx.c line 293
Quoted 10 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +10
-0+ + br_port_cfg.bridge_port_id =cpu_to_le16 (port); + br_port_cfg.src_mac_learning_disable = false; + br_port_cfg.vlan_src_mac_vid_enable = true; + br_port_cfg.vlan_dst_mac_vid_enable = true; + +/* include all assigned user ports in the CPU portmap */ +dsa_switch_for_each_user_port (dp, ds) { +if (dp->cpu_dp->index != port) +continue ;Can
dp->cpu_dpbe dereferenced safely here?
Quoted 6 lines
The code dereferences
dp->cpu_dp->indexwithout checking ifcpu_dpis NULL. While the DSA framework should ensure user ports have a CPU port assigned during initialization, there is no explicit NULL check. Ifcpu_dpis NULL for any reason such as incomplete initialization or configuration error, this will cause a NULL pointer dereference and kernel crash.
No incompletely initialized ports are published to dst->ports.
Quoted 20 lines
drivers/net/dsa/mxl862xx/mxl862xx.c line 429
Quoted 3 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +3
-0+ ret =dsa_register_switch (ds); +if (ret) +return ret;Is
dev_set_drvdata()called too early?The function calls
dev_set_drvdata(dev, ds)before callingdsa_register_switch(ds). Ifdsa_register_switch()fails, the function returns the error but leaves the driver data pointing to an unregistered switch.This creates a problem for cleanup paths. The
mxl862xx_remove()function retrieves the switch withdev_get_drvdata()and callsdsa_unregister_switch(ds). If probe failed, ds is not NULL so the check passes but the switch was never successfully registered. Similarly,mxl862xx_shutdown()could be called and would also operate on an unregistered switch. Whiledsa_switch_shutdown()has a check fords->setupflag that provides some protection, this is fragile and could lead to inconsistent state or crashes in error scenarios.
Quoted 2 lines
[Jakub] AI is a bit pedantic about leaving the drvdata pointer in place
but I guess it's not a bad thing to clean up
Can somebody gently explain what is the point? It is easily testable that if mdio_driver :: probe() fails, .remove() or .shutdown() will not be called. It really does not matter whether dev_set_drvdata() was called or not.
Daniel Golle
maintainerRe: [net-next,v11,4/4] net: dsa: add basic initial driver for MxL862xx switches
Quoted 27 lines · Vladimir Oltean
Quoted 17 lines · Jakub Kicinski
Quoted 17 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +17
-0+static int mxl862xx_add_single_port_bridge (struct dsa_switch *ds,int port) +{ +struct mxl862xx_bridge_port_config br_port_cfg = {}; +struct dsa_port *dp =dsa_to_port (ds, port); +struct mxl862xx_bridge_alloc br_alloc = {}; +int ret; + + ret =MXL862XX_API_READ (ds->priv,MXL862XX_BRIDGE_ALLOC , br_alloc); +if (ret) { +dev_err (ds->dev,"failed to allocate a bridge for port %d\n" , port); +return ret; + } + + br_port_cfg.bridge_id = br_alloc.bridge_id; + br_port_cfg.bridge_port_id =cpu_to_le16 (port); + br_port_cfg.mask = cpu_to_le32(MXL862XX_BRIDGE_PORT_CONFIG_MASK_BRIDGE_ID | + MXL862XX_BRIDGE_PORT_CONFIG_MASK_BRIDGE_PORT_MAP |Quoted 8 lines
drivers/net/dsa/mxl862xx/mxl862xx.c line 195
Quoted 6 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +6
-0+MXL862XX_BRIDGE_PORT_CONFIG_MASK_MC_SRC_MAC_LEARNING | + MXL862XX_BRIDGE_PORT_CONFIG_MASK_VLAN_BASED_MAC_LEARNING); + br_port_cfg.src_mac_learning_disable = true; + br_port_cfg.vlan_src_mac_vid_enable = false; + br_port_cfg.vlan_dst_mac_vid_enable = false; + br_port_cfg.bridge_port_map[0 ] =cpu_to_le16 (BIT (dp->cpu_dp->index));Can this dereference NULL pointers?
Quoted 2 lines
No. DSA will not make calls to
ds->ops->port_setup()with "port" values for whichdsa_to_port(ds, port)will not find a structdsa_port*.
So I should again remove the check for !=NULL here and...
Quoted 19 lines
Quoted 19 lines
The
dsa_to_port()function can return NULL if the port is not found in the switch tree. The code stores the result in dp but doesn't check if dp is NULL before dereferencingdp->cpu_dp->index.Looking at
include/net/dsa.h,dsa_to_port()returns NULL when the port is not found:static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p) { struct dsa_switch_tree *dst = ds->dst; struct dsa_port *dp; list_for_each_entry(dp, &dst->ports, list) if (dp->ds == ds && dp->index == p) return dp; return NULL; }
Quoted 7 lines
Quoted 2 lines
Additionally, even if dp is non-NULL,
dp->cpu_dpcould also be NULL if the CPU port hasn't been properly assigned during initialization.
mxl862xx_add_single_port_bridge()has been called when all other port types except user ports have been excluded. All user and DSA ports have a non-NULLdp->cpu_dppointer afterdsa_tree_setup_cpu_ports()runs, i.e. also at the time ofds->ops->port_setup().
here, as well as in mxl862xx_setup_cpu_bridge(), right?
Quoted 27 lines
Quoted 27 lines
Quoted 8 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +7
-0+static int mxl862xx_setup_mdio (struct dsa_switch *ds) +{ +struct mxl862xx_priv *priv = ds->priv; +struct device *dev = ds->dev; +struct device_node *mdio_np; +struct mii_bus *bus; +static int idx;drivers/net/dsa/mxl862xx/mxl862xx.c line 216
Quoted 10 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +10
-0+int ret; + + bus =devm_mdiobus_alloc (dev); +if (!bus) +return -ENOMEM ; + + bus->priv = priv; + ds->user_mii_bus = bus; + bus->name =KBUILD_MODNAME "-mii" ; +snprintf (bus->id,MII_BUS_ID_SIZE ,KBUILD_MODNAME "-%d" , idx++);Can concurrent probes race on the idx variable?
The static int idx is shared across all driver instances and accessed without synchronization. If multiple instances of this driver are probed concurrently on different CPU cores, they could race on the idx++ operation potentially leading to duplicate MII bus IDs. MII bus IDs must be unique system-wide.
Quoted 5 lines
Quoted 1 line
[ ... ]
Technically
ds->ops->setup()runs underdsa2_mutex, but the "static int idx" is still not ideal due to the ever-increasing index upon unbinding and rebinding.
As mentioned in my reply to Jakub[1] many drivers follow this pattern and it would imho be a good idea to agree on a convention or even provide a helper function which names the MII bus for DSA drivers. What do you think?
[1]: https://patchwork.kernel.org/comment/26768088/
Quoted 24 lines
Quoted 24 lines
Quoted 11 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +11
-0+static int mxl862xx_setup_cpu_bridge (struct dsa_switch *ds,int port) +{ +struct mxl862xx_bridge_port_config br_port_cfg = {}; +struct mxl862xx_priv *priv = ds->priv; +u16 bridge_port_map =0 ; +struct dsa_port *dp; + +/* CPU port bridge setup */ + br_port_cfg.mask =cpu_to_le32 (MXL862XX_BRIDGE_PORT_CONFIG_MASK_BRIDGE_PORT_MAP | +MXL862XX_BRIDGE_PORT_CONFIG_MASK_MC_SRC_MAC_LEARNING | +MXL862XX_BRIDGE_PORT_CONFIG_MASK_VLAN_BASED_MAC_LEARNING );drivers/net/dsa/mxl862xx/mxl862xx.c line 293
Quoted 10 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +10
-0+ + br_port_cfg.bridge_port_id =cpu_to_le16 (port); + br_port_cfg.src_mac_learning_disable = false; + br_port_cfg.vlan_src_mac_vid_enable = true; + br_port_cfg.vlan_dst_mac_vid_enable = true; + +/* include all assigned user ports in the CPU portmap */ +dsa_switch_for_each_user_port (dp, ds) { +if (dp->cpu_dp->index != port) +continue ;Can
dp->cpu_dpbe dereferenced safely here?
Quoted 8 lines
Quoted 6 lines
The code dereferences
dp->cpu_dp->indexwithout checking ifcpu_dpis NULL. While the DSA framework should ensure user ports have a CPU port assigned during initialization, there is no explicit NULL check. Ifcpu_dpis NULL for any reason such as incomplete initialization or configuration error, this will cause a NULL pointer dereference and kernel crash.No incompletely initialized ports are published to
dst->ports.
Will remove the !=NULL check then again, see above.
Quoted 28 lines
Quoted 21 lines
drivers/net/dsa/mxl862xx/mxl862xx.c line 429
Quoted 4 lines
drivers/net/dsa/mxl862xx/mxl862xx.c new file +3
-0+ ret =dsa_register_switch (ds); +if (ret) +return ret;Is
dev_set_drvdata()called too early?The function calls
dev_set_drvdata(dev, ds)before callingdsa_register_switch(ds). Ifdsa_register_switch()fails, the function returns the error but leaves the driver data pointing to an unregistered switch.This creates a problem for cleanup paths. The
mxl862xx_remove()function retrieves the switch withdev_get_drvdata()and callsdsa_unregister_switch(ds). If probe failed, ds is not NULL so the check passes but the switch was never successfully registered. Similarly,mxl862xx_shutdown()could be called and would also operate on an unregistered switch. Whiledsa_switch_shutdown()has a check fords->setupflag that provides some protection, this is fragile and could lead to inconsistent state or crashes in error scenarios.Quoted 2 lines
[Jakub] AI is a bit pedantic about leaving the drvdata pointer in place
but I guess it's not a bad thing to clean upCan somebody gently explain what is the point? It is easily testable that if
mdio_driver::probe()fails, .remove() or .shutdown() will not be called. It really does not matter whetherdev_set_drvdata()was called or not.
Vladimir Oltean
maintainerRe: [net-next,v11,4/4] net: dsa: add basic initial driver for MxL862xx switches
Quoted 22 lines · Daniel Golle
Quoted 22 lines
Quoted 21 lines
The
dsa_to_port()function can return NULL if the port is not found in the switch tree. The code stores the result in dp but doesn't check if dp is NULL before dereferencingdp->cpu_dp->index.Looking at
include/net/dsa.h,dsa_to_port()returns NULL when the port is not found:static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p) { struct dsa_switch_tree *dst = ds->dst; struct dsa_port *dp; list_for_each_entry(dp, &dst->ports, list) if (dp->ds == ds && dp->index == p) return dp; return NULL; }Additionally, even if dp is non-NULL,
dp->cpu_dpcould also be NULL if the CPU port hasn't been properly assigned during initialization.
Quoted 6 lines
Quoted 4 lines
mxl862xx_add_single_port_bridge()has been called when all other port types except user ports have been excluded. All user and DSA ports have a non-NULLdp->cpu_dppointer afterdsa_tree_setup_cpu_ports()runs, i.e. also at the time ofds->ops->port_setup().here, as well as in
mxl862xx_setup_cpu_bridge(), right?
Yeah. If you think adding a comment helps keep spirits calm, you can also do that.
Quoted 9 lines
Quoted 3 lines
Technically
ds->ops->setup()runs underdsa2_mutex, but the "static int idx" is still not ideal due to the ever-increasing index upon unbinding and rebinding.As mentioned in my reply to Jakub[1] many drivers follow this pattern and it would imho be a good idea to agree on a convention or even provide a helper function which names the MII bus for DSA drivers. What do you think?
I don't really have an opinion, the internal MDIO bus is part of each driver's housekeeping, and DSA tries to stay out of that as much as possible.
Maybe PHY maintainers may know more what user space tooling may break if the mii_bus->id gets changed, or may prefer a naming convention irrespective of the bus being part of a DSA switch or not.