blob: 51300532ec26306079f75a216dbb98b07b0ce3ed [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 Gynther51a966a2015-02-23 11:00:46 -080057#define GENET_Q16_TX_BD_CNT \
58 (TOTAL_DESC - priv->hw_params->tx_queues * priv->hw_params->tx_bds_per_q)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080059
60#define RX_BUF_LENGTH 2048
61#define SKB_ALIGNMENT 32
62
63/* Tx/Rx DMA register offset, skip 256 descriptors */
64#define WORDS_PER_BD(p) (p->hw_params->words_per_bd)
65#define DMA_DESC_SIZE (WORDS_PER_BD(priv) * sizeof(u32))
66
67#define GENET_TDMA_REG_OFF (priv->hw_params->tdma_offset + \
68 TOTAL_DESC * DMA_DESC_SIZE)
69
70#define GENET_RDMA_REG_OFF (priv->hw_params->rdma_offset + \
71 TOTAL_DESC * DMA_DESC_SIZE)
72
73static inline void dmadesc_set_length_status(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -070074 void __iomem *d, u32 value)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080075{
76 __raw_writel(value, d + DMA_DESC_LENGTH_STATUS);
77}
78
79static inline u32 dmadesc_get_length_status(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -070080 void __iomem *d)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080081{
82 return __raw_readl(d + DMA_DESC_LENGTH_STATUS);
83}
84
85static inline void dmadesc_set_addr(struct bcmgenet_priv *priv,
86 void __iomem *d,
87 dma_addr_t addr)
88{
89 __raw_writel(lower_32_bits(addr), d + DMA_DESC_ADDRESS_LO);
90
91 /* Register writes to GISB bus can take couple hundred nanoseconds
92 * and are done for each packet, save these expensive writes unless
Brian Norris7fc527f2014-07-29 14:34:14 -070093 * the platform is explicitly configured for 64-bits/LPAE.
Florian Fainelli1c1008c2014-02-13 16:08:47 -080094 */
95#ifdef CONFIG_PHYS_ADDR_T_64BIT
96 if (priv->hw_params->flags & GENET_HAS_40BITS)
97 __raw_writel(upper_32_bits(addr), d + DMA_DESC_ADDRESS_HI);
98#endif
99}
100
101/* Combined address + length/status setter */
102static inline void dmadesc_set(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700103 void __iomem *d, dma_addr_t addr, u32 val)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800104{
105 dmadesc_set_length_status(priv, d, val);
106 dmadesc_set_addr(priv, d, addr);
107}
108
109static inline dma_addr_t dmadesc_get_addr(struct bcmgenet_priv *priv,
110 void __iomem *d)
111{
112 dma_addr_t addr;
113
114 addr = __raw_readl(d + DMA_DESC_ADDRESS_LO);
115
116 /* Register writes to GISB bus can take couple hundred nanoseconds
117 * and are done for each packet, save these expensive writes unless
Brian Norris7fc527f2014-07-29 14:34:14 -0700118 * the platform is explicitly configured for 64-bits/LPAE.
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800119 */
120#ifdef CONFIG_PHYS_ADDR_T_64BIT
121 if (priv->hw_params->flags & GENET_HAS_40BITS)
122 addr |= (u64)__raw_readl(d + DMA_DESC_ADDRESS_HI) << 32;
123#endif
124 return addr;
125}
126
127#define GENET_VER_FMT "%1d.%1d EPHY: 0x%04x"
128
129#define GENET_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
130 NETIF_MSG_LINK)
131
132static inline u32 bcmgenet_rbuf_ctrl_get(struct bcmgenet_priv *priv)
133{
134 if (GENET_IS_V1(priv))
135 return bcmgenet_rbuf_readl(priv, RBUF_FLUSH_CTRL_V1);
136 else
137 return bcmgenet_sys_readl(priv, SYS_RBUF_FLUSH_CTRL);
138}
139
140static inline void bcmgenet_rbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
141{
142 if (GENET_IS_V1(priv))
143 bcmgenet_rbuf_writel(priv, val, RBUF_FLUSH_CTRL_V1);
144 else
145 bcmgenet_sys_writel(priv, val, SYS_RBUF_FLUSH_CTRL);
146}
147
148/* These macros are defined to deal with register map change
149 * between GENET1.1 and GENET2. Only those currently being used
150 * by driver are defined.
151 */
152static inline u32 bcmgenet_tbuf_ctrl_get(struct bcmgenet_priv *priv)
153{
154 if (GENET_IS_V1(priv))
155 return bcmgenet_rbuf_readl(priv, TBUF_CTRL_V1);
156 else
157 return __raw_readl(priv->base +
158 priv->hw_params->tbuf_offset + TBUF_CTRL);
159}
160
161static inline void bcmgenet_tbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
162{
163 if (GENET_IS_V1(priv))
164 bcmgenet_rbuf_writel(priv, val, TBUF_CTRL_V1);
165 else
166 __raw_writel(val, priv->base +
167 priv->hw_params->tbuf_offset + TBUF_CTRL);
168}
169
170static inline u32 bcmgenet_bp_mc_get(struct bcmgenet_priv *priv)
171{
172 if (GENET_IS_V1(priv))
173 return bcmgenet_rbuf_readl(priv, TBUF_BP_MC_V1);
174 else
175 return __raw_readl(priv->base +
176 priv->hw_params->tbuf_offset + TBUF_BP_MC);
177}
178
179static inline void bcmgenet_bp_mc_set(struct bcmgenet_priv *priv, u32 val)
180{
181 if (GENET_IS_V1(priv))
182 bcmgenet_rbuf_writel(priv, val, TBUF_BP_MC_V1);
183 else
184 __raw_writel(val, priv->base +
185 priv->hw_params->tbuf_offset + TBUF_BP_MC);
186}
187
188/* RX/TX DMA register accessors */
189enum dma_reg {
190 DMA_RING_CFG = 0,
191 DMA_CTRL,
192 DMA_STATUS,
193 DMA_SCB_BURST_SIZE,
194 DMA_ARB_CTRL,
Petri Gynther37742162014-10-07 09:30:01 -0700195 DMA_PRIORITY_0,
196 DMA_PRIORITY_1,
197 DMA_PRIORITY_2,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800198};
199
200static const u8 bcmgenet_dma_regs_v3plus[] = {
201 [DMA_RING_CFG] = 0x00,
202 [DMA_CTRL] = 0x04,
203 [DMA_STATUS] = 0x08,
204 [DMA_SCB_BURST_SIZE] = 0x0C,
205 [DMA_ARB_CTRL] = 0x2C,
Petri Gynther37742162014-10-07 09:30:01 -0700206 [DMA_PRIORITY_0] = 0x30,
207 [DMA_PRIORITY_1] = 0x34,
208 [DMA_PRIORITY_2] = 0x38,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800209};
210
211static const u8 bcmgenet_dma_regs_v2[] = {
212 [DMA_RING_CFG] = 0x00,
213 [DMA_CTRL] = 0x04,
214 [DMA_STATUS] = 0x08,
215 [DMA_SCB_BURST_SIZE] = 0x0C,
216 [DMA_ARB_CTRL] = 0x30,
Petri Gynther37742162014-10-07 09:30:01 -0700217 [DMA_PRIORITY_0] = 0x34,
218 [DMA_PRIORITY_1] = 0x38,
219 [DMA_PRIORITY_2] = 0x3C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800220};
221
222static const u8 bcmgenet_dma_regs_v1[] = {
223 [DMA_CTRL] = 0x00,
224 [DMA_STATUS] = 0x04,
225 [DMA_SCB_BURST_SIZE] = 0x0C,
226 [DMA_ARB_CTRL] = 0x30,
Petri Gynther37742162014-10-07 09:30:01 -0700227 [DMA_PRIORITY_0] = 0x34,
228 [DMA_PRIORITY_1] = 0x38,
229 [DMA_PRIORITY_2] = 0x3C,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800230};
231
232/* Set at runtime once bcmgenet version is known */
233static const u8 *bcmgenet_dma_regs;
234
235static inline struct bcmgenet_priv *dev_to_priv(struct device *dev)
236{
237 return netdev_priv(dev_get_drvdata(dev));
238}
239
240static inline u32 bcmgenet_tdma_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700241 enum dma_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800242{
243 return __raw_readl(priv->base + GENET_TDMA_REG_OFF +
244 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
245}
246
247static inline void bcmgenet_tdma_writel(struct bcmgenet_priv *priv,
248 u32 val, enum dma_reg r)
249{
250 __raw_writel(val, priv->base + GENET_TDMA_REG_OFF +
251 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
252}
253
254static inline u32 bcmgenet_rdma_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700255 enum dma_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800256{
257 return __raw_readl(priv->base + GENET_RDMA_REG_OFF +
258 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
259}
260
261static inline void bcmgenet_rdma_writel(struct bcmgenet_priv *priv,
262 u32 val, enum dma_reg r)
263{
264 __raw_writel(val, priv->base + GENET_RDMA_REG_OFF +
265 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
266}
267
268/* RDMA/TDMA ring registers and accessors
269 * we merge the common fields and just prefix with T/D the registers
270 * having different meaning depending on the direction
271 */
272enum dma_ring_reg {
273 TDMA_READ_PTR = 0,
274 RDMA_WRITE_PTR = TDMA_READ_PTR,
275 TDMA_READ_PTR_HI,
276 RDMA_WRITE_PTR_HI = TDMA_READ_PTR_HI,
277 TDMA_CONS_INDEX,
278 RDMA_PROD_INDEX = TDMA_CONS_INDEX,
279 TDMA_PROD_INDEX,
280 RDMA_CONS_INDEX = TDMA_PROD_INDEX,
281 DMA_RING_BUF_SIZE,
282 DMA_START_ADDR,
283 DMA_START_ADDR_HI,
284 DMA_END_ADDR,
285 DMA_END_ADDR_HI,
286 DMA_MBUF_DONE_THRESH,
287 TDMA_FLOW_PERIOD,
288 RDMA_XON_XOFF_THRESH = TDMA_FLOW_PERIOD,
289 TDMA_WRITE_PTR,
290 RDMA_READ_PTR = TDMA_WRITE_PTR,
291 TDMA_WRITE_PTR_HI,
292 RDMA_READ_PTR_HI = TDMA_WRITE_PTR_HI
293};
294
295/* GENET v4 supports 40-bits pointer addressing
296 * for obvious reasons the LO and HI word parts
297 * are contiguous, but this offsets the other
298 * registers.
299 */
300static const u8 genet_dma_ring_regs_v4[] = {
301 [TDMA_READ_PTR] = 0x00,
302 [TDMA_READ_PTR_HI] = 0x04,
303 [TDMA_CONS_INDEX] = 0x08,
304 [TDMA_PROD_INDEX] = 0x0C,
305 [DMA_RING_BUF_SIZE] = 0x10,
306 [DMA_START_ADDR] = 0x14,
307 [DMA_START_ADDR_HI] = 0x18,
308 [DMA_END_ADDR] = 0x1C,
309 [DMA_END_ADDR_HI] = 0x20,
310 [DMA_MBUF_DONE_THRESH] = 0x24,
311 [TDMA_FLOW_PERIOD] = 0x28,
312 [TDMA_WRITE_PTR] = 0x2C,
313 [TDMA_WRITE_PTR_HI] = 0x30,
314};
315
316static const u8 genet_dma_ring_regs_v123[] = {
317 [TDMA_READ_PTR] = 0x00,
318 [TDMA_CONS_INDEX] = 0x04,
319 [TDMA_PROD_INDEX] = 0x08,
320 [DMA_RING_BUF_SIZE] = 0x0C,
321 [DMA_START_ADDR] = 0x10,
322 [DMA_END_ADDR] = 0x14,
323 [DMA_MBUF_DONE_THRESH] = 0x18,
324 [TDMA_FLOW_PERIOD] = 0x1C,
325 [TDMA_WRITE_PTR] = 0x20,
326};
327
328/* Set at runtime once GENET version is known */
329static const u8 *genet_dma_ring_regs;
330
331static inline u32 bcmgenet_tdma_ring_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700332 unsigned int ring,
333 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800334{
335 return __raw_readl(priv->base + GENET_TDMA_REG_OFF +
336 (DMA_RING_SIZE * ring) +
337 genet_dma_ring_regs[r]);
338}
339
340static inline void bcmgenet_tdma_ring_writel(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700341 unsigned int ring, u32 val,
342 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800343{
344 __raw_writel(val, priv->base + GENET_TDMA_REG_OFF +
345 (DMA_RING_SIZE * ring) +
346 genet_dma_ring_regs[r]);
347}
348
349static inline u32 bcmgenet_rdma_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_RDMA_REG_OFF +
354 (DMA_RING_SIZE * ring) +
355 genet_dma_ring_regs[r]);
356}
357
358static inline void bcmgenet_rdma_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_RDMA_REG_OFF +
363 (DMA_RING_SIZE * ring) +
364 genet_dma_ring_regs[r]);
365}
366
367static int bcmgenet_get_settings(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700368 struct ethtool_cmd *cmd)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800369{
370 struct bcmgenet_priv *priv = netdev_priv(dev);
371
372 if (!netif_running(dev))
373 return -EINVAL;
374
375 if (!priv->phydev)
376 return -ENODEV;
377
378 return phy_ethtool_gset(priv->phydev, cmd);
379}
380
381static int bcmgenet_set_settings(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700382 struct ethtool_cmd *cmd)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800383{
384 struct bcmgenet_priv *priv = netdev_priv(dev);
385
386 if (!netif_running(dev))
387 return -EINVAL;
388
389 if (!priv->phydev)
390 return -ENODEV;
391
392 return phy_ethtool_sset(priv->phydev, cmd);
393}
394
395static int bcmgenet_set_rx_csum(struct net_device *dev,
396 netdev_features_t wanted)
397{
398 struct bcmgenet_priv *priv = netdev_priv(dev);
399 u32 rbuf_chk_ctrl;
400 bool rx_csum_en;
401
402 rx_csum_en = !!(wanted & NETIF_F_RXCSUM);
403
404 rbuf_chk_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CHK_CTRL);
405
406 /* enable rx checksumming */
407 if (rx_csum_en)
408 rbuf_chk_ctrl |= RBUF_RXCHK_EN;
409 else
410 rbuf_chk_ctrl &= ~RBUF_RXCHK_EN;
411 priv->desc_rxchk_en = rx_csum_en;
Florian Fainelliebe5e3c2014-03-26 21:18:39 -0700412
413 /* If UniMAC forwards CRC, we need to skip over it to get
414 * a valid CHK bit to be set in the per-packet status word
415 */
416 if (rx_csum_en && priv->crc_fwd_en)
417 rbuf_chk_ctrl |= RBUF_SKIP_FCS;
418 else
419 rbuf_chk_ctrl &= ~RBUF_SKIP_FCS;
420
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800421 bcmgenet_rbuf_writel(priv, rbuf_chk_ctrl, RBUF_CHK_CTRL);
422
423 return 0;
424}
425
426static int bcmgenet_set_tx_csum(struct net_device *dev,
427 netdev_features_t wanted)
428{
429 struct bcmgenet_priv *priv = netdev_priv(dev);
430 bool desc_64b_en;
431 u32 tbuf_ctrl, rbuf_ctrl;
432
433 tbuf_ctrl = bcmgenet_tbuf_ctrl_get(priv);
434 rbuf_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
435
436 desc_64b_en = !!(wanted & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
437
438 /* enable 64 bytes descriptor in both directions (RBUF and TBUF) */
439 if (desc_64b_en) {
440 tbuf_ctrl |= RBUF_64B_EN;
441 rbuf_ctrl |= RBUF_64B_EN;
442 } else {
443 tbuf_ctrl &= ~RBUF_64B_EN;
444 rbuf_ctrl &= ~RBUF_64B_EN;
445 }
446 priv->desc_64b_en = desc_64b_en;
447
448 bcmgenet_tbuf_ctrl_set(priv, tbuf_ctrl);
449 bcmgenet_rbuf_writel(priv, rbuf_ctrl, RBUF_CTRL);
450
451 return 0;
452}
453
454static int bcmgenet_set_features(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700455 netdev_features_t features)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800456{
457 netdev_features_t changed = features ^ dev->features;
458 netdev_features_t wanted = dev->wanted_features;
459 int ret = 0;
460
461 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
462 ret = bcmgenet_set_tx_csum(dev, wanted);
463 if (changed & (NETIF_F_RXCSUM))
464 ret = bcmgenet_set_rx_csum(dev, wanted);
465
466 return ret;
467}
468
469static u32 bcmgenet_get_msglevel(struct net_device *dev)
470{
471 struct bcmgenet_priv *priv = netdev_priv(dev);
472
473 return priv->msg_enable;
474}
475
476static void bcmgenet_set_msglevel(struct net_device *dev, u32 level)
477{
478 struct bcmgenet_priv *priv = netdev_priv(dev);
479
480 priv->msg_enable = level;
481}
482
483/* standard ethtool support functions. */
484enum bcmgenet_stat_type {
485 BCMGENET_STAT_NETDEV = -1,
486 BCMGENET_STAT_MIB_RX,
487 BCMGENET_STAT_MIB_TX,
488 BCMGENET_STAT_RUNT,
489 BCMGENET_STAT_MISC,
490};
491
492struct bcmgenet_stats {
493 char stat_string[ETH_GSTRING_LEN];
494 int stat_sizeof;
495 int stat_offset;
496 enum bcmgenet_stat_type type;
497 /* reg offset from UMAC base for misc counters */
498 u16 reg_offset;
499};
500
501#define STAT_NETDEV(m) { \
502 .stat_string = __stringify(m), \
503 .stat_sizeof = sizeof(((struct net_device_stats *)0)->m), \
504 .stat_offset = offsetof(struct net_device_stats, m), \
505 .type = BCMGENET_STAT_NETDEV, \
506}
507
508#define STAT_GENET_MIB(str, m, _type) { \
509 .stat_string = str, \
510 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
511 .stat_offset = offsetof(struct bcmgenet_priv, m), \
512 .type = _type, \
513}
514
515#define STAT_GENET_MIB_RX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_RX)
516#define STAT_GENET_MIB_TX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_TX)
517#define STAT_GENET_RUNT(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_RUNT)
518
519#define STAT_GENET_MISC(str, m, offset) { \
520 .stat_string = str, \
521 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
522 .stat_offset = offsetof(struct bcmgenet_priv, m), \
523 .type = BCMGENET_STAT_MISC, \
524 .reg_offset = offset, \
525}
526
527
528/* There is a 0xC gap between the end of RX and beginning of TX stats and then
529 * between the end of TX stats and the beginning of the RX RUNT
530 */
531#define BCMGENET_STAT_OFFSET 0xc
532
533/* Hardware counters must be kept in sync because the order/offset
534 * is important here (order in structure declaration = order in hardware)
535 */
536static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
537 /* general stats */
538 STAT_NETDEV(rx_packets),
539 STAT_NETDEV(tx_packets),
540 STAT_NETDEV(rx_bytes),
541 STAT_NETDEV(tx_bytes),
542 STAT_NETDEV(rx_errors),
543 STAT_NETDEV(tx_errors),
544 STAT_NETDEV(rx_dropped),
545 STAT_NETDEV(tx_dropped),
546 STAT_NETDEV(multicast),
547 /* UniMAC RSV counters */
548 STAT_GENET_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
549 STAT_GENET_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
550 STAT_GENET_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
551 STAT_GENET_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
552 STAT_GENET_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
553 STAT_GENET_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
554 STAT_GENET_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
555 STAT_GENET_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
556 STAT_GENET_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
557 STAT_GENET_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
558 STAT_GENET_MIB_RX("rx_pkts", mib.rx.pkt),
559 STAT_GENET_MIB_RX("rx_bytes", mib.rx.bytes),
560 STAT_GENET_MIB_RX("rx_multicast", mib.rx.mca),
561 STAT_GENET_MIB_RX("rx_broadcast", mib.rx.bca),
562 STAT_GENET_MIB_RX("rx_fcs", mib.rx.fcs),
563 STAT_GENET_MIB_RX("rx_control", mib.rx.cf),
564 STAT_GENET_MIB_RX("rx_pause", mib.rx.pf),
565 STAT_GENET_MIB_RX("rx_unknown", mib.rx.uo),
566 STAT_GENET_MIB_RX("rx_align", mib.rx.aln),
567 STAT_GENET_MIB_RX("rx_outrange", mib.rx.flr),
568 STAT_GENET_MIB_RX("rx_code", mib.rx.cde),
569 STAT_GENET_MIB_RX("rx_carrier", mib.rx.fcr),
570 STAT_GENET_MIB_RX("rx_oversize", mib.rx.ovr),
571 STAT_GENET_MIB_RX("rx_jabber", mib.rx.jbr),
572 STAT_GENET_MIB_RX("rx_mtu_err", mib.rx.mtue),
573 STAT_GENET_MIB_RX("rx_good_pkts", mib.rx.pok),
574 STAT_GENET_MIB_RX("rx_unicast", mib.rx.uc),
575 STAT_GENET_MIB_RX("rx_ppp", mib.rx.ppp),
576 STAT_GENET_MIB_RX("rx_crc", mib.rx.rcrc),
577 /* UniMAC TSV counters */
578 STAT_GENET_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
579 STAT_GENET_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
580 STAT_GENET_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
581 STAT_GENET_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
582 STAT_GENET_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
583 STAT_GENET_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
584 STAT_GENET_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
585 STAT_GENET_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
586 STAT_GENET_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
587 STAT_GENET_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
588 STAT_GENET_MIB_TX("tx_pkts", mib.tx.pkts),
589 STAT_GENET_MIB_TX("tx_multicast", mib.tx.mca),
590 STAT_GENET_MIB_TX("tx_broadcast", mib.tx.bca),
591 STAT_GENET_MIB_TX("tx_pause", mib.tx.pf),
592 STAT_GENET_MIB_TX("tx_control", mib.tx.cf),
593 STAT_GENET_MIB_TX("tx_fcs_err", mib.tx.fcs),
594 STAT_GENET_MIB_TX("tx_oversize", mib.tx.ovr),
595 STAT_GENET_MIB_TX("tx_defer", mib.tx.drf),
596 STAT_GENET_MIB_TX("tx_excess_defer", mib.tx.edf),
597 STAT_GENET_MIB_TX("tx_single_col", mib.tx.scl),
598 STAT_GENET_MIB_TX("tx_multi_col", mib.tx.mcl),
599 STAT_GENET_MIB_TX("tx_late_col", mib.tx.lcl),
600 STAT_GENET_MIB_TX("tx_excess_col", mib.tx.ecl),
601 STAT_GENET_MIB_TX("tx_frags", mib.tx.frg),
602 STAT_GENET_MIB_TX("tx_total_col", mib.tx.ncl),
603 STAT_GENET_MIB_TX("tx_jabber", mib.tx.jbr),
604 STAT_GENET_MIB_TX("tx_bytes", mib.tx.bytes),
605 STAT_GENET_MIB_TX("tx_good_pkts", mib.tx.pok),
606 STAT_GENET_MIB_TX("tx_unicast", mib.tx.uc),
607 /* UniMAC RUNT counters */
608 STAT_GENET_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
609 STAT_GENET_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
610 STAT_GENET_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
611 STAT_GENET_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
612 /* Misc UniMAC counters */
613 STAT_GENET_MISC("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt,
614 UMAC_RBUF_OVFL_CNT),
615 STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt, UMAC_RBUF_ERR_CNT),
616 STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT),
Florian Fainelli44c8bc32014-11-19 10:29:56 -0800617 STAT_GENET_MIB_RX("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
618 STAT_GENET_MIB_RX("rx_dma_failed", mib.rx_dma_failed),
619 STAT_GENET_MIB_TX("tx_dma_failed", mib.tx_dma_failed),
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800620};
621
622#define BCMGENET_STATS_LEN ARRAY_SIZE(bcmgenet_gstrings_stats)
623
624static void bcmgenet_get_drvinfo(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700625 struct ethtool_drvinfo *info)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800626{
627 strlcpy(info->driver, "bcmgenet", sizeof(info->driver));
628 strlcpy(info->version, "v2.0", sizeof(info->version));
629 info->n_stats = BCMGENET_STATS_LEN;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800630}
631
632static int bcmgenet_get_sset_count(struct net_device *dev, int string_set)
633{
634 switch (string_set) {
635 case ETH_SS_STATS:
636 return BCMGENET_STATS_LEN;
637 default:
638 return -EOPNOTSUPP;
639 }
640}
641
Florian Fainellic91b7f62014-07-23 10:42:12 -0700642static void bcmgenet_get_strings(struct net_device *dev, u32 stringset,
643 u8 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800644{
645 int i;
646
647 switch (stringset) {
648 case ETH_SS_STATS:
649 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
650 memcpy(data + i * ETH_GSTRING_LEN,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700651 bcmgenet_gstrings_stats[i].stat_string,
652 ETH_GSTRING_LEN);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800653 }
654 break;
655 }
656}
657
658static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv)
659{
660 int i, j = 0;
661
662 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
663 const struct bcmgenet_stats *s;
664 u8 offset = 0;
665 u32 val = 0;
666 char *p;
667
668 s = &bcmgenet_gstrings_stats[i];
669 switch (s->type) {
670 case BCMGENET_STAT_NETDEV:
671 continue;
672 case BCMGENET_STAT_MIB_RX:
673 case BCMGENET_STAT_MIB_TX:
674 case BCMGENET_STAT_RUNT:
675 if (s->type != BCMGENET_STAT_MIB_RX)
676 offset = BCMGENET_STAT_OFFSET;
Florian Fainellic91b7f62014-07-23 10:42:12 -0700677 val = bcmgenet_umac_readl(priv,
678 UMAC_MIB_START + j + offset);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800679 break;
680 case BCMGENET_STAT_MISC:
681 val = bcmgenet_umac_readl(priv, s->reg_offset);
682 /* clear if overflowed */
683 if (val == ~0)
684 bcmgenet_umac_writel(priv, 0, s->reg_offset);
685 break;
686 }
687
688 j += s->stat_sizeof;
689 p = (char *)priv + s->stat_offset;
690 *(u32 *)p = val;
691 }
692}
693
694static void bcmgenet_get_ethtool_stats(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700695 struct ethtool_stats *stats,
696 u64 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800697{
698 struct bcmgenet_priv *priv = netdev_priv(dev);
699 int i;
700
701 if (netif_running(dev))
702 bcmgenet_update_mib_counters(priv);
703
704 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
705 const struct bcmgenet_stats *s;
706 char *p;
707
708 s = &bcmgenet_gstrings_stats[i];
709 if (s->type == BCMGENET_STAT_NETDEV)
710 p = (char *)&dev->stats;
711 else
712 p = (char *)priv;
713 p += s->stat_offset;
714 data[i] = *(u32 *)p;
715 }
716}
717
Florian Fainelli6ef398e2014-11-25 21:16:35 -0800718static void bcmgenet_eee_enable_set(struct net_device *dev, bool enable)
719{
720 struct bcmgenet_priv *priv = netdev_priv(dev);
721 u32 off = priv->hw_params->tbuf_offset + TBUF_ENERGY_CTRL;
722 u32 reg;
723
724 if (enable && !priv->clk_eee_enabled) {
725 clk_prepare_enable(priv->clk_eee);
726 priv->clk_eee_enabled = true;
727 }
728
729 reg = bcmgenet_umac_readl(priv, UMAC_EEE_CTRL);
730 if (enable)
731 reg |= EEE_EN;
732 else
733 reg &= ~EEE_EN;
734 bcmgenet_umac_writel(priv, reg, UMAC_EEE_CTRL);
735
736 /* Enable EEE and switch to a 27Mhz clock automatically */
737 reg = __raw_readl(priv->base + off);
738 if (enable)
739 reg |= TBUF_EEE_EN | TBUF_PM_EN;
740 else
741 reg &= ~(TBUF_EEE_EN | TBUF_PM_EN);
742 __raw_writel(reg, priv->base + off);
743
744 /* Do the same for thing for RBUF */
745 reg = bcmgenet_rbuf_readl(priv, RBUF_ENERGY_CTRL);
746 if (enable)
747 reg |= RBUF_EEE_EN | RBUF_PM_EN;
748 else
749 reg &= ~(RBUF_EEE_EN | RBUF_PM_EN);
750 bcmgenet_rbuf_writel(priv, reg, RBUF_ENERGY_CTRL);
751
752 if (!enable && priv->clk_eee_enabled) {
753 clk_disable_unprepare(priv->clk_eee);
754 priv->clk_eee_enabled = false;
755 }
756
757 priv->eee.eee_enabled = enable;
758 priv->eee.eee_active = enable;
759}
760
761static int bcmgenet_get_eee(struct net_device *dev, struct ethtool_eee *e)
762{
763 struct bcmgenet_priv *priv = netdev_priv(dev);
764 struct ethtool_eee *p = &priv->eee;
765
766 if (GENET_IS_V1(priv))
767 return -EOPNOTSUPP;
768
769 e->eee_enabled = p->eee_enabled;
770 e->eee_active = p->eee_active;
771 e->tx_lpi_timer = bcmgenet_umac_readl(priv, UMAC_EEE_LPI_TIMER);
772
773 return phy_ethtool_get_eee(priv->phydev, e);
774}
775
776static int bcmgenet_set_eee(struct net_device *dev, struct ethtool_eee *e)
777{
778 struct bcmgenet_priv *priv = netdev_priv(dev);
779 struct ethtool_eee *p = &priv->eee;
780 int ret = 0;
781
782 if (GENET_IS_V1(priv))
783 return -EOPNOTSUPP;
784
785 p->eee_enabled = e->eee_enabled;
786
787 if (!p->eee_enabled) {
788 bcmgenet_eee_enable_set(dev, false);
789 } else {
790 ret = phy_init_eee(priv->phydev, 0);
791 if (ret) {
792 netif_err(priv, hw, dev, "EEE initialization failed\n");
793 return ret;
794 }
795
796 bcmgenet_umac_writel(priv, e->tx_lpi_timer, UMAC_EEE_LPI_TIMER);
797 bcmgenet_eee_enable_set(dev, true);
798 }
799
800 return phy_ethtool_set_eee(priv->phydev, e);
801}
802
Florian Fainelli6b0c5402014-11-25 21:16:36 -0800803static int bcmgenet_nway_reset(struct net_device *dev)
804{
805 struct bcmgenet_priv *priv = netdev_priv(dev);
806
807 return genphy_restart_aneg(priv->phydev);
808}
809
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800810/* standard ethtool support functions. */
811static struct ethtool_ops bcmgenet_ethtool_ops = {
812 .get_strings = bcmgenet_get_strings,
813 .get_sset_count = bcmgenet_get_sset_count,
814 .get_ethtool_stats = bcmgenet_get_ethtool_stats,
815 .get_settings = bcmgenet_get_settings,
816 .set_settings = bcmgenet_set_settings,
817 .get_drvinfo = bcmgenet_get_drvinfo,
818 .get_link = ethtool_op_get_link,
819 .get_msglevel = bcmgenet_get_msglevel,
820 .set_msglevel = bcmgenet_set_msglevel,
Florian Fainelli06ba8372014-07-21 15:29:29 -0700821 .get_wol = bcmgenet_get_wol,
822 .set_wol = bcmgenet_set_wol,
Florian Fainelli6ef398e2014-11-25 21:16:35 -0800823 .get_eee = bcmgenet_get_eee,
824 .set_eee = bcmgenet_set_eee,
Florian Fainelli6b0c5402014-11-25 21:16:36 -0800825 .nway_reset = bcmgenet_nway_reset,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800826};
827
828/* Power down the unimac, based on mode. */
829static void bcmgenet_power_down(struct bcmgenet_priv *priv,
830 enum bcmgenet_power_mode mode)
831{
832 u32 reg;
833
834 switch (mode) {
835 case GENET_POWER_CABLE_SENSE:
Florian Fainelli80d8e962014-02-24 16:56:11 -0800836 phy_detach(priv->phydev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800837 break;
838
Florian Fainellic3ae64a2014-07-21 15:29:25 -0700839 case GENET_POWER_WOL_MAGIC:
840 bcmgenet_wol_power_down_cfg(priv, mode);
841 break;
842
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800843 case GENET_POWER_PASSIVE:
844 /* Power down LED */
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800845 if (priv->hw_params->flags & GENET_HAS_EXT) {
846 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
847 reg |= (EXT_PWR_DOWN_PHY |
848 EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
849 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
850 }
851 break;
852 default:
853 break;
854 }
855}
856
857static void bcmgenet_power_up(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700858 enum bcmgenet_power_mode mode)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800859{
860 u32 reg;
861
862 if (!(priv->hw_params->flags & GENET_HAS_EXT))
863 return;
864
865 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
866
867 switch (mode) {
868 case GENET_POWER_PASSIVE:
869 reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_PHY |
870 EXT_PWR_DOWN_BIAS);
871 /* fallthrough */
872 case GENET_POWER_CABLE_SENSE:
873 /* enable APD */
874 reg |= EXT_PWR_DN_EN_LD;
875 break;
Florian Fainellic3ae64a2014-07-21 15:29:25 -0700876 case GENET_POWER_WOL_MAGIC:
877 bcmgenet_wol_power_up_cfg(priv, mode);
878 return;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800879 default:
880 break;
881 }
882
883 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
Florian Fainellicc013fb2014-08-11 14:50:43 -0700884
885 if (mode == GENET_POWER_PASSIVE)
886 bcmgenet_mii_reset(priv->dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800887}
888
889/* ioctl handle special commands that are not present in ethtool. */
890static int bcmgenet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
891{
892 struct bcmgenet_priv *priv = netdev_priv(dev);
893 int val = 0;
894
895 if (!netif_running(dev))
896 return -EINVAL;
897
898 switch (cmd) {
899 case SIOCGMIIPHY:
900 case SIOCGMIIREG:
901 case SIOCSMIIREG:
902 if (!priv->phydev)
903 val = -ENODEV;
904 else
905 val = phy_mii_ioctl(priv->phydev, rq, cmd);
906 break;
907
908 default:
909 val = -EINVAL;
910 break;
911 }
912
913 return val;
914}
915
916static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv,
917 struct bcmgenet_tx_ring *ring)
918{
919 struct enet_cb *tx_cb_ptr;
920
921 tx_cb_ptr = ring->cbs;
922 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
Petri Gynther014012a2015-02-23 11:00:45 -0800923
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800924 /* Advancing local write pointer */
925 if (ring->write_ptr == ring->end_ptr)
926 ring->write_ptr = ring->cb_ptr;
927 else
928 ring->write_ptr++;
929
930 return tx_cb_ptr;
931}
932
933/* Simple helper to free a control block's resources */
934static void bcmgenet_free_cb(struct enet_cb *cb)
935{
936 dev_kfree_skb_any(cb->skb);
937 cb->skb = NULL;
938 dma_unmap_addr_set(cb, dma_addr, 0);
939}
940
941static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_priv *priv,
942 struct bcmgenet_tx_ring *ring)
943{
944 bcmgenet_intrl2_0_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700945 UMAC_IRQ_TXDMA_BDONE | UMAC_IRQ_TXDMA_PDONE,
946 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800947}
948
949static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_priv *priv,
950 struct bcmgenet_tx_ring *ring)
951{
952 bcmgenet_intrl2_0_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700953 UMAC_IRQ_TXDMA_BDONE | UMAC_IRQ_TXDMA_PDONE,
954 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800955}
956
957static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700958 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800959{
Florian Fainellic91b7f62014-07-23 10:42:12 -0700960 bcmgenet_intrl2_1_writel(priv, (1 << ring->index),
961 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800962 priv->int1_mask &= ~(1 << ring->index);
963}
964
965static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_priv *priv,
966 struct bcmgenet_tx_ring *ring)
967{
Florian Fainellic91b7f62014-07-23 10:42:12 -0700968 bcmgenet_intrl2_1_writel(priv, (1 << ring->index),
969 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800970 priv->int1_mask |= (1 << ring->index);
971}
972
973/* Unlocked version of the reclaim routine */
974static void __bcmgenet_tx_reclaim(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700975 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800976{
977 struct bcmgenet_priv *priv = netdev_priv(dev);
978 int last_tx_cn, last_c_index, num_tx_bds;
979 struct enet_cb *tx_cb_ptr;
Florian Fainellib2cde2c2014-03-20 10:53:23 -0700980 struct netdev_queue *txq;
Florian Fainelli478a0102014-09-22 11:54:42 -0700981 unsigned int bds_compl;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800982 unsigned int c_index;
983
Brian Norris7fc527f2014-07-29 14:34:14 -0700984 /* Compute how many buffers are transmitted since last xmit call */
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800985 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
Florian Fainellib2cde2c2014-03-20 10:53:23 -0700986 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800987
988 last_c_index = ring->c_index;
989 num_tx_bds = ring->size;
990
991 c_index &= (num_tx_bds - 1);
992
993 if (c_index >= last_c_index)
994 last_tx_cn = c_index - last_c_index;
995 else
996 last_tx_cn = num_tx_bds - last_c_index + c_index;
997
998 netif_dbg(priv, tx_done, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700999 "%s ring=%d index=%d last_tx_cn=%d last_index=%d\n",
1000 __func__, ring->index,
1001 c_index, last_tx_cn, last_c_index);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001002
1003 /* Reclaim transmitted buffers */
1004 while (last_tx_cn-- > 0) {
1005 tx_cb_ptr = ring->cbs + last_c_index;
Florian Fainelli478a0102014-09-22 11:54:42 -07001006 bds_compl = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001007 if (tx_cb_ptr->skb) {
Florian Fainelli478a0102014-09-22 11:54:42 -07001008 bds_compl = skb_shinfo(tx_cb_ptr->skb)->nr_frags + 1;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001009 dev->stats.tx_bytes += tx_cb_ptr->skb->len;
1010 dma_unmap_single(&dev->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001011 dma_unmap_addr(tx_cb_ptr, dma_addr),
1012 tx_cb_ptr->skb->len,
1013 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001014 bcmgenet_free_cb(tx_cb_ptr);
1015 } else if (dma_unmap_addr(tx_cb_ptr, dma_addr)) {
1016 dev->stats.tx_bytes +=
1017 dma_unmap_len(tx_cb_ptr, dma_len);
1018 dma_unmap_page(&dev->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001019 dma_unmap_addr(tx_cb_ptr, dma_addr),
1020 dma_unmap_len(tx_cb_ptr, dma_len),
1021 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001022 dma_unmap_addr_set(tx_cb_ptr, dma_addr, 0);
1023 }
1024 dev->stats.tx_packets++;
Florian Fainelli478a0102014-09-22 11:54:42 -07001025 ring->free_bds += bds_compl;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001026
1027 last_c_index++;
1028 last_c_index &= (num_tx_bds - 1);
1029 }
1030
1031 if (ring->free_bds > (MAX_SKB_FRAGS + 1))
1032 ring->int_disable(priv, ring);
1033
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001034 if (netif_tx_queue_stopped(txq))
1035 netif_tx_wake_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001036
1037 ring->c_index = c_index;
1038}
1039
1040static void bcmgenet_tx_reclaim(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001041 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001042{
1043 unsigned long flags;
1044
1045 spin_lock_irqsave(&ring->lock, flags);
1046 __bcmgenet_tx_reclaim(dev, ring);
1047 spin_unlock_irqrestore(&ring->lock, flags);
1048}
1049
1050static void bcmgenet_tx_reclaim_all(struct net_device *dev)
1051{
1052 struct bcmgenet_priv *priv = netdev_priv(dev);
1053 int i;
1054
1055 if (netif_is_multiqueue(dev)) {
1056 for (i = 0; i < priv->hw_params->tx_queues; i++)
1057 bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
1058 }
1059
1060 bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
1061}
1062
1063/* Transmits a single SKB (either head of a fragment or a single SKB)
1064 * caller must hold priv->lock
1065 */
1066static int bcmgenet_xmit_single(struct net_device *dev,
1067 struct sk_buff *skb,
1068 u16 dma_desc_flags,
1069 struct bcmgenet_tx_ring *ring)
1070{
1071 struct bcmgenet_priv *priv = netdev_priv(dev);
1072 struct device *kdev = &priv->pdev->dev;
1073 struct enet_cb *tx_cb_ptr;
1074 unsigned int skb_len;
1075 dma_addr_t mapping;
1076 u32 length_status;
1077 int ret;
1078
1079 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
1080
1081 if (unlikely(!tx_cb_ptr))
1082 BUG();
1083
1084 tx_cb_ptr->skb = skb;
1085
1086 skb_len = skb_headlen(skb) < ETH_ZLEN ? ETH_ZLEN : skb_headlen(skb);
1087
1088 mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
1089 ret = dma_mapping_error(kdev, mapping);
1090 if (ret) {
Florian Fainelli44c8bc32014-11-19 10:29:56 -08001091 priv->mib.tx_dma_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001092 netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
1093 dev_kfree_skb(skb);
1094 return ret;
1095 }
1096
1097 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
1098 dma_unmap_len_set(tx_cb_ptr, dma_len, skb->len);
1099 length_status = (skb_len << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
1100 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT) |
1101 DMA_TX_APPEND_CRC;
1102
1103 if (skb->ip_summed == CHECKSUM_PARTIAL)
1104 length_status |= DMA_TX_DO_CSUM;
1105
1106 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, length_status);
1107
1108 /* Decrement total BD count and advance our write pointer */
1109 ring->free_bds -= 1;
1110 ring->prod_index += 1;
1111 ring->prod_index &= DMA_P_INDEX_MASK;
1112
1113 return 0;
1114}
1115
Brian Norris7fc527f2014-07-29 14:34:14 -07001116/* Transmit a SKB fragment */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001117static int bcmgenet_xmit_frag(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001118 skb_frag_t *frag,
1119 u16 dma_desc_flags,
1120 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001121{
1122 struct bcmgenet_priv *priv = netdev_priv(dev);
1123 struct device *kdev = &priv->pdev->dev;
1124 struct enet_cb *tx_cb_ptr;
1125 dma_addr_t mapping;
1126 int ret;
1127
1128 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
1129
1130 if (unlikely(!tx_cb_ptr))
1131 BUG();
1132 tx_cb_ptr->skb = NULL;
1133
1134 mapping = skb_frag_dma_map(kdev, frag, 0,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001135 skb_frag_size(frag), DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001136 ret = dma_mapping_error(kdev, mapping);
1137 if (ret) {
Florian Fainelli44c8bc32014-11-19 10:29:56 -08001138 priv->mib.tx_dma_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001139 netif_err(priv, tx_err, dev, "%s: Tx DMA map failed\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001140 __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001141 return ret;
1142 }
1143
1144 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
1145 dma_unmap_len_set(tx_cb_ptr, dma_len, frag->size);
1146
1147 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001148 (frag->size << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
1149 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001150
1151
1152 ring->free_bds -= 1;
1153 ring->prod_index += 1;
1154 ring->prod_index &= DMA_P_INDEX_MASK;
1155
1156 return 0;
1157}
1158
1159/* Reallocate the SKB to put enough headroom in front of it and insert
1160 * the transmit checksum offsets in the descriptors
1161 */
Petri Gyntherbc233332014-10-01 11:30:01 -07001162static struct sk_buff *bcmgenet_put_tx_csum(struct net_device *dev,
1163 struct sk_buff *skb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001164{
1165 struct status_64 *status = NULL;
1166 struct sk_buff *new_skb;
1167 u16 offset;
1168 u8 ip_proto;
1169 u16 ip_ver;
1170 u32 tx_csum_info;
1171
1172 if (unlikely(skb_headroom(skb) < sizeof(*status))) {
1173 /* If 64 byte status block enabled, must make sure skb has
1174 * enough headroom for us to insert 64B status block.
1175 */
1176 new_skb = skb_realloc_headroom(skb, sizeof(*status));
1177 dev_kfree_skb(skb);
1178 if (!new_skb) {
1179 dev->stats.tx_errors++;
1180 dev->stats.tx_dropped++;
Petri Gyntherbc233332014-10-01 11:30:01 -07001181 return NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001182 }
1183 skb = new_skb;
1184 }
1185
1186 skb_push(skb, sizeof(*status));
1187 status = (struct status_64 *)skb->data;
1188
1189 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1190 ip_ver = htons(skb->protocol);
1191 switch (ip_ver) {
1192 case ETH_P_IP:
1193 ip_proto = ip_hdr(skb)->protocol;
1194 break;
1195 case ETH_P_IPV6:
1196 ip_proto = ipv6_hdr(skb)->nexthdr;
1197 break;
1198 default:
Petri Gyntherbc233332014-10-01 11:30:01 -07001199 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001200 }
1201
1202 offset = skb_checksum_start_offset(skb) - sizeof(*status);
1203 tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
1204 (offset + skb->csum_offset);
1205
1206 /* Set the length valid bit for TCP and UDP and just set
1207 * the special UDP flag for IPv4, else just set to 0.
1208 */
1209 if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
1210 tx_csum_info |= STATUS_TX_CSUM_LV;
1211 if (ip_proto == IPPROTO_UDP && ip_ver == ETH_P_IP)
1212 tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
Florian Fainelli8900ea572014-07-23 10:42:14 -07001213 } else {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001214 tx_csum_info = 0;
Florian Fainelli8900ea572014-07-23 10:42:14 -07001215 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001216
1217 status->tx_csum_info = tx_csum_info;
1218 }
1219
Petri Gyntherbc233332014-10-01 11:30:01 -07001220 return skb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001221}
1222
1223static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
1224{
1225 struct bcmgenet_priv *priv = netdev_priv(dev);
1226 struct bcmgenet_tx_ring *ring = NULL;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001227 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001228 unsigned long flags = 0;
1229 int nr_frags, index;
1230 u16 dma_desc_flags;
1231 int ret;
1232 int i;
1233
1234 index = skb_get_queue_mapping(skb);
1235 /* Mapping strategy:
1236 * queue_mapping = 0, unclassified, packet xmited through ring16
1237 * queue_mapping = 1, goes to ring 0. (highest priority queue
1238 * queue_mapping = 2, goes to ring 1.
1239 * queue_mapping = 3, goes to ring 2.
1240 * queue_mapping = 4, goes to ring 3.
1241 */
1242 if (index == 0)
1243 index = DESC_INDEX;
1244 else
1245 index -= 1;
1246
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001247 nr_frags = skb_shinfo(skb)->nr_frags;
1248 ring = &priv->tx_rings[index];
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001249 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001250
1251 spin_lock_irqsave(&ring->lock, flags);
1252 if (ring->free_bds <= nr_frags + 1) {
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001253 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001254 netdev_err(dev, "%s: tx ring %d full when queue %d awake\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001255 __func__, index, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001256 ret = NETDEV_TX_BUSY;
1257 goto out;
1258 }
1259
Florian Fainelli474ea9c2014-07-22 11:01:52 -07001260 if (skb_padto(skb, ETH_ZLEN)) {
1261 ret = NETDEV_TX_OK;
1262 goto out;
1263 }
1264
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001265 /* set the SKB transmit checksum */
1266 if (priv->desc_64b_en) {
Petri Gyntherbc233332014-10-01 11:30:01 -07001267 skb = bcmgenet_put_tx_csum(dev, skb);
1268 if (!skb) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001269 ret = NETDEV_TX_OK;
1270 goto out;
1271 }
1272 }
1273
1274 dma_desc_flags = DMA_SOP;
1275 if (nr_frags == 0)
1276 dma_desc_flags |= DMA_EOP;
1277
1278 /* Transmit single SKB or head of fragment list */
1279 ret = bcmgenet_xmit_single(dev, skb, dma_desc_flags, ring);
1280 if (ret) {
1281 ret = NETDEV_TX_OK;
1282 goto out;
1283 }
1284
1285 /* xmit fragment */
1286 for (i = 0; i < nr_frags; i++) {
1287 ret = bcmgenet_xmit_frag(dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001288 &skb_shinfo(skb)->frags[i],
1289 (i == nr_frags - 1) ? DMA_EOP : 0,
1290 ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001291 if (ret) {
1292 ret = NETDEV_TX_OK;
1293 goto out;
1294 }
1295 }
1296
Florian Fainellid03825f2014-03-20 10:53:21 -07001297 skb_tx_timestamp(skb);
1298
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001299 /* we kept a software copy of how much we should advance the TDMA
1300 * producer index, now write it down to the hardware
1301 */
1302 bcmgenet_tdma_ring_writel(priv, ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001303 ring->prod_index, TDMA_PROD_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001304
1305 if (ring->free_bds <= (MAX_SKB_FRAGS + 1)) {
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001306 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001307 ring->int_enable(priv, ring);
1308 }
1309
1310out:
1311 spin_unlock_irqrestore(&ring->lock, flags);
1312
1313 return ret;
1314}
1315
1316
Florian Fainellic91b7f62014-07-23 10:42:12 -07001317static int bcmgenet_rx_refill(struct bcmgenet_priv *priv, struct enet_cb *cb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001318{
1319 struct device *kdev = &priv->pdev->dev;
1320 struct sk_buff *skb;
1321 dma_addr_t mapping;
1322 int ret;
1323
Florian Fainellic91b7f62014-07-23 10:42:12 -07001324 skb = netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001325 if (!skb)
1326 return -ENOMEM;
1327
1328 /* a caller did not release this control block */
1329 WARN_ON(cb->skb != NULL);
1330 cb->skb = skb;
1331 mapping = dma_map_single(kdev, skb->data,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001332 priv->rx_buf_len, DMA_FROM_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001333 ret = dma_mapping_error(kdev, mapping);
1334 if (ret) {
Florian Fainelli44c8bc32014-11-19 10:29:56 -08001335 priv->mib.rx_dma_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001336 bcmgenet_free_cb(cb);
1337 netif_err(priv, rx_err, priv->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001338 "%s DMA map failed\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001339 return ret;
1340 }
1341
1342 dma_unmap_addr_set(cb, dma_addr, mapping);
1343 /* assign packet, prepare descriptor, and advance pointer */
1344
1345 dmadesc_set_addr(priv, priv->rx_bd_assign_ptr, mapping);
1346
1347 /* turn on the newly assigned BD for DMA to use */
1348 priv->rx_bd_assign_index++;
1349 priv->rx_bd_assign_index &= (priv->num_rx_bds - 1);
1350
1351 priv->rx_bd_assign_ptr = priv->rx_bds +
1352 (priv->rx_bd_assign_index * DMA_DESC_SIZE);
1353
1354 return 0;
1355}
1356
1357/* bcmgenet_desc_rx - descriptor based rx process.
1358 * this could be called from bottom half, or from NAPI polling method.
1359 */
1360static unsigned int bcmgenet_desc_rx(struct bcmgenet_priv *priv,
1361 unsigned int budget)
1362{
1363 struct net_device *dev = priv->dev;
1364 struct enet_cb *cb;
1365 struct sk_buff *skb;
1366 u32 dma_length_status;
1367 unsigned long dma_flag;
1368 int len, err;
1369 unsigned int rxpktprocessed = 0, rxpkttoprocess;
1370 unsigned int p_index;
1371 unsigned int chksum_ok = 0;
1372
Florian Fainellic91b7f62014-07-23 10:42:12 -07001373 p_index = bcmgenet_rdma_ring_readl(priv, DESC_INDEX, RDMA_PROD_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001374 p_index &= DMA_P_INDEX_MASK;
1375
1376 if (p_index < priv->rx_c_index)
1377 rxpkttoprocess = (DMA_C_INDEX_MASK + 1) -
1378 priv->rx_c_index + p_index;
1379 else
1380 rxpkttoprocess = p_index - priv->rx_c_index;
1381
1382 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001383 "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001384
1385 while ((rxpktprocessed < rxpkttoprocess) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001386 (rxpktprocessed < budget)) {
Florian Fainellib629be52014-09-08 11:37:52 -07001387 cb = &priv->rx_cbs[priv->rx_read_ptr];
1388 skb = cb->skb;
1389
Florian Fainellib629be52014-09-08 11:37:52 -07001390 /* We do not have a backing SKB, so we do not have a
1391 * corresponding DMA mapping for this incoming packet since
1392 * bcmgenet_rx_refill always either has both skb and mapping or
1393 * none.
1394 */
1395 if (unlikely(!skb)) {
1396 dev->stats.rx_dropped++;
1397 dev->stats.rx_errors++;
1398 goto refill;
1399 }
1400
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001401 /* Unmap the packet contents such that we can use the
1402 * RSV from the 64 bytes descriptor when enabled and save
1403 * a 32-bits register read
1404 */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001405 dma_unmap_single(&dev->dev, dma_unmap_addr(cb, dma_addr),
Florian Fainellic91b7f62014-07-23 10:42:12 -07001406 priv->rx_buf_len, DMA_FROM_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001407
1408 if (!priv->desc_64b_en) {
Florian Fainellic91b7f62014-07-23 10:42:12 -07001409 dma_length_status =
1410 dmadesc_get_length_status(priv,
1411 priv->rx_bds +
1412 (priv->rx_read_ptr *
1413 DMA_DESC_SIZE));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001414 } else {
1415 struct status_64 *status;
Florian Fainelli164d4f22014-07-23 10:42:13 -07001416
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001417 status = (struct status_64 *)skb->data;
1418 dma_length_status = status->length_status;
1419 }
1420
1421 /* DMA flags and length are still valid no matter how
1422 * we got the Receive Status Vector (64B RSB or register)
1423 */
1424 dma_flag = dma_length_status & 0xffff;
1425 len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
1426
1427 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001428 "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
1429 __func__, p_index, priv->rx_c_index,
1430 priv->rx_read_ptr, dma_length_status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001431
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001432 if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
1433 netif_err(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001434 "dropping fragmented packet!\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001435 dev->stats.rx_dropped++;
1436 dev->stats.rx_errors++;
1437 dev_kfree_skb_any(cb->skb);
1438 cb->skb = NULL;
1439 goto refill;
1440 }
1441 /* report errors */
1442 if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
1443 DMA_RX_OV |
1444 DMA_RX_NO |
1445 DMA_RX_LG |
1446 DMA_RX_RXER))) {
1447 netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001448 (unsigned int)dma_flag);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001449 if (dma_flag & DMA_RX_CRC_ERROR)
1450 dev->stats.rx_crc_errors++;
1451 if (dma_flag & DMA_RX_OV)
1452 dev->stats.rx_over_errors++;
1453 if (dma_flag & DMA_RX_NO)
1454 dev->stats.rx_frame_errors++;
1455 if (dma_flag & DMA_RX_LG)
1456 dev->stats.rx_length_errors++;
1457 dev->stats.rx_dropped++;
1458 dev->stats.rx_errors++;
1459
1460 /* discard the packet and advance consumer index.*/
1461 dev_kfree_skb_any(cb->skb);
1462 cb->skb = NULL;
1463 goto refill;
1464 } /* error packet */
1465
1466 chksum_ok = (dma_flag & priv->dma_rx_chk_bit) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001467 priv->desc_rxchk_en;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001468
1469 skb_put(skb, len);
1470 if (priv->desc_64b_en) {
1471 skb_pull(skb, 64);
1472 len -= 64;
1473 }
1474
1475 if (likely(chksum_ok))
1476 skb->ip_summed = CHECKSUM_UNNECESSARY;
1477
1478 /* remove hardware 2bytes added for IP alignment */
1479 skb_pull(skb, 2);
1480 len -= 2;
1481
1482 if (priv->crc_fwd_en) {
1483 skb_trim(skb, len - ETH_FCS_LEN);
1484 len -= ETH_FCS_LEN;
1485 }
1486
1487 /*Finish setting up the received SKB and send it to the kernel*/
1488 skb->protocol = eth_type_trans(skb, priv->dev);
1489 dev->stats.rx_packets++;
1490 dev->stats.rx_bytes += len;
1491 if (dma_flag & DMA_RX_MULT)
1492 dev->stats.multicast++;
1493
1494 /* Notify kernel */
1495 napi_gro_receive(&priv->napi, skb);
1496 cb->skb = NULL;
1497 netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
1498
1499 /* refill RX path on the current control block */
1500refill:
1501 err = bcmgenet_rx_refill(priv, cb);
Florian Fainelli44c8bc32014-11-19 10:29:56 -08001502 if (err) {
1503 priv->mib.alloc_rx_buff_failed++;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001504 netif_err(priv, rx_err, dev, "Rx refill failed\n");
Florian Fainelli44c8bc32014-11-19 10:29:56 -08001505 }
Florian Fainellicf377d82014-10-10 10:51:52 -07001506
1507 rxpktprocessed++;
1508 priv->rx_read_ptr++;
1509 priv->rx_read_ptr &= (priv->num_rx_bds - 1);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001510 }
1511
1512 return rxpktprocessed;
1513}
1514
1515/* Assign skb to RX DMA descriptor. */
1516static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv)
1517{
1518 struct enet_cb *cb;
1519 int ret = 0;
1520 int i;
1521
1522 netif_dbg(priv, hw, priv->dev, "%s:\n", __func__);
1523
1524 /* loop here for each buffer needing assign */
1525 for (i = 0; i < priv->num_rx_bds; i++) {
1526 cb = &priv->rx_cbs[priv->rx_bd_assign_index];
1527 if (cb->skb)
1528 continue;
1529
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001530 ret = bcmgenet_rx_refill(priv, cb);
1531 if (ret)
1532 break;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001533 }
1534
1535 return ret;
1536}
1537
1538static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
1539{
1540 struct enet_cb *cb;
1541 int i;
1542
1543 for (i = 0; i < priv->num_rx_bds; i++) {
1544 cb = &priv->rx_cbs[i];
1545
1546 if (dma_unmap_addr(cb, dma_addr)) {
1547 dma_unmap_single(&priv->dev->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001548 dma_unmap_addr(cb, dma_addr),
1549 priv->rx_buf_len, DMA_FROM_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001550 dma_unmap_addr_set(cb, dma_addr, 0);
1551 }
1552
1553 if (cb->skb)
1554 bcmgenet_free_cb(cb);
1555 }
1556}
1557
Florian Fainellic91b7f62014-07-23 10:42:12 -07001558static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable)
Florian Fainellie29585b2014-07-21 15:29:20 -07001559{
1560 u32 reg;
1561
1562 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1563 if (enable)
1564 reg |= mask;
1565 else
1566 reg &= ~mask;
1567 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
1568
1569 /* UniMAC stops on a packet boundary, wait for a full-size packet
1570 * to be processed
1571 */
1572 if (enable == 0)
1573 usleep_range(1000, 2000);
1574}
1575
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001576static int reset_umac(struct bcmgenet_priv *priv)
1577{
1578 struct device *kdev = &priv->pdev->dev;
1579 unsigned int timeout = 0;
1580 u32 reg;
1581
1582 /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
1583 bcmgenet_rbuf_ctrl_set(priv, 0);
1584 udelay(10);
1585
1586 /* disable MAC while updating its registers */
1587 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1588
1589 /* issue soft reset, wait for it to complete */
1590 bcmgenet_umac_writel(priv, CMD_SW_RESET, UMAC_CMD);
1591 while (timeout++ < 1000) {
1592 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1593 if (!(reg & CMD_SW_RESET))
1594 return 0;
1595
1596 udelay(1);
1597 }
1598
1599 if (timeout == 1000) {
1600 dev_err(kdev,
Brian Norris7fc527f2014-07-29 14:34:14 -07001601 "timeout waiting for MAC to come out of reset\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001602 return -ETIMEDOUT;
1603 }
1604
1605 return 0;
1606}
1607
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001608static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
1609{
1610 /* Mask all interrupts.*/
1611 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1612 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
1613 bcmgenet_intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1614 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1615 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
1616 bcmgenet_intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1617}
1618
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001619static int init_umac(struct bcmgenet_priv *priv)
1620{
1621 struct device *kdev = &priv->pdev->dev;
1622 int ret;
1623 u32 reg, cpu_mask_clear;
1624
1625 dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
1626
1627 ret = reset_umac(priv);
1628 if (ret)
1629 return ret;
1630
1631 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1632 /* clear tx/rx counter */
1633 bcmgenet_umac_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001634 MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
1635 UMAC_MIB_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001636 bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
1637
1638 bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
1639
1640 /* init rx registers, enable ip header optimization */
1641 reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
1642 reg |= RBUF_ALIGN_2B;
1643 bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
1644
1645 if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
1646 bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
1647
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001648 bcmgenet_intr_disable(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001649
1650 cpu_mask_clear = UMAC_IRQ_RXDMA_BDONE;
1651
1652 dev_dbg(kdev, "%s:Enabling RXDMA_BDONE interrupt\n", __func__);
1653
Brian Norris7fc527f2014-07-29 14:34:14 -07001654 /* Monitor cable plug/unplugged event for internal PHY */
Florian Fainelli8900ea572014-07-23 10:42:14 -07001655 if (phy_is_internal(priv->phydev)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001656 cpu_mask_clear |= (UMAC_IRQ_LINK_DOWN | UMAC_IRQ_LINK_UP);
Florian Fainelli8900ea572014-07-23 10:42:14 -07001657 } else if (priv->ext_phy) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001658 cpu_mask_clear |= (UMAC_IRQ_LINK_DOWN | UMAC_IRQ_LINK_UP);
Florian Fainelli8900ea572014-07-23 10:42:14 -07001659 } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001660 reg = bcmgenet_bp_mc_get(priv);
1661 reg |= BIT(priv->hw_params->bp_in_en_shift);
1662
1663 /* bp_mask: back pressure mask */
1664 if (netif_is_multiqueue(priv->dev))
1665 reg |= priv->hw_params->bp_in_mask;
1666 else
1667 reg &= ~priv->hw_params->bp_in_mask;
1668 bcmgenet_bp_mc_set(priv, reg);
1669 }
1670
1671 /* Enable MDIO interrupts on GENET v3+ */
1672 if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
1673 cpu_mask_clear |= UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR;
1674
Florian Fainellic91b7f62014-07-23 10:42:12 -07001675 bcmgenet_intrl2_0_writel(priv, cpu_mask_clear, INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001676
1677 /* Enable rx/tx engine.*/
1678 dev_dbg(kdev, "done init umac\n");
1679
1680 return 0;
1681}
1682
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001683/* Initialize a Tx ring along with corresponding hardware registers */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001684static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
1685 unsigned int index, unsigned int size,
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001686 unsigned int start_ptr, unsigned int end_ptr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001687{
1688 struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
1689 u32 words_per_bd = WORDS_PER_BD(priv);
1690 u32 flow_period_val = 0;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001691
1692 spin_lock_init(&ring->lock);
1693 ring->index = index;
1694 if (index == DESC_INDEX) {
1695 ring->queue = 0;
1696 ring->int_enable = bcmgenet_tx_ring16_int_enable;
1697 ring->int_disable = bcmgenet_tx_ring16_int_disable;
1698 } else {
1699 ring->queue = index + 1;
1700 ring->int_enable = bcmgenet_tx_ring_int_enable;
1701 ring->int_disable = bcmgenet_tx_ring_int_disable;
1702 }
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001703 ring->cbs = priv->tx_cbs + start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001704 ring->size = size;
1705 ring->c_index = 0;
1706 ring->free_bds = size;
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001707 ring->write_ptr = start_ptr;
1708 ring->cb_ptr = start_ptr;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001709 ring->end_ptr = end_ptr - 1;
1710 ring->prod_index = 0;
1711
1712 /* Set flow period for ring != 16 */
1713 if (index != DESC_INDEX)
1714 flow_period_val = ENET_MAX_MTU_SIZE << 16;
1715
1716 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
1717 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
1718 bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
1719 /* Disable rate control for now */
1720 bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001721 TDMA_FLOW_PERIOD);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001722 bcmgenet_tdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001723 ((size << DMA_RING_SIZE_SHIFT) |
1724 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001725
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001726 /* Set start and end address, read and write pointers */
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001727 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001728 DMA_START_ADDR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001729 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001730 TDMA_READ_PTR);
Petri Gynther4f8b2d72015-02-23 11:00:45 -08001731 bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001732 TDMA_WRITE_PTR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001733 bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001734 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001735}
1736
1737/* Initialize a RDMA ring */
1738static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001739 unsigned int index, unsigned int size)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001740{
1741 u32 words_per_bd = WORDS_PER_BD(priv);
1742 int ret;
1743
1744 priv->num_rx_bds = TOTAL_DESC;
1745 priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
1746 priv->rx_bd_assign_ptr = priv->rx_bds;
1747 priv->rx_bd_assign_index = 0;
1748 priv->rx_c_index = 0;
1749 priv->rx_read_ptr = 0;
Florian Fainellic489be02014-07-23 10:42:15 -07001750 priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct enet_cb),
1751 GFP_KERNEL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001752 if (!priv->rx_cbs)
1753 return -ENOMEM;
1754
1755 ret = bcmgenet_alloc_rx_buffers(priv);
1756 if (ret) {
1757 kfree(priv->rx_cbs);
1758 return ret;
1759 }
1760
1761 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_WRITE_PTR);
1762 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
1763 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
1764 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001765 ((size << DMA_RING_SIZE_SHIFT) |
1766 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001767 bcmgenet_rdma_ring_writel(priv, index, 0, DMA_START_ADDR);
1768 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001769 words_per_bd * size - 1, DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001770 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001771 (DMA_FC_THRESH_LO <<
1772 DMA_XOFF_THRESHOLD_SHIFT) |
1773 DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001774 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_READ_PTR);
1775
1776 return ret;
1777}
1778
Petri Gynther16c6d662015-02-23 11:00:45 -08001779/* Initialize Tx queues
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001780 *
Petri Gynther16c6d662015-02-23 11:00:45 -08001781 * Queues 0-3 are priority-based, each one has 32 descriptors,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001782 * with queue 0 being the highest priority queue.
1783 *
Petri Gynther16c6d662015-02-23 11:00:45 -08001784 * Queue 16 is the default Tx queue with
Petri Gynther51a966a2015-02-23 11:00:46 -08001785 * GENET_Q16_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors.
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001786 *
Petri Gynther16c6d662015-02-23 11:00:45 -08001787 * The transmit control block pool is then partitioned as follows:
1788 * - Tx queue 0 uses tx_cbs[0..31]
1789 * - Tx queue 1 uses tx_cbs[32..63]
1790 * - Tx queue 2 uses tx_cbs[64..95]
1791 * - Tx queue 3 uses tx_cbs[96..127]
1792 * - Tx queue 16 uses tx_cbs[128..255]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001793 */
Petri Gynther16c6d662015-02-23 11:00:45 -08001794static void bcmgenet_init_tx_queues(struct net_device *dev)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001795{
1796 struct bcmgenet_priv *priv = netdev_priv(dev);
Petri Gynther16c6d662015-02-23 11:00:45 -08001797 u32 i, dma_enable;
1798 u32 dma_ctrl, ring_cfg;
Petri Gynther37742162014-10-07 09:30:01 -07001799 u32 dma_priority[3] = {0, 0, 0};
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001800
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001801 dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
1802 dma_enable = dma_ctrl & DMA_EN;
1803 dma_ctrl &= ~DMA_EN;
1804 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
1805
Petri Gynther16c6d662015-02-23 11:00:45 -08001806 dma_ctrl = 0;
1807 ring_cfg = 0;
1808
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001809 /* Enable strict priority arbiter mode */
1810 bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
1811
Petri Gynther16c6d662015-02-23 11:00:45 -08001812 /* Initialize Tx priority queues */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001813 for (i = 0; i < priv->hw_params->tx_queues; i++) {
Petri Gynther51a966a2015-02-23 11:00:46 -08001814 bcmgenet_init_tx_ring(priv, i, priv->hw_params->tx_bds_per_q,
1815 i * priv->hw_params->tx_bds_per_q,
1816 (i + 1) * priv->hw_params->tx_bds_per_q);
Petri Gynther16c6d662015-02-23 11:00:45 -08001817 ring_cfg |= (1 << i);
1818 dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07001819 dma_priority[DMA_PRIO_REG_INDEX(i)] |=
1820 ((GENET_Q0_PRIORITY + i) << DMA_PRIO_REG_SHIFT(i));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001821 }
1822
Petri Gynther16c6d662015-02-23 11:00:45 -08001823 /* Initialize Tx default queue 16 */
Petri Gynther51a966a2015-02-23 11:00:46 -08001824 bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_Q16_TX_BD_CNT,
Petri Gynther16c6d662015-02-23 11:00:45 -08001825 priv->hw_params->tx_queues *
Petri Gynther51a966a2015-02-23 11:00:46 -08001826 priv->hw_params->tx_bds_per_q,
Petri Gynther16c6d662015-02-23 11:00:45 -08001827 TOTAL_DESC);
1828 ring_cfg |= (1 << DESC_INDEX);
1829 dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
Petri Gynther37742162014-10-07 09:30:01 -07001830 dma_priority[DMA_PRIO_REG_INDEX(DESC_INDEX)] |=
1831 ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) <<
1832 DMA_PRIO_REG_SHIFT(DESC_INDEX));
Petri Gynther16c6d662015-02-23 11:00:45 -08001833
1834 /* Set Tx queue priorities */
Petri Gynther37742162014-10-07 09:30:01 -07001835 bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
1836 bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
1837 bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);
1838
Petri Gynther16c6d662015-02-23 11:00:45 -08001839 /* Enable Tx queues */
1840 bcmgenet_tdma_writel(priv, ring_cfg, DMA_RING_CFG);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001841
Petri Gynther16c6d662015-02-23 11:00:45 -08001842 /* Enable Tx DMA */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001843 if (dma_enable)
Petri Gynther16c6d662015-02-23 11:00:45 -08001844 dma_ctrl |= DMA_EN;
1845 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001846}
1847
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07001848static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
1849{
1850 int ret = 0;
1851 int timeout = 0;
1852 u32 reg;
1853
1854 /* Disable TDMA to stop add more frames in TX DMA */
1855 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
1856 reg &= ~DMA_EN;
1857 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
1858
1859 /* Check TDMA status register to confirm TDMA is disabled */
1860 while (timeout++ < DMA_TIMEOUT_VAL) {
1861 reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
1862 if (reg & DMA_DISABLED)
1863 break;
1864
1865 udelay(1);
1866 }
1867
1868 if (timeout == DMA_TIMEOUT_VAL) {
1869 netdev_warn(priv->dev, "Timed out while disabling TX DMA\n");
1870 ret = -ETIMEDOUT;
1871 }
1872
1873 /* Wait 10ms for packet drain in both tx and rx dma */
1874 usleep_range(10000, 20000);
1875
1876 /* Disable RDMA */
1877 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
1878 reg &= ~DMA_EN;
1879 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
1880
1881 timeout = 0;
1882 /* Check RDMA status register to confirm RDMA is disabled */
1883 while (timeout++ < DMA_TIMEOUT_VAL) {
1884 reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
1885 if (reg & DMA_DISABLED)
1886 break;
1887
1888 udelay(1);
1889 }
1890
1891 if (timeout == DMA_TIMEOUT_VAL) {
1892 netdev_warn(priv->dev, "Timed out while disabling RX DMA\n");
1893 ret = -ETIMEDOUT;
1894 }
1895
1896 return ret;
1897}
1898
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001899static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
1900{
1901 int i;
1902
1903 /* disable DMA */
Florian Fainelli4a0c081e2014-09-22 11:54:43 -07001904 bcmgenet_dma_teardown(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001905
1906 for (i = 0; i < priv->num_tx_bds; i++) {
1907 if (priv->tx_cbs[i].skb != NULL) {
1908 dev_kfree_skb(priv->tx_cbs[i].skb);
1909 priv->tx_cbs[i].skb = NULL;
1910 }
1911 }
1912
1913 bcmgenet_free_rx_buffers(priv);
1914 kfree(priv->rx_cbs);
1915 kfree(priv->tx_cbs);
1916}
1917
1918/* init_edma: Initialize DMA control register */
1919static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
1920{
1921 int ret;
Petri Gynther014012a2015-02-23 11:00:45 -08001922 unsigned int i;
1923 struct enet_cb *cb;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001924
1925 netif_dbg(priv, hw, priv->dev, "bcmgenet: init_edma\n");
1926
1927 /* by default, enable ring 16 (descriptor based) */
1928 ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, TOTAL_DESC);
1929 if (ret) {
1930 netdev_err(priv->dev, "failed to initialize RX ring\n");
1931 return ret;
1932 }
1933
1934 /* init rDma */
1935 bcmgenet_rdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
1936
1937 /* Init tDma */
1938 bcmgenet_tdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
1939
Brian Norris7fc527f2014-07-29 14:34:14 -07001940 /* Initialize common TX ring structures */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001941 priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
1942 priv->num_tx_bds = TOTAL_DESC;
Florian Fainellic489be02014-07-23 10:42:15 -07001943 priv->tx_cbs = kcalloc(priv->num_tx_bds, sizeof(struct enet_cb),
Florian Fainellic91b7f62014-07-23 10:42:12 -07001944 GFP_KERNEL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001945 if (!priv->tx_cbs) {
1946 bcmgenet_fini_dma(priv);
1947 return -ENOMEM;
1948 }
1949
Petri Gynther014012a2015-02-23 11:00:45 -08001950 for (i = 0; i < priv->num_tx_bds; i++) {
1951 cb = priv->tx_cbs + i;
1952 cb->bd_addr = priv->tx_bds + i * DMA_DESC_SIZE;
1953 }
1954
Petri Gynther16c6d662015-02-23 11:00:45 -08001955 /* Initialize Tx queues */
1956 bcmgenet_init_tx_queues(priv->dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001957
1958 return 0;
1959}
1960
1961/* NAPI polling method*/
1962static int bcmgenet_poll(struct napi_struct *napi, int budget)
1963{
1964 struct bcmgenet_priv *priv = container_of(napi,
1965 struct bcmgenet_priv, napi);
1966 unsigned int work_done;
1967
1968 /* tx reclaim */
1969 bcmgenet_tx_reclaim(priv->dev, &priv->tx_rings[DESC_INDEX]);
1970
1971 work_done = bcmgenet_desc_rx(priv, budget);
1972
1973 /* Advancing our consumer index*/
1974 priv->rx_c_index += work_done;
1975 priv->rx_c_index &= DMA_C_INDEX_MASK;
1976 bcmgenet_rdma_ring_writel(priv, DESC_INDEX,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001977 priv->rx_c_index, RDMA_CONS_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001978 if (work_done < budget) {
1979 napi_complete(napi);
Florian Fainellic91b7f62014-07-23 10:42:12 -07001980 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_RXDMA_BDONE,
1981 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001982 }
1983
1984 return work_done;
1985}
1986
1987/* Interrupt bottom half */
1988static void bcmgenet_irq_task(struct work_struct *work)
1989{
1990 struct bcmgenet_priv *priv = container_of(
1991 work, struct bcmgenet_priv, bcmgenet_irq_work);
1992
1993 netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
1994
Florian Fainelli8fdb0e02014-07-21 15:29:26 -07001995 if (priv->irq0_stat & UMAC_IRQ_MPD_R) {
1996 priv->irq0_stat &= ~UMAC_IRQ_MPD_R;
1997 netif_dbg(priv, wol, priv->dev,
1998 "magic packet detected, waking up\n");
1999 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
2000 }
2001
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002002 /* Link UP/DOWN event */
2003 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07002004 (priv->irq0_stat & (UMAC_IRQ_LINK_UP|UMAC_IRQ_LINK_DOWN))) {
Florian Fainelli80d8e962014-02-24 16:56:11 -08002005 phy_mac_interrupt(priv->phydev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002006 priv->irq0_stat & UMAC_IRQ_LINK_UP);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002007 priv->irq0_stat &= ~(UMAC_IRQ_LINK_UP|UMAC_IRQ_LINK_DOWN);
2008 }
2009}
2010
2011/* bcmgenet_isr1: interrupt handler for ring buffer. */
2012static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
2013{
2014 struct bcmgenet_priv *priv = dev_id;
2015 unsigned int index;
2016
2017 /* Save irq status for bottom-half processing. */
2018 priv->irq1_stat =
2019 bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
2020 ~priv->int1_mask;
Brian Norris7fc527f2014-07-29 14:34:14 -07002021 /* clear interrupts */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002022 bcmgenet_intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
2023
2024 netif_dbg(priv, intr, priv->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002025 "%s: IRQ=0x%x\n", __func__, priv->irq1_stat);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002026 /* Check the MBDONE interrupts.
2027 * packet is done, reclaim descriptors
2028 */
2029 if (priv->irq1_stat & 0x0000ffff) {
2030 index = 0;
2031 for (index = 0; index < 16; index++) {
2032 if (priv->irq1_stat & (1 << index))
2033 bcmgenet_tx_reclaim(priv->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002034 &priv->tx_rings[index]);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002035 }
2036 }
2037 return IRQ_HANDLED;
2038}
2039
2040/* bcmgenet_isr0: Handle various interrupts. */
2041static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
2042{
2043 struct bcmgenet_priv *priv = dev_id;
2044
2045 /* Save irq status for bottom-half processing. */
2046 priv->irq0_stat =
2047 bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
2048 ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
Brian Norris7fc527f2014-07-29 14:34:14 -07002049 /* clear interrupts */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002050 bcmgenet_intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
2051
2052 netif_dbg(priv, intr, priv->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002053 "IRQ=0x%x\n", priv->irq0_stat);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002054
2055 if (priv->irq0_stat & (UMAC_IRQ_RXDMA_BDONE | UMAC_IRQ_RXDMA_PDONE)) {
2056 /* We use NAPI(software interrupt throttling, if
2057 * Rx Descriptor throttling is not used.
2058 * Disable interrupt, will be enabled in the poll method.
2059 */
2060 if (likely(napi_schedule_prep(&priv->napi))) {
Florian Fainellic91b7f62014-07-23 10:42:12 -07002061 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_RXDMA_BDONE,
2062 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002063 __napi_schedule(&priv->napi);
2064 }
2065 }
2066 if (priv->irq0_stat &
2067 (UMAC_IRQ_TXDMA_BDONE | UMAC_IRQ_TXDMA_PDONE)) {
2068 /* Tx reclaim */
2069 bcmgenet_tx_reclaim(priv->dev, &priv->tx_rings[DESC_INDEX]);
2070 }
2071 if (priv->irq0_stat & (UMAC_IRQ_PHY_DET_R |
2072 UMAC_IRQ_PHY_DET_F |
2073 UMAC_IRQ_LINK_UP |
2074 UMAC_IRQ_LINK_DOWN |
2075 UMAC_IRQ_HFB_SM |
2076 UMAC_IRQ_HFB_MM |
2077 UMAC_IRQ_MPD_R)) {
2078 /* all other interested interrupts handled in bottom half */
2079 schedule_work(&priv->bcmgenet_irq_work);
2080 }
2081
2082 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07002083 priv->irq0_stat & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002084 priv->irq0_stat &= ~(UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
2085 wake_up(&priv->wq);
2086 }
2087
2088 return IRQ_HANDLED;
2089}
2090
Florian Fainelli85620562014-07-21 15:29:23 -07002091static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
2092{
2093 struct bcmgenet_priv *priv = dev_id;
2094
2095 pm_wakeup_event(&priv->pdev->dev, 0);
2096
2097 return IRQ_HANDLED;
2098}
2099
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002100static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
2101{
2102 u32 reg;
2103
2104 reg = bcmgenet_rbuf_ctrl_get(priv);
2105 reg |= BIT(1);
2106 bcmgenet_rbuf_ctrl_set(priv, reg);
2107 udelay(10);
2108
2109 reg &= ~BIT(1);
2110 bcmgenet_rbuf_ctrl_set(priv, reg);
2111 udelay(10);
2112}
2113
2114static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002115 unsigned char *addr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002116{
2117 bcmgenet_umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
2118 (addr[2] << 8) | addr[3], UMAC_MAC0);
2119 bcmgenet_umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
2120}
2121
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002122/* Returns a reusable dma control register value */
2123static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
2124{
2125 u32 reg;
2126 u32 dma_ctrl;
2127
2128 /* disable DMA */
2129 dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
2130 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2131 reg &= ~dma_ctrl;
2132 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2133
2134 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2135 reg &= ~dma_ctrl;
2136 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2137
2138 bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
2139 udelay(10);
2140 bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
2141
2142 return dma_ctrl;
2143}
2144
2145static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
2146{
2147 u32 reg;
2148
2149 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2150 reg |= dma_ctrl;
2151 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2152
2153 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2154 reg |= dma_ctrl;
2155 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2156}
2157
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002158static void bcmgenet_netif_start(struct net_device *dev)
2159{
2160 struct bcmgenet_priv *priv = netdev_priv(dev);
2161
2162 /* Start the network engine */
2163 napi_enable(&priv->napi);
2164
2165 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
2166
2167 if (phy_is_internal(priv->phydev))
2168 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
2169
2170 netif_tx_start_all_queues(dev);
2171
2172 phy_start(priv->phydev);
2173}
2174
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002175static int bcmgenet_open(struct net_device *dev)
2176{
2177 struct bcmgenet_priv *priv = netdev_priv(dev);
2178 unsigned long dma_ctrl;
2179 u32 reg;
2180 int ret;
2181
2182 netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
2183
2184 /* Turn on the clock */
2185 if (!IS_ERR(priv->clk))
2186 clk_prepare_enable(priv->clk);
2187
2188 /* take MAC out of reset */
2189 bcmgenet_umac_reset(priv);
2190
2191 ret = init_umac(priv);
2192 if (ret)
2193 goto err_clk_disable;
2194
2195 /* disable ethernet MAC while updating its registers */
Florian Fainellie29585b2014-07-21 15:29:20 -07002196 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002197
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002198 /* Make sure we reflect the value of CRC_CMD_FWD */
2199 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
2200 priv->crc_fwd_en = !!(reg & CMD_CRC_FWD);
2201
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002202 bcmgenet_set_hw_addr(priv, dev->dev_addr);
2203
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002204 if (phy_is_internal(priv->phydev)) {
2205 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
2206 reg |= EXT_ENERGY_DET_MASK;
2207 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
2208 }
2209
2210 /* Disable RX/TX DMA and flush TX queues */
2211 dma_ctrl = bcmgenet_dma_disable(priv);
2212
2213 /* Reinitialize TDMA and RDMA and SW housekeeping */
2214 ret = bcmgenet_init_dma(priv);
2215 if (ret) {
2216 netdev_err(dev, "failed to initialize DMA\n");
2217 goto err_fini_dma;
2218 }
2219
2220 /* Always enable ring 16 - descriptor ring */
2221 bcmgenet_enable_dma(priv, dma_ctrl);
2222
2223 ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002224 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002225 if (ret < 0) {
2226 netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
2227 goto err_fini_dma;
2228 }
2229
2230 ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002231 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002232 if (ret < 0) {
2233 netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
2234 goto err_irq0;
2235 }
2236
Florian Fainellidbd479d2014-11-10 18:06:21 -08002237 /* Re-configure the port multiplexer towards the PHY device */
2238 bcmgenet_mii_config(priv->dev, false);
2239
Florian Fainellic96e7312014-11-10 18:06:20 -08002240 phy_connect_direct(dev, priv->phydev, bcmgenet_mii_setup,
2241 priv->phy_interface);
2242
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002243 bcmgenet_netif_start(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002244
2245 return 0;
2246
2247err_irq0:
2248 free_irq(priv->irq0, dev);
2249err_fini_dma:
2250 bcmgenet_fini_dma(priv);
2251err_clk_disable:
2252 if (!IS_ERR(priv->clk))
2253 clk_disable_unprepare(priv->clk);
2254 return ret;
2255}
2256
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002257static void bcmgenet_netif_stop(struct net_device *dev)
2258{
2259 struct bcmgenet_priv *priv = netdev_priv(dev);
2260
2261 netif_tx_stop_all_queues(dev);
2262 napi_disable(&priv->napi);
2263 phy_stop(priv->phydev);
2264
2265 bcmgenet_intr_disable(priv);
2266
2267 /* Wait for pending work items to complete. Since interrupts are
2268 * disabled no new work will be scheduled.
2269 */
2270 cancel_work_sync(&priv->bcmgenet_irq_work);
Florian Fainellicc013fb2014-08-11 14:50:43 -07002271
Florian Fainellicc013fb2014-08-11 14:50:43 -07002272 priv->old_link = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002273 priv->old_speed = -1;
Florian Fainellicc013fb2014-08-11 14:50:43 -07002274 priv->old_duplex = -1;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -07002275 priv->old_pause = -1;
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002276}
2277
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002278static int bcmgenet_close(struct net_device *dev)
2279{
2280 struct bcmgenet_priv *priv = netdev_priv(dev);
2281 int ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002282
2283 netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
2284
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002285 bcmgenet_netif_stop(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002286
Florian Fainellic96e7312014-11-10 18:06:20 -08002287 /* Really kill the PHY state machine and disconnect from it */
2288 phy_disconnect(priv->phydev);
2289
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002290 /* Disable MAC receive */
Florian Fainellie29585b2014-07-21 15:29:20 -07002291 umac_enable_set(priv, CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002292
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002293 ret = bcmgenet_dma_teardown(priv);
2294 if (ret)
2295 return ret;
2296
2297 /* Disable MAC transmit. TX DMA disabled have to done before this */
Florian Fainellie29585b2014-07-21 15:29:20 -07002298 umac_enable_set(priv, CMD_TX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002299
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002300 /* tx reclaim */
2301 bcmgenet_tx_reclaim_all(dev);
2302 bcmgenet_fini_dma(priv);
2303
2304 free_irq(priv->irq0, priv);
2305 free_irq(priv->irq1, priv);
2306
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002307 if (phy_is_internal(priv->phydev))
2308 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
2309
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002310 if (!IS_ERR(priv->clk))
2311 clk_disable_unprepare(priv->clk);
2312
2313 return 0;
2314}
2315
2316static void bcmgenet_timeout(struct net_device *dev)
2317{
2318 struct bcmgenet_priv *priv = netdev_priv(dev);
2319
2320 netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
2321
2322 dev->trans_start = jiffies;
2323
2324 dev->stats.tx_errors++;
2325
2326 netif_tx_wake_all_queues(dev);
2327}
2328
2329#define MAX_MC_COUNT 16
2330
2331static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
2332 unsigned char *addr,
2333 int *i,
2334 int *mc)
2335{
2336 u32 reg;
2337
Florian Fainellic91b7f62014-07-23 10:42:12 -07002338 bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
2339 UMAC_MDF_ADDR + (*i * 4));
2340 bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
2341 addr[4] << 8 | addr[5],
2342 UMAC_MDF_ADDR + ((*i + 1) * 4));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002343 reg = bcmgenet_umac_readl(priv, UMAC_MDF_CTRL);
2344 reg |= (1 << (MAX_MC_COUNT - *mc));
2345 bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
2346 *i += 2;
2347 (*mc)++;
2348}
2349
2350static void bcmgenet_set_rx_mode(struct net_device *dev)
2351{
2352 struct bcmgenet_priv *priv = netdev_priv(dev);
2353 struct netdev_hw_addr *ha;
2354 int i, mc;
2355 u32 reg;
2356
2357 netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
2358
Brian Norris7fc527f2014-07-29 14:34:14 -07002359 /* Promiscuous mode */
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002360 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
2361 if (dev->flags & IFF_PROMISC) {
2362 reg |= CMD_PROMISC;
2363 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
2364 bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
2365 return;
2366 } else {
2367 reg &= ~CMD_PROMISC;
2368 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
2369 }
2370
2371 /* UniMac doesn't support ALLMULTI */
2372 if (dev->flags & IFF_ALLMULTI) {
2373 netdev_warn(dev, "ALLMULTI is not supported\n");
2374 return;
2375 }
2376
2377 /* update MDF filter */
2378 i = 0;
2379 mc = 0;
2380 /* Broadcast */
2381 bcmgenet_set_mdf_addr(priv, dev->broadcast, &i, &mc);
2382 /* my own address.*/
2383 bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i, &mc);
2384 /* Unicast list*/
2385 if (netdev_uc_count(dev) > (MAX_MC_COUNT - mc))
2386 return;
2387
2388 if (!netdev_uc_empty(dev))
2389 netdev_for_each_uc_addr(ha, dev)
2390 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
2391 /* Multicast */
2392 if (netdev_mc_empty(dev) || netdev_mc_count(dev) >= (MAX_MC_COUNT - mc))
2393 return;
2394
2395 netdev_for_each_mc_addr(ha, dev)
2396 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
2397}
2398
2399/* Set the hardware MAC address. */
2400static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
2401{
2402 struct sockaddr *addr = p;
2403
2404 /* Setting the MAC address at the hardware level is not possible
2405 * without disabling the UniMAC RX/TX enable bits.
2406 */
2407 if (netif_running(dev))
2408 return -EBUSY;
2409
2410 ether_addr_copy(dev->dev_addr, addr->sa_data);
2411
2412 return 0;
2413}
2414
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002415static const struct net_device_ops bcmgenet_netdev_ops = {
2416 .ndo_open = bcmgenet_open,
2417 .ndo_stop = bcmgenet_close,
2418 .ndo_start_xmit = bcmgenet_xmit,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002419 .ndo_tx_timeout = bcmgenet_timeout,
2420 .ndo_set_rx_mode = bcmgenet_set_rx_mode,
2421 .ndo_set_mac_address = bcmgenet_set_mac_addr,
2422 .ndo_do_ioctl = bcmgenet_ioctl,
2423 .ndo_set_features = bcmgenet_set_features,
2424};
2425
2426/* Array of GENET hardware parameters/characteristics */
2427static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
2428 [GENET_V1] = {
2429 .tx_queues = 0,
Petri Gynther51a966a2015-02-23 11:00:46 -08002430 .tx_bds_per_q = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002431 .rx_queues = 0,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002432 .bp_in_en_shift = 16,
2433 .bp_in_mask = 0xffff,
2434 .hfb_filter_cnt = 16,
2435 .qtag_mask = 0x1F,
2436 .hfb_offset = 0x1000,
2437 .rdma_offset = 0x2000,
2438 .tdma_offset = 0x3000,
2439 .words_per_bd = 2,
2440 },
2441 [GENET_V2] = {
2442 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08002443 .tx_bds_per_q = 32,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002444 .rx_queues = 4,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002445 .bp_in_en_shift = 16,
2446 .bp_in_mask = 0xffff,
2447 .hfb_filter_cnt = 16,
2448 .qtag_mask = 0x1F,
2449 .tbuf_offset = 0x0600,
2450 .hfb_offset = 0x1000,
2451 .hfb_reg_offset = 0x2000,
2452 .rdma_offset = 0x3000,
2453 .tdma_offset = 0x4000,
2454 .words_per_bd = 2,
2455 .flags = GENET_HAS_EXT,
2456 },
2457 [GENET_V3] = {
2458 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08002459 .tx_bds_per_q = 32,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002460 .rx_queues = 4,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002461 .bp_in_en_shift = 17,
2462 .bp_in_mask = 0x1ffff,
2463 .hfb_filter_cnt = 48,
2464 .qtag_mask = 0x3F,
2465 .tbuf_offset = 0x0600,
2466 .hfb_offset = 0x8000,
2467 .hfb_reg_offset = 0xfc00,
2468 .rdma_offset = 0x10000,
2469 .tdma_offset = 0x11000,
2470 .words_per_bd = 2,
2471 .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR,
2472 },
2473 [GENET_V4] = {
2474 .tx_queues = 4,
Petri Gynther51a966a2015-02-23 11:00:46 -08002475 .tx_bds_per_q = 32,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002476 .rx_queues = 4,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002477 .bp_in_en_shift = 17,
2478 .bp_in_mask = 0x1ffff,
2479 .hfb_filter_cnt = 48,
2480 .qtag_mask = 0x3F,
2481 .tbuf_offset = 0x0600,
2482 .hfb_offset = 0x8000,
2483 .hfb_reg_offset = 0xfc00,
2484 .rdma_offset = 0x2000,
2485 .tdma_offset = 0x4000,
2486 .words_per_bd = 3,
2487 .flags = GENET_HAS_40BITS | GENET_HAS_EXT | GENET_HAS_MDIO_INTR,
2488 },
2489};
2490
2491/* Infer hardware parameters from the detected GENET version */
2492static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
2493{
2494 struct bcmgenet_hw_params *params;
2495 u32 reg;
2496 u8 major;
Florian Fainellib04a2f52014-12-03 09:56:59 -08002497 u16 gphy_rev;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002498
2499 if (GENET_IS_V4(priv)) {
2500 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
2501 genet_dma_ring_regs = genet_dma_ring_regs_v4;
2502 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
2503 priv->version = GENET_V4;
2504 } else if (GENET_IS_V3(priv)) {
2505 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
2506 genet_dma_ring_regs = genet_dma_ring_regs_v123;
2507 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
2508 priv->version = GENET_V3;
2509 } else if (GENET_IS_V2(priv)) {
2510 bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
2511 genet_dma_ring_regs = genet_dma_ring_regs_v123;
2512 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
2513 priv->version = GENET_V2;
2514 } else if (GENET_IS_V1(priv)) {
2515 bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
2516 genet_dma_ring_regs = genet_dma_ring_regs_v123;
2517 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
2518 priv->version = GENET_V1;
2519 }
2520
2521 /* enum genet_version starts at 1 */
2522 priv->hw_params = &bcmgenet_hw_params[priv->version];
2523 params = priv->hw_params;
2524
2525 /* Read GENET HW version */
2526 reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
2527 major = (reg >> 24 & 0x0f);
2528 if (major == 5)
2529 major = 4;
2530 else if (major == 0)
2531 major = 1;
2532 if (major != priv->version) {
2533 dev_err(&priv->pdev->dev,
2534 "GENET version mismatch, got: %d, configured for: %d\n",
2535 major, priv->version);
2536 }
2537
2538 /* Print the GENET core version */
2539 dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002540 major, (reg >> 16) & 0x0f, reg & 0xffff);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002541
Florian Fainelli487320c2014-09-19 13:07:53 -07002542 /* Store the integrated PHY revision for the MDIO probing function
2543 * to pass this information to the PHY driver. The PHY driver expects
2544 * to find the PHY major revision in bits 15:8 while the GENET register
2545 * stores that information in bits 7:0, account for that.
Florian Fainellib04a2f52014-12-03 09:56:59 -08002546 *
2547 * On newer chips, starting with PHY revision G0, a new scheme is
2548 * deployed similar to the Starfighter 2 switch with GPHY major
2549 * revision in bits 15:8 and patch level in bits 7:0. Major revision 0
2550 * is reserved as well as special value 0x01ff, we have a small
2551 * heuristic to check for the new GPHY revision and re-arrange things
2552 * so the GPHY driver is happy.
Florian Fainelli487320c2014-09-19 13:07:53 -07002553 */
Florian Fainellib04a2f52014-12-03 09:56:59 -08002554 gphy_rev = reg & 0xffff;
2555
2556 /* This is the good old scheme, just GPHY major, no minor nor patch */
2557 if ((gphy_rev & 0xf0) != 0)
2558 priv->gphy_rev = gphy_rev << 8;
2559
2560 /* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */
2561 else if ((gphy_rev & 0xff00) != 0)
2562 priv->gphy_rev = gphy_rev;
2563
2564 /* This is reserved so should require special treatment */
2565 else if (gphy_rev == 0 || gphy_rev == 0x01ff) {
2566 pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev);
2567 return;
2568 }
Florian Fainelli487320c2014-09-19 13:07:53 -07002569
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002570#ifdef CONFIG_PHYS_ADDR_T_64BIT
2571 if (!(params->flags & GENET_HAS_40BITS))
2572 pr_warn("GENET does not support 40-bits PA\n");
2573#endif
2574
2575 pr_debug("Configuration for version: %d\n"
Petri Gynther51a966a2015-02-23 11:00:46 -08002576 "TXq: %1d, TXqBDs: %1d, RXq: %1d\n"
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002577 "BP << en: %2d, BP msk: 0x%05x\n"
2578 "HFB count: %2d, QTAQ msk: 0x%05x\n"
2579 "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
2580 "RDMA: 0x%05x, TDMA: 0x%05x\n"
2581 "Words/BD: %d\n",
2582 priv->version,
Petri Gynther51a966a2015-02-23 11:00:46 -08002583 params->tx_queues, params->tx_bds_per_q,
2584 params->rx_queues,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002585 params->bp_in_en_shift, params->bp_in_mask,
2586 params->hfb_filter_cnt, params->qtag_mask,
2587 params->tbuf_offset, params->hfb_offset,
2588 params->hfb_reg_offset,
2589 params->rdma_offset, params->tdma_offset,
2590 params->words_per_bd);
2591}
2592
2593static const struct of_device_id bcmgenet_match[] = {
2594 { .compatible = "brcm,genet-v1", .data = (void *)GENET_V1 },
2595 { .compatible = "brcm,genet-v2", .data = (void *)GENET_V2 },
2596 { .compatible = "brcm,genet-v3", .data = (void *)GENET_V3 },
2597 { .compatible = "brcm,genet-v4", .data = (void *)GENET_V4 },
2598 { },
2599};
2600
2601static int bcmgenet_probe(struct platform_device *pdev)
2602{
Petri Gyntherb0ba5122014-12-01 16:18:08 -08002603 struct bcmgenet_platform_data *pd = pdev->dev.platform_data;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002604 struct device_node *dn = pdev->dev.of_node;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08002605 const struct of_device_id *of_id = NULL;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002606 struct bcmgenet_priv *priv;
2607 struct net_device *dev;
2608 const void *macaddr;
2609 struct resource *r;
2610 int err = -EIO;
2611
2612 /* Up to GENET_MAX_MQ_CNT + 1 TX queues and a single RX queue */
2613 dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1, 1);
2614 if (!dev) {
2615 dev_err(&pdev->dev, "can't allocate net device\n");
2616 return -ENOMEM;
2617 }
2618
Petri Gyntherb0ba5122014-12-01 16:18:08 -08002619 if (dn) {
2620 of_id = of_match_node(bcmgenet_match, dn);
2621 if (!of_id)
2622 return -EINVAL;
2623 }
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002624
2625 priv = netdev_priv(dev);
2626 priv->irq0 = platform_get_irq(pdev, 0);
2627 priv->irq1 = platform_get_irq(pdev, 1);
Florian Fainelli85620562014-07-21 15:29:23 -07002628 priv->wol_irq = platform_get_irq(pdev, 2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002629 if (!priv->irq0 || !priv->irq1) {
2630 dev_err(&pdev->dev, "can't find IRQs\n");
2631 err = -EINVAL;
2632 goto err;
2633 }
2634
Petri Gyntherb0ba5122014-12-01 16:18:08 -08002635 if (dn) {
2636 macaddr = of_get_mac_address(dn);
2637 if (!macaddr) {
2638 dev_err(&pdev->dev, "can't find MAC address\n");
2639 err = -EINVAL;
2640 goto err;
2641 }
2642 } else {
2643 macaddr = pd->mac_address;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002644 }
2645
2646 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam5343a102014-02-24 00:47:24 -03002647 priv->base = devm_ioremap_resource(&pdev->dev, r);
2648 if (IS_ERR(priv->base)) {
2649 err = PTR_ERR(priv->base);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002650 goto err;
2651 }
2652
2653 SET_NETDEV_DEV(dev, &pdev->dev);
2654 dev_set_drvdata(&pdev->dev, dev);
2655 ether_addr_copy(dev->dev_addr, macaddr);
2656 dev->watchdog_timeo = 2 * HZ;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002657 dev->ethtool_ops = &bcmgenet_ethtool_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002658 dev->netdev_ops = &bcmgenet_netdev_ops;
2659 netif_napi_add(dev, &priv->napi, bcmgenet_poll, 64);
2660
2661 priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
2662
2663 /* Set hardware features */
2664 dev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM |
2665 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
2666
Florian Fainelli85620562014-07-21 15:29:23 -07002667 /* Request the WOL interrupt and advertise suspend if available */
2668 priv->wol_irq_disabled = true;
2669 err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0,
2670 dev->name, priv);
2671 if (!err)
2672 device_set_wakeup_capable(&pdev->dev, 1);
2673
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002674 /* Set the needed headroom to account for any possible
2675 * features enabling/disabling at runtime
2676 */
2677 dev->needed_headroom += 64;
2678
2679 netdev_boot_setup_check(dev);
2680
2681 priv->dev = dev;
2682 priv->pdev = pdev;
Petri Gyntherb0ba5122014-12-01 16:18:08 -08002683 if (of_id)
2684 priv->version = (enum bcmgenet_version)of_id->data;
2685 else
2686 priv->version = pd->genet_version;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002687
Florian Fainellie4a60a92014-08-11 14:50:42 -07002688 priv->clk = devm_clk_get(&priv->pdev->dev, "enet");
2689 if (IS_ERR(priv->clk))
2690 dev_warn(&priv->pdev->dev, "failed to get enet clock\n");
2691
2692 if (!IS_ERR(priv->clk))
2693 clk_prepare_enable(priv->clk);
2694
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002695 bcmgenet_set_hw_params(priv);
2696
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002697 /* Mii wait queue */
2698 init_waitqueue_head(&priv->wq);
2699 /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
2700 priv->rx_buf_len = RX_BUF_LENGTH;
2701 INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
2702
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002703 priv->clk_wol = devm_clk_get(&priv->pdev->dev, "enet-wol");
2704 if (IS_ERR(priv->clk_wol))
2705 dev_warn(&priv->pdev->dev, "failed to get enet-wol clock\n");
2706
Florian Fainelli6ef398e2014-11-25 21:16:35 -08002707 priv->clk_eee = devm_clk_get(&priv->pdev->dev, "enet-eee");
2708 if (IS_ERR(priv->clk_eee)) {
2709 dev_warn(&priv->pdev->dev, "failed to get enet-eee clock\n");
2710 priv->clk_eee = NULL;
2711 }
2712
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002713 err = reset_umac(priv);
2714 if (err)
2715 goto err_clk_disable;
2716
2717 err = bcmgenet_mii_init(dev);
2718 if (err)
2719 goto err_clk_disable;
2720
2721 /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
2722 * just the ring 16 descriptor based TX
2723 */
2724 netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
2725 netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
2726
Florian Fainelli219575e2014-06-26 10:26:21 -07002727 /* libphy will determine the link state */
2728 netif_carrier_off(dev);
2729
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002730 /* Turn off the main clock, WOL clock is handled separately */
2731 if (!IS_ERR(priv->clk))
2732 clk_disable_unprepare(priv->clk);
2733
Florian Fainelli0f50ce92014-06-26 10:26:20 -07002734 err = register_netdev(dev);
2735 if (err)
2736 goto err;
2737
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002738 return err;
2739
2740err_clk_disable:
2741 if (!IS_ERR(priv->clk))
2742 clk_disable_unprepare(priv->clk);
2743err:
2744 free_netdev(dev);
2745 return err;
2746}
2747
2748static int bcmgenet_remove(struct platform_device *pdev)
2749{
2750 struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
2751
2752 dev_set_drvdata(&pdev->dev, NULL);
2753 unregister_netdev(priv->dev);
2754 bcmgenet_mii_exit(priv->dev);
2755 free_netdev(priv->dev);
2756
2757 return 0;
2758}
2759
Florian Fainellib6e978e2014-07-21 15:29:22 -07002760#ifdef CONFIG_PM_SLEEP
2761static int bcmgenet_suspend(struct device *d)
2762{
2763 struct net_device *dev = dev_get_drvdata(d);
2764 struct bcmgenet_priv *priv = netdev_priv(dev);
2765 int ret;
2766
2767 if (!netif_running(dev))
2768 return 0;
2769
2770 bcmgenet_netif_stop(dev);
2771
Florian Fainellicc013fb2014-08-11 14:50:43 -07002772 phy_suspend(priv->phydev);
2773
Florian Fainellib6e978e2014-07-21 15:29:22 -07002774 netif_device_detach(dev);
2775
2776 /* Disable MAC receive */
2777 umac_enable_set(priv, CMD_RX_EN, false);
2778
2779 ret = bcmgenet_dma_teardown(priv);
2780 if (ret)
2781 return ret;
2782
2783 /* Disable MAC transmit. TX DMA disabled have to done before this */
2784 umac_enable_set(priv, CMD_TX_EN, false);
2785
2786 /* tx reclaim */
2787 bcmgenet_tx_reclaim_all(dev);
2788 bcmgenet_fini_dma(priv);
2789
Florian Fainelli8c90db72014-07-21 15:29:28 -07002790 /* Prepare the device for Wake-on-LAN and switch to the slow clock */
2791 if (device_may_wakeup(d) && priv->wolopts) {
2792 bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
2793 clk_prepare_enable(priv->clk_wol);
2794 }
2795
Florian Fainellib6e978e2014-07-21 15:29:22 -07002796 /* Turn off the clocks */
2797 clk_disable_unprepare(priv->clk);
2798
2799 return 0;
2800}
2801
2802static int bcmgenet_resume(struct device *d)
2803{
2804 struct net_device *dev = dev_get_drvdata(d);
2805 struct bcmgenet_priv *priv = netdev_priv(dev);
2806 unsigned long dma_ctrl;
2807 int ret;
2808 u32 reg;
2809
2810 if (!netif_running(dev))
2811 return 0;
2812
2813 /* Turn on the clock */
2814 ret = clk_prepare_enable(priv->clk);
2815 if (ret)
2816 return ret;
2817
2818 bcmgenet_umac_reset(priv);
2819
2820 ret = init_umac(priv);
2821 if (ret)
2822 goto out_clk_disable;
2823
Tobias Klauser0a29b3d2014-09-23 15:19:41 +02002824 /* From WOL-enabled suspend, switch to regular clock */
2825 if (priv->wolopts)
2826 clk_disable_unprepare(priv->clk_wol);
2827
2828 phy_init_hw(priv->phydev);
2829 /* Speed settings must be restored */
Florian Fainellidbd479d2014-11-10 18:06:21 -08002830 bcmgenet_mii_config(priv->dev, false);
Florian Fainelli8c90db72014-07-21 15:29:28 -07002831
Florian Fainellib6e978e2014-07-21 15:29:22 -07002832 /* disable ethernet MAC while updating its registers */
2833 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
2834
2835 bcmgenet_set_hw_addr(priv, dev->dev_addr);
2836
2837 if (phy_is_internal(priv->phydev)) {
2838 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
2839 reg |= EXT_ENERGY_DET_MASK;
2840 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
2841 }
2842
Florian Fainelli98bb7392014-08-11 14:50:45 -07002843 if (priv->wolopts)
2844 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
2845
Florian Fainellib6e978e2014-07-21 15:29:22 -07002846 /* Disable RX/TX DMA and flush TX queues */
2847 dma_ctrl = bcmgenet_dma_disable(priv);
2848
2849 /* Reinitialize TDMA and RDMA and SW housekeeping */
2850 ret = bcmgenet_init_dma(priv);
2851 if (ret) {
2852 netdev_err(dev, "failed to initialize DMA\n");
2853 goto out_clk_disable;
2854 }
2855
2856 /* Always enable ring 16 - descriptor ring */
2857 bcmgenet_enable_dma(priv, dma_ctrl);
2858
2859 netif_device_attach(dev);
2860
Florian Fainellicc013fb2014-08-11 14:50:43 -07002861 phy_resume(priv->phydev);
2862
Florian Fainelli6ef398e2014-11-25 21:16:35 -08002863 if (priv->eee.eee_enabled)
2864 bcmgenet_eee_enable_set(dev, true);
2865
Florian Fainellib6e978e2014-07-21 15:29:22 -07002866 bcmgenet_netif_start(dev);
2867
2868 return 0;
2869
2870out_clk_disable:
2871 clk_disable_unprepare(priv->clk);
2872 return ret;
2873}
2874#endif /* CONFIG_PM_SLEEP */
2875
2876static SIMPLE_DEV_PM_OPS(bcmgenet_pm_ops, bcmgenet_suspend, bcmgenet_resume);
2877
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002878static struct platform_driver bcmgenet_driver = {
2879 .probe = bcmgenet_probe,
2880 .remove = bcmgenet_remove,
2881 .driver = {
2882 .name = "bcmgenet",
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002883 .of_match_table = bcmgenet_match,
Florian Fainellib6e978e2014-07-21 15:29:22 -07002884 .pm = &bcmgenet_pm_ops,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002885 },
2886};
2887module_platform_driver(bcmgenet_driver);
2888
2889MODULE_AUTHOR("Broadcom Corporation");
2890MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
2891MODULE_ALIAS("platform:bcmgenet");
2892MODULE_LICENSE("GPL");