blob: 319b94381d2e17156dad3452a30fd7dd76ee0ca1 [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.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#define pr_fmt(fmt) "bcmgenet: " fmt
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/sched.h>
25#include <linux/types.h>
26#include <linux/fcntl.h>
27#include <linux/interrupt.h>
28#include <linux/string.h>
29#include <linux/if_ether.h>
30#include <linux/init.h>
31#include <linux/errno.h>
32#include <linux/delay.h>
33#include <linux/platform_device.h>
34#include <linux/dma-mapping.h>
35#include <linux/pm.h>
36#include <linux/clk.h>
Florian Fainelli1c1008c2014-02-13 16:08:47 -080037#include <linux/of.h>
38#include <linux/of_address.h>
39#include <linux/of_irq.h>
40#include <linux/of_net.h>
41#include <linux/of_platform.h>
42#include <net/arp.h>
43
44#include <linux/mii.h>
45#include <linux/ethtool.h>
46#include <linux/netdevice.h>
47#include <linux/inetdevice.h>
48#include <linux/etherdevice.h>
49#include <linux/skbuff.h>
50#include <linux/in.h>
51#include <linux/ip.h>
52#include <linux/ipv6.h>
53#include <linux/phy.h>
54
55#include <asm/unaligned.h>
56
57#include "bcmgenet.h"
58
59/* Maximum number of hardware queues, downsized if needed */
60#define GENET_MAX_MQ_CNT 4
61
62/* Default highest priority queue for multi queue support */
63#define GENET_Q0_PRIORITY 0
64
65#define GENET_DEFAULT_BD_CNT \
66 (TOTAL_DESC - priv->hw_params->tx_queues * priv->hw_params->bds_cnt)
67
68#define RX_BUF_LENGTH 2048
69#define SKB_ALIGNMENT 32
70
71/* Tx/Rx DMA register offset, skip 256 descriptors */
72#define WORDS_PER_BD(p) (p->hw_params->words_per_bd)
73#define DMA_DESC_SIZE (WORDS_PER_BD(priv) * sizeof(u32))
74
75#define GENET_TDMA_REG_OFF (priv->hw_params->tdma_offset + \
76 TOTAL_DESC * DMA_DESC_SIZE)
77
78#define GENET_RDMA_REG_OFF (priv->hw_params->rdma_offset + \
79 TOTAL_DESC * DMA_DESC_SIZE)
80
81static inline void dmadesc_set_length_status(struct bcmgenet_priv *priv,
82 void __iomem *d, u32 value)
83{
84 __raw_writel(value, d + DMA_DESC_LENGTH_STATUS);
85}
86
87static inline u32 dmadesc_get_length_status(struct bcmgenet_priv *priv,
88 void __iomem *d)
89{
90 return __raw_readl(d + DMA_DESC_LENGTH_STATUS);
91}
92
93static inline void dmadesc_set_addr(struct bcmgenet_priv *priv,
94 void __iomem *d,
95 dma_addr_t addr)
96{
97 __raw_writel(lower_32_bits(addr), d + DMA_DESC_ADDRESS_LO);
98
99 /* Register writes to GISB bus can take couple hundred nanoseconds
100 * and are done for each packet, save these expensive writes unless
101 * the platform is explicitely configured for 64-bits/LPAE.
102 */
103#ifdef CONFIG_PHYS_ADDR_T_64BIT
104 if (priv->hw_params->flags & GENET_HAS_40BITS)
105 __raw_writel(upper_32_bits(addr), d + DMA_DESC_ADDRESS_HI);
106#endif
107}
108
109/* Combined address + length/status setter */
110static inline void dmadesc_set(struct bcmgenet_priv *priv,
111 void __iomem *d, dma_addr_t addr, u32 val)
112{
113 dmadesc_set_length_status(priv, d, val);
114 dmadesc_set_addr(priv, d, addr);
115}
116
117static inline dma_addr_t dmadesc_get_addr(struct bcmgenet_priv *priv,
118 void __iomem *d)
119{
120 dma_addr_t addr;
121
122 addr = __raw_readl(d + DMA_DESC_ADDRESS_LO);
123
124 /* Register writes to GISB bus can take couple hundred nanoseconds
125 * and are done for each packet, save these expensive writes unless
126 * the platform is explicitely configured for 64-bits/LPAE.
127 */
128#ifdef CONFIG_PHYS_ADDR_T_64BIT
129 if (priv->hw_params->flags & GENET_HAS_40BITS)
130 addr |= (u64)__raw_readl(d + DMA_DESC_ADDRESS_HI) << 32;
131#endif
132 return addr;
133}
134
135#define GENET_VER_FMT "%1d.%1d EPHY: 0x%04x"
136
137#define GENET_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
138 NETIF_MSG_LINK)
139
140static inline u32 bcmgenet_rbuf_ctrl_get(struct bcmgenet_priv *priv)
141{
142 if (GENET_IS_V1(priv))
143 return bcmgenet_rbuf_readl(priv, RBUF_FLUSH_CTRL_V1);
144 else
145 return bcmgenet_sys_readl(priv, SYS_RBUF_FLUSH_CTRL);
146}
147
148static inline void bcmgenet_rbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
149{
150 if (GENET_IS_V1(priv))
151 bcmgenet_rbuf_writel(priv, val, RBUF_FLUSH_CTRL_V1);
152 else
153 bcmgenet_sys_writel(priv, val, SYS_RBUF_FLUSH_CTRL);
154}
155
156/* These macros are defined to deal with register map change
157 * between GENET1.1 and GENET2. Only those currently being used
158 * by driver are defined.
159 */
160static inline u32 bcmgenet_tbuf_ctrl_get(struct bcmgenet_priv *priv)
161{
162 if (GENET_IS_V1(priv))
163 return bcmgenet_rbuf_readl(priv, TBUF_CTRL_V1);
164 else
165 return __raw_readl(priv->base +
166 priv->hw_params->tbuf_offset + TBUF_CTRL);
167}
168
169static inline void bcmgenet_tbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
170{
171 if (GENET_IS_V1(priv))
172 bcmgenet_rbuf_writel(priv, val, TBUF_CTRL_V1);
173 else
174 __raw_writel(val, priv->base +
175 priv->hw_params->tbuf_offset + TBUF_CTRL);
176}
177
178static inline u32 bcmgenet_bp_mc_get(struct bcmgenet_priv *priv)
179{
180 if (GENET_IS_V1(priv))
181 return bcmgenet_rbuf_readl(priv, TBUF_BP_MC_V1);
182 else
183 return __raw_readl(priv->base +
184 priv->hw_params->tbuf_offset + TBUF_BP_MC);
185}
186
187static inline void bcmgenet_bp_mc_set(struct bcmgenet_priv *priv, u32 val)
188{
189 if (GENET_IS_V1(priv))
190 bcmgenet_rbuf_writel(priv, val, TBUF_BP_MC_V1);
191 else
192 __raw_writel(val, priv->base +
193 priv->hw_params->tbuf_offset + TBUF_BP_MC);
194}
195
196/* RX/TX DMA register accessors */
197enum dma_reg {
198 DMA_RING_CFG = 0,
199 DMA_CTRL,
200 DMA_STATUS,
201 DMA_SCB_BURST_SIZE,
202 DMA_ARB_CTRL,
203 DMA_PRIORITY,
204 DMA_RING_PRIORITY,
205};
206
207static const u8 bcmgenet_dma_regs_v3plus[] = {
208 [DMA_RING_CFG] = 0x00,
209 [DMA_CTRL] = 0x04,
210 [DMA_STATUS] = 0x08,
211 [DMA_SCB_BURST_SIZE] = 0x0C,
212 [DMA_ARB_CTRL] = 0x2C,
213 [DMA_PRIORITY] = 0x30,
214 [DMA_RING_PRIORITY] = 0x38,
215};
216
217static const u8 bcmgenet_dma_regs_v2[] = {
218 [DMA_RING_CFG] = 0x00,
219 [DMA_CTRL] = 0x04,
220 [DMA_STATUS] = 0x08,
221 [DMA_SCB_BURST_SIZE] = 0x0C,
222 [DMA_ARB_CTRL] = 0x30,
223 [DMA_PRIORITY] = 0x34,
224 [DMA_RING_PRIORITY] = 0x3C,
225};
226
227static const u8 bcmgenet_dma_regs_v1[] = {
228 [DMA_CTRL] = 0x00,
229 [DMA_STATUS] = 0x04,
230 [DMA_SCB_BURST_SIZE] = 0x0C,
231 [DMA_ARB_CTRL] = 0x30,
232 [DMA_PRIORITY] = 0x34,
233 [DMA_RING_PRIORITY] = 0x3C,
234};
235
236/* Set at runtime once bcmgenet version is known */
237static const u8 *bcmgenet_dma_regs;
238
239static inline struct bcmgenet_priv *dev_to_priv(struct device *dev)
240{
241 return netdev_priv(dev_get_drvdata(dev));
242}
243
244static inline u32 bcmgenet_tdma_readl(struct bcmgenet_priv *priv,
245 enum dma_reg r)
246{
247 return __raw_readl(priv->base + GENET_TDMA_REG_OFF +
248 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
249}
250
251static inline void bcmgenet_tdma_writel(struct bcmgenet_priv *priv,
252 u32 val, enum dma_reg r)
253{
254 __raw_writel(val, priv->base + GENET_TDMA_REG_OFF +
255 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
256}
257
258static inline u32 bcmgenet_rdma_readl(struct bcmgenet_priv *priv,
259 enum dma_reg r)
260{
261 return __raw_readl(priv->base + GENET_RDMA_REG_OFF +
262 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
263}
264
265static inline void bcmgenet_rdma_writel(struct bcmgenet_priv *priv,
266 u32 val, enum dma_reg r)
267{
268 __raw_writel(val, priv->base + GENET_RDMA_REG_OFF +
269 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
270}
271
272/* RDMA/TDMA ring registers and accessors
273 * we merge the common fields and just prefix with T/D the registers
274 * having different meaning depending on the direction
275 */
276enum dma_ring_reg {
277 TDMA_READ_PTR = 0,
278 RDMA_WRITE_PTR = TDMA_READ_PTR,
279 TDMA_READ_PTR_HI,
280 RDMA_WRITE_PTR_HI = TDMA_READ_PTR_HI,
281 TDMA_CONS_INDEX,
282 RDMA_PROD_INDEX = TDMA_CONS_INDEX,
283 TDMA_PROD_INDEX,
284 RDMA_CONS_INDEX = TDMA_PROD_INDEX,
285 DMA_RING_BUF_SIZE,
286 DMA_START_ADDR,
287 DMA_START_ADDR_HI,
288 DMA_END_ADDR,
289 DMA_END_ADDR_HI,
290 DMA_MBUF_DONE_THRESH,
291 TDMA_FLOW_PERIOD,
292 RDMA_XON_XOFF_THRESH = TDMA_FLOW_PERIOD,
293 TDMA_WRITE_PTR,
294 RDMA_READ_PTR = TDMA_WRITE_PTR,
295 TDMA_WRITE_PTR_HI,
296 RDMA_READ_PTR_HI = TDMA_WRITE_PTR_HI
297};
298
299/* GENET v4 supports 40-bits pointer addressing
300 * for obvious reasons the LO and HI word parts
301 * are contiguous, but this offsets the other
302 * registers.
303 */
304static const u8 genet_dma_ring_regs_v4[] = {
305 [TDMA_READ_PTR] = 0x00,
306 [TDMA_READ_PTR_HI] = 0x04,
307 [TDMA_CONS_INDEX] = 0x08,
308 [TDMA_PROD_INDEX] = 0x0C,
309 [DMA_RING_BUF_SIZE] = 0x10,
310 [DMA_START_ADDR] = 0x14,
311 [DMA_START_ADDR_HI] = 0x18,
312 [DMA_END_ADDR] = 0x1C,
313 [DMA_END_ADDR_HI] = 0x20,
314 [DMA_MBUF_DONE_THRESH] = 0x24,
315 [TDMA_FLOW_PERIOD] = 0x28,
316 [TDMA_WRITE_PTR] = 0x2C,
317 [TDMA_WRITE_PTR_HI] = 0x30,
318};
319
320static const u8 genet_dma_ring_regs_v123[] = {
321 [TDMA_READ_PTR] = 0x00,
322 [TDMA_CONS_INDEX] = 0x04,
323 [TDMA_PROD_INDEX] = 0x08,
324 [DMA_RING_BUF_SIZE] = 0x0C,
325 [DMA_START_ADDR] = 0x10,
326 [DMA_END_ADDR] = 0x14,
327 [DMA_MBUF_DONE_THRESH] = 0x18,
328 [TDMA_FLOW_PERIOD] = 0x1C,
329 [TDMA_WRITE_PTR] = 0x20,
330};
331
332/* Set at runtime once GENET version is known */
333static const u8 *genet_dma_ring_regs;
334
335static inline u32 bcmgenet_tdma_ring_readl(struct bcmgenet_priv *priv,
336 unsigned int ring,
337 enum dma_ring_reg r)
338{
339 return __raw_readl(priv->base + GENET_TDMA_REG_OFF +
340 (DMA_RING_SIZE * ring) +
341 genet_dma_ring_regs[r]);
342}
343
344static inline void bcmgenet_tdma_ring_writel(struct bcmgenet_priv *priv,
345 unsigned int ring,
346 u32 val,
347 enum dma_ring_reg r)
348{
349 __raw_writel(val, priv->base + GENET_TDMA_REG_OFF +
350 (DMA_RING_SIZE * ring) +
351 genet_dma_ring_regs[r]);
352}
353
354static inline u32 bcmgenet_rdma_ring_readl(struct bcmgenet_priv *priv,
355 unsigned int ring,
356 enum dma_ring_reg r)
357{
358 return __raw_readl(priv->base + GENET_RDMA_REG_OFF +
359 (DMA_RING_SIZE * ring) +
360 genet_dma_ring_regs[r]);
361}
362
363static inline void bcmgenet_rdma_ring_writel(struct bcmgenet_priv *priv,
364 unsigned int ring,
365 u32 val,
366 enum dma_ring_reg r)
367{
368 __raw_writel(val, priv->base + GENET_RDMA_REG_OFF +
369 (DMA_RING_SIZE * ring) +
370 genet_dma_ring_regs[r]);
371}
372
373static int bcmgenet_get_settings(struct net_device *dev,
374 struct ethtool_cmd *cmd)
375{
376 struct bcmgenet_priv *priv = netdev_priv(dev);
377
378 if (!netif_running(dev))
379 return -EINVAL;
380
381 if (!priv->phydev)
382 return -ENODEV;
383
384 return phy_ethtool_gset(priv->phydev, cmd);
385}
386
387static int bcmgenet_set_settings(struct net_device *dev,
388 struct ethtool_cmd *cmd)
389{
390 struct bcmgenet_priv *priv = netdev_priv(dev);
391
392 if (!netif_running(dev))
393 return -EINVAL;
394
395 if (!priv->phydev)
396 return -ENODEV;
397
398 return phy_ethtool_sset(priv->phydev, cmd);
399}
400
401static int bcmgenet_set_rx_csum(struct net_device *dev,
402 netdev_features_t wanted)
403{
404 struct bcmgenet_priv *priv = netdev_priv(dev);
405 u32 rbuf_chk_ctrl;
406 bool rx_csum_en;
407
408 rx_csum_en = !!(wanted & NETIF_F_RXCSUM);
409
410 rbuf_chk_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CHK_CTRL);
411
412 /* enable rx checksumming */
413 if (rx_csum_en)
414 rbuf_chk_ctrl |= RBUF_RXCHK_EN;
415 else
416 rbuf_chk_ctrl &= ~RBUF_RXCHK_EN;
417 priv->desc_rxchk_en = rx_csum_en;
Florian Fainelliebe5e3c2014-03-26 21:18:39 -0700418
419 /* If UniMAC forwards CRC, we need to skip over it to get
420 * a valid CHK bit to be set in the per-packet status word
421 */
422 if (rx_csum_en && priv->crc_fwd_en)
423 rbuf_chk_ctrl |= RBUF_SKIP_FCS;
424 else
425 rbuf_chk_ctrl &= ~RBUF_SKIP_FCS;
426
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800427 bcmgenet_rbuf_writel(priv, rbuf_chk_ctrl, RBUF_CHK_CTRL);
428
429 return 0;
430}
431
432static int bcmgenet_set_tx_csum(struct net_device *dev,
433 netdev_features_t wanted)
434{
435 struct bcmgenet_priv *priv = netdev_priv(dev);
436 bool desc_64b_en;
437 u32 tbuf_ctrl, rbuf_ctrl;
438
439 tbuf_ctrl = bcmgenet_tbuf_ctrl_get(priv);
440 rbuf_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
441
442 desc_64b_en = !!(wanted & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
443
444 /* enable 64 bytes descriptor in both directions (RBUF and TBUF) */
445 if (desc_64b_en) {
446 tbuf_ctrl |= RBUF_64B_EN;
447 rbuf_ctrl |= RBUF_64B_EN;
448 } else {
449 tbuf_ctrl &= ~RBUF_64B_EN;
450 rbuf_ctrl &= ~RBUF_64B_EN;
451 }
452 priv->desc_64b_en = desc_64b_en;
453
454 bcmgenet_tbuf_ctrl_set(priv, tbuf_ctrl);
455 bcmgenet_rbuf_writel(priv, rbuf_ctrl, RBUF_CTRL);
456
457 return 0;
458}
459
460static int bcmgenet_set_features(struct net_device *dev,
461 netdev_features_t features)
462{
463 netdev_features_t changed = features ^ dev->features;
464 netdev_features_t wanted = dev->wanted_features;
465 int ret = 0;
466
467 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
468 ret = bcmgenet_set_tx_csum(dev, wanted);
469 if (changed & (NETIF_F_RXCSUM))
470 ret = bcmgenet_set_rx_csum(dev, wanted);
471
472 return ret;
473}
474
475static u32 bcmgenet_get_msglevel(struct net_device *dev)
476{
477 struct bcmgenet_priv *priv = netdev_priv(dev);
478
479 return priv->msg_enable;
480}
481
482static void bcmgenet_set_msglevel(struct net_device *dev, u32 level)
483{
484 struct bcmgenet_priv *priv = netdev_priv(dev);
485
486 priv->msg_enable = level;
487}
488
489/* standard ethtool support functions. */
490enum bcmgenet_stat_type {
491 BCMGENET_STAT_NETDEV = -1,
492 BCMGENET_STAT_MIB_RX,
493 BCMGENET_STAT_MIB_TX,
494 BCMGENET_STAT_RUNT,
495 BCMGENET_STAT_MISC,
496};
497
498struct bcmgenet_stats {
499 char stat_string[ETH_GSTRING_LEN];
500 int stat_sizeof;
501 int stat_offset;
502 enum bcmgenet_stat_type type;
503 /* reg offset from UMAC base for misc counters */
504 u16 reg_offset;
505};
506
507#define STAT_NETDEV(m) { \
508 .stat_string = __stringify(m), \
509 .stat_sizeof = sizeof(((struct net_device_stats *)0)->m), \
510 .stat_offset = offsetof(struct net_device_stats, m), \
511 .type = BCMGENET_STAT_NETDEV, \
512}
513
514#define STAT_GENET_MIB(str, m, _type) { \
515 .stat_string = str, \
516 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
517 .stat_offset = offsetof(struct bcmgenet_priv, m), \
518 .type = _type, \
519}
520
521#define STAT_GENET_MIB_RX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_RX)
522#define STAT_GENET_MIB_TX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_TX)
523#define STAT_GENET_RUNT(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_RUNT)
524
525#define STAT_GENET_MISC(str, m, offset) { \
526 .stat_string = str, \
527 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
528 .stat_offset = offsetof(struct bcmgenet_priv, m), \
529 .type = BCMGENET_STAT_MISC, \
530 .reg_offset = offset, \
531}
532
533
534/* There is a 0xC gap between the end of RX and beginning of TX stats and then
535 * between the end of TX stats and the beginning of the RX RUNT
536 */
537#define BCMGENET_STAT_OFFSET 0xc
538
539/* Hardware counters must be kept in sync because the order/offset
540 * is important here (order in structure declaration = order in hardware)
541 */
542static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
543 /* general stats */
544 STAT_NETDEV(rx_packets),
545 STAT_NETDEV(tx_packets),
546 STAT_NETDEV(rx_bytes),
547 STAT_NETDEV(tx_bytes),
548 STAT_NETDEV(rx_errors),
549 STAT_NETDEV(tx_errors),
550 STAT_NETDEV(rx_dropped),
551 STAT_NETDEV(tx_dropped),
552 STAT_NETDEV(multicast),
553 /* UniMAC RSV counters */
554 STAT_GENET_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
555 STAT_GENET_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
556 STAT_GENET_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
557 STAT_GENET_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
558 STAT_GENET_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
559 STAT_GENET_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
560 STAT_GENET_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
561 STAT_GENET_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
562 STAT_GENET_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
563 STAT_GENET_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
564 STAT_GENET_MIB_RX("rx_pkts", mib.rx.pkt),
565 STAT_GENET_MIB_RX("rx_bytes", mib.rx.bytes),
566 STAT_GENET_MIB_RX("rx_multicast", mib.rx.mca),
567 STAT_GENET_MIB_RX("rx_broadcast", mib.rx.bca),
568 STAT_GENET_MIB_RX("rx_fcs", mib.rx.fcs),
569 STAT_GENET_MIB_RX("rx_control", mib.rx.cf),
570 STAT_GENET_MIB_RX("rx_pause", mib.rx.pf),
571 STAT_GENET_MIB_RX("rx_unknown", mib.rx.uo),
572 STAT_GENET_MIB_RX("rx_align", mib.rx.aln),
573 STAT_GENET_MIB_RX("rx_outrange", mib.rx.flr),
574 STAT_GENET_MIB_RX("rx_code", mib.rx.cde),
575 STAT_GENET_MIB_RX("rx_carrier", mib.rx.fcr),
576 STAT_GENET_MIB_RX("rx_oversize", mib.rx.ovr),
577 STAT_GENET_MIB_RX("rx_jabber", mib.rx.jbr),
578 STAT_GENET_MIB_RX("rx_mtu_err", mib.rx.mtue),
579 STAT_GENET_MIB_RX("rx_good_pkts", mib.rx.pok),
580 STAT_GENET_MIB_RX("rx_unicast", mib.rx.uc),
581 STAT_GENET_MIB_RX("rx_ppp", mib.rx.ppp),
582 STAT_GENET_MIB_RX("rx_crc", mib.rx.rcrc),
583 /* UniMAC TSV counters */
584 STAT_GENET_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
585 STAT_GENET_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
586 STAT_GENET_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
587 STAT_GENET_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
588 STAT_GENET_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
589 STAT_GENET_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
590 STAT_GENET_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
591 STAT_GENET_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
592 STAT_GENET_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
593 STAT_GENET_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
594 STAT_GENET_MIB_TX("tx_pkts", mib.tx.pkts),
595 STAT_GENET_MIB_TX("tx_multicast", mib.tx.mca),
596 STAT_GENET_MIB_TX("tx_broadcast", mib.tx.bca),
597 STAT_GENET_MIB_TX("tx_pause", mib.tx.pf),
598 STAT_GENET_MIB_TX("tx_control", mib.tx.cf),
599 STAT_GENET_MIB_TX("tx_fcs_err", mib.tx.fcs),
600 STAT_GENET_MIB_TX("tx_oversize", mib.tx.ovr),
601 STAT_GENET_MIB_TX("tx_defer", mib.tx.drf),
602 STAT_GENET_MIB_TX("tx_excess_defer", mib.tx.edf),
603 STAT_GENET_MIB_TX("tx_single_col", mib.tx.scl),
604 STAT_GENET_MIB_TX("tx_multi_col", mib.tx.mcl),
605 STAT_GENET_MIB_TX("tx_late_col", mib.tx.lcl),
606 STAT_GENET_MIB_TX("tx_excess_col", mib.tx.ecl),
607 STAT_GENET_MIB_TX("tx_frags", mib.tx.frg),
608 STAT_GENET_MIB_TX("tx_total_col", mib.tx.ncl),
609 STAT_GENET_MIB_TX("tx_jabber", mib.tx.jbr),
610 STAT_GENET_MIB_TX("tx_bytes", mib.tx.bytes),
611 STAT_GENET_MIB_TX("tx_good_pkts", mib.tx.pok),
612 STAT_GENET_MIB_TX("tx_unicast", mib.tx.uc),
613 /* UniMAC RUNT counters */
614 STAT_GENET_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
615 STAT_GENET_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
616 STAT_GENET_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
617 STAT_GENET_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
618 /* Misc UniMAC counters */
619 STAT_GENET_MISC("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt,
620 UMAC_RBUF_OVFL_CNT),
621 STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt, UMAC_RBUF_ERR_CNT),
622 STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT),
623};
624
625#define BCMGENET_STATS_LEN ARRAY_SIZE(bcmgenet_gstrings_stats)
626
627static void bcmgenet_get_drvinfo(struct net_device *dev,
628 struct ethtool_drvinfo *info)
629{
630 strlcpy(info->driver, "bcmgenet", sizeof(info->driver));
631 strlcpy(info->version, "v2.0", sizeof(info->version));
632 info->n_stats = BCMGENET_STATS_LEN;
633
634}
635
636static int bcmgenet_get_sset_count(struct net_device *dev, int string_set)
637{
638 switch (string_set) {
639 case ETH_SS_STATS:
640 return BCMGENET_STATS_LEN;
641 default:
642 return -EOPNOTSUPP;
643 }
644}
645
646static void bcmgenet_get_strings(struct net_device *dev,
647 u32 stringset, u8 *data)
648{
649 int i;
650
651 switch (stringset) {
652 case ETH_SS_STATS:
653 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
654 memcpy(data + i * ETH_GSTRING_LEN,
655 bcmgenet_gstrings_stats[i].stat_string,
656 ETH_GSTRING_LEN);
657 }
658 break;
659 }
660}
661
662static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv)
663{
664 int i, j = 0;
665
666 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
667 const struct bcmgenet_stats *s;
668 u8 offset = 0;
669 u32 val = 0;
670 char *p;
671
672 s = &bcmgenet_gstrings_stats[i];
673 switch (s->type) {
674 case BCMGENET_STAT_NETDEV:
675 continue;
676 case BCMGENET_STAT_MIB_RX:
677 case BCMGENET_STAT_MIB_TX:
678 case BCMGENET_STAT_RUNT:
679 if (s->type != BCMGENET_STAT_MIB_RX)
680 offset = BCMGENET_STAT_OFFSET;
681 val = bcmgenet_umac_readl(priv, UMAC_MIB_START +
682 j + offset);
683 break;
684 case BCMGENET_STAT_MISC:
685 val = bcmgenet_umac_readl(priv, s->reg_offset);
686 /* clear if overflowed */
687 if (val == ~0)
688 bcmgenet_umac_writel(priv, 0, s->reg_offset);
689 break;
690 }
691
692 j += s->stat_sizeof;
693 p = (char *)priv + s->stat_offset;
694 *(u32 *)p = val;
695 }
696}
697
698static void bcmgenet_get_ethtool_stats(struct net_device *dev,
699 struct ethtool_stats *stats,
700 u64 *data)
701{
702 struct bcmgenet_priv *priv = netdev_priv(dev);
703 int i;
704
705 if (netif_running(dev))
706 bcmgenet_update_mib_counters(priv);
707
708 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
709 const struct bcmgenet_stats *s;
710 char *p;
711
712 s = &bcmgenet_gstrings_stats[i];
713 if (s->type == BCMGENET_STAT_NETDEV)
714 p = (char *)&dev->stats;
715 else
716 p = (char *)priv;
717 p += s->stat_offset;
718 data[i] = *(u32 *)p;
719 }
720}
721
722/* standard ethtool support functions. */
723static struct ethtool_ops bcmgenet_ethtool_ops = {
724 .get_strings = bcmgenet_get_strings,
725 .get_sset_count = bcmgenet_get_sset_count,
726 .get_ethtool_stats = bcmgenet_get_ethtool_stats,
727 .get_settings = bcmgenet_get_settings,
728 .set_settings = bcmgenet_set_settings,
729 .get_drvinfo = bcmgenet_get_drvinfo,
730 .get_link = ethtool_op_get_link,
731 .get_msglevel = bcmgenet_get_msglevel,
732 .set_msglevel = bcmgenet_set_msglevel,
733};
734
735/* Power down the unimac, based on mode. */
736static void bcmgenet_power_down(struct bcmgenet_priv *priv,
737 enum bcmgenet_power_mode mode)
738{
739 u32 reg;
740
741 switch (mode) {
742 case GENET_POWER_CABLE_SENSE:
Florian Fainelli80d8e962014-02-24 16:56:11 -0800743 phy_detach(priv->phydev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800744 break;
745
Florian Fainellic3ae64a2014-07-21 15:29:25 -0700746 case GENET_POWER_WOL_MAGIC:
747 bcmgenet_wol_power_down_cfg(priv, mode);
748 break;
749
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800750 case GENET_POWER_PASSIVE:
751 /* Power down LED */
752 bcmgenet_mii_reset(priv->dev);
753 if (priv->hw_params->flags & GENET_HAS_EXT) {
754 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
755 reg |= (EXT_PWR_DOWN_PHY |
756 EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
757 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
758 }
759 break;
760 default:
761 break;
762 }
763}
764
765static void bcmgenet_power_up(struct bcmgenet_priv *priv,
766 enum bcmgenet_power_mode mode)
767{
768 u32 reg;
769
770 if (!(priv->hw_params->flags & GENET_HAS_EXT))
771 return;
772
773 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
774
775 switch (mode) {
776 case GENET_POWER_PASSIVE:
777 reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_PHY |
778 EXT_PWR_DOWN_BIAS);
779 /* fallthrough */
780 case GENET_POWER_CABLE_SENSE:
781 /* enable APD */
782 reg |= EXT_PWR_DN_EN_LD;
783 break;
Florian Fainellic3ae64a2014-07-21 15:29:25 -0700784 case GENET_POWER_WOL_MAGIC:
785 bcmgenet_wol_power_up_cfg(priv, mode);
786 return;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800787 default:
788 break;
789 }
790
791 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
792 bcmgenet_mii_reset(priv->dev);
793}
794
795/* ioctl handle special commands that are not present in ethtool. */
796static int bcmgenet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
797{
798 struct bcmgenet_priv *priv = netdev_priv(dev);
799 int val = 0;
800
801 if (!netif_running(dev))
802 return -EINVAL;
803
804 switch (cmd) {
805 case SIOCGMIIPHY:
806 case SIOCGMIIREG:
807 case SIOCSMIIREG:
808 if (!priv->phydev)
809 val = -ENODEV;
810 else
811 val = phy_mii_ioctl(priv->phydev, rq, cmd);
812 break;
813
814 default:
815 val = -EINVAL;
816 break;
817 }
818
819 return val;
820}
821
822static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv,
823 struct bcmgenet_tx_ring *ring)
824{
825 struct enet_cb *tx_cb_ptr;
826
827 tx_cb_ptr = ring->cbs;
828 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
829 tx_cb_ptr->bd_addr = priv->tx_bds + ring->write_ptr * DMA_DESC_SIZE;
830 /* Advancing local write pointer */
831 if (ring->write_ptr == ring->end_ptr)
832 ring->write_ptr = ring->cb_ptr;
833 else
834 ring->write_ptr++;
835
836 return tx_cb_ptr;
837}
838
839/* Simple helper to free a control block's resources */
840static void bcmgenet_free_cb(struct enet_cb *cb)
841{
842 dev_kfree_skb_any(cb->skb);
843 cb->skb = NULL;
844 dma_unmap_addr_set(cb, dma_addr, 0);
845}
846
847static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_priv *priv,
848 struct bcmgenet_tx_ring *ring)
849{
850 bcmgenet_intrl2_0_writel(priv,
851 UMAC_IRQ_TXDMA_BDONE | UMAC_IRQ_TXDMA_PDONE,
852 INTRL2_CPU_MASK_SET);
853}
854
855static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_priv *priv,
856 struct bcmgenet_tx_ring *ring)
857{
858 bcmgenet_intrl2_0_writel(priv,
859 UMAC_IRQ_TXDMA_BDONE | UMAC_IRQ_TXDMA_PDONE,
860 INTRL2_CPU_MASK_CLEAR);
861}
862
863static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_priv *priv,
864 struct bcmgenet_tx_ring *ring)
865{
866 bcmgenet_intrl2_1_writel(priv,
867 (1 << ring->index), INTRL2_CPU_MASK_CLEAR);
868 priv->int1_mask &= ~(1 << ring->index);
869}
870
871static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_priv *priv,
872 struct bcmgenet_tx_ring *ring)
873{
874 bcmgenet_intrl2_1_writel(priv,
875 (1 << ring->index), INTRL2_CPU_MASK_SET);
876 priv->int1_mask |= (1 << ring->index);
877}
878
879/* Unlocked version of the reclaim routine */
880static void __bcmgenet_tx_reclaim(struct net_device *dev,
881 struct bcmgenet_tx_ring *ring)
882{
883 struct bcmgenet_priv *priv = netdev_priv(dev);
884 int last_tx_cn, last_c_index, num_tx_bds;
885 struct enet_cb *tx_cb_ptr;
Florian Fainellib2cde2c2014-03-20 10:53:23 -0700886 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800887 unsigned int c_index;
888
889 /* Compute how many buffers are transmited since last xmit call */
890 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
Florian Fainellib2cde2c2014-03-20 10:53:23 -0700891 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800892
893 last_c_index = ring->c_index;
894 num_tx_bds = ring->size;
895
896 c_index &= (num_tx_bds - 1);
897
898 if (c_index >= last_c_index)
899 last_tx_cn = c_index - last_c_index;
900 else
901 last_tx_cn = num_tx_bds - last_c_index + c_index;
902
903 netif_dbg(priv, tx_done, dev,
904 "%s ring=%d index=%d last_tx_cn=%d last_index=%d\n",
905 __func__, ring->index,
906 c_index, last_tx_cn, last_c_index);
907
908 /* Reclaim transmitted buffers */
909 while (last_tx_cn-- > 0) {
910 tx_cb_ptr = ring->cbs + last_c_index;
911 if (tx_cb_ptr->skb) {
912 dev->stats.tx_bytes += tx_cb_ptr->skb->len;
913 dma_unmap_single(&dev->dev,
914 dma_unmap_addr(tx_cb_ptr, dma_addr),
915 tx_cb_ptr->skb->len,
916 DMA_TO_DEVICE);
917 bcmgenet_free_cb(tx_cb_ptr);
918 } else if (dma_unmap_addr(tx_cb_ptr, dma_addr)) {
919 dev->stats.tx_bytes +=
920 dma_unmap_len(tx_cb_ptr, dma_len);
921 dma_unmap_page(&dev->dev,
922 dma_unmap_addr(tx_cb_ptr, dma_addr),
923 dma_unmap_len(tx_cb_ptr, dma_len),
924 DMA_TO_DEVICE);
925 dma_unmap_addr_set(tx_cb_ptr, dma_addr, 0);
926 }
927 dev->stats.tx_packets++;
928 ring->free_bds += 1;
929
930 last_c_index++;
931 last_c_index &= (num_tx_bds - 1);
932 }
933
934 if (ring->free_bds > (MAX_SKB_FRAGS + 1))
935 ring->int_disable(priv, ring);
936
Florian Fainellib2cde2c2014-03-20 10:53:23 -0700937 if (netif_tx_queue_stopped(txq))
938 netif_tx_wake_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800939
940 ring->c_index = c_index;
941}
942
943static void bcmgenet_tx_reclaim(struct net_device *dev,
944 struct bcmgenet_tx_ring *ring)
945{
946 unsigned long flags;
947
948 spin_lock_irqsave(&ring->lock, flags);
949 __bcmgenet_tx_reclaim(dev, ring);
950 spin_unlock_irqrestore(&ring->lock, flags);
951}
952
953static void bcmgenet_tx_reclaim_all(struct net_device *dev)
954{
955 struct bcmgenet_priv *priv = netdev_priv(dev);
956 int i;
957
958 if (netif_is_multiqueue(dev)) {
959 for (i = 0; i < priv->hw_params->tx_queues; i++)
960 bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
961 }
962
963 bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
964}
965
966/* Transmits a single SKB (either head of a fragment or a single SKB)
967 * caller must hold priv->lock
968 */
969static int bcmgenet_xmit_single(struct net_device *dev,
970 struct sk_buff *skb,
971 u16 dma_desc_flags,
972 struct bcmgenet_tx_ring *ring)
973{
974 struct bcmgenet_priv *priv = netdev_priv(dev);
975 struct device *kdev = &priv->pdev->dev;
976 struct enet_cb *tx_cb_ptr;
977 unsigned int skb_len;
978 dma_addr_t mapping;
979 u32 length_status;
980 int ret;
981
982 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
983
984 if (unlikely(!tx_cb_ptr))
985 BUG();
986
987 tx_cb_ptr->skb = skb;
988
989 skb_len = skb_headlen(skb) < ETH_ZLEN ? ETH_ZLEN : skb_headlen(skb);
990
991 mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
992 ret = dma_mapping_error(kdev, mapping);
993 if (ret) {
994 netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
995 dev_kfree_skb(skb);
996 return ret;
997 }
998
999 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
1000 dma_unmap_len_set(tx_cb_ptr, dma_len, skb->len);
1001 length_status = (skb_len << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
1002 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT) |
1003 DMA_TX_APPEND_CRC;
1004
1005 if (skb->ip_summed == CHECKSUM_PARTIAL)
1006 length_status |= DMA_TX_DO_CSUM;
1007
1008 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, length_status);
1009
1010 /* Decrement total BD count and advance our write pointer */
1011 ring->free_bds -= 1;
1012 ring->prod_index += 1;
1013 ring->prod_index &= DMA_P_INDEX_MASK;
1014
1015 return 0;
1016}
1017
1018/* Transmit a SKB fragement */
1019static int bcmgenet_xmit_frag(struct net_device *dev,
1020 skb_frag_t *frag,
1021 u16 dma_desc_flags,
1022 struct bcmgenet_tx_ring *ring)
1023{
1024 struct bcmgenet_priv *priv = netdev_priv(dev);
1025 struct device *kdev = &priv->pdev->dev;
1026 struct enet_cb *tx_cb_ptr;
1027 dma_addr_t mapping;
1028 int ret;
1029
1030 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
1031
1032 if (unlikely(!tx_cb_ptr))
1033 BUG();
1034 tx_cb_ptr->skb = NULL;
1035
1036 mapping = skb_frag_dma_map(kdev, frag, 0,
1037 skb_frag_size(frag), DMA_TO_DEVICE);
1038 ret = dma_mapping_error(kdev, mapping);
1039 if (ret) {
1040 netif_err(priv, tx_err, dev, "%s: Tx DMA map failed\n",
1041 __func__);
1042 return ret;
1043 }
1044
1045 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
1046 dma_unmap_len_set(tx_cb_ptr, dma_len, frag->size);
1047
1048 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping,
1049 (frag->size << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
1050 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT));
1051
1052
1053 ring->free_bds -= 1;
1054 ring->prod_index += 1;
1055 ring->prod_index &= DMA_P_INDEX_MASK;
1056
1057 return 0;
1058}
1059
1060/* Reallocate the SKB to put enough headroom in front of it and insert
1061 * the transmit checksum offsets in the descriptors
1062 */
1063static int bcmgenet_put_tx_csum(struct net_device *dev, struct sk_buff *skb)
1064{
1065 struct status_64 *status = NULL;
1066 struct sk_buff *new_skb;
1067 u16 offset;
1068 u8 ip_proto;
1069 u16 ip_ver;
1070 u32 tx_csum_info;
1071
1072 if (unlikely(skb_headroom(skb) < sizeof(*status))) {
1073 /* If 64 byte status block enabled, must make sure skb has
1074 * enough headroom for us to insert 64B status block.
1075 */
1076 new_skb = skb_realloc_headroom(skb, sizeof(*status));
1077 dev_kfree_skb(skb);
1078 if (!new_skb) {
1079 dev->stats.tx_errors++;
1080 dev->stats.tx_dropped++;
1081 return -ENOMEM;
1082 }
1083 skb = new_skb;
1084 }
1085
1086 skb_push(skb, sizeof(*status));
1087 status = (struct status_64 *)skb->data;
1088
1089 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1090 ip_ver = htons(skb->protocol);
1091 switch (ip_ver) {
1092 case ETH_P_IP:
1093 ip_proto = ip_hdr(skb)->protocol;
1094 break;
1095 case ETH_P_IPV6:
1096 ip_proto = ipv6_hdr(skb)->nexthdr;
1097 break;
1098 default:
1099 return 0;
1100 }
1101
1102 offset = skb_checksum_start_offset(skb) - sizeof(*status);
1103 tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
1104 (offset + skb->csum_offset);
1105
1106 /* Set the length valid bit for TCP and UDP and just set
1107 * the special UDP flag for IPv4, else just set to 0.
1108 */
1109 if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
1110 tx_csum_info |= STATUS_TX_CSUM_LV;
1111 if (ip_proto == IPPROTO_UDP && ip_ver == ETH_P_IP)
1112 tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
1113 } else
1114 tx_csum_info = 0;
1115
1116 status->tx_csum_info = tx_csum_info;
1117 }
1118
1119 return 0;
1120}
1121
1122static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
1123{
1124 struct bcmgenet_priv *priv = netdev_priv(dev);
1125 struct bcmgenet_tx_ring *ring = NULL;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001126 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001127 unsigned long flags = 0;
1128 int nr_frags, index;
1129 u16 dma_desc_flags;
1130 int ret;
1131 int i;
1132
1133 index = skb_get_queue_mapping(skb);
1134 /* Mapping strategy:
1135 * queue_mapping = 0, unclassified, packet xmited through ring16
1136 * queue_mapping = 1, goes to ring 0. (highest priority queue
1137 * queue_mapping = 2, goes to ring 1.
1138 * queue_mapping = 3, goes to ring 2.
1139 * queue_mapping = 4, goes to ring 3.
1140 */
1141 if (index == 0)
1142 index = DESC_INDEX;
1143 else
1144 index -= 1;
1145
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001146 nr_frags = skb_shinfo(skb)->nr_frags;
1147 ring = &priv->tx_rings[index];
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001148 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001149
1150 spin_lock_irqsave(&ring->lock, flags);
1151 if (ring->free_bds <= nr_frags + 1) {
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001152 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001153 netdev_err(dev, "%s: tx ring %d full when queue %d awake\n",
1154 __func__, index, ring->queue);
1155 ret = NETDEV_TX_BUSY;
1156 goto out;
1157 }
1158
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001159 /* set the SKB transmit checksum */
1160 if (priv->desc_64b_en) {
1161 ret = bcmgenet_put_tx_csum(dev, skb);
1162 if (ret) {
1163 ret = NETDEV_TX_OK;
1164 goto out;
1165 }
1166 }
1167
1168 dma_desc_flags = DMA_SOP;
1169 if (nr_frags == 0)
1170 dma_desc_flags |= DMA_EOP;
1171
1172 /* Transmit single SKB or head of fragment list */
1173 ret = bcmgenet_xmit_single(dev, skb, dma_desc_flags, ring);
1174 if (ret) {
1175 ret = NETDEV_TX_OK;
1176 goto out;
1177 }
1178
1179 /* xmit fragment */
1180 for (i = 0; i < nr_frags; i++) {
1181 ret = bcmgenet_xmit_frag(dev,
1182 &skb_shinfo(skb)->frags[i],
1183 (i == nr_frags - 1) ? DMA_EOP : 0, ring);
1184 if (ret) {
1185 ret = NETDEV_TX_OK;
1186 goto out;
1187 }
1188 }
1189
Florian Fainellid03825f2014-03-20 10:53:21 -07001190 skb_tx_timestamp(skb);
1191
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001192 /* we kept a software copy of how much we should advance the TDMA
1193 * producer index, now write it down to the hardware
1194 */
1195 bcmgenet_tdma_ring_writel(priv, ring->index,
1196 ring->prod_index, TDMA_PROD_INDEX);
1197
1198 if (ring->free_bds <= (MAX_SKB_FRAGS + 1)) {
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001199 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001200 ring->int_enable(priv, ring);
1201 }
1202
1203out:
1204 spin_unlock_irqrestore(&ring->lock, flags);
1205
1206 return ret;
1207}
1208
1209
1210static int bcmgenet_rx_refill(struct bcmgenet_priv *priv,
1211 struct enet_cb *cb)
1212{
1213 struct device *kdev = &priv->pdev->dev;
1214 struct sk_buff *skb;
1215 dma_addr_t mapping;
1216 int ret;
1217
1218 skb = netdev_alloc_skb(priv->dev,
1219 priv->rx_buf_len + SKB_ALIGNMENT);
1220 if (!skb)
1221 return -ENOMEM;
1222
1223 /* a caller did not release this control block */
1224 WARN_ON(cb->skb != NULL);
1225 cb->skb = skb;
1226 mapping = dma_map_single(kdev, skb->data,
1227 priv->rx_buf_len, DMA_FROM_DEVICE);
1228 ret = dma_mapping_error(kdev, mapping);
1229 if (ret) {
1230 bcmgenet_free_cb(cb);
1231 netif_err(priv, rx_err, priv->dev,
1232 "%s DMA map failed\n", __func__);
1233 return ret;
1234 }
1235
1236 dma_unmap_addr_set(cb, dma_addr, mapping);
1237 /* assign packet, prepare descriptor, and advance pointer */
1238
1239 dmadesc_set_addr(priv, priv->rx_bd_assign_ptr, mapping);
1240
1241 /* turn on the newly assigned BD for DMA to use */
1242 priv->rx_bd_assign_index++;
1243 priv->rx_bd_assign_index &= (priv->num_rx_bds - 1);
1244
1245 priv->rx_bd_assign_ptr = priv->rx_bds +
1246 (priv->rx_bd_assign_index * DMA_DESC_SIZE);
1247
1248 return 0;
1249}
1250
1251/* bcmgenet_desc_rx - descriptor based rx process.
1252 * this could be called from bottom half, or from NAPI polling method.
1253 */
1254static unsigned int bcmgenet_desc_rx(struct bcmgenet_priv *priv,
1255 unsigned int budget)
1256{
1257 struct net_device *dev = priv->dev;
1258 struct enet_cb *cb;
1259 struct sk_buff *skb;
1260 u32 dma_length_status;
1261 unsigned long dma_flag;
1262 int len, err;
1263 unsigned int rxpktprocessed = 0, rxpkttoprocess;
1264 unsigned int p_index;
1265 unsigned int chksum_ok = 0;
1266
1267 p_index = bcmgenet_rdma_ring_readl(priv,
1268 DESC_INDEX, RDMA_PROD_INDEX);
1269 p_index &= DMA_P_INDEX_MASK;
1270
1271 if (p_index < priv->rx_c_index)
1272 rxpkttoprocess = (DMA_C_INDEX_MASK + 1) -
1273 priv->rx_c_index + p_index;
1274 else
1275 rxpkttoprocess = p_index - priv->rx_c_index;
1276
1277 netif_dbg(priv, rx_status, dev,
1278 "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
1279
1280 while ((rxpktprocessed < rxpkttoprocess) &&
1281 (rxpktprocessed < budget)) {
1282
1283 /* Unmap the packet contents such that we can use the
1284 * RSV from the 64 bytes descriptor when enabled and save
1285 * a 32-bits register read
1286 */
1287 cb = &priv->rx_cbs[priv->rx_read_ptr];
1288 skb = cb->skb;
1289 dma_unmap_single(&dev->dev, dma_unmap_addr(cb, dma_addr),
1290 priv->rx_buf_len, DMA_FROM_DEVICE);
1291
1292 if (!priv->desc_64b_en) {
1293 dma_length_status = dmadesc_get_length_status(priv,
1294 priv->rx_bds +
1295 (priv->rx_read_ptr *
1296 DMA_DESC_SIZE));
1297 } else {
1298 struct status_64 *status;
1299 status = (struct status_64 *)skb->data;
1300 dma_length_status = status->length_status;
1301 }
1302
1303 /* DMA flags and length are still valid no matter how
1304 * we got the Receive Status Vector (64B RSB or register)
1305 */
1306 dma_flag = dma_length_status & 0xffff;
1307 len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
1308
1309 netif_dbg(priv, rx_status, dev,
1310 "%s: p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
1311 __func__, p_index, priv->rx_c_index, priv->rx_read_ptr,
1312 dma_length_status);
1313
1314 rxpktprocessed++;
1315
1316 priv->rx_read_ptr++;
1317 priv->rx_read_ptr &= (priv->num_rx_bds - 1);
1318
1319 /* out of memory, just drop packets at the hardware level */
1320 if (unlikely(!skb)) {
1321 dev->stats.rx_dropped++;
1322 dev->stats.rx_errors++;
1323 goto refill;
1324 }
1325
1326 if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
1327 netif_err(priv, rx_status, dev,
1328 "Droping fragmented packet!\n");
1329 dev->stats.rx_dropped++;
1330 dev->stats.rx_errors++;
1331 dev_kfree_skb_any(cb->skb);
1332 cb->skb = NULL;
1333 goto refill;
1334 }
1335 /* report errors */
1336 if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
1337 DMA_RX_OV |
1338 DMA_RX_NO |
1339 DMA_RX_LG |
1340 DMA_RX_RXER))) {
1341 netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
1342 (unsigned int)dma_flag);
1343 if (dma_flag & DMA_RX_CRC_ERROR)
1344 dev->stats.rx_crc_errors++;
1345 if (dma_flag & DMA_RX_OV)
1346 dev->stats.rx_over_errors++;
1347 if (dma_flag & DMA_RX_NO)
1348 dev->stats.rx_frame_errors++;
1349 if (dma_flag & DMA_RX_LG)
1350 dev->stats.rx_length_errors++;
1351 dev->stats.rx_dropped++;
1352 dev->stats.rx_errors++;
1353
1354 /* discard the packet and advance consumer index.*/
1355 dev_kfree_skb_any(cb->skb);
1356 cb->skb = NULL;
1357 goto refill;
1358 } /* error packet */
1359
1360 chksum_ok = (dma_flag & priv->dma_rx_chk_bit) &&
1361 priv->desc_rxchk_en;
1362
1363 skb_put(skb, len);
1364 if (priv->desc_64b_en) {
1365 skb_pull(skb, 64);
1366 len -= 64;
1367 }
1368
1369 if (likely(chksum_ok))
1370 skb->ip_summed = CHECKSUM_UNNECESSARY;
1371
1372 /* remove hardware 2bytes added for IP alignment */
1373 skb_pull(skb, 2);
1374 len -= 2;
1375
1376 if (priv->crc_fwd_en) {
1377 skb_trim(skb, len - ETH_FCS_LEN);
1378 len -= ETH_FCS_LEN;
1379 }
1380
1381 /*Finish setting up the received SKB and send it to the kernel*/
1382 skb->protocol = eth_type_trans(skb, priv->dev);
1383 dev->stats.rx_packets++;
1384 dev->stats.rx_bytes += len;
1385 if (dma_flag & DMA_RX_MULT)
1386 dev->stats.multicast++;
1387
1388 /* Notify kernel */
1389 napi_gro_receive(&priv->napi, skb);
1390 cb->skb = NULL;
1391 netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
1392
1393 /* refill RX path on the current control block */
1394refill:
1395 err = bcmgenet_rx_refill(priv, cb);
1396 if (err)
1397 netif_err(priv, rx_err, dev, "Rx refill failed\n");
1398 }
1399
1400 return rxpktprocessed;
1401}
1402
1403/* Assign skb to RX DMA descriptor. */
1404static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv)
1405{
1406 struct enet_cb *cb;
1407 int ret = 0;
1408 int i;
1409
1410 netif_dbg(priv, hw, priv->dev, "%s:\n", __func__);
1411
1412 /* loop here for each buffer needing assign */
1413 for (i = 0; i < priv->num_rx_bds; i++) {
1414 cb = &priv->rx_cbs[priv->rx_bd_assign_index];
1415 if (cb->skb)
1416 continue;
1417
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001418 ret = bcmgenet_rx_refill(priv, cb);
1419 if (ret)
1420 break;
1421
1422 }
1423
1424 return ret;
1425}
1426
1427static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
1428{
1429 struct enet_cb *cb;
1430 int i;
1431
1432 for (i = 0; i < priv->num_rx_bds; i++) {
1433 cb = &priv->rx_cbs[i];
1434
1435 if (dma_unmap_addr(cb, dma_addr)) {
1436 dma_unmap_single(&priv->dev->dev,
1437 dma_unmap_addr(cb, dma_addr),
1438 priv->rx_buf_len, DMA_FROM_DEVICE);
1439 dma_unmap_addr_set(cb, dma_addr, 0);
1440 }
1441
1442 if (cb->skb)
1443 bcmgenet_free_cb(cb);
1444 }
1445}
1446
Florian Fainellie29585b2014-07-21 15:29:20 -07001447static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask,
1448 bool enable)
1449{
1450 u32 reg;
1451
1452 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1453 if (enable)
1454 reg |= mask;
1455 else
1456 reg &= ~mask;
1457 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
1458
1459 /* UniMAC stops on a packet boundary, wait for a full-size packet
1460 * to be processed
1461 */
1462 if (enable == 0)
1463 usleep_range(1000, 2000);
1464}
1465
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001466static int reset_umac(struct bcmgenet_priv *priv)
1467{
1468 struct device *kdev = &priv->pdev->dev;
1469 unsigned int timeout = 0;
1470 u32 reg;
1471
1472 /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
1473 bcmgenet_rbuf_ctrl_set(priv, 0);
1474 udelay(10);
1475
1476 /* disable MAC while updating its registers */
1477 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1478
1479 /* issue soft reset, wait for it to complete */
1480 bcmgenet_umac_writel(priv, CMD_SW_RESET, UMAC_CMD);
1481 while (timeout++ < 1000) {
1482 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1483 if (!(reg & CMD_SW_RESET))
1484 return 0;
1485
1486 udelay(1);
1487 }
1488
1489 if (timeout == 1000) {
1490 dev_err(kdev,
1491 "timeout waiting for MAC to come out of resetn\n");
1492 return -ETIMEDOUT;
1493 }
1494
1495 return 0;
1496}
1497
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001498static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
1499{
1500 /* Mask all interrupts.*/
1501 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1502 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
1503 bcmgenet_intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1504 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1505 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
1506 bcmgenet_intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1507}
1508
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001509static int init_umac(struct bcmgenet_priv *priv)
1510{
1511 struct device *kdev = &priv->pdev->dev;
1512 int ret;
1513 u32 reg, cpu_mask_clear;
1514
1515 dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
1516
1517 ret = reset_umac(priv);
1518 if (ret)
1519 return ret;
1520
1521 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1522 /* clear tx/rx counter */
1523 bcmgenet_umac_writel(priv,
1524 MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT, UMAC_MIB_CTRL);
1525 bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
1526
1527 bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
1528
1529 /* init rx registers, enable ip header optimization */
1530 reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
1531 reg |= RBUF_ALIGN_2B;
1532 bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
1533
1534 if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
1535 bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
1536
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001537 bcmgenet_intr_disable(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001538
1539 cpu_mask_clear = UMAC_IRQ_RXDMA_BDONE;
1540
1541 dev_dbg(kdev, "%s:Enabling RXDMA_BDONE interrupt\n", __func__);
1542
1543 /* Monitor cable plug/unpluged event for internal PHY */
1544 if (phy_is_internal(priv->phydev))
1545 cpu_mask_clear |= (UMAC_IRQ_LINK_DOWN | UMAC_IRQ_LINK_UP);
1546 else if (priv->ext_phy)
1547 cpu_mask_clear |= (UMAC_IRQ_LINK_DOWN | UMAC_IRQ_LINK_UP);
1548 else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
1549 reg = bcmgenet_bp_mc_get(priv);
1550 reg |= BIT(priv->hw_params->bp_in_en_shift);
1551
1552 /* bp_mask: back pressure mask */
1553 if (netif_is_multiqueue(priv->dev))
1554 reg |= priv->hw_params->bp_in_mask;
1555 else
1556 reg &= ~priv->hw_params->bp_in_mask;
1557 bcmgenet_bp_mc_set(priv, reg);
1558 }
1559
1560 /* Enable MDIO interrupts on GENET v3+ */
1561 if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
1562 cpu_mask_clear |= UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR;
1563
1564 bcmgenet_intrl2_0_writel(priv, cpu_mask_clear,
1565 INTRL2_CPU_MASK_CLEAR);
1566
1567 /* Enable rx/tx engine.*/
1568 dev_dbg(kdev, "done init umac\n");
1569
1570 return 0;
1571}
1572
1573/* Initialize all house-keeping variables for a TX ring, along
1574 * with corresponding hardware registers
1575 */
1576static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
1577 unsigned int index, unsigned int size,
1578 unsigned int write_ptr, unsigned int end_ptr)
1579{
1580 struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
1581 u32 words_per_bd = WORDS_PER_BD(priv);
1582 u32 flow_period_val = 0;
1583 unsigned int first_bd;
1584
1585 spin_lock_init(&ring->lock);
1586 ring->index = index;
1587 if (index == DESC_INDEX) {
1588 ring->queue = 0;
1589 ring->int_enable = bcmgenet_tx_ring16_int_enable;
1590 ring->int_disable = bcmgenet_tx_ring16_int_disable;
1591 } else {
1592 ring->queue = index + 1;
1593 ring->int_enable = bcmgenet_tx_ring_int_enable;
1594 ring->int_disable = bcmgenet_tx_ring_int_disable;
1595 }
1596 ring->cbs = priv->tx_cbs + write_ptr;
1597 ring->size = size;
1598 ring->c_index = 0;
1599 ring->free_bds = size;
1600 ring->write_ptr = write_ptr;
1601 ring->cb_ptr = write_ptr;
1602 ring->end_ptr = end_ptr - 1;
1603 ring->prod_index = 0;
1604
1605 /* Set flow period for ring != 16 */
1606 if (index != DESC_INDEX)
1607 flow_period_val = ENET_MAX_MTU_SIZE << 16;
1608
1609 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
1610 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
1611 bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
1612 /* Disable rate control for now */
1613 bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
1614 TDMA_FLOW_PERIOD);
1615 /* Unclassified traffic goes to ring 16 */
1616 bcmgenet_tdma_ring_writel(priv, index,
1617 ((size << DMA_RING_SIZE_SHIFT) | RX_BUF_LENGTH),
1618 DMA_RING_BUF_SIZE);
1619
1620 first_bd = write_ptr;
1621
1622 /* Set start and end address, read and write pointers */
1623 bcmgenet_tdma_ring_writel(priv, index, first_bd * words_per_bd,
1624 DMA_START_ADDR);
1625 bcmgenet_tdma_ring_writel(priv, index, first_bd * words_per_bd,
1626 TDMA_READ_PTR);
1627 bcmgenet_tdma_ring_writel(priv, index, first_bd,
1628 TDMA_WRITE_PTR);
1629 bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
1630 DMA_END_ADDR);
1631}
1632
1633/* Initialize a RDMA ring */
1634static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
1635 unsigned int index, unsigned int size)
1636{
1637 u32 words_per_bd = WORDS_PER_BD(priv);
1638 int ret;
1639
1640 priv->num_rx_bds = TOTAL_DESC;
1641 priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
1642 priv->rx_bd_assign_ptr = priv->rx_bds;
1643 priv->rx_bd_assign_index = 0;
1644 priv->rx_c_index = 0;
1645 priv->rx_read_ptr = 0;
1646 priv->rx_cbs = kzalloc(priv->num_rx_bds * sizeof(struct enet_cb),
1647 GFP_KERNEL);
1648 if (!priv->rx_cbs)
1649 return -ENOMEM;
1650
1651 ret = bcmgenet_alloc_rx_buffers(priv);
1652 if (ret) {
1653 kfree(priv->rx_cbs);
1654 return ret;
1655 }
1656
1657 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_WRITE_PTR);
1658 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
1659 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
1660 bcmgenet_rdma_ring_writel(priv, index,
1661 ((size << DMA_RING_SIZE_SHIFT) | RX_BUF_LENGTH),
1662 DMA_RING_BUF_SIZE);
1663 bcmgenet_rdma_ring_writel(priv, index, 0, DMA_START_ADDR);
1664 bcmgenet_rdma_ring_writel(priv, index,
1665 words_per_bd * size - 1, DMA_END_ADDR);
1666 bcmgenet_rdma_ring_writel(priv, index,
1667 (DMA_FC_THRESH_LO << DMA_XOFF_THRESHOLD_SHIFT) |
1668 DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
1669 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_READ_PTR);
1670
1671 return ret;
1672}
1673
1674/* init multi xmit queues, only available for GENET2+
1675 * the queue is partitioned as follows:
1676 *
1677 * queue 0 - 3 is priority based, each one has 32 descriptors,
1678 * with queue 0 being the highest priority queue.
1679 *
1680 * queue 16 is the default tx queue with GENET_DEFAULT_BD_CNT
1681 * descriptors: 256 - (number of tx queues * bds per queues) = 128
1682 * descriptors.
1683 *
1684 * The transmit control block pool is then partitioned as following:
1685 * - tx_cbs[0...127] are for queue 16
1686 * - tx_ring_cbs[0] points to tx_cbs[128..159]
1687 * - tx_ring_cbs[1] points to tx_cbs[160..191]
1688 * - tx_ring_cbs[2] points to tx_cbs[192..223]
1689 * - tx_ring_cbs[3] points to tx_cbs[224..255]
1690 */
1691static void bcmgenet_init_multiq(struct net_device *dev)
1692{
1693 struct bcmgenet_priv *priv = netdev_priv(dev);
1694 unsigned int i, dma_enable;
1695 u32 reg, dma_ctrl, ring_cfg = 0, dma_priority = 0;
1696
1697 if (!netif_is_multiqueue(dev)) {
1698 netdev_warn(dev, "called with non multi queue aware HW\n");
1699 return;
1700 }
1701
1702 dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
1703 dma_enable = dma_ctrl & DMA_EN;
1704 dma_ctrl &= ~DMA_EN;
1705 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
1706
1707 /* Enable strict priority arbiter mode */
1708 bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
1709
1710 for (i = 0; i < priv->hw_params->tx_queues; i++) {
1711 /* first 64 tx_cbs are reserved for default tx queue
1712 * (ring 16)
1713 */
1714 bcmgenet_init_tx_ring(priv, i, priv->hw_params->bds_cnt,
1715 i * priv->hw_params->bds_cnt,
1716 (i + 1) * priv->hw_params->bds_cnt);
1717
1718 /* Configure ring as decriptor ring and setup priority */
1719 ring_cfg |= 1 << i;
1720 dma_priority |= ((GENET_Q0_PRIORITY + i) <<
1721 (GENET_MAX_MQ_CNT + 1) * i);
1722 dma_ctrl |= 1 << (i + DMA_RING_BUF_EN_SHIFT);
1723 }
1724
1725 /* Enable rings */
1726 reg = bcmgenet_tdma_readl(priv, DMA_RING_CFG);
1727 reg |= ring_cfg;
1728 bcmgenet_tdma_writel(priv, reg, DMA_RING_CFG);
1729
1730 /* Use configured rings priority and set ring #16 priority */
1731 reg = bcmgenet_tdma_readl(priv, DMA_RING_PRIORITY);
1732 reg |= ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) << 20);
1733 reg |= dma_priority;
1734 bcmgenet_tdma_writel(priv, reg, DMA_PRIORITY);
1735
1736 /* Configure ring as descriptor ring and re-enable DMA if enabled */
1737 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
1738 reg |= dma_ctrl;
1739 if (dma_enable)
1740 reg |= DMA_EN;
1741 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
1742}
1743
1744static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
1745{
1746 int i;
1747
1748 /* disable DMA */
1749 bcmgenet_rdma_writel(priv, 0, DMA_CTRL);
1750 bcmgenet_tdma_writel(priv, 0, DMA_CTRL);
1751
1752 for (i = 0; i < priv->num_tx_bds; i++) {
1753 if (priv->tx_cbs[i].skb != NULL) {
1754 dev_kfree_skb(priv->tx_cbs[i].skb);
1755 priv->tx_cbs[i].skb = NULL;
1756 }
1757 }
1758
1759 bcmgenet_free_rx_buffers(priv);
1760 kfree(priv->rx_cbs);
1761 kfree(priv->tx_cbs);
1762}
1763
1764/* init_edma: Initialize DMA control register */
1765static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
1766{
1767 int ret;
1768
1769 netif_dbg(priv, hw, priv->dev, "bcmgenet: init_edma\n");
1770
1771 /* by default, enable ring 16 (descriptor based) */
1772 ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, TOTAL_DESC);
1773 if (ret) {
1774 netdev_err(priv->dev, "failed to initialize RX ring\n");
1775 return ret;
1776 }
1777
1778 /* init rDma */
1779 bcmgenet_rdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
1780
1781 /* Init tDma */
1782 bcmgenet_tdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
1783
1784 /* Initialize commont TX ring structures */
1785 priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
1786 priv->num_tx_bds = TOTAL_DESC;
1787 priv->tx_cbs = kzalloc(priv->num_tx_bds * sizeof(struct enet_cb),
1788 GFP_KERNEL);
1789 if (!priv->tx_cbs) {
1790 bcmgenet_fini_dma(priv);
1791 return -ENOMEM;
1792 }
1793
1794 /* initialize multi xmit queue */
1795 bcmgenet_init_multiq(priv->dev);
1796
1797 /* initialize special ring 16 */
1798 bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_DEFAULT_BD_CNT,
1799 priv->hw_params->tx_queues * priv->hw_params->bds_cnt,
1800 TOTAL_DESC);
1801
1802 return 0;
1803}
1804
1805/* NAPI polling method*/
1806static int bcmgenet_poll(struct napi_struct *napi, int budget)
1807{
1808 struct bcmgenet_priv *priv = container_of(napi,
1809 struct bcmgenet_priv, napi);
1810 unsigned int work_done;
1811
1812 /* tx reclaim */
1813 bcmgenet_tx_reclaim(priv->dev, &priv->tx_rings[DESC_INDEX]);
1814
1815 work_done = bcmgenet_desc_rx(priv, budget);
1816
1817 /* Advancing our consumer index*/
1818 priv->rx_c_index += work_done;
1819 priv->rx_c_index &= DMA_C_INDEX_MASK;
1820 bcmgenet_rdma_ring_writel(priv, DESC_INDEX,
1821 priv->rx_c_index, RDMA_CONS_INDEX);
1822 if (work_done < budget) {
1823 napi_complete(napi);
1824 bcmgenet_intrl2_0_writel(priv,
1825 UMAC_IRQ_RXDMA_BDONE, INTRL2_CPU_MASK_CLEAR);
1826 }
1827
1828 return work_done;
1829}
1830
1831/* Interrupt bottom half */
1832static void bcmgenet_irq_task(struct work_struct *work)
1833{
1834 struct bcmgenet_priv *priv = container_of(
1835 work, struct bcmgenet_priv, bcmgenet_irq_work);
1836
1837 netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
1838
Florian Fainelli8fdb0e02014-07-21 15:29:26 -07001839 if (priv->irq0_stat & UMAC_IRQ_MPD_R) {
1840 priv->irq0_stat &= ~UMAC_IRQ_MPD_R;
1841 netif_dbg(priv, wol, priv->dev,
1842 "magic packet detected, waking up\n");
1843 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
1844 }
1845
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001846 /* Link UP/DOWN event */
1847 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
1848 (priv->irq0_stat & (UMAC_IRQ_LINK_UP|UMAC_IRQ_LINK_DOWN))) {
Florian Fainelli80d8e962014-02-24 16:56:11 -08001849 phy_mac_interrupt(priv->phydev,
1850 priv->irq0_stat & UMAC_IRQ_LINK_UP);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001851 priv->irq0_stat &= ~(UMAC_IRQ_LINK_UP|UMAC_IRQ_LINK_DOWN);
1852 }
1853}
1854
1855/* bcmgenet_isr1: interrupt handler for ring buffer. */
1856static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
1857{
1858 struct bcmgenet_priv *priv = dev_id;
1859 unsigned int index;
1860
1861 /* Save irq status for bottom-half processing. */
1862 priv->irq1_stat =
1863 bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
1864 ~priv->int1_mask;
1865 /* clear inerrupts*/
1866 bcmgenet_intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
1867
1868 netif_dbg(priv, intr, priv->dev,
1869 "%s: IRQ=0x%x\n", __func__, priv->irq1_stat);
1870 /* Check the MBDONE interrupts.
1871 * packet is done, reclaim descriptors
1872 */
1873 if (priv->irq1_stat & 0x0000ffff) {
1874 index = 0;
1875 for (index = 0; index < 16; index++) {
1876 if (priv->irq1_stat & (1 << index))
1877 bcmgenet_tx_reclaim(priv->dev,
1878 &priv->tx_rings[index]);
1879 }
1880 }
1881 return IRQ_HANDLED;
1882}
1883
1884/* bcmgenet_isr0: Handle various interrupts. */
1885static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
1886{
1887 struct bcmgenet_priv *priv = dev_id;
1888
1889 /* Save irq status for bottom-half processing. */
1890 priv->irq0_stat =
1891 bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
1892 ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
1893 /* clear inerrupts*/
1894 bcmgenet_intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
1895
1896 netif_dbg(priv, intr, priv->dev,
1897 "IRQ=0x%x\n", priv->irq0_stat);
1898
1899 if (priv->irq0_stat & (UMAC_IRQ_RXDMA_BDONE | UMAC_IRQ_RXDMA_PDONE)) {
1900 /* We use NAPI(software interrupt throttling, if
1901 * Rx Descriptor throttling is not used.
1902 * Disable interrupt, will be enabled in the poll method.
1903 */
1904 if (likely(napi_schedule_prep(&priv->napi))) {
1905 bcmgenet_intrl2_0_writel(priv,
1906 UMAC_IRQ_RXDMA_BDONE, INTRL2_CPU_MASK_SET);
1907 __napi_schedule(&priv->napi);
1908 }
1909 }
1910 if (priv->irq0_stat &
1911 (UMAC_IRQ_TXDMA_BDONE | UMAC_IRQ_TXDMA_PDONE)) {
1912 /* Tx reclaim */
1913 bcmgenet_tx_reclaim(priv->dev, &priv->tx_rings[DESC_INDEX]);
1914 }
1915 if (priv->irq0_stat & (UMAC_IRQ_PHY_DET_R |
1916 UMAC_IRQ_PHY_DET_F |
1917 UMAC_IRQ_LINK_UP |
1918 UMAC_IRQ_LINK_DOWN |
1919 UMAC_IRQ_HFB_SM |
1920 UMAC_IRQ_HFB_MM |
1921 UMAC_IRQ_MPD_R)) {
1922 /* all other interested interrupts handled in bottom half */
1923 schedule_work(&priv->bcmgenet_irq_work);
1924 }
1925
1926 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
1927 priv->irq0_stat & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
1928 priv->irq0_stat &= ~(UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
1929 wake_up(&priv->wq);
1930 }
1931
1932 return IRQ_HANDLED;
1933}
1934
Florian Fainelli85620562014-07-21 15:29:23 -07001935static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
1936{
1937 struct bcmgenet_priv *priv = dev_id;
1938
1939 pm_wakeup_event(&priv->pdev->dev, 0);
1940
1941 return IRQ_HANDLED;
1942}
1943
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001944static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
1945{
1946 u32 reg;
1947
1948 reg = bcmgenet_rbuf_ctrl_get(priv);
1949 reg |= BIT(1);
1950 bcmgenet_rbuf_ctrl_set(priv, reg);
1951 udelay(10);
1952
1953 reg &= ~BIT(1);
1954 bcmgenet_rbuf_ctrl_set(priv, reg);
1955 udelay(10);
1956}
1957
1958static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
1959 unsigned char *addr)
1960{
1961 bcmgenet_umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
1962 (addr[2] << 8) | addr[3], UMAC_MAC0);
1963 bcmgenet_umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
1964}
1965
1966static int bcmgenet_wol_resume(struct bcmgenet_priv *priv)
1967{
1968 int ret;
1969
1970 /* From WOL-enabled suspend, switch to regular clock */
1971 clk_disable(priv->clk_wol);
1972 /* init umac registers to synchronize s/w with h/w */
1973 ret = init_umac(priv);
1974 if (ret)
1975 return ret;
1976
Florian Fainelli80d8e962014-02-24 16:56:11 -08001977 phy_init_hw(priv->phydev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001978 /* Speed settings must be restored */
1979 bcmgenet_mii_config(priv->dev);
1980
1981 return 0;
1982}
1983
1984/* Returns a reusable dma control register value */
1985static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
1986{
1987 u32 reg;
1988 u32 dma_ctrl;
1989
1990 /* disable DMA */
1991 dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
1992 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
1993 reg &= ~dma_ctrl;
1994 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
1995
1996 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
1997 reg &= ~dma_ctrl;
1998 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
1999
2000 bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
2001 udelay(10);
2002 bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
2003
2004 return dma_ctrl;
2005}
2006
2007static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
2008{
2009 u32 reg;
2010
2011 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2012 reg |= dma_ctrl;
2013 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2014
2015 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2016 reg |= dma_ctrl;
2017 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2018}
2019
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002020static void bcmgenet_netif_start(struct net_device *dev)
2021{
2022 struct bcmgenet_priv *priv = netdev_priv(dev);
2023
2024 /* Start the network engine */
2025 napi_enable(&priv->napi);
2026
2027 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
2028
2029 if (phy_is_internal(priv->phydev))
2030 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
2031
2032 netif_tx_start_all_queues(dev);
2033
2034 phy_start(priv->phydev);
2035}
2036
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002037static int bcmgenet_open(struct net_device *dev)
2038{
2039 struct bcmgenet_priv *priv = netdev_priv(dev);
2040 unsigned long dma_ctrl;
2041 u32 reg;
2042 int ret;
2043
2044 netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
2045
2046 /* Turn on the clock */
2047 if (!IS_ERR(priv->clk))
2048 clk_prepare_enable(priv->clk);
2049
2050 /* take MAC out of reset */
2051 bcmgenet_umac_reset(priv);
2052
2053 ret = init_umac(priv);
2054 if (ret)
2055 goto err_clk_disable;
2056
2057 /* disable ethernet MAC while updating its registers */
Florian Fainellie29585b2014-07-21 15:29:20 -07002058 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002059
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002060 /* Make sure we reflect the value of CRC_CMD_FWD */
2061 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
2062 priv->crc_fwd_en = !!(reg & CMD_CRC_FWD);
2063
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002064 bcmgenet_set_hw_addr(priv, dev->dev_addr);
2065
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002066 if (phy_is_internal(priv->phydev)) {
2067 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
2068 reg |= EXT_ENERGY_DET_MASK;
2069 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
2070 }
2071
2072 /* Disable RX/TX DMA and flush TX queues */
2073 dma_ctrl = bcmgenet_dma_disable(priv);
2074
2075 /* Reinitialize TDMA and RDMA and SW housekeeping */
2076 ret = bcmgenet_init_dma(priv);
2077 if (ret) {
2078 netdev_err(dev, "failed to initialize DMA\n");
2079 goto err_fini_dma;
2080 }
2081
2082 /* Always enable ring 16 - descriptor ring */
2083 bcmgenet_enable_dma(priv, dma_ctrl);
2084
2085 ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
2086 dev->name, priv);
2087 if (ret < 0) {
2088 netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
2089 goto err_fini_dma;
2090 }
2091
2092 ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
2093 dev->name, priv);
2094 if (ret < 0) {
2095 netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
2096 goto err_irq0;
2097 }
2098
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002099 bcmgenet_netif_start(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002100
2101 return 0;
2102
2103err_irq0:
2104 free_irq(priv->irq0, dev);
2105err_fini_dma:
2106 bcmgenet_fini_dma(priv);
2107err_clk_disable:
2108 if (!IS_ERR(priv->clk))
2109 clk_disable_unprepare(priv->clk);
2110 return ret;
2111}
2112
2113static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
2114{
2115 int ret = 0;
2116 int timeout = 0;
2117 u32 reg;
2118
2119 /* Disable TDMA to stop add more frames in TX DMA */
2120 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2121 reg &= ~DMA_EN;
2122 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2123
2124 /* Check TDMA status register to confirm TDMA is disabled */
2125 while (timeout++ < DMA_TIMEOUT_VAL) {
2126 reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
2127 if (reg & DMA_DISABLED)
2128 break;
2129
2130 udelay(1);
2131 }
2132
2133 if (timeout == DMA_TIMEOUT_VAL) {
2134 netdev_warn(priv->dev,
2135 "Timed out while disabling TX DMA\n");
2136 ret = -ETIMEDOUT;
2137 }
2138
2139 /* Wait 10ms for packet drain in both tx and rx dma */
2140 usleep_range(10000, 20000);
2141
2142 /* Disable RDMA */
2143 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2144 reg &= ~DMA_EN;
2145 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2146
2147 timeout = 0;
2148 /* Check RDMA status register to confirm RDMA is disabled */
2149 while (timeout++ < DMA_TIMEOUT_VAL) {
2150 reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
2151 if (reg & DMA_DISABLED)
2152 break;
2153
2154 udelay(1);
2155 }
2156
2157 if (timeout == DMA_TIMEOUT_VAL) {
2158 netdev_warn(priv->dev,
2159 "Timed out while disabling RX DMA\n");
2160 ret = -ETIMEDOUT;
2161 }
2162
2163 return ret;
2164}
2165
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002166static void bcmgenet_netif_stop(struct net_device *dev)
2167{
2168 struct bcmgenet_priv *priv = netdev_priv(dev);
2169
2170 netif_tx_stop_all_queues(dev);
2171 napi_disable(&priv->napi);
2172 phy_stop(priv->phydev);
2173
2174 bcmgenet_intr_disable(priv);
2175
2176 /* Wait for pending work items to complete. Since interrupts are
2177 * disabled no new work will be scheduled.
2178 */
2179 cancel_work_sync(&priv->bcmgenet_irq_work);
2180}
2181
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002182static int bcmgenet_close(struct net_device *dev)
2183{
2184 struct bcmgenet_priv *priv = netdev_priv(dev);
2185 int ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002186
2187 netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
2188
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002189 bcmgenet_netif_stop(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002190
2191 /* Disable MAC receive */
Florian Fainellie29585b2014-07-21 15:29:20 -07002192 umac_enable_set(priv, CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002193
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002194 ret = bcmgenet_dma_teardown(priv);
2195 if (ret)
2196 return ret;
2197
2198 /* Disable MAC transmit. TX DMA disabled have to done before this */
Florian Fainellie29585b2014-07-21 15:29:20 -07002199 umac_enable_set(priv, CMD_TX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002200
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002201 /* tx reclaim */
2202 bcmgenet_tx_reclaim_all(dev);
2203 bcmgenet_fini_dma(priv);
2204
2205 free_irq(priv->irq0, priv);
2206 free_irq(priv->irq1, priv);
2207
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002208 if (phy_is_internal(priv->phydev))
2209 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
2210
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002211 if (!IS_ERR(priv->clk))
2212 clk_disable_unprepare(priv->clk);
2213
2214 return 0;
2215}
2216
2217static void bcmgenet_timeout(struct net_device *dev)
2218{
2219 struct bcmgenet_priv *priv = netdev_priv(dev);
2220
2221 netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
2222
2223 dev->trans_start = jiffies;
2224
2225 dev->stats.tx_errors++;
2226
2227 netif_tx_wake_all_queues(dev);
2228}
2229
2230#define MAX_MC_COUNT 16
2231
2232static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
2233 unsigned char *addr,
2234 int *i,
2235 int *mc)
2236{
2237 u32 reg;
2238
2239 bcmgenet_umac_writel(priv,
2240 addr[0] << 8 | addr[1], UMAC_MDF_ADDR + (*i * 4));
2241 bcmgenet_umac_writel(priv,
2242 addr[2] << 24 | addr[3] << 16 |
2243 addr[4] << 8 | addr[5],
2244 UMAC_MDF_ADDR + ((*i + 1) * 4));
2245 reg = bcmgenet_umac_readl(priv, UMAC_MDF_CTRL);
2246 reg |= (1 << (MAX_MC_COUNT - *mc));
2247 bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
2248 *i += 2;
2249 (*mc)++;
2250}
2251
2252static void bcmgenet_set_rx_mode(struct net_device *dev)
2253{
2254 struct bcmgenet_priv *priv = netdev_priv(dev);
2255 struct netdev_hw_addr *ha;
2256 int i, mc;
2257 u32 reg;
2258
2259 netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
2260
2261 /* Promiscous mode */
2262 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
2263 if (dev->flags & IFF_PROMISC) {
2264 reg |= CMD_PROMISC;
2265 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
2266 bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
2267 return;
2268 } else {
2269 reg &= ~CMD_PROMISC;
2270 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
2271 }
2272
2273 /* UniMac doesn't support ALLMULTI */
2274 if (dev->flags & IFF_ALLMULTI) {
2275 netdev_warn(dev, "ALLMULTI is not supported\n");
2276 return;
2277 }
2278
2279 /* update MDF filter */
2280 i = 0;
2281 mc = 0;
2282 /* Broadcast */
2283 bcmgenet_set_mdf_addr(priv, dev->broadcast, &i, &mc);
2284 /* my own address.*/
2285 bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i, &mc);
2286 /* Unicast list*/
2287 if (netdev_uc_count(dev) > (MAX_MC_COUNT - mc))
2288 return;
2289
2290 if (!netdev_uc_empty(dev))
2291 netdev_for_each_uc_addr(ha, dev)
2292 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
2293 /* Multicast */
2294 if (netdev_mc_empty(dev) || netdev_mc_count(dev) >= (MAX_MC_COUNT - mc))
2295 return;
2296
2297 netdev_for_each_mc_addr(ha, dev)
2298 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
2299}
2300
2301/* Set the hardware MAC address. */
2302static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
2303{
2304 struct sockaddr *addr = p;
2305
2306 /* Setting the MAC address at the hardware level is not possible
2307 * without disabling the UniMAC RX/TX enable bits.
2308 */
2309 if (netif_running(dev))
2310 return -EBUSY;
2311
2312 ether_addr_copy(dev->dev_addr, addr->sa_data);
2313
2314 return 0;
2315}
2316
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002317static const struct net_device_ops bcmgenet_netdev_ops = {
2318 .ndo_open = bcmgenet_open,
2319 .ndo_stop = bcmgenet_close,
2320 .ndo_start_xmit = bcmgenet_xmit,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002321 .ndo_tx_timeout = bcmgenet_timeout,
2322 .ndo_set_rx_mode = bcmgenet_set_rx_mode,
2323 .ndo_set_mac_address = bcmgenet_set_mac_addr,
2324 .ndo_do_ioctl = bcmgenet_ioctl,
2325 .ndo_set_features = bcmgenet_set_features,
2326};
2327
2328/* Array of GENET hardware parameters/characteristics */
2329static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
2330 [GENET_V1] = {
2331 .tx_queues = 0,
2332 .rx_queues = 0,
2333 .bds_cnt = 0,
2334 .bp_in_en_shift = 16,
2335 .bp_in_mask = 0xffff,
2336 .hfb_filter_cnt = 16,
2337 .qtag_mask = 0x1F,
2338 .hfb_offset = 0x1000,
2339 .rdma_offset = 0x2000,
2340 .tdma_offset = 0x3000,
2341 .words_per_bd = 2,
2342 },
2343 [GENET_V2] = {
2344 .tx_queues = 4,
2345 .rx_queues = 4,
2346 .bds_cnt = 32,
2347 .bp_in_en_shift = 16,
2348 .bp_in_mask = 0xffff,
2349 .hfb_filter_cnt = 16,
2350 .qtag_mask = 0x1F,
2351 .tbuf_offset = 0x0600,
2352 .hfb_offset = 0x1000,
2353 .hfb_reg_offset = 0x2000,
2354 .rdma_offset = 0x3000,
2355 .tdma_offset = 0x4000,
2356 .words_per_bd = 2,
2357 .flags = GENET_HAS_EXT,
2358 },
2359 [GENET_V3] = {
2360 .tx_queues = 4,
2361 .rx_queues = 4,
2362 .bds_cnt = 32,
2363 .bp_in_en_shift = 17,
2364 .bp_in_mask = 0x1ffff,
2365 .hfb_filter_cnt = 48,
2366 .qtag_mask = 0x3F,
2367 .tbuf_offset = 0x0600,
2368 .hfb_offset = 0x8000,
2369 .hfb_reg_offset = 0xfc00,
2370 .rdma_offset = 0x10000,
2371 .tdma_offset = 0x11000,
2372 .words_per_bd = 2,
2373 .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR,
2374 },
2375 [GENET_V4] = {
2376 .tx_queues = 4,
2377 .rx_queues = 4,
2378 .bds_cnt = 32,
2379 .bp_in_en_shift = 17,
2380 .bp_in_mask = 0x1ffff,
2381 .hfb_filter_cnt = 48,
2382 .qtag_mask = 0x3F,
2383 .tbuf_offset = 0x0600,
2384 .hfb_offset = 0x8000,
2385 .hfb_reg_offset = 0xfc00,
2386 .rdma_offset = 0x2000,
2387 .tdma_offset = 0x4000,
2388 .words_per_bd = 3,
2389 .flags = GENET_HAS_40BITS | GENET_HAS_EXT | GENET_HAS_MDIO_INTR,
2390 },
2391};
2392
2393/* Infer hardware parameters from the detected GENET version */
2394static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
2395{
2396 struct bcmgenet_hw_params *params;
2397 u32 reg;
2398 u8 major;
2399
2400 if (GENET_IS_V4(priv)) {
2401 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
2402 genet_dma_ring_regs = genet_dma_ring_regs_v4;
2403 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
2404 priv->version = GENET_V4;
2405 } else if (GENET_IS_V3(priv)) {
2406 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
2407 genet_dma_ring_regs = genet_dma_ring_regs_v123;
2408 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
2409 priv->version = GENET_V3;
2410 } else if (GENET_IS_V2(priv)) {
2411 bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
2412 genet_dma_ring_regs = genet_dma_ring_regs_v123;
2413 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
2414 priv->version = GENET_V2;
2415 } else if (GENET_IS_V1(priv)) {
2416 bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
2417 genet_dma_ring_regs = genet_dma_ring_regs_v123;
2418 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
2419 priv->version = GENET_V1;
2420 }
2421
2422 /* enum genet_version starts at 1 */
2423 priv->hw_params = &bcmgenet_hw_params[priv->version];
2424 params = priv->hw_params;
2425
2426 /* Read GENET HW version */
2427 reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
2428 major = (reg >> 24 & 0x0f);
2429 if (major == 5)
2430 major = 4;
2431 else if (major == 0)
2432 major = 1;
2433 if (major != priv->version) {
2434 dev_err(&priv->pdev->dev,
2435 "GENET version mismatch, got: %d, configured for: %d\n",
2436 major, priv->version);
2437 }
2438
2439 /* Print the GENET core version */
2440 dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
2441 major, (reg >> 16) & 0x0f, reg & 0xffff);
2442
2443#ifdef CONFIG_PHYS_ADDR_T_64BIT
2444 if (!(params->flags & GENET_HAS_40BITS))
2445 pr_warn("GENET does not support 40-bits PA\n");
2446#endif
2447
2448 pr_debug("Configuration for version: %d\n"
2449 "TXq: %1d, RXq: %1d, BDs: %1d\n"
2450 "BP << en: %2d, BP msk: 0x%05x\n"
2451 "HFB count: %2d, QTAQ msk: 0x%05x\n"
2452 "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
2453 "RDMA: 0x%05x, TDMA: 0x%05x\n"
2454 "Words/BD: %d\n",
2455 priv->version,
2456 params->tx_queues, params->rx_queues, params->bds_cnt,
2457 params->bp_in_en_shift, params->bp_in_mask,
2458 params->hfb_filter_cnt, params->qtag_mask,
2459 params->tbuf_offset, params->hfb_offset,
2460 params->hfb_reg_offset,
2461 params->rdma_offset, params->tdma_offset,
2462 params->words_per_bd);
2463}
2464
2465static const struct of_device_id bcmgenet_match[] = {
2466 { .compatible = "brcm,genet-v1", .data = (void *)GENET_V1 },
2467 { .compatible = "brcm,genet-v2", .data = (void *)GENET_V2 },
2468 { .compatible = "brcm,genet-v3", .data = (void *)GENET_V3 },
2469 { .compatible = "brcm,genet-v4", .data = (void *)GENET_V4 },
2470 { },
2471};
2472
2473static int bcmgenet_probe(struct platform_device *pdev)
2474{
2475 struct device_node *dn = pdev->dev.of_node;
2476 const struct of_device_id *of_id;
2477 struct bcmgenet_priv *priv;
2478 struct net_device *dev;
2479 const void *macaddr;
2480 struct resource *r;
2481 int err = -EIO;
2482
2483 /* Up to GENET_MAX_MQ_CNT + 1 TX queues and a single RX queue */
2484 dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1, 1);
2485 if (!dev) {
2486 dev_err(&pdev->dev, "can't allocate net device\n");
2487 return -ENOMEM;
2488 }
2489
2490 of_id = of_match_node(bcmgenet_match, dn);
2491 if (!of_id)
2492 return -EINVAL;
2493
2494 priv = netdev_priv(dev);
2495 priv->irq0 = platform_get_irq(pdev, 0);
2496 priv->irq1 = platform_get_irq(pdev, 1);
Florian Fainelli85620562014-07-21 15:29:23 -07002497 priv->wol_irq = platform_get_irq(pdev, 2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002498 if (!priv->irq0 || !priv->irq1) {
2499 dev_err(&pdev->dev, "can't find IRQs\n");
2500 err = -EINVAL;
2501 goto err;
2502 }
2503
2504 macaddr = of_get_mac_address(dn);
2505 if (!macaddr) {
2506 dev_err(&pdev->dev, "can't find MAC address\n");
2507 err = -EINVAL;
2508 goto err;
2509 }
2510
2511 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam5343a102014-02-24 00:47:24 -03002512 priv->base = devm_ioremap_resource(&pdev->dev, r);
2513 if (IS_ERR(priv->base)) {
2514 err = PTR_ERR(priv->base);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002515 goto err;
2516 }
2517
2518 SET_NETDEV_DEV(dev, &pdev->dev);
2519 dev_set_drvdata(&pdev->dev, dev);
2520 ether_addr_copy(dev->dev_addr, macaddr);
2521 dev->watchdog_timeo = 2 * HZ;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002522 dev->ethtool_ops = &bcmgenet_ethtool_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002523 dev->netdev_ops = &bcmgenet_netdev_ops;
2524 netif_napi_add(dev, &priv->napi, bcmgenet_poll, 64);
2525
2526 priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
2527
2528 /* Set hardware features */
2529 dev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM |
2530 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
2531
Florian Fainelli85620562014-07-21 15:29:23 -07002532 /* Request the WOL interrupt and advertise suspend if available */
2533 priv->wol_irq_disabled = true;
2534 err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0,
2535 dev->name, priv);
2536 if (!err)
2537 device_set_wakeup_capable(&pdev->dev, 1);
2538
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002539 /* Set the needed headroom to account for any possible
2540 * features enabling/disabling at runtime
2541 */
2542 dev->needed_headroom += 64;
2543
2544 netdev_boot_setup_check(dev);
2545
2546 priv->dev = dev;
2547 priv->pdev = pdev;
2548 priv->version = (enum bcmgenet_version)of_id->data;
2549
2550 bcmgenet_set_hw_params(priv);
2551
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002552 /* Mii wait queue */
2553 init_waitqueue_head(&priv->wq);
2554 /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
2555 priv->rx_buf_len = RX_BUF_LENGTH;
2556 INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
2557
2558 priv->clk = devm_clk_get(&priv->pdev->dev, "enet");
2559 if (IS_ERR(priv->clk))
2560 dev_warn(&priv->pdev->dev, "failed to get enet clock\n");
2561
2562 priv->clk_wol = devm_clk_get(&priv->pdev->dev, "enet-wol");
2563 if (IS_ERR(priv->clk_wol))
2564 dev_warn(&priv->pdev->dev, "failed to get enet-wol clock\n");
2565
2566 if (!IS_ERR(priv->clk))
2567 clk_prepare_enable(priv->clk);
2568
2569 err = reset_umac(priv);
2570 if (err)
2571 goto err_clk_disable;
2572
2573 err = bcmgenet_mii_init(dev);
2574 if (err)
2575 goto err_clk_disable;
2576
2577 /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
2578 * just the ring 16 descriptor based TX
2579 */
2580 netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
2581 netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
2582
Florian Fainelli219575e2014-06-26 10:26:21 -07002583 /* libphy will determine the link state */
2584 netif_carrier_off(dev);
2585
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002586 /* Turn off the main clock, WOL clock is handled separately */
2587 if (!IS_ERR(priv->clk))
2588 clk_disable_unprepare(priv->clk);
2589
Florian Fainelli0f50ce92014-06-26 10:26:20 -07002590 err = register_netdev(dev);
2591 if (err)
2592 goto err;
2593
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002594 return err;
2595
2596err_clk_disable:
2597 if (!IS_ERR(priv->clk))
2598 clk_disable_unprepare(priv->clk);
2599err:
2600 free_netdev(dev);
2601 return err;
2602}
2603
2604static int bcmgenet_remove(struct platform_device *pdev)
2605{
2606 struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
2607
2608 dev_set_drvdata(&pdev->dev, NULL);
2609 unregister_netdev(priv->dev);
2610 bcmgenet_mii_exit(priv->dev);
2611 free_netdev(priv->dev);
2612
2613 return 0;
2614}
2615
Florian Fainellib6e978e2014-07-21 15:29:22 -07002616#ifdef CONFIG_PM_SLEEP
2617static int bcmgenet_suspend(struct device *d)
2618{
2619 struct net_device *dev = dev_get_drvdata(d);
2620 struct bcmgenet_priv *priv = netdev_priv(dev);
2621 int ret;
2622
2623 if (!netif_running(dev))
2624 return 0;
2625
2626 bcmgenet_netif_stop(dev);
2627
2628 netif_device_detach(dev);
2629
2630 /* Disable MAC receive */
2631 umac_enable_set(priv, CMD_RX_EN, false);
2632
2633 ret = bcmgenet_dma_teardown(priv);
2634 if (ret)
2635 return ret;
2636
2637 /* Disable MAC transmit. TX DMA disabled have to done before this */
2638 umac_enable_set(priv, CMD_TX_EN, false);
2639
2640 /* tx reclaim */
2641 bcmgenet_tx_reclaim_all(dev);
2642 bcmgenet_fini_dma(priv);
2643
2644 /* Turn off the clocks */
2645 clk_disable_unprepare(priv->clk);
2646
2647 return 0;
2648}
2649
2650static int bcmgenet_resume(struct device *d)
2651{
2652 struct net_device *dev = dev_get_drvdata(d);
2653 struct bcmgenet_priv *priv = netdev_priv(dev);
2654 unsigned long dma_ctrl;
2655 int ret;
2656 u32 reg;
2657
2658 if (!netif_running(dev))
2659 return 0;
2660
2661 /* Turn on the clock */
2662 ret = clk_prepare_enable(priv->clk);
2663 if (ret)
2664 return ret;
2665
2666 bcmgenet_umac_reset(priv);
2667
2668 ret = init_umac(priv);
2669 if (ret)
2670 goto out_clk_disable;
2671
2672 /* disable ethernet MAC while updating its registers */
2673 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
2674
2675 bcmgenet_set_hw_addr(priv, dev->dev_addr);
2676
2677 if (phy_is_internal(priv->phydev)) {
2678 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
2679 reg |= EXT_ENERGY_DET_MASK;
2680 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
2681 }
2682
2683 /* Disable RX/TX DMA and flush TX queues */
2684 dma_ctrl = bcmgenet_dma_disable(priv);
2685
2686 /* Reinitialize TDMA and RDMA and SW housekeeping */
2687 ret = bcmgenet_init_dma(priv);
2688 if (ret) {
2689 netdev_err(dev, "failed to initialize DMA\n");
2690 goto out_clk_disable;
2691 }
2692
2693 /* Always enable ring 16 - descriptor ring */
2694 bcmgenet_enable_dma(priv, dma_ctrl);
2695
2696 netif_device_attach(dev);
2697
2698 bcmgenet_netif_start(dev);
2699
2700 return 0;
2701
2702out_clk_disable:
2703 clk_disable_unprepare(priv->clk);
2704 return ret;
2705}
2706#endif /* CONFIG_PM_SLEEP */
2707
2708static SIMPLE_DEV_PM_OPS(bcmgenet_pm_ops, bcmgenet_suspend, bcmgenet_resume);
2709
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002710static struct platform_driver bcmgenet_driver = {
2711 .probe = bcmgenet_probe,
2712 .remove = bcmgenet_remove,
2713 .driver = {
2714 .name = "bcmgenet",
2715 .owner = THIS_MODULE,
2716 .of_match_table = bcmgenet_match,
Florian Fainellib6e978e2014-07-21 15:29:22 -07002717 .pm = &bcmgenet_pm_ops,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002718 },
2719};
2720module_platform_driver(bcmgenet_driver);
2721
2722MODULE_AUTHOR("Broadcom Corporation");
2723MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
2724MODULE_ALIAS("platform:bcmgenet");
2725MODULE_LICENSE("GPL");