blob: 13e9154db25360916de8085bc08d1ed59e0972b5 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002/*
3 * Broadcom GENET (Gigabit Ethernet) controller driver
4 *
Doug Berger99d55632019-12-17 16:51:08 -08005 * Copyright (c) 2014-2019 Broadcom
Florian Fainelli1c1008c2014-02-13 16:08:47 -08006 */
7
8#define pr_fmt(fmt) "bcmgenet: " fmt
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/sched.h>
13#include <linux/types.h>
14#include <linux/fcntl.h>
15#include <linux/interrupt.h>
16#include <linux/string.h>
17#include <linux/if_ether.h>
18#include <linux/init.h>
19#include <linux/errno.h>
20#include <linux/delay.h>
21#include <linux/platform_device.h>
22#include <linux/dma-mapping.h>
23#include <linux/pm.h>
24#include <linux/clk.h>
Florian Fainelli1c1008c2014-02-13 16:08:47 -080025#include <linux/of.h>
26#include <linux/of_address.h>
27#include <linux/of_irq.h>
28#include <linux/of_net.h>
29#include <linux/of_platform.h>
30#include <net/arp.h>
31
32#include <linux/mii.h>
33#include <linux/ethtool.h>
34#include <linux/netdevice.h>
35#include <linux/inetdevice.h>
36#include <linux/etherdevice.h>
37#include <linux/skbuff.h>
38#include <linux/in.h>
39#include <linux/ip.h>
40#include <linux/ipv6.h>
41#include <linux/phy.h>
Petri Gyntherb0ba5122014-12-01 16:18:08 -080042#include <linux/platform_data/bcmgenet.h>
Florian Fainelli1c1008c2014-02-13 16:08:47 -080043
44#include <asm/unaligned.h>
45
46#include "bcmgenet.h"
47
48/* Maximum number of hardware queues, downsized if needed */
49#define GENET_MAX_MQ_CNT 4
50
51/* Default highest priority queue for multi queue support */
52#define GENET_Q0_PRIORITY 0
53
Petri Gynther3feafa02015-03-05 17:40:14 -080054#define GENET_Q16_RX_BD_CNT \
55 (TOTAL_DESC - priv->hw_params->rx_queues * priv->hw_params->rx_bds_per_q)
Petri Gynther51a966a2015-02-23 11:00:46 -080056#define GENET_Q16_TX_BD_CNT \
57 (TOTAL_DESC - priv->hw_params->tx_queues * priv->hw_params->tx_bds_per_q)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080058
59#define RX_BUF_LENGTH 2048
60#define SKB_ALIGNMENT 32
61
62/* Tx/Rx DMA register offset, skip 256 descriptors */
63#define WORDS_PER_BD(p) (p->hw_params->words_per_bd)
64#define DMA_DESC_SIZE (WORDS_PER_BD(priv) * sizeof(u32))
65
66#define GENET_TDMA_REG_OFF (priv->hw_params->tdma_offset + \
67 TOTAL_DESC * DMA_DESC_SIZE)
68
69#define GENET_RDMA_REG_OFF (priv->hw_params->rdma_offset + \
70 TOTAL_DESC * DMA_DESC_SIZE)
71
Florian Fainelli69d2ea92017-08-29 12:25:31 -070072static inline void bcmgenet_writel(u32 value, void __iomem *offset)
73{
74 /* MIPS chips strapped for BE will automagically configure the
75 * peripheral registers for CPU-native byte order.
76 */
77 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
78 __raw_writel(value, offset);
79 else
80 writel_relaxed(value, offset);
81}
82
83static inline u32 bcmgenet_readl(void __iomem *offset)
84{
85 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
86 return __raw_readl(offset);
87 else
88 return readl_relaxed(offset);
89}
90
Florian Fainelli1c1008c2014-02-13 16:08:47 -080091static inline void dmadesc_set_length_status(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -070092 void __iomem *d, u32 value)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080093{
Florian Fainelli69d2ea92017-08-29 12:25:31 -070094 bcmgenet_writel(value, d + DMA_DESC_LENGTH_STATUS);
Florian Fainelli1c1008c2014-02-13 16:08:47 -080095}
96
97static inline u32 dmadesc_get_length_status(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -070098 void __iomem *d)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080099{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700100 return bcmgenet_readl(d + DMA_DESC_LENGTH_STATUS);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800101}
102
103static inline void dmadesc_set_addr(struct bcmgenet_priv *priv,
104 void __iomem *d,
105 dma_addr_t addr)
106{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700107 bcmgenet_writel(lower_32_bits(addr), d + DMA_DESC_ADDRESS_LO);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800108
109 /* Register writes to GISB bus can take couple hundred nanoseconds
110 * and are done for each packet, save these expensive writes unless
Brian Norris7fc527f2014-07-29 14:34:14 -0700111 * the platform is explicitly configured for 64-bits/LPAE.
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800112 */
113#ifdef CONFIG_PHYS_ADDR_T_64BIT
114 if (priv->hw_params->flags & GENET_HAS_40BITS)
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700115 bcmgenet_writel(upper_32_bits(addr), d + DMA_DESC_ADDRESS_HI);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800116#endif
117}
118
119/* Combined address + length/status setter */
120static inline void dmadesc_set(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700121 void __iomem *d, dma_addr_t addr, u32 val)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800122{
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800123 dmadesc_set_addr(priv, d, addr);
Petri Gynther7ee40622016-04-05 14:00:01 -0700124 dmadesc_set_length_status(priv, d, val);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800125}
126
127static inline dma_addr_t dmadesc_get_addr(struct bcmgenet_priv *priv,
128 void __iomem *d)
129{
130 dma_addr_t addr;
131
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700132 addr = bcmgenet_readl(d + DMA_DESC_ADDRESS_LO);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800133
134 /* Register writes to GISB bus can take couple hundred nanoseconds
135 * and are done for each packet, save these expensive writes unless
Brian Norris7fc527f2014-07-29 14:34:14 -0700136 * the platform is explicitly configured for 64-bits/LPAE.
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800137 */
138#ifdef CONFIG_PHYS_ADDR_T_64BIT
139 if (priv->hw_params->flags & GENET_HAS_40BITS)
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700140 addr |= (u64)bcmgenet_readl(d + DMA_DESC_ADDRESS_HI) << 32;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800141#endif
142 return addr;
143}
144
145#define GENET_VER_FMT "%1d.%1d EPHY: 0x%04x"
146
147#define GENET_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
148 NETIF_MSG_LINK)
149
150static inline u32 bcmgenet_rbuf_ctrl_get(struct bcmgenet_priv *priv)
151{
152 if (GENET_IS_V1(priv))
153 return bcmgenet_rbuf_readl(priv, RBUF_FLUSH_CTRL_V1);
154 else
155 return bcmgenet_sys_readl(priv, SYS_RBUF_FLUSH_CTRL);
156}
157
158static inline void bcmgenet_rbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
159{
160 if (GENET_IS_V1(priv))
161 bcmgenet_rbuf_writel(priv, val, RBUF_FLUSH_CTRL_V1);
162 else
163 bcmgenet_sys_writel(priv, val, SYS_RBUF_FLUSH_CTRL);
164}
165
166/* These macros are defined to deal with register map change
167 * between GENET1.1 and GENET2. Only those currently being used
168 * by driver are defined.
169 */
170static inline u32 bcmgenet_tbuf_ctrl_get(struct bcmgenet_priv *priv)
171{
172 if (GENET_IS_V1(priv))
173 return bcmgenet_rbuf_readl(priv, TBUF_CTRL_V1);
174 else
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700175 return bcmgenet_readl(priv->base +
176 priv->hw_params->tbuf_offset + TBUF_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800177}
178
179static inline void bcmgenet_tbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
180{
181 if (GENET_IS_V1(priv))
182 bcmgenet_rbuf_writel(priv, val, TBUF_CTRL_V1);
183 else
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700184 bcmgenet_writel(val, priv->base +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800185 priv->hw_params->tbuf_offset + TBUF_CTRL);
186}
187
188static inline u32 bcmgenet_bp_mc_get(struct bcmgenet_priv *priv)
189{
190 if (GENET_IS_V1(priv))
191 return bcmgenet_rbuf_readl(priv, TBUF_BP_MC_V1);
192 else
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700193 return bcmgenet_readl(priv->base +
194 priv->hw_params->tbuf_offset + TBUF_BP_MC);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800195}
196
197static inline void bcmgenet_bp_mc_set(struct bcmgenet_priv *priv, u32 val)
198{
199 if (GENET_IS_V1(priv))
200 bcmgenet_rbuf_writel(priv, val, TBUF_BP_MC_V1);
201 else
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700202 bcmgenet_writel(val, priv->base +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800203 priv->hw_params->tbuf_offset + TBUF_BP_MC);
204}
205
206/* RX/TX DMA register accessors */
207enum dma_reg {
208 DMA_RING_CFG = 0,
209 DMA_CTRL,
210 DMA_STATUS,
211 DMA_SCB_BURST_SIZE,
212 DMA_ARB_CTRL,
Petri Gynther37742162014-10-07 09:30:01 -0700213 DMA_PRIORITY_0,
214 DMA_PRIORITY_1,
215 DMA_PRIORITY_2,
Petri Gynther0034de42015-03-13 14:45:00 -0700216 DMA_INDEX2RING_0,
217 DMA_INDEX2RING_1,
218 DMA_INDEX2RING_2,
219 DMA_INDEX2RING_3,
220 DMA_INDEX2RING_4,
221 DMA_INDEX2RING_5,
222 DMA_INDEX2RING_6,
223 DMA_INDEX2RING_7,
Florian Fainelli4a296452015-09-16 16:47:40 -0700224 DMA_RING0_TIMEOUT,
225 DMA_RING1_TIMEOUT,
226 DMA_RING2_TIMEOUT,
227 DMA_RING3_TIMEOUT,
228 DMA_RING4_TIMEOUT,
229 DMA_RING5_TIMEOUT,
230 DMA_RING6_TIMEOUT,
231 DMA_RING7_TIMEOUT,
232 DMA_RING8_TIMEOUT,
233 DMA_RING9_TIMEOUT,
234 DMA_RING10_TIMEOUT,
235 DMA_RING11_TIMEOUT,
236 DMA_RING12_TIMEOUT,
237 DMA_RING13_TIMEOUT,
238 DMA_RING14_TIMEOUT,
239 DMA_RING15_TIMEOUT,
240 DMA_RING16_TIMEOUT,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800241};
242
243static const u8 bcmgenet_dma_regs_v3plus[] = {
244 [DMA_RING_CFG] = 0x00,
245 [DMA_CTRL] = 0x04,
246 [DMA_STATUS] = 0x08,
247 [DMA_SCB_BURST_SIZE] = 0x0C,
248 [DMA_ARB_CTRL] = 0x2C,
Petri Gynther37742162014-10-07 09:30:01 -0700249 [DMA_PRIORITY_0] = 0x30,
250 [DMA_PRIORITY_1] = 0x34,
251 [DMA_PRIORITY_2] = 0x38,
Florian Fainelli4a296452015-09-16 16:47:40 -0700252 [DMA_RING0_TIMEOUT] = 0x2C,
253 [DMA_RING1_TIMEOUT] = 0x30,
254 [DMA_RING2_TIMEOUT] = 0x34,
255 [DMA_RING3_TIMEOUT] = 0x38,
256 [DMA_RING4_TIMEOUT] = 0x3c,
257 [DMA_RING5_TIMEOUT] = 0x40,
258 [DMA_RING6_TIMEOUT] = 0x44,
259 [DMA_RING7_TIMEOUT] = 0x48,
260 [DMA_RING8_TIMEOUT] = 0x4c,
261 [DMA_RING9_TIMEOUT] = 0x50,
262 [DMA_RING10_TIMEOUT] = 0x54,
263 [DMA_RING11_TIMEOUT] = 0x58,
264 [DMA_RING12_TIMEOUT] = 0x5c,
265 [DMA_RING13_TIMEOUT] = 0x60,
266 [DMA_RING14_TIMEOUT] = 0x64,
267 [DMA_RING15_TIMEOUT] = 0x68,
268 [DMA_RING16_TIMEOUT] = 0x6C,
Petri Gynther0034de42015-03-13 14:45:00 -0700269 [DMA_INDEX2RING_0] = 0x70,
270 [DMA_INDEX2RING_1] = 0x74,
271 [DMA_INDEX2RING_2] = 0x78,
272 [DMA_INDEX2RING_3] = 0x7C,
273 [DMA_INDEX2RING_4] = 0x80,
274 [DMA_INDEX2RING_5] = 0x84,
275 [DMA_INDEX2RING_6] = 0x88,
276 [DMA_INDEX2RING_7] = 0x8C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800277};
278
279static const u8 bcmgenet_dma_regs_v2[] = {
280 [DMA_RING_CFG] = 0x00,
281 [DMA_CTRL] = 0x04,
282 [DMA_STATUS] = 0x08,
283 [DMA_SCB_BURST_SIZE] = 0x0C,
284 [DMA_ARB_CTRL] = 0x30,
Petri Gynther37742162014-10-07 09:30:01 -0700285 [DMA_PRIORITY_0] = 0x34,
286 [DMA_PRIORITY_1] = 0x38,
287 [DMA_PRIORITY_2] = 0x3C,
Florian Fainelli4a296452015-09-16 16:47:40 -0700288 [DMA_RING0_TIMEOUT] = 0x2C,
289 [DMA_RING1_TIMEOUT] = 0x30,
290 [DMA_RING2_TIMEOUT] = 0x34,
291 [DMA_RING3_TIMEOUT] = 0x38,
292 [DMA_RING4_TIMEOUT] = 0x3c,
293 [DMA_RING5_TIMEOUT] = 0x40,
294 [DMA_RING6_TIMEOUT] = 0x44,
295 [DMA_RING7_TIMEOUT] = 0x48,
296 [DMA_RING8_TIMEOUT] = 0x4c,
297 [DMA_RING9_TIMEOUT] = 0x50,
298 [DMA_RING10_TIMEOUT] = 0x54,
299 [DMA_RING11_TIMEOUT] = 0x58,
300 [DMA_RING12_TIMEOUT] = 0x5c,
301 [DMA_RING13_TIMEOUT] = 0x60,
302 [DMA_RING14_TIMEOUT] = 0x64,
303 [DMA_RING15_TIMEOUT] = 0x68,
304 [DMA_RING16_TIMEOUT] = 0x6C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800305};
306
307static const u8 bcmgenet_dma_regs_v1[] = {
308 [DMA_CTRL] = 0x00,
309 [DMA_STATUS] = 0x04,
310 [DMA_SCB_BURST_SIZE] = 0x0C,
311 [DMA_ARB_CTRL] = 0x30,
Petri Gynther37742162014-10-07 09:30:01 -0700312 [DMA_PRIORITY_0] = 0x34,
313 [DMA_PRIORITY_1] = 0x38,
314 [DMA_PRIORITY_2] = 0x3C,
Florian Fainelli4a296452015-09-16 16:47:40 -0700315 [DMA_RING0_TIMEOUT] = 0x2C,
316 [DMA_RING1_TIMEOUT] = 0x30,
317 [DMA_RING2_TIMEOUT] = 0x34,
318 [DMA_RING3_TIMEOUT] = 0x38,
319 [DMA_RING4_TIMEOUT] = 0x3c,
320 [DMA_RING5_TIMEOUT] = 0x40,
321 [DMA_RING6_TIMEOUT] = 0x44,
322 [DMA_RING7_TIMEOUT] = 0x48,
323 [DMA_RING8_TIMEOUT] = 0x4c,
324 [DMA_RING9_TIMEOUT] = 0x50,
325 [DMA_RING10_TIMEOUT] = 0x54,
326 [DMA_RING11_TIMEOUT] = 0x58,
327 [DMA_RING12_TIMEOUT] = 0x5c,
328 [DMA_RING13_TIMEOUT] = 0x60,
329 [DMA_RING14_TIMEOUT] = 0x64,
330 [DMA_RING15_TIMEOUT] = 0x68,
331 [DMA_RING16_TIMEOUT] = 0x6C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800332};
333
334/* Set at runtime once bcmgenet version is known */
335static const u8 *bcmgenet_dma_regs;
336
337static inline struct bcmgenet_priv *dev_to_priv(struct device *dev)
338{
339 return netdev_priv(dev_get_drvdata(dev));
340}
341
342static inline u32 bcmgenet_tdma_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700343 enum dma_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800344{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700345 return bcmgenet_readl(priv->base + GENET_TDMA_REG_OFF +
346 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800347}
348
349static inline void bcmgenet_tdma_writel(struct bcmgenet_priv *priv,
350 u32 val, enum dma_reg r)
351{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700352 bcmgenet_writel(val, priv->base + GENET_TDMA_REG_OFF +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800353 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
354}
355
356static inline u32 bcmgenet_rdma_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700357 enum dma_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800358{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700359 return bcmgenet_readl(priv->base + GENET_RDMA_REG_OFF +
360 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800361}
362
363static inline void bcmgenet_rdma_writel(struct bcmgenet_priv *priv,
364 u32 val, enum dma_reg r)
365{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700366 bcmgenet_writel(val, priv->base + GENET_RDMA_REG_OFF +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800367 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
368}
369
370/* RDMA/TDMA ring registers and accessors
371 * we merge the common fields and just prefix with T/D the registers
372 * having different meaning depending on the direction
373 */
374enum dma_ring_reg {
375 TDMA_READ_PTR = 0,
376 RDMA_WRITE_PTR = TDMA_READ_PTR,
377 TDMA_READ_PTR_HI,
378 RDMA_WRITE_PTR_HI = TDMA_READ_PTR_HI,
379 TDMA_CONS_INDEX,
380 RDMA_PROD_INDEX = TDMA_CONS_INDEX,
381 TDMA_PROD_INDEX,
382 RDMA_CONS_INDEX = TDMA_PROD_INDEX,
383 DMA_RING_BUF_SIZE,
384 DMA_START_ADDR,
385 DMA_START_ADDR_HI,
386 DMA_END_ADDR,
387 DMA_END_ADDR_HI,
388 DMA_MBUF_DONE_THRESH,
389 TDMA_FLOW_PERIOD,
390 RDMA_XON_XOFF_THRESH = TDMA_FLOW_PERIOD,
391 TDMA_WRITE_PTR,
392 RDMA_READ_PTR = TDMA_WRITE_PTR,
393 TDMA_WRITE_PTR_HI,
394 RDMA_READ_PTR_HI = TDMA_WRITE_PTR_HI
395};
396
397/* GENET v4 supports 40-bits pointer addressing
398 * for obvious reasons the LO and HI word parts
399 * are contiguous, but this offsets the other
400 * registers.
401 */
402static const u8 genet_dma_ring_regs_v4[] = {
403 [TDMA_READ_PTR] = 0x00,
404 [TDMA_READ_PTR_HI] = 0x04,
405 [TDMA_CONS_INDEX] = 0x08,
406 [TDMA_PROD_INDEX] = 0x0C,
407 [DMA_RING_BUF_SIZE] = 0x10,
408 [DMA_START_ADDR] = 0x14,
409 [DMA_START_ADDR_HI] = 0x18,
410 [DMA_END_ADDR] = 0x1C,
411 [DMA_END_ADDR_HI] = 0x20,
412 [DMA_MBUF_DONE_THRESH] = 0x24,
413 [TDMA_FLOW_PERIOD] = 0x28,
414 [TDMA_WRITE_PTR] = 0x2C,
415 [TDMA_WRITE_PTR_HI] = 0x30,
416};
417
418static const u8 genet_dma_ring_regs_v123[] = {
419 [TDMA_READ_PTR] = 0x00,
420 [TDMA_CONS_INDEX] = 0x04,
421 [TDMA_PROD_INDEX] = 0x08,
422 [DMA_RING_BUF_SIZE] = 0x0C,
423 [DMA_START_ADDR] = 0x10,
424 [DMA_END_ADDR] = 0x14,
425 [DMA_MBUF_DONE_THRESH] = 0x18,
426 [TDMA_FLOW_PERIOD] = 0x1C,
427 [TDMA_WRITE_PTR] = 0x20,
428};
429
430/* Set at runtime once GENET version is known */
431static const u8 *genet_dma_ring_regs;
432
433static inline u32 bcmgenet_tdma_ring_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700434 unsigned int ring,
435 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800436{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700437 return bcmgenet_readl(priv->base + GENET_TDMA_REG_OFF +
438 (DMA_RING_SIZE * ring) +
439 genet_dma_ring_regs[r]);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800440}
441
442static inline void bcmgenet_tdma_ring_writel(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700443 unsigned int ring, u32 val,
444 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800445{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700446 bcmgenet_writel(val, priv->base + GENET_TDMA_REG_OFF +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800447 (DMA_RING_SIZE * ring) +
448 genet_dma_ring_regs[r]);
449}
450
451static inline u32 bcmgenet_rdma_ring_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700452 unsigned int ring,
453 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800454{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700455 return bcmgenet_readl(priv->base + GENET_RDMA_REG_OFF +
456 (DMA_RING_SIZE * ring) +
457 genet_dma_ring_regs[r]);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800458}
459
460static inline void bcmgenet_rdma_ring_writel(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700461 unsigned int ring, u32 val,
462 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800463{
Florian Fainelli69d2ea92017-08-29 12:25:31 -0700464 bcmgenet_writel(val, priv->base + GENET_RDMA_REG_OFF +
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800465 (DMA_RING_SIZE * ring) +
466 genet_dma_ring_regs[r]);
467}
468
Edwin Chan89316fa2017-03-09 16:58:49 -0800469static int bcmgenet_begin(struct net_device *dev)
470{
471 struct bcmgenet_priv *priv = netdev_priv(dev);
472
473 /* Turn on the clock */
474 return clk_prepare_enable(priv->clk);
475}
476
477static void bcmgenet_complete(struct net_device *dev)
478{
479 struct bcmgenet_priv *priv = netdev_priv(dev);
480
481 /* Turn off the clock */
482 clk_disable_unprepare(priv->clk);
483}
484
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200485static int bcmgenet_get_link_ksettings(struct net_device *dev,
486 struct ethtool_link_ksettings *cmd)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200487{
488 if (!netif_running(dev))
489 return -EINVAL;
490
Doug Berger6c97f012017-10-25 15:04:19 -0700491 if (!dev->phydev)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200492 return -ENODEV;
493
Doug Berger6c97f012017-10-25 15:04:19 -0700494 phy_ethtool_ksettings_get(dev->phydev, cmd);
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +0300495
496 return 0;
Philippe Reynesbac65c42016-07-09 00:54:47 +0200497}
498
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200499static int bcmgenet_set_link_ksettings(struct net_device *dev,
500 const struct ethtool_link_ksettings *cmd)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200501{
502 if (!netif_running(dev))
503 return -EINVAL;
504
Doug Berger6c97f012017-10-25 15:04:19 -0700505 if (!dev->phydev)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200506 return -ENODEV;
507
Doug Berger6c97f012017-10-25 15:04:19 -0700508 return phy_ethtool_ksettings_set(dev->phydev, cmd);
Philippe Reynesbac65c42016-07-09 00:54:47 +0200509}
510
Doug Bergerf63db4e2019-12-17 16:51:11 -0800511static void bcmgenet_set_rx_csum(struct net_device *dev,
512 netdev_features_t wanted)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800513{
514 struct bcmgenet_priv *priv = netdev_priv(dev);
515 u32 rbuf_chk_ctrl;
516 bool rx_csum_en;
517
518 rx_csum_en = !!(wanted & NETIF_F_RXCSUM);
519
520 rbuf_chk_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CHK_CTRL);
521
522 /* enable rx checksumming */
523 if (rx_csum_en)
Doug Berger81015532019-12-17 16:51:10 -0800524 rbuf_chk_ctrl |= RBUF_RXCHK_EN | RBUF_L3_PARSE_DIS;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800525 else
526 rbuf_chk_ctrl &= ~RBUF_RXCHK_EN;
527 priv->desc_rxchk_en = rx_csum_en;
Florian Fainelliebe5e3c2014-03-26 21:18:39 -0700528
529 /* If UniMAC forwards CRC, we need to skip over it to get
530 * a valid CHK bit to be set in the per-packet status word
531 */
532 if (rx_csum_en && priv->crc_fwd_en)
533 rbuf_chk_ctrl |= RBUF_SKIP_FCS;
534 else
535 rbuf_chk_ctrl &= ~RBUF_SKIP_FCS;
536
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800537 bcmgenet_rbuf_writel(priv, rbuf_chk_ctrl, RBUF_CHK_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800538}
539
Doug Bergerf63db4e2019-12-17 16:51:11 -0800540static void bcmgenet_set_tx_csum(struct net_device *dev,
541 netdev_features_t wanted)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800542{
543 struct bcmgenet_priv *priv = netdev_priv(dev);
544 bool desc_64b_en;
545 u32 tbuf_ctrl, rbuf_ctrl;
546
547 tbuf_ctrl = bcmgenet_tbuf_ctrl_get(priv);
548 rbuf_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
549
Doug Bergerdd8e9112019-12-17 16:51:09 -0800550 desc_64b_en = !!(wanted & NETIF_F_HW_CSUM);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800551
552 /* enable 64 bytes descriptor in both directions (RBUF and TBUF) */
553 if (desc_64b_en) {
554 tbuf_ctrl |= RBUF_64B_EN;
555 rbuf_ctrl |= RBUF_64B_EN;
556 } else {
557 tbuf_ctrl &= ~RBUF_64B_EN;
558 rbuf_ctrl &= ~RBUF_64B_EN;
559 }
560 priv->desc_64b_en = desc_64b_en;
561
562 bcmgenet_tbuf_ctrl_set(priv, tbuf_ctrl);
563 bcmgenet_rbuf_writel(priv, rbuf_ctrl, RBUF_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800564}
565
566static int bcmgenet_set_features(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700567 netdev_features_t features)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800568{
Doug Bergerf63db4e2019-12-17 16:51:11 -0800569 struct bcmgenet_priv *priv = netdev_priv(dev);
570 u32 reg;
571 int ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800572
Doug Bergerf63db4e2019-12-17 16:51:11 -0800573 ret = clk_prepare_enable(priv->clk);
574 if (ret)
575 return ret;
576
577 /* Make sure we reflect the value of CRC_CMD_FWD */
578 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
579 priv->crc_fwd_en = !!(reg & CMD_CRC_FWD);
580
581 bcmgenet_set_tx_csum(dev, features);
582 bcmgenet_set_rx_csum(dev, features);
583
584 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800585
586 return ret;
587}
588
589static u32 bcmgenet_get_msglevel(struct net_device *dev)
590{
591 struct bcmgenet_priv *priv = netdev_priv(dev);
592
593 return priv->msg_enable;
594}
595
596static void bcmgenet_set_msglevel(struct net_device *dev, u32 level)
597{
598 struct bcmgenet_priv *priv = netdev_priv(dev);
599
600 priv->msg_enable = level;
601}
602
Florian Fainelli2f913072015-09-16 16:47:39 -0700603static int bcmgenet_get_coalesce(struct net_device *dev,
604 struct ethtool_coalesce *ec)
605{
606 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700607 struct bcmgenet_rx_ring *ring;
608 unsigned int i;
Florian Fainelli2f913072015-09-16 16:47:39 -0700609
610 ec->tx_max_coalesced_frames =
611 bcmgenet_tdma_ring_readl(priv, DESC_INDEX,
612 DMA_MBUF_DONE_THRESH);
Florian Fainelli4a296452015-09-16 16:47:40 -0700613 ec->rx_max_coalesced_frames =
614 bcmgenet_rdma_ring_readl(priv, DESC_INDEX,
615 DMA_MBUF_DONE_THRESH);
616 ec->rx_coalesce_usecs =
617 bcmgenet_rdma_readl(priv, DMA_RING16_TIMEOUT) * 8192 / 1000;
Florian Fainelli2f913072015-09-16 16:47:39 -0700618
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700619 for (i = 0; i < priv->hw_params->rx_queues; i++) {
620 ring = &priv->rx_rings[i];
621 ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
622 }
623 ring = &priv->rx_rings[DESC_INDEX];
624 ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
625
Florian Fainelli2f913072015-09-16 16:47:39 -0700626 return 0;
627}
628
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700629static void bcmgenet_set_rx_coalesce(struct bcmgenet_rx_ring *ring,
630 u32 usecs, u32 pkts)
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700631{
632 struct bcmgenet_priv *priv = ring->priv;
633 unsigned int i = ring->index;
634 u32 reg;
635
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700636 bcmgenet_rdma_ring_writel(priv, i, pkts, DMA_MBUF_DONE_THRESH);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700637
638 reg = bcmgenet_rdma_readl(priv, DMA_RING0_TIMEOUT + i);
639 reg &= ~DMA_TIMEOUT_MASK;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700640 reg |= DIV_ROUND_UP(usecs * 1000, 8192);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700641 bcmgenet_rdma_writel(priv, reg, DMA_RING0_TIMEOUT + i);
642}
643
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700644static void bcmgenet_set_ring_rx_coalesce(struct bcmgenet_rx_ring *ring,
645 struct ethtool_coalesce *ec)
646{
Tal Gilboa8960b382019-01-31 16:44:48 +0200647 struct dim_cq_moder moder;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700648 u32 usecs, pkts;
649
650 ring->rx_coalesce_usecs = ec->rx_coalesce_usecs;
651 ring->rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
652 usecs = ring->rx_coalesce_usecs;
653 pkts = ring->rx_max_coalesced_frames;
654
655 if (ec->use_adaptive_rx_coalesce && !ring->dim.use_dim) {
Tal Gilboa026a8072018-04-24 13:36:01 +0300656 moder = net_dim_get_def_rx_moderation(ring->dim.dim.mode);
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700657 usecs = moder.usec;
658 pkts = moder.pkts;
659 }
660
661 ring->dim.use_dim = ec->use_adaptive_rx_coalesce;
662 bcmgenet_set_rx_coalesce(ring, usecs, pkts);
663}
664
Florian Fainelli2f913072015-09-16 16:47:39 -0700665static int bcmgenet_set_coalesce(struct net_device *dev,
666 struct ethtool_coalesce *ec)
667{
668 struct bcmgenet_priv *priv = netdev_priv(dev);
669 unsigned int i;
670
Florian Fainelli4a296452015-09-16 16:47:40 -0700671 /* Base system clock is 125Mhz, DMA timeout is this reference clock
672 * divided by 1024, which yields roughly 8.192us, our maximum value
673 * has to fit in the DMA_TIMEOUT_MASK (16 bits)
674 */
Florian Fainelli2f913072015-09-16 16:47:39 -0700675 if (ec->tx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
Florian Fainelli4a296452015-09-16 16:47:40 -0700676 ec->tx_max_coalesced_frames == 0 ||
677 ec->rx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
678 ec->rx_coalesce_usecs > (DMA_TIMEOUT_MASK * 8) + 1)
679 return -EINVAL;
680
681 if (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0)
Florian Fainelli2f913072015-09-16 16:47:39 -0700682 return -EINVAL;
683
684 /* GENET TDMA hardware does not support a configurable timeout, but will
685 * always generate an interrupt either after MBDONE packets have been
Doug Berger556c2cf2017-03-13 17:41:34 -0700686 * transmitted, or when the ring is empty.
Florian Fainelli2f913072015-09-16 16:47:39 -0700687 */
688 if (ec->tx_coalesce_usecs || ec->tx_coalesce_usecs_high ||
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700689 ec->tx_coalesce_usecs_irq || ec->tx_coalesce_usecs_low ||
690 ec->use_adaptive_tx_coalesce)
Florian Fainelli2f913072015-09-16 16:47:39 -0700691 return -EOPNOTSUPP;
692
693 /* Program all TX queues with the same values, as there is no
694 * ethtool knob to do coalescing on a per-queue basis
695 */
696 for (i = 0; i < priv->hw_params->tx_queues; i++)
697 bcmgenet_tdma_ring_writel(priv, i,
698 ec->tx_max_coalesced_frames,
699 DMA_MBUF_DONE_THRESH);
700 bcmgenet_tdma_ring_writel(priv, DESC_INDEX,
701 ec->tx_max_coalesced_frames,
702 DMA_MBUF_DONE_THRESH);
703
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700704 for (i = 0; i < priv->hw_params->rx_queues; i++)
705 bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[i], ec);
706 bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[DESC_INDEX], ec);
Florian Fainelli4a296452015-09-16 16:47:40 -0700707
Florian Fainelli2f913072015-09-16 16:47:39 -0700708 return 0;
709}
710
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800711/* standard ethtool support functions. */
712enum bcmgenet_stat_type {
713 BCMGENET_STAT_NETDEV = -1,
714 BCMGENET_STAT_MIB_RX,
715 BCMGENET_STAT_MIB_TX,
716 BCMGENET_STAT_RUNT,
717 BCMGENET_STAT_MISC,
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800718 BCMGENET_STAT_SOFT,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800719};
720
721struct bcmgenet_stats {
722 char stat_string[ETH_GSTRING_LEN];
723 int stat_sizeof;
724 int stat_offset;
725 enum bcmgenet_stat_type type;
726 /* reg offset from UMAC base for misc counters */
727 u16 reg_offset;
728};
729
730#define STAT_NETDEV(m) { \
731 .stat_string = __stringify(m), \
732 .stat_sizeof = sizeof(((struct net_device_stats *)0)->m), \
733 .stat_offset = offsetof(struct net_device_stats, m), \
734 .type = BCMGENET_STAT_NETDEV, \
735}
736
737#define STAT_GENET_MIB(str, m, _type) { \
738 .stat_string = str, \
739 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
740 .stat_offset = offsetof(struct bcmgenet_priv, m), \
741 .type = _type, \
742}
743
744#define STAT_GENET_MIB_RX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_RX)
745#define STAT_GENET_MIB_TX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_TX)
746#define STAT_GENET_RUNT(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_RUNT)
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800747#define STAT_GENET_SOFT_MIB(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_SOFT)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800748
749#define STAT_GENET_MISC(str, m, offset) { \
750 .stat_string = str, \
751 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
752 .stat_offset = offsetof(struct bcmgenet_priv, m), \
753 .type = BCMGENET_STAT_MISC, \
754 .reg_offset = offset, \
755}
756
Florian Fainelli37a30b42017-03-16 10:27:08 -0700757#define STAT_GENET_Q(num) \
758 STAT_GENET_SOFT_MIB("txq" __stringify(num) "_packets", \
759 tx_rings[num].packets), \
760 STAT_GENET_SOFT_MIB("txq" __stringify(num) "_bytes", \
761 tx_rings[num].bytes), \
762 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_bytes", \
763 rx_rings[num].bytes), \
764 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_packets", \
765 rx_rings[num].packets), \
766 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_errors", \
767 rx_rings[num].errors), \
768 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_dropped", \
769 rx_rings[num].dropped)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800770
771/* There is a 0xC gap between the end of RX and beginning of TX stats and then
772 * between the end of TX stats and the beginning of the RX RUNT
773 */
774#define BCMGENET_STAT_OFFSET 0xc
775
776/* Hardware counters must be kept in sync because the order/offset
777 * is important here (order in structure declaration = order in hardware)
778 */
779static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
780 /* general stats */
781 STAT_NETDEV(rx_packets),
782 STAT_NETDEV(tx_packets),
783 STAT_NETDEV(rx_bytes),
784 STAT_NETDEV(tx_bytes),
785 STAT_NETDEV(rx_errors),
786 STAT_NETDEV(tx_errors),
787 STAT_NETDEV(rx_dropped),
788 STAT_NETDEV(tx_dropped),
789 STAT_NETDEV(multicast),
790 /* UniMAC RSV counters */
791 STAT_GENET_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
792 STAT_GENET_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
793 STAT_GENET_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
794 STAT_GENET_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
795 STAT_GENET_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
796 STAT_GENET_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
797 STAT_GENET_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
798 STAT_GENET_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
799 STAT_GENET_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
800 STAT_GENET_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
801 STAT_GENET_MIB_RX("rx_pkts", mib.rx.pkt),
802 STAT_GENET_MIB_RX("rx_bytes", mib.rx.bytes),
803 STAT_GENET_MIB_RX("rx_multicast", mib.rx.mca),
804 STAT_GENET_MIB_RX("rx_broadcast", mib.rx.bca),
805 STAT_GENET_MIB_RX("rx_fcs", mib.rx.fcs),
806 STAT_GENET_MIB_RX("rx_control", mib.rx.cf),
807 STAT_GENET_MIB_RX("rx_pause", mib.rx.pf),
808 STAT_GENET_MIB_RX("rx_unknown", mib.rx.uo),
809 STAT_GENET_MIB_RX("rx_align", mib.rx.aln),
810 STAT_GENET_MIB_RX("rx_outrange", mib.rx.flr),
811 STAT_GENET_MIB_RX("rx_code", mib.rx.cde),
812 STAT_GENET_MIB_RX("rx_carrier", mib.rx.fcr),
813 STAT_GENET_MIB_RX("rx_oversize", mib.rx.ovr),
814 STAT_GENET_MIB_RX("rx_jabber", mib.rx.jbr),
815 STAT_GENET_MIB_RX("rx_mtu_err", mib.rx.mtue),
816 STAT_GENET_MIB_RX("rx_good_pkts", mib.rx.pok),
817 STAT_GENET_MIB_RX("rx_unicast", mib.rx.uc),
818 STAT_GENET_MIB_RX("rx_ppp", mib.rx.ppp),
819 STAT_GENET_MIB_RX("rx_crc", mib.rx.rcrc),
820 /* UniMAC TSV counters */
821 STAT_GENET_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
822 STAT_GENET_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
823 STAT_GENET_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
824 STAT_GENET_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
825 STAT_GENET_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
826 STAT_GENET_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
827 STAT_GENET_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
828 STAT_GENET_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
829 STAT_GENET_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
830 STAT_GENET_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
831 STAT_GENET_MIB_TX("tx_pkts", mib.tx.pkts),
832 STAT_GENET_MIB_TX("tx_multicast", mib.tx.mca),
833 STAT_GENET_MIB_TX("tx_broadcast", mib.tx.bca),
834 STAT_GENET_MIB_TX("tx_pause", mib.tx.pf),
835 STAT_GENET_MIB_TX("tx_control", mib.tx.cf),
836 STAT_GENET_MIB_TX("tx_fcs_err", mib.tx.fcs),
837 STAT_GENET_MIB_TX("tx_oversize", mib.tx.ovr),
838 STAT_GENET_MIB_TX("tx_defer", mib.tx.drf),
839 STAT_GENET_MIB_TX("tx_excess_defer", mib.tx.edf),
840 STAT_GENET_MIB_TX("tx_single_col", mib.tx.scl),
841 STAT_GENET_MIB_TX("tx_multi_col", mib.tx.mcl),
842 STAT_GENET_MIB_TX("tx_late_col", mib.tx.lcl),
843 STAT_GENET_MIB_TX("tx_excess_col", mib.tx.ecl),
844 STAT_GENET_MIB_TX("tx_frags", mib.tx.frg),
845 STAT_GENET_MIB_TX("tx_total_col", mib.tx.ncl),
846 STAT_GENET_MIB_TX("tx_jabber", mib.tx.jbr),
847 STAT_GENET_MIB_TX("tx_bytes", mib.tx.bytes),
848 STAT_GENET_MIB_TX("tx_good_pkts", mib.tx.pok),
849 STAT_GENET_MIB_TX("tx_unicast", mib.tx.uc),
850 /* UniMAC RUNT counters */
851 STAT_GENET_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
852 STAT_GENET_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
853 STAT_GENET_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
854 STAT_GENET_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
855 /* Misc UniMAC counters */
856 STAT_GENET_MISC("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt,
Doug Bergerffff7132017-03-09 16:58:43 -0800857 UMAC_RBUF_OVFL_CNT_V1),
858 STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt,
859 UMAC_RBUF_ERR_CNT_V1),
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800860 STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT),
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800861 STAT_GENET_SOFT_MIB("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
862 STAT_GENET_SOFT_MIB("rx_dma_failed", mib.rx_dma_failed),
863 STAT_GENET_SOFT_MIB("tx_dma_failed", mib.tx_dma_failed),
Florian Fainelli37a30b42017-03-16 10:27:08 -0700864 /* Per TX queues */
865 STAT_GENET_Q(0),
866 STAT_GENET_Q(1),
867 STAT_GENET_Q(2),
868 STAT_GENET_Q(3),
869 STAT_GENET_Q(16),
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800870};
871
872#define BCMGENET_STATS_LEN ARRAY_SIZE(bcmgenet_gstrings_stats)
873
874static void bcmgenet_get_drvinfo(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700875 struct ethtool_drvinfo *info)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800876{
877 strlcpy(info->driver, "bcmgenet", sizeof(info->driver));
878 strlcpy(info->version, "v2.0", sizeof(info->version));
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800879}
880
881static int bcmgenet_get_sset_count(struct net_device *dev, int string_set)
882{
883 switch (string_set) {
884 case ETH_SS_STATS:
885 return BCMGENET_STATS_LEN;
886 default:
887 return -EOPNOTSUPP;
888 }
889}
890
Florian Fainellic91b7f62014-07-23 10:42:12 -0700891static void bcmgenet_get_strings(struct net_device *dev, u32 stringset,
892 u8 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800893{
894 int i;
895
896 switch (stringset) {
897 case ETH_SS_STATS:
898 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
899 memcpy(data + i * ETH_GSTRING_LEN,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700900 bcmgenet_gstrings_stats[i].stat_string,
901 ETH_GSTRING_LEN);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800902 }
903 break;
904 }
905}
906
Doug Bergerffff7132017-03-09 16:58:43 -0800907static u32 bcmgenet_update_stat_misc(struct bcmgenet_priv *priv, u16 offset)
908{
909 u16 new_offset;
910 u32 val;
911
912 switch (offset) {
913 case UMAC_RBUF_OVFL_CNT_V1:
914 if (GENET_IS_V2(priv))
915 new_offset = RBUF_OVFL_CNT_V2;
916 else
917 new_offset = RBUF_OVFL_CNT_V3PLUS;
918
919 val = bcmgenet_rbuf_readl(priv, new_offset);
920 /* clear if overflowed */
921 if (val == ~0)
922 bcmgenet_rbuf_writel(priv, 0, new_offset);
923 break;
924 case UMAC_RBUF_ERR_CNT_V1:
925 if (GENET_IS_V2(priv))
926 new_offset = RBUF_ERR_CNT_V2;
927 else
928 new_offset = RBUF_ERR_CNT_V3PLUS;
929
930 val = bcmgenet_rbuf_readl(priv, new_offset);
931 /* clear if overflowed */
932 if (val == ~0)
933 bcmgenet_rbuf_writel(priv, 0, new_offset);
934 break;
935 default:
936 val = bcmgenet_umac_readl(priv, offset);
937 /* clear if overflowed */
938 if (val == ~0)
939 bcmgenet_umac_writel(priv, 0, offset);
940 break;
941 }
942
943 return val;
944}
945
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800946static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv)
947{
948 int i, j = 0;
949
950 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
951 const struct bcmgenet_stats *s;
952 u8 offset = 0;
953 u32 val = 0;
954 char *p;
955
956 s = &bcmgenet_gstrings_stats[i];
957 switch (s->type) {
958 case BCMGENET_STAT_NETDEV:
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800959 case BCMGENET_STAT_SOFT:
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800960 continue;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800961 case BCMGENET_STAT_RUNT:
Doug Berger1ad3d222017-03-09 16:58:44 -0800962 offset += BCMGENET_STAT_OFFSET;
963 /* fall through */
964 case BCMGENET_STAT_MIB_TX:
965 offset += BCMGENET_STAT_OFFSET;
966 /* fall through */
967 case BCMGENET_STAT_MIB_RX:
Florian Fainellic91b7f62014-07-23 10:42:12 -0700968 val = bcmgenet_umac_readl(priv,
969 UMAC_MIB_START + j + offset);
Doug Berger1ad3d222017-03-09 16:58:44 -0800970 offset = 0; /* Reset Offset */
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800971 break;
972 case BCMGENET_STAT_MISC:
Doug Bergerffff7132017-03-09 16:58:43 -0800973 if (GENET_IS_V1(priv)) {
974 val = bcmgenet_umac_readl(priv, s->reg_offset);
975 /* clear if overflowed */
976 if (val == ~0)
977 bcmgenet_umac_writel(priv, 0,
978 s->reg_offset);
979 } else {
980 val = bcmgenet_update_stat_misc(priv,
981 s->reg_offset);
982 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800983 break;
984 }
985
986 j += s->stat_sizeof;
987 p = (char *)priv + s->stat_offset;
988 *(u32 *)p = val;
989 }
990}
991
992static void bcmgenet_get_ethtool_stats(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700993 struct ethtool_stats *stats,
994 u64 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800995{
996 struct bcmgenet_priv *priv = netdev_priv(dev);
997 int i;
998
999 if (netif_running(dev))
1000 bcmgenet_update_mib_counters(priv);
1001
1002 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
1003 const struct bcmgenet_stats *s;
1004 char *p;
1005
1006 s = &bcmgenet_gstrings_stats[i];
1007 if (s->type == BCMGENET_STAT_NETDEV)
1008 p = (char *)&dev->stats;
1009 else
1010 p = (char *)priv;
1011 p += s->stat_offset;
Eric Dumazet6517eb52016-04-15 10:47:52 -07001012 if (sizeof(unsigned long) != sizeof(u32) &&
1013 s->stat_sizeof == sizeof(unsigned long))
1014 data[i] = *(unsigned long *)p;
1015 else
1016 data[i] = *(u32 *)p;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001017 }
1018}
1019
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001020static void bcmgenet_eee_enable_set(struct net_device *dev, bool enable)
1021{
1022 struct bcmgenet_priv *priv = netdev_priv(dev);
1023 u32 off = priv->hw_params->tbuf_offset + TBUF_ENERGY_CTRL;
1024 u32 reg;
1025
1026 if (enable && !priv->clk_eee_enabled) {
1027 clk_prepare_enable(priv->clk_eee);
1028 priv->clk_eee_enabled = true;
1029 }
1030
1031 reg = bcmgenet_umac_readl(priv, UMAC_EEE_CTRL);
1032 if (enable)
1033 reg |= EEE_EN;
1034 else
1035 reg &= ~EEE_EN;
1036 bcmgenet_umac_writel(priv, reg, UMAC_EEE_CTRL);
1037
1038 /* Enable EEE and switch to a 27Mhz clock automatically */
Florian Fainelli69d2ea92017-08-29 12:25:31 -07001039 reg = bcmgenet_readl(priv->base + off);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001040 if (enable)
1041 reg |= TBUF_EEE_EN | TBUF_PM_EN;
1042 else
1043 reg &= ~(TBUF_EEE_EN | TBUF_PM_EN);
Florian Fainelli69d2ea92017-08-29 12:25:31 -07001044 bcmgenet_writel(reg, priv->base + off);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001045
1046 /* Do the same for thing for RBUF */
1047 reg = bcmgenet_rbuf_readl(priv, RBUF_ENERGY_CTRL);
1048 if (enable)
1049 reg |= RBUF_EEE_EN | RBUF_PM_EN;
1050 else
1051 reg &= ~(RBUF_EEE_EN | RBUF_PM_EN);
1052 bcmgenet_rbuf_writel(priv, reg, RBUF_ENERGY_CTRL);
1053
1054 if (!enable && priv->clk_eee_enabled) {
1055 clk_disable_unprepare(priv->clk_eee);
1056 priv->clk_eee_enabled = false;
1057 }
1058
1059 priv->eee.eee_enabled = enable;
1060 priv->eee.eee_active = enable;
1061}
1062
1063static int bcmgenet_get_eee(struct net_device *dev, struct ethtool_eee *e)
1064{
1065 struct bcmgenet_priv *priv = netdev_priv(dev);
1066 struct ethtool_eee *p = &priv->eee;
1067
1068 if (GENET_IS_V1(priv))
1069 return -EOPNOTSUPP;
1070
Doug Berger6c97f012017-10-25 15:04:19 -07001071 if (!dev->phydev)
1072 return -ENODEV;
1073
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001074 e->eee_enabled = p->eee_enabled;
1075 e->eee_active = p->eee_active;
1076 e->tx_lpi_timer = bcmgenet_umac_readl(priv, UMAC_EEE_LPI_TIMER);
1077
Doug Berger6c97f012017-10-25 15:04:19 -07001078 return phy_ethtool_get_eee(dev->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001079}
1080
1081static int bcmgenet_set_eee(struct net_device *dev, struct ethtool_eee *e)
1082{
1083 struct bcmgenet_priv *priv = netdev_priv(dev);
1084 struct ethtool_eee *p = &priv->eee;
1085 int ret = 0;
1086
1087 if (GENET_IS_V1(priv))
1088 return -EOPNOTSUPP;
1089
Doug Berger6c97f012017-10-25 15:04:19 -07001090 if (!dev->phydev)
1091 return -ENODEV;
1092
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001093 p->eee_enabled = e->eee_enabled;
1094
1095 if (!p->eee_enabled) {
1096 bcmgenet_eee_enable_set(dev, false);
1097 } else {
Doug Berger6c97f012017-10-25 15:04:19 -07001098 ret = phy_init_eee(dev->phydev, 0);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001099 if (ret) {
1100 netif_err(priv, hw, dev, "EEE initialization failed\n");
1101 return ret;
1102 }
1103
1104 bcmgenet_umac_writel(priv, e->tx_lpi_timer, UMAC_EEE_LPI_TIMER);
1105 bcmgenet_eee_enable_set(dev, true);
1106 }
1107
Doug Berger6c97f012017-10-25 15:04:19 -07001108 return phy_ethtool_set_eee(dev->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001109}
1110
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001111/* standard ethtool support functions. */
Julia Lawall70591ab2016-08-31 09:30:45 +02001112static const struct ethtool_ops bcmgenet_ethtool_ops = {
Edwin Chan89316fa2017-03-09 16:58:49 -08001113 .begin = bcmgenet_begin,
1114 .complete = bcmgenet_complete,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001115 .get_strings = bcmgenet_get_strings,
1116 .get_sset_count = bcmgenet_get_sset_count,
1117 .get_ethtool_stats = bcmgenet_get_ethtool_stats,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001118 .get_drvinfo = bcmgenet_get_drvinfo,
1119 .get_link = ethtool_op_get_link,
1120 .get_msglevel = bcmgenet_get_msglevel,
1121 .set_msglevel = bcmgenet_set_msglevel,
Florian Fainelli06ba8372014-07-21 15:29:29 -07001122 .get_wol = bcmgenet_get_wol,
1123 .set_wol = bcmgenet_set_wol,
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001124 .get_eee = bcmgenet_get_eee,
1125 .set_eee = bcmgenet_set_eee,
Florian Fainelli016e7702016-11-15 10:06:38 -08001126 .nway_reset = phy_ethtool_nway_reset,
Florian Fainelli2f913072015-09-16 16:47:39 -07001127 .get_coalesce = bcmgenet_get_coalesce,
1128 .set_coalesce = bcmgenet_set_coalesce,
Philippe Reynesfa92bf02016-09-26 22:31:57 +02001129 .get_link_ksettings = bcmgenet_get_link_ksettings,
1130 .set_link_ksettings = bcmgenet_set_link_ksettings,
Ryan M. Collinsdd1bf472019-08-30 14:49:55 -04001131 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001132};
1133
1134/* Power down the unimac, based on mode. */
Florian Fainellica8cf342015-03-23 15:09:51 -07001135static int bcmgenet_power_down(struct bcmgenet_priv *priv,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001136 enum bcmgenet_power_mode mode)
1137{
Florian Fainellica8cf342015-03-23 15:09:51 -07001138 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001139 u32 reg;
1140
1141 switch (mode) {
1142 case GENET_POWER_CABLE_SENSE:
Doug Berger6c97f012017-10-25 15:04:19 -07001143 phy_detach(priv->dev->phydev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001144 break;
1145
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001146 case GENET_POWER_WOL_MAGIC:
Florian Fainellica8cf342015-03-23 15:09:51 -07001147 ret = bcmgenet_wol_power_down_cfg(priv, mode);
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001148 break;
1149
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001150 case GENET_POWER_PASSIVE:
1151 /* Power down LED */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001152 if (priv->hw_params->flags & GENET_HAS_EXT) {
1153 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
Doug Berger42138082017-03-13 17:41:42 -07001154 if (GENET_IS_V5(priv))
1155 reg |= EXT_PWR_DOWN_PHY_EN |
1156 EXT_PWR_DOWN_PHY_RD |
1157 EXT_PWR_DOWN_PHY_SD |
1158 EXT_PWR_DOWN_PHY_RX |
1159 EXT_PWR_DOWN_PHY_TX |
1160 EXT_IDDQ_GLBL_PWR;
1161 else
1162 reg |= EXT_PWR_DOWN_PHY;
1163
1164 reg |= (EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001165 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
Florian Fainellia642c4f2015-03-23 15:09:56 -07001166
1167 bcmgenet_phy_power_set(priv->dev, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001168 }
1169 break;
1170 default:
1171 break;
1172 }
Florian Fainellica8cf342015-03-23 15:09:51 -07001173
YueHaibing0db55092018-11-08 02:08:43 +00001174 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001175}
1176
1177static void bcmgenet_power_up(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001178 enum bcmgenet_power_mode mode)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001179{
1180 u32 reg;
1181
1182 if (!(priv->hw_params->flags & GENET_HAS_EXT))
1183 return;
1184
1185 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
1186
1187 switch (mode) {
1188 case GENET_POWER_PASSIVE:
Doug Berger42138082017-03-13 17:41:42 -07001189 reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
1190 if (GENET_IS_V5(priv)) {
1191 reg &= ~(EXT_PWR_DOWN_PHY_EN |
1192 EXT_PWR_DOWN_PHY_RD |
1193 EXT_PWR_DOWN_PHY_SD |
1194 EXT_PWR_DOWN_PHY_RX |
1195 EXT_PWR_DOWN_PHY_TX |
1196 EXT_IDDQ_GLBL_PWR);
1197 reg |= EXT_PHY_RESET;
1198 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1199 mdelay(1);
1200
1201 reg &= ~EXT_PHY_RESET;
1202 } else {
1203 reg &= ~EXT_PWR_DOWN_PHY;
1204 reg |= EXT_PWR_DN_EN_LD;
1205 }
1206 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1207 bcmgenet_phy_power_set(priv->dev, true);
Doug Berger42138082017-03-13 17:41:42 -07001208 break;
1209
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001210 case GENET_POWER_CABLE_SENSE:
1211 /* enable APD */
Doug Berger42138082017-03-13 17:41:42 -07001212 if (!GENET_IS_V5(priv)) {
1213 reg |= EXT_PWR_DN_EN_LD;
1214 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1215 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001216 break;
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001217 case GENET_POWER_WOL_MAGIC:
1218 bcmgenet_wol_power_up_cfg(priv, mode);
1219 return;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001220 default:
1221 break;
1222 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001223}
1224
1225/* ioctl handle special commands that are not present in ethtool. */
1226static int bcmgenet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1227{
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001228 if (!netif_running(dev))
1229 return -EINVAL;
1230
Doug Berger6c97f012017-10-25 15:04:19 -07001231 if (!dev->phydev)
Doug Berger54fecff2017-03-13 17:41:39 -07001232 return -ENODEV;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001233
Doug Berger6c97f012017-10-25 15:04:19 -07001234 return phy_mii_ioctl(dev->phydev, rq, cmd);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001235}
1236
1237static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv,
1238 struct bcmgenet_tx_ring *ring)
1239{
1240 struct enet_cb *tx_cb_ptr;
1241
1242 tx_cb_ptr = ring->cbs;
1243 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
Petri Gynther014012a2015-02-23 11:00:45 -08001244
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001245 /* Advancing local write pointer */
1246 if (ring->write_ptr == ring->end_ptr)
1247 ring->write_ptr = ring->cb_ptr;
1248 else
1249 ring->write_ptr++;
1250
1251 return tx_cb_ptr;
1252}
1253
Doug Berger876dbad2017-07-14 16:12:09 -07001254static struct enet_cb *bcmgenet_put_txcb(struct bcmgenet_priv *priv,
1255 struct bcmgenet_tx_ring *ring)
1256{
1257 struct enet_cb *tx_cb_ptr;
1258
1259 tx_cb_ptr = ring->cbs;
1260 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
1261
1262 /* Rewinding local write pointer */
1263 if (ring->write_ptr == ring->cb_ptr)
1264 ring->write_ptr = ring->end_ptr;
1265 else
1266 ring->write_ptr--;
1267
1268 return tx_cb_ptr;
1269}
1270
Petri Gynther4055eae2015-03-25 12:35:16 -07001271static inline void bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring *ring)
1272{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001273 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001274 INTRL2_CPU_MASK_SET);
1275}
1276
1277static inline void bcmgenet_rx_ring16_int_enable(struct bcmgenet_rx_ring *ring)
1278{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001279 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001280 INTRL2_CPU_MASK_CLEAR);
1281}
1282
1283static inline void bcmgenet_rx_ring_int_disable(struct bcmgenet_rx_ring *ring)
1284{
1285 bcmgenet_intrl2_1_writel(ring->priv,
1286 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1287 INTRL2_CPU_MASK_SET);
1288}
1289
1290static inline void bcmgenet_rx_ring_int_enable(struct bcmgenet_rx_ring *ring)
1291{
1292 bcmgenet_intrl2_1_writel(ring->priv,
1293 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1294 INTRL2_CPU_MASK_CLEAR);
1295}
1296
Petri Gynther9dbac282015-03-25 12:35:10 -07001297static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001298{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001299 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001300 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001301}
1302
Petri Gynther9dbac282015-03-25 12:35:10 -07001303static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001304{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001305 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001306 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001307}
1308
Petri Gynther9dbac282015-03-25 12:35:10 -07001309static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001310{
Petri Gynther9dbac282015-03-25 12:35:10 -07001311 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001312 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001313}
1314
Petri Gynther9dbac282015-03-25 12:35:10 -07001315static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001316{
Petri Gynther9dbac282015-03-25 12:35:10 -07001317 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001318 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001319}
1320
Doug Bergerf48bed12017-07-14 16:12:10 -07001321/* Simple helper to free a transmit control block's resources
1322 * Returns an skb when the last transmit control block associated with the
1323 * skb is freed. The skb should be freed by the caller if necessary.
1324 */
1325static struct sk_buff *bcmgenet_free_tx_cb(struct device *dev,
1326 struct enet_cb *cb)
1327{
1328 struct sk_buff *skb;
1329
1330 skb = cb->skb;
1331
1332 if (skb) {
1333 cb->skb = NULL;
1334 if (cb == GENET_CB(skb)->first_cb)
1335 dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
1336 dma_unmap_len(cb, dma_len),
1337 DMA_TO_DEVICE);
1338 else
1339 dma_unmap_page(dev, dma_unmap_addr(cb, dma_addr),
1340 dma_unmap_len(cb, dma_len),
1341 DMA_TO_DEVICE);
1342 dma_unmap_addr_set(cb, dma_addr, 0);
1343
1344 if (cb == GENET_CB(skb)->last_cb)
1345 return skb;
1346
1347 } else if (dma_unmap_addr(cb, dma_addr)) {
1348 dma_unmap_page(dev,
1349 dma_unmap_addr(cb, dma_addr),
1350 dma_unmap_len(cb, dma_len),
1351 DMA_TO_DEVICE);
1352 dma_unmap_addr_set(cb, dma_addr, 0);
1353 }
1354
Wei Yongjun335ab8b2018-03-28 12:51:19 +00001355 return NULL;
Doug Bergerf48bed12017-07-14 16:12:10 -07001356}
1357
1358/* Simple helper to free a receive control block's resources */
1359static struct sk_buff *bcmgenet_free_rx_cb(struct device *dev,
1360 struct enet_cb *cb)
1361{
1362 struct sk_buff *skb;
1363
1364 skb = cb->skb;
1365 cb->skb = NULL;
1366
1367 if (dma_unmap_addr(cb, dma_addr)) {
1368 dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
1369 dma_unmap_len(cb, dma_len), DMA_FROM_DEVICE);
1370 dma_unmap_addr_set(cb, dma_addr, 0);
1371 }
1372
1373 return skb;
1374}
1375
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001376/* Unlocked version of the reclaim routine */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001377static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
1378 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001379{
1380 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther66d06752015-03-04 14:30:01 -08001381 unsigned int txbds_processed = 0;
Doug Bergerf48bed12017-07-14 16:12:10 -07001382 unsigned int bytes_compl = 0;
1383 unsigned int pkts_compl = 0;
1384 unsigned int txbds_ready;
1385 unsigned int c_index;
1386 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001387
Doug Bergerd5810ca2017-03-13 17:41:37 -07001388 /* Clear status before servicing to reduce spurious interrupts */
1389 if (ring->index == DESC_INDEX)
1390 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_TXDMA_DONE,
1391 INTRL2_CPU_CLEAR);
1392 else
1393 bcmgenet_intrl2_1_writel(priv, (1 << ring->index),
1394 INTRL2_CPU_CLEAR);
1395
Brian Norris7fc527f2014-07-29 14:34:14 -07001396 /* Compute how many buffers are transmitted since last xmit call */
Doug Bergerc298ede2017-03-13 17:41:33 -07001397 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX)
1398 & DMA_C_INDEX_MASK;
1399 txbds_ready = (c_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001400
1401 netif_dbg(priv, tx_done, dev,
Petri Gynther66d06752015-03-04 14:30:01 -08001402 "%s ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
1403 __func__, ring->index, ring->c_index, c_index, txbds_ready);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001404
1405 /* Reclaim transmitted buffers */
Petri Gynther66d06752015-03-04 14:30:01 -08001406 while (txbds_processed < txbds_ready) {
Doug Bergerf48bed12017-07-14 16:12:10 -07001407 skb = bcmgenet_free_tx_cb(&priv->pdev->dev,
1408 &priv->tx_cbs[ring->clean_ptr]);
1409 if (skb) {
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001410 pkts_compl++;
Doug Bergerf48bed12017-07-14 16:12:10 -07001411 bytes_compl += GENET_CB(skb)->bytes_sent;
Florian Fainellid4fec852017-08-24 15:56:29 -07001412 dev_consume_skb_any(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001413 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001414
Petri Gynther66d06752015-03-04 14:30:01 -08001415 txbds_processed++;
1416 if (likely(ring->clean_ptr < ring->end_ptr))
1417 ring->clean_ptr++;
1418 else
1419 ring->clean_ptr = ring->cb_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001420 }
1421
Petri Gynther66d06752015-03-04 14:30:01 -08001422 ring->free_bds += txbds_processed;
Doug Bergerc4d453d2017-03-13 17:41:38 -07001423 ring->c_index = c_index;
Petri Gynther66d06752015-03-04 14:30:01 -08001424
Florian Fainelli37a30b42017-03-16 10:27:08 -07001425 ring->packets += pkts_compl;
1426 ring->bytes += bytes_compl;
Petri Gynther55868122016-03-24 11:27:20 -07001427
Doug Berger6d22fe12017-03-09 16:58:50 -08001428 netdev_tx_completed_queue(netdev_get_tx_queue(dev, ring->queue),
1429 pkts_compl, bytes_compl);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001430
Doug Bergerc4d453d2017-03-13 17:41:38 -07001431 return txbds_processed;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001432}
1433
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001434static unsigned int bcmgenet_tx_reclaim(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001435 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001436{
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001437 unsigned int released;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001438
Doug Bergerb0447ec2017-10-25 15:04:17 -07001439 spin_lock_bh(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001440 released = __bcmgenet_tx_reclaim(dev, ring);
Doug Bergerb0447ec2017-10-25 15:04:17 -07001441 spin_unlock_bh(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001442
1443 return released;
1444}
1445
1446static int bcmgenet_tx_poll(struct napi_struct *napi, int budget)
1447{
1448 struct bcmgenet_tx_ring *ring =
1449 container_of(napi, struct bcmgenet_tx_ring, napi);
1450 unsigned int work_done = 0;
Doug Berger6d22fe12017-03-09 16:58:50 -08001451 struct netdev_queue *txq;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001452
Doug Bergerb0447ec2017-10-25 15:04:17 -07001453 spin_lock(&ring->lock);
Doug Berger6d22fe12017-03-09 16:58:50 -08001454 work_done = __bcmgenet_tx_reclaim(ring->priv->dev, ring);
1455 if (ring->free_bds > (MAX_SKB_FRAGS + 1)) {
1456 txq = netdev_get_tx_queue(ring->priv->dev, ring->queue);
1457 netif_tx_wake_queue(txq);
1458 }
Doug Bergerb0447ec2017-10-25 15:04:17 -07001459 spin_unlock(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001460
1461 if (work_done == 0) {
1462 napi_complete(napi);
Petri Gynther9dbac282015-03-25 12:35:10 -07001463 ring->int_enable(ring);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001464
1465 return 0;
1466 }
1467
1468 return budget;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001469}
1470
1471static void bcmgenet_tx_reclaim_all(struct net_device *dev)
1472{
1473 struct bcmgenet_priv *priv = netdev_priv(dev);
1474 int i;
1475
1476 if (netif_is_multiqueue(dev)) {
1477 for (i = 0; i < priv->hw_params->tx_queues; i++)
1478 bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
1479 }
1480
1481 bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
1482}
1483
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001484/* Reallocate the SKB to put enough headroom in front of it and insert
1485 * the transmit checksum offsets in the descriptors
1486 */
Petri Gyntherbc233332014-10-01 11:30:01 -07001487static struct sk_buff *bcmgenet_put_tx_csum(struct net_device *dev,
1488 struct sk_buff *skb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001489{
1490 struct status_64 *status = NULL;
1491 struct sk_buff *new_skb;
1492 u16 offset;
1493 u8 ip_proto;
Florian Fainelli6f894212018-04-02 15:58:55 -07001494 __be16 ip_ver;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001495 u32 tx_csum_info;
1496
1497 if (unlikely(skb_headroom(skb) < sizeof(*status))) {
1498 /* If 64 byte status block enabled, must make sure skb has
1499 * enough headroom for us to insert 64B status block.
1500 */
1501 new_skb = skb_realloc_headroom(skb, sizeof(*status));
1502 dev_kfree_skb(skb);
1503 if (!new_skb) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001504 dev->stats.tx_dropped++;
Petri Gyntherbc233332014-10-01 11:30:01 -07001505 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001506 }
1507 skb = new_skb;
1508 }
1509
1510 skb_push(skb, sizeof(*status));
1511 status = (struct status_64 *)skb->data;
1512
1513 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Florian Fainelli6f894212018-04-02 15:58:55 -07001514 ip_ver = skb->protocol;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001515 switch (ip_ver) {
Florian Fainelli6f894212018-04-02 15:58:55 -07001516 case htons(ETH_P_IP):
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001517 ip_proto = ip_hdr(skb)->protocol;
1518 break;
Florian Fainelli6f894212018-04-02 15:58:55 -07001519 case htons(ETH_P_IPV6):
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001520 ip_proto = ipv6_hdr(skb)->nexthdr;
1521 break;
1522 default:
Doug Bergerdd8e9112019-12-17 16:51:09 -08001523 /* don't use UDP flag */
1524 ip_proto = 0;
1525 break;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001526 }
1527
1528 offset = skb_checksum_start_offset(skb) - sizeof(*status);
1529 tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
Doug Bergerdd8e9112019-12-17 16:51:09 -08001530 (offset + skb->csum_offset) |
1531 STATUS_TX_CSUM_LV;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001532
Doug Bergerdd8e9112019-12-17 16:51:09 -08001533 /* Set the special UDP flag for UDP */
1534 if (ip_proto == IPPROTO_UDP)
1535 tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001536
1537 status->tx_csum_info = tx_csum_info;
1538 }
1539
Petri Gyntherbc233332014-10-01 11:30:01 -07001540 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001541}
1542
1543static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
1544{
1545 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Berger876dbad2017-07-14 16:12:09 -07001546 struct device *kdev = &priv->pdev->dev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001547 struct bcmgenet_tx_ring *ring = NULL;
Doug Berger876dbad2017-07-14 16:12:09 -07001548 struct enet_cb *tx_cb_ptr;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001549 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001550 int nr_frags, index;
Doug Berger876dbad2017-07-14 16:12:09 -07001551 dma_addr_t mapping;
1552 unsigned int size;
1553 skb_frag_t *frag;
1554 u32 len_stat;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001555 int ret;
1556 int i;
1557
1558 index = skb_get_queue_mapping(skb);
1559 /* Mapping strategy:
1560 * queue_mapping = 0, unclassified, packet xmited through ring16
1561 * queue_mapping = 1, goes to ring 0. (highest priority queue
1562 * queue_mapping = 2, goes to ring 1.
1563 * queue_mapping = 3, goes to ring 2.
1564 * queue_mapping = 4, goes to ring 3.
1565 */
1566 if (index == 0)
1567 index = DESC_INDEX;
1568 else
1569 index -= 1;
1570
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001571 ring = &priv->tx_rings[index];
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001572 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001573
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07001574 nr_frags = skb_shinfo(skb)->nr_frags;
1575
Doug Bergerb0447ec2017-10-25 15:04:17 -07001576 spin_lock(&ring->lock);
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07001577 if (ring->free_bds <= (nr_frags + 1)) {
1578 if (!netif_tx_queue_stopped(txq)) {
1579 netif_tx_stop_queue(txq);
1580 netdev_err(dev,
1581 "%s: tx ring %d full when queue %d awake\n",
1582 __func__, index, ring->queue);
1583 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001584 ret = NETDEV_TX_BUSY;
1585 goto out;
1586 }
1587
Florian Fainelli474ea9c2014-07-22 11:01:52 -07001588 if (skb_padto(skb, ETH_ZLEN)) {
1589 ret = NETDEV_TX_OK;
1590 goto out;
1591 }
1592
Petri Gynther55868122016-03-24 11:27:20 -07001593 /* Retain how many bytes will be sent on the wire, without TSB inserted
1594 * by transmit checksum offload
1595 */
1596 GENET_CB(skb)->bytes_sent = skb->len;
1597
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001598 /* set the SKB transmit checksum */
1599 if (priv->desc_64b_en) {
Petri Gyntherbc233332014-10-01 11:30:01 -07001600 skb = bcmgenet_put_tx_csum(dev, skb);
1601 if (!skb) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001602 ret = NETDEV_TX_OK;
1603 goto out;
1604 }
1605 }
1606
Doug Berger876dbad2017-07-14 16:12:09 -07001607 for (i = 0; i <= nr_frags; i++) {
1608 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001609
Gustavo A. R. Silva4fa112f2017-10-26 07:16:01 -05001610 BUG_ON(!tx_cb_ptr);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001611
Doug Berger876dbad2017-07-14 16:12:09 -07001612 if (!i) {
1613 /* Transmit single SKB or head of fragment list */
Doug Bergerf48bed12017-07-14 16:12:10 -07001614 GENET_CB(skb)->first_cb = tx_cb_ptr;
Doug Berger876dbad2017-07-14 16:12:09 -07001615 size = skb_headlen(skb);
1616 mapping = dma_map_single(kdev, skb->data, size,
1617 DMA_TO_DEVICE);
1618 } else {
1619 /* xmit fragment */
Doug Berger876dbad2017-07-14 16:12:09 -07001620 frag = &skb_shinfo(skb)->frags[i - 1];
1621 size = skb_frag_size(frag);
1622 mapping = skb_frag_dma_map(kdev, frag, 0, size,
1623 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001624 }
Doug Berger876dbad2017-07-14 16:12:09 -07001625
1626 ret = dma_mapping_error(kdev, mapping);
1627 if (ret) {
1628 priv->mib.tx_dma_failed++;
1629 netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
1630 ret = NETDEV_TX_OK;
1631 goto out_unmap_frags;
1632 }
1633 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
1634 dma_unmap_len_set(tx_cb_ptr, dma_len, size);
1635
Doug Bergerf48bed12017-07-14 16:12:10 -07001636 tx_cb_ptr->skb = skb;
1637
Doug Berger876dbad2017-07-14 16:12:09 -07001638 len_stat = (size << DMA_BUFLENGTH_SHIFT) |
1639 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT);
1640
1641 if (!i) {
1642 len_stat |= DMA_TX_APPEND_CRC | DMA_SOP;
1643 if (skb->ip_summed == CHECKSUM_PARTIAL)
1644 len_stat |= DMA_TX_DO_CSUM;
1645 }
1646 if (i == nr_frags)
1647 len_stat |= DMA_EOP;
1648
1649 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, len_stat);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001650 }
1651
Doug Bergerf48bed12017-07-14 16:12:10 -07001652 GENET_CB(skb)->last_cb = tx_cb_ptr;
Florian Fainellid03825f2014-03-20 10:53:21 -07001653 skb_tx_timestamp(skb);
1654
Florian Fainelliae67bf02015-03-13 12:11:06 -07001655 /* Decrement total BD count and advance our write pointer */
1656 ring->free_bds -= nr_frags + 1;
1657 ring->prod_index += nr_frags + 1;
1658 ring->prod_index &= DMA_P_INDEX_MASK;
1659
Petri Gynthere178c8c2016-04-09 00:20:36 -07001660 netdev_tx_sent_queue(txq, GENET_CB(skb)->bytes_sent);
1661
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001662 if (ring->free_bds <= (MAX_SKB_FRAGS + 1))
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001663 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001664
Florian Westphal6b16f9e2019-04-01 16:42:14 +02001665 if (!netdev_xmit_more() || netif_xmit_stopped(txq))
Florian Fainelliddd0ca52015-03-13 12:11:07 -07001666 /* Packets are ready, update producer index */
1667 bcmgenet_tdma_ring_writel(priv, ring->index,
1668 ring->prod_index, TDMA_PROD_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001669out:
Doug Bergerb0447ec2017-10-25 15:04:17 -07001670 spin_unlock(&ring->lock);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001671
1672 return ret;
Doug Berger876dbad2017-07-14 16:12:09 -07001673
1674out_unmap_frags:
1675 /* Back up for failed control block mapping */
1676 bcmgenet_put_txcb(priv, ring);
1677
1678 /* Unmap successfully mapped control blocks */
1679 while (i-- > 0) {
1680 tx_cb_ptr = bcmgenet_put_txcb(priv, ring);
Doug Bergerf48bed12017-07-14 16:12:10 -07001681 bcmgenet_free_tx_cb(kdev, tx_cb_ptr);
Doug Berger876dbad2017-07-14 16:12:09 -07001682 }
1683
1684 dev_kfree_skb(skb);
1685 goto out;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001686}
1687
Petri Gyntherd6707be2015-03-12 15:48:00 -07001688static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
1689 struct enet_cb *cb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001690{
1691 struct device *kdev = &priv->pdev->dev;
1692 struct sk_buff *skb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001693 struct sk_buff *rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001694 dma_addr_t mapping;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001695
Petri Gyntherd6707be2015-03-12 15:48:00 -07001696 /* Allocate a new Rx skb */
Florian Fainellic91b7f62014-07-23 10:42:12 -07001697 skb = netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT);
Petri Gyntherd6707be2015-03-12 15:48:00 -07001698 if (!skb) {
1699 priv->mib.alloc_rx_buff_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001700 netif_err(priv, rx_err, priv->dev,
Petri Gyntherd6707be2015-03-12 15:48:00 -07001701 "%s: Rx skb allocation failed\n", __func__);
1702 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001703 }
1704
Petri Gyntherd6707be2015-03-12 15:48:00 -07001705 /* DMA-map the new Rx skb */
1706 mapping = dma_map_single(kdev, skb->data, priv->rx_buf_len,
1707 DMA_FROM_DEVICE);
1708 if (dma_mapping_error(kdev, mapping)) {
1709 priv->mib.rx_dma_failed++;
1710 dev_kfree_skb_any(skb);
1711 netif_err(priv, rx_err, priv->dev,
1712 "%s: Rx skb DMA mapping failed\n", __func__);
1713 return NULL;
1714 }
1715
1716 /* Grab the current Rx skb from the ring and DMA-unmap it */
Doug Bergerf48bed12017-07-14 16:12:10 -07001717 rx_skb = bcmgenet_free_rx_cb(kdev, cb);
Petri Gyntherd6707be2015-03-12 15:48:00 -07001718
1719 /* Put the new Rx skb on the ring */
1720 cb->skb = skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001721 dma_unmap_addr_set(cb, dma_addr, mapping);
Doug Bergerf48bed12017-07-14 16:12:10 -07001722 dma_unmap_len_set(cb, dma_len, priv->rx_buf_len);
Petri Gynther8ac467e2015-03-09 13:40:00 -07001723 dmadesc_set_addr(priv, cb->bd_addr, mapping);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001724
Petri Gyntherd6707be2015-03-12 15:48:00 -07001725 /* Return the current Rx skb to caller */
1726 return rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001727}
1728
1729/* bcmgenet_desc_rx - descriptor based rx process.
1730 * this could be called from bottom half, or from NAPI polling method.
1731 */
Petri Gynther4055eae2015-03-25 12:35:16 -07001732static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001733 unsigned int budget)
1734{
Petri Gynther4055eae2015-03-25 12:35:16 -07001735 struct bcmgenet_priv *priv = ring->priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001736 struct net_device *dev = priv->dev;
1737 struct enet_cb *cb;
1738 struct sk_buff *skb;
1739 u32 dma_length_status;
1740 unsigned long dma_flag;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001741 int len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001742 unsigned int rxpktprocessed = 0, rxpkttoprocess;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001743 unsigned int bytes_processed = 0;
Doug Bergerd5810ca2017-03-13 17:41:37 -07001744 unsigned int p_index, mask;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001745 unsigned int discards;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001746
Doug Bergerd5810ca2017-03-13 17:41:37 -07001747 /* Clear status before servicing to reduce spurious interrupts */
1748 if (ring->index == DESC_INDEX) {
1749 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_RXDMA_DONE,
1750 INTRL2_CPU_CLEAR);
1751 } else {
1752 mask = 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index);
1753 bcmgenet_intrl2_1_writel(priv,
1754 mask,
1755 INTRL2_CPU_CLEAR);
1756 }
1757
Petri Gynther4055eae2015-03-25 12:35:16 -07001758 p_index = bcmgenet_rdma_ring_readl(priv, ring->index, RDMA_PROD_INDEX);
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001759
1760 discards = (p_index >> DMA_P_INDEX_DISCARD_CNT_SHIFT) &
1761 DMA_P_INDEX_DISCARD_CNT_MASK;
1762 if (discards > ring->old_discards) {
1763 discards = discards - ring->old_discards;
Florian Fainelli37a30b42017-03-16 10:27:08 -07001764 ring->errors += discards;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001765 ring->old_discards += discards;
1766
1767 /* Clear HW register when we reach 75% of maximum 0xFFFF */
1768 if (ring->old_discards >= 0xC000) {
1769 ring->old_discards = 0;
Petri Gynther4055eae2015-03-25 12:35:16 -07001770 bcmgenet_rdma_ring_writel(priv, ring->index, 0,
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001771 RDMA_PROD_INDEX);
1772 }
1773 }
1774
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001775 p_index &= DMA_P_INDEX_MASK;
Doug Bergerc298ede2017-03-13 17:41:33 -07001776 rxpkttoprocess = (p_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001777
1778 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001779 "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001780
1781 while ((rxpktprocessed < rxpkttoprocess) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001782 (rxpktprocessed < budget)) {
Petri Gynther8ac467e2015-03-09 13:40:00 -07001783 cb = &priv->rx_cbs[ring->read_ptr];
Petri Gyntherd6707be2015-03-12 15:48:00 -07001784 skb = bcmgenet_rx_refill(priv, cb);
Florian Fainellib629be52014-09-08 11:37:52 -07001785
Florian Fainellib629be52014-09-08 11:37:52 -07001786 if (unlikely(!skb)) {
Florian Fainelli37a30b42017-03-16 10:27:08 -07001787 ring->dropped++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001788 goto next;
Florian Fainellib629be52014-09-08 11:37:52 -07001789 }
1790
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001791 if (!priv->desc_64b_en) {
Florian Fainellic91b7f62014-07-23 10:42:12 -07001792 dma_length_status =
Petri Gynther8ac467e2015-03-09 13:40:00 -07001793 dmadesc_get_length_status(priv, cb->bd_addr);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001794 } else {
1795 struct status_64 *status;
Doug Berger81015532019-12-17 16:51:10 -08001796 __be16 rx_csum;
Florian Fainelli164d4f22014-07-23 10:42:13 -07001797
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001798 status = (struct status_64 *)skb->data;
1799 dma_length_status = status->length_status;
Doug Berger81015532019-12-17 16:51:10 -08001800 rx_csum = (__force __be16)(status->rx_csum & 0xffff);
1801 if (priv->desc_rxchk_en) {
1802 skb->csum = (__force __wsum)ntohs(rx_csum);
1803 skb->ip_summed = CHECKSUM_COMPLETE;
1804 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001805 }
1806
1807 /* DMA flags and length are still valid no matter how
1808 * we got the Receive Status Vector (64B RSB or register)
1809 */
1810 dma_flag = dma_length_status & 0xffff;
1811 len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
1812
1813 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001814 "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
Petri Gynther8ac467e2015-03-09 13:40:00 -07001815 __func__, p_index, ring->c_index,
1816 ring->read_ptr, dma_length_status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001817
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001818 if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
1819 netif_err(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001820 "dropping fragmented packet!\n");
Florian Fainelli37a30b42017-03-16 10:27:08 -07001821 ring->errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001822 dev_kfree_skb_any(skb);
1823 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001824 }
Petri Gyntherd6707be2015-03-12 15:48:00 -07001825
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001826 /* report errors */
1827 if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
1828 DMA_RX_OV |
1829 DMA_RX_NO |
1830 DMA_RX_LG |
1831 DMA_RX_RXER))) {
1832 netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001833 (unsigned int)dma_flag);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001834 if (dma_flag & DMA_RX_CRC_ERROR)
1835 dev->stats.rx_crc_errors++;
1836 if (dma_flag & DMA_RX_OV)
1837 dev->stats.rx_over_errors++;
1838 if (dma_flag & DMA_RX_NO)
1839 dev->stats.rx_frame_errors++;
1840 if (dma_flag & DMA_RX_LG)
1841 dev->stats.rx_length_errors++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001842 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001843 dev_kfree_skb_any(skb);
1844 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001845 } /* error packet */
1846
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001847 skb_put(skb, len);
1848 if (priv->desc_64b_en) {
1849 skb_pull(skb, 64);
1850 len -= 64;
1851 }
1852
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001853 /* remove hardware 2bytes added for IP alignment */
1854 skb_pull(skb, 2);
1855 len -= 2;
1856
1857 if (priv->crc_fwd_en) {
1858 skb_trim(skb, len - ETH_FCS_LEN);
1859 len -= ETH_FCS_LEN;
1860 }
1861
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001862 bytes_processed += len;
1863
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001864 /*Finish setting up the received SKB and send it to the kernel*/
1865 skb->protocol = eth_type_trans(skb, priv->dev);
Florian Fainelli37a30b42017-03-16 10:27:08 -07001866 ring->packets++;
1867 ring->bytes += len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001868 if (dma_flag & DMA_RX_MULT)
1869 dev->stats.multicast++;
1870
1871 /* Notify kernel */
Petri Gynther4055eae2015-03-25 12:35:16 -07001872 napi_gro_receive(&ring->napi, skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001873 netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
1874
Petri Gyntherd6707be2015-03-12 15:48:00 -07001875next:
Florian Fainellicf377d82014-10-10 10:51:52 -07001876 rxpktprocessed++;
Petri Gynther8ac467e2015-03-09 13:40:00 -07001877 if (likely(ring->read_ptr < ring->end_ptr))
1878 ring->read_ptr++;
1879 else
1880 ring->read_ptr = ring->cb_ptr;
1881
1882 ring->c_index = (ring->c_index + 1) & DMA_C_INDEX_MASK;
Petri Gynther4055eae2015-03-25 12:35:16 -07001883 bcmgenet_rdma_ring_writel(priv, ring->index, ring->c_index, RDMA_CONS_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001884 }
1885
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001886 ring->dim.bytes = bytes_processed;
1887 ring->dim.packets = rxpktprocessed;
1888
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001889 return rxpktprocessed;
1890}
1891
Petri Gynther3ab11332015-03-25 12:35:15 -07001892/* Rx NAPI polling method */
1893static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
1894{
Petri Gynther4055eae2015-03-25 12:35:16 -07001895 struct bcmgenet_rx_ring *ring = container_of(napi,
1896 struct bcmgenet_rx_ring, napi);
Yamin Friedmanf06d0ca2019-07-23 10:22:47 +03001897 struct dim_sample dim_sample = {};
Petri Gynther3ab11332015-03-25 12:35:15 -07001898 unsigned int work_done;
1899
Petri Gynther4055eae2015-03-25 12:35:16 -07001900 work_done = bcmgenet_desc_rx(ring, budget);
Petri Gynther3ab11332015-03-25 12:35:15 -07001901
1902 if (work_done < budget) {
Eric Dumazeteb96ce02016-04-08 22:06:40 -07001903 napi_complete_done(napi, work_done);
Petri Gynther4055eae2015-03-25 12:35:16 -07001904 ring->int_enable(ring);
Petri Gynther3ab11332015-03-25 12:35:15 -07001905 }
1906
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001907 if (ring->dim.use_dim) {
Tal Gilboa8960b382019-01-31 16:44:48 +02001908 dim_update_sample(ring->dim.event_ctr, ring->dim.packets,
1909 ring->dim.bytes, &dim_sample);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001910 net_dim(&ring->dim.dim, dim_sample);
1911 }
1912
Petri Gynther3ab11332015-03-25 12:35:15 -07001913 return work_done;
1914}
1915
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001916static void bcmgenet_dim_work(struct work_struct *work)
1917{
Tal Gilboa8960b382019-01-31 16:44:48 +02001918 struct dim *dim = container_of(work, struct dim, work);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001919 struct bcmgenet_net_dim *ndim =
1920 container_of(dim, struct bcmgenet_net_dim, dim);
1921 struct bcmgenet_rx_ring *ring =
1922 container_of(ndim, struct bcmgenet_rx_ring, dim);
Tal Gilboa8960b382019-01-31 16:44:48 +02001923 struct dim_cq_moder cur_profile =
Tal Gilboa026a8072018-04-24 13:36:01 +03001924 net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001925
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07001926 bcmgenet_set_rx_coalesce(ring, cur_profile.usec, cur_profile.pkts);
Tal Gilboac002bd52018-11-05 12:07:52 +02001927 dim->state = DIM_START_MEASURE;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001928}
1929
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001930/* Assign skb to RX DMA descriptor. */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001931static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv,
1932 struct bcmgenet_rx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001933{
1934 struct enet_cb *cb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001935 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001936 int i;
1937
Petri Gynther8ac467e2015-03-09 13:40:00 -07001938 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001939
1940 /* loop here for each buffer needing assign */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001941 for (i = 0; i < ring->size; i++) {
1942 cb = ring->cbs + i;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001943 skb = bcmgenet_rx_refill(priv, cb);
1944 if (skb)
Florian Fainellid4fec852017-08-24 15:56:29 -07001945 dev_consume_skb_any(skb);
Petri Gyntherd6707be2015-03-12 15:48:00 -07001946 if (!cb->skb)
1947 return -ENOMEM;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001948 }
1949
Petri Gyntherd6707be2015-03-12 15:48:00 -07001950 return 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001951}
1952
1953static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
1954{
Doug Bergerf48bed12017-07-14 16:12:10 -07001955 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001956 struct enet_cb *cb;
1957 int i;
1958
1959 for (i = 0; i < priv->num_rx_bds; i++) {
1960 cb = &priv->rx_cbs[i];
1961
Doug Bergerf48bed12017-07-14 16:12:10 -07001962 skb = bcmgenet_free_rx_cb(&priv->pdev->dev, cb);
1963 if (skb)
Florian Fainellid4fec852017-08-24 15:56:29 -07001964 dev_consume_skb_any(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001965 }
1966}
1967
Florian Fainellic91b7f62014-07-23 10:42:12 -07001968static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable)
Florian Fainellie29585b2014-07-21 15:29:20 -07001969{
1970 u32 reg;
1971
1972 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1973 if (enable)
1974 reg |= mask;
1975 else
1976 reg &= ~mask;
1977 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
1978
1979 /* UniMAC stops on a packet boundary, wait for a full-size packet
1980 * to be processed
1981 */
1982 if (enable == 0)
1983 usleep_range(1000, 2000);
1984}
1985
Doug Berger28c2d1a2017-10-25 15:04:13 -07001986static void reset_umac(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001987{
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001988 /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
1989 bcmgenet_rbuf_ctrl_set(priv, 0);
1990 udelay(10);
1991
1992 /* disable MAC while updating its registers */
1993 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1994
Doug Berger28c2d1a2017-10-25 15:04:13 -07001995 /* issue soft reset with (rg)mii loopback to ensure a stable rxclk */
1996 bcmgenet_umac_writel(priv, CMD_SW_RESET | CMD_LCL_LOOP_EN, UMAC_CMD);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001997}
1998
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001999static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
2000{
2001 /* Mask all interrupts.*/
2002 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
2003 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002004 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
2005 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002006}
2007
Florian Fainelli37850e32015-10-17 14:22:46 -07002008static void bcmgenet_link_intr_enable(struct bcmgenet_priv *priv)
2009{
2010 u32 int0_enable = 0;
2011
2012 /* Monitor cable plug/unplugged event for internal PHY, external PHY
2013 * and MoCA PHY
2014 */
2015 if (priv->internal_phy) {
2016 int0_enable |= UMAC_IRQ_LINK_EVENT;
Doug Berger25382b92019-10-16 16:06:32 -07002017 if (GENET_IS_V1(priv) || GENET_IS_V2(priv) || GENET_IS_V3(priv))
2018 int0_enable |= UMAC_IRQ_PHY_DET_R;
Florian Fainelli37850e32015-10-17 14:22:46 -07002019 } else if (priv->ext_phy) {
2020 int0_enable |= UMAC_IRQ_LINK_EVENT;
2021 } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
2022 if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
2023 int0_enable |= UMAC_IRQ_LINK_EVENT;
2024 }
2025 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
2026}
2027
Doug Berger28c2d1a2017-10-25 15:04:13 -07002028static void init_umac(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002029{
2030 struct device *kdev = &priv->pdev->dev;
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002031 u32 reg;
2032 u32 int0_enable = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002033
2034 dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
2035
Doug Berger28c2d1a2017-10-25 15:04:13 -07002036 reset_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002037
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002038 /* clear tx/rx counter */
2039 bcmgenet_umac_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002040 MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
2041 UMAC_MIB_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002042 bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
2043
2044 bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
2045
2046 /* init rx registers, enable ip header optimization */
2047 reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
2048 reg |= RBUF_ALIGN_2B;
2049 bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
2050
2051 if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
2052 bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
2053
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002054 bcmgenet_intr_disable(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002055
Florian Fainelli37850e32015-10-17 14:22:46 -07002056 /* Configure backpressure vectors for MoCA */
2057 if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002058 reg = bcmgenet_bp_mc_get(priv);
2059 reg |= BIT(priv->hw_params->bp_in_en_shift);
2060
2061 /* bp_mask: back pressure mask */
2062 if (netif_is_multiqueue(priv->dev))
2063 reg |= priv->hw_params->bp_in_mask;
2064 else
2065 reg &= ~priv->hw_params->bp_in_mask;
2066 bcmgenet_bp_mc_set(priv, reg);
2067 }
2068
2069 /* Enable MDIO interrupts on GENET v3+ */
2070 if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002071 int0_enable |= (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002072
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002073 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002074
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002075 dev_dbg(kdev, "done init umac\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002076}
2077
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002078static void bcmgenet_init_dim(struct bcmgenet_rx_ring *ring,
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002079 void (*cb)(struct work_struct *work))
2080{
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002081 struct bcmgenet_net_dim *dim = &ring->dim;
2082
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002083 INIT_WORK(&dim->dim.work, cb);
Tal Gilboac002bd52018-11-05 12:07:52 +02002084 dim->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002085 dim->event_ctr = 0;
2086 dim->packets = 0;
2087 dim->bytes = 0;
2088}
2089
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002090static void bcmgenet_init_rx_coalesce(struct bcmgenet_rx_ring *ring)
2091{
2092 struct bcmgenet_net_dim *dim = &ring->dim;
Tal Gilboa8960b382019-01-31 16:44:48 +02002093 struct dim_cq_moder moder;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002094 u32 usecs, pkts;
2095
2096 usecs = ring->rx_coalesce_usecs;
2097 pkts = ring->rx_max_coalesced_frames;
2098
2099 /* If DIM was enabled, re-apply default parameters */
2100 if (dim->use_dim) {
Tal Gilboa026a8072018-04-24 13:36:01 +03002101 moder = net_dim_get_def_rx_moderation(dim->dim.mode);
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002102 usecs = moder.usec;
2103 pkts = moder.pkts;
2104 }
2105
2106 bcmgenet_set_rx_coalesce(ring, usecs, pkts);
2107}
2108
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002109/* Initialize a Tx ring along with corresponding hardware registers */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002110static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
2111 unsigned int index, unsigned int size,
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002112 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002113{
2114 struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
2115 u32 words_per_bd = WORDS_PER_BD(priv);
2116 u32 flow_period_val = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002117
2118 spin_lock_init(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002119 ring->priv = priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002120 ring->index = index;
2121 if (index == DESC_INDEX) {
2122 ring->queue = 0;
2123 ring->int_enable = bcmgenet_tx_ring16_int_enable;
2124 ring->int_disable = bcmgenet_tx_ring16_int_disable;
2125 } else {
2126 ring->queue = index + 1;
2127 ring->int_enable = bcmgenet_tx_ring_int_enable;
2128 ring->int_disable = bcmgenet_tx_ring_int_disable;
2129 }
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002130 ring->cbs = priv->tx_cbs + start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002131 ring->size = size;
Petri Gynther66d06752015-03-04 14:30:01 -08002132 ring->clean_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002133 ring->c_index = 0;
2134 ring->free_bds = size;
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002135 ring->write_ptr = start_ptr;
2136 ring->cb_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002137 ring->end_ptr = end_ptr - 1;
2138 ring->prod_index = 0;
2139
2140 /* Set flow period for ring != 16 */
2141 if (index != DESC_INDEX)
2142 flow_period_val = ENET_MAX_MTU_SIZE << 16;
2143
2144 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
2145 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
2146 bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
2147 /* Disable rate control for now */
2148 bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002149 TDMA_FLOW_PERIOD);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002150 bcmgenet_tdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002151 ((size << DMA_RING_SIZE_SHIFT) |
2152 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002153
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002154 /* Set start and end address, read and write pointers */
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002155 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002156 DMA_START_ADDR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002157 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002158 TDMA_READ_PTR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002159 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002160 TDMA_WRITE_PTR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002161 bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002162 DMA_END_ADDR);
Doug Berger75879352017-10-25 15:04:14 -07002163
2164 /* Initialize Tx NAPI */
2165 netif_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll,
2166 NAPI_POLL_WEIGHT);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002167}
2168
2169/* Initialize a RDMA ring */
2170static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
Petri Gynther8ac467e2015-03-09 13:40:00 -07002171 unsigned int index, unsigned int size,
2172 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002173{
Petri Gynther8ac467e2015-03-09 13:40:00 -07002174 struct bcmgenet_rx_ring *ring = &priv->rx_rings[index];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002175 u32 words_per_bd = WORDS_PER_BD(priv);
2176 int ret;
2177
Petri Gynther4055eae2015-03-25 12:35:16 -07002178 ring->priv = priv;
Petri Gynther8ac467e2015-03-09 13:40:00 -07002179 ring->index = index;
Petri Gynther4055eae2015-03-25 12:35:16 -07002180 if (index == DESC_INDEX) {
2181 ring->int_enable = bcmgenet_rx_ring16_int_enable;
2182 ring->int_disable = bcmgenet_rx_ring16_int_disable;
2183 } else {
2184 ring->int_enable = bcmgenet_rx_ring_int_enable;
2185 ring->int_disable = bcmgenet_rx_ring_int_disable;
2186 }
Petri Gynther8ac467e2015-03-09 13:40:00 -07002187 ring->cbs = priv->rx_cbs + start_ptr;
2188 ring->size = size;
2189 ring->c_index = 0;
2190 ring->read_ptr = start_ptr;
2191 ring->cb_ptr = start_ptr;
2192 ring->end_ptr = end_ptr - 1;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002193
Petri Gynther8ac467e2015-03-09 13:40:00 -07002194 ret = bcmgenet_alloc_rx_buffers(priv, ring);
2195 if (ret)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002196 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002197
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002198 bcmgenet_init_dim(ring, bcmgenet_dim_work);
2199 bcmgenet_init_rx_coalesce(ring);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002200
Doug Berger75879352017-10-25 15:04:14 -07002201 /* Initialize Rx NAPI */
2202 netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll,
2203 NAPI_POLL_WEIGHT);
2204
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002205 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
2206 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
2207 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002208 ((size << DMA_RING_SIZE_SHIFT) |
2209 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002210 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002211 (DMA_FC_THRESH_LO <<
2212 DMA_XOFF_THRESHOLD_SHIFT) |
2213 DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
Petri Gynther6f5a2722015-03-06 13:45:00 -08002214
2215 /* Set start and end address, read and write pointers */
Petri Gynther8ac467e2015-03-09 13:40:00 -07002216 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2217 DMA_START_ADDR);
2218 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2219 RDMA_READ_PTR);
2220 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2221 RDMA_WRITE_PTR);
2222 bcmgenet_rdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Petri Gynther6f5a2722015-03-06 13:45:00 -08002223 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002224
2225 return ret;
2226}
2227
Petri Gynthere2aadb42015-03-25 12:35:14 -07002228static void bcmgenet_enable_tx_napi(struct bcmgenet_priv *priv)
2229{
2230 unsigned int i;
2231 struct bcmgenet_tx_ring *ring;
2232
2233 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2234 ring = &priv->tx_rings[i];
2235 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002236 ring->int_enable(ring);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002237 }
2238
2239 ring = &priv->tx_rings[DESC_INDEX];
2240 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002241 ring->int_enable(ring);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002242}
2243
2244static void bcmgenet_disable_tx_napi(struct bcmgenet_priv *priv)
2245{
2246 unsigned int i;
2247 struct bcmgenet_tx_ring *ring;
2248
2249 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2250 ring = &priv->tx_rings[i];
2251 napi_disable(&ring->napi);
2252 }
2253
2254 ring = &priv->tx_rings[DESC_INDEX];
2255 napi_disable(&ring->napi);
2256}
2257
2258static void bcmgenet_fini_tx_napi(struct bcmgenet_priv *priv)
2259{
2260 unsigned int i;
2261 struct bcmgenet_tx_ring *ring;
2262
2263 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2264 ring = &priv->tx_rings[i];
2265 netif_napi_del(&ring->napi);
2266 }
2267
2268 ring = &priv->tx_rings[DESC_INDEX];
2269 netif_napi_del(&ring->napi);
2270}
2271
Petri Gynther16c6d662015-02-23 11:00:45 -08002272/* Initialize Tx queues
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002273 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002274 * Queues 0-3 are priority-based, each one has 32 descriptors,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002275 * with queue 0 being the highest priority queue.
2276 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002277 * Queue 16 is the default Tx queue with
Petri Gynther51a966a2015-02-23 11:00:46 -08002278 * GENET_Q16_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors.
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002279 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002280 * The transmit control block pool is then partitioned as follows:
2281 * - Tx queue 0 uses tx_cbs[0..31]
2282 * - Tx queue 1 uses tx_cbs[32..63]
2283 * - Tx queue 2 uses tx_cbs[64..95]
2284 * - Tx queue 3 uses tx_cbs[96..127]
2285 * - Tx queue 16 uses tx_cbs[128..255]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002286 */
Petri Gynther16c6d662015-02-23 11:00:45 -08002287static void bcmgenet_init_tx_queues(struct net_device *dev)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002288{
2289 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther16c6d662015-02-23 11:00:45 -08002290 u32 i, dma_enable;
2291 u32 dma_ctrl, ring_cfg;
Petri Gynther37742162014-10-07 09:30:01 -07002292 u32 dma_priority[3] = {0, 0, 0};
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002293
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002294 dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
2295 dma_enable = dma_ctrl & DMA_EN;
2296 dma_ctrl &= ~DMA_EN;
2297 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
2298
Petri Gynther16c6d662015-02-23 11:00:45 -08002299 dma_ctrl = 0;
2300 ring_cfg = 0;
2301
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002302 /* Enable strict priority arbiter mode */
2303 bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
2304
Petri Gynther16c6d662015-02-23 11:00:45 -08002305 /* Initialize Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002306 for (i = 0; i < priv->hw_params->tx_queues; i++) {
Petri Gynther51a966a2015-02-23 11:00:46 -08002307 bcmgenet_init_tx_ring(priv, i, priv->hw_params->tx_bds_per_q,
2308 i * priv->hw_params->tx_bds_per_q,
2309 (i + 1) * priv->hw_params->tx_bds_per_q);
Petri Gynther16c6d662015-02-23 11:00:45 -08002310 ring_cfg |= (1 << i);
2311 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002312 dma_priority[DMA_PRIO_REG_INDEX(i)] |=
2313 ((GENET_Q0_PRIORITY + i) << DMA_PRIO_REG_SHIFT(i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002314 }
2315
Petri Gynther16c6d662015-02-23 11:00:45 -08002316 /* Initialize Tx default queue 16 */
Petri Gynther51a966a2015-02-23 11:00:46 -08002317 bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_Q16_TX_BD_CNT,
Petri Gynther16c6d662015-02-23 11:00:45 -08002318 priv->hw_params->tx_queues *
Petri Gynther51a966a2015-02-23 11:00:46 -08002319 priv->hw_params->tx_bds_per_q,
Petri Gynther16c6d662015-02-23 11:00:45 -08002320 TOTAL_DESC);
2321 ring_cfg |= (1 << DESC_INDEX);
2322 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002323 dma_priority[DMA_PRIO_REG_INDEX(DESC_INDEX)] |=
2324 ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) <<
2325 DMA_PRIO_REG_SHIFT(DESC_INDEX));
Petri Gynther16c6d662015-02-23 11:00:45 -08002326
2327 /* Set Tx queue priorities */
Petri Gynther37742162014-10-07 09:30:01 -07002328 bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
2329 bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
2330 bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);
2331
Petri Gynther16c6d662015-02-23 11:00:45 -08002332 /* Enable Tx queues */
2333 bcmgenet_tdma_writel(priv, ring_cfg, DMA_RING_CFG);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002334
Petri Gynther16c6d662015-02-23 11:00:45 -08002335 /* Enable Tx DMA */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002336 if (dma_enable)
Petri Gynther16c6d662015-02-23 11:00:45 -08002337 dma_ctrl |= DMA_EN;
2338 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002339}
2340
Petri Gynther3ab11332015-03-25 12:35:15 -07002341static void bcmgenet_enable_rx_napi(struct bcmgenet_priv *priv)
2342{
Petri Gynther4055eae2015-03-25 12:35:16 -07002343 unsigned int i;
2344 struct bcmgenet_rx_ring *ring;
2345
2346 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2347 ring = &priv->rx_rings[i];
2348 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002349 ring->int_enable(ring);
Petri Gynther4055eae2015-03-25 12:35:16 -07002350 }
2351
2352 ring = &priv->rx_rings[DESC_INDEX];
2353 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002354 ring->int_enable(ring);
Petri Gynther3ab11332015-03-25 12:35:15 -07002355}
2356
2357static void bcmgenet_disable_rx_napi(struct bcmgenet_priv *priv)
2358{
Petri Gynther4055eae2015-03-25 12:35:16 -07002359 unsigned int i;
2360 struct bcmgenet_rx_ring *ring;
2361
2362 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2363 ring = &priv->rx_rings[i];
2364 napi_disable(&ring->napi);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002365 cancel_work_sync(&ring->dim.dim.work);
Petri Gynther4055eae2015-03-25 12:35:16 -07002366 }
2367
2368 ring = &priv->rx_rings[DESC_INDEX];
2369 napi_disable(&ring->napi);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002370 cancel_work_sync(&ring->dim.dim.work);
Petri Gynther3ab11332015-03-25 12:35:15 -07002371}
2372
2373static void bcmgenet_fini_rx_napi(struct bcmgenet_priv *priv)
2374{
Petri Gynther4055eae2015-03-25 12:35:16 -07002375 unsigned int i;
2376 struct bcmgenet_rx_ring *ring;
2377
2378 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2379 ring = &priv->rx_rings[i];
2380 netif_napi_del(&ring->napi);
2381 }
2382
2383 ring = &priv->rx_rings[DESC_INDEX];
2384 netif_napi_del(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002385}
2386
Petri Gynther8ac467e2015-03-09 13:40:00 -07002387/* Initialize Rx queues
2388 *
2389 * Queues 0-15 are priority queues. Hardware Filtering Block (HFB) can be
2390 * used to direct traffic to these queues.
2391 *
2392 * Queue 16 is the default Rx queue with GENET_Q16_RX_BD_CNT descriptors.
2393 */
2394static int bcmgenet_init_rx_queues(struct net_device *dev)
2395{
2396 struct bcmgenet_priv *priv = netdev_priv(dev);
2397 u32 i;
2398 u32 dma_enable;
2399 u32 dma_ctrl;
2400 u32 ring_cfg;
2401 int ret;
2402
2403 dma_ctrl = bcmgenet_rdma_readl(priv, DMA_CTRL);
2404 dma_enable = dma_ctrl & DMA_EN;
2405 dma_ctrl &= ~DMA_EN;
2406 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2407
2408 dma_ctrl = 0;
2409 ring_cfg = 0;
2410
2411 /* Initialize Rx priority queues */
2412 for (i = 0; i < priv->hw_params->rx_queues; i++) {
2413 ret = bcmgenet_init_rx_ring(priv, i,
2414 priv->hw_params->rx_bds_per_q,
2415 i * priv->hw_params->rx_bds_per_q,
2416 (i + 1) *
2417 priv->hw_params->rx_bds_per_q);
2418 if (ret)
2419 return ret;
2420
2421 ring_cfg |= (1 << i);
2422 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2423 }
2424
2425 /* Initialize Rx default queue 16 */
2426 ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, GENET_Q16_RX_BD_CNT,
2427 priv->hw_params->rx_queues *
2428 priv->hw_params->rx_bds_per_q,
2429 TOTAL_DESC);
2430 if (ret)
2431 return ret;
2432
2433 ring_cfg |= (1 << DESC_INDEX);
2434 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
2435
2436 /* Enable rings */
2437 bcmgenet_rdma_writel(priv, ring_cfg, DMA_RING_CFG);
2438
2439 /* Configure ring as descriptor ring and re-enable DMA if enabled */
2440 if (dma_enable)
2441 dma_ctrl |= DMA_EN;
2442 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2443
2444 return 0;
2445}
2446
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002447static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
2448{
2449 int ret = 0;
2450 int timeout = 0;
2451 u32 reg;
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002452 u32 dma_ctrl;
2453 int i;
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002454
2455 /* Disable TDMA to stop add more frames in TX DMA */
2456 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2457 reg &= ~DMA_EN;
2458 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2459
2460 /* Check TDMA status register to confirm TDMA is disabled */
2461 while (timeout++ < DMA_TIMEOUT_VAL) {
2462 reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
2463 if (reg & DMA_DISABLED)
2464 break;
2465
2466 udelay(1);
2467 }
2468
2469 if (timeout == DMA_TIMEOUT_VAL) {
2470 netdev_warn(priv->dev, "Timed out while disabling TX DMA\n");
2471 ret = -ETIMEDOUT;
2472 }
2473
2474 /* Wait 10ms for packet drain in both tx and rx dma */
2475 usleep_range(10000, 20000);
2476
2477 /* Disable RDMA */
2478 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2479 reg &= ~DMA_EN;
2480 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2481
2482 timeout = 0;
2483 /* Check RDMA status register to confirm RDMA is disabled */
2484 while (timeout++ < DMA_TIMEOUT_VAL) {
2485 reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
2486 if (reg & DMA_DISABLED)
2487 break;
2488
2489 udelay(1);
2490 }
2491
2492 if (timeout == DMA_TIMEOUT_VAL) {
2493 netdev_warn(priv->dev, "Timed out while disabling RX DMA\n");
2494 ret = -ETIMEDOUT;
2495 }
2496
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002497 dma_ctrl = 0;
2498 for (i = 0; i < priv->hw_params->rx_queues; i++)
2499 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2500 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2501 reg &= ~dma_ctrl;
2502 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2503
2504 dma_ctrl = 0;
2505 for (i = 0; i < priv->hw_params->tx_queues; i++)
2506 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2507 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2508 reg &= ~dma_ctrl;
2509 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2510
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002511 return ret;
2512}
2513
Petri Gynther9abab962015-03-30 00:29:01 -07002514static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002515{
Petri Gynthere178c8c2016-04-09 00:20:36 -07002516 struct netdev_queue *txq;
Doug Bergerf48bed12017-07-14 16:12:10 -07002517 int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002518
Petri Gynther9abab962015-03-30 00:29:01 -07002519 bcmgenet_fini_rx_napi(priv);
2520 bcmgenet_fini_tx_napi(priv);
2521
Markus Elfring399e06a2019-08-22 20:02:56 +02002522 for (i = 0; i < priv->num_tx_bds; i++)
2523 dev_kfree_skb(bcmgenet_free_tx_cb(&priv->pdev->dev,
2524 priv->tx_cbs + i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002525
Petri Gynthere178c8c2016-04-09 00:20:36 -07002526 for (i = 0; i < priv->hw_params->tx_queues; i++) {
2527 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[i].queue);
2528 netdev_tx_reset_queue(txq);
2529 }
2530
2531 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[DESC_INDEX].queue);
2532 netdev_tx_reset_queue(txq);
2533
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002534 bcmgenet_free_rx_buffers(priv);
2535 kfree(priv->rx_cbs);
2536 kfree(priv->tx_cbs);
2537}
2538
2539/* init_edma: Initialize DMA control register */
2540static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
2541{
2542 int ret;
Petri Gynther014012a2015-02-23 11:00:45 -08002543 unsigned int i;
2544 struct enet_cb *cb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002545
Petri Gynther6f5a2722015-03-06 13:45:00 -08002546 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002547
Petri Gynther6f5a2722015-03-06 13:45:00 -08002548 /* Initialize common Rx ring structures */
2549 priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
2550 priv->num_rx_bds = TOTAL_DESC;
2551 priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct enet_cb),
2552 GFP_KERNEL);
2553 if (!priv->rx_cbs)
2554 return -ENOMEM;
2555
2556 for (i = 0; i < priv->num_rx_bds; i++) {
2557 cb = priv->rx_cbs + i;
2558 cb->bd_addr = priv->rx_bds + i * DMA_DESC_SIZE;
2559 }
2560
Brian Norris7fc527f2014-07-29 14:34:14 -07002561 /* Initialize common TX ring structures */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002562 priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
2563 priv->num_tx_bds = TOTAL_DESC;
Florian Fainellic489be02014-07-23 10:42:15 -07002564 priv->tx_cbs = kcalloc(priv->num_tx_bds, sizeof(struct enet_cb),
Florian Fainellic91b7f62014-07-23 10:42:12 -07002565 GFP_KERNEL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002566 if (!priv->tx_cbs) {
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002567 kfree(priv->rx_cbs);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002568 return -ENOMEM;
2569 }
2570
Petri Gynther014012a2015-02-23 11:00:45 -08002571 for (i = 0; i < priv->num_tx_bds; i++) {
2572 cb = priv->tx_cbs + i;
2573 cb->bd_addr = priv->tx_bds + i * DMA_DESC_SIZE;
2574 }
2575
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002576 /* Init rDma */
Stefan Wahrena50e3a92019-11-11 20:49:23 +01002577 bcmgenet_rdma_writel(priv, priv->dma_max_burst_length,
2578 DMA_SCB_BURST_SIZE);
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002579
2580 /* Initialize Rx queues */
2581 ret = bcmgenet_init_rx_queues(priv->dev);
2582 if (ret) {
2583 netdev_err(priv->dev, "failed to initialize Rx queues\n");
2584 bcmgenet_free_rx_buffers(priv);
2585 kfree(priv->rx_cbs);
2586 kfree(priv->tx_cbs);
2587 return ret;
2588 }
2589
2590 /* Init tDma */
Stefan Wahrena50e3a92019-11-11 20:49:23 +01002591 bcmgenet_tdma_writel(priv, priv->dma_max_burst_length,
2592 DMA_SCB_BURST_SIZE);
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002593
Petri Gynther16c6d662015-02-23 11:00:45 -08002594 /* Initialize Tx queues */
2595 bcmgenet_init_tx_queues(priv->dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002596
2597 return 0;
2598}
2599
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002600/* Interrupt bottom half */
2601static void bcmgenet_irq_task(struct work_struct *work)
2602{
Doug Berger07c52d62017-03-09 16:58:47 -08002603 unsigned int status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002604 struct bcmgenet_priv *priv = container_of(
2605 work, struct bcmgenet_priv, bcmgenet_irq_work);
2606
2607 netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
2608
Doug Bergerb0447ec2017-10-25 15:04:17 -07002609 spin_lock_irq(&priv->lock);
Doug Berger07c52d62017-03-09 16:58:47 -08002610 status = priv->irq0_stat;
2611 priv->irq0_stat = 0;
Doug Bergerb0447ec2017-10-25 15:04:17 -07002612 spin_unlock_irq(&priv->lock);
Doug Berger07c52d62017-03-09 16:58:47 -08002613
Doug Berger25382b92019-10-16 16:06:32 -07002614 if (status & UMAC_IRQ_PHY_DET_R &&
Doug Berger0686bd92019-11-05 11:07:26 -08002615 priv->dev->phydev->autoneg != AUTONEG_ENABLE) {
Doug Berger25382b92019-10-16 16:06:32 -07002616 phy_init_hw(priv->dev->phydev);
Doug Berger0686bd92019-11-05 11:07:26 -08002617 genphy_config_aneg(priv->dev->phydev);
2618 }
Doug Berger25382b92019-10-16 16:06:32 -07002619
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002620 /* Link UP/DOWN event */
Doug Berger7de48402019-10-16 16:06:29 -07002621 if (status & UMAC_IRQ_LINK_EVENT)
Heiner Kallweit28b2e0d2018-01-10 21:21:31 +01002622 phy_mac_interrupt(priv->dev->phydev);
Doug Berger25382b92019-10-16 16:06:32 -07002623
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002624}
2625
Petri Gynther4055eae2015-03-25 12:35:16 -07002626/* bcmgenet_isr1: handle Rx and Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002627static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
2628{
2629 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002630 struct bcmgenet_rx_ring *rx_ring;
2631 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08002632 unsigned int index, status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002633
Doug Berger07c52d62017-03-09 16:58:47 -08002634 /* Read irq status */
2635 status = bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002636 ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002637
Brian Norris7fc527f2014-07-29 14:34:14 -07002638 /* clear interrupts */
Doug Berger07c52d62017-03-09 16:58:47 -08002639 bcmgenet_intrl2_1_writel(priv, status, INTRL2_CPU_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002640
2641 netif_dbg(priv, intr, priv->dev,
Doug Berger07c52d62017-03-09 16:58:47 -08002642 "%s: IRQ=0x%x\n", __func__, status);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002643
Petri Gynther4055eae2015-03-25 12:35:16 -07002644 /* Check Rx priority queue interrupts */
2645 for (index = 0; index < priv->hw_params->rx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08002646 if (!(status & BIT(UMAC_IRQ1_RX_INTR_SHIFT + index)))
Petri Gynther4055eae2015-03-25 12:35:16 -07002647 continue;
2648
2649 rx_ring = &priv->rx_rings[index];
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002650 rx_ring->dim.event_ctr++;
Petri Gynther4055eae2015-03-25 12:35:16 -07002651
2652 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2653 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002654 __napi_schedule_irqoff(&rx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07002655 }
2656 }
2657
2658 /* Check Tx priority queue interrupts */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002659 for (index = 0; index < priv->hw_params->tx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08002660 if (!(status & BIT(index)))
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002661 continue;
2662
Petri Gynther4055eae2015-03-25 12:35:16 -07002663 tx_ring = &priv->tx_rings[index];
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002664
Petri Gynther4055eae2015-03-25 12:35:16 -07002665 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2666 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002667 __napi_schedule_irqoff(&tx_ring->napi);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002668 }
2669 }
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002670
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002671 return IRQ_HANDLED;
2672}
2673
Petri Gynther4055eae2015-03-25 12:35:16 -07002674/* bcmgenet_isr0: handle Rx and Tx default queues + other stuff */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002675static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
2676{
2677 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002678 struct bcmgenet_rx_ring *rx_ring;
2679 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08002680 unsigned int status;
2681 unsigned long flags;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002682
Doug Berger07c52d62017-03-09 16:58:47 -08002683 /* Read irq status */
2684 status = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002685 ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002686
Brian Norris7fc527f2014-07-29 14:34:14 -07002687 /* clear interrupts */
Doug Berger07c52d62017-03-09 16:58:47 -08002688 bcmgenet_intrl2_0_writel(priv, status, INTRL2_CPU_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002689
2690 netif_dbg(priv, intr, priv->dev,
Doug Berger07c52d62017-03-09 16:58:47 -08002691 "IRQ=0x%x\n", status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002692
Doug Berger07c52d62017-03-09 16:58:47 -08002693 if (status & UMAC_IRQ_RXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07002694 rx_ring = &priv->rx_rings[DESC_INDEX];
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002695 rx_ring->dim.event_ctr++;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002696
Petri Gynther4055eae2015-03-25 12:35:16 -07002697 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2698 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002699 __napi_schedule_irqoff(&rx_ring->napi);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002700 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002701 }
Petri Gynther4055eae2015-03-25 12:35:16 -07002702
Doug Berger07c52d62017-03-09 16:58:47 -08002703 if (status & UMAC_IRQ_TXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07002704 tx_ring = &priv->tx_rings[DESC_INDEX];
2705
2706 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2707 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002708 __napi_schedule_irqoff(&tx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07002709 }
2710 }
2711
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002712 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Doug Berger07c52d62017-03-09 16:58:47 -08002713 status & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002714 wake_up(&priv->wq);
2715 }
2716
Doug Berger07c52d62017-03-09 16:58:47 -08002717 /* all other interested interrupts handled in bottom half */
Doug Berger25382b92019-10-16 16:06:32 -07002718 status &= (UMAC_IRQ_LINK_EVENT | UMAC_IRQ_PHY_DET_R);
Doug Berger07c52d62017-03-09 16:58:47 -08002719 if (status) {
2720 /* Save irq status for bottom-half processing. */
2721 spin_lock_irqsave(&priv->lock, flags);
2722 priv->irq0_stat |= status;
2723 spin_unlock_irqrestore(&priv->lock, flags);
2724
2725 schedule_work(&priv->bcmgenet_irq_work);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002726 }
2727
2728 return IRQ_HANDLED;
2729}
2730
Florian Fainelli85620562014-07-21 15:29:23 -07002731static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
2732{
2733 struct bcmgenet_priv *priv = dev_id;
2734
2735 pm_wakeup_event(&priv->pdev->dev, 0);
2736
2737 return IRQ_HANDLED;
2738}
2739
Florian Fainelli4d2e8882015-07-31 11:42:54 -07002740#ifdef CONFIG_NET_POLL_CONTROLLER
2741static void bcmgenet_poll_controller(struct net_device *dev)
2742{
2743 struct bcmgenet_priv *priv = netdev_priv(dev);
2744
2745 /* Invoke the main RX/TX interrupt handler */
2746 disable_irq(priv->irq0);
2747 bcmgenet_isr0(priv->irq0, priv);
2748 enable_irq(priv->irq0);
2749
2750 /* And the interrupt handler for RX/TX priority queues */
2751 disable_irq(priv->irq1);
2752 bcmgenet_isr1(priv->irq1, priv);
2753 enable_irq(priv->irq1);
2754}
2755#endif
2756
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002757static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
2758{
2759 u32 reg;
2760
2761 reg = bcmgenet_rbuf_ctrl_get(priv);
2762 reg |= BIT(1);
2763 bcmgenet_rbuf_ctrl_set(priv, reg);
2764 udelay(10);
2765
2766 reg &= ~BIT(1);
2767 bcmgenet_rbuf_ctrl_set(priv, reg);
2768 udelay(10);
2769}
2770
2771static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002772 unsigned char *addr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002773{
2774 bcmgenet_umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
2775 (addr[2] << 8) | addr[3], UMAC_MAC0);
2776 bcmgenet_umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
2777}
2778
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002779/* Returns a reusable dma control register value */
2780static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
2781{
2782 u32 reg;
2783 u32 dma_ctrl;
2784
2785 /* disable DMA */
2786 dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
2787 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2788 reg &= ~dma_ctrl;
2789 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2790
2791 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2792 reg &= ~dma_ctrl;
2793 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2794
2795 bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
2796 udelay(10);
2797 bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
2798
2799 return dma_ctrl;
2800}
2801
2802static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
2803{
2804 u32 reg;
2805
2806 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2807 reg |= dma_ctrl;
2808 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2809
2810 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2811 reg |= dma_ctrl;
2812 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2813}
2814
Petri Gynther0034de42015-03-13 14:45:00 -07002815/* bcmgenet_hfb_clear
2816 *
2817 * Clear Hardware Filter Block and disable all filtering.
2818 */
2819static void bcmgenet_hfb_clear(struct bcmgenet_priv *priv)
2820{
2821 u32 i;
2822
2823 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_CTRL);
2824 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS);
2825 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS + 4);
2826
2827 for (i = DMA_INDEX2RING_0; i <= DMA_INDEX2RING_7; i++)
2828 bcmgenet_rdma_writel(priv, 0x0, i);
2829
2830 for (i = 0; i < (priv->hw_params->hfb_filter_cnt / 4); i++)
2831 bcmgenet_hfb_reg_writel(priv, 0x0,
2832 HFB_FLT_LEN_V3PLUS + i * sizeof(u32));
2833
2834 for (i = 0; i < priv->hw_params->hfb_filter_cnt *
2835 priv->hw_params->hfb_filter_size; i++)
2836 bcmgenet_hfb_writel(priv, 0x0, i * sizeof(u32));
2837}
2838
2839static void bcmgenet_hfb_init(struct bcmgenet_priv *priv)
2840{
2841 if (GENET_IS_V1(priv) || GENET_IS_V2(priv))
2842 return;
2843
2844 bcmgenet_hfb_clear(priv);
2845}
2846
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002847static void bcmgenet_netif_start(struct net_device *dev)
2848{
2849 struct bcmgenet_priv *priv = netdev_priv(dev);
2850
2851 /* Start the network engine */
Petri Gynther3ab11332015-03-25 12:35:15 -07002852 bcmgenet_enable_rx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002853
2854 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
2855
Doug Bergerd215dba2017-10-25 15:04:16 -07002856 bcmgenet_enable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002857
Florian Fainelli37850e32015-10-17 14:22:46 -07002858 /* Monitor link interrupts now */
2859 bcmgenet_link_intr_enable(priv);
2860
Doug Berger6c97f012017-10-25 15:04:19 -07002861 phy_start(dev->phydev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002862}
2863
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002864static int bcmgenet_open(struct net_device *dev)
2865{
2866 struct bcmgenet_priv *priv = netdev_priv(dev);
2867 unsigned long dma_ctrl;
2868 u32 reg;
2869 int ret;
2870
2871 netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
2872
2873 /* Turn on the clock */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002874 clk_prepare_enable(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002875
Florian Fainellia642c4f2015-03-23 15:09:56 -07002876 /* If this is an internal GPHY, power it back on now, before UniMAC is
2877 * brought out of reset as absolutely no UniMAC activity is allowed
2878 */
Florian Fainellic624f892015-07-16 15:51:17 -07002879 if (priv->internal_phy)
Florian Fainellia642c4f2015-03-23 15:09:56 -07002880 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
2881
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002882 /* take MAC out of reset */
2883 bcmgenet_umac_reset(priv);
2884
Doug Berger28c2d1a2017-10-25 15:04:13 -07002885 init_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002886
Doug Berger206f54b2019-12-17 16:51:12 -08002887 /* Apply features again in case we changed them while interface was
2888 * down
2889 */
2890 bcmgenet_set_features(dev, dev->features);
2891
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002892 bcmgenet_set_hw_addr(priv, dev->dev_addr);
2893
Florian Fainellic624f892015-07-16 15:51:17 -07002894 if (priv->internal_phy) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002895 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
2896 reg |= EXT_ENERGY_DET_MASK;
2897 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
2898 }
2899
2900 /* Disable RX/TX DMA and flush TX queues */
2901 dma_ctrl = bcmgenet_dma_disable(priv);
2902
2903 /* Reinitialize TDMA and RDMA and SW housekeeping */
2904 ret = bcmgenet_init_dma(priv);
2905 if (ret) {
2906 netdev_err(dev, "failed to initialize DMA\n");
Doug Berger6b6d017f2019-11-05 11:07:25 -08002907 goto err_clk_disable;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002908 }
2909
2910 /* Always enable ring 16 - descriptor ring */
2911 bcmgenet_enable_dma(priv, dma_ctrl);
2912
Petri Gynther0034de42015-03-13 14:45:00 -07002913 /* HFB init */
2914 bcmgenet_hfb_init(priv);
2915
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002916 ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002917 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002918 if (ret < 0) {
2919 netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
2920 goto err_fini_dma;
2921 }
2922
2923 ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002924 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002925 if (ret < 0) {
2926 netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
2927 goto err_irq0;
2928 }
2929
Doug Berger6b6d017f2019-11-05 11:07:25 -08002930 ret = bcmgenet_mii_probe(dev);
2931 if (ret) {
2932 netdev_err(dev, "failed to connect to PHY\n");
2933 goto err_irq1;
2934 }
2935
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002936 bcmgenet_netif_start(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002937
Doug Berger09e805d2018-11-01 15:55:37 -07002938 netif_tx_start_all_queues(dev);
2939
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002940 return 0;
2941
Doug Berger6b6d017f2019-11-05 11:07:25 -08002942err_irq1:
2943 free_irq(priv->irq1, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002944err_irq0:
Florian Fainelli978ffac42015-07-16 15:51:15 -07002945 free_irq(priv->irq0, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002946err_fini_dma:
Doug Berger4fd6dc92017-10-25 15:04:12 -07002947 bcmgenet_dma_teardown(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002948 bcmgenet_fini_dma(priv);
2949err_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08002950 if (priv->internal_phy)
2951 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002952 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002953 return ret;
2954}
2955
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002956static void bcmgenet_netif_stop(struct net_device *dev)
2957{
2958 struct bcmgenet_priv *priv = netdev_priv(dev);
2959
Doug Bergerd215dba2017-10-25 15:04:16 -07002960 bcmgenet_disable_tx_napi(priv);
Doug Berger09e805d2018-11-01 15:55:37 -07002961 netif_tx_disable(dev);
Doug Bergerd215dba2017-10-25 15:04:16 -07002962
2963 /* Disable MAC receive */
2964 umac_enable_set(priv, CMD_RX_EN, false);
2965
2966 bcmgenet_dma_teardown(priv);
2967
2968 /* Disable MAC transmit. TX DMA disabled must be done before this */
2969 umac_enable_set(priv, CMD_TX_EN, false);
2970
Doug Berger6c97f012017-10-25 15:04:19 -07002971 phy_stop(dev->phydev);
Petri Gynther3ab11332015-03-25 12:35:15 -07002972 bcmgenet_disable_rx_napi(priv);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002973 bcmgenet_intr_disable(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002974
2975 /* Wait for pending work items to complete. Since interrupts are
2976 * disabled no new work will be scheduled.
2977 */
2978 cancel_work_sync(&priv->bcmgenet_irq_work);
Florian Fainellicc013fb2014-08-11 14:50:43 -07002979
Florian Fainellicc013fb2014-08-11 14:50:43 -07002980 priv->old_link = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002981 priv->old_speed = -1;
Florian Fainellicc013fb2014-08-11 14:50:43 -07002982 priv->old_duplex = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002983 priv->old_pause = -1;
Doug Bergerd215dba2017-10-25 15:04:16 -07002984
2985 /* tx reclaim */
2986 bcmgenet_tx_reclaim_all(dev);
2987 bcmgenet_fini_dma(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002988}
2989
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002990static int bcmgenet_close(struct net_device *dev)
2991{
2992 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Bergerd215dba2017-10-25 15:04:16 -07002993 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002994
2995 netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
2996
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002997 bcmgenet_netif_stop(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002998
Florian Fainellic96e7312014-11-10 18:06:20 -08002999 /* Really kill the PHY state machine and disconnect from it */
Doug Berger6c97f012017-10-25 15:04:19 -07003000 phy_disconnect(dev->phydev);
Florian Fainellic96e7312014-11-10 18:06:20 -08003001
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003002 free_irq(priv->irq0, priv);
3003 free_irq(priv->irq1, priv);
3004
Florian Fainellic624f892015-07-16 15:51:17 -07003005 if (priv->internal_phy)
Florian Fainellica8cf342015-03-23 15:09:51 -07003006 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003007
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003008 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003009
Florian Fainellica8cf342015-03-23 15:09:51 -07003010 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003011}
3012
Florian Fainelli13ea6572015-06-04 16:15:50 -07003013static void bcmgenet_dump_tx_queue(struct bcmgenet_tx_ring *ring)
3014{
3015 struct bcmgenet_priv *priv = ring->priv;
3016 u32 p_index, c_index, intsts, intmsk;
3017 struct netdev_queue *txq;
3018 unsigned int free_bds;
Florian Fainelli13ea6572015-06-04 16:15:50 -07003019 bool txq_stopped;
3020
3021 if (!netif_msg_tx_err(priv))
3022 return;
3023
3024 txq = netdev_get_tx_queue(priv->dev, ring->queue);
3025
Doug Bergerb0447ec2017-10-25 15:04:17 -07003026 spin_lock(&ring->lock);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003027 if (ring->index == DESC_INDEX) {
3028 intsts = ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
3029 intmsk = UMAC_IRQ_TXDMA_DONE | UMAC_IRQ_TXDMA_MBDONE;
3030 } else {
3031 intsts = ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
3032 intmsk = 1 << ring->index;
3033 }
3034 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
3035 p_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_PROD_INDEX);
3036 txq_stopped = netif_tx_queue_stopped(txq);
3037 free_bds = ring->free_bds;
Doug Bergerb0447ec2017-10-25 15:04:17 -07003038 spin_unlock(&ring->lock);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003039
3040 netif_err(priv, tx_err, priv->dev, "Ring %d queue %d status summary\n"
3041 "TX queue status: %s, interrupts: %s\n"
3042 "(sw)free_bds: %d (sw)size: %d\n"
3043 "(sw)p_index: %d (hw)p_index: %d\n"
3044 "(sw)c_index: %d (hw)c_index: %d\n"
3045 "(sw)clean_p: %d (sw)write_p: %d\n"
3046 "(sw)cb_ptr: %d (sw)end_ptr: %d\n",
3047 ring->index, ring->queue,
3048 txq_stopped ? "stopped" : "active",
3049 intsts & intmsk ? "enabled" : "disabled",
3050 free_bds, ring->size,
3051 ring->prod_index, p_index & DMA_P_INDEX_MASK,
3052 ring->c_index, c_index & DMA_C_INDEX_MASK,
3053 ring->clean_ptr, ring->write_ptr,
3054 ring->cb_ptr, ring->end_ptr);
3055}
3056
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05003057static void bcmgenet_timeout(struct net_device *dev, unsigned int txqueue)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003058{
3059 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003060 u32 int0_enable = 0;
3061 u32 int1_enable = 0;
3062 unsigned int q;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003063
3064 netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
3065
Florian Fainelli13ea6572015-06-04 16:15:50 -07003066 for (q = 0; q < priv->hw_params->tx_queues; q++)
3067 bcmgenet_dump_tx_queue(&priv->tx_rings[q]);
3068 bcmgenet_dump_tx_queue(&priv->tx_rings[DESC_INDEX]);
3069
3070 bcmgenet_tx_reclaim_all(dev);
3071
3072 for (q = 0; q < priv->hw_params->tx_queues; q++)
3073 int1_enable |= (1 << q);
3074
3075 int0_enable = UMAC_IRQ_TXDMA_DONE;
3076
3077 /* Re-enable TX interrupts if disabled */
3078 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
3079 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
3080
Florian Westphal860e9532016-05-03 16:33:13 +02003081 netif_trans_update(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003082
3083 dev->stats.tx_errors++;
3084
3085 netif_tx_wake_all_queues(dev);
3086}
3087
Justin Chen35cbef92019-07-17 14:58:53 -07003088#define MAX_MDF_FILTER 17
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003089
3090static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
3091 unsigned char *addr,
Justin Chen35cbef92019-07-17 14:58:53 -07003092 int *i)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003093{
Florian Fainellic91b7f62014-07-23 10:42:12 -07003094 bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
3095 UMAC_MDF_ADDR + (*i * 4));
3096 bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
3097 addr[4] << 8 | addr[5],
3098 UMAC_MDF_ADDR + ((*i + 1) * 4));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003099 *i += 2;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003100}
3101
3102static void bcmgenet_set_rx_mode(struct net_device *dev)
3103{
3104 struct bcmgenet_priv *priv = netdev_priv(dev);
3105 struct netdev_hw_addr *ha;
Justin Chen35cbef92019-07-17 14:58:53 -07003106 int i, nfilter;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003107 u32 reg;
3108
3109 netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
3110
Justin Chen35cbef92019-07-17 14:58:53 -07003111 /* Number of filters needed */
3112 nfilter = netdev_uc_count(dev) + netdev_mc_count(dev) + 2;
3113
3114 /*
3115 * Turn on promicuous mode for three scenarios
3116 * 1. IFF_PROMISC flag is set
3117 * 2. IFF_ALLMULTI flag is set
3118 * 3. The number of filters needed exceeds the number filters
3119 * supported by the hardware.
3120 */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003121 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
Justin Chen35cbef92019-07-17 14:58:53 -07003122 if ((dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) ||
3123 (nfilter > MAX_MDF_FILTER)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003124 reg |= CMD_PROMISC;
3125 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3126 bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
3127 return;
3128 } else {
3129 reg &= ~CMD_PROMISC;
3130 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3131 }
3132
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003133 /* update MDF filter */
3134 i = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003135 /* Broadcast */
Justin Chen35cbef92019-07-17 14:58:53 -07003136 bcmgenet_set_mdf_addr(priv, dev->broadcast, &i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003137 /* my own address.*/
Justin Chen35cbef92019-07-17 14:58:53 -07003138 bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003139
Justin Chen35cbef92019-07-17 14:58:53 -07003140 /* Unicast */
3141 netdev_for_each_uc_addr(ha, dev)
3142 bcmgenet_set_mdf_addr(priv, ha->addr, &i);
3143
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003144 /* Multicast */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003145 netdev_for_each_mc_addr(ha, dev)
Justin Chen35cbef92019-07-17 14:58:53 -07003146 bcmgenet_set_mdf_addr(priv, ha->addr, &i);
3147
3148 /* Enable filters */
3149 reg = GENMASK(MAX_MDF_FILTER - 1, MAX_MDF_FILTER - nfilter);
3150 bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003151}
3152
3153/* Set the hardware MAC address. */
3154static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
3155{
3156 struct sockaddr *addr = p;
3157
3158 /* Setting the MAC address at the hardware level is not possible
3159 * without disabling the UniMAC RX/TX enable bits.
3160 */
3161 if (netif_running(dev))
3162 return -EBUSY;
3163
3164 ether_addr_copy(dev->dev_addr, addr->sa_data);
3165
3166 return 0;
3167}
3168
Florian Fainelli37a30b42017-03-16 10:27:08 -07003169static struct net_device_stats *bcmgenet_get_stats(struct net_device *dev)
3170{
3171 struct bcmgenet_priv *priv = netdev_priv(dev);
3172 unsigned long tx_bytes = 0, tx_packets = 0;
3173 unsigned long rx_bytes = 0, rx_packets = 0;
3174 unsigned long rx_errors = 0, rx_dropped = 0;
3175 struct bcmgenet_tx_ring *tx_ring;
3176 struct bcmgenet_rx_ring *rx_ring;
3177 unsigned int q;
3178
3179 for (q = 0; q < priv->hw_params->tx_queues; q++) {
3180 tx_ring = &priv->tx_rings[q];
3181 tx_bytes += tx_ring->bytes;
3182 tx_packets += tx_ring->packets;
3183 }
3184 tx_ring = &priv->tx_rings[DESC_INDEX];
3185 tx_bytes += tx_ring->bytes;
3186 tx_packets += tx_ring->packets;
3187
3188 for (q = 0; q < priv->hw_params->rx_queues; q++) {
3189 rx_ring = &priv->rx_rings[q];
3190
3191 rx_bytes += rx_ring->bytes;
3192 rx_packets += rx_ring->packets;
3193 rx_errors += rx_ring->errors;
3194 rx_dropped += rx_ring->dropped;
3195 }
3196 rx_ring = &priv->rx_rings[DESC_INDEX];
3197 rx_bytes += rx_ring->bytes;
3198 rx_packets += rx_ring->packets;
3199 rx_errors += rx_ring->errors;
3200 rx_dropped += rx_ring->dropped;
3201
3202 dev->stats.tx_bytes = tx_bytes;
3203 dev->stats.tx_packets = tx_packets;
3204 dev->stats.rx_bytes = rx_bytes;
3205 dev->stats.rx_packets = rx_packets;
3206 dev->stats.rx_errors = rx_errors;
3207 dev->stats.rx_missed_errors = rx_errors;
3208 return &dev->stats;
3209}
3210
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003211static const struct net_device_ops bcmgenet_netdev_ops = {
3212 .ndo_open = bcmgenet_open,
3213 .ndo_stop = bcmgenet_close,
3214 .ndo_start_xmit = bcmgenet_xmit,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003215 .ndo_tx_timeout = bcmgenet_timeout,
3216 .ndo_set_rx_mode = bcmgenet_set_rx_mode,
3217 .ndo_set_mac_address = bcmgenet_set_mac_addr,
3218 .ndo_do_ioctl = bcmgenet_ioctl,
3219 .ndo_set_features = bcmgenet_set_features,
Florian Fainelli4d2e8882015-07-31 11:42:54 -07003220#ifdef CONFIG_NET_POLL_CONTROLLER
3221 .ndo_poll_controller = bcmgenet_poll_controller,
3222#endif
Florian Fainelli37a30b42017-03-16 10:27:08 -07003223 .ndo_get_stats = bcmgenet_get_stats,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003224};
3225
3226/* Array of GENET hardware parameters/characteristics */
3227static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
3228 [GENET_V1] = {
3229 .tx_queues = 0,
Petri Gynther51a966a2015-02-23 11:00:46 -08003230 .tx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003231 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003232 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003233 .bp_in_en_shift = 16,
3234 .bp_in_mask = 0xffff,
3235 .hfb_filter_cnt = 16,
3236 .qtag_mask = 0x1F,
3237 .hfb_offset = 0x1000,
3238 .rdma_offset = 0x2000,
3239 .tdma_offset = 0x3000,
3240 .words_per_bd = 2,
3241 },
3242 [GENET_V2] = {
3243 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003244 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003245 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003246 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003247 .bp_in_en_shift = 16,
3248 .bp_in_mask = 0xffff,
3249 .hfb_filter_cnt = 16,
3250 .qtag_mask = 0x1F,
3251 .tbuf_offset = 0x0600,
3252 .hfb_offset = 0x1000,
3253 .hfb_reg_offset = 0x2000,
3254 .rdma_offset = 0x3000,
3255 .tdma_offset = 0x4000,
3256 .words_per_bd = 2,
3257 .flags = GENET_HAS_EXT,
3258 },
3259 [GENET_V3] = {
3260 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003261 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003262 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003263 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003264 .bp_in_en_shift = 17,
3265 .bp_in_mask = 0x1ffff,
3266 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003267 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003268 .qtag_mask = 0x3F,
3269 .tbuf_offset = 0x0600,
3270 .hfb_offset = 0x8000,
3271 .hfb_reg_offset = 0xfc00,
3272 .rdma_offset = 0x10000,
3273 .tdma_offset = 0x11000,
3274 .words_per_bd = 2,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003275 .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR |
3276 GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003277 },
3278 [GENET_V4] = {
3279 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003280 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003281 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003282 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003283 .bp_in_en_shift = 17,
3284 .bp_in_mask = 0x1ffff,
3285 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003286 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003287 .qtag_mask = 0x3F,
3288 .tbuf_offset = 0x0600,
3289 .hfb_offset = 0x8000,
3290 .hfb_reg_offset = 0xfc00,
3291 .rdma_offset = 0x2000,
3292 .tdma_offset = 0x4000,
3293 .words_per_bd = 3,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003294 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3295 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003296 },
Doug Berger42138082017-03-13 17:41:42 -07003297 [GENET_V5] = {
3298 .tx_queues = 4,
3299 .tx_bds_per_q = 32,
3300 .rx_queues = 0,
3301 .rx_bds_per_q = 0,
3302 .bp_in_en_shift = 17,
3303 .bp_in_mask = 0x1ffff,
3304 .hfb_filter_cnt = 48,
3305 .hfb_filter_size = 128,
3306 .qtag_mask = 0x3F,
3307 .tbuf_offset = 0x0600,
3308 .hfb_offset = 0x8000,
3309 .hfb_reg_offset = 0xfc00,
3310 .rdma_offset = 0x2000,
3311 .tdma_offset = 0x4000,
3312 .words_per_bd = 3,
3313 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3314 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
3315 },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003316};
3317
3318/* Infer hardware parameters from the detected GENET version */
3319static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
3320{
3321 struct bcmgenet_hw_params *params;
3322 u32 reg;
3323 u8 major;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003324 u16 gphy_rev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003325
Doug Berger42138082017-03-13 17:41:42 -07003326 if (GENET_IS_V5(priv) || GENET_IS_V4(priv)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003327 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3328 genet_dma_ring_regs = genet_dma_ring_regs_v4;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003329 } else if (GENET_IS_V3(priv)) {
3330 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3331 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003332 } else if (GENET_IS_V2(priv)) {
3333 bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
3334 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003335 } else if (GENET_IS_V1(priv)) {
3336 bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
3337 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003338 }
3339
3340 /* enum genet_version starts at 1 */
3341 priv->hw_params = &bcmgenet_hw_params[priv->version];
3342 params = priv->hw_params;
3343
3344 /* Read GENET HW version */
3345 reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
3346 major = (reg >> 24 & 0x0f);
Doug Berger42138082017-03-13 17:41:42 -07003347 if (major == 6)
3348 major = 5;
3349 else if (major == 5)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003350 major = 4;
3351 else if (major == 0)
3352 major = 1;
3353 if (major != priv->version) {
3354 dev_err(&priv->pdev->dev,
3355 "GENET version mismatch, got: %d, configured for: %d\n",
3356 major, priv->version);
3357 }
3358
3359 /* Print the GENET core version */
3360 dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003361 major, (reg >> 16) & 0x0f, reg & 0xffff);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003362
Florian Fainelli487320c2014-09-19 13:07:53 -07003363 /* Store the integrated PHY revision for the MDIO probing function
3364 * to pass this information to the PHY driver. The PHY driver expects
3365 * to find the PHY major revision in bits 15:8 while the GENET register
3366 * stores that information in bits 7:0, account for that.
Florian Fainellib04a2f52014-12-03 09:56:59 -08003367 *
3368 * On newer chips, starting with PHY revision G0, a new scheme is
3369 * deployed similar to the Starfighter 2 switch with GPHY major
3370 * revision in bits 15:8 and patch level in bits 7:0. Major revision 0
3371 * is reserved as well as special value 0x01ff, we have a small
3372 * heuristic to check for the new GPHY revision and re-arrange things
3373 * so the GPHY driver is happy.
Florian Fainelli487320c2014-09-19 13:07:53 -07003374 */
Florian Fainellib04a2f52014-12-03 09:56:59 -08003375 gphy_rev = reg & 0xffff;
3376
Doug Berger42138082017-03-13 17:41:42 -07003377 if (GENET_IS_V5(priv)) {
3378 /* The EPHY revision should come from the MDIO registers of
3379 * the PHY not from GENET.
3380 */
3381 if (gphy_rev != 0) {
3382 pr_warn("GENET is reporting EPHY revision: 0x%04x\n",
3383 gphy_rev);
3384 }
Doug Bergereca4bad2017-03-09 16:58:45 -08003385 /* This is reserved so should require special treatment */
David S. Miller101c4312017-03-15 11:59:10 -07003386 } else if (gphy_rev == 0 || gphy_rev == 0x01ff) {
Doug Bergereca4bad2017-03-09 16:58:45 -08003387 pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev);
3388 return;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003389 /* This is the good old scheme, just GPHY major, no minor nor patch */
Doug Berger42138082017-03-13 17:41:42 -07003390 } else if ((gphy_rev & 0xf0) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003391 priv->gphy_rev = gphy_rev << 8;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003392 /* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */
Doug Berger42138082017-03-13 17:41:42 -07003393 } else if ((gphy_rev & 0xff00) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003394 priv->gphy_rev = gphy_rev;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003395 }
Florian Fainelli487320c2014-09-19 13:07:53 -07003396
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003397#ifdef CONFIG_PHYS_ADDR_T_64BIT
3398 if (!(params->flags & GENET_HAS_40BITS))
3399 pr_warn("GENET does not support 40-bits PA\n");
3400#endif
3401
3402 pr_debug("Configuration for version: %d\n"
Petri Gynther3feafa02015-03-05 17:40:14 -08003403 "TXq: %1d, TXqBDs: %1d, RXq: %1d, RXqBDs: %1d\n"
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003404 "BP << en: %2d, BP msk: 0x%05x\n"
3405 "HFB count: %2d, QTAQ msk: 0x%05x\n"
3406 "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
3407 "RDMA: 0x%05x, TDMA: 0x%05x\n"
3408 "Words/BD: %d\n",
3409 priv->version,
Petri Gynther51a966a2015-02-23 11:00:46 -08003410 params->tx_queues, params->tx_bds_per_q,
Petri Gynther3feafa02015-03-05 17:40:14 -08003411 params->rx_queues, params->rx_bds_per_q,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003412 params->bp_in_en_shift, params->bp_in_mask,
3413 params->hfb_filter_cnt, params->qtag_mask,
3414 params->tbuf_offset, params->hfb_offset,
3415 params->hfb_reg_offset,
3416 params->rdma_offset, params->tdma_offset,
3417 params->words_per_bd);
3418}
3419
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003420struct bcmgenet_plat_data {
3421 enum bcmgenet_version version;
3422 u32 dma_max_burst_length;
3423};
3424
3425static const struct bcmgenet_plat_data v1_plat_data = {
3426 .version = GENET_V1,
3427 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3428};
3429
3430static const struct bcmgenet_plat_data v2_plat_data = {
3431 .version = GENET_V2,
3432 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3433};
3434
3435static const struct bcmgenet_plat_data v3_plat_data = {
3436 .version = GENET_V3,
3437 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3438};
3439
3440static const struct bcmgenet_plat_data v4_plat_data = {
3441 .version = GENET_V4,
3442 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3443};
3444
3445static const struct bcmgenet_plat_data v5_plat_data = {
3446 .version = GENET_V5,
3447 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3448};
3449
3450static const struct bcmgenet_plat_data bcm2711_plat_data = {
3451 .version = GENET_V5,
3452 .dma_max_burst_length = 0x08,
3453};
3454
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003455static const struct of_device_id bcmgenet_match[] = {
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003456 { .compatible = "brcm,genet-v1", .data = &v1_plat_data },
3457 { .compatible = "brcm,genet-v2", .data = &v2_plat_data },
3458 { .compatible = "brcm,genet-v3", .data = &v3_plat_data },
3459 { .compatible = "brcm,genet-v4", .data = &v4_plat_data },
3460 { .compatible = "brcm,genet-v5", .data = &v5_plat_data },
3461 { .compatible = "brcm,bcm2711-genet-v5", .data = &bcm2711_plat_data },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003462 { },
3463};
Luis de Bethencourte8048e52015-09-18 17:55:02 +02003464MODULE_DEVICE_TABLE(of, bcmgenet_match);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003465
3466static int bcmgenet_probe(struct platform_device *pdev)
3467{
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003468 struct bcmgenet_platform_data *pd = pdev->dev.platform_data;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003469 struct device_node *dn = pdev->dev.of_node;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003470 const struct of_device_id *of_id = NULL;
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003471 const struct bcmgenet_plat_data *pdata;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003472 struct bcmgenet_priv *priv;
3473 struct net_device *dev;
3474 const void *macaddr;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07003475 unsigned int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003476 int err = -EIO;
Doug Berger6be371b2017-03-09 16:58:48 -08003477 const char *phy_mode_str;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003478
Petri Gynther3feafee2015-03-05 17:40:12 -08003479 /* Up to GENET_MAX_MQ_CNT + 1 TX queues and RX queues */
3480 dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1,
3481 GENET_MAX_MQ_CNT + 1);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003482 if (!dev) {
3483 dev_err(&pdev->dev, "can't allocate net device\n");
3484 return -ENOMEM;
3485 }
3486
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003487 if (dn) {
3488 of_id = of_match_node(bcmgenet_match, dn);
3489 if (!of_id)
3490 return -EINVAL;
3491 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003492
3493 priv = netdev_priv(dev);
3494 priv->irq0 = platform_get_irq(pdev, 0);
Stefan Wahren2b65f932019-11-11 20:49:21 +01003495 if (priv->irq0 < 0) {
3496 err = priv->irq0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003497 goto err;
3498 }
Stefan Wahren2b65f932019-11-11 20:49:21 +01003499 priv->irq1 = platform_get_irq(pdev, 1);
3500 if (priv->irq1 < 0) {
3501 err = priv->irq1;
3502 goto err;
3503 }
3504 priv->wol_irq = platform_get_irq_optional(pdev, 2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003505
Florian Fainellid0337162019-10-14 14:20:00 -07003506 if (dn)
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003507 macaddr = of_get_mac_address(dn);
Florian Fainellid0337162019-10-14 14:20:00 -07003508 else
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003509 macaddr = pd->mac_address;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003510
YueHaibing4ca33482019-08-21 21:41:31 +08003511 priv->base = devm_platform_ioremap_resource(pdev, 0);
Fabio Estevam5343a102014-02-24 00:47:24 -03003512 if (IS_ERR(priv->base)) {
3513 err = PTR_ERR(priv->base);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003514 goto err;
3515 }
3516
Doug Berger07c52d62017-03-09 16:58:47 -08003517 spin_lock_init(&priv->lock);
3518
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003519 SET_NETDEV_DEV(dev, &pdev->dev);
3520 dev_set_drvdata(&pdev->dev, dev);
Florian Fainellid0337162019-10-14 14:20:00 -07003521 if (IS_ERR_OR_NULL(macaddr) || !is_valid_ether_addr(macaddr)) {
3522 dev_warn(&pdev->dev, "using random Ethernet MAC\n");
3523 eth_hw_addr_random(dev);
3524 } else {
3525 ether_addr_copy(dev->dev_addr, macaddr);
3526 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003527 dev->watchdog_timeo = 2 * HZ;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003528 dev->ethtool_ops = &bcmgenet_ethtool_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003529 dev->netdev_ops = &bcmgenet_netdev_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003530
3531 priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
3532
Doug Bergerae895c42019-12-17 16:51:13 -08003533 /* Set default features */
3534 dev->features |= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
3535 NETIF_F_RXCSUM;
3536 dev->hw_features |= dev->features;
3537 dev->vlan_features |= dev->features;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003538
Florian Fainelli85620562014-07-21 15:29:23 -07003539 /* Request the WOL interrupt and advertise suspend if available */
3540 priv->wol_irq_disabled = true;
3541 err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0,
3542 dev->name, priv);
3543 if (!err)
3544 device_set_wakeup_capable(&pdev->dev, 1);
3545
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003546 /* Set the needed headroom to account for any possible
3547 * features enabling/disabling at runtime
3548 */
3549 dev->needed_headroom += 64;
3550
3551 netdev_boot_setup_check(dev);
3552
3553 priv->dev = dev;
3554 priv->pdev = pdev;
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003555 if (of_id) {
3556 pdata = of_id->data;
3557 priv->version = pdata->version;
3558 priv->dma_max_burst_length = pdata->dma_max_burst_length;
3559 } else {
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003560 priv->version = pd->genet_version;
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003561 priv->dma_max_burst_length = DMA_MAX_BURST_LENGTH;
3562 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003563
Florian Fainellie4a60a92014-08-11 14:50:42 -07003564 priv->clk = devm_clk_get(&priv->pdev->dev, "enet");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003565 if (IS_ERR(priv->clk)) {
Florian Fainellie4a60a92014-08-11 14:50:42 -07003566 dev_warn(&priv->pdev->dev, "failed to get enet clock\n");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003567 priv->clk = NULL;
3568 }
Florian Fainellie4a60a92014-08-11 14:50:42 -07003569
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003570 clk_prepare_enable(priv->clk);
Florian Fainellie4a60a92014-08-11 14:50:42 -07003571
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003572 bcmgenet_set_hw_params(priv);
3573
Doug Berger99d55632019-12-17 16:51:08 -08003574 err = -EIO;
3575 if (priv->hw_params->flags & GENET_HAS_40BITS)
3576 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
3577 if (err)
3578 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3579 if (err)
3580 goto err;
3581
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003582 /* Mii wait queue */
3583 init_waitqueue_head(&priv->wq);
3584 /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
3585 priv->rx_buf_len = RX_BUF_LENGTH;
3586 INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
3587
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003588 priv->clk_wol = devm_clk_get(&priv->pdev->dev, "enet-wol");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003589 if (IS_ERR(priv->clk_wol)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003590 dev_warn(&priv->pdev->dev, "failed to get enet-wol clock\n");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003591 priv->clk_wol = NULL;
3592 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003593
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003594 priv->clk_eee = devm_clk_get(&priv->pdev->dev, "enet-eee");
3595 if (IS_ERR(priv->clk_eee)) {
3596 dev_warn(&priv->pdev->dev, "failed to get enet-eee clock\n");
3597 priv->clk_eee = NULL;
3598 }
3599
Doug Berger6be371b2017-03-09 16:58:48 -08003600 /* If this is an internal GPHY, power it on now, before UniMAC is
3601 * brought out of reset as absolutely no UniMAC activity is allowed
3602 */
3603 if (dn && !of_property_read_string(dn, "phy-mode", &phy_mode_str) &&
3604 !strcasecmp(phy_mode_str, "internal"))
3605 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3606
Doug Berger28c2d1a2017-10-25 15:04:13 -07003607 reset_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003608
3609 err = bcmgenet_mii_init(dev);
3610 if (err)
3611 goto err_clk_disable;
3612
3613 /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
3614 * just the ring 16 descriptor based TX
3615 */
3616 netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
3617 netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
3618
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07003619 /* Set default coalescing parameters */
3620 for (i = 0; i < priv->hw_params->rx_queues; i++)
3621 priv->rx_rings[i].rx_max_coalesced_frames = 1;
3622 priv->rx_rings[DESC_INDEX].rx_max_coalesced_frames = 1;
3623
Florian Fainelli219575e2014-06-26 10:26:21 -07003624 /* libphy will determine the link state */
3625 netif_carrier_off(dev);
3626
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003627 /* Turn off the main clock, WOL clock is handled separately */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003628 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003629
Florian Fainelli0f50ce92014-06-26 10:26:20 -07003630 err = register_netdev(dev);
3631 if (err)
3632 goto err;
3633
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003634 return err;
3635
3636err_clk_disable:
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003637 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003638err:
3639 free_netdev(dev);
3640 return err;
3641}
3642
3643static int bcmgenet_remove(struct platform_device *pdev)
3644{
3645 struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
3646
3647 dev_set_drvdata(&pdev->dev, NULL);
3648 unregister_netdev(priv->dev);
3649 bcmgenet_mii_exit(priv->dev);
3650 free_netdev(priv->dev);
3651
3652 return 0;
3653}
3654
Florian Fainellid9f45ab2019-10-15 10:36:24 -07003655static void bcmgenet_shutdown(struct platform_device *pdev)
3656{
3657 bcmgenet_remove(pdev);
3658}
3659
Florian Fainellib6e978e2014-07-21 15:29:22 -07003660#ifdef CONFIG_PM_SLEEP
Florian Fainellib6e978e2014-07-21 15:29:22 -07003661static int bcmgenet_resume(struct device *d)
3662{
3663 struct net_device *dev = dev_get_drvdata(d);
3664 struct bcmgenet_priv *priv = netdev_priv(dev);
3665 unsigned long dma_ctrl;
3666 int ret;
3667 u32 reg;
3668
3669 if (!netif_running(dev))
3670 return 0;
3671
3672 /* Turn on the clock */
3673 ret = clk_prepare_enable(priv->clk);
3674 if (ret)
3675 return ret;
3676
Florian Fainellia6f31f52015-03-23 15:09:57 -07003677 /* If this is an internal GPHY, power it back on now, before UniMAC is
3678 * brought out of reset as absolutely no UniMAC activity is allowed
3679 */
Florian Fainellic624f892015-07-16 15:51:17 -07003680 if (priv->internal_phy)
Florian Fainellia6f31f52015-03-23 15:09:57 -07003681 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3682
Florian Fainellib6e978e2014-07-21 15:29:22 -07003683 bcmgenet_umac_reset(priv);
3684
Doug Berger28c2d1a2017-10-25 15:04:13 -07003685 init_umac(priv);
Florian Fainellib6e978e2014-07-21 15:29:22 -07003686
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003687 /* From WOL-enabled suspend, switch to regular clock */
3688 if (priv->wolopts)
3689 clk_disable_unprepare(priv->clk_wol);
3690
Doug Berger6b6d017f2019-11-05 11:07:25 -08003691 phy_init_hw(dev->phydev);
3692
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003693 /* Speed settings must be restored */
Doug Berger0686bd92019-11-05 11:07:26 -08003694 genphy_config_aneg(dev->phydev);
Florian Fainelli00d51092017-07-31 11:05:32 -07003695 bcmgenet_mii_config(priv->dev, false);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003696
Doug Berger206f54b2019-12-17 16:51:12 -08003697 /* Restore enabled features */
3698 bcmgenet_set_features(dev, dev->features);
3699
Florian Fainellib6e978e2014-07-21 15:29:22 -07003700 bcmgenet_set_hw_addr(priv, dev->dev_addr);
3701
Florian Fainellic624f892015-07-16 15:51:17 -07003702 if (priv->internal_phy) {
Florian Fainellib6e978e2014-07-21 15:29:22 -07003703 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
3704 reg |= EXT_ENERGY_DET_MASK;
3705 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
3706 }
3707
Florian Fainelli98bb7392014-08-11 14:50:45 -07003708 if (priv->wolopts)
3709 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
3710
Florian Fainellib6e978e2014-07-21 15:29:22 -07003711 /* Disable RX/TX DMA and flush TX queues */
3712 dma_ctrl = bcmgenet_dma_disable(priv);
3713
3714 /* Reinitialize TDMA and RDMA and SW housekeeping */
3715 ret = bcmgenet_init_dma(priv);
3716 if (ret) {
3717 netdev_err(dev, "failed to initialize DMA\n");
3718 goto out_clk_disable;
3719 }
3720
3721 /* Always enable ring 16 - descriptor ring */
3722 bcmgenet_enable_dma(priv, dma_ctrl);
3723
Florian Fainelli5371bbf42017-03-15 12:57:21 -07003724 if (!device_may_wakeup(d))
Doug Berger6c97f012017-10-25 15:04:19 -07003725 phy_resume(dev->phydev);
Florian Fainellicc013fb2014-08-11 14:50:43 -07003726
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003727 if (priv->eee.eee_enabled)
3728 bcmgenet_eee_enable_set(dev, true);
3729
Florian Fainellib6e978e2014-07-21 15:29:22 -07003730 bcmgenet_netif_start(dev);
3731
Doug Berger09e805d2018-11-01 15:55:37 -07003732 netif_device_attach(dev);
3733
Florian Fainellib6e978e2014-07-21 15:29:22 -07003734 return 0;
3735
3736out_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08003737 if (priv->internal_phy)
3738 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainellib6e978e2014-07-21 15:29:22 -07003739 clk_disable_unprepare(priv->clk);
3740 return ret;
3741}
Doug Bergera94cbf02018-11-16 18:00:21 -08003742
3743static int bcmgenet_suspend(struct device *d)
3744{
3745 struct net_device *dev = dev_get_drvdata(d);
3746 struct bcmgenet_priv *priv = netdev_priv(dev);
3747 int ret = 0;
3748
3749 if (!netif_running(dev))
3750 return 0;
3751
3752 netif_device_detach(dev);
3753
3754 bcmgenet_netif_stop(dev);
3755
3756 if (!device_may_wakeup(d))
3757 phy_suspend(dev->phydev);
3758
3759 /* Prepare the device for Wake-on-LAN and switch to the slow clock */
3760 if (device_may_wakeup(d) && priv->wolopts) {
3761 ret = bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
3762 clk_prepare_enable(priv->clk_wol);
3763 } else if (priv->internal_phy) {
3764 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
3765 }
3766
3767 /* Turn off the clocks */
3768 clk_disable_unprepare(priv->clk);
3769
Doug Bergerc5a54bb2018-11-16 18:00:22 -08003770 if (ret)
3771 bcmgenet_resume(d);
3772
Doug Bergera94cbf02018-11-16 18:00:21 -08003773 return ret;
3774}
Florian Fainellib6e978e2014-07-21 15:29:22 -07003775#endif /* CONFIG_PM_SLEEP */
3776
3777static SIMPLE_DEV_PM_OPS(bcmgenet_pm_ops, bcmgenet_suspend, bcmgenet_resume);
3778
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003779static struct platform_driver bcmgenet_driver = {
3780 .probe = bcmgenet_probe,
3781 .remove = bcmgenet_remove,
Florian Fainellid9f45ab2019-10-15 10:36:24 -07003782 .shutdown = bcmgenet_shutdown,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003783 .driver = {
3784 .name = "bcmgenet",
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003785 .of_match_table = bcmgenet_match,
Florian Fainellib6e978e2014-07-21 15:29:22 -07003786 .pm = &bcmgenet_pm_ops,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003787 },
3788};
3789module_platform_driver(bcmgenet_driver);
3790
3791MODULE_AUTHOR("Broadcom Corporation");
3792MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
3793MODULE_ALIAS("platform:bcmgenet");
3794MODULE_LICENSE("GPL");