From patchwork Thu Jul 19 12:37:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Carpenter X-Patchwork-Id: 10534295 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 357F0601D2 for ; Thu, 19 Jul 2018 12:38:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 21C3629607 for ; Thu, 19 Jul 2018 12:38:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1580A29614; Thu, 19 Jul 2018 12:38:57 +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=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, MAILING_LIST_MULTI, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 98FA629607 for ; Thu, 19 Jul 2018 12:38:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=pwnon6bMeKyvStmwkCJh/ieU+RFmAASDgcEw9Kzf7QQ=; b=mEwyjFpN0hc88K oQW3r+walzBECH7CDVauNrDscbZHHH/bNsdI8/up1TOZfb/YiDf5rcXEno3JjMR83awMwIWyAFKP6 wAtBaIftNt2kcx6b5txiHZ4Pkwf2z9VyepqdRQ6ogpxhmQnWcKrDrMdflyszjicjHfWjTd66Dd62F bhAJHx2nb3byqZA8zverUjF0D8jYWCkyE2rfmVtdPDr+V63BDJCvH4RmvVly2umOyEI0St9Bsm8qC URUtKYZt/4uux6hpyNbzFsFQzijgLmMxWpNlvdNHqKJrQoxo62k4w+hcL7O2OWN8n8aKFgg51VYic Dbg6Pl6kSbxUZYErN+tA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fg8Cr-0007WA-55; Thu, 19 Jul 2018 12:38:53 +0000 Received: from userp2130.oracle.com ([156.151.31.86]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fg8Cn-0007VU-8t for linux-arm-kernel@lists.infradead.org; Thu, 19 Jul 2018 12:38:50 +0000 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w6JCYB2F162888; Thu, 19 Jul 2018 12:37:37 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : references : mime-version : content-type : in-reply-to; s=corp-2018-07-02; bh=++fiZEgIB1TGMQQmcdpOKsGFpvgH20TecVNYGoJQWAI=; b=UekZQAF8u5ECnKXR34lqdIudEZIGJHXrYP6axT0+nDY/oXKk9rNxWKFDba2iI2voa8ZF p2ezOJvHCn3lX5KqJ2iiS4IagtN+vvgKh08CcQp81/tceQ4IZqZWrTkguMdzLyjijcv1 Etfl5burZHvXakLLwWYMZXbvP7PbwH6+zh49I8U/ldWbZKCjE+7bP7jMlqgsnQdivENb oU1KAsIJjV5DJqL5aEsrxYU8TPsTsEqED5yEN8GUkgPViOtSsMYsUDkBUlO78L8Yr47I 7FGvxYO17KknlouarD93Fi9WPsv5fyt5zPdoyVg4WaIWDHlURuxP5YCJr1CavCGw6wPG /A== Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by userp2130.oracle.com with ESMTP id 2k9ykc6w96-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 19 Jul 2018 12:37:37 +0000 Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by userv0022.oracle.com (8.14.4/8.14.4) with ESMTP id w6JCbZoh010331 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 19 Jul 2018 12:37:36 GMT Received: from abhmp0008.oracle.com (abhmp0008.oracle.com [141.146.116.14]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id w6JCbXGj026171; Thu, 19 Jul 2018 12:37:34 GMT Received: from mwanda (/197.179.115.196) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 19 Jul 2018 05:37:33 -0700 Date: Thu, 19 Jul 2018 15:37:22 +0300 From: Dan Carpenter To: Bartosz Golaszewski Subject: Re: [PATCH v2 2/3] net: add support for nvmem to eth_platform_get_mac_address() Message-ID: <20180719123722.sk5fcih6pufy4wpd@mwanda> References: <20180719082028.26116-1-brgl@bgdev.pl> <20180719082028.26116-3-brgl@bgdev.pl> <20180719084503.tfv6jllsukk2zv3f@mwanda> <20180719090929.arbysqo2bk3esoar@mwanda> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20170609 (1.8.3) X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8958 signatures=668706 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=2 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1806210000 definitions=main-1807190135 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180719_053849_450183_E8AC0585 X-CRM114-Status: GOOD ( 23.87 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Rob Herring , Grygorii Strashko , David Lechner , Ivan Khoronzhuk , Kevin Hilman , Greg Kroah-Hartman , Sekhar Nori , Russell King , LKML , Andrew Lunn , Bartosz Golaszewski , Lukas Wunner , Srinivas Kandagatla , netdev@vger.kernel.org, Florian Fainelli , Linux-OMAP , "David S . Miller" , arm-soc Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP 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);