blob: ee84a26bd8f38077026f6701355807028df86f44 [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
546static int bcmgenet_hfb_insert_data(u32 *f, int offset,
547 void *val, void *mask, size_t size)
548{
549 int index;
550 u32 tmp;
551
552 index = offset / 2;
553 tmp = f[index];
554
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 }
570 f[index++] = tmp;
571 if (size)
572 tmp = f[index];
573 } else {
574 tmp &= ~0xCFF00;
575 tmp |= (*(unsigned char *)val++) << 8;
576 switch ((*(unsigned char *)mask++)) {
577 case 0xFF:
578 tmp |= 0xC0000;
579 break;
580 case 0xF0:
581 tmp |= 0x80000;
582 break;
583 case 0x0F:
584 tmp |= 0x40000;
585 break;
586 }
587 if (!size)
588 f[index] = tmp;
589 }
590 }
591
592 return 0;
593}
594
595static void bcmgenet_hfb_set_filter(struct bcmgenet_priv *priv, u32 *f_data,
596 u32 f_length, u32 rx_queue, int f_index)
597{
598 u32 base = f_index * priv->hw_params->hfb_filter_size;
599 int i;
600
601 for (i = 0; i < f_length; i++)
602 bcmgenet_hfb_writel(priv, f_data[i], (base + i) * sizeof(u32));
603
604 bcmgenet_hfb_set_filter_length(priv, f_index, 2 * f_length);
605 bcmgenet_hfb_set_filter_rx_queue_mapping(priv, f_index, rx_queue);
606}
607
608static int bcmgenet_hfb_create_rxnfc_filter(struct bcmgenet_priv *priv,
609 struct bcmgenet_rxnfc_rule *rule)
610{
611 struct ethtool_rx_flow_spec *fs = &rule->fs;
612 int err = 0, offset = 0, f_length = 0;
Doug Berger3e370952020-04-29 13:02:05 -0700613 u8 val_8, mask_8;
Doug Bergerd966d2e2020-06-24 18:14:54 -0700614 __be16 val_16;
615 u16 mask_16;
Doug Berger3e370952020-04-29 13:02:05 -0700616 size_t size;
617 u32 *f_data;
618
619 f_data = kcalloc(priv->hw_params->hfb_filter_size, sizeof(u32),
620 GFP_KERNEL);
621 if (!f_data)
622 return -ENOMEM;
623
624 if (fs->flow_type & FLOW_MAC_EXT) {
625 bcmgenet_hfb_insert_data(f_data, 0,
626 &fs->h_ext.h_dest, &fs->m_ext.h_dest,
627 sizeof(fs->h_ext.h_dest));
628 }
629
630 if (fs->flow_type & FLOW_EXT) {
631 if (fs->m_ext.vlan_etype ||
632 fs->m_ext.vlan_tci) {
633 bcmgenet_hfb_insert_data(f_data, 12,
634 &fs->h_ext.vlan_etype,
635 &fs->m_ext.vlan_etype,
636 sizeof(fs->h_ext.vlan_etype));
637 bcmgenet_hfb_insert_data(f_data, 14,
638 &fs->h_ext.vlan_tci,
639 &fs->m_ext.vlan_tci,
640 sizeof(fs->h_ext.vlan_tci));
641 offset += VLAN_HLEN;
642 f_length += DIV_ROUND_UP(VLAN_HLEN, 2);
643 }
644 }
645
646 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
647 case ETHER_FLOW:
648 f_length += DIV_ROUND_UP(ETH_HLEN, 2);
649 bcmgenet_hfb_insert_data(f_data, 0,
650 &fs->h_u.ether_spec.h_dest,
651 &fs->m_u.ether_spec.h_dest,
652 sizeof(fs->h_u.ether_spec.h_dest));
653 bcmgenet_hfb_insert_data(f_data, ETH_ALEN,
654 &fs->h_u.ether_spec.h_source,
655 &fs->m_u.ether_spec.h_source,
656 sizeof(fs->h_u.ether_spec.h_source));
657 bcmgenet_hfb_insert_data(f_data, (2 * ETH_ALEN) + offset,
658 &fs->h_u.ether_spec.h_proto,
659 &fs->m_u.ether_spec.h_proto,
660 sizeof(fs->h_u.ether_spec.h_proto));
661 break;
662 case IP_USER_FLOW:
663 f_length += DIV_ROUND_UP(ETH_HLEN + 20, 2);
664 /* Specify IP Ether Type */
665 val_16 = htons(ETH_P_IP);
666 mask_16 = 0xFFFF;
667 bcmgenet_hfb_insert_data(f_data, (2 * ETH_ALEN) + offset,
668 &val_16, &mask_16, sizeof(val_16));
669 bcmgenet_hfb_insert_data(f_data, 15 + offset,
670 &fs->h_u.usr_ip4_spec.tos,
671 &fs->m_u.usr_ip4_spec.tos,
672 sizeof(fs->h_u.usr_ip4_spec.tos));
673 bcmgenet_hfb_insert_data(f_data, 23 + offset,
674 &fs->h_u.usr_ip4_spec.proto,
675 &fs->m_u.usr_ip4_spec.proto,
676 sizeof(fs->h_u.usr_ip4_spec.proto));
677 bcmgenet_hfb_insert_data(f_data, 26 + offset,
678 &fs->h_u.usr_ip4_spec.ip4src,
679 &fs->m_u.usr_ip4_spec.ip4src,
680 sizeof(fs->h_u.usr_ip4_spec.ip4src));
681 bcmgenet_hfb_insert_data(f_data, 30 + offset,
682 &fs->h_u.usr_ip4_spec.ip4dst,
683 &fs->m_u.usr_ip4_spec.ip4dst,
684 sizeof(fs->h_u.usr_ip4_spec.ip4dst));
685 if (!fs->m_u.usr_ip4_spec.l4_4_bytes)
686 break;
687
688 /* Only supports 20 byte IPv4 header */
689 val_8 = 0x45;
690 mask_8 = 0xFF;
691 bcmgenet_hfb_insert_data(f_data, ETH_HLEN + offset,
692 &val_8, &mask_8,
693 sizeof(val_8));
694 size = sizeof(fs->h_u.usr_ip4_spec.l4_4_bytes);
695 bcmgenet_hfb_insert_data(f_data,
696 ETH_HLEN + 20 + offset,
697 &fs->h_u.usr_ip4_spec.l4_4_bytes,
698 &fs->m_u.usr_ip4_spec.l4_4_bytes,
699 size);
700 f_length += DIV_ROUND_UP(size, 2);
701 break;
702 }
703
Doug Bergerf50932c2020-04-29 13:02:06 -0700704 if (!fs->ring_cookie || fs->ring_cookie == RX_CLS_FLOW_WAKE) {
Doug Berger3e370952020-04-29 13:02:05 -0700705 /* Ring 0 flows can be handled by the default Descriptor Ring
706 * We'll map them to ring 0, but don't enable the filter
707 */
708 bcmgenet_hfb_set_filter(priv, f_data, f_length, 0,
709 fs->location);
710 rule->state = BCMGENET_RXNFC_STATE_DISABLED;
711 } else {
712 /* Other Rx rings are direct mapped here */
713 bcmgenet_hfb_set_filter(priv, f_data, f_length,
714 fs->ring_cookie, fs->location);
715 bcmgenet_hfb_enable_filter(priv, fs->location);
716 rule->state = BCMGENET_RXNFC_STATE_ENABLED;
717 }
718
719 kfree(f_data);
720
721 return err;
722}
723
Doug Berger854295d2020-04-29 13:02:04 -0700724/* bcmgenet_hfb_clear
725 *
726 * Clear Hardware Filter Block and disable all filtering.
727 */
728static void bcmgenet_hfb_clear(struct bcmgenet_priv *priv)
729{
730 u32 i;
731
732 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_CTRL);
733 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS);
734 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS + 4);
735
736 for (i = DMA_INDEX2RING_0; i <= DMA_INDEX2RING_7; i++)
737 bcmgenet_rdma_writel(priv, 0x0, i);
738
739 for (i = 0; i < (priv->hw_params->hfb_filter_cnt / 4); i++)
740 bcmgenet_hfb_reg_writel(priv, 0x0,
741 HFB_FLT_LEN_V3PLUS + i * sizeof(u32));
742
743 for (i = 0; i < priv->hw_params->hfb_filter_cnt *
744 priv->hw_params->hfb_filter_size; i++)
745 bcmgenet_hfb_writel(priv, 0x0, i * sizeof(u32));
746}
747
748static void bcmgenet_hfb_init(struct bcmgenet_priv *priv)
749{
Doug Berger3e370952020-04-29 13:02:05 -0700750 int i;
751
Doug Berger854295d2020-04-29 13:02:04 -0700752 if (GENET_IS_V1(priv) || GENET_IS_V2(priv))
753 return;
754
Doug Berger3e370952020-04-29 13:02:05 -0700755 INIT_LIST_HEAD(&priv->rxnfc_list);
756 for (i = 0; i < MAX_NUM_OF_FS_RULES; i++) {
757 INIT_LIST_HEAD(&priv->rxnfc_rules[i].list);
758 priv->rxnfc_rules[i].state = BCMGENET_RXNFC_STATE_UNUSED;
759 }
760
Doug Berger854295d2020-04-29 13:02:04 -0700761 bcmgenet_hfb_clear(priv);
762}
763
Edwin Chan89316fa2017-03-09 16:58:49 -0800764static int bcmgenet_begin(struct net_device *dev)
765{
766 struct bcmgenet_priv *priv = netdev_priv(dev);
767
768 /* Turn on the clock */
769 return clk_prepare_enable(priv->clk);
770}
771
772static void bcmgenet_complete(struct net_device *dev)
773{
774 struct bcmgenet_priv *priv = netdev_priv(dev);
775
776 /* Turn off the clock */
777 clk_disable_unprepare(priv->clk);
778}
779
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200780static int bcmgenet_get_link_ksettings(struct net_device *dev,
781 struct ethtool_link_ksettings *cmd)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200782{
783 if (!netif_running(dev))
784 return -EINVAL;
785
Doug Berger6c97f012017-10-25 15:04:19 -0700786 if (!dev->phydev)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200787 return -ENODEV;
788
Doug Berger6c97f012017-10-25 15:04:19 -0700789 phy_ethtool_ksettings_get(dev->phydev, cmd);
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +0300790
791 return 0;
Philippe Reynesbac65c42016-07-09 00:54:47 +0200792}
793
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200794static int bcmgenet_set_link_ksettings(struct net_device *dev,
795 const struct ethtool_link_ksettings *cmd)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200796{
797 if (!netif_running(dev))
798 return -EINVAL;
799
Doug Berger6c97f012017-10-25 15:04:19 -0700800 if (!dev->phydev)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200801 return -ENODEV;
802
Doug Berger6c97f012017-10-25 15:04:19 -0700803 return phy_ethtool_ksettings_set(dev->phydev, cmd);
Philippe Reynesbac65c42016-07-09 00:54:47 +0200804}
805
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800806static int bcmgenet_set_features(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700807 netdev_features_t features)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800808{
Doug Bergerf63db4e2019-12-17 16:51:11 -0800809 struct bcmgenet_priv *priv = netdev_priv(dev);
810 u32 reg;
811 int ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800812
Doug Bergerf63db4e2019-12-17 16:51:11 -0800813 ret = clk_prepare_enable(priv->clk);
814 if (ret)
815 return ret;
816
817 /* Make sure we reflect the value of CRC_CMD_FWD */
818 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
819 priv->crc_fwd_en = !!(reg & CMD_CRC_FWD);
820
Doug Bergerf63db4e2019-12-17 16:51:11 -0800821 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800822
823 return ret;
824}
825
826static u32 bcmgenet_get_msglevel(struct net_device *dev)
827{
828 struct bcmgenet_priv *priv = netdev_priv(dev);
829
830 return priv->msg_enable;
831}
832
833static void bcmgenet_set_msglevel(struct net_device *dev, u32 level)
834{
835 struct bcmgenet_priv *priv = netdev_priv(dev);
836
837 priv->msg_enable = level;
838}
839
Florian Fainelli2f913072015-09-16 16:47:39 -0700840static int bcmgenet_get_coalesce(struct net_device *dev,
841 struct ethtool_coalesce *ec)
842{
843 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700844 struct bcmgenet_rx_ring *ring;
845 unsigned int i;
Florian Fainelli2f913072015-09-16 16:47:39 -0700846
847 ec->tx_max_coalesced_frames =
848 bcmgenet_tdma_ring_readl(priv, DESC_INDEX,
849 DMA_MBUF_DONE_THRESH);
Florian Fainelli4a296452015-09-16 16:47:40 -0700850 ec->rx_max_coalesced_frames =
851 bcmgenet_rdma_ring_readl(priv, DESC_INDEX,
852 DMA_MBUF_DONE_THRESH);
853 ec->rx_coalesce_usecs =
854 bcmgenet_rdma_readl(priv, DMA_RING16_TIMEOUT) * 8192 / 1000;
Florian Fainelli2f913072015-09-16 16:47:39 -0700855
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700856 for (i = 0; i < priv->hw_params->rx_queues; i++) {
857 ring = &priv->rx_rings[i];
858 ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
859 }
860 ring = &priv->rx_rings[DESC_INDEX];
861 ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
862
Florian Fainelli2f913072015-09-16 16:47:39 -0700863 return 0;
864}
865
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700866static void bcmgenet_set_rx_coalesce(struct bcmgenet_rx_ring *ring,
867 u32 usecs, u32 pkts)
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700868{
869 struct bcmgenet_priv *priv = ring->priv;
870 unsigned int i = ring->index;
871 u32 reg;
872
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700873 bcmgenet_rdma_ring_writel(priv, i, pkts, DMA_MBUF_DONE_THRESH);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700874
875 reg = bcmgenet_rdma_readl(priv, DMA_RING0_TIMEOUT + i);
876 reg &= ~DMA_TIMEOUT_MASK;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700877 reg |= DIV_ROUND_UP(usecs * 1000, 8192);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700878 bcmgenet_rdma_writel(priv, reg, DMA_RING0_TIMEOUT + i);
879}
880
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700881static void bcmgenet_set_ring_rx_coalesce(struct bcmgenet_rx_ring *ring,
882 struct ethtool_coalesce *ec)
883{
Tal Gilboa8960b382019-01-31 16:44:48 +0200884 struct dim_cq_moder moder;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700885 u32 usecs, pkts;
886
887 ring->rx_coalesce_usecs = ec->rx_coalesce_usecs;
888 ring->rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
889 usecs = ring->rx_coalesce_usecs;
890 pkts = ring->rx_max_coalesced_frames;
891
892 if (ec->use_adaptive_rx_coalesce && !ring->dim.use_dim) {
Tal Gilboa026a8072018-04-24 13:36:01 +0300893 moder = net_dim_get_def_rx_moderation(ring->dim.dim.mode);
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700894 usecs = moder.usec;
895 pkts = moder.pkts;
896 }
897
898 ring->dim.use_dim = ec->use_adaptive_rx_coalesce;
899 bcmgenet_set_rx_coalesce(ring, usecs, pkts);
900}
901
Florian Fainelli2f913072015-09-16 16:47:39 -0700902static int bcmgenet_set_coalesce(struct net_device *dev,
903 struct ethtool_coalesce *ec)
904{
905 struct bcmgenet_priv *priv = netdev_priv(dev);
906 unsigned int i;
907
Florian Fainelli4a296452015-09-16 16:47:40 -0700908 /* Base system clock is 125Mhz, DMA timeout is this reference clock
909 * divided by 1024, which yields roughly 8.192us, our maximum value
910 * has to fit in the DMA_TIMEOUT_MASK (16 bits)
911 */
Florian Fainelli2f913072015-09-16 16:47:39 -0700912 if (ec->tx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
Florian Fainelli4a296452015-09-16 16:47:40 -0700913 ec->tx_max_coalesced_frames == 0 ||
914 ec->rx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
915 ec->rx_coalesce_usecs > (DMA_TIMEOUT_MASK * 8) + 1)
916 return -EINVAL;
917
918 if (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0)
Florian Fainelli2f913072015-09-16 16:47:39 -0700919 return -EINVAL;
920
921 /* GENET TDMA hardware does not support a configurable timeout, but will
922 * always generate an interrupt either after MBDONE packets have been
Doug Berger556c2cf2017-03-13 17:41:34 -0700923 * transmitted, or when the ring is empty.
Florian Fainelli2f913072015-09-16 16:47:39 -0700924 */
Florian Fainelli2f913072015-09-16 16:47:39 -0700925
926 /* Program all TX queues with the same values, as there is no
927 * ethtool knob to do coalescing on a per-queue basis
928 */
929 for (i = 0; i < priv->hw_params->tx_queues; i++)
930 bcmgenet_tdma_ring_writel(priv, i,
931 ec->tx_max_coalesced_frames,
932 DMA_MBUF_DONE_THRESH);
933 bcmgenet_tdma_ring_writel(priv, DESC_INDEX,
934 ec->tx_max_coalesced_frames,
935 DMA_MBUF_DONE_THRESH);
936
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700937 for (i = 0; i < priv->hw_params->rx_queues; i++)
938 bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[i], ec);
939 bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[DESC_INDEX], ec);
Florian Fainelli4a296452015-09-16 16:47:40 -0700940
Florian Fainelli2f913072015-09-16 16:47:39 -0700941 return 0;
942}
943
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800944/* standard ethtool support functions. */
945enum bcmgenet_stat_type {
946 BCMGENET_STAT_NETDEV = -1,
947 BCMGENET_STAT_MIB_RX,
948 BCMGENET_STAT_MIB_TX,
949 BCMGENET_STAT_RUNT,
950 BCMGENET_STAT_MISC,
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800951 BCMGENET_STAT_SOFT,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800952};
953
954struct bcmgenet_stats {
955 char stat_string[ETH_GSTRING_LEN];
956 int stat_sizeof;
957 int stat_offset;
958 enum bcmgenet_stat_type type;
959 /* reg offset from UMAC base for misc counters */
960 u16 reg_offset;
961};
962
963#define STAT_NETDEV(m) { \
964 .stat_string = __stringify(m), \
965 .stat_sizeof = sizeof(((struct net_device_stats *)0)->m), \
966 .stat_offset = offsetof(struct net_device_stats, m), \
967 .type = BCMGENET_STAT_NETDEV, \
968}
969
970#define STAT_GENET_MIB(str, m, _type) { \
971 .stat_string = str, \
972 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
973 .stat_offset = offsetof(struct bcmgenet_priv, m), \
974 .type = _type, \
975}
976
977#define STAT_GENET_MIB_RX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_RX)
978#define STAT_GENET_MIB_TX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_TX)
979#define STAT_GENET_RUNT(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_RUNT)
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800980#define STAT_GENET_SOFT_MIB(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_SOFT)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800981
982#define STAT_GENET_MISC(str, m, offset) { \
983 .stat_string = str, \
984 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
985 .stat_offset = offsetof(struct bcmgenet_priv, m), \
986 .type = BCMGENET_STAT_MISC, \
987 .reg_offset = offset, \
988}
989
Florian Fainelli37a30b42017-03-16 10:27:08 -0700990#define STAT_GENET_Q(num) \
991 STAT_GENET_SOFT_MIB("txq" __stringify(num) "_packets", \
992 tx_rings[num].packets), \
993 STAT_GENET_SOFT_MIB("txq" __stringify(num) "_bytes", \
994 tx_rings[num].bytes), \
995 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_bytes", \
996 rx_rings[num].bytes), \
997 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_packets", \
998 rx_rings[num].packets), \
999 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_errors", \
1000 rx_rings[num].errors), \
1001 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_dropped", \
1002 rx_rings[num].dropped)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001003
1004/* There is a 0xC gap between the end of RX and beginning of TX stats and then
1005 * between the end of TX stats and the beginning of the RX RUNT
1006 */
1007#define BCMGENET_STAT_OFFSET 0xc
1008
1009/* Hardware counters must be kept in sync because the order/offset
1010 * is important here (order in structure declaration = order in hardware)
1011 */
1012static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
1013 /* general stats */
1014 STAT_NETDEV(rx_packets),
1015 STAT_NETDEV(tx_packets),
1016 STAT_NETDEV(rx_bytes),
1017 STAT_NETDEV(tx_bytes),
1018 STAT_NETDEV(rx_errors),
1019 STAT_NETDEV(tx_errors),
1020 STAT_NETDEV(rx_dropped),
1021 STAT_NETDEV(tx_dropped),
1022 STAT_NETDEV(multicast),
1023 /* UniMAC RSV counters */
1024 STAT_GENET_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
1025 STAT_GENET_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
1026 STAT_GENET_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
1027 STAT_GENET_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
1028 STAT_GENET_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
1029 STAT_GENET_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
1030 STAT_GENET_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
1031 STAT_GENET_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
1032 STAT_GENET_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
1033 STAT_GENET_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
1034 STAT_GENET_MIB_RX("rx_pkts", mib.rx.pkt),
1035 STAT_GENET_MIB_RX("rx_bytes", mib.rx.bytes),
1036 STAT_GENET_MIB_RX("rx_multicast", mib.rx.mca),
1037 STAT_GENET_MIB_RX("rx_broadcast", mib.rx.bca),
1038 STAT_GENET_MIB_RX("rx_fcs", mib.rx.fcs),
1039 STAT_GENET_MIB_RX("rx_control", mib.rx.cf),
1040 STAT_GENET_MIB_RX("rx_pause", mib.rx.pf),
1041 STAT_GENET_MIB_RX("rx_unknown", mib.rx.uo),
1042 STAT_GENET_MIB_RX("rx_align", mib.rx.aln),
1043 STAT_GENET_MIB_RX("rx_outrange", mib.rx.flr),
1044 STAT_GENET_MIB_RX("rx_code", mib.rx.cde),
1045 STAT_GENET_MIB_RX("rx_carrier", mib.rx.fcr),
1046 STAT_GENET_MIB_RX("rx_oversize", mib.rx.ovr),
1047 STAT_GENET_MIB_RX("rx_jabber", mib.rx.jbr),
1048 STAT_GENET_MIB_RX("rx_mtu_err", mib.rx.mtue),
1049 STAT_GENET_MIB_RX("rx_good_pkts", mib.rx.pok),
1050 STAT_GENET_MIB_RX("rx_unicast", mib.rx.uc),
1051 STAT_GENET_MIB_RX("rx_ppp", mib.rx.ppp),
1052 STAT_GENET_MIB_RX("rx_crc", mib.rx.rcrc),
1053 /* UniMAC TSV counters */
1054 STAT_GENET_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
1055 STAT_GENET_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
1056 STAT_GENET_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
1057 STAT_GENET_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
1058 STAT_GENET_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
1059 STAT_GENET_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
1060 STAT_GENET_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
1061 STAT_GENET_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
1062 STAT_GENET_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
1063 STAT_GENET_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
1064 STAT_GENET_MIB_TX("tx_pkts", mib.tx.pkts),
1065 STAT_GENET_MIB_TX("tx_multicast", mib.tx.mca),
1066 STAT_GENET_MIB_TX("tx_broadcast", mib.tx.bca),
1067 STAT_GENET_MIB_TX("tx_pause", mib.tx.pf),
1068 STAT_GENET_MIB_TX("tx_control", mib.tx.cf),
1069 STAT_GENET_MIB_TX("tx_fcs_err", mib.tx.fcs),
1070 STAT_GENET_MIB_TX("tx_oversize", mib.tx.ovr),
1071 STAT_GENET_MIB_TX("tx_defer", mib.tx.drf),
1072 STAT_GENET_MIB_TX("tx_excess_defer", mib.tx.edf),
1073 STAT_GENET_MIB_TX("tx_single_col", mib.tx.scl),
1074 STAT_GENET_MIB_TX("tx_multi_col", mib.tx.mcl),
1075 STAT_GENET_MIB_TX("tx_late_col", mib.tx.lcl),
1076 STAT_GENET_MIB_TX("tx_excess_col", mib.tx.ecl),
1077 STAT_GENET_MIB_TX("tx_frags", mib.tx.frg),
1078 STAT_GENET_MIB_TX("tx_total_col", mib.tx.ncl),
1079 STAT_GENET_MIB_TX("tx_jabber", mib.tx.jbr),
1080 STAT_GENET_MIB_TX("tx_bytes", mib.tx.bytes),
1081 STAT_GENET_MIB_TX("tx_good_pkts", mib.tx.pok),
1082 STAT_GENET_MIB_TX("tx_unicast", mib.tx.uc),
1083 /* UniMAC RUNT counters */
1084 STAT_GENET_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
1085 STAT_GENET_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
1086 STAT_GENET_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
1087 STAT_GENET_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
1088 /* Misc UniMAC counters */
1089 STAT_GENET_MISC("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt,
Doug Bergerffff7132017-03-09 16:58:43 -08001090 UMAC_RBUF_OVFL_CNT_V1),
1091 STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt,
1092 UMAC_RBUF_ERR_CNT_V1),
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001093 STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT),
Florian Fainellif62ba9c2015-02-28 18:09:16 -08001094 STAT_GENET_SOFT_MIB("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
1095 STAT_GENET_SOFT_MIB("rx_dma_failed", mib.rx_dma_failed),
1096 STAT_GENET_SOFT_MIB("tx_dma_failed", mib.tx_dma_failed),
Doug Bergerf1af17c2019-12-17 16:51:15 -08001097 STAT_GENET_SOFT_MIB("tx_realloc_tsb", mib.tx_realloc_tsb),
1098 STAT_GENET_SOFT_MIB("tx_realloc_tsb_failed",
1099 mib.tx_realloc_tsb_failed),
Florian Fainelli37a30b42017-03-16 10:27:08 -07001100 /* Per TX queues */
1101 STAT_GENET_Q(0),
1102 STAT_GENET_Q(1),
1103 STAT_GENET_Q(2),
1104 STAT_GENET_Q(3),
1105 STAT_GENET_Q(16),
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001106};
1107
1108#define BCMGENET_STATS_LEN ARRAY_SIZE(bcmgenet_gstrings_stats)
1109
1110static void bcmgenet_get_drvinfo(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001111 struct ethtool_drvinfo *info)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001112{
1113 strlcpy(info->driver, "bcmgenet", sizeof(info->driver));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001114}
1115
1116static int bcmgenet_get_sset_count(struct net_device *dev, int string_set)
1117{
1118 switch (string_set) {
1119 case ETH_SS_STATS:
1120 return BCMGENET_STATS_LEN;
1121 default:
1122 return -EOPNOTSUPP;
1123 }
1124}
1125
Florian Fainellic91b7f62014-07-23 10:42:12 -07001126static void bcmgenet_get_strings(struct net_device *dev, u32 stringset,
1127 u8 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001128{
1129 int i;
1130
1131 switch (stringset) {
1132 case ETH_SS_STATS:
1133 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
1134 memcpy(data + i * ETH_GSTRING_LEN,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001135 bcmgenet_gstrings_stats[i].stat_string,
1136 ETH_GSTRING_LEN);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001137 }
1138 break;
1139 }
1140}
1141
Doug Bergerffff7132017-03-09 16:58:43 -08001142static u32 bcmgenet_update_stat_misc(struct bcmgenet_priv *priv, u16 offset)
1143{
1144 u16 new_offset;
1145 u32 val;
1146
1147 switch (offset) {
1148 case UMAC_RBUF_OVFL_CNT_V1:
1149 if (GENET_IS_V2(priv))
1150 new_offset = RBUF_OVFL_CNT_V2;
1151 else
1152 new_offset = RBUF_OVFL_CNT_V3PLUS;
1153
1154 val = bcmgenet_rbuf_readl(priv, new_offset);
1155 /* clear if overflowed */
1156 if (val == ~0)
1157 bcmgenet_rbuf_writel(priv, 0, new_offset);
1158 break;
1159 case UMAC_RBUF_ERR_CNT_V1:
1160 if (GENET_IS_V2(priv))
1161 new_offset = RBUF_ERR_CNT_V2;
1162 else
1163 new_offset = RBUF_ERR_CNT_V3PLUS;
1164
1165 val = bcmgenet_rbuf_readl(priv, new_offset);
1166 /* clear if overflowed */
1167 if (val == ~0)
1168 bcmgenet_rbuf_writel(priv, 0, new_offset);
1169 break;
1170 default:
1171 val = bcmgenet_umac_readl(priv, offset);
1172 /* clear if overflowed */
1173 if (val == ~0)
1174 bcmgenet_umac_writel(priv, 0, offset);
1175 break;
1176 }
1177
1178 return val;
1179}
1180
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001181static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv)
1182{
1183 int i, j = 0;
1184
1185 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
1186 const struct bcmgenet_stats *s;
1187 u8 offset = 0;
1188 u32 val = 0;
1189 char *p;
1190
1191 s = &bcmgenet_gstrings_stats[i];
1192 switch (s->type) {
1193 case BCMGENET_STAT_NETDEV:
Florian Fainellif62ba9c2015-02-28 18:09:16 -08001194 case BCMGENET_STAT_SOFT:
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001195 continue;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001196 case BCMGENET_STAT_RUNT:
Doug Berger1ad3d222017-03-09 16:58:44 -08001197 offset += BCMGENET_STAT_OFFSET;
1198 /* fall through */
1199 case BCMGENET_STAT_MIB_TX:
1200 offset += BCMGENET_STAT_OFFSET;
1201 /* fall through */
1202 case BCMGENET_STAT_MIB_RX:
Florian Fainellic91b7f62014-07-23 10:42:12 -07001203 val = bcmgenet_umac_readl(priv,
1204 UMAC_MIB_START + j + offset);
Doug Berger1ad3d222017-03-09 16:58:44 -08001205 offset = 0; /* Reset Offset */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001206 break;
1207 case BCMGENET_STAT_MISC:
Doug Bergerffff7132017-03-09 16:58:43 -08001208 if (GENET_IS_V1(priv)) {
1209 val = bcmgenet_umac_readl(priv, s->reg_offset);
1210 /* clear if overflowed */
1211 if (val == ~0)
1212 bcmgenet_umac_writel(priv, 0,
1213 s->reg_offset);
1214 } else {
1215 val = bcmgenet_update_stat_misc(priv,
1216 s->reg_offset);
1217 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001218 break;
1219 }
1220
1221 j += s->stat_sizeof;
1222 p = (char *)priv + s->stat_offset;
1223 *(u32 *)p = val;
1224 }
1225}
1226
1227static void bcmgenet_get_ethtool_stats(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001228 struct ethtool_stats *stats,
1229 u64 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001230{
1231 struct bcmgenet_priv *priv = netdev_priv(dev);
1232 int i;
1233
1234 if (netif_running(dev))
1235 bcmgenet_update_mib_counters(priv);
1236
Doug Bergera6d0b832020-04-23 15:44:17 -07001237 dev->netdev_ops->ndo_get_stats(dev);
1238
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001239 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
1240 const struct bcmgenet_stats *s;
1241 char *p;
1242
1243 s = &bcmgenet_gstrings_stats[i];
1244 if (s->type == BCMGENET_STAT_NETDEV)
1245 p = (char *)&dev->stats;
1246 else
1247 p = (char *)priv;
1248 p += s->stat_offset;
Eric Dumazet6517eb52016-04-15 10:47:52 -07001249 if (sizeof(unsigned long) != sizeof(u32) &&
1250 s->stat_sizeof == sizeof(unsigned long))
1251 data[i] = *(unsigned long *)p;
1252 else
1253 data[i] = *(u32 *)p;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001254 }
1255}
1256
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001257static void bcmgenet_eee_enable_set(struct net_device *dev, bool enable)
1258{
1259 struct bcmgenet_priv *priv = netdev_priv(dev);
1260 u32 off = priv->hw_params->tbuf_offset + TBUF_ENERGY_CTRL;
1261 u32 reg;
1262
1263 if (enable && !priv->clk_eee_enabled) {
1264 clk_prepare_enable(priv->clk_eee);
1265 priv->clk_eee_enabled = true;
1266 }
1267
1268 reg = bcmgenet_umac_readl(priv, UMAC_EEE_CTRL);
1269 if (enable)
1270 reg |= EEE_EN;
1271 else
1272 reg &= ~EEE_EN;
1273 bcmgenet_umac_writel(priv, reg, UMAC_EEE_CTRL);
1274
1275 /* Enable EEE and switch to a 27Mhz clock automatically */
Florian Fainelli69d2ea92017-08-29 12:25:31 -07001276 reg = bcmgenet_readl(priv->base + off);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001277 if (enable)
1278 reg |= TBUF_EEE_EN | TBUF_PM_EN;
1279 else
1280 reg &= ~(TBUF_EEE_EN | TBUF_PM_EN);
Florian Fainelli69d2ea92017-08-29 12:25:31 -07001281 bcmgenet_writel(reg, priv->base + off);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001282
1283 /* Do the same for thing for RBUF */
1284 reg = bcmgenet_rbuf_readl(priv, RBUF_ENERGY_CTRL);
1285 if (enable)
1286 reg |= RBUF_EEE_EN | RBUF_PM_EN;
1287 else
1288 reg &= ~(RBUF_EEE_EN | RBUF_PM_EN);
1289 bcmgenet_rbuf_writel(priv, reg, RBUF_ENERGY_CTRL);
1290
1291 if (!enable && priv->clk_eee_enabled) {
1292 clk_disable_unprepare(priv->clk_eee);
1293 priv->clk_eee_enabled = false;
1294 }
1295
1296 priv->eee.eee_enabled = enable;
1297 priv->eee.eee_active = enable;
1298}
1299
1300static int bcmgenet_get_eee(struct net_device *dev, struct ethtool_eee *e)
1301{
1302 struct bcmgenet_priv *priv = netdev_priv(dev);
1303 struct ethtool_eee *p = &priv->eee;
1304
1305 if (GENET_IS_V1(priv))
1306 return -EOPNOTSUPP;
1307
Doug Berger6c97f012017-10-25 15:04:19 -07001308 if (!dev->phydev)
1309 return -ENODEV;
1310
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001311 e->eee_enabled = p->eee_enabled;
1312 e->eee_active = p->eee_active;
1313 e->tx_lpi_timer = bcmgenet_umac_readl(priv, UMAC_EEE_LPI_TIMER);
1314
Doug Berger6c97f012017-10-25 15:04:19 -07001315 return phy_ethtool_get_eee(dev->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001316}
1317
1318static int bcmgenet_set_eee(struct net_device *dev, struct ethtool_eee *e)
1319{
1320 struct bcmgenet_priv *priv = netdev_priv(dev);
1321 struct ethtool_eee *p = &priv->eee;
1322 int ret = 0;
1323
1324 if (GENET_IS_V1(priv))
1325 return -EOPNOTSUPP;
1326
Doug Berger6c97f012017-10-25 15:04:19 -07001327 if (!dev->phydev)
1328 return -ENODEV;
1329
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001330 p->eee_enabled = e->eee_enabled;
1331
1332 if (!p->eee_enabled) {
1333 bcmgenet_eee_enable_set(dev, false);
1334 } else {
Doug Berger6c97f012017-10-25 15:04:19 -07001335 ret = phy_init_eee(dev->phydev, 0);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001336 if (ret) {
1337 netif_err(priv, hw, dev, "EEE initialization failed\n");
1338 return ret;
1339 }
1340
1341 bcmgenet_umac_writel(priv, e->tx_lpi_timer, UMAC_EEE_LPI_TIMER);
1342 bcmgenet_eee_enable_set(dev, true);
1343 }
1344
Doug Berger6c97f012017-10-25 15:04:19 -07001345 return phy_ethtool_set_eee(dev->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001346}
1347
Doug Berger3e370952020-04-29 13:02:05 -07001348static int bcmgenet_validate_flow(struct net_device *dev,
1349 struct ethtool_rxnfc *cmd)
1350{
1351 struct ethtool_usrip4_spec *l4_mask;
1352 struct ethhdr *eth_mask;
1353
1354 if (cmd->fs.location >= MAX_NUM_OF_FS_RULES) {
1355 netdev_err(dev, "rxnfc: Invalid location (%d)\n",
1356 cmd->fs.location);
1357 return -EINVAL;
1358 }
1359
1360 switch (cmd->fs.flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
1361 case IP_USER_FLOW:
1362 l4_mask = &cmd->fs.m_u.usr_ip4_spec;
1363 /* don't allow mask which isn't valid */
1364 if (VALIDATE_MASK(l4_mask->ip4src) ||
1365 VALIDATE_MASK(l4_mask->ip4dst) ||
1366 VALIDATE_MASK(l4_mask->l4_4_bytes) ||
1367 VALIDATE_MASK(l4_mask->proto) ||
1368 VALIDATE_MASK(l4_mask->ip_ver) ||
1369 VALIDATE_MASK(l4_mask->tos)) {
1370 netdev_err(dev, "rxnfc: Unsupported mask\n");
1371 return -EINVAL;
1372 }
1373 break;
1374 case ETHER_FLOW:
1375 eth_mask = &cmd->fs.m_u.ether_spec;
1376 /* don't allow mask which isn't valid */
1377 if (VALIDATE_MASK(eth_mask->h_source) ||
1378 VALIDATE_MASK(eth_mask->h_source) ||
1379 VALIDATE_MASK(eth_mask->h_proto)) {
1380 netdev_err(dev, "rxnfc: Unsupported mask\n");
1381 return -EINVAL;
1382 }
1383 break;
1384 default:
1385 netdev_err(dev, "rxnfc: Unsupported flow type (0x%x)\n",
1386 cmd->fs.flow_type);
1387 return -EINVAL;
1388 }
1389
1390 if ((cmd->fs.flow_type & FLOW_EXT)) {
1391 /* don't allow mask which isn't valid */
1392 if (VALIDATE_MASK(cmd->fs.m_ext.vlan_etype) ||
1393 VALIDATE_MASK(cmd->fs.m_ext.vlan_tci)) {
1394 netdev_err(dev, "rxnfc: Unsupported mask\n");
1395 return -EINVAL;
1396 }
1397 if (cmd->fs.m_ext.data[0] || cmd->fs.m_ext.data[1]) {
1398 netdev_err(dev, "rxnfc: user-def not supported\n");
1399 return -EINVAL;
1400 }
1401 }
1402
1403 if ((cmd->fs.flow_type & FLOW_MAC_EXT)) {
1404 /* don't allow mask which isn't valid */
1405 if (VALIDATE_MASK(cmd->fs.m_ext.h_dest)) {
1406 netdev_err(dev, "rxnfc: Unsupported mask\n");
1407 return -EINVAL;
1408 }
1409 }
1410
1411 return 0;
1412}
1413
1414static int bcmgenet_insert_flow(struct net_device *dev,
1415 struct ethtool_rxnfc *cmd)
1416{
1417 struct bcmgenet_priv *priv = netdev_priv(dev);
1418 struct bcmgenet_rxnfc_rule *loc_rule;
1419 int err;
1420
1421 if (priv->hw_params->hfb_filter_size < 128) {
1422 netdev_err(dev, "rxnfc: Not supported by this device\n");
1423 return -EINVAL;
1424 }
1425
Doug Bergerf50932c2020-04-29 13:02:06 -07001426 if (cmd->fs.ring_cookie > priv->hw_params->rx_queues &&
1427 cmd->fs.ring_cookie != RX_CLS_FLOW_WAKE) {
Doug Berger3e370952020-04-29 13:02:05 -07001428 netdev_err(dev, "rxnfc: Unsupported action (%llu)\n",
1429 cmd->fs.ring_cookie);
1430 return -EINVAL;
1431 }
1432
1433 err = bcmgenet_validate_flow(dev, cmd);
1434 if (err)
1435 return err;
1436
1437 loc_rule = &priv->rxnfc_rules[cmd->fs.location];
1438 if (loc_rule->state == BCMGENET_RXNFC_STATE_ENABLED)
1439 bcmgenet_hfb_disable_filter(priv, cmd->fs.location);
1440 if (loc_rule->state != BCMGENET_RXNFC_STATE_UNUSED)
1441 list_del(&loc_rule->list);
1442 loc_rule->state = BCMGENET_RXNFC_STATE_UNUSED;
1443 memcpy(&loc_rule->fs, &cmd->fs,
1444 sizeof(struct ethtool_rx_flow_spec));
1445
1446 err = bcmgenet_hfb_create_rxnfc_filter(priv, loc_rule);
1447 if (err) {
1448 netdev_err(dev, "rxnfc: Could not install rule (%d)\n",
1449 err);
1450 return err;
1451 }
1452
1453 list_add_tail(&loc_rule->list, &priv->rxnfc_list);
1454
1455 return 0;
1456}
1457
1458static int bcmgenet_delete_flow(struct net_device *dev,
1459 struct ethtool_rxnfc *cmd)
1460{
1461 struct bcmgenet_priv *priv = netdev_priv(dev);
1462 struct bcmgenet_rxnfc_rule *rule;
1463 int err = 0;
1464
1465 if (cmd->fs.location >= MAX_NUM_OF_FS_RULES)
1466 return -EINVAL;
1467
1468 rule = &priv->rxnfc_rules[cmd->fs.location];
1469 if (rule->state == BCMGENET_RXNFC_STATE_UNUSED) {
1470 err = -ENOENT;
1471 goto out;
1472 }
1473
1474 if (rule->state == BCMGENET_RXNFC_STATE_ENABLED)
1475 bcmgenet_hfb_disable_filter(priv, cmd->fs.location);
1476 if (rule->state != BCMGENET_RXNFC_STATE_UNUSED)
1477 list_del(&rule->list);
1478 rule->state = BCMGENET_RXNFC_STATE_UNUSED;
1479 memset(&rule->fs, 0, sizeof(struct ethtool_rx_flow_spec));
1480
1481out:
1482 return err;
1483}
1484
1485static int bcmgenet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1486{
1487 struct bcmgenet_priv *priv = netdev_priv(dev);
1488 int err = 0;
1489
1490 switch (cmd->cmd) {
1491 case ETHTOOL_SRXCLSRLINS:
1492 err = bcmgenet_insert_flow(dev, cmd);
1493 break;
1494 case ETHTOOL_SRXCLSRLDEL:
1495 err = bcmgenet_delete_flow(dev, cmd);
1496 break;
1497 default:
1498 netdev_warn(priv->dev, "Unsupported ethtool command. (%d)\n",
1499 cmd->cmd);
1500 return -EINVAL;
1501 }
1502
1503 return err;
1504}
1505
1506static int bcmgenet_get_flow(struct net_device *dev, struct ethtool_rxnfc *cmd,
1507 int loc)
1508{
1509 struct bcmgenet_priv *priv = netdev_priv(dev);
1510 struct bcmgenet_rxnfc_rule *rule;
1511 int err = 0;
1512
1513 if (loc < 0 || loc >= MAX_NUM_OF_FS_RULES)
1514 return -EINVAL;
1515
1516 rule = &priv->rxnfc_rules[loc];
1517 if (rule->state == BCMGENET_RXNFC_STATE_UNUSED)
1518 err = -ENOENT;
1519 else
1520 memcpy(&cmd->fs, &rule->fs,
1521 sizeof(struct ethtool_rx_flow_spec));
1522
1523 return err;
1524}
1525
1526static int bcmgenet_get_num_flows(struct bcmgenet_priv *priv)
1527{
1528 struct list_head *pos;
1529 int res = 0;
1530
1531 list_for_each(pos, &priv->rxnfc_list)
1532 res++;
1533
1534 return res;
1535}
1536
1537static int bcmgenet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1538 u32 *rule_locs)
1539{
1540 struct bcmgenet_priv *priv = netdev_priv(dev);
1541 struct bcmgenet_rxnfc_rule *rule;
1542 int err = 0;
1543 int i = 0;
1544
1545 switch (cmd->cmd) {
1546 case ETHTOOL_GRXRINGS:
1547 cmd->data = priv->hw_params->rx_queues ?: 1;
1548 break;
1549 case ETHTOOL_GRXCLSRLCNT:
1550 cmd->rule_cnt = bcmgenet_get_num_flows(priv);
1551 cmd->data = MAX_NUM_OF_FS_RULES;
1552 break;
1553 case ETHTOOL_GRXCLSRULE:
1554 err = bcmgenet_get_flow(dev, cmd, cmd->fs.location);
1555 break;
1556 case ETHTOOL_GRXCLSRLALL:
1557 list_for_each_entry(rule, &priv->rxnfc_list, list)
1558 if (i < cmd->rule_cnt)
1559 rule_locs[i++] = rule->fs.location;
1560 cmd->rule_cnt = i;
1561 cmd->data = MAX_NUM_OF_FS_RULES;
1562 break;
1563 default:
1564 err = -EOPNOTSUPP;
1565 break;
1566 }
1567
1568 return err;
1569}
1570
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001571/* standard ethtool support functions. */
Julia Lawall70591ab2016-08-31 09:30:45 +02001572static const struct ethtool_ops bcmgenet_ethtool_ops = {
Jakub Kicinskif6f508c2020-03-09 19:15:03 -07001573 .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
1574 ETHTOOL_COALESCE_MAX_FRAMES |
1575 ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
Edwin Chan89316fa2017-03-09 16:58:49 -08001576 .begin = bcmgenet_begin,
1577 .complete = bcmgenet_complete,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001578 .get_strings = bcmgenet_get_strings,
1579 .get_sset_count = bcmgenet_get_sset_count,
1580 .get_ethtool_stats = bcmgenet_get_ethtool_stats,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001581 .get_drvinfo = bcmgenet_get_drvinfo,
1582 .get_link = ethtool_op_get_link,
1583 .get_msglevel = bcmgenet_get_msglevel,
1584 .set_msglevel = bcmgenet_set_msglevel,
Florian Fainelli06ba8372014-07-21 15:29:29 -07001585 .get_wol = bcmgenet_get_wol,
1586 .set_wol = bcmgenet_set_wol,
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001587 .get_eee = bcmgenet_get_eee,
1588 .set_eee = bcmgenet_set_eee,
Florian Fainelli016e7702016-11-15 10:06:38 -08001589 .nway_reset = phy_ethtool_nway_reset,
Florian Fainelli2f913072015-09-16 16:47:39 -07001590 .get_coalesce = bcmgenet_get_coalesce,
1591 .set_coalesce = bcmgenet_set_coalesce,
Philippe Reynesfa92bf02016-09-26 22:31:57 +02001592 .get_link_ksettings = bcmgenet_get_link_ksettings,
1593 .set_link_ksettings = bcmgenet_set_link_ksettings,
Ryan M. Collinsdd1bf472019-08-30 14:49:55 -04001594 .get_ts_info = ethtool_op_get_ts_info,
Doug Berger3e370952020-04-29 13:02:05 -07001595 .get_rxnfc = bcmgenet_get_rxnfc,
1596 .set_rxnfc = bcmgenet_set_rxnfc,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001597};
1598
1599/* Power down the unimac, based on mode. */
Florian Fainellica8cf342015-03-23 15:09:51 -07001600static int bcmgenet_power_down(struct bcmgenet_priv *priv,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001601 enum bcmgenet_power_mode mode)
1602{
Florian Fainellica8cf342015-03-23 15:09:51 -07001603 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001604 u32 reg;
1605
1606 switch (mode) {
1607 case GENET_POWER_CABLE_SENSE:
Doug Berger6c97f012017-10-25 15:04:19 -07001608 phy_detach(priv->dev->phydev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001609 break;
1610
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001611 case GENET_POWER_WOL_MAGIC:
Florian Fainellica8cf342015-03-23 15:09:51 -07001612 ret = bcmgenet_wol_power_down_cfg(priv, mode);
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001613 break;
1614
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001615 case GENET_POWER_PASSIVE:
1616 /* Power down LED */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001617 if (priv->hw_params->flags & GENET_HAS_EXT) {
1618 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
Doug Berger42138082017-03-13 17:41:42 -07001619 if (GENET_IS_V5(priv))
1620 reg |= EXT_PWR_DOWN_PHY_EN |
1621 EXT_PWR_DOWN_PHY_RD |
1622 EXT_PWR_DOWN_PHY_SD |
1623 EXT_PWR_DOWN_PHY_RX |
1624 EXT_PWR_DOWN_PHY_TX |
1625 EXT_IDDQ_GLBL_PWR;
1626 else
1627 reg |= EXT_PWR_DOWN_PHY;
1628
1629 reg |= (EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001630 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
Florian Fainellia642c4f2015-03-23 15:09:56 -07001631
1632 bcmgenet_phy_power_set(priv->dev, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001633 }
1634 break;
1635 default:
1636 break;
1637 }
Florian Fainellica8cf342015-03-23 15:09:51 -07001638
YueHaibing0db55092018-11-08 02:08:43 +00001639 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001640}
1641
1642static void bcmgenet_power_up(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001643 enum bcmgenet_power_mode mode)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001644{
1645 u32 reg;
1646
1647 if (!(priv->hw_params->flags & GENET_HAS_EXT))
1648 return;
1649
1650 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
1651
1652 switch (mode) {
1653 case GENET_POWER_PASSIVE:
Doug Berger42138082017-03-13 17:41:42 -07001654 reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
1655 if (GENET_IS_V5(priv)) {
1656 reg &= ~(EXT_PWR_DOWN_PHY_EN |
1657 EXT_PWR_DOWN_PHY_RD |
1658 EXT_PWR_DOWN_PHY_SD |
1659 EXT_PWR_DOWN_PHY_RX |
1660 EXT_PWR_DOWN_PHY_TX |
1661 EXT_IDDQ_GLBL_PWR);
1662 reg |= EXT_PHY_RESET;
1663 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1664 mdelay(1);
1665
1666 reg &= ~EXT_PHY_RESET;
1667 } else {
1668 reg &= ~EXT_PWR_DOWN_PHY;
1669 reg |= EXT_PWR_DN_EN_LD;
1670 }
1671 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1672 bcmgenet_phy_power_set(priv->dev, true);
Doug Berger42138082017-03-13 17:41:42 -07001673 break;
1674
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001675 case GENET_POWER_CABLE_SENSE:
1676 /* enable APD */
Doug Berger42138082017-03-13 17:41:42 -07001677 if (!GENET_IS_V5(priv)) {
1678 reg |= EXT_PWR_DN_EN_LD;
1679 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1680 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001681 break;
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001682 case GENET_POWER_WOL_MAGIC:
1683 bcmgenet_wol_power_up_cfg(priv, mode);
1684 return;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001685 default:
1686 break;
1687 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001688}
1689
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001690static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv,
1691 struct bcmgenet_tx_ring *ring)
1692{
1693 struct enet_cb *tx_cb_ptr;
1694
1695 tx_cb_ptr = ring->cbs;
1696 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
Petri Gynther014012a2015-02-23 11:00:45 -08001697
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001698 /* Advancing local write pointer */
1699 if (ring->write_ptr == ring->end_ptr)
1700 ring->write_ptr = ring->cb_ptr;
1701 else
1702 ring->write_ptr++;
1703
1704 return tx_cb_ptr;
1705}
1706
Doug Berger876dbad2017-07-14 16:12:09 -07001707static struct enet_cb *bcmgenet_put_txcb(struct bcmgenet_priv *priv,
1708 struct bcmgenet_tx_ring *ring)
1709{
1710 struct enet_cb *tx_cb_ptr;
1711
1712 tx_cb_ptr = ring->cbs;
1713 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
1714
1715 /* Rewinding local write pointer */
1716 if (ring->write_ptr == ring->cb_ptr)
1717 ring->write_ptr = ring->end_ptr;
1718 else
1719 ring->write_ptr--;
1720
1721 return tx_cb_ptr;
1722}
1723
Petri Gynther4055eae2015-03-25 12:35:16 -07001724static inline void bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring *ring)
1725{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001726 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001727 INTRL2_CPU_MASK_SET);
1728}
1729
1730static inline void bcmgenet_rx_ring16_int_enable(struct bcmgenet_rx_ring *ring)
1731{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001732 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001733 INTRL2_CPU_MASK_CLEAR);
1734}
1735
1736static inline void bcmgenet_rx_ring_int_disable(struct bcmgenet_rx_ring *ring)
1737{
1738 bcmgenet_intrl2_1_writel(ring->priv,
1739 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1740 INTRL2_CPU_MASK_SET);
1741}
1742
1743static inline void bcmgenet_rx_ring_int_enable(struct bcmgenet_rx_ring *ring)
1744{
1745 bcmgenet_intrl2_1_writel(ring->priv,
1746 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1747 INTRL2_CPU_MASK_CLEAR);
1748}
1749
Petri Gynther9dbac282015-03-25 12:35:10 -07001750static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001751{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001752 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001753 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001754}
1755
Petri Gynther9dbac282015-03-25 12:35:10 -07001756static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001757{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001758 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001759 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001760}
1761
Petri Gynther9dbac282015-03-25 12:35:10 -07001762static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001763{
Petri Gynther9dbac282015-03-25 12:35:10 -07001764 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001765 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001766}
1767
Petri Gynther9dbac282015-03-25 12:35:10 -07001768static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001769{
Petri Gynther9dbac282015-03-25 12:35:10 -07001770 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001771 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001772}
1773
Doug Bergerf48bed12017-07-14 16:12:10 -07001774/* Simple helper to free a transmit control block's resources
1775 * Returns an skb when the last transmit control block associated with the
1776 * skb is freed. The skb should be freed by the caller if necessary.
1777 */
1778static struct sk_buff *bcmgenet_free_tx_cb(struct device *dev,
1779 struct enet_cb *cb)
1780{
1781 struct sk_buff *skb;
1782
1783 skb = cb->skb;
1784
1785 if (skb) {
1786 cb->skb = NULL;
1787 if (cb == GENET_CB(skb)->first_cb)
1788 dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
1789 dma_unmap_len(cb, dma_len),
1790 DMA_TO_DEVICE);
1791 else
1792 dma_unmap_page(dev, dma_unmap_addr(cb, dma_addr),
1793 dma_unmap_len(cb, dma_len),
1794 DMA_TO_DEVICE);
1795 dma_unmap_addr_set(cb, dma_addr, 0);
1796
1797 if (cb == GENET_CB(skb)->last_cb)
1798 return skb;
1799
1800 } else if (dma_unmap_addr(cb, dma_addr)) {
1801 dma_unmap_page(dev,
1802 dma_unmap_addr(cb, dma_addr),
1803 dma_unmap_len(cb, dma_len),
1804 DMA_TO_DEVICE);
1805 dma_unmap_addr_set(cb, dma_addr, 0);
1806 }
1807
Wei Yongjun335ab8b2018-03-28 12:51:19 +00001808 return NULL;
Doug Bergerf48bed12017-07-14 16:12:10 -07001809}
1810
1811/* Simple helper to free a receive control block's resources */
1812static struct sk_buff *bcmgenet_free_rx_cb(struct device *dev,
1813 struct enet_cb *cb)
1814{
1815 struct sk_buff *skb;
1816
1817 skb = cb->skb;
1818 cb->skb = NULL;
1819
1820 if (dma_unmap_addr(cb, dma_addr)) {
1821 dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
1822 dma_unmap_len(cb, dma_len), DMA_FROM_DEVICE);
1823 dma_unmap_addr_set(cb, dma_addr, 0);
1824 }
1825
1826 return skb;
1827}
1828
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001829/* Unlocked version of the reclaim routine */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001830static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
1831 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001832{
1833 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther66d06752015-03-04 14:30:01 -08001834 unsigned int txbds_processed = 0;
Doug Bergerf48bed12017-07-14 16:12:10 -07001835 unsigned int bytes_compl = 0;
1836 unsigned int pkts_compl = 0;
1837 unsigned int txbds_ready;
1838 unsigned int c_index;
1839 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001840
Doug Bergerd5810ca2017-03-13 17:41:37 -07001841 /* Clear status before servicing to reduce spurious interrupts */
1842 if (ring->index == DESC_INDEX)
1843 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_TXDMA_DONE,
1844 INTRL2_CPU_CLEAR);
1845 else
1846 bcmgenet_intrl2_1_writel(priv, (1 << ring->index),
1847 INTRL2_CPU_CLEAR);
1848
Brian Norris7fc527f2014-07-29 14:34:14 -07001849 /* Compute how many buffers are transmitted since last xmit call */
Doug Bergerc298ede2017-03-13 17:41:33 -07001850 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX)
1851 & DMA_C_INDEX_MASK;
1852 txbds_ready = (c_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001853
1854 netif_dbg(priv, tx_done, dev,
Petri Gynther66d06752015-03-04 14:30:01 -08001855 "%s ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
1856 __func__, ring->index, ring->c_index, c_index, txbds_ready);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001857
1858 /* Reclaim transmitted buffers */
Petri Gynther66d06752015-03-04 14:30:01 -08001859 while (txbds_processed < txbds_ready) {
Doug Bergerf48bed12017-07-14 16:12:10 -07001860 skb = bcmgenet_free_tx_cb(&priv->pdev->dev,
1861 &priv->tx_cbs[ring->clean_ptr]);
1862 if (skb) {
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001863 pkts_compl++;
Doug Bergerf48bed12017-07-14 16:12:10 -07001864 bytes_compl += GENET_CB(skb)->bytes_sent;
Florian Fainellid4fec852017-08-24 15:56:29 -07001865 dev_consume_skb_any(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001866 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001867
Petri Gynther66d06752015-03-04 14:30:01 -08001868 txbds_processed++;
1869 if (likely(ring->clean_ptr < ring->end_ptr))
1870 ring->clean_ptr++;
1871 else
1872 ring->clean_ptr = ring->cb_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001873 }
1874
Petri Gynther66d06752015-03-04 14:30:01 -08001875 ring->free_bds += txbds_processed;
Doug Bergerc4d453d2017-03-13 17:41:38 -07001876 ring->c_index = c_index;
Petri Gynther66d06752015-03-04 14:30:01 -08001877
Florian Fainelli37a30b42017-03-16 10:27:08 -07001878 ring->packets += pkts_compl;
1879 ring->bytes += bytes_compl;
Petri Gynther55868122016-03-24 11:27:20 -07001880
Doug Berger6d22fe12017-03-09 16:58:50 -08001881 netdev_tx_completed_queue(netdev_get_tx_queue(dev, ring->queue),
1882 pkts_compl, bytes_compl);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001883
Doug Bergerc4d453d2017-03-13 17:41:38 -07001884 return txbds_processed;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001885}
1886
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001887static unsigned int bcmgenet_tx_reclaim(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001888 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001889{
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001890 unsigned int released;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001891
Doug Bergerb0447ec2017-10-25 15:04:17 -07001892 spin_lock_bh(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001893 released = __bcmgenet_tx_reclaim(dev, ring);
Doug Bergerb0447ec2017-10-25 15:04:17 -07001894 spin_unlock_bh(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001895
1896 return released;
1897}
1898
1899static int bcmgenet_tx_poll(struct napi_struct *napi, int budget)
1900{
1901 struct bcmgenet_tx_ring *ring =
1902 container_of(napi, struct bcmgenet_tx_ring, napi);
1903 unsigned int work_done = 0;
Doug Berger6d22fe12017-03-09 16:58:50 -08001904 struct netdev_queue *txq;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001905
Doug Bergerb0447ec2017-10-25 15:04:17 -07001906 spin_lock(&ring->lock);
Doug Berger6d22fe12017-03-09 16:58:50 -08001907 work_done = __bcmgenet_tx_reclaim(ring->priv->dev, ring);
1908 if (ring->free_bds > (MAX_SKB_FRAGS + 1)) {
1909 txq = netdev_get_tx_queue(ring->priv->dev, ring->queue);
1910 netif_tx_wake_queue(txq);
1911 }
Doug Bergerb0447ec2017-10-25 15:04:17 -07001912 spin_unlock(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001913
1914 if (work_done == 0) {
1915 napi_complete(napi);
Petri Gynther9dbac282015-03-25 12:35:10 -07001916 ring->int_enable(ring);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001917
1918 return 0;
1919 }
1920
1921 return budget;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001922}
1923
1924static void bcmgenet_tx_reclaim_all(struct net_device *dev)
1925{
1926 struct bcmgenet_priv *priv = netdev_priv(dev);
1927 int i;
1928
1929 if (netif_is_multiqueue(dev)) {
1930 for (i = 0; i < priv->hw_params->tx_queues; i++)
1931 bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
1932 }
1933
1934 bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
1935}
1936
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001937/* Reallocate the SKB to put enough headroom in front of it and insert
1938 * the transmit checksum offsets in the descriptors
1939 */
Doug Berger9a9ba2a2020-03-17 17:05:36 -07001940static struct sk_buff *bcmgenet_add_tsb(struct net_device *dev,
1941 struct sk_buff *skb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001942{
Doug Bergerf1af17c2019-12-17 16:51:15 -08001943 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001944 struct status_64 *status = NULL;
1945 struct sk_buff *new_skb;
1946 u16 offset;
1947 u8 ip_proto;
Florian Fainelli6f894212018-04-02 15:58:55 -07001948 __be16 ip_ver;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001949 u32 tx_csum_info;
1950
1951 if (unlikely(skb_headroom(skb) < sizeof(*status))) {
1952 /* If 64 byte status block enabled, must make sure skb has
1953 * enough headroom for us to insert 64B status block.
1954 */
1955 new_skb = skb_realloc_headroom(skb, sizeof(*status));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001956 if (!new_skb) {
Doug Bergere3fa8582019-12-17 16:51:14 -08001957 dev_kfree_skb_any(skb);
Doug Bergerf1af17c2019-12-17 16:51:15 -08001958 priv->mib.tx_realloc_tsb_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001959 dev->stats.tx_dropped++;
Petri Gyntherbc233332014-10-01 11:30:01 -07001960 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001961 }
Doug Bergere3fa8582019-12-17 16:51:14 -08001962 dev_consume_skb_any(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001963 skb = new_skb;
Doug Bergerf1af17c2019-12-17 16:51:15 -08001964 priv->mib.tx_realloc_tsb++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001965 }
1966
1967 skb_push(skb, sizeof(*status));
1968 status = (struct status_64 *)skb->data;
1969
1970 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Florian Fainelli6f894212018-04-02 15:58:55 -07001971 ip_ver = skb->protocol;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001972 switch (ip_ver) {
Florian Fainelli6f894212018-04-02 15:58:55 -07001973 case htons(ETH_P_IP):
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001974 ip_proto = ip_hdr(skb)->protocol;
1975 break;
Florian Fainelli6f894212018-04-02 15:58:55 -07001976 case htons(ETH_P_IPV6):
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001977 ip_proto = ipv6_hdr(skb)->nexthdr;
1978 break;
1979 default:
Doug Bergerdd8e9112019-12-17 16:51:09 -08001980 /* don't use UDP flag */
1981 ip_proto = 0;
1982 break;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001983 }
1984
1985 offset = skb_checksum_start_offset(skb) - sizeof(*status);
1986 tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
Doug Bergerdd8e9112019-12-17 16:51:09 -08001987 (offset + skb->csum_offset) |
1988 STATUS_TX_CSUM_LV;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001989
Doug Bergerdd8e9112019-12-17 16:51:09 -08001990 /* Set the special UDP flag for UDP */
1991 if (ip_proto == IPPROTO_UDP)
1992 tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001993
1994 status->tx_csum_info = tx_csum_info;
1995 }
1996
Petri Gyntherbc233332014-10-01 11:30:01 -07001997 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001998}
1999
2000static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
2001{
2002 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Berger876dbad2017-07-14 16:12:09 -07002003 struct device *kdev = &priv->pdev->dev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002004 struct bcmgenet_tx_ring *ring = NULL;
Doug Berger876dbad2017-07-14 16:12:09 -07002005 struct enet_cb *tx_cb_ptr;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07002006 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002007 int nr_frags, index;
Doug Berger876dbad2017-07-14 16:12:09 -07002008 dma_addr_t mapping;
2009 unsigned int size;
2010 skb_frag_t *frag;
2011 u32 len_stat;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002012 int ret;
2013 int i;
2014
2015 index = skb_get_queue_mapping(skb);
2016 /* Mapping strategy:
2017 * queue_mapping = 0, unclassified, packet xmited through ring16
2018 * queue_mapping = 1, goes to ring 0. (highest priority queue
2019 * queue_mapping = 2, goes to ring 1.
2020 * queue_mapping = 3, goes to ring 2.
2021 * queue_mapping = 4, goes to ring 3.
2022 */
2023 if (index == 0)
2024 index = DESC_INDEX;
2025 else
2026 index -= 1;
2027
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002028 ring = &priv->tx_rings[index];
Florian Fainellib2cde2c2014-03-20 10:53:23 -07002029 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002030
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07002031 nr_frags = skb_shinfo(skb)->nr_frags;
2032
Doug Bergerb0447ec2017-10-25 15:04:17 -07002033 spin_lock(&ring->lock);
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07002034 if (ring->free_bds <= (nr_frags + 1)) {
2035 if (!netif_tx_queue_stopped(txq)) {
2036 netif_tx_stop_queue(txq);
2037 netdev_err(dev,
2038 "%s: tx ring %d full when queue %d awake\n",
2039 __func__, index, ring->queue);
2040 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002041 ret = NETDEV_TX_BUSY;
2042 goto out;
2043 }
2044
Petri Gynther55868122016-03-24 11:27:20 -07002045 /* Retain how many bytes will be sent on the wire, without TSB inserted
2046 * by transmit checksum offload
2047 */
2048 GENET_CB(skb)->bytes_sent = skb->len;
2049
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002050 /* add the Transmit Status Block */
2051 skb = bcmgenet_add_tsb(dev, skb);
2052 if (!skb) {
2053 ret = NETDEV_TX_OK;
2054 goto out;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002055 }
2056
Doug Berger876dbad2017-07-14 16:12:09 -07002057 for (i = 0; i <= nr_frags; i++) {
2058 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002059
Gustavo A. R. Silva4fa112f2017-10-26 07:16:01 -05002060 BUG_ON(!tx_cb_ptr);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002061
Doug Berger876dbad2017-07-14 16:12:09 -07002062 if (!i) {
2063 /* Transmit single SKB or head of fragment list */
Doug Bergerf48bed12017-07-14 16:12:10 -07002064 GENET_CB(skb)->first_cb = tx_cb_ptr;
Doug Berger876dbad2017-07-14 16:12:09 -07002065 size = skb_headlen(skb);
2066 mapping = dma_map_single(kdev, skb->data, size,
2067 DMA_TO_DEVICE);
2068 } else {
2069 /* xmit fragment */
Doug Berger876dbad2017-07-14 16:12:09 -07002070 frag = &skb_shinfo(skb)->frags[i - 1];
2071 size = skb_frag_size(frag);
2072 mapping = skb_frag_dma_map(kdev, frag, 0, size,
2073 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002074 }
Doug Berger876dbad2017-07-14 16:12:09 -07002075
2076 ret = dma_mapping_error(kdev, mapping);
2077 if (ret) {
2078 priv->mib.tx_dma_failed++;
2079 netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
2080 ret = NETDEV_TX_OK;
2081 goto out_unmap_frags;
2082 }
2083 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
2084 dma_unmap_len_set(tx_cb_ptr, dma_len, size);
2085
Doug Bergerf48bed12017-07-14 16:12:10 -07002086 tx_cb_ptr->skb = skb;
2087
Doug Berger876dbad2017-07-14 16:12:09 -07002088 len_stat = (size << DMA_BUFLENGTH_SHIFT) |
2089 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT);
2090
Doug Berger20d1f2d2020-06-24 18:14:55 -07002091 /* Note: if we ever change from DMA_TX_APPEND_CRC below we
2092 * will need to restore software padding of "runt" packets
2093 */
Doug Berger876dbad2017-07-14 16:12:09 -07002094 if (!i) {
2095 len_stat |= DMA_TX_APPEND_CRC | DMA_SOP;
2096 if (skb->ip_summed == CHECKSUM_PARTIAL)
2097 len_stat |= DMA_TX_DO_CSUM;
2098 }
2099 if (i == nr_frags)
2100 len_stat |= DMA_EOP;
2101
2102 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, len_stat);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002103 }
2104
Doug Bergerf48bed12017-07-14 16:12:10 -07002105 GENET_CB(skb)->last_cb = tx_cb_ptr;
Florian Fainellid03825f2014-03-20 10:53:21 -07002106 skb_tx_timestamp(skb);
2107
Florian Fainelliae67bf02015-03-13 12:11:06 -07002108 /* Decrement total BD count and advance our write pointer */
2109 ring->free_bds -= nr_frags + 1;
2110 ring->prod_index += nr_frags + 1;
2111 ring->prod_index &= DMA_P_INDEX_MASK;
2112
Petri Gynthere178c8c2016-04-09 00:20:36 -07002113 netdev_tx_sent_queue(txq, GENET_CB(skb)->bytes_sent);
2114
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002115 if (ring->free_bds <= (MAX_SKB_FRAGS + 1))
Florian Fainellib2cde2c2014-03-20 10:53:23 -07002116 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002117
Florian Westphal6b16f9e2019-04-01 16:42:14 +02002118 if (!netdev_xmit_more() || netif_xmit_stopped(txq))
Florian Fainelliddd0ca52015-03-13 12:11:07 -07002119 /* Packets are ready, update producer index */
2120 bcmgenet_tdma_ring_writel(priv, ring->index,
2121 ring->prod_index, TDMA_PROD_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002122out:
Doug Bergerb0447ec2017-10-25 15:04:17 -07002123 spin_unlock(&ring->lock);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002124
2125 return ret;
Doug Berger876dbad2017-07-14 16:12:09 -07002126
2127out_unmap_frags:
2128 /* Back up for failed control block mapping */
2129 bcmgenet_put_txcb(priv, ring);
2130
2131 /* Unmap successfully mapped control blocks */
2132 while (i-- > 0) {
2133 tx_cb_ptr = bcmgenet_put_txcb(priv, ring);
Doug Bergerf48bed12017-07-14 16:12:10 -07002134 bcmgenet_free_tx_cb(kdev, tx_cb_ptr);
Doug Berger876dbad2017-07-14 16:12:09 -07002135 }
2136
2137 dev_kfree_skb(skb);
2138 goto out;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002139}
2140
Petri Gyntherd6707be2015-03-12 15:48:00 -07002141static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
2142 struct enet_cb *cb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002143{
2144 struct device *kdev = &priv->pdev->dev;
2145 struct sk_buff *skb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002146 struct sk_buff *rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002147 dma_addr_t mapping;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002148
Petri Gyntherd6707be2015-03-12 15:48:00 -07002149 /* Allocate a new Rx skb */
Doug Bergerecaeceb2020-04-23 16:02:11 -07002150 skb = __netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT,
2151 GFP_ATOMIC | __GFP_NOWARN);
Petri Gyntherd6707be2015-03-12 15:48:00 -07002152 if (!skb) {
2153 priv->mib.alloc_rx_buff_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002154 netif_err(priv, rx_err, priv->dev,
Petri Gyntherd6707be2015-03-12 15:48:00 -07002155 "%s: Rx skb allocation failed\n", __func__);
2156 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002157 }
2158
Petri Gyntherd6707be2015-03-12 15:48:00 -07002159 /* DMA-map the new Rx skb */
2160 mapping = dma_map_single(kdev, skb->data, priv->rx_buf_len,
2161 DMA_FROM_DEVICE);
2162 if (dma_mapping_error(kdev, mapping)) {
2163 priv->mib.rx_dma_failed++;
2164 dev_kfree_skb_any(skb);
2165 netif_err(priv, rx_err, priv->dev,
2166 "%s: Rx skb DMA mapping failed\n", __func__);
2167 return NULL;
2168 }
2169
2170 /* Grab the current Rx skb from the ring and DMA-unmap it */
Doug Bergerf48bed12017-07-14 16:12:10 -07002171 rx_skb = bcmgenet_free_rx_cb(kdev, cb);
Petri Gyntherd6707be2015-03-12 15:48:00 -07002172
2173 /* Put the new Rx skb on the ring */
2174 cb->skb = skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002175 dma_unmap_addr_set(cb, dma_addr, mapping);
Doug Bergerf48bed12017-07-14 16:12:10 -07002176 dma_unmap_len_set(cb, dma_len, priv->rx_buf_len);
Petri Gynther8ac467e2015-03-09 13:40:00 -07002177 dmadesc_set_addr(priv, cb->bd_addr, mapping);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002178
Petri Gyntherd6707be2015-03-12 15:48:00 -07002179 /* Return the current Rx skb to caller */
2180 return rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002181}
2182
2183/* bcmgenet_desc_rx - descriptor based rx process.
2184 * this could be called from bottom half, or from NAPI polling method.
2185 */
Petri Gynther4055eae2015-03-25 12:35:16 -07002186static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002187 unsigned int budget)
2188{
Petri Gynther4055eae2015-03-25 12:35:16 -07002189 struct bcmgenet_priv *priv = ring->priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002190 struct net_device *dev = priv->dev;
2191 struct enet_cb *cb;
2192 struct sk_buff *skb;
2193 u32 dma_length_status;
2194 unsigned long dma_flag;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002195 int len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002196 unsigned int rxpktprocessed = 0, rxpkttoprocess;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002197 unsigned int bytes_processed = 0;
Doug Bergerd5810ca2017-03-13 17:41:37 -07002198 unsigned int p_index, mask;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07002199 unsigned int discards;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002200
Doug Bergerd5810ca2017-03-13 17:41:37 -07002201 /* Clear status before servicing to reduce spurious interrupts */
2202 if (ring->index == DESC_INDEX) {
2203 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_RXDMA_DONE,
2204 INTRL2_CPU_CLEAR);
2205 } else {
2206 mask = 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index);
2207 bcmgenet_intrl2_1_writel(priv,
2208 mask,
2209 INTRL2_CPU_CLEAR);
2210 }
2211
Petri Gynther4055eae2015-03-25 12:35:16 -07002212 p_index = bcmgenet_rdma_ring_readl(priv, ring->index, RDMA_PROD_INDEX);
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07002213
2214 discards = (p_index >> DMA_P_INDEX_DISCARD_CNT_SHIFT) &
2215 DMA_P_INDEX_DISCARD_CNT_MASK;
2216 if (discards > ring->old_discards) {
2217 discards = discards - ring->old_discards;
Florian Fainelli37a30b42017-03-16 10:27:08 -07002218 ring->errors += discards;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07002219 ring->old_discards += discards;
2220
2221 /* Clear HW register when we reach 75% of maximum 0xFFFF */
2222 if (ring->old_discards >= 0xC000) {
2223 ring->old_discards = 0;
Petri Gynther4055eae2015-03-25 12:35:16 -07002224 bcmgenet_rdma_ring_writel(priv, ring->index, 0,
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07002225 RDMA_PROD_INDEX);
2226 }
2227 }
2228
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002229 p_index &= DMA_P_INDEX_MASK;
Doug Bergerc298ede2017-03-13 17:41:33 -07002230 rxpkttoprocess = (p_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002231
2232 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002233 "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002234
2235 while ((rxpktprocessed < rxpkttoprocess) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07002236 (rxpktprocessed < budget)) {
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002237 struct status_64 *status;
2238 __be16 rx_csum;
2239
Petri Gynther8ac467e2015-03-09 13:40:00 -07002240 cb = &priv->rx_cbs[ring->read_ptr];
Petri Gyntherd6707be2015-03-12 15:48:00 -07002241 skb = bcmgenet_rx_refill(priv, cb);
Florian Fainellib629be52014-09-08 11:37:52 -07002242
Florian Fainellib629be52014-09-08 11:37:52 -07002243 if (unlikely(!skb)) {
Florian Fainelli37a30b42017-03-16 10:27:08 -07002244 ring->dropped++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002245 goto next;
Florian Fainellib629be52014-09-08 11:37:52 -07002246 }
2247
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002248 status = (struct status_64 *)skb->data;
2249 dma_length_status = status->length_status;
2250 if (dev->features & NETIF_F_RXCSUM) {
Doug Berger81015532019-12-17 16:51:10 -08002251 rx_csum = (__force __be16)(status->rx_csum & 0xffff);
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002252 skb->csum = (__force __wsum)ntohs(rx_csum);
2253 skb->ip_summed = CHECKSUM_COMPLETE;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002254 }
2255
2256 /* DMA flags and length are still valid no matter how
2257 * we got the Receive Status Vector (64B RSB or register)
2258 */
2259 dma_flag = dma_length_status & 0xffff;
2260 len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
2261
2262 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002263 "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
Petri Gynther8ac467e2015-03-09 13:40:00 -07002264 __func__, p_index, ring->c_index,
2265 ring->read_ptr, dma_length_status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002266
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002267 if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
2268 netif_err(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002269 "dropping fragmented packet!\n");
Florian Fainelli37a30b42017-03-16 10:27:08 -07002270 ring->errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002271 dev_kfree_skb_any(skb);
2272 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002273 }
Petri Gyntherd6707be2015-03-12 15:48:00 -07002274
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002275 /* report errors */
2276 if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
2277 DMA_RX_OV |
2278 DMA_RX_NO |
2279 DMA_RX_LG |
2280 DMA_RX_RXER))) {
2281 netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07002282 (unsigned int)dma_flag);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002283 if (dma_flag & DMA_RX_CRC_ERROR)
2284 dev->stats.rx_crc_errors++;
2285 if (dma_flag & DMA_RX_OV)
2286 dev->stats.rx_over_errors++;
2287 if (dma_flag & DMA_RX_NO)
2288 dev->stats.rx_frame_errors++;
2289 if (dma_flag & DMA_RX_LG)
2290 dev->stats.rx_length_errors++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002291 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002292 dev_kfree_skb_any(skb);
2293 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002294 } /* error packet */
2295
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002296 skb_put(skb, len);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002297
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002298 /* remove RSB and hardware 2bytes added for IP alignment */
2299 skb_pull(skb, 66);
2300 len -= 66;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002301
2302 if (priv->crc_fwd_en) {
2303 skb_trim(skb, len - ETH_FCS_LEN);
2304 len -= ETH_FCS_LEN;
2305 }
2306
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002307 bytes_processed += len;
2308
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002309 /*Finish setting up the received SKB and send it to the kernel*/
2310 skb->protocol = eth_type_trans(skb, priv->dev);
Florian Fainelli37a30b42017-03-16 10:27:08 -07002311 ring->packets++;
2312 ring->bytes += len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002313 if (dma_flag & DMA_RX_MULT)
2314 dev->stats.multicast++;
2315
2316 /* Notify kernel */
Petri Gynther4055eae2015-03-25 12:35:16 -07002317 napi_gro_receive(&ring->napi, skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002318 netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
2319
Petri Gyntherd6707be2015-03-12 15:48:00 -07002320next:
Florian Fainellicf377d82014-10-10 10:51:52 -07002321 rxpktprocessed++;
Petri Gynther8ac467e2015-03-09 13:40:00 -07002322 if (likely(ring->read_ptr < ring->end_ptr))
2323 ring->read_ptr++;
2324 else
2325 ring->read_ptr = ring->cb_ptr;
2326
2327 ring->c_index = (ring->c_index + 1) & DMA_C_INDEX_MASK;
Petri Gynther4055eae2015-03-25 12:35:16 -07002328 bcmgenet_rdma_ring_writel(priv, ring->index, ring->c_index, RDMA_CONS_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002329 }
2330
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002331 ring->dim.bytes = bytes_processed;
2332 ring->dim.packets = rxpktprocessed;
2333
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002334 return rxpktprocessed;
2335}
2336
Petri Gynther3ab11332015-03-25 12:35:15 -07002337/* Rx NAPI polling method */
2338static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
2339{
Petri Gynther4055eae2015-03-25 12:35:16 -07002340 struct bcmgenet_rx_ring *ring = container_of(napi,
2341 struct bcmgenet_rx_ring, napi);
Yamin Friedmanf06d0ca2019-07-23 10:22:47 +03002342 struct dim_sample dim_sample = {};
Petri Gynther3ab11332015-03-25 12:35:15 -07002343 unsigned int work_done;
2344
Petri Gynther4055eae2015-03-25 12:35:16 -07002345 work_done = bcmgenet_desc_rx(ring, budget);
Petri Gynther3ab11332015-03-25 12:35:15 -07002346
2347 if (work_done < budget) {
Eric Dumazeteb96ce02016-04-08 22:06:40 -07002348 napi_complete_done(napi, work_done);
Petri Gynther4055eae2015-03-25 12:35:16 -07002349 ring->int_enable(ring);
Petri Gynther3ab11332015-03-25 12:35:15 -07002350 }
2351
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002352 if (ring->dim.use_dim) {
Tal Gilboa8960b382019-01-31 16:44:48 +02002353 dim_update_sample(ring->dim.event_ctr, ring->dim.packets,
2354 ring->dim.bytes, &dim_sample);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002355 net_dim(&ring->dim.dim, dim_sample);
2356 }
2357
Petri Gynther3ab11332015-03-25 12:35:15 -07002358 return work_done;
2359}
2360
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002361static void bcmgenet_dim_work(struct work_struct *work)
2362{
Tal Gilboa8960b382019-01-31 16:44:48 +02002363 struct dim *dim = container_of(work, struct dim, work);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002364 struct bcmgenet_net_dim *ndim =
2365 container_of(dim, struct bcmgenet_net_dim, dim);
2366 struct bcmgenet_rx_ring *ring =
2367 container_of(ndim, struct bcmgenet_rx_ring, dim);
Tal Gilboa8960b382019-01-31 16:44:48 +02002368 struct dim_cq_moder cur_profile =
Tal Gilboa026a8072018-04-24 13:36:01 +03002369 net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002370
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002371 bcmgenet_set_rx_coalesce(ring, cur_profile.usec, cur_profile.pkts);
Tal Gilboac002bd52018-11-05 12:07:52 +02002372 dim->state = DIM_START_MEASURE;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002373}
2374
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002375/* Assign skb to RX DMA descriptor. */
Petri Gynther8ac467e2015-03-09 13:40:00 -07002376static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv,
2377 struct bcmgenet_rx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002378{
2379 struct enet_cb *cb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002380 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002381 int i;
2382
Petri Gynther8ac467e2015-03-09 13:40:00 -07002383 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002384
2385 /* loop here for each buffer needing assign */
Petri Gynther8ac467e2015-03-09 13:40:00 -07002386 for (i = 0; i < ring->size; i++) {
2387 cb = ring->cbs + i;
Petri Gyntherd6707be2015-03-12 15:48:00 -07002388 skb = bcmgenet_rx_refill(priv, cb);
2389 if (skb)
Florian Fainellid4fec852017-08-24 15:56:29 -07002390 dev_consume_skb_any(skb);
Petri Gyntherd6707be2015-03-12 15:48:00 -07002391 if (!cb->skb)
2392 return -ENOMEM;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002393 }
2394
Petri Gyntherd6707be2015-03-12 15:48:00 -07002395 return 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002396}
2397
2398static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
2399{
Doug Bergerf48bed12017-07-14 16:12:10 -07002400 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002401 struct enet_cb *cb;
2402 int i;
2403
2404 for (i = 0; i < priv->num_rx_bds; i++) {
2405 cb = &priv->rx_cbs[i];
2406
Doug Bergerf48bed12017-07-14 16:12:10 -07002407 skb = bcmgenet_free_rx_cb(&priv->pdev->dev, cb);
2408 if (skb)
Florian Fainellid4fec852017-08-24 15:56:29 -07002409 dev_consume_skb_any(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002410 }
2411}
2412
Florian Fainellic91b7f62014-07-23 10:42:12 -07002413static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable)
Florian Fainellie29585b2014-07-21 15:29:20 -07002414{
2415 u32 reg;
2416
2417 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
Doug Berger88f6c8b2020-03-16 14:44:56 -07002418 if (reg & CMD_SW_RESET)
2419 return;
Florian Fainellie29585b2014-07-21 15:29:20 -07002420 if (enable)
2421 reg |= mask;
2422 else
2423 reg &= ~mask;
2424 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
2425
2426 /* UniMAC stops on a packet boundary, wait for a full-size packet
2427 * to be processed
2428 */
2429 if (enable == 0)
2430 usleep_range(1000, 2000);
2431}
2432
Doug Berger28c2d1a2017-10-25 15:04:13 -07002433static void reset_umac(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002434{
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002435 /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
2436 bcmgenet_rbuf_ctrl_set(priv, 0);
2437 udelay(10);
2438
Doug Berger88f6c8b2020-03-16 14:44:56 -07002439 /* issue soft reset and disable MAC while updating its registers */
2440 bcmgenet_umac_writel(priv, CMD_SW_RESET, UMAC_CMD);
Doug Berger612eb1c2020-03-16 14:44:55 -07002441 udelay(2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002442}
2443
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002444static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
2445{
2446 /* Mask all interrupts.*/
2447 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
2448 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002449 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
2450 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002451}
2452
Florian Fainelli37850e32015-10-17 14:22:46 -07002453static void bcmgenet_link_intr_enable(struct bcmgenet_priv *priv)
2454{
2455 u32 int0_enable = 0;
2456
2457 /* Monitor cable plug/unplugged event for internal PHY, external PHY
2458 * and MoCA PHY
2459 */
2460 if (priv->internal_phy) {
2461 int0_enable |= UMAC_IRQ_LINK_EVENT;
Doug Berger25382b92019-10-16 16:06:32 -07002462 if (GENET_IS_V1(priv) || GENET_IS_V2(priv) || GENET_IS_V3(priv))
2463 int0_enable |= UMAC_IRQ_PHY_DET_R;
Florian Fainelli37850e32015-10-17 14:22:46 -07002464 } else if (priv->ext_phy) {
2465 int0_enable |= UMAC_IRQ_LINK_EVENT;
2466 } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
2467 if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
2468 int0_enable |= UMAC_IRQ_LINK_EVENT;
2469 }
2470 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
2471}
2472
Doug Berger28c2d1a2017-10-25 15:04:13 -07002473static void init_umac(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002474{
2475 struct device *kdev = &priv->pdev->dev;
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002476 u32 reg;
2477 u32 int0_enable = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002478
2479 dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
2480
Doug Berger28c2d1a2017-10-25 15:04:13 -07002481 reset_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002482
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002483 /* clear tx/rx counter */
2484 bcmgenet_umac_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002485 MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
2486 UMAC_MIB_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002487 bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
2488
2489 bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
2490
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002491 /* init tx registers, enable TSB */
2492 reg = bcmgenet_tbuf_ctrl_get(priv);
2493 reg |= TBUF_64B_EN;
2494 bcmgenet_tbuf_ctrl_set(priv, reg);
2495
2496 /* init rx registers, enable ip header optimization and RSB */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002497 reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002498 reg |= RBUF_ALIGN_2B | RBUF_64B_EN;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002499 bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
2500
Doug Berger9a9ba2a2020-03-17 17:05:36 -07002501 /* enable rx checksumming */
2502 reg = bcmgenet_rbuf_readl(priv, RBUF_CHK_CTRL);
2503 reg |= RBUF_RXCHK_EN | RBUF_L3_PARSE_DIS;
2504 /* If UniMAC forwards CRC, we need to skip over it to get
2505 * a valid CHK bit to be set in the per-packet status word
2506 */
2507 if (priv->crc_fwd_en)
2508 reg |= RBUF_SKIP_FCS;
2509 else
2510 reg &= ~RBUF_SKIP_FCS;
2511 bcmgenet_rbuf_writel(priv, reg, RBUF_CHK_CTRL);
2512
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002513 if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
2514 bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
2515
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002516 bcmgenet_intr_disable(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002517
Florian Fainelli37850e32015-10-17 14:22:46 -07002518 /* Configure backpressure vectors for MoCA */
2519 if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002520 reg = bcmgenet_bp_mc_get(priv);
2521 reg |= BIT(priv->hw_params->bp_in_en_shift);
2522
2523 /* bp_mask: back pressure mask */
2524 if (netif_is_multiqueue(priv->dev))
2525 reg |= priv->hw_params->bp_in_mask;
2526 else
2527 reg &= ~priv->hw_params->bp_in_mask;
2528 bcmgenet_bp_mc_set(priv, reg);
2529 }
2530
2531 /* Enable MDIO interrupts on GENET v3+ */
2532 if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002533 int0_enable |= (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002534
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002535 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002536
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002537 dev_dbg(kdev, "done init umac\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002538}
2539
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002540static void bcmgenet_init_dim(struct bcmgenet_rx_ring *ring,
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002541 void (*cb)(struct work_struct *work))
2542{
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002543 struct bcmgenet_net_dim *dim = &ring->dim;
2544
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002545 INIT_WORK(&dim->dim.work, cb);
Tal Gilboac002bd52018-11-05 12:07:52 +02002546 dim->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002547 dim->event_ctr = 0;
2548 dim->packets = 0;
2549 dim->bytes = 0;
2550}
2551
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002552static void bcmgenet_init_rx_coalesce(struct bcmgenet_rx_ring *ring)
2553{
2554 struct bcmgenet_net_dim *dim = &ring->dim;
Tal Gilboa8960b382019-01-31 16:44:48 +02002555 struct dim_cq_moder moder;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002556 u32 usecs, pkts;
2557
2558 usecs = ring->rx_coalesce_usecs;
2559 pkts = ring->rx_max_coalesced_frames;
2560
2561 /* If DIM was enabled, re-apply default parameters */
2562 if (dim->use_dim) {
Tal Gilboa026a8072018-04-24 13:36:01 +03002563 moder = net_dim_get_def_rx_moderation(dim->dim.mode);
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002564 usecs = moder.usec;
2565 pkts = moder.pkts;
2566 }
2567
2568 bcmgenet_set_rx_coalesce(ring, usecs, pkts);
2569}
2570
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002571/* Initialize a Tx ring along with corresponding hardware registers */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002572static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
2573 unsigned int index, unsigned int size,
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002574 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002575{
2576 struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
2577 u32 words_per_bd = WORDS_PER_BD(priv);
2578 u32 flow_period_val = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002579
2580 spin_lock_init(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002581 ring->priv = priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002582 ring->index = index;
2583 if (index == DESC_INDEX) {
2584 ring->queue = 0;
2585 ring->int_enable = bcmgenet_tx_ring16_int_enable;
2586 ring->int_disable = bcmgenet_tx_ring16_int_disable;
2587 } else {
2588 ring->queue = index + 1;
2589 ring->int_enable = bcmgenet_tx_ring_int_enable;
2590 ring->int_disable = bcmgenet_tx_ring_int_disable;
2591 }
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002592 ring->cbs = priv->tx_cbs + start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002593 ring->size = size;
Petri Gynther66d06752015-03-04 14:30:01 -08002594 ring->clean_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002595 ring->c_index = 0;
2596 ring->free_bds = size;
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002597 ring->write_ptr = start_ptr;
2598 ring->cb_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002599 ring->end_ptr = end_ptr - 1;
2600 ring->prod_index = 0;
2601
2602 /* Set flow period for ring != 16 */
2603 if (index != DESC_INDEX)
2604 flow_period_val = ENET_MAX_MTU_SIZE << 16;
2605
2606 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
2607 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
2608 bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
2609 /* Disable rate control for now */
2610 bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002611 TDMA_FLOW_PERIOD);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002612 bcmgenet_tdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002613 ((size << DMA_RING_SIZE_SHIFT) |
2614 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002615
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002616 /* Set start and end address, read and write pointers */
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002617 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002618 DMA_START_ADDR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002619 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002620 TDMA_READ_PTR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002621 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002622 TDMA_WRITE_PTR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002623 bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002624 DMA_END_ADDR);
Doug Berger75879352017-10-25 15:04:14 -07002625
2626 /* Initialize Tx NAPI */
Florian Fainelli148965d2020-01-23 09:49:34 -08002627 netif_tx_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll,
2628 NAPI_POLL_WEIGHT);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002629}
2630
2631/* Initialize a RDMA ring */
2632static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
Petri Gynther8ac467e2015-03-09 13:40:00 -07002633 unsigned int index, unsigned int size,
2634 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002635{
Petri Gynther8ac467e2015-03-09 13:40:00 -07002636 struct bcmgenet_rx_ring *ring = &priv->rx_rings[index];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002637 u32 words_per_bd = WORDS_PER_BD(priv);
2638 int ret;
2639
Petri Gynther4055eae2015-03-25 12:35:16 -07002640 ring->priv = priv;
Petri Gynther8ac467e2015-03-09 13:40:00 -07002641 ring->index = index;
Petri Gynther4055eae2015-03-25 12:35:16 -07002642 if (index == DESC_INDEX) {
2643 ring->int_enable = bcmgenet_rx_ring16_int_enable;
2644 ring->int_disable = bcmgenet_rx_ring16_int_disable;
2645 } else {
2646 ring->int_enable = bcmgenet_rx_ring_int_enable;
2647 ring->int_disable = bcmgenet_rx_ring_int_disable;
2648 }
Petri Gynther8ac467e2015-03-09 13:40:00 -07002649 ring->cbs = priv->rx_cbs + start_ptr;
2650 ring->size = size;
2651 ring->c_index = 0;
2652 ring->read_ptr = start_ptr;
2653 ring->cb_ptr = start_ptr;
2654 ring->end_ptr = end_ptr - 1;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002655
Petri Gynther8ac467e2015-03-09 13:40:00 -07002656 ret = bcmgenet_alloc_rx_buffers(priv, ring);
2657 if (ret)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002658 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002659
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002660 bcmgenet_init_dim(ring, bcmgenet_dim_work);
2661 bcmgenet_init_rx_coalesce(ring);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002662
Doug Berger75879352017-10-25 15:04:14 -07002663 /* Initialize Rx NAPI */
2664 netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll,
2665 NAPI_POLL_WEIGHT);
2666
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002667 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
2668 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
2669 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002670 ((size << DMA_RING_SIZE_SHIFT) |
2671 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002672 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002673 (DMA_FC_THRESH_LO <<
2674 DMA_XOFF_THRESHOLD_SHIFT) |
2675 DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
Petri Gynther6f5a2722015-03-06 13:45:00 -08002676
2677 /* Set start and end address, read and write pointers */
Petri Gynther8ac467e2015-03-09 13:40:00 -07002678 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2679 DMA_START_ADDR);
2680 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2681 RDMA_READ_PTR);
2682 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2683 RDMA_WRITE_PTR);
2684 bcmgenet_rdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Petri Gynther6f5a2722015-03-06 13:45:00 -08002685 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002686
2687 return ret;
2688}
2689
Petri Gynthere2aadb42015-03-25 12:35:14 -07002690static void bcmgenet_enable_tx_napi(struct bcmgenet_priv *priv)
2691{
2692 unsigned int i;
2693 struct bcmgenet_tx_ring *ring;
2694
2695 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2696 ring = &priv->tx_rings[i];
2697 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002698 ring->int_enable(ring);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002699 }
2700
2701 ring = &priv->tx_rings[DESC_INDEX];
2702 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002703 ring->int_enable(ring);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002704}
2705
2706static void bcmgenet_disable_tx_napi(struct bcmgenet_priv *priv)
2707{
2708 unsigned int i;
2709 struct bcmgenet_tx_ring *ring;
2710
2711 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2712 ring = &priv->tx_rings[i];
2713 napi_disable(&ring->napi);
2714 }
2715
2716 ring = &priv->tx_rings[DESC_INDEX];
2717 napi_disable(&ring->napi);
2718}
2719
2720static void bcmgenet_fini_tx_napi(struct bcmgenet_priv *priv)
2721{
2722 unsigned int i;
2723 struct bcmgenet_tx_ring *ring;
2724
2725 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2726 ring = &priv->tx_rings[i];
2727 netif_napi_del(&ring->napi);
2728 }
2729
2730 ring = &priv->tx_rings[DESC_INDEX];
2731 netif_napi_del(&ring->napi);
2732}
2733
Petri Gynther16c6d662015-02-23 11:00:45 -08002734/* Initialize Tx queues
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002735 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002736 * Queues 0-3 are priority-based, each one has 32 descriptors,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002737 * with queue 0 being the highest priority queue.
2738 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002739 * Queue 16 is the default Tx queue with
Petri Gynther51a966a2015-02-23 11:00:46 -08002740 * GENET_Q16_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors.
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002741 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002742 * The transmit control block pool is then partitioned as follows:
2743 * - Tx queue 0 uses tx_cbs[0..31]
2744 * - Tx queue 1 uses tx_cbs[32..63]
2745 * - Tx queue 2 uses tx_cbs[64..95]
2746 * - Tx queue 3 uses tx_cbs[96..127]
2747 * - Tx queue 16 uses tx_cbs[128..255]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002748 */
Petri Gynther16c6d662015-02-23 11:00:45 -08002749static void bcmgenet_init_tx_queues(struct net_device *dev)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002750{
2751 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther16c6d662015-02-23 11:00:45 -08002752 u32 i, dma_enable;
2753 u32 dma_ctrl, ring_cfg;
Petri Gynther37742162014-10-07 09:30:01 -07002754 u32 dma_priority[3] = {0, 0, 0};
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002755
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002756 dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
2757 dma_enable = dma_ctrl & DMA_EN;
2758 dma_ctrl &= ~DMA_EN;
2759 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
2760
Petri Gynther16c6d662015-02-23 11:00:45 -08002761 dma_ctrl = 0;
2762 ring_cfg = 0;
2763
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002764 /* Enable strict priority arbiter mode */
2765 bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
2766
Petri Gynther16c6d662015-02-23 11:00:45 -08002767 /* Initialize Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002768 for (i = 0; i < priv->hw_params->tx_queues; i++) {
Petri Gynther51a966a2015-02-23 11:00:46 -08002769 bcmgenet_init_tx_ring(priv, i, priv->hw_params->tx_bds_per_q,
2770 i * priv->hw_params->tx_bds_per_q,
2771 (i + 1) * priv->hw_params->tx_bds_per_q);
Petri Gynther16c6d662015-02-23 11:00:45 -08002772 ring_cfg |= (1 << i);
2773 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002774 dma_priority[DMA_PRIO_REG_INDEX(i)] |=
2775 ((GENET_Q0_PRIORITY + i) << DMA_PRIO_REG_SHIFT(i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002776 }
2777
Petri Gynther16c6d662015-02-23 11:00:45 -08002778 /* Initialize Tx default queue 16 */
Petri Gynther51a966a2015-02-23 11:00:46 -08002779 bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_Q16_TX_BD_CNT,
Petri Gynther16c6d662015-02-23 11:00:45 -08002780 priv->hw_params->tx_queues *
Petri Gynther51a966a2015-02-23 11:00:46 -08002781 priv->hw_params->tx_bds_per_q,
Petri Gynther16c6d662015-02-23 11:00:45 -08002782 TOTAL_DESC);
2783 ring_cfg |= (1 << DESC_INDEX);
2784 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002785 dma_priority[DMA_PRIO_REG_INDEX(DESC_INDEX)] |=
2786 ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) <<
2787 DMA_PRIO_REG_SHIFT(DESC_INDEX));
Petri Gynther16c6d662015-02-23 11:00:45 -08002788
2789 /* Set Tx queue priorities */
Petri Gynther37742162014-10-07 09:30:01 -07002790 bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
2791 bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
2792 bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);
2793
Petri Gynther16c6d662015-02-23 11:00:45 -08002794 /* Enable Tx queues */
2795 bcmgenet_tdma_writel(priv, ring_cfg, DMA_RING_CFG);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002796
Petri Gynther16c6d662015-02-23 11:00:45 -08002797 /* Enable Tx DMA */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002798 if (dma_enable)
Petri Gynther16c6d662015-02-23 11:00:45 -08002799 dma_ctrl |= DMA_EN;
2800 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002801}
2802
Petri Gynther3ab11332015-03-25 12:35:15 -07002803static void bcmgenet_enable_rx_napi(struct bcmgenet_priv *priv)
2804{
Petri Gynther4055eae2015-03-25 12:35:16 -07002805 unsigned int i;
2806 struct bcmgenet_rx_ring *ring;
2807
2808 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2809 ring = &priv->rx_rings[i];
2810 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002811 ring->int_enable(ring);
Petri Gynther4055eae2015-03-25 12:35:16 -07002812 }
2813
2814 ring = &priv->rx_rings[DESC_INDEX];
2815 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002816 ring->int_enable(ring);
Petri Gynther3ab11332015-03-25 12:35:15 -07002817}
2818
2819static void bcmgenet_disable_rx_napi(struct bcmgenet_priv *priv)
2820{
Petri Gynther4055eae2015-03-25 12:35:16 -07002821 unsigned int i;
2822 struct bcmgenet_rx_ring *ring;
2823
2824 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2825 ring = &priv->rx_rings[i];
2826 napi_disable(&ring->napi);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002827 cancel_work_sync(&ring->dim.dim.work);
Petri Gynther4055eae2015-03-25 12:35:16 -07002828 }
2829
2830 ring = &priv->rx_rings[DESC_INDEX];
2831 napi_disable(&ring->napi);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002832 cancel_work_sync(&ring->dim.dim.work);
Petri Gynther3ab11332015-03-25 12:35:15 -07002833}
2834
2835static void bcmgenet_fini_rx_napi(struct bcmgenet_priv *priv)
2836{
Petri Gynther4055eae2015-03-25 12:35:16 -07002837 unsigned int i;
2838 struct bcmgenet_rx_ring *ring;
2839
2840 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2841 ring = &priv->rx_rings[i];
2842 netif_napi_del(&ring->napi);
2843 }
2844
2845 ring = &priv->rx_rings[DESC_INDEX];
2846 netif_napi_del(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002847}
2848
Petri Gynther8ac467e2015-03-09 13:40:00 -07002849/* Initialize Rx queues
2850 *
2851 * Queues 0-15 are priority queues. Hardware Filtering Block (HFB) can be
2852 * used to direct traffic to these queues.
2853 *
2854 * Queue 16 is the default Rx queue with GENET_Q16_RX_BD_CNT descriptors.
2855 */
2856static int bcmgenet_init_rx_queues(struct net_device *dev)
2857{
2858 struct bcmgenet_priv *priv = netdev_priv(dev);
2859 u32 i;
2860 u32 dma_enable;
2861 u32 dma_ctrl;
2862 u32 ring_cfg;
2863 int ret;
2864
2865 dma_ctrl = bcmgenet_rdma_readl(priv, DMA_CTRL);
2866 dma_enable = dma_ctrl & DMA_EN;
2867 dma_ctrl &= ~DMA_EN;
2868 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2869
2870 dma_ctrl = 0;
2871 ring_cfg = 0;
2872
2873 /* Initialize Rx priority queues */
2874 for (i = 0; i < priv->hw_params->rx_queues; i++) {
2875 ret = bcmgenet_init_rx_ring(priv, i,
2876 priv->hw_params->rx_bds_per_q,
2877 i * priv->hw_params->rx_bds_per_q,
2878 (i + 1) *
2879 priv->hw_params->rx_bds_per_q);
2880 if (ret)
2881 return ret;
2882
2883 ring_cfg |= (1 << i);
2884 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2885 }
2886
2887 /* Initialize Rx default queue 16 */
2888 ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, GENET_Q16_RX_BD_CNT,
2889 priv->hw_params->rx_queues *
2890 priv->hw_params->rx_bds_per_q,
2891 TOTAL_DESC);
2892 if (ret)
2893 return ret;
2894
2895 ring_cfg |= (1 << DESC_INDEX);
2896 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
2897
2898 /* Enable rings */
2899 bcmgenet_rdma_writel(priv, ring_cfg, DMA_RING_CFG);
2900
2901 /* Configure ring as descriptor ring and re-enable DMA if enabled */
2902 if (dma_enable)
2903 dma_ctrl |= DMA_EN;
2904 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2905
2906 return 0;
2907}
2908
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002909static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
2910{
2911 int ret = 0;
2912 int timeout = 0;
2913 u32 reg;
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002914 u32 dma_ctrl;
2915 int i;
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002916
2917 /* Disable TDMA to stop add more frames in TX DMA */
2918 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2919 reg &= ~DMA_EN;
2920 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2921
2922 /* Check TDMA status register to confirm TDMA is disabled */
2923 while (timeout++ < DMA_TIMEOUT_VAL) {
2924 reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
2925 if (reg & DMA_DISABLED)
2926 break;
2927
2928 udelay(1);
2929 }
2930
2931 if (timeout == DMA_TIMEOUT_VAL) {
2932 netdev_warn(priv->dev, "Timed out while disabling TX DMA\n");
2933 ret = -ETIMEDOUT;
2934 }
2935
2936 /* Wait 10ms for packet drain in both tx and rx dma */
2937 usleep_range(10000, 20000);
2938
2939 /* Disable RDMA */
2940 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2941 reg &= ~DMA_EN;
2942 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2943
2944 timeout = 0;
2945 /* Check RDMA status register to confirm RDMA is disabled */
2946 while (timeout++ < DMA_TIMEOUT_VAL) {
2947 reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
2948 if (reg & DMA_DISABLED)
2949 break;
2950
2951 udelay(1);
2952 }
2953
2954 if (timeout == DMA_TIMEOUT_VAL) {
2955 netdev_warn(priv->dev, "Timed out while disabling RX DMA\n");
2956 ret = -ETIMEDOUT;
2957 }
2958
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002959 dma_ctrl = 0;
2960 for (i = 0; i < priv->hw_params->rx_queues; i++)
2961 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2962 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2963 reg &= ~dma_ctrl;
2964 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2965
2966 dma_ctrl = 0;
2967 for (i = 0; i < priv->hw_params->tx_queues; i++)
2968 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2969 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2970 reg &= ~dma_ctrl;
2971 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2972
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002973 return ret;
2974}
2975
Petri Gynther9abab962015-03-30 00:29:01 -07002976static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002977{
Petri Gynthere178c8c2016-04-09 00:20:36 -07002978 struct netdev_queue *txq;
Doug Bergerf48bed12017-07-14 16:12:10 -07002979 int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002980
Petri Gynther9abab962015-03-30 00:29:01 -07002981 bcmgenet_fini_rx_napi(priv);
2982 bcmgenet_fini_tx_napi(priv);
2983
Markus Elfring399e06a2019-08-22 20:02:56 +02002984 for (i = 0; i < priv->num_tx_bds; i++)
2985 dev_kfree_skb(bcmgenet_free_tx_cb(&priv->pdev->dev,
2986 priv->tx_cbs + i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002987
Petri Gynthere178c8c2016-04-09 00:20:36 -07002988 for (i = 0; i < priv->hw_params->tx_queues; i++) {
2989 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[i].queue);
2990 netdev_tx_reset_queue(txq);
2991 }
2992
2993 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[DESC_INDEX].queue);
2994 netdev_tx_reset_queue(txq);
2995
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002996 bcmgenet_free_rx_buffers(priv);
2997 kfree(priv->rx_cbs);
2998 kfree(priv->tx_cbs);
2999}
3000
3001/* init_edma: Initialize DMA control register */
3002static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
3003{
3004 int ret;
Petri Gynther014012a2015-02-23 11:00:45 -08003005 unsigned int i;
3006 struct enet_cb *cb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003007
Petri Gynther6f5a2722015-03-06 13:45:00 -08003008 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003009
Petri Gynther6f5a2722015-03-06 13:45:00 -08003010 /* Initialize common Rx ring structures */
3011 priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
3012 priv->num_rx_bds = TOTAL_DESC;
3013 priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct enet_cb),
3014 GFP_KERNEL);
3015 if (!priv->rx_cbs)
3016 return -ENOMEM;
3017
3018 for (i = 0; i < priv->num_rx_bds; i++) {
3019 cb = priv->rx_cbs + i;
3020 cb->bd_addr = priv->rx_bds + i * DMA_DESC_SIZE;
3021 }
3022
Brian Norris7fc527f2014-07-29 14:34:14 -07003023 /* Initialize common TX ring structures */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003024 priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
3025 priv->num_tx_bds = TOTAL_DESC;
Florian Fainellic489be02014-07-23 10:42:15 -07003026 priv->tx_cbs = kcalloc(priv->num_tx_bds, sizeof(struct enet_cb),
Florian Fainellic91b7f62014-07-23 10:42:12 -07003027 GFP_KERNEL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003028 if (!priv->tx_cbs) {
Petri Gyntherebbd96f2015-03-25 12:35:11 -07003029 kfree(priv->rx_cbs);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003030 return -ENOMEM;
3031 }
3032
Petri Gynther014012a2015-02-23 11:00:45 -08003033 for (i = 0; i < priv->num_tx_bds; i++) {
3034 cb = priv->tx_cbs + i;
3035 cb->bd_addr = priv->tx_bds + i * DMA_DESC_SIZE;
3036 }
3037
Petri Gyntherebbd96f2015-03-25 12:35:11 -07003038 /* Init rDma */
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003039 bcmgenet_rdma_writel(priv, priv->dma_max_burst_length,
3040 DMA_SCB_BURST_SIZE);
Petri Gyntherebbd96f2015-03-25 12:35:11 -07003041
3042 /* Initialize Rx queues */
3043 ret = bcmgenet_init_rx_queues(priv->dev);
3044 if (ret) {
3045 netdev_err(priv->dev, "failed to initialize Rx queues\n");
3046 bcmgenet_free_rx_buffers(priv);
3047 kfree(priv->rx_cbs);
3048 kfree(priv->tx_cbs);
3049 return ret;
3050 }
3051
3052 /* Init tDma */
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003053 bcmgenet_tdma_writel(priv, priv->dma_max_burst_length,
3054 DMA_SCB_BURST_SIZE);
Petri Gyntherebbd96f2015-03-25 12:35:11 -07003055
Petri Gynther16c6d662015-02-23 11:00:45 -08003056 /* Initialize Tx queues */
3057 bcmgenet_init_tx_queues(priv->dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003058
3059 return 0;
3060}
3061
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003062/* Interrupt bottom half */
3063static void bcmgenet_irq_task(struct work_struct *work)
3064{
Doug Berger07c52d62017-03-09 16:58:47 -08003065 unsigned int status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003066 struct bcmgenet_priv *priv = container_of(
3067 work, struct bcmgenet_priv, bcmgenet_irq_work);
3068
3069 netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
3070
Doug Bergerb0447ec2017-10-25 15:04:17 -07003071 spin_lock_irq(&priv->lock);
Doug Berger07c52d62017-03-09 16:58:47 -08003072 status = priv->irq0_stat;
3073 priv->irq0_stat = 0;
Doug Bergerb0447ec2017-10-25 15:04:17 -07003074 spin_unlock_irq(&priv->lock);
Doug Berger07c52d62017-03-09 16:58:47 -08003075
Doug Berger25382b92019-10-16 16:06:32 -07003076 if (status & UMAC_IRQ_PHY_DET_R &&
Doug Berger0686bd92019-11-05 11:07:26 -08003077 priv->dev->phydev->autoneg != AUTONEG_ENABLE) {
Doug Berger25382b92019-10-16 16:06:32 -07003078 phy_init_hw(priv->dev->phydev);
Doug Berger0686bd92019-11-05 11:07:26 -08003079 genphy_config_aneg(priv->dev->phydev);
3080 }
Doug Berger25382b92019-10-16 16:06:32 -07003081
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003082 /* Link UP/DOWN event */
Doug Berger7de48402019-10-16 16:06:29 -07003083 if (status & UMAC_IRQ_LINK_EVENT)
Heiner Kallweit28b2e0d2018-01-10 21:21:31 +01003084 phy_mac_interrupt(priv->dev->phydev);
Doug Berger25382b92019-10-16 16:06:32 -07003085
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003086}
3087
Petri Gynther4055eae2015-03-25 12:35:16 -07003088/* bcmgenet_isr1: handle Rx and Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003089static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
3090{
3091 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07003092 struct bcmgenet_rx_ring *rx_ring;
3093 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08003094 unsigned int index, status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003095
Doug Berger07c52d62017-03-09 16:58:47 -08003096 /* Read irq status */
3097 status = bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003098 ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07003099
Brian Norris7fc527f2014-07-29 14:34:14 -07003100 /* clear interrupts */
Doug Berger07c52d62017-03-09 16:58:47 -08003101 bcmgenet_intrl2_1_writel(priv, status, INTRL2_CPU_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003102
3103 netif_dbg(priv, intr, priv->dev,
Doug Berger07c52d62017-03-09 16:58:47 -08003104 "%s: IRQ=0x%x\n", __func__, status);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003105
Petri Gynther4055eae2015-03-25 12:35:16 -07003106 /* Check Rx priority queue interrupts */
3107 for (index = 0; index < priv->hw_params->rx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08003108 if (!(status & BIT(UMAC_IRQ1_RX_INTR_SHIFT + index)))
Petri Gynther4055eae2015-03-25 12:35:16 -07003109 continue;
3110
3111 rx_ring = &priv->rx_rings[index];
Florian Fainelli9f4ca052018-03-22 18:19:33 -07003112 rx_ring->dim.event_ctr++;
Petri Gynther4055eae2015-03-25 12:35:16 -07003113
3114 if (likely(napi_schedule_prep(&rx_ring->napi))) {
3115 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07003116 __napi_schedule_irqoff(&rx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07003117 }
3118 }
3119
3120 /* Check Tx priority queue interrupts */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003121 for (index = 0; index < priv->hw_params->tx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08003122 if (!(status & BIT(index)))
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003123 continue;
3124
Petri Gynther4055eae2015-03-25 12:35:16 -07003125 tx_ring = &priv->tx_rings[index];
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003126
Petri Gynther4055eae2015-03-25 12:35:16 -07003127 if (likely(napi_schedule_prep(&tx_ring->napi))) {
3128 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07003129 __napi_schedule_irqoff(&tx_ring->napi);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003130 }
3131 }
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003132
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003133 return IRQ_HANDLED;
3134}
3135
Petri Gynther4055eae2015-03-25 12:35:16 -07003136/* bcmgenet_isr0: handle Rx and Tx default queues + other stuff */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003137static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
3138{
3139 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07003140 struct bcmgenet_rx_ring *rx_ring;
3141 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08003142 unsigned int status;
3143 unsigned long flags;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003144
Doug Berger07c52d62017-03-09 16:58:47 -08003145 /* Read irq status */
3146 status = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003147 ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07003148
Brian Norris7fc527f2014-07-29 14:34:14 -07003149 /* clear interrupts */
Doug Berger07c52d62017-03-09 16:58:47 -08003150 bcmgenet_intrl2_0_writel(priv, status, INTRL2_CPU_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003151
3152 netif_dbg(priv, intr, priv->dev,
Doug Berger07c52d62017-03-09 16:58:47 -08003153 "IRQ=0x%x\n", status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003154
Doug Berger07c52d62017-03-09 16:58:47 -08003155 if (status & UMAC_IRQ_RXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07003156 rx_ring = &priv->rx_rings[DESC_INDEX];
Florian Fainelli9f4ca052018-03-22 18:19:33 -07003157 rx_ring->dim.event_ctr++;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003158
Petri Gynther4055eae2015-03-25 12:35:16 -07003159 if (likely(napi_schedule_prep(&rx_ring->napi))) {
3160 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07003161 __napi_schedule_irqoff(&rx_ring->napi);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09003162 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003163 }
Petri Gynther4055eae2015-03-25 12:35:16 -07003164
Doug Berger07c52d62017-03-09 16:58:47 -08003165 if (status & UMAC_IRQ_TXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07003166 tx_ring = &priv->tx_rings[DESC_INDEX];
3167
3168 if (likely(napi_schedule_prep(&tx_ring->napi))) {
3169 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07003170 __napi_schedule_irqoff(&tx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07003171 }
3172 }
3173
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003174 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Doug Berger07c52d62017-03-09 16:58:47 -08003175 status & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003176 wake_up(&priv->wq);
3177 }
3178
Doug Berger07c52d62017-03-09 16:58:47 -08003179 /* all other interested interrupts handled in bottom half */
Doug Berger25382b92019-10-16 16:06:32 -07003180 status &= (UMAC_IRQ_LINK_EVENT | UMAC_IRQ_PHY_DET_R);
Doug Berger07c52d62017-03-09 16:58:47 -08003181 if (status) {
3182 /* Save irq status for bottom-half processing. */
3183 spin_lock_irqsave(&priv->lock, flags);
3184 priv->irq0_stat |= status;
3185 spin_unlock_irqrestore(&priv->lock, flags);
3186
3187 schedule_work(&priv->bcmgenet_irq_work);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003188 }
3189
3190 return IRQ_HANDLED;
3191}
3192
Florian Fainelli85620562014-07-21 15:29:23 -07003193static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
3194{
Doug Bergereb236c22020-04-30 16:26:51 -07003195 /* Acknowledge the interrupt */
Florian Fainelli85620562014-07-21 15:29:23 -07003196 return IRQ_HANDLED;
3197}
3198
Florian Fainelli4d2e8882015-07-31 11:42:54 -07003199#ifdef CONFIG_NET_POLL_CONTROLLER
3200static void bcmgenet_poll_controller(struct net_device *dev)
3201{
3202 struct bcmgenet_priv *priv = netdev_priv(dev);
3203
3204 /* Invoke the main RX/TX interrupt handler */
3205 disable_irq(priv->irq0);
3206 bcmgenet_isr0(priv->irq0, priv);
3207 enable_irq(priv->irq0);
3208
3209 /* And the interrupt handler for RX/TX priority queues */
3210 disable_irq(priv->irq1);
3211 bcmgenet_isr1(priv->irq1, priv);
3212 enable_irq(priv->irq1);
3213}
3214#endif
3215
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003216static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
3217{
3218 u32 reg;
3219
3220 reg = bcmgenet_rbuf_ctrl_get(priv);
3221 reg |= BIT(1);
3222 bcmgenet_rbuf_ctrl_set(priv, reg);
3223 udelay(10);
3224
3225 reg &= ~BIT(1);
3226 bcmgenet_rbuf_ctrl_set(priv, reg);
3227 udelay(10);
3228}
3229
3230static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003231 unsigned char *addr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003232{
Andy Shevchenkod2af1422020-04-21 00:51:20 +03003233 bcmgenet_umac_writel(priv, get_unaligned_be32(&addr[0]), UMAC_MAC0);
3234 bcmgenet_umac_writel(priv, get_unaligned_be16(&addr[4]), UMAC_MAC1);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003235}
3236
Jeremy Linton26bd9cc2020-02-24 16:54:02 -06003237static void bcmgenet_get_hw_addr(struct bcmgenet_priv *priv,
3238 unsigned char *addr)
3239{
3240 u32 addr_tmp;
3241
3242 addr_tmp = bcmgenet_umac_readl(priv, UMAC_MAC0);
Andy Shevchenkod2af1422020-04-21 00:51:20 +03003243 put_unaligned_be32(addr_tmp, &addr[0]);
Jeremy Linton26bd9cc2020-02-24 16:54:02 -06003244 addr_tmp = bcmgenet_umac_readl(priv, UMAC_MAC1);
Andy Shevchenkod2af1422020-04-21 00:51:20 +03003245 put_unaligned_be16(addr_tmp, &addr[4]);
Jeremy Linton26bd9cc2020-02-24 16:54:02 -06003246}
3247
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003248/* Returns a reusable dma control register value */
3249static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
3250{
3251 u32 reg;
3252 u32 dma_ctrl;
3253
3254 /* disable DMA */
3255 dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
3256 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
3257 reg &= ~dma_ctrl;
3258 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
3259
3260 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
3261 reg &= ~dma_ctrl;
3262 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
3263
3264 bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
3265 udelay(10);
3266 bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
3267
3268 return dma_ctrl;
3269}
3270
3271static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
3272{
3273 u32 reg;
3274
3275 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
3276 reg |= dma_ctrl;
3277 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
3278
3279 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
3280 reg |= dma_ctrl;
3281 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
3282}
3283
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003284static void bcmgenet_netif_start(struct net_device *dev)
3285{
3286 struct bcmgenet_priv *priv = netdev_priv(dev);
3287
3288 /* Start the network engine */
Doug Berger72f96342020-04-29 13:02:00 -07003289 bcmgenet_set_rx_mode(dev);
Petri Gynther3ab11332015-03-25 12:35:15 -07003290 bcmgenet_enable_rx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003291
3292 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
3293
Doug Bergerd215dba2017-10-25 15:04:16 -07003294 bcmgenet_enable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003295
Florian Fainelli37850e32015-10-17 14:22:46 -07003296 /* Monitor link interrupts now */
3297 bcmgenet_link_intr_enable(priv);
3298
Doug Berger6c97f012017-10-25 15:04:19 -07003299 phy_start(dev->phydev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003300}
3301
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003302static int bcmgenet_open(struct net_device *dev)
3303{
3304 struct bcmgenet_priv *priv = netdev_priv(dev);
3305 unsigned long dma_ctrl;
3306 u32 reg;
3307 int ret;
3308
3309 netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
3310
3311 /* Turn on the clock */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003312 clk_prepare_enable(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003313
Florian Fainellia642c4f2015-03-23 15:09:56 -07003314 /* If this is an internal GPHY, power it back on now, before UniMAC is
3315 * brought out of reset as absolutely no UniMAC activity is allowed
3316 */
Florian Fainellic624f892015-07-16 15:51:17 -07003317 if (priv->internal_phy)
Florian Fainellia642c4f2015-03-23 15:09:56 -07003318 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3319
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003320 /* take MAC out of reset */
3321 bcmgenet_umac_reset(priv);
3322
Doug Berger28c2d1a2017-10-25 15:04:13 -07003323 init_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003324
Doug Berger206f54b2019-12-17 16:51:12 -08003325 /* Apply features again in case we changed them while interface was
3326 * down
3327 */
3328 bcmgenet_set_features(dev, dev->features);
3329
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003330 bcmgenet_set_hw_addr(priv, dev->dev_addr);
3331
Florian Fainellic624f892015-07-16 15:51:17 -07003332 if (priv->internal_phy) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003333 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
3334 reg |= EXT_ENERGY_DET_MASK;
3335 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
3336 }
3337
3338 /* Disable RX/TX DMA and flush TX queues */
3339 dma_ctrl = bcmgenet_dma_disable(priv);
3340
3341 /* Reinitialize TDMA and RDMA and SW housekeeping */
3342 ret = bcmgenet_init_dma(priv);
3343 if (ret) {
3344 netdev_err(dev, "failed to initialize DMA\n");
Doug Berger6b6d017f2019-11-05 11:07:25 -08003345 goto err_clk_disable;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003346 }
3347
3348 /* Always enable ring 16 - descriptor ring */
3349 bcmgenet_enable_dma(priv, dma_ctrl);
3350
Petri Gynther0034de42015-03-13 14:45:00 -07003351 /* HFB init */
3352 bcmgenet_hfb_init(priv);
3353
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003354 ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003355 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003356 if (ret < 0) {
3357 netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
3358 goto err_fini_dma;
3359 }
3360
3361 ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003362 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003363 if (ret < 0) {
3364 netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
3365 goto err_irq0;
3366 }
3367
Doug Berger6b6d017f2019-11-05 11:07:25 -08003368 ret = bcmgenet_mii_probe(dev);
3369 if (ret) {
3370 netdev_err(dev, "failed to connect to PHY\n");
3371 goto err_irq1;
3372 }
3373
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003374 bcmgenet_netif_start(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003375
Doug Berger09e805d2018-11-01 15:55:37 -07003376 netif_tx_start_all_queues(dev);
3377
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003378 return 0;
3379
Doug Berger6b6d017f2019-11-05 11:07:25 -08003380err_irq1:
3381 free_irq(priv->irq1, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003382err_irq0:
Florian Fainelli978ffac42015-07-16 15:51:15 -07003383 free_irq(priv->irq0, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003384err_fini_dma:
Doug Berger4fd6dc92017-10-25 15:04:12 -07003385 bcmgenet_dma_teardown(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003386 bcmgenet_fini_dma(priv);
3387err_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08003388 if (priv->internal_phy)
3389 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003390 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003391 return ret;
3392}
3393
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003394static void bcmgenet_netif_stop(struct net_device *dev)
3395{
3396 struct bcmgenet_priv *priv = netdev_priv(dev);
3397
Doug Bergerd215dba2017-10-25 15:04:16 -07003398 bcmgenet_disable_tx_napi(priv);
Doug Berger09e805d2018-11-01 15:55:37 -07003399 netif_tx_disable(dev);
Doug Bergerd215dba2017-10-25 15:04:16 -07003400
3401 /* Disable MAC receive */
3402 umac_enable_set(priv, CMD_RX_EN, false);
3403
3404 bcmgenet_dma_teardown(priv);
3405
3406 /* Disable MAC transmit. TX DMA disabled must be done before this */
3407 umac_enable_set(priv, CMD_TX_EN, false);
3408
Doug Berger6c97f012017-10-25 15:04:19 -07003409 phy_stop(dev->phydev);
Petri Gynther3ab11332015-03-25 12:35:15 -07003410 bcmgenet_disable_rx_napi(priv);
Doug Bergerfbf557d2017-10-25 15:04:15 -07003411 bcmgenet_intr_disable(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003412
3413 /* Wait for pending work items to complete. Since interrupts are
3414 * disabled no new work will be scheduled.
3415 */
3416 cancel_work_sync(&priv->bcmgenet_irq_work);
Florian Fainellicc013fb2014-08-11 14:50:43 -07003417
Florian Fainellicc013fb2014-08-11 14:50:43 -07003418 priv->old_link = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07003419 priv->old_speed = -1;
Florian Fainellicc013fb2014-08-11 14:50:43 -07003420 priv->old_duplex = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07003421 priv->old_pause = -1;
Doug Bergerd215dba2017-10-25 15:04:16 -07003422
3423 /* tx reclaim */
3424 bcmgenet_tx_reclaim_all(dev);
3425 bcmgenet_fini_dma(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003426}
3427
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003428static int bcmgenet_close(struct net_device *dev)
3429{
3430 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Bergerd215dba2017-10-25 15:04:16 -07003431 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003432
3433 netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
3434
Florian Fainelli909ff5e2014-07-21 15:29:21 -07003435 bcmgenet_netif_stop(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003436
Florian Fainellic96e7312014-11-10 18:06:20 -08003437 /* Really kill the PHY state machine and disconnect from it */
Doug Berger6c97f012017-10-25 15:04:19 -07003438 phy_disconnect(dev->phydev);
Florian Fainellic96e7312014-11-10 18:06:20 -08003439
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003440 free_irq(priv->irq0, priv);
3441 free_irq(priv->irq1, priv);
3442
Florian Fainellic624f892015-07-16 15:51:17 -07003443 if (priv->internal_phy)
Florian Fainellica8cf342015-03-23 15:09:51 -07003444 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003445
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003446 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003447
Florian Fainellica8cf342015-03-23 15:09:51 -07003448 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003449}
3450
Florian Fainelli13ea6572015-06-04 16:15:50 -07003451static void bcmgenet_dump_tx_queue(struct bcmgenet_tx_ring *ring)
3452{
3453 struct bcmgenet_priv *priv = ring->priv;
3454 u32 p_index, c_index, intsts, intmsk;
3455 struct netdev_queue *txq;
3456 unsigned int free_bds;
Florian Fainelli13ea6572015-06-04 16:15:50 -07003457 bool txq_stopped;
3458
3459 if (!netif_msg_tx_err(priv))
3460 return;
3461
3462 txq = netdev_get_tx_queue(priv->dev, ring->queue);
3463
Doug Bergerb0447ec2017-10-25 15:04:17 -07003464 spin_lock(&ring->lock);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003465 if (ring->index == DESC_INDEX) {
3466 intsts = ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
3467 intmsk = UMAC_IRQ_TXDMA_DONE | UMAC_IRQ_TXDMA_MBDONE;
3468 } else {
3469 intsts = ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
3470 intmsk = 1 << ring->index;
3471 }
3472 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
3473 p_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_PROD_INDEX);
3474 txq_stopped = netif_tx_queue_stopped(txq);
3475 free_bds = ring->free_bds;
Doug Bergerb0447ec2017-10-25 15:04:17 -07003476 spin_unlock(&ring->lock);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003477
3478 netif_err(priv, tx_err, priv->dev, "Ring %d queue %d status summary\n"
3479 "TX queue status: %s, interrupts: %s\n"
3480 "(sw)free_bds: %d (sw)size: %d\n"
3481 "(sw)p_index: %d (hw)p_index: %d\n"
3482 "(sw)c_index: %d (hw)c_index: %d\n"
3483 "(sw)clean_p: %d (sw)write_p: %d\n"
3484 "(sw)cb_ptr: %d (sw)end_ptr: %d\n",
3485 ring->index, ring->queue,
3486 txq_stopped ? "stopped" : "active",
3487 intsts & intmsk ? "enabled" : "disabled",
3488 free_bds, ring->size,
3489 ring->prod_index, p_index & DMA_P_INDEX_MASK,
3490 ring->c_index, c_index & DMA_C_INDEX_MASK,
3491 ring->clean_ptr, ring->write_ptr,
3492 ring->cb_ptr, ring->end_ptr);
3493}
3494
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05003495static void bcmgenet_timeout(struct net_device *dev, unsigned int txqueue)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003496{
3497 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003498 u32 int0_enable = 0;
3499 u32 int1_enable = 0;
3500 unsigned int q;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003501
3502 netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
3503
Florian Fainelli13ea6572015-06-04 16:15:50 -07003504 for (q = 0; q < priv->hw_params->tx_queues; q++)
3505 bcmgenet_dump_tx_queue(&priv->tx_rings[q]);
3506 bcmgenet_dump_tx_queue(&priv->tx_rings[DESC_INDEX]);
3507
3508 bcmgenet_tx_reclaim_all(dev);
3509
3510 for (q = 0; q < priv->hw_params->tx_queues; q++)
3511 int1_enable |= (1 << q);
3512
3513 int0_enable = UMAC_IRQ_TXDMA_DONE;
3514
3515 /* Re-enable TX interrupts if disabled */
3516 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
3517 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
3518
Florian Westphal860e9532016-05-03 16:33:13 +02003519 netif_trans_update(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003520
3521 dev->stats.tx_errors++;
3522
3523 netif_tx_wake_all_queues(dev);
3524}
3525
Justin Chen35cbef92019-07-17 14:58:53 -07003526#define MAX_MDF_FILTER 17
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003527
3528static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
3529 unsigned char *addr,
Justin Chen35cbef92019-07-17 14:58:53 -07003530 int *i)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003531{
Florian Fainellic91b7f62014-07-23 10:42:12 -07003532 bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
3533 UMAC_MDF_ADDR + (*i * 4));
3534 bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
3535 addr[4] << 8 | addr[5],
3536 UMAC_MDF_ADDR + ((*i + 1) * 4));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003537 *i += 2;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003538}
3539
3540static void bcmgenet_set_rx_mode(struct net_device *dev)
3541{
3542 struct bcmgenet_priv *priv = netdev_priv(dev);
3543 struct netdev_hw_addr *ha;
Justin Chen35cbef92019-07-17 14:58:53 -07003544 int i, nfilter;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003545 u32 reg;
3546
3547 netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
3548
Justin Chen35cbef92019-07-17 14:58:53 -07003549 /* Number of filters needed */
3550 nfilter = netdev_uc_count(dev) + netdev_mc_count(dev) + 2;
3551
3552 /*
3553 * Turn on promicuous mode for three scenarios
3554 * 1. IFF_PROMISC flag is set
3555 * 2. IFF_ALLMULTI flag is set
3556 * 3. The number of filters needed exceeds the number filters
3557 * supported by the hardware.
3558 */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003559 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
Justin Chen35cbef92019-07-17 14:58:53 -07003560 if ((dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) ||
3561 (nfilter > MAX_MDF_FILTER)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003562 reg |= CMD_PROMISC;
3563 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3564 bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
3565 return;
3566 } else {
3567 reg &= ~CMD_PROMISC;
3568 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3569 }
3570
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003571 /* update MDF filter */
3572 i = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003573 /* Broadcast */
Justin Chen35cbef92019-07-17 14:58:53 -07003574 bcmgenet_set_mdf_addr(priv, dev->broadcast, &i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003575 /* my own address.*/
Justin Chen35cbef92019-07-17 14:58:53 -07003576 bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003577
Justin Chen35cbef92019-07-17 14:58:53 -07003578 /* Unicast */
3579 netdev_for_each_uc_addr(ha, dev)
3580 bcmgenet_set_mdf_addr(priv, ha->addr, &i);
3581
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003582 /* Multicast */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003583 netdev_for_each_mc_addr(ha, dev)
Justin Chen35cbef92019-07-17 14:58:53 -07003584 bcmgenet_set_mdf_addr(priv, ha->addr, &i);
3585
3586 /* Enable filters */
3587 reg = GENMASK(MAX_MDF_FILTER - 1, MAX_MDF_FILTER - nfilter);
3588 bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003589}
3590
3591/* Set the hardware MAC address. */
3592static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
3593{
3594 struct sockaddr *addr = p;
3595
3596 /* Setting the MAC address at the hardware level is not possible
3597 * without disabling the UniMAC RX/TX enable bits.
3598 */
3599 if (netif_running(dev))
3600 return -EBUSY;
3601
3602 ether_addr_copy(dev->dev_addr, addr->sa_data);
3603
3604 return 0;
3605}
3606
Florian Fainelli37a30b42017-03-16 10:27:08 -07003607static struct net_device_stats *bcmgenet_get_stats(struct net_device *dev)
3608{
3609 struct bcmgenet_priv *priv = netdev_priv(dev);
3610 unsigned long tx_bytes = 0, tx_packets = 0;
3611 unsigned long rx_bytes = 0, rx_packets = 0;
3612 unsigned long rx_errors = 0, rx_dropped = 0;
3613 struct bcmgenet_tx_ring *tx_ring;
3614 struct bcmgenet_rx_ring *rx_ring;
3615 unsigned int q;
3616
3617 for (q = 0; q < priv->hw_params->tx_queues; q++) {
3618 tx_ring = &priv->tx_rings[q];
3619 tx_bytes += tx_ring->bytes;
3620 tx_packets += tx_ring->packets;
3621 }
3622 tx_ring = &priv->tx_rings[DESC_INDEX];
3623 tx_bytes += tx_ring->bytes;
3624 tx_packets += tx_ring->packets;
3625
3626 for (q = 0; q < priv->hw_params->rx_queues; q++) {
3627 rx_ring = &priv->rx_rings[q];
3628
3629 rx_bytes += rx_ring->bytes;
3630 rx_packets += rx_ring->packets;
3631 rx_errors += rx_ring->errors;
3632 rx_dropped += rx_ring->dropped;
3633 }
3634 rx_ring = &priv->rx_rings[DESC_INDEX];
3635 rx_bytes += rx_ring->bytes;
3636 rx_packets += rx_ring->packets;
3637 rx_errors += rx_ring->errors;
3638 rx_dropped += rx_ring->dropped;
3639
3640 dev->stats.tx_bytes = tx_bytes;
3641 dev->stats.tx_packets = tx_packets;
3642 dev->stats.rx_bytes = rx_bytes;
3643 dev->stats.rx_packets = rx_packets;
3644 dev->stats.rx_errors = rx_errors;
3645 dev->stats.rx_missed_errors = rx_errors;
Doug Bergera6d0b832020-04-23 15:44:17 -07003646 dev->stats.rx_dropped = rx_dropped;
Florian Fainelli37a30b42017-03-16 10:27:08 -07003647 return &dev->stats;
3648}
3649
Florian Fainelli47ff61542020-07-02 21:57:00 -07003650static int bcmgenet_change_carrier(struct net_device *dev, bool new_carrier)
3651{
3652 struct bcmgenet_priv *priv = netdev_priv(dev);
3653
3654 if (!dev->phydev || !phy_is_pseudo_fixed_link(dev->phydev) ||
3655 priv->phy_interface != PHY_INTERFACE_MODE_MOCA)
3656 return -EOPNOTSUPP;
3657
3658 if (new_carrier)
3659 netif_carrier_on(dev);
3660 else
3661 netif_carrier_off(dev);
3662
3663 return 0;
3664}
3665
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003666static const struct net_device_ops bcmgenet_netdev_ops = {
3667 .ndo_open = bcmgenet_open,
3668 .ndo_stop = bcmgenet_close,
3669 .ndo_start_xmit = bcmgenet_xmit,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003670 .ndo_tx_timeout = bcmgenet_timeout,
3671 .ndo_set_rx_mode = bcmgenet_set_rx_mode,
3672 .ndo_set_mac_address = bcmgenet_set_mac_addr,
Heiner Kallweitfd786fb12020-01-21 22:09:33 +01003673 .ndo_do_ioctl = phy_do_ioctl_running,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003674 .ndo_set_features = bcmgenet_set_features,
Florian Fainelli4d2e8882015-07-31 11:42:54 -07003675#ifdef CONFIG_NET_POLL_CONTROLLER
3676 .ndo_poll_controller = bcmgenet_poll_controller,
3677#endif
Florian Fainelli37a30b42017-03-16 10:27:08 -07003678 .ndo_get_stats = bcmgenet_get_stats,
Florian Fainelli47ff61542020-07-02 21:57:00 -07003679 .ndo_change_carrier = bcmgenet_change_carrier,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003680};
3681
3682/* Array of GENET hardware parameters/characteristics */
3683static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
3684 [GENET_V1] = {
3685 .tx_queues = 0,
Petri Gynther51a966a2015-02-23 11:00:46 -08003686 .tx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003687 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003688 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003689 .bp_in_en_shift = 16,
3690 .bp_in_mask = 0xffff,
3691 .hfb_filter_cnt = 16,
3692 .qtag_mask = 0x1F,
3693 .hfb_offset = 0x1000,
3694 .rdma_offset = 0x2000,
3695 .tdma_offset = 0x3000,
3696 .words_per_bd = 2,
3697 },
3698 [GENET_V2] = {
3699 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003700 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003701 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003702 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003703 .bp_in_en_shift = 16,
3704 .bp_in_mask = 0xffff,
3705 .hfb_filter_cnt = 16,
3706 .qtag_mask = 0x1F,
3707 .tbuf_offset = 0x0600,
3708 .hfb_offset = 0x1000,
3709 .hfb_reg_offset = 0x2000,
3710 .rdma_offset = 0x3000,
3711 .tdma_offset = 0x4000,
3712 .words_per_bd = 2,
3713 .flags = GENET_HAS_EXT,
3714 },
3715 [GENET_V3] = {
3716 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003717 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003718 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003719 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003720 .bp_in_en_shift = 17,
3721 .bp_in_mask = 0x1ffff,
3722 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003723 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003724 .qtag_mask = 0x3F,
3725 .tbuf_offset = 0x0600,
3726 .hfb_offset = 0x8000,
3727 .hfb_reg_offset = 0xfc00,
3728 .rdma_offset = 0x10000,
3729 .tdma_offset = 0x11000,
3730 .words_per_bd = 2,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003731 .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR |
3732 GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003733 },
3734 [GENET_V4] = {
3735 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003736 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003737 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003738 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003739 .bp_in_en_shift = 17,
3740 .bp_in_mask = 0x1ffff,
3741 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003742 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003743 .qtag_mask = 0x3F,
3744 .tbuf_offset = 0x0600,
3745 .hfb_offset = 0x8000,
3746 .hfb_reg_offset = 0xfc00,
3747 .rdma_offset = 0x2000,
3748 .tdma_offset = 0x4000,
3749 .words_per_bd = 3,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003750 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3751 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003752 },
Doug Berger42138082017-03-13 17:41:42 -07003753 [GENET_V5] = {
3754 .tx_queues = 4,
3755 .tx_bds_per_q = 32,
3756 .rx_queues = 0,
3757 .rx_bds_per_q = 0,
3758 .bp_in_en_shift = 17,
3759 .bp_in_mask = 0x1ffff,
3760 .hfb_filter_cnt = 48,
3761 .hfb_filter_size = 128,
3762 .qtag_mask = 0x3F,
3763 .tbuf_offset = 0x0600,
3764 .hfb_offset = 0x8000,
3765 .hfb_reg_offset = 0xfc00,
3766 .rdma_offset = 0x2000,
3767 .tdma_offset = 0x4000,
3768 .words_per_bd = 3,
3769 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3770 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
3771 },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003772};
3773
3774/* Infer hardware parameters from the detected GENET version */
3775static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
3776{
3777 struct bcmgenet_hw_params *params;
3778 u32 reg;
3779 u8 major;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003780 u16 gphy_rev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003781
Doug Berger42138082017-03-13 17:41:42 -07003782 if (GENET_IS_V5(priv) || GENET_IS_V4(priv)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003783 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3784 genet_dma_ring_regs = genet_dma_ring_regs_v4;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003785 } else if (GENET_IS_V3(priv)) {
3786 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3787 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003788 } else if (GENET_IS_V2(priv)) {
3789 bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
3790 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003791 } else if (GENET_IS_V1(priv)) {
3792 bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
3793 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003794 }
3795
3796 /* enum genet_version starts at 1 */
3797 priv->hw_params = &bcmgenet_hw_params[priv->version];
3798 params = priv->hw_params;
3799
3800 /* Read GENET HW version */
3801 reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
3802 major = (reg >> 24 & 0x0f);
Doug Berger42138082017-03-13 17:41:42 -07003803 if (major == 6)
3804 major = 5;
3805 else if (major == 5)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003806 major = 4;
3807 else if (major == 0)
3808 major = 1;
3809 if (major != priv->version) {
3810 dev_err(&priv->pdev->dev,
3811 "GENET version mismatch, got: %d, configured for: %d\n",
3812 major, priv->version);
3813 }
3814
3815 /* Print the GENET core version */
3816 dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003817 major, (reg >> 16) & 0x0f, reg & 0xffff);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003818
Florian Fainelli487320c2014-09-19 13:07:53 -07003819 /* Store the integrated PHY revision for the MDIO probing function
3820 * to pass this information to the PHY driver. The PHY driver expects
3821 * to find the PHY major revision in bits 15:8 while the GENET register
3822 * stores that information in bits 7:0, account for that.
Florian Fainellib04a2f52014-12-03 09:56:59 -08003823 *
3824 * On newer chips, starting with PHY revision G0, a new scheme is
3825 * deployed similar to the Starfighter 2 switch with GPHY major
3826 * revision in bits 15:8 and patch level in bits 7:0. Major revision 0
3827 * is reserved as well as special value 0x01ff, we have a small
3828 * heuristic to check for the new GPHY revision and re-arrange things
3829 * so the GPHY driver is happy.
Florian Fainelli487320c2014-09-19 13:07:53 -07003830 */
Florian Fainellib04a2f52014-12-03 09:56:59 -08003831 gphy_rev = reg & 0xffff;
3832
Doug Berger42138082017-03-13 17:41:42 -07003833 if (GENET_IS_V5(priv)) {
3834 /* The EPHY revision should come from the MDIO registers of
3835 * the PHY not from GENET.
3836 */
3837 if (gphy_rev != 0) {
3838 pr_warn("GENET is reporting EPHY revision: 0x%04x\n",
3839 gphy_rev);
3840 }
Doug Bergereca4bad2017-03-09 16:58:45 -08003841 /* This is reserved so should require special treatment */
David S. Miller101c4312017-03-15 11:59:10 -07003842 } else if (gphy_rev == 0 || gphy_rev == 0x01ff) {
Doug Bergereca4bad2017-03-09 16:58:45 -08003843 pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev);
3844 return;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003845 /* This is the good old scheme, just GPHY major, no minor nor patch */
Doug Berger42138082017-03-13 17:41:42 -07003846 } else if ((gphy_rev & 0xf0) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003847 priv->gphy_rev = gphy_rev << 8;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003848 /* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */
Doug Berger42138082017-03-13 17:41:42 -07003849 } else if ((gphy_rev & 0xff00) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003850 priv->gphy_rev = gphy_rev;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003851 }
Florian Fainelli487320c2014-09-19 13:07:53 -07003852
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003853#ifdef CONFIG_PHYS_ADDR_T_64BIT
3854 if (!(params->flags & GENET_HAS_40BITS))
3855 pr_warn("GENET does not support 40-bits PA\n");
3856#endif
3857
3858 pr_debug("Configuration for version: %d\n"
Petri Gynther3feafa02015-03-05 17:40:14 -08003859 "TXq: %1d, TXqBDs: %1d, RXq: %1d, RXqBDs: %1d\n"
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003860 "BP << en: %2d, BP msk: 0x%05x\n"
3861 "HFB count: %2d, QTAQ msk: 0x%05x\n"
3862 "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
3863 "RDMA: 0x%05x, TDMA: 0x%05x\n"
3864 "Words/BD: %d\n",
3865 priv->version,
Petri Gynther51a966a2015-02-23 11:00:46 -08003866 params->tx_queues, params->tx_bds_per_q,
Petri Gynther3feafa02015-03-05 17:40:14 -08003867 params->rx_queues, params->rx_bds_per_q,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003868 params->bp_in_en_shift, params->bp_in_mask,
3869 params->hfb_filter_cnt, params->qtag_mask,
3870 params->tbuf_offset, params->hfb_offset,
3871 params->hfb_reg_offset,
3872 params->rdma_offset, params->tdma_offset,
3873 params->words_per_bd);
3874}
3875
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003876struct bcmgenet_plat_data {
3877 enum bcmgenet_version version;
3878 u32 dma_max_burst_length;
3879};
3880
3881static const struct bcmgenet_plat_data v1_plat_data = {
3882 .version = GENET_V1,
3883 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3884};
3885
3886static const struct bcmgenet_plat_data v2_plat_data = {
3887 .version = GENET_V2,
3888 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3889};
3890
3891static const struct bcmgenet_plat_data v3_plat_data = {
3892 .version = GENET_V3,
3893 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3894};
3895
3896static const struct bcmgenet_plat_data v4_plat_data = {
3897 .version = GENET_V4,
3898 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3899};
3900
3901static const struct bcmgenet_plat_data v5_plat_data = {
3902 .version = GENET_V5,
3903 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3904};
3905
3906static const struct bcmgenet_plat_data bcm2711_plat_data = {
3907 .version = GENET_V5,
3908 .dma_max_burst_length = 0x08,
3909};
3910
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003911static const struct of_device_id bcmgenet_match[] = {
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003912 { .compatible = "brcm,genet-v1", .data = &v1_plat_data },
3913 { .compatible = "brcm,genet-v2", .data = &v2_plat_data },
3914 { .compatible = "brcm,genet-v3", .data = &v3_plat_data },
3915 { .compatible = "brcm,genet-v4", .data = &v4_plat_data },
3916 { .compatible = "brcm,genet-v5", .data = &v5_plat_data },
3917 { .compatible = "brcm,bcm2711-genet-v5", .data = &bcm2711_plat_data },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003918 { },
3919};
Luis de Bethencourte8048e52015-09-18 17:55:02 +02003920MODULE_DEVICE_TABLE(of, bcmgenet_match);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003921
3922static int bcmgenet_probe(struct platform_device *pdev)
3923{
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003924 struct bcmgenet_platform_data *pd = pdev->dev.platform_data;
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003925 const struct bcmgenet_plat_data *pdata;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003926 struct bcmgenet_priv *priv;
3927 struct net_device *dev;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07003928 unsigned int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003929 int err = -EIO;
3930
Petri Gynther3feafee2015-03-05 17:40:12 -08003931 /* Up to GENET_MAX_MQ_CNT + 1 TX queues and RX queues */
3932 dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1,
3933 GENET_MAX_MQ_CNT + 1);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003934 if (!dev) {
3935 dev_err(&pdev->dev, "can't allocate net device\n");
3936 return -ENOMEM;
3937 }
3938
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003939 priv = netdev_priv(dev);
3940 priv->irq0 = platform_get_irq(pdev, 0);
Stefan Wahren2b65f932019-11-11 20:49:21 +01003941 if (priv->irq0 < 0) {
3942 err = priv->irq0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003943 goto err;
3944 }
Stefan Wahren2b65f932019-11-11 20:49:21 +01003945 priv->irq1 = platform_get_irq(pdev, 1);
3946 if (priv->irq1 < 0) {
3947 err = priv->irq1;
3948 goto err;
3949 }
3950 priv->wol_irq = platform_get_irq_optional(pdev, 2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003951
YueHaibing4ca33482019-08-21 21:41:31 +08003952 priv->base = devm_platform_ioremap_resource(pdev, 0);
Fabio Estevam5343a102014-02-24 00:47:24 -03003953 if (IS_ERR(priv->base)) {
3954 err = PTR_ERR(priv->base);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003955 goto err;
3956 }
3957
Doug Berger07c52d62017-03-09 16:58:47 -08003958 spin_lock_init(&priv->lock);
3959
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003960 SET_NETDEV_DEV(dev, &pdev->dev);
3961 dev_set_drvdata(&pdev->dev, dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003962 dev->watchdog_timeo = 2 * HZ;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003963 dev->ethtool_ops = &bcmgenet_ethtool_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003964 dev->netdev_ops = &bcmgenet_netdev_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003965
3966 priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
3967
Doug Bergerae895c42019-12-17 16:51:13 -08003968 /* Set default features */
3969 dev->features |= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
3970 NETIF_F_RXCSUM;
3971 dev->hw_features |= dev->features;
3972 dev->vlan_features |= dev->features;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003973
Florian Fainelli85620562014-07-21 15:29:23 -07003974 /* Request the WOL interrupt and advertise suspend if available */
3975 priv->wol_irq_disabled = true;
3976 err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0,
3977 dev->name, priv);
3978 if (!err)
3979 device_set_wakeup_capable(&pdev->dev, 1);
3980
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003981 /* Set the needed headroom to account for any possible
3982 * features enabling/disabling at runtime
3983 */
3984 dev->needed_headroom += 64;
3985
3986 netdev_boot_setup_check(dev);
3987
3988 priv->dev = dev;
3989 priv->pdev = pdev;
Jeremy Linton99c6b062020-02-24 16:54:01 -06003990
3991 pdata = device_get_match_data(&pdev->dev);
3992 if (pdata) {
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003993 priv->version = pdata->version;
3994 priv->dma_max_burst_length = pdata->dma_max_burst_length;
3995 } else {
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003996 priv->version = pd->genet_version;
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003997 priv->dma_max_burst_length = DMA_MAX_BURST_LENGTH;
3998 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003999
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03004000 priv->clk = devm_clk_get_optional(&priv->pdev->dev, "enet");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07004001 if (IS_ERR(priv->clk)) {
Jeremy Lintonae200c22020-02-24 16:54:03 -06004002 dev_dbg(&priv->pdev->dev, "failed to get enet clock\n");
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03004003 err = PTR_ERR(priv->clk);
4004 goto err;
Florian Fainelli7d5d3072015-07-22 17:28:23 -07004005 }
Florian Fainellie4a60a92014-08-11 14:50:42 -07004006
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03004007 err = clk_prepare_enable(priv->clk);
4008 if (err)
4009 goto err;
Florian Fainellie4a60a92014-08-11 14:50:42 -07004010
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004011 bcmgenet_set_hw_params(priv);
4012
Doug Berger99d55632019-12-17 16:51:08 -08004013 err = -EIO;
4014 if (priv->hw_params->flags & GENET_HAS_40BITS)
4015 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
4016 if (err)
4017 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
4018 if (err)
4019 goto err;
4020
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004021 /* Mii wait queue */
4022 init_waitqueue_head(&priv->wq);
4023 /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
4024 priv->rx_buf_len = RX_BUF_LENGTH;
4025 INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
4026
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03004027 priv->clk_wol = devm_clk_get_optional(&priv->pdev->dev, "enet-wol");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07004028 if (IS_ERR(priv->clk_wol)) {
Jeremy Lintonae200c22020-02-24 16:54:03 -06004029 dev_dbg(&priv->pdev->dev, "failed to get enet-wol clock\n");
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03004030 err = PTR_ERR(priv->clk_wol);
4031 goto err;
Florian Fainelli7d5d3072015-07-22 17:28:23 -07004032 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004033
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03004034 priv->clk_eee = devm_clk_get_optional(&priv->pdev->dev, "enet-eee");
Florian Fainelli6ef398e2014-11-25 21:16:35 -08004035 if (IS_ERR(priv->clk_eee)) {
Jeremy Lintonae200c22020-02-24 16:54:03 -06004036 dev_dbg(&priv->pdev->dev, "failed to get enet-eee clock\n");
Andy Shevchenkoc80d36f2020-04-21 00:51:19 +03004037 err = PTR_ERR(priv->clk_eee);
4038 goto err;
Florian Fainelli6ef398e2014-11-25 21:16:35 -08004039 }
4040
Doug Berger6be371b2017-03-09 16:58:48 -08004041 /* If this is an internal GPHY, power it on now, before UniMAC is
4042 * brought out of reset as absolutely no UniMAC activity is allowed
4043 */
Jeremy Linton99c6b062020-02-24 16:54:01 -06004044 if (device_get_phy_mode(&pdev->dev) == PHY_INTERFACE_MODE_INTERNAL)
Doug Berger6be371b2017-03-09 16:58:48 -08004045 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
4046
Andy Shevchenko7d3cca72020-04-21 00:51:21 +03004047 if (pd && !IS_ERR_OR_NULL(pd->mac_address))
Jeremy Linton26bd9cc2020-02-24 16:54:02 -06004048 ether_addr_copy(dev->dev_addr, pd->mac_address);
4049 else
4050 if (!device_get_mac_address(&pdev->dev, dev->dev_addr, ETH_ALEN))
4051 if (has_acpi_companion(&pdev->dev))
4052 bcmgenet_get_hw_addr(priv, dev->dev_addr);
4053
4054 if (!is_valid_ether_addr(dev->dev_addr)) {
4055 dev_warn(&pdev->dev, "using random Ethernet MAC\n");
4056 eth_hw_addr_random(dev);
4057 }
4058
Doug Berger28c2d1a2017-10-25 15:04:13 -07004059 reset_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004060
4061 err = bcmgenet_mii_init(dev);
4062 if (err)
4063 goto err_clk_disable;
4064
4065 /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
4066 * just the ring 16 descriptor based TX
4067 */
4068 netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
4069 netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
4070
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07004071 /* Set default coalescing parameters */
4072 for (i = 0; i < priv->hw_params->rx_queues; i++)
4073 priv->rx_rings[i].rx_max_coalesced_frames = 1;
4074 priv->rx_rings[DESC_INDEX].rx_max_coalesced_frames = 1;
4075
Florian Fainelli219575e2014-06-26 10:26:21 -07004076 /* libphy will determine the link state */
4077 netif_carrier_off(dev);
4078
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004079 /* Turn off the main clock, WOL clock is handled separately */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07004080 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004081
Florian Fainelli0f50ce92014-06-26 10:26:20 -07004082 err = register_netdev(dev);
4083 if (err)
4084 goto err;
4085
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004086 return err;
4087
4088err_clk_disable:
Florian Fainelli7d5d3072015-07-22 17:28:23 -07004089 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004090err:
4091 free_netdev(dev);
4092 return err;
4093}
4094
4095static int bcmgenet_remove(struct platform_device *pdev)
4096{
4097 struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
4098
4099 dev_set_drvdata(&pdev->dev, NULL);
4100 unregister_netdev(priv->dev);
4101 bcmgenet_mii_exit(priv->dev);
4102 free_netdev(priv->dev);
4103
4104 return 0;
4105}
4106
Florian Fainellid9f45ab2019-10-15 10:36:24 -07004107static void bcmgenet_shutdown(struct platform_device *pdev)
4108{
4109 bcmgenet_remove(pdev);
4110}
4111
Florian Fainellib6e978e2014-07-21 15:29:22 -07004112#ifdef CONFIG_PM_SLEEP
Doug Bergereb236c22020-04-30 16:26:51 -07004113static int bcmgenet_resume_noirq(struct device *d)
4114{
4115 struct net_device *dev = dev_get_drvdata(d);
4116 struct bcmgenet_priv *priv = netdev_priv(dev);
4117 int ret;
4118 u32 reg;
4119
4120 if (!netif_running(dev))
4121 return 0;
4122
4123 /* Turn on the clock */
4124 ret = clk_prepare_enable(priv->clk);
4125 if (ret)
4126 return ret;
4127
4128 if (device_may_wakeup(d) && priv->wolopts) {
4129 /* Account for Wake-on-LAN events and clear those events
4130 * (Some devices need more time between enabling the clocks
4131 * and the interrupt register reflecting the wake event so
4132 * read the register twice)
4133 */
4134 reg = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT);
4135 reg = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT);
4136 if (reg & UMAC_IRQ_WAKE_EVENT)
4137 pm_wakeup_event(&priv->pdev->dev, 0);
4138 }
4139
4140 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_WAKE_EVENT, INTRL2_CPU_CLEAR);
4141
4142 return 0;
4143}
4144
Florian Fainellib6e978e2014-07-21 15:29:22 -07004145static int bcmgenet_resume(struct device *d)
4146{
4147 struct net_device *dev = dev_get_drvdata(d);
4148 struct bcmgenet_priv *priv = netdev_priv(dev);
4149 unsigned long dma_ctrl;
Doug Berger3e370952020-04-29 13:02:05 -07004150 u32 offset, reg;
Florian Fainellib6e978e2014-07-21 15:29:22 -07004151 int ret;
Florian Fainellib6e978e2014-07-21 15:29:22 -07004152
4153 if (!netif_running(dev))
4154 return 0;
4155
Doug Berger1a1d5102020-04-29 13:02:02 -07004156 /* From WOL-enabled suspend, switch to regular clock */
4157 if (device_may_wakeup(d) && priv->wolopts)
4158 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
4159
Florian Fainellia6f31f52015-03-23 15:09:57 -07004160 /* If this is an internal GPHY, power it back on now, before UniMAC is
4161 * brought out of reset as absolutely no UniMAC activity is allowed
4162 */
Florian Fainellic624f892015-07-16 15:51:17 -07004163 if (priv->internal_phy)
Florian Fainellia6f31f52015-03-23 15:09:57 -07004164 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
4165
Florian Fainellib6e978e2014-07-21 15:29:22 -07004166 bcmgenet_umac_reset(priv);
4167
Doug Berger28c2d1a2017-10-25 15:04:13 -07004168 init_umac(priv);
Florian Fainellib6e978e2014-07-21 15:29:22 -07004169
Doug Berger6b6d017f2019-11-05 11:07:25 -08004170 phy_init_hw(dev->phydev);
4171
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02004172 /* Speed settings must be restored */
Doug Berger0686bd92019-11-05 11:07:26 -08004173 genphy_config_aneg(dev->phydev);
Florian Fainelli00d51092017-07-31 11:05:32 -07004174 bcmgenet_mii_config(priv->dev, false);
Florian Fainelli8c90db72014-07-21 15:29:28 -07004175
Doug Berger206f54b2019-12-17 16:51:12 -08004176 /* Restore enabled features */
4177 bcmgenet_set_features(dev, dev->features);
4178
Florian Fainellib6e978e2014-07-21 15:29:22 -07004179 bcmgenet_set_hw_addr(priv, dev->dev_addr);
4180
Doug Berger3e370952020-04-29 13:02:05 -07004181 offset = HFB_FLT_ENABLE_V3PLUS;
4182 bcmgenet_hfb_reg_writel(priv, priv->hfb_en[1], offset);
4183 bcmgenet_hfb_reg_writel(priv, priv->hfb_en[2], offset + sizeof(u32));
4184 bcmgenet_hfb_reg_writel(priv, priv->hfb_en[0], HFB_CTRL);
4185
Florian Fainellic624f892015-07-16 15:51:17 -07004186 if (priv->internal_phy) {
Florian Fainellib6e978e2014-07-21 15:29:22 -07004187 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
4188 reg |= EXT_ENERGY_DET_MASK;
4189 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
4190 }
4191
4192 /* Disable RX/TX DMA and flush TX queues */
4193 dma_ctrl = bcmgenet_dma_disable(priv);
4194
4195 /* Reinitialize TDMA and RDMA and SW housekeeping */
4196 ret = bcmgenet_init_dma(priv);
4197 if (ret) {
4198 netdev_err(dev, "failed to initialize DMA\n");
4199 goto out_clk_disable;
4200 }
4201
4202 /* Always enable ring 16 - descriptor ring */
4203 bcmgenet_enable_dma(priv, dma_ctrl);
4204
Florian Fainelli5371bbf42017-03-15 12:57:21 -07004205 if (!device_may_wakeup(d))
Doug Berger6c97f012017-10-25 15:04:19 -07004206 phy_resume(dev->phydev);
Florian Fainellicc013fb2014-08-11 14:50:43 -07004207
Florian Fainelli6ef398e2014-11-25 21:16:35 -08004208 if (priv->eee.eee_enabled)
4209 bcmgenet_eee_enable_set(dev, true);
4210
Florian Fainellib6e978e2014-07-21 15:29:22 -07004211 bcmgenet_netif_start(dev);
4212
Doug Berger09e805d2018-11-01 15:55:37 -07004213 netif_device_attach(dev);
4214
Florian Fainellib6e978e2014-07-21 15:29:22 -07004215 return 0;
4216
4217out_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08004218 if (priv->internal_phy)
4219 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainellib6e978e2014-07-21 15:29:22 -07004220 clk_disable_unprepare(priv->clk);
4221 return ret;
4222}
Doug Bergera94cbf02018-11-16 18:00:21 -08004223
4224static int bcmgenet_suspend(struct device *d)
4225{
4226 struct net_device *dev = dev_get_drvdata(d);
4227 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Berger3e370952020-04-29 13:02:05 -07004228 u32 offset;
Doug Bergera94cbf02018-11-16 18:00:21 -08004229
4230 if (!netif_running(dev))
4231 return 0;
4232
4233 netif_device_detach(dev);
4234
4235 bcmgenet_netif_stop(dev);
4236
4237 if (!device_may_wakeup(d))
4238 phy_suspend(dev->phydev);
4239
Doug Berger3e370952020-04-29 13:02:05 -07004240 /* Preserve filter state and disable filtering */
4241 priv->hfb_en[0] = bcmgenet_hfb_reg_readl(priv, HFB_CTRL);
4242 offset = HFB_FLT_ENABLE_V3PLUS;
4243 priv->hfb_en[1] = bcmgenet_hfb_reg_readl(priv, offset);
4244 priv->hfb_en[2] = bcmgenet_hfb_reg_readl(priv, offset + sizeof(u32));
4245 bcmgenet_hfb_reg_writel(priv, 0, HFB_CTRL);
4246
Doug Bergereb236c22020-04-30 16:26:51 -07004247 return 0;
4248}
4249
4250static int bcmgenet_suspend_noirq(struct device *d)
4251{
4252 struct net_device *dev = dev_get_drvdata(d);
4253 struct bcmgenet_priv *priv = netdev_priv(dev);
4254 int ret = 0;
4255
4256 if (!netif_running(dev))
4257 return 0;
4258
Doug Bergera94cbf02018-11-16 18:00:21 -08004259 /* Prepare the device for Wake-on-LAN and switch to the slow clock */
Doug Berger1a1d5102020-04-29 13:02:02 -07004260 if (device_may_wakeup(d) && priv->wolopts)
Doug Bergera94cbf02018-11-16 18:00:21 -08004261 ret = bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
Doug Berger1a1d5102020-04-29 13:02:02 -07004262 else if (priv->internal_phy)
Doug Bergera94cbf02018-11-16 18:00:21 -08004263 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Doug Bergera94cbf02018-11-16 18:00:21 -08004264
Doug Bergereb236c22020-04-30 16:26:51 -07004265 /* Let the framework handle resumption and leave the clocks on */
4266 if (ret)
4267 return ret;
4268
Doug Bergera94cbf02018-11-16 18:00:21 -08004269 /* Turn off the clocks */
4270 clk_disable_unprepare(priv->clk);
4271
Doug Bergereb236c22020-04-30 16:26:51 -07004272 return 0;
Doug Bergera94cbf02018-11-16 18:00:21 -08004273}
Doug Bergereb236c22020-04-30 16:26:51 -07004274#else
4275#define bcmgenet_suspend NULL
4276#define bcmgenet_suspend_noirq NULL
4277#define bcmgenet_resume NULL
4278#define bcmgenet_resume_noirq NULL
Florian Fainellib6e978e2014-07-21 15:29:22 -07004279#endif /* CONFIG_PM_SLEEP */
4280
Doug Bergereb236c22020-04-30 16:26:51 -07004281static const struct dev_pm_ops bcmgenet_pm_ops = {
4282 .suspend = bcmgenet_suspend,
4283 .suspend_noirq = bcmgenet_suspend_noirq,
4284 .resume = bcmgenet_resume,
4285 .resume_noirq = bcmgenet_resume_noirq,
4286};
Florian Fainellib6e978e2014-07-21 15:29:22 -07004287
Jeremy Linton99c6b062020-02-24 16:54:01 -06004288static const struct acpi_device_id genet_acpi_match[] = {
4289 { "BCM6E4E", (kernel_ulong_t)&bcm2711_plat_data },
4290 { },
4291};
4292MODULE_DEVICE_TABLE(acpi, genet_acpi_match);
4293
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004294static struct platform_driver bcmgenet_driver = {
4295 .probe = bcmgenet_probe,
4296 .remove = bcmgenet_remove,
Florian Fainellid9f45ab2019-10-15 10:36:24 -07004297 .shutdown = bcmgenet_shutdown,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004298 .driver = {
4299 .name = "bcmgenet",
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004300 .of_match_table = bcmgenet_match,
Florian Fainellib6e978e2014-07-21 15:29:22 -07004301 .pm = &bcmgenet_pm_ops,
Andy Shevchenkod4d9b472020-04-21 00:51:17 +03004302 .acpi_match_table = genet_acpi_match,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08004303 },
4304};
4305module_platform_driver(bcmgenet_driver);
4306
4307MODULE_AUTHOR("Broadcom Corporation");
4308MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
4309MODULE_ALIAS("platform:bcmgenet");
4310MODULE_LICENSE("GPL");