Skip to contentSkip to conversation

net: eth-uclass: Write MAC address to hardware after probe

PATCH In review Thierry Reding 3 participants 7 messages May 20, 2019 – Jun 1, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 net: eth-uclass: Write MAC address to hardware after probe

From: Thierry Reding <[email protected]>

In order for the device to use the proper MAC address, which can have been configured in the environment prior to the device being registered, ensure that the MAC address is written after the device has been probed. For devices that are registered before the network stack is initialized, this is already done during eth_initialize(). If the Ethernet device is on a bus that is not initialized on early boot, such as PCI, the device is not available at the time eth_initialize() is called, so we need the MAC address programming to also happen after probe.

---
 net/eth-uclass.c | 2 ++
 1 file changed, 2 insertions(+)
net/eth-uclass.c +2 -0
index 2ef20df19203..4225aabf1fa1 100644
@@ -524,6 +524,8 @@ static int eth_post_probe(struct udevice *dev)
 #endif
 	}
 
+	eth_write_hwaddr(dev);
+
 	return 0;
 }
 
Signature · 1 line
2.21.0
#1 lore raw

PATCH v4 2/2 net: eth-uclass: Support device tree MAC addresses

From: Thierry Reding <[email protected]>

Add the standard Ethernet device tree bindings (imported from v5.0 of the Linux kernel) and implement support for reading the MAC address for Ethernet devices in the Ethernet uclass. If the "mac-address" property exists, the MAC address will be parsed from that. If that property does not exist, the "local-mac-address" property will be tried as fallback.

MAC addresses from device tree take precedence over the ones stored in a network interface card's ROM.

---
Changes in v4:
- do not clear MAC address on failure to avoid overwriting existing MAC
- fix build on xtensa (missing CONFIG_OF_CONTROL)

Changes in v3:
- add additional check to make sure the MAC address read from device tree is a valid MAC address

Changes in v2:
- use dev_read_u8_array_ptr()

---
 .../devicetree/bindings/net/ethernet.txt      | 66 +++++++++++++++++++
 net/eth-uclass.c                              | 30 ++++++++-
 2 files changed, 93 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/net/ethernet.txt
Documentation/devicetree/bindings/net/ethernet.txt new file +66 -0
new file mode 100644
index 000000000000..cfc376bc977a
@@ -0,0 +1,66 @@
+The following properties are common to the Ethernet controllers:
+
+NOTE: All 'phy*' properties documented below are Ethernet specific. For the
+generic PHY 'phys' property, see
+Documentation/devicetree/bindings/phy/phy-bindings.txt.
+
+- local-mac-address: array of 6 bytes, specifies the MAC address that was
+  assigned to the network device;
+- mac-address: array of 6 bytes, specifies the MAC address that was last used by
+  the boot program; should be used in cases where the MAC address assigned to
+  the device by the boot program is different from the "local-mac-address"
+  property;
+- nvmem-cells: phandle, reference to an nvmem node for the MAC address;
+- nvmem-cell-names: string, should be "mac-address" if nvmem is to be used;
+- max-speed: number, specifies maximum speed in Mbit/s supported by the device;
+- max-frame-size: number, maximum transfer unit (IEEE defined MTU), rather than
+  the maximum frame size (there's contradiction in the Devicetree
+  Specification).
+- phy-mode: string, operation mode of the PHY interface. This is now a de-facto
+  standard property; supported values are:
+  * "internal"
+  * "mii"
+  * "gmii"
+  * "sgmii"
+  * "qsgmii"
+  * "tbi"
+  * "rev-mii"
+  * "rmii"
+  * "rgmii" (RX and TX delays are added by the MAC when required)
+  * "rgmii-id" (RGMII with internal RX and TX delays provided by the PHY, the
+     MAC should not add the RX or TX delays in this case)
+  * "rgmii-rxid" (RGMII with internal RX delay provided by the PHY, the MAC
+     should not add an RX delay in this case)
+  * "rgmii-txid" (RGMII with internal TX delay provided by the PHY, the MAC
+     should not add an TX delay in this case)
+  * "rtbi"
+  * "smii"
+  * "xgmii"
+  * "trgmii"
+  * "2000base-x",
+  * "2500base-x",
+  * "rxaui"
+  * "xaui"
+  * "10gbase-kr" (10GBASE-KR, XFI, SFI)
+- phy-connection-type: the same as "phy-mode" property but described in the
+  Devicetree Specification;
+- phy-handle: phandle, specifies a reference to a node representing a PHY
+  device; this property is described in the Devicetree Specification and so
+  preferred;
+- phy: the same as "phy-handle" property, not recommended for new bindings.
+- phy-device: the same as "phy-handle" property, not recommended for new
+  bindings.
+- rx-fifo-depth: the size of the controller's receive fifo in bytes. This
+  is used for components that can have configurable receive fifo sizes,
+  and is useful for determining certain configuration settings such as
+  flow control thresholds.
+- tx-fifo-depth: the size of the controller's transmit fifo in bytes. This
+  is used for components that can have configurable fifo sizes.
+- managed: string, specifies the PHY management type. Supported values are:
+  "auto", "in-band-status". "auto" is the default, it usess MDIO for
+  management if fixed-link is not specified.
+
+Child nodes of the Ethernet controller are typically the individual PHY devices
+connected via the MDIO bus (sometimes the MDIO bus controller is separate).
+They are described in the phy.txt file in this same directory.
+For non-MDIO PHY management see fixed-link.txt.
net/eth-uclass.c +27 -3
index 4225aabf1fa1..031d55862583 100644
@@ -455,6 +455,26 @@ static int eth_pre_unbind(struct udevice *dev)
 	return 0;
 }
 
