blob: f493276d432a17dd58bf2e9fcfb8754f99783e82 [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
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200480 return phy_ethtool_ksettings_get(priv->phydev, cmd);
Philippe Reynesbac65c42016-07-09 00:54:47 +0200481}
482
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200483static int bcmgenet_set_link_ksettings(struct net_device *dev,
484 const struct ethtool_link_ksettings *cmd)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200485{
Florian Fainelli0299b6a2016-09-26 22:31:56 +0200486 struct bcmgenet_priv *priv = netdev_priv(dev);
487
Philippe Reynesbac65c42016-07-09 00:54:47 +0200488 if (!netif_running(dev))
489 return -EINVAL;
490
Florian Fainelli0299b6a2016-09-26 22:31:56 +0200491 if (!priv->phydev)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200492 return -ENODEV;
493
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200494 return phy_ethtool_ksettings_set(priv->phydev, cmd);
Philippe Reynesbac65c42016-07-09 00:54:47 +0200495}
496
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800497static int bcmgenet_set_rx_csum(struct net_device *dev,
498 netdev_features_t wanted)
499{
500 struct bcmgenet_priv *priv = netdev_priv(dev);
501 u32 rbuf_chk_ctrl;
502 bool rx_csum_en;
503
504 rx_csum_en = !!(wanted & NETIF_F_RXCSUM);
505
506 rbuf_chk_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CHK_CTRL);
507
508 /* enable rx checksumming */
509 if (rx_csum_en)
510 rbuf_chk_ctrl |= RBUF_RXCHK_EN;
511 else
512 rbuf_chk_ctrl &= ~RBUF_RXCHK_EN;
513 priv->desc_rxchk_en = rx_csum_en;
Florian Fainelliebe5e3c2014-03-26 21:18:39 -0700514
515 /* If UniMAC forwards CRC, we need to skip over it to get
516 * a valid CHK bit to be set in the per-packet status word
517 */
518 if (rx_csum_en && priv->crc_fwd_en)
519 rbuf_chk_ctrl |= RBUF_SKIP_FCS;
520 else
521 rbuf_chk_ctrl &= ~RBUF_SKIP_FCS;
522
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800523 bcmgenet_rbuf_writel(priv, rbuf_chk_ctrl, RBUF_CHK_CTRL);
524
525 return 0;
526}
527
528static int bcmgenet_set_tx_csum(struct net_device *dev,
529 netdev_features_t wanted)
530{
531 struct bcmgenet_priv *priv = netdev_priv(dev);
532 bool desc_64b_en;
533 u32 tbuf_ctrl, rbuf_ctrl;
534
535 tbuf_ctrl = bcmgenet_tbuf_ctrl_get(priv);
536 rbuf_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
537
538 desc_64b_en = !!(wanted & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
539
540 /* enable 64 bytes descriptor in both directions (RBUF and TBUF) */
541 if (desc_64b_en) {
542 tbuf_ctrl |= RBUF_64B_EN;
543 rbuf_ctrl |= RBUF_64B_EN;
544 } else {
545 tbuf_ctrl &= ~RBUF_64B_EN;
546 rbuf_ctrl &= ~RBUF_64B_EN;
547 }
548 priv->desc_64b_en = desc_64b_en;
549
550 bcmgenet_tbuf_ctrl_set(priv, tbuf_ctrl);
551 bcmgenet_rbuf_writel(priv, rbuf_ctrl, RBUF_CTRL);
552
553 return 0;
554}
555
556static int bcmgenet_set_features(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700557 netdev_features_t features)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800558{
559 netdev_features_t changed = features ^ dev->features;
560 netdev_features_t wanted = dev->wanted_features;
561 int ret = 0;
562
563 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
564 ret = bcmgenet_set_tx_csum(dev, wanted);
565 if (changed & (NETIF_F_RXCSUM))
566 ret = bcmgenet_set_rx_csum(dev, wanted);
567
568 return ret;
569}
570
571static u32 bcmgenet_get_msglevel(struct net_device *dev)
572{
573 struct bcmgenet_priv *priv = netdev_priv(dev);
574
575 return priv->msg_enable;
576}
577
578static void bcmgenet_set_msglevel(struct net_device *dev, u32 level)
579{
580 struct bcmgenet_priv *priv = netdev_priv(dev);
581
582 priv->msg_enable = level;
583}
584
Florian Fainelli2f913072015-09-16 16:47:39 -0700585static int bcmgenet_get_coalesce(struct net_device *dev,
586 struct ethtool_coalesce *ec)
587{
588 struct bcmgenet_priv *priv = netdev_priv(dev);
589
590 ec->tx_max_coalesced_frames =
591 bcmgenet_tdma_ring_readl(priv, DESC_INDEX,
592 DMA_MBUF_DONE_THRESH);
Florian Fainelli4a296452015-09-16 16:47:40 -0700593 ec->rx_max_coalesced_frames =
594 bcmgenet_rdma_ring_readl(priv, DESC_INDEX,
595 DMA_MBUF_DONE_THRESH);
596 ec->rx_coalesce_usecs =
597 bcmgenet_rdma_readl(priv, DMA_RING16_TIMEOUT) * 8192 / 1000;
Florian Fainelli2f913072015-09-16 16:47:39 -0700598
599 return 0;
600}
601
602static int bcmgenet_set_coalesce(struct net_device *dev,
603 struct ethtool_coalesce *ec)
604{
605 struct bcmgenet_priv *priv = netdev_priv(dev);
606 unsigned int i;
Florian Fainelli4a296452015-09-16 16:47:40 -0700607 u32 reg;
Florian Fainelli2f913072015-09-16 16:47:39 -0700608
Florian Fainelli4a296452015-09-16 16:47:40 -0700609 /* Base system clock is 125Mhz, DMA timeout is this reference clock
610 * divided by 1024, which yields roughly 8.192us, our maximum value
611 * has to fit in the DMA_TIMEOUT_MASK (16 bits)
612 */
Florian Fainelli2f913072015-09-16 16:47:39 -0700613 if (ec->tx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
Florian Fainelli4a296452015-09-16 16:47:40 -0700614 ec->tx_max_coalesced_frames == 0 ||
615 ec->rx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
616 ec->rx_coalesce_usecs > (DMA_TIMEOUT_MASK * 8) + 1)
617 return -EINVAL;
618
619 if (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0)
Florian Fainelli2f913072015-09-16 16:47:39 -0700620 return -EINVAL;
621
622 /* GENET TDMA hardware does not support a configurable timeout, but will
623 * always generate an interrupt either after MBDONE packets have been
Doug Berger556c2cf2017-03-13 17:41:34 -0700624 * transmitted, or when the ring is empty.
Florian Fainelli2f913072015-09-16 16:47:39 -0700625 */
626 if (ec->tx_coalesce_usecs || ec->tx_coalesce_usecs_high ||
Florian Fainelli852bcaf2015-09-18 14:16:53 -0700627 ec->tx_coalesce_usecs_irq || ec->tx_coalesce_usecs_low)
Florian Fainelli2f913072015-09-16 16:47:39 -0700628 return -EOPNOTSUPP;
629
630 /* Program all TX queues with the same values, as there is no
631 * ethtool knob to do coalescing on a per-queue basis
632 */
633 for (i = 0; i < priv->hw_params->tx_queues; i++)
634 bcmgenet_tdma_ring_writel(priv, i,
635 ec->tx_max_coalesced_frames,
636 DMA_MBUF_DONE_THRESH);
637 bcmgenet_tdma_ring_writel(priv, DESC_INDEX,
638 ec->tx_max_coalesced_frames,
639 DMA_MBUF_DONE_THRESH);
640
Florian Fainelli4a296452015-09-16 16:47:40 -0700641 for (i = 0; i < priv->hw_params->rx_queues; i++) {
642 bcmgenet_rdma_ring_writel(priv, i,
643 ec->rx_max_coalesced_frames,
644 DMA_MBUF_DONE_THRESH);
645
646 reg = bcmgenet_rdma_readl(priv, DMA_RING0_TIMEOUT + i);
647 reg &= ~DMA_TIMEOUT_MASK;
648 reg |= DIV_ROUND_UP(ec->rx_coalesce_usecs * 1000, 8192);
649 bcmgenet_rdma_writel(priv, reg, DMA_RING0_TIMEOUT + i);
650 }
651
652 bcmgenet_rdma_ring_writel(priv, DESC_INDEX,
653 ec->rx_max_coalesced_frames,
654 DMA_MBUF_DONE_THRESH);
655
656 reg = bcmgenet_rdma_readl(priv, DMA_RING16_TIMEOUT);
657 reg &= ~DMA_TIMEOUT_MASK;
658 reg |= DIV_ROUND_UP(ec->rx_coalesce_usecs * 1000, 8192);
659 bcmgenet_rdma_writel(priv, reg, DMA_RING16_TIMEOUT);
660
Florian Fainelli2f913072015-09-16 16:47:39 -0700661 return 0;
662}
663
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800664/* standard ethtool support functions. */
665enum bcmgenet_stat_type {
666 BCMGENET_STAT_NETDEV = -1,
667 BCMGENET_STAT_MIB_RX,
668 BCMGENET_STAT_MIB_TX,
669 BCMGENET_STAT_RUNT,
670 BCMGENET_STAT_MISC,
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800671 BCMGENET_STAT_SOFT,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800672};
673
674struct bcmgenet_stats {
675 char stat_string[ETH_GSTRING_LEN];
676 int stat_sizeof;
677 int stat_offset;
678 enum bcmgenet_stat_type type;
679 /* reg offset from UMAC base for misc counters */
680 u16 reg_offset;
681};
682
683#define STAT_NETDEV(m) { \
684 .stat_string = __stringify(m), \
685 .stat_sizeof = sizeof(((struct net_device_stats *)0)->m), \
686 .stat_offset = offsetof(struct net_device_stats, m), \
687 .type = BCMGENET_STAT_NETDEV, \
688}
689
690#define STAT_GENET_MIB(str, m, _type) { \
691 .stat_string = str, \
692 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
693 .stat_offset = offsetof(struct bcmgenet_priv, m), \
694 .type = _type, \
695}
696
697#define STAT_GENET_MIB_RX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_RX)
698#define STAT_GENET_MIB_TX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_TX)
699#define STAT_GENET_RUNT(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_RUNT)
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800700#define STAT_GENET_SOFT_MIB(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_SOFT)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800701
702#define STAT_GENET_MISC(str, m, offset) { \
703 .stat_string = str, \
704 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
705 .stat_offset = offsetof(struct bcmgenet_priv, m), \
706 .type = BCMGENET_STAT_MISC, \
707 .reg_offset = offset, \
708}
709
Florian Fainelli37a30b42017-03-16 10:27:08 -0700710#define STAT_GENET_Q(num) \
711 STAT_GENET_SOFT_MIB("txq" __stringify(num) "_packets", \
712 tx_rings[num].packets), \
713 STAT_GENET_SOFT_MIB("txq" __stringify(num) "_bytes", \
714 tx_rings[num].bytes), \
715 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_bytes", \
716 rx_rings[num].bytes), \
717 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_packets", \
718 rx_rings[num].packets), \
719 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_errors", \
720 rx_rings[num].errors), \
721 STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_dropped", \
722 rx_rings[num].dropped)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800723
724/* There is a 0xC gap between the end of RX and beginning of TX stats and then
725 * between the end of TX stats and the beginning of the RX RUNT
726 */
727#define BCMGENET_STAT_OFFSET 0xc
728
729/* Hardware counters must be kept in sync because the order/offset
730 * is important here (order in structure declaration = order in hardware)
731 */
732static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
733 /* general stats */
734 STAT_NETDEV(rx_packets),
735 STAT_NETDEV(tx_packets),
736 STAT_NETDEV(rx_bytes),
737 STAT_NETDEV(tx_bytes),
738 STAT_NETDEV(rx_errors),
739 STAT_NETDEV(tx_errors),
740 STAT_NETDEV(rx_dropped),
741 STAT_NETDEV(tx_dropped),
742 STAT_NETDEV(multicast),
743 /* UniMAC RSV counters */
744 STAT_GENET_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
745 STAT_GENET_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
746 STAT_GENET_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
747 STAT_GENET_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
748 STAT_GENET_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
749 STAT_GENET_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
750 STAT_GENET_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
751 STAT_GENET_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
752 STAT_GENET_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
753 STAT_GENET_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
754 STAT_GENET_MIB_RX("rx_pkts", mib.rx.pkt),
755 STAT_GENET_MIB_RX("rx_bytes", mib.rx.bytes),
756 STAT_GENET_MIB_RX("rx_multicast", mib.rx.mca),
757 STAT_GENET_MIB_RX("rx_broadcast", mib.rx.bca),
758 STAT_GENET_MIB_RX("rx_fcs", mib.rx.fcs),
759 STAT_GENET_MIB_RX("rx_control", mib.rx.cf),
760 STAT_GENET_MIB_RX("rx_pause", mib.rx.pf),
761 STAT_GENET_MIB_RX("rx_unknown", mib.rx.uo),
762 STAT_GENET_MIB_RX("rx_align", mib.rx.aln),
763 STAT_GENET_MIB_RX("rx_outrange", mib.rx.flr),
764 STAT_GENET_MIB_RX("rx_code", mib.rx.cde),
765 STAT_GENET_MIB_RX("rx_carrier", mib.rx.fcr),
766 STAT_GENET_MIB_RX("rx_oversize", mib.rx.ovr),
767 STAT_GENET_MIB_RX("rx_jabber", mib.rx.jbr),
768 STAT_GENET_MIB_RX("rx_mtu_err", mib.rx.mtue),
769 STAT_GENET_MIB_RX("rx_good_pkts", mib.rx.pok),
770 STAT_GENET_MIB_RX("rx_unicast", mib.rx.uc),
771 STAT_GENET_MIB_RX("rx_ppp", mib.rx.ppp),
772 STAT_GENET_MIB_RX("rx_crc", mib.rx.rcrc),
773 /* UniMAC TSV counters */
774 STAT_GENET_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
775 STAT_GENET_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
776 STAT_GENET_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
777 STAT_GENET_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
778 STAT_GENET_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
779 STAT_GENET_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
780 STAT_GENET_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
781 STAT_GENET_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
782 STAT_GENET_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
783 STAT_GENET_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
784 STAT_GENET_MIB_TX("tx_pkts", mib.tx.pkts),
785 STAT_GENET_MIB_TX("tx_multicast", mib.tx.mca),
786 STAT_GENET_MIB_TX("tx_broadcast", mib.tx.bca),
787 STAT_GENET_MIB_TX("tx_pause", mib.tx.pf),
788 STAT_GENET_MIB_TX("tx_control", mib.tx.cf),
789 STAT_GENET_MIB_TX("tx_fcs_err", mib.tx.fcs),
790 STAT_GENET_MIB_TX("tx_oversize", mib.tx.ovr),
791 STAT_GENET_MIB_TX("tx_defer", mib.tx.drf),
792 STAT_GENET_MIB_TX("tx_excess_defer", mib.tx.edf),
793 STAT_GENET_MIB_TX("tx_single_col", mib.tx.scl),
794 STAT_GENET_MIB_TX("tx_multi_col", mib.tx.mcl),
795 STAT_GENET_MIB_TX("tx_late_col", mib.tx.lcl),
796 STAT_GENET_MIB_TX("tx_excess_col", mib.tx.ecl),
797 STAT_GENET_MIB_TX("tx_frags", mib.tx.frg),
798 STAT_GENET_MIB_TX("tx_total_col", mib.tx.ncl),
799 STAT_GENET_MIB_TX("tx_jabber", mib.tx.jbr),
800 STAT_GENET_MIB_TX("tx_bytes", mib.tx.bytes),
801 STAT_GENET_MIB_TX("tx_good_pkts", mib.tx.pok),
802 STAT_GENET_MIB_TX("tx_unicast", mib.tx.uc),
803 /* UniMAC RUNT counters */
804 STAT_GENET_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
805 STAT_GENET_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
806 STAT_GENET_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
807 STAT_GENET_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
808 /* Misc UniMAC counters */
809 STAT_GENET_MISC("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt,
Doug Bergerffff7132017-03-09 16:58:43 -0800810 UMAC_RBUF_OVFL_CNT_V1),
811 STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt,
812 UMAC_RBUF_ERR_CNT_V1),
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800813 STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT),
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800814 STAT_GENET_SOFT_MIB("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
815 STAT_GENET_SOFT_MIB("rx_dma_failed", mib.rx_dma_failed),
816 STAT_GENET_SOFT_MIB("tx_dma_failed", mib.tx_dma_failed),
Florian Fainelli37a30b42017-03-16 10:27:08 -0700817 /* Per TX queues */
818 STAT_GENET_Q(0),
819 STAT_GENET_Q(1),
820 STAT_GENET_Q(2),
821 STAT_GENET_Q(3),
822 STAT_GENET_Q(16),
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800823};
824
825#define BCMGENET_STATS_LEN ARRAY_SIZE(bcmgenet_gstrings_stats)
826
827static void bcmgenet_get_drvinfo(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700828 struct ethtool_drvinfo *info)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800829{
830 strlcpy(info->driver, "bcmgenet", sizeof(info->driver));
831 strlcpy(info->version, "v2.0", sizeof(info->version));
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800832}
833
834static int bcmgenet_get_sset_count(struct net_device *dev, int string_set)
835{
836 switch (string_set) {
837 case ETH_SS_STATS:
838 return BCMGENET_STATS_LEN;
839 default:
840 return -EOPNOTSUPP;
841 }
842}
843
Florian Fainellic91b7f62014-07-23 10:42:12 -0700844static void bcmgenet_get_strings(struct net_device *dev, u32 stringset,
845 u8 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800846{
847 int i;
848
849 switch (stringset) {
850 case ETH_SS_STATS:
851 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
852 memcpy(data + i * ETH_GSTRING_LEN,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700853 bcmgenet_gstrings_stats[i].stat_string,
854 ETH_GSTRING_LEN);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800855 }
856 break;
857 }
858}
859
Doug Bergerffff7132017-03-09 16:58:43 -0800860static u32 bcmgenet_update_stat_misc(struct bcmgenet_priv *priv, u16 offset)
861{
862 u16 new_offset;
863 u32 val;
864
865 switch (offset) {
866 case UMAC_RBUF_OVFL_CNT_V1:
867 if (GENET_IS_V2(priv))
868 new_offset = RBUF_OVFL_CNT_V2;
869 else
870 new_offset = RBUF_OVFL_CNT_V3PLUS;
871
872 val = bcmgenet_rbuf_readl(priv, new_offset);
873 /* clear if overflowed */
874 if (val == ~0)
875 bcmgenet_rbuf_writel(priv, 0, new_offset);
876 break;
877 case UMAC_RBUF_ERR_CNT_V1:
878 if (GENET_IS_V2(priv))
879 new_offset = RBUF_ERR_CNT_V2;
880 else
881 new_offset = RBUF_ERR_CNT_V3PLUS;
882
883 val = bcmgenet_rbuf_readl(priv, new_offset);
884 /* clear if overflowed */
885 if (val == ~0)
886 bcmgenet_rbuf_writel(priv, 0, new_offset);
887 break;
888 default:
889 val = bcmgenet_umac_readl(priv, offset);
890 /* clear if overflowed */
891 if (val == ~0)
892 bcmgenet_umac_writel(priv, 0, offset);
893 break;
894 }
895
896 return val;
897}
898
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800899static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv)
900{
901 int i, j = 0;
902
903 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
904 const struct bcmgenet_stats *s;
905 u8 offset = 0;
906 u32 val = 0;
907 char *p;
908
909 s = &bcmgenet_gstrings_stats[i];
910 switch (s->type) {
911 case BCMGENET_STAT_NETDEV:
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800912 case BCMGENET_STAT_SOFT:
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800913 continue;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800914 case BCMGENET_STAT_RUNT:
Doug Berger1ad3d222017-03-09 16:58:44 -0800915 offset += BCMGENET_STAT_OFFSET;
916 /* fall through */
917 case BCMGENET_STAT_MIB_TX:
918 offset += BCMGENET_STAT_OFFSET;
919 /* fall through */
920 case BCMGENET_STAT_MIB_RX:
Florian Fainellic91b7f62014-07-23 10:42:12 -0700921 val = bcmgenet_umac_readl(priv,
922 UMAC_MIB_START + j + offset);
Doug Berger1ad3d222017-03-09 16:58:44 -0800923 offset = 0; /* Reset Offset */
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800924 break;
925 case BCMGENET_STAT_MISC:
Doug Bergerffff7132017-03-09 16:58:43 -0800926 if (GENET_IS_V1(priv)) {
927 val = bcmgenet_umac_readl(priv, s->reg_offset);
928 /* clear if overflowed */
929 if (val == ~0)
930 bcmgenet_umac_writel(priv, 0,
931 s->reg_offset);
932 } else {
933 val = bcmgenet_update_stat_misc(priv,
934 s->reg_offset);
935 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800936 break;
937 }
938
939 j += s->stat_sizeof;
940 p = (char *)priv + s->stat_offset;
941 *(u32 *)p = val;
942 }
943}
944
945static void bcmgenet_get_ethtool_stats(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700946 struct ethtool_stats *stats,
947 u64 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800948{
949 struct bcmgenet_priv *priv = netdev_priv(dev);
950 int i;
951
952 if (netif_running(dev))
953 bcmgenet_update_mib_counters(priv);
954
955 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
956 const struct bcmgenet_stats *s;
957 char *p;
958
959 s = &bcmgenet_gstrings_stats[i];
960 if (s->type == BCMGENET_STAT_NETDEV)
961 p = (char *)&dev->stats;
962 else
963 p = (char *)priv;
964 p += s->stat_offset;
Eric Dumazet6517eb52016-04-15 10:47:52 -0700965 if (sizeof(unsigned long) != sizeof(u32) &&
966 s->stat_sizeof == sizeof(unsigned long))
967 data[i] = *(unsigned long *)p;
968 else
969 data[i] = *(u32 *)p;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800970 }
971}
972
Florian Fainelli6ef398e2014-11-25 21:16:35 -0800973static void bcmgenet_eee_enable_set(struct net_device *dev, bool enable)
974{
975 struct bcmgenet_priv *priv = netdev_priv(dev);
976 u32 off = priv->hw_params->tbuf_offset + TBUF_ENERGY_CTRL;
977 u32 reg;
978
979 if (enable && !priv->clk_eee_enabled) {
980 clk_prepare_enable(priv->clk_eee);
981 priv->clk_eee_enabled = true;
982 }
983
984 reg = bcmgenet_umac_readl(priv, UMAC_EEE_CTRL);
985 if (enable)
986 reg |= EEE_EN;
987 else
988 reg &= ~EEE_EN;
989 bcmgenet_umac_writel(priv, reg, UMAC_EEE_CTRL);
990
991 /* Enable EEE and switch to a 27Mhz clock automatically */
992 reg = __raw_readl(priv->base + off);
993 if (enable)
994 reg |= TBUF_EEE_EN | TBUF_PM_EN;
995 else
996 reg &= ~(TBUF_EEE_EN | TBUF_PM_EN);
997 __raw_writel(reg, priv->base + off);
998
999 /* Do the same for thing for RBUF */
1000 reg = bcmgenet_rbuf_readl(priv, RBUF_ENERGY_CTRL);
1001 if (enable)
1002 reg |= RBUF_EEE_EN | RBUF_PM_EN;
1003 else
1004 reg &= ~(RBUF_EEE_EN | RBUF_PM_EN);
1005 bcmgenet_rbuf_writel(priv, reg, RBUF_ENERGY_CTRL);
1006
1007 if (!enable && priv->clk_eee_enabled) {
1008 clk_disable_unprepare(priv->clk_eee);
1009 priv->clk_eee_enabled = false;
1010 }
1011
1012 priv->eee.eee_enabled = enable;
1013 priv->eee.eee_active = enable;
1014}
1015
1016static int bcmgenet_get_eee(struct net_device *dev, struct ethtool_eee *e)
1017{
1018 struct bcmgenet_priv *priv = netdev_priv(dev);
1019 struct ethtool_eee *p = &priv->eee;
1020
1021 if (GENET_IS_V1(priv))
1022 return -EOPNOTSUPP;
1023
1024 e->eee_enabled = p->eee_enabled;
1025 e->eee_active = p->eee_active;
1026 e->tx_lpi_timer = bcmgenet_umac_readl(priv, UMAC_EEE_LPI_TIMER);
1027
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001028 return phy_ethtool_get_eee(priv->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001029}
1030
1031static int bcmgenet_set_eee(struct net_device *dev, struct ethtool_eee *e)
1032{
1033 struct bcmgenet_priv *priv = netdev_priv(dev);
1034 struct ethtool_eee *p = &priv->eee;
1035 int ret = 0;
1036
1037 if (GENET_IS_V1(priv))
1038 return -EOPNOTSUPP;
1039
1040 p->eee_enabled = e->eee_enabled;
1041
1042 if (!p->eee_enabled) {
1043 bcmgenet_eee_enable_set(dev, false);
1044 } else {
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001045 ret = phy_init_eee(priv->phydev, 0);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001046 if (ret) {
1047 netif_err(priv, hw, dev, "EEE initialization failed\n");
1048 return ret;
1049 }
1050
1051 bcmgenet_umac_writel(priv, e->tx_lpi_timer, UMAC_EEE_LPI_TIMER);
1052 bcmgenet_eee_enable_set(dev, true);
1053 }
1054
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001055 return phy_ethtool_set_eee(priv->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001056}
1057
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001058/* standard ethtool support functions. */
Julia Lawall70591ab2016-08-31 09:30:45 +02001059static const struct ethtool_ops bcmgenet_ethtool_ops = {
Edwin Chan89316fa2017-03-09 16:58:49 -08001060 .begin = bcmgenet_begin,
1061 .complete = bcmgenet_complete,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001062 .get_strings = bcmgenet_get_strings,
1063 .get_sset_count = bcmgenet_get_sset_count,
1064 .get_ethtool_stats = bcmgenet_get_ethtool_stats,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001065 .get_drvinfo = bcmgenet_get_drvinfo,
1066 .get_link = ethtool_op_get_link,
1067 .get_msglevel = bcmgenet_get_msglevel,
1068 .set_msglevel = bcmgenet_set_msglevel,
Florian Fainelli06ba8372014-07-21 15:29:29 -07001069 .get_wol = bcmgenet_get_wol,
1070 .set_wol = bcmgenet_set_wol,
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001071 .get_eee = bcmgenet_get_eee,
1072 .set_eee = bcmgenet_set_eee,
Florian Fainelli016e7702016-11-15 10:06:38 -08001073 .nway_reset = phy_ethtool_nway_reset,
Florian Fainelli2f913072015-09-16 16:47:39 -07001074 .get_coalesce = bcmgenet_get_coalesce,
1075 .set_coalesce = bcmgenet_set_coalesce,
Philippe Reynesfa92bf02016-09-26 22:31:57 +02001076 .get_link_ksettings = bcmgenet_get_link_ksettings,
1077 .set_link_ksettings = bcmgenet_set_link_ksettings,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001078};
1079
1080/* Power down the unimac, based on mode. */
Florian Fainellica8cf342015-03-23 15:09:51 -07001081static int bcmgenet_power_down(struct bcmgenet_priv *priv,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001082 enum bcmgenet_power_mode mode)
1083{
Florian Fainellica8cf342015-03-23 15:09:51 -07001084 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001085 u32 reg;
1086
1087 switch (mode) {
1088 case GENET_POWER_CABLE_SENSE:
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001089 phy_detach(priv->phydev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001090 break;
1091
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001092 case GENET_POWER_WOL_MAGIC:
Florian Fainellica8cf342015-03-23 15:09:51 -07001093 ret = bcmgenet_wol_power_down_cfg(priv, mode);
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001094 break;
1095
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001096 case GENET_POWER_PASSIVE:
1097 /* Power down LED */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001098 if (priv->hw_params->flags & GENET_HAS_EXT) {
1099 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
Doug Berger42138082017-03-13 17:41:42 -07001100 if (GENET_IS_V5(priv))
1101 reg |= EXT_PWR_DOWN_PHY_EN |
1102 EXT_PWR_DOWN_PHY_RD |
1103 EXT_PWR_DOWN_PHY_SD |
1104 EXT_PWR_DOWN_PHY_RX |
1105 EXT_PWR_DOWN_PHY_TX |
1106 EXT_IDDQ_GLBL_PWR;
1107 else
1108 reg |= EXT_PWR_DOWN_PHY;
1109
1110 reg |= (EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001111 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
Florian Fainellia642c4f2015-03-23 15:09:56 -07001112
1113 bcmgenet_phy_power_set(priv->dev, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001114 }
1115 break;
1116 default:
1117 break;
1118 }
Florian Fainellica8cf342015-03-23 15:09:51 -07001119
1120 return 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001121}
1122
1123static void bcmgenet_power_up(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001124 enum bcmgenet_power_mode mode)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001125{
1126 u32 reg;
1127
1128 if (!(priv->hw_params->flags & GENET_HAS_EXT))
1129 return;
1130
1131 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
1132
1133 switch (mode) {
1134 case GENET_POWER_PASSIVE:
Doug Berger42138082017-03-13 17:41:42 -07001135 reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
1136 if (GENET_IS_V5(priv)) {
1137 reg &= ~(EXT_PWR_DOWN_PHY_EN |
1138 EXT_PWR_DOWN_PHY_RD |
1139 EXT_PWR_DOWN_PHY_SD |
1140 EXT_PWR_DOWN_PHY_RX |
1141 EXT_PWR_DOWN_PHY_TX |
1142 EXT_IDDQ_GLBL_PWR);
1143 reg |= EXT_PHY_RESET;
1144 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1145 mdelay(1);
1146
1147 reg &= ~EXT_PHY_RESET;
1148 } else {
1149 reg &= ~EXT_PWR_DOWN_PHY;
1150 reg |= EXT_PWR_DN_EN_LD;
1151 }
1152 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1153 bcmgenet_phy_power_set(priv->dev, true);
1154 bcmgenet_mii_reset(priv->dev);
1155 break;
1156
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001157 case GENET_POWER_CABLE_SENSE:
1158 /* enable APD */
Doug Berger42138082017-03-13 17:41:42 -07001159 if (!GENET_IS_V5(priv)) {
1160 reg |= EXT_PWR_DN_EN_LD;
1161 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1162 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001163 break;
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001164 case GENET_POWER_WOL_MAGIC:
1165 bcmgenet_wol_power_up_cfg(priv, mode);
1166 return;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001167 default:
1168 break;
1169 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001170}
1171
1172/* ioctl handle special commands that are not present in ethtool. */
1173static int bcmgenet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1174{
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001175 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001176
1177 if (!netif_running(dev))
1178 return -EINVAL;
1179
Doug Berger54fecff2017-03-13 17:41:39 -07001180 if (!priv->phydev)
1181 return -ENODEV;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001182
Doug Berger54fecff2017-03-13 17:41:39 -07001183 return phy_mii_ioctl(priv->phydev, rq, cmd);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001184}
1185
1186static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv,
1187 struct bcmgenet_tx_ring *ring)
1188{
1189 struct enet_cb *tx_cb_ptr;
1190
1191 tx_cb_ptr = ring->cbs;
1192 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
Petri Gynther014012a2015-02-23 11:00:45 -08001193
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001194 /* Advancing local write pointer */
1195 if (ring->write_ptr == ring->end_ptr)
1196 ring->write_ptr = ring->cb_ptr;
1197 else
1198 ring->write_ptr++;
1199
1200 return tx_cb_ptr;
1201}
1202
1203/* Simple helper to free a control block's resources */
1204static void bcmgenet_free_cb(struct enet_cb *cb)
1205{
1206 dev_kfree_skb_any(cb->skb);
1207 cb->skb = NULL;
1208 dma_unmap_addr_set(cb, dma_addr, 0);
1209}
1210
Petri Gynther4055eae2015-03-25 12:35:16 -07001211static inline void bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring *ring)
1212{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001213 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001214 INTRL2_CPU_MASK_SET);
1215}
1216
1217static inline void bcmgenet_rx_ring16_int_enable(struct bcmgenet_rx_ring *ring)
1218{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001219 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001220 INTRL2_CPU_MASK_CLEAR);
1221}
1222
1223static inline void bcmgenet_rx_ring_int_disable(struct bcmgenet_rx_ring *ring)
1224{
1225 bcmgenet_intrl2_1_writel(ring->priv,
1226 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1227 INTRL2_CPU_MASK_SET);
1228}
1229
1230static inline void bcmgenet_rx_ring_int_enable(struct bcmgenet_rx_ring *ring)
1231{
1232 bcmgenet_intrl2_1_writel(ring->priv,
1233 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1234 INTRL2_CPU_MASK_CLEAR);
1235}
1236
Petri Gynther9dbac282015-03-25 12:35:10 -07001237static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001238{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001239 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001240 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001241}
1242
Petri Gynther9dbac282015-03-25 12:35:10 -07001243static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001244{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001245 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001246 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001247}
1248
Petri Gynther9dbac282015-03-25 12:35:10 -07001249static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001250{
Petri Gynther9dbac282015-03-25 12:35:10 -07001251 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001252 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001253}
1254
Petri Gynther9dbac282015-03-25 12:35:10 -07001255static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001256{
Petri Gynther9dbac282015-03-25 12:35:10 -07001257 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001258 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001259}
1260
1261/* Unlocked version of the reclaim routine */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001262static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
1263 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001264{
1265 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001266 struct device *kdev = &priv->pdev->dev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001267 struct enet_cb *tx_cb_ptr;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001268 unsigned int pkts_compl = 0;
Petri Gynther55868122016-03-24 11:27:20 -07001269 unsigned int bytes_compl = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001270 unsigned int c_index;
Petri Gynther66d06752015-03-04 14:30:01 -08001271 unsigned int txbds_ready;
1272 unsigned int txbds_processed = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001273
Doug Bergerd5810ca2017-03-13 17:41:37 -07001274 /* Clear status before servicing to reduce spurious interrupts */
1275 if (ring->index == DESC_INDEX)
1276 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_TXDMA_DONE,
1277 INTRL2_CPU_CLEAR);
1278 else
1279 bcmgenet_intrl2_1_writel(priv, (1 << ring->index),
1280 INTRL2_CPU_CLEAR);
1281
Brian Norris7fc527f2014-07-29 14:34:14 -07001282 /* Compute how many buffers are transmitted since last xmit call */
Doug Bergerc298ede2017-03-13 17:41:33 -07001283 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX)
1284 & DMA_C_INDEX_MASK;
1285 txbds_ready = (c_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001286
1287 netif_dbg(priv, tx_done, dev,
Petri Gynther66d06752015-03-04 14:30:01 -08001288 "%s ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
1289 __func__, ring->index, ring->c_index, c_index, txbds_ready);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001290
1291 /* Reclaim transmitted buffers */
Petri Gynther66d06752015-03-04 14:30:01 -08001292 while (txbds_processed < txbds_ready) {
1293 tx_cb_ptr = &priv->tx_cbs[ring->clean_ptr];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001294 if (tx_cb_ptr->skb) {
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001295 pkts_compl++;
Petri Gynther55868122016-03-24 11:27:20 -07001296 bytes_compl += GENET_CB(tx_cb_ptr->skb)->bytes_sent;
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001297 dma_unmap_single(kdev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001298 dma_unmap_addr(tx_cb_ptr, dma_addr),
Eric Dumazeteee57722016-03-17 11:57:06 -07001299 dma_unmap_len(tx_cb_ptr, dma_len),
Florian Fainellic91b7f62014-07-23 10:42:12 -07001300 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001301 bcmgenet_free_cb(tx_cb_ptr);
1302 } else if (dma_unmap_addr(tx_cb_ptr, dma_addr)) {
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001303 dma_unmap_page(kdev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001304 dma_unmap_addr(tx_cb_ptr, dma_addr),
1305 dma_unmap_len(tx_cb_ptr, dma_len),
1306 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001307 dma_unmap_addr_set(tx_cb_ptr, dma_addr, 0);
1308 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001309
Petri Gynther66d06752015-03-04 14:30:01 -08001310 txbds_processed++;
1311 if (likely(ring->clean_ptr < ring->end_ptr))
1312 ring->clean_ptr++;
1313 else
1314 ring->clean_ptr = ring->cb_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001315 }
1316
Petri Gynther66d06752015-03-04 14:30:01 -08001317 ring->free_bds += txbds_processed;
Doug Bergerc4d453d2017-03-13 17:41:38 -07001318 ring->c_index = c_index;
Petri Gynther66d06752015-03-04 14:30:01 -08001319
Florian Fainelli37a30b42017-03-16 10:27:08 -07001320 ring->packets += pkts_compl;
1321 ring->bytes += bytes_compl;
Petri Gynther55868122016-03-24 11:27:20 -07001322
Doug Berger6d22fe12017-03-09 16:58:50 -08001323 netdev_tx_completed_queue(netdev_get_tx_queue(dev, ring->queue),
1324 pkts_compl, bytes_compl);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001325
Doug Bergerc4d453d2017-03-13 17:41:38 -07001326 return txbds_processed;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001327}
1328
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001329static unsigned int bcmgenet_tx_reclaim(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001330 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001331{
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001332 unsigned int released;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001333 unsigned long flags;
1334
1335 spin_lock_irqsave(&ring->lock, flags);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001336 released = __bcmgenet_tx_reclaim(dev, ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001337 spin_unlock_irqrestore(&ring->lock, flags);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001338
1339 return released;
1340}
1341
1342static int bcmgenet_tx_poll(struct napi_struct *napi, int budget)
1343{
1344 struct bcmgenet_tx_ring *ring =
1345 container_of(napi, struct bcmgenet_tx_ring, napi);
1346 unsigned int work_done = 0;
Doug Berger6d22fe12017-03-09 16:58:50 -08001347 struct netdev_queue *txq;
1348 unsigned long flags;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001349
Doug Berger6d22fe12017-03-09 16:58:50 -08001350 spin_lock_irqsave(&ring->lock, flags);
1351 work_done = __bcmgenet_tx_reclaim(ring->priv->dev, ring);
1352 if (ring->free_bds > (MAX_SKB_FRAGS + 1)) {
1353 txq = netdev_get_tx_queue(ring->priv->dev, ring->queue);
1354 netif_tx_wake_queue(txq);
1355 }
1356 spin_unlock_irqrestore(&ring->lock, flags);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001357
1358 if (work_done == 0) {
1359 napi_complete(napi);
Petri Gynther9dbac282015-03-25 12:35:10 -07001360 ring->int_enable(ring);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001361
1362 return 0;
1363 }
1364
1365 return budget;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001366}
1367
1368static void bcmgenet_tx_reclaim_all(struct net_device *dev)
1369{
1370 struct bcmgenet_priv *priv = netdev_priv(dev);
1371 int i;
1372
1373 if (netif_is_multiqueue(dev)) {
1374 for (i = 0; i < priv->hw_params->tx_queues; i++)
1375 bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
1376 }
1377
1378 bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
1379}
1380
1381/* Transmits a single SKB (either head of a fragment or a single SKB)
1382 * caller must hold priv->lock
1383 */
1384static int bcmgenet_xmit_single(struct net_device *dev,
1385 struct sk_buff *skb,
1386 u16 dma_desc_flags,
1387 struct bcmgenet_tx_ring *ring)
1388{
1389 struct bcmgenet_priv *priv = netdev_priv(dev);
1390 struct device *kdev = &priv->pdev->dev;
1391 struct enet_cb *tx_cb_ptr;
1392 unsigned int skb_len;
1393 dma_addr_t mapping;
1394 u32 length_status;
1395 int ret;
1396
1397 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
1398
1399 if (unlikely(!tx_cb_ptr))
1400 BUG();
1401
1402 tx_cb_ptr->skb = skb;
1403
Petri Gynther7dd39912016-03-24 11:27:21 -07001404 skb_len = skb_headlen(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001405
1406 mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
1407 ret = dma_mapping_error(kdev, mapping);
1408 if (ret) {
Florian Fainelli44c8bc32014-11-19 10:29:56 -08001409 priv->mib.tx_dma_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001410 netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
1411 dev_kfree_skb(skb);
1412 return ret;
1413 }
1414
1415 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
Eric Dumazeteee57722016-03-17 11:57:06 -07001416 dma_unmap_len_set(tx_cb_ptr, dma_len, skb_len);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001417 length_status = (skb_len << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
1418 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT) |
1419 DMA_TX_APPEND_CRC;
1420
1421 if (skb->ip_summed == CHECKSUM_PARTIAL)
1422 length_status |= DMA_TX_DO_CSUM;
1423
1424 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, length_status);
1425
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001426 return 0;
1427}
1428
Brian Norris7fc527f2014-07-29 14:34:14 -07001429/* Transmit a SKB fragment */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001430static int bcmgenet_xmit_frag(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001431 skb_frag_t *frag,
1432 u16 dma_desc_flags,
1433 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001434{
1435 struct bcmgenet_priv *priv = netdev_priv(dev);
1436 struct device *kdev = &priv->pdev->dev;
1437 struct enet_cb *tx_cb_ptr;
Petri Gynther824ba602016-04-05 14:00:00 -07001438 unsigned int frag_size;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001439 dma_addr_t mapping;
1440 int ret;
1441
1442 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
1443
1444 if (unlikely(!tx_cb_ptr))
1445 BUG();
Petri Gynther824ba602016-04-05 14:00:00 -07001446
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001447 tx_cb_ptr->skb = NULL;
1448
Petri Gynther824ba602016-04-05 14:00:00 -07001449 frag_size = skb_frag_size(frag);
1450
1451 mapping = skb_frag_dma_map(kdev, frag, 0, frag_size, DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001452 ret = dma_mapping_error(kdev, mapping);
1453 if (ret) {
Florian Fainelli44c8bc32014-11-19 10:29:56 -08001454 priv->mib.tx_dma_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001455 netif_err(priv, tx_err, dev, "%s: Tx DMA map failed\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001456 __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001457 return ret;
1458 }
1459
1460 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
Petri Gynther824ba602016-04-05 14:00:00 -07001461 dma_unmap_len_set(tx_cb_ptr, dma_len, frag_size);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001462
1463 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping,
Petri Gynther824ba602016-04-05 14:00:00 -07001464 (frag_size << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
Florian Fainellic91b7f62014-07-23 10:42:12 -07001465 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001466
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001467 return 0;
1468}
1469
1470/* Reallocate the SKB to put enough headroom in front of it and insert
1471 * the transmit checksum offsets in the descriptors
1472 */
Petri Gyntherbc233332014-10-01 11:30:01 -07001473static struct sk_buff *bcmgenet_put_tx_csum(struct net_device *dev,
1474 struct sk_buff *skb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001475{
1476 struct status_64 *status = NULL;
1477 struct sk_buff *new_skb;
1478 u16 offset;
1479 u8 ip_proto;
1480 u16 ip_ver;
1481 u32 tx_csum_info;
1482
1483 if (unlikely(skb_headroom(skb) < sizeof(*status))) {
1484 /* If 64 byte status block enabled, must make sure skb has
1485 * enough headroom for us to insert 64B status block.
1486 */
1487 new_skb = skb_realloc_headroom(skb, sizeof(*status));
1488 dev_kfree_skb(skb);
1489 if (!new_skb) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001490 dev->stats.tx_dropped++;
Petri Gyntherbc233332014-10-01 11:30:01 -07001491 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001492 }
1493 skb = new_skb;
1494 }
1495
1496 skb_push(skb, sizeof(*status));
1497 status = (struct status_64 *)skb->data;
1498
1499 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1500 ip_ver = htons(skb->protocol);
1501 switch (ip_ver) {
1502 case ETH_P_IP:
1503 ip_proto = ip_hdr(skb)->protocol;
1504 break;
1505 case ETH_P_IPV6:
1506 ip_proto = ipv6_hdr(skb)->nexthdr;
1507 break;
1508 default:
Petri Gyntherbc233332014-10-01 11:30:01 -07001509 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001510 }
1511
1512 offset = skb_checksum_start_offset(skb) - sizeof(*status);
1513 tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
1514 (offset + skb->csum_offset);
1515
1516 /* Set the length valid bit for TCP and UDP and just set
1517 * the special UDP flag for IPv4, else just set to 0.
1518 */
1519 if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
1520 tx_csum_info |= STATUS_TX_CSUM_LV;
1521 if (ip_proto == IPPROTO_UDP && ip_ver == ETH_P_IP)
1522 tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
Florian Fainelli8900ea572014-07-23 10:42:14 -07001523 } else {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001524 tx_csum_info = 0;
Florian Fainelli8900ea572014-07-23 10:42:14 -07001525 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001526
1527 status->tx_csum_info = tx_csum_info;
1528 }
1529
Petri Gyntherbc233332014-10-01 11:30:01 -07001530 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001531}
1532
1533static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
1534{
1535 struct bcmgenet_priv *priv = netdev_priv(dev);
1536 struct bcmgenet_tx_ring *ring = NULL;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001537 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001538 unsigned long flags = 0;
1539 int nr_frags, index;
1540 u16 dma_desc_flags;
1541 int ret;
1542 int i;
1543
1544 index = skb_get_queue_mapping(skb);
1545 /* Mapping strategy:
1546 * queue_mapping = 0, unclassified, packet xmited through ring16
1547 * queue_mapping = 1, goes to ring 0. (highest priority queue
1548 * queue_mapping = 2, goes to ring 1.
1549 * queue_mapping = 3, goes to ring 2.
1550 * queue_mapping = 4, goes to ring 3.
1551 */
1552 if (index == 0)
1553 index = DESC_INDEX;
1554 else
1555 index -= 1;
1556
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001557 ring = &priv->tx_rings[index];
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001558 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001559
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07001560 nr_frags = skb_shinfo(skb)->nr_frags;
1561
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001562 spin_lock_irqsave(&ring->lock, flags);
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07001563 if (ring->free_bds <= (nr_frags + 1)) {
1564 if (!netif_tx_queue_stopped(txq)) {
1565 netif_tx_stop_queue(txq);
1566 netdev_err(dev,
1567 "%s: tx ring %d full when queue %d awake\n",
1568 __func__, index, ring->queue);
1569 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001570 ret = NETDEV_TX_BUSY;
1571 goto out;
1572 }
1573
Florian Fainelli474ea9c2014-07-22 11:01:52 -07001574 if (skb_padto(skb, ETH_ZLEN)) {
1575 ret = NETDEV_TX_OK;
1576 goto out;
1577 }
1578
Petri Gynther55868122016-03-24 11:27:20 -07001579 /* Retain how many bytes will be sent on the wire, without TSB inserted
1580 * by transmit checksum offload
1581 */
1582 GENET_CB(skb)->bytes_sent = skb->len;
1583
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001584 /* set the SKB transmit checksum */
1585 if (priv->desc_64b_en) {
Petri Gyntherbc233332014-10-01 11:30:01 -07001586 skb = bcmgenet_put_tx_csum(dev, skb);
1587 if (!skb) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001588 ret = NETDEV_TX_OK;
1589 goto out;
1590 }
1591 }
1592
1593 dma_desc_flags = DMA_SOP;
1594 if (nr_frags == 0)
1595 dma_desc_flags |= DMA_EOP;
1596
1597 /* Transmit single SKB or head of fragment list */
1598 ret = bcmgenet_xmit_single(dev, skb, dma_desc_flags, ring);
1599 if (ret) {
1600 ret = NETDEV_TX_OK;
1601 goto out;
1602 }
1603
1604 /* xmit fragment */
1605 for (i = 0; i < nr_frags; i++) {
1606 ret = bcmgenet_xmit_frag(dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001607 &skb_shinfo(skb)->frags[i],
1608 (i == nr_frags - 1) ? DMA_EOP : 0,
1609 ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001610 if (ret) {
1611 ret = NETDEV_TX_OK;
1612 goto out;
1613 }
1614 }
1615
Florian Fainellid03825f2014-03-20 10:53:21 -07001616 skb_tx_timestamp(skb);
1617
Florian Fainelliae67bf02015-03-13 12:11:06 -07001618 /* Decrement total BD count and advance our write pointer */
1619 ring->free_bds -= nr_frags + 1;
1620 ring->prod_index += nr_frags + 1;
1621 ring->prod_index &= DMA_P_INDEX_MASK;
1622
Petri Gynthere178c8c2016-04-09 00:20:36 -07001623 netdev_tx_sent_queue(txq, GENET_CB(skb)->bytes_sent);
1624
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001625 if (ring->free_bds <= (MAX_SKB_FRAGS + 1))
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001626 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001627
Florian Fainelliddd0ca52015-03-13 12:11:07 -07001628 if (!skb->xmit_more || netif_xmit_stopped(txq))
1629 /* Packets are ready, update producer index */
1630 bcmgenet_tdma_ring_writel(priv, ring->index,
1631 ring->prod_index, TDMA_PROD_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001632out:
1633 spin_unlock_irqrestore(&ring->lock, flags);
1634
1635 return ret;
1636}
1637
Petri Gyntherd6707be2015-03-12 15:48:00 -07001638static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
1639 struct enet_cb *cb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001640{
1641 struct device *kdev = &priv->pdev->dev;
1642 struct sk_buff *skb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001643 struct sk_buff *rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001644 dma_addr_t mapping;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001645
Petri Gyntherd6707be2015-03-12 15:48:00 -07001646 /* Allocate a new Rx skb */
Florian Fainellic91b7f62014-07-23 10:42:12 -07001647 skb = netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT);
Petri Gyntherd6707be2015-03-12 15:48:00 -07001648 if (!skb) {
1649 priv->mib.alloc_rx_buff_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001650 netif_err(priv, rx_err, priv->dev,
Petri Gyntherd6707be2015-03-12 15:48:00 -07001651 "%s: Rx skb allocation failed\n", __func__);
1652 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001653 }
1654
Petri Gyntherd6707be2015-03-12 15:48:00 -07001655 /* DMA-map the new Rx skb */
1656 mapping = dma_map_single(kdev, skb->data, priv->rx_buf_len,
1657 DMA_FROM_DEVICE);
1658 if (dma_mapping_error(kdev, mapping)) {
1659 priv->mib.rx_dma_failed++;
1660 dev_kfree_skb_any(skb);
1661 netif_err(priv, rx_err, priv->dev,
1662 "%s: Rx skb DMA mapping failed\n", __func__);
1663 return NULL;
1664 }
1665
1666 /* Grab the current Rx skb from the ring and DMA-unmap it */
1667 rx_skb = cb->skb;
1668 if (likely(rx_skb))
1669 dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
1670 priv->rx_buf_len, DMA_FROM_DEVICE);
1671
1672 /* Put the new Rx skb on the ring */
1673 cb->skb = skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001674 dma_unmap_addr_set(cb, dma_addr, mapping);
Petri Gynther8ac467e2015-03-09 13:40:00 -07001675 dmadesc_set_addr(priv, cb->bd_addr, mapping);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001676
Petri Gyntherd6707be2015-03-12 15:48:00 -07001677 /* Return the current Rx skb to caller */
1678 return rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001679}
1680
1681/* bcmgenet_desc_rx - descriptor based rx process.
1682 * this could be called from bottom half, or from NAPI polling method.
1683 */
Petri Gynther4055eae2015-03-25 12:35:16 -07001684static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001685 unsigned int budget)
1686{
Petri Gynther4055eae2015-03-25 12:35:16 -07001687 struct bcmgenet_priv *priv = ring->priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001688 struct net_device *dev = priv->dev;
1689 struct enet_cb *cb;
1690 struct sk_buff *skb;
1691 u32 dma_length_status;
1692 unsigned long dma_flag;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001693 int len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001694 unsigned int rxpktprocessed = 0, rxpkttoprocess;
Doug Bergerd5810ca2017-03-13 17:41:37 -07001695 unsigned int p_index, mask;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001696 unsigned int discards;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001697 unsigned int chksum_ok = 0;
1698
Doug Bergerd5810ca2017-03-13 17:41:37 -07001699 /* Clear status before servicing to reduce spurious interrupts */
1700 if (ring->index == DESC_INDEX) {
1701 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_RXDMA_DONE,
1702 INTRL2_CPU_CLEAR);
1703 } else {
1704 mask = 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index);
1705 bcmgenet_intrl2_1_writel(priv,
1706 mask,
1707 INTRL2_CPU_CLEAR);
1708 }
1709
Petri Gynther4055eae2015-03-25 12:35:16 -07001710 p_index = bcmgenet_rdma_ring_readl(priv, ring->index, RDMA_PROD_INDEX);
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001711
1712 discards = (p_index >> DMA_P_INDEX_DISCARD_CNT_SHIFT) &
1713 DMA_P_INDEX_DISCARD_CNT_MASK;
1714 if (discards > ring->old_discards) {
1715 discards = discards - ring->old_discards;
Florian Fainelli37a30b42017-03-16 10:27:08 -07001716 ring->errors += discards;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001717 ring->old_discards += discards;
1718
1719 /* Clear HW register when we reach 75% of maximum 0xFFFF */
1720 if (ring->old_discards >= 0xC000) {
1721 ring->old_discards = 0;
Petri Gynther4055eae2015-03-25 12:35:16 -07001722 bcmgenet_rdma_ring_writel(priv, ring->index, 0,
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001723 RDMA_PROD_INDEX);
1724 }
1725 }
1726
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001727 p_index &= DMA_P_INDEX_MASK;
Doug Bergerc298ede2017-03-13 17:41:33 -07001728 rxpkttoprocess = (p_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001729
1730 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001731 "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001732
1733 while ((rxpktprocessed < rxpkttoprocess) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001734 (rxpktprocessed < budget)) {
Petri Gynther8ac467e2015-03-09 13:40:00 -07001735 cb = &priv->rx_cbs[ring->read_ptr];
Petri Gyntherd6707be2015-03-12 15:48:00 -07001736 skb = bcmgenet_rx_refill(priv, cb);
Florian Fainellib629be52014-09-08 11:37:52 -07001737
Florian Fainellib629be52014-09-08 11:37:52 -07001738 if (unlikely(!skb)) {
Florian Fainelli37a30b42017-03-16 10:27:08 -07001739 ring->dropped++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001740 goto next;
Florian Fainellib629be52014-09-08 11:37:52 -07001741 }
1742
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001743 if (!priv->desc_64b_en) {
Florian Fainellic91b7f62014-07-23 10:42:12 -07001744 dma_length_status =
Petri Gynther8ac467e2015-03-09 13:40:00 -07001745 dmadesc_get_length_status(priv, cb->bd_addr);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001746 } else {
1747 struct status_64 *status;
Florian Fainelli164d4f22014-07-23 10:42:13 -07001748
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001749 status = (struct status_64 *)skb->data;
1750 dma_length_status = status->length_status;
1751 }
1752
1753 /* DMA flags and length are still valid no matter how
1754 * we got the Receive Status Vector (64B RSB or register)
1755 */
1756 dma_flag = dma_length_status & 0xffff;
1757 len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
1758
1759 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001760 "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
Petri Gynther8ac467e2015-03-09 13:40:00 -07001761 __func__, p_index, ring->c_index,
1762 ring->read_ptr, dma_length_status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001763
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001764 if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
1765 netif_err(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001766 "dropping fragmented packet!\n");
Florian Fainelli37a30b42017-03-16 10:27:08 -07001767 ring->errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001768 dev_kfree_skb_any(skb);
1769 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001770 }
Petri Gyntherd6707be2015-03-12 15:48:00 -07001771
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001772 /* report errors */
1773 if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
1774 DMA_RX_OV |
1775 DMA_RX_NO |
1776 DMA_RX_LG |
1777 DMA_RX_RXER))) {
1778 netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001779 (unsigned int)dma_flag);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001780 if (dma_flag & DMA_RX_CRC_ERROR)
1781 dev->stats.rx_crc_errors++;
1782 if (dma_flag & DMA_RX_OV)
1783 dev->stats.rx_over_errors++;
1784 if (dma_flag & DMA_RX_NO)
1785 dev->stats.rx_frame_errors++;
1786 if (dma_flag & DMA_RX_LG)
1787 dev->stats.rx_length_errors++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001788 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001789 dev_kfree_skb_any(skb);
1790 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001791 } /* error packet */
1792
1793 chksum_ok = (dma_flag & priv->dma_rx_chk_bit) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001794 priv->desc_rxchk_en;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001795
1796 skb_put(skb, len);
1797 if (priv->desc_64b_en) {
1798 skb_pull(skb, 64);
1799 len -= 64;
1800 }
1801
1802 if (likely(chksum_ok))
1803 skb->ip_summed = CHECKSUM_UNNECESSARY;
1804
1805 /* remove hardware 2bytes added for IP alignment */
1806 skb_pull(skb, 2);
1807 len -= 2;
1808
1809 if (priv->crc_fwd_en) {
1810 skb_trim(skb, len - ETH_FCS_LEN);
1811 len -= ETH_FCS_LEN;
1812 }
1813
1814 /*Finish setting up the received SKB and send it to the kernel*/
1815 skb->protocol = eth_type_trans(skb, priv->dev);
Florian Fainelli37a30b42017-03-16 10:27:08 -07001816 ring->packets++;
1817 ring->bytes += len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001818 if (dma_flag & DMA_RX_MULT)
1819 dev->stats.multicast++;
1820
1821 /* Notify kernel */
Petri Gynther4055eae2015-03-25 12:35:16 -07001822 napi_gro_receive(&ring->napi, skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001823 netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
1824
Petri Gyntherd6707be2015-03-12 15:48:00 -07001825next:
Florian Fainellicf377d82014-10-10 10:51:52 -07001826 rxpktprocessed++;
Petri Gynther8ac467e2015-03-09 13:40:00 -07001827 if (likely(ring->read_ptr < ring->end_ptr))
1828 ring->read_ptr++;
1829 else
1830 ring->read_ptr = ring->cb_ptr;
1831
1832 ring->c_index = (ring->c_index + 1) & DMA_C_INDEX_MASK;
Petri Gynther4055eae2015-03-25 12:35:16 -07001833 bcmgenet_rdma_ring_writel(priv, ring->index, ring->c_index, RDMA_CONS_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001834 }
1835
1836 return rxpktprocessed;
1837}
1838
Petri Gynther3ab11332015-03-25 12:35:15 -07001839/* Rx NAPI polling method */
1840static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
1841{
Petri Gynther4055eae2015-03-25 12:35:16 -07001842 struct bcmgenet_rx_ring *ring = container_of(napi,
1843 struct bcmgenet_rx_ring, napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07001844 unsigned int work_done;
1845
Petri Gynther4055eae2015-03-25 12:35:16 -07001846 work_done = bcmgenet_desc_rx(ring, budget);
Petri Gynther3ab11332015-03-25 12:35:15 -07001847
1848 if (work_done < budget) {
Eric Dumazeteb96ce02016-04-08 22:06:40 -07001849 napi_complete_done(napi, work_done);
Petri Gynther4055eae2015-03-25 12:35:16 -07001850 ring->int_enable(ring);
Petri Gynther3ab11332015-03-25 12:35:15 -07001851 }
1852
1853 return work_done;
1854}
1855
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001856/* Assign skb to RX DMA descriptor. */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001857static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv,
1858 struct bcmgenet_rx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001859{
1860 struct enet_cb *cb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001861 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001862 int i;
1863
Petri Gynther8ac467e2015-03-09 13:40:00 -07001864 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001865
1866 /* loop here for each buffer needing assign */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001867 for (i = 0; i < ring->size; i++) {
1868 cb = ring->cbs + i;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001869 skb = bcmgenet_rx_refill(priv, cb);
1870 if (skb)
1871 dev_kfree_skb_any(skb);
1872 if (!cb->skb)
1873 return -ENOMEM;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001874 }
1875
Petri Gyntherd6707be2015-03-12 15:48:00 -07001876 return 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001877}
1878
1879static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
1880{
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001881 struct device *kdev = &priv->pdev->dev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001882 struct enet_cb *cb;
1883 int i;
1884
1885 for (i = 0; i < priv->num_rx_bds; i++) {
1886 cb = &priv->rx_cbs[i];
1887
1888 if (dma_unmap_addr(cb, dma_addr)) {
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001889 dma_unmap_single(kdev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001890 dma_unmap_addr(cb, dma_addr),
1891 priv->rx_buf_len, DMA_FROM_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001892 dma_unmap_addr_set(cb, dma_addr, 0);
1893 }
1894
1895 if (cb->skb)
1896 bcmgenet_free_cb(cb);
1897 }
1898}
1899
Florian Fainellic91b7f62014-07-23 10:42:12 -07001900static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable)
Florian Fainellie29585b2014-07-21 15:29:20 -07001901{
1902 u32 reg;
1903
1904 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1905 if (enable)
1906 reg |= mask;
1907 else
1908 reg &= ~mask;
1909 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
1910
1911 /* UniMAC stops on a packet boundary, wait for a full-size packet
1912 * to be processed
1913 */
1914 if (enable == 0)
1915 usleep_range(1000, 2000);
1916}
1917
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001918static int reset_umac(struct bcmgenet_priv *priv)
1919{
1920 struct device *kdev = &priv->pdev->dev;
1921 unsigned int timeout = 0;
1922 u32 reg;
1923
1924 /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
1925 bcmgenet_rbuf_ctrl_set(priv, 0);
1926 udelay(10);
1927
1928 /* disable MAC while updating its registers */
1929 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1930
1931 /* issue soft reset, wait for it to complete */
1932 bcmgenet_umac_writel(priv, CMD_SW_RESET, UMAC_CMD);
1933 while (timeout++ < 1000) {
1934 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1935 if (!(reg & CMD_SW_RESET))
1936 return 0;
1937
1938 udelay(1);
1939 }
1940
1941 if (timeout == 1000) {
1942 dev_err(kdev,
Brian Norris7fc527f2014-07-29 14:34:14 -07001943 "timeout waiting for MAC to come out of reset\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001944 return -ETIMEDOUT;
1945 }
1946
1947 return 0;
1948}
1949
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001950static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
1951{
1952 /* Mask all interrupts.*/
1953 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1954 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001955 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1956 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001957}
1958
Florian Fainelli37850e32015-10-17 14:22:46 -07001959static void bcmgenet_link_intr_enable(struct bcmgenet_priv *priv)
1960{
1961 u32 int0_enable = 0;
1962
1963 /* Monitor cable plug/unplugged event for internal PHY, external PHY
1964 * and MoCA PHY
1965 */
1966 if (priv->internal_phy) {
1967 int0_enable |= UMAC_IRQ_LINK_EVENT;
1968 } else if (priv->ext_phy) {
1969 int0_enable |= UMAC_IRQ_LINK_EVENT;
1970 } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
1971 if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
1972 int0_enable |= UMAC_IRQ_LINK_EVENT;
1973 }
1974 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
1975}
1976
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001977static int init_umac(struct bcmgenet_priv *priv)
1978{
1979 struct device *kdev = &priv->pdev->dev;
1980 int ret;
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001981 u32 reg;
1982 u32 int0_enable = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001983
1984 dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
1985
1986 ret = reset_umac(priv);
1987 if (ret)
1988 return ret;
1989
1990 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1991 /* clear tx/rx counter */
1992 bcmgenet_umac_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001993 MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
1994 UMAC_MIB_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001995 bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
1996
1997 bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
1998
1999 /* init rx registers, enable ip header optimization */
2000 reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
2001 reg |= RBUF_ALIGN_2B;
2002 bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
2003
2004 if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
2005 bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
2006
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002007 bcmgenet_intr_disable(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002008
Florian Fainelli37850e32015-10-17 14:22:46 -07002009 /* Configure backpressure vectors for MoCA */
2010 if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002011 reg = bcmgenet_bp_mc_get(priv);
2012 reg |= BIT(priv->hw_params->bp_in_en_shift);
2013
2014 /* bp_mask: back pressure mask */
2015 if (netif_is_multiqueue(priv->dev))
2016 reg |= priv->hw_params->bp_in_mask;
2017 else
2018 reg &= ~priv->hw_params->bp_in_mask;
2019 bcmgenet_bp_mc_set(priv, reg);
2020 }
2021
2022 /* Enable MDIO interrupts on GENET v3+ */
2023 if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002024 int0_enable |= (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002025
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002026 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002027
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002028 dev_dbg(kdev, "done init umac\n");
2029
2030 return 0;
2031}
2032
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002033/* Initialize a Tx ring along with corresponding hardware registers */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002034static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
2035 unsigned int index, unsigned int size,
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002036 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002037{
2038 struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
2039 u32 words_per_bd = WORDS_PER_BD(priv);
2040 u32 flow_period_val = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002041
2042 spin_lock_init(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002043 ring->priv = priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002044 ring->index = index;
2045 if (index == DESC_INDEX) {
2046 ring->queue = 0;
2047 ring->int_enable = bcmgenet_tx_ring16_int_enable;
2048 ring->int_disable = bcmgenet_tx_ring16_int_disable;
2049 } else {
2050 ring->queue = index + 1;
2051 ring->int_enable = bcmgenet_tx_ring_int_enable;
2052 ring->int_disable = bcmgenet_tx_ring_int_disable;
2053 }
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002054 ring->cbs = priv->tx_cbs + start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002055 ring->size = size;
Petri Gynther66d06752015-03-04 14:30:01 -08002056 ring->clean_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002057 ring->c_index = 0;
2058 ring->free_bds = size;
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002059 ring->write_ptr = start_ptr;
2060 ring->cb_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002061 ring->end_ptr = end_ptr - 1;
2062 ring->prod_index = 0;
2063
2064 /* Set flow period for ring != 16 */
2065 if (index != DESC_INDEX)
2066 flow_period_val = ENET_MAX_MTU_SIZE << 16;
2067
2068 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
2069 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
2070 bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
2071 /* Disable rate control for now */
2072 bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002073 TDMA_FLOW_PERIOD);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002074 bcmgenet_tdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002075 ((size << DMA_RING_SIZE_SHIFT) |
2076 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002077
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002078 /* Set start and end address, read and write pointers */
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002079 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002080 DMA_START_ADDR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002081 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002082 TDMA_READ_PTR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002083 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002084 TDMA_WRITE_PTR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002085 bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002086 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002087}
2088
2089/* Initialize a RDMA ring */
2090static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
Petri Gynther8ac467e2015-03-09 13:40:00 -07002091 unsigned int index, unsigned int size,
2092 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002093{
Petri Gynther8ac467e2015-03-09 13:40:00 -07002094 struct bcmgenet_rx_ring *ring = &priv->rx_rings[index];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002095 u32 words_per_bd = WORDS_PER_BD(priv);
2096 int ret;
2097
Petri Gynther4055eae2015-03-25 12:35:16 -07002098 ring->priv = priv;
Petri Gynther8ac467e2015-03-09 13:40:00 -07002099 ring->index = index;
Petri Gynther4055eae2015-03-25 12:35:16 -07002100 if (index == DESC_INDEX) {
2101 ring->int_enable = bcmgenet_rx_ring16_int_enable;
2102 ring->int_disable = bcmgenet_rx_ring16_int_disable;
2103 } else {
2104 ring->int_enable = bcmgenet_rx_ring_int_enable;
2105 ring->int_disable = bcmgenet_rx_ring_int_disable;
2106 }
Petri Gynther8ac467e2015-03-09 13:40:00 -07002107 ring->cbs = priv->rx_cbs + start_ptr;
2108 ring->size = size;
2109 ring->c_index = 0;
2110 ring->read_ptr = start_ptr;
2111 ring->cb_ptr = start_ptr;
2112 ring->end_ptr = end_ptr - 1;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002113
Petri Gynther8ac467e2015-03-09 13:40:00 -07002114 ret = bcmgenet_alloc_rx_buffers(priv, ring);
2115 if (ret)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002116 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002117
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002118 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
2119 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
Petri Gynther6f5a2722015-03-06 13:45:00 -08002120 bcmgenet_rdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002121 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002122 ((size << DMA_RING_SIZE_SHIFT) |
2123 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002124 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002125 (DMA_FC_THRESH_LO <<
2126 DMA_XOFF_THRESHOLD_SHIFT) |
2127 DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
Petri Gynther6f5a2722015-03-06 13:45:00 -08002128
2129 /* Set start and end address, read and write pointers */
Petri Gynther8ac467e2015-03-09 13:40:00 -07002130 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2131 DMA_START_ADDR);
2132 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2133 RDMA_READ_PTR);
2134 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2135 RDMA_WRITE_PTR);
2136 bcmgenet_rdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Petri Gynther6f5a2722015-03-06 13:45:00 -08002137 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002138
2139 return ret;
2140}
2141
Petri Gynthere2aadb42015-03-25 12:35:14 -07002142static void bcmgenet_init_tx_napi(struct bcmgenet_priv *priv)
2143{
2144 unsigned int i;
2145 struct bcmgenet_tx_ring *ring;
2146
2147 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2148 ring = &priv->tx_rings[i];
Eric Dumazetd64b5e82015-11-18 06:31:00 -08002149 netif_tx_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll, 64);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002150 }
2151
2152 ring = &priv->tx_rings[DESC_INDEX];
Eric Dumazetd64b5e82015-11-18 06:31:00 -08002153 netif_tx_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll, 64);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002154}
2155
2156static void bcmgenet_enable_tx_napi(struct bcmgenet_priv *priv)
2157{
2158 unsigned int i;
Doug Berger6689da12017-03-13 17:41:35 -07002159 u32 int0_enable = UMAC_IRQ_TXDMA_DONE;
2160 u32 int1_enable = 0;
Petri Gynthere2aadb42015-03-25 12:35:14 -07002161 struct bcmgenet_tx_ring *ring;
2162
2163 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2164 ring = &priv->tx_rings[i];
2165 napi_enable(&ring->napi);
Doug Berger6689da12017-03-13 17:41:35 -07002166 int1_enable |= (1 << i);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002167 }
2168
2169 ring = &priv->tx_rings[DESC_INDEX];
2170 napi_enable(&ring->napi);
Doug Berger6689da12017-03-13 17:41:35 -07002171
2172 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
2173 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002174}
2175
2176static void bcmgenet_disable_tx_napi(struct bcmgenet_priv *priv)
2177{
2178 unsigned int i;
Doug Berger6689da12017-03-13 17:41:35 -07002179 u32 int0_disable = UMAC_IRQ_TXDMA_DONE;
2180 u32 int1_disable = 0xffff;
Petri Gynthere2aadb42015-03-25 12:35:14 -07002181 struct bcmgenet_tx_ring *ring;
2182
Doug Berger6689da12017-03-13 17:41:35 -07002183 bcmgenet_intrl2_0_writel(priv, int0_disable, INTRL2_CPU_MASK_SET);
2184 bcmgenet_intrl2_1_writel(priv, int1_disable, INTRL2_CPU_MASK_SET);
2185
Petri Gynthere2aadb42015-03-25 12:35:14 -07002186 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2187 ring = &priv->tx_rings[i];
2188 napi_disable(&ring->napi);
2189 }
2190
2191 ring = &priv->tx_rings[DESC_INDEX];
2192 napi_disable(&ring->napi);
2193}
2194
2195static void bcmgenet_fini_tx_napi(struct bcmgenet_priv *priv)
2196{
2197 unsigned int i;
2198 struct bcmgenet_tx_ring *ring;
2199
2200 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2201 ring = &priv->tx_rings[i];
2202 netif_napi_del(&ring->napi);
2203 }
2204
2205 ring = &priv->tx_rings[DESC_INDEX];
2206 netif_napi_del(&ring->napi);
2207}
2208
Petri Gynther16c6d662015-02-23 11:00:45 -08002209/* Initialize Tx queues
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002210 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002211 * Queues 0-3 are priority-based, each one has 32 descriptors,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002212 * with queue 0 being the highest priority queue.
2213 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002214 * Queue 16 is the default Tx queue with
Petri Gynther51a966a2015-02-23 11:00:46 -08002215 * GENET_Q16_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors.
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002216 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002217 * The transmit control block pool is then partitioned as follows:
2218 * - Tx queue 0 uses tx_cbs[0..31]
2219 * - Tx queue 1 uses tx_cbs[32..63]
2220 * - Tx queue 2 uses tx_cbs[64..95]
2221 * - Tx queue 3 uses tx_cbs[96..127]
2222 * - Tx queue 16 uses tx_cbs[128..255]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002223 */
Petri Gynther16c6d662015-02-23 11:00:45 -08002224static void bcmgenet_init_tx_queues(struct net_device *dev)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002225{
2226 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther16c6d662015-02-23 11:00:45 -08002227 u32 i, dma_enable;
2228 u32 dma_ctrl, ring_cfg;
Petri Gynther37742162014-10-07 09:30:01 -07002229 u32 dma_priority[3] = {0, 0, 0};
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002230
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002231 dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
2232 dma_enable = dma_ctrl & DMA_EN;
2233 dma_ctrl &= ~DMA_EN;
2234 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
2235
Petri Gynther16c6d662015-02-23 11:00:45 -08002236 dma_ctrl = 0;
2237 ring_cfg = 0;
2238
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002239 /* Enable strict priority arbiter mode */
2240 bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
2241
Petri Gynther16c6d662015-02-23 11:00:45 -08002242 /* Initialize Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002243 for (i = 0; i < priv->hw_params->tx_queues; i++) {
Petri Gynther51a966a2015-02-23 11:00:46 -08002244 bcmgenet_init_tx_ring(priv, i, priv->hw_params->tx_bds_per_q,
2245 i * priv->hw_params->tx_bds_per_q,
2246 (i + 1) * priv->hw_params->tx_bds_per_q);
Petri Gynther16c6d662015-02-23 11:00:45 -08002247 ring_cfg |= (1 << i);
2248 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002249 dma_priority[DMA_PRIO_REG_INDEX(i)] |=
2250 ((GENET_Q0_PRIORITY + i) << DMA_PRIO_REG_SHIFT(i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002251 }
2252
Petri Gynther16c6d662015-02-23 11:00:45 -08002253 /* Initialize Tx default queue 16 */
Petri Gynther51a966a2015-02-23 11:00:46 -08002254 bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_Q16_TX_BD_CNT,
Petri Gynther16c6d662015-02-23 11:00:45 -08002255 priv->hw_params->tx_queues *
Petri Gynther51a966a2015-02-23 11:00:46 -08002256 priv->hw_params->tx_bds_per_q,
Petri Gynther16c6d662015-02-23 11:00:45 -08002257 TOTAL_DESC);
2258 ring_cfg |= (1 << DESC_INDEX);
2259 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002260 dma_priority[DMA_PRIO_REG_INDEX(DESC_INDEX)] |=
2261 ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) <<
2262 DMA_PRIO_REG_SHIFT(DESC_INDEX));
Petri Gynther16c6d662015-02-23 11:00:45 -08002263
2264 /* Set Tx queue priorities */
Petri Gynther37742162014-10-07 09:30:01 -07002265 bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
2266 bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
2267 bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);
2268
Petri Gynthere2aadb42015-03-25 12:35:14 -07002269 /* Initialize Tx NAPI */
2270 bcmgenet_init_tx_napi(priv);
2271
Petri Gynther16c6d662015-02-23 11:00:45 -08002272 /* Enable Tx queues */
2273 bcmgenet_tdma_writel(priv, ring_cfg, DMA_RING_CFG);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002274
Petri Gynther16c6d662015-02-23 11:00:45 -08002275 /* Enable Tx DMA */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002276 if (dma_enable)
Petri Gynther16c6d662015-02-23 11:00:45 -08002277 dma_ctrl |= DMA_EN;
2278 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002279}
2280
Petri Gynther3ab11332015-03-25 12:35:15 -07002281static void bcmgenet_init_rx_napi(struct bcmgenet_priv *priv)
2282{
Petri Gynther4055eae2015-03-25 12:35:16 -07002283 unsigned int i;
2284 struct bcmgenet_rx_ring *ring;
2285
2286 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2287 ring = &priv->rx_rings[i];
2288 netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll, 64);
2289 }
2290
2291 ring = &priv->rx_rings[DESC_INDEX];
2292 netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll, 64);
Petri Gynther3ab11332015-03-25 12:35:15 -07002293}
2294
2295static void bcmgenet_enable_rx_napi(struct bcmgenet_priv *priv)
2296{
Petri Gynther4055eae2015-03-25 12:35:16 -07002297 unsigned int i;
Doug Berger6689da12017-03-13 17:41:35 -07002298 u32 int0_enable = UMAC_IRQ_RXDMA_DONE;
2299 u32 int1_enable = 0;
Petri Gynther4055eae2015-03-25 12:35:16 -07002300 struct bcmgenet_rx_ring *ring;
2301
2302 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2303 ring = &priv->rx_rings[i];
2304 napi_enable(&ring->napi);
Doug Berger6689da12017-03-13 17:41:35 -07002305 int1_enable |= (1 << (UMAC_IRQ1_RX_INTR_SHIFT + i));
Petri Gynther4055eae2015-03-25 12:35:16 -07002306 }
2307
2308 ring = &priv->rx_rings[DESC_INDEX];
2309 napi_enable(&ring->napi);
Doug Berger6689da12017-03-13 17:41:35 -07002310
2311 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
2312 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
Petri Gynther3ab11332015-03-25 12:35:15 -07002313}
2314
2315static void bcmgenet_disable_rx_napi(struct bcmgenet_priv *priv)
2316{
Petri Gynther4055eae2015-03-25 12:35:16 -07002317 unsigned int i;
Doug Berger6689da12017-03-13 17:41:35 -07002318 u32 int0_disable = UMAC_IRQ_RXDMA_DONE;
2319 u32 int1_disable = 0xffff << UMAC_IRQ1_RX_INTR_SHIFT;
Petri Gynther4055eae2015-03-25 12:35:16 -07002320 struct bcmgenet_rx_ring *ring;
2321
Doug Berger6689da12017-03-13 17:41:35 -07002322 bcmgenet_intrl2_0_writel(priv, int0_disable, INTRL2_CPU_MASK_SET);
2323 bcmgenet_intrl2_1_writel(priv, int1_disable, INTRL2_CPU_MASK_SET);
2324
Petri Gynther4055eae2015-03-25 12:35:16 -07002325 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2326 ring = &priv->rx_rings[i];
2327 napi_disable(&ring->napi);
2328 }
2329
2330 ring = &priv->rx_rings[DESC_INDEX];
2331 napi_disable(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002332}
2333
2334static void bcmgenet_fini_rx_napi(struct bcmgenet_priv *priv)
2335{
Petri Gynther4055eae2015-03-25 12:35:16 -07002336 unsigned int i;
2337 struct bcmgenet_rx_ring *ring;
2338
2339 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2340 ring = &priv->rx_rings[i];
2341 netif_napi_del(&ring->napi);
2342 }
2343
2344 ring = &priv->rx_rings[DESC_INDEX];
2345 netif_napi_del(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002346}
2347
Petri Gynther8ac467e2015-03-09 13:40:00 -07002348/* Initialize Rx queues
2349 *
2350 * Queues 0-15 are priority queues. Hardware Filtering Block (HFB) can be
2351 * used to direct traffic to these queues.
2352 *
2353 * Queue 16 is the default Rx queue with GENET_Q16_RX_BD_CNT descriptors.
2354 */
2355static int bcmgenet_init_rx_queues(struct net_device *dev)
2356{
2357 struct bcmgenet_priv *priv = netdev_priv(dev);
2358 u32 i;
2359 u32 dma_enable;
2360 u32 dma_ctrl;
2361 u32 ring_cfg;
2362 int ret;
2363
2364 dma_ctrl = bcmgenet_rdma_readl(priv, DMA_CTRL);
2365 dma_enable = dma_ctrl & DMA_EN;
2366 dma_ctrl &= ~DMA_EN;
2367 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2368
2369 dma_ctrl = 0;
2370 ring_cfg = 0;
2371
2372 /* Initialize Rx priority queues */
2373 for (i = 0; i < priv->hw_params->rx_queues; i++) {
2374 ret = bcmgenet_init_rx_ring(priv, i,
2375 priv->hw_params->rx_bds_per_q,
2376 i * priv->hw_params->rx_bds_per_q,
2377 (i + 1) *
2378 priv->hw_params->rx_bds_per_q);
2379 if (ret)
2380 return ret;
2381
2382 ring_cfg |= (1 << i);
2383 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2384 }
2385
2386 /* Initialize Rx default queue 16 */
2387 ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, GENET_Q16_RX_BD_CNT,
2388 priv->hw_params->rx_queues *
2389 priv->hw_params->rx_bds_per_q,
2390 TOTAL_DESC);
2391 if (ret)
2392 return ret;
2393
2394 ring_cfg |= (1 << DESC_INDEX);
2395 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
2396
Petri Gynther3ab11332015-03-25 12:35:15 -07002397 /* Initialize Rx NAPI */
2398 bcmgenet_init_rx_napi(priv);
2399
Petri Gynther8ac467e2015-03-09 13:40:00 -07002400 /* Enable rings */
2401 bcmgenet_rdma_writel(priv, ring_cfg, DMA_RING_CFG);
2402
2403 /* Configure ring as descriptor ring and re-enable DMA if enabled */
2404 if (dma_enable)
2405 dma_ctrl |= DMA_EN;
2406 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2407
2408 return 0;
2409}
2410
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002411static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
2412{
2413 int ret = 0;
2414 int timeout = 0;
2415 u32 reg;
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002416 u32 dma_ctrl;
2417 int i;
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002418
2419 /* Disable TDMA to stop add more frames in TX DMA */
2420 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2421 reg &= ~DMA_EN;
2422 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2423
2424 /* Check TDMA status register to confirm TDMA is disabled */
2425 while (timeout++ < DMA_TIMEOUT_VAL) {
2426 reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
2427 if (reg & DMA_DISABLED)
2428 break;
2429
2430 udelay(1);
2431 }
2432
2433 if (timeout == DMA_TIMEOUT_VAL) {
2434 netdev_warn(priv->dev, "Timed out while disabling TX DMA\n");
2435 ret = -ETIMEDOUT;
2436 }
2437
2438 /* Wait 10ms for packet drain in both tx and rx dma */
2439 usleep_range(10000, 20000);
2440
2441 /* Disable RDMA */
2442 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2443 reg &= ~DMA_EN;
2444 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2445
2446 timeout = 0;
2447 /* Check RDMA status register to confirm RDMA is disabled */
2448 while (timeout++ < DMA_TIMEOUT_VAL) {
2449 reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
2450 if (reg & DMA_DISABLED)
2451 break;
2452
2453 udelay(1);
2454 }
2455
2456 if (timeout == DMA_TIMEOUT_VAL) {
2457 netdev_warn(priv->dev, "Timed out while disabling RX DMA\n");
2458 ret = -ETIMEDOUT;
2459 }
2460
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002461 dma_ctrl = 0;
2462 for (i = 0; i < priv->hw_params->rx_queues; i++)
2463 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2464 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2465 reg &= ~dma_ctrl;
2466 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2467
2468 dma_ctrl = 0;
2469 for (i = 0; i < priv->hw_params->tx_queues; i++)
2470 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2471 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2472 reg &= ~dma_ctrl;
2473 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2474
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002475 return ret;
2476}
2477
Petri Gynther9abab962015-03-30 00:29:01 -07002478static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002479{
2480 int i;
Petri Gynthere178c8c2016-04-09 00:20:36 -07002481 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002482
Petri Gynther9abab962015-03-30 00:29:01 -07002483 bcmgenet_fini_rx_napi(priv);
2484 bcmgenet_fini_tx_napi(priv);
2485
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002486 /* disable DMA */
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002487 bcmgenet_dma_teardown(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002488
2489 for (i = 0; i < priv->num_tx_bds; i++) {
2490 if (priv->tx_cbs[i].skb != NULL) {
2491 dev_kfree_skb(priv->tx_cbs[i].skb);
2492 priv->tx_cbs[i].skb = NULL;
2493 }
2494 }
2495
Petri Gynthere178c8c2016-04-09 00:20:36 -07002496 for (i = 0; i < priv->hw_params->tx_queues; i++) {
2497 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[i].queue);
2498 netdev_tx_reset_queue(txq);
2499 }
2500
2501 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[DESC_INDEX].queue);
2502 netdev_tx_reset_queue(txq);
2503
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002504 bcmgenet_free_rx_buffers(priv);
2505 kfree(priv->rx_cbs);
2506 kfree(priv->tx_cbs);
2507}
2508
2509/* init_edma: Initialize DMA control register */
2510static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
2511{
2512 int ret;
Petri Gynther014012a2015-02-23 11:00:45 -08002513 unsigned int i;
2514 struct enet_cb *cb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002515
Petri Gynther6f5a2722015-03-06 13:45:00 -08002516 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002517
Petri Gynther6f5a2722015-03-06 13:45:00 -08002518 /* Initialize common Rx ring structures */
2519 priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
2520 priv->num_rx_bds = TOTAL_DESC;
2521 priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct enet_cb),
2522 GFP_KERNEL);
2523 if (!priv->rx_cbs)
2524 return -ENOMEM;
2525
2526 for (i = 0; i < priv->num_rx_bds; i++) {
2527 cb = priv->rx_cbs + i;
2528 cb->bd_addr = priv->rx_bds + i * DMA_DESC_SIZE;
2529 }
2530
Brian Norris7fc527f2014-07-29 14:34:14 -07002531 /* Initialize common TX ring structures */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002532 priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
2533 priv->num_tx_bds = TOTAL_DESC;
Florian Fainellic489be02014-07-23 10:42:15 -07002534 priv->tx_cbs = kcalloc(priv->num_tx_bds, sizeof(struct enet_cb),
Florian Fainellic91b7f62014-07-23 10:42:12 -07002535 GFP_KERNEL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002536 if (!priv->tx_cbs) {
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002537 kfree(priv->rx_cbs);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002538 return -ENOMEM;
2539 }
2540
Petri Gynther014012a2015-02-23 11:00:45 -08002541 for (i = 0; i < priv->num_tx_bds; i++) {
2542 cb = priv->tx_cbs + i;
2543 cb->bd_addr = priv->tx_bds + i * DMA_DESC_SIZE;
2544 }
2545
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002546 /* Init rDma */
2547 bcmgenet_rdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
2548
2549 /* Initialize Rx queues */
2550 ret = bcmgenet_init_rx_queues(priv->dev);
2551 if (ret) {
2552 netdev_err(priv->dev, "failed to initialize Rx queues\n");
2553 bcmgenet_free_rx_buffers(priv);
2554 kfree(priv->rx_cbs);
2555 kfree(priv->tx_cbs);
2556 return ret;
2557 }
2558
2559 /* Init tDma */
2560 bcmgenet_tdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
2561
Petri Gynther16c6d662015-02-23 11:00:45 -08002562 /* Initialize Tx queues */
2563 bcmgenet_init_tx_queues(priv->dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002564
2565 return 0;
2566}
2567
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002568/* Interrupt bottom half */
2569static void bcmgenet_irq_task(struct work_struct *work)
2570{
Doug Berger07c52d62017-03-09 16:58:47 -08002571 unsigned long flags;
2572 unsigned int status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002573 struct bcmgenet_priv *priv = container_of(
2574 work, struct bcmgenet_priv, bcmgenet_irq_work);
2575
2576 netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
2577
Doug Berger07c52d62017-03-09 16:58:47 -08002578 spin_lock_irqsave(&priv->lock, flags);
2579 status = priv->irq0_stat;
2580 priv->irq0_stat = 0;
2581 spin_unlock_irqrestore(&priv->lock, flags);
2582
2583 if (status & UMAC_IRQ_MPD_R) {
Florian Fainelli8fdb0e02014-07-21 15:29:26 -07002584 netif_dbg(priv, wol, priv->dev,
2585 "magic packet detected, waking up\n");
2586 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002587 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002588
2589 /* Link UP/DOWN event */
Doug Berger07c52d62017-03-09 16:58:47 -08002590 if (status & UMAC_IRQ_LINK_EVENT)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002591 phy_mac_interrupt(priv->phydev,
Doug Berger07c52d62017-03-09 16:58:47 -08002592 !!(status & UMAC_IRQ_LINK_UP));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002593}
2594
Petri Gynther4055eae2015-03-25 12:35:16 -07002595/* bcmgenet_isr1: handle Rx and Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002596static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
2597{
2598 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002599 struct bcmgenet_rx_ring *rx_ring;
2600 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08002601 unsigned int index, status;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002602
Doug Berger07c52d62017-03-09 16:58:47 -08002603 /* Read irq status */
2604 status = bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002605 ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002606
Brian Norris7fc527f2014-07-29 14:34:14 -07002607 /* clear interrupts */
Doug Berger07c52d62017-03-09 16:58:47 -08002608 bcmgenet_intrl2_1_writel(priv, status, INTRL2_CPU_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002609
2610 netif_dbg(priv, intr, priv->dev,
Doug Berger07c52d62017-03-09 16:58:47 -08002611 "%s: IRQ=0x%x\n", __func__, status);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002612
Petri Gynther4055eae2015-03-25 12:35:16 -07002613 /* Check Rx priority queue interrupts */
2614 for (index = 0; index < priv->hw_params->rx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08002615 if (!(status & BIT(UMAC_IRQ1_RX_INTR_SHIFT + index)))
Petri Gynther4055eae2015-03-25 12:35:16 -07002616 continue;
2617
2618 rx_ring = &priv->rx_rings[index];
2619
2620 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2621 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002622 __napi_schedule_irqoff(&rx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07002623 }
2624 }
2625
2626 /* Check Tx priority queue interrupts */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002627 for (index = 0; index < priv->hw_params->tx_queues; index++) {
Doug Berger07c52d62017-03-09 16:58:47 -08002628 if (!(status & BIT(index)))
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002629 continue;
2630
Petri Gynther4055eae2015-03-25 12:35:16 -07002631 tx_ring = &priv->tx_rings[index];
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002632
Petri Gynther4055eae2015-03-25 12:35:16 -07002633 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2634 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002635 __napi_schedule_irqoff(&tx_ring->napi);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002636 }
2637 }
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002638
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002639 return IRQ_HANDLED;
2640}
2641
Petri Gynther4055eae2015-03-25 12:35:16 -07002642/* bcmgenet_isr0: handle Rx and Tx default queues + other stuff */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002643static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
2644{
2645 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002646 struct bcmgenet_rx_ring *rx_ring;
2647 struct bcmgenet_tx_ring *tx_ring;
Doug Berger07c52d62017-03-09 16:58:47 -08002648 unsigned int status;
2649 unsigned long flags;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002650
Doug Berger07c52d62017-03-09 16:58:47 -08002651 /* Read irq status */
2652 status = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002653 ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002654
Brian Norris7fc527f2014-07-29 14:34:14 -07002655 /* clear interrupts */
Doug Berger07c52d62017-03-09 16:58:47 -08002656 bcmgenet_intrl2_0_writel(priv, status, INTRL2_CPU_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002657
2658 netif_dbg(priv, intr, priv->dev,
Doug Berger07c52d62017-03-09 16:58:47 -08002659 "IRQ=0x%x\n", status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002660
Doug Berger07c52d62017-03-09 16:58:47 -08002661 if (status & UMAC_IRQ_RXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07002662 rx_ring = &priv->rx_rings[DESC_INDEX];
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002663
Petri Gynther4055eae2015-03-25 12:35:16 -07002664 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2665 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002666 __napi_schedule_irqoff(&rx_ring->napi);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002667 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002668 }
Petri Gynther4055eae2015-03-25 12:35:16 -07002669
Doug Berger07c52d62017-03-09 16:58:47 -08002670 if (status & UMAC_IRQ_TXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07002671 tx_ring = &priv->tx_rings[DESC_INDEX];
2672
2673 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2674 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002675 __napi_schedule_irqoff(&tx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07002676 }
2677 }
2678
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002679 if (priv->irq0_stat & (UMAC_IRQ_PHY_DET_R |
2680 UMAC_IRQ_PHY_DET_F |
Petri Gynthere122966d2015-03-30 00:29:24 -07002681 UMAC_IRQ_LINK_EVENT |
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002682 UMAC_IRQ_HFB_SM |
Doug Bergerb1ec4942017-03-13 17:41:36 -07002683 UMAC_IRQ_HFB_MM)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002684 /* all other interested interrupts handled in bottom half */
2685 schedule_work(&priv->bcmgenet_irq_work);
2686 }
2687
2688 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Doug Berger07c52d62017-03-09 16:58:47 -08002689 status & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002690 wake_up(&priv->wq);
2691 }
2692
Doug Berger07c52d62017-03-09 16:58:47 -08002693 /* all other interested interrupts handled in bottom half */
2694 status &= (UMAC_IRQ_LINK_EVENT |
2695 UMAC_IRQ_MPD_R);
2696 if (status) {
2697 /* Save irq status for bottom-half processing. */
2698 spin_lock_irqsave(&priv->lock, flags);
2699 priv->irq0_stat |= status;
2700 spin_unlock_irqrestore(&priv->lock, flags);
2701
2702 schedule_work(&priv->bcmgenet_irq_work);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002703 }
2704
2705 return IRQ_HANDLED;
2706}
2707
Florian Fainelli85620562014-07-21 15:29:23 -07002708static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
2709{
2710 struct bcmgenet_priv *priv = dev_id;
2711
2712 pm_wakeup_event(&priv->pdev->dev, 0);
2713
2714 return IRQ_HANDLED;
2715}
2716
Florian Fainelli4d2e8882015-07-31 11:42:54 -07002717#ifdef CONFIG_NET_POLL_CONTROLLER
2718static void bcmgenet_poll_controller(struct net_device *dev)
2719{
2720 struct bcmgenet_priv *priv = netdev_priv(dev);
2721
2722 /* Invoke the main RX/TX interrupt handler */
2723 disable_irq(priv->irq0);
2724 bcmgenet_isr0(priv->irq0, priv);
2725 enable_irq(priv->irq0);
2726
2727 /* And the interrupt handler for RX/TX priority queues */
2728 disable_irq(priv->irq1);
2729 bcmgenet_isr1(priv->irq1, priv);
2730 enable_irq(priv->irq1);
2731}
2732#endif
2733
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002734static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
2735{
2736 u32 reg;
2737
2738 reg = bcmgenet_rbuf_ctrl_get(priv);
2739 reg |= BIT(1);
2740 bcmgenet_rbuf_ctrl_set(priv, reg);
2741 udelay(10);
2742
2743 reg &= ~BIT(1);
2744 bcmgenet_rbuf_ctrl_set(priv, reg);
2745 udelay(10);
2746}
2747
2748static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002749 unsigned char *addr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002750{
2751 bcmgenet_umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
2752 (addr[2] << 8) | addr[3], UMAC_MAC0);
2753 bcmgenet_umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
2754}
2755
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002756/* Returns a reusable dma control register value */
2757static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
2758{
2759 u32 reg;
2760 u32 dma_ctrl;
2761
2762 /* disable DMA */
2763 dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
2764 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2765 reg &= ~dma_ctrl;
2766 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2767
2768 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2769 reg &= ~dma_ctrl;
2770 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2771
2772 bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
2773 udelay(10);
2774 bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
2775
2776 return dma_ctrl;
2777}
2778
2779static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
2780{
2781 u32 reg;
2782
2783 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2784 reg |= dma_ctrl;
2785 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2786
2787 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2788 reg |= dma_ctrl;
2789 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2790}
2791
Petri Gynther0034de42015-03-13 14:45:00 -07002792/* bcmgenet_hfb_clear
2793 *
2794 * Clear Hardware Filter Block and disable all filtering.
2795 */
2796static void bcmgenet_hfb_clear(struct bcmgenet_priv *priv)
2797{
2798 u32 i;
2799
2800 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_CTRL);
2801 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS);
2802 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS + 4);
2803
2804 for (i = DMA_INDEX2RING_0; i <= DMA_INDEX2RING_7; i++)
2805 bcmgenet_rdma_writel(priv, 0x0, i);
2806
2807 for (i = 0; i < (priv->hw_params->hfb_filter_cnt / 4); i++)
2808 bcmgenet_hfb_reg_writel(priv, 0x0,
2809 HFB_FLT_LEN_V3PLUS + i * sizeof(u32));
2810
2811 for (i = 0; i < priv->hw_params->hfb_filter_cnt *
2812 priv->hw_params->hfb_filter_size; i++)
2813 bcmgenet_hfb_writel(priv, 0x0, i * sizeof(u32));
2814}
2815
2816static void bcmgenet_hfb_init(struct bcmgenet_priv *priv)
2817{
2818 if (GENET_IS_V1(priv) || GENET_IS_V2(priv))
2819 return;
2820
2821 bcmgenet_hfb_clear(priv);
2822}
2823
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002824static void bcmgenet_netif_start(struct net_device *dev)
2825{
2826 struct bcmgenet_priv *priv = netdev_priv(dev);
2827
2828 /* Start the network engine */
Petri Gynther3ab11332015-03-25 12:35:15 -07002829 bcmgenet_enable_rx_napi(priv);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002830 bcmgenet_enable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002831
2832 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
2833
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002834 netif_tx_start_all_queues(dev);
2835
Florian Fainelli37850e32015-10-17 14:22:46 -07002836 /* Monitor link interrupts now */
2837 bcmgenet_link_intr_enable(priv);
2838
Florian Fainelli0299b6a2016-09-26 22:31:56 +02002839 phy_start(priv->phydev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002840}
2841
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002842static int bcmgenet_open(struct net_device *dev)
2843{
2844 struct bcmgenet_priv *priv = netdev_priv(dev);
2845 unsigned long dma_ctrl;
2846 u32 reg;
2847 int ret;
2848
2849 netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
2850
2851 /* Turn on the clock */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002852 clk_prepare_enable(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002853
Florian Fainellia642c4f2015-03-23 15:09:56 -07002854 /* If this is an internal GPHY, power it back on now, before UniMAC is
2855 * brought out of reset as absolutely no UniMAC activity is allowed
2856 */
Florian Fainellic624f892015-07-16 15:51:17 -07002857 if (priv->internal_phy)
Florian Fainellia642c4f2015-03-23 15:09:56 -07002858 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
2859
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002860 /* take MAC out of reset */
2861 bcmgenet_umac_reset(priv);
2862
2863 ret = init_umac(priv);
2864 if (ret)
2865 goto err_clk_disable;
2866
2867 /* disable ethernet MAC while updating its registers */
Florian Fainellie29585b2014-07-21 15:29:20 -07002868 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002869
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002870 /* Make sure we reflect the value of CRC_CMD_FWD */
2871 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
2872 priv->crc_fwd_en = !!(reg & CMD_CRC_FWD);
2873
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002874 bcmgenet_set_hw_addr(priv, dev->dev_addr);
2875
Florian Fainellic624f892015-07-16 15:51:17 -07002876 if (priv->internal_phy) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002877 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
2878 reg |= EXT_ENERGY_DET_MASK;
2879 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
2880 }
2881
2882 /* Disable RX/TX DMA and flush TX queues */
2883 dma_ctrl = bcmgenet_dma_disable(priv);
2884
2885 /* Reinitialize TDMA and RDMA and SW housekeeping */
2886 ret = bcmgenet_init_dma(priv);
2887 if (ret) {
2888 netdev_err(dev, "failed to initialize DMA\n");
Petri Gyntherfac25942015-03-30 00:29:13 -07002889 goto err_clk_disable;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002890 }
2891
2892 /* Always enable ring 16 - descriptor ring */
2893 bcmgenet_enable_dma(priv, dma_ctrl);
2894
Petri Gynther0034de42015-03-13 14:45:00 -07002895 /* HFB init */
2896 bcmgenet_hfb_init(priv);
2897
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002898 ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002899 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002900 if (ret < 0) {
2901 netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
2902 goto err_fini_dma;
2903 }
2904
2905 ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002906 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002907 if (ret < 0) {
2908 netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
2909 goto err_irq0;
2910 }
2911
Florian Fainelli6cc8e6d2015-07-16 15:51:18 -07002912 ret = bcmgenet_mii_probe(dev);
2913 if (ret) {
2914 netdev_err(dev, "failed to connect to PHY\n");
2915 goto err_irq1;
2916 }
Florian Fainellic96e7312014-11-10 18:06:20 -08002917
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002918 bcmgenet_netif_start(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002919
2920 return 0;
2921
Florian Fainelli6cc8e6d2015-07-16 15:51:18 -07002922err_irq1:
2923 free_irq(priv->irq1, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002924err_irq0:
Florian Fainelli978ffac2015-07-16 15:51:15 -07002925 free_irq(priv->irq0, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002926err_fini_dma:
2927 bcmgenet_fini_dma(priv);
2928err_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08002929 if (priv->internal_phy)
2930 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002931 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002932 return ret;
2933}
2934
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002935static void bcmgenet_netif_stop(struct net_device *dev)
2936{
2937 struct bcmgenet_priv *priv = netdev_priv(dev);
2938
2939 netif_tx_stop_all_queues(dev);
Florian Fainelli0299b6a2016-09-26 22:31:56 +02002940 phy_stop(priv->phydev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002941 bcmgenet_intr_disable(priv);
Petri Gynther3ab11332015-03-25 12:35:15 -07002942 bcmgenet_disable_rx_napi(priv);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002943 bcmgenet_disable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002944
2945 /* Wait for pending work items to complete. Since interrupts are
2946 * disabled no new work will be scheduled.
2947 */
2948 cancel_work_sync(&priv->bcmgenet_irq_work);
Florian Fainellicc013fb2014-08-11 14:50:43 -07002949
Florian Fainellicc013fb2014-08-11 14:50:43 -07002950 priv->old_link = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002951 priv->old_speed = -1;
Florian Fainellicc013fb2014-08-11 14:50:43 -07002952 priv->old_duplex = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002953 priv->old_pause = -1;
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002954}
2955
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002956static int bcmgenet_close(struct net_device *dev)
2957{
2958 struct bcmgenet_priv *priv = netdev_priv(dev);
2959 int ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002960
2961 netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
2962
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002963 bcmgenet_netif_stop(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002964
Florian Fainellic96e7312014-11-10 18:06:20 -08002965 /* Really kill the PHY state machine and disconnect from it */
Florian Fainelli0299b6a2016-09-26 22:31:56 +02002966 phy_disconnect(priv->phydev);
Florian Fainellic96e7312014-11-10 18:06:20 -08002967
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002968 /* Disable MAC receive */
Florian Fainellie29585b2014-07-21 15:29:20 -07002969 umac_enable_set(priv, CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002970
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002971 ret = bcmgenet_dma_teardown(priv);
2972 if (ret)
2973 return ret;
2974
Doug Berger556c2cf2017-03-13 17:41:34 -07002975 /* Disable MAC transmit. TX DMA disabled must be done before this */
Florian Fainellie29585b2014-07-21 15:29:20 -07002976 umac_enable_set(priv, CMD_TX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002977
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002978 /* tx reclaim */
2979 bcmgenet_tx_reclaim_all(dev);
2980 bcmgenet_fini_dma(priv);
2981
2982 free_irq(priv->irq0, priv);
2983 free_irq(priv->irq1, priv);
2984
Florian Fainellic624f892015-07-16 15:51:17 -07002985 if (priv->internal_phy)
Florian Fainellica8cf342015-03-23 15:09:51 -07002986 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002987
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002988 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002989
Florian Fainellica8cf342015-03-23 15:09:51 -07002990 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002991}
2992
Florian Fainelli13ea6572015-06-04 16:15:50 -07002993static void bcmgenet_dump_tx_queue(struct bcmgenet_tx_ring *ring)
2994{
2995 struct bcmgenet_priv *priv = ring->priv;
2996 u32 p_index, c_index, intsts, intmsk;
2997 struct netdev_queue *txq;
2998 unsigned int free_bds;
2999 unsigned long flags;
3000 bool txq_stopped;
3001
3002 if (!netif_msg_tx_err(priv))
3003 return;
3004
3005 txq = netdev_get_tx_queue(priv->dev, ring->queue);
3006
3007 spin_lock_irqsave(&ring->lock, flags);
3008 if (ring->index == DESC_INDEX) {
3009 intsts = ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
3010 intmsk = UMAC_IRQ_TXDMA_DONE | UMAC_IRQ_TXDMA_MBDONE;
3011 } else {
3012 intsts = ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
3013 intmsk = 1 << ring->index;
3014 }
3015 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
3016 p_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_PROD_INDEX);
3017 txq_stopped = netif_tx_queue_stopped(txq);
3018 free_bds = ring->free_bds;
3019 spin_unlock_irqrestore(&ring->lock, flags);
3020
3021 netif_err(priv, tx_err, priv->dev, "Ring %d queue %d status summary\n"
3022 "TX queue status: %s, interrupts: %s\n"
3023 "(sw)free_bds: %d (sw)size: %d\n"
3024 "(sw)p_index: %d (hw)p_index: %d\n"
3025 "(sw)c_index: %d (hw)c_index: %d\n"
3026 "(sw)clean_p: %d (sw)write_p: %d\n"
3027 "(sw)cb_ptr: %d (sw)end_ptr: %d\n",
3028 ring->index, ring->queue,
3029 txq_stopped ? "stopped" : "active",
3030 intsts & intmsk ? "enabled" : "disabled",
3031 free_bds, ring->size,
3032 ring->prod_index, p_index & DMA_P_INDEX_MASK,
3033 ring->c_index, c_index & DMA_C_INDEX_MASK,
3034 ring->clean_ptr, ring->write_ptr,
3035 ring->cb_ptr, ring->end_ptr);
3036}
3037
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003038static void bcmgenet_timeout(struct net_device *dev)
3039{
3040 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli13ea6572015-06-04 16:15:50 -07003041 u32 int0_enable = 0;
3042 u32 int1_enable = 0;
3043 unsigned int q;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003044
3045 netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
3046
Florian Fainelli13ea6572015-06-04 16:15:50 -07003047 for (q = 0; q < priv->hw_params->tx_queues; q++)
3048 bcmgenet_dump_tx_queue(&priv->tx_rings[q]);
3049 bcmgenet_dump_tx_queue(&priv->tx_rings[DESC_INDEX]);
3050
3051 bcmgenet_tx_reclaim_all(dev);
3052
3053 for (q = 0; q < priv->hw_params->tx_queues; q++)
3054 int1_enable |= (1 << q);
3055
3056 int0_enable = UMAC_IRQ_TXDMA_DONE;
3057
3058 /* Re-enable TX interrupts if disabled */
3059 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
3060 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
3061
Florian Westphal860e9532016-05-03 16:33:13 +02003062 netif_trans_update(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003063
3064 dev->stats.tx_errors++;
3065
3066 netif_tx_wake_all_queues(dev);
3067}
3068
3069#define MAX_MC_COUNT 16
3070
3071static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
3072 unsigned char *addr,
3073 int *i,
3074 int *mc)
3075{
3076 u32 reg;
3077
Florian Fainellic91b7f62014-07-23 10:42:12 -07003078 bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
3079 UMAC_MDF_ADDR + (*i * 4));
3080 bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
3081 addr[4] << 8 | addr[5],
3082 UMAC_MDF_ADDR + ((*i + 1) * 4));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003083 reg = bcmgenet_umac_readl(priv, UMAC_MDF_CTRL);
3084 reg |= (1 << (MAX_MC_COUNT - *mc));
3085 bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
3086 *i += 2;
3087 (*mc)++;
3088}
3089
3090static void bcmgenet_set_rx_mode(struct net_device *dev)
3091{
3092 struct bcmgenet_priv *priv = netdev_priv(dev);
3093 struct netdev_hw_addr *ha;
3094 int i, mc;
3095 u32 reg;
3096
3097 netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
3098
Brian Norris7fc527f2014-07-29 14:34:14 -07003099 /* Promiscuous mode */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003100 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
3101 if (dev->flags & IFF_PROMISC) {
3102 reg |= CMD_PROMISC;
3103 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3104 bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
3105 return;
3106 } else {
3107 reg &= ~CMD_PROMISC;
3108 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3109 }
3110
3111 /* UniMac doesn't support ALLMULTI */
3112 if (dev->flags & IFF_ALLMULTI) {
3113 netdev_warn(dev, "ALLMULTI is not supported\n");
3114 return;
3115 }
3116
3117 /* update MDF filter */
3118 i = 0;
3119 mc = 0;
3120 /* Broadcast */
3121 bcmgenet_set_mdf_addr(priv, dev->broadcast, &i, &mc);
3122 /* my own address.*/
3123 bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i, &mc);
3124 /* Unicast list*/
3125 if (netdev_uc_count(dev) > (MAX_MC_COUNT - mc))
3126 return;
3127
3128 if (!netdev_uc_empty(dev))
3129 netdev_for_each_uc_addr(ha, dev)
3130 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
3131 /* Multicast */
3132 if (netdev_mc_empty(dev) || netdev_mc_count(dev) >= (MAX_MC_COUNT - mc))
3133 return;
3134
3135 netdev_for_each_mc_addr(ha, dev)
3136 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
3137}
3138
3139/* Set the hardware MAC address. */
3140static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
3141{
3142 struct sockaddr *addr = p;
3143
3144 /* Setting the MAC address at the hardware level is not possible
3145 * without disabling the UniMAC RX/TX enable bits.
3146 */
3147 if (netif_running(dev))
3148 return -EBUSY;
3149
3150 ether_addr_copy(dev->dev_addr, addr->sa_data);
3151
3152 return 0;
3153}
3154
Florian Fainelli37a30b42017-03-16 10:27:08 -07003155static struct net_device_stats *bcmgenet_get_stats(struct net_device *dev)
3156{
3157 struct bcmgenet_priv *priv = netdev_priv(dev);
3158 unsigned long tx_bytes = 0, tx_packets = 0;
3159 unsigned long rx_bytes = 0, rx_packets = 0;
3160 unsigned long rx_errors = 0, rx_dropped = 0;
3161 struct bcmgenet_tx_ring *tx_ring;
3162 struct bcmgenet_rx_ring *rx_ring;
3163 unsigned int q;
3164
3165 for (q = 0; q < priv->hw_params->tx_queues; q++) {
3166 tx_ring = &priv->tx_rings[q];
3167 tx_bytes += tx_ring->bytes;
3168 tx_packets += tx_ring->packets;
3169 }
3170 tx_ring = &priv->tx_rings[DESC_INDEX];
3171 tx_bytes += tx_ring->bytes;
3172 tx_packets += tx_ring->packets;
3173
3174 for (q = 0; q < priv->hw_params->rx_queues; q++) {
3175 rx_ring = &priv->rx_rings[q];
3176
3177 rx_bytes += rx_ring->bytes;
3178 rx_packets += rx_ring->packets;
3179 rx_errors += rx_ring->errors;
3180 rx_dropped += rx_ring->dropped;
3181 }
3182 rx_ring = &priv->rx_rings[DESC_INDEX];
3183 rx_bytes += rx_ring->bytes;
3184 rx_packets += rx_ring->packets;
3185 rx_errors += rx_ring->errors;
3186 rx_dropped += rx_ring->dropped;
3187
3188 dev->stats.tx_bytes = tx_bytes;
3189 dev->stats.tx_packets = tx_packets;
3190 dev->stats.rx_bytes = rx_bytes;
3191 dev->stats.rx_packets = rx_packets;
3192 dev->stats.rx_errors = rx_errors;
3193 dev->stats.rx_missed_errors = rx_errors;
3194 return &dev->stats;
3195}
3196
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003197static const struct net_device_ops bcmgenet_netdev_ops = {
3198 .ndo_open = bcmgenet_open,
3199 .ndo_stop = bcmgenet_close,
3200 .ndo_start_xmit = bcmgenet_xmit,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003201 .ndo_tx_timeout = bcmgenet_timeout,
3202 .ndo_set_rx_mode = bcmgenet_set_rx_mode,
3203 .ndo_set_mac_address = bcmgenet_set_mac_addr,
3204 .ndo_do_ioctl = bcmgenet_ioctl,
3205 .ndo_set_features = bcmgenet_set_features,
Florian Fainelli4d2e8882015-07-31 11:42:54 -07003206#ifdef CONFIG_NET_POLL_CONTROLLER
3207 .ndo_poll_controller = bcmgenet_poll_controller,
3208#endif
Florian Fainelli37a30b42017-03-16 10:27:08 -07003209 .ndo_get_stats = bcmgenet_get_stats,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003210};
3211
3212/* Array of GENET hardware parameters/characteristics */
3213static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
3214 [GENET_V1] = {
3215 .tx_queues = 0,
Petri Gynther51a966a2015-02-23 11:00:46 -08003216 .tx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003217 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003218 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003219 .bp_in_en_shift = 16,
3220 .bp_in_mask = 0xffff,
3221 .hfb_filter_cnt = 16,
3222 .qtag_mask = 0x1F,
3223 .hfb_offset = 0x1000,
3224 .rdma_offset = 0x2000,
3225 .tdma_offset = 0x3000,
3226 .words_per_bd = 2,
3227 },
3228 [GENET_V2] = {
3229 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003230 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003231 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003232 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003233 .bp_in_en_shift = 16,
3234 .bp_in_mask = 0xffff,
3235 .hfb_filter_cnt = 16,
3236 .qtag_mask = 0x1F,
3237 .tbuf_offset = 0x0600,
3238 .hfb_offset = 0x1000,
3239 .hfb_reg_offset = 0x2000,
3240 .rdma_offset = 0x3000,
3241 .tdma_offset = 0x4000,
3242 .words_per_bd = 2,
3243 .flags = GENET_HAS_EXT,
3244 },
3245 [GENET_V3] = {
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 = 0x10000,
3259 .tdma_offset = 0x11000,
3260 .words_per_bd = 2,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003261 .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR |
3262 GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003263 },
3264 [GENET_V4] = {
3265 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003266 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003267 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003268 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003269 .bp_in_en_shift = 17,
3270 .bp_in_mask = 0x1ffff,
3271 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003272 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003273 .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,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003280 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3281 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003282 },
Doug Berger42138082017-03-13 17:41:42 -07003283 [GENET_V5] = {
3284 .tx_queues = 4,
3285 .tx_bds_per_q = 32,
3286 .rx_queues = 0,
3287 .rx_bds_per_q = 0,
3288 .bp_in_en_shift = 17,
3289 .bp_in_mask = 0x1ffff,
3290 .hfb_filter_cnt = 48,
3291 .hfb_filter_size = 128,
3292 .qtag_mask = 0x3F,
3293 .tbuf_offset = 0x0600,
3294 .hfb_offset = 0x8000,
3295 .hfb_reg_offset = 0xfc00,
3296 .rdma_offset = 0x2000,
3297 .tdma_offset = 0x4000,
3298 .words_per_bd = 3,
3299 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3300 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
3301 },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003302};
3303
3304/* Infer hardware parameters from the detected GENET version */
3305static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
3306{
3307 struct bcmgenet_hw_params *params;
3308 u32 reg;
3309 u8 major;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003310 u16 gphy_rev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003311
Doug Berger42138082017-03-13 17:41:42 -07003312 if (GENET_IS_V5(priv) || GENET_IS_V4(priv)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003313 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3314 genet_dma_ring_regs = genet_dma_ring_regs_v4;
3315 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003316 } else if (GENET_IS_V3(priv)) {
3317 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3318 genet_dma_ring_regs = genet_dma_ring_regs_v123;
3319 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003320 } else if (GENET_IS_V2(priv)) {
3321 bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
3322 genet_dma_ring_regs = genet_dma_ring_regs_v123;
3323 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003324 } else if (GENET_IS_V1(priv)) {
3325 bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
3326 genet_dma_ring_regs = genet_dma_ring_regs_v123;
3327 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003328 }
3329
3330 /* enum genet_version starts at 1 */
3331 priv->hw_params = &bcmgenet_hw_params[priv->version];
3332 params = priv->hw_params;
3333
3334 /* Read GENET HW version */
3335 reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
3336 major = (reg >> 24 & 0x0f);
Doug Berger42138082017-03-13 17:41:42 -07003337 if (major == 6)
3338 major = 5;
3339 else if (major == 5)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003340 major = 4;
3341 else if (major == 0)
3342 major = 1;
3343 if (major != priv->version) {
3344 dev_err(&priv->pdev->dev,
3345 "GENET version mismatch, got: %d, configured for: %d\n",
3346 major, priv->version);
3347 }
3348
3349 /* Print the GENET core version */
3350 dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003351 major, (reg >> 16) & 0x0f, reg & 0xffff);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003352
Florian Fainelli487320c2014-09-19 13:07:53 -07003353 /* Store the integrated PHY revision for the MDIO probing function
3354 * to pass this information to the PHY driver. The PHY driver expects
3355 * to find the PHY major revision in bits 15:8 while the GENET register
3356 * stores that information in bits 7:0, account for that.
Florian Fainellib04a2f52014-12-03 09:56:59 -08003357 *
3358 * On newer chips, starting with PHY revision G0, a new scheme is
3359 * deployed similar to the Starfighter 2 switch with GPHY major
3360 * revision in bits 15:8 and patch level in bits 7:0. Major revision 0
3361 * is reserved as well as special value 0x01ff, we have a small
3362 * heuristic to check for the new GPHY revision and re-arrange things
3363 * so the GPHY driver is happy.
Florian Fainelli487320c2014-09-19 13:07:53 -07003364 */
Florian Fainellib04a2f52014-12-03 09:56:59 -08003365 gphy_rev = reg & 0xffff;
3366
Doug Berger42138082017-03-13 17:41:42 -07003367 if (GENET_IS_V5(priv)) {
3368 /* The EPHY revision should come from the MDIO registers of
3369 * the PHY not from GENET.
3370 */
3371 if (gphy_rev != 0) {
3372 pr_warn("GENET is reporting EPHY revision: 0x%04x\n",
3373 gphy_rev);
3374 }
Doug Bergereca4bad2017-03-09 16:58:45 -08003375 /* This is reserved so should require special treatment */
David S. Miller101c4312017-03-15 11:59:10 -07003376 } else if (gphy_rev == 0 || gphy_rev == 0x01ff) {
Doug Bergereca4bad2017-03-09 16:58:45 -08003377 pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev);
3378 return;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003379 /* This is the good old scheme, just GPHY major, no minor nor patch */
Doug Berger42138082017-03-13 17:41:42 -07003380 } else if ((gphy_rev & 0xf0) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003381 priv->gphy_rev = gphy_rev << 8;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003382 /* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */
Doug Berger42138082017-03-13 17:41:42 -07003383 } else if ((gphy_rev & 0xff00) != 0) {
Florian Fainellib04a2f52014-12-03 09:56:59 -08003384 priv->gphy_rev = gphy_rev;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003385 }
Florian Fainelli487320c2014-09-19 13:07:53 -07003386
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003387#ifdef CONFIG_PHYS_ADDR_T_64BIT
3388 if (!(params->flags & GENET_HAS_40BITS))
3389 pr_warn("GENET does not support 40-bits PA\n");
3390#endif
3391
3392 pr_debug("Configuration for version: %d\n"
Petri Gynther3feafa02015-03-05 17:40:14 -08003393 "TXq: %1d, TXqBDs: %1d, RXq: %1d, RXqBDs: %1d\n"
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003394 "BP << en: %2d, BP msk: 0x%05x\n"
3395 "HFB count: %2d, QTAQ msk: 0x%05x\n"
3396 "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
3397 "RDMA: 0x%05x, TDMA: 0x%05x\n"
3398 "Words/BD: %d\n",
3399 priv->version,
Petri Gynther51a966a2015-02-23 11:00:46 -08003400 params->tx_queues, params->tx_bds_per_q,
Petri Gynther3feafa02015-03-05 17:40:14 -08003401 params->rx_queues, params->rx_bds_per_q,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003402 params->bp_in_en_shift, params->bp_in_mask,
3403 params->hfb_filter_cnt, params->qtag_mask,
3404 params->tbuf_offset, params->hfb_offset,
3405 params->hfb_reg_offset,
3406 params->rdma_offset, params->tdma_offset,
3407 params->words_per_bd);
3408}
3409
3410static const struct of_device_id bcmgenet_match[] = {
3411 { .compatible = "brcm,genet-v1", .data = (void *)GENET_V1 },
3412 { .compatible = "brcm,genet-v2", .data = (void *)GENET_V2 },
3413 { .compatible = "brcm,genet-v3", .data = (void *)GENET_V3 },
3414 { .compatible = "brcm,genet-v4", .data = (void *)GENET_V4 },
Doug Berger42138082017-03-13 17:41:42 -07003415 { .compatible = "brcm,genet-v5", .data = (void *)GENET_V5 },
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003416 { },
3417};
Luis de Bethencourte8048e52015-09-18 17:55:02 +02003418MODULE_DEVICE_TABLE(of, bcmgenet_match);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003419
3420static int bcmgenet_probe(struct platform_device *pdev)
3421{
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003422 struct bcmgenet_platform_data *pd = pdev->dev.platform_data;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003423 struct device_node *dn = pdev->dev.of_node;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003424 const struct of_device_id *of_id = NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003425 struct bcmgenet_priv *priv;
3426 struct net_device *dev;
3427 const void *macaddr;
3428 struct resource *r;
3429 int err = -EIO;
Doug Berger6be371b2017-03-09 16:58:48 -08003430 const char *phy_mode_str;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003431
Petri Gynther3feafee2015-03-05 17:40:12 -08003432 /* Up to GENET_MAX_MQ_CNT + 1 TX queues and RX queues */
3433 dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1,
3434 GENET_MAX_MQ_CNT + 1);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003435 if (!dev) {
3436 dev_err(&pdev->dev, "can't allocate net device\n");
3437 return -ENOMEM;
3438 }
3439
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003440 if (dn) {
3441 of_id = of_match_node(bcmgenet_match, dn);
3442 if (!of_id)
3443 return -EINVAL;
3444 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003445
3446 priv = netdev_priv(dev);
3447 priv->irq0 = platform_get_irq(pdev, 0);
3448 priv->irq1 = platform_get_irq(pdev, 1);
Florian Fainelli85620562014-07-21 15:29:23 -07003449 priv->wol_irq = platform_get_irq(pdev, 2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003450 if (!priv->irq0 || !priv->irq1) {
3451 dev_err(&pdev->dev, "can't find IRQs\n");
3452 err = -EINVAL;
3453 goto err;
3454 }
3455
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003456 if (dn) {
3457 macaddr = of_get_mac_address(dn);
3458 if (!macaddr) {
3459 dev_err(&pdev->dev, "can't find MAC address\n");
3460 err = -EINVAL;
3461 goto err;
3462 }
3463 } else {
3464 macaddr = pd->mac_address;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003465 }
3466
3467 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam5343a102014-02-24 00:47:24 -03003468 priv->base = devm_ioremap_resource(&pdev->dev, r);
3469 if (IS_ERR(priv->base)) {
3470 err = PTR_ERR(priv->base);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003471 goto err;
3472 }
3473
Doug Berger07c52d62017-03-09 16:58:47 -08003474 spin_lock_init(&priv->lock);
3475
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003476 SET_NETDEV_DEV(dev, &pdev->dev);
3477 dev_set_drvdata(&pdev->dev, dev);
3478 ether_addr_copy(dev->dev_addr, macaddr);
3479 dev->watchdog_timeo = 2 * HZ;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003480 dev->ethtool_ops = &bcmgenet_ethtool_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003481 dev->netdev_ops = &bcmgenet_netdev_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003482
3483 priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
3484
3485 /* Set hardware features */
3486 dev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM |
3487 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
3488
Florian Fainelli85620562014-07-21 15:29:23 -07003489 /* Request the WOL interrupt and advertise suspend if available */
3490 priv->wol_irq_disabled = true;
3491 err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0,
3492 dev->name, priv);
3493 if (!err)
3494 device_set_wakeup_capable(&pdev->dev, 1);
3495
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003496 /* Set the needed headroom to account for any possible
3497 * features enabling/disabling at runtime
3498 */
3499 dev->needed_headroom += 64;
3500
3501 netdev_boot_setup_check(dev);
3502
3503 priv->dev = dev;
3504 priv->pdev = pdev;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003505 if (of_id)
3506 priv->version = (enum bcmgenet_version)of_id->data;
3507 else
3508 priv->version = pd->genet_version;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003509
Florian Fainellie4a60a92014-08-11 14:50:42 -07003510 priv->clk = devm_clk_get(&priv->pdev->dev, "enet");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003511 if (IS_ERR(priv->clk)) {
Florian Fainellie4a60a92014-08-11 14:50:42 -07003512 dev_warn(&priv->pdev->dev, "failed to get enet clock\n");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003513 priv->clk = NULL;
3514 }
Florian Fainellie4a60a92014-08-11 14:50:42 -07003515
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003516 clk_prepare_enable(priv->clk);
Florian Fainellie4a60a92014-08-11 14:50:42 -07003517
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003518 bcmgenet_set_hw_params(priv);
3519
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003520 /* Mii wait queue */
3521 init_waitqueue_head(&priv->wq);
3522 /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
3523 priv->rx_buf_len = RX_BUF_LENGTH;
3524 INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
3525
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003526 priv->clk_wol = devm_clk_get(&priv->pdev->dev, "enet-wol");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003527 if (IS_ERR(priv->clk_wol)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003528 dev_warn(&priv->pdev->dev, "failed to get enet-wol clock\n");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003529 priv->clk_wol = NULL;
3530 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003531
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003532 priv->clk_eee = devm_clk_get(&priv->pdev->dev, "enet-eee");
3533 if (IS_ERR(priv->clk_eee)) {
3534 dev_warn(&priv->pdev->dev, "failed to get enet-eee clock\n");
3535 priv->clk_eee = NULL;
3536 }
3537
Doug Berger6be371b2017-03-09 16:58:48 -08003538 /* If this is an internal GPHY, power it on now, before UniMAC is
3539 * brought out of reset as absolutely no UniMAC activity is allowed
3540 */
3541 if (dn && !of_property_read_string(dn, "phy-mode", &phy_mode_str) &&
3542 !strcasecmp(phy_mode_str, "internal"))
3543 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3544
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003545 err = reset_umac(priv);
3546 if (err)
3547 goto err_clk_disable;
3548
3549 err = bcmgenet_mii_init(dev);
3550 if (err)
3551 goto err_clk_disable;
3552
3553 /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
3554 * just the ring 16 descriptor based TX
3555 */
3556 netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
3557 netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
3558
Florian Fainelli219575e2014-06-26 10:26:21 -07003559 /* libphy will determine the link state */
3560 netif_carrier_off(dev);
3561
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003562 /* Turn off the main clock, WOL clock is handled separately */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003563 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003564
Florian Fainelli0f50ce92014-06-26 10:26:20 -07003565 err = register_netdev(dev);
3566 if (err)
3567 goto err;
3568
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003569 return err;
3570
3571err_clk_disable:
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003572 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003573err:
3574 free_netdev(dev);
3575 return err;
3576}
3577
3578static int bcmgenet_remove(struct platform_device *pdev)
3579{
3580 struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
3581
3582 dev_set_drvdata(&pdev->dev, NULL);
3583 unregister_netdev(priv->dev);
3584 bcmgenet_mii_exit(priv->dev);
3585 free_netdev(priv->dev);
3586
3587 return 0;
3588}
3589
Florian Fainellib6e978e2014-07-21 15:29:22 -07003590#ifdef CONFIG_PM_SLEEP
3591static int bcmgenet_suspend(struct device *d)
3592{
3593 struct net_device *dev = dev_get_drvdata(d);
3594 struct bcmgenet_priv *priv = netdev_priv(dev);
3595 int ret;
3596
3597 if (!netif_running(dev))
3598 return 0;
3599
3600 bcmgenet_netif_stop(dev);
3601
Florian Fainelli0299b6a2016-09-26 22:31:56 +02003602 phy_suspend(priv->phydev);
Florian Fainellicc013fb2014-08-11 14:50:43 -07003603
Florian Fainellib6e978e2014-07-21 15:29:22 -07003604 netif_device_detach(dev);
3605
3606 /* Disable MAC receive */
3607 umac_enable_set(priv, CMD_RX_EN, false);
3608
3609 ret = bcmgenet_dma_teardown(priv);
3610 if (ret)
3611 return ret;
3612
Doug Berger556c2cf2017-03-13 17:41:34 -07003613 /* Disable MAC transmit. TX DMA disabled must be done before this */
Florian Fainellib6e978e2014-07-21 15:29:22 -07003614 umac_enable_set(priv, CMD_TX_EN, false);
3615
3616 /* tx reclaim */
3617 bcmgenet_tx_reclaim_all(dev);
3618 bcmgenet_fini_dma(priv);
3619
Florian Fainelli8c90db72014-07-21 15:29:28 -07003620 /* Prepare the device for Wake-on-LAN and switch to the slow clock */
3621 if (device_may_wakeup(d) && priv->wolopts) {
Florian Fainellica8cf342015-03-23 15:09:51 -07003622 ret = bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003623 clk_prepare_enable(priv->clk_wol);
Florian Fainellic624f892015-07-16 15:51:17 -07003624 } else if (priv->internal_phy) {
Florian Fainellia6f31f52015-03-23 15:09:57 -07003625 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003626 }
3627
Florian Fainellib6e978e2014-07-21 15:29:22 -07003628 /* Turn off the clocks */
3629 clk_disable_unprepare(priv->clk);
3630
Florian Fainellica8cf342015-03-23 15:09:51 -07003631 return ret;
Florian Fainellib6e978e2014-07-21 15:29:22 -07003632}
3633
3634static int bcmgenet_resume(struct device *d)
3635{
3636 struct net_device *dev = dev_get_drvdata(d);
3637 struct bcmgenet_priv *priv = netdev_priv(dev);
3638 unsigned long dma_ctrl;
3639 int ret;
3640 u32 reg;
3641
3642 if (!netif_running(dev))
3643 return 0;
3644
3645 /* Turn on the clock */
3646 ret = clk_prepare_enable(priv->clk);
3647 if (ret)
3648 return ret;
3649
Florian Fainellia6f31f52015-03-23 15:09:57 -07003650 /* If this is an internal GPHY, power it back on now, before UniMAC is
3651 * brought out of reset as absolutely no UniMAC activity is allowed
3652 */
Florian Fainellic624f892015-07-16 15:51:17 -07003653 if (priv->internal_phy)
Florian Fainellia6f31f52015-03-23 15:09:57 -07003654 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3655
Florian Fainellib6e978e2014-07-21 15:29:22 -07003656 bcmgenet_umac_reset(priv);
3657
3658 ret = init_umac(priv);
3659 if (ret)
3660 goto out_clk_disable;
3661
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003662 /* From WOL-enabled suspend, switch to regular clock */
3663 if (priv->wolopts)
3664 clk_disable_unprepare(priv->clk_wol);
3665
Florian Fainelli0299b6a2016-09-26 22:31:56 +02003666 phy_init_hw(priv->phydev);
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003667 /* Speed settings must be restored */
Florian Fainelli28b45912015-07-16 15:51:19 -07003668 bcmgenet_mii_config(priv->dev);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003669
Florian Fainellib6e978e2014-07-21 15:29:22 -07003670 /* disable ethernet MAC while updating its registers */
3671 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
3672
3673 bcmgenet_set_hw_addr(priv, dev->dev_addr);
3674
Florian Fainellic624f892015-07-16 15:51:17 -07003675 if (priv->internal_phy) {
Florian Fainellib6e978e2014-07-21 15:29:22 -07003676 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
3677 reg |= EXT_ENERGY_DET_MASK;
3678 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
3679 }
3680
Florian Fainelli98bb7392014-08-11 14:50:45 -07003681 if (priv->wolopts)
3682 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
3683
Florian Fainellib6e978e2014-07-21 15:29:22 -07003684 /* Disable RX/TX DMA and flush TX queues */
3685 dma_ctrl = bcmgenet_dma_disable(priv);
3686
3687 /* Reinitialize TDMA and RDMA and SW housekeeping */
3688 ret = bcmgenet_init_dma(priv);
3689 if (ret) {
3690 netdev_err(dev, "failed to initialize DMA\n");
3691 goto out_clk_disable;
3692 }
3693
3694 /* Always enable ring 16 - descriptor ring */
3695 bcmgenet_enable_dma(priv, dma_ctrl);
3696
3697 netif_device_attach(dev);
3698
Florian Fainelli0299b6a2016-09-26 22:31:56 +02003699 phy_resume(priv->phydev);
Florian Fainellicc013fb2014-08-11 14:50:43 -07003700
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003701 if (priv->eee.eee_enabled)
3702 bcmgenet_eee_enable_set(dev, true);
3703
Florian Fainellib6e978e2014-07-21 15:29:22 -07003704 bcmgenet_netif_start(dev);
3705
3706 return 0;
3707
3708out_clk_disable:
Doug Berger76274092017-03-09 16:58:46 -08003709 if (priv->internal_phy)
3710 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainellib6e978e2014-07-21 15:29:22 -07003711 clk_disable_unprepare(priv->clk);
3712 return ret;
3713}
3714#endif /* CONFIG_PM_SLEEP */
3715
3716static SIMPLE_DEV_PM_OPS(bcmgenet_pm_ops, bcmgenet_suspend, bcmgenet_resume);
3717
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003718static struct platform_driver bcmgenet_driver = {
3719 .probe = bcmgenet_probe,
3720 .remove = bcmgenet_remove,
3721 .driver = {
3722 .name = "bcmgenet",
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003723 .of_match_table = bcmgenet_match,
Florian Fainellib6e978e2014-07-21 15:29:22 -07003724 .pm = &bcmgenet_pm_ops,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003725 },
3726};
3727module_platform_driver(bcmgenet_driver);
3728
3729MODULE_AUTHOR("Broadcom Corporation");
3730MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
3731MODULE_ALIAS("platform:bcmgenet");
3732MODULE_LICENSE("GPL");