From patchwork Thu Jul 19 08:20:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 10533789 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B9285600F4 for ; Thu, 19 Jul 2018 08:20:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A7B9D2939B for ; Thu, 19 Jul 2018 08:20:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9B4E529438; Thu, 19 Jul 2018 08:20:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3D99B2939B for ; Thu, 19 Jul 2018 08:20:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731557AbeGSJCf (ORCPT ); Thu, 19 Jul 2018 05:02:35 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:37383 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731481AbeGSJCf (ORCPT ); Thu, 19 Jul 2018 05:02:35 -0400 Received: by mail-wr1-f66.google.com with SMTP id q10-v6so7132802wrd.4 for ; Thu, 19 Jul 2018 01:20:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6aX5BtucoOQijr14hJLQWkiX22ZRMT0Pi0ieKWGm2A8=; b=XZ4vKeDVThbroNNkg12Q/lIkPtkRFVcqrvmiIPVLOt7+hb9dqDHmUpfmbsFiMQA2Vj aAlGaWvXNKSzzUrSmtVtpAWNrB3caagzy3HnnVFpLg3zO4R1clc2mcfVwACRgnhlVO42 B2Os09Sp0xecdo0stMvYdT2WKdE5XRGTKteoT9W4nB/u9svAYqhFpa/R4YvppZybky1b QwSP9dYTeGkxuhIoGIS1S2NBIoewNtdkHX5gKNPXOIewfGhgvR+lm251Fwz4oJBU0NWX mnZFifn1SGVUmUtsxF4eSVr0XbDdGu61jUMzIO4wek4NZLFydFkYusOKRehpWnUsjjmy O4rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=6aX5BtucoOQijr14hJLQWkiX22ZRMT0Pi0ieKWGm2A8=; b=U3BCavY33tfceRoq+bhDmvu6aSGYafIr8VJGiov8JtUyoWMXTPBw1vEXvPhqD1wUOe 5CFrhEQUtZDzEXZT/yicT8Ci5H37kIsHwzEzXobe0gdCYlEHlRKXxwOTFu7tpuUz6PEA TWhk08fS3GDsZMTk6qUMc9rSEenzPiOxvjNesArjzcCycRZZTif0+DJVcUEF17TUbfxw LsVuTeggEPzlIJFjdHvEBuQbJd40AHmX5L9eKHHMSRtnqvsnvAvHfua0aEXIZbyJueht kJdyLXbSX8GrkaJRJI1KpPn6QvBsd1z5ebrj72gSQEk//iuh382c1SeNbejBRzfLKMY1 KBJw== X-Gm-Message-State: AOUpUlHcV3SdhZPx7lmBebjHagnsBJwcVs5RyRevtQMw9Lbvmtdk3awY kZxpNi1OrR2IZVXcdBIbTkjmDA== X-Google-Smtp-Source: AAOMgpfRQlntvwTygkmWO3wjmGHEbxy7UlmbI+t6NYuopvlbk/rsIsyF6G9P9xOwMyWs66Sg+0Wbqg== X-Received: by 2002:a5d:4cc5:: with SMTP id c5-v6mr6995087wrt.210.1531988434522; Thu, 19 Jul 2018 01:20:34 -0700 (PDT) Received: from brgl-bgdev.baylibre.local (AStLambert-681-1-40-185.w90-86.abo.wanadoo.fr. [90.86.159.185]) by smtp.gmail.com with ESMTPSA id h43-v6sm1716338wrh.1.2018.07.19.01.20.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Jul 2018 01:20:33 -0700 (PDT) From: Bartosz Golaszewski To: Sekhar Nori , Kevin Hilman , Russell King , Grygorii Strashko , "David S . Miller" , Srinivas Kandagatla , Lukas Wunner , Rob Herring , Florian Fainelli , Dan Carpenter , Ivan Khoronzhuk , David Lechner , Greg Kroah-Hartman , Andrew Lunn Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-omap@vger.kernel.org, netdev@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 2/3] net: add support for nvmem to eth_platform_get_mac_address() Date: Thu, 19 Jul 2018 10:20:27 +0200 Message-Id: <20180719082028.26116-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180719082028.26116-1-brgl@bgdev.pl> References: <20180719082028.26116-1-brgl@bgdev.pl> Sender: linux-omap-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-omap@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Bartosz Golaszewski Many non-DT platforms read the MAC address from EEPROM. Usually it's either done with callbacks defined in board files or from SoC-specific ethernet drivers. In order to generalize this, try to read the MAC from nvmem in eth_platform_get_mac_address() using a standard lookup name: "mac-address". Signed-off-by: Bartosz Golaszewski --- net/ethernet/eth.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/net/ethernet/eth.c b/net/ethernet/eth.c index 39af03894598..af3b4b1b77eb 100644 --- a/net/ethernet/eth.c +++ b/net/ethernet/eth.c @@ -54,6 +54,7 @@ #include #include #include +#include #include #include #include @@ -527,8 +528,11 @@ unsigned char * __weak arch_get_platform_mac_address(void) int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr) { + unsigned char addrbuf[ETH_ALEN]; const unsigned char *addr; + struct nvmem_cell *nvmem; struct device_node *dp; + size_t alen; if (dev_is_pci(dev)) dp = pci_device_to_OF_node(to_pci_dev(dev)); @@ -541,6 +545,29 @@ int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr) if (!addr) addr = arch_get_platform_mac_address(); + if (!addr) { + nvmem = nvmem_cell_get(dev, "mac-address"); + if (IS_ERR(nvmem) && PTR_ERR(nvmem) == -EPROBE_DEFER) + /* We may have a lookup registered for MAC address but + * the corresponding nvmem provider hasn't been + * registered yet. + */ + return -EPROBE_DEFER; + + if (!IS_ERR(nvmem)) { + addr = nvmem_cell_read(nvmem, &alen); + if (!IS_ERR(addr)) { + if (alen == ETH_ALEN) + ether_addr_copy(addrbuf, addr); + + kfree(addr); + addr = alen == ETH_ALEN ? addrbuf : NULL; + } + + nvmem_cell_put(nvmem); + } + } + if (!addr || !is_valid_ether_addr(addr)) return -ENODEV;