ea6e6974ca026a68442fd0ea8aa6d5f4b3dfc4be
[openwrt/staging/hauke.git] /
1 From 83216e3988cd196183542937c9bd58b279f946af Mon Sep 17 00:00:00 2001
2 From: Michael Walle <michael@walle.cc>
3 Date: Mon, 12 Apr 2021 19:47:17 +0200
4 Subject: of: net: pass the dst buffer to of_get_mac_address()
5
6 of_get_mac_address() returns a "const void*" pointer to a MAC address.
7 Lately, support to fetch the MAC address by an NVMEM provider was added.
8 But this will only work with platform devices. It will not work with
9 PCI devices (e.g. of an integrated root complex) and esp. not with DSA
10 ports.
11
12 There is an of_* variant of the nvmem binding which works without
13 devices. The returned data of a nvmem_cell_read() has to be freed after
14 use. On the other hand the return of_get_mac_address() points to some
15 static data without a lifetime. The trick for now, was to allocate a
16 device resource managed buffer which is then returned. This will only
17 work if we have an actual device.
18
19 Change it, so that the caller of of_get_mac_address() has to supply a
20 buffer where the MAC address is written to. Unfortunately, this will
21 touch all drivers which use the of_get_mac_address().
22
23 Usually the code looks like:
24
25 const char *addr;
26 addr = of_get_mac_address(np);
27 if (!IS_ERR(addr))
28 ether_addr_copy(ndev->dev_addr, addr);
29
30 This can then be simply rewritten as:
31
32 of_get_mac_address(np, ndev->dev_addr);
33
34 Sometimes is_valid_ether_addr() is used to test the MAC address.
35 of_get_mac_address() already makes sure, it just returns a valid MAC
36 address. Thus we can just test its return code. But we have to be
37 careful if there are still other sources for the MAC address before the
38 of_get_mac_address(). In this case we have to keep the
39 is_valid_ether_addr() call.
40
41 The following coccinelle patch was used to convert common cases to the
42 new style. Afterwards, I've manually gone over the drivers and fixed the
43 return code variable: either used a new one or if one was already
44 available use that. Mansour Moufid, thanks for that coccinelle patch!
45
46 <spml>
47 @a@
48 identifier x;
49 expression y, z;
50 @@
51 - x = of_get_mac_address(y);
52 + x = of_get_mac_address(y, z);
53 <...
54 - ether_addr_copy(z, x);
55 ...>
56
57 @@
58 identifier a.x;
59 @@
60 - if (<+... x ...+>) {}
61
62 @@
63 identifier a.x;
64 @@
65 if (<+... x ...+>) {
66 ...
67 }
68 - else {}
69
70 @@
71 identifier a.x;
72 expression e;
73 @@
74 - if (<+... x ...+>@e)
75 - {}
76 - else
77 + if (!(e))
78 {...}
79
80 @@
81 expression x, y, z;
82 @@
83 - x = of_get_mac_address(y, z);
84 + of_get_mac_address(y, z);
85 ... when != x
86 </spml>
87
88 All drivers, except drivers/net/ethernet/aeroflex/greth.c, were
89 compile-time tested.
90
91 Suggested-by: Andrew Lunn <andrew@lunn.ch>
92 Signed-off-by: Michael Walle <michael@walle.cc>
93 Reviewed-by: Andrew Lunn <andrew@lunn.ch>
94 Signed-off-by: David S. Miller <davem@davemloft.net>
95 ---
96 arch/arm/mach-mvebu/kirkwood.c | 3 +-
97 arch/powerpc/sysdev/tsi108_dev.c | 5 +-
98 drivers/net/ethernet/aeroflex/greth.c | 6 +--
99 drivers/net/ethernet/allwinner/sun4i-emac.c | 10 ++--
100 drivers/net/ethernet/altera/altera_tse_main.c | 7 +--
101 drivers/net/ethernet/arc/emac_main.c | 8 +--
102 drivers/net/ethernet/atheros/ag71xx.c | 7 +--
103 drivers/net/ethernet/broadcom/bcm4908_enet.c | 7 +--
104 drivers/net/ethernet/broadcom/bcmsysport.c | 7 +--
105 drivers/net/ethernet/broadcom/bgmac-bcma.c | 10 ++--
106 drivers/net/ethernet/broadcom/bgmac-platform.c | 11 ++--
107 drivers/net/ethernet/cadence/macb_main.c | 11 ++--
108 drivers/net/ethernet/cavium/octeon/octeon_mgmt.c | 8 +--
109 drivers/net/ethernet/cavium/thunder/thunder_bgx.c | 5 +-
110 drivers/net/ethernet/davicom/dm9000.c | 10 ++--
111 drivers/net/ethernet/ethoc.c | 6 +--
112 drivers/net/ethernet/ezchip/nps_enet.c | 7 +--
113 drivers/net/ethernet/freescale/fec_main.c | 7 +--
114 drivers/net/ethernet/freescale/fec_mpc52xx.c | 7 +--
115 drivers/net/ethernet/freescale/fman/mac.c | 9 ++--
116 .../net/ethernet/freescale/fs_enet/fs_enet-main.c | 5 +-
117 drivers/net/ethernet/freescale/gianfar.c | 8 +--
118 drivers/net/ethernet/freescale/ucc_geth.c | 5 +-
119 drivers/net/ethernet/hisilicon/hisi_femac.c | 7 +--
120 drivers/net/ethernet/hisilicon/hix5hd2_gmac.c | 7 +--
121 drivers/net/ethernet/lantiq_xrx200.c | 7 +--
122 drivers/net/ethernet/marvell/mv643xx_eth.c | 5 +-
123 drivers/net/ethernet/marvell/mvneta.c | 6 +--
124 .../net/ethernet/marvell/prestera/prestera_main.c | 11 ++--
125 drivers/net/ethernet/marvell/pxa168_eth.c | 9 +---
126 drivers/net/ethernet/marvell/sky2.c | 8 ++-
127 drivers/net/ethernet/mediatek/mtk_eth_soc.c | 11 ++--
128 drivers/net/ethernet/micrel/ks8851_common.c | 7 ++-
129 drivers/net/ethernet/microchip/lan743x_main.c | 5 +-
130 drivers/net/ethernet/nxp/lpc_eth.c | 4 +-
131 drivers/net/ethernet/qualcomm/qca_spi.c | 10 ++--
132 drivers/net/ethernet/qualcomm/qca_uart.c | 9 +---
133 drivers/net/ethernet/renesas/ravb_main.c | 12 +++--
134 drivers/net/ethernet/renesas/sh_eth.c | 5 +-
135 .../net/ethernet/samsung/sxgbe/sxgbe_platform.c | 13 ++---
136 drivers/net/ethernet/socionext/sni_ave.c | 10 ++--
137 .../net/ethernet/stmicro/stmmac/dwmac-anarion.c | 2 +-
138 .../ethernet/stmicro/stmmac/dwmac-dwc-qos-eth.c | 2 +-
139 .../net/ethernet/stmicro/stmmac/dwmac-generic.c | 2 +-
140 drivers/net/ethernet/stmicro/stmmac/dwmac-imx.c | 2 +-
141 .../net/ethernet/stmicro/stmmac/dwmac-intel-plat.c | 2 +-
142 .../net/ethernet/stmicro/stmmac/dwmac-ipq806x.c | 2 +-
143 .../net/ethernet/stmicro/stmmac/dwmac-lpc18xx.c | 2 +-
144 .../net/ethernet/stmicro/stmmac/dwmac-mediatek.c | 2 +-
145 drivers/net/ethernet/stmicro/stmmac/dwmac-meson.c | 2 +-
146 .../net/ethernet/stmicro/stmmac/dwmac-meson8b.c | 2 +-
147 drivers/net/ethernet/stmicro/stmmac/dwmac-oxnas.c | 2 +-
148 .../ethernet/stmicro/stmmac/dwmac-qcom-ethqos.c | 2 +-
149 drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c | 2 +-
150 .../net/ethernet/stmicro/stmmac/dwmac-socfpga.c | 2 +-
151 drivers/net/ethernet/stmicro/stmmac/dwmac-sti.c | 2 +-
152 drivers/net/ethernet/stmicro/stmmac/dwmac-stm32.c | 2 +-
153 drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c | 2 +-
154 drivers/net/ethernet/stmicro/stmmac/dwmac-sunxi.c | 2 +-
155 .../net/ethernet/stmicro/stmmac/dwmac-visconti.c | 2 +-
156 drivers/net/ethernet/stmicro/stmmac/stmmac.h | 2 +-
157 drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 2 +-
158 .../net/ethernet/stmicro/stmmac/stmmac_platform.c | 14 ++---
159 .../net/ethernet/stmicro/stmmac/stmmac_platform.h | 2 +-
160 drivers/net/ethernet/ti/am65-cpsw-nuss.c | 19 ++++---
161 drivers/net/ethernet/ti/cpsw.c | 7 +--
162 drivers/net/ethernet/ti/cpsw_new.c | 7 +--
163 drivers/net/ethernet/ti/davinci_emac.c | 8 +--
164 drivers/net/ethernet/ti/netcp_core.c | 7 +--
165 drivers/net/ethernet/wiznet/w5100-spi.c | 8 ++-
166 drivers/net/ethernet/wiznet/w5100.c | 2 +-
167 drivers/net/ethernet/xilinx/ll_temac_main.c | 8 +--
168 drivers/net/ethernet/xilinx/xilinx_axienet_main.c | 15 +++---
169 drivers/net/ethernet/xilinx/xilinx_emaclite.c | 8 +--
170 drivers/net/wireless/ath/ath9k/init.c | 5 +-
171 drivers/net/wireless/mediatek/mt76/eeprom.c | 9 +---
172 drivers/net/wireless/ralink/rt2x00/rt2x00dev.c | 6 +--
173 drivers/of/of_net.c | 60 ++++++++++------------
174 drivers/staging/octeon/ethernet.c | 10 ++--
175 drivers/staging/wfx/main.c | 7 ++-
176 include/linux/of_net.h | 6 +--
177 include/net/dsa.h | 2 +-
178 net/dsa/dsa2.c | 2 +-
179 net/dsa/slave.c | 2 +-
180 net/ethernet/eth.c | 11 ++--
181 85 files changed, 218 insertions(+), 364 deletions(-)
182
183 --- a/drivers/net/wireless/ath/ath9k/init.c
184 +++ b/drivers/net/wireless/ath/ath9k/init.c
185 @@ -618,7 +618,6 @@ static int ath9k_of_init(struct ath_soft
186 struct ath_hw *ah = sc->sc_ah;
187 struct ath_common *common = ath9k_hw_common(ah);
188 enum ath_bus_type bus_type = common->bus_ops->ath_bus_type;
189 - const char *mac;
190 char eeprom_name[100];
191 int ret;
192
193 @@ -641,9 +640,7 @@ static int ath9k_of_init(struct ath_soft
194 ah->ah_flags |= AH_NO_EEP_SWAP;
195 }
196
197 - mac = of_get_mac_address(np);
198 - if (!IS_ERR(mac))
199 - ether_addr_copy(common->macaddr, mac);
200 + of_get_mac_address(np, common->macaddr);
201
202 return 0;
203 }
204 --- a/drivers/net/wireless/mediatek/mt76/eeprom.c
205 +++ b/drivers/net/wireless/mediatek/mt76/eeprom.c
206 @@ -91,16 +91,9 @@ void
207 mt76_eeprom_override(struct mt76_phy *phy)
208 {
209 struct mt76_dev *dev = phy->dev;
210 -
211 -#ifdef CONFIG_OF
212 struct device_node *np = dev->dev->of_node;
213 - const u8 *mac = NULL;
214
215 - if (np)
216 - mac = of_get_mac_address(np);
217 - if (!IS_ERR_OR_NULL(mac))
218 - ether_addr_copy(phy->macaddr, mac);
219 -#endif
220 + of_get_mac_address(np, phy->macaddr);
221
222 if (!is_valid_ether_addr(phy->macaddr)) {
223 eth_random_addr(phy->macaddr);
224 --- a/drivers/net/wireless/ralink/rt2x00/rt2x00dev.c
225 +++ b/drivers/net/wireless/ralink/rt2x00/rt2x00dev.c
226 @@ -989,11 +989,7 @@ static void rt2x00lib_rate(struct ieee80
227
228 void rt2x00lib_set_mac_address(struct rt2x00_dev *rt2x00dev, u8 *eeprom_mac_addr)
229 {
230 - const char *mac_addr;
231 -
232 - mac_addr = of_get_mac_address(rt2x00dev->dev->of_node);
233 - if (!IS_ERR(mac_addr))
234 - ether_addr_copy(eeprom_mac_addr, mac_addr);
235 + of_get_mac_address(rt2x00dev->dev->of_node, eeprom_mac_addr);
236
237 if (!is_valid_ether_addr(eeprom_mac_addr)) {
238 eth_random_addr(eeprom_mac_addr);