blob: e471b14fc6e98c9c513162e2d828ccc9fa00845f [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 Berger1a1d5102020-04-29 13:02:02 -07005 * Copyright (c) 2014-2020 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 <net/arp.h>
27
28#include <linux/mii.h>
29#include <linux/ethtool.h>
30#include <linux/netdevice.h>
31#include <linux/inetdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/skbuff.h>
34#include <linux/in.h>
35#include <linux/ip.h>
36#include <linux/ipv6.h>
37#include <linux/phy.h>
Petri Gyntherb0ba5122014-12-01 16:18:08 -080038#include <linux/platform_data/bcmgenet.h>
Florian Fainelli1c1008c2014-02-13 16:08:47 -080039
40#include <asm/unaligned.h>
41
42#include "bcmgenet.h"
43
44/* Maximum number of hardware queues, downsized if needed */
45#define GENET_MAX_MQ_CNT 4
46
47/* Default highest priority queue for multi queue support */
48#define GENET_Q0_PRIORITY 0
49
Petri Gynther3feafa02015-03-05 17:40:14 -080050#define GENET_Q16_RX_BD_CNT \
51 (TOTAL_DESC - priv->hw_params->rx_queues * priv->hw_params->rx_bds_per_q)
Petri Gynther51a966a2015-02-23 11:00:46 -080052#define GENET_Q16_TX_BD_CNT \
53 (TOTAL_DESC - priv->hw_params->tx_queues * priv->hw_params->tx_bds_per_q)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080054
55#define RX_BUF_LENGTH 2048
56#define SKB_ALIGNMENT 32
57
58/* Tx/Rx DMA register offset, skip 256 descriptors */
59#define WORDS_PER_BD(p) (p->hw_params->words_per_bd)
60#define DMA_DESC_SIZE (WORDS_PER_BD(priv) * sizeof(u32))
61
62#define GENET_TDMA_REG_OFF (priv->hw_params->tdma_offset + \
63 TOTAL_DESC * DMA_DESC_SIZE)
64
65#define GENET_RDMA_REG_OFF (priv->hw_params->rdma_offset + \
66 TOTAL_DESC * DMA_DESC_SIZE)
67
Doug Berger72f96342020-04-29 13:02:00 -070068/* Forward declarations */
69static void bcmgenet_set_rx_mode(struct net_device *dev);
70
Florian Fainelli69d2ea92017-08-29 12:25:31 -070071static inline void bcmgenet_writel(u32 value, void __iomem *offset)
72{
73 /* MIPS chips strapped for BE will automagically configure the
74 * peripheral registers for CPU-native byte order.
75 */
76 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
77 __raw_writel(value, offset);
78 else
79 writel_relaxed(value, offset);
80}
81
82static inline u32 bcmgenet_readl(void __iomem *offset)
83{
84 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
85 return __raw_readl(offset);
86 else
87 return readl_relaxed(offset);
88}
89
Florian Fainelli1c1008c2014-02-13 16:08:47 -080090static inline void dmadesc_set_length_status(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -070091 void __iomem *d, u32 value)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080092{
Florian Fainelli69d2ea92017-08-29 12:25:31 -070093 bcmgenet_writel(value, d + DMA_DESC_LENGTH_STATUS);
Florian Fainelli1c1008c2014-02-13 16:08:47 -080094}
95
Florian Fainelli1c1008c2014-02-13 16:08:47 -080096static inline void dmadesc_set_addr(struct bcmgenet_priv *priv,
97 void __iomem *d,
98 dma_addr_t addr)
99{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700100 bcmgenet_writel(lower_32_bits(addr), d + DMA_DESC_ADDRESS_LO);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800101
102 /* Register writes to GISB bus can take couple hundred nanoseconds
103 * and are done for each packet, save these expensive writes unless
Brian Norris7fc527f2014-07-29 14:34:14 -0700104 * the platform is explicitly configured for 64-bits/LPAE.
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800105 */
106#ifdef CONFIG_PHYS_ADDR_T_64BIT
107 if (priv->hw_params->flags & GENET_HAS_40BITS)
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700108 bcmgenet_writel(upper_32_bits(addr), d + DMA_DESC_ADDRESS_HI);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800109#endif
110}
111
112/* Combined address + length/status setter */
113static inline void dmadesc_set(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700114 void __iomem *d, dma_addr_t addr, u32 val)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800115{
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800116 dmadesc_set_addr(priv, d, addr);
Petri Gynther7ee40622016-04-05 14:00:01 -0700117 dmadesc_set_length_status(priv, d, val);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800118}
119
120static inline dma_addr_t dmadesc_get_addr(struct bcmgenet_priv *priv,
121 void __iomem *d)
122{
123 dma_addr_t addr;
124
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700125 addr = bcmgenet_readl(d + DMA_DESC_ADDRESS_LO);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800126
127 /* Register writes to GISB bus can take couple hundred nanoseconds
128 * and are done for each packet, save these expensive writes unless
Brian Norris7fc527f2014-07-29 14:34:14 -0700129 * the platform is explicitly configured for 64-bits/LPAE.
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800130 */
131#ifdef CONFIG_PHYS_ADDR_T_64BIT
132 if (priv->hw_params->flags & GENET_HAS_40BITS)
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700133 addr |= (u64)bcmgenet_readl(d + DMA_DESC_ADDRESS_HI) << 32;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800134#endif
135 return addr;
136}
137
138#define GENET_VER_FMT "%1d.%1d EPHY: 0x%04x"
139
140#define GENET_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
141 NETIF_MSG_LINK)
142
143static inline u32 bcmgenet_rbuf_ctrl_get(struct bcmgenet_priv *priv)
144{
145 if (GENET_IS_V1(priv))
146 return bcmgenet_rbuf_readl(priv, RBUF_FLUSH_CTRL_V1);
147 else
148 return bcmgenet_sys_readl(priv, SYS_RBUF_FLUSH_CTRL);
149}
150
151static inline void bcmgenet_rbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
152{
153 if (GENET_IS_V1(priv))
154 bcmgenet_rbuf_writel(priv, val, RBUF_FLUSH_CTRL_V1);
155 else
156 bcmgenet_sys_writel(priv, val, SYS_RBUF_FLUSH_CTRL);
157}
158
159/* These macros are defined to deal with register map change
160 * between GENET1.1 and GENET2. Only those currently being used
161 * by driver are defined.
162 */
163static inline u32 bcmgenet_tbuf_ctrl_get(struct bcmgenet_priv *priv)
164{
165 if (GENET_IS_V1(priv))
166 return bcmgenet_rbuf_readl(priv, TBUF_CTRL_V1);
167 else
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700168 return bcmgenet_readl(priv->base +
169 priv->hw_params->tbuf_offset + TBUF_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800170}
171
172static inline void bcmgenet_tbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
173{
174 if (GENET_IS_V1(priv))
175 bcmgenet_rbuf_writel(priv, val, TBUF_CTRL_V1);
176 else
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700177 bcmgenet_writel(val, priv->base +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800178 priv->hw_params->tbuf_offset + TBUF_CTRL);
179}
180
181static inline u32 bcmgenet_bp_mc_get(struct bcmgenet_priv *priv)
182{
183 if (GENET_IS_V1(priv))
184 return bcmgenet_rbuf_readl(priv, TBUF_BP_MC_V1);
185 else
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700186 return bcmgenet_readl(priv->base +
187 priv->hw_params->tbuf_offset + TBUF_BP_MC);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800188}
189
190static inline void bcmgenet_bp_mc_set(struct bcmgenet_priv *priv, u32 val)
191{
192 if (GENET_IS_V1(priv))
193 bcmgenet_rbuf_writel(priv, val, TBUF_BP_MC_V1);
194 else
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700195 bcmgenet_writel(val, priv->base +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800196 priv->hw_params->tbuf_offset + TBUF_BP_MC);
197}
198
199/* RX/TX DMA register accessors */
200enum dma_reg {
201 DMA_RING_CFG = 0,
202 DMA_CTRL,
203 DMA_STATUS,
204 DMA_SCB_BURST_SIZE,
205 DMA_ARB_CTRL,
Petri Gynther37742162014-10-07 09:30:01 -0700206 DMA_PRIORITY_0,
207 DMA_PRIORITY_1,
208 DMA_PRIORITY_2,
Petri Gynther0034de42015-03-13 14:45:00 -0700209 DMA_INDEX2RING_0,
210 DMA_INDEX2RING_1,
211 DMA_INDEX2RING_2,
212 DMA_INDEX2RING_3,
213 DMA_INDEX2RING_4,
214 DMA_INDEX2RING_5,
215 DMA_INDEX2RING_6,
216 DMA_INDEX2RING_7,
Florian Fainelli4a296452015-09-16 16:47:40 -0700217 DMA_RING0_TIMEOUT,
218 DMA_RING1_TIMEOUT,
219 DMA_RING2_TIMEOUT,
220 DMA_RING3_TIMEOUT,
221 DMA_RING4_TIMEOUT,
222 DMA_RING5_TIMEOUT,
223 DMA_RING6_TIMEOUT,
224 DMA_RING7_TIMEOUT,
225 DMA_RING8_TIMEOUT,
226 DMA_RING9_TIMEOUT,
227 DMA_RING10_TIMEOUT,
228 DMA_RING11_TIMEOUT,
229 DMA_RING12_TIMEOUT,
230 DMA_RING13_TIMEOUT,
231 DMA_RING14_TIMEOUT,
232 DMA_RING15_TIMEOUT,
233 DMA_RING16_TIMEOUT,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800234};
235
236static const u8 bcmgenet_dma_regs_v3plus[] = {
237 [DMA_RING_CFG] = 0x00,
238 [DMA_CTRL] = 0x04,
239 [DMA_STATUS] = 0x08,
240 [DMA_SCB_BURST_SIZE] = 0x0C,
241 [DMA_ARB_CTRL] = 0x2C,
Petri Gynther37742162014-10-07 09:30:01 -0700242 [DMA_PRIORITY_0] = 0x30,
243 [DMA_PRIORITY_1] = 0x34,
244 [DMA_PRIORITY_2] = 0x38,
Florian Fainelli4a296452015-09-16 16:47:40 -0700245 [DMA_RING0_TIMEOUT] = 0x2C,
246 [DMA_RING1_TIMEOUT] = 0x30,
247 [DMA_RING2_TIMEOUT] = 0x34,
248 [DMA_RING3_TIMEOUT] = 0x38,
249 [DMA_RING4_TIMEOUT] = 0x3c,
250 [DMA_RING5_TIMEOUT] = 0x40,
251 [DMA_RING6_TIMEOUT] = 0x44,
252 [DMA_RING7_TIMEOUT] = 0x48,
253 [DMA_RING8_TIMEOUT] = 0x4c,
254 [DMA_RING9_TIMEOUT] = 0x50,
255 [DMA_RING10_TIMEOUT] = 0x54,
256 [DMA_RING11_TIMEOUT] = 0x58,
257 [DMA_RING12_TIMEOUT] = 0x5c,
258 [DMA_RING13_TIMEOUT] = 0x60,
259 [DMA_RING14_TIMEOUT] = 0x64,
260 [DMA_RING15_TIMEOUT] = 0x68,
261 [DMA_RING16_TIMEOUT] = 0x6C,
Petri Gynther0034de42015-03-13 14:45:00 -0700262 [DMA_INDEX2RING_0] = 0x70,
263 [DMA_INDEX2RING_1] = 0x74,
264 [DMA_INDEX2RING_2] = 0x78,
265 [DMA_INDEX2RING_3] = 0x7C,
266 [DMA_INDEX2RING_4] = 0x80,
267 [DMA_INDEX2RING_5] = 0x84,
268 [DMA_INDEX2RING_6] = 0x88,
269 [DMA_INDEX2RING_7] = 0x8C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800270};
271
272static const u8 bcmgenet_dma_regs_v2[] = {
273 [DMA_RING_CFG] = 0x00,
274 [DMA_CTRL] = 0x04,
275 [DMA_STATUS] = 0x08,
276 [DMA_SCB_BURST_SIZE] = 0x0C,
277 [DMA_ARB_CTRL] = 0x30,
Petri Gynther37742162014-10-07 09:30:01 -0700278 [DMA_PRIORITY_0] = 0x34,
279 [DMA_PRIORITY_1] = 0x38,
280 [DMA_PRIORITY_2] = 0x3C,
Florian Fainelli4a296452015-09-16 16:47:40 -0700281 [DMA_RING0_TIMEOUT] = 0x2C,
282 [DMA_RING1_TIMEOUT] = 0x30,
283 [DMA_RING2_TIMEOUT] = 0x34,
284 [DMA_RING3_TIMEOUT] = 0x38,
285 [DMA_RING4_TIMEOUT] = 0x3c,
286 [DMA_RING5_TIMEOUT] = 0x40,
287 [DMA_RING6_TIMEOUT] = 0x44,
288 [DMA_RING7_TIMEOUT] = 0x48,
289 [DMA_RING8_TIMEOUT] = 0x4c,
290 [DMA_RING9_TIMEOUT] = 0x50,
291 [DMA_RING10_TIMEOUT] = 0x54,
292 [DMA_RING11_TIMEOUT] = 0x58,
293 [DMA_RING12_TIMEOUT] = 0x5c,
294 [DMA_RING13_TIMEOUT] = 0x60,
295 [DMA_RING14_TIMEOUT] = 0x64,
296 [DMA_RING15_TIMEOUT] = 0x68,
297 [DMA_RING16_TIMEOUT] = 0x6C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800298};
299
300static const u8 bcmgenet_dma_regs_v1[] = {
301 [DMA_CTRL] = 0x00,
302 [DMA_STATUS] = 0x04,
303 [DMA_SCB_BURST_SIZE] = 0x0C,
304 [DMA_ARB_CTRL] = 0x30,
Petri Gynther37742162014-10-07 09:30:01 -0700305 [DMA_PRIORITY_0] = 0x34,
306 [DMA_PRIORITY_1] = 0x38,
307 [DMA_PRIORITY_2] = 0x3C,
Florian Fainelli4a296452015-09-16 16:47:40 -0700308 [DMA_RING0_TIMEOUT] = 0x2C,
309 [DMA_RING1_TIMEOUT] = 0x30,
310 [DMA_RING2_TIMEOUT] = 0x34,
311 [DMA_RING3_TIMEOUT] = 0x38,
312 [DMA_RING4_TIMEOUT] = 0x3c,
313 [DMA_RING5_TIMEOUT] = 0x40,
314 [DMA_RING6_TIMEOUT] = 0x44,
315 [DMA_RING7_TIMEOUT] = 0x48,
316 [DMA_RING8_TIMEOUT] = 0x4c,
317 [DMA_RING9_TIMEOUT] = 0x50,
318 [DMA_RING10_TIMEOUT] = 0x54,
319 [DMA_RING11_TIMEOUT] = 0x58,
320 [DMA_RING12_TIMEOUT] = 0x5c,
321 [DMA_RING13_TIMEOUT] = 0x60,
322 [DMA_RING14_TIMEOUT] = 0x64,
323 [DMA_RING15_TIMEOUT] = 0x68,
324 [DMA_RING16_TIMEOUT] = 0x6C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800325};
326
327/* Set at runtime once bcmgenet version is known */
328static const u8 *bcmgenet_dma_regs;
329
330static inline struct bcmgenet_priv *dev_to_priv(struct device *dev)
331{
332 return netdev_priv(dev_get_drvdata(dev));
333}
334
335static inline u32 bcmgenet_tdma_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700336 enum dma_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800337{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700338 return bcmgenet_readl(priv->base + GENET_TDMA_REG_OFF +
339 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800340}
341
342static inline void bcmgenet_tdma_writel(struct bcmgenet_priv *priv,
343 u32 val, enum dma_reg r)
344{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700345 bcmgenet_writel(val, priv->base + GENET_TDMA_REG_OFF +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800346 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
347}
348
349static inline u32 bcmgenet_rdma_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700350 enum dma_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800351{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700352 return bcmgenet_readl(priv->base + GENET_RDMA_REG_OFF +
353 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800354}
355
356static inline void bcmgenet_rdma_writel(struct bcmgenet_priv *priv,
357 u32 val, enum dma_reg r)
358{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700359 bcmgenet_writel(val, priv->base + GENET_RDMA_REG_OFF +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800360 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
361}
362
363/* RDMA/TDMA ring registers and accessors
364 * we merge the common fields and just prefix with T/D the registers
365 * having different meaning depending on the direction
366 */
367enum dma_ring_reg {
368 TDMA_READ_PTR = 0,
369 RDMA_WRITE_PTR = TDMA_READ_PTR,
370 TDMA_READ_PTR_HI,
371 RDMA_WRITE_PTR_HI = TDMA_READ_PTR_HI,
372 TDMA_CONS_INDEX,
373 RDMA_PROD_INDEX = TDMA_CONS_INDEX,
374 TDMA_PROD_INDEX,
375 RDMA_CONS_INDEX = TDMA_PROD_INDEX,
376 DMA_RING_BUF_SIZE,
377 DMA_START_ADDR,
378 DMA_START_ADDR_HI,
379 DMA_END_ADDR,
380 DMA_END_ADDR_HI,
381 DMA_MBUF_DONE_THRESH,
382 TDMA_FLOW_PERIOD,
383 RDMA_XON_XOFF_THRESH = TDMA_FLOW_PERIOD,
384 TDMA_WRITE_PTR,
385 RDMA_READ_PTR = TDMA_WRITE_PTR,
386 TDMA_WRITE_PTR_HI,
387 RDMA_READ_PTR_HI = TDMA_WRITE_PTR_HI
388};
389
390/* GENET v4 supports 40-bits pointer addressing
391 * for obvious reasons the LO and HI word parts
392 * are contiguous, but this offsets the other
393 * registers.
394 */
395static const u8 genet_dma_ring_regs_v4[] = {
396 [TDMA_READ_PTR] = 0x00,
397 [TDMA_READ_PTR_HI] = 0x04,
398 [TDMA_CONS_INDEX] = 0x08,
399 [TDMA_PROD_INDEX] = 0x0C,
400 [DMA_RING_BUF_SIZE] = 0x10,
401 [DMA_START_ADDR] = 0x14,
402 [DMA_START_ADDR_HI] = 0x18,
403 [DMA_END_ADDR] = 0x1C,
404 [DMA_END_ADDR_HI] = 0x20,
405 [DMA_MBUF_DONE_THRESH] = 0x24,
406 [TDMA_FLOW_PERIOD] = 0x28,
407 [TDMA_WRITE_PTR] = 0x2C,
408 [TDMA_WRITE_PTR_HI] = 0x30,
409};
410
411static const u8 genet_dma_ring_regs_v123[] = {
412 [TDMA_READ_PTR] = 0x00,
413 [TDMA_CONS_INDEX] = 0x04,
414 [TDMA_PROD_INDEX] = 0x08,
415 [DMA_RING_BUF_SIZE] = 0x0C,
416 [DMA_START_ADDR] = 0x10,
417 [DMA_END_ADDR] = 0x14,
418 [DMA_MBUF_DONE_THRESH] = 0x18,
419 [TDMA_FLOW_PERIOD] = 0x1C,
420 [TDMA_WRITE_PTR] = 0x20,
421};
422
423/* Set at runtime once GENET version is known */
424static const u8 *genet_dma_ring_regs;
425
426static inline u32 bcmgenet_tdma_ring_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700427 unsigned int ring,
428 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800429{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700430 return bcmgenet_readl(priv->base + GENET_TDMA_REG_OFF +
431 (DMA_RING_SIZE * ring) +
432 genet_dma_ring_regs[r]);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800433}
434
435static inline void bcmgenet_tdma_ring_writel(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700436 unsigned int ring, u32 val,
437 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800438{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700439 bcmgenet_writel(val, priv->base + GENET_TDMA_REG_OFF +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800440 (DMA_RING_SIZE * ring) +
441 genet_dma_ring_regs[r]);
442}
443
444static inline u32 bcmgenet_rdma_ring_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700445 unsigned int ring,
446 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800447{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700448 return bcmgenet_readl(priv->base + GENET_RDMA_REG_OFF +
449 (DMA_RING_SIZE * ring) +
450 genet_dma_ring_regs[r]);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800451}
452
453static inline void bcmgenet_rdma_ring_writel(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700454 unsigned int ring, u32 val,
455 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800456{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700457 bcmgenet_writel(val, priv->base + GENET_RDMA_REG_OFF +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800458 (DMA_RING_SIZE * ring) +
459 genet_dma_ring_regs[r]);
460}
461
Doug Berger854295d2020-04-29 13:02:04 -0700462static void bcmgenet_hfb_enable_filter(struct bcmgenet_priv *priv, u32 f_index)
463{
464 u32 offset;
465 u32 reg;
466
467 offset = HFB_FLT_ENABLE_V3PLUS + (f_index < 32) * sizeof(u32);
468 reg = bcmgenet_hfb_reg_readl(priv, offset);
469 reg |= (1 << (f_index % 32));
470 bcmgenet_hfb_reg_writel(priv, reg, offset);
Doug Berger3e370952020-04-29 13:02:05 -0700471 reg = bcmgenet_hfb_reg_readl(priv, HFB_CTRL);
472 reg |= RBUF_HFB_EN;
473 bcmgenet_hfb_reg_writel(priv, reg, HFB_CTRL);
474}
475
476static void bcmgenet_hfb_disable_filter(struct bcmgenet_priv *priv, u32 f_index)
477{
478 u32 offset, reg, reg1;
479
480 offset = HFB_FLT_ENABLE_V3PLUS;
481 reg = bcmgenet_hfb_reg_readl(priv, offset);
482 reg1 = bcmgenet_hfb_reg_readl(priv, offset + sizeof(u32));
483 if (f_index < 32) {
484 reg1 &= ~(1 << (f_index % 32));
485 bcmgenet_hfb_reg_writel(priv, reg1, offset + sizeof(u32));
486 } else {
487 reg &= ~(1 << (f_index % 32));
488 bcmgenet_hfb_reg_writel(priv, reg, offset);
489 }
490 if (!reg && !reg1) {
491 reg = bcmgenet_hfb_reg_readl(priv, HFB_CTRL);
492 reg &= ~RBUF_HFB_EN;
493 bcmgenet_hfb_reg_writel(priv, reg, HFB_CTRL);
494 }
Doug Berger854295d2020-04-29 13:02:04 -0700495}
496
497static void bcmgenet_hfb_set_filter_rx_queue_mapping(struct bcmgenet_priv *priv,
498 u32 f_index, u32 rx_queue)
499{
500 u32 offset;
501 u32 reg;
502
503 offset = f_index / 8;
504 reg = bcmgenet_rdma_readl(priv, DMA_INDEX2RING_0 + offset);
505 reg &= ~(0xF << (4 * (f_index % 8)));
506 reg |= ((rx_queue & 0xF) << (4 * (f_index % 8)));
507 bcmgenet_rdma_writel(priv, reg, DMA_INDEX2RING_0 + offset);
508}
509
510static void bcmgenet_hfb_set_filter_length(struct bcmgenet_priv *priv,
511 u32 f_index, u32 f_length)
512{
513 u32 offset;
514 u32 reg;
515
516 offset = HFB_FLT_LEN_V3PLUS +
517 ((priv->hw_params->hfb_filter_cnt - 1 - f_index) / 4) *
518 sizeof(u32);
519 reg = bcmgenet_hfb_reg_readl(priv, offset);
520 reg &= ~(0xFF << (8 * (f_index % 4)));
521 reg |= ((f_length & 0xFF) << (8 * (f_index % 4)));
522 bcmgenet_hfb_reg_writel(priv, reg, offset);
523}
524
Doug Berger3e370952020-04-29 13:02:05 -0700525static int bcmgenet_hfb_validate_mask(void *mask, size_t size)
526{
527 while (size) {
528 switch (*(unsigned char *)mask++) {
529 case 0x00:
530 case 0x0f:
531 case 0xf0:
532 case 0xff:
533 size--;
534 continue;
535 default:
536 return -EINVAL;
537 }
538 }
539
540 return 0;
541}
542
543#define VALIDATE_MASK(x) \
544 bcmgenet_hfb_validate_mask(&(x), sizeof(x))
545
Doug Bergera8c64542020-07-16 16:38:17 -0700546static int bcmgenet_hfb_insert_data(struct bcmgenet_priv *priv, u32 f_index,
547 u32 offset, void *val, void *mask,
548 size_t size)
Doug Berger3e370952020-04-29 13:02:05 -0700549{
Doug Bergera8c64542020-07-16 16:38:17 -0700550 u32 index, tmp;
Doug Berger3e370952020-04-29 13:02:05 -0700551
Doug Bergera8c64542020-07-16 16:38:17 -0700552 index = f_index * priv->hw_params->hfb_filter_size + offset / 2;
553 tmp = bcmgenet_hfb_readl(priv, index * sizeof(u32));
Doug Berger3e370952020-04-29 13:02:05 -0700554
555 while (size--) {
556 if (offset++ & 1) {
557 tmp &= ~0x300FF;
558 tmp |= (*(unsigned char *)val++);
559 switch ((*(unsigned char *)mask++)) {
560 case 0xFF:
561 tmp |= 0x30000;
562 break;
563 case 0xF0:
564 tmp |= 0x20000;
565 break;
566 case 0x0F:
567 tmp |= 0x10000;
568 break;
569 }
Doug Bergera8c64542020-07-16 16:38:17 -0700570 bcmgenet_hfb_writel(priv, tmp, index++ * sizeof(u32));
Doug Berger3e370952020-04-29 13:02:05 -0700571 if (size)
Doug Bergera8c64542020-07-16 16:38:17 -0700572 tmp = bcmgenet_hfb_readl(priv,
573 index * sizeof(u32));
Doug Berger3e370952020-04-29 13:02:05 -0700574 } else {
575 tmp &= ~0xCFF00;
576 tmp |= (*(unsigned char *)val++) << 8;
577 switch ((*(unsigned char *)mask++)) {
578 case 0xFF:
579 tmp |= 0xC0000;
580 break;
581 case 0xF0:
582 tmp |= 0x80000;
583 break;
584 case 0x0F:
585 tmp |= 0x40000;
586 break;
587 }
588 if (!size)
Doug Bergera8c64542020-07-16 16:38:17 -0700589 bcmgenet_hfb_writel(priv, tmp, index * sizeof(u32));
Doug Berger3e370952020-04-29 13:02:05 -0700590 }
591 }
592
593 return 0;
594}
595
Doug Bergera8c64542020-07-16 16:38:17 -0700596static void bcmgenet_hfb_create_rxnfc_filter(struct bcmgenet_priv *priv,
597 struct bcmgenet_rxnfc_rule *rule)
Doug Berger3e370952020-04-29 13:02:05 -0700598{
599 struct ethtool_rx_flow_spec *fs = &rule->fs;
Doug Bergera8c64542020-07-16 16:38:17 -0700600 u32 offset = 0, f_length = 0, f;
Doug Berger3e370952020-04-29 13:02:05 -0700601 u8 val_8, mask_8;
Doug Bergerd966d2e2020-06-24 18:14:54 -0700602 __be16 val_16;
603 u16 mask_16;
Doug Berger3e370952020-04-29 13:02:05 -0700604 size_t size;
Doug Berger3e370952020-04-29 13:02:05 -0700605
Doug Bergera8c64542020-07-16 16:38:17 -0700606 f = fs->location;
Doug Berger3e370952020-04-29 13:02:05 -0700607 if (fs->flow_type & FLOW_MAC_EXT) {
Doug Bergera8c64542020-07-16 16:38:17 -0700608 bcmgenet_hfb_insert_data(priv, f, 0,
Doug Berger3e370952020-04-29 13:02:05 -0700609 &fs->h_ext.h_dest, &fs->m_ext.h_dest,
610 sizeof(fs->h_ext.h_dest));
611 }
612
613 if (fs->flow_type & FLOW_EXT) {
614 if (fs->m_ext.vlan_etype ||
615 fs->m_ext.vlan_tci) {
Doug Bergera8c64542020-07-16 16:38:17 -0700616 bcmgenet_hfb_insert_data(priv, f, 12,
Doug Berger3e370952020-04-29 13:02:05 -0700617 &fs->h_ext.vlan_etype,
618 &fs->m_ext.vlan_etype,
619 sizeof(fs->h_ext.vlan_etype));
Doug Bergera8c64542020-07-16 16:38:17 -0700620 bcmgenet_hfb_insert_data(priv, f, 14,
Doug Berger3e370952020-04-29 13:02:05 -0700621 &fs->h_ext.vlan_tci,
622 &fs->m_ext.vlan_tci,
623 sizeof(fs->h_ext.vlan_tci));
624 offset += VLAN_HLEN;
625 f_length += DIV_ROUND_UP(VLAN_HLEN, 2);
626 }
627 }
628
629 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
630 case ETHER_FLOW:
631 f_length += DIV_ROUND_UP(ETH_HLEN, 2);
Doug Bergera8c64542020-07-16 16:38:17 -0700632 bcmgenet_hfb_insert_data(priv, f, 0,
Doug Berger3e370952020-04-29 13:02:05 -0700633 &fs->h_u.ether_spec.h_dest,
634 &fs->m_u.ether_spec.h_dest,
635 sizeof(fs->h_u.ether_spec.h_dest));
Doug Bergera8c64542020-07-16 16:38:17 -0700636 bcmgenet_hfb_insert_data(priv, f, ETH_ALEN,
Doug Berger3e370952020-04-29 13:02:05 -0700637 &fs->h_u.ether_spec.h_source,
638 &fs->m_u.ether_spec.h_source,
639 sizeof(fs->h_u.ether_spec.h_source));
Doug Bergera8c64542020-07-16 16:38:17 -0700640 bcmgenet_hfb_insert_data(priv, f, (2 * ETH_ALEN) + offset,
Doug Berger3e370952020-04-29 13:02:05 -0700641 &fs->h_u.ether_spec.h_proto,
642 &fs->m_u.ether_spec.h_proto,
643 sizeof(fs->h_u.ether_spec.h_proto));
644 break;
645 case IP_USER_FLOW:
646 f_length += DIV_ROUND_UP(ETH_HLEN + 20, 2);
647 /* Specify IP Ether Type */
648 val_16 = htons(ETH_P_IP);
649 mask_16 = 0xFFFF;
Doug Bergera8c64542020-07-16 16:38:17 -0700650 bcmgenet_hfb_insert_data(priv, f, (2 * ETH_ALEN) + offset,
Doug Berger3e370952020-04-29 13:02:05 -0700651 &val_16, &mask_16, sizeof(val_16));
Doug Bergera8c64542020-07-16 16:38:17 -0700652 bcmgenet_hfb_insert_data(priv, f, 15 + offset,
Doug Berger3e370952020-04-29 13:02:05 -0700653 &fs->h_u.usr_ip4_spec.tos,
654 &fs->m_u.usr_ip4_spec.tos,
655 sizeof(fs->h_u.usr_ip4_spec.tos));
Doug Bergera8c64542020-07-16 16:38:17 -0700656 bcmgenet_hfb_insert_data(priv, f, 23 + offset,
Doug Berger3e370952020-04-29 13:02:05 -0700657 &fs->h_u.usr_ip4_spec.proto,
658 &fs->m_u.usr_ip4_spec.proto,
659 sizeof(fs->h_u.usr_ip4_spec.proto));
Doug Bergera8c64542020-07-16 16:38:17 -0700660 bcmgenet_hfb_insert_data(priv, f, 26 + offset,
Doug Berger3e370952020-04-29 13:02:05 -0700661 &fs->h_u.usr_ip4_spec.ip4src,
662 &fs->m_u.usr_ip4_spec.ip4src,
663 sizeof(fs->h_u.usr_ip4_spec.ip4src));
Doug Bergera8c64542020-07-16 16:38:17 -0700664 bcmgenet_hfb_insert_data(priv, f, 30 + offset,
Doug Berger3e370952020-04-29 13:02:05 -0700665 &fs->h_u.usr_ip4_spec.ip4dst,
666 &fs->m_u.usr_ip4_spec.ip4dst,
667 sizeof(fs->h_u.usr_ip4_spec.ip4dst));
668 if (!fs->m_u.usr_ip4_spec.l4_4_bytes)
669 break;
670
671 /* Only supports 20 byte IPv4 header */
672 val_8 = 0x45;
673 mask_8 = 0xFF;
Doug Bergera8c64542020-07-16 16:38:17 -0700674 bcmgenet_hfb_insert_data(priv, f, ETH_HLEN + offset,
Doug Berger3e370952020-04-29 13:02:05 -0700675 &val_8, &mask_8,
676 sizeof(val_8));
677 size = sizeof(fs->h_u.usr_ip4_spec.l4_4_bytes);
Doug Bergera8c64542020-07-16 16:38:17 -0700678 bcmgenet_hfb_insert_data(priv, f,
Doug Berger3e370952020-04-29 13:02:05 -0700679 ETH_HLEN + 20 + offset,
680 &fs->h_u.usr_ip4_spec.l4_4_bytes,
681 &fs->m_u.usr_ip4_spec.l4_4_bytes,
682 size);
683 f_length += DIV_ROUND_UP(size, 2);
684 break;
685 }
686
Doug Bergera8c64542020-07-16 16:38:17 -0700687 bcmgenet_hfb_set_filter_length(priv, f, 2 * f_length);
Doug Bergerf50932c2020-04-29 13:02:06 -0700688 if (!fs->ring_cookie || fs->ring_cookie == RX_CLS_FLOW_WAKE) {
Doug Berger3e370952020-04-29 13:02:05 -0700689 /* Ring 0 flows can be handled by the default Descriptor Ring
690 * We'll map them to ring 0, but don't enable the filter
691 */
Doug Bergera8c64542020-07-16 16:38:17 -0700692 bcmgenet_hfb_set_filter_rx_queue_mapping(priv, f, 0);
Doug Berger3e370952020-04-29 13:02:05 -0700693 rule->state = BCMGENET_RXNFC_STATE_DISABLED;
694 } else {
695 /* Other Rx rings are direct mapped here */
Doug Bergera8c64542020-07-16 16:38:17 -0700696 bcmgenet_hfb_set_filter_rx_queue_mapping(priv, f,
697 fs->ring_cookie);
698 bcmgenet_hfb_enable_filter(priv, f);
Doug Berger3e370952020-04-29 13:02:05 -0700699 rule->state = BCMGENET_RXNFC_STATE_ENABLED;
700 }
Doug Berger3e370952020-04-29 13:02:05 -0700701}
702
Doug Berger854295d2020-04-29 13:02:04 -0700703/* bcmgenet_hfb_clear
704 *
705 * Clear Hardware Filter Block and disable all filtering.
706 */
Doug Bergera8c64542020-07-16 16:38:17 -0700707static void bcmgenet_hfb_clear_filter(struct bcmgenet_priv *priv, u32 f_index)
708{
709 u32 base, i;
710
711 base = f_index * priv->hw_params->hfb_filter_size;
712 for (i = 0; i < priv->hw_params->hfb_filter_size; i++)
713 bcmgenet_hfb_writel(priv, 0x0, (base + i) * sizeof(u32));
714}
715
Doug Berger854295d2020-04-29 13:02:04 -0700716static void bcmgenet_hfb_clear(struct bcmgenet_priv *priv)
717{
718 u32 i;
719
Doug Bergera8c64542020-07-16 16:38:17 -0700720 if (GENET_IS_V1(priv) || GENET_IS_V2(priv))
721 return;
722
Doug Berger854295d2020-04-29 13:02:04 -0700723 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_CTRL);
724 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS);
725 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS + 4);
726
727 for (i = DMA_INDEX2RING_0; i <= DMA_INDEX2RING_7; i++)
728 bcmgenet_rdma_writel(priv, 0x0, i);
729
730 for (i = 0; i < (priv->hw_params->hfb_filter_cnt / 4); i++)
731 bcmgenet_hfb_reg_writel(priv, 0x0,
732 HFB_FLT_LEN_V3PLUS + i * sizeof(u32));
733
Doug Bergera8c64542020-07-16 16:38:17 -0700734 for (i = 0; i < priv->hw_params->hfb_filter_cnt; i++)
735 bcmgenet_hfb_clear_filter(priv, i);
Doug Berger854295d2020-04-29 13:02:04 -0700736}
737
738static void bcmgenet_hfb_init(struct bcmgenet_priv *priv)
739{
Doug Berger3e370952020-04-29 13:02:05 -0700740 int i;
741
Doug Bergera8c64542020-07-16 16:38:17 -0700742 INIT_LIST_HEAD(&priv->rxnfc_list);
Doug Berger854295d2020-04-29 13:02:04 -0700743 if (GENET_IS_V1(priv) || GENET_IS_V2(priv))
744 return;
745
Doug Berger3e370952020-04-29 13:02:05 -0700746 for (i = 0; i < MAX_NUM_OF_FS_RULES; i++) {
747 INIT_LIST_HEAD(&priv->rxnfc_rules[i].list);
748 priv->rxnfc_rules[i].state = BCMGENET_RXNFC_STATE_UNUSED;
749 }
750
Doug Berger854295d2020-04-29 13:02:04 -0700751 bcmgenet_hfb_clear(priv);
752}
753
Edwin Chan89316fa2017-03-09 16:58:49 -0800754static int bcmgenet_begin(struct net_device *dev)
755{
756 struct bcmgenet_priv *priv = netdev_priv(dev);
757
758 /* Turn on the clock */
759 return clk_prepare_enable(priv->clk);
760}
761
762static void bcmgenet_complete(struct net_device *dev)
763{
764 struct bcmgenet_priv *priv = netdev_priv(dev);
765
766 /* Turn off the clock */
767 clk_disable_unprepare(priv->clk);
768}
769
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200770static int bcmgenet_get_link_ksettings(struct net_device *dev,
771 struct ethtool_link_ksettings *cmd)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200772{
773 if (!netif_running(dev))
774 return -EINVAL;
775
Doug Berger6c97f012017-10-25 15:04:19 -0700776 if (!dev->phydev)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200777 return -ENODEV;
778
Doug Berger6c97f012017-10-25 15:04:19 -0700779 phy_ethtool_ksettings_get(dev->phydev, cmd);
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +0300780
781 return 0;
Philippe Reynesbac65c42016-07-09 00:54:47 +0200782}
783
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200784static int bcmgenet_set_link_ksettings(struct net_device *dev,
785 const struct ethtool_link_ksettings *cmd)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200786{
787 if (!netif_running(dev))
788 return -EINVAL;
789
Doug Berger6c97f012017-10-25 15:04:19 -0700790 if (!dev->phydev)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200791 return -ENODEV;
792
Doug Berger6c97f012017-10-25 15:04:19 -0700793 return phy_ethtool_ksettings_set(dev->phydev, cmd);
Philippe Reynesbac65c42016-07-09 00:54:47 +0200794}
795
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800796static int bcmgenet_set_features(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700797 netdev_features_t features)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800798{
Doug Bergerf63db4e2019-12-17 16:51:11 -0800799 struct bcmgenet_priv *priv = netdev_priv(dev);
800 u32 reg;
801 int ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800802
Doug Bergerf63db4e2019-12-17 16:51:11 -0800803 ret = clk_prepare_enable(priv->clk);
804 if (ret)
805 return ret;
806
807 /* Make sure we reflect the value of CRC_CMD_FWD */
808 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
809 priv->crc_fwd_en = !!(reg & CMD_CRC_FWD);
810
Doug Bergerf63db4e2019-12-17 16:51:11 -0800811 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800812
813 return ret;
814}
815
816static u32 bcmgenet_get_msglevel(struct net_device *dev)
817{
818 struct bcmgenet_priv *priv = netdev_priv(dev);
819
820 return priv->msg_enable;
821}
822
823static void bcmgenet_set_msglevel(struct net_device *dev, u32 level)
824{
825 struct bcmgenet_priv *priv = netdev_priv(dev);
826
827 priv->msg_enable = level;
828}
829
Florian Fainelli2f913072015-09-16 16:47:39 -0700830static int bcmgenet_get_coalesce(struct net_device *dev,
831 struct ethtool_coalesce *ec)
832{
833 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700834 struct bcmgenet_rx_ring *ring;
835 unsigned int i;
Florian Fainelli2f913072015-09-16 16:47:39 -0700836
837 ec->tx_max_coalesced_frames =
838 bcmgenet_tdma_ring_readl(priv, DESC_INDEX,
839 DMA_MBUF_DONE_THRESH);
Florian Fainelli4a296452015-09-16 16:47:40 -0700840 ec->rx_max_coalesced_frames =
841 bcmgenet_rdma_ring_readl(priv, DESC_INDEX,
842 DMA_MBUF_DONE_THRESH);
843 ec->rx_coalesce_usecs =
844 bcmgenet_rdma_readl(priv, DMA_RING16_TIMEOUT) * 8192 / 1000;
Florian Fainelli2f913072015-09-16 16:47:39 -0700845
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700846 for (i = 0; i < priv->hw_params->rx_queues; i++) {
847 ring = &priv->rx_rings[i];
848 ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
849 }
850 ring = &priv->rx_rings[DESC_INDEX];
851 ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
852
Florian Fainelli2f913072015-09-16 16:47:39 -0700853 return 0;
854}
855
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700856static void bcmgenet_set_rx_coalesce(struct bcmgenet_rx_ring *ring,
857 u32 usecs, u32 pkts)
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700858{
859 struct bcmgenet_priv *priv = ring->priv;
860 unsigned int i = ring->index;
861 u32 reg;
862
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700863 bcmgenet_rdma_ring_writel(priv, i, pkts, DMA_MBUF_DONE_THRESH);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700864
865 reg = bcmgenet_rdma_readl(priv, DMA_RING0_TIMEOUT + i);
866 reg &= ~DMA_TIMEOUT_MASK;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700867 reg |= DIV_ROUND_UP(usecs * 1000, 8192);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700868 bcmgenet_rdma_writel(priv, reg, DMA_RING0_TIMEOUT + i);
869}
870
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700871static void bcmgenet_set_ring_rx_coalesce(struct bcmgenet_rx_ring *ring,
872 struct ethtool_coalesce *ec)
873{
Tal Gilboa8960b382019-01-31 16:44:48 +0200874 struct dim_cq_moder moder;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700875 u32 usecs, pkts;
876
877 ring->rx_coalesce_usecs = ec->rx_coalesce_usecs;
878 ring->rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
879 usecs = ring->rx_coalesce_usecs;
880 pkts = ring->rx_max_coalesced_frames;
881
882 if (ec->use_adaptive_rx_coalesce && !ring->dim.use_dim) {
Tal Gilboa026a8072018-04-24 13:36:01 +0300883 moder = net_dim_get_def_rx_moderation(ring->dim.dim.mode);
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700884 usecs = moder.usec;
885 pkts = moder.pkts;
886 }
887
888 ring->dim.use_dim = ec->use_adaptive_rx_coalesce;
889 bcmgenet_set_rx_coalesce(ring, usecs, pkts);
890}
891
Florian Fainelli2f913072015-09-16 16:47:39 -0700892static int bcmgenet_set_coalesce(struct net_device *dev,
893 struct ethtool_coalesce *ec)
894{
895 struct bcmgenet_priv *priv = netdev_priv(dev);
896 unsigned int i;
897
Florian Fainelli4a296452015-09-16 16:47:40 -0700898 /* Base system clock is 125Mhz, DMA timeout is this reference clock
899 * divided by 1024, which yields roughly 8.192us, our maximum value
900 * has to fit in the DMA_TIMEOUT_MASK (16 bits)
901 */
Florian Fainelli2f913072015-09-16 16:47:39 -0700902 if (ec->tx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
Florian Fainelli4a296452015-09-16 16:47:40 -0700903 ec->tx_max_coalesced_frames == 0 ||
904 ec->rx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
905 ec->rx_coalesce_usecs > (DMA_TIMEOUT_MASK * 8) + 1)
906 return -EINVAL;
907
908 if (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0)
Florian Fainelli2f913072015-09-16 16:47:39 -0700909 return -EINVAL;
910
911 /* GENET TDMA hardware does not support a configurable timeout, but will
912 * always generate an interrupt either after MBDONE packets have been
Doug Berger556c2cf2017-03-13 17:41:34 -0700913 * transmitted, or when the ring is empty.
Florian Fainelli2f913072015-09-16 16:47:39 -0700914 */
Florian Fainelli2f913072015-09-16 16:47:39 -0700915
916 /* Program all TX queues with the same values, as there is no
917 * ethtool knob to do coalescing on a per-queue basis
918 */
919 for (i = 0; i < priv->hw_params->tx_queues; i++)
920 bcmgenet_tdma_ring_writel(priv, i,
921 ec->tx_max_coalesced_frames,
922 DMA_MBUF_DONE_THRESH);
923 bcmgenet_tdma_ring_writel(priv, DESC_INDEX,
924 ec->tx_max_coalesced_frames,
925 DMA_MBUF_DONE_THRESH);
926
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700927 for (i = 0; i < priv->hw_params->rx_queues; i++)
928 bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[i], ec);
929 bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[DESC_INDEX], ec);
Florian Fainelli4a296452015-09-16 16:47:40 -0700930
Florian Fainelli2f913072015-09-16 16:47:39 -0700931 return 0;
932}
933
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800934/* standard ethtool support functions. */
935enum bcmgenet_stat_type {
936 BCMGENET_STAT_NETDEV = -1,
937 BCMGENET_STAT_MIB_RX,
938 BCMGENET_STAT_MIB_TX,
939 BCMGENET_STAT_RUNT,
940 BCMGENET_STAT_MISC,
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800941 BCMGENET_STAT_SOFT,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800942};
943
944struct bcmgenet_stats {
945 char stat_string[ETH_GSTRING_LEN];
946 int stat_sizeof;
947 int stat_offset;
948 enum bcmgenet_stat_type type;
949 /* reg offset from UMAC base for misc counters */
950 u16 reg_offset;
951};
952
953#define STAT_NETDEV(m) { \
954 .stat_string = __stringify(m), \
955 .stat_sizeof = sizeof(((struct net_device_stats *)0)->m), \
956 .stat_offset = offsetof(struct net_device_stats, m), \
957 .type = BCMGENET_STAT_NETDEV, \
958}
959
960#define STAT_GENET_MIB(str, m, _type) { \
961 .stat_string = str, \
962 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
963 .stat_offset = offsetof(struct bcmgenet_priv, m), \
964 .type = _type, \
965}
966
967#define STAT_GENET_MIB_RX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_RX)
968#define STAT_GENET_MIB_TX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_TX)
969#define STAT_GENET_RUNT(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_RUNT)
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800970#define STAT_GENET_SOFT_MIB(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_SOFT)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800971
972#define STAT_GENET_MISC(str, m, offset) { \
973 .stat_string = str, \
974 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
975 .stat_offset = offsetof(struct bcmgenet_priv, m), \
976 .type = BCMGENET_STAT_MISC, \
977 .reg_offset = offset, \
978}
979
Florian Fainelli37a30b42017-03-16 10:27:08 -0700980#define STAT_GENET_Q(num) \
981 STAT_GENET_SOFT_MIB("txq" __stringify(num) "_packets", \
982 tx_rings[num].packets), \
983 STAT_GENET_SOFT_MIB("txq" __stringify(num) "_bytes", \
984 tx_rings[num].bytes), \
985 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_bytes", \
986 rx_rings[num].bytes), \
987 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_packets", \
988 rx_rings[num].packets), \
989 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_errors", \
990 rx_rings[num].errors), \
991 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_dropped", \
992 rx_rings[num].dropped)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800993
994/* There is a 0xC gap between the end of RX and beginning of TX stats and then
995 * between the end of TX stats and the beginning of the RX RUNT
996 */
997#define BCMGENET_STAT_OFFSET 0xc
998
999/* Hardware counters must be kept in sync because the order/offset
1000 * is important here (order in structure declaration = order in hardware)
1001 */
1002static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
1003 /* general stats */
1004 STAT_NETDEV(rx_packets),
1005 STAT_NETDEV(tx_packets),
1006 STAT_NETDEV(rx_bytes),
1007 STAT_NETDEV(tx_bytes),
1008 STAT_NETDEV(rx_errors),
1009 STAT_NETDEV(tx_errors),
1010 STAT_NETDEV(rx_dropped),
1011 STAT_NETDEV(tx_dropped),
1012 STAT_NETDEV(multicast),
1013 /* UniMAC RSV counters */
1014 STAT_GENET_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
1015 STAT_GENET_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
1016 STAT_GENET_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
1017 STAT_GENET_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
1018 STAT_GENET_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
1019 STAT_GENET_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
1020 STAT_GENET_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
1021 STAT_GENET_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
1022 STAT_GENET_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
1023 STAT_GENET_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
1024 STAT_GENET_MIB_RX("rx_pkts", mib.rx.pkt),
1025 STAT_GENET_MIB_RX("rx_bytes", mib.rx.bytes),
1026 STAT_GENET_MIB_RX("rx_multicast", mib.rx.mca),
1027 STAT_GENET_MIB_RX("rx_broadcast", mib.rx.bca),
1028 STAT_GENET_MIB_RX("rx_fcs", mib.rx.fcs),
1029 STAT_GENET_MIB_RX("rx_control", mib.rx.cf),
1030 STAT_GENET_MIB_RX("rx_pause", mib.rx.pf),
1031 STAT_GENET_MIB_RX("rx_unknown", mib.rx.uo),
1032 STAT_GENET_MIB_RX("rx_align", mib.rx.aln),
1033 STAT_GENET_MIB_RX("rx_outrange", mib.rx.flr),
1034 STAT_GENET_MIB_RX("rx_code", mib.rx.cde),
1035 STAT_GENET_MIB_RX("rx_carrier", mib.rx.fcr),
1036 STAT_GENET_MIB_RX("rx_oversize", mib.rx.ovr),
1037 STAT_GENET_MIB_RX("rx_jabber", mib.rx.jbr),
1038 STAT_GENET_MIB_RX("rx_mtu_err", mib.rx.mtue),
1039 STAT_GENET_MIB_RX("rx_good_pkts", mib.rx.pok),
1040 STAT_GENET_MIB_RX("rx_unicast", mib.rx.uc),
1041 STAT_GENET_MIB_RX("rx_ppp", mib.rx.ppp),
1042 STAT_GENET_MIB_RX("rx_crc", mib.rx.rcrc),
1043 /* UniMAC TSV counters */
1044 STAT_GENET_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
1045 STAT_GENET_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
1046 STAT_GENET_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
1047 STAT_GENET_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
1048 STAT_GENET_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
1049 STAT_GENET_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
1050 STAT_GENET_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
1051 STAT_GENET_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
1052 STAT_GENET_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
1053 STAT_GENET_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
1054 STAT_GENET_MIB_TX("tx_pkts", mib.tx.pkts),
1055 STAT_GENET_MIB_TX("tx_multicast", mib.tx.mca),
1056 STAT_GENET_MIB_TX("tx_broadcast", mib.tx.bca),
1057 STAT_GENET_MIB_TX("tx_pause", mib.tx.pf),
1058 STAT_GENET_MIB_TX("tx_control", mib.tx.cf),
1059 STAT_GENET_MIB_TX("tx_fcs_err", mib.tx.fcs),
1060 STAT_GENET_MIB_TX("tx_oversize", mib.tx.ovr),
1061 STAT_GENET_MIB_TX("tx_defer", mib.tx.drf),
1062 STAT_GENET_MIB_TX("tx_excess_defer", mib.tx.edf),
1063 STAT_GENET_MIB_TX("tx_single_col", mib.tx.scl),
1064 STAT_GENET_MIB_TX("tx_multi_col", mib.tx.mcl),
1065 STAT_GENET_MIB_TX("tx_late_col", mib.tx.lcl),
1066 STAT_GENET_MIB_TX("tx_excess_col", mib.tx.ecl),
1067 STAT_GENET_MIB_TX("tx_frags", mib.tx.frg),
1068 STAT_GENET_MIB_TX("tx_total_col", mib.tx.ncl),
1069 STAT_GENET_MIB_TX("tx_jabber", mib.tx.jbr),
1070 STAT_GENET_MIB_TX("tx_bytes", mib.tx.bytes),
1071 STAT_GENET_MIB_TX("tx_good_pkts", mib.tx.pok),
1072 STAT_GENET_MIB_TX("tx_unicast", mib.tx.uc),
1073 /* UniMAC RUNT counters */
1074 STAT_GENET_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
1075 STAT_GENET_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
1076 STAT_GENET_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
1077 STAT_GENET_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
1078 /* Misc UniMAC counters */
1079 STAT_GENET_MISC("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt,
Doug Bergerffff7132017-03-09 16:58:43 -08001080 UMAC_RBUF_OVFL_CNT_V1),
1081 STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt,
1082 UMAC_RBUF_ERR_CNT_V1),
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001083 STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT),
Florian Fainellif62ba9c2015-02-28 18:09:16 -08001084 STAT_GENET_SOFT_MIB("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
1085 STAT_GENET_SOFT_MIB("rx_dma_failed", mib.rx_dma_failed),
1086 STAT_GENET_SOFT_MIB("tx_dma_failed", mib.tx_dma_failed),
Doug Bergerf1af17c2019-12-17 16:51:15 -08001087 STAT_GENET_SOFT_MIB("tx_realloc_tsb", mib.tx_realloc_tsb),
1088 STAT_GENET_SOFT_MIB("tx_realloc_tsb_failed",
1089 mib.tx_realloc_tsb_failed),
Florian Fainelli37a30b42017-03-16 10:27:08 -07001090 /* Per TX queues */
1091 STAT_GENET_Q(0),
1092 STAT_GENET_Q(1),
1093 STAT_GENET_Q(2),
1094 STAT_GENET_Q(3),
1095 STAT_GENET_Q(16),
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001096};
1097
1098#define BCMGENET_STATS_LEN ARRAY_SIZE(bcmgenet_gstrings_stats)
1099
1100static void bcmgenet_get_drvinfo(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001101 struct ethtool_drvinfo *info)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001102{
1103 strlcpy(info->driver, "bcmgenet", sizeof(info->driver));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001104}
1105
1106static int bcmgenet_get_sset_count(struct net_device *dev, int string_set)
1107{
1108 switch (string_set) {
1109 case ETH_SS_STATS:
1110 return BCMGENET_STATS_LEN;
1111 default:
1112 return -EOPNOTSUPP;
1113 }
1114}
1115
Florian Fainellic91b7f62014-07-23 10:42:12 -07001116static void bcmgenet_get_strings(struct net_device *dev, u32 stringset,
1117 u8 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001118{
1119 int i;
1120
1121 switch (stringset) {
1122 case ETH_SS_STATS:
1123 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
1124 memcpy(data + i * ETH_GSTRING_LEN,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001125 bcmgenet_gstrings_stats[i].stat_string,
1126 ETH_GSTRING_LEN);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001127 }
1128 break;
1129 }
1130}
1131
Doug Bergerffff7132017-03-09 16:58:43 -08001132static u32 bcmgenet_update_stat_misc(struct bcmgenet_priv *priv, u16 offset)
1133{
1134 u16 new_offset;
1135 u32 val;
1136
1137 switch (offset) {
1138 case UMAC_RBUF_OVFL_CNT_V1:
1139 if (GENET_IS_V2(priv))
1140 new_offset = RBUF_OVFL_CNT_V2;
1141 else
1142 new_offset = RBUF_OVFL_CNT_V3PLUS;
1143
1144 val = bcmgenet_rbuf_readl(priv, new_offset);
1145 /* clear if overflowed */
1146 if (val == ~0)
1147 bcmgenet_rbuf_writel(priv, 0, new_offset);
1148 break;
1149 case UMAC_RBUF_ERR_CNT_V1:
1150 if (GENET_IS_V2(priv))
1151 new_offset = RBUF_ERR_CNT_V2;
1152 else
1153 new_offset = RBUF_ERR_CNT_V3PLUS;
1154
1155 val = bcmgenet_rbuf_readl(priv, new_offset);
1156 /* clear if overflowed */
1157 if (val == ~0)
1158 bcmgenet_rbuf_writel(priv, 0, new_offset);
1159 break;
1160 default:
1161 val = bcmgenet_umac_readl(priv, offset);
1162 /* clear if overflowed */
1163 if (val == ~0)
1164 bcmgenet_umac_writel(priv, 0, offset);
1165 break;
1166 }
1167
1168 return val;
1169}
1170
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001171static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv)
1172{
1173 int i, j = 0;
1174
1175 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
1176 const struct bcmgenet_stats *s;
1177 u8 offset = 0;
1178 u32 val = 0;
1179 char *p;
1180
1181 s = &bcmgenet_gstrings_stats[i];
1182 switch (s->type) {
1183 case BCMGENET_STAT_NETDEV:
Florian Fainellif62ba9c2015-02-28 18:09:16 -08001184 case BCMGENET_STAT_SOFT:
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001185 continue;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001186 case BCMGENET_STAT_RUNT:
Doug Berger1ad3d222017-03-09 16:58:44 -08001187 offset += BCMGENET_STAT_OFFSET;
1188 /* fall through */
1189 case BCMGENET_STAT_MIB_TX:
1190 offset += BCMGENET_STAT_OFFSET;
1191 /* fall through */
1192 case BCMGENET_STAT_MIB_RX:
Florian Fainellic91b7f62014-07-23 10:42:12 -07001193 val = bcmgenet_umac_readl(priv,
1194 UMAC_MIB_START + j + offset);
Doug Berger1ad3d222017-03-09 16:58:44 -08001195 offset = 0; /* Reset Offset */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001196 break;
1197 case BCMGENET_STAT_MISC:
Doug Bergerffff7132017-03-09 16:58:43 -08001198 if (GENET_IS_V1(priv)) {
1199 val = bcmgenet_umac_readl(priv, s->reg_offset);
1200 /* clear if overflowed */
1201 if (val == ~0)
1202 bcmgenet_umac_writel(priv, 0,
1203 s->reg_offset);
1204 } else {
1205 val = bcmgenet_update_stat_misc(priv,
1206 s->reg_offset);
1207 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001208 break;
1209 }
1210
1211 j += s->stat_sizeof;
1212 p = (char *)priv + s->stat_offset;
1213 *(u32 *)p = val;
1214 }
1215}
1216
1217static void bcmgenet_get_ethtool_stats(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001218 struct ethtool_stats *stats,
1219 u64 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001220{
1221 struct bcmgenet_priv *priv = netdev_priv(dev);
1222 int i;
1223
1224 if (netif_running(dev))
1225 bcmgenet_update_mib_counters(priv);
1226
Doug Bergera6d0b832020-04-23 15:44:17 -07001227 dev->netdev_ops->ndo_get_stats(dev);
1228
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001229 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
1230 const struct bcmgenet_stats *s;
1231 char *p;
1232
1233 s = &bcmgenet_gstrings_stats[i];
1234 if (s->type == BCMGENET_STAT_NETDEV)
1235 p = (char *)&dev->stats;
1236 else
1237 p = (char *)priv;
1238 p += s->stat_offset;
Eric Dumazet6517eb52016-04-15 10:47:52 -07001239 if (sizeof(unsigned long) != sizeof(u32) &&
1240 s->stat_sizeof == sizeof(unsigned long))
1241 data[i] = *(unsigned long *)p;
1242 else
1243 data[i] = *(u32 *)p;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001244 }
1245}
1246
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001247static void bcmgenet_eee_enable_set(struct net_device *dev, bool enable)
1248{
1249 struct bcmgenet_priv *priv = netdev_priv(dev);
1250 u32 off = priv->hw_params->tbuf_offset + TBUF_ENERGY_CTRL;
1251 u32 reg;
1252
1253 if (enable && !priv->clk_eee_enabled) {
1254 clk_prepare_enable(priv->clk_eee);
1255 priv->clk_eee_enabled = true;
1256 }
1257
1258 reg = bcmgenet_umac_readl(priv, UMAC_EEE_CTRL);
1259 if (enable)
1260 reg |= EEE_EN;
1261 else
1262 reg &= ~EEE_EN;
1263 bcmgenet_umac_writel(priv, reg, UMAC_EEE_CTRL);
1264
1265 /* Enable EEE and switch to a 27Mhz clock automatically */
Florian Fainelli69d2ea92017-08-29 12:25:31 -07001266 reg = bcmgenet_readl(priv->base + off);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001267 if (enable)
1268 reg |= TBUF_EEE_EN | TBUF_PM_EN;
1269 else
1270 reg &= ~(TBUF_EEE_EN | TBUF_PM_EN);
Florian Fainelli69d2ea92017-08-29 12:25:31 -07001271 bcmgenet_writel(reg, priv->base + off);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001272
1273 /* Do the same for thing for RBUF */
1274 reg = bcmgenet_rbuf_readl(priv, RBUF_ENERGY_CTRL);
1275 if (enable)
1276 reg |= RBUF_EEE_EN | RBUF_PM_EN;
1277 else
1278 reg &= ~(RBUF_EEE_EN | RBUF_PM_EN);
1279 bcmgenet_rbuf_writel(priv, reg, RBUF_ENERGY_CTRL);
1280
1281 if (!enable && priv->clk_eee_enabled) {
1282 clk_disable_unprepare(priv->clk_eee);
1283 priv->clk_eee_enabled = false;
1284 }
1285
1286 priv->eee.eee_enabled = enable;
1287 priv->eee.eee_active = enable;
1288}
1289
1290static int bcmgenet_get_eee(struct net_device *dev, struct ethtool_eee *e)
1291{
1292 struct bcmgenet_priv *priv = netdev_priv(dev);
1293 struct ethtool_eee *p = &priv->eee;
1294
1295 if (GENET_IS_V1(priv))
1296 return -EOPNOTSUPP;
1297
Doug Berger6c97f012017-10-25 15:04:19 -07001298 if (!dev->phydev)
1299 return -ENODEV;
1300
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001301 e->eee_enabled = p->eee_enabled;
1302 e->eee_active = p->eee_active;
1303 e->tx_lpi_timer = bcmgenet_umac_readl(priv, UMAC_EEE_LPI_TIMER);
1304
Doug Berger6c97f012017-10-25 15:04:19 -07001305 return phy_ethtool_get_eee(dev->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001306}
1307
1308static int bcmgenet_set_eee(struct net_device *dev, struct ethtool_eee *e)
1309{
1310 struct bcmgenet_priv *priv = netdev_priv(dev);
1311 struct ethtool_eee *p = &priv->eee;
1312 int ret = 0;
1313
1314 if (GENET_IS_V1(priv))
1315 return -EOPNOTSUPP;
1316
Doug Berger6c97f012017-10-25 15:04:19 -07001317 if (!dev->phydev)
1318 return -ENODEV;
1319
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001320 p->eee_enabled = e->eee_enabled;
1321
1322 if (!p->eee_enabled) {
1323 bcmgenet_eee_enable_set(dev, false);
1324 } else {
Doug Berger6c97f012017-10-25 15:04:19 -07001325 ret = phy_init_eee(dev->phydev, 0);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001326 if (ret) {
1327 netif_err(priv, hw, dev, "EEE initialization failed\n");
1328 return ret;
1329 }
1330
1331 bcmgenet_umac_writel(priv, e->tx_lpi_timer, UMAC_EEE_LPI_TIMER);
1332 bcmgenet_eee_enable_set(dev, true);
1333 }
1334
Doug Berger6c97f012017-10-25 15:04:19 -07001335 return phy_ethtool_set_eee(dev->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001336}
1337
Doug Berger3e370952020-04-29 13:02:05 -07001338static int bcmgenet_validate_flow(struct net_device *dev,
1339 struct ethtool_rxnfc *cmd)
1340{
1341 struct ethtool_usrip4_spec *l4_mask;
1342 struct ethhdr *eth_mask;
1343
1344 if (cmd->fs.location >= MAX_NUM_OF_FS_RULES) {
1345 netdev_err(dev, "rxnfc: Invalid location (%d)\n",
1346 cmd->fs.location);
1347 return -EINVAL;
1348 }
1349
1350 switch (cmd->fs.flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
1351 case IP_USER_FLOW:
1352 l4_mask = &cmd->fs.m_u.usr_ip4_spec;
1353 /* don't allow mask which isn't valid */
1354 if (VALIDATE_MASK(l4_mask->ip4src) ||
1355 VALIDATE_MASK(l4_mask->ip4dst) ||
1356 VALIDATE_MASK(l4_mask->l4_4_bytes) ||
1357 VALIDATE_MASK(l4_mask->proto) ||
1358 VALIDATE_MASK(l4_mask->ip_ver) ||
1359 VALIDATE_MASK(l4_mask->tos)) {
1360 netdev_err(dev, "rxnfc: Unsupported mask\n");
1361 return -EINVAL;
1362 }
1363 break;
1364 case ETHER_FLOW:
1365 eth_mask = &cmd->fs.m_u.ether_spec;
1366 /* don't allow mask which isn't valid */
1367 if (VALIDATE_MASK(eth_mask->h_source) ||
1368 VALIDATE_MASK(eth_mask->h_source) ||
1369 VALIDATE_MASK(eth_mask->h_proto)) {
1370 netdev_err(dev, "rxnfc: Unsupported mask\n");
1371 return -EINVAL;
1372 }
1373 break;
1374 default:
1375 netdev_err(dev, "rxnfc: Unsupported flow type (0x%x)\n",
1376 cmd->fs.flow_type);
1377 return -EINVAL;
1378 }
1379
1380 if ((cmd->fs.flow_type & FLOW_EXT)) {
1381 /* don't allow mask which isn't valid */
1382 if (VALIDATE_MASK(cmd->fs.m_ext.vlan_etype) ||
1383 VALIDATE_MASK(cmd->fs.m_ext.vlan_tci)) {
1384 netdev_err(dev, "rxnfc: Unsupported mask\n");
1385 return -EINVAL;
1386 }
1387 if (cmd->fs.m_ext.data[0] || cmd->fs.m_ext.data[1]) {
1388 netdev_err(dev, "rxnfc: user-def not supported\n");
1389 return -EINVAL;
1390 }
1391 }
1392
1393 if ((cmd->fs.flow_type & FLOW_MAC_EXT)) {
1394 /* don't allow mask which isn't valid */
1395 if (VALIDATE_MASK(cmd->fs.m_ext.h_dest)) {
1396 netdev_err(dev, "rxnfc: Unsupported mask\n");
1397 return -EINVAL;
1398 }
1399 }
1400
1401 return 0;
1402}
1403
1404static int bcmgenet_insert_flow(struct net_device *dev,
1405 struct ethtool_rxnfc *cmd)
1406{
1407 struct bcmgenet_priv *priv = netdev_priv(dev);
1408 struct bcmgenet_rxnfc_rule *loc_rule;
1409 int err;
1410
1411 if (priv->hw_params->hfb_filter_size < 128) {
1412 netdev_err(dev, "rxnfc: Not supported by this device\n");
1413 return -EINVAL;
1414 }
1415
Doug Bergerf50932c2020-04-29 13:02:06 -07001416 if (cmd->fs.ring_cookie > priv->hw_params->rx_queues &&
1417 cmd->fs.ring_cookie != RX_CLS_FLOW_WAKE) {
Doug Berger3e370952020-04-29 13:02:05 -07001418 netdev_err(dev, "rxnfc: Unsupported action (%llu)\n",
1419 cmd->fs.ring_cookie);
1420 return -EINVAL;
1421 }
1422
1423 err = bcmgenet_validate_flow(dev, cmd);
1424 if (err)
1425 return err;
1426
1427 loc_rule = &priv->rxnfc_rules[cmd->fs.location];
1428 if (loc_rule->state == BCMGENET_RXNFC_STATE_ENABLED)
1429 bcmgenet_hfb_disable_filter(priv, cmd->fs.location);
Doug Bergera8c64542020-07-16 16:38:17 -07001430 if (loc_rule->state != BCMGENET_RXNFC_STATE_UNUSED) {
Doug Berger3e370952020-04-29 13:02:05 -07001431 list_del(&loc_rule->list);
Doug Bergera8c64542020-07-16 16:38:17 -07001432 bcmgenet_hfb_clear_filter(priv, cmd->fs.location);
1433 }
Doug Berger3e370952020-04-29 13:02:05 -07001434 loc_rule->state = BCMGENET_RXNFC_STATE_UNUSED;
1435 memcpy(&loc_rule->fs, &cmd->fs,
1436 sizeof(struct ethtool_rx_flow_spec));
1437
Doug Bergera8c64542020-07-16 16:38:17 -07001438 bcmgenet_hfb_create_rxnfc_filter(priv, loc_rule);
Doug Berger3e370952020-04-29 13:02:05 -07001439
1440 list_add_tail(&loc_rule->list, &priv->rxnfc_list);
1441
1442 return 0;
1443}
1444
1445static int bcmgenet_delete_flow(struct net_device *dev,
1446 struct ethtool_rxnfc *cmd)
1447{
1448 struct bcmgenet_priv *priv = netdev_priv(dev);
1449 struct bcmgenet_rxnfc_rule *rule;
1450 int err = 0;
1451
1452 if (cmd->fs.location >= MAX_NUM_OF_FS_RULES)
1453 return -EINVAL;
1454
1455 rule = &priv->rxnfc_rules[cmd->fs.location];
1456 if (rule->state == BCMGENET_RXNFC_STATE_UNUSED) {
1457 err = -ENOENT;
1458 goto out;
1459 }
1460
1461 if (rule->state == BCMGENET_RXNFC_STATE_ENABLED)
1462 bcmgenet_hfb_disable_filter(priv, cmd->fs.location);
Doug Bergera8c64542020-07-16 16:38:17 -07001463 if (rule->state != BCMGENET_RXNFC_STATE_UNUSED) {
Doug Berger3e370952020-04-29 13:02:05 -07001464 list_del(&rule->list);
Doug Bergera8c64542020-07-16 16:38:17 -07001465 bcmgenet_hfb_clear_filter(priv, cmd->fs.location);
1466 }
Doug Berger3e370952020-04-29 13:02:05 -07001467 rule->state = BCMGENET_RXNFC_STATE_UNUSED;
1468 memset(&rule->fs, 0, sizeof(struct ethtool_rx_flow_spec));
1469
1470out:
1471 return err;
1472}
1473
1474static int bcmgenet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1475{
1476 struct bcmgenet_priv *priv = netdev_priv(dev);
1477 int err = 0;
1478
1479 switch (cmd->cmd) {
1480 case ETHTOOL_SRXCLSRLINS:
1481 err = bcmgenet_insert_flow(dev, cmd);
1482 break;
1483 case ETHTOOL_SRXCLSRLDEL:
1484 err = bcmgenet_delete_flow(dev, cmd);
1485 break;
1486 default:
1487 netdev_warn(priv->dev, "Unsupported ethtool command. (%d)\n",
1488 cmd->cmd);
1489 return -EINVAL;
1490 }
1491
1492 return err;
1493}
1494
1495static int bcmgenet_get_flow(struct net_device *dev, struct ethtool_rxnfc *cmd,
1496 int loc)
1497{
1498 struct bcmgenet_priv *priv = netdev_priv(dev);
1499 struct bcmgenet_rxnfc_rule *rule;
1500 int err = 0;
1501
1502 if (loc < 0 || loc >= MAX_NUM_OF_FS_RULES)
1503 return -EINVAL;
1504
1505 rule = &priv->rxnfc_rules[loc];
1506 if (rule->state == BCMGENET_RXNFC_STATE_UNUSED)
1507 err = -ENOENT;
1508 else
1509 memcpy(&cmd->fs, &rule->fs,
1510 sizeof(struct ethtool_rx_flow_spec));
1511
1512 return err;
1513}
1514
1515static int bcmgenet_get_num_flows(struct bcmgenet_priv *priv)
1516{
1517 struct list_head *pos;
1518 int res = 0;
1519
1520 list_for_each(pos, &priv->rxnfc_list)
1521 res++;
1522
1523 return res;
1524}
1525
1526static int bcmgenet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1527 u32 *rule_locs)
1528{
1529 struct bcmgenet_priv *priv = netdev_priv(dev);
1530 struct bcmgenet_rxnfc_rule *rule;
1531 int err = 0;
1532 int i = 0;
1533
1534 switch (cmd->cmd) {
1535 case ETHTOOL_GRXRINGS:
1536 cmd->data = priv->hw_params->rx_queues ?: 1;
1537 break;
1538 case ETHTOOL_GRXCLSRLCNT:
1539 cmd->rule_cnt = bcmgenet_get_num_flows(priv);
1540 cmd->data = MAX_NUM_OF_FS_RULES;
1541 break;
1542 case ETHTOOL_GRXCLSRULE:
1543 err = bcmgenet_get_flow(dev, cmd, cmd->fs.location);
1544 break;
1545 case ETHTOOL_GRXCLSRLALL:
1546 list_for_each_entry(rule, &priv->rxnfc_list, list)
1547 if (i < cmd->rule_cnt)
1548 rule_locs[i++] = rule->fs.location;
1549 cmd->rule_cnt = i;
1550 cmd->data = MAX_NUM_OF_FS_RULES;
1551 break;
1552 default:
1553 err = -EOPNOTSUPP;
1554 break;
1555 }
1556
1557 return err;
1558}
1559
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001560/* standard ethtool support functions. */
Julia Lawall70591ab2016-08-31 09:30:45 +02001561static const struct ethtool_ops bcmgenet_ethtool_ops = {
Jakub Kicinskif6f508c2020-03-09 19:15:03 -07001562 .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
1563 ETHTOOL_COALESCE_MAX_FRAMES |
1564 ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
Edwin Chan89316fa2017-03-09 16:58:49 -08001565 .begin = bcmgenet_begin,
1566 .complete = bcmgenet_complete,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001567 .get_strings = bcmgenet_get_strings,
1568 .get_sset_count = bcmgenet_get_sset_count,
1569 .get_ethtool_stats = bcmgenet_get_ethtool_stats,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001570 .get_drvinfo = bcmgenet_get_drvinfo,
1571 .get_link = ethtool_op_get_link,
1572 .get_msglevel = bcmgenet_get_msglevel,
1573 .set_msglevel = bcmgenet_set_msglevel,
Florian Fainelli06ba8372014-07-21 15:29:29 -07001574 .get_wol = bcmgenet_get_wol,
1575 .set_wol = bcmgenet_set_wol,
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001576 .get_eee = bcmgenet_get_eee,
1577 .set_eee = bcmgenet_set_eee,
Florian Fainelli016e7702016-11-15 10:06:38 -08001578 .nway_reset = phy_ethtool_nway_reset,
Florian Fainelli2f913072015-09-16 16:47:39 -07001579 .get_coalesce = bcmgenet_get_coalesce,
1580 .set_coalesce = bcmgenet_set_coalesce,
Philippe Reynesfa92bf02016-09-26 22:31:57 +02001581 .get_link_ksettings = bcmgenet_get_link_ksettings,
1582 .set_link_ksettings = bcmgenet_set_link_ksettings,
Ryan M. Collinsdd1bf472019-08-30 14:49:55 -04001583 .get_ts_info = ethtool_op_get_ts_info,
Doug Berger3e370952020-04-29 13:02:05 -07001584 .get_rxnfc = bcmgenet_get_rxnfc,
1585 .set_rxnfc = bcmgenet_set_rxnfc,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001586};
1587
1588/* Power down the unimac, based on mode. */
Florian Fainellica8cf342015-03-23 15:09:51 -07001589static int bcmgenet_power_down(struct bcmgenet_priv *priv,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001590 enum bcmgenet_power_mode mode)
1591{
Florian Fainellica8cf342015-03-23 15:09:51 -07001592 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001593 u32 reg;
1594
1595 switch (mode) {
1596 case GENET_POWER_CABLE_SENSE:
Doug Berger6c97f012017-10-25 15:04:19 -07001597 phy_detach(priv->dev->phydev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001598 break;
1599
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001600 case GENET_POWER_WOL_MAGIC:
Florian Fainellica8cf342015-03-23 15:09:51 -07001601 ret = bcmgenet_wol_power_down_cfg(priv, mode);
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001602 break;
1603
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001604 case GENET_POWER_PASSIVE:
1605 /* Power down LED */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001606 if (priv->hw_params->flags & GENET_HAS_EXT) {
1607 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
Doug Berger42138082017-03-13 17:41:42 -07001608 if (GENET_IS_V5(priv))
1609 reg |= EXT_PWR_DOWN_PHY_EN |
1610 EXT_PWR_DOWN_PHY_RD |
1611 EXT_PWR_DOWN_PHY_SD |
1612 EXT_PWR_DOWN_PHY_RX |
1613 EXT_PWR_DOWN_PHY_TX |
1614 EXT_IDDQ_GLBL_PWR;
1615 else
1616 reg |= EXT_PWR_DOWN_PHY;
1617
1618 reg |= (EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001619 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
Florian Fainellia642c4f2015-03-23 15:09:56 -07001620
1621 bcmgenet_phy_power_set(priv->dev, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001622 }
1623 break;
1624 default:
1625 break;
1626 }
Florian Fainellica8cf342015-03-23 15:09:51 -07001627
YueHaibing0db55092018-11-08 02:08:43 +00001628 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001629}
1630
1631static void bcmgenet_power_up(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001632 enum bcmgenet_power_mode mode)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001633{
1634 u32 reg;
1635
1636 if (!(priv->hw_params->flags & GENET_HAS_EXT))
1637 return;
1638
1639 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
1640
1641 switch (mode) {
1642 case GENET_POWER_PASSIVE:
Doug Berger42138082017-03-13 17:41:42 -07001643 reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
1644 if (GENET_IS_V5(priv)) {
1645 reg &= ~(EXT_PWR_DOWN_PHY_EN |
1646 EXT_PWR_DOWN_PHY_RD |
1647 EXT_PWR_DOWN_PHY_SD |
1648 EXT_PWR_DOWN_PHY_RX |
1649 EXT_PWR_DOWN_PHY_TX |
1650 EXT_IDDQ_GLBL_PWR);
1651 reg |= EXT_PHY_RESET;
1652 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1653 mdelay(1);
1654
1655 reg &= ~EXT_PHY_RESET;
1656 } else {
1657 reg &= ~EXT_PWR_DOWN_PHY;
1658 reg |= EXT_PWR_DN_EN_LD;
1659 }
1660 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1661 bcmgenet_phy_power_set(priv->dev, true);
Doug Berger42138082017-03-13 17:41:42 -07001662 break;
1663
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001664 case GENET_POWER_CABLE_SENSE:
1665 /* enable APD */
Doug Berger42138082017-03-13 17:41:42 -07001666 if (!GENET_IS_V5(priv)) {
1667 reg |= EXT_PWR_DN_EN_LD;
1668 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1669 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001670 break;
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001671 case GENET_POWER_WOL_MAGIC:
1672 bcmgenet_wol_power_up_cfg(priv, mode);
1673 return;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001674 default:
1675 break;
1676 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001677}
1678
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001679static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv,
1680 struct bcmgenet_tx_ring *ring)
1681{
1682 struct enet_cb *tx_cb_ptr;
1683
1684 tx_cb_ptr = ring->cbs;
1685 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
Petri Gynther014012a2015-02-23 11:00:45 -08001686
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001687 /* Advancing local write pointer */
1688 if (ring->write_ptr == ring->end_ptr)
1689 ring->write_ptr = ring->cb_ptr;
1690 else
1691 ring->write_ptr++;
1692
1693 return tx_cb_ptr;
1694}
1695
Doug Berger876dbad2017-07-14 16:12:09 -07001696static struct enet_cb *bcmgenet_put_txcb(struct bcmgenet_priv *priv,
1697 struct bcmgenet_tx_ring *ring)
1698{
1699 struct enet_cb *tx_cb_ptr;
1700
1701 tx_cb_ptr = ring->cbs;
1702 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
1703
1704 /* Rewinding local write pointer */
1705 if (ring->write_ptr == ring->cb_ptr)
1706 ring->write_ptr = ring->end_ptr;
1707 else
1708 ring->write_ptr--;
1709
1710 return tx_cb_ptr;
1711}
1712
Petri Gynther4055eae2015-03-25 12:35:16 -07001713static inline void bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring *ring)
1714{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001715 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001716 INTRL2_CPU_MASK_SET);
1717}
1718
1719static inline void bcmgenet_rx_ring16_int_enable(struct bcmgenet_rx_ring *ring)
1720{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001721 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001722 INTRL2_CPU_MASK_CLEAR);
1723}
1724
1725static inline void bcmgenet_rx_ring_int_disable(struct bcmgenet_rx_ring *ring)
1726{
1727 bcmgenet_intrl2_1_writel(ring->priv,
1728 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1729 INTRL2_CPU_MASK_SET);
1730}
1731
1732static inline void bcmgenet_rx_ring_int_enable(struct bcmgenet_rx_ring *ring)
1733{
1734 bcmgenet_intrl2_1_writel(ring->priv,
1735 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1736 INTRL2_CPU_MASK_CLEAR);
1737}
1738
Petri Gynther9dbac282015-03-25 12:35:10 -07001739static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001740{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001741 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001742 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001743}
1744
Petri Gynther9dbac282015-03-25 12:35:10 -07001745static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001746{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001747 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001748 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001749}
1750
Petri Gynther9dbac282015-03-25 12:35:10 -07001751static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001752{
Petri Gynther9dbac282015-03-25 12:35:10 -07001753 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001754 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001755}
1756
Petri Gynther9dbac282015-03-25 12:35:10 -07001757static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001758{
Petri Gynther9dbac282015-03-25 12:35:10 -07001759 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001760 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001761}
1762
Doug Bergerf48bed12017-07-14 16:12:10 -07001763/* Simple helper to free a transmit control block's resources
1764 * Returns an skb when the last transmit control block associated with the
1765 * skb is freed. The skb should be freed by the caller if necessary.
1766 */
1767static struct sk_buff *bcmgenet_free_tx_cb(struct device *dev,
1768 struct enet_cb *cb)
1769{
1770 struct sk_buff *skb;
1771
1772 skb = cb->skb;
1773
1774 if (skb) {
1775 cb->skb = NULL;
1776 if (cb == GENET_CB(skb)->first_cb)
1777 dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
1778 dma_unmap_len(cb, dma_len),
1779 DMA_TO_DEVICE);
1780 else
1781 dma_unmap_page(dev, dma_unmap_addr(cb, dma_addr),
1782 dma_unmap_len(cb, dma_len),
1783 DMA_TO_DEVICE);
1784 dma_unmap_addr_set(cb, dma_addr, 0);
1785
1786 if (cb == GENET_CB(skb)->last_cb)
1787 return skb;
1788
1789 } else if (dma_unmap_addr(cb, dma_addr)) {
1790 dma_unmap_page(dev,
1791 dma_unmap_addr(cb, dma_addr),
1792 dma_unmap_len(cb, dma_len),
1793 DMA_TO_DEVICE);
1794 dma_unmap_addr_set(cb, dma_addr, 0);
1795 }
1796
Wei Yongjun335ab8b2018-03-28 12:51:19 +00001797 return NULL;
Doug Bergerf48bed12017-07-14 16:12:10 -07001798}
1799
1800/* Simple helper to free a receive control block's resources */
1801static struct sk_buff *bcmgenet_free_rx_cb(struct device *dev,
1802 struct enet_cb *cb)
1803{
1804 struct sk_buff *skb;
1805
1806 skb = cb->skb;
1807 cb->skb = NULL;
1808
1809 if (dma_unmap_addr(cb, dma_addr)) {
1810 dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
1811 dma_unmap_len(cb, dma_len), DMA_FROM_DEVICE);
1812 dma_unmap_addr_set(cb, dma_addr, 0);
1813 }
1814
1815 return skb;
1816}
1817
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001818/* Unlocked version of the reclaim routine */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001819static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
1820 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001821{
1822 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther66d06752015-03-04 14:30:01 -08001823 unsigned int txbds_processed = 0;
Doug Bergerf48bed12017-07-14 16:12:10 -07001824 unsigned int bytes_compl = 0;
1825 unsigned int pkts_compl = 0;
1826 unsigned int txbds_ready;
1827 unsigned int c_index;
1828 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001829
Doug Bergerd5810ca2017-03-13 17:41:37 -07001830 /* Clear status before servicing to reduce spurious interrupts */
1831 if (ring->index == DESC_INDEX)
1832 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_TXDMA_DONE,
1833 INTRL2_CPU_CLEAR);
1834 else
1835 bcmgenet_intrl2_1_writel(priv, (1 << ring->index),
1836 INTRL2_CPU_CLEAR);
1837
Brian Norris7fc527f2014-07-29 14:34:14 -07001838 /* Compute how many buffers are transmitted since last xmit call */
Doug Bergerc298ede2017-03-13 17:41:33 -07001839 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX)
1840 & DMA_C_INDEX_MASK;
1841 txbds_ready = (c_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001842
1843 netif_dbg(priv, tx_done, dev,
Petri Gynther66d06752015-03-04 14:30:01 -08001844 "%s ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
1845 __func__, ring->index, ring->c_index, c_index, txbds_ready);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001846
1847 /* Reclaim transmitted buffers */
Petri Gynther66d06752015-03-04 14:30:01 -08001848 while (txbds_processed < txbds_ready) {
Doug Bergerf48bed12017-07-14 16:12:10 -07001849 skb = bcmgenet_free_tx_cb(&priv->pdev->dev,
1850 &priv->tx_cbs[ring->clean_ptr]);
1851 if (skb) {
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001852 pkts_compl++;
Doug Bergerf48bed12017-07-14 16:12:10 -07001853 bytes_compl += GENET_CB(skb)->bytes_sent;
Florian Fainellid4fec852017-08-24 15:56:29 -07001854 dev_consume_skb_any(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001855 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001856
Petri Gynther66d06752015-03-04 14:30:01 -08001857 txbds_processed++;
1858 if (likely(ring->clean_ptr < ring->end_ptr))
1859 ring->clean_ptr++;
1860 else
1861 ring->clean_ptr = ring->cb_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001862 }
1863
Petri Gynther66d06752015-03-04 14:30:01 -08001864 ring->free_bds += txbds_processed;
Doug Bergerc4d453d2017-03-13 17:41:38 -07001865 ring->c_index = c_index;
Petri Gynther66d06752015-03-04 14:30:01 -08001866
Florian Fainelli37a30b42017-03-16 10:27:08 -07001867 ring->packets += pkts_compl;
1868 ring->bytes += bytes_compl;
Petri Gynther55868122016-03-24 11:27:20 -07001869
Doug Berger6d22fe12017-03-09 16:58:50 -08001870 netdev_tx_completed_queue(netdev_get_tx_queue(dev, ring->queue),
1871 pkts_compl, bytes_compl);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001872
Doug Bergerc4d453d2017-03-13 17:41:38 -07001873 return txbds_processed;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001874}
1875
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001876static unsigned int bcmgenet_tx_reclaim(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001877 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001878{
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001879 unsigned int released;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001880
Doug Bergerb0447ec2017-10-25 15:04:17 -07001881 spin_lock_bh(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001882 released = __bcmgenet_tx_reclaim(dev, ring);
Doug Bergerb0447ec2017-10-25 15:04:17 -07001883 spin_unlock_bh(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001884
1885 return released;
1886}
1887
1888static int bcmgenet_tx_poll(struct napi_struct *napi, int budget)
1889{
1890 struct bcmgenet_tx_ring *ring =
1891 container_of(napi, struct bcmgenet_tx_ring, napi);
1892 unsigned int work_done = 0;
Doug Berger6d22fe12017-03-09 16:58:50 -08001893 struct netdev_queue *txq;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001894
Doug Bergerb0447ec2017-10-25 15:04:17 -07001895 spin_lock(&ring->lock);
Doug Berger6d22fe12017-03-09 16:58:50 -08001896 work_done = __bcmgenet_tx_reclaim(ring->priv->dev, ring);
1897 if (ring->free_bds > (MAX_SKB_FRAGS + 1)) {
1898 txq = netdev_get_tx_queue(ring->priv->dev, ring->queue);
1899 netif_tx_wake_queue(txq);
1900 }
Doug Bergerb0447ec2017-10-25 15:04:17 -07001901 spin_unlock(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001902
1903 if (work_done == 0) {
1904 napi_complete(napi);
Petri Gynther9dbac282015-03-25 12:35:10 -07001905 ring->int_enable(ring);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001906
1907 return 0;
1908 }
1909
1910 return budget;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001911}
1912
1913static void bcmgenet_tx_reclaim_all(struct net_device *dev)
1914{
1915 struct bcmgenet_priv *priv = netdev_priv(dev);
1916 int i;
1917
1918 if (netif_is_multiqueue(dev)) {
1919 for (i = 0; i < priv->hw_params->tx_queues; i++)
1920 bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
1921 }
1922
1923 bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
1924}
1925
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001926/* Reallocate the SKB to put enough headroom in front of it and insert
1927 * the transmit checksum offsets in the descriptors
1928 */
Doug Berger9a9ba2a2020-03-17 17:05:36 -07001929static struct sk_buff *bcmgenet_add_tsb(struct net_device *dev,
1930 struct sk_buff *skb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001931{
Doug Bergerf1af17c2019-12-17 16:51:15 -08001932 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001933 struct status_64 *status = NULL;
1934 struct sk_buff *new_skb;
1935 u16 offset;
1936 u8 ip_proto;
Florian Fainelli6f894212018-04-02 15:58:55 -07001937 __be16 ip_ver;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001938 u32 tx_csum_info;
1939
1940 if (unlikely(skb_headroom(skb) < sizeof(*status))) {
1941 /* If 64 byte status block enabled, must make sure skb has
1942 * enough headroom for us to insert 64B status block.
1943 */
1944 new_skb = skb_realloc_headroom(skb, sizeof(*status));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001945 if (!new_skb) {
Doug Bergere3fa8582019-12-17 16:51:14 -08001946 dev_kfree_skb_any(skb);
Doug Bergerf1af17c2019-12-17 16:51:15 -08001947 priv->mib.tx_realloc_tsb_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001948 dev->stats.tx_dropped++;
Petri Gyntherbc233332014-10-01 11:30:01 -07001949 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001950 }
Doug Bergere3fa8582019-12-17 16:51:14 -08001951 dev_consume_skb_any(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001952 skb = new_skb;
Doug Bergerf1af17c2019-12-17 16:51:15 -08001953 priv->mib.tx_realloc_tsb++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001954 }
1955
1956 skb_push(skb, sizeof(*status));
1957 status = (struct status_64 *)skb->data;
1958
1959 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Florian Fainelli6f894212018-04-02 15:58:55 -07001960 ip_ver = skb->protocol;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001961 switch (ip_ver) {
Florian Fainelli6f894212018-04-02 15:58:55 -07001962 case htons(ETH_P_IP):
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001963 ip_proto = ip_hdr(skb)->protocol;
1964 break;
Florian Fainelli6f894212018-04-02 15:58:55 -07001965 case htons(ETH_P_IPV6):
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001966 ip_proto = ipv6_hdr(skb)->nexthdr;
1967 break;
1968 default:
Doug Bergerdd8e9112019-12-17 16:51:09 -08001969 /* don't use UDP flag */
1970 ip_proto = 0;
1971 break;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001972 }
1973
1974 offset = skb_checksum_start_offset(skb) - sizeof(*status);
1975 tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
Doug Bergerdd8e9112019-12-17 16:51:09 -08001976 (offset + skb->csum_offset) |
1977 STATUS_TX_CSUM_LV;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001978
Doug Bergerdd8e9112019-12-17 16:51:09 -08001979 /* Set the special UDP flag for UDP */
1980 if (ip_proto == IPPROTO_UDP)
1981 tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001982
1983 status->tx_csum_info = tx_csum_info;
1984 }
1985
Petri Gyntherbc233332014-10-01 11:30:01 -07001986 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001987}
1988
1989static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
1990{
1991 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Berger876dbad2017-07-14 16:12:09 -07001992 struct device *kdev = &priv->pdev->dev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001993 struct bcmgenet_tx_ring *ring = NULL;
Doug Berger876dbad2017-07-14 16:12:09 -07001994 struct enet_cb *tx_cb_ptr;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001995 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001996 int nr_frags, index;
Doug Berger876dbad2017-07-14 16:12:09 -07001997 dma_addr_t mapping;
1998 unsigned int size;
1999 skb_frag_t *frag;
2000 u32 len_stat;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002001 int ret;
2002 int i;
2003
2004 index = skb_get_queue_mapping(skb);
2005 /* Mapping strategy:
2006 * queue_mapping = 0, unclassified, packet xmited through ring16
2007 * queue_mapping = 1, goes to ring 0. (highest priority queue
2008 * queue_mapping = 2, goes to ring 1.
2009 * queue_mapping = 3, goes to ring 2.
2010 * queue_mapping = 4, goes to ring 3.
2011 */
2012 if (index == 0)
2013 index = DESC_INDEX;
2014 else
2015 index -= 1;
2016
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002017 ring = &priv->tx_rings[index];
Florian Fainellib2cde2c2014-03-20 10:53:23 -07002018 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002019
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07002020 nr_frags = skb_shinfo(skb)->nr_frags;
2021
Doug Bergerb0447ec2017-10-25 15:04:17 -07002022 spin_lock(&ring->lock);
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07002023 if (ring->free_bds <= (nr_frags + 1)) {
2024 if (!netif_tx_queue_stopped(txq)) {
2025 netif_tx_stop_queue(txq);
2026 netdev_err(dev,
2027 "%s: tx ring %d full when queue %d awake\n",
2028 __func__, index, ring->queue);
2029 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002030 ret = NETDEV_TX_BUSY;
2031 goto out;
2032 }
2033
Petri Gynther55868122016-03-24 11:27:20 -07002034 /* Retain how many bytes will be sent on the wire, without TSB inserted
2035 * by transmit checksum offload
2036 */
2037 GENET_CB(skb)->bytes_sent = skb->len;
2038
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002039 /* add the Transmit Status Block */
2040 skb = bcmgenet_add_tsb(dev, skb);
2041 if (!skb) {
2042 ret = NETDEV_TX_OK;
2043 goto out;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002044 }
2045
Doug Berger876dbad2017-07-14 16:12:09 -07002046 for (i = 0; i <= nr_frags; i++) {
2047 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002048
Gustavo A. R. Silva4fa112f2017-10-26 07:16:01 -05002049 BUG_ON(!tx_cb_ptr);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002050
Doug Berger876dbad2017-07-14 16:12:09 -07002051 if (!i) {
2052 /* Transmit single SKB or head of fragment list */
Doug Bergerf48bed12017-07-14 16:12:10 -07002053 GENET_CB(skb)->first_cb = tx_cb_ptr;
Doug Berger876dbad2017-07-14 16:12:09 -07002054 size = skb_headlen(skb);
2055 mapping = dma_map_single(kdev, skb->data, size,
2056 DMA_TO_DEVICE);
2057 } else {
2058 /* xmit fragment */
Doug Berger876dbad2017-07-14 16:12:09 -07002059 frag = &skb_shinfo(skb)->frags[i - 1];
2060 size = skb_frag_size(frag);
2061 mapping = skb_frag_dma_map(kdev, frag, 0, size,
2062 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002063 }
Doug Berger876dbad2017-07-14 16:12:09 -07002064
2065 ret = dma_mapping_error(kdev, mapping);
2066 if (ret) {
2067 priv->mib.tx_dma_failed++;
2068 netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
2069 ret = NETDEV_TX_OK;
2070 goto out_unmap_frags;
2071 }
2072 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
2073 dma_unmap_len_set(tx_cb_ptr, dma_len, size);
2074
Doug Bergerf48bed12017-07-14 16:12:10 -07002075 tx_cb_ptr->skb = skb;
2076
Doug Berger876dbad2017-07-14 16:12:09 -07002077 len_stat = (size << DMA_BUFLENGTH_SHIFT) |
2078 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT);
2079
Doug Berger20d1f2d2020-06-24 18:14:55 -07002080 /* Note: if we ever change from DMA_TX_APPEND_CRC below we
2081 * will need to restore software padding of "runt" packets
2082 */
Doug Berger876dbad2017-07-14 16:12:09 -07002083 if (!i) {
2084 len_stat |= DMA_TX_APPEND_CRC | DMA_SOP;
2085 if (skb->ip_summed == CHECKSUM_PARTIAL)
2086 len_stat |= DMA_TX_DO_CSUM;
2087 }
2088 if (i == nr_frags)
2089 len_stat |= DMA_EOP;
2090
2091 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, len_stat);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002092 }
2093
Doug Bergerf48bed12017-07-14 16:12:10 -07002094 GENET_CB(skb)->last_cb = tx_cb_ptr;
Florian Fainellid03825f2014-03-20 10:53:21 -07002095 skb_tx_timestamp(skb);
2096
Florian Fainelliae67bf02015-03-13 12:11:06 -07002097 /* Decrement total BD count and advance our write pointer */
2098 ring->free_bds -= nr_frags + 1;
2099 ring->prod_index += nr_frags + 1;
2100 ring->prod_index &= DMA_P_INDEX_MASK;
2101
Petri Gynthere178c8c2016-04-09 00:20:36 -07002102 netdev_tx_sent_queue(txq, GENET_CB(skb)->bytes_sent);
2103
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002104 if (ring->free_bds <= (MAX_SKB_FRAGS + 1))
Florian Fainellib2cde2c2014-03-20 10:53:23 -07002105 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002106
Florian Westphal6b16f9e2019-04-01 16:42:14 +02002107 if (!netdev_xmit_more() || netif_xmit_stopped(txq))
Florian Fainelliddd0ca52015-03-13 12:11:07 -07002108 /* Packets are ready, update producer index */
2109 bcmgenet_tdma_ring_writel(priv, ring->index,
2110 ring->prod_index, TDMA_PROD_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002111out:
Doug Bergerb0447ec2017-10-25 15:04:17 -07002112 spin_unlock(&ring->lock);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002113
2114 return ret;
Doug Berger876dbad2017-07-14 16:12:09 -07002115
2116out_unmap_frags:
2117 /* Back up for failed control block mapping */
2118 bcmgenet_put_txcb(priv, ring);
2119
2120 /* Unmap successfully mapped control blocks */
2121 while (i-- > 0) {
2122 tx_cb_ptr = bcmgenet_put_txcb(priv, ring);
Doug Bergerf48bed12017-07-14 16:12:10 -07002123 bcmgenet_free_tx_cb(kdev, tx_cb_ptr);
Doug Berger876dbad2017-07-14 16:12:09 -07002124 }
2125
2126 dev_kfree_skb(skb);
2127 goto out;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002128}
2129
Petri Gyntherd6707be2015-03-12 15:48:00 -07002130static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
2131 struct enet_cb *cb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002132{
2133 struct device *kdev = &priv->pdev->dev;
2134 struct sk_buff *skb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002135 struct sk_buff *rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002136 dma_addr_t mapping;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002137
Petri Gyntherd6707be2015-03-12 15:48:00 -07002138 /* Allocate a new Rx skb */
Doug Bergerecaeceb2020-04-23 16:02:11 -07002139 skb = __netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT,
2140 GFP_ATOMIC | __GFP_NOWARN);
Petri Gyntherd6707be2015-03-12 15:48:00 -07002141 if (!skb) {
2142 priv->mib.alloc_rx_buff_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002143 netif_err(priv, rx_err, priv->dev,
Petri Gyntherd6707be2015-03-12 15:48:00 -07002144 "%s: Rx skb allocation failed\n", __func__);
2145 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002146 }
2147
Petri Gyntherd6707be2015-03-12 15:48:00 -07002148 /* DMA-map the new Rx skb */
2149 mapping = dma_map_single(kdev, skb->data, priv->rx_buf_len,
2150 DMA_FROM_DEVICE);
2151 if (dma_mapping_error(kdev, mapping)) {
2152 priv->mib.rx_dma_failed++;
2153 dev_kfree_skb_any(skb);
2154 netif_err(priv, rx_err, priv->dev,
2155 "%s: Rx skb DMA mapping failed\n", __func__);
2156 return NULL;
2157 }
2158
2159 /* Grab the current Rx skb from the ring and DMA-unmap it */
Doug Bergerf48bed12017-07-14 16:12:10 -07002160 rx_skb = bcmgenet_free_rx_cb(kdev, cb);
Petri Gyntherd6707be2015-03-12 15:48:00 -07002161
2162 /* Put the new Rx skb on the ring */
2163 cb->skb = skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002164 dma_unmap_addr_set(cb, dma_addr, mapping);
Doug Bergerf48bed12017-07-14 16:12:10 -07002165 dma_unmap_len_set(cb, dma_len, priv->rx_buf_len);
Petri Gynther8ac467e2015-03-09 13:40:00 -07002166 dmadesc_set_addr(priv, cb->bd_addr, mapping);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002167
Petri Gyntherd6707be2015-03-12 15:48:00 -07002168 /* Return the current Rx skb to caller */
2169 return rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002170}
2171
2172/* bcmgenet_desc_rx - descriptor based rx process.
2173 * this could be called from bottom half, or from NAPI polling method.
2174 */
Petri Gynther4055eae2015-03-25 12:35:16 -07002175static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002176 unsigned int budget)
2177{
Petri Gynther4055eae2015-03-25 12:35:16 -07002178 struct bcmgenet_priv *priv = ring->priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002179 struct net_device *dev = priv->dev;
2180 struct enet_cb *cb;
2181 struct sk_buff *skb;
2182 u32 dma_length_status;
2183 unsigned long dma_flag;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002184 int len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002185 unsigned int rxpktprocessed = 0, rxpkttoprocess;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002186 unsigned int bytes_processed = 0;
Doug Bergerd5810ca2017-03-13 17:41:37 -07002187 unsigned int p_index, mask;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07002188 unsigned int discards;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002189
Doug Bergerd5810ca2017-03-13 17:41:37 -07002190 /* Clear status before servicing to reduce spurious interrupts */
2191 if (ring->index == DESC_INDEX) {
2192 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_RXDMA_DONE,
2193 INTRL2_CPU_CLEAR);
2194 } else {
2195 mask = 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index);
2196 bcmgenet_intrl2_1_writel(priv,
2197 mask,
2198 INTRL2_CPU_CLEAR);
2199 }
2200
Petri Gynther4055eae2015-03-25 12:35:16 -07002201 p_index = bcmgenet_rdma_ring_readl(priv, ring->index, RDMA_PROD_INDEX);
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07002202
2203 discards = (p_index >> DMA_P_INDEX_DISCARD_CNT_SHIFT) &
2204 DMA_P_INDEX_DISCARD_CNT_MASK;
2205 if (discards > ring->old_discards) {
2206 discards = discards - ring->old_discards;
Florian Fainelli37a30b42017-03-16 10:27:08 -07002207 ring->errors += discards;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07002208 ring->old_discards += discards;
2209
2210 /* Clear HW register when we reach 75% of maximum 0xFFFF */
2211 if (ring->old_discards >= 0xC000) {
2212 ring->old_discards = 0;
Petri Gynther4055eae2015-03-25 12:35:16 -07002213 bcmgenet_rdma_ring_writel(priv, ring->index, 0,
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07002214 RDMA_PROD_INDEX);
2215 }
2216 }
2217
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002218 p_index &= DMA_P_INDEX_MASK;
Doug Bergerc298ede2017-03-13 17:41:33 -07002219 rxpkttoprocess = (p_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002220
2221 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002222 "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002223
2224 while ((rxpktprocessed < rxpkttoprocess) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07002225 (rxpktprocessed < budget)) {
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002226 struct status_64 *status;
2227 __be16 rx_csum;
2228
Petri Gynther8ac467e2015-03-09 13:40:00 -07002229 cb = &priv->rx_cbs[ring->read_ptr];
Petri Gyntherd6707be2015-03-12 15:48:00 -07002230 skb = bcmgenet_rx_refill(priv, cb);
Florian Fainellib629be52014-09-08 11:37:52 -07002231
Florian Fainellib629be52014-09-08 11:37:52 -07002232 if (unlikely(!skb)) {
Florian Fainelli37a30b42017-03-16 10:27:08 -07002233 ring->dropped++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002234 goto next;
Florian Fainellib629be52014-09-08 11:37:52 -07002235 }
2236
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002237 status = (struct status_64 *)skb->data;
2238 dma_length_status = status->length_status;
2239 if (dev->features & NETIF_F_RXCSUM) {
Doug Berger81015532019-12-17 16:51:10 -08002240 rx_csum = (__force __be16)(status->rx_csum & 0xffff);
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002241 skb->csum = (__force __wsum)ntohs(rx_csum);
2242 skb->ip_summed = CHECKSUM_COMPLETE;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002243 }
2244
2245 /* DMA flags and length are still valid no matter how
2246 * we got the Receive Status Vector (64B RSB or register)
2247 */
2248 dma_flag = dma_length_status & 0xffff;
2249 len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
2250
2251 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002252 "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
Petri Gynther8ac467e2015-03-09 13:40:00 -07002253 __func__, p_index, ring->c_index,
2254 ring->read_ptr, dma_length_status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002255
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002256 if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
2257 netif_err(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002258 "dropping fragmented packet!\n");
Florian Fainelli37a30b42017-03-16 10:27:08 -07002259 ring->errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002260 dev_kfree_skb_any(skb);
2261 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002262 }
Petri Gyntherd6707be2015-03-12 15:48:00 -07002263
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002264 /* report errors */
2265 if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
2266 DMA_RX_OV |
2267 DMA_RX_NO |
2268 DMA_RX_LG |
2269 DMA_RX_RXER))) {
2270 netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07002271 (unsigned int)dma_flag);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002272 if (dma_flag & DMA_RX_CRC_ERROR)
2273 dev->stats.rx_crc_errors++;
2274 if (dma_flag & DMA_RX_OV)
2275 dev->stats.rx_over_errors++;
2276 if (dma_flag & DMA_RX_NO)
2277 dev->stats.rx_frame_errors++;
2278 if (dma_flag & DMA_RX_LG)
2279 dev->stats.rx_length_errors++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002280 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002281 dev_kfree_skb_any(skb);
2282 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002283 } /* error packet */
2284
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002285 skb_put(skb, len);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002286
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002287 /* remove RSB and hardware 2bytes added for IP alignment */
2288 skb_pull(skb, 66);
2289 len -= 66;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002290
2291 if (priv->crc_fwd_en) {
2292 skb_trim(skb, len - ETH_FCS_LEN);
2293 len -= ETH_FCS_LEN;
2294 }
2295
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002296 bytes_processed += len;
2297
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002298 /*Finish setting up the received SKB and send it to the kernel*/
2299 skb->protocol = eth_type_trans(skb, priv->dev);
Florian Fainelli37a30b42017-03-16 10:27:08 -07002300 ring->packets++;
2301 ring->bytes += len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002302 if (dma_flag & DMA_RX_MULT)
2303 dev->stats.multicast++;
2304
2305 /* Notify kernel */
Petri Gynther4055eae2015-03-25 12:35:16 -07002306 napi_gro_receive(&ring->napi, skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002307 netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
2308
Petri Gyntherd6707be2015-03-12 15:48:00 -07002309next:
Florian Fainellicf377d82014-10-10 10:51:52 -07002310 rxpktprocessed++;
Petri Gynther8ac467e2015-03-09 13:40:00 -07002311 if (likely(ring->read_ptr < ring->end_ptr))
2312 ring->read_ptr++;
2313 else
2314 ring->read_ptr = ring->cb_ptr;
2315
2316 ring->c_index = (ring->c_index + 1) & DMA_C_INDEX_MASK;
Petri Gynther4055eae2015-03-25 12:35:16 -07002317 bcmgenet_rdma_ring_writel(priv, ring->index, ring->c_index, RDMA_CONS_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002318 }
2319
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002320 ring->dim.bytes = bytes_processed;
2321 ring->dim.packets = rxpktprocessed;
2322
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002323 return rxpktprocessed;
2324}
2325
Petri Gynther3ab11332015-03-25 12:35:15 -07002326/* Rx NAPI polling method */
2327static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
2328{
Petri Gynther4055eae2015-03-25 12:35:16 -07002329 struct bcmgenet_rx_ring *ring = container_of(napi,
2330 struct bcmgenet_rx_ring, napi);
Yamin Friedmanf06d0ca2019-07-23 10:22:47 +03002331 struct dim_sample dim_sample = {};
Petri Gynther3ab11332015-03-25 12:35:15 -07002332 unsigned int work_done;
2333
Petri Gynther4055eae2015-03-25 12:35:16 -07002334 work_done = bcmgenet_desc_rx(ring, budget);
Petri Gynther3ab11332015-03-25 12:35:15 -07002335
2336 if (work_done < budget) {
Eric Dumazeteb96ce02016-04-08 22:06:40 -07002337 napi_complete_done(napi, work_done);
Petri Gynther4055eae2015-03-25 12:35:16 -07002338 ring->int_enable(ring);
Petri Gynther3ab11332015-03-25 12:35:15 -07002339 }
2340
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002341 if (ring->dim.use_dim) {
Tal Gilboa8960b382019-01-31 16:44:48 +02002342 dim_update_sample(ring->dim.event_ctr, ring->dim.packets,
2343 ring->dim.bytes, &dim_sample);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002344 net_dim(&ring->dim.dim, dim_sample);
2345 }
2346
Petri Gynther3ab11332015-03-25 12:35:15 -07002347 return work_done;
2348}
2349
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002350static void bcmgenet_dim_work(struct work_struct *work)
2351{
Tal Gilboa8960b382019-01-31 16:44:48 +02002352 struct dim *dim = container_of(work, struct dim, work);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002353 struct bcmgenet_net_dim *ndim =
2354 container_of(dim, struct bcmgenet_net_dim, dim);
2355 struct bcmgenet_rx_ring *ring =
2356 container_of(ndim, struct bcmgenet_rx_ring, dim);
Tal Gilboa8960b382019-01-31 16:44:48 +02002357 struct dim_cq_moder cur_profile =
Tal Gilboa026a8072018-04-24 13:36:01 +03002358 net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002359
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002360 bcmgenet_set_rx_coalesce(ring, cur_profile.usec, cur_profile.pkts);
Tal Gilboac002bd52018-11-05 12:07:52 +02002361 dim->state = DIM_START_MEASURE;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002362}
2363
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002364/* Assign skb to RX DMA descriptor. */
Petri Gynther8ac467e2015-03-09 13:40:00 -07002365static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv,
2366 struct bcmgenet_rx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002367{
2368 struct enet_cb *cb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002369 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002370 int i;
2371
Petri Gynther8ac467e2015-03-09 13:40:00 -07002372 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002373
2374 /* loop here for each buffer needing assign */
Petri Gynther8ac467e2015-03-09 13:40:00 -07002375 for (i = 0; i < ring->size; i++) {
2376 cb = ring->cbs + i;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002377 skb = bcmgenet_rx_refill(priv, cb);
2378 if (skb)
Florian Fainellid4fec852017-08-24 15:56:29 -07002379 dev_consume_skb_any(skb);
Petri Gyntherd6707be2015-03-12 15:48:00 -07002380 if (!cb->skb)
2381 return -ENOMEM;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002382 }
2383
Petri Gyntherd6707be2015-03-12 15:48:00 -07002384 return 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002385}
2386
2387static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
2388{
Doug Bergerf48bed12017-07-14 16:12:10 -07002389 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002390 struct enet_cb *cb;
2391 int i;
2392
2393 for (i = 0; i < priv->num_rx_bds; i++) {
2394 cb = &priv->rx_cbs[i];
2395
Doug Bergerf48bed12017-07-14 16:12:10 -07002396 skb = bcmgenet_free_rx_cb(&priv->pdev->dev, cb);
2397 if (skb)
Florian Fainellid4fec852017-08-24 15:56:29 -07002398 dev_consume_skb_any(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002399 }
2400}
2401
Florian Fainellic91b7f62014-07-23 10:42:12 -07002402static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable)
Florian Fainellie29585b2014-07-21 15:29:20 -07002403{
2404 u32 reg;
2405
2406 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
Doug Berger88f6c8b2020-03-16 14:44:56 -07002407 if (reg & CMD_SW_RESET)
2408 return;
Florian Fainellie29585b2014-07-21 15:29:20 -07002409 if (enable)
2410 reg |= mask;
2411 else
2412 reg &= ~mask;
2413 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
2414
2415 /* UniMAC stops on a packet boundary, wait for a full-size packet
2416 * to be processed
2417 */
2418 if (enable == 0)
2419 usleep_range(1000, 2000);
2420}
2421
Doug Berger28c2d1a2017-10-25 15:04:13 -07002422static void reset_umac(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002423{
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002424 /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
2425 bcmgenet_rbuf_ctrl_set(priv, 0);
2426 udelay(10);
2427
Doug Berger88f6c8b2020-03-16 14:44:56 -07002428 /* issue soft reset and disable MAC while updating its registers */
2429 bcmgenet_umac_writel(priv, CMD_SW_RESET, UMAC_CMD);
Doug Berger612eb1c2020-03-16 14:44:55 -07002430 udelay(2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002431}
2432
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002433static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
2434{
2435 /* Mask all interrupts.*/
2436 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
2437 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002438 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
2439 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002440}
2441
Florian Fainelli37850e32015-10-17 14:22:46 -07002442static void bcmgenet_link_intr_enable(struct bcmgenet_priv *priv)
2443{
2444 u32 int0_enable = 0;
2445
2446 /* Monitor cable plug/unplugged event for internal PHY, external PHY
2447 * and MoCA PHY
2448 */
2449 if (priv->internal_phy) {
2450 int0_enable |= UMAC_IRQ_LINK_EVENT;
Doug Berger25382b92019-10-16 16:06:32 -07002451 if (GENET_IS_V1(priv) || GENET_IS_V2(priv) || GENET_IS_V3(priv))
2452 int0_enable |= UMAC_IRQ_PHY_DET_R;
Florian Fainelli37850e32015-10-17 14:22:46 -07002453 } else if (priv->ext_phy) {
2454 int0_enable |= UMAC_IRQ_LINK_EVENT;
2455 } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
2456 if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
2457 int0_enable |= UMAC_IRQ_LINK_EVENT;
2458 }
2459 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
2460}
2461
Doug Berger28c2d1a2017-10-25 15:04:13 -07002462static void init_umac(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002463{
2464 struct device *kdev = &priv->pdev->dev;
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002465 u32 reg;
2466 u32 int0_enable = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002467
2468 dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
2469
Doug Berger28c2d1a2017-10-25 15:04:13 -07002470 reset_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002471
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002472 /* clear tx/rx counter */
2473 bcmgenet_umac_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002474 MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
2475 UMAC_MIB_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002476 bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
2477
2478 bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
2479
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002480 /* init tx registers, enable TSB */
2481 reg = bcmgenet_tbuf_ctrl_get(priv);
2482 reg |= TBUF_64B_EN;
2483 bcmgenet_tbuf_ctrl_set(priv, reg);
2484
2485 /* init rx registers, enable ip header optimization and RSB */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002486 reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002487 reg |= RBUF_ALIGN_2B | RBUF_64B_EN;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002488 bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
2489
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002490 /* enable rx checksumming */
2491 reg = bcmgenet_rbuf_readl(priv, RBUF_CHK_CTRL);
2492 reg |= RBUF_RXCHK_EN | RBUF_L3_PARSE_DIS;
2493 /* If UniMAC forwards CRC, we need to skip over it to get
2494 * a valid CHK bit to be set in the per-packet status word
2495 */
2496 if (priv->crc_fwd_en)
2497 reg |= RBUF_SKIP_FCS;
2498 else
2499 reg &= ~RBUF_SKIP_FCS;
2500 bcmgenet_rbuf_writel(priv, reg, RBUF_CHK_CTRL);
2501
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002502 if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
2503 bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
2504
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002505 bcmgenet_intr_disable(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002506
Florian Fainelli37850e32015-10-17 14:22:46 -07002507 /* Configure backpressure vectors for MoCA */
2508 if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002509 reg = bcmgenet_bp_mc_get(priv);
2510 reg |= BIT(priv->hw_params->bp_in_en_shift);
2511
2512 /* bp_mask: back pressure mask */
2513 if (netif_is_multiqueue(priv->dev))
2514 reg |= priv->hw_params->bp_in_mask;
2515 else
2516 reg &= ~priv->hw_params->bp_in_mask;
2517 bcmgenet_bp_mc_set(priv, reg);
2518 }
2519
2520 /* Enable MDIO interrupts on GENET v3+ */
2521 if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002522 int0_enable |= (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002523
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002524 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002525
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002526 dev_dbg(kdev, "done init umac\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002527}
2528
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002529static void bcmgenet_init_dim(struct bcmgenet_rx_ring *ring,
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002530 void (*cb)(struct work_struct *work))
2531{
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002532 struct bcmgenet_net_dim *dim = &ring->dim;
2533
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002534 INIT_WORK(&dim->dim.work, cb);
Tal Gilboac002bd52018-11-05 12:07:52 +02002535 dim->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002536 dim->event_ctr = 0;
2537 dim->packets = 0;
2538 dim->bytes = 0;
2539}
2540
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002541static void bcmgenet_init_rx_coalesce(struct bcmgenet_rx_ring *ring)
2542{
2543 struct bcmgenet_net_dim *dim = &ring->dim;
Tal Gilboa8960b382019-01-31 16:44:48 +02002544 struct dim_cq_moder moder;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002545 u32 usecs, pkts;
2546
2547 usecs = ring->rx_coalesce_usecs;
2548 pkts = ring->rx_max_coalesced_frames;
2549
2550 /* If DIM was enabled, re-apply default parameters */
2551 if (dim->use_dim) {
Tal Gilboa026a8072018-04-24 13:36:01 +03002552 moder = net_dim_get_def_rx_moderation(dim->dim.mode);
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002553 usecs = moder.usec;
2554 pkts = moder.pkts;
2555 }
2556
2557 bcmgenet_set_rx_coalesce(ring, usecs, pkts);
2558}
2559
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002560/* Initialize a Tx ring along with corresponding hardware registers */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002561static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
2562 unsigned int index, unsigned int size,
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002563 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002564{
2565 struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
2566 u32 words_per_bd = WORDS_PER_BD(priv);
2567 u32 flow_period_val = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002568
2569 spin_lock_init(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002570 ring->priv = priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002571 ring->index = index;
2572 if (index == DESC_INDEX) {
2573 ring->queue = 0;
2574 ring->int_enable = bcmgenet_tx_ring16_int_enable;
2575 ring->int_disable = bcmgenet_tx_ring16_int_disable;
2576 } else {
2577 ring->queue = index + 1;
2578 ring->int_enable = bcmgenet_tx_ring_int_enable;
2579 ring->int_disable = bcmgenet_tx_ring_int_disable;
2580 }
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002581 ring->cbs = priv->tx_cbs + start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002582 ring->size = size;
Petri Gynther66d06752015-03-04 14:30:01 -08002583 ring->clean_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002584 ring->c_index = 0;
2585 ring->free_bds = size;
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002586 ring->write_ptr = start_ptr;
2587 ring->cb_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002588 ring->end_ptr = end_ptr - 1;
2589 ring->prod_index = 0;
2590
2591 /* Set flow period for ring != 16 */
2592 if (index != DESC_INDEX)
2593 flow_period_val = ENET_MAX_MTU_SIZE << 16;
2594
2595 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
2596 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
2597 bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
2598 /* Disable rate control for now */
2599 bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002600 TDMA_FLOW_PERIOD);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002601 bcmgenet_tdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002602 ((size << DMA_RING_SIZE_SHIFT) |
2603 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002604
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002605 /* Set start and end address, read and write pointers */
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002606 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002607 DMA_START_ADDR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002608 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002609 TDMA_READ_PTR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002610 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002611 TDMA_WRITE_PTR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002612 bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002613 DMA_END_ADDR);
Doug Berger75879352017-10-25 15:04:14 -07002614
2615 /* Initialize Tx NAPI */
Florian Fainelli148965d2020-01-23 09:49:34 -08002616 netif_tx_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll,
2617 NAPI_POLL_WEIGHT);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002618}
2619
2620/* Initialize a RDMA ring */
2621static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
Petri Gynther8ac467e2015-03-09 13:40:00 -07002622 unsigned int index, unsigned int size,
2623 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002624{
Petri Gynther8ac467e2015-03-09 13:40:00 -07002625 struct bcmgenet_rx_ring *ring = &priv->rx_rings[index];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002626 u32 words_per_bd = WORDS_PER_BD(priv);
2627 int ret;
2628
Petri Gynther4055eae2015-03-25 12:35:16 -07002629 ring->priv = priv;
Petri Gynther8ac467e2015-03-09 13:40:00 -07002630 ring->index = index;
Petri Gynther4055eae2015-03-25 12:35:16 -07002631 if (index == DESC_INDEX) {
2632 ring->int_enable = bcmgenet_rx_ring16_int_enable;
2633 ring->int_disable = bcmgenet_rx_ring16_int_disable;
2634 } else {
2635 ring->int_enable = bcmgenet_rx_ring_int_enable;
2636 ring->int_disable = bcmgenet_rx_ring_int_disable;
2637 }
Petri Gynther8ac467e2015-03-09 13:40:00 -07002638 ring->cbs = priv->rx_cbs + start_ptr;
2639 ring->size = size;
2640 ring->c_index = 0;
2641 ring->read_ptr = start_ptr;
2642 ring->cb_ptr = start_ptr;
2643 ring->end_ptr = end_ptr - 1;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002644
Petri Gynther8ac467e2015-03-09 13:40:00 -07002645 ret = bcmgenet_alloc_rx_buffers(priv, ring);
2646 if (ret)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002647 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002648
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002649 bcmgenet_init_dim(ring, bcmgenet_dim_work);
2650 bcmgenet_init_rx_coalesce(ring);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002651
Doug Berger75879352017-10-25 15:04:14 -07002652 /* Initialize Rx NAPI */
2653 netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll,
2654 NAPI_POLL_WEIGHT);
2655
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002656 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
2657 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
2658 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002659 ((size << DMA_RING_SIZE_SHIFT) |
2660 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002661 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002662 (DMA_FC_THRESH_LO <<
2663 DMA_XOFF_THRESHOLD_SHIFT) |
2664 DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
Petri Gynther6f5a2722015-03-06 13:45:00 -08002665
2666 /* Set start and end address, read and write pointers */
Petri Gynther8ac467e2015-03-09 13:40:00 -07002667 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2668 DMA_START_ADDR);
2669 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2670 RDMA_READ_PTR);
2671 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2672 RDMA_WRITE_PTR);
2673 bcmgenet_rdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Petri Gynther6f5a2722015-03-06 13:45:00 -08002674 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002675
2676 return ret;
2677}
2678
Petri Gynthere2aadb42015-03-25 12:35:14 -07002679static void bcmgenet_enable_tx_napi(struct bcmgenet_priv *priv)
2680{
2681 unsigned int i;
2682 struct bcmgenet_tx_ring *ring;
2683
2684 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2685 ring = &priv->tx_rings[i];
2686 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002687 ring->int_enable(ring);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002688 }
2689
2690 ring = &priv->tx_rings[DESC_INDEX];
2691 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002692 ring->int_enable(ring);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002693}
2694
2695static void bcmgenet_disable_tx_napi(struct bcmgenet_priv *priv)
2696{
2697 unsigned int i;
2698 struct bcmgenet_tx_ring *ring;
2699
2700 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2701 ring = &priv->tx_rings[i];
2702 napi_disable(&ring->napi);
2703 }
2704
2705 ring = &priv->tx_rings[DESC_INDEX];
2706 napi_disable(&ring->napi);
2707}
2708
2709static void bcmgenet_fini_tx_napi(struct bcmgenet_priv *priv)
2710{
2711 unsigned int i;
2712 struct bcmgenet_tx_ring *ring;
2713
2714 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2715 ring = &priv->tx_rings[i];
2716 netif_napi_del(&ring->napi);
2717 }
2718
2719 ring = &priv->tx_rings[DESC_INDEX];
2720 netif_napi_del(&ring->napi);
2721}
2722
Petri Gynther16c6d662015-02-23 11:00:45 -08002723/* Initialize Tx queues
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002724 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002725 * Queues 0-3 are priority-based, each one has 32 descriptors,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002726 * with queue 0 being the highest priority queue.
2727 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002728 * Queue 16 is the default Tx queue with
Petri Gynther51a966a2015-02-23 11:00:46 -08002729 * GENET_Q16_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors.
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002730 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002731 * The transmit control block pool is then partitioned as follows:
2732 * - Tx queue 0 uses tx_cbs[0..31]
2733 * - Tx queue 1 uses tx_cbs[32..63]
2734 * - Tx queue 2 uses tx_cbs[64..95]
2735 * - Tx queue 3 uses tx_cbs[96..127]
2736 * - Tx queue 16 uses tx_cbs[128..255]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002737 */
Petri Gynther16c6d662015-02-23 11:00:45 -08002738static void bcmgenet_init_tx_queues(struct net_device *dev)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002739{
2740 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther16c6d662015-02-23 11:00:45 -08002741 u32 i, dma_enable;
2742 u32 dma_ctrl, ring_cfg;
Petri Gynther37742162014-10-07 09:30:01 -07002743 u32 dma_priority[3] = {0, 0, 0};
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002744
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002745 dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
2746 dma_enable = dma_ctrl & DMA_EN;
2747 dma_ctrl &= ~DMA_EN;
2748 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
2749
Petri Gynther16c6d662015-02-23 11:00:45 -08002750 dma_ctrl = 0;
2751 ring_cfg = 0;
2752
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002753 /* Enable strict priority arbiter mode */
2754 bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
2755
Petri Gynther16c6d662015-02-23 11:00:45 -08002756 /* Initialize Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002757 for (i = 0; i < priv->hw_params->tx_queues; i++) {
Petri Gynther51a966a2015-02-23 11:00:46 -08002758 bcmgenet_init_tx_ring(priv, i, priv->hw_params->tx_bds_per_q,
2759 i * priv->hw_params->tx_bds_per_q,
2760 (i + 1) * priv->hw_params->tx_bds_per_q);
Petri Gynther16c6d662015-02-23 11:00:45 -08002761 ring_cfg |= (1 << i);
2762 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002763 dma_priority[DMA_PRIO_REG_INDEX(i)] |=
2764 ((GENET_Q0_PRIORITY + i) << DMA_PRIO_REG_SHIFT(i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002765 }
2766
Petri Gynther16c6d662015-02-23 11:00:45 -08002767 /* Initialize Tx default queue 16 */
Petri Gynther51a966a2015-02-23 11:00:46 -08002768 bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_Q16_TX_BD_CNT,
Petri Gynther16c6d662015-02-23 11:00:45 -08002769 priv->hw_params->tx_queues *
Petri Gynther51a966a2015-02-23 11:00:46 -08002770 priv->hw_params->tx_bds_per_q,
Petri Gynther16c6d662015-02-23 11:00:45 -08002771 TOTAL_DESC);
2772 ring_cfg |= (1 << DESC_INDEX);
2773 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002774 dma_priority[DMA_PRIO_REG_INDEX(DESC_INDEX)] |=
2775 ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) <<
2776 DMA_PRIO_REG_SHIFT(DESC_INDEX));
Petri Gynther16c6d662015-02-23 11:00:45 -08002777
2778 /* Set Tx queue priorities */
Petri Gynther37742162014-10-07 09:30:01 -07002779 bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
2780 bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
2781 bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);
2782
Petri Gynther16c6d662015-02-23 11:00:45 -08002783 /* Enable Tx queues */
2784 bcmgenet_tdma_writel(priv, ring_cfg, DMA_RING_CFG);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002785
Petri Gynther16c6d662015-02-23 11:00:45 -08002786 /* Enable Tx DMA */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002787 if (dma_enable)
Petri Gynther16c6d662015-02-23 11:00:45 -08002788 dma_ctrl |= DMA_EN;
2789 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002790}
2791
Petri Gynther3ab11332015-03-25 12:35:15 -07002792static void bcmgenet_enable_rx_napi(struct bcmgenet_priv *priv)
2793{
Petri Gynther4055eae2015-03-25 12:35:16 -07002794 unsigned int i;
2795 struct bcmgenet_rx_ring *ring;
2796
2797 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2798 ring = &priv->rx_rings[i];
2799 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002800 ring->int_enable(ring);
Petri Gynther4055eae2015-03-25 12:35:16 -07002801 }
2802
2803 ring = &priv->rx_rings[DESC_INDEX];
2804 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002805 ring->int_enable(ring);
Petri Gynther3ab11332015-03-25 12:35:15 -07002806}
2807
2808static void bcmgenet_disable_rx_napi(struct bcmgenet_priv *priv)
2809{
Petri Gynther4055eae2015-03-25 12:35:16 -07002810 unsigned int i;
2811 struct bcmgenet_rx_ring *ring;
2812
2813 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2814 ring = &priv->rx_rings[i];
2815 napi_disable(&ring->napi);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002816 cancel_work_sync(&ring->dim.dim.work);
Petri Gynther4055eae2015-03-25 12:35:16 -07002817 }
2818
2819 ring = &priv->rx_rings[DESC_INDEX];
2820 napi_disable(&ring->napi);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002821 cancel_work_sync(&ring->dim.dim.work);
Petri Gynther3ab11332015-03-25 12:35:15 -07002822}
2823
2824static void bcmgenet_fini_rx_napi(struct bcmgenet_priv *priv)
2825{
Petri Gynther4055eae2015-03-25 12:35:16 -07002826 unsigned int i;
2827 struct bcmgenet_rx_ring *ring;
2828
2829 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2830 ring = &priv->rx_rings[i];
2831 netif_napi_del(&ring->napi);
2832 }
2833
2834 ring = &priv->rx_rings[DESC_INDEX];
2835 netif_napi_del(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002836}
2837
Petri Gynther8ac467e2015-03-09 13:40:00 -07002838/* Initialize Rx queues
2839 *
2840 * Queues 0-15 are priority queues. Hardware Filtering Block (HFB) can be
2841 * used to direct traffic to these queues.
2842 *
2843 * Queue 16 is the default Rx queue with GENET_Q16_RX_BD_CNT descriptors.
2844 */
2845static int bcmgenet_init_rx_queues(struct net_device *dev)
2846{
2847 struct bcmgenet_priv *priv = netdev_priv(dev);
2848 u32 i;
2849 u32 dma_enable;
2850 u32 dma_ctrl;
2851 u32 ring_cfg;
2852 int ret;
2853
2854 dma_ctrl = bcmgenet_rdma_readl(priv, DMA_CTRL);
2855 dma_enable = dma_ctrl & DMA_EN;
2856 dma_ctrl &= ~DMA_EN;
2857 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2858
2859 dma_ctrl = 0;
2860 ring_cfg = 0;
2861
2862 /* Initialize Rx priority queues */
2863 for (i = 0; i < priv->hw_params->rx_queues; i++) {
2864 ret = bcmgenet_init_rx_ring(priv, i,
2865 priv->hw_params->rx_bds_per_q,
2866 i * priv->hw_params->rx_bds_per_q,
2867 (i + 1) *
2868 priv->hw_params->rx_bds_per_q);
2869 if (ret)
2870 return ret;
2871
2872 ring_cfg |= (1 << i);
2873 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2874 }
2875
2876 /* Initialize Rx default queue 16 */
2877 ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, GENET_Q16_RX_BD_CNT,
2878 priv->hw_params->rx_queues *
2879 priv->hw_params->rx_bds_per_q,
2880 TOTAL_DESC);
2881 if (ret)
2882 return ret;
2883
2884 ring_cfg |= (1 << DESC_INDEX);
2885 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
2886
2887 /* Enable rings */
2888 bcmgenet_rdma_writel(priv, ring_cfg, DMA_RING_CFG);
2889
2890 /* Configure ring as descriptor ring and re-enable DMA if enabled */
2891 if (dma_enable)
2892 dma_ctrl |= DMA_EN;
2893 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2894
2895 return 0;
2896}
2897
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002898static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
2899{
2900 int ret = 0;
2901 int timeout = 0;
2902 u32 reg;
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002903 u32 dma_ctrl;
2904 int i;
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002905
2906 /* Disable TDMA to stop add more frames in TX DMA */
2907 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2908 reg &= ~DMA_EN;
2909 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2910
2911 /* Check TDMA status register to confirm TDMA is disabled */
2912 while (timeout++ < DMA_TIMEOUT_VAL) {
2913 reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
2914 if (reg & DMA_DISABLED)
2915 break;
2916
2917 udelay(1);
2918 }
2919
2920 if (timeout == DMA_TIMEOUT_VAL) {
2921 netdev_warn(priv->dev, "Timed out while disabling TX DMA\n");
2922 ret = -ETIMEDOUT;
2923 }
2924
2925 /* Wait 10ms for packet drain in both tx and rx dma */
2926 usleep_range(10000, 20000);
2927
2928 /* Disable RDMA */
2929 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2930 reg &= ~DMA_EN;
2931 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2932
2933 timeout = 0;
2934 /* Check RDMA status register to confirm RDMA is disabled */
2935 while (timeout++ < DMA_TIMEOUT_VAL) {
2936 reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
2937 if (reg & DMA_DISABLED)
2938 break;
2939
2940 udelay(1);
2941 }
2942
2943 if (timeout == DMA_TIMEOUT_VAL) {
2944 netdev_warn(priv->dev, "Timed out while disabling RX DMA\n");
2945 ret = -ETIMEDOUT;
2946 }
2947
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002948 dma_ctrl = 0;
2949 for (i = 0; i < priv->hw_params->rx_queues; i++)
2950 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2951 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2952 reg &= ~dma_ctrl;
2953 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2954
2955 dma_ctrl = 0;
2956 for (i = 0; i < priv->hw_params->tx_queues; i++)
2957 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2958 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2959 reg &= ~dma_ctrl;
2960 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2961
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002962 return ret;
2963}
2964
Petri Gynther9abab962015-03-30 00:29:01 -07002965static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002966{
Petri Gynthere178c8c2016-04-09 00:20:36 -07002967 struct netdev_queue *txq;
Doug Bergerf48bed12017-07-14 16:12:10 -07002968 int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002969
Petri Gynther9abab962015-03-30 00:29:01 -07002970 bcmgenet_fini_rx_napi(priv);
2971 bcmgenet_fini_tx_napi(priv);
2972
Markus Elfring399e06a2019-08-22 20:02:56 +02002973 for (i = 0; i < priv->num_tx_bds; i++)
2974 dev_kfree_skb(bcmgenet_free_tx_cb(&priv->pdev->dev,
2975 priv->tx_cbs + i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002976
Petri Gynthere178c8c2016-04-09 00:20:36 -07002977 for (i = 0; i < priv->hw_params->tx_queues; i++) {
2978 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[i].queue);
2979 netdev_tx_reset_queue(txq);
2980 }
2981
2982 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[DESC_INDEX].queue);
2983 netdev_tx_reset_queue(txq);
2984
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002985 bcmgenet_free_rx_buffers(priv);
2986 kfree(priv->rx_cbs);
2987 kfree(priv->tx_cbs);
2988}
2989
2990/* init_edma: Initialize DMA control register */
2991static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
2992{
2993 int ret;
Petri Gynther014012a2015-02-23 11:00:45 -08002994 unsigned int i;
2995 struct enet_cb *cb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002996
Petri Gynther6f5a2722015-03-06 13:45:00 -08002997 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002998
Petri Gynther6f5a2722015-03-06 13:45:00 -08002999 /* Initialize common Rx ring structures */
3000 priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
3001 priv->num_rx_bds = TOTAL_DESC;
3002 priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct enet_cb),
3003 GFP_KERNEL);
3004 if (!priv->rx_cbs)
3005 return -ENOMEM;
3006
3007 for (i = 0; i < priv->num_rx_bds; i++) {
3008 cb = priv->rx_cbs + i;
3009 cb->bd_addr = priv->rx_bds + i * DMA_DESC_SIZE;
3010 }
3011
Brian Norris7fc527f2014-07-29 14:34:14 -07003012 /* Initialize common TX ring structures */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003013 priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
3014 priv->num_tx_bds = TOTAL_DESC;
Florian Fainellic489be02014-07-23 10:42:15 -07003015 priv->tx_cbs = kcalloc(priv->num_tx_bds, sizeof(struct enet_cb),
Florian Fainellic91b7f62014-07-23 10:42:12 -07003016 GFP_KERNEL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003017 if (!priv->tx_cbs) {
Petri Gyntherebbd96f2015-03-25 12:35:11 -07003018 kfree(priv->rx_cbs);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003019 return -ENOMEM;
3020 }
3021
Petri Gynther014012a2015-02-23 11:00:45 -08003022 for (i = 0; i < priv->num_tx_bds; i++) {
3023 cb = priv->tx_cbs + i;
3024 cb->bd_addr = priv->tx_bds + i * DMA_DESC_SIZE;
3025 }
3026
Petri Gyntherebbd96f2015-03-25 12:35:11 -07003027 /* Init rDma */
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003028 bcmgenet_rdma_writel(priv, priv->dma_max_burst_length,
3029 DMA_SCB_BURST_SIZE);
Petri Gyntherebbd96f2015-03-25 12:35:11 -07003030
3031 /* Initialize Rx queues */
3032 ret = bcmgenet_init_rx_queues(priv->dev);
3033 if (ret) {
3034 netdev_err(priv->dev, "failed to initialize Rx queues\n");
3035 bcmgenet_free_rx_buffers(priv);
3036 kfree(priv->rx_cbs);
3037 kfree(priv->tx_cbs);
3038 return ret;
3039 }
3040
3041 /* Init tDma */
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003042 bcmgenet_tdma_writel(priv, priv->dma_max_burst_length,
3043 DMA_SCB_BURST_SIZE);
Petri Gyntherebbd96f2015-03-25 12:35:11 -07003044
Petri Gynther16c6d662015-02-23 11:00:45 -08003045 /* Initialize Tx queues */
3046 bcmgenet_init_tx_queues(priv->dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003047
3048 return 0;
3049}
3050
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003051/* Interrupt bottom half */
3052static void bcmgenet_irq_task(struct work_struct *work)
3053{
Doug Berger07c52d62017-03-09 16:58:47 -08003054 unsigned int status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003055 struct bcmgenet_priv *priv = container_of(
3056 work, struct bcmgenet_priv, bcmgenet_irq_work);
3057
3058 netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
3059
Doug Bergerb0447ec2017-10-25 15:04:17 -07003060 spin_lock_irq(&priv->lock);
Doug Berger07c52d62017-03-09 16:58:47 -08003061 status = priv->irq0_stat;
3062 priv->irq0_stat = 0;
Doug Bergerb0447ec2017-10-25 15:04:17 -07003063 spin_unlock_irq(&priv->lock);
Doug Berger07c52d62017-03-09 16:58:47 -08003064
Doug Berger25382b92019-10-16 16:06:32 -07003065 if (status & UMAC_IRQ_PHY_DET_R &&
Doug Berger0686bd92019-11-05 11:07:26 -08003066 priv->dev->phydev->autoneg != AUTONEG_ENABLE) {
Doug Berger25382b92019-10-16 16:06:32 -07003067 phy_init_hw(priv->dev->phydev);
Doug Berger0686bd92019-11-05 11:07:26 -08003068 genphy_config_aneg(priv->dev->phydev);
3069 }
Doug Berger25382b92019-10-16 16:06:32 -07003070
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003071 /* Link UP/DOWN event */
Doug Berger7de48402019-10-16 16:06:29 -07003072 if (status & UMAC_IRQ_LINK_EVENT)
Heiner Kallweit28b2e0d2018-01-10 21:21:31 +01003073 phy_mac_interrupt(priv->dev->phydev);
Doug Berger25382b92019-10-16 16:06:32 -07003074
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003075}
3076
Petri Gynther4055eae2015-03-25 12:35:16 -07003077/* bcmgenet_isr1: handle Rx and Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003078static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
3079{
3080 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07003081 struct bcmgenet_rx_ring *rx_ring;
3082 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08003083 unsigned int index, status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003084
Doug Berger07c52d62017-03-09 16:58:47 -08003085 /* Read irq status */
3086 status = bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003087 ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07003088
Brian Norris7fc527f2014-07-29 14:34:14 -07003089 /* clear interrupts */
Doug Berger07c52d62017-03-09 16:58:47 -08003090 bcmgenet_intrl2_1_writel(priv, status, INTRL2_CPU_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003091
3092 netif_dbg(priv, intr, priv->dev,
Doug Berger07c52d62017-03-09 16:58:47 -08003093 "%s: IRQ=0x%x\n", __func__, status);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003094
Petri Gynther4055eae2015-03-25 12:35:16 -07003095 /* Check Rx priority queue interrupts */
3096 for (index = 0; index < priv->hw_params->rx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08003097 if (!(status & BIT(UMAC_IRQ1_RX_INTR_SHIFT + index)))
Petri Gynther4055eae2015-03-25 12:35:16 -07003098 continue;
3099
3100 rx_ring = &priv->rx_rings[index];
Florian Fainelli9f4ca052018-03-22 18:19:33 -07003101 rx_ring->dim.event_ctr++;
Petri Gynther4055eae2015-03-25 12:35:16 -07003102
3103 if (likely(napi_schedule_prep(&rx_ring->napi))) {
3104 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07003105 __napi_schedule_irqoff(&rx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07003106 }
3107 }
3108
3109 /* Check Tx priority queue interrupts */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003110 for (index = 0; index < priv->hw_params->tx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08003111 if (!(status & BIT(index)))
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003112 continue;
3113
Petri Gynther4055eae2015-03-25 12:35:16 -07003114 tx_ring = &priv->tx_rings[index];
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003115
Petri Gynther4055eae2015-03-25 12:35:16 -07003116 if (likely(napi_schedule_prep(&tx_ring->napi))) {
3117 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07003118 __napi_schedule_irqoff(&tx_ring->napi);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003119 }
3120 }
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003121
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003122 return IRQ_HANDLED;
3123}
3124
Petri Gynther4055eae2015-03-25 12:35:16 -07003125/* bcmgenet_isr0: handle Rx and Tx default queues + other stuff */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003126static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
3127{
3128 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07003129 struct bcmgenet_rx_ring *rx_ring;
3130 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08003131 unsigned int status;
3132 unsigned long flags;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003133
Doug Berger07c52d62017-03-09 16:58:47 -08003134 /* Read irq status */
3135 status = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003136 ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07003137
Brian Norris7fc527f2014-07-29 14:34:14 -07003138 /* clear interrupts */
Doug Berger07c52d62017-03-09 16:58:47 -08003139 bcmgenet_intrl2_0_writel(priv, status, INTRL2_CPU_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003140
3141 netif_dbg(priv, intr, priv->dev,
Doug Berger07c52d62017-03-09 16:58:47 -08003142 "IRQ=0x%x\n", status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003143
Doug Berger07c52d62017-03-09 16:58:47 -08003144 if (status & UMAC_IRQ_RXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07003145 rx_ring = &priv->rx_rings[DESC_INDEX];
Florian Fainelli9f4ca052018-03-22 18:19:33 -07003146 rx_ring->dim.event_ctr++;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003147
Petri Gynther4055eae2015-03-25 12:35:16 -07003148 if (likely(napi_schedule_prep(&rx_ring->napi))) {
3149 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07003150 __napi_schedule_irqoff(&rx_ring->napi);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003151 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003152 }
Petri Gynther4055eae2015-03-25 12:35:16 -07003153
Doug Berger07c52d62017-03-09 16:58:47 -08003154 if (status & UMAC_IRQ_TXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07003155 tx_ring = &priv->tx_rings[DESC_INDEX];
3156
3157 if (likely(napi_schedule_prep(&tx_ring->napi))) {
3158 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07003159 __napi_schedule_irqoff(&tx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07003160 }
3161 }
3162
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003163 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Doug Berger07c52d62017-03-09 16:58:47 -08003164 status & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003165 wake_up(&priv->wq);
3166 }
3167
Doug Berger07c52d62017-03-09 16:58:47 -08003168 /* all other interested interrupts handled in bottom half */
Doug Berger25382b92019-10-16 16:06:32 -07003169 status &= (UMAC_IRQ_LINK_EVENT | UMAC_IRQ_PHY_DET_R);
Doug Berger07c52d62017-03-09 16:58:47 -08003170 if (status) {
3171 /* Save irq status for bottom-half processing. */
3172 spin_lock_irqsave(&priv->lock, flags);
3173 priv->irq0_stat |= status;
3174 spin_unlock_irqrestore(&priv->lock, flags);
3175
3176 schedule_work(&priv->bcmgenet_irq_work);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003177 }
3178
3179 return IRQ_HANDLED;
3180}
3181
Florian Fainelli85620562014-07-21 15:29:23 -07003182static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
3183{
Doug Bergereb236c22020-04-30 16:26:51 -07003184 /* Acknowledge the interrupt */
Florian Fainelli85620562014-07-21 15:29:23 -07003185 return IRQ_HANDLED;
3186}
3187
Florian Fainelli4d2e8882015-07-31 11:42:54 -07003188#ifdef CONFIG_NET_POLL_CONTROLLER
3189static void bcmgenet_poll_controller(struct net_device *dev)
3190{
3191 struct bcmgenet_priv *priv = netdev_priv(dev);
3192
3193 /* Invoke the main RX/TX interrupt handler */
3194 disable_irq(priv->irq0);
3195 bcmgenet_isr0(priv->irq0, priv);
3196 enable_irq(priv->irq0);
3197
3198 /* And the interrupt handler for RX/TX priority queues */
3199 disable_irq(priv->irq1);
3200 bcmgenet_isr1(priv->irq1, priv);
3201 enable_irq(priv->irq1);
3202}
3203#endif
3204
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003205static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
3206{
3207 u32 reg;
3208
3209 reg = bcmgenet_rbuf_ctrl_get(priv);
3210 reg |= BIT(1);
3211 bcmgenet_rbuf_ctrl_set(priv, reg);
3212 udelay(10);
3213
3214 reg &= ~BIT(1);
3215 bcmgenet_rbuf_ctrl_set(priv, reg);
3216 udelay(10);
3217}
3218
3219static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003220 unsigned char *addr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003221{
Andy Shevchenkod2af1422020-04-21 00:51:20 +03003222 bcmgenet_umac_writel(priv, get_unaligned_be32(&addr[0]), UMAC_MAC0);
3223 bcmgenet_umac_writel(priv, get_unaligned_be16(&addr[4]), UMAC_MAC1);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003224}
3225
Jeremy Linton26bd9cc2020-02-24 16:54:02 -06003226static void bcmgenet_get_hw_addr(struct bcmgenet_priv *priv,
3227 unsigned char *addr)
3228{
3229 u32 addr_tmp;
3230
3231 addr_tmp = bcmgenet_umac_readl(priv, UMAC_MAC0);
Andy Shevchenkod2af1422020-04-21 00:51:20 +03003232 put_unaligned_be32(addr_tmp, &addr[0]);
Jeremy Linton26bd9cc2020-02-24 16:54:02 -06003233 addr_tmp = bcmgenet_umac_readl(priv, UMAC_MAC1);
Andy Shevchenkod2af1422020-04-21 00:51:20 +03003234 put_unaligned_be16(addr_tmp, &addr[4]);
Jeremy Linton26bd9cc2020-02-24 16:54:02 -06003235}
3236
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003237/* Returns a reusable dma control register value */
3238static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
3239{
3240 u32 reg;
3241 u32 dma_ctrl;
3242
3243 /* disable DMA */
3244 dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
3245 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
3246 reg &= ~dma_ctrl;
3247 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
3248
3249 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
3250 reg &= ~dma_ctrl;
3251 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
3252
3253 bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
3254 udelay(10);
3255 bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
3256
3257 return dma_ctrl;
3258}
3259
3260static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
3261{
3262 u32 reg;
3263
3264 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
3265 reg |= dma_ctrl;
3266 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
3267
3268 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
3269 reg |= dma_ctrl;
3270 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
3271}
3272
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003273static void bcmgenet_netif_start(struct net_device *dev)
3274{
3275 struct bcmgenet_priv *priv = netdev_priv(dev);
3276
3277 /* Start the network engine */
Doug Berger72f96342020-04-29 13:02:00 -07003278 bcmgenet_set_rx_mode(dev);
Petri Gynther3ab11332015-03-25 12:35:15 -07003279 bcmgenet_enable_rx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003280
3281 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
3282
Doug Bergerd215dba2017-10-25 15:04:16 -07003283 bcmgenet_enable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003284
Florian Fainelli37850e32015-10-17 14:22:46 -07003285 /* Monitor link interrupts now */
3286 bcmgenet_link_intr_enable(priv);
3287
Doug Berger6c97f012017-10-25 15:04:19 -07003288 phy_start(dev->phydev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003289}
3290
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003291static int bcmgenet_open(struct net_device *dev)
3292{
3293 struct bcmgenet_priv *priv = netdev_priv(dev);
3294 unsigned long dma_ctrl;
3295 u32 reg;
3296 int ret;
3297
3298 netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
3299
3300 /* Turn on the clock */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003301 clk_prepare_enable(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003302
Florian Fainellia642c4f2015-03-23 15:09:56 -07003303 /* If this is an internal GPHY, power it back on now, before UniMAC is
3304 * brought out of reset as absolutely no UniMAC activity is allowed
3305 */
Florian Fainellic624f892015-07-16 15:51:17 -07003306 if (priv->internal_phy)
Florian Fainellia642c4f2015-03-23 15:09:56 -07003307 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3308
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003309 /* take MAC out of reset */
3310 bcmgenet_umac_reset(priv);
3311
Doug Berger28c2d1a2017-10-25 15:04:13 -07003312 init_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003313
Doug Berger206f54b2019-12-17 16:51:12 -08003314 /* Apply features again in case we changed them while interface was
3315 * down
3316 */
3317 bcmgenet_set_features(dev, dev->features);
3318
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003319 bcmgenet_set_hw_addr(priv, dev->dev_addr);
3320
Florian Fainellic624f892015-07-16 15:51:17 -07003321 if (priv->internal_phy) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003322 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
3323 reg |= EXT_ENERGY_DET_MASK;
3324 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
3325 }
3326
3327 /* Disable RX/TX DMA and flush TX queues */
3328 dma_ctrl = bcmgenet_dma_disable(priv);
3329
3330 /* Reinitialize TDMA and RDMA and SW housekeeping */
3331 ret = bcmgenet_init_dma(priv);
3332 if (ret) {
3333 netdev_err(dev, "failed to initialize DMA\n");
Doug Berger6b6d017f2019-11-05 11:07:25 -08003334 goto err_clk_disable;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003335 }
3336
3337 /* Always enable ring 16 - descriptor ring */
3338 bcmgenet_enable_dma(priv, dma_ctrl);
3339
Petri Gynther0034de42015-03-13 14:45:00 -07003340 /* HFB init */
3341 bcmgenet_hfb_init(priv);
3342
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003343 ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003344 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003345 if (ret < 0) {
3346 netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
3347 goto err_fini_dma;
3348 }
3349
3350 ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003351 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003352 if (ret < 0) {
3353 netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
3354 goto err_irq0;
3355 }
3356
Doug Berger6b6d017f2019-11-05 11:07:25 -08003357 ret = bcmgenet_mii_probe(dev);
3358 if (ret) {
3359 netdev_err(dev, "failed to connect to PHY\n");
3360 goto err_irq1;
3361 }
3362
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003363 bcmgenet_netif_start(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003364
Doug Berger09e805d2018-11-01 15:55:37 -07003365 netif_tx_start_all_queues(dev);
3366
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003367 return 0;
3368
Doug Berger6b6d017f2019-11-05 11:07:25 -08003369err_irq1:
3370 free_irq(priv->irq1, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003371err_irq0:
Florian Fainelli978ffac42015-07-16 15:51:15 -07003372 free_irq(priv->irq0, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003373err_fini_dma:
Doug Berger4fd6dc92017-10-25 15:04:12 -07003374 bcmgenet_dma_teardown(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003375 bcmgenet_fini_dma(priv);
3376err_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08003377 if (priv->internal_phy)
3378 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003379 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003380 return ret;
3381}
3382
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003383static void bcmgenet_netif_stop(struct net_device *dev)
3384{
3385 struct bcmgenet_priv *priv = netdev_priv(dev);
3386
Doug Bergerd215dba2017-10-25 15:04:16 -07003387 bcmgenet_disable_tx_napi(priv);
Doug Berger09e805d2018-11-01 15:55:37 -07003388 netif_tx_disable(dev);
Doug Bergerd215dba2017-10-25 15:04:16 -07003389
3390 /* Disable MAC receive */
3391 umac_enable_set(priv, CMD_RX_EN, false);
3392
3393 bcmgenet_dma_teardown(priv);
3394
3395 /* Disable MAC transmit. TX DMA disabled must be done before this */
3396 umac_enable_set(priv, CMD_TX_EN, false);
3397
Doug Berger6c97f012017-10-25 15:04:19 -07003398 phy_stop(dev->phydev);
Petri Gynther3ab11332015-03-25 12:35:15 -07003399 bcmgenet_disable_rx_napi(priv);
Doug Bergerfbf557d2017-10-25 15:04:15 -07003400 bcmgenet_intr_disable(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003401
3402 /* Wait for pending work items to complete. Since interrupts are
3403 * disabled no new work will be scheduled.
3404 */
3405 cancel_work_sync(&priv->bcmgenet_irq_work);
Florian Fainellicc013fb2014-08-11 14:50:43 -07003406
Florian Fainellicc013fb2014-08-11 14:50:43 -07003407 priv->old_link = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07003408 priv->old_speed = -1;
Florian Fainellicc013fb2014-08-11 14:50:43 -07003409 priv->old_duplex = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07003410 priv->old_pause = -1;
Doug Bergerd215dba2017-10-25 15:04:16 -07003411
3412 /* tx reclaim */
3413 bcmgenet_tx_reclaim_all(dev);
3414 bcmgenet_fini_dma(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003415}
3416
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003417static int bcmgenet_close(struct net_device *dev)
3418{
3419 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Bergerd215dba2017-10-25 15:04:16 -07003420 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003421
3422 netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
3423
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003424 bcmgenet_netif_stop(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003425
Florian Fainellic96e7312014-11-10 18:06:20 -08003426 /* Really kill the PHY state machine and disconnect from it */
Doug Berger6c97f012017-10-25 15:04:19 -07003427 phy_disconnect(dev->phydev);
Florian Fainellic96e7312014-11-10 18:06:20 -08003428
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003429 free_irq(priv->irq0, priv);
3430 free_irq(priv->irq1, priv);
3431
Florian Fainellic624f892015-07-16 15:51:17 -07003432 if (priv->internal_phy)
Florian Fainellica8cf342015-03-23 15:09:51 -07003433 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003434
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003435 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003436
Florian Fainellica8cf342015-03-23 15:09:51 -07003437 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003438}
3439
Florian Fainelli13ea6572015-06-04 16:15:50 -07003440static void bcmgenet_dump_tx_queue(struct bcmgenet_tx_ring *ring)
3441{
3442 struct bcmgenet_priv *priv = ring->priv;
3443 u32 p_index, c_index, intsts, intmsk;
3444 struct netdev_queue *txq;
3445 unsigned int free_bds;
Florian Fainelli13ea6572015-06-04 16:15:50 -07003446 bool txq_stopped;
3447
3448 if (!netif_msg_tx_err(priv))
3449 return;
3450
3451 txq = netdev_get_tx_queue(priv->dev, ring->queue);
3452
Doug Bergerb0447ec2017-10-25 15:04:17 -07003453 spin_lock(&ring->lock);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003454 if (ring->index == DESC_INDEX) {
3455 intsts = ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
3456 intmsk = UMAC_IRQ_TXDMA_DONE | UMAC_IRQ_TXDMA_MBDONE;
3457 } else {
3458 intsts = ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
3459 intmsk = 1 << ring->index;
3460 }
3461 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
3462 p_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_PROD_INDEX);
3463 txq_stopped = netif_tx_queue_stopped(txq);
3464 free_bds = ring->free_bds;
Doug Bergerb0447ec2017-10-25 15:04:17 -07003465 spin_unlock(&ring->lock);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003466
3467 netif_err(priv, tx_err, priv->dev, "Ring %d queue %d status summary\n"
3468 "TX queue status: %s, interrupts: %s\n"
3469 "(sw)free_bds: %d (sw)size: %d\n"
3470 "(sw)p_index: %d (hw)p_index: %d\n"
3471 "(sw)c_index: %d (hw)c_index: %d\n"
3472 "(sw)clean_p: %d (sw)write_p: %d\n"
3473 "(sw)cb_ptr: %d (sw)end_ptr: %d\n",
3474 ring->index, ring->queue,
3475 txq_stopped ? "stopped" : "active",
3476 intsts & intmsk ? "enabled" : "disabled",
3477 free_bds, ring->size,
3478 ring->prod_index, p_index & DMA_P_INDEX_MASK,
3479 ring->c_index, c_index & DMA_C_INDEX_MASK,
3480 ring->clean_ptr, ring->write_ptr,
3481 ring->cb_ptr, ring->end_ptr);
3482}
3483
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05003484static void bcmgenet_timeout(struct net_device *dev, unsigned int txqueue)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003485{
3486 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003487 u32 int0_enable = 0;
3488 u32 int1_enable = 0;
3489 unsigned int q;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003490
3491 netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
3492
Florian Fainelli13ea6572015-06-04 16:15:50 -07003493 for (q = 0; q < priv->hw_params->tx_queues; q++)
3494 bcmgenet_dump_tx_queue(&priv->tx_rings[q]);
3495 bcmgenet_dump_tx_queue(&priv->tx_rings[DESC_INDEX]);
3496
3497 bcmgenet_tx_reclaim_all(dev);
3498
3499 for (q = 0; q < priv->hw_params->tx_queues; q++)
3500 int1_enable |= (1 << q);
3501
3502 int0_enable = UMAC_IRQ_TXDMA_DONE;
3503
3504 /* Re-enable TX interrupts if disabled */
3505 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
3506 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
3507
Florian Westphal860e9532016-05-03 16:33:13 +02003508 netif_trans_update(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003509
3510 dev->stats.tx_errors++;
3511
3512 netif_tx_wake_all_queues(dev);
3513}
3514
Justin Chen35cbef92019-07-17 14:58:53 -07003515#define MAX_MDF_FILTER 17
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003516
3517static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
3518 unsigned char *addr,
Justin Chen35cbef92019-07-17 14:58:53 -07003519 int *i)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003520{
Florian Fainellic91b7f62014-07-23 10:42:12 -07003521 bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
3522 UMAC_MDF_ADDR + (*i * 4));
3523 bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
3524 addr[4] << 8 | addr[5],
3525 UMAC_MDF_ADDR + ((*i + 1) * 4));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003526 *i += 2;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003527}
3528
3529static void bcmgenet_set_rx_mode(struct net_device *dev)
3530{
3531 struct bcmgenet_priv *priv = netdev_priv(dev);
3532 struct netdev_hw_addr *ha;
Justin Chen35cbef92019-07-17 14:58:53 -07003533 int i, nfilter;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003534 u32 reg;
3535
3536 netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
3537
Justin Chen35cbef92019-07-17 14:58:53 -07003538 /* Number of filters needed */
3539 nfilter = netdev_uc_count(dev) + netdev_mc_count(dev) + 2;
3540
3541 /*
3542 * Turn on promicuous mode for three scenarios
3543 * 1. IFF_PROMISC flag is set
3544 * 2. IFF_ALLMULTI flag is set
3545 * 3. The number of filters needed exceeds the number filters
3546 * supported by the hardware.
3547 */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003548 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
Justin Chen35cbef92019-07-17 14:58:53 -07003549 if ((dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) ||
3550 (nfilter > MAX_MDF_FILTER)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003551 reg |= CMD_PROMISC;
3552 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3553 bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
3554 return;
3555 } else {
3556 reg &= ~CMD_PROMISC;
3557 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3558 }
3559
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003560 /* update MDF filter */
3561 i = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003562 /* Broadcast */
Justin Chen35cbef92019-07-17 14:58:53 -07003563 bcmgenet_set_mdf_addr(priv, dev->broadcast, &i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003564 /* my own address.*/
Justin Chen35cbef92019-07-17 14:58:53 -07003565 bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003566
Justin Chen35cbef92019-07-17 14:58:53 -07003567 /* Unicast */
3568 netdev_for_each_uc_addr(ha, dev)
3569 bcmgenet_set_mdf_addr(priv, ha->addr, &i);
3570
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003571 /* Multicast */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003572 netdev_for_each_mc_addr(ha, dev)
Justin Chen35cbef92019-07-17 14:58:53 -07003573 bcmgenet_set_mdf_addr(priv, ha->addr, &i);
3574
3575 /* Enable filters */
3576 reg = GENMASK(MAX_MDF_FILTER - 1, MAX_MDF_FILTER - nfilter);
3577 bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003578}
3579
3580/* Set the hardware MAC address. */
3581static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
3582{
3583 struct sockaddr *addr = p;
3584
3585 /* Setting the MAC address at the hardware level is not possible
3586 * without disabling the UniMAC RX/TX enable bits.
3587 */
3588 if (netif_running(dev))
3589 return -EBUSY;
3590
3591 ether_addr_copy(dev->dev_addr, addr->sa_data);
3592
3593 return 0;
3594}
3595
Florian Fainelli37a30b42017-03-16 10:27:08 -07003596static struct net_device_stats *bcmgenet_get_stats(struct net_device *dev)
3597{
3598 struct bcmgenet_priv *priv = netdev_priv(dev);
3599 unsigned long tx_bytes = 0, tx_packets = 0;
3600 unsigned long rx_bytes = 0, rx_packets = 0;
3601 unsigned long rx_errors = 0, rx_dropped = 0;
3602 struct bcmgenet_tx_ring *tx_ring;
3603 struct bcmgenet_rx_ring *rx_ring;
3604 unsigned int q;
3605
3606 for (q = 0; q < priv->hw_params->tx_queues; q++) {
3607 tx_ring = &priv->tx_rings[q];
3608 tx_bytes += tx_ring->bytes;
3609 tx_packets += tx_ring->packets;
3610 }
3611 tx_ring = &priv->tx_rings[DESC_INDEX];
3612 tx_bytes += tx_ring->bytes;
3613 tx_packets += tx_ring->packets;
3614
3615 for (q = 0; q < priv->hw_params->rx_queues; q++) {
3616 rx_ring = &priv->rx_rings[q];
3617
3618 rx_bytes += rx_ring->bytes;
3619 rx_packets += rx_ring->packets;
3620 rx_errors += rx_ring->errors;
3621 rx_dropped += rx_ring->dropped;
3622 }
3623 rx_ring = &priv->rx_rings[DESC_INDEX];
3624 rx_bytes += rx_ring->bytes;
3625 rx_packets += rx_ring->packets;
3626 rx_errors += rx_ring->errors;
3627 rx_dropped += rx_ring->dropped;
3628
3629 dev->stats.tx_bytes = tx_bytes;
3630 dev->stats.tx_packets = tx_packets;
3631 dev->stats.rx_bytes = rx_bytes;
3632 dev->stats.rx_packets = rx_packets;
3633 dev->stats.rx_errors = rx_errors;
3634 dev->stats.rx_missed_errors = rx_errors;
Doug Bergera6d0b832020-04-23 15:44:17 -07003635 dev->stats.rx_dropped = rx_dropped;
Florian Fainelli37a30b42017-03-16 10:27:08 -07003636 return &dev->stats;
3637}
3638
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003639static const struct net_device_ops bcmgenet_netdev_ops = {
3640 .ndo_open = bcmgenet_open,
3641 .ndo_stop = bcmgenet_close,
3642 .ndo_start_xmit = bcmgenet_xmit,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003643 .ndo_tx_timeout = bcmgenet_timeout,
3644 .ndo_set_rx_mode = bcmgenet_set_rx_mode,
3645 .ndo_set_mac_address = bcmgenet_set_mac_addr,
Heiner Kallweitfd786fb12020-01-21 22:09:33 +01003646 .ndo_do_ioctl = phy_do_ioctl_running,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003647 .ndo_set_features = bcmgenet_set_features,
Florian Fainelli4d2e8882015-07-31 11:42:54 -07003648#ifdef CONFIG_NET_POLL_CONTROLLER
3649 .ndo_poll_controller = bcmgenet_poll_controller,
3650#endif
Florian Fainelli37a30b42017-03-16 10:27:08 -07003651 .ndo_get_stats = bcmgenet_get_stats,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003652};
3653
3654/* Array of GENET hardware parameters/characteristics */
3655static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
3656 [GENET_V1] = {
3657 .tx_queues = 0,
Petri Gynther51a966a2015-02-23 11:00:46 -08003658 .tx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003659 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003660 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003661 .bp_in_en_shift = 16,
3662 .bp_in_mask = 0xffff,
3663 .hfb_filter_cnt = 16,
3664 .qtag_mask = 0x1F,
3665 .hfb_offset = 0x1000,
3666 .rdma_offset = 0x2000,
3667 .tdma_offset = 0x3000,
3668 .words_per_bd = 2,
3669 },
3670 [GENET_V2] = {
3671 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003672 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003673 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003674 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003675 .bp_in_en_shift = 16,
3676 .bp_in_mask = 0xffff,
3677 .hfb_filter_cnt = 16,
3678 .qtag_mask = 0x1F,
3679 .tbuf_offset = 0x0600,
3680 .hfb_offset = 0x1000,
3681 .hfb_reg_offset = 0x2000,
3682 .rdma_offset = 0x3000,
3683 .tdma_offset = 0x4000,
3684 .words_per_bd = 2,
3685 .flags = GENET_HAS_EXT,
3686 },
3687 [GENET_V3] = {
3688 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003689 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003690 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003691 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003692 .bp_in_en_shift = 17,
3693 .bp_in_mask = 0x1ffff,
3694 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003695 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003696 .qtag_mask = 0x3F,
3697 .tbuf_offset = 0x0600,
3698 .hfb_offset = 0x8000,
3699 .hfb_reg_offset = 0xfc00,
3700 .rdma_offset = 0x10000,
3701 .tdma_offset = 0x11000,
3702 .words_per_bd = 2,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003703 .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR |
3704 GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003705 },
3706 [GENET_V4] = {
3707 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003708 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003709 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003710 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003711 .bp_in_en_shift = 17,
3712 .bp_in_mask = 0x1ffff,
3713 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003714 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003715 .qtag_mask = 0x3F,
3716 .tbuf_offset = 0x0600,
3717 .hfb_offset = 0x8000,
3718 .hfb_reg_offset = 0xfc00,
3719 .rdma_offset = 0x2000,
3720 .tdma_offset = 0x4000,
3721 .words_per_bd = 3,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003722 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3723 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003724 },
Doug Berger42138082017-03-13 17:41:42 -07003725 [GENET_V5] = {
3726 .tx_queues = 4,
3727 .tx_bds_per_q = 32,
3728 .rx_queues = 0,
3729 .rx_bds_per_q = 0,
3730 .bp_in_en_shift = 17,
3731 .bp_in_mask = 0x1ffff,
3732 .hfb_filter_cnt = 48,
3733 .hfb_filter_size = 128,
3734 .qtag_mask = 0x3F,
3735 .tbuf_offset = 0x0600,
3736 .hfb_offset = 0x8000,
3737 .hfb_reg_offset = 0xfc00,
3738 .rdma_offset = 0x2000,
3739 .tdma_offset = 0x4000,
3740 .words_per_bd = 3,
3741 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3742 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
3743 },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003744};
3745
3746/* Infer hardware parameters from the detected GENET version */
3747static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
3748{
3749 struct bcmgenet_hw_params *params;
3750 u32 reg;
3751 u8 major;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003752 u16 gphy_rev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003753
Doug Berger42138082017-03-13 17:41:42 -07003754 if (GENET_IS_V5(priv) || GENET_IS_V4(priv)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003755 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3756 genet_dma_ring_regs = genet_dma_ring_regs_v4;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003757 } else if (GENET_IS_V3(priv)) {
3758 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3759 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003760 } else if (GENET_IS_V2(priv)) {
3761 bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
3762 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003763 } else if (GENET_IS_V1(priv)) {
3764 bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
3765 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003766 }
3767
3768 /* enum genet_version starts at 1 */
3769 priv->hw_params = &bcmgenet_hw_params[priv->version];
3770 params = priv->hw_params;
3771
3772 /* Read GENET HW version */
3773 reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
3774 major = (reg >> 24 & 0x0f);
Doug Berger42138082017-03-13 17:41:42 -07003775 if (major == 6)
3776 major = 5;
3777 else if (major == 5)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003778 major = 4;
3779 else if (major == 0)
3780 major = 1;
3781 if (major != priv->version) {
3782 dev_err(&priv->pdev->dev,
3783 "GENET version mismatch, got: %d, configured for: %d\n",
3784 major, priv->version);
3785 }
3786
3787 /* Print the GENET core version */
3788 dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003789 major, (reg >> 16) & 0x0f, reg & 0xffff);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003790
Florian Fainelli487320c2014-09-19 13:07:53 -07003791 /* Store the integrated PHY revision for the MDIO probing function
3792 * to pass this information to the PHY driver. The PHY driver expects
3793 * to find the PHY major revision in bits 15:8 while the GENET register
3794 * stores that information in bits 7:0, account for that.
Florian Fainellib04a2f52014-12-03 09:56:59 -08003795 *
3796 * On newer chips, starting with PHY revision G0, a new scheme is
3797 * deployed similar to the Starfighter 2 switch with GPHY major
3798 * revision in bits 15:8 and patch level in bits 7:0. Major revision 0
3799 * is reserved as well as special value 0x01ff, we have a small
3800 * heuristic to check for the new GPHY revision and re-arrange things
3801 * so the GPHY driver is happy.
Florian Fainelli487320c2014-09-19 13:07:53 -07003802 */
Florian Fainellib04a2f52014-12-03 09:56:59 -08003803 gphy_rev = reg & 0xffff;
3804
Doug Berger42138082017-03-13 17:41:42 -07003805 if (GENET_IS_V5(priv)) {
3806 /* The EPHY revision should come from the MDIO registers of
3807 * the PHY not from GENET.
3808 */
3809 if (gphy_rev != 0) {
3810 pr_warn("GENET is reporting EPHY revision: 0x%04x\n",
3811 gphy_rev);
3812 }
Doug Bergereca4bad2017-03-09 16:58:45 -08003813 /* This is reserved so should require special treatment */
David S. Miller101c4312017-03-15 11:59:10 -07003814 } else if (gphy_rev == 0 || gphy_rev == 0x01ff) {
Doug Bergereca4bad2017-03-09 16:58:45 -08003815 pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev);
3816 return;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003817 /* This is the good old scheme, just GPHY major, no minor nor patch */
Doug Berger42138082017-03-13 17:41:42 -07003818 } else if ((gphy_rev & 0xf0) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003819 priv->gphy_rev = gphy_rev << 8;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003820 /* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */
Doug Berger42138082017-03-13 17:41:42 -07003821 } else if ((gphy_rev & 0xff00) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003822 priv->gphy_rev = gphy_rev;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003823 }
Florian Fainelli487320c2014-09-19 13:07:53 -07003824
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003825#ifdef CONFIG_PHYS_ADDR_T_64BIT
3826 if (!(params->flags & GENET_HAS_40BITS))
3827 pr_warn("GENET does not support 40-bits PA\n");
3828#endif
3829
3830 pr_debug("Configuration for version: %d\n"
Petri Gynther3feafa02015-03-05 17:40:14 -08003831 "TXq: %1d, TXqBDs: %1d, RXq: %1d, RXqBDs: %1d\n"
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003832 "BP << en: %2d, BP msk: 0x%05x\n"
3833 "HFB count: %2d, QTAQ msk: 0x%05x\n"
3834 "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
3835 "RDMA: 0x%05x, TDMA: 0x%05x\n"
3836 "Words/BD: %d\n",
3837 priv->version,
Petri Gynther51a966a2015-02-23 11:00:46 -08003838 params->tx_queues, params->tx_bds_per_q,
Petri Gynther3feafa02015-03-05 17:40:14 -08003839 params->rx_queues, params->rx_bds_per_q,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003840 params->bp_in_en_shift, params->bp_in_mask,
3841 params->hfb_filter_cnt, params->qtag_mask,
3842 params->tbuf_offset, params->hfb_offset,
3843 params->hfb_reg_offset,
3844 params->rdma_offset, params->tdma_offset,
3845 params->words_per_bd);
3846}
3847
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003848struct bcmgenet_plat_data {
3849 enum bcmgenet_version version;
3850 u32 dma_max_burst_length;
3851};
3852
3853static const struct bcmgenet_plat_data v1_plat_data = {
3854 .version = GENET_V1,
3855 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3856};
3857
3858static const struct bcmgenet_plat_data v2_plat_data = {
3859 .version = GENET_V2,
3860 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3861};
3862
3863static const struct bcmgenet_plat_data v3_plat_data = {
3864 .version = GENET_V3,
3865 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3866};
3867
3868static const struct bcmgenet_plat_data v4_plat_data = {
3869 .version = GENET_V4,
3870 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3871};
3872
3873static const struct bcmgenet_plat_data v5_plat_data = {
3874 .version = GENET_V5,
3875 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3876};
3877
3878static const struct bcmgenet_plat_data bcm2711_plat_data = {
3879 .version = GENET_V5,
3880 .dma_max_burst_length = 0x08,
3881};
3882
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003883static const struct of_device_id bcmgenet_match[] = {
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003884 { .compatible = "brcm,genet-v1", .data = &v1_plat_data },
3885 { .compatible = "brcm,genet-v2", .data = &v2_plat_data },
3886 { .compatible = "brcm,genet-v3", .data = &v3_plat_data },
3887 { .compatible = "brcm,genet-v4", .data = &v4_plat_data },
3888 { .compatible = "brcm,genet-v5", .data = &v5_plat_data },
3889 { .compatible = "brcm,bcm2711-genet-v5", .data = &bcm2711_plat_data },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003890 { },
3891};
Luis de Bethencourte8048e52015-09-18 17:55:02 +02003892MODULE_DEVICE_TABLE(of, bcmgenet_match);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003893
3894static int bcmgenet_probe(struct platform_device *pdev)
3895{
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003896 struct bcmgenet_platform_data *pd = pdev->dev.platform_data;
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003897 const struct bcmgenet_plat_data *pdata;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003898 struct bcmgenet_priv *priv;
3899 struct net_device *dev;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07003900 unsigned int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003901 int err = -EIO;
3902
Petri Gynther3feafee2015-03-05 17:40:12 -08003903 /* Up to GENET_MAX_MQ_CNT + 1 TX queues and RX queues */
3904 dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1,
3905 GENET_MAX_MQ_CNT + 1);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003906 if (!dev) {
3907 dev_err(&pdev->dev, "can't allocate net device\n");
3908 return -ENOMEM;
3909 }
3910
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003911 priv = netdev_priv(dev);
3912 priv->irq0 = platform_get_irq(pdev, 0);
Stefan Wahren2b65f932019-11-11 20:49:21 +01003913 if (priv->irq0 < 0) {
3914 err = priv->irq0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003915 goto err;
3916 }
Stefan Wahren2b65f932019-11-11 20:49:21 +01003917 priv->irq1 = platform_get_irq(pdev, 1);
3918 if (priv->irq1 < 0) {
3919 err = priv->irq1;
3920 goto err;
3921 }
3922 priv->wol_irq = platform_get_irq_optional(pdev, 2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003923
YueHaibing4ca33482019-08-21 21:41:31 +08003924 priv->base = devm_platform_ioremap_resource(pdev, 0);
Fabio Estevam5343a102014-02-24 00:47:24 -03003925 if (IS_ERR(priv->base)) {
3926 err = PTR_ERR(priv->base);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003927 goto err;
3928 }
3929
Doug Berger07c52d62017-03-09 16:58:47 -08003930 spin_lock_init(&priv->lock);
3931
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003932 SET_NETDEV_DEV(dev, &pdev->dev);
3933 dev_set_drvdata(&pdev->dev, dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003934 dev->watchdog_timeo = 2 * HZ;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003935 dev->ethtool_ops = &bcmgenet_ethtool_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003936 dev->netdev_ops = &bcmgenet_netdev_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003937
3938 priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
3939
Doug Bergerae895c42019-12-17 16:51:13 -08003940 /* Set default features */
3941 dev->features |= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
3942 NETIF_F_RXCSUM;
3943 dev->hw_features |= dev->features;
3944 dev->vlan_features |= dev->features;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003945
Florian Fainelli85620562014-07-21 15:29:23 -07003946 /* Request the WOL interrupt and advertise suspend if available */
3947 priv->wol_irq_disabled = true;
3948 err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0,
3949 dev->name, priv);
3950 if (!err)
3951 device_set_wakeup_capable(&pdev->dev, 1);
3952
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003953 /* Set the needed headroom to account for any possible
3954 * features enabling/disabling at runtime
3955 */
3956 dev->needed_headroom += 64;
3957
3958 netdev_boot_setup_check(dev);
3959
3960 priv->dev = dev;
3961 priv->pdev = pdev;
Jeremy Linton99c6b062020-02-24 16:54:01 -06003962
3963 pdata = device_get_match_data(&pdev->dev);
3964 if (pdata) {
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003965 priv->version = pdata->version;
3966 priv->dma_max_burst_length = pdata->dma_max_burst_length;
3967 } else {
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003968 priv->version = pd->genet_version;
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003969 priv->dma_max_burst_length = DMA_MAX_BURST_LENGTH;
3970 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003971
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03003972 priv->clk = devm_clk_get_optional(&priv->pdev->dev, "enet");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003973 if (IS_ERR(priv->clk)) {
Jeremy Lintonae200c22020-02-24 16:54:03 -06003974 dev_dbg(&priv->pdev->dev, "failed to get enet clock\n");
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03003975 err = PTR_ERR(priv->clk);
3976 goto err;
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003977 }
Florian Fainellie4a60a92014-08-11 14:50:42 -07003978
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03003979 err = clk_prepare_enable(priv->clk);
3980 if (err)
3981 goto err;
Florian Fainellie4a60a92014-08-11 14:50:42 -07003982
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003983 bcmgenet_set_hw_params(priv);
3984
Doug Berger99d55632019-12-17 16:51:08 -08003985 err = -EIO;
3986 if (priv->hw_params->flags & GENET_HAS_40BITS)
3987 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
3988 if (err)
3989 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3990 if (err)
Zhang Changzhong24a63fe2020-07-20 15:18:43 +08003991 goto err_clk_disable;
Doug Berger99d55632019-12-17 16:51:08 -08003992
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003993 /* Mii wait queue */
3994 init_waitqueue_head(&priv->wq);
3995 /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
3996 priv->rx_buf_len = RX_BUF_LENGTH;
3997 INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
3998
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03003999 priv->clk_wol = devm_clk_get_optional(&priv->pdev->dev, "enet-wol");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07004000 if (IS_ERR(priv->clk_wol)) {
Jeremy Lintonae200c22020-02-24 16:54:03 -06004001 dev_dbg(&priv->pdev->dev, "failed to get enet-wol clock\n");
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03004002 err = PTR_ERR(priv->clk_wol);
Zhang Changzhong53a92882020-07-20 17:36:34 +08004003 goto err_clk_disable;
Florian Fainelli7d5d3072015-07-22 17:28:23 -07004004 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004005
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03004006 priv->clk_eee = devm_clk_get_optional(&priv->pdev->dev, "enet-eee");
Florian Fainelli6ef398e2014-11-25 21:16:35 -08004007 if (IS_ERR(priv->clk_eee)) {
Jeremy Lintonae200c22020-02-24 16:54:03 -06004008 dev_dbg(&priv->pdev->dev, "failed to get enet-eee clock\n");
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03004009 err = PTR_ERR(priv->clk_eee);
Zhang Changzhong53a92882020-07-20 17:36:34 +08004010 goto err_clk_disable;
Florian Fainelli6ef398e2014-11-25 21:16:35 -08004011 }
4012
Doug Berger6be371b2017-03-09 16:58:48 -08004013 /* If this is an internal GPHY, power it on now, before UniMAC is
4014 * brought out of reset as absolutely no UniMAC activity is allowed
4015 */
Jeremy Linton99c6b062020-02-24 16:54:01 -06004016 if (device_get_phy_mode(&pdev->dev) == PHY_INTERFACE_MODE_INTERNAL)
Doug Berger6be371b2017-03-09 16:58:48 -08004017 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
4018
Andy Shevchenko7d3cca72020-04-21 00:51:21 +03004019 if (pd && !IS_ERR_OR_NULL(pd->mac_address))
Jeremy Linton26bd9cc2020-02-24 16:54:02 -06004020 ether_addr_copy(dev->dev_addr, pd->mac_address);
4021 else
4022 if (!device_get_mac_address(&pdev->dev, dev->dev_addr, ETH_ALEN))
4023 if (has_acpi_companion(&pdev->dev))
4024 bcmgenet_get_hw_addr(priv, dev->dev_addr);
4025
4026 if (!is_valid_ether_addr(dev->dev_addr)) {
4027 dev_warn(&pdev->dev, "using random Ethernet MAC\n");
4028 eth_hw_addr_random(dev);
4029 }
4030
Doug Berger28c2d1a2017-10-25 15:04:13 -07004031 reset_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004032
4033 err = bcmgenet_mii_init(dev);
4034 if (err)
4035 goto err_clk_disable;
4036
4037 /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
4038 * just the ring 16 descriptor based TX
4039 */
4040 netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
4041 netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
4042
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07004043 /* Set default coalescing parameters */
4044 for (i = 0; i < priv->hw_params->rx_queues; i++)
4045 priv->rx_rings[i].rx_max_coalesced_frames = 1;
4046 priv->rx_rings[DESC_INDEX].rx_max_coalesced_frames = 1;
4047
Florian Fainelli219575e2014-06-26 10:26:21 -07004048 /* libphy will determine the link state */
4049 netif_carrier_off(dev);
4050
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004051 /* Turn off the main clock, WOL clock is handled separately */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07004052 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004053
Florian Fainelli0f50ce92014-06-26 10:26:20 -07004054 err = register_netdev(dev);
4055 if (err)
4056 goto err;
4057
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004058 return err;
4059
4060err_clk_disable:
Florian Fainelli7d5d3072015-07-22 17:28:23 -07004061 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004062err:
4063 free_netdev(dev);
4064 return err;
4065}
4066
4067static int bcmgenet_remove(struct platform_device *pdev)
4068{
4069 struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
4070
4071 dev_set_drvdata(&pdev->dev, NULL);
4072 unregister_netdev(priv->dev);
4073 bcmgenet_mii_exit(priv->dev);
4074 free_netdev(priv->dev);
4075
4076 return 0;
4077}
4078
Florian Fainellid9f45ab2019-10-15 10:36:24 -07004079static void bcmgenet_shutdown(struct platform_device *pdev)
4080{
4081 bcmgenet_remove(pdev);
4082}
4083
Florian Fainellib6e978e2014-07-21 15:29:22 -07004084#ifdef CONFIG_PM_SLEEP
Doug Bergereb236c22020-04-30 16:26:51 -07004085static int bcmgenet_resume_noirq(struct device *d)
4086{
4087 struct net_device *dev = dev_get_drvdata(d);
4088 struct bcmgenet_priv *priv = netdev_priv(dev);
4089 int ret;
4090 u32 reg;
4091
4092 if (!netif_running(dev))
4093 return 0;
4094
4095 /* Turn on the clock */
4096 ret = clk_prepare_enable(priv->clk);
4097 if (ret)
4098 return ret;
4099
4100 if (device_may_wakeup(d) && priv->wolopts) {
4101 /* Account for Wake-on-LAN events and clear those events
4102 * (Some devices need more time between enabling the clocks
4103 * and the interrupt register reflecting the wake event so
4104 * read the register twice)
4105 */
4106 reg = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT);
4107 reg = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT);
4108 if (reg & UMAC_IRQ_WAKE_EVENT)
4109 pm_wakeup_event(&priv->pdev->dev, 0);
4110 }
4111
4112 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_WAKE_EVENT, INTRL2_CPU_CLEAR);
4113
4114 return 0;
4115}
4116
Florian Fainellib6e978e2014-07-21 15:29:22 -07004117static int bcmgenet_resume(struct device *d)
4118{
4119 struct net_device *dev = dev_get_drvdata(d);
4120 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Bergera8c64542020-07-16 16:38:17 -07004121 struct bcmgenet_rxnfc_rule *rule;
Florian Fainellib6e978e2014-07-21 15:29:22 -07004122 unsigned long dma_ctrl;
Doug Bergera8c64542020-07-16 16:38:17 -07004123 u32 reg;
Florian Fainellib6e978e2014-07-21 15:29:22 -07004124 int ret;
Florian Fainellib6e978e2014-07-21 15:29:22 -07004125
4126 if (!netif_running(dev))
4127 return 0;
4128
Doug Berger1a1d5102020-04-29 13:02:02 -07004129 /* From WOL-enabled suspend, switch to regular clock */
4130 if (device_may_wakeup(d) && priv->wolopts)
4131 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
4132
Florian Fainellia6f31f52015-03-23 15:09:57 -07004133 /* If this is an internal GPHY, power it back on now, before UniMAC is
4134 * brought out of reset as absolutely no UniMAC activity is allowed
4135 */
Florian Fainellic624f892015-07-16 15:51:17 -07004136 if (priv->internal_phy)
Florian Fainellia6f31f52015-03-23 15:09:57 -07004137 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
4138
Florian Fainellib6e978e2014-07-21 15:29:22 -07004139 bcmgenet_umac_reset(priv);
4140
Doug Berger28c2d1a2017-10-25 15:04:13 -07004141 init_umac(priv);
Florian Fainellib6e978e2014-07-21 15:29:22 -07004142
Doug Berger6b6d017f2019-11-05 11:07:25 -08004143 phy_init_hw(dev->phydev);
4144
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02004145 /* Speed settings must be restored */
Doug Berger0686bd92019-11-05 11:07:26 -08004146 genphy_config_aneg(dev->phydev);
Florian Fainelli00d51092017-07-31 11:05:32 -07004147 bcmgenet_mii_config(priv->dev, false);
Florian Fainelli8c90db72014-07-21 15:29:28 -07004148
Doug Berger206f54b2019-12-17 16:51:12 -08004149 /* Restore enabled features */
4150 bcmgenet_set_features(dev, dev->features);
4151
Florian Fainellib6e978e2014-07-21 15:29:22 -07004152 bcmgenet_set_hw_addr(priv, dev->dev_addr);
4153
Doug Bergera8c64542020-07-16 16:38:17 -07004154 /* Restore hardware filters */
4155 bcmgenet_hfb_clear(priv);
4156 list_for_each_entry(rule, &priv->rxnfc_list, list)
4157 if (rule->state != BCMGENET_RXNFC_STATE_UNUSED)
4158 bcmgenet_hfb_create_rxnfc_filter(priv, rule);
Doug Berger3e370952020-04-29 13:02:05 -07004159
Florian Fainellic624f892015-07-16 15:51:17 -07004160 if (priv->internal_phy) {
Florian Fainellib6e978e2014-07-21 15:29:22 -07004161 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
4162 reg |= EXT_ENERGY_DET_MASK;
4163 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
4164 }
4165
4166 /* Disable RX/TX DMA and flush TX queues */
4167 dma_ctrl = bcmgenet_dma_disable(priv);
4168
4169 /* Reinitialize TDMA and RDMA and SW housekeeping */
4170 ret = bcmgenet_init_dma(priv);
4171 if (ret) {
4172 netdev_err(dev, "failed to initialize DMA\n");
4173 goto out_clk_disable;
4174 }
4175
4176 /* Always enable ring 16 - descriptor ring */
4177 bcmgenet_enable_dma(priv, dma_ctrl);
4178
Florian Fainelli5371bbf42017-03-15 12:57:21 -07004179 if (!device_may_wakeup(d))
Doug Berger6c97f012017-10-25 15:04:19 -07004180 phy_resume(dev->phydev);
Florian Fainellicc013fb2014-08-11 14:50:43 -07004181
Florian Fainelli6ef398e2014-11-25 21:16:35 -08004182 if (priv->eee.eee_enabled)
4183 bcmgenet_eee_enable_set(dev, true);
4184
Florian Fainellib6e978e2014-07-21 15:29:22 -07004185 bcmgenet_netif_start(dev);
4186
Doug Berger09e805d2018-11-01 15:55:37 -07004187 netif_device_attach(dev);
4188
Florian Fainellib6e978e2014-07-21 15:29:22 -07004189 return 0;
4190
4191out_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08004192 if (priv->internal_phy)
4193 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainellib6e978e2014-07-21 15:29:22 -07004194 clk_disable_unprepare(priv->clk);
4195 return ret;
4196}
Doug Bergera94cbf02018-11-16 18:00:21 -08004197
4198static int bcmgenet_suspend(struct device *d)
4199{
4200 struct net_device *dev = dev_get_drvdata(d);
4201 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Bergera94cbf02018-11-16 18:00:21 -08004202
4203 if (!netif_running(dev))
4204 return 0;
4205
4206 netif_device_detach(dev);
4207
4208 bcmgenet_netif_stop(dev);
4209
4210 if (!device_may_wakeup(d))
4211 phy_suspend(dev->phydev);
4212
Doug Bergera8c64542020-07-16 16:38:17 -07004213 /* Disable filtering */
Doug Berger3e370952020-04-29 13:02:05 -07004214 bcmgenet_hfb_reg_writel(priv, 0, HFB_CTRL);
4215
Doug Bergereb236c22020-04-30 16:26:51 -07004216 return 0;
4217}
4218
4219static int bcmgenet_suspend_noirq(struct device *d)
4220{
4221 struct net_device *dev = dev_get_drvdata(d);
4222 struct bcmgenet_priv *priv = netdev_priv(dev);
4223 int ret = 0;
4224
4225 if (!netif_running(dev))
4226 return 0;
4227
Doug Bergera94cbf02018-11-16 18:00:21 -08004228 /* Prepare the device for Wake-on-LAN and switch to the slow clock */
Doug Berger1a1d5102020-04-29 13:02:02 -07004229 if (device_may_wakeup(d) && priv->wolopts)
Doug Bergera94cbf02018-11-16 18:00:21 -08004230 ret = bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
Doug Berger1a1d5102020-04-29 13:02:02 -07004231 else if (priv->internal_phy)
Doug Bergera94cbf02018-11-16 18:00:21 -08004232 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Doug Bergera94cbf02018-11-16 18:00:21 -08004233
Doug Bergereb236c22020-04-30 16:26:51 -07004234 /* Let the framework handle resumption and leave the clocks on */
4235 if (ret)
4236 return ret;
4237
Doug Bergera94cbf02018-11-16 18:00:21 -08004238 /* Turn off the clocks */
4239 clk_disable_unprepare(priv->clk);
4240
Doug Bergereb236c22020-04-30 16:26:51 -07004241 return 0;
Doug Bergera94cbf02018-11-16 18:00:21 -08004242}
Doug Bergereb236c22020-04-30 16:26:51 -07004243#else
4244#define bcmgenet_suspend NULL
4245#define bcmgenet_suspend_noirq NULL
4246#define bcmgenet_resume NULL
4247#define bcmgenet_resume_noirq NULL
Florian Fainellib6e978e2014-07-21 15:29:22 -07004248#endif /* CONFIG_PM_SLEEP */
4249
Doug Bergereb236c22020-04-30 16:26:51 -07004250static const struct dev_pm_ops bcmgenet_pm_ops = {
4251 .suspend = bcmgenet_suspend,
4252 .suspend_noirq = bcmgenet_suspend_noirq,
4253 .resume = bcmgenet_resume,
4254 .resume_noirq = bcmgenet_resume_noirq,
4255};
Florian Fainellib6e978e2014-07-21 15:29:22 -07004256
Jeremy Linton99c6b062020-02-24 16:54:01 -06004257static const struct acpi_device_id genet_acpi_match[] = {
4258 { "BCM6E4E", (kernel_ulong_t)&bcm2711_plat_data },
4259 { },
4260};
4261MODULE_DEVICE_TABLE(acpi, genet_acpi_match);
4262
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004263static struct platform_driver bcmgenet_driver = {
4264 .probe = bcmgenet_probe,
4265 .remove = bcmgenet_remove,
Florian Fainellid9f45ab2019-10-15 10:36:24 -07004266 .shutdown = bcmgenet_shutdown,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004267 .driver = {
4268 .name = "bcmgenet",
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004269 .of_match_table = bcmgenet_match,
Florian Fainellib6e978e2014-07-21 15:29:22 -07004270 .pm = &bcmgenet_pm_ops,
Andy Shevchenkod4d9b472020-04-21 00:51:17 +03004271 .acpi_match_table = genet_acpi_match,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004272 },
4273};
4274module_platform_driver(bcmgenet_driver);
4275
4276MODULE_AUTHOR("Broadcom Corporation");
4277MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
4278MODULE_ALIAS("platform:bcmgenet");
4279MODULE_LICENSE("GPL");