blob: 1586316eb6f1a8612d9aa7137263ac36c4290ed6 [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 Bergerc298ede2017-03-13 17:41:33 -07005 * Copyright (c) 2014-2017 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
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800511static int bcmgenet_set_rx_csum(struct net_device *dev,
512 netdev_features_t wanted)
513{
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)
524 rbuf_chk_ctrl |= RBUF_RXCHK_EN;
525 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);
538
539 return 0;
540}
541
542static int bcmgenet_set_tx_csum(struct net_device *dev,
543 netdev_features_t wanted)
544{
545 struct bcmgenet_priv *priv = netdev_priv(dev);
546 bool desc_64b_en;
547 u32 tbuf_ctrl, rbuf_ctrl;
548
549 tbuf_ctrl = bcmgenet_tbuf_ctrl_get(priv);
550 rbuf_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
551
552 desc_64b_en = !!(wanted & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
553
554 /* enable 64 bytes descriptor in both directions (RBUF and TBUF) */
555 if (desc_64b_en) {
556 tbuf_ctrl |= RBUF_64B_EN;
557 rbuf_ctrl |= RBUF_64B_EN;
558 } else {
559 tbuf_ctrl &= ~RBUF_64B_EN;
560 rbuf_ctrl &= ~RBUF_64B_EN;
561 }
562 priv->desc_64b_en = desc_64b_en;
563
564 bcmgenet_tbuf_ctrl_set(priv, tbuf_ctrl);
565 bcmgenet_rbuf_writel(priv, rbuf_ctrl, RBUF_CTRL);
566
567 return 0;
568}
569
570static int bcmgenet_set_features(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700571 netdev_features_t features)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800572{
573 netdev_features_t changed = features ^ dev->features;
574 netdev_features_t wanted = dev->wanted_features;
575 int ret = 0;
576
577 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
578 ret = bcmgenet_set_tx_csum(dev, wanted);
579 if (changed & (NETIF_F_RXCSUM))
580 ret = bcmgenet_set_rx_csum(dev, wanted);
581
582 return ret;
583}
584
585static u32 bcmgenet_get_msglevel(struct net_device *dev)
586{
587 struct bcmgenet_priv *priv = netdev_priv(dev);
588
589 return priv->msg_enable;
590}
591
592static void bcmgenet_set_msglevel(struct net_device *dev, u32 level)
593{
594 struct bcmgenet_priv *priv = netdev_priv(dev);
595
596 priv->msg_enable = level;
597}
598
Florian Fainelli2f913072015-09-16 16:47:39 -0700599static int bcmgenet_get_coalesce(struct net_device *dev,
600 struct ethtool_coalesce *ec)
601{
602 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700603 struct bcmgenet_rx_ring *ring;
604 unsigned int i;
Florian Fainelli2f913072015-09-16 16:47:39 -0700605
606 ec->tx_max_coalesced_frames =
607 bcmgenet_tdma_ring_readl(priv, DESC_INDEX,
608 DMA_MBUF_DONE_THRESH);
Florian Fainelli4a296452015-09-16 16:47:40 -0700609 ec->rx_max_coalesced_frames =
610 bcmgenet_rdma_ring_readl(priv, DESC_INDEX,
611 DMA_MBUF_DONE_THRESH);
612 ec->rx_coalesce_usecs =
613 bcmgenet_rdma_readl(priv, DMA_RING16_TIMEOUT) * 8192 / 1000;
Florian Fainelli2f913072015-09-16 16:47:39 -0700614
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700615 for (i = 0; i < priv->hw_params->rx_queues; i++) {
616 ring = &priv->rx_rings[i];
617 ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
618 }
619 ring = &priv->rx_rings[DESC_INDEX];
620 ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
621
Florian Fainelli2f913072015-09-16 16:47:39 -0700622 return 0;
623}
624
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700625static void bcmgenet_set_rx_coalesce(struct bcmgenet_rx_ring *ring,
626 u32 usecs, u32 pkts)
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700627{
628 struct bcmgenet_priv *priv = ring->priv;
629 unsigned int i = ring->index;
630 u32 reg;
631
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700632 bcmgenet_rdma_ring_writel(priv, i, pkts, DMA_MBUF_DONE_THRESH);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700633
634 reg = bcmgenet_rdma_readl(priv, DMA_RING0_TIMEOUT + i);
635 reg &= ~DMA_TIMEOUT_MASK;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700636 reg |= DIV_ROUND_UP(usecs * 1000, 8192);
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700637 bcmgenet_rdma_writel(priv, reg, DMA_RING0_TIMEOUT + i);
638}
639
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700640static void bcmgenet_set_ring_rx_coalesce(struct bcmgenet_rx_ring *ring,
641 struct ethtool_coalesce *ec)
642{
Tal Gilboa8960b382019-01-31 16:44:48 +0200643 struct dim_cq_moder moder;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700644 u32 usecs, pkts;
645
646 ring->rx_coalesce_usecs = ec->rx_coalesce_usecs;
647 ring->rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
648 usecs = ring->rx_coalesce_usecs;
649 pkts = ring->rx_max_coalesced_frames;
650
651 if (ec->use_adaptive_rx_coalesce && !ring->dim.use_dim) {
Tal Gilboa026a8072018-04-24 13:36:01 +0300652 moder = net_dim_get_def_rx_moderation(ring->dim.dim.mode);
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700653 usecs = moder.usec;
654 pkts = moder.pkts;
655 }
656
657 ring->dim.use_dim = ec->use_adaptive_rx_coalesce;
658 bcmgenet_set_rx_coalesce(ring, usecs, pkts);
659}
660
Florian Fainelli2f913072015-09-16 16:47:39 -0700661static int bcmgenet_set_coalesce(struct net_device *dev,
662 struct ethtool_coalesce *ec)
663{
664 struct bcmgenet_priv *priv = netdev_priv(dev);
665 unsigned int i;
666
Florian Fainelli4a296452015-09-16 16:47:40 -0700667 /* Base system clock is 125Mhz, DMA timeout is this reference clock
668 * divided by 1024, which yields roughly 8.192us, our maximum value
669 * has to fit in the DMA_TIMEOUT_MASK (16 bits)
670 */
Florian Fainelli2f913072015-09-16 16:47:39 -0700671 if (ec->tx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
Florian Fainelli4a296452015-09-16 16:47:40 -0700672 ec->tx_max_coalesced_frames == 0 ||
673 ec->rx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
674 ec->rx_coalesce_usecs > (DMA_TIMEOUT_MASK * 8) + 1)
675 return -EINVAL;
676
677 if (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0)
Florian Fainelli2f913072015-09-16 16:47:39 -0700678 return -EINVAL;
679
680 /* GENET TDMA hardware does not support a configurable timeout, but will
681 * always generate an interrupt either after MBDONE packets have been
Doug Berger556c2cf2017-03-13 17:41:34 -0700682 * transmitted, or when the ring is empty.
Florian Fainelli2f913072015-09-16 16:47:39 -0700683 */
684 if (ec->tx_coalesce_usecs || ec->tx_coalesce_usecs_high ||
Florian Fainelli9f4ca052018-03-22 18:19:33 -0700685 ec->tx_coalesce_usecs_irq || ec->tx_coalesce_usecs_low ||
686 ec->use_adaptive_tx_coalesce)
Florian Fainelli2f913072015-09-16 16:47:39 -0700687 return -EOPNOTSUPP;
688
689 /* Program all TX queues with the same values, as there is no
690 * ethtool knob to do coalescing on a per-queue basis
691 */
692 for (i = 0; i < priv->hw_params->tx_queues; i++)
693 bcmgenet_tdma_ring_writel(priv, i,
694 ec->tx_max_coalesced_frames,
695 DMA_MBUF_DONE_THRESH);
696 bcmgenet_tdma_ring_writel(priv, DESC_INDEX,
697 ec->tx_max_coalesced_frames,
698 DMA_MBUF_DONE_THRESH);
699
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -0700700 for (i = 0; i < priv->hw_params->rx_queues; i++)
701 bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[i], ec);
702 bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[DESC_INDEX], ec);
Florian Fainelli4a296452015-09-16 16:47:40 -0700703
Florian Fainelli2f913072015-09-16 16:47:39 -0700704 return 0;
705}
706
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800707/* standard ethtool support functions. */
708enum bcmgenet_stat_type {
709 BCMGENET_STAT_NETDEV = -1,
710 BCMGENET_STAT_MIB_RX,
711 BCMGENET_STAT_MIB_TX,
712 BCMGENET_STAT_RUNT,
713 BCMGENET_STAT_MISC,
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800714 BCMGENET_STAT_SOFT,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800715};
716
717struct bcmgenet_stats {
718 char stat_string[ETH_GSTRING_LEN];
719 int stat_sizeof;
720 int stat_offset;
721 enum bcmgenet_stat_type type;
722 /* reg offset from UMAC base for misc counters */
723 u16 reg_offset;
724};
725
726#define STAT_NETDEV(m) { \
727 .stat_string = __stringify(m), \
728 .stat_sizeof = sizeof(((struct net_device_stats *)0)->m), \
729 .stat_offset = offsetof(struct net_device_stats, m), \
730 .type = BCMGENET_STAT_NETDEV, \
731}
732
733#define STAT_GENET_MIB(str, m, _type) { \
734 .stat_string = str, \
735 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
736 .stat_offset = offsetof(struct bcmgenet_priv, m), \
737 .type = _type, \
738}
739
740#define STAT_GENET_MIB_RX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_RX)
741#define STAT_GENET_MIB_TX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_TX)
742#define STAT_GENET_RUNT(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_RUNT)
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800743#define STAT_GENET_SOFT_MIB(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_SOFT)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800744
745#define STAT_GENET_MISC(str, m, offset) { \
746 .stat_string = str, \
747 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
748 .stat_offset = offsetof(struct bcmgenet_priv, m), \
749 .type = BCMGENET_STAT_MISC, \
750 .reg_offset = offset, \
751}
752
Florian Fainelli37a30b42017-03-16 10:27:08 -0700753#define STAT_GENET_Q(num) \
754 STAT_GENET_SOFT_MIB("txq" __stringify(num) "_packets", \
755 tx_rings[num].packets), \
756 STAT_GENET_SOFT_MIB("txq" __stringify(num) "_bytes", \
757 tx_rings[num].bytes), \
758 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_bytes", \
759 rx_rings[num].bytes), \
760 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_packets", \
761 rx_rings[num].packets), \
762 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_errors", \
763 rx_rings[num].errors), \
764 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_dropped", \
765 rx_rings[num].dropped)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800766
767/* There is a 0xC gap between the end of RX and beginning of TX stats and then
768 * between the end of TX stats and the beginning of the RX RUNT
769 */
770#define BCMGENET_STAT_OFFSET 0xc
771
772/* Hardware counters must be kept in sync because the order/offset
773 * is important here (order in structure declaration = order in hardware)
774 */
775static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
776 /* general stats */
777 STAT_NETDEV(rx_packets),
778 STAT_NETDEV(tx_packets),
779 STAT_NETDEV(rx_bytes),
780 STAT_NETDEV(tx_bytes),
781 STAT_NETDEV(rx_errors),
782 STAT_NETDEV(tx_errors),
783 STAT_NETDEV(rx_dropped),
784 STAT_NETDEV(tx_dropped),
785 STAT_NETDEV(multicast),
786 /* UniMAC RSV counters */
787 STAT_GENET_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
788 STAT_GENET_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
789 STAT_GENET_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
790 STAT_GENET_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
791 STAT_GENET_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
792 STAT_GENET_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
793 STAT_GENET_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
794 STAT_GENET_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
795 STAT_GENET_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
796 STAT_GENET_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
797 STAT_GENET_MIB_RX("rx_pkts", mib.rx.pkt),
798 STAT_GENET_MIB_RX("rx_bytes", mib.rx.bytes),
799 STAT_GENET_MIB_RX("rx_multicast", mib.rx.mca),
800 STAT_GENET_MIB_RX("rx_broadcast", mib.rx.bca),
801 STAT_GENET_MIB_RX("rx_fcs", mib.rx.fcs),
802 STAT_GENET_MIB_RX("rx_control", mib.rx.cf),
803 STAT_GENET_MIB_RX("rx_pause", mib.rx.pf),
804 STAT_GENET_MIB_RX("rx_unknown", mib.rx.uo),
805 STAT_GENET_MIB_RX("rx_align", mib.rx.aln),
806 STAT_GENET_MIB_RX("rx_outrange", mib.rx.flr),
807 STAT_GENET_MIB_RX("rx_code", mib.rx.cde),
808 STAT_GENET_MIB_RX("rx_carrier", mib.rx.fcr),
809 STAT_GENET_MIB_RX("rx_oversize", mib.rx.ovr),
810 STAT_GENET_MIB_RX("rx_jabber", mib.rx.jbr),
811 STAT_GENET_MIB_RX("rx_mtu_err", mib.rx.mtue),
812 STAT_GENET_MIB_RX("rx_good_pkts", mib.rx.pok),
813 STAT_GENET_MIB_RX("rx_unicast", mib.rx.uc),
814 STAT_GENET_MIB_RX("rx_ppp", mib.rx.ppp),
815 STAT_GENET_MIB_RX("rx_crc", mib.rx.rcrc),
816 /* UniMAC TSV counters */
817 STAT_GENET_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
818 STAT_GENET_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
819 STAT_GENET_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
820 STAT_GENET_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
821 STAT_GENET_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
822 STAT_GENET_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
823 STAT_GENET_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
824 STAT_GENET_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
825 STAT_GENET_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
826 STAT_GENET_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
827 STAT_GENET_MIB_TX("tx_pkts", mib.tx.pkts),
828 STAT_GENET_MIB_TX("tx_multicast", mib.tx.mca),
829 STAT_GENET_MIB_TX("tx_broadcast", mib.tx.bca),
830 STAT_GENET_MIB_TX("tx_pause", mib.tx.pf),
831 STAT_GENET_MIB_TX("tx_control", mib.tx.cf),
832 STAT_GENET_MIB_TX("tx_fcs_err", mib.tx.fcs),
833 STAT_GENET_MIB_TX("tx_oversize", mib.tx.ovr),
834 STAT_GENET_MIB_TX("tx_defer", mib.tx.drf),
835 STAT_GENET_MIB_TX("tx_excess_defer", mib.tx.edf),
836 STAT_GENET_MIB_TX("tx_single_col", mib.tx.scl),
837 STAT_GENET_MIB_TX("tx_multi_col", mib.tx.mcl),
838 STAT_GENET_MIB_TX("tx_late_col", mib.tx.lcl),
839 STAT_GENET_MIB_TX("tx_excess_col", mib.tx.ecl),
840 STAT_GENET_MIB_TX("tx_frags", mib.tx.frg),
841 STAT_GENET_MIB_TX("tx_total_col", mib.tx.ncl),
842 STAT_GENET_MIB_TX("tx_jabber", mib.tx.jbr),
843 STAT_GENET_MIB_TX("tx_bytes", mib.tx.bytes),
844 STAT_GENET_MIB_TX("tx_good_pkts", mib.tx.pok),
845 STAT_GENET_MIB_TX("tx_unicast", mib.tx.uc),
846 /* UniMAC RUNT counters */
847 STAT_GENET_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
848 STAT_GENET_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
849 STAT_GENET_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
850 STAT_GENET_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
851 /* Misc UniMAC counters */
852 STAT_GENET_MISC("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt,
Doug Bergerffff7132017-03-09 16:58:43 -0800853 UMAC_RBUF_OVFL_CNT_V1),
854 STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt,
855 UMAC_RBUF_ERR_CNT_V1),
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800856 STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT),
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800857 STAT_GENET_SOFT_MIB("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
858 STAT_GENET_SOFT_MIB("rx_dma_failed", mib.rx_dma_failed),
859 STAT_GENET_SOFT_MIB("tx_dma_failed", mib.tx_dma_failed),
Florian Fainelli37a30b42017-03-16 10:27:08 -0700860 /* Per TX queues */
861 STAT_GENET_Q(0),
862 STAT_GENET_Q(1),
863 STAT_GENET_Q(2),
864 STAT_GENET_Q(3),
865 STAT_GENET_Q(16),
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800866};
867
868#define BCMGENET_STATS_LEN ARRAY_SIZE(bcmgenet_gstrings_stats)
869
870static void bcmgenet_get_drvinfo(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700871 struct ethtool_drvinfo *info)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800872{
873 strlcpy(info->driver, "bcmgenet", sizeof(info->driver));
874 strlcpy(info->version, "v2.0", sizeof(info->version));
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800875}
876
877static int bcmgenet_get_sset_count(struct net_device *dev, int string_set)
878{
879 switch (string_set) {
880 case ETH_SS_STATS:
881 return BCMGENET_STATS_LEN;
882 default:
883 return -EOPNOTSUPP;
884 }
885}
886
Florian Fainellic91b7f62014-07-23 10:42:12 -0700887static void bcmgenet_get_strings(struct net_device *dev, u32 stringset,
888 u8 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800889{
890 int i;
891
892 switch (stringset) {
893 case ETH_SS_STATS:
894 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
895 memcpy(data + i * ETH_GSTRING_LEN,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700896 bcmgenet_gstrings_stats[i].stat_string,
897 ETH_GSTRING_LEN);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800898 }
899 break;
900 }
901}
902
Doug Bergerffff7132017-03-09 16:58:43 -0800903static u32 bcmgenet_update_stat_misc(struct bcmgenet_priv *priv, u16 offset)
904{
905 u16 new_offset;
906 u32 val;
907
908 switch (offset) {
909 case UMAC_RBUF_OVFL_CNT_V1:
910 if (GENET_IS_V2(priv))
911 new_offset = RBUF_OVFL_CNT_V2;
912 else
913 new_offset = RBUF_OVFL_CNT_V3PLUS;
914
915 val = bcmgenet_rbuf_readl(priv, new_offset);
916 /* clear if overflowed */
917 if (val == ~0)
918 bcmgenet_rbuf_writel(priv, 0, new_offset);
919 break;
920 case UMAC_RBUF_ERR_CNT_V1:
921 if (GENET_IS_V2(priv))
922 new_offset = RBUF_ERR_CNT_V2;
923 else
924 new_offset = RBUF_ERR_CNT_V3PLUS;
925
926 val = bcmgenet_rbuf_readl(priv, new_offset);
927 /* clear if overflowed */
928 if (val == ~0)
929 bcmgenet_rbuf_writel(priv, 0, new_offset);
930 break;
931 default:
932 val = bcmgenet_umac_readl(priv, offset);
933 /* clear if overflowed */
934 if (val == ~0)
935 bcmgenet_umac_writel(priv, 0, offset);
936 break;
937 }
938
939 return val;
940}
941
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800942static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv)
943{
944 int i, j = 0;
945
946 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
947 const struct bcmgenet_stats *s;
948 u8 offset = 0;
949 u32 val = 0;
950 char *p;
951
952 s = &bcmgenet_gstrings_stats[i];
953 switch (s->type) {
954 case BCMGENET_STAT_NETDEV:
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800955 case BCMGENET_STAT_SOFT:
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800956 continue;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800957 case BCMGENET_STAT_RUNT:
Doug Berger1ad3d222017-03-09 16:58:44 -0800958 offset += BCMGENET_STAT_OFFSET;
959 /* fall through */
960 case BCMGENET_STAT_MIB_TX:
961 offset += BCMGENET_STAT_OFFSET;
962 /* fall through */
963 case BCMGENET_STAT_MIB_RX:
Florian Fainellic91b7f62014-07-23 10:42:12 -0700964 val = bcmgenet_umac_readl(priv,
965 UMAC_MIB_START + j + offset);
Doug Berger1ad3d222017-03-09 16:58:44 -0800966 offset = 0; /* Reset Offset */
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800967 break;
968 case BCMGENET_STAT_MISC:
Doug Bergerffff7132017-03-09 16:58:43 -0800969 if (GENET_IS_V1(priv)) {
970 val = bcmgenet_umac_readl(priv, s->reg_offset);
971 /* clear if overflowed */
972 if (val == ~0)
973 bcmgenet_umac_writel(priv, 0,
974 s->reg_offset);
975 } else {
976 val = bcmgenet_update_stat_misc(priv,
977 s->reg_offset);
978 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800979 break;
980 }
981
982 j += s->stat_sizeof;
983 p = (char *)priv + s->stat_offset;
984 *(u32 *)p = val;
985 }
986}
987
988static void bcmgenet_get_ethtool_stats(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700989 struct ethtool_stats *stats,
990 u64 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800991{
992 struct bcmgenet_priv *priv = netdev_priv(dev);
993 int i;
994
995 if (netif_running(dev))
996 bcmgenet_update_mib_counters(priv);
997
998 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
999 const struct bcmgenet_stats *s;
1000 char *p;
1001
1002 s = &bcmgenet_gstrings_stats[i];
1003 if (s->type == BCMGENET_STAT_NETDEV)
1004 p = (char *)&dev->stats;
1005 else
1006 p = (char *)priv;
1007 p += s->stat_offset;
Eric Dumazet6517eb52016-04-15 10:47:52 -07001008 if (sizeof(unsigned long) != sizeof(u32) &&
1009 s->stat_sizeof == sizeof(unsigned long))
1010 data[i] = *(unsigned long *)p;
1011 else
1012 data[i] = *(u32 *)p;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001013 }
1014}
1015
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001016static void bcmgenet_eee_enable_set(struct net_device *dev, bool enable)
1017{
1018 struct bcmgenet_priv *priv = netdev_priv(dev);
1019 u32 off = priv->hw_params->tbuf_offset + TBUF_ENERGY_CTRL;
1020 u32 reg;
1021
1022 if (enable && !priv->clk_eee_enabled) {
1023 clk_prepare_enable(priv->clk_eee);
1024 priv->clk_eee_enabled = true;
1025 }
1026
1027 reg = bcmgenet_umac_readl(priv, UMAC_EEE_CTRL);
1028 if (enable)
1029 reg |= EEE_EN;
1030 else
1031 reg &= ~EEE_EN;
1032 bcmgenet_umac_writel(priv, reg, UMAC_EEE_CTRL);
1033
1034 /* Enable EEE and switch to a 27Mhz clock automatically */
Florian Fainelli69d2ea92017-08-29 12:25:31 -07001035 reg = bcmgenet_readl(priv->base + off);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001036 if (enable)
1037 reg |= TBUF_EEE_EN | TBUF_PM_EN;
1038 else
1039 reg &= ~(TBUF_EEE_EN | TBUF_PM_EN);
Florian Fainelli69d2ea92017-08-29 12:25:31 -07001040 bcmgenet_writel(reg, priv->base + off);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001041
1042 /* Do the same for thing for RBUF */
1043 reg = bcmgenet_rbuf_readl(priv, RBUF_ENERGY_CTRL);
1044 if (enable)
1045 reg |= RBUF_EEE_EN | RBUF_PM_EN;
1046 else
1047 reg &= ~(RBUF_EEE_EN | RBUF_PM_EN);
1048 bcmgenet_rbuf_writel(priv, reg, RBUF_ENERGY_CTRL);
1049
1050 if (!enable && priv->clk_eee_enabled) {
1051 clk_disable_unprepare(priv->clk_eee);
1052 priv->clk_eee_enabled = false;
1053 }
1054
1055 priv->eee.eee_enabled = enable;
1056 priv->eee.eee_active = enable;
1057}
1058
1059static int bcmgenet_get_eee(struct net_device *dev, struct ethtool_eee *e)
1060{
1061 struct bcmgenet_priv *priv = netdev_priv(dev);
1062 struct ethtool_eee *p = &priv->eee;
1063
1064 if (GENET_IS_V1(priv))
1065 return -EOPNOTSUPP;
1066
Doug Berger6c97f012017-10-25 15:04:19 -07001067 if (!dev->phydev)
1068 return -ENODEV;
1069
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001070 e->eee_enabled = p->eee_enabled;
1071 e->eee_active = p->eee_active;
1072 e->tx_lpi_timer = bcmgenet_umac_readl(priv, UMAC_EEE_LPI_TIMER);
1073
Doug Berger6c97f012017-10-25 15:04:19 -07001074 return phy_ethtool_get_eee(dev->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001075}
1076
1077static int bcmgenet_set_eee(struct net_device *dev, struct ethtool_eee *e)
1078{
1079 struct bcmgenet_priv *priv = netdev_priv(dev);
1080 struct ethtool_eee *p = &priv->eee;
1081 int ret = 0;
1082
1083 if (GENET_IS_V1(priv))
1084 return -EOPNOTSUPP;
1085
Doug Berger6c97f012017-10-25 15:04:19 -07001086 if (!dev->phydev)
1087 return -ENODEV;
1088
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001089 p->eee_enabled = e->eee_enabled;
1090
1091 if (!p->eee_enabled) {
1092 bcmgenet_eee_enable_set(dev, false);
1093 } else {
Doug Berger6c97f012017-10-25 15:04:19 -07001094 ret = phy_init_eee(dev->phydev, 0);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001095 if (ret) {
1096 netif_err(priv, hw, dev, "EEE initialization failed\n");
1097 return ret;
1098 }
1099
1100 bcmgenet_umac_writel(priv, e->tx_lpi_timer, UMAC_EEE_LPI_TIMER);
1101 bcmgenet_eee_enable_set(dev, true);
1102 }
1103
Doug Berger6c97f012017-10-25 15:04:19 -07001104 return phy_ethtool_set_eee(dev->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001105}
1106
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001107/* standard ethtool support functions. */
Julia Lawall70591ab2016-08-31 09:30:45 +02001108static const struct ethtool_ops bcmgenet_ethtool_ops = {
Edwin Chan89316fa2017-03-09 16:58:49 -08001109 .begin = bcmgenet_begin,
1110 .complete = bcmgenet_complete,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001111 .get_strings = bcmgenet_get_strings,
1112 .get_sset_count = bcmgenet_get_sset_count,
1113 .get_ethtool_stats = bcmgenet_get_ethtool_stats,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001114 .get_drvinfo = bcmgenet_get_drvinfo,
1115 .get_link = ethtool_op_get_link,
1116 .get_msglevel = bcmgenet_get_msglevel,
1117 .set_msglevel = bcmgenet_set_msglevel,
Florian Fainelli06ba8372014-07-21 15:29:29 -07001118 .get_wol = bcmgenet_get_wol,
1119 .set_wol = bcmgenet_set_wol,
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001120 .get_eee = bcmgenet_get_eee,
1121 .set_eee = bcmgenet_set_eee,
Florian Fainelli016e7702016-11-15 10:06:38 -08001122 .nway_reset = phy_ethtool_nway_reset,
Florian Fainelli2f913072015-09-16 16:47:39 -07001123 .get_coalesce = bcmgenet_get_coalesce,
1124 .set_coalesce = bcmgenet_set_coalesce,
Philippe Reynesfa92bf02016-09-26 22:31:57 +02001125 .get_link_ksettings = bcmgenet_get_link_ksettings,
1126 .set_link_ksettings = bcmgenet_set_link_ksettings,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001127};
1128
1129/* Power down the unimac, based on mode. */
Florian Fainellica8cf342015-03-23 15:09:51 -07001130static int bcmgenet_power_down(struct bcmgenet_priv *priv,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001131 enum bcmgenet_power_mode mode)
1132{
Florian Fainellica8cf342015-03-23 15:09:51 -07001133 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001134 u32 reg;
1135
1136 switch (mode) {
1137 case GENET_POWER_CABLE_SENSE:
Doug Berger6c97f012017-10-25 15:04:19 -07001138 phy_detach(priv->dev->phydev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001139 break;
1140
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001141 case GENET_POWER_WOL_MAGIC:
Florian Fainellica8cf342015-03-23 15:09:51 -07001142 ret = bcmgenet_wol_power_down_cfg(priv, mode);
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001143 break;
1144
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001145 case GENET_POWER_PASSIVE:
1146 /* Power down LED */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001147 if (priv->hw_params->flags & GENET_HAS_EXT) {
1148 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
Doug Berger42138082017-03-13 17:41:42 -07001149 if (GENET_IS_V5(priv))
1150 reg |= EXT_PWR_DOWN_PHY_EN |
1151 EXT_PWR_DOWN_PHY_RD |
1152 EXT_PWR_DOWN_PHY_SD |
1153 EXT_PWR_DOWN_PHY_RX |
1154 EXT_PWR_DOWN_PHY_TX |
1155 EXT_IDDQ_GLBL_PWR;
1156 else
1157 reg |= EXT_PWR_DOWN_PHY;
1158
1159 reg |= (EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001160 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
Florian Fainellia642c4f2015-03-23 15:09:56 -07001161
1162 bcmgenet_phy_power_set(priv->dev, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001163 }
1164 break;
1165 default:
1166 break;
1167 }
Florian Fainellica8cf342015-03-23 15:09:51 -07001168
YueHaibing0db55092018-11-08 02:08:43 +00001169 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001170}
1171
1172static void bcmgenet_power_up(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001173 enum bcmgenet_power_mode mode)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001174{
1175 u32 reg;
1176
1177 if (!(priv->hw_params->flags & GENET_HAS_EXT))
1178 return;
1179
1180 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
1181
1182 switch (mode) {
1183 case GENET_POWER_PASSIVE:
Doug Berger42138082017-03-13 17:41:42 -07001184 reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
1185 if (GENET_IS_V5(priv)) {
1186 reg &= ~(EXT_PWR_DOWN_PHY_EN |
1187 EXT_PWR_DOWN_PHY_RD |
1188 EXT_PWR_DOWN_PHY_SD |
1189 EXT_PWR_DOWN_PHY_RX |
1190 EXT_PWR_DOWN_PHY_TX |
1191 EXT_IDDQ_GLBL_PWR);
1192 reg |= EXT_PHY_RESET;
1193 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1194 mdelay(1);
1195
1196 reg &= ~EXT_PHY_RESET;
1197 } else {
1198 reg &= ~EXT_PWR_DOWN_PHY;
1199 reg |= EXT_PWR_DN_EN_LD;
1200 }
1201 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1202 bcmgenet_phy_power_set(priv->dev, true);
Doug Berger42138082017-03-13 17:41:42 -07001203 break;
1204
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001205 case GENET_POWER_CABLE_SENSE:
1206 /* enable APD */
Doug Berger42138082017-03-13 17:41:42 -07001207 if (!GENET_IS_V5(priv)) {
1208 reg |= EXT_PWR_DN_EN_LD;
1209 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1210 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001211 break;
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001212 case GENET_POWER_WOL_MAGIC:
1213 bcmgenet_wol_power_up_cfg(priv, mode);
1214 return;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001215 default:
1216 break;
1217 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001218}
1219
1220/* ioctl handle special commands that are not present in ethtool. */
1221static int bcmgenet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1222{
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001223 if (!netif_running(dev))
1224 return -EINVAL;
1225
Doug Berger6c97f012017-10-25 15:04:19 -07001226 if (!dev->phydev)
Doug Berger54fecff2017-03-13 17:41:39 -07001227 return -ENODEV;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001228
Doug Berger6c97f012017-10-25 15:04:19 -07001229 return phy_mii_ioctl(dev->phydev, rq, cmd);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001230}
1231
1232static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv,
1233 struct bcmgenet_tx_ring *ring)
1234{
1235 struct enet_cb *tx_cb_ptr;
1236
1237 tx_cb_ptr = ring->cbs;
1238 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
Petri Gynther014012a2015-02-23 11:00:45 -08001239
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001240 /* Advancing local write pointer */
1241 if (ring->write_ptr == ring->end_ptr)
1242 ring->write_ptr = ring->cb_ptr;
1243 else
1244 ring->write_ptr++;
1245
1246 return tx_cb_ptr;
1247}
1248
Doug Berger876dbad2017-07-14 16:12:09 -07001249static struct enet_cb *bcmgenet_put_txcb(struct bcmgenet_priv *priv,
1250 struct bcmgenet_tx_ring *ring)
1251{
1252 struct enet_cb *tx_cb_ptr;
1253
1254 tx_cb_ptr = ring->cbs;
1255 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
1256
1257 /* Rewinding local write pointer */
1258 if (ring->write_ptr == ring->cb_ptr)
1259 ring->write_ptr = ring->end_ptr;
1260 else
1261 ring->write_ptr--;
1262
1263 return tx_cb_ptr;
1264}
1265
Petri Gynther4055eae2015-03-25 12:35:16 -07001266static inline void bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring *ring)
1267{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001268 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001269 INTRL2_CPU_MASK_SET);
1270}
1271
1272static inline void bcmgenet_rx_ring16_int_enable(struct bcmgenet_rx_ring *ring)
1273{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001274 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001275 INTRL2_CPU_MASK_CLEAR);
1276}
1277
1278static inline void bcmgenet_rx_ring_int_disable(struct bcmgenet_rx_ring *ring)
1279{
1280 bcmgenet_intrl2_1_writel(ring->priv,
1281 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1282 INTRL2_CPU_MASK_SET);
1283}
1284
1285static inline void bcmgenet_rx_ring_int_enable(struct bcmgenet_rx_ring *ring)
1286{
1287 bcmgenet_intrl2_1_writel(ring->priv,
1288 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1289 INTRL2_CPU_MASK_CLEAR);
1290}
1291
Petri Gynther9dbac282015-03-25 12:35:10 -07001292static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001293{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001294 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001295 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001296}
1297
Petri Gynther9dbac282015-03-25 12:35:10 -07001298static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001299{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001300 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001301 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001302}
1303
Petri Gynther9dbac282015-03-25 12:35:10 -07001304static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001305{
Petri Gynther9dbac282015-03-25 12:35:10 -07001306 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001307 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001308}
1309
Petri Gynther9dbac282015-03-25 12:35:10 -07001310static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001311{
Petri Gynther9dbac282015-03-25 12:35:10 -07001312 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001313 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001314}
1315
Doug Bergerf48bed12017-07-14 16:12:10 -07001316/* Simple helper to free a transmit control block's resources
1317 * Returns an skb when the last transmit control block associated with the
1318 * skb is freed. The skb should be freed by the caller if necessary.
1319 */
1320static struct sk_buff *bcmgenet_free_tx_cb(struct device *dev,
1321 struct enet_cb *cb)
1322{
1323 struct sk_buff *skb;
1324
1325 skb = cb->skb;
1326
1327 if (skb) {
1328 cb->skb = NULL;
1329 if (cb == GENET_CB(skb)->first_cb)
1330 dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
1331 dma_unmap_len(cb, dma_len),
1332 DMA_TO_DEVICE);
1333 else
1334 dma_unmap_page(dev, dma_unmap_addr(cb, dma_addr),
1335 dma_unmap_len(cb, dma_len),
1336 DMA_TO_DEVICE);
1337 dma_unmap_addr_set(cb, dma_addr, 0);
1338
1339 if (cb == GENET_CB(skb)->last_cb)
1340 return skb;
1341
1342 } else if (dma_unmap_addr(cb, dma_addr)) {
1343 dma_unmap_page(dev,
1344 dma_unmap_addr(cb, dma_addr),
1345 dma_unmap_len(cb, dma_len),
1346 DMA_TO_DEVICE);
1347 dma_unmap_addr_set(cb, dma_addr, 0);
1348 }
1349
Wei Yongjun335ab8b2018-03-28 12:51:19 +00001350 return NULL;
Doug Bergerf48bed12017-07-14 16:12:10 -07001351}
1352
1353/* Simple helper to free a receive control block's resources */
1354static struct sk_buff *bcmgenet_free_rx_cb(struct device *dev,
1355 struct enet_cb *cb)
1356{
1357 struct sk_buff *skb;
1358
1359 skb = cb->skb;
1360 cb->skb = NULL;
1361
1362 if (dma_unmap_addr(cb, dma_addr)) {
1363 dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
1364 dma_unmap_len(cb, dma_len), DMA_FROM_DEVICE);
1365 dma_unmap_addr_set(cb, dma_addr, 0);
1366 }
1367
1368 return skb;
1369}
1370
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001371/* Unlocked version of the reclaim routine */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001372static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
1373 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001374{
1375 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther66d06752015-03-04 14:30:01 -08001376 unsigned int txbds_processed = 0;
Doug Bergerf48bed12017-07-14 16:12:10 -07001377 unsigned int bytes_compl = 0;
1378 unsigned int pkts_compl = 0;
1379 unsigned int txbds_ready;
1380 unsigned int c_index;
1381 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001382
Doug Bergerd5810ca2017-03-13 17:41:37 -07001383 /* Clear status before servicing to reduce spurious interrupts */
1384 if (ring->index == DESC_INDEX)
1385 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_TXDMA_DONE,
1386 INTRL2_CPU_CLEAR);
1387 else
1388 bcmgenet_intrl2_1_writel(priv, (1 << ring->index),
1389 INTRL2_CPU_CLEAR);
1390
Brian Norris7fc527f2014-07-29 14:34:14 -07001391 /* Compute how many buffers are transmitted since last xmit call */
Doug Bergerc298ede2017-03-13 17:41:33 -07001392 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX)
1393 & DMA_C_INDEX_MASK;
1394 txbds_ready = (c_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001395
1396 netif_dbg(priv, tx_done, dev,
Petri Gynther66d06752015-03-04 14:30:01 -08001397 "%s ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
1398 __func__, ring->index, ring->c_index, c_index, txbds_ready);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001399
1400 /* Reclaim transmitted buffers */
Petri Gynther66d06752015-03-04 14:30:01 -08001401 while (txbds_processed < txbds_ready) {
Doug Bergerf48bed12017-07-14 16:12:10 -07001402 skb = bcmgenet_free_tx_cb(&priv->pdev->dev,
1403 &priv->tx_cbs[ring->clean_ptr]);
1404 if (skb) {
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001405 pkts_compl++;
Doug Bergerf48bed12017-07-14 16:12:10 -07001406 bytes_compl += GENET_CB(skb)->bytes_sent;
Florian Fainellid4fec852017-08-24 15:56:29 -07001407 dev_consume_skb_any(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001408 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001409
Petri Gynther66d06752015-03-04 14:30:01 -08001410 txbds_processed++;
1411 if (likely(ring->clean_ptr < ring->end_ptr))
1412 ring->clean_ptr++;
1413 else
1414 ring->clean_ptr = ring->cb_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001415 }
1416
Petri Gynther66d06752015-03-04 14:30:01 -08001417 ring->free_bds += txbds_processed;
Doug Bergerc4d453d2017-03-13 17:41:38 -07001418 ring->c_index = c_index;
Petri Gynther66d06752015-03-04 14:30:01 -08001419
Florian Fainelli37a30b42017-03-16 10:27:08 -07001420 ring->packets += pkts_compl;
1421 ring->bytes += bytes_compl;
Petri Gynther55868122016-03-24 11:27:20 -07001422
Doug Berger6d22fe12017-03-09 16:58:50 -08001423 netdev_tx_completed_queue(netdev_get_tx_queue(dev, ring->queue),
1424 pkts_compl, bytes_compl);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001425
Doug Bergerc4d453d2017-03-13 17:41:38 -07001426 return txbds_processed;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001427}
1428
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001429static unsigned int bcmgenet_tx_reclaim(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001430 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001431{
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001432 unsigned int released;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001433
Doug Bergerb0447ec2017-10-25 15:04:17 -07001434 spin_lock_bh(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001435 released = __bcmgenet_tx_reclaim(dev, ring);
Doug Bergerb0447ec2017-10-25 15:04:17 -07001436 spin_unlock_bh(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001437
1438 return released;
1439}
1440
1441static int bcmgenet_tx_poll(struct napi_struct *napi, int budget)
1442{
1443 struct bcmgenet_tx_ring *ring =
1444 container_of(napi, struct bcmgenet_tx_ring, napi);
1445 unsigned int work_done = 0;
Doug Berger6d22fe12017-03-09 16:58:50 -08001446 struct netdev_queue *txq;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001447
Doug Bergerb0447ec2017-10-25 15:04:17 -07001448 spin_lock(&ring->lock);
Doug Berger6d22fe12017-03-09 16:58:50 -08001449 work_done = __bcmgenet_tx_reclaim(ring->priv->dev, ring);
1450 if (ring->free_bds > (MAX_SKB_FRAGS + 1)) {
1451 txq = netdev_get_tx_queue(ring->priv->dev, ring->queue);
1452 netif_tx_wake_queue(txq);
1453 }
Doug Bergerb0447ec2017-10-25 15:04:17 -07001454 spin_unlock(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001455
1456 if (work_done == 0) {
1457 napi_complete(napi);
Petri Gynther9dbac282015-03-25 12:35:10 -07001458 ring->int_enable(ring);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001459
1460 return 0;
1461 }
1462
1463 return budget;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001464}
1465
1466static void bcmgenet_tx_reclaim_all(struct net_device *dev)
1467{
1468 struct bcmgenet_priv *priv = netdev_priv(dev);
1469 int i;
1470
1471 if (netif_is_multiqueue(dev)) {
1472 for (i = 0; i < priv->hw_params->tx_queues; i++)
1473 bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
1474 }
1475
1476 bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
1477}
1478
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001479/* Reallocate the SKB to put enough headroom in front of it and insert
1480 * the transmit checksum offsets in the descriptors
1481 */
Petri Gyntherbc233332014-10-01 11:30:01 -07001482static struct sk_buff *bcmgenet_put_tx_csum(struct net_device *dev,
1483 struct sk_buff *skb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001484{
1485 struct status_64 *status = NULL;
1486 struct sk_buff *new_skb;
1487 u16 offset;
1488 u8 ip_proto;
Florian Fainelli6f894212018-04-02 15:58:55 -07001489 __be16 ip_ver;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001490 u32 tx_csum_info;
1491
1492 if (unlikely(skb_headroom(skb) < sizeof(*status))) {
1493 /* If 64 byte status block enabled, must make sure skb has
1494 * enough headroom for us to insert 64B status block.
1495 */
1496 new_skb = skb_realloc_headroom(skb, sizeof(*status));
1497 dev_kfree_skb(skb);
1498 if (!new_skb) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001499 dev->stats.tx_dropped++;
Petri Gyntherbc233332014-10-01 11:30:01 -07001500 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001501 }
1502 skb = new_skb;
1503 }
1504
1505 skb_push(skb, sizeof(*status));
1506 status = (struct status_64 *)skb->data;
1507
1508 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Florian Fainelli6f894212018-04-02 15:58:55 -07001509 ip_ver = skb->protocol;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001510 switch (ip_ver) {
Florian Fainelli6f894212018-04-02 15:58:55 -07001511 case htons(ETH_P_IP):
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001512 ip_proto = ip_hdr(skb)->protocol;
1513 break;
Florian Fainelli6f894212018-04-02 15:58:55 -07001514 case htons(ETH_P_IPV6):
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001515 ip_proto = ipv6_hdr(skb)->nexthdr;
1516 break;
1517 default:
Petri Gyntherbc233332014-10-01 11:30:01 -07001518 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001519 }
1520
1521 offset = skb_checksum_start_offset(skb) - sizeof(*status);
1522 tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
1523 (offset + skb->csum_offset);
1524
1525 /* Set the length valid bit for TCP and UDP and just set
1526 * the special UDP flag for IPv4, else just set to 0.
1527 */
1528 if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
1529 tx_csum_info |= STATUS_TX_CSUM_LV;
Florian Fainelli6f894212018-04-02 15:58:55 -07001530 if (ip_proto == IPPROTO_UDP &&
1531 ip_ver == htons(ETH_P_IP))
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001532 tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
Florian Fainelli8900ea572014-07-23 10:42:14 -07001533 } else {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001534 tx_csum_info = 0;
Florian Fainelli8900ea572014-07-23 10:42:14 -07001535 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001536
1537 status->tx_csum_info = tx_csum_info;
1538 }
1539
Petri Gyntherbc233332014-10-01 11:30:01 -07001540 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001541}
1542
1543static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
1544{
1545 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Berger876dbad2017-07-14 16:12:09 -07001546 struct device *kdev = &priv->pdev->dev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001547 struct bcmgenet_tx_ring *ring = NULL;
Doug Berger876dbad2017-07-14 16:12:09 -07001548 struct enet_cb *tx_cb_ptr;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001549 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001550 int nr_frags, index;
Doug Berger876dbad2017-07-14 16:12:09 -07001551 dma_addr_t mapping;
1552 unsigned int size;
1553 skb_frag_t *frag;
1554 u32 len_stat;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001555 int ret;
1556 int i;
1557
1558 index = skb_get_queue_mapping(skb);
1559 /* Mapping strategy:
1560 * queue_mapping = 0, unclassified, packet xmited through ring16
1561 * queue_mapping = 1, goes to ring 0. (highest priority queue
1562 * queue_mapping = 2, goes to ring 1.
1563 * queue_mapping = 3, goes to ring 2.
1564 * queue_mapping = 4, goes to ring 3.
1565 */
1566 if (index == 0)
1567 index = DESC_INDEX;
1568 else
1569 index -= 1;
1570
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001571 ring = &priv->tx_rings[index];
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001572 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001573
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07001574 nr_frags = skb_shinfo(skb)->nr_frags;
1575
Doug Bergerb0447ec2017-10-25 15:04:17 -07001576 spin_lock(&ring->lock);
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07001577 if (ring->free_bds <= (nr_frags + 1)) {
1578 if (!netif_tx_queue_stopped(txq)) {
1579 netif_tx_stop_queue(txq);
1580 netdev_err(dev,
1581 "%s: tx ring %d full when queue %d awake\n",
1582 __func__, index, ring->queue);
1583 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001584 ret = NETDEV_TX_BUSY;
1585 goto out;
1586 }
1587
Florian Fainelli474ea9c2014-07-22 11:01:52 -07001588 if (skb_padto(skb, ETH_ZLEN)) {
1589 ret = NETDEV_TX_OK;
1590 goto out;
1591 }
1592
Petri Gynther55868122016-03-24 11:27:20 -07001593 /* Retain how many bytes will be sent on the wire, without TSB inserted
1594 * by transmit checksum offload
1595 */
1596 GENET_CB(skb)->bytes_sent = skb->len;
1597
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001598 /* set the SKB transmit checksum */
1599 if (priv->desc_64b_en) {
Petri Gyntherbc233332014-10-01 11:30:01 -07001600 skb = bcmgenet_put_tx_csum(dev, skb);
1601 if (!skb) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001602 ret = NETDEV_TX_OK;
1603 goto out;
1604 }
1605 }
1606
Doug Berger876dbad2017-07-14 16:12:09 -07001607 for (i = 0; i <= nr_frags; i++) {
1608 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001609
Gustavo A. R. Silva4fa112f2017-10-26 07:16:01 -05001610 BUG_ON(!tx_cb_ptr);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001611
Doug Berger876dbad2017-07-14 16:12:09 -07001612 if (!i) {
1613 /* Transmit single SKB or head of fragment list */
Doug Bergerf48bed12017-07-14 16:12:10 -07001614 GENET_CB(skb)->first_cb = tx_cb_ptr;
Doug Berger876dbad2017-07-14 16:12:09 -07001615 size = skb_headlen(skb);
1616 mapping = dma_map_single(kdev, skb->data, size,
1617 DMA_TO_DEVICE);
1618 } else {
1619 /* xmit fragment */
Doug Berger876dbad2017-07-14 16:12:09 -07001620 frag = &skb_shinfo(skb)->frags[i - 1];
1621 size = skb_frag_size(frag);
1622 mapping = skb_frag_dma_map(kdev, frag, 0, size,
1623 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001624 }
Doug Berger876dbad2017-07-14 16:12:09 -07001625
1626 ret = dma_mapping_error(kdev, mapping);
1627 if (ret) {
1628 priv->mib.tx_dma_failed++;
1629 netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
1630 ret = NETDEV_TX_OK;
1631 goto out_unmap_frags;
1632 }
1633 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
1634 dma_unmap_len_set(tx_cb_ptr, dma_len, size);
1635
Doug Bergerf48bed12017-07-14 16:12:10 -07001636 tx_cb_ptr->skb = skb;
1637
Doug Berger876dbad2017-07-14 16:12:09 -07001638 len_stat = (size << DMA_BUFLENGTH_SHIFT) |
1639 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT);
1640
1641 if (!i) {
1642 len_stat |= DMA_TX_APPEND_CRC | DMA_SOP;
1643 if (skb->ip_summed == CHECKSUM_PARTIAL)
1644 len_stat |= DMA_TX_DO_CSUM;
1645 }
1646 if (i == nr_frags)
1647 len_stat |= DMA_EOP;
1648
1649 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, len_stat);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001650 }
1651
Doug Bergerf48bed12017-07-14 16:12:10 -07001652 GENET_CB(skb)->last_cb = tx_cb_ptr;
Florian Fainellid03825f2014-03-20 10:53:21 -07001653 skb_tx_timestamp(skb);
1654
Florian Fainelliae67bf02015-03-13 12:11:06 -07001655 /* Decrement total BD count and advance our write pointer */
1656 ring->free_bds -= nr_frags + 1;
1657 ring->prod_index += nr_frags + 1;
1658 ring->prod_index &= DMA_P_INDEX_MASK;
1659
Petri Gynthere178c8c2016-04-09 00:20:36 -07001660 netdev_tx_sent_queue(txq, GENET_CB(skb)->bytes_sent);
1661
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001662 if (ring->free_bds <= (MAX_SKB_FRAGS + 1))
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001663 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001664
Florian Westphal6b16f9e2019-04-01 16:42:14 +02001665 if (!netdev_xmit_more() || netif_xmit_stopped(txq))
Florian Fainelliddd0ca52015-03-13 12:11:07 -07001666 /* Packets are ready, update producer index */
1667 bcmgenet_tdma_ring_writel(priv, ring->index,
1668 ring->prod_index, TDMA_PROD_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001669out:
Doug Bergerb0447ec2017-10-25 15:04:17 -07001670 spin_unlock(&ring->lock);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001671
1672 return ret;
Doug Berger876dbad2017-07-14 16:12:09 -07001673
1674out_unmap_frags:
1675 /* Back up for failed control block mapping */
1676 bcmgenet_put_txcb(priv, ring);
1677
1678 /* Unmap successfully mapped control blocks */
1679 while (i-- > 0) {
1680 tx_cb_ptr = bcmgenet_put_txcb(priv, ring);
Doug Bergerf48bed12017-07-14 16:12:10 -07001681 bcmgenet_free_tx_cb(kdev, tx_cb_ptr);
Doug Berger876dbad2017-07-14 16:12:09 -07001682 }
1683
1684 dev_kfree_skb(skb);
1685 goto out;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001686}
1687
Petri Gyntherd6707be2015-03-12 15:48:00 -07001688static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
1689 struct enet_cb *cb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001690{
1691 struct device *kdev = &priv->pdev->dev;
1692 struct sk_buff *skb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001693 struct sk_buff *rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001694 dma_addr_t mapping;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001695
Petri Gyntherd6707be2015-03-12 15:48:00 -07001696 /* Allocate a new Rx skb */
Florian Fainellic91b7f62014-07-23 10:42:12 -07001697 skb = netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT);
Petri Gyntherd6707be2015-03-12 15:48:00 -07001698 if (!skb) {
1699 priv->mib.alloc_rx_buff_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001700 netif_err(priv, rx_err, priv->dev,
Petri Gyntherd6707be2015-03-12 15:48:00 -07001701 "%s: Rx skb allocation failed\n", __func__);
1702 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001703 }
1704
Petri Gyntherd6707be2015-03-12 15:48:00 -07001705 /* DMA-map the new Rx skb */
1706 mapping = dma_map_single(kdev, skb->data, priv->rx_buf_len,
1707 DMA_FROM_DEVICE);
1708 if (dma_mapping_error(kdev, mapping)) {
1709 priv->mib.rx_dma_failed++;
1710 dev_kfree_skb_any(skb);
1711 netif_err(priv, rx_err, priv->dev,
1712 "%s: Rx skb DMA mapping failed\n", __func__);
1713 return NULL;
1714 }
1715
1716 /* Grab the current Rx skb from the ring and DMA-unmap it */
Doug Bergerf48bed12017-07-14 16:12:10 -07001717 rx_skb = bcmgenet_free_rx_cb(kdev, cb);
Petri Gyntherd6707be2015-03-12 15:48:00 -07001718
1719 /* Put the new Rx skb on the ring */
1720 cb->skb = skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001721 dma_unmap_addr_set(cb, dma_addr, mapping);
Doug Bergerf48bed12017-07-14 16:12:10 -07001722 dma_unmap_len_set(cb, dma_len, priv->rx_buf_len);
Petri Gynther8ac467e2015-03-09 13:40:00 -07001723 dmadesc_set_addr(priv, cb->bd_addr, mapping);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001724
Petri Gyntherd6707be2015-03-12 15:48:00 -07001725 /* Return the current Rx skb to caller */
1726 return rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001727}
1728
1729/* bcmgenet_desc_rx - descriptor based rx process.
1730 * this could be called from bottom half, or from NAPI polling method.
1731 */
Petri Gynther4055eae2015-03-25 12:35:16 -07001732static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001733 unsigned int budget)
1734{
Petri Gynther4055eae2015-03-25 12:35:16 -07001735 struct bcmgenet_priv *priv = ring->priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001736 struct net_device *dev = priv->dev;
1737 struct enet_cb *cb;
1738 struct sk_buff *skb;
1739 u32 dma_length_status;
1740 unsigned long dma_flag;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001741 int len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001742 unsigned int rxpktprocessed = 0, rxpkttoprocess;
Florian Fainelli9f4ca052018-03-22 18:19:33 -07001743 unsigned int bytes_processed = 0;
Doug Bergerd5810ca2017-03-13 17:41:37 -07001744 unsigned int p_index, mask;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001745 unsigned int discards;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001746 unsigned int chksum_ok = 0;
1747
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;
Florian Fainelli164d4f22014-07-23 10:42:13 -07001797
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001798 status = (struct status_64 *)skb->data;
1799 dma_length_status = status->length_status;
1800 }
1801
1802 /* DMA flags and length are still valid no matter how
1803 * we got the Receive Status Vector (64B RSB or register)
1804 */
1805 dma_flag = dma_length_status & 0xffff;
1806 len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
1807
1808 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001809 "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
Petri Gynther8ac467e2015-03-09 13:40:00 -07001810 __func__, p_index, ring->c_index,
1811 ring->read_ptr, dma_length_status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001812
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001813 if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
1814 netif_err(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001815 "dropping fragmented packet!\n");
Florian Fainelli37a30b42017-03-16 10:27:08 -07001816 ring->errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001817 dev_kfree_skb_any(skb);
1818 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001819 }
Petri Gyntherd6707be2015-03-12 15:48:00 -07001820
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001821 /* report errors */
1822 if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
1823 DMA_RX_OV |
1824 DMA_RX_NO |
1825 DMA_RX_LG |
1826 DMA_RX_RXER))) {
1827 netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001828 (unsigned int)dma_flag);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001829 if (dma_flag & DMA_RX_CRC_ERROR)
1830 dev->stats.rx_crc_errors++;
1831 if (dma_flag & DMA_RX_OV)
1832 dev->stats.rx_over_errors++;
1833 if (dma_flag & DMA_RX_NO)
1834 dev->stats.rx_frame_errors++;
1835 if (dma_flag & DMA_RX_LG)
1836 dev->stats.rx_length_errors++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001837 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001838 dev_kfree_skb_any(skb);
1839 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001840 } /* error packet */
1841
1842 chksum_ok = (dma_flag & priv->dma_rx_chk_bit) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001843 priv->desc_rxchk_en;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001844
1845 skb_put(skb, len);
1846 if (priv->desc_64b_en) {
1847 skb_pull(skb, 64);
1848 len -= 64;
1849 }
1850
1851 if (likely(chksum_ok))
1852 skb->ip_summed = CHECKSUM_UNNECESSARY;
1853
1854 /* 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);
1998 udelay(2);
1999 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002000}
2001
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002002static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
2003{
2004 /* Mask all interrupts.*/
2005 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
2006 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002007 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
2008 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002009}
2010
Florian Fainelli37850e32015-10-17 14:22:46 -07002011static void bcmgenet_link_intr_enable(struct bcmgenet_priv *priv)
2012{
2013 u32 int0_enable = 0;
2014
2015 /* Monitor cable plug/unplugged event for internal PHY, external PHY
2016 * and MoCA PHY
2017 */
2018 if (priv->internal_phy) {
2019 int0_enable |= UMAC_IRQ_LINK_EVENT;
2020 } 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 */
2578 bcmgenet_rdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
2579
2580 /* Initialize Rx queues */
2581 ret = bcmgenet_init_rx_queues(priv->dev);
2582 if (ret) {
2583 netdev_err(priv->dev, "failed to initialize Rx queues\n");
2584 bcmgenet_free_rx_buffers(priv);
2585 kfree(priv->rx_cbs);
2586 kfree(priv->tx_cbs);
2587 return ret;
2588 }
2589
2590 /* Init tDma */
2591 bcmgenet_tdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
2592
Petri Gynther16c6d662015-02-23 11:00:45 -08002593 /* Initialize Tx queues */
2594 bcmgenet_init_tx_queues(priv->dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002595
2596 return 0;
2597}
2598
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002599/* Interrupt bottom half */
2600static void bcmgenet_irq_task(struct work_struct *work)
2601{
Doug Berger07c52d62017-03-09 16:58:47 -08002602 unsigned int status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002603 struct bcmgenet_priv *priv = container_of(
2604 work, struct bcmgenet_priv, bcmgenet_irq_work);
2605
2606 netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
2607
Doug Bergerb0447ec2017-10-25 15:04:17 -07002608 spin_lock_irq(&priv->lock);
Doug Berger07c52d62017-03-09 16:58:47 -08002609 status = priv->irq0_stat;
2610 priv->irq0_stat = 0;
Doug Bergerb0447ec2017-10-25 15:04:17 -07002611 spin_unlock_irq(&priv->lock);
Doug Berger07c52d62017-03-09 16:58:47 -08002612
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002613 /* Link UP/DOWN event */
Heiner Kallweit28b2e0d2018-01-10 21:21:31 +01002614 if (status & UMAC_IRQ_LINK_EVENT) {
2615 priv->dev->phydev->link = !!(status & UMAC_IRQ_LINK_UP);
2616 phy_mac_interrupt(priv->dev->phydev);
2617 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002618}
2619
Petri Gynther4055eae2015-03-25 12:35:16 -07002620/* bcmgenet_isr1: handle Rx and Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002621static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
2622{
2623 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002624 struct bcmgenet_rx_ring *rx_ring;
2625 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08002626 unsigned int index, status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002627
Doug Berger07c52d62017-03-09 16:58:47 -08002628 /* Read irq status */
2629 status = bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002630 ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002631
Brian Norris7fc527f2014-07-29 14:34:14 -07002632 /* clear interrupts */
Doug Berger07c52d62017-03-09 16:58:47 -08002633 bcmgenet_intrl2_1_writel(priv, status, INTRL2_CPU_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002634
2635 netif_dbg(priv, intr, priv->dev,
Doug Berger07c52d62017-03-09 16:58:47 -08002636 "%s: IRQ=0x%x\n", __func__, status);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002637
Petri Gynther4055eae2015-03-25 12:35:16 -07002638 /* Check Rx priority queue interrupts */
2639 for (index = 0; index < priv->hw_params->rx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08002640 if (!(status & BIT(UMAC_IRQ1_RX_INTR_SHIFT + index)))
Petri Gynther4055eae2015-03-25 12:35:16 -07002641 continue;
2642
2643 rx_ring = &priv->rx_rings[index];
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002644 rx_ring->dim.event_ctr++;
Petri Gynther4055eae2015-03-25 12:35:16 -07002645
2646 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2647 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002648 __napi_schedule_irqoff(&rx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07002649 }
2650 }
2651
2652 /* Check Tx priority queue interrupts */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002653 for (index = 0; index < priv->hw_params->tx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08002654 if (!(status & BIT(index)))
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002655 continue;
2656
Petri Gynther4055eae2015-03-25 12:35:16 -07002657 tx_ring = &priv->tx_rings[index];
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002658
Petri Gynther4055eae2015-03-25 12:35:16 -07002659 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2660 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002661 __napi_schedule_irqoff(&tx_ring->napi);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002662 }
2663 }
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002664
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002665 return IRQ_HANDLED;
2666}
2667
Petri Gynther4055eae2015-03-25 12:35:16 -07002668/* bcmgenet_isr0: handle Rx and Tx default queues + other stuff */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002669static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
2670{
2671 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002672 struct bcmgenet_rx_ring *rx_ring;
2673 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08002674 unsigned int status;
2675 unsigned long flags;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002676
Doug Berger07c52d62017-03-09 16:58:47 -08002677 /* Read irq status */
2678 status = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002679 ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002680
Brian Norris7fc527f2014-07-29 14:34:14 -07002681 /* clear interrupts */
Doug Berger07c52d62017-03-09 16:58:47 -08002682 bcmgenet_intrl2_0_writel(priv, status, INTRL2_CPU_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002683
2684 netif_dbg(priv, intr, priv->dev,
Doug Berger07c52d62017-03-09 16:58:47 -08002685 "IRQ=0x%x\n", status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002686
Doug Berger07c52d62017-03-09 16:58:47 -08002687 if (status & UMAC_IRQ_RXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07002688 rx_ring = &priv->rx_rings[DESC_INDEX];
Florian Fainelli9f4ca052018-03-22 18:19:33 -07002689 rx_ring->dim.event_ctr++;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002690
Petri Gynther4055eae2015-03-25 12:35:16 -07002691 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2692 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002693 __napi_schedule_irqoff(&rx_ring->napi);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002694 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002695 }
Petri Gynther4055eae2015-03-25 12:35:16 -07002696
Doug Berger07c52d62017-03-09 16:58:47 -08002697 if (status & UMAC_IRQ_TXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07002698 tx_ring = &priv->tx_rings[DESC_INDEX];
2699
2700 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2701 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002702 __napi_schedule_irqoff(&tx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07002703 }
2704 }
2705
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002706 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Doug Berger07c52d62017-03-09 16:58:47 -08002707 status & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002708 wake_up(&priv->wq);
2709 }
2710
Doug Berger07c52d62017-03-09 16:58:47 -08002711 /* all other interested interrupts handled in bottom half */
Doug Berger0d314502017-10-25 15:04:11 -07002712 status &= UMAC_IRQ_LINK_EVENT;
Doug Berger07c52d62017-03-09 16:58:47 -08002713 if (status) {
2714 /* Save irq status for bottom-half processing. */
2715 spin_lock_irqsave(&priv->lock, flags);
2716 priv->irq0_stat |= status;
2717 spin_unlock_irqrestore(&priv->lock, flags);
2718
2719 schedule_work(&priv->bcmgenet_irq_work);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002720 }
2721
2722 return IRQ_HANDLED;
2723}
2724
Florian Fainelli85620562014-07-21 15:29:23 -07002725static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
2726{
2727 struct bcmgenet_priv *priv = dev_id;
2728
2729 pm_wakeup_event(&priv->pdev->dev, 0);
2730
2731 return IRQ_HANDLED;
2732}
2733
Florian Fainelli4d2e8882015-07-31 11:42:54 -07002734#ifdef CONFIG_NET_POLL_CONTROLLER
2735static void bcmgenet_poll_controller(struct net_device *dev)
2736{
2737 struct bcmgenet_priv *priv = netdev_priv(dev);
2738
2739 /* Invoke the main RX/TX interrupt handler */
2740 disable_irq(priv->irq0);
2741 bcmgenet_isr0(priv->irq0, priv);
2742 enable_irq(priv->irq0);
2743
2744 /* And the interrupt handler for RX/TX priority queues */
2745 disable_irq(priv->irq1);
2746 bcmgenet_isr1(priv->irq1, priv);
2747 enable_irq(priv->irq1);
2748}
2749#endif
2750
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002751static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
2752{
2753 u32 reg;
2754
2755 reg = bcmgenet_rbuf_ctrl_get(priv);
2756 reg |= BIT(1);
2757 bcmgenet_rbuf_ctrl_set(priv, reg);
2758 udelay(10);
2759
2760 reg &= ~BIT(1);
2761 bcmgenet_rbuf_ctrl_set(priv, reg);
2762 udelay(10);
2763}
2764
2765static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002766 unsigned char *addr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002767{
2768 bcmgenet_umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
2769 (addr[2] << 8) | addr[3], UMAC_MAC0);
2770 bcmgenet_umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
2771}
2772
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002773/* Returns a reusable dma control register value */
2774static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
2775{
2776 u32 reg;
2777 u32 dma_ctrl;
2778
2779 /* disable DMA */
2780 dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
2781 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2782 reg &= ~dma_ctrl;
2783 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2784
2785 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2786 reg &= ~dma_ctrl;
2787 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2788
2789 bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
2790 udelay(10);
2791 bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
2792
2793 return dma_ctrl;
2794}
2795
2796static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
2797{
2798 u32 reg;
2799
2800 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2801 reg |= dma_ctrl;
2802 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2803
2804 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2805 reg |= dma_ctrl;
2806 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2807}
2808
Petri Gynther0034de42015-03-13 14:45:00 -07002809/* bcmgenet_hfb_clear
2810 *
2811 * Clear Hardware Filter Block and disable all filtering.
2812 */
2813static void bcmgenet_hfb_clear(struct bcmgenet_priv *priv)
2814{
2815 u32 i;
2816
2817 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_CTRL);
2818 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS);
2819 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS + 4);
2820
2821 for (i = DMA_INDEX2RING_0; i <= DMA_INDEX2RING_7; i++)
2822 bcmgenet_rdma_writel(priv, 0x0, i);
2823
2824 for (i = 0; i < (priv->hw_params->hfb_filter_cnt / 4); i++)
2825 bcmgenet_hfb_reg_writel(priv, 0x0,
2826 HFB_FLT_LEN_V3PLUS + i * sizeof(u32));
2827
2828 for (i = 0; i < priv->hw_params->hfb_filter_cnt *
2829 priv->hw_params->hfb_filter_size; i++)
2830 bcmgenet_hfb_writel(priv, 0x0, i * sizeof(u32));
2831}
2832
2833static void bcmgenet_hfb_init(struct bcmgenet_priv *priv)
2834{
2835 if (GENET_IS_V1(priv) || GENET_IS_V2(priv))
2836 return;
2837
2838 bcmgenet_hfb_clear(priv);
2839}
2840
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002841static void bcmgenet_netif_start(struct net_device *dev)
2842{
2843 struct bcmgenet_priv *priv = netdev_priv(dev);
2844
2845 /* Start the network engine */
Petri Gynther3ab11332015-03-25 12:35:15 -07002846 bcmgenet_enable_rx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002847
2848 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
2849
Doug Bergerd215dba2017-10-25 15:04:16 -07002850 bcmgenet_enable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002851
Florian Fainelli37850e32015-10-17 14:22:46 -07002852 /* Monitor link interrupts now */
2853 bcmgenet_link_intr_enable(priv);
2854
Doug Berger6c97f012017-10-25 15:04:19 -07002855 phy_start(dev->phydev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002856}
2857
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002858static int bcmgenet_open(struct net_device *dev)
2859{
2860 struct bcmgenet_priv *priv = netdev_priv(dev);
2861 unsigned long dma_ctrl;
2862 u32 reg;
2863 int ret;
2864
2865 netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
2866
2867 /* Turn on the clock */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002868 clk_prepare_enable(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002869
Florian Fainellia642c4f2015-03-23 15:09:56 -07002870 /* If this is an internal GPHY, power it back on now, before UniMAC is
2871 * brought out of reset as absolutely no UniMAC activity is allowed
2872 */
Florian Fainellic624f892015-07-16 15:51:17 -07002873 if (priv->internal_phy)
Florian Fainellia642c4f2015-03-23 15:09:56 -07002874 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
2875
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002876 /* take MAC out of reset */
2877 bcmgenet_umac_reset(priv);
2878
Doug Berger28c2d1a2017-10-25 15:04:13 -07002879 init_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002880
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002881 /* Make sure we reflect the value of CRC_CMD_FWD */
2882 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
2883 priv->crc_fwd_en = !!(reg & CMD_CRC_FWD);
2884
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002885 bcmgenet_set_hw_addr(priv, dev->dev_addr);
2886
Florian Fainellic624f892015-07-16 15:51:17 -07002887 if (priv->internal_phy) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002888 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
2889 reg |= EXT_ENERGY_DET_MASK;
2890 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
2891 }
2892
2893 /* Disable RX/TX DMA and flush TX queues */
2894 dma_ctrl = bcmgenet_dma_disable(priv);
2895
2896 /* Reinitialize TDMA and RDMA and SW housekeeping */
2897 ret = bcmgenet_init_dma(priv);
2898 if (ret) {
2899 netdev_err(dev, "failed to initialize DMA\n");
Petri Gyntherfac25942015-03-30 00:29:13 -07002900 goto err_clk_disable;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002901 }
2902
2903 /* Always enable ring 16 - descriptor ring */
2904 bcmgenet_enable_dma(priv, dma_ctrl);
2905
Petri Gynther0034de42015-03-13 14:45:00 -07002906 /* HFB init */
2907 bcmgenet_hfb_init(priv);
2908
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002909 ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002910 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002911 if (ret < 0) {
2912 netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
2913 goto err_fini_dma;
2914 }
2915
2916 ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002917 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002918 if (ret < 0) {
2919 netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
2920 goto err_irq0;
2921 }
2922
Florian Fainelli6cc8e6d2015-07-16 15:51:18 -07002923 ret = bcmgenet_mii_probe(dev);
2924 if (ret) {
2925 netdev_err(dev, "failed to connect to PHY\n");
2926 goto err_irq1;
2927 }
Florian Fainellic96e7312014-11-10 18:06:20 -08002928
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002929 bcmgenet_netif_start(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002930
Doug Berger09e805d2018-11-01 15:55:37 -07002931 netif_tx_start_all_queues(dev);
2932
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002933 return 0;
2934
Florian Fainelli6cc8e6d2015-07-16 15:51:18 -07002935err_irq1:
2936 free_irq(priv->irq1, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002937err_irq0:
Florian Fainelli978ffac42015-07-16 15:51:15 -07002938 free_irq(priv->irq0, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002939err_fini_dma:
Doug Berger4fd6dc92017-10-25 15:04:12 -07002940 bcmgenet_dma_teardown(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002941 bcmgenet_fini_dma(priv);
2942err_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08002943 if (priv->internal_phy)
2944 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002945 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002946 return ret;
2947}
2948
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002949static void bcmgenet_netif_stop(struct net_device *dev)
2950{
2951 struct bcmgenet_priv *priv = netdev_priv(dev);
2952
Doug Bergerd215dba2017-10-25 15:04:16 -07002953 bcmgenet_disable_tx_napi(priv);
Doug Berger09e805d2018-11-01 15:55:37 -07002954 netif_tx_disable(dev);
Doug Bergerd215dba2017-10-25 15:04:16 -07002955
2956 /* Disable MAC receive */
2957 umac_enable_set(priv, CMD_RX_EN, false);
2958
2959 bcmgenet_dma_teardown(priv);
2960
2961 /* Disable MAC transmit. TX DMA disabled must be done before this */
2962 umac_enable_set(priv, CMD_TX_EN, false);
2963
Doug Berger6c97f012017-10-25 15:04:19 -07002964 phy_stop(dev->phydev);
Petri Gynther3ab11332015-03-25 12:35:15 -07002965 bcmgenet_disable_rx_napi(priv);
Doug Bergerfbf557d2017-10-25 15:04:15 -07002966 bcmgenet_intr_disable(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002967
2968 /* Wait for pending work items to complete. Since interrupts are
2969 * disabled no new work will be scheduled.
2970 */
2971 cancel_work_sync(&priv->bcmgenet_irq_work);
Florian Fainellicc013fb2014-08-11 14:50:43 -07002972
Florian Fainellicc013fb2014-08-11 14:50:43 -07002973 priv->old_link = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002974 priv->old_speed = -1;
Florian Fainellicc013fb2014-08-11 14:50:43 -07002975 priv->old_duplex = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002976 priv->old_pause = -1;
Doug Bergerd215dba2017-10-25 15:04:16 -07002977
2978 /* tx reclaim */
2979 bcmgenet_tx_reclaim_all(dev);
2980 bcmgenet_fini_dma(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002981}
2982
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002983static int bcmgenet_close(struct net_device *dev)
2984{
2985 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Bergerd215dba2017-10-25 15:04:16 -07002986 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002987
2988 netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
2989
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002990 bcmgenet_netif_stop(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002991
Florian Fainellic96e7312014-11-10 18:06:20 -08002992 /* Really kill the PHY state machine and disconnect from it */
Doug Berger6c97f012017-10-25 15:04:19 -07002993 phy_disconnect(dev->phydev);
Florian Fainellic96e7312014-11-10 18:06:20 -08002994
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002995 free_irq(priv->irq0, priv);
2996 free_irq(priv->irq1, priv);
2997
Florian Fainellic624f892015-07-16 15:51:17 -07002998 if (priv->internal_phy)
Florian Fainellica8cf342015-03-23 15:09:51 -07002999 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003000
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003001 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003002
Florian Fainellica8cf342015-03-23 15:09:51 -07003003 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003004}
3005
Florian Fainelli13ea6572015-06-04 16:15:50 -07003006static void bcmgenet_dump_tx_queue(struct bcmgenet_tx_ring *ring)
3007{
3008 struct bcmgenet_priv *priv = ring->priv;
3009 u32 p_index, c_index, intsts, intmsk;
3010 struct netdev_queue *txq;
3011 unsigned int free_bds;
Florian Fainelli13ea6572015-06-04 16:15:50 -07003012 bool txq_stopped;
3013
3014 if (!netif_msg_tx_err(priv))
3015 return;
3016
3017 txq = netdev_get_tx_queue(priv->dev, ring->queue);
3018
Doug Bergerb0447ec2017-10-25 15:04:17 -07003019 spin_lock(&ring->lock);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003020 if (ring->index == DESC_INDEX) {
3021 intsts = ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
3022 intmsk = UMAC_IRQ_TXDMA_DONE | UMAC_IRQ_TXDMA_MBDONE;
3023 } else {
3024 intsts = ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
3025 intmsk = 1 << ring->index;
3026 }
3027 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
3028 p_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_PROD_INDEX);
3029 txq_stopped = netif_tx_queue_stopped(txq);
3030 free_bds = ring->free_bds;
Doug Bergerb0447ec2017-10-25 15:04:17 -07003031 spin_unlock(&ring->lock);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003032
3033 netif_err(priv, tx_err, priv->dev, "Ring %d queue %d status summary\n"
3034 "TX queue status: %s, interrupts: %s\n"
3035 "(sw)free_bds: %d (sw)size: %d\n"
3036 "(sw)p_index: %d (hw)p_index: %d\n"
3037 "(sw)c_index: %d (hw)c_index: %d\n"
3038 "(sw)clean_p: %d (sw)write_p: %d\n"
3039 "(sw)cb_ptr: %d (sw)end_ptr: %d\n",
3040 ring->index, ring->queue,
3041 txq_stopped ? "stopped" : "active",
3042 intsts & intmsk ? "enabled" : "disabled",
3043 free_bds, ring->size,
3044 ring->prod_index, p_index & DMA_P_INDEX_MASK,
3045 ring->c_index, c_index & DMA_C_INDEX_MASK,
3046 ring->clean_ptr, ring->write_ptr,
3047 ring->cb_ptr, ring->end_ptr);
3048}
3049
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003050static void bcmgenet_timeout(struct net_device *dev)
3051{
3052 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003053 u32 int0_enable = 0;
3054 u32 int1_enable = 0;
3055 unsigned int q;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003056
3057 netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
3058
Florian Fainelli13ea6572015-06-04 16:15:50 -07003059 for (q = 0; q < priv->hw_params->tx_queues; q++)
3060 bcmgenet_dump_tx_queue(&priv->tx_rings[q]);
3061 bcmgenet_dump_tx_queue(&priv->tx_rings[DESC_INDEX]);
3062
3063 bcmgenet_tx_reclaim_all(dev);
3064
3065 for (q = 0; q < priv->hw_params->tx_queues; q++)
3066 int1_enable |= (1 << q);
3067
3068 int0_enable = UMAC_IRQ_TXDMA_DONE;
3069
3070 /* Re-enable TX interrupts if disabled */
3071 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
3072 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
3073
Florian Westphal860e9532016-05-03 16:33:13 +02003074 netif_trans_update(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003075
3076 dev->stats.tx_errors++;
3077
3078 netif_tx_wake_all_queues(dev);
3079}
3080
Justin Chen35cbef92019-07-17 14:58:53 -07003081#define MAX_MDF_FILTER 17
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003082
3083static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
3084 unsigned char *addr,
Justin Chen35cbef92019-07-17 14:58:53 -07003085 int *i)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003086{
Florian Fainellic91b7f62014-07-23 10:42:12 -07003087 bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
3088 UMAC_MDF_ADDR + (*i * 4));
3089 bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
3090 addr[4] << 8 | addr[5],
3091 UMAC_MDF_ADDR + ((*i + 1) * 4));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003092 *i += 2;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003093}
3094
3095static void bcmgenet_set_rx_mode(struct net_device *dev)
3096{
3097 struct bcmgenet_priv *priv = netdev_priv(dev);
3098 struct netdev_hw_addr *ha;
Justin Chen35cbef92019-07-17 14:58:53 -07003099 int i, nfilter;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003100 u32 reg;
3101
3102 netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
3103
Justin Chen35cbef92019-07-17 14:58:53 -07003104 /* Number of filters needed */
3105 nfilter = netdev_uc_count(dev) + netdev_mc_count(dev) + 2;
3106
3107 /*
3108 * Turn on promicuous mode for three scenarios
3109 * 1. IFF_PROMISC flag is set
3110 * 2. IFF_ALLMULTI flag is set
3111 * 3. The number of filters needed exceeds the number filters
3112 * supported by the hardware.
3113 */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003114 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
Justin Chen35cbef92019-07-17 14:58:53 -07003115 if ((dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) ||
3116 (nfilter > MAX_MDF_FILTER)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003117 reg |= CMD_PROMISC;
3118 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3119 bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
3120 return;
3121 } else {
3122 reg &= ~CMD_PROMISC;
3123 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3124 }
3125
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003126 /* update MDF filter */
3127 i = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003128 /* Broadcast */
Justin Chen35cbef92019-07-17 14:58:53 -07003129 bcmgenet_set_mdf_addr(priv, dev->broadcast, &i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003130 /* my own address.*/
Justin Chen35cbef92019-07-17 14:58:53 -07003131 bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003132
Justin Chen35cbef92019-07-17 14:58:53 -07003133 /* Unicast */
3134 netdev_for_each_uc_addr(ha, dev)
3135 bcmgenet_set_mdf_addr(priv, ha->addr, &i);
3136
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003137 /* Multicast */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003138 netdev_for_each_mc_addr(ha, dev)
Justin Chen35cbef92019-07-17 14:58:53 -07003139 bcmgenet_set_mdf_addr(priv, ha->addr, &i);
3140
3141 /* Enable filters */
3142 reg = GENMASK(MAX_MDF_FILTER - 1, MAX_MDF_FILTER - nfilter);
3143 bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003144}
3145
3146/* Set the hardware MAC address. */
3147static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
3148{
3149 struct sockaddr *addr = p;
3150
3151 /* Setting the MAC address at the hardware level is not possible
3152 * without disabling the UniMAC RX/TX enable bits.
3153 */
3154 if (netif_running(dev))
3155 return -EBUSY;
3156
3157 ether_addr_copy(dev->dev_addr, addr->sa_data);
3158
3159 return 0;
3160}
3161
Florian Fainelli37a30b42017-03-16 10:27:08 -07003162static struct net_device_stats *bcmgenet_get_stats(struct net_device *dev)
3163{
3164 struct bcmgenet_priv *priv = netdev_priv(dev);
3165 unsigned long tx_bytes = 0, tx_packets = 0;
3166 unsigned long rx_bytes = 0, rx_packets = 0;
3167 unsigned long rx_errors = 0, rx_dropped = 0;
3168 struct bcmgenet_tx_ring *tx_ring;
3169 struct bcmgenet_rx_ring *rx_ring;
3170 unsigned int q;
3171
3172 for (q = 0; q < priv->hw_params->tx_queues; q++) {
3173 tx_ring = &priv->tx_rings[q];
3174 tx_bytes += tx_ring->bytes;
3175 tx_packets += tx_ring->packets;
3176 }
3177 tx_ring = &priv->tx_rings[DESC_INDEX];
3178 tx_bytes += tx_ring->bytes;
3179 tx_packets += tx_ring->packets;
3180
3181 for (q = 0; q < priv->hw_params->rx_queues; q++) {
3182 rx_ring = &priv->rx_rings[q];
3183
3184 rx_bytes += rx_ring->bytes;
3185 rx_packets += rx_ring->packets;
3186 rx_errors += rx_ring->errors;
3187 rx_dropped += rx_ring->dropped;
3188 }
3189 rx_ring = &priv->rx_rings[DESC_INDEX];
3190 rx_bytes += rx_ring->bytes;
3191 rx_packets += rx_ring->packets;
3192 rx_errors += rx_ring->errors;
3193 rx_dropped += rx_ring->dropped;
3194
3195 dev->stats.tx_bytes = tx_bytes;
3196 dev->stats.tx_packets = tx_packets;
3197 dev->stats.rx_bytes = rx_bytes;
3198 dev->stats.rx_packets = rx_packets;
3199 dev->stats.rx_errors = rx_errors;
3200 dev->stats.rx_missed_errors = rx_errors;
3201 return &dev->stats;
3202}
3203
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003204static const struct net_device_ops bcmgenet_netdev_ops = {
3205 .ndo_open = bcmgenet_open,
3206 .ndo_stop = bcmgenet_close,
3207 .ndo_start_xmit = bcmgenet_xmit,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003208 .ndo_tx_timeout = bcmgenet_timeout,
3209 .ndo_set_rx_mode = bcmgenet_set_rx_mode,
3210 .ndo_set_mac_address = bcmgenet_set_mac_addr,
3211 .ndo_do_ioctl = bcmgenet_ioctl,
3212 .ndo_set_features = bcmgenet_set_features,
Florian Fainelli4d2e8882015-07-31 11:42:54 -07003213#ifdef CONFIG_NET_POLL_CONTROLLER
3214 .ndo_poll_controller = bcmgenet_poll_controller,
3215#endif
Florian Fainelli37a30b42017-03-16 10:27:08 -07003216 .ndo_get_stats = bcmgenet_get_stats,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003217};
3218
3219/* Array of GENET hardware parameters/characteristics */
3220static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
3221 [GENET_V1] = {
3222 .tx_queues = 0,
Petri Gynther51a966a2015-02-23 11:00:46 -08003223 .tx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003224 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003225 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003226 .bp_in_en_shift = 16,
3227 .bp_in_mask = 0xffff,
3228 .hfb_filter_cnt = 16,
3229 .qtag_mask = 0x1F,
3230 .hfb_offset = 0x1000,
3231 .rdma_offset = 0x2000,
3232 .tdma_offset = 0x3000,
3233 .words_per_bd = 2,
3234 },
3235 [GENET_V2] = {
3236 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003237 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003238 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003239 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003240 .bp_in_en_shift = 16,
3241 .bp_in_mask = 0xffff,
3242 .hfb_filter_cnt = 16,
3243 .qtag_mask = 0x1F,
3244 .tbuf_offset = 0x0600,
3245 .hfb_offset = 0x1000,
3246 .hfb_reg_offset = 0x2000,
3247 .rdma_offset = 0x3000,
3248 .tdma_offset = 0x4000,
3249 .words_per_bd = 2,
3250 .flags = GENET_HAS_EXT,
3251 },
3252 [GENET_V3] = {
3253 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003254 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003255 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003256 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003257 .bp_in_en_shift = 17,
3258 .bp_in_mask = 0x1ffff,
3259 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003260 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003261 .qtag_mask = 0x3F,
3262 .tbuf_offset = 0x0600,
3263 .hfb_offset = 0x8000,
3264 .hfb_reg_offset = 0xfc00,
3265 .rdma_offset = 0x10000,
3266 .tdma_offset = 0x11000,
3267 .words_per_bd = 2,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003268 .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR |
3269 GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003270 },
3271 [GENET_V4] = {
3272 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003273 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003274 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003275 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003276 .bp_in_en_shift = 17,
3277 .bp_in_mask = 0x1ffff,
3278 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003279 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003280 .qtag_mask = 0x3F,
3281 .tbuf_offset = 0x0600,
3282 .hfb_offset = 0x8000,
3283 .hfb_reg_offset = 0xfc00,
3284 .rdma_offset = 0x2000,
3285 .tdma_offset = 0x4000,
3286 .words_per_bd = 3,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003287 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3288 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003289 },
Doug Berger42138082017-03-13 17:41:42 -07003290 [GENET_V5] = {
3291 .tx_queues = 4,
3292 .tx_bds_per_q = 32,
3293 .rx_queues = 0,
3294 .rx_bds_per_q = 0,
3295 .bp_in_en_shift = 17,
3296 .bp_in_mask = 0x1ffff,
3297 .hfb_filter_cnt = 48,
3298 .hfb_filter_size = 128,
3299 .qtag_mask = 0x3F,
3300 .tbuf_offset = 0x0600,
3301 .hfb_offset = 0x8000,
3302 .hfb_reg_offset = 0xfc00,
3303 .rdma_offset = 0x2000,
3304 .tdma_offset = 0x4000,
3305 .words_per_bd = 3,
3306 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3307 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
3308 },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003309};
3310
3311/* Infer hardware parameters from the detected GENET version */
3312static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
3313{
3314 struct bcmgenet_hw_params *params;
3315 u32 reg;
3316 u8 major;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003317 u16 gphy_rev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003318
Doug Berger42138082017-03-13 17:41:42 -07003319 if (GENET_IS_V5(priv) || GENET_IS_V4(priv)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003320 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3321 genet_dma_ring_regs = genet_dma_ring_regs_v4;
3322 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003323 } else if (GENET_IS_V3(priv)) {
3324 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3325 genet_dma_ring_regs = genet_dma_ring_regs_v123;
3326 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003327 } else if (GENET_IS_V2(priv)) {
3328 bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
3329 genet_dma_ring_regs = genet_dma_ring_regs_v123;
3330 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003331 } else if (GENET_IS_V1(priv)) {
3332 bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
3333 genet_dma_ring_regs = genet_dma_ring_regs_v123;
3334 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003335 }
3336
3337 /* enum genet_version starts at 1 */
3338 priv->hw_params = &bcmgenet_hw_params[priv->version];
3339 params = priv->hw_params;
3340
3341 /* Read GENET HW version */
3342 reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
3343 major = (reg >> 24 & 0x0f);
Doug Berger42138082017-03-13 17:41:42 -07003344 if (major == 6)
3345 major = 5;
3346 else if (major == 5)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003347 major = 4;
3348 else if (major == 0)
3349 major = 1;
3350 if (major != priv->version) {
3351 dev_err(&priv->pdev->dev,
3352 "GENET version mismatch, got: %d, configured for: %d\n",
3353 major, priv->version);
3354 }
3355
3356 /* Print the GENET core version */
3357 dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003358 major, (reg >> 16) & 0x0f, reg & 0xffff);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003359
Florian Fainelli487320c2014-09-19 13:07:53 -07003360 /* Store the integrated PHY revision for the MDIO probing function
3361 * to pass this information to the PHY driver. The PHY driver expects
3362 * to find the PHY major revision in bits 15:8 while the GENET register
3363 * stores that information in bits 7:0, account for that.
Florian Fainellib04a2f52014-12-03 09:56:59 -08003364 *
3365 * On newer chips, starting with PHY revision G0, a new scheme is
3366 * deployed similar to the Starfighter 2 switch with GPHY major
3367 * revision in bits 15:8 and patch level in bits 7:0. Major revision 0
3368 * is reserved as well as special value 0x01ff, we have a small
3369 * heuristic to check for the new GPHY revision and re-arrange things
3370 * so the GPHY driver is happy.
Florian Fainelli487320c2014-09-19 13:07:53 -07003371 */
Florian Fainellib04a2f52014-12-03 09:56:59 -08003372 gphy_rev = reg & 0xffff;
3373
Doug Berger42138082017-03-13 17:41:42 -07003374 if (GENET_IS_V5(priv)) {
3375 /* The EPHY revision should come from the MDIO registers of
3376 * the PHY not from GENET.
3377 */
3378 if (gphy_rev != 0) {
3379 pr_warn("GENET is reporting EPHY revision: 0x%04x\n",
3380 gphy_rev);
3381 }
Doug Bergereca4bad2017-03-09 16:58:45 -08003382 /* This is reserved so should require special treatment */
David S. Miller101c4312017-03-15 11:59:10 -07003383 } else if (gphy_rev == 0 || gphy_rev == 0x01ff) {
Doug Bergereca4bad2017-03-09 16:58:45 -08003384 pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev);
3385 return;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003386 /* This is the good old scheme, just GPHY major, no minor nor patch */
Doug Berger42138082017-03-13 17:41:42 -07003387 } else if ((gphy_rev & 0xf0) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003388 priv->gphy_rev = gphy_rev << 8;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003389 /* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */
Doug Berger42138082017-03-13 17:41:42 -07003390 } else if ((gphy_rev & 0xff00) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003391 priv->gphy_rev = gphy_rev;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003392 }
Florian Fainelli487320c2014-09-19 13:07:53 -07003393
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003394#ifdef CONFIG_PHYS_ADDR_T_64BIT
3395 if (!(params->flags & GENET_HAS_40BITS))
3396 pr_warn("GENET does not support 40-bits PA\n");
3397#endif
3398
3399 pr_debug("Configuration for version: %d\n"
Petri Gynther3feafa02015-03-05 17:40:14 -08003400 "TXq: %1d, TXqBDs: %1d, RXq: %1d, RXqBDs: %1d\n"
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003401 "BP << en: %2d, BP msk: 0x%05x\n"
3402 "HFB count: %2d, QTAQ msk: 0x%05x\n"
3403 "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
3404 "RDMA: 0x%05x, TDMA: 0x%05x\n"
3405 "Words/BD: %d\n",
3406 priv->version,
Petri Gynther51a966a2015-02-23 11:00:46 -08003407 params->tx_queues, params->tx_bds_per_q,
Petri Gynther3feafa02015-03-05 17:40:14 -08003408 params->rx_queues, params->rx_bds_per_q,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003409 params->bp_in_en_shift, params->bp_in_mask,
3410 params->hfb_filter_cnt, params->qtag_mask,
3411 params->tbuf_offset, params->hfb_offset,
3412 params->hfb_reg_offset,
3413 params->rdma_offset, params->tdma_offset,
3414 params->words_per_bd);
3415}
3416
3417static const struct of_device_id bcmgenet_match[] = {
3418 { .compatible = "brcm,genet-v1", .data = (void *)GENET_V1 },
3419 { .compatible = "brcm,genet-v2", .data = (void *)GENET_V2 },
3420 { .compatible = "brcm,genet-v3", .data = (void *)GENET_V3 },
3421 { .compatible = "brcm,genet-v4", .data = (void *)GENET_V4 },
Doug Berger42138082017-03-13 17:41:42 -07003422 { .compatible = "brcm,genet-v5", .data = (void *)GENET_V5 },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003423 { },
3424};
Luis de Bethencourte8048e52015-09-18 17:55:02 +02003425MODULE_DEVICE_TABLE(of, bcmgenet_match);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003426
3427static int bcmgenet_probe(struct platform_device *pdev)
3428{
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003429 struct bcmgenet_platform_data *pd = pdev->dev.platform_data;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003430 struct device_node *dn = pdev->dev.of_node;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003431 const struct of_device_id *of_id = NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003432 struct bcmgenet_priv *priv;
3433 struct net_device *dev;
3434 const void *macaddr;
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07003435 unsigned int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003436 int err = -EIO;
Doug Berger6be371b2017-03-09 16:58:48 -08003437 const char *phy_mode_str;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003438
Petri Gynther3feafee2015-03-05 17:40:12 -08003439 /* Up to GENET_MAX_MQ_CNT + 1 TX queues and RX queues */
3440 dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1,
3441 GENET_MAX_MQ_CNT + 1);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003442 if (!dev) {
3443 dev_err(&pdev->dev, "can't allocate net device\n");
3444 return -ENOMEM;
3445 }
3446
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003447 if (dn) {
3448 of_id = of_match_node(bcmgenet_match, dn);
3449 if (!of_id)
3450 return -EINVAL;
3451 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003452
3453 priv = netdev_priv(dev);
3454 priv->irq0 = platform_get_irq(pdev, 0);
3455 priv->irq1 = platform_get_irq(pdev, 1);
Florian Fainelli85620562014-07-21 15:29:23 -07003456 priv->wol_irq = platform_get_irq(pdev, 2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003457 if (!priv->irq0 || !priv->irq1) {
3458 dev_err(&pdev->dev, "can't find IRQs\n");
3459 err = -EINVAL;
3460 goto err;
3461 }
3462
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003463 if (dn) {
3464 macaddr = of_get_mac_address(dn);
Petr Å tetiara51645f2019-05-06 23:27:04 +02003465 if (IS_ERR(macaddr)) {
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003466 dev_err(&pdev->dev, "can't find MAC address\n");
3467 err = -EINVAL;
3468 goto err;
3469 }
3470 } else {
3471 macaddr = pd->mac_address;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003472 }
3473
YueHaibing4ca33482019-08-21 21:41:31 +08003474 priv->base = devm_platform_ioremap_resource(pdev, 0);
Fabio Estevam5343a102014-02-24 00:47:24 -03003475 if (IS_ERR(priv->base)) {
3476 err = PTR_ERR(priv->base);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003477 goto err;
3478 }
3479
Doug Berger07c52d62017-03-09 16:58:47 -08003480 spin_lock_init(&priv->lock);
3481
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003482 SET_NETDEV_DEV(dev, &pdev->dev);
3483 dev_set_drvdata(&pdev->dev, dev);
3484 ether_addr_copy(dev->dev_addr, macaddr);
3485 dev->watchdog_timeo = 2 * HZ;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003486 dev->ethtool_ops = &bcmgenet_ethtool_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003487 dev->netdev_ops = &bcmgenet_netdev_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003488
3489 priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
3490
3491 /* Set hardware features */
3492 dev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM |
3493 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
3494
Florian Fainelli85620562014-07-21 15:29:23 -07003495 /* Request the WOL interrupt and advertise suspend if available */
3496 priv->wol_irq_disabled = true;
3497 err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0,
3498 dev->name, priv);
3499 if (!err)
3500 device_set_wakeup_capable(&pdev->dev, 1);
3501
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003502 /* Set the needed headroom to account for any possible
3503 * features enabling/disabling at runtime
3504 */
3505 dev->needed_headroom += 64;
3506
3507 netdev_boot_setup_check(dev);
3508
3509 priv->dev = dev;
3510 priv->pdev = pdev;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003511 if (of_id)
3512 priv->version = (enum bcmgenet_version)of_id->data;
3513 else
3514 priv->version = pd->genet_version;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003515
Florian Fainellie4a60a92014-08-11 14:50:42 -07003516 priv->clk = devm_clk_get(&priv->pdev->dev, "enet");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003517 if (IS_ERR(priv->clk)) {
Florian Fainellie4a60a92014-08-11 14:50:42 -07003518 dev_warn(&priv->pdev->dev, "failed to get enet clock\n");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003519 priv->clk = NULL;
3520 }
Florian Fainellie4a60a92014-08-11 14:50:42 -07003521
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003522 clk_prepare_enable(priv->clk);
Florian Fainellie4a60a92014-08-11 14:50:42 -07003523
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003524 bcmgenet_set_hw_params(priv);
3525
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003526 /* Mii wait queue */
3527 init_waitqueue_head(&priv->wq);
3528 /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
3529 priv->rx_buf_len = RX_BUF_LENGTH;
3530 INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
3531
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003532 priv->clk_wol = devm_clk_get(&priv->pdev->dev, "enet-wol");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003533 if (IS_ERR(priv->clk_wol)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003534 dev_warn(&priv->pdev->dev, "failed to get enet-wol clock\n");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003535 priv->clk_wol = NULL;
3536 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003537
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003538 priv->clk_eee = devm_clk_get(&priv->pdev->dev, "enet-eee");
3539 if (IS_ERR(priv->clk_eee)) {
3540 dev_warn(&priv->pdev->dev, "failed to get enet-eee clock\n");
3541 priv->clk_eee = NULL;
3542 }
3543
Doug Berger6be371b2017-03-09 16:58:48 -08003544 /* If this is an internal GPHY, power it on now, before UniMAC is
3545 * brought out of reset as absolutely no UniMAC activity is allowed
3546 */
3547 if (dn && !of_property_read_string(dn, "phy-mode", &phy_mode_str) &&
3548 !strcasecmp(phy_mode_str, "internal"))
3549 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3550
Doug Berger28c2d1a2017-10-25 15:04:13 -07003551 reset_umac(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003552
3553 err = bcmgenet_mii_init(dev);
3554 if (err)
3555 goto err_clk_disable;
3556
3557 /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
3558 * just the ring 16 descriptor based TX
3559 */
3560 netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
3561 netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
3562
Florian Fainelli5e6ce1f2018-03-28 15:15:38 -07003563 /* Set default coalescing parameters */
3564 for (i = 0; i < priv->hw_params->rx_queues; i++)
3565 priv->rx_rings[i].rx_max_coalesced_frames = 1;
3566 priv->rx_rings[DESC_INDEX].rx_max_coalesced_frames = 1;
3567
Florian Fainelli219575e2014-06-26 10:26:21 -07003568 /* libphy will determine the link state */
3569 netif_carrier_off(dev);
3570
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003571 /* Turn off the main clock, WOL clock is handled separately */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003572 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003573
Florian Fainelli0f50ce92014-06-26 10:26:20 -07003574 err = register_netdev(dev);
3575 if (err)
3576 goto err;
3577
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003578 return err;
3579
3580err_clk_disable:
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003581 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003582err:
3583 free_netdev(dev);
3584 return err;
3585}
3586
3587static int bcmgenet_remove(struct platform_device *pdev)
3588{
3589 struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
3590
3591 dev_set_drvdata(&pdev->dev, NULL);
3592 unregister_netdev(priv->dev);
3593 bcmgenet_mii_exit(priv->dev);
3594 free_netdev(priv->dev);
3595
3596 return 0;
3597}
3598
Florian Fainellib6e978e2014-07-21 15:29:22 -07003599#ifdef CONFIG_PM_SLEEP
Florian Fainellib6e978e2014-07-21 15:29:22 -07003600static int bcmgenet_resume(struct device *d)
3601{
3602 struct net_device *dev = dev_get_drvdata(d);
3603 struct bcmgenet_priv *priv = netdev_priv(dev);
3604 unsigned long dma_ctrl;
3605 int ret;
3606 u32 reg;
3607
3608 if (!netif_running(dev))
3609 return 0;
3610
3611 /* Turn on the clock */
3612 ret = clk_prepare_enable(priv->clk);
3613 if (ret)
3614 return ret;
3615
Florian Fainellia6f31f52015-03-23 15:09:57 -07003616 /* If this is an internal GPHY, power it back on now, before UniMAC is
3617 * brought out of reset as absolutely no UniMAC activity is allowed
3618 */
Florian Fainellic624f892015-07-16 15:51:17 -07003619 if (priv->internal_phy)
Florian Fainellia6f31f52015-03-23 15:09:57 -07003620 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3621
Florian Fainellib6e978e2014-07-21 15:29:22 -07003622 bcmgenet_umac_reset(priv);
3623
Doug Berger28c2d1a2017-10-25 15:04:13 -07003624 init_umac(priv);
Florian Fainellib6e978e2014-07-21 15:29:22 -07003625
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003626 /* From WOL-enabled suspend, switch to regular clock */
3627 if (priv->wolopts)
3628 clk_disable_unprepare(priv->clk_wol);
3629
Doug Berger6c97f012017-10-25 15:04:19 -07003630 phy_init_hw(dev->phydev);
3631
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003632 /* Speed settings must be restored */
Florian Fainelli00d51092017-07-31 11:05:32 -07003633 bcmgenet_mii_config(priv->dev, false);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003634
Florian Fainellib6e978e2014-07-21 15:29:22 -07003635 bcmgenet_set_hw_addr(priv, dev->dev_addr);
3636
Florian Fainellic624f892015-07-16 15:51:17 -07003637 if (priv->internal_phy) {
Florian Fainellib6e978e2014-07-21 15:29:22 -07003638 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
3639 reg |= EXT_ENERGY_DET_MASK;
3640 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
3641 }
3642
Florian Fainelli98bb7392014-08-11 14:50:45 -07003643 if (priv->wolopts)
3644 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
3645
Florian Fainellib6e978e2014-07-21 15:29:22 -07003646 /* Disable RX/TX DMA and flush TX queues */
3647 dma_ctrl = bcmgenet_dma_disable(priv);
3648
3649 /* Reinitialize TDMA and RDMA and SW housekeeping */
3650 ret = bcmgenet_init_dma(priv);
3651 if (ret) {
3652 netdev_err(dev, "failed to initialize DMA\n");
3653 goto out_clk_disable;
3654 }
3655
3656 /* Always enable ring 16 - descriptor ring */
3657 bcmgenet_enable_dma(priv, dma_ctrl);
3658
Florian Fainelli5371bbf42017-03-15 12:57:21 -07003659 if (!device_may_wakeup(d))
Doug Berger6c97f012017-10-25 15:04:19 -07003660 phy_resume(dev->phydev);
Florian Fainellicc013fb2014-08-11 14:50:43 -07003661
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003662 if (priv->eee.eee_enabled)
3663 bcmgenet_eee_enable_set(dev, true);
3664
Florian Fainellib6e978e2014-07-21 15:29:22 -07003665 bcmgenet_netif_start(dev);
3666
Doug Berger09e805d2018-11-01 15:55:37 -07003667 netif_device_attach(dev);
3668
Florian Fainellib6e978e2014-07-21 15:29:22 -07003669 return 0;
3670
3671out_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08003672 if (priv->internal_phy)
3673 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainellib6e978e2014-07-21 15:29:22 -07003674 clk_disable_unprepare(priv->clk);
3675 return ret;
3676}
Doug Bergera94cbf02018-11-16 18:00:21 -08003677
3678static int bcmgenet_suspend(struct device *d)
3679{
3680 struct net_device *dev = dev_get_drvdata(d);
3681 struct bcmgenet_priv *priv = netdev_priv(dev);
3682 int ret = 0;
3683
3684 if (!netif_running(dev))
3685 return 0;
3686
3687 netif_device_detach(dev);
3688
3689 bcmgenet_netif_stop(dev);
3690
3691 if (!device_may_wakeup(d))
3692 phy_suspend(dev->phydev);
3693
3694 /* Prepare the device for Wake-on-LAN and switch to the slow clock */
3695 if (device_may_wakeup(d) && priv->wolopts) {
3696 ret = bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
3697 clk_prepare_enable(priv->clk_wol);
3698 } else if (priv->internal_phy) {
3699 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
3700 }
3701
3702 /* Turn off the clocks */
3703 clk_disable_unprepare(priv->clk);
3704
Doug Bergerc5a54bb2018-11-16 18:00:22 -08003705 if (ret)
3706 bcmgenet_resume(d);
3707
Doug Bergera94cbf02018-11-16 18:00:21 -08003708 return ret;
3709}
Florian Fainellib6e978e2014-07-21 15:29:22 -07003710#endif /* CONFIG_PM_SLEEP */
3711
3712static SIMPLE_DEV_PM_OPS(bcmgenet_pm_ops, bcmgenet_suspend, bcmgenet_resume);
3713
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003714static struct platform_driver bcmgenet_driver = {
3715 .probe = bcmgenet_probe,
3716 .remove = bcmgenet_remove,
3717 .driver = {
3718 .name = "bcmgenet",
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003719 .of_match_table = bcmgenet_match,
Florian Fainellib6e978e2014-07-21 15:29:22 -07003720 .pm = &bcmgenet_pm_ops,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003721 },
3722};
3723module_platform_driver(bcmgenet_driver);
3724
3725MODULE_AUTHOR("Broadcom Corporation");
3726MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
3727MODULE_ALIAS("platform:bcmgenet");
3728MODULE_LICENSE("GPL");