+static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
+{
+#if IS_ENABLED(CONFIG_OF_CONTROL)
+	const uint8_t *p;
+
+	p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
+	if (!p)
+		p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
+
+	if (!p)
+		return false;
+
+	memcpy(mac, p, ARP_HLEN);
+
+	return true;
+#else
+	return false;
+#endif
+}
+
 static int eth_post_probe(struct udevice *dev)
 {
 	struct eth_device_priv *priv = dev->uclass_priv;
@@ -489,9 +509,13 @@ static int eth_post_probe(struct udevice *dev)
 
 	priv->state = ETH_STATE_INIT;
 
-	/* Check if the device has a MAC address in ROM */
-	if (eth_get_ops(dev)->read_rom_hwaddr)
-		eth_get_ops(dev)->read_rom_hwaddr(dev);
+	/* Check if the device has a valid MAC address in device tree */
+	if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
+	    !is_valid_ethaddr(pdata->enetaddr)) {
+		/* Check if the device has a MAC address in ROM */
+		if (eth_get_ops(dev)->read_rom_hwaddr)
+			eth_get_ops(dev)->read_rom_hwaddr(dev);
+	}
 
 	eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
 	if (!is_zero_ethaddr(env_enetaddr)) {
Signature · 1 line
2.21.0
#2 lore raw

PATCH v4 2/2 net: eth-uclass: Support device tree MAC addresses

Quoted 23 lines · Thierry Reding

From: Thierry Reding <[email protected]>

Add the standard Ethernet device tree bindings (imported from v5.0 of the Linux kernel) and implement support for reading the MAC address for Ethernet devices in the Ethernet uclass. If the "mac-address" property exists, the MAC address will be parsed from that. If that property does not exist, the "local-mac-address" property will be tried as fallback.

MAC addresses from device tree take precedence over the ones stored in a network interface card's ROM.

---
Changes in v4:
- do not clear MAC address on failure to avoid overwriting existing MAC
- fix build on xtensa (missing CONFIG_OF_CONTROL)

Changes in v3:
- add additional check to make sure the MAC address read from device tree is a valid MAC address

Quoted 7 lines

Changes in v2:
- use dev_read_u8_array_ptr()

---
 .../devicetree/bindings/net/ethernet.txt      | 66 +++++++++++++++++++
 net/eth-uclass.c                              | 30 ++++++++-
 2 files changed, 93 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/net/ethernet.txt

Hi Joe,

did you have a chance to look at this updated version? I've manually verified that the qemu-x86_64 test failure is no longer there and the build failure for xtensa is also gone.

A fairly largish series for Tom's Tegra tree is currently blocked on this because without these two patches it will cause a test failure.

Thierry

Quoted 123 lines
Documentation/devicetree/bindings/net/ethernet.txt new file +66 -0
new file mode 100644
index 000000000000..cfc376bc977a
@@ -0,0 +1,66 @@
+The following properties are common to the Ethernet controllers:
+
+NOTE: All 'phy*' properties documented below are Ethernet specific. For the
+generic PHY 'phys' property, see
+Documentation/devicetree/bindings/phy/phy-bindings.txt.
+
+- local-mac-address: array of 6 bytes, specifies the MAC address that was
+  assigned to the network device;
+- mac-address: array of 6 bytes, specifies the MAC address that was last used by
+  the boot program; should be used in cases where the MAC address assigned to
+  the device by the boot program is different from the "local-mac-address"
+  property;
+- nvmem-cells: phandle, reference to an nvmem node for the MAC address;
+- nvmem-cell-names: string, should be "mac-address" if nvmem is to be used;
+- max-speed: number, specifies maximum speed in Mbit/s supported by the device;
+- max-frame-size: number, maximum transfer unit (IEEE defined MTU), rather than
+  the maximum frame size (there's contradiction in the Devicetree
+  Specification).
+- phy-mode: string, operation mode of the PHY interface. This is now a de-facto
+  standard property; supported values are:
+  * "internal"
+  * "mii"
+  * "gmii"
+  * "sgmii"
+  * "qsgmii"
+  * "tbi"
+  * "rev-mii"
+  * "rmii"
+  * "rgmii" (RX and TX delays are added by the MAC when required)
+  * "rgmii-id" (RGMII with internal RX and TX delays provided by the PHY, the
+     MAC should not add the RX or TX delays in this case)
+  * "rgmii-rxid" (RGMII with internal RX delay provided by the PHY, the MAC
+     should not add an RX delay in this case)
+  * "rgmii-txid" (RGMII with internal TX delay provided by the PHY, the MAC
+     should not add an TX delay in this case)
+  * "rtbi"
+  * "smii"
+  * "xgmii"
+  * "trgmii"
+  * "2000base-x",
+  * "2500base-x",
+  * "rxaui"
+  * "xaui"
+  * "10gbase-kr" (10GBASE-KR, XFI, SFI)
+- phy-connection-type: the same as "phy-mode" property but described in the
+  Devicetree Specification;
+- phy-handle: phandle, specifies a reference to a node representing a PHY
+  device; this property is described in the Devicetree Specification and so
+  preferred;
+- phy: the same as "phy-handle" property, not recommended for new bindings.
+- phy-device: the same as "phy-handle" property, not recommended for new
+  bindings.
+- rx-fifo-depth: the size of the controller's receive fifo in bytes. This
+  is used for components that can have configurable receive fifo sizes,
+  and is useful for determining certain configuration settings such as
+  flow control thresholds.
+- tx-fifo-depth: the size of the controller's transmit fifo in bytes. This
+  is used for components that can have configurable fifo sizes.
+- managed: string, specifies the PHY management type. Supported values are:
+  "auto", "in-band-status". "auto" is the default, it usess MDIO for
+  management if fixed-link is not specified.
+
+Child nodes of the Ethernet controller are typically the individual PHY devices
+connected via the MDIO bus (sometimes the MDIO bus controller is separate).
+They are described in the phy.txt file in this same directory.
+For non-MDIO PHY management see fixed-link.txt.
net/eth-uclass.c +27 -3
index 4225aabf1fa1..031d55862583 100644
@@ -455,6 +455,26 @@ static int eth_pre_unbind(struct udevice *dev)
 	return 0;
 }
 
