blob: c4765bbe527b064d87dec0dab820344ac31b1c84 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002/*
3 * Broadcom GENET (Gigabit Ethernet) controller driver
4 *
Doug Berger99d55632019-12-17 16:51:08 -08005 * Copyright (c) 2014-2019 Broadcom
Florian Fainelli1c1008c2014-02-13 16:08:47 -08006 */
7
8#define pr_fmt(fmt) "bcmgenet: " fmt
9
Jeremy Linton99c6b062020-02-24 16:54:01 -060010#include <linux/acpi.h>
Florian Fainelli1c1008c2014-02-13 16:08:47 -080011#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/sched.h>
14#include <linux/types.h>
15#include <linux/fcntl.h>
16#include <linux/interrupt.h>
17#include <linux/string.h>
18#include <linux/if_ether.h>
19#include <linux/init.h>
20#include <linux/errno.h>
21#include <linux/delay.h>
22#include <linux/platform_device.h>
23#include <linux/dma-mapping.h>
24#include <linux/pm.h>
25#include <linux/clk.h>
Florian Fainelli1c1008c2014-02-13 16:08:47 -080026#include <linux/of.h>
27#include <linux/of_address.h>
28#include <linux/of_irq.h>
29#include <linux/of_net.h>
30#include <linux/of_platform.h>
31#include <net/arp.h>
32
33#include <linux/mii.h>
34#include <linux/ethtool.h>
35#include <linux/netdevice.h>
36#include <linux/inetdevice.h>
37#include <linux/etherdevice.h>
38#include <linux/skbuff.h>
39#include <linux/in.h>
40#include <linux/ip.h>
41#include <linux/ipv6.h>
42#include <linux/phy.h>
Petri Gyntherb0ba5122014-12-01 16:18:08 -080043#include <linux/platform_data/bcmgenet.h>
Florian Fainelli1c1008c2014-02-13 16:08:47 -080044
45#include <asm/unaligned.h>
46
47#include "bcmgenet.h"
48
49/* Maximum number of hardware queues, downsized if needed */
50#define GENET_MAX_MQ_CNT 4
51
52/* Default highest priority queue for multi queue support */
53#define GENET_Q0_PRIORITY 0
54
Petri Gynther3feafa02015-03-05 17:40:14 -080055#define GENET_Q16_RX_BD_CNT \
56 (TOTAL_DESC - priv->hw_params->rx_queues * priv->hw_params->rx_bds_per_q)
Petri Gynther51a966a2015-02-23 11:00:46 -080057#define GENET_Q16_TX_BD_CNT \
58 (TOTAL_DESC - priv->hw_params->tx_queues * priv->hw_params->tx_bds_per_q)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080059
60#define RX_BUF_LENGTH 2048
61#define SKB_ALIGNMENT 32
62
63/* Tx/Rx DMA register offset, skip 256 descriptors */
64#define WORDS_PER_BD(p) (p->hw_params->words_per_bd)
65#define DMA_DESC_SIZE (WORDS_PER_BD(priv) * sizeof(u32))
66
67#define GENET_TDMA_REG_OFF (priv->hw_params->tdma_offset + \
68 TOTAL_DESC * DMA_DESC_SIZE)
69
70#define GENET_RDMA_REG_OFF (priv->hw_params->rdma_offset + \
71 TOTAL_DESC * DMA_DESC_SIZE)
72
Florian Fainelli69d2ea92017-08-29 12:25:31 -070073static inline void bcmgenet_writel(u32 value, void __iomem *offset)
74{
75 /* MIPS chips strapped for BE will automagically configure the
76 * peripheral registers for CPU-native byte order.
77 */
78 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
79 __raw_writel(value, offset);
80 else
81 writel_relaxed(value, offset);
82}
83
84static inline u32 bcmgenet_readl(void __iomem *offset)
85{
86 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
87 return __raw_readl(offset);
88 else
89 return readl_relaxed(offset);
90}
91
Florian Fainelli1c1008c2014-02-13 16:08:47 -080092static inline void dmadesc_set_length_status(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -070093 void __iomem *d, u32 value)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080094{
Florian Fainelli69d2ea92017-08-29 12:25:31 -070095 bcmgenet_writel(value, d + DMA_DESC_LENGTH_STATUS);
Florian Fainelli1c1008c2014-02-13 16:08:47 -080096}
97
Florian Fainelli1c1008c2014-02-13 16:08:47 -080098static inline void dmadesc_set_addr(struct bcmgenet_priv *priv,
99 void __iomem *d,
100 dma_addr_t addr)
101{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700102 bcmgenet_writel(lower_32_bits(addr), d + DMA_DESC_ADDRESS_LO);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800103
104 /* Register writes to GISB bus can take couple hundred nanoseconds
105 * and are done for each packet, save these expensive writes unless
Brian Norris7fc527f2014-07-29 14:34:14 -0700106 * the platform is explicitly configured for 64-bits/LPAE.
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800107 */
108#ifdef CONFIG_PHYS_ADDR_T_64BIT
109 if (priv->hw_params->flags & GENET_HAS_40BITS)
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700110 bcmgenet_writel(upper_32_bits(addr), d + DMA_DESC_ADDRESS_HI);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800111#endif
112}
113
114/* Combined address + length/status setter */
115static inline void dmadesc_set(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700116 void __iomem *d, dma_addr_t addr, u32 val)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800117{
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800118 dmadesc_set_addr(priv, d, addr);
Petri Gynther7ee40622016-04-05 14:00:01 -0700119 dmadesc_set_length_status(priv, d, val);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800120}
121
122static inline dma_addr_t dmadesc_get_addr(struct bcmgenet_priv *priv,
123 void __iomem *d)
124{
125 dma_addr_t addr;
126
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700127 addr = bcmgenet_readl(d + DMA_DESC_ADDRESS_LO);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800128
129 /* Register writes to GISB bus can take couple hundred nanoseconds
130 * and are done for each packet, save these expensive writes unless
Brian Norris7fc527f2014-07-29 14:34:14 -0700131 * the platform is explicitly configured for 64-bits/LPAE.
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800132 */
133#ifdef CONFIG_PHYS_ADDR_T_64BIT
134 if (priv->hw_params->flags & GENET_HAS_40BITS)
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700135 addr |= (u64)bcmgenet_readl(d + DMA_DESC_ADDRESS_HI) << 32;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800136#endif
137 return addr;
138}
139
140#define GENET_VER_FMT "%1d.%1d EPHY: 0x%04x"
141
142#define GENET_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
143 NETIF_MSG_LINK)
144
145static inline u32 bcmgenet_rbuf_ctrl_get(struct bcmgenet_priv *priv)
146{
147 if (GENET_IS_V1(priv))
148 return bcmgenet_rbuf_readl(priv, RBUF_FLUSH_CTRL_V1);
149 else
150 return bcmgenet_sys_readl(priv, SYS_RBUF_FLUSH_CTRL);
151}
152
153static inline void bcmgenet_rbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
154{
155 if (GENET_IS_V1(priv))
156 bcmgenet_rbuf_writel(priv, val, RBUF_FLUSH_CTRL_V1);
157 else
158 bcmgenet_sys_writel(priv, val, SYS_RBUF_FLUSH_CTRL);
159}
160
161/* These macros are defined to deal with register map change
162 * between GENET1.1 and GENET2. Only those currently being used
163 * by driver are defined.
164 */
165static inline u32 bcmgenet_tbuf_ctrl_get(struct bcmgenet_priv *priv)
166{
167 if (GENET_IS_V1(priv))
168 return bcmgenet_rbuf_readl(priv, TBUF_CTRL_V1);
169 else
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700170 return bcmgenet_readl(priv->base +
171 priv->hw_params->tbuf_offset + TBUF_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800172}
173
174static inline void bcmgenet_tbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
175{
176 if (GENET_IS_V1(priv))
177 bcmgenet_rbuf_writel(priv, val, TBUF_CTRL_V1);
178 else
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700179 bcmgenet_writel(val, priv->base +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800180 priv->hw_params->tbuf_offset + TBUF_CTRL);
181}
182
183static inline u32 bcmgenet_bp_mc_get(struct bcmgenet_priv *priv)
184{
185 if (GENET_IS_V1(priv))
186 return bcmgenet_rbuf_readl(priv, TBUF_BP_MC_V1);
187 else
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700188 return bcmgenet_readl(priv->base +
189 priv->hw_params->tbuf_offset + TBUF_BP_MC);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800190}
191
192static inline void bcmgenet_bp_mc_set(struct bcmgenet_priv *priv, u32 val)
193{
194 if (GENET_IS_V1(priv))
195 bcmgenet_rbuf_writel(priv, val, TBUF_BP_MC_V1);
196 else
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700197 bcmgenet_writel(val, priv->base +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800198 priv->hw_params->tbuf_offset + TBUF_BP_MC);
199}
200
201/* RX/TX DMA register accessors */
202enum dma_reg {
203 DMA_RING_CFG = 0,
204 DMA_CTRL,
205 DMA_STATUS,
206 DMA_SCB_BURST_SIZE,
207 DMA_ARB_CTRL,
Petri Gynther37742162014-10-07 09:30:01 -0700208 DMA_PRIORITY_0,
209 DMA_PRIORITY_1,
210 DMA_PRIORITY_2,
Petri Gynther0034de42015-03-13 14:45:00 -0700211 DMA_INDEX2RING_0,
212 DMA_INDEX2RING_1,
213 DMA_INDEX2RING_2,
214 DMA_INDEX2RING_3,
215 DMA_INDEX2RING_4,
216 DMA_INDEX2RING_5,
217 DMA_INDEX2RING_6,
218 DMA_INDEX2RING_7,
Florian Fainelli4a296452015-09-16 16:47:40 -0700219 DMA_RING0_TIMEOUT,
220 DMA_RING1_TIMEOUT,
221 DMA_RING2_TIMEOUT,
222 DMA_RING3_TIMEOUT,
223 DMA_RING4_TIMEOUT,
224 DMA_RING5_TIMEOUT,
225 DMA_RING6_TIMEOUT,
226 DMA_RING7_TIMEOUT,
227 DMA_RING8_TIMEOUT,
228 DMA_RING9_TIMEOUT,
229 DMA_RING10_TIMEOUT,
230 DMA_RING11_TIMEOUT,
231 DMA_RING12_TIMEOUT,
232 DMA_RING13_TIMEOUT,
233 DMA_RING14_TIMEOUT,
234 DMA_RING15_TIMEOUT,
235 DMA_RING16_TIMEOUT,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800236};
237
238static const u8 bcmgenet_dma_regs_v3plus[] = {
239 [DMA_RING_CFG] = 0x00,
240 [DMA_CTRL] = 0x04,
241 [DMA_STATUS] = 0x08,
242 [DMA_SCB_BURST_SIZE] = 0x0C,
243 [DMA_ARB_CTRL] = 0x2C,
Petri Gynther37742162014-10-07 09:30:01 -0700244 [DMA_PRIORITY_0] = 0x30,
245 [DMA_PRIORITY_1] = 0x34,
246 [DMA_PRIORITY_2] = 0x38,
Florian Fainelli4a296452015-09-16 16:47:40 -0700247 [DMA_RING0_TIMEOUT] = 0x2C,
248 [DMA_RING1_TIMEOUT] = 0x30,
249 [DMA_RING2_TIMEOUT] = 0x34,
250 [DMA_RING3_TIMEOUT] = 0x38,
251 [DMA_RING4_TIMEOUT] = 0x3c,
252 [DMA_RING5_TIMEOUT] = 0x40,
253 [DMA_RING6_TIMEOUT] = 0x44,
254 [DMA_RING7_TIMEOUT] = 0x48,
255 [DMA_RING8_TIMEOUT] = 0x4c,
256 [DMA_RING9_TIMEOUT] = 0x50,
257 [DMA_RING10_TIMEOUT] = 0x54,
258 [DMA_RING11_TIMEOUT] = 0x58,
259 [DMA_RING12_TIMEOUT] = 0x5c,
260 [DMA_RING13_TIMEOUT] = 0x60,
261 [DMA_RING14_TIMEOUT] = 0x64,
262 [DMA_RING15_TIMEOUT] = 0x68,
263 [DMA_RING16_TIMEOUT] = 0x6C,
Petri Gynther0034de42015-03-13 14:45:00 -0700264 [DMA_INDEX2RING_0] = 0x70,
265 [DMA_INDEX2RING_1] = 0x74,
266 [DMA_INDEX2RING_2] = 0x78,
267 [DMA_INDEX2RING_3] = 0x7C,
268 [DMA_INDEX2RING_4] = 0x80,
269 [DMA_INDEX2RING_5] = 0x84,
270 [DMA_INDEX2RING_6] = 0x88,
271 [DMA_INDEX2RING_7] = 0x8C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800272};
273
274static const u8 bcmgenet_dma_regs_v2[] = {
275 [DMA_RING_CFG] = 0x00,
276 [DMA_CTRL] = 0x04,
277 [DMA_STATUS] = 0x08,
278 [DMA_SCB_BURST_SIZE] = 0x0C,
279 [DMA_ARB_CTRL] = 0x30,
Petri Gynther37742162014-10-07 09:30:01 -0700280 [DMA_PRIORITY_0] = 0x34,
281 [DMA_PRIORITY_1] = 0x38,
282 [DMA_PRIORITY_2] = 0x3C,
Florian Fainelli4a296452015-09-16 16:47:40 -0700283 [DMA_RING0_TIMEOUT] = 0x2C,
284 [DMA_RING1_TIMEOUT] = 0x30,
285 [DMA_RING2_TIMEOUT] = 0x34,
286 [DMA_RING3_TIMEOUT] = 0x38,
287 [DMA_RING4_TIMEOUT] = 0x3c,
288 [DMA_RING5_TIMEOUT] = 0x40,
289 [DMA_RING6_TIMEOUT] = 0x44,
290 [DMA_RING7_TIMEOUT] = 0x48,
291 [DMA_RING8_TIMEOUT] = 0x4c,
292 [DMA_RING9_TIMEOUT] = 0x50,
293 [DMA_RING10_TIMEOUT] = 0x54,
294 [DMA_RING11_TIMEOUT] = 0x58,
295 [DMA_RING12_TIMEOUT] = 0x5c,
296 [DMA_RING13_TIMEOUT] = 0x60,
297 [DMA_RING14_TIMEOUT] = 0x64,
298 [DMA_RING15_TIMEOUT] = 0x68,
299 [DMA_RING16_TIMEOUT] = 0x6C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800300};
301
302static const u8 bcmgenet_dma_regs_v1[] = {
303 [DMA_CTRL] = 0x00,
304 [DMA_STATUS] = 0x04,
305 [DMA_SCB_BURST_SIZE] = 0x0C,
306 [DMA_ARB_CTRL] = 0x30,
Petri Gynther37742162014-10-07 09:30:01 -0700307 [DMA_PRIORITY_0] = 0x34,
308 [DMA_PRIORITY_1] = 0x38,
309 [DMA_PRIORITY_2] = 0x3C,
Florian Fainelli4a296452015-09-16 16:47:40 -0700310 [DMA_RING0_TIMEOUT] = 0x2C,
311 [DMA_RING1_TIMEOUT] = 0x30,
312 [DMA_RING2_TIMEOUT] = 0x34,
313 [DMA_RING3_TIMEOUT] = 0x38,
314 [DMA_RING4_TIMEOUT] = 0x3c,
315 [DMA_RING5_TIMEOUT] = 0x40,
316 [DMA_RING6_TIMEOUT] = 0x44,
317 [DMA_RING7_TIMEOUT] = 0x48,
318 [DMA_RING8_TIMEOUT] = 0x4c,
319 [DMA_RING9_TIMEOUT] = 0x50,
320 [DMA_RING10_TIMEOUT] = 0x54,
321 [DMA_RING11_TIMEOUT] = 0x58,
322 [DMA_RING12_TIMEOUT] = 0x5c,
323 [DMA_RING13_TIMEOUT] = 0x60,
324 [DMA_RING14_TIMEOUT] = 0x64,
325 [DMA_RING15_TIMEOUT] = 0x68,
326 [DMA_RING16_TIMEOUT] = 0x6C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800327};
328
329/* Set at runtime once bcmgenet version is known */
330static const u8 *bcmgenet_dma_regs;
331
332static inline struct bcmgenet_priv *dev_to_priv(struct device *dev)
333{
334 return netdev_priv(dev_get_drvdata(dev));
335}
336
337static inline u32 bcmgenet_tdma_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700338 enum dma_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800339{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700340 return bcmgenet_readl(priv->base + GENET_TDMA_REG_OFF +
341 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800342}
343
344static inline void bcmgenet_tdma_writel(struct bcmgenet_priv *priv,
345 u32 val, enum dma_reg r)
346{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700347 bcmgenet_writel(val, priv->base + GENET_TDMA_REG_OFF +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800348 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
349}
350
351static inline u32 bcmgenet_rdma_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700352 enum dma_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800353{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700354 return bcmgenet_readl(priv->base + GENET_RDMA_REG_OFF +
355 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800356}
357
358static inline void bcmgenet_rdma_writel(struct bcmgenet_priv *priv,
359 u32 val, enum dma_reg r)
360{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700361 bcmgenet_writel(val, priv->base + GENET_RDMA_REG_OFF +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800362 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
363}
364
365/* RDMA/TDMA ring registers and accessors
366 * we merge the common fields and just prefix with T/D the registers
367 * having different meaning depending on the direction
368 */
369enum dma_ring_reg {
370 TDMA_READ_PTR = 0,
371 RDMA_WRITE_PTR = TDMA_READ_PTR,
372 TDMA_READ_PTR_HI,
373 RDMA_WRITE_PTR_HI = TDMA_READ_PTR_HI,
374 TDMA_CONS_INDEX,
375 RDMA_PROD_INDEX = TDMA_CONS_INDEX,
376 TDMA_PROD_INDEX,
377 RDMA_CONS_INDEX = TDMA_PROD_INDEX,
378 DMA_RING_BUF_SIZE,
379 DMA_START_ADDR,
380 DMA_START_ADDR_HI,
381 DMA_END_ADDR,
382 DMA_END_ADDR_HI,
383 DMA_MBUF_DONE_THRESH,
384 TDMA_FLOW_PERIOD,
385 RDMA_XON_XOFF_THRESH = TDMA_FLOW_PERIOD,
386 TDMA_WRITE_PTR,
387 RDMA_READ_PTR = TDMA_WRITE_PTR,
388 TDMA_WRITE_PTR_HI,
389 RDMA_READ_PTR_HI = TDMA_WRITE_PTR_HI
390};
391
392/* GENET v4 supports 40-bits pointer addressing
393 * for obvious reasons the LO and HI word parts
394 * are contiguous, but this offsets the other
395 * registers.
396 */
397static const u8 genet_dma_ring_regs_v4[] = {
398 [TDMA_READ_PTR] = 0x00,
399 [TDMA_READ_PTR_HI] = 0x04,
400 [TDMA_CONS_INDEX] = 0x08,
401 [TDMA_PROD_INDEX] = 0x0C,
402 [DMA_RING_BUF_SIZE] = 0x10,
403 [DMA_START_ADDR] = 0x14,
404 [DMA_START_ADDR_HI] = 0x18,
405 [DMA_END_ADDR] = 0x1C,
406 [DMA_END_ADDR_HI] = 0x20,
407 [DMA_MBUF_DONE_THRESH] = 0x24,
408 [TDMA_FLOW_PERIOD] = 0x28,
409 [TDMA_WRITE_PTR] = 0x2C,
410 [TDMA_WRITE_PTR_HI] = 0x30,
411};
412
413static const u8 genet_dma_ring_regs_v123[] = {
414 [TDMA_READ_PTR] = 0x00,
415 [TDMA_CONS_INDEX] = 0x04,
416 [TDMA_PROD_INDEX] = 0x08,
417 [DMA_RING_BUF_SIZE] = 0x0C,
418 [DMA_START_ADDR] = 0x10,
419 [DMA_END_ADDR] = 0x14,
420 [DMA_MBUF_DONE_THRESH] = 0x18,
421 [TDMA_FLOW_PERIOD] = 0x1C,
422 [TDMA_WRITE_PTR] = 0x20,
423};
424
425/* Set at runtime once GENET version is known */
426static const u8 *genet_dma_ring_regs;
427
428static inline u32 bcmgenet_tdma_ring_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700429 unsigned int ring,
430 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800431{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700432 return bcmgenet_readl(priv->base + GENET_TDMA_REG_OFF +
433 (DMA_RING_SIZE * ring) +
434 genet_dma_ring_regs[r]);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800435}
436
437static inline void bcmgenet_tdma_ring_writel(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700438 unsigned int ring, u32 val,
439 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800440{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700441 bcmgenet_writel(val, priv->base + GENET_TDMA_REG_OFF +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800442 (DMA_RING_SIZE * ring) +
443 genet_dma_ring_regs[r]);
444}
445
446static inline u32 bcmgenet_rdma_ring_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700447 unsigned int ring,
448 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800449{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700450 return bcmgenet_readl(priv->base + GENET_RDMA_REG_OFF +
451 (DMA_RING_SIZE * ring) +
452 genet_dma_ring_regs[r]);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800453}
454
455static inline void bcmgenet_rdma_ring_writel(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700456 unsigned int ring, u32 val,
457 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800458{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700459 bcmgenet_writel(val, priv->base + GENET_RDMA_REG_OFF +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800460 (DMA_RING_SIZE * ring) +
461 genet_dma_ring_regs[r]);
462}
463
Edwin Chan89316fa2017-03-09 16:58:49 -0800464static int bcmgenet_begin(struct net_device *dev)
465{
466 struct bcmgenet_priv *priv = netdev_priv(dev);
467
468 /* Turn on the clock */
469 return clk_prepare_enable(priv->clk);
470}
471
472static void bcmgenet_complete(struct net_device *dev)
473{
474 struct bcmgenet_priv *priv = netdev_priv(dev);
475
476 /* Turn off the clock */
477 clk_disable_unprepare(priv->clk);
478}
479
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200480static int bcmgenet_get_link_ksettings(struct net_device *dev,
481 struct ethtool_link_ksettings *cmd)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200482{
483 if (!netif_running(dev))
484 return -EINVAL;
485
Doug Berger6c97f012017-10-25 15:04:19 -0700486 if (!dev->phydev)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200487 return -ENODEV;
488
Doug Berger6c97f012017-10-25 15:04:19 -0700489 phy_ethtool_ksettings_get(dev->phydev, cmd);
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +0300490
491 return 0;
Philippe Reynesbac65c42016-07-09 00:54:47 +0200492}
493
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200494static int bcmgenet_set_link_ksettings(struct net_device *dev,
495 const struct ethtool_link_ksettings *cmd)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200496{
497 if (!netif_running(dev))
498 return -EINVAL;
499
Doug Berger6c97f012017-10-25 15:04:19 -0700500 if (!dev->phydev)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200501 return -ENODEV;
502
Doug Berger6c97f012017-10-25 15:04:19 -0700503 return phy_ethtool_ksettings_set(dev->phydev, cmd);
Philippe Reynesbac65c42016-07-09 00:54:47 +0200504}
505
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800506static int bcmgenet_set_features(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700507 netdev_features_t features)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800508{
Doug Bergerf63db4e2019-12-17 16:51:11 -0800509 struct bcmgenet_priv *priv = netdev_priv(dev);
510 u32 reg;
511 int ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800512
Doug Bergerf63db4e2019-12-17 16:51:11 -0800513 ret = clk_prepare_enable(priv->clk);
514 if (ret)
515 return ret;
516
517 /* Make sure we reflect the value of CRC_CMD_FWD */
518 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
519 priv->crc_fwd_en = !!(reg & CMD_CRC_FWD);
520
Doug Bergerf63db4e2019-12-17 16:51:11 -0800521 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800522
523 return ret;
524}
525
526static u32 bcmgenet_get_msglevel(struct net_device *dev)
527{
528 struct bcmgenet_priv *priv = netdev_priv(dev);
529
530 return priv->msg_enable;
531}
532
533static void bcmgenet_set_msglevel(struct net_device *dev, u32 level)
534{
535 struct bcmgenet_priv *priv = netdev_priv(dev);
536
537 priv->msg_enable = level;
538}
539
Florian Fainelli2f913072015-09-16 16:47:39 -0700540static int bcmgenet_get_coalesce(struct net_device *dev,
541 struct ethtool_coalesce *ec)
542{
543 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700544 struct bcmgenet_rx_ring *ring;
545 unsigned int i;
Florian Fainelli2f913072015-09-16 16:47:39 -0700546
547 ec->tx_max_coalesced_frames =
548 bcmgenet_tdma_ring_readl(priv, DESC_INDEX,
549 DMA_MBUF_DONE_THRESH);
Florian Fainelli4a296452015-09-16 16:47:40 -0700550 ec->rx_max_coalesced_frames =
551 bcmgenet_rdma_ring_readl(priv, DESC_INDEX,
552 DMA_MBUF_DONE_THRESH);
553 ec->rx_coalesce_usecs =
554 bcmgenet_rdma_readl(priv, DMA_RING16_TIMEOUT) * 8192 / 1000;
Florian Fainelli2f913072015-09-16 16:47:39 -0700555
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700556 for (i = 0; i < priv->hw_params->rx_queues; i++) {
557 ring = &priv->rx_rings[i];
558 ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
559 }
560 ring = &priv->rx_rings[DESC_INDEX];
561 ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
562
Florian Fainelli2f913072015-09-16 16:47:39 -0700563 return 0;
564}
565
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700566static void bcmgenet_set_rx_coalesce(struct bcmgenet_rx_ring *ring,
567 u32 usecs, u32 pkts)
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700568{
569 struct bcmgenet_priv *priv = ring->priv;
570 unsigned int i = ring->index;
571 u32 reg;
572
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700573 bcmgenet_rdma_ring_writel(priv, i, pkts, DMA_MBUF_DONE_THRESH);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700574
575 reg = bcmgenet_rdma_readl(priv, DMA_RING0_TIMEOUT + i);
576 reg &= ~DMA_TIMEOUT_MASK;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700577 reg |= DIV_ROUND_UP(usecs * 1000, 8192);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700578 bcmgenet_rdma_writel(priv, reg, DMA_RING0_TIMEOUT + i);
579}
580
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700581static void bcmgenet_set_ring_rx_coalesce(struct bcmgenet_rx_ring *ring,
582 struct ethtool_coalesce *ec)
583{
Tal Gilboa8960b382019-01-31 16:44:48 +0200584 struct dim_cq_moder moder;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700585 u32 usecs, pkts;
586
587 ring->rx_coalesce_usecs = ec->rx_coalesce_usecs;
588 ring->rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
589 usecs = ring->rx_coalesce_usecs;
590 pkts = ring->rx_max_coalesced_frames;
591
592 if (ec->use_adaptive_rx_coalesce && !ring->dim.use_dim) {
Tal Gilboa026a8072018-04-24 13:36:01 +0300593 moder = net_dim_get_def_rx_moderation(ring->dim.dim.mode);
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700594 usecs = moder.usec;
595 pkts = moder.pkts;
596 }
597
598 ring->dim.use_dim = ec->use_adaptive_rx_coalesce;
599 bcmgenet_set_rx_coalesce(ring, usecs, pkts);
600}
601
Florian Fainelli2f913072015-09-16 16:47:39 -0700602static int bcmgenet_set_coalesce(struct net_device *dev,
603 struct ethtool_coalesce *ec)
604{
605 struct bcmgenet_priv *priv = netdev_priv(dev);
606 unsigned int i;
607
Florian Fainelli4a296452015-09-16 16:47:40 -0700608 /* Base system clock is 125Mhz, DMA timeout is this reference clock
609 * divided by 1024, which yields roughly 8.192us, our maximum value
610 * has to fit in the DMA_TIMEOUT_MASK (16 bits)
611 */
Florian Fainelli2f913072015-09-16 16:47:39 -0700612 if (ec->tx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
Florian Fainelli4a296452015-09-16 16:47:40 -0700613 ec->tx_max_coalesced_frames == 0 ||
614 ec->rx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
615 ec->rx_coalesce_usecs > (DMA_TIMEOUT_MASK * 8) + 1)
616 return -EINVAL;
617
618 if (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0)
Florian Fainelli2f913072015-09-16 16:47:39 -0700619 return -EINVAL;
620
621 /* GENET TDMA hardware does not support a configurable timeout, but will
622 * always generate an interrupt either after MBDONE packets have been
Doug Berger556c2cf2017-03-13 17:41:34 -0700623 * transmitted, or when the ring is empty.
Florian Fainelli2f913072015-09-16 16:47:39 -0700624 */
Florian Fainelli2f913072015-09-16 16:47:39 -0700625
626 /* Program all TX queues with the same values, as there is no
627 * ethtool knob to do coalescing on a per-queue basis
628 */
629 for (i = 0; i < priv->hw_params->tx_queues; i++)
630 bcmgenet_tdma_ring_writel(priv, i,
631 ec->tx_max_coalesced_frames,
632 DMA_MBUF_DONE_THRESH);
633 bcmgenet_tdma_ring_writel(priv, DESC_INDEX,
634 ec->tx_max_coalesced_frames,
635 DMA_MBUF_DONE_THRESH);
636
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700637 for (i = 0; i < priv->hw_params->rx_queues; i++)
638 bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[i], ec);
639 bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[DESC_INDEX], ec);
Florian Fainelli4a296452015-09-16 16:47:40 -0700640
Florian Fainelli2f913072015-09-16 16:47:39 -0700641 return 0;
642}
643
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800644/* standard ethtool support functions. */
645enum bcmgenet_stat_type {
646 BCMGENET_STAT_NETDEV = -1,
647 BCMGENET_STAT_MIB_RX,
648 BCMGENET_STAT_MIB_TX,
649 BCMGENET_STAT_RUNT,
650 BCMGENET_STAT_MISC,
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800651 BCMGENET_STAT_SOFT,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800652};
653
654struct bcmgenet_stats {
655 char stat_string[ETH_GSTRING_LEN];
656 int stat_sizeof;
657 int stat_offset;
658 enum bcmgenet_stat_type type;
659 /* reg offset from UMAC base for misc counters */
660 u16 reg_offset;
661};
662
663#define STAT_NETDEV(m) { \
664 .stat_string = __stringify(m), \
665 .stat_sizeof = sizeof(((struct net_device_stats *)0)->m), \
666 .stat_offset = offsetof(struct net_device_stats, m), \
667 .type = BCMGENET_STAT_NETDEV, \
668}
669
670#define STAT_GENET_MIB(str, m, _type) { \
671 .stat_string = str, \
672 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
673 .stat_offset = offsetof(struct bcmgenet_priv, m), \
674 .type = _type, \
675}
676
677#define STAT_GENET_MIB_RX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_RX)
678#define STAT_GENET_MIB_TX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_TX)
679#define STAT_GENET_RUNT(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_RUNT)
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800680#define STAT_GENET_SOFT_MIB(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_SOFT)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800681
682#define STAT_GENET_MISC(str, m, offset) { \
683 .stat_string = str, \
684 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
685 .stat_offset = offsetof(struct bcmgenet_priv, m), \
686 .type = BCMGENET_STAT_MISC, \
687 .reg_offset = offset, \
688}
689
Florian Fainelli37a30b42017-03-16 10:27:08 -0700690#define STAT_GENET_Q(num) \
691 STAT_GENET_SOFT_MIB("txq" __stringify(num) "_packets", \
692 tx_rings[num].packets), \
693 STAT_GENET_SOFT_MIB("txq" __stringify(num) "_bytes", \
694 tx_rings[num].bytes), \
695 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_bytes", \
696 rx_rings[num].bytes), \
697 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_packets", \
698 rx_rings[num].packets), \
699 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_errors", \
700 rx_rings[num].errors), \
701 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_dropped", \
702 rx_rings[num].dropped)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800703
704/* There is a 0xC gap between the end of RX and beginning of TX stats and then
705 * between the end of TX stats and the beginning of the RX RUNT
706 */
707#define BCMGENET_STAT_OFFSET 0xc
708
709/* Hardware counters must be kept in sync because the order/offset
710 * is important here (order in structure declaration = order in hardware)
711 */
712static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
713 /* general stats */
714 STAT_NETDEV(rx_packets),
715 STAT_NETDEV(tx_packets),
716 STAT_NETDEV(rx_bytes),
717 STAT_NETDEV(tx_bytes),
718 STAT_NETDEV(rx_errors),
719 STAT_NETDEV(tx_errors),
720 STAT_NETDEV(rx_dropped),
721 STAT_NETDEV(tx_dropped),
722 STAT_NETDEV(multicast),
723 /* UniMAC RSV counters */
724 STAT_GENET_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
725 STAT_GENET_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
726 STAT_GENET_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
727 STAT_GENET_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
728 STAT_GENET_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
729 STAT_GENET_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
730 STAT_GENET_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
731 STAT_GENET_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
732 STAT_GENET_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
733 STAT_GENET_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
734 STAT_GENET_MIB_RX("rx_pkts", mib.rx.pkt),
735 STAT_GENET_MIB_RX("rx_bytes", mib.rx.bytes),
736 STAT_GENET_MIB_RX("rx_multicast", mib.rx.mca),
737 STAT_GENET_MIB_RX("rx_broadcast", mib.rx.bca),
738 STAT_GENET_MIB_RX("rx_fcs", mib.rx.fcs),
739 STAT_GENET_MIB_RX("rx_control", mib.rx.cf),
740 STAT_GENET_MIB_RX("rx_pause", mib.rx.pf),
741 STAT_GENET_MIB_RX("rx_unknown", mib.rx.uo),
742 STAT_GENET_MIB_RX("rx_align", mib.rx.aln),
743 STAT_GENET_MIB_RX("rx_outrange", mib.rx.flr),
744 STAT_GENET_MIB_RX("rx_code", mib.rx.cde),
745 STAT_GENET_MIB_RX("rx_carrier", mib.rx.fcr),
746 STAT_GENET_MIB_RX("rx_oversize", mib.rx.ovr),
747 STAT_GENET_MIB_RX("rx_jabber", mib.rx.jbr),
748 STAT_GENET_MIB_RX("rx_mtu_err", mib.rx.mtue),
749 STAT_GENET_MIB_RX("rx_good_pkts", mib.rx.pok),
750 STAT_GENET_MIB_RX("rx_unicast", mib.rx.uc),
751 STAT_GENET_MIB_RX("rx_ppp", mib.rx.ppp),
752 STAT_GENET_MIB_RX("rx_crc", mib.rx.rcrc),
753 /* UniMAC TSV counters */
754 STAT_GENET_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
755 STAT_GENET_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
756 STAT_GENET_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
757 STAT_GENET_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
758 STAT_GENET_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
759 STAT_GENET_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
760 STAT_GENET_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
761 STAT_GENET_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
762 STAT_GENET_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
763 STAT_GENET_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
764 STAT_GENET_MIB_TX("tx_pkts", mib.tx.pkts),
765 STAT_GENET_MIB_TX("tx_multicast", mib.tx.mca),
766 STAT_GENET_MIB_TX("tx_broadcast", mib.tx.bca),
767 STAT_GENET_MIB_TX("tx_pause", mib.tx.pf),
768 STAT_GENET_MIB_TX("tx_control", mib.tx.cf),
769 STAT_GENET_MIB_TX("tx_fcs_err", mib.tx.fcs),
770 STAT_GENET_MIB_TX("tx_oversize", mib.tx.ovr),
771 STAT_GENET_MIB_TX("tx_defer", mib.tx.drf),
772 STAT_GENET_MIB_TX("tx_excess_defer", mib.tx.edf),
773 STAT_GENET_MIB_TX("tx_single_col", mib.tx.scl),
774 STAT_GENET_MIB_TX("tx_multi_col", mib.tx.mcl),
775 STAT_GENET_MIB_TX("tx_late_col", mib.tx.lcl),
776 STAT_GENET_MIB_TX("tx_excess_col", mib.tx.ecl),
777 STAT_GENET_MIB_TX("tx_frags", mib.tx.frg),
778 STAT_GENET_MIB_TX("tx_total_col", mib.tx.ncl),
779 STAT_GENET_MIB_TX("tx_jabber", mib.tx.jbr),
780 STAT_GENET_MIB_TX("tx_bytes", mib.tx.bytes),
781 STAT_GENET_MIB_TX("tx_good_pkts", mib.tx.pok),
782 STAT_GENET_MIB_TX("tx_unicast", mib.tx.uc),
783 /* UniMAC RUNT counters */
784 STAT_GENET_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
785 STAT_GENET_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
786 STAT_GENET_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
787 STAT_GENET_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
788 /* Misc UniMAC counters */
789 STAT_GENET_MISC("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt,
Doug Bergerffff7132017-03-09 16:58:43 -0800790 UMAC_RBUF_OVFL_CNT_V1),
791 STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt,
792 UMAC_RBUF_ERR_CNT_V1),
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800793 STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT),
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800794 STAT_GENET_SOFT_MIB("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
795 STAT_GENET_SOFT_MIB("rx_dma_failed", mib.rx_dma_failed),
796 STAT_GENET_SOFT_MIB("tx_dma_failed", mib.tx_dma_failed),
Doug Bergerf1af17c2019-12-17 16:51:15 -0800797 STAT_GENET_SOFT_MIB("tx_realloc_tsb", mib.tx_realloc_tsb),
798 STAT_GENET_SOFT_MIB("tx_realloc_tsb_failed",
799 mib.tx_realloc_tsb_failed),
Florian Fainelli37a30b42017-03-16 10:27:08 -0700800 /* Per TX queues */
801 STAT_GENET_Q(0),
802 STAT_GENET_Q(1),
803 STAT_GENET_Q(2),
804 STAT_GENET_Q(3),
805 STAT_GENET_Q(16),
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800806};
807
808#define BCMGENET_STATS_LEN ARRAY_SIZE(bcmgenet_gstrings_stats)
809
810static void bcmgenet_get_drvinfo(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700811 struct ethtool_drvinfo *info)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800812{
813 strlcpy(info->driver, "bcmgenet", sizeof(info->driver));
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800814}
815
816static int bcmgenet_get_sset_count(struct net_device *dev, int string_set)
817{
818 switch (string_set) {
819 case ETH_SS_STATS:
820 return BCMGENET_STATS_LEN;
821 default:
822 return -EOPNOTSUPP;
823 }
824}
825
Florian Fainellic91b7f62014-07-23 10:42:12 -0700826static void bcmgenet_get_strings(struct net_device *dev, u32 stringset,
827 u8 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800828{
829 int i;
830
831 switch (stringset) {
832 case ETH_SS_STATS:
833 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
834 memcpy(data + i * ETH_GSTRING_LEN,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700835 bcmgenet_gstrings_stats[i].stat_string,
836 ETH_GSTRING_LEN);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800837 }
838 break;
839 }
840}
841
Doug Bergerffff7132017-03-09 16:58:43 -0800842static u32 bcmgenet_update_stat_misc(struct bcmgenet_priv *priv, u16 offset)
843{
844 u16 new_offset;
845 u32 val;
846
847 switch (offset) {
848 case UMAC_RBUF_OVFL_CNT_V1:
849 if (GENET_IS_V2(priv))
850 new_offset = RBUF_OVFL_CNT_V2;
851 else
852 new_offset = RBUF_OVFL_CNT_V3PLUS;
853
854 val = bcmgenet_rbuf_readl(priv, new_offset);
855 /* clear if overflowed */
856 if (val == ~0)
857 bcmgenet_rbuf_writel(priv, 0, new_offset);
858 break;
859 case UMAC_RBUF_ERR_CNT_V1:
860 if (GENET_IS_V2(priv))
861 new_offset = RBUF_ERR_CNT_V2;
862 else
863 new_offset = RBUF_ERR_CNT_V3PLUS;
864
865 val = bcmgenet_rbuf_readl(priv, new_offset);
866 /* clear if overflowed */
867 if (val == ~0)
868 bcmgenet_rbuf_writel(priv, 0, new_offset);
869 break;
870 default:
871 val = bcmgenet_umac_readl(priv, offset);
872 /* clear if overflowed */
873 if (val == ~0)
874 bcmgenet_umac_writel(priv, 0, offset);
875 break;
876 }
877
878 return val;
879}
880
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800881static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv)
882{
883 int i, j = 0;
884
885 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
886 const struct bcmgenet_stats *s;
887 u8 offset = 0;
888 u32 val = 0;
889 char *p;
890
891 s = &bcmgenet_gstrings_stats[i];
892 switch (s->type) {
893 case BCMGENET_STAT_NETDEV:
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800894 case BCMGENET_STAT_SOFT:
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800895 continue;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800896 case BCMGENET_STAT_RUNT:
Doug Berger1ad3d222017-03-09 16:58:44 -0800897 offset += BCMGENET_STAT_OFFSET;
898 /* fall through */
899 case BCMGENET_STAT_MIB_TX:
900 offset += BCMGENET_STAT_OFFSET;
901 /* fall through */
902 case BCMGENET_STAT_MIB_RX:
Florian Fainellic91b7f62014-07-23 10:42:12 -0700903 val = bcmgenet_umac_readl(priv,
904 UMAC_MIB_START + j + offset);
Doug Berger1ad3d222017-03-09 16:58:44 -0800905 offset = 0; /* Reset Offset */
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800906 break;
907 case BCMGENET_STAT_MISC:
Doug Bergerffff7132017-03-09 16:58:43 -0800908 if (GENET_IS_V1(priv)) {
909 val = bcmgenet_umac_readl(priv, s->reg_offset);
910 /* clear if overflowed */
911 if (val == ~0)
912 bcmgenet_umac_writel(priv, 0,
913 s->reg_offset);
914 } else {
915 val = bcmgenet_update_stat_misc(priv,
916 s->reg_offset);
917 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800918 break;
919 }
920
921 j += s->stat_sizeof;
922 p = (char *)priv + s->stat_offset;
923 *(u32 *)p = val;
924 }
925}
926
927static void bcmgenet_get_ethtool_stats(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700928 struct ethtool_stats *stats,
929 u64 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800930{
931 struct bcmgenet_priv *priv = netdev_priv(dev);
932 int i;
933
934 if (netif_running(dev))
935 bcmgenet_update_mib_counters(priv);
936
Doug Bergera6d0b832020-04-23 15:44:17 -0700937 dev->netdev_ops->ndo_get_stats(dev);
938
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800939 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
940 const struct bcmgenet_stats *s;
941 char *p;
942
943 s = &bcmgenet_gstrings_stats[i];
944 if (s->type == BCMGENET_STAT_NETDEV)
945 p = (char *)&dev->stats;
946 else
947 p = (char *)priv;
948 p += s->stat_offset;
Eric Dumazet6517eb52016-04-15 10:47:52 -0700949 if (sizeof(unsigned long) != sizeof(u32) &&
950 s->stat_sizeof == sizeof(unsigned long))
951 data[i] = *(unsigned long *)p;
952 else
953 data[i] = *(u32 *)p;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800954 }
955}
956
Florian Fainelli6ef398e2014-11-25 21:16:35 -0800957static void bcmgenet_eee_enable_set(struct net_device *dev, bool enable)
958{
959 struct bcmgenet_priv *priv = netdev_priv(dev);
960 u32 off = priv->hw_params->tbuf_offset + TBUF_ENERGY_CTRL;
961 u32 reg;
962
963 if (enable && !priv->clk_eee_enabled) {
964 clk_prepare_enable(priv->clk_eee);
965 priv->clk_eee_enabled = true;
966 }
967
968 reg = bcmgenet_umac_readl(priv, UMAC_EEE_CTRL);
969 if (enable)
970 reg |= EEE_EN;
971 else
972 reg &= ~EEE_EN;
973 bcmgenet_umac_writel(priv, reg, UMAC_EEE_CTRL);
974
975 /* Enable EEE and switch to a 27Mhz clock automatically */
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700976 reg = bcmgenet_readl(priv->base + off);
Florian Fainelli6ef398e2014-11-25 21:16:35 -0800977 if (enable)
978 reg |= TBUF_EEE_EN | TBUF_PM_EN;
979 else
980 reg &= ~(TBUF_EEE_EN | TBUF_PM_EN);
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700981 bcmgenet_writel(reg, priv->base + off);
Florian Fainelli6ef398e2014-11-25 21:16:35 -0800982
983 /* Do the same for thing for RBUF */
984 reg = bcmgenet_rbuf_readl(priv, RBUF_ENERGY_CTRL);
985 if (enable)
986 reg |= RBUF_EEE_EN | RBUF_PM_EN;
987 else
988 reg &= ~(RBUF_EEE_EN | RBUF_PM_EN);
989 bcmgenet_rbuf_writel(priv, reg, RBUF_ENERGY_CTRL);
990
991 if (!enable && priv->clk_eee_enabled) {
992 clk_disable_unprepare(priv->clk_eee);
993 priv->clk_eee_enabled = false;
994 }
995
996 priv->eee.eee_enabled = enable;
997 priv->eee.eee_active = enable;
998}
999
1000static int bcmgenet_get_eee(struct net_device *dev, struct ethtool_eee *e)
1001{
1002 struct bcmgenet_priv *priv = netdev_priv(dev);
1003 struct ethtool_eee *p = &priv->eee;
1004
1005 if (GENET_IS_V1(priv))
1006 return -EOPNOTSUPP;
1007
Doug Berger6c97f012017-10-25 15:04:19 -07001008 if (!dev->phydev)
1009 return -ENODEV;
1010
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001011 e->eee_enabled = p->eee_enabled;
1012 e->eee_active = p->eee_active;
1013 e->tx_lpi_timer = bcmgenet_umac_readl(priv, UMAC_EEE_LPI_TIMER);
1014
Doug Berger6c97f012017-10-25 15:04:19 -07001015 return phy_ethtool_get_eee(dev->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001016}
1017
1018static int bcmgenet_set_eee(struct net_device *dev, struct ethtool_eee *e)
1019{
1020 struct bcmgenet_priv *priv = netdev_priv(dev);
1021 struct ethtool_eee *p = &priv->eee;
1022 int ret = 0;
1023
1024 if (GENET_IS_V1(priv))
1025 return -EOPNOTSUPP;
1026
Doug Berger6c97f012017-10-25 15:04:19 -07001027 if (!dev->phydev)
1028 return -ENODEV;
1029
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001030 p->eee_enabled = e->eee_enabled;
1031
1032 if (!p->eee_enabled) {
1033 bcmgenet_eee_enable_set(dev, false);
1034 } else {
Doug Berger6c97f012017-10-25 15:04:19 -07001035 ret = phy_init_eee(dev->phydev, 0);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001036 if (ret) {
1037 netif_err(priv, hw, dev, "EEE initialization failed\n");
1038 return ret;
1039 }
1040
1041 bcmgenet_umac_writel(priv, e->tx_lpi_timer, UMAC_EEE_LPI_TIMER);
1042 bcmgenet_eee_enable_set(dev, true);
1043 }
1044
Doug Berger6c97f012017-10-25 15:04:19 -07001045 return phy_ethtool_set_eee(dev->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001046}
1047
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001048/* standard ethtool support functions. */
Julia Lawall70591ab2016-08-31 09:30:45 +02001049static const struct ethtool_ops bcmgenet_ethtool_ops = {
Jakub Kicinskif6f508c2020-03-09 19:15:03 -07001050 .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
1051 ETHTOOL_COALESCE_MAX_FRAMES |
1052 ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
Edwin Chan89316fa2017-03-09 16:58:49 -08001053 .begin = bcmgenet_begin,
1054 .complete = bcmgenet_complete,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001055 .get_strings = bcmgenet_get_strings,
1056 .get_sset_count = bcmgenet_get_sset_count,
1057 .get_ethtool_stats = bcmgenet_get_ethtool_stats,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001058 .get_drvinfo = bcmgenet_get_drvinfo,
1059 .get_link = ethtool_op_get_link,
1060 .get_msglevel = bcmgenet_get_msglevel,
1061 .set_msglevel = bcmgenet_set_msglevel,
Florian Fainelli06ba8372014-07-21 15:29:29 -07001062 .get_wol = bcmgenet_get_wol,
1063 .set_wol = bcmgenet_set_wol,
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001064 .get_eee = bcmgenet_get_eee,
1065 .set_eee = bcmgenet_set_eee,
Florian Fainelli016e7702016-11-15 10:06:38 -08001066 .nway_reset = phy_ethtool_nway_reset,
Florian Fainelli2f913072015-09-16 16:47:39 -07001067 .get_coalesce = bcmgenet_get_coalesce,
1068 .set_coalesce = bcmgenet_set_coalesce,
Philippe Reynesfa92bf02016-09-26 22:31:57 +02001069 .get_link_ksettings = bcmgenet_get_link_ksettings,
1070 .set_link_ksettings = bcmgenet_set_link_ksettings,
Ryan M. Collinsdd1bf472019-08-30 14:49:55 -04001071 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001072};
1073
1074/* Power down the unimac, based on mode. */
Florian Fainellica8cf342015-03-23 15:09:51 -07001075static int bcmgenet_power_down(struct bcmgenet_priv *priv,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001076 enum bcmgenet_power_mode mode)
1077{
Florian Fainellica8cf342015-03-23 15:09:51 -07001078 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001079 u32 reg;
1080
1081 switch (mode) {
1082 case GENET_POWER_CABLE_SENSE:
Doug Berger6c97f012017-10-25 15:04:19 -07001083 phy_detach(priv->dev->phydev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001084 break;
1085
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001086 case GENET_POWER_WOL_MAGIC:
Florian Fainellica8cf342015-03-23 15:09:51 -07001087 ret = bcmgenet_wol_power_down_cfg(priv, mode);
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001088 break;
1089
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001090 case GENET_POWER_PASSIVE:
1091 /* Power down LED */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001092 if (priv->hw_params->flags & GENET_HAS_EXT) {
1093 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
Doug Berger42138082017-03-13 17:41:42 -07001094 if (GENET_IS_V5(priv))
1095 reg |= EXT_PWR_DOWN_PHY_EN |
1096 EXT_PWR_DOWN_PHY_RD |
1097 EXT_PWR_DOWN_PHY_SD |
1098 EXT_PWR_DOWN_PHY_RX |
1099 EXT_PWR_DOWN_PHY_TX |
1100 EXT_IDDQ_GLBL_PWR;
1101 else
1102 reg |= EXT_PWR_DOWN_PHY;
1103
1104 reg |= (EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001105 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
Florian Fainellia642c4f2015-03-23 15:09:56 -07001106
1107 bcmgenet_phy_power_set(priv->dev, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001108 }
1109 break;
1110 default:
1111 break;
1112 }
Florian Fainellica8cf342015-03-23 15:09:51 -07001113
YueHaibing0db55092018-11-08 02:08:43 +00001114 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001115}
1116
1117static void bcmgenet_power_up(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001118 enum bcmgenet_power_mode mode)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001119{
1120 u32 reg;
1121
1122 if (!(priv->hw_params->flags & GENET_HAS_EXT))
1123 return;
1124
1125 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
1126
1127 switch (mode) {
1128 case GENET_POWER_PASSIVE:
Doug Berger42138082017-03-13 17:41:42 -07001129 reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
1130 if (GENET_IS_V5(priv)) {
1131 reg &= ~(EXT_PWR_DOWN_PHY_EN |
1132 EXT_PWR_DOWN_PHY_RD |
1133 EXT_PWR_DOWN_PHY_SD |
1134 EXT_PWR_DOWN_PHY_RX |
1135 EXT_PWR_DOWN_PHY_TX |
1136 EXT_IDDQ_GLBL_PWR);
1137 reg |= EXT_PHY_RESET;
1138 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1139 mdelay(1);
1140
1141 reg &= ~EXT_PHY_RESET;
1142 } else {
1143 reg &= ~EXT_PWR_DOWN_PHY;
1144 reg |= EXT_PWR_DN_EN_LD;
1145 }
1146 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1147 bcmgenet_phy_power_set(priv->dev, true);
Doug Berger42138082017-03-13 17:41:42 -07001148 break;
1149
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001150 case GENET_POWER_CABLE_SENSE:
1151 /* enable APD */
Doug Berger42138082017-03-13 17:41:42 -07001152 if (!GENET_IS_V5(priv)) {
1153 reg |= EXT_PWR_DN_EN_LD;
1154 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1155 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001156 break;
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001157 case GENET_POWER_WOL_MAGIC:
1158 bcmgenet_wol_power_up_cfg(priv, mode);
1159 return;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001160 default:
1161 break;
1162 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001163}
1164
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001165static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv,
1166 struct bcmgenet_tx_ring *ring)
1167{
1168 struct enet_cb *tx_cb_ptr;
1169
1170 tx_cb_ptr = ring->cbs;
1171 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
Petri Gynther014012a2015-02-23 11:00:45 -08001172
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001173 /* Advancing local write pointer */
1174 if (ring->write_ptr == ring->end_ptr)
1175 ring->write_ptr = ring->cb_ptr;
1176 else
1177 ring->write_ptr++;
1178
1179 return tx_cb_ptr;
1180}
1181
Doug Berger876dbad2017-07-14 16:12:09 -07001182static struct enet_cb *bcmgenet_put_txcb(struct bcmgenet_priv *priv,
1183 struct bcmgenet_tx_ring *ring)
1184{
1185 struct enet_cb *tx_cb_ptr;
1186
1187 tx_cb_ptr = ring->cbs;
1188 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
1189
1190 /* Rewinding local write pointer */
1191 if (ring->write_ptr == ring->cb_ptr)
1192 ring->write_ptr = ring->end_ptr;
1193 else
1194 ring->write_ptr--;
1195
1196 return tx_cb_ptr;
1197}
1198
Petri Gynther4055eae2015-03-25 12:35:16 -07001199static inline void bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring *ring)
1200{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001201 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001202 INTRL2_CPU_MASK_SET);
1203}
1204
1205static inline void bcmgenet_rx_ring16_int_enable(struct bcmgenet_rx_ring *ring)
1206{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001207 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001208 INTRL2_CPU_MASK_CLEAR);
1209}
1210
1211static inline void bcmgenet_rx_ring_int_disable(struct bcmgenet_rx_ring *ring)
1212{
1213 bcmgenet_intrl2_1_writel(ring->priv,
1214 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1215 INTRL2_CPU_MASK_SET);
1216}
1217
1218static inline void bcmgenet_rx_ring_int_enable(struct bcmgenet_rx_ring *ring)
1219{
1220 bcmgenet_intrl2_1_writel(ring->priv,
1221 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1222 INTRL2_CPU_MASK_CLEAR);
1223}
1224
Petri Gynther9dbac282015-03-25 12:35:10 -07001225static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001226{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001227 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001228 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001229}
1230
Petri Gynther9dbac282015-03-25 12:35:10 -07001231static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001232{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001233 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001234 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001235}
1236
Petri Gynther9dbac282015-03-25 12:35:10 -07001237static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001238{
Petri Gynther9dbac282015-03-25 12:35:10 -07001239 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001240 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001241}
1242
Petri Gynther9dbac282015-03-25 12:35:10 -07001243static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001244{
Petri Gynther9dbac282015-03-25 12:35:10 -07001245 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001246 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001247}
1248
Doug Bergerf48bed12017-07-14 16:12:10 -07001249/* Simple helper to free a transmit control block's resources
1250 * Returns an skb when the last transmit control block associated with the
1251 * skb is freed. The skb should be freed by the caller if necessary.
1252 */
1253static struct sk_buff *bcmgenet_free_tx_cb(struct device *dev,
1254 struct enet_cb *cb)
1255{
1256 struct sk_buff *skb;
1257
1258 skb = cb->skb;
1259
1260 if (skb) {
1261 cb->skb = NULL;
1262 if (cb == GENET_CB(skb)->first_cb)
1263 dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
1264 dma_unmap_len(cb, dma_len),
1265 DMA_TO_DEVICE);
1266 else
1267 dma_unmap_page(dev, dma_unmap_addr(cb, dma_addr),
1268 dma_unmap_len(cb, dma_len),
1269 DMA_TO_DEVICE);
1270 dma_unmap_addr_set(cb, dma_addr, 0);
1271
1272 if (cb == GENET_CB(skb)->last_cb)
1273 return skb;
1274
1275 } else if (dma_unmap_addr(cb, dma_addr)) {
1276 dma_unmap_page(dev,
1277 dma_unmap_addr(cb, dma_addr),
1278 dma_unmap_len(cb, dma_len),
1279 DMA_TO_DEVICE);
1280 dma_unmap_addr_set(cb, dma_addr, 0);
1281 }
1282
Wei Yongjun335ab8b2018-03-28 12:51:19 +00001283 return NULL;
Doug Bergerf48bed12017-07-14 16:12:10 -07001284}
1285
1286/* Simple helper to free a receive control block's resources */
1287static struct sk_buff *bcmgenet_free_rx_cb(struct device *dev,
1288 struct enet_cb *cb)
1289{
1290 struct sk_buff *skb;
1291
1292 skb = cb->skb;
1293 cb->skb = NULL;
1294
1295 if (dma_unmap_addr(cb, dma_addr)) {
1296 dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
1297 dma_unmap_len(cb, dma_len), DMA_FROM_DEVICE);
1298 dma_unmap_addr_set(cb, dma_addr, 0);
1299 }
1300
1301 return skb;
1302}
1303
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001304/* Unlocked version of the reclaim routine */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001305static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
1306 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001307{
1308 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther66d06752015-03-04 14:30:01 -08001309 unsigned int txbds_processed = 0;
Doug Bergerf48bed12017-07-14 16:12:10 -07001310 unsigned int bytes_compl = 0;
1311 unsigned int pkts_compl = 0;
1312 unsigned int txbds_ready;
1313 unsigned int c_index;
1314 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001315
Doug Bergerd5810ca2017-03-13 17:41:37 -07001316 /* Clear status before servicing to reduce spurious interrupts */
1317 if (ring->index == DESC_INDEX)
1318 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_TXDMA_DONE,
1319 INTRL2_CPU_CLEAR);
1320 else
1321 bcmgenet_intrl2_1_writel(priv, (1 << ring->index),
1322 INTRL2_CPU_CLEAR);
1323
Brian Norris7fc527f2014-07-29 14:34:14 -07001324 /* Compute how many buffers are transmitted since last xmit call */
Doug Bergerc298ede2017-03-13 17:41:33 -07001325 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX)
1326 & DMA_C_INDEX_MASK;
1327 txbds_ready = (c_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001328
1329 netif_dbg(priv, tx_done, dev,
Petri Gynther66d06752015-03-04 14:30:01 -08001330 "%s ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
1331 __func__, ring->index, ring->c_index, c_index, txbds_ready);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001332
1333 /* Reclaim transmitted buffers */
Petri Gynther66d06752015-03-04 14:30:01 -08001334 while (txbds_processed < txbds_ready) {
Doug Bergerf48bed12017-07-14 16:12:10 -07001335 skb = bcmgenet_free_tx_cb(&priv->pdev->dev,
1336 &priv->tx_cbs[ring->clean_ptr]);
1337 if (skb) {
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001338 pkts_compl++;
Doug Bergerf48bed12017-07-14 16:12:10 -07001339 bytes_compl += GENET_CB(skb)->bytes_sent;
Florian Fainellid4fec852017-08-24 15:56:29 -07001340 dev_consume_skb_any(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001341 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001342
Petri Gynther66d06752015-03-04 14:30:01 -08001343 txbds_processed++;
1344 if (likely(ring->clean_ptr < ring->end_ptr))
1345 ring->clean_ptr++;
1346 else
1347 ring->clean_ptr = ring->cb_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001348 }
1349
Petri Gynther66d06752015-03-04 14:30:01 -08001350 ring->free_bds += txbds_processed;
Doug Bergerc4d453d2017-03-13 17:41:38 -07001351 ring->c_index = c_index;
Petri Gynther66d06752015-03-04 14:30:01 -08001352
Florian Fainelli37a30b42017-03-16 10:27:08 -07001353 ring->packets += pkts_compl;
1354 ring->bytes += bytes_compl;
Petri Gynther55868122016-03-24 11:27:20 -07001355
Doug Berger6d22fe12017-03-09 16:58:50 -08001356 netdev_tx_completed_queue(netdev_get_tx_queue(dev, ring->queue),
1357 pkts_compl, bytes_compl);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001358
Doug Bergerc4d453d2017-03-13 17:41:38 -07001359 return txbds_processed;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001360}
1361
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001362static unsigned int bcmgenet_tx_reclaim(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001363 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001364{
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001365 unsigned int released;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001366
Doug Bergerb0447ec2017-10-25 15:04:17 -07001367 spin_lock_bh(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001368 released = __bcmgenet_tx_reclaim(dev, ring);
Doug Bergerb0447ec2017-10-25 15:04:17 -07001369 spin_unlock_bh(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001370
1371 return released;
1372}
1373
1374static int bcmgenet_tx_poll(struct napi_struct *napi, int budget)
1375{
1376 struct bcmgenet_tx_ring *ring =
1377 container_of(napi, struct bcmgenet_tx_ring, napi);
1378 unsigned int work_done = 0;
Doug Berger6d22fe12017-03-09 16:58:50 -08001379 struct netdev_queue *txq;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001380
Doug Bergerb0447ec2017-10-25 15:04:17 -07001381 spin_lock(&ring->lock);
Doug Berger6d22fe12017-03-09 16:58:50 -08001382 work_done = __bcmgenet_tx_reclaim(ring->priv->dev, ring);
1383 if (ring->free_bds > (MAX_SKB_FRAGS + 1)) {
1384 txq = netdev_get_tx_queue(ring->priv->dev, ring->queue);
1385 netif_tx_wake_queue(txq);
1386 }
Doug Bergerb0447ec2017-10-25 15:04:17 -07001387 spin_unlock(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001388
1389 if (work_done == 0) {
1390 napi_complete(napi);
Petri Gynther9dbac282015-03-25 12:35:10 -07001391 ring->int_enable(ring);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001392
1393 return 0;
1394 }
1395
1396 return budget;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001397}
1398
1399static void bcmgenet_tx_reclaim_all(struct net_device *dev)
1400{
1401 struct bcmgenet_priv *priv = netdev_priv(dev);
1402 int i;
1403
1404 if (netif_is_multiqueue(dev)) {
1405 for (i = 0; i < priv->hw_params->tx_queues; i++)
1406 bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
1407 }
1408
1409 bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
1410}
1411
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001412/* Reallocate the SKB to put enough headroom in front of it and insert
1413 * the transmit checksum offsets in the descriptors
1414 */
Doug Berger9a9ba2a2020-03-17 17:05:36 -07001415static struct sk_buff *bcmgenet_add_tsb(struct net_device *dev,
1416 struct sk_buff *skb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001417{
Doug Bergerf1af17c2019-12-17 16:51:15 -08001418 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001419 struct status_64 *status = NULL;
1420 struct sk_buff *new_skb;
1421 u16 offset;
1422 u8 ip_proto;
Florian Fainelli6f894212018-04-02 15:58:55 -07001423 __be16 ip_ver;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001424 u32 tx_csum_info;
1425
1426 if (unlikely(skb_headroom(skb) < sizeof(*status))) {
1427 /* If 64 byte status block enabled, must make sure skb has
1428 * enough headroom for us to insert 64B status block.
1429 */
1430 new_skb = skb_realloc_headroom(skb, sizeof(*status));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001431 if (!new_skb) {
Doug Bergere3fa8582019-12-17 16:51:14 -08001432 dev_kfree_skb_any(skb);
Doug Bergerf1af17c2019-12-17 16:51:15 -08001433 priv->mib.tx_realloc_tsb_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001434 dev->stats.tx_dropped++;
Petri Gyntherbc233332014-10-01 11:30:01 -07001435 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001436 }
Doug Bergere3fa8582019-12-17 16:51:14 -08001437 dev_consume_skb_any(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001438 skb = new_skb;
Doug Bergerf1af17c2019-12-17 16:51:15 -08001439 priv->mib.tx_realloc_tsb++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001440 }
1441
1442 skb_push(skb, sizeof(*status));
1443 status = (struct status_64 *)skb->data;
1444
1445 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Florian Fainelli6f894212018-04-02 15:58:55 -07001446 ip_ver = skb->protocol;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001447 switch (ip_ver) {
Florian Fainelli6f894212018-04-02 15:58:55 -07001448 case htons(ETH_P_IP):
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001449 ip_proto = ip_hdr(skb)->protocol;
1450 break;
Florian Fainelli6f894212018-04-02 15:58:55 -07001451 case htons(ETH_P_IPV6):
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001452 ip_proto = ipv6_hdr(skb)->nexthdr;
1453 break;
1454 default:
Doug Bergerdd8e9112019-12-17 16:51:09 -08001455 /* don't use UDP flag */
1456 ip_proto = 0;
1457 break;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001458 }
1459
1460 offset = skb_checksum_start_offset(skb) - sizeof(*status);
1461 tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
Doug Bergerdd8e9112019-12-17 16:51:09 -08001462 (offset + skb->csum_offset) |
1463 STATUS_TX_CSUM_LV;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001464
Doug Bergerdd8e9112019-12-17 16:51:09 -08001465 /* Set the special UDP flag for UDP */
1466 if (ip_proto == IPPROTO_UDP)
1467 tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001468
1469 status->tx_csum_info = tx_csum_info;
1470 }
1471
Petri Gyntherbc233332014-10-01 11:30:01 -07001472 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001473}
1474
1475static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
1476{
1477 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Berger876dbad2017-07-14 16:12:09 -07001478 struct device *kdev = &priv->pdev->dev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001479 struct bcmgenet_tx_ring *ring = NULL;
Doug Berger876dbad2017-07-14 16:12:09 -07001480 struct enet_cb *tx_cb_ptr;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001481 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001482 int nr_frags, index;
Doug Berger876dbad2017-07-14 16:12:09 -07001483 dma_addr_t mapping;
1484 unsigned int size;
1485 skb_frag_t *frag;
1486 u32 len_stat;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001487 int ret;
1488 int i;
1489
1490 index = skb_get_queue_mapping(skb);
1491 /* Mapping strategy:
1492 * queue_mapping = 0, unclassified, packet xmited through ring16
1493 * queue_mapping = 1, goes to ring 0. (highest priority queue
1494 * queue_mapping = 2, goes to ring 1.
1495 * queue_mapping = 3, goes to ring 2.
1496 * queue_mapping = 4, goes to ring 3.
1497 */
1498 if (index == 0)
1499 index = DESC_INDEX;
1500 else
1501 index -= 1;
1502
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001503 ring = &priv->tx_rings[index];
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001504 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001505
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07001506 nr_frags = skb_shinfo(skb)->nr_frags;
1507
Doug Bergerb0447ec2017-10-25 15:04:17 -07001508 spin_lock(&ring->lock);
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07001509 if (ring->free_bds <= (nr_frags + 1)) {
1510 if (!netif_tx_queue_stopped(txq)) {
1511 netif_tx_stop_queue(txq);
1512 netdev_err(dev,
1513 "%s: tx ring %d full when queue %d awake\n",
1514 __func__, index, ring->queue);
1515 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001516 ret = NETDEV_TX_BUSY;
1517 goto out;
1518 }
1519
Florian Fainelli474ea9c2014-07-22 11:01:52 -07001520 if (skb_padto(skb, ETH_ZLEN)) {
1521 ret = NETDEV_TX_OK;
1522 goto out;
1523 }
1524
Petri Gynther55868122016-03-24 11:27:20 -07001525 /* Retain how many bytes will be sent on the wire, without TSB inserted
1526 * by transmit checksum offload
1527 */
1528 GENET_CB(skb)->bytes_sent = skb->len;
1529
Doug Berger9a9ba2a2020-03-17 17:05:36 -07001530 /* add the Transmit Status Block */
1531 skb = bcmgenet_add_tsb(dev, skb);
1532 if (!skb) {
1533 ret = NETDEV_TX_OK;
1534 goto out;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001535 }
1536
Doug Berger876dbad2017-07-14 16:12:09 -07001537 for (i = 0; i <= nr_frags; i++) {
1538 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001539
Gustavo A. R. Silva4fa112f2017-10-26 07:16:01 -05001540 BUG_ON(!tx_cb_ptr);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001541
Doug Berger876dbad2017-07-14 16:12:09 -07001542 if (!i) {
1543 /* Transmit single SKB or head of fragment list */
Doug Bergerf48bed12017-07-14 16:12:10 -07001544 GENET_CB(skb)->first_cb = tx_cb_ptr;
Doug Berger876dbad2017-07-14 16:12:09 -07001545 size = skb_headlen(skb);
1546 mapping = dma_map_single(kdev, skb->data, size,
1547 DMA_TO_DEVICE);
1548 } else {
1549 /* xmit fragment */
Doug Berger876dbad2017-07-14 16:12:09 -07001550 frag = &skb_shinfo(skb)->frags[i - 1];
1551 size = skb_frag_size(frag);
1552 mapping = skb_frag_dma_map(kdev, frag, 0, size,
1553 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001554 }
Doug Berger876dbad2017-07-14 16:12:09 -07001555
1556 ret = dma_mapping_error(kdev, mapping);
1557 if (ret) {
1558 priv->mib.tx_dma_failed++;
1559 netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
1560 ret = NETDEV_TX_OK;
1561 goto out_unmap_frags;
1562 }
1563 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
1564 dma_unmap_len_set(tx_cb_ptr, dma_len, size);
1565
Doug Bergerf48bed12017-07-14 16:12:10 -07001566 tx_cb_ptr->skb = skb;
1567
Doug Berger876dbad2017-07-14 16:12:09 -07001568 len_stat = (size << DMA_BUFLENGTH_SHIFT) |
1569 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT);
1570
1571 if (!i) {
1572 len_stat |= DMA_TX_APPEND_CRC | DMA_SOP;
1573 if (skb->ip_summed == CHECKSUM_PARTIAL)
1574 len_stat |= DMA_TX_DO_CSUM;
1575 }
1576 if (i == nr_frags)
1577 len_stat |= DMA_EOP;
1578
1579 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, len_stat);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001580 }
1581
Doug Bergerf48bed12017-07-14 16:12:10 -07001582 GENET_CB(skb)->last_cb = tx_cb_ptr;
Florian Fainellid03825f2014-03-20 10:53:21 -07001583 skb_tx_timestamp(skb);
1584
Florian Fainelliae67bf02015-03-13 12:11:06 -07001585 /* Decrement total BD count and advance our write pointer */
1586 ring->free_bds -= nr_frags + 1;
1587 ring->prod_index += nr_frags + 1;
1588 ring->prod_index &= DMA_P_INDEX_MASK;
1589
Petri Gynthere178c8c2016-04-09 00:20:36 -07001590 netdev_tx_sent_queue(txq, GENET_CB(skb)->bytes_sent);
1591
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001592 if (ring->free_bds <= (MAX_SKB_FRAGS + 1))
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001593 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001594
Florian Westphal6b16f9e2019-04-01 16:42:14 +02001595 if (!netdev_xmit_more() || netif_xmit_stopped(txq))
Florian Fainelliddd0ca52015-03-13 12:11:07 -07001596 /* Packets are ready, update producer index */
1597 bcmgenet_tdma_ring_writel(priv, ring->index,
1598 ring->prod_index, TDMA_PROD_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001599out:
Doug Bergerb0447ec2017-10-25 15:04:17 -07001600 spin_unlock(&ring->lock);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001601
1602 return ret;
Doug Berger876dbad2017-07-14 16:12:09 -07001603
1604out_unmap_frags:
1605 /* Back up for failed control block mapping */
1606 bcmgenet_put_txcb(priv, ring);
1607
1608 /* Unmap successfully mapped control blocks */
1609 while (i-- > 0) {
1610 tx_cb_ptr = bcmgenet_put_txcb(priv, ring);
Doug Bergerf48bed12017-07-14 16:12:10 -07001611 bcmgenet_free_tx_cb(kdev, tx_cb_ptr);
Doug Berger876dbad2017-07-14 16:12:09 -07001612 }
1613
1614 dev_kfree_skb(skb);
1615 goto out;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001616}
1617
Petri Gyntherd6707be2015-03-12 15:48:00 -07001618static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
1619 struct enet_cb *cb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001620{
1621 struct device *kdev = &priv->pdev->dev;
1622 struct sk_buff *skb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001623 struct sk_buff *rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001624 dma_addr_t mapping;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001625
Petri Gyntherd6707be2015-03-12 15:48:00 -07001626 /* Allocate a new Rx skb */
Florian Fainellic91b7f62014-07-23 10:42:12 -07001627 skb = netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT);
Petri Gyntherd6707be2015-03-12 15:48:00 -07001628 if (!skb) {
1629 priv->mib.alloc_rx_buff_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001630 netif_err(priv, rx_err, priv->dev,
Petri Gyntherd6707be2015-03-12 15:48:00 -07001631 "%s: Rx skb allocation failed\n", __func__);
1632 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001633 }
1634
Petri Gyntherd6707be2015-03-12 15:48:00 -07001635 /* DMA-map the new Rx skb */
1636 mapping = dma_map_single(kdev, skb->data, priv->rx_buf_len,
1637 DMA_FROM_DEVICE);
1638 if (dma_mapping_error(kdev, mapping)) {
1639 priv->mib.rx_dma_failed++;
1640 dev_kfree_skb_any(skb);
1641 netif_err(priv, rx_err, priv->dev,
1642 "%s: Rx skb DMA mapping failed\n", __func__);
1643 return NULL;
1644 }
1645
1646 /* Grab the current Rx skb from the ring and DMA-unmap it */
Doug Bergerf48bed12017-07-14 16:12:10 -07001647 rx_skb = bcmgenet_free_rx_cb(kdev, cb);
Petri Gyntherd6707be2015-03-12 15:48:00 -07001648
1649 /* Put the new Rx skb on the ring */
1650 cb->skb = skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001651 dma_unmap_addr_set(cb, dma_addr, mapping);
Doug Bergerf48bed12017-07-14 16:12:10 -07001652 dma_unmap_len_set(cb, dma_len, priv->rx_buf_len);
Petri Gynther8ac467e2015-03-09 13:40:00 -07001653 dmadesc_set_addr(priv, cb->bd_addr, mapping);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001654
Petri Gyntherd6707be2015-03-12 15:48:00 -07001655 /* Return the current Rx skb to caller */
1656 return rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001657}
1658
1659/* bcmgenet_desc_rx - descriptor based rx process.
1660 * this could be called from bottom half, or from NAPI polling method.
1661 */
Petri Gynther4055eae2015-03-25 12:35:16 -07001662static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001663 unsigned int budget)
1664{
Petri Gynther4055eae2015-03-25 12:35:16 -07001665 struct bcmgenet_priv *priv = ring->priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001666 struct net_device *dev = priv->dev;
1667 struct enet_cb *cb;
1668 struct sk_buff *skb;
1669 u32 dma_length_status;
1670 unsigned long dma_flag;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001671 int len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001672 unsigned int rxpktprocessed = 0, rxpkttoprocess;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001673 unsigned int bytes_processed = 0;
Doug Bergerd5810ca2017-03-13 17:41:37 -07001674 unsigned int p_index, mask;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001675 unsigned int discards;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001676
Doug Bergerd5810ca2017-03-13 17:41:37 -07001677 /* Clear status before servicing to reduce spurious interrupts */
1678 if (ring->index == DESC_INDEX) {
1679 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_RXDMA_DONE,
1680 INTRL2_CPU_CLEAR);
1681 } else {
1682 mask = 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index);
1683 bcmgenet_intrl2_1_writel(priv,
1684 mask,
1685 INTRL2_CPU_CLEAR);
1686 }
1687
Petri Gynther4055eae2015-03-25 12:35:16 -07001688 p_index = bcmgenet_rdma_ring_readl(priv, ring->index, RDMA_PROD_INDEX);
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001689
1690 discards = (p_index >> DMA_P_INDEX_DISCARD_CNT_SHIFT) &
1691 DMA_P_INDEX_DISCARD_CNT_MASK;
1692 if (discards > ring->old_discards) {
1693 discards = discards - ring->old_discards;
Florian Fainelli37a30b42017-03-16 10:27:08 -07001694 ring->errors += discards;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001695 ring->old_discards += discards;
1696
1697 /* Clear HW register when we reach 75% of maximum 0xFFFF */
1698 if (ring->old_discards >= 0xC000) {
1699 ring->old_discards = 0;
Petri Gynther4055eae2015-03-25 12:35:16 -07001700 bcmgenet_rdma_ring_writel(priv, ring->index, 0,
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001701 RDMA_PROD_INDEX);
1702 }
1703 }
1704
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001705 p_index &= DMA_P_INDEX_MASK;
Doug Bergerc298ede2017-03-13 17:41:33 -07001706 rxpkttoprocess = (p_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001707
1708 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001709 "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001710
1711 while ((rxpktprocessed < rxpkttoprocess) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001712 (rxpktprocessed < budget)) {
Doug Berger9a9ba2a2020-03-17 17:05:36 -07001713 struct status_64 *status;
1714 __be16 rx_csum;
1715
Petri Gynther8ac467e2015-03-09 13:40:00 -07001716 cb = &priv->rx_cbs[ring->read_ptr];
Petri Gyntherd6707be2015-03-12 15:48:00 -07001717 skb = bcmgenet_rx_refill(priv, cb);
Florian Fainellib629be52014-09-08 11:37:52 -07001718
Florian Fainellib629be52014-09-08 11:37:52 -07001719 if (unlikely(!skb)) {
Florian Fainelli37a30b42017-03-16 10:27:08 -07001720 ring->dropped++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001721 goto next;
Florian Fainellib629be52014-09-08 11:37:52 -07001722 }
1723
Doug Berger9a9ba2a2020-03-17 17:05:36 -07001724 status = (struct status_64 *)skb->data;
1725 dma_length_status = status->length_status;
1726 if (dev->features & NETIF_F_RXCSUM) {
Doug Berger81015532019-12-17 16:51:10 -08001727 rx_csum = (__force __be16)(status->rx_csum & 0xffff);
Doug Berger9a9ba2a2020-03-17 17:05:36 -07001728 skb->csum = (__force __wsum)ntohs(rx_csum);
1729 skb->ip_summed = CHECKSUM_COMPLETE;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001730 }
1731
1732 /* DMA flags and length are still valid no matter how
1733 * we got the Receive Status Vector (64B RSB or register)
1734 */
1735 dma_flag = dma_length_status & 0xffff;
1736 len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
1737
1738 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001739 "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
Petri Gynther8ac467e2015-03-09 13:40:00 -07001740 __func__, p_index, ring->c_index,
1741 ring->read_ptr, dma_length_status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001742
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001743 if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
1744 netif_err(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001745 "dropping fragmented packet!\n");
Florian Fainelli37a30b42017-03-16 10:27:08 -07001746 ring->errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001747 dev_kfree_skb_any(skb);
1748 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001749 }
Petri Gyntherd6707be2015-03-12 15:48:00 -07001750
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001751 /* report errors */
1752 if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
1753 DMA_RX_OV |
1754 DMA_RX_NO |
1755 DMA_RX_LG |
1756 DMA_RX_RXER))) {
1757 netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001758 (unsigned int)dma_flag);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001759 if (dma_flag & DMA_RX_CRC_ERROR)
1760 dev->stats.rx_crc_errors++;
1761 if (dma_flag & DMA_RX_OV)
1762 dev->stats.rx_over_errors++;
1763 if (dma_flag & DMA_RX_NO)
1764 dev->stats.rx_frame_errors++;
1765 if (dma_flag & DMA_RX_LG)
1766 dev->stats.rx_length_errors++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001767 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001768 dev_kfree_skb_any(skb);
1769 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001770 } /* error packet */
1771
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001772 skb_put(skb, len);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001773
Doug Berger9a9ba2a2020-03-17 17:05:36 -07001774 /* remove RSB and hardware 2bytes added for IP alignment */
1775 skb_pull(skb, 66);
1776 len -= 66;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001777
1778 if (priv->crc_fwd_en) {
1779 skb_trim(skb, len - ETH_FCS_LEN);
1780 len -= ETH_FCS_LEN;
1781 }
1782
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001783 bytes_processed += len;
1784
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001785 /*Finish setting up the received SKB and send it to the kernel*/
1786 skb->protocol = eth_type_trans(skb, priv->dev);
Florian Fainelli37a30b42017-03-16 10:27:08 -07001787 ring->packets++;
1788 ring->bytes += len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001789 if (dma_flag & DMA_RX_MULT)
1790 dev->stats.multicast++;
1791
1792 /* Notify kernel */
Petri Gynther4055eae2015-03-25 12:35:16 -07001793 napi_gro_receive(&ring->napi, skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001794 netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
1795
Petri Gyntherd6707be2015-03-12 15:48:00 -07001796next:
Florian Fainellicf377d82014-10-10 10:51:52 -07001797 rxpktprocessed++;
Petri Gynther8ac467e2015-03-09 13:40:00 -07001798 if (likely(ring->read_ptr < ring->end_ptr))
1799 ring->read_ptr++;
1800 else
1801 ring->read_ptr = ring->cb_ptr;
1802
1803 ring->c_index = (ring->c_index + 1) & DMA_C_INDEX_MASK;
Petri Gynther4055eae2015-03-25 12:35:16 -07001804 bcmgenet_rdma_ring_writel(priv, ring->index, ring->c_index, RDMA_CONS_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001805 }
1806
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001807 ring->dim.bytes = bytes_processed;
1808 ring->dim.packets = rxpktprocessed;
1809
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001810 return rxpktprocessed;
1811}
1812
Petri Gynther3ab11332015-03-25 12:35:15 -07001813/* Rx NAPI polling method */
1814static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
1815{
Petri Gynther4055eae2015-03-25 12:35:16 -07001816 struct bcmgenet_rx_ring *ring = container_of(napi,
1817 struct bcmgenet_rx_ring, napi);
Yamin Friedmanf06d0ca2019-07-23 10:22:47 +03001818 struct dim_sample dim_sample = {};
Petri Gynther3ab11332015-03-25 12:35:15 -07001819 unsigned int work_done;
1820
Petri Gynther4055eae2015-03-25 12:35:16 -07001821 work_done = bcmgenet_desc_rx(ring, budget);
Petri Gynther3ab11332015-03-25 12:35:15 -07001822
1823 if (work_done < budget) {
Eric Dumazeteb96ce02016-04-08 22:06:40 -07001824 napi_complete_done(napi, work_done);
Petri Gynther4055eae2015-03-25 12:35:16 -07001825 ring->int_enable(ring);
Petri Gynther3ab11332015-03-25 12:35:15 -07001826 }
1827
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001828 if (ring->dim.use_dim) {
Tal Gilboa8960b382019-01-31 16:44:48 +02001829 dim_update_sample(ring->dim.event_ctr, ring->dim.packets,
1830 ring->dim.bytes, &dim_sample);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001831 net_dim(&ring->dim.dim, dim_sample);
1832 }
1833
Petri Gynther3ab11332015-03-25 12:35:15 -07001834 return work_done;
1835}
1836
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001837static void bcmgenet_dim_work(struct work_struct *work)
1838{
Tal Gilboa8960b382019-01-31 16:44:48 +02001839 struct dim *dim = container_of(work, struct dim, work);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001840 struct bcmgenet_net_dim *ndim =
1841 container_of(dim, struct bcmgenet_net_dim, dim);
1842 struct bcmgenet_rx_ring *ring =
1843 container_of(ndim, struct bcmgenet_rx_ring, dim);
Tal Gilboa8960b382019-01-31 16:44:48 +02001844 struct dim_cq_moder cur_profile =
Tal Gilboa026a8072018-04-24 13:36:01 +03001845 net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001846
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07001847 bcmgenet_set_rx_coalesce(ring, cur_profile.usec, cur_profile.pkts);
Tal Gilboac002bd52018-11-05 12:07:52 +02001848 dim->state = DIM_START_MEASURE;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001849}
1850
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001851/* Assign skb to RX DMA descriptor. */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001852static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv,
1853 struct bcmgenet_rx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001854{
1855 struct enet_cb *cb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001856 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001857 int i;
1858
Petri Gynther8ac467e2015-03-09 13:40:00 -07001859 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001860
1861 /* loop here for each buffer needing assign */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001862 for (i = 0; i < ring->size; i++) {
1863 cb = ring->cbs + i;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001864 skb = bcmgenet_rx_refill(priv, cb);
1865 if (skb)
Florian Fainellid4fec852017-08-24 15:56:29 -07001866 dev_consume_skb_any(skb);
Petri Gyntherd6707be2015-03-12 15:48:00 -07001867 if (!cb->skb)
1868 return -ENOMEM;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001869 }
1870
Petri Gyntherd6707be2015-03-12 15:48:00 -07001871 return 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001872}
1873
1874static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
1875{
Doug Bergerf48bed12017-07-14 16:12:10 -07001876 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001877 struct enet_cb *cb;
1878 int i;
1879
1880 for (i = 0; i < priv->num_rx_bds; i++) {
1881 cb = &priv->rx_cbs[i];
1882
Doug Bergerf48bed12017-07-14 16:12:10 -07001883 skb = bcmgenet_free_rx_cb(&priv->pdev->dev, cb);
1884 if (skb)
Florian Fainellid4fec852017-08-24 15:56:29 -07001885 dev_consume_skb_any(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001886 }
1887}
1888
Florian Fainellic91b7f62014-07-23 10:42:12 -07001889static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable)
Florian Fainellie29585b2014-07-21 15:29:20 -07001890{
1891 u32 reg;
1892
1893 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
Doug Berger88f6c8b2020-03-16 14:44:56 -07001894 if (reg & CMD_SW_RESET)
1895 return;
Florian Fainellie29585b2014-07-21 15:29:20 -07001896 if (enable)
1897 reg |= mask;
1898 else
1899 reg &= ~mask;
1900 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
1901
1902 /* UniMAC stops on a packet boundary, wait for a full-size packet
1903 * to be processed
1904 */
1905 if (enable == 0)
1906 usleep_range(1000, 2000);
1907}
1908
Doug Berger28c2d1a2017-10-25 15:04:13 -07001909static void reset_umac(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001910{
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001911 /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
1912 bcmgenet_rbuf_ctrl_set(priv, 0);
1913 udelay(10);
1914
Doug Berger88f6c8b2020-03-16 14:44:56 -07001915 /* issue soft reset and disable MAC while updating its registers */
1916 bcmgenet_umac_writel(priv, CMD_SW_RESET, UMAC_CMD);
Doug Berger612eb1c2020-03-16 14:44:55 -07001917 udelay(2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001918}
1919
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001920static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
1921{
1922 /* Mask all interrupts.*/
1923 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1924 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001925 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1926 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001927}
1928
Florian Fainelli37850e32015-10-17 14:22:46 -07001929static void bcmgenet_link_intr_enable(struct bcmgenet_priv *priv)
1930{
1931 u32 int0_enable = 0;
1932
1933 /* Monitor cable plug/unplugged event for internal PHY, external PHY
1934 * and MoCA PHY
1935 */
1936 if (priv->internal_phy) {
1937 int0_enable |= UMAC_IRQ_LINK_EVENT;
Doug Berger25382b92019-10-16 16:06:32 -07001938 if (GENET_IS_V1(priv) || GENET_IS_V2(priv) || GENET_IS_V3(priv))
1939 int0_enable |= UMAC_IRQ_PHY_DET_R;
Florian Fainelli37850e32015-10-17 14:22:46 -07001940 } else if (priv->ext_phy) {
1941 int0_enable |= UMAC_IRQ_LINK_EVENT;
1942 } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
1943 if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
1944 int0_enable |= UMAC_IRQ_LINK_EVENT;
1945 }
1946 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
1947}
1948
Doug Berger28c2d1a2017-10-25 15:04:13 -07001949static void init_umac(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001950{
1951 struct device *kdev = &priv->pdev->dev;
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001952 u32 reg;
1953 u32 int0_enable = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001954
1955 dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
1956
Doug Berger28c2d1a2017-10-25 15:04:13 -07001957 reset_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001958
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001959 /* clear tx/rx counter */
1960 bcmgenet_umac_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001961 MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
1962 UMAC_MIB_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001963 bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
1964
1965 bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
1966
Doug Berger9a9ba2a2020-03-17 17:05:36 -07001967 /* init tx registers, enable TSB */
1968 reg = bcmgenet_tbuf_ctrl_get(priv);
1969 reg |= TBUF_64B_EN;
1970 bcmgenet_tbuf_ctrl_set(priv, reg);
1971
1972 /* init rx registers, enable ip header optimization and RSB */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001973 reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
Doug Berger9a9ba2a2020-03-17 17:05:36 -07001974 reg |= RBUF_ALIGN_2B | RBUF_64B_EN;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001975 bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
1976
Doug Berger9a9ba2a2020-03-17 17:05:36 -07001977 /* enable rx checksumming */
1978 reg = bcmgenet_rbuf_readl(priv, RBUF_CHK_CTRL);
1979 reg |= RBUF_RXCHK_EN | RBUF_L3_PARSE_DIS;
1980 /* If UniMAC forwards CRC, we need to skip over it to get
1981 * a valid CHK bit to be set in the per-packet status word
1982 */
1983 if (priv->crc_fwd_en)
1984 reg |= RBUF_SKIP_FCS;
1985 else
1986 reg &= ~RBUF_SKIP_FCS;
1987 bcmgenet_rbuf_writel(priv, reg, RBUF_CHK_CTRL);
1988
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001989 if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
1990 bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
1991
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001992 bcmgenet_intr_disable(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001993
Florian Fainelli37850e32015-10-17 14:22:46 -07001994 /* Configure backpressure vectors for MoCA */
1995 if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001996 reg = bcmgenet_bp_mc_get(priv);
1997 reg |= BIT(priv->hw_params->bp_in_en_shift);
1998
1999 /* bp_mask: back pressure mask */
2000 if (netif_is_multiqueue(priv->dev))
2001 reg |= priv->hw_params->bp_in_mask;
2002 else
2003 reg &= ~priv->hw_params->bp_in_mask;
2004 bcmgenet_bp_mc_set(priv, reg);
2005 }
2006
2007 /* Enable MDIO interrupts on GENET v3+ */
2008 if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002009 int0_enable |= (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002010
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002011 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002012
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002013 dev_dbg(kdev, "done init umac\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002014}
2015
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002016static void bcmgenet_init_dim(struct bcmgenet_rx_ring *ring,
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002017 void (*cb)(struct work_struct *work))
2018{
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002019 struct bcmgenet_net_dim *dim = &ring->dim;
2020
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002021 INIT_WORK(&dim->dim.work, cb);
Tal Gilboac002bd52018-11-05 12:07:52 +02002022 dim->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002023 dim->event_ctr = 0;
2024 dim->packets = 0;
2025 dim->bytes = 0;
2026}
2027
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002028static void bcmgenet_init_rx_coalesce(struct bcmgenet_rx_ring *ring)
2029{
2030 struct bcmgenet_net_dim *dim = &ring->dim;
Tal Gilboa8960b382019-01-31 16:44:48 +02002031 struct dim_cq_moder moder;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002032 u32 usecs, pkts;
2033
2034 usecs = ring->rx_coalesce_usecs;
2035 pkts = ring->rx_max_coalesced_frames;
2036
2037 /* If DIM was enabled, re-apply default parameters */
2038 if (dim->use_dim) {
Tal Gilboa026a8072018-04-24 13:36:01 +03002039 moder = net_dim_get_def_rx_moderation(dim->dim.mode);
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002040 usecs = moder.usec;
2041 pkts = moder.pkts;
2042 }
2043
2044 bcmgenet_set_rx_coalesce(ring, usecs, pkts);
2045}
2046
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002047/* Initialize a Tx ring along with corresponding hardware registers */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002048static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
2049 unsigned int index, unsigned int size,
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002050 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002051{
2052 struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
2053 u32 words_per_bd = WORDS_PER_BD(priv);
2054 u32 flow_period_val = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002055
2056 spin_lock_init(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002057 ring->priv = priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002058 ring->index = index;
2059 if (index == DESC_INDEX) {
2060 ring->queue = 0;
2061 ring->int_enable = bcmgenet_tx_ring16_int_enable;
2062 ring->int_disable = bcmgenet_tx_ring16_int_disable;
2063 } else {
2064 ring->queue = index + 1;
2065 ring->int_enable = bcmgenet_tx_ring_int_enable;
2066 ring->int_disable = bcmgenet_tx_ring_int_disable;
2067 }
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002068 ring->cbs = priv->tx_cbs + start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002069 ring->size = size;
Petri Gynther66d06752015-03-04 14:30:01 -08002070 ring->clean_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002071 ring->c_index = 0;
2072 ring->free_bds = size;
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002073 ring->write_ptr = start_ptr;
2074 ring->cb_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002075 ring->end_ptr = end_ptr - 1;
2076 ring->prod_index = 0;
2077
2078 /* Set flow period for ring != 16 */
2079 if (index != DESC_INDEX)
2080 flow_period_val = ENET_MAX_MTU_SIZE << 16;
2081
2082 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
2083 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
2084 bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
2085 /* Disable rate control for now */
2086 bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002087 TDMA_FLOW_PERIOD);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002088 bcmgenet_tdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002089 ((size << DMA_RING_SIZE_SHIFT) |
2090 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002091
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002092 /* Set start and end address, read and write pointers */
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002093 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002094 DMA_START_ADDR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002095 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002096 TDMA_READ_PTR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002097 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002098 TDMA_WRITE_PTR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002099 bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002100 DMA_END_ADDR);
Doug Berger75879352017-10-25 15:04:14 -07002101
2102 /* Initialize Tx NAPI */
Florian Fainelli148965d2020-01-23 09:49:34 -08002103 netif_tx_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll,
2104 NAPI_POLL_WEIGHT);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002105}
2106
2107/* Initialize a RDMA ring */
2108static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
Petri Gynther8ac467e2015-03-09 13:40:00 -07002109 unsigned int index, unsigned int size,
2110 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002111{
Petri Gynther8ac467e2015-03-09 13:40:00 -07002112 struct bcmgenet_rx_ring *ring = &priv->rx_rings[index];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002113 u32 words_per_bd = WORDS_PER_BD(priv);
2114 int ret;
2115
Petri Gynther4055eae2015-03-25 12:35:16 -07002116 ring->priv = priv;
Petri Gynther8ac467e2015-03-09 13:40:00 -07002117 ring->index = index;
Petri Gynther4055eae2015-03-25 12:35:16 -07002118 if (index == DESC_INDEX) {
2119 ring->int_enable = bcmgenet_rx_ring16_int_enable;
2120 ring->int_disable = bcmgenet_rx_ring16_int_disable;
2121 } else {
2122 ring->int_enable = bcmgenet_rx_ring_int_enable;
2123 ring->int_disable = bcmgenet_rx_ring_int_disable;
2124 }
Petri Gynther8ac467e2015-03-09 13:40:00 -07002125 ring->cbs = priv->rx_cbs + start_ptr;
2126 ring->size = size;
2127 ring->c_index = 0;
2128 ring->read_ptr = start_ptr;
2129 ring->cb_ptr = start_ptr;
2130 ring->end_ptr = end_ptr - 1;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002131
Petri Gynther8ac467e2015-03-09 13:40:00 -07002132 ret = bcmgenet_alloc_rx_buffers(priv, ring);
2133 if (ret)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002134 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002135
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002136 bcmgenet_init_dim(ring, bcmgenet_dim_work);
2137 bcmgenet_init_rx_coalesce(ring);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002138
Doug Berger75879352017-10-25 15:04:14 -07002139 /* Initialize Rx NAPI */
2140 netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll,
2141 NAPI_POLL_WEIGHT);
2142
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002143 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
2144 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
2145 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002146 ((size << DMA_RING_SIZE_SHIFT) |
2147 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002148 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002149 (DMA_FC_THRESH_LO <<
2150 DMA_XOFF_THRESHOLD_SHIFT) |
2151 DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
Petri Gynther6f5a2722015-03-06 13:45:00 -08002152
2153 /* Set start and end address, read and write pointers */
Petri Gynther8ac467e2015-03-09 13:40:00 -07002154 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2155 DMA_START_ADDR);
2156 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2157 RDMA_READ_PTR);
2158 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2159 RDMA_WRITE_PTR);
2160 bcmgenet_rdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Petri Gynther6f5a2722015-03-06 13:45:00 -08002161 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002162
2163 return ret;
2164}
2165
Petri Gynthere2aadb42015-03-25 12:35:14 -07002166static void bcmgenet_enable_tx_napi(struct bcmgenet_priv *priv)
2167{
2168 unsigned int i;
2169 struct bcmgenet_tx_ring *ring;
2170
2171 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2172 ring = &priv->tx_rings[i];
2173 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002174 ring->int_enable(ring);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002175 }
2176
2177 ring = &priv->tx_rings[DESC_INDEX];
2178 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002179 ring->int_enable(ring);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002180}
2181
2182static void bcmgenet_disable_tx_napi(struct bcmgenet_priv *priv)
2183{
2184 unsigned int i;
2185 struct bcmgenet_tx_ring *ring;
2186
2187 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2188 ring = &priv->tx_rings[i];
2189 napi_disable(&ring->napi);
2190 }
2191
2192 ring = &priv->tx_rings[DESC_INDEX];
2193 napi_disable(&ring->napi);
2194}
2195
2196static void bcmgenet_fini_tx_napi(struct bcmgenet_priv *priv)
2197{
2198 unsigned int i;
2199 struct bcmgenet_tx_ring *ring;
2200
2201 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2202 ring = &priv->tx_rings[i];
2203 netif_napi_del(&ring->napi);
2204 }
2205
2206 ring = &priv->tx_rings[DESC_INDEX];
2207 netif_napi_del(&ring->napi);
2208}
2209
Petri Gynther16c6d662015-02-23 11:00:45 -08002210/* Initialize Tx queues
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002211 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002212 * Queues 0-3 are priority-based, each one has 32 descriptors,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002213 * with queue 0 being the highest priority queue.
2214 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002215 * Queue 16 is the default Tx queue with
Petri Gynther51a966a2015-02-23 11:00:46 -08002216 * GENET_Q16_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors.
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002217 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002218 * The transmit control block pool is then partitioned as follows:
2219 * - Tx queue 0 uses tx_cbs[0..31]
2220 * - Tx queue 1 uses tx_cbs[32..63]
2221 * - Tx queue 2 uses tx_cbs[64..95]
2222 * - Tx queue 3 uses tx_cbs[96..127]
2223 * - Tx queue 16 uses tx_cbs[128..255]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002224 */
Petri Gynther16c6d662015-02-23 11:00:45 -08002225static void bcmgenet_init_tx_queues(struct net_device *dev)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002226{
2227 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther16c6d662015-02-23 11:00:45 -08002228 u32 i, dma_enable;
2229 u32 dma_ctrl, ring_cfg;
Petri Gynther37742162014-10-07 09:30:01 -07002230 u32 dma_priority[3] = {0, 0, 0};
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002231
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002232 dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
2233 dma_enable = dma_ctrl & DMA_EN;
2234 dma_ctrl &= ~DMA_EN;
2235 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
2236
Petri Gynther16c6d662015-02-23 11:00:45 -08002237 dma_ctrl = 0;
2238 ring_cfg = 0;
2239
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002240 /* Enable strict priority arbiter mode */
2241 bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
2242
Petri Gynther16c6d662015-02-23 11:00:45 -08002243 /* Initialize Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002244 for (i = 0; i < priv->hw_params->tx_queues; i++) {
Petri Gynther51a966a2015-02-23 11:00:46 -08002245 bcmgenet_init_tx_ring(priv, i, priv->hw_params->tx_bds_per_q,
2246 i * priv->hw_params->tx_bds_per_q,
2247 (i + 1) * priv->hw_params->tx_bds_per_q);
Petri Gynther16c6d662015-02-23 11:00:45 -08002248 ring_cfg |= (1 << i);
2249 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002250 dma_priority[DMA_PRIO_REG_INDEX(i)] |=
2251 ((GENET_Q0_PRIORITY + i) << DMA_PRIO_REG_SHIFT(i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002252 }
2253
Petri Gynther16c6d662015-02-23 11:00:45 -08002254 /* Initialize Tx default queue 16 */
Petri Gynther51a966a2015-02-23 11:00:46 -08002255 bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_Q16_TX_BD_CNT,
Petri Gynther16c6d662015-02-23 11:00:45 -08002256 priv->hw_params->tx_queues *
Petri Gynther51a966a2015-02-23 11:00:46 -08002257 priv->hw_params->tx_bds_per_q,
Petri Gynther16c6d662015-02-23 11:00:45 -08002258 TOTAL_DESC);
2259 ring_cfg |= (1 << DESC_INDEX);
2260 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002261 dma_priority[DMA_PRIO_REG_INDEX(DESC_INDEX)] |=
2262 ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) <<
2263 DMA_PRIO_REG_SHIFT(DESC_INDEX));
Petri Gynther16c6d662015-02-23 11:00:45 -08002264
2265 /* Set Tx queue priorities */
Petri Gynther37742162014-10-07 09:30:01 -07002266 bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
2267 bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
2268 bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);
2269
Petri Gynther16c6d662015-02-23 11:00:45 -08002270 /* Enable Tx queues */
2271 bcmgenet_tdma_writel(priv, ring_cfg, DMA_RING_CFG);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002272
Petri Gynther16c6d662015-02-23 11:00:45 -08002273 /* Enable Tx DMA */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002274 if (dma_enable)
Petri Gynther16c6d662015-02-23 11:00:45 -08002275 dma_ctrl |= DMA_EN;
2276 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002277}
2278
Petri Gynther3ab11332015-03-25 12:35:15 -07002279static void bcmgenet_enable_rx_napi(struct bcmgenet_priv *priv)
2280{
Petri Gynther4055eae2015-03-25 12:35:16 -07002281 unsigned int i;
2282 struct bcmgenet_rx_ring *ring;
2283
2284 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2285 ring = &priv->rx_rings[i];
2286 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002287 ring->int_enable(ring);
Petri Gynther4055eae2015-03-25 12:35:16 -07002288 }
2289
2290 ring = &priv->rx_rings[DESC_INDEX];
2291 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002292 ring->int_enable(ring);
Petri Gynther3ab11332015-03-25 12:35:15 -07002293}
2294
2295static void bcmgenet_disable_rx_napi(struct bcmgenet_priv *priv)
2296{
Petri Gynther4055eae2015-03-25 12:35:16 -07002297 unsigned int i;
2298 struct bcmgenet_rx_ring *ring;
2299
2300 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2301 ring = &priv->rx_rings[i];
2302 napi_disable(&ring->napi);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002303 cancel_work_sync(&ring->dim.dim.work);
Petri Gynther4055eae2015-03-25 12:35:16 -07002304 }
2305
2306 ring = &priv->rx_rings[DESC_INDEX];
2307 napi_disable(&ring->napi);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002308 cancel_work_sync(&ring->dim.dim.work);
Petri Gynther3ab11332015-03-25 12:35:15 -07002309}
2310
2311static void bcmgenet_fini_rx_napi(struct bcmgenet_priv *priv)
2312{
Petri Gynther4055eae2015-03-25 12:35:16 -07002313 unsigned int i;
2314 struct bcmgenet_rx_ring *ring;
2315
2316 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2317 ring = &priv->rx_rings[i];
2318 netif_napi_del(&ring->napi);
2319 }
2320
2321 ring = &priv->rx_rings[DESC_INDEX];
2322 netif_napi_del(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002323}
2324
Petri Gynther8ac467e2015-03-09 13:40:00 -07002325/* Initialize Rx queues
2326 *
2327 * Queues 0-15 are priority queues. Hardware Filtering Block (HFB) can be
2328 * used to direct traffic to these queues.
2329 *
2330 * Queue 16 is the default Rx queue with GENET_Q16_RX_BD_CNT descriptors.
2331 */
2332static int bcmgenet_init_rx_queues(struct net_device *dev)
2333{
2334 struct bcmgenet_priv *priv = netdev_priv(dev);
2335 u32 i;
2336 u32 dma_enable;
2337 u32 dma_ctrl;
2338 u32 ring_cfg;
2339 int ret;
2340
2341 dma_ctrl = bcmgenet_rdma_readl(priv, DMA_CTRL);
2342 dma_enable = dma_ctrl & DMA_EN;
2343 dma_ctrl &= ~DMA_EN;
2344 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2345
2346 dma_ctrl = 0;
2347 ring_cfg = 0;
2348
2349 /* Initialize Rx priority queues */
2350 for (i = 0; i < priv->hw_params->rx_queues; i++) {
2351 ret = bcmgenet_init_rx_ring(priv, i,
2352 priv->hw_params->rx_bds_per_q,
2353 i * priv->hw_params->rx_bds_per_q,
2354 (i + 1) *
2355 priv->hw_params->rx_bds_per_q);
2356 if (ret)
2357 return ret;
2358
2359 ring_cfg |= (1 << i);
2360 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2361 }
2362
2363 /* Initialize Rx default queue 16 */
2364 ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, GENET_Q16_RX_BD_CNT,
2365 priv->hw_params->rx_queues *
2366 priv->hw_params->rx_bds_per_q,
2367 TOTAL_DESC);
2368 if (ret)
2369 return ret;
2370
2371 ring_cfg |= (1 << DESC_INDEX);
2372 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
2373
2374 /* Enable rings */
2375 bcmgenet_rdma_writel(priv, ring_cfg, DMA_RING_CFG);
2376
2377 /* Configure ring as descriptor ring and re-enable DMA if enabled */
2378 if (dma_enable)
2379 dma_ctrl |= DMA_EN;
2380 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2381
2382 return 0;
2383}
2384
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002385static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
2386{
2387 int ret = 0;
2388 int timeout = 0;
2389 u32 reg;
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002390 u32 dma_ctrl;
2391 int i;
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002392
2393 /* Disable TDMA to stop add more frames in TX DMA */
2394 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2395 reg &= ~DMA_EN;
2396 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2397
2398 /* Check TDMA status register to confirm TDMA is disabled */
2399 while (timeout++ < DMA_TIMEOUT_VAL) {
2400 reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
2401 if (reg & DMA_DISABLED)
2402 break;
2403
2404 udelay(1);
2405 }
2406
2407 if (timeout == DMA_TIMEOUT_VAL) {
2408 netdev_warn(priv->dev, "Timed out while disabling TX DMA\n");
2409 ret = -ETIMEDOUT;
2410 }
2411
2412 /* Wait 10ms for packet drain in both tx and rx dma */
2413 usleep_range(10000, 20000);
2414
2415 /* Disable RDMA */
2416 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2417 reg &= ~DMA_EN;
2418 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2419
2420 timeout = 0;
2421 /* Check RDMA status register to confirm RDMA is disabled */
2422 while (timeout++ < DMA_TIMEOUT_VAL) {
2423 reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
2424 if (reg & DMA_DISABLED)
2425 break;
2426
2427 udelay(1);
2428 }
2429
2430 if (timeout == DMA_TIMEOUT_VAL) {
2431 netdev_warn(priv->dev, "Timed out while disabling RX DMA\n");
2432 ret = -ETIMEDOUT;
2433 }
2434
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002435 dma_ctrl = 0;
2436 for (i = 0; i < priv->hw_params->rx_queues; i++)
2437 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2438 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2439 reg &= ~dma_ctrl;
2440 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2441
2442 dma_ctrl = 0;
2443 for (i = 0; i < priv->hw_params->tx_queues; i++)
2444 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2445 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2446 reg &= ~dma_ctrl;
2447 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2448
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002449 return ret;
2450}
2451
Petri Gynther9abab962015-03-30 00:29:01 -07002452static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002453{
Petri Gynthere178c8c2016-04-09 00:20:36 -07002454 struct netdev_queue *txq;
Doug Bergerf48bed12017-07-14 16:12:10 -07002455 int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002456
Petri Gynther9abab962015-03-30 00:29:01 -07002457 bcmgenet_fini_rx_napi(priv);
2458 bcmgenet_fini_tx_napi(priv);
2459
Markus Elfring399e06a2019-08-22 20:02:56 +02002460 for (i = 0; i < priv->num_tx_bds; i++)
2461 dev_kfree_skb(bcmgenet_free_tx_cb(&priv->pdev->dev,
2462 priv->tx_cbs + i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002463
Petri Gynthere178c8c2016-04-09 00:20:36 -07002464 for (i = 0; i < priv->hw_params->tx_queues; i++) {
2465 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[i].queue);
2466 netdev_tx_reset_queue(txq);
2467 }
2468
2469 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[DESC_INDEX].queue);
2470 netdev_tx_reset_queue(txq);
2471
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002472 bcmgenet_free_rx_buffers(priv);
2473 kfree(priv->rx_cbs);
2474 kfree(priv->tx_cbs);
2475}
2476
2477/* init_edma: Initialize DMA control register */
2478static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
2479{
2480 int ret;
Petri Gynther014012a2015-02-23 11:00:45 -08002481 unsigned int i;
2482 struct enet_cb *cb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002483
Petri Gynther6f5a2722015-03-06 13:45:00 -08002484 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002485
Petri Gynther6f5a2722015-03-06 13:45:00 -08002486 /* Initialize common Rx ring structures */
2487 priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
2488 priv->num_rx_bds = TOTAL_DESC;
2489 priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct enet_cb),
2490 GFP_KERNEL);
2491 if (!priv->rx_cbs)
2492 return -ENOMEM;
2493
2494 for (i = 0; i < priv->num_rx_bds; i++) {
2495 cb = priv->rx_cbs + i;
2496 cb->bd_addr = priv->rx_bds + i * DMA_DESC_SIZE;
2497 }
2498
Brian Norris7fc527f2014-07-29 14:34:14 -07002499 /* Initialize common TX ring structures */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002500 priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
2501 priv->num_tx_bds = TOTAL_DESC;
Florian Fainellic489be02014-07-23 10:42:15 -07002502 priv->tx_cbs = kcalloc(priv->num_tx_bds, sizeof(struct enet_cb),
Florian Fainellic91b7f62014-07-23 10:42:12 -07002503 GFP_KERNEL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002504 if (!priv->tx_cbs) {
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002505 kfree(priv->rx_cbs);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002506 return -ENOMEM;
2507 }
2508
Petri Gynther014012a2015-02-23 11:00:45 -08002509 for (i = 0; i < priv->num_tx_bds; i++) {
2510 cb = priv->tx_cbs + i;
2511 cb->bd_addr = priv->tx_bds + i * DMA_DESC_SIZE;
2512 }
2513
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002514 /* Init rDma */
Stefan Wahrena50e3a92019-11-11 20:49:23 +01002515 bcmgenet_rdma_writel(priv, priv->dma_max_burst_length,
2516 DMA_SCB_BURST_SIZE);
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002517
2518 /* Initialize Rx queues */
2519 ret = bcmgenet_init_rx_queues(priv->dev);
2520 if (ret) {
2521 netdev_err(priv->dev, "failed to initialize Rx queues\n");
2522 bcmgenet_free_rx_buffers(priv);
2523 kfree(priv->rx_cbs);
2524 kfree(priv->tx_cbs);
2525 return ret;
2526 }
2527
2528 /* Init tDma */
Stefan Wahrena50e3a92019-11-11 20:49:23 +01002529 bcmgenet_tdma_writel(priv, priv->dma_max_burst_length,
2530 DMA_SCB_BURST_SIZE);
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002531
Petri Gynther16c6d662015-02-23 11:00:45 -08002532 /* Initialize Tx queues */
2533 bcmgenet_init_tx_queues(priv->dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002534
2535 return 0;
2536}
2537
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002538/* Interrupt bottom half */
2539static void bcmgenet_irq_task(struct work_struct *work)
2540{
Doug Berger07c52d62017-03-09 16:58:47 -08002541 unsigned int status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002542 struct bcmgenet_priv *priv = container_of(
2543 work, struct bcmgenet_priv, bcmgenet_irq_work);
2544
2545 netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
2546
Doug Bergerb0447ec2017-10-25 15:04:17 -07002547 spin_lock_irq(&priv->lock);
Doug Berger07c52d62017-03-09 16:58:47 -08002548 status = priv->irq0_stat;
2549 priv->irq0_stat = 0;
Doug Bergerb0447ec2017-10-25 15:04:17 -07002550 spin_unlock_irq(&priv->lock);
Doug Berger07c52d62017-03-09 16:58:47 -08002551
Doug Berger25382b92019-10-16 16:06:32 -07002552 if (status & UMAC_IRQ_PHY_DET_R &&
Doug Berger0686bd92019-11-05 11:07:26 -08002553 priv->dev->phydev->autoneg != AUTONEG_ENABLE) {
Doug Berger25382b92019-10-16 16:06:32 -07002554 phy_init_hw(priv->dev->phydev);
Doug Berger0686bd92019-11-05 11:07:26 -08002555 genphy_config_aneg(priv->dev->phydev);
2556 }
Doug Berger25382b92019-10-16 16:06:32 -07002557
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002558 /* Link UP/DOWN event */
Doug Berger7de48402019-10-16 16:06:29 -07002559 if (status & UMAC_IRQ_LINK_EVENT)
Heiner Kallweit28b2e0d2018-01-10 21:21:31 +01002560 phy_mac_interrupt(priv->dev->phydev);
Doug Berger25382b92019-10-16 16:06:32 -07002561
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002562}
2563
Petri Gynther4055eae2015-03-25 12:35:16 -07002564/* bcmgenet_isr1: handle Rx and Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002565static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
2566{
2567 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002568 struct bcmgenet_rx_ring *rx_ring;
2569 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08002570 unsigned int index, status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002571
Doug Berger07c52d62017-03-09 16:58:47 -08002572 /* Read irq status */
2573 status = bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002574 ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002575
Brian Norris7fc527f2014-07-29 14:34:14 -07002576 /* clear interrupts */
Doug Berger07c52d62017-03-09 16:58:47 -08002577 bcmgenet_intrl2_1_writel(priv, status, INTRL2_CPU_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002578
2579 netif_dbg(priv, intr, priv->dev,
Doug Berger07c52d62017-03-09 16:58:47 -08002580 "%s: IRQ=0x%x\n", __func__, status);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002581
Petri Gynther4055eae2015-03-25 12:35:16 -07002582 /* Check Rx priority queue interrupts */
2583 for (index = 0; index < priv->hw_params->rx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08002584 if (!(status & BIT(UMAC_IRQ1_RX_INTR_SHIFT + index)))
Petri Gynther4055eae2015-03-25 12:35:16 -07002585 continue;
2586
2587 rx_ring = &priv->rx_rings[index];
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002588 rx_ring->dim.event_ctr++;
Petri Gynther4055eae2015-03-25 12:35:16 -07002589
2590 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2591 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002592 __napi_schedule_irqoff(&rx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07002593 }
2594 }
2595
2596 /* Check Tx priority queue interrupts */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002597 for (index = 0; index < priv->hw_params->tx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08002598 if (!(status & BIT(index)))
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002599 continue;
2600
Petri Gynther4055eae2015-03-25 12:35:16 -07002601 tx_ring = &priv->tx_rings[index];
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002602
Petri Gynther4055eae2015-03-25 12:35:16 -07002603 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2604 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002605 __napi_schedule_irqoff(&tx_ring->napi);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002606 }
2607 }
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002608
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002609 return IRQ_HANDLED;
2610}
2611
Petri Gynther4055eae2015-03-25 12:35:16 -07002612/* bcmgenet_isr0: handle Rx and Tx default queues + other stuff */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002613static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
2614{
2615 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002616 struct bcmgenet_rx_ring *rx_ring;
2617 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08002618 unsigned int status;
2619 unsigned long flags;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002620
Doug Berger07c52d62017-03-09 16:58:47 -08002621 /* Read irq status */
2622 status = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002623 ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002624
Brian Norris7fc527f2014-07-29 14:34:14 -07002625 /* clear interrupts */
Doug Berger07c52d62017-03-09 16:58:47 -08002626 bcmgenet_intrl2_0_writel(priv, status, INTRL2_CPU_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002627
2628 netif_dbg(priv, intr, priv->dev,
Doug Berger07c52d62017-03-09 16:58:47 -08002629 "IRQ=0x%x\n", status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002630
Doug Berger07c52d62017-03-09 16:58:47 -08002631 if (status & UMAC_IRQ_RXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07002632 rx_ring = &priv->rx_rings[DESC_INDEX];
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002633 rx_ring->dim.event_ctr++;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002634
Petri Gynther4055eae2015-03-25 12:35:16 -07002635 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2636 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002637 __napi_schedule_irqoff(&rx_ring->napi);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002638 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002639 }
Petri Gynther4055eae2015-03-25 12:35:16 -07002640
Doug Berger07c52d62017-03-09 16:58:47 -08002641 if (status & UMAC_IRQ_TXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07002642 tx_ring = &priv->tx_rings[DESC_INDEX];
2643
2644 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2645 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002646 __napi_schedule_irqoff(&tx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07002647 }
2648 }
2649
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002650 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Doug Berger07c52d62017-03-09 16:58:47 -08002651 status & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002652 wake_up(&priv->wq);
2653 }
2654
Doug Berger07c52d62017-03-09 16:58:47 -08002655 /* all other interested interrupts handled in bottom half */
Doug Berger25382b92019-10-16 16:06:32 -07002656 status &= (UMAC_IRQ_LINK_EVENT | UMAC_IRQ_PHY_DET_R);
Doug Berger07c52d62017-03-09 16:58:47 -08002657 if (status) {
2658 /* Save irq status for bottom-half processing. */
2659 spin_lock_irqsave(&priv->lock, flags);
2660 priv->irq0_stat |= status;
2661 spin_unlock_irqrestore(&priv->lock, flags);
2662
2663 schedule_work(&priv->bcmgenet_irq_work);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002664 }
2665
2666 return IRQ_HANDLED;
2667}
2668
Florian Fainelli85620562014-07-21 15:29:23 -07002669static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
2670{
2671 struct bcmgenet_priv *priv = dev_id;
2672
2673 pm_wakeup_event(&priv->pdev->dev, 0);
2674
2675 return IRQ_HANDLED;
2676}
2677
Florian Fainelli4d2e8882015-07-31 11:42:54 -07002678#ifdef CONFIG_NET_POLL_CONTROLLER
2679static void bcmgenet_poll_controller(struct net_device *dev)
2680{
2681 struct bcmgenet_priv *priv = netdev_priv(dev);
2682
2683 /* Invoke the main RX/TX interrupt handler */
2684 disable_irq(priv->irq0);
2685 bcmgenet_isr0(priv->irq0, priv);
2686 enable_irq(priv->irq0);
2687
2688 /* And the interrupt handler for RX/TX priority queues */
2689 disable_irq(priv->irq1);
2690 bcmgenet_isr1(priv->irq1, priv);
2691 enable_irq(priv->irq1);
2692}
2693#endif
2694
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002695static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
2696{
2697 u32 reg;
2698
2699 reg = bcmgenet_rbuf_ctrl_get(priv);
2700 reg |= BIT(1);
2701 bcmgenet_rbuf_ctrl_set(priv, reg);
2702 udelay(10);
2703
2704 reg &= ~BIT(1);
2705 bcmgenet_rbuf_ctrl_set(priv, reg);
2706 udelay(10);
2707}
2708
2709static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002710 unsigned char *addr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002711{
2712 bcmgenet_umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
2713 (addr[2] << 8) | addr[3], UMAC_MAC0);
2714 bcmgenet_umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
2715}
2716
Jeremy Linton26bd9cc2020-02-24 16:54:02 -06002717static void bcmgenet_get_hw_addr(struct bcmgenet_priv *priv,
2718 unsigned char *addr)
2719{
2720 u32 addr_tmp;
2721
2722 addr_tmp = bcmgenet_umac_readl(priv, UMAC_MAC0);
2723 addr[0] = addr_tmp >> 24;
2724 addr[1] = (addr_tmp >> 16) & 0xff;
2725 addr[2] = (addr_tmp >> 8) & 0xff;
2726 addr[3] = addr_tmp & 0xff;
2727 addr_tmp = bcmgenet_umac_readl(priv, UMAC_MAC1);
2728 addr[4] = (addr_tmp >> 8) & 0xff;
2729 addr[5] = addr_tmp & 0xff;
2730}
2731
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002732/* Returns a reusable dma control register value */
2733static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
2734{
2735 u32 reg;
2736 u32 dma_ctrl;
2737
2738 /* disable DMA */
2739 dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
2740 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2741 reg &= ~dma_ctrl;
2742 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2743
2744 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2745 reg &= ~dma_ctrl;
2746 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2747
2748 bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
2749 udelay(10);
2750 bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
2751
2752 return dma_ctrl;
2753}
2754
2755static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
2756{
2757 u32 reg;
2758
2759 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2760 reg |= dma_ctrl;
2761 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2762
2763 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2764 reg |= dma_ctrl;
2765 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2766}
2767
Petri Gynther0034de42015-03-13 14:45:00 -07002768/* bcmgenet_hfb_clear
2769 *
2770 * Clear Hardware Filter Block and disable all filtering.
2771 */
2772static void bcmgenet_hfb_clear(struct bcmgenet_priv *priv)
2773{
2774 u32 i;
2775
2776 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_CTRL);
2777 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS);
2778 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS + 4);
2779
2780 for (i = DMA_INDEX2RING_0; i <= DMA_INDEX2RING_7; i++)
2781 bcmgenet_rdma_writel(priv, 0x0, i);
2782
2783 for (i = 0; i < (priv->hw_params->hfb_filter_cnt / 4); i++)
2784 bcmgenet_hfb_reg_writel(priv, 0x0,
2785 HFB_FLT_LEN_V3PLUS + i * sizeof(u32));
2786
2787 for (i = 0; i < priv->hw_params->hfb_filter_cnt *
2788 priv->hw_params->hfb_filter_size; i++)
2789 bcmgenet_hfb_writel(priv, 0x0, i * sizeof(u32));
2790}
2791
2792static void bcmgenet_hfb_init(struct bcmgenet_priv *priv)
2793{
2794 if (GENET_IS_V1(priv) || GENET_IS_V2(priv))
2795 return;
2796
2797 bcmgenet_hfb_clear(priv);
2798}
2799
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002800static void bcmgenet_netif_start(struct net_device *dev)
2801{
2802 struct bcmgenet_priv *priv = netdev_priv(dev);
2803
2804 /* Start the network engine */
Petri Gynther3ab11332015-03-25 12:35:15 -07002805 bcmgenet_enable_rx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002806
2807 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
2808
Doug Bergerd215dba2017-10-25 15:04:16 -07002809 bcmgenet_enable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002810
Florian Fainelli37850e32015-10-17 14:22:46 -07002811 /* Monitor link interrupts now */
2812 bcmgenet_link_intr_enable(priv);
2813
Doug Berger6c97f012017-10-25 15:04:19 -07002814 phy_start(dev->phydev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002815}
2816
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002817static int bcmgenet_open(struct net_device *dev)
2818{
2819 struct bcmgenet_priv *priv = netdev_priv(dev);
2820 unsigned long dma_ctrl;
2821 u32 reg;
2822 int ret;
2823
2824 netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
2825
2826 /* Turn on the clock */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002827 clk_prepare_enable(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002828
Florian Fainellia642c4f2015-03-23 15:09:56 -07002829 /* If this is an internal GPHY, power it back on now, before UniMAC is
2830 * brought out of reset as absolutely no UniMAC activity is allowed
2831 */
Florian Fainellic624f892015-07-16 15:51:17 -07002832 if (priv->internal_phy)
Florian Fainellia642c4f2015-03-23 15:09:56 -07002833 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
2834
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002835 /* take MAC out of reset */
2836 bcmgenet_umac_reset(priv);
2837
Doug Berger28c2d1a2017-10-25 15:04:13 -07002838 init_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002839
Doug Berger206f54b2019-12-17 16:51:12 -08002840 /* Apply features again in case we changed them while interface was
2841 * down
2842 */
2843 bcmgenet_set_features(dev, dev->features);
2844
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002845 bcmgenet_set_hw_addr(priv, dev->dev_addr);
2846
Florian Fainellic624f892015-07-16 15:51:17 -07002847 if (priv->internal_phy) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002848 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
2849 reg |= EXT_ENERGY_DET_MASK;
2850 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
2851 }
2852
2853 /* Disable RX/TX DMA and flush TX queues */
2854 dma_ctrl = bcmgenet_dma_disable(priv);
2855
2856 /* Reinitialize TDMA and RDMA and SW housekeeping */
2857 ret = bcmgenet_init_dma(priv);
2858 if (ret) {
2859 netdev_err(dev, "failed to initialize DMA\n");
Doug Berger6b6d017f2019-11-05 11:07:25 -08002860 goto err_clk_disable;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002861 }
2862
2863 /* Always enable ring 16 - descriptor ring */
2864 bcmgenet_enable_dma(priv, dma_ctrl);
2865
Petri Gynther0034de42015-03-13 14:45:00 -07002866 /* HFB init */
2867 bcmgenet_hfb_init(priv);
2868
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002869 ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002870 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002871 if (ret < 0) {
2872 netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
2873 goto err_fini_dma;
2874 }
2875
2876 ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002877 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002878 if (ret < 0) {
2879 netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
2880 goto err_irq0;
2881 }
2882
Doug Berger6b6d017f2019-11-05 11:07:25 -08002883 ret = bcmgenet_mii_probe(dev);
2884 if (ret) {
2885 netdev_err(dev, "failed to connect to PHY\n");
2886 goto err_irq1;
2887 }
2888
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002889 bcmgenet_netif_start(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002890
Doug Berger09e805d2018-11-01 15:55:37 -07002891 netif_tx_start_all_queues(dev);
2892
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002893 return 0;
2894
Doug Berger6b6d017f2019-11-05 11:07:25 -08002895err_irq1:
2896 free_irq(priv->irq1, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002897err_irq0:
Florian Fainelli978ffac42015-07-16 15:51:15 -07002898 free_irq(priv->irq0, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002899err_fini_dma:
Doug Berger4fd6dc92017-10-25 15:04:12 -07002900 bcmgenet_dma_teardown(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002901 bcmgenet_fini_dma(priv);
2902err_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08002903 if (priv->internal_phy)
2904 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002905 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002906 return ret;
2907}
2908
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002909static void bcmgenet_netif_stop(struct net_device *dev)
2910{
2911 struct bcmgenet_priv *priv = netdev_priv(dev);
2912
Doug Bergerd215dba2017-10-25 15:04:16 -07002913 bcmgenet_disable_tx_napi(priv);
Doug Berger09e805d2018-11-01 15:55:37 -07002914 netif_tx_disable(dev);
Doug Bergerd215dba2017-10-25 15:04:16 -07002915
2916 /* Disable MAC receive */
2917 umac_enable_set(priv, CMD_RX_EN, false);
2918
2919 bcmgenet_dma_teardown(priv);
2920
2921 /* Disable MAC transmit. TX DMA disabled must be done before this */
2922 umac_enable_set(priv, CMD_TX_EN, false);
2923
Doug Berger6c97f012017-10-25 15:04:19 -07002924 phy_stop(dev->phydev);
Petri Gynther3ab11332015-03-25 12:35:15 -07002925 bcmgenet_disable_rx_napi(priv);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002926 bcmgenet_intr_disable(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002927
2928 /* Wait for pending work items to complete. Since interrupts are
2929 * disabled no new work will be scheduled.
2930 */
2931 cancel_work_sync(&priv->bcmgenet_irq_work);
Florian Fainellicc013fb2014-08-11 14:50:43 -07002932
Florian Fainellicc013fb2014-08-11 14:50:43 -07002933 priv->old_link = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002934 priv->old_speed = -1;
Florian Fainellicc013fb2014-08-11 14:50:43 -07002935 priv->old_duplex = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002936 priv->old_pause = -1;
Doug Bergerd215dba2017-10-25 15:04:16 -07002937
2938 /* tx reclaim */
2939 bcmgenet_tx_reclaim_all(dev);
2940 bcmgenet_fini_dma(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002941}
2942
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002943static int bcmgenet_close(struct net_device *dev)
2944{
2945 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Bergerd215dba2017-10-25 15:04:16 -07002946 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002947
2948 netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
2949
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002950 bcmgenet_netif_stop(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002951
Florian Fainellic96e7312014-11-10 18:06:20 -08002952 /* Really kill the PHY state machine and disconnect from it */
Doug Berger6c97f012017-10-25 15:04:19 -07002953 phy_disconnect(dev->phydev);
Florian Fainellic96e7312014-11-10 18:06:20 -08002954
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002955 free_irq(priv->irq0, priv);
2956 free_irq(priv->irq1, priv);
2957
Florian Fainellic624f892015-07-16 15:51:17 -07002958 if (priv->internal_phy)
Florian Fainellica8cf342015-03-23 15:09:51 -07002959 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002960
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002961 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002962
Florian Fainellica8cf342015-03-23 15:09:51 -07002963 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002964}
2965
Florian Fainelli13ea6572015-06-04 16:15:50 -07002966static void bcmgenet_dump_tx_queue(struct bcmgenet_tx_ring *ring)
2967{
2968 struct bcmgenet_priv *priv = ring->priv;
2969 u32 p_index, c_index, intsts, intmsk;
2970 struct netdev_queue *txq;
2971 unsigned int free_bds;
Florian Fainelli13ea6572015-06-04 16:15:50 -07002972 bool txq_stopped;
2973
2974 if (!netif_msg_tx_err(priv))
2975 return;
2976
2977 txq = netdev_get_tx_queue(priv->dev, ring->queue);
2978
Doug Bergerb0447ec2017-10-25 15:04:17 -07002979 spin_lock(&ring->lock);
Florian Fainelli13ea6572015-06-04 16:15:50 -07002980 if (ring->index == DESC_INDEX) {
2981 intsts = ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
2982 intmsk = UMAC_IRQ_TXDMA_DONE | UMAC_IRQ_TXDMA_MBDONE;
2983 } else {
2984 intsts = ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
2985 intmsk = 1 << ring->index;
2986 }
2987 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
2988 p_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_PROD_INDEX);
2989 txq_stopped = netif_tx_queue_stopped(txq);
2990 free_bds = ring->free_bds;
Doug Bergerb0447ec2017-10-25 15:04:17 -07002991 spin_unlock(&ring->lock);
Florian Fainelli13ea6572015-06-04 16:15:50 -07002992
2993 netif_err(priv, tx_err, priv->dev, "Ring %d queue %d status summary\n"
2994 "TX queue status: %s, interrupts: %s\n"
2995 "(sw)free_bds: %d (sw)size: %d\n"
2996 "(sw)p_index: %d (hw)p_index: %d\n"
2997 "(sw)c_index: %d (hw)c_index: %d\n"
2998 "(sw)clean_p: %d (sw)write_p: %d\n"
2999 "(sw)cb_ptr: %d (sw)end_ptr: %d\n",
3000 ring->index, ring->queue,
3001 txq_stopped ? "stopped" : "active",
3002 intsts & intmsk ? "enabled" : "disabled",
3003 free_bds, ring->size,
3004 ring->prod_index, p_index & DMA_P_INDEX_MASK,
3005 ring->c_index, c_index & DMA_C_INDEX_MASK,
3006 ring->clean_ptr, ring->write_ptr,
3007 ring->cb_ptr, ring->end_ptr);
3008}
3009
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05003010static void bcmgenet_timeout(struct net_device *dev, unsigned int txqueue)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003011{
3012 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003013 u32 int0_enable = 0;
3014 u32 int1_enable = 0;
3015 unsigned int q;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003016
3017 netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
3018
Florian Fainelli13ea6572015-06-04 16:15:50 -07003019 for (q = 0; q < priv->hw_params->tx_queues; q++)
3020 bcmgenet_dump_tx_queue(&priv->tx_rings[q]);
3021 bcmgenet_dump_tx_queue(&priv->tx_rings[DESC_INDEX]);
3022
3023 bcmgenet_tx_reclaim_all(dev);
3024
3025 for (q = 0; q < priv->hw_params->tx_queues; q++)
3026 int1_enable |= (1 << q);
3027
3028 int0_enable = UMAC_IRQ_TXDMA_DONE;
3029
3030 /* Re-enable TX interrupts if disabled */
3031 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
3032 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
3033
Florian Westphal860e9532016-05-03 16:33:13 +02003034 netif_trans_update(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003035
3036 dev->stats.tx_errors++;
3037
3038 netif_tx_wake_all_queues(dev);
3039}
3040
Justin Chen35cbef92019-07-17 14:58:53 -07003041#define MAX_MDF_FILTER 17
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003042
3043static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
3044 unsigned char *addr,
Justin Chen35cbef92019-07-17 14:58:53 -07003045 int *i)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003046{
Florian Fainellic91b7f62014-07-23 10:42:12 -07003047 bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
3048 UMAC_MDF_ADDR + (*i * 4));
3049 bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
3050 addr[4] << 8 | addr[5],
3051 UMAC_MDF_ADDR + ((*i + 1) * 4));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003052 *i += 2;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003053}
3054
3055static void bcmgenet_set_rx_mode(struct net_device *dev)
3056{
3057 struct bcmgenet_priv *priv = netdev_priv(dev);
3058 struct netdev_hw_addr *ha;
Justin Chen35cbef92019-07-17 14:58:53 -07003059 int i, nfilter;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003060 u32 reg;
3061
3062 netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
3063
Justin Chen35cbef92019-07-17 14:58:53 -07003064 /* Number of filters needed */
3065 nfilter = netdev_uc_count(dev) + netdev_mc_count(dev) + 2;
3066
3067 /*
3068 * Turn on promicuous mode for three scenarios
3069 * 1. IFF_PROMISC flag is set
3070 * 2. IFF_ALLMULTI flag is set
3071 * 3. The number of filters needed exceeds the number filters
3072 * supported by the hardware.
3073 */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003074 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
Justin Chen35cbef92019-07-17 14:58:53 -07003075 if ((dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) ||
3076 (nfilter > MAX_MDF_FILTER)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003077 reg |= CMD_PROMISC;
3078 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3079 bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
3080 return;
3081 } else {
3082 reg &= ~CMD_PROMISC;
3083 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3084 }
3085
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003086 /* update MDF filter */
3087 i = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003088 /* Broadcast */
Justin Chen35cbef92019-07-17 14:58:53 -07003089 bcmgenet_set_mdf_addr(priv, dev->broadcast, &i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003090 /* my own address.*/
Justin Chen35cbef92019-07-17 14:58:53 -07003091 bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003092
Justin Chen35cbef92019-07-17 14:58:53 -07003093 /* Unicast */
3094 netdev_for_each_uc_addr(ha, dev)
3095 bcmgenet_set_mdf_addr(priv, ha->addr, &i);
3096
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003097 /* Multicast */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003098 netdev_for_each_mc_addr(ha, dev)
Justin Chen35cbef92019-07-17 14:58:53 -07003099 bcmgenet_set_mdf_addr(priv, ha->addr, &i);
3100
3101 /* Enable filters */
3102 reg = GENMASK(MAX_MDF_FILTER - 1, MAX_MDF_FILTER - nfilter);
3103 bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003104}
3105
3106/* Set the hardware MAC address. */
3107static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
3108{
3109 struct sockaddr *addr = p;
3110
3111 /* Setting the MAC address at the hardware level is not possible
3112 * without disabling the UniMAC RX/TX enable bits.
3113 */
3114 if (netif_running(dev))
3115 return -EBUSY;
3116
3117 ether_addr_copy(dev->dev_addr, addr->sa_data);
3118
3119 return 0;
3120}
3121
Florian Fainelli37a30b42017-03-16 10:27:08 -07003122static struct net_device_stats *bcmgenet_get_stats(struct net_device *dev)
3123{
3124 struct bcmgenet_priv *priv = netdev_priv(dev);
3125 unsigned long tx_bytes = 0, tx_packets = 0;
3126 unsigned long rx_bytes = 0, rx_packets = 0;
3127 unsigned long rx_errors = 0, rx_dropped = 0;
3128 struct bcmgenet_tx_ring *tx_ring;
3129 struct bcmgenet_rx_ring *rx_ring;
3130 unsigned int q;
3131
3132 for (q = 0; q < priv->hw_params->tx_queues; q++) {
3133 tx_ring = &priv->tx_rings[q];
3134 tx_bytes += tx_ring->bytes;
3135 tx_packets += tx_ring->packets;
3136 }
3137 tx_ring = &priv->tx_rings[DESC_INDEX];
3138 tx_bytes += tx_ring->bytes;
3139 tx_packets += tx_ring->packets;
3140
3141 for (q = 0; q < priv->hw_params->rx_queues; q++) {
3142 rx_ring = &priv->rx_rings[q];
3143
3144 rx_bytes += rx_ring->bytes;
3145 rx_packets += rx_ring->packets;
3146 rx_errors += rx_ring->errors;
3147 rx_dropped += rx_ring->dropped;
3148 }
3149 rx_ring = &priv->rx_rings[DESC_INDEX];
3150 rx_bytes += rx_ring->bytes;
3151 rx_packets += rx_ring->packets;
3152 rx_errors += rx_ring->errors;
3153 rx_dropped += rx_ring->dropped;
3154
3155 dev->stats.tx_bytes = tx_bytes;
3156 dev->stats.tx_packets = tx_packets;
3157 dev->stats.rx_bytes = rx_bytes;
3158 dev->stats.rx_packets = rx_packets;
3159 dev->stats.rx_errors = rx_errors;
3160 dev->stats.rx_missed_errors = rx_errors;
Doug Bergera6d0b832020-04-23 15:44:17 -07003161 dev->stats.rx_dropped = rx_dropped;
Florian Fainelli37a30b42017-03-16 10:27:08 -07003162 return &dev->stats;
3163}
3164
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003165static const struct net_device_ops bcmgenet_netdev_ops = {
3166 .ndo_open = bcmgenet_open,
3167 .ndo_stop = bcmgenet_close,
3168 .ndo_start_xmit = bcmgenet_xmit,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003169 .ndo_tx_timeout = bcmgenet_timeout,
3170 .ndo_set_rx_mode = bcmgenet_set_rx_mode,
3171 .ndo_set_mac_address = bcmgenet_set_mac_addr,
Heiner Kallweitfd786fb12020-01-21 22:09:33 +01003172 .ndo_do_ioctl = phy_do_ioctl_running,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003173 .ndo_set_features = bcmgenet_set_features,
Florian Fainelli4d2e8882015-07-31 11:42:54 -07003174#ifdef CONFIG_NET_POLL_CONTROLLER
3175 .ndo_poll_controller = bcmgenet_poll_controller,
3176#endif
Florian Fainelli37a30b42017-03-16 10:27:08 -07003177 .ndo_get_stats = bcmgenet_get_stats,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003178};
3179
3180/* Array of GENET hardware parameters/characteristics */
3181static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
3182 [GENET_V1] = {
3183 .tx_queues = 0,
Petri Gynther51a966a2015-02-23 11:00:46 -08003184 .tx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003185 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003186 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003187 .bp_in_en_shift = 16,
3188 .bp_in_mask = 0xffff,
3189 .hfb_filter_cnt = 16,
3190 .qtag_mask = 0x1F,
3191 .hfb_offset = 0x1000,
3192 .rdma_offset = 0x2000,
3193 .tdma_offset = 0x3000,
3194 .words_per_bd = 2,
3195 },
3196 [GENET_V2] = {
3197 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003198 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003199 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003200 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003201 .bp_in_en_shift = 16,
3202 .bp_in_mask = 0xffff,
3203 .hfb_filter_cnt = 16,
3204 .qtag_mask = 0x1F,
3205 .tbuf_offset = 0x0600,
3206 .hfb_offset = 0x1000,
3207 .hfb_reg_offset = 0x2000,
3208 .rdma_offset = 0x3000,
3209 .tdma_offset = 0x4000,
3210 .words_per_bd = 2,
3211 .flags = GENET_HAS_EXT,
3212 },
3213 [GENET_V3] = {
3214 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003215 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003216 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003217 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003218 .bp_in_en_shift = 17,
3219 .bp_in_mask = 0x1ffff,
3220 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003221 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003222 .qtag_mask = 0x3F,
3223 .tbuf_offset = 0x0600,
3224 .hfb_offset = 0x8000,
3225 .hfb_reg_offset = 0xfc00,
3226 .rdma_offset = 0x10000,
3227 .tdma_offset = 0x11000,
3228 .words_per_bd = 2,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003229 .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR |
3230 GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003231 },
3232 [GENET_V4] = {
3233 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003234 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003235 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003236 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003237 .bp_in_en_shift = 17,
3238 .bp_in_mask = 0x1ffff,
3239 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003240 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003241 .qtag_mask = 0x3F,
3242 .tbuf_offset = 0x0600,
3243 .hfb_offset = 0x8000,
3244 .hfb_reg_offset = 0xfc00,
3245 .rdma_offset = 0x2000,
3246 .tdma_offset = 0x4000,
3247 .words_per_bd = 3,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003248 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3249 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003250 },
Doug Berger42138082017-03-13 17:41:42 -07003251 [GENET_V5] = {
3252 .tx_queues = 4,
3253 .tx_bds_per_q = 32,
3254 .rx_queues = 0,
3255 .rx_bds_per_q = 0,
3256 .bp_in_en_shift = 17,
3257 .bp_in_mask = 0x1ffff,
3258 .hfb_filter_cnt = 48,
3259 .hfb_filter_size = 128,
3260 .qtag_mask = 0x3F,
3261 .tbuf_offset = 0x0600,
3262 .hfb_offset = 0x8000,
3263 .hfb_reg_offset = 0xfc00,
3264 .rdma_offset = 0x2000,
3265 .tdma_offset = 0x4000,
3266 .words_per_bd = 3,
3267 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3268 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
3269 },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003270};
3271
3272/* Infer hardware parameters from the detected GENET version */
3273static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
3274{
3275 struct bcmgenet_hw_params *params;
3276 u32 reg;
3277 u8 major;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003278 u16 gphy_rev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003279
Doug Berger42138082017-03-13 17:41:42 -07003280 if (GENET_IS_V5(priv) || GENET_IS_V4(priv)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003281 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3282 genet_dma_ring_regs = genet_dma_ring_regs_v4;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003283 } else if (GENET_IS_V3(priv)) {
3284 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3285 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003286 } else if (GENET_IS_V2(priv)) {
3287 bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
3288 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003289 } else if (GENET_IS_V1(priv)) {
3290 bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
3291 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003292 }
3293
3294 /* enum genet_version starts at 1 */
3295 priv->hw_params = &bcmgenet_hw_params[priv->version];
3296 params = priv->hw_params;
3297
3298 /* Read GENET HW version */
3299 reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
3300 major = (reg >> 24 & 0x0f);
Doug Berger42138082017-03-13 17:41:42 -07003301 if (major == 6)
3302 major = 5;
3303 else if (major == 5)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003304 major = 4;
3305 else if (major == 0)
3306 major = 1;
3307 if (major != priv->version) {
3308 dev_err(&priv->pdev->dev,
3309 "GENET version mismatch, got: %d, configured for: %d\n",
3310 major, priv->version);
3311 }
3312
3313 /* Print the GENET core version */
3314 dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003315 major, (reg >> 16) & 0x0f, reg & 0xffff);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003316
Florian Fainelli487320c2014-09-19 13:07:53 -07003317 /* Store the integrated PHY revision for the MDIO probing function
3318 * to pass this information to the PHY driver. The PHY driver expects
3319 * to find the PHY major revision in bits 15:8 while the GENET register
3320 * stores that information in bits 7:0, account for that.
Florian Fainellib04a2f52014-12-03 09:56:59 -08003321 *
3322 * On newer chips, starting with PHY revision G0, a new scheme is
3323 * deployed similar to the Starfighter 2 switch with GPHY major
3324 * revision in bits 15:8 and patch level in bits 7:0. Major revision 0
3325 * is reserved as well as special value 0x01ff, we have a small
3326 * heuristic to check for the new GPHY revision and re-arrange things
3327 * so the GPHY driver is happy.
Florian Fainelli487320c2014-09-19 13:07:53 -07003328 */
Florian Fainellib04a2f52014-12-03 09:56:59 -08003329 gphy_rev = reg & 0xffff;
3330
Doug Berger42138082017-03-13 17:41:42 -07003331 if (GENET_IS_V5(priv)) {
3332 /* The EPHY revision should come from the MDIO registers of
3333 * the PHY not from GENET.
3334 */
3335 if (gphy_rev != 0) {
3336 pr_warn("GENET is reporting EPHY revision: 0x%04x\n",
3337 gphy_rev);
3338 }
Doug Bergereca4bad2017-03-09 16:58:45 -08003339 /* This is reserved so should require special treatment */
David S. Miller101c4312017-03-15 11:59:10 -07003340 } else if (gphy_rev == 0 || gphy_rev == 0x01ff) {
Doug Bergereca4bad2017-03-09 16:58:45 -08003341 pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev);
3342 return;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003343 /* This is the good old scheme, just GPHY major, no minor nor patch */
Doug Berger42138082017-03-13 17:41:42 -07003344 } else if ((gphy_rev & 0xf0) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003345 priv->gphy_rev = gphy_rev << 8;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003346 /* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */
Doug Berger42138082017-03-13 17:41:42 -07003347 } else if ((gphy_rev & 0xff00) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003348 priv->gphy_rev = gphy_rev;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003349 }
Florian Fainelli487320c2014-09-19 13:07:53 -07003350
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003351#ifdef CONFIG_PHYS_ADDR_T_64BIT
3352 if (!(params->flags & GENET_HAS_40BITS))
3353 pr_warn("GENET does not support 40-bits PA\n");
3354#endif
3355
3356 pr_debug("Configuration for version: %d\n"
Petri Gynther3feafa02015-03-05 17:40:14 -08003357 "TXq: %1d, TXqBDs: %1d, RXq: %1d, RXqBDs: %1d\n"
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003358 "BP << en: %2d, BP msk: 0x%05x\n"
3359 "HFB count: %2d, QTAQ msk: 0x%05x\n"
3360 "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
3361 "RDMA: 0x%05x, TDMA: 0x%05x\n"
3362 "Words/BD: %d\n",
3363 priv->version,
Petri Gynther51a966a2015-02-23 11:00:46 -08003364 params->tx_queues, params->tx_bds_per_q,
Petri Gynther3feafa02015-03-05 17:40:14 -08003365 params->rx_queues, params->rx_bds_per_q,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003366 params->bp_in_en_shift, params->bp_in_mask,
3367 params->hfb_filter_cnt, params->qtag_mask,
3368 params->tbuf_offset, params->hfb_offset,
3369 params->hfb_reg_offset,
3370 params->rdma_offset, params->tdma_offset,
3371 params->words_per_bd);
3372}
3373
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003374struct bcmgenet_plat_data {
3375 enum bcmgenet_version version;
3376 u32 dma_max_burst_length;
3377};
3378
3379static const struct bcmgenet_plat_data v1_plat_data = {
3380 .version = GENET_V1,
3381 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3382};
3383
3384static const struct bcmgenet_plat_data v2_plat_data = {
3385 .version = GENET_V2,
3386 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3387};
3388
3389static const struct bcmgenet_plat_data v3_plat_data = {
3390 .version = GENET_V3,
3391 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3392};
3393
3394static const struct bcmgenet_plat_data v4_plat_data = {
3395 .version = GENET_V4,
3396 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3397};
3398
3399static const struct bcmgenet_plat_data v5_plat_data = {
3400 .version = GENET_V5,
3401 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3402};
3403
3404static const struct bcmgenet_plat_data bcm2711_plat_data = {
3405 .version = GENET_V5,
3406 .dma_max_burst_length = 0x08,
3407};
3408
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003409static const struct of_device_id bcmgenet_match[] = {
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003410 { .compatible = "brcm,genet-v1", .data = &v1_plat_data },
3411 { .compatible = "brcm,genet-v2", .data = &v2_plat_data },
3412 { .compatible = "brcm,genet-v3", .data = &v3_plat_data },
3413 { .compatible = "brcm,genet-v4", .data = &v4_plat_data },
3414 { .compatible = "brcm,genet-v5", .data = &v5_plat_data },
3415 { .compatible = "brcm,bcm2711-genet-v5", .data = &bcm2711_plat_data },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003416 { },
3417};
Luis de Bethencourte8048e52015-09-18 17:55:02 +02003418MODULE_DEVICE_TABLE(of, bcmgenet_match);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003419
3420static int bcmgenet_probe(struct platform_device *pdev)
3421{
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003422 struct bcmgenet_platform_data *pd = pdev->dev.platform_data;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003423 struct device_node *dn = pdev->dev.of_node;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003424 const struct of_device_id *of_id = NULL;
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003425 const struct bcmgenet_plat_data *pdata;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003426 struct bcmgenet_priv *priv;
3427 struct net_device *dev;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07003428 unsigned int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003429 int err = -EIO;
3430
Petri Gynther3feafee2015-03-05 17:40:12 -08003431 /* Up to GENET_MAX_MQ_CNT + 1 TX queues and RX queues */
3432 dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1,
3433 GENET_MAX_MQ_CNT + 1);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003434 if (!dev) {
3435 dev_err(&pdev->dev, "can't allocate net device\n");
3436 return -ENOMEM;
3437 }
3438
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003439 if (dn) {
3440 of_id = of_match_node(bcmgenet_match, dn);
3441 if (!of_id)
3442 return -EINVAL;
3443 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003444
3445 priv = netdev_priv(dev);
3446 priv->irq0 = platform_get_irq(pdev, 0);
Stefan Wahren2b65f932019-11-11 20:49:21 +01003447 if (priv->irq0 < 0) {
3448 err = priv->irq0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003449 goto err;
3450 }
Stefan Wahren2b65f932019-11-11 20:49:21 +01003451 priv->irq1 = platform_get_irq(pdev, 1);
3452 if (priv->irq1 < 0) {
3453 err = priv->irq1;
3454 goto err;
3455 }
3456 priv->wol_irq = platform_get_irq_optional(pdev, 2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003457
YueHaibing4ca33482019-08-21 21:41:31 +08003458 priv->base = devm_platform_ioremap_resource(pdev, 0);
Fabio Estevam5343a102014-02-24 00:47:24 -03003459 if (IS_ERR(priv->base)) {
3460 err = PTR_ERR(priv->base);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003461 goto err;
3462 }
3463
Doug Berger07c52d62017-03-09 16:58:47 -08003464 spin_lock_init(&priv->lock);
3465
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003466 SET_NETDEV_DEV(dev, &pdev->dev);
3467 dev_set_drvdata(&pdev->dev, dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003468 dev->watchdog_timeo = 2 * HZ;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003469 dev->ethtool_ops = &bcmgenet_ethtool_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003470 dev->netdev_ops = &bcmgenet_netdev_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003471
3472 priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
3473
Doug Bergerae895c42019-12-17 16:51:13 -08003474 /* Set default features */
3475 dev->features |= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
3476 NETIF_F_RXCSUM;
3477 dev->hw_features |= dev->features;
3478 dev->vlan_features |= dev->features;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003479
Florian Fainelli85620562014-07-21 15:29:23 -07003480 /* Request the WOL interrupt and advertise suspend if available */
3481 priv->wol_irq_disabled = true;
3482 err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0,
3483 dev->name, priv);
3484 if (!err)
3485 device_set_wakeup_capable(&pdev->dev, 1);
3486
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003487 /* Set the needed headroom to account for any possible
3488 * features enabling/disabling at runtime
3489 */
3490 dev->needed_headroom += 64;
3491
3492 netdev_boot_setup_check(dev);
3493
3494 priv->dev = dev;
3495 priv->pdev = pdev;
Jeremy Linton99c6b062020-02-24 16:54:01 -06003496
3497 pdata = device_get_match_data(&pdev->dev);
3498 if (pdata) {
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003499 priv->version = pdata->version;
3500 priv->dma_max_burst_length = pdata->dma_max_burst_length;
3501 } else {
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003502 priv->version = pd->genet_version;
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003503 priv->dma_max_burst_length = DMA_MAX_BURST_LENGTH;
3504 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003505
Florian Fainellie4a60a92014-08-11 14:50:42 -07003506 priv->clk = devm_clk_get(&priv->pdev->dev, "enet");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003507 if (IS_ERR(priv->clk)) {
Jeremy Lintonae200c22020-02-24 16:54:03 -06003508 dev_dbg(&priv->pdev->dev, "failed to get enet clock\n");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003509 priv->clk = NULL;
3510 }
Florian Fainellie4a60a92014-08-11 14:50:42 -07003511
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003512 clk_prepare_enable(priv->clk);
Florian Fainellie4a60a92014-08-11 14:50:42 -07003513
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003514 bcmgenet_set_hw_params(priv);
3515
Doug Berger99d55632019-12-17 16:51:08 -08003516 err = -EIO;
3517 if (priv->hw_params->flags & GENET_HAS_40BITS)
3518 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
3519 if (err)
3520 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3521 if (err)
3522 goto err;
3523
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003524 /* Mii wait queue */
3525 init_waitqueue_head(&priv->wq);
3526 /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
3527 priv->rx_buf_len = RX_BUF_LENGTH;
3528 INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
3529
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003530 priv->clk_wol = devm_clk_get(&priv->pdev->dev, "enet-wol");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003531 if (IS_ERR(priv->clk_wol)) {
Jeremy Lintonae200c22020-02-24 16:54:03 -06003532 dev_dbg(&priv->pdev->dev, "failed to get enet-wol clock\n");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003533 priv->clk_wol = NULL;
3534 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003535
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003536 priv->clk_eee = devm_clk_get(&priv->pdev->dev, "enet-eee");
3537 if (IS_ERR(priv->clk_eee)) {
Jeremy Lintonae200c22020-02-24 16:54:03 -06003538 dev_dbg(&priv->pdev->dev, "failed to get enet-eee clock\n");
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003539 priv->clk_eee = NULL;
3540 }
3541
Doug Berger6be371b2017-03-09 16:58:48 -08003542 /* If this is an internal GPHY, power it on now, before UniMAC is
3543 * brought out of reset as absolutely no UniMAC activity is allowed
3544 */
Jeremy Linton99c6b062020-02-24 16:54:01 -06003545 if (device_get_phy_mode(&pdev->dev) == PHY_INTERFACE_MODE_INTERNAL)
Doug Berger6be371b2017-03-09 16:58:48 -08003546 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3547
Jeremy Linton26bd9cc2020-02-24 16:54:02 -06003548 if ((pd) && (!IS_ERR_OR_NULL(pd->mac_address)))
3549 ether_addr_copy(dev->dev_addr, pd->mac_address);
3550 else
3551 if (!device_get_mac_address(&pdev->dev, dev->dev_addr, ETH_ALEN))
3552 if (has_acpi_companion(&pdev->dev))
3553 bcmgenet_get_hw_addr(priv, dev->dev_addr);
3554
3555 if (!is_valid_ether_addr(dev->dev_addr)) {
3556 dev_warn(&pdev->dev, "using random Ethernet MAC\n");
3557 eth_hw_addr_random(dev);
3558 }
3559
Doug Berger28c2d1a2017-10-25 15:04:13 -07003560 reset_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003561
3562 err = bcmgenet_mii_init(dev);
3563 if (err)
3564 goto err_clk_disable;
3565
3566 /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
3567 * just the ring 16 descriptor based TX
3568 */
3569 netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
3570 netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
3571
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07003572 /* Set default coalescing parameters */
3573 for (i = 0; i < priv->hw_params->rx_queues; i++)
3574 priv->rx_rings[i].rx_max_coalesced_frames = 1;
3575 priv->rx_rings[DESC_INDEX].rx_max_coalesced_frames = 1;
3576
Florian Fainelli219575e2014-06-26 10:26:21 -07003577 /* libphy will determine the link state */
3578 netif_carrier_off(dev);
3579
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003580 /* Turn off the main clock, WOL clock is handled separately */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003581 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003582
Florian Fainelli0f50ce92014-06-26 10:26:20 -07003583 err = register_netdev(dev);
3584 if (err)
3585 goto err;
3586
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003587 return err;
3588
3589err_clk_disable:
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003590 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003591err:
3592 free_netdev(dev);
3593 return err;
3594}
3595
3596static int bcmgenet_remove(struct platform_device *pdev)
3597{
3598 struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
3599
3600 dev_set_drvdata(&pdev->dev, NULL);
3601 unregister_netdev(priv->dev);
3602 bcmgenet_mii_exit(priv->dev);
3603 free_netdev(priv->dev);
3604
3605 return 0;
3606}
3607
Florian Fainellid9f45ab2019-10-15 10:36:24 -07003608static void bcmgenet_shutdown(struct platform_device *pdev)
3609{
3610 bcmgenet_remove(pdev);
3611}
3612
Florian Fainellib6e978e2014-07-21 15:29:22 -07003613#ifdef CONFIG_PM_SLEEP
Florian Fainellib6e978e2014-07-21 15:29:22 -07003614static int bcmgenet_resume(struct device *d)
3615{
3616 struct net_device *dev = dev_get_drvdata(d);
3617 struct bcmgenet_priv *priv = netdev_priv(dev);
3618 unsigned long dma_ctrl;
3619 int ret;
3620 u32 reg;
3621
3622 if (!netif_running(dev))
3623 return 0;
3624
3625 /* Turn on the clock */
3626 ret = clk_prepare_enable(priv->clk);
3627 if (ret)
3628 return ret;
3629
Florian Fainellia6f31f52015-03-23 15:09:57 -07003630 /* If this is an internal GPHY, power it back on now, before UniMAC is
3631 * brought out of reset as absolutely no UniMAC activity is allowed
3632 */
Florian Fainellic624f892015-07-16 15:51:17 -07003633 if (priv->internal_phy)
Florian Fainellia6f31f52015-03-23 15:09:57 -07003634 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3635
Florian Fainellib6e978e2014-07-21 15:29:22 -07003636 bcmgenet_umac_reset(priv);
3637
Doug Berger28c2d1a2017-10-25 15:04:13 -07003638 init_umac(priv);
Florian Fainellib6e978e2014-07-21 15:29:22 -07003639
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003640 /* From WOL-enabled suspend, switch to regular clock */
3641 if (priv->wolopts)
3642 clk_disable_unprepare(priv->clk_wol);
3643
Doug Berger6b6d017f2019-11-05 11:07:25 -08003644 phy_init_hw(dev->phydev);
3645
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003646 /* Speed settings must be restored */
Doug Berger0686bd92019-11-05 11:07:26 -08003647 genphy_config_aneg(dev->phydev);
Florian Fainelli00d51092017-07-31 11:05:32 -07003648 bcmgenet_mii_config(priv->dev, false);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003649
Doug Berger206f54b2019-12-17 16:51:12 -08003650 /* Restore enabled features */
3651 bcmgenet_set_features(dev, dev->features);
3652
Florian Fainellib6e978e2014-07-21 15:29:22 -07003653 bcmgenet_set_hw_addr(priv, dev->dev_addr);
3654
Florian Fainellic624f892015-07-16 15:51:17 -07003655 if (priv->internal_phy) {
Florian Fainellib6e978e2014-07-21 15:29:22 -07003656 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
3657 reg |= EXT_ENERGY_DET_MASK;
3658 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
3659 }
3660
Florian Fainelli98bb7392014-08-11 14:50:45 -07003661 if (priv->wolopts)
3662 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
3663
Florian Fainellib6e978e2014-07-21 15:29:22 -07003664 /* Disable RX/TX DMA and flush TX queues */
3665 dma_ctrl = bcmgenet_dma_disable(priv);
3666
3667 /* Reinitialize TDMA and RDMA and SW housekeeping */
3668 ret = bcmgenet_init_dma(priv);
3669 if (ret) {
3670 netdev_err(dev, "failed to initialize DMA\n");
3671 goto out_clk_disable;
3672 }
3673
3674 /* Always enable ring 16 - descriptor ring */
3675 bcmgenet_enable_dma(priv, dma_ctrl);
3676
Florian Fainelli5371bbf42017-03-15 12:57:21 -07003677 if (!device_may_wakeup(d))
Doug Berger6c97f012017-10-25 15:04:19 -07003678 phy_resume(dev->phydev);
Florian Fainellicc013fb2014-08-11 14:50:43 -07003679
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003680 if (priv->eee.eee_enabled)
3681 bcmgenet_eee_enable_set(dev, true);
3682
Florian Fainellib6e978e2014-07-21 15:29:22 -07003683 bcmgenet_netif_start(dev);
3684
Doug Berger09e805d2018-11-01 15:55:37 -07003685 netif_device_attach(dev);
3686
Florian Fainellib6e978e2014-07-21 15:29:22 -07003687 return 0;
3688
3689out_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08003690 if (priv->internal_phy)
3691 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainellib6e978e2014-07-21 15:29:22 -07003692 clk_disable_unprepare(priv->clk);
3693 return ret;
3694}
Doug Bergera94cbf02018-11-16 18:00:21 -08003695
3696static int bcmgenet_suspend(struct device *d)
3697{
3698 struct net_device *dev = dev_get_drvdata(d);
3699 struct bcmgenet_priv *priv = netdev_priv(dev);
3700 int ret = 0;
3701
3702 if (!netif_running(dev))
3703 return 0;
3704
3705 netif_device_detach(dev);
3706
3707 bcmgenet_netif_stop(dev);
3708
3709 if (!device_may_wakeup(d))
3710 phy_suspend(dev->phydev);
3711
3712 /* Prepare the device for Wake-on-LAN and switch to the slow clock */
3713 if (device_may_wakeup(d) && priv->wolopts) {
3714 ret = bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
3715 clk_prepare_enable(priv->clk_wol);
3716 } else if (priv->internal_phy) {
3717 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
3718 }
3719
3720 /* Turn off the clocks */
3721 clk_disable_unprepare(priv->clk);
3722
Doug Bergerc5a54bb2018-11-16 18:00:22 -08003723 if (ret)
3724 bcmgenet_resume(d);
3725
Doug Bergera94cbf02018-11-16 18:00:21 -08003726 return ret;
3727}
Florian Fainellib6e978e2014-07-21 15:29:22 -07003728#endif /* CONFIG_PM_SLEEP */
3729
3730static SIMPLE_DEV_PM_OPS(bcmgenet_pm_ops, bcmgenet_suspend, bcmgenet_resume);
3731
Jeremy Linton99c6b062020-02-24 16:54:01 -06003732static const struct acpi_device_id genet_acpi_match[] = {
3733 { "BCM6E4E", (kernel_ulong_t)&bcm2711_plat_data },
3734 { },
3735};
3736MODULE_DEVICE_TABLE(acpi, genet_acpi_match);
3737
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003738static struct platform_driver bcmgenet_driver = {
3739 .probe = bcmgenet_probe,
3740 .remove = bcmgenet_remove,
Florian Fainellid9f45ab2019-10-15 10:36:24 -07003741 .shutdown = bcmgenet_shutdown,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003742 .driver = {
3743 .name = "bcmgenet",
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003744 .of_match_table = bcmgenet_match,
Florian Fainellib6e978e2014-07-21 15:29:22 -07003745 .pm = &bcmgenet_pm_ops,
Jeremy Linton99c6b062020-02-24 16:54:01 -06003746 .acpi_match_table = ACPI_PTR(genet_acpi_match),
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003747 },
3748};
3749module_platform_driver(bcmgenet_driver);
3750
3751MODULE_AUTHOR("Broadcom Corporation");
3752MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
3753MODULE_ALIAS("platform:bcmgenet");
3754MODULE_LICENSE("GPL");