Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 4 Jul 2018 03:02:54 +0000 (UTC)
From:      Oleksandr Tymoshenko <gonzo@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r335925 - head/sys/dev/usb/net
Message-ID:  <201807040302.w6432s4M086771@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: gonzo
Date: Wed Jul  4 03:02:53 2018
New Revision: 335925
URL: https://svnweb.freebsd.org/changeset/base/335925

Log:
  muge(4): add DTB blob as one more possible source of MAC address
  
  On FDT-enabled platforms check if DTB blob has MAC address configured by
  a boot loader. This information passed as a "local-mac-address" or
  "mac-address" property of the device node. For USB NICs node
  can be found by looking for compatibility string "usbVVV,PPP" where
  VVV - vendor id (hex) and PPP - product id (hex)
  
  Reviewed by:	emaste
  Differential Revision:	https://reviews.freebsd.org/D16117

Modified:
  head/sys/dev/usb/net/if_muge.c

Modified: head/sys/dev/usb/net/if_muge.c
==============================================================================
--- head/sys/dev/usb/net/if_muge.c	Wed Jul  4 02:47:16 2018	(r335924)
+++ head/sys/dev/usb/net/if_muge.c	Wed Jul  4 03:02:53 2018	(r335925)
@@ -95,6 +95,12 @@ __FBSDID("$FreeBSD$");
 
 #include "opt_platform.h"
 
+#ifdef FDT
+#include <dev/fdt/fdt_common.h>
+#include <dev/ofw/ofw_bus.h>
+#include <dev/ofw/ofw_bus_subr.h>
+#endif
+
 #include <dev/usb/usb.h>
 #include <dev/usb/usbdi.h>
 #include <dev/usb/usbdi_util.h>
@@ -1427,30 +1433,117 @@ tr_setup:
 	}
 }
 
+#ifdef FDT
 /**
- *	muge_attach_post - Called after the driver attached to the USB interface
- *	@ue: the USB ethernet device
+ *	muge_fdt_find_eth_node - find descendant node with required compatibility
+ *	@start: start node
+ *	@compatible: compatible string used to identify the node
  *
- *	This is where the chip is intialised for the first time.  This is
- *	different from the muge_init() function in that that one is designed to
- *	setup the H/W to match the UE settings and can be called after a reset.
+ *	Loop through all descendant nodes and return first match with required
+ *	compatibility.
  *
+ *	RETURNS:
+ *	Returns node's phandle on success -1 otherwise
  */
+static phandle_t
+muge_fdt_find_eth_node(phandle_t start, const char *compatible)
+{
+	phandle_t child, node;
+
+	/* Traverse through entire tree to find usb ethernet nodes. */
+	for (node = OF_child(start); node != 0; node = OF_peer(node)) {
+		if (ofw_bus_node_is_compatible(node, compatible))
+			return (node);
+		child = muge_fdt_find_eth_node(node, compatible);
+		if (child != -1)
+			return (child);
+	}
+
+	return (-1);
+}
+
+/**
+ *	muge_fdt_read_mac_property - read MAC address from node
+ *	@node: USB device node
+ *	@mac: memory to store MAC address to
+ *
+ *	Check for common properties that might contain MAC address
+ *	passed by boot loader.
+ *
+ *	RETURNS:
+ *	Returns 0 on success, error code otherwise
+ */
+static int
+muge_fdt_read_mac_property(phandle_t node, unsigned char *mac)
+{
+	int len;
+
+	/* Check if there is property */
+	if ((len = OF_getproplen(node, "local-mac-address")) > 0) {
+		if (len != ETHER_ADDR_LEN)
+			return (EINVAL);
+
+		OF_getprop(node, "local-mac-address", mac,
+		    ETHER_ADDR_LEN);
+		return (0);
+	}
+
+	if ((len = OF_getproplen(node, "mac-address")) > 0) {
+		if (len != ETHER_ADDR_LEN)
+			return (EINVAL);
+
+		OF_getprop(node, "mac-address", mac,
+		    ETHER_ADDR_LEN);
+		return (0);
+	}
+
+	return (ENXIO);
+}
+
+/**
+ *	muge_fdt_find_mac - read MAC address from node
+ *	@compatible: compatible string for DTB node in the form "usb[N]NNN,[M]MMM"
+ *	    where NNN is vendor id and MMM is product id
+ *	@mac: memory to store MAC address to
+ *
+ *	Tries to find matching node in DTS and obtain MAC address info from it
+ *
+ *	RETURNS:
+ *	Returns 0 on success, error code otherwise
+ */
+static int
+muge_fdt_find_mac(const char *compatible, unsigned char *mac)
+{
+	phandle_t node, root;
+
+	root = OF_finddevice("/");
+	node = muge_fdt_find_eth_node(root, compatible);
+	if (node != -1) {
+		if (muge_fdt_read_mac_property(node, mac) == 0)
+			return (0);
+	}
+
+	return (ENXIO);
+}
+#endif
+
+/**
+ *	muge_set_mac_addr - Initiailizes NIC MAC address
+ *	@ue: the USB ethernet device
+ *
+ *	Tries to obtain MAC address from number of sources: registers,
+ *	EEPROM, DTB blob. If all sources fail - generates random MAC.
+ */
 static void
