Skip to contentSkip to conversation

net: axienet: allow instantiation without a device tree

PATCH Superseded Sagi Maimon 2 participants 5 messages Sep 27 – Sep 28

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover net: axienet: allow instantiation without a device tree

The ADVA TimeCard X2 is a PCIe timing card driven by ptp_ocp. Its FPGA contains an AXI Ethernet MAC with an AXI DMA engine. The plan discussed on netdev [1] is for ptp_ocp to register the MAC as a child platform device described by a software node, so that this driver runs it rather than a copy in ptp_ocp; Vadim Fedorenko agreed that a platform device is the way to go.

These two patches are what axienet needs for that, apart from one more change - letting the DMA device differ from the platform device - which has no user without ptp_ocp and will be posted together with the ptp_ocp patches:

1/2 reads the probe-time configuration through the device_property and
fwnode APIs, so it can come from a software node. No change for device tree users.
2/2 adds a MODULE_ALIAS, so a device registered by name autoloads the
module.

Both were tested on the X2 with a local ptp_ocp change.

[1] https://lore.kernel.org/netdev/CAMuE1bHr84n5LKcj02HmMR5XefPtvQ-UEJAx7+UKfYvRgS0eDg@mail.gmail.com/

Sagi Maimon (2):
net: axienet: use device_property and fwnode APIs for probe-time
config
net: axienet: add a MODULE_ALIAS for platform instantiation

 .../net/ethernet/xilinx/xilinx_axienet_main.c | 34 ++++++++++++-------
 1 file changed, 22 insertions(+), 12 deletions(-)

base-commit: 014d795c73837ea2339a4ea8e8f82c6e959b845d

Signature · 1 line
2.47.0
#1 lore raw

PATCH 1/2 net: axienet: use device_property and fwnode APIs for probe-time config

axienet_probe() reads its configuration exclusively through the of_* API, so the driver can only be instantiated from a device tree node.

The ADVA TimeCard X2 is a PCIe timing card, driven by ptp_ocp, whose FPGA contains an AXI Ethernet MAC with an AXI DMA engine. ptp_ocp is going to register the MAC as a child platform device described by a software node, so that this driver runs it rather than a copy in ptp_ocp. That needs the probe-time configuration to be readable from a software node.

The of_property_read_*(), of_get_phy_mode(), of_get_mac_address() and phylink_of_phy_connect() calls used here all have fwnode-based equivalents that resolve identically for a DT-backed device, and additionally work for a device described by a software node.

Convert them. On a device tree this is a no-op: device_property_*() dispatches to the of_* implementation when dev->fwnode is an OF node, and device_get_phy_mode()/device_get_mac_address() are thin fwnode wrappers around the same lookups. device_get_mac_address() also keeps the "mac-address" nvmem cell fallback that of_get_mac_address() has.

Note that device_get_phy_mode() returns the mode as a positive value rather than through an out parameter, so the error test changes from "if (ret)" to "if (ret < 0)".

Not everything is converted. The "axistream-connected" lookup stays on the OF API because its result is passed to of_address_to_resource(), which has no fwnode counterpart. The "pcs-handle" and "phy-handle" lookups could use fwnode_find_reference() and fwnode_mdio_find_device(), but they refer to MDIO devices that only a device tree describes - the bus axienet_mdio_setup() registers goes through of_mdiobus_register() - so converting them would not let a caller without a device tree use them. Such a caller takes neither branch: it gets its DMA registers from its own platform resources, and it cannot use the SGMII and 1000BASE-X modes, which need a PCS.

No functional change intended.

Tested on the X2 with a local ptp_ocp change, with CONFIG_OF disabled and enabled: the interface probes with its configuration read from the software node and passes traffic. No device tree board was available, so the claim that this is a no-op for device tree users rests on the dispatch described above.

