Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3738377pxf; Mon, 5 Apr 2021 21:57:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw8K/TGAkJfiNBiUY5SHSd9nX5fjuVSrmU0Z/Z6SFgrt41tXo8I37fcI0VezUk8V2lVRerO X-Received: by 2002:a05:6402:c8:: with SMTP id i8mr35853595edu.57.1617685043219; Mon, 05 Apr 2021 21:57:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617685043; cv=none; d=google.com; s=arc-20160816; b=b7Aj+lSGKrjwP7filxyHO6HomkOkzbKM3/mkvisMfH2CC2iys+rQyY8wCGvO9hFFlq IiZZp6ahLT0XEgK6xFP4xNub3BCg0bfuAsJz6Q6ZMTMoQHbpVY0DYg3NQKOY24w+bNqH vfFVMfEa/dsJq7sriRWdXHDJSWwuvy2dofcdfeXdnVBkQa6uSOb786yLg7yrZNvcQU6S M9SZMDNlptY18fgoKtqWvujUaoXSzR5TGYwE+jdO/PaYTRcSYkTykqsAUVfIQFUE8ZSE Zjb908tVey6E66tNfa6XOdcX+ml09f1zCcWbBJ6XaggryacxjC2JvY4hrHrSpFdMR1lP l6EQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=lACUh6+pO6Mh+0/uWp/aTCFDfW7S2kCGechEIv6LxiY=; b=C5aCZ2BAdp/4d2TsVBdkAZLIn7mznvwCuNvBRBgA0s324NDrGU0h+Bio4NFm42PY4F OEUsQItU9EVFSgp4xFYjKU/imXMQbZDQLez4moQOPbAbZRL/6B2+5xvCMKfKd4fzRWJy 9qOkRFpJkxkHFt+Kqo4Xyavsqq9u7yOGoR3RpCw5+mNqZgCl/mR1V59NpBJ04G3rTu16 7j0kNoQAqESGoX0dM2qw46Cy4He4sD17KIZuRE/3NxBKzLkptmxwsPacLn6LhNEKUlbR ziMUA4UIEtRlm9er6h5DU0iawSFcij4cElVwcXOyA1bFG02RvawIOZjYlMT2ox0w638f +0sQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@walle.cc header.s=mail2016061301 header.b=lwFE87EY; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u4si2422025ejz.726.2021.04.05.21.57.00; Mon, 05 Apr 2021 21:57:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@walle.cc header.s=mail2016061301 header.b=lwFE87EY; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231767AbhDEQrw (ORCPT + 99 others); Mon, 5 Apr 2021 12:47:52 -0400 Received: from ssl.serverraum.org ([176.9.125.105]:55715 "EHLO ssl.serverraum.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229735AbhDEQrs (ORCPT ); Mon, 5 Apr 2021 12:47:48 -0400 Received: from mwalle01.fritz.box (unknown [IPv6:2a02:810c:c200:2e91:fa59:71ff:fe9b:b851]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by ssl.serverraum.org (Postfix) with ESMTPSA id CEB002224E; Mon, 5 Apr 2021 18:47:37 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=walle.cc; s=mail2016061301; t=1617641259; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lACUh6+pO6Mh+0/uWp/aTCFDfW7S2kCGechEIv6LxiY=; b=lwFE87EYukRw6Yc6p60CLMzC5zc1q4XkEPTn8w8EvzA1JTUuv+YccJm62Q7VbOfBftWYWl UYVpBPvEm+zmXff6Nt+50Y7ystqKWnOHm4H8B+opiuB/6tPwdRVQ2hz+cKHZqMG/Bd3MLL hij5E+nDvi2JnjZOiwmh02pWHeeGxPw= From: Michael Walle To: ath9k-devel@qca.qualcomm.com, UNGLinuxDriver@microchip.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, netdev@vger.kernel.org, linux-mediatek@lists.infradead.org, linux-renesas-soc@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-amlogic@lists.infradead.org, linux-oxnas@groups.io, linux-omap@vger.kernel.org, linux-wireless@vger.kernel.org, devicetree@vger.kernel.org, linux-staging@lists.linux.dev Cc: Andrew Lunn , Gregory Clement , Sebastian Hesselbarth , Russell King , Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , Andreas Larsson , "David S . Miller" , Jakub Kicinski , Maxime Ripard , Chen-Yu Tsai , Jernej Skrabec , Joyce Ooi , Chris Snook , =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= , bcm-kernel-feedback-list@broadcom.com, Florian Fainelli , Nicolas Ferre , Claudiu Beznea , Sunil Goutham , Fugang Duan , Madalin Bucur , Pantelis Antoniou , Claudiu Manoil , Li Yang , Yisen Zhuang , Salil Mehta , Hauke Mehrtens , Thomas Petazzoni , Vadym Kochan , Taras Chornyi , Mirko Lindner , Stephen Hemminger , Felix Fietkau , John Crispin , Sean Wang , Mark Lee , Matthias Brugger , Bryan Whitehead , Vladimir Zapolskiy , Sergei Shtylyov , Byungho An , Kunihiko Hayashi , Giuseppe Cavallaro , Alexandre Torgue , Jose Abreu , Maxime Coquelin , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , NXP Linux Team , Kevin Hilman , Neil Armstrong , Jerome Brunet , Martin Blumenstingl , Vinod Koul , Nobuhiro Iwamatsu , Grygorii Strashko , Wingman Kwok , Murali Karicheri , Michal Simek , Radhey Shyam Pandey , Kalle Valo , Lorenzo Bianconi , Ryder Lee , Stanislaw Gruszka , Helmut Schaa , Heiner Kallweit , Rob Herring , Frank Rowand , Greg Kroah-Hartman , =?UTF-8?q?J=C3=A9r=C3=B4me=20Pouiller?= , Vivien Didelot , Vladimir Oltean , Michael Walle Subject: [PATCH 2/2] of: net: fix of_get_mac_addr_nvmem() for PCI and DSA nodes Date: Mon, 5 Apr 2021 18:46:43 +0200 Message-Id: <20210405164643.21130-3-michael@walle.cc> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210405164643.21130-1-michael@walle.cc> References: <20210405164643.21130-1-michael@walle.cc> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org of_get_mac_address() already supports fetching the MAC address by an nvmem provider. But until now, it was just working for platform devices. Esp. it was not working for DSA ports and PCI devices. It gets more common that PCI devices have a device tree binding since SoCs contain integrated root complexes. Use the nvmem of_* binding to fetch the nvmem cells by a struct device_node. We still have to try to read the cell by device first because there might be a nvmem_cell_lookup associated with that device. Signed-off-by: Michael Walle --- Please note, that I've kept the nvmem_get_mac_address() which operates on a device. The new of_get_mac_addr_nvmem() is almost identical and there are no users of the former function right now, but it seems to be the "newer" version to get the MAC address for a "struct device". Thus I've kept it. Please advise, if I should kill it though. drivers/of/of_net.c | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/drivers/of/of_net.c b/drivers/of/of_net.c index 2344ad7fff5e..2323c6063eaf 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 @@ -56,18 +57,42 @@ static int of_get_mac_addr(struct device_node *np, const char *name, u8 *addr) return -ENODEV; } -static int of_get_mac_addr_nvmem(struct device_node *np, u8 addr) +static int of_get_mac_addr_nvmem(struct device_node *np, u8 *addr) { struct platform_device *pdev = of_find_device_by_node(np); + struct nvmem_cell *cell; + const void *mac; + size_t len; int ret; - if (!pdev) - return -ENODEV; + /* Try lookup by device first, there might be a nvmem_cell_lookup + * associated with a given device. + */ + if (pdev) { + ret = nvmem_get_mac_address(&pdev->dev, addr); + put_device(&pdev->dev); + return ret; + } + + cell = of_nvmem_cell_get(np, "mac-address"); + if (IS_ERR(cell)) + return PTR_ERR(cell); + + mac = nvmem_cell_read(cell, &len); + nvmem_cell_put(cell); + + if (IS_ERR(mac)) + return PTR_ERR(mac); + + if (len != ETH_ALEN || !is_valid_ether_addr(mac)) { + kfree(mac); + return -EINVAL; + } - ret = nvmem_get_mac_address(&pdev->dev, addr); - put_device(&pdev->dev); + ether_addr_copy(addr, mac); + kfree(mac); - return ret; + return 0; } /** -- 2.20.1