blob: 2c008b09c4e36a9141a8ceab077d75b6393ca343 [file] [log] [blame]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001/*
2 * Broadcom GENET (Gigabit Ethernet) controller driver
3 *
Doug Bergerc298ede2017-03-13 17:41:33 -07004 * Copyright (c) 2014-2017 Broadcom
Florian Fainelli1c1008c2014-02-13 16:08:47 -08005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
Florian Fainelli1c1008c2014-02-13 16:08:47 -08009 */
10
11#define pr_fmt(fmt) "bcmgenet: " fmt
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/sched.h>
16#include <linux/types.h>
17#include <linux/fcntl.h>
18#include <linux/interrupt.h>
19#include <linux/string.h>
20#include <linux/if_ether.h>
21#include <linux/init.h>
22#include <linux/errno.h>
23#include <linux/delay.h>
24#include <linux/platform_device.h>
25#include <linux/dma-mapping.h>
26#include <linux/pm.h>
27#include <linux/clk.h>
Florian Fainelli1c1008c2014-02-13 16:08:47 -080028#include <linux/of.h>
29#include <linux/of_address.h>
30#include <linux/of_irq.h>
31#include <linux/of_net.h>
32#include <linux/of_platform.h>
33#include <net/arp.h>
34
35#include <linux/mii.h>
36#include <linux/ethtool.h>
37#include <linux/netdevice.h>
38#include <linux/inetdevice.h>
39#include <linux/etherdevice.h>
40#include <linux/skbuff.h>
41#include <linux/in.h>
42#include <linux/ip.h>
43#include <linux/ipv6.h>
44#include <linux/phy.h>
Petri Gyntherb0ba5122014-12-01 16:18:08 -080045#include <linux/platform_data/bcmgenet.h>
Florian Fainelli1c1008c2014-02-13 16:08:47 -080046
47#include <asm/unaligned.h>
48
49#include "bcmgenet.h"
50
51/* Maximum number of hardware queues, downsized if needed */
52#define GENET_MAX_MQ_CNT 4
53
54/* Default highest priority queue for multi queue support */
55#define GENET_Q0_PRIORITY 0
56
Petri Gynther3feafa02015-03-05 17:40:14 -080057#define GENET_Q16_RX_BD_CNT \
58 (TOTAL_DESC - priv->hw_params->rx_queues * priv->hw_params->rx_bds_per_q)
Petri Gynther51a966a2015-02-23 11:00:46 -080059#define GENET_Q16_TX_BD_CNT \
60 (TOTAL_DESC - priv->hw_params->tx_queues * priv->hw_params->tx_bds_per_q)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080061
62#define RX_BUF_LENGTH 2048
63#define SKB_ALIGNMENT 32
64
65/* Tx/Rx DMA register offset, skip 256 descriptors */
66#define WORDS_PER_BD(p) (p->hw_params->words_per_bd)
67#define DMA_DESC_SIZE (WORDS_PER_BD(priv) * sizeof(u32))
68
69#define GENET_TDMA_REG_OFF (priv->hw_params->tdma_offset + \
70 TOTAL_DESC * DMA_DESC_SIZE)
71
72#define GENET_RDMA_REG_OFF (priv->hw_params->rdma_offset + \
73 TOTAL_DESC * DMA_DESC_SIZE)
74
75static inline void dmadesc_set_length_status(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -070076 void __iomem *d, u32 value)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080077{
78 __raw_writel(value, d + DMA_DESC_LENGTH_STATUS);
79}
80
81static inline u32 dmadesc_get_length_status(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -070082 void __iomem *d)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080083{
84 return __raw_readl(d + DMA_DESC_LENGTH_STATUS);
85}
86
87static inline void dmadesc_set_addr(struct bcmgenet_priv *priv,
88 void __iomem *d,
89 dma_addr_t addr)
90{
91 __raw_writel(lower_32_bits(addr), d + DMA_DESC_ADDRESS_LO);
92
93 /* Register writes to GISB bus can take couple hundred nanoseconds
94 * and are done for each packet, save these expensive writes unless
Brian Norris7fc527f2014-07-29 14:34:14 -070095 * the platform is explicitly configured for 64-bits/LPAE.
Florian Fainelli1c1008c2014-02-13 16:08:47 -080096 */
97#ifdef CONFIG_PHYS_ADDR_T_64BIT
98 if (priv->hw_params->flags & GENET_HAS_40BITS)
99 __raw_writel(upper_32_bits(addr), d + DMA_DESC_ADDRESS_HI);
100#endif
101}
102
103/* Combined address + length/status setter */
104static inline void dmadesc_set(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700105 void __iomem *d, dma_addr_t addr, u32 val)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800106{
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800107 dmadesc_set_addr(priv, d, addr);
Petri Gynther7ee40622016-04-05 14:00:01 -0700108 dmadesc_set_length_status(priv, d, val);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800109}
110
111static inline dma_addr_t dmadesc_get_addr(struct bcmgenet_priv *priv,
112 void __iomem *d)
113{
114 dma_addr_t addr;
115
116 addr = __raw_readl(d + DMA_DESC_ADDRESS_LO);
117
118 /* Register writes to GISB bus can take couple hundred nanoseconds
119 * and are done for each packet, save these expensive writes unless
Brian Norris7fc527f2014-07-29 14:34:14 -0700120 * the platform is explicitly configured for 64-bits/LPAE.
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800121 */
122#ifdef CONFIG_PHYS_ADDR_T_64BIT
123 if (priv->hw_params->flags & GENET_HAS_40BITS)
124 addr |= (u64)__raw_readl(d + DMA_DESC_ADDRESS_HI) << 32;
125#endif
126 return addr;
127}
128
129#define GENET_VER_FMT "%1d.%1d EPHY: 0x%04x"
130
131#define GENET_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
132 NETIF_MSG_LINK)
133
134static inline u32 bcmgenet_rbuf_ctrl_get(struct bcmgenet_priv *priv)
135{
136 if (GENET_IS_V1(priv))
137 return bcmgenet_rbuf_readl(priv, RBUF_FLUSH_CTRL_V1);
138 else
139 return bcmgenet_sys_readl(priv, SYS_RBUF_FLUSH_CTRL);
140}
141
142static inline void bcmgenet_rbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
143{
144 if (GENET_IS_V1(priv))
145 bcmgenet_rbuf_writel(priv, val, RBUF_FLUSH_CTRL_V1);
146 else
147 bcmgenet_sys_writel(priv, val, SYS_RBUF_FLUSH_CTRL);
148}
149
150/* These macros are defined to deal with register map change
151 * between GENET1.1 and GENET2. Only those currently being used
152 * by driver are defined.
153 */
154static inline u32 bcmgenet_tbuf_ctrl_get(struct bcmgenet_priv *priv)
155{
156 if (GENET_IS_V1(priv))
157 return bcmgenet_rbuf_readl(priv, TBUF_CTRL_V1);
158 else
159 return __raw_readl(priv->base +
160 priv->hw_params->tbuf_offset + TBUF_CTRL);
161}
162
163static inline void bcmgenet_tbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
164{
165 if (GENET_IS_V1(priv))
166 bcmgenet_rbuf_writel(priv, val, TBUF_CTRL_V1);
167 else
168 __raw_writel(val, priv->base +
169 priv->hw_params->tbuf_offset + TBUF_CTRL);
170}
171
172static inline u32 bcmgenet_bp_mc_get(struct bcmgenet_priv *priv)
173{
174 if (GENET_IS_V1(priv))
175 return bcmgenet_rbuf_readl(priv, TBUF_BP_MC_V1);
176 else
177 return __raw_readl(priv->base +
178 priv->hw_params->tbuf_offset + TBUF_BP_MC);
179}
180
181static inline void bcmgenet_bp_mc_set(struct bcmgenet_priv *priv, u32 val)
182{
183 if (GENET_IS_V1(priv))
184 bcmgenet_rbuf_writel(priv, val, TBUF_BP_MC_V1);
185 else
186 __raw_writel(val, priv->base +
187 priv->hw_params->tbuf_offset + TBUF_BP_MC);
188}
189
190/* RX/TX DMA register accessors */
191enum dma_reg {
192 DMA_RING_CFG = 0,
193 DMA_CTRL,
194 DMA_STATUS,
195 DMA_SCB_BURST_SIZE,
196 DMA_ARB_CTRL,
Petri Gynther37742162014-10-07 09:30:01 -0700197 DMA_PRIORITY_0,
198 DMA_PRIORITY_1,
199 DMA_PRIORITY_2,
Petri Gynther0034de42015-03-13 14:45:00 -0700200 DMA_INDEX2RING_0,
201 DMA_INDEX2RING_1,
202 DMA_INDEX2RING_2,
203 DMA_INDEX2RING_3,
204 DMA_INDEX2RING_4,
205 DMA_INDEX2RING_5,
206 DMA_INDEX2RING_6,
207 DMA_INDEX2RING_7,
Florian Fainelli4a296452015-09-16 16:47:40 -0700208 DMA_RING0_TIMEOUT,
209 DMA_RING1_TIMEOUT,
210 DMA_RING2_TIMEOUT,
211 DMA_RING3_TIMEOUT,
212 DMA_RING4_TIMEOUT,
213 DMA_RING5_TIMEOUT,
214 DMA_RING6_TIMEOUT,
215 DMA_RING7_TIMEOUT,
216 DMA_RING8_TIMEOUT,
217 DMA_RING9_TIMEOUT,
218 DMA_RING10_TIMEOUT,
219 DMA_RING11_TIMEOUT,
220 DMA_RING12_TIMEOUT,
221 DMA_RING13_TIMEOUT,
222 DMA_RING14_TIMEOUT,
223 DMA_RING15_TIMEOUT,
224 DMA_RING16_TIMEOUT,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800225};
226
227static const u8 bcmgenet_dma_regs_v3plus[] = {
228 [DMA_RING_CFG] = 0x00,
229 [DMA_CTRL] = 0x04,
230 [DMA_STATUS] = 0x08,
231 [DMA_SCB_BURST_SIZE] = 0x0C,
232 [DMA_ARB_CTRL] = 0x2C,
Petri Gynther37742162014-10-07 09:30:01 -0700233 [DMA_PRIORITY_0] = 0x30,
234 [DMA_PRIORITY_1] = 0x34,
235 [DMA_PRIORITY_2] = 0x38,
Florian Fainelli4a296452015-09-16 16:47:40 -0700236 [DMA_RING0_TIMEOUT] = 0x2C,
237 [DMA_RING1_TIMEOUT] = 0x30,
238 [DMA_RING2_TIMEOUT] = 0x34,
239 [DMA_RING3_TIMEOUT] = 0x38,
240 [DMA_RING4_TIMEOUT] = 0x3c,
241 [DMA_RING5_TIMEOUT] = 0x40,
242 [DMA_RING6_TIMEOUT] = 0x44,
243 [DMA_RING7_TIMEOUT] = 0x48,
244 [DMA_RING8_TIMEOUT] = 0x4c,
245 [DMA_RING9_TIMEOUT] = 0x50,
246 [DMA_RING10_TIMEOUT] = 0x54,
247 [DMA_RING11_TIMEOUT] = 0x58,
248 [DMA_RING12_TIMEOUT] = 0x5c,
249 [DMA_RING13_TIMEOUT] = 0x60,
250 [DMA_RING14_TIMEOUT] = 0x64,
251 [DMA_RING15_TIMEOUT] = 0x68,
252 [DMA_RING16_TIMEOUT] = 0x6C,
Petri Gynther0034de42015-03-13 14:45:00 -0700253 [DMA_INDEX2RING_0] = 0x70,
254 [DMA_INDEX2RING_1] = 0x74,
255 [DMA_INDEX2RING_2] = 0x78,
256 [DMA_INDEX2RING_3] = 0x7C,
257 [DMA_INDEX2RING_4] = 0x80,
258 [DMA_INDEX2RING_5] = 0x84,
259 [DMA_INDEX2RING_6] = 0x88,
260 [DMA_INDEX2RING_7] = 0x8C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800261};
262
263static const u8 bcmgenet_dma_regs_v2[] = {
264 [DMA_RING_CFG] = 0x00,
265 [DMA_CTRL] = 0x04,
266 [DMA_STATUS] = 0x08,
267 [DMA_SCB_BURST_SIZE] = 0x0C,
268 [DMA_ARB_CTRL] = 0x30,
Petri Gynther37742162014-10-07 09:30:01 -0700269 [DMA_PRIORITY_0] = 0x34,
270 [DMA_PRIORITY_1] = 0x38,
271 [DMA_PRIORITY_2] = 0x3C,
Florian Fainelli4a296452015-09-16 16:47:40 -0700272 [DMA_RING0_TIMEOUT] = 0x2C,
273 [DMA_RING1_TIMEOUT] = 0x30,
274 [DMA_RING2_TIMEOUT] = 0x34,
275 [DMA_RING3_TIMEOUT] = 0x38,
276 [DMA_RING4_TIMEOUT] = 0x3c,
277 [DMA_RING5_TIMEOUT] = 0x40,
278 [DMA_RING6_TIMEOUT] = 0x44,
279 [DMA_RING7_TIMEOUT] = 0x48,
280 [DMA_RING8_TIMEOUT] = 0x4c,
281 [DMA_RING9_TIMEOUT] = 0x50,
282 [DMA_RING10_TIMEOUT] = 0x54,
283 [DMA_RING11_TIMEOUT] = 0x58,
284 [DMA_RING12_TIMEOUT] = 0x5c,
285 [DMA_RING13_TIMEOUT] = 0x60,
286 [DMA_RING14_TIMEOUT] = 0x64,
287 [DMA_RING15_TIMEOUT] = 0x68,
288 [DMA_RING16_TIMEOUT] = 0x6C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800289};
290
291static const u8 bcmgenet_dma_regs_v1[] = {
292 [DMA_CTRL] = 0x00,
293 [DMA_STATUS] = 0x04,
294 [DMA_SCB_BURST_SIZE] = 0x0C,
295 [DMA_ARB_CTRL] = 0x30,
Petri Gynther37742162014-10-07 09:30:01 -0700296 [DMA_PRIORITY_0] = 0x34,
297 [DMA_PRIORITY_1] = 0x38,
298 [DMA_PRIORITY_2] = 0x3C,
Florian Fainelli4a296452015-09-16 16:47:40 -0700299 [DMA_RING0_TIMEOUT] = 0x2C,
300 [DMA_RING1_TIMEOUT] = 0x30,
301 [DMA_RING2_TIMEOUT] = 0x34,
302 [DMA_RING3_TIMEOUT] = 0x38,
303 [DMA_RING4_TIMEOUT] = 0x3c,
304 [DMA_RING5_TIMEOUT] = 0x40,
305 [DMA_RING6_TIMEOUT] = 0x44,
306 [DMA_RING7_TIMEOUT] = 0x48,
307 [DMA_RING8_TIMEOUT] = 0x4c,
308 [DMA_RING9_TIMEOUT] = 0x50,
309 [DMA_RING10_TIMEOUT] = 0x54,
310 [DMA_RING11_TIMEOUT] = 0x58,
311 [DMA_RING12_TIMEOUT] = 0x5c,
312 [DMA_RING13_TIMEOUT] = 0x60,
313 [DMA_RING14_TIMEOUT] = 0x64,
314 [DMA_RING15_TIMEOUT] = 0x68,
315 [DMA_RING16_TIMEOUT] = 0x6C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800316};
317
318/* Set at runtime once bcmgenet version is known */
319static const u8 *bcmgenet_dma_regs;
320
321static inline struct bcmgenet_priv *dev_to_priv(struct device *dev)
322{
323 return netdev_priv(dev_get_drvdata(dev));
324}
325
326static inline u32 bcmgenet_tdma_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700327 enum dma_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800328{
329 return __raw_readl(priv->base + GENET_TDMA_REG_OFF +
330 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
331}
332
333static inline void bcmgenet_tdma_writel(struct bcmgenet_priv *priv,
334 u32 val, enum dma_reg r)
335{
336 __raw_writel(val, priv->base + GENET_TDMA_REG_OFF +
337 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
338}
339
340static inline u32 bcmgenet_rdma_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700341 enum dma_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800342{
343 return __raw_readl(priv->base + GENET_RDMA_REG_OFF +
344 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
345}
346
347static inline void bcmgenet_rdma_writel(struct bcmgenet_priv *priv,
348 u32 val, enum dma_reg r)
349{
350 __raw_writel(val, priv->base + GENET_RDMA_REG_OFF +
351 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
352}
353
354/* RDMA/TDMA ring registers and accessors
355 * we merge the common fields and just prefix with T/D the registers
356 * having different meaning depending on the direction
357 */
358enum dma_ring_reg {
359 TDMA_READ_PTR = 0,
360 RDMA_WRITE_PTR = TDMA_READ_PTR,
361 TDMA_READ_PTR_HI,
362 RDMA_WRITE_PTR_HI = TDMA_READ_PTR_HI,
363 TDMA_CONS_INDEX,
364 RDMA_PROD_INDEX = TDMA_CONS_INDEX,
365 TDMA_PROD_INDEX,
366 RDMA_CONS_INDEX = TDMA_PROD_INDEX,
367 DMA_RING_BUF_SIZE,
368 DMA_START_ADDR,
369 DMA_START_ADDR_HI,
370 DMA_END_ADDR,
371 DMA_END_ADDR_HI,
372 DMA_MBUF_DONE_THRESH,
373 TDMA_FLOW_PERIOD,
374 RDMA_XON_XOFF_THRESH = TDMA_FLOW_PERIOD,
375 TDMA_WRITE_PTR,
376 RDMA_READ_PTR = TDMA_WRITE_PTR,
377 TDMA_WRITE_PTR_HI,
378 RDMA_READ_PTR_HI = TDMA_WRITE_PTR_HI
379};
380
381/* GENET v4 supports 40-bits pointer addressing
382 * for obvious reasons the LO and HI word parts
383 * are contiguous, but this offsets the other
384 * registers.
385 */
386static const u8 genet_dma_ring_regs_v4[] = {
387 [TDMA_READ_PTR] = 0x00,
388 [TDMA_READ_PTR_HI] = 0x04,
389 [TDMA_CONS_INDEX] = 0x08,
390 [TDMA_PROD_INDEX] = 0x0C,
391 [DMA_RING_BUF_SIZE] = 0x10,
392 [DMA_START_ADDR] = 0x14,
393 [DMA_START_ADDR_HI] = 0x18,
394 [DMA_END_ADDR] = 0x1C,
395 [DMA_END_ADDR_HI] = 0x20,
396 [DMA_MBUF_DONE_THRESH] = 0x24,
397 [TDMA_FLOW_PERIOD] = 0x28,
398 [TDMA_WRITE_PTR] = 0x2C,
399 [TDMA_WRITE_PTR_HI] = 0x30,
400};
401
402static const u8 genet_dma_ring_regs_v123[] = {
403 [TDMA_READ_PTR] = 0x00,
404 [TDMA_CONS_INDEX] = 0x04,
405 [TDMA_PROD_INDEX] = 0x08,
406 [DMA_RING_BUF_SIZE] = 0x0C,
407 [DMA_START_ADDR] = 0x10,
408 [DMA_END_ADDR] = 0x14,
409 [DMA_MBUF_DONE_THRESH] = 0x18,
410 [TDMA_FLOW_PERIOD] = 0x1C,
411 [TDMA_WRITE_PTR] = 0x20,
412};
413
414/* Set at runtime once GENET version is known */
415static const u8 *genet_dma_ring_regs;
416
417static inline u32 bcmgenet_tdma_ring_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700418 unsigned int ring,
419 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800420{
421 return __raw_readl(priv->base + GENET_TDMA_REG_OFF +
422 (DMA_RING_SIZE * ring) +
423 genet_dma_ring_regs[r]);
424}
425
426static inline void bcmgenet_tdma_ring_writel(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700427 unsigned int ring, u32 val,
428 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800429{
430 __raw_writel(val, priv->base + GENET_TDMA_REG_OFF +
431 (DMA_RING_SIZE * ring) +
432 genet_dma_ring_regs[r]);
433}
434
435static inline u32 bcmgenet_rdma_ring_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700436 unsigned int ring,
437 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800438{
439 return __raw_readl(priv->base + GENET_RDMA_REG_OFF +
440 (DMA_RING_SIZE * ring) +
441 genet_dma_ring_regs[r]);
442}
443
444static inline void bcmgenet_rdma_ring_writel(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700445 unsigned int ring, u32 val,
446 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800447{
448 __raw_writel(val, priv->base + GENET_RDMA_REG_OFF +
449 (DMA_RING_SIZE * ring) +
450 genet_dma_ring_regs[r]);
451}
452
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,
781 UMAC_RBUF_OVFL_CNT),
782 STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt, UMAC_RBUF_ERR_CNT),
783 STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT),
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800784 STAT_GENET_SOFT_MIB("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
785 STAT_GENET_SOFT_MIB("rx_dma_failed", mib.rx_dma_failed),
786 STAT_GENET_SOFT_MIB("tx_dma_failed", mib.tx_dma_failed),
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800787};
788
789#define BCMGENET_STATS_LEN ARRAY_SIZE(bcmgenet_gstrings_stats)
790
791static void bcmgenet_get_drvinfo(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700792 struct ethtool_drvinfo *info)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800793{
794 strlcpy(info->driver, "bcmgenet", sizeof(info->driver));
795 strlcpy(info->version, "v2.0", sizeof(info->version));
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800796}
797
798static int bcmgenet_get_sset_count(struct net_device *dev, int string_set)
799{
800 switch (string_set) {
801 case ETH_SS_STATS:
802 return BCMGENET_STATS_LEN;
803 default:
804 return -EOPNOTSUPP;
805 }
806}
807
Florian Fainellic91b7f62014-07-23 10:42:12 -0700808static void bcmgenet_get_strings(struct net_device *dev, u32 stringset,
809 u8 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800810{
811 int i;
812
813 switch (stringset) {
814 case ETH_SS_STATS:
815 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
816 memcpy(data + i * ETH_GSTRING_LEN,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700817 bcmgenet_gstrings_stats[i].stat_string,
818 ETH_GSTRING_LEN);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800819 }
820 break;
821 }
822}
823
824static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv)
825{
826 int i, j = 0;
827
828 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
829 const struct bcmgenet_stats *s;
830 u8 offset = 0;
831 u32 val = 0;
832 char *p;
833
834 s = &bcmgenet_gstrings_stats[i];
835 switch (s->type) {
836 case BCMGENET_STAT_NETDEV:
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800837 case BCMGENET_STAT_SOFT:
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800838 continue;
839 case BCMGENET_STAT_MIB_RX:
840 case BCMGENET_STAT_MIB_TX:
841 case BCMGENET_STAT_RUNT:
842 if (s->type != BCMGENET_STAT_MIB_RX)
843 offset = BCMGENET_STAT_OFFSET;
Florian Fainellic91b7f62014-07-23 10:42:12 -0700844 val = bcmgenet_umac_readl(priv,
845 UMAC_MIB_START + j + offset);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800846 break;
847 case BCMGENET_STAT_MISC:
848 val = bcmgenet_umac_readl(priv, s->reg_offset);
849 /* clear if overflowed */
850 if (val == ~0)
851 bcmgenet_umac_writel(priv, 0, s->reg_offset);
852 break;
853 }
854
855 j += s->stat_sizeof;
856 p = (char *)priv + s->stat_offset;
857 *(u32 *)p = val;
858 }
859}
860
861static void bcmgenet_get_ethtool_stats(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700862 struct ethtool_stats *stats,
863 u64 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800864{
865 struct bcmgenet_priv *priv = netdev_priv(dev);
866 int i;
867
868 if (netif_running(dev))
869 bcmgenet_update_mib_counters(priv);
870
871 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
872 const struct bcmgenet_stats *s;
873 char *p;
874
875 s = &bcmgenet_gstrings_stats[i];
876 if (s->type == BCMGENET_STAT_NETDEV)
877 p = (char *)&dev->stats;
878 else
879 p = (char *)priv;
880 p += s->stat_offset;
Eric Dumazet6517eb52016-04-15 10:47:52 -0700881 if (sizeof(unsigned long) != sizeof(u32) &&
882 s->stat_sizeof == sizeof(unsigned long))
883 data[i] = *(unsigned long *)p;
884 else
885 data[i] = *(u32 *)p;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800886 }
887}
888
Florian Fainelli6ef398e2014-11-25 21:16:35 -0800889static void bcmgenet_eee_enable_set(struct net_device *dev, bool enable)
890{
891 struct bcmgenet_priv *priv = netdev_priv(dev);
892 u32 off = priv->hw_params->tbuf_offset + TBUF_ENERGY_CTRL;
893 u32 reg;
894
895 if (enable && !priv->clk_eee_enabled) {
896 clk_prepare_enable(priv->clk_eee);
897 priv->clk_eee_enabled = true;
898 }
899
900 reg = bcmgenet_umac_readl(priv, UMAC_EEE_CTRL);
901 if (enable)
902 reg |= EEE_EN;
903 else
904 reg &= ~EEE_EN;
905 bcmgenet_umac_writel(priv, reg, UMAC_EEE_CTRL);
906
907 /* Enable EEE and switch to a 27Mhz clock automatically */
908 reg = __raw_readl(priv->base + off);
909 if (enable)
910 reg |= TBUF_EEE_EN | TBUF_PM_EN;
911 else
912 reg &= ~(TBUF_EEE_EN | TBUF_PM_EN);
913 __raw_writel(reg, priv->base + off);
914
915 /* Do the same for thing for RBUF */
916 reg = bcmgenet_rbuf_readl(priv, RBUF_ENERGY_CTRL);
917 if (enable)
918 reg |= RBUF_EEE_EN | RBUF_PM_EN;
919 else
920 reg &= ~(RBUF_EEE_EN | RBUF_PM_EN);
921 bcmgenet_rbuf_writel(priv, reg, RBUF_ENERGY_CTRL);
922
923 if (!enable && priv->clk_eee_enabled) {
924 clk_disable_unprepare(priv->clk_eee);
925 priv->clk_eee_enabled = false;
926 }
927
928 priv->eee.eee_enabled = enable;
929 priv->eee.eee_active = enable;
930}
931
932static int bcmgenet_get_eee(struct net_device *dev, struct ethtool_eee *e)
933{
934 struct bcmgenet_priv *priv = netdev_priv(dev);
935 struct ethtool_eee *p = &priv->eee;
936
937 if (GENET_IS_V1(priv))
938 return -EOPNOTSUPP;
939
940 e->eee_enabled = p->eee_enabled;
941 e->eee_active = p->eee_active;
942 e->tx_lpi_timer = bcmgenet_umac_readl(priv, UMAC_EEE_LPI_TIMER);
943
Florian Fainelli0299b6a2016-09-26 22:31:56 +0200944 return phy_ethtool_get_eee(priv->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -0800945}
946
947static int bcmgenet_set_eee(struct net_device *dev, struct ethtool_eee *e)
948{
949 struct bcmgenet_priv *priv = netdev_priv(dev);
950 struct ethtool_eee *p = &priv->eee;
951 int ret = 0;
952
953 if (GENET_IS_V1(priv))
954 return -EOPNOTSUPP;
955
956 p->eee_enabled = e->eee_enabled;
957
958 if (!p->eee_enabled) {
959 bcmgenet_eee_enable_set(dev, false);
960 } else {
Florian Fainelli0299b6a2016-09-26 22:31:56 +0200961 ret = phy_init_eee(priv->phydev, 0);
Florian Fainelli6ef398e2014-11-25 21:16:35 -0800962 if (ret) {
963 netif_err(priv, hw, dev, "EEE initialization failed\n");
964 return ret;
965 }
966
967 bcmgenet_umac_writel(priv, e->tx_lpi_timer, UMAC_EEE_LPI_TIMER);
968 bcmgenet_eee_enable_set(dev, true);
969 }
970
Florian Fainelli0299b6a2016-09-26 22:31:56 +0200971 return phy_ethtool_set_eee(priv->phydev, e);
Florian Fainelli6ef398e2014-11-25 21:16:35 -0800972}
973
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800974/* standard ethtool support functions. */
Julia Lawall70591ab2016-08-31 09:30:45 +0200975static const struct ethtool_ops bcmgenet_ethtool_ops = {
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800976 .get_strings = bcmgenet_get_strings,
977 .get_sset_count = bcmgenet_get_sset_count,
978 .get_ethtool_stats = bcmgenet_get_ethtool_stats,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800979 .get_drvinfo = bcmgenet_get_drvinfo,
980 .get_link = ethtool_op_get_link,
981 .get_msglevel = bcmgenet_get_msglevel,
982 .set_msglevel = bcmgenet_set_msglevel,
Florian Fainelli06ba8372014-07-21 15:29:29 -0700983 .get_wol = bcmgenet_get_wol,
984 .set_wol = bcmgenet_set_wol,
Florian Fainelli6ef398e2014-11-25 21:16:35 -0800985 .get_eee = bcmgenet_get_eee,
986 .set_eee = bcmgenet_set_eee,
Florian Fainelli016e7702016-11-15 10:06:38 -0800987 .nway_reset = phy_ethtool_nway_reset,
Florian Fainelli2f913072015-09-16 16:47:39 -0700988 .get_coalesce = bcmgenet_get_coalesce,
989 .set_coalesce = bcmgenet_set_coalesce,
Philippe Reynesfa92bf02016-09-26 22:31:57 +0200990 .get_link_ksettings = bcmgenet_get_link_ksettings,
991 .set_link_ksettings = bcmgenet_set_link_ksettings,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800992};
993
994/* Power down the unimac, based on mode. */
Florian Fainellica8cf342015-03-23 15:09:51 -0700995static int bcmgenet_power_down(struct bcmgenet_priv *priv,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800996 enum bcmgenet_power_mode mode)
997{
Florian Fainellica8cf342015-03-23 15:09:51 -0700998 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800999 u32 reg;
1000
1001 switch (mode) {
1002 case GENET_POWER_CABLE_SENSE:
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001003 phy_detach(priv->phydev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001004 break;
1005
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001006 case GENET_POWER_WOL_MAGIC:
Florian Fainellica8cf342015-03-23 15:09:51 -07001007 ret = bcmgenet_wol_power_down_cfg(priv, mode);
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001008 break;
1009
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001010 case GENET_POWER_PASSIVE:
1011 /* Power down LED */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001012 if (priv->hw_params->flags & GENET_HAS_EXT) {
1013 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
1014 reg |= (EXT_PWR_DOWN_PHY |
1015 EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
1016 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
Florian Fainellia642c4f2015-03-23 15:09:56 -07001017
1018 bcmgenet_phy_power_set(priv->dev, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001019 }
1020 break;
1021 default:
1022 break;
1023 }
Florian Fainellica8cf342015-03-23 15:09:51 -07001024
1025 return 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001026}
1027
1028static void bcmgenet_power_up(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001029 enum bcmgenet_power_mode mode)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001030{
1031 u32 reg;
1032
1033 if (!(priv->hw_params->flags & GENET_HAS_EXT))
1034 return;
1035
1036 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
1037
1038 switch (mode) {
1039 case GENET_POWER_PASSIVE:
1040 reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_PHY |
1041 EXT_PWR_DOWN_BIAS);
1042 /* fallthrough */
1043 case GENET_POWER_CABLE_SENSE:
1044 /* enable APD */
1045 reg |= EXT_PWR_DN_EN_LD;
1046 break;
Florian Fainellic3ae64a2014-07-21 15:29:25 -07001047 case GENET_POWER_WOL_MAGIC:
1048 bcmgenet_wol_power_up_cfg(priv, mode);
1049 return;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001050 default:
1051 break;
1052 }
1053
1054 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
Florian Fainelli5dbebbb2015-10-29 18:11:35 -07001055 if (mode == GENET_POWER_PASSIVE) {
Florian Fainellibd4060a2015-07-16 15:51:16 -07001056 bcmgenet_phy_power_set(priv->dev, true);
Florian Fainelli5dbebbb2015-10-29 18:11:35 -07001057 bcmgenet_mii_reset(priv->dev);
1058 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001059}
1060
1061/* ioctl handle special commands that are not present in ethtool. */
1062static int bcmgenet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1063{
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001064 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001065 int val = 0;
1066
1067 if (!netif_running(dev))
1068 return -EINVAL;
1069
1070 switch (cmd) {
1071 case SIOCGMIIPHY:
1072 case SIOCGMIIREG:
1073 case SIOCSMIIREG:
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001074 if (!priv->phydev)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001075 val = -ENODEV;
1076 else
Florian Fainelli0299b6a2016-09-26 22:31:56 +02001077 val = phy_mii_ioctl(priv->phydev, rq, cmd);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001078 break;
1079
1080 default:
1081 val = -EINVAL;
1082 break;
1083 }
1084
1085 return val;
1086}
1087
1088static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv,
1089 struct bcmgenet_tx_ring *ring)
1090{
1091 struct enet_cb *tx_cb_ptr;
1092
1093 tx_cb_ptr = ring->cbs;
1094 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
Petri Gynther014012a2015-02-23 11:00:45 -08001095
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001096 /* Advancing local write pointer */
1097 if (ring->write_ptr == ring->end_ptr)
1098 ring->write_ptr = ring->cb_ptr;
1099 else
1100 ring->write_ptr++;
1101
1102 return tx_cb_ptr;
1103}
1104
1105/* Simple helper to free a control block's resources */
1106static void bcmgenet_free_cb(struct enet_cb *cb)
1107{
1108 dev_kfree_skb_any(cb->skb);
1109 cb->skb = NULL;
1110 dma_unmap_addr_set(cb, dma_addr, 0);
1111}
1112
Petri Gynther4055eae2015-03-25 12:35:16 -07001113static inline void bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring *ring)
1114{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001115 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001116 INTRL2_CPU_MASK_SET);
1117}
1118
1119static inline void bcmgenet_rx_ring16_int_enable(struct bcmgenet_rx_ring *ring)
1120{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001121 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
Petri Gynther4055eae2015-03-25 12:35:16 -07001122 INTRL2_CPU_MASK_CLEAR);
1123}
1124
1125static inline void bcmgenet_rx_ring_int_disable(struct bcmgenet_rx_ring *ring)
1126{
1127 bcmgenet_intrl2_1_writel(ring->priv,
1128 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1129 INTRL2_CPU_MASK_SET);
1130}
1131
1132static inline void bcmgenet_rx_ring_int_enable(struct bcmgenet_rx_ring *ring)
1133{
1134 bcmgenet_intrl2_1_writel(ring->priv,
1135 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1136 INTRL2_CPU_MASK_CLEAR);
1137}
1138
Petri Gynther9dbac282015-03-25 12:35:10 -07001139static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001140{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001141 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001142 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001143}
1144
Petri Gynther9dbac282015-03-25 12:35:10 -07001145static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001146{
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001147 bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001148 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001149}
1150
Petri Gynther9dbac282015-03-25 12:35:10 -07001151static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001152{
Petri Gynther9dbac282015-03-25 12:35:10 -07001153 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001154 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001155}
1156
Petri Gynther9dbac282015-03-25 12:35:10 -07001157static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001158{
Petri Gynther9dbac282015-03-25 12:35:10 -07001159 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001160 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001161}
1162
1163/* Unlocked version of the reclaim routine */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001164static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
1165 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001166{
1167 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001168 struct device *kdev = &priv->pdev->dev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001169 struct enet_cb *tx_cb_ptr;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001170 struct netdev_queue *txq;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001171 unsigned int pkts_compl = 0;
Petri Gynther55868122016-03-24 11:27:20 -07001172 unsigned int bytes_compl = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001173 unsigned int c_index;
Petri Gynther66d06752015-03-04 14:30:01 -08001174 unsigned int txbds_ready;
1175 unsigned int txbds_processed = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001176
Brian Norris7fc527f2014-07-29 14:34:14 -07001177 /* Compute how many buffers are transmitted since last xmit call */
Doug Bergerc298ede2017-03-13 17:41:33 -07001178 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX)
1179 & DMA_C_INDEX_MASK;
1180 txbds_ready = (c_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001181
1182 netif_dbg(priv, tx_done, dev,
Petri Gynther66d06752015-03-04 14:30:01 -08001183 "%s ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
1184 __func__, ring->index, ring->c_index, c_index, txbds_ready);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001185
1186 /* Reclaim transmitted buffers */
Petri Gynther66d06752015-03-04 14:30:01 -08001187 while (txbds_processed < txbds_ready) {
1188 tx_cb_ptr = &priv->tx_cbs[ring->clean_ptr];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001189 if (tx_cb_ptr->skb) {
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001190 pkts_compl++;
Petri Gynther55868122016-03-24 11:27:20 -07001191 bytes_compl += GENET_CB(tx_cb_ptr->skb)->bytes_sent;
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001192 dma_unmap_single(kdev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001193 dma_unmap_addr(tx_cb_ptr, dma_addr),
Eric Dumazeteee57722016-03-17 11:57:06 -07001194 dma_unmap_len(tx_cb_ptr, dma_len),
Florian Fainellic91b7f62014-07-23 10:42:12 -07001195 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001196 bcmgenet_free_cb(tx_cb_ptr);
1197 } else if (dma_unmap_addr(tx_cb_ptr, dma_addr)) {
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001198 dma_unmap_page(kdev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001199 dma_unmap_addr(tx_cb_ptr, dma_addr),
1200 dma_unmap_len(tx_cb_ptr, dma_len),
1201 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001202 dma_unmap_addr_set(tx_cb_ptr, dma_addr, 0);
1203 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001204
Petri Gynther66d06752015-03-04 14:30:01 -08001205 txbds_processed++;
1206 if (likely(ring->clean_ptr < ring->end_ptr))
1207 ring->clean_ptr++;
1208 else
1209 ring->clean_ptr = ring->cb_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001210 }
1211
Petri Gynther66d06752015-03-04 14:30:01 -08001212 ring->free_bds += txbds_processed;
1213 ring->c_index = (ring->c_index + txbds_processed) & DMA_C_INDEX_MASK;
1214
Petri Gynther55868122016-03-24 11:27:20 -07001215 dev->stats.tx_packets += pkts_compl;
1216 dev->stats.tx_bytes += bytes_compl;
1217
Petri Gynthere178c8c2016-04-09 00:20:36 -07001218 txq = netdev_get_tx_queue(dev, ring->queue);
1219 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
1220
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001221 if (ring->free_bds > (MAX_SKB_FRAGS + 1)) {
1222 if (netif_tx_queue_stopped(txq))
1223 netif_tx_wake_queue(txq);
1224 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001225
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001226 return pkts_compl;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001227}
1228
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001229static unsigned int bcmgenet_tx_reclaim(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001230 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001231{
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001232 unsigned int released;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001233 unsigned long flags;
1234
1235 spin_lock_irqsave(&ring->lock, flags);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001236 released = __bcmgenet_tx_reclaim(dev, ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001237 spin_unlock_irqrestore(&ring->lock, flags);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001238
1239 return released;
1240}
1241
1242static int bcmgenet_tx_poll(struct napi_struct *napi, int budget)
1243{
1244 struct bcmgenet_tx_ring *ring =
1245 container_of(napi, struct bcmgenet_tx_ring, napi);
1246 unsigned int work_done = 0;
1247
1248 work_done = bcmgenet_tx_reclaim(ring->priv->dev, ring);
1249
1250 if (work_done == 0) {
1251 napi_complete(napi);
Petri Gynther9dbac282015-03-25 12:35:10 -07001252 ring->int_enable(ring);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001253
1254 return 0;
1255 }
1256
1257 return budget;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001258}
1259
1260static void bcmgenet_tx_reclaim_all(struct net_device *dev)
1261{
1262 struct bcmgenet_priv *priv = netdev_priv(dev);
1263 int i;
1264
1265 if (netif_is_multiqueue(dev)) {
1266 for (i = 0; i < priv->hw_params->tx_queues; i++)
1267 bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
1268 }
1269
1270 bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
1271}
1272
1273/* Transmits a single SKB (either head of a fragment or a single SKB)
1274 * caller must hold priv->lock
1275 */
1276static int bcmgenet_xmit_single(struct net_device *dev,
1277 struct sk_buff *skb,
1278 u16 dma_desc_flags,
1279 struct bcmgenet_tx_ring *ring)
1280{
1281 struct bcmgenet_priv *priv = netdev_priv(dev);
1282 struct device *kdev = &priv->pdev->dev;
1283 struct enet_cb *tx_cb_ptr;
1284 unsigned int skb_len;
1285 dma_addr_t mapping;
1286 u32 length_status;
1287 int ret;
1288
1289 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
1290
1291 if (unlikely(!tx_cb_ptr))
1292 BUG();
1293
1294 tx_cb_ptr->skb = skb;
1295
Petri Gynther7dd39912016-03-24 11:27:21 -07001296 skb_len = skb_headlen(skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001297
1298 mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
1299 ret = dma_mapping_error(kdev, mapping);
1300 if (ret) {
Florian Fainelli44c8bc32014-11-19 10:29:56 -08001301 priv->mib.tx_dma_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001302 netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
1303 dev_kfree_skb(skb);
1304 return ret;
1305 }
1306
1307 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
Eric Dumazeteee57722016-03-17 11:57:06 -07001308 dma_unmap_len_set(tx_cb_ptr, dma_len, skb_len);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001309 length_status = (skb_len << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
1310 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT) |
1311 DMA_TX_APPEND_CRC;
1312
1313 if (skb->ip_summed == CHECKSUM_PARTIAL)
1314 length_status |= DMA_TX_DO_CSUM;
1315
1316 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, length_status);
1317
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001318 return 0;
1319}
1320
Brian Norris7fc527f2014-07-29 14:34:14 -07001321/* Transmit a SKB fragment */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001322static int bcmgenet_xmit_frag(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001323 skb_frag_t *frag,
1324 u16 dma_desc_flags,
1325 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001326{
1327 struct bcmgenet_priv *priv = netdev_priv(dev);
1328 struct device *kdev = &priv->pdev->dev;
1329 struct enet_cb *tx_cb_ptr;
Petri Gynther824ba602016-04-05 14:00:00 -07001330 unsigned int frag_size;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001331 dma_addr_t mapping;
1332 int ret;
1333
1334 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
1335
1336 if (unlikely(!tx_cb_ptr))
1337 BUG();
Petri Gynther824ba602016-04-05 14:00:00 -07001338
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001339 tx_cb_ptr->skb = NULL;
1340
Petri Gynther824ba602016-04-05 14:00:00 -07001341 frag_size = skb_frag_size(frag);
1342
1343 mapping = skb_frag_dma_map(kdev, frag, 0, frag_size, DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001344 ret = dma_mapping_error(kdev, mapping);
1345 if (ret) {
Florian Fainelli44c8bc32014-11-19 10:29:56 -08001346 priv->mib.tx_dma_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001347 netif_err(priv, tx_err, dev, "%s: Tx DMA map failed\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001348 __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001349 return ret;
1350 }
1351
1352 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
Petri Gynther824ba602016-04-05 14:00:00 -07001353 dma_unmap_len_set(tx_cb_ptr, dma_len, frag_size);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001354
1355 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping,
Petri Gynther824ba602016-04-05 14:00:00 -07001356 (frag_size << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
Florian Fainellic91b7f62014-07-23 10:42:12 -07001357 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001358
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001359 return 0;
1360}
1361
1362/* Reallocate the SKB to put enough headroom in front of it and insert
1363 * the transmit checksum offsets in the descriptors
1364 */
Petri Gyntherbc233332014-10-01 11:30:01 -07001365static struct sk_buff *bcmgenet_put_tx_csum(struct net_device *dev,
1366 struct sk_buff *skb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001367{
1368 struct status_64 *status = NULL;
1369 struct sk_buff *new_skb;
1370 u16 offset;
1371 u8 ip_proto;
1372 u16 ip_ver;
1373 u32 tx_csum_info;
1374
1375 if (unlikely(skb_headroom(skb) < sizeof(*status))) {
1376 /* If 64 byte status block enabled, must make sure skb has
1377 * enough headroom for us to insert 64B status block.
1378 */
1379 new_skb = skb_realloc_headroom(skb, sizeof(*status));
1380 dev_kfree_skb(skb);
1381 if (!new_skb) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001382 dev->stats.tx_dropped++;
Petri Gyntherbc233332014-10-01 11:30:01 -07001383 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001384 }
1385 skb = new_skb;
1386 }
1387
1388 skb_push(skb, sizeof(*status));
1389 status = (struct status_64 *)skb->data;
1390
1391 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1392 ip_ver = htons(skb->protocol);
1393 switch (ip_ver) {
1394 case ETH_P_IP:
1395 ip_proto = ip_hdr(skb)->protocol;
1396 break;
1397 case ETH_P_IPV6:
1398 ip_proto = ipv6_hdr(skb)->nexthdr;
1399 break;
1400 default:
Petri Gyntherbc233332014-10-01 11:30:01 -07001401 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001402 }
1403
1404 offset = skb_checksum_start_offset(skb) - sizeof(*status);
1405 tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
1406 (offset + skb->csum_offset);
1407
1408 /* Set the length valid bit for TCP and UDP and just set
1409 * the special UDP flag for IPv4, else just set to 0.
1410 */
1411 if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
1412 tx_csum_info |= STATUS_TX_CSUM_LV;
1413 if (ip_proto == IPPROTO_UDP && ip_ver == ETH_P_IP)
1414 tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
Florian Fainelli8900ea572014-07-23 10:42:14 -07001415 } else {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001416 tx_csum_info = 0;
Florian Fainelli8900ea572014-07-23 10:42:14 -07001417 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001418
1419 status->tx_csum_info = tx_csum_info;
1420 }
1421
Petri Gyntherbc233332014-10-01 11:30:01 -07001422 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001423}
1424
1425static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
1426{
1427 struct bcmgenet_priv *priv = netdev_priv(dev);
1428 struct bcmgenet_tx_ring *ring = NULL;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001429 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001430 unsigned long flags = 0;
1431 int nr_frags, index;
1432 u16 dma_desc_flags;
1433 int ret;
1434 int i;
1435
1436 index = skb_get_queue_mapping(skb);
1437 /* Mapping strategy:
1438 * queue_mapping = 0, unclassified, packet xmited through ring16
1439 * queue_mapping = 1, goes to ring 0. (highest priority queue
1440 * queue_mapping = 2, goes to ring 1.
1441 * queue_mapping = 3, goes to ring 2.
1442 * queue_mapping = 4, goes to ring 3.
1443 */
1444 if (index == 0)
1445 index = DESC_INDEX;
1446 else
1447 index -= 1;
1448
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001449 ring = &priv->tx_rings[index];
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001450 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001451
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07001452 nr_frags = skb_shinfo(skb)->nr_frags;
1453
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001454 spin_lock_irqsave(&ring->lock, flags);
Petri Gyntherf5a9ec22016-04-05 13:59:59 -07001455 if (ring->free_bds <= (nr_frags + 1)) {
1456 if (!netif_tx_queue_stopped(txq)) {
1457 netif_tx_stop_queue(txq);
1458 netdev_err(dev,
1459 "%s: tx ring %d full when queue %d awake\n",
1460 __func__, index, ring->queue);
1461 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001462 ret = NETDEV_TX_BUSY;
1463 goto out;
1464 }
1465
Florian Fainelli474ea9c2014-07-22 11:01:52 -07001466 if (skb_padto(skb, ETH_ZLEN)) {
1467 ret = NETDEV_TX_OK;
1468 goto out;
1469 }
1470
Petri Gynther55868122016-03-24 11:27:20 -07001471 /* Retain how many bytes will be sent on the wire, without TSB inserted
1472 * by transmit checksum offload
1473 */
1474 GENET_CB(skb)->bytes_sent = skb->len;
1475
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001476 /* set the SKB transmit checksum */
1477 if (priv->desc_64b_en) {
Petri Gyntherbc233332014-10-01 11:30:01 -07001478 skb = bcmgenet_put_tx_csum(dev, skb);
1479 if (!skb) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001480 ret = NETDEV_TX_OK;
1481 goto out;
1482 }
1483 }
1484
1485 dma_desc_flags = DMA_SOP;
1486 if (nr_frags == 0)
1487 dma_desc_flags |= DMA_EOP;
1488
1489 /* Transmit single SKB or head of fragment list */
1490 ret = bcmgenet_xmit_single(dev, skb, dma_desc_flags, ring);
1491 if (ret) {
1492 ret = NETDEV_TX_OK;
1493 goto out;
1494 }
1495
1496 /* xmit fragment */
1497 for (i = 0; i < nr_frags; i++) {
1498 ret = bcmgenet_xmit_frag(dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001499 &skb_shinfo(skb)->frags[i],
1500 (i == nr_frags - 1) ? DMA_EOP : 0,
1501 ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001502 if (ret) {
1503 ret = NETDEV_TX_OK;
1504 goto out;
1505 }
1506 }
1507
Florian Fainellid03825f2014-03-20 10:53:21 -07001508 skb_tx_timestamp(skb);
1509
Florian Fainelliae67bf02015-03-13 12:11:06 -07001510 /* Decrement total BD count and advance our write pointer */
1511 ring->free_bds -= nr_frags + 1;
1512 ring->prod_index += nr_frags + 1;
1513 ring->prod_index &= DMA_P_INDEX_MASK;
1514
Petri Gynthere178c8c2016-04-09 00:20:36 -07001515 netdev_tx_sent_queue(txq, GENET_CB(skb)->bytes_sent);
1516
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001517 if (ring->free_bds <= (MAX_SKB_FRAGS + 1))
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001518 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001519
Florian Fainelliddd0ca52015-03-13 12:11:07 -07001520 if (!skb->xmit_more || netif_xmit_stopped(txq))
1521 /* Packets are ready, update producer index */
1522 bcmgenet_tdma_ring_writel(priv, ring->index,
1523 ring->prod_index, TDMA_PROD_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001524out:
1525 spin_unlock_irqrestore(&ring->lock, flags);
1526
1527 return ret;
1528}
1529
Petri Gyntherd6707be2015-03-12 15:48:00 -07001530static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
1531 struct enet_cb *cb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001532{
1533 struct device *kdev = &priv->pdev->dev;
1534 struct sk_buff *skb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001535 struct sk_buff *rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001536 dma_addr_t mapping;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001537
Petri Gyntherd6707be2015-03-12 15:48:00 -07001538 /* Allocate a new Rx skb */
Florian Fainellic91b7f62014-07-23 10:42:12 -07001539 skb = netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT);
Petri Gyntherd6707be2015-03-12 15:48:00 -07001540 if (!skb) {
1541 priv->mib.alloc_rx_buff_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001542 netif_err(priv, rx_err, priv->dev,
Petri Gyntherd6707be2015-03-12 15:48:00 -07001543 "%s: Rx skb allocation failed\n", __func__);
1544 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001545 }
1546
Petri Gyntherd6707be2015-03-12 15:48:00 -07001547 /* DMA-map the new Rx skb */
1548 mapping = dma_map_single(kdev, skb->data, priv->rx_buf_len,
1549 DMA_FROM_DEVICE);
1550 if (dma_mapping_error(kdev, mapping)) {
1551 priv->mib.rx_dma_failed++;
1552 dev_kfree_skb_any(skb);
1553 netif_err(priv, rx_err, priv->dev,
1554 "%s: Rx skb DMA mapping failed\n", __func__);
1555 return NULL;
1556 }
1557
1558 /* Grab the current Rx skb from the ring and DMA-unmap it */
1559 rx_skb = cb->skb;
1560 if (likely(rx_skb))
1561 dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
1562 priv->rx_buf_len, DMA_FROM_DEVICE);
1563
1564 /* Put the new Rx skb on the ring */
1565 cb->skb = skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001566 dma_unmap_addr_set(cb, dma_addr, mapping);
Petri Gynther8ac467e2015-03-09 13:40:00 -07001567 dmadesc_set_addr(priv, cb->bd_addr, mapping);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001568
Petri Gyntherd6707be2015-03-12 15:48:00 -07001569 /* Return the current Rx skb to caller */
1570 return rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001571}
1572
1573/* bcmgenet_desc_rx - descriptor based rx process.
1574 * this could be called from bottom half, or from NAPI polling method.
1575 */
Petri Gynther4055eae2015-03-25 12:35:16 -07001576static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001577 unsigned int budget)
1578{
Petri Gynther4055eae2015-03-25 12:35:16 -07001579 struct bcmgenet_priv *priv = ring->priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001580 struct net_device *dev = priv->dev;
1581 struct enet_cb *cb;
1582 struct sk_buff *skb;
1583 u32 dma_length_status;
1584 unsigned long dma_flag;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001585 int len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001586 unsigned int rxpktprocessed = 0, rxpkttoprocess;
1587 unsigned int p_index;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001588 unsigned int discards;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001589 unsigned int chksum_ok = 0;
1590
Petri Gynther4055eae2015-03-25 12:35:16 -07001591 p_index = bcmgenet_rdma_ring_readl(priv, ring->index, RDMA_PROD_INDEX);
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001592
1593 discards = (p_index >> DMA_P_INDEX_DISCARD_CNT_SHIFT) &
1594 DMA_P_INDEX_DISCARD_CNT_MASK;
1595 if (discards > ring->old_discards) {
1596 discards = discards - ring->old_discards;
1597 dev->stats.rx_missed_errors += discards;
1598 dev->stats.rx_errors += discards;
1599 ring->old_discards += discards;
1600
1601 /* Clear HW register when we reach 75% of maximum 0xFFFF */
1602 if (ring->old_discards >= 0xC000) {
1603 ring->old_discards = 0;
Petri Gynther4055eae2015-03-25 12:35:16 -07001604 bcmgenet_rdma_ring_writel(priv, ring->index, 0,
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001605 RDMA_PROD_INDEX);
1606 }
1607 }
1608
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001609 p_index &= DMA_P_INDEX_MASK;
Doug Bergerc298ede2017-03-13 17:41:33 -07001610 rxpkttoprocess = (p_index - ring->c_index) & DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001611
1612 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001613 "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001614
1615 while ((rxpktprocessed < rxpkttoprocess) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001616 (rxpktprocessed < budget)) {
Petri Gynther8ac467e2015-03-09 13:40:00 -07001617 cb = &priv->rx_cbs[ring->read_ptr];
Petri Gyntherd6707be2015-03-12 15:48:00 -07001618 skb = bcmgenet_rx_refill(priv, cb);
Florian Fainellib629be52014-09-08 11:37:52 -07001619
Florian Fainellib629be52014-09-08 11:37:52 -07001620 if (unlikely(!skb)) {
1621 dev->stats.rx_dropped++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001622 goto next;
Florian Fainellib629be52014-09-08 11:37:52 -07001623 }
1624
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001625 if (!priv->desc_64b_en) {
Florian Fainellic91b7f62014-07-23 10:42:12 -07001626 dma_length_status =
Petri Gynther8ac467e2015-03-09 13:40:00 -07001627 dmadesc_get_length_status(priv, cb->bd_addr);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001628 } else {
1629 struct status_64 *status;
Florian Fainelli164d4f22014-07-23 10:42:13 -07001630
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001631 status = (struct status_64 *)skb->data;
1632 dma_length_status = status->length_status;
1633 }
1634
1635 /* DMA flags and length are still valid no matter how
1636 * we got the Receive Status Vector (64B RSB or register)
1637 */
1638 dma_flag = dma_length_status & 0xffff;
1639 len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
1640
1641 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001642 "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
Petri Gynther8ac467e2015-03-09 13:40:00 -07001643 __func__, p_index, ring->c_index,
1644 ring->read_ptr, dma_length_status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001645
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001646 if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
1647 netif_err(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001648 "dropping fragmented packet!\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001649 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001650 dev_kfree_skb_any(skb);
1651 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001652 }
Petri Gyntherd6707be2015-03-12 15:48:00 -07001653
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001654 /* report errors */
1655 if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
1656 DMA_RX_OV |
1657 DMA_RX_NO |
1658 DMA_RX_LG |
1659 DMA_RX_RXER))) {
1660 netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001661 (unsigned int)dma_flag);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001662 if (dma_flag & DMA_RX_CRC_ERROR)
1663 dev->stats.rx_crc_errors++;
1664 if (dma_flag & DMA_RX_OV)
1665 dev->stats.rx_over_errors++;
1666 if (dma_flag & DMA_RX_NO)
1667 dev->stats.rx_frame_errors++;
1668 if (dma_flag & DMA_RX_LG)
1669 dev->stats.rx_length_errors++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001670 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001671 dev_kfree_skb_any(skb);
1672 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001673 } /* error packet */
1674
1675 chksum_ok = (dma_flag & priv->dma_rx_chk_bit) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001676 priv->desc_rxchk_en;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001677
1678 skb_put(skb, len);
1679 if (priv->desc_64b_en) {
1680 skb_pull(skb, 64);
1681 len -= 64;
1682 }
1683
1684 if (likely(chksum_ok))
1685 skb->ip_summed = CHECKSUM_UNNECESSARY;
1686
1687 /* remove hardware 2bytes added for IP alignment */
1688 skb_pull(skb, 2);
1689 len -= 2;
1690
1691 if (priv->crc_fwd_en) {
1692 skb_trim(skb, len - ETH_FCS_LEN);
1693 len -= ETH_FCS_LEN;
1694 }
1695
1696 /*Finish setting up the received SKB and send it to the kernel*/
1697 skb->protocol = eth_type_trans(skb, priv->dev);
1698 dev->stats.rx_packets++;
1699 dev->stats.rx_bytes += len;
1700 if (dma_flag & DMA_RX_MULT)
1701 dev->stats.multicast++;
1702
1703 /* Notify kernel */
Petri Gynther4055eae2015-03-25 12:35:16 -07001704 napi_gro_receive(&ring->napi, skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001705 netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
1706
Petri Gyntherd6707be2015-03-12 15:48:00 -07001707next:
Florian Fainellicf377d82014-10-10 10:51:52 -07001708 rxpktprocessed++;
Petri Gynther8ac467e2015-03-09 13:40:00 -07001709 if (likely(ring->read_ptr < ring->end_ptr))
1710 ring->read_ptr++;
1711 else
1712 ring->read_ptr = ring->cb_ptr;
1713
1714 ring->c_index = (ring->c_index + 1) & DMA_C_INDEX_MASK;
Petri Gynther4055eae2015-03-25 12:35:16 -07001715 bcmgenet_rdma_ring_writel(priv, ring->index, ring->c_index, RDMA_CONS_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001716 }
1717
1718 return rxpktprocessed;
1719}
1720
Petri Gynther3ab11332015-03-25 12:35:15 -07001721/* Rx NAPI polling method */
1722static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
1723{
Petri Gynther4055eae2015-03-25 12:35:16 -07001724 struct bcmgenet_rx_ring *ring = container_of(napi,
1725 struct bcmgenet_rx_ring, napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07001726 unsigned int work_done;
1727
Petri Gynther4055eae2015-03-25 12:35:16 -07001728 work_done = bcmgenet_desc_rx(ring, budget);
Petri Gynther3ab11332015-03-25 12:35:15 -07001729
1730 if (work_done < budget) {
Eric Dumazeteb96ce02016-04-08 22:06:40 -07001731 napi_complete_done(napi, work_done);
Petri Gynther4055eae2015-03-25 12:35:16 -07001732 ring->int_enable(ring);
Petri Gynther3ab11332015-03-25 12:35:15 -07001733 }
1734
1735 return work_done;
1736}
1737
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001738/* Assign skb to RX DMA descriptor. */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001739static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv,
1740 struct bcmgenet_rx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001741{
1742 struct enet_cb *cb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001743 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001744 int i;
1745
Petri Gynther8ac467e2015-03-09 13:40:00 -07001746 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001747
1748 /* loop here for each buffer needing assign */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001749 for (i = 0; i < ring->size; i++) {
1750 cb = ring->cbs + i;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001751 skb = bcmgenet_rx_refill(priv, cb);
1752 if (skb)
1753 dev_kfree_skb_any(skb);
1754 if (!cb->skb)
1755 return -ENOMEM;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001756 }
1757
Petri Gyntherd6707be2015-03-12 15:48:00 -07001758 return 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001759}
1760
1761static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
1762{
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001763 struct device *kdev = &priv->pdev->dev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001764 struct enet_cb *cb;
1765 int i;
1766
1767 for (i = 0; i < priv->num_rx_bds; i++) {
1768 cb = &priv->rx_cbs[i];
1769
1770 if (dma_unmap_addr(cb, dma_addr)) {
Florian Fainelli8c4799a2016-12-01 09:45:45 -08001771 dma_unmap_single(kdev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001772 dma_unmap_addr(cb, dma_addr),
1773 priv->rx_buf_len, DMA_FROM_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001774 dma_unmap_addr_set(cb, dma_addr, 0);
1775 }
1776
1777 if (cb->skb)
1778 bcmgenet_free_cb(cb);
1779 }
1780}
1781
Florian Fainellic91b7f62014-07-23 10:42:12 -07001782static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable)
Florian Fainellie29585b2014-07-21 15:29:20 -07001783{
1784 u32 reg;
1785
1786 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1787 if (enable)
1788 reg |= mask;
1789 else
1790 reg &= ~mask;
1791 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
1792
1793 /* UniMAC stops on a packet boundary, wait for a full-size packet
1794 * to be processed
1795 */
1796 if (enable == 0)
1797 usleep_range(1000, 2000);
1798}
1799
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001800static int reset_umac(struct bcmgenet_priv *priv)
1801{
1802 struct device *kdev = &priv->pdev->dev;
1803 unsigned int timeout = 0;
1804 u32 reg;
1805
1806 /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
1807 bcmgenet_rbuf_ctrl_set(priv, 0);
1808 udelay(10);
1809
1810 /* disable MAC while updating its registers */
1811 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1812
1813 /* issue soft reset, wait for it to complete */
1814 bcmgenet_umac_writel(priv, CMD_SW_RESET, UMAC_CMD);
1815 while (timeout++ < 1000) {
1816 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1817 if (!(reg & CMD_SW_RESET))
1818 return 0;
1819
1820 udelay(1);
1821 }
1822
1823 if (timeout == 1000) {
1824 dev_err(kdev,
Brian Norris7fc527f2014-07-29 14:34:14 -07001825 "timeout waiting for MAC to come out of reset\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001826 return -ETIMEDOUT;
1827 }
1828
1829 return 0;
1830}
1831
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001832static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
1833{
1834 /* Mask all interrupts.*/
1835 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1836 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
1837 bcmgenet_intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1838 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1839 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
1840 bcmgenet_intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1841}
1842
Florian Fainelli37850e32015-10-17 14:22:46 -07001843static void bcmgenet_link_intr_enable(struct bcmgenet_priv *priv)
1844{
1845 u32 int0_enable = 0;
1846
1847 /* Monitor cable plug/unplugged event for internal PHY, external PHY
1848 * and MoCA PHY
1849 */
1850 if (priv->internal_phy) {
1851 int0_enable |= UMAC_IRQ_LINK_EVENT;
1852 } else if (priv->ext_phy) {
1853 int0_enable |= UMAC_IRQ_LINK_EVENT;
1854 } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
1855 if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
1856 int0_enable |= UMAC_IRQ_LINK_EVENT;
1857 }
1858 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
1859}
1860
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001861static int init_umac(struct bcmgenet_priv *priv)
1862{
1863 struct device *kdev = &priv->pdev->dev;
1864 int ret;
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001865 u32 reg;
1866 u32 int0_enable = 0;
1867 u32 int1_enable = 0;
1868 int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001869
1870 dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
1871
1872 ret = reset_umac(priv);
1873 if (ret)
1874 return ret;
1875
1876 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1877 /* clear tx/rx counter */
1878 bcmgenet_umac_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001879 MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
1880 UMAC_MIB_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001881 bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
1882
1883 bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
1884
1885 /* init rx registers, enable ip header optimization */
1886 reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
1887 reg |= RBUF_ALIGN_2B;
1888 bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
1889
1890 if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
1891 bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
1892
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001893 bcmgenet_intr_disable(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001894
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001895 /* Enable Rx default queue 16 interrupts */
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001896 int0_enable |= UMAC_IRQ_RXDMA_DONE;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001897
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001898 /* Enable Tx default queue 16 interrupts */
Petri Gyntheree7d8c22015-03-30 00:28:50 -07001899 int0_enable |= UMAC_IRQ_TXDMA_DONE;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001900
Florian Fainelli37850e32015-10-17 14:22:46 -07001901 /* Configure backpressure vectors for MoCA */
1902 if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001903 reg = bcmgenet_bp_mc_get(priv);
1904 reg |= BIT(priv->hw_params->bp_in_en_shift);
1905
1906 /* bp_mask: back pressure mask */
1907 if (netif_is_multiqueue(priv->dev))
1908 reg |= priv->hw_params->bp_in_mask;
1909 else
1910 reg &= ~priv->hw_params->bp_in_mask;
1911 bcmgenet_bp_mc_set(priv, reg);
1912 }
1913
1914 /* Enable MDIO interrupts on GENET v3+ */
1915 if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001916 int0_enable |= (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001917
Petri Gynther4055eae2015-03-25 12:35:16 -07001918 /* Enable Rx priority queue interrupts */
1919 for (i = 0; i < priv->hw_params->rx_queues; ++i)
1920 int1_enable |= (1 << (UMAC_IRQ1_RX_INTR_SHIFT + i));
1921
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001922 /* Enable Tx priority queue interrupts */
1923 for (i = 0; i < priv->hw_params->tx_queues; ++i)
1924 int1_enable |= (1 << i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001925
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001926 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
1927 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001928
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001929 /* Enable rx/tx engine.*/
1930 dev_dbg(kdev, "done init umac\n");
1931
1932 return 0;
1933}
1934
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001935/* Initialize a Tx ring along with corresponding hardware registers */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001936static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
1937 unsigned int index, unsigned int size,
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001938 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001939{
1940 struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
1941 u32 words_per_bd = WORDS_PER_BD(priv);
1942 u32 flow_period_val = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001943
1944 spin_lock_init(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001945 ring->priv = priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001946 ring->index = index;
1947 if (index == DESC_INDEX) {
1948 ring->queue = 0;
1949 ring->int_enable = bcmgenet_tx_ring16_int_enable;
1950 ring->int_disable = bcmgenet_tx_ring16_int_disable;
1951 } else {
1952 ring->queue = index + 1;
1953 ring->int_enable = bcmgenet_tx_ring_int_enable;
1954 ring->int_disable = bcmgenet_tx_ring_int_disable;
1955 }
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001956 ring->cbs = priv->tx_cbs + start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001957 ring->size = size;
Petri Gynther66d06752015-03-04 14:30:01 -08001958 ring->clean_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001959 ring->c_index = 0;
1960 ring->free_bds = size;
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001961 ring->write_ptr = start_ptr;
1962 ring->cb_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001963 ring->end_ptr = end_ptr - 1;
1964 ring->prod_index = 0;
1965
1966 /* Set flow period for ring != 16 */
1967 if (index != DESC_INDEX)
1968 flow_period_val = ENET_MAX_MTU_SIZE << 16;
1969
1970 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
1971 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
1972 bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
1973 /* Disable rate control for now */
1974 bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001975 TDMA_FLOW_PERIOD);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001976 bcmgenet_tdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001977 ((size << DMA_RING_SIZE_SHIFT) |
1978 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001979
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001980 /* Set start and end address, read and write pointers */
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001981 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001982 DMA_START_ADDR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001983 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001984 TDMA_READ_PTR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001985 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001986 TDMA_WRITE_PTR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001987 bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001988 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001989}
1990
1991/* Initialize a RDMA ring */
1992static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
Petri Gynther8ac467e2015-03-09 13:40:00 -07001993 unsigned int index, unsigned int size,
1994 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001995{
Petri Gynther8ac467e2015-03-09 13:40:00 -07001996 struct bcmgenet_rx_ring *ring = &priv->rx_rings[index];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001997 u32 words_per_bd = WORDS_PER_BD(priv);
1998 int ret;
1999
Petri Gynther4055eae2015-03-25 12:35:16 -07002000 ring->priv = priv;
Petri Gynther8ac467e2015-03-09 13:40:00 -07002001 ring->index = index;
Petri Gynther4055eae2015-03-25 12:35:16 -07002002 if (index == DESC_INDEX) {
2003 ring->int_enable = bcmgenet_rx_ring16_int_enable;
2004 ring->int_disable = bcmgenet_rx_ring16_int_disable;
2005 } else {
2006 ring->int_enable = bcmgenet_rx_ring_int_enable;
2007 ring->int_disable = bcmgenet_rx_ring_int_disable;
2008 }
Petri Gynther8ac467e2015-03-09 13:40:00 -07002009 ring->cbs = priv->rx_cbs + start_ptr;
2010 ring->size = size;
2011 ring->c_index = 0;
2012 ring->read_ptr = start_ptr;
2013 ring->cb_ptr = start_ptr;
2014 ring->end_ptr = end_ptr - 1;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002015
Petri Gynther8ac467e2015-03-09 13:40:00 -07002016 ret = bcmgenet_alloc_rx_buffers(priv, ring);
2017 if (ret)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002018 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002019
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002020 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
2021 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
Petri Gynther6f5a2722015-03-06 13:45:00 -08002022 bcmgenet_rdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002023 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002024 ((size << DMA_RING_SIZE_SHIFT) |
2025 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002026 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002027 (DMA_FC_THRESH_LO <<
2028 DMA_XOFF_THRESHOLD_SHIFT) |
2029 DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
Petri Gynther6f5a2722015-03-06 13:45:00 -08002030
2031 /* Set start and end address, read and write pointers */
Petri Gynther8ac467e2015-03-09 13:40:00 -07002032 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2033 DMA_START_ADDR);
2034 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2035 RDMA_READ_PTR);
2036 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2037 RDMA_WRITE_PTR);
2038 bcmgenet_rdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Petri Gynther6f5a2722015-03-06 13:45:00 -08002039 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002040
2041 return ret;
2042}
2043
Petri Gynthere2aadb42015-03-25 12:35:14 -07002044static void bcmgenet_init_tx_napi(struct bcmgenet_priv *priv)
2045{
2046 unsigned int i;
2047 struct bcmgenet_tx_ring *ring;
2048
2049 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2050 ring = &priv->tx_rings[i];
Eric Dumazetd64b5e82015-11-18 06:31:00 -08002051 netif_tx_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll, 64);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002052 }
2053
2054 ring = &priv->tx_rings[DESC_INDEX];
Eric Dumazetd64b5e82015-11-18 06:31:00 -08002055 netif_tx_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll, 64);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002056}
2057
2058static void bcmgenet_enable_tx_napi(struct bcmgenet_priv *priv)
2059{
2060 unsigned int i;
2061 struct bcmgenet_tx_ring *ring;
2062
2063 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2064 ring = &priv->tx_rings[i];
2065 napi_enable(&ring->napi);
2066 }
2067
2068 ring = &priv->tx_rings[DESC_INDEX];
2069 napi_enable(&ring->napi);
2070}
2071
2072static void bcmgenet_disable_tx_napi(struct bcmgenet_priv *priv)
2073{
2074 unsigned int i;
2075 struct bcmgenet_tx_ring *ring;
2076
2077 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2078 ring = &priv->tx_rings[i];
2079 napi_disable(&ring->napi);
2080 }
2081
2082 ring = &priv->tx_rings[DESC_INDEX];
2083 napi_disable(&ring->napi);
2084}
2085
2086static void bcmgenet_fini_tx_napi(struct bcmgenet_priv *priv)
2087{
2088 unsigned int i;
2089 struct bcmgenet_tx_ring *ring;
2090
2091 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2092 ring = &priv->tx_rings[i];
2093 netif_napi_del(&ring->napi);
2094 }
2095
2096 ring = &priv->tx_rings[DESC_INDEX];
2097 netif_napi_del(&ring->napi);
2098}
2099
Petri Gynther16c6d662015-02-23 11:00:45 -08002100/* Initialize Tx queues
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002101 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002102 * Queues 0-3 are priority-based, each one has 32 descriptors,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002103 * with queue 0 being the highest priority queue.
2104 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002105 * Queue 16 is the default Tx queue with
Petri Gynther51a966a2015-02-23 11:00:46 -08002106 * GENET_Q16_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors.
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002107 *
Petri Gynther16c6d662015-02-23 11:00:45 -08002108 * The transmit control block pool is then partitioned as follows:
2109 * - Tx queue 0 uses tx_cbs[0..31]
2110 * - Tx queue 1 uses tx_cbs[32..63]
2111 * - Tx queue 2 uses tx_cbs[64..95]
2112 * - Tx queue 3 uses tx_cbs[96..127]
2113 * - Tx queue 16 uses tx_cbs[128..255]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002114 */
Petri Gynther16c6d662015-02-23 11:00:45 -08002115static void bcmgenet_init_tx_queues(struct net_device *dev)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002116{
2117 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther16c6d662015-02-23 11:00:45 -08002118 u32 i, dma_enable;
2119 u32 dma_ctrl, ring_cfg;
Petri Gynther37742162014-10-07 09:30:01 -07002120 u32 dma_priority[3] = {0, 0, 0};
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002121
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002122 dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
2123 dma_enable = dma_ctrl & DMA_EN;
2124 dma_ctrl &= ~DMA_EN;
2125 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
2126
Petri Gynther16c6d662015-02-23 11:00:45 -08002127 dma_ctrl = 0;
2128 ring_cfg = 0;
2129
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002130 /* Enable strict priority arbiter mode */
2131 bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
2132
Petri Gynther16c6d662015-02-23 11:00:45 -08002133 /* Initialize Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002134 for (i = 0; i < priv->hw_params->tx_queues; i++) {
Petri Gynther51a966a2015-02-23 11:00:46 -08002135 bcmgenet_init_tx_ring(priv, i, priv->hw_params->tx_bds_per_q,
2136 i * priv->hw_params->tx_bds_per_q,
2137 (i + 1) * priv->hw_params->tx_bds_per_q);
Petri Gynther16c6d662015-02-23 11:00:45 -08002138 ring_cfg |= (1 << i);
2139 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002140 dma_priority[DMA_PRIO_REG_INDEX(i)] |=
2141 ((GENET_Q0_PRIORITY + i) << DMA_PRIO_REG_SHIFT(i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002142 }
2143
Petri Gynther16c6d662015-02-23 11:00:45 -08002144 /* Initialize Tx default queue 16 */
Petri Gynther51a966a2015-02-23 11:00:46 -08002145 bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_Q16_TX_BD_CNT,
Petri Gynther16c6d662015-02-23 11:00:45 -08002146 priv->hw_params->tx_queues *
Petri Gynther51a966a2015-02-23 11:00:46 -08002147 priv->hw_params->tx_bds_per_q,
Petri Gynther16c6d662015-02-23 11:00:45 -08002148 TOTAL_DESC);
2149 ring_cfg |= (1 << DESC_INDEX);
2150 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07002151 dma_priority[DMA_PRIO_REG_INDEX(DESC_INDEX)] |=
2152 ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) <<
2153 DMA_PRIO_REG_SHIFT(DESC_INDEX));
Petri Gynther16c6d662015-02-23 11:00:45 -08002154
2155 /* Set Tx queue priorities */
Petri Gynther37742162014-10-07 09:30:01 -07002156 bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
2157 bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
2158 bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);
2159
Petri Gynthere2aadb42015-03-25 12:35:14 -07002160 /* Initialize Tx NAPI */
2161 bcmgenet_init_tx_napi(priv);
2162
Petri Gynther16c6d662015-02-23 11:00:45 -08002163 /* Enable Tx queues */
2164 bcmgenet_tdma_writel(priv, ring_cfg, DMA_RING_CFG);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002165
Petri Gynther16c6d662015-02-23 11:00:45 -08002166 /* Enable Tx DMA */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002167 if (dma_enable)
Petri Gynther16c6d662015-02-23 11:00:45 -08002168 dma_ctrl |= DMA_EN;
2169 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002170}
2171
Petri Gynther3ab11332015-03-25 12:35:15 -07002172static void bcmgenet_init_rx_napi(struct bcmgenet_priv *priv)
2173{
Petri Gynther4055eae2015-03-25 12:35:16 -07002174 unsigned int i;
2175 struct bcmgenet_rx_ring *ring;
2176
2177 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2178 ring = &priv->rx_rings[i];
2179 netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll, 64);
2180 }
2181
2182 ring = &priv->rx_rings[DESC_INDEX];
2183 netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll, 64);
Petri Gynther3ab11332015-03-25 12:35:15 -07002184}
2185
2186static void bcmgenet_enable_rx_napi(struct bcmgenet_priv *priv)
2187{
Petri Gynther4055eae2015-03-25 12:35:16 -07002188 unsigned int i;
2189 struct bcmgenet_rx_ring *ring;
2190
2191 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2192 ring = &priv->rx_rings[i];
2193 napi_enable(&ring->napi);
2194 }
2195
2196 ring = &priv->rx_rings[DESC_INDEX];
2197 napi_enable(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002198}
2199
2200static void bcmgenet_disable_rx_napi(struct bcmgenet_priv *priv)
2201{
Petri Gynther4055eae2015-03-25 12:35:16 -07002202 unsigned int i;
2203 struct bcmgenet_rx_ring *ring;
2204
2205 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2206 ring = &priv->rx_rings[i];
2207 napi_disable(&ring->napi);
2208 }
2209
2210 ring = &priv->rx_rings[DESC_INDEX];
2211 napi_disable(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002212}
2213
2214static void bcmgenet_fini_rx_napi(struct bcmgenet_priv *priv)
2215{
Petri Gynther4055eae2015-03-25 12:35:16 -07002216 unsigned int i;
2217 struct bcmgenet_rx_ring *ring;
2218
2219 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2220 ring = &priv->rx_rings[i];
2221 netif_napi_del(&ring->napi);
2222 }
2223
2224 ring = &priv->rx_rings[DESC_INDEX];
2225 netif_napi_del(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002226}
2227
Petri Gynther8ac467e2015-03-09 13:40:00 -07002228/* Initialize Rx queues
2229 *
2230 * Queues 0-15 are priority queues. Hardware Filtering Block (HFB) can be
2231 * used to direct traffic to these queues.
2232 *
2233 * Queue 16 is the default Rx queue with GENET_Q16_RX_BD_CNT descriptors.
2234 */
2235static int bcmgenet_init_rx_queues(struct net_device *dev)
2236{
2237 struct bcmgenet_priv *priv = netdev_priv(dev);
2238 u32 i;
2239 u32 dma_enable;
2240 u32 dma_ctrl;
2241 u32 ring_cfg;
2242 int ret;
2243
2244 dma_ctrl = bcmgenet_rdma_readl(priv, DMA_CTRL);
2245 dma_enable = dma_ctrl & DMA_EN;
2246 dma_ctrl &= ~DMA_EN;
2247 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2248
2249 dma_ctrl = 0;
2250 ring_cfg = 0;
2251
2252 /* Initialize Rx priority queues */
2253 for (i = 0; i < priv->hw_params->rx_queues; i++) {
2254 ret = bcmgenet_init_rx_ring(priv, i,
2255 priv->hw_params->rx_bds_per_q,
2256 i * priv->hw_params->rx_bds_per_q,
2257 (i + 1) *
2258 priv->hw_params->rx_bds_per_q);
2259 if (ret)
2260 return ret;
2261
2262 ring_cfg |= (1 << i);
2263 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2264 }
2265
2266 /* Initialize Rx default queue 16 */
2267 ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, GENET_Q16_RX_BD_CNT,
2268 priv->hw_params->rx_queues *
2269 priv->hw_params->rx_bds_per_q,
2270 TOTAL_DESC);
2271 if (ret)
2272 return ret;
2273
2274 ring_cfg |= (1 << DESC_INDEX);
2275 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
2276
Petri Gynther3ab11332015-03-25 12:35:15 -07002277 /* Initialize Rx NAPI */
2278 bcmgenet_init_rx_napi(priv);
2279
Petri Gynther8ac467e2015-03-09 13:40:00 -07002280 /* Enable rings */
2281 bcmgenet_rdma_writel(priv, ring_cfg, DMA_RING_CFG);
2282
2283 /* Configure ring as descriptor ring and re-enable DMA if enabled */
2284 if (dma_enable)
2285 dma_ctrl |= DMA_EN;
2286 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2287
2288 return 0;
2289}
2290
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002291static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
2292{
2293 int ret = 0;
2294 int timeout = 0;
2295 u32 reg;
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002296 u32 dma_ctrl;
2297 int i;
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002298
2299 /* Disable TDMA to stop add more frames in TX DMA */
2300 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2301 reg &= ~DMA_EN;
2302 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2303
2304 /* Check TDMA status register to confirm TDMA is disabled */
2305 while (timeout++ < DMA_TIMEOUT_VAL) {
2306 reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
2307 if (reg & DMA_DISABLED)
2308 break;
2309
2310 udelay(1);
2311 }
2312
2313 if (timeout == DMA_TIMEOUT_VAL) {
2314 netdev_warn(priv->dev, "Timed out while disabling TX DMA\n");
2315 ret = -ETIMEDOUT;
2316 }
2317
2318 /* Wait 10ms for packet drain in both tx and rx dma */
2319 usleep_range(10000, 20000);
2320
2321 /* Disable RDMA */
2322 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2323 reg &= ~DMA_EN;
2324 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2325
2326 timeout = 0;
2327 /* Check RDMA status register to confirm RDMA is disabled */
2328 while (timeout++ < DMA_TIMEOUT_VAL) {
2329 reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
2330 if (reg & DMA_DISABLED)
2331 break;
2332
2333 udelay(1);
2334 }
2335
2336 if (timeout == DMA_TIMEOUT_VAL) {
2337 netdev_warn(priv->dev, "Timed out while disabling RX DMA\n");
2338 ret = -ETIMEDOUT;
2339 }
2340
Jaedon Shinb6df7d62015-08-21 10:08:26 +09002341 dma_ctrl = 0;
2342 for (i = 0; i < priv->hw_params->rx_queues; i++)
2343 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2344 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2345 reg &= ~dma_ctrl;
2346 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2347
2348 dma_ctrl = 0;
2349 for (i = 0; i < priv->hw_params->tx_queues; i++)
2350 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2351 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2352 reg &= ~dma_ctrl;
2353 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2354
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002355 return ret;
2356}
2357
Petri Gynther9abab962015-03-30 00:29:01 -07002358static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002359{
2360 int i;
Petri Gynthere178c8c2016-04-09 00:20:36 -07002361 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002362
Petri Gynther9abab962015-03-30 00:29:01 -07002363 bcmgenet_fini_rx_napi(priv);
2364 bcmgenet_fini_tx_napi(priv);
2365
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002366 /* disable DMA */
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002367 bcmgenet_dma_teardown(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002368
2369 for (i = 0; i < priv->num_tx_bds; i++) {
2370 if (priv->tx_cbs[i].skb != NULL) {
2371 dev_kfree_skb(priv->tx_cbs[i].skb);
2372 priv->tx_cbs[i].skb = NULL;
2373 }
2374 }
2375
Petri Gynthere178c8c2016-04-09 00:20:36 -07002376 for (i = 0; i < priv->hw_params->tx_queues; i++) {
2377 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[i].queue);
2378 netdev_tx_reset_queue(txq);
2379 }
2380
2381 txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[DESC_INDEX].queue);
2382 netdev_tx_reset_queue(txq);
2383
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002384 bcmgenet_free_rx_buffers(priv);
2385 kfree(priv->rx_cbs);
2386 kfree(priv->tx_cbs);
2387}
2388
2389/* init_edma: Initialize DMA control register */
2390static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
2391{
2392 int ret;
Petri Gynther014012a2015-02-23 11:00:45 -08002393 unsigned int i;
2394 struct enet_cb *cb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002395
Petri Gynther6f5a2722015-03-06 13:45:00 -08002396 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002397
Petri Gynther6f5a2722015-03-06 13:45:00 -08002398 /* Initialize common Rx ring structures */
2399 priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
2400 priv->num_rx_bds = TOTAL_DESC;
2401 priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct enet_cb),
2402 GFP_KERNEL);
2403 if (!priv->rx_cbs)
2404 return -ENOMEM;
2405
2406 for (i = 0; i < priv->num_rx_bds; i++) {
2407 cb = priv->rx_cbs + i;
2408 cb->bd_addr = priv->rx_bds + i * DMA_DESC_SIZE;
2409 }
2410
Brian Norris7fc527f2014-07-29 14:34:14 -07002411 /* Initialize common TX ring structures */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002412 priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
2413 priv->num_tx_bds = TOTAL_DESC;
Florian Fainellic489be02014-07-23 10:42:15 -07002414 priv->tx_cbs = kcalloc(priv->num_tx_bds, sizeof(struct enet_cb),
Florian Fainellic91b7f62014-07-23 10:42:12 -07002415 GFP_KERNEL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002416 if (!priv->tx_cbs) {
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002417 kfree(priv->rx_cbs);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002418 return -ENOMEM;
2419 }
2420
Petri Gynther014012a2015-02-23 11:00:45 -08002421 for (i = 0; i < priv->num_tx_bds; i++) {
2422 cb = priv->tx_cbs + i;
2423 cb->bd_addr = priv->tx_bds + i * DMA_DESC_SIZE;
2424 }
2425
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002426 /* Init rDma */
2427 bcmgenet_rdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
2428
2429 /* Initialize Rx queues */
2430 ret = bcmgenet_init_rx_queues(priv->dev);
2431 if (ret) {
2432 netdev_err(priv->dev, "failed to initialize Rx queues\n");
2433 bcmgenet_free_rx_buffers(priv);
2434 kfree(priv->rx_cbs);
2435 kfree(priv->tx_cbs);
2436 return ret;
2437 }
2438
2439 /* Init tDma */
2440 bcmgenet_tdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
2441
Petri Gynther16c6d662015-02-23 11:00:45 -08002442 /* Initialize Tx queues */
2443 bcmgenet_init_tx_queues(priv->dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002444
2445 return 0;
2446}
2447
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002448/* Interrupt bottom half */
2449static void bcmgenet_irq_task(struct work_struct *work)
2450{
2451 struct bcmgenet_priv *priv = container_of(
2452 work, struct bcmgenet_priv, bcmgenet_irq_work);
2453
2454 netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
2455
Florian Fainelli8fdb0e02014-07-21 15:29:26 -07002456 if (priv->irq0_stat & UMAC_IRQ_MPD_R) {
2457 priv->irq0_stat &= ~UMAC_IRQ_MPD_R;
2458 netif_dbg(priv, wol, priv->dev,
2459 "magic packet detected, waking up\n");
2460 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
2461 }
2462
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002463 /* Link UP/DOWN event */
Jaedon Shind07c0272016-02-19 13:48:50 +09002464 if (priv->irq0_stat & UMAC_IRQ_LINK_EVENT) {
Florian Fainelli0299b6a2016-09-26 22:31:56 +02002465 phy_mac_interrupt(priv->phydev,
Petri Gynther451e1ca2015-03-30 00:29:35 -07002466 !!(priv->irq0_stat & UMAC_IRQ_LINK_UP));
Petri Gynthere122966d2015-03-30 00:29:24 -07002467 priv->irq0_stat &= ~UMAC_IRQ_LINK_EVENT;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002468 }
2469}
2470
Petri Gynther4055eae2015-03-25 12:35:16 -07002471/* bcmgenet_isr1: handle Rx and Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002472static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
2473{
2474 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002475 struct bcmgenet_rx_ring *rx_ring;
2476 struct bcmgenet_tx_ring *tx_ring;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002477 unsigned int index;
2478
2479 /* Save irq status for bottom-half processing. */
2480 priv->irq1_stat =
2481 bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002482 ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002483
Brian Norris7fc527f2014-07-29 14:34:14 -07002484 /* clear interrupts */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002485 bcmgenet_intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
2486
2487 netif_dbg(priv, intr, priv->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002488 "%s: IRQ=0x%x\n", __func__, priv->irq1_stat);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002489
Petri Gynther4055eae2015-03-25 12:35:16 -07002490 /* Check Rx priority queue interrupts */
2491 for (index = 0; index < priv->hw_params->rx_queues; index++) {
2492 if (!(priv->irq1_stat & BIT(UMAC_IRQ1_RX_INTR_SHIFT + index)))
2493 continue;
2494
2495 rx_ring = &priv->rx_rings[index];
2496
2497 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2498 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002499 __napi_schedule_irqoff(&rx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07002500 }
2501 }
2502
2503 /* Check Tx priority queue interrupts */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002504 for (index = 0; index < priv->hw_params->tx_queues; index++) {
2505 if (!(priv->irq1_stat & BIT(index)))
2506 continue;
2507
Petri Gynther4055eae2015-03-25 12:35:16 -07002508 tx_ring = &priv->tx_rings[index];
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002509
Petri Gynther4055eae2015-03-25 12:35:16 -07002510 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2511 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002512 __napi_schedule_irqoff(&tx_ring->napi);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002513 }
2514 }
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002515
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002516 return IRQ_HANDLED;
2517}
2518
Petri Gynther4055eae2015-03-25 12:35:16 -07002519/* bcmgenet_isr0: handle Rx and Tx default queues + other stuff */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002520static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
2521{
2522 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002523 struct bcmgenet_rx_ring *rx_ring;
2524 struct bcmgenet_tx_ring *tx_ring;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002525
2526 /* Save irq status for bottom-half processing. */
2527 priv->irq0_stat =
2528 bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
2529 ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002530
Brian Norris7fc527f2014-07-29 14:34:14 -07002531 /* clear interrupts */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002532 bcmgenet_intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
2533
2534 netif_dbg(priv, intr, priv->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002535 "IRQ=0x%x\n", priv->irq0_stat);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002536
Petri Gyntheree7d8c22015-03-30 00:28:50 -07002537 if (priv->irq0_stat & UMAC_IRQ_RXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07002538 rx_ring = &priv->rx_rings[DESC_INDEX];
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002539
Petri Gynther4055eae2015-03-25 12:35:16 -07002540 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2541 rx_ring->int_disable(rx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002542 __napi_schedule_irqoff(&rx_ring->napi);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002543 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002544 }
Petri Gynther4055eae2015-03-25 12:35:16 -07002545
Petri Gyntheree7d8c22015-03-30 00:28:50 -07002546 if (priv->irq0_stat & UMAC_IRQ_TXDMA_DONE) {
Petri Gynther4055eae2015-03-25 12:35:16 -07002547 tx_ring = &priv->tx_rings[DESC_INDEX];
2548
2549 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2550 tx_ring->int_disable(tx_ring);
Florian Fainellidac916f2016-04-08 22:30:56 -07002551 __napi_schedule_irqoff(&tx_ring->napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07002552 }
2553 }
2554
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002555 if (priv->irq0_stat & (UMAC_IRQ_PHY_DET_R |
2556 UMAC_IRQ_PHY_DET_F |
Petri Gynthere122966d2015-03-30 00:29:24 -07002557 UMAC_IRQ_LINK_EVENT |
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002558 UMAC_IRQ_HFB_SM |
2559 UMAC_IRQ_HFB_MM |
2560 UMAC_IRQ_MPD_R)) {
2561 /* all other interested interrupts handled in bottom half */
2562 schedule_work(&priv->bcmgenet_irq_work);
2563 }
2564
2565 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07002566 priv->irq0_stat & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002567 priv->irq0_stat &= ~(UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
2568 wake_up(&priv->wq);
2569 }
2570
2571 return IRQ_HANDLED;
2572}
2573
Florian Fainelli85620562014-07-21 15:29:23 -07002574static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
2575{
2576 struct bcmgenet_priv *priv = dev_id;
2577
2578 pm_wakeup_event(&priv->pdev->dev, 0);
2579
2580 return IRQ_HANDLED;
2581}
2582
Florian Fainelli4d2e8882015-07-31 11:42:54 -07002583#ifdef CONFIG_NET_POLL_CONTROLLER
2584static void bcmgenet_poll_controller(struct net_device *dev)
2585{
2586 struct bcmgenet_priv *priv = netdev_priv(dev);
2587
2588 /* Invoke the main RX/TX interrupt handler */
2589 disable_irq(priv->irq0);
2590 bcmgenet_isr0(priv->irq0, priv);
2591 enable_irq(priv->irq0);
2592
2593 /* And the interrupt handler for RX/TX priority queues */
2594 disable_irq(priv->irq1);
2595 bcmgenet_isr1(priv->irq1, priv);
2596 enable_irq(priv->irq1);
2597}
2598#endif
2599
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002600static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
2601{
2602 u32 reg;
2603
2604 reg = bcmgenet_rbuf_ctrl_get(priv);
2605 reg |= BIT(1);
2606 bcmgenet_rbuf_ctrl_set(priv, reg);
2607 udelay(10);
2608
2609 reg &= ~BIT(1);
2610 bcmgenet_rbuf_ctrl_set(priv, reg);
2611 udelay(10);
2612}
2613
2614static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002615 unsigned char *addr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002616{
2617 bcmgenet_umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
2618 (addr[2] << 8) | addr[3], UMAC_MAC0);
2619 bcmgenet_umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
2620}
2621
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002622/* Returns a reusable dma control register value */
2623static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
2624{
2625 u32 reg;
2626 u32 dma_ctrl;
2627
2628 /* disable DMA */
2629 dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
2630 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2631 reg &= ~dma_ctrl;
2632 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2633
2634 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2635 reg &= ~dma_ctrl;
2636 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2637
2638 bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
2639 udelay(10);
2640 bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
2641
2642 return dma_ctrl;
2643}
2644
2645static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
2646{
2647 u32 reg;
2648
2649 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2650 reg |= dma_ctrl;
2651 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2652
2653 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2654 reg |= dma_ctrl;
2655 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2656}
2657
Petri Gynther0034de42015-03-13 14:45:00 -07002658/* bcmgenet_hfb_clear
2659 *
2660 * Clear Hardware Filter Block and disable all filtering.
2661 */
2662static void bcmgenet_hfb_clear(struct bcmgenet_priv *priv)
2663{
2664 u32 i;
2665
2666 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_CTRL);
2667 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS);
2668 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS + 4);
2669
2670 for (i = DMA_INDEX2RING_0; i <= DMA_INDEX2RING_7; i++)
2671 bcmgenet_rdma_writel(priv, 0x0, i);
2672
2673 for (i = 0; i < (priv->hw_params->hfb_filter_cnt / 4); i++)
2674 bcmgenet_hfb_reg_writel(priv, 0x0,
2675 HFB_FLT_LEN_V3PLUS + i * sizeof(u32));
2676
2677 for (i = 0; i < priv->hw_params->hfb_filter_cnt *
2678 priv->hw_params->hfb_filter_size; i++)
2679 bcmgenet_hfb_writel(priv, 0x0, i * sizeof(u32));
2680}
2681
2682static void bcmgenet_hfb_init(struct bcmgenet_priv *priv)
2683{
2684 if (GENET_IS_V1(priv) || GENET_IS_V2(priv))
2685 return;
2686
2687 bcmgenet_hfb_clear(priv);
2688}
2689
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002690static void bcmgenet_netif_start(struct net_device *dev)
2691{
2692 struct bcmgenet_priv *priv = netdev_priv(dev);
2693
2694 /* Start the network engine */
Petri Gynther3ab11332015-03-25 12:35:15 -07002695 bcmgenet_enable_rx_napi(priv);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002696 bcmgenet_enable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002697
2698 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
2699
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002700 netif_tx_start_all_queues(dev);
2701
Florian Fainelli37850e32015-10-17 14:22:46 -07002702 /* Monitor link interrupts now */
2703 bcmgenet_link_intr_enable(priv);
2704
Florian Fainelli0299b6a2016-09-26 22:31:56 +02002705 phy_start(priv->phydev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002706}
2707
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002708static int bcmgenet_open(struct net_device *dev)
2709{
2710 struct bcmgenet_priv *priv = netdev_priv(dev);
2711 unsigned long dma_ctrl;
2712 u32 reg;
2713 int ret;
2714
2715 netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
2716
2717 /* Turn on the clock */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002718 clk_prepare_enable(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002719
Florian Fainellia642c4f2015-03-23 15:09:56 -07002720 /* If this is an internal GPHY, power it back on now, before UniMAC is
2721 * brought out of reset as absolutely no UniMAC activity is allowed
2722 */
Florian Fainellic624f892015-07-16 15:51:17 -07002723 if (priv->internal_phy)
Florian Fainellia642c4f2015-03-23 15:09:56 -07002724 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
2725
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002726 /* take MAC out of reset */
2727 bcmgenet_umac_reset(priv);
2728
2729 ret = init_umac(priv);
2730 if (ret)
2731 goto err_clk_disable;
2732
2733 /* disable ethernet MAC while updating its registers */
Florian Fainellie29585b2014-07-21 15:29:20 -07002734 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002735
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002736 /* Make sure we reflect the value of CRC_CMD_FWD */
2737 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
2738 priv->crc_fwd_en = !!(reg & CMD_CRC_FWD);
2739
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002740 bcmgenet_set_hw_addr(priv, dev->dev_addr);
2741
Florian Fainellic624f892015-07-16 15:51:17 -07002742 if (priv->internal_phy) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002743 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
2744 reg |= EXT_ENERGY_DET_MASK;
2745 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
2746 }
2747
2748 /* Disable RX/TX DMA and flush TX queues */
2749 dma_ctrl = bcmgenet_dma_disable(priv);
2750
2751 /* Reinitialize TDMA and RDMA and SW housekeeping */
2752 ret = bcmgenet_init_dma(priv);
2753 if (ret) {
2754 netdev_err(dev, "failed to initialize DMA\n");
Petri Gyntherfac25942015-03-30 00:29:13 -07002755 goto err_clk_disable;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002756 }
2757
2758 /* Always enable ring 16 - descriptor ring */
2759 bcmgenet_enable_dma(priv, dma_ctrl);
2760
Petri Gynther0034de42015-03-13 14:45:00 -07002761 /* HFB init */
2762 bcmgenet_hfb_init(priv);
2763
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002764 ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002765 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002766 if (ret < 0) {
2767 netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
2768 goto err_fini_dma;
2769 }
2770
2771 ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002772 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002773 if (ret < 0) {
2774 netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
2775 goto err_irq0;
2776 }
2777
Florian Fainelli6cc8e6d2015-07-16 15:51:18 -07002778 ret = bcmgenet_mii_probe(dev);
2779 if (ret) {
2780 netdev_err(dev, "failed to connect to PHY\n");
2781 goto err_irq1;
2782 }
Florian Fainellic96e7312014-11-10 18:06:20 -08002783
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002784 bcmgenet_netif_start(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002785
2786 return 0;
2787
Florian Fainelli6cc8e6d2015-07-16 15:51:18 -07002788err_irq1:
2789 free_irq(priv->irq1, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002790err_irq0:
Florian Fainelli978ffac42015-07-16 15:51:15 -07002791 free_irq(priv->irq0, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002792err_fini_dma:
2793 bcmgenet_fini_dma(priv);
2794err_clk_disable:
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002795 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002796 return ret;
2797}
2798
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002799static void bcmgenet_netif_stop(struct net_device *dev)
2800{
2801 struct bcmgenet_priv *priv = netdev_priv(dev);
2802
2803 netif_tx_stop_all_queues(dev);
Florian Fainelli0299b6a2016-09-26 22:31:56 +02002804 phy_stop(priv->phydev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002805 bcmgenet_intr_disable(priv);
Petri Gynther3ab11332015-03-25 12:35:15 -07002806 bcmgenet_disable_rx_napi(priv);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002807 bcmgenet_disable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002808
2809 /* Wait for pending work items to complete. Since interrupts are
2810 * disabled no new work will be scheduled.
2811 */
2812 cancel_work_sync(&priv->bcmgenet_irq_work);
Florian Fainellicc013fb2014-08-11 14:50:43 -07002813
Florian Fainellicc013fb2014-08-11 14:50:43 -07002814 priv->old_link = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002815 priv->old_speed = -1;
Florian Fainellicc013fb2014-08-11 14:50:43 -07002816 priv->old_duplex = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002817 priv->old_pause = -1;
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002818}
2819
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002820static int bcmgenet_close(struct net_device *dev)
2821{
2822 struct bcmgenet_priv *priv = netdev_priv(dev);
2823 int ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002824
2825 netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
2826
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002827 bcmgenet_netif_stop(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002828
Florian Fainellic96e7312014-11-10 18:06:20 -08002829 /* Really kill the PHY state machine and disconnect from it */
Florian Fainelli0299b6a2016-09-26 22:31:56 +02002830 phy_disconnect(priv->phydev);
Florian Fainellic96e7312014-11-10 18:06:20 -08002831
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002832 /* Disable MAC receive */
Florian Fainellie29585b2014-07-21 15:29:20 -07002833 umac_enable_set(priv, CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002834
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002835 ret = bcmgenet_dma_teardown(priv);
2836 if (ret)
2837 return ret;
2838
2839 /* Disable MAC transmit. TX DMA disabled have to done before this */
Florian Fainellie29585b2014-07-21 15:29:20 -07002840 umac_enable_set(priv, CMD_TX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002841
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002842 /* tx reclaim */
2843 bcmgenet_tx_reclaim_all(dev);
2844 bcmgenet_fini_dma(priv);
2845
2846 free_irq(priv->irq0, priv);
2847 free_irq(priv->irq1, priv);
2848
Florian Fainellic624f892015-07-16 15:51:17 -07002849 if (priv->internal_phy)
Florian Fainellica8cf342015-03-23 15:09:51 -07002850 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002851
Florian Fainelli7d5d3072015-07-22 17:28:23 -07002852 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002853
Florian Fainellica8cf342015-03-23 15:09:51 -07002854 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002855}
2856
Florian Fainelli13ea6572015-06-04 16:15:50 -07002857static void bcmgenet_dump_tx_queue(struct bcmgenet_tx_ring *ring)
2858{
2859 struct bcmgenet_priv *priv = ring->priv;
2860 u32 p_index, c_index, intsts, intmsk;
2861 struct netdev_queue *txq;
2862 unsigned int free_bds;
2863 unsigned long flags;
2864 bool txq_stopped;
2865
2866 if (!netif_msg_tx_err(priv))
2867 return;
2868
2869 txq = netdev_get_tx_queue(priv->dev, ring->queue);
2870
2871 spin_lock_irqsave(&ring->lock, flags);
2872 if (ring->index == DESC_INDEX) {
2873 intsts = ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
2874 intmsk = UMAC_IRQ_TXDMA_DONE | UMAC_IRQ_TXDMA_MBDONE;
2875 } else {
2876 intsts = ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
2877 intmsk = 1 << ring->index;
2878 }
2879 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
2880 p_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_PROD_INDEX);
2881 txq_stopped = netif_tx_queue_stopped(txq);
2882 free_bds = ring->free_bds;
2883 spin_unlock_irqrestore(&ring->lock, flags);
2884
2885 netif_err(priv, tx_err, priv->dev, "Ring %d queue %d status summary\n"
2886 "TX queue status: %s, interrupts: %s\n"
2887 "(sw)free_bds: %d (sw)size: %d\n"
2888 "(sw)p_index: %d (hw)p_index: %d\n"
2889 "(sw)c_index: %d (hw)c_index: %d\n"
2890 "(sw)clean_p: %d (sw)write_p: %d\n"
2891 "(sw)cb_ptr: %d (sw)end_ptr: %d\n",
2892 ring->index, ring->queue,
2893 txq_stopped ? "stopped" : "active",
2894 intsts & intmsk ? "enabled" : "disabled",
2895 free_bds, ring->size,
2896 ring->prod_index, p_index & DMA_P_INDEX_MASK,
2897 ring->c_index, c_index & DMA_C_INDEX_MASK,
2898 ring->clean_ptr, ring->write_ptr,
2899 ring->cb_ptr, ring->end_ptr);
2900}
2901
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002902static void bcmgenet_timeout(struct net_device *dev)
2903{
2904 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli13ea6572015-06-04 16:15:50 -07002905 u32 int0_enable = 0;
2906 u32 int1_enable = 0;
2907 unsigned int q;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002908
2909 netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
2910
Florian Fainelli13ea6572015-06-04 16:15:50 -07002911 for (q = 0; q < priv->hw_params->tx_queues; q++)
2912 bcmgenet_dump_tx_queue(&priv->tx_rings[q]);
2913 bcmgenet_dump_tx_queue(&priv->tx_rings[DESC_INDEX]);
2914
2915 bcmgenet_tx_reclaim_all(dev);
2916
2917 for (q = 0; q < priv->hw_params->tx_queues; q++)
2918 int1_enable |= (1 << q);
2919
2920 int0_enable = UMAC_IRQ_TXDMA_DONE;
2921
2922 /* Re-enable TX interrupts if disabled */
2923 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
2924 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
2925
Florian Westphal860e9532016-05-03 16:33:13 +02002926 netif_trans_update(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002927
2928 dev->stats.tx_errors++;
2929
2930 netif_tx_wake_all_queues(dev);
2931}
2932
2933#define MAX_MC_COUNT 16
2934
2935static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
2936 unsigned char *addr,
2937 int *i,
2938 int *mc)
2939{
2940 u32 reg;
2941
Florian Fainellic91b7f62014-07-23 10:42:12 -07002942 bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
2943 UMAC_MDF_ADDR + (*i * 4));
2944 bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
2945 addr[4] << 8 | addr[5],
2946 UMAC_MDF_ADDR + ((*i + 1) * 4));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002947 reg = bcmgenet_umac_readl(priv, UMAC_MDF_CTRL);
2948 reg |= (1 << (MAX_MC_COUNT - *mc));
2949 bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
2950 *i += 2;
2951 (*mc)++;
2952}
2953
2954static void bcmgenet_set_rx_mode(struct net_device *dev)
2955{
2956 struct bcmgenet_priv *priv = netdev_priv(dev);
2957 struct netdev_hw_addr *ha;
2958 int i, mc;
2959 u32 reg;
2960
2961 netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
2962
Brian Norris7fc527f2014-07-29 14:34:14 -07002963 /* Promiscuous mode */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002964 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
2965 if (dev->flags & IFF_PROMISC) {
2966 reg |= CMD_PROMISC;
2967 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
2968 bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
2969 return;
2970 } else {
2971 reg &= ~CMD_PROMISC;
2972 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
2973 }
2974
2975 /* UniMac doesn't support ALLMULTI */
2976 if (dev->flags & IFF_ALLMULTI) {
2977 netdev_warn(dev, "ALLMULTI is not supported\n");
2978 return;
2979 }
2980
2981 /* update MDF filter */
2982 i = 0;
2983 mc = 0;
2984 /* Broadcast */
2985 bcmgenet_set_mdf_addr(priv, dev->broadcast, &i, &mc);
2986 /* my own address.*/
2987 bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i, &mc);
2988 /* Unicast list*/
2989 if (netdev_uc_count(dev) > (MAX_MC_COUNT - mc))
2990 return;
2991
2992 if (!netdev_uc_empty(dev))
2993 netdev_for_each_uc_addr(ha, dev)
2994 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
2995 /* Multicast */
2996 if (netdev_mc_empty(dev) || netdev_mc_count(dev) >= (MAX_MC_COUNT - mc))
2997 return;
2998
2999 netdev_for_each_mc_addr(ha, dev)
3000 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
3001}
3002
3003/* Set the hardware MAC address. */
3004static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
3005{
3006 struct sockaddr *addr = p;
3007
3008 /* Setting the MAC address at the hardware level is not possible
3009 * without disabling the UniMAC RX/TX enable bits.
3010 */
3011 if (netif_running(dev))
3012 return -EBUSY;
3013
3014 ether_addr_copy(dev->dev_addr, addr->sa_data);
3015
3016 return 0;
3017}
3018
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003019static const struct net_device_ops bcmgenet_netdev_ops = {
3020 .ndo_open = bcmgenet_open,
3021 .ndo_stop = bcmgenet_close,
3022 .ndo_start_xmit = bcmgenet_xmit,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003023 .ndo_tx_timeout = bcmgenet_timeout,
3024 .ndo_set_rx_mode = bcmgenet_set_rx_mode,
3025 .ndo_set_mac_address = bcmgenet_set_mac_addr,
3026 .ndo_do_ioctl = bcmgenet_ioctl,
3027 .ndo_set_features = bcmgenet_set_features,
Florian Fainelli4d2e8882015-07-31 11:42:54 -07003028#ifdef CONFIG_NET_POLL_CONTROLLER
3029 .ndo_poll_controller = bcmgenet_poll_controller,
3030#endif
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003031};
3032
3033/* Array of GENET hardware parameters/characteristics */
3034static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
3035 [GENET_V1] = {
3036 .tx_queues = 0,
Petri Gynther51a966a2015-02-23 11:00:46 -08003037 .tx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003038 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003039 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003040 .bp_in_en_shift = 16,
3041 .bp_in_mask = 0xffff,
3042 .hfb_filter_cnt = 16,
3043 .qtag_mask = 0x1F,
3044 .hfb_offset = 0x1000,
3045 .rdma_offset = 0x2000,
3046 .tdma_offset = 0x3000,
3047 .words_per_bd = 2,
3048 },
3049 [GENET_V2] = {
3050 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003051 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003052 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003053 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003054 .bp_in_en_shift = 16,
3055 .bp_in_mask = 0xffff,
3056 .hfb_filter_cnt = 16,
3057 .qtag_mask = 0x1F,
3058 .tbuf_offset = 0x0600,
3059 .hfb_offset = 0x1000,
3060 .hfb_reg_offset = 0x2000,
3061 .rdma_offset = 0x3000,
3062 .tdma_offset = 0x4000,
3063 .words_per_bd = 2,
3064 .flags = GENET_HAS_EXT,
3065 },
3066 [GENET_V3] = {
3067 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003068 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003069 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003070 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003071 .bp_in_en_shift = 17,
3072 .bp_in_mask = 0x1ffff,
3073 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003074 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003075 .qtag_mask = 0x3F,
3076 .tbuf_offset = 0x0600,
3077 .hfb_offset = 0x8000,
3078 .hfb_reg_offset = 0xfc00,
3079 .rdma_offset = 0x10000,
3080 .tdma_offset = 0x11000,
3081 .words_per_bd = 2,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003082 .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR |
3083 GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003084 },
3085 [GENET_V4] = {
3086 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08003087 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08003088 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08003089 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003090 .bp_in_en_shift = 17,
3091 .bp_in_mask = 0x1ffff,
3092 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07003093 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003094 .qtag_mask = 0x3F,
3095 .tbuf_offset = 0x0600,
3096 .hfb_offset = 0x8000,
3097 .hfb_reg_offset = 0xfc00,
3098 .rdma_offset = 0x2000,
3099 .tdma_offset = 0x4000,
3100 .words_per_bd = 3,
Petri Gynther8d88c6e2015-04-01 00:40:00 -07003101 .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3102 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003103 },
3104};
3105
3106/* Infer hardware parameters from the detected GENET version */
3107static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
3108{
3109 struct bcmgenet_hw_params *params;
3110 u32 reg;
3111 u8 major;
Florian Fainellib04a2f52014-12-03 09:56:59 -08003112 u16 gphy_rev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003113
3114 if (GENET_IS_V4(priv)) {
3115 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3116 genet_dma_ring_regs = genet_dma_ring_regs_v4;
3117 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
3118 priv->version = GENET_V4;
3119 } else if (GENET_IS_V3(priv)) {
3120 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3121 genet_dma_ring_regs = genet_dma_ring_regs_v123;
3122 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
3123 priv->version = GENET_V3;
3124 } else if (GENET_IS_V2(priv)) {
3125 bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
3126 genet_dma_ring_regs = genet_dma_ring_regs_v123;
3127 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
3128 priv->version = GENET_V2;
3129 } else if (GENET_IS_V1(priv)) {
3130 bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
3131 genet_dma_ring_regs = genet_dma_ring_regs_v123;
3132 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
3133 priv->version = GENET_V1;
3134 }
3135
3136 /* enum genet_version starts at 1 */
3137 priv->hw_params = &bcmgenet_hw_params[priv->version];
3138 params = priv->hw_params;
3139
3140 /* Read GENET HW version */
3141 reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
3142 major = (reg >> 24 & 0x0f);
3143 if (major == 5)
3144 major = 4;
3145 else if (major == 0)
3146 major = 1;
3147 if (major != priv->version) {
3148 dev_err(&priv->pdev->dev,
3149 "GENET version mismatch, got: %d, configured for: %d\n",
3150 major, priv->version);
3151 }
3152
3153 /* Print the GENET core version */
3154 dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003155 major, (reg >> 16) & 0x0f, reg & 0xffff);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003156
Florian Fainelli487320c2014-09-19 13:07:53 -07003157 /* Store the integrated PHY revision for the MDIO probing function
3158 * to pass this information to the PHY driver. The PHY driver expects
3159 * to find the PHY major revision in bits 15:8 while the GENET register
3160 * stores that information in bits 7:0, account for that.
Florian Fainellib04a2f52014-12-03 09:56:59 -08003161 *
3162 * On newer chips, starting with PHY revision G0, a new scheme is
3163 * deployed similar to the Starfighter 2 switch with GPHY major
3164 * revision in bits 15:8 and patch level in bits 7:0. Major revision 0
3165 * is reserved as well as special value 0x01ff, we have a small
3166 * heuristic to check for the new GPHY revision and re-arrange things
3167 * so the GPHY driver is happy.
Florian Fainelli487320c2014-09-19 13:07:53 -07003168 */
Florian Fainellib04a2f52014-12-03 09:56:59 -08003169 gphy_rev = reg & 0xffff;
3170
3171 /* This is the good old scheme, just GPHY major, no minor nor patch */
3172 if ((gphy_rev & 0xf0) != 0)
3173 priv->gphy_rev = gphy_rev << 8;
3174
3175 /* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */
3176 else if ((gphy_rev & 0xff00) != 0)
3177 priv->gphy_rev = gphy_rev;
3178
3179 /* This is reserved so should require special treatment */
3180 else if (gphy_rev == 0 || gphy_rev == 0x01ff) {
3181 pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev);
3182 return;
3183 }
Florian Fainelli487320c2014-09-19 13:07:53 -07003184
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003185#ifdef CONFIG_PHYS_ADDR_T_64BIT
3186 if (!(params->flags & GENET_HAS_40BITS))
3187 pr_warn("GENET does not support 40-bits PA\n");
3188#endif
3189
3190 pr_debug("Configuration for version: %d\n"
Petri Gynther3feafa02015-03-05 17:40:14 -08003191 "TXq: %1d, TXqBDs: %1d, RXq: %1d, RXqBDs: %1d\n"
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003192 "BP << en: %2d, BP msk: 0x%05x\n"
3193 "HFB count: %2d, QTAQ msk: 0x%05x\n"
3194 "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
3195 "RDMA: 0x%05x, TDMA: 0x%05x\n"
3196 "Words/BD: %d\n",
3197 priv->version,
Petri Gynther51a966a2015-02-23 11:00:46 -08003198 params->tx_queues, params->tx_bds_per_q,
Petri Gynther3feafa02015-03-05 17:40:14 -08003199 params->rx_queues, params->rx_bds_per_q,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003200 params->bp_in_en_shift, params->bp_in_mask,
3201 params->hfb_filter_cnt, params->qtag_mask,
3202 params->tbuf_offset, params->hfb_offset,
3203 params->hfb_reg_offset,
3204 params->rdma_offset, params->tdma_offset,
3205 params->words_per_bd);
3206}
3207
3208static const struct of_device_id bcmgenet_match[] = {
3209 { .compatible = "brcm,genet-v1", .data = (void *)GENET_V1 },
3210 { .compatible = "brcm,genet-v2", .data = (void *)GENET_V2 },
3211 { .compatible = "brcm,genet-v3", .data = (void *)GENET_V3 },
3212 { .compatible = "brcm,genet-v4", .data = (void *)GENET_V4 },
3213 { },
3214};
Luis de Bethencourte8048e52015-09-18 17:55:02 +02003215MODULE_DEVICE_TABLE(of, bcmgenet_match);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003216
3217static int bcmgenet_probe(struct platform_device *pdev)
3218{
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003219 struct bcmgenet_platform_data *pd = pdev->dev.platform_data;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003220 struct device_node *dn = pdev->dev.of_node;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003221 const struct of_device_id *of_id = NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003222 struct bcmgenet_priv *priv;
3223 struct net_device *dev;
3224 const void *macaddr;
3225 struct resource *r;
3226 int err = -EIO;
3227
Petri Gynther3feafee2015-03-05 17:40:12 -08003228 /* Up to GENET_MAX_MQ_CNT + 1 TX queues and RX queues */
3229 dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1,
3230 GENET_MAX_MQ_CNT + 1);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003231 if (!dev) {
3232 dev_err(&pdev->dev, "can't allocate net device\n");
3233 return -ENOMEM;
3234 }
3235
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003236 if (dn) {
3237 of_id = of_match_node(bcmgenet_match, dn);
3238 if (!of_id)
3239 return -EINVAL;
3240 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003241
3242 priv = netdev_priv(dev);
3243 priv->irq0 = platform_get_irq(pdev, 0);
3244 priv->irq1 = platform_get_irq(pdev, 1);
Florian Fainelli85620562014-07-21 15:29:23 -07003245 priv->wol_irq = platform_get_irq(pdev, 2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003246 if (!priv->irq0 || !priv->irq1) {
3247 dev_err(&pdev->dev, "can't find IRQs\n");
3248 err = -EINVAL;
3249 goto err;
3250 }
3251
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003252 if (dn) {
3253 macaddr = of_get_mac_address(dn);
3254 if (!macaddr) {
3255 dev_err(&pdev->dev, "can't find MAC address\n");
3256 err = -EINVAL;
3257 goto err;
3258 }
3259 } else {
3260 macaddr = pd->mac_address;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003261 }
3262
3263 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam5343a102014-02-24 00:47:24 -03003264 priv->base = devm_ioremap_resource(&pdev->dev, r);
3265 if (IS_ERR(priv->base)) {
3266 err = PTR_ERR(priv->base);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003267 goto err;
3268 }
3269
3270 SET_NETDEV_DEV(dev, &pdev->dev);
3271 dev_set_drvdata(&pdev->dev, dev);
3272 ether_addr_copy(dev->dev_addr, macaddr);
3273 dev->watchdog_timeo = 2 * HZ;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003274 dev->ethtool_ops = &bcmgenet_ethtool_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003275 dev->netdev_ops = &bcmgenet_netdev_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003276
3277 priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
3278
3279 /* Set hardware features */
3280 dev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM |
3281 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
3282
Florian Fainelli85620562014-07-21 15:29:23 -07003283 /* Request the WOL interrupt and advertise suspend if available */
3284 priv->wol_irq_disabled = true;
3285 err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0,
3286 dev->name, priv);
3287 if (!err)
3288 device_set_wakeup_capable(&pdev->dev, 1);
3289
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003290 /* Set the needed headroom to account for any possible
3291 * features enabling/disabling at runtime
3292 */
3293 dev->needed_headroom += 64;
3294
3295 netdev_boot_setup_check(dev);
3296
3297 priv->dev = dev;
3298 priv->pdev = pdev;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003299 if (of_id)
3300 priv->version = (enum bcmgenet_version)of_id->data;
3301 else
3302 priv->version = pd->genet_version;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003303
Florian Fainellie4a60a92014-08-11 14:50:42 -07003304 priv->clk = devm_clk_get(&priv->pdev->dev, "enet");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003305 if (IS_ERR(priv->clk)) {
Florian Fainellie4a60a92014-08-11 14:50:42 -07003306 dev_warn(&priv->pdev->dev, "failed to get enet clock\n");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003307 priv->clk = NULL;
3308 }
Florian Fainellie4a60a92014-08-11 14:50:42 -07003309
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003310 clk_prepare_enable(priv->clk);
Florian Fainellie4a60a92014-08-11 14:50:42 -07003311
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003312 bcmgenet_set_hw_params(priv);
3313
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003314 /* Mii wait queue */
3315 init_waitqueue_head(&priv->wq);
3316 /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
3317 priv->rx_buf_len = RX_BUF_LENGTH;
3318 INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
3319
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003320 priv->clk_wol = devm_clk_get(&priv->pdev->dev, "enet-wol");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003321 if (IS_ERR(priv->clk_wol)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003322 dev_warn(&priv->pdev->dev, "failed to get enet-wol clock\n");
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003323 priv->clk_wol = NULL;
3324 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003325
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003326 priv->clk_eee = devm_clk_get(&priv->pdev->dev, "enet-eee");
3327 if (IS_ERR(priv->clk_eee)) {
3328 dev_warn(&priv->pdev->dev, "failed to get enet-eee clock\n");
3329 priv->clk_eee = NULL;
3330 }
3331
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003332 err = reset_umac(priv);
3333 if (err)
3334 goto err_clk_disable;
3335
3336 err = bcmgenet_mii_init(dev);
3337 if (err)
3338 goto err_clk_disable;
3339
3340 /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
3341 * just the ring 16 descriptor based TX
3342 */
3343 netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
3344 netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
3345
Florian Fainelli219575e2014-06-26 10:26:21 -07003346 /* libphy will determine the link state */
3347 netif_carrier_off(dev);
3348
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003349 /* Turn off the main clock, WOL clock is handled separately */
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003350 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003351
Florian Fainelli0f50ce92014-06-26 10:26:20 -07003352 err = register_netdev(dev);
3353 if (err)
3354 goto err;
3355
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003356 return err;
3357
3358err_clk_disable:
Florian Fainelli7d5d3072015-07-22 17:28:23 -07003359 clk_disable_unprepare(priv->clk);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003360err:
3361 free_netdev(dev);
3362 return err;
3363}
3364
3365static int bcmgenet_remove(struct platform_device *pdev)
3366{
3367 struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
3368
3369 dev_set_drvdata(&pdev->dev, NULL);
3370 unregister_netdev(priv->dev);
3371 bcmgenet_mii_exit(priv->dev);
3372 free_netdev(priv->dev);
3373
3374 return 0;
3375}
3376
Florian Fainellib6e978e2014-07-21 15:29:22 -07003377#ifdef CONFIG_PM_SLEEP
3378static int bcmgenet_suspend(struct device *d)
3379{
3380 struct net_device *dev = dev_get_drvdata(d);
3381 struct bcmgenet_priv *priv = netdev_priv(dev);
3382 int ret;
3383
3384 if (!netif_running(dev))
3385 return 0;
3386
3387 bcmgenet_netif_stop(dev);
3388
Florian Fainelli0299b6a2016-09-26 22:31:56 +02003389 phy_suspend(priv->phydev);
Florian Fainellicc013fb2014-08-11 14:50:43 -07003390
Florian Fainellib6e978e2014-07-21 15:29:22 -07003391 netif_device_detach(dev);
3392
3393 /* Disable MAC receive */
3394 umac_enable_set(priv, CMD_RX_EN, false);
3395
3396 ret = bcmgenet_dma_teardown(priv);
3397 if (ret)
3398 return ret;
3399
3400 /* Disable MAC transmit. TX DMA disabled have to done before this */
3401 umac_enable_set(priv, CMD_TX_EN, false);
3402
3403 /* tx reclaim */
3404 bcmgenet_tx_reclaim_all(dev);
3405 bcmgenet_fini_dma(priv);
3406
Florian Fainelli8c90db72014-07-21 15:29:28 -07003407 /* Prepare the device for Wake-on-LAN and switch to the slow clock */
3408 if (device_may_wakeup(d) && priv->wolopts) {
Florian Fainellica8cf342015-03-23 15:09:51 -07003409 ret = bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003410 clk_prepare_enable(priv->clk_wol);
Florian Fainellic624f892015-07-16 15:51:17 -07003411 } else if (priv->internal_phy) {
Florian Fainellia6f31f52015-03-23 15:09:57 -07003412 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003413 }
3414
Florian Fainellib6e978e2014-07-21 15:29:22 -07003415 /* Turn off the clocks */
3416 clk_disable_unprepare(priv->clk);
3417
Florian Fainellica8cf342015-03-23 15:09:51 -07003418 return ret;
Florian Fainellib6e978e2014-07-21 15:29:22 -07003419}
3420
3421static int bcmgenet_resume(struct device *d)
3422{
3423 struct net_device *dev = dev_get_drvdata(d);
3424 struct bcmgenet_priv *priv = netdev_priv(dev);
3425 unsigned long dma_ctrl;
3426 int ret;
3427 u32 reg;
3428
3429 if (!netif_running(dev))
3430 return 0;
3431
3432 /* Turn on the clock */
3433 ret = clk_prepare_enable(priv->clk);
3434 if (ret)
3435 return ret;
3436
Florian Fainellia6f31f52015-03-23 15:09:57 -07003437 /* If this is an internal GPHY, power it back on now, before UniMAC is
3438 * brought out of reset as absolutely no UniMAC activity is allowed
3439 */
Florian Fainellic624f892015-07-16 15:51:17 -07003440 if (priv->internal_phy)
Florian Fainellia6f31f52015-03-23 15:09:57 -07003441 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3442
Florian Fainellib6e978e2014-07-21 15:29:22 -07003443 bcmgenet_umac_reset(priv);
3444
3445 ret = init_umac(priv);
3446 if (ret)
3447 goto out_clk_disable;
3448
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003449 /* From WOL-enabled suspend, switch to regular clock */
3450 if (priv->wolopts)
3451 clk_disable_unprepare(priv->clk_wol);
3452
Florian Fainelli0299b6a2016-09-26 22:31:56 +02003453 phy_init_hw(priv->phydev);
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003454 /* Speed settings must be restored */
Florian Fainelli28b45912015-07-16 15:51:19 -07003455 bcmgenet_mii_config(priv->dev);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003456
Florian Fainellib6e978e2014-07-21 15:29:22 -07003457 /* disable ethernet MAC while updating its registers */
3458 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
3459
3460 bcmgenet_set_hw_addr(priv, dev->dev_addr);
3461
Florian Fainellic624f892015-07-16 15:51:17 -07003462 if (priv->internal_phy) {
Florian Fainellib6e978e2014-07-21 15:29:22 -07003463 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
3464 reg |= EXT_ENERGY_DET_MASK;
3465 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
3466 }
3467
Florian Fainelli98bb7392014-08-11 14:50:45 -07003468 if (priv->wolopts)
3469 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
3470
Florian Fainellib6e978e2014-07-21 15:29:22 -07003471 /* Disable RX/TX DMA and flush TX queues */
3472 dma_ctrl = bcmgenet_dma_disable(priv);
3473
3474 /* Reinitialize TDMA and RDMA and SW housekeeping */
3475 ret = bcmgenet_init_dma(priv);
3476 if (ret) {
3477 netdev_err(dev, "failed to initialize DMA\n");
3478 goto out_clk_disable;
3479 }
3480
3481 /* Always enable ring 16 - descriptor ring */
3482 bcmgenet_enable_dma(priv, dma_ctrl);
3483
3484 netif_device_attach(dev);
3485
Florian Fainelli0299b6a2016-09-26 22:31:56 +02003486 phy_resume(priv->phydev);
Florian Fainellicc013fb2014-08-11 14:50:43 -07003487
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003488 if (priv->eee.eee_enabled)
3489 bcmgenet_eee_enable_set(dev, true);
3490
Florian Fainellib6e978e2014-07-21 15:29:22 -07003491 bcmgenet_netif_start(dev);
3492
3493 return 0;
3494
3495out_clk_disable:
3496 clk_disable_unprepare(priv->clk);
3497 return ret;
3498}
3499#endif /* CONFIG_PM_SLEEP */
3500
3501static SIMPLE_DEV_PM_OPS(bcmgenet_pm_ops, bcmgenet_suspend, bcmgenet_resume);
3502
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003503static struct platform_driver bcmgenet_driver = {
3504 .probe = bcmgenet_probe,
3505 .remove = bcmgenet_remove,
3506 .driver = {
3507 .name = "bcmgenet",
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003508 .of_match_table = bcmgenet_match,
Florian Fainellib6e978e2014-07-21 15:29:22 -07003509 .pm = &bcmgenet_pm_ops,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003510 },
3511};
3512module_platform_driver(bcmgenet_driver);
3513
3514MODULE_AUTHOR("Broadcom Corporation");
3515MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
3516MODULE_ALIAS("platform:bcmgenet");
3517MODULE_LICENSE("GPL");