+static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
+{
+#if IS_ENABLED(CONFIG_OF_CONTROL)
+	const uint8_t *p;
+
+	p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
+	if (!p)
+		p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
+
+	if (!p)
+		return false;
+
+	memcpy(mac, p, ARP_HLEN);
+
+	return true;
+#else
+	return false;
+#endif
+}
+
 static int eth_post_probe(struct udevice *dev)
 {
 	struct eth_device_priv *priv = dev->uclass_priv;
@@ -489,9 +509,13 @@ static int eth_post_probe(struct udevice *dev)
 
 	priv->state = ETH_STATE_INIT;
 
-	/* Check if the device has a MAC address in ROM */
-	if (eth_get_ops(dev)->read_rom_hwaddr)
-		eth_get_ops(dev)->read_rom_hwaddr(dev);
+	/* Check if the device has a valid MAC address in device tree */
+	if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
+	    !is_valid_ethaddr(pdata->enetaddr)) {
+		/* Check if the device has a MAC address in ROM */
+		if (eth_get_ops(dev)->read_rom_hwaddr)
+			eth_get_ops(dev)->read_rom_hwaddr(dev);
+	}
 
 	eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
 	if (!is_zero_ethaddr(env_enetaddr)) {
Signature · 1 line
2.21.0

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190528/b1ad5630/attachment.sig>

#3 lore raw

PATCH v4 2/2 net: eth-uclass: Support device tree MAC addresses

Quoted 33 lines · Thierry Reding
Quoted 23 lines · Thierry Reding

From: Thierry Reding <[email protected]>

Add the standard Ethernet device tree bindings (imported from v5.0 of the Linux kernel) and implement support for reading the MAC address for Ethernet devices in the Ethernet uclass. If the "mac-address" property exists, the MAC address will be parsed from that. If that property does not exist, the "local-mac-address" property will be tried as fallback.

MAC addresses from device tree take precedence over the ones stored in a network interface card's ROM.

---
Changes in v4:
- do not clear MAC address on failure to avoid overwriting existing MAC
- fix build on xtensa (missing CONFIG_OF_CONTROL)

Changes in v3:
- add additional check to make sure the MAC address read from device tree is a valid MAC address

Quoted 7 lines

Changes in v2:
- use dev_read_u8_array_ptr()

---
 .../devicetree/bindings/net/ethernet.txt      | 66 +++++++++++++++++++
 net/eth-uclass.c                              | 30 ++++++++-
 2 files changed, 93 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/net/ethernet.txt
Quoted 8 lines

Hi Joe,

did you have a chance to look at this updated version? I've manually verified that the qemu-x86_64 test failure is no longer there and the build failure for xtensa is also gone.

A fairly largish series for Tom's Tegra tree is currently blocked on this because without these two patches it will cause a test failure.

I've tested v2 of this series, I have on my list to rebase and test v4 later this week if that will help.

Peter

Quoted 129 lines

Thierry

Quoted 121 lines
Documentation/devicetree/bindings/net/ethernet.txt new file +66 -0
new file mode 100644
index 000000000000..cfc376bc977a
@@ -0,0 +1,66 @@
+The following properties are common to the Ethernet controllers:
+
+NOTE: All 'phy*' properties documented below are Ethernet specific. For the
+generic PHY 'phys' property, see
+Documentation/devicetree/bindings/phy/phy-bindings.txt.
+
+- local-mac-address: array of 6 bytes, specifies the MAC address that was
+  assigned to the network device;
+- mac-address: array of 6 bytes, specifies the MAC address that was last used by
+  the boot program; should be used in cases where the MAC address assigned to
+  the device by the boot program is different from the "local-mac-address"
+  property;
+- nvmem-cells: phandle, reference to an nvmem node for the MAC address;
+- nvmem-cell-names: string, should be "mac-address" if nvmem is to be used;
+- max-speed: number, specifies maximum speed in Mbit/s supported by the device;
+- max-frame-size: number, maximum transfer unit (IEEE defined MTU), rather than
+  the maximum frame size (there's contradiction in the Devicetree
+  Specification).
+- phy-mode: string, operation mode of the PHY interface. This is now a de-facto
+  standard property; supported values are:
+  * "internal"
+  * "mii"
+  * "gmii"
+  * "sgmii"
+  * "qsgmii"
+  * "tbi"
+  * "rev-mii"
+  * "rmii"
+  * "rgmii" (RX and TX delays are added by the MAC when required)
+  * "rgmii-id" (RGMII with internal RX and TX delays provided by the PHY, the
+     MAC should not add the RX or TX delays in this case)
+  * "rgmii-rxid" (RGMII with internal RX delay provided by the PHY, the MAC
+     should not add an RX delay in this case)
+  * "rgmii-txid" (RGMII with internal TX delay provided by the PHY, the MAC
+     should not add an TX delay in this case)
+  * "rtbi"
+  * "smii"
+  * "xgmii"
+  * "trgmii"
+  * "2000base-x",
+  * "2500base-x",
+  * "rxaui"
+  * "xaui"
+  * "10gbase-kr" (10GBASE-KR, XFI, SFI)
+- phy-connection-type: the same as "phy-mode" property but described in the
+  Devicetree Specification;
+- phy-handle: phandle, specifies a reference to a node representing a PHY
+  device; this property is described in the Devicetree Specification and so
+  preferred;
+- phy: the same as "phy-handle" property, not recommended for new bindings.
+- phy-device: the same as "phy-handle" property, not recommended for new
+  bindings.
+- rx-fifo-depth: the size of the controller's receive fifo in bytes. This
+  is used for components that can have configurable receive fifo sizes,
+  and is useful for determining certain configuration settings such as
+  flow control thresholds.
+- tx-fifo-depth: the size of the controller's transmit fifo in bytes. This
+  is used for components that can have configurable fifo sizes.
+- managed: string, specifies the PHY management type. Supported values are:
+  "auto", "in-band-status". "auto" is the default, it usess MDIO for
+  management if fixed-link is not specified.
+
+Child nodes of the Ethernet controller are typically the individual PHY devices
+connected via the MDIO bus (sometimes the MDIO bus controller is separate).
+They are described in the phy.txt file in this same directory.
+For non-MDIO PHY management see fixed-link.txt.
net/eth-uclass.c +27 -4
index 4225aabf1fa1..031d55862583 100644
@@ -455,6 +455,26 @@ static int eth_pre_unbind(struct udevice *dev)
      return 0;
 }
 
