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