blob: 31e14079e1d7988b4b9429b68c18f09477544b29 [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 Gynther4055eae2015-03-25 12:35:16 -0700967static inline void bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring *ring)
968{
969 bcmgenet_intrl2_0_writel(ring->priv,
970 UMAC_IRQ_RXDMA_BDONE | UMAC_IRQ_RXDMA_PDONE,
971 INTRL2_CPU_MASK_SET);
972}
973
974static inline void bcmgenet_rx_ring16_int_enable(struct bcmgenet_rx_ring *ring)
975{
976 bcmgenet_intrl2_0_writel(ring->priv,
977 UMAC_IRQ_RXDMA_BDONE | UMAC_IRQ_RXDMA_PDONE,
978 INTRL2_CPU_MASK_CLEAR);
979}
980
981static inline void bcmgenet_rx_ring_int_disable(struct bcmgenet_rx_ring *ring)
982{
983 bcmgenet_intrl2_1_writel(ring->priv,
984 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
985 INTRL2_CPU_MASK_SET);
986}
987
988static inline void bcmgenet_rx_ring_int_enable(struct bcmgenet_rx_ring *ring)
989{
990 bcmgenet_intrl2_1_writel(ring->priv,
991 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
992 INTRL2_CPU_MASK_CLEAR);
993}
994
Petri Gynther9dbac282015-03-25 12:35:10 -0700995static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800996{
Petri Gynther9dbac282015-03-25 12:35:10 -0700997 bcmgenet_intrl2_0_writel(ring->priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700998 UMAC_IRQ_TXDMA_BDONE | UMAC_IRQ_TXDMA_PDONE,
999 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001000}
1001
Petri Gynther9dbac282015-03-25 12:35:10 -07001002static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001003{
Petri Gynther9dbac282015-03-25 12:35:10 -07001004 bcmgenet_intrl2_0_writel(ring->priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001005 UMAC_IRQ_TXDMA_BDONE | UMAC_IRQ_TXDMA_PDONE,
1006 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001007}
1008
Petri Gynther9dbac282015-03-25 12:35:10 -07001009static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001010{
Petri Gynther9dbac282015-03-25 12:35:10 -07001011 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001012 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001013}
1014
Petri Gynther9dbac282015-03-25 12:35:10 -07001015static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001016{
Petri Gynther9dbac282015-03-25 12:35:10 -07001017 bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001018 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001019}
1020
1021/* Unlocked version of the reclaim routine */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001022static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
1023 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001024{
1025 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001026 struct enet_cb *tx_cb_ptr;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001027 struct netdev_queue *txq;
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001028 unsigned int pkts_compl = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001029 unsigned int c_index;
Petri Gynther66d06752015-03-04 14:30:01 -08001030 unsigned int txbds_ready;
1031 unsigned int txbds_processed = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001032
Brian Norris7fc527f2014-07-29 14:34:14 -07001033 /* Compute how many buffers are transmitted since last xmit call */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001034 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
Petri Gynther66d06752015-03-04 14:30:01 -08001035 c_index &= DMA_C_INDEX_MASK;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001036
Petri Gynther66d06752015-03-04 14:30:01 -08001037 if (likely(c_index >= ring->c_index))
1038 txbds_ready = c_index - ring->c_index;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001039 else
Petri Gynther66d06752015-03-04 14:30:01 -08001040 txbds_ready = (DMA_C_INDEX_MASK + 1) - ring->c_index + c_index;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001041
1042 netif_dbg(priv, tx_done, dev,
Petri Gynther66d06752015-03-04 14:30:01 -08001043 "%s ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
1044 __func__, ring->index, ring->c_index, c_index, txbds_ready);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001045
1046 /* Reclaim transmitted buffers */
Petri Gynther66d06752015-03-04 14:30:01 -08001047 while (txbds_processed < txbds_ready) {
1048 tx_cb_ptr = &priv->tx_cbs[ring->clean_ptr];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001049 if (tx_cb_ptr->skb) {
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001050 pkts_compl++;
Petri Gynther66d06752015-03-04 14:30:01 -08001051 dev->stats.tx_packets++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001052 dev->stats.tx_bytes += tx_cb_ptr->skb->len;
1053 dma_unmap_single(&dev->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001054 dma_unmap_addr(tx_cb_ptr, dma_addr),
1055 tx_cb_ptr->skb->len,
1056 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001057 bcmgenet_free_cb(tx_cb_ptr);
1058 } else if (dma_unmap_addr(tx_cb_ptr, dma_addr)) {
1059 dev->stats.tx_bytes +=
1060 dma_unmap_len(tx_cb_ptr, dma_len);
1061 dma_unmap_page(&dev->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001062 dma_unmap_addr(tx_cb_ptr, dma_addr),
1063 dma_unmap_len(tx_cb_ptr, dma_len),
1064 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001065 dma_unmap_addr_set(tx_cb_ptr, dma_addr, 0);
1066 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001067
Petri Gynther66d06752015-03-04 14:30:01 -08001068 txbds_processed++;
1069 if (likely(ring->clean_ptr < ring->end_ptr))
1070 ring->clean_ptr++;
1071 else
1072 ring->clean_ptr = ring->cb_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001073 }
1074
Petri Gynther66d06752015-03-04 14:30:01 -08001075 ring->free_bds += txbds_processed;
1076 ring->c_index = (ring->c_index + txbds_processed) & DMA_C_INDEX_MASK;
1077
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001078 if (ring->free_bds > (MAX_SKB_FRAGS + 1)) {
Petri Gynther66d06752015-03-04 14:30:01 -08001079 txq = netdev_get_tx_queue(dev, ring->queue);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001080 if (netif_tx_queue_stopped(txq))
1081 netif_tx_wake_queue(txq);
1082 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001083
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001084 return pkts_compl;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001085}
1086
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001087static unsigned int bcmgenet_tx_reclaim(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001088 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001089{
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001090 unsigned int released;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001091 unsigned long flags;
1092
1093 spin_lock_irqsave(&ring->lock, flags);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001094 released = __bcmgenet_tx_reclaim(dev, ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001095 spin_unlock_irqrestore(&ring->lock, flags);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001096
1097 return released;
1098}
1099
1100static int bcmgenet_tx_poll(struct napi_struct *napi, int budget)
1101{
1102 struct bcmgenet_tx_ring *ring =
1103 container_of(napi, struct bcmgenet_tx_ring, napi);
1104 unsigned int work_done = 0;
1105
1106 work_done = bcmgenet_tx_reclaim(ring->priv->dev, ring);
1107
1108 if (work_done == 0) {
1109 napi_complete(napi);
Petri Gynther9dbac282015-03-25 12:35:10 -07001110 ring->int_enable(ring);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001111
1112 return 0;
1113 }
1114
1115 return budget;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001116}
1117
1118static void bcmgenet_tx_reclaim_all(struct net_device *dev)
1119{
1120 struct bcmgenet_priv *priv = netdev_priv(dev);
1121 int i;
1122
1123 if (netif_is_multiqueue(dev)) {
1124 for (i = 0; i < priv->hw_params->tx_queues; i++)
1125 bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
1126 }
1127
1128 bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
1129}
1130
1131/* Transmits a single SKB (either head of a fragment or a single SKB)
1132 * caller must hold priv->lock
1133 */
1134static int bcmgenet_xmit_single(struct net_device *dev,
1135 struct sk_buff *skb,
1136 u16 dma_desc_flags,
1137 struct bcmgenet_tx_ring *ring)
1138{
1139 struct bcmgenet_priv *priv = netdev_priv(dev);
1140 struct device *kdev = &priv->pdev->dev;
1141 struct enet_cb *tx_cb_ptr;
1142 unsigned int skb_len;
1143 dma_addr_t mapping;
1144 u32 length_status;
1145 int ret;
1146
1147 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
1148
1149 if (unlikely(!tx_cb_ptr))
1150 BUG();
1151
1152 tx_cb_ptr->skb = skb;
1153
1154 skb_len = skb_headlen(skb) < ETH_ZLEN ? ETH_ZLEN : skb_headlen(skb);
1155
1156 mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
1157 ret = dma_mapping_error(kdev, mapping);
1158 if (ret) {
Florian Fainelli44c8bc32014-11-19 10:29:56 -08001159 priv->mib.tx_dma_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001160 netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
1161 dev_kfree_skb(skb);
1162 return ret;
1163 }
1164
1165 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
1166 dma_unmap_len_set(tx_cb_ptr, dma_len, skb->len);
1167 length_status = (skb_len << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
1168 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT) |
1169 DMA_TX_APPEND_CRC;
1170
1171 if (skb->ip_summed == CHECKSUM_PARTIAL)
1172 length_status |= DMA_TX_DO_CSUM;
1173
1174 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, length_status);
1175
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001176 return 0;
1177}
1178
Brian Norris7fc527f2014-07-29 14:34:14 -07001179/* Transmit a SKB fragment */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001180static int bcmgenet_xmit_frag(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001181 skb_frag_t *frag,
1182 u16 dma_desc_flags,
1183 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001184{
1185 struct bcmgenet_priv *priv = netdev_priv(dev);
1186 struct device *kdev = &priv->pdev->dev;
1187 struct enet_cb *tx_cb_ptr;
1188 dma_addr_t mapping;
1189 int ret;
1190
1191 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
1192
1193 if (unlikely(!tx_cb_ptr))
1194 BUG();
1195 tx_cb_ptr->skb = NULL;
1196
1197 mapping = skb_frag_dma_map(kdev, frag, 0,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001198 skb_frag_size(frag), DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001199 ret = dma_mapping_error(kdev, mapping);
1200 if (ret) {
Florian Fainelli44c8bc32014-11-19 10:29:56 -08001201 priv->mib.tx_dma_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001202 netif_err(priv, tx_err, dev, "%s: Tx DMA map failed\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001203 __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001204 return ret;
1205 }
1206
1207 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
1208 dma_unmap_len_set(tx_cb_ptr, dma_len, frag->size);
1209
1210 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001211 (frag->size << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
1212 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001213
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001214 return 0;
1215}
1216
1217/* Reallocate the SKB to put enough headroom in front of it and insert
1218 * the transmit checksum offsets in the descriptors
1219 */
Petri Gyntherbc233332014-10-01 11:30:01 -07001220static struct sk_buff *bcmgenet_put_tx_csum(struct net_device *dev,
1221 struct sk_buff *skb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001222{
1223 struct status_64 *status = NULL;
1224 struct sk_buff *new_skb;
1225 u16 offset;
1226 u8 ip_proto;
1227 u16 ip_ver;
1228 u32 tx_csum_info;
1229
1230 if (unlikely(skb_headroom(skb) < sizeof(*status))) {
1231 /* If 64 byte status block enabled, must make sure skb has
1232 * enough headroom for us to insert 64B status block.
1233 */
1234 new_skb = skb_realloc_headroom(skb, sizeof(*status));
1235 dev_kfree_skb(skb);
1236 if (!new_skb) {
1237 dev->stats.tx_errors++;
1238 dev->stats.tx_dropped++;
Petri Gyntherbc233332014-10-01 11:30:01 -07001239 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001240 }
1241 skb = new_skb;
1242 }
1243
1244 skb_push(skb, sizeof(*status));
1245 status = (struct status_64 *)skb->data;
1246
1247 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1248 ip_ver = htons(skb->protocol);
1249 switch (ip_ver) {
1250 case ETH_P_IP:
1251 ip_proto = ip_hdr(skb)->protocol;
1252 break;
1253 case ETH_P_IPV6:
1254 ip_proto = ipv6_hdr(skb)->nexthdr;
1255 break;
1256 default:
Petri Gyntherbc233332014-10-01 11:30:01 -07001257 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001258 }
1259
1260 offset = skb_checksum_start_offset(skb) - sizeof(*status);
1261 tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
1262 (offset + skb->csum_offset);
1263
1264 /* Set the length valid bit for TCP and UDP and just set
1265 * the special UDP flag for IPv4, else just set to 0.
1266 */
1267 if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
1268 tx_csum_info |= STATUS_TX_CSUM_LV;
1269 if (ip_proto == IPPROTO_UDP && ip_ver == ETH_P_IP)
1270 tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
Florian Fainelli8900ea572014-07-23 10:42:14 -07001271 } else {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001272 tx_csum_info = 0;
Florian Fainelli8900ea572014-07-23 10:42:14 -07001273 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001274
1275 status->tx_csum_info = tx_csum_info;
1276 }
1277
Petri Gyntherbc233332014-10-01 11:30:01 -07001278 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001279}
1280
1281static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
1282{
1283 struct bcmgenet_priv *priv = netdev_priv(dev);
1284 struct bcmgenet_tx_ring *ring = NULL;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001285 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001286 unsigned long flags = 0;
1287 int nr_frags, index;
1288 u16 dma_desc_flags;
1289 int ret;
1290 int i;
1291
1292 index = skb_get_queue_mapping(skb);
1293 /* Mapping strategy:
1294 * queue_mapping = 0, unclassified, packet xmited through ring16
1295 * queue_mapping = 1, goes to ring 0. (highest priority queue
1296 * queue_mapping = 2, goes to ring 1.
1297 * queue_mapping = 3, goes to ring 2.
1298 * queue_mapping = 4, goes to ring 3.
1299 */
1300 if (index == 0)
1301 index = DESC_INDEX;
1302 else
1303 index -= 1;
1304
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001305 nr_frags = skb_shinfo(skb)->nr_frags;
1306 ring = &priv->tx_rings[index];
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001307 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001308
1309 spin_lock_irqsave(&ring->lock, flags);
1310 if (ring->free_bds <= nr_frags + 1) {
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001311 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001312 netdev_err(dev, "%s: tx ring %d full when queue %d awake\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001313 __func__, index, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001314 ret = NETDEV_TX_BUSY;
1315 goto out;
1316 }
1317
Florian Fainelli474ea9c2014-07-22 11:01:52 -07001318 if (skb_padto(skb, ETH_ZLEN)) {
1319 ret = NETDEV_TX_OK;
1320 goto out;
1321 }
1322
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001323 /* set the SKB transmit checksum */
1324 if (priv->desc_64b_en) {
Petri Gyntherbc233332014-10-01 11:30:01 -07001325 skb = bcmgenet_put_tx_csum(dev, skb);
1326 if (!skb) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001327 ret = NETDEV_TX_OK;
1328 goto out;
1329 }
1330 }
1331
1332 dma_desc_flags = DMA_SOP;
1333 if (nr_frags == 0)
1334 dma_desc_flags |= DMA_EOP;
1335
1336 /* Transmit single SKB or head of fragment list */
1337 ret = bcmgenet_xmit_single(dev, skb, dma_desc_flags, ring);
1338 if (ret) {
1339 ret = NETDEV_TX_OK;
1340 goto out;
1341 }
1342
1343 /* xmit fragment */
1344 for (i = 0; i < nr_frags; i++) {
1345 ret = bcmgenet_xmit_frag(dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001346 &skb_shinfo(skb)->frags[i],
1347 (i == nr_frags - 1) ? DMA_EOP : 0,
1348 ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001349 if (ret) {
1350 ret = NETDEV_TX_OK;
1351 goto out;
1352 }
1353 }
1354
Florian Fainellid03825f2014-03-20 10:53:21 -07001355 skb_tx_timestamp(skb);
1356
Florian Fainelliae67bf02015-03-13 12:11:06 -07001357 /* Decrement total BD count and advance our write pointer */
1358 ring->free_bds -= nr_frags + 1;
1359 ring->prod_index += nr_frags + 1;
1360 ring->prod_index &= DMA_P_INDEX_MASK;
1361
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001362 if (ring->free_bds <= (MAX_SKB_FRAGS + 1))
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001363 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001364
Florian Fainelliddd0ca52015-03-13 12:11:07 -07001365 if (!skb->xmit_more || netif_xmit_stopped(txq))
1366 /* Packets are ready, update producer index */
1367 bcmgenet_tdma_ring_writel(priv, ring->index,
1368 ring->prod_index, TDMA_PROD_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001369out:
1370 spin_unlock_irqrestore(&ring->lock, flags);
1371
1372 return ret;
1373}
1374
Petri Gyntherd6707be2015-03-12 15:48:00 -07001375static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
1376 struct enet_cb *cb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001377{
1378 struct device *kdev = &priv->pdev->dev;
1379 struct sk_buff *skb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001380 struct sk_buff *rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001381 dma_addr_t mapping;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001382
Petri Gyntherd6707be2015-03-12 15:48:00 -07001383 /* Allocate a new Rx skb */
Florian Fainellic91b7f62014-07-23 10:42:12 -07001384 skb = netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT);
Petri Gyntherd6707be2015-03-12 15:48:00 -07001385 if (!skb) {
1386 priv->mib.alloc_rx_buff_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001387 netif_err(priv, rx_err, priv->dev,
Petri Gyntherd6707be2015-03-12 15:48:00 -07001388 "%s: Rx skb allocation failed\n", __func__);
1389 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001390 }
1391
Petri Gyntherd6707be2015-03-12 15:48:00 -07001392 /* DMA-map the new Rx skb */
1393 mapping = dma_map_single(kdev, skb->data, priv->rx_buf_len,
1394 DMA_FROM_DEVICE);
1395 if (dma_mapping_error(kdev, mapping)) {
1396 priv->mib.rx_dma_failed++;
1397 dev_kfree_skb_any(skb);
1398 netif_err(priv, rx_err, priv->dev,
1399 "%s: Rx skb DMA mapping failed\n", __func__);
1400 return NULL;
1401 }
1402
1403 /* Grab the current Rx skb from the ring and DMA-unmap it */
1404 rx_skb = cb->skb;
1405 if (likely(rx_skb))
1406 dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
1407 priv->rx_buf_len, DMA_FROM_DEVICE);
1408
1409 /* Put the new Rx skb on the ring */
1410 cb->skb = skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001411 dma_unmap_addr_set(cb, dma_addr, mapping);
Petri Gynther8ac467e2015-03-09 13:40:00 -07001412 dmadesc_set_addr(priv, cb->bd_addr, mapping);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001413
Petri Gyntherd6707be2015-03-12 15:48:00 -07001414 /* Return the current Rx skb to caller */
1415 return rx_skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001416}
1417
1418/* bcmgenet_desc_rx - descriptor based rx process.
1419 * this could be called from bottom half, or from NAPI polling method.
1420 */
Petri Gynther4055eae2015-03-25 12:35:16 -07001421static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001422 unsigned int budget)
1423{
Petri Gynther4055eae2015-03-25 12:35:16 -07001424 struct bcmgenet_priv *priv = ring->priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001425 struct net_device *dev = priv->dev;
1426 struct enet_cb *cb;
1427 struct sk_buff *skb;
1428 u32 dma_length_status;
1429 unsigned long dma_flag;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001430 int len;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001431 unsigned int rxpktprocessed = 0, rxpkttoprocess;
1432 unsigned int p_index;
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001433 unsigned int discards;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001434 unsigned int chksum_ok = 0;
1435
Petri Gynther4055eae2015-03-25 12:35:16 -07001436 p_index = bcmgenet_rdma_ring_readl(priv, ring->index, RDMA_PROD_INDEX);
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001437
1438 discards = (p_index >> DMA_P_INDEX_DISCARD_CNT_SHIFT) &
1439 DMA_P_INDEX_DISCARD_CNT_MASK;
1440 if (discards > ring->old_discards) {
1441 discards = discards - ring->old_discards;
1442 dev->stats.rx_missed_errors += discards;
1443 dev->stats.rx_errors += discards;
1444 ring->old_discards += discards;
1445
1446 /* Clear HW register when we reach 75% of maximum 0xFFFF */
1447 if (ring->old_discards >= 0xC000) {
1448 ring->old_discards = 0;
Petri Gynther4055eae2015-03-25 12:35:16 -07001449 bcmgenet_rdma_ring_writel(priv, ring->index, 0,
Petri Gyntherd26ea6c2015-03-10 15:55:00 -07001450 RDMA_PROD_INDEX);
1451 }
1452 }
1453
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001454 p_index &= DMA_P_INDEX_MASK;
1455
Petri Gynther8ac467e2015-03-09 13:40:00 -07001456 if (likely(p_index >= ring->c_index))
1457 rxpkttoprocess = p_index - ring->c_index;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001458 else
Petri Gynther8ac467e2015-03-09 13:40:00 -07001459 rxpkttoprocess = (DMA_C_INDEX_MASK + 1) - ring->c_index +
1460 p_index;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001461
1462 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001463 "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001464
1465 while ((rxpktprocessed < rxpkttoprocess) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001466 (rxpktprocessed < budget)) {
Petri Gynther8ac467e2015-03-09 13:40:00 -07001467 cb = &priv->rx_cbs[ring->read_ptr];
Petri Gyntherd6707be2015-03-12 15:48:00 -07001468 skb = bcmgenet_rx_refill(priv, cb);
Florian Fainellib629be52014-09-08 11:37:52 -07001469
Florian Fainellib629be52014-09-08 11:37:52 -07001470 if (unlikely(!skb)) {
1471 dev->stats.rx_dropped++;
1472 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001473 goto next;
Florian Fainellib629be52014-09-08 11:37:52 -07001474 }
1475
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001476 if (!priv->desc_64b_en) {
Florian Fainellic91b7f62014-07-23 10:42:12 -07001477 dma_length_status =
Petri Gynther8ac467e2015-03-09 13:40:00 -07001478 dmadesc_get_length_status(priv, cb->bd_addr);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001479 } else {
1480 struct status_64 *status;
Florian Fainelli164d4f22014-07-23 10:42:13 -07001481
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001482 status = (struct status_64 *)skb->data;
1483 dma_length_status = status->length_status;
1484 }
1485
1486 /* DMA flags and length are still valid no matter how
1487 * we got the Receive Status Vector (64B RSB or register)
1488 */
1489 dma_flag = dma_length_status & 0xffff;
1490 len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
1491
1492 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001493 "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
Petri Gynther8ac467e2015-03-09 13:40:00 -07001494 __func__, p_index, ring->c_index,
1495 ring->read_ptr, dma_length_status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001496
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001497 if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
1498 netif_err(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001499 "dropping fragmented packet!\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001500 dev->stats.rx_dropped++;
1501 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001502 dev_kfree_skb_any(skb);
1503 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001504 }
Petri Gyntherd6707be2015-03-12 15:48:00 -07001505
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001506 /* report errors */
1507 if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
1508 DMA_RX_OV |
1509 DMA_RX_NO |
1510 DMA_RX_LG |
1511 DMA_RX_RXER))) {
1512 netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001513 (unsigned int)dma_flag);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001514 if (dma_flag & DMA_RX_CRC_ERROR)
1515 dev->stats.rx_crc_errors++;
1516 if (dma_flag & DMA_RX_OV)
1517 dev->stats.rx_over_errors++;
1518 if (dma_flag & DMA_RX_NO)
1519 dev->stats.rx_frame_errors++;
1520 if (dma_flag & DMA_RX_LG)
1521 dev->stats.rx_length_errors++;
1522 dev->stats.rx_dropped++;
1523 dev->stats.rx_errors++;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001524 dev_kfree_skb_any(skb);
1525 goto next;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001526 } /* error packet */
1527
1528 chksum_ok = (dma_flag & priv->dma_rx_chk_bit) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001529 priv->desc_rxchk_en;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001530
1531 skb_put(skb, len);
1532 if (priv->desc_64b_en) {
1533 skb_pull(skb, 64);
1534 len -= 64;
1535 }
1536
1537 if (likely(chksum_ok))
1538 skb->ip_summed = CHECKSUM_UNNECESSARY;
1539
1540 /* remove hardware 2bytes added for IP alignment */
1541 skb_pull(skb, 2);
1542 len -= 2;
1543
1544 if (priv->crc_fwd_en) {
1545 skb_trim(skb, len - ETH_FCS_LEN);
1546 len -= ETH_FCS_LEN;
1547 }
1548
1549 /*Finish setting up the received SKB and send it to the kernel*/
1550 skb->protocol = eth_type_trans(skb, priv->dev);
1551 dev->stats.rx_packets++;
1552 dev->stats.rx_bytes += len;
1553 if (dma_flag & DMA_RX_MULT)
1554 dev->stats.multicast++;
1555
1556 /* Notify kernel */
Petri Gynther4055eae2015-03-25 12:35:16 -07001557 napi_gro_receive(&ring->napi, skb);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001558 netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
1559
Petri Gyntherd6707be2015-03-12 15:48:00 -07001560next:
Florian Fainellicf377d82014-10-10 10:51:52 -07001561 rxpktprocessed++;
Petri Gynther8ac467e2015-03-09 13:40:00 -07001562 if (likely(ring->read_ptr < ring->end_ptr))
1563 ring->read_ptr++;
1564 else
1565 ring->read_ptr = ring->cb_ptr;
1566
1567 ring->c_index = (ring->c_index + 1) & DMA_C_INDEX_MASK;
Petri Gynther4055eae2015-03-25 12:35:16 -07001568 bcmgenet_rdma_ring_writel(priv, ring->index, ring->c_index, RDMA_CONS_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001569 }
1570
1571 return rxpktprocessed;
1572}
1573
Petri Gynther3ab11332015-03-25 12:35:15 -07001574/* Rx NAPI polling method */
1575static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
1576{
Petri Gynther4055eae2015-03-25 12:35:16 -07001577 struct bcmgenet_rx_ring *ring = container_of(napi,
1578 struct bcmgenet_rx_ring, napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07001579 unsigned int work_done;
1580
Petri Gynther4055eae2015-03-25 12:35:16 -07001581 work_done = bcmgenet_desc_rx(ring, budget);
Petri Gynther3ab11332015-03-25 12:35:15 -07001582
1583 if (work_done < budget) {
1584 napi_complete(napi);
Petri Gynther4055eae2015-03-25 12:35:16 -07001585 ring->int_enable(ring);
Petri Gynther3ab11332015-03-25 12:35:15 -07001586 }
1587
1588 return work_done;
1589}
1590
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001591/* Assign skb to RX DMA descriptor. */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001592static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv,
1593 struct bcmgenet_rx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001594{
1595 struct enet_cb *cb;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001596 struct sk_buff *skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001597 int i;
1598
Petri Gynther8ac467e2015-03-09 13:40:00 -07001599 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001600
1601 /* loop here for each buffer needing assign */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001602 for (i = 0; i < ring->size; i++) {
1603 cb = ring->cbs + i;
Petri Gyntherd6707be2015-03-12 15:48:00 -07001604 skb = bcmgenet_rx_refill(priv, cb);
1605 if (skb)
1606 dev_kfree_skb_any(skb);
1607 if (!cb->skb)
1608 return -ENOMEM;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001609 }
1610
Petri Gyntherd6707be2015-03-12 15:48:00 -07001611 return 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001612}
1613
1614static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
1615{
1616 struct enet_cb *cb;
1617 int i;
1618
1619 for (i = 0; i < priv->num_rx_bds; i++) {
1620 cb = &priv->rx_cbs[i];
1621
1622 if (dma_unmap_addr(cb, dma_addr)) {
1623 dma_unmap_single(&priv->dev->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001624 dma_unmap_addr(cb, dma_addr),
1625 priv->rx_buf_len, DMA_FROM_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001626 dma_unmap_addr_set(cb, dma_addr, 0);
1627 }
1628
1629 if (cb->skb)
1630 bcmgenet_free_cb(cb);
1631 }
1632}
1633
Florian Fainellic91b7f62014-07-23 10:42:12 -07001634static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable)
Florian Fainellie29585b2014-07-21 15:29:20 -07001635{
1636 u32 reg;
1637
1638 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1639 if (enable)
1640 reg |= mask;
1641 else
1642 reg &= ~mask;
1643 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
1644
1645 /* UniMAC stops on a packet boundary, wait for a full-size packet
1646 * to be processed
1647 */
1648 if (enable == 0)
1649 usleep_range(1000, 2000);
1650}
1651
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001652static int reset_umac(struct bcmgenet_priv *priv)
1653{
1654 struct device *kdev = &priv->pdev->dev;
1655 unsigned int timeout = 0;
1656 u32 reg;
1657
1658 /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
1659 bcmgenet_rbuf_ctrl_set(priv, 0);
1660 udelay(10);
1661
1662 /* disable MAC while updating its registers */
1663 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1664
1665 /* issue soft reset, wait for it to complete */
1666 bcmgenet_umac_writel(priv, CMD_SW_RESET, UMAC_CMD);
1667 while (timeout++ < 1000) {
1668 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1669 if (!(reg & CMD_SW_RESET))
1670 return 0;
1671
1672 udelay(1);
1673 }
1674
1675 if (timeout == 1000) {
1676 dev_err(kdev,
Brian Norris7fc527f2014-07-29 14:34:14 -07001677 "timeout waiting for MAC to come out of reset\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001678 return -ETIMEDOUT;
1679 }
1680
1681 return 0;
1682}
1683
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001684static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
1685{
1686 /* Mask all interrupts.*/
1687 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1688 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
1689 bcmgenet_intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1690 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1691 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
1692 bcmgenet_intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1693}
1694
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001695static int init_umac(struct bcmgenet_priv *priv)
1696{
1697 struct device *kdev = &priv->pdev->dev;
1698 int ret;
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001699 u32 reg;
1700 u32 int0_enable = 0;
1701 u32 int1_enable = 0;
1702 int i;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001703
1704 dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
1705
1706 ret = reset_umac(priv);
1707 if (ret)
1708 return ret;
1709
1710 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1711 /* clear tx/rx counter */
1712 bcmgenet_umac_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001713 MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
1714 UMAC_MIB_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001715 bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
1716
1717 bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
1718
1719 /* init rx registers, enable ip header optimization */
1720 reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
1721 reg |= RBUF_ALIGN_2B;
1722 bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
1723
1724 if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
1725 bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
1726
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001727 bcmgenet_intr_disable(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001728
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001729 /* Enable Rx default queue 16 interrupts */
1730 int0_enable |= (UMAC_IRQ_RXDMA_BDONE | UMAC_IRQ_RXDMA_PDONE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001731
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001732 /* Enable Tx default queue 16 interrupts */
1733 int0_enable |= (UMAC_IRQ_TXDMA_BDONE | UMAC_IRQ_TXDMA_PDONE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001734
Brian Norris7fc527f2014-07-29 14:34:14 -07001735 /* Monitor cable plug/unplugged event for internal PHY */
Florian Fainelli8900ea572014-07-23 10:42:14 -07001736 if (phy_is_internal(priv->phydev)) {
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001737 int0_enable |= (UMAC_IRQ_LINK_DOWN | UMAC_IRQ_LINK_UP);
Florian Fainelli8900ea572014-07-23 10:42:14 -07001738 } else if (priv->ext_phy) {
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001739 int0_enable |= (UMAC_IRQ_LINK_DOWN | UMAC_IRQ_LINK_UP);
Florian Fainelli8900ea572014-07-23 10:42:14 -07001740 } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001741 reg = bcmgenet_bp_mc_get(priv);
1742 reg |= BIT(priv->hw_params->bp_in_en_shift);
1743
1744 /* bp_mask: back pressure mask */
1745 if (netif_is_multiqueue(priv->dev))
1746 reg |= priv->hw_params->bp_in_mask;
1747 else
1748 reg &= ~priv->hw_params->bp_in_mask;
1749 bcmgenet_bp_mc_set(priv, reg);
1750 }
1751
1752 /* Enable MDIO interrupts on GENET v3+ */
1753 if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001754 int0_enable |= (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001755
Petri Gynther4055eae2015-03-25 12:35:16 -07001756 /* Enable Rx priority queue interrupts */
1757 for (i = 0; i < priv->hw_params->rx_queues; ++i)
1758 int1_enable |= (1 << (UMAC_IRQ1_RX_INTR_SHIFT + i));
1759
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001760 /* Enable Tx priority queue interrupts */
1761 for (i = 0; i < priv->hw_params->tx_queues; ++i)
1762 int1_enable |= (1 << i);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001763
Petri Gyntherb2e97ec2015-03-25 12:35:12 -07001764 bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
1765 bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001766
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001767 /* Enable rx/tx engine.*/
1768 dev_dbg(kdev, "done init umac\n");
1769
1770 return 0;
1771}
1772
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001773/* Initialize a Tx ring along with corresponding hardware registers */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001774static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
1775 unsigned int index, unsigned int size,
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001776 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001777{
1778 struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
1779 u32 words_per_bd = WORDS_PER_BD(priv);
1780 u32 flow_period_val = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001781
1782 spin_lock_init(&ring->lock);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09001783 ring->priv = priv;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001784 ring->index = index;
1785 if (index == DESC_INDEX) {
1786 ring->queue = 0;
1787 ring->int_enable = bcmgenet_tx_ring16_int_enable;
1788 ring->int_disable = bcmgenet_tx_ring16_int_disable;
1789 } else {
1790 ring->queue = index + 1;
1791 ring->int_enable = bcmgenet_tx_ring_int_enable;
1792 ring->int_disable = bcmgenet_tx_ring_int_disable;
1793 }
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001794 ring->cbs = priv->tx_cbs + start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001795 ring->size = size;
Petri Gynther66d06752015-03-04 14:30:01 -08001796 ring->clean_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001797 ring->c_index = 0;
1798 ring->free_bds = size;
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001799 ring->write_ptr = start_ptr;
1800 ring->cb_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001801 ring->end_ptr = end_ptr - 1;
1802 ring->prod_index = 0;
1803
1804 /* Set flow period for ring != 16 */
1805 if (index != DESC_INDEX)
1806 flow_period_val = ENET_MAX_MTU_SIZE << 16;
1807
1808 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
1809 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
1810 bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
1811 /* Disable rate control for now */
1812 bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001813 TDMA_FLOW_PERIOD);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001814 bcmgenet_tdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001815 ((size << DMA_RING_SIZE_SHIFT) |
1816 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001817
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001818 /* Set start and end address, read and write pointers */
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001819 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001820 DMA_START_ADDR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001821 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001822 TDMA_READ_PTR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001823 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001824 TDMA_WRITE_PTR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001825 bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001826 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001827}
1828
1829/* Initialize a RDMA ring */
1830static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
Petri Gynther8ac467e2015-03-09 13:40:00 -07001831 unsigned int index, unsigned int size,
1832 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001833{
Petri Gynther8ac467e2015-03-09 13:40:00 -07001834 struct bcmgenet_rx_ring *ring = &priv->rx_rings[index];
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001835 u32 words_per_bd = WORDS_PER_BD(priv);
1836 int ret;
1837
Petri Gynther4055eae2015-03-25 12:35:16 -07001838 ring->priv = priv;
Petri Gynther8ac467e2015-03-09 13:40:00 -07001839 ring->index = index;
Petri Gynther4055eae2015-03-25 12:35:16 -07001840 if (index == DESC_INDEX) {
1841 ring->int_enable = bcmgenet_rx_ring16_int_enable;
1842 ring->int_disable = bcmgenet_rx_ring16_int_disable;
1843 } else {
1844 ring->int_enable = bcmgenet_rx_ring_int_enable;
1845 ring->int_disable = bcmgenet_rx_ring_int_disable;
1846 }
Petri Gynther8ac467e2015-03-09 13:40:00 -07001847 ring->cbs = priv->rx_cbs + start_ptr;
1848 ring->size = size;
1849 ring->c_index = 0;
1850 ring->read_ptr = start_ptr;
1851 ring->cb_ptr = start_ptr;
1852 ring->end_ptr = end_ptr - 1;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001853
Petri Gynther8ac467e2015-03-09 13:40:00 -07001854 ret = bcmgenet_alloc_rx_buffers(priv, ring);
1855 if (ret)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001856 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001857
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001858 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
1859 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
Petri Gynther6f5a2722015-03-06 13:45:00 -08001860 bcmgenet_rdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001861 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001862 ((size << DMA_RING_SIZE_SHIFT) |
1863 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001864 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001865 (DMA_FC_THRESH_LO <<
1866 DMA_XOFF_THRESHOLD_SHIFT) |
1867 DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
Petri Gynther6f5a2722015-03-06 13:45:00 -08001868
1869 /* Set start and end address, read and write pointers */
Petri Gynther8ac467e2015-03-09 13:40:00 -07001870 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
1871 DMA_START_ADDR);
1872 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
1873 RDMA_READ_PTR);
1874 bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
1875 RDMA_WRITE_PTR);
1876 bcmgenet_rdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Petri Gynther6f5a2722015-03-06 13:45:00 -08001877 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001878
1879 return ret;
1880}
1881
Petri Gynthere2aadb42015-03-25 12:35:14 -07001882static void bcmgenet_init_tx_napi(struct bcmgenet_priv *priv)
1883{
1884 unsigned int i;
1885 struct bcmgenet_tx_ring *ring;
1886
1887 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
1888 ring = &priv->tx_rings[i];
1889 netif_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll, 64);
1890 }
1891
1892 ring = &priv->tx_rings[DESC_INDEX];
1893 netif_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll, 64);
1894}
1895
1896static void bcmgenet_enable_tx_napi(struct bcmgenet_priv *priv)
1897{
1898 unsigned int i;
1899 struct bcmgenet_tx_ring *ring;
1900
1901 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
1902 ring = &priv->tx_rings[i];
1903 napi_enable(&ring->napi);
1904 }
1905
1906 ring = &priv->tx_rings[DESC_INDEX];
1907 napi_enable(&ring->napi);
1908}
1909
1910static void bcmgenet_disable_tx_napi(struct bcmgenet_priv *priv)
1911{
1912 unsigned int i;
1913 struct bcmgenet_tx_ring *ring;
1914
1915 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
1916 ring = &priv->tx_rings[i];
1917 napi_disable(&ring->napi);
1918 }
1919
1920 ring = &priv->tx_rings[DESC_INDEX];
1921 napi_disable(&ring->napi);
1922}
1923
1924static void bcmgenet_fini_tx_napi(struct bcmgenet_priv *priv)
1925{
1926 unsigned int i;
1927 struct bcmgenet_tx_ring *ring;
1928
1929 for (i = 0; i < priv->hw_params->tx_queues; ++i) {
1930 ring = &priv->tx_rings[i];
1931 netif_napi_del(&ring->napi);
1932 }
1933
1934 ring = &priv->tx_rings[DESC_INDEX];
1935 netif_napi_del(&ring->napi);
1936}
1937
Petri Gynther16c6d662015-02-23 11:00:45 -08001938/* Initialize Tx queues
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001939 *
Petri Gynther16c6d662015-02-23 11:00:45 -08001940 * Queues 0-3 are priority-based, each one has 32 descriptors,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001941 * with queue 0 being the highest priority queue.
1942 *
Petri Gynther16c6d662015-02-23 11:00:45 -08001943 * Queue 16 is the default Tx queue with
Petri Gynther51a966a2015-02-23 11:00:46 -08001944 * GENET_Q16_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors.
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001945 *
Petri Gynther16c6d662015-02-23 11:00:45 -08001946 * The transmit control block pool is then partitioned as follows:
1947 * - Tx queue 0 uses tx_cbs[0..31]
1948 * - Tx queue 1 uses tx_cbs[32..63]
1949 * - Tx queue 2 uses tx_cbs[64..95]
1950 * - Tx queue 3 uses tx_cbs[96..127]
1951 * - Tx queue 16 uses tx_cbs[128..255]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001952 */
Petri Gynther16c6d662015-02-23 11:00:45 -08001953static void bcmgenet_init_tx_queues(struct net_device *dev)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001954{
1955 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther16c6d662015-02-23 11:00:45 -08001956 u32 i, dma_enable;
1957 u32 dma_ctrl, ring_cfg;
Petri Gynther37742162014-10-07 09:30:01 -07001958 u32 dma_priority[3] = {0, 0, 0};
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001959
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001960 dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
1961 dma_enable = dma_ctrl & DMA_EN;
1962 dma_ctrl &= ~DMA_EN;
1963 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
1964
Petri Gynther16c6d662015-02-23 11:00:45 -08001965 dma_ctrl = 0;
1966 ring_cfg = 0;
1967
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001968 /* Enable strict priority arbiter mode */
1969 bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
1970
Petri Gynther16c6d662015-02-23 11:00:45 -08001971 /* Initialize Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001972 for (i = 0; i < priv->hw_params->tx_queues; i++) {
Petri Gynther51a966a2015-02-23 11:00:46 -08001973 bcmgenet_init_tx_ring(priv, i, priv->hw_params->tx_bds_per_q,
1974 i * priv->hw_params->tx_bds_per_q,
1975 (i + 1) * priv->hw_params->tx_bds_per_q);
Petri Gynther16c6d662015-02-23 11:00:45 -08001976 ring_cfg |= (1 << i);
1977 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07001978 dma_priority[DMA_PRIO_REG_INDEX(i)] |=
1979 ((GENET_Q0_PRIORITY + i) << DMA_PRIO_REG_SHIFT(i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001980 }
1981
Petri Gynther16c6d662015-02-23 11:00:45 -08001982 /* Initialize Tx default queue 16 */
Petri Gynther51a966a2015-02-23 11:00:46 -08001983 bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_Q16_TX_BD_CNT,
Petri Gynther16c6d662015-02-23 11:00:45 -08001984 priv->hw_params->tx_queues *
Petri Gynther51a966a2015-02-23 11:00:46 -08001985 priv->hw_params->tx_bds_per_q,
Petri Gynther16c6d662015-02-23 11:00:45 -08001986 TOTAL_DESC);
1987 ring_cfg |= (1 << DESC_INDEX);
1988 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07001989 dma_priority[DMA_PRIO_REG_INDEX(DESC_INDEX)] |=
1990 ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) <<
1991 DMA_PRIO_REG_SHIFT(DESC_INDEX));
Petri Gynther16c6d662015-02-23 11:00:45 -08001992
1993 /* Set Tx queue priorities */
Petri Gynther37742162014-10-07 09:30:01 -07001994 bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
1995 bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
1996 bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);
1997
Petri Gynthere2aadb42015-03-25 12:35:14 -07001998 /* Initialize Tx NAPI */
1999 bcmgenet_init_tx_napi(priv);
2000
Petri Gynther16c6d662015-02-23 11:00:45 -08002001 /* Enable Tx queues */
2002 bcmgenet_tdma_writel(priv, ring_cfg, DMA_RING_CFG);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002003
Petri Gynther16c6d662015-02-23 11:00:45 -08002004 /* Enable Tx DMA */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002005 if (dma_enable)
Petri Gynther16c6d662015-02-23 11:00:45 -08002006 dma_ctrl |= DMA_EN;
2007 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002008}
2009
Petri Gynther3ab11332015-03-25 12:35:15 -07002010static void bcmgenet_init_rx_napi(struct bcmgenet_priv *priv)
2011{
Petri Gynther4055eae2015-03-25 12:35:16 -07002012 unsigned int i;
2013 struct bcmgenet_rx_ring *ring;
2014
2015 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2016 ring = &priv->rx_rings[i];
2017 netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll, 64);
2018 }
2019
2020 ring = &priv->rx_rings[DESC_INDEX];
2021 netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll, 64);
Petri Gynther3ab11332015-03-25 12:35:15 -07002022}
2023
2024static void bcmgenet_enable_rx_napi(struct bcmgenet_priv *priv)
2025{
Petri Gynther4055eae2015-03-25 12:35:16 -07002026 unsigned int i;
2027 struct bcmgenet_rx_ring *ring;
2028
2029 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2030 ring = &priv->rx_rings[i];
2031 napi_enable(&ring->napi);
2032 }
2033
2034 ring = &priv->rx_rings[DESC_INDEX];
2035 napi_enable(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002036}
2037
2038static void bcmgenet_disable_rx_napi(struct bcmgenet_priv *priv)
2039{
Petri Gynther4055eae2015-03-25 12:35:16 -07002040 unsigned int i;
2041 struct bcmgenet_rx_ring *ring;
2042
2043 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2044 ring = &priv->rx_rings[i];
2045 napi_disable(&ring->napi);
2046 }
2047
2048 ring = &priv->rx_rings[DESC_INDEX];
2049 napi_disable(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002050}
2051
2052static void bcmgenet_fini_rx_napi(struct bcmgenet_priv *priv)
2053{
Petri Gynther4055eae2015-03-25 12:35:16 -07002054 unsigned int i;
2055 struct bcmgenet_rx_ring *ring;
2056
2057 for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2058 ring = &priv->rx_rings[i];
2059 netif_napi_del(&ring->napi);
2060 }
2061
2062 ring = &priv->rx_rings[DESC_INDEX];
2063 netif_napi_del(&ring->napi);
Petri Gynther3ab11332015-03-25 12:35:15 -07002064}
2065
Petri Gynther8ac467e2015-03-09 13:40:00 -07002066/* Initialize Rx queues
2067 *
2068 * Queues 0-15 are priority queues. Hardware Filtering Block (HFB) can be
2069 * used to direct traffic to these queues.
2070 *
2071 * Queue 16 is the default Rx queue with GENET_Q16_RX_BD_CNT descriptors.
2072 */
2073static int bcmgenet_init_rx_queues(struct net_device *dev)
2074{
2075 struct bcmgenet_priv *priv = netdev_priv(dev);
2076 u32 i;
2077 u32 dma_enable;
2078 u32 dma_ctrl;
2079 u32 ring_cfg;
2080 int ret;
2081
2082 dma_ctrl = bcmgenet_rdma_readl(priv, DMA_CTRL);
2083 dma_enable = dma_ctrl & DMA_EN;
2084 dma_ctrl &= ~DMA_EN;
2085 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2086
2087 dma_ctrl = 0;
2088 ring_cfg = 0;
2089
2090 /* Initialize Rx priority queues */
2091 for (i = 0; i < priv->hw_params->rx_queues; i++) {
2092 ret = bcmgenet_init_rx_ring(priv, i,
2093 priv->hw_params->rx_bds_per_q,
2094 i * priv->hw_params->rx_bds_per_q,
2095 (i + 1) *
2096 priv->hw_params->rx_bds_per_q);
2097 if (ret)
2098 return ret;
2099
2100 ring_cfg |= (1 << i);
2101 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2102 }
2103
2104 /* Initialize Rx default queue 16 */
2105 ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, GENET_Q16_RX_BD_CNT,
2106 priv->hw_params->rx_queues *
2107 priv->hw_params->rx_bds_per_q,
2108 TOTAL_DESC);
2109 if (ret)
2110 return ret;
2111
2112 ring_cfg |= (1 << DESC_INDEX);
2113 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
2114
Petri Gynther3ab11332015-03-25 12:35:15 -07002115 /* Initialize Rx NAPI */
2116 bcmgenet_init_rx_napi(priv);
2117
Petri Gynther8ac467e2015-03-09 13:40:00 -07002118 /* Enable rings */
2119 bcmgenet_rdma_writel(priv, ring_cfg, DMA_RING_CFG);
2120
2121 /* Configure ring as descriptor ring and re-enable DMA if enabled */
2122 if (dma_enable)
2123 dma_ctrl |= DMA_EN;
2124 bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2125
2126 return 0;
2127}
2128
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002129static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
2130{
2131 int ret = 0;
2132 int timeout = 0;
2133 u32 reg;
2134
2135 /* Disable TDMA to stop add more frames in TX DMA */
2136 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2137 reg &= ~DMA_EN;
2138 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2139
2140 /* Check TDMA status register to confirm TDMA is disabled */
2141 while (timeout++ < DMA_TIMEOUT_VAL) {
2142 reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
2143 if (reg & DMA_DISABLED)
2144 break;
2145
2146 udelay(1);
2147 }
2148
2149 if (timeout == DMA_TIMEOUT_VAL) {
2150 netdev_warn(priv->dev, "Timed out while disabling TX DMA\n");
2151 ret = -ETIMEDOUT;
2152 }
2153
2154 /* Wait 10ms for packet drain in both tx and rx dma */
2155 usleep_range(10000, 20000);
2156
2157 /* Disable RDMA */
2158 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2159 reg &= ~DMA_EN;
2160 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2161
2162 timeout = 0;
2163 /* Check RDMA status register to confirm RDMA is disabled */
2164 while (timeout++ < DMA_TIMEOUT_VAL) {
2165 reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
2166 if (reg & DMA_DISABLED)
2167 break;
2168
2169 udelay(1);
2170 }
2171
2172 if (timeout == DMA_TIMEOUT_VAL) {
2173 netdev_warn(priv->dev, "Timed out while disabling RX DMA\n");
2174 ret = -ETIMEDOUT;
2175 }
2176
2177 return ret;
2178}
2179
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002180static void __bcmgenet_fini_dma(struct bcmgenet_priv *priv)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002181{
2182 int i;
2183
2184 /* disable DMA */
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07002185 bcmgenet_dma_teardown(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002186
2187 for (i = 0; i < priv->num_tx_bds; i++) {
2188 if (priv->tx_cbs[i].skb != NULL) {
2189 dev_kfree_skb(priv->tx_cbs[i].skb);
2190 priv->tx_cbs[i].skb = NULL;
2191 }
2192 }
2193
2194 bcmgenet_free_rx_buffers(priv);
2195 kfree(priv->rx_cbs);
2196 kfree(priv->tx_cbs);
2197}
2198
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002199static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
2200{
Petri Gynther3ab11332015-03-25 12:35:15 -07002201 bcmgenet_fini_rx_napi(priv);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002202 bcmgenet_fini_tx_napi(priv);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002203
2204 __bcmgenet_fini_dma(priv);
2205}
2206
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002207/* init_edma: Initialize DMA control register */
2208static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
2209{
2210 int ret;
Petri Gynther014012a2015-02-23 11:00:45 -08002211 unsigned int i;
2212 struct enet_cb *cb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002213
Petri Gynther6f5a2722015-03-06 13:45:00 -08002214 netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002215
Petri Gynther6f5a2722015-03-06 13:45:00 -08002216 /* Initialize common Rx ring structures */
2217 priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
2218 priv->num_rx_bds = TOTAL_DESC;
2219 priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct enet_cb),
2220 GFP_KERNEL);
2221 if (!priv->rx_cbs)
2222 return -ENOMEM;
2223
2224 for (i = 0; i < priv->num_rx_bds; i++) {
2225 cb = priv->rx_cbs + i;
2226 cb->bd_addr = priv->rx_bds + i * DMA_DESC_SIZE;
2227 }
2228
Brian Norris7fc527f2014-07-29 14:34:14 -07002229 /* Initialize common TX ring structures */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002230 priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
2231 priv->num_tx_bds = TOTAL_DESC;
Florian Fainellic489be02014-07-23 10:42:15 -07002232 priv->tx_cbs = kcalloc(priv->num_tx_bds, sizeof(struct enet_cb),
Florian Fainellic91b7f62014-07-23 10:42:12 -07002233 GFP_KERNEL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002234 if (!priv->tx_cbs) {
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002235 kfree(priv->rx_cbs);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002236 return -ENOMEM;
2237 }
2238
Petri Gynther014012a2015-02-23 11:00:45 -08002239 for (i = 0; i < priv->num_tx_bds; i++) {
2240 cb = priv->tx_cbs + i;
2241 cb->bd_addr = priv->tx_bds + i * DMA_DESC_SIZE;
2242 }
2243
Petri Gyntherebbd96f2015-03-25 12:35:11 -07002244 /* Init rDma */
2245 bcmgenet_rdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
2246
2247 /* Initialize Rx queues */
2248 ret = bcmgenet_init_rx_queues(priv->dev);
2249 if (ret) {
2250 netdev_err(priv->dev, "failed to initialize Rx queues\n");
2251 bcmgenet_free_rx_buffers(priv);
2252 kfree(priv->rx_cbs);
2253 kfree(priv->tx_cbs);
2254 return ret;
2255 }
2256
2257 /* Init tDma */
2258 bcmgenet_tdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
2259
Petri Gynther16c6d662015-02-23 11:00:45 -08002260 /* Initialize Tx queues */
2261 bcmgenet_init_tx_queues(priv->dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002262
2263 return 0;
2264}
2265
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002266/* Interrupt bottom half */
2267static void bcmgenet_irq_task(struct work_struct *work)
2268{
2269 struct bcmgenet_priv *priv = container_of(
2270 work, struct bcmgenet_priv, bcmgenet_irq_work);
2271
2272 netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
2273
Florian Fainelli8fdb0e02014-07-21 15:29:26 -07002274 if (priv->irq0_stat & UMAC_IRQ_MPD_R) {
2275 priv->irq0_stat &= ~UMAC_IRQ_MPD_R;
2276 netif_dbg(priv, wol, priv->dev,
2277 "magic packet detected, waking up\n");
2278 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
2279 }
2280
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002281 /* Link UP/DOWN event */
2282 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07002283 (priv->irq0_stat & (UMAC_IRQ_LINK_UP|UMAC_IRQ_LINK_DOWN))) {
Florian Fainelli80d8e962014-02-24 16:56:11 -08002284 phy_mac_interrupt(priv->phydev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002285 priv->irq0_stat & UMAC_IRQ_LINK_UP);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002286 priv->irq0_stat &= ~(UMAC_IRQ_LINK_UP|UMAC_IRQ_LINK_DOWN);
2287 }
2288}
2289
Petri Gynther4055eae2015-03-25 12:35:16 -07002290/* bcmgenet_isr1: handle Rx and Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002291static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
2292{
2293 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002294 struct bcmgenet_rx_ring *rx_ring;
2295 struct bcmgenet_tx_ring *tx_ring;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002296 unsigned int index;
2297
2298 /* Save irq status for bottom-half processing. */
2299 priv->irq1_stat =
2300 bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002301 ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002302
Brian Norris7fc527f2014-07-29 14:34:14 -07002303 /* clear interrupts */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002304 bcmgenet_intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
2305
2306 netif_dbg(priv, intr, priv->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002307 "%s: IRQ=0x%x\n", __func__, priv->irq1_stat);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002308
Petri Gynther4055eae2015-03-25 12:35:16 -07002309 /* Check Rx priority queue interrupts */
2310 for (index = 0; index < priv->hw_params->rx_queues; index++) {
2311 if (!(priv->irq1_stat & BIT(UMAC_IRQ1_RX_INTR_SHIFT + index)))
2312 continue;
2313
2314 rx_ring = &priv->rx_rings[index];
2315
2316 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2317 rx_ring->int_disable(rx_ring);
2318 __napi_schedule(&rx_ring->napi);
2319 }
2320 }
2321
2322 /* Check Tx priority queue interrupts */
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002323 for (index = 0; index < priv->hw_params->tx_queues; index++) {
2324 if (!(priv->irq1_stat & BIT(index)))
2325 continue;
2326
Petri Gynther4055eae2015-03-25 12:35:16 -07002327 tx_ring = &priv->tx_rings[index];
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002328
Petri Gynther4055eae2015-03-25 12:35:16 -07002329 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2330 tx_ring->int_disable(tx_ring);
2331 __napi_schedule(&tx_ring->napi);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002332 }
2333 }
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002334
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002335 return IRQ_HANDLED;
2336}
2337
Petri Gynther4055eae2015-03-25 12:35:16 -07002338/* bcmgenet_isr0: handle Rx and Tx default queues + other stuff */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002339static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
2340{
2341 struct bcmgenet_priv *priv = dev_id;
Petri Gynther4055eae2015-03-25 12:35:16 -07002342 struct bcmgenet_rx_ring *rx_ring;
2343 struct bcmgenet_tx_ring *tx_ring;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002344
2345 /* Save irq status for bottom-half processing. */
2346 priv->irq0_stat =
2347 bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
2348 ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
Petri Gynther4055eae2015-03-25 12:35:16 -07002349
Brian Norris7fc527f2014-07-29 14:34:14 -07002350 /* clear interrupts */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002351 bcmgenet_intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
2352
2353 netif_dbg(priv, intr, priv->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002354 "IRQ=0x%x\n", priv->irq0_stat);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002355
2356 if (priv->irq0_stat & (UMAC_IRQ_RXDMA_BDONE | UMAC_IRQ_RXDMA_PDONE)) {
Petri Gynther4055eae2015-03-25 12:35:16 -07002357 rx_ring = &priv->rx_rings[DESC_INDEX];
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002358
Petri Gynther4055eae2015-03-25 12:35:16 -07002359 if (likely(napi_schedule_prep(&rx_ring->napi))) {
2360 rx_ring->int_disable(rx_ring);
2361 __napi_schedule(&rx_ring->napi);
Jaedon Shin4092e6a2015-02-28 11:48:26 +09002362 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002363 }
Petri Gynther4055eae2015-03-25 12:35:16 -07002364
2365 if (priv->irq0_stat & (UMAC_IRQ_TXDMA_BDONE | UMAC_IRQ_TXDMA_PDONE)) {
2366 tx_ring = &priv->tx_rings[DESC_INDEX];
2367
2368 if (likely(napi_schedule_prep(&tx_ring->napi))) {
2369 tx_ring->int_disable(tx_ring);
2370 __napi_schedule(&tx_ring->napi);
2371 }
2372 }
2373
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002374 if (priv->irq0_stat & (UMAC_IRQ_PHY_DET_R |
2375 UMAC_IRQ_PHY_DET_F |
2376 UMAC_IRQ_LINK_UP |
2377 UMAC_IRQ_LINK_DOWN |
2378 UMAC_IRQ_HFB_SM |
2379 UMAC_IRQ_HFB_MM |
2380 UMAC_IRQ_MPD_R)) {
2381 /* all other interested interrupts handled in bottom half */
2382 schedule_work(&priv->bcmgenet_irq_work);
2383 }
2384
2385 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07002386 priv->irq0_stat & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002387 priv->irq0_stat &= ~(UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
2388 wake_up(&priv->wq);
2389 }
2390
2391 return IRQ_HANDLED;
2392}
2393
Florian Fainelli85620562014-07-21 15:29:23 -07002394static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
2395{
2396 struct bcmgenet_priv *priv = dev_id;
2397
2398 pm_wakeup_event(&priv->pdev->dev, 0);
2399
2400 return IRQ_HANDLED;
2401}
2402
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002403static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
2404{
2405 u32 reg;
2406
2407 reg = bcmgenet_rbuf_ctrl_get(priv);
2408 reg |= BIT(1);
2409 bcmgenet_rbuf_ctrl_set(priv, reg);
2410 udelay(10);
2411
2412 reg &= ~BIT(1);
2413 bcmgenet_rbuf_ctrl_set(priv, reg);
2414 udelay(10);
2415}
2416
2417static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002418 unsigned char *addr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002419{
2420 bcmgenet_umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
2421 (addr[2] << 8) | addr[3], UMAC_MAC0);
2422 bcmgenet_umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
2423}
2424
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002425/* Returns a reusable dma control register value */
2426static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
2427{
2428 u32 reg;
2429 u32 dma_ctrl;
2430
2431 /* disable DMA */
2432 dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
2433 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2434 reg &= ~dma_ctrl;
2435 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2436
2437 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2438 reg &= ~dma_ctrl;
2439 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2440
2441 bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
2442 udelay(10);
2443 bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
2444
2445 return dma_ctrl;
2446}
2447
2448static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
2449{
2450 u32 reg;
2451
2452 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2453 reg |= dma_ctrl;
2454 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2455
2456 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2457 reg |= dma_ctrl;
2458 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2459}
2460
Petri Gynther0034de42015-03-13 14:45:00 -07002461static bool bcmgenet_hfb_is_filter_enabled(struct bcmgenet_priv *priv,
2462 u32 f_index)
2463{
2464 u32 offset;
2465 u32 reg;
2466
2467 offset = HFB_FLT_ENABLE_V3PLUS + (f_index < 32) * sizeof(u32);
2468 reg = bcmgenet_hfb_reg_readl(priv, offset);
2469 return !!(reg & (1 << (f_index % 32)));
2470}
2471
2472static void bcmgenet_hfb_enable_filter(struct bcmgenet_priv *priv, u32 f_index)
2473{
2474 u32 offset;
2475 u32 reg;
2476
2477 offset = HFB_FLT_ENABLE_V3PLUS + (f_index < 32) * sizeof(u32);
2478 reg = bcmgenet_hfb_reg_readl(priv, offset);
2479 reg |= (1 << (f_index % 32));
2480 bcmgenet_hfb_reg_writel(priv, reg, offset);
2481}
2482
2483static void bcmgenet_hfb_set_filter_rx_queue_mapping(struct bcmgenet_priv *priv,
2484 u32 f_index, u32 rx_queue)
2485{
2486 u32 offset;
2487 u32 reg;
2488
2489 offset = f_index / 8;
2490 reg = bcmgenet_rdma_readl(priv, DMA_INDEX2RING_0 + offset);
2491 reg &= ~(0xF << (4 * (f_index % 8)));
2492 reg |= ((rx_queue & 0xF) << (4 * (f_index % 8)));
2493 bcmgenet_rdma_writel(priv, reg, DMA_INDEX2RING_0 + offset);
2494}
2495
2496static void bcmgenet_hfb_set_filter_length(struct bcmgenet_priv *priv,
2497 u32 f_index, u32 f_length)
2498{
2499 u32 offset;
2500 u32 reg;
2501
2502 offset = HFB_FLT_LEN_V3PLUS +
2503 ((priv->hw_params->hfb_filter_cnt - 1 - f_index) / 4) *
2504 sizeof(u32);
2505 reg = bcmgenet_hfb_reg_readl(priv, offset);
2506 reg &= ~(0xFF << (8 * (f_index % 4)));
2507 reg |= ((f_length & 0xFF) << (8 * (f_index % 4)));
2508 bcmgenet_hfb_reg_writel(priv, reg, offset);
2509}
2510
2511static int bcmgenet_hfb_find_unused_filter(struct bcmgenet_priv *priv)
2512{
2513 u32 f_index;
2514
2515 for (f_index = 0; f_index < priv->hw_params->hfb_filter_cnt; f_index++)
2516 if (!bcmgenet_hfb_is_filter_enabled(priv, f_index))
2517 return f_index;
2518
2519 return -ENOMEM;
2520}
2521
2522/* bcmgenet_hfb_add_filter
2523 *
2524 * Add new filter to Hardware Filter Block to match and direct Rx traffic to
2525 * desired Rx queue.
2526 *
2527 * f_data is an array of unsigned 32-bit integers where each 32-bit integer
2528 * provides filter data for 2 bytes (4 nibbles) of Rx frame:
2529 *
2530 * bits 31:20 - unused
2531 * bit 19 - nibble 0 match enable
2532 * bit 18 - nibble 1 match enable
2533 * bit 17 - nibble 2 match enable
2534 * bit 16 - nibble 3 match enable
2535 * bits 15:12 - nibble 0 data
2536 * bits 11:8 - nibble 1 data
2537 * bits 7:4 - nibble 2 data
2538 * bits 3:0 - nibble 3 data
2539 *
2540 * Example:
2541 * In order to match:
2542 * - Ethernet frame type = 0x0800 (IP)
2543 * - IP version field = 4
2544 * - IP protocol field = 0x11 (UDP)
2545 *
2546 * The following filter is needed:
2547 * u32 hfb_filter_ipv4_udp[] = {
2548 * Rx frame offset 0x00: 0x00000000, 0x00000000, 0x00000000, 0x00000000,
2549 * Rx frame offset 0x08: 0x00000000, 0x00000000, 0x000F0800, 0x00084000,
2550 * Rx frame offset 0x10: 0x00000000, 0x00000000, 0x00000000, 0x00030011,
2551 * };
2552 *
2553 * To add the filter to HFB and direct the traffic to Rx queue 0, call:
2554 * bcmgenet_hfb_add_filter(priv, hfb_filter_ipv4_udp,
2555 * ARRAY_SIZE(hfb_filter_ipv4_udp), 0);
2556 */
2557int bcmgenet_hfb_add_filter(struct bcmgenet_priv *priv, u32 *f_data,
2558 u32 f_length, u32 rx_queue)
2559{
2560 int f_index;
2561 u32 i;
2562
2563 f_index = bcmgenet_hfb_find_unused_filter(priv);
2564 if (f_index < 0)
2565 return -ENOMEM;
2566
2567 if (f_length > priv->hw_params->hfb_filter_size)
2568 return -EINVAL;
2569
2570 for (i = 0; i < f_length; i++)
2571 bcmgenet_hfb_writel(priv, f_data[i],
2572 (f_index * priv->hw_params->hfb_filter_size + i) *
2573 sizeof(u32));
2574
2575 bcmgenet_hfb_set_filter_length(priv, f_index, 2 * f_length);
2576 bcmgenet_hfb_set_filter_rx_queue_mapping(priv, f_index, rx_queue);
2577 bcmgenet_hfb_enable_filter(priv, f_index);
2578 bcmgenet_hfb_reg_writel(priv, 0x1, HFB_CTRL);
2579
2580 return 0;
2581}
2582
2583/* bcmgenet_hfb_clear
2584 *
2585 * Clear Hardware Filter Block and disable all filtering.
2586 */
2587static void bcmgenet_hfb_clear(struct bcmgenet_priv *priv)
2588{
2589 u32 i;
2590
2591 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_CTRL);
2592 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS);
2593 bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS + 4);
2594
2595 for (i = DMA_INDEX2RING_0; i <= DMA_INDEX2RING_7; i++)
2596 bcmgenet_rdma_writel(priv, 0x0, i);
2597
2598 for (i = 0; i < (priv->hw_params->hfb_filter_cnt / 4); i++)
2599 bcmgenet_hfb_reg_writel(priv, 0x0,
2600 HFB_FLT_LEN_V3PLUS + i * sizeof(u32));
2601
2602 for (i = 0; i < priv->hw_params->hfb_filter_cnt *
2603 priv->hw_params->hfb_filter_size; i++)
2604 bcmgenet_hfb_writel(priv, 0x0, i * sizeof(u32));
2605}
2606
2607static void bcmgenet_hfb_init(struct bcmgenet_priv *priv)
2608{
2609 if (GENET_IS_V1(priv) || GENET_IS_V2(priv))
2610 return;
2611
2612 bcmgenet_hfb_clear(priv);
2613}
2614
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002615static void bcmgenet_netif_start(struct net_device *dev)
2616{
2617 struct bcmgenet_priv *priv = netdev_priv(dev);
2618
2619 /* Start the network engine */
Petri Gynther3ab11332015-03-25 12:35:15 -07002620 bcmgenet_enable_rx_napi(priv);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002621 bcmgenet_enable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002622
2623 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
2624
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002625 netif_tx_start_all_queues(dev);
2626
2627 phy_start(priv->phydev);
2628}
2629
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002630static int bcmgenet_open(struct net_device *dev)
2631{
2632 struct bcmgenet_priv *priv = netdev_priv(dev);
2633 unsigned long dma_ctrl;
2634 u32 reg;
2635 int ret;
2636
2637 netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
2638
2639 /* Turn on the clock */
2640 if (!IS_ERR(priv->clk))
2641 clk_prepare_enable(priv->clk);
2642
Florian Fainellia642c4f2015-03-23 15:09:56 -07002643 /* If this is an internal GPHY, power it back on now, before UniMAC is
2644 * brought out of reset as absolutely no UniMAC activity is allowed
2645 */
2646 if (phy_is_internal(priv->phydev))
2647 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
2648
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002649 /* take MAC out of reset */
2650 bcmgenet_umac_reset(priv);
2651
2652 ret = init_umac(priv);
2653 if (ret)
2654 goto err_clk_disable;
2655
2656 /* disable ethernet MAC while updating its registers */
Florian Fainellie29585b2014-07-21 15:29:20 -07002657 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002658
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002659 /* Make sure we reflect the value of CRC_CMD_FWD */
2660 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
2661 priv->crc_fwd_en = !!(reg & CMD_CRC_FWD);
2662
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002663 bcmgenet_set_hw_addr(priv, dev->dev_addr);
2664
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002665 if (phy_is_internal(priv->phydev)) {
2666 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
2667 reg |= EXT_ENERGY_DET_MASK;
2668 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
2669 }
2670
2671 /* Disable RX/TX DMA and flush TX queues */
2672 dma_ctrl = bcmgenet_dma_disable(priv);
2673
2674 /* Reinitialize TDMA and RDMA and SW housekeeping */
2675 ret = bcmgenet_init_dma(priv);
2676 if (ret) {
2677 netdev_err(dev, "failed to initialize DMA\n");
2678 goto err_fini_dma;
2679 }
2680
2681 /* Always enable ring 16 - descriptor ring */
2682 bcmgenet_enable_dma(priv, dma_ctrl);
2683
Petri Gynther0034de42015-03-13 14:45:00 -07002684 /* HFB init */
2685 bcmgenet_hfb_init(priv);
2686
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002687 ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002688 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002689 if (ret < 0) {
2690 netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
2691 goto err_fini_dma;
2692 }
2693
2694 ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002695 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002696 if (ret < 0) {
2697 netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
2698 goto err_irq0;
2699 }
2700
Florian Fainellidbd479d2014-11-10 18:06:21 -08002701 /* Re-configure the port multiplexer towards the PHY device */
2702 bcmgenet_mii_config(priv->dev, false);
2703
Florian Fainellic96e7312014-11-10 18:06:20 -08002704 phy_connect_direct(dev, priv->phydev, bcmgenet_mii_setup,
2705 priv->phy_interface);
2706
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002707 bcmgenet_netif_start(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002708
2709 return 0;
2710
2711err_irq0:
2712 free_irq(priv->irq0, dev);
2713err_fini_dma:
2714 bcmgenet_fini_dma(priv);
2715err_clk_disable:
2716 if (!IS_ERR(priv->clk))
2717 clk_disable_unprepare(priv->clk);
2718 return ret;
2719}
2720
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002721static void bcmgenet_netif_stop(struct net_device *dev)
2722{
2723 struct bcmgenet_priv *priv = netdev_priv(dev);
2724
2725 netif_tx_stop_all_queues(dev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002726 phy_stop(priv->phydev);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002727 bcmgenet_intr_disable(priv);
Petri Gynther3ab11332015-03-25 12:35:15 -07002728 bcmgenet_disable_rx_napi(priv);
Petri Gynthere2aadb42015-03-25 12:35:14 -07002729 bcmgenet_disable_tx_napi(priv);
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002730
2731 /* Wait for pending work items to complete. Since interrupts are
2732 * disabled no new work will be scheduled.
2733 */
2734 cancel_work_sync(&priv->bcmgenet_irq_work);
Florian Fainellicc013fb2014-08-11 14:50:43 -07002735
Florian Fainellicc013fb2014-08-11 14:50:43 -07002736 priv->old_link = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002737 priv->old_speed = -1;
Florian Fainellicc013fb2014-08-11 14:50:43 -07002738 priv->old_duplex = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002739 priv->old_pause = -1;
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002740}
2741
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002742static int bcmgenet_close(struct net_device *dev)
2743{
2744 struct bcmgenet_priv *priv = netdev_priv(dev);
2745 int ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002746
2747 netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
2748
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002749 bcmgenet_netif_stop(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002750
Florian Fainellic96e7312014-11-10 18:06:20 -08002751 /* Really kill the PHY state machine and disconnect from it */
2752 phy_disconnect(priv->phydev);
2753
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002754 /* Disable MAC receive */
Florian Fainellie29585b2014-07-21 15:29:20 -07002755 umac_enable_set(priv, CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002756
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002757 ret = bcmgenet_dma_teardown(priv);
2758 if (ret)
2759 return ret;
2760
2761 /* Disable MAC transmit. TX DMA disabled have to done before this */
Florian Fainellie29585b2014-07-21 15:29:20 -07002762 umac_enable_set(priv, CMD_TX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002763
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002764 /* tx reclaim */
2765 bcmgenet_tx_reclaim_all(dev);
2766 bcmgenet_fini_dma(priv);
2767
2768 free_irq(priv->irq0, priv);
2769 free_irq(priv->irq1, priv);
2770
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002771 if (phy_is_internal(priv->phydev))
Florian Fainellica8cf342015-03-23 15:09:51 -07002772 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002773
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002774 if (!IS_ERR(priv->clk))
2775 clk_disable_unprepare(priv->clk);
2776
Florian Fainellica8cf342015-03-23 15:09:51 -07002777 return ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002778}
2779
2780static void bcmgenet_timeout(struct net_device *dev)
2781{
2782 struct bcmgenet_priv *priv = netdev_priv(dev);
2783
2784 netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
2785
2786 dev->trans_start = jiffies;
2787
2788 dev->stats.tx_errors++;
2789
2790 netif_tx_wake_all_queues(dev);
2791}
2792
2793#define MAX_MC_COUNT 16
2794
2795static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
2796 unsigned char *addr,
2797 int *i,
2798 int *mc)
2799{
2800 u32 reg;
2801
Florian Fainellic91b7f62014-07-23 10:42:12 -07002802 bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
2803 UMAC_MDF_ADDR + (*i * 4));
2804 bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
2805 addr[4] << 8 | addr[5],
2806 UMAC_MDF_ADDR + ((*i + 1) * 4));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002807 reg = bcmgenet_umac_readl(priv, UMAC_MDF_CTRL);
2808 reg |= (1 << (MAX_MC_COUNT - *mc));
2809 bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
2810 *i += 2;
2811 (*mc)++;
2812}
2813
2814static void bcmgenet_set_rx_mode(struct net_device *dev)
2815{
2816 struct bcmgenet_priv *priv = netdev_priv(dev);
2817 struct netdev_hw_addr *ha;
2818 int i, mc;
2819 u32 reg;
2820
2821 netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
2822
Brian Norris7fc527f2014-07-29 14:34:14 -07002823 /* Promiscuous mode */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002824 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
2825 if (dev->flags & IFF_PROMISC) {
2826 reg |= CMD_PROMISC;
2827 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
2828 bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
2829 return;
2830 } else {
2831 reg &= ~CMD_PROMISC;
2832 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
2833 }
2834
2835 /* UniMac doesn't support ALLMULTI */
2836 if (dev->flags & IFF_ALLMULTI) {
2837 netdev_warn(dev, "ALLMULTI is not supported\n");
2838 return;
2839 }
2840
2841 /* update MDF filter */
2842 i = 0;
2843 mc = 0;
2844 /* Broadcast */
2845 bcmgenet_set_mdf_addr(priv, dev->broadcast, &i, &mc);
2846 /* my own address.*/
2847 bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i, &mc);
2848 /* Unicast list*/
2849 if (netdev_uc_count(dev) > (MAX_MC_COUNT - mc))
2850 return;
2851
2852 if (!netdev_uc_empty(dev))
2853 netdev_for_each_uc_addr(ha, dev)
2854 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
2855 /* Multicast */
2856 if (netdev_mc_empty(dev) || netdev_mc_count(dev) >= (MAX_MC_COUNT - mc))
2857 return;
2858
2859 netdev_for_each_mc_addr(ha, dev)
2860 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
2861}
2862
2863/* Set the hardware MAC address. */
2864static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
2865{
2866 struct sockaddr *addr = p;
2867
2868 /* Setting the MAC address at the hardware level is not possible
2869 * without disabling the UniMAC RX/TX enable bits.
2870 */
2871 if (netif_running(dev))
2872 return -EBUSY;
2873
2874 ether_addr_copy(dev->dev_addr, addr->sa_data);
2875
2876 return 0;
2877}
2878
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002879static const struct net_device_ops bcmgenet_netdev_ops = {
2880 .ndo_open = bcmgenet_open,
2881 .ndo_stop = bcmgenet_close,
2882 .ndo_start_xmit = bcmgenet_xmit,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002883 .ndo_tx_timeout = bcmgenet_timeout,
2884 .ndo_set_rx_mode = bcmgenet_set_rx_mode,
2885 .ndo_set_mac_address = bcmgenet_set_mac_addr,
2886 .ndo_do_ioctl = bcmgenet_ioctl,
2887 .ndo_set_features = bcmgenet_set_features,
2888};
2889
2890/* Array of GENET hardware parameters/characteristics */
2891static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
2892 [GENET_V1] = {
2893 .tx_queues = 0,
Petri Gynther51a966a2015-02-23 11:00:46 -08002894 .tx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002895 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08002896 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002897 .bp_in_en_shift = 16,
2898 .bp_in_mask = 0xffff,
2899 .hfb_filter_cnt = 16,
2900 .qtag_mask = 0x1F,
2901 .hfb_offset = 0x1000,
2902 .rdma_offset = 0x2000,
2903 .tdma_offset = 0x3000,
2904 .words_per_bd = 2,
2905 },
2906 [GENET_V2] = {
2907 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08002908 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08002909 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08002910 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002911 .bp_in_en_shift = 16,
2912 .bp_in_mask = 0xffff,
2913 .hfb_filter_cnt = 16,
2914 .qtag_mask = 0x1F,
2915 .tbuf_offset = 0x0600,
2916 .hfb_offset = 0x1000,
2917 .hfb_reg_offset = 0x2000,
2918 .rdma_offset = 0x3000,
2919 .tdma_offset = 0x4000,
2920 .words_per_bd = 2,
2921 .flags = GENET_HAS_EXT,
2922 },
2923 [GENET_V3] = {
2924 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08002925 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08002926 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08002927 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002928 .bp_in_en_shift = 17,
2929 .bp_in_mask = 0x1ffff,
2930 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07002931 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002932 .qtag_mask = 0x3F,
2933 .tbuf_offset = 0x0600,
2934 .hfb_offset = 0x8000,
2935 .hfb_reg_offset = 0xfc00,
2936 .rdma_offset = 0x10000,
2937 .tdma_offset = 0x11000,
2938 .words_per_bd = 2,
2939 .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR,
2940 },
2941 [GENET_V4] = {
2942 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08002943 .tx_bds_per_q = 32,
Petri Gynther7e906e02015-03-05 17:40:10 -08002944 .rx_queues = 0,
Petri Gynther3feafa02015-03-05 17:40:14 -08002945 .rx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002946 .bp_in_en_shift = 17,
2947 .bp_in_mask = 0x1ffff,
2948 .hfb_filter_cnt = 48,
Petri Gynther0034de42015-03-13 14:45:00 -07002949 .hfb_filter_size = 128,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002950 .qtag_mask = 0x3F,
2951 .tbuf_offset = 0x0600,
2952 .hfb_offset = 0x8000,
2953 .hfb_reg_offset = 0xfc00,
2954 .rdma_offset = 0x2000,
2955 .tdma_offset = 0x4000,
2956 .words_per_bd = 3,
2957 .flags = GENET_HAS_40BITS | GENET_HAS_EXT | GENET_HAS_MDIO_INTR,
2958 },
2959};
2960
2961/* Infer hardware parameters from the detected GENET version */
2962static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
2963{
2964 struct bcmgenet_hw_params *params;
2965 u32 reg;
2966 u8 major;
Florian Fainellib04a2f52014-12-03 09:56:59 -08002967 u16 gphy_rev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002968
2969 if (GENET_IS_V4(priv)) {
2970 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
2971 genet_dma_ring_regs = genet_dma_ring_regs_v4;
2972 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
2973 priv->version = GENET_V4;
2974 } else if (GENET_IS_V3(priv)) {
2975 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
2976 genet_dma_ring_regs = genet_dma_ring_regs_v123;
2977 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
2978 priv->version = GENET_V3;
2979 } else if (GENET_IS_V2(priv)) {
2980 bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
2981 genet_dma_ring_regs = genet_dma_ring_regs_v123;
2982 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
2983 priv->version = GENET_V2;
2984 } else if (GENET_IS_V1(priv)) {
2985 bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
2986 genet_dma_ring_regs = genet_dma_ring_regs_v123;
2987 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
2988 priv->version = GENET_V1;
2989 }
2990
2991 /* enum genet_version starts at 1 */
2992 priv->hw_params = &bcmgenet_hw_params[priv->version];
2993 params = priv->hw_params;
2994
2995 /* Read GENET HW version */
2996 reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
2997 major = (reg >> 24 & 0x0f);
2998 if (major == 5)
2999 major = 4;
3000 else if (major == 0)
3001 major = 1;
3002 if (major != priv->version) {
3003 dev_err(&priv->pdev->dev,
3004 "GENET version mismatch, got: %d, configured for: %d\n",
3005 major, priv->version);
3006 }
3007
3008 /* Print the GENET core version */
3009 dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
Florian Fainellic91b7f62014-07-23 10:42:12 -07003010 major, (reg >> 16) & 0x0f, reg & 0xffff);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003011
Florian Fainelli487320c2014-09-19 13:07:53 -07003012 /* Store the integrated PHY revision for the MDIO probing function
3013 * to pass this information to the PHY driver. The PHY driver expects
3014 * to find the PHY major revision in bits 15:8 while the GENET register
3015 * stores that information in bits 7:0, account for that.
Florian Fainellib04a2f52014-12-03 09:56:59 -08003016 *
3017 * On newer chips, starting with PHY revision G0, a new scheme is
3018 * deployed similar to the Starfighter 2 switch with GPHY major
3019 * revision in bits 15:8 and patch level in bits 7:0. Major revision 0
3020 * is reserved as well as special value 0x01ff, we have a small
3021 * heuristic to check for the new GPHY revision and re-arrange things
3022 * so the GPHY driver is happy.
Florian Fainelli487320c2014-09-19 13:07:53 -07003023 */
Florian Fainellib04a2f52014-12-03 09:56:59 -08003024 gphy_rev = reg & 0xffff;
3025
3026 /* This is the good old scheme, just GPHY major, no minor nor patch */
3027 if ((gphy_rev & 0xf0) != 0)
3028 priv->gphy_rev = gphy_rev << 8;
3029
3030 /* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */
3031 else if ((gphy_rev & 0xff00) != 0)
3032 priv->gphy_rev = gphy_rev;
3033
3034 /* This is reserved so should require special treatment */
3035 else if (gphy_rev == 0 || gphy_rev == 0x01ff) {
3036 pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev);
3037 return;
3038 }
Florian Fainelli487320c2014-09-19 13:07:53 -07003039
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003040#ifdef CONFIG_PHYS_ADDR_T_64BIT
3041 if (!(params->flags & GENET_HAS_40BITS))
3042 pr_warn("GENET does not support 40-bits PA\n");
3043#endif
3044
3045 pr_debug("Configuration for version: %d\n"
Petri Gynther3feafa02015-03-05 17:40:14 -08003046 "TXq: %1d, TXqBDs: %1d, RXq: %1d, RXqBDs: %1d\n"
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003047 "BP << en: %2d, BP msk: 0x%05x\n"
3048 "HFB count: %2d, QTAQ msk: 0x%05x\n"
3049 "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
3050 "RDMA: 0x%05x, TDMA: 0x%05x\n"
3051 "Words/BD: %d\n",
3052 priv->version,
Petri Gynther51a966a2015-02-23 11:00:46 -08003053 params->tx_queues, params->tx_bds_per_q,
Petri Gynther3feafa02015-03-05 17:40:14 -08003054 params->rx_queues, params->rx_bds_per_q,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003055 params->bp_in_en_shift, params->bp_in_mask,
3056 params->hfb_filter_cnt, params->qtag_mask,
3057 params->tbuf_offset, params->hfb_offset,
3058 params->hfb_reg_offset,
3059 params->rdma_offset, params->tdma_offset,
3060 params->words_per_bd);
3061}
3062
3063static const struct of_device_id bcmgenet_match[] = {
3064 { .compatible = "brcm,genet-v1", .data = (void *)GENET_V1 },
3065 { .compatible = "brcm,genet-v2", .data = (void *)GENET_V2 },
3066 { .compatible = "brcm,genet-v3", .data = (void *)GENET_V3 },
3067 { .compatible = "brcm,genet-v4", .data = (void *)GENET_V4 },
3068 { },
3069};
3070
3071static int bcmgenet_probe(struct platform_device *pdev)
3072{
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003073 struct bcmgenet_platform_data *pd = pdev->dev.platform_data;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003074 struct device_node *dn = pdev->dev.of_node;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003075 const struct of_device_id *of_id = NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003076 struct bcmgenet_priv *priv;
3077 struct net_device *dev;
3078 const void *macaddr;
3079 struct resource *r;
3080 int err = -EIO;
3081
Petri Gynther3feafee2015-03-05 17:40:12 -08003082 /* Up to GENET_MAX_MQ_CNT + 1 TX queues and RX queues */
3083 dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1,
3084 GENET_MAX_MQ_CNT + 1);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003085 if (!dev) {
3086 dev_err(&pdev->dev, "can't allocate net device\n");
3087 return -ENOMEM;
3088 }
3089
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003090 if (dn) {
3091 of_id = of_match_node(bcmgenet_match, dn);
3092 if (!of_id)
3093 return -EINVAL;
3094 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003095
3096 priv = netdev_priv(dev);
3097 priv->irq0 = platform_get_irq(pdev, 0);
3098 priv->irq1 = platform_get_irq(pdev, 1);
Florian Fainelli85620562014-07-21 15:29:23 -07003099 priv->wol_irq = platform_get_irq(pdev, 2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003100 if (!priv->irq0 || !priv->irq1) {
3101 dev_err(&pdev->dev, "can't find IRQs\n");
3102 err = -EINVAL;
3103 goto err;
3104 }
3105
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003106 if (dn) {
3107 macaddr = of_get_mac_address(dn);
3108 if (!macaddr) {
3109 dev_err(&pdev->dev, "can't find MAC address\n");
3110 err = -EINVAL;
3111 goto err;
3112 }
3113 } else {
3114 macaddr = pd->mac_address;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003115 }
3116
3117 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam5343a102014-02-24 00:47:24 -03003118 priv->base = devm_ioremap_resource(&pdev->dev, r);
3119 if (IS_ERR(priv->base)) {
3120 err = PTR_ERR(priv->base);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003121 goto err;
3122 }
3123
3124 SET_NETDEV_DEV(dev, &pdev->dev);
3125 dev_set_drvdata(&pdev->dev, dev);
3126 ether_addr_copy(dev->dev_addr, macaddr);
3127 dev->watchdog_timeo = 2 * HZ;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003128 dev->ethtool_ops = &bcmgenet_ethtool_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003129 dev->netdev_ops = &bcmgenet_netdev_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003130
3131 priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
3132
3133 /* Set hardware features */
3134 dev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM |
3135 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
3136
Florian Fainelli85620562014-07-21 15:29:23 -07003137 /* Request the WOL interrupt and advertise suspend if available */
3138 priv->wol_irq_disabled = true;
3139 err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0,
3140 dev->name, priv);
3141 if (!err)
3142 device_set_wakeup_capable(&pdev->dev, 1);
3143
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003144 /* Set the needed headroom to account for any possible
3145 * features enabling/disabling at runtime
3146 */
3147 dev->needed_headroom += 64;
3148
3149 netdev_boot_setup_check(dev);
3150
3151 priv->dev = dev;
3152 priv->pdev = pdev;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08003153 if (of_id)
3154 priv->version = (enum bcmgenet_version)of_id->data;
3155 else
3156 priv->version = pd->genet_version;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003157
Florian Fainellie4a60a92014-08-11 14:50:42 -07003158 priv->clk = devm_clk_get(&priv->pdev->dev, "enet");
3159 if (IS_ERR(priv->clk))
3160 dev_warn(&priv->pdev->dev, "failed to get enet clock\n");
3161
3162 if (!IS_ERR(priv->clk))
3163 clk_prepare_enable(priv->clk);
3164
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003165 bcmgenet_set_hw_params(priv);
3166
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003167 /* Mii wait queue */
3168 init_waitqueue_head(&priv->wq);
3169 /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
3170 priv->rx_buf_len = RX_BUF_LENGTH;
3171 INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
3172
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003173 priv->clk_wol = devm_clk_get(&priv->pdev->dev, "enet-wol");
3174 if (IS_ERR(priv->clk_wol))
3175 dev_warn(&priv->pdev->dev, "failed to get enet-wol clock\n");
3176
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003177 priv->clk_eee = devm_clk_get(&priv->pdev->dev, "enet-eee");
3178 if (IS_ERR(priv->clk_eee)) {
3179 dev_warn(&priv->pdev->dev, "failed to get enet-eee clock\n");
3180 priv->clk_eee = NULL;
3181 }
3182
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003183 err = reset_umac(priv);
3184 if (err)
3185 goto err_clk_disable;
3186
3187 err = bcmgenet_mii_init(dev);
3188 if (err)
3189 goto err_clk_disable;
3190
3191 /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
3192 * just the ring 16 descriptor based TX
3193 */
3194 netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
3195 netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
3196
Florian Fainelli219575e2014-06-26 10:26:21 -07003197 /* libphy will determine the link state */
3198 netif_carrier_off(dev);
3199
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003200 /* Turn off the main clock, WOL clock is handled separately */
3201 if (!IS_ERR(priv->clk))
3202 clk_disable_unprepare(priv->clk);
3203
Florian Fainelli0f50ce92014-06-26 10:26:20 -07003204 err = register_netdev(dev);
3205 if (err)
3206 goto err;
3207
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003208 return err;
3209
3210err_clk_disable:
3211 if (!IS_ERR(priv->clk))
3212 clk_disable_unprepare(priv->clk);
3213err:
3214 free_netdev(dev);
3215 return err;
3216}
3217
3218static int bcmgenet_remove(struct platform_device *pdev)
3219{
3220 struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
3221
3222 dev_set_drvdata(&pdev->dev, NULL);
3223 unregister_netdev(priv->dev);
3224 bcmgenet_mii_exit(priv->dev);
3225 free_netdev(priv->dev);
3226
3227 return 0;
3228}
3229
Florian Fainellib6e978e2014-07-21 15:29:22 -07003230#ifdef CONFIG_PM_SLEEP
3231static int bcmgenet_suspend(struct device *d)
3232{
3233 struct net_device *dev = dev_get_drvdata(d);
3234 struct bcmgenet_priv *priv = netdev_priv(dev);
3235 int ret;
3236
3237 if (!netif_running(dev))
3238 return 0;
3239
3240 bcmgenet_netif_stop(dev);
3241
Florian Fainellicc013fb2014-08-11 14:50:43 -07003242 phy_suspend(priv->phydev);
3243
Florian Fainellib6e978e2014-07-21 15:29:22 -07003244 netif_device_detach(dev);
3245
3246 /* Disable MAC receive */
3247 umac_enable_set(priv, CMD_RX_EN, false);
3248
3249 ret = bcmgenet_dma_teardown(priv);
3250 if (ret)
3251 return ret;
3252
3253 /* Disable MAC transmit. TX DMA disabled have to done before this */
3254 umac_enable_set(priv, CMD_TX_EN, false);
3255
3256 /* tx reclaim */
3257 bcmgenet_tx_reclaim_all(dev);
3258 bcmgenet_fini_dma(priv);
3259
Florian Fainelli8c90db72014-07-21 15:29:28 -07003260 /* Prepare the device for Wake-on-LAN and switch to the slow clock */
3261 if (device_may_wakeup(d) && priv->wolopts) {
Florian Fainellica8cf342015-03-23 15:09:51 -07003262 ret = bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003263 clk_prepare_enable(priv->clk_wol);
Florian Fainellia6f31f52015-03-23 15:09:57 -07003264 } else if (phy_is_internal(priv->phydev)) {
3265 ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003266 }
3267
Florian Fainellib6e978e2014-07-21 15:29:22 -07003268 /* Turn off the clocks */
3269 clk_disable_unprepare(priv->clk);
3270
Florian Fainellica8cf342015-03-23 15:09:51 -07003271 return ret;
Florian Fainellib6e978e2014-07-21 15:29:22 -07003272}
3273
3274static int bcmgenet_resume(struct device *d)
3275{
3276 struct net_device *dev = dev_get_drvdata(d);
3277 struct bcmgenet_priv *priv = netdev_priv(dev);
3278 unsigned long dma_ctrl;
3279 int ret;
3280 u32 reg;
3281
3282 if (!netif_running(dev))
3283 return 0;
3284
3285 /* Turn on the clock */
3286 ret = clk_prepare_enable(priv->clk);
3287 if (ret)
3288 return ret;
3289
Florian Fainellia6f31f52015-03-23 15:09:57 -07003290 /* If this is an internal GPHY, power it back on now, before UniMAC is
3291 * brought out of reset as absolutely no UniMAC activity is allowed
3292 */
3293 if (phy_is_internal(priv->phydev))
3294 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3295
Florian Fainellib6e978e2014-07-21 15:29:22 -07003296 bcmgenet_umac_reset(priv);
3297
3298 ret = init_umac(priv);
3299 if (ret)
3300 goto out_clk_disable;
3301
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02003302 /* From WOL-enabled suspend, switch to regular clock */
3303 if (priv->wolopts)
3304 clk_disable_unprepare(priv->clk_wol);
3305
3306 phy_init_hw(priv->phydev);
3307 /* Speed settings must be restored */
Florian Fainellidbd479d2014-11-10 18:06:21 -08003308 bcmgenet_mii_config(priv->dev, false);
Florian Fainelli8c90db72014-07-21 15:29:28 -07003309
Florian Fainellib6e978e2014-07-21 15:29:22 -07003310 /* disable ethernet MAC while updating its registers */
3311 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
3312
3313 bcmgenet_set_hw_addr(priv, dev->dev_addr);
3314
3315 if (phy_is_internal(priv->phydev)) {
3316 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
3317 reg |= EXT_ENERGY_DET_MASK;
3318 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
3319 }
3320
Florian Fainelli98bb7392014-08-11 14:50:45 -07003321 if (priv->wolopts)
3322 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
3323
Florian Fainellib6e978e2014-07-21 15:29:22 -07003324 /* Disable RX/TX DMA and flush TX queues */
3325 dma_ctrl = bcmgenet_dma_disable(priv);
3326
3327 /* Reinitialize TDMA and RDMA and SW housekeeping */
3328 ret = bcmgenet_init_dma(priv);
3329 if (ret) {
3330 netdev_err(dev, "failed to initialize DMA\n");
3331 goto out_clk_disable;
3332 }
3333
3334 /* Always enable ring 16 - descriptor ring */
3335 bcmgenet_enable_dma(priv, dma_ctrl);
3336
3337 netif_device_attach(dev);
3338
Florian Fainellicc013fb2014-08-11 14:50:43 -07003339 phy_resume(priv->phydev);
3340
Florian Fainelli6ef398e2014-11-25 21:16:35 -08003341 if (priv->eee.eee_enabled)
3342 bcmgenet_eee_enable_set(dev, true);
3343
Florian Fainellib6e978e2014-07-21 15:29:22 -07003344 bcmgenet_netif_start(dev);
3345
3346 return 0;
3347
3348out_clk_disable:
3349 clk_disable_unprepare(priv->clk);
3350 return ret;
3351}
3352#endif /* CONFIG_PM_SLEEP */
3353
3354static SIMPLE_DEV_PM_OPS(bcmgenet_pm_ops, bcmgenet_suspend, bcmgenet_resume);
3355
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003356static struct platform_driver bcmgenet_driver = {
3357 .probe = bcmgenet_probe,
3358 .remove = bcmgenet_remove,
3359 .driver = {
3360 .name = "bcmgenet",
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003361 .of_match_table = bcmgenet_match,
Florian Fainellib6e978e2014-07-21 15:29:22 -07003362 .pm = &bcmgenet_pm_ops,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08003363 },
3364};
3365module_platform_driver(bcmgenet_driver);
3366
3367MODULE_AUTHOR("Broadcom Corporation");
3368MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
3369MODULE_ALIAS("platform:bcmgenet");
3370MODULE_LICENSE("GPL");