2 * Broadcom BCM7xxx System Port Ethernet MAC driver
4 * Copyright (C) 2014 Broadcom Corporation
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.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/platform_device.h>
21 #include <linux/of_net.h>
22 #include <linux/of_mdio.h>
23 #include <linux/phy.h>
24 #include <linux/phy_fixed.h>
29 #include "bcmsysport.h"
31 /* I/O accessors register helpers */
32 #define BCM_SYSPORT_IO_MACRO(name, offset) \
33 static inline u32 name##_readl(struct bcm_sysport_priv *priv, u32 off) \
35 u32 reg = readl_relaxed(priv->base + offset + off); \
38 static inline void name##_writel(struct bcm_sysport_priv *priv, \
41 writel_relaxed(val, priv->base + offset + off); \
44 BCM_SYSPORT_IO_MACRO(intrl2_0, SYS_PORT_INTRL2_0_OFFSET);
45 BCM_SYSPORT_IO_MACRO(intrl2_1, SYS_PORT_INTRL2_1_OFFSET);
46 BCM_SYSPORT_IO_MACRO(umac, SYS_PORT_UMAC_OFFSET);
47 BCM_SYSPORT_IO_MACRO(gib, SYS_PORT_GIB_OFFSET);
48 BCM_SYSPORT_IO_MACRO(tdma, SYS_PORT_TDMA_OFFSET);
49 BCM_SYSPORT_IO_MACRO(rxchk, SYS_PORT_RXCHK_OFFSET);
50 BCM_SYSPORT_IO_MACRO(txchk, SYS_PORT_TXCHK_OFFSET);
51 BCM_SYSPORT_IO_MACRO(rbuf, SYS_PORT_RBUF_OFFSET);
52 BCM_SYSPORT_IO_MACRO(tbuf, SYS_PORT_TBUF_OFFSET);
53 BCM_SYSPORT_IO_MACRO(topctrl, SYS_PORT_TOPCTRL_OFFSET);
55 /* On SYSTEMPORT Lite, any register after RDMA_STATUS has the exact
56 * same layout, except it has been moved by 4 bytes up, *sigh*
58 static inline u32 rdma_readl(struct bcm_sysport_priv *priv, u32 off)
60 if (priv->is_lite && off >= RDMA_STATUS)
62 return readl_relaxed(priv->base + SYS_PORT_RDMA_OFFSET + off);
65 static inline void rdma_writel(struct bcm_sysport_priv *priv, u32 val, u32 off)
67 if (priv->is_lite && off >= RDMA_STATUS)
69 writel_relaxed(val, priv->base + SYS_PORT_RDMA_OFFSET + off);
72 static inline u32 tdma_control_bit(struct bcm_sysport_priv *priv, u32 bit)
84 /* L2-interrupt masking/unmasking helpers, does automatic saving of the applied
85 * mask in a software copy to avoid CPU_MASK_STATUS reads in hot-paths.
87 #define BCM_SYSPORT_INTR_L2(which) \
88 static inline void intrl2_##which##_mask_clear(struct bcm_sysport_priv *priv, \
91 priv->irq##which##_mask &= ~(mask); \
92 intrl2_##which##_writel(priv, mask, INTRL2_CPU_MASK_CLEAR); \
94 static inline void intrl2_##which##_mask_set(struct bcm_sysport_priv *priv, \
97 intrl2_## which##_writel(priv, mask, INTRL2_CPU_MASK_SET); \
98 priv->irq##which##_mask |= (mask); \
101 BCM_SYSPORT_INTR_L2(0)
102 BCM_SYSPORT_INTR_L2(1)
104 /* Register accesses to GISB/RBUS registers are expensive (few hundred
105 * nanoseconds), so keep the check for 64-bits explicit here to save
106 * one register write per-packet on 32-bits platforms.
108 static inline void dma_desc_set_addr(struct bcm_sysport_priv *priv,
112 #ifdef CONFIG_PHYS_ADDR_T_64BIT
113 writel_relaxed(upper_32_bits(addr) & DESC_ADDR_HI_MASK,
114 d + DESC_ADDR_HI_STATUS_LEN);
116 writel_relaxed(lower_32_bits(addr), d + DESC_ADDR_LO);
119 static inline void tdma_port_write_desc_addr(struct bcm_sysport_priv *priv,
120 struct dma_desc *desc,
123 /* Ports are latched, so write upper address first */
124 tdma_writel(priv, desc->addr_status_len, TDMA_WRITE_PORT_HI(port));
125 tdma_writel(priv, desc->addr_lo, TDMA_WRITE_PORT_LO(port));
128 /* Ethtool operations */
129 static int bcm_sysport_set_rx_csum(struct net_device *dev,
130 netdev_features_t wanted)
132 struct bcm_sysport_priv *priv = netdev_priv(dev);
135 priv->rx_chk_en = !!(wanted & NETIF_F_RXCSUM);
136 reg = rxchk_readl(priv, RXCHK_CONTROL);
142 /* If UniMAC forwards CRC, we need to skip over it to get
143 * a valid CHK bit to be set in the per-packet status word
145 if (priv->rx_chk_en && priv->crc_fwd)
146 reg |= RXCHK_SKIP_FCS;
148 reg &= ~RXCHK_SKIP_FCS;
150 /* If Broadcom tags are enabled (e.g: using a switch), make
151 * sure we tell the RXCHK hardware to expect a 4-bytes Broadcom
152 * tag after the Ethernet MAC Source Address.
154 if (netdev_uses_dsa(dev))
155 reg |= RXCHK_BRCM_TAG_EN;
157 reg &= ~RXCHK_BRCM_TAG_EN;
159 rxchk_writel(priv, reg, RXCHK_CONTROL);
164 static int bcm_sysport_set_tx_csum(struct net_device *dev,
165 netdev_features_t wanted)
167 struct bcm_sysport_priv *priv = netdev_priv(dev);
170 /* Hardware transmit checksum requires us to enable the Transmit status
171 * block prepended to the packet contents
173 priv->tsb_en = !!(wanted & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
174 reg = tdma_readl(priv, TDMA_CONTROL);
176 reg |= tdma_control_bit(priv, TSB_EN);
178 reg &= ~tdma_control_bit(priv, TSB_EN);
179 tdma_writel(priv, reg, TDMA_CONTROL);
184 static int bcm_sysport_set_features(struct net_device *dev,
185 netdev_features_t features)
187 netdev_features_t changed = features ^ dev->features;
188 netdev_features_t wanted = dev->wanted_features;
191 if (changed & NETIF_F_RXCSUM)
192 ret = bcm_sysport_set_rx_csum(dev, wanted);
193 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
194 ret = bcm_sysport_set_tx_csum(dev, wanted);
199 /* Hardware counters must be kept in sync because the order/offset
200 * is important here (order in structure declaration = order in hardware)
202 static const struct bcm_sysport_stats bcm_sysport_gstrings_stats[] = {
204 STAT_NETDEV64(rx_packets),
205 STAT_NETDEV64(tx_packets),
206 STAT_NETDEV64(rx_bytes),
207 STAT_NETDEV64(tx_bytes),
208 STAT_NETDEV(rx_errors),
209 STAT_NETDEV(tx_errors),
210 STAT_NETDEV(rx_dropped),
211 STAT_NETDEV(tx_dropped),
212 STAT_NETDEV(multicast),
213 /* UniMAC RSV counters */
214 STAT_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
215 STAT_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
216 STAT_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
217 STAT_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
218 STAT_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
219 STAT_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
220 STAT_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
221 STAT_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
222 STAT_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
223 STAT_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
224 STAT_MIB_RX("rx_pkts", mib.rx.pkt),
225 STAT_MIB_RX("rx_bytes", mib.rx.bytes),
226 STAT_MIB_RX("rx_multicast", mib.rx.mca),
227 STAT_MIB_RX("rx_broadcast", mib.rx.bca),
228 STAT_MIB_RX("rx_fcs", mib.rx.fcs),
229 STAT_MIB_RX("rx_control", mib.rx.cf),
230 STAT_MIB_RX("rx_pause", mib.rx.pf),
231 STAT_MIB_RX("rx_unknown", mib.rx.uo),
232 STAT_MIB_RX("rx_align", mib.rx.aln),
233 STAT_MIB_RX("rx_outrange", mib.rx.flr),
234 STAT_MIB_RX("rx_code", mib.rx.cde),
235 STAT_MIB_RX("rx_carrier", mib.rx.fcr),
236 STAT_MIB_RX("rx_oversize", mib.rx.ovr),
237 STAT_MIB_RX("rx_jabber", mib.rx.jbr),
238 STAT_MIB_RX("rx_mtu_err", mib.rx.mtue),
239 STAT_MIB_RX("rx_good_pkts", mib.rx.pok),
240 STAT_MIB_RX("rx_unicast", mib.rx.uc),
241 STAT_MIB_RX("rx_ppp", mib.rx.ppp),
242 STAT_MIB_RX("rx_crc", mib.rx.rcrc),
243 /* UniMAC TSV counters */
244 STAT_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
245 STAT_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
246 STAT_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
247 STAT_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
248 STAT_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
249 STAT_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
250 STAT_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
251 STAT_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
252 STAT_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
253 STAT_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
254 STAT_MIB_TX("tx_pkts", mib.tx.pkts),
255 STAT_MIB_TX("tx_multicast", mib.tx.mca),
256 STAT_MIB_TX("tx_broadcast", mib.tx.bca),
257 STAT_MIB_TX("tx_pause", mib.tx.pf),
258 STAT_MIB_TX("tx_control", mib.tx.cf),
259 STAT_MIB_TX("tx_fcs_err", mib.tx.fcs),
260 STAT_MIB_TX("tx_oversize", mib.tx.ovr),
261 STAT_MIB_TX("tx_defer", mib.tx.drf),
262 STAT_MIB_TX("tx_excess_defer", mib.tx.edf),
263 STAT_MIB_TX("tx_single_col", mib.tx.scl),
264 STAT_MIB_TX("tx_multi_col", mib.tx.mcl),
265 STAT_MIB_TX("tx_late_col", mib.tx.lcl),
266 STAT_MIB_TX("tx_excess_col", mib.tx.ecl),
267 STAT_MIB_TX("tx_frags", mib.tx.frg),
268 STAT_MIB_TX("tx_total_col", mib.tx.ncl),
269 STAT_MIB_TX("tx_jabber", mib.tx.jbr),
270 STAT_MIB_TX("tx_bytes", mib.tx.bytes),
271 STAT_MIB_TX("tx_good_pkts", mib.tx.pok),
272 STAT_MIB_TX("tx_unicast", mib.tx.uc),
273 /* UniMAC RUNT counters */
274 STAT_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
275 STAT_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
276 STAT_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
277 STAT_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
278 /* RXCHK misc statistics */
279 STAT_RXCHK("rxchk_bad_csum", mib.rxchk_bad_csum, RXCHK_BAD_CSUM_CNTR),
280 STAT_RXCHK("rxchk_other_pkt_disc", mib.rxchk_other_pkt_disc,
281 RXCHK_OTHER_DISC_CNTR),
282 /* RBUF misc statistics */
283 STAT_RBUF("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt, RBUF_OVFL_DISC_CNTR),
284 STAT_RBUF("rbuf_err_cnt", mib.rbuf_err_cnt, RBUF_ERR_PKT_CNTR),
285 STAT_MIB_SOFT("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
286 STAT_MIB_SOFT("rx_dma_failed", mib.rx_dma_failed),
287 STAT_MIB_SOFT("tx_dma_failed", mib.tx_dma_failed),
288 /* Per TX-queue statistics are dynamically appended */
291 #define BCM_SYSPORT_STATS_LEN ARRAY_SIZE(bcm_sysport_gstrings_stats)
293 static void bcm_sysport_get_drvinfo(struct net_device *dev,
294 struct ethtool_drvinfo *info)
296 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
297 strlcpy(info->version, "0.1", sizeof(info->version));
298 strlcpy(info->bus_info, "platform", sizeof(info->bus_info));
301 static u32 bcm_sysport_get_msglvl(struct net_device *dev)
303 struct bcm_sysport_priv *priv = netdev_priv(dev);
305 return priv->msg_enable;
308 static void bcm_sysport_set_msglvl(struct net_device *dev, u32 enable)
310 struct bcm_sysport_priv *priv = netdev_priv(dev);
312 priv->msg_enable = enable;
315 static inline bool bcm_sysport_lite_stat_valid(enum bcm_sysport_stat_type type)
318 case BCM_SYSPORT_STAT_NETDEV:
319 case BCM_SYSPORT_STAT_NETDEV64:
320 case BCM_SYSPORT_STAT_RXCHK:
321 case BCM_SYSPORT_STAT_RBUF:
322 case BCM_SYSPORT_STAT_SOFT:
329 static int bcm_sysport_get_sset_count(struct net_device *dev, int string_set)
331 struct bcm_sysport_priv *priv = netdev_priv(dev);
332 const struct bcm_sysport_stats *s;
335 switch (string_set) {
337 for (i = 0, j = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
338 s = &bcm_sysport_gstrings_stats[i];
340 !bcm_sysport_lite_stat_valid(s->type))
344 /* Include per-queue statistics */
345 return j + dev->num_tx_queues * NUM_SYSPORT_TXQ_STAT;
351 static void bcm_sysport_get_strings(struct net_device *dev,
352 u32 stringset, u8 *data)
354 struct bcm_sysport_priv *priv = netdev_priv(dev);
355 const struct bcm_sysport_stats *s;
361 for (i = 0, j = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
362 s = &bcm_sysport_gstrings_stats[i];
364 !bcm_sysport_lite_stat_valid(s->type))
367 memcpy(data + j * ETH_GSTRING_LEN, s->stat_string,
372 for (i = 0; i < dev->num_tx_queues; i++) {
373 snprintf(buf, sizeof(buf), "txq%d_packets", i);
374 memcpy(data + j * ETH_GSTRING_LEN, buf,
378 snprintf(buf, sizeof(buf), "txq%d_bytes", i);
379 memcpy(data + j * ETH_GSTRING_LEN, buf,
389 static void bcm_sysport_update_mib_counters(struct bcm_sysport_priv *priv)
393 for (i = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
394 const struct bcm_sysport_stats *s;
399 s = &bcm_sysport_gstrings_stats[i];
401 case BCM_SYSPORT_STAT_NETDEV:
402 case BCM_SYSPORT_STAT_NETDEV64:
403 case BCM_SYSPORT_STAT_SOFT:
405 case BCM_SYSPORT_STAT_MIB_RX:
406 case BCM_SYSPORT_STAT_MIB_TX:
407 case BCM_SYSPORT_STAT_RUNT:
411 if (s->type != BCM_SYSPORT_STAT_MIB_RX)
412 offset = UMAC_MIB_STAT_OFFSET;
413 val = umac_readl(priv, UMAC_MIB_START + j + offset);
415 case BCM_SYSPORT_STAT_RXCHK:
416 val = rxchk_readl(priv, s->reg_offset);
418 rxchk_writel(priv, 0, s->reg_offset);
420 case BCM_SYSPORT_STAT_RBUF:
421 val = rbuf_readl(priv, s->reg_offset);
423 rbuf_writel(priv, 0, s->reg_offset);
428 p = (char *)priv + s->stat_offset;
432 netif_dbg(priv, hw, priv->netdev, "updated MIB counters\n");
435 static void bcm_sysport_update_tx_stats(struct bcm_sysport_priv *priv,
436 u64 *tx_bytes, u64 *tx_packets)
438 struct bcm_sysport_tx_ring *ring;
439 u64 bytes = 0, packets = 0;
443 for (q = 0; q < priv->netdev->num_tx_queues; q++) {
444 ring = &priv->tx_rings[q];
446 start = u64_stats_fetch_begin_irq(&priv->syncp);
448 packets = ring->packets;
449 } while (u64_stats_fetch_retry_irq(&priv->syncp, start));
452 *tx_packets += packets;
456 static void bcm_sysport_get_stats(struct net_device *dev,
457 struct ethtool_stats *stats, u64 *data)
459 struct bcm_sysport_priv *priv = netdev_priv(dev);
460 struct bcm_sysport_stats64 *stats64 = &priv->stats64;
461 struct u64_stats_sync *syncp = &priv->syncp;
462 struct bcm_sysport_tx_ring *ring;
463 u64 tx_bytes = 0, tx_packets = 0;
467 if (netif_running(dev)) {
468 bcm_sysport_update_mib_counters(priv);
469 bcm_sysport_update_tx_stats(priv, &tx_bytes, &tx_packets);
470 stats64->tx_bytes = tx_bytes;
471 stats64->tx_packets = tx_packets;
474 for (i = 0, j = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
475 const struct bcm_sysport_stats *s;
478 s = &bcm_sysport_gstrings_stats[i];
479 if (s->type == BCM_SYSPORT_STAT_NETDEV)
480 p = (char *)&dev->stats;
481 else if (s->type == BCM_SYSPORT_STAT_NETDEV64)
486 if (priv->is_lite && !bcm_sysport_lite_stat_valid(s->type))
490 if (s->stat_sizeof == sizeof(u64) &&
491 s->type == BCM_SYSPORT_STAT_NETDEV64) {
493 start = u64_stats_fetch_begin_irq(syncp);
495 } while (u64_stats_fetch_retry_irq(syncp, start));
501 /* For SYSTEMPORT Lite since we have holes in our statistics, j would
502 * be equal to BCM_SYSPORT_STATS_LEN at the end of the loop, but it
503 * needs to point to how many total statistics we have minus the
504 * number of per TX queue statistics
506 j = bcm_sysport_get_sset_count(dev, ETH_SS_STATS) -
507 dev->num_tx_queues * NUM_SYSPORT_TXQ_STAT;
509 for (i = 0; i < dev->num_tx_queues; i++) {
510 ring = &priv->tx_rings[i];
511 data[j] = ring->packets;
513 data[j] = ring->bytes;
518 static void bcm_sysport_get_wol(struct net_device *dev,
519 struct ethtool_wolinfo *wol)
521 struct bcm_sysport_priv *priv = netdev_priv(dev);
524 wol->supported = WAKE_MAGIC | WAKE_MAGICSECURE;
525 wol->wolopts = priv->wolopts;
527 if (!(priv->wolopts & WAKE_MAGICSECURE))
530 /* Return the programmed SecureOn password */
531 reg = umac_readl(priv, UMAC_PSW_MS);
532 put_unaligned_be16(reg, &wol->sopass[0]);
533 reg = umac_readl(priv, UMAC_PSW_LS);
534 put_unaligned_be32(reg, &wol->sopass[2]);
537 static int bcm_sysport_set_wol(struct net_device *dev,
538 struct ethtool_wolinfo *wol)
540 struct bcm_sysport_priv *priv = netdev_priv(dev);
541 struct device *kdev = &priv->pdev->dev;
542 u32 supported = WAKE_MAGIC | WAKE_MAGICSECURE;
544 if (!device_can_wakeup(kdev))
547 if (wol->wolopts & ~supported)
550 /* Program the SecureOn password */
551 if (wol->wolopts & WAKE_MAGICSECURE) {
552 umac_writel(priv, get_unaligned_be16(&wol->sopass[0]),
554 umac_writel(priv, get_unaligned_be32(&wol->sopass[2]),
558 /* Flag the device and relevant IRQ as wakeup capable */
560 device_set_wakeup_enable(kdev, 1);
561 if (priv->wol_irq_disabled)
562 enable_irq_wake(priv->wol_irq);
563 priv->wol_irq_disabled = 0;
565 device_set_wakeup_enable(kdev, 0);
566 /* Avoid unbalanced disable_irq_wake calls */
567 if (!priv->wol_irq_disabled)
568 disable_irq_wake(priv->wol_irq);
569 priv->wol_irq_disabled = 1;
572 priv->wolopts = wol->wolopts;
577 static void bcm_sysport_set_rx_coalesce(struct bcm_sysport_priv *priv,
582 reg = rdma_readl(priv, RDMA_MBDONE_INTR);
583 reg &= ~(RDMA_INTR_THRESH_MASK |
584 RDMA_TIMEOUT_MASK << RDMA_TIMEOUT_SHIFT);
586 reg |= DIV_ROUND_UP(usecs * 1000, 8192) << RDMA_TIMEOUT_SHIFT;
587 rdma_writel(priv, reg, RDMA_MBDONE_INTR);
590 static void bcm_sysport_set_tx_coalesce(struct bcm_sysport_tx_ring *ring,
591 struct ethtool_coalesce *ec)
593 struct bcm_sysport_priv *priv = ring->priv;
596 reg = tdma_readl(priv, TDMA_DESC_RING_INTR_CONTROL(ring->index));
597 reg &= ~(RING_INTR_THRESH_MASK |
598 RING_TIMEOUT_MASK << RING_TIMEOUT_SHIFT);
599 reg |= ec->tx_max_coalesced_frames;
600 reg |= DIV_ROUND_UP(ec->tx_coalesce_usecs * 1000, 8192) <<
602 tdma_writel(priv, reg, TDMA_DESC_RING_INTR_CONTROL(ring->index));
605 static int bcm_sysport_get_coalesce(struct net_device *dev,
606 struct ethtool_coalesce *ec)
608 struct bcm_sysport_priv *priv = netdev_priv(dev);
611 reg = tdma_readl(priv, TDMA_DESC_RING_INTR_CONTROL(0));
613 ec->tx_coalesce_usecs = (reg >> RING_TIMEOUT_SHIFT) * 8192 / 1000;
614 ec->tx_max_coalesced_frames = reg & RING_INTR_THRESH_MASK;
616 reg = rdma_readl(priv, RDMA_MBDONE_INTR);
618 ec->rx_coalesce_usecs = (reg >> RDMA_TIMEOUT_SHIFT) * 8192 / 1000;
619 ec->rx_max_coalesced_frames = reg & RDMA_INTR_THRESH_MASK;
620 ec->use_adaptive_rx_coalesce = priv->dim.use_dim;
625 static int bcm_sysport_set_coalesce(struct net_device *dev,
626 struct ethtool_coalesce *ec)
628 struct bcm_sysport_priv *priv = netdev_priv(dev);
629 struct net_dim_cq_moder moder;
633 /* Base system clock is 125Mhz, DMA timeout is this reference clock
634 * divided by 1024, which yield roughly 8.192 us, our maximum value has
635 * to fit in the RING_TIMEOUT_MASK (16 bits).
637 if (ec->tx_max_coalesced_frames > RING_INTR_THRESH_MASK ||
638 ec->tx_coalesce_usecs > (RING_TIMEOUT_MASK * 8) + 1 ||
639 ec->rx_max_coalesced_frames > RDMA_INTR_THRESH_MASK ||
640 ec->rx_coalesce_usecs > (RDMA_TIMEOUT_MASK * 8) + 1)
643 if ((ec->tx_coalesce_usecs == 0 && ec->tx_max_coalesced_frames == 0) ||
644 (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0) ||
645 ec->use_adaptive_tx_coalesce)
648 for (i = 0; i < dev->num_tx_queues; i++)
649 bcm_sysport_set_tx_coalesce(&priv->tx_rings[i], ec);
651 priv->rx_coalesce_usecs = ec->rx_coalesce_usecs;
652 priv->rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
653 usecs = priv->rx_coalesce_usecs;
654 pkts = priv->rx_max_coalesced_frames;
656 if (ec->use_adaptive_rx_coalesce && !priv->dim.use_dim) {
657 moder = net_dim_get_def_profile(priv->dim.dim.mode);
662 priv->dim.use_dim = ec->use_adaptive_rx_coalesce;
664 /* Apply desired coalescing parameters */
665 bcm_sysport_set_rx_coalesce(priv, usecs, pkts);
670 static void bcm_sysport_free_cb(struct bcm_sysport_cb *cb)
672 dev_consume_skb_any(cb->skb);
674 dma_unmap_addr_set(cb, dma_addr, 0);
677 static struct sk_buff *bcm_sysport_rx_refill(struct bcm_sysport_priv *priv,
678 struct bcm_sysport_cb *cb)
680 struct device *kdev = &priv->pdev->dev;
681 struct net_device *ndev = priv->netdev;
682 struct sk_buff *skb, *rx_skb;
685 /* Allocate a new SKB for a new packet */
686 skb = netdev_alloc_skb(priv->netdev, RX_BUF_LENGTH);
688 priv->mib.alloc_rx_buff_failed++;
689 netif_err(priv, rx_err, ndev, "SKB alloc failed\n");
693 mapping = dma_map_single(kdev, skb->data,
694 RX_BUF_LENGTH, DMA_FROM_DEVICE);
695 if (dma_mapping_error(kdev, mapping)) {
696 priv->mib.rx_dma_failed++;
697 dev_kfree_skb_any(skb);
698 netif_err(priv, rx_err, ndev, "DMA mapping failure\n");
702 /* Grab the current SKB on the ring */
705 dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
706 RX_BUF_LENGTH, DMA_FROM_DEVICE);
708 /* Put the new SKB on the ring */
710 dma_unmap_addr_set(cb, dma_addr, mapping);
711 dma_desc_set_addr(priv, cb->bd_addr, mapping);
713 netif_dbg(priv, rx_status, ndev, "RX refill\n");
715 /* Return the current SKB to the caller */
719 static int bcm_sysport_alloc_rx_bufs(struct bcm_sysport_priv *priv)
721 struct bcm_sysport_cb *cb;
725 for (i = 0; i < priv->num_rx_bds; i++) {
726 cb = &priv->rx_cbs[i];
727 skb = bcm_sysport_rx_refill(priv, cb);
737 /* Poll the hardware for up to budget packets to process */
738 static unsigned int bcm_sysport_desc_rx(struct bcm_sysport_priv *priv,
741 struct bcm_sysport_stats64 *stats64 = &priv->stats64;
742 struct net_device *ndev = priv->netdev;
743 unsigned int processed = 0, to_process;
744 unsigned int processed_bytes = 0;
745 struct bcm_sysport_cb *cb;
747 unsigned int p_index;
751 /* Clear status before servicing to reduce spurious interrupts */
752 intrl2_0_writel(priv, INTRL2_0_RDMA_MBDONE, INTRL2_CPU_CLEAR);
754 /* Determine how much we should process since last call, SYSTEMPORT Lite
755 * groups the producer and consumer indexes into the same 32-bit
756 * which we access using RDMA_CONS_INDEX
759 p_index = rdma_readl(priv, RDMA_PROD_INDEX);
761 p_index = rdma_readl(priv, RDMA_CONS_INDEX);
762 p_index &= RDMA_PROD_INDEX_MASK;
764 to_process = (p_index - priv->rx_c_index) & RDMA_CONS_INDEX_MASK;
766 netif_dbg(priv, rx_status, ndev,
767 "p_index=%d rx_c_index=%d to_process=%d\n",
768 p_index, priv->rx_c_index, to_process);
770 while ((processed < to_process) && (processed < budget)) {
771 cb = &priv->rx_cbs[priv->rx_read_ptr];
772 skb = bcm_sysport_rx_refill(priv, cb);
775 /* We do not have a backing SKB, so we do not a corresponding
776 * DMA mapping for this incoming packet since
777 * bcm_sysport_rx_refill always either has both skb and mapping
780 if (unlikely(!skb)) {
781 netif_err(priv, rx_err, ndev, "out of memory!\n");
782 ndev->stats.rx_dropped++;
783 ndev->stats.rx_errors++;
787 /* Extract the Receive Status Block prepended */
788 rsb = (struct bcm_rsb *)skb->data;
789 len = (rsb->rx_status_len >> DESC_LEN_SHIFT) & DESC_LEN_MASK;
790 status = (rsb->rx_status_len >> DESC_STATUS_SHIFT) &
793 netif_dbg(priv, rx_status, ndev,
794 "p=%d, c=%d, rd_ptr=%d, len=%d, flag=0x%04x\n",
795 p_index, priv->rx_c_index, priv->rx_read_ptr,
798 if (unlikely(len > RX_BUF_LENGTH)) {
799 netif_err(priv, rx_status, ndev, "oversized packet\n");
800 ndev->stats.rx_length_errors++;
801 ndev->stats.rx_errors++;
802 dev_kfree_skb_any(skb);
806 if (unlikely(!(status & DESC_EOP) || !(status & DESC_SOP))) {
807 netif_err(priv, rx_status, ndev, "fragmented packet!\n");
808 ndev->stats.rx_dropped++;
809 ndev->stats.rx_errors++;
810 dev_kfree_skb_any(skb);
814 if (unlikely(status & (RX_STATUS_ERR | RX_STATUS_OVFLOW))) {
815 netif_err(priv, rx_err, ndev, "error packet\n");
816 if (status & RX_STATUS_OVFLOW)
817 ndev->stats.rx_over_errors++;
818 ndev->stats.rx_dropped++;
819 ndev->stats.rx_errors++;
820 dev_kfree_skb_any(skb);
826 /* Hardware validated our checksum */
827 if (likely(status & DESC_L4_CSUM))
828 skb->ip_summed = CHECKSUM_UNNECESSARY;
830 /* Hardware pre-pends packets with 2bytes before Ethernet
831 * header plus we have the Receive Status Block, strip off all
832 * of this from the SKB.
834 skb_pull(skb, sizeof(*rsb) + 2);
835 len -= (sizeof(*rsb) + 2);
836 processed_bytes += len;
838 /* UniMAC may forward CRC */
840 skb_trim(skb, len - ETH_FCS_LEN);
844 skb->protocol = eth_type_trans(skb, ndev);
845 ndev->stats.rx_packets++;
846 ndev->stats.rx_bytes += len;
847 u64_stats_update_begin(&priv->syncp);
848 stats64->rx_packets++;
849 stats64->rx_bytes += len;
850 u64_stats_update_end(&priv->syncp);
852 napi_gro_receive(&priv->napi, skb);
857 if (priv->rx_read_ptr == priv->num_rx_bds)
858 priv->rx_read_ptr = 0;
861 priv->dim.packets = processed;
862 priv->dim.bytes = processed_bytes;
867 static void bcm_sysport_tx_reclaim_one(struct bcm_sysport_tx_ring *ring,
868 struct bcm_sysport_cb *cb,
869 unsigned int *bytes_compl,
870 unsigned int *pkts_compl)
872 struct bcm_sysport_priv *priv = ring->priv;
873 struct device *kdev = &priv->pdev->dev;
876 *bytes_compl += cb->skb->len;
877 dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
878 dma_unmap_len(cb, dma_len),
881 bcm_sysport_free_cb(cb);
883 } else if (dma_unmap_addr(cb, dma_addr)) {
884 *bytes_compl += dma_unmap_len(cb, dma_len);
885 dma_unmap_page(kdev, dma_unmap_addr(cb, dma_addr),
886 dma_unmap_len(cb, dma_len), DMA_TO_DEVICE);
887 dma_unmap_addr_set(cb, dma_addr, 0);
891 /* Reclaim queued SKBs for transmission completion, lockless version */
892 static unsigned int __bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
893 struct bcm_sysport_tx_ring *ring)
895 unsigned int pkts_compl = 0, bytes_compl = 0;
896 struct net_device *ndev = priv->netdev;
897 unsigned int txbds_processed = 0;
898 struct bcm_sysport_cb *cb;
899 unsigned int txbds_ready;
900 unsigned int c_index;
903 /* Clear status before servicing to reduce spurious interrupts */
904 if (!ring->priv->is_lite)
905 intrl2_1_writel(ring->priv, BIT(ring->index), INTRL2_CPU_CLEAR);
907 intrl2_0_writel(ring->priv, BIT(ring->index +
908 INTRL2_0_TDMA_MBDONE_SHIFT), INTRL2_CPU_CLEAR);
910 /* Compute how many descriptors have been processed since last call */
911 hw_ind = tdma_readl(priv, TDMA_DESC_RING_PROD_CONS_INDEX(ring->index));
912 c_index = (hw_ind >> RING_CONS_INDEX_SHIFT) & RING_CONS_INDEX_MASK;
913 txbds_ready = (c_index - ring->c_index) & RING_CONS_INDEX_MASK;
915 netif_dbg(priv, tx_done, ndev,
916 "ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
917 ring->index, ring->c_index, c_index, txbds_ready);
919 while (txbds_processed < txbds_ready) {
920 cb = &ring->cbs[ring->clean_index];
921 bcm_sysport_tx_reclaim_one(ring, cb, &bytes_compl, &pkts_compl);
926 if (likely(ring->clean_index < ring->size - 1))
929 ring->clean_index = 0;
932 u64_stats_update_begin(&priv->syncp);
933 ring->packets += pkts_compl;
934 ring->bytes += bytes_compl;
935 u64_stats_update_end(&priv->syncp);
937 ring->c_index = c_index;
939 netif_dbg(priv, tx_done, ndev,
940 "ring=%d c_index=%d pkts_compl=%d, bytes_compl=%d\n",
941 ring->index, ring->c_index, pkts_compl, bytes_compl);
946 /* Locked version of the per-ring TX reclaim routine */
947 static unsigned int bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
948 struct bcm_sysport_tx_ring *ring)
950 struct netdev_queue *txq;
951 unsigned int released;
954 txq = netdev_get_tx_queue(priv->netdev, ring->index);
956 spin_lock_irqsave(&ring->lock, flags);
957 released = __bcm_sysport_tx_reclaim(priv, ring);
959 netif_tx_wake_queue(txq);
961 spin_unlock_irqrestore(&ring->lock, flags);
966 /* Locked version of the per-ring TX reclaim, but does not wake the queue */
967 static void bcm_sysport_tx_clean(struct bcm_sysport_priv *priv,
968 struct bcm_sysport_tx_ring *ring)
972 spin_lock_irqsave(&ring->lock, flags);
973 __bcm_sysport_tx_reclaim(priv, ring);
974 spin_unlock_irqrestore(&ring->lock, flags);
977 static int bcm_sysport_tx_poll(struct napi_struct *napi, int budget)
979 struct bcm_sysport_tx_ring *ring =
980 container_of(napi, struct bcm_sysport_tx_ring, napi);
981 unsigned int work_done = 0;
983 work_done = bcm_sysport_tx_reclaim(ring->priv, ring);
985 if (work_done == 0) {
987 /* re-enable TX interrupt */
988 if (!ring->priv->is_lite)
989 intrl2_1_mask_clear(ring->priv, BIT(ring->index));
991 intrl2_0_mask_clear(ring->priv, BIT(ring->index +
992 INTRL2_0_TDMA_MBDONE_SHIFT));
1000 static void bcm_sysport_tx_reclaim_all(struct bcm_sysport_priv *priv)
1004 for (q = 0; q < priv->netdev->num_tx_queues; q++)
1005 bcm_sysport_tx_reclaim(priv, &priv->tx_rings[q]);
1008 static int bcm_sysport_poll(struct napi_struct *napi, int budget)
1010 struct bcm_sysport_priv *priv =
1011 container_of(napi, struct bcm_sysport_priv, napi);
1012 struct net_dim_sample dim_sample;
1013 unsigned int work_done = 0;
1015 work_done = bcm_sysport_desc_rx(priv, budget);
1017 priv->rx_c_index += work_done;
1018 priv->rx_c_index &= RDMA_CONS_INDEX_MASK;
1020 /* SYSTEMPORT Lite groups the producer/consumer index, producer is
1021 * maintained by HW, but writes to it will be ignore while RDMA
1025 rdma_writel(priv, priv->rx_c_index, RDMA_CONS_INDEX);
1027 rdma_writel(priv, priv->rx_c_index << 16, RDMA_CONS_INDEX);
1029 if (work_done < budget) {
1030 napi_complete_done(napi, work_done);
1031 /* re-enable RX interrupts */
1032 intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE);
1035 if (priv->dim.use_dim) {
1036 net_dim_sample(priv->dim.event_ctr, priv->dim.packets,
1037 priv->dim.bytes, &dim_sample);
1038 net_dim(&priv->dim.dim, dim_sample);
1044 static void bcm_sysport_resume_from_wol(struct bcm_sysport_priv *priv)
1048 /* Stop monitoring MPD interrupt */
1049 intrl2_0_mask_set(priv, INTRL2_0_MPD);
1051 /* Clear the MagicPacket detection logic */
1052 reg = umac_readl(priv, UMAC_MPD_CTRL);
1054 umac_writel(priv, reg, UMAC_MPD_CTRL);
1056 netif_dbg(priv, wol, priv->netdev, "resumed from WOL\n");
1059 static void bcm_sysport_dim_work(struct work_struct *work)
1061 struct net_dim *dim = container_of(work, struct net_dim, work);
1062 struct bcm_sysport_net_dim *ndim =
1063 container_of(dim, struct bcm_sysport_net_dim, dim);
1064 struct bcm_sysport_priv *priv =
1065 container_of(ndim, struct bcm_sysport_priv, dim);
1066 struct net_dim_cq_moder cur_profile =
1067 net_dim_get_profile(dim->mode, dim->profile_ix);
1069 bcm_sysport_set_rx_coalesce(priv, cur_profile.usec, cur_profile.pkts);
1070 dim->state = NET_DIM_START_MEASURE;
1073 /* RX and misc interrupt routine */
1074 static irqreturn_t bcm_sysport_rx_isr(int irq, void *dev_id)
1076 struct net_device *dev = dev_id;
1077 struct bcm_sysport_priv *priv = netdev_priv(dev);
1078 struct bcm_sysport_tx_ring *txr;
1079 unsigned int ring, ring_bit;
1081 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
1082 ~intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
1083 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
1085 if (unlikely(priv->irq0_stat == 0)) {
1086 netdev_warn(priv->netdev, "spurious RX interrupt\n");
1090 if (priv->irq0_stat & INTRL2_0_RDMA_MBDONE) {
1091 priv->dim.event_ctr++;
1092 if (likely(napi_schedule_prep(&priv->napi))) {
1093 /* disable RX interrupts */
1094 intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE);
1095 __napi_schedule_irqoff(&priv->napi);
1099 /* TX ring is full, perform a full reclaim since we do not know
1100 * which one would trigger this interrupt
1102 if (priv->irq0_stat & INTRL2_0_TX_RING_FULL)
1103 bcm_sysport_tx_reclaim_all(priv);
1105 if (priv->irq0_stat & INTRL2_0_MPD) {
1106 netdev_info(priv->netdev, "Wake-on-LAN interrupt!\n");
1107 bcm_sysport_resume_from_wol(priv);
1113 for (ring = 0; ring < dev->num_tx_queues; ring++) {
1114 ring_bit = BIT(ring + INTRL2_0_TDMA_MBDONE_SHIFT);
1115 if (!(priv->irq0_stat & ring_bit))
1118 txr = &priv->tx_rings[ring];
1120 if (likely(napi_schedule_prep(&txr->napi))) {
1121 intrl2_0_mask_set(priv, ring_bit);
1122 __napi_schedule(&txr->napi);
1129 /* TX interrupt service routine */
1130 static irqreturn_t bcm_sysport_tx_isr(int irq, void *dev_id)
1132 struct net_device *dev = dev_id;
1133 struct bcm_sysport_priv *priv = netdev_priv(dev);
1134 struct bcm_sysport_tx_ring *txr;
1137 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
1138 ~intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
1139 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1141 if (unlikely(priv->irq1_stat == 0)) {
1142 netdev_warn(priv->netdev, "spurious TX interrupt\n");
1146 for (ring = 0; ring < dev->num_tx_queues; ring++) {
1147 if (!(priv->irq1_stat & BIT(ring)))
1150 txr = &priv->tx_rings[ring];
1152 if (likely(napi_schedule_prep(&txr->napi))) {
1153 intrl2_1_mask_set(priv, BIT(ring));
1154 __napi_schedule_irqoff(&txr->napi);
1161 static irqreturn_t bcm_sysport_wol_isr(int irq, void *dev_id)
1163 struct bcm_sysport_priv *priv = dev_id;
1165 pm_wakeup_event(&priv->pdev->dev, 0);
1170 #ifdef CONFIG_NET_POLL_CONTROLLER
1171 static void bcm_sysport_poll_controller(struct net_device *dev)
1173 struct bcm_sysport_priv *priv = netdev_priv(dev);
1175 disable_irq(priv->irq0);
1176 bcm_sysport_rx_isr(priv->irq0, priv);
1177 enable_irq(priv->irq0);
1179 if (!priv->is_lite) {
1180 disable_irq(priv->irq1);
1181 bcm_sysport_tx_isr(priv->irq1, priv);
1182 enable_irq(priv->irq1);
1187 static struct sk_buff *bcm_sysport_insert_tsb(struct sk_buff *skb,
1188 struct net_device *dev)
1190 struct sk_buff *nskb;
1191 struct bcm_tsb *tsb;
1197 /* Re-allocate SKB if needed */
1198 if (unlikely(skb_headroom(skb) < sizeof(*tsb))) {
1199 nskb = skb_realloc_headroom(skb, sizeof(*tsb));
1202 dev->stats.tx_errors++;
1203 dev->stats.tx_dropped++;
1209 tsb = skb_push(skb, sizeof(*tsb));
1210 /* Zero-out TSB by default */
1211 memset(tsb, 0, sizeof(*tsb));
1213 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1214 ip_ver = htons(skb->protocol);
1217 ip_proto = ip_hdr(skb)->protocol;
1220 ip_proto = ipv6_hdr(skb)->nexthdr;
1226 /* Get the checksum offset and the L4 (transport) offset */
1227 csum_start = skb_checksum_start_offset(skb) - sizeof(*tsb);
1228 csum_info = (csum_start + skb->csum_offset) & L4_CSUM_PTR_MASK;
1229 csum_info |= (csum_start << L4_PTR_SHIFT);
1231 if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
1232 csum_info |= L4_LENGTH_VALID;
1233 if (ip_proto == IPPROTO_UDP && ip_ver == ETH_P_IP)
1234 csum_info |= L4_UDP;
1239 tsb->l4_ptr_dest_map = csum_info;
1245 static netdev_tx_t bcm_sysport_xmit(struct sk_buff *skb,
1246 struct net_device *dev)
1248 struct bcm_sysport_priv *priv = netdev_priv(dev);
1249 struct device *kdev = &priv->pdev->dev;
1250 struct bcm_sysport_tx_ring *ring;
1251 struct bcm_sysport_cb *cb;
1252 struct netdev_queue *txq;
1253 struct dma_desc *desc;
1254 unsigned int skb_len;
1255 unsigned long flags;
1261 queue = skb_get_queue_mapping(skb);
1262 txq = netdev_get_tx_queue(dev, queue);
1263 ring = &priv->tx_rings[queue];
1265 /* lock against tx reclaim in BH context and TX ring full interrupt */
1266 spin_lock_irqsave(&ring->lock, flags);
1267 if (unlikely(ring->desc_count == 0)) {
1268 netif_tx_stop_queue(txq);
1269 netdev_err(dev, "queue %d awake and ring full!\n", queue);
1270 ret = NETDEV_TX_BUSY;
1274 /* Insert TSB and checksum infos */
1276 skb = bcm_sysport_insert_tsb(skb, dev);
1285 mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
1286 if (dma_mapping_error(kdev, mapping)) {
1287 priv->mib.tx_dma_failed++;
1288 netif_err(priv, tx_err, dev, "DMA map failed at %p (len=%d)\n",
1289 skb->data, skb_len);
1294 /* Remember the SKB for future freeing */
1295 cb = &ring->cbs[ring->curr_desc];
1297 dma_unmap_addr_set(cb, dma_addr, mapping);
1298 dma_unmap_len_set(cb, dma_len, skb_len);
1300 /* Fetch a descriptor entry from our pool */
1301 desc = ring->desc_cpu;
1303 desc->addr_lo = lower_32_bits(mapping);
1304 len_status = upper_32_bits(mapping) & DESC_ADDR_HI_MASK;
1305 len_status |= (skb_len << DESC_LEN_SHIFT);
1306 len_status |= (DESC_SOP | DESC_EOP | TX_STATUS_APP_CRC) <<
1308 if (skb->ip_summed == CHECKSUM_PARTIAL)
1309 len_status |= (DESC_L4_CSUM << DESC_STATUS_SHIFT);
1312 if (ring->curr_desc == ring->size)
1313 ring->curr_desc = 0;
1316 /* Ensure write completion of the descriptor status/length
1317 * in DRAM before the System Port WRITE_PORT register latches
1321 desc->addr_status_len = len_status;
1324 /* Write this descriptor address to the RING write port */
1325 tdma_port_write_desc_addr(priv, desc, ring->index);
1327 /* Check ring space and update SW control flow */
1328 if (ring->desc_count == 0)
1329 netif_tx_stop_queue(txq);
1331 netif_dbg(priv, tx_queued, dev, "ring=%d desc_count=%d, curr_desc=%d\n",
1332 ring->index, ring->desc_count, ring->curr_desc);
1336 spin_unlock_irqrestore(&ring->lock, flags);
1340 static void bcm_sysport_tx_timeout(struct net_device *dev)
1342 netdev_warn(dev, "transmit timeout!\n");
1344 netif_trans_update(dev);
1345 dev->stats.tx_errors++;
1347 netif_tx_wake_all_queues(dev);
1350 /* phylib adjust link callback */
1351 static void bcm_sysport_adj_link(struct net_device *dev)
1353 struct bcm_sysport_priv *priv = netdev_priv(dev);
1354 struct phy_device *phydev = dev->phydev;
1355 unsigned int changed = 0;
1356 u32 cmd_bits = 0, reg;
1358 if (priv->old_link != phydev->link) {
1360 priv->old_link = phydev->link;
1363 if (priv->old_duplex != phydev->duplex) {
1365 priv->old_duplex = phydev->duplex;
1371 switch (phydev->speed) {
1373 cmd_bits = CMD_SPEED_2500;
1376 cmd_bits = CMD_SPEED_1000;
1379 cmd_bits = CMD_SPEED_100;
1382 cmd_bits = CMD_SPEED_10;
1387 cmd_bits <<= CMD_SPEED_SHIFT;
1389 if (phydev->duplex == DUPLEX_HALF)
1390 cmd_bits |= CMD_HD_EN;
1392 if (priv->old_pause != phydev->pause) {
1394 priv->old_pause = phydev->pause;
1398 cmd_bits |= CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE;
1404 reg = umac_readl(priv, UMAC_CMD);
1405 reg &= ~((CMD_SPEED_MASK << CMD_SPEED_SHIFT) |
1406 CMD_HD_EN | CMD_RX_PAUSE_IGNORE |
1407 CMD_TX_PAUSE_IGNORE);
1409 umac_writel(priv, reg, UMAC_CMD);
1413 phy_print_status(phydev);
1416 static void bcm_sysport_init_dim(struct bcm_sysport_priv *priv,
1417 void (*cb)(struct work_struct *work))
1419 struct bcm_sysport_net_dim *dim = &priv->dim;
1421 INIT_WORK(&dim->dim.work, cb);
1422 dim->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1428 static void bcm_sysport_init_rx_coalesce(struct bcm_sysport_priv *priv)
1430 struct bcm_sysport_net_dim *dim = &priv->dim;
1431 struct net_dim_cq_moder moder;
1434 usecs = priv->rx_coalesce_usecs;
1435 pkts = priv->rx_max_coalesced_frames;
1437 /* If DIM was enabled, re-apply default parameters */
1439 moder = net_dim_get_def_profile(dim->dim.mode);
1444 bcm_sysport_set_rx_coalesce(priv, usecs, pkts);
1447 static int bcm_sysport_init_tx_ring(struct bcm_sysport_priv *priv,
1450 struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
1451 struct device *kdev = &priv->pdev->dev;
1456 /* Simple descriptors partitioning for now */
1459 /* We just need one DMA descriptor which is DMA-able, since writing to
1460 * the port will allocate a new descriptor in its internal linked-list
1462 p = dma_zalloc_coherent(kdev, sizeof(struct dma_desc), &ring->desc_dma,
1465 netif_err(priv, hw, priv->netdev, "DMA alloc failed\n");
1469 ring->cbs = kcalloc(size, sizeof(struct bcm_sysport_cb), GFP_KERNEL);
1471 dma_free_coherent(kdev, sizeof(struct dma_desc),
1472 ring->desc_cpu, ring->desc_dma);
1473 netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
1477 /* Initialize SW view of the ring */
1478 spin_lock_init(&ring->lock);
1480 netif_tx_napi_add(priv->netdev, &ring->napi, bcm_sysport_tx_poll, 64);
1481 ring->index = index;
1483 ring->clean_index = 0;
1484 ring->alloc_size = ring->size;
1486 ring->desc_count = ring->size;
1487 ring->curr_desc = 0;
1489 /* Initialize HW ring */
1490 tdma_writel(priv, RING_EN, TDMA_DESC_RING_HEAD_TAIL_PTR(index));
1491 tdma_writel(priv, 0, TDMA_DESC_RING_COUNT(index));
1492 tdma_writel(priv, 1, TDMA_DESC_RING_INTR_CONTROL(index));
1493 tdma_writel(priv, 0, TDMA_DESC_RING_PROD_CONS_INDEX(index));
1495 /* Configure QID and port mapping */
1496 reg = tdma_readl(priv, TDMA_DESC_RING_MAPPING(index));
1497 reg &= ~(RING_QID_MASK | RING_PORT_ID_MASK << RING_PORT_ID_SHIFT);
1498 if (ring->inspect) {
1499 reg |= ring->switch_queue & RING_QID_MASK;
1500 reg |= ring->switch_port << RING_PORT_ID_SHIFT;
1502 reg |= RING_IGNORE_STATUS;
1504 tdma_writel(priv, reg, TDMA_DESC_RING_MAPPING(index));
1505 tdma_writel(priv, 0, TDMA_DESC_RING_PCP_DEI_VID(index));
1507 /* Enable ACB algorithm 2 */
1508 reg = tdma_readl(priv, TDMA_CONTROL);
1509 reg |= tdma_control_bit(priv, ACB_ALGO);
1510 tdma_writel(priv, reg, TDMA_CONTROL);
1512 /* Do not use tdma_control_bit() here because TSB_SWAP1 collides
1513 * with the original definition of ACB_ALGO
1515 reg = tdma_readl(priv, TDMA_CONTROL);
1517 reg &= ~BIT(TSB_SWAP1);
1518 /* Set a correct TSB format based on host endian */
1519 if (!IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
1520 reg |= tdma_control_bit(priv, TSB_SWAP0);
1522 reg &= ~tdma_control_bit(priv, TSB_SWAP0);
1523 tdma_writel(priv, reg, TDMA_CONTROL);
1525 /* Program the number of descriptors as MAX_THRESHOLD and half of
1526 * its size for the hysteresis trigger
1528 tdma_writel(priv, ring->size |
1529 1 << RING_HYST_THRESH_SHIFT,
1530 TDMA_DESC_RING_MAX_HYST(index));
1532 /* Enable the ring queue in the arbiter */
1533 reg = tdma_readl(priv, TDMA_TIER1_ARB_0_QUEUE_EN);
1534 reg |= (1 << index);
1535 tdma_writel(priv, reg, TDMA_TIER1_ARB_0_QUEUE_EN);
1537 napi_enable(&ring->napi);
1539 netif_dbg(priv, hw, priv->netdev,
1540 "TDMA cfg, size=%d, desc_cpu=%p switch q=%d,port=%d\n",
1541 ring->size, ring->desc_cpu, ring->switch_queue,
1547 static void bcm_sysport_fini_tx_ring(struct bcm_sysport_priv *priv,
1550 struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
1551 struct device *kdev = &priv->pdev->dev;
1554 /* Caller should stop the TDMA engine */
1555 reg = tdma_readl(priv, TDMA_STATUS);
1556 if (!(reg & TDMA_DISABLED))
1557 netdev_warn(priv->netdev, "TDMA not stopped!\n");
1559 /* ring->cbs is the last part in bcm_sysport_init_tx_ring which could
1560 * fail, so by checking this pointer we know whether the TX ring was
1561 * fully initialized or not.
1566 napi_disable(&ring->napi);
1567 netif_napi_del(&ring->napi);
1569 bcm_sysport_tx_clean(priv, ring);
1574 if (ring->desc_dma) {
1575 dma_free_coherent(kdev, sizeof(struct dma_desc),
1576 ring->desc_cpu, ring->desc_dma);
1580 ring->alloc_size = 0;
1582 netif_dbg(priv, hw, priv->netdev, "TDMA fini done\n");
1586 static inline int rdma_enable_set(struct bcm_sysport_priv *priv,
1587 unsigned int enable)
1589 unsigned int timeout = 1000;
1592 reg = rdma_readl(priv, RDMA_CONTROL);
1597 rdma_writel(priv, reg, RDMA_CONTROL);
1599 /* Poll for RMDA disabling completion */
1601 reg = rdma_readl(priv, RDMA_STATUS);
1602 if (!!(reg & RDMA_DISABLED) == !enable)
1604 usleep_range(1000, 2000);
1605 } while (timeout-- > 0);
1607 netdev_err(priv->netdev, "timeout waiting for RDMA to finish\n");
1613 static inline int tdma_enable_set(struct bcm_sysport_priv *priv,
1614 unsigned int enable)
1616 unsigned int timeout = 1000;
1619 reg = tdma_readl(priv, TDMA_CONTROL);
1621 reg |= tdma_control_bit(priv, TDMA_EN);
1623 reg &= ~tdma_control_bit(priv, TDMA_EN);
1624 tdma_writel(priv, reg, TDMA_CONTROL);
1626 /* Poll for TMDA disabling completion */
1628 reg = tdma_readl(priv, TDMA_STATUS);
1629 if (!!(reg & TDMA_DISABLED) == !enable)
1632 usleep_range(1000, 2000);
1633 } while (timeout-- > 0);
1635 netdev_err(priv->netdev, "timeout waiting for TDMA to finish\n");
1640 static int bcm_sysport_init_rx_ring(struct bcm_sysport_priv *priv)
1642 struct bcm_sysport_cb *cb;
1647 /* Initialize SW view of the RX ring */
1648 priv->num_rx_bds = priv->num_rx_desc_words / WORDS_PER_DESC;
1649 priv->rx_bds = priv->base + SYS_PORT_RDMA_OFFSET;
1650 priv->rx_c_index = 0;
1651 priv->rx_read_ptr = 0;
1652 priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct bcm_sysport_cb),
1654 if (!priv->rx_cbs) {
1655 netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
1659 for (i = 0; i < priv->num_rx_bds; i++) {
1660 cb = priv->rx_cbs + i;
1661 cb->bd_addr = priv->rx_bds + i * DESC_SIZE;
1664 ret = bcm_sysport_alloc_rx_bufs(priv);
1666 netif_err(priv, hw, priv->netdev, "SKB allocation failed\n");
1670 /* Initialize HW, ensure RDMA is disabled */
1671 reg = rdma_readl(priv, RDMA_STATUS);
1672 if (!(reg & RDMA_DISABLED))
1673 rdma_enable_set(priv, 0);
1675 rdma_writel(priv, 0, RDMA_WRITE_PTR_LO);
1676 rdma_writel(priv, 0, RDMA_WRITE_PTR_HI);
1677 rdma_writel(priv, 0, RDMA_PROD_INDEX);
1678 rdma_writel(priv, 0, RDMA_CONS_INDEX);
1679 rdma_writel(priv, priv->num_rx_bds << RDMA_RING_SIZE_SHIFT |
1680 RX_BUF_LENGTH, RDMA_RING_BUF_SIZE);
1681 /* Operate the queue in ring mode */
1682 rdma_writel(priv, 0, RDMA_START_ADDR_HI);
1683 rdma_writel(priv, 0, RDMA_START_ADDR_LO);
1684 rdma_writel(priv, 0, RDMA_END_ADDR_HI);
1685 rdma_writel(priv, priv->num_rx_desc_words - 1, RDMA_END_ADDR_LO);
1687 netif_dbg(priv, hw, priv->netdev,
1688 "RDMA cfg, num_rx_bds=%d, rx_bds=%p\n",
1689 priv->num_rx_bds, priv->rx_bds);
1694 static void bcm_sysport_fini_rx_ring(struct bcm_sysport_priv *priv)
1696 struct bcm_sysport_cb *cb;
1700 /* Caller should ensure RDMA is disabled */
1701 reg = rdma_readl(priv, RDMA_STATUS);
1702 if (!(reg & RDMA_DISABLED))
1703 netdev_warn(priv->netdev, "RDMA not stopped!\n");
1705 for (i = 0; i < priv->num_rx_bds; i++) {
1706 cb = &priv->rx_cbs[i];
1707 if (dma_unmap_addr(cb, dma_addr))
1708 dma_unmap_single(&priv->pdev->dev,
1709 dma_unmap_addr(cb, dma_addr),
1710 RX_BUF_LENGTH, DMA_FROM_DEVICE);
1711 bcm_sysport_free_cb(cb);
1714 kfree(priv->rx_cbs);
1715 priv->rx_cbs = NULL;
1717 netif_dbg(priv, hw, priv->netdev, "RDMA fini done\n");
1720 static void bcm_sysport_set_rx_mode(struct net_device *dev)
1722 struct bcm_sysport_priv *priv = netdev_priv(dev);
1728 reg = umac_readl(priv, UMAC_CMD);
1729 if (dev->flags & IFF_PROMISC)
1732 reg &= ~CMD_PROMISC;
1733 umac_writel(priv, reg, UMAC_CMD);
1735 /* No support for ALLMULTI */
1736 if (dev->flags & IFF_ALLMULTI)
1740 static inline void umac_enable_set(struct bcm_sysport_priv *priv,
1741 u32 mask, unsigned int enable)
1745 if (!priv->is_lite) {
1746 reg = umac_readl(priv, UMAC_CMD);
1751 umac_writel(priv, reg, UMAC_CMD);
1753 reg = gib_readl(priv, GIB_CONTROL);
1758 gib_writel(priv, reg, GIB_CONTROL);
1761 /* UniMAC stops on a packet boundary, wait for a full-sized packet
1762 * to be processed (1 msec).
1765 usleep_range(1000, 2000);
1768 static inline void umac_reset(struct bcm_sysport_priv *priv)
1775 reg = umac_readl(priv, UMAC_CMD);
1776 reg |= CMD_SW_RESET;
1777 umac_writel(priv, reg, UMAC_CMD);
1779 reg = umac_readl(priv, UMAC_CMD);
1780 reg &= ~CMD_SW_RESET;
1781 umac_writel(priv, reg, UMAC_CMD);
1784 static void umac_set_hw_addr(struct bcm_sysport_priv *priv,
1785 unsigned char *addr)
1787 u32 mac0 = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) |
1789 u32 mac1 = (addr[4] << 8) | addr[5];
1791 if (!priv->is_lite) {
1792 umac_writel(priv, mac0, UMAC_MAC0);
1793 umac_writel(priv, mac1, UMAC_MAC1);
1795 gib_writel(priv, mac0, GIB_MAC0);
1796 gib_writel(priv, mac1, GIB_MAC1);
1800 static void topctrl_flush(struct bcm_sysport_priv *priv)
1802 topctrl_writel(priv, RX_FLUSH, RX_FLUSH_CNTL);
1803 topctrl_writel(priv, TX_FLUSH, TX_FLUSH_CNTL);
1805 topctrl_writel(priv, 0, RX_FLUSH_CNTL);
1806 topctrl_writel(priv, 0, TX_FLUSH_CNTL);
1809 static int bcm_sysport_change_mac(struct net_device *dev, void *p)
1811 struct bcm_sysport_priv *priv = netdev_priv(dev);
1812 struct sockaddr *addr = p;
1814 if (!is_valid_ether_addr(addr->sa_data))
1817 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1819 /* interface is disabled, changes to MAC will be reflected on next
1822 if (!netif_running(dev))
1825 umac_set_hw_addr(priv, dev->dev_addr);
1830 static void bcm_sysport_get_stats64(struct net_device *dev,
1831 struct rtnl_link_stats64 *stats)
1833 struct bcm_sysport_priv *priv = netdev_priv(dev);
1834 struct bcm_sysport_stats64 *stats64 = &priv->stats64;
1837 netdev_stats_to_stats64(stats, &dev->stats);
1839 bcm_sysport_update_tx_stats(priv, &stats->tx_bytes,
1840 &stats->tx_packets);
1843 start = u64_stats_fetch_begin_irq(&priv->syncp);
1844 stats->rx_packets = stats64->rx_packets;
1845 stats->rx_bytes = stats64->rx_bytes;
1846 } while (u64_stats_fetch_retry_irq(&priv->syncp, start));
1849 static void bcm_sysport_netif_start(struct net_device *dev)
1851 struct bcm_sysport_priv *priv = netdev_priv(dev);
1854 bcm_sysport_init_dim(priv, bcm_sysport_dim_work);
1855 bcm_sysport_init_rx_coalesce(priv);
1856 napi_enable(&priv->napi);
1858 /* Enable RX interrupt and TX ring full interrupt */
1859 intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
1861 phy_start(dev->phydev);
1863 /* Enable TX interrupts for the TXQs */
1865 intrl2_1_mask_clear(priv, 0xffffffff);
1867 intrl2_0_mask_clear(priv, INTRL2_0_TDMA_MBDONE_MASK);
1869 /* Last call before we start the real business */
1870 netif_tx_start_all_queues(dev);
1873 static void rbuf_init(struct bcm_sysport_priv *priv)
1877 reg = rbuf_readl(priv, RBUF_CONTROL);
1878 reg |= RBUF_4B_ALGN | RBUF_RSB_EN;
1879 /* Set a correct RSB format on SYSTEMPORT Lite */
1881 reg &= ~RBUF_RSB_SWAP1;
1883 /* Set a correct RSB format based on host endian */
1884 if (!IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
1885 reg |= RBUF_RSB_SWAP0;
1887 reg &= ~RBUF_RSB_SWAP0;
1888 rbuf_writel(priv, reg, RBUF_CONTROL);
1891 static inline void bcm_sysport_mask_all_intrs(struct bcm_sysport_priv *priv)
1893 intrl2_0_mask_set(priv, 0xffffffff);
1894 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1895 if (!priv->is_lite) {
1896 intrl2_1_mask_set(priv, 0xffffffff);
1897 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1901 static inline void gib_set_pad_extension(struct bcm_sysport_priv *priv)
1905 reg = gib_readl(priv, GIB_CONTROL);
1906 /* Include Broadcom tag in pad extension and fix up IPG_LENGTH */
1907 if (netdev_uses_dsa(priv->netdev)) {
1908 reg &= ~(GIB_PAD_EXTENSION_MASK << GIB_PAD_EXTENSION_SHIFT);
1909 reg |= ENET_BRCM_TAG_LEN << GIB_PAD_EXTENSION_SHIFT;
1911 reg &= ~(GIB_IPG_LEN_MASK << GIB_IPG_LEN_SHIFT);
1912 reg |= 12 << GIB_IPG_LEN_SHIFT;
1913 gib_writel(priv, reg, GIB_CONTROL);
1916 static int bcm_sysport_open(struct net_device *dev)
1918 struct bcm_sysport_priv *priv = netdev_priv(dev);
1919 struct phy_device *phydev;
1926 /* Flush TX and RX FIFOs at TOPCTRL level */
1927 topctrl_flush(priv);
1929 /* Disable the UniMAC RX/TX */
1930 umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 0);
1932 /* Enable RBUF 2bytes alignment and Receive Status Block */
1935 /* Set maximum frame length */
1937 umac_writel(priv, UMAC_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
1939 gib_set_pad_extension(priv);
1941 /* Set MAC address */
1942 umac_set_hw_addr(priv, dev->dev_addr);
1944 /* Read CRC forward */
1946 priv->crc_fwd = !!(umac_readl(priv, UMAC_CMD) & CMD_CRC_FWD);
1948 priv->crc_fwd = !!(gib_readl(priv, GIB_CONTROL) &
1951 phydev = of_phy_connect(dev, priv->phy_dn, bcm_sysport_adj_link,
1952 0, priv->phy_interface);
1954 netdev_err(dev, "could not attach to PHY\n");
1958 /* Reset house keeping link status */
1959 priv->old_duplex = -1;
1960 priv->old_link = -1;
1961 priv->old_pause = -1;
1963 /* mask all interrupts and request them */
1964 bcm_sysport_mask_all_intrs(priv);
1966 ret = request_irq(priv->irq0, bcm_sysport_rx_isr, 0, dev->name, dev);
1968 netdev_err(dev, "failed to request RX interrupt\n");
1969 goto out_phy_disconnect;
1972 if (!priv->is_lite) {
1973 ret = request_irq(priv->irq1, bcm_sysport_tx_isr, 0,
1976 netdev_err(dev, "failed to request TX interrupt\n");
1981 /* Initialize both hardware and software ring */
1982 for (i = 0; i < dev->num_tx_queues; i++) {
1983 ret = bcm_sysport_init_tx_ring(priv, i);
1985 netdev_err(dev, "failed to initialize TX ring %d\n",
1987 goto out_free_tx_ring;
1991 /* Initialize linked-list */
1992 tdma_writel(priv, TDMA_LL_RAM_INIT_BUSY, TDMA_STATUS);
1994 /* Initialize RX ring */
1995 ret = bcm_sysport_init_rx_ring(priv);
1997 netdev_err(dev, "failed to initialize RX ring\n");
1998 goto out_free_rx_ring;
2002 ret = rdma_enable_set(priv, 1);
2004 goto out_free_rx_ring;
2007 ret = tdma_enable_set(priv, 1);
2009 goto out_clear_rx_int;
2011 /* Turn on UniMAC TX/RX */
2012 umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 1);
2014 bcm_sysport_netif_start(dev);
2019 intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
2021 bcm_sysport_fini_rx_ring(priv);
2023 for (i = 0; i < dev->num_tx_queues; i++)
2024 bcm_sysport_fini_tx_ring(priv, i);
2026 free_irq(priv->irq1, dev);
2028 free_irq(priv->irq0, dev);
2030 phy_disconnect(phydev);
2034 static void bcm_sysport_netif_stop(struct net_device *dev)
2036 struct bcm_sysport_priv *priv = netdev_priv(dev);
2038 /* stop all software from updating hardware */
2039 netif_tx_stop_all_queues(dev);
2040 napi_disable(&priv->napi);
2041 cancel_work_sync(&priv->dim.dim.work);
2042 phy_stop(dev->phydev);
2044 /* mask all interrupts */
2045 bcm_sysport_mask_all_intrs(priv);
2048 static int bcm_sysport_stop(struct net_device *dev)
2050 struct bcm_sysport_priv *priv = netdev_priv(dev);
2054 bcm_sysport_netif_stop(dev);
2056 /* Disable UniMAC RX */
2057 umac_enable_set(priv, CMD_RX_EN, 0);
2059 ret = tdma_enable_set(priv, 0);
2061 netdev_err(dev, "timeout disabling RDMA\n");
2065 /* Wait for a maximum packet size to be drained */
2066 usleep_range(2000, 3000);
2068 ret = rdma_enable_set(priv, 0);
2070 netdev_err(dev, "timeout disabling TDMA\n");
2074 /* Disable UniMAC TX */
2075 umac_enable_set(priv, CMD_TX_EN, 0);
2077 /* Free RX/TX rings SW structures */
2078 for (i = 0; i < dev->num_tx_queues; i++)
2079 bcm_sysport_fini_tx_ring(priv, i);
2080 bcm_sysport_fini_rx_ring(priv);
2082 free_irq(priv->irq0, dev);
2084 free_irq(priv->irq1, dev);
2086 /* Disconnect from PHY */
2087 phy_disconnect(dev->phydev);
2092 static const struct ethtool_ops bcm_sysport_ethtool_ops = {
2093 .get_drvinfo = bcm_sysport_get_drvinfo,
2094 .get_msglevel = bcm_sysport_get_msglvl,
2095 .set_msglevel = bcm_sysport_set_msglvl,
2096 .get_link = ethtool_op_get_link,
2097 .get_strings = bcm_sysport_get_strings,
2098 .get_ethtool_stats = bcm_sysport_get_stats,
2099 .get_sset_count = bcm_sysport_get_sset_count,
2100 .get_wol = bcm_sysport_get_wol,
2101 .set_wol = bcm_sysport_set_wol,
2102 .get_coalesce = bcm_sysport_get_coalesce,
2103 .set_coalesce = bcm_sysport_set_coalesce,
2104 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2105 .set_link_ksettings = phy_ethtool_set_link_ksettings,
2108 static u16 bcm_sysport_select_queue(struct net_device *dev, struct sk_buff *skb,
2110 select_queue_fallback_t fallback)
2112 struct bcm_sysport_priv *priv = netdev_priv(dev);
2113 u16 queue = skb_get_queue_mapping(skb);
2114 struct bcm_sysport_tx_ring *tx_ring;
2115 unsigned int q, port;
2117 if (!netdev_uses_dsa(dev))
2118 return fallback(dev, skb);
2120 /* DSA tagging layer will have configured the correct queue */
2121 q = BRCM_TAG_GET_QUEUE(queue);
2122 port = BRCM_TAG_GET_PORT(queue);
2123 tx_ring = priv->ring_map[q + port * priv->per_port_num_tx_queues];
2125 if (unlikely(!tx_ring))
2126 return fallback(dev, skb);
2128 return tx_ring->index;
2131 static const struct net_device_ops bcm_sysport_netdev_ops = {
2132 .ndo_start_xmit = bcm_sysport_xmit,
2133 .ndo_tx_timeout = bcm_sysport_tx_timeout,
2134 .ndo_open = bcm_sysport_open,
2135 .ndo_stop = bcm_sysport_stop,
2136 .ndo_set_features = bcm_sysport_set_features,
2137 .ndo_set_rx_mode = bcm_sysport_set_rx_mode,
2138 .ndo_set_mac_address = bcm_sysport_change_mac,
2139 #ifdef CONFIG_NET_POLL_CONTROLLER
2140 .ndo_poll_controller = bcm_sysport_poll_controller,
2142 .ndo_get_stats64 = bcm_sysport_get_stats64,
2143 .ndo_select_queue = bcm_sysport_select_queue,
2146 static int bcm_sysport_map_queues(struct net_device *dev,
2147 struct dsa_notifier_register_info *info)
2149 struct bcm_sysport_priv *priv = netdev_priv(dev);
2150 struct bcm_sysport_tx_ring *ring;
2151 struct net_device *slave_dev;
2152 unsigned int num_tx_queues;
2153 unsigned int q, start, port;
2155 /* We can't be setting up queue inspection for non directly attached
2158 if (info->switch_number)
2161 if (dev->netdev_ops != &bcm_sysport_netdev_ops)
2164 port = info->port_number;
2165 slave_dev = info->info.dev;
2167 /* On SYSTEMPORT Lite we have twice as less queues, so we cannot do a
2168 * 1:1 mapping, we can only do a 2:1 mapping. By reducing the number of
2169 * per-port (slave_dev) network devices queue, we achieve just that.
2170 * This need to happen now before any slave network device is used such
2171 * it accurately reflects the number of real TX queues.
2174 netif_set_real_num_tx_queues(slave_dev,
2175 slave_dev->num_tx_queues / 2);
2176 num_tx_queues = slave_dev->real_num_tx_queues;
2178 if (priv->per_port_num_tx_queues &&
2179 priv->per_port_num_tx_queues != num_tx_queues)
2180 netdev_warn(slave_dev, "asymetric number of per-port queues\n");
2182 priv->per_port_num_tx_queues = num_tx_queues;
2184 start = find_first_zero_bit(&priv->queue_bitmap, dev->num_tx_queues);
2185 for (q = 0; q < num_tx_queues; q++) {
2186 ring = &priv->tx_rings[q + start];
2188 /* Just remember the mapping actual programming done
2189 * during bcm_sysport_init_tx_ring
2191 ring->switch_queue = q;
2192 ring->switch_port = port;
2193 ring->inspect = true;
2194 priv->ring_map[q + port * num_tx_queues] = ring;
2196 /* Set all queues as being used now */
2197 set_bit(q + start, &priv->queue_bitmap);
2203 static int bcm_sysport_dsa_notifier(struct notifier_block *unused,
2204 unsigned long event, void *ptr)
2206 struct dsa_notifier_register_info *info;
2208 if (event != DSA_PORT_REGISTER)
2213 return notifier_from_errno(bcm_sysport_map_queues(info->master, info));
2216 #define REV_FMT "v%2x.%02x"
2218 static const struct bcm_sysport_hw_params bcm_sysport_params[] = {
2221 .num_rx_desc_words = SP_NUM_HW_RX_DESC_WORDS,
2223 [SYSTEMPORT_LITE] = {
2225 .num_rx_desc_words = SP_LT_NUM_HW_RX_DESC_WORDS,
2229 static const struct of_device_id bcm_sysport_of_match[] = {
2230 { .compatible = "brcm,systemportlite-v1.00",
2231 .data = &bcm_sysport_params[SYSTEMPORT_LITE] },
2232 { .compatible = "brcm,systemport-v1.00",
2233 .data = &bcm_sysport_params[SYSTEMPORT] },
2234 { .compatible = "brcm,systemport",
2235 .data = &bcm_sysport_params[SYSTEMPORT] },
2238 MODULE_DEVICE_TABLE(of, bcm_sysport_of_match);
2240 static int bcm_sysport_probe(struct platform_device *pdev)
2242 const struct bcm_sysport_hw_params *params;
2243 const struct of_device_id *of_id = NULL;
2244 struct bcm_sysport_priv *priv;
2245 struct device_node *dn;
2246 struct net_device *dev;
2247 const void *macaddr;
2252 dn = pdev->dev.of_node;
2253 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2254 of_id = of_match_node(bcm_sysport_of_match, dn);
2255 if (!of_id || !of_id->data)
2258 /* Fairly quickly we need to know the type of adapter we have */
2259 params = of_id->data;
2261 /* Read the Transmit/Receive Queue properties */
2262 if (of_property_read_u32(dn, "systemport,num-txq", &txq))
2263 txq = TDMA_NUM_RINGS;
2264 if (of_property_read_u32(dn, "systemport,num-rxq", &rxq))
2267 /* Sanity check the number of transmit queues */
2268 if (!txq || txq > TDMA_NUM_RINGS)
2271 dev = alloc_etherdev_mqs(sizeof(*priv), txq, rxq);
2275 /* Initialize private members */
2276 priv = netdev_priv(dev);
2278 /* Allocate number of TX rings */
2279 priv->tx_rings = devm_kcalloc(&pdev->dev, txq,
2280 sizeof(struct bcm_sysport_tx_ring),
2282 if (!priv->tx_rings)
2285 priv->is_lite = params->is_lite;
2286 priv->num_rx_desc_words = params->num_rx_desc_words;
2288 priv->irq0 = platform_get_irq(pdev, 0);
2289 if (!priv->is_lite) {
2290 priv->irq1 = platform_get_irq(pdev, 1);
2291 priv->wol_irq = platform_get_irq(pdev, 2);
2293 priv->wol_irq = platform_get_irq(pdev, 1);
2295 if (priv->irq0 <= 0 || (priv->irq1 <= 0 && !priv->is_lite)) {
2296 dev_err(&pdev->dev, "invalid interrupts\n");
2298 goto err_free_netdev;
2301 priv->base = devm_ioremap_resource(&pdev->dev, r);
2302 if (IS_ERR(priv->base)) {
2303 ret = PTR_ERR(priv->base);
2304 goto err_free_netdev;
2310 priv->phy_interface = of_get_phy_mode(dn);
2311 /* Default to GMII interface mode */
2312 if (priv->phy_interface < 0)
2313 priv->phy_interface = PHY_INTERFACE_MODE_GMII;
2315 /* In the case of a fixed PHY, the DT node associated
2316 * to the PHY is the Ethernet MAC DT node.
2318 if (of_phy_is_fixed_link(dn)) {
2319 ret = of_phy_register_fixed_link(dn);
2321 dev_err(&pdev->dev, "failed to register fixed PHY\n");
2322 goto err_free_netdev;
2328 /* Initialize netdevice members */
2329 macaddr = of_get_mac_address(dn);
2330 if (!macaddr || !is_valid_ether_addr(macaddr)) {
2331 dev_warn(&pdev->dev, "using random Ethernet MAC\n");
2332 eth_hw_addr_random(dev);
2334 ether_addr_copy(dev->dev_addr, macaddr);
2337 SET_NETDEV_DEV(dev, &pdev->dev);
2338 dev_set_drvdata(&pdev->dev, dev);
2339 dev->ethtool_ops = &bcm_sysport_ethtool_ops;
2340 dev->netdev_ops = &bcm_sysport_netdev_ops;
2341 netif_napi_add(dev, &priv->napi, bcm_sysport_poll, 64);
2343 /* HW supported features, none enabled by default */
2344 dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_HIGHDMA |
2345 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2347 /* Request the WOL interrupt and advertise suspend if available */
2348 priv->wol_irq_disabled = 1;
2349 ret = devm_request_irq(&pdev->dev, priv->wol_irq,
2350 bcm_sysport_wol_isr, 0, dev->name, priv);
2352 device_set_wakeup_capable(&pdev->dev, 1);
2354 /* Set the needed headroom once and for all */
2355 BUILD_BUG_ON(sizeof(struct bcm_tsb) != 8);
2356 dev->needed_headroom += sizeof(struct bcm_tsb);
2358 /* libphy will adjust the link state accordingly */
2359 netif_carrier_off(dev);
2361 priv->rx_max_coalesced_frames = 1;
2362 u64_stats_init(&priv->syncp);
2364 priv->dsa_notifier.notifier_call = bcm_sysport_dsa_notifier;
2366 ret = register_dsa_notifier(&priv->dsa_notifier);
2368 dev_err(&pdev->dev, "failed to register DSA notifier\n");
2369 goto err_deregister_fixed_link;
2372 ret = register_netdev(dev);
2374 dev_err(&pdev->dev, "failed to register net_device\n");
2375 goto err_deregister_notifier;
2378 priv->rev = topctrl_readl(priv, REV_CNTL) & REV_MASK;
2379 dev_info(&pdev->dev,
2380 "Broadcom SYSTEMPORT%s" REV_FMT
2381 " at 0x%p (irqs: %d, %d, TXQs: %d, RXQs: %d)\n",
2382 priv->is_lite ? " Lite" : "",
2383 (priv->rev >> 8) & 0xff, priv->rev & 0xff,
2384 priv->base, priv->irq0, priv->irq1, txq, rxq);
2388 err_deregister_notifier:
2389 unregister_dsa_notifier(&priv->dsa_notifier);
2390 err_deregister_fixed_link:
2391 if (of_phy_is_fixed_link(dn))
2392 of_phy_deregister_fixed_link(dn);
2398 static int bcm_sysport_remove(struct platform_device *pdev)
2400 struct net_device *dev = dev_get_drvdata(&pdev->dev);
2401 struct bcm_sysport_priv *priv = netdev_priv(dev);
2402 struct device_node *dn = pdev->dev.of_node;
2404 /* Not much to do, ndo_close has been called
2405 * and we use managed allocations
2407 unregister_dsa_notifier(&priv->dsa_notifier);
2408 unregister_netdev(dev);
2409 if (of_phy_is_fixed_link(dn))
2410 of_phy_deregister_fixed_link(dn);
2412 dev_set_drvdata(&pdev->dev, NULL);
2417 #ifdef CONFIG_PM_SLEEP
2418 static int bcm_sysport_suspend_to_wol(struct bcm_sysport_priv *priv)
2420 struct net_device *ndev = priv->netdev;
2421 unsigned int timeout = 1000;
2424 /* Password has already been programmed */
2425 reg = umac_readl(priv, UMAC_MPD_CTRL);
2428 if (priv->wolopts & WAKE_MAGICSECURE)
2430 umac_writel(priv, reg, UMAC_MPD_CTRL);
2432 /* Make sure RBUF entered WoL mode as result */
2434 reg = rbuf_readl(priv, RBUF_STATUS);
2435 if (reg & RBUF_WOL_MODE)
2439 } while (timeout-- > 0);
2441 /* Do not leave the UniMAC RBUF matching only MPD packets */
2443 reg = umac_readl(priv, UMAC_MPD_CTRL);
2445 umac_writel(priv, reg, UMAC_MPD_CTRL);
2446 netif_err(priv, wol, ndev, "failed to enter WOL mode\n");
2450 /* UniMAC receive needs to be turned on */
2451 umac_enable_set(priv, CMD_RX_EN, 1);
2453 /* Enable the interrupt wake-up source */
2454 intrl2_0_mask_clear(priv, INTRL2_0_MPD);
2456 netif_dbg(priv, wol, ndev, "entered WOL mode\n");
2461 static int bcm_sysport_suspend(struct device *d)
2463 struct net_device *dev = dev_get_drvdata(d);
2464 struct bcm_sysport_priv *priv = netdev_priv(dev);
2469 if (!netif_running(dev))
2472 bcm_sysport_netif_stop(dev);
2474 phy_suspend(dev->phydev);
2476 netif_device_detach(dev);
2478 /* Disable UniMAC RX */
2479 umac_enable_set(priv, CMD_RX_EN, 0);
2481 ret = rdma_enable_set(priv, 0);
2483 netdev_err(dev, "RDMA timeout!\n");
2487 /* Disable RXCHK if enabled */
2488 if (priv->rx_chk_en) {
2489 reg = rxchk_readl(priv, RXCHK_CONTROL);
2491 rxchk_writel(priv, reg, RXCHK_CONTROL);
2496 topctrl_writel(priv, RX_FLUSH, RX_FLUSH_CNTL);
2498 ret = tdma_enable_set(priv, 0);
2500 netdev_err(dev, "TDMA timeout!\n");
2504 /* Wait for a packet boundary */
2505 usleep_range(2000, 3000);
2507 umac_enable_set(priv, CMD_TX_EN, 0);
2509 topctrl_writel(priv, TX_FLUSH, TX_FLUSH_CNTL);
2511 /* Free RX/TX rings SW structures */
2512 for (i = 0; i < dev->num_tx_queues; i++)
2513 bcm_sysport_fini_tx_ring(priv, i);
2514 bcm_sysport_fini_rx_ring(priv);
2516 /* Get prepared for Wake-on-LAN */
2517 if (device_may_wakeup(d) && priv->wolopts)
2518 ret = bcm_sysport_suspend_to_wol(priv);
2523 static int bcm_sysport_resume(struct device *d)
2525 struct net_device *dev = dev_get_drvdata(d);
2526 struct bcm_sysport_priv *priv = netdev_priv(dev);
2531 if (!netif_running(dev))
2536 /* We may have been suspended and never received a WOL event that
2537 * would turn off MPD detection, take care of that now
2539 bcm_sysport_resume_from_wol(priv);
2541 /* Initialize both hardware and software ring */
2542 for (i = 0; i < dev->num_tx_queues; i++) {
2543 ret = bcm_sysport_init_tx_ring(priv, i);
2545 netdev_err(dev, "failed to initialize TX ring %d\n",
2547 goto out_free_tx_rings;
2551 /* Initialize linked-list */
2552 tdma_writel(priv, TDMA_LL_RAM_INIT_BUSY, TDMA_STATUS);
2554 /* Initialize RX ring */
2555 ret = bcm_sysport_init_rx_ring(priv);
2557 netdev_err(dev, "failed to initialize RX ring\n");
2558 goto out_free_rx_ring;
2561 netif_device_attach(dev);
2563 /* RX pipe enable */
2564 topctrl_writel(priv, 0, RX_FLUSH_CNTL);
2566 ret = rdma_enable_set(priv, 1);
2568 netdev_err(dev, "failed to enable RDMA\n");
2569 goto out_free_rx_ring;
2573 if (priv->rx_chk_en) {
2574 reg = rxchk_readl(priv, RXCHK_CONTROL);
2576 rxchk_writel(priv, reg, RXCHK_CONTROL);
2581 /* Set maximum frame length */
2583 umac_writel(priv, UMAC_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
2585 gib_set_pad_extension(priv);
2587 /* Set MAC address */
2588 umac_set_hw_addr(priv, dev->dev_addr);
2590 umac_enable_set(priv, CMD_RX_EN, 1);
2592 /* TX pipe enable */
2593 topctrl_writel(priv, 0, TX_FLUSH_CNTL);
2595 umac_enable_set(priv, CMD_TX_EN, 1);
2597 ret = tdma_enable_set(priv, 1);
2599 netdev_err(dev, "TDMA timeout!\n");
2600 goto out_free_rx_ring;
2603 phy_resume(dev->phydev);
2605 bcm_sysport_netif_start(dev);
2610 bcm_sysport_fini_rx_ring(priv);
2612 for (i = 0; i < dev->num_tx_queues; i++)
2613 bcm_sysport_fini_tx_ring(priv, i);
2618 static SIMPLE_DEV_PM_OPS(bcm_sysport_pm_ops,
2619 bcm_sysport_suspend, bcm_sysport_resume);
2621 static struct platform_driver bcm_sysport_driver = {
2622 .probe = bcm_sysport_probe,
2623 .remove = bcm_sysport_remove,
2625 .name = "brcm-systemport",
2626 .of_match_table = bcm_sysport_of_match,
2627 .pm = &bcm_sysport_pm_ops,
2630 module_platform_driver(bcm_sysport_driver);
2632 MODULE_AUTHOR("Broadcom Corporation");
2633 MODULE_DESCRIPTION("Broadcom System Port Ethernet MAC driver");
2634 MODULE_ALIAS("platform:brcm-systemport");
2635 MODULE_LICENSE("GPL");