linux/drivers/net/ethernet/broadcom/bcmsysport.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Broadcom BCM7xxx System Port Ethernet MAC driver
   4 *
   5 * Copyright (C) 2014 Broadcom Corporation
   6 */
   7
   8#define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
   9
  10#include <linux/init.h>
  11#include <linux/interrupt.h>
  12#include <linux/module.h>
  13#include <linux/kernel.h>
  14#include <linux/netdevice.h>
  15#include <linux/etherdevice.h>
  16#include <linux/platform_device.h>
  17#include <linux/of.h>
  18#include <linux/of_net.h>
  19#include <linux/of_mdio.h>
  20#include <linux/phy.h>
  21#include <linux/phy_fixed.h>
  22#include <net/dsa.h>
  23#include <net/ip.h>
  24#include <net/ipv6.h>
  25
  26#include "bcmsysport.h"
  27
  28/* I/O accessors register helpers */
  29#define BCM_SYSPORT_IO_MACRO(name, offset) \
  30static inline u32 name##_readl(struct bcm_sysport_priv *priv, u32 off)  \
  31{                                                                       \
  32        u32 reg = readl_relaxed(priv->base + offset + off);             \
  33        return reg;                                                     \
  34}                                                                       \
  35static inline void name##_writel(struct bcm_sysport_priv *priv,         \
  36                                  u32 val, u32 off)                     \
  37{                                                                       \
  38        writel_relaxed(val, priv->base + offset + off);                 \
  39}                                                                       \
  40
  41BCM_SYSPORT_IO_MACRO(intrl2_0, SYS_PORT_INTRL2_0_OFFSET);
  42BCM_SYSPORT_IO_MACRO(intrl2_1, SYS_PORT_INTRL2_1_OFFSET);
  43BCM_SYSPORT_IO_MACRO(umac, SYS_PORT_UMAC_OFFSET);
  44BCM_SYSPORT_IO_MACRO(gib, SYS_PORT_GIB_OFFSET);
  45BCM_SYSPORT_IO_MACRO(tdma, SYS_PORT_TDMA_OFFSET);
  46BCM_SYSPORT_IO_MACRO(rxchk, SYS_PORT_RXCHK_OFFSET);
  47BCM_SYSPORT_IO_MACRO(txchk, SYS_PORT_TXCHK_OFFSET);
  48BCM_SYSPORT_IO_MACRO(rbuf, SYS_PORT_RBUF_OFFSET);
  49BCM_SYSPORT_IO_MACRO(tbuf, SYS_PORT_TBUF_OFFSET);
  50BCM_SYSPORT_IO_MACRO(topctrl, SYS_PORT_TOPCTRL_OFFSET);
  51
  52/* On SYSTEMPORT Lite, any register after RDMA_STATUS has the exact
  53 * same layout, except it has been moved by 4 bytes up, *sigh*
  54 */
  55static inline u32 rdma_readl(struct bcm_sysport_priv *priv, u32 off)
  56{
  57        if (priv->is_lite && off >= RDMA_STATUS)
  58                off += 4;
  59        return readl_relaxed(priv->base + SYS_PORT_RDMA_OFFSET + off);
  60}
  61
  62static inline void rdma_writel(struct bcm_sysport_priv *priv, u32 val, u32 off)
  63{
  64        if (priv->is_lite && off >= RDMA_STATUS)
  65                off += 4;
  66        writel_relaxed(val, priv->base + SYS_PORT_RDMA_OFFSET + off);
  67}
  68
  69static inline u32 tdma_control_bit(struct bcm_sysport_priv *priv, u32 bit)
  70{
  71        if (!priv->is_lite) {
  72                return BIT(bit);
  73        } else {
  74                if (bit >= ACB_ALGO)
  75                        return BIT(bit + 1);
  76                else
  77                        return BIT(bit);
  78        }
  79}
  80
  81/* L2-interrupt masking/unmasking helpers, does automatic saving of the applied
  82 * mask in a software copy to avoid CPU_MASK_STATUS reads in hot-paths.
  83  */
  84#define BCM_SYSPORT_INTR_L2(which)      \
  85static inline void intrl2_##which##_mask_clear(struct bcm_sysport_priv *priv, \
  86                                                u32 mask)               \
  87{                                                                       \
  88        priv->irq##which##_mask &= ~(mask);                             \
  89        intrl2_##which##_writel(priv, mask, INTRL2_CPU_MASK_CLEAR);     \
  90}                                                                       \
  91static inline void intrl2_##which##_mask_set(struct bcm_sysport_priv *priv, \
  92                                                u32 mask)               \
  93{                                                                       \
  94        intrl2_## which##_writel(priv, mask, INTRL2_CPU_MASK_SET);      \
  95        priv->irq##which##_mask |= (mask);                              \
  96}                                                                       \
  97
  98BCM_SYSPORT_INTR_L2(0)
  99BCM_SYSPORT_INTR_L2(1)
 100
 101/* Register accesses to GISB/RBUS registers are expensive (few hundred
 102 * nanoseconds), so keep the check for 64-bits explicit here to save
 103 * one register write per-packet on 32-bits platforms.
 104 */
 105static inline void dma_desc_set_addr(struct bcm_sysport_priv *priv,
 106                                     void __iomem *d,
 107                                     dma_addr_t addr)
 108{
 109#ifdef CONFIG_PHYS_ADDR_T_64BIT
 110        writel_relaxed(upper_32_bits(addr) & DESC_ADDR_HI_MASK,
 111                     d + DESC_ADDR_HI_STATUS_LEN);
 112#endif
 113        writel_relaxed(lower_32_bits(addr), d + DESC_ADDR_LO);
 114}
 115
 116/* Ethtool operations */
 117static void bcm_sysport_set_rx_csum(struct net_device *dev,
 118                                    netdev_features_t wanted)
 119{
 120        struct bcm_sysport_priv *priv = netdev_priv(dev);
 121        u32 reg;
 122
 123        priv->rx_chk_en = !!(wanted & NETIF_F_RXCSUM);
 124        reg = rxchk_readl(priv, RXCHK_CONTROL);
 125        /* Clear L2 header checks, which would prevent BPDUs
 126         * from being received.
 127         */
 128        reg &= ~RXCHK_L2_HDR_DIS;
 129        if (priv->rx_chk_en)
 130                reg |= RXCHK_EN;
 131        else
 132                reg &= ~RXCHK_EN;
 133
 134        /* If UniMAC forwards CRC, we need to skip over it to get
 135         * a valid CHK bit to be set in the per-packet status word
 136         */
 137        if (priv->rx_chk_en && priv->crc_fwd)
 138                reg |= RXCHK_SKIP_FCS;
 139        else
 140                reg &= ~RXCHK_SKIP_FCS;
 141
 142        /* If Broadcom tags are enabled (e.g: using a switch), make
 143         * sure we tell the RXCHK hardware to expect a 4-bytes Broadcom
 144         * tag after the Ethernet MAC Source Address.
 145         */
 146        if (netdev_uses_dsa(dev))
 147                reg |= RXCHK_BRCM_TAG_EN;
 148        else
 149                reg &= ~RXCHK_BRCM_TAG_EN;
 150
 151        rxchk_writel(priv, reg, RXCHK_CONTROL);
 152}
 153
 154static void bcm_sysport_set_tx_csum(struct net_device *dev,
 155                                    netdev_features_t wanted)
 156{
 157        struct bcm_sysport_priv *priv = netdev_priv(dev);
 158        u32 reg;
 159
 160        /* Hardware transmit checksum requires us to enable the Transmit status
 161         * block prepended to the packet contents
 162         */
 163        priv->tsb_en = !!(wanted & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
 164        reg = tdma_readl(priv, TDMA_CONTROL);
 165        if (priv->tsb_en)
 166                reg |= tdma_control_bit(priv, TSB_EN);
 167        else
 168                reg &= ~tdma_control_bit(priv, TSB_EN);
 169        tdma_writel(priv, reg, TDMA_CONTROL);
 170}
 171
 172static int bcm_sysport_set_features(struct net_device *dev,
 173                                    netdev_features_t features)
 174{
 175        struct bcm_sysport_priv *priv = netdev_priv(dev);
 176
 177        /* Read CRC forward */
 178        if (!priv->is_lite)
 179                priv->crc_fwd = !!(umac_readl(priv, UMAC_CMD) & CMD_CRC_FWD);
 180        else
 181                priv->crc_fwd = !((gib_readl(priv, GIB_CONTROL) &
 182                                  GIB_FCS_STRIP) >> GIB_FCS_STRIP_SHIFT);
 183
 184        bcm_sysport_set_rx_csum(dev, features);
 185        bcm_sysport_set_tx_csum(dev, features);
 186
 187        return 0;
 188}
 189
 190/* Hardware counters must be kept in sync because the order/offset
 191 * is important here (order in structure declaration = order in hardware)
 192 */
 193static const struct bcm_sysport_stats bcm_sysport_gstrings_stats[] = {
 194        /* general stats */
 195        STAT_NETDEV64(rx_packets),
 196        STAT_NETDEV64(tx_packets),
 197        STAT_NETDEV64(rx_bytes),
 198        STAT_NETDEV64(tx_bytes),
 199        STAT_NETDEV(rx_errors),
 200        STAT_NETDEV(tx_errors),
 201        STAT_NETDEV(rx_dropped),
 202        STAT_NETDEV(tx_dropped),
 203        STAT_NETDEV(multicast),
 204        /* UniMAC RSV counters */
 205        STAT_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
 206        STAT_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
 207        STAT_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
 208        STAT_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
 209        STAT_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
 210        STAT_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
 211        STAT_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
 212        STAT_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
 213        STAT_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
 214        STAT_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
 215        STAT_MIB_RX("rx_pkts", mib.rx.pkt),
 216        STAT_MIB_RX("rx_bytes", mib.rx.bytes),
 217        STAT_MIB_RX("rx_multicast", mib.rx.mca),
 218        STAT_MIB_RX("rx_broadcast", mib.rx.bca),
 219        STAT_MIB_RX("rx_fcs", mib.rx.fcs),
 220        STAT_MIB_RX("rx_control", mib.rx.cf),
 221        STAT_MIB_RX("rx_pause", mib.rx.pf),
 222        STAT_MIB_RX("rx_unknown", mib.rx.uo),
 223        STAT_MIB_RX("rx_align", mib.rx.aln),
 224        STAT_MIB_RX("rx_outrange", mib.rx.flr),
 225        STAT_MIB_RX("rx_code", mib.rx.cde),
 226        STAT_MIB_RX("rx_carrier", mib.rx.fcr),
 227        STAT_MIB_RX("rx_oversize", mib.rx.ovr),
 228        STAT_MIB_RX("rx_jabber", mib.rx.jbr),
 229        STAT_MIB_RX("rx_mtu_err", mib.rx.mtue),
 230        STAT_MIB_RX("rx_good_pkts", mib.rx.pok),
 231        STAT_MIB_RX("rx_unicast", mib.rx.uc),
 232        STAT_MIB_RX("rx_ppp", mib.rx.ppp),
 233        STAT_MIB_RX("rx_crc", mib.rx.rcrc),
 234        /* UniMAC TSV counters */
 235        STAT_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
 236        STAT_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
 237        STAT_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
 238        STAT_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
 239        STAT_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
 240        STAT_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
 241        STAT_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
 242        STAT_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
 243        STAT_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
 244        STAT_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
 245        STAT_MIB_TX("tx_pkts", mib.tx.pkts),
 246        STAT_MIB_TX("tx_multicast", mib.tx.mca),
 247        STAT_MIB_TX("tx_broadcast", mib.tx.bca),
 248        STAT_MIB_TX("tx_pause", mib.tx.pf),
 249        STAT_MIB_TX("tx_control", mib.tx.cf),
 250        STAT_MIB_TX("tx_fcs_err", mib.tx.fcs),
 251        STAT_MIB_TX("tx_oversize", mib.tx.ovr),
 252        STAT_MIB_TX("tx_defer", mib.tx.drf),
 253        STAT_MIB_TX("tx_excess_defer", mib.tx.edf),
 254        STAT_MIB_TX("tx_single_col", mib.tx.scl),
 255        STAT_MIB_TX("tx_multi_col", mib.tx.mcl),
 256        STAT_MIB_TX("tx_late_col", mib.tx.lcl),
 257        STAT_MIB_TX("tx_excess_col", mib.tx.ecl),
 258        STAT_MIB_TX("tx_frags", mib.tx.frg),
 259        STAT_MIB_TX("tx_total_col", mib.tx.ncl),
 260        STAT_MIB_TX("tx_jabber", mib.tx.jbr),
 261        STAT_MIB_TX("tx_bytes", mib.tx.bytes),
 262        STAT_MIB_TX("tx_good_pkts", mib.tx.pok),
 263        STAT_MIB_TX("tx_unicast", mib.tx.uc),
 264        /* UniMAC RUNT counters */
 265        STAT_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
 266        STAT_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
 267        STAT_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
 268        STAT_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
 269        /* RXCHK misc statistics */
 270        STAT_RXCHK("rxchk_bad_csum", mib.rxchk_bad_csum, RXCHK_BAD_CSUM_CNTR),
 271        STAT_RXCHK("rxchk_other_pkt_disc", mib.rxchk_other_pkt_disc,
 272                   RXCHK_OTHER_DISC_CNTR),
 273        /* RBUF misc statistics */
 274        STAT_RBUF("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt, RBUF_OVFL_DISC_CNTR),
 275        STAT_RBUF("rbuf_err_cnt", mib.rbuf_err_cnt, RBUF_ERR_PKT_CNTR),
 276        STAT_MIB_SOFT("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
 277        STAT_MIB_SOFT("rx_dma_failed", mib.rx_dma_failed),
 278        STAT_MIB_SOFT("tx_dma_failed", mib.tx_dma_failed),
 279        STAT_MIB_SOFT("tx_realloc_tsb", mib.tx_realloc_tsb),
 280        STAT_MIB_SOFT("tx_realloc_tsb_failed", mib.tx_realloc_tsb_failed),
 281        /* Per TX-queue statistics are dynamically appended */
 282};
 283
 284#define BCM_SYSPORT_STATS_LEN   ARRAY_SIZE(bcm_sysport_gstrings_stats)
 285
 286static void bcm_sysport_get_drvinfo(struct net_device *dev,
 287                                    struct ethtool_drvinfo *info)
 288{
 289        strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
 290        strlcpy(info->bus_info, "platform", sizeof(info->bus_info));
 291}
 292
 293static u32 bcm_sysport_get_msglvl(struct net_device *dev)
 294{
 295        struct bcm_sysport_priv *priv = netdev_priv(dev);
 296
 297        return priv->msg_enable;
 298}
 299
 300static void bcm_sysport_set_msglvl(struct net_device *dev, u32 enable)
 301{
 302        struct bcm_sysport_priv *priv = netdev_priv(dev);
 303
 304        priv->msg_enable = enable;
 305}
 306
 307static inline bool bcm_sysport_lite_stat_valid(enum bcm_sysport_stat_type type)
 308{
 309        switch (type) {
 310        case BCM_SYSPORT_STAT_NETDEV:
 311        case BCM_SYSPORT_STAT_NETDEV64:
 312        case BCM_SYSPORT_STAT_RXCHK:
 313        case BCM_SYSPORT_STAT_RBUF:
 314        case BCM_SYSPORT_STAT_SOFT:
 315                return true;
 316        default:
 317                return false;
 318        }
 319}
 320
 321static int bcm_sysport_get_sset_count(struct net_device *dev, int string_set)
 322{
 323        struct bcm_sysport_priv *priv = netdev_priv(dev);
 324        const struct bcm_sysport_stats *s;
 325        unsigned int i, j;
 326
 327        switch (string_set) {
 328        case ETH_SS_STATS:
 329                for (i = 0, j = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
 330                        s = &bcm_sysport_gstrings_stats[i];
 331                        if (priv->is_lite &&
 332                            !bcm_sysport_lite_stat_valid(s->type))
 333                                continue;
 334                        j++;
 335                }
 336                /* Include per-queue statistics */
 337                return j + dev->num_tx_queues * NUM_SYSPORT_TXQ_STAT;
 338        default:
 339                return -EOPNOTSUPP;
 340        }
 341}
 342
 343static void bcm_sysport_get_strings(struct net_device *dev,
 344                                    u32 stringset, u8 *data)
 345{
 346        struct bcm_sysport_priv *priv = netdev_priv(dev);
 347        const struct bcm_sysport_stats *s;
 348        char buf[128];
 349        int i, j;
 350
 351        switch (stringset) {
 352        case ETH_SS_STATS:
 353                for (i = 0, j = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
 354                        s = &bcm_sysport_gstrings_stats[i];
 355                        if (priv->is_lite &&
 356                            !bcm_sysport_lite_stat_valid(s->type))
 357                                continue;
 358
 359                        memcpy(data + j * ETH_GSTRING_LEN, s->stat_string,
 360                               ETH_GSTRING_LEN);
 361                        j++;
 362                }
 363
 364                for (i = 0; i < dev->num_tx_queues; i++) {
 365                        snprintf(buf, sizeof(buf), "txq%d_packets", i);
 366                        memcpy(data + j * ETH_GSTRING_LEN, buf,
 367                               ETH_GSTRING_LEN);
 368                        j++;
 369
 370                        snprintf(buf, sizeof(buf), "txq%d_bytes", i);
 371                        memcpy(data + j * ETH_GSTRING_LEN, buf,
 372                               ETH_GSTRING_LEN);
 373                        j++;
 374                }
 375                break;
 376        default:
 377                break;
 378        }
 379}
 380
 381static void bcm_sysport_update_mib_counters(struct bcm_sysport_priv *priv)
 382{
 383        int i, j = 0;
 384
 385        for (i = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
 386                const struct bcm_sysport_stats *s;
 387                u8 offset = 0;
 388                u32 val = 0;
 389                char *p;
 390
 391                s = &bcm_sysport_gstrings_stats[i];
 392                switch (s->type) {
 393                case BCM_SYSPORT_STAT_NETDEV:
 394                case BCM_SYSPORT_STAT_NETDEV64:
 395                case BCM_SYSPORT_STAT_SOFT:
 396                        continue;
 397                case BCM_SYSPORT_STAT_MIB_RX:
 398                case BCM_SYSPORT_STAT_MIB_TX:
 399                case BCM_SYSPORT_STAT_RUNT:
 400                        if (priv->is_lite)
 401                                continue;
 402
 403                        if (s->type != BCM_SYSPORT_STAT_MIB_RX)
 404                                offset = UMAC_MIB_STAT_OFFSET;
 405                        val = umac_readl(priv, UMAC_MIB_START + j + offset);
 406                        break;
 407                case BCM_SYSPORT_STAT_RXCHK:
 408                        val = rxchk_readl(priv, s->reg_offset);
 409                        if (val == ~0)
 410                                rxchk_writel(priv, 0, s->reg_offset);
 411                        break;
 412                case BCM_SYSPORT_STAT_RBUF:
 413                        val = rbuf_readl(priv, s->reg_offset);
 414                        if (val == ~0)
 415                                rbuf_writel(priv, 0, s->reg_offset);
 416                        break;
 417                }
 418
 419                j += s->stat_sizeof;
 420                p = (char *)priv + s->stat_offset;
 421                *(u32 *)p = val;
 422        }
 423
 424        netif_dbg(priv, hw, priv->netdev, "updated MIB counters\n");
 425}
 426
 427static void bcm_sysport_update_tx_stats(struct bcm_sysport_priv *priv,
 428                                        u64 *tx_bytes, u64 *tx_packets)
 429{
 430        struct bcm_sysport_tx_ring *ring;
 431        u64 bytes = 0, packets = 0;
 432        unsigned int start;
 433        unsigned int q;
 434
 435        for (q = 0; q < priv->netdev->num_tx_queues; q++) {
 436                ring = &priv->tx_rings[q];
 437                do {
 438                        start = u64_stats_fetch_begin_irq(&priv->syncp);
 439                        bytes = ring->bytes;
 440                        packets = ring->packets;
 441                } while (u64_stats_fetch_retry_irq(&priv->syncp, start));
 442
 443                *tx_bytes += bytes;
 444                *tx_packets += packets;
 445        }
 446}
 447
 448static void bcm_sysport_get_stats(struct net_device *dev,
 449                                  struct ethtool_stats *stats, u64 *data)
 450{
 451        struct bcm_sysport_priv *priv = netdev_priv(dev);
 452        struct bcm_sysport_stats64 *stats64 = &priv->stats64;
 453        struct u64_stats_sync *syncp = &priv->syncp;
 454        struct bcm_sysport_tx_ring *ring;
 455        u64 tx_bytes = 0, tx_packets = 0;
 456        unsigned int start;
 457        int i, j;
 458
 459        if (netif_running(dev)) {
 460                bcm_sysport_update_mib_counters(priv);
 461                bcm_sysport_update_tx_stats(priv, &tx_bytes, &tx_packets);
 462                stats64->tx_bytes = tx_bytes;
 463                stats64->tx_packets = tx_packets;
 464        }
 465
 466        for (i =  0, j = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
 467                const struct bcm_sysport_stats *s;
 468                char *p;
 469
 470                s = &bcm_sysport_gstrings_stats[i];
 471                if (s->type == BCM_SYSPORT_STAT_NETDEV)
 472                        p = (char *)&dev->stats;
 473                else if (s->type == BCM_SYSPORT_STAT_NETDEV64)
 474                        p = (char *)stats64;
 475                else
 476                        p = (char *)priv;
 477
 478                if (priv->is_lite && !bcm_sysport_lite_stat_valid(s->type))
 479                        continue;
 480                p += s->stat_offset;
 481
 482                if (s->stat_sizeof == sizeof(u64) &&
 483                    s->type == BCM_SYSPORT_STAT_NETDEV64) {
 484                        do {
 485                                start = u64_stats_fetch_begin_irq(syncp);
 486                                data[i] = *(u64 *)p;
 487                        } while (u64_stats_fetch_retry_irq(syncp, start));
 488                } else
 489                        data[i] = *(u32 *)p;
 490                j++;
 491        }
 492
 493        /* For SYSTEMPORT Lite since we have holes in our statistics, j would
 494         * be equal to BCM_SYSPORT_STATS_LEN at the end of the loop, but it
 495         * needs to point to how many total statistics we have minus the
 496         * number of per TX queue statistics
 497         */
 498        j = bcm_sysport_get_sset_count(dev, ETH_SS_STATS) -
 499            dev->num_tx_queues * NUM_SYSPORT_TXQ_STAT;
 500
 501        for (i = 0; i < dev->num_tx_queues; i++) {
 502                ring = &priv->tx_rings[i];
 503                data[j] = ring->packets;
 504                j++;
 505                data[j] = ring->bytes;
 506                j++;
 507        }
 508}
 509
 510static void bcm_sysport_get_wol(struct net_device *dev,
 511                                struct ethtool_wolinfo *wol)
 512{
 513        struct bcm_sysport_priv *priv = netdev_priv(dev);
 514
 515        wol->supported = WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER;
 516        wol->wolopts = priv->wolopts;
 517
 518        if (!(priv->wolopts & WAKE_MAGICSECURE))
 519                return;
 520
 521        memcpy(wol->sopass, priv->sopass, sizeof(priv->sopass));
 522}
 523
 524static int bcm_sysport_set_wol(struct net_device *dev,
 525                               struct ethtool_wolinfo *wol)
 526{
 527        struct bcm_sysport_priv *priv = netdev_priv(dev);
 528        struct device *kdev = &priv->pdev->dev;
 529        u32 supported = WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER;
 530
 531        if (!device_can_wakeup(kdev))
 532                return -ENOTSUPP;
 533
 534        if (wol->wolopts & ~supported)
 535                return -EINVAL;
 536
 537        if (wol->wolopts & WAKE_MAGICSECURE)
 538                memcpy(priv->sopass, wol->sopass, sizeof(priv->sopass));
 539
 540        /* Flag the device and relevant IRQ as wakeup capable */
 541        if (wol->wolopts) {
 542                device_set_wakeup_enable(kdev, 1);
 543                if (priv->wol_irq_disabled)
 544                        enable_irq_wake(priv->wol_irq);
 545                priv->wol_irq_disabled = 0;
 546        } else {
 547                device_set_wakeup_enable(kdev, 0);
 548                /* Avoid unbalanced disable_irq_wake calls */
 549                if (!priv->wol_irq_disabled)
 550                        disable_irq_wake(priv->wol_irq);
 551                priv->wol_irq_disabled = 1;
 552        }
 553
 554        priv->wolopts = wol->wolopts;
 555
 556        return 0;
 557}
 558
 559static void bcm_sysport_set_rx_coalesce(struct bcm_sysport_priv *priv,
 560                                        u32 usecs, u32 pkts)
 561{
 562        u32 reg;
 563
 564        reg = rdma_readl(priv, RDMA_MBDONE_INTR);
 565        reg &= ~(RDMA_INTR_THRESH_MASK |
 566                 RDMA_TIMEOUT_MASK << RDMA_TIMEOUT_SHIFT);
 567        reg |= pkts;
 568        reg |= DIV_ROUND_UP(usecs * 1000, 8192) << RDMA_TIMEOUT_SHIFT;
 569        rdma_writel(priv, reg, RDMA_MBDONE_INTR);
 570}
 571
 572static void bcm_sysport_set_tx_coalesce(struct bcm_sysport_tx_ring *ring,
 573                                        struct ethtool_coalesce *ec)
 574{
 575        struct bcm_sysport_priv *priv = ring->priv;
 576        u32 reg;
 577
 578        reg = tdma_readl(priv, TDMA_DESC_RING_INTR_CONTROL(ring->index));
 579        reg &= ~(RING_INTR_THRESH_MASK |
 580                 RING_TIMEOUT_MASK << RING_TIMEOUT_SHIFT);
 581        reg |= ec->tx_max_coalesced_frames;
 582        reg |= DIV_ROUND_UP(ec->tx_coalesce_usecs * 1000, 8192) <<
 583                            RING_TIMEOUT_SHIFT;
 584        tdma_writel(priv, reg, TDMA_DESC_RING_INTR_CONTROL(ring->index));
 585}
 586
 587static int bcm_sysport_get_coalesce(struct net_device *dev,
 588                                    struct ethtool_coalesce *ec)
 589{
 590        struct bcm_sysport_priv *priv = netdev_priv(dev);
 591        u32 reg;
 592
 593        reg = tdma_readl(priv, TDMA_DESC_RING_INTR_CONTROL(0));
 594
 595        ec->tx_coalesce_usecs = (reg >> RING_TIMEOUT_SHIFT) * 8192 / 1000;
 596        ec->tx_max_coalesced_frames = reg & RING_INTR_THRESH_MASK;
 597
 598        reg = rdma_readl(priv, RDMA_MBDONE_INTR);
 599
 600        ec->rx_coalesce_usecs = (reg >> RDMA_TIMEOUT_SHIFT) * 8192 / 1000;
 601        ec->rx_max_coalesced_frames = reg & RDMA_INTR_THRESH_MASK;
 602        ec->use_adaptive_rx_coalesce = priv->dim.use_dim;
 603
 604        return 0;
 605}
 606
 607static int bcm_sysport_set_coalesce(struct net_device *dev,
 608                                    struct ethtool_coalesce *ec)
 609{
 610        struct bcm_sysport_priv *priv = netdev_priv(dev);
 611        struct dim_cq_moder moder;
 612        u32 usecs, pkts;
 613        unsigned int i;
 614
 615        /* Base system clock is 125Mhz, DMA timeout is this reference clock
 616         * divided by 1024, which yield roughly 8.192 us, our maximum value has
 617         * to fit in the RING_TIMEOUT_MASK (16 bits).
 618         */
 619        if (ec->tx_max_coalesced_frames > RING_INTR_THRESH_MASK ||
 620            ec->tx_coalesce_usecs > (RING_TIMEOUT_MASK * 8) + 1 ||
 621            ec->rx_max_coalesced_frames > RDMA_INTR_THRESH_MASK ||
 622            ec->rx_coalesce_usecs > (RDMA_TIMEOUT_MASK * 8) + 1)
 623                return -EINVAL;
 624
 625        if ((ec->tx_coalesce_usecs == 0 && ec->tx_max_coalesced_frames == 0) ||
 626            (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0))
 627                return -EINVAL;
 628
 629        for (i = 0; i < dev->num_tx_queues; i++)
 630                bcm_sysport_set_tx_coalesce(&priv->tx_rings[i], ec);
 631
 632        priv->rx_coalesce_usecs = ec->rx_coalesce_usecs;
 633        priv->rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
 634        usecs = priv->rx_coalesce_usecs;
 635        pkts = priv->rx_max_coalesced_frames;
 636
 637        if (ec->use_adaptive_rx_coalesce && !priv->dim.use_dim) {
 638                moder = net_dim_get_def_rx_moderation(priv->dim.dim.mode);
 639                usecs = moder.usec;
 640                pkts = moder.pkts;
 641        }
 642
 643        priv->dim.use_dim = ec->use_adaptive_rx_coalesce;
 644
 645        /* Apply desired coalescing parameters */
 646        bcm_sysport_set_rx_coalesce(priv, usecs, pkts);
 647
 648        return 0;
 649}
 650
 651static void bcm_sysport_free_cb(struct bcm_sysport_cb *cb)
 652{
 653        dev_consume_skb_any(cb->skb);
 654        cb->skb = NULL;
 655        dma_unmap_addr_set(cb, dma_addr, 0);
 656}
 657
 658static struct sk_buff *bcm_sysport_rx_refill(struct bcm_sysport_priv *priv,
 659                                             struct bcm_sysport_cb *cb)
 660{
 661        struct device *kdev = &priv->pdev->dev;
 662        struct net_device *ndev = priv->netdev;
 663        struct sk_buff *skb, *rx_skb;
 664        dma_addr_t mapping;
 665
 666        /* Allocate a new SKB for a new packet */
 667        skb = __netdev_alloc_skb(priv->netdev, RX_BUF_LENGTH,
 668                                 GFP_ATOMIC | __GFP_NOWARN);
 669        if (!skb) {
 670                priv->mib.alloc_rx_buff_failed++;
 671                netif_err(priv, rx_err, ndev, "SKB alloc failed\n");
 672                return NULL;
 673        }
 674
 675        mapping = dma_map_single(kdev, skb->data,
 676                                 RX_BUF_LENGTH, DMA_FROM_DEVICE);
 677        if (dma_mapping_error(kdev, mapping)) {
 678                priv->mib.rx_dma_failed++;
 679                dev_kfree_skb_any(skb);
 680                netif_err(priv, rx_err, ndev, "DMA mapping failure\n");
 681                return NULL;
 682        }
 683
 684        /* Grab the current SKB on the ring */
 685        rx_skb = cb->skb;
 686        if (likely(rx_skb))
 687                dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
 688                                 RX_BUF_LENGTH, DMA_FROM_DEVICE);
 689
 690        /* Put the new SKB on the ring */
 691        cb->skb = skb;
 692        dma_unmap_addr_set(cb, dma_addr, mapping);
 693        dma_desc_set_addr(priv, cb->bd_addr, mapping);
 694
 695        netif_dbg(priv, rx_status, ndev, "RX refill\n");
 696
 697        /* Return the current SKB to the caller */
 698        return rx_skb;
 699}
 700
 701static int bcm_sysport_alloc_rx_bufs(struct bcm_sysport_priv *priv)
 702{
 703        struct bcm_sysport_cb *cb;
 704        struct sk_buff *skb;
 705        unsigned int i;
 706
 707        for (i = 0; i < priv->num_rx_bds; i++) {
 708                cb = &priv->rx_cbs[i];
 709                skb = bcm_sysport_rx_refill(priv, cb);
 710                dev_kfree_skb(skb);
 711                if (!cb->skb)
 712                        return -ENOMEM;
 713        }
 714
 715        return 0;
 716}
 717
 718/* Poll the hardware for up to budget packets to process */
 719static unsigned int bcm_sysport_desc_rx(struct bcm_sysport_priv *priv,
 720                                        unsigned int budget)
 721{
 722        struct bcm_sysport_stats64 *stats64 = &priv->stats64;
 723        struct net_device *ndev = priv->netdev;
 724        unsigned int processed = 0, to_process;
 725        unsigned int processed_bytes = 0;
 726        struct bcm_sysport_cb *cb;
 727        struct sk_buff *skb;
 728        unsigned int p_index;
 729        u16 len, status;
 730        struct bcm_rsb *rsb;
 731
 732        /* Clear status before servicing to reduce spurious interrupts */
 733        intrl2_0_writel(priv, INTRL2_0_RDMA_MBDONE, INTRL2_CPU_CLEAR);
 734
 735        /* Determine how much we should process since last call, SYSTEMPORT Lite
 736         * groups the producer and consumer indexes into the same 32-bit
 737         * which we access using RDMA_CONS_INDEX
 738         */
 739        if (!priv->is_lite)
 740                p_index = rdma_readl(priv, RDMA_PROD_INDEX);
 741        else
 742                p_index = rdma_readl(priv, RDMA_CONS_INDEX);
 743        p_index &= RDMA_PROD_INDEX_MASK;
 744
 745        to_process = (p_index - priv->rx_c_index) & RDMA_CONS_INDEX_MASK;
 746
 747        netif_dbg(priv, rx_status, ndev,
 748                  "p_index=%d rx_c_index=%d to_process=%d\n",
 749                  p_index, priv->rx_c_index, to_process);
 750
 751        while ((processed < to_process) && (processed < budget)) {
 752                cb = &priv->rx_cbs[priv->rx_read_ptr];
 753                skb = bcm_sysport_rx_refill(priv, cb);
 754
 755
 756                /* We do not have a backing SKB, so we do not a corresponding
 757                 * DMA mapping for this incoming packet since
 758                 * bcm_sysport_rx_refill always either has both skb and mapping
 759                 * or none.
 760                 */
 761                if (unlikely(!skb)) {
 762                        netif_err(priv, rx_err, ndev, "out of memory!\n");
 763                        ndev->stats.rx_dropped++;
 764                        ndev->stats.rx_errors++;
 765                        goto next;
 766                }
 767
 768                /* Extract the Receive Status Block prepended */
 769                rsb = (struct bcm_rsb *)skb->data;
 770                len = (rsb->rx_status_len >> DESC_LEN_SHIFT) & DESC_LEN_MASK;
 771                status = (rsb->rx_status_len >> DESC_STATUS_SHIFT) &
 772                          DESC_STATUS_MASK;
 773
 774                netif_dbg(priv, rx_status, ndev,
 775                          "p=%d, c=%d, rd_ptr=%d, len=%d, flag=0x%04x\n",
 776                          p_index, priv->rx_c_index, priv->rx_read_ptr,
 777                          len, status);
 778
 779                if (unlikely(len > RX_BUF_LENGTH)) {
 780                        netif_err(priv, rx_status, ndev, "oversized packet\n");
 781                        ndev->stats.rx_length_errors++;
 782                        ndev->stats.rx_errors++;
 783                        dev_kfree_skb_any(skb);
 784                        goto next;
 785                }
 786
 787                if (unlikely(!(status & DESC_EOP) || !(status & DESC_SOP))) {
 788                        netif_err(priv, rx_status, ndev, "fragmented packet!\n");
 789                        ndev->stats.rx_dropped++;
 790                        ndev->stats.rx_errors++;
 791                        dev_kfree_skb_any(skb);
 792                        goto next;
 793                }
 794
 795                if (unlikely(status & (RX_STATUS_ERR | RX_STATUS_OVFLOW))) {
 796                        netif_err(priv, rx_err, ndev, "error packet\n");
 797                        if (status & RX_STATUS_OVFLOW)
 798                                ndev->stats.rx_over_errors++;
 799                        ndev->stats.rx_dropped++;
 800                        ndev->stats.rx_errors++;
 801                        dev_kfree_skb_any(skb);
 802                        goto next;
 803                }
 804
 805                skb_put(skb, len);
 806
 807                /* Hardware validated our checksum */
 808                if (likely(status & DESC_L4_CSUM))
 809                        skb->ip_summed = CHECKSUM_UNNECESSARY;
 810
 811                /* Hardware pre-pends packets with 2bytes before Ethernet
 812                 * header plus we have the Receive Status Block, strip off all
 813                 * of this from the SKB.
 814                 */
 815                skb_pull(skb, sizeof(*rsb) + 2);
 816                len -= (sizeof(*rsb) + 2);
 817                processed_bytes += len;
 818
 819                /* UniMAC may forward CRC */
 820                if (priv->crc_fwd) {
 821                        skb_trim(skb, len - ETH_FCS_LEN);
 822                        len -= ETH_FCS_LEN;
 823                }
 824
 825                skb->protocol = eth_type_trans(skb, ndev);
 826                ndev->stats.rx_packets++;
 827                ndev->stats.rx_bytes += len;
 828                u64_stats_update_begin(&priv->syncp);
 829                stats64->rx_packets++;
 830                stats64->rx_bytes += len;
 831                u64_stats_update_end(&priv->syncp);
 832
 833                napi_gro_receive(&priv->napi, skb);
 834next:
 835                processed++;
 836                priv->rx_read_ptr++;
 837
 838                if (priv->rx_read_ptr == priv->num_rx_bds)
 839                        priv->rx_read_ptr = 0;
 840        }
 841
 842        priv->dim.packets = processed;
 843        priv->dim.bytes = processed_bytes;
 844
 845        return processed;
 846}
 847
 848static void bcm_sysport_tx_reclaim_one(struct bcm_sysport_tx_ring *ring,
 849                                       struct bcm_sysport_cb *cb,
 850                                       unsigned int *bytes_compl,
 851                                       unsigned int *pkts_compl)
 852{
 853        struct bcm_sysport_priv *priv = ring->priv;
 854        struct device *kdev = &priv->pdev->dev;
 855
 856        if (cb->skb) {
 857                *bytes_compl += cb->skb->len;
 858                dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
 859                                 dma_unmap_len(cb, dma_len),
 860                                 DMA_TO_DEVICE);
 861                (*pkts_compl)++;
 862                bcm_sysport_free_cb(cb);
 863        /* SKB fragment */
 864        } else if (dma_unmap_addr(cb, dma_addr)) {
 865                *bytes_compl += dma_unmap_len(cb, dma_len);
 866                dma_unmap_page(kdev, dma_unmap_addr(cb, dma_addr),
 867                               dma_unmap_len(cb, dma_len), DMA_TO_DEVICE);
 868                dma_unmap_addr_set(cb, dma_addr, 0);
 869        }
 870}
 871
 872/* Reclaim queued SKBs for transmission completion, lockless version */
 873static unsigned int __bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
 874                                             struct bcm_sysport_tx_ring *ring)
 875{
 876        unsigned int pkts_compl = 0, bytes_compl = 0;
 877        struct net_device *ndev = priv->netdev;
 878        unsigned int txbds_processed = 0;
 879        struct bcm_sysport_cb *cb;
 880        unsigned int txbds_ready;
 881        unsigned int c_index;
 882        u32 hw_ind;
 883
 884        /* Clear status before servicing to reduce spurious interrupts */
 885        if (!ring->priv->is_lite)
 886                intrl2_1_writel(ring->priv, BIT(ring->index), INTRL2_CPU_CLEAR);
 887        else
 888                intrl2_0_writel(ring->priv, BIT(ring->index +
 889                                INTRL2_0_TDMA_MBDONE_SHIFT), INTRL2_CPU_CLEAR);
 890
 891        /* Compute how many descriptors have been processed since last call */
 892        hw_ind = tdma_readl(priv, TDMA_DESC_RING_PROD_CONS_INDEX(ring->index));
 893        c_index = (hw_ind >> RING_CONS_INDEX_SHIFT) & RING_CONS_INDEX_MASK;
 894        txbds_ready = (c_index - ring->c_index) & RING_CONS_INDEX_MASK;
 895
 896        netif_dbg(priv, tx_done, ndev,
 897                  "ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
 898                  ring->index, ring->c_index, c_index, txbds_ready);
 899
 900        while (txbds_processed < txbds_ready) {
 901                cb = &ring->cbs[ring->clean_index];
 902                bcm_sysport_tx_reclaim_one(ring, cb, &bytes_compl, &pkts_compl);
 903
 904                ring->desc_count++;
 905                txbds_processed++;
 906
 907                if (likely(ring->clean_index < ring->size - 1))
 908                        ring->clean_index++;
 909                else
 910                        ring->clean_index = 0;
 911        }
 912
 913        u64_stats_update_begin(&priv->syncp);
 914        ring->packets += pkts_compl;
 915        ring->bytes += bytes_compl;
 916        u64_stats_update_end(&priv->syncp);
 917
 918        ring->c_index = c_index;
 919
 920        netif_dbg(priv, tx_done, ndev,
 921                  "ring=%d c_index=%d pkts_compl=%d, bytes_compl=%d\n",
 922                  ring->index, ring->c_index, pkts_compl, bytes_compl);
 923
 924        return pkts_compl;
 925}
 926
 927/* Locked version of the per-ring TX reclaim routine */
 928static unsigned int bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
 929                                           struct bcm_sysport_tx_ring *ring)
 930{
 931        struct netdev_queue *txq;
 932        unsigned int released;
 933        unsigned long flags;
 934
 935        txq = netdev_get_tx_queue(priv->netdev, ring->index);
 936
 937        spin_lock_irqsave(&ring->lock, flags);
 938        released = __bcm_sysport_tx_reclaim(priv, ring);
 939        if (released)
 940                netif_tx_wake_queue(txq);
 941
 942        spin_unlock_irqrestore(&ring->lock, flags);
 943
 944        return released;
 945}
 946
 947/* Locked version of the per-ring TX reclaim, but does not wake the queue */
 948static void bcm_sysport_tx_clean(struct bcm_sysport_priv *priv,
 949                                 struct bcm_sysport_tx_ring *ring)
 950{
 951        unsigned long flags;
 952
 953        spin_lock_irqsave(&ring->lock, flags);
 954        __bcm_sysport_tx_reclaim(priv, ring);
 955        spin_unlock_irqrestore(&ring->lock, flags);
 956}
 957
 958static int bcm_sysport_tx_poll(struct napi_struct *napi, int budget)
 959{
 960        struct bcm_sysport_tx_ring *ring =
 961                container_of(napi, struct bcm_sysport_tx_ring, napi);
 962        unsigned int work_done = 0;
 963
 964        work_done = bcm_sysport_tx_reclaim(ring->priv, ring);
 965
 966        if (work_done == 0) {
 967                napi_complete(napi);
 968                /* re-enable TX interrupt */
 969                if (!ring->priv->is_lite)
 970                        intrl2_1_mask_clear(ring->priv, BIT(ring->index));
 971                else
 972                        intrl2_0_mask_clear(ring->priv, BIT(ring->index +
 973                                            INTRL2_0_TDMA_MBDONE_SHIFT));
 974
 975                return 0;
 976        }
 977
 978        return budget;
 979}
 980
 981static void bcm_sysport_tx_reclaim_all(struct bcm_sysport_priv *priv)
 982{
 983        unsigned int q;
 984
 985        for (q = 0; q < priv->netdev->num_tx_queues; q++)
 986                bcm_sysport_tx_reclaim(priv, &priv->tx_rings[q]);
 987}
 988
 989static int bcm_sysport_poll(struct napi_struct *napi, int budget)
 990{
 991        struct bcm_sysport_priv *priv =
 992                container_of(napi, struct bcm_sysport_priv, napi);
 993        struct dim_sample dim_sample = {};
 994        unsigned int work_done = 0;
 995
 996        work_done = bcm_sysport_desc_rx(priv, budget);
 997
 998        priv->rx_c_index += work_done;
 999        priv->rx_c_index &= RDMA_CONS_INDEX_MASK;
1000
1001        /* SYSTEMPORT Lite groups the producer/consumer index, producer is
1002         * maintained by HW, but writes to it will be ignore while RDMA
1003         * is active
1004         */
1005        if (!priv->is_lite)
1006                rdma_writel(priv, priv->rx_c_index, RDMA_CONS_INDEX);
1007        else
1008                rdma_writel(priv, priv->rx_c_index << 16, RDMA_CONS_INDEX);
1009
1010        if (work_done < budget) {
1011                napi_complete_done(napi, work_done);
1012                /* re-enable RX interrupts */
1013                intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE);
1014        }
1015
1016        if (priv->dim.use_dim) {
1017                dim_update_sample(priv->dim.event_ctr, priv->dim.packets,
1018                                  priv->dim.bytes, &dim_sample);
1019                net_dim(&priv->dim.dim, dim_sample);
1020        }
1021
1022        return work_done;
1023}
1024
1025static void mpd_enable_set(struct bcm_sysport_priv *priv, bool enable)
1026{
1027        u32 reg, bit;
1028
1029        reg = umac_readl(priv, UMAC_MPD_CTRL);
1030        if (enable)
1031                reg |= MPD_EN;
1032        else
1033                reg &= ~MPD_EN;
1034        umac_writel(priv, reg, UMAC_MPD_CTRL);
1035
1036        if (priv->is_lite)
1037                bit = RBUF_ACPI_EN_LITE;
1038        else
1039                bit = RBUF_ACPI_EN;
1040
1041        reg = rbuf_readl(priv, RBUF_CONTROL);
1042        if (enable)
1043                reg |= bit;
1044        else
1045                reg &= ~bit;
1046        rbuf_writel(priv, reg, RBUF_CONTROL);
1047}
1048
1049static void bcm_sysport_resume_from_wol(struct bcm_sysport_priv *priv)
1050{
1051        unsigned int index;
1052        u32 reg;
1053
1054        /* Disable RXCHK, active filters and Broadcom tag matching */
1055        reg = rxchk_readl(priv, RXCHK_CONTROL);
1056        reg &= ~(RXCHK_BRCM_TAG_MATCH_MASK <<
1057                 RXCHK_BRCM_TAG_MATCH_SHIFT | RXCHK_EN | RXCHK_BRCM_TAG_EN);
1058        rxchk_writel(priv, reg, RXCHK_CONTROL);
1059
1060        /* Make sure we restore correct CID index in case HW lost
1061         * its context during deep idle state
1062         */
1063        for_each_set_bit(index, priv->filters, RXCHK_BRCM_TAG_MAX) {
1064                rxchk_writel(priv, priv->filters_loc[index] <<
1065                             RXCHK_BRCM_TAG_CID_SHIFT, RXCHK_BRCM_TAG(index));
1066                rxchk_writel(priv, 0xff00ffff, RXCHK_BRCM_TAG_MASK(index));
1067        }
1068
1069        /* Clear the MagicPacket detection logic */
1070        mpd_enable_set(priv, false);
1071
1072        reg = intrl2_0_readl(priv, INTRL2_CPU_STATUS);
1073        if (reg & INTRL2_0_MPD)
1074                netdev_info(priv->netdev, "Wake-on-LAN (MPD) interrupt!\n");
1075
1076        if (reg & INTRL2_0_BRCM_MATCH_TAG) {
1077                reg = rxchk_readl(priv, RXCHK_BRCM_TAG_MATCH_STATUS) &
1078                                  RXCHK_BRCM_TAG_MATCH_MASK;
1079                netdev_info(priv->netdev,
1080                            "Wake-on-LAN (filters 0x%02x) interrupt!\n", reg);
1081        }
1082
1083        netif_dbg(priv, wol, priv->netdev, "resumed from WOL\n");
1084}
1085
1086static void bcm_sysport_dim_work(struct work_struct *work)
1087{
1088        struct dim *dim = container_of(work, struct dim, work);
1089        struct bcm_sysport_net_dim *ndim =
1090                        container_of(dim, struct bcm_sysport_net_dim, dim);
1091        struct bcm_sysport_priv *priv =
1092                        container_of(ndim, struct bcm_sysport_priv, dim);
1093        struct dim_cq_moder cur_profile = net_dim_get_rx_moderation(dim->mode,
1094                                                                    dim->profile_ix);
1095
1096        bcm_sysport_set_rx_coalesce(priv, cur_profile.usec, cur_profile.pkts);
1097        dim->state = DIM_START_MEASURE;
1098}
1099
1100/* RX and misc interrupt routine */
1101static irqreturn_t bcm_sysport_rx_isr(int irq, void *dev_id)
1102{
1103        struct net_device *dev = dev_id;
1104        struct bcm_sysport_priv *priv = netdev_priv(dev);
1105        struct bcm_sysport_tx_ring *txr;
1106        unsigned int ring, ring_bit;
1107
1108        priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
1109                          ~intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
1110        intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
1111
1112        if (unlikely(priv->irq0_stat == 0)) {
1113                netdev_warn(priv->netdev, "spurious RX interrupt\n");
1114                return IRQ_NONE;
1115        }
1116
1117        if (priv->irq0_stat & INTRL2_0_RDMA_MBDONE) {
1118                priv->dim.event_ctr++;
1119                if (likely(napi_schedule_prep(&priv->napi))) {
1120                        /* disable RX interrupts */
1121                        intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE);
1122                        __napi_schedule_irqoff(&priv->napi);
1123                }
1124        }
1125
1126        /* TX ring is full, perform a full reclaim since we do not know
1127         * which one would trigger this interrupt
1128         */
1129        if (priv->irq0_stat & INTRL2_0_TX_RING_FULL)
1130                bcm_sysport_tx_reclaim_all(priv);
1131
1132        if (!priv->is_lite)
1133                goto out;
1134
1135        for (ring = 0; ring < dev->num_tx_queues; ring++) {
1136                ring_bit = BIT(ring + INTRL2_0_TDMA_MBDONE_SHIFT);
1137                if (!(priv->irq0_stat & ring_bit))
1138                        continue;
1139
1140                txr = &priv->tx_rings[ring];
1141
1142                if (likely(napi_schedule_prep(&txr->napi))) {
1143                        intrl2_0_mask_set(priv, ring_bit);
1144                        __napi_schedule(&txr->napi);
1145                }
1146        }
1147out:
1148        return IRQ_HANDLED;
1149}
1150
1151/* TX interrupt service routine */
1152static irqreturn_t bcm_sysport_tx_isr(int irq, void *dev_id)
1153{
1154        struct net_device *dev = dev_id;
1155        struct bcm_sysport_priv *priv = netdev_priv(dev);
1156        struct bcm_sysport_tx_ring *txr;
1157        unsigned int ring;
1158
1159        priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
1160                                ~intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
1161        intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1162
1163        if (unlikely(priv->irq1_stat == 0)) {
1164                netdev_warn(priv->netdev, "spurious TX interrupt\n");
1165                return IRQ_NONE;
1166        }
1167
1168        for (ring = 0; ring < dev->num_tx_queues; ring++) {
1169                if (!(priv->irq1_stat & BIT(ring)))
1170                        continue;
1171
1172                txr = &priv->tx_rings[ring];
1173
1174                if (likely(napi_schedule_prep(&txr->napi))) {
1175                        intrl2_1_mask_set(priv, BIT(ring));
1176                        __napi_schedule_irqoff(&txr->napi);
1177                }
1178        }
1179
1180        return IRQ_HANDLED;
1181}
1182
1183static irqreturn_t bcm_sysport_wol_isr(int irq, void *dev_id)
1184{
1185        struct bcm_sysport_priv *priv = dev_id;
1186
1187        pm_wakeup_event(&priv->pdev->dev, 0);
1188
1189        return IRQ_HANDLED;
1190}
1191
1192#ifdef CONFIG_NET_POLL_CONTROLLER
1193static void bcm_sysport_poll_controller(struct net_device *dev)
1194{
1195        struct bcm_sysport_priv *priv = netdev_priv(dev);
1196
1197        disable_irq(priv->irq0);
1198        bcm_sysport_rx_isr(priv->irq0, priv);
1199        enable_irq(priv->irq0);
1200
1201        if (!priv->is_lite) {
1202                disable_irq(priv->irq1);
1203                bcm_sysport_tx_isr(priv->irq1, priv);
1204                enable_irq(priv->irq1);
1205        }
1206}
1207#endif
1208
1209static struct sk_buff *bcm_sysport_insert_tsb(struct sk_buff *skb,
1210                                              struct net_device *dev)
1211{
1212        struct bcm_sysport_priv *priv = netdev_priv(dev);
1213        struct sk_buff *nskb;
1214        struct bcm_tsb *tsb;
1215        u32 csum_info;
1216        u8 ip_proto;
1217        u16 csum_start;
1218        __be16 ip_ver;
1219
1220        /* Re-allocate SKB if needed */
1221        if (unlikely(skb_headroom(skb) < sizeof(*tsb))) {
1222                nskb = skb_realloc_headroom(skb, sizeof(*tsb));
1223                if (!nskb) {
1224                        dev_kfree_skb_any(skb);
1225                        priv->mib.tx_realloc_tsb_failed++;
1226                        dev->stats.tx_errors++;
1227                        dev->stats.tx_dropped++;
1228                        return NULL;
1229                }
1230                dev_consume_skb_any(skb);
1231                skb = nskb;
1232                priv->mib.tx_realloc_tsb++;
1233        }
1234
1235        tsb = skb_push(skb, sizeof(*tsb));
1236        /* Zero-out TSB by default */
1237        memset(tsb, 0, sizeof(*tsb));
1238
1239        if (skb->ip_summed == CHECKSUM_PARTIAL) {
1240                ip_ver = skb->protocol;
1241                switch (ip_ver) {
1242                case htons(ETH_P_IP):
1243                        ip_proto = ip_hdr(skb)->protocol;
1244                        break;
1245                case htons(ETH_P_IPV6):
1246                        ip_proto = ipv6_hdr(skb)->nexthdr;
1247                        break;
1248                default:
1249                        return skb;
1250                }
1251
1252                /* Get the checksum offset and the L4 (transport) offset */
1253                csum_start = skb_checksum_start_offset(skb) - sizeof(*tsb);
1254                csum_info = (csum_start + skb->csum_offset) & L4_CSUM_PTR_MASK;
1255                csum_info |= (csum_start << L4_PTR_SHIFT);
1256
1257                if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
1258                        csum_info |= L4_LENGTH_VALID;
1259                        if (ip_proto == IPPROTO_UDP &&
1260                            ip_ver == htons(ETH_P_IP))
1261                                csum_info |= L4_UDP;
1262                } else {
1263                        csum_info = 0;
1264                }
1265
1266                tsb->l4_ptr_dest_map = csum_info;
1267        }
1268
1269        return skb;
1270}
1271
1272static netdev_tx_t bcm_sysport_xmit(struct sk_buff *skb,
1273                                    struct net_device *dev)
1274{
1275        struct bcm_sysport_priv *priv = netdev_priv(dev);
1276        struct device *kdev = &priv->pdev->dev;
1277        struct bcm_sysport_tx_ring *ring;
1278        struct bcm_sysport_cb *cb;
1279        struct netdev_queue *txq;
1280        u32 len_status, addr_lo;
1281        unsigned int skb_len;
1282        unsigned long flags;
1283        dma_addr_t mapping;
1284        u16 queue;
1285        int ret;
1286
1287        queue = skb_get_queue_mapping(skb);
1288        txq = netdev_get_tx_queue(dev, queue);
1289        ring = &priv->tx_rings[queue];
1290
1291        /* lock against tx reclaim in BH context and TX ring full interrupt */
1292        spin_lock_irqsave(&ring->lock, flags);
1293        if (unlikely(ring->desc_count == 0)) {
1294                netif_tx_stop_queue(txq);
1295                netdev_err(dev, "queue %d awake and ring full!\n", queue);
1296                ret = NETDEV_TX_BUSY;
1297                goto out;
1298        }
1299
1300        /* Insert TSB and checksum infos */
1301        if (priv->tsb_en) {
1302                skb = bcm_sysport_insert_tsb(skb, dev);
1303                if (!skb) {
1304                        ret = NETDEV_TX_OK;
1305                        goto out;
1306                }
1307        }
1308
1309        skb_len = skb->len;
1310
1311        mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
1312        if (dma_mapping_error(kdev, mapping)) {
1313                priv->mib.tx_dma_failed++;
1314                netif_err(priv, tx_err, dev, "DMA map failed at %p (len=%d)\n",
1315                          skb->data, skb_len);
1316                ret = NETDEV_TX_OK;
1317                goto out;
1318        }
1319
1320        /* Remember the SKB for future freeing */
1321        cb = &ring->cbs[ring->curr_desc];
1322        cb->skb = skb;
1323        dma_unmap_addr_set(cb, dma_addr, mapping);
1324        dma_unmap_len_set(cb, dma_len, skb_len);
1325
1326        addr_lo = lower_32_bits(mapping);
1327        len_status = upper_32_bits(mapping) & DESC_ADDR_HI_MASK;
1328        len_status |= (skb_len << DESC_LEN_SHIFT);
1329        len_status |= (DESC_SOP | DESC_EOP | TX_STATUS_APP_CRC) <<
1330                       DESC_STATUS_SHIFT;
1331        if (skb->ip_summed == CHECKSUM_PARTIAL)
1332                len_status |= (DESC_L4_CSUM << DESC_STATUS_SHIFT);
1333
1334        ring->curr_desc++;
1335        if (ring->curr_desc == ring->size)
1336                ring->curr_desc = 0;
1337        ring->desc_count--;
1338
1339        /* Ports are latched, so write upper address first */
1340        tdma_writel(priv, len_status, TDMA_WRITE_PORT_HI(ring->index));
1341        tdma_writel(priv, addr_lo, TDMA_WRITE_PORT_LO(ring->index));
1342
1343        /* Check ring space and update SW control flow */
1344        if (ring->desc_count == 0)
1345                netif_tx_stop_queue(txq);
1346
1347        netif_dbg(priv, tx_queued, dev, "ring=%d desc_count=%d, curr_desc=%d\n",
1348                  ring->index, ring->desc_count, ring->curr_desc);
1349
1350        ret = NETDEV_TX_OK;
1351out:
1352        spin_unlock_irqrestore(&ring->lock, flags);
1353        return ret;
1354}
1355
1356static void bcm_sysport_tx_timeout(struct net_device *dev, unsigned int txqueue)
1357{
1358        netdev_warn(dev, "transmit timeout!\n");
1359
1360        netif_trans_update(dev);
1361        dev->stats.tx_errors++;
1362
1363        netif_tx_wake_all_queues(dev);
1364}
1365
1366/* phylib adjust link callback */
1367static void bcm_sysport_adj_link(struct net_device *dev)
1368{
1369        struct bcm_sysport_priv *priv = netdev_priv(dev);
1370        struct phy_device *phydev = dev->phydev;
1371        unsigned int changed = 0;
1372        u32 cmd_bits = 0, reg;
1373
1374        if (priv->old_link != phydev->link) {
1375                changed = 1;
1376                priv->old_link = phydev->link;
1377        }
1378
1379        if (priv->old_duplex != phydev->duplex) {
1380                changed = 1;
1381                priv->old_duplex = phydev->duplex;
1382        }
1383
1384        if (priv->is_lite)
1385                goto out;
1386
1387        switch (phydev->speed) {
1388        case SPEED_2500:
1389                cmd_bits = CMD_SPEED_2500;
1390                break;
1391        case SPEED_1000:
1392                cmd_bits = CMD_SPEED_1000;
1393                break;
1394        case SPEED_100:
1395                cmd_bits = CMD_SPEED_100;
1396                break;
1397        case SPEED_10:
1398                cmd_bits = CMD_SPEED_10;
1399                break;
1400        default:
1401                break;
1402        }
1403        cmd_bits <<= CMD_SPEED_SHIFT;
1404
1405        if (phydev->duplex == DUPLEX_HALF)
1406                cmd_bits |= CMD_HD_EN;
1407
1408        if (priv->old_pause != phydev->pause) {
1409                changed = 1;
1410                priv->old_pause = phydev->pause;
1411        }
1412
1413        if (!phydev->pause)
1414                cmd_bits |= CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE;
1415
1416        if (!changed)
1417                return;
1418
1419        if (phydev->link) {
1420                reg = umac_readl(priv, UMAC_CMD);
1421                reg &= ~((CMD_SPEED_MASK << CMD_SPEED_SHIFT) |
1422                        CMD_HD_EN | CMD_RX_PAUSE_IGNORE |
1423                        CMD_TX_PAUSE_IGNORE);
1424                reg |= cmd_bits;
1425                umac_writel(priv, reg, UMAC_CMD);
1426        }
1427out:
1428        if (changed)
1429                phy_print_status(phydev);
1430}
1431
1432static void bcm_sysport_init_dim(struct bcm_sysport_priv *priv,
1433                                 void (*cb)(struct work_struct *work))
1434{
1435        struct bcm_sysport_net_dim *dim = &priv->dim;
1436
1437        INIT_WORK(&dim->dim.work, cb);
1438        dim->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1439        dim->event_ctr = 0;
1440        dim->packets = 0;
1441        dim->bytes = 0;
1442}
1443
1444static void bcm_sysport_init_rx_coalesce(struct bcm_sysport_priv *priv)
1445{
1446        struct bcm_sysport_net_dim *dim = &priv->dim;
1447        struct dim_cq_moder moder;
1448        u32 usecs, pkts;
1449
1450        usecs = priv->rx_coalesce_usecs;
1451        pkts = priv->rx_max_coalesced_frames;
1452
1453        /* If DIM was enabled, re-apply default parameters */
1454        if (dim->use_dim) {
1455                moder = net_dim_get_def_rx_moderation(dim->dim.mode);
1456                usecs = moder.usec;
1457                pkts = moder.pkts;
1458        }
1459
1460        bcm_sysport_set_rx_coalesce(priv, usecs, pkts);
1461}
1462
1463static int bcm_sysport_init_tx_ring(struct bcm_sysport_priv *priv,
1464                                    unsigned int index)
1465{
1466        struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
1467        size_t size;
1468        u32 reg;
1469
1470        /* Simple descriptors partitioning for now */
1471        size = 256;
1472
1473        ring->cbs = kcalloc(size, sizeof(struct bcm_sysport_cb), GFP_KERNEL);
1474        if (!ring->cbs) {
1475                netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
1476                return -ENOMEM;
1477        }
1478
1479        /* Initialize SW view of the ring */
1480        spin_lock_init(&ring->lock);
1481        ring->priv = priv;
1482        netif_tx_napi_add(priv->netdev, &ring->napi, bcm_sysport_tx_poll, 64);
1483        ring->index = index;
1484        ring->size = size;
1485        ring->clean_index = 0;
1486        ring->alloc_size = ring->size;
1487        ring->desc_count = ring->size;
1488        ring->curr_desc = 0;
1489
1490        /* Initialize HW ring */
1491        tdma_writel(priv, RING_EN, TDMA_DESC_RING_HEAD_TAIL_PTR(index));
1492        tdma_writel(priv, 0, TDMA_DESC_RING_COUNT(index));
1493        tdma_writel(priv, 1, TDMA_DESC_RING_INTR_CONTROL(index));
1494        tdma_writel(priv, 0, TDMA_DESC_RING_PROD_CONS_INDEX(index));
1495
1496        /* Configure QID and port mapping */
1497        reg = tdma_readl(priv, TDMA_DESC_RING_MAPPING(index));
1498        reg &= ~(RING_QID_MASK | RING_PORT_ID_MASK << RING_PORT_ID_SHIFT);
1499        if (ring->inspect) {
1500                reg |= ring->switch_queue & RING_QID_MASK;
1501                reg |= ring->switch_port << RING_PORT_ID_SHIFT;
1502        } else {
1503                reg |= RING_IGNORE_STATUS;
1504        }
1505        tdma_writel(priv, reg, TDMA_DESC_RING_MAPPING(index));
1506        tdma_writel(priv, 0, TDMA_DESC_RING_PCP_DEI_VID(index));
1507
1508        /* Enable ACB algorithm 2 */
1509        reg = tdma_readl(priv, TDMA_CONTROL);
1510        reg |= tdma_control_bit(priv, ACB_ALGO);
1511        tdma_writel(priv, reg, TDMA_CONTROL);
1512
1513        /* Do not use tdma_control_bit() here because TSB_SWAP1 collides
1514         * with the original definition of ACB_ALGO
1515         */
1516        reg = tdma_readl(priv, TDMA_CONTROL);
1517        if (priv->is_lite)
1518                reg &= ~BIT(TSB_SWAP1);
1519        /* Set a correct TSB format based on host endian */
1520        if (!IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
1521                reg |= tdma_control_bit(priv, TSB_SWAP0);
1522        else
1523                reg &= ~tdma_control_bit(priv, TSB_SWAP0);
1524        tdma_writel(priv, reg, TDMA_CONTROL);
1525
1526        /* Program the number of descriptors as MAX_THRESHOLD and half of
1527         * its size for the hysteresis trigger
1528         */
1529        tdma_writel(priv, ring->size |
1530                        1 << RING_HYST_THRESH_SHIFT,
1531                        TDMA_DESC_RING_MAX_HYST(index));
1532
1533        /* Enable the ring queue in the arbiter */
1534        reg = tdma_readl(priv, TDMA_TIER1_ARB_0_QUEUE_EN);
1535        reg |= (1 << index);
1536        tdma_writel(priv, reg, TDMA_TIER1_ARB_0_QUEUE_EN);
1537
1538        napi_enable(&ring->napi);
1539
1540        netif_dbg(priv, hw, priv->netdev,
1541                  "TDMA cfg, size=%d, switch q=%d,port=%d\n",
1542                  ring->size, ring->switch_queue,
1543                  ring->switch_port);
1544
1545        return 0;
1546}
1547
1548static void bcm_sysport_fini_tx_ring(struct bcm_sysport_priv *priv,
1549                                     unsigned int index)
1550{
1551        struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
1552        u32 reg;
1553
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");
1558
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.
1562         */
1563        if (!ring->cbs)
1564                return;
1565
1566        napi_disable(&ring->napi);
1567        netif_napi_del(&ring->napi);
1568
1569        bcm_sysport_tx_clean(priv, ring);
1570
1571        kfree(ring->cbs);
1572        ring->cbs = NULL;
1573        ring->size = 0;
1574        ring->alloc_size = 0;
1575
1576        netif_dbg(priv, hw, priv->netdev, "TDMA fini done\n");
1577}
1578
1579/* RDMA helper */
1580static inline int rdma_enable_set(struct bcm_sysport_priv *priv,
1581                                  unsigned int enable)
1582{
1583        unsigned int timeout = 1000;
1584        u32 reg;
1585
1586        reg = rdma_readl(priv, RDMA_CONTROL);
1587        if (enable)
1588                reg |= RDMA_EN;
1589        else
1590                reg &= ~RDMA_EN;
1591        rdma_writel(priv, reg, RDMA_CONTROL);
1592
1593        /* Poll for RMDA disabling completion */
1594        do {
1595                reg = rdma_readl(priv, RDMA_STATUS);
1596                if (!!(reg & RDMA_DISABLED) == !enable)
1597                        return 0;
1598                usleep_range(1000, 2000);
1599        } while (timeout-- > 0);
1600
1601        netdev_err(priv->netdev, "timeout waiting for RDMA to finish\n");
1602
1603        return -ETIMEDOUT;
1604}
1605
1606/* TDMA helper */
1607static inline int tdma_enable_set(struct bcm_sysport_priv *priv,
1608                                  unsigned int enable)
1609{
1610        unsigned int timeout = 1000;
1611        u32 reg;
1612
1613        reg = tdma_readl(priv, TDMA_CONTROL);
1614        if (enable)
1615                reg |= tdma_control_bit(priv, TDMA_EN);
1616        else
1617                reg &= ~tdma_control_bit(priv, TDMA_EN);
1618        tdma_writel(priv, reg, TDMA_CONTROL);
1619
1620        /* Poll for TMDA disabling completion */
1621        do {
1622                reg = tdma_readl(priv, TDMA_STATUS);
1623                if (!!(reg & TDMA_DISABLED) == !enable)
1624                        return 0;
1625
1626                usleep_range(1000, 2000);
1627        } while (timeout-- > 0);
1628
1629        netdev_err(priv->netdev, "timeout waiting for TDMA to finish\n");
1630
1631        return -ETIMEDOUT;
1632}
1633
1634static int bcm_sysport_init_rx_ring(struct bcm_sysport_priv *priv)
1635{
1636        struct bcm_sysport_cb *cb;
1637        u32 reg;
1638        int ret;
1639        int i;
1640
1641        /* Initialize SW view of the RX ring */
1642        priv->num_rx_bds = priv->num_rx_desc_words / WORDS_PER_DESC;
1643        priv->rx_bds = priv->base + SYS_PORT_RDMA_OFFSET;
1644        priv->rx_c_index = 0;
1645        priv->rx_read_ptr = 0;
1646        priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct bcm_sysport_cb),
1647                                GFP_KERNEL);
1648        if (!priv->rx_cbs) {
1649                netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
1650                return -ENOMEM;
1651        }
1652
1653        for (i = 0; i < priv->num_rx_bds; i++) {
1654                cb = priv->rx_cbs + i;
1655                cb->bd_addr = priv->rx_bds + i * DESC_SIZE;
1656        }
1657
1658        ret = bcm_sysport_alloc_rx_bufs(priv);
1659        if (ret) {
1660                netif_err(priv, hw, priv->netdev, "SKB allocation failed\n");
1661                return ret;
1662        }
1663
1664        /* Initialize HW, ensure RDMA is disabled */
1665        reg = rdma_readl(priv, RDMA_STATUS);
1666        if (!(reg & RDMA_DISABLED))
1667                rdma_enable_set(priv, 0);
1668
1669        rdma_writel(priv, 0, RDMA_WRITE_PTR_LO);
1670        rdma_writel(priv, 0, RDMA_WRITE_PTR_HI);
1671        rdma_writel(priv, 0, RDMA_PROD_INDEX);
1672        rdma_writel(priv, 0, RDMA_CONS_INDEX);
1673        rdma_writel(priv, priv->num_rx_bds << RDMA_RING_SIZE_SHIFT |
1674                          RX_BUF_LENGTH, RDMA_RING_BUF_SIZE);
1675        /* Operate the queue in ring mode */
1676        rdma_writel(priv, 0, RDMA_START_ADDR_HI);
1677        rdma_writel(priv, 0, RDMA_START_ADDR_LO);
1678        rdma_writel(priv, 0, RDMA_END_ADDR_HI);
1679        rdma_writel(priv, priv->num_rx_desc_words - 1, RDMA_END_ADDR_LO);
1680
1681        netif_dbg(priv, hw, priv->netdev,
1682                  "RDMA cfg, num_rx_bds=%d, rx_bds=%p\n",
1683                  priv->num_rx_bds, priv->rx_bds);
1684
1685        return 0;
1686}
1687
1688static void bcm_sysport_fini_rx_ring(struct bcm_sysport_priv *priv)
1689{
1690        struct bcm_sysport_cb *cb;
1691        unsigned int i;
1692        u32 reg;
1693
1694        /* Caller should ensure RDMA is disabled */
1695        reg = rdma_readl(priv, RDMA_STATUS);
1696        if (!(reg & RDMA_DISABLED))
1697                netdev_warn(priv->netdev, "RDMA not stopped!\n");
1698
1699        for (i = 0; i < priv->num_rx_bds; i++) {
1700                cb = &priv->rx_cbs[i];
1701                if (dma_unmap_addr(cb, dma_addr))
1702                        dma_unmap_single(&priv->pdev->dev,
1703                                         dma_unmap_addr(cb, dma_addr),
1704                                         RX_BUF_LENGTH, DMA_FROM_DEVICE);
1705                bcm_sysport_free_cb(cb);
1706        }
1707
1708        kfree(priv->rx_cbs);
1709        priv->rx_cbs = NULL;
1710
1711        netif_dbg(priv, hw, priv->netdev, "RDMA fini done\n");
1712}
1713
1714static void bcm_sysport_set_rx_mode(struct net_device *dev)
1715{
1716        struct bcm_sysport_priv *priv = netdev_priv(dev);
1717        u32 reg;
1718
1719        if (priv->is_lite)
1720                return;
1721
1722        reg = umac_readl(priv, UMAC_CMD);
1723        if (dev->flags & IFF_PROMISC)
1724                reg |= CMD_PROMISC;
1725        else
1726                reg &= ~CMD_PROMISC;
1727        umac_writel(priv, reg, UMAC_CMD);
1728
1729        /* No support for ALLMULTI */
1730        if (dev->flags & IFF_ALLMULTI)
1731                return;
1732}
1733
1734static inline void umac_enable_set(struct bcm_sysport_priv *priv,
1735                                   u32 mask, unsigned int enable)
1736{
1737        u32 reg;
1738
1739        if (!priv->is_lite) {
1740                reg = umac_readl(priv, UMAC_CMD);
1741                if (enable)
1742                        reg |= mask;
1743                else
1744                        reg &= ~mask;
1745                umac_writel(priv, reg, UMAC_CMD);
1746        } else {
1747                reg = gib_readl(priv, GIB_CONTROL);
1748                if (enable)
1749                        reg |= mask;
1750                else
1751                        reg &= ~mask;
1752                gib_writel(priv, reg, GIB_CONTROL);
1753        }
1754
1755        /* UniMAC stops on a packet boundary, wait for a full-sized packet
1756         * to be processed (1 msec).
1757         */
1758        if (enable == 0)
1759                usleep_range(1000, 2000);
1760}
1761
1762static inline void umac_reset(struct bcm_sysport_priv *priv)
1763{
1764        u32 reg;
1765
1766        if (priv->is_lite)
1767                return;
1768
1769        reg = umac_readl(priv, UMAC_CMD);
1770        reg |= CMD_SW_RESET;
1771        umac_writel(priv, reg, UMAC_CMD);
1772        udelay(10);
1773        reg = umac_readl(priv, UMAC_CMD);
1774        reg &= ~CMD_SW_RESET;
1775        umac_writel(priv, reg, UMAC_CMD);
1776}
1777
1778static void umac_set_hw_addr(struct bcm_sysport_priv *priv,
1779                             unsigned char *addr)
1780{
1781        u32 mac0 = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) |
1782                    addr[3];
1783        u32 mac1 = (addr[4] << 8) | addr[5];
1784
1785        if (!priv->is_lite) {
1786                umac_writel(priv, mac0, UMAC_MAC0);
1787                umac_writel(priv, mac1, UMAC_MAC1);
1788        } else {
1789                gib_writel(priv, mac0, GIB_MAC0);
1790                gib_writel(priv, mac1, GIB_MAC1);
1791        }
1792}
1793
1794static void topctrl_flush(struct bcm_sysport_priv *priv)
1795{
1796        topctrl_writel(priv, RX_FLUSH, RX_FLUSH_CNTL);
1797        topctrl_writel(priv, TX_FLUSH, TX_FLUSH_CNTL);
1798        mdelay(1);
1799        topctrl_writel(priv, 0, RX_FLUSH_CNTL);
1800        topctrl_writel(priv, 0, TX_FLUSH_CNTL);
1801}
1802
1803static int bcm_sysport_change_mac(struct net_device *dev, void *p)
1804{
1805        struct bcm_sysport_priv *priv = netdev_priv(dev);
1806        struct sockaddr *addr = p;
1807
1808        if (!is_valid_ether_addr(addr->sa_data))
1809                return -EINVAL;
1810
1811        memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1812
1813        /* interface is disabled, changes to MAC will be reflected on next
1814         * open call
1815         */
1816        if (!netif_running(dev))
1817                return 0;
1818
1819        umac_set_hw_addr(priv, dev->dev_addr);
1820
1821        return 0;
1822}
1823
1824static void bcm_sysport_get_stats64(struct net_device *dev,
1825                                    struct rtnl_link_stats64 *stats)
1826{
1827        struct bcm_sysport_priv *priv = netdev_priv(dev);
1828        struct bcm_sysport_stats64 *stats64 = &priv->stats64;
1829        unsigned int start;
1830
1831        netdev_stats_to_stats64(stats, &dev->stats);
1832
1833        bcm_sysport_update_tx_stats(priv, &stats->tx_bytes,
1834                                    &stats->tx_packets);
1835
1836        do {
1837                start = u64_stats_fetch_begin_irq(&priv->syncp);
1838                stats->rx_packets = stats64->rx_packets;
1839                stats->rx_bytes = stats64->rx_bytes;
1840        } while (u64_stats_fetch_retry_irq(&priv->syncp, start));
1841}
1842
1843static void bcm_sysport_netif_start(struct net_device *dev)
1844{
1845        struct bcm_sysport_priv *priv = netdev_priv(dev);
1846
1847        /* Enable NAPI */
1848        bcm_sysport_init_dim(priv, bcm_sysport_dim_work);
1849        bcm_sysport_init_rx_coalesce(priv);
1850        napi_enable(&priv->napi);
1851
1852        /* Enable RX interrupt and TX ring full interrupt */
1853        intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
1854
1855        phy_start(dev->phydev);
1856
1857        /* Enable TX interrupts for the TXQs */
1858        if (!priv->is_lite)
1859                intrl2_1_mask_clear(priv, 0xffffffff);
1860        else
1861                intrl2_0_mask_clear(priv, INTRL2_0_TDMA_MBDONE_MASK);
1862}
1863
1864static void rbuf_init(struct bcm_sysport_priv *priv)
1865{
1866        u32 reg;
1867
1868        reg = rbuf_readl(priv, RBUF_CONTROL);
1869        reg |= RBUF_4B_ALGN | RBUF_RSB_EN;
1870        /* Set a correct RSB format on SYSTEMPORT Lite */
1871        if (priv->is_lite)
1872                reg &= ~RBUF_RSB_SWAP1;
1873
1874        /* Set a correct RSB format based on host endian */
1875        if (!IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
1876                reg |= RBUF_RSB_SWAP0;
1877        else
1878                reg &= ~RBUF_RSB_SWAP0;
1879        rbuf_writel(priv, reg, RBUF_CONTROL);
1880}
1881
1882static inline void bcm_sysport_mask_all_intrs(struct bcm_sysport_priv *priv)
1883{
1884        intrl2_0_mask_set(priv, 0xffffffff);
1885        intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1886        if (!priv->is_lite) {
1887                intrl2_1_mask_set(priv, 0xffffffff);
1888                intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1889        }
1890}
1891
1892static inline void gib_set_pad_extension(struct bcm_sysport_priv *priv)
1893{
1894        u32 reg;
1895
1896        reg = gib_readl(priv, GIB_CONTROL);
1897        /* Include Broadcom tag in pad extension and fix up IPG_LENGTH */
1898        if (netdev_uses_dsa(priv->netdev)) {
1899                reg &= ~(GIB_PAD_EXTENSION_MASK << GIB_PAD_EXTENSION_SHIFT);
1900                reg |= ENET_BRCM_TAG_LEN << GIB_PAD_EXTENSION_SHIFT;
1901        }
1902        reg &= ~(GIB_IPG_LEN_MASK << GIB_IPG_LEN_SHIFT);
1903        reg |= 12 << GIB_IPG_LEN_SHIFT;
1904        gib_writel(priv, reg, GIB_CONTROL);
1905}
1906
1907static int bcm_sysport_open(struct net_device *dev)
1908{
1909        struct bcm_sysport_priv *priv = netdev_priv(dev);
1910        struct phy_device *phydev;
1911        unsigned int i;
1912        int ret;
1913
1914        /* Reset UniMAC */
1915        umac_reset(priv);
1916
1917        /* Flush TX and RX FIFOs at TOPCTRL level */
1918        topctrl_flush(priv);
1919
1920        /* Disable the UniMAC RX/TX */
1921        umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 0);
1922
1923        /* Enable RBUF 2bytes alignment and Receive Status Block */
1924        rbuf_init(priv);
1925
1926        /* Set maximum frame length */
1927        if (!priv->is_lite)
1928                umac_writel(priv, UMAC_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
1929        else
1930                gib_set_pad_extension(priv);
1931
1932        /* Apply features again in case we changed them while interface was
1933         * down
1934         */
1935        bcm_sysport_set_features(dev, dev->features);
1936
1937        /* Set MAC address */
1938        umac_set_hw_addr(priv, dev->dev_addr);
1939
1940        phydev = of_phy_connect(dev, priv->phy_dn, bcm_sysport_adj_link,
1941                                0, priv->phy_interface);
1942        if (!phydev) {
1943                netdev_err(dev, "could not attach to PHY\n");
1944                return -ENODEV;
1945        }
1946
1947        /* Reset house keeping link status */
1948        priv->old_duplex = -1;
1949        priv->old_link = -1;
1950        priv->old_pause = -1;
1951
1952        /* mask all interrupts and request them */
1953        bcm_sysport_mask_all_intrs(priv);
1954
1955        ret = request_irq(priv->irq0, bcm_sysport_rx_isr, 0, dev->name, dev);
1956        if (ret) {
1957                netdev_err(dev, "failed to request RX interrupt\n");
1958                goto out_phy_disconnect;
1959        }
1960
1961        if (!priv->is_lite) {
1962                ret = request_irq(priv->irq1, bcm_sysport_tx_isr, 0,
1963                                  dev->name, dev);
1964                if (ret) {
1965                        netdev_err(dev, "failed to request TX interrupt\n");
1966                        goto out_free_irq0;
1967                }
1968        }
1969
1970        /* Initialize both hardware and software ring */
1971        for (i = 0; i < dev->num_tx_queues; i++) {
1972                ret = bcm_sysport_init_tx_ring(priv, i);
1973                if (ret) {
1974                        netdev_err(dev, "failed to initialize TX ring %d\n",
1975                                   i);
1976                        goto out_free_tx_ring;
1977                }
1978        }
1979
1980        /* Initialize linked-list */
1981        tdma_writel(priv, TDMA_LL_RAM_INIT_BUSY, TDMA_STATUS);
1982
1983        /* Initialize RX ring */
1984        ret = bcm_sysport_init_rx_ring(priv);
1985        if (ret) {
1986                netdev_err(dev, "failed to initialize RX ring\n");
1987                goto out_free_rx_ring;
1988        }
1989
1990        /* Turn on RDMA */
1991        ret = rdma_enable_set(priv, 1);
1992        if (ret)
1993                goto out_free_rx_ring;
1994
1995        /* Turn on TDMA */
1996        ret = tdma_enable_set(priv, 1);
1997        if (ret)
1998                goto out_clear_rx_int;
1999
2000        /* Turn on UniMAC TX/RX */
2001        umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 1);
2002
2003        bcm_sysport_netif_start(dev);
2004
2005        netif_tx_start_all_queues(dev);
2006
2007        return 0;
2008
2009out_clear_rx_int:
2010        intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
2011out_free_rx_ring:
2012        bcm_sysport_fini_rx_ring(priv);
2013out_free_tx_ring:
2014        for (i = 0; i < dev->num_tx_queues; i++)
2015                bcm_sysport_fini_tx_ring(priv, i);
2016        if (!priv->is_lite)
2017                free_irq(priv->irq1, dev);
2018out_free_irq0:
2019        free_irq(priv->irq0, dev);
2020out_phy_disconnect:
2021        phy_disconnect(phydev);
2022        return ret;
2023}
2024
2025static void bcm_sysport_netif_stop(struct net_device *dev)
2026{
2027        struct bcm_sysport_priv *priv = netdev_priv(dev);
2028
2029        /* stop all software from updating hardware */
2030        netif_tx_disable(dev);
2031        napi_disable(&priv->napi);
2032        cancel_work_sync(&priv->dim.dim.work);
2033        phy_stop(dev->phydev);
2034
2035        /* mask all interrupts */
2036        bcm_sysport_mask_all_intrs(priv);
2037}
2038
2039static int bcm_sysport_stop(struct net_device *dev)
2040{
2041        struct bcm_sysport_priv *priv = netdev_priv(dev);
2042        unsigned int i;
2043        int ret;
2044
2045        bcm_sysport_netif_stop(dev);
2046
2047        /* Disable UniMAC RX */
2048        umac_enable_set(priv, CMD_RX_EN, 0);
2049
2050        ret = tdma_enable_set(priv, 0);
2051        if (ret) {
2052                netdev_err(dev, "timeout disabling RDMA\n");
2053                return ret;
2054        }
2055
2056        /* Wait for a maximum packet size to be drained */
2057        usleep_range(2000, 3000);
2058
2059        ret = rdma_enable_set(priv, 0);
2060        if (ret) {
2061                netdev_err(dev, "timeout disabling TDMA\n");
2062                return ret;
2063        }
2064
2065        /* Disable UniMAC TX */
2066        umac_enable_set(priv, CMD_TX_EN, 0);
2067
2068        /* Free RX/TX rings SW structures */
2069        for (i = 0; i < dev->num_tx_queues; i++)
2070                bcm_sysport_fini_tx_ring(priv, i);
2071        bcm_sysport_fini_rx_ring(priv);
2072
2073        free_irq(priv->irq0, dev);
2074        if (!priv->is_lite)
2075                free_irq(priv->irq1, dev);
2076
2077        /* Disconnect from PHY */
2078        phy_disconnect(dev->phydev);
2079
2080        return 0;
2081}
2082
2083static int bcm_sysport_rule_find(struct bcm_sysport_priv *priv,
2084                                 u64 location)
2085{
2086        unsigned int index;
2087        u32 reg;
2088
2089        for_each_set_bit(index, priv->filters, RXCHK_BRCM_TAG_MAX) {
2090                reg = rxchk_readl(priv, RXCHK_BRCM_TAG(index));
2091                reg >>= RXCHK_BRCM_TAG_CID_SHIFT;
2092                reg &= RXCHK_BRCM_TAG_CID_MASK;
2093                if (reg == location)
2094                        return index;
2095        }
2096
2097        return -EINVAL;
2098}
2099
2100static int bcm_sysport_rule_get(struct bcm_sysport_priv *priv,
2101                                struct ethtool_rxnfc *nfc)
2102{
2103        int index;
2104
2105        /* This is not a rule that we know about */
2106        index = bcm_sysport_rule_find(priv, nfc->fs.location);
2107        if (index < 0)
2108                return -EOPNOTSUPP;
2109
2110        nfc->fs.ring_cookie = RX_CLS_FLOW_WAKE;
2111
2112        return 0;
2113}
2114
2115static int bcm_sysport_rule_set(struct bcm_sysport_priv *priv,
2116                                struct ethtool_rxnfc *nfc)
2117{
2118        unsigned int index;
2119        u32 reg;
2120
2121        /* We cannot match locations greater than what the classification ID
2122         * permits (256 entries)
2123         */
2124        if (nfc->fs.location > RXCHK_BRCM_TAG_CID_MASK)
2125                return -E2BIG;
2126
2127        /* We cannot support flows that are not destined for a wake-up */
2128        if (nfc->fs.ring_cookie != RX_CLS_FLOW_WAKE)
2129                return -EOPNOTSUPP;
2130
2131        /* All filters are already in use, we cannot match more rules */
2132        if (bitmap_weight(priv->filters, RXCHK_BRCM_TAG_MAX) ==
2133            RXCHK_BRCM_TAG_MAX)
2134                return -ENOSPC;
2135
2136        index = find_first_zero_bit(priv->filters, RXCHK_BRCM_TAG_MAX);
2137        if (index >= RXCHK_BRCM_TAG_MAX)
2138                return -ENOSPC;
2139
2140        /* Location is the classification ID, and index is the position
2141         * within one of our 8 possible filters to be programmed
2142         */
2143        reg = rxchk_readl(priv, RXCHK_BRCM_TAG(index));
2144        reg &= ~(RXCHK_BRCM_TAG_CID_MASK << RXCHK_BRCM_TAG_CID_SHIFT);
2145        reg |= nfc->fs.location << RXCHK_BRCM_TAG_CID_SHIFT;
2146        rxchk_writel(priv, reg, RXCHK_BRCM_TAG(index));
2147        rxchk_writel(priv, 0xff00ffff, RXCHK_BRCM_TAG_MASK(index));
2148
2149        priv->filters_loc[index] = nfc->fs.location;
2150        set_bit(index, priv->filters);
2151
2152        return 0;
2153}
2154
2155static int bcm_sysport_rule_del(struct bcm_sysport_priv *priv,
2156                                u64 location)
2157{
2158        int index;
2159
2160        /* This is not a rule that we know about */
2161        index = bcm_sysport_rule_find(priv, location);
2162        if (index < 0)
2163                return -EOPNOTSUPP;
2164
2165        /* No need to disable this filter if it was enabled, this will
2166         * be taken care of during suspend time by bcm_sysport_suspend_to_wol
2167         */
2168        clear_bit(index, priv->filters);
2169        priv->filters_loc[index] = 0;
2170
2171        return 0;
2172}
2173
2174static int bcm_sysport_get_rxnfc(struct net_device *dev,
2175                                 struct ethtool_rxnfc *nfc, u32 *rule_locs)
2176{
2177        struct bcm_sysport_priv *priv = netdev_priv(dev);
2178        int ret = -EOPNOTSUPP;
2179
2180        switch (nfc->cmd) {
2181        case ETHTOOL_GRXCLSRULE:
2182                ret = bcm_sysport_rule_get(priv, nfc);
2183                break;
2184        default:
2185                break;
2186        }
2187
2188        return ret;
2189}
2190
2191static int bcm_sysport_set_rxnfc(struct net_device *dev,
2192                                 struct ethtool_rxnfc *nfc)
2193{
2194        struct bcm_sysport_priv *priv = netdev_priv(dev);
2195        int ret = -EOPNOTSUPP;
2196
2197        switch (nfc->cmd) {
2198        case ETHTOOL_SRXCLSRLINS:
2199                ret = bcm_sysport_rule_set(priv, nfc);
2200                break;
2201        case ETHTOOL_SRXCLSRLDEL:
2202                ret = bcm_sysport_rule_del(priv, nfc->fs.location);
2203                break;
2204        default:
2205                break;
2206        }
2207
2208        return ret;
2209}
2210
2211static const struct ethtool_ops bcm_sysport_ethtool_ops = {
2212        .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
2213                                     ETHTOOL_COALESCE_MAX_FRAMES |
2214                                     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
2215        .get_drvinfo            = bcm_sysport_get_drvinfo,
2216        .get_msglevel           = bcm_sysport_get_msglvl,
2217        .set_msglevel           = bcm_sysport_set_msglvl,
2218        .get_link               = ethtool_op_get_link,
2219        .get_strings            = bcm_sysport_get_strings,
2220        .get_ethtool_stats      = bcm_sysport_get_stats,
2221        .get_sset_count         = bcm_sysport_get_sset_count,
2222        .get_wol                = bcm_sysport_get_wol,
2223        .set_wol                = bcm_sysport_set_wol,
2224        .get_coalesce           = bcm_sysport_get_coalesce,
2225        .set_coalesce           = bcm_sysport_set_coalesce,
2226        .get_link_ksettings     = phy_ethtool_get_link_ksettings,
2227        .set_link_ksettings     = phy_ethtool_set_link_ksettings,
2228        .get_rxnfc              = bcm_sysport_get_rxnfc,
2229        .set_rxnfc              = bcm_sysport_set_rxnfc,
2230};
2231
2232static u16 bcm_sysport_select_queue(struct net_device *dev, struct sk_buff *skb,
2233                                    struct net_device *sb_dev)
2234{
2235        struct bcm_sysport_priv *priv = netdev_priv(dev);
2236        u16 queue = skb_get_queue_mapping(skb);
2237        struct bcm_sysport_tx_ring *tx_ring;
2238        unsigned int q, port;
2239
2240        if (!netdev_uses_dsa(dev))
2241                return netdev_pick_tx(dev, skb, NULL);
2242
2243        /* DSA tagging layer will have configured the correct queue */
2244        q = BRCM_TAG_GET_QUEUE(queue);
2245        port = BRCM_TAG_GET_PORT(queue);
2246        tx_ring = priv->ring_map[q + port * priv->per_port_num_tx_queues];
2247
2248        if (unlikely(!tx_ring))
2249                return netdev_pick_tx(dev, skb, NULL);
2250
2251        return tx_ring->index;
2252}
2253
2254static const struct net_device_ops bcm_sysport_netdev_ops = {
2255        .ndo_start_xmit         = bcm_sysport_xmit,
2256        .ndo_tx_timeout         = bcm_sysport_tx_timeout,
2257        .ndo_open               = bcm_sysport_open,
2258        .ndo_stop               = bcm_sysport_stop,
2259        .ndo_set_features       = bcm_sysport_set_features,
2260        .ndo_set_rx_mode        = bcm_sysport_set_rx_mode,
2261        .ndo_set_mac_address    = bcm_sysport_change_mac,
2262#ifdef CONFIG_NET_POLL_CONTROLLER
2263        .ndo_poll_controller    = bcm_sysport_poll_controller,
2264#endif
2265        .ndo_get_stats64        = bcm_sysport_get_stats64,
2266        .ndo_select_queue       = bcm_sysport_select_queue,
2267};
2268
2269static int bcm_sysport_map_queues(struct notifier_block *nb,
2270                                  struct dsa_notifier_register_info *info)
2271{
2272        struct bcm_sysport_tx_ring *ring;
2273        struct bcm_sysport_priv *priv;
2274        struct net_device *slave_dev;
2275        unsigned int num_tx_queues;
2276        unsigned int q, qp, port;
2277        struct net_device *dev;
2278
2279        priv = container_of(nb, struct bcm_sysport_priv, dsa_notifier);
2280        if (priv->netdev != info->master)
2281                return 0;
2282
2283        dev = info->master;
2284
2285        /* We can't be setting up queue inspection for non directly attached
2286         * switches
2287         */
2288        if (info->switch_number)
2289                return 0;
2290
2291        if (dev->netdev_ops != &bcm_sysport_netdev_ops)
2292                return 0;
2293
2294        port = info->port_number;
2295        slave_dev = info->info.dev;
2296
2297        /* On SYSTEMPORT Lite we have twice as less queues, so we cannot do a
2298         * 1:1 mapping, we can only do a 2:1 mapping. By reducing the number of
2299         * per-port (slave_dev) network devices queue, we achieve just that.
2300         * This need to happen now before any slave network device is used such
2301         * it accurately reflects the number of real TX queues.
2302         */
2303        if (priv->is_lite)
2304                netif_set_real_num_tx_queues(slave_dev,
2305                                             slave_dev->num_tx_queues / 2);
2306
2307        num_tx_queues = slave_dev->real_num_tx_queues;
2308
2309        if (priv->per_port_num_tx_queues &&
2310            priv->per_port_num_tx_queues != num_tx_queues)
2311                netdev_warn(slave_dev, "asymmetric number of per-port queues\n");
2312
2313        priv->per_port_num_tx_queues = num_tx_queues;
2314
2315        for (q = 0, qp = 0; q < dev->num_tx_queues && qp < num_tx_queues;
2316             q++) {
2317                ring = &priv->tx_rings[q];
2318
2319                if (ring->inspect)
2320                        continue;
2321
2322                /* Just remember the mapping actual programming done
2323                 * during bcm_sysport_init_tx_ring
2324                 */
2325                ring->switch_queue = qp;
2326                ring->switch_port = port;
2327                ring->inspect = true;
2328                priv->ring_map[qp + port * num_tx_queues] = ring;
2329                qp++;
2330        }
2331
2332        return 0;
2333}
2334
2335static int bcm_sysport_unmap_queues(struct notifier_block *nb,
2336                                    struct dsa_notifier_register_info *info)
2337{
2338        struct bcm_sysport_tx_ring *ring;
2339        struct bcm_sysport_priv *priv;
2340        struct net_device *slave_dev;
2341        unsigned int num_tx_queues;
2342        struct net_device *dev;
2343        unsigned int q, qp, port;
2344
2345        priv = container_of(nb, struct bcm_sysport_priv, dsa_notifier);
2346        if (priv->netdev != info->master)
2347                return 0;
2348
2349        dev = info->master;
2350
2351        if (dev->netdev_ops != &bcm_sysport_netdev_ops)
2352                return 0;
2353
2354        port = info->port_number;
2355        slave_dev = info->info.dev;
2356
2357        num_tx_queues = slave_dev->real_num_tx_queues;
2358
2359        for (q = 0; q < dev->num_tx_queues; q++) {
2360                ring = &priv->tx_rings[q];
2361
2362                if (ring->switch_port != port)
2363                        continue;
2364
2365                if (!ring->inspect)
2366                        continue;
2367
2368                ring->inspect = false;
2369                qp = ring->switch_queue;
2370                priv->ring_map[qp + port * num_tx_queues] = NULL;
2371        }
2372
2373        return 0;
2374}
2375
2376static int bcm_sysport_dsa_notifier(struct notifier_block *nb,
2377                                    unsigned long event, void *ptr)
2378{
2379        int ret = NOTIFY_DONE;
2380
2381        switch (event) {
2382        case DSA_PORT_REGISTER:
2383                ret = bcm_sysport_map_queues(nb, ptr);
2384                break;
2385        case DSA_PORT_UNREGISTER:
2386                ret = bcm_sysport_unmap_queues(nb, ptr);
2387                break;
2388        }
2389
2390        return notifier_from_errno(ret);
2391}
2392
2393#define REV_FMT "v%2x.%02x"
2394
2395static const struct bcm_sysport_hw_params bcm_sysport_params[] = {
2396        [SYSTEMPORT] = {
2397                .is_lite = false,
2398                .num_rx_desc_words = SP_NUM_HW_RX_DESC_WORDS,
2399        },
2400        [SYSTEMPORT_LITE] = {
2401                .is_lite = true,
2402                .num_rx_desc_words = SP_LT_NUM_HW_RX_DESC_WORDS,
2403        },
2404};
2405
2406static const struct of_device_id bcm_sysport_of_match[] = {
2407        { .compatible = "brcm,systemportlite-v1.00",
2408          .data = &bcm_sysport_params[SYSTEMPORT_LITE] },
2409        { .compatible = "brcm,systemport-v1.00",
2410          .data = &bcm_sysport_params[SYSTEMPORT] },
2411        { .compatible = "brcm,systemport",
2412          .data = &bcm_sysport_params[SYSTEMPORT] },
2413        { /* sentinel */ }
2414};
2415MODULE_DEVICE_TABLE(of, bcm_sysport_of_match);
2416
2417static int bcm_sysport_probe(struct platform_device *pdev)
2418{
2419        const struct bcm_sysport_hw_params *params;
2420        const struct of_device_id *of_id = NULL;
2421        struct bcm_sysport_priv *priv;
2422        struct device_node *dn;
2423        struct net_device *dev;
2424        const void *macaddr;
2425        u32 txq, rxq;
2426        int ret;
2427
2428        dn = pdev->dev.of_node;
2429        of_id = of_match_node(bcm_sysport_of_match, dn);
2430        if (!of_id || !of_id->data)
2431                return -EINVAL;
2432
2433        ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
2434        if (ret)
2435                ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2436        if (ret) {
2437                dev_err(&pdev->dev, "unable to set DMA mask: %d\n", ret);
2438                return ret;
2439        }
2440
2441        /* Fairly quickly we need to know the type of adapter we have */
2442        params = of_id->data;
2443
2444        /* Read the Transmit/Receive Queue properties */
2445        if (of_property_read_u32(dn, "systemport,num-txq", &txq))
2446                txq = TDMA_NUM_RINGS;
2447        if (of_property_read_u32(dn, "systemport,num-rxq", &rxq))
2448                rxq = 1;
2449
2450        /* Sanity check the number of transmit queues */
2451        if (!txq || txq > TDMA_NUM_RINGS)
2452                return -EINVAL;
2453
2454        dev = alloc_etherdev_mqs(sizeof(*priv), txq, rxq);
2455        if (!dev)
2456                return -ENOMEM;
2457
2458        /* Initialize private members */
2459        priv = netdev_priv(dev);
2460
2461        /* Allocate number of TX rings */
2462        priv->tx_rings = devm_kcalloc(&pdev->dev, txq,
2463                                      sizeof(struct bcm_sysport_tx_ring),
2464                                      GFP_KERNEL);
2465        if (!priv->tx_rings)
2466                return -ENOMEM;
2467
2468        priv->is_lite = params->is_lite;
2469        priv->num_rx_desc_words = params->num_rx_desc_words;
2470
2471        priv->irq0 = platform_get_irq(pdev, 0);
2472        if (!priv->is_lite) {
2473                priv->irq1 = platform_get_irq(pdev, 1);
2474                priv->wol_irq = platform_get_irq(pdev, 2);
2475        } else {
2476                priv->wol_irq = platform_get_irq(pdev, 1);
2477        }
2478        if (priv->irq0 <= 0 || (priv->irq1 <= 0 && !priv->is_lite)) {
2479                ret = -EINVAL;
2480                goto err_free_netdev;
2481        }
2482
2483        priv->base = devm_platform_ioremap_resource(pdev, 0);
2484        if (IS_ERR(priv->base)) {
2485                ret = PTR_ERR(priv->base);
2486                goto err_free_netdev;
2487        }
2488
2489        priv->netdev = dev;
2490        priv->pdev = pdev;
2491
2492        ret = of_get_phy_mode(dn, &priv->phy_interface);
2493        /* Default to GMII interface mode */
2494        if (ret)
2495                priv->phy_interface = PHY_INTERFACE_MODE_GMII;
2496
2497        /* In the case of a fixed PHY, the DT node associated
2498         * to the PHY is the Ethernet MAC DT node.
2499         */
2500        if (of_phy_is_fixed_link(dn)) {
2501                ret = of_phy_register_fixed_link(dn);
2502                if (ret) {
2503                        dev_err(&pdev->dev, "failed to register fixed PHY\n");
2504                        goto err_free_netdev;
2505                }
2506
2507                priv->phy_dn = dn;
2508        }
2509
2510        /* Initialize netdevice members */
2511        macaddr = of_get_mac_address(dn);
2512        if (IS_ERR(macaddr)) {
2513                dev_warn(&pdev->dev, "using random Ethernet MAC\n");
2514                eth_hw_addr_random(dev);
2515        } else {
2516                ether_addr_copy(dev->dev_addr, macaddr);
2517        }
2518
2519        SET_NETDEV_DEV(dev, &pdev->dev);
2520        dev_set_drvdata(&pdev->dev, dev);
2521        dev->ethtool_ops = &bcm_sysport_ethtool_ops;
2522        dev->netdev_ops = &bcm_sysport_netdev_ops;
2523        netif_napi_add(dev, &priv->napi, bcm_sysport_poll, 64);
2524
2525        dev->features |= NETIF_F_RXCSUM | NETIF_F_HIGHDMA |
2526                         NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2527        dev->hw_features |= dev->features;
2528        dev->vlan_features |= dev->features;
2529
2530        /* Request the WOL interrupt and advertise suspend if available */
2531        priv->wol_irq_disabled = 1;
2532        ret = devm_request_irq(&pdev->dev, priv->wol_irq,
2533                               bcm_sysport_wol_isr, 0, dev->name, priv);
2534        if (!ret)
2535                device_set_wakeup_capable(&pdev->dev, 1);
2536
2537        /* Set the needed headroom once and for all */
2538        BUILD_BUG_ON(sizeof(struct bcm_tsb) != 8);
2539        dev->needed_headroom += sizeof(struct bcm_tsb);
2540
2541        /* libphy will adjust the link state accordingly */
2542        netif_carrier_off(dev);
2543
2544        priv->rx_max_coalesced_frames = 1;
2545        u64_stats_init(&priv->syncp);
2546
2547        priv->dsa_notifier.notifier_call = bcm_sysport_dsa_notifier;
2548
2549        ret = register_dsa_notifier(&priv->dsa_notifier);
2550        if (ret) {
2551                dev_err(&pdev->dev, "failed to register DSA notifier\n");
2552                goto err_deregister_fixed_link;
2553        }
2554
2555        ret = register_netdev(dev);
2556        if (ret) {
2557                dev_err(&pdev->dev, "failed to register net_device\n");
2558                goto err_deregister_notifier;
2559        }
2560
2561        priv->rev = topctrl_readl(priv, REV_CNTL) & REV_MASK;
2562        dev_info(&pdev->dev,
2563                 "Broadcom SYSTEMPORT%s " REV_FMT
2564                 " (irqs: %d, %d, TXQs: %d, RXQs: %d)\n",
2565                 priv->is_lite ? " Lite" : "",
2566                 (priv->rev >> 8) & 0xff, priv->rev & 0xff,
2567                 priv->irq0, priv->irq1, txq, rxq);
2568
2569        return 0;
2570
2571err_deregister_notifier:
2572        unregister_dsa_notifier(&priv->dsa_notifier);
2573err_deregister_fixed_link:
2574        if (of_phy_is_fixed_link(dn))
2575                of_phy_deregister_fixed_link(dn);
2576err_free_netdev:
2577        free_netdev(dev);
2578        return ret;
2579}
2580
2581static int bcm_sysport_remove(struct platform_device *pdev)
2582{
2583        struct net_device *dev = dev_get_drvdata(&pdev->dev);
2584        struct bcm_sysport_priv *priv = netdev_priv(dev);
2585        struct device_node *dn = pdev->dev.of_node;
2586
2587        /* Not much to do, ndo_close has been called
2588         * and we use managed allocations
2589         */
2590        unregister_dsa_notifier(&priv->dsa_notifier);
2591        unregister_netdev(dev);
2592        if (of_phy_is_fixed_link(dn))
2593                of_phy_deregister_fixed_link(dn);
2594        free_netdev(dev);
2595        dev_set_drvdata(&pdev->dev, NULL);
2596
2597        return 0;
2598}
2599
2600static int bcm_sysport_suspend_to_wol(struct bcm_sysport_priv *priv)
2601{
2602        struct net_device *ndev = priv->netdev;
2603        unsigned int timeout = 1000;
2604        unsigned int index, i = 0;
2605        u32 reg;
2606
2607        reg = umac_readl(priv, UMAC_MPD_CTRL);
2608        if (priv->wolopts & (WAKE_MAGIC | WAKE_MAGICSECURE))
2609                reg |= MPD_EN;
2610        reg &= ~PSW_EN;
2611        if (priv->wolopts & WAKE_MAGICSECURE) {
2612                /* Program the SecureOn password */
2613                umac_writel(priv, get_unaligned_be16(&priv->sopass[0]),
2614                            UMAC_PSW_MS);
2615                umac_writel(priv, get_unaligned_be32(&priv->sopass[2]),
2616                            UMAC_PSW_LS);
2617                reg |= PSW_EN;
2618        }
2619        umac_writel(priv, reg, UMAC_MPD_CTRL);
2620
2621        if (priv->wolopts & WAKE_FILTER) {
2622                /* Turn on ACPI matching to steal packets from RBUF */
2623                reg = rbuf_readl(priv, RBUF_CONTROL);
2624                if (priv->is_lite)
2625                        reg |= RBUF_ACPI_EN_LITE;
2626                else
2627                        reg |= RBUF_ACPI_EN;
2628                rbuf_writel(priv, reg, RBUF_CONTROL);
2629
2630                /* Enable RXCHK, active filters and Broadcom tag matching */
2631                reg = rxchk_readl(priv, RXCHK_CONTROL);
2632                reg &= ~(RXCHK_BRCM_TAG_MATCH_MASK <<
2633                         RXCHK_BRCM_TAG_MATCH_SHIFT);
2634                for_each_set_bit(index, priv->filters, RXCHK_BRCM_TAG_MAX) {
2635                        reg |= BIT(RXCHK_BRCM_TAG_MATCH_SHIFT + i);
2636                        i++;
2637                }
2638                reg |= RXCHK_EN | RXCHK_BRCM_TAG_EN;
2639                rxchk_writel(priv, reg, RXCHK_CONTROL);
2640        }
2641
2642        /* Make sure RBUF entered WoL mode as result */
2643        do {
2644                reg = rbuf_readl(priv, RBUF_STATUS);
2645                if (reg & RBUF_WOL_MODE)
2646                        break;
2647
2648                udelay(10);
2649        } while (timeout-- > 0);
2650
2651        /* Do not leave the UniMAC RBUF matching only MPD packets */
2652        if (!timeout) {
2653                mpd_enable_set(priv, false);
2654                netif_err(priv, wol, ndev, "failed to enter WOL mode\n");
2655                return -ETIMEDOUT;
2656        }
2657
2658        /* UniMAC receive needs to be turned on */
2659        umac_enable_set(priv, CMD_RX_EN, 1);
2660
2661        netif_dbg(priv, wol, ndev, "entered WOL mode\n");
2662
2663        return 0;
2664}
2665
2666static int __maybe_unused bcm_sysport_suspend(struct device *d)
2667{
2668        struct net_device *dev = dev_get_drvdata(d);
2669        struct bcm_sysport_priv *priv = netdev_priv(dev);
2670        unsigned int i;
2671        int ret = 0;
2672        u32 reg;
2673
2674        if (!netif_running(dev))
2675                return 0;
2676
2677        netif_device_detach(dev);
2678
2679        bcm_sysport_netif_stop(dev);
2680
2681        phy_suspend(dev->phydev);
2682
2683        /* Disable UniMAC RX */
2684        umac_enable_set(priv, CMD_RX_EN, 0);
2685
2686        ret = rdma_enable_set(priv, 0);
2687        if (ret) {
2688                netdev_err(dev, "RDMA timeout!\n");
2689                return ret;
2690        }
2691
2692        /* Disable RXCHK if enabled */
2693        if (priv->rx_chk_en) {
2694                reg = rxchk_readl(priv, RXCHK_CONTROL);
2695                reg &= ~RXCHK_EN;
2696                rxchk_writel(priv, reg, RXCHK_CONTROL);
2697        }
2698
2699        /* Flush RX pipe */
2700        if (!priv->wolopts)
2701                topctrl_writel(priv, RX_FLUSH, RX_FLUSH_CNTL);
2702
2703        ret = tdma_enable_set(priv, 0);
2704        if (ret) {
2705                netdev_err(dev, "TDMA timeout!\n");
2706                return ret;
2707        }
2708
2709        /* Wait for a packet boundary */
2710        usleep_range(2000, 3000);
2711
2712        umac_enable_set(priv, CMD_TX_EN, 0);
2713
2714        topctrl_writel(priv, TX_FLUSH, TX_FLUSH_CNTL);
2715
2716        /* Free RX/TX rings SW structures */
2717        for (i = 0; i < dev->num_tx_queues; i++)
2718                bcm_sysport_fini_tx_ring(priv, i);
2719        bcm_sysport_fini_rx_ring(priv);
2720
2721        /* Get prepared for Wake-on-LAN */
2722        if (device_may_wakeup(d) && priv->wolopts)
2723                ret = bcm_sysport_suspend_to_wol(priv);
2724
2725        return ret;
2726}
2727
2728static int __maybe_unused bcm_sysport_resume(struct device *d)
2729{
2730        struct net_device *dev = dev_get_drvdata(d);
2731        struct bcm_sysport_priv *priv = netdev_priv(dev);
2732        unsigned int i;
2733        int ret;
2734
2735        if (!netif_running(dev))
2736                return 0;
2737
2738        umac_reset(priv);
2739
2740        /* Disable the UniMAC RX/TX */
2741        umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 0);
2742
2743        /* We may have been suspended and never received a WOL event that
2744         * would turn off MPD detection, take care of that now
2745         */
2746        bcm_sysport_resume_from_wol(priv);
2747
2748        /* Initialize both hardware and software ring */
2749        for (i = 0; i < dev->num_tx_queues; i++) {
2750                ret = bcm_sysport_init_tx_ring(priv, i);
2751                if (ret) {
2752                        netdev_err(dev, "failed to initialize TX ring %d\n",
2753                                   i);
2754                        goto out_free_tx_rings;
2755                }
2756        }
2757
2758        /* Initialize linked-list */
2759        tdma_writel(priv, TDMA_LL_RAM_INIT_BUSY, TDMA_STATUS);
2760
2761        /* Initialize RX ring */
2762        ret = bcm_sysport_init_rx_ring(priv);
2763        if (ret) {
2764                netdev_err(dev, "failed to initialize RX ring\n");
2765                goto out_free_rx_ring;
2766        }
2767
2768        /* RX pipe enable */
2769        topctrl_writel(priv, 0, RX_FLUSH_CNTL);
2770
2771        ret = rdma_enable_set(priv, 1);
2772        if (ret) {
2773                netdev_err(dev, "failed to enable RDMA\n");
2774                goto out_free_rx_ring;
2775        }
2776
2777        /* Restore enabled features */
2778        bcm_sysport_set_features(dev, dev->features);
2779
2780        rbuf_init(priv);
2781
2782        /* Set maximum frame length */
2783        if (!priv->is_lite)
2784                umac_writel(priv, UMAC_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
2785        else
2786                gib_set_pad_extension(priv);
2787
2788        /* Set MAC address */
2789        umac_set_hw_addr(priv, dev->dev_addr);
2790
2791        umac_enable_set(priv, CMD_RX_EN, 1);
2792
2793        /* TX pipe enable */
2794        topctrl_writel(priv, 0, TX_FLUSH_CNTL);
2795
2796        umac_enable_set(priv, CMD_TX_EN, 1);
2797
2798        ret = tdma_enable_set(priv, 1);
2799        if (ret) {
2800                netdev_err(dev, "TDMA timeout!\n");
2801                goto out_free_rx_ring;
2802        }
2803
2804        phy_resume(dev->phydev);
2805
2806        bcm_sysport_netif_start(dev);
2807
2808        netif_device_attach(dev);
2809
2810        return 0;
2811
2812out_free_rx_ring:
2813        bcm_sysport_fini_rx_ring(priv);
2814out_free_tx_rings:
2815        for (i = 0; i < dev->num_tx_queues; i++)
2816                bcm_sysport_fini_tx_ring(priv, i);
2817        return ret;
2818}
2819
2820static SIMPLE_DEV_PM_OPS(bcm_sysport_pm_ops,
2821                bcm_sysport_suspend, bcm_sysport_resume);
2822
2823static struct platform_driver bcm_sysport_driver = {
2824        .probe  = bcm_sysport_probe,
2825        .remove = bcm_sysport_remove,
2826        .driver =  {
2827                .name = "brcm-systemport",
2828                .of_match_table = bcm_sysport_of_match,
2829                .pm = &bcm_sysport_pm_ops,
2830        },
2831};
2832module_platform_driver(bcm_sysport_driver);
2833
2834MODULE_AUTHOR("Broadcom Corporation");
2835MODULE_DESCRIPTION("Broadcom System Port Ethernet MAC driver");
2836MODULE_ALIAS("platform:brcm-systemport");
2837MODULE_LICENSE("GPL");
2838