From nobody Thu Dec 28 14:41:16 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4T1B5m64pzz55xZc; Thu, 28 Dec 2023 14:41:16 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4T1B5m5cgBz4g6T; Thu, 28 Dec 2023 14:41:16 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703774476; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=F8sWonuwEAe3Q0V686+5/ScPfRpdrCjqyQjunR58iuU=; b=kkEP91eJ8DGHDbiyO/nZOu7ztbTCt0aWmDfN9xfhmWL3wrzuAm4pIc9CkI1ZfEtJdKls0/ 4h//+AYbtE5fqP6WlURMC29MBh1c/ZO+7XD8O4u3KCtMtbsAjP0htrXBE2ak8l6fFuPLy3 KvpLG70ThQSGJCZSlMwxYQZ51X79iBopPiwt4yQbN9AHXt1x0wk+GEoq0gZAlZ+Qe6roc0 YQSVrNvadguRE7Cbzs2dkjegyDRAYWjhypD/AMAL3lKmMbNCLqq6Mlbsb5N1Qxunjx1QZC Zm7U2as07XVUkMM9nPnbooMnJbKYAwn38+7kusHrnO0W5PNkYYEQw6DVk7wh3A== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1703774476; a=rsa-sha256; cv=none; b=WPMhUFBJcrUG7D7fDZrr0qopQ3ThJthGAJOvzigcU1NQisL62OsTuvoIdzInE2i5DJaABB c7zaLOngmUzlZeCy2bmYgLZbBeXsJ50RiS+GPG720112/BFXAnNexow2lptvLBlRfStyVR Sn4u3mZ3In0SuoxVaE2FFvQJLvkXuRND+b+KIJlizwVy3u35mYrUpC6wPFiyxDoevdgT1M spmuQaTtakRSZkwvJYbrVHhSXH7q2zDSD1bqXONtYYNc84SG4SV5dbbr1lGHs+fiCr+sWD 5fnkwYe5Ara0q5g0iWDDHpUbf9bUbOLCIKazRmDvg6D7jMVxSFi+f3lcLS0lNg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703774476; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=F8sWonuwEAe3Q0V686+5/ScPfRpdrCjqyQjunR58iuU=; b=LhPw3kfxHYkp7xMreKqYOq02F43WDYrxF1qxPunzSOlZpEYgVjgoMNSAWOO9Ho1yarD2tc /UEmdMnICsdT9QXL3WhxpdoT7dvAy9JVuDD/LMu5vcH+/DjMZHDxVtkySb8GC/+GoZtd6D H8igaRg+bhIu6SZwI6q7ZrydSHFqBqR3PUnFI1vu4tYPQHYWbcsXIl0xIFsAwG7rz6GaQb Hd/LeTqiYgnnnxUNPF0xPwVImYowJCfLYr+sSvZO+nHeTbFyRhTcX25i+LcJE0wxv2kcSR z31Ts8o8hEIn2xOUrhYWkeXFXwH2137WGuaL0Xj7KNzTwJ56hsjEWlX/FdVG0Q== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4T1B5m4gh9zC8t; Thu, 28 Dec 2023 14:41:16 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3BSEfGXK013640; Thu, 28 Dec 2023 14:41:16 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BSEfGUl013637; Thu, 28 Dec 2023 14:41:16 GMT (envelope-from git) Date: Thu, 28 Dec 2023 14:41:16 GMT Message-Id: <202312281441.3BSEfGUl013637@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Ronald Klop Subject: git: 028e4c6548e4 - stable/14 - Teach if_smsc to get MAC from bootargs. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: ronald X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 028e4c6548e44f3d37210f0087bafb29d49704be Auto-Submitted: auto-generated The branch stable/14 has been updated by ronald: URL: https://cgit.FreeBSD.org/src/commit/?id=028e4c6548e44f3d37210f0087bafb29d49704be commit 028e4c6548e44f3d37210f0087bafb29d49704be Author: Ronald Klop AuthorDate: 2023-11-04 14:14:00 +0000 Commit: Ronald Klop CommitDate: 2023-12-28 14:40:31 +0000 Teach if_smsc to get MAC from bootargs. Some Raspberry Pi pass smsc95xx.macaddr=XX:XX:XX:XX:XX:XX as bootargs. Use this if no ethernet address is found in an EEPROM. As last resort fall back to ether_gen_addr() instead of random MAC. (cherry picked from commit 3878bbf1bb9e68f8579b57cde7d4e5c77de93320) if_smsc: fix build on armv6 & armv7 compile error was: /usr/src/sys/dev/usb/net/if_smsc.c:1597:40: error: format specifies type 'unsigned long' but the argument has type 'ssize_t' (aka 'int') [-Werror,-Wformat] "failed alloc for bootargs (%lu)", len); ~~~ ^~~ %zd (cherry picked from commit 8a0ee306227a17a998bdc7af2275fd94b9164342) PR: 274092 Reported by: Patrick M. Hausen (via ML) Reviewed by: imp, karels, zlei Tested by: Patrick M. Hausen Approved by: karels Relnotes: yes Differential Revision: https://reviews.freebsd.org/D42463 --- sys/dev/usb/net/if_smsc.c | 86 +++++++++++++++++++++++++++++++++++++++++++++-- sys/net/ethernet.h | 1 + sys/net/if_ethersubr.c | 10 ++++-- 3 files changed, 92 insertions(+), 5 deletions(-) diff --git a/sys/dev/usb/net/if_smsc.c b/sys/dev/usb/net/if_smsc.c index ec8197229f17..a59501b6bbff 100644 --- a/sys/dev/usb/net/if_smsc.c +++ b/sys/dev/usb/net/if_smsc.c @@ -179,6 +179,8 @@ static const struct usb_device_id smsc_devs[] = { #define ETHER_IS_VALID(addr) \ (!ETHER_IS_MULTICAST(addr) && !ETHER_IS_ZERO(addr)) +#define BOOTARGS_SMSC95XX "smsc95xx.macaddr" + static device_probe_t smsc_probe; static device_attach_t smsc_attach; static device_detach_t smsc_detach; @@ -1538,6 +1540,76 @@ smsc_ioctl(if_t ifp, u_long cmd, caddr_t data) return (rc); } +#ifdef FDT +static bool +smsc_get_smsc95xx_macaddr(char* bootargs, size_t len, struct usb_ether *ue) +{ + int values[6]; + int i; + char* p; + + p = strnstr(bootargs, BOOTARGS_SMSC95XX, len); + if (p == NULL) + return (false); + + if (sscanf(p, BOOTARGS_SMSC95XX "=%x:%x:%x:%x:%x:%x%*c", + &values[0], &values[1], &values[2], + &values[3], &values[4], &values[5]) != 6) { + smsc_warn_printf((struct smsc_softc *)ue->ue_sc, + "invalid mac from bootargs '%s'.\n", p); + return (false); + } + + for (i = 0; i < ETHER_ADDR_LEN; ++i) + ue->ue_eaddr[i] = values[i]; + + smsc_dbg_printf((struct smsc_softc *)ue->ue_sc, + "bootargs mac=%6D.\n", ue->ue_eaddr, ":"); + return (true); +} + +/** + * Raspberry Pi is known to pass smsc95xx.macaddr=XX:XX:XX:XX:XX:XX via + * bootargs. + */ +static bool +smsc_bootargs_get_mac_addr(device_t dev, struct usb_ether *ue) +{ + char *bootargs; + ssize_t len; + phandle_t node; + + /* only use bootargs for the first device + * to prevent duplicate mac addresses */ + if (device_get_unit(dev) != 0) + return (false); + node = OF_finddevice("/chosen"); + if (node == -1) + return (false); + if (OF_hasprop(node, "bootargs") == 0) { + smsc_dbg_printf((struct smsc_softc *)ue->ue_sc, + "bootargs not found"); + return (false); + } + len = OF_getprop_alloc(node, "bootargs", (void **)&bootargs); + if (len == -1 || bootargs == NULL) { + smsc_warn_printf((struct smsc_softc *)ue->ue_sc, + "failed alloc for bootargs (%zd)", len); + return (false); + } + smsc_dbg_printf((struct smsc_softc *)ue->ue_sc, "bootargs: %s.\n", + bootargs); + if (!smsc_get_smsc95xx_macaddr(bootargs, len, ue)) { + OF_prop_free(bootargs); + return (false); + } + OF_prop_free(bootargs); + device_printf(dev, "MAC address found in bootargs %6D.\n", + ue->ue_eaddr, ":"); + return (true); +} +#endif + /** * smsc_attach_post - Called after the driver attached to the USB interface * @ue: the USB ethernet device @@ -1552,8 +1624,10 @@ static void smsc_attach_post(struct usb_ether *ue) { struct smsc_softc *sc = uether_getsc(ue); + struct ether_addr eaddr; uint32_t mac_h, mac_l; int err; + int i; smsc_dbg_printf(sc, "smsc_attach_post\n"); @@ -1585,11 +1659,17 @@ smsc_attach_post(struct usb_ether *ue) #ifdef FDT if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) err = usb_fdt_get_mac_addr(sc->sc_ue.ue_dev, &sc->sc_ue); + if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) + err = smsc_bootargs_get_mac_addr(sc->sc_ue.ue_dev, + &sc->sc_ue) ? (0) : (1); #endif if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) { - 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 */ + smsc_dbg_printf(sc, "No MAC address found." + " Using ether_gen_addr().\n"); + ether_gen_addr_byname(device_get_nameunit(ue->ue_dev), + &eaddr); + for (i = 0; i < ETHER_ADDR_LEN; i++) + sc->sc_ue.ue_eaddr[i] = eaddr.octet[i]; } } diff --git a/sys/net/ethernet.h b/sys/net/ethernet.h index fca6748a0da7..e7313e78c5bb 100644 --- a/sys/net/ethernet.h +++ b/sys/net/ethernet.h @@ -448,6 +448,7 @@ struct mbuf *ether_vlanencap_proto(struct mbuf *, uint16_t, uint16_t); bool ether_8021q_frame(struct mbuf **mp, struct ifnet *ife, struct ifnet *p, const struct ether_8021q_tag *); void ether_gen_addr(struct ifnet *ifp, struct ether_addr *hwaddr); +void ether_gen_addr_byname(const char *nameunit, struct ether_addr *hwaddr); static __inline struct mbuf *ether_vlanencap(struct mbuf *m, uint16_t tag) { diff --git a/sys/net/if_ethersubr.c b/sys/net/if_ethersubr.c index 2cbe0ea98f27..4a726831a3db 100644 --- a/sys/net/if_ethersubr.c +++ b/sys/net/if_ethersubr.c @@ -1489,7 +1489,7 @@ ether_8021q_frame(struct mbuf **mp, struct ifnet *ife, struct ifnet *p, * allocate non-locally-administered addresses. */ void -ether_gen_addr(struct ifnet *ifp, struct ether_addr *hwaddr) +ether_gen_addr_byname(const char *nameunit, struct ether_addr *hwaddr) { SHA1_CTX ctx; char *buf; @@ -1508,7 +1508,7 @@ ether_gen_addr(struct ifnet *ifp, struct ether_addr *hwaddr) /* If each (vnet) jail would also have a unique hostuuid this would not * be necessary. */ getjailname(curthread->td_ucred, jailname, sizeof(jailname)); - sz = asprintf(&buf, M_TEMP, "%s-%s-%s", uuid, if_name(ifp), + sz = asprintf(&buf, M_TEMP, "%s-%s-%s", uuid, nameunit, jailname); if (sz < 0) { /* Fall back to a random mac address. */ @@ -1537,5 +1537,11 @@ rando: hwaddr->octet[0] |= 0x02; } +void +ether_gen_addr(struct ifnet *ifp, struct ether_addr *hwaddr) +{ + ether_gen_addr_byname(if_name(ifp), hwaddr); +} + DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY); MODULE_VERSION(ether, 1);