Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1649121imm; Thu, 19 Jul 2018 05:39:50 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeM6uY87WG3Lsv3vEcUXSCDLcRD1Xk36dV1XFp8GPVqeLcUR8EHHSHqtc4lzLEXD1IZdDcO X-Received: by 2002:a63:f18:: with SMTP id e24-v6mr9923821pgl.320.1532003990655; Thu, 19 Jul 2018 05:39:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532003990; cv=none; d=google.com; s=arc-20160816; b=aotAm0VN1ZupK/8F/ar3v42qgB9i9ctHJvjymBkUisc5gjM3SQx+Xb8xNI3vHXMGB7 GYnpbezA2+39yLGpE6MMySlGyvJ9UU625O+GlOB8FJDwDGDlyritlMZwOjj8BK56bG41 nncMiYWp8QR7GHFV/i5yA331x8h9wM02/+sLyPX8H+YY8i7J6kIST7EaTjGo9zc1udcU 8rUru6X9qfa8Eo4fncaPxxZ86vUSBLeCKP/SvpG0yo59U7B69mA1iUzYxxS2Py2efHQU fohyYDevDSPYFvHwoYcuTrf67lN4pUyCWbJItpIF28uAnzzGcPK94Rg9NlIRvBTHY+gV 1SRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=++fiZEgIB1TGMQQmcdpOKsGFpvgH20TecVNYGoJQWAI=; b=RghR/MkJvlDmpybwNH1FjXBn1ogXrTNAcUeJ3QTZkkAOy4F1CBK1rKRIQ6x96POCfT fXM3Y2OlipCxx+9cOzxEGtFdIY4QhAWPY38W5dIgk8JT8BrH962tDTwE9F4xzbcb32r9 O3yBGN+5a6B+Xqblv7zUXn3YTwmLSrdwVoFe8rE0j3WagpdWvmdodbgg6j/vv/H2iKjr cxg1GTg+MKIboWwbUcbnR5C66pOFDc7vZdE80w/+w1W2YTw56tX7D6qQBe5a4A9mLe64 c4laweOvKx8mloZPZ78trgmOi8A7mlu1vwZuQ5NnFrzYS+4uYRCVZag+p41CxKx22GU5 lKoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=UekZQAF8; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d11-v6si5424872pgm.556.2018.07.19.05.39.36; Thu, 19 Jul 2018 05:39:50 -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=@oracle.com header.s=corp-2018-07-02 header.b=UekZQAF8; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731477AbeGSNV6 (ORCPT + 99 others); Thu, 19 Jul 2018 09:21:58 -0400 Received: from userp2130.oracle.com ([156.151.31.86]:46080 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730191AbeGSNV6 (ORCPT ); Thu, 19 Jul 2018 09:21:58 -0400 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 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 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-Type: text/plain; charset=us-ascii 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 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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);