blob: 87f1056e29ff234bac5e3fbf37c7b55e7d0b4349 [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,
Yufeng Mof3ccfda12021-08-20 15:35:18 +0800831 struct ethtool_coalesce *ec,
832 struct kernel_ethtool_coalesce *kernel_coal,
833 struct netlink_ext_ack *extack)
Florian Fainelli2f913072015-09-16 16:47:39 -0700834{
835 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700836 struct bcmgenet_rx_ring *ring;
837 unsigned int i;
Florian Fainelli2f913072015-09-16 16:47:39 -0700838
839 ec->tx_max_coalesced_frames =
840 bcmgenet_tdma_ring_readl(priv, DESC_INDEX,
841 DMA_MBUF_DONE_THRESH);
Florian Fainelli4a296452015-09-16 16:47:40 -0700842 ec->rx_max_coalesced_frames =
843 bcmgenet_rdma_ring_readl(priv, DESC_INDEX,
844 DMA_MBUF_DONE_THRESH);
845 ec->rx_coalesce_usecs =
846 bcmgenet_rdma_readl(priv, DMA_RING16_TIMEOUT) * 8192 / 1000;
Florian Fainelli2f913072015-09-16 16:47:39 -0700847
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700848 for (i = 0; i < priv->hw_params->rx_queues; i++) {
849 ring = &priv->rx_rings[i];
850 ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
851 }
852 ring = &priv->rx_rings[DESC_INDEX];
853 ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
854
Florian Fainelli2f913072015-09-16 16:47:39 -0700855 return 0;
856}
857
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700858static void bcmgenet_set_rx_coalesce(struct bcmgenet_rx_ring *ring,
859 u32 usecs, u32 pkts)
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700860{
861 struct bcmgenet_priv *priv = ring->priv;
862 unsigned int i = ring->index;
863 u32 reg;
864
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700865 bcmgenet_rdma_ring_writel(priv, i, pkts, DMA_MBUF_DONE_THRESH);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700866
867 reg = bcmgenet_rdma_readl(priv, DMA_RING0_TIMEOUT + i);
868 reg &= ~DMA_TIMEOUT_MASK;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700869 reg |= DIV_ROUND_UP(usecs * 1000, 8192);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700870 bcmgenet_rdma_writel(priv, reg, DMA_RING0_TIMEOUT + i);
871}
872
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700873static void bcmgenet_set_ring_rx_coalesce(struct bcmgenet_rx_ring *ring,
874 struct ethtool_coalesce *ec)
875{
Tal Gilboa8960b382019-01-31 16:44:48 +0200876 struct dim_cq_moder moder;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700877 u32 usecs, pkts;
878
879 ring->rx_coalesce_usecs = ec->rx_coalesce_usecs;
880 ring->rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
881 usecs = ring->rx_coalesce_usecs;
882 pkts = ring->rx_max_coalesced_frames;
883
884 if (ec->use_adaptive_rx_coalesce && !ring->dim.use_dim) {
Tal Gilboa026a8072018-04-24 13:36:01 +0300885 moder = net_dim_get_def_rx_moderation(ring->dim.dim.mode);
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700886 usecs = moder.usec;
887 pkts = moder.pkts;
888 }
889
890 ring->dim.use_dim = ec->use_adaptive_rx_coalesce;
891 bcmgenet_set_rx_coalesce(ring, usecs, pkts);
892}
893
Florian Fainelli2f913072015-09-16 16:47:39 -0700894static int bcmgenet_set_coalesce(struct net_device *dev,
Yufeng Mof3ccfda12021-08-20 15:35:18 +0800895 struct ethtool_coalesce *ec,
896 struct kernel_ethtool_coalesce *kernel_coal,
897 struct netlink_ext_ack *extack)
Florian Fainelli2f913072015-09-16 16:47:39 -0700898{
899 struct bcmgenet_priv *priv = netdev_priv(dev);
900 unsigned int i;
901
Florian Fainelli4a296452015-09-16 16:47:40 -0700902 /* Base system clock is 125Mhz, DMA timeout is this reference clock
903 * divided by 1024, which yields roughly 8.192us, our maximum value
904 * has to fit in the DMA_TIMEOUT_MASK (16 bits)
905 */
Florian Fainelli2f913072015-09-16 16:47:39 -0700906 if (ec->tx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
Florian Fainelli4a296452015-09-16 16:47:40 -0700907 ec->tx_max_coalesced_frames == 0 ||
908 ec->rx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
909 ec->rx_coalesce_usecs > (DMA_TIMEOUT_MASK * 8) + 1)
910 return -EINVAL;
911
912 if (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0)
Florian Fainelli2f913072015-09-16 16:47:39 -0700913 return -EINVAL;
914
915 /* GENET TDMA hardware does not support a configurable timeout, but will
916 * always generate an interrupt either after MBDONE packets have been
Doug Berger556c2cf2017-03-13 17:41:34 -0700917 * transmitted, or when the ring is empty.
Florian Fainelli2f913072015-09-16 16:47:39 -0700918 */
Florian Fainelli2f913072015-09-16 16:47:39 -0700919
920 /* Program all TX queues with the same values, as there is no
921 * ethtool knob to do coalescing on a per-queue basis
922 */
923 for (i = 0; i < priv->hw_params->tx_queues; i++)
924 bcmgenet_tdma_ring_writel(priv, i,
925 ec->tx_max_coalesced_frames,
926 DMA_MBUF_DONE_THRESH);
927 bcmgenet_tdma_ring_writel(priv, DESC_INDEX,
928 ec->tx_max_coalesced_frames,
929 DMA_MBUF_DONE_THRESH);
930
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700931 for (i = 0; i < priv->hw_params->rx_queues; i++)
932 bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[i], ec);
933 bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[DESC_INDEX], ec);
Florian Fainelli4a296452015-09-16 16:47:40 -0700934
Florian Fainelli2f913072015-09-16 16:47:39 -0700935 return 0;
936}
937
Doug Berger2d8bdf522021-09-25 20:21:14 -0700938static void bcmgenet_get_pauseparam(struct net_device *dev,
939 struct ethtool_pauseparam *epause)
940{
941 struct bcmgenet_priv *priv;
942 u32 umac_cmd;
943
944 priv = netdev_priv(dev);
945
946 epause->autoneg = priv->autoneg_pause;
947
948 if (netif_carrier_ok(dev)) {
949 /* report active state when link is up */
950 umac_cmd = bcmgenet_umac_readl(priv, UMAC_CMD);
951 epause->tx_pause = !(umac_cmd & CMD_TX_PAUSE_IGNORE);
952 epause->rx_pause = !(umac_cmd & CMD_RX_PAUSE_IGNORE);
953 } else {
954 /* otherwise report stored settings */
955 epause->tx_pause = priv->tx_pause;
956 epause->rx_pause = priv->rx_pause;
957 }
958}
959
960static int bcmgenet_set_pauseparam(struct net_device *dev,
961 struct ethtool_pauseparam *epause)
962{
963 struct bcmgenet_priv *priv = netdev_priv(dev);
964
965 if (!dev->phydev)
966 return -ENODEV;
967
968 if (!phy_validate_pause(dev->phydev, epause))
969 return -EINVAL;
970
971 priv->autoneg_pause = !!epause->autoneg;
972 priv->tx_pause = !!epause->tx_pause;
973 priv->rx_pause = !!epause->rx_pause;
974
975 bcmgenet_phy_pause_set(dev, priv->rx_pause, priv->tx_pause);
976
977 return 0;
978}
979
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800980/* standard ethtool support functions. */
981enum bcmgenet_stat_type {
982 BCMGENET_STAT_NETDEV = -1,
983 BCMGENET_STAT_MIB_RX,
984 BCMGENET_STAT_MIB_TX,
985 BCMGENET_STAT_RUNT,
986 BCMGENET_STAT_MISC,
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800987 BCMGENET_STAT_SOFT,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800988};
989
990struct bcmgenet_stats {
991 char stat_string[ETH_GSTRING_LEN];
992 int stat_sizeof;
993 int stat_offset;
994 enum bcmgenet_stat_type type;
995 /* reg offset from UMAC base for misc counters */
996 u16 reg_offset;
997};
998
999#define STAT_NETDEV(m) { \
1000 .stat_string = __stringify(m), \
1001 .stat_sizeof = sizeof(((struct net_device_stats *)0)->m), \
1002 .stat_offset = offsetof(struct net_device_stats, m), \
1003 .type = BCMGENET_STAT_NETDEV, \
1004}
1005
1006#define STAT_GENET_MIB(str, m, _type) { \
1007 .stat_string = str, \
1008 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
1009 .stat_offset = offsetof(struct bcmgenet_priv, m), \
1010 .type = _type, \
1011}
1012
1013#define STAT_GENET_MIB_RX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_RX)
1014#define STAT_GENET_MIB_TX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_TX)
1015#define STAT_GENET_RUNT(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_RUNT)
Florian Fainellif62ba9c2015-02-28 18:09:16 -08001016#define STAT_GENET_SOFT_MIB(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_SOFT)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001017
1018#define STAT_GENET_MISC(str, m, offset) { \
1019 .stat_string = str, \
1020 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
1021 .stat_offset = offsetof(struct bcmgenet_priv, m), \
1022 .type = BCMGENET_STAT_MISC, \
1023 .reg_offset = offset, \
1024}
1025
Florian Fainelli37a30b42017-03-16 10:27:08 -07001026#define STAT_GENET_Q(num) \
1027 STAT_GENET_SOFT_MIB("txq" __stringify(num) "_packets", \
1028 tx_rings[num].packets), \
1029 STAT_GENET_SOFT_MIB("txq" __stringify(num) "_bytes", \
1030 tx_rings[num].bytes), \
1031 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_bytes", \
1032 rx_rings[num].bytes), \
1033 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_packets", \
1034 rx_rings[num].packets), \
1035 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_errors", \
1036 rx_rings[num].errors), \
1037 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_dropped", \
1038 rx_rings[num].dropped)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001039
1040/* There is a 0xC gap between the end of RX and beginning of TX stats and then
1041 * between the end of TX stats and the beginning of the RX RUNT
1042 */
1043#define BCMGENET_STAT_OFFSET 0xc
1044
1045/* Hardware counters must be kept in sync because the order/offset
1046 * is important here (order in structure declaration = order in hardware)
1047 */
1048static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
1049 /* general stats */
1050 STAT_NETDEV(rx_packets),
1051 STAT_NETDEV(tx_packets),
1052 STAT_NETDEV(rx_bytes),
1053 STAT_NETDEV(tx_bytes),
1054 STAT_NETDEV(rx_errors),
1055 STAT_NETDEV(tx_errors),
1056 STAT_NETDEV(rx_dropped),
1057 STAT_NETDEV(tx_dropped),
1058 STAT_NETDEV(multicast),
1059 /* UniMAC RSV counters */
1060 STAT_GENET_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
1061 STAT_GENET_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
1062 STAT_GENET_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
1063 STAT_GENET_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
1064 STAT_GENET_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
1065 STAT_GENET_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
1066 STAT_GENET_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
1067 STAT_GENET_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
1068 STAT_GENET_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
1069 STAT_GENET_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
1070 STAT_GENET_MIB_RX("rx_pkts", mib.rx.pkt),
1071 STAT_GENET_MIB_RX("rx_bytes", mib.rx.bytes),
1072 STAT_GENET_MIB_RX("rx_multicast", mib.rx.mca),
1073 STAT_GENET_MIB_RX("rx_broadcast", mib.rx.bca),
1074 STAT_GENET_MIB_RX("rx_fcs", mib.rx.fcs),
1075 STAT_GENET_MIB_RX("rx_control", mib.rx.cf),
1076 STAT_GENET_MIB_RX("rx_pause", mib.rx.pf),
1077 STAT_GENET_MIB_RX("rx_unknown", mib.rx.uo),
1078 STAT_GENET_MIB_RX("rx_align", mib.rx.aln),
1079 STAT_GENET_MIB_RX("rx_outrange", mib.rx.flr),
1080 STAT_GENET_MIB_RX("rx_code", mib.rx.cde),
1081 STAT_GENET_MIB_RX("rx_carrier", mib.rx.fcr),
1082 STAT_GENET_MIB_RX("rx_oversize", mib.rx.ovr),
1083 STAT_GENET_MIB_RX("rx_jabber", mib.rx.jbr),
1084 STAT_GENET_MIB_RX("rx_mtu_err", mib.rx.mtue),
1085 STAT_GENET_MIB_RX("rx_good_pkts", mib.rx.pok),
1086 STAT_GENET_MIB_RX("rx_unicast", mib.rx.uc),
1087 STAT_GENET_MIB_RX("rx_ppp", mib.rx.ppp),
1088 STAT_GENET_MIB_RX("rx_crc", mib.rx.rcrc),
1089 /* UniMAC TSV counters */
1090 STAT_GENET_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
1091 STAT_GENET_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
1092 STAT_GENET_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
1093 STAT_GENET_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
1094 STAT_GENET_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
1095 STAT_GENET_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
1096 STAT_GENET_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
1097 STAT_GENET_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
1098 STAT_GENET_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
1099 STAT_GENET_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
1100 STAT_GENET_MIB_TX("tx_pkts", mib.tx.pkts),
1101 STAT_GENET_MIB_TX("tx_multicast", mib.tx.mca),
1102 STAT_GENET_MIB_TX("tx_broadcast", mib.tx.bca),
1103 STAT_GENET_MIB_TX("tx_pause", mib.tx.pf),
1104 STAT_GENET_MIB_TX("tx_control", mib.tx.cf),
1105 STAT_GENET_MIB_TX("tx_fcs_err", mib.tx.fcs),
1106 STAT_GENET_MIB_TX("tx_oversize", mib.tx.ovr),
1107 STAT_GENET_MIB_TX("tx_defer", mib.tx.drf),
1108 STAT_GENET_MIB_TX("tx_excess_defer", mib.tx.edf),
1109 STAT_GENET_MIB_TX("tx_single_col", mib.tx.scl),
1110 STAT_GENET_MIB_TX("tx_multi_col", mib.tx.mcl),
1111 STAT_GENET_MIB_TX("tx_late_col", mib.tx.lcl),
1112 STAT_GENET_MIB_TX("tx_excess_col", mib.tx.ecl),
1113 STAT_GENET_MIB_TX("tx_frags", mib.tx.frg),
1114 STAT_GENET_MIB_TX("tx_total_col", mib.tx.ncl),
1115 STAT_GENET_MIB_TX("tx_jabber", mib.tx.jbr),
1116 STAT_GENET_MIB_TX("tx_bytes", mib.tx.bytes),
1117 STAT_GENET_MIB_TX("tx_good_pkts", mib.tx.pok),
1118 STAT_GENET_MIB_TX("tx_unicast", mib.tx.uc),
1119 /* UniMAC RUNT counters */
1120 STAT_GENET_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
1121 STAT_GENET_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
1122 STAT_GENET_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
1123 STAT_GENET_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
1124 /* Misc UniMAC counters */
1125 STAT_GENET_MISC("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt,
Doug Bergerffff7132017-03-09 16:58:43 -08001126 UMAC_RBUF_OVFL_CNT_V1),
1127 STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt,
1128 UMAC_RBUF_ERR_CNT_V1),
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001129 STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT),
Florian Fainellif62ba9c2015-02-28 18:09:16 -08001130 STAT_GENET_SOFT_MIB("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
1131 STAT_GENET_SOFT_MIB("rx_dma_failed", mib.rx_dma_failed),
1132 STAT_GENET_SOFT_MIB("tx_dma_failed", mib.tx_dma_failed),
Doug Bergerf1af17c2019-12-17 16:51:15 -08001133 STAT_GENET_SOFT_MIB("tx_realloc_tsb", mib.tx_realloc_tsb),
1134 STAT_GENET_SOFT_MIB("tx_realloc_tsb_failed",
1135 mib.tx_realloc_tsb_failed),
Florian Fainelli37a30b42017-03-16 10:27:08 -07001136 /* Per TX queues */
1137 STAT_GENET_Q(0),
1138 STAT_GENET_Q(1),
1139 STAT_GENET_Q(2),
1140 STAT_GENET_Q(3),
1141 STAT_GENET_Q(16),
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001142};
1143
1144#define BCMGENET_STATS_LEN ARRAY_SIZE(bcmgenet_gstrings_stats)
1145
1146static void bcmgenet_get_drvinfo(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001147 struct ethtool_drvinfo *info)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001148{
1149 strlcpy(info->driver, "bcmgenet", sizeof(info->driver));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001150}
1151
1152static int bcmgenet_get_sset_count(struct net_device *dev, int string_set)
1153{
1154 switch (string_set) {
1155 case ETH_SS_STATS:
1156 return BCMGENET_STATS_LEN;
1157 default:
1158 return -EOPNOTSUPP;
1159 }
1160}
1161
Florian Fainellic91b7f62014-07-23 10:42:12 -07001162static void bcmgenet_get_strings(struct net_device *dev, u32 stringset,
1163 u8 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001164{
1165 int i;
1166
1167 switch (stringset) {
1168 case ETH_SS_STATS:
1169 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
1170 memcpy(data + i * ETH_GSTRING_LEN,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001171 bcmgenet_gstrings_stats[i].stat_string,
1172 ETH_GSTRING_LEN);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001173 }
1174 break;
1175 }
1176}
1177
Doug Bergerffff7132017-03-09 16:58:43 -08001178static u32 bcmgenet_update_stat_misc(struct bcmgenet_priv *priv, u16 offset)
1179{
1180 u16 new_offset;
1181 u32 val;
1182
1183 switch (offset) {
1184 case UMAC_RBUF_OVFL_CNT_V1:
1185 if (GENET_IS_V2(priv))
1186 new_offset = RBUF_OVFL_CNT_V2;
1187 else
1188 new_offset = RBUF_OVFL_CNT_V3PLUS;
1189
1190 val = bcmgenet_rbuf_readl(priv, new_offset);
1191 /* clear if overflowed */
1192 if (val == ~0)
1193 bcmgenet_rbuf_writel(priv, 0, new_offset);
1194 break;
1195 case UMAC_RBUF_ERR_CNT_V1:
1196 if (GENET_IS_V2(priv))
1197 new_offset = RBUF_ERR_CNT_V2;
1198 else
1199 new_offset = RBUF_ERR_CNT_V3PLUS;
1200
1201 val = bcmgenet_rbuf_readl(priv, new_offset);
1202 /* clear if overflowed */
1203 if (val == ~0)
1204 bcmgenet_rbuf_writel(priv, 0, new_offset);
1205 break;
1206 default:
1207 val = bcmgenet_umac_readl(priv, offset);
1208 /* clear if overflowed */
1209 if (val == ~0)
1210 bcmgenet_umac_writel(priv, 0, offset);
1211 break;
1212 }
1213
1214 return val;
1215}
1216
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001217static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv)
1218{
1219 int i, j = 0;
1220
1221 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
1222 const struct bcmgenet_stats *s;
1223 u8 offset = 0;
1224 u32 val = 0;
1225 char *p;
1226
1227 s = &bcmgenet_gstrings_stats[i];
1228 switch (s->type) {
1229 case BCMGENET_STAT_NETDEV:
Florian Fainellif62ba9c2015-02-28 18:09:16 -08001230 case BCMGENET_STAT_SOFT:
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001231 continue;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001232 case BCMGENET_STAT_RUNT:
Doug Berger1ad3d222017-03-09 16:58:44 -08001233 offset += BCMGENET_STAT_OFFSET;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001234 fallthrough;
Doug Berger1ad3d222017-03-09 16:58:44 -08001235 case BCMGENET_STAT_MIB_TX:
1236 offset += BCMGENET_STAT_OFFSET;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001237 fallthrough;
Doug Berger1ad3d222017-03-09 16:58:44 -08001238 case BCMGENET_STAT_MIB_RX:
Florian Fainellic91b7f62014-07-23 10:42:12 -07001239 val = bcmgenet_umac_readl(priv,
1240 UMAC_MIB_START + j + offset);
Doug Berger1ad3d222017-03-09 16:58:44 -08001241 offset = 0; /* Reset Offset */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001242 break;
1243 case BCMGENET_STAT_MISC:
Doug Bergerffff7132017-03-09 16:58:43 -08001244 if (GENET_IS_V1(priv)) {
1245 val = bcmgenet_umac_readl(priv, s->reg_offset);
1246 /* clear if overflowed */
1247 if (val == ~0)
1248 bcmgenet_umac_writel(priv, 0,
1249 s->reg_offset);
1250 } else {
1251 val = bcmgenet_update_stat_misc(priv,
1252 s->reg_offset);
1253 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001254 break;
1255 }
1256
1257 j += s->stat_sizeof;
1258 p = (char *)priv + s->stat_offset;
1259 *(u32 *)p = val;
1260 }
1261}
1262
1263static void bcmgenet_get_ethtool_stats(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001264 struct ethtool_stats *stats,
1265 u64 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001266{
1267 struct bcmgenet_priv *priv = netdev_priv(dev);
1268 int i;
1269
1270 if (netif_running(dev))
1271 bcmgenet_update_mib_counters(priv);
1272
Doug Bergera6d0b832020-04-23 15:44:17 -07001273 dev->netdev_ops->ndo_get_stats(dev);
1274
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001275 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
1276 const struct bcmgenet_stats *s;
1277 char *p;
1278
1279 s = &bcmgenet_gstrings_stats[i];
1280 if (s->type == BCMGENET_STAT_NETDEV)
1281 p = (char *)&dev->stats;
1282 else
1283 p = (char *)priv;
1284 p += s->stat_offset;
Eric Dumazet6517eb52016-04-15 10:47:52 -07001285 if (sizeof(unsigned long) != sizeof(u32) &&
1286 s->stat_sizeof == sizeof(unsigned long))
1287 data[i] = *(unsigned long *)p;
1288 else
1289 data[i] = *(u32 *)p;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001290 }
1291}
1292
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001293static void bcmgenet_eee_enable_set(struct net_device *dev, bool enable)
1294{
1295 struct bcmgenet_priv *priv = netdev_priv(dev);
1296 u32 off = priv->hw_params->tbuf_offset + TBUF_ENERGY_CTRL;
1297 u32 reg;
1298
1299 if (enable && !priv->clk_eee_enabled) {
1300 clk_prepare_enable(priv->clk_eee);
1301 priv->clk_eee_enabled = true;
1302 }
1303
1304 reg = bcmgenet_umac_readl(priv, UMAC_EEE_CTRL);
1305 if (enable)
1306 reg |= EEE_EN;
1307 else
1308 reg &= ~EEE_EN;
1309 bcmgenet_umac_writel(priv, reg, UMAC_EEE_CTRL);
1310
1311 /* Enable EEE and switch to a 27Mhz clock automatically */
Florian Fainelli69d2ea92017-08-29 12:25:31 -07001312 reg = bcmgenet_readl(priv->base + off);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001313 if (enable)
1314 reg |= TBUF_EEE_EN | TBUF_PM_EN;
1315 else
1316 reg &= ~(TBUF_EEE_EN | TBUF_PM_EN);
Florian Fainelli69d2ea92017-08-29 12:25:31 -07001317 bcmgenet_writel(reg, priv->base + off);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001318
1319 /* Do the same for thing for RBUF */
1320 reg = bcmgenet_rbuf_readl(priv, RBUF_ENERGY_CTRL);
1321 if (enable)
1322 reg |= RBUF_EEE_EN | RBUF_PM_EN;
1323 else
1324 reg &= ~(RBUF_EEE_EN | RBUF_PM_EN);
1325 bcmgenet_rbuf_writel(priv, reg, RBUF_ENERGY_CTRL);
1326
1327 if (!enable && priv->clk_eee_enabled) {
1328 clk_disable_unprepare(priv->clk_eee);
1329 priv->clk_eee_enabled = false;
1330 }
1331
1332 priv->eee.eee_enabled = enable;
1333 priv->eee.eee_active = enable;
1334}
1335
1336static int bcmgenet_get_eee(struct net_device *dev, struct ethtool_eee *e)
1337{
1338 struct bcmgenet_priv *priv = netdev_priv(dev);
1339 struct ethtool_eee *p = &priv->eee;
1340
1341 if (GENET_IS_V1(priv))
1342 return -EOPNOTSUPP;
1343
Doug Berger6c97f012017-10-25 15:04:19 -07001344 if (!dev->phydev)
1345 return -ENODEV;
1346
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001347 e->eee_enabled = p->eee_enabled;
1348 e->eee_active = p->eee_active;
1349 e->tx_lpi_timer = bcmgenet_umac_readl(priv, UMAC_EEE_LPI_TIMER);
1350
Doug Berger6c97f012017-10-25 15:04:19 -07001351 return phy_ethtool_get_eee(dev->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001352}
1353
1354static int bcmgenet_set_eee(struct net_device *dev, struct ethtool_eee *e)
1355{
1356 struct bcmgenet_priv *priv = netdev_priv(dev);
1357 struct ethtool_eee *p = &priv->eee;
1358 int ret = 0;
1359
1360 if (GENET_IS_V1(priv))
1361 return -EOPNOTSUPP;
1362
Doug Berger6c97f012017-10-25 15:04:19 -07001363 if (!dev->phydev)
1364 return -ENODEV;
1365
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001366 p->eee_enabled = e->eee_enabled;
1367
1368 if (!p->eee_enabled) {
1369 bcmgenet_eee_enable_set(dev, false);
1370 } else {
Doug Berger6c97f012017-10-25 15:04:19 -07001371 ret = phy_init_eee(dev->phydev, 0);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001372 if (ret) {
1373 netif_err(priv, hw, dev, "EEE initialization failed\n");
1374 return ret;
1375 }
1376
1377 bcmgenet_umac_writel(priv, e->tx_lpi_timer, UMAC_EEE_LPI_TIMER);
1378 bcmgenet_eee_enable_set(dev, true);
1379 }
1380
Doug Berger6c97f012017-10-25 15:04:19 -07001381 return phy_ethtool_set_eee(dev->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001382}
1383
Doug Berger3e370952020-04-29 13:02:05 -07001384static int bcmgenet_validate_flow(struct net_device *dev,
1385 struct ethtool_rxnfc *cmd)
1386{
1387 struct ethtool_usrip4_spec *l4_mask;
1388 struct ethhdr *eth_mask;
1389
1390 if (cmd->fs.location >= MAX_NUM_OF_FS_RULES) {
1391 netdev_err(dev, "rxnfc: Invalid location (%d)\n",
1392 cmd->fs.location);
1393 return -EINVAL;
1394 }
1395
1396 switch (cmd->fs.flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
1397 case IP_USER_FLOW:
1398 l4_mask = &cmd->fs.m_u.usr_ip4_spec;
1399 /* don't allow mask which isn't valid */
1400 if (VALIDATE_MASK(l4_mask->ip4src) ||
1401 VALIDATE_MASK(l4_mask->ip4dst) ||
1402 VALIDATE_MASK(l4_mask->l4_4_bytes) ||
1403 VALIDATE_MASK(l4_mask->proto) ||
1404 VALIDATE_MASK(l4_mask->ip_ver) ||
1405 VALIDATE_MASK(l4_mask->tos)) {
1406 netdev_err(dev, "rxnfc: Unsupported mask\n");
1407 return -EINVAL;
1408 }
1409 break;
1410 case ETHER_FLOW:
1411 eth_mask = &cmd->fs.m_u.ether_spec;
1412 /* don't allow mask which isn't valid */
Denis Efremov1996cf42020-09-02 14:18:45 +03001413 if (VALIDATE_MASK(eth_mask->h_dest) ||
Doug Berger3e370952020-04-29 13:02:05 -07001414 VALIDATE_MASK(eth_mask->h_source) ||
1415 VALIDATE_MASK(eth_mask->h_proto)) {
1416 netdev_err(dev, "rxnfc: Unsupported mask\n");
1417 return -EINVAL;
1418 }
1419 break;
1420 default:
1421 netdev_err(dev, "rxnfc: Unsupported flow type (0x%x)\n",
1422 cmd->fs.flow_type);
1423 return -EINVAL;
1424 }
1425
1426 if ((cmd->fs.flow_type & FLOW_EXT)) {
1427 /* don't allow mask which isn't valid */
1428 if (VALIDATE_MASK(cmd->fs.m_ext.vlan_etype) ||
1429 VALIDATE_MASK(cmd->fs.m_ext.vlan_tci)) {
1430 netdev_err(dev, "rxnfc: Unsupported mask\n");
1431 return -EINVAL;
1432 }
1433 if (cmd->fs.m_ext.data[0] || cmd->fs.m_ext.data[1]) {
1434 netdev_err(dev, "rxnfc: user-def not supported\n");
1435 return -EINVAL;
1436 }
1437 }
1438
1439 if ((cmd->fs.flow_type & FLOW_MAC_EXT)) {
1440 /* don't allow mask which isn't valid */
1441 if (VALIDATE_MASK(cmd->fs.m_ext.h_dest)) {
1442 netdev_err(dev, "rxnfc: Unsupported mask\n");
1443 return -EINVAL;
1444 }
1445 }
1446
1447 return 0;
1448}
1449
1450static int bcmgenet_insert_flow(struct net_device *dev,
1451 struct ethtool_rxnfc *cmd)
1452{
1453 struct bcmgenet_priv *priv = netdev_priv(dev);
1454 struct bcmgenet_rxnfc_rule *loc_rule;
1455 int err;
1456
1457 if (priv->hw_params->hfb_filter_size < 128) {
1458 netdev_err(dev, "rxnfc: Not supported by this device\n");
1459 return -EINVAL;
1460 }
1461
Doug Bergerf50932c2020-04-29 13:02:06 -07001462 if (cmd->fs.ring_cookie > priv->hw_params->rx_queues &&
1463 cmd->fs.ring_cookie != RX_CLS_FLOW_WAKE) {
Doug Berger3e370952020-04-29 13:02:05 -07001464 netdev_err(dev, "rxnfc: Unsupported action (%llu)\n",
1465 cmd->fs.ring_cookie);
1466 return -EINVAL;
1467 }
1468
1469 err = bcmgenet_validate_flow(dev, cmd);
1470 if (err)
1471 return err;
1472
1473 loc_rule = &priv->rxnfc_rules[cmd->fs.location];
1474 if (loc_rule->state == BCMGENET_RXNFC_STATE_ENABLED)
1475 bcmgenet_hfb_disable_filter(priv, cmd->fs.location);
Doug Bergera8c64542020-07-16 16:38:17 -07001476 if (loc_rule->state != BCMGENET_RXNFC_STATE_UNUSED) {
Doug Berger3e370952020-04-29 13:02:05 -07001477 list_del(&loc_rule->list);
Doug Bergera8c64542020-07-16 16:38:17 -07001478 bcmgenet_hfb_clear_filter(priv, cmd->fs.location);
1479 }
Doug Berger3e370952020-04-29 13:02:05 -07001480 loc_rule->state = BCMGENET_RXNFC_STATE_UNUSED;
1481 memcpy(&loc_rule->fs, &cmd->fs,
1482 sizeof(struct ethtool_rx_flow_spec));
1483
Doug Bergera8c64542020-07-16 16:38:17 -07001484 bcmgenet_hfb_create_rxnfc_filter(priv, loc_rule);
Doug Berger3e370952020-04-29 13:02:05 -07001485
1486 list_add_tail(&loc_rule->list, &priv->rxnfc_list);
1487
1488 return 0;
1489}
1490
1491static int bcmgenet_delete_flow(struct net_device *dev,
1492 struct ethtool_rxnfc *cmd)
1493{
1494 struct bcmgenet_priv *priv = netdev_priv(dev);
1495 struct bcmgenet_rxnfc_rule *rule;
1496 int err = 0;
1497
1498 if (cmd->fs.location >= MAX_NUM_OF_FS_RULES)
1499 return -EINVAL;
1500
1501 rule = &priv->rxnfc_rules[cmd->fs.location];
1502 if (rule->state == BCMGENET_RXNFC_STATE_UNUSED) {
1503 err = -ENOENT;
1504 goto out;
1505 }
1506
1507 if (rule->state == BCMGENET_RXNFC_STATE_ENABLED)
1508 bcmgenet_hfb_disable_filter(priv, cmd->fs.location);
Doug Bergera8c64542020-07-16 16:38:17 -07001509 if (rule->state != BCMGENET_RXNFC_STATE_UNUSED) {
Doug Berger3e370952020-04-29 13:02:05 -07001510 list_del(&rule->list);
Doug Bergera8c64542020-07-16 16:38:17 -07001511 bcmgenet_hfb_clear_filter(priv, cmd->fs.location);
1512 }
Doug Berger3e370952020-04-29 13:02:05 -07001513 rule->state = BCMGENET_RXNFC_STATE_UNUSED;
1514 memset(&rule->fs, 0, sizeof(struct ethtool_rx_flow_spec));
1515
1516out:
1517 return err;
1518}
1519
1520static int bcmgenet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1521{
1522 struct bcmgenet_priv *priv = netdev_priv(dev);
1523 int err = 0;
1524
1525 switch (cmd->cmd) {
1526 case ETHTOOL_SRXCLSRLINS:
1527 err = bcmgenet_insert_flow(dev, cmd);
1528 break;
1529 case ETHTOOL_SRXCLSRLDEL:
1530 err = bcmgenet_delete_flow(dev, cmd);
1531 break;
1532 default:
1533 netdev_warn(priv->dev, "Unsupported ethtool command. (%d)\n",
1534 cmd->cmd);
1535 return -EINVAL;
1536 }
1537
1538 return err;
1539}
1540
1541static int bcmgenet_get_flow(struct net_device *dev, struct ethtool_rxnfc *cmd,
1542 int loc)
1543{
1544 struct bcmgenet_priv *priv = netdev_priv(dev);
1545 struct bcmgenet_rxnfc_rule *rule;
1546 int err = 0;
1547
1548 if (loc < 0 || loc >= MAX_NUM_OF_FS_RULES)
1549 return -EINVAL;
1550
1551 rule = &priv->rxnfc_rules[loc];
1552 if (rule->state == BCMGENET_RXNFC_STATE_UNUSED)
1553 err = -ENOENT;
1554 else
1555 memcpy(&cmd->fs, &rule->fs,
1556 sizeof(struct ethtool_rx_flow_spec));
1557
1558 return err;
1559}
1560
1561static int bcmgenet_get_num_flows(struct bcmgenet_priv *priv)
1562{
1563 struct list_head *pos;
1564 int res = 0;
1565
1566 list_for_each(pos, &priv->rxnfc_list)
1567 res++;
1568
1569 return res;
1570}
1571
1572static int bcmgenet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1573 u32 *rule_locs)
1574{
1575 struct bcmgenet_priv *priv = netdev_priv(dev);
1576 struct bcmgenet_rxnfc_rule *rule;
1577 int err = 0;
1578 int i = 0;
1579
1580 switch (cmd->cmd) {
1581 case ETHTOOL_GRXRINGS:
1582 cmd->data = priv->hw_params->rx_queues ?: 1;
1583 break;
1584 case ETHTOOL_GRXCLSRLCNT:
1585 cmd->rule_cnt = bcmgenet_get_num_flows(priv);
1586 cmd->data = MAX_NUM_OF_FS_RULES;
1587 break;
1588 case ETHTOOL_GRXCLSRULE:
1589 err = bcmgenet_get_flow(dev, cmd, cmd->fs.location);
1590 break;
1591 case ETHTOOL_GRXCLSRLALL:
1592 list_for_each_entry(rule, &priv->rxnfc_list, list)
1593 if (i < cmd->rule_cnt)
1594 rule_locs[i++] = rule->fs.location;
1595 cmd->rule_cnt = i;
1596 cmd->data = MAX_NUM_OF_FS_RULES;
1597 break;
1598 default:
1599 err = -EOPNOTSUPP;
1600 break;
1601 }
1602
1603 return err;
1604}
1605
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001606/* standard ethtool support functions. */
Julia Lawall70591ab2016-08-31 09:30:45 +02001607static const struct ethtool_ops bcmgenet_ethtool_ops = {
Jakub Kicinskif6f508c2020-03-09 19:15:03 -07001608 .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
1609 ETHTOOL_COALESCE_MAX_FRAMES |
1610 ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
Edwin Chan89316fa2017-03-09 16:58:49 -08001611 .begin = bcmgenet_begin,
1612 .complete = bcmgenet_complete,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001613 .get_strings = bcmgenet_get_strings,
1614 .get_sset_count = bcmgenet_get_sset_count,
1615 .get_ethtool_stats = bcmgenet_get_ethtool_stats,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001616 .get_drvinfo = bcmgenet_get_drvinfo,
1617 .get_link = ethtool_op_get_link,
1618 .get_msglevel = bcmgenet_get_msglevel,
1619 .set_msglevel = bcmgenet_set_msglevel,
Florian Fainelli06ba8372014-07-21 15:29:29 -07001620 .get_wol = bcmgenet_get_wol,
1621 .set_wol = bcmgenet_set_wol,
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001622 .get_eee = bcmgenet_get_eee,
1623 .set_eee = bcmgenet_set_eee,
Florian Fainelli016e7702016-11-15 10:06:38 -08001624 .nway_reset = phy_ethtool_nway_reset,
Florian Fainelli2f913072015-09-16 16:47:39 -07001625 .get_coalesce = bcmgenet_get_coalesce,
1626 .set_coalesce = bcmgenet_set_coalesce,
Philippe Reynesfa92bf02016-09-26 22:31:57 +02001627 .get_link_ksettings = bcmgenet_get_link_ksettings,
1628 .set_link_ksettings = bcmgenet_set_link_ksettings,
Ryan M. Collinsdd1bf472019-08-30 14:49:55 -04001629 .get_ts_info = ethtool_op_get_ts_info,
Doug Berger3e370952020-04-29 13:02:05 -07001630 .get_rxnfc = bcmgenet_get_rxnfc,
1631 .set_rxnfc = bcmgenet_set_rxnfc,
Doug Berger2d8bdf522021-09-25 20:21:14 -07001632 .get_pauseparam = bcmgenet_get_pauseparam,
1633 .set_pauseparam = bcmgenet_set_pauseparam,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001634};
1635
1636/* Power down the unimac, based on mode. */
Florian Fainellica8cf342015-03-23 15:09:51 -07001637static int bcmgenet_power_down(struct bcmgenet_priv *priv,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001638 enum bcmgenet_power_mode mode)
1639{
Florian Fainellica8cf342015-03-23 15:09:51 -07001640 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001641 u32 reg;
1642
1643 switch (mode) {
1644 case GENET_POWER_CABLE_SENSE:
Doug Berger6c97f012017-10-25 15:04:19 -07001645 phy_detach(priv->dev->phydev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001646 break;
1647
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001648 case GENET_POWER_WOL_MAGIC:
Florian Fainellica8cf342015-03-23 15:09:51 -07001649 ret = bcmgenet_wol_power_down_cfg(priv, mode);
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001650 break;
1651
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001652 case GENET_POWER_PASSIVE:
1653 /* Power down LED */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001654 if (priv->hw_params->flags & GENET_HAS_EXT) {
1655 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
Florian Fainelli3cd92ea2021-10-22 09:17:03 -07001656 if (GENET_IS_V5(priv) && !priv->ephy_16nm)
Doug Berger42138082017-03-13 17:41:42 -07001657 reg |= EXT_PWR_DOWN_PHY_EN |
1658 EXT_PWR_DOWN_PHY_RD |
1659 EXT_PWR_DOWN_PHY_SD |
1660 EXT_PWR_DOWN_PHY_RX |
1661 EXT_PWR_DOWN_PHY_TX |
1662 EXT_IDDQ_GLBL_PWR;
1663 else
1664 reg |= EXT_PWR_DOWN_PHY;
1665
1666 reg |= (EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001667 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
Florian Fainellia642c4f2015-03-23 15:09:56 -07001668
1669 bcmgenet_phy_power_set(priv->dev, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001670 }
1671 break;
1672 default:
1673 break;
1674 }
Florian Fainellica8cf342015-03-23 15:09:51 -07001675
YueHaibing0db55092018-11-08 02:08:43 +00001676 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001677}
1678
1679static void bcmgenet_power_up(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001680 enum bcmgenet_power_mode mode)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001681{
1682 u32 reg;
1683
1684 if (!(priv->hw_params->flags & GENET_HAS_EXT))
1685 return;
1686
1687 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
1688
1689 switch (mode) {
1690 case GENET_POWER_PASSIVE:
Doug Berger5a3c6802021-06-29 17:14:19 -07001691 reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS |
1692 EXT_ENERGY_DET_MASK);
Florian Fainelli3cd92ea2021-10-22 09:17:03 -07001693 if (GENET_IS_V5(priv) && !priv->ephy_16nm) {
Doug Berger42138082017-03-13 17:41:42 -07001694 reg &= ~(EXT_PWR_DOWN_PHY_EN |
1695 EXT_PWR_DOWN_PHY_RD |
1696 EXT_PWR_DOWN_PHY_SD |
1697 EXT_PWR_DOWN_PHY_RX |
1698 EXT_PWR_DOWN_PHY_TX |
1699 EXT_IDDQ_GLBL_PWR);
1700 reg |= EXT_PHY_RESET;
1701 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1702 mdelay(1);
1703
1704 reg &= ~EXT_PHY_RESET;
1705 } else {
1706 reg &= ~EXT_PWR_DOWN_PHY;
1707 reg |= EXT_PWR_DN_EN_LD;
1708 }
1709 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1710 bcmgenet_phy_power_set(priv->dev, true);
Doug Berger42138082017-03-13 17:41:42 -07001711 break;
1712
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001713 case GENET_POWER_CABLE_SENSE:
1714 /* enable APD */
Doug Berger42138082017-03-13 17:41:42 -07001715 if (!GENET_IS_V5(priv)) {
1716 reg |= EXT_PWR_DN_EN_LD;
1717 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1718 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001719 break;
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001720 case GENET_POWER_WOL_MAGIC:
1721 bcmgenet_wol_power_up_cfg(priv, mode);
1722 return;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001723 default:
1724 break;
1725 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001726}
1727
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001728static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv,
1729 struct bcmgenet_tx_ring *ring)
1730{
1731 struct enet_cb *tx_cb_ptr;
1732
1733 tx_cb_ptr = ring->cbs;
1734 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
Petri Gynther014012a2015-02-23 11:00:45 -08001735
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001736 /* Advancing local write pointer */
1737 if (ring->write_ptr == ring->end_ptr)
1738 ring->write_ptr = ring->cb_ptr;
1739 else
1740 ring->write_ptr++;
1741
1742 return tx_cb_ptr;
1743}
1744
Doug Berger876dbad2017-07-14 16:12:09 -07001745static struct enet_cb *bcmgenet_put_txcb(struct bcmgenet_priv *priv,
1746 struct bcmgenet_tx_ring *ring)
1747{
1748 struct enet_cb *tx_cb_ptr;
1749
1750 tx_cb_ptr = ring->cbs;
1751 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
1752
1753 /* Rewinding local write pointer */
1754 if (ring->write_ptr == ring->cb_ptr)
1755 ring->write_ptr = ring->end_ptr;
1756 else
1757 ring->write_ptr--;
1758
1759 return tx_cb_ptr;
1760}
1761
Petri Gynther4055eae2015-03-25 12:35:16 -07001762static inline void bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring *ring)
1763{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001764 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001765 INTRL2_CPU_MASK_SET);
1766}
1767
1768static inline void bcmgenet_rx_ring16_int_enable(struct bcmgenet_rx_ring *ring)
1769{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001770 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001771 INTRL2_CPU_MASK_CLEAR);
1772}
1773
1774static inline void bcmgenet_rx_ring_int_disable(struct bcmgenet_rx_ring *ring)
1775{
1776 bcmgenet_intrl2_1_writel(ring->priv,
1777 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1778 INTRL2_CPU_MASK_SET);
1779}
1780
1781static inline void bcmgenet_rx_ring_int_enable(struct bcmgenet_rx_ring *ring)
1782{
1783 bcmgenet_intrl2_1_writel(ring->priv,
1784 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1785 INTRL2_CPU_MASK_CLEAR);
1786}
1787
Petri Gynther9dbac282015-03-25 12:35:10 -07001788static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001789{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001790 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001791 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001792}
1793
Petri Gynther9dbac282015-03-25 12:35:10 -07001794static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001795{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001796 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001797 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001798}
1799
Petri Gynther9dbac282015-03-25 12:35:10 -07001800static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001801{
Petri Gynther9dbac282015-03-25 12:35:10 -07001802 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001803 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001804}
1805
Petri Gynther9dbac282015-03-25 12:35:10 -07001806static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001807{
Petri Gynther9dbac282015-03-25 12:35:10 -07001808 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001809 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001810}
1811
Doug Bergerf48bed12017-07-14 16:12:10 -07001812/* Simple helper to free a transmit control block's resources
1813 * Returns an skb when the last transmit control block associated with the
1814 * skb is freed. The skb should be freed by the caller if necessary.
1815 */
1816static struct sk_buff *bcmgenet_free_tx_cb(struct device *dev,
1817 struct enet_cb *cb)
1818{
1819 struct sk_buff *skb;
1820
1821 skb = cb->skb;
1822
1823 if (skb) {
1824 cb->skb = NULL;
1825 if (cb == GENET_CB(skb)->first_cb)
1826 dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
1827 dma_unmap_len(cb, dma_len),
1828 DMA_TO_DEVICE);
1829 else
1830 dma_unmap_page(dev, dma_unmap_addr(cb, dma_addr),
1831 dma_unmap_len(cb, dma_len),
1832 DMA_TO_DEVICE);
1833 dma_unmap_addr_set(cb, dma_addr, 0);
1834
1835 if (cb == GENET_CB(skb)->last_cb)
1836 return skb;
1837
1838 } else if (dma_unmap_addr(cb, dma_addr)) {
1839 dma_unmap_page(dev,
1840 dma_unmap_addr(cb, dma_addr),
1841 dma_unmap_len(cb, dma_len),
1842 DMA_TO_DEVICE);
1843 dma_unmap_addr_set(cb, dma_addr, 0);
1844 }
1845
Wei Yongjun335ab8b2018-03-28 12:51:19 +00001846 return NULL;
Doug Bergerf48bed12017-07-14 16:12:10 -07001847}
1848
1849/* Simple helper to free a receive control block's resources */
1850static struct sk_buff *bcmgenet_free_rx_cb(struct device *dev,
1851 struct enet_cb *cb)
1852{
1853 struct sk_buff *skb;
1854
1855 skb = cb->skb;
1856 cb->skb = NULL;
1857
1858 if (dma_unmap_addr(cb, dma_addr)) {
1859 dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
1860 dma_unmap_len(cb, dma_len), DMA_FROM_DEVICE);
1861 dma_unmap_addr_set(cb, dma_addr, 0);
1862 }
1863
1864 return skb;
1865}
1866
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001867/* Unlocked version of the reclaim routine */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001868static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
1869 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001870{
1871 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther66d06752015-03-04 14:30:01 -08001872 unsigned int txbds_processed = 0;
Doug Bergerf48bed12017-07-14 16:12:10 -07001873 unsigned int bytes_compl = 0;
1874 unsigned int pkts_compl = 0;
1875 unsigned int txbds_ready;
1876 unsigned int c_index;
1877 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001878
Doug Bergerd5810ca2017-03-13 17:41:37 -07001879 /* Clear status before servicing to reduce spurious interrupts */
1880 if (ring->index == DESC_INDEX)
1881 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_TXDMA_DONE,
1882 INTRL2_CPU_CLEAR);
1883 else
1884 bcmgenet_intrl2_1_writel(priv, (1 << ring->index),
1885 INTRL2_CPU_CLEAR);
1886
Brian Norris7fc527f2014-07-29 14:34:14 -07001887 /* Compute how many buffers are transmitted since last xmit call */
Doug Bergerc298ede2017-03-13 17:41:33 -07001888 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX)
1889 & DMA_C_INDEX_MASK;
1890 txbds_ready = (c_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001891
1892 netif_dbg(priv, tx_done, dev,
Petri Gynther66d06752015-03-04 14:30:01 -08001893 "%s ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
1894 __func__, ring->index, ring->c_index, c_index, txbds_ready);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001895
1896 /* Reclaim transmitted buffers */
Petri Gynther66d06752015-03-04 14:30:01 -08001897 while (txbds_processed < txbds_ready) {
Doug Bergerf48bed12017-07-14 16:12:10 -07001898 skb = bcmgenet_free_tx_cb(&priv->pdev->dev,
1899 &priv->tx_cbs[ring->clean_ptr]);
1900 if (skb) {
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001901 pkts_compl++;
Doug Bergerf48bed12017-07-14 16:12:10 -07001902 bytes_compl += GENET_CB(skb)->bytes_sent;
Florian Fainellid4fec852017-08-24 15:56:29 -07001903 dev_consume_skb_any(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001904 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001905
Petri Gynther66d06752015-03-04 14:30:01 -08001906 txbds_processed++;
1907 if (likely(ring->clean_ptr < ring->end_ptr))
1908 ring->clean_ptr++;
1909 else
1910 ring->clean_ptr = ring->cb_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001911 }
1912
Petri Gynther66d06752015-03-04 14:30:01 -08001913 ring->free_bds += txbds_processed;
Doug Bergerc4d453d2017-03-13 17:41:38 -07001914 ring->c_index = c_index;
Petri Gynther66d06752015-03-04 14:30:01 -08001915
Florian Fainelli37a30b42017-03-16 10:27:08 -07001916 ring->packets += pkts_compl;
1917 ring->bytes += bytes_compl;
Petri Gynther55868122016-03-24 11:27:20 -07001918
Doug Berger6d22fe12017-03-09 16:58:50 -08001919 netdev_tx_completed_queue(netdev_get_tx_queue(dev, ring->queue),
1920 pkts_compl, bytes_compl);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001921
Doug Bergerc4d453d2017-03-13 17:41:38 -07001922 return txbds_processed;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001923}
1924
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001925static unsigned int bcmgenet_tx_reclaim(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001926 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001927{
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001928 unsigned int released;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001929
Doug Bergerb0447ec2017-10-25 15:04:17 -07001930 spin_lock_bh(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001931 released = __bcmgenet_tx_reclaim(dev, ring);
Doug Bergerb0447ec2017-10-25 15:04:17 -07001932 spin_unlock_bh(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001933
1934 return released;
1935}
1936
1937static int bcmgenet_tx_poll(struct napi_struct *napi, int budget)
1938{
1939 struct bcmgenet_tx_ring *ring =
1940 container_of(napi, struct bcmgenet_tx_ring, napi);
1941 unsigned int work_done = 0;
Doug Berger6d22fe12017-03-09 16:58:50 -08001942 struct netdev_queue *txq;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001943
Doug Bergerb0447ec2017-10-25 15:04:17 -07001944 spin_lock(&ring->lock);
Doug Berger6d22fe12017-03-09 16:58:50 -08001945 work_done = __bcmgenet_tx_reclaim(ring->priv->dev, ring);
1946 if (ring->free_bds > (MAX_SKB_FRAGS + 1)) {
1947 txq = netdev_get_tx_queue(ring->priv->dev, ring->queue);
1948 netif_tx_wake_queue(txq);
1949 }
Doug Bergerb0447ec2017-10-25 15:04:17 -07001950 spin_unlock(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001951
1952 if (work_done == 0) {
1953 napi_complete(napi);
Petri Gynther9dbac282015-03-25 12:35:10 -07001954 ring->int_enable(ring);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001955
1956 return 0;
1957 }
1958
1959 return budget;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001960}
1961
1962static void bcmgenet_tx_reclaim_all(struct net_device *dev)
1963{
1964 struct bcmgenet_priv *priv = netdev_priv(dev);
1965 int i;
1966
1967 if (netif_is_multiqueue(dev)) {
1968 for (i = 0; i < priv->hw_params->tx_queues; i++)
1969 bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
1970 }
1971
1972 bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
1973}
1974
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001975/* Reallocate the SKB to put enough headroom in front of it and insert
1976 * the transmit checksum offsets in the descriptors
1977 */
Doug Berger9a9ba2a2020-03-17 17:05:36 -07001978static struct sk_buff *bcmgenet_add_tsb(struct net_device *dev,
1979 struct sk_buff *skb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001980{
Doug Bergerf1af17c2019-12-17 16:51:15 -08001981 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001982 struct status_64 *status = NULL;
1983 struct sk_buff *new_skb;
1984 u16 offset;
1985 u8 ip_proto;
Florian Fainelli6f894212018-04-02 15:58:55 -07001986 __be16 ip_ver;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001987 u32 tx_csum_info;
1988
1989 if (unlikely(skb_headroom(skb) < sizeof(*status))) {
1990 /* If 64 byte status block enabled, must make sure skb has
1991 * enough headroom for us to insert 64B status block.
1992 */
1993 new_skb = skb_realloc_headroom(skb, sizeof(*status));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001994 if (!new_skb) {
Doug Bergere3fa8582019-12-17 16:51:14 -08001995 dev_kfree_skb_any(skb);
Doug Bergerf1af17c2019-12-17 16:51:15 -08001996 priv->mib.tx_realloc_tsb_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001997 dev->stats.tx_dropped++;
Petri Gyntherbc233332014-10-01 11:30:01 -07001998 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001999 }
Doug Bergere3fa8582019-12-17 16:51:14 -08002000 dev_consume_skb_any(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002001 skb = new_skb;
Doug Bergerf1af17c2019-12-17 16:51:15 -08002002 priv->mib.tx_realloc_tsb++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002003 }
2004
2005 skb_push(skb, sizeof(*status));
2006 status = (struct status_64 *)skb->data;
2007
2008 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Florian Fainelli6f894212018-04-02 15:58:55 -07002009 ip_ver = skb->protocol;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002010 switch (ip_ver) {
Florian Fainelli6f894212018-04-02 15:58:55 -07002011 case htons(ETH_P_IP):
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002012 ip_proto = ip_hdr(skb)->protocol;
2013 break;
Florian Fainelli6f894212018-04-02 15:58:55 -07002014 case htons(ETH_P_IPV6):
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002015 ip_proto = ipv6_hdr(skb)->nexthdr;
2016 break;
2017 default:
Doug Bergerdd8e9112019-12-17 16:51:09 -08002018 /* don't use UDP flag */
2019 ip_proto = 0;
2020 break;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002021 }
2022
2023 offset = skb_checksum_start_offset(skb) - sizeof(*status);
2024 tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
Doug Bergerdd8e9112019-12-17 16:51:09 -08002025 (offset + skb->csum_offset) |
2026 STATUS_TX_CSUM_LV;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002027
Doug Bergerdd8e9112019-12-17 16:51:09 -08002028 /* Set the special UDP flag for UDP */
2029 if (ip_proto == IPPROTO_UDP)
2030 tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002031
2032 status->tx_csum_info = tx_csum_info;
2033 }
2034
Petri Gyntherbc233332014-10-01 11:30:01 -07002035 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002036}
2037
2038static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
2039{
2040 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Berger876dbad2017-07-14 16:12:09 -07002041 struct device *kdev = &priv->pdev->dev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002042 struct bcmgenet_tx_ring *ring = NULL;
Doug Berger876dbad2017-07-14 16:12:09 -07002043 struct enet_cb *tx_cb_ptr;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07002044 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002045 int nr_frags, index;
Doug Berger876dbad2017-07-14 16:12:09 -07002046 dma_addr_t mapping;
2047 unsigned int size;
2048 skb_frag_t *frag;
2049 u32 len_stat;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002050 int ret;
2051 int i;
2052
2053 index = skb_get_queue_mapping(skb);
2054 /* Mapping strategy:
2055 * queue_mapping = 0, unclassified, packet xmited through ring16
2056 * queue_mapping = 1, goes to ring 0. (highest priority queue
2057 * queue_mapping = 2, goes to ring 1.
2058 * queue_mapping = 3, goes to ring 2.
2059 * queue_mapping = 4, goes to ring 3.
2060 */
2061 if (index == 0)
2062 index = DESC_INDEX;
2063 else
2064 index -= 1;
2065
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002066 ring = &priv->tx_rings[index];
Florian Fainellib2cde2c2014-03-20 10:53:23 -07002067 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002068
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07002069 nr_frags = skb_shinfo(skb)->nr_frags;
2070
Doug Bergerb0447ec2017-10-25 15:04:17 -07002071 spin_lock(&ring->lock);
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07002072 if (ring->free_bds <= (nr_frags + 1)) {
2073 if (!netif_tx_queue_stopped(txq)) {
2074 netif_tx_stop_queue(txq);
2075 netdev_err(dev,
2076 "%s: tx ring %d full when queue %d awake\n",
2077 __func__, index, ring->queue);
2078 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002079 ret = NETDEV_TX_BUSY;
2080 goto out;
2081 }
2082
Petri Gynther55868122016-03-24 11:27:20 -07002083 /* Retain how many bytes will be sent on the wire, without TSB inserted
2084 * by transmit checksum offload
2085 */
2086 GENET_CB(skb)->bytes_sent = skb->len;
2087
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002088 /* add the Transmit Status Block */
2089 skb = bcmgenet_add_tsb(dev, skb);
2090 if (!skb) {
2091 ret = NETDEV_TX_OK;
2092 goto out;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002093 }
2094
Doug Berger876dbad2017-07-14 16:12:09 -07002095 for (i = 0; i <= nr_frags; i++) {
2096 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002097
Gustavo A. R. Silva4fa112f2017-10-26 07:16:01 -05002098 BUG_ON(!tx_cb_ptr);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002099
Doug Berger876dbad2017-07-14 16:12:09 -07002100 if (!i) {
2101 /* Transmit single SKB or head of fragment list */
Doug Bergerf48bed12017-07-14 16:12:10 -07002102 GENET_CB(skb)->first_cb = tx_cb_ptr;
Doug Berger876dbad2017-07-14 16:12:09 -07002103 size = skb_headlen(skb);
2104 mapping = dma_map_single(kdev, skb->data, size,
2105 DMA_TO_DEVICE);
2106 } else {
2107 /* xmit fragment */
Doug Berger876dbad2017-07-14 16:12:09 -07002108 frag = &skb_shinfo(skb)->frags[i - 1];
2109 size = skb_frag_size(frag);
2110 mapping = skb_frag_dma_map(kdev, frag, 0, size,
2111 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002112 }
Doug Berger876dbad2017-07-14 16:12:09 -07002113
2114 ret = dma_mapping_error(kdev, mapping);
2115 if (ret) {
2116 priv->mib.tx_dma_failed++;
2117 netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
2118 ret = NETDEV_TX_OK;
2119 goto out_unmap_frags;
2120 }
2121 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
2122 dma_unmap_len_set(tx_cb_ptr, dma_len, size);
2123
Doug Bergerf48bed12017-07-14 16:12:10 -07002124 tx_cb_ptr->skb = skb;
2125
Doug Berger876dbad2017-07-14 16:12:09 -07002126 len_stat = (size << DMA_BUFLENGTH_SHIFT) |
2127 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT);
2128
Doug Berger20d1f2d2020-06-24 18:14:55 -07002129 /* Note: if we ever change from DMA_TX_APPEND_CRC below we
2130 * will need to restore software padding of "runt" packets
2131 */
Doug Berger876dbad2017-07-14 16:12:09 -07002132 if (!i) {
2133 len_stat |= DMA_TX_APPEND_CRC | DMA_SOP;
2134 if (skb->ip_summed == CHECKSUM_PARTIAL)
2135 len_stat |= DMA_TX_DO_CSUM;
2136 }
2137 if (i == nr_frags)
2138 len_stat |= DMA_EOP;
2139
2140 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, len_stat);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002141 }
2142
Doug Bergerf48bed12017-07-14 16:12:10 -07002143 GENET_CB(skb)->last_cb = tx_cb_ptr;
Florian Fainellid03825f2014-03-20 10:53:21 -07002144 skb_tx_timestamp(skb);
2145
Florian Fainelliae67bf02015-03-13 12:11:06 -07002146 /* Decrement total BD count and advance our write pointer */
2147 ring->free_bds -= nr_frags + 1;
2148 ring->prod_index += nr_frags + 1;
2149 ring->prod_index &= DMA_P_INDEX_MASK;
2150
Petri Gynthere178c8c2016-04-09 00:20:36 -07002151 netdev_tx_sent_queue(txq, GENET_CB(skb)->bytes_sent);
2152
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002153 if (ring->free_bds <= (MAX_SKB_FRAGS + 1))
Florian Fainellib2cde2c2014-03-20 10:53:23 -07002154 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002155
Florian Westphal6b16f9e2019-04-01 16:42:14 +02002156 if (!netdev_xmit_more() || netif_xmit_stopped(txq))
Florian Fainelliddd0ca52015-03-13 12:11:07 -07002157 /* Packets are ready, update producer index */
2158 bcmgenet_tdma_ring_writel(priv, ring->index,
2159 ring->prod_index, TDMA_PROD_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002160out:
Doug Bergerb0447ec2017-10-25 15:04:17 -07002161 spin_unlock(&ring->lock);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002162
2163 return ret;
Doug Berger876dbad2017-07-14 16:12:09 -07002164
2165out_unmap_frags:
2166 /* Back up for failed control block mapping */
2167 bcmgenet_put_txcb(priv, ring);
2168
2169 /* Unmap successfully mapped control blocks */
2170 while (i-- > 0) {
2171 tx_cb_ptr = bcmgenet_put_txcb(priv, ring);
Doug Bergerf48bed12017-07-14 16:12:10 -07002172 bcmgenet_free_tx_cb(kdev, tx_cb_ptr);
Doug Berger876dbad2017-07-14 16:12:09 -07002173 }
2174
2175 dev_kfree_skb(skb);
2176 goto out;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002177}
2178
Petri Gyntherd6707be2015-03-12 15:48:00 -07002179static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
2180 struct enet_cb *cb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002181{
2182 struct device *kdev = &priv->pdev->dev;
2183 struct sk_buff *skb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002184 struct sk_buff *rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002185 dma_addr_t mapping;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002186
Petri Gyntherd6707be2015-03-12 15:48:00 -07002187 /* Allocate a new Rx skb */
Doug Bergerecaeceb2020-04-23 16:02:11 -07002188 skb = __netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT,
2189 GFP_ATOMIC | __GFP_NOWARN);
Petri Gyntherd6707be2015-03-12 15:48:00 -07002190 if (!skb) {
2191 priv->mib.alloc_rx_buff_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002192 netif_err(priv, rx_err, priv->dev,
Petri Gyntherd6707be2015-03-12 15:48:00 -07002193 "%s: Rx skb allocation failed\n", __func__);
2194 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002195 }
2196
Petri Gyntherd6707be2015-03-12 15:48:00 -07002197 /* DMA-map the new Rx skb */
2198 mapping = dma_map_single(kdev, skb->data, priv->rx_buf_len,
2199 DMA_FROM_DEVICE);
2200 if (dma_mapping_error(kdev, mapping)) {
2201 priv->mib.rx_dma_failed++;
2202 dev_kfree_skb_any(skb);
2203 netif_err(priv, rx_err, priv->dev,
2204 "%s: Rx skb DMA mapping failed\n", __func__);
2205 return NULL;
2206 }
2207
2208 /* Grab the current Rx skb from the ring and DMA-unmap it */
Doug Bergerf48bed12017-07-14 16:12:10 -07002209 rx_skb = bcmgenet_free_rx_cb(kdev, cb);
Petri Gyntherd6707be2015-03-12 15:48:00 -07002210
2211 /* Put the new Rx skb on the ring */
2212 cb->skb = skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002213 dma_unmap_addr_set(cb, dma_addr, mapping);
Doug Bergerf48bed12017-07-14 16:12:10 -07002214 dma_unmap_len_set(cb, dma_len, priv->rx_buf_len);
Petri Gynther8ac467e2015-03-09 13:40:00 -07002215 dmadesc_set_addr(priv, cb->bd_addr, mapping);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002216
Petri Gyntherd6707be2015-03-12 15:48:00 -07002217 /* Return the current Rx skb to caller */
2218 return rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002219}
2220
2221/* bcmgenet_desc_rx - descriptor based rx process.
2222 * this could be called from bottom half, or from NAPI polling method.
2223 */
Petri Gynther4055eae2015-03-25 12:35:16 -07002224static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002225 unsigned int budget)
2226{
Petri Gynther4055eae2015-03-25 12:35:16 -07002227 struct bcmgenet_priv *priv = ring->priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002228 struct net_device *dev = priv->dev;
2229 struct enet_cb *cb;
2230 struct sk_buff *skb;
2231 u32 dma_length_status;
2232 unsigned long dma_flag;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002233 int len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002234 unsigned int rxpktprocessed = 0, rxpkttoprocess;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002235 unsigned int bytes_processed = 0;
Doug Bergerd5810ca2017-03-13 17:41:37 -07002236 unsigned int p_index, mask;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07002237 unsigned int discards;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002238
Doug Bergerd5810ca2017-03-13 17:41:37 -07002239 /* Clear status before servicing to reduce spurious interrupts */
2240 if (ring->index == DESC_INDEX) {
2241 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_RXDMA_DONE,
2242 INTRL2_CPU_CLEAR);
2243 } else {
2244 mask = 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index);
2245 bcmgenet_intrl2_1_writel(priv,
2246 mask,
2247 INTRL2_CPU_CLEAR);
2248 }
2249
Petri Gynther4055eae2015-03-25 12:35:16 -07002250 p_index = bcmgenet_rdma_ring_readl(priv, ring->index, RDMA_PROD_INDEX);
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07002251
2252 discards = (p_index >> DMA_P_INDEX_DISCARD_CNT_SHIFT) &
2253 DMA_P_INDEX_DISCARD_CNT_MASK;
2254 if (discards > ring->old_discards) {
2255 discards = discards - ring->old_discards;
Florian Fainelli37a30b42017-03-16 10:27:08 -07002256 ring->errors += discards;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07002257 ring->old_discards += discards;
2258
2259 /* Clear HW register when we reach 75% of maximum 0xFFFF */
2260 if (ring->old_discards >= 0xC000) {
2261 ring->old_discards = 0;
Petri Gynther4055eae2015-03-25 12:35:16 -07002262 bcmgenet_rdma_ring_writel(priv, ring->index, 0,
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07002263 RDMA_PROD_INDEX);
2264 }
2265 }
2266
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002267 p_index &= DMA_P_INDEX_MASK;
Doug Bergerc298ede2017-03-13 17:41:33 -07002268 rxpkttoprocess = (p_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002269
2270 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002271 "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002272
2273 while ((rxpktprocessed < rxpkttoprocess) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07002274 (rxpktprocessed < budget)) {
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002275 struct status_64 *status;
2276 __be16 rx_csum;
2277
Petri Gynther8ac467e2015-03-09 13:40:00 -07002278 cb = &priv->rx_cbs[ring->read_ptr];
Petri Gyntherd6707be2015-03-12 15:48:00 -07002279 skb = bcmgenet_rx_refill(priv, cb);
Florian Fainellib629be52014-09-08 11:37:52 -07002280
Florian Fainellib629be52014-09-08 11:37:52 -07002281 if (unlikely(!skb)) {
Florian Fainelli37a30b42017-03-16 10:27:08 -07002282 ring->dropped++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002283 goto next;
Florian Fainellib629be52014-09-08 11:37:52 -07002284 }
2285
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002286 status = (struct status_64 *)skb->data;
2287 dma_length_status = status->length_status;
2288 if (dev->features & NETIF_F_RXCSUM) {
Doug Berger81015532019-12-17 16:51:10 -08002289 rx_csum = (__force __be16)(status->rx_csum & 0xffff);
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002290 skb->csum = (__force __wsum)ntohs(rx_csum);
2291 skb->ip_summed = CHECKSUM_COMPLETE;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002292 }
2293
2294 /* DMA flags and length are still valid no matter how
2295 * we got the Receive Status Vector (64B RSB or register)
2296 */
2297 dma_flag = dma_length_status & 0xffff;
2298 len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
2299
2300 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002301 "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
Petri Gynther8ac467e2015-03-09 13:40:00 -07002302 __func__, p_index, ring->c_index,
2303 ring->read_ptr, dma_length_status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002304
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002305 if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
2306 netif_err(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002307 "dropping fragmented packet!\n");
Florian Fainelli37a30b42017-03-16 10:27:08 -07002308 ring->errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002309 dev_kfree_skb_any(skb);
2310 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002311 }
Petri Gyntherd6707be2015-03-12 15:48:00 -07002312
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002313 /* report errors */
2314 if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
2315 DMA_RX_OV |
2316 DMA_RX_NO |
2317 DMA_RX_LG |
2318 DMA_RX_RXER))) {
2319 netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07002320 (unsigned int)dma_flag);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002321 if (dma_flag & DMA_RX_CRC_ERROR)
2322 dev->stats.rx_crc_errors++;
2323 if (dma_flag & DMA_RX_OV)
2324 dev->stats.rx_over_errors++;
2325 if (dma_flag & DMA_RX_NO)
2326 dev->stats.rx_frame_errors++;
2327 if (dma_flag & DMA_RX_LG)
2328 dev->stats.rx_length_errors++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002329 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002330 dev_kfree_skb_any(skb);
2331 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002332 } /* error packet */
2333
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002334 skb_put(skb, len);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002335
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002336 /* remove RSB and hardware 2bytes added for IP alignment */
2337 skb_pull(skb, 66);
2338 len -= 66;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002339
2340 if (priv->crc_fwd_en) {
2341 skb_trim(skb, len - ETH_FCS_LEN);
2342 len -= ETH_FCS_LEN;
2343 }
2344
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002345 bytes_processed += len;
2346
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002347 /*Finish setting up the received SKB and send it to the kernel*/
2348 skb->protocol = eth_type_trans(skb, priv->dev);
Florian Fainelli37a30b42017-03-16 10:27:08 -07002349 ring->packets++;
2350 ring->bytes += len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002351 if (dma_flag & DMA_RX_MULT)
2352 dev->stats.multicast++;
2353
2354 /* Notify kernel */
Petri Gynther4055eae2015-03-25 12:35:16 -07002355 napi_gro_receive(&ring->napi, skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002356 netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
2357
Petri Gyntherd6707be2015-03-12 15:48:00 -07002358next:
Florian Fainellicf377d82014-10-10 10:51:52 -07002359 rxpktprocessed++;
Petri Gynther8ac467e2015-03-09 13:40:00 -07002360 if (likely(ring->read_ptr < ring->end_ptr))
2361 ring->read_ptr++;
2362 else
2363 ring->read_ptr = ring->cb_ptr;
2364
2365 ring->c_index = (ring->c_index + 1) & DMA_C_INDEX_MASK;
Petri Gynther4055eae2015-03-25 12:35:16 -07002366 bcmgenet_rdma_ring_writel(priv, ring->index, ring->c_index, RDMA_CONS_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002367 }
2368
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002369 ring->dim.bytes = bytes_processed;
2370 ring->dim.packets = rxpktprocessed;
2371
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002372 return rxpktprocessed;
2373}
2374
Petri Gynther3ab11332015-03-25 12:35:15 -07002375/* Rx NAPI polling method */
2376static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
2377{
Petri Gynther4055eae2015-03-25 12:35:16 -07002378 struct bcmgenet_rx_ring *ring = container_of(napi,
2379 struct bcmgenet_rx_ring, napi);
Yamin Friedmanf06d0ca2019-07-23 10:22:47 +03002380 struct dim_sample dim_sample = {};
Petri Gynther3ab11332015-03-25 12:35:15 -07002381 unsigned int work_done;
2382
Petri Gynther4055eae2015-03-25 12:35:16 -07002383 work_done = bcmgenet_desc_rx(ring, budget);
Petri Gynther3ab11332015-03-25 12:35:15 -07002384
2385 if (work_done < budget) {
Eric Dumazeteb96ce02016-04-08 22:06:40 -07002386 napi_complete_done(napi, work_done);
Petri Gynther4055eae2015-03-25 12:35:16 -07002387 ring->int_enable(ring);
Petri Gynther3ab11332015-03-25 12:35:15 -07002388 }
2389
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002390 if (ring->dim.use_dim) {
Tal Gilboa8960b382019-01-31 16:44:48 +02002391 dim_update_sample(ring->dim.event_ctr, ring->dim.packets,
2392 ring->dim.bytes, &dim_sample);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002393 net_dim(&ring->dim.dim, dim_sample);
2394 }
2395
Petri Gynther3ab11332015-03-25 12:35:15 -07002396 return work_done;
2397}
2398
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002399static void bcmgenet_dim_work(struct work_struct *work)
2400{
Tal Gilboa8960b382019-01-31 16:44:48 +02002401 struct dim *dim = container_of(work, struct dim, work);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002402 struct bcmgenet_net_dim *ndim =
2403 container_of(dim, struct bcmgenet_net_dim, dim);
2404 struct bcmgenet_rx_ring *ring =
2405 container_of(ndim, struct bcmgenet_rx_ring, dim);
Tal Gilboa8960b382019-01-31 16:44:48 +02002406 struct dim_cq_moder cur_profile =
Tal Gilboa026a8072018-04-24 13:36:01 +03002407 net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002408
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002409 bcmgenet_set_rx_coalesce(ring, cur_profile.usec, cur_profile.pkts);
Tal Gilboac002bd52018-11-05 12:07:52 +02002410 dim->state = DIM_START_MEASURE;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002411}
2412
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002413/* Assign skb to RX DMA descriptor. */
Petri Gynther8ac467e2015-03-09 13:40:00 -07002414static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv,
2415 struct bcmgenet_rx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002416{
2417 struct enet_cb *cb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002418 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002419 int i;
2420
Petri Gynther8ac467e2015-03-09 13:40:00 -07002421 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002422
2423 /* loop here for each buffer needing assign */
Petri Gynther8ac467e2015-03-09 13:40:00 -07002424 for (i = 0; i < ring->size; i++) {
2425 cb = ring->cbs + i;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002426 skb = bcmgenet_rx_refill(priv, cb);
2427 if (skb)
Florian Fainellid4fec852017-08-24 15:56:29 -07002428 dev_consume_skb_any(skb);
Petri Gyntherd6707be2015-03-12 15:48:00 -07002429 if (!cb->skb)
2430 return -ENOMEM;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002431 }
2432
Petri Gyntherd6707be2015-03-12 15:48:00 -07002433 return 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002434}
2435
2436static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
2437{
Doug Bergerf48bed12017-07-14 16:12:10 -07002438 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002439 struct enet_cb *cb;
2440 int i;
2441
2442 for (i = 0; i < priv->num_rx_bds; i++) {
2443 cb = &priv->rx_cbs[i];
2444
Doug Bergerf48bed12017-07-14 16:12:10 -07002445 skb = bcmgenet_free_rx_cb(&priv->pdev->dev, cb);
2446 if (skb)
Florian Fainellid4fec852017-08-24 15:56:29 -07002447 dev_consume_skb_any(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002448 }
2449}
2450
Florian Fainellic91b7f62014-07-23 10:42:12 -07002451static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable)
Florian Fainellie29585b2014-07-21 15:29:20 -07002452{
2453 u32 reg;
2454
2455 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
Doug Berger88f6c8b2020-03-16 14:44:56 -07002456 if (reg & CMD_SW_RESET)
2457 return;
Florian Fainellie29585b2014-07-21 15:29:20 -07002458 if (enable)
2459 reg |= mask;
2460 else
2461 reg &= ~mask;
2462 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
2463
2464 /* UniMAC stops on a packet boundary, wait for a full-size packet
2465 * to be processed
2466 */
2467 if (enable == 0)
2468 usleep_range(1000, 2000);
2469}
2470
Doug Berger28c2d1a2017-10-25 15:04:13 -07002471static void reset_umac(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002472{
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002473 /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
2474 bcmgenet_rbuf_ctrl_set(priv, 0);
2475 udelay(10);
2476
Doug Berger88f6c8b2020-03-16 14:44:56 -07002477 /* issue soft reset and disable MAC while updating its registers */
2478 bcmgenet_umac_writel(priv, CMD_SW_RESET, UMAC_CMD);
Doug Berger612eb1c2020-03-16 14:44:55 -07002479 udelay(2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002480}
2481
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002482static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
2483{
2484 /* Mask all interrupts.*/
2485 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
2486 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002487 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
2488 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002489}
2490
Florian Fainelli37850e32015-10-17 14:22:46 -07002491static void bcmgenet_link_intr_enable(struct bcmgenet_priv *priv)
2492{
2493 u32 int0_enable = 0;
2494
2495 /* Monitor cable plug/unplugged event for internal PHY, external PHY
2496 * and MoCA PHY
2497 */
2498 if (priv->internal_phy) {
2499 int0_enable |= UMAC_IRQ_LINK_EVENT;
Doug Berger25382b92019-10-16 16:06:32 -07002500 if (GENET_IS_V1(priv) || GENET_IS_V2(priv) || GENET_IS_V3(priv))
2501 int0_enable |= UMAC_IRQ_PHY_DET_R;
Florian Fainelli37850e32015-10-17 14:22:46 -07002502 } else if (priv->ext_phy) {
2503 int0_enable |= UMAC_IRQ_LINK_EVENT;
2504 } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
2505 if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
2506 int0_enable |= UMAC_IRQ_LINK_EVENT;
2507 }
2508 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
2509}
2510
Doug Berger28c2d1a2017-10-25 15:04:13 -07002511static void init_umac(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002512{
2513 struct device *kdev = &priv->pdev->dev;
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002514 u32 reg;
2515 u32 int0_enable = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002516
2517 dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
2518
Doug Berger28c2d1a2017-10-25 15:04:13 -07002519 reset_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002520
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002521 /* clear tx/rx counter */
2522 bcmgenet_umac_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002523 MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
2524 UMAC_MIB_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002525 bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
2526
2527 bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
2528
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002529 /* init tx registers, enable TSB */
2530 reg = bcmgenet_tbuf_ctrl_get(priv);
2531 reg |= TBUF_64B_EN;
2532 bcmgenet_tbuf_ctrl_set(priv, reg);
2533
2534 /* init rx registers, enable ip header optimization and RSB */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002535 reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002536 reg |= RBUF_ALIGN_2B | RBUF_64B_EN;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002537 bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
2538
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002539 /* enable rx checksumming */
2540 reg = bcmgenet_rbuf_readl(priv, RBUF_CHK_CTRL);
2541 reg |= RBUF_RXCHK_EN | RBUF_L3_PARSE_DIS;
2542 /* If UniMAC forwards CRC, we need to skip over it to get
2543 * a valid CHK bit to be set in the per-packet status word
2544 */
2545 if (priv->crc_fwd_en)
2546 reg |= RBUF_SKIP_FCS;
2547 else
2548 reg &= ~RBUF_SKIP_FCS;
2549 bcmgenet_rbuf_writel(priv, reg, RBUF_CHK_CTRL);
2550
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002551 if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
2552 bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
2553
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002554 bcmgenet_intr_disable(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002555
Florian Fainelli37850e32015-10-17 14:22:46 -07002556 /* Configure backpressure vectors for MoCA */
2557 if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002558 reg = bcmgenet_bp_mc_get(priv);
2559 reg |= BIT(priv->hw_params->bp_in_en_shift);
2560
2561 /* bp_mask: back pressure mask */
2562 if (netif_is_multiqueue(priv->dev))
2563 reg |= priv->hw_params->bp_in_mask;
2564 else
2565 reg &= ~priv->hw_params->bp_in_mask;
2566 bcmgenet_bp_mc_set(priv, reg);
2567 }
2568
2569 /* Enable MDIO interrupts on GENET v3+ */
2570 if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002571 int0_enable |= (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002572
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002573 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002574
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002575 dev_dbg(kdev, "done init umac\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002576}
2577
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002578static void bcmgenet_init_dim(struct bcmgenet_rx_ring *ring,
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002579 void (*cb)(struct work_struct *work))
2580{
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002581 struct bcmgenet_net_dim *dim = &ring->dim;
2582
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002583 INIT_WORK(&dim->dim.work, cb);
Tal Gilboac002bd52018-11-05 12:07:52 +02002584 dim->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002585 dim->event_ctr = 0;
2586 dim->packets = 0;
2587 dim->bytes = 0;
2588}
2589
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002590static void bcmgenet_init_rx_coalesce(struct bcmgenet_rx_ring *ring)
2591{
2592 struct bcmgenet_net_dim *dim = &ring->dim;
Tal Gilboa8960b382019-01-31 16:44:48 +02002593 struct dim_cq_moder moder;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002594 u32 usecs, pkts;
2595
2596 usecs = ring->rx_coalesce_usecs;
2597 pkts = ring->rx_max_coalesced_frames;
2598
2599 /* If DIM was enabled, re-apply default parameters */
2600 if (dim->use_dim) {
Tal Gilboa026a8072018-04-24 13:36:01 +03002601 moder = net_dim_get_def_rx_moderation(dim->dim.mode);
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002602 usecs = moder.usec;
2603 pkts = moder.pkts;
2604 }
2605
2606 bcmgenet_set_rx_coalesce(ring, usecs, pkts);
2607}
2608
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002609/* Initialize a Tx ring along with corresponding hardware registers */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002610static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
2611 unsigned int index, unsigned int size,
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002612 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002613{
2614 struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
2615 u32 words_per_bd = WORDS_PER_BD(priv);
2616 u32 flow_period_val = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002617
2618 spin_lock_init(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002619 ring->priv = priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002620 ring->index = index;
2621 if (index == DESC_INDEX) {
2622 ring->queue = 0;
2623 ring->int_enable = bcmgenet_tx_ring16_int_enable;
2624 ring->int_disable = bcmgenet_tx_ring16_int_disable;
2625 } else {
2626 ring->queue = index + 1;
2627 ring->int_enable = bcmgenet_tx_ring_int_enable;
2628 ring->int_disable = bcmgenet_tx_ring_int_disable;
2629 }
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002630 ring->cbs = priv->tx_cbs + start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002631 ring->size = size;
Petri Gynther66d06752015-03-04 14:30:01 -08002632 ring->clean_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002633 ring->c_index = 0;
2634 ring->free_bds = size;
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002635 ring->write_ptr = start_ptr;
2636 ring->cb_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002637 ring->end_ptr = end_ptr - 1;
2638 ring->prod_index = 0;
2639
2640 /* Set flow period for ring != 16 */
2641 if (index != DESC_INDEX)
2642 flow_period_val = ENET_MAX_MTU_SIZE << 16;
2643
2644 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
2645 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
2646 bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
2647 /* Disable rate control for now */
2648 bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002649 TDMA_FLOW_PERIOD);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002650 bcmgenet_tdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002651 ((size << DMA_RING_SIZE_SHIFT) |
2652 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002653
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002654 /* Set start and end address, read and write pointers */
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002655 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002656 DMA_START_ADDR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002657 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002658 TDMA_READ_PTR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002659 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002660 TDMA_WRITE_PTR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002661 bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002662 DMA_END_ADDR);
Doug Berger75879352017-10-25 15:04:14 -07002663
2664 /* Initialize Tx NAPI */
Florian Fainelli148965d2020-01-23 09:49:34 -08002665 netif_tx_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll,
2666 NAPI_POLL_WEIGHT);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002667}
2668
2669/* Initialize a RDMA ring */
2670static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
Petri Gynther8ac467e2015-03-09 13:40:00 -07002671 unsigned int index, unsigned int size,
2672 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002673{
Petri Gynther8ac467e2015-03-09 13:40:00 -07002674 struct bcmgenet_rx_ring *ring = &priv->rx_rings[index];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002675 u32 words_per_bd = WORDS_PER_BD(priv);
2676 int ret;
2677
Petri Gynther4055eae2015-03-25 12:35:16 -07002678 ring->priv = priv;
Petri Gynther8ac467e2015-03-09 13:40:00 -07002679 ring->index = index;
Petri Gynther4055eae2015-03-25 12:35:16 -07002680 if (index == DESC_INDEX) {
2681 ring->int_enable = bcmgenet_rx_ring16_int_enable;
2682 ring->int_disable = bcmgenet_rx_ring16_int_disable;
2683 } else {
2684 ring->int_enable = bcmgenet_rx_ring_int_enable;
2685 ring->int_disable = bcmgenet_rx_ring_int_disable;
2686 }
Petri Gynther8ac467e2015-03-09 13:40:00 -07002687 ring->cbs = priv->rx_cbs + start_ptr;
2688 ring->size = size;
2689 ring->c_index = 0;
2690 ring->read_ptr = start_ptr;
2691 ring->cb_ptr = start_ptr;
2692 ring->end_ptr = end_ptr - 1;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002693
Petri Gynther8ac467e2015-03-09 13:40:00 -07002694 ret = bcmgenet_alloc_rx_buffers(priv, ring);
2695 if (ret)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002696 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002697
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002698 bcmgenet_init_dim(ring, bcmgenet_dim_work);
2699 bcmgenet_init_rx_coalesce(ring);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002700
Doug Berger75879352017-10-25 15:04:14 -07002701 /* Initialize Rx NAPI */
2702 netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll,
2703 NAPI_POLL_WEIGHT);
2704
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002705 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
2706 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
2707 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002708 ((size << DMA_RING_SIZE_SHIFT) |
2709 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002710 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002711 (DMA_FC_THRESH_LO <<
2712 DMA_XOFF_THRESHOLD_SHIFT) |
2713 DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
Petri Gynther6f5a2722015-03-06 13:45:00 -08002714
2715 /* Set start and end address, read and write pointers */
Petri Gynther8ac467e2015-03-09 13:40:00 -07002716 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2717 DMA_START_ADDR);
2718 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2719 RDMA_READ_PTR);
2720 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2721 RDMA_WRITE_PTR);
2722 bcmgenet_rdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Petri Gynther6f5a2722015-03-06 13:45:00 -08002723 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002724
2725 return ret;
2726}
2727
Petri Gynthere2aadb42015-03-25 12:35:14 -07002728static void bcmgenet_enable_tx_napi(struct bcmgenet_priv *priv)
2729{
2730 unsigned int i;
2731 struct bcmgenet_tx_ring *ring;
2732
2733 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2734 ring = &priv->tx_rings[i];
2735 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002736 ring->int_enable(ring);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002737 }
2738
2739 ring = &priv->tx_rings[DESC_INDEX];
2740 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002741 ring->int_enable(ring);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002742}
2743
2744static void bcmgenet_disable_tx_napi(struct bcmgenet_priv *priv)
2745{
2746 unsigned int i;
2747 struct bcmgenet_tx_ring *ring;
2748
2749 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2750 ring = &priv->tx_rings[i];
2751 napi_disable(&ring->napi);
2752 }
2753
2754 ring = &priv->tx_rings[DESC_INDEX];
2755 napi_disable(&ring->napi);
2756}
2757
2758static void bcmgenet_fini_tx_napi(struct bcmgenet_priv *priv)
2759{
2760 unsigned int i;
2761 struct bcmgenet_tx_ring *ring;
2762
2763 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2764 ring = &priv->tx_rings[i];
2765 netif_napi_del(&ring->napi);
2766 }
2767
2768 ring = &priv->tx_rings[DESC_INDEX];
2769 netif_napi_del(&ring->napi);
2770}
2771
Petri Gynther16c6d662015-02-23 11:00:45 -08002772/* Initialize Tx queues
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002773 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002774 * Queues 0-3 are priority-based, each one has 32 descriptors,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002775 * with queue 0 being the highest priority queue.
2776 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002777 * Queue 16 is the default Tx queue with
Petri Gynther51a966a2015-02-23 11:00:46 -08002778 * GENET_Q16_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors.
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002779 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002780 * The transmit control block pool is then partitioned as follows:
2781 * - Tx queue 0 uses tx_cbs[0..31]
2782 * - Tx queue 1 uses tx_cbs[32..63]
2783 * - Tx queue 2 uses tx_cbs[64..95]
2784 * - Tx queue 3 uses tx_cbs[96..127]
2785 * - Tx queue 16 uses tx_cbs[128..255]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002786 */
Petri Gynther16c6d662015-02-23 11:00:45 -08002787static void bcmgenet_init_tx_queues(struct net_device *dev)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002788{
2789 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther16c6d662015-02-23 11:00:45 -08002790 u32 i, dma_enable;
2791 u32 dma_ctrl, ring_cfg;
Petri Gynther37742162014-10-07 09:30:01 -07002792 u32 dma_priority[3] = {0, 0, 0};
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002793
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002794 dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
2795 dma_enable = dma_ctrl & DMA_EN;
2796 dma_ctrl &= ~DMA_EN;
2797 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
2798
Petri Gynther16c6d662015-02-23 11:00:45 -08002799 dma_ctrl = 0;
2800 ring_cfg = 0;
2801
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002802 /* Enable strict priority arbiter mode */
2803 bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
2804
Petri Gynther16c6d662015-02-23 11:00:45 -08002805 /* Initialize Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002806 for (i = 0; i < priv->hw_params->tx_queues; i++) {
Petri Gynther51a966a2015-02-23 11:00:46 -08002807 bcmgenet_init_tx_ring(priv, i, priv->hw_params->tx_bds_per_q,
2808 i * priv->hw_params->tx_bds_per_q,
2809 (i + 1) * priv->hw_params->tx_bds_per_q);
Petri Gynther16c6d662015-02-23 11:00:45 -08002810 ring_cfg |= (1 << i);
2811 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002812 dma_priority[DMA_PRIO_REG_INDEX(i)] |=
2813 ((GENET_Q0_PRIORITY + i) << DMA_PRIO_REG_SHIFT(i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002814 }
2815
Petri Gynther16c6d662015-02-23 11:00:45 -08002816 /* Initialize Tx default queue 16 */
Petri Gynther51a966a2015-02-23 11:00:46 -08002817 bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_Q16_TX_BD_CNT,
Petri Gynther16c6d662015-02-23 11:00:45 -08002818 priv->hw_params->tx_queues *
Petri Gynther51a966a2015-02-23 11:00:46 -08002819 priv->hw_params->tx_bds_per_q,
Petri Gynther16c6d662015-02-23 11:00:45 -08002820 TOTAL_DESC);
2821 ring_cfg |= (1 << DESC_INDEX);
2822 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002823 dma_priority[DMA_PRIO_REG_INDEX(DESC_INDEX)] |=
2824 ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) <<
2825 DMA_PRIO_REG_SHIFT(DESC_INDEX));
Petri Gynther16c6d662015-02-23 11:00:45 -08002826
2827 /* Set Tx queue priorities */
Petri Gynther37742162014-10-07 09:30:01 -07002828 bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
2829 bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
2830 bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);
2831
Petri Gynther16c6d662015-02-23 11:00:45 -08002832 /* Enable Tx queues */
2833 bcmgenet_tdma_writel(priv, ring_cfg, DMA_RING_CFG);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002834
Petri Gynther16c6d662015-02-23 11:00:45 -08002835 /* Enable Tx DMA */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002836 if (dma_enable)
Petri Gynther16c6d662015-02-23 11:00:45 -08002837 dma_ctrl |= DMA_EN;
2838 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002839}
2840
Petri Gynther3ab11332015-03-25 12:35:15 -07002841static void bcmgenet_enable_rx_napi(struct bcmgenet_priv *priv)
2842{
Petri Gynther4055eae2015-03-25 12:35:16 -07002843 unsigned int i;
2844 struct bcmgenet_rx_ring *ring;
2845
2846 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2847 ring = &priv->rx_rings[i];
2848 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002849 ring->int_enable(ring);
Petri Gynther4055eae2015-03-25 12:35:16 -07002850 }
2851
2852 ring = &priv->rx_rings[DESC_INDEX];
2853 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002854 ring->int_enable(ring);
Petri Gynther3ab11332015-03-25 12:35:15 -07002855}
2856
2857static void bcmgenet_disable_rx_napi(struct bcmgenet_priv *priv)
2858{
Petri Gynther4055eae2015-03-25 12:35:16 -07002859 unsigned int i;
2860 struct bcmgenet_rx_ring *ring;
2861
2862 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2863 ring = &priv->rx_rings[i];
2864 napi_disable(&ring->napi);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002865 cancel_work_sync(&ring->dim.dim.work);
Petri Gynther4055eae2015-03-25 12:35:16 -07002866 }
2867
2868 ring = &priv->rx_rings[DESC_INDEX];
2869 napi_disable(&ring->napi);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002870 cancel_work_sync(&ring->dim.dim.work);
Petri Gynther3ab11332015-03-25 12:35:15 -07002871}
2872
2873static void bcmgenet_fini_rx_napi(struct bcmgenet_priv *priv)
2874{
Petri Gynther4055eae2015-03-25 12:35:16 -07002875 unsigned int i;
2876 struct bcmgenet_rx_ring *ring;
2877
2878 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2879 ring = &priv->rx_rings[i];
2880 netif_napi_del(&ring->napi);
2881 }
2882
2883 ring = &priv->rx_rings[DESC_INDEX];
2884 netif_napi_del(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002885}
2886
Petri Gynther8ac467e2015-03-09 13:40:00 -07002887/* Initialize Rx queues
2888 *
2889 * Queues 0-15 are priority queues. Hardware Filtering Block (HFB) can be
2890 * used to direct traffic to these queues.
2891 *
2892 * Queue 16 is the default Rx queue with GENET_Q16_RX_BD_CNT descriptors.
2893 */
2894static int bcmgenet_init_rx_queues(struct net_device *dev)
2895{
2896 struct bcmgenet_priv *priv = netdev_priv(dev);
2897 u32 i;
2898 u32 dma_enable;
2899 u32 dma_ctrl;
2900 u32 ring_cfg;
2901 int ret;
2902
2903 dma_ctrl = bcmgenet_rdma_readl(priv, DMA_CTRL);
2904 dma_enable = dma_ctrl & DMA_EN;
2905 dma_ctrl &= ~DMA_EN;
2906 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2907
2908 dma_ctrl = 0;
2909 ring_cfg = 0;
2910
2911 /* Initialize Rx priority queues */
2912 for (i = 0; i < priv->hw_params->rx_queues; i++) {
2913 ret = bcmgenet_init_rx_ring(priv, i,
2914 priv->hw_params->rx_bds_per_q,
2915 i * priv->hw_params->rx_bds_per_q,
2916 (i + 1) *
2917 priv->hw_params->rx_bds_per_q);
2918 if (ret)
2919 return ret;
2920
2921 ring_cfg |= (1 << i);
2922 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2923 }
2924
2925 /* Initialize Rx default queue 16 */
2926 ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, GENET_Q16_RX_BD_CNT,
2927 priv->hw_params->rx_queues *
2928 priv->hw_params->rx_bds_per_q,
2929 TOTAL_DESC);
2930 if (ret)
2931 return ret;
2932
2933 ring_cfg |= (1 << DESC_INDEX);
2934 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
2935
2936 /* Enable rings */
2937 bcmgenet_rdma_writel(priv, ring_cfg, DMA_RING_CFG);
2938
2939 /* Configure ring as descriptor ring and re-enable DMA if enabled */
2940 if (dma_enable)
2941 dma_ctrl |= DMA_EN;
2942 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2943
2944 return 0;
2945}
2946
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002947static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
2948{
2949 int ret = 0;
2950 int timeout = 0;
2951 u32 reg;
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002952 u32 dma_ctrl;
2953 int i;
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002954
2955 /* Disable TDMA to stop add more frames in TX DMA */
2956 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2957 reg &= ~DMA_EN;
2958 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2959
2960 /* Check TDMA status register to confirm TDMA is disabled */
2961 while (timeout++ < DMA_TIMEOUT_VAL) {
2962 reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
2963 if (reg & DMA_DISABLED)
2964 break;
2965
2966 udelay(1);
2967 }
2968
2969 if (timeout == DMA_TIMEOUT_VAL) {
2970 netdev_warn(priv->dev, "Timed out while disabling TX DMA\n");
2971 ret = -ETIMEDOUT;
2972 }
2973
2974 /* Wait 10ms for packet drain in both tx and rx dma */
2975 usleep_range(10000, 20000);
2976
2977 /* Disable RDMA */
2978 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2979 reg &= ~DMA_EN;
2980 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2981
2982 timeout = 0;
2983 /* Check RDMA status register to confirm RDMA is disabled */
2984 while (timeout++ < DMA_TIMEOUT_VAL) {
2985 reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
2986 if (reg & DMA_DISABLED)
2987 break;
2988
2989 udelay(1);
2990 }
2991
2992 if (timeout == DMA_TIMEOUT_VAL) {
2993 netdev_warn(priv->dev, "Timed out while disabling RX DMA\n");
2994 ret = -ETIMEDOUT;
2995 }
2996
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002997 dma_ctrl = 0;
2998 for (i = 0; i < priv->hw_params->rx_queues; i++)
2999 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
3000 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
3001 reg &= ~dma_ctrl;
3002 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
3003
3004 dma_ctrl = 0;
3005 for (i = 0; i < priv->hw_params->tx_queues; i++)
3006 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
3007 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
3008 reg &= ~dma_ctrl;
3009 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
3010
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07003011 return ret;
3012}
3013
Petri Gynther9abab962015-03-30 00:29:01 -07003014static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003015{
Petri Gynthere178c8c2016-04-09 00:20:36 -07003016 struct netdev_queue *txq;
Doug Bergerf48bed12017-07-14 16:12:10 -07003017 int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003018
Petri Gynther9abab962015-03-30 00:29:01 -07003019 bcmgenet_fini_rx_napi(priv);
3020 bcmgenet_fini_tx_napi(priv);
3021
Markus Elfring399e06a2019-08-22 20:02:56 +02003022 for (i = 0; i < priv->num_tx_bds; i++)
3023 dev_kfree_skb(bcmgenet_free_tx_cb(&priv->pdev->dev,
3024 priv->tx_cbs + i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003025
Petri Gynthere178c8c2016-04-09 00:20:36 -07003026 for (i = 0; i < priv->hw_params->tx_queues; i++) {
3027 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[i].queue);
3028 netdev_tx_reset_queue(txq);
3029 }
3030
3031 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[DESC_INDEX].queue);
3032 netdev_tx_reset_queue(txq);
3033
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003034 bcmgenet_free_rx_buffers(priv);
3035 kfree(priv->rx_cbs);
3036 kfree(priv->tx_cbs);
3037}
3038
3039/* init_edma: Initialize DMA control register */
3040static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
3041{
3042 int ret;
Petri Gynther014012a2015-02-23 11:00:45 -08003043 unsigned int i;
3044 struct enet_cb *cb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003045
Petri Gynther6f5a2722015-03-06 13:45:00 -08003046 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003047
Petri Gynther6f5a2722015-03-06 13:45:00 -08003048 /* Initialize common Rx ring structures */
3049 priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
3050 priv->num_rx_bds = TOTAL_DESC;
3051 priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct enet_cb),
3052 GFP_KERNEL);
3053 if (!priv->rx_cbs)
3054 return -ENOMEM;
3055
3056 for (i = 0; i < priv->num_rx_bds; i++) {
3057 cb = priv->rx_cbs + i;
3058 cb->bd_addr = priv->rx_bds + i * DMA_DESC_SIZE;
3059 }
3060
Brian Norris7fc527f2014-07-29 14:34:14 -07003061 /* Initialize common TX ring structures */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003062 priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
3063 priv->num_tx_bds = TOTAL_DESC;
Florian Fainellic489be02014-07-23 10:42:15 -07003064 priv->tx_cbs = kcalloc(priv->num_tx_bds, sizeof(struct enet_cb),
Florian Fainellic91b7f62014-07-23 10:42:12 -07003065 GFP_KERNEL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003066 if (!priv->tx_cbs) {
Petri Gyntherebbd96f2015-03-25 12:35:11 -07003067 kfree(priv->rx_cbs);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003068 return -ENOMEM;
3069 }
3070
Petri Gynther014012a2015-02-23 11:00:45 -08003071 for (i = 0; i < priv->num_tx_bds; i++) {
3072 cb = priv->tx_cbs + i;
3073 cb->bd_addr = priv->tx_bds + i * DMA_DESC_SIZE;
3074 }
3075
Petri Gyntherebbd96f2015-03-25 12:35:11 -07003076 /* Init rDma */
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003077 bcmgenet_rdma_writel(priv, priv->dma_max_burst_length,
3078 DMA_SCB_BURST_SIZE);
Petri Gyntherebbd96f2015-03-25 12:35:11 -07003079
3080 /* Initialize Rx queues */
3081 ret = bcmgenet_init_rx_queues(priv->dev);
3082 if (ret) {
3083 netdev_err(priv->dev, "failed to initialize Rx queues\n");
3084 bcmgenet_free_rx_buffers(priv);
3085 kfree(priv->rx_cbs);
3086 kfree(priv->tx_cbs);
3087 return ret;
3088 }
3089
3090 /* Init tDma */
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003091 bcmgenet_tdma_writel(priv, priv->dma_max_burst_length,
3092 DMA_SCB_BURST_SIZE);
Petri Gyntherebbd96f2015-03-25 12:35:11 -07003093
Petri Gynther16c6d662015-02-23 11:00:45 -08003094 /* Initialize Tx queues */
3095 bcmgenet_init_tx_queues(priv->dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003096
3097 return 0;
3098}
3099
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003100/* Interrupt bottom half */
3101static void bcmgenet_irq_task(struct work_struct *work)
3102{
Doug Berger07c52d62017-03-09 16:58:47 -08003103 unsigned int status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003104 struct bcmgenet_priv *priv = container_of(
3105 work, struct bcmgenet_priv, bcmgenet_irq_work);
3106
3107 netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
3108
Doug Bergerb0447ec2017-10-25 15:04:17 -07003109 spin_lock_irq(&priv->lock);
Doug Berger07c52d62017-03-09 16:58:47 -08003110 status = priv->irq0_stat;
3111 priv->irq0_stat = 0;
Doug Bergerb0447ec2017-10-25 15:04:17 -07003112 spin_unlock_irq(&priv->lock);
Doug Berger07c52d62017-03-09 16:58:47 -08003113
Doug Berger25382b92019-10-16 16:06:32 -07003114 if (status & UMAC_IRQ_PHY_DET_R &&
Doug Berger0686bd92019-11-05 11:07:26 -08003115 priv->dev->phydev->autoneg != AUTONEG_ENABLE) {
Doug Berger25382b92019-10-16 16:06:32 -07003116 phy_init_hw(priv->dev->phydev);
Doug Berger0686bd92019-11-05 11:07:26 -08003117 genphy_config_aneg(priv->dev->phydev);
3118 }
Doug Berger25382b92019-10-16 16:06:32 -07003119
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003120 /* Link UP/DOWN event */
Doug Berger7de48402019-10-16 16:06:29 -07003121 if (status & UMAC_IRQ_LINK_EVENT)
Heiner Kallweit28b2e0d2018-01-10 21:21:31 +01003122 phy_mac_interrupt(priv->dev->phydev);
Doug Berger25382b92019-10-16 16:06:32 -07003123
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003124}
3125
Petri Gynther4055eae2015-03-25 12:35:16 -07003126/* bcmgenet_isr1: handle Rx and Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003127static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
3128{
3129 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07003130 struct bcmgenet_rx_ring *rx_ring;
3131 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08003132 unsigned int index, status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003133
Doug Berger07c52d62017-03-09 16:58:47 -08003134 /* Read irq status */
3135 status = bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003136 ~bcmgenet_intrl2_1_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_1_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 "%s: IRQ=0x%x\n", __func__, status);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003143
Petri Gynther4055eae2015-03-25 12:35:16 -07003144 /* Check Rx priority queue interrupts */
3145 for (index = 0; index < priv->hw_params->rx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08003146 if (!(status & BIT(UMAC_IRQ1_RX_INTR_SHIFT + index)))
Petri Gynther4055eae2015-03-25 12:35:16 -07003147 continue;
3148
3149 rx_ring = &priv->rx_rings[index];
Florian Fainelli9f4ca052018-03-22 18:19:33 -07003150 rx_ring->dim.event_ctr++;
Petri Gynther4055eae2015-03-25 12:35:16 -07003151
3152 if (likely(napi_schedule_prep(&rx_ring->napi))) {
3153 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07003154 __napi_schedule_irqoff(&rx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07003155 }
3156 }
3157
3158 /* Check Tx priority queue interrupts */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003159 for (index = 0; index < priv->hw_params->tx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08003160 if (!(status & BIT(index)))
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003161 continue;
3162
Petri Gynther4055eae2015-03-25 12:35:16 -07003163 tx_ring = &priv->tx_rings[index];
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003164
Petri Gynther4055eae2015-03-25 12:35:16 -07003165 if (likely(napi_schedule_prep(&tx_ring->napi))) {
3166 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07003167 __napi_schedule_irqoff(&tx_ring->napi);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003168 }
3169 }
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003170
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003171 return IRQ_HANDLED;
3172}
3173
Petri Gynther4055eae2015-03-25 12:35:16 -07003174/* bcmgenet_isr0: handle Rx and Tx default queues + other stuff */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003175static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
3176{
3177 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07003178 struct bcmgenet_rx_ring *rx_ring;
3179 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08003180 unsigned int status;
3181 unsigned long flags;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003182
Doug Berger07c52d62017-03-09 16:58:47 -08003183 /* Read irq status */
3184 status = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003185 ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07003186
Brian Norris7fc527f2014-07-29 14:34:14 -07003187 /* clear interrupts */
Doug Berger07c52d62017-03-09 16:58:47 -08003188 bcmgenet_intrl2_0_writel(priv, status, INTRL2_CPU_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003189
3190 netif_dbg(priv, intr, priv->dev,
Doug Berger07c52d62017-03-09 16:58:47 -08003191 "IRQ=0x%x\n", status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003192
Doug Berger07c52d62017-03-09 16:58:47 -08003193 if (status & UMAC_IRQ_RXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07003194 rx_ring = &priv->rx_rings[DESC_INDEX];
Florian Fainelli9f4ca052018-03-22 18:19:33 -07003195 rx_ring->dim.event_ctr++;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003196
Petri Gynther4055eae2015-03-25 12:35:16 -07003197 if (likely(napi_schedule_prep(&rx_ring->napi))) {
3198 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07003199 __napi_schedule_irqoff(&rx_ring->napi);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003200 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003201 }
Petri Gynther4055eae2015-03-25 12:35:16 -07003202
Doug Berger07c52d62017-03-09 16:58:47 -08003203 if (status & UMAC_IRQ_TXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07003204 tx_ring = &priv->tx_rings[DESC_INDEX];
3205
3206 if (likely(napi_schedule_prep(&tx_ring->napi))) {
3207 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07003208 __napi_schedule_irqoff(&tx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07003209 }
3210 }
3211
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003212 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Doug Berger07c52d62017-03-09 16:58:47 -08003213 status & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003214 wake_up(&priv->wq);
3215 }
3216
Doug Berger07c52d62017-03-09 16:58:47 -08003217 /* all other interested interrupts handled in bottom half */
Doug Berger25382b92019-10-16 16:06:32 -07003218 status &= (UMAC_IRQ_LINK_EVENT | UMAC_IRQ_PHY_DET_R);
Doug Berger07c52d62017-03-09 16:58:47 -08003219 if (status) {
3220 /* Save irq status for bottom-half processing. */
3221 spin_lock_irqsave(&priv->lock, flags);
3222 priv->irq0_stat |= status;
3223 spin_unlock_irqrestore(&priv->lock, flags);
3224
3225 schedule_work(&priv->bcmgenet_irq_work);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003226 }
3227
3228 return IRQ_HANDLED;
3229}
3230
Florian Fainelli85620562014-07-21 15:29:23 -07003231static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
3232{
Doug Bergereb236c22020-04-30 16:26:51 -07003233 /* Acknowledge the interrupt */
Florian Fainelli85620562014-07-21 15:29:23 -07003234 return IRQ_HANDLED;
3235}
3236
Florian Fainelli4d2e8882015-07-31 11:42:54 -07003237#ifdef CONFIG_NET_POLL_CONTROLLER
3238static void bcmgenet_poll_controller(struct net_device *dev)
3239{
3240 struct bcmgenet_priv *priv = netdev_priv(dev);
3241
3242 /* Invoke the main RX/TX interrupt handler */
3243 disable_irq(priv->irq0);
3244 bcmgenet_isr0(priv->irq0, priv);
3245 enable_irq(priv->irq0);
3246
3247 /* And the interrupt handler for RX/TX priority queues */
3248 disable_irq(priv->irq1);
3249 bcmgenet_isr1(priv->irq1, priv);
3250 enable_irq(priv->irq1);
3251}
3252#endif
3253
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003254static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
3255{
3256 u32 reg;
3257
3258 reg = bcmgenet_rbuf_ctrl_get(priv);
3259 reg |= BIT(1);
3260 bcmgenet_rbuf_ctrl_set(priv, reg);
3261 udelay(10);
3262
3263 reg &= ~BIT(1);
3264 bcmgenet_rbuf_ctrl_set(priv, reg);
3265 udelay(10);
3266}
3267
3268static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
Jakub Kicinski76660752021-10-14 07:24:31 -07003269 const unsigned char *addr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003270{
Andy Shevchenkod2af1422020-04-21 00:51:20 +03003271 bcmgenet_umac_writel(priv, get_unaligned_be32(&addr[0]), UMAC_MAC0);
3272 bcmgenet_umac_writel(priv, get_unaligned_be16(&addr[4]), UMAC_MAC1);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003273}
3274
Jeremy Linton26bd9cc2020-02-24 16:54:02 -06003275static void bcmgenet_get_hw_addr(struct bcmgenet_priv *priv,
3276 unsigned char *addr)
3277{
3278 u32 addr_tmp;
3279
3280 addr_tmp = bcmgenet_umac_readl(priv, UMAC_MAC0);
Andy Shevchenkod2af1422020-04-21 00:51:20 +03003281 put_unaligned_be32(addr_tmp, &addr[0]);
Jeremy Linton26bd9cc2020-02-24 16:54:02 -06003282 addr_tmp = bcmgenet_umac_readl(priv, UMAC_MAC1);
Andy Shevchenkod2af1422020-04-21 00:51:20 +03003283 put_unaligned_be16(addr_tmp, &addr[4]);
Jeremy Linton26bd9cc2020-02-24 16:54:02 -06003284}
3285
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003286/* Returns a reusable dma control register value */
3287static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
3288{
Florian Fainelli2b452552021-07-08 18:55:32 -07003289 unsigned int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003290 u32 reg;
3291 u32 dma_ctrl;
3292
3293 /* disable DMA */
3294 dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
Florian Fainelli2b452552021-07-08 18:55:32 -07003295 for (i = 0; i < priv->hw_params->tx_queues; i++)
3296 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003297 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
3298 reg &= ~dma_ctrl;
3299 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
3300
Florian Fainelli2b452552021-07-08 18:55:32 -07003301 dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
3302 for (i = 0; i < priv->hw_params->rx_queues; i++)
3303 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003304 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
3305 reg &= ~dma_ctrl;
3306 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
3307
3308 bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
3309 udelay(10);
3310 bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
3311
3312 return dma_ctrl;
3313}
3314
3315static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
3316{
3317 u32 reg;
3318
3319 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
3320 reg |= dma_ctrl;
3321 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
3322
3323 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
3324 reg |= dma_ctrl;
3325 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
3326}
3327
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003328static void bcmgenet_netif_start(struct net_device *dev)
3329{
3330 struct bcmgenet_priv *priv = netdev_priv(dev);
3331
3332 /* Start the network engine */
Doug Berger72f96342020-04-29 13:02:00 -07003333 bcmgenet_set_rx_mode(dev);
Petri Gynther3ab11332015-03-25 12:35:15 -07003334 bcmgenet_enable_rx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003335
3336 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
3337
Doug Bergerd215dba2017-10-25 15:04:16 -07003338 bcmgenet_enable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003339
Florian Fainelli37850e32015-10-17 14:22:46 -07003340 /* Monitor link interrupts now */
3341 bcmgenet_link_intr_enable(priv);
3342
Doug Berger6c97f012017-10-25 15:04:19 -07003343 phy_start(dev->phydev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003344}
3345
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003346static int bcmgenet_open(struct net_device *dev)
3347{
3348 struct bcmgenet_priv *priv = netdev_priv(dev);
3349 unsigned long dma_ctrl;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003350 int ret;
3351
3352 netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
3353
3354 /* Turn on the clock */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003355 clk_prepare_enable(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003356
Florian Fainellia642c4f2015-03-23 15:09:56 -07003357 /* If this is an internal GPHY, power it back on now, before UniMAC is
3358 * brought out of reset as absolutely no UniMAC activity is allowed
3359 */
Florian Fainellic624f892015-07-16 15:51:17 -07003360 if (priv->internal_phy)
Florian Fainellia642c4f2015-03-23 15:09:56 -07003361 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3362
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003363 /* take MAC out of reset */
3364 bcmgenet_umac_reset(priv);
3365
Doug Berger28c2d1a2017-10-25 15:04:13 -07003366 init_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003367
Doug Berger206f54b2019-12-17 16:51:12 -08003368 /* Apply features again in case we changed them while interface was
3369 * down
3370 */
3371 bcmgenet_set_features(dev, dev->features);
3372
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003373 bcmgenet_set_hw_addr(priv, dev->dev_addr);
3374
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003375 /* Disable RX/TX DMA and flush TX queues */
3376 dma_ctrl = bcmgenet_dma_disable(priv);
3377
3378 /* Reinitialize TDMA and RDMA and SW housekeeping */
3379 ret = bcmgenet_init_dma(priv);
3380 if (ret) {
3381 netdev_err(dev, "failed to initialize DMA\n");
Doug Berger6b6d017f2019-11-05 11:07:25 -08003382 goto err_clk_disable;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003383 }
3384
3385 /* Always enable ring 16 - descriptor ring */
3386 bcmgenet_enable_dma(priv, dma_ctrl);
3387
Petri Gynther0034de42015-03-13 14:45:00 -07003388 /* HFB init */
3389 bcmgenet_hfb_init(priv);
3390
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003391 ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003392 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003393 if (ret < 0) {
3394 netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
3395 goto err_fini_dma;
3396 }
3397
3398 ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003399 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003400 if (ret < 0) {
3401 netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
3402 goto err_irq0;
3403 }
3404
Doug Berger6b6d017f2019-11-05 11:07:25 -08003405 ret = bcmgenet_mii_probe(dev);
3406 if (ret) {
3407 netdev_err(dev, "failed to connect to PHY\n");
3408 goto err_irq1;
3409 }
3410
Doug Berger2d8bdf522021-09-25 20:21:14 -07003411 bcmgenet_phy_pause_set(dev, priv->rx_pause, priv->tx_pause);
3412
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003413 bcmgenet_netif_start(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003414
Doug Berger09e805d2018-11-01 15:55:37 -07003415 netif_tx_start_all_queues(dev);
3416
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003417 return 0;
3418
Doug Berger6b6d017f2019-11-05 11:07:25 -08003419err_irq1:
3420 free_irq(priv->irq1, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003421err_irq0:
Florian Fainelli978ffac42015-07-16 15:51:15 -07003422 free_irq(priv->irq0, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003423err_fini_dma:
Doug Berger4fd6dc92017-10-25 15:04:12 -07003424 bcmgenet_dma_teardown(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003425 bcmgenet_fini_dma(priv);
3426err_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08003427 if (priv->internal_phy)
3428 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003429 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003430 return ret;
3431}
3432
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003433static void bcmgenet_netif_stop(struct net_device *dev)
3434{
3435 struct bcmgenet_priv *priv = netdev_priv(dev);
3436
Doug Bergerd215dba2017-10-25 15:04:16 -07003437 bcmgenet_disable_tx_napi(priv);
Doug Berger09e805d2018-11-01 15:55:37 -07003438 netif_tx_disable(dev);
Doug Bergerd215dba2017-10-25 15:04:16 -07003439
3440 /* Disable MAC receive */
3441 umac_enable_set(priv, CMD_RX_EN, false);
3442
3443 bcmgenet_dma_teardown(priv);
3444
3445 /* Disable MAC transmit. TX DMA disabled must be done before this */
3446 umac_enable_set(priv, CMD_TX_EN, false);
3447
Doug Berger6c97f012017-10-25 15:04:19 -07003448 phy_stop(dev->phydev);
Petri Gynther3ab11332015-03-25 12:35:15 -07003449 bcmgenet_disable_rx_napi(priv);
Doug Bergerfbf557d2017-10-25 15:04:15 -07003450 bcmgenet_intr_disable(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003451
3452 /* Wait for pending work items to complete. Since interrupts are
3453 * disabled no new work will be scheduled.
3454 */
3455 cancel_work_sync(&priv->bcmgenet_irq_work);
Florian Fainellicc013fb2014-08-11 14:50:43 -07003456
Doug Bergerd215dba2017-10-25 15:04:16 -07003457 /* tx reclaim */
3458 bcmgenet_tx_reclaim_all(dev);
3459 bcmgenet_fini_dma(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003460}
3461
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003462static int bcmgenet_close(struct net_device *dev)
3463{
3464 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Bergerd215dba2017-10-25 15:04:16 -07003465 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003466
3467 netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
3468
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003469 bcmgenet_netif_stop(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003470
Florian Fainellic96e7312014-11-10 18:06:20 -08003471 /* Really kill the PHY state machine and disconnect from it */
Doug Berger6c97f012017-10-25 15:04:19 -07003472 phy_disconnect(dev->phydev);
Florian Fainellic96e7312014-11-10 18:06:20 -08003473
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003474 free_irq(priv->irq0, priv);
3475 free_irq(priv->irq1, priv);
3476
Florian Fainellic624f892015-07-16 15:51:17 -07003477 if (priv->internal_phy)
Florian Fainellica8cf342015-03-23 15:09:51 -07003478 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003479
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003480 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003481
Florian Fainellica8cf342015-03-23 15:09:51 -07003482 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003483}
3484
Florian Fainelli13ea6572015-06-04 16:15:50 -07003485static void bcmgenet_dump_tx_queue(struct bcmgenet_tx_ring *ring)
3486{
3487 struct bcmgenet_priv *priv = ring->priv;
3488 u32 p_index, c_index, intsts, intmsk;
3489 struct netdev_queue *txq;
3490 unsigned int free_bds;
Florian Fainelli13ea6572015-06-04 16:15:50 -07003491 bool txq_stopped;
3492
3493 if (!netif_msg_tx_err(priv))
3494 return;
3495
3496 txq = netdev_get_tx_queue(priv->dev, ring->queue);
3497
Doug Bergerb0447ec2017-10-25 15:04:17 -07003498 spin_lock(&ring->lock);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003499 if (ring->index == DESC_INDEX) {
3500 intsts = ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
3501 intmsk = UMAC_IRQ_TXDMA_DONE | UMAC_IRQ_TXDMA_MBDONE;
3502 } else {
3503 intsts = ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
3504 intmsk = 1 << ring->index;
3505 }
3506 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
3507 p_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_PROD_INDEX);
3508 txq_stopped = netif_tx_queue_stopped(txq);
3509 free_bds = ring->free_bds;
Doug Bergerb0447ec2017-10-25 15:04:17 -07003510 spin_unlock(&ring->lock);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003511
3512 netif_err(priv, tx_err, priv->dev, "Ring %d queue %d status summary\n"
3513 "TX queue status: %s, interrupts: %s\n"
3514 "(sw)free_bds: %d (sw)size: %d\n"
3515 "(sw)p_index: %d (hw)p_index: %d\n"
3516 "(sw)c_index: %d (hw)c_index: %d\n"
3517 "(sw)clean_p: %d (sw)write_p: %d\n"
3518 "(sw)cb_ptr: %d (sw)end_ptr: %d\n",
3519 ring->index, ring->queue,
3520 txq_stopped ? "stopped" : "active",
3521 intsts & intmsk ? "enabled" : "disabled",
3522 free_bds, ring->size,
3523 ring->prod_index, p_index & DMA_P_INDEX_MASK,
3524 ring->c_index, c_index & DMA_C_INDEX_MASK,
3525 ring->clean_ptr, ring->write_ptr,
3526 ring->cb_ptr, ring->end_ptr);
3527}
3528
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05003529static void bcmgenet_timeout(struct net_device *dev, unsigned int txqueue)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003530{
3531 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003532 u32 int0_enable = 0;
3533 u32 int1_enable = 0;
3534 unsigned int q;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003535
3536 netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
3537
Florian Fainelli13ea6572015-06-04 16:15:50 -07003538 for (q = 0; q < priv->hw_params->tx_queues; q++)
3539 bcmgenet_dump_tx_queue(&priv->tx_rings[q]);
3540 bcmgenet_dump_tx_queue(&priv->tx_rings[DESC_INDEX]);
3541
3542 bcmgenet_tx_reclaim_all(dev);
3543
3544 for (q = 0; q < priv->hw_params->tx_queues; q++)
3545 int1_enable |= (1 << q);
3546
3547 int0_enable = UMAC_IRQ_TXDMA_DONE;
3548
3549 /* Re-enable TX interrupts if disabled */
3550 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
3551 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
3552
Florian Westphal860e9532016-05-03 16:33:13 +02003553 netif_trans_update(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003554
3555 dev->stats.tx_errors++;
3556
3557 netif_tx_wake_all_queues(dev);
3558}
3559
Justin Chen35cbef92019-07-17 14:58:53 -07003560#define MAX_MDF_FILTER 17
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003561
3562static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
Jakub Kicinski76660752021-10-14 07:24:31 -07003563 const unsigned char *addr,
Justin Chen35cbef92019-07-17 14:58:53 -07003564 int *i)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003565{
Florian Fainellic91b7f62014-07-23 10:42:12 -07003566 bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
3567 UMAC_MDF_ADDR + (*i * 4));
3568 bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
3569 addr[4] << 8 | addr[5],
3570 UMAC_MDF_ADDR + ((*i + 1) * 4));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003571 *i += 2;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003572}
3573
3574static void bcmgenet_set_rx_mode(struct net_device *dev)
3575{
3576 struct bcmgenet_priv *priv = netdev_priv(dev);
3577 struct netdev_hw_addr *ha;
Justin Chen35cbef92019-07-17 14:58:53 -07003578 int i, nfilter;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003579 u32 reg;
3580
3581 netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
3582
Justin Chen35cbef92019-07-17 14:58:53 -07003583 /* Number of filters needed */
3584 nfilter = netdev_uc_count(dev) + netdev_mc_count(dev) + 2;
3585
3586 /*
3587 * Turn on promicuous mode for three scenarios
3588 * 1. IFF_PROMISC flag is set
3589 * 2. IFF_ALLMULTI flag is set
3590 * 3. The number of filters needed exceeds the number filters
3591 * supported by the hardware.
3592 */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003593 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
Justin Chen35cbef92019-07-17 14:58:53 -07003594 if ((dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) ||
3595 (nfilter > MAX_MDF_FILTER)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003596 reg |= CMD_PROMISC;
3597 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3598 bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
3599 return;
3600 } else {
3601 reg &= ~CMD_PROMISC;
3602 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3603 }
3604
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003605 /* update MDF filter */
3606 i = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003607 /* Broadcast */
Justin Chen35cbef92019-07-17 14:58:53 -07003608 bcmgenet_set_mdf_addr(priv, dev->broadcast, &i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003609 /* my own address.*/
Justin Chen35cbef92019-07-17 14:58:53 -07003610 bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003611
Justin Chen35cbef92019-07-17 14:58:53 -07003612 /* Unicast */
3613 netdev_for_each_uc_addr(ha, dev)
3614 bcmgenet_set_mdf_addr(priv, ha->addr, &i);
3615
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003616 /* Multicast */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003617 netdev_for_each_mc_addr(ha, dev)
Justin Chen35cbef92019-07-17 14:58:53 -07003618 bcmgenet_set_mdf_addr(priv, ha->addr, &i);
3619
3620 /* Enable filters */
3621 reg = GENMASK(MAX_MDF_FILTER - 1, MAX_MDF_FILTER - nfilter);
3622 bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003623}
3624
3625/* Set the hardware MAC address. */
3626static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
3627{
3628 struct sockaddr *addr = p;
3629
3630 /* Setting the MAC address at the hardware level is not possible
3631 * without disabling the UniMAC RX/TX enable bits.
3632 */
3633 if (netif_running(dev))
3634 return -EBUSY;
3635
Jakub Kicinskif3956eb2021-10-01 14:32:23 -07003636 eth_hw_addr_set(dev, addr->sa_data);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003637
3638 return 0;
3639}
3640
Florian Fainelli37a30b42017-03-16 10:27:08 -07003641static struct net_device_stats *bcmgenet_get_stats(struct net_device *dev)
3642{
3643 struct bcmgenet_priv *priv = netdev_priv(dev);
3644 unsigned long tx_bytes = 0, tx_packets = 0;
3645 unsigned long rx_bytes = 0, rx_packets = 0;
3646 unsigned long rx_errors = 0, rx_dropped = 0;
3647 struct bcmgenet_tx_ring *tx_ring;
3648 struct bcmgenet_rx_ring *rx_ring;
3649 unsigned int q;
3650
3651 for (q = 0; q < priv->hw_params->tx_queues; q++) {
3652 tx_ring = &priv->tx_rings[q];
3653 tx_bytes += tx_ring->bytes;
3654 tx_packets += tx_ring->packets;
3655 }
3656 tx_ring = &priv->tx_rings[DESC_INDEX];
3657 tx_bytes += tx_ring->bytes;
3658 tx_packets += tx_ring->packets;
3659
3660 for (q = 0; q < priv->hw_params->rx_queues; q++) {
3661 rx_ring = &priv->rx_rings[q];
3662
3663 rx_bytes += rx_ring->bytes;
3664 rx_packets += rx_ring->packets;
3665 rx_errors += rx_ring->errors;
3666 rx_dropped += rx_ring->dropped;
3667 }
3668 rx_ring = &priv->rx_rings[DESC_INDEX];
3669 rx_bytes += rx_ring->bytes;
3670 rx_packets += rx_ring->packets;
3671 rx_errors += rx_ring->errors;
3672 rx_dropped += rx_ring->dropped;
3673
3674 dev->stats.tx_bytes = tx_bytes;
3675 dev->stats.tx_packets = tx_packets;
3676 dev->stats.rx_bytes = rx_bytes;
3677 dev->stats.rx_packets = rx_packets;
3678 dev->stats.rx_errors = rx_errors;
3679 dev->stats.rx_missed_errors = rx_errors;
Doug Bergera6d0b832020-04-23 15:44:17 -07003680 dev->stats.rx_dropped = rx_dropped;
Florian Fainelli37a30b42017-03-16 10:27:08 -07003681 return &dev->stats;
3682}
3683
Florian Fainelli47ff61542020-07-02 21:57:00 -07003684static int bcmgenet_change_carrier(struct net_device *dev, bool new_carrier)
3685{
3686 struct bcmgenet_priv *priv = netdev_priv(dev);
3687
3688 if (!dev->phydev || !phy_is_pseudo_fixed_link(dev->phydev) ||
3689 priv->phy_interface != PHY_INTERFACE_MODE_MOCA)
3690 return -EOPNOTSUPP;
3691
3692 if (new_carrier)
3693 netif_carrier_on(dev);
3694 else
3695 netif_carrier_off(dev);
3696
3697 return 0;
3698}
3699
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003700static const struct net_device_ops bcmgenet_netdev_ops = {
3701 .ndo_open = bcmgenet_open,
3702 .ndo_stop = bcmgenet_close,
3703 .ndo_start_xmit = bcmgenet_xmit,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003704 .ndo_tx_timeout = bcmgenet_timeout,
3705 .ndo_set_rx_mode = bcmgenet_set_rx_mode,
3706 .ndo_set_mac_address = bcmgenet_set_mac_addr,
Arnd Bergmanna7605372021-07-27 15:45:13 +02003707 .ndo_eth_ioctl = phy_do_ioctl_running,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003708 .ndo_set_features = bcmgenet_set_features,
Florian Fainelli4d2e8882015-07-31 11:42:54 -07003709#ifdef CONFIG_NET_POLL_CONTROLLER
3710 .ndo_poll_controller = bcmgenet_poll_controller,
3711#endif
Florian Fainelli37a30b42017-03-16 10:27:08 -07003712 .ndo_get_stats = bcmgenet_get_stats,
Florian Fainelli47ff61542020-07-02 21:57:00 -07003713 .ndo_change_carrier = bcmgenet_change_carrier,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003714};
3715
3716/* Array of GENET hardware parameters/characteristics */
3717static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
3718 [GENET_V1] = {
3719 .tx_queues = 0,
Petri Gynther51a966a2015-02-23 11:00:46 -08003720 .tx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003721 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003722 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003723 .bp_in_en_shift = 16,
3724 .bp_in_mask = 0xffff,
3725 .hfb_filter_cnt = 16,
3726 .qtag_mask = 0x1F,
3727 .hfb_offset = 0x1000,
3728 .rdma_offset = 0x2000,
3729 .tdma_offset = 0x3000,
3730 .words_per_bd = 2,
3731 },
3732 [GENET_V2] = {
3733 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003734 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003735 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003736 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003737 .bp_in_en_shift = 16,
3738 .bp_in_mask = 0xffff,
3739 .hfb_filter_cnt = 16,
3740 .qtag_mask = 0x1F,
3741 .tbuf_offset = 0x0600,
3742 .hfb_offset = 0x1000,
3743 .hfb_reg_offset = 0x2000,
3744 .rdma_offset = 0x3000,
3745 .tdma_offset = 0x4000,
3746 .words_per_bd = 2,
3747 .flags = GENET_HAS_EXT,
3748 },
3749 [GENET_V3] = {
3750 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003751 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003752 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003753 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003754 .bp_in_en_shift = 17,
3755 .bp_in_mask = 0x1ffff,
3756 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003757 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003758 .qtag_mask = 0x3F,
3759 .tbuf_offset = 0x0600,
3760 .hfb_offset = 0x8000,
3761 .hfb_reg_offset = 0xfc00,
3762 .rdma_offset = 0x10000,
3763 .tdma_offset = 0x11000,
3764 .words_per_bd = 2,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003765 .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR |
3766 GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003767 },
3768 [GENET_V4] = {
3769 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003770 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003771 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003772 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003773 .bp_in_en_shift = 17,
3774 .bp_in_mask = 0x1ffff,
3775 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003776 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003777 .qtag_mask = 0x3F,
3778 .tbuf_offset = 0x0600,
3779 .hfb_offset = 0x8000,
3780 .hfb_reg_offset = 0xfc00,
3781 .rdma_offset = 0x2000,
3782 .tdma_offset = 0x4000,
3783 .words_per_bd = 3,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003784 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3785 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003786 },
Doug Berger42138082017-03-13 17:41:42 -07003787 [GENET_V5] = {
3788 .tx_queues = 4,
3789 .tx_bds_per_q = 32,
3790 .rx_queues = 0,
3791 .rx_bds_per_q = 0,
3792 .bp_in_en_shift = 17,
3793 .bp_in_mask = 0x1ffff,
3794 .hfb_filter_cnt = 48,
3795 .hfb_filter_size = 128,
3796 .qtag_mask = 0x3F,
3797 .tbuf_offset = 0x0600,
3798 .hfb_offset = 0x8000,
3799 .hfb_reg_offset = 0xfc00,
3800 .rdma_offset = 0x2000,
3801 .tdma_offset = 0x4000,
3802 .words_per_bd = 3,
3803 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3804 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
3805 },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003806};
3807
3808/* Infer hardware parameters from the detected GENET version */
3809static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
3810{
3811 struct bcmgenet_hw_params *params;
3812 u32 reg;
3813 u8 major;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003814 u16 gphy_rev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003815
Doug Berger42138082017-03-13 17:41:42 -07003816 if (GENET_IS_V5(priv) || GENET_IS_V4(priv)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003817 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3818 genet_dma_ring_regs = genet_dma_ring_regs_v4;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003819 } else if (GENET_IS_V3(priv)) {
3820 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3821 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003822 } else if (GENET_IS_V2(priv)) {
3823 bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
3824 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003825 } else if (GENET_IS_V1(priv)) {
3826 bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
3827 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003828 }
3829
3830 /* enum genet_version starts at 1 */
3831 priv->hw_params = &bcmgenet_hw_params[priv->version];
3832 params = priv->hw_params;
3833
3834 /* Read GENET HW version */
3835 reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
3836 major = (reg >> 24 & 0x0f);
Doug Berger42138082017-03-13 17:41:42 -07003837 if (major == 6)
3838 major = 5;
3839 else if (major == 5)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003840 major = 4;
3841 else if (major == 0)
3842 major = 1;
3843 if (major != priv->version) {
3844 dev_err(&priv->pdev->dev,
3845 "GENET version mismatch, got: %d, configured for: %d\n",
3846 major, priv->version);
3847 }
3848
3849 /* Print the GENET core version */
3850 dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003851 major, (reg >> 16) & 0x0f, reg & 0xffff);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003852
Florian Fainelli487320c2014-09-19 13:07:53 -07003853 /* Store the integrated PHY revision for the MDIO probing function
3854 * to pass this information to the PHY driver. The PHY driver expects
3855 * to find the PHY major revision in bits 15:8 while the GENET register
3856 * stores that information in bits 7:0, account for that.
Florian Fainellib04a2f52014-12-03 09:56:59 -08003857 *
3858 * On newer chips, starting with PHY revision G0, a new scheme is
3859 * deployed similar to the Starfighter 2 switch with GPHY major
3860 * revision in bits 15:8 and patch level in bits 7:0. Major revision 0
3861 * is reserved as well as special value 0x01ff, we have a small
3862 * heuristic to check for the new GPHY revision and re-arrange things
3863 * so the GPHY driver is happy.
Florian Fainelli487320c2014-09-19 13:07:53 -07003864 */
Florian Fainellib04a2f52014-12-03 09:56:59 -08003865 gphy_rev = reg & 0xffff;
3866
Doug Berger42138082017-03-13 17:41:42 -07003867 if (GENET_IS_V5(priv)) {
3868 /* The EPHY revision should come from the MDIO registers of
3869 * the PHY not from GENET.
3870 */
3871 if (gphy_rev != 0) {
3872 pr_warn("GENET is reporting EPHY revision: 0x%04x\n",
3873 gphy_rev);
3874 }
Doug Bergereca4bad2017-03-09 16:58:45 -08003875 /* This is reserved so should require special treatment */
David S. Miller101c4312017-03-15 11:59:10 -07003876 } else if (gphy_rev == 0 || gphy_rev == 0x01ff) {
Doug Bergereca4bad2017-03-09 16:58:45 -08003877 pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev);
3878 return;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003879 /* This is the good old scheme, just GPHY major, no minor nor patch */
Doug Berger42138082017-03-13 17:41:42 -07003880 } else if ((gphy_rev & 0xf0) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003881 priv->gphy_rev = gphy_rev << 8;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003882 /* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */
Doug Berger42138082017-03-13 17:41:42 -07003883 } else if ((gphy_rev & 0xff00) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003884 priv->gphy_rev = gphy_rev;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003885 }
Florian Fainelli487320c2014-09-19 13:07:53 -07003886
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003887#ifdef CONFIG_PHYS_ADDR_T_64BIT
3888 if (!(params->flags & GENET_HAS_40BITS))
3889 pr_warn("GENET does not support 40-bits PA\n");
3890#endif
3891
3892 pr_debug("Configuration for version: %d\n"
Petri Gynther3feafa02015-03-05 17:40:14 -08003893 "TXq: %1d, TXqBDs: %1d, RXq: %1d, RXqBDs: %1d\n"
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003894 "BP << en: %2d, BP msk: 0x%05x\n"
3895 "HFB count: %2d, QTAQ msk: 0x%05x\n"
3896 "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
3897 "RDMA: 0x%05x, TDMA: 0x%05x\n"
3898 "Words/BD: %d\n",
3899 priv->version,
Petri Gynther51a966a2015-02-23 11:00:46 -08003900 params->tx_queues, params->tx_bds_per_q,
Petri Gynther3feafa02015-03-05 17:40:14 -08003901 params->rx_queues, params->rx_bds_per_q,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003902 params->bp_in_en_shift, params->bp_in_mask,
3903 params->hfb_filter_cnt, params->qtag_mask,
3904 params->tbuf_offset, params->hfb_offset,
3905 params->hfb_reg_offset,
3906 params->rdma_offset, params->tdma_offset,
3907 params->words_per_bd);
3908}
3909
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003910struct bcmgenet_plat_data {
3911 enum bcmgenet_version version;
3912 u32 dma_max_burst_length;
Florian Fainelli3cd92ea2021-10-22 09:17:03 -07003913 bool ephy_16nm;
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003914};
3915
3916static const struct bcmgenet_plat_data v1_plat_data = {
3917 .version = GENET_V1,
3918 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3919};
3920
3921static const struct bcmgenet_plat_data v2_plat_data = {
3922 .version = GENET_V2,
3923 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3924};
3925
3926static const struct bcmgenet_plat_data v3_plat_data = {
3927 .version = GENET_V3,
3928 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3929};
3930
3931static const struct bcmgenet_plat_data v4_plat_data = {
3932 .version = GENET_V4,
3933 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3934};
3935
3936static const struct bcmgenet_plat_data v5_plat_data = {
3937 .version = GENET_V5,
3938 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3939};
3940
3941static const struct bcmgenet_plat_data bcm2711_plat_data = {
3942 .version = GENET_V5,
3943 .dma_max_burst_length = 0x08,
3944};
3945
Florian Fainelli3cd92ea2021-10-22 09:17:03 -07003946static const struct bcmgenet_plat_data bcm7712_plat_data = {
3947 .version = GENET_V5,
3948 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3949 .ephy_16nm = true,
3950};
3951
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003952static const struct of_device_id bcmgenet_match[] = {
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003953 { .compatible = "brcm,genet-v1", .data = &v1_plat_data },
3954 { .compatible = "brcm,genet-v2", .data = &v2_plat_data },
3955 { .compatible = "brcm,genet-v3", .data = &v3_plat_data },
3956 { .compatible = "brcm,genet-v4", .data = &v4_plat_data },
3957 { .compatible = "brcm,genet-v5", .data = &v5_plat_data },
3958 { .compatible = "brcm,bcm2711-genet-v5", .data = &bcm2711_plat_data },
Florian Fainelli3cd92ea2021-10-22 09:17:03 -07003959 { .compatible = "brcm,bcm7712-genet-v5", .data = &bcm7712_plat_data },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003960 { },
3961};
Luis de Bethencourte8048e52015-09-18 17:55:02 +02003962MODULE_DEVICE_TABLE(of, bcmgenet_match);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003963
3964static int bcmgenet_probe(struct platform_device *pdev)
3965{
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003966 struct bcmgenet_platform_data *pd = pdev->dev.platform_data;
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003967 const struct bcmgenet_plat_data *pdata;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003968 struct bcmgenet_priv *priv;
3969 struct net_device *dev;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07003970 unsigned int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003971 int err = -EIO;
3972
Petri Gynther3feafee2015-03-05 17:40:12 -08003973 /* Up to GENET_MAX_MQ_CNT + 1 TX queues and RX queues */
3974 dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1,
3975 GENET_MAX_MQ_CNT + 1);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003976 if (!dev) {
3977 dev_err(&pdev->dev, "can't allocate net device\n");
3978 return -ENOMEM;
3979 }
3980
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003981 priv = netdev_priv(dev);
3982 priv->irq0 = platform_get_irq(pdev, 0);
Stefan Wahren2b65f932019-11-11 20:49:21 +01003983 if (priv->irq0 < 0) {
3984 err = priv->irq0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003985 goto err;
3986 }
Stefan Wahren2b65f932019-11-11 20:49:21 +01003987 priv->irq1 = platform_get_irq(pdev, 1);
3988 if (priv->irq1 < 0) {
3989 err = priv->irq1;
3990 goto err;
3991 }
3992 priv->wol_irq = platform_get_irq_optional(pdev, 2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003993
YueHaibing4ca33482019-08-21 21:41:31 +08003994 priv->base = devm_platform_ioremap_resource(pdev, 0);
Fabio Estevam5343a102014-02-24 00:47:24 -03003995 if (IS_ERR(priv->base)) {
3996 err = PTR_ERR(priv->base);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003997 goto err;
3998 }
3999
Doug Berger07c52d62017-03-09 16:58:47 -08004000 spin_lock_init(&priv->lock);
4001
Doug Berger2d8bdf522021-09-25 20:21:14 -07004002 /* Set default pause parameters */
4003 priv->autoneg_pause = 1;
4004 priv->tx_pause = 1;
4005 priv->rx_pause = 1;
4006
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004007 SET_NETDEV_DEV(dev, &pdev->dev);
4008 dev_set_drvdata(&pdev->dev, dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004009 dev->watchdog_timeo = 2 * HZ;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00004010 dev->ethtool_ops = &bcmgenet_ethtool_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004011 dev->netdev_ops = &bcmgenet_netdev_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004012
4013 priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
4014
Doug Bergerae895c42019-12-17 16:51:13 -08004015 /* Set default features */
4016 dev->features |= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
4017 NETIF_F_RXCSUM;
4018 dev->hw_features |= dev->features;
4019 dev->vlan_features |= dev->features;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004020
Florian Fainelli85620562014-07-21 15:29:23 -07004021 /* Request the WOL interrupt and advertise suspend if available */
4022 priv->wol_irq_disabled = true;
Sergey Shtylyov9deb48b2022-01-13 22:46:07 +03004023 if (priv->wol_irq > 0) {
4024 err = devm_request_irq(&pdev->dev, priv->wol_irq,
4025 bcmgenet_wol_isr, 0, dev->name, priv);
4026 if (!err)
4027 device_set_wakeup_capable(&pdev->dev, 1);
4028 }
Florian Fainelli85620562014-07-21 15:29:23 -07004029
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004030 /* Set the needed headroom to account for any possible
4031 * features enabling/disabling at runtime
4032 */
4033 dev->needed_headroom += 64;
4034
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004035 priv->dev = dev;
4036 priv->pdev = pdev;
Jeremy Linton99c6b062020-02-24 16:54:01 -06004037
4038 pdata = device_get_match_data(&pdev->dev);
4039 if (pdata) {
Stefan Wahrena50e3a92019-11-11 20:49:23 +01004040 priv->version = pdata->version;
4041 priv->dma_max_burst_length = pdata->dma_max_burst_length;
Florian Fainelli3cd92ea2021-10-22 09:17:03 -07004042 priv->ephy_16nm = pdata->ephy_16nm;
Stefan Wahrena50e3a92019-11-11 20:49:23 +01004043 } else {
Petri Gyntherb0ba5122014-12-01 16:18:08 -08004044 priv->version = pd->genet_version;
Stefan Wahrena50e3a92019-11-11 20:49:23 +01004045 priv->dma_max_burst_length = DMA_MAX_BURST_LENGTH;
4046 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004047
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03004048 priv->clk = devm_clk_get_optional(&priv->pdev->dev, "enet");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07004049 if (IS_ERR(priv->clk)) {
Jeremy Lintonae200c22020-02-24 16:54:03 -06004050 dev_dbg(&priv->pdev->dev, "failed to get enet clock\n");
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03004051 err = PTR_ERR(priv->clk);
4052 goto err;
Florian Fainelli7d5d3072015-07-22 17:28:23 -07004053 }
Florian Fainellie4a60a92014-08-11 14:50:42 -07004054
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03004055 err = clk_prepare_enable(priv->clk);
4056 if (err)
4057 goto err;
Florian Fainellie4a60a92014-08-11 14:50:42 -07004058
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004059 bcmgenet_set_hw_params(priv);
4060
Doug Berger99d55632019-12-17 16:51:08 -08004061 err = -EIO;
4062 if (priv->hw_params->flags & GENET_HAS_40BITS)
4063 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
4064 if (err)
4065 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
4066 if (err)
Zhang Changzhong24a63fe2020-07-20 15:18:43 +08004067 goto err_clk_disable;
Doug Berger99d55632019-12-17 16:51:08 -08004068
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004069 /* Mii wait queue */
4070 init_waitqueue_head(&priv->wq);
4071 /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
4072 priv->rx_buf_len = RX_BUF_LENGTH;
4073 INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
4074
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03004075 priv->clk_wol = devm_clk_get_optional(&priv->pdev->dev, "enet-wol");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07004076 if (IS_ERR(priv->clk_wol)) {
Jeremy Lintonae200c22020-02-24 16:54:03 -06004077 dev_dbg(&priv->pdev->dev, "failed to get enet-wol clock\n");
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03004078 err = PTR_ERR(priv->clk_wol);
Zhang Changzhong53a92882020-07-20 17:36:34 +08004079 goto err_clk_disable;
Florian Fainelli7d5d3072015-07-22 17:28:23 -07004080 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004081
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03004082 priv->clk_eee = devm_clk_get_optional(&priv->pdev->dev, "enet-eee");
Florian Fainelli6ef398e2014-11-25 21:16:35 -08004083 if (IS_ERR(priv->clk_eee)) {
Jeremy Lintonae200c22020-02-24 16:54:03 -06004084 dev_dbg(&priv->pdev->dev, "failed to get enet-eee clock\n");
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03004085 err = PTR_ERR(priv->clk_eee);
Zhang Changzhong53a92882020-07-20 17:36:34 +08004086 goto err_clk_disable;
Florian Fainelli6ef398e2014-11-25 21:16:35 -08004087 }
4088
Doug Berger6be371b2017-03-09 16:58:48 -08004089 /* If this is an internal GPHY, power it on now, before UniMAC is
4090 * brought out of reset as absolutely no UniMAC activity is allowed
4091 */
Jeremy Linton99c6b062020-02-24 16:54:01 -06004092 if (device_get_phy_mode(&pdev->dev) == PHY_INTERFACE_MODE_INTERNAL)
Doug Berger6be371b2017-03-09 16:58:48 -08004093 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
4094
Andy Shevchenko7d3cca72020-04-21 00:51:21 +03004095 if (pd && !IS_ERR_OR_NULL(pd->mac_address))
Jakub Kicinskif3956eb2021-10-01 14:32:23 -07004096 eth_hw_addr_set(dev, pd->mac_address);
Jeremy Linton26bd9cc2020-02-24 16:54:02 -06004097 else
Jakub Kicinskib8eeac52021-10-06 18:07:01 -07004098 if (device_get_ethdev_address(&pdev->dev, dev))
Jakub Kicinski0c9e0c72021-10-15 15:16:47 -07004099 if (has_acpi_companion(&pdev->dev)) {
4100 u8 addr[ETH_ALEN];
4101
4102 bcmgenet_get_hw_addr(priv, addr);
4103 eth_hw_addr_set(dev, addr);
4104 }
Jeremy Linton26bd9cc2020-02-24 16:54:02 -06004105
4106 if (!is_valid_ether_addr(dev->dev_addr)) {
4107 dev_warn(&pdev->dev, "using random Ethernet MAC\n");
4108 eth_hw_addr_random(dev);
4109 }
4110
Doug Berger28c2d1a2017-10-25 15:04:13 -07004111 reset_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004112
4113 err = bcmgenet_mii_init(dev);
4114 if (err)
4115 goto err_clk_disable;
4116
4117 /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
4118 * just the ring 16 descriptor based TX
4119 */
4120 netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
4121 netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
4122
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07004123 /* Set default coalescing parameters */
4124 for (i = 0; i < priv->hw_params->rx_queues; i++)
4125 priv->rx_rings[i].rx_max_coalesced_frames = 1;
4126 priv->rx_rings[DESC_INDEX].rx_max_coalesced_frames = 1;
4127
Florian Fainelli219575e2014-06-26 10:26:21 -07004128 /* libphy will determine the link state */
4129 netif_carrier_off(dev);
4130
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004131 /* Turn off the main clock, WOL clock is handled separately */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07004132 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004133
Florian Fainelli0f50ce92014-06-26 10:26:20 -07004134 err = register_netdev(dev);
Christophe JAILLET4375ada2020-12-12 19:20:05 +01004135 if (err) {
4136 bcmgenet_mii_exit(dev);
Florian Fainelli0f50ce92014-06-26 10:26:20 -07004137 goto err;
Christophe JAILLET4375ada2020-12-12 19:20:05 +01004138 }
Florian Fainelli0f50ce92014-06-26 10:26:20 -07004139
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004140 return err;
4141
4142err_clk_disable:
Florian Fainelli7d5d3072015-07-22 17:28:23 -07004143 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004144err:
4145 free_netdev(dev);
4146 return err;
4147}
4148
4149static int bcmgenet_remove(struct platform_device *pdev)
4150{
4151 struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
4152
4153 dev_set_drvdata(&pdev->dev, NULL);
4154 unregister_netdev(priv->dev);
4155 bcmgenet_mii_exit(priv->dev);
4156 free_netdev(priv->dev);
4157
4158 return 0;
4159}
4160
Florian Fainellid9f45ab2019-10-15 10:36:24 -07004161static void bcmgenet_shutdown(struct platform_device *pdev)
4162{
4163 bcmgenet_remove(pdev);
4164}
4165
Florian Fainellib6e978e2014-07-21 15:29:22 -07004166#ifdef CONFIG_PM_SLEEP
Doug Bergereb236c22020-04-30 16:26:51 -07004167static int bcmgenet_resume_noirq(struct device *d)
4168{
4169 struct net_device *dev = dev_get_drvdata(d);
4170 struct bcmgenet_priv *priv = netdev_priv(dev);
4171 int ret;
4172 u32 reg;
4173
4174 if (!netif_running(dev))
4175 return 0;
4176
4177 /* Turn on the clock */
4178 ret = clk_prepare_enable(priv->clk);
4179 if (ret)
4180 return ret;
4181
4182 if (device_may_wakeup(d) && priv->wolopts) {
4183 /* Account for Wake-on-LAN events and clear those events
4184 * (Some devices need more time between enabling the clocks
4185 * and the interrupt register reflecting the wake event so
4186 * read the register twice)
4187 */
4188 reg = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT);
4189 reg = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT);
4190 if (reg & UMAC_IRQ_WAKE_EVENT)
4191 pm_wakeup_event(&priv->pdev->dev, 0);
4192 }
4193
4194 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_WAKE_EVENT, INTRL2_CPU_CLEAR);
4195
4196 return 0;
4197}
4198
Florian Fainellib6e978e2014-07-21 15:29:22 -07004199static int bcmgenet_resume(struct device *d)
4200{
4201 struct net_device *dev = dev_get_drvdata(d);
4202 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Bergera8c64542020-07-16 16:38:17 -07004203 struct bcmgenet_rxnfc_rule *rule;
Florian Fainellib6e978e2014-07-21 15:29:22 -07004204 unsigned long dma_ctrl;
4205 int ret;
Florian Fainellib6e978e2014-07-21 15:29:22 -07004206
4207 if (!netif_running(dev))
4208 return 0;
4209
Doug Berger1a1d5102020-04-29 13:02:02 -07004210 /* From WOL-enabled suspend, switch to regular clock */
4211 if (device_may_wakeup(d) && priv->wolopts)
4212 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
4213
Florian Fainellia6f31f52015-03-23 15:09:57 -07004214 /* If this is an internal GPHY, power it back on now, before UniMAC is
4215 * brought out of reset as absolutely no UniMAC activity is allowed
4216 */
Florian Fainellic624f892015-07-16 15:51:17 -07004217 if (priv->internal_phy)
Florian Fainellia6f31f52015-03-23 15:09:57 -07004218 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
4219
Florian Fainellib6e978e2014-07-21 15:29:22 -07004220 bcmgenet_umac_reset(priv);
4221
Doug Berger28c2d1a2017-10-25 15:04:13 -07004222 init_umac(priv);
Florian Fainellib6e978e2014-07-21 15:29:22 -07004223
Doug Berger6b6d017f2019-11-05 11:07:25 -08004224 phy_init_hw(dev->phydev);
4225
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02004226 /* Speed settings must be restored */
Doug Berger0686bd92019-11-05 11:07:26 -08004227 genphy_config_aneg(dev->phydev);
Florian Fainelli00d51092017-07-31 11:05:32 -07004228 bcmgenet_mii_config(priv->dev, false);
Florian Fainelli8c90db72014-07-21 15:29:28 -07004229
Doug Berger206f54b2019-12-17 16:51:12 -08004230 /* Restore enabled features */
4231 bcmgenet_set_features(dev, dev->features);
4232
Florian Fainellib6e978e2014-07-21 15:29:22 -07004233 bcmgenet_set_hw_addr(priv, dev->dev_addr);
4234
Doug Bergera8c64542020-07-16 16:38:17 -07004235 /* Restore hardware filters */
4236 bcmgenet_hfb_clear(priv);
4237 list_for_each_entry(rule, &priv->rxnfc_list, list)
4238 if (rule->state != BCMGENET_RXNFC_STATE_UNUSED)
4239 bcmgenet_hfb_create_rxnfc_filter(priv, rule);
Doug Berger3e370952020-04-29 13:02:05 -07004240
Florian Fainellib6e978e2014-07-21 15:29:22 -07004241 /* Disable RX/TX DMA and flush TX queues */
4242 dma_ctrl = bcmgenet_dma_disable(priv);
4243
4244 /* Reinitialize TDMA and RDMA and SW housekeeping */
4245 ret = bcmgenet_init_dma(priv);
4246 if (ret) {
4247 netdev_err(dev, "failed to initialize DMA\n");
4248 goto out_clk_disable;
4249 }
4250
4251 /* Always enable ring 16 - descriptor ring */
4252 bcmgenet_enable_dma(priv, dma_ctrl);
4253
Florian Fainelli5371bbf42017-03-15 12:57:21 -07004254 if (!device_may_wakeup(d))
Doug Berger6c97f012017-10-25 15:04:19 -07004255 phy_resume(dev->phydev);
Florian Fainellicc013fb2014-08-11 14:50:43 -07004256
Florian Fainelli6ef398e2014-11-25 21:16:35 -08004257 if (priv->eee.eee_enabled)
4258 bcmgenet_eee_enable_set(dev, true);
4259
Florian Fainellib6e978e2014-07-21 15:29:22 -07004260 bcmgenet_netif_start(dev);
4261
Doug Berger09e805d2018-11-01 15:55:37 -07004262 netif_device_attach(dev);
4263
Florian Fainellib6e978e2014-07-21 15:29:22 -07004264 return 0;
4265
4266out_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08004267 if (priv->internal_phy)
4268 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainellib6e978e2014-07-21 15:29:22 -07004269 clk_disable_unprepare(priv->clk);
4270 return ret;
4271}
Doug Bergera94cbf02018-11-16 18:00:21 -08004272
4273static int bcmgenet_suspend(struct device *d)
4274{
4275 struct net_device *dev = dev_get_drvdata(d);
4276 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Bergera94cbf02018-11-16 18:00:21 -08004277
4278 if (!netif_running(dev))
4279 return 0;
4280
4281 netif_device_detach(dev);
4282
4283 bcmgenet_netif_stop(dev);
4284
4285 if (!device_may_wakeup(d))
4286 phy_suspend(dev->phydev);
4287
Doug Bergera8c64542020-07-16 16:38:17 -07004288 /* Disable filtering */
Doug Berger3e370952020-04-29 13:02:05 -07004289 bcmgenet_hfb_reg_writel(priv, 0, HFB_CTRL);
4290
Doug Bergereb236c22020-04-30 16:26:51 -07004291 return 0;
4292}
4293
4294static int bcmgenet_suspend_noirq(struct device *d)
4295{
4296 struct net_device *dev = dev_get_drvdata(d);
4297 struct bcmgenet_priv *priv = netdev_priv(dev);
4298 int ret = 0;
4299
4300 if (!netif_running(dev))
4301 return 0;
4302
Doug Bergera94cbf02018-11-16 18:00:21 -08004303 /* Prepare the device for Wake-on-LAN and switch to the slow clock */
Doug Berger1a1d5102020-04-29 13:02:02 -07004304 if (device_may_wakeup(d) && priv->wolopts)
Doug Bergera94cbf02018-11-16 18:00:21 -08004305 ret = bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
Doug Berger1a1d5102020-04-29 13:02:02 -07004306 else if (priv->internal_phy)
Doug Bergera94cbf02018-11-16 18:00:21 -08004307 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Doug Bergera94cbf02018-11-16 18:00:21 -08004308
Doug Bergereb236c22020-04-30 16:26:51 -07004309 /* Let the framework handle resumption and leave the clocks on */
4310 if (ret)
4311 return ret;
4312
Doug Bergera94cbf02018-11-16 18:00:21 -08004313 /* Turn off the clocks */
4314 clk_disable_unprepare(priv->clk);
4315
Doug Bergereb236c22020-04-30 16:26:51 -07004316 return 0;
Doug Bergera94cbf02018-11-16 18:00:21 -08004317}
Doug Bergereb236c22020-04-30 16:26:51 -07004318#else
4319#define bcmgenet_suspend NULL
4320#define bcmgenet_suspend_noirq NULL
4321#define bcmgenet_resume NULL
4322#define bcmgenet_resume_noirq NULL
Florian Fainellib6e978e2014-07-21 15:29:22 -07004323#endif /* CONFIG_PM_SLEEP */
4324
Doug Bergereb236c22020-04-30 16:26:51 -07004325static const struct dev_pm_ops bcmgenet_pm_ops = {
4326 .suspend = bcmgenet_suspend,
4327 .suspend_noirq = bcmgenet_suspend_noirq,
4328 .resume = bcmgenet_resume,
4329 .resume_noirq = bcmgenet_resume_noirq,
4330};
Florian Fainellib6e978e2014-07-21 15:29:22 -07004331
Jeremy Linton99c6b062020-02-24 16:54:01 -06004332static const struct acpi_device_id genet_acpi_match[] = {
4333 { "BCM6E4E", (kernel_ulong_t)&bcm2711_plat_data },
4334 { },
4335};
4336MODULE_DEVICE_TABLE(acpi, genet_acpi_match);
4337
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004338static struct platform_driver bcmgenet_driver = {
4339 .probe = bcmgenet_probe,
4340 .remove = bcmgenet_remove,
Florian Fainellid9f45ab2019-10-15 10:36:24 -07004341 .shutdown = bcmgenet_shutdown,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004342 .driver = {
4343 .name = "bcmgenet",
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004344 .of_match_table = bcmgenet_match,
Florian Fainellib6e978e2014-07-21 15:29:22 -07004345 .pm = &bcmgenet_pm_ops,
Andy Shevchenkod4d9b472020-04-21 00:51:17 +03004346 .acpi_match_table = genet_acpi_match,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004347 },
4348};
4349module_platform_driver(bcmgenet_driver);
4350
4351MODULE_AUTHOR("Broadcom Corporation");
4352MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
4353MODULE_ALIAS("platform:bcmgenet");
4354MODULE_LICENSE("GPL");
Jian-Hong Pan19938ba2021-06-24 11:22:41 +08004355MODULE_SOFTDEP("pre: mdio-bcm-unimac");