blob: 20021525f79548a96febbfc8b72988a68a974e97 [file] [log] [blame]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001/*
2 * Broadcom GENET (Gigabit Ethernet) controller driver
3 *
Doug Bergerc298ede2017-03-13 17:41:33 -07004 * Copyright (c) 2014-2017 Broadcom
Florian Fainelli1c1008c2014-02-13 16:08:47 -08005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
Florian Fainelli1c1008c2014-02-13 16:08:47 -08009 */
10
11#define pr_fmt(fmt) "bcmgenet: " fmt
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/sched.h>
16#include <linux/types.h>
17#include <linux/fcntl.h>
18#include <linux/interrupt.h>
19#include <linux/string.h>
20#include <linux/if_ether.h>
21#include <linux/init.h>
22#include <linux/errno.h>
23#include <linux/delay.h>
24#include <linux/platform_device.h>
25#include <linux/dma-mapping.h>
26#include <linux/pm.h>
27#include <linux/clk.h>
Florian Fainelli1c1008c2014-02-13 16:08:47 -080028#include <linux/of.h>
29#include <linux/of_address.h>
30#include <linux/of_irq.h>
31#include <linux/of_net.h>
32#include <linux/of_platform.h>
33#include <net/arp.h>
34
35#include <linux/mii.h>
36#include <linux/ethtool.h>
37#include <linux/netdevice.h>
38#include <linux/inetdevice.h>
39#include <linux/etherdevice.h>
40#include <linux/skbuff.h>
41#include <linux/in.h>
42#include <linux/ip.h>
43#include <linux/ipv6.h>
44#include <linux/phy.h>
Petri Gyntherb0ba5122014-12-01 16:18:08 -080045#include <linux/platform_data/bcmgenet.h>
Florian Fainelli1c1008c2014-02-13 16:08:47 -080046
47#include <asm/unaligned.h>
48
49#include "bcmgenet.h"
50
51/* Maximum number of hardware queues, downsized if needed */
52#define GENET_MAX_MQ_CNT 4
53
54/* Default highest priority queue for multi queue support */
55#define GENET_Q0_PRIORITY 0
56
Petri Gynther3feafa02015-03-05 17:40:14 -080057#define GENET_Q16_RX_BD_CNT \
58 (TOTAL_DESC - priv->hw_params->rx_queues * priv->hw_params->rx_bds_per_q)
Petri Gynther51a966a2015-02-23 11:00:46 -080059#define GENET_Q16_TX_BD_CNT \
60 (TOTAL_DESC - priv->hw_params->tx_queues * priv->hw_params->tx_bds_per_q)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080061
62#define RX_BUF_LENGTH 2048
63#define SKB_ALIGNMENT 32
64
65/* Tx/Rx DMA register offset, skip 256 descriptors */
66#define WORDS_PER_BD(p) (p->hw_params->words_per_bd)
67#define DMA_DESC_SIZE (WORDS_PER_BD(priv) * sizeof(u32))
68
69#define GENET_TDMA_REG_OFF (priv->hw_params->tdma_offset + \
70 TOTAL_DESC * DMA_DESC_SIZE)
71
72#define GENET_RDMA_REG_OFF (priv->hw_params->rdma_offset + \
73 TOTAL_DESC * DMA_DESC_SIZE)
74
75static inline void dmadesc_set_length_status(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -070076 void __iomem *d, u32 value)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080077{
78 __raw_writel(value, d + DMA_DESC_LENGTH_STATUS);
79}
80
81static inline u32 dmadesc_get_length_status(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -070082 void __iomem *d)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080083{
84 return __raw_readl(d + DMA_DESC_LENGTH_STATUS);
85}
86
87static inline void dmadesc_set_addr(struct bcmgenet_priv *priv,
88 void __iomem *d,
89 dma_addr_t addr)
90{
91 __raw_writel(lower_32_bits(addr), d + DMA_DESC_ADDRESS_LO);
92
93 /* Register writes to GISB bus can take couple hundred nanoseconds
94 * and are done for each packet, save these expensive writes unless
Brian Norris7fc527f2014-07-29 14:34:14 -070095 * the platform is explicitly configured for 64-bits/LPAE.
Florian Fainelli1c1008c2014-02-13 16:08:47 -080096 */
97#ifdef CONFIG_PHYS_ADDR_T_64BIT
98 if (priv->hw_params->flags & GENET_HAS_40BITS)
99 __raw_writel(upper_32_bits(addr), d + DMA_DESC_ADDRESS_HI);
100#endif
101}
102
103/* Combined address + length/status setter */
104static inline void dmadesc_set(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700105 void __iomem *d, dma_addr_t addr, u32 val)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800106{
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800107 dmadesc_set_addr(priv, d, addr);
Petri Gynther7ee40622016-04-05 14:00:01 -0700108 dmadesc_set_length_status(priv, d, val);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800109}
110
111static inline dma_addr_t dmadesc_get_addr(struct bcmgenet_priv *priv,
112 void __iomem *d)
113{
114 dma_addr_t addr;
115
116 addr = __raw_readl(d + DMA_DESC_ADDRESS_LO);
117
118 /* Register writes to GISB bus can take couple hundred nanoseconds
119 * and are done for each packet, save these expensive writes unless
Brian Norris7fc527f2014-07-29 14:34:14 -0700120 * the platform is explicitly configured for 64-bits/LPAE.
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800121 */
122#ifdef CONFIG_PHYS_ADDR_T_64BIT
123 if (priv->hw_params->flags & GENET_HAS_40BITS)
124 addr |= (u64)__raw_readl(d + DMA_DESC_ADDRESS_HI) << 32;
125#endif
126 return addr;
127}
128
129#define GENET_VER_FMT "%1d.%1d EPHY: 0x%04x"
130
131#define GENET_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
132 NETIF_MSG_LINK)
133
134static inline u32 bcmgenet_rbuf_ctrl_get(struct bcmgenet_priv *priv)
135{
136 if (GENET_IS_V1(priv))
137 return bcmgenet_rbuf_readl(priv, RBUF_FLUSH_CTRL_V1);
138 else
139 return bcmgenet_sys_readl(priv, SYS_RBUF_FLUSH_CTRL);
140}
141
142static inline void bcmgenet_rbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
143{
144 if (GENET_IS_V1(priv))
145 bcmgenet_rbuf_writel(priv, val, RBUF_FLUSH_CTRL_V1);
146 else
147 bcmgenet_sys_writel(priv, val, SYS_RBUF_FLUSH_CTRL);
148}
149
150/* These macros are defined to deal with register map change
151 * between GENET1.1 and GENET2. Only those currently being used
152 * by driver are defined.
153 */
154static inline u32 bcmgenet_tbuf_ctrl_get(struct bcmgenet_priv *priv)
155{
156 if (GENET_IS_V1(priv))
157 return bcmgenet_rbuf_readl(priv, TBUF_CTRL_V1);
158 else
159 return __raw_readl(priv->base +
160 priv->hw_params->tbuf_offset + TBUF_CTRL);
161}
162
163static inline void bcmgenet_tbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
164{
165 if (GENET_IS_V1(priv))
166 bcmgenet_rbuf_writel(priv, val, TBUF_CTRL_V1);
167 else
168 __raw_writel(val, priv->base +
169 priv->hw_params->tbuf_offset + TBUF_CTRL);
170}
171
172static inline u32 bcmgenet_bp_mc_get(struct bcmgenet_priv *priv)
173{
174 if (GENET_IS_V1(priv))
175 return bcmgenet_rbuf_readl(priv, TBUF_BP_MC_V1);
176 else
177 return __raw_readl(priv->base +
178 priv->hw_params->tbuf_offset + TBUF_BP_MC);
179}
180
181static inline void bcmgenet_bp_mc_set(struct bcmgenet_priv *priv, u32 val)
182{
183 if (GENET_IS_V1(priv))
184 bcmgenet_rbuf_writel(priv, val, TBUF_BP_MC_V1);
185 else
186 __raw_writel(val, priv->base +
187 priv->hw_params->tbuf_offset + TBUF_BP_MC);
188}
189
190/* RX/TX DMA register accessors */
191enum dma_reg {
192 DMA_RING_CFG = 0,
193 DMA_CTRL,
194 DMA_STATUS,
195 DMA_SCB_BURST_SIZE,
196 DMA_ARB_CTRL,
Petri Gynther37742162014-10-07 09:30:01 -0700197 DMA_PRIORITY_0,
198 DMA_PRIORITY_1,
199 DMA_PRIORITY_2,
Petri Gynther0034de42015-03-13 14:45:00 -0700200 DMA_INDEX2RING_0,
201 DMA_INDEX2RING_1,
202 DMA_INDEX2RING_2,
203 DMA_INDEX2RING_3,
204 DMA_INDEX2RING_4,
205 DMA_INDEX2RING_5,
206 DMA_INDEX2RING_6,
207 DMA_INDEX2RING_7,
Florian Fainelli4a296452015-09-16 16:47:40 -0700208 DMA_RING0_TIMEOUT,
209 DMA_RING1_TIMEOUT,
210 DMA_RING2_TIMEOUT,
211 DMA_RING3_TIMEOUT,
212 DMA_RING4_TIMEOUT,
213 DMA_RING5_TIMEOUT,
214 DMA_RING6_TIMEOUT,
215 DMA_RING7_TIMEOUT,
216 DMA_RING8_TIMEOUT,
217 DMA_RING9_TIMEOUT,
218 DMA_RING10_TIMEOUT,
219 DMA_RING11_TIMEOUT,
220 DMA_RING12_TIMEOUT,
221 DMA_RING13_TIMEOUT,
222 DMA_RING14_TIMEOUT,
223 DMA_RING15_TIMEOUT,
224 DMA_RING16_TIMEOUT,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800225};
226
227static const u8 bcmgenet_dma_regs_v3plus[] = {
228 [DMA_RING_CFG] = 0x00,
229 [DMA_CTRL] = 0x04,
230 [DMA_STATUS] = 0x08,
231 [DMA_SCB_BURST_SIZE] = 0x0C,
232 [DMA_ARB_CTRL] = 0x2C,
Petri Gynther37742162014-10-07 09:30:01 -0700233 [DMA_PRIORITY_0] = 0x30,
234 [DMA_PRIORITY_1] = 0x34,
235 [DMA_PRIORITY_2] = 0x38,
Florian Fainelli4a296452015-09-16 16:47:40 -0700236 [DMA_RING0_TIMEOUT] = 0x2C,
237 [DMA_RING1_TIMEOUT] = 0x30,
238 [DMA_RING2_TIMEOUT] = 0x34,
239 [DMA_RING3_TIMEOUT] = 0x38,
240 [DMA_RING4_TIMEOUT] = 0x3c,
241 [DMA_RING5_TIMEOUT] = 0x40,
242 [DMA_RING6_TIMEOUT] = 0x44,
243 [DMA_RING7_TIMEOUT] = 0x48,
244 [DMA_RING8_TIMEOUT] = 0x4c,
245 [DMA_RING9_TIMEOUT] = 0x50,
246 [DMA_RING10_TIMEOUT] = 0x54,
247 [DMA_RING11_TIMEOUT] = 0x58,
248 [DMA_RING12_TIMEOUT] = 0x5c,
249 [DMA_RING13_TIMEOUT] = 0x60,
250 [DMA_RING14_TIMEOUT] = 0x64,
251 [DMA_RING15_TIMEOUT] = 0x68,
252 [DMA_RING16_TIMEOUT] = 0x6C,
Petri Gynther0034de42015-03-13 14:45:00 -0700253 [DMA_INDEX2RING_0] = 0x70,
254 [DMA_INDEX2RING_1] = 0x74,
255 [DMA_INDEX2RING_2] = 0x78,
256 [DMA_INDEX2RING_3] = 0x7C,
257 [DMA_INDEX2RING_4] = 0x80,
258 [DMA_INDEX2RING_5] = 0x84,
259 [DMA_INDEX2RING_6] = 0x88,
260 [DMA_INDEX2RING_7] = 0x8C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800261};
262
263static const u8 bcmgenet_dma_regs_v2[] = {
264 [DMA_RING_CFG] = 0x00,
265 [DMA_CTRL] = 0x04,
266 [DMA_STATUS] = 0x08,
267 [DMA_SCB_BURST_SIZE] = 0x0C,
268 [DMA_ARB_CTRL] = 0x30,
Petri Gynther37742162014-10-07 09:30:01 -0700269 [DMA_PRIORITY_0] = 0x34,
270 [DMA_PRIORITY_1] = 0x38,
271 [DMA_PRIORITY_2] = 0x3C,
Florian Fainelli4a296452015-09-16 16:47:40 -0700272 [DMA_RING0_TIMEOUT] = 0x2C,
273 [DMA_RING1_TIMEOUT] = 0x30,
274 [DMA_RING2_TIMEOUT] = 0x34,
275 [DMA_RING3_TIMEOUT] = 0x38,
276 [DMA_RING4_TIMEOUT] = 0x3c,
277 [DMA_RING5_TIMEOUT] = 0x40,
278 [DMA_RING6_TIMEOUT] = 0x44,
279 [DMA_RING7_TIMEOUT] = 0x48,
280 [DMA_RING8_TIMEOUT] = 0x4c,
281 [DMA_RING9_TIMEOUT] = 0x50,
282 [DMA_RING10_TIMEOUT] = 0x54,
283 [DMA_RING11_TIMEOUT] = 0x58,
284 [DMA_RING12_TIMEOUT] = 0x5c,
285 [DMA_RING13_TIMEOUT] = 0x60,
286 [DMA_RING14_TIMEOUT] = 0x64,
287 [DMA_RING15_TIMEOUT] = 0x68,
288 [DMA_RING16_TIMEOUT] = 0x6C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800289};
290
291static const u8 bcmgenet_dma_regs_v1[] = {
292 [DMA_CTRL] = 0x00,
293 [DMA_STATUS] = 0x04,
294 [DMA_SCB_BURST_SIZE] = 0x0C,
295 [DMA_ARB_CTRL] = 0x30,
Petri Gynther37742162014-10-07 09:30:01 -0700296 [DMA_PRIORITY_0] = 0x34,
297 [DMA_PRIORITY_1] = 0x38,
298 [DMA_PRIORITY_2] = 0x3C,
Florian Fainelli4a296452015-09-16 16:47:40 -0700299 [DMA_RING0_TIMEOUT] = 0x2C,
300 [DMA_RING1_TIMEOUT] = 0x30,
301 [DMA_RING2_TIMEOUT] = 0x34,
302 [DMA_RING3_TIMEOUT] = 0x38,
303 [DMA_RING4_TIMEOUT] = 0x3c,
304 [DMA_RING5_TIMEOUT] = 0x40,
305 [DMA_RING6_TIMEOUT] = 0x44,
306 [DMA_RING7_TIMEOUT] = 0x48,
307 [DMA_RING8_TIMEOUT] = 0x4c,
308 [DMA_RING9_TIMEOUT] = 0x50,
309 [DMA_RING10_TIMEOUT] = 0x54,
310 [DMA_RING11_TIMEOUT] = 0x58,
311 [DMA_RING12_TIMEOUT] = 0x5c,
312 [DMA_RING13_TIMEOUT] = 0x60,
313 [DMA_RING14_TIMEOUT] = 0x64,
314 [DMA_RING15_TIMEOUT] = 0x68,
315 [DMA_RING16_TIMEOUT] = 0x6C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800316};
317
318/* Set at runtime once bcmgenet version is known */
319static const u8 *bcmgenet_dma_regs;
320
321static inline struct bcmgenet_priv *dev_to_priv(struct device *dev)
322{
323 return netdev_priv(dev_get_drvdata(dev));
324}
325
326static inline u32 bcmgenet_tdma_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700327 enum dma_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800328{
329 return __raw_readl(priv->base + GENET_TDMA_REG_OFF +
330 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
331}
332
333static inline void bcmgenet_tdma_writel(struct bcmgenet_priv *priv,
334 u32 val, enum dma_reg r)
335{
336 __raw_writel(val, priv->base + GENET_TDMA_REG_OFF +
337 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
338}
339
340static inline u32 bcmgenet_rdma_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700341 enum dma_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800342{
343 return __raw_readl(priv->base + GENET_RDMA_REG_OFF +
344 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
345}
346
347static inline void bcmgenet_rdma_writel(struct bcmgenet_priv *priv,
348 u32 val, enum dma_reg r)
349{
350 __raw_writel(val, priv->base + GENET_RDMA_REG_OFF +
351 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
352}
353
354/* RDMA/TDMA ring registers and accessors
355 * we merge the common fields and just prefix with T/D the registers
356 * having different meaning depending on the direction
357 */
358enum dma_ring_reg {
359 TDMA_READ_PTR = 0,
360 RDMA_WRITE_PTR = TDMA_READ_PTR,
361 TDMA_READ_PTR_HI,
362 RDMA_WRITE_PTR_HI = TDMA_READ_PTR_HI,
363 TDMA_CONS_INDEX,
364 RDMA_PROD_INDEX = TDMA_CONS_INDEX,
365 TDMA_PROD_INDEX,
366 RDMA_CONS_INDEX = TDMA_PROD_INDEX,
367 DMA_RING_BUF_SIZE,
368 DMA_START_ADDR,
369 DMA_START_ADDR_HI,
370 DMA_END_ADDR,
371 DMA_END_ADDR_HI,
372 DMA_MBUF_DONE_THRESH,
373 TDMA_FLOW_PERIOD,
374 RDMA_XON_XOFF_THRESH = TDMA_FLOW_PERIOD,
375 TDMA_WRITE_PTR,
376 RDMA_READ_PTR = TDMA_WRITE_PTR,
377 TDMA_WRITE_PTR_HI,
378 RDMA_READ_PTR_HI = TDMA_WRITE_PTR_HI
379};
380
381/* GENET v4 supports 40-bits pointer addressing
382 * for obvious reasons the LO and HI word parts
383 * are contiguous, but this offsets the other
384 * registers.
385 */
386static const u8 genet_dma_ring_regs_v4[] = {
387 [TDMA_READ_PTR] = 0x00,
388 [TDMA_READ_PTR_HI] = 0x04,
389 [TDMA_CONS_INDEX] = 0x08,
390 [TDMA_PROD_INDEX] = 0x0C,
391 [DMA_RING_BUF_SIZE] = 0x10,
392 [DMA_START_ADDR] = 0x14,
393 [DMA_START_ADDR_HI] = 0x18,
394 [DMA_END_ADDR] = 0x1C,
395 [DMA_END_ADDR_HI] = 0x20,
396 [DMA_MBUF_DONE_THRESH] = 0x24,
397 [TDMA_FLOW_PERIOD] = 0x28,
398 [TDMA_WRITE_PTR] = 0x2C,
399 [TDMA_WRITE_PTR_HI] = 0x30,
400};
401
402static const u8 genet_dma_ring_regs_v123[] = {
403 [TDMA_READ_PTR] = 0x00,
404 [TDMA_CONS_INDEX] = 0x04,
405 [TDMA_PROD_INDEX] = 0x08,
406 [DMA_RING_BUF_SIZE] = 0x0C,
407 [DMA_START_ADDR] = 0x10,
408 [DMA_END_ADDR] = 0x14,
409 [DMA_MBUF_DONE_THRESH] = 0x18,
410 [TDMA_FLOW_PERIOD] = 0x1C,
411 [TDMA_WRITE_PTR] = 0x20,
412};
413
414/* Set at runtime once GENET version is known */
415static const u8 *genet_dma_ring_regs;
416
417static inline u32 bcmgenet_tdma_ring_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700418 unsigned int ring,
419 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800420{
421 return __raw_readl(priv->base + GENET_TDMA_REG_OFF +
422 (DMA_RING_SIZE * ring) +
423 genet_dma_ring_regs[r]);
424}
425
426static inline void bcmgenet_tdma_ring_writel(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700427 unsigned int ring, u32 val,
428 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800429{
430 __raw_writel(val, priv->base + GENET_TDMA_REG_OFF +
431 (DMA_RING_SIZE * ring) +
432 genet_dma_ring_regs[r]);
433}
434
435static inline u32 bcmgenet_rdma_ring_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700436 unsigned int ring,
437 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800438{
439 return __raw_readl(priv->base + GENET_RDMA_REG_OFF +
440 (DMA_RING_SIZE * ring) +
441 genet_dma_ring_regs[r]);
442}
443
444static inline void bcmgenet_rdma_ring_writel(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700445 unsigned int ring, u32 val,
446 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800447{
448 __raw_writel(val, priv->base + GENET_RDMA_REG_OFF +
449 (DMA_RING_SIZE * ring) +
450 genet_dma_ring_regs[r]);
451}
452
Edwin Chan89316fa2017-03-09 16:58:49 -0800453static int bcmgenet_begin(struct net_device *dev)
454{
455 struct bcmgenet_priv *priv = netdev_priv(dev);
456
457 /* Turn on the clock */
458 return clk_prepare_enable(priv->clk);
459}
460
461static void bcmgenet_complete(struct net_device *dev)
462{
463 struct bcmgenet_priv *priv = netdev_priv(dev);
464
465 /* Turn off the clock */
466 clk_disable_unprepare(priv->clk);
467}
468
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200469static int bcmgenet_get_link_ksettings(struct net_device *dev,
470 struct ethtool_link_ksettings *cmd)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200471{
Florian Fainelli0299b6a2016-09-26 22:31:56 +0200472 struct bcmgenet_priv *priv = netdev_priv(dev);
473
Philippe Reynesbac65c42016-07-09 00:54:47 +0200474 if (!netif_running(dev))
475 return -EINVAL;
476
Florian Fainelli0299b6a2016-09-26 22:31:56 +0200477 if (!priv->phydev)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200478 return -ENODEV;
479
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +0300480 phy_ethtool_ksettings_get(priv->phydev, cmd);
481
482 return 0;
Philippe Reynesbac65c42016-07-09 00:54:47 +0200483}
484
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200485static int bcmgenet_set_link_ksettings(struct net_device *dev,
486 const struct ethtool_link_ksettings *cmd)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200487{
Florian Fainelli0299b6a2016-09-26 22:31:56 +0200488 struct bcmgenet_priv *priv = netdev_priv(dev);
489
Philippe Reynesbac65c42016-07-09 00:54:47 +0200490 if (!netif_running(dev))
491 return -EINVAL;
492
Florian Fainelli0299b6a2016-09-26 22:31:56 +0200493 if (!priv->phydev)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200494 return -ENODEV;
495
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200496 return phy_ethtool_ksettings_set(priv->phydev, cmd);
Philippe Reynesbac65c42016-07-09 00:54:47 +0200497}
498
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800499static int bcmgenet_set_rx_csum(struct net_device *dev,
500 netdev_features_t wanted)
501{
502 struct bcmgenet_priv *priv = netdev_priv(dev);
503 u32 rbuf_chk_ctrl;
504 bool rx_csum_en;
505
506 rx_csum_en = !!(wanted & NETIF_F_RXCSUM);
507
508 rbuf_chk_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CHK_CTRL);
509
510 /* enable rx checksumming */
511 if (rx_csum_en)
512 rbuf_chk_ctrl |= RBUF_RXCHK_EN;
513 else
514 rbuf_chk_ctrl &= ~RBUF_RXCHK_EN;
515 priv->desc_rxchk_en = rx_csum_en;
Florian Fainelliebe5e3c2014-03-26 21:18:39 -0700516
517 /* If UniMAC forwards CRC, we need to skip over it to get
518 * a valid CHK bit to be set in the per-packet status word
519 */
520 if (rx_csum_en && priv->crc_fwd_en)
521 rbuf_chk_ctrl |= RBUF_SKIP_FCS;
522 else
523 rbuf_chk_ctrl &= ~RBUF_SKIP_FCS;
524
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800525 bcmgenet_rbuf_writel(priv, rbuf_chk_ctrl, RBUF_CHK_CTRL);
526
527 return 0;
528}
529
530static int bcmgenet_set_tx_csum(struct net_device *dev,
531 netdev_features_t wanted)
532{
533 struct bcmgenet_priv *priv = netdev_priv(dev);
534 bool desc_64b_en;
535 u32 tbuf_ctrl, rbuf_ctrl;
536
537 tbuf_ctrl = bcmgenet_tbuf_ctrl_get(priv);
538 rbuf_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
539
540 desc_64b_en = !!(wanted & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
541
542 /* enable 64 bytes descriptor in both directions (RBUF and TBUF) */
543 if (desc_64b_en) {
544 tbuf_ctrl |= RBUF_64B_EN;
545 rbuf_ctrl |= RBUF_64B_EN;
546 } else {
547 tbuf_ctrl &= ~RBUF_64B_EN;
548 rbuf_ctrl &= ~RBUF_64B_EN;
549 }
550 priv->desc_64b_en = desc_64b_en;
551
552 bcmgenet_tbuf_ctrl_set(priv, tbuf_ctrl);
553 bcmgenet_rbuf_writel(priv, rbuf_ctrl, RBUF_CTRL);
554
555 return 0;
556}
557
558static int bcmgenet_set_features(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700559 netdev_features_t features)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800560{
561 netdev_features_t changed = features ^ dev->features;
562 netdev_features_t wanted = dev->wanted_features;
563 int ret = 0;
564
565 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
566 ret = bcmgenet_set_tx_csum(dev, wanted);
567 if (changed & (NETIF_F_RXCSUM))
568 ret = bcmgenet_set_rx_csum(dev, wanted);
569
570 return ret;
571}
572
573static u32 bcmgenet_get_msglevel(struct net_device *dev)
574{
575 struct bcmgenet_priv *priv = netdev_priv(dev);
576
577 return priv->msg_enable;
578}
579
580static void bcmgenet_set_msglevel(struct net_device *dev, u32 level)
581{
582 struct bcmgenet_priv *priv = netdev_priv(dev);
583
584 priv->msg_enable = level;
585}
586
Florian Fainelli2f913072015-09-16 16:47:39 -0700587static int bcmgenet_get_coalesce(struct net_device *dev,
588 struct ethtool_coalesce *ec)
589{
590 struct bcmgenet_priv *priv = netdev_priv(dev);
591
592 ec->tx_max_coalesced_frames =
593 bcmgenet_tdma_ring_readl(priv, DESC_INDEX,
594 DMA_MBUF_DONE_THRESH);
Florian Fainelli4a296452015-09-16 16:47:40 -0700595 ec->rx_max_coalesced_frames =
596 bcmgenet_rdma_ring_readl(priv, DESC_INDEX,
597 DMA_MBUF_DONE_THRESH);
598 ec->rx_coalesce_usecs =
599 bcmgenet_rdma_readl(priv, DMA_RING16_TIMEOUT) * 8192 / 1000;
Florian Fainelli2f913072015-09-16 16:47:39 -0700600
601 return 0;
602}
603
604static int bcmgenet_set_coalesce(struct net_device *dev,
605 struct ethtool_coalesce *ec)
606{
607 struct bcmgenet_priv *priv = netdev_priv(dev);
608 unsigned int i;
Florian Fainelli4a296452015-09-16 16:47:40 -0700609 u32 reg;
Florian Fainelli2f913072015-09-16 16:47:39 -0700610
Florian Fainelli4a296452015-09-16 16:47:40 -0700611 /* Base system clock is 125Mhz, DMA timeout is this reference clock
612 * divided by 1024, which yields roughly 8.192us, our maximum value
613 * has to fit in the DMA_TIMEOUT_MASK (16 bits)
614 */
Florian Fainelli2f913072015-09-16 16:47:39 -0700615 if (ec->tx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
Florian Fainelli4a296452015-09-16 16:47:40 -0700616 ec->tx_max_coalesced_frames == 0 ||
617 ec->rx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
618 ec->rx_coalesce_usecs > (DMA_TIMEOUT_MASK * 8) + 1)
619 return -EINVAL;
620
621 if (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0)
Florian Fainelli2f913072015-09-16 16:47:39 -0700622 return -EINVAL;
623
624 /* GENET TDMA hardware does not support a configurable timeout, but will
625 * always generate an interrupt either after MBDONE packets have been
Doug Berger556c2cf2017-03-13 17:41:34 -0700626 * transmitted, or when the ring is empty.
Florian Fainelli2f913072015-09-16 16:47:39 -0700627 */
628 if (ec->tx_coalesce_usecs || ec->tx_coalesce_usecs_high ||
Florian Fainelli852bcaf2015-09-18 14:16:53 -0700629 ec->tx_coalesce_usecs_irq || ec->tx_coalesce_usecs_low)
Florian Fainelli2f913072015-09-16 16:47:39 -0700630 return -EOPNOTSUPP;
631
632 /* Program all TX queues with the same values, as there is no
633 * ethtool knob to do coalescing on a per-queue basis
634 */
635 for (i = 0; i < priv->hw_params->tx_queues; i++)
636 bcmgenet_tdma_ring_writel(priv, i,
637 ec->tx_max_coalesced_frames,
638 DMA_MBUF_DONE_THRESH);
639 bcmgenet_tdma_ring_writel(priv, DESC_INDEX,
640 ec->tx_max_coalesced_frames,
641 DMA_MBUF_DONE_THRESH);
642
Florian Fainelli4a296452015-09-16 16:47:40 -0700643 for (i = 0; i < priv->hw_params->rx_queues; i++) {
644 bcmgenet_rdma_ring_writel(priv, i,
645 ec->rx_max_coalesced_frames,
646 DMA_MBUF_DONE_THRESH);
647
648 reg = bcmgenet_rdma_readl(priv, DMA_RING0_TIMEOUT + i);
649 reg &= ~DMA_TIMEOUT_MASK;
650 reg |= DIV_ROUND_UP(ec->rx_coalesce_usecs * 1000, 8192);
651 bcmgenet_rdma_writel(priv, reg, DMA_RING0_TIMEOUT + i);
652 }
653
654 bcmgenet_rdma_ring_writel(priv, DESC_INDEX,
655 ec->rx_max_coalesced_frames,
656 DMA_MBUF_DONE_THRESH);
657
658 reg = bcmgenet_rdma_readl(priv, DMA_RING16_TIMEOUT);
659 reg &= ~DMA_TIMEOUT_MASK;
660 reg |= DIV_ROUND_UP(ec->rx_coalesce_usecs * 1000, 8192);
661 bcmgenet_rdma_writel(priv, reg, DMA_RING16_TIMEOUT);
662
Florian Fainelli2f913072015-09-16 16:47:39 -0700663 return 0;
664}
665
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800666/* standard ethtool support functions. */
667enum bcmgenet_stat_type {
668 BCMGENET_STAT_NETDEV = -1,
669 BCMGENET_STAT_MIB_RX,
670 BCMGENET_STAT_MIB_TX,
671 BCMGENET_STAT_RUNT,
672 BCMGENET_STAT_MISC,
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800673 BCMGENET_STAT_SOFT,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800674};
675
676struct bcmgenet_stats {
677 char stat_string[ETH_GSTRING_LEN];
678 int stat_sizeof;
679 int stat_offset;
680 enum bcmgenet_stat_type type;
681 /* reg offset from UMAC base for misc counters */
682 u16 reg_offset;
683};
684
685#define STAT_NETDEV(m) { \
686 .stat_string = __stringify(m), \
687 .stat_sizeof = sizeof(((struct net_device_stats *)0)->m), \
688 .stat_offset = offsetof(struct net_device_stats, m), \
689 .type = BCMGENET_STAT_NETDEV, \
690}
691
692#define STAT_GENET_MIB(str, m, _type) { \
693 .stat_string = str, \
694 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
695 .stat_offset = offsetof(struct bcmgenet_priv, m), \
696 .type = _type, \
697}
698
699#define STAT_GENET_MIB_RX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_RX)
700#define STAT_GENET_MIB_TX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_TX)
701#define STAT_GENET_RUNT(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_RUNT)
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800702#define STAT_GENET_SOFT_MIB(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_SOFT)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800703
704#define STAT_GENET_MISC(str, m, offset) { \
705 .stat_string = str, \
706 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
707 .stat_offset = offsetof(struct bcmgenet_priv, m), \
708 .type = BCMGENET_STAT_MISC, \
709 .reg_offset = offset, \
710}
711
Florian Fainelli37a30b42017-03-16 10:27:08 -0700712#define STAT_GENET_Q(num) \
713 STAT_GENET_SOFT_MIB("txq" __stringify(num) "_packets", \
714 tx_rings[num].packets), \
715 STAT_GENET_SOFT_MIB("txq" __stringify(num) "_bytes", \
716 tx_rings[num].bytes), \
717 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_bytes", \
718 rx_rings[num].bytes), \
719 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_packets", \
720 rx_rings[num].packets), \
721 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_errors", \
722 rx_rings[num].errors), \
723 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_dropped", \
724 rx_rings[num].dropped)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800725
726/* There is a 0xC gap between the end of RX and beginning of TX stats and then
727 * between the end of TX stats and the beginning of the RX RUNT
728 */
729#define BCMGENET_STAT_OFFSET 0xc
730
731/* Hardware counters must be kept in sync because the order/offset
732 * is important here (order in structure declaration = order in hardware)
733 */
734static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
735 /* general stats */
736 STAT_NETDEV(rx_packets),
737 STAT_NETDEV(tx_packets),
738 STAT_NETDEV(rx_bytes),
739 STAT_NETDEV(tx_bytes),
740 STAT_NETDEV(rx_errors),
741 STAT_NETDEV(tx_errors),
742 STAT_NETDEV(rx_dropped),
743 STAT_NETDEV(tx_dropped),
744 STAT_NETDEV(multicast),
745 /* UniMAC RSV counters */
746 STAT_GENET_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
747 STAT_GENET_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
748 STAT_GENET_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
749 STAT_GENET_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
750 STAT_GENET_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
751 STAT_GENET_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
752 STAT_GENET_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
753 STAT_GENET_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
754 STAT_GENET_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
755 STAT_GENET_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
756 STAT_GENET_MIB_RX("rx_pkts", mib.rx.pkt),
757 STAT_GENET_MIB_RX("rx_bytes", mib.rx.bytes),
758 STAT_GENET_MIB_RX("rx_multicast", mib.rx.mca),
759 STAT_GENET_MIB_RX("rx_broadcast", mib.rx.bca),
760 STAT_GENET_MIB_RX("rx_fcs", mib.rx.fcs),
761 STAT_GENET_MIB_RX("rx_control", mib.rx.cf),
762 STAT_GENET_MIB_RX("rx_pause", mib.rx.pf),
763 STAT_GENET_MIB_RX("rx_unknown", mib.rx.uo),
764 STAT_GENET_MIB_RX("rx_align", mib.rx.aln),
765 STAT_GENET_MIB_RX("rx_outrange", mib.rx.flr),
766 STAT_GENET_MIB_RX("rx_code", mib.rx.cde),
767 STAT_GENET_MIB_RX("rx_carrier", mib.rx.fcr),
768 STAT_GENET_MIB_RX("rx_oversize", mib.rx.ovr),
769 STAT_GENET_MIB_RX("rx_jabber", mib.rx.jbr),
770 STAT_GENET_MIB_RX("rx_mtu_err", mib.rx.mtue),
771 STAT_GENET_MIB_RX("rx_good_pkts", mib.rx.pok),
772 STAT_GENET_MIB_RX("rx_unicast", mib.rx.uc),
773 STAT_GENET_MIB_RX("rx_ppp", mib.rx.ppp),
774 STAT_GENET_MIB_RX("rx_crc", mib.rx.rcrc),
775 /* UniMAC TSV counters */
776 STAT_GENET_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
777 STAT_GENET_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
778 STAT_GENET_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
779 STAT_GENET_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
780 STAT_GENET_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
781 STAT_GENET_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
782 STAT_GENET_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
783 STAT_GENET_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
784 STAT_GENET_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
785 STAT_GENET_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
786 STAT_GENET_MIB_TX("tx_pkts", mib.tx.pkts),
787 STAT_GENET_MIB_TX("tx_multicast", mib.tx.mca),
788 STAT_GENET_MIB_TX("tx_broadcast", mib.tx.bca),
789 STAT_GENET_MIB_TX("tx_pause", mib.tx.pf),
790 STAT_GENET_MIB_TX("tx_control", mib.tx.cf),
791 STAT_GENET_MIB_TX("tx_fcs_err", mib.tx.fcs),
792 STAT_GENET_MIB_TX("tx_oversize", mib.tx.ovr),
793 STAT_GENET_MIB_TX("tx_defer", mib.tx.drf),
794 STAT_GENET_MIB_TX("tx_excess_defer", mib.tx.edf),
795 STAT_GENET_MIB_TX("tx_single_col", mib.tx.scl),
796 STAT_GENET_MIB_TX("tx_multi_col", mib.tx.mcl),
797 STAT_GENET_MIB_TX("tx_late_col", mib.tx.lcl),
798 STAT_GENET_MIB_TX("tx_excess_col", mib.tx.ecl),
799 STAT_GENET_MIB_TX("tx_frags", mib.tx.frg),
800 STAT_GENET_MIB_TX("tx_total_col", mib.tx.ncl),
801 STAT_GENET_MIB_TX("tx_jabber", mib.tx.jbr),
802 STAT_GENET_MIB_TX("tx_bytes", mib.tx.bytes),
803 STAT_GENET_MIB_TX("tx_good_pkts", mib.tx.pok),
804 STAT_GENET_MIB_TX("tx_unicast", mib.tx.uc),
805 /* UniMAC RUNT counters */
806 STAT_GENET_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
807 STAT_GENET_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
808 STAT_GENET_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
809 STAT_GENET_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
810 /* Misc UniMAC counters */
811 STAT_GENET_MISC("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt,
Doug Bergerffff7132017-03-09 16:58:43 -0800812 UMAC_RBUF_OVFL_CNT_V1),
813 STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt,
814 UMAC_RBUF_ERR_CNT_V1),
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800815 STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT),
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800816 STAT_GENET_SOFT_MIB("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
817 STAT_GENET_SOFT_MIB("rx_dma_failed", mib.rx_dma_failed),
818 STAT_GENET_SOFT_MIB("tx_dma_failed", mib.tx_dma_failed),
Florian Fainelli37a30b42017-03-16 10:27:08 -0700819 /* Per TX queues */
820 STAT_GENET_Q(0),
821 STAT_GENET_Q(1),
822 STAT_GENET_Q(2),
823 STAT_GENET_Q(3),
824 STAT_GENET_Q(16),
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800825};
826
827#define BCMGENET_STATS_LEN ARRAY_SIZE(bcmgenet_gstrings_stats)
828
829static void bcmgenet_get_drvinfo(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700830 struct ethtool_drvinfo *info)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800831{
832 strlcpy(info->driver, "bcmgenet", sizeof(info->driver));
833 strlcpy(info->version, "v2.0", sizeof(info->version));
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800834}
835
836static int bcmgenet_get_sset_count(struct net_device *dev, int string_set)
837{
838 switch (string_set) {
839 case ETH_SS_STATS:
840 return BCMGENET_STATS_LEN;
841 default:
842 return -EOPNOTSUPP;
843 }
844}
845
Florian Fainellic91b7f62014-07-23 10:42:12 -0700846static void bcmgenet_get_strings(struct net_device *dev, u32 stringset,
847 u8 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800848{
849 int i;
850
851 switch (stringset) {
852 case ETH_SS_STATS:
853 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
854 memcpy(data + i * ETH_GSTRING_LEN,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700855 bcmgenet_gstrings_stats[i].stat_string,
856 ETH_GSTRING_LEN);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800857 }
858 break;
859 }
860}
861
Doug Bergerffff7132017-03-09 16:58:43 -0800862static u32 bcmgenet_update_stat_misc(struct bcmgenet_priv *priv, u16 offset)
863{
864 u16 new_offset;
865 u32 val;
866
867 switch (offset) {
868 case UMAC_RBUF_OVFL_CNT_V1:
869 if (GENET_IS_V2(priv))
870 new_offset = RBUF_OVFL_CNT_V2;
871 else
872 new_offset = RBUF_OVFL_CNT_V3PLUS;
873
874 val = bcmgenet_rbuf_readl(priv, new_offset);
875 /* clear if overflowed */
876 if (val == ~0)
877 bcmgenet_rbuf_writel(priv, 0, new_offset);
878 break;
879 case UMAC_RBUF_ERR_CNT_V1:
880 if (GENET_IS_V2(priv))
881 new_offset = RBUF_ERR_CNT_V2;
882 else
883 new_offset = RBUF_ERR_CNT_V3PLUS;
884
885 val = bcmgenet_rbuf_readl(priv, new_offset);
886 /* clear if overflowed */
887 if (val == ~0)
888 bcmgenet_rbuf_writel(priv, 0, new_offset);
889 break;
890 default:
891 val = bcmgenet_umac_readl(priv, offset);
892 /* clear if overflowed */
893 if (val == ~0)
894 bcmgenet_umac_writel(priv, 0, offset);
895 break;
896 }
897
898 return val;
899}
900
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800901static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv)
902{
903 int i, j = 0;
904
905 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
906 const struct bcmgenet_stats *s;
907 u8 offset = 0;
908 u32 val = 0;
909 char *p;
910
911 s = &bcmgenet_gstrings_stats[i];
912 switch (s->type) {
913 case BCMGENET_STAT_NETDEV:
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800914 case BCMGENET_STAT_SOFT:
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800915 continue;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800916 case BCMGENET_STAT_RUNT:
Doug Berger1ad3d222017-03-09 16:58:44 -0800917 offset += BCMGENET_STAT_OFFSET;
918 /* fall through */
919 case BCMGENET_STAT_MIB_TX:
920 offset += BCMGENET_STAT_OFFSET;
921 /* fall through */
922 case BCMGENET_STAT_MIB_RX:
Florian Fainellic91b7f62014-07-23 10:42:12 -0700923 val = bcmgenet_umac_readl(priv,
924 UMAC_MIB_START + j + offset);
Doug Berger1ad3d222017-03-09 16:58:44 -0800925 offset = 0; /* Reset Offset */
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800926 break;
927 case BCMGENET_STAT_MISC:
Doug Bergerffff7132017-03-09 16:58:43 -0800928 if (GENET_IS_V1(priv)) {
929 val = bcmgenet_umac_readl(priv, s->reg_offset);
930 /* clear if overflowed */
931 if (val == ~0)
932 bcmgenet_umac_writel(priv, 0,
933 s->reg_offset);
934 } else {
935 val = bcmgenet_update_stat_misc(priv,
936 s->reg_offset);
937 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800938 break;
939 }
940
941 j += s->stat_sizeof;
942 p = (char *)priv + s->stat_offset;
943 *(u32 *)p = val;
944 }
945}
946
947static void bcmgenet_get_ethtool_stats(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700948 struct ethtool_stats *stats,
949 u64 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800950{
951 struct bcmgenet_priv *priv = netdev_priv(dev);
952 int i;
953
954 if (netif_running(dev))
955 bcmgenet_update_mib_counters(priv);
956
957 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
958 const struct bcmgenet_stats *s;
959 char *p;
960
961 s = &bcmgenet_gstrings_stats[i];
962 if (s->type == BCMGENET_STAT_NETDEV)
963 p = (char *)&dev->stats;
964 else
965 p = (char *)priv;
966 p += s->stat_offset;
Eric Dumazet6517eb52016-04-15 10:47:52 -0700967 if (sizeof(unsigned long) != sizeof(u32) &&
968 s->stat_sizeof == sizeof(unsigned long))
969 data[i] = *(unsigned long *)p;
970 else
971 data[i] = *(u32 *)p;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800972 }
973}
974
Florian Fainelli6ef398e2014-11-25 21:16:35 -0800975static void bcmgenet_eee_enable_set(struct net_device *dev, bool enable)
976{
977 struct bcmgenet_priv *priv = netdev_priv(dev);
978 u32 off = priv->hw_params->tbuf_offset + TBUF_ENERGY_CTRL;
979 u32 reg;
980
981 if (enable && !priv->clk_eee_enabled) {
982 clk_prepare_enable(priv->clk_eee);
983 priv->clk_eee_enabled = true;
984 }
985
986 reg = bcmgenet_umac_readl(priv, UMAC_EEE_CTRL);
987 if (enable)
988 reg |= EEE_EN;
989 else
990 reg &= ~EEE_EN;
991 bcmgenet_umac_writel(priv, reg, UMAC_EEE_CTRL);
992
993 /* Enable EEE and switch to a 27Mhz clock automatically */
994 reg = __raw_readl(priv->base + off);
995 if (enable)
996 reg |= TBUF_EEE_EN | TBUF_PM_EN;
997 else
998 reg &= ~(TBUF_EEE_EN | TBUF_PM_EN);
999 __raw_writel(reg, priv->base + off);
1000
1001 /* Do the same for thing for RBUF */
1002 reg = bcmgenet_rbuf_readl(priv, RBUF_ENERGY_CTRL);
1003 if (enable)
1004 reg |= RBUF_EEE_EN | RBUF_PM_EN;
1005 else
1006 reg &= ~(RBUF_EEE_EN | RBUF_PM_EN);
1007 bcmgenet_rbuf_writel(priv, reg, RBUF_ENERGY_CTRL);
1008
1009 if (!enable && priv->clk_eee_enabled) {
1010 clk_disable_unprepare(priv->clk_eee);
1011 priv->clk_eee_enabled = false;
1012 }
1013
1014 priv->eee.eee_enabled = enable;
1015 priv->eee.eee_active = enable;
1016}
1017
1018static int bcmgenet_get_eee(struct net_device *dev, struct ethtool_eee *e)
1019{
1020 struct bcmgenet_priv *priv = netdev_priv(dev);
1021 struct ethtool_eee *p = &priv->eee;
1022
1023 if (GENET_IS_V1(priv))
1024 return -EOPNOTSUPP;
1025
1026 e->eee_enabled = p->eee_enabled;
1027 e->eee_active = p->eee_active;
1028 e->tx_lpi_timer = bcmgenet_umac_readl(priv, UMAC_EEE_LPI_TIMER);
1029
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001030 return phy_ethtool_get_eee(priv->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001031}
1032
1033static int bcmgenet_set_eee(struct net_device *dev, struct ethtool_eee *e)
1034{
1035 struct bcmgenet_priv *priv = netdev_priv(dev);
1036 struct ethtool_eee *p = &priv->eee;
1037 int ret = 0;
1038
1039 if (GENET_IS_V1(priv))
1040 return -EOPNOTSUPP;
1041
1042 p->eee_enabled = e->eee_enabled;
1043
1044 if (!p->eee_enabled) {
1045 bcmgenet_eee_enable_set(dev, false);
1046 } else {
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001047 ret = phy_init_eee(priv->phydev, 0);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001048 if (ret) {
1049 netif_err(priv, hw, dev, "EEE initialization failed\n");
1050 return ret;
1051 }
1052
1053 bcmgenet_umac_writel(priv, e->tx_lpi_timer, UMAC_EEE_LPI_TIMER);
1054 bcmgenet_eee_enable_set(dev, true);
1055 }
1056
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001057 return phy_ethtool_set_eee(priv->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001058}
1059
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001060/* standard ethtool support functions. */
Julia Lawall70591ab2016-08-31 09:30:45 +02001061static const struct ethtool_ops bcmgenet_ethtool_ops = {
Edwin Chan89316fa2017-03-09 16:58:49 -08001062 .begin = bcmgenet_begin,
1063 .complete = bcmgenet_complete,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001064 .get_strings = bcmgenet_get_strings,
1065 .get_sset_count = bcmgenet_get_sset_count,
1066 .get_ethtool_stats = bcmgenet_get_ethtool_stats,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001067 .get_drvinfo = bcmgenet_get_drvinfo,
1068 .get_link = ethtool_op_get_link,
1069 .get_msglevel = bcmgenet_get_msglevel,
1070 .set_msglevel = bcmgenet_set_msglevel,
Florian Fainelli06ba8372014-07-21 15:29:29 -07001071 .get_wol = bcmgenet_get_wol,
1072 .set_wol = bcmgenet_set_wol,
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001073 .get_eee = bcmgenet_get_eee,
1074 .set_eee = bcmgenet_set_eee,
Florian Fainelli016e7702016-11-15 10:06:38 -08001075 .nway_reset = phy_ethtool_nway_reset,
Florian Fainelli2f913072015-09-16 16:47:39 -07001076 .get_coalesce = bcmgenet_get_coalesce,
1077 .set_coalesce = bcmgenet_set_coalesce,
Philippe Reynesfa92bf02016-09-26 22:31:57 +02001078 .get_link_ksettings = bcmgenet_get_link_ksettings,
1079 .set_link_ksettings = bcmgenet_set_link_ksettings,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001080};
1081
1082/* Power down the unimac, based on mode. */
Florian Fainellica8cf342015-03-23 15:09:51 -07001083static int bcmgenet_power_down(struct bcmgenet_priv *priv,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001084 enum bcmgenet_power_mode mode)
1085{
Florian Fainellica8cf342015-03-23 15:09:51 -07001086 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001087 u32 reg;
1088
1089 switch (mode) {
1090 case GENET_POWER_CABLE_SENSE:
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001091 phy_detach(priv->phydev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001092 break;
1093
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001094 case GENET_POWER_WOL_MAGIC:
Florian Fainellica8cf342015-03-23 15:09:51 -07001095 ret = bcmgenet_wol_power_down_cfg(priv, mode);
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001096 break;
1097
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001098 case GENET_POWER_PASSIVE:
1099 /* Power down LED */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001100 if (priv->hw_params->flags & GENET_HAS_EXT) {
1101 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
Doug Berger42138082017-03-13 17:41:42 -07001102 if (GENET_IS_V5(priv))
1103 reg |= EXT_PWR_DOWN_PHY_EN |
1104 EXT_PWR_DOWN_PHY_RD |
1105 EXT_PWR_DOWN_PHY_SD |
1106 EXT_PWR_DOWN_PHY_RX |
1107 EXT_PWR_DOWN_PHY_TX |
1108 EXT_IDDQ_GLBL_PWR;
1109 else
1110 reg |= EXT_PWR_DOWN_PHY;
1111
1112 reg |= (EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001113 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
Florian Fainellia642c4f2015-03-23 15:09:56 -07001114
1115 bcmgenet_phy_power_set(priv->dev, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001116 }
1117 break;
1118 default:
1119 break;
1120 }
Florian Fainellica8cf342015-03-23 15:09:51 -07001121
1122 return 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001123}
1124
1125static void bcmgenet_power_up(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001126 enum bcmgenet_power_mode mode)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001127{
1128 u32 reg;
1129
1130 if (!(priv->hw_params->flags & GENET_HAS_EXT))
1131 return;
1132
1133 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
1134
1135 switch (mode) {
1136 case GENET_POWER_PASSIVE:
Doug Berger42138082017-03-13 17:41:42 -07001137 reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
1138 if (GENET_IS_V5(priv)) {
1139 reg &= ~(EXT_PWR_DOWN_PHY_EN |
1140 EXT_PWR_DOWN_PHY_RD |
1141 EXT_PWR_DOWN_PHY_SD |
1142 EXT_PWR_DOWN_PHY_RX |
1143 EXT_PWR_DOWN_PHY_TX |
1144 EXT_IDDQ_GLBL_PWR);
1145 reg |= EXT_PHY_RESET;
1146 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1147 mdelay(1);
1148
1149 reg &= ~EXT_PHY_RESET;
1150 } else {
1151 reg &= ~EXT_PWR_DOWN_PHY;
1152 reg |= EXT_PWR_DN_EN_LD;
1153 }
1154 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1155 bcmgenet_phy_power_set(priv->dev, true);
1156 bcmgenet_mii_reset(priv->dev);
1157 break;
1158
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001159 case GENET_POWER_CABLE_SENSE:
1160 /* enable APD */
Doug Berger42138082017-03-13 17:41:42 -07001161 if (!GENET_IS_V5(priv)) {
1162 reg |= EXT_PWR_DN_EN_LD;
1163 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1164 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001165 break;
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001166 case GENET_POWER_WOL_MAGIC:
1167 bcmgenet_wol_power_up_cfg(priv, mode);
1168 return;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001169 default:
1170 break;
1171 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001172}
1173
1174/* ioctl handle special commands that are not present in ethtool. */
1175static int bcmgenet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1176{
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001177 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001178
1179 if (!netif_running(dev))
1180 return -EINVAL;
1181
Doug Berger54fecff2017-03-13 17:41:39 -07001182 if (!priv->phydev)
1183 return -ENODEV;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001184
Doug Berger54fecff2017-03-13 17:41:39 -07001185 return phy_mii_ioctl(priv->phydev, rq, cmd);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001186}
1187
1188static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv,
1189 struct bcmgenet_tx_ring *ring)
1190{
1191 struct enet_cb *tx_cb_ptr;
1192
1193 tx_cb_ptr = ring->cbs;
1194 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
Petri Gynther014012a2015-02-23 11:00:45 -08001195
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001196 /* Advancing local write pointer */
1197 if (ring->write_ptr == ring->end_ptr)
1198 ring->write_ptr = ring->cb_ptr;
1199 else
1200 ring->write_ptr++;
1201
1202 return tx_cb_ptr;
1203}
1204
Doug Berger876dbad2017-07-14 16:12:09 -07001205static struct enet_cb *bcmgenet_put_txcb(struct bcmgenet_priv *priv,
1206 struct bcmgenet_tx_ring *ring)
1207{
1208 struct enet_cb *tx_cb_ptr;
1209
1210 tx_cb_ptr = ring->cbs;
1211 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
1212
1213 /* Rewinding local write pointer */
1214 if (ring->write_ptr == ring->cb_ptr)
1215 ring->write_ptr = ring->end_ptr;
1216 else
1217 ring->write_ptr--;
1218
1219 return tx_cb_ptr;
1220}
1221
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001222/* Simple helper to free a control block's resources */
1223static void bcmgenet_free_cb(struct enet_cb *cb)
1224{
1225 dev_kfree_skb_any(cb->skb);
1226 cb->skb = NULL;
1227 dma_unmap_addr_set(cb, dma_addr, 0);
1228}
1229
Petri Gynther4055eae2015-03-25 12:35:16 -07001230static inline void bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring *ring)
1231{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001232 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001233 INTRL2_CPU_MASK_SET);
1234}
1235
1236static inline void bcmgenet_rx_ring16_int_enable(struct bcmgenet_rx_ring *ring)
1237{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001238 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001239 INTRL2_CPU_MASK_CLEAR);
1240}
1241
1242static inline void bcmgenet_rx_ring_int_disable(struct bcmgenet_rx_ring *ring)
1243{
1244 bcmgenet_intrl2_1_writel(ring->priv,
1245 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1246 INTRL2_CPU_MASK_SET);
1247}
1248
1249static inline void bcmgenet_rx_ring_int_enable(struct bcmgenet_rx_ring *ring)
1250{
1251 bcmgenet_intrl2_1_writel(ring->priv,
1252 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1253 INTRL2_CPU_MASK_CLEAR);
1254}
1255
Petri Gynther9dbac282015-03-25 12:35:10 -07001256static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001257{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001258 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001259 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001260}
1261
Petri Gynther9dbac282015-03-25 12:35:10 -07001262static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001263{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001264 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001265 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001266}
1267
Petri Gynther9dbac282015-03-25 12:35:10 -07001268static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001269{
Petri Gynther9dbac282015-03-25 12:35:10 -07001270 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001271 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001272}
1273
Petri Gynther9dbac282015-03-25 12:35:10 -07001274static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001275{
Petri Gynther9dbac282015-03-25 12:35:10 -07001276 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001277 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001278}
1279
1280/* Unlocked version of the reclaim routine */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001281static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
1282 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001283{
1284 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001285 struct device *kdev = &priv->pdev->dev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001286 struct enet_cb *tx_cb_ptr;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001287 unsigned int pkts_compl = 0;
Petri Gynther55868122016-03-24 11:27:20 -07001288 unsigned int bytes_compl = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001289 unsigned int c_index;
Petri Gynther66d06752015-03-04 14:30:01 -08001290 unsigned int txbds_ready;
1291 unsigned int txbds_processed = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001292
Doug Bergerd5810ca2017-03-13 17:41:37 -07001293 /* Clear status before servicing to reduce spurious interrupts */
1294 if (ring->index == DESC_INDEX)
1295 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_TXDMA_DONE,
1296 INTRL2_CPU_CLEAR);
1297 else
1298 bcmgenet_intrl2_1_writel(priv, (1 << ring->index),
1299 INTRL2_CPU_CLEAR);
1300
Brian Norris7fc527f2014-07-29 14:34:14 -07001301 /* Compute how many buffers are transmitted since last xmit call */
Doug Bergerc298ede2017-03-13 17:41:33 -07001302 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX)
1303 & DMA_C_INDEX_MASK;
1304 txbds_ready = (c_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001305
1306 netif_dbg(priv, tx_done, dev,
Petri Gynther66d06752015-03-04 14:30:01 -08001307 "%s ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
1308 __func__, ring->index, ring->c_index, c_index, txbds_ready);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001309
1310 /* Reclaim transmitted buffers */
Petri Gynther66d06752015-03-04 14:30:01 -08001311 while (txbds_processed < txbds_ready) {
1312 tx_cb_ptr = &priv->tx_cbs[ring->clean_ptr];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001313 if (tx_cb_ptr->skb) {
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001314 pkts_compl++;
Petri Gynther55868122016-03-24 11:27:20 -07001315 bytes_compl += GENET_CB(tx_cb_ptr->skb)->bytes_sent;
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001316 dma_unmap_single(kdev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001317 dma_unmap_addr(tx_cb_ptr, dma_addr),
Eric Dumazeteee57722016-03-17 11:57:06 -07001318 dma_unmap_len(tx_cb_ptr, dma_len),
Florian Fainellic91b7f62014-07-23 10:42:12 -07001319 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001320 bcmgenet_free_cb(tx_cb_ptr);
1321 } else if (dma_unmap_addr(tx_cb_ptr, dma_addr)) {
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001322 dma_unmap_page(kdev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001323 dma_unmap_addr(tx_cb_ptr, dma_addr),
1324 dma_unmap_len(tx_cb_ptr, dma_len),
1325 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001326 dma_unmap_addr_set(tx_cb_ptr, dma_addr, 0);
1327 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001328
Petri Gynther66d06752015-03-04 14:30:01 -08001329 txbds_processed++;
1330 if (likely(ring->clean_ptr < ring->end_ptr))
1331 ring->clean_ptr++;
1332 else
1333 ring->clean_ptr = ring->cb_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001334 }
1335
Petri Gynther66d06752015-03-04 14:30:01 -08001336 ring->free_bds += txbds_processed;
Doug Bergerc4d453d2017-03-13 17:41:38 -07001337 ring->c_index = c_index;
Petri Gynther66d06752015-03-04 14:30:01 -08001338
Florian Fainelli37a30b42017-03-16 10:27:08 -07001339 ring->packets += pkts_compl;
1340 ring->bytes += bytes_compl;
Petri Gynther55868122016-03-24 11:27:20 -07001341
Doug Berger6d22fe12017-03-09 16:58:50 -08001342 netdev_tx_completed_queue(netdev_get_tx_queue(dev, ring->queue),
1343 pkts_compl, bytes_compl);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001344
Doug Bergerc4d453d2017-03-13 17:41:38 -07001345 return txbds_processed;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001346}
1347
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001348static unsigned int bcmgenet_tx_reclaim(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001349 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001350{
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001351 unsigned int released;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001352 unsigned long flags;
1353
1354 spin_lock_irqsave(&ring->lock, flags);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001355 released = __bcmgenet_tx_reclaim(dev, ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001356 spin_unlock_irqrestore(&ring->lock, flags);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001357
1358 return released;
1359}
1360
1361static int bcmgenet_tx_poll(struct napi_struct *napi, int budget)
1362{
1363 struct bcmgenet_tx_ring *ring =
1364 container_of(napi, struct bcmgenet_tx_ring, napi);
1365 unsigned int work_done = 0;
Doug Berger6d22fe12017-03-09 16:58:50 -08001366 struct netdev_queue *txq;
1367 unsigned long flags;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001368
Doug Berger6d22fe12017-03-09 16:58:50 -08001369 spin_lock_irqsave(&ring->lock, flags);
1370 work_done = __bcmgenet_tx_reclaim(ring->priv->dev, ring);
1371 if (ring->free_bds > (MAX_SKB_FRAGS + 1)) {
1372 txq = netdev_get_tx_queue(ring->priv->dev, ring->queue);
1373 netif_tx_wake_queue(txq);
1374 }
1375 spin_unlock_irqrestore(&ring->lock, flags);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001376
1377 if (work_done == 0) {
1378 napi_complete(napi);
Petri Gynther9dbac282015-03-25 12:35:10 -07001379 ring->int_enable(ring);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001380
1381 return 0;
1382 }
1383
1384 return budget;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001385}
1386
1387static void bcmgenet_tx_reclaim_all(struct net_device *dev)
1388{
1389 struct bcmgenet_priv *priv = netdev_priv(dev);
1390 int i;
1391
1392 if (netif_is_multiqueue(dev)) {
1393 for (i = 0; i < priv->hw_params->tx_queues; i++)
1394 bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
1395 }
1396
1397 bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
1398}
1399
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001400/* Reallocate the SKB to put enough headroom in front of it and insert
1401 * the transmit checksum offsets in the descriptors
1402 */
Petri Gyntherbc233332014-10-01 11:30:01 -07001403static struct sk_buff *bcmgenet_put_tx_csum(struct net_device *dev,
1404 struct sk_buff *skb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001405{
1406 struct status_64 *status = NULL;
1407 struct sk_buff *new_skb;
1408 u16 offset;
1409 u8 ip_proto;
1410 u16 ip_ver;
1411 u32 tx_csum_info;
1412
1413 if (unlikely(skb_headroom(skb) < sizeof(*status))) {
1414 /* If 64 byte status block enabled, must make sure skb has
1415 * enough headroom for us to insert 64B status block.
1416 */
1417 new_skb = skb_realloc_headroom(skb, sizeof(*status));
1418 dev_kfree_skb(skb);
1419 if (!new_skb) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001420 dev->stats.tx_dropped++;
Petri Gyntherbc233332014-10-01 11:30:01 -07001421 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001422 }
1423 skb = new_skb;
1424 }
1425
1426 skb_push(skb, sizeof(*status));
1427 status = (struct status_64 *)skb->data;
1428
1429 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1430 ip_ver = htons(skb->protocol);
1431 switch (ip_ver) {
1432 case ETH_P_IP:
1433 ip_proto = ip_hdr(skb)->protocol;
1434 break;
1435 case ETH_P_IPV6:
1436 ip_proto = ipv6_hdr(skb)->nexthdr;
1437 break;
1438 default:
Petri Gyntherbc233332014-10-01 11:30:01 -07001439 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001440 }
1441
1442 offset = skb_checksum_start_offset(skb) - sizeof(*status);
1443 tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
1444 (offset + skb->csum_offset);
1445
1446 /* Set the length valid bit for TCP and UDP and just set
1447 * the special UDP flag for IPv4, else just set to 0.
1448 */
1449 if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
1450 tx_csum_info |= STATUS_TX_CSUM_LV;
1451 if (ip_proto == IPPROTO_UDP && ip_ver == ETH_P_IP)
1452 tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
Florian Fainelli8900ea572014-07-23 10:42:14 -07001453 } else {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001454 tx_csum_info = 0;
Florian Fainelli8900ea572014-07-23 10:42:14 -07001455 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001456
1457 status->tx_csum_info = tx_csum_info;
1458 }
1459
Petri Gyntherbc233332014-10-01 11:30:01 -07001460 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001461}
1462
1463static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
1464{
1465 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Berger876dbad2017-07-14 16:12:09 -07001466 struct device *kdev = &priv->pdev->dev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001467 struct bcmgenet_tx_ring *ring = NULL;
Doug Berger876dbad2017-07-14 16:12:09 -07001468 struct enet_cb *tx_cb_ptr;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001469 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001470 unsigned long flags = 0;
1471 int nr_frags, index;
Doug Berger876dbad2017-07-14 16:12:09 -07001472 dma_addr_t mapping;
1473 unsigned int size;
1474 skb_frag_t *frag;
1475 u32 len_stat;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001476 int ret;
1477 int i;
1478
1479 index = skb_get_queue_mapping(skb);
1480 /* Mapping strategy:
1481 * queue_mapping = 0, unclassified, packet xmited through ring16
1482 * queue_mapping = 1, goes to ring 0. (highest priority queue
1483 * queue_mapping = 2, goes to ring 1.
1484 * queue_mapping = 3, goes to ring 2.
1485 * queue_mapping = 4, goes to ring 3.
1486 */
1487 if (index == 0)
1488 index = DESC_INDEX;
1489 else
1490 index -= 1;
1491
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001492 ring = &priv->tx_rings[index];
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001493 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001494
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07001495 nr_frags = skb_shinfo(skb)->nr_frags;
1496
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001497 spin_lock_irqsave(&ring->lock, flags);
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07001498 if (ring->free_bds <= (nr_frags + 1)) {
1499 if (!netif_tx_queue_stopped(txq)) {
1500 netif_tx_stop_queue(txq);
1501 netdev_err(dev,
1502 "%s: tx ring %d full when queue %d awake\n",
1503 __func__, index, ring->queue);
1504 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001505 ret = NETDEV_TX_BUSY;
1506 goto out;
1507 }
1508
Florian Fainelli474ea9c2014-07-22 11:01:52 -07001509 if (skb_padto(skb, ETH_ZLEN)) {
1510 ret = NETDEV_TX_OK;
1511 goto out;
1512 }
1513
Petri Gynther55868122016-03-24 11:27:20 -07001514 /* Retain how many bytes will be sent on the wire, without TSB inserted
1515 * by transmit checksum offload
1516 */
1517 GENET_CB(skb)->bytes_sent = skb->len;
1518
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001519 /* set the SKB transmit checksum */
1520 if (priv->desc_64b_en) {
Petri Gyntherbc233332014-10-01 11:30:01 -07001521 skb = bcmgenet_put_tx_csum(dev, skb);
1522 if (!skb) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001523 ret = NETDEV_TX_OK;
1524 goto out;
1525 }
1526 }
1527
Doug Berger876dbad2017-07-14 16:12:09 -07001528 for (i = 0; i <= nr_frags; i++) {
1529 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001530
Doug Berger876dbad2017-07-14 16:12:09 -07001531 if (unlikely(!tx_cb_ptr))
1532 BUG();
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001533
Doug Berger876dbad2017-07-14 16:12:09 -07001534 if (!i) {
1535 /* Transmit single SKB or head of fragment list */
1536 tx_cb_ptr->skb = skb;
1537 size = skb_headlen(skb);
1538 mapping = dma_map_single(kdev, skb->data, size,
1539 DMA_TO_DEVICE);
1540 } else {
1541 /* xmit fragment */
1542 tx_cb_ptr->skb = NULL;
1543 frag = &skb_shinfo(skb)->frags[i - 1];
1544 size = skb_frag_size(frag);
1545 mapping = skb_frag_dma_map(kdev, frag, 0, size,
1546 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001547 }
Doug Berger876dbad2017-07-14 16:12:09 -07001548
1549 ret = dma_mapping_error(kdev, mapping);
1550 if (ret) {
1551 priv->mib.tx_dma_failed++;
1552 netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
1553 ret = NETDEV_TX_OK;
1554 goto out_unmap_frags;
1555 }
1556 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
1557 dma_unmap_len_set(tx_cb_ptr, dma_len, size);
1558
1559 len_stat = (size << DMA_BUFLENGTH_SHIFT) |
1560 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT);
1561
1562 if (!i) {
1563 len_stat |= DMA_TX_APPEND_CRC | DMA_SOP;
1564 if (skb->ip_summed == CHECKSUM_PARTIAL)
1565 len_stat |= DMA_TX_DO_CSUM;
1566 }
1567 if (i == nr_frags)
1568 len_stat |= DMA_EOP;
1569
1570 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, len_stat);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001571 }
1572
Florian Fainellid03825f2014-03-20 10:53:21 -07001573 skb_tx_timestamp(skb);
1574
Florian Fainelliae67bf02015-03-13 12:11:06 -07001575 /* Decrement total BD count and advance our write pointer */
1576 ring->free_bds -= nr_frags + 1;
1577 ring->prod_index += nr_frags + 1;
1578 ring->prod_index &= DMA_P_INDEX_MASK;
1579
Petri Gynthere178c8c2016-04-09 00:20:36 -07001580 netdev_tx_sent_queue(txq, GENET_CB(skb)->bytes_sent);
1581
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001582 if (ring->free_bds <= (MAX_SKB_FRAGS + 1))
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001583 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001584
Florian Fainelliddd0ca52015-03-13 12:11:07 -07001585 if (!skb->xmit_more || netif_xmit_stopped(txq))
1586 /* Packets are ready, update producer index */
1587 bcmgenet_tdma_ring_writel(priv, ring->index,
1588 ring->prod_index, TDMA_PROD_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001589out:
1590 spin_unlock_irqrestore(&ring->lock, flags);
1591
1592 return ret;
Doug Berger876dbad2017-07-14 16:12:09 -07001593
1594out_unmap_frags:
1595 /* Back up for failed control block mapping */
1596 bcmgenet_put_txcb(priv, ring);
1597
1598 /* Unmap successfully mapped control blocks */
1599 while (i-- > 0) {
1600 tx_cb_ptr = bcmgenet_put_txcb(priv, ring);
1601 if (tx_cb_ptr->skb)
1602 dma_unmap_single(kdev,
1603 dma_unmap_addr(tx_cb_ptr, dma_addr),
1604 dma_unmap_len(tx_cb_ptr, dma_len),
1605 DMA_TO_DEVICE);
1606 else
1607 dma_unmap_page(kdev,
1608 dma_unmap_addr(tx_cb_ptr, dma_addr),
1609 dma_unmap_len(tx_cb_ptr, dma_len),
1610 DMA_TO_DEVICE);
1611 dma_unmap_addr_set(tx_cb_ptr, dma_addr, 0);
1612 tx_cb_ptr->skb = NULL;
1613 }
1614
1615 dev_kfree_skb(skb);
1616 goto out;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001617}
1618
Petri Gyntherd6707be2015-03-12 15:48:00 -07001619static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
1620 struct enet_cb *cb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001621{
1622 struct device *kdev = &priv->pdev->dev;
1623 struct sk_buff *skb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001624 struct sk_buff *rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001625 dma_addr_t mapping;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001626
Petri Gyntherd6707be2015-03-12 15:48:00 -07001627 /* Allocate a new Rx skb */
Florian Fainellic91b7f62014-07-23 10:42:12 -07001628 skb = netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT);
Petri Gyntherd6707be2015-03-12 15:48:00 -07001629 if (!skb) {
1630 priv->mib.alloc_rx_buff_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001631 netif_err(priv, rx_err, priv->dev,
Petri Gyntherd6707be2015-03-12 15:48:00 -07001632 "%s: Rx skb allocation failed\n", __func__);
1633 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001634 }
1635
Petri Gyntherd6707be2015-03-12 15:48:00 -07001636 /* DMA-map the new Rx skb */
1637 mapping = dma_map_single(kdev, skb->data, priv->rx_buf_len,
1638 DMA_FROM_DEVICE);
1639 if (dma_mapping_error(kdev, mapping)) {
1640 priv->mib.rx_dma_failed++;
1641 dev_kfree_skb_any(skb);
1642 netif_err(priv, rx_err, priv->dev,
1643 "%s: Rx skb DMA mapping failed\n", __func__);
1644 return NULL;
1645 }
1646
1647 /* Grab the current Rx skb from the ring and DMA-unmap it */
1648 rx_skb = cb->skb;
1649 if (likely(rx_skb))
1650 dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
1651 priv->rx_buf_len, DMA_FROM_DEVICE);
1652
1653 /* Put the new Rx skb on the ring */
1654 cb->skb = skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001655 dma_unmap_addr_set(cb, dma_addr, mapping);
Petri Gynther8ac467e2015-03-09 13:40:00 -07001656 dmadesc_set_addr(priv, cb->bd_addr, mapping);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001657
Petri Gyntherd6707be2015-03-12 15:48:00 -07001658 /* Return the current Rx skb to caller */
1659 return rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001660}
1661
1662/* bcmgenet_desc_rx - descriptor based rx process.
1663 * this could be called from bottom half, or from NAPI polling method.
1664 */
Petri Gynther4055eae2015-03-25 12:35:16 -07001665static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001666 unsigned int budget)
1667{
Petri Gynther4055eae2015-03-25 12:35:16 -07001668 struct bcmgenet_priv *priv = ring->priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001669 struct net_device *dev = priv->dev;
1670 struct enet_cb *cb;
1671 struct sk_buff *skb;
1672 u32 dma_length_status;
1673 unsigned long dma_flag;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001674 int len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001675 unsigned int rxpktprocessed = 0, rxpkttoprocess;
Doug Bergerd5810ca2017-03-13 17:41:37 -07001676 unsigned int p_index, mask;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001677 unsigned int discards;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001678 unsigned int chksum_ok = 0;
1679
Doug Bergerd5810ca2017-03-13 17:41:37 -07001680 /* Clear status before servicing to reduce spurious interrupts */
1681 if (ring->index == DESC_INDEX) {
1682 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_RXDMA_DONE,
1683 INTRL2_CPU_CLEAR);
1684 } else {
1685 mask = 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index);
1686 bcmgenet_intrl2_1_writel(priv,
1687 mask,
1688 INTRL2_CPU_CLEAR);
1689 }
1690
Petri Gynther4055eae2015-03-25 12:35:16 -07001691 p_index = bcmgenet_rdma_ring_readl(priv, ring->index, RDMA_PROD_INDEX);
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001692
1693 discards = (p_index >> DMA_P_INDEX_DISCARD_CNT_SHIFT) &
1694 DMA_P_INDEX_DISCARD_CNT_MASK;
1695 if (discards > ring->old_discards) {
1696 discards = discards - ring->old_discards;
Florian Fainelli37a30b42017-03-16 10:27:08 -07001697 ring->errors += discards;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001698 ring->old_discards += discards;
1699
1700 /* Clear HW register when we reach 75% of maximum 0xFFFF */
1701 if (ring->old_discards >= 0xC000) {
1702 ring->old_discards = 0;
Petri Gynther4055eae2015-03-25 12:35:16 -07001703 bcmgenet_rdma_ring_writel(priv, ring->index, 0,
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001704 RDMA_PROD_INDEX);
1705 }
1706 }
1707
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001708 p_index &= DMA_P_INDEX_MASK;
Doug Bergerc298ede2017-03-13 17:41:33 -07001709 rxpkttoprocess = (p_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001710
1711 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001712 "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001713
1714 while ((rxpktprocessed < rxpkttoprocess) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001715 (rxpktprocessed < budget)) {
Petri Gynther8ac467e2015-03-09 13:40:00 -07001716 cb = &priv->rx_cbs[ring->read_ptr];
Petri Gyntherd6707be2015-03-12 15:48:00 -07001717 skb = bcmgenet_rx_refill(priv, cb);
Florian Fainellib629be52014-09-08 11:37:52 -07001718
Florian Fainellib629be52014-09-08 11:37:52 -07001719 if (unlikely(!skb)) {
Florian Fainelli37a30b42017-03-16 10:27:08 -07001720 ring->dropped++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001721 goto next;
Florian Fainellib629be52014-09-08 11:37:52 -07001722 }
1723
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001724 if (!priv->desc_64b_en) {
Florian Fainellic91b7f62014-07-23 10:42:12 -07001725 dma_length_status =
Petri Gynther8ac467e2015-03-09 13:40:00 -07001726 dmadesc_get_length_status(priv, cb->bd_addr);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001727 } else {
1728 struct status_64 *status;
Florian Fainelli164d4f22014-07-23 10:42:13 -07001729
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001730 status = (struct status_64 *)skb->data;
1731 dma_length_status = status->length_status;
1732 }
1733
1734 /* DMA flags and length are still valid no matter how
1735 * we got the Receive Status Vector (64B RSB or register)
1736 */
1737 dma_flag = dma_length_status & 0xffff;
1738 len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
1739
1740 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001741 "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
Petri Gynther8ac467e2015-03-09 13:40:00 -07001742 __func__, p_index, ring->c_index,
1743 ring->read_ptr, dma_length_status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001744
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001745 if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
1746 netif_err(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001747 "dropping fragmented packet!\n");
Florian Fainelli37a30b42017-03-16 10:27:08 -07001748 ring->errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001749 dev_kfree_skb_any(skb);
1750 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001751 }
Petri Gyntherd6707be2015-03-12 15:48:00 -07001752
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001753 /* report errors */
1754 if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
1755 DMA_RX_OV |
1756 DMA_RX_NO |
1757 DMA_RX_LG |
1758 DMA_RX_RXER))) {
1759 netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001760 (unsigned int)dma_flag);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001761 if (dma_flag & DMA_RX_CRC_ERROR)
1762 dev->stats.rx_crc_errors++;
1763 if (dma_flag & DMA_RX_OV)
1764 dev->stats.rx_over_errors++;
1765 if (dma_flag & DMA_RX_NO)
1766 dev->stats.rx_frame_errors++;
1767 if (dma_flag & DMA_RX_LG)
1768 dev->stats.rx_length_errors++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001769 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001770 dev_kfree_skb_any(skb);
1771 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001772 } /* error packet */
1773
1774 chksum_ok = (dma_flag & priv->dma_rx_chk_bit) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001775 priv->desc_rxchk_en;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001776
1777 skb_put(skb, len);
1778 if (priv->desc_64b_en) {
1779 skb_pull(skb, 64);
1780 len -= 64;
1781 }
1782
1783 if (likely(chksum_ok))
1784 skb->ip_summed = CHECKSUM_UNNECESSARY;
1785
1786 /* remove hardware 2bytes added for IP alignment */
1787 skb_pull(skb, 2);
1788 len -= 2;
1789
1790 if (priv->crc_fwd_en) {
1791 skb_trim(skb, len - ETH_FCS_LEN);
1792 len -= ETH_FCS_LEN;
1793 }
1794
1795 /*Finish setting up the received SKB and send it to the kernel*/
1796 skb->protocol = eth_type_trans(skb, priv->dev);
Florian Fainelli37a30b42017-03-16 10:27:08 -07001797 ring->packets++;
1798 ring->bytes += len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001799 if (dma_flag & DMA_RX_MULT)
1800 dev->stats.multicast++;
1801
1802 /* Notify kernel */
Petri Gynther4055eae2015-03-25 12:35:16 -07001803 napi_gro_receive(&ring->napi, skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001804 netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
1805
Petri Gyntherd6707be2015-03-12 15:48:00 -07001806next:
Florian Fainellicf377d82014-10-10 10:51:52 -07001807 rxpktprocessed++;
Petri Gynther8ac467e2015-03-09 13:40:00 -07001808 if (likely(ring->read_ptr < ring->end_ptr))
1809 ring->read_ptr++;
1810 else
1811 ring->read_ptr = ring->cb_ptr;
1812
1813 ring->c_index = (ring->c_index + 1) & DMA_C_INDEX_MASK;
Petri Gynther4055eae2015-03-25 12:35:16 -07001814 bcmgenet_rdma_ring_writel(priv, ring->index, ring->c_index, RDMA_CONS_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001815 }
1816
1817 return rxpktprocessed;
1818}
1819
Petri Gynther3ab11332015-03-25 12:35:15 -07001820/* Rx NAPI polling method */
1821static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
1822{
Petri Gynther4055eae2015-03-25 12:35:16 -07001823 struct bcmgenet_rx_ring *ring = container_of(napi,
1824 struct bcmgenet_rx_ring, napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07001825 unsigned int work_done;
1826
Petri Gynther4055eae2015-03-25 12:35:16 -07001827 work_done = bcmgenet_desc_rx(ring, budget);
Petri Gynther3ab11332015-03-25 12:35:15 -07001828
1829 if (work_done < budget) {
Eric Dumazeteb96ce02016-04-08 22:06:40 -07001830 napi_complete_done(napi, work_done);
Petri Gynther4055eae2015-03-25 12:35:16 -07001831 ring->int_enable(ring);
Petri Gynther3ab11332015-03-25 12:35:15 -07001832 }
1833
1834 return work_done;
1835}
1836
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001837/* Assign skb to RX DMA descriptor. */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001838static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv,
1839 struct bcmgenet_rx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001840{
1841 struct enet_cb *cb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001842 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001843 int i;
1844
Petri Gynther8ac467e2015-03-09 13:40:00 -07001845 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001846
1847 /* loop here for each buffer needing assign */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001848 for (i = 0; i < ring->size; i++) {
1849 cb = ring->cbs + i;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001850 skb = bcmgenet_rx_refill(priv, cb);
1851 if (skb)
1852 dev_kfree_skb_any(skb);
1853 if (!cb->skb)
1854 return -ENOMEM;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001855 }
1856
Petri Gyntherd6707be2015-03-12 15:48:00 -07001857 return 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001858}
1859
1860static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
1861{
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001862 struct device *kdev = &priv->pdev->dev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001863 struct enet_cb *cb;
1864 int i;
1865
1866 for (i = 0; i < priv->num_rx_bds; i++) {
1867 cb = &priv->rx_cbs[i];
1868
1869 if (dma_unmap_addr(cb, dma_addr)) {
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001870 dma_unmap_single(kdev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001871 dma_unmap_addr(cb, dma_addr),
1872 priv->rx_buf_len, DMA_FROM_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001873 dma_unmap_addr_set(cb, dma_addr, 0);
1874 }
1875
1876 if (cb->skb)
1877 bcmgenet_free_cb(cb);
1878 }
1879}
1880
Florian Fainellic91b7f62014-07-23 10:42:12 -07001881static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable)
Florian Fainellie29585b2014-07-21 15:29:20 -07001882{
1883 u32 reg;
1884
1885 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1886 if (enable)
1887 reg |= mask;
1888 else
1889 reg &= ~mask;
1890 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
1891
1892 /* UniMAC stops on a packet boundary, wait for a full-size packet
1893 * to be processed
1894 */
1895 if (enable == 0)
1896 usleep_range(1000, 2000);
1897}
1898
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001899static int reset_umac(struct bcmgenet_priv *priv)
1900{
1901 struct device *kdev = &priv->pdev->dev;
1902 unsigned int timeout = 0;
1903 u32 reg;
1904
1905 /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
1906 bcmgenet_rbuf_ctrl_set(priv, 0);
1907 udelay(10);
1908
1909 /* disable MAC while updating its registers */
1910 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1911
1912 /* issue soft reset, wait for it to complete */
1913 bcmgenet_umac_writel(priv, CMD_SW_RESET, UMAC_CMD);
1914 while (timeout++ < 1000) {
1915 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1916 if (!(reg & CMD_SW_RESET))
1917 return 0;
1918
1919 udelay(1);
1920 }
1921
1922 if (timeout == 1000) {
1923 dev_err(kdev,
Brian Norris7fc527f2014-07-29 14:34:14 -07001924 "timeout waiting for MAC to come out of reset\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001925 return -ETIMEDOUT;
1926 }
1927
1928 return 0;
1929}
1930
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001931static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
1932{
1933 /* Mask all interrupts.*/
1934 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1935 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001936 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1937 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001938}
1939
Florian Fainelli37850e32015-10-17 14:22:46 -07001940static void bcmgenet_link_intr_enable(struct bcmgenet_priv *priv)
1941{
1942 u32 int0_enable = 0;
1943
1944 /* Monitor cable plug/unplugged event for internal PHY, external PHY
1945 * and MoCA PHY
1946 */
1947 if (priv->internal_phy) {
1948 int0_enable |= UMAC_IRQ_LINK_EVENT;
1949 } else if (priv->ext_phy) {
1950 int0_enable |= UMAC_IRQ_LINK_EVENT;
1951 } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
1952 if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
1953 int0_enable |= UMAC_IRQ_LINK_EVENT;
1954 }
1955 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
1956}
1957
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001958static int init_umac(struct bcmgenet_priv *priv)
1959{
1960 struct device *kdev = &priv->pdev->dev;
1961 int ret;
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001962 u32 reg;
1963 u32 int0_enable = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001964
1965 dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
1966
1967 ret = reset_umac(priv);
1968 if (ret)
1969 return ret;
1970
1971 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1972 /* clear tx/rx counter */
1973 bcmgenet_umac_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001974 MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
1975 UMAC_MIB_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001976 bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
1977
1978 bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
1979
1980 /* init rx registers, enable ip header optimization */
1981 reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
1982 reg |= RBUF_ALIGN_2B;
1983 bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
1984
1985 if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
1986 bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
1987
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001988 bcmgenet_intr_disable(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001989
Florian Fainelli37850e32015-10-17 14:22:46 -07001990 /* Configure backpressure vectors for MoCA */
1991 if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001992 reg = bcmgenet_bp_mc_get(priv);
1993 reg |= BIT(priv->hw_params->bp_in_en_shift);
1994
1995 /* bp_mask: back pressure mask */
1996 if (netif_is_multiqueue(priv->dev))
1997 reg |= priv->hw_params->bp_in_mask;
1998 else
1999 reg &= ~priv->hw_params->bp_in_mask;
2000 bcmgenet_bp_mc_set(priv, reg);
2001 }
2002
2003 /* Enable MDIO interrupts on GENET v3+ */
2004 if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002005 int0_enable |= (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002006
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002007 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002008
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002009 dev_dbg(kdev, "done init umac\n");
2010
2011 return 0;
2012}
2013
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002014/* Initialize a Tx ring along with corresponding hardware registers */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002015static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
2016 unsigned int index, unsigned int size,
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002017 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002018{
2019 struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
2020 u32 words_per_bd = WORDS_PER_BD(priv);
2021 u32 flow_period_val = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002022
2023 spin_lock_init(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002024 ring->priv = priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002025 ring->index = index;
2026 if (index == DESC_INDEX) {
2027 ring->queue = 0;
2028 ring->int_enable = bcmgenet_tx_ring16_int_enable;
2029 ring->int_disable = bcmgenet_tx_ring16_int_disable;
2030 } else {
2031 ring->queue = index + 1;
2032 ring->int_enable = bcmgenet_tx_ring_int_enable;
2033 ring->int_disable = bcmgenet_tx_ring_int_disable;
2034 }
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002035 ring->cbs = priv->tx_cbs + start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002036 ring->size = size;
Petri Gynther66d06752015-03-04 14:30:01 -08002037 ring->clean_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002038 ring->c_index = 0;
2039 ring->free_bds = size;
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002040 ring->write_ptr = start_ptr;
2041 ring->cb_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002042 ring->end_ptr = end_ptr - 1;
2043 ring->prod_index = 0;
2044
2045 /* Set flow period for ring != 16 */
2046 if (index != DESC_INDEX)
2047 flow_period_val = ENET_MAX_MTU_SIZE << 16;
2048
2049 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
2050 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
2051 bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
2052 /* Disable rate control for now */
2053 bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002054 TDMA_FLOW_PERIOD);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002055 bcmgenet_tdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002056 ((size << DMA_RING_SIZE_SHIFT) |
2057 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002058
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002059 /* Set start and end address, read and write pointers */
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002060 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002061 DMA_START_ADDR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002062 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002063 TDMA_READ_PTR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002064 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002065 TDMA_WRITE_PTR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002066 bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002067 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002068}
2069
2070/* Initialize a RDMA ring */
2071static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
Petri Gynther8ac467e2015-03-09 13:40:00 -07002072 unsigned int index, unsigned int size,
2073 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002074{
Petri Gynther8ac467e2015-03-09 13:40:00 -07002075 struct bcmgenet_rx_ring *ring = &priv->rx_rings[index];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002076 u32 words_per_bd = WORDS_PER_BD(priv);
2077 int ret;
2078
Petri Gynther4055eae2015-03-25 12:35:16 -07002079 ring->priv = priv;
Petri Gynther8ac467e2015-03-09 13:40:00 -07002080 ring->index = index;
Petri Gynther4055eae2015-03-25 12:35:16 -07002081 if (index == DESC_INDEX) {
2082 ring->int_enable = bcmgenet_rx_ring16_int_enable;
2083 ring->int_disable = bcmgenet_rx_ring16_int_disable;
2084 } else {
2085 ring->int_enable = bcmgenet_rx_ring_int_enable;
2086 ring->int_disable = bcmgenet_rx_ring_int_disable;
2087 }
Petri Gynther8ac467e2015-03-09 13:40:00 -07002088 ring->cbs = priv->rx_cbs + start_ptr;
2089 ring->size = size;
2090 ring->c_index = 0;
2091 ring->read_ptr = start_ptr;
2092 ring->cb_ptr = start_ptr;
2093 ring->end_ptr = end_ptr - 1;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002094
Petri Gynther8ac467e2015-03-09 13:40:00 -07002095 ret = bcmgenet_alloc_rx_buffers(priv, ring);
2096 if (ret)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002097 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002098
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002099 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
2100 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
Petri Gynther6f5a2722015-03-06 13:45:00 -08002101 bcmgenet_rdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002102 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002103 ((size << DMA_RING_SIZE_SHIFT) |
2104 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002105 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002106 (DMA_FC_THRESH_LO <<
2107 DMA_XOFF_THRESHOLD_SHIFT) |
2108 DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
Petri Gynther6f5a2722015-03-06 13:45:00 -08002109
2110 /* Set start and end address, read and write pointers */
Petri Gynther8ac467e2015-03-09 13:40:00 -07002111 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2112 DMA_START_ADDR);
2113 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2114 RDMA_READ_PTR);
2115 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2116 RDMA_WRITE_PTR);
2117 bcmgenet_rdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Petri Gynther6f5a2722015-03-06 13:45:00 -08002118 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002119
2120 return ret;
2121}
2122
Petri Gynthere2aadb42015-03-25 12:35:14 -07002123static void bcmgenet_init_tx_napi(struct bcmgenet_priv *priv)
2124{
2125 unsigned int i;
2126 struct bcmgenet_tx_ring *ring;
2127
2128 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2129 ring = &priv->tx_rings[i];
Eric Dumazetd64b5e82015-11-18 06:31:00 -08002130 netif_tx_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll, 64);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002131 }
2132
2133 ring = &priv->tx_rings[DESC_INDEX];
Eric Dumazetd64b5e82015-11-18 06:31:00 -08002134 netif_tx_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll, 64);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002135}
2136
2137static void bcmgenet_enable_tx_napi(struct bcmgenet_priv *priv)
2138{
2139 unsigned int i;
Doug Berger6689da12017-03-13 17:41:35 -07002140 u32 int0_enable = UMAC_IRQ_TXDMA_DONE;
2141 u32 int1_enable = 0;
Petri Gynthere2aadb42015-03-25 12:35:14 -07002142 struct bcmgenet_tx_ring *ring;
2143
2144 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2145 ring = &priv->tx_rings[i];
2146 napi_enable(&ring->napi);
Doug Berger6689da12017-03-13 17:41:35 -07002147 int1_enable |= (1 << i);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002148 }
2149
2150 ring = &priv->tx_rings[DESC_INDEX];
2151 napi_enable(&ring->napi);
Doug Berger6689da12017-03-13 17:41:35 -07002152
2153 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
2154 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002155}
2156
2157static void bcmgenet_disable_tx_napi(struct bcmgenet_priv *priv)
2158{
2159 unsigned int i;
Doug Berger6689da12017-03-13 17:41:35 -07002160 u32 int0_disable = UMAC_IRQ_TXDMA_DONE;
2161 u32 int1_disable = 0xffff;
Petri Gynthere2aadb42015-03-25 12:35:14 -07002162 struct bcmgenet_tx_ring *ring;
2163
Doug Berger6689da12017-03-13 17:41:35 -07002164 bcmgenet_intrl2_0_writel(priv, int0_disable, INTRL2_CPU_MASK_SET);
2165 bcmgenet_intrl2_1_writel(priv, int1_disable, INTRL2_CPU_MASK_SET);
2166
Petri Gynthere2aadb42015-03-25 12:35:14 -07002167 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2168 ring = &priv->tx_rings[i];
2169 napi_disable(&ring->napi);
2170 }
2171
2172 ring = &priv->tx_rings[DESC_INDEX];
2173 napi_disable(&ring->napi);
2174}
2175
2176static void bcmgenet_fini_tx_napi(struct bcmgenet_priv *priv)
2177{
2178 unsigned int i;
2179 struct bcmgenet_tx_ring *ring;
2180
2181 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2182 ring = &priv->tx_rings[i];
2183 netif_napi_del(&ring->napi);
2184 }
2185
2186 ring = &priv->tx_rings[DESC_INDEX];
2187 netif_napi_del(&ring->napi);
2188}
2189
Petri Gynther16c6d662015-02-23 11:00:45 -08002190/* Initialize Tx queues
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002191 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002192 * Queues 0-3 are priority-based, each one has 32 descriptors,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002193 * with queue 0 being the highest priority queue.
2194 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002195 * Queue 16 is the default Tx queue with
Petri Gynther51a966a2015-02-23 11:00:46 -08002196 * GENET_Q16_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors.
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002197 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002198 * The transmit control block pool is then partitioned as follows:
2199 * - Tx queue 0 uses tx_cbs[0..31]
2200 * - Tx queue 1 uses tx_cbs[32..63]
2201 * - Tx queue 2 uses tx_cbs[64..95]
2202 * - Tx queue 3 uses tx_cbs[96..127]
2203 * - Tx queue 16 uses tx_cbs[128..255]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002204 */
Petri Gynther16c6d662015-02-23 11:00:45 -08002205static void bcmgenet_init_tx_queues(struct net_device *dev)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002206{
2207 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther16c6d662015-02-23 11:00:45 -08002208 u32 i, dma_enable;
2209 u32 dma_ctrl, ring_cfg;
Petri Gynther37742162014-10-07 09:30:01 -07002210 u32 dma_priority[3] = {0, 0, 0};
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002211
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002212 dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
2213 dma_enable = dma_ctrl & DMA_EN;
2214 dma_ctrl &= ~DMA_EN;
2215 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
2216
Petri Gynther16c6d662015-02-23 11:00:45 -08002217 dma_ctrl = 0;
2218 ring_cfg = 0;
2219
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002220 /* Enable strict priority arbiter mode */
2221 bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
2222
Petri Gynther16c6d662015-02-23 11:00:45 -08002223 /* Initialize Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002224 for (i = 0; i < priv->hw_params->tx_queues; i++) {
Petri Gynther51a966a2015-02-23 11:00:46 -08002225 bcmgenet_init_tx_ring(priv, i, priv->hw_params->tx_bds_per_q,
2226 i * priv->hw_params->tx_bds_per_q,
2227 (i + 1) * priv->hw_params->tx_bds_per_q);
Petri Gynther16c6d662015-02-23 11:00:45 -08002228 ring_cfg |= (1 << i);
2229 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002230 dma_priority[DMA_PRIO_REG_INDEX(i)] |=
2231 ((GENET_Q0_PRIORITY + i) << DMA_PRIO_REG_SHIFT(i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002232 }
2233
Petri Gynther16c6d662015-02-23 11:00:45 -08002234 /* Initialize Tx default queue 16 */
Petri Gynther51a966a2015-02-23 11:00:46 -08002235 bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_Q16_TX_BD_CNT,
Petri Gynther16c6d662015-02-23 11:00:45 -08002236 priv->hw_params->tx_queues *
Petri Gynther51a966a2015-02-23 11:00:46 -08002237 priv->hw_params->tx_bds_per_q,
Petri Gynther16c6d662015-02-23 11:00:45 -08002238 TOTAL_DESC);
2239 ring_cfg |= (1 << DESC_INDEX);
2240 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002241 dma_priority[DMA_PRIO_REG_INDEX(DESC_INDEX)] |=
2242 ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) <<
2243 DMA_PRIO_REG_SHIFT(DESC_INDEX));
Petri Gynther16c6d662015-02-23 11:00:45 -08002244
2245 /* Set Tx queue priorities */
Petri Gynther37742162014-10-07 09:30:01 -07002246 bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
2247 bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
2248 bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);
2249
Petri Gynthere2aadb42015-03-25 12:35:14 -07002250 /* Initialize Tx NAPI */
2251 bcmgenet_init_tx_napi(priv);
2252
Petri Gynther16c6d662015-02-23 11:00:45 -08002253 /* Enable Tx queues */
2254 bcmgenet_tdma_writel(priv, ring_cfg, DMA_RING_CFG);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002255
Petri Gynther16c6d662015-02-23 11:00:45 -08002256 /* Enable Tx DMA */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002257 if (dma_enable)
Petri Gynther16c6d662015-02-23 11:00:45 -08002258 dma_ctrl |= DMA_EN;
2259 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002260}
2261
Petri Gynther3ab11332015-03-25 12:35:15 -07002262static void bcmgenet_init_rx_napi(struct bcmgenet_priv *priv)
2263{
Petri Gynther4055eae2015-03-25 12:35:16 -07002264 unsigned int i;
2265 struct bcmgenet_rx_ring *ring;
2266
2267 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2268 ring = &priv->rx_rings[i];
2269 netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll, 64);
2270 }
2271
2272 ring = &priv->rx_rings[DESC_INDEX];
2273 netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll, 64);
Petri Gynther3ab11332015-03-25 12:35:15 -07002274}
2275
2276static void bcmgenet_enable_rx_napi(struct bcmgenet_priv *priv)
2277{
Petri Gynther4055eae2015-03-25 12:35:16 -07002278 unsigned int i;
Doug Berger6689da12017-03-13 17:41:35 -07002279 u32 int0_enable = UMAC_IRQ_RXDMA_DONE;
2280 u32 int1_enable = 0;
Petri Gynther4055eae2015-03-25 12:35:16 -07002281 struct bcmgenet_rx_ring *ring;
2282
2283 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2284 ring = &priv->rx_rings[i];
2285 napi_enable(&ring->napi);
Doug Berger6689da12017-03-13 17:41:35 -07002286 int1_enable |= (1 << (UMAC_IRQ1_RX_INTR_SHIFT + i));
Petri Gynther4055eae2015-03-25 12:35:16 -07002287 }
2288
2289 ring = &priv->rx_rings[DESC_INDEX];
2290 napi_enable(&ring->napi);
Doug Berger6689da12017-03-13 17:41:35 -07002291
2292 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
2293 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
Petri Gynther3ab11332015-03-25 12:35:15 -07002294}
2295
2296static void bcmgenet_disable_rx_napi(struct bcmgenet_priv *priv)
2297{
Petri Gynther4055eae2015-03-25 12:35:16 -07002298 unsigned int i;
Doug Berger6689da12017-03-13 17:41:35 -07002299 u32 int0_disable = UMAC_IRQ_RXDMA_DONE;
2300 u32 int1_disable = 0xffff << UMAC_IRQ1_RX_INTR_SHIFT;
Petri Gynther4055eae2015-03-25 12:35:16 -07002301 struct bcmgenet_rx_ring *ring;
2302
Doug Berger6689da12017-03-13 17:41:35 -07002303 bcmgenet_intrl2_0_writel(priv, int0_disable, INTRL2_CPU_MASK_SET);
2304 bcmgenet_intrl2_1_writel(priv, int1_disable, INTRL2_CPU_MASK_SET);
2305
Petri Gynther4055eae2015-03-25 12:35:16 -07002306 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2307 ring = &priv->rx_rings[i];
2308 napi_disable(&ring->napi);
2309 }
2310
2311 ring = &priv->rx_rings[DESC_INDEX];
2312 napi_disable(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002313}
2314
2315static void bcmgenet_fini_rx_napi(struct bcmgenet_priv *priv)
2316{
Petri Gynther4055eae2015-03-25 12:35:16 -07002317 unsigned int i;
2318 struct bcmgenet_rx_ring *ring;
2319
2320 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2321 ring = &priv->rx_rings[i];
2322 netif_napi_del(&ring->napi);
2323 }
2324
2325 ring = &priv->rx_rings[DESC_INDEX];
2326 netif_napi_del(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002327}
2328
Petri Gynther8ac467e2015-03-09 13:40:00 -07002329/* Initialize Rx queues
2330 *
2331 * Queues 0-15 are priority queues. Hardware Filtering Block (HFB) can be
2332 * used to direct traffic to these queues.
2333 *
2334 * Queue 16 is the default Rx queue with GENET_Q16_RX_BD_CNT descriptors.
2335 */
2336static int bcmgenet_init_rx_queues(struct net_device *dev)
2337{
2338 struct bcmgenet_priv *priv = netdev_priv(dev);
2339 u32 i;
2340 u32 dma_enable;
2341 u32 dma_ctrl;
2342 u32 ring_cfg;
2343 int ret;
2344
2345 dma_ctrl = bcmgenet_rdma_readl(priv, DMA_CTRL);
2346 dma_enable = dma_ctrl & DMA_EN;
2347 dma_ctrl &= ~DMA_EN;
2348 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2349
2350 dma_ctrl = 0;
2351 ring_cfg = 0;
2352
2353 /* Initialize Rx priority queues */
2354 for (i = 0; i < priv->hw_params->rx_queues; i++) {
2355 ret = bcmgenet_init_rx_ring(priv, i,
2356 priv->hw_params->rx_bds_per_q,
2357 i * priv->hw_params->rx_bds_per_q,
2358 (i + 1) *
2359 priv->hw_params->rx_bds_per_q);
2360 if (ret)
2361 return ret;
2362
2363 ring_cfg |= (1 << i);
2364 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2365 }
2366
2367 /* Initialize Rx default queue 16 */
2368 ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, GENET_Q16_RX_BD_CNT,
2369 priv->hw_params->rx_queues *
2370 priv->hw_params->rx_bds_per_q,
2371 TOTAL_DESC);
2372 if (ret)
2373 return ret;
2374
2375 ring_cfg |= (1 << DESC_INDEX);
2376 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
2377
Petri Gynther3ab11332015-03-25 12:35:15 -07002378 /* Initialize Rx NAPI */
2379 bcmgenet_init_rx_napi(priv);
2380
Petri Gynther8ac467e2015-03-09 13:40:00 -07002381 /* Enable rings */
2382 bcmgenet_rdma_writel(priv, ring_cfg, DMA_RING_CFG);
2383
2384 /* Configure ring as descriptor ring and re-enable DMA if enabled */
2385 if (dma_enable)
2386 dma_ctrl |= DMA_EN;
2387 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2388
2389 return 0;
2390}
2391
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002392static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
2393{
2394 int ret = 0;
2395 int timeout = 0;
2396 u32 reg;
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002397 u32 dma_ctrl;
2398 int i;
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002399
2400 /* Disable TDMA to stop add more frames in TX DMA */
2401 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2402 reg &= ~DMA_EN;
2403 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2404
2405 /* Check TDMA status register to confirm TDMA is disabled */
2406 while (timeout++ < DMA_TIMEOUT_VAL) {
2407 reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
2408 if (reg & DMA_DISABLED)
2409 break;
2410
2411 udelay(1);
2412 }
2413
2414 if (timeout == DMA_TIMEOUT_VAL) {
2415 netdev_warn(priv->dev, "Timed out while disabling TX DMA\n");
2416 ret = -ETIMEDOUT;
2417 }
2418
2419 /* Wait 10ms for packet drain in both tx and rx dma */
2420 usleep_range(10000, 20000);
2421
2422 /* Disable RDMA */
2423 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2424 reg &= ~DMA_EN;
2425 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2426
2427 timeout = 0;
2428 /* Check RDMA status register to confirm RDMA is disabled */
2429 while (timeout++ < DMA_TIMEOUT_VAL) {
2430 reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
2431 if (reg & DMA_DISABLED)
2432 break;
2433
2434 udelay(1);
2435 }
2436
2437 if (timeout == DMA_TIMEOUT_VAL) {
2438 netdev_warn(priv->dev, "Timed out while disabling RX DMA\n");
2439 ret = -ETIMEDOUT;
2440 }
2441
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002442 dma_ctrl = 0;
2443 for (i = 0; i < priv->hw_params->rx_queues; i++)
2444 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2445 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2446 reg &= ~dma_ctrl;
2447 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2448
2449 dma_ctrl = 0;
2450 for (i = 0; i < priv->hw_params->tx_queues; i++)
2451 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2452 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2453 reg &= ~dma_ctrl;
2454 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2455
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002456 return ret;
2457}
2458
Petri Gynther9abab962015-03-30 00:29:01 -07002459static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002460{
2461 int i;
Petri Gynthere178c8c2016-04-09 00:20:36 -07002462 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002463
Petri Gynther9abab962015-03-30 00:29:01 -07002464 bcmgenet_fini_rx_napi(priv);
2465 bcmgenet_fini_tx_napi(priv);
2466
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002467 /* disable DMA */
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002468 bcmgenet_dma_teardown(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002469
2470 for (i = 0; i < priv->num_tx_bds; i++) {
2471 if (priv->tx_cbs[i].skb != NULL) {
2472 dev_kfree_skb(priv->tx_cbs[i].skb);
2473 priv->tx_cbs[i].skb = NULL;
2474 }
2475 }
2476
Petri Gynthere178c8c2016-04-09 00:20:36 -07002477 for (i = 0; i < priv->hw_params->tx_queues; i++) {
2478 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[i].queue);
2479 netdev_tx_reset_queue(txq);
2480 }
2481
2482 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[DESC_INDEX].queue);
2483 netdev_tx_reset_queue(txq);
2484
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002485 bcmgenet_free_rx_buffers(priv);
2486 kfree(priv->rx_cbs);
2487 kfree(priv->tx_cbs);
2488}
2489
2490/* init_edma: Initialize DMA control register */
2491static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
2492{
2493 int ret;
Petri Gynther014012a2015-02-23 11:00:45 -08002494 unsigned int i;
2495 struct enet_cb *cb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002496
Petri Gynther6f5a2722015-03-06 13:45:00 -08002497 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002498
Petri Gynther6f5a2722015-03-06 13:45:00 -08002499 /* Initialize common Rx ring structures */
2500 priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
2501 priv->num_rx_bds = TOTAL_DESC;
2502 priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct enet_cb),
2503 GFP_KERNEL);
2504 if (!priv->rx_cbs)
2505 return -ENOMEM;
2506
2507 for (i = 0; i < priv->num_rx_bds; i++) {
2508 cb = priv->rx_cbs + i;
2509 cb->bd_addr = priv->rx_bds + i * DMA_DESC_SIZE;
2510 }
2511
Brian Norris7fc527f2014-07-29 14:34:14 -07002512 /* Initialize common TX ring structures */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002513 priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
2514 priv->num_tx_bds = TOTAL_DESC;
Florian Fainellic489be02014-07-23 10:42:15 -07002515 priv->tx_cbs = kcalloc(priv->num_tx_bds, sizeof(struct enet_cb),
Florian Fainellic91b7f62014-07-23 10:42:12 -07002516 GFP_KERNEL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002517 if (!priv->tx_cbs) {
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002518 kfree(priv->rx_cbs);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002519 return -ENOMEM;
2520 }
2521
Petri Gynther014012a2015-02-23 11:00:45 -08002522 for (i = 0; i < priv->num_tx_bds; i++) {
2523 cb = priv->tx_cbs + i;
2524 cb->bd_addr = priv->tx_bds + i * DMA_DESC_SIZE;
2525 }
2526
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002527 /* Init rDma */
2528 bcmgenet_rdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
2529
2530 /* Initialize Rx queues */
2531 ret = bcmgenet_init_rx_queues(priv->dev);
2532 if (ret) {
2533 netdev_err(priv->dev, "failed to initialize Rx queues\n");
2534 bcmgenet_free_rx_buffers(priv);
2535 kfree(priv->rx_cbs);
2536 kfree(priv->tx_cbs);
2537 return ret;
2538 }
2539
2540 /* Init tDma */
2541 bcmgenet_tdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
2542
Petri Gynther16c6d662015-02-23 11:00:45 -08002543 /* Initialize Tx queues */
2544 bcmgenet_init_tx_queues(priv->dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002545
2546 return 0;
2547}
2548
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002549/* Interrupt bottom half */
2550static void bcmgenet_irq_task(struct work_struct *work)
2551{
Doug Berger07c52d62017-03-09 16:58:47 -08002552 unsigned long flags;
2553 unsigned int status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002554 struct bcmgenet_priv *priv = container_of(
2555 work, struct bcmgenet_priv, bcmgenet_irq_work);
2556
2557 netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
2558
Doug Berger07c52d62017-03-09 16:58:47 -08002559 spin_lock_irqsave(&priv->lock, flags);
2560 status = priv->irq0_stat;
2561 priv->irq0_stat = 0;
2562 spin_unlock_irqrestore(&priv->lock, flags);
2563
2564 if (status & UMAC_IRQ_MPD_R) {
Florian Fainelli8fdb0e02014-07-21 15:29:26 -07002565 netif_dbg(priv, wol, priv->dev,
2566 "magic packet detected, waking up\n");
2567 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002568 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002569
2570 /* Link UP/DOWN event */
Doug Berger07c52d62017-03-09 16:58:47 -08002571 if (status & UMAC_IRQ_LINK_EVENT)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002572 phy_mac_interrupt(priv->phydev,
Doug Berger07c52d62017-03-09 16:58:47 -08002573 !!(status & UMAC_IRQ_LINK_UP));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002574}
2575
Petri Gynther4055eae2015-03-25 12:35:16 -07002576/* bcmgenet_isr1: handle Rx and Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002577static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
2578{
2579 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002580 struct bcmgenet_rx_ring *rx_ring;
2581 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08002582 unsigned int index, status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002583
Doug Berger07c52d62017-03-09 16:58:47 -08002584 /* Read irq status */
2585 status = bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002586 ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002587
Brian Norris7fc527f2014-07-29 14:34:14 -07002588 /* clear interrupts */
Doug Berger07c52d62017-03-09 16:58:47 -08002589 bcmgenet_intrl2_1_writel(priv, status, INTRL2_CPU_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002590
2591 netif_dbg(priv, intr, priv->dev,
Doug Berger07c52d62017-03-09 16:58:47 -08002592 "%s: IRQ=0x%x\n", __func__, status);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002593
Petri Gynther4055eae2015-03-25 12:35:16 -07002594 /* Check Rx priority queue interrupts */
2595 for (index = 0; index < priv->hw_params->rx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08002596 if (!(status & BIT(UMAC_IRQ1_RX_INTR_SHIFT + index)))
Petri Gynther4055eae2015-03-25 12:35:16 -07002597 continue;
2598
2599 rx_ring = &priv->rx_rings[index];
2600
2601 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2602 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002603 __napi_schedule_irqoff(&rx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07002604 }
2605 }
2606
2607 /* Check Tx priority queue interrupts */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002608 for (index = 0; index < priv->hw_params->tx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08002609 if (!(status & BIT(index)))
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002610 continue;
2611
Petri Gynther4055eae2015-03-25 12:35:16 -07002612 tx_ring = &priv->tx_rings[index];
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002613
Petri Gynther4055eae2015-03-25 12:35:16 -07002614 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2615 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002616 __napi_schedule_irqoff(&tx_ring->napi);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002617 }
2618 }
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002619
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002620 return IRQ_HANDLED;
2621}
2622
Petri Gynther4055eae2015-03-25 12:35:16 -07002623/* bcmgenet_isr0: handle Rx and Tx default queues + other stuff */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002624static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
2625{
2626 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002627 struct bcmgenet_rx_ring *rx_ring;
2628 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08002629 unsigned int status;
2630 unsigned long flags;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002631
Doug Berger07c52d62017-03-09 16:58:47 -08002632 /* Read irq status */
2633 status = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002634 ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002635
Brian Norris7fc527f2014-07-29 14:34:14 -07002636 /* clear interrupts */
Doug Berger07c52d62017-03-09 16:58:47 -08002637 bcmgenet_intrl2_0_writel(priv, status, INTRL2_CPU_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002638
2639 netif_dbg(priv, intr, priv->dev,
Doug Berger07c52d62017-03-09 16:58:47 -08002640 "IRQ=0x%x\n", status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002641
Doug Berger07c52d62017-03-09 16:58:47 -08002642 if (status & UMAC_IRQ_RXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07002643 rx_ring = &priv->rx_rings[DESC_INDEX];
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002644
Petri Gynther4055eae2015-03-25 12:35:16 -07002645 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2646 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002647 __napi_schedule_irqoff(&rx_ring->napi);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002648 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002649 }
Petri Gynther4055eae2015-03-25 12:35:16 -07002650
Doug Berger07c52d62017-03-09 16:58:47 -08002651 if (status & UMAC_IRQ_TXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07002652 tx_ring = &priv->tx_rings[DESC_INDEX];
2653
2654 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2655 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002656 __napi_schedule_irqoff(&tx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07002657 }
2658 }
2659
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002660 if (priv->irq0_stat & (UMAC_IRQ_PHY_DET_R |
2661 UMAC_IRQ_PHY_DET_F |
Petri Gynthere122966d2015-03-30 00:29:24 -07002662 UMAC_IRQ_LINK_EVENT |
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002663 UMAC_IRQ_HFB_SM |
Doug Bergerb1ec4942017-03-13 17:41:36 -07002664 UMAC_IRQ_HFB_MM)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002665 /* all other interested interrupts handled in bottom half */
2666 schedule_work(&priv->bcmgenet_irq_work);
2667 }
2668
2669 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Doug Berger07c52d62017-03-09 16:58:47 -08002670 status & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002671 wake_up(&priv->wq);
2672 }
2673
Doug Berger07c52d62017-03-09 16:58:47 -08002674 /* all other interested interrupts handled in bottom half */
2675 status &= (UMAC_IRQ_LINK_EVENT |
2676 UMAC_IRQ_MPD_R);
2677 if (status) {
2678 /* Save irq status for bottom-half processing. */
2679 spin_lock_irqsave(&priv->lock, flags);
2680 priv->irq0_stat |= status;
2681 spin_unlock_irqrestore(&priv->lock, flags);
2682
2683 schedule_work(&priv->bcmgenet_irq_work);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002684 }
2685
2686 return IRQ_HANDLED;
2687}
2688
Florian Fainelli85620562014-07-21 15:29:23 -07002689static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
2690{
2691 struct bcmgenet_priv *priv = dev_id;
2692
2693 pm_wakeup_event(&priv->pdev->dev, 0);
2694
2695 return IRQ_HANDLED;
2696}
2697
Florian Fainelli4d2e8882015-07-31 11:42:54 -07002698#ifdef CONFIG_NET_POLL_CONTROLLER
2699static void bcmgenet_poll_controller(struct net_device *dev)
2700{
2701 struct bcmgenet_priv *priv = netdev_priv(dev);
2702
2703 /* Invoke the main RX/TX interrupt handler */
2704 disable_irq(priv->irq0);
2705 bcmgenet_isr0(priv->irq0, priv);
2706 enable_irq(priv->irq0);
2707
2708 /* And the interrupt handler for RX/TX priority queues */
2709 disable_irq(priv->irq1);
2710 bcmgenet_isr1(priv->irq1, priv);
2711 enable_irq(priv->irq1);
2712}
2713#endif
2714
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002715static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
2716{
2717 u32 reg;
2718
2719 reg = bcmgenet_rbuf_ctrl_get(priv);
2720 reg |= BIT(1);
2721 bcmgenet_rbuf_ctrl_set(priv, reg);
2722 udelay(10);
2723
2724 reg &= ~BIT(1);
2725 bcmgenet_rbuf_ctrl_set(priv, reg);
2726 udelay(10);
2727}
2728
2729static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002730 unsigned char *addr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002731{
2732 bcmgenet_umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
2733 (addr[2] << 8) | addr[3], UMAC_MAC0);
2734 bcmgenet_umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
2735}
2736
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002737/* Returns a reusable dma control register value */
2738static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
2739{
2740 u32 reg;
2741 u32 dma_ctrl;
2742
2743 /* disable DMA */
2744 dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
2745 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2746 reg &= ~dma_ctrl;
2747 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2748
2749 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2750 reg &= ~dma_ctrl;
2751 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2752
2753 bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
2754 udelay(10);
2755 bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
2756
2757 return dma_ctrl;
2758}
2759
2760static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
2761{
2762 u32 reg;
2763
2764 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2765 reg |= dma_ctrl;
2766 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2767
2768 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2769 reg |= dma_ctrl;
2770 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2771}
2772
Petri Gynther0034de42015-03-13 14:45:00 -07002773/* bcmgenet_hfb_clear
2774 *
2775 * Clear Hardware Filter Block and disable all filtering.
2776 */
2777static void bcmgenet_hfb_clear(struct bcmgenet_priv *priv)
2778{
2779 u32 i;
2780
2781 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_CTRL);
2782 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS);
2783 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS + 4);
2784
2785 for (i = DMA_INDEX2RING_0; i <= DMA_INDEX2RING_7; i++)
2786 bcmgenet_rdma_writel(priv, 0x0, i);
2787
2788 for (i = 0; i < (priv->hw_params->hfb_filter_cnt / 4); i++)
2789 bcmgenet_hfb_reg_writel(priv, 0x0,
2790 HFB_FLT_LEN_V3PLUS + i * sizeof(u32));
2791
2792 for (i = 0; i < priv->hw_params->hfb_filter_cnt *
2793 priv->hw_params->hfb_filter_size; i++)
2794 bcmgenet_hfb_writel(priv, 0x0, i * sizeof(u32));
2795}
2796
2797static void bcmgenet_hfb_init(struct bcmgenet_priv *priv)
2798{
2799 if (GENET_IS_V1(priv) || GENET_IS_V2(priv))
2800 return;
2801
2802 bcmgenet_hfb_clear(priv);
2803}
2804
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002805static void bcmgenet_netif_start(struct net_device *dev)
2806{
2807 struct bcmgenet_priv *priv = netdev_priv(dev);
2808
2809 /* Start the network engine */
Petri Gynther3ab11332015-03-25 12:35:15 -07002810 bcmgenet_enable_rx_napi(priv);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002811 bcmgenet_enable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002812
2813 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
2814
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002815 netif_tx_start_all_queues(dev);
2816
Florian Fainelli37850e32015-10-17 14:22:46 -07002817 /* Monitor link interrupts now */
2818 bcmgenet_link_intr_enable(priv);
2819
Florian Fainelli0299b6a2016-09-26 22:31:56 +02002820 phy_start(priv->phydev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002821}
2822
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002823static int bcmgenet_open(struct net_device *dev)
2824{
2825 struct bcmgenet_priv *priv = netdev_priv(dev);
2826 unsigned long dma_ctrl;
2827 u32 reg;
2828 int ret;
2829
2830 netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
2831
2832 /* Turn on the clock */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002833 clk_prepare_enable(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002834
Florian Fainellia642c4f2015-03-23 15:09:56 -07002835 /* If this is an internal GPHY, power it back on now, before UniMAC is
2836 * brought out of reset as absolutely no UniMAC activity is allowed
2837 */
Florian Fainellic624f892015-07-16 15:51:17 -07002838 if (priv->internal_phy)
Florian Fainellia642c4f2015-03-23 15:09:56 -07002839 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
2840
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002841 /* take MAC out of reset */
2842 bcmgenet_umac_reset(priv);
2843
2844 ret = init_umac(priv);
2845 if (ret)
2846 goto err_clk_disable;
2847
2848 /* disable ethernet MAC while updating its registers */
Florian Fainellie29585b2014-07-21 15:29:20 -07002849 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002850
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002851 /* Make sure we reflect the value of CRC_CMD_FWD */
2852 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
2853 priv->crc_fwd_en = !!(reg & CMD_CRC_FWD);
2854
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002855 bcmgenet_set_hw_addr(priv, dev->dev_addr);
2856
Florian Fainellic624f892015-07-16 15:51:17 -07002857 if (priv->internal_phy) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002858 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
2859 reg |= EXT_ENERGY_DET_MASK;
2860 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
2861 }
2862
2863 /* Disable RX/TX DMA and flush TX queues */
2864 dma_ctrl = bcmgenet_dma_disable(priv);
2865
2866 /* Reinitialize TDMA and RDMA and SW housekeeping */
2867 ret = bcmgenet_init_dma(priv);
2868 if (ret) {
2869 netdev_err(dev, "failed to initialize DMA\n");
Petri Gyntherfac25942015-03-30 00:29:13 -07002870 goto err_clk_disable;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002871 }
2872
2873 /* Always enable ring 16 - descriptor ring */
2874 bcmgenet_enable_dma(priv, dma_ctrl);
2875
Petri Gynther0034de42015-03-13 14:45:00 -07002876 /* HFB init */
2877 bcmgenet_hfb_init(priv);
2878
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002879 ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002880 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002881 if (ret < 0) {
2882 netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
2883 goto err_fini_dma;
2884 }
2885
2886 ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002887 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002888 if (ret < 0) {
2889 netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
2890 goto err_irq0;
2891 }
2892
Florian Fainelli6cc8e6d2015-07-16 15:51:18 -07002893 ret = bcmgenet_mii_probe(dev);
2894 if (ret) {
2895 netdev_err(dev, "failed to connect to PHY\n");
2896 goto err_irq1;
2897 }
Florian Fainellic96e7312014-11-10 18:06:20 -08002898
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002899 bcmgenet_netif_start(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002900
2901 return 0;
2902
Florian Fainelli6cc8e6d2015-07-16 15:51:18 -07002903err_irq1:
2904 free_irq(priv->irq1, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002905err_irq0:
Florian Fainelli978ffac42015-07-16 15:51:15 -07002906 free_irq(priv->irq0, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002907err_fini_dma:
2908 bcmgenet_fini_dma(priv);
2909err_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08002910 if (priv->internal_phy)
2911 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002912 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002913 return ret;
2914}
2915
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002916static void bcmgenet_netif_stop(struct net_device *dev)
2917{
2918 struct bcmgenet_priv *priv = netdev_priv(dev);
2919
2920 netif_tx_stop_all_queues(dev);
Florian Fainelli0299b6a2016-09-26 22:31:56 +02002921 phy_stop(priv->phydev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002922 bcmgenet_intr_disable(priv);
Petri Gynther3ab11332015-03-25 12:35:15 -07002923 bcmgenet_disable_rx_napi(priv);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002924 bcmgenet_disable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002925
2926 /* Wait for pending work items to complete. Since interrupts are
2927 * disabled no new work will be scheduled.
2928 */
2929 cancel_work_sync(&priv->bcmgenet_irq_work);
Florian Fainellicc013fb2014-08-11 14:50:43 -07002930
Florian Fainellicc013fb2014-08-11 14:50:43 -07002931 priv->old_link = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002932 priv->old_speed = -1;
Florian Fainellicc013fb2014-08-11 14:50:43 -07002933 priv->old_duplex = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002934 priv->old_pause = -1;
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002935}
2936
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002937static int bcmgenet_close(struct net_device *dev)
2938{
2939 struct bcmgenet_priv *priv = netdev_priv(dev);
2940 int ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002941
2942 netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
2943
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002944 bcmgenet_netif_stop(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002945
Florian Fainellic96e7312014-11-10 18:06:20 -08002946 /* Really kill the PHY state machine and disconnect from it */
Florian Fainelli0299b6a2016-09-26 22:31:56 +02002947 phy_disconnect(priv->phydev);
Florian Fainellic96e7312014-11-10 18:06:20 -08002948
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002949 /* Disable MAC receive */
Florian Fainellie29585b2014-07-21 15:29:20 -07002950 umac_enable_set(priv, CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002951
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002952 ret = bcmgenet_dma_teardown(priv);
2953 if (ret)
2954 return ret;
2955
Doug Berger556c2cf2017-03-13 17:41:34 -07002956 /* Disable MAC transmit. TX DMA disabled must be done before this */
Florian Fainellie29585b2014-07-21 15:29:20 -07002957 umac_enable_set(priv, CMD_TX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002958
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002959 /* tx reclaim */
2960 bcmgenet_tx_reclaim_all(dev);
2961 bcmgenet_fini_dma(priv);
2962
2963 free_irq(priv->irq0, priv);
2964 free_irq(priv->irq1, priv);
2965
Florian Fainellic624f892015-07-16 15:51:17 -07002966 if (priv->internal_phy)
Florian Fainellica8cf342015-03-23 15:09:51 -07002967 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002968
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002969 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002970
Florian Fainellica8cf342015-03-23 15:09:51 -07002971 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002972}
2973
Florian Fainelli13ea6572015-06-04 16:15:50 -07002974static void bcmgenet_dump_tx_queue(struct bcmgenet_tx_ring *ring)
2975{
2976 struct bcmgenet_priv *priv = ring->priv;
2977 u32 p_index, c_index, intsts, intmsk;
2978 struct netdev_queue *txq;
2979 unsigned int free_bds;
2980 unsigned long flags;
2981 bool txq_stopped;
2982
2983 if (!netif_msg_tx_err(priv))
2984 return;
2985
2986 txq = netdev_get_tx_queue(priv->dev, ring->queue);
2987
2988 spin_lock_irqsave(&ring->lock, flags);
2989 if (ring->index == DESC_INDEX) {
2990 intsts = ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
2991 intmsk = UMAC_IRQ_TXDMA_DONE | UMAC_IRQ_TXDMA_MBDONE;
2992 } else {
2993 intsts = ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
2994 intmsk = 1 << ring->index;
2995 }
2996 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
2997 p_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_PROD_INDEX);
2998 txq_stopped = netif_tx_queue_stopped(txq);
2999 free_bds = ring->free_bds;
3000 spin_unlock_irqrestore(&ring->lock, flags);
3001
3002 netif_err(priv, tx_err, priv->dev, "Ring %d queue %d status summary\n"
3003 "TX queue status: %s, interrupts: %s\n"
3004 "(sw)free_bds: %d (sw)size: %d\n"
3005 "(sw)p_index: %d (hw)p_index: %d\n"
3006 "(sw)c_index: %d (hw)c_index: %d\n"
3007 "(sw)clean_p: %d (sw)write_p: %d\n"
3008 "(sw)cb_ptr: %d (sw)end_ptr: %d\n",
3009 ring->index, ring->queue,
3010 txq_stopped ? "stopped" : "active",
3011 intsts & intmsk ? "enabled" : "disabled",
3012 free_bds, ring->size,
3013 ring->prod_index, p_index & DMA_P_INDEX_MASK,
3014 ring->c_index, c_index & DMA_C_INDEX_MASK,
3015 ring->clean_ptr, ring->write_ptr,
3016 ring->cb_ptr, ring->end_ptr);
3017}
3018
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003019static void bcmgenet_timeout(struct net_device *dev)
3020{
3021 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003022 u32 int0_enable = 0;
3023 u32 int1_enable = 0;
3024 unsigned int q;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003025
3026 netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
3027
Florian Fainelli13ea6572015-06-04 16:15:50 -07003028 for (q = 0; q < priv->hw_params->tx_queues; q++)
3029 bcmgenet_dump_tx_queue(&priv->tx_rings[q]);
3030 bcmgenet_dump_tx_queue(&priv->tx_rings[DESC_INDEX]);
3031
3032 bcmgenet_tx_reclaim_all(dev);
3033
3034 for (q = 0; q < priv->hw_params->tx_queues; q++)
3035 int1_enable |= (1 << q);
3036
3037 int0_enable = UMAC_IRQ_TXDMA_DONE;
3038
3039 /* Re-enable TX interrupts if disabled */
3040 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
3041 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
3042
Florian Westphal860e9532016-05-03 16:33:13 +02003043 netif_trans_update(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003044
3045 dev->stats.tx_errors++;
3046
3047 netif_tx_wake_all_queues(dev);
3048}
3049
3050#define MAX_MC_COUNT 16
3051
3052static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
3053 unsigned char *addr,
3054 int *i,
3055 int *mc)
3056{
3057 u32 reg;
3058
Florian Fainellic91b7f62014-07-23 10:42:12 -07003059 bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
3060 UMAC_MDF_ADDR + (*i * 4));
3061 bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
3062 addr[4] << 8 | addr[5],
3063 UMAC_MDF_ADDR + ((*i + 1) * 4));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003064 reg = bcmgenet_umac_readl(priv, UMAC_MDF_CTRL);
3065 reg |= (1 << (MAX_MC_COUNT - *mc));
3066 bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
3067 *i += 2;
3068 (*mc)++;
3069}
3070
3071static void bcmgenet_set_rx_mode(struct net_device *dev)
3072{
3073 struct bcmgenet_priv *priv = netdev_priv(dev);
3074 struct netdev_hw_addr *ha;
3075 int i, mc;
3076 u32 reg;
3077
3078 netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
3079
Brian Norris7fc527f2014-07-29 14:34:14 -07003080 /* Promiscuous mode */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003081 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
3082 if (dev->flags & IFF_PROMISC) {
3083 reg |= CMD_PROMISC;
3084 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3085 bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
3086 return;
3087 } else {
3088 reg &= ~CMD_PROMISC;
3089 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3090 }
3091
3092 /* UniMac doesn't support ALLMULTI */
3093 if (dev->flags & IFF_ALLMULTI) {
3094 netdev_warn(dev, "ALLMULTI is not supported\n");
3095 return;
3096 }
3097
3098 /* update MDF filter */
3099 i = 0;
3100 mc = 0;
3101 /* Broadcast */
3102 bcmgenet_set_mdf_addr(priv, dev->broadcast, &i, &mc);
3103 /* my own address.*/
3104 bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i, &mc);
3105 /* Unicast list*/
3106 if (netdev_uc_count(dev) > (MAX_MC_COUNT - mc))
3107 return;
3108
3109 if (!netdev_uc_empty(dev))
3110 netdev_for_each_uc_addr(ha, dev)
3111 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
3112 /* Multicast */
3113 if (netdev_mc_empty(dev) || netdev_mc_count(dev) >= (MAX_MC_COUNT - mc))
3114 return;
3115
3116 netdev_for_each_mc_addr(ha, dev)
3117 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
3118}
3119
3120/* Set the hardware MAC address. */
3121static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
3122{
3123 struct sockaddr *addr = p;
3124
3125 /* Setting the MAC address at the hardware level is not possible
3126 * without disabling the UniMAC RX/TX enable bits.
3127 */
3128 if (netif_running(dev))
3129 return -EBUSY;
3130
3131 ether_addr_copy(dev->dev_addr, addr->sa_data);
3132
3133 return 0;
3134}
3135
Florian Fainelli37a30b42017-03-16 10:27:08 -07003136static struct net_device_stats *bcmgenet_get_stats(struct net_device *dev)
3137{
3138 struct bcmgenet_priv *priv = netdev_priv(dev);
3139 unsigned long tx_bytes = 0, tx_packets = 0;
3140 unsigned long rx_bytes = 0, rx_packets = 0;
3141 unsigned long rx_errors = 0, rx_dropped = 0;
3142 struct bcmgenet_tx_ring *tx_ring;
3143 struct bcmgenet_rx_ring *rx_ring;
3144 unsigned int q;
3145
3146 for (q = 0; q < priv->hw_params->tx_queues; q++) {
3147 tx_ring = &priv->tx_rings[q];
3148 tx_bytes += tx_ring->bytes;
3149 tx_packets += tx_ring->packets;
3150 }
3151 tx_ring = &priv->tx_rings[DESC_INDEX];
3152 tx_bytes += tx_ring->bytes;
3153 tx_packets += tx_ring->packets;
3154
3155 for (q = 0; q < priv->hw_params->rx_queues; q++) {
3156 rx_ring = &priv->rx_rings[q];
3157
3158 rx_bytes += rx_ring->bytes;
3159 rx_packets += rx_ring->packets;
3160 rx_errors += rx_ring->errors;
3161 rx_dropped += rx_ring->dropped;
3162 }
3163 rx_ring = &priv->rx_rings[DESC_INDEX];
3164 rx_bytes += rx_ring->bytes;
3165 rx_packets += rx_ring->packets;
3166 rx_errors += rx_ring->errors;
3167 rx_dropped += rx_ring->dropped;
3168
3169 dev->stats.tx_bytes = tx_bytes;
3170 dev->stats.tx_packets = tx_packets;
3171 dev->stats.rx_bytes = rx_bytes;
3172 dev->stats.rx_packets = rx_packets;
3173 dev->stats.rx_errors = rx_errors;
3174 dev->stats.rx_missed_errors = rx_errors;
3175 return &dev->stats;
3176}
3177
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003178static const struct net_device_ops bcmgenet_netdev_ops = {
3179 .ndo_open = bcmgenet_open,
3180 .ndo_stop = bcmgenet_close,
3181 .ndo_start_xmit = bcmgenet_xmit,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003182 .ndo_tx_timeout = bcmgenet_timeout,
3183 .ndo_set_rx_mode = bcmgenet_set_rx_mode,
3184 .ndo_set_mac_address = bcmgenet_set_mac_addr,
3185 .ndo_do_ioctl = bcmgenet_ioctl,
3186 .ndo_set_features = bcmgenet_set_features,
Florian Fainelli4d2e8882015-07-31 11:42:54 -07003187#ifdef CONFIG_NET_POLL_CONTROLLER
3188 .ndo_poll_controller = bcmgenet_poll_controller,
3189#endif
Florian Fainelli37a30b42017-03-16 10:27:08 -07003190 .ndo_get_stats = bcmgenet_get_stats,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003191};
3192
3193/* Array of GENET hardware parameters/characteristics */
3194static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
3195 [GENET_V1] = {
3196 .tx_queues = 0,
Petri Gynther51a966a2015-02-23 11:00:46 -08003197 .tx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003198 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003199 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003200 .bp_in_en_shift = 16,
3201 .bp_in_mask = 0xffff,
3202 .hfb_filter_cnt = 16,
3203 .qtag_mask = 0x1F,
3204 .hfb_offset = 0x1000,
3205 .rdma_offset = 0x2000,
3206 .tdma_offset = 0x3000,
3207 .words_per_bd = 2,
3208 },
3209 [GENET_V2] = {
3210 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003211 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003212 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003213 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003214 .bp_in_en_shift = 16,
3215 .bp_in_mask = 0xffff,
3216 .hfb_filter_cnt = 16,
3217 .qtag_mask = 0x1F,
3218 .tbuf_offset = 0x0600,
3219 .hfb_offset = 0x1000,
3220 .hfb_reg_offset = 0x2000,
3221 .rdma_offset = 0x3000,
3222 .tdma_offset = 0x4000,
3223 .words_per_bd = 2,
3224 .flags = GENET_HAS_EXT,
3225 },
3226 [GENET_V3] = {
3227 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003228 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003229 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003230 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003231 .bp_in_en_shift = 17,
3232 .bp_in_mask = 0x1ffff,
3233 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003234 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003235 .qtag_mask = 0x3F,
3236 .tbuf_offset = 0x0600,
3237 .hfb_offset = 0x8000,
3238 .hfb_reg_offset = 0xfc00,
3239 .rdma_offset = 0x10000,
3240 .tdma_offset = 0x11000,
3241 .words_per_bd = 2,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003242 .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR |
3243 GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003244 },
3245 [GENET_V4] = {
3246 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003247 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003248 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003249 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003250 .bp_in_en_shift = 17,
3251 .bp_in_mask = 0x1ffff,
3252 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003253 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003254 .qtag_mask = 0x3F,
3255 .tbuf_offset = 0x0600,
3256 .hfb_offset = 0x8000,
3257 .hfb_reg_offset = 0xfc00,
3258 .rdma_offset = 0x2000,
3259 .tdma_offset = 0x4000,
3260 .words_per_bd = 3,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003261 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3262 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003263 },
Doug Berger42138082017-03-13 17:41:42 -07003264 [GENET_V5] = {
3265 .tx_queues = 4,
3266 .tx_bds_per_q = 32,
3267 .rx_queues = 0,
3268 .rx_bds_per_q = 0,
3269 .bp_in_en_shift = 17,
3270 .bp_in_mask = 0x1ffff,
3271 .hfb_filter_cnt = 48,
3272 .hfb_filter_size = 128,
3273 .qtag_mask = 0x3F,
3274 .tbuf_offset = 0x0600,
3275 .hfb_offset = 0x8000,
3276 .hfb_reg_offset = 0xfc00,
3277 .rdma_offset = 0x2000,
3278 .tdma_offset = 0x4000,
3279 .words_per_bd = 3,
3280 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3281 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
3282 },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003283};
3284
3285/* Infer hardware parameters from the detected GENET version */
3286static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
3287{
3288 struct bcmgenet_hw_params *params;
3289 u32 reg;
3290 u8 major;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003291 u16 gphy_rev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003292
Doug Berger42138082017-03-13 17:41:42 -07003293 if (GENET_IS_V5(priv) || GENET_IS_V4(priv)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003294 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3295 genet_dma_ring_regs = genet_dma_ring_regs_v4;
3296 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003297 } else if (GENET_IS_V3(priv)) {
3298 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3299 genet_dma_ring_regs = genet_dma_ring_regs_v123;
3300 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003301 } else if (GENET_IS_V2(priv)) {
3302 bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
3303 genet_dma_ring_regs = genet_dma_ring_regs_v123;
3304 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003305 } else if (GENET_IS_V1(priv)) {
3306 bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
3307 genet_dma_ring_regs = genet_dma_ring_regs_v123;
3308 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003309 }
3310
3311 /* enum genet_version starts at 1 */
3312 priv->hw_params = &bcmgenet_hw_params[priv->version];
3313 params = priv->hw_params;
3314
3315 /* Read GENET HW version */
3316 reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
3317 major = (reg >> 24 & 0x0f);
Doug Berger42138082017-03-13 17:41:42 -07003318 if (major == 6)
3319 major = 5;
3320 else if (major == 5)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003321 major = 4;
3322 else if (major == 0)
3323 major = 1;
3324 if (major != priv->version) {
3325 dev_err(&priv->pdev->dev,
3326 "GENET version mismatch, got: %d, configured for: %d\n",
3327 major, priv->version);
3328 }
3329
3330 /* Print the GENET core version */
3331 dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003332 major, (reg >> 16) & 0x0f, reg & 0xffff);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003333
Florian Fainelli487320c2014-09-19 13:07:53 -07003334 /* Store the integrated PHY revision for the MDIO probing function
3335 * to pass this information to the PHY driver. The PHY driver expects
3336 * to find the PHY major revision in bits 15:8 while the GENET register
3337 * stores that information in bits 7:0, account for that.
Florian Fainellib04a2f52014-12-03 09:56:59 -08003338 *
3339 * On newer chips, starting with PHY revision G0, a new scheme is
3340 * deployed similar to the Starfighter 2 switch with GPHY major
3341 * revision in bits 15:8 and patch level in bits 7:0. Major revision 0
3342 * is reserved as well as special value 0x01ff, we have a small
3343 * heuristic to check for the new GPHY revision and re-arrange things
3344 * so the GPHY driver is happy.
Florian Fainelli487320c2014-09-19 13:07:53 -07003345 */
Florian Fainellib04a2f52014-12-03 09:56:59 -08003346 gphy_rev = reg & 0xffff;
3347
Doug Berger42138082017-03-13 17:41:42 -07003348 if (GENET_IS_V5(priv)) {
3349 /* The EPHY revision should come from the MDIO registers of
3350 * the PHY not from GENET.
3351 */
3352 if (gphy_rev != 0) {
3353 pr_warn("GENET is reporting EPHY revision: 0x%04x\n",
3354 gphy_rev);
3355 }
Doug Bergereca4bad2017-03-09 16:58:45 -08003356 /* This is reserved so should require special treatment */
David S. Miller101c4312017-03-15 11:59:10 -07003357 } else if (gphy_rev == 0 || gphy_rev == 0x01ff) {
Doug Bergereca4bad2017-03-09 16:58:45 -08003358 pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev);
3359 return;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003360 /* This is the good old scheme, just GPHY major, no minor nor patch */
Doug Berger42138082017-03-13 17:41:42 -07003361 } else if ((gphy_rev & 0xf0) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003362 priv->gphy_rev = gphy_rev << 8;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003363 /* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */
Doug Berger42138082017-03-13 17:41:42 -07003364 } else if ((gphy_rev & 0xff00) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003365 priv->gphy_rev = gphy_rev;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003366 }
Florian Fainelli487320c2014-09-19 13:07:53 -07003367
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003368#ifdef CONFIG_PHYS_ADDR_T_64BIT
3369 if (!(params->flags & GENET_HAS_40BITS))
3370 pr_warn("GENET does not support 40-bits PA\n");
3371#endif
3372
3373 pr_debug("Configuration for version: %d\n"
Petri Gynther3feafa02015-03-05 17:40:14 -08003374 "TXq: %1d, TXqBDs: %1d, RXq: %1d, RXqBDs: %1d\n"
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003375 "BP << en: %2d, BP msk: 0x%05x\n"
3376 "HFB count: %2d, QTAQ msk: 0x%05x\n"
3377 "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
3378 "RDMA: 0x%05x, TDMA: 0x%05x\n"
3379 "Words/BD: %d\n",
3380 priv->version,
Petri Gynther51a966a2015-02-23 11:00:46 -08003381 params->tx_queues, params->tx_bds_per_q,
Petri Gynther3feafa02015-03-05 17:40:14 -08003382 params->rx_queues, params->rx_bds_per_q,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003383 params->bp_in_en_shift, params->bp_in_mask,
3384 params->hfb_filter_cnt, params->qtag_mask,
3385 params->tbuf_offset, params->hfb_offset,
3386 params->hfb_reg_offset,
3387 params->rdma_offset, params->tdma_offset,
3388 params->words_per_bd);
3389}
3390
3391static const struct of_device_id bcmgenet_match[] = {
3392 { .compatible = "brcm,genet-v1", .data = (void *)GENET_V1 },
3393 { .compatible = "brcm,genet-v2", .data = (void *)GENET_V2 },
3394 { .compatible = "brcm,genet-v3", .data = (void *)GENET_V3 },
3395 { .compatible = "brcm,genet-v4", .data = (void *)GENET_V4 },
Doug Berger42138082017-03-13 17:41:42 -07003396 { .compatible = "brcm,genet-v5", .data = (void *)GENET_V5 },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003397 { },
3398};
Luis de Bethencourte8048e52015-09-18 17:55:02 +02003399MODULE_DEVICE_TABLE(of, bcmgenet_match);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003400
3401static int bcmgenet_probe(struct platform_device *pdev)
3402{
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003403 struct bcmgenet_platform_data *pd = pdev->dev.platform_data;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003404 struct device_node *dn = pdev->dev.of_node;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003405 const struct of_device_id *of_id = NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003406 struct bcmgenet_priv *priv;
3407 struct net_device *dev;
3408 const void *macaddr;
3409 struct resource *r;
3410 int err = -EIO;
Doug Berger6be371b2017-03-09 16:58:48 -08003411 const char *phy_mode_str;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003412
Petri Gynther3feafee2015-03-05 17:40:12 -08003413 /* Up to GENET_MAX_MQ_CNT + 1 TX queues and RX queues */
3414 dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1,
3415 GENET_MAX_MQ_CNT + 1);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003416 if (!dev) {
3417 dev_err(&pdev->dev, "can't allocate net device\n");
3418 return -ENOMEM;
3419 }
3420
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003421 if (dn) {
3422 of_id = of_match_node(bcmgenet_match, dn);
3423 if (!of_id)
3424 return -EINVAL;
3425 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003426
3427 priv = netdev_priv(dev);
3428 priv->irq0 = platform_get_irq(pdev, 0);
3429 priv->irq1 = platform_get_irq(pdev, 1);
Florian Fainelli85620562014-07-21 15:29:23 -07003430 priv->wol_irq = platform_get_irq(pdev, 2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003431 if (!priv->irq0 || !priv->irq1) {
3432 dev_err(&pdev->dev, "can't find IRQs\n");
3433 err = -EINVAL;
3434 goto err;
3435 }
3436
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003437 if (dn) {
3438 macaddr = of_get_mac_address(dn);
3439 if (!macaddr) {
3440 dev_err(&pdev->dev, "can't find MAC address\n");
3441 err = -EINVAL;
3442 goto err;
3443 }
3444 } else {
3445 macaddr = pd->mac_address;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003446 }
3447
3448 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam5343a102014-02-24 00:47:24 -03003449 priv->base = devm_ioremap_resource(&pdev->dev, r);
3450 if (IS_ERR(priv->base)) {
3451 err = PTR_ERR(priv->base);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003452 goto err;
3453 }
3454
Doug Berger07c52d62017-03-09 16:58:47 -08003455 spin_lock_init(&priv->lock);
3456
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003457 SET_NETDEV_DEV(dev, &pdev->dev);
3458 dev_set_drvdata(&pdev->dev, dev);
3459 ether_addr_copy(dev->dev_addr, macaddr);
3460 dev->watchdog_timeo = 2 * HZ;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003461 dev->ethtool_ops = &bcmgenet_ethtool_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003462 dev->netdev_ops = &bcmgenet_netdev_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003463
3464 priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
3465
3466 /* Set hardware features */
3467 dev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM |
3468 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
3469
Florian Fainelli85620562014-07-21 15:29:23 -07003470 /* Request the WOL interrupt and advertise suspend if available */
3471 priv->wol_irq_disabled = true;
3472 err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0,
3473 dev->name, priv);
3474 if (!err)
3475 device_set_wakeup_capable(&pdev->dev, 1);
3476
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003477 /* Set the needed headroom to account for any possible
3478 * features enabling/disabling at runtime
3479 */
3480 dev->needed_headroom += 64;
3481
3482 netdev_boot_setup_check(dev);
3483
3484 priv->dev = dev;
3485 priv->pdev = pdev;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003486 if (of_id)
3487 priv->version = (enum bcmgenet_version)of_id->data;
3488 else
3489 priv->version = pd->genet_version;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003490
Florian Fainellie4a60a92014-08-11 14:50:42 -07003491 priv->clk = devm_clk_get(&priv->pdev->dev, "enet");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003492 if (IS_ERR(priv->clk)) {
Florian Fainellie4a60a92014-08-11 14:50:42 -07003493 dev_warn(&priv->pdev->dev, "failed to get enet clock\n");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003494 priv->clk = NULL;
3495 }
Florian Fainellie4a60a92014-08-11 14:50:42 -07003496
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003497 clk_prepare_enable(priv->clk);
Florian Fainellie4a60a92014-08-11 14:50:42 -07003498
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003499 bcmgenet_set_hw_params(priv);
3500
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003501 /* Mii wait queue */
3502 init_waitqueue_head(&priv->wq);
3503 /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
3504 priv->rx_buf_len = RX_BUF_LENGTH;
3505 INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
3506
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003507 priv->clk_wol = devm_clk_get(&priv->pdev->dev, "enet-wol");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003508 if (IS_ERR(priv->clk_wol)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003509 dev_warn(&priv->pdev->dev, "failed to get enet-wol clock\n");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003510 priv->clk_wol = NULL;
3511 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003512
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003513 priv->clk_eee = devm_clk_get(&priv->pdev->dev, "enet-eee");
3514 if (IS_ERR(priv->clk_eee)) {
3515 dev_warn(&priv->pdev->dev, "failed to get enet-eee clock\n");
3516 priv->clk_eee = NULL;
3517 }
3518
Doug Berger6be371b2017-03-09 16:58:48 -08003519 /* If this is an internal GPHY, power it on now, before UniMAC is
3520 * brought out of reset as absolutely no UniMAC activity is allowed
3521 */
3522 if (dn && !of_property_read_string(dn, "phy-mode", &phy_mode_str) &&
3523 !strcasecmp(phy_mode_str, "internal"))
3524 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3525
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003526 err = reset_umac(priv);
3527 if (err)
3528 goto err_clk_disable;
3529
3530 err = bcmgenet_mii_init(dev);
3531 if (err)
3532 goto err_clk_disable;
3533
3534 /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
3535 * just the ring 16 descriptor based TX
3536 */
3537 netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
3538 netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
3539
Florian Fainelli219575e2014-06-26 10:26:21 -07003540 /* libphy will determine the link state */
3541 netif_carrier_off(dev);
3542
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003543 /* Turn off the main clock, WOL clock is handled separately */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003544 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003545
Florian Fainelli0f50ce92014-06-26 10:26:20 -07003546 err = register_netdev(dev);
3547 if (err)
3548 goto err;
3549
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003550 return err;
3551
3552err_clk_disable:
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003553 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003554err:
3555 free_netdev(dev);
3556 return err;
3557}
3558
3559static int bcmgenet_remove(struct platform_device *pdev)
3560{
3561 struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
3562
3563 dev_set_drvdata(&pdev->dev, NULL);
3564 unregister_netdev(priv->dev);
3565 bcmgenet_mii_exit(priv->dev);
3566 free_netdev(priv->dev);
3567
3568 return 0;
3569}
3570
Florian Fainellib6e978e2014-07-21 15:29:22 -07003571#ifdef CONFIG_PM_SLEEP
3572static int bcmgenet_suspend(struct device *d)
3573{
3574 struct net_device *dev = dev_get_drvdata(d);
3575 struct bcmgenet_priv *priv = netdev_priv(dev);
3576 int ret;
3577
3578 if (!netif_running(dev))
3579 return 0;
3580
3581 bcmgenet_netif_stop(dev);
3582
Florian Fainelli5371bbf42017-03-15 12:57:21 -07003583 if (!device_may_wakeup(d))
3584 phy_suspend(priv->phydev);
Florian Fainellicc013fb2014-08-11 14:50:43 -07003585
Florian Fainellib6e978e2014-07-21 15:29:22 -07003586 netif_device_detach(dev);
3587
3588 /* Disable MAC receive */
3589 umac_enable_set(priv, CMD_RX_EN, false);
3590
3591 ret = bcmgenet_dma_teardown(priv);
3592 if (ret)
3593 return ret;
3594
Doug Berger556c2cf2017-03-13 17:41:34 -07003595 /* Disable MAC transmit. TX DMA disabled must be done before this */
Florian Fainellib6e978e2014-07-21 15:29:22 -07003596 umac_enable_set(priv, CMD_TX_EN, false);
3597
3598 /* tx reclaim */
3599 bcmgenet_tx_reclaim_all(dev);
3600 bcmgenet_fini_dma(priv);
3601
Florian Fainelli8c90db72014-07-21 15:29:28 -07003602 /* Prepare the device for Wake-on-LAN and switch to the slow clock */
3603 if (device_may_wakeup(d) && priv->wolopts) {
Florian Fainellica8cf342015-03-23 15:09:51 -07003604 ret = bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003605 clk_prepare_enable(priv->clk_wol);
Florian Fainellic624f892015-07-16 15:51:17 -07003606 } else if (priv->internal_phy) {
Florian Fainellia6f31f52015-03-23 15:09:57 -07003607 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003608 }
3609
Florian Fainellib6e978e2014-07-21 15:29:22 -07003610 /* Turn off the clocks */
3611 clk_disable_unprepare(priv->clk);
3612
Florian Fainellica8cf342015-03-23 15:09:51 -07003613 return ret;
Florian Fainellib6e978e2014-07-21 15:29:22 -07003614}
3615
3616static int bcmgenet_resume(struct device *d)
3617{
3618 struct net_device *dev = dev_get_drvdata(d);
3619 struct bcmgenet_priv *priv = netdev_priv(dev);
3620 unsigned long dma_ctrl;
3621 int ret;
3622 u32 reg;
3623
3624 if (!netif_running(dev))
3625 return 0;
3626
3627 /* Turn on the clock */
3628 ret = clk_prepare_enable(priv->clk);
3629 if (ret)
3630 return ret;
3631
Florian Fainellia6f31f52015-03-23 15:09:57 -07003632 /* If this is an internal GPHY, power it back on now, before UniMAC is
3633 * brought out of reset as absolutely no UniMAC activity is allowed
3634 */
Florian Fainellic624f892015-07-16 15:51:17 -07003635 if (priv->internal_phy)
Florian Fainellia6f31f52015-03-23 15:09:57 -07003636 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3637
Florian Fainellib6e978e2014-07-21 15:29:22 -07003638 bcmgenet_umac_reset(priv);
3639
3640 ret = init_umac(priv);
3641 if (ret)
3642 goto out_clk_disable;
3643
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003644 /* From WOL-enabled suspend, switch to regular clock */
3645 if (priv->wolopts)
3646 clk_disable_unprepare(priv->clk_wol);
3647
Florian Fainelli0299b6a2016-09-26 22:31:56 +02003648 phy_init_hw(priv->phydev);
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003649 /* Speed settings must be restored */
Florian Fainelli28b45912015-07-16 15:51:19 -07003650 bcmgenet_mii_config(priv->dev);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003651
Florian Fainellib6e978e2014-07-21 15:29:22 -07003652 /* disable ethernet MAC while updating its registers */
3653 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
3654
3655 bcmgenet_set_hw_addr(priv, dev->dev_addr);
3656
Florian Fainellic624f892015-07-16 15:51:17 -07003657 if (priv->internal_phy) {
Florian Fainellib6e978e2014-07-21 15:29:22 -07003658 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
3659 reg |= EXT_ENERGY_DET_MASK;
3660 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
3661 }
3662
Florian Fainelli98bb7392014-08-11 14:50:45 -07003663 if (priv->wolopts)
3664 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
3665
Florian Fainellib6e978e2014-07-21 15:29:22 -07003666 /* Disable RX/TX DMA and flush TX queues */
3667 dma_ctrl = bcmgenet_dma_disable(priv);
3668
3669 /* Reinitialize TDMA and RDMA and SW housekeeping */
3670 ret = bcmgenet_init_dma(priv);
3671 if (ret) {
3672 netdev_err(dev, "failed to initialize DMA\n");
3673 goto out_clk_disable;
3674 }
3675
3676 /* Always enable ring 16 - descriptor ring */
3677 bcmgenet_enable_dma(priv, dma_ctrl);
3678
3679 netif_device_attach(dev);
3680
Florian Fainelli5371bbf42017-03-15 12:57:21 -07003681 if (!device_may_wakeup(d))
3682 phy_resume(priv->phydev);
Florian Fainellicc013fb2014-08-11 14:50:43 -07003683
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003684 if (priv->eee.eee_enabled)
3685 bcmgenet_eee_enable_set(dev, true);
3686
Florian Fainellib6e978e2014-07-21 15:29:22 -07003687 bcmgenet_netif_start(dev);
3688
3689 return 0;
3690
3691out_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08003692 if (priv->internal_phy)
3693 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainellib6e978e2014-07-21 15:29:22 -07003694 clk_disable_unprepare(priv->clk);
3695 return ret;
3696}
3697#endif /* CONFIG_PM_SLEEP */
3698
3699static SIMPLE_DEV_PM_OPS(bcmgenet_pm_ops, bcmgenet_suspend, bcmgenet_resume);
3700
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003701static struct platform_driver bcmgenet_driver = {
3702 .probe = bcmgenet_probe,
3703 .remove = bcmgenet_remove,
3704 .driver = {
3705 .name = "bcmgenet",
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003706 .of_match_table = bcmgenet_match,
Florian Fainellib6e978e2014-07-21 15:29:22 -07003707 .pm = &bcmgenet_pm_ops,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003708 },
3709};
3710module_platform_driver(bcmgenet_driver);
3711
3712MODULE_AUTHOR("Broadcom Corporation");
3713MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
3714MODULE_ALIAS("platform:bcmgenet");
3715MODULE_LICENSE("GPL");