Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1836335imm; Thu, 19 Jul 2018 08:37:12 -0700 (PDT) X-Google-Smtp-Source: AAOMgpd7GgwH6Cz8mGbi/kpD42Rsh87Kxh5Pt+coD48jGjjX/VQDqj8b9X8Vz72FQ7JngK/ycIlG X-Received: by 2002:a17:902:24a5:: with SMTP id w34-v6mr10527514pla.52.1532014632327; Thu, 19 Jul 2018 08:37:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532014632; cv=none; d=google.com; s=arc-20160816; b=ByQ61nHJrgxVQgG1ejT/m5tLAke74tk7/V70jsKv5JAX9PXhiEnrNSyuDrfQ57FSUV yA/WGucWZvKq2DYiFhpM47VCs+SadzKkJi1GK1Jm6b1j/kSw649/zgkT9pU+naQkYJ8Z I2HTbJk+126z3zpB0IYaRyx/Ot5fui0reMrYIiktoYcfEziZ9IKPFDT6+jP8AAwdRz/l 3P+Dn0qprX4sndf/Ti044sU1kOvgnC96E3ULH4g/F0ODspFi2h9tGCwZxyxblt+4E2pL iODlLwQKsOFcOUv10unY/JxTXXwWqITTZdq1LmfTAMz/xsVZMKP0ZYJLMhcud4FW7qrQ fqGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=fpjT7LGURvaHnPeD63JRN66HubRrDYEim016IoZqmxA=; b=HO+WQzJ5iG10os7U9iy8ILdpyqA+5zPtIVXqFRD8KhBNOt6aSUqc8fDA+g9hVlONnK XJlFTILUpu/TtD8dYakE3g7hYKhS49HhbRunDJ+D3pCUn1R3UYCfTkGbiMp9UorG+9c4 MzI9lwh0EtAQf2OJk4NrB+dKTK9eHw6NWVL8DfB8BFum+FLyeJS/+nGEL8qtvTZt2OKC h5/LHwmcaTGGJlX0DuTdCrT2n2ArI0Mg9u01pMMvgomls7B+oPod5GC1zH3vNAJvwDwt j+5Yw/dRtdqPyrevGlPzoD7nWOUctgbK5k/Ls1g6OTg8CnJ+GiwqjMMgrnp0HYn5FSxL p39g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=rblFLWIF; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k64-v6si5586420pgd.3.2018.07.19.08.36.57; Thu, 19 Jul 2018 08:37:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=rblFLWIF; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731949AbeGSQTY (ORCPT + 99 others); Thu, 19 Jul 2018 12:19:24 -0400 Received: from mail-it0-f66.google.com ([209.85.214.66]:34866 "EHLO mail-it0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731775AbeGSQTY (ORCPT ); Thu, 19 Jul 2018 12:19:24 -0400 Received: by mail-it0-f66.google.com with SMTP id q20-v6so10511557ith.0 for ; Thu, 19 Jul 2018 08:35:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=fpjT7LGURvaHnPeD63JRN66HubRrDYEim016IoZqmxA=; b=rblFLWIFFbmqsU0RLZTSCr+Oo26aomVVWshVESWGGzSU6ZpFM3iKWqnhacIhoGAq5r 5khRDzcCqWMF4USiuNgZAckrI3oq3SybGQzSKl1zkxv3H0JUjvaRB9Q6nln8EOD3Yt99 40QvEnE1xZFxwpCko6LV3f8xPW5Lw0RYPeYm5Z8AqvDyUqIGCtR2mtwy6obiVUJZKG3G YX+AQmVYMONYyv8UU5l/QWR1fSOAjiMRIeSeAn7x/ZHxUYfTEg6lGK/2aXdBmXoCezxy 8tsoTvj1qjcEVfaEKixbB6fkmFAmStOPLpjAnhlxMi5gyEe+4jmez4FakUM8Bnbp1j1N CrEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=fpjT7LGURvaHnPeD63JRN66HubRrDYEim016IoZqmxA=; b=Yam7p4jgt4SnLRulz0EaS3vvSZWON3sw0UUMX/CuGLQk5ApacdC5y68ZB+zHb7Yvx2 e+Gyrc59n4KthkFAr1Lxxt9SJWcqBq6a+/MBV0tUwdm+EuGTXpZ2jxvybpO+dMh6EQ6Z ASF8qCRT3cDMtfQf+ExmLPqXiiTIGkW//MKTq39xANF9J7CF+UdXIFi9x0+hEWiQowxt 52Wl6YH//fE9FZ+vwxVk+cheBk1P4tPjfaQUXDoH9DC0eGRq1ud7zw2etF9mM/8D4tla 8svPeU5hquZ2Jtcnuc3DdSMKmyX5II1+2kPFyXuWk0kAu6pdFHDMQ2vnGLRaFiWCY3RJ cX/w== X-Gm-Message-State: AOUpUlHDGLZoFlK8hzhZBMHrF/diguwmMQDRM2+WMu1CkaLsn0+fWWdX fImuJwX0OMsjxKa8yhx/wyStNMbNowQGlZXPgZxqzQ== X-Received: by 2002:a24:e48c:: with SMTP id o134-v6mr5161662ith.125.1532014540903; Thu, 19 Jul 2018 08:35:40 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a5e:9402:0:0:0:0:0 with HTTP; Thu, 19 Jul 2018 08:35:40 -0700 (PDT) In-Reply-To: <20180719123722.sk5fcih6pufy4wpd@mwanda> References: <20180719082028.26116-1-brgl@bgdev.pl> <20180719082028.26116-3-brgl@bgdev.pl> <20180719084503.tfv6jllsukk2zv3f@mwanda> <20180719090929.arbysqo2bk3esoar@mwanda> <20180719123722.sk5fcih6pufy4wpd@mwanda> From: Bartosz Golaszewski Date: Thu, 19 Jul 2018 17:35:40 +0200 Message-ID: Subject: Re: [PATCH v2 2/3] net: add support for nvmem to eth_platform_get_mac_address() To: Dan Carpenter Cc: Bartosz Golaszewski , Sekhar Nori , Kevin Hilman , Russell King , Grygorii Strashko , "David S . Miller" , Srinivas Kandagatla , Lukas Wunner , Rob Herring , Florian Fainelli , Ivan Khoronzhuk , David Lechner , Greg Kroah-Hartman , Andrew Lunn , arm-soc , LKML , Linux-OMAP , netdev@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 2018-07-19 14:37 GMT+02:00 Dan Carpenter : > On Thu, Jul 19, 2018 at 12:06:51PM +0200, Bartosz Golaszewski wrote: >> It looks simpler as long as you don't add all the new routines >> resulting from this approach. I've just tried to quickly implement >> this solution and it resulted in much bigger and duplicated code >> (checking the validity of mac_addr, copying it etc.). I would prefer >> the current approach and would like to read someone else's opinion on >> that. > > It's not too bad. There is two extra ether_addr_copy() calls and one > extra is_valid_ether_addr(). There are two extra alen declarations and > one extra addr declaration. The functions don't share *that* much code. > > regards, > dan carpenter > > diff --git a/net/ethernet/eth.c b/net/ethernet/eth.c > index fd8faa0dfa61..8ab7289a5069 100644 > --- a/net/ethernet/eth.c > +++ b/net/ethernet/eth.c > @@ -54,6 +54,8 @@ > #include > #include > #include > +#include > +#include > #include > #include > #include > @@ -525,7 +527,7 @@ unsigned char * __weak arch_get_platform_mac_address(void) > return NULL; > } > > -int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr) > +static int of_eth_get_mac_address(struct device *dev, u8 *mac_addr) > { > const unsigned char *addr; > struct device_node *dp; > @@ -547,4 +549,82 @@ int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr) > ether_addr_copy(mac_addr, addr); > return 0; > } > + > +static int nvmem_eth_get_mac_address(struct device *dev, u8 *mac_addr) > +{ > + struct nvmem_cell *nvmem; > + unsigned char *addr; > + size_t alen; > + int ret = 0; > + > + nvmem = nvmem_cell_get(dev, "mac-address"); > + if (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)) > + return PTR_ERR(nvmem); > + addr = nvmem_cell_read(nvmem, &alen); > + if (IS_ERR(addr)) { > + ret = PTR_ERR(addr); > + goto put_nvmem; > + } > + if (alen != ETH_ALEN || !is_valid_ether_addr(addr)) { > + ret = -EINVAL; > + goto free_addr; > + } > + ether_addr_copy(mac_addr, addr); > +free_addr: > + kfree(addr); > +put_nvmem: > + nvmem_cell_put(nvmem); > + return ret; > +} > + > +static int mtd_eth_get_mac_address(struct device *dev, u8 *mac_addr) > +{ > + struct mtd_info *mtd; > + u8 addrbuf[ETH_ALEN]; > + size_t alen; > + int ret; > + > + /* This function should go away as soon as MTD gets nvmem support. */ > + if (!IS_ENABLED(CONFIG_MTD)) > + return -ENODEV; > + > + mtd = get_mtd_device_nm("MAC-Address"); > + if (IS_ERR(mtd)) > + return PTR_ERR(mtd); > + ret = mtd_read(mtd, 0, ETH_ALEN, &alen, addrbuf); > + if (ret) > + goto put_mtd; > + if (!is_valid_ether_addr(addrbuf)) { > + ret = -EINVAL; > + goto put_mtd; > + } > + ether_addr_copy(mac_addr, addrbuf); > +put_mtd: > + put_mtd_device(mtd); > + return ret; > +} > + > +int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr) > +{ > + int ret; > + > + ret = of_eth_get_mac_address(dev, mac_addr); > + if (!ret) > + return 0; > + ret = nvmem_eth_get_mac_address(dev, mac_addr); > + if (ret == -EPROBE_DEFER) > + return -EPROBE_DEFER; > + if (!ret) > + return 0; > + ret = mtd_eth_get_mac_address(dev, mac_addr); > + if (!ret) > + return 0; > + > + return -ENODEV; > +} > EXPORT_SYMBOL(eth_platform_get_mac_address); Please take a look at v3 - I did it a bit differently but the idea is the same. Bart