-muge_attach_post(struct usb_ether *ue)
+muge_set_mac_addr(struct usb_ether *ue)
 {
 	struct muge_softc *sc = uether_getsc(ue);
 	uint32_t mac_h, mac_l;
-	muge_dbg_printf(sc, "Calling muge_attach_post.\n");
+#ifdef FDT
+	char compatible[16];
+	struct usb_attach_arg *uaa = device_get_ivars(ue->ue_dev);
+#endif
 
-	/* Setup some of the basics */
-	sc->sc_phyno = 1;
-
-	/*
-	 * Attempt to get the mac address, if an EEPROM is not attached this
-	 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
-	 * address based on urandom.
-	 */
 	memset(sc->sc_ue.ue_eaddr, 0xff, ETHER_ADDR_LEN);
 
 	uint32_t val;
@@ -1468,29 +1561,57 @@ muge_attach_post(struct usb_ether *ue)
 	}
 
 	/* If RX_ADDRx did not provide a valid MAC address, try EEPROM. */
-	if (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr)) {
-		if ((lan78xx_eeprom_present(sc) &&
-		    lan78xx_eeprom_read_raw(sc, ETH_E2P_MAC_OFFSET,
-		    sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN) == 0) ||
-		    (lan78xx_otp_read(sc, OTP_MAC_OFFSET,
-		    sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN) == 0)) {
-			if (ETHER_IS_VALID(sc->sc_ue.ue_eaddr)) {
-				muge_dbg_printf(sc, "MAC read from EEPROM\n");
-			} else {
-				muge_dbg_printf(sc, "MAC assigned randomly\n");
-				read_random(sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN);
-				sc->sc_ue.ue_eaddr[0] &= ~0x01;	/* unicast */
-				sc->sc_ue.ue_eaddr[0] |= 0x02;/* locally administered */
-			}
-		} else {
-			muge_dbg_printf(sc, "MAC assigned randomly\n");
-			arc4rand(sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN, 0);
-			sc->sc_ue.ue_eaddr[0] &= ~0x01;	/* unicast */
-			sc->sc_ue.ue_eaddr[0] |= 0x02;	/* locally administered */
-		}
-	} else {
+	if (ETHER_IS_VALID(sc->sc_ue.ue_eaddr)) {
 		muge_dbg_printf(sc, "MAC assigned from registers\n");
+		return;
 	}
+
+	if ((lan78xx_eeprom_present(sc) &&
+	    lan78xx_eeprom_read_raw(sc, ETH_E2P_MAC_OFFSET,
+	    sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN) == 0) ||
+	    (lan78xx_otp_read(sc, OTP_MAC_OFFSET,
+	    sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN) == 0)) {
+		if (ETHER_IS_VALID(sc->sc_ue.ue_eaddr)) {
+			muge_dbg_printf(sc, "MAC read from EEPROM\n");
+			return;
+		}
+	}
+
+#ifdef FDT
+	snprintf(compatible, sizeof(compatible), "usb%x,%x",
+	    uaa->info.idVendor, uaa->info.idProduct);
+	if (muge_fdt_find_mac(compatible, sc->sc_ue.ue_eaddr) == 0) {
+		muge_dbg_printf(sc, "MAC assigned from FDT blob\n");
+		return;
+	}
+#endif
+
+	muge_dbg_printf(sc, "MAC assigned randomly\n");
+	arc4rand(sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN, 0);
+	sc->sc_ue.ue_eaddr[0] &= ~0x01;	/* unicast */
+	sc->sc_ue.ue_eaddr[0] |= 0x02;	/* locally administered */
+}
+
+/**
+ *	muge_attach_post - Called after the driver attached to the USB interface
+ *	@ue: the USB ethernet device
+ *
+ *	This is where the chip is intialised for the first time.  This is
+ *	different from the muge_init() function in that that one is designed to
+ *	setup the H/W to match the UE settings and can be called after a reset.
+ *
+ */
+static void
+muge_attach_post(struct usb_ether *ue)
+{
+	struct muge_softc *sc = uether_getsc(ue);
+
+	muge_dbg_printf(sc, "Calling muge_attach_post.\n");
+
+	/* Setup some of the basics */
+	sc->sc_phyno = 1;
+
+	muge_set_mac_addr(ue);
 
 	/* Initialise the chip for the first time */
 	lan78xx_chip_init(sc);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201807040302.w6432s4M086771>