Assisted-by LLM sparse
---
 .../net/ethernet/xilinx/xilinx_axienet_main.c | 24 ++++++++++---------
 1 file changed, 13 insertions(+), 11 deletions(-)
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +13 -11
index 782f903d318f..cfef8ad75909 100644
@@ -33,6 +33,7 @@
 #include <linux/of_irq.h>
 #include <linux/of_address.h>
 #include <linux/platform_device.h>
+#include <linux/property.h>
 #include <linux/skbuff.h>
 #include <linux/math64.h>
 #include <linux/phy.h>
@@ -1671,7 +1672,7 @@ static int axienet_open(struct net_device *ndev)
 	ret = axienet_device_reset(ndev);
 	axienet_unlock_mii(lp);
 
-	ret = phylink_of_phy_connect(lp->phylink, lp->dev->of_node, 0);
+	ret = phylink_fwnode_phy_connect(lp->phylink, dev_fwnode(lp->dev), 0);
 	if (ret) {
 		dev_err(lp->dev, "phylink_of_phy_connect() failed: %d\n", ret);
 		return ret;
@@ -2864,7 +2865,7 @@ static int axienet_probe(struct platform_device *pdev)
 	if (axienet_ior(lp, XAE_ABILITY_OFFSET) & XAE_ABILITY_STATS)
 		lp->features |= XAE_FEATURE_STATS;
 
-	ret = of_property_read_u32(pdev->dev.of_node, "xlnx,txcsum", &value);
+	ret = device_property_read_u32(&pdev->dev, "xlnx,txcsum", &value);
 	if (!ret) {
 		switch (value) {
 		case 1:
@@ -2879,7 +2880,7 @@ static int axienet_probe(struct platform_device *pdev)
 			break;
 		}
 	}
-	ret = of_property_read_u32(pdev->dev.of_node, "xlnx,rxcsum", &value);
+	ret = device_property_read_u32(&pdev->dev, "xlnx,rxcsum", &value);
 	if (!ret) {
 		switch (value) {
 		case 1:
@@ -2898,16 +2899,16 @@ static int axienet_probe(struct platform_device *pdev)
 	 * Here we check for memory allocated for Rx/Tx in the hardware from
 	 * the device-tree and accordingly set flags.
 	 */
-	ret = of_property_read_u32(pdev->dev.of_node, "xlnx,rxmem", &lp->rxmem);
+	ret = device_property_read_u32(&pdev->dev, "xlnx,rxmem", &lp->rxmem);
 	if (ret)
 		return dev_err_probe(&pdev->dev, ret,
 				     "failed to read xlnx,rxmem property\n");
 
-	lp->switch_x_sgmii = of_property_read_bool(pdev->dev.of_node,
-						   "xlnx,switch-x-sgmii");
+	lp->switch_x_sgmii = device_property_read_bool(&pdev->dev,
+						       "xlnx,switch-x-sgmii");
 
 	/* Start with the proprietary, and broken phy_type */
-	ret = of_property_read_u32(pdev->dev.of_node, "xlnx,phy-type", &value);
+	ret = device_property_read_u32(&pdev->dev, "xlnx,phy-type", &value);
 	if (!ret) {
 		netdev_warn(ndev, "Please upgrade your device tree binary blob to use phy-mode");
 		switch (value) {
@@ -2930,9 +2931,10 @@ static int axienet_probe(struct platform_device *pdev)
 			return -EINVAL;
 		}
 	} else {
-		ret = of_get_phy_mode(pdev->dev.of_node, &lp->phy_mode);
-		if (ret)
+		ret = device_get_phy_mode(&pdev->dev);
+		if (ret < 0)
 			return ret;
+		lp->phy_mode = ret;
 	}
 	if (lp->switch_x_sgmii && lp->phy_mode != PHY_INTERFACE_MODE_SGMII &&
 	    lp->phy_mode != PHY_INTERFACE_MODE_1000BASEX) {
@@ -2940,7 +2942,7 @@ static int axienet_probe(struct platform_device *pdev)
 		return -EINVAL;
 	}
 
-	if (!of_property_present(pdev->dev.of_node, "dmas")) {
+	if (!device_property_present(&pdev->dev, "dmas")) {
 		/* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
 		np = of_parse_phandle(pdev->dev.of_node, "axistream-connected", 0);
 
@@ -3060,7 +3062,7 @@ static int axienet_probe(struct platform_device *pdev)
 		dev_info(&pdev->dev, "Ethernet core IRQ not defined\n");
 
 	/* Retrieve the MAC address */
-	ret = of_get_mac_address(pdev->dev.of_node, mac_addr);
+	ret = device_get_mac_address(&pdev->dev, mac_addr);
 	if (!ret) {
 		axienet_set_mac_address(ndev, mac_addr);
 	} else {
Signature · 1 line
2.47.0
#4 lore raw

PATCH 2/2 net: axienet: add a MODULE_ALIAS for platform instantiation

In reply to Sagi Maimon

The module is built as xilinx_emac.ko while the platform driver it registers is named xilinx_axienet. For a device tree instantiation that mismatch does not matter, because the OF match table supplies the modalias udev uses to load the module.

A device registered by name from another driver has no OF node and so no OF modalias. It advertises platform:xilinx_axienet, which matches nothing, and the device is created but never bound. ptp_ocp is going to register the AXI Ethernet MAC of the ADVA TimeCard X2 this way, as a child platform device described by a software node.

Add the corresponding MODULE_ALIAS so name-based instantiation can autoload the driver, and give the name a define now that it is used in more than one place.

Tested on the X2 with a local ptp_ocp change: with this patch the module is loaded and binds when ptp_ocp registers the device; without it the device stays unbound until xilinx_emac is loaded by hand.

Assisted-by LLM sparse
---
 drivers/net/ethernet/xilinx/xilinx_axienet_main.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +9 -1
index cfef8ad75909..ff1837103553 100644
@@ -48,6 +48,7 @@
 #include "xilinx_axienet.h"
 
 /* Descriptors defines for Tx and Rx DMA */
+#define AXIENET_DRIVER_NAME		"xilinx_axienet"
 #define TX_BD_NUM_DEFAULT		128
 #define RX_BD_NUM_DEFAULT		1024
 #define TX_BD_NUM_MIN			(MAX_SKB_FRAGS + 1)
@@ -3223,7 +3224,7 @@ static struct platform_driver axienet_driver = {
 	.remove = axienet_remove,
 	.shutdown = axienet_shutdown,
 	.driver = {
-		 .name = "xilinx_axienet",
+		 .name = AXIENET_DRIVER_NAME,
 		 .pm = &axienet_pm_ops,
 		 .of_match_table = axienet_of_match,
 	},
@@ -3231,6 +3232,13 @@ static struct platform_driver axienet_driver = {
 
 module_platform_driver(axienet_driver);
 
+/* The module is named xilinx_emac, the platform driver xilinx_axienet.  A
+ * device registered by name rather than from firmware advertises a
+ * platform:xilinx_axienet modalias, which without this matches no module:
+ * udev cannot autoload the driver and the device stays unbound.
+ */
+MODULE_ALIAS("platform:" AXIENET_DRIVER_NAME);
+
 MODULE_DESCRIPTION("Xilinx Axi Ethernet driver");
 MODULE_AUTHOR("Xilinx");
 MODULE_LICENSE("GPL");
Signature · 1 line
2.47.0
#2 lore raw

Re: [PATCH net-next 1/2] net: axienet: use device_property and fwnode APIs for probe-time config

In reply to Sagi Maimon

drivers/net/ethernet/xilinx/xilinx_axienet_main.c static int axienet_probe(struct platform_device *pdev) · line 2908

Quoted 4 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +2 -2
-	lp->switch_x_sgmii = of_property_read_bool(pdev->dev.of_node,
-						   "xlnx,switch-x-sgmii");
+	lp->switch_x_sgmii = device_property_read_bool(&pdev->dev,
+						       "xlnx,switch-x-sgmii");

What version of the core is being used:

6c8f06bb2e5147b2c25bdd726365df8416c13987

Author: Robert Hancock <[email protected]>
Date:   Fri Feb 12 18:23:56 2021 -0600

net: axienet: Support dynamic switching between 1000BaseX and SGMII

Newer versions of the Xilinx AXI Ethernet core (specifically version 7.2 or later) allow the core to be configured with a PHY interface mode of "Both", allowing either 1000BaseX or SGMII modes to be selected at runtime. Add support for this in the driver to allow better support for applications which can use both fiber and copper SFP modules.

If this property is not needed, please leave it as OF only.

drivers/net/ethernet/xilinx/xilinx_axienet_main.c static int axienet_probe(struct platform_device *pdev) · line 2911

Quoted 4 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +1 -1
 	/* Start with the proprietary, and broken phy_type */
-	ret = of_property_read_u32(pdev->dev.of_node, "xlnx,phy-type", &value);
+	ret = device_property_read_u32(&pdev->dev, "xlnx,phy-type", &value);

xlnx,phy-type:
description:
Do not use, but still accepted in preference to phy-mode.
deprecated: true

This property is deprecated. So it should remain an OF property and not get expanded in scope.

Andrew

#3 lore raw

Re: [PATCH net-next 1/2] net: axienet: use device_property and fwnode APIs for probe-time config

Quoted 14 lines · Andrew Lunn

drivers/net/ethernet/xilinx/xilinx_axienet_main.c static int axienet_probe(struct platform_device *pdev) · line 2908

Quoted 5 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +2 -2
-     lp->switch_x_sgmii = of_property_read_bool(pdev->dev.of_node,
-                                                "xlnx,switch-x-sgmii");
+     lp->switch_x_sgmii = device_property_read_bool(&pdev->dev,
+                                                    "xlnx,switch-x-sgmii");

What version of the core is being used:

6c8f06bb2e5147b2c25bdd726365df8416c13987

Author: Robert Hancock <[email protected]>
Date:   Fri Feb 12 18:23:56 2021 -0600

net: axienet: Support dynamic switching between 1000BaseX and SGMII

Quoted 8 lines

Newer versions of the Xilinx AXI Ethernet core (specifically version 7.2 or later) allow the core to be configured with a PHY interface mode of "Both", allowing either 1000BaseX or SGMII modes to be selected at runtime. Add support for this in the driver to allow better support for applications which can use both fiber and copper SFP modules.

If this property is not needed, please leave it as OF only.

It is not needed here. The X2's MAC is wired to the FPGA fabric over a fixed link in "internal" mode, so there is no SGMII/1000BASE-X switching to do.

Quoted 5 lines

drivers/net/ethernet/xilinx/xilinx_axienet_main.c static int axienet_probe(struct platform_device *pdev) · line 2911

Quoted 4 lines
      /* Start with the proprietary, and broken phy_type */
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +1 -1
-     ret = of_property_read_u32(pdev->dev.of_node, "xlnx,phy-type", &value);
+     ret = device_property_read_u32(&pdev->dev, "xlnx,phy-type", &value);
Quoted 8 lines

xlnx,phy-type:
description:
Do not use, but still accepted in preference to phy-mode.
deprecated: true

This property is deprecated. So it should remain an OF property and not get expanded in scope.

Agreed. v2 converts only what the software node provides: xlnx,rxmem, phy-mode, the MAC address and the PHY connection, for the fixed-link. xlnx,switch-x-sgmii and xlnx,phy-type stay OF only, and so do xlnx,txcsum, xlnx,rxcsum and the "dmas" test, which the X2 does not use either.

Thanks,
Sagi

Quoted 1 line

Andrew