blob: 99f8d90246331b1c8756c00d7dc0378cdabd3cda [file] [log] [blame]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001/*
2 * Broadcom GENET (Gigabit Ethernet) controller driver
3 *
Doug Bergerffff7132017-03-09 16:58:43 -08004 * 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
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200453static int bcmgenet_get_link_ksettings(struct net_device *dev,
454 struct ethtool_link_ksettings *cmd)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200455{
Florian Fainelli0299b6a2016-09-26 22:31:56 +0200456 struct bcmgenet_priv *priv = netdev_priv(dev);
457
Philippe Reynesbac65c42016-07-09 00:54:47 +0200458 if (!netif_running(dev))
459 return -EINVAL;
460
Florian Fainelli0299b6a2016-09-26 22:31:56 +0200461 if (!priv->phydev)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200462 return -ENODEV;
463
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200464 return phy_ethtool_ksettings_get(priv->phydev, cmd);
Philippe Reynesbac65c42016-07-09 00:54:47 +0200465}
466
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200467static int bcmgenet_set_link_ksettings(struct net_device *dev,
468 const struct ethtool_link_ksettings *cmd)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200469{
Florian Fainelli0299b6a2016-09-26 22:31:56 +0200470 struct bcmgenet_priv *priv = netdev_priv(dev);
471
Philippe Reynesbac65c42016-07-09 00:54:47 +0200472 if (!netif_running(dev))
473 return -EINVAL;
474
Florian Fainelli0299b6a2016-09-26 22:31:56 +0200475 if (!priv->phydev)
Philippe Reynesbac65c42016-07-09 00:54:47 +0200476 return -ENODEV;
477
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200478 return phy_ethtool_ksettings_set(priv->phydev, cmd);
Philippe Reynesbac65c42016-07-09 00:54:47 +0200479}
480
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800481static int bcmgenet_set_rx_csum(struct net_device *dev,
482 netdev_features_t wanted)
483{
484 struct bcmgenet_priv *priv = netdev_priv(dev);
485 u32 rbuf_chk_ctrl;
486 bool rx_csum_en;
487
488 rx_csum_en = !!(wanted & NETIF_F_RXCSUM);
489
490 rbuf_chk_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CHK_CTRL);
491
492 /* enable rx checksumming */
493 if (rx_csum_en)
494 rbuf_chk_ctrl |= RBUF_RXCHK_EN;
495 else
496 rbuf_chk_ctrl &= ~RBUF_RXCHK_EN;
497 priv->desc_rxchk_en = rx_csum_en;
Florian Fainelliebe5e3c2014-03-26 21:18:39 -0700498
499 /* If UniMAC forwards CRC, we need to skip over it to get
500 * a valid CHK bit to be set in the per-packet status word
501 */
502 if (rx_csum_en && priv->crc_fwd_en)
503 rbuf_chk_ctrl |= RBUF_SKIP_FCS;
504 else
505 rbuf_chk_ctrl &= ~RBUF_SKIP_FCS;
506
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800507 bcmgenet_rbuf_writel(priv, rbuf_chk_ctrl, RBUF_CHK_CTRL);
508
509 return 0;
510}
511
512static int bcmgenet_set_tx_csum(struct net_device *dev,
513 netdev_features_t wanted)
514{
515 struct bcmgenet_priv *priv = netdev_priv(dev);
516 bool desc_64b_en;
517 u32 tbuf_ctrl, rbuf_ctrl;
518
519 tbuf_ctrl = bcmgenet_tbuf_ctrl_get(priv);
520 rbuf_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
521
522 desc_64b_en = !!(wanted & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
523
524 /* enable 64 bytes descriptor in both directions (RBUF and TBUF) */
525 if (desc_64b_en) {
526 tbuf_ctrl |= RBUF_64B_EN;
527 rbuf_ctrl |= RBUF_64B_EN;
528 } else {
529 tbuf_ctrl &= ~RBUF_64B_EN;
530 rbuf_ctrl &= ~RBUF_64B_EN;
531 }
532 priv->desc_64b_en = desc_64b_en;
533
534 bcmgenet_tbuf_ctrl_set(priv, tbuf_ctrl);
535 bcmgenet_rbuf_writel(priv, rbuf_ctrl, RBUF_CTRL);
536
537 return 0;
538}
539
540static int bcmgenet_set_features(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700541 netdev_features_t features)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800542{
543 netdev_features_t changed = features ^ dev->features;
544 netdev_features_t wanted = dev->wanted_features;
545 int ret = 0;
546
547 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
548 ret = bcmgenet_set_tx_csum(dev, wanted);
549 if (changed & (NETIF_F_RXCSUM))
550 ret = bcmgenet_set_rx_csum(dev, wanted);
551
552 return ret;
553}
554
555static u32 bcmgenet_get_msglevel(struct net_device *dev)
556{
557 struct bcmgenet_priv *priv = netdev_priv(dev);
558
559 return priv->msg_enable;
560}
561
562static void bcmgenet_set_msglevel(struct net_device *dev, u32 level)
563{
564 struct bcmgenet_priv *priv = netdev_priv(dev);
565
566 priv->msg_enable = level;
567}
568
Florian Fainelli2f913072015-09-16 16:47:39 -0700569static int bcmgenet_get_coalesce(struct net_device *dev,
570 struct ethtool_coalesce *ec)
571{
572 struct bcmgenet_priv *priv = netdev_priv(dev);
573
574 ec->tx_max_coalesced_frames =
575 bcmgenet_tdma_ring_readl(priv, DESC_INDEX,
576 DMA_MBUF_DONE_THRESH);
Florian Fainelli4a296452015-09-16 16:47:40 -0700577 ec->rx_max_coalesced_frames =
578 bcmgenet_rdma_ring_readl(priv, DESC_INDEX,
579 DMA_MBUF_DONE_THRESH);
580 ec->rx_coalesce_usecs =
581 bcmgenet_rdma_readl(priv, DMA_RING16_TIMEOUT) * 8192 / 1000;
Florian Fainelli2f913072015-09-16 16:47:39 -0700582
583 return 0;
584}
585
586static int bcmgenet_set_coalesce(struct net_device *dev,
587 struct ethtool_coalesce *ec)
588{
589 struct bcmgenet_priv *priv = netdev_priv(dev);
590 unsigned int i;
Florian Fainelli4a296452015-09-16 16:47:40 -0700591 u32 reg;
Florian Fainelli2f913072015-09-16 16:47:39 -0700592
Florian Fainelli4a296452015-09-16 16:47:40 -0700593 /* Base system clock is 125Mhz, DMA timeout is this reference clock
594 * divided by 1024, which yields roughly 8.192us, our maximum value
595 * has to fit in the DMA_TIMEOUT_MASK (16 bits)
596 */
Florian Fainelli2f913072015-09-16 16:47:39 -0700597 if (ec->tx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
Florian Fainelli4a296452015-09-16 16:47:40 -0700598 ec->tx_max_coalesced_frames == 0 ||
599 ec->rx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
600 ec->rx_coalesce_usecs > (DMA_TIMEOUT_MASK * 8) + 1)
601 return -EINVAL;
602
603 if (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0)
Florian Fainelli2f913072015-09-16 16:47:39 -0700604 return -EINVAL;
605
606 /* GENET TDMA hardware does not support a configurable timeout, but will
607 * always generate an interrupt either after MBDONE packets have been
608 * transmitted, or when the ring is emtpy.
609 */
610 if (ec->tx_coalesce_usecs || ec->tx_coalesce_usecs_high ||
Florian Fainelli852bcaf2015-09-18 14:16:53 -0700611 ec->tx_coalesce_usecs_irq || ec->tx_coalesce_usecs_low)
Florian Fainelli2f913072015-09-16 16:47:39 -0700612 return -EOPNOTSUPP;
613
614 /* Program all TX queues with the same values, as there is no
615 * ethtool knob to do coalescing on a per-queue basis
616 */
617 for (i = 0; i < priv->hw_params->tx_queues; i++)
618 bcmgenet_tdma_ring_writel(priv, i,
619 ec->tx_max_coalesced_frames,
620 DMA_MBUF_DONE_THRESH);
621 bcmgenet_tdma_ring_writel(priv, DESC_INDEX,
622 ec->tx_max_coalesced_frames,
623 DMA_MBUF_DONE_THRESH);
624
Florian Fainelli4a296452015-09-16 16:47:40 -0700625 for (i = 0; i < priv->hw_params->rx_queues; i++) {
626 bcmgenet_rdma_ring_writel(priv, i,
627 ec->rx_max_coalesced_frames,
628 DMA_MBUF_DONE_THRESH);
629
630 reg = bcmgenet_rdma_readl(priv, DMA_RING0_TIMEOUT + i);
631 reg &= ~DMA_TIMEOUT_MASK;
632 reg |= DIV_ROUND_UP(ec->rx_coalesce_usecs * 1000, 8192);
633 bcmgenet_rdma_writel(priv, reg, DMA_RING0_TIMEOUT + i);
634 }
635
636 bcmgenet_rdma_ring_writel(priv, DESC_INDEX,
637 ec->rx_max_coalesced_frames,
638 DMA_MBUF_DONE_THRESH);
639
640 reg = bcmgenet_rdma_readl(priv, DMA_RING16_TIMEOUT);
641 reg &= ~DMA_TIMEOUT_MASK;
642 reg |= DIV_ROUND_UP(ec->rx_coalesce_usecs * 1000, 8192);
643 bcmgenet_rdma_writel(priv, reg, DMA_RING16_TIMEOUT);
644
Florian Fainelli2f913072015-09-16 16:47:39 -0700645 return 0;
646}
647
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800648/* standard ethtool support functions. */
649enum bcmgenet_stat_type {
650 BCMGENET_STAT_NETDEV = -1,
651 BCMGENET_STAT_MIB_RX,
652 BCMGENET_STAT_MIB_TX,
653 BCMGENET_STAT_RUNT,
654 BCMGENET_STAT_MISC,
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800655 BCMGENET_STAT_SOFT,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800656};
657
658struct bcmgenet_stats {
659 char stat_string[ETH_GSTRING_LEN];
660 int stat_sizeof;
661 int stat_offset;
662 enum bcmgenet_stat_type type;
663 /* reg offset from UMAC base for misc counters */
664 u16 reg_offset;
665};
666
667#define STAT_NETDEV(m) { \
668 .stat_string = __stringify(m), \
669 .stat_sizeof = sizeof(((struct net_device_stats *)0)->m), \
670 .stat_offset = offsetof(struct net_device_stats, m), \
671 .type = BCMGENET_STAT_NETDEV, \
672}
673
674#define STAT_GENET_MIB(str, m, _type) { \
675 .stat_string = str, \
676 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
677 .stat_offset = offsetof(struct bcmgenet_priv, m), \
678 .type = _type, \
679}
680
681#define STAT_GENET_MIB_RX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_RX)
682#define STAT_GENET_MIB_TX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_TX)
683#define STAT_GENET_RUNT(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_RUNT)
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800684#define STAT_GENET_SOFT_MIB(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_SOFT)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800685
686#define STAT_GENET_MISC(str, m, offset) { \
687 .stat_string = str, \
688 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
689 .stat_offset = offsetof(struct bcmgenet_priv, m), \
690 .type = BCMGENET_STAT_MISC, \
691 .reg_offset = offset, \
692}
693
694
695/* There is a 0xC gap between the end of RX and beginning of TX stats and then
696 * between the end of TX stats and the beginning of the RX RUNT
697 */
698#define BCMGENET_STAT_OFFSET 0xc
699
700/* Hardware counters must be kept in sync because the order/offset
701 * is important here (order in structure declaration = order in hardware)
702 */
703static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
704 /* general stats */
705 STAT_NETDEV(rx_packets),
706 STAT_NETDEV(tx_packets),
707 STAT_NETDEV(rx_bytes),
708 STAT_NETDEV(tx_bytes),
709 STAT_NETDEV(rx_errors),
710 STAT_NETDEV(tx_errors),
711 STAT_NETDEV(rx_dropped),
712 STAT_NETDEV(tx_dropped),
713 STAT_NETDEV(multicast),
714 /* UniMAC RSV counters */
715 STAT_GENET_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
716 STAT_GENET_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
717 STAT_GENET_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
718 STAT_GENET_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
719 STAT_GENET_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
720 STAT_GENET_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
721 STAT_GENET_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
722 STAT_GENET_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
723 STAT_GENET_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
724 STAT_GENET_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
725 STAT_GENET_MIB_RX("rx_pkts", mib.rx.pkt),
726 STAT_GENET_MIB_RX("rx_bytes", mib.rx.bytes),
727 STAT_GENET_MIB_RX("rx_multicast", mib.rx.mca),
728 STAT_GENET_MIB_RX("rx_broadcast", mib.rx.bca),
729 STAT_GENET_MIB_RX("rx_fcs", mib.rx.fcs),
730 STAT_GENET_MIB_RX("rx_control", mib.rx.cf),
731 STAT_GENET_MIB_RX("rx_pause", mib.rx.pf),
732 STAT_GENET_MIB_RX("rx_unknown", mib.rx.uo),
733 STAT_GENET_MIB_RX("rx_align", mib.rx.aln),
734 STAT_GENET_MIB_RX("rx_outrange", mib.rx.flr),
735 STAT_GENET_MIB_RX("rx_code", mib.rx.cde),
736 STAT_GENET_MIB_RX("rx_carrier", mib.rx.fcr),
737 STAT_GENET_MIB_RX("rx_oversize", mib.rx.ovr),
738 STAT_GENET_MIB_RX("rx_jabber", mib.rx.jbr),
739 STAT_GENET_MIB_RX("rx_mtu_err", mib.rx.mtue),
740 STAT_GENET_MIB_RX("rx_good_pkts", mib.rx.pok),
741 STAT_GENET_MIB_RX("rx_unicast", mib.rx.uc),
742 STAT_GENET_MIB_RX("rx_ppp", mib.rx.ppp),
743 STAT_GENET_MIB_RX("rx_crc", mib.rx.rcrc),
744 /* UniMAC TSV counters */
745 STAT_GENET_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
746 STAT_GENET_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
747 STAT_GENET_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
748 STAT_GENET_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
749 STAT_GENET_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
750 STAT_GENET_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
751 STAT_GENET_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
752 STAT_GENET_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
753 STAT_GENET_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
754 STAT_GENET_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
755 STAT_GENET_MIB_TX("tx_pkts", mib.tx.pkts),
756 STAT_GENET_MIB_TX("tx_multicast", mib.tx.mca),
757 STAT_GENET_MIB_TX("tx_broadcast", mib.tx.bca),
758 STAT_GENET_MIB_TX("tx_pause", mib.tx.pf),
759 STAT_GENET_MIB_TX("tx_control", mib.tx.cf),
760 STAT_GENET_MIB_TX("tx_fcs_err", mib.tx.fcs),
761 STAT_GENET_MIB_TX("tx_oversize", mib.tx.ovr),
762 STAT_GENET_MIB_TX("tx_defer", mib.tx.drf),
763 STAT_GENET_MIB_TX("tx_excess_defer", mib.tx.edf),
764 STAT_GENET_MIB_TX("tx_single_col", mib.tx.scl),
765 STAT_GENET_MIB_TX("tx_multi_col", mib.tx.mcl),
766 STAT_GENET_MIB_TX("tx_late_col", mib.tx.lcl),
767 STAT_GENET_MIB_TX("tx_excess_col", mib.tx.ecl),
768 STAT_GENET_MIB_TX("tx_frags", mib.tx.frg),
769 STAT_GENET_MIB_TX("tx_total_col", mib.tx.ncl),
770 STAT_GENET_MIB_TX("tx_jabber", mib.tx.jbr),
771 STAT_GENET_MIB_TX("tx_bytes", mib.tx.bytes),
772 STAT_GENET_MIB_TX("tx_good_pkts", mib.tx.pok),
773 STAT_GENET_MIB_TX("tx_unicast", mib.tx.uc),
774 /* UniMAC RUNT counters */
775 STAT_GENET_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
776 STAT_GENET_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
777 STAT_GENET_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
778 STAT_GENET_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
779 /* Misc UniMAC counters */
780 STAT_GENET_MISC("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt,
Doug Bergerffff7132017-03-09 16:58:43 -0800781 UMAC_RBUF_OVFL_CNT_V1),
782 STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt,
783 UMAC_RBUF_ERR_CNT_V1),
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800784 STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT),
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800785 STAT_GENET_SOFT_MIB("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
786 STAT_GENET_SOFT_MIB("rx_dma_failed", mib.rx_dma_failed),
787 STAT_GENET_SOFT_MIB("tx_dma_failed", mib.tx_dma_failed),
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800788};
789
790#define BCMGENET_STATS_LEN ARRAY_SIZE(bcmgenet_gstrings_stats)
791
792static void bcmgenet_get_drvinfo(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700793 struct ethtool_drvinfo *info)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800794{
795 strlcpy(info->driver, "bcmgenet", sizeof(info->driver));
796 strlcpy(info->version, "v2.0", sizeof(info->version));
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800797}
798
799static int bcmgenet_get_sset_count(struct net_device *dev, int string_set)
800{
801 switch (string_set) {
802 case ETH_SS_STATS:
803 return BCMGENET_STATS_LEN;
804 default:
805 return -EOPNOTSUPP;
806 }
807}
808
Florian Fainellic91b7f62014-07-23 10:42:12 -0700809static void bcmgenet_get_strings(struct net_device *dev, u32 stringset,
810 u8 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800811{
812 int i;
813
814 switch (stringset) {
815 case ETH_SS_STATS:
816 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
817 memcpy(data + i * ETH_GSTRING_LEN,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700818 bcmgenet_gstrings_stats[i].stat_string,
819 ETH_GSTRING_LEN);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800820 }
821 break;
822 }
823}
824
Doug Bergerffff7132017-03-09 16:58:43 -0800825static u32 bcmgenet_update_stat_misc(struct bcmgenet_priv *priv, u16 offset)
826{
827 u16 new_offset;
828 u32 val;
829
830 switch (offset) {
831 case UMAC_RBUF_OVFL_CNT_V1:
832 if (GENET_IS_V2(priv))
833 new_offset = RBUF_OVFL_CNT_V2;
834 else
835 new_offset = RBUF_OVFL_CNT_V3PLUS;
836
837 val = bcmgenet_rbuf_readl(priv, new_offset);
838 /* clear if overflowed */
839 if (val == ~0)
840 bcmgenet_rbuf_writel(priv, 0, new_offset);
841 break;
842 case UMAC_RBUF_ERR_CNT_V1:
843 if (GENET_IS_V2(priv))
844 new_offset = RBUF_ERR_CNT_V2;
845 else
846 new_offset = RBUF_ERR_CNT_V3PLUS;
847
848 val = bcmgenet_rbuf_readl(priv, new_offset);
849 /* clear if overflowed */
850 if (val == ~0)
851 bcmgenet_rbuf_writel(priv, 0, new_offset);
852 break;
853 default:
854 val = bcmgenet_umac_readl(priv, offset);
855 /* clear if overflowed */
856 if (val == ~0)
857 bcmgenet_umac_writel(priv, 0, offset);
858 break;
859 }
860
861 return val;
862}
863
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800864static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv)
865{
866 int i, j = 0;
867
868 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
869 const struct bcmgenet_stats *s;
870 u8 offset = 0;
871 u32 val = 0;
872 char *p;
873
874 s = &bcmgenet_gstrings_stats[i];
875 switch (s->type) {
876 case BCMGENET_STAT_NETDEV:
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800877 case BCMGENET_STAT_SOFT:
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800878 continue;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800879 case BCMGENET_STAT_RUNT:
Doug Berger1ad3d222017-03-09 16:58:44 -0800880 offset += BCMGENET_STAT_OFFSET;
881 /* fall through */
882 case BCMGENET_STAT_MIB_TX:
883 offset += BCMGENET_STAT_OFFSET;
884 /* fall through */
885 case BCMGENET_STAT_MIB_RX:
Florian Fainellic91b7f62014-07-23 10:42:12 -0700886 val = bcmgenet_umac_readl(priv,
887 UMAC_MIB_START + j + offset);
Doug Berger1ad3d222017-03-09 16:58:44 -0800888 offset = 0; /* Reset Offset */
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800889 break;
890 case BCMGENET_STAT_MISC:
Doug Bergerffff7132017-03-09 16:58:43 -0800891 if (GENET_IS_V1(priv)) {
892 val = bcmgenet_umac_readl(priv, s->reg_offset);
893 /* clear if overflowed */
894 if (val == ~0)
895 bcmgenet_umac_writel(priv, 0,
896 s->reg_offset);
897 } else {
898 val = bcmgenet_update_stat_misc(priv,
899 s->reg_offset);
900 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800901 break;
902 }
903
904 j += s->stat_sizeof;
905 p = (char *)priv + s->stat_offset;
906 *(u32 *)p = val;
907 }
908}
909
910static void bcmgenet_get_ethtool_stats(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700911 struct ethtool_stats *stats,
912 u64 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800913{
914 struct bcmgenet_priv *priv = netdev_priv(dev);
915 int i;
916
917 if (netif_running(dev))
918 bcmgenet_update_mib_counters(priv);
919
920 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
921 const struct bcmgenet_stats *s;
922 char *p;
923
924 s = &bcmgenet_gstrings_stats[i];
925 if (s->type == BCMGENET_STAT_NETDEV)
926 p = (char *)&dev->stats;
927 else
928 p = (char *)priv;
929 p += s->stat_offset;
Eric Dumazet6517eb52016-04-15 10:47:52 -0700930 if (sizeof(unsigned long) != sizeof(u32) &&
931 s->stat_sizeof == sizeof(unsigned long))
932 data[i] = *(unsigned long *)p;
933 else
934 data[i] = *(u32 *)p;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800935 }
936}
937
Florian Fainelli6ef398e2014-11-25 21:16:35 -0800938static void bcmgenet_eee_enable_set(struct net_device *dev, bool enable)
939{
940 struct bcmgenet_priv *priv = netdev_priv(dev);
941 u32 off = priv->hw_params->tbuf_offset + TBUF_ENERGY_CTRL;
942 u32 reg;
943
944 if (enable && !priv->clk_eee_enabled) {
945 clk_prepare_enable(priv->clk_eee);
946 priv->clk_eee_enabled = true;
947 }
948
949 reg = bcmgenet_umac_readl(priv, UMAC_EEE_CTRL);
950 if (enable)
951 reg |= EEE_EN;
952 else
953 reg &= ~EEE_EN;
954 bcmgenet_umac_writel(priv, reg, UMAC_EEE_CTRL);
955
956 /* Enable EEE and switch to a 27Mhz clock automatically */
957 reg = __raw_readl(priv->base + off);
958 if (enable)
959 reg |= TBUF_EEE_EN | TBUF_PM_EN;
960 else
961 reg &= ~(TBUF_EEE_EN | TBUF_PM_EN);
962 __raw_writel(reg, priv->base + off);
963
964 /* Do the same for thing for RBUF */
965 reg = bcmgenet_rbuf_readl(priv, RBUF_ENERGY_CTRL);
966 if (enable)
967 reg |= RBUF_EEE_EN | RBUF_PM_EN;
968 else
969 reg &= ~(RBUF_EEE_EN | RBUF_PM_EN);
970 bcmgenet_rbuf_writel(priv, reg, RBUF_ENERGY_CTRL);
971
972 if (!enable && priv->clk_eee_enabled) {
973 clk_disable_unprepare(priv->clk_eee);
974 priv->clk_eee_enabled = false;
975 }
976
977 priv->eee.eee_enabled = enable;
978 priv->eee.eee_active = enable;
979}
980
981static int bcmgenet_get_eee(struct net_device *dev, struct ethtool_eee *e)
982{
983 struct bcmgenet_priv *priv = netdev_priv(dev);
984 struct ethtool_eee *p = &priv->eee;
985
986 if (GENET_IS_V1(priv))
987 return -EOPNOTSUPP;
988
989 e->eee_enabled = p->eee_enabled;
990 e->eee_active = p->eee_active;
991 e->tx_lpi_timer = bcmgenet_umac_readl(priv, UMAC_EEE_LPI_TIMER);
992
Florian Fainelli0299b6a2016-09-26 22:31:56 +0200993 return phy_ethtool_get_eee(priv->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -0800994}
995
996static int bcmgenet_set_eee(struct net_device *dev, struct ethtool_eee *e)
997{
998 struct bcmgenet_priv *priv = netdev_priv(dev);
999 struct ethtool_eee *p = &priv->eee;
1000 int ret = 0;
1001
1002 if (GENET_IS_V1(priv))
1003 return -EOPNOTSUPP;
1004
1005 p->eee_enabled = e->eee_enabled;
1006
1007 if (!p->eee_enabled) {
1008 bcmgenet_eee_enable_set(dev, false);
1009 } else {
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001010 ret = phy_init_eee(priv->phydev, 0);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001011 if (ret) {
1012 netif_err(priv, hw, dev, "EEE initialization failed\n");
1013 return ret;
1014 }
1015
1016 bcmgenet_umac_writel(priv, e->tx_lpi_timer, UMAC_EEE_LPI_TIMER);
1017 bcmgenet_eee_enable_set(dev, true);
1018 }
1019
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001020 return phy_ethtool_set_eee(priv->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001021}
1022
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001023/* standard ethtool support functions. */
Julia Lawall70591ab2016-08-31 09:30:45 +02001024static const struct ethtool_ops bcmgenet_ethtool_ops = {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001025 .get_strings = bcmgenet_get_strings,
1026 .get_sset_count = bcmgenet_get_sset_count,
1027 .get_ethtool_stats = bcmgenet_get_ethtool_stats,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001028 .get_drvinfo = bcmgenet_get_drvinfo,
1029 .get_link = ethtool_op_get_link,
1030 .get_msglevel = bcmgenet_get_msglevel,
1031 .set_msglevel = bcmgenet_set_msglevel,
Florian Fainelli06ba8372014-07-21 15:29:29 -07001032 .get_wol = bcmgenet_get_wol,
1033 .set_wol = bcmgenet_set_wol,
Florian Fainelli6ef398e2014-11-25 21:16:35 -08001034 .get_eee = bcmgenet_get_eee,
1035 .set_eee = bcmgenet_set_eee,
Florian Fainelli016e7702016-11-15 10:06:38 -08001036 .nway_reset = phy_ethtool_nway_reset,
Florian Fainelli2f913072015-09-16 16:47:39 -07001037 .get_coalesce = bcmgenet_get_coalesce,
1038 .set_coalesce = bcmgenet_set_coalesce,
Philippe Reynesfa92bf02016-09-26 22:31:57 +02001039 .get_link_ksettings = bcmgenet_get_link_ksettings,
1040 .set_link_ksettings = bcmgenet_set_link_ksettings,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001041};
1042
1043/* Power down the unimac, based on mode. */
Florian Fainellica8cf342015-03-23 15:09:51 -07001044static int bcmgenet_power_down(struct bcmgenet_priv *priv,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001045 enum bcmgenet_power_mode mode)
1046{
Florian Fainellica8cf342015-03-23 15:09:51 -07001047 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001048 u32 reg;
1049
1050 switch (mode) {
1051 case GENET_POWER_CABLE_SENSE:
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001052 phy_detach(priv->phydev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001053 break;
1054
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001055 case GENET_POWER_WOL_MAGIC:
Florian Fainellica8cf342015-03-23 15:09:51 -07001056 ret = bcmgenet_wol_power_down_cfg(priv, mode);
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001057 break;
1058
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001059 case GENET_POWER_PASSIVE:
1060 /* Power down LED */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001061 if (priv->hw_params->flags & GENET_HAS_EXT) {
1062 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
1063 reg |= (EXT_PWR_DOWN_PHY |
1064 EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
1065 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
Florian Fainellia642c4f2015-03-23 15:09:56 -07001066
1067 bcmgenet_phy_power_set(priv->dev, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001068 }
1069 break;
1070 default:
1071 break;
1072 }
Florian Fainellica8cf342015-03-23 15:09:51 -07001073
1074 return 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001075}
1076
1077static void bcmgenet_power_up(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001078 enum bcmgenet_power_mode mode)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001079{
1080 u32 reg;
1081
1082 if (!(priv->hw_params->flags & GENET_HAS_EXT))
1083 return;
1084
1085 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
1086
1087 switch (mode) {
1088 case GENET_POWER_PASSIVE:
1089 reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_PHY |
1090 EXT_PWR_DOWN_BIAS);
1091 /* fallthrough */
1092 case GENET_POWER_CABLE_SENSE:
1093 /* enable APD */
1094 reg |= EXT_PWR_DN_EN_LD;
1095 break;
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001096 case GENET_POWER_WOL_MAGIC:
1097 bcmgenet_wol_power_up_cfg(priv, mode);
1098 return;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001099 default:
1100 break;
1101 }
1102
1103 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
Florian Fainelli5dbebbb2015-10-29 18:11:35 -07001104 if (mode == GENET_POWER_PASSIVE) {
Florian Fainellibd4060a2015-07-16 15:51:16 -07001105 bcmgenet_phy_power_set(priv->dev, true);
Florian Fainelli5dbebbb2015-10-29 18:11:35 -07001106 bcmgenet_mii_reset(priv->dev);
1107 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001108}
1109
1110/* ioctl handle special commands that are not present in ethtool. */
1111static int bcmgenet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1112{
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001113 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001114 int val = 0;
1115
1116 if (!netif_running(dev))
1117 return -EINVAL;
1118
1119 switch (cmd) {
1120 case SIOCGMIIPHY:
1121 case SIOCGMIIREG:
1122 case SIOCSMIIREG:
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001123 if (!priv->phydev)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001124 val = -ENODEV;
1125 else
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001126 val = phy_mii_ioctl(priv->phydev, rq, cmd);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001127 break;
1128
1129 default:
1130 val = -EINVAL;
1131 break;
1132 }
1133
1134 return val;
1135}
1136
1137static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv,
1138 struct bcmgenet_tx_ring *ring)
1139{
1140 struct enet_cb *tx_cb_ptr;
1141
1142 tx_cb_ptr = ring->cbs;
1143 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
Petri Gynther014012a2015-02-23 11:00:45 -08001144
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001145 /* Advancing local write pointer */
1146 if (ring->write_ptr == ring->end_ptr)
1147 ring->write_ptr = ring->cb_ptr;
1148 else
1149 ring->write_ptr++;
1150
1151 return tx_cb_ptr;
1152}
1153
1154/* Simple helper to free a control block's resources */
1155static void bcmgenet_free_cb(struct enet_cb *cb)
1156{
1157 dev_kfree_skb_any(cb->skb);
1158 cb->skb = NULL;
1159 dma_unmap_addr_set(cb, dma_addr, 0);
1160}
1161
Petri Gynther4055eae2015-03-25 12:35:16 -07001162static inline void bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring *ring)
1163{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001164 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001165 INTRL2_CPU_MASK_SET);
1166}
1167
1168static inline void bcmgenet_rx_ring16_int_enable(struct bcmgenet_rx_ring *ring)
1169{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001170 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001171 INTRL2_CPU_MASK_CLEAR);
1172}
1173
1174static inline void bcmgenet_rx_ring_int_disable(struct bcmgenet_rx_ring *ring)
1175{
1176 bcmgenet_intrl2_1_writel(ring->priv,
1177 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1178 INTRL2_CPU_MASK_SET);
1179}
1180
1181static inline void bcmgenet_rx_ring_int_enable(struct bcmgenet_rx_ring *ring)
1182{
1183 bcmgenet_intrl2_1_writel(ring->priv,
1184 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1185 INTRL2_CPU_MASK_CLEAR);
1186}
1187
Petri Gynther9dbac282015-03-25 12:35:10 -07001188static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001189{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001190 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001191 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001192}
1193
Petri Gynther9dbac282015-03-25 12:35:10 -07001194static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001195{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001196 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001197 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001198}
1199
Petri Gynther9dbac282015-03-25 12:35:10 -07001200static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001201{
Petri Gynther9dbac282015-03-25 12:35:10 -07001202 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001203 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001204}
1205
Petri Gynther9dbac282015-03-25 12:35:10 -07001206static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001207{
Petri Gynther9dbac282015-03-25 12:35:10 -07001208 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001209 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001210}
1211
1212/* Unlocked version of the reclaim routine */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001213static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
1214 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001215{
1216 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001217 struct device *kdev = &priv->pdev->dev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001218 struct enet_cb *tx_cb_ptr;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001219 struct netdev_queue *txq;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001220 unsigned int pkts_compl = 0;
Petri Gynther55868122016-03-24 11:27:20 -07001221 unsigned int bytes_compl = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001222 unsigned int c_index;
Petri Gynther66d06752015-03-04 14:30:01 -08001223 unsigned int txbds_ready;
1224 unsigned int txbds_processed = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001225
Brian Norris7fc527f2014-07-29 14:34:14 -07001226 /* Compute how many buffers are transmitted since last xmit call */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001227 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
Petri Gynther66d06752015-03-04 14:30:01 -08001228 c_index &= DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001229
Petri Gynther66d06752015-03-04 14:30:01 -08001230 if (likely(c_index >= ring->c_index))
1231 txbds_ready = c_index - ring->c_index;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001232 else
Petri Gynther66d06752015-03-04 14:30:01 -08001233 txbds_ready = (DMA_C_INDEX_MASK + 1) - ring->c_index + c_index;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001234
1235 netif_dbg(priv, tx_done, dev,
Petri Gynther66d06752015-03-04 14:30:01 -08001236 "%s ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
1237 __func__, ring->index, ring->c_index, c_index, txbds_ready);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001238
1239 /* Reclaim transmitted buffers */
Petri Gynther66d06752015-03-04 14:30:01 -08001240 while (txbds_processed < txbds_ready) {
1241 tx_cb_ptr = &priv->tx_cbs[ring->clean_ptr];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001242 if (tx_cb_ptr->skb) {
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001243 pkts_compl++;
Petri Gynther55868122016-03-24 11:27:20 -07001244 bytes_compl += GENET_CB(tx_cb_ptr->skb)->bytes_sent;
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001245 dma_unmap_single(kdev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001246 dma_unmap_addr(tx_cb_ptr, dma_addr),
Eric Dumazeteee57722016-03-17 11:57:06 -07001247 dma_unmap_len(tx_cb_ptr, dma_len),
Florian Fainellic91b7f62014-07-23 10:42:12 -07001248 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001249 bcmgenet_free_cb(tx_cb_ptr);
1250 } else if (dma_unmap_addr(tx_cb_ptr, dma_addr)) {
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001251 dma_unmap_page(kdev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001252 dma_unmap_addr(tx_cb_ptr, dma_addr),
1253 dma_unmap_len(tx_cb_ptr, dma_len),
1254 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001255 dma_unmap_addr_set(tx_cb_ptr, dma_addr, 0);
1256 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001257
Petri Gynther66d06752015-03-04 14:30:01 -08001258 txbds_processed++;
1259 if (likely(ring->clean_ptr < ring->end_ptr))
1260 ring->clean_ptr++;
1261 else
1262 ring->clean_ptr = ring->cb_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001263 }
1264
Petri Gynther66d06752015-03-04 14:30:01 -08001265 ring->free_bds += txbds_processed;
1266 ring->c_index = (ring->c_index + txbds_processed) & DMA_C_INDEX_MASK;
1267
Petri Gynther55868122016-03-24 11:27:20 -07001268 dev->stats.tx_packets += pkts_compl;
1269 dev->stats.tx_bytes += bytes_compl;
1270
Petri Gynthere178c8c2016-04-09 00:20:36 -07001271 txq = netdev_get_tx_queue(dev, ring->queue);
1272 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
1273
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001274 if (ring->free_bds > (MAX_SKB_FRAGS + 1)) {
1275 if (netif_tx_queue_stopped(txq))
1276 netif_tx_wake_queue(txq);
1277 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001278
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001279 return pkts_compl;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001280}
1281
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001282static unsigned int bcmgenet_tx_reclaim(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001283 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001284{
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001285 unsigned int released;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001286 unsigned long flags;
1287
1288 spin_lock_irqsave(&ring->lock, flags);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001289 released = __bcmgenet_tx_reclaim(dev, ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001290 spin_unlock_irqrestore(&ring->lock, flags);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001291
1292 return released;
1293}
1294
1295static int bcmgenet_tx_poll(struct napi_struct *napi, int budget)
1296{
1297 struct bcmgenet_tx_ring *ring =
1298 container_of(napi, struct bcmgenet_tx_ring, napi);
1299 unsigned int work_done = 0;
1300
1301 work_done = bcmgenet_tx_reclaim(ring->priv->dev, ring);
1302
1303 if (work_done == 0) {
1304 napi_complete(napi);
Petri Gynther9dbac282015-03-25 12:35:10 -07001305 ring->int_enable(ring);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001306
1307 return 0;
1308 }
1309
1310 return budget;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001311}
1312
1313static void bcmgenet_tx_reclaim_all(struct net_device *dev)
1314{
1315 struct bcmgenet_priv *priv = netdev_priv(dev);
1316 int i;
1317
1318 if (netif_is_multiqueue(dev)) {
1319 for (i = 0; i < priv->hw_params->tx_queues; i++)
1320 bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
1321 }
1322
1323 bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
1324}
1325
1326/* Transmits a single SKB (either head of a fragment or a single SKB)
1327 * caller must hold priv->lock
1328 */
1329static int bcmgenet_xmit_single(struct net_device *dev,
1330 struct sk_buff *skb,
1331 u16 dma_desc_flags,
1332 struct bcmgenet_tx_ring *ring)
1333{
1334 struct bcmgenet_priv *priv = netdev_priv(dev);
1335 struct device *kdev = &priv->pdev->dev;
1336 struct enet_cb *tx_cb_ptr;
1337 unsigned int skb_len;
1338 dma_addr_t mapping;
1339 u32 length_status;
1340 int ret;
1341
1342 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
1343
1344 if (unlikely(!tx_cb_ptr))
1345 BUG();
1346
1347 tx_cb_ptr->skb = skb;
1348
Petri Gynther7dd39912016-03-24 11:27:21 -07001349 skb_len = skb_headlen(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001350
1351 mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
1352 ret = dma_mapping_error(kdev, mapping);
1353 if (ret) {
Florian Fainelli44c8bc32014-11-19 10:29:56 -08001354 priv->mib.tx_dma_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001355 netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
1356 dev_kfree_skb(skb);
1357 return ret;
1358 }
1359
1360 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
Eric Dumazeteee57722016-03-17 11:57:06 -07001361 dma_unmap_len_set(tx_cb_ptr, dma_len, skb_len);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001362 length_status = (skb_len << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
1363 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT) |
1364 DMA_TX_APPEND_CRC;
1365
1366 if (skb->ip_summed == CHECKSUM_PARTIAL)
1367 length_status |= DMA_TX_DO_CSUM;
1368
1369 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, length_status);
1370
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001371 return 0;
1372}
1373
Brian Norris7fc527f2014-07-29 14:34:14 -07001374/* Transmit a SKB fragment */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001375static int bcmgenet_xmit_frag(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001376 skb_frag_t *frag,
1377 u16 dma_desc_flags,
1378 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001379{
1380 struct bcmgenet_priv *priv = netdev_priv(dev);
1381 struct device *kdev = &priv->pdev->dev;
1382 struct enet_cb *tx_cb_ptr;
Petri Gynther824ba602016-04-05 14:00:00 -07001383 unsigned int frag_size;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001384 dma_addr_t mapping;
1385 int ret;
1386
1387 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
1388
1389 if (unlikely(!tx_cb_ptr))
1390 BUG();
Petri Gynther824ba602016-04-05 14:00:00 -07001391
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001392 tx_cb_ptr->skb = NULL;
1393
Petri Gynther824ba602016-04-05 14:00:00 -07001394 frag_size = skb_frag_size(frag);
1395
1396 mapping = skb_frag_dma_map(kdev, frag, 0, frag_size, DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001397 ret = dma_mapping_error(kdev, mapping);
1398 if (ret) {
Florian Fainelli44c8bc32014-11-19 10:29:56 -08001399 priv->mib.tx_dma_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001400 netif_err(priv, tx_err, dev, "%s: Tx DMA map failed\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001401 __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001402 return ret;
1403 }
1404
1405 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
Petri Gynther824ba602016-04-05 14:00:00 -07001406 dma_unmap_len_set(tx_cb_ptr, dma_len, frag_size);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001407
1408 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping,
Petri Gynther824ba602016-04-05 14:00:00 -07001409 (frag_size << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
Florian Fainellic91b7f62014-07-23 10:42:12 -07001410 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001411
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001412 return 0;
1413}
1414
1415/* Reallocate the SKB to put enough headroom in front of it and insert
1416 * the transmit checksum offsets in the descriptors
1417 */
Petri Gyntherbc233332014-10-01 11:30:01 -07001418static struct sk_buff *bcmgenet_put_tx_csum(struct net_device *dev,
1419 struct sk_buff *skb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001420{
1421 struct status_64 *status = NULL;
1422 struct sk_buff *new_skb;
1423 u16 offset;
1424 u8 ip_proto;
1425 u16 ip_ver;
1426 u32 tx_csum_info;
1427
1428 if (unlikely(skb_headroom(skb) < sizeof(*status))) {
1429 /* If 64 byte status block enabled, must make sure skb has
1430 * enough headroom for us to insert 64B status block.
1431 */
1432 new_skb = skb_realloc_headroom(skb, sizeof(*status));
1433 dev_kfree_skb(skb);
1434 if (!new_skb) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001435 dev->stats.tx_dropped++;
Petri Gyntherbc233332014-10-01 11:30:01 -07001436 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001437 }
1438 skb = new_skb;
1439 }
1440
1441 skb_push(skb, sizeof(*status));
1442 status = (struct status_64 *)skb->data;
1443
1444 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1445 ip_ver = htons(skb->protocol);
1446 switch (ip_ver) {
1447 case ETH_P_IP:
1448 ip_proto = ip_hdr(skb)->protocol;
1449 break;
1450 case ETH_P_IPV6:
1451 ip_proto = ipv6_hdr(skb)->nexthdr;
1452 break;
1453 default:
Petri Gyntherbc233332014-10-01 11:30:01 -07001454 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001455 }
1456
1457 offset = skb_checksum_start_offset(skb) - sizeof(*status);
1458 tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
1459 (offset + skb->csum_offset);
1460
1461 /* Set the length valid bit for TCP and UDP and just set
1462 * the special UDP flag for IPv4, else just set to 0.
1463 */
1464 if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
1465 tx_csum_info |= STATUS_TX_CSUM_LV;
1466 if (ip_proto == IPPROTO_UDP && ip_ver == ETH_P_IP)
1467 tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
Florian Fainelli8900ea572014-07-23 10:42:14 -07001468 } else {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001469 tx_csum_info = 0;
Florian Fainelli8900ea572014-07-23 10:42:14 -07001470 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001471
1472 status->tx_csum_info = tx_csum_info;
1473 }
1474
Petri Gyntherbc233332014-10-01 11:30:01 -07001475 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001476}
1477
1478static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
1479{
1480 struct bcmgenet_priv *priv = netdev_priv(dev);
1481 struct bcmgenet_tx_ring *ring = NULL;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001482 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001483 unsigned long flags = 0;
1484 int nr_frags, index;
1485 u16 dma_desc_flags;
1486 int ret;
1487 int i;
1488
1489 index = skb_get_queue_mapping(skb);
1490 /* Mapping strategy:
1491 * queue_mapping = 0, unclassified, packet xmited through ring16
1492 * queue_mapping = 1, goes to ring 0. (highest priority queue
1493 * queue_mapping = 2, goes to ring 1.
1494 * queue_mapping = 3, goes to ring 2.
1495 * queue_mapping = 4, goes to ring 3.
1496 */
1497 if (index == 0)
1498 index = DESC_INDEX;
1499 else
1500 index -= 1;
1501
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001502 ring = &priv->tx_rings[index];
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001503 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001504
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07001505 nr_frags = skb_shinfo(skb)->nr_frags;
1506
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001507 spin_lock_irqsave(&ring->lock, flags);
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07001508 if (ring->free_bds <= (nr_frags + 1)) {
1509 if (!netif_tx_queue_stopped(txq)) {
1510 netif_tx_stop_queue(txq);
1511 netdev_err(dev,
1512 "%s: tx ring %d full when queue %d awake\n",
1513 __func__, index, ring->queue);
1514 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001515 ret = NETDEV_TX_BUSY;
1516 goto out;
1517 }
1518
Florian Fainelli474ea9c2014-07-22 11:01:52 -07001519 if (skb_padto(skb, ETH_ZLEN)) {
1520 ret = NETDEV_TX_OK;
1521 goto out;
1522 }
1523
Petri Gynther55868122016-03-24 11:27:20 -07001524 /* Retain how many bytes will be sent on the wire, without TSB inserted
1525 * by transmit checksum offload
1526 */
1527 GENET_CB(skb)->bytes_sent = skb->len;
1528
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001529 /* set the SKB transmit checksum */
1530 if (priv->desc_64b_en) {
Petri Gyntherbc233332014-10-01 11:30:01 -07001531 skb = bcmgenet_put_tx_csum(dev, skb);
1532 if (!skb) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001533 ret = NETDEV_TX_OK;
1534 goto out;
1535 }
1536 }
1537
1538 dma_desc_flags = DMA_SOP;
1539 if (nr_frags == 0)
1540 dma_desc_flags |= DMA_EOP;
1541
1542 /* Transmit single SKB or head of fragment list */
1543 ret = bcmgenet_xmit_single(dev, skb, dma_desc_flags, ring);
1544 if (ret) {
1545 ret = NETDEV_TX_OK;
1546 goto out;
1547 }
1548
1549 /* xmit fragment */
1550 for (i = 0; i < nr_frags; i++) {
1551 ret = bcmgenet_xmit_frag(dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001552 &skb_shinfo(skb)->frags[i],
1553 (i == nr_frags - 1) ? DMA_EOP : 0,
1554 ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001555 if (ret) {
1556 ret = NETDEV_TX_OK;
1557 goto out;
1558 }
1559 }
1560
Florian Fainellid03825f2014-03-20 10:53:21 -07001561 skb_tx_timestamp(skb);
1562
Florian Fainelliae67bf02015-03-13 12:11:06 -07001563 /* Decrement total BD count and advance our write pointer */
1564 ring->free_bds -= nr_frags + 1;
1565 ring->prod_index += nr_frags + 1;
1566 ring->prod_index &= DMA_P_INDEX_MASK;
1567
Petri Gynthere178c8c2016-04-09 00:20:36 -07001568 netdev_tx_sent_queue(txq, GENET_CB(skb)->bytes_sent);
1569
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001570 if (ring->free_bds <= (MAX_SKB_FRAGS + 1))
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001571 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001572
Florian Fainelliddd0ca52015-03-13 12:11:07 -07001573 if (!skb->xmit_more || netif_xmit_stopped(txq))
1574 /* Packets are ready, update producer index */
1575 bcmgenet_tdma_ring_writel(priv, ring->index,
1576 ring->prod_index, TDMA_PROD_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001577out:
1578 spin_unlock_irqrestore(&ring->lock, flags);
1579
1580 return ret;
1581}
1582
Petri Gyntherd6707be2015-03-12 15:48:00 -07001583static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
1584 struct enet_cb *cb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001585{
1586 struct device *kdev = &priv->pdev->dev;
1587 struct sk_buff *skb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001588 struct sk_buff *rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001589 dma_addr_t mapping;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001590
Petri Gyntherd6707be2015-03-12 15:48:00 -07001591 /* Allocate a new Rx skb */
Florian Fainellic91b7f62014-07-23 10:42:12 -07001592 skb = netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT);
Petri Gyntherd6707be2015-03-12 15:48:00 -07001593 if (!skb) {
1594 priv->mib.alloc_rx_buff_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001595 netif_err(priv, rx_err, priv->dev,
Petri Gyntherd6707be2015-03-12 15:48:00 -07001596 "%s: Rx skb allocation failed\n", __func__);
1597 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001598 }
1599
Petri Gyntherd6707be2015-03-12 15:48:00 -07001600 /* DMA-map the new Rx skb */
1601 mapping = dma_map_single(kdev, skb->data, priv->rx_buf_len,
1602 DMA_FROM_DEVICE);
1603 if (dma_mapping_error(kdev, mapping)) {
1604 priv->mib.rx_dma_failed++;
1605 dev_kfree_skb_any(skb);
1606 netif_err(priv, rx_err, priv->dev,
1607 "%s: Rx skb DMA mapping failed\n", __func__);
1608 return NULL;
1609 }
1610
1611 /* Grab the current Rx skb from the ring and DMA-unmap it */
1612 rx_skb = cb->skb;
1613 if (likely(rx_skb))
1614 dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
1615 priv->rx_buf_len, DMA_FROM_DEVICE);
1616
1617 /* Put the new Rx skb on the ring */
1618 cb->skb = skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001619 dma_unmap_addr_set(cb, dma_addr, mapping);
Petri Gynther8ac467e2015-03-09 13:40:00 -07001620 dmadesc_set_addr(priv, cb->bd_addr, mapping);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001621
Petri Gyntherd6707be2015-03-12 15:48:00 -07001622 /* Return the current Rx skb to caller */
1623 return rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001624}
1625
1626/* bcmgenet_desc_rx - descriptor based rx process.
1627 * this could be called from bottom half, or from NAPI polling method.
1628 */
Petri Gynther4055eae2015-03-25 12:35:16 -07001629static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001630 unsigned int budget)
1631{
Petri Gynther4055eae2015-03-25 12:35:16 -07001632 struct bcmgenet_priv *priv = ring->priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001633 struct net_device *dev = priv->dev;
1634 struct enet_cb *cb;
1635 struct sk_buff *skb;
1636 u32 dma_length_status;
1637 unsigned long dma_flag;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001638 int len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001639 unsigned int rxpktprocessed = 0, rxpkttoprocess;
1640 unsigned int p_index;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001641 unsigned int discards;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001642 unsigned int chksum_ok = 0;
1643
Petri Gynther4055eae2015-03-25 12:35:16 -07001644 p_index = bcmgenet_rdma_ring_readl(priv, ring->index, RDMA_PROD_INDEX);
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001645
1646 discards = (p_index >> DMA_P_INDEX_DISCARD_CNT_SHIFT) &
1647 DMA_P_INDEX_DISCARD_CNT_MASK;
1648 if (discards > ring->old_discards) {
1649 discards = discards - ring->old_discards;
1650 dev->stats.rx_missed_errors += discards;
1651 dev->stats.rx_errors += discards;
1652 ring->old_discards += discards;
1653
1654 /* Clear HW register when we reach 75% of maximum 0xFFFF */
1655 if (ring->old_discards >= 0xC000) {
1656 ring->old_discards = 0;
Petri Gynther4055eae2015-03-25 12:35:16 -07001657 bcmgenet_rdma_ring_writel(priv, ring->index, 0,
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001658 RDMA_PROD_INDEX);
1659 }
1660 }
1661
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001662 p_index &= DMA_P_INDEX_MASK;
1663
Petri Gynther8ac467e2015-03-09 13:40:00 -07001664 if (likely(p_index >= ring->c_index))
1665 rxpkttoprocess = p_index - ring->c_index;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001666 else
Petri Gynther8ac467e2015-03-09 13:40:00 -07001667 rxpkttoprocess = (DMA_C_INDEX_MASK + 1) - ring->c_index +
1668 p_index;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001669
1670 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001671 "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001672
1673 while ((rxpktprocessed < rxpkttoprocess) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001674 (rxpktprocessed < budget)) {
Petri Gynther8ac467e2015-03-09 13:40:00 -07001675 cb = &priv->rx_cbs[ring->read_ptr];
Petri Gyntherd6707be2015-03-12 15:48:00 -07001676 skb = bcmgenet_rx_refill(priv, cb);
Florian Fainellib629be52014-09-08 11:37:52 -07001677
Florian Fainellib629be52014-09-08 11:37:52 -07001678 if (unlikely(!skb)) {
1679 dev->stats.rx_dropped++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001680 goto next;
Florian Fainellib629be52014-09-08 11:37:52 -07001681 }
1682
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001683 if (!priv->desc_64b_en) {
Florian Fainellic91b7f62014-07-23 10:42:12 -07001684 dma_length_status =
Petri Gynther8ac467e2015-03-09 13:40:00 -07001685 dmadesc_get_length_status(priv, cb->bd_addr);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001686 } else {
1687 struct status_64 *status;
Florian Fainelli164d4f22014-07-23 10:42:13 -07001688
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001689 status = (struct status_64 *)skb->data;
1690 dma_length_status = status->length_status;
1691 }
1692
1693 /* DMA flags and length are still valid no matter how
1694 * we got the Receive Status Vector (64B RSB or register)
1695 */
1696 dma_flag = dma_length_status & 0xffff;
1697 len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
1698
1699 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001700 "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
Petri Gynther8ac467e2015-03-09 13:40:00 -07001701 __func__, p_index, ring->c_index,
1702 ring->read_ptr, dma_length_status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001703
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001704 if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
1705 netif_err(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001706 "dropping fragmented packet!\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001707 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001708 dev_kfree_skb_any(skb);
1709 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001710 }
Petri Gyntherd6707be2015-03-12 15:48:00 -07001711
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001712 /* report errors */
1713 if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
1714 DMA_RX_OV |
1715 DMA_RX_NO |
1716 DMA_RX_LG |
1717 DMA_RX_RXER))) {
1718 netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001719 (unsigned int)dma_flag);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001720 if (dma_flag & DMA_RX_CRC_ERROR)
1721 dev->stats.rx_crc_errors++;
1722 if (dma_flag & DMA_RX_OV)
1723 dev->stats.rx_over_errors++;
1724 if (dma_flag & DMA_RX_NO)
1725 dev->stats.rx_frame_errors++;
1726 if (dma_flag & DMA_RX_LG)
1727 dev->stats.rx_length_errors++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001728 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001729 dev_kfree_skb_any(skb);
1730 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001731 } /* error packet */
1732
1733 chksum_ok = (dma_flag & priv->dma_rx_chk_bit) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001734 priv->desc_rxchk_en;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001735
1736 skb_put(skb, len);
1737 if (priv->desc_64b_en) {
1738 skb_pull(skb, 64);
1739 len -= 64;
1740 }
1741
1742 if (likely(chksum_ok))
1743 skb->ip_summed = CHECKSUM_UNNECESSARY;
1744
1745 /* remove hardware 2bytes added for IP alignment */
1746 skb_pull(skb, 2);
1747 len -= 2;
1748
1749 if (priv->crc_fwd_en) {
1750 skb_trim(skb, len - ETH_FCS_LEN);
1751 len -= ETH_FCS_LEN;
1752 }
1753
1754 /*Finish setting up the received SKB and send it to the kernel*/
1755 skb->protocol = eth_type_trans(skb, priv->dev);
1756 dev->stats.rx_packets++;
1757 dev->stats.rx_bytes += len;
1758 if (dma_flag & DMA_RX_MULT)
1759 dev->stats.multicast++;
1760
1761 /* Notify kernel */
Petri Gynther4055eae2015-03-25 12:35:16 -07001762 napi_gro_receive(&ring->napi, skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001763 netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
1764
Petri Gyntherd6707be2015-03-12 15:48:00 -07001765next:
Florian Fainellicf377d82014-10-10 10:51:52 -07001766 rxpktprocessed++;
Petri Gynther8ac467e2015-03-09 13:40:00 -07001767 if (likely(ring->read_ptr < ring->end_ptr))
1768 ring->read_ptr++;
1769 else
1770 ring->read_ptr = ring->cb_ptr;
1771
1772 ring->c_index = (ring->c_index + 1) & DMA_C_INDEX_MASK;
Petri Gynther4055eae2015-03-25 12:35:16 -07001773 bcmgenet_rdma_ring_writel(priv, ring->index, ring->c_index, RDMA_CONS_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001774 }
1775
1776 return rxpktprocessed;
1777}
1778
Petri Gynther3ab11332015-03-25 12:35:15 -07001779/* Rx NAPI polling method */
1780static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
1781{
Petri Gynther4055eae2015-03-25 12:35:16 -07001782 struct bcmgenet_rx_ring *ring = container_of(napi,
1783 struct bcmgenet_rx_ring, napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07001784 unsigned int work_done;
1785
Petri Gynther4055eae2015-03-25 12:35:16 -07001786 work_done = bcmgenet_desc_rx(ring, budget);
Petri Gynther3ab11332015-03-25 12:35:15 -07001787
1788 if (work_done < budget) {
Eric Dumazeteb96ce02016-04-08 22:06:40 -07001789 napi_complete_done(napi, work_done);
Petri Gynther4055eae2015-03-25 12:35:16 -07001790 ring->int_enable(ring);
Petri Gynther3ab11332015-03-25 12:35:15 -07001791 }
1792
1793 return work_done;
1794}
1795
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001796/* Assign skb to RX DMA descriptor. */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001797static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv,
1798 struct bcmgenet_rx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001799{
1800 struct enet_cb *cb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001801 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001802 int i;
1803
Petri Gynther8ac467e2015-03-09 13:40:00 -07001804 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001805
1806 /* loop here for each buffer needing assign */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001807 for (i = 0; i < ring->size; i++) {
1808 cb = ring->cbs + i;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001809 skb = bcmgenet_rx_refill(priv, cb);
1810 if (skb)
1811 dev_kfree_skb_any(skb);
1812 if (!cb->skb)
1813 return -ENOMEM;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001814 }
1815
Petri Gyntherd6707be2015-03-12 15:48:00 -07001816 return 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001817}
1818
1819static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
1820{
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001821 struct device *kdev = &priv->pdev->dev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001822 struct enet_cb *cb;
1823 int i;
1824
1825 for (i = 0; i < priv->num_rx_bds; i++) {
1826 cb = &priv->rx_cbs[i];
1827
1828 if (dma_unmap_addr(cb, dma_addr)) {
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001829 dma_unmap_single(kdev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001830 dma_unmap_addr(cb, dma_addr),
1831 priv->rx_buf_len, DMA_FROM_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001832 dma_unmap_addr_set(cb, dma_addr, 0);
1833 }
1834
1835 if (cb->skb)
1836 bcmgenet_free_cb(cb);
1837 }
1838}
1839
Florian Fainellic91b7f62014-07-23 10:42:12 -07001840static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable)
Florian Fainellie29585b2014-07-21 15:29:20 -07001841{
1842 u32 reg;
1843
1844 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1845 if (enable)
1846 reg |= mask;
1847 else
1848 reg &= ~mask;
1849 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
1850
1851 /* UniMAC stops on a packet boundary, wait for a full-size packet
1852 * to be processed
1853 */
1854 if (enable == 0)
1855 usleep_range(1000, 2000);
1856}
1857
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001858static int reset_umac(struct bcmgenet_priv *priv)
1859{
1860 struct device *kdev = &priv->pdev->dev;
1861 unsigned int timeout = 0;
1862 u32 reg;
1863
1864 /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
1865 bcmgenet_rbuf_ctrl_set(priv, 0);
1866 udelay(10);
1867
1868 /* disable MAC while updating its registers */
1869 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1870
1871 /* issue soft reset, wait for it to complete */
1872 bcmgenet_umac_writel(priv, CMD_SW_RESET, UMAC_CMD);
1873 while (timeout++ < 1000) {
1874 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1875 if (!(reg & CMD_SW_RESET))
1876 return 0;
1877
1878 udelay(1);
1879 }
1880
1881 if (timeout == 1000) {
1882 dev_err(kdev,
Brian Norris7fc527f2014-07-29 14:34:14 -07001883 "timeout waiting for MAC to come out of reset\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001884 return -ETIMEDOUT;
1885 }
1886
1887 return 0;
1888}
1889
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001890static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
1891{
1892 /* Mask all interrupts.*/
1893 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1894 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
1895 bcmgenet_intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1896 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1897 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
1898 bcmgenet_intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1899}
1900
Florian Fainelli37850e32015-10-17 14:22:46 -07001901static void bcmgenet_link_intr_enable(struct bcmgenet_priv *priv)
1902{
1903 u32 int0_enable = 0;
1904
1905 /* Monitor cable plug/unplugged event for internal PHY, external PHY
1906 * and MoCA PHY
1907 */
1908 if (priv->internal_phy) {
1909 int0_enable |= UMAC_IRQ_LINK_EVENT;
1910 } else if (priv->ext_phy) {
1911 int0_enable |= UMAC_IRQ_LINK_EVENT;
1912 } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
1913 if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
1914 int0_enable |= UMAC_IRQ_LINK_EVENT;
1915 }
1916 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
1917}
1918
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001919static int init_umac(struct bcmgenet_priv *priv)
1920{
1921 struct device *kdev = &priv->pdev->dev;
1922 int ret;
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001923 u32 reg;
1924 u32 int0_enable = 0;
1925 u32 int1_enable = 0;
1926 int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001927
1928 dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
1929
1930 ret = reset_umac(priv);
1931 if (ret)
1932 return ret;
1933
1934 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1935 /* clear tx/rx counter */
1936 bcmgenet_umac_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001937 MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
1938 UMAC_MIB_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001939 bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
1940
1941 bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
1942
1943 /* init rx registers, enable ip header optimization */
1944 reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
1945 reg |= RBUF_ALIGN_2B;
1946 bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
1947
1948 if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
1949 bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
1950
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001951 bcmgenet_intr_disable(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001952
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001953 /* Enable Rx default queue 16 interrupts */
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001954 int0_enable |= UMAC_IRQ_RXDMA_DONE;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001955
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001956 /* Enable Tx default queue 16 interrupts */
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001957 int0_enable |= UMAC_IRQ_TXDMA_DONE;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001958
Florian Fainelli37850e32015-10-17 14:22:46 -07001959 /* Configure backpressure vectors for MoCA */
1960 if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001961 reg = bcmgenet_bp_mc_get(priv);
1962 reg |= BIT(priv->hw_params->bp_in_en_shift);
1963
1964 /* bp_mask: back pressure mask */
1965 if (netif_is_multiqueue(priv->dev))
1966 reg |= priv->hw_params->bp_in_mask;
1967 else
1968 reg &= ~priv->hw_params->bp_in_mask;
1969 bcmgenet_bp_mc_set(priv, reg);
1970 }
1971
1972 /* Enable MDIO interrupts on GENET v3+ */
1973 if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001974 int0_enable |= (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001975
Petri Gynther4055eae2015-03-25 12:35:16 -07001976 /* Enable Rx priority queue interrupts */
1977 for (i = 0; i < priv->hw_params->rx_queues; ++i)
1978 int1_enable |= (1 << (UMAC_IRQ1_RX_INTR_SHIFT + i));
1979
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001980 /* Enable Tx priority queue interrupts */
1981 for (i = 0; i < priv->hw_params->tx_queues; ++i)
1982 int1_enable |= (1 << i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001983
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001984 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
1985 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001986
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001987 /* Enable rx/tx engine.*/
1988 dev_dbg(kdev, "done init umac\n");
1989
1990 return 0;
1991}
1992
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001993/* Initialize a Tx ring along with corresponding hardware registers */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001994static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
1995 unsigned int index, unsigned int size,
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001996 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001997{
1998 struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
1999 u32 words_per_bd = WORDS_PER_BD(priv);
2000 u32 flow_period_val = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002001
2002 spin_lock_init(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002003 ring->priv = priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002004 ring->index = index;
2005 if (index == DESC_INDEX) {
2006 ring->queue = 0;
2007 ring->int_enable = bcmgenet_tx_ring16_int_enable;
2008 ring->int_disable = bcmgenet_tx_ring16_int_disable;
2009 } else {
2010 ring->queue = index + 1;
2011 ring->int_enable = bcmgenet_tx_ring_int_enable;
2012 ring->int_disable = bcmgenet_tx_ring_int_disable;
2013 }
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002014 ring->cbs = priv->tx_cbs + start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002015 ring->size = size;
Petri Gynther66d06752015-03-04 14:30:01 -08002016 ring->clean_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002017 ring->c_index = 0;
2018 ring->free_bds = size;
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002019 ring->write_ptr = start_ptr;
2020 ring->cb_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002021 ring->end_ptr = end_ptr - 1;
2022 ring->prod_index = 0;
2023
2024 /* Set flow period for ring != 16 */
2025 if (index != DESC_INDEX)
2026 flow_period_val = ENET_MAX_MTU_SIZE << 16;
2027
2028 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
2029 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
2030 bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
2031 /* Disable rate control for now */
2032 bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002033 TDMA_FLOW_PERIOD);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002034 bcmgenet_tdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002035 ((size << DMA_RING_SIZE_SHIFT) |
2036 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002037
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002038 /* Set start and end address, read and write pointers */
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002039 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002040 DMA_START_ADDR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002041 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002042 TDMA_READ_PTR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08002043 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002044 TDMA_WRITE_PTR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002045 bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002046 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002047}
2048
2049/* Initialize a RDMA ring */
2050static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
Petri Gynther8ac467e2015-03-09 13:40:00 -07002051 unsigned int index, unsigned int size,
2052 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002053{
Petri Gynther8ac467e2015-03-09 13:40:00 -07002054 struct bcmgenet_rx_ring *ring = &priv->rx_rings[index];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002055 u32 words_per_bd = WORDS_PER_BD(priv);
2056 int ret;
2057
Petri Gynther4055eae2015-03-25 12:35:16 -07002058 ring->priv = priv;
Petri Gynther8ac467e2015-03-09 13:40:00 -07002059 ring->index = index;
Petri Gynther4055eae2015-03-25 12:35:16 -07002060 if (index == DESC_INDEX) {
2061 ring->int_enable = bcmgenet_rx_ring16_int_enable;
2062 ring->int_disable = bcmgenet_rx_ring16_int_disable;
2063 } else {
2064 ring->int_enable = bcmgenet_rx_ring_int_enable;
2065 ring->int_disable = bcmgenet_rx_ring_int_disable;
2066 }
Petri Gynther8ac467e2015-03-09 13:40:00 -07002067 ring->cbs = priv->rx_cbs + start_ptr;
2068 ring->size = size;
2069 ring->c_index = 0;
2070 ring->read_ptr = start_ptr;
2071 ring->cb_ptr = start_ptr;
2072 ring->end_ptr = end_ptr - 1;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002073
Petri Gynther8ac467e2015-03-09 13:40:00 -07002074 ret = bcmgenet_alloc_rx_buffers(priv, ring);
2075 if (ret)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002076 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002077
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002078 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
2079 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
Petri Gynther6f5a2722015-03-06 13:45:00 -08002080 bcmgenet_rdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002081 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002082 ((size << DMA_RING_SIZE_SHIFT) |
2083 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002084 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002085 (DMA_FC_THRESH_LO <<
2086 DMA_XOFF_THRESHOLD_SHIFT) |
2087 DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
Petri Gynther6f5a2722015-03-06 13:45:00 -08002088
2089 /* Set start and end address, read and write pointers */
Petri Gynther8ac467e2015-03-09 13:40:00 -07002090 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2091 DMA_START_ADDR);
2092 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2093 RDMA_READ_PTR);
2094 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2095 RDMA_WRITE_PTR);
2096 bcmgenet_rdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Petri Gynther6f5a2722015-03-06 13:45:00 -08002097 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002098
2099 return ret;
2100}
2101
Petri Gynthere2aadb42015-03-25 12:35:14 -07002102static void bcmgenet_init_tx_napi(struct bcmgenet_priv *priv)
2103{
2104 unsigned int i;
2105 struct bcmgenet_tx_ring *ring;
2106
2107 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2108 ring = &priv->tx_rings[i];
Eric Dumazetd64b5e82015-11-18 06:31:00 -08002109 netif_tx_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll, 64);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002110 }
2111
2112 ring = &priv->tx_rings[DESC_INDEX];
Eric Dumazetd64b5e82015-11-18 06:31:00 -08002113 netif_tx_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll, 64);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002114}
2115
2116static void bcmgenet_enable_tx_napi(struct bcmgenet_priv *priv)
2117{
2118 unsigned int i;
2119 struct bcmgenet_tx_ring *ring;
2120
2121 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2122 ring = &priv->tx_rings[i];
2123 napi_enable(&ring->napi);
2124 }
2125
2126 ring = &priv->tx_rings[DESC_INDEX];
2127 napi_enable(&ring->napi);
2128}
2129
2130static void bcmgenet_disable_tx_napi(struct bcmgenet_priv *priv)
2131{
2132 unsigned int i;
2133 struct bcmgenet_tx_ring *ring;
2134
2135 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2136 ring = &priv->tx_rings[i];
2137 napi_disable(&ring->napi);
2138 }
2139
2140 ring = &priv->tx_rings[DESC_INDEX];
2141 napi_disable(&ring->napi);
2142}
2143
2144static void bcmgenet_fini_tx_napi(struct bcmgenet_priv *priv)
2145{
2146 unsigned int i;
2147 struct bcmgenet_tx_ring *ring;
2148
2149 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2150 ring = &priv->tx_rings[i];
2151 netif_napi_del(&ring->napi);
2152 }
2153
2154 ring = &priv->tx_rings[DESC_INDEX];
2155 netif_napi_del(&ring->napi);
2156}
2157
Petri Gynther16c6d662015-02-23 11:00:45 -08002158/* Initialize Tx queues
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002159 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002160 * Queues 0-3 are priority-based, each one has 32 descriptors,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002161 * with queue 0 being the highest priority queue.
2162 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002163 * Queue 16 is the default Tx queue with
Petri Gynther51a966a2015-02-23 11:00:46 -08002164 * GENET_Q16_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors.
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002165 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002166 * The transmit control block pool is then partitioned as follows:
2167 * - Tx queue 0 uses tx_cbs[0..31]
2168 * - Tx queue 1 uses tx_cbs[32..63]
2169 * - Tx queue 2 uses tx_cbs[64..95]
2170 * - Tx queue 3 uses tx_cbs[96..127]
2171 * - Tx queue 16 uses tx_cbs[128..255]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002172 */
Petri Gynther16c6d662015-02-23 11:00:45 -08002173static void bcmgenet_init_tx_queues(struct net_device *dev)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002174{
2175 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther16c6d662015-02-23 11:00:45 -08002176 u32 i, dma_enable;
2177 u32 dma_ctrl, ring_cfg;
Petri Gynther37742162014-10-07 09:30:01 -07002178 u32 dma_priority[3] = {0, 0, 0};
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002179
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002180 dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
2181 dma_enable = dma_ctrl & DMA_EN;
2182 dma_ctrl &= ~DMA_EN;
2183 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
2184
Petri Gynther16c6d662015-02-23 11:00:45 -08002185 dma_ctrl = 0;
2186 ring_cfg = 0;
2187
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002188 /* Enable strict priority arbiter mode */
2189 bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
2190
Petri Gynther16c6d662015-02-23 11:00:45 -08002191 /* Initialize Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002192 for (i = 0; i < priv->hw_params->tx_queues; i++) {
Petri Gynther51a966a2015-02-23 11:00:46 -08002193 bcmgenet_init_tx_ring(priv, i, priv->hw_params->tx_bds_per_q,
2194 i * priv->hw_params->tx_bds_per_q,
2195 (i + 1) * priv->hw_params->tx_bds_per_q);
Petri Gynther16c6d662015-02-23 11:00:45 -08002196 ring_cfg |= (1 << i);
2197 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002198 dma_priority[DMA_PRIO_REG_INDEX(i)] |=
2199 ((GENET_Q0_PRIORITY + i) << DMA_PRIO_REG_SHIFT(i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002200 }
2201
Petri Gynther16c6d662015-02-23 11:00:45 -08002202 /* Initialize Tx default queue 16 */
Petri Gynther51a966a2015-02-23 11:00:46 -08002203 bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_Q16_TX_BD_CNT,
Petri Gynther16c6d662015-02-23 11:00:45 -08002204 priv->hw_params->tx_queues *
Petri Gynther51a966a2015-02-23 11:00:46 -08002205 priv->hw_params->tx_bds_per_q,
Petri Gynther16c6d662015-02-23 11:00:45 -08002206 TOTAL_DESC);
2207 ring_cfg |= (1 << DESC_INDEX);
2208 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002209 dma_priority[DMA_PRIO_REG_INDEX(DESC_INDEX)] |=
2210 ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) <<
2211 DMA_PRIO_REG_SHIFT(DESC_INDEX));
Petri Gynther16c6d662015-02-23 11:00:45 -08002212
2213 /* Set Tx queue priorities */
Petri Gynther37742162014-10-07 09:30:01 -07002214 bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
2215 bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
2216 bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);
2217
Petri Gynthere2aadb42015-03-25 12:35:14 -07002218 /* Initialize Tx NAPI */
2219 bcmgenet_init_tx_napi(priv);
2220
Petri Gynther16c6d662015-02-23 11:00:45 -08002221 /* Enable Tx queues */
2222 bcmgenet_tdma_writel(priv, ring_cfg, DMA_RING_CFG);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002223
Petri Gynther16c6d662015-02-23 11:00:45 -08002224 /* Enable Tx DMA */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002225 if (dma_enable)
Petri Gynther16c6d662015-02-23 11:00:45 -08002226 dma_ctrl |= DMA_EN;
2227 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002228}
2229
Petri Gynther3ab11332015-03-25 12:35:15 -07002230static void bcmgenet_init_rx_napi(struct bcmgenet_priv *priv)
2231{
Petri Gynther4055eae2015-03-25 12:35:16 -07002232 unsigned int i;
2233 struct bcmgenet_rx_ring *ring;
2234
2235 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2236 ring = &priv->rx_rings[i];
2237 netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll, 64);
2238 }
2239
2240 ring = &priv->rx_rings[DESC_INDEX];
2241 netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll, 64);
Petri Gynther3ab11332015-03-25 12:35:15 -07002242}
2243
2244static void bcmgenet_enable_rx_napi(struct bcmgenet_priv *priv)
2245{
Petri Gynther4055eae2015-03-25 12:35:16 -07002246 unsigned int i;
2247 struct bcmgenet_rx_ring *ring;
2248
2249 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2250 ring = &priv->rx_rings[i];
2251 napi_enable(&ring->napi);
2252 }
2253
2254 ring = &priv->rx_rings[DESC_INDEX];
2255 napi_enable(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002256}
2257
2258static void bcmgenet_disable_rx_napi(struct bcmgenet_priv *priv)
2259{
Petri Gynther4055eae2015-03-25 12:35:16 -07002260 unsigned int i;
2261 struct bcmgenet_rx_ring *ring;
2262
2263 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2264 ring = &priv->rx_rings[i];
2265 napi_disable(&ring->napi);
2266 }
2267
2268 ring = &priv->rx_rings[DESC_INDEX];
2269 napi_disable(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002270}
2271
2272static void bcmgenet_fini_rx_napi(struct bcmgenet_priv *priv)
2273{
Petri Gynther4055eae2015-03-25 12:35:16 -07002274 unsigned int i;
2275 struct bcmgenet_rx_ring *ring;
2276
2277 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2278 ring = &priv->rx_rings[i];
2279 netif_napi_del(&ring->napi);
2280 }
2281
2282 ring = &priv->rx_rings[DESC_INDEX];
2283 netif_napi_del(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002284}
2285
Petri Gynther8ac467e2015-03-09 13:40:00 -07002286/* Initialize Rx queues
2287 *
2288 * Queues 0-15 are priority queues. Hardware Filtering Block (HFB) can be
2289 * used to direct traffic to these queues.
2290 *
2291 * Queue 16 is the default Rx queue with GENET_Q16_RX_BD_CNT descriptors.
2292 */
2293static int bcmgenet_init_rx_queues(struct net_device *dev)
2294{
2295 struct bcmgenet_priv *priv = netdev_priv(dev);
2296 u32 i;
2297 u32 dma_enable;
2298 u32 dma_ctrl;
2299 u32 ring_cfg;
2300 int ret;
2301
2302 dma_ctrl = bcmgenet_rdma_readl(priv, DMA_CTRL);
2303 dma_enable = dma_ctrl & DMA_EN;
2304 dma_ctrl &= ~DMA_EN;
2305 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2306
2307 dma_ctrl = 0;
2308 ring_cfg = 0;
2309
2310 /* Initialize Rx priority queues */
2311 for (i = 0; i < priv->hw_params->rx_queues; i++) {
2312 ret = bcmgenet_init_rx_ring(priv, i,
2313 priv->hw_params->rx_bds_per_q,
2314 i * priv->hw_params->rx_bds_per_q,
2315 (i + 1) *
2316 priv->hw_params->rx_bds_per_q);
2317 if (ret)
2318 return ret;
2319
2320 ring_cfg |= (1 << i);
2321 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2322 }
2323
2324 /* Initialize Rx default queue 16 */
2325 ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, GENET_Q16_RX_BD_CNT,
2326 priv->hw_params->rx_queues *
2327 priv->hw_params->rx_bds_per_q,
2328 TOTAL_DESC);
2329 if (ret)
2330 return ret;
2331
2332 ring_cfg |= (1 << DESC_INDEX);
2333 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
2334
Petri Gynther3ab11332015-03-25 12:35:15 -07002335 /* Initialize Rx NAPI */
2336 bcmgenet_init_rx_napi(priv);
2337
Petri Gynther8ac467e2015-03-09 13:40:00 -07002338 /* Enable rings */
2339 bcmgenet_rdma_writel(priv, ring_cfg, DMA_RING_CFG);
2340
2341 /* Configure ring as descriptor ring and re-enable DMA if enabled */
2342 if (dma_enable)
2343 dma_ctrl |= DMA_EN;
2344 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2345
2346 return 0;
2347}
2348
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002349static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
2350{
2351 int ret = 0;
2352 int timeout = 0;
2353 u32 reg;
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002354 u32 dma_ctrl;
2355 int i;
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002356
2357 /* Disable TDMA to stop add more frames in TX DMA */
2358 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2359 reg &= ~DMA_EN;
2360 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2361
2362 /* Check TDMA status register to confirm TDMA is disabled */
2363 while (timeout++ < DMA_TIMEOUT_VAL) {
2364 reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
2365 if (reg & DMA_DISABLED)
2366 break;
2367
2368 udelay(1);
2369 }
2370
2371 if (timeout == DMA_TIMEOUT_VAL) {
2372 netdev_warn(priv->dev, "Timed out while disabling TX DMA\n");
2373 ret = -ETIMEDOUT;
2374 }
2375
2376 /* Wait 10ms for packet drain in both tx and rx dma */
2377 usleep_range(10000, 20000);
2378
2379 /* Disable RDMA */
2380 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2381 reg &= ~DMA_EN;
2382 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2383
2384 timeout = 0;
2385 /* Check RDMA status register to confirm RDMA is disabled */
2386 while (timeout++ < DMA_TIMEOUT_VAL) {
2387 reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
2388 if (reg & DMA_DISABLED)
2389 break;
2390
2391 udelay(1);
2392 }
2393
2394 if (timeout == DMA_TIMEOUT_VAL) {
2395 netdev_warn(priv->dev, "Timed out while disabling RX DMA\n");
2396 ret = -ETIMEDOUT;
2397 }
2398
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002399 dma_ctrl = 0;
2400 for (i = 0; i < priv->hw_params->rx_queues; i++)
2401 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2402 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2403 reg &= ~dma_ctrl;
2404 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2405
2406 dma_ctrl = 0;
2407 for (i = 0; i < priv->hw_params->tx_queues; i++)
2408 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2409 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2410 reg &= ~dma_ctrl;
2411 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2412
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002413 return ret;
2414}
2415
Petri Gynther9abab962015-03-30 00:29:01 -07002416static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002417{
2418 int i;
Petri Gynthere178c8c2016-04-09 00:20:36 -07002419 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002420
Petri Gynther9abab962015-03-30 00:29:01 -07002421 bcmgenet_fini_rx_napi(priv);
2422 bcmgenet_fini_tx_napi(priv);
2423
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002424 /* disable DMA */
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002425 bcmgenet_dma_teardown(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002426
2427 for (i = 0; i < priv->num_tx_bds; i++) {
2428 if (priv->tx_cbs[i].skb != NULL) {
2429 dev_kfree_skb(priv->tx_cbs[i].skb);
2430 priv->tx_cbs[i].skb = NULL;
2431 }
2432 }
2433
Petri Gynthere178c8c2016-04-09 00:20:36 -07002434 for (i = 0; i < priv->hw_params->tx_queues; i++) {
2435 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[i].queue);
2436 netdev_tx_reset_queue(txq);
2437 }
2438
2439 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[DESC_INDEX].queue);
2440 netdev_tx_reset_queue(txq);
2441
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002442 bcmgenet_free_rx_buffers(priv);
2443 kfree(priv->rx_cbs);
2444 kfree(priv->tx_cbs);
2445}
2446
2447/* init_edma: Initialize DMA control register */
2448static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
2449{
2450 int ret;
Petri Gynther014012a2015-02-23 11:00:45 -08002451 unsigned int i;
2452 struct enet_cb *cb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002453
Petri Gynther6f5a2722015-03-06 13:45:00 -08002454 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002455
Petri Gynther6f5a2722015-03-06 13:45:00 -08002456 /* Initialize common Rx ring structures */
2457 priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
2458 priv->num_rx_bds = TOTAL_DESC;
2459 priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct enet_cb),
2460 GFP_KERNEL);
2461 if (!priv->rx_cbs)
2462 return -ENOMEM;
2463
2464 for (i = 0; i < priv->num_rx_bds; i++) {
2465 cb = priv->rx_cbs + i;
2466 cb->bd_addr = priv->rx_bds + i * DMA_DESC_SIZE;
2467 }
2468
Brian Norris7fc527f2014-07-29 14:34:14 -07002469 /* Initialize common TX ring structures */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002470 priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
2471 priv->num_tx_bds = TOTAL_DESC;
Florian Fainellic489be02014-07-23 10:42:15 -07002472 priv->tx_cbs = kcalloc(priv->num_tx_bds, sizeof(struct enet_cb),
Florian Fainellic91b7f62014-07-23 10:42:12 -07002473 GFP_KERNEL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002474 if (!priv->tx_cbs) {
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002475 kfree(priv->rx_cbs);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002476 return -ENOMEM;
2477 }
2478
Petri Gynther014012a2015-02-23 11:00:45 -08002479 for (i = 0; i < priv->num_tx_bds; i++) {
2480 cb = priv->tx_cbs + i;
2481 cb->bd_addr = priv->tx_bds + i * DMA_DESC_SIZE;
2482 }
2483
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002484 /* Init rDma */
2485 bcmgenet_rdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
2486
2487 /* Initialize Rx queues */
2488 ret = bcmgenet_init_rx_queues(priv->dev);
2489 if (ret) {
2490 netdev_err(priv->dev, "failed to initialize Rx queues\n");
2491 bcmgenet_free_rx_buffers(priv);
2492 kfree(priv->rx_cbs);
2493 kfree(priv->tx_cbs);
2494 return ret;
2495 }
2496
2497 /* Init tDma */
2498 bcmgenet_tdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
2499
Petri Gynther16c6d662015-02-23 11:00:45 -08002500 /* Initialize Tx queues */
2501 bcmgenet_init_tx_queues(priv->dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002502
2503 return 0;
2504}
2505
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002506/* Interrupt bottom half */
2507static void bcmgenet_irq_task(struct work_struct *work)
2508{
2509 struct bcmgenet_priv *priv = container_of(
2510 work, struct bcmgenet_priv, bcmgenet_irq_work);
2511
2512 netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
2513
Florian Fainelli8fdb0e02014-07-21 15:29:26 -07002514 if (priv->irq0_stat & UMAC_IRQ_MPD_R) {
2515 priv->irq0_stat &= ~UMAC_IRQ_MPD_R;
2516 netif_dbg(priv, wol, priv->dev,
2517 "magic packet detected, waking up\n");
2518 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
2519 }
2520
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002521 /* Link UP/DOWN event */
Jaedon Shind07c0272016-02-19 13:48:50 +09002522 if (priv->irq0_stat & UMAC_IRQ_LINK_EVENT) {
Florian Fainelli0299b6a2016-09-26 22:31:56 +02002523 phy_mac_interrupt(priv->phydev,
Petri Gynther451e1ca2015-03-30 00:29:35 -07002524 !!(priv->irq0_stat & UMAC_IRQ_LINK_UP));
Petri Gynthere122966d2015-03-30 00:29:24 -07002525 priv->irq0_stat &= ~UMAC_IRQ_LINK_EVENT;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002526 }
2527}
2528
Petri Gynther4055eae2015-03-25 12:35:16 -07002529/* bcmgenet_isr1: handle Rx and Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002530static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
2531{
2532 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002533 struct bcmgenet_rx_ring *rx_ring;
2534 struct bcmgenet_tx_ring *tx_ring;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002535 unsigned int index;
2536
2537 /* Save irq status for bottom-half processing. */
2538 priv->irq1_stat =
2539 bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002540 ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002541
Brian Norris7fc527f2014-07-29 14:34:14 -07002542 /* clear interrupts */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002543 bcmgenet_intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
2544
2545 netif_dbg(priv, intr, priv->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002546 "%s: IRQ=0x%x\n", __func__, priv->irq1_stat);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002547
Petri Gynther4055eae2015-03-25 12:35:16 -07002548 /* Check Rx priority queue interrupts */
2549 for (index = 0; index < priv->hw_params->rx_queues; index++) {
2550 if (!(priv->irq1_stat & BIT(UMAC_IRQ1_RX_INTR_SHIFT + index)))
2551 continue;
2552
2553 rx_ring = &priv->rx_rings[index];
2554
2555 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2556 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002557 __napi_schedule_irqoff(&rx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07002558 }
2559 }
2560
2561 /* Check Tx priority queue interrupts */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002562 for (index = 0; index < priv->hw_params->tx_queues; index++) {
2563 if (!(priv->irq1_stat & BIT(index)))
2564 continue;
2565
Petri Gynther4055eae2015-03-25 12:35:16 -07002566 tx_ring = &priv->tx_rings[index];
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002567
Petri Gynther4055eae2015-03-25 12:35:16 -07002568 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2569 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002570 __napi_schedule_irqoff(&tx_ring->napi);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002571 }
2572 }
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002573
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002574 return IRQ_HANDLED;
2575}
2576
Petri Gynther4055eae2015-03-25 12:35:16 -07002577/* bcmgenet_isr0: handle Rx and Tx default queues + other stuff */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002578static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
2579{
2580 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002581 struct bcmgenet_rx_ring *rx_ring;
2582 struct bcmgenet_tx_ring *tx_ring;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002583
2584 /* Save irq status for bottom-half processing. */
2585 priv->irq0_stat =
2586 bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
2587 ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002588
Brian Norris7fc527f2014-07-29 14:34:14 -07002589 /* clear interrupts */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002590 bcmgenet_intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
2591
2592 netif_dbg(priv, intr, priv->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002593 "IRQ=0x%x\n", priv->irq0_stat);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002594
Petri Gyntheree7d8c22015-03-30 00:28:50 -07002595 if (priv->irq0_stat & UMAC_IRQ_RXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07002596 rx_ring = &priv->rx_rings[DESC_INDEX];
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002597
Petri Gynther4055eae2015-03-25 12:35:16 -07002598 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2599 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002600 __napi_schedule_irqoff(&rx_ring->napi);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002601 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002602 }
Petri Gynther4055eae2015-03-25 12:35:16 -07002603
Petri Gyntheree7d8c22015-03-30 00:28:50 -07002604 if (priv->irq0_stat & UMAC_IRQ_TXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07002605 tx_ring = &priv->tx_rings[DESC_INDEX];
2606
2607 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2608 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002609 __napi_schedule_irqoff(&tx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07002610 }
2611 }
2612
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002613 if (priv->irq0_stat & (UMAC_IRQ_PHY_DET_R |
2614 UMAC_IRQ_PHY_DET_F |
Petri Gynthere122966d2015-03-30 00:29:24 -07002615 UMAC_IRQ_LINK_EVENT |
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002616 UMAC_IRQ_HFB_SM |
2617 UMAC_IRQ_HFB_MM |
2618 UMAC_IRQ_MPD_R)) {
2619 /* all other interested interrupts handled in bottom half */
2620 schedule_work(&priv->bcmgenet_irq_work);
2621 }
2622
2623 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07002624 priv->irq0_stat & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002625 priv->irq0_stat &= ~(UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
2626 wake_up(&priv->wq);
2627 }
2628
2629 return IRQ_HANDLED;
2630}
2631
Florian Fainelli85620562014-07-21 15:29:23 -07002632static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
2633{
2634 struct bcmgenet_priv *priv = dev_id;
2635
2636 pm_wakeup_event(&priv->pdev->dev, 0);
2637
2638 return IRQ_HANDLED;
2639}
2640
Florian Fainelli4d2e8882015-07-31 11:42:54 -07002641#ifdef CONFIG_NET_POLL_CONTROLLER
2642static void bcmgenet_poll_controller(struct net_device *dev)
2643{
2644 struct bcmgenet_priv *priv = netdev_priv(dev);
2645
2646 /* Invoke the main RX/TX interrupt handler */
2647 disable_irq(priv->irq0);
2648 bcmgenet_isr0(priv->irq0, priv);
2649 enable_irq(priv->irq0);
2650
2651 /* And the interrupt handler for RX/TX priority queues */
2652 disable_irq(priv->irq1);
2653 bcmgenet_isr1(priv->irq1, priv);
2654 enable_irq(priv->irq1);
2655}
2656#endif
2657
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002658static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
2659{
2660 u32 reg;
2661
2662 reg = bcmgenet_rbuf_ctrl_get(priv);
2663 reg |= BIT(1);
2664 bcmgenet_rbuf_ctrl_set(priv, reg);
2665 udelay(10);
2666
2667 reg &= ~BIT(1);
2668 bcmgenet_rbuf_ctrl_set(priv, reg);
2669 udelay(10);
2670}
2671
2672static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002673 unsigned char *addr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002674{
2675 bcmgenet_umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
2676 (addr[2] << 8) | addr[3], UMAC_MAC0);
2677 bcmgenet_umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
2678}
2679
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002680/* Returns a reusable dma control register value */
2681static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
2682{
2683 u32 reg;
2684 u32 dma_ctrl;
2685
2686 /* disable DMA */
2687 dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
2688 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2689 reg &= ~dma_ctrl;
2690 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2691
2692 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2693 reg &= ~dma_ctrl;
2694 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2695
2696 bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
2697 udelay(10);
2698 bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
2699
2700 return dma_ctrl;
2701}
2702
2703static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
2704{
2705 u32 reg;
2706
2707 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2708 reg |= dma_ctrl;
2709 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2710
2711 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2712 reg |= dma_ctrl;
2713 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2714}
2715
Petri Gynther0034de42015-03-13 14:45:00 -07002716/* bcmgenet_hfb_clear
2717 *
2718 * Clear Hardware Filter Block and disable all filtering.
2719 */
2720static void bcmgenet_hfb_clear(struct bcmgenet_priv *priv)
2721{
2722 u32 i;
2723
2724 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_CTRL);
2725 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS);
2726 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS + 4);
2727
2728 for (i = DMA_INDEX2RING_0; i <= DMA_INDEX2RING_7; i++)
2729 bcmgenet_rdma_writel(priv, 0x0, i);
2730
2731 for (i = 0; i < (priv->hw_params->hfb_filter_cnt / 4); i++)
2732 bcmgenet_hfb_reg_writel(priv, 0x0,
2733 HFB_FLT_LEN_V3PLUS + i * sizeof(u32));
2734
2735 for (i = 0; i < priv->hw_params->hfb_filter_cnt *
2736 priv->hw_params->hfb_filter_size; i++)
2737 bcmgenet_hfb_writel(priv, 0x0, i * sizeof(u32));
2738}
2739
2740static void bcmgenet_hfb_init(struct bcmgenet_priv *priv)
2741{
2742 if (GENET_IS_V1(priv) || GENET_IS_V2(priv))
2743 return;
2744
2745 bcmgenet_hfb_clear(priv);
2746}
2747
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002748static void bcmgenet_netif_start(struct net_device *dev)
2749{
2750 struct bcmgenet_priv *priv = netdev_priv(dev);
2751
2752 /* Start the network engine */
Petri Gynther3ab11332015-03-25 12:35:15 -07002753 bcmgenet_enable_rx_napi(priv);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002754 bcmgenet_enable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002755
2756 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
2757
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002758 netif_tx_start_all_queues(dev);
2759
Florian Fainelli37850e32015-10-17 14:22:46 -07002760 /* Monitor link interrupts now */
2761 bcmgenet_link_intr_enable(priv);
2762
Florian Fainelli0299b6a2016-09-26 22:31:56 +02002763 phy_start(priv->phydev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002764}
2765
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002766static int bcmgenet_open(struct net_device *dev)
2767{
2768 struct bcmgenet_priv *priv = netdev_priv(dev);
2769 unsigned long dma_ctrl;
2770 u32 reg;
2771 int ret;
2772
2773 netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
2774
2775 /* Turn on the clock */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002776 clk_prepare_enable(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002777
Florian Fainellia642c4f2015-03-23 15:09:56 -07002778 /* If this is an internal GPHY, power it back on now, before UniMAC is
2779 * brought out of reset as absolutely no UniMAC activity is allowed
2780 */
Florian Fainellic624f892015-07-16 15:51:17 -07002781 if (priv->internal_phy)
Florian Fainellia642c4f2015-03-23 15:09:56 -07002782 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
2783
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002784 /* take MAC out of reset */
2785 bcmgenet_umac_reset(priv);
2786
2787 ret = init_umac(priv);
2788 if (ret)
2789 goto err_clk_disable;
2790
2791 /* disable ethernet MAC while updating its registers */
Florian Fainellie29585b2014-07-21 15:29:20 -07002792 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002793
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002794 /* Make sure we reflect the value of CRC_CMD_FWD */
2795 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
2796 priv->crc_fwd_en = !!(reg & CMD_CRC_FWD);
2797
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002798 bcmgenet_set_hw_addr(priv, dev->dev_addr);
2799
Florian Fainellic624f892015-07-16 15:51:17 -07002800 if (priv->internal_phy) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002801 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
2802 reg |= EXT_ENERGY_DET_MASK;
2803 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
2804 }
2805
2806 /* Disable RX/TX DMA and flush TX queues */
2807 dma_ctrl = bcmgenet_dma_disable(priv);
2808
2809 /* Reinitialize TDMA and RDMA and SW housekeeping */
2810 ret = bcmgenet_init_dma(priv);
2811 if (ret) {
2812 netdev_err(dev, "failed to initialize DMA\n");
Petri Gyntherfac25942015-03-30 00:29:13 -07002813 goto err_clk_disable;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002814 }
2815
2816 /* Always enable ring 16 - descriptor ring */
2817 bcmgenet_enable_dma(priv, dma_ctrl);
2818
Petri Gynther0034de42015-03-13 14:45:00 -07002819 /* HFB init */
2820 bcmgenet_hfb_init(priv);
2821
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002822 ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002823 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002824 if (ret < 0) {
2825 netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
2826 goto err_fini_dma;
2827 }
2828
2829 ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002830 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002831 if (ret < 0) {
2832 netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
2833 goto err_irq0;
2834 }
2835
Florian Fainelli6cc8e6d2015-07-16 15:51:18 -07002836 ret = bcmgenet_mii_probe(dev);
2837 if (ret) {
2838 netdev_err(dev, "failed to connect to PHY\n");
2839 goto err_irq1;
2840 }
Florian Fainellic96e7312014-11-10 18:06:20 -08002841
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002842 bcmgenet_netif_start(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002843
2844 return 0;
2845
Florian Fainelli6cc8e6d2015-07-16 15:51:18 -07002846err_irq1:
2847 free_irq(priv->irq1, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002848err_irq0:
Florian Fainelli978ffac42015-07-16 15:51:15 -07002849 free_irq(priv->irq0, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002850err_fini_dma:
2851 bcmgenet_fini_dma(priv);
2852err_clk_disable:
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002853 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002854 return ret;
2855}
2856
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002857static void bcmgenet_netif_stop(struct net_device *dev)
2858{
2859 struct bcmgenet_priv *priv = netdev_priv(dev);
2860
2861 netif_tx_stop_all_queues(dev);
Florian Fainelli0299b6a2016-09-26 22:31:56 +02002862 phy_stop(priv->phydev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002863 bcmgenet_intr_disable(priv);
Petri Gynther3ab11332015-03-25 12:35:15 -07002864 bcmgenet_disable_rx_napi(priv);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002865 bcmgenet_disable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002866
2867 /* Wait for pending work items to complete. Since interrupts are
2868 * disabled no new work will be scheduled.
2869 */
2870 cancel_work_sync(&priv->bcmgenet_irq_work);
Florian Fainellicc013fb2014-08-11 14:50:43 -07002871
Florian Fainellicc013fb2014-08-11 14:50:43 -07002872 priv->old_link = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002873 priv->old_speed = -1;
Florian Fainellicc013fb2014-08-11 14:50:43 -07002874 priv->old_duplex = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002875 priv->old_pause = -1;
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002876}
2877
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002878static int bcmgenet_close(struct net_device *dev)
2879{
2880 struct bcmgenet_priv *priv = netdev_priv(dev);
2881 int ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002882
2883 netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
2884
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002885 bcmgenet_netif_stop(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002886
Florian Fainellic96e7312014-11-10 18:06:20 -08002887 /* Really kill the PHY state machine and disconnect from it */
Florian Fainelli0299b6a2016-09-26 22:31:56 +02002888 phy_disconnect(priv->phydev);
Florian Fainellic96e7312014-11-10 18:06:20 -08002889
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002890 /* Disable MAC receive */
Florian Fainellie29585b2014-07-21 15:29:20 -07002891 umac_enable_set(priv, CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002892
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002893 ret = bcmgenet_dma_teardown(priv);
2894 if (ret)
2895 return ret;
2896
2897 /* Disable MAC transmit. TX DMA disabled have to done before this */
Florian Fainellie29585b2014-07-21 15:29:20 -07002898 umac_enable_set(priv, CMD_TX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002899
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002900 /* tx reclaim */
2901 bcmgenet_tx_reclaim_all(dev);
2902 bcmgenet_fini_dma(priv);
2903
2904 free_irq(priv->irq0, priv);
2905 free_irq(priv->irq1, priv);
2906
Florian Fainellic624f892015-07-16 15:51:17 -07002907 if (priv->internal_phy)
Florian Fainellica8cf342015-03-23 15:09:51 -07002908 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002909
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002910 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002911
Florian Fainellica8cf342015-03-23 15:09:51 -07002912 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002913}
2914
Florian Fainelli13ea6572015-06-04 16:15:50 -07002915static void bcmgenet_dump_tx_queue(struct bcmgenet_tx_ring *ring)
2916{
2917 struct bcmgenet_priv *priv = ring->priv;
2918 u32 p_index, c_index, intsts, intmsk;
2919 struct netdev_queue *txq;
2920 unsigned int free_bds;
2921 unsigned long flags;
2922 bool txq_stopped;
2923
2924 if (!netif_msg_tx_err(priv))
2925 return;
2926
2927 txq = netdev_get_tx_queue(priv->dev, ring->queue);
2928
2929 spin_lock_irqsave(&ring->lock, flags);
2930 if (ring->index == DESC_INDEX) {
2931 intsts = ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
2932 intmsk = UMAC_IRQ_TXDMA_DONE | UMAC_IRQ_TXDMA_MBDONE;
2933 } else {
2934 intsts = ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
2935 intmsk = 1 << ring->index;
2936 }
2937 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
2938 p_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_PROD_INDEX);
2939 txq_stopped = netif_tx_queue_stopped(txq);
2940 free_bds = ring->free_bds;
2941 spin_unlock_irqrestore(&ring->lock, flags);
2942
2943 netif_err(priv, tx_err, priv->dev, "Ring %d queue %d status summary\n"
2944 "TX queue status: %s, interrupts: %s\n"
2945 "(sw)free_bds: %d (sw)size: %d\n"
2946 "(sw)p_index: %d (hw)p_index: %d\n"
2947 "(sw)c_index: %d (hw)c_index: %d\n"
2948 "(sw)clean_p: %d (sw)write_p: %d\n"
2949 "(sw)cb_ptr: %d (sw)end_ptr: %d\n",
2950 ring->index, ring->queue,
2951 txq_stopped ? "stopped" : "active",
2952 intsts & intmsk ? "enabled" : "disabled",
2953 free_bds, ring->size,
2954 ring->prod_index, p_index & DMA_P_INDEX_MASK,
2955 ring->c_index, c_index & DMA_C_INDEX_MASK,
2956 ring->clean_ptr, ring->write_ptr,
2957 ring->cb_ptr, ring->end_ptr);
2958}
2959
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002960static void bcmgenet_timeout(struct net_device *dev)
2961{
2962 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli13ea6572015-06-04 16:15:50 -07002963 u32 int0_enable = 0;
2964 u32 int1_enable = 0;
2965 unsigned int q;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002966
2967 netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
2968
Florian Fainelli13ea6572015-06-04 16:15:50 -07002969 for (q = 0; q < priv->hw_params->tx_queues; q++)
2970 bcmgenet_dump_tx_queue(&priv->tx_rings[q]);
2971 bcmgenet_dump_tx_queue(&priv->tx_rings[DESC_INDEX]);
2972
2973 bcmgenet_tx_reclaim_all(dev);
2974
2975 for (q = 0; q < priv->hw_params->tx_queues; q++)
2976 int1_enable |= (1 << q);
2977
2978 int0_enable = UMAC_IRQ_TXDMA_DONE;
2979
2980 /* Re-enable TX interrupts if disabled */
2981 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
2982 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
2983
Florian Westphal860e9532016-05-03 16:33:13 +02002984 netif_trans_update(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002985
2986 dev->stats.tx_errors++;
2987
2988 netif_tx_wake_all_queues(dev);
2989}
2990
2991#define MAX_MC_COUNT 16
2992
2993static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
2994 unsigned char *addr,
2995 int *i,
2996 int *mc)
2997{
2998 u32 reg;
2999
Florian Fainellic91b7f62014-07-23 10:42:12 -07003000 bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
3001 UMAC_MDF_ADDR + (*i * 4));
3002 bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
3003 addr[4] << 8 | addr[5],
3004 UMAC_MDF_ADDR + ((*i + 1) * 4));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003005 reg = bcmgenet_umac_readl(priv, UMAC_MDF_CTRL);
3006 reg |= (1 << (MAX_MC_COUNT - *mc));
3007 bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
3008 *i += 2;
3009 (*mc)++;
3010}
3011
3012static void bcmgenet_set_rx_mode(struct net_device *dev)
3013{
3014 struct bcmgenet_priv *priv = netdev_priv(dev);
3015 struct netdev_hw_addr *ha;
3016 int i, mc;
3017 u32 reg;
3018
3019 netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
3020
Brian Norris7fc527f2014-07-29 14:34:14 -07003021 /* Promiscuous mode */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003022 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
3023 if (dev->flags & IFF_PROMISC) {
3024 reg |= CMD_PROMISC;
3025 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3026 bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
3027 return;
3028 } else {
3029 reg &= ~CMD_PROMISC;
3030 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3031 }
3032
3033 /* UniMac doesn't support ALLMULTI */
3034 if (dev->flags & IFF_ALLMULTI) {
3035 netdev_warn(dev, "ALLMULTI is not supported\n");
3036 return;
3037 }
3038
3039 /* update MDF filter */
3040 i = 0;
3041 mc = 0;
3042 /* Broadcast */
3043 bcmgenet_set_mdf_addr(priv, dev->broadcast, &i, &mc);
3044 /* my own address.*/
3045 bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i, &mc);
3046 /* Unicast list*/
3047 if (netdev_uc_count(dev) > (MAX_MC_COUNT - mc))
3048 return;
3049
3050 if (!netdev_uc_empty(dev))
3051 netdev_for_each_uc_addr(ha, dev)
3052 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
3053 /* Multicast */
3054 if (netdev_mc_empty(dev) || netdev_mc_count(dev) >= (MAX_MC_COUNT - mc))
3055 return;
3056
3057 netdev_for_each_mc_addr(ha, dev)
3058 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
3059}
3060
3061/* Set the hardware MAC address. */
3062static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
3063{
3064 struct sockaddr *addr = p;
3065
3066 /* Setting the MAC address at the hardware level is not possible
3067 * without disabling the UniMAC RX/TX enable bits.
3068 */
3069 if (netif_running(dev))
3070 return -EBUSY;
3071
3072 ether_addr_copy(dev->dev_addr, addr->sa_data);
3073
3074 return 0;
3075}
3076
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003077static const struct net_device_ops bcmgenet_netdev_ops = {
3078 .ndo_open = bcmgenet_open,
3079 .ndo_stop = bcmgenet_close,
3080 .ndo_start_xmit = bcmgenet_xmit,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003081 .ndo_tx_timeout = bcmgenet_timeout,
3082 .ndo_set_rx_mode = bcmgenet_set_rx_mode,
3083 .ndo_set_mac_address = bcmgenet_set_mac_addr,
3084 .ndo_do_ioctl = bcmgenet_ioctl,
3085 .ndo_set_features = bcmgenet_set_features,
Florian Fainelli4d2e8882015-07-31 11:42:54 -07003086#ifdef CONFIG_NET_POLL_CONTROLLER
3087 .ndo_poll_controller = bcmgenet_poll_controller,
3088#endif
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003089};
3090
3091/* Array of GENET hardware parameters/characteristics */
3092static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
3093 [GENET_V1] = {
3094 .tx_queues = 0,
Petri Gynther51a966a2015-02-23 11:00:46 -08003095 .tx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003096 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003097 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003098 .bp_in_en_shift = 16,
3099 .bp_in_mask = 0xffff,
3100 .hfb_filter_cnt = 16,
3101 .qtag_mask = 0x1F,
3102 .hfb_offset = 0x1000,
3103 .rdma_offset = 0x2000,
3104 .tdma_offset = 0x3000,
3105 .words_per_bd = 2,
3106 },
3107 [GENET_V2] = {
3108 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003109 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003110 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003111 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003112 .bp_in_en_shift = 16,
3113 .bp_in_mask = 0xffff,
3114 .hfb_filter_cnt = 16,
3115 .qtag_mask = 0x1F,
3116 .tbuf_offset = 0x0600,
3117 .hfb_offset = 0x1000,
3118 .hfb_reg_offset = 0x2000,
3119 .rdma_offset = 0x3000,
3120 .tdma_offset = 0x4000,
3121 .words_per_bd = 2,
3122 .flags = GENET_HAS_EXT,
3123 },
3124 [GENET_V3] = {
3125 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003126 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003127 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003128 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003129 .bp_in_en_shift = 17,
3130 .bp_in_mask = 0x1ffff,
3131 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003132 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003133 .qtag_mask = 0x3F,
3134 .tbuf_offset = 0x0600,
3135 .hfb_offset = 0x8000,
3136 .hfb_reg_offset = 0xfc00,
3137 .rdma_offset = 0x10000,
3138 .tdma_offset = 0x11000,
3139 .words_per_bd = 2,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003140 .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR |
3141 GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003142 },
3143 [GENET_V4] = {
3144 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003145 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003146 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003147 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003148 .bp_in_en_shift = 17,
3149 .bp_in_mask = 0x1ffff,
3150 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003151 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003152 .qtag_mask = 0x3F,
3153 .tbuf_offset = 0x0600,
3154 .hfb_offset = 0x8000,
3155 .hfb_reg_offset = 0xfc00,
3156 .rdma_offset = 0x2000,
3157 .tdma_offset = 0x4000,
3158 .words_per_bd = 3,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003159 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3160 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003161 },
3162};
3163
3164/* Infer hardware parameters from the detected GENET version */
3165static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
3166{
3167 struct bcmgenet_hw_params *params;
3168 u32 reg;
3169 u8 major;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003170 u16 gphy_rev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003171
3172 if (GENET_IS_V4(priv)) {
3173 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3174 genet_dma_ring_regs = genet_dma_ring_regs_v4;
3175 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
3176 priv->version = GENET_V4;
3177 } else if (GENET_IS_V3(priv)) {
3178 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3179 genet_dma_ring_regs = genet_dma_ring_regs_v123;
3180 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
3181 priv->version = GENET_V3;
3182 } else if (GENET_IS_V2(priv)) {
3183 bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
3184 genet_dma_ring_regs = genet_dma_ring_regs_v123;
3185 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
3186 priv->version = GENET_V2;
3187 } else if (GENET_IS_V1(priv)) {
3188 bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
3189 genet_dma_ring_regs = genet_dma_ring_regs_v123;
3190 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
3191 priv->version = GENET_V1;
3192 }
3193
3194 /* enum genet_version starts at 1 */
3195 priv->hw_params = &bcmgenet_hw_params[priv->version];
3196 params = priv->hw_params;
3197
3198 /* Read GENET HW version */
3199 reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
3200 major = (reg >> 24 & 0x0f);
3201 if (major == 5)
3202 major = 4;
3203 else if (major == 0)
3204 major = 1;
3205 if (major != priv->version) {
3206 dev_err(&priv->pdev->dev,
3207 "GENET version mismatch, got: %d, configured for: %d\n",
3208 major, priv->version);
3209 }
3210
3211 /* Print the GENET core version */
3212 dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003213 major, (reg >> 16) & 0x0f, reg & 0xffff);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003214
Florian Fainelli487320c2014-09-19 13:07:53 -07003215 /* Store the integrated PHY revision for the MDIO probing function
3216 * to pass this information to the PHY driver. The PHY driver expects
3217 * to find the PHY major revision in bits 15:8 while the GENET register
3218 * stores that information in bits 7:0, account for that.
Florian Fainellib04a2f52014-12-03 09:56:59 -08003219 *
3220 * On newer chips, starting with PHY revision G0, a new scheme is
3221 * deployed similar to the Starfighter 2 switch with GPHY major
3222 * revision in bits 15:8 and patch level in bits 7:0. Major revision 0
3223 * is reserved as well as special value 0x01ff, we have a small
3224 * heuristic to check for the new GPHY revision and re-arrange things
3225 * so the GPHY driver is happy.
Florian Fainelli487320c2014-09-19 13:07:53 -07003226 */
Florian Fainellib04a2f52014-12-03 09:56:59 -08003227 gphy_rev = reg & 0xffff;
3228
Doug Bergereca4bad2017-03-09 16:58:45 -08003229 /* This is reserved so should require special treatment */
3230 if (gphy_rev == 0 || gphy_rev == 0x01ff) {
3231 pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev);
3232 return;
3233 }
3234
Florian Fainellib04a2f52014-12-03 09:56:59 -08003235 /* This is the good old scheme, just GPHY major, no minor nor patch */
3236 if ((gphy_rev & 0xf0) != 0)
3237 priv->gphy_rev = gphy_rev << 8;
3238
3239 /* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */
3240 else if ((gphy_rev & 0xff00) != 0)
3241 priv->gphy_rev = gphy_rev;
3242
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003243#ifdef CONFIG_PHYS_ADDR_T_64BIT
3244 if (!(params->flags & GENET_HAS_40BITS))
3245 pr_warn("GENET does not support 40-bits PA\n");
3246#endif
3247
3248 pr_debug("Configuration for version: %d\n"
Petri Gynther3feafa02015-03-05 17:40:14 -08003249 "TXq: %1d, TXqBDs: %1d, RXq: %1d, RXqBDs: %1d\n"
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003250 "BP << en: %2d, BP msk: 0x%05x\n"
3251 "HFB count: %2d, QTAQ msk: 0x%05x\n"
3252 "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
3253 "RDMA: 0x%05x, TDMA: 0x%05x\n"
3254 "Words/BD: %d\n",
3255 priv->version,
Petri Gynther51a966a2015-02-23 11:00:46 -08003256 params->tx_queues, params->tx_bds_per_q,
Petri Gynther3feafa02015-03-05 17:40:14 -08003257 params->rx_queues, params->rx_bds_per_q,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003258 params->bp_in_en_shift, params->bp_in_mask,
3259 params->hfb_filter_cnt, params->qtag_mask,
3260 params->tbuf_offset, params->hfb_offset,
3261 params->hfb_reg_offset,
3262 params->rdma_offset, params->tdma_offset,
3263 params->words_per_bd);
3264}
3265
3266static const struct of_device_id bcmgenet_match[] = {
3267 { .compatible = "brcm,genet-v1", .data = (void *)GENET_V1 },
3268 { .compatible = "brcm,genet-v2", .data = (void *)GENET_V2 },
3269 { .compatible = "brcm,genet-v3", .data = (void *)GENET_V3 },
3270 { .compatible = "brcm,genet-v4", .data = (void *)GENET_V4 },
3271 { },
3272};
Luis de Bethencourte8048e52015-09-18 17:55:02 +02003273MODULE_DEVICE_TABLE(of, bcmgenet_match);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003274
3275static int bcmgenet_probe(struct platform_device *pdev)
3276{
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003277 struct bcmgenet_platform_data *pd = pdev->dev.platform_data;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003278 struct device_node *dn = pdev->dev.of_node;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003279 const struct of_device_id *of_id = NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003280 struct bcmgenet_priv *priv;
3281 struct net_device *dev;
3282 const void *macaddr;
3283 struct resource *r;
3284 int err = -EIO;
3285
Petri Gynther3feafee2015-03-05 17:40:12 -08003286 /* Up to GENET_MAX_MQ_CNT + 1 TX queues and RX queues */
3287 dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1,
3288 GENET_MAX_MQ_CNT + 1);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003289 if (!dev) {
3290 dev_err(&pdev->dev, "can't allocate net device\n");
3291 return -ENOMEM;
3292 }
3293
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003294 if (dn) {
3295 of_id = of_match_node(bcmgenet_match, dn);
3296 if (!of_id)
3297 return -EINVAL;
3298 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003299
3300 priv = netdev_priv(dev);
3301 priv->irq0 = platform_get_irq(pdev, 0);
3302 priv->irq1 = platform_get_irq(pdev, 1);
Florian Fainelli85620562014-07-21 15:29:23 -07003303 priv->wol_irq = platform_get_irq(pdev, 2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003304 if (!priv->irq0 || !priv->irq1) {
3305 dev_err(&pdev->dev, "can't find IRQs\n");
3306 err = -EINVAL;
3307 goto err;
3308 }
3309
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003310 if (dn) {
3311 macaddr = of_get_mac_address(dn);
3312 if (!macaddr) {
3313 dev_err(&pdev->dev, "can't find MAC address\n");
3314 err = -EINVAL;
3315 goto err;
3316 }
3317 } else {
3318 macaddr = pd->mac_address;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003319 }
3320
3321 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam5343a102014-02-24 00:47:24 -03003322 priv->base = devm_ioremap_resource(&pdev->dev, r);
3323 if (IS_ERR(priv->base)) {
3324 err = PTR_ERR(priv->base);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003325 goto err;
3326 }
3327
3328 SET_NETDEV_DEV(dev, &pdev->dev);
3329 dev_set_drvdata(&pdev->dev, dev);
3330 ether_addr_copy(dev->dev_addr, macaddr);
3331 dev->watchdog_timeo = 2 * HZ;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003332 dev->ethtool_ops = &bcmgenet_ethtool_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003333 dev->netdev_ops = &bcmgenet_netdev_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003334
3335 priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
3336
3337 /* Set hardware features */
3338 dev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM |
3339 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
3340
Florian Fainelli85620562014-07-21 15:29:23 -07003341 /* Request the WOL interrupt and advertise suspend if available */
3342 priv->wol_irq_disabled = true;
3343 err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0,
3344 dev->name, priv);
3345 if (!err)
3346 device_set_wakeup_capable(&pdev->dev, 1);
3347
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003348 /* Set the needed headroom to account for any possible
3349 * features enabling/disabling at runtime
3350 */
3351 dev->needed_headroom += 64;
3352
3353 netdev_boot_setup_check(dev);
3354
3355 priv->dev = dev;
3356 priv->pdev = pdev;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003357 if (of_id)
3358 priv->version = (enum bcmgenet_version)of_id->data;
3359 else
3360 priv->version = pd->genet_version;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003361
Florian Fainellie4a60a92014-08-11 14:50:42 -07003362 priv->clk = devm_clk_get(&priv->pdev->dev, "enet");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003363 if (IS_ERR(priv->clk)) {
Florian Fainellie4a60a92014-08-11 14:50:42 -07003364 dev_warn(&priv->pdev->dev, "failed to get enet clock\n");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003365 priv->clk = NULL;
3366 }
Florian Fainellie4a60a92014-08-11 14:50:42 -07003367
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003368 clk_prepare_enable(priv->clk);
Florian Fainellie4a60a92014-08-11 14:50:42 -07003369
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003370 bcmgenet_set_hw_params(priv);
3371
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003372 /* Mii wait queue */
3373 init_waitqueue_head(&priv->wq);
3374 /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
3375 priv->rx_buf_len = RX_BUF_LENGTH;
3376 INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
3377
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003378 priv->clk_wol = devm_clk_get(&priv->pdev->dev, "enet-wol");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003379 if (IS_ERR(priv->clk_wol)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003380 dev_warn(&priv->pdev->dev, "failed to get enet-wol clock\n");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003381 priv->clk_wol = NULL;
3382 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003383
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003384 priv->clk_eee = devm_clk_get(&priv->pdev->dev, "enet-eee");
3385 if (IS_ERR(priv->clk_eee)) {
3386 dev_warn(&priv->pdev->dev, "failed to get enet-eee clock\n");
3387 priv->clk_eee = NULL;
3388 }
3389
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003390 err = reset_umac(priv);
3391 if (err)
3392 goto err_clk_disable;
3393
3394 err = bcmgenet_mii_init(dev);
3395 if (err)
3396 goto err_clk_disable;
3397
3398 /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
3399 * just the ring 16 descriptor based TX
3400 */
3401 netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
3402 netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
3403
Florian Fainelli219575e2014-06-26 10:26:21 -07003404 /* libphy will determine the link state */
3405 netif_carrier_off(dev);
3406
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003407 /* Turn off the main clock, WOL clock is handled separately */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003408 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003409
Florian Fainelli0f50ce92014-06-26 10:26:20 -07003410 err = register_netdev(dev);
3411 if (err)
3412 goto err;
3413
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003414 return err;
3415
3416err_clk_disable:
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003417 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003418err:
3419 free_netdev(dev);
3420 return err;
3421}
3422
3423static int bcmgenet_remove(struct platform_device *pdev)
3424{
3425 struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
3426
3427 dev_set_drvdata(&pdev->dev, NULL);
3428 unregister_netdev(priv->dev);
3429 bcmgenet_mii_exit(priv->dev);
3430 free_netdev(priv->dev);
3431
3432 return 0;
3433}
3434
Florian Fainellib6e978e2014-07-21 15:29:22 -07003435#ifdef CONFIG_PM_SLEEP
3436static int bcmgenet_suspend(struct device *d)
3437{
3438 struct net_device *dev = dev_get_drvdata(d);
3439 struct bcmgenet_priv *priv = netdev_priv(dev);
3440 int ret;
3441
3442 if (!netif_running(dev))
3443 return 0;
3444
3445 bcmgenet_netif_stop(dev);
3446
Florian Fainelli0299b6a2016-09-26 22:31:56 +02003447 phy_suspend(priv->phydev);
Florian Fainellicc013fb2014-08-11 14:50:43 -07003448
Florian Fainellib6e978e2014-07-21 15:29:22 -07003449 netif_device_detach(dev);
3450
3451 /* Disable MAC receive */
3452 umac_enable_set(priv, CMD_RX_EN, false);
3453
3454 ret = bcmgenet_dma_teardown(priv);
3455 if (ret)
3456 return ret;
3457
3458 /* Disable MAC transmit. TX DMA disabled have to done before this */
3459 umac_enable_set(priv, CMD_TX_EN, false);
3460
3461 /* tx reclaim */
3462 bcmgenet_tx_reclaim_all(dev);
3463 bcmgenet_fini_dma(priv);
3464
Florian Fainelli8c90db72014-07-21 15:29:28 -07003465 /* Prepare the device for Wake-on-LAN and switch to the slow clock */
3466 if (device_may_wakeup(d) && priv->wolopts) {
Florian Fainellica8cf342015-03-23 15:09:51 -07003467 ret = bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003468 clk_prepare_enable(priv->clk_wol);
Florian Fainellic624f892015-07-16 15:51:17 -07003469 } else if (priv->internal_phy) {
Florian Fainellia6f31f52015-03-23 15:09:57 -07003470 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003471 }
3472
Florian Fainellib6e978e2014-07-21 15:29:22 -07003473 /* Turn off the clocks */
3474 clk_disable_unprepare(priv->clk);
3475
Florian Fainellica8cf342015-03-23 15:09:51 -07003476 return ret;
Florian Fainellib6e978e2014-07-21 15:29:22 -07003477}
3478
3479static int bcmgenet_resume(struct device *d)
3480{
3481 struct net_device *dev = dev_get_drvdata(d);
3482 struct bcmgenet_priv *priv = netdev_priv(dev);
3483 unsigned long dma_ctrl;
3484 int ret;
3485 u32 reg;
3486
3487 if (!netif_running(dev))
3488 return 0;
3489
3490 /* Turn on the clock */
3491 ret = clk_prepare_enable(priv->clk);
3492 if (ret)
3493 return ret;
3494
Florian Fainellia6f31f52015-03-23 15:09:57 -07003495 /* If this is an internal GPHY, power it back on now, before UniMAC is
3496 * brought out of reset as absolutely no UniMAC activity is allowed
3497 */
Florian Fainellic624f892015-07-16 15:51:17 -07003498 if (priv->internal_phy)
Florian Fainellia6f31f52015-03-23 15:09:57 -07003499 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3500
Florian Fainellib6e978e2014-07-21 15:29:22 -07003501 bcmgenet_umac_reset(priv);
3502
3503 ret = init_umac(priv);
3504 if (ret)
3505 goto out_clk_disable;
3506
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003507 /* From WOL-enabled suspend, switch to regular clock */
3508 if (priv->wolopts)
3509 clk_disable_unprepare(priv->clk_wol);
3510
Florian Fainelli0299b6a2016-09-26 22:31:56 +02003511 phy_init_hw(priv->phydev);
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003512 /* Speed settings must be restored */
Florian Fainelli28b45912015-07-16 15:51:19 -07003513 bcmgenet_mii_config(priv->dev);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003514
Florian Fainellib6e978e2014-07-21 15:29:22 -07003515 /* disable ethernet MAC while updating its registers */
3516 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
3517
3518 bcmgenet_set_hw_addr(priv, dev->dev_addr);
3519
Florian Fainellic624f892015-07-16 15:51:17 -07003520 if (priv->internal_phy) {
Florian Fainellib6e978e2014-07-21 15:29:22 -07003521 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
3522 reg |= EXT_ENERGY_DET_MASK;
3523 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
3524 }
3525
Florian Fainelli98bb7392014-08-11 14:50:45 -07003526 if (priv->wolopts)
3527 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
3528
Florian Fainellib6e978e2014-07-21 15:29:22 -07003529 /* Disable RX/TX DMA and flush TX queues */
3530 dma_ctrl = bcmgenet_dma_disable(priv);
3531
3532 /* Reinitialize TDMA and RDMA and SW housekeeping */
3533 ret = bcmgenet_init_dma(priv);
3534 if (ret) {
3535 netdev_err(dev, "failed to initialize DMA\n");
3536 goto out_clk_disable;
3537 }
3538
3539 /* Always enable ring 16 - descriptor ring */
3540 bcmgenet_enable_dma(priv, dma_ctrl);
3541
3542 netif_device_attach(dev);
3543
Florian Fainelli0299b6a2016-09-26 22:31:56 +02003544 phy_resume(priv->phydev);
Florian Fainellicc013fb2014-08-11 14:50:43 -07003545
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003546 if (priv->eee.eee_enabled)
3547 bcmgenet_eee_enable_set(dev, true);
3548
Florian Fainellib6e978e2014-07-21 15:29:22 -07003549 bcmgenet_netif_start(dev);
3550
3551 return 0;
3552
3553out_clk_disable:
3554 clk_disable_unprepare(priv->clk);
3555 return ret;
3556}
3557#endif /* CONFIG_PM_SLEEP */
3558
3559static SIMPLE_DEV_PM_OPS(bcmgenet_pm_ops, bcmgenet_suspend, bcmgenet_resume);
3560
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003561static struct platform_driver bcmgenet_driver = {
3562 .probe = bcmgenet_probe,
3563 .remove = bcmgenet_remove,
3564 .driver = {
3565 .name = "bcmgenet",
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003566 .of_match_table = bcmgenet_match,
Florian Fainellib6e978e2014-07-21 15:29:22 -07003567 .pm = &bcmgenet_pm_ops,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003568 },
3569};
3570module_platform_driver(bcmgenet_driver);
3571
3572MODULE_AUTHOR("Broadcom Corporation");
3573MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
3574MODULE_ALIAS("platform:bcmgenet");
3575MODULE_LICENSE("GPL");