+static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
+{
+#if IS_ENABLED(CONFIG_OF_CONTROL)
+     const uint8_t *p;
+
+     p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
+     if (!p)
+             p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
+
+     if (!p)
+             return false;
+
+     memcpy(mac, p, ARP_HLEN);
+
+     return true;
+#else
+     return false;
+#endif
+}
+
 static int eth_post_probe(struct udevice *dev)
 {
      struct eth_device_priv *priv = dev->uclass_priv;
@@ -489,9 +509,13 @@ static int eth_post_probe(struct udevice *dev)
 
      priv->state = ETH_STATE_INIT;
 
-     /* Check if the device has a MAC address in ROM */
-     if (eth_get_ops(dev)->read_rom_hwaddr)
-             eth_get_ops(dev)->read_rom_hwaddr(dev);
+     /* Check if the device has a valid MAC address in device tree */
+     if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
+         !is_valid_ethaddr(pdata->enetaddr)) {
+             /* Check if the device has a MAC address in ROM */
+             if (eth_get_ops(dev)->read_rom_hwaddr)
+                     eth_get_ops(dev)->read_rom_hwaddr(dev);
+     }
 
      eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
      if (!is_zero_ethaddr(env_enetaddr)) {
--
Quoted 2 lines

2.21.0

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
https://lists.denx.de/listinfo/u-boot

#4 lore raw

PATCH v4 2/2 net: eth-uclass: Support device tree MAC addresses

In reply to Thierry Reding
Quoted 33 lines · Thierry Reding
Quoted 23 lines · Thierry Reding

From: Thierry Reding <[email protected]>

Add the standard Ethernet device tree bindings (imported from v5.0 of the Linux kernel) and implement support for reading the MAC address for Ethernet devices in the Ethernet uclass. If the "mac-address" property exists, the MAC address will be parsed from that. If that property does not exist, the "local-mac-address" property will be tried as fallback.

MAC addresses from device tree take precedence over the ones stored in a network interface card's ROM.

---
Changes in v4:
- do not clear MAC address on failure to avoid overwriting existing MAC
- fix build on xtensa (missing CONFIG_OF_CONTROL)

Changes in v3:
- add additional check to make sure the MAC address read from device tree is a valid MAC address

Quoted 7 lines

Changes in v2:
- use dev_read_u8_array_ptr()

---
 .../devicetree/bindings/net/ethernet.txt      | 66 +++++++++++++++++++
 net/eth-uclass.c                              | 30 ++++++++-
 2 files changed, 93 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/net/ethernet.txt
Quoted 8 lines

Hi Joe,

did you have a chance to look at this updated version? I've manually verified that the qemu-x86_64 test failure is no longer there and the build failure for xtensa is also gone.

A fairly largish series for Tom's Tegra tree is currently blocked on this because without these two patches it will cause a test failure.

I'll be build testing this soon.

Cheers,
-Joe