blob: dc3b1faf6bbdadf0665ff68b3c1d22b8f8f0027c [file] [log] [blame]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001/*
2 * Broadcom GENET (Gigabit Ethernet) controller driver
3 *
4 * Copyright (c) 2014 Broadcom Corporation
5 *
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{
107 dmadesc_set_length_status(priv, d, val);
108 dmadesc_set_addr(priv, d, addr);
109}
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 Fainelli1c1008c2014-02-13 16:08:47 -0800208};
209
210static const u8 bcmgenet_dma_regs_v3plus[] = {
211 [DMA_RING_CFG] = 0x00,
212 [DMA_CTRL] = 0x04,
213 [DMA_STATUS] = 0x08,
214 [DMA_SCB_BURST_SIZE] = 0x0C,
215 [DMA_ARB_CTRL] = 0x2C,
Petri Gynther37742162014-10-07 09:30:01 -0700216 [DMA_PRIORITY_0] = 0x30,
217 [DMA_PRIORITY_1] = 0x34,
218 [DMA_PRIORITY_2] = 0x38,
Petri Gynther0034de42015-03-13 14:45:00 -0700219 [DMA_INDEX2RING_0] = 0x70,
220 [DMA_INDEX2RING_1] = 0x74,
221 [DMA_INDEX2RING_2] = 0x78,
222 [DMA_INDEX2RING_3] = 0x7C,
223 [DMA_INDEX2RING_4] = 0x80,
224 [DMA_INDEX2RING_5] = 0x84,
225 [DMA_INDEX2RING_6] = 0x88,
226 [DMA_INDEX2RING_7] = 0x8C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800227};
228
229static const u8 bcmgenet_dma_regs_v2[] = {
230 [DMA_RING_CFG] = 0x00,
231 [DMA_CTRL] = 0x04,
232 [DMA_STATUS] = 0x08,
233 [DMA_SCB_BURST_SIZE] = 0x0C,
234 [DMA_ARB_CTRL] = 0x30,
Petri Gynther37742162014-10-07 09:30:01 -0700235 [DMA_PRIORITY_0] = 0x34,
236 [DMA_PRIORITY_1] = 0x38,
237 [DMA_PRIORITY_2] = 0x3C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800238};
239
240static const u8 bcmgenet_dma_regs_v1[] = {
241 [DMA_CTRL] = 0x00,
242 [DMA_STATUS] = 0x04,
243 [DMA_SCB_BURST_SIZE] = 0x0C,
244 [DMA_ARB_CTRL] = 0x30,
Petri Gynther37742162014-10-07 09:30:01 -0700245 [DMA_PRIORITY_0] = 0x34,
246 [DMA_PRIORITY_1] = 0x38,
247 [DMA_PRIORITY_2] = 0x3C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800248};
249
250/* Set at runtime once bcmgenet version is known */
251static const u8 *bcmgenet_dma_regs;
252
253static inline struct bcmgenet_priv *dev_to_priv(struct device *dev)
254{
255 return netdev_priv(dev_get_drvdata(dev));
256}
257
258static inline u32 bcmgenet_tdma_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700259 enum dma_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800260{
261 return __raw_readl(priv->base + GENET_TDMA_REG_OFF +
262 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
263}
264
265static inline void bcmgenet_tdma_writel(struct bcmgenet_priv *priv,
266 u32 val, enum dma_reg r)
267{
268 __raw_writel(val, priv->base + GENET_TDMA_REG_OFF +
269 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
270}
271
272static inline u32 bcmgenet_rdma_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700273 enum dma_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800274{
275 return __raw_readl(priv->base + GENET_RDMA_REG_OFF +
276 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
277}
278
279static inline void bcmgenet_rdma_writel(struct bcmgenet_priv *priv,
280 u32 val, enum dma_reg r)
281{
282 __raw_writel(val, priv->base + GENET_RDMA_REG_OFF +
283 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
284}
285
286/* RDMA/TDMA ring registers and accessors
287 * we merge the common fields and just prefix with T/D the registers
288 * having different meaning depending on the direction
289 */
290enum dma_ring_reg {
291 TDMA_READ_PTR = 0,
292 RDMA_WRITE_PTR = TDMA_READ_PTR,
293 TDMA_READ_PTR_HI,
294 RDMA_WRITE_PTR_HI = TDMA_READ_PTR_HI,
295 TDMA_CONS_INDEX,
296 RDMA_PROD_INDEX = TDMA_CONS_INDEX,
297 TDMA_PROD_INDEX,
298 RDMA_CONS_INDEX = TDMA_PROD_INDEX,
299 DMA_RING_BUF_SIZE,
300 DMA_START_ADDR,
301 DMA_START_ADDR_HI,
302 DMA_END_ADDR,
303 DMA_END_ADDR_HI,
304 DMA_MBUF_DONE_THRESH,
305 TDMA_FLOW_PERIOD,
306 RDMA_XON_XOFF_THRESH = TDMA_FLOW_PERIOD,
307 TDMA_WRITE_PTR,
308 RDMA_READ_PTR = TDMA_WRITE_PTR,
309 TDMA_WRITE_PTR_HI,
310 RDMA_READ_PTR_HI = TDMA_WRITE_PTR_HI
311};
312
313/* GENET v4 supports 40-bits pointer addressing
314 * for obvious reasons the LO and HI word parts
315 * are contiguous, but this offsets the other
316 * registers.
317 */
318static const u8 genet_dma_ring_regs_v4[] = {
319 [TDMA_READ_PTR] = 0x00,
320 [TDMA_READ_PTR_HI] = 0x04,
321 [TDMA_CONS_INDEX] = 0x08,
322 [TDMA_PROD_INDEX] = 0x0C,
323 [DMA_RING_BUF_SIZE] = 0x10,
324 [DMA_START_ADDR] = 0x14,
325 [DMA_START_ADDR_HI] = 0x18,
326 [DMA_END_ADDR] = 0x1C,
327 [DMA_END_ADDR_HI] = 0x20,
328 [DMA_MBUF_DONE_THRESH] = 0x24,
329 [TDMA_FLOW_PERIOD] = 0x28,
330 [TDMA_WRITE_PTR] = 0x2C,
331 [TDMA_WRITE_PTR_HI] = 0x30,
332};
333
334static const u8 genet_dma_ring_regs_v123[] = {
335 [TDMA_READ_PTR] = 0x00,
336 [TDMA_CONS_INDEX] = 0x04,
337 [TDMA_PROD_INDEX] = 0x08,
338 [DMA_RING_BUF_SIZE] = 0x0C,
339 [DMA_START_ADDR] = 0x10,
340 [DMA_END_ADDR] = 0x14,
341 [DMA_MBUF_DONE_THRESH] = 0x18,
342 [TDMA_FLOW_PERIOD] = 0x1C,
343 [TDMA_WRITE_PTR] = 0x20,
344};
345
346/* Set at runtime once GENET version is known */
347static const u8 *genet_dma_ring_regs;
348
349static inline u32 bcmgenet_tdma_ring_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700350 unsigned int ring,
351 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800352{
353 return __raw_readl(priv->base + GENET_TDMA_REG_OFF +
354 (DMA_RING_SIZE * ring) +
355 genet_dma_ring_regs[r]);
356}
357
358static inline void bcmgenet_tdma_ring_writel(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700359 unsigned int ring, u32 val,
360 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800361{
362 __raw_writel(val, priv->base + GENET_TDMA_REG_OFF +
363 (DMA_RING_SIZE * ring) +
364 genet_dma_ring_regs[r]);
365}
366
367static inline u32 bcmgenet_rdma_ring_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700368 unsigned int ring,
369 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800370{
371 return __raw_readl(priv->base + GENET_RDMA_REG_OFF +
372 (DMA_RING_SIZE * ring) +
373 genet_dma_ring_regs[r]);
374}
375
376static inline void bcmgenet_rdma_ring_writel(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700377 unsigned int ring, u32 val,
378 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800379{
380 __raw_writel(val, priv->base + GENET_RDMA_REG_OFF +
381 (DMA_RING_SIZE * ring) +
382 genet_dma_ring_regs[r]);
383}
384
385static int bcmgenet_get_settings(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700386 struct ethtool_cmd *cmd)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800387{
388 struct bcmgenet_priv *priv = netdev_priv(dev);
389
390 if (!netif_running(dev))
391 return -EINVAL;
392
393 if (!priv->phydev)
394 return -ENODEV;
395
396 return phy_ethtool_gset(priv->phydev, cmd);
397}
398
399static int bcmgenet_set_settings(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700400 struct ethtool_cmd *cmd)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800401{
402 struct bcmgenet_priv *priv = netdev_priv(dev);
403
404 if (!netif_running(dev))
405 return -EINVAL;
406
407 if (!priv->phydev)
408 return -ENODEV;
409
410 return phy_ethtool_sset(priv->phydev, cmd);
411}
412
413static int bcmgenet_set_rx_csum(struct net_device *dev,
414 netdev_features_t wanted)
415{
416 struct bcmgenet_priv *priv = netdev_priv(dev);
417 u32 rbuf_chk_ctrl;
418 bool rx_csum_en;
419
420 rx_csum_en = !!(wanted & NETIF_F_RXCSUM);
421
422 rbuf_chk_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CHK_CTRL);
423
424 /* enable rx checksumming */
425 if (rx_csum_en)
426 rbuf_chk_ctrl |= RBUF_RXCHK_EN;
427 else
428 rbuf_chk_ctrl &= ~RBUF_RXCHK_EN;
429 priv->desc_rxchk_en = rx_csum_en;
Florian Fainelliebe5e3c2014-03-26 21:18:39 -0700430
431 /* If UniMAC forwards CRC, we need to skip over it to get
432 * a valid CHK bit to be set in the per-packet status word
433 */
434 if (rx_csum_en && priv->crc_fwd_en)
435 rbuf_chk_ctrl |= RBUF_SKIP_FCS;
436 else
437 rbuf_chk_ctrl &= ~RBUF_SKIP_FCS;
438
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800439 bcmgenet_rbuf_writel(priv, rbuf_chk_ctrl, RBUF_CHK_CTRL);
440
441 return 0;
442}
443
444static int bcmgenet_set_tx_csum(struct net_device *dev,
445 netdev_features_t wanted)
446{
447 struct bcmgenet_priv *priv = netdev_priv(dev);
448 bool desc_64b_en;
449 u32 tbuf_ctrl, rbuf_ctrl;
450
451 tbuf_ctrl = bcmgenet_tbuf_ctrl_get(priv);
452 rbuf_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
453
454 desc_64b_en = !!(wanted & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
455
456 /* enable 64 bytes descriptor in both directions (RBUF and TBUF) */
457 if (desc_64b_en) {
458 tbuf_ctrl |= RBUF_64B_EN;
459 rbuf_ctrl |= RBUF_64B_EN;
460 } else {
461 tbuf_ctrl &= ~RBUF_64B_EN;
462 rbuf_ctrl &= ~RBUF_64B_EN;
463 }
464 priv->desc_64b_en = desc_64b_en;
465
466 bcmgenet_tbuf_ctrl_set(priv, tbuf_ctrl);
467 bcmgenet_rbuf_writel(priv, rbuf_ctrl, RBUF_CTRL);
468
469 return 0;
470}
471
472static int bcmgenet_set_features(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700473 netdev_features_t features)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800474{
475 netdev_features_t changed = features ^ dev->features;
476 netdev_features_t wanted = dev->wanted_features;
477 int ret = 0;
478
479 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
480 ret = bcmgenet_set_tx_csum(dev, wanted);
481 if (changed & (NETIF_F_RXCSUM))
482 ret = bcmgenet_set_rx_csum(dev, wanted);
483
484 return ret;
485}
486
487static u32 bcmgenet_get_msglevel(struct net_device *dev)
488{
489 struct bcmgenet_priv *priv = netdev_priv(dev);
490
491 return priv->msg_enable;
492}
493
494static void bcmgenet_set_msglevel(struct net_device *dev, u32 level)
495{
496 struct bcmgenet_priv *priv = netdev_priv(dev);
497
498 priv->msg_enable = level;
499}
500
501/* standard ethtool support functions. */
502enum bcmgenet_stat_type {
503 BCMGENET_STAT_NETDEV = -1,
504 BCMGENET_STAT_MIB_RX,
505 BCMGENET_STAT_MIB_TX,
506 BCMGENET_STAT_RUNT,
507 BCMGENET_STAT_MISC,
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800508 BCMGENET_STAT_SOFT,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800509};
510
511struct bcmgenet_stats {
512 char stat_string[ETH_GSTRING_LEN];
513 int stat_sizeof;
514 int stat_offset;
515 enum bcmgenet_stat_type type;
516 /* reg offset from UMAC base for misc counters */
517 u16 reg_offset;
518};
519
520#define STAT_NETDEV(m) { \
521 .stat_string = __stringify(m), \
522 .stat_sizeof = sizeof(((struct net_device_stats *)0)->m), \
523 .stat_offset = offsetof(struct net_device_stats, m), \
524 .type = BCMGENET_STAT_NETDEV, \
525}
526
527#define STAT_GENET_MIB(str, m, _type) { \
528 .stat_string = str, \
529 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
530 .stat_offset = offsetof(struct bcmgenet_priv, m), \
531 .type = _type, \
532}
533
534#define STAT_GENET_MIB_RX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_RX)
535#define STAT_GENET_MIB_TX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_TX)
536#define STAT_GENET_RUNT(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_RUNT)
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800537#define STAT_GENET_SOFT_MIB(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_SOFT)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800538
539#define STAT_GENET_MISC(str, m, offset) { \
540 .stat_string = str, \
541 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
542 .stat_offset = offsetof(struct bcmgenet_priv, m), \
543 .type = BCMGENET_STAT_MISC, \
544 .reg_offset = offset, \
545}
546
547
548/* There is a 0xC gap between the end of RX and beginning of TX stats and then
549 * between the end of TX stats and the beginning of the RX RUNT
550 */
551#define BCMGENET_STAT_OFFSET 0xc
552
553/* Hardware counters must be kept in sync because the order/offset
554 * is important here (order in structure declaration = order in hardware)
555 */
556static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
557 /* general stats */
558 STAT_NETDEV(rx_packets),
559 STAT_NETDEV(tx_packets),
560 STAT_NETDEV(rx_bytes),
561 STAT_NETDEV(tx_bytes),
562 STAT_NETDEV(rx_errors),
563 STAT_NETDEV(tx_errors),
564 STAT_NETDEV(rx_dropped),
565 STAT_NETDEV(tx_dropped),
566 STAT_NETDEV(multicast),
567 /* UniMAC RSV counters */
568 STAT_GENET_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
569 STAT_GENET_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
570 STAT_GENET_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
571 STAT_GENET_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
572 STAT_GENET_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
573 STAT_GENET_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
574 STAT_GENET_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
575 STAT_GENET_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
576 STAT_GENET_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
577 STAT_GENET_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
578 STAT_GENET_MIB_RX("rx_pkts", mib.rx.pkt),
579 STAT_GENET_MIB_RX("rx_bytes", mib.rx.bytes),
580 STAT_GENET_MIB_RX("rx_multicast", mib.rx.mca),
581 STAT_GENET_MIB_RX("rx_broadcast", mib.rx.bca),
582 STAT_GENET_MIB_RX("rx_fcs", mib.rx.fcs),
583 STAT_GENET_MIB_RX("rx_control", mib.rx.cf),
584 STAT_GENET_MIB_RX("rx_pause", mib.rx.pf),
585 STAT_GENET_MIB_RX("rx_unknown", mib.rx.uo),
586 STAT_GENET_MIB_RX("rx_align", mib.rx.aln),
587 STAT_GENET_MIB_RX("rx_outrange", mib.rx.flr),
588 STAT_GENET_MIB_RX("rx_code", mib.rx.cde),
589 STAT_GENET_MIB_RX("rx_carrier", mib.rx.fcr),
590 STAT_GENET_MIB_RX("rx_oversize", mib.rx.ovr),
591 STAT_GENET_MIB_RX("rx_jabber", mib.rx.jbr),
592 STAT_GENET_MIB_RX("rx_mtu_err", mib.rx.mtue),
593 STAT_GENET_MIB_RX("rx_good_pkts", mib.rx.pok),
594 STAT_GENET_MIB_RX("rx_unicast", mib.rx.uc),
595 STAT_GENET_MIB_RX("rx_ppp", mib.rx.ppp),
596 STAT_GENET_MIB_RX("rx_crc", mib.rx.rcrc),
597 /* UniMAC TSV counters */
598 STAT_GENET_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
599 STAT_GENET_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
600 STAT_GENET_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
601 STAT_GENET_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
602 STAT_GENET_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
603 STAT_GENET_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
604 STAT_GENET_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
605 STAT_GENET_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
606 STAT_GENET_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
607 STAT_GENET_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
608 STAT_GENET_MIB_TX("tx_pkts", mib.tx.pkts),
609 STAT_GENET_MIB_TX("tx_multicast", mib.tx.mca),
610 STAT_GENET_MIB_TX("tx_broadcast", mib.tx.bca),
611 STAT_GENET_MIB_TX("tx_pause", mib.tx.pf),
612 STAT_GENET_MIB_TX("tx_control", mib.tx.cf),
613 STAT_GENET_MIB_TX("tx_fcs_err", mib.tx.fcs),
614 STAT_GENET_MIB_TX("tx_oversize", mib.tx.ovr),
615 STAT_GENET_MIB_TX("tx_defer", mib.tx.drf),
616 STAT_GENET_MIB_TX("tx_excess_defer", mib.tx.edf),
617 STAT_GENET_MIB_TX("tx_single_col", mib.tx.scl),
618 STAT_GENET_MIB_TX("tx_multi_col", mib.tx.mcl),
619 STAT_GENET_MIB_TX("tx_late_col", mib.tx.lcl),
620 STAT_GENET_MIB_TX("tx_excess_col", mib.tx.ecl),
621 STAT_GENET_MIB_TX("tx_frags", mib.tx.frg),
622 STAT_GENET_MIB_TX("tx_total_col", mib.tx.ncl),
623 STAT_GENET_MIB_TX("tx_jabber", mib.tx.jbr),
624 STAT_GENET_MIB_TX("tx_bytes", mib.tx.bytes),
625 STAT_GENET_MIB_TX("tx_good_pkts", mib.tx.pok),
626 STAT_GENET_MIB_TX("tx_unicast", mib.tx.uc),
627 /* UniMAC RUNT counters */
628 STAT_GENET_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
629 STAT_GENET_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
630 STAT_GENET_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
631 STAT_GENET_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
632 /* Misc UniMAC counters */
633 STAT_GENET_MISC("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt,
634 UMAC_RBUF_OVFL_CNT),
635 STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt, UMAC_RBUF_ERR_CNT),
636 STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT),
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800637 STAT_GENET_SOFT_MIB("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
638 STAT_GENET_SOFT_MIB("rx_dma_failed", mib.rx_dma_failed),
639 STAT_GENET_SOFT_MIB("tx_dma_failed", mib.tx_dma_failed),
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800640};
641
642#define BCMGENET_STATS_LEN ARRAY_SIZE(bcmgenet_gstrings_stats)
643
644static void bcmgenet_get_drvinfo(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700645 struct ethtool_drvinfo *info)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800646{
647 strlcpy(info->driver, "bcmgenet", sizeof(info->driver));
648 strlcpy(info->version, "v2.0", sizeof(info->version));
649 info->n_stats = BCMGENET_STATS_LEN;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800650}
651
652static int bcmgenet_get_sset_count(struct net_device *dev, int string_set)
653{
654 switch (string_set) {
655 case ETH_SS_STATS:
656 return BCMGENET_STATS_LEN;
657 default:
658 return -EOPNOTSUPP;
659 }
660}
661
Florian Fainellic91b7f62014-07-23 10:42:12 -0700662static void bcmgenet_get_strings(struct net_device *dev, u32 stringset,
663 u8 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800664{
665 int i;
666
667 switch (stringset) {
668 case ETH_SS_STATS:
669 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
670 memcpy(data + i * ETH_GSTRING_LEN,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700671 bcmgenet_gstrings_stats[i].stat_string,
672 ETH_GSTRING_LEN);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800673 }
674 break;
675 }
676}
677
678static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv)
679{
680 int i, j = 0;
681
682 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
683 const struct bcmgenet_stats *s;
684 u8 offset = 0;
685 u32 val = 0;
686 char *p;
687
688 s = &bcmgenet_gstrings_stats[i];
689 switch (s->type) {
690 case BCMGENET_STAT_NETDEV:
Florian Fainellif62ba9c2015-02-28 18:09:16 -0800691 case BCMGENET_STAT_SOFT:
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800692 continue;
693 case BCMGENET_STAT_MIB_RX:
694 case BCMGENET_STAT_MIB_TX:
695 case BCMGENET_STAT_RUNT:
696 if (s->type != BCMGENET_STAT_MIB_RX)
697 offset = BCMGENET_STAT_OFFSET;
Florian Fainellic91b7f62014-07-23 10:42:12 -0700698 val = bcmgenet_umac_readl(priv,
699 UMAC_MIB_START + j + offset);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800700 break;
701 case BCMGENET_STAT_MISC:
702 val = bcmgenet_umac_readl(priv, s->reg_offset);
703 /* clear if overflowed */
704 if (val == ~0)
705 bcmgenet_umac_writel(priv, 0, s->reg_offset);
706 break;
707 }
708
709 j += s->stat_sizeof;
710 p = (char *)priv + s->stat_offset;
711 *(u32 *)p = val;
712 }
713}
714
715static void bcmgenet_get_ethtool_stats(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700716 struct ethtool_stats *stats,
717 u64 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800718{
719 struct bcmgenet_priv *priv = netdev_priv(dev);
720 int i;
721
722 if (netif_running(dev))
723 bcmgenet_update_mib_counters(priv);
724
725 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
726 const struct bcmgenet_stats *s;
727 char *p;
728
729 s = &bcmgenet_gstrings_stats[i];
730 if (s->type == BCMGENET_STAT_NETDEV)
731 p = (char *)&dev->stats;
732 else
733 p = (char *)priv;
734 p += s->stat_offset;
735 data[i] = *(u32 *)p;
736 }
737}
738
Florian Fainelli6ef398e2014-11-25 21:16:35 -0800739static void bcmgenet_eee_enable_set(struct net_device *dev, bool enable)
740{
741 struct bcmgenet_priv *priv = netdev_priv(dev);
742 u32 off = priv->hw_params->tbuf_offset + TBUF_ENERGY_CTRL;
743 u32 reg;
744
745 if (enable && !priv->clk_eee_enabled) {
746 clk_prepare_enable(priv->clk_eee);
747 priv->clk_eee_enabled = true;
748 }
749
750 reg = bcmgenet_umac_readl(priv, UMAC_EEE_CTRL);
751 if (enable)
752 reg |= EEE_EN;
753 else
754 reg &= ~EEE_EN;
755 bcmgenet_umac_writel(priv, reg, UMAC_EEE_CTRL);
756
757 /* Enable EEE and switch to a 27Mhz clock automatically */
758 reg = __raw_readl(priv->base + off);
759 if (enable)
760 reg |= TBUF_EEE_EN | TBUF_PM_EN;
761 else
762 reg &= ~(TBUF_EEE_EN | TBUF_PM_EN);
763 __raw_writel(reg, priv->base + off);
764
765 /* Do the same for thing for RBUF */
766 reg = bcmgenet_rbuf_readl(priv, RBUF_ENERGY_CTRL);
767 if (enable)
768 reg |= RBUF_EEE_EN | RBUF_PM_EN;
769 else
770 reg &= ~(RBUF_EEE_EN | RBUF_PM_EN);
771 bcmgenet_rbuf_writel(priv, reg, RBUF_ENERGY_CTRL);
772
773 if (!enable && priv->clk_eee_enabled) {
774 clk_disable_unprepare(priv->clk_eee);
775 priv->clk_eee_enabled = false;
776 }
777
778 priv->eee.eee_enabled = enable;
779 priv->eee.eee_active = enable;
780}
781
782static int bcmgenet_get_eee(struct net_device *dev, struct ethtool_eee *e)
783{
784 struct bcmgenet_priv *priv = netdev_priv(dev);
785 struct ethtool_eee *p = &priv->eee;
786
787 if (GENET_IS_V1(priv))
788 return -EOPNOTSUPP;
789
790 e->eee_enabled = p->eee_enabled;
791 e->eee_active = p->eee_active;
792 e->tx_lpi_timer = bcmgenet_umac_readl(priv, UMAC_EEE_LPI_TIMER);
793
794 return phy_ethtool_get_eee(priv->phydev, e);
795}
796
797static int bcmgenet_set_eee(struct net_device *dev, struct ethtool_eee *e)
798{
799 struct bcmgenet_priv *priv = netdev_priv(dev);
800 struct ethtool_eee *p = &priv->eee;
801 int ret = 0;
802
803 if (GENET_IS_V1(priv))
804 return -EOPNOTSUPP;
805
806 p->eee_enabled = e->eee_enabled;
807
808 if (!p->eee_enabled) {
809 bcmgenet_eee_enable_set(dev, false);
810 } else {
811 ret = phy_init_eee(priv->phydev, 0);
812 if (ret) {
813 netif_err(priv, hw, dev, "EEE initialization failed\n");
814 return ret;
815 }
816
817 bcmgenet_umac_writel(priv, e->tx_lpi_timer, UMAC_EEE_LPI_TIMER);
818 bcmgenet_eee_enable_set(dev, true);
819 }
820
821 return phy_ethtool_set_eee(priv->phydev, e);
822}
823
Florian Fainelli6b0c5402014-11-25 21:16:36 -0800824static int bcmgenet_nway_reset(struct net_device *dev)
825{
826 struct bcmgenet_priv *priv = netdev_priv(dev);
827
828 return genphy_restart_aneg(priv->phydev);
829}
830
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800831/* standard ethtool support functions. */
832static struct ethtool_ops bcmgenet_ethtool_ops = {
833 .get_strings = bcmgenet_get_strings,
834 .get_sset_count = bcmgenet_get_sset_count,
835 .get_ethtool_stats = bcmgenet_get_ethtool_stats,
836 .get_settings = bcmgenet_get_settings,
837 .set_settings = bcmgenet_set_settings,
838 .get_drvinfo = bcmgenet_get_drvinfo,
839 .get_link = ethtool_op_get_link,
840 .get_msglevel = bcmgenet_get_msglevel,
841 .set_msglevel = bcmgenet_set_msglevel,
Florian Fainelli06ba8372014-07-21 15:29:29 -0700842 .get_wol = bcmgenet_get_wol,
843 .set_wol = bcmgenet_set_wol,
Florian Fainelli6ef398e2014-11-25 21:16:35 -0800844 .get_eee = bcmgenet_get_eee,
845 .set_eee = bcmgenet_set_eee,
Florian Fainelli6b0c5402014-11-25 21:16:36 -0800846 .nway_reset = bcmgenet_nway_reset,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800847};
848
849/* Power down the unimac, based on mode. */
Florian Fainellica8cf342015-03-23 15:09:51 -0700850static int bcmgenet_power_down(struct bcmgenet_priv *priv,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800851 enum bcmgenet_power_mode mode)
852{
Florian Fainellica8cf342015-03-23 15:09:51 -0700853 int ret = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800854 u32 reg;
855
856 switch (mode) {
857 case GENET_POWER_CABLE_SENSE:
Florian Fainelli80d8e962014-02-24 16:56:11 -0800858 phy_detach(priv->phydev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800859 break;
860
Florian Fainellic3ae64a2014-07-21 15:29:25 -0700861 case GENET_POWER_WOL_MAGIC:
Florian Fainellica8cf342015-03-23 15:09:51 -0700862 ret = bcmgenet_wol_power_down_cfg(priv, mode);
Florian Fainellic3ae64a2014-07-21 15:29:25 -0700863 break;
864
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800865 case GENET_POWER_PASSIVE:
866 /* Power down LED */
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800867 if (priv->hw_params->flags & GENET_HAS_EXT) {
868 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
869 reg |= (EXT_PWR_DOWN_PHY |
870 EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
871 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
Florian Fainellia642c4f2015-03-23 15:09:56 -0700872
873 bcmgenet_phy_power_set(priv->dev, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800874 }
875 break;
876 default:
877 break;
878 }
Florian Fainellica8cf342015-03-23 15:09:51 -0700879
880 return 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800881}
882
883static void bcmgenet_power_up(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700884 enum bcmgenet_power_mode mode)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800885{
886 u32 reg;
887
888 if (!(priv->hw_params->flags & GENET_HAS_EXT))
889 return;
890
891 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
892
893 switch (mode) {
894 case GENET_POWER_PASSIVE:
895 reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_PHY |
896 EXT_PWR_DOWN_BIAS);
897 /* fallthrough */
898 case GENET_POWER_CABLE_SENSE:
899 /* enable APD */
900 reg |= EXT_PWR_DN_EN_LD;
901 break;
Florian Fainellic3ae64a2014-07-21 15:29:25 -0700902 case GENET_POWER_WOL_MAGIC:
903 bcmgenet_wol_power_up_cfg(priv, mode);
904 return;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800905 default:
906 break;
907 }
908
909 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
Florian Fainellicc013fb2014-08-11 14:50:43 -0700910
911 if (mode == GENET_POWER_PASSIVE)
912 bcmgenet_mii_reset(priv->dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800913}
914
915/* ioctl handle special commands that are not present in ethtool. */
916static int bcmgenet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
917{
918 struct bcmgenet_priv *priv = netdev_priv(dev);
919 int val = 0;
920
921 if (!netif_running(dev))
922 return -EINVAL;
923
924 switch (cmd) {
925 case SIOCGMIIPHY:
926 case SIOCGMIIREG:
927 case SIOCSMIIREG:
928 if (!priv->phydev)
929 val = -ENODEV;
930 else
931 val = phy_mii_ioctl(priv->phydev, rq, cmd);
932 break;
933
934 default:
935 val = -EINVAL;
936 break;
937 }
938
939 return val;
940}
941
942static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv,
943 struct bcmgenet_tx_ring *ring)
944{
945 struct enet_cb *tx_cb_ptr;
946
947 tx_cb_ptr = ring->cbs;
948 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
Petri Gynther014012a2015-02-23 11:00:45 -0800949
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800950 /* Advancing local write pointer */
951 if (ring->write_ptr == ring->end_ptr)
952 ring->write_ptr = ring->cb_ptr;
953 else
954 ring->write_ptr++;
955
956 return tx_cb_ptr;
957}
958
959/* Simple helper to free a control block's resources */
960static void bcmgenet_free_cb(struct enet_cb *cb)
961{
962 dev_kfree_skb_any(cb->skb);
963 cb->skb = NULL;
964 dma_unmap_addr_set(cb, dma_addr, 0);
965}
966
Petri Gynther9dbac282015-03-25 12:35:10 -0700967static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800968{
Petri Gynther9dbac282015-03-25 12:35:10 -0700969 bcmgenet_intrl2_0_writel(ring->priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700970 UMAC_IRQ_TXDMA_BDONE | UMAC_IRQ_TXDMA_PDONE,
971 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800972}
973
Petri Gynther9dbac282015-03-25 12:35:10 -0700974static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800975{
Petri Gynther9dbac282015-03-25 12:35:10 -0700976 bcmgenet_intrl2_0_writel(ring->priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700977 UMAC_IRQ_TXDMA_BDONE | UMAC_IRQ_TXDMA_PDONE,
978 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800979}
980
Petri Gynther9dbac282015-03-25 12:35:10 -0700981static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800982{
Petri Gynther9dbac282015-03-25 12:35:10 -0700983 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700984 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800985}
986
Petri Gynther9dbac282015-03-25 12:35:10 -0700987static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800988{
Petri Gynther9dbac282015-03-25 12:35:10 -0700989 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700990 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800991}
992
993/* Unlocked version of the reclaim routine */
Jaedon Shin4092e6a2015-02-28 11:48:26 +0900994static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
995 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800996{
997 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800998 struct enet_cb *tx_cb_ptr;
Florian Fainellib2cde2c2014-03-20 10:53:23 -0700999 struct netdev_queue *txq;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001000 unsigned int pkts_compl = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001001 unsigned int c_index;
Petri Gynther66d06752015-03-04 14:30:01 -08001002 unsigned int txbds_ready;
1003 unsigned int txbds_processed = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001004
Brian Norris7fc527f2014-07-29 14:34:14 -07001005 /* Compute how many buffers are transmitted since last xmit call */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001006 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
Petri Gynther66d06752015-03-04 14:30:01 -08001007 c_index &= DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001008
Petri Gynther66d06752015-03-04 14:30:01 -08001009 if (likely(c_index >= ring->c_index))
1010 txbds_ready = c_index - ring->c_index;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001011 else
Petri Gynther66d06752015-03-04 14:30:01 -08001012 txbds_ready = (DMA_C_INDEX_MASK + 1) - ring->c_index + c_index;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001013
1014 netif_dbg(priv, tx_done, dev,
Petri Gynther66d06752015-03-04 14:30:01 -08001015 "%s ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
1016 __func__, ring->index, ring->c_index, c_index, txbds_ready);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001017
1018 /* Reclaim transmitted buffers */
Petri Gynther66d06752015-03-04 14:30:01 -08001019 while (txbds_processed < txbds_ready) {
1020 tx_cb_ptr = &priv->tx_cbs[ring->clean_ptr];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001021 if (tx_cb_ptr->skb) {
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001022 pkts_compl++;
Petri Gynther66d06752015-03-04 14:30:01 -08001023 dev->stats.tx_packets++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001024 dev->stats.tx_bytes += tx_cb_ptr->skb->len;
1025 dma_unmap_single(&dev->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001026 dma_unmap_addr(tx_cb_ptr, dma_addr),
1027 tx_cb_ptr->skb->len,
1028 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001029 bcmgenet_free_cb(tx_cb_ptr);
1030 } else if (dma_unmap_addr(tx_cb_ptr, dma_addr)) {
1031 dev->stats.tx_bytes +=
1032 dma_unmap_len(tx_cb_ptr, dma_len);
1033 dma_unmap_page(&dev->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001034 dma_unmap_addr(tx_cb_ptr, dma_addr),
1035 dma_unmap_len(tx_cb_ptr, dma_len),
1036 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001037 dma_unmap_addr_set(tx_cb_ptr, dma_addr, 0);
1038 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001039
Petri Gynther66d06752015-03-04 14:30:01 -08001040 txbds_processed++;
1041 if (likely(ring->clean_ptr < ring->end_ptr))
1042 ring->clean_ptr++;
1043 else
1044 ring->clean_ptr = ring->cb_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001045 }
1046
Petri Gynther66d06752015-03-04 14:30:01 -08001047 ring->free_bds += txbds_processed;
1048 ring->c_index = (ring->c_index + txbds_processed) & DMA_C_INDEX_MASK;
1049
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001050 if (ring->free_bds > (MAX_SKB_FRAGS + 1)) {
Petri Gynther66d06752015-03-04 14:30:01 -08001051 txq = netdev_get_tx_queue(dev, ring->queue);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001052 if (netif_tx_queue_stopped(txq))
1053 netif_tx_wake_queue(txq);
1054 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001055
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001056 return pkts_compl;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001057}
1058
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001059static unsigned int bcmgenet_tx_reclaim(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001060 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001061{
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001062 unsigned int released;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001063 unsigned long flags;
1064
1065 spin_lock_irqsave(&ring->lock, flags);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001066 released = __bcmgenet_tx_reclaim(dev, ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001067 spin_unlock_irqrestore(&ring->lock, flags);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001068
1069 return released;
1070}
1071
1072static int bcmgenet_tx_poll(struct napi_struct *napi, int budget)
1073{
1074 struct bcmgenet_tx_ring *ring =
1075 container_of(napi, struct bcmgenet_tx_ring, napi);
1076 unsigned int work_done = 0;
1077
1078 work_done = bcmgenet_tx_reclaim(ring->priv->dev, ring);
1079
1080 if (work_done == 0) {
1081 napi_complete(napi);
Petri Gynther9dbac282015-03-25 12:35:10 -07001082 ring->int_enable(ring);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001083
1084 return 0;
1085 }
1086
1087 return budget;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001088}
1089
1090static void bcmgenet_tx_reclaim_all(struct net_device *dev)
1091{
1092 struct bcmgenet_priv *priv = netdev_priv(dev);
1093 int i;
1094
1095 if (netif_is_multiqueue(dev)) {
1096 for (i = 0; i < priv->hw_params->tx_queues; i++)
1097 bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
1098 }
1099
1100 bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
1101}
1102
1103/* Transmits a single SKB (either head of a fragment or a single SKB)
1104 * caller must hold priv->lock
1105 */
1106static int bcmgenet_xmit_single(struct net_device *dev,
1107 struct sk_buff *skb,
1108 u16 dma_desc_flags,
1109 struct bcmgenet_tx_ring *ring)
1110{
1111 struct bcmgenet_priv *priv = netdev_priv(dev);
1112 struct device *kdev = &priv->pdev->dev;
1113 struct enet_cb *tx_cb_ptr;
1114 unsigned int skb_len;
1115 dma_addr_t mapping;
1116 u32 length_status;
1117 int ret;
1118
1119 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
1120
1121 if (unlikely(!tx_cb_ptr))
1122 BUG();
1123
1124 tx_cb_ptr->skb = skb;
1125
1126 skb_len = skb_headlen(skb) < ETH_ZLEN ? ETH_ZLEN : skb_headlen(skb);
1127
1128 mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
1129 ret = dma_mapping_error(kdev, mapping);
1130 if (ret) {
Florian Fainelli44c8bc32014-11-19 10:29:56 -08001131 priv->mib.tx_dma_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001132 netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
1133 dev_kfree_skb(skb);
1134 return ret;
1135 }
1136
1137 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
1138 dma_unmap_len_set(tx_cb_ptr, dma_len, skb->len);
1139 length_status = (skb_len << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
1140 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT) |
1141 DMA_TX_APPEND_CRC;
1142
1143 if (skb->ip_summed == CHECKSUM_PARTIAL)
1144 length_status |= DMA_TX_DO_CSUM;
1145
1146 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, length_status);
1147
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001148 return 0;
1149}
1150
Brian Norris7fc527f2014-07-29 14:34:14 -07001151/* Transmit a SKB fragment */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001152static int bcmgenet_xmit_frag(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001153 skb_frag_t *frag,
1154 u16 dma_desc_flags,
1155 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001156{
1157 struct bcmgenet_priv *priv = netdev_priv(dev);
1158 struct device *kdev = &priv->pdev->dev;
1159 struct enet_cb *tx_cb_ptr;
1160 dma_addr_t mapping;
1161 int ret;
1162
1163 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
1164
1165 if (unlikely(!tx_cb_ptr))
1166 BUG();
1167 tx_cb_ptr->skb = NULL;
1168
1169 mapping = skb_frag_dma_map(kdev, frag, 0,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001170 skb_frag_size(frag), DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001171 ret = dma_mapping_error(kdev, mapping);
1172 if (ret) {
Florian Fainelli44c8bc32014-11-19 10:29:56 -08001173 priv->mib.tx_dma_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001174 netif_err(priv, tx_err, dev, "%s: Tx DMA map failed\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001175 __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001176 return ret;
1177 }
1178
1179 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
1180 dma_unmap_len_set(tx_cb_ptr, dma_len, frag->size);
1181
1182 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001183 (frag->size << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
1184 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001185
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001186 return 0;
1187}
1188
1189/* Reallocate the SKB to put enough headroom in front of it and insert
1190 * the transmit checksum offsets in the descriptors
1191 */
Petri Gyntherbc233332014-10-01 11:30:01 -07001192static struct sk_buff *bcmgenet_put_tx_csum(struct net_device *dev,
1193 struct sk_buff *skb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001194{
1195 struct status_64 *status = NULL;
1196 struct sk_buff *new_skb;
1197 u16 offset;
1198 u8 ip_proto;
1199 u16 ip_ver;
1200 u32 tx_csum_info;
1201
1202 if (unlikely(skb_headroom(skb) < sizeof(*status))) {
1203 /* If 64 byte status block enabled, must make sure skb has
1204 * enough headroom for us to insert 64B status block.
1205 */
1206 new_skb = skb_realloc_headroom(skb, sizeof(*status));
1207 dev_kfree_skb(skb);
1208 if (!new_skb) {
1209 dev->stats.tx_errors++;
1210 dev->stats.tx_dropped++;
Petri Gyntherbc233332014-10-01 11:30:01 -07001211 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001212 }
1213 skb = new_skb;
1214 }
1215
1216 skb_push(skb, sizeof(*status));
1217 status = (struct status_64 *)skb->data;
1218
1219 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1220 ip_ver = htons(skb->protocol);
1221 switch (ip_ver) {
1222 case ETH_P_IP:
1223 ip_proto = ip_hdr(skb)->protocol;
1224 break;
1225 case ETH_P_IPV6:
1226 ip_proto = ipv6_hdr(skb)->nexthdr;
1227 break;
1228 default:
Petri Gyntherbc233332014-10-01 11:30:01 -07001229 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001230 }
1231
1232 offset = skb_checksum_start_offset(skb) - sizeof(*status);
1233 tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
1234 (offset + skb->csum_offset);
1235
1236 /* Set the length valid bit for TCP and UDP and just set
1237 * the special UDP flag for IPv4, else just set to 0.
1238 */
1239 if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
1240 tx_csum_info |= STATUS_TX_CSUM_LV;
1241 if (ip_proto == IPPROTO_UDP && ip_ver == ETH_P_IP)
1242 tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
Florian Fainelli8900ea572014-07-23 10:42:14 -07001243 } else {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001244 tx_csum_info = 0;
Florian Fainelli8900ea572014-07-23 10:42:14 -07001245 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001246
1247 status->tx_csum_info = tx_csum_info;
1248 }
1249
Petri Gyntherbc233332014-10-01 11:30:01 -07001250 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001251}
1252
1253static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
1254{
1255 struct bcmgenet_priv *priv = netdev_priv(dev);
1256 struct bcmgenet_tx_ring *ring = NULL;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001257 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001258 unsigned long flags = 0;
1259 int nr_frags, index;
1260 u16 dma_desc_flags;
1261 int ret;
1262 int i;
1263
1264 index = skb_get_queue_mapping(skb);
1265 /* Mapping strategy:
1266 * queue_mapping = 0, unclassified, packet xmited through ring16
1267 * queue_mapping = 1, goes to ring 0. (highest priority queue
1268 * queue_mapping = 2, goes to ring 1.
1269 * queue_mapping = 3, goes to ring 2.
1270 * queue_mapping = 4, goes to ring 3.
1271 */
1272 if (index == 0)
1273 index = DESC_INDEX;
1274 else
1275 index -= 1;
1276
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001277 nr_frags = skb_shinfo(skb)->nr_frags;
1278 ring = &priv->tx_rings[index];
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001279 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001280
1281 spin_lock_irqsave(&ring->lock, flags);
1282 if (ring->free_bds <= nr_frags + 1) {
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001283 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001284 netdev_err(dev, "%s: tx ring %d full when queue %d awake\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001285 __func__, index, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001286 ret = NETDEV_TX_BUSY;
1287 goto out;
1288 }
1289
Florian Fainelli474ea9c2014-07-22 11:01:52 -07001290 if (skb_padto(skb, ETH_ZLEN)) {
1291 ret = NETDEV_TX_OK;
1292 goto out;
1293 }
1294
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001295 /* set the SKB transmit checksum */
1296 if (priv->desc_64b_en) {
Petri Gyntherbc233332014-10-01 11:30:01 -07001297 skb = bcmgenet_put_tx_csum(dev, skb);
1298 if (!skb) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001299 ret = NETDEV_TX_OK;
1300 goto out;
1301 }
1302 }
1303
1304 dma_desc_flags = DMA_SOP;
1305 if (nr_frags == 0)
1306 dma_desc_flags |= DMA_EOP;
1307
1308 /* Transmit single SKB or head of fragment list */
1309 ret = bcmgenet_xmit_single(dev, skb, dma_desc_flags, ring);
1310 if (ret) {
1311 ret = NETDEV_TX_OK;
1312 goto out;
1313 }
1314
1315 /* xmit fragment */
1316 for (i = 0; i < nr_frags; i++) {
1317 ret = bcmgenet_xmit_frag(dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001318 &skb_shinfo(skb)->frags[i],
1319 (i == nr_frags - 1) ? DMA_EOP : 0,
1320 ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001321 if (ret) {
1322 ret = NETDEV_TX_OK;
1323 goto out;
1324 }
1325 }
1326
Florian Fainellid03825f2014-03-20 10:53:21 -07001327 skb_tx_timestamp(skb);
1328
Florian Fainelliae67bf02015-03-13 12:11:06 -07001329 /* Decrement total BD count and advance our write pointer */
1330 ring->free_bds -= nr_frags + 1;
1331 ring->prod_index += nr_frags + 1;
1332 ring->prod_index &= DMA_P_INDEX_MASK;
1333
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001334 if (ring->free_bds <= (MAX_SKB_FRAGS + 1))
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001335 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001336
Florian Fainelliddd0ca52015-03-13 12:11:07 -07001337 if (!skb->xmit_more || netif_xmit_stopped(txq))
1338 /* Packets are ready, update producer index */
1339 bcmgenet_tdma_ring_writel(priv, ring->index,
1340 ring->prod_index, TDMA_PROD_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001341out:
1342 spin_unlock_irqrestore(&ring->lock, flags);
1343
1344 return ret;
1345}
1346
Petri Gyntherd6707be2015-03-12 15:48:00 -07001347static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
1348 struct enet_cb *cb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001349{
1350 struct device *kdev = &priv->pdev->dev;
1351 struct sk_buff *skb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001352 struct sk_buff *rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001353 dma_addr_t mapping;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001354
Petri Gyntherd6707be2015-03-12 15:48:00 -07001355 /* Allocate a new Rx skb */
Florian Fainellic91b7f62014-07-23 10:42:12 -07001356 skb = netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT);
Petri Gyntherd6707be2015-03-12 15:48:00 -07001357 if (!skb) {
1358 priv->mib.alloc_rx_buff_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001359 netif_err(priv, rx_err, priv->dev,
Petri Gyntherd6707be2015-03-12 15:48:00 -07001360 "%s: Rx skb allocation failed\n", __func__);
1361 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001362 }
1363
Petri Gyntherd6707be2015-03-12 15:48:00 -07001364 /* DMA-map the new Rx skb */
1365 mapping = dma_map_single(kdev, skb->data, priv->rx_buf_len,
1366 DMA_FROM_DEVICE);
1367 if (dma_mapping_error(kdev, mapping)) {
1368 priv->mib.rx_dma_failed++;
1369 dev_kfree_skb_any(skb);
1370 netif_err(priv, rx_err, priv->dev,
1371 "%s: Rx skb DMA mapping failed\n", __func__);
1372 return NULL;
1373 }
1374
1375 /* Grab the current Rx skb from the ring and DMA-unmap it */
1376 rx_skb = cb->skb;
1377 if (likely(rx_skb))
1378 dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
1379 priv->rx_buf_len, DMA_FROM_DEVICE);
1380
1381 /* Put the new Rx skb on the ring */
1382 cb->skb = skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001383 dma_unmap_addr_set(cb, dma_addr, mapping);
Petri Gynther8ac467e2015-03-09 13:40:00 -07001384 dmadesc_set_addr(priv, cb->bd_addr, mapping);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001385
Petri Gyntherd6707be2015-03-12 15:48:00 -07001386 /* Return the current Rx skb to caller */
1387 return rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001388}
1389
1390/* bcmgenet_desc_rx - descriptor based rx process.
1391 * this could be called from bottom half, or from NAPI polling method.
1392 */
1393static unsigned int bcmgenet_desc_rx(struct bcmgenet_priv *priv,
Petri Gynther8ac467e2015-03-09 13:40:00 -07001394 unsigned int index,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001395 unsigned int budget)
1396{
Petri Gynther8ac467e2015-03-09 13:40:00 -07001397 struct bcmgenet_rx_ring *ring = &priv->rx_rings[index];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001398 struct net_device *dev = priv->dev;
1399 struct enet_cb *cb;
1400 struct sk_buff *skb;
1401 u32 dma_length_status;
1402 unsigned long dma_flag;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001403 int len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001404 unsigned int rxpktprocessed = 0, rxpkttoprocess;
1405 unsigned int p_index;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001406 unsigned int discards;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001407 unsigned int chksum_ok = 0;
1408
Petri Gynther8ac467e2015-03-09 13:40:00 -07001409 p_index = bcmgenet_rdma_ring_readl(priv, index, RDMA_PROD_INDEX);
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001410
1411 discards = (p_index >> DMA_P_INDEX_DISCARD_CNT_SHIFT) &
1412 DMA_P_INDEX_DISCARD_CNT_MASK;
1413 if (discards > ring->old_discards) {
1414 discards = discards - ring->old_discards;
1415 dev->stats.rx_missed_errors += discards;
1416 dev->stats.rx_errors += discards;
1417 ring->old_discards += discards;
1418
1419 /* Clear HW register when we reach 75% of maximum 0xFFFF */
1420 if (ring->old_discards >= 0xC000) {
1421 ring->old_discards = 0;
1422 bcmgenet_rdma_ring_writel(priv, index, 0,
1423 RDMA_PROD_INDEX);
1424 }
1425 }
1426
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001427 p_index &= DMA_P_INDEX_MASK;
1428
Petri Gynther8ac467e2015-03-09 13:40:00 -07001429 if (likely(p_index >= ring->c_index))
1430 rxpkttoprocess = p_index - ring->c_index;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001431 else
Petri Gynther8ac467e2015-03-09 13:40:00 -07001432 rxpkttoprocess = (DMA_C_INDEX_MASK + 1) - ring->c_index +
1433 p_index;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001434
1435 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001436 "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001437
1438 while ((rxpktprocessed < rxpkttoprocess) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001439 (rxpktprocessed < budget)) {
Petri Gynther8ac467e2015-03-09 13:40:00 -07001440 cb = &priv->rx_cbs[ring->read_ptr];
Petri Gyntherd6707be2015-03-12 15:48:00 -07001441 skb = bcmgenet_rx_refill(priv, cb);
Florian Fainellib629be52014-09-08 11:37:52 -07001442
Florian Fainellib629be52014-09-08 11:37:52 -07001443 if (unlikely(!skb)) {
1444 dev->stats.rx_dropped++;
1445 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001446 goto next;
Florian Fainellib629be52014-09-08 11:37:52 -07001447 }
1448
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001449 if (!priv->desc_64b_en) {
Florian Fainellic91b7f62014-07-23 10:42:12 -07001450 dma_length_status =
Petri Gynther8ac467e2015-03-09 13:40:00 -07001451 dmadesc_get_length_status(priv, cb->bd_addr);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001452 } else {
1453 struct status_64 *status;
Florian Fainelli164d4f22014-07-23 10:42:13 -07001454
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001455 status = (struct status_64 *)skb->data;
1456 dma_length_status = status->length_status;
1457 }
1458
1459 /* DMA flags and length are still valid no matter how
1460 * we got the Receive Status Vector (64B RSB or register)
1461 */
1462 dma_flag = dma_length_status & 0xffff;
1463 len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
1464
1465 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001466 "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
Petri Gynther8ac467e2015-03-09 13:40:00 -07001467 __func__, p_index, ring->c_index,
1468 ring->read_ptr, dma_length_status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001469
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001470 if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
1471 netif_err(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001472 "dropping fragmented packet!\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001473 dev->stats.rx_dropped++;
1474 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001475 dev_kfree_skb_any(skb);
1476 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001477 }
Petri Gyntherd6707be2015-03-12 15:48:00 -07001478
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001479 /* report errors */
1480 if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
1481 DMA_RX_OV |
1482 DMA_RX_NO |
1483 DMA_RX_LG |
1484 DMA_RX_RXER))) {
1485 netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001486 (unsigned int)dma_flag);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001487 if (dma_flag & DMA_RX_CRC_ERROR)
1488 dev->stats.rx_crc_errors++;
1489 if (dma_flag & DMA_RX_OV)
1490 dev->stats.rx_over_errors++;
1491 if (dma_flag & DMA_RX_NO)
1492 dev->stats.rx_frame_errors++;
1493 if (dma_flag & DMA_RX_LG)
1494 dev->stats.rx_length_errors++;
1495 dev->stats.rx_dropped++;
1496 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001497 dev_kfree_skb_any(skb);
1498 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001499 } /* error packet */
1500
1501 chksum_ok = (dma_flag & priv->dma_rx_chk_bit) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001502 priv->desc_rxchk_en;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001503
1504 skb_put(skb, len);
1505 if (priv->desc_64b_en) {
1506 skb_pull(skb, 64);
1507 len -= 64;
1508 }
1509
1510 if (likely(chksum_ok))
1511 skb->ip_summed = CHECKSUM_UNNECESSARY;
1512
1513 /* remove hardware 2bytes added for IP alignment */
1514 skb_pull(skb, 2);
1515 len -= 2;
1516
1517 if (priv->crc_fwd_en) {
1518 skb_trim(skb, len - ETH_FCS_LEN);
1519 len -= ETH_FCS_LEN;
1520 }
1521
1522 /*Finish setting up the received SKB and send it to the kernel*/
1523 skb->protocol = eth_type_trans(skb, priv->dev);
1524 dev->stats.rx_packets++;
1525 dev->stats.rx_bytes += len;
1526 if (dma_flag & DMA_RX_MULT)
1527 dev->stats.multicast++;
1528
1529 /* Notify kernel */
1530 napi_gro_receive(&priv->napi, skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001531 netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
1532
Petri Gyntherd6707be2015-03-12 15:48:00 -07001533next:
Florian Fainellicf377d82014-10-10 10:51:52 -07001534 rxpktprocessed++;
Petri Gynther8ac467e2015-03-09 13:40:00 -07001535 if (likely(ring->read_ptr < ring->end_ptr))
1536 ring->read_ptr++;
1537 else
1538 ring->read_ptr = ring->cb_ptr;
1539
1540 ring->c_index = (ring->c_index + 1) & DMA_C_INDEX_MASK;
1541 bcmgenet_rdma_ring_writel(priv, index, ring->c_index, RDMA_CONS_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001542 }
1543
1544 return rxpktprocessed;
1545}
1546
Petri Gynther3ab11332015-03-25 12:35:15 -07001547/* Rx NAPI polling method */
1548static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
1549{
1550 struct bcmgenet_priv *priv = container_of(napi,
1551 struct bcmgenet_priv, napi);
1552 unsigned int work_done;
1553
1554 work_done = bcmgenet_desc_rx(priv, DESC_INDEX, budget);
1555
1556 if (work_done < budget) {
1557 napi_complete(napi);
1558 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_RXDMA_BDONE |
1559 UMAC_IRQ_RXDMA_PDONE,
1560 INTRL2_CPU_MASK_CLEAR);
1561 }
1562
1563 return work_done;
1564}
1565
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001566/* Assign skb to RX DMA descriptor. */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001567static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv,
1568 struct bcmgenet_rx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001569{
1570 struct enet_cb *cb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001571 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001572 int i;
1573
Petri Gynther8ac467e2015-03-09 13:40:00 -07001574 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001575
1576 /* loop here for each buffer needing assign */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001577 for (i = 0; i < ring->size; i++) {
1578 cb = ring->cbs + i;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001579 skb = bcmgenet_rx_refill(priv, cb);
1580 if (skb)
1581 dev_kfree_skb_any(skb);
1582 if (!cb->skb)
1583 return -ENOMEM;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001584 }
1585
Petri Gyntherd6707be2015-03-12 15:48:00 -07001586 return 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001587}
1588
1589static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
1590{
1591 struct enet_cb *cb;
1592 int i;
1593
1594 for (i = 0; i < priv->num_rx_bds; i++) {
1595 cb = &priv->rx_cbs[i];
1596
1597 if (dma_unmap_addr(cb, dma_addr)) {
1598 dma_unmap_single(&priv->dev->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001599 dma_unmap_addr(cb, dma_addr),
1600 priv->rx_buf_len, DMA_FROM_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001601 dma_unmap_addr_set(cb, dma_addr, 0);
1602 }
1603
1604 if (cb->skb)
1605 bcmgenet_free_cb(cb);
1606 }
1607}
1608
Florian Fainellic91b7f62014-07-23 10:42:12 -07001609static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable)
Florian Fainellie29585b2014-07-21 15:29:20 -07001610{
1611 u32 reg;
1612
1613 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1614 if (enable)
1615 reg |= mask;
1616 else
1617 reg &= ~mask;
1618 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
1619
1620 /* UniMAC stops on a packet boundary, wait for a full-size packet
1621 * to be processed
1622 */
1623 if (enable == 0)
1624 usleep_range(1000, 2000);
1625}
1626
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001627static int reset_umac(struct bcmgenet_priv *priv)
1628{
1629 struct device *kdev = &priv->pdev->dev;
1630 unsigned int timeout = 0;
1631 u32 reg;
1632
1633 /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
1634 bcmgenet_rbuf_ctrl_set(priv, 0);
1635 udelay(10);
1636
1637 /* disable MAC while updating its registers */
1638 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1639
1640 /* issue soft reset, wait for it to complete */
1641 bcmgenet_umac_writel(priv, CMD_SW_RESET, UMAC_CMD);
1642 while (timeout++ < 1000) {
1643 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1644 if (!(reg & CMD_SW_RESET))
1645 return 0;
1646
1647 udelay(1);
1648 }
1649
1650 if (timeout == 1000) {
1651 dev_err(kdev,
Brian Norris7fc527f2014-07-29 14:34:14 -07001652 "timeout waiting for MAC to come out of reset\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001653 return -ETIMEDOUT;
1654 }
1655
1656 return 0;
1657}
1658
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001659static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
1660{
1661 /* Mask all interrupts.*/
1662 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1663 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
1664 bcmgenet_intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1665 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1666 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
1667 bcmgenet_intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1668}
1669
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001670static int init_umac(struct bcmgenet_priv *priv)
1671{
1672 struct device *kdev = &priv->pdev->dev;
1673 int ret;
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001674 u32 reg;
1675 u32 int0_enable = 0;
1676 u32 int1_enable = 0;
1677 int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001678
1679 dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
1680
1681 ret = reset_umac(priv);
1682 if (ret)
1683 return ret;
1684
1685 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1686 /* clear tx/rx counter */
1687 bcmgenet_umac_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001688 MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
1689 UMAC_MIB_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001690 bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
1691
1692 bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
1693
1694 /* init rx registers, enable ip header optimization */
1695 reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
1696 reg |= RBUF_ALIGN_2B;
1697 bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
1698
1699 if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
1700 bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
1701
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001702 bcmgenet_intr_disable(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001703
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001704 /* Enable Rx default queue 16 interrupts */
1705 int0_enable |= (UMAC_IRQ_RXDMA_BDONE | UMAC_IRQ_RXDMA_PDONE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001706
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001707 /* Enable Tx default queue 16 interrupts */
1708 int0_enable |= (UMAC_IRQ_TXDMA_BDONE | UMAC_IRQ_TXDMA_PDONE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001709
Brian Norris7fc527f2014-07-29 14:34:14 -07001710 /* Monitor cable plug/unplugged event for internal PHY */
Florian Fainelli8900ea572014-07-23 10:42:14 -07001711 if (phy_is_internal(priv->phydev)) {
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001712 int0_enable |= (UMAC_IRQ_LINK_DOWN | UMAC_IRQ_LINK_UP);
Florian Fainelli8900ea572014-07-23 10:42:14 -07001713 } else if (priv->ext_phy) {
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001714 int0_enable |= (UMAC_IRQ_LINK_DOWN | UMAC_IRQ_LINK_UP);
Florian Fainelli8900ea572014-07-23 10:42:14 -07001715 } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001716 reg = bcmgenet_bp_mc_get(priv);
1717 reg |= BIT(priv->hw_params->bp_in_en_shift);
1718
1719 /* bp_mask: back pressure mask */
1720 if (netif_is_multiqueue(priv->dev))
1721 reg |= priv->hw_params->bp_in_mask;
1722 else
1723 reg &= ~priv->hw_params->bp_in_mask;
1724 bcmgenet_bp_mc_set(priv, reg);
1725 }
1726
1727 /* Enable MDIO interrupts on GENET v3+ */
1728 if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001729 int0_enable |= (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001730
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001731 /* Enable Tx priority queue interrupts */
1732 for (i = 0; i < priv->hw_params->tx_queues; ++i)
1733 int1_enable |= (1 << i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001734
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001735 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
1736 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001737
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001738 /* Enable rx/tx engine.*/
1739 dev_dbg(kdev, "done init umac\n");
1740
1741 return 0;
1742}
1743
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001744/* Initialize a Tx ring along with corresponding hardware registers */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001745static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
1746 unsigned int index, unsigned int size,
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001747 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001748{
1749 struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
1750 u32 words_per_bd = WORDS_PER_BD(priv);
1751 u32 flow_period_val = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001752
1753 spin_lock_init(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001754 ring->priv = priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001755 ring->index = index;
1756 if (index == DESC_INDEX) {
1757 ring->queue = 0;
1758 ring->int_enable = bcmgenet_tx_ring16_int_enable;
1759 ring->int_disable = bcmgenet_tx_ring16_int_disable;
1760 } else {
1761 ring->queue = index + 1;
1762 ring->int_enable = bcmgenet_tx_ring_int_enable;
1763 ring->int_disable = bcmgenet_tx_ring_int_disable;
1764 }
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001765 ring->cbs = priv->tx_cbs + start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001766 ring->size = size;
Petri Gynther66d06752015-03-04 14:30:01 -08001767 ring->clean_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001768 ring->c_index = 0;
1769 ring->free_bds = size;
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001770 ring->write_ptr = start_ptr;
1771 ring->cb_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001772 ring->end_ptr = end_ptr - 1;
1773 ring->prod_index = 0;
1774
1775 /* Set flow period for ring != 16 */
1776 if (index != DESC_INDEX)
1777 flow_period_val = ENET_MAX_MTU_SIZE << 16;
1778
1779 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
1780 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
1781 bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
1782 /* Disable rate control for now */
1783 bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001784 TDMA_FLOW_PERIOD);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001785 bcmgenet_tdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001786 ((size << DMA_RING_SIZE_SHIFT) |
1787 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001788
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001789 /* Set start and end address, read and write pointers */
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001790 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001791 DMA_START_ADDR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001792 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001793 TDMA_READ_PTR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001794 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001795 TDMA_WRITE_PTR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001796 bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001797 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001798}
1799
1800/* Initialize a RDMA ring */
1801static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
Petri Gynther8ac467e2015-03-09 13:40:00 -07001802 unsigned int index, unsigned int size,
1803 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001804{
Petri Gynther8ac467e2015-03-09 13:40:00 -07001805 struct bcmgenet_rx_ring *ring = &priv->rx_rings[index];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001806 u32 words_per_bd = WORDS_PER_BD(priv);
1807 int ret;
1808
Petri Gynther8ac467e2015-03-09 13:40:00 -07001809 ring->index = index;
1810 ring->cbs = priv->rx_cbs + start_ptr;
1811 ring->size = size;
1812 ring->c_index = 0;
1813 ring->read_ptr = start_ptr;
1814 ring->cb_ptr = start_ptr;
1815 ring->end_ptr = end_ptr - 1;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001816
Petri Gynther8ac467e2015-03-09 13:40:00 -07001817 ret = bcmgenet_alloc_rx_buffers(priv, ring);
1818 if (ret)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001819 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001820
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001821 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
1822 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
Petri Gynther6f5a2722015-03-06 13:45:00 -08001823 bcmgenet_rdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001824 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001825 ((size << DMA_RING_SIZE_SHIFT) |
1826 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001827 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001828 (DMA_FC_THRESH_LO <<
1829 DMA_XOFF_THRESHOLD_SHIFT) |
1830 DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
Petri Gynther6f5a2722015-03-06 13:45:00 -08001831
1832 /* Set start and end address, read and write pointers */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001833 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
1834 DMA_START_ADDR);
1835 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
1836 RDMA_READ_PTR);
1837 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
1838 RDMA_WRITE_PTR);
1839 bcmgenet_rdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Petri Gynther6f5a2722015-03-06 13:45:00 -08001840 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001841
1842 return ret;
1843}
1844
Petri Gynthere2aadb42015-03-25 12:35:14 -07001845static void bcmgenet_init_tx_napi(struct bcmgenet_priv *priv)
1846{
1847 unsigned int i;
1848 struct bcmgenet_tx_ring *ring;
1849
1850 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
1851 ring = &priv->tx_rings[i];
1852 netif_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll, 64);
1853 }
1854
1855 ring = &priv->tx_rings[DESC_INDEX];
1856 netif_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll, 64);
1857}
1858
1859static void bcmgenet_enable_tx_napi(struct bcmgenet_priv *priv)
1860{
1861 unsigned int i;
1862 struct bcmgenet_tx_ring *ring;
1863
1864 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
1865 ring = &priv->tx_rings[i];
1866 napi_enable(&ring->napi);
1867 }
1868
1869 ring = &priv->tx_rings[DESC_INDEX];
1870 napi_enable(&ring->napi);
1871}
1872
1873static void bcmgenet_disable_tx_napi(struct bcmgenet_priv *priv)
1874{
1875 unsigned int i;
1876 struct bcmgenet_tx_ring *ring;
1877
1878 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
1879 ring = &priv->tx_rings[i];
1880 napi_disable(&ring->napi);
1881 }
1882
1883 ring = &priv->tx_rings[DESC_INDEX];
1884 napi_disable(&ring->napi);
1885}
1886
1887static void bcmgenet_fini_tx_napi(struct bcmgenet_priv *priv)
1888{
1889 unsigned int i;
1890 struct bcmgenet_tx_ring *ring;
1891
1892 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
1893 ring = &priv->tx_rings[i];
1894 netif_napi_del(&ring->napi);
1895 }
1896
1897 ring = &priv->tx_rings[DESC_INDEX];
1898 netif_napi_del(&ring->napi);
1899}
1900
Petri Gynther16c6d662015-02-23 11:00:45 -08001901/* Initialize Tx queues
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001902 *
Petri Gynther16c6d662015-02-23 11:00:45 -08001903 * Queues 0-3 are priority-based, each one has 32 descriptors,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001904 * with queue 0 being the highest priority queue.
1905 *
Petri Gynther16c6d662015-02-23 11:00:45 -08001906 * Queue 16 is the default Tx queue with
Petri Gynther51a966a2015-02-23 11:00:46 -08001907 * GENET_Q16_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors.
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001908 *
Petri Gynther16c6d662015-02-23 11:00:45 -08001909 * The transmit control block pool is then partitioned as follows:
1910 * - Tx queue 0 uses tx_cbs[0..31]
1911 * - Tx queue 1 uses tx_cbs[32..63]
1912 * - Tx queue 2 uses tx_cbs[64..95]
1913 * - Tx queue 3 uses tx_cbs[96..127]
1914 * - Tx queue 16 uses tx_cbs[128..255]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001915 */
Petri Gynther16c6d662015-02-23 11:00:45 -08001916static void bcmgenet_init_tx_queues(struct net_device *dev)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001917{
1918 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther16c6d662015-02-23 11:00:45 -08001919 u32 i, dma_enable;
1920 u32 dma_ctrl, ring_cfg;
Petri Gynther37742162014-10-07 09:30:01 -07001921 u32 dma_priority[3] = {0, 0, 0};
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001922
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001923 dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
1924 dma_enable = dma_ctrl & DMA_EN;
1925 dma_ctrl &= ~DMA_EN;
1926 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
1927
Petri Gynther16c6d662015-02-23 11:00:45 -08001928 dma_ctrl = 0;
1929 ring_cfg = 0;
1930
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001931 /* Enable strict priority arbiter mode */
1932 bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
1933
Petri Gynther16c6d662015-02-23 11:00:45 -08001934 /* Initialize Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001935 for (i = 0; i < priv->hw_params->tx_queues; i++) {
Petri Gynther51a966a2015-02-23 11:00:46 -08001936 bcmgenet_init_tx_ring(priv, i, priv->hw_params->tx_bds_per_q,
1937 i * priv->hw_params->tx_bds_per_q,
1938 (i + 1) * priv->hw_params->tx_bds_per_q);
Petri Gynther16c6d662015-02-23 11:00:45 -08001939 ring_cfg |= (1 << i);
1940 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07001941 dma_priority[DMA_PRIO_REG_INDEX(i)] |=
1942 ((GENET_Q0_PRIORITY + i) << DMA_PRIO_REG_SHIFT(i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001943 }
1944
Petri Gynther16c6d662015-02-23 11:00:45 -08001945 /* Initialize Tx default queue 16 */
Petri Gynther51a966a2015-02-23 11:00:46 -08001946 bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_Q16_TX_BD_CNT,
Petri Gynther16c6d662015-02-23 11:00:45 -08001947 priv->hw_params->tx_queues *
Petri Gynther51a966a2015-02-23 11:00:46 -08001948 priv->hw_params->tx_bds_per_q,
Petri Gynther16c6d662015-02-23 11:00:45 -08001949 TOTAL_DESC);
1950 ring_cfg |= (1 << DESC_INDEX);
1951 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07001952 dma_priority[DMA_PRIO_REG_INDEX(DESC_INDEX)] |=
1953 ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) <<
1954 DMA_PRIO_REG_SHIFT(DESC_INDEX));
Petri Gynther16c6d662015-02-23 11:00:45 -08001955
1956 /* Set Tx queue priorities */
Petri Gynther37742162014-10-07 09:30:01 -07001957 bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
1958 bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
1959 bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);
1960
Petri Gynthere2aadb42015-03-25 12:35:14 -07001961 /* Initialize Tx NAPI */
1962 bcmgenet_init_tx_napi(priv);
1963
Petri Gynther16c6d662015-02-23 11:00:45 -08001964 /* Enable Tx queues */
1965 bcmgenet_tdma_writel(priv, ring_cfg, DMA_RING_CFG);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001966
Petri Gynther16c6d662015-02-23 11:00:45 -08001967 /* Enable Tx DMA */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001968 if (dma_enable)
Petri Gynther16c6d662015-02-23 11:00:45 -08001969 dma_ctrl |= DMA_EN;
1970 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001971}
1972
Petri Gynther3ab11332015-03-25 12:35:15 -07001973static void bcmgenet_init_rx_napi(struct bcmgenet_priv *priv)
1974{
1975 netif_napi_add(priv->dev, &priv->napi, bcmgenet_rx_poll, 64);
1976}
1977
1978static void bcmgenet_enable_rx_napi(struct bcmgenet_priv *priv)
1979{
1980 napi_enable(&priv->napi);
1981}
1982
1983static void bcmgenet_disable_rx_napi(struct bcmgenet_priv *priv)
1984{
1985 napi_disable(&priv->napi);
1986}
1987
1988static void bcmgenet_fini_rx_napi(struct bcmgenet_priv *priv)
1989{
1990 netif_napi_del(&priv->napi);
1991}
1992
Petri Gynther8ac467e2015-03-09 13:40:00 -07001993/* Initialize Rx queues
1994 *
1995 * Queues 0-15 are priority queues. Hardware Filtering Block (HFB) can be
1996 * used to direct traffic to these queues.
1997 *
1998 * Queue 16 is the default Rx queue with GENET_Q16_RX_BD_CNT descriptors.
1999 */
2000static int bcmgenet_init_rx_queues(struct net_device *dev)
2001{
2002 struct bcmgenet_priv *priv = netdev_priv(dev);
2003 u32 i;
2004 u32 dma_enable;
2005 u32 dma_ctrl;
2006 u32 ring_cfg;
2007 int ret;
2008
2009 dma_ctrl = bcmgenet_rdma_readl(priv, DMA_CTRL);
2010 dma_enable = dma_ctrl & DMA_EN;
2011 dma_ctrl &= ~DMA_EN;
2012 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2013
2014 dma_ctrl = 0;
2015 ring_cfg = 0;
2016
2017 /* Initialize Rx priority queues */
2018 for (i = 0; i < priv->hw_params->rx_queues; i++) {
2019 ret = bcmgenet_init_rx_ring(priv, i,
2020 priv->hw_params->rx_bds_per_q,
2021 i * priv->hw_params->rx_bds_per_q,
2022 (i + 1) *
2023 priv->hw_params->rx_bds_per_q);
2024 if (ret)
2025 return ret;
2026
2027 ring_cfg |= (1 << i);
2028 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2029 }
2030
2031 /* Initialize Rx default queue 16 */
2032 ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, GENET_Q16_RX_BD_CNT,
2033 priv->hw_params->rx_queues *
2034 priv->hw_params->rx_bds_per_q,
2035 TOTAL_DESC);
2036 if (ret)
2037 return ret;
2038
2039 ring_cfg |= (1 << DESC_INDEX);
2040 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
2041
Petri Gynther3ab11332015-03-25 12:35:15 -07002042 /* Initialize Rx NAPI */
2043 bcmgenet_init_rx_napi(priv);
2044
Petri Gynther8ac467e2015-03-09 13:40:00 -07002045 /* Enable rings */
2046 bcmgenet_rdma_writel(priv, ring_cfg, DMA_RING_CFG);
2047
2048 /* Configure ring as descriptor ring and re-enable DMA if enabled */
2049 if (dma_enable)
2050 dma_ctrl |= DMA_EN;
2051 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2052
2053 return 0;
2054}
2055
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002056static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
2057{
2058 int ret = 0;
2059 int timeout = 0;
2060 u32 reg;
2061
2062 /* Disable TDMA to stop add more frames in TX DMA */
2063 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2064 reg &= ~DMA_EN;
2065 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2066
2067 /* Check TDMA status register to confirm TDMA is disabled */
2068 while (timeout++ < DMA_TIMEOUT_VAL) {
2069 reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
2070 if (reg & DMA_DISABLED)
2071 break;
2072
2073 udelay(1);
2074 }
2075
2076 if (timeout == DMA_TIMEOUT_VAL) {
2077 netdev_warn(priv->dev, "Timed out while disabling TX DMA\n");
2078 ret = -ETIMEDOUT;
2079 }
2080
2081 /* Wait 10ms for packet drain in both tx and rx dma */
2082 usleep_range(10000, 20000);
2083
2084 /* Disable RDMA */
2085 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2086 reg &= ~DMA_EN;
2087 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2088
2089 timeout = 0;
2090 /* Check RDMA status register to confirm RDMA is disabled */
2091 while (timeout++ < DMA_TIMEOUT_VAL) {
2092 reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
2093 if (reg & DMA_DISABLED)
2094 break;
2095
2096 udelay(1);
2097 }
2098
2099 if (timeout == DMA_TIMEOUT_VAL) {
2100 netdev_warn(priv->dev, "Timed out while disabling RX DMA\n");
2101 ret = -ETIMEDOUT;
2102 }
2103
2104 return ret;
2105}
2106
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002107static void __bcmgenet_fini_dma(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002108{
2109 int i;
2110
2111 /* disable DMA */
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002112 bcmgenet_dma_teardown(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002113
2114 for (i = 0; i < priv->num_tx_bds; i++) {
2115 if (priv->tx_cbs[i].skb != NULL) {
2116 dev_kfree_skb(priv->tx_cbs[i].skb);
2117 priv->tx_cbs[i].skb = NULL;
2118 }
2119 }
2120
2121 bcmgenet_free_rx_buffers(priv);
2122 kfree(priv->rx_cbs);
2123 kfree(priv->tx_cbs);
2124}
2125
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002126static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
2127{
Petri Gynther3ab11332015-03-25 12:35:15 -07002128 bcmgenet_fini_rx_napi(priv);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002129 bcmgenet_fini_tx_napi(priv);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002130
2131 __bcmgenet_fini_dma(priv);
2132}
2133
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002134/* init_edma: Initialize DMA control register */
2135static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
2136{
2137 int ret;
Petri Gynther014012a2015-02-23 11:00:45 -08002138 unsigned int i;
2139 struct enet_cb *cb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002140
Petri Gynther6f5a2722015-03-06 13:45:00 -08002141 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002142
Petri Gynther6f5a2722015-03-06 13:45:00 -08002143 /* Initialize common Rx ring structures */
2144 priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
2145 priv->num_rx_bds = TOTAL_DESC;
2146 priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct enet_cb),
2147 GFP_KERNEL);
2148 if (!priv->rx_cbs)
2149 return -ENOMEM;
2150
2151 for (i = 0; i < priv->num_rx_bds; i++) {
2152 cb = priv->rx_cbs + i;
2153 cb->bd_addr = priv->rx_bds + i * DMA_DESC_SIZE;
2154 }
2155
Brian Norris7fc527f2014-07-29 14:34:14 -07002156 /* Initialize common TX ring structures */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002157 priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
2158 priv->num_tx_bds = TOTAL_DESC;
Florian Fainellic489be02014-07-23 10:42:15 -07002159 priv->tx_cbs = kcalloc(priv->num_tx_bds, sizeof(struct enet_cb),
Florian Fainellic91b7f62014-07-23 10:42:12 -07002160 GFP_KERNEL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002161 if (!priv->tx_cbs) {
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002162 kfree(priv->rx_cbs);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002163 return -ENOMEM;
2164 }
2165
Petri Gynther014012a2015-02-23 11:00:45 -08002166 for (i = 0; i < priv->num_tx_bds; i++) {
2167 cb = priv->tx_cbs + i;
2168 cb->bd_addr = priv->tx_bds + i * DMA_DESC_SIZE;
2169 }
2170
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002171 /* Init rDma */
2172 bcmgenet_rdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
2173
2174 /* Initialize Rx queues */
2175 ret = bcmgenet_init_rx_queues(priv->dev);
2176 if (ret) {
2177 netdev_err(priv->dev, "failed to initialize Rx queues\n");
2178 bcmgenet_free_rx_buffers(priv);
2179 kfree(priv->rx_cbs);
2180 kfree(priv->tx_cbs);
2181 return ret;
2182 }
2183
2184 /* Init tDma */
2185 bcmgenet_tdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
2186
Petri Gynther16c6d662015-02-23 11:00:45 -08002187 /* Initialize Tx queues */
2188 bcmgenet_init_tx_queues(priv->dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002189
2190 return 0;
2191}
2192
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002193/* Interrupt bottom half */
2194static void bcmgenet_irq_task(struct work_struct *work)
2195{
2196 struct bcmgenet_priv *priv = container_of(
2197 work, struct bcmgenet_priv, bcmgenet_irq_work);
2198
2199 netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
2200
Florian Fainelli8fdb0e02014-07-21 15:29:26 -07002201 if (priv->irq0_stat & UMAC_IRQ_MPD_R) {
2202 priv->irq0_stat &= ~UMAC_IRQ_MPD_R;
2203 netif_dbg(priv, wol, priv->dev,
2204 "magic packet detected, waking up\n");
2205 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
2206 }
2207
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002208 /* Link UP/DOWN event */
2209 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07002210 (priv->irq0_stat & (UMAC_IRQ_LINK_UP|UMAC_IRQ_LINK_DOWN))) {
Florian Fainelli80d8e962014-02-24 16:56:11 -08002211 phy_mac_interrupt(priv->phydev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002212 priv->irq0_stat & UMAC_IRQ_LINK_UP);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002213 priv->irq0_stat &= ~(UMAC_IRQ_LINK_UP|UMAC_IRQ_LINK_DOWN);
2214 }
2215}
2216
2217/* bcmgenet_isr1: interrupt handler for ring buffer. */
2218static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
2219{
2220 struct bcmgenet_priv *priv = dev_id;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002221 struct bcmgenet_tx_ring *ring;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002222 unsigned int index;
2223
2224 /* Save irq status for bottom-half processing. */
2225 priv->irq1_stat =
2226 bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002227 ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
Brian Norris7fc527f2014-07-29 14:34:14 -07002228 /* clear interrupts */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002229 bcmgenet_intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
2230
2231 netif_dbg(priv, intr, priv->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002232 "%s: IRQ=0x%x\n", __func__, priv->irq1_stat);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002233
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002234 /* Check the MBDONE interrupts.
2235 * packet is done, reclaim descriptors
2236 */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002237 for (index = 0; index < priv->hw_params->tx_queues; index++) {
2238 if (!(priv->irq1_stat & BIT(index)))
2239 continue;
2240
2241 ring = &priv->tx_rings[index];
2242
2243 if (likely(napi_schedule_prep(&ring->napi))) {
Petri Gynther9dbac282015-03-25 12:35:10 -07002244 ring->int_disable(ring);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002245 __napi_schedule(&ring->napi);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002246 }
2247 }
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002248
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002249 return IRQ_HANDLED;
2250}
2251
2252/* bcmgenet_isr0: Handle various interrupts. */
2253static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
2254{
2255 struct bcmgenet_priv *priv = dev_id;
2256
2257 /* Save irq status for bottom-half processing. */
2258 priv->irq0_stat =
2259 bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
2260 ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
Brian Norris7fc527f2014-07-29 14:34:14 -07002261 /* clear interrupts */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002262 bcmgenet_intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
2263
2264 netif_dbg(priv, intr, priv->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002265 "IRQ=0x%x\n", priv->irq0_stat);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002266
2267 if (priv->irq0_stat & (UMAC_IRQ_RXDMA_BDONE | UMAC_IRQ_RXDMA_PDONE)) {
2268 /* We use NAPI(software interrupt throttling, if
2269 * Rx Descriptor throttling is not used.
2270 * Disable interrupt, will be enabled in the poll method.
2271 */
2272 if (likely(napi_schedule_prep(&priv->napi))) {
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07002273 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_RXDMA_BDONE |
2274 UMAC_IRQ_RXDMA_PDONE,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002275 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002276 __napi_schedule(&priv->napi);
2277 }
2278 }
2279 if (priv->irq0_stat &
2280 (UMAC_IRQ_TXDMA_BDONE | UMAC_IRQ_TXDMA_PDONE)) {
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002281 struct bcmgenet_tx_ring *ring = &priv->tx_rings[DESC_INDEX];
2282
2283 if (likely(napi_schedule_prep(&ring->napi))) {
Petri Gynther9dbac282015-03-25 12:35:10 -07002284 ring->int_disable(ring);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002285 __napi_schedule(&ring->napi);
2286 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002287 }
2288 if (priv->irq0_stat & (UMAC_IRQ_PHY_DET_R |
2289 UMAC_IRQ_PHY_DET_F |
2290 UMAC_IRQ_LINK_UP |
2291 UMAC_IRQ_LINK_DOWN |
2292 UMAC_IRQ_HFB_SM |
2293 UMAC_IRQ_HFB_MM |
2294 UMAC_IRQ_MPD_R)) {
2295 /* all other interested interrupts handled in bottom half */
2296 schedule_work(&priv->bcmgenet_irq_work);
2297 }
2298
2299 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07002300 priv->irq0_stat & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002301 priv->irq0_stat &= ~(UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
2302 wake_up(&priv->wq);
2303 }
2304
2305 return IRQ_HANDLED;
2306}
2307
Florian Fainelli85620562014-07-21 15:29:23 -07002308static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
2309{
2310 struct bcmgenet_priv *priv = dev_id;
2311
2312 pm_wakeup_event(&priv->pdev->dev, 0);
2313
2314 return IRQ_HANDLED;
2315}
2316
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002317static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
2318{
2319 u32 reg;
2320
2321 reg = bcmgenet_rbuf_ctrl_get(priv);
2322 reg |= BIT(1);
2323 bcmgenet_rbuf_ctrl_set(priv, reg);
2324 udelay(10);
2325
2326 reg &= ~BIT(1);
2327 bcmgenet_rbuf_ctrl_set(priv, reg);
2328 udelay(10);
2329}
2330
2331static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002332 unsigned char *addr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002333{
2334 bcmgenet_umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
2335 (addr[2] << 8) | addr[3], UMAC_MAC0);
2336 bcmgenet_umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
2337}
2338
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002339/* Returns a reusable dma control register value */
2340static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
2341{
2342 u32 reg;
2343 u32 dma_ctrl;
2344
2345 /* disable DMA */
2346 dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
2347 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2348 reg &= ~dma_ctrl;
2349 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2350
2351 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2352 reg &= ~dma_ctrl;
2353 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2354
2355 bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
2356 udelay(10);
2357 bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
2358
2359 return dma_ctrl;
2360}
2361
2362static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
2363{
2364 u32 reg;
2365
2366 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2367 reg |= dma_ctrl;
2368 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2369
2370 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2371 reg |= dma_ctrl;
2372 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2373}
2374
Petri Gynther0034de42015-03-13 14:45:00 -07002375static bool bcmgenet_hfb_is_filter_enabled(struct bcmgenet_priv *priv,
2376 u32 f_index)
2377{
2378 u32 offset;
2379 u32 reg;
2380
2381 offset = HFB_FLT_ENABLE_V3PLUS + (f_index < 32) * sizeof(u32);
2382 reg = bcmgenet_hfb_reg_readl(priv, offset);
2383 return !!(reg & (1 << (f_index % 32)));
2384}
2385
2386static void bcmgenet_hfb_enable_filter(struct bcmgenet_priv *priv, u32 f_index)
2387{
2388 u32 offset;
2389 u32 reg;
2390
2391 offset = HFB_FLT_ENABLE_V3PLUS + (f_index < 32) * sizeof(u32);
2392 reg = bcmgenet_hfb_reg_readl(priv, offset);
2393 reg |= (1 << (f_index % 32));
2394 bcmgenet_hfb_reg_writel(priv, reg, offset);
2395}
2396
2397static void bcmgenet_hfb_set_filter_rx_queue_mapping(struct bcmgenet_priv *priv,
2398 u32 f_index, u32 rx_queue)
2399{
2400 u32 offset;
2401 u32 reg;
2402
2403 offset = f_index / 8;
2404 reg = bcmgenet_rdma_readl(priv, DMA_INDEX2RING_0 + offset);
2405 reg &= ~(0xF << (4 * (f_index % 8)));
2406 reg |= ((rx_queue & 0xF) << (4 * (f_index % 8)));
2407 bcmgenet_rdma_writel(priv, reg, DMA_INDEX2RING_0 + offset);
2408}
2409
2410static void bcmgenet_hfb_set_filter_length(struct bcmgenet_priv *priv,
2411 u32 f_index, u32 f_length)
2412{
2413 u32 offset;
2414 u32 reg;
2415
2416 offset = HFB_FLT_LEN_V3PLUS +
2417 ((priv->hw_params->hfb_filter_cnt - 1 - f_index) / 4) *
2418 sizeof(u32);
2419 reg = bcmgenet_hfb_reg_readl(priv, offset);
2420 reg &= ~(0xFF << (8 * (f_index % 4)));
2421 reg |= ((f_length & 0xFF) << (8 * (f_index % 4)));
2422 bcmgenet_hfb_reg_writel(priv, reg, offset);
2423}
2424
2425static int bcmgenet_hfb_find_unused_filter(struct bcmgenet_priv *priv)
2426{
2427 u32 f_index;
2428
2429 for (f_index = 0; f_index < priv->hw_params->hfb_filter_cnt; f_index++)
2430 if (!bcmgenet_hfb_is_filter_enabled(priv, f_index))
2431 return f_index;
2432
2433 return -ENOMEM;
2434}
2435
2436/* bcmgenet_hfb_add_filter
2437 *
2438 * Add new filter to Hardware Filter Block to match and direct Rx traffic to
2439 * desired Rx queue.
2440 *
2441 * f_data is an array of unsigned 32-bit integers where each 32-bit integer
2442 * provides filter data for 2 bytes (4 nibbles) of Rx frame:
2443 *
2444 * bits 31:20 - unused
2445 * bit 19 - nibble 0 match enable
2446 * bit 18 - nibble 1 match enable
2447 * bit 17 - nibble 2 match enable
2448 * bit 16 - nibble 3 match enable
2449 * bits 15:12 - nibble 0 data
2450 * bits 11:8 - nibble 1 data
2451 * bits 7:4 - nibble 2 data
2452 * bits 3:0 - nibble 3 data
2453 *
2454 * Example:
2455 * In order to match:
2456 * - Ethernet frame type = 0x0800 (IP)
2457 * - IP version field = 4
2458 * - IP protocol field = 0x11 (UDP)
2459 *
2460 * The following filter is needed:
2461 * u32 hfb_filter_ipv4_udp[] = {
2462 * Rx frame offset 0x00: 0x00000000, 0x00000000, 0x00000000, 0x00000000,
2463 * Rx frame offset 0x08: 0x00000000, 0x00000000, 0x000F0800, 0x00084000,
2464 * Rx frame offset 0x10: 0x00000000, 0x00000000, 0x00000000, 0x00030011,
2465 * };
2466 *
2467 * To add the filter to HFB and direct the traffic to Rx queue 0, call:
2468 * bcmgenet_hfb_add_filter(priv, hfb_filter_ipv4_udp,
2469 * ARRAY_SIZE(hfb_filter_ipv4_udp), 0);
2470 */
2471int bcmgenet_hfb_add_filter(struct bcmgenet_priv *priv, u32 *f_data,
2472 u32 f_length, u32 rx_queue)
2473{
2474 int f_index;
2475 u32 i;
2476
2477 f_index = bcmgenet_hfb_find_unused_filter(priv);
2478 if (f_index < 0)
2479 return -ENOMEM;
2480
2481 if (f_length > priv->hw_params->hfb_filter_size)
2482 return -EINVAL;
2483
2484 for (i = 0; i < f_length; i++)
2485 bcmgenet_hfb_writel(priv, f_data[i],
2486 (f_index * priv->hw_params->hfb_filter_size + i) *
2487 sizeof(u32));
2488
2489 bcmgenet_hfb_set_filter_length(priv, f_index, 2 * f_length);
2490 bcmgenet_hfb_set_filter_rx_queue_mapping(priv, f_index, rx_queue);
2491 bcmgenet_hfb_enable_filter(priv, f_index);
2492 bcmgenet_hfb_reg_writel(priv, 0x1, HFB_CTRL);
2493
2494 return 0;
2495}
2496
2497/* bcmgenet_hfb_clear
2498 *
2499 * Clear Hardware Filter Block and disable all filtering.
2500 */
2501static void bcmgenet_hfb_clear(struct bcmgenet_priv *priv)
2502{
2503 u32 i;
2504
2505 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_CTRL);
2506 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS);
2507 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS + 4);
2508
2509 for (i = DMA_INDEX2RING_0; i <= DMA_INDEX2RING_7; i++)
2510 bcmgenet_rdma_writel(priv, 0x0, i);
2511
2512 for (i = 0; i < (priv->hw_params->hfb_filter_cnt / 4); i++)
2513 bcmgenet_hfb_reg_writel(priv, 0x0,
2514 HFB_FLT_LEN_V3PLUS + i * sizeof(u32));
2515
2516 for (i = 0; i < priv->hw_params->hfb_filter_cnt *
2517 priv->hw_params->hfb_filter_size; i++)
2518 bcmgenet_hfb_writel(priv, 0x0, i * sizeof(u32));
2519}
2520
2521static void bcmgenet_hfb_init(struct bcmgenet_priv *priv)
2522{
2523 if (GENET_IS_V1(priv) || GENET_IS_V2(priv))
2524 return;
2525
2526 bcmgenet_hfb_clear(priv);
2527}
2528
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002529static void bcmgenet_netif_start(struct net_device *dev)
2530{
2531 struct bcmgenet_priv *priv = netdev_priv(dev);
2532
2533 /* Start the network engine */
Petri Gynther3ab11332015-03-25 12:35:15 -07002534 bcmgenet_enable_rx_napi(priv);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002535 bcmgenet_enable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002536
2537 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
2538
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002539 netif_tx_start_all_queues(dev);
2540
2541 phy_start(priv->phydev);
2542}
2543
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002544static int bcmgenet_open(struct net_device *dev)
2545{
2546 struct bcmgenet_priv *priv = netdev_priv(dev);
2547 unsigned long dma_ctrl;
2548 u32 reg;
2549 int ret;
2550
2551 netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
2552
2553 /* Turn on the clock */
2554 if (!IS_ERR(priv->clk))
2555 clk_prepare_enable(priv->clk);
2556
Florian Fainellia642c4f2015-03-23 15:09:56 -07002557 /* If this is an internal GPHY, power it back on now, before UniMAC is
2558 * brought out of reset as absolutely no UniMAC activity is allowed
2559 */
2560 if (phy_is_internal(priv->phydev))
2561 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
2562
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002563 /* take MAC out of reset */
2564 bcmgenet_umac_reset(priv);
2565
2566 ret = init_umac(priv);
2567 if (ret)
2568 goto err_clk_disable;
2569
2570 /* disable ethernet MAC while updating its registers */
Florian Fainellie29585b2014-07-21 15:29:20 -07002571 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002572
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002573 /* Make sure we reflect the value of CRC_CMD_FWD */
2574 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
2575 priv->crc_fwd_en = !!(reg & CMD_CRC_FWD);
2576
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002577 bcmgenet_set_hw_addr(priv, dev->dev_addr);
2578
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002579 if (phy_is_internal(priv->phydev)) {
2580 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
2581 reg |= EXT_ENERGY_DET_MASK;
2582 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
2583 }
2584
2585 /* Disable RX/TX DMA and flush TX queues */
2586 dma_ctrl = bcmgenet_dma_disable(priv);
2587
2588 /* Reinitialize TDMA and RDMA and SW housekeeping */
2589 ret = bcmgenet_init_dma(priv);
2590 if (ret) {
2591 netdev_err(dev, "failed to initialize DMA\n");
2592 goto err_fini_dma;
2593 }
2594
2595 /* Always enable ring 16 - descriptor ring */
2596 bcmgenet_enable_dma(priv, dma_ctrl);
2597
Petri Gynther0034de42015-03-13 14:45:00 -07002598 /* HFB init */
2599 bcmgenet_hfb_init(priv);
2600
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002601 ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002602 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002603 if (ret < 0) {
2604 netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
2605 goto err_fini_dma;
2606 }
2607
2608 ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002609 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002610 if (ret < 0) {
2611 netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
2612 goto err_irq0;
2613 }
2614
Florian Fainellidbd479d2014-11-10 18:06:21 -08002615 /* Re-configure the port multiplexer towards the PHY device */
2616 bcmgenet_mii_config(priv->dev, false);
2617
Florian Fainellic96e7312014-11-10 18:06:20 -08002618 phy_connect_direct(dev, priv->phydev, bcmgenet_mii_setup,
2619 priv->phy_interface);
2620
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002621 bcmgenet_netif_start(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002622
2623 return 0;
2624
2625err_irq0:
2626 free_irq(priv->irq0, dev);
2627err_fini_dma:
2628 bcmgenet_fini_dma(priv);
2629err_clk_disable:
2630 if (!IS_ERR(priv->clk))
2631 clk_disable_unprepare(priv->clk);
2632 return ret;
2633}
2634
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002635static void bcmgenet_netif_stop(struct net_device *dev)
2636{
2637 struct bcmgenet_priv *priv = netdev_priv(dev);
2638
2639 netif_tx_stop_all_queues(dev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002640 phy_stop(priv->phydev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002641 bcmgenet_intr_disable(priv);
Petri Gynther3ab11332015-03-25 12:35:15 -07002642 bcmgenet_disable_rx_napi(priv);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002643 bcmgenet_disable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002644
2645 /* Wait for pending work items to complete. Since interrupts are
2646 * disabled no new work will be scheduled.
2647 */
2648 cancel_work_sync(&priv->bcmgenet_irq_work);
Florian Fainellicc013fb2014-08-11 14:50:43 -07002649
Florian Fainellicc013fb2014-08-11 14:50:43 -07002650 priv->old_link = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002651 priv->old_speed = -1;
Florian Fainellicc013fb2014-08-11 14:50:43 -07002652 priv->old_duplex = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002653 priv->old_pause = -1;
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002654}
2655
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002656static int bcmgenet_close(struct net_device *dev)
2657{
2658 struct bcmgenet_priv *priv = netdev_priv(dev);
2659 int ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002660
2661 netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
2662
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002663 bcmgenet_netif_stop(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002664
Florian Fainellic96e7312014-11-10 18:06:20 -08002665 /* Really kill the PHY state machine and disconnect from it */
2666 phy_disconnect(priv->phydev);
2667
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002668 /* Disable MAC receive */
Florian Fainellie29585b2014-07-21 15:29:20 -07002669 umac_enable_set(priv, CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002670
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002671 ret = bcmgenet_dma_teardown(priv);
2672 if (ret)
2673 return ret;
2674
2675 /* Disable MAC transmit. TX DMA disabled have to done before this */
Florian Fainellie29585b2014-07-21 15:29:20 -07002676 umac_enable_set(priv, CMD_TX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002677
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002678 /* tx reclaim */
2679 bcmgenet_tx_reclaim_all(dev);
2680 bcmgenet_fini_dma(priv);
2681
2682 free_irq(priv->irq0, priv);
2683 free_irq(priv->irq1, priv);
2684
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002685 if (phy_is_internal(priv->phydev))
Florian Fainellica8cf342015-03-23 15:09:51 -07002686 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002687
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002688 if (!IS_ERR(priv->clk))
2689 clk_disable_unprepare(priv->clk);
2690
Florian Fainellica8cf342015-03-23 15:09:51 -07002691 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002692}
2693
2694static void bcmgenet_timeout(struct net_device *dev)
2695{
2696 struct bcmgenet_priv *priv = netdev_priv(dev);
2697
2698 netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
2699
2700 dev->trans_start = jiffies;
2701
2702 dev->stats.tx_errors++;
2703
2704 netif_tx_wake_all_queues(dev);
2705}
2706
2707#define MAX_MC_COUNT 16
2708
2709static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
2710 unsigned char *addr,
2711 int *i,
2712 int *mc)
2713{
2714 u32 reg;
2715
Florian Fainellic91b7f62014-07-23 10:42:12 -07002716 bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
2717 UMAC_MDF_ADDR + (*i * 4));
2718 bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
2719 addr[4] << 8 | addr[5],
2720 UMAC_MDF_ADDR + ((*i + 1) * 4));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002721 reg = bcmgenet_umac_readl(priv, UMAC_MDF_CTRL);
2722 reg |= (1 << (MAX_MC_COUNT - *mc));
2723 bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
2724 *i += 2;
2725 (*mc)++;
2726}
2727
2728static void bcmgenet_set_rx_mode(struct net_device *dev)
2729{
2730 struct bcmgenet_priv *priv = netdev_priv(dev);
2731 struct netdev_hw_addr *ha;
2732 int i, mc;
2733 u32 reg;
2734
2735 netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
2736
Brian Norris7fc527f2014-07-29 14:34:14 -07002737 /* Promiscuous mode */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002738 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
2739 if (dev->flags & IFF_PROMISC) {
2740 reg |= CMD_PROMISC;
2741 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
2742 bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
2743 return;
2744 } else {
2745 reg &= ~CMD_PROMISC;
2746 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
2747 }
2748
2749 /* UniMac doesn't support ALLMULTI */
2750 if (dev->flags & IFF_ALLMULTI) {
2751 netdev_warn(dev, "ALLMULTI is not supported\n");
2752 return;
2753 }
2754
2755 /* update MDF filter */
2756 i = 0;
2757 mc = 0;
2758 /* Broadcast */
2759 bcmgenet_set_mdf_addr(priv, dev->broadcast, &i, &mc);
2760 /* my own address.*/
2761 bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i, &mc);
2762 /* Unicast list*/
2763 if (netdev_uc_count(dev) > (MAX_MC_COUNT - mc))
2764 return;
2765
2766 if (!netdev_uc_empty(dev))
2767 netdev_for_each_uc_addr(ha, dev)
2768 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
2769 /* Multicast */
2770 if (netdev_mc_empty(dev) || netdev_mc_count(dev) >= (MAX_MC_COUNT - mc))
2771 return;
2772
2773 netdev_for_each_mc_addr(ha, dev)
2774 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
2775}
2776
2777/* Set the hardware MAC address. */
2778static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
2779{
2780 struct sockaddr *addr = p;
2781
2782 /* Setting the MAC address at the hardware level is not possible
2783 * without disabling the UniMAC RX/TX enable bits.
2784 */
2785 if (netif_running(dev))
2786 return -EBUSY;
2787
2788 ether_addr_copy(dev->dev_addr, addr->sa_data);
2789
2790 return 0;
2791}
2792
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002793static const struct net_device_ops bcmgenet_netdev_ops = {
2794 .ndo_open = bcmgenet_open,
2795 .ndo_stop = bcmgenet_close,
2796 .ndo_start_xmit = bcmgenet_xmit,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002797 .ndo_tx_timeout = bcmgenet_timeout,
2798 .ndo_set_rx_mode = bcmgenet_set_rx_mode,
2799 .ndo_set_mac_address = bcmgenet_set_mac_addr,
2800 .ndo_do_ioctl = bcmgenet_ioctl,
2801 .ndo_set_features = bcmgenet_set_features,
2802};
2803
2804/* Array of GENET hardware parameters/characteristics */
2805static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
2806 [GENET_V1] = {
2807 .tx_queues = 0,
Petri Gynther51a966a2015-02-23 11:00:46 -08002808 .tx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002809 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08002810 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002811 .bp_in_en_shift = 16,
2812 .bp_in_mask = 0xffff,
2813 .hfb_filter_cnt = 16,
2814 .qtag_mask = 0x1F,
2815 .hfb_offset = 0x1000,
2816 .rdma_offset = 0x2000,
2817 .tdma_offset = 0x3000,
2818 .words_per_bd = 2,
2819 },
2820 [GENET_V2] = {
2821 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08002822 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08002823 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08002824 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002825 .bp_in_en_shift = 16,
2826 .bp_in_mask = 0xffff,
2827 .hfb_filter_cnt = 16,
2828 .qtag_mask = 0x1F,
2829 .tbuf_offset = 0x0600,
2830 .hfb_offset = 0x1000,
2831 .hfb_reg_offset = 0x2000,
2832 .rdma_offset = 0x3000,
2833 .tdma_offset = 0x4000,
2834 .words_per_bd = 2,
2835 .flags = GENET_HAS_EXT,
2836 },
2837 [GENET_V3] = {
2838 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08002839 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08002840 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08002841 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002842 .bp_in_en_shift = 17,
2843 .bp_in_mask = 0x1ffff,
2844 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07002845 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002846 .qtag_mask = 0x3F,
2847 .tbuf_offset = 0x0600,
2848 .hfb_offset = 0x8000,
2849 .hfb_reg_offset = 0xfc00,
2850 .rdma_offset = 0x10000,
2851 .tdma_offset = 0x11000,
2852 .words_per_bd = 2,
2853 .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR,
2854 },
2855 [GENET_V4] = {
2856 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08002857 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08002858 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08002859 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002860 .bp_in_en_shift = 17,
2861 .bp_in_mask = 0x1ffff,
2862 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07002863 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002864 .qtag_mask = 0x3F,
2865 .tbuf_offset = 0x0600,
2866 .hfb_offset = 0x8000,
2867 .hfb_reg_offset = 0xfc00,
2868 .rdma_offset = 0x2000,
2869 .tdma_offset = 0x4000,
2870 .words_per_bd = 3,
2871 .flags = GENET_HAS_40BITS | GENET_HAS_EXT | GENET_HAS_MDIO_INTR,
2872 },
2873};
2874
2875/* Infer hardware parameters from the detected GENET version */
2876static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
2877{
2878 struct bcmgenet_hw_params *params;
2879 u32 reg;
2880 u8 major;
Florian Fainellib04a2f52014-12-03 09:56:59 -08002881 u16 gphy_rev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002882
2883 if (GENET_IS_V4(priv)) {
2884 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
2885 genet_dma_ring_regs = genet_dma_ring_regs_v4;
2886 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
2887 priv->version = GENET_V4;
2888 } else if (GENET_IS_V3(priv)) {
2889 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
2890 genet_dma_ring_regs = genet_dma_ring_regs_v123;
2891 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
2892 priv->version = GENET_V3;
2893 } else if (GENET_IS_V2(priv)) {
2894 bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
2895 genet_dma_ring_regs = genet_dma_ring_regs_v123;
2896 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
2897 priv->version = GENET_V2;
2898 } else if (GENET_IS_V1(priv)) {
2899 bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
2900 genet_dma_ring_regs = genet_dma_ring_regs_v123;
2901 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
2902 priv->version = GENET_V1;
2903 }
2904
2905 /* enum genet_version starts at 1 */
2906 priv->hw_params = &bcmgenet_hw_params[priv->version];
2907 params = priv->hw_params;
2908
2909 /* Read GENET HW version */
2910 reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
2911 major = (reg >> 24 & 0x0f);
2912 if (major == 5)
2913 major = 4;
2914 else if (major == 0)
2915 major = 1;
2916 if (major != priv->version) {
2917 dev_err(&priv->pdev->dev,
2918 "GENET version mismatch, got: %d, configured for: %d\n",
2919 major, priv->version);
2920 }
2921
2922 /* Print the GENET core version */
2923 dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002924 major, (reg >> 16) & 0x0f, reg & 0xffff);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002925
Florian Fainelli487320c2014-09-19 13:07:53 -07002926 /* Store the integrated PHY revision for the MDIO probing function
2927 * to pass this information to the PHY driver. The PHY driver expects
2928 * to find the PHY major revision in bits 15:8 while the GENET register
2929 * stores that information in bits 7:0, account for that.
Florian Fainellib04a2f52014-12-03 09:56:59 -08002930 *
2931 * On newer chips, starting with PHY revision G0, a new scheme is
2932 * deployed similar to the Starfighter 2 switch with GPHY major
2933 * revision in bits 15:8 and patch level in bits 7:0. Major revision 0
2934 * is reserved as well as special value 0x01ff, we have a small
2935 * heuristic to check for the new GPHY revision and re-arrange things
2936 * so the GPHY driver is happy.
Florian Fainelli487320c2014-09-19 13:07:53 -07002937 */
Florian Fainellib04a2f52014-12-03 09:56:59 -08002938 gphy_rev = reg & 0xffff;
2939
2940 /* This is the good old scheme, just GPHY major, no minor nor patch */
2941 if ((gphy_rev & 0xf0) != 0)
2942 priv->gphy_rev = gphy_rev << 8;
2943
2944 /* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */
2945 else if ((gphy_rev & 0xff00) != 0)
2946 priv->gphy_rev = gphy_rev;
2947
2948 /* This is reserved so should require special treatment */
2949 else if (gphy_rev == 0 || gphy_rev == 0x01ff) {
2950 pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev);
2951 return;
2952 }
Florian Fainelli487320c2014-09-19 13:07:53 -07002953
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002954#ifdef CONFIG_PHYS_ADDR_T_64BIT
2955 if (!(params->flags & GENET_HAS_40BITS))
2956 pr_warn("GENET does not support 40-bits PA\n");
2957#endif
2958
2959 pr_debug("Configuration for version: %d\n"
Petri Gynther3feafa02015-03-05 17:40:14 -08002960 "TXq: %1d, TXqBDs: %1d, RXq: %1d, RXqBDs: %1d\n"
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002961 "BP << en: %2d, BP msk: 0x%05x\n"
2962 "HFB count: %2d, QTAQ msk: 0x%05x\n"
2963 "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
2964 "RDMA: 0x%05x, TDMA: 0x%05x\n"
2965 "Words/BD: %d\n",
2966 priv->version,
Petri Gynther51a966a2015-02-23 11:00:46 -08002967 params->tx_queues, params->tx_bds_per_q,
Petri Gynther3feafa02015-03-05 17:40:14 -08002968 params->rx_queues, params->rx_bds_per_q,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002969 params->bp_in_en_shift, params->bp_in_mask,
2970 params->hfb_filter_cnt, params->qtag_mask,
2971 params->tbuf_offset, params->hfb_offset,
2972 params->hfb_reg_offset,
2973 params->rdma_offset, params->tdma_offset,
2974 params->words_per_bd);
2975}
2976
2977static const struct of_device_id bcmgenet_match[] = {
2978 { .compatible = "brcm,genet-v1", .data = (void *)GENET_V1 },
2979 { .compatible = "brcm,genet-v2", .data = (void *)GENET_V2 },
2980 { .compatible = "brcm,genet-v3", .data = (void *)GENET_V3 },
2981 { .compatible = "brcm,genet-v4", .data = (void *)GENET_V4 },
2982 { },
2983};
2984
2985static int bcmgenet_probe(struct platform_device *pdev)
2986{
Petri Gyntherb0ba5122014-12-01 16:18:08 -08002987 struct bcmgenet_platform_data *pd = pdev->dev.platform_data;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002988 struct device_node *dn = pdev->dev.of_node;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08002989 const struct of_device_id *of_id = NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002990 struct bcmgenet_priv *priv;
2991 struct net_device *dev;
2992 const void *macaddr;
2993 struct resource *r;
2994 int err = -EIO;
2995
Petri Gynther3feafee2015-03-05 17:40:12 -08002996 /* Up to GENET_MAX_MQ_CNT + 1 TX queues and RX queues */
2997 dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1,
2998 GENET_MAX_MQ_CNT + 1);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002999 if (!dev) {
3000 dev_err(&pdev->dev, "can't allocate net device\n");
3001 return -ENOMEM;
3002 }
3003
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003004 if (dn) {
3005 of_id = of_match_node(bcmgenet_match, dn);
3006 if (!of_id)
3007 return -EINVAL;
3008 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003009
3010 priv = netdev_priv(dev);
3011 priv->irq0 = platform_get_irq(pdev, 0);
3012 priv->irq1 = platform_get_irq(pdev, 1);
Florian Fainelli85620562014-07-21 15:29:23 -07003013 priv->wol_irq = platform_get_irq(pdev, 2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003014 if (!priv->irq0 || !priv->irq1) {
3015 dev_err(&pdev->dev, "can't find IRQs\n");
3016 err = -EINVAL;
3017 goto err;
3018 }
3019
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003020 if (dn) {
3021 macaddr = of_get_mac_address(dn);
3022 if (!macaddr) {
3023 dev_err(&pdev->dev, "can't find MAC address\n");
3024 err = -EINVAL;
3025 goto err;
3026 }
3027 } else {
3028 macaddr = pd->mac_address;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003029 }
3030
3031 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam5343a102014-02-24 00:47:24 -03003032 priv->base = devm_ioremap_resource(&pdev->dev, r);
3033 if (IS_ERR(priv->base)) {
3034 err = PTR_ERR(priv->base);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003035 goto err;
3036 }
3037
3038 SET_NETDEV_DEV(dev, &pdev->dev);
3039 dev_set_drvdata(&pdev->dev, dev);
3040 ether_addr_copy(dev->dev_addr, macaddr);
3041 dev->watchdog_timeo = 2 * HZ;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003042 dev->ethtool_ops = &bcmgenet_ethtool_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003043 dev->netdev_ops = &bcmgenet_netdev_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003044
3045 priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
3046
3047 /* Set hardware features */
3048 dev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM |
3049 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
3050
Florian Fainelli85620562014-07-21 15:29:23 -07003051 /* Request the WOL interrupt and advertise suspend if available */
3052 priv->wol_irq_disabled = true;
3053 err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0,
3054 dev->name, priv);
3055 if (!err)
3056 device_set_wakeup_capable(&pdev->dev, 1);
3057
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003058 /* Set the needed headroom to account for any possible
3059 * features enabling/disabling at runtime
3060 */
3061 dev->needed_headroom += 64;
3062
3063 netdev_boot_setup_check(dev);
3064
3065 priv->dev = dev;
3066 priv->pdev = pdev;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003067 if (of_id)
3068 priv->version = (enum bcmgenet_version)of_id->data;
3069 else
3070 priv->version = pd->genet_version;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003071
Florian Fainellie4a60a92014-08-11 14:50:42 -07003072 priv->clk = devm_clk_get(&priv->pdev->dev, "enet");
3073 if (IS_ERR(priv->clk))
3074 dev_warn(&priv->pdev->dev, "failed to get enet clock\n");
3075
3076 if (!IS_ERR(priv->clk))
3077 clk_prepare_enable(priv->clk);
3078
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003079 bcmgenet_set_hw_params(priv);
3080
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003081 /* Mii wait queue */
3082 init_waitqueue_head(&priv->wq);
3083 /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
3084 priv->rx_buf_len = RX_BUF_LENGTH;
3085 INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
3086
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003087 priv->clk_wol = devm_clk_get(&priv->pdev->dev, "enet-wol");
3088 if (IS_ERR(priv->clk_wol))
3089 dev_warn(&priv->pdev->dev, "failed to get enet-wol clock\n");
3090
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003091 priv->clk_eee = devm_clk_get(&priv->pdev->dev, "enet-eee");
3092 if (IS_ERR(priv->clk_eee)) {
3093 dev_warn(&priv->pdev->dev, "failed to get enet-eee clock\n");
3094 priv->clk_eee = NULL;
3095 }
3096
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003097 err = reset_umac(priv);
3098 if (err)
3099 goto err_clk_disable;
3100
3101 err = bcmgenet_mii_init(dev);
3102 if (err)
3103 goto err_clk_disable;
3104
3105 /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
3106 * just the ring 16 descriptor based TX
3107 */
3108 netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
3109 netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
3110
Florian Fainelli219575e2014-06-26 10:26:21 -07003111 /* libphy will determine the link state */
3112 netif_carrier_off(dev);
3113
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003114 /* Turn off the main clock, WOL clock is handled separately */
3115 if (!IS_ERR(priv->clk))
3116 clk_disable_unprepare(priv->clk);
3117
Florian Fainelli0f50ce92014-06-26 10:26:20 -07003118 err = register_netdev(dev);
3119 if (err)
3120 goto err;
3121
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003122 return err;
3123
3124err_clk_disable:
3125 if (!IS_ERR(priv->clk))
3126 clk_disable_unprepare(priv->clk);
3127err:
3128 free_netdev(dev);
3129 return err;
3130}
3131
3132static int bcmgenet_remove(struct platform_device *pdev)
3133{
3134 struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
3135
3136 dev_set_drvdata(&pdev->dev, NULL);
3137 unregister_netdev(priv->dev);
3138 bcmgenet_mii_exit(priv->dev);
3139 free_netdev(priv->dev);
3140
3141 return 0;
3142}
3143
Florian Fainellib6e978e2014-07-21 15:29:22 -07003144#ifdef CONFIG_PM_SLEEP
3145static int bcmgenet_suspend(struct device *d)
3146{
3147 struct net_device *dev = dev_get_drvdata(d);
3148 struct bcmgenet_priv *priv = netdev_priv(dev);
3149 int ret;
3150
3151 if (!netif_running(dev))
3152 return 0;
3153
3154 bcmgenet_netif_stop(dev);
3155
Florian Fainellicc013fb2014-08-11 14:50:43 -07003156 phy_suspend(priv->phydev);
3157
Florian Fainellib6e978e2014-07-21 15:29:22 -07003158 netif_device_detach(dev);
3159
3160 /* Disable MAC receive */
3161 umac_enable_set(priv, CMD_RX_EN, false);
3162
3163 ret = bcmgenet_dma_teardown(priv);
3164 if (ret)
3165 return ret;
3166
3167 /* Disable MAC transmit. TX DMA disabled have to done before this */
3168 umac_enable_set(priv, CMD_TX_EN, false);
3169
3170 /* tx reclaim */
3171 bcmgenet_tx_reclaim_all(dev);
3172 bcmgenet_fini_dma(priv);
3173
Florian Fainelli8c90db72014-07-21 15:29:28 -07003174 /* Prepare the device for Wake-on-LAN and switch to the slow clock */
3175 if (device_may_wakeup(d) && priv->wolopts) {
Florian Fainellica8cf342015-03-23 15:09:51 -07003176 ret = bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003177 clk_prepare_enable(priv->clk_wol);
Florian Fainellia6f31f52015-03-23 15:09:57 -07003178 } else if (phy_is_internal(priv->phydev)) {
3179 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003180 }
3181
Florian Fainellib6e978e2014-07-21 15:29:22 -07003182 /* Turn off the clocks */
3183 clk_disable_unprepare(priv->clk);
3184
Florian Fainellica8cf342015-03-23 15:09:51 -07003185 return ret;
Florian Fainellib6e978e2014-07-21 15:29:22 -07003186}
3187
3188static int bcmgenet_resume(struct device *d)
3189{
3190 struct net_device *dev = dev_get_drvdata(d);
3191 struct bcmgenet_priv *priv = netdev_priv(dev);
3192 unsigned long dma_ctrl;
3193 int ret;
3194 u32 reg;
3195
3196 if (!netif_running(dev))
3197 return 0;
3198
3199 /* Turn on the clock */
3200 ret = clk_prepare_enable(priv->clk);
3201 if (ret)
3202 return ret;
3203
Florian Fainellia6f31f52015-03-23 15:09:57 -07003204 /* If this is an internal GPHY, power it back on now, before UniMAC is
3205 * brought out of reset as absolutely no UniMAC activity is allowed
3206 */
3207 if (phy_is_internal(priv->phydev))
3208 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3209
Florian Fainellib6e978e2014-07-21 15:29:22 -07003210 bcmgenet_umac_reset(priv);
3211
3212 ret = init_umac(priv);
3213 if (ret)
3214 goto out_clk_disable;
3215
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003216 /* From WOL-enabled suspend, switch to regular clock */
3217 if (priv->wolopts)
3218 clk_disable_unprepare(priv->clk_wol);
3219
3220 phy_init_hw(priv->phydev);
3221 /* Speed settings must be restored */
Florian Fainellidbd479d2014-11-10 18:06:21 -08003222 bcmgenet_mii_config(priv->dev, false);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003223
Florian Fainellib6e978e2014-07-21 15:29:22 -07003224 /* disable ethernet MAC while updating its registers */
3225 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
3226
3227 bcmgenet_set_hw_addr(priv, dev->dev_addr);
3228
3229 if (phy_is_internal(priv->phydev)) {
3230 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
3231 reg |= EXT_ENERGY_DET_MASK;
3232 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
3233 }
3234
Florian Fainelli98bb7392014-08-11 14:50:45 -07003235 if (priv->wolopts)
3236 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
3237
Florian Fainellib6e978e2014-07-21 15:29:22 -07003238 /* Disable RX/TX DMA and flush TX queues */
3239 dma_ctrl = bcmgenet_dma_disable(priv);
3240
3241 /* Reinitialize TDMA and RDMA and SW housekeeping */
3242 ret = bcmgenet_init_dma(priv);
3243 if (ret) {
3244 netdev_err(dev, "failed to initialize DMA\n");
3245 goto out_clk_disable;
3246 }
3247
3248 /* Always enable ring 16 - descriptor ring */
3249 bcmgenet_enable_dma(priv, dma_ctrl);
3250
3251 netif_device_attach(dev);
3252
Florian Fainellicc013fb2014-08-11 14:50:43 -07003253 phy_resume(priv->phydev);
3254
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003255 if (priv->eee.eee_enabled)
3256 bcmgenet_eee_enable_set(dev, true);
3257
Florian Fainellib6e978e2014-07-21 15:29:22 -07003258 bcmgenet_netif_start(dev);
3259
3260 return 0;
3261
3262out_clk_disable:
3263 clk_disable_unprepare(priv->clk);
3264 return ret;
3265}
3266#endif /* CONFIG_PM_SLEEP */
3267
3268static SIMPLE_DEV_PM_OPS(bcmgenet_pm_ops, bcmgenet_suspend, bcmgenet_resume);
3269
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003270static struct platform_driver bcmgenet_driver = {
3271 .probe = bcmgenet_probe,
3272 .remove = bcmgenet_remove,
3273 .driver = {
3274 .name = "bcmgenet",
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003275 .of_match_table = bcmgenet_match,
Florian Fainellib6e978e2014-07-21 15:29:22 -07003276 .pm = &bcmgenet_pm_ops,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003277 },
3278};
3279module_platform_driver(bcmgenet_driver);
3280
3281MODULE_AUTHOR("Broadcom Corporation");
3282MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
3283MODULE_ALIAS("platform:bcmgenet");
3284MODULE_LICENSE("GPL");