blob: e2bca19bf10b8f4eb20af90707e32bea1182a38b [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));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001502 if (!new_skb) {
Doug Bergere3fa8582019-12-17 16:51:14 -08001503 dev_kfree_skb_any(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 }
Doug Bergere3fa8582019-12-17 16:51:14 -08001507 dev_consume_skb_any(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001508 skb = new_skb;
1509 }
1510
1511 skb_push(skb, sizeof(*status));
1512 status = (struct status_64 *)skb->data;
1513
1514 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Florian Fainelli6f894212018-04-02 15:58:55 -07001515 ip_ver = skb->protocol;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001516 switch (ip_ver) {
Florian Fainelli6f894212018-04-02 15:58:55 -07001517 case htons(ETH_P_IP):
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001518 ip_proto = ip_hdr(skb)->protocol;
1519 break;
Florian Fainelli6f894212018-04-02 15:58:55 -07001520 case htons(ETH_P_IPV6):
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001521 ip_proto = ipv6_hdr(skb)->nexthdr;
1522 break;
1523 default:
Doug Bergerdd8e9112019-12-17 16:51:09 -08001524 /* don't use UDP flag */
1525 ip_proto = 0;
1526 break;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001527 }
1528
1529 offset = skb_checksum_start_offset(skb) - sizeof(*status);
1530 tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
Doug Bergerdd8e9112019-12-17 16:51:09 -08001531 (offset + skb->csum_offset) |
1532 STATUS_TX_CSUM_LV;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001533
Doug Bergerdd8e9112019-12-17 16:51:09 -08001534 /* Set the special UDP flag for UDP */
1535 if (ip_proto == IPPROTO_UDP)
1536 tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001537
1538 status->tx_csum_info = tx_csum_info;
1539 }
1540
Petri Gyntherbc233332014-10-01 11:30:01 -07001541 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001542}
1543
1544static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
1545{
1546 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Berger876dbad2017-07-14 16:12:09 -07001547 struct device *kdev = &priv->pdev->dev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001548 struct bcmgenet_tx_ring *ring = NULL;
Doug Berger876dbad2017-07-14 16:12:09 -07001549 struct enet_cb *tx_cb_ptr;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001550 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001551 int nr_frags, index;
Doug Berger876dbad2017-07-14 16:12:09 -07001552 dma_addr_t mapping;
1553 unsigned int size;
1554 skb_frag_t *frag;
1555 u32 len_stat;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001556 int ret;
1557 int i;
1558
1559 index = skb_get_queue_mapping(skb);
1560 /* Mapping strategy:
1561 * queue_mapping = 0, unclassified, packet xmited through ring16
1562 * queue_mapping = 1, goes to ring 0. (highest priority queue
1563 * queue_mapping = 2, goes to ring 1.
1564 * queue_mapping = 3, goes to ring 2.
1565 * queue_mapping = 4, goes to ring 3.
1566 */
1567 if (index == 0)
1568 index = DESC_INDEX;
1569 else
1570 index -= 1;
1571
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001572 ring = &priv->tx_rings[index];
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001573 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001574
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07001575 nr_frags = skb_shinfo(skb)->nr_frags;
1576
Doug Bergerb0447ec2017-10-25 15:04:17 -07001577 spin_lock(&ring->lock);
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07001578 if (ring->free_bds <= (nr_frags + 1)) {
1579 if (!netif_tx_queue_stopped(txq)) {
1580 netif_tx_stop_queue(txq);
1581 netdev_err(dev,
1582 "%s: tx ring %d full when queue %d awake\n",
1583 __func__, index, ring->queue);
1584 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001585 ret = NETDEV_TX_BUSY;
1586 goto out;
1587 }
1588
Florian Fainelli474ea9c2014-07-22 11:01:52 -07001589 if (skb_padto(skb, ETH_ZLEN)) {
1590 ret = NETDEV_TX_OK;
1591 goto out;
1592 }
1593
Petri Gynther55868122016-03-24 11:27:20 -07001594 /* Retain how many bytes will be sent on the wire, without TSB inserted
1595 * by transmit checksum offload
1596 */
1597 GENET_CB(skb)->bytes_sent = skb->len;
1598
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001599 /* set the SKB transmit checksum */
1600 if (priv->desc_64b_en) {
Petri Gyntherbc233332014-10-01 11:30:01 -07001601 skb = bcmgenet_put_tx_csum(dev, skb);
1602 if (!skb) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001603 ret = NETDEV_TX_OK;
1604 goto out;
1605 }
1606 }
1607
Doug Berger876dbad2017-07-14 16:12:09 -07001608 for (i = 0; i <= nr_frags; i++) {
1609 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001610
Gustavo A. R. Silva4fa112f2017-10-26 07:16:01 -05001611 BUG_ON(!tx_cb_ptr);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001612
Doug Berger876dbad2017-07-14 16:12:09 -07001613 if (!i) {
1614 /* Transmit single SKB or head of fragment list */
Doug Bergerf48bed12017-07-14 16:12:10 -07001615 GENET_CB(skb)->first_cb = tx_cb_ptr;
Doug Berger876dbad2017-07-14 16:12:09 -07001616 size = skb_headlen(skb);
1617 mapping = dma_map_single(kdev, skb->data, size,
1618 DMA_TO_DEVICE);
1619 } else {
1620 /* xmit fragment */
Doug Berger876dbad2017-07-14 16:12:09 -07001621 frag = &skb_shinfo(skb)->frags[i - 1];
1622 size = skb_frag_size(frag);
1623 mapping = skb_frag_dma_map(kdev, frag, 0, size,
1624 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001625 }
Doug Berger876dbad2017-07-14 16:12:09 -07001626
1627 ret = dma_mapping_error(kdev, mapping);
1628 if (ret) {
1629 priv->mib.tx_dma_failed++;
1630 netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
1631 ret = NETDEV_TX_OK;
1632 goto out_unmap_frags;
1633 }
1634 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
1635 dma_unmap_len_set(tx_cb_ptr, dma_len, size);
1636
Doug Bergerf48bed12017-07-14 16:12:10 -07001637 tx_cb_ptr->skb = skb;
1638
Doug Berger876dbad2017-07-14 16:12:09 -07001639 len_stat = (size << DMA_BUFLENGTH_SHIFT) |
1640 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT);
1641
1642 if (!i) {
1643 len_stat |= DMA_TX_APPEND_CRC | DMA_SOP;
1644 if (skb->ip_summed == CHECKSUM_PARTIAL)
1645 len_stat |= DMA_TX_DO_CSUM;
1646 }
1647 if (i == nr_frags)
1648 len_stat |= DMA_EOP;
1649
1650 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, len_stat);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001651 }
1652
Doug Bergerf48bed12017-07-14 16:12:10 -07001653 GENET_CB(skb)->last_cb = tx_cb_ptr;
Florian Fainellid03825f2014-03-20 10:53:21 -07001654 skb_tx_timestamp(skb);
1655
Florian Fainelliae67bf02015-03-13 12:11:06 -07001656 /* Decrement total BD count and advance our write pointer */
1657 ring->free_bds -= nr_frags + 1;
1658 ring->prod_index += nr_frags + 1;
1659 ring->prod_index &= DMA_P_INDEX_MASK;
1660
Petri Gynthere178c8c2016-04-09 00:20:36 -07001661 netdev_tx_sent_queue(txq, GENET_CB(skb)->bytes_sent);
1662
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001663 if (ring->free_bds <= (MAX_SKB_FRAGS + 1))
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001664 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001665
Florian Westphal6b16f9e2019-04-01 16:42:14 +02001666 if (!netdev_xmit_more() || netif_xmit_stopped(txq))
Florian Fainelliddd0ca52015-03-13 12:11:07 -07001667 /* Packets are ready, update producer index */
1668 bcmgenet_tdma_ring_writel(priv, ring->index,
1669 ring->prod_index, TDMA_PROD_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001670out:
Doug Bergerb0447ec2017-10-25 15:04:17 -07001671 spin_unlock(&ring->lock);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001672
1673 return ret;
Doug Berger876dbad2017-07-14 16:12:09 -07001674
1675out_unmap_frags:
1676 /* Back up for failed control block mapping */
1677 bcmgenet_put_txcb(priv, ring);
1678
1679 /* Unmap successfully mapped control blocks */
1680 while (i-- > 0) {
1681 tx_cb_ptr = bcmgenet_put_txcb(priv, ring);
Doug Bergerf48bed12017-07-14 16:12:10 -07001682 bcmgenet_free_tx_cb(kdev, tx_cb_ptr);
Doug Berger876dbad2017-07-14 16:12:09 -07001683 }
1684
1685 dev_kfree_skb(skb);
1686 goto out;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001687}
1688
Petri Gyntherd6707be2015-03-12 15:48:00 -07001689static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
1690 struct enet_cb *cb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001691{
1692 struct device *kdev = &priv->pdev->dev;
1693 struct sk_buff *skb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001694 struct sk_buff *rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001695 dma_addr_t mapping;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001696
Petri Gyntherd6707be2015-03-12 15:48:00 -07001697 /* Allocate a new Rx skb */
Florian Fainellic91b7f62014-07-23 10:42:12 -07001698 skb = netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT);
Petri Gyntherd6707be2015-03-12 15:48:00 -07001699 if (!skb) {
1700 priv->mib.alloc_rx_buff_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001701 netif_err(priv, rx_err, priv->dev,
Petri Gyntherd6707be2015-03-12 15:48:00 -07001702 "%s: Rx skb allocation failed\n", __func__);
1703 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001704 }
1705
Petri Gyntherd6707be2015-03-12 15:48:00 -07001706 /* DMA-map the new Rx skb */
1707 mapping = dma_map_single(kdev, skb->data, priv->rx_buf_len,
1708 DMA_FROM_DEVICE);
1709 if (dma_mapping_error(kdev, mapping)) {
1710 priv->mib.rx_dma_failed++;
1711 dev_kfree_skb_any(skb);
1712 netif_err(priv, rx_err, priv->dev,
1713 "%s: Rx skb DMA mapping failed\n", __func__);
1714 return NULL;
1715 }
1716
1717 /* Grab the current Rx skb from the ring and DMA-unmap it */
Doug Bergerf48bed12017-07-14 16:12:10 -07001718 rx_skb = bcmgenet_free_rx_cb(kdev, cb);
Petri Gyntherd6707be2015-03-12 15:48:00 -07001719
1720 /* Put the new Rx skb on the ring */
1721 cb->skb = skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001722 dma_unmap_addr_set(cb, dma_addr, mapping);
Doug Bergerf48bed12017-07-14 16:12:10 -07001723 dma_unmap_len_set(cb, dma_len, priv->rx_buf_len);
Petri Gynther8ac467e2015-03-09 13:40:00 -07001724 dmadesc_set_addr(priv, cb->bd_addr, mapping);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001725
Petri Gyntherd6707be2015-03-12 15:48:00 -07001726 /* Return the current Rx skb to caller */
1727 return rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001728}
1729
1730/* bcmgenet_desc_rx - descriptor based rx process.
1731 * this could be called from bottom half, or from NAPI polling method.
1732 */
Petri Gynther4055eae2015-03-25 12:35:16 -07001733static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001734 unsigned int budget)
1735{
Petri Gynther4055eae2015-03-25 12:35:16 -07001736 struct bcmgenet_priv *priv = ring->priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001737 struct net_device *dev = priv->dev;
1738 struct enet_cb *cb;
1739 struct sk_buff *skb;
1740 u32 dma_length_status;
1741 unsigned long dma_flag;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001742 int len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001743 unsigned int rxpktprocessed = 0, rxpkttoprocess;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001744 unsigned int bytes_processed = 0;
Doug Bergerd5810ca2017-03-13 17:41:37 -07001745 unsigned int p_index, mask;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001746 unsigned int discards;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001747
Doug Bergerd5810ca2017-03-13 17:41:37 -07001748 /* Clear status before servicing to reduce spurious interrupts */
1749 if (ring->index == DESC_INDEX) {
1750 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_RXDMA_DONE,
1751 INTRL2_CPU_CLEAR);
1752 } else {
1753 mask = 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index);
1754 bcmgenet_intrl2_1_writel(priv,
1755 mask,
1756 INTRL2_CPU_CLEAR);
1757 }
1758
Petri Gynther4055eae2015-03-25 12:35:16 -07001759 p_index = bcmgenet_rdma_ring_readl(priv, ring->index, RDMA_PROD_INDEX);
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001760
1761 discards = (p_index >> DMA_P_INDEX_DISCARD_CNT_SHIFT) &
1762 DMA_P_INDEX_DISCARD_CNT_MASK;
1763 if (discards > ring->old_discards) {
1764 discards = discards - ring->old_discards;
Florian Fainelli37a30b42017-03-16 10:27:08 -07001765 ring->errors += discards;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001766 ring->old_discards += discards;
1767
1768 /* Clear HW register when we reach 75% of maximum 0xFFFF */
1769 if (ring->old_discards >= 0xC000) {
1770 ring->old_discards = 0;
Petri Gynther4055eae2015-03-25 12:35:16 -07001771 bcmgenet_rdma_ring_writel(priv, ring->index, 0,
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001772 RDMA_PROD_INDEX);
1773 }
1774 }
1775
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001776 p_index &= DMA_P_INDEX_MASK;
Doug Bergerc298ede2017-03-13 17:41:33 -07001777 rxpkttoprocess = (p_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001778
1779 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001780 "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001781
1782 while ((rxpktprocessed < rxpkttoprocess) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001783 (rxpktprocessed < budget)) {
Petri Gynther8ac467e2015-03-09 13:40:00 -07001784 cb = &priv->rx_cbs[ring->read_ptr];
Petri Gyntherd6707be2015-03-12 15:48:00 -07001785 skb = bcmgenet_rx_refill(priv, cb);
Florian Fainellib629be52014-09-08 11:37:52 -07001786
Florian Fainellib629be52014-09-08 11:37:52 -07001787 if (unlikely(!skb)) {
Florian Fainelli37a30b42017-03-16 10:27:08 -07001788 ring->dropped++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001789 goto next;
Florian Fainellib629be52014-09-08 11:37:52 -07001790 }
1791
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001792 if (!priv->desc_64b_en) {
Florian Fainellic91b7f62014-07-23 10:42:12 -07001793 dma_length_status =
Petri Gynther8ac467e2015-03-09 13:40:00 -07001794 dmadesc_get_length_status(priv, cb->bd_addr);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001795 } else {
1796 struct status_64 *status;
Doug Berger81015532019-12-17 16:51:10 -08001797 __be16 rx_csum;
Florian Fainelli164d4f22014-07-23 10:42:13 -07001798
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001799 status = (struct status_64 *)skb->data;
1800 dma_length_status = status->length_status;
Doug Berger81015532019-12-17 16:51:10 -08001801 rx_csum = (__force __be16)(status->rx_csum & 0xffff);
1802 if (priv->desc_rxchk_en) {
1803 skb->csum = (__force __wsum)ntohs(rx_csum);
1804 skb->ip_summed = CHECKSUM_COMPLETE;
1805 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001806 }
1807
1808 /* DMA flags and length are still valid no matter how
1809 * we got the Receive Status Vector (64B RSB or register)
1810 */
1811 dma_flag = dma_length_status & 0xffff;
1812 len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
1813
1814 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001815 "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
Petri Gynther8ac467e2015-03-09 13:40:00 -07001816 __func__, p_index, ring->c_index,
1817 ring->read_ptr, dma_length_status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001818
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001819 if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
1820 netif_err(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001821 "dropping fragmented packet!\n");
Florian Fainelli37a30b42017-03-16 10:27:08 -07001822 ring->errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001823 dev_kfree_skb_any(skb);
1824 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001825 }
Petri Gyntherd6707be2015-03-12 15:48:00 -07001826
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001827 /* report errors */
1828 if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
1829 DMA_RX_OV |
1830 DMA_RX_NO |
1831 DMA_RX_LG |
1832 DMA_RX_RXER))) {
1833 netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001834 (unsigned int)dma_flag);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001835 if (dma_flag & DMA_RX_CRC_ERROR)
1836 dev->stats.rx_crc_errors++;
1837 if (dma_flag & DMA_RX_OV)
1838 dev->stats.rx_over_errors++;
1839 if (dma_flag & DMA_RX_NO)
1840 dev->stats.rx_frame_errors++;
1841 if (dma_flag & DMA_RX_LG)
1842 dev->stats.rx_length_errors++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001843 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001844 dev_kfree_skb_any(skb);
1845 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001846 } /* error packet */
1847
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001848 skb_put(skb, len);
1849 if (priv->desc_64b_en) {
1850 skb_pull(skb, 64);
1851 len -= 64;
1852 }
1853
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001854 /* remove hardware 2bytes added for IP alignment */
1855 skb_pull(skb, 2);
1856 len -= 2;
1857
1858 if (priv->crc_fwd_en) {
1859 skb_trim(skb, len - ETH_FCS_LEN);
1860 len -= ETH_FCS_LEN;
1861 }
1862
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001863 bytes_processed += len;
1864
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001865 /*Finish setting up the received SKB and send it to the kernel*/
1866 skb->protocol = eth_type_trans(skb, priv->dev);
Florian Fainelli37a30b42017-03-16 10:27:08 -07001867 ring->packets++;
1868 ring->bytes += len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001869 if (dma_flag & DMA_RX_MULT)
1870 dev->stats.multicast++;
1871
1872 /* Notify kernel */
Petri Gynther4055eae2015-03-25 12:35:16 -07001873 napi_gro_receive(&ring->napi, skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001874 netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
1875
Petri Gyntherd6707be2015-03-12 15:48:00 -07001876next:
Florian Fainellicf377d82014-10-10 10:51:52 -07001877 rxpktprocessed++;
Petri Gynther8ac467e2015-03-09 13:40:00 -07001878 if (likely(ring->read_ptr < ring->end_ptr))
1879 ring->read_ptr++;
1880 else
1881 ring->read_ptr = ring->cb_ptr;
1882
1883 ring->c_index = (ring->c_index + 1) & DMA_C_INDEX_MASK;
Petri Gynther4055eae2015-03-25 12:35:16 -07001884 bcmgenet_rdma_ring_writel(priv, ring->index, ring->c_index, RDMA_CONS_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001885 }
1886
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001887 ring->dim.bytes = bytes_processed;
1888 ring->dim.packets = rxpktprocessed;
1889
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001890 return rxpktprocessed;
1891}
1892
Petri Gynther3ab11332015-03-25 12:35:15 -07001893/* Rx NAPI polling method */
1894static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
1895{
Petri Gynther4055eae2015-03-25 12:35:16 -07001896 struct bcmgenet_rx_ring *ring = container_of(napi,
1897 struct bcmgenet_rx_ring, napi);
Yamin Friedmanf06d0ca2019-07-23 10:22:47 +03001898 struct dim_sample dim_sample = {};
Petri Gynther3ab11332015-03-25 12:35:15 -07001899 unsigned int work_done;
1900
Petri Gynther4055eae2015-03-25 12:35:16 -07001901 work_done = bcmgenet_desc_rx(ring, budget);
Petri Gynther3ab11332015-03-25 12:35:15 -07001902
1903 if (work_done < budget) {
Eric Dumazeteb96ce02016-04-08 22:06:40 -07001904 napi_complete_done(napi, work_done);
Petri Gynther4055eae2015-03-25 12:35:16 -07001905 ring->int_enable(ring);
Petri Gynther3ab11332015-03-25 12:35:15 -07001906 }
1907
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001908 if (ring->dim.use_dim) {
Tal Gilboa8960b382019-01-31 16:44:48 +02001909 dim_update_sample(ring->dim.event_ctr, ring->dim.packets,
1910 ring->dim.bytes, &dim_sample);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001911 net_dim(&ring->dim.dim, dim_sample);
1912 }
1913
Petri Gynther3ab11332015-03-25 12:35:15 -07001914 return work_done;
1915}
1916
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001917static void bcmgenet_dim_work(struct work_struct *work)
1918{
Tal Gilboa8960b382019-01-31 16:44:48 +02001919 struct dim *dim = container_of(work, struct dim, work);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001920 struct bcmgenet_net_dim *ndim =
1921 container_of(dim, struct bcmgenet_net_dim, dim);
1922 struct bcmgenet_rx_ring *ring =
1923 container_of(ndim, struct bcmgenet_rx_ring, dim);
Tal Gilboa8960b382019-01-31 16:44:48 +02001924 struct dim_cq_moder cur_profile =
Tal Gilboa026a8072018-04-24 13:36:01 +03001925 net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001926
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07001927 bcmgenet_set_rx_coalesce(ring, cur_profile.usec, cur_profile.pkts);
Tal Gilboac002bd52018-11-05 12:07:52 +02001928 dim->state = DIM_START_MEASURE;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001929}
1930
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001931/* Assign skb to RX DMA descriptor. */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001932static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv,
1933 struct bcmgenet_rx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001934{
1935 struct enet_cb *cb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001936 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001937 int i;
1938
Petri Gynther8ac467e2015-03-09 13:40:00 -07001939 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001940
1941 /* loop here for each buffer needing assign */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001942 for (i = 0; i < ring->size; i++) {
1943 cb = ring->cbs + i;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001944 skb = bcmgenet_rx_refill(priv, cb);
1945 if (skb)
Florian Fainellid4fec852017-08-24 15:56:29 -07001946 dev_consume_skb_any(skb);
Petri Gyntherd6707be2015-03-12 15:48:00 -07001947 if (!cb->skb)
1948 return -ENOMEM;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001949 }
1950
Petri Gyntherd6707be2015-03-12 15:48:00 -07001951 return 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001952}
1953
1954static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
1955{
Doug Bergerf48bed12017-07-14 16:12:10 -07001956 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001957 struct enet_cb *cb;
1958 int i;
1959
1960 for (i = 0; i < priv->num_rx_bds; i++) {
1961 cb = &priv->rx_cbs[i];
1962
Doug Bergerf48bed12017-07-14 16:12:10 -07001963 skb = bcmgenet_free_rx_cb(&priv->pdev->dev, cb);
1964 if (skb)
Florian Fainellid4fec852017-08-24 15:56:29 -07001965 dev_consume_skb_any(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001966 }
1967}
1968
Florian Fainellic91b7f62014-07-23 10:42:12 -07001969static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable)
Florian Fainellie29585b2014-07-21 15:29:20 -07001970{
1971 u32 reg;
1972
1973 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1974 if (enable)
1975 reg |= mask;
1976 else
1977 reg &= ~mask;
1978 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
1979
1980 /* UniMAC stops on a packet boundary, wait for a full-size packet
1981 * to be processed
1982 */
1983 if (enable == 0)
1984 usleep_range(1000, 2000);
1985}
1986
Doug Berger28c2d1a2017-10-25 15:04:13 -07001987static void reset_umac(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001988{
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001989 /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
1990 bcmgenet_rbuf_ctrl_set(priv, 0);
1991 udelay(10);
1992
1993 /* disable MAC while updating its registers */
1994 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1995
Doug Berger28c2d1a2017-10-25 15:04:13 -07001996 /* issue soft reset with (rg)mii loopback to ensure a stable rxclk */
1997 bcmgenet_umac_writel(priv, CMD_SW_RESET | CMD_LCL_LOOP_EN, UMAC_CMD);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001998}
1999
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002000static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
2001{
2002 /* Mask all interrupts.*/
2003 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
2004 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002005 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
2006 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002007}
2008
Florian Fainelli37850e32015-10-17 14:22:46 -07002009static void bcmgenet_link_intr_enable(struct bcmgenet_priv *priv)
2010{
2011 u32 int0_enable = 0;
2012
2013 /* Monitor cable plug/unplugged event for internal PHY, external PHY
2014 * and MoCA PHY
2015 */
2016 if (priv->internal_phy) {
2017 int0_enable |= UMAC_IRQ_LINK_EVENT;
Doug Berger25382b92019-10-16 16:06:32 -07002018 if (GENET_IS_V1(priv) || GENET_IS_V2(priv) || GENET_IS_V3(priv))
2019 int0_enable |= UMAC_IRQ_PHY_DET_R;
Florian Fainelli37850e32015-10-17 14:22:46 -07002020 } else if (priv->ext_phy) {
2021 int0_enable |= UMAC_IRQ_LINK_EVENT;
2022 } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
2023 if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
2024 int0_enable |= UMAC_IRQ_LINK_EVENT;
2025 }
2026 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
2027}
2028
Doug Berger28c2d1a2017-10-25 15:04:13 -07002029static void init_umac(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002030{
2031 struct device *kdev = &priv->pdev->dev;
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002032 u32 reg;
2033 u32 int0_enable = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002034
2035 dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
2036
Doug Berger28c2d1a2017-10-25 15:04:13 -07002037 reset_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002038
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002039 /* clear tx/rx counter */
2040 bcmgenet_umac_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002041 MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
2042 UMAC_MIB_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002043 bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
2044
2045 bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
2046
2047 /* init rx registers, enable ip header optimization */
2048 reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
2049 reg |= RBUF_ALIGN_2B;
2050 bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
2051
2052 if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
2053 bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
2054
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002055 bcmgenet_intr_disable(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002056
Florian Fainelli37850e32015-10-17 14:22:46 -07002057 /* Configure backpressure vectors for MoCA */
2058 if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002059 reg = bcmgenet_bp_mc_get(priv);
2060 reg |= BIT(priv->hw_params->bp_in_en_shift);
2061
2062 /* bp_mask: back pressure mask */
2063 if (netif_is_multiqueue(priv->dev))
2064 reg |= priv->hw_params->bp_in_mask;
2065 else
2066 reg &= ~priv->hw_params->bp_in_mask;
2067 bcmgenet_bp_mc_set(priv, reg);
2068 }
2069
2070 /* Enable MDIO interrupts on GENET v3+ */
2071 if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002072 int0_enable |= (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002073
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002074 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002075
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002076 dev_dbg(kdev, "done init umac\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002077}
2078
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002079static void bcmgenet_init_dim(struct bcmgenet_rx_ring *ring,
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002080 void (*cb)(struct work_struct *work))
2081{
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002082 struct bcmgenet_net_dim *dim = &ring->dim;
2083
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002084 INIT_WORK(&dim->dim.work, cb);
Tal Gilboac002bd52018-11-05 12:07:52 +02002085 dim->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002086 dim->event_ctr = 0;
2087 dim->packets = 0;
2088 dim->bytes = 0;
2089}
2090
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002091static void bcmgenet_init_rx_coalesce(struct bcmgenet_rx_ring *ring)
2092{
2093 struct bcmgenet_net_dim *dim = &ring->dim;
Tal Gilboa8960b382019-01-31 16:44:48 +02002094 struct dim_cq_moder moder;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002095 u32 usecs, pkts;
2096
2097 usecs = ring->rx_coalesce_usecs;
2098 pkts = ring->rx_max_coalesced_frames;
2099
2100 /* If DIM was enabled, re-apply default parameters */
2101 if (dim->use_dim) {
Tal Gilboa026a8072018-04-24 13:36:01 +03002102 moder = net_dim_get_def_rx_moderation(dim->dim.mode);
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002103 usecs = moder.usec;
2104 pkts = moder.pkts;
2105 }
2106
2107 bcmgenet_set_rx_coalesce(ring, usecs, pkts);
2108}
2109
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002110/* Initialize a Tx ring along with corresponding hardware registers */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002111static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
2112 unsigned int index, unsigned int size,
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002113 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002114{
2115 struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
2116 u32 words_per_bd = WORDS_PER_BD(priv);
2117 u32 flow_period_val = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002118
2119 spin_lock_init(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002120 ring->priv = priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002121 ring->index = index;
2122 if (index == DESC_INDEX) {
2123 ring->queue = 0;
2124 ring->int_enable = bcmgenet_tx_ring16_int_enable;
2125 ring->int_disable = bcmgenet_tx_ring16_int_disable;
2126 } else {
2127 ring->queue = index + 1;
2128 ring->int_enable = bcmgenet_tx_ring_int_enable;
2129 ring->int_disable = bcmgenet_tx_ring_int_disable;
2130 }
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002131 ring->cbs = priv->tx_cbs + start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002132 ring->size = size;
Petri Gynther66d06752015-03-04 14:30:01 -08002133 ring->clean_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002134 ring->c_index = 0;
2135 ring->free_bds = size;
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002136 ring->write_ptr = start_ptr;
2137 ring->cb_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002138 ring->end_ptr = end_ptr - 1;
2139 ring->prod_index = 0;
2140
2141 /* Set flow period for ring != 16 */
2142 if (index != DESC_INDEX)
2143 flow_period_val = ENET_MAX_MTU_SIZE << 16;
2144
2145 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
2146 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
2147 bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
2148 /* Disable rate control for now */
2149 bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002150 TDMA_FLOW_PERIOD);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002151 bcmgenet_tdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002152 ((size << DMA_RING_SIZE_SHIFT) |
2153 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002154
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002155 /* Set start and end address, read and write pointers */
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002156 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002157 DMA_START_ADDR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002158 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002159 TDMA_READ_PTR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002160 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002161 TDMA_WRITE_PTR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002162 bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002163 DMA_END_ADDR);
Doug Berger75879352017-10-25 15:04:14 -07002164
2165 /* Initialize Tx NAPI */
2166 netif_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll,
2167 NAPI_POLL_WEIGHT);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002168}
2169
2170/* Initialize a RDMA ring */
2171static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
Petri Gynther8ac467e2015-03-09 13:40:00 -07002172 unsigned int index, unsigned int size,
2173 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002174{
Petri Gynther8ac467e2015-03-09 13:40:00 -07002175 struct bcmgenet_rx_ring *ring = &priv->rx_rings[index];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002176 u32 words_per_bd = WORDS_PER_BD(priv);
2177 int ret;
2178
Petri Gynther4055eae2015-03-25 12:35:16 -07002179 ring->priv = priv;
Petri Gynther8ac467e2015-03-09 13:40:00 -07002180 ring->index = index;
Petri Gynther4055eae2015-03-25 12:35:16 -07002181 if (index == DESC_INDEX) {
2182 ring->int_enable = bcmgenet_rx_ring16_int_enable;
2183 ring->int_disable = bcmgenet_rx_ring16_int_disable;
2184 } else {
2185 ring->int_enable = bcmgenet_rx_ring_int_enable;
2186 ring->int_disable = bcmgenet_rx_ring_int_disable;
2187 }
Petri Gynther8ac467e2015-03-09 13:40:00 -07002188 ring->cbs = priv->rx_cbs + start_ptr;
2189 ring->size = size;
2190 ring->c_index = 0;
2191 ring->read_ptr = start_ptr;
2192 ring->cb_ptr = start_ptr;
2193 ring->end_ptr = end_ptr - 1;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002194
Petri Gynther8ac467e2015-03-09 13:40:00 -07002195 ret = bcmgenet_alloc_rx_buffers(priv, ring);
2196 if (ret)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002197 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002198
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07002199 bcmgenet_init_dim(ring, bcmgenet_dim_work);
2200 bcmgenet_init_rx_coalesce(ring);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002201
Doug Berger75879352017-10-25 15:04:14 -07002202 /* Initialize Rx NAPI */
2203 netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll,
2204 NAPI_POLL_WEIGHT);
2205
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002206 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
2207 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
2208 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002209 ((size << DMA_RING_SIZE_SHIFT) |
2210 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002211 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002212 (DMA_FC_THRESH_LO <<
2213 DMA_XOFF_THRESHOLD_SHIFT) |
2214 DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
Petri Gynther6f5a2722015-03-06 13:45:00 -08002215
2216 /* Set start and end address, read and write pointers */
Petri Gynther8ac467e2015-03-09 13:40:00 -07002217 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2218 DMA_START_ADDR);
2219 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2220 RDMA_READ_PTR);
2221 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2222 RDMA_WRITE_PTR);
2223 bcmgenet_rdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Petri Gynther6f5a2722015-03-06 13:45:00 -08002224 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002225
2226 return ret;
2227}
2228
Petri Gynthere2aadb42015-03-25 12:35:14 -07002229static void bcmgenet_enable_tx_napi(struct bcmgenet_priv *priv)
2230{
2231 unsigned int i;
2232 struct bcmgenet_tx_ring *ring;
2233
2234 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2235 ring = &priv->tx_rings[i];
2236 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002237 ring->int_enable(ring);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002238 }
2239
2240 ring = &priv->tx_rings[DESC_INDEX];
2241 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002242 ring->int_enable(ring);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002243}
2244
2245static void bcmgenet_disable_tx_napi(struct bcmgenet_priv *priv)
2246{
2247 unsigned int i;
2248 struct bcmgenet_tx_ring *ring;
2249
2250 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2251 ring = &priv->tx_rings[i];
2252 napi_disable(&ring->napi);
2253 }
2254
2255 ring = &priv->tx_rings[DESC_INDEX];
2256 napi_disable(&ring->napi);
2257}
2258
2259static void bcmgenet_fini_tx_napi(struct bcmgenet_priv *priv)
2260{
2261 unsigned int i;
2262 struct bcmgenet_tx_ring *ring;
2263
2264 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2265 ring = &priv->tx_rings[i];
2266 netif_napi_del(&ring->napi);
2267 }
2268
2269 ring = &priv->tx_rings[DESC_INDEX];
2270 netif_napi_del(&ring->napi);
2271}
2272
Petri Gynther16c6d662015-02-23 11:00:45 -08002273/* Initialize Tx queues
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002274 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002275 * Queues 0-3 are priority-based, each one has 32 descriptors,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002276 * with queue 0 being the highest priority queue.
2277 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002278 * Queue 16 is the default Tx queue with
Petri Gynther51a966a2015-02-23 11:00:46 -08002279 * GENET_Q16_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors.
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002280 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002281 * The transmit control block pool is then partitioned as follows:
2282 * - Tx queue 0 uses tx_cbs[0..31]
2283 * - Tx queue 1 uses tx_cbs[32..63]
2284 * - Tx queue 2 uses tx_cbs[64..95]
2285 * - Tx queue 3 uses tx_cbs[96..127]
2286 * - Tx queue 16 uses tx_cbs[128..255]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002287 */
Petri Gynther16c6d662015-02-23 11:00:45 -08002288static void bcmgenet_init_tx_queues(struct net_device *dev)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002289{
2290 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther16c6d662015-02-23 11:00:45 -08002291 u32 i, dma_enable;
2292 u32 dma_ctrl, ring_cfg;
Petri Gynther37742162014-10-07 09:30:01 -07002293 u32 dma_priority[3] = {0, 0, 0};
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002294
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002295 dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
2296 dma_enable = dma_ctrl & DMA_EN;
2297 dma_ctrl &= ~DMA_EN;
2298 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
2299
Petri Gynther16c6d662015-02-23 11:00:45 -08002300 dma_ctrl = 0;
2301 ring_cfg = 0;
2302
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002303 /* Enable strict priority arbiter mode */
2304 bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
2305
Petri Gynther16c6d662015-02-23 11:00:45 -08002306 /* Initialize Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002307 for (i = 0; i < priv->hw_params->tx_queues; i++) {
Petri Gynther51a966a2015-02-23 11:00:46 -08002308 bcmgenet_init_tx_ring(priv, i, priv->hw_params->tx_bds_per_q,
2309 i * priv->hw_params->tx_bds_per_q,
2310 (i + 1) * priv->hw_params->tx_bds_per_q);
Petri Gynther16c6d662015-02-23 11:00:45 -08002311 ring_cfg |= (1 << i);
2312 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002313 dma_priority[DMA_PRIO_REG_INDEX(i)] |=
2314 ((GENET_Q0_PRIORITY + i) << DMA_PRIO_REG_SHIFT(i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002315 }
2316
Petri Gynther16c6d662015-02-23 11:00:45 -08002317 /* Initialize Tx default queue 16 */
Petri Gynther51a966a2015-02-23 11:00:46 -08002318 bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_Q16_TX_BD_CNT,
Petri Gynther16c6d662015-02-23 11:00:45 -08002319 priv->hw_params->tx_queues *
Petri Gynther51a966a2015-02-23 11:00:46 -08002320 priv->hw_params->tx_bds_per_q,
Petri Gynther16c6d662015-02-23 11:00:45 -08002321 TOTAL_DESC);
2322 ring_cfg |= (1 << DESC_INDEX);
2323 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002324 dma_priority[DMA_PRIO_REG_INDEX(DESC_INDEX)] |=
2325 ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) <<
2326 DMA_PRIO_REG_SHIFT(DESC_INDEX));
Petri Gynther16c6d662015-02-23 11:00:45 -08002327
2328 /* Set Tx queue priorities */
Petri Gynther37742162014-10-07 09:30:01 -07002329 bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
2330 bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
2331 bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);
2332
Petri Gynther16c6d662015-02-23 11:00:45 -08002333 /* Enable Tx queues */
2334 bcmgenet_tdma_writel(priv, ring_cfg, DMA_RING_CFG);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002335
Petri Gynther16c6d662015-02-23 11:00:45 -08002336 /* Enable Tx DMA */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002337 if (dma_enable)
Petri Gynther16c6d662015-02-23 11:00:45 -08002338 dma_ctrl |= DMA_EN;
2339 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002340}
2341
Petri Gynther3ab11332015-03-25 12:35:15 -07002342static void bcmgenet_enable_rx_napi(struct bcmgenet_priv *priv)
2343{
Petri Gynther4055eae2015-03-25 12:35:16 -07002344 unsigned int i;
2345 struct bcmgenet_rx_ring *ring;
2346
2347 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2348 ring = &priv->rx_rings[i];
2349 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002350 ring->int_enable(ring);
Petri Gynther4055eae2015-03-25 12:35:16 -07002351 }
2352
2353 ring = &priv->rx_rings[DESC_INDEX];
2354 napi_enable(&ring->napi);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002355 ring->int_enable(ring);
Petri Gynther3ab11332015-03-25 12:35:15 -07002356}
2357
2358static void bcmgenet_disable_rx_napi(struct bcmgenet_priv *priv)
2359{
Petri Gynther4055eae2015-03-25 12:35:16 -07002360 unsigned int i;
2361 struct bcmgenet_rx_ring *ring;
2362
2363 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2364 ring = &priv->rx_rings[i];
2365 napi_disable(&ring->napi);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002366 cancel_work_sync(&ring->dim.dim.work);
Petri Gynther4055eae2015-03-25 12:35:16 -07002367 }
2368
2369 ring = &priv->rx_rings[DESC_INDEX];
2370 napi_disable(&ring->napi);
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002371 cancel_work_sync(&ring->dim.dim.work);
Petri Gynther3ab11332015-03-25 12:35:15 -07002372}
2373
2374static void bcmgenet_fini_rx_napi(struct bcmgenet_priv *priv)
2375{
Petri Gynther4055eae2015-03-25 12:35:16 -07002376 unsigned int i;
2377 struct bcmgenet_rx_ring *ring;
2378
2379 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2380 ring = &priv->rx_rings[i];
2381 netif_napi_del(&ring->napi);
2382 }
2383
2384 ring = &priv->rx_rings[DESC_INDEX];
2385 netif_napi_del(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002386}
2387
Petri Gynther8ac467e2015-03-09 13:40:00 -07002388/* Initialize Rx queues
2389 *
2390 * Queues 0-15 are priority queues. Hardware Filtering Block (HFB) can be
2391 * used to direct traffic to these queues.
2392 *
2393 * Queue 16 is the default Rx queue with GENET_Q16_RX_BD_CNT descriptors.
2394 */
2395static int bcmgenet_init_rx_queues(struct net_device *dev)
2396{
2397 struct bcmgenet_priv *priv = netdev_priv(dev);
2398 u32 i;
2399 u32 dma_enable;
2400 u32 dma_ctrl;
2401 u32 ring_cfg;
2402 int ret;
2403
2404 dma_ctrl = bcmgenet_rdma_readl(priv, DMA_CTRL);
2405 dma_enable = dma_ctrl & DMA_EN;
2406 dma_ctrl &= ~DMA_EN;
2407 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2408
2409 dma_ctrl = 0;
2410 ring_cfg = 0;
2411
2412 /* Initialize Rx priority queues */
2413 for (i = 0; i < priv->hw_params->rx_queues; i++) {
2414 ret = bcmgenet_init_rx_ring(priv, i,
2415 priv->hw_params->rx_bds_per_q,
2416 i * priv->hw_params->rx_bds_per_q,
2417 (i + 1) *
2418 priv->hw_params->rx_bds_per_q);
2419 if (ret)
2420 return ret;
2421
2422 ring_cfg |= (1 << i);
2423 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2424 }
2425
2426 /* Initialize Rx default queue 16 */
2427 ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, GENET_Q16_RX_BD_CNT,
2428 priv->hw_params->rx_queues *
2429 priv->hw_params->rx_bds_per_q,
2430 TOTAL_DESC);
2431 if (ret)
2432 return ret;
2433
2434 ring_cfg |= (1 << DESC_INDEX);
2435 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
2436
2437 /* Enable rings */
2438 bcmgenet_rdma_writel(priv, ring_cfg, DMA_RING_CFG);
2439
2440 /* Configure ring as descriptor ring and re-enable DMA if enabled */
2441 if (dma_enable)
2442 dma_ctrl |= DMA_EN;
2443 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2444
2445 return 0;
2446}
2447
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002448static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
2449{
2450 int ret = 0;
2451 int timeout = 0;
2452 u32 reg;
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002453 u32 dma_ctrl;
2454 int i;
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002455
2456 /* Disable TDMA to stop add more frames in TX DMA */
2457 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2458 reg &= ~DMA_EN;
2459 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2460
2461 /* Check TDMA status register to confirm TDMA is disabled */
2462 while (timeout++ < DMA_TIMEOUT_VAL) {
2463 reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
2464 if (reg & DMA_DISABLED)
2465 break;
2466
2467 udelay(1);
2468 }
2469
2470 if (timeout == DMA_TIMEOUT_VAL) {
2471 netdev_warn(priv->dev, "Timed out while disabling TX DMA\n");
2472 ret = -ETIMEDOUT;
2473 }
2474
2475 /* Wait 10ms for packet drain in both tx and rx dma */
2476 usleep_range(10000, 20000);
2477
2478 /* Disable RDMA */
2479 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2480 reg &= ~DMA_EN;
2481 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2482
2483 timeout = 0;
2484 /* Check RDMA status register to confirm RDMA is disabled */
2485 while (timeout++ < DMA_TIMEOUT_VAL) {
2486 reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
2487 if (reg & DMA_DISABLED)
2488 break;
2489
2490 udelay(1);
2491 }
2492
2493 if (timeout == DMA_TIMEOUT_VAL) {
2494 netdev_warn(priv->dev, "Timed out while disabling RX DMA\n");
2495 ret = -ETIMEDOUT;
2496 }
2497
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002498 dma_ctrl = 0;
2499 for (i = 0; i < priv->hw_params->rx_queues; i++)
2500 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2501 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2502 reg &= ~dma_ctrl;
2503 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2504
2505 dma_ctrl = 0;
2506 for (i = 0; i < priv->hw_params->tx_queues; i++)
2507 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2508 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2509 reg &= ~dma_ctrl;
2510 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2511
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002512 return ret;
2513}
2514
Petri Gynther9abab962015-03-30 00:29:01 -07002515static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002516{
Petri Gynthere178c8c2016-04-09 00:20:36 -07002517 struct netdev_queue *txq;
Doug Bergerf48bed12017-07-14 16:12:10 -07002518 int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002519
Petri Gynther9abab962015-03-30 00:29:01 -07002520 bcmgenet_fini_rx_napi(priv);
2521 bcmgenet_fini_tx_napi(priv);
2522
Markus Elfring399e06a2019-08-22 20:02:56 +02002523 for (i = 0; i < priv->num_tx_bds; i++)
2524 dev_kfree_skb(bcmgenet_free_tx_cb(&priv->pdev->dev,
2525 priv->tx_cbs + i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002526
Petri Gynthere178c8c2016-04-09 00:20:36 -07002527 for (i = 0; i < priv->hw_params->tx_queues; i++) {
2528 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[i].queue);
2529 netdev_tx_reset_queue(txq);
2530 }
2531
2532 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[DESC_INDEX].queue);
2533 netdev_tx_reset_queue(txq);
2534
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002535 bcmgenet_free_rx_buffers(priv);
2536 kfree(priv->rx_cbs);
2537 kfree(priv->tx_cbs);
2538}
2539
2540/* init_edma: Initialize DMA control register */
2541static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
2542{
2543 int ret;
Petri Gynther014012a2015-02-23 11:00:45 -08002544 unsigned int i;
2545 struct enet_cb *cb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002546
Petri Gynther6f5a2722015-03-06 13:45:00 -08002547 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002548
Petri Gynther6f5a2722015-03-06 13:45:00 -08002549 /* Initialize common Rx ring structures */
2550 priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
2551 priv->num_rx_bds = TOTAL_DESC;
2552 priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct enet_cb),
2553 GFP_KERNEL);
2554 if (!priv->rx_cbs)
2555 return -ENOMEM;
2556
2557 for (i = 0; i < priv->num_rx_bds; i++) {
2558 cb = priv->rx_cbs + i;
2559 cb->bd_addr = priv->rx_bds + i * DMA_DESC_SIZE;
2560 }
2561
Brian Norris7fc527f2014-07-29 14:34:14 -07002562 /* Initialize common TX ring structures */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002563 priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
2564 priv->num_tx_bds = TOTAL_DESC;
Florian Fainellic489be02014-07-23 10:42:15 -07002565 priv->tx_cbs = kcalloc(priv->num_tx_bds, sizeof(struct enet_cb),
Florian Fainellic91b7f62014-07-23 10:42:12 -07002566 GFP_KERNEL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002567 if (!priv->tx_cbs) {
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002568 kfree(priv->rx_cbs);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002569 return -ENOMEM;
2570 }
2571
Petri Gynther014012a2015-02-23 11:00:45 -08002572 for (i = 0; i < priv->num_tx_bds; i++) {
2573 cb = priv->tx_cbs + i;
2574 cb->bd_addr = priv->tx_bds + i * DMA_DESC_SIZE;
2575 }
2576
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002577 /* Init rDma */
Stefan Wahrena50e3a92019-11-11 20:49:23 +01002578 bcmgenet_rdma_writel(priv, priv->dma_max_burst_length,
2579 DMA_SCB_BURST_SIZE);
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002580
2581 /* Initialize Rx queues */
2582 ret = bcmgenet_init_rx_queues(priv->dev);
2583 if (ret) {
2584 netdev_err(priv->dev, "failed to initialize Rx queues\n");
2585 bcmgenet_free_rx_buffers(priv);
2586 kfree(priv->rx_cbs);
2587 kfree(priv->tx_cbs);
2588 return ret;
2589 }
2590
2591 /* Init tDma */
Stefan Wahrena50e3a92019-11-11 20:49:23 +01002592 bcmgenet_tdma_writel(priv, priv->dma_max_burst_length,
2593 DMA_SCB_BURST_SIZE);
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002594
Petri Gynther16c6d662015-02-23 11:00:45 -08002595 /* Initialize Tx queues */
2596 bcmgenet_init_tx_queues(priv->dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002597
2598 return 0;
2599}
2600
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002601/* Interrupt bottom half */
2602static void bcmgenet_irq_task(struct work_struct *work)
2603{
Doug Berger07c52d62017-03-09 16:58:47 -08002604 unsigned int status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002605 struct bcmgenet_priv *priv = container_of(
2606 work, struct bcmgenet_priv, bcmgenet_irq_work);
2607
2608 netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
2609
Doug Bergerb0447ec2017-10-25 15:04:17 -07002610 spin_lock_irq(&priv->lock);
Doug Berger07c52d62017-03-09 16:58:47 -08002611 status = priv->irq0_stat;
2612 priv->irq0_stat = 0;
Doug Bergerb0447ec2017-10-25 15:04:17 -07002613 spin_unlock_irq(&priv->lock);
Doug Berger07c52d62017-03-09 16:58:47 -08002614
Doug Berger25382b92019-10-16 16:06:32 -07002615 if (status & UMAC_IRQ_PHY_DET_R &&
Doug Berger0686bd92019-11-05 11:07:26 -08002616 priv->dev->phydev->autoneg != AUTONEG_ENABLE) {
Doug Berger25382b92019-10-16 16:06:32 -07002617 phy_init_hw(priv->dev->phydev);
Doug Berger0686bd92019-11-05 11:07:26 -08002618 genphy_config_aneg(priv->dev->phydev);
2619 }
Doug Berger25382b92019-10-16 16:06:32 -07002620
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002621 /* Link UP/DOWN event */
Doug Berger7de48402019-10-16 16:06:29 -07002622 if (status & UMAC_IRQ_LINK_EVENT)
Heiner Kallweit28b2e0d2018-01-10 21:21:31 +01002623 phy_mac_interrupt(priv->dev->phydev);
Doug Berger25382b92019-10-16 16:06:32 -07002624
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002625}
2626
Petri Gynther4055eae2015-03-25 12:35:16 -07002627/* bcmgenet_isr1: handle Rx and Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002628static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
2629{
2630 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002631 struct bcmgenet_rx_ring *rx_ring;
2632 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08002633 unsigned int index, status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002634
Doug Berger07c52d62017-03-09 16:58:47 -08002635 /* Read irq status */
2636 status = bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002637 ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002638
Brian Norris7fc527f2014-07-29 14:34:14 -07002639 /* clear interrupts */
Doug Berger07c52d62017-03-09 16:58:47 -08002640 bcmgenet_intrl2_1_writel(priv, status, INTRL2_CPU_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002641
2642 netif_dbg(priv, intr, priv->dev,
Doug Berger07c52d62017-03-09 16:58:47 -08002643 "%s: IRQ=0x%x\n", __func__, status);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002644
Petri Gynther4055eae2015-03-25 12:35:16 -07002645 /* Check Rx priority queue interrupts */
2646 for (index = 0; index < priv->hw_params->rx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08002647 if (!(status & BIT(UMAC_IRQ1_RX_INTR_SHIFT + index)))
Petri Gynther4055eae2015-03-25 12:35:16 -07002648 continue;
2649
2650 rx_ring = &priv->rx_rings[index];
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002651 rx_ring->dim.event_ctr++;
Petri Gynther4055eae2015-03-25 12:35:16 -07002652
2653 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2654 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002655 __napi_schedule_irqoff(&rx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07002656 }
2657 }
2658
2659 /* Check Tx priority queue interrupts */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002660 for (index = 0; index < priv->hw_params->tx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08002661 if (!(status & BIT(index)))
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002662 continue;
2663
Petri Gynther4055eae2015-03-25 12:35:16 -07002664 tx_ring = &priv->tx_rings[index];
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002665
Petri Gynther4055eae2015-03-25 12:35:16 -07002666 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2667 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002668 __napi_schedule_irqoff(&tx_ring->napi);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002669 }
2670 }
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002671
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002672 return IRQ_HANDLED;
2673}
2674
Petri Gynther4055eae2015-03-25 12:35:16 -07002675/* bcmgenet_isr0: handle Rx and Tx default queues + other stuff */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002676static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
2677{
2678 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002679 struct bcmgenet_rx_ring *rx_ring;
2680 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08002681 unsigned int status;
2682 unsigned long flags;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002683
Doug Berger07c52d62017-03-09 16:58:47 -08002684 /* Read irq status */
2685 status = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002686 ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002687
Brian Norris7fc527f2014-07-29 14:34:14 -07002688 /* clear interrupts */
Doug Berger07c52d62017-03-09 16:58:47 -08002689 bcmgenet_intrl2_0_writel(priv, status, INTRL2_CPU_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002690
2691 netif_dbg(priv, intr, priv->dev,
Doug Berger07c52d62017-03-09 16:58:47 -08002692 "IRQ=0x%x\n", status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002693
Doug Berger07c52d62017-03-09 16:58:47 -08002694 if (status & UMAC_IRQ_RXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07002695 rx_ring = &priv->rx_rings[DESC_INDEX];
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002696 rx_ring->dim.event_ctr++;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002697
Petri Gynther4055eae2015-03-25 12:35:16 -07002698 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2699 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002700 __napi_schedule_irqoff(&rx_ring->napi);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002701 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002702 }
Petri Gynther4055eae2015-03-25 12:35:16 -07002703
Doug Berger07c52d62017-03-09 16:58:47 -08002704 if (status & UMAC_IRQ_TXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07002705 tx_ring = &priv->tx_rings[DESC_INDEX];
2706
2707 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2708 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002709 __napi_schedule_irqoff(&tx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07002710 }
2711 }
2712
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002713 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Doug Berger07c52d62017-03-09 16:58:47 -08002714 status & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002715 wake_up(&priv->wq);
2716 }
2717
Doug Berger07c52d62017-03-09 16:58:47 -08002718 /* all other interested interrupts handled in bottom half */
Doug Berger25382b92019-10-16 16:06:32 -07002719 status &= (UMAC_IRQ_LINK_EVENT | UMAC_IRQ_PHY_DET_R);
Doug Berger07c52d62017-03-09 16:58:47 -08002720 if (status) {
2721 /* Save irq status for bottom-half processing. */
2722 spin_lock_irqsave(&priv->lock, flags);
2723 priv->irq0_stat |= status;
2724 spin_unlock_irqrestore(&priv->lock, flags);
2725
2726 schedule_work(&priv->bcmgenet_irq_work);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002727 }
2728
2729 return IRQ_HANDLED;
2730}
2731
Florian Fainelli85620562014-07-21 15:29:23 -07002732static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
2733{
2734 struct bcmgenet_priv *priv = dev_id;
2735
2736 pm_wakeup_event(&priv->pdev->dev, 0);
2737
2738 return IRQ_HANDLED;
2739}
2740
Florian Fainelli4d2e8882015-07-31 11:42:54 -07002741#ifdef CONFIG_NET_POLL_CONTROLLER
2742static void bcmgenet_poll_controller(struct net_device *dev)
2743{
2744 struct bcmgenet_priv *priv = netdev_priv(dev);
2745
2746 /* Invoke the main RX/TX interrupt handler */
2747 disable_irq(priv->irq0);
2748 bcmgenet_isr0(priv->irq0, priv);
2749 enable_irq(priv->irq0);
2750
2751 /* And the interrupt handler for RX/TX priority queues */
2752 disable_irq(priv->irq1);
2753 bcmgenet_isr1(priv->irq1, priv);
2754 enable_irq(priv->irq1);
2755}
2756#endif
2757
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002758static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
2759{
2760 u32 reg;
2761
2762 reg = bcmgenet_rbuf_ctrl_get(priv);
2763 reg |= BIT(1);
2764 bcmgenet_rbuf_ctrl_set(priv, reg);
2765 udelay(10);
2766
2767 reg &= ~BIT(1);
2768 bcmgenet_rbuf_ctrl_set(priv, reg);
2769 udelay(10);
2770}
2771
2772static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002773 unsigned char *addr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002774{
2775 bcmgenet_umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
2776 (addr[2] << 8) | addr[3], UMAC_MAC0);
2777 bcmgenet_umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
2778}
2779
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002780/* Returns a reusable dma control register value */
2781static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
2782{
2783 u32 reg;
2784 u32 dma_ctrl;
2785
2786 /* disable DMA */
2787 dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
2788 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2789 reg &= ~dma_ctrl;
2790 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2791
2792 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2793 reg &= ~dma_ctrl;
2794 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2795
2796 bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
2797 udelay(10);
2798 bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
2799
2800 return dma_ctrl;
2801}
2802
2803static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
2804{
2805 u32 reg;
2806
2807 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2808 reg |= dma_ctrl;
2809 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2810
2811 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2812 reg |= dma_ctrl;
2813 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2814}
2815
Petri Gynther0034de42015-03-13 14:45:00 -07002816/* bcmgenet_hfb_clear
2817 *
2818 * Clear Hardware Filter Block and disable all filtering.
2819 */
2820static void bcmgenet_hfb_clear(struct bcmgenet_priv *priv)
2821{
2822 u32 i;
2823
2824 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_CTRL);
2825 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS);
2826 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS + 4);
2827
2828 for (i = DMA_INDEX2RING_0; i <= DMA_INDEX2RING_7; i++)
2829 bcmgenet_rdma_writel(priv, 0x0, i);
2830
2831 for (i = 0; i < (priv->hw_params->hfb_filter_cnt / 4); i++)
2832 bcmgenet_hfb_reg_writel(priv, 0x0,
2833 HFB_FLT_LEN_V3PLUS + i * sizeof(u32));
2834
2835 for (i = 0; i < priv->hw_params->hfb_filter_cnt *
2836 priv->hw_params->hfb_filter_size; i++)
2837 bcmgenet_hfb_writel(priv, 0x0, i * sizeof(u32));
2838}
2839
2840static void bcmgenet_hfb_init(struct bcmgenet_priv *priv)
2841{
2842 if (GENET_IS_V1(priv) || GENET_IS_V2(priv))
2843 return;
2844
2845 bcmgenet_hfb_clear(priv);
2846}
2847
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002848static void bcmgenet_netif_start(struct net_device *dev)
2849{
2850 struct bcmgenet_priv *priv = netdev_priv(dev);
2851
2852 /* Start the network engine */
Petri Gynther3ab11332015-03-25 12:35:15 -07002853 bcmgenet_enable_rx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002854
2855 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
2856
Doug Bergerd215dba2017-10-25 15:04:16 -07002857 bcmgenet_enable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002858
Florian Fainelli37850e32015-10-17 14:22:46 -07002859 /* Monitor link interrupts now */
2860 bcmgenet_link_intr_enable(priv);
2861
Doug Berger6c97f012017-10-25 15:04:19 -07002862 phy_start(dev->phydev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002863}
2864
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002865static int bcmgenet_open(struct net_device *dev)
2866{
2867 struct bcmgenet_priv *priv = netdev_priv(dev);
2868 unsigned long dma_ctrl;
2869 u32 reg;
2870 int ret;
2871
2872 netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
2873
2874 /* Turn on the clock */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002875 clk_prepare_enable(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002876
Florian Fainellia642c4f2015-03-23 15:09:56 -07002877 /* If this is an internal GPHY, power it back on now, before UniMAC is
2878 * brought out of reset as absolutely no UniMAC activity is allowed
2879 */
Florian Fainellic624f892015-07-16 15:51:17 -07002880 if (priv->internal_phy)
Florian Fainellia642c4f2015-03-23 15:09:56 -07002881 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
2882
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002883 /* take MAC out of reset */
2884 bcmgenet_umac_reset(priv);
2885
Doug Berger28c2d1a2017-10-25 15:04:13 -07002886 init_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002887
Doug Berger206f54b2019-12-17 16:51:12 -08002888 /* Apply features again in case we changed them while interface was
2889 * down
2890 */
2891 bcmgenet_set_features(dev, dev->features);
2892
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002893 bcmgenet_set_hw_addr(priv, dev->dev_addr);
2894
Florian Fainellic624f892015-07-16 15:51:17 -07002895 if (priv->internal_phy) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002896 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
2897 reg |= EXT_ENERGY_DET_MASK;
2898 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
2899 }
2900
2901 /* Disable RX/TX DMA and flush TX queues */
2902 dma_ctrl = bcmgenet_dma_disable(priv);
2903
2904 /* Reinitialize TDMA and RDMA and SW housekeeping */
2905 ret = bcmgenet_init_dma(priv);
2906 if (ret) {
2907 netdev_err(dev, "failed to initialize DMA\n");
Doug Berger6b6d017f2019-11-05 11:07:25 -08002908 goto err_clk_disable;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002909 }
2910
2911 /* Always enable ring 16 - descriptor ring */
2912 bcmgenet_enable_dma(priv, dma_ctrl);
2913
Petri Gynther0034de42015-03-13 14:45:00 -07002914 /* HFB init */
2915 bcmgenet_hfb_init(priv);
2916
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002917 ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002918 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002919 if (ret < 0) {
2920 netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
2921 goto err_fini_dma;
2922 }
2923
2924 ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002925 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002926 if (ret < 0) {
2927 netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
2928 goto err_irq0;
2929 }
2930
Doug Berger6b6d017f2019-11-05 11:07:25 -08002931 ret = bcmgenet_mii_probe(dev);
2932 if (ret) {
2933 netdev_err(dev, "failed to connect to PHY\n");
2934 goto err_irq1;
2935 }
2936
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002937 bcmgenet_netif_start(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002938
Doug Berger09e805d2018-11-01 15:55:37 -07002939 netif_tx_start_all_queues(dev);
2940
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002941 return 0;
2942
Doug Berger6b6d017f2019-11-05 11:07:25 -08002943err_irq1:
2944 free_irq(priv->irq1, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002945err_irq0:
Florian Fainelli978ffac42015-07-16 15:51:15 -07002946 free_irq(priv->irq0, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002947err_fini_dma:
Doug Berger4fd6dc92017-10-25 15:04:12 -07002948 bcmgenet_dma_teardown(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002949 bcmgenet_fini_dma(priv);
2950err_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08002951 if (priv->internal_phy)
2952 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002953 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002954 return ret;
2955}
2956
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002957static void bcmgenet_netif_stop(struct net_device *dev)
2958{
2959 struct bcmgenet_priv *priv = netdev_priv(dev);
2960
Doug Bergerd215dba2017-10-25 15:04:16 -07002961 bcmgenet_disable_tx_napi(priv);
Doug Berger09e805d2018-11-01 15:55:37 -07002962 netif_tx_disable(dev);
Doug Bergerd215dba2017-10-25 15:04:16 -07002963
2964 /* Disable MAC receive */
2965 umac_enable_set(priv, CMD_RX_EN, false);
2966
2967 bcmgenet_dma_teardown(priv);
2968
2969 /* Disable MAC transmit. TX DMA disabled must be done before this */
2970 umac_enable_set(priv, CMD_TX_EN, false);
2971
Doug Berger6c97f012017-10-25 15:04:19 -07002972 phy_stop(dev->phydev);
Petri Gynther3ab11332015-03-25 12:35:15 -07002973 bcmgenet_disable_rx_napi(priv);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002974 bcmgenet_intr_disable(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002975
2976 /* Wait for pending work items to complete. Since interrupts are
2977 * disabled no new work will be scheduled.
2978 */
2979 cancel_work_sync(&priv->bcmgenet_irq_work);
Florian Fainellicc013fb2014-08-11 14:50:43 -07002980
Florian Fainellicc013fb2014-08-11 14:50:43 -07002981 priv->old_link = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002982 priv->old_speed = -1;
Florian Fainellicc013fb2014-08-11 14:50:43 -07002983 priv->old_duplex = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002984 priv->old_pause = -1;
Doug Bergerd215dba2017-10-25 15:04:16 -07002985
2986 /* tx reclaim */
2987 bcmgenet_tx_reclaim_all(dev);
2988 bcmgenet_fini_dma(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002989}
2990
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002991static int bcmgenet_close(struct net_device *dev)
2992{
2993 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Bergerd215dba2017-10-25 15:04:16 -07002994 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002995
2996 netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
2997
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002998 bcmgenet_netif_stop(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002999
Florian Fainellic96e7312014-11-10 18:06:20 -08003000 /* Really kill the PHY state machine and disconnect from it */
Doug Berger6c97f012017-10-25 15:04:19 -07003001 phy_disconnect(dev->phydev);
Florian Fainellic96e7312014-11-10 18:06:20 -08003002
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003003 free_irq(priv->irq0, priv);
3004 free_irq(priv->irq1, priv);
3005
Florian Fainellic624f892015-07-16 15:51:17 -07003006 if (priv->internal_phy)
Florian Fainellica8cf342015-03-23 15:09:51 -07003007 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003008
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003009 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003010
Florian Fainellica8cf342015-03-23 15:09:51 -07003011 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003012}
3013
Florian Fainelli13ea6572015-06-04 16:15:50 -07003014static void bcmgenet_dump_tx_queue(struct bcmgenet_tx_ring *ring)
3015{
3016 struct bcmgenet_priv *priv = ring->priv;
3017 u32 p_index, c_index, intsts, intmsk;
3018 struct netdev_queue *txq;
3019 unsigned int free_bds;
Florian Fainelli13ea6572015-06-04 16:15:50 -07003020 bool txq_stopped;
3021
3022 if (!netif_msg_tx_err(priv))
3023 return;
3024
3025 txq = netdev_get_tx_queue(priv->dev, ring->queue);
3026
Doug Bergerb0447ec2017-10-25 15:04:17 -07003027 spin_lock(&ring->lock);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003028 if (ring->index == DESC_INDEX) {
3029 intsts = ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
3030 intmsk = UMAC_IRQ_TXDMA_DONE | UMAC_IRQ_TXDMA_MBDONE;
3031 } else {
3032 intsts = ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
3033 intmsk = 1 << ring->index;
3034 }
3035 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
3036 p_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_PROD_INDEX);
3037 txq_stopped = netif_tx_queue_stopped(txq);
3038 free_bds = ring->free_bds;
Doug Bergerb0447ec2017-10-25 15:04:17 -07003039 spin_unlock(&ring->lock);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003040
3041 netif_err(priv, tx_err, priv->dev, "Ring %d queue %d status summary\n"
3042 "TX queue status: %s, interrupts: %s\n"
3043 "(sw)free_bds: %d (sw)size: %d\n"
3044 "(sw)p_index: %d (hw)p_index: %d\n"
3045 "(sw)c_index: %d (hw)c_index: %d\n"
3046 "(sw)clean_p: %d (sw)write_p: %d\n"
3047 "(sw)cb_ptr: %d (sw)end_ptr: %d\n",
3048 ring->index, ring->queue,
3049 txq_stopped ? "stopped" : "active",
3050 intsts & intmsk ? "enabled" : "disabled",
3051 free_bds, ring->size,
3052 ring->prod_index, p_index & DMA_P_INDEX_MASK,
3053 ring->c_index, c_index & DMA_C_INDEX_MASK,
3054 ring->clean_ptr, ring->write_ptr,
3055 ring->cb_ptr, ring->end_ptr);
3056}
3057
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05003058static void bcmgenet_timeout(struct net_device *dev, unsigned int txqueue)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003059{
3060 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003061 u32 int0_enable = 0;
3062 u32 int1_enable = 0;
3063 unsigned int q;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003064
3065 netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
3066
Florian Fainelli13ea6572015-06-04 16:15:50 -07003067 for (q = 0; q < priv->hw_params->tx_queues; q++)
3068 bcmgenet_dump_tx_queue(&priv->tx_rings[q]);
3069 bcmgenet_dump_tx_queue(&priv->tx_rings[DESC_INDEX]);
3070
3071 bcmgenet_tx_reclaim_all(dev);
3072
3073 for (q = 0; q < priv->hw_params->tx_queues; q++)
3074 int1_enable |= (1 << q);
3075
3076 int0_enable = UMAC_IRQ_TXDMA_DONE;
3077
3078 /* Re-enable TX interrupts if disabled */
3079 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
3080 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
3081
Florian Westphal860e9532016-05-03 16:33:13 +02003082 netif_trans_update(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003083
3084 dev->stats.tx_errors++;
3085
3086 netif_tx_wake_all_queues(dev);
3087}
3088
Justin Chen35cbef92019-07-17 14:58:53 -07003089#define MAX_MDF_FILTER 17
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003090
3091static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
3092 unsigned char *addr,
Justin Chen35cbef92019-07-17 14:58:53 -07003093 int *i)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003094{
Florian Fainellic91b7f62014-07-23 10:42:12 -07003095 bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
3096 UMAC_MDF_ADDR + (*i * 4));
3097 bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
3098 addr[4] << 8 | addr[5],
3099 UMAC_MDF_ADDR + ((*i + 1) * 4));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003100 *i += 2;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003101}
3102
3103static void bcmgenet_set_rx_mode(struct net_device *dev)
3104{
3105 struct bcmgenet_priv *priv = netdev_priv(dev);
3106 struct netdev_hw_addr *ha;
Justin Chen35cbef92019-07-17 14:58:53 -07003107 int i, nfilter;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003108 u32 reg;
3109
3110 netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
3111
Justin Chen35cbef92019-07-17 14:58:53 -07003112 /* Number of filters needed */
3113 nfilter = netdev_uc_count(dev) + netdev_mc_count(dev) + 2;
3114
3115 /*
3116 * Turn on promicuous mode for three scenarios
3117 * 1. IFF_PROMISC flag is set
3118 * 2. IFF_ALLMULTI flag is set
3119 * 3. The number of filters needed exceeds the number filters
3120 * supported by the hardware.
3121 */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003122 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
Justin Chen35cbef92019-07-17 14:58:53 -07003123 if ((dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) ||
3124 (nfilter > MAX_MDF_FILTER)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003125 reg |= CMD_PROMISC;
3126 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3127 bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
3128 return;
3129 } else {
3130 reg &= ~CMD_PROMISC;
3131 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3132 }
3133
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003134 /* update MDF filter */
3135 i = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003136 /* Broadcast */
Justin Chen35cbef92019-07-17 14:58:53 -07003137 bcmgenet_set_mdf_addr(priv, dev->broadcast, &i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003138 /* my own address.*/
Justin Chen35cbef92019-07-17 14:58:53 -07003139 bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003140
Justin Chen35cbef92019-07-17 14:58:53 -07003141 /* Unicast */
3142 netdev_for_each_uc_addr(ha, dev)
3143 bcmgenet_set_mdf_addr(priv, ha->addr, &i);
3144
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003145 /* Multicast */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003146 netdev_for_each_mc_addr(ha, dev)
Justin Chen35cbef92019-07-17 14:58:53 -07003147 bcmgenet_set_mdf_addr(priv, ha->addr, &i);
3148
3149 /* Enable filters */
3150 reg = GENMASK(MAX_MDF_FILTER - 1, MAX_MDF_FILTER - nfilter);
3151 bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003152}
3153
3154/* Set the hardware MAC address. */
3155static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
3156{
3157 struct sockaddr *addr = p;
3158
3159 /* Setting the MAC address at the hardware level is not possible
3160 * without disabling the UniMAC RX/TX enable bits.
3161 */
3162 if (netif_running(dev))
3163 return -EBUSY;
3164
3165 ether_addr_copy(dev->dev_addr, addr->sa_data);
3166
3167 return 0;
3168}
3169
Florian Fainelli37a30b42017-03-16 10:27:08 -07003170static struct net_device_stats *bcmgenet_get_stats(struct net_device *dev)
3171{
3172 struct bcmgenet_priv *priv = netdev_priv(dev);
3173 unsigned long tx_bytes = 0, tx_packets = 0;
3174 unsigned long rx_bytes = 0, rx_packets = 0;
3175 unsigned long rx_errors = 0, rx_dropped = 0;
3176 struct bcmgenet_tx_ring *tx_ring;
3177 struct bcmgenet_rx_ring *rx_ring;
3178 unsigned int q;
3179
3180 for (q = 0; q < priv->hw_params->tx_queues; q++) {
3181 tx_ring = &priv->tx_rings[q];
3182 tx_bytes += tx_ring->bytes;
3183 tx_packets += tx_ring->packets;
3184 }
3185 tx_ring = &priv->tx_rings[DESC_INDEX];
3186 tx_bytes += tx_ring->bytes;
3187 tx_packets += tx_ring->packets;
3188
3189 for (q = 0; q < priv->hw_params->rx_queues; q++) {
3190 rx_ring = &priv->rx_rings[q];
3191
3192 rx_bytes += rx_ring->bytes;
3193 rx_packets += rx_ring->packets;
3194 rx_errors += rx_ring->errors;
3195 rx_dropped += rx_ring->dropped;
3196 }
3197 rx_ring = &priv->rx_rings[DESC_INDEX];
3198 rx_bytes += rx_ring->bytes;
3199 rx_packets += rx_ring->packets;
3200 rx_errors += rx_ring->errors;
3201 rx_dropped += rx_ring->dropped;
3202
3203 dev->stats.tx_bytes = tx_bytes;
3204 dev->stats.tx_packets = tx_packets;
3205 dev->stats.rx_bytes = rx_bytes;
3206 dev->stats.rx_packets = rx_packets;
3207 dev->stats.rx_errors = rx_errors;
3208 dev->stats.rx_missed_errors = rx_errors;
3209 return &dev->stats;
3210}
3211
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003212static const struct net_device_ops bcmgenet_netdev_ops = {
3213 .ndo_open = bcmgenet_open,
3214 .ndo_stop = bcmgenet_close,
3215 .ndo_start_xmit = bcmgenet_xmit,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003216 .ndo_tx_timeout = bcmgenet_timeout,
3217 .ndo_set_rx_mode = bcmgenet_set_rx_mode,
3218 .ndo_set_mac_address = bcmgenet_set_mac_addr,
3219 .ndo_do_ioctl = bcmgenet_ioctl,
3220 .ndo_set_features = bcmgenet_set_features,
Florian Fainelli4d2e8882015-07-31 11:42:54 -07003221#ifdef CONFIG_NET_POLL_CONTROLLER
3222 .ndo_poll_controller = bcmgenet_poll_controller,
3223#endif
Florian Fainelli37a30b42017-03-16 10:27:08 -07003224 .ndo_get_stats = bcmgenet_get_stats,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003225};
3226
3227/* Array of GENET hardware parameters/characteristics */
3228static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
3229 [GENET_V1] = {
3230 .tx_queues = 0,
Petri Gynther51a966a2015-02-23 11:00:46 -08003231 .tx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003232 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003233 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003234 .bp_in_en_shift = 16,
3235 .bp_in_mask = 0xffff,
3236 .hfb_filter_cnt = 16,
3237 .qtag_mask = 0x1F,
3238 .hfb_offset = 0x1000,
3239 .rdma_offset = 0x2000,
3240 .tdma_offset = 0x3000,
3241 .words_per_bd = 2,
3242 },
3243 [GENET_V2] = {
3244 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003245 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003246 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003247 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003248 .bp_in_en_shift = 16,
3249 .bp_in_mask = 0xffff,
3250 .hfb_filter_cnt = 16,
3251 .qtag_mask = 0x1F,
3252 .tbuf_offset = 0x0600,
3253 .hfb_offset = 0x1000,
3254 .hfb_reg_offset = 0x2000,
3255 .rdma_offset = 0x3000,
3256 .tdma_offset = 0x4000,
3257 .words_per_bd = 2,
3258 .flags = GENET_HAS_EXT,
3259 },
3260 [GENET_V3] = {
3261 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003262 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003263 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003264 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003265 .bp_in_en_shift = 17,
3266 .bp_in_mask = 0x1ffff,
3267 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003268 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003269 .qtag_mask = 0x3F,
3270 .tbuf_offset = 0x0600,
3271 .hfb_offset = 0x8000,
3272 .hfb_reg_offset = 0xfc00,
3273 .rdma_offset = 0x10000,
3274 .tdma_offset = 0x11000,
3275 .words_per_bd = 2,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003276 .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR |
3277 GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003278 },
3279 [GENET_V4] = {
3280 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003281 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003282 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003283 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003284 .bp_in_en_shift = 17,
3285 .bp_in_mask = 0x1ffff,
3286 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003287 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003288 .qtag_mask = 0x3F,
3289 .tbuf_offset = 0x0600,
3290 .hfb_offset = 0x8000,
3291 .hfb_reg_offset = 0xfc00,
3292 .rdma_offset = 0x2000,
3293 .tdma_offset = 0x4000,
3294 .words_per_bd = 3,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003295 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3296 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003297 },
Doug Berger42138082017-03-13 17:41:42 -07003298 [GENET_V5] = {
3299 .tx_queues = 4,
3300 .tx_bds_per_q = 32,
3301 .rx_queues = 0,
3302 .rx_bds_per_q = 0,
3303 .bp_in_en_shift = 17,
3304 .bp_in_mask = 0x1ffff,
3305 .hfb_filter_cnt = 48,
3306 .hfb_filter_size = 128,
3307 .qtag_mask = 0x3F,
3308 .tbuf_offset = 0x0600,
3309 .hfb_offset = 0x8000,
3310 .hfb_reg_offset = 0xfc00,
3311 .rdma_offset = 0x2000,
3312 .tdma_offset = 0x4000,
3313 .words_per_bd = 3,
3314 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3315 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
3316 },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003317};
3318
3319/* Infer hardware parameters from the detected GENET version */
3320static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
3321{
3322 struct bcmgenet_hw_params *params;
3323 u32 reg;
3324 u8 major;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003325 u16 gphy_rev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003326
Doug Berger42138082017-03-13 17:41:42 -07003327 if (GENET_IS_V5(priv) || GENET_IS_V4(priv)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003328 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3329 genet_dma_ring_regs = genet_dma_ring_regs_v4;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003330 } else if (GENET_IS_V3(priv)) {
3331 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3332 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003333 } else if (GENET_IS_V2(priv)) {
3334 bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
3335 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003336 } else if (GENET_IS_V1(priv)) {
3337 bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
3338 genet_dma_ring_regs = genet_dma_ring_regs_v123;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003339 }
3340
3341 /* enum genet_version starts at 1 */
3342 priv->hw_params = &bcmgenet_hw_params[priv->version];
3343 params = priv->hw_params;
3344
3345 /* Read GENET HW version */
3346 reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
3347 major = (reg >> 24 & 0x0f);
Doug Berger42138082017-03-13 17:41:42 -07003348 if (major == 6)
3349 major = 5;
3350 else if (major == 5)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003351 major = 4;
3352 else if (major == 0)
3353 major = 1;
3354 if (major != priv->version) {
3355 dev_err(&priv->pdev->dev,
3356 "GENET version mismatch, got: %d, configured for: %d\n",
3357 major, priv->version);
3358 }
3359
3360 /* Print the GENET core version */
3361 dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003362 major, (reg >> 16) & 0x0f, reg & 0xffff);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003363
Florian Fainelli487320c2014-09-19 13:07:53 -07003364 /* Store the integrated PHY revision for the MDIO probing function
3365 * to pass this information to the PHY driver. The PHY driver expects
3366 * to find the PHY major revision in bits 15:8 while the GENET register
3367 * stores that information in bits 7:0, account for that.
Florian Fainellib04a2f52014-12-03 09:56:59 -08003368 *
3369 * On newer chips, starting with PHY revision G0, a new scheme is
3370 * deployed similar to the Starfighter 2 switch with GPHY major
3371 * revision in bits 15:8 and patch level in bits 7:0. Major revision 0
3372 * is reserved as well as special value 0x01ff, we have a small
3373 * heuristic to check for the new GPHY revision and re-arrange things
3374 * so the GPHY driver is happy.
Florian Fainelli487320c2014-09-19 13:07:53 -07003375 */
Florian Fainellib04a2f52014-12-03 09:56:59 -08003376 gphy_rev = reg & 0xffff;
3377
Doug Berger42138082017-03-13 17:41:42 -07003378 if (GENET_IS_V5(priv)) {
3379 /* The EPHY revision should come from the MDIO registers of
3380 * the PHY not from GENET.
3381 */
3382 if (gphy_rev != 0) {
3383 pr_warn("GENET is reporting EPHY revision: 0x%04x\n",
3384 gphy_rev);
3385 }
Doug Bergereca4bad2017-03-09 16:58:45 -08003386 /* This is reserved so should require special treatment */
David S. Miller101c4312017-03-15 11:59:10 -07003387 } else if (gphy_rev == 0 || gphy_rev == 0x01ff) {
Doug Bergereca4bad2017-03-09 16:58:45 -08003388 pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev);
3389 return;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003390 /* This is the good old scheme, just GPHY major, no minor nor patch */
Doug Berger42138082017-03-13 17:41:42 -07003391 } else if ((gphy_rev & 0xf0) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003392 priv->gphy_rev = gphy_rev << 8;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003393 /* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */
Doug Berger42138082017-03-13 17:41:42 -07003394 } else if ((gphy_rev & 0xff00) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003395 priv->gphy_rev = gphy_rev;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003396 }
Florian Fainelli487320c2014-09-19 13:07:53 -07003397
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003398#ifdef CONFIG_PHYS_ADDR_T_64BIT
3399 if (!(params->flags & GENET_HAS_40BITS))
3400 pr_warn("GENET does not support 40-bits PA\n");
3401#endif
3402
3403 pr_debug("Configuration for version: %d\n"
Petri Gynther3feafa02015-03-05 17:40:14 -08003404 "TXq: %1d, TXqBDs: %1d, RXq: %1d, RXqBDs: %1d\n"
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003405 "BP << en: %2d, BP msk: 0x%05x\n"
3406 "HFB count: %2d, QTAQ msk: 0x%05x\n"
3407 "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
3408 "RDMA: 0x%05x, TDMA: 0x%05x\n"
3409 "Words/BD: %d\n",
3410 priv->version,
Petri Gynther51a966a2015-02-23 11:00:46 -08003411 params->tx_queues, params->tx_bds_per_q,
Petri Gynther3feafa02015-03-05 17:40:14 -08003412 params->rx_queues, params->rx_bds_per_q,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003413 params->bp_in_en_shift, params->bp_in_mask,
3414 params->hfb_filter_cnt, params->qtag_mask,
3415 params->tbuf_offset, params->hfb_offset,
3416 params->hfb_reg_offset,
3417 params->rdma_offset, params->tdma_offset,
3418 params->words_per_bd);
3419}
3420
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003421struct bcmgenet_plat_data {
3422 enum bcmgenet_version version;
3423 u32 dma_max_burst_length;
3424};
3425
3426static const struct bcmgenet_plat_data v1_plat_data = {
3427 .version = GENET_V1,
3428 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3429};
3430
3431static const struct bcmgenet_plat_data v2_plat_data = {
3432 .version = GENET_V2,
3433 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3434};
3435
3436static const struct bcmgenet_plat_data v3_plat_data = {
3437 .version = GENET_V3,
3438 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3439};
3440
3441static const struct bcmgenet_plat_data v4_plat_data = {
3442 .version = GENET_V4,
3443 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3444};
3445
3446static const struct bcmgenet_plat_data v5_plat_data = {
3447 .version = GENET_V5,
3448 .dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3449};
3450
3451static const struct bcmgenet_plat_data bcm2711_plat_data = {
3452 .version = GENET_V5,
3453 .dma_max_burst_length = 0x08,
3454};
3455
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003456static const struct of_device_id bcmgenet_match[] = {
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003457 { .compatible = "brcm,genet-v1", .data = &v1_plat_data },
3458 { .compatible = "brcm,genet-v2", .data = &v2_plat_data },
3459 { .compatible = "brcm,genet-v3", .data = &v3_plat_data },
3460 { .compatible = "brcm,genet-v4", .data = &v4_plat_data },
3461 { .compatible = "brcm,genet-v5", .data = &v5_plat_data },
3462 { .compatible = "brcm,bcm2711-genet-v5", .data = &bcm2711_plat_data },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003463 { },
3464};
Luis de Bethencourte8048e52015-09-18 17:55:02 +02003465MODULE_DEVICE_TABLE(of, bcmgenet_match);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003466
3467static int bcmgenet_probe(struct platform_device *pdev)
3468{
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003469 struct bcmgenet_platform_data *pd = pdev->dev.platform_data;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003470 struct device_node *dn = pdev->dev.of_node;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003471 const struct of_device_id *of_id = NULL;
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003472 const struct bcmgenet_plat_data *pdata;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003473 struct bcmgenet_priv *priv;
3474 struct net_device *dev;
3475 const void *macaddr;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07003476 unsigned int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003477 int err = -EIO;
Doug Berger6be371b2017-03-09 16:58:48 -08003478 const char *phy_mode_str;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003479
Petri Gynther3feafee2015-03-05 17:40:12 -08003480 /* Up to GENET_MAX_MQ_CNT + 1 TX queues and RX queues */
3481 dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1,
3482 GENET_MAX_MQ_CNT + 1);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003483 if (!dev) {
3484 dev_err(&pdev->dev, "can't allocate net device\n");
3485 return -ENOMEM;
3486 }
3487
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003488 if (dn) {
3489 of_id = of_match_node(bcmgenet_match, dn);
3490 if (!of_id)
3491 return -EINVAL;
3492 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003493
3494 priv = netdev_priv(dev);
3495 priv->irq0 = platform_get_irq(pdev, 0);
Stefan Wahren2b65f932019-11-11 20:49:21 +01003496 if (priv->irq0 < 0) {
3497 err = priv->irq0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003498 goto err;
3499 }
Stefan Wahren2b65f932019-11-11 20:49:21 +01003500 priv->irq1 = platform_get_irq(pdev, 1);
3501 if (priv->irq1 < 0) {
3502 err = priv->irq1;
3503 goto err;
3504 }
3505 priv->wol_irq = platform_get_irq_optional(pdev, 2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003506
Florian Fainellid0337162019-10-14 14:20:00 -07003507 if (dn)
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003508 macaddr = of_get_mac_address(dn);
Florian Fainellid0337162019-10-14 14:20:00 -07003509 else
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003510 macaddr = pd->mac_address;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003511
YueHaibing4ca33482019-08-21 21:41:31 +08003512 priv->base = devm_platform_ioremap_resource(pdev, 0);
Fabio Estevam5343a102014-02-24 00:47:24 -03003513 if (IS_ERR(priv->base)) {
3514 err = PTR_ERR(priv->base);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003515 goto err;
3516 }
3517
Doug Berger07c52d62017-03-09 16:58:47 -08003518 spin_lock_init(&priv->lock);
3519
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003520 SET_NETDEV_DEV(dev, &pdev->dev);
3521 dev_set_drvdata(&pdev->dev, dev);
Florian Fainellid0337162019-10-14 14:20:00 -07003522 if (IS_ERR_OR_NULL(macaddr) || !is_valid_ether_addr(macaddr)) {
3523 dev_warn(&pdev->dev, "using random Ethernet MAC\n");
3524 eth_hw_addr_random(dev);
3525 } else {
3526 ether_addr_copy(dev->dev_addr, macaddr);
3527 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003528 dev->watchdog_timeo = 2 * HZ;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003529 dev->ethtool_ops = &bcmgenet_ethtool_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003530 dev->netdev_ops = &bcmgenet_netdev_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003531
3532 priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
3533
Doug Bergerae895c42019-12-17 16:51:13 -08003534 /* Set default features */
3535 dev->features |= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
3536 NETIF_F_RXCSUM;
3537 dev->hw_features |= dev->features;
3538 dev->vlan_features |= dev->features;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003539
Florian Fainelli85620562014-07-21 15:29:23 -07003540 /* Request the WOL interrupt and advertise suspend if available */
3541 priv->wol_irq_disabled = true;
3542 err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0,
3543 dev->name, priv);
3544 if (!err)
3545 device_set_wakeup_capable(&pdev->dev, 1);
3546
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003547 /* Set the needed headroom to account for any possible
3548 * features enabling/disabling at runtime
3549 */
3550 dev->needed_headroom += 64;
3551
3552 netdev_boot_setup_check(dev);
3553
3554 priv->dev = dev;
3555 priv->pdev = pdev;
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003556 if (of_id) {
3557 pdata = of_id->data;
3558 priv->version = pdata->version;
3559 priv->dma_max_burst_length = pdata->dma_max_burst_length;
3560 } else {
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003561 priv->version = pd->genet_version;
Stefan Wahrena50e3a92019-11-11 20:49:23 +01003562 priv->dma_max_burst_length = DMA_MAX_BURST_LENGTH;
3563 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003564
Florian Fainellie4a60a92014-08-11 14:50:42 -07003565 priv->clk = devm_clk_get(&priv->pdev->dev, "enet");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003566 if (IS_ERR(priv->clk)) {
Florian Fainellie4a60a92014-08-11 14:50:42 -07003567 dev_warn(&priv->pdev->dev, "failed to get enet clock\n");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003568 priv->clk = NULL;
3569 }
Florian Fainellie4a60a92014-08-11 14:50:42 -07003570
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003571 clk_prepare_enable(priv->clk);
Florian Fainellie4a60a92014-08-11 14:50:42 -07003572
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003573 bcmgenet_set_hw_params(priv);
3574
Doug Berger99d55632019-12-17 16:51:08 -08003575 err = -EIO;
3576 if (priv->hw_params->flags & GENET_HAS_40BITS)
3577 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
3578 if (err)
3579 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3580 if (err)
3581 goto err;
3582
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003583 /* Mii wait queue */
3584 init_waitqueue_head(&priv->wq);
3585 /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
3586 priv->rx_buf_len = RX_BUF_LENGTH;
3587 INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
3588
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003589 priv->clk_wol = devm_clk_get(&priv->pdev->dev, "enet-wol");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003590 if (IS_ERR(priv->clk_wol)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003591 dev_warn(&priv->pdev->dev, "failed to get enet-wol clock\n");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003592 priv->clk_wol = NULL;
3593 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003594
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003595 priv->clk_eee = devm_clk_get(&priv->pdev->dev, "enet-eee");
3596 if (IS_ERR(priv->clk_eee)) {
3597 dev_warn(&priv->pdev->dev, "failed to get enet-eee clock\n");
3598 priv->clk_eee = NULL;
3599 }
3600
Doug Berger6be371b2017-03-09 16:58:48 -08003601 /* If this is an internal GPHY, power it on now, before UniMAC is
3602 * brought out of reset as absolutely no UniMAC activity is allowed
3603 */
3604 if (dn && !of_property_read_string(dn, "phy-mode", &phy_mode_str) &&
3605 !strcasecmp(phy_mode_str, "internal"))
3606 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3607
Doug Berger28c2d1a2017-10-25 15:04:13 -07003608 reset_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003609
3610 err = bcmgenet_mii_init(dev);
3611 if (err)
3612 goto err_clk_disable;
3613
3614 /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
3615 * just the ring 16 descriptor based TX
3616 */
3617 netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
3618 netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
3619
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07003620 /* Set default coalescing parameters */
3621 for (i = 0; i < priv->hw_params->rx_queues; i++)
3622 priv->rx_rings[i].rx_max_coalesced_frames = 1;
3623 priv->rx_rings[DESC_INDEX].rx_max_coalesced_frames = 1;
3624
Florian Fainelli219575e2014-06-26 10:26:21 -07003625 /* libphy will determine the link state */
3626 netif_carrier_off(dev);
3627
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003628 /* Turn off the main clock, WOL clock is handled separately */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003629 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003630
Florian Fainelli0f50ce92014-06-26 10:26:20 -07003631 err = register_netdev(dev);
3632 if (err)
3633 goto err;
3634
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003635 return err;
3636
3637err_clk_disable:
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003638 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003639err:
3640 free_netdev(dev);
3641 return err;
3642}
3643
3644static int bcmgenet_remove(struct platform_device *pdev)
3645{
3646 struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
3647
3648 dev_set_drvdata(&pdev->dev, NULL);
3649 unregister_netdev(priv->dev);
3650 bcmgenet_mii_exit(priv->dev);
3651 free_netdev(priv->dev);
3652
3653 return 0;
3654}
3655
Florian Fainellid9f45ab2019-10-15 10:36:24 -07003656static void bcmgenet_shutdown(struct platform_device *pdev)
3657{
3658 bcmgenet_remove(pdev);
3659}
3660
Florian Fainellib6e978e2014-07-21 15:29:22 -07003661#ifdef CONFIG_PM_SLEEP
Florian Fainellib6e978e2014-07-21 15:29:22 -07003662static int bcmgenet_resume(struct device *d)
3663{
3664 struct net_device *dev = dev_get_drvdata(d);
3665 struct bcmgenet_priv *priv = netdev_priv(dev);
3666 unsigned long dma_ctrl;
3667 int ret;
3668 u32 reg;
3669
3670 if (!netif_running(dev))
3671 return 0;
3672
3673 /* Turn on the clock */
3674 ret = clk_prepare_enable(priv->clk);
3675 if (ret)
3676 return ret;
3677
Florian Fainellia6f31f52015-03-23 15:09:57 -07003678 /* If this is an internal GPHY, power it back on now, before UniMAC is
3679 * brought out of reset as absolutely no UniMAC activity is allowed
3680 */
Florian Fainellic624f892015-07-16 15:51:17 -07003681 if (priv->internal_phy)
Florian Fainellia6f31f52015-03-23 15:09:57 -07003682 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3683
Florian Fainellib6e978e2014-07-21 15:29:22 -07003684 bcmgenet_umac_reset(priv);
3685
Doug Berger28c2d1a2017-10-25 15:04:13 -07003686 init_umac(priv);
Florian Fainellib6e978e2014-07-21 15:29:22 -07003687
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003688 /* From WOL-enabled suspend, switch to regular clock */
3689 if (priv->wolopts)
3690 clk_disable_unprepare(priv->clk_wol);
3691
Doug Berger6b6d017f2019-11-05 11:07:25 -08003692 phy_init_hw(dev->phydev);
3693
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003694 /* Speed settings must be restored */
Doug Berger0686bd92019-11-05 11:07:26 -08003695 genphy_config_aneg(dev->phydev);
Florian Fainelli00d51092017-07-31 11:05:32 -07003696 bcmgenet_mii_config(priv->dev, false);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003697
Doug Berger206f54b2019-12-17 16:51:12 -08003698 /* Restore enabled features */
3699 bcmgenet_set_features(dev, dev->features);
3700
Florian Fainellib6e978e2014-07-21 15:29:22 -07003701 bcmgenet_set_hw_addr(priv, dev->dev_addr);
3702
Florian Fainellic624f892015-07-16 15:51:17 -07003703 if (priv->internal_phy) {
Florian Fainellib6e978e2014-07-21 15:29:22 -07003704 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
3705 reg |= EXT_ENERGY_DET_MASK;
3706 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
3707 }
3708
Florian Fainelli98bb7392014-08-11 14:50:45 -07003709 if (priv->wolopts)
3710 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
3711
Florian Fainellib6e978e2014-07-21 15:29:22 -07003712 /* Disable RX/TX DMA and flush TX queues */
3713 dma_ctrl = bcmgenet_dma_disable(priv);
3714
3715 /* Reinitialize TDMA and RDMA and SW housekeeping */
3716 ret = bcmgenet_init_dma(priv);
3717 if (ret) {
3718 netdev_err(dev, "failed to initialize DMA\n");
3719 goto out_clk_disable;
3720 }
3721
3722 /* Always enable ring 16 - descriptor ring */
3723 bcmgenet_enable_dma(priv, dma_ctrl);
3724
Florian Fainelli5371bbf42017-03-15 12:57:21 -07003725 if (!device_may_wakeup(d))
Doug Berger6c97f012017-10-25 15:04:19 -07003726 phy_resume(dev->phydev);
Florian Fainellicc013fb2014-08-11 14:50:43 -07003727
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003728 if (priv->eee.eee_enabled)
3729 bcmgenet_eee_enable_set(dev, true);
3730
Florian Fainellib6e978e2014-07-21 15:29:22 -07003731 bcmgenet_netif_start(dev);
3732
Doug Berger09e805d2018-11-01 15:55:37 -07003733 netif_device_attach(dev);
3734
Florian Fainellib6e978e2014-07-21 15:29:22 -07003735 return 0;
3736
3737out_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08003738 if (priv->internal_phy)
3739 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainellib6e978e2014-07-21 15:29:22 -07003740 clk_disable_unprepare(priv->clk);
3741 return ret;
3742}
Doug Bergera94cbf02018-11-16 18:00:21 -08003743
3744static int bcmgenet_suspend(struct device *d)
3745{
3746 struct net_device *dev = dev_get_drvdata(d);
3747 struct bcmgenet_priv *priv = netdev_priv(dev);
3748 int ret = 0;
3749
3750 if (!netif_running(dev))
3751 return 0;
3752
3753 netif_device_detach(dev);
3754
3755 bcmgenet_netif_stop(dev);
3756
3757 if (!device_may_wakeup(d))
3758 phy_suspend(dev->phydev);
3759
3760 /* Prepare the device for Wake-on-LAN and switch to the slow clock */
3761 if (device_may_wakeup(d) && priv->wolopts) {
3762 ret = bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
3763 clk_prepare_enable(priv->clk_wol);
3764 } else if (priv->internal_phy) {
3765 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
3766 }
3767
3768 /* Turn off the clocks */
3769 clk_disable_unprepare(priv->clk);
3770
Doug Bergerc5a54bb2018-11-16 18:00:22 -08003771 if (ret)
3772 bcmgenet_resume(d);
3773
Doug Bergera94cbf02018-11-16 18:00:21 -08003774 return ret;
3775}
Florian Fainellib6e978e2014-07-21 15:29:22 -07003776#endif /* CONFIG_PM_SLEEP */
3777
3778static SIMPLE_DEV_PM_OPS(bcmgenet_pm_ops, bcmgenet_suspend, bcmgenet_resume);
3779
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003780static struct platform_driver bcmgenet_driver = {
3781 .probe = bcmgenet_probe,
3782 .remove = bcmgenet_remove,
Florian Fainellid9f45ab2019-10-15 10:36:24 -07003783 .shutdown = bcmgenet_shutdown,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003784 .driver = {
3785 .name = "bcmgenet",
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003786 .of_match_table = bcmgenet_match,
Florian Fainellib6e978e2014-07-21 15:29:22 -07003787 .pm = &bcmgenet_pm_ops,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003788 },
3789};
3790module_platform_driver(bcmgenet_driver);
3791
3792MODULE_AUTHOR("Broadcom Corporation");
3793MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
3794MODULE_ALIAS("platform:bcmgenet");
3795MODULE_LICENSE("GPL");