From owner-svn-src-head@freebsd.org Wed Jul 4 03:02:54 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id B7B451024DC2; Wed, 4 Jul 2018 03:02:54 +0000 (UTC) (envelope-from gonzo@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 8F95D80888; Wed, 4 Jul 2018 03:02:54 +0000 (UTC) (envelope-from gonzo@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 72340250F6; Wed, 4 Jul 2018 03:02:54 +0000 (UTC) (envelope-from gonzo@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w6432seQ086772; Wed, 4 Jul 2018 03:02:54 GMT (envelope-from gonzo@FreeBSD.org) Received: (from gonzo@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w6432s4M086771; Wed, 4 Jul 2018 03:02:54 GMT (envelope-from gonzo@FreeBSD.org) Message-Id: <201807040302.w6432s4M086771@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: gonzo set sender to gonzo@FreeBSD.org using -f From: Oleksandr Tymoshenko Date: Wed, 4 Jul 2018 03:02:54 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r335925 - head/sys/dev/usb/net X-SVN-Group: head X-SVN-Commit-Author: gonzo X-SVN-Commit-Paths: head/sys/dev/usb/net X-SVN-Commit-Revision: 335925 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 04 Jul 2018 03:02:55 -0000 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 +#include +#include +#endif + #include #include #include @@ -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);