From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5BDA4C10F12 for ; Wed, 17 Apr 2019 05:00:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1ED7D217D7 for ; Wed, 17 Apr 2019 05:00:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZPkgZn2R" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727618AbfDQFAz (ORCPT ); Wed, 17 Apr 2019 01:00:55 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:43296 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725767AbfDQFAy (ORCPT ); Wed, 17 Apr 2019 01:00:54 -0400 Received: by mail-wr1-f65.google.com with SMTP id k17so25369070wrx.10; Tue, 16 Apr 2019 22:00:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=4SPHMD/Nb+0REFk7ucTGIOc94eU7H0QKdLwbUoP0bck=; b=ZPkgZn2RwoQC8/kL18bj6uPffCAu+Wun6ljuArsRaMHThU21d1OjFUd5E7NKix3vk+ C1T2V+BLIpHHCUhMBxqhCDlAvdpYPEbLmcUePA8GEg41VlLQCJf52woltT9M7HpffEyK ZWjjCq5SOFWNyJRXQ2skUTPdQn8LAlG8iTjayFxlkwFbCwNkp5T0XB99rdhvnZV1+PZW xuATTeMVNH3WXqdSo8S7pTPfl2FuvE0/8RTRH344j/3JykIBSbUIQacBHjas8ihKfF5X cAwu52NDZ91for9VdtdwnfuqlEEnzZ1xoyXI4ou+mGREd0oj5sC9EvntTe4Zm3BdkVoO kZUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=4SPHMD/Nb+0REFk7ucTGIOc94eU7H0QKdLwbUoP0bck=; b=o/K+fyEy6MaLD8HWkvu3i5Q0bHcPocjc45H25mBhSRZsAk0tXRkGmDCSc0Uet8pTFh Tz78y4HDdvWRuwpod5H7jKRowfDb/QTbqiwbVmSpQ276/63RsX98AbTGgnVrJhBh3HaC of8x7I4c2+fHFV86mrhD3ABVhB2nhPZKa35r82FCbDtIs9dY9pSOZIBgmQTiT0rsfOHw fEXv1LcSoJmjKytu8P9oQ+VabdGzJVEFDX7N0LyEUULnmJO1CgdfP0noNwH/Qayb/Lhc aUjCsrmH7M7jz6O0oM39ZHzn3tTQqUFDGPZNpHzxhEu7/9qAS1oufSMjBSDjKS8PvERB 51EQ== X-Gm-Message-State: APjAAAVLnOZKPAM5kHcXmKfmi8bBtS9LeMPck3MuiW6llM9MIV7wG+vK t+0uZ76fzAmACUJ/Mx0yFNg= X-Google-Smtp-Source: APXvYqxlOlmy4aI8zUNecn+lHbtU/FuBtPONqntX5OVKR7fyQ76H7CvXjZv4LM4yAreJB/r2X5oQOA== X-Received: by 2002:a5d:428c:: with SMTP id k12mr40752162wrq.279.1555477252465; Tue, 16 Apr 2019 22:00:52 -0700 (PDT) Received: from ?IPv6:2003:ea:8bd4:5700:3418:36cb:e8e5:54f6? (p200300EA8BD45700341836CBE8E554F6.dip0.t-ipconnect.de. [2003:ea:8bd4:5700:3418:36cb:e8e5:54f6]) by smtp.googlemail.com with ESMTPSA id 4sm1158047wmg.12.2019.04.16.22.00.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Apr 2019 22:00:51 -0700 (PDT) Subject: Re: [PATCH] of_net: add mtd-mac-address support to of_get_mac_address() To: =?UTF-8?Q?Petr_=c5=a0tetiar?= , netdev@vger.kernel.org, devicetree@vger.kernel.org Cc: "David S. Miller" , Rob Herring , Mark Rutland , Andrew Lunn , Florian Fainelli , Frank Rowand , John Crispin , Felix Fietkau References: <1555445100-30936-1-git-send-email-ynezz@true.cz> From: Heiner Kallweit Message-ID: <4f8b582f-6df5-81eb-d03c-84b4ca9a2ae7@gmail.com> Date: Wed, 17 Apr 2019 07:00:45 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <1555445100-30936-1-git-send-email-ynezz@true.cz> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On 16.04.2019 22:05, Petr Štetiar wrote: > From: John Crispin > > Many embedded devices have information such as MAC addresses stored > inside MTD devices. This patch allows us to add a property inside a node > describing a network interface. The new property points at a MTD > partition with an offset where the MAC address can be found. > > This patch has originated in OpenWrt some time ago, so in order to > consider usefulness of this patch, here are some real-world numbers > which hopefully speak for themselves: > > * mtd-mac-address used 497 times in 357 device tree files > * mtd-mac-address-increment used 74 times in 58 device tree files > * mtd-mac-address-increment-byte used 1 time in 1 device tree file > > Signed-off-by: John Crispin > Signed-off-by: Felix Fietkau > [cleanup of the patch for upstream submission] > Signed-off-by: Petr Štetiar > --- > Documentation/devicetree/bindings/net/ethernet.txt | 5 ++ > drivers/of/of_net.c | 82 +++++++++++++++++++++- > 2 files changed, 86 insertions(+), 1 deletion(-) > > diff --git a/Documentation/devicetree/bindings/net/ethernet.txt b/Documentation/devicetree/bindings/net/ethernet.txt > index cfc376b..8a7891e 100644 > --- a/Documentation/devicetree/bindings/net/ethernet.txt > +++ b/Documentation/devicetree/bindings/net/ethernet.txt > @@ -10,6 +10,11 @@ Documentation/devicetree/bindings/phy/phy-bindings.txt. > 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; > +- mtd-mac-address: specify a MTD partition + offset containing array of 6 bytes > +- mtd-mac-address-increment: specify number by which we should increment the > + MAC address stored in the MTD partition > +- mtd-mac-address-increment-byte: specify octet/byte(0-5) in the MAC address, > + where we should increment the value, defaults to octet 5 (the last one) > - 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; > diff --git a/drivers/of/of_net.c b/drivers/of/of_net.c > index 810ab0f..01b24d6 100644 > --- a/drivers/of/of_net.c > +++ b/drivers/of/of_net.c > @@ -11,6 +11,7 @@ > #include > #include > #include > +#include > > /** > * of_get_phy_mode - Get phy mode for given device_node > @@ -39,7 +40,7 @@ int of_get_phy_mode(struct device_node *np) > } > EXPORT_SYMBOL_GPL(of_get_phy_mode); > > -static const void *of_get_mac_addr(struct device_node *np, const char *name) > +static void *of_get_mac_addr(struct device_node *np, const char *name) > { > struct property *pp = of_find_property(np, name, NULL); > > @@ -48,6 +49,78 @@ static const void *of_get_mac_addr(struct device_node *np, const char *name) > return NULL; > } > > +static const void *of_get_mac_address_mtd(struct device_node *np) > +{ > +#ifdef CONFIG_MTD > + void *addr; > + size_t retlen; > + int size, ret; > + u8 mac[ETH_ALEN]; > + phandle phandle; > + const char *part; > + const __be32 *list; > + struct mtd_info *mtd; > + struct property *prop; > + u32 mac_inc = 0; > + u32 inc_idx = ETH_ALEN-1; > + struct device_node *mtd_np = NULL; > + > + list = of_get_property(np, "mtd-mac-address", &size); > + if (!list || (size != (2 * sizeof(*list)))) > + return NULL; > + > + phandle = be32_to_cpup(list++); > + if (phandle) > + mtd_np = of_find_node_by_phandle(phandle); > + > + if (!mtd_np) > + return NULL; > + > + part = of_get_property(mtd_np, "label", NULL); > + if (!part) > + part = mtd_np->name; > + > + mtd = get_mtd_device_nm(part); > + if (IS_ERR(mtd)) > + return NULL; > + > + ret = mtd_read(mtd, be32_to_cpup(list), ETH_ALEN, &retlen, mac); ret doesn't seem to be checked. And checking retlen wouldn't be bad too. > + put_mtd_device(mtd); > + > + of_property_read_u32(np, "mtd-mac-address-increment-byte", &inc_idx); > + if (inc_idx > ETH_ALEN-1) > + return NULL; > + > + if (!of_property_read_u32(np, "mtd-mac-address-increment", &mac_inc)) > + mac[inc_idx] += mac_inc; > + > + if (!is_valid_ether_addr(mac)) > + return NULL; > + > + addr = of_get_mac_addr(np, "mac-address"); > + if (addr) { > + memcpy(addr, mac, ETH_ALEN); > + return addr; > + } > + > + prop = kzalloc(sizeof(*prop), GFP_KERNEL); > + if (!prop) > + return NULL; > + > + prop->name = "mac-address"; > + prop->length = ETH_ALEN; > + prop->value = kmemdup(mac, ETH_ALEN, GFP_KERNEL); > + if (!prop->value || of_add_property(np, prop)) > + goto free; > + > + return prop->value; > +free: > + kfree(prop->value); > + kfree(prop); > +#endif > + return NULL; > +} > + > /** > * Search the device tree for the best MAC address to use. 'mac-address' is > * checked first, because that is supposed to contain to "most recent" MAC > @@ -65,11 +138,18 @@ static const void *of_get_mac_addr(struct device_node *np, const char *name) > * addresses. Some older U-Boots only initialized 'local-mac-address'. In > * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists > * but is all zeros. > + * > + * If a mtd-mac-address property exists, try to fetch the MAC address from the > + * specified MTD device, and store it as a 'mac-address' property. > */ > const void *of_get_mac_address(struct device_node *np) > { > const void *addr; > > + addr = of_get_mac_address_mtd(np); > + if (addr) > + return addr; > + > addr = of_get_mac_addr(np, "mac-address"); > if (addr) > return addr; >