linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] tg3: optionally get mac address from devicetree
@ 2018-11-13 16:15 thesven73
  2018-11-13 21:09 ` Andrew Lunn
  2018-11-17  3:37 ` David Miller
  0 siblings, 2 replies; 3+ messages in thread
From: thesven73 @ 2018-11-13 16:15 UTC (permalink / raw)
  To: svendev, siva.kallam, prashant, mchan; +Cc: davem, linux-kernel, netdev, arnd

If the tg3 has a device node, and that node contains a valid
mac address property, use that as the tg3's mac address.

This behaviour was previously only present on SPARC, using a
conditional compile (#ifdef CONFIG_SPARC), presumably because
at the time, devicetree nodes for pci devices only worked on
SPARC. However, this has recently been made universal, see
commit 98d9f30c820d ("pci/of: Match PCI devices to OF nodes dynamically")

Devicetree example:
(see Documentation/devicetree/bindings/pci/pci.txt)

&pcie {
	host@0 {
		#address-cells = <3>;
		#size-cells = <2>;
		reg = <0 0 0 0 0>;
		bcm5778: bcm5778@0 {
			reg = <0 0 0 0 0>;
			mac-address = [CA 11 AB 1E 10 01];
		};
	};
};

Signed-off-by: Sven Van Asbroeck <svendev@arcx.com>
---
 drivers/net/ethernet/broadcom/tg3.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)

diff --git a/drivers/net/ethernet/broadcom/tg3.c b/drivers/net/ethernet/broadcom/tg3.c
index 89295306f161..b60381a70454 100644
--- a/drivers/net/ethernet/broadcom/tg3.c
+++ b/drivers/net/ethernet/broadcom/tg3.c
@@ -55,6 +55,7 @@
 #include <linux/hwmon.h>
 #include <linux/hwmon-sysfs.h>
 #include <linux/crc32poly.h>
+#include <linux/of_net.h>
 
 #include <net/checksum.h>
 #include <net/ip.h>
@@ -16959,23 +16960,21 @@ static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
 	return err;
 }
 
-#ifdef CONFIG_SPARC
-static int tg3_get_macaddr_sparc(struct tg3 *tp)
+static int tg3_of_get_macaddr(struct tg3 *tp)
 {
-	struct net_device *dev = tp->dev;
-	struct pci_dev *pdev = tp->pdev;
-	struct device_node *dp = pci_device_to_OF_node(pdev);
-	const unsigned char *addr;
-	int len;
+	struct device_node *np = pci_device_to_OF_node(tp->pdev);
+	const void *mac;
 
-	addr = of_get_property(dp, "local-mac-address", &len);
-	if (addr && len == ETH_ALEN) {
-		memcpy(dev->dev_addr, addr, ETH_ALEN);
-		return 0;
-	}
-	return -ENODEV;
+	if (!np)
+		return -ENODEV;
+	mac = of_get_mac_address(np);
+	if (!mac || !is_valid_ether_addr(mac))
+		return -EINVAL;
+	memcpy(tp->dev->dev_addr, mac, ETH_ALEN);
+	return 0;
 }
 
+#ifdef CONFIG_SPARC
 static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
 {
 	struct net_device *dev = tp->dev;
@@ -16992,10 +16991,8 @@ static int tg3_get_device_address(struct tg3 *tp)
 	int addr_ok = 0;
 	int err;
 
-#ifdef CONFIG_SPARC
-	if (!tg3_get_macaddr_sparc(tp))
+	if (!tg3_of_get_macaddr(tp))
 		return 0;
-#endif
 
 	if (tg3_flag(tp, IS_SSB_CORE)) {
 		err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v1] tg3: optionally get mac address from devicetree
  2018-11-13 16:15 [PATCH v1] tg3: optionally get mac address from devicetree thesven73
@ 2018-11-13 21:09 ` Andrew Lunn
  2018-11-17  3:37 ` David Miller
  1 sibling, 0 replies; 3+ messages in thread
From: Andrew Lunn @ 2018-11-13 21:09 UTC (permalink / raw)
  To: thesven73
  Cc: svendev, siva.kallam, prashant, mchan, davem, linux-kernel, netdev, arnd

> +static int tg3_of_get_macaddr(struct tg3 *tp)
>  {
> -	struct net_device *dev = tp->dev;
> -	struct pci_dev *pdev = tp->pdev;
> -	struct device_node *dp = pci_device_to_OF_node(pdev);
> -	const unsigned char *addr;
> -	int len;
> +	struct device_node *np = pci_device_to_OF_node(tp->pdev);
> +	const void *mac;
>  
> -	addr = of_get_property(dp, "local-mac-address", &len);
> -	if (addr && len == ETH_ALEN) {
> -		memcpy(dev->dev_addr, addr, ETH_ALEN);
> -		return 0;
> -	}
> -	return -ENODEV;
> +	if (!np)
> +		return -ENODEV;
> +	mac = of_get_mac_address(np);
> +	if (!mac || !is_valid_ether_addr(mac))
> +		return -EINVAL;

Hi Sven

If i'm reading of_get_mac_address() correctly, there is no need to
call is_valid_ether_addr() afterwards. It does it already.

     Andrew

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v1] tg3: optionally get mac address from devicetree
  2018-11-13 16:15 [PATCH v1] tg3: optionally get mac address from devicetree thesven73
  2018-11-13 21:09 ` Andrew Lunn
@ 2018-11-17  3:37 ` David Miller
  1 sibling, 0 replies; 3+ messages in thread
From: David Miller @ 2018-11-17  3:37 UTC (permalink / raw)
  To: thesven73
  Cc: svendev, siva.kallam, prashant, mchan, linux-kernel, netdev, arnd

From: thesven73@gmail.com
Date: Tue, 13 Nov 2018 11:15:08 -0500

> If the tg3 has a device node, and that node contains a valid
> mac address property, use that as the tg3's mac address.
> 
> This behaviour was previously only present on SPARC, using a
> conditional compile (#ifdef CONFIG_SPARC), presumably because
> at the time, devicetree nodes for pci devices only worked on
> SPARC. However, this has recently been made universal, see
> commit 98d9f30c820d ("pci/of: Match PCI devices to OF nodes dynamically")
> 
> Devicetree example:
> (see Documentation/devicetree/bindings/pci/pci.txt)
> 
> &pcie {
> 	host@0 {
> 		#address-cells = <3>;
> 		#size-cells = <2>;
> 		reg = <0 0 0 0 0>;
> 		bcm5778: bcm5778@0 {
> 			reg = <0 0 0 0 0>;
> 			mac-address = [CA 11 AB 1E 10 01];
> 		};
> 	};
> };
> 
> Signed-off-by: Sven Van Asbroeck <svendev@arcx.com>

Please use eth_platform_get_mac_address() and you can delete all of the
SPARC specific code altogether.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-11-17  3:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-13 16:15 [PATCH v1] tg3: optionally get mac address from devicetree thesven73
2018-11-13 21:09 ` Andrew Lunn
2018-11-17  3:37 ` David Miller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).