linux/drivers/net/ethernet/marvell/mvpp2.c
<<
>>
Prefs
   1/*
   2 * Driver for Marvell PPv2 network controller for Armada 375 SoC.
   3 *
   4 * Copyright (C) 2014 Marvell
   5 *
   6 * Marcin Wojtas <mw@semihalf.com>
   7 *
   8 * This file is licensed under the terms of the GNU General Public
   9 * License version 2. This program is licensed "as is" without any
  10 * warranty of any kind, whether express or implied.
  11 */
  12
  13#include <linux/kernel.h>
  14#include <linux/netdevice.h>
  15#include <linux/etherdevice.h>
  16#include <linux/platform_device.h>
  17#include <linux/skbuff.h>
  18#include <linux/inetdevice.h>
  19#include <linux/mbus.h>
  20#include <linux/module.h>
  21#include <linux/interrupt.h>
  22#include <linux/cpumask.h>
  23#include <linux/of.h>
  24#include <linux/of_irq.h>
  25#include <linux/of_mdio.h>
  26#include <linux/of_net.h>
  27#include <linux/of_address.h>
  28#include <linux/phy.h>
  29#include <linux/clk.h>
  30#include <linux/hrtimer.h>
  31#include <linux/ktime.h>
  32#include <uapi/linux/ppp_defs.h>
  33#include <net/ip.h>
  34#include <net/ipv6.h>
  35
  36/* RX Fifo Registers */
  37#define MVPP2_RX_DATA_FIFO_SIZE_REG(port)       (0x00 + 4 * (port))
  38#define MVPP2_RX_ATTR_FIFO_SIZE_REG(port)       (0x20 + 4 * (port))
  39#define MVPP2_RX_MIN_PKT_SIZE_REG               0x60
  40#define MVPP2_RX_FIFO_INIT_REG                  0x64
  41
  42/* RX DMA Top Registers */
  43#define MVPP2_RX_CTRL_REG(port)                 (0x140 + 4 * (port))
  44#define     MVPP2_RX_LOW_LATENCY_PKT_SIZE(s)    (((s) & 0xfff) << 16)
  45#define     MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK   BIT(31)
  46#define MVPP2_POOL_BUF_SIZE_REG(pool)           (0x180 + 4 * (pool))
  47#define     MVPP2_POOL_BUF_SIZE_OFFSET          5
  48#define MVPP2_RXQ_CONFIG_REG(rxq)               (0x800 + 4 * (rxq))
  49#define     MVPP2_SNOOP_PKT_SIZE_MASK           0x1ff
  50#define     MVPP2_SNOOP_BUF_HDR_MASK            BIT(9)
  51#define     MVPP2_RXQ_POOL_SHORT_OFFS           20
  52#define     MVPP2_RXQ_POOL_SHORT_MASK           0x700000
  53#define     MVPP2_RXQ_POOL_LONG_OFFS            24
  54#define     MVPP2_RXQ_POOL_LONG_MASK            0x7000000
  55#define     MVPP2_RXQ_PACKET_OFFSET_OFFS        28
  56#define     MVPP2_RXQ_PACKET_OFFSET_MASK        0x70000000
  57#define     MVPP2_RXQ_DISABLE_MASK              BIT(31)
  58
  59/* Parser Registers */
  60#define MVPP2_PRS_INIT_LOOKUP_REG               0x1000
  61#define     MVPP2_PRS_PORT_LU_MAX               0xf
  62#define     MVPP2_PRS_PORT_LU_MASK(port)        (0xff << ((port) * 4))
  63#define     MVPP2_PRS_PORT_LU_VAL(port, val)    ((val) << ((port) * 4))
  64#define MVPP2_PRS_INIT_OFFS_REG(port)           (0x1004 + ((port) & 4))
  65#define     MVPP2_PRS_INIT_OFF_MASK(port)       (0x3f << (((port) % 4) * 8))
  66#define     MVPP2_PRS_INIT_OFF_VAL(port, val)   ((val) << (((port) % 4) * 8))
  67#define MVPP2_PRS_MAX_LOOP_REG(port)            (0x100c + ((port) & 4))
  68#define     MVPP2_PRS_MAX_LOOP_MASK(port)       (0xff << (((port) % 4) * 8))
  69#define     MVPP2_PRS_MAX_LOOP_VAL(port, val)   ((val) << (((port) % 4) * 8))
  70#define MVPP2_PRS_TCAM_IDX_REG                  0x1100
  71#define MVPP2_PRS_TCAM_DATA_REG(idx)            (0x1104 + (idx) * 4)
  72#define     MVPP2_PRS_TCAM_INV_MASK             BIT(31)
  73#define MVPP2_PRS_SRAM_IDX_REG                  0x1200
  74#define MVPP2_PRS_SRAM_DATA_REG(idx)            (0x1204 + (idx) * 4)
  75#define MVPP2_PRS_TCAM_CTRL_REG                 0x1230
  76#define     MVPP2_PRS_TCAM_EN_MASK              BIT(0)
  77
  78/* Classifier Registers */
  79#define MVPP2_CLS_MODE_REG                      0x1800
  80#define     MVPP2_CLS_MODE_ACTIVE_MASK          BIT(0)
  81#define MVPP2_CLS_PORT_WAY_REG                  0x1810
  82#define     MVPP2_CLS_PORT_WAY_MASK(port)       (1 << (port))
  83#define MVPP2_CLS_LKP_INDEX_REG                 0x1814
  84#define     MVPP2_CLS_LKP_INDEX_WAY_OFFS        6
  85#define MVPP2_CLS_LKP_TBL_REG                   0x1818
  86#define     MVPP2_CLS_LKP_TBL_RXQ_MASK          0xff
  87#define     MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK    BIT(25)
  88#define MVPP2_CLS_FLOW_INDEX_REG                0x1820
  89#define MVPP2_CLS_FLOW_TBL0_REG                 0x1824
  90#define MVPP2_CLS_FLOW_TBL1_REG                 0x1828
  91#define MVPP2_CLS_FLOW_TBL2_REG                 0x182c
  92#define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port)    (0x1980 + ((port) * 4))
  93#define     MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS     3
  94#define     MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK     0x7
  95#define MVPP2_CLS_SWFWD_P2HQ_REG(port)          (0x19b0 + ((port) * 4))
  96#define MVPP2_CLS_SWFWD_PCTRL_REG               0x19d0
  97#define     MVPP2_CLS_SWFWD_PCTRL_MASK(port)    (1 << (port))
  98
  99/* Descriptor Manager Top Registers */
 100#define MVPP2_RXQ_NUM_REG                       0x2040
 101#define MVPP2_RXQ_DESC_ADDR_REG                 0x2044
 102#define MVPP2_RXQ_DESC_SIZE_REG                 0x2048
 103#define     MVPP2_RXQ_DESC_SIZE_MASK            0x3ff0
 104#define MVPP2_RXQ_STATUS_UPDATE_REG(rxq)        (0x3000 + 4 * (rxq))
 105#define     MVPP2_RXQ_NUM_PROCESSED_OFFSET      0
 106#define     MVPP2_RXQ_NUM_NEW_OFFSET            16
 107#define MVPP2_RXQ_STATUS_REG(rxq)               (0x3400 + 4 * (rxq))
 108#define     MVPP2_RXQ_OCCUPIED_MASK             0x3fff
 109#define     MVPP2_RXQ_NON_OCCUPIED_OFFSET       16
 110#define     MVPP2_RXQ_NON_OCCUPIED_MASK         0x3fff0000
 111#define MVPP2_RXQ_THRESH_REG                    0x204c
 112#define     MVPP2_OCCUPIED_THRESH_OFFSET        0
 113#define     MVPP2_OCCUPIED_THRESH_MASK          0x3fff
 114#define MVPP2_RXQ_INDEX_REG                     0x2050
 115#define MVPP2_TXQ_NUM_REG                       0x2080
 116#define MVPP2_TXQ_DESC_ADDR_REG                 0x2084
 117#define MVPP2_TXQ_DESC_SIZE_REG                 0x2088
 118#define     MVPP2_TXQ_DESC_SIZE_MASK            0x3ff0
 119#define MVPP2_AGGR_TXQ_UPDATE_REG               0x2090
 120#define MVPP2_TXQ_THRESH_REG                    0x2094
 121#define     MVPP2_TRANSMITTED_THRESH_OFFSET     16
 122#define     MVPP2_TRANSMITTED_THRESH_MASK       0x3fff0000
 123#define MVPP2_TXQ_INDEX_REG                     0x2098
 124#define MVPP2_TXQ_PREF_BUF_REG                  0x209c
 125#define     MVPP2_PREF_BUF_PTR(desc)            ((desc) & 0xfff)
 126#define     MVPP2_PREF_BUF_SIZE_4               (BIT(12) | BIT(13))
 127#define     MVPP2_PREF_BUF_SIZE_16              (BIT(12) | BIT(14))
 128#define     MVPP2_PREF_BUF_THRESH(val)          ((val) << 17)
 129#define     MVPP2_TXQ_DRAIN_EN_MASK             BIT(31)
 130#define MVPP2_TXQ_PENDING_REG                   0x20a0
 131#define     MVPP2_TXQ_PENDING_MASK              0x3fff
 132#define MVPP2_TXQ_INT_STATUS_REG                0x20a4
 133#define MVPP2_TXQ_SENT_REG(txq)                 (0x3c00 + 4 * (txq))
 134#define     MVPP2_TRANSMITTED_COUNT_OFFSET      16
 135#define     MVPP2_TRANSMITTED_COUNT_MASK        0x3fff0000
 136#define MVPP2_TXQ_RSVD_REQ_REG                  0x20b0
 137#define     MVPP2_TXQ_RSVD_REQ_Q_OFFSET         16
 138#define MVPP2_TXQ_RSVD_RSLT_REG                 0x20b4
 139#define     MVPP2_TXQ_RSVD_RSLT_MASK            0x3fff
 140#define MVPP2_TXQ_RSVD_CLR_REG                  0x20b8
 141#define     MVPP2_TXQ_RSVD_CLR_OFFSET           16
 142#define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu)       (0x2100 + 4 * (cpu))
 143#define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu)       (0x2140 + 4 * (cpu))
 144#define     MVPP2_AGGR_TXQ_DESC_SIZE_MASK       0x3ff0
 145#define MVPP2_AGGR_TXQ_STATUS_REG(cpu)          (0x2180 + 4 * (cpu))
 146#define     MVPP2_AGGR_TXQ_PENDING_MASK         0x3fff
 147#define MVPP2_AGGR_TXQ_INDEX_REG(cpu)           (0x21c0 + 4 * (cpu))
 148
 149/* MBUS bridge registers */
 150#define MVPP2_WIN_BASE(w)                       (0x4000 + ((w) << 2))
 151#define MVPP2_WIN_SIZE(w)                       (0x4020 + ((w) << 2))
 152#define MVPP2_WIN_REMAP(w)                      (0x4040 + ((w) << 2))
 153#define MVPP2_BASE_ADDR_ENABLE                  0x4060
 154
 155/* Interrupt Cause and Mask registers */
 156#define MVPP2_ISR_RX_THRESHOLD_REG(rxq)         (0x5200 + 4 * (rxq))
 157#define MVPP2_ISR_RXQ_GROUP_REG(rxq)            (0x5400 + 4 * (rxq))
 158#define MVPP2_ISR_ENABLE_REG(port)              (0x5420 + 4 * (port))
 159#define     MVPP2_ISR_ENABLE_INTERRUPT(mask)    ((mask) & 0xffff)
 160#define     MVPP2_ISR_DISABLE_INTERRUPT(mask)   (((mask) << 16) & 0xffff0000)
 161#define MVPP2_ISR_RX_TX_CAUSE_REG(port)         (0x5480 + 4 * (port))
 162#define     MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
 163#define     MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
 164#define     MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK    BIT(24)
 165#define     MVPP2_CAUSE_FCS_ERR_MASK            BIT(25)
 166#define     MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK   BIT(26)
 167#define     MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK   BIT(29)
 168#define     MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK   BIT(30)
 169#define     MVPP2_CAUSE_MISC_SUM_MASK           BIT(31)
 170#define MVPP2_ISR_RX_TX_MASK_REG(port)          (0x54a0 + 4 * (port))
 171#define MVPP2_ISR_PON_RX_TX_MASK_REG            0x54bc
 172#define     MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK     0xffff
 173#define     MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK     0x3fc00000
 174#define     MVPP2_PON_CAUSE_MISC_SUM_MASK               BIT(31)
 175#define MVPP2_ISR_MISC_CAUSE_REG                0x55b0
 176
 177/* Buffer Manager registers */
 178#define MVPP2_BM_POOL_BASE_REG(pool)            (0x6000 + ((pool) * 4))
 179#define     MVPP2_BM_POOL_BASE_ADDR_MASK        0xfffff80
 180#define MVPP2_BM_POOL_SIZE_REG(pool)            (0x6040 + ((pool) * 4))
 181#define     MVPP2_BM_POOL_SIZE_MASK             0xfff0
 182#define MVPP2_BM_POOL_READ_PTR_REG(pool)        (0x6080 + ((pool) * 4))
 183#define     MVPP2_BM_POOL_GET_READ_PTR_MASK     0xfff0
 184#define MVPP2_BM_POOL_PTRS_NUM_REG(pool)        (0x60c0 + ((pool) * 4))
 185#define     MVPP2_BM_POOL_PTRS_NUM_MASK         0xfff0
 186#define MVPP2_BM_BPPI_READ_PTR_REG(pool)        (0x6100 + ((pool) * 4))
 187#define MVPP2_BM_BPPI_PTRS_NUM_REG(pool)        (0x6140 + ((pool) * 4))
 188#define     MVPP2_BM_BPPI_PTR_NUM_MASK          0x7ff
 189#define     MVPP2_BM_BPPI_PREFETCH_FULL_MASK    BIT(16)
 190#define MVPP2_BM_POOL_CTRL_REG(pool)            (0x6200 + ((pool) * 4))
 191#define     MVPP2_BM_START_MASK                 BIT(0)
 192#define     MVPP2_BM_STOP_MASK                  BIT(1)
 193#define     MVPP2_BM_STATE_MASK                 BIT(4)
 194#define     MVPP2_BM_LOW_THRESH_OFFS            8
 195#define     MVPP2_BM_LOW_THRESH_MASK            0x7f00
 196#define     MVPP2_BM_LOW_THRESH_VALUE(val)      ((val) << \
 197                                                MVPP2_BM_LOW_THRESH_OFFS)
 198#define     MVPP2_BM_HIGH_THRESH_OFFS           16
 199#define     MVPP2_BM_HIGH_THRESH_MASK           0x7f0000
 200#define     MVPP2_BM_HIGH_THRESH_VALUE(val)     ((val) << \
 201                                                MVPP2_BM_HIGH_THRESH_OFFS)
 202#define MVPP2_BM_INTR_CAUSE_REG(pool)           (0x6240 + ((pool) * 4))
 203#define     MVPP2_BM_RELEASED_DELAY_MASK        BIT(0)
 204#define     MVPP2_BM_ALLOC_FAILED_MASK          BIT(1)
 205#define     MVPP2_BM_BPPE_EMPTY_MASK            BIT(2)
 206#define     MVPP2_BM_BPPE_FULL_MASK             BIT(3)
 207#define     MVPP2_BM_AVAILABLE_BP_LOW_MASK      BIT(4)
 208#define MVPP2_BM_INTR_MASK_REG(pool)            (0x6280 + ((pool) * 4))
 209#define MVPP2_BM_PHY_ALLOC_REG(pool)            (0x6400 + ((pool) * 4))
 210#define     MVPP2_BM_PHY_ALLOC_GRNTD_MASK       BIT(0)
 211#define MVPP2_BM_VIRT_ALLOC_REG                 0x6440
 212#define MVPP2_BM_PHY_RLS_REG(pool)              (0x6480 + ((pool) * 4))
 213#define     MVPP2_BM_PHY_RLS_MC_BUFF_MASK       BIT(0)
 214#define     MVPP2_BM_PHY_RLS_PRIO_EN_MASK       BIT(1)
 215#define     MVPP2_BM_PHY_RLS_GRNTD_MASK         BIT(2)
 216#define MVPP2_BM_VIRT_RLS_REG                   0x64c0
 217#define MVPP2_BM_MC_RLS_REG                     0x64c4
 218#define     MVPP2_BM_MC_ID_MASK                 0xfff
 219#define     MVPP2_BM_FORCE_RELEASE_MASK         BIT(12)
 220
 221/* TX Scheduler registers */
 222#define MVPP2_TXP_SCHED_PORT_INDEX_REG          0x8000
 223#define MVPP2_TXP_SCHED_Q_CMD_REG               0x8004
 224#define     MVPP2_TXP_SCHED_ENQ_MASK            0xff
 225#define     MVPP2_TXP_SCHED_DISQ_OFFSET         8
 226#define MVPP2_TXP_SCHED_CMD_1_REG               0x8010
 227#define MVPP2_TXP_SCHED_PERIOD_REG              0x8018
 228#define MVPP2_TXP_SCHED_MTU_REG                 0x801c
 229#define     MVPP2_TXP_MTU_MAX                   0x7FFFF
 230#define MVPP2_TXP_SCHED_REFILL_REG              0x8020
 231#define     MVPP2_TXP_REFILL_TOKENS_ALL_MASK    0x7ffff
 232#define     MVPP2_TXP_REFILL_PERIOD_ALL_MASK    0x3ff00000
 233#define     MVPP2_TXP_REFILL_PERIOD_MASK(v)     ((v) << 20)
 234#define MVPP2_TXP_SCHED_TOKEN_SIZE_REG          0x8024
 235#define     MVPP2_TXP_TOKEN_SIZE_MAX            0xffffffff
 236#define MVPP2_TXQ_SCHED_REFILL_REG(q)           (0x8040 + ((q) << 2))
 237#define     MVPP2_TXQ_REFILL_TOKENS_ALL_MASK    0x7ffff
 238#define     MVPP2_TXQ_REFILL_PERIOD_ALL_MASK    0x3ff00000
 239#define     MVPP2_TXQ_REFILL_PERIOD_MASK(v)     ((v) << 20)
 240#define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q)       (0x8060 + ((q) << 2))
 241#define     MVPP2_TXQ_TOKEN_SIZE_MAX            0x7fffffff
 242#define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q)       (0x8080 + ((q) << 2))
 243#define     MVPP2_TXQ_TOKEN_CNTR_MAX            0xffffffff
 244
 245/* TX general registers */
 246#define MVPP2_TX_SNOOP_REG                      0x8800
 247#define MVPP2_TX_PORT_FLUSH_REG                 0x8810
 248#define     MVPP2_TX_PORT_FLUSH_MASK(port)      (1 << (port))
 249
 250/* LMS registers */
 251#define MVPP2_SRC_ADDR_MIDDLE                   0x24
 252#define MVPP2_SRC_ADDR_HIGH                     0x28
 253#define MVPP2_PHY_AN_CFG0_REG                   0x34
 254#define     MVPP2_PHY_AN_STOP_SMI0_MASK         BIT(7)
 255#define MVPP2_MIB_COUNTERS_BASE(port)           (0x1000 + ((port) >> 1) * \
 256                                                0x400 + (port) * 0x400)
 257#define     MVPP2_MIB_LATE_COLLISION            0x7c
 258#define MVPP2_ISR_SUM_MASK_REG                  0x220c
 259#define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG      0x305c
 260#define MVPP2_EXT_GLOBAL_CTRL_DEFAULT           0x27
 261
 262/* Per-port registers */
 263#define MVPP2_GMAC_CTRL_0_REG                   0x0
 264#define      MVPP2_GMAC_PORT_EN_MASK            BIT(0)
 265#define      MVPP2_GMAC_MAX_RX_SIZE_OFFS        2
 266#define      MVPP2_GMAC_MAX_RX_SIZE_MASK        0x7ffc
 267#define      MVPP2_GMAC_MIB_CNTR_EN_MASK        BIT(15)
 268#define MVPP2_GMAC_CTRL_1_REG                   0x4
 269#define      MVPP2_GMAC_PERIODIC_XON_EN_MASK    BIT(1)
 270#define      MVPP2_GMAC_GMII_LB_EN_MASK         BIT(5)
 271#define      MVPP2_GMAC_PCS_LB_EN_BIT           6
 272#define      MVPP2_GMAC_PCS_LB_EN_MASK          BIT(6)
 273#define      MVPP2_GMAC_SA_LOW_OFFS             7
 274#define MVPP2_GMAC_CTRL_2_REG                   0x8
 275#define      MVPP2_GMAC_INBAND_AN_MASK          BIT(0)
 276#define      MVPP2_GMAC_PCS_ENABLE_MASK         BIT(3)
 277#define      MVPP2_GMAC_PORT_RGMII_MASK         BIT(4)
 278#define      MVPP2_GMAC_PORT_RESET_MASK         BIT(6)
 279#define MVPP2_GMAC_AUTONEG_CONFIG               0xc
 280#define      MVPP2_GMAC_FORCE_LINK_DOWN         BIT(0)
 281#define      MVPP2_GMAC_FORCE_LINK_PASS         BIT(1)
 282#define      MVPP2_GMAC_CONFIG_MII_SPEED        BIT(5)
 283#define      MVPP2_GMAC_CONFIG_GMII_SPEED       BIT(6)
 284#define      MVPP2_GMAC_AN_SPEED_EN             BIT(7)
 285#define      MVPP2_GMAC_FC_ADV_EN               BIT(9)
 286#define      MVPP2_GMAC_CONFIG_FULL_DUPLEX      BIT(12)
 287#define      MVPP2_GMAC_AN_DUPLEX_EN            BIT(13)
 288#define MVPP2_GMAC_PORT_FIFO_CFG_1_REG          0x1c
 289#define      MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS     6
 290#define      MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
 291#define      MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v)  (((v) << 6) & \
 292                                        MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
 293
 294#define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK      0xff
 295
 296/* Descriptor ring Macros */
 297#define MVPP2_QUEUE_NEXT_DESC(q, index) \
 298        (((index) < (q)->last_desc) ? ((index) + 1) : 0)
 299
 300/* Various constants */
 301
 302/* Coalescing */
 303#define MVPP2_TXDONE_COAL_PKTS_THRESH   15
 304#define MVPP2_TXDONE_HRTIMER_PERIOD_NS  1000000UL
 305#define MVPP2_RX_COAL_PKTS              32
 306#define MVPP2_RX_COAL_USEC              100
 307
 308/* The two bytes Marvell header. Either contains a special value used
 309 * by Marvell switches when a specific hardware mode is enabled (not
 310 * supported by this driver) or is filled automatically by zeroes on
 311 * the RX side. Those two bytes being at the front of the Ethernet
 312 * header, they allow to have the IP header aligned on a 4 bytes
 313 * boundary automatically: the hardware skips those two bytes on its
 314 * own.
 315 */
 316#define MVPP2_MH_SIZE                   2
 317#define MVPP2_ETH_TYPE_LEN              2
 318#define MVPP2_PPPOE_HDR_SIZE            8
 319#define MVPP2_VLAN_TAG_LEN              4
 320
 321/* Lbtd 802.3 type */
 322#define MVPP2_IP_LBDT_TYPE              0xfffa
 323
 324#define MVPP2_TX_CSUM_MAX_SIZE          9800
 325
 326/* Timeout constants */
 327#define MVPP2_TX_DISABLE_TIMEOUT_MSEC   1000
 328#define MVPP2_TX_PENDING_TIMEOUT_MSEC   1000
 329
 330#define MVPP2_TX_MTU_MAX                0x7ffff
 331
 332/* Maximum number of T-CONTs of PON port */
 333#define MVPP2_MAX_TCONT                 16
 334
 335/* Maximum number of supported ports */
 336#define MVPP2_MAX_PORTS                 4
 337
 338/* Maximum number of TXQs used by single port */
 339#define MVPP2_MAX_TXQ                   8
 340
 341/* Maximum number of RXQs used by single port */
 342#define MVPP2_MAX_RXQ                   8
 343
 344/* Dfault number of RXQs in use */
 345#define MVPP2_DEFAULT_RXQ               4
 346
 347/* Total number of RXQs available to all ports */
 348#define MVPP2_RXQ_TOTAL_NUM             (MVPP2_MAX_PORTS * MVPP2_MAX_RXQ)
 349
 350/* Max number of Rx descriptors */
 351#define MVPP2_MAX_RXD                   128
 352
 353/* Max number of Tx descriptors */
 354#define MVPP2_MAX_TXD                   1024
 355
 356/* Amount of Tx descriptors that can be reserved at once by CPU */
 357#define MVPP2_CPU_DESC_CHUNK            64
 358
 359/* Max number of Tx descriptors in each aggregated queue */
 360#define MVPP2_AGGR_TXQ_SIZE             256
 361
 362/* Descriptor aligned size */
 363#define MVPP2_DESC_ALIGNED_SIZE         32
 364
 365/* Descriptor alignment mask */
 366#define MVPP2_TX_DESC_ALIGN             (MVPP2_DESC_ALIGNED_SIZE - 1)
 367
 368/* RX FIFO constants */
 369#define MVPP2_RX_FIFO_PORT_DATA_SIZE    0x2000
 370#define MVPP2_RX_FIFO_PORT_ATTR_SIZE    0x80
 371#define MVPP2_RX_FIFO_PORT_MIN_PKT      0x80
 372
 373/* RX buffer constants */
 374#define MVPP2_SKB_SHINFO_SIZE \
 375        SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
 376
 377#define MVPP2_RX_PKT_SIZE(mtu) \
 378        ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
 379              ETH_HLEN + ETH_FCS_LEN, cache_line_size())
 380
 381#define MVPP2_RX_BUF_SIZE(pkt_size)     ((pkt_size) + NET_SKB_PAD)
 382#define MVPP2_RX_TOTAL_SIZE(buf_size)   ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
 383#define MVPP2_RX_MAX_PKT_SIZE(total_size) \
 384        ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
 385
 386#define MVPP2_BIT_TO_BYTE(bit)          ((bit) / 8)
 387
 388/* IPv6 max L3 address size */
 389#define MVPP2_MAX_L3_ADDR_SIZE          16
 390
 391/* Port flags */
 392#define MVPP2_F_LOOPBACK                BIT(0)
 393
 394/* Marvell tag types */
 395enum mvpp2_tag_type {
 396        MVPP2_TAG_TYPE_NONE = 0,
 397        MVPP2_TAG_TYPE_MH   = 1,
 398        MVPP2_TAG_TYPE_DSA  = 2,
 399        MVPP2_TAG_TYPE_EDSA = 3,
 400        MVPP2_TAG_TYPE_VLAN = 4,
 401        MVPP2_TAG_TYPE_LAST = 5
 402};
 403
 404/* Parser constants */
 405#define MVPP2_PRS_TCAM_SRAM_SIZE        256
 406#define MVPP2_PRS_TCAM_WORDS            6
 407#define MVPP2_PRS_SRAM_WORDS            4
 408#define MVPP2_PRS_FLOW_ID_SIZE          64
 409#define MVPP2_PRS_FLOW_ID_MASK          0x3f
 410#define MVPP2_PRS_TCAM_ENTRY_INVALID    1
 411#define MVPP2_PRS_TCAM_DSA_TAGGED_BIT   BIT(5)
 412#define MVPP2_PRS_IPV4_HEAD             0x40
 413#define MVPP2_PRS_IPV4_HEAD_MASK        0xf0
 414#define MVPP2_PRS_IPV4_MC               0xe0
 415#define MVPP2_PRS_IPV4_MC_MASK          0xf0
 416#define MVPP2_PRS_IPV4_BC_MASK          0xff
 417#define MVPP2_PRS_IPV4_IHL              0x5
 418#define MVPP2_PRS_IPV4_IHL_MASK         0xf
 419#define MVPP2_PRS_IPV6_MC               0xff
 420#define MVPP2_PRS_IPV6_MC_MASK          0xff
 421#define MVPP2_PRS_IPV6_HOP_MASK         0xff
 422#define MVPP2_PRS_TCAM_PROTO_MASK       0xff
 423#define MVPP2_PRS_TCAM_PROTO_MASK_L     0x3f
 424#define MVPP2_PRS_DBL_VLANS_MAX         100
 425
 426/* Tcam structure:
 427 * - lookup ID - 4 bits
 428 * - port ID - 1 byte
 429 * - additional information - 1 byte
 430 * - header data - 8 bytes
 431 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
 432 */
 433#define MVPP2_PRS_AI_BITS                       8
 434#define MVPP2_PRS_PORT_MASK                     0xff
 435#define MVPP2_PRS_LU_MASK                       0xf
 436#define MVPP2_PRS_TCAM_DATA_BYTE(offs)          \
 437                                    (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
 438#define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)       \
 439                                              (((offs) * 2) - ((offs) % 2)  + 2)
 440#define MVPP2_PRS_TCAM_AI_BYTE                  16
 441#define MVPP2_PRS_TCAM_PORT_BYTE                17
 442#define MVPP2_PRS_TCAM_LU_BYTE                  20
 443#define MVPP2_PRS_TCAM_EN_OFFS(offs)            ((offs) + 2)
 444#define MVPP2_PRS_TCAM_INV_WORD                 5
 445/* Tcam entries ID */
 446#define MVPP2_PE_DROP_ALL               0
 447#define MVPP2_PE_FIRST_FREE_TID         1
 448#define MVPP2_PE_LAST_FREE_TID          (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
 449#define MVPP2_PE_IP6_EXT_PROTO_UN       (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
 450#define MVPP2_PE_MAC_MC_IP6             (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
 451#define MVPP2_PE_IP6_ADDR_UN            (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
 452#define MVPP2_PE_IP4_ADDR_UN            (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
 453#define MVPP2_PE_LAST_DEFAULT_FLOW      (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
 454#define MVPP2_PE_FIRST_DEFAULT_FLOW     (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
 455#define MVPP2_PE_EDSA_TAGGED            (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
 456#define MVPP2_PE_EDSA_UNTAGGED          (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
 457#define MVPP2_PE_DSA_TAGGED             (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
 458#define MVPP2_PE_DSA_UNTAGGED           (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
 459#define MVPP2_PE_ETYPE_EDSA_TAGGED      (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
 460#define MVPP2_PE_ETYPE_EDSA_UNTAGGED    (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
 461#define MVPP2_PE_ETYPE_DSA_TAGGED       (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
 462#define MVPP2_PE_ETYPE_DSA_UNTAGGED     (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
 463#define MVPP2_PE_MH_DEFAULT             (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
 464#define MVPP2_PE_DSA_DEFAULT            (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
 465#define MVPP2_PE_IP6_PROTO_UN           (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
 466#define MVPP2_PE_IP4_PROTO_UN           (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
 467#define MVPP2_PE_ETH_TYPE_UN            (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
 468#define MVPP2_PE_VLAN_DBL               (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
 469#define MVPP2_PE_VLAN_NONE              (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
 470#define MVPP2_PE_MAC_MC_ALL             (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
 471#define MVPP2_PE_MAC_PROMISCUOUS        (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
 472#define MVPP2_PE_MAC_NON_PROMISCUOUS    (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
 473
 474/* Sram structure
 475 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
 476 */
 477#define MVPP2_PRS_SRAM_RI_OFFS                  0
 478#define MVPP2_PRS_SRAM_RI_WORD                  0
 479#define MVPP2_PRS_SRAM_RI_CTRL_OFFS             32
 480#define MVPP2_PRS_SRAM_RI_CTRL_WORD             1
 481#define MVPP2_PRS_SRAM_RI_CTRL_BITS             32
 482#define MVPP2_PRS_SRAM_SHIFT_OFFS               64
 483#define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT           72
 484#define MVPP2_PRS_SRAM_UDF_OFFS                 73
 485#define MVPP2_PRS_SRAM_UDF_BITS                 8
 486#define MVPP2_PRS_SRAM_UDF_MASK                 0xff
 487#define MVPP2_PRS_SRAM_UDF_SIGN_BIT             81
 488#define MVPP2_PRS_SRAM_UDF_TYPE_OFFS            82
 489#define MVPP2_PRS_SRAM_UDF_TYPE_MASK            0x7
 490#define MVPP2_PRS_SRAM_UDF_TYPE_L3              1
 491#define MVPP2_PRS_SRAM_UDF_TYPE_L4              4
 492#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS        85
 493#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK        0x3
 494#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD         1
 495#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD     2
 496#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD     3
 497#define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS          87
 498#define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS          2
 499#define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK          0x3
 500#define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD           0
 501#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD       2
 502#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD       3
 503#define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS         89
 504#define MVPP2_PRS_SRAM_AI_OFFS                  90
 505#define MVPP2_PRS_SRAM_AI_CTRL_OFFS             98
 506#define MVPP2_PRS_SRAM_AI_CTRL_BITS             8
 507#define MVPP2_PRS_SRAM_AI_MASK                  0xff
 508#define MVPP2_PRS_SRAM_NEXT_LU_OFFS             106
 509#define MVPP2_PRS_SRAM_NEXT_LU_MASK             0xf
 510#define MVPP2_PRS_SRAM_LU_DONE_BIT              110
 511#define MVPP2_PRS_SRAM_LU_GEN_BIT               111
 512
 513/* Sram result info bits assignment */
 514#define MVPP2_PRS_RI_MAC_ME_MASK                0x1
 515#define MVPP2_PRS_RI_DSA_MASK                   0x2
 516#define MVPP2_PRS_RI_VLAN_MASK                  0xc
 517#define MVPP2_PRS_RI_VLAN_NONE                  ~(BIT(2) | BIT(3))
 518#define MVPP2_PRS_RI_VLAN_SINGLE                BIT(2)
 519#define MVPP2_PRS_RI_VLAN_DOUBLE                BIT(3)
 520#define MVPP2_PRS_RI_VLAN_TRIPLE                (BIT(2) | BIT(3))
 521#define MVPP2_PRS_RI_CPU_CODE_MASK              0x70
 522#define MVPP2_PRS_RI_CPU_CODE_RX_SPEC           BIT(4)
 523#define MVPP2_PRS_RI_L2_CAST_MASK               0x600
 524#define MVPP2_PRS_RI_L2_UCAST                   ~(BIT(9) | BIT(10))
 525#define MVPP2_PRS_RI_L2_MCAST                   BIT(9)
 526#define MVPP2_PRS_RI_L2_BCAST                   BIT(10)
 527#define MVPP2_PRS_RI_PPPOE_MASK                 0x800
 528#define MVPP2_PRS_RI_L3_PROTO_MASK              0x7000
 529#define MVPP2_PRS_RI_L3_UN                      ~(BIT(12) | BIT(13) | BIT(14))
 530#define MVPP2_PRS_RI_L3_IP4                     BIT(12)
 531#define MVPP2_PRS_RI_L3_IP4_OPT                 BIT(13)
 532#define MVPP2_PRS_RI_L3_IP4_OTHER               (BIT(12) | BIT(13))
 533#define MVPP2_PRS_RI_L3_IP6                     BIT(14)
 534#define MVPP2_PRS_RI_L3_IP6_EXT                 (BIT(12) | BIT(14))
 535#define MVPP2_PRS_RI_L3_ARP                     (BIT(13) | BIT(14))
 536#define MVPP2_PRS_RI_L3_ADDR_MASK               0x18000
 537#define MVPP2_PRS_RI_L3_UCAST                   ~(BIT(15) | BIT(16))
 538#define MVPP2_PRS_RI_L3_MCAST                   BIT(15)
 539#define MVPP2_PRS_RI_L3_BCAST                   (BIT(15) | BIT(16))
 540#define MVPP2_PRS_RI_IP_FRAG_MASK               0x20000
 541#define MVPP2_PRS_RI_UDF3_MASK                  0x300000
 542#define MVPP2_PRS_RI_UDF3_RX_SPECIAL            BIT(21)
 543#define MVPP2_PRS_RI_L4_PROTO_MASK              0x1c00000
 544#define MVPP2_PRS_RI_L4_TCP                     BIT(22)
 545#define MVPP2_PRS_RI_L4_UDP                     BIT(23)
 546#define MVPP2_PRS_RI_L4_OTHER                   (BIT(22) | BIT(23))
 547#define MVPP2_PRS_RI_UDF7_MASK                  0x60000000
 548#define MVPP2_PRS_RI_UDF7_IP6_LITE              BIT(29)
 549#define MVPP2_PRS_RI_DROP_MASK                  0x80000000
 550
 551/* Sram additional info bits assignment */
 552#define MVPP2_PRS_IPV4_DIP_AI_BIT               BIT(0)
 553#define MVPP2_PRS_IPV6_NO_EXT_AI_BIT            BIT(0)
 554#define MVPP2_PRS_IPV6_EXT_AI_BIT               BIT(1)
 555#define MVPP2_PRS_IPV6_EXT_AH_AI_BIT            BIT(2)
 556#define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT        BIT(3)
 557#define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT         BIT(4)
 558#define MVPP2_PRS_SINGLE_VLAN_AI                0
 559#define MVPP2_PRS_DBL_VLAN_AI_BIT               BIT(7)
 560
 561/* DSA/EDSA type */
 562#define MVPP2_PRS_TAGGED                true
 563#define MVPP2_PRS_UNTAGGED              false
 564#define MVPP2_PRS_EDSA                  true
 565#define MVPP2_PRS_DSA                   false
 566
 567/* MAC entries, shadow udf */
 568enum mvpp2_prs_udf {
 569        MVPP2_PRS_UDF_MAC_DEF,
 570        MVPP2_PRS_UDF_MAC_RANGE,
 571        MVPP2_PRS_UDF_L2_DEF,
 572        MVPP2_PRS_UDF_L2_DEF_COPY,
 573        MVPP2_PRS_UDF_L2_USER,
 574};
 575
 576/* Lookup ID */
 577enum mvpp2_prs_lookup {
 578        MVPP2_PRS_LU_MH,
 579        MVPP2_PRS_LU_MAC,
 580        MVPP2_PRS_LU_DSA,
 581        MVPP2_PRS_LU_VLAN,
 582        MVPP2_PRS_LU_L2,
 583        MVPP2_PRS_LU_PPPOE,
 584        MVPP2_PRS_LU_IP4,
 585        MVPP2_PRS_LU_IP6,
 586        MVPP2_PRS_LU_FLOWS,
 587        MVPP2_PRS_LU_LAST,
 588};
 589
 590/* L3 cast enum */
 591enum mvpp2_prs_l3_cast {
 592        MVPP2_PRS_L3_UNI_CAST,
 593        MVPP2_PRS_L3_MULTI_CAST,
 594        MVPP2_PRS_L3_BROAD_CAST
 595};
 596
 597/* Classifier constants */
 598#define MVPP2_CLS_FLOWS_TBL_SIZE        512
 599#define MVPP2_CLS_FLOWS_TBL_DATA_WORDS  3
 600#define MVPP2_CLS_LKP_TBL_SIZE          64
 601
 602/* BM constants */
 603#define MVPP2_BM_POOLS_NUM              8
 604#define MVPP2_BM_LONG_BUF_NUM           1024
 605#define MVPP2_BM_SHORT_BUF_NUM          2048
 606#define MVPP2_BM_POOL_SIZE_MAX          (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
 607#define MVPP2_BM_POOL_PTR_ALIGN         128
 608#define MVPP2_BM_SWF_LONG_POOL(port)    ((port > 2) ? 2 : port)
 609#define MVPP2_BM_SWF_SHORT_POOL         3
 610
 611/* BM cookie (32 bits) definition */
 612#define MVPP2_BM_COOKIE_POOL_OFFS       8
 613#define MVPP2_BM_COOKIE_CPU_OFFS        24
 614
 615/* BM short pool packet size
 616 * These value assure that for SWF the total number
 617 * of bytes allocated for each buffer will be 512
 618 */
 619#define MVPP2_BM_SHORT_PKT_SIZE         MVPP2_RX_MAX_PKT_SIZE(512)
 620
 621enum mvpp2_bm_type {
 622        MVPP2_BM_FREE,
 623        MVPP2_BM_SWF_LONG,
 624        MVPP2_BM_SWF_SHORT
 625};
 626
 627/* Definitions */
 628
 629/* Shared Packet Processor resources */
 630struct mvpp2 {
 631        /* Shared registers' base addresses */
 632        void __iomem *base;
 633        void __iomem *lms_base;
 634
 635        /* Common clocks */
 636        struct clk *pp_clk;
 637        struct clk *gop_clk;
 638
 639        /* List of pointers to port structures */
 640        struct mvpp2_port **port_list;
 641
 642        /* Aggregated TXQs */
 643        struct mvpp2_tx_queue *aggr_txqs;
 644
 645        /* BM pools */
 646        struct mvpp2_bm_pool *bm_pools;
 647
 648        /* PRS shadow table */
 649        struct mvpp2_prs_shadow *prs_shadow;
 650        /* PRS auxiliary table for double vlan entries control */
 651        bool *prs_double_vlans;
 652
 653        /* Tclk value */
 654        u32 tclk;
 655};
 656
 657struct mvpp2_pcpu_stats {
 658        struct  u64_stats_sync syncp;
 659        u64     rx_packets;
 660        u64     rx_bytes;
 661        u64     tx_packets;
 662        u64     tx_bytes;
 663};
 664
 665/* Per-CPU port control */
 666struct mvpp2_port_pcpu {
 667        struct hrtimer tx_done_timer;
 668        bool timer_scheduled;
 669        /* Tasklet for egress finalization */
 670        struct tasklet_struct tx_done_tasklet;
 671};
 672
 673struct mvpp2_port {
 674        u8 id;
 675
 676        int irq;
 677
 678        struct mvpp2 *priv;
 679
 680        /* Per-port registers' base address */
 681        void __iomem *base;
 682
 683        struct mvpp2_rx_queue **rxqs;
 684        struct mvpp2_tx_queue **txqs;
 685        struct net_device *dev;
 686
 687        int pkt_size;
 688
 689        u32 pending_cause_rx;
 690        struct napi_struct napi;
 691
 692        /* Per-CPU port control */
 693        struct mvpp2_port_pcpu __percpu *pcpu;
 694
 695        /* Flags */
 696        unsigned long flags;
 697
 698        u16 tx_ring_size;
 699        u16 rx_ring_size;
 700        struct mvpp2_pcpu_stats __percpu *stats;
 701
 702        phy_interface_t phy_interface;
 703        struct device_node *phy_node;
 704        unsigned int link;
 705        unsigned int duplex;
 706        unsigned int speed;
 707
 708        struct mvpp2_bm_pool *pool_long;
 709        struct mvpp2_bm_pool *pool_short;
 710
 711        /* Index of first port's physical RXQ */
 712        u8 first_rxq;
 713};
 714
 715/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
 716 * layout of the transmit and reception DMA descriptors, and their
 717 * layout is therefore defined by the hardware design
 718 */
 719
 720#define MVPP2_TXD_L3_OFF_SHIFT          0
 721#define MVPP2_TXD_IP_HLEN_SHIFT         8
 722#define MVPP2_TXD_L4_CSUM_FRAG          BIT(13)
 723#define MVPP2_TXD_L4_CSUM_NOT           BIT(14)
 724#define MVPP2_TXD_IP_CSUM_DISABLE       BIT(15)
 725#define MVPP2_TXD_PADDING_DISABLE       BIT(23)
 726#define MVPP2_TXD_L4_UDP                BIT(24)
 727#define MVPP2_TXD_L3_IP6                BIT(26)
 728#define MVPP2_TXD_L_DESC                BIT(28)
 729#define MVPP2_TXD_F_DESC                BIT(29)
 730
 731#define MVPP2_RXD_ERR_SUMMARY           BIT(15)
 732#define MVPP2_RXD_ERR_CODE_MASK         (BIT(13) | BIT(14))
 733#define MVPP2_RXD_ERR_CRC               0x0
 734#define MVPP2_RXD_ERR_OVERRUN           BIT(13)
 735#define MVPP2_RXD_ERR_RESOURCE          (BIT(13) | BIT(14))
 736#define MVPP2_RXD_BM_POOL_ID_OFFS       16
 737#define MVPP2_RXD_BM_POOL_ID_MASK       (BIT(16) | BIT(17) | BIT(18))
 738#define MVPP2_RXD_HWF_SYNC              BIT(21)
 739#define MVPP2_RXD_L4_CSUM_OK            BIT(22)
 740#define MVPP2_RXD_IP4_HEADER_ERR        BIT(24)
 741#define MVPP2_RXD_L4_TCP                BIT(25)
 742#define MVPP2_RXD_L4_UDP                BIT(26)
 743#define MVPP2_RXD_L3_IP4                BIT(28)
 744#define MVPP2_RXD_L3_IP6                BIT(30)
 745#define MVPP2_RXD_BUF_HDR               BIT(31)
 746
 747struct mvpp2_tx_desc {
 748        u32 command;            /* Options used by HW for packet transmitting.*/
 749        u8  packet_offset;      /* the offset from the buffer beginning */
 750        u8  phys_txq;           /* destination queue ID                 */
 751        u16 data_size;          /* data size of transmitted packet in bytes */
 752        u32 buf_phys_addr;      /* physical addr of transmitted buffer  */
 753        u32 buf_cookie;         /* cookie for access to TX buffer in tx path */
 754        u32 reserved1[3];       /* hw_cmd (for future use, BM, PON, PNC) */
 755        u32 reserved2;          /* reserved (for future use)            */
 756};
 757
 758struct mvpp2_rx_desc {
 759        u32 status;             /* info about received packet           */
 760        u16 reserved1;          /* parser_info (for future use, PnC)    */
 761        u16 data_size;          /* size of received packet in bytes     */
 762        u32 buf_phys_addr;      /* physical address of the buffer       */
 763        u32 buf_cookie;         /* cookie for access to RX buffer in rx path */
 764        u16 reserved2;          /* gem_port_id (for future use, PON)    */
 765        u16 reserved3;          /* csum_l4 (for future use, PnC)        */
 766        u8  reserved4;          /* bm_qset (for future use, BM)         */
 767        u8  reserved5;
 768        u16 reserved6;          /* classify_info (for future use, PnC)  */
 769        u32 reserved7;          /* flow_id (for future use, PnC) */
 770        u32 reserved8;
 771};
 772
 773/* Per-CPU Tx queue control */
 774struct mvpp2_txq_pcpu {
 775        int cpu;
 776
 777        /* Number of Tx DMA descriptors in the descriptor ring */
 778        int size;
 779
 780        /* Number of currently used Tx DMA descriptor in the
 781         * descriptor ring
 782         */
 783        int count;
 784
 785        /* Number of Tx DMA descriptors reserved for each CPU */
 786        int reserved_num;
 787
 788        /* Array of transmitted skb */
 789        struct sk_buff **tx_skb;
 790
 791        /* Array of transmitted buffers' physical addresses */
 792        dma_addr_t *tx_buffs;
 793
 794        /* Index of last TX DMA descriptor that was inserted */
 795        int txq_put_index;
 796
 797        /* Index of the TX DMA descriptor to be cleaned up */
 798        int txq_get_index;
 799};
 800
 801struct mvpp2_tx_queue {
 802        /* Physical number of this Tx queue */
 803        u8 id;
 804
 805        /* Logical number of this Tx queue */
 806        u8 log_id;
 807
 808        /* Number of Tx DMA descriptors in the descriptor ring */
 809        int size;
 810
 811        /* Number of currently used Tx DMA descriptor in the descriptor ring */
 812        int count;
 813
 814        /* Per-CPU control of physical Tx queues */
 815        struct mvpp2_txq_pcpu __percpu *pcpu;
 816
 817        /* Array of transmitted skb */
 818        struct sk_buff **tx_skb;
 819
 820        u32 done_pkts_coal;
 821
 822        /* Virtual address of thex Tx DMA descriptors array */
 823        struct mvpp2_tx_desc *descs;
 824
 825        /* DMA address of the Tx DMA descriptors array */
 826        dma_addr_t descs_phys;
 827
 828        /* Index of the last Tx DMA descriptor */
 829        int last_desc;
 830
 831        /* Index of the next Tx DMA descriptor to process */
 832        int next_desc_to_proc;
 833};
 834
 835struct mvpp2_rx_queue {
 836        /* RX queue number, in the range 0-31 for physical RXQs */
 837        u8 id;
 838
 839        /* Num of rx descriptors in the rx descriptor ring */
 840        int size;
 841
 842        u32 pkts_coal;
 843        u32 time_coal;
 844
 845        /* Virtual address of the RX DMA descriptors array */
 846        struct mvpp2_rx_desc *descs;
 847
 848        /* DMA address of the RX DMA descriptors array */
 849        dma_addr_t descs_phys;
 850
 851        /* Index of the last RX DMA descriptor */
 852        int last_desc;
 853
 854        /* Index of the next RX DMA descriptor to process */
 855        int next_desc_to_proc;
 856
 857        /* ID of port to which physical RXQ is mapped */
 858        int port;
 859
 860        /* Port's logic RXQ number to which physical RXQ is mapped */
 861        int logic_rxq;
 862};
 863
 864union mvpp2_prs_tcam_entry {
 865        u32 word[MVPP2_PRS_TCAM_WORDS];
 866        u8  byte[MVPP2_PRS_TCAM_WORDS * 4];
 867};
 868
 869union mvpp2_prs_sram_entry {
 870        u32 word[MVPP2_PRS_SRAM_WORDS];
 871        u8  byte[MVPP2_PRS_SRAM_WORDS * 4];
 872};
 873
 874struct mvpp2_prs_entry {
 875        u32 index;
 876        union mvpp2_prs_tcam_entry tcam;
 877        union mvpp2_prs_sram_entry sram;
 878};
 879
 880struct mvpp2_prs_shadow {
 881        bool valid;
 882        bool finish;
 883
 884        /* Lookup ID */
 885        int lu;
 886
 887        /* User defined offset */
 888        int udf;
 889
 890        /* Result info */
 891        u32 ri;
 892        u32 ri_mask;
 893};
 894
 895struct mvpp2_cls_flow_entry {
 896        u32 index;
 897        u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
 898};
 899
 900struct mvpp2_cls_lookup_entry {
 901        u32 lkpid;
 902        u32 way;
 903        u32 data;
 904};
 905
 906struct mvpp2_bm_pool {
 907        /* Pool number in the range 0-7 */
 908        int id;
 909        enum mvpp2_bm_type type;
 910
 911        /* Buffer Pointers Pool External (BPPE) size */
 912        int size;
 913        /* Number of buffers for this pool */
 914        int buf_num;
 915        /* Pool buffer size */
 916        int buf_size;
 917        /* Packet size */
 918        int pkt_size;
 919
 920        /* BPPE virtual base address */
 921        u32 *virt_addr;
 922        /* BPPE physical base address */
 923        dma_addr_t phys_addr;
 924
 925        /* Ports using BM pool */
 926        u32 port_map;
 927
 928        /* Occupied buffers indicator */
 929        atomic_t in_use;
 930        int in_use_thresh;
 931};
 932
 933struct mvpp2_buff_hdr {
 934        u32 next_buff_phys_addr;
 935        u32 next_buff_virt_addr;
 936        u16 byte_count;
 937        u16 info;
 938        u8  reserved1;          /* bm_qset (for future use, BM)         */
 939};
 940
 941/* Buffer header info bits */
 942#define MVPP2_B_HDR_INFO_MC_ID_MASK     0xfff
 943#define MVPP2_B_HDR_INFO_MC_ID(info)    ((info) & MVPP2_B_HDR_INFO_MC_ID_MASK)
 944#define MVPP2_B_HDR_INFO_LAST_OFFS      12
 945#define MVPP2_B_HDR_INFO_LAST_MASK      BIT(12)
 946#define MVPP2_B_HDR_INFO_IS_LAST(info) \
 947           ((info & MVPP2_B_HDR_INFO_LAST_MASK) >> MVPP2_B_HDR_INFO_LAST_OFFS)
 948
 949/* Static declaractions */
 950
 951/* Number of RXQs used by single port */
 952static int rxq_number = MVPP2_DEFAULT_RXQ;
 953/* Number of TXQs used by single port */
 954static int txq_number = MVPP2_MAX_TXQ;
 955
 956#define MVPP2_DRIVER_NAME "mvpp2"
 957#define MVPP2_DRIVER_VERSION "1.0"
 958
 959/* Utility/helper methods */
 960
 961static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
 962{
 963        writel(data, priv->base + offset);
 964}
 965
 966static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
 967{
 968        return readl(priv->base + offset);
 969}
 970
 971static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
 972{
 973        txq_pcpu->txq_get_index++;
 974        if (txq_pcpu->txq_get_index == txq_pcpu->size)
 975                txq_pcpu->txq_get_index = 0;
 976}
 977
 978static void mvpp2_txq_inc_put(struct mvpp2_txq_pcpu *txq_pcpu,
 979                              struct sk_buff *skb,
 980                              struct mvpp2_tx_desc *tx_desc)
 981{
 982        txq_pcpu->tx_skb[txq_pcpu->txq_put_index] = skb;
 983        if (skb)
 984                txq_pcpu->tx_buffs[txq_pcpu->txq_put_index] =
 985                                                         tx_desc->buf_phys_addr;
 986        txq_pcpu->txq_put_index++;
 987        if (txq_pcpu->txq_put_index == txq_pcpu->size)
 988                txq_pcpu->txq_put_index = 0;
 989}
 990
 991/* Get number of physical egress port */
 992static inline int mvpp2_egress_port(struct mvpp2_port *port)
 993{
 994        return MVPP2_MAX_TCONT + port->id;
 995}
 996
 997/* Get number of physical TXQ */
 998static inline int mvpp2_txq_phys(int port, int txq)
 999{
1000        return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1001}
1002
1003/* Parser configuration routines */
1004
1005/* Update parser tcam and sram hw entries */
1006static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1007{
1008        int i;
1009
1010        if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1011                return -EINVAL;
1012
1013        /* Clear entry invalidation bit */
1014        pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1015
1016        /* Write tcam index - indirect access */
1017        mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1018        for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1019                mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1020
1021        /* Write sram index - indirect access */
1022        mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1023        for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1024                mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1025
1026        return 0;
1027}
1028
1029/* Read tcam entry from hw */
1030static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1031{
1032        int i;
1033
1034        if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1035                return -EINVAL;
1036
1037        /* Write tcam index - indirect access */
1038        mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1039
1040        pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1041                              MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1042        if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1043                return MVPP2_PRS_TCAM_ENTRY_INVALID;
1044
1045        for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1046                pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1047
1048        /* Write sram index - indirect access */
1049        mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1050        for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1051                pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1052
1053        return 0;
1054}
1055
1056/* Invalidate tcam hw entry */
1057static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1058{
1059        /* Write index - indirect access */
1060        mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1061        mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1062                    MVPP2_PRS_TCAM_INV_MASK);
1063}
1064
1065/* Enable shadow table entry and set its lookup ID */
1066static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1067{
1068        priv->prs_shadow[index].valid = true;
1069        priv->prs_shadow[index].lu = lu;
1070}
1071
1072/* Update ri fields in shadow table entry */
1073static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1074                                    unsigned int ri, unsigned int ri_mask)
1075{
1076        priv->prs_shadow[index].ri_mask = ri_mask;
1077        priv->prs_shadow[index].ri = ri;
1078}
1079
1080/* Update lookup field in tcam sw entry */
1081static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1082{
1083        int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1084
1085        pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1086        pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1087}
1088
1089/* Update mask for single port in tcam sw entry */
1090static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1091                                    unsigned int port, bool add)
1092{
1093        int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1094
1095        if (add)
1096                pe->tcam.byte[enable_off] &= ~(1 << port);
1097        else
1098                pe->tcam.byte[enable_off] |= 1 << port;
1099}
1100
1101/* Update port map in tcam sw entry */
1102static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1103                                        unsigned int ports)
1104{
1105        unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1106        int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1107
1108        pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1109        pe->tcam.byte[enable_off] &= ~port_mask;
1110        pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1111}
1112
1113/* Obtain port map from tcam sw entry */
1114static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1115{
1116        int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1117
1118        return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1119}
1120
1121/* Set byte of data and its enable bits in tcam sw entry */
1122static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1123                                         unsigned int offs, unsigned char byte,
1124                                         unsigned char enable)
1125{
1126        pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1127        pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1128}
1129
1130/* Get byte of data and its enable bits from tcam sw entry */
1131static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1132                                         unsigned int offs, unsigned char *byte,
1133                                         unsigned char *enable)
1134{
1135        *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1136        *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1137}
1138
1139/* Compare tcam data bytes with a pattern */
1140static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1141                                    u16 data)
1142{
1143        int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1144        u16 tcam_data;
1145
1146        tcam_data = (8 << pe->tcam.byte[off + 1]) | pe->tcam.byte[off];
1147        if (tcam_data != data)
1148                return false;
1149        return true;
1150}
1151
1152/* Update ai bits in tcam sw entry */
1153static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1154                                     unsigned int bits, unsigned int enable)
1155{
1156        int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1157
1158        for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1159
1160                if (!(enable & BIT(i)))
1161                        continue;
1162
1163                if (bits & BIT(i))
1164                        pe->tcam.byte[ai_idx] |= 1 << i;
1165                else
1166                        pe->tcam.byte[ai_idx] &= ~(1 << i);
1167        }
1168
1169        pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1170}
1171
1172/* Get ai bits from tcam sw entry */
1173static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1174{
1175        return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1176}
1177
1178/* Set ethertype in tcam sw entry */
1179static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1180                                  unsigned short ethertype)
1181{
1182        mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1183        mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1184}
1185
1186/* Set bits in sram sw entry */
1187static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1188                                    int val)
1189{
1190        pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1191}
1192
1193/* Clear bits in sram sw entry */
1194static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1195                                      int val)
1196{
1197        pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1198}
1199
1200/* Update ri bits in sram sw entry */
1201static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1202                                     unsigned int bits, unsigned int mask)
1203{
1204        unsigned int i;
1205
1206        for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1207                int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1208
1209                if (!(mask & BIT(i)))
1210                        continue;
1211
1212                if (bits & BIT(i))
1213                        mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1214                else
1215                        mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1216
1217                mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1218        }
1219}
1220
1221/* Obtain ri bits from sram sw entry */
1222static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1223{
1224        return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1225}
1226
1227/* Update ai bits in sram sw entry */
1228static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1229                                     unsigned int bits, unsigned int mask)
1230{
1231        unsigned int i;
1232        int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1233
1234        for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1235
1236                if (!(mask & BIT(i)))
1237                        continue;
1238
1239                if (bits & BIT(i))
1240                        mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1241                else
1242                        mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1243
1244                mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1245        }
1246}
1247
1248/* Read ai bits from sram sw entry */
1249static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1250{
1251        u8 bits;
1252        int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1253        int ai_en_off = ai_off + 1;
1254        int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1255
1256        bits = (pe->sram.byte[ai_off] >> ai_shift) |
1257               (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1258
1259        return bits;
1260}
1261
1262/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1263 * lookup interation
1264 */
1265static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1266                                       unsigned int lu)
1267{
1268        int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1269
1270        mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1271                                  MVPP2_PRS_SRAM_NEXT_LU_MASK);
1272        mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1273}
1274
1275/* In the sram sw entry set sign and value of the next lookup offset
1276 * and the offset value generated to the classifier
1277 */
1278static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1279                                     unsigned int op)
1280{
1281        /* Set sign */
1282        if (shift < 0) {
1283                mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1284                shift = 0 - shift;
1285        } else {
1286                mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1287        }
1288
1289        /* Set value */
1290        pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1291                                                           (unsigned char)shift;
1292
1293        /* Reset and set operation */
1294        mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1295                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1296        mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1297
1298        /* Set base offset as current */
1299        mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1300}
1301
1302/* In the sram sw entry set sign and value of the user defined offset
1303 * generated to the classifier
1304 */
1305static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1306                                      unsigned int type, int offset,
1307                                      unsigned int op)
1308{
1309        /* Set sign */
1310        if (offset < 0) {
1311                mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1312                offset = 0 - offset;
1313        } else {
1314                mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1315        }
1316
1317        /* Set value */
1318        mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1319                                  MVPP2_PRS_SRAM_UDF_MASK);
1320        mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1321        pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1322                                        MVPP2_PRS_SRAM_UDF_BITS)] &=
1323              ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1324        pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1325                                        MVPP2_PRS_SRAM_UDF_BITS)] |=
1326                                (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1327
1328        /* Set offset type */
1329        mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1330                                  MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1331        mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1332
1333        /* Set offset operation */
1334        mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1335                                  MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1336        mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1337
1338        pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1339                                        MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1340                                             ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1341                                    (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1342
1343        pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1344                                        MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1345                             (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1346
1347        /* Set base offset as current */
1348        mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1349}
1350
1351/* Find parser flow entry */
1352static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1353{
1354        struct mvpp2_prs_entry *pe;
1355        int tid;
1356
1357        pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1358        if (!pe)
1359                return NULL;
1360        mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1361
1362        /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1363        for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1364                u8 bits;
1365
1366                if (!priv->prs_shadow[tid].valid ||
1367                    priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1368                        continue;
1369
1370                pe->index = tid;
1371                mvpp2_prs_hw_read(priv, pe);
1372                bits = mvpp2_prs_sram_ai_get(pe);
1373
1374                /* Sram store classification lookup ID in AI bits [5:0] */
1375                if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1376                        return pe;
1377        }
1378        kfree(pe);
1379
1380        return NULL;
1381}
1382
1383/* Return first free tcam index, seeking from start to end */
1384static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1385                                     unsigned char end)
1386{
1387        int tid;
1388
1389        if (start > end)
1390                swap(start, end);
1391
1392        if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1393                end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1394
1395        for (tid = start; tid <= end; tid++) {
1396                if (!priv->prs_shadow[tid].valid)
1397                        return tid;
1398        }
1399
1400        return -EINVAL;
1401}
1402
1403/* Enable/disable dropping all mac da's */
1404static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1405{
1406        struct mvpp2_prs_entry pe;
1407
1408        if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1409                /* Entry exist - update port only */
1410                pe.index = MVPP2_PE_DROP_ALL;
1411                mvpp2_prs_hw_read(priv, &pe);
1412        } else {
1413                /* Entry doesn't exist - create new */
1414                memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1415                mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1416                pe.index = MVPP2_PE_DROP_ALL;
1417
1418                /* Non-promiscuous mode for all ports - DROP unknown packets */
1419                mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1420                                         MVPP2_PRS_RI_DROP_MASK);
1421
1422                mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1423                mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1424
1425                /* Update shadow table */
1426                mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1427
1428                /* Mask all ports */
1429                mvpp2_prs_tcam_port_map_set(&pe, 0);
1430        }
1431
1432        /* Update port mask */
1433        mvpp2_prs_tcam_port_set(&pe, port, add);
1434
1435        mvpp2_prs_hw_write(priv, &pe);
1436}
1437
1438/* Set port to promiscuous mode */
1439static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1440{
1441        struct mvpp2_prs_entry pe;
1442
1443        /* Promiscuous mode - Accept unknown packets */
1444
1445        if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1446                /* Entry exist - update port only */
1447                pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1448                mvpp2_prs_hw_read(priv, &pe);
1449        } else {
1450                /* Entry doesn't exist - create new */
1451                memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1452                mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1453                pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1454
1455                /* Continue - set next lookup */
1456                mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1457
1458                /* Set result info bits */
1459                mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1460                                         MVPP2_PRS_RI_L2_CAST_MASK);
1461
1462                /* Shift to ethertype */
1463                mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1464                                         MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1465
1466                /* Mask all ports */
1467                mvpp2_prs_tcam_port_map_set(&pe, 0);
1468
1469                /* Update shadow table */
1470                mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1471        }
1472
1473        /* Update port mask */
1474        mvpp2_prs_tcam_port_set(&pe, port, add);
1475
1476        mvpp2_prs_hw_write(priv, &pe);
1477}
1478
1479/* Accept multicast */
1480static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1481                                    bool add)
1482{
1483        struct mvpp2_prs_entry pe;
1484        unsigned char da_mc;
1485
1486        /* Ethernet multicast address first byte is
1487         * 0x01 for IPv4 and 0x33 for IPv6
1488         */
1489        da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1490
1491        if (priv->prs_shadow[index].valid) {
1492                /* Entry exist - update port only */
1493                pe.index = index;
1494                mvpp2_prs_hw_read(priv, &pe);
1495        } else {
1496                /* Entry doesn't exist - create new */
1497                memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1498                mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1499                pe.index = index;
1500
1501                /* Continue - set next lookup */
1502                mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1503
1504                /* Set result info bits */
1505                mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1506                                         MVPP2_PRS_RI_L2_CAST_MASK);
1507
1508                /* Update tcam entry data first byte */
1509                mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1510
1511                /* Shift to ethertype */
1512                mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1513                                         MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1514
1515                /* Mask all ports */
1516                mvpp2_prs_tcam_port_map_set(&pe, 0);
1517
1518                /* Update shadow table */
1519                mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1520        }
1521
1522        /* Update port mask */
1523        mvpp2_prs_tcam_port_set(&pe, port, add);
1524
1525        mvpp2_prs_hw_write(priv, &pe);
1526}
1527
1528/* Set entry for dsa packets */
1529static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
1530                                  bool tagged, bool extend)
1531{
1532        struct mvpp2_prs_entry pe;
1533        int tid, shift;
1534
1535        if (extend) {
1536                tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
1537                shift = 8;
1538        } else {
1539                tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
1540                shift = 4;
1541        }
1542
1543        if (priv->prs_shadow[tid].valid) {
1544                /* Entry exist - update port only */
1545                pe.index = tid;
1546                mvpp2_prs_hw_read(priv, &pe);
1547        } else {
1548                /* Entry doesn't exist - create new */
1549                memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1550                mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1551                pe.index = tid;
1552
1553                /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
1554                mvpp2_prs_sram_shift_set(&pe, shift,
1555                                         MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1556
1557                /* Update shadow table */
1558                mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1559
1560                if (tagged) {
1561                        /* Set tagged bit in DSA tag */
1562                        mvpp2_prs_tcam_data_byte_set(&pe, 0,
1563                                                     MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1564                                                     MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1565                        /* Clear all ai bits for next iteration */
1566                        mvpp2_prs_sram_ai_update(&pe, 0,
1567                                                 MVPP2_PRS_SRAM_AI_MASK);
1568                        /* If packet is tagged continue check vlans */
1569                        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1570                } else {
1571                        /* Set result info bits to 'no vlans' */
1572                        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1573                                                 MVPP2_PRS_RI_VLAN_MASK);
1574                        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1575                }
1576
1577                /* Mask all ports */
1578                mvpp2_prs_tcam_port_map_set(&pe, 0);
1579        }
1580
1581        /* Update port mask */
1582        mvpp2_prs_tcam_port_set(&pe, port, add);
1583
1584        mvpp2_prs_hw_write(priv, &pe);
1585}
1586
1587/* Set entry for dsa ethertype */
1588static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
1589                                            bool add, bool tagged, bool extend)
1590{
1591        struct mvpp2_prs_entry pe;
1592        int tid, shift, port_mask;
1593
1594        if (extend) {
1595                tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
1596                      MVPP2_PE_ETYPE_EDSA_UNTAGGED;
1597                port_mask = 0;
1598                shift = 8;
1599        } else {
1600                tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
1601                      MVPP2_PE_ETYPE_DSA_UNTAGGED;
1602                port_mask = MVPP2_PRS_PORT_MASK;
1603                shift = 4;
1604        }
1605
1606        if (priv->prs_shadow[tid].valid) {
1607                /* Entry exist - update port only */
1608                pe.index = tid;
1609                mvpp2_prs_hw_read(priv, &pe);
1610        } else {
1611                /* Entry doesn't exist - create new */
1612                memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1613                mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1614                pe.index = tid;
1615
1616                /* Set ethertype */
1617                mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
1618                mvpp2_prs_match_etype(&pe, 2, 0);
1619
1620                mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
1621                                         MVPP2_PRS_RI_DSA_MASK);
1622                /* Shift ethertype + 2 byte reserved + tag*/
1623                mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
1624                                         MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1625
1626                /* Update shadow table */
1627                mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1628
1629                if (tagged) {
1630                        /* Set tagged bit in DSA tag */
1631                        mvpp2_prs_tcam_data_byte_set(&pe,
1632                                                     MVPP2_ETH_TYPE_LEN + 2 + 3,
1633                                                 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1634                                                 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1635                        /* Clear all ai bits for next iteration */
1636                        mvpp2_prs_sram_ai_update(&pe, 0,
1637                                                 MVPP2_PRS_SRAM_AI_MASK);
1638                        /* If packet is tagged continue check vlans */
1639                        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1640                } else {
1641                        /* Set result info bits to 'no vlans' */
1642                        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1643                                                 MVPP2_PRS_RI_VLAN_MASK);
1644                        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1645                }
1646                /* Mask/unmask all ports, depending on dsa type */
1647                mvpp2_prs_tcam_port_map_set(&pe, port_mask);
1648        }
1649
1650        /* Update port mask */
1651        mvpp2_prs_tcam_port_set(&pe, port, add);
1652
1653        mvpp2_prs_hw_write(priv, &pe);
1654}
1655
1656/* Search for existing single/triple vlan entry */
1657static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
1658                                                   unsigned short tpid, int ai)
1659{
1660        struct mvpp2_prs_entry *pe;
1661        int tid;
1662
1663        pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1664        if (!pe)
1665                return NULL;
1666        mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
1667
1668        /* Go through the all entries with MVPP2_PRS_LU_VLAN */
1669        for (tid = MVPP2_PE_FIRST_FREE_TID;
1670             tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
1671                unsigned int ri_bits, ai_bits;
1672                bool match;
1673
1674                if (!priv->prs_shadow[tid].valid ||
1675                    priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
1676                        continue;
1677
1678                pe->index = tid;
1679
1680                mvpp2_prs_hw_read(priv, pe);
1681                match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
1682                if (!match)
1683                        continue;
1684
1685                /* Get vlan type */
1686                ri_bits = mvpp2_prs_sram_ri_get(pe);
1687                ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
1688
1689                /* Get current ai value from tcam */
1690                ai_bits = mvpp2_prs_tcam_ai_get(pe);
1691                /* Clear double vlan bit */
1692                ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
1693
1694                if (ai != ai_bits)
1695                        continue;
1696
1697                if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
1698                    ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
1699                        return pe;
1700        }
1701        kfree(pe);
1702
1703        return NULL;
1704}
1705
1706/* Add/update single/triple vlan entry */
1707static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
1708                              unsigned int port_map)
1709{
1710        struct mvpp2_prs_entry *pe;
1711        int tid_aux, tid;
1712        int ret = 0;
1713
1714        pe = mvpp2_prs_vlan_find(priv, tpid, ai);
1715
1716        if (!pe) {
1717                /* Create new tcam entry */
1718                tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
1719                                                MVPP2_PE_FIRST_FREE_TID);
1720                if (tid < 0)
1721                        return tid;
1722
1723                pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1724                if (!pe)
1725                        return -ENOMEM;
1726
1727                /* Get last double vlan tid */
1728                for (tid_aux = MVPP2_PE_LAST_FREE_TID;
1729                     tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
1730                        unsigned int ri_bits;
1731
1732                        if (!priv->prs_shadow[tid_aux].valid ||
1733                            priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
1734                                continue;
1735
1736                        pe->index = tid_aux;
1737                        mvpp2_prs_hw_read(priv, pe);
1738                        ri_bits = mvpp2_prs_sram_ri_get(pe);
1739                        if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
1740                            MVPP2_PRS_RI_VLAN_DOUBLE)
1741                                break;
1742                }
1743
1744                if (tid <= tid_aux) {
1745                        ret = -EINVAL;
1746                        goto error;
1747                }
1748
1749                memset(pe, 0 , sizeof(struct mvpp2_prs_entry));
1750                mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
1751                pe->index = tid;
1752
1753                mvpp2_prs_match_etype(pe, 0, tpid);
1754
1755                mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
1756                /* Shift 4 bytes - skip 1 vlan tag */
1757                mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
1758                                         MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1759                /* Clear all ai bits for next iteration */
1760                mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
1761
1762                if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
1763                        mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
1764                                                 MVPP2_PRS_RI_VLAN_MASK);
1765                } else {
1766                        ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
1767                        mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
1768                                                 MVPP2_PRS_RI_VLAN_MASK);
1769                }
1770                mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
1771
1772                mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
1773        }
1774        /* Update ports' mask */
1775        mvpp2_prs_tcam_port_map_set(pe, port_map);
1776
1777        mvpp2_prs_hw_write(priv, pe);
1778
1779error:
1780        kfree(pe);
1781
1782        return ret;
1783}
1784
1785/* Get first free double vlan ai number */
1786static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
1787{
1788        int i;
1789
1790        for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
1791                if (!priv->prs_double_vlans[i])
1792                        return i;
1793        }
1794
1795        return -EINVAL;
1796}
1797
1798/* Search for existing double vlan entry */
1799static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
1800                                                          unsigned short tpid1,
1801                                                          unsigned short tpid2)
1802{
1803        struct mvpp2_prs_entry *pe;
1804        int tid;
1805
1806        pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1807        if (!pe)
1808                return NULL;
1809        mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
1810
1811        /* Go through the all entries with MVPP2_PRS_LU_VLAN */
1812        for (tid = MVPP2_PE_FIRST_FREE_TID;
1813             tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
1814                unsigned int ri_mask;
1815                bool match;
1816
1817                if (!priv->prs_shadow[tid].valid ||
1818                    priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
1819                        continue;
1820
1821                pe->index = tid;
1822                mvpp2_prs_hw_read(priv, pe);
1823
1824                match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
1825                        && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
1826
1827                if (!match)
1828                        continue;
1829
1830                ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
1831                if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
1832                        return pe;
1833        }
1834        kfree(pe);
1835
1836        return NULL;
1837}
1838
1839/* Add or update double vlan entry */
1840static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
1841                                     unsigned short tpid2,
1842                                     unsigned int port_map)
1843{
1844        struct mvpp2_prs_entry *pe;
1845        int tid_aux, tid, ai, ret = 0;
1846
1847        pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
1848
1849        if (!pe) {
1850                /* Create new tcam entry */
1851                tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1852                                MVPP2_PE_LAST_FREE_TID);
1853                if (tid < 0)
1854                        return tid;
1855
1856                pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1857                if (!pe)
1858                        return -ENOMEM;
1859
1860                /* Set ai value for new double vlan entry */
1861                ai = mvpp2_prs_double_vlan_ai_free_get(priv);
1862                if (ai < 0) {
1863                        ret = ai;
1864                        goto error;
1865                }
1866
1867                /* Get first single/triple vlan tid */
1868                for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
1869                     tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
1870                        unsigned int ri_bits;
1871
1872                        if (!priv->prs_shadow[tid_aux].valid ||
1873                            priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
1874                                continue;
1875
1876                        pe->index = tid_aux;
1877                        mvpp2_prs_hw_read(priv, pe);
1878                        ri_bits = mvpp2_prs_sram_ri_get(pe);
1879                        ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
1880                        if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
1881                            ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
1882                                break;
1883                }
1884
1885                if (tid >= tid_aux) {
1886                        ret = -ERANGE;
1887                        goto error;
1888                }
1889
1890                memset(pe, 0, sizeof(struct mvpp2_prs_entry));
1891                mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
1892                pe->index = tid;
1893
1894                priv->prs_double_vlans[ai] = true;
1895
1896                mvpp2_prs_match_etype(pe, 0, tpid1);
1897                mvpp2_prs_match_etype(pe, 4, tpid2);
1898
1899                mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
1900                /* Shift 8 bytes - skip 2 vlan tags */
1901                mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
1902                                         MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1903                mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
1904                                         MVPP2_PRS_RI_VLAN_MASK);
1905                mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
1906                                         MVPP2_PRS_SRAM_AI_MASK);
1907
1908                mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
1909        }
1910
1911        /* Update ports' mask */
1912        mvpp2_prs_tcam_port_map_set(pe, port_map);
1913        mvpp2_prs_hw_write(priv, pe);
1914
1915error:
1916        kfree(pe);
1917        return ret;
1918}
1919
1920/* IPv4 header parsing for fragmentation and L4 offset */
1921static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
1922                               unsigned int ri, unsigned int ri_mask)
1923{
1924        struct mvpp2_prs_entry pe;
1925        int tid;
1926
1927        if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
1928            (proto != IPPROTO_IGMP))
1929                return -EINVAL;
1930
1931        /* Fragmented packet */
1932        tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1933                                        MVPP2_PE_LAST_FREE_TID);
1934        if (tid < 0)
1935                return tid;
1936
1937        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1938        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
1939        pe.index = tid;
1940
1941        /* Set next lu to IPv4 */
1942        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
1943        mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1944        /* Set L4 offset */
1945        mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
1946                                  sizeof(struct iphdr) - 4,
1947                                  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1948        mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
1949                                 MVPP2_PRS_IPV4_DIP_AI_BIT);
1950        mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_MASK,
1951                                 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
1952
1953        mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
1954        mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
1955        /* Unmask all ports */
1956        mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1957
1958        /* Update shadow table and hw entry */
1959        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
1960        mvpp2_prs_hw_write(priv, &pe);
1961
1962        /* Not fragmented packet */
1963        tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1964                                        MVPP2_PE_LAST_FREE_TID);
1965        if (tid < 0)
1966                return tid;
1967
1968        pe.index = tid;
1969        /* Clear ri before updating */
1970        pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
1971        pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
1972        mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
1973
1974        mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, MVPP2_PRS_TCAM_PROTO_MASK_L);
1975        mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, MVPP2_PRS_TCAM_PROTO_MASK);
1976
1977        /* Update shadow table and hw entry */
1978        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
1979        mvpp2_prs_hw_write(priv, &pe);
1980
1981        return 0;
1982}
1983
1984/* IPv4 L3 multicast or broadcast */
1985static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
1986{
1987        struct mvpp2_prs_entry pe;
1988        int mask, tid;
1989
1990        tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1991                                        MVPP2_PE_LAST_FREE_TID);
1992        if (tid < 0)
1993                return tid;
1994
1995        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1996        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
1997        pe.index = tid;
1998
1999        switch (l3_cast) {
2000        case MVPP2_PRS_L3_MULTI_CAST:
2001                mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2002                                             MVPP2_PRS_IPV4_MC_MASK);
2003                mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2004                                         MVPP2_PRS_RI_L3_ADDR_MASK);
2005                break;
2006        case  MVPP2_PRS_L3_BROAD_CAST:
2007                mask = MVPP2_PRS_IPV4_BC_MASK;
2008                mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2009                mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2010                mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2011                mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2012                mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2013                                         MVPP2_PRS_RI_L3_ADDR_MASK);
2014                break;
2015        default:
2016                return -EINVAL;
2017        }
2018
2019        /* Finished: go to flowid generation */
2020        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2021        mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2022
2023        mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2024                                 MVPP2_PRS_IPV4_DIP_AI_BIT);
2025        /* Unmask all ports */
2026        mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2027
2028        /* Update shadow table and hw entry */
2029        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2030        mvpp2_prs_hw_write(priv, &pe);
2031
2032        return 0;
2033}
2034
2035/* Set entries for protocols over IPv6  */
2036static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2037                               unsigned int ri, unsigned int ri_mask)
2038{
2039        struct mvpp2_prs_entry pe;
2040        int tid;
2041
2042        if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2043            (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2044                return -EINVAL;
2045
2046        tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2047                                        MVPP2_PE_LAST_FREE_TID);
2048        if (tid < 0)
2049                return tid;
2050
2051        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2052        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2053        pe.index = tid;
2054
2055        /* Finished: go to flowid generation */
2056        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2057        mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2058        mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2059        mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2060                                  sizeof(struct ipv6hdr) - 6,
2061                                  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2062
2063        mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2064        mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2065                                 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2066        /* Unmask all ports */
2067        mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2068
2069        /* Write HW */
2070        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2071        mvpp2_prs_hw_write(priv, &pe);
2072
2073        return 0;
2074}
2075
2076/* IPv6 L3 multicast entry */
2077static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2078{
2079        struct mvpp2_prs_entry pe;
2080        int tid;
2081
2082        if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2083                return -EINVAL;
2084
2085        tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2086                                        MVPP2_PE_LAST_FREE_TID);
2087        if (tid < 0)
2088                return tid;
2089
2090        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2091        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2092        pe.index = tid;
2093
2094        /* Finished: go to flowid generation */
2095        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2096        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2097                                 MVPP2_PRS_RI_L3_ADDR_MASK);
2098        mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2099                                 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2100        /* Shift back to IPv6 NH */
2101        mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2102
2103        mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2104                                     MVPP2_PRS_IPV6_MC_MASK);
2105        mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2106        /* Unmask all ports */
2107        mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2108
2109        /* Update shadow table and hw entry */
2110        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2111        mvpp2_prs_hw_write(priv, &pe);
2112
2113        return 0;
2114}
2115
2116/* Parser per-port initialization */
2117static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2118                                   int lu_max, int offset)
2119{
2120        u32 val;
2121
2122        /* Set lookup ID */
2123        val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2124        val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2125        val |=  MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2126        mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2127
2128        /* Set maximum number of loops for packet received from port */
2129        val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2130        val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2131        val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2132        mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2133
2134        /* Set initial offset for packet header extraction for the first
2135         * searching loop
2136         */
2137        val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2138        val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2139        val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2140        mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2141}
2142
2143/* Default flow entries initialization for all ports */
2144static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2145{
2146        struct mvpp2_prs_entry pe;
2147        int port;
2148
2149        for (port = 0; port < MVPP2_MAX_PORTS; port++) {
2150                memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2151                mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2152                pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2153
2154                /* Mask all ports */
2155                mvpp2_prs_tcam_port_map_set(&pe, 0);
2156
2157                /* Set flow ID*/
2158                mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2159                mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2160
2161                /* Update shadow table and hw entry */
2162                mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2163                mvpp2_prs_hw_write(priv, &pe);
2164        }
2165}
2166
2167/* Set default entry for Marvell Header field */
2168static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2169{
2170        struct mvpp2_prs_entry pe;
2171
2172        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2173
2174        pe.index = MVPP2_PE_MH_DEFAULT;
2175        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2176        mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2177                                 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2178        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2179
2180        /* Unmask all ports */
2181        mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2182
2183        /* Update shadow table and hw entry */
2184        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2185        mvpp2_prs_hw_write(priv, &pe);
2186}
2187
2188/* Set default entires (place holder) for promiscuous, non-promiscuous and
2189 * multicast MAC addresses
2190 */
2191static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2192{
2193        struct mvpp2_prs_entry pe;
2194
2195        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2196
2197        /* Non-promiscuous mode for all ports - DROP unknown packets */
2198        pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2199        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2200
2201        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2202                                 MVPP2_PRS_RI_DROP_MASK);
2203        mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2204        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2205
2206        /* Unmask all ports */
2207        mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2208
2209        /* Update shadow table and hw entry */
2210        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2211        mvpp2_prs_hw_write(priv, &pe);
2212
2213        /* place holders only - no ports */
2214        mvpp2_prs_mac_drop_all_set(priv, 0, false);
2215        mvpp2_prs_mac_promisc_set(priv, 0, false);
2216        mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
2217        mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
2218}
2219
2220/* Set default entries for various types of dsa packets */
2221static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2222{
2223        struct mvpp2_prs_entry pe;
2224
2225        /* None tagged EDSA entry - place holder */
2226        mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2227                              MVPP2_PRS_EDSA);
2228
2229        /* Tagged EDSA entry - place holder */
2230        mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2231
2232        /* None tagged DSA entry - place holder */
2233        mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2234                              MVPP2_PRS_DSA);
2235
2236        /* Tagged DSA entry - place holder */
2237        mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2238
2239        /* None tagged EDSA ethertype entry - place holder*/
2240        mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2241                                        MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2242
2243        /* Tagged EDSA ethertype entry - place holder*/
2244        mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2245                                        MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2246
2247        /* None tagged DSA ethertype entry */
2248        mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2249                                        MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2250
2251        /* Tagged DSA ethertype entry */
2252        mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2253                                        MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2254
2255        /* Set default entry, in case DSA or EDSA tag not found */
2256        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2257        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2258        pe.index = MVPP2_PE_DSA_DEFAULT;
2259        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2260
2261        /* Shift 0 bytes */
2262        mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2263        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2264
2265        /* Clear all sram ai bits for next iteration */
2266        mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2267
2268        /* Unmask all ports */
2269        mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2270
2271        mvpp2_prs_hw_write(priv, &pe);
2272}
2273
2274/* Match basic ethertypes */
2275static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2276{
2277        struct mvpp2_prs_entry pe;
2278        int tid;
2279
2280        /* Ethertype: PPPoE */
2281        tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2282                                        MVPP2_PE_LAST_FREE_TID);
2283        if (tid < 0)
2284                return tid;
2285
2286        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2287        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2288        pe.index = tid;
2289
2290        mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2291
2292        mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2293                                 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2294        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2295        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2296                                 MVPP2_PRS_RI_PPPOE_MASK);
2297
2298        /* Update shadow table and hw entry */
2299        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2300        priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2301        priv->prs_shadow[pe.index].finish = false;
2302        mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2303                                MVPP2_PRS_RI_PPPOE_MASK);
2304        mvpp2_prs_hw_write(priv, &pe);
2305
2306        /* Ethertype: ARP */
2307        tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2308                                        MVPP2_PE_LAST_FREE_TID);
2309        if (tid < 0)
2310                return tid;
2311
2312        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2313        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2314        pe.index = tid;
2315
2316        mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2317
2318        /* Generate flow in the next iteration*/
2319        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2320        mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2321        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2322                                 MVPP2_PRS_RI_L3_PROTO_MASK);
2323        /* Set L3 offset */
2324        mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2325                                  MVPP2_ETH_TYPE_LEN,
2326                                  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2327
2328        /* Update shadow table and hw entry */
2329        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2330        priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2331        priv->prs_shadow[pe.index].finish = true;
2332        mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2333                                MVPP2_PRS_RI_L3_PROTO_MASK);
2334        mvpp2_prs_hw_write(priv, &pe);
2335
2336        /* Ethertype: LBTD */
2337        tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2338                                        MVPP2_PE_LAST_FREE_TID);
2339        if (tid < 0)
2340                return tid;
2341
2342        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2343        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2344        pe.index = tid;
2345
2346        mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2347
2348        /* Generate flow in the next iteration*/
2349        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2350        mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2351        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2352                                 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2353                                 MVPP2_PRS_RI_CPU_CODE_MASK |
2354                                 MVPP2_PRS_RI_UDF3_MASK);
2355        /* Set L3 offset */
2356        mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2357                                  MVPP2_ETH_TYPE_LEN,
2358                                  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2359
2360        /* Update shadow table and hw entry */
2361        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2362        priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2363        priv->prs_shadow[pe.index].finish = true;
2364        mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2365                                MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2366                                MVPP2_PRS_RI_CPU_CODE_MASK |
2367                                MVPP2_PRS_RI_UDF3_MASK);
2368        mvpp2_prs_hw_write(priv, &pe);
2369
2370        /* Ethertype: IPv4 without options */
2371        tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2372                                        MVPP2_PE_LAST_FREE_TID);
2373        if (tid < 0)
2374                return tid;
2375
2376        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2377        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2378        pe.index = tid;
2379
2380        mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2381        mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2382                                     MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2383                                     MVPP2_PRS_IPV4_HEAD_MASK |
2384                                     MVPP2_PRS_IPV4_IHL_MASK);
2385
2386        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2387        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2388                                 MVPP2_PRS_RI_L3_PROTO_MASK);
2389        /* Skip eth_type + 4 bytes of IP header */
2390        mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2391                                 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2392        /* Set L3 offset */
2393        mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2394                                  MVPP2_ETH_TYPE_LEN,
2395                                  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2396
2397        /* Update shadow table and hw entry */
2398        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2399        priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2400        priv->prs_shadow[pe.index].finish = false;
2401        mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2402                                MVPP2_PRS_RI_L3_PROTO_MASK);
2403        mvpp2_prs_hw_write(priv, &pe);
2404
2405        /* Ethertype: IPv4 with options */
2406        tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2407                                        MVPP2_PE_LAST_FREE_TID);
2408        if (tid < 0)
2409                return tid;
2410
2411        pe.index = tid;
2412
2413        /* Clear tcam data before updating */
2414        pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
2415        pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
2416
2417        mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2418                                     MVPP2_PRS_IPV4_HEAD,
2419                                     MVPP2_PRS_IPV4_HEAD_MASK);
2420
2421        /* Clear ri before updating */
2422        pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2423        pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2424        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2425                                 MVPP2_PRS_RI_L3_PROTO_MASK);
2426
2427        /* Update shadow table and hw entry */
2428        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2429        priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2430        priv->prs_shadow[pe.index].finish = false;
2431        mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
2432                                MVPP2_PRS_RI_L3_PROTO_MASK);
2433        mvpp2_prs_hw_write(priv, &pe);
2434
2435        /* Ethertype: IPv6 without options */
2436        tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2437                                        MVPP2_PE_LAST_FREE_TID);
2438        if (tid < 0)
2439                return tid;
2440
2441        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2442        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2443        pe.index = tid;
2444
2445        mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
2446
2447        /* Skip DIP of IPV6 header */
2448        mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
2449                                 MVPP2_MAX_L3_ADDR_SIZE,
2450                                 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2451        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2452        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2453                                 MVPP2_PRS_RI_L3_PROTO_MASK);
2454        /* Set L3 offset */
2455        mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2456                                  MVPP2_ETH_TYPE_LEN,
2457                                  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2458
2459        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2460        priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2461        priv->prs_shadow[pe.index].finish = false;
2462        mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
2463                                MVPP2_PRS_RI_L3_PROTO_MASK);
2464        mvpp2_prs_hw_write(priv, &pe);
2465
2466        /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
2467        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2468        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2469        pe.index = MVPP2_PE_ETH_TYPE_UN;
2470
2471        /* Unmask all ports */
2472        mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2473
2474        /* Generate flow in the next iteration*/
2475        mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2476        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2477        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2478                                 MVPP2_PRS_RI_L3_PROTO_MASK);
2479        /* Set L3 offset even it's unknown L3 */
2480        mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2481                                  MVPP2_ETH_TYPE_LEN,
2482                                  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2483
2484        /* Update shadow table and hw entry */
2485        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2486        priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2487        priv->prs_shadow[pe.index].finish = true;
2488        mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
2489                                MVPP2_PRS_RI_L3_PROTO_MASK);
2490        mvpp2_prs_hw_write(priv, &pe);
2491
2492        return 0;
2493}
2494
2495/* Configure vlan entries and detect up to 2 successive VLAN tags.
2496 * Possible options:
2497 * 0x8100, 0x88A8
2498 * 0x8100, 0x8100
2499 * 0x8100
2500 * 0x88A8
2501 */
2502static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
2503{
2504        struct mvpp2_prs_entry pe;
2505        int err;
2506
2507        priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
2508                                              MVPP2_PRS_DBL_VLANS_MAX,
2509                                              GFP_KERNEL);
2510        if (!priv->prs_double_vlans)
2511                return -ENOMEM;
2512
2513        /* Double VLAN: 0x8100, 0x88A8 */
2514        err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
2515                                        MVPP2_PRS_PORT_MASK);
2516        if (err)
2517                return err;
2518
2519        /* Double VLAN: 0x8100, 0x8100 */
2520        err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
2521                                        MVPP2_PRS_PORT_MASK);
2522        if (err)
2523                return err;
2524
2525        /* Single VLAN: 0x88a8 */
2526        err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
2527                                 MVPP2_PRS_PORT_MASK);
2528        if (err)
2529                return err;
2530
2531        /* Single VLAN: 0x8100 */
2532        err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
2533                                 MVPP2_PRS_PORT_MASK);
2534        if (err)
2535                return err;
2536
2537        /* Set default double vlan entry */
2538        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2539        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2540        pe.index = MVPP2_PE_VLAN_DBL;
2541
2542        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2543        /* Clear ai for next iterations */
2544        mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2545        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2546                                 MVPP2_PRS_RI_VLAN_MASK);
2547
2548        mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
2549                                 MVPP2_PRS_DBL_VLAN_AI_BIT);
2550        /* Unmask all ports */
2551        mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2552
2553        /* Update shadow table and hw entry */
2554        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2555        mvpp2_prs_hw_write(priv, &pe);
2556
2557        /* Set default vlan none entry */
2558        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2559        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2560        pe.index = MVPP2_PE_VLAN_NONE;
2561
2562        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2563        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2564                                 MVPP2_PRS_RI_VLAN_MASK);
2565
2566        /* Unmask all ports */
2567        mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2568
2569        /* Update shadow table and hw entry */
2570        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2571        mvpp2_prs_hw_write(priv, &pe);
2572
2573        return 0;
2574}
2575
2576/* Set entries for PPPoE ethertype */
2577static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
2578{
2579        struct mvpp2_prs_entry pe;
2580        int tid;
2581
2582        /* IPv4 over PPPoE with options */
2583        tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2584                                        MVPP2_PE_LAST_FREE_TID);
2585        if (tid < 0)
2586                return tid;
2587
2588        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2589        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2590        pe.index = tid;
2591
2592        mvpp2_prs_match_etype(&pe, 0, PPP_IP);
2593
2594        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2595        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2596                                 MVPP2_PRS_RI_L3_PROTO_MASK);
2597        /* Skip eth_type + 4 bytes of IP header */
2598        mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2599                                 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2600        /* Set L3 offset */
2601        mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2602                                  MVPP2_ETH_TYPE_LEN,
2603                                  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2604
2605        /* Update shadow table and hw entry */
2606        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2607        mvpp2_prs_hw_write(priv, &pe);
2608
2609        /* IPv4 over PPPoE without options */
2610        tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2611                                        MVPP2_PE_LAST_FREE_TID);
2612        if (tid < 0)
2613                return tid;
2614
2615        pe.index = tid;
2616
2617        mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2618                                     MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2619                                     MVPP2_PRS_IPV4_HEAD_MASK |
2620                                     MVPP2_PRS_IPV4_IHL_MASK);
2621
2622        /* Clear ri before updating */
2623        pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2624        pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2625        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2626                                 MVPP2_PRS_RI_L3_PROTO_MASK);
2627
2628        /* Update shadow table and hw entry */
2629        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2630        mvpp2_prs_hw_write(priv, &pe);
2631
2632        /* IPv6 over PPPoE */
2633        tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2634                                        MVPP2_PE_LAST_FREE_TID);
2635        if (tid < 0)
2636                return tid;
2637
2638        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2639        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2640        pe.index = tid;
2641
2642        mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
2643
2644        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2645        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2646                                 MVPP2_PRS_RI_L3_PROTO_MASK);
2647        /* Skip eth_type + 4 bytes of IPv6 header */
2648        mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2649                                 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2650        /* Set L3 offset */
2651        mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2652                                  MVPP2_ETH_TYPE_LEN,
2653                                  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2654
2655        /* Update shadow table and hw entry */
2656        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2657        mvpp2_prs_hw_write(priv, &pe);
2658
2659        /* Non-IP over PPPoE */
2660        tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2661                                        MVPP2_PE_LAST_FREE_TID);
2662        if (tid < 0)
2663                return tid;
2664
2665        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2666        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2667        pe.index = tid;
2668
2669        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2670                                 MVPP2_PRS_RI_L3_PROTO_MASK);
2671
2672        /* Finished: go to flowid generation */
2673        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2674        mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2675        /* Set L3 offset even if it's unknown L3 */
2676        mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2677                                  MVPP2_ETH_TYPE_LEN,
2678                                  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2679
2680        /* Update shadow table and hw entry */
2681        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2682        mvpp2_prs_hw_write(priv, &pe);
2683
2684        return 0;
2685}
2686
2687/* Initialize entries for IPv4 */
2688static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
2689{
2690        struct mvpp2_prs_entry pe;
2691        int err;
2692
2693        /* Set entries for TCP, UDP and IGMP over IPv4 */
2694        err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
2695                                  MVPP2_PRS_RI_L4_PROTO_MASK);
2696        if (err)
2697                return err;
2698
2699        err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
2700                                  MVPP2_PRS_RI_L4_PROTO_MASK);
2701        if (err)
2702                return err;
2703
2704        err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
2705                                  MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2706                                  MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2707                                  MVPP2_PRS_RI_CPU_CODE_MASK |
2708                                  MVPP2_PRS_RI_UDF3_MASK);
2709        if (err)
2710                return err;
2711
2712        /* IPv4 Broadcast */
2713        err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
2714        if (err)
2715                return err;
2716
2717        /* IPv4 Multicast */
2718        err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
2719        if (err)
2720                return err;
2721
2722        /* Default IPv4 entry for unknown protocols */
2723        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2724        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2725        pe.index = MVPP2_PE_IP4_PROTO_UN;
2726
2727        /* Set next lu to IPv4 */
2728        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2729        mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2730        /* Set L4 offset */
2731        mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2732                                  sizeof(struct iphdr) - 4,
2733                                  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2734        mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2735                                 MVPP2_PRS_IPV4_DIP_AI_BIT);
2736        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
2737                                 MVPP2_PRS_RI_L4_PROTO_MASK);
2738
2739        mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2740        /* Unmask all ports */
2741        mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2742
2743        /* Update shadow table and hw entry */
2744        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2745        mvpp2_prs_hw_write(priv, &pe);
2746
2747        /* Default IPv4 entry for unicast address */
2748        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2749        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2750        pe.index = MVPP2_PE_IP4_ADDR_UN;
2751
2752        /* Finished: go to flowid generation */
2753        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2754        mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2755        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
2756                                 MVPP2_PRS_RI_L3_ADDR_MASK);
2757
2758        mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2759                                 MVPP2_PRS_IPV4_DIP_AI_BIT);
2760        /* Unmask all ports */
2761        mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2762
2763        /* Update shadow table and hw entry */
2764        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2765        mvpp2_prs_hw_write(priv, &pe);
2766
2767        return 0;
2768}
2769
2770/* Initialize entries for IPv6 */
2771static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
2772{
2773        struct mvpp2_prs_entry pe;
2774        int tid, err;
2775
2776        /* Set entries for TCP, UDP and ICMP over IPv6 */
2777        err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
2778                                  MVPP2_PRS_RI_L4_TCP,
2779                                  MVPP2_PRS_RI_L4_PROTO_MASK);
2780        if (err)
2781                return err;
2782
2783        err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
2784                                  MVPP2_PRS_RI_L4_UDP,
2785                                  MVPP2_PRS_RI_L4_PROTO_MASK);
2786        if (err)
2787                return err;
2788
2789        err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
2790                                  MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2791                                  MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2792                                  MVPP2_PRS_RI_CPU_CODE_MASK |
2793                                  MVPP2_PRS_RI_UDF3_MASK);
2794        if (err)
2795                return err;
2796
2797        /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
2798        /* Result Info: UDF7=1, DS lite */
2799        err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
2800                                  MVPP2_PRS_RI_UDF7_IP6_LITE,
2801                                  MVPP2_PRS_RI_UDF7_MASK);
2802        if (err)
2803                return err;
2804
2805        /* IPv6 multicast */
2806        err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
2807        if (err)
2808                return err;
2809
2810        /* Entry for checking hop limit */
2811        tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2812                                        MVPP2_PE_LAST_FREE_TID);
2813        if (tid < 0)
2814                return tid;
2815
2816        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2817        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2818        pe.index = tid;
2819
2820        /* Finished: go to flowid generation */
2821        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2822        mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2823        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
2824                                 MVPP2_PRS_RI_DROP_MASK,
2825                                 MVPP2_PRS_RI_L3_PROTO_MASK |
2826                                 MVPP2_PRS_RI_DROP_MASK);
2827
2828        mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
2829        mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2830                                 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2831
2832        /* Update shadow table and hw entry */
2833        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2834        mvpp2_prs_hw_write(priv, &pe);
2835
2836        /* Default IPv6 entry for unknown protocols */
2837        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2838        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2839        pe.index = MVPP2_PE_IP6_PROTO_UN;
2840
2841        /* Finished: go to flowid generation */
2842        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2843        mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2844        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
2845                                 MVPP2_PRS_RI_L4_PROTO_MASK);
2846        /* Set L4 offset relatively to our current place */
2847        mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2848                                  sizeof(struct ipv6hdr) - 4,
2849                                  MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2850
2851        mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2852                                 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2853        /* Unmask all ports */
2854        mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2855
2856        /* Update shadow table and hw entry */
2857        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2858        mvpp2_prs_hw_write(priv, &pe);
2859
2860        /* Default IPv6 entry for unknown ext protocols */
2861        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2862        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2863        pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
2864
2865        /* Finished: go to flowid generation */
2866        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2867        mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2868        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
2869                                 MVPP2_PRS_RI_L4_PROTO_MASK);
2870
2871        mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
2872                                 MVPP2_PRS_IPV6_EXT_AI_BIT);
2873        /* Unmask all ports */
2874        mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2875
2876        /* Update shadow table and hw entry */
2877        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2878        mvpp2_prs_hw_write(priv, &pe);
2879
2880        /* Default IPv6 entry for unicast address */
2881        memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2882        mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2883        pe.index = MVPP2_PE_IP6_ADDR_UN;
2884
2885        /* Finished: go to IPv6 again */
2886        mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2887        mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
2888                                 MVPP2_PRS_RI_L3_ADDR_MASK);
2889        mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2890                                 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2891        /* Shift back to IPV6 NH */
2892        mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2893
2894        mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2895        /* Unmask all ports */
2896        mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2897
2898        /* Update shadow table and hw entry */
2899        mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2900        mvpp2_prs_hw_write(priv, &pe);
2901
2902        return 0;
2903}
2904
2905/* Parser default initialization */
2906static int mvpp2_prs_default_init(struct platform_device *pdev,
2907                                  struct mvpp2 *priv)
2908{
2909        int err, index, i;
2910
2911        /* Enable tcam table */
2912        mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
2913
2914        /* Clear all tcam and sram entries */
2915        for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
2916                mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
2917                for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
2918                        mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
2919
2920                mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
2921                for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
2922                        mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
2923        }
2924
2925        /* Invalidate all tcam entries */
2926        for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
2927                mvpp2_prs_hw_inv(priv, index);
2928
2929        priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
2930                                        sizeof(struct mvpp2_prs_shadow),
2931                                        GFP_KERNEL);
2932        if (!priv->prs_shadow)
2933                return -ENOMEM;
2934
2935        /* Always start from lookup = 0 */
2936        for (index = 0; index < MVPP2_MAX_PORTS; index++)
2937                mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
2938                                       MVPP2_PRS_PORT_LU_MAX, 0);
2939
2940        mvpp2_prs_def_flow_init(priv);
2941
2942        mvpp2_prs_mh_init(priv);
2943
2944        mvpp2_prs_mac_init(priv);
2945
2946        mvpp2_prs_dsa_init(priv);
2947
2948        err = mvpp2_prs_etype_init(priv);
2949        if (err)
2950                return err;
2951
2952        err = mvpp2_prs_vlan_init(pdev, priv);
2953        if (err)
2954                return err;
2955
2956        err = mvpp2_prs_pppoe_init(priv);
2957        if (err)
2958                return err;
2959
2960        err = mvpp2_prs_ip6_init(priv);
2961        if (err)
2962                return err;
2963
2964        err = mvpp2_prs_ip4_init(priv);
2965        if (err)
2966                return err;
2967
2968        return 0;
2969}
2970
2971/* Compare MAC DA with tcam entry data */
2972static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
2973                                       const u8 *da, unsigned char *mask)
2974{
2975        unsigned char tcam_byte, tcam_mask;
2976        int index;
2977
2978        for (index = 0; index < ETH_ALEN; index++) {
2979                mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
2980                if (tcam_mask != mask[index])
2981                        return false;
2982
2983                if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
2984                        return false;
2985        }
2986
2987        return true;
2988}
2989
2990/* Find tcam entry with matched pair <MAC DA, port> */
2991static struct mvpp2_prs_entry *
2992mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
2993                            unsigned char *mask, int udf_type)
2994{
2995        struct mvpp2_prs_entry *pe;
2996        int tid;
2997
2998        pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2999        if (!pe)
3000                return NULL;
3001        mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3002
3003        /* Go through the all entires with MVPP2_PRS_LU_MAC */
3004        for (tid = MVPP2_PE_FIRST_FREE_TID;
3005             tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3006                unsigned int entry_pmap;
3007
3008                if (!priv->prs_shadow[tid].valid ||
3009                    (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3010                    (priv->prs_shadow[tid].udf != udf_type))
3011                        continue;
3012
3013                pe->index = tid;
3014                mvpp2_prs_hw_read(priv, pe);
3015                entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3016
3017                if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3018                    entry_pmap == pmap)
3019                        return pe;
3020        }
3021        kfree(pe);
3022
3023        return NULL;
3024}
3025
3026/* Update parser's mac da entry */
3027static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3028                                   const u8 *da, bool add)
3029{
3030        struct mvpp2_prs_entry *pe;
3031        unsigned int pmap, len, ri;
3032        unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3033        int tid;
3034
3035        /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3036        pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3037                                         MVPP2_PRS_UDF_MAC_DEF);
3038
3039        /* No such entry */
3040        if (!pe) {
3041                if (!add)
3042                        return 0;
3043
3044                /* Create new TCAM entry */
3045                /* Find first range mac entry*/
3046                for (tid = MVPP2_PE_FIRST_FREE_TID;
3047                     tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3048                        if (priv->prs_shadow[tid].valid &&
3049                            (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3050                            (priv->prs_shadow[tid].udf ==
3051                                                       MVPP2_PRS_UDF_MAC_RANGE))
3052                                break;
3053
3054                /* Go through the all entries from first to last */
3055                tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3056                                                tid - 1);
3057                if (tid < 0)
3058                        return tid;
3059
3060                pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3061                if (!pe)
3062                        return -ENOMEM;
3063                mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3064                pe->index = tid;
3065
3066                /* Mask all ports */
3067                mvpp2_prs_tcam_port_map_set(pe, 0);
3068        }
3069
3070        /* Update port mask */
3071        mvpp2_prs_tcam_port_set(pe, port, add);
3072
3073        /* Invalidate the entry if no ports are left enabled */
3074        pmap = mvpp2_prs_tcam_port_map_get(pe);
3075        if (pmap == 0) {
3076                if (add) {
3077                        kfree(pe);
3078                        return -EINVAL;
3079                }
3080                mvpp2_prs_hw_inv(priv, pe->index);
3081                priv->prs_shadow[pe->index].valid = false;
3082                kfree(pe);
3083                return 0;
3084        }
3085
3086        /* Continue - set next lookup */
3087        mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3088
3089        /* Set match on DA */
3090        len = ETH_ALEN;
3091        while (len--)
3092                mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3093
3094        /* Set result info bits */
3095        if (is_broadcast_ether_addr(da))
3096                ri = MVPP2_PRS_RI_L2_BCAST;
3097        else if (is_multicast_ether_addr(da))
3098                ri = MVPP2_PRS_RI_L2_MCAST;
3099        else
3100                ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3101
3102        mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3103                                 MVPP2_PRS_RI_MAC_ME_MASK);
3104        mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3105                                MVPP2_PRS_RI_MAC_ME_MASK);
3106
3107        /* Shift to ethertype */
3108        mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3109                                 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3110
3111        /* Update shadow table and hw entry */
3112        priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3113        mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3114        mvpp2_prs_hw_write(priv, pe);
3115
3116        kfree(pe);
3117
3118        return 0;
3119}
3120
3121static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3122{
3123        struct mvpp2_port *port = netdev_priv(dev);
3124        int err;
3125
3126        /* Remove old parser entry */
3127        err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3128                                      false);
3129        if (err)
3130                return err;
3131
3132        /* Add new parser entry */
3133        err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3134        if (err)
3135                return err;
3136
3137        /* Set addr in the device */
3138        ether_addr_copy(dev->dev_addr, da);
3139
3140        return 0;
3141}
3142
3143/* Delete all port's multicast simple (not range) entries */
3144static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3145{
3146        struct mvpp2_prs_entry pe;
3147        int index, tid;
3148
3149        for (tid = MVPP2_PE_FIRST_FREE_TID;
3150             tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3151                unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3152
3153                if (!priv->prs_shadow[tid].valid ||
3154                    (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3155                    (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3156                        continue;
3157
3158                /* Only simple mac entries */
3159                pe.index = tid;
3160                mvpp2_prs_hw_read(priv, &pe);
3161
3162                /* Read mac addr from entry */
3163                for (index = 0; index < ETH_ALEN; index++)
3164                        mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3165                                                     &da_mask[index]);
3166
3167                if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3168                        /* Delete this entry */
3169                        mvpp2_prs_mac_da_accept(priv, port, da, false);
3170        }
3171}
3172
3173static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3174{
3175        switch (type) {
3176        case MVPP2_TAG_TYPE_EDSA:
3177                /* Add port to EDSA entries */
3178                mvpp2_prs_dsa_tag_set(priv, port, true,
3179                                      MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3180                mvpp2_prs_dsa_tag_set(priv, port, true,
3181                                      MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3182                /* Remove port from DSA entries */
3183                mvpp2_prs_dsa_tag_set(priv, port, false,
3184                                      MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3185                mvpp2_prs_dsa_tag_set(priv, port, false,
3186                                      MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3187                break;
3188
3189        case MVPP2_TAG_TYPE_DSA:
3190                /* Add port to DSA entries */
3191                mvpp2_prs_dsa_tag_set(priv, port, true,
3192                                      MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3193                mvpp2_prs_dsa_tag_set(priv, port, true,
3194                                      MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3195                /* Remove port from EDSA entries */
3196                mvpp2_prs_dsa_tag_set(priv, port, false,
3197                                      MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3198                mvpp2_prs_dsa_tag_set(priv, port, false,
3199                                      MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3200                break;
3201
3202        case MVPP2_TAG_TYPE_MH:
3203        case MVPP2_TAG_TYPE_NONE:
3204                /* Remove port form EDSA and DSA entries */
3205                mvpp2_prs_dsa_tag_set(priv, port, false,
3206                                      MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3207                mvpp2_prs_dsa_tag_set(priv, port, false,
3208                                      MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3209                mvpp2_prs_dsa_tag_set(priv, port, false,
3210                                      MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3211                mvpp2_prs_dsa_tag_set(priv, port, false,
3212                                      MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3213                break;
3214
3215        default:
3216                if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
3217                        return -EINVAL;
3218        }
3219
3220        return 0;
3221}
3222
3223/* Set prs flow for the port */
3224static int mvpp2_prs_def_flow(struct mvpp2_port *port)
3225{
3226        struct mvpp2_prs_entry *pe;
3227        int tid;
3228
3229        pe = mvpp2_prs_flow_find(port->priv, port->id);
3230
3231        /* Such entry not exist */
3232        if (!pe) {
3233                /* Go through the all entires from last to first */
3234                tid = mvpp2_prs_tcam_first_free(port->priv,
3235                                                MVPP2_PE_LAST_FREE_TID,
3236                                               MVPP2_PE_FIRST_FREE_TID);
3237                if (tid < 0)
3238                        return tid;
3239
3240                pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3241                if (!pe)
3242                        return -ENOMEM;
3243
3244                mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
3245                pe->index = tid;
3246
3247                /* Set flow ID*/
3248                mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
3249                mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
3250
3251                /* Update shadow table */
3252                mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
3253        }
3254
3255        mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
3256        mvpp2_prs_hw_write(port->priv, pe);
3257        kfree(pe);
3258
3259        return 0;
3260}
3261
3262/* Classifier configuration routines */
3263
3264/* Update classification flow table registers */
3265static void mvpp2_cls_flow_write(struct mvpp2 *priv,
3266                                 struct mvpp2_cls_flow_entry *fe)
3267{
3268        mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
3269        mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG,  fe->data[0]);
3270        mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG,  fe->data[1]);
3271        mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG,  fe->data[2]);
3272}
3273
3274/* Update classification lookup table register */
3275static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
3276                                   struct mvpp2_cls_lookup_entry *le)
3277{
3278        u32 val;
3279
3280        val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
3281        mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
3282        mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
3283}
3284
3285/* Classifier default initialization */
3286static void mvpp2_cls_init(struct mvpp2 *priv)
3287{
3288        struct mvpp2_cls_lookup_entry le;
3289        struct mvpp2_cls_flow_entry fe;
3290        int index;
3291
3292        /* Enable classifier */
3293        mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
3294
3295        /* Clear classifier flow table */
3296        memset(&fe.data, 0, sizeof(fe.data));
3297        for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
3298                fe.index = index;
3299                mvpp2_cls_flow_write(priv, &fe);
3300        }
3301
3302        /* Clear classifier lookup table */
3303        le.data = 0;
3304        for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
3305                le.lkpid = index;
3306                le.way = 0;
3307                mvpp2_cls_lookup_write(priv, &le);
3308
3309                le.way = 1;
3310                mvpp2_cls_lookup_write(priv, &le);
3311        }
3312}
3313
3314static void mvpp2_cls_port_config(struct mvpp2_port *port)
3315{
3316        struct mvpp2_cls_lookup_entry le;
3317        u32 val;
3318
3319        /* Set way for the port */
3320        val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
3321        val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
3322        mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
3323
3324        /* Pick the entry to be accessed in lookup ID decoding table
3325         * according to the way and lkpid.
3326         */
3327        le.lkpid = port->id;
3328        le.way = 0;
3329        le.data = 0;
3330
3331        /* Set initial CPU queue for receiving packets */
3332        le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
3333        le.data |= port->first_rxq;
3334
3335        /* Disable classification engines */
3336        le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
3337
3338        /* Update lookup ID table entry */
3339        mvpp2_cls_lookup_write(port->priv, &le);
3340}
3341
3342/* Set CPU queue number for oversize packets */
3343static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
3344{
3345        u32 val;
3346
3347        mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
3348                    port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
3349
3350        mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
3351                    (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
3352
3353        val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
3354        val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
3355        mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
3356}
3357
3358/* Buffer Manager configuration routines */
3359
3360/* Create pool */
3361static int mvpp2_bm_pool_create(struct platform_device *pdev,
3362                                struct mvpp2 *priv,
3363                                struct mvpp2_bm_pool *bm_pool, int size)
3364{
3365        int size_bytes;
3366        u32 val;
3367
3368        size_bytes = sizeof(u32) * size;
3369        bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, size_bytes,
3370                                                &bm_pool->phys_addr,
3371                                                GFP_KERNEL);
3372        if (!bm_pool->virt_addr)
3373                return -ENOMEM;
3374
3375        if (!IS_ALIGNED((u32)bm_pool->virt_addr, MVPP2_BM_POOL_PTR_ALIGN)) {
3376                dma_free_coherent(&pdev->dev, size_bytes, bm_pool->virt_addr,
3377                                  bm_pool->phys_addr);
3378                dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
3379                        bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
3380                return -ENOMEM;
3381        }
3382
3383        mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
3384                    bm_pool->phys_addr);
3385        mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
3386
3387        val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3388        val |= MVPP2_BM_START_MASK;
3389        mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3390
3391        bm_pool->type = MVPP2_BM_FREE;
3392        bm_pool->size = size;
3393        bm_pool->pkt_size = 0;
3394        bm_pool->buf_num = 0;
3395        atomic_set(&bm_pool->in_use, 0);
3396
3397        return 0;
3398}
3399
3400/* Set pool buffer size */
3401static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
3402                                      struct mvpp2_bm_pool *bm_pool,
3403                                      int buf_size)
3404{
3405        u32 val;
3406
3407        bm_pool->buf_size = buf_size;
3408
3409        val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
3410        mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
3411}
3412
3413/* Free all buffers from the pool */
3414static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
3415                               struct mvpp2_bm_pool *bm_pool)
3416{
3417        int i;
3418
3419        for (i = 0; i < bm_pool->buf_num; i++) {
3420                dma_addr_t buf_phys_addr;
3421                u32 vaddr;
3422
3423                /* Get buffer virtual address (indirect access) */
3424                buf_phys_addr = mvpp2_read(priv,
3425                                           MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
3426                vaddr = mvpp2_read(priv, MVPP2_BM_VIRT_ALLOC_REG);
3427
3428                dma_unmap_single(dev, buf_phys_addr,
3429                                 bm_pool->buf_size, DMA_FROM_DEVICE);
3430
3431                if (!vaddr)
3432                        break;
3433                dev_kfree_skb_any((struct sk_buff *)vaddr);
3434        }
3435
3436        /* Update BM driver with number of buffers removed from pool */
3437        bm_pool->buf_num -= i;
3438}
3439
3440/* Cleanup pool */
3441static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
3442                                 struct mvpp2 *priv,
3443                                 struct mvpp2_bm_pool *bm_pool)
3444{
3445        u32 val;
3446
3447        mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
3448        if (bm_pool->buf_num) {
3449                WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
3450                return 0;
3451        }
3452
3453        val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3454        val |= MVPP2_BM_STOP_MASK;
3455        mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3456
3457        dma_free_coherent(&pdev->dev, sizeof(u32) * bm_pool->size,
3458                          bm_pool->virt_addr,
3459                          bm_pool->phys_addr);
3460        return 0;
3461}
3462
3463static int mvpp2_bm_pools_init(struct platform_device *pdev,
3464                               struct mvpp2 *priv)
3465{
3466        int i, err, size;
3467        struct mvpp2_bm_pool *bm_pool;
3468
3469        /* Create all pools with maximum size */
3470        size = MVPP2_BM_POOL_SIZE_MAX;
3471        for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3472                bm_pool = &priv->bm_pools[i];
3473                bm_pool->id = i;
3474                err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
3475                if (err)
3476                        goto err_unroll_pools;
3477                mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
3478        }
3479        return 0;
3480
3481err_unroll_pools:
3482        dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
3483        for (i = i - 1; i >= 0; i--)
3484                mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
3485        return err;
3486}
3487
3488static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
3489{
3490        int i, err;
3491
3492        for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3493                /* Mask BM all interrupts */
3494                mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
3495                /* Clear BM cause register */
3496                mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
3497        }
3498
3499        /* Allocate and initialize BM pools */
3500        priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
3501                                     sizeof(struct mvpp2_bm_pool), GFP_KERNEL);
3502        if (!priv->bm_pools)
3503                return -ENOMEM;
3504
3505        err = mvpp2_bm_pools_init(pdev, priv);
3506        if (err < 0)
3507                return err;
3508        return 0;
3509}
3510
3511/* Attach long pool to rxq */
3512static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
3513                                    int lrxq, int long_pool)
3514{
3515        u32 val;
3516        int prxq;
3517
3518        /* Get queue physical ID */
3519        prxq = port->rxqs[lrxq]->id;
3520
3521        val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3522        val &= ~MVPP2_RXQ_POOL_LONG_MASK;
3523        val |= ((long_pool << MVPP2_RXQ_POOL_LONG_OFFS) &
3524                    MVPP2_RXQ_POOL_LONG_MASK);
3525
3526        mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3527}
3528
3529/* Attach short pool to rxq */
3530static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
3531                                     int lrxq, int short_pool)
3532{
3533        u32 val;
3534        int prxq;
3535
3536        /* Get queue physical ID */
3537        prxq = port->rxqs[lrxq]->id;
3538
3539        val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3540        val &= ~MVPP2_RXQ_POOL_SHORT_MASK;
3541        val |= ((short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) &
3542                    MVPP2_RXQ_POOL_SHORT_MASK);
3543
3544        mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3545}
3546
3547/* Allocate skb for BM pool */
3548static struct sk_buff *mvpp2_skb_alloc(struct mvpp2_port *port,
3549                                       struct mvpp2_bm_pool *bm_pool,
3550                                       dma_addr_t *buf_phys_addr,
3551                                       gfp_t gfp_mask)
3552{
3553        struct sk_buff *skb;
3554        dma_addr_t phys_addr;
3555
3556        skb = __dev_alloc_skb(bm_pool->pkt_size, gfp_mask);
3557        if (!skb)
3558                return NULL;
3559
3560        phys_addr = dma_map_single(port->dev->dev.parent, skb->head,
3561                                   MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
3562                                    DMA_FROM_DEVICE);
3563        if (unlikely(dma_mapping_error(port->dev->dev.parent, phys_addr))) {
3564                dev_kfree_skb_any(skb);
3565                return NULL;
3566        }
3567        *buf_phys_addr = phys_addr;
3568
3569        return skb;
3570}
3571
3572/* Set pool number in a BM cookie */
3573static inline u32 mvpp2_bm_cookie_pool_set(u32 cookie, int pool)
3574{
3575        u32 bm;
3576
3577        bm = cookie & ~(0xFF << MVPP2_BM_COOKIE_POOL_OFFS);
3578        bm |= ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS);
3579
3580        return bm;
3581}
3582
3583/* Get pool number from a BM cookie */
3584static inline int mvpp2_bm_cookie_pool_get(u32 cookie)
3585{
3586        return (cookie >> MVPP2_BM_COOKIE_POOL_OFFS) & 0xFF;
3587}
3588
3589/* Release buffer to BM */
3590static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
3591                                     u32 buf_phys_addr, u32 buf_virt_addr)
3592{
3593        mvpp2_write(port->priv, MVPP2_BM_VIRT_RLS_REG, buf_virt_addr);
3594        mvpp2_write(port->priv, MVPP2_BM_PHY_RLS_REG(pool), buf_phys_addr);
3595}
3596
3597/* Release multicast buffer */
3598static void mvpp2_bm_pool_mc_put(struct mvpp2_port *port, int pool,
3599                                 u32 buf_phys_addr, u32 buf_virt_addr,
3600                                 int mc_id)
3601{
3602        u32 val = 0;
3603
3604        val |= (mc_id & MVPP2_BM_MC_ID_MASK);
3605        mvpp2_write(port->priv, MVPP2_BM_MC_RLS_REG, val);
3606
3607        mvpp2_bm_pool_put(port, pool,
3608                          buf_phys_addr | MVPP2_BM_PHY_RLS_MC_BUFF_MASK,
3609                          buf_virt_addr);
3610}
3611
3612/* Refill BM pool */
3613static void mvpp2_pool_refill(struct mvpp2_port *port, u32 bm,
3614                              u32 phys_addr, u32 cookie)
3615{
3616        int pool = mvpp2_bm_cookie_pool_get(bm);
3617
3618        mvpp2_bm_pool_put(port, pool, phys_addr, cookie);
3619}
3620
3621/* Allocate buffers for the pool */
3622static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
3623                             struct mvpp2_bm_pool *bm_pool, int buf_num)
3624{
3625        struct sk_buff *skb;
3626        int i, buf_size, total_size;
3627        u32 bm;
3628        dma_addr_t phys_addr;
3629
3630        buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
3631        total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
3632
3633        if (buf_num < 0 ||
3634            (buf_num + bm_pool->buf_num > bm_pool->size)) {
3635                netdev_err(port->dev,
3636                           "cannot allocate %d buffers for pool %d\n",
3637                           buf_num, bm_pool->id);
3638                return 0;
3639        }
3640
3641        bm = mvpp2_bm_cookie_pool_set(0, bm_pool->id);
3642        for (i = 0; i < buf_num; i++) {
3643                skb = mvpp2_skb_alloc(port, bm_pool, &phys_addr, GFP_KERNEL);
3644                if (!skb)
3645                        break;
3646
3647                mvpp2_pool_refill(port, bm, (u32)phys_addr, (u32)skb);
3648        }
3649
3650        /* Update BM driver with number of buffers added to pool */
3651        bm_pool->buf_num += i;
3652        bm_pool->in_use_thresh = bm_pool->buf_num / 4;
3653
3654        netdev_dbg(port->dev,
3655                   "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
3656                   bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
3657                   bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
3658
3659        netdev_dbg(port->dev,
3660                   "%s pool %d: %d of %d buffers added\n",
3661                   bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
3662                   bm_pool->id, i, buf_num);
3663        return i;
3664}
3665
3666/* Notify the driver that BM pool is being used as specific type and return the
3667 * pool pointer on success
3668 */
3669static struct mvpp2_bm_pool *
3670mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
3671                  int pkt_size)
3672{
3673        struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
3674        int num;
3675
3676        if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
3677                netdev_err(port->dev, "mixing pool types is forbidden\n");
3678                return NULL;
3679        }
3680
3681        if (new_pool->type == MVPP2_BM_FREE)
3682                new_pool->type = type;
3683
3684        /* Allocate buffers in case BM pool is used as long pool, but packet
3685         * size doesn't match MTU or BM pool hasn't being used yet
3686         */
3687        if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
3688            (new_pool->pkt_size == 0)) {
3689                int pkts_num;
3690
3691                /* Set default buffer number or free all the buffers in case
3692                 * the pool is not empty
3693                 */
3694                pkts_num = new_pool->buf_num;
3695                if (pkts_num == 0)
3696                        pkts_num = type == MVPP2_BM_SWF_LONG ?
3697                                   MVPP2_BM_LONG_BUF_NUM :
3698                                   MVPP2_BM_SHORT_BUF_NUM;
3699                else
3700                        mvpp2_bm_bufs_free(port->dev->dev.parent,
3701                                           port->priv, new_pool);
3702
3703                new_pool->pkt_size = pkt_size;
3704
3705                /* Allocate buffers for this pool */
3706                num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
3707                if (num != pkts_num) {
3708                        WARN(1, "pool %d: %d of %d allocated\n",
3709                             new_pool->id, num, pkts_num);
3710                        return NULL;
3711                }
3712        }
3713
3714        mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
3715                                  MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
3716
3717        return new_pool;
3718}
3719
3720/* Initialize pools for swf */
3721static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
3722{
3723        int rxq;
3724
3725        if (!port->pool_long) {
3726                port->pool_long =
3727                       mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
3728                                         MVPP2_BM_SWF_LONG,
3729                                         port->pkt_size);
3730                if (!port->pool_long)
3731                        return -ENOMEM;
3732
3733                port->pool_long->port_map |= (1 << port->id);
3734
3735                for (rxq = 0; rxq < rxq_number; rxq++)
3736                        mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
3737        }
3738
3739        if (!port->pool_short) {
3740                port->pool_short =
3741                        mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
3742                                          MVPP2_BM_SWF_SHORT,
3743                                          MVPP2_BM_SHORT_PKT_SIZE);
3744                if (!port->pool_short)
3745                        return -ENOMEM;
3746
3747                port->pool_short->port_map |= (1 << port->id);
3748
3749                for (rxq = 0; rxq < rxq_number; rxq++)
3750                        mvpp2_rxq_short_pool_set(port, rxq,
3751                                                 port->pool_short->id);
3752        }
3753
3754        return 0;
3755}
3756
3757static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
3758{
3759        struct mvpp2_port *port = netdev_priv(dev);
3760        struct mvpp2_bm_pool *port_pool = port->pool_long;
3761        int num, pkts_num = port_pool->buf_num;
3762        int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
3763
3764        /* Update BM pool with new buffer size */
3765        mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
3766        if (port_pool->buf_num) {
3767                WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
3768                return -EIO;
3769        }
3770
3771        port_pool->pkt_size = pkt_size;
3772        num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
3773        if (num != pkts_num) {
3774                WARN(1, "pool %d: %d of %d allocated\n",
3775                     port_pool->id, num, pkts_num);
3776                return -EIO;
3777        }
3778
3779        mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
3780                                  MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
3781        dev->mtu = mtu;
3782        netdev_update_features(dev);
3783        return 0;
3784}
3785
3786static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
3787{
3788        int cpu, cpu_mask = 0;
3789
3790        for_each_present_cpu(cpu)
3791                cpu_mask |= 1 << cpu;
3792        mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
3793                    MVPP2_ISR_ENABLE_INTERRUPT(cpu_mask));
3794}
3795
3796static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
3797{
3798        int cpu, cpu_mask = 0;
3799
3800        for_each_present_cpu(cpu)
3801                cpu_mask |= 1 << cpu;
3802        mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
3803                    MVPP2_ISR_DISABLE_INTERRUPT(cpu_mask));
3804}
3805
3806/* Mask the current CPU's Rx/Tx interrupts */
3807static void mvpp2_interrupts_mask(void *arg)
3808{
3809        struct mvpp2_port *port = arg;
3810
3811        mvpp2_write(port->priv, MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
3812}
3813
3814/* Unmask the current CPU's Rx/Tx interrupts */
3815static void mvpp2_interrupts_unmask(void *arg)
3816{
3817        struct mvpp2_port *port = arg;
3818
3819        mvpp2_write(port->priv, MVPP2_ISR_RX_TX_MASK_REG(port->id),
3820                    (MVPP2_CAUSE_MISC_SUM_MASK |
3821                     MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK));
3822}
3823
3824/* Port configuration routines */
3825
3826static void mvpp2_port_mii_set(struct mvpp2_port *port)
3827{
3828        u32 val;
3829
3830        val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
3831
3832        switch (port->phy_interface) {
3833        case PHY_INTERFACE_MODE_SGMII:
3834                val |= MVPP2_GMAC_INBAND_AN_MASK;
3835                break;
3836        case PHY_INTERFACE_MODE_RGMII:
3837                val |= MVPP2_GMAC_PORT_RGMII_MASK;
3838        default:
3839                val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
3840        }
3841
3842        writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
3843}
3844
3845static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
3846{
3847        u32 val;
3848
3849        val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
3850        val |= MVPP2_GMAC_FC_ADV_EN;
3851        writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
3852}
3853
3854static void mvpp2_port_enable(struct mvpp2_port *port)
3855{
3856        u32 val;
3857
3858        val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
3859        val |= MVPP2_GMAC_PORT_EN_MASK;
3860        val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
3861        writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
3862}
3863
3864static void mvpp2_port_disable(struct mvpp2_port *port)
3865{
3866        u32 val;
3867
3868        val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
3869        val &= ~(MVPP2_GMAC_PORT_EN_MASK);
3870        writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
3871}
3872
3873/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
3874static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
3875{
3876        u32 val;
3877
3878        val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
3879                    ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
3880        writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
3881}
3882
3883/* Configure loopback port */
3884static void mvpp2_port_loopback_set(struct mvpp2_port *port)
3885{
3886        u32 val;
3887
3888        val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
3889
3890        if (port->speed == 1000)
3891                val |= MVPP2_GMAC_GMII_LB_EN_MASK;
3892        else
3893                val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
3894
3895        if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
3896                val |= MVPP2_GMAC_PCS_LB_EN_MASK;
3897        else
3898                val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
3899
3900        writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
3901}
3902
3903static void mvpp2_port_reset(struct mvpp2_port *port)
3904{
3905        u32 val;
3906
3907        val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
3908                    ~MVPP2_GMAC_PORT_RESET_MASK;
3909        writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
3910
3911        while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
3912               MVPP2_GMAC_PORT_RESET_MASK)
3913                continue;
3914}
3915
3916/* Change maximum receive size of the port */
3917static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
3918{
3919        u32 val;
3920
3921        val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
3922        val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
3923        val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
3924                    MVPP2_GMAC_MAX_RX_SIZE_OFFS);
3925        writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
3926}
3927
3928/* Set defaults to the MVPP2 port */
3929static void mvpp2_defaults_set(struct mvpp2_port *port)
3930{
3931        int tx_port_num, val, queue, ptxq, lrxq;
3932
3933        /* Configure port to loopback if needed */
3934        if (port->flags & MVPP2_F_LOOPBACK)
3935                mvpp2_port_loopback_set(port);
3936
3937        /* Update TX FIFO MIN Threshold */
3938        val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
3939        val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
3940        /* Min. TX threshold must be less than minimal packet length */
3941        val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
3942        writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
3943
3944        /* Disable Legacy WRR, Disable EJP, Release from reset */
3945        tx_port_num = mvpp2_egress_port(port);
3946        mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
3947                    tx_port_num);
3948        mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
3949
3950        /* Close bandwidth for all queues */
3951        for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
3952                ptxq = mvpp2_txq_phys(port->id, queue);
3953                mvpp2_write(port->priv,
3954                            MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
3955        }
3956
3957        /* Set refill period to 1 usec, refill tokens
3958         * and bucket size to maximum
3959         */
3960        mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
3961                    port->priv->tclk / USEC_PER_SEC);
3962        val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
3963        val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
3964        val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
3965        val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
3966        mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
3967        val = MVPP2_TXP_TOKEN_SIZE_MAX;
3968        mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
3969
3970        /* Set MaximumLowLatencyPacketSize value to 256 */
3971        mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
3972                    MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
3973                    MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
3974
3975        /* Enable Rx cache snoop */
3976        for (lrxq = 0; lrxq < rxq_number; lrxq++) {
3977                queue = port->rxqs[lrxq]->id;
3978                val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
3979                val |= MVPP2_SNOOP_PKT_SIZE_MASK |
3980                           MVPP2_SNOOP_BUF_HDR_MASK;
3981                mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
3982        }
3983
3984        /* At default, mask all interrupts to all present cpus */
3985        mvpp2_interrupts_disable(port);
3986}
3987
3988/* Enable/disable receiving packets */
3989static void mvpp2_ingress_enable(struct mvpp2_port *port)
3990{
3991        u32 val;
3992        int lrxq, queue;
3993
3994        for (lrxq = 0; lrxq < rxq_number; lrxq++) {
3995                queue = port->rxqs[lrxq]->id;
3996                val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
3997                val &= ~MVPP2_RXQ_DISABLE_MASK;
3998                mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
3999        }
4000}
4001
4002static void mvpp2_ingress_disable(struct mvpp2_port *port)
4003{
4004        u32 val;
4005        int lrxq, queue;
4006
4007        for (lrxq = 0; lrxq < rxq_number; lrxq++) {
4008                queue = port->rxqs[lrxq]->id;
4009                val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4010                val |= MVPP2_RXQ_DISABLE_MASK;
4011                mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4012        }
4013}
4014
4015/* Enable transmit via physical egress queue
4016 * - HW starts take descriptors from DRAM
4017 */
4018static void mvpp2_egress_enable(struct mvpp2_port *port)
4019{
4020        u32 qmap;
4021        int queue;
4022        int tx_port_num = mvpp2_egress_port(port);
4023
4024        /* Enable all initialized TXs. */
4025        qmap = 0;
4026        for (queue = 0; queue < txq_number; queue++) {
4027                struct mvpp2_tx_queue *txq = port->txqs[queue];
4028
4029                if (txq->descs != NULL)
4030                        qmap |= (1 << queue);
4031        }
4032
4033        mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4034        mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
4035}
4036
4037/* Disable transmit via physical egress queue
4038 * - HW doesn't take descriptors from DRAM
4039 */
4040static void mvpp2_egress_disable(struct mvpp2_port *port)
4041{
4042        u32 reg_data;
4043        int delay;
4044        int tx_port_num = mvpp2_egress_port(port);
4045
4046        /* Issue stop command for active channels only */
4047        mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4048        reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
4049                    MVPP2_TXP_SCHED_ENQ_MASK;
4050        if (reg_data != 0)
4051                mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
4052                            (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
4053
4054        /* Wait for all Tx activity to terminate. */
4055        delay = 0;
4056        do {
4057                if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
4058                        netdev_warn(port->dev,
4059                                    "Tx stop timed out, status=0x%08x\n",
4060                                    reg_data);
4061                        break;
4062                }
4063                mdelay(1);
4064                delay++;
4065
4066                /* Check port TX Command register that all
4067                 * Tx queues are stopped
4068                 */
4069                reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
4070        } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
4071}
4072
4073/* Rx descriptors helper methods */
4074
4075/* Get number of Rx descriptors occupied by received packets */
4076static inline int
4077mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
4078{
4079        u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
4080
4081        return val & MVPP2_RXQ_OCCUPIED_MASK;
4082}
4083
4084/* Update Rx queue status with the number of occupied and available
4085 * Rx descriptor slots.
4086 */
4087static inline void
4088mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
4089                        int used_count, int free_count)
4090{
4091        /* Decrement the number of used descriptors and increment count
4092         * increment the number of free descriptors.
4093         */
4094        u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
4095
4096        mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
4097}
4098
4099/* Get pointer to next RX descriptor to be processed by SW */
4100static inline struct mvpp2_rx_desc *
4101mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
4102{
4103        int rx_desc = rxq->next_desc_to_proc;
4104
4105        rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
4106        prefetch(rxq->descs + rxq->next_desc_to_proc);
4107        return rxq->descs + rx_desc;
4108}
4109
4110/* Set rx queue offset */
4111static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
4112                                 int prxq, int offset)
4113{
4114        u32 val;
4115
4116        /* Convert offset from bytes to units of 32 bytes */
4117        offset = offset >> 5;
4118
4119        val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4120        val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
4121
4122        /* Offset is in */
4123        val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
4124                    MVPP2_RXQ_PACKET_OFFSET_MASK);
4125
4126        mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4127}
4128
4129/* Obtain BM cookie information from descriptor */
4130static u32 mvpp2_bm_cookie_build(struct mvpp2_rx_desc *rx_desc)
4131{
4132        int pool = (rx_desc->status & MVPP2_RXD_BM_POOL_ID_MASK) >>
4133                   MVPP2_RXD_BM_POOL_ID_OFFS;
4134        int cpu = smp_processor_id();
4135
4136        return ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS) |
4137               ((cpu & 0xFF) << MVPP2_BM_COOKIE_CPU_OFFS);
4138}
4139
4140/* Tx descriptors helper methods */
4141
4142/* Get number of Tx descriptors waiting to be transmitted by HW */
4143static int mvpp2_txq_pend_desc_num_get(struct mvpp2_port *port,
4144                                       struct mvpp2_tx_queue *txq)
4145{
4146        u32 val;
4147
4148        mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
4149        val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
4150
4151        return val & MVPP2_TXQ_PENDING_MASK;
4152}
4153
4154/* Get pointer to next Tx descriptor to be processed (send) by HW */
4155static struct mvpp2_tx_desc *
4156mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
4157{
4158        int tx_desc = txq->next_desc_to_proc;
4159
4160        txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
4161        return txq->descs + tx_desc;
4162}
4163
4164/* Update HW with number of aggregated Tx descriptors to be sent */
4165static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
4166{
4167        /* aggregated access - relevant TXQ number is written in TX desc */
4168        mvpp2_write(port->priv, MVPP2_AGGR_TXQ_UPDATE_REG, pending);
4169}
4170
4171
4172/* Check if there are enough free descriptors in aggregated txq.
4173 * If not, update the number of occupied descriptors and repeat the check.
4174 */
4175static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
4176                                     struct mvpp2_tx_queue *aggr_txq, int num)
4177{
4178        if ((aggr_txq->count + num) > aggr_txq->size) {
4179                /* Update number of occupied aggregated Tx descriptors */
4180                int cpu = smp_processor_id();
4181                u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
4182
4183                aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
4184        }
4185
4186        if ((aggr_txq->count + num) > aggr_txq->size)
4187                return -ENOMEM;
4188
4189        return 0;
4190}
4191
4192/* Reserved Tx descriptors allocation request */
4193static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
4194                                         struct mvpp2_tx_queue *txq, int num)
4195{
4196        u32 val;
4197
4198        val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
4199        mvpp2_write(priv, MVPP2_TXQ_RSVD_REQ_REG, val);
4200
4201        val = mvpp2_read(priv, MVPP2_TXQ_RSVD_RSLT_REG);
4202
4203        return val & MVPP2_TXQ_RSVD_RSLT_MASK;
4204}
4205
4206/* Check if there are enough reserved descriptors for transmission.
4207 * If not, request chunk of reserved descriptors and check again.
4208 */
4209static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
4210                                            struct mvpp2_tx_queue *txq,
4211                                            struct mvpp2_txq_pcpu *txq_pcpu,
4212                                            int num)
4213{
4214        int req, cpu, desc_count;
4215
4216        if (txq_pcpu->reserved_num >= num)
4217                return 0;
4218
4219        /* Not enough descriptors reserved! Update the reserved descriptor
4220         * count and check again.
4221         */
4222
4223        desc_count = 0;
4224        /* Compute total of used descriptors */
4225        for_each_present_cpu(cpu) {
4226                struct mvpp2_txq_pcpu *txq_pcpu_aux;
4227
4228                txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
4229                desc_count += txq_pcpu_aux->count;
4230                desc_count += txq_pcpu_aux->reserved_num;
4231        }
4232
4233        req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
4234        desc_count += req;
4235
4236        if (desc_count >
4237           (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
4238                return -ENOMEM;
4239
4240        txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
4241
4242        /* OK, the descriptor cound has been updated: check again. */
4243        if (txq_pcpu->reserved_num < num)
4244                return -ENOMEM;
4245        return 0;
4246}
4247
4248/* Release the last allocated Tx descriptor. Useful to handle DMA
4249 * mapping failures in the Tx path.
4250 */
4251static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
4252{
4253        if (txq->next_desc_to_proc == 0)
4254                txq->next_desc_to_proc = txq->last_desc - 1;
4255        else
4256                txq->next_desc_to_proc--;
4257}
4258
4259/* Set Tx descriptors fields relevant for CSUM calculation */
4260static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
4261                               int ip_hdr_len, int l4_proto)
4262{
4263        u32 command;
4264
4265        /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
4266         * G_L4_chk, L4_type required only for checksum calculation
4267         */
4268        command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
4269        command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
4270        command |= MVPP2_TXD_IP_CSUM_DISABLE;
4271
4272        if (l3_proto == swab16(ETH_P_IP)) {
4273                command &= ~MVPP2_TXD_IP_CSUM_DISABLE;  /* enable IPv4 csum */
4274                command &= ~MVPP2_TXD_L3_IP6;           /* enable IPv4 */
4275        } else {
4276                command |= MVPP2_TXD_L3_IP6;            /* enable IPv6 */
4277        }
4278
4279        if (l4_proto == IPPROTO_TCP) {
4280                command &= ~MVPP2_TXD_L4_UDP;           /* enable TCP */
4281                command &= ~MVPP2_TXD_L4_CSUM_FRAG;     /* generate L4 csum */
4282        } else if (l4_proto == IPPROTO_UDP) {
4283                command |= MVPP2_TXD_L4_UDP;            /* enable UDP */
4284                command &= ~MVPP2_TXD_L4_CSUM_FRAG;     /* generate L4 csum */
4285        } else {
4286                command |= MVPP2_TXD_L4_CSUM_NOT;
4287        }
4288
4289        return command;
4290}
4291
4292/* Get number of sent descriptors and decrement counter.
4293 * The number of sent descriptors is returned.
4294 * Per-CPU access
4295 */
4296static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
4297                                           struct mvpp2_tx_queue *txq)
4298{
4299        u32 val;
4300
4301        /* Reading status reg resets transmitted descriptor counter */
4302        val = mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(txq->id));
4303
4304        return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
4305                MVPP2_TRANSMITTED_COUNT_OFFSET;
4306}
4307
4308static void mvpp2_txq_sent_counter_clear(void *arg)
4309{
4310        struct mvpp2_port *port = arg;
4311        int queue;
4312
4313        for (queue = 0; queue < txq_number; queue++) {
4314                int id = port->txqs[queue]->id;
4315
4316                mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(id));
4317        }
4318}
4319
4320/* Set max sizes for Tx queues */
4321static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
4322{
4323        u32     val, size, mtu;
4324        int     txq, tx_port_num;
4325
4326        mtu = port->pkt_size * 8;
4327        if (mtu > MVPP2_TXP_MTU_MAX)
4328                mtu = MVPP2_TXP_MTU_MAX;
4329
4330        /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
4331        mtu = 3 * mtu;
4332
4333        /* Indirect access to registers */
4334        tx_port_num = mvpp2_egress_port(port);
4335        mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4336
4337        /* Set MTU */
4338        val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
4339        val &= ~MVPP2_TXP_MTU_MAX;
4340        val |= mtu;
4341        mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
4342
4343        /* TXP token size and all TXQs token size must be larger that MTU */
4344        val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
4345        size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
4346        if (size < mtu) {
4347                size = mtu;
4348                val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
4349                val |= size;
4350                mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4351        }
4352
4353        for (txq = 0; txq < txq_number; txq++) {
4354                val = mvpp2_read(port->priv,
4355                                 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
4356                size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
4357
4358                if (size < mtu) {
4359                        size = mtu;
4360                        val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
4361                        val |= size;
4362                        mvpp2_write(port->priv,
4363                                    MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
4364                                    val);
4365                }
4366        }
4367}
4368
4369/* Set the number of packets that will be received before Rx interrupt
4370 * will be generated by HW.
4371 */
4372static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
4373                                   struct mvpp2_rx_queue *rxq, u32 pkts)
4374{
4375        u32 val;
4376
4377        val = (pkts & MVPP2_OCCUPIED_THRESH_MASK);
4378        mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
4379        mvpp2_write(port->priv, MVPP2_RXQ_THRESH_REG, val);
4380
4381        rxq->pkts_coal = pkts;
4382}
4383
4384/* Set the time delay in usec before Rx interrupt */
4385static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
4386                                   struct mvpp2_rx_queue *rxq, u32 usec)
4387{
4388        u32 val;
4389
4390        val = (port->priv->tclk / USEC_PER_SEC) * usec;
4391        mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
4392
4393        rxq->time_coal = usec;
4394}
4395
4396/* Free Tx queue skbuffs */
4397static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
4398                                struct mvpp2_tx_queue *txq,
4399                                struct mvpp2_txq_pcpu *txq_pcpu, int num)
4400{
4401        int i;
4402
4403        for (i = 0; i < num; i++) {
4404                dma_addr_t buf_phys_addr =
4405                                    txq_pcpu->tx_buffs[txq_pcpu->txq_get_index];
4406                struct sk_buff *skb = txq_pcpu->tx_skb[txq_pcpu->txq_get_index];
4407
4408                mvpp2_txq_inc_get(txq_pcpu);
4409
4410                dma_unmap_single(port->dev->dev.parent, buf_phys_addr,
4411                                 skb_headlen(skb), DMA_TO_DEVICE);
4412                if (!skb)
4413                        continue;
4414                dev_kfree_skb_any(skb);
4415        }
4416}
4417
4418static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
4419                                                        u32 cause)
4420{
4421        int queue = fls(cause) - 1;
4422
4423        return port->rxqs[queue];
4424}
4425
4426static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
4427                                                        u32 cause)
4428{
4429        int queue = fls(cause) - 1;
4430
4431        return port->txqs[queue];
4432}
4433
4434/* Handle end of transmission */
4435static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
4436                           struct mvpp2_txq_pcpu *txq_pcpu)
4437{
4438        struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
4439        int tx_done;
4440
4441        if (txq_pcpu->cpu != smp_processor_id())
4442                netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
4443
4444        tx_done = mvpp2_txq_sent_desc_proc(port, txq);
4445        if (!tx_done)
4446                return;
4447        mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
4448
4449        txq_pcpu->count -= tx_done;
4450
4451        if (netif_tx_queue_stopped(nq))
4452                if (txq_pcpu->size - txq_pcpu->count >= MAX_SKB_FRAGS + 1)
4453                        netif_tx_wake_queue(nq);
4454}
4455
4456static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause)
4457{
4458        struct mvpp2_tx_queue *txq;
4459        struct mvpp2_txq_pcpu *txq_pcpu;
4460        unsigned int tx_todo = 0;
4461
4462        while (cause) {
4463                txq = mvpp2_get_tx_queue(port, cause);
4464                if (!txq)
4465                        break;
4466
4467                txq_pcpu = this_cpu_ptr(txq->pcpu);
4468
4469                if (txq_pcpu->count) {
4470                        mvpp2_txq_done(port, txq, txq_pcpu);
4471                        tx_todo += txq_pcpu->count;
4472                }
4473
4474                cause &= ~(1 << txq->log_id);
4475        }
4476        return tx_todo;
4477}
4478
4479/* Rx/Tx queue initialization/cleanup methods */
4480
4481/* Allocate and initialize descriptors for aggr TXQ */
4482static int mvpp2_aggr_txq_init(struct platform_device *pdev,
4483                               struct mvpp2_tx_queue *aggr_txq,
4484                               int desc_num, int cpu,
4485                               struct mvpp2 *priv)
4486{
4487        /* Allocate memory for TX descriptors */
4488        aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
4489                                desc_num * MVPP2_DESC_ALIGNED_SIZE,
4490                                &aggr_txq->descs_phys, GFP_KERNEL);
4491        if (!aggr_txq->descs)
4492                return -ENOMEM;
4493
4494        aggr_txq->last_desc = aggr_txq->size - 1;
4495
4496        /* Aggr TXQ no reset WA */
4497        aggr_txq->next_desc_to_proc = mvpp2_read(priv,
4498                                                 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
4499
4500        /* Set Tx descriptors queue starting address */
4501        /* indirect access */
4502        mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu),
4503                    aggr_txq->descs_phys);
4504        mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
4505
4506        return 0;
4507}
4508
4509/* Create a specified Rx queue */
4510static int mvpp2_rxq_init(struct mvpp2_port *port,
4511                          struct mvpp2_rx_queue *rxq)
4512
4513{
4514        rxq->size = port->rx_ring_size;
4515
4516        /* Allocate memory for RX descriptors */
4517        rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
4518                                        rxq->size * MVPP2_DESC_ALIGNED_SIZE,
4519                                        &rxq->descs_phys, GFP_KERNEL);
4520        if (!rxq->descs)
4521                return -ENOMEM;
4522
4523        rxq->last_desc = rxq->size - 1;
4524
4525        /* Zero occupied and non-occupied counters - direct access */
4526        mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
4527
4528        /* Set Rx descriptors queue starting address - indirect access */
4529        mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
4530        mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, rxq->descs_phys);
4531        mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
4532        mvpp2_write(port->priv, MVPP2_RXQ_INDEX_REG, 0);
4533
4534        /* Set Offset */
4535        mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
4536
4537        /* Set coalescing pkts and time */
4538        mvpp2_rx_pkts_coal_set(port, rxq, rxq->pkts_coal);
4539        mvpp2_rx_time_coal_set(port, rxq, rxq->time_coal);
4540
4541        /* Add number of descriptors ready for receiving packets */
4542        mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
4543
4544        return 0;
4545}
4546
4547/* Push packets received by the RXQ to BM pool */
4548static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
4549                                struct mvpp2_rx_queue *rxq)
4550{
4551        int rx_received, i;
4552
4553        rx_received = mvpp2_rxq_received(port, rxq->id);
4554        if (!rx_received)
4555                return;
4556
4557        for (i = 0; i < rx_received; i++) {
4558                struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
4559                u32 bm = mvpp2_bm_cookie_build(rx_desc);
4560
4561                mvpp2_pool_refill(port, bm, rx_desc->buf_phys_addr,
4562                                  rx_desc->buf_cookie);
4563        }
4564        mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
4565}
4566
4567/* Cleanup Rx queue */
4568static void mvpp2_rxq_deinit(struct mvpp2_port *port,
4569                             struct mvpp2_rx_queue *rxq)
4570{
4571        mvpp2_rxq_drop_pkts(port, rxq);
4572
4573        if (rxq->descs)
4574                dma_free_coherent(port->dev->dev.parent,
4575                                  rxq->size * MVPP2_DESC_ALIGNED_SIZE,
4576                                  rxq->descs,
4577                                  rxq->descs_phys);
4578
4579        rxq->descs             = NULL;
4580        rxq->last_desc         = 0;
4581        rxq->next_desc_to_proc = 0;
4582        rxq->descs_phys        = 0;
4583
4584        /* Clear Rx descriptors queue starting address and size;
4585         * free descriptor number
4586         */
4587        mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
4588        mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
4589        mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, 0);
4590        mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, 0);
4591}
4592
4593/* Create and initialize a Tx queue */
4594static int mvpp2_txq_init(struct mvpp2_port *port,
4595                          struct mvpp2_tx_queue *txq)
4596{
4597        u32 val;
4598        int cpu, desc, desc_per_txq, tx_port_num;
4599        struct mvpp2_txq_pcpu *txq_pcpu;
4600
4601        txq->size = port->tx_ring_size;
4602
4603        /* Allocate memory for Tx descriptors */
4604        txq->descs = dma_alloc_coherent(port->dev->dev.parent,
4605                                txq->size * MVPP2_DESC_ALIGNED_SIZE,
4606                                &txq->descs_phys, GFP_KERNEL);
4607        if (!txq->descs)
4608                return -ENOMEM;
4609
4610        txq->last_desc = txq->size - 1;
4611
4612        /* Set Tx descriptors queue starting address - indirect access */
4613        mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
4614        mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, txq->descs_phys);
4615        mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, txq->size &
4616                                             MVPP2_TXQ_DESC_SIZE_MASK);
4617        mvpp2_write(port->priv, MVPP2_TXQ_INDEX_REG, 0);
4618        mvpp2_write(port->priv, MVPP2_TXQ_RSVD_CLR_REG,
4619                    txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
4620        val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
4621        val &= ~MVPP2_TXQ_PENDING_MASK;
4622        mvpp2_write(port->priv, MVPP2_TXQ_PENDING_REG, val);
4623
4624        /* Calculate base address in prefetch buffer. We reserve 16 descriptors
4625         * for each existing TXQ.
4626         * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
4627         * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
4628         */
4629        desc_per_txq = 16;
4630        desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
4631               (txq->log_id * desc_per_txq);
4632
4633        mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG,
4634                    MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
4635                    MVPP2_PREF_BUF_THRESH(desc_per_txq/2));
4636
4637        /* WRR / EJP configuration - indirect access */
4638        tx_port_num = mvpp2_egress_port(port);
4639        mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4640
4641        val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
4642        val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
4643        val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
4644        val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
4645        mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
4646
4647        val = MVPP2_TXQ_TOKEN_SIZE_MAX;
4648        mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
4649                    val);
4650
4651        for_each_present_cpu(cpu) {
4652                txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
4653                txq_pcpu->size = txq->size;
4654                txq_pcpu->tx_skb = kmalloc(txq_pcpu->size *
4655                                           sizeof(*txq_pcpu->tx_skb),
4656                                           GFP_KERNEL);
4657                if (!txq_pcpu->tx_skb)
4658                        goto error;
4659
4660                txq_pcpu->tx_buffs = kmalloc(txq_pcpu->size *
4661                                             sizeof(dma_addr_t), GFP_KERNEL);
4662                if (!txq_pcpu->tx_buffs)
4663                        goto error;
4664
4665                txq_pcpu->count = 0;
4666                txq_pcpu->reserved_num = 0;
4667                txq_pcpu->txq_put_index = 0;
4668                txq_pcpu->txq_get_index = 0;
4669        }
4670
4671        return 0;
4672
4673error:
4674        for_each_present_cpu(cpu) {
4675                txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
4676                kfree(txq_pcpu->tx_skb);
4677                kfree(txq_pcpu->tx_buffs);
4678        }
4679
4680        dma_free_coherent(port->dev->dev.parent,
4681                          txq->size * MVPP2_DESC_ALIGNED_SIZE,
4682                          txq->descs, txq->descs_phys);
4683
4684        return -ENOMEM;
4685}
4686
4687/* Free allocated TXQ resources */
4688static void mvpp2_txq_deinit(struct mvpp2_port *port,
4689                             struct mvpp2_tx_queue *txq)
4690{
4691        struct mvpp2_txq_pcpu *txq_pcpu;
4692        int cpu;
4693
4694        for_each_present_cpu(cpu) {
4695                txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
4696                kfree(txq_pcpu->tx_skb);
4697                kfree(txq_pcpu->tx_buffs);
4698        }
4699
4700        if (txq->descs)
4701                dma_free_coherent(port->dev->dev.parent,
4702                                  txq->size * MVPP2_DESC_ALIGNED_SIZE,
4703                                  txq->descs, txq->descs_phys);
4704
4705        txq->descs             = NULL;
4706        txq->last_desc         = 0;
4707        txq->next_desc_to_proc = 0;
4708        txq->descs_phys        = 0;
4709
4710        /* Set minimum bandwidth for disabled TXQs */
4711        mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
4712
4713        /* Set Tx descriptors queue starting address and size */
4714        mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
4715        mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, 0);
4716        mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, 0);
4717}
4718
4719/* Cleanup Tx ports */
4720static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
4721{
4722        struct mvpp2_txq_pcpu *txq_pcpu;
4723        int delay, pending, cpu;
4724        u32 val;
4725
4726        mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
4727        val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
4728        val |= MVPP2_TXQ_DRAIN_EN_MASK;
4729        mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
4730
4731        /* The napi queue has been stopped so wait for all packets
4732         * to be transmitted.
4733         */
4734        delay = 0;
4735        do {
4736                if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
4737                        netdev_warn(port->dev,
4738                                    "port %d: cleaning queue %d timed out\n",
4739                                    port->id, txq->log_id);
4740                        break;
4741                }
4742                mdelay(1);
4743                delay++;
4744
4745                pending = mvpp2_txq_pend_desc_num_get(port, txq);
4746        } while (pending);
4747
4748        val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
4749        mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
4750
4751        for_each_present_cpu(cpu) {
4752                txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
4753
4754                /* Release all packets */
4755                mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
4756
4757                /* Reset queue */
4758                txq_pcpu->count = 0;
4759                txq_pcpu->txq_put_index = 0;
4760                txq_pcpu->txq_get_index = 0;
4761        }
4762}
4763
4764/* Cleanup all Tx queues */
4765static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
4766{
4767        struct mvpp2_tx_queue *txq;
4768        int queue;
4769        u32 val;
4770
4771        val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
4772
4773        /* Reset Tx ports and delete Tx queues */
4774        val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
4775        mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
4776
4777        for (queue = 0; queue < txq_number; queue++) {
4778                txq = port->txqs[queue];
4779                mvpp2_txq_clean(port, txq);
4780                mvpp2_txq_deinit(port, txq);
4781        }
4782
4783        on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
4784
4785        val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
4786        mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
4787}
4788
4789/* Cleanup all Rx queues */
4790static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
4791{
4792        int queue;
4793
4794        for (queue = 0; queue < rxq_number; queue++)
4795                mvpp2_rxq_deinit(port, port->rxqs[queue]);
4796}
4797
4798/* Init all Rx queues for port */
4799static int mvpp2_setup_rxqs(struct mvpp2_port *port)
4800{
4801        int queue, err;
4802
4803        for (queue = 0; queue < rxq_number; queue++) {
4804                err = mvpp2_rxq_init(port, port->rxqs[queue]);
4805                if (err)
4806                        goto err_cleanup;
4807        }
4808        return 0;
4809
4810err_cleanup:
4811        mvpp2_cleanup_rxqs(port);
4812        return err;
4813}
4814
4815/* Init all tx queues for port */
4816static int mvpp2_setup_txqs(struct mvpp2_port *port)
4817{
4818        struct mvpp2_tx_queue *txq;
4819        int queue, err;
4820
4821        for (queue = 0; queue < txq_number; queue++) {
4822                txq = port->txqs[queue];
4823                err = mvpp2_txq_init(port, txq);
4824                if (err)
4825                        goto err_cleanup;
4826        }
4827
4828        on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
4829        return 0;
4830
4831err_cleanup:
4832        mvpp2_cleanup_txqs(port);
4833        return err;
4834}
4835
4836/* The callback for per-port interrupt */
4837static irqreturn_t mvpp2_isr(int irq, void *dev_id)
4838{
4839        struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
4840
4841        mvpp2_interrupts_disable(port);
4842
4843        napi_schedule(&port->napi);
4844
4845        return IRQ_HANDLED;
4846}
4847
4848/* Adjust link */
4849static void mvpp2_link_event(struct net_device *dev)
4850{
4851        struct mvpp2_port *port = netdev_priv(dev);
4852        struct phy_device *phydev = dev->phydev;
4853        int status_change = 0;
4854        u32 val;
4855
4856        if (phydev->link) {
4857                if ((port->speed != phydev->speed) ||
4858                    (port->duplex != phydev->duplex)) {
4859                        u32 val;
4860
4861                        val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4862                        val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
4863                                 MVPP2_GMAC_CONFIG_GMII_SPEED |
4864                                 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
4865                                 MVPP2_GMAC_AN_SPEED_EN |
4866                                 MVPP2_GMAC_AN_DUPLEX_EN);
4867
4868                        if (phydev->duplex)
4869                                val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
4870
4871                        if (phydev->speed == SPEED_1000)
4872                                val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
4873                        else if (phydev->speed == SPEED_100)
4874                                val |= MVPP2_GMAC_CONFIG_MII_SPEED;
4875
4876                        writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4877
4878                        port->duplex = phydev->duplex;
4879                        port->speed  = phydev->speed;
4880                }
4881        }
4882
4883        if (phydev->link != port->link) {
4884                if (!phydev->link) {
4885                        port->duplex = -1;
4886                        port->speed = 0;
4887                }
4888
4889                port->link = phydev->link;
4890                status_change = 1;
4891        }
4892
4893        if (status_change) {
4894                if (phydev->link) {
4895                        val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4896                        val |= (MVPP2_GMAC_FORCE_LINK_PASS |
4897                                MVPP2_GMAC_FORCE_LINK_DOWN);
4898                        writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4899                        mvpp2_egress_enable(port);
4900                        mvpp2_ingress_enable(port);
4901                } else {
4902                        mvpp2_ingress_disable(port);
4903                        mvpp2_egress_disable(port);
4904                }
4905                phy_print_status(phydev);
4906        }
4907}
4908
4909static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
4910{
4911        ktime_t interval;
4912
4913        if (!port_pcpu->timer_scheduled) {
4914                port_pcpu->timer_scheduled = true;
4915                interval = ktime_set(0, MVPP2_TXDONE_HRTIMER_PERIOD_NS);
4916                hrtimer_start(&port_pcpu->tx_done_timer, interval,
4917                              HRTIMER_MODE_REL_PINNED);
4918        }
4919}
4920
4921static void mvpp2_tx_proc_cb(unsigned long data)
4922{
4923        struct net_device *dev = (struct net_device *)data;
4924        struct mvpp2_port *port = netdev_priv(dev);
4925        struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
4926        unsigned int tx_todo, cause;
4927
4928        if (!netif_running(dev))
4929                return;
4930        port_pcpu->timer_scheduled = false;
4931
4932        /* Process all the Tx queues */
4933        cause = (1 << txq_number) - 1;
4934        tx_todo = mvpp2_tx_done(port, cause);
4935
4936        /* Set the timer in case not all the packets were processed */
4937        if (tx_todo)
4938                mvpp2_timer_set(port_pcpu);
4939}
4940
4941static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
4942{
4943        struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
4944                                                         struct mvpp2_port_pcpu,
4945                                                         tx_done_timer);
4946
4947        tasklet_schedule(&port_pcpu->tx_done_tasklet);
4948
4949        return HRTIMER_NORESTART;
4950}
4951
4952/* Main RX/TX processing routines */
4953
4954/* Display more error info */
4955static void mvpp2_rx_error(struct mvpp2_port *port,
4956                           struct mvpp2_rx_desc *rx_desc)
4957{
4958        u32 status = rx_desc->status;
4959
4960        switch (status & MVPP2_RXD_ERR_CODE_MASK) {
4961        case MVPP2_RXD_ERR_CRC:
4962                netdev_err(port->dev, "bad rx status %08x (crc error), size=%d\n",
4963                           status, rx_desc->data_size);
4964                break;
4965        case MVPP2_RXD_ERR_OVERRUN:
4966                netdev_err(port->dev, "bad rx status %08x (overrun error), size=%d\n",
4967                           status, rx_desc->data_size);
4968                break;
4969        case MVPP2_RXD_ERR_RESOURCE:
4970                netdev_err(port->dev, "bad rx status %08x (resource error), size=%d\n",
4971                           status, rx_desc->data_size);
4972                break;
4973        }
4974}
4975
4976/* Handle RX checksum offload */
4977static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
4978                          struct sk_buff *skb)
4979{
4980        if (((status & MVPP2_RXD_L3_IP4) &&
4981             !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
4982            (status & MVPP2_RXD_L3_IP6))
4983                if (((status & MVPP2_RXD_L4_UDP) ||
4984                     (status & MVPP2_RXD_L4_TCP)) &&
4985                     (status & MVPP2_RXD_L4_CSUM_OK)) {
4986                        skb->csum = 0;
4987                        skb->ip_summed = CHECKSUM_UNNECESSARY;
4988                        return;
4989                }
4990
4991        skb->ip_summed = CHECKSUM_NONE;
4992}
4993
4994/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
4995static int mvpp2_rx_refill(struct mvpp2_port *port,
4996                           struct mvpp2_bm_pool *bm_pool,
4997                           u32 bm, int is_recycle)
4998{
4999        struct sk_buff *skb;
5000        dma_addr_t phys_addr;
5001
5002        if (is_recycle &&
5003            (atomic_read(&bm_pool->in_use) < bm_pool->in_use_thresh))
5004                return 0;
5005
5006        /* No recycle or too many buffers are in use, so allocate a new skb */
5007        skb = mvpp2_skb_alloc(port, bm_pool, &phys_addr, GFP_ATOMIC);
5008        if (!skb)
5009                return -ENOMEM;
5010
5011        mvpp2_pool_refill(port, bm, (u32)phys_addr, (u32)skb);
5012        atomic_dec(&bm_pool->in_use);
5013        return 0;
5014}
5015
5016/* Handle tx checksum */
5017static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
5018{
5019        if (skb->ip_summed == CHECKSUM_PARTIAL) {
5020                int ip_hdr_len = 0;
5021                u8 l4_proto;
5022
5023                if (skb->protocol == htons(ETH_P_IP)) {
5024                        struct iphdr *ip4h = ip_hdr(skb);
5025
5026                        /* Calculate IPv4 checksum and L4 checksum */
5027                        ip_hdr_len = ip4h->ihl;
5028                        l4_proto = ip4h->protocol;
5029                } else if (skb->protocol == htons(ETH_P_IPV6)) {
5030                        struct ipv6hdr *ip6h = ipv6_hdr(skb);
5031
5032                        /* Read l4_protocol from one of IPv6 extra headers */
5033                        if (skb_network_header_len(skb) > 0)
5034                                ip_hdr_len = (skb_network_header_len(skb) >> 2);
5035                        l4_proto = ip6h->nexthdr;
5036                } else {
5037                        return MVPP2_TXD_L4_CSUM_NOT;
5038                }
5039
5040                return mvpp2_txq_desc_csum(skb_network_offset(skb),
5041                                skb->protocol, ip_hdr_len, l4_proto);
5042        }
5043
5044        return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
5045}
5046
5047static void mvpp2_buff_hdr_rx(struct mvpp2_port *port,
5048                              struct mvpp2_rx_desc *rx_desc)
5049{
5050        struct mvpp2_buff_hdr *buff_hdr;
5051        struct sk_buff *skb;
5052        u32 rx_status = rx_desc->status;
5053        u32 buff_phys_addr;
5054        u32 buff_virt_addr;
5055        u32 buff_phys_addr_next;
5056        u32 buff_virt_addr_next;
5057        int mc_id;
5058        int pool_id;
5059
5060        pool_id = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5061                   MVPP2_RXD_BM_POOL_ID_OFFS;
5062        buff_phys_addr = rx_desc->buf_phys_addr;
5063        buff_virt_addr = rx_desc->buf_cookie;
5064
5065        do {
5066                skb = (struct sk_buff *)buff_virt_addr;
5067                buff_hdr = (struct mvpp2_buff_hdr *)skb->head;
5068
5069                mc_id = MVPP2_B_HDR_INFO_MC_ID(buff_hdr->info);
5070
5071                buff_phys_addr_next = buff_hdr->next_buff_phys_addr;
5072                buff_virt_addr_next = buff_hdr->next_buff_virt_addr;
5073
5074                /* Release buffer */
5075                mvpp2_bm_pool_mc_put(port, pool_id, buff_phys_addr,
5076                                     buff_virt_addr, mc_id);
5077
5078                buff_phys_addr = buff_phys_addr_next;
5079                buff_virt_addr = buff_virt_addr_next;
5080
5081        } while (!MVPP2_B_HDR_INFO_IS_LAST(buff_hdr->info));
5082}
5083
5084/* Main rx processing */
5085static int mvpp2_rx(struct mvpp2_port *port, int rx_todo,
5086                    struct mvpp2_rx_queue *rxq)
5087{
5088        struct net_device *dev = port->dev;
5089        int rx_received;
5090        int rx_done = 0;
5091        u32 rcvd_pkts = 0;
5092        u32 rcvd_bytes = 0;
5093
5094        /* Get number of received packets and clamp the to-do */
5095        rx_received = mvpp2_rxq_received(port, rxq->id);
5096        if (rx_todo > rx_received)
5097                rx_todo = rx_received;
5098
5099        while (rx_done < rx_todo) {
5100                struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
5101                struct mvpp2_bm_pool *bm_pool;
5102                struct sk_buff *skb;
5103                dma_addr_t phys_addr;
5104                u32 bm, rx_status;
5105                int pool, rx_bytes, err;
5106
5107                rx_done++;
5108                rx_status = rx_desc->status;
5109                rx_bytes = rx_desc->data_size - MVPP2_MH_SIZE;
5110                phys_addr = rx_desc->buf_phys_addr;
5111
5112                bm = mvpp2_bm_cookie_build(rx_desc);
5113                pool = mvpp2_bm_cookie_pool_get(bm);
5114                bm_pool = &port->priv->bm_pools[pool];
5115                /* Check if buffer header is used */
5116                if (rx_status & MVPP2_RXD_BUF_HDR) {
5117                        mvpp2_buff_hdr_rx(port, rx_desc);
5118                        continue;
5119                }
5120
5121                /* In case of an error, release the requested buffer pointer
5122                 * to the Buffer Manager. This request process is controlled
5123                 * by the hardware, and the information about the buffer is
5124                 * comprised by the RX descriptor.
5125                 */
5126                if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
5127                err_drop_frame:
5128                        dev->stats.rx_errors++;
5129                        mvpp2_rx_error(port, rx_desc);
5130                        /* Return the buffer to the pool */
5131                        mvpp2_pool_refill(port, bm, rx_desc->buf_phys_addr,
5132                                          rx_desc->buf_cookie);
5133                        continue;
5134                }
5135
5136                skb = (struct sk_buff *)rx_desc->buf_cookie;
5137
5138                err = mvpp2_rx_refill(port, bm_pool, bm, 0);
5139                if (err) {
5140                        netdev_err(port->dev, "failed to refill BM pools\n");
5141                        goto err_drop_frame;
5142                }
5143
5144                dma_unmap_single(dev->dev.parent, phys_addr,
5145                                 bm_pool->buf_size, DMA_FROM_DEVICE);
5146
5147                rcvd_pkts++;
5148                rcvd_bytes += rx_bytes;
5149                atomic_inc(&bm_pool->in_use);
5150
5151                skb_reserve(skb, MVPP2_MH_SIZE);
5152                skb_put(skb, rx_bytes);
5153                skb->protocol = eth_type_trans(skb, dev);
5154                mvpp2_rx_csum(port, rx_status, skb);
5155
5156                napi_gro_receive(&port->napi, skb);
5157        }
5158
5159        if (rcvd_pkts) {
5160                struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
5161
5162                u64_stats_update_begin(&stats->syncp);
5163                stats->rx_packets += rcvd_pkts;
5164                stats->rx_bytes   += rcvd_bytes;
5165                u64_stats_update_end(&stats->syncp);
5166        }
5167
5168        /* Update Rx queue management counters */
5169        wmb();
5170        mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
5171
5172        return rx_todo;
5173}
5174
5175static inline void
5176tx_desc_unmap_put(struct device *dev, struct mvpp2_tx_queue *txq,
5177                  struct mvpp2_tx_desc *desc)
5178{
5179        dma_unmap_single(dev, desc->buf_phys_addr,
5180                         desc->data_size, DMA_TO_DEVICE);
5181        mvpp2_txq_desc_put(txq);
5182}
5183
5184/* Handle tx fragmentation processing */
5185static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
5186                                 struct mvpp2_tx_queue *aggr_txq,
5187                                 struct mvpp2_tx_queue *txq)
5188{
5189        struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
5190        struct mvpp2_tx_desc *tx_desc;
5191        int i;
5192        dma_addr_t buf_phys_addr;
5193
5194        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
5195                skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5196                void *addr = page_address(frag->page.p) + frag->page_offset;
5197
5198                tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
5199                tx_desc->phys_txq = txq->id;
5200                tx_desc->data_size = frag->size;
5201
5202                buf_phys_addr = dma_map_single(port->dev->dev.parent, addr,
5203                                               tx_desc->data_size,
5204                                               DMA_TO_DEVICE);
5205                if (dma_mapping_error(port->dev->dev.parent, buf_phys_addr)) {
5206                        mvpp2_txq_desc_put(txq);
5207                        goto error;
5208                }
5209
5210                tx_desc->packet_offset = buf_phys_addr & MVPP2_TX_DESC_ALIGN;
5211                tx_desc->buf_phys_addr = buf_phys_addr & (~MVPP2_TX_DESC_ALIGN);
5212
5213                if (i == (skb_shinfo(skb)->nr_frags - 1)) {
5214                        /* Last descriptor */
5215                        tx_desc->command = MVPP2_TXD_L_DESC;
5216                        mvpp2_txq_inc_put(txq_pcpu, skb, tx_desc);
5217                } else {
5218                        /* Descriptor in the middle: Not First, Not Last */
5219                        tx_desc->command = 0;
5220                        mvpp2_txq_inc_put(txq_pcpu, NULL, tx_desc);
5221                }
5222        }
5223
5224        return 0;
5225
5226error:
5227        /* Release all descriptors that were used to map fragments of
5228         * this packet, as well as the corresponding DMA mappings
5229         */
5230        for (i = i - 1; i >= 0; i--) {
5231                tx_desc = txq->descs + i;
5232                tx_desc_unmap_put(port->dev->dev.parent, txq, tx_desc);
5233        }
5234
5235        return -ENOMEM;
5236}
5237
5238/* Main tx processing */
5239static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
5240{
5241        struct mvpp2_port *port = netdev_priv(dev);
5242        struct mvpp2_tx_queue *txq, *aggr_txq;
5243        struct mvpp2_txq_pcpu *txq_pcpu;
5244        struct mvpp2_tx_desc *tx_desc;
5245        dma_addr_t buf_phys_addr;
5246        int frags = 0;
5247        u16 txq_id;
5248        u32 tx_cmd;
5249
5250        txq_id = skb_get_queue_mapping(skb);
5251        txq = port->txqs[txq_id];
5252        txq_pcpu = this_cpu_ptr(txq->pcpu);
5253        aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
5254
5255        frags = skb_shinfo(skb)->nr_frags + 1;
5256
5257        /* Check number of available descriptors */
5258        if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
5259            mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
5260                                             txq_pcpu, frags)) {
5261                frags = 0;
5262                goto out;
5263        }
5264
5265        /* Get a descriptor for the first part of the packet */
5266        tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
5267        tx_desc->phys_txq = txq->id;
5268        tx_desc->data_size = skb_headlen(skb);
5269
5270        buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
5271                                       tx_desc->data_size, DMA_TO_DEVICE);
5272        if (unlikely(dma_mapping_error(dev->dev.parent, buf_phys_addr))) {
5273                mvpp2_txq_desc_put(txq);
5274                frags = 0;
5275                goto out;
5276        }
5277        tx_desc->packet_offset = buf_phys_addr & MVPP2_TX_DESC_ALIGN;
5278        tx_desc->buf_phys_addr = buf_phys_addr & ~MVPP2_TX_DESC_ALIGN;
5279
5280        tx_cmd = mvpp2_skb_tx_csum(port, skb);
5281
5282        if (frags == 1) {
5283                /* First and Last descriptor */
5284                tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
5285                tx_desc->command = tx_cmd;
5286                mvpp2_txq_inc_put(txq_pcpu, skb, tx_desc);
5287        } else {
5288                /* First but not Last */
5289                tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
5290                tx_desc->command = tx_cmd;
5291                mvpp2_txq_inc_put(txq_pcpu, NULL, tx_desc);
5292
5293                /* Continue with other skb fragments */
5294                if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
5295                        tx_desc_unmap_put(port->dev->dev.parent, txq, tx_desc);
5296                        frags = 0;
5297                        goto out;
5298                }
5299        }
5300
5301        txq_pcpu->reserved_num -= frags;
5302        txq_pcpu->count += frags;
5303        aggr_txq->count += frags;
5304
5305        /* Enable transmit */
5306        wmb();
5307        mvpp2_aggr_txq_pend_desc_add(port, frags);
5308
5309        if (txq_pcpu->size - txq_pcpu->count < MAX_SKB_FRAGS + 1) {
5310                struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
5311
5312                netif_tx_stop_queue(nq);
5313        }
5314out:
5315        if (frags > 0) {
5316                struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
5317
5318                u64_stats_update_begin(&stats->syncp);
5319                stats->tx_packets++;
5320                stats->tx_bytes += skb->len;
5321                u64_stats_update_end(&stats->syncp);
5322        } else {
5323                dev->stats.tx_dropped++;
5324                dev_kfree_skb_any(skb);
5325        }
5326
5327        /* Finalize TX processing */
5328        if (txq_pcpu->count >= txq->done_pkts_coal)
5329                mvpp2_txq_done(port, txq, txq_pcpu);
5330
5331        /* Set the timer in case not all frags were processed */
5332        if (txq_pcpu->count <= frags && txq_pcpu->count > 0) {
5333                struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5334
5335                mvpp2_timer_set(port_pcpu);
5336        }
5337
5338        return NETDEV_TX_OK;
5339}
5340
5341static inline void mvpp2_cause_error(struct net_device *dev, int cause)
5342{
5343        if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
5344                netdev_err(dev, "FCS error\n");
5345        if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
5346                netdev_err(dev, "rx fifo overrun error\n");
5347        if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
5348                netdev_err(dev, "tx fifo underrun error\n");
5349}
5350
5351static int mvpp2_poll(struct napi_struct *napi, int budget)
5352{
5353        u32 cause_rx_tx, cause_rx, cause_misc;
5354        int rx_done = 0;
5355        struct mvpp2_port *port = netdev_priv(napi->dev);
5356
5357        /* Rx/Tx cause register
5358         *
5359         * Bits 0-15: each bit indicates received packets on the Rx queue
5360         * (bit 0 is for Rx queue 0).
5361         *
5362         * Bits 16-23: each bit indicates transmitted packets on the Tx queue
5363         * (bit 16 is for Tx queue 0).
5364         *
5365         * Each CPU has its own Rx/Tx cause register
5366         */
5367        cause_rx_tx = mvpp2_read(port->priv,
5368                                 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
5369        cause_rx_tx &= ~MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
5370        cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
5371
5372        if (cause_misc) {
5373                mvpp2_cause_error(port->dev, cause_misc);
5374
5375                /* Clear the cause register */
5376                mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
5377                mvpp2_write(port->priv, MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
5378                            cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
5379        }
5380
5381        cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
5382
5383        /* Process RX packets */
5384        cause_rx |= port->pending_cause_rx;
5385        while (cause_rx && budget > 0) {
5386                int count;
5387                struct mvpp2_rx_queue *rxq;
5388
5389                rxq = mvpp2_get_rx_queue(port, cause_rx);
5390                if (!rxq)
5391                        break;
5392
5393                count = mvpp2_rx(port, budget, rxq);
5394                rx_done += count;
5395                budget -= count;
5396                if (budget > 0) {
5397                        /* Clear the bit associated to this Rx queue
5398                         * so that next iteration will continue from
5399                         * the next Rx queue.
5400                         */
5401                        cause_rx &= ~(1 << rxq->logic_rxq);
5402                }
5403        }
5404
5405        if (budget > 0) {
5406                cause_rx = 0;
5407                napi_complete(napi);
5408
5409                mvpp2_interrupts_enable(port);
5410        }
5411        port->pending_cause_rx = cause_rx;
5412        return rx_done;
5413}
5414
5415/* Set hw internals when starting port */
5416static void mvpp2_start_dev(struct mvpp2_port *port)
5417{
5418        struct net_device *ndev = port->dev;
5419
5420        mvpp2_gmac_max_rx_size_set(port);
5421        mvpp2_txp_max_tx_size_set(port);
5422
5423        napi_enable(&port->napi);
5424
5425        /* Enable interrupts on all CPUs */
5426        mvpp2_interrupts_enable(port);
5427
5428        mvpp2_port_enable(port);
5429        phy_start(ndev->phydev);
5430        netif_tx_start_all_queues(port->dev);
5431}
5432
5433/* Set hw internals when stopping port */
5434static void mvpp2_stop_dev(struct mvpp2_port *port)
5435{
5436        struct net_device *ndev = port->dev;
5437
5438        /* Stop new packets from arriving to RXQs */
5439        mvpp2_ingress_disable(port);
5440
5441        mdelay(10);
5442
5443        /* Disable interrupts on all CPUs */
5444        mvpp2_interrupts_disable(port);
5445
5446        napi_disable(&port->napi);
5447
5448        netif_carrier_off(port->dev);
5449        netif_tx_stop_all_queues(port->dev);
5450
5451        mvpp2_egress_disable(port);
5452        mvpp2_port_disable(port);
5453        phy_stop(ndev->phydev);
5454}
5455
5456/* Return positive if MTU is valid */
5457static inline int mvpp2_check_mtu_valid(struct net_device *dev, int mtu)
5458{
5459        if (mtu < 68) {
5460                netdev_err(dev, "cannot change mtu to less than 68\n");
5461                return -EINVAL;
5462        }
5463
5464        /* 9676 == 9700 - 20 and rounding to 8 */
5465        if (mtu > 9676) {
5466                netdev_info(dev, "illegal MTU value %d, round to 9676\n", mtu);
5467                mtu = 9676;
5468        }
5469
5470        if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
5471                netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
5472                            ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
5473                mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
5474        }
5475
5476        return mtu;
5477}
5478
5479static int mvpp2_check_ringparam_valid(struct net_device *dev,
5480                                       struct ethtool_ringparam *ring)
5481{
5482        u16 new_rx_pending = ring->rx_pending;
5483        u16 new_tx_pending = ring->tx_pending;
5484
5485        if (ring->rx_pending == 0 || ring->tx_pending == 0)
5486                return -EINVAL;
5487
5488        if (ring->rx_pending > MVPP2_MAX_RXD)
5489                new_rx_pending = MVPP2_MAX_RXD;
5490        else if (!IS_ALIGNED(ring->rx_pending, 16))
5491                new_rx_pending = ALIGN(ring->rx_pending, 16);
5492
5493        if (ring->tx_pending > MVPP2_MAX_TXD)
5494                new_tx_pending = MVPP2_MAX_TXD;
5495        else if (!IS_ALIGNED(ring->tx_pending, 32))
5496                new_tx_pending = ALIGN(ring->tx_pending, 32);
5497
5498        if (ring->rx_pending != new_rx_pending) {
5499                netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
5500                            ring->rx_pending, new_rx_pending);
5501                ring->rx_pending = new_rx_pending;
5502        }
5503
5504        if (ring->tx_pending != new_tx_pending) {
5505                netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
5506                            ring->tx_pending, new_tx_pending);
5507                ring->tx_pending = new_tx_pending;
5508        }
5509
5510        return 0;
5511}
5512
5513static void mvpp2_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
5514{
5515        u32 mac_addr_l, mac_addr_m, mac_addr_h;
5516
5517        mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
5518        mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
5519        mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
5520        addr[0] = (mac_addr_h >> 24) & 0xFF;
5521        addr[1] = (mac_addr_h >> 16) & 0xFF;
5522        addr[2] = (mac_addr_h >> 8) & 0xFF;
5523        addr[3] = mac_addr_h & 0xFF;
5524        addr[4] = mac_addr_m & 0xFF;
5525        addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
5526}
5527
5528static int mvpp2_phy_connect(struct mvpp2_port *port)
5529{
5530        struct phy_device *phy_dev;
5531
5532        phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
5533                                 port->phy_interface);
5534        if (!phy_dev) {
5535                netdev_err(port->dev, "cannot connect to phy\n");
5536                return -ENODEV;
5537        }
5538        phy_dev->supported &= PHY_GBIT_FEATURES;
5539        phy_dev->advertising = phy_dev->supported;
5540
5541        port->link    = 0;
5542        port->duplex  = 0;
5543        port->speed   = 0;
5544
5545        return 0;
5546}
5547
5548static void mvpp2_phy_disconnect(struct mvpp2_port *port)
5549{
5550        struct net_device *ndev = port->dev;
5551
5552        phy_disconnect(ndev->phydev);
5553}
5554
5555static int mvpp2_open(struct net_device *dev)
5556{
5557        struct mvpp2_port *port = netdev_priv(dev);
5558        unsigned char mac_bcast[ETH_ALEN] = {
5559                        0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5560        int err;
5561
5562        err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
5563        if (err) {
5564                netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
5565                return err;
5566        }
5567        err = mvpp2_prs_mac_da_accept(port->priv, port->id,
5568                                      dev->dev_addr, true);
5569        if (err) {
5570                netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
5571                return err;
5572        }
5573        err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
5574        if (err) {
5575                netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
5576                return err;
5577        }
5578        err = mvpp2_prs_def_flow(port);
5579        if (err) {
5580                netdev_err(dev, "mvpp2_prs_def_flow failed\n");
5581                return err;
5582        }
5583
5584        /* Allocate the Rx/Tx queues */
5585        err = mvpp2_setup_rxqs(port);
5586        if (err) {
5587                netdev_err(port->dev, "cannot allocate Rx queues\n");
5588                return err;
5589        }
5590
5591        err = mvpp2_setup_txqs(port);
5592        if (err) {
5593                netdev_err(port->dev, "cannot allocate Tx queues\n");
5594                goto err_cleanup_rxqs;
5595        }
5596
5597        err = request_irq(port->irq, mvpp2_isr, 0, dev->name, port);
5598        if (err) {
5599                netdev_err(port->dev, "cannot request IRQ %d\n", port->irq);
5600                goto err_cleanup_txqs;
5601        }
5602
5603        /* In default link is down */
5604        netif_carrier_off(port->dev);
5605
5606        err = mvpp2_phy_connect(port);
5607        if (err < 0)
5608                goto err_free_irq;
5609
5610        /* Unmask interrupts on all CPUs */
5611        on_each_cpu(mvpp2_interrupts_unmask, port, 1);
5612
5613        mvpp2_start_dev(port);
5614
5615        return 0;
5616
5617err_free_irq:
5618        free_irq(port->irq, port);
5619err_cleanup_txqs:
5620        mvpp2_cleanup_txqs(port);
5621err_cleanup_rxqs:
5622        mvpp2_cleanup_rxqs(port);
5623        return err;
5624}
5625
5626static int mvpp2_stop(struct net_device *dev)
5627{
5628        struct mvpp2_port *port = netdev_priv(dev);
5629        struct mvpp2_port_pcpu *port_pcpu;
5630        int cpu;
5631
5632        mvpp2_stop_dev(port);
5633        mvpp2_phy_disconnect(port);
5634
5635        /* Mask interrupts on all CPUs */
5636        on_each_cpu(mvpp2_interrupts_mask, port, 1);
5637
5638        free_irq(port->irq, port);
5639        for_each_present_cpu(cpu) {
5640                port_pcpu = per_cpu_ptr(port->pcpu, cpu);
5641
5642                hrtimer_cancel(&port_pcpu->tx_done_timer);
5643                port_pcpu->timer_scheduled = false;
5644                tasklet_kill(&port_pcpu->tx_done_tasklet);
5645        }
5646        mvpp2_cleanup_rxqs(port);
5647        mvpp2_cleanup_txqs(port);
5648
5649        return 0;
5650}
5651
5652static void mvpp2_set_rx_mode(struct net_device *dev)
5653{
5654        struct mvpp2_port *port = netdev_priv(dev);
5655        struct mvpp2 *priv = port->priv;
5656        struct netdev_hw_addr *ha;
5657        int id = port->id;
5658        bool allmulti = dev->flags & IFF_ALLMULTI;
5659
5660        mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
5661        mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
5662        mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
5663
5664        /* Remove all port->id's mcast enries */
5665        mvpp2_prs_mcast_del_all(priv, id);
5666
5667        if (allmulti && !netdev_mc_empty(dev)) {
5668                netdev_for_each_mc_addr(ha, dev)
5669                        mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
5670        }
5671}
5672
5673static int mvpp2_set_mac_address(struct net_device *dev, void *p)
5674{
5675        struct mvpp2_port *port = netdev_priv(dev);
5676        const struct sockaddr *addr = p;
5677        int err;
5678
5679        if (!is_valid_ether_addr(addr->sa_data)) {
5680                err = -EADDRNOTAVAIL;
5681                goto error;
5682        }
5683
5684        if (!netif_running(dev)) {
5685                err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
5686                if (!err)
5687                        return 0;
5688                /* Reconfigure parser to accept the original MAC address */
5689                err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
5690                if (err)
5691                        goto error;
5692        }
5693
5694        mvpp2_stop_dev(port);
5695
5696        err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
5697        if (!err)
5698                goto out_start;
5699
5700        /* Reconfigure parser accept the original MAC address */
5701        err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
5702        if (err)
5703                goto error;
5704out_start:
5705        mvpp2_start_dev(port);
5706        mvpp2_egress_enable(port);
5707        mvpp2_ingress_enable(port);
5708        return 0;
5709
5710error:
5711        netdev_err(dev, "fail to change MAC address\n");
5712        return err;
5713}
5714
5715static int mvpp2_change_mtu(struct net_device *dev, int mtu)
5716{
5717        struct mvpp2_port *port = netdev_priv(dev);
5718        int err;
5719
5720        mtu = mvpp2_check_mtu_valid(dev, mtu);
5721        if (mtu < 0) {
5722                err = mtu;
5723                goto error;
5724        }
5725
5726        if (!netif_running(dev)) {
5727                err = mvpp2_bm_update_mtu(dev, mtu);
5728                if (!err) {
5729                        port->pkt_size =  MVPP2_RX_PKT_SIZE(mtu);
5730                        return 0;
5731                }
5732
5733                /* Reconfigure BM to the original MTU */
5734                err = mvpp2_bm_update_mtu(dev, dev->mtu);
5735                if (err)
5736                        goto error;
5737        }
5738
5739        mvpp2_stop_dev(port);
5740
5741        err = mvpp2_bm_update_mtu(dev, mtu);
5742        if (!err) {
5743                port->pkt_size =  MVPP2_RX_PKT_SIZE(mtu);
5744                goto out_start;
5745        }
5746
5747        /* Reconfigure BM to the original MTU */
5748        err = mvpp2_bm_update_mtu(dev, dev->mtu);
5749        if (err)
5750                goto error;
5751
5752out_start:
5753        mvpp2_start_dev(port);
5754        mvpp2_egress_enable(port);
5755        mvpp2_ingress_enable(port);
5756
5757        return 0;
5758
5759error:
5760        netdev_err(dev, "fail to change MTU\n");
5761        return err;
5762}
5763
5764static struct rtnl_link_stats64 *
5765mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
5766{
5767        struct mvpp2_port *port = netdev_priv(dev);
5768        unsigned int start;
5769        int cpu;
5770
5771        for_each_possible_cpu(cpu) {
5772                struct mvpp2_pcpu_stats *cpu_stats;
5773                u64 rx_packets;
5774                u64 rx_bytes;
5775                u64 tx_packets;
5776                u64 tx_bytes;
5777
5778                cpu_stats = per_cpu_ptr(port->stats, cpu);
5779                do {
5780                        start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
5781                        rx_packets = cpu_stats->rx_packets;
5782                        rx_bytes   = cpu_stats->rx_bytes;
5783                        tx_packets = cpu_stats->tx_packets;
5784                        tx_bytes   = cpu_stats->tx_bytes;
5785                } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
5786
5787                stats->rx_packets += rx_packets;
5788                stats->rx_bytes   += rx_bytes;
5789                stats->tx_packets += tx_packets;
5790                stats->tx_bytes   += tx_bytes;
5791        }
5792
5793        stats->rx_errors        = dev->stats.rx_errors;
5794        stats->rx_dropped       = dev->stats.rx_dropped;
5795        stats->tx_dropped       = dev->stats.tx_dropped;
5796
5797        return stats;
5798}
5799
5800static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
5801{
5802        int ret;
5803
5804        if (!dev->phydev)
5805                return -ENOTSUPP;
5806
5807        ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
5808        if (!ret)
5809                mvpp2_link_event(dev);
5810
5811        return ret;
5812}
5813
5814/* Ethtool methods */
5815
5816/* Set interrupt coalescing for ethtools */
5817static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
5818                                      struct ethtool_coalesce *c)
5819{
5820        struct mvpp2_port *port = netdev_priv(dev);
5821        int queue;
5822
5823        for (queue = 0; queue < rxq_number; queue++) {
5824                struct mvpp2_rx_queue *rxq = port->rxqs[queue];
5825
5826                rxq->time_coal = c->rx_coalesce_usecs;
5827                rxq->pkts_coal = c->rx_max_coalesced_frames;
5828                mvpp2_rx_pkts_coal_set(port, rxq, rxq->pkts_coal);
5829                mvpp2_rx_time_coal_set(port, rxq, rxq->time_coal);
5830        }
5831
5832        for (queue = 0; queue < txq_number; queue++) {
5833                struct mvpp2_tx_queue *txq = port->txqs[queue];
5834
5835                txq->done_pkts_coal = c->tx_max_coalesced_frames;
5836        }
5837
5838        return 0;
5839}
5840
5841/* get coalescing for ethtools */
5842static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
5843                                      struct ethtool_coalesce *c)
5844{
5845        struct mvpp2_port *port = netdev_priv(dev);
5846
5847        c->rx_coalesce_usecs        = port->rxqs[0]->time_coal;
5848        c->rx_max_coalesced_frames  = port->rxqs[0]->pkts_coal;
5849        c->tx_max_coalesced_frames =  port->txqs[0]->done_pkts_coal;
5850        return 0;
5851}
5852
5853static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
5854                                      struct ethtool_drvinfo *drvinfo)
5855{
5856        strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
5857                sizeof(drvinfo->driver));
5858        strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
5859                sizeof(drvinfo->version));
5860        strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
5861                sizeof(drvinfo->bus_info));
5862}
5863
5864static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
5865                                        struct ethtool_ringparam *ring)
5866{
5867        struct mvpp2_port *port = netdev_priv(dev);
5868
5869        ring->rx_max_pending = MVPP2_MAX_RXD;
5870        ring->tx_max_pending = MVPP2_MAX_TXD;
5871        ring->rx_pending = port->rx_ring_size;
5872        ring->tx_pending = port->tx_ring_size;
5873}
5874
5875static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
5876                                       struct ethtool_ringparam *ring)
5877{
5878        struct mvpp2_port *port = netdev_priv(dev);
5879        u16 prev_rx_ring_size = port->rx_ring_size;
5880        u16 prev_tx_ring_size = port->tx_ring_size;
5881        int err;
5882
5883        err = mvpp2_check_ringparam_valid(dev, ring);
5884        if (err)
5885                return err;
5886
5887        if (!netif_running(dev)) {
5888                port->rx_ring_size = ring->rx_pending;
5889                port->tx_ring_size = ring->tx_pending;
5890                return 0;
5891        }
5892
5893        /* The interface is running, so we have to force a
5894         * reallocation of the queues
5895         */
5896        mvpp2_stop_dev(port);
5897        mvpp2_cleanup_rxqs(port);
5898        mvpp2_cleanup_txqs(port);
5899
5900        port->rx_ring_size = ring->rx_pending;
5901        port->tx_ring_size = ring->tx_pending;
5902
5903        err = mvpp2_setup_rxqs(port);
5904        if (err) {
5905                /* Reallocate Rx queues with the original ring size */
5906                port->rx_ring_size = prev_rx_ring_size;
5907                ring->rx_pending = prev_rx_ring_size;
5908                err = mvpp2_setup_rxqs(port);
5909                if (err)
5910                        goto err_out;
5911        }
5912        err = mvpp2_setup_txqs(port);
5913        if (err) {
5914                /* Reallocate Tx queues with the original ring size */
5915                port->tx_ring_size = prev_tx_ring_size;
5916                ring->tx_pending = prev_tx_ring_size;
5917                err = mvpp2_setup_txqs(port);
5918                if (err)
5919                        goto err_clean_rxqs;
5920        }
5921
5922        mvpp2_start_dev(port);
5923        mvpp2_egress_enable(port);
5924        mvpp2_ingress_enable(port);
5925
5926        return 0;
5927
5928err_clean_rxqs:
5929        mvpp2_cleanup_rxqs(port);
5930err_out:
5931        netdev_err(dev, "fail to change ring parameters");
5932        return err;
5933}
5934
5935/* Device ops */
5936
5937static const struct net_device_ops mvpp2_netdev_ops = {
5938        .ndo_open               = mvpp2_open,
5939        .ndo_stop               = mvpp2_stop,
5940        .ndo_start_xmit         = mvpp2_tx,
5941        .ndo_set_rx_mode        = mvpp2_set_rx_mode,
5942        .ndo_set_mac_address    = mvpp2_set_mac_address,
5943        .ndo_change_mtu         = mvpp2_change_mtu,
5944        .ndo_get_stats64        = mvpp2_get_stats64,
5945        .ndo_do_ioctl           = mvpp2_ioctl,
5946};
5947
5948static const struct ethtool_ops mvpp2_eth_tool_ops = {
5949        .get_link       = ethtool_op_get_link,
5950        .set_coalesce   = mvpp2_ethtool_set_coalesce,
5951        .get_coalesce   = mvpp2_ethtool_get_coalesce,
5952        .get_drvinfo    = mvpp2_ethtool_get_drvinfo,
5953        .get_ringparam  = mvpp2_ethtool_get_ringparam,
5954        .set_ringparam  = mvpp2_ethtool_set_ringparam,
5955        .get_link_ksettings = phy_ethtool_get_link_ksettings,
5956        .set_link_ksettings = phy_ethtool_set_link_ksettings,
5957};
5958
5959/* Driver initialization */
5960
5961static void mvpp2_port_power_up(struct mvpp2_port *port)
5962{
5963        mvpp2_port_mii_set(port);
5964        mvpp2_port_periodic_xon_disable(port);
5965        mvpp2_port_fc_adv_enable(port);
5966        mvpp2_port_reset(port);
5967}
5968
5969/* Initialize port HW */
5970static int mvpp2_port_init(struct mvpp2_port *port)
5971{
5972        struct device *dev = port->dev->dev.parent;
5973        struct mvpp2 *priv = port->priv;
5974        struct mvpp2_txq_pcpu *txq_pcpu;
5975        int queue, cpu, err;
5976
5977        if (port->first_rxq + rxq_number > MVPP2_RXQ_TOTAL_NUM)
5978                return -EINVAL;
5979
5980        /* Disable port */
5981        mvpp2_egress_disable(port);
5982        mvpp2_port_disable(port);
5983
5984        port->txqs = devm_kcalloc(dev, txq_number, sizeof(*port->txqs),
5985                                  GFP_KERNEL);
5986        if (!port->txqs)
5987                return -ENOMEM;
5988
5989        /* Associate physical Tx queues to this port and initialize.
5990         * The mapping is predefined.
5991         */
5992        for (queue = 0; queue < txq_number; queue++) {
5993                int queue_phy_id = mvpp2_txq_phys(port->id, queue);
5994                struct mvpp2_tx_queue *txq;
5995
5996                txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
5997                if (!txq)
5998                        return -ENOMEM;
5999
6000                txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
6001                if (!txq->pcpu) {
6002                        err = -ENOMEM;
6003                        goto err_free_percpu;
6004                }
6005
6006                txq->id = queue_phy_id;
6007                txq->log_id = queue;
6008                txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
6009                for_each_present_cpu(cpu) {
6010                        txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
6011                        txq_pcpu->cpu = cpu;
6012                }
6013
6014                port->txqs[queue] = txq;
6015        }
6016
6017        port->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*port->rxqs),
6018                                  GFP_KERNEL);
6019        if (!port->rxqs) {
6020                err = -ENOMEM;
6021                goto err_free_percpu;
6022        }
6023
6024        /* Allocate and initialize Rx queue for this port */
6025        for (queue = 0; queue < rxq_number; queue++) {
6026                struct mvpp2_rx_queue *rxq;
6027
6028                /* Map physical Rx queue to port's logical Rx queue */
6029                rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
6030                if (!rxq) {
6031                        err = -ENOMEM;
6032                        goto err_free_percpu;
6033                }
6034                /* Map this Rx queue to a physical queue */
6035                rxq->id = port->first_rxq + queue;
6036                rxq->port = port->id;
6037                rxq->logic_rxq = queue;
6038
6039                port->rxqs[queue] = rxq;
6040        }
6041
6042        /* Configure Rx queue group interrupt for this port */
6043        mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(port->id), rxq_number);
6044
6045        /* Create Rx descriptor rings */
6046        for (queue = 0; queue < rxq_number; queue++) {
6047                struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6048
6049                rxq->size = port->rx_ring_size;
6050                rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
6051                rxq->time_coal = MVPP2_RX_COAL_USEC;
6052        }
6053
6054        mvpp2_ingress_disable(port);
6055
6056        /* Port default configuration */
6057        mvpp2_defaults_set(port);
6058
6059        /* Port's classifier configuration */
6060        mvpp2_cls_oversize_rxq_set(port);
6061        mvpp2_cls_port_config(port);
6062
6063        /* Provide an initial Rx packet size */
6064        port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
6065
6066        /* Initialize pools for swf */
6067        err = mvpp2_swf_bm_pool_init(port);
6068        if (err)
6069                goto err_free_percpu;
6070
6071        return 0;
6072
6073err_free_percpu:
6074        for (queue = 0; queue < txq_number; queue++) {
6075                if (!port->txqs[queue])
6076                        continue;
6077                free_percpu(port->txqs[queue]->pcpu);
6078        }
6079        return err;
6080}
6081
6082/* Ports initialization */
6083static int mvpp2_port_probe(struct platform_device *pdev,
6084                            struct device_node *port_node,
6085                            struct mvpp2 *priv,
6086                            int *next_first_rxq)
6087{
6088        struct device_node *phy_node;
6089        struct mvpp2_port *port;
6090        struct mvpp2_port_pcpu *port_pcpu;
6091        struct net_device *dev;
6092        struct resource *res;
6093        const char *dt_mac_addr;
6094        const char *mac_from;
6095        char hw_mac_addr[ETH_ALEN];
6096        u32 id;
6097        int features;
6098        int phy_mode;
6099        int priv_common_regs_num = 2;
6100        int err, i, cpu;
6101
6102        dev = alloc_etherdev_mqs(sizeof(struct mvpp2_port), txq_number,
6103                                 rxq_number);
6104        if (!dev)
6105                return -ENOMEM;
6106
6107        phy_node = of_parse_phandle(port_node, "phy", 0);
6108        if (!phy_node) {
6109                dev_err(&pdev->dev, "missing phy\n");
6110                err = -ENODEV;
6111                goto err_free_netdev;
6112        }
6113
6114        phy_mode = of_get_phy_mode(port_node);
6115        if (phy_mode < 0) {
6116                dev_err(&pdev->dev, "incorrect phy mode\n");
6117                err = phy_mode;
6118                goto err_free_netdev;
6119        }
6120
6121        if (of_property_read_u32(port_node, "port-id", &id)) {
6122                err = -EINVAL;
6123                dev_err(&pdev->dev, "missing port-id value\n");
6124                goto err_free_netdev;
6125        }
6126
6127        dev->tx_queue_len = MVPP2_MAX_TXD;
6128        dev->watchdog_timeo = 5 * HZ;
6129        dev->netdev_ops = &mvpp2_netdev_ops;
6130        dev->ethtool_ops = &mvpp2_eth_tool_ops;
6131
6132        port = netdev_priv(dev);
6133
6134        port->irq = irq_of_parse_and_map(port_node, 0);
6135        if (port->irq <= 0) {
6136                err = -EINVAL;
6137                goto err_free_netdev;
6138        }
6139
6140        if (of_property_read_bool(port_node, "marvell,loopback"))
6141                port->flags |= MVPP2_F_LOOPBACK;
6142
6143        port->priv = priv;
6144        port->id = id;
6145        port->first_rxq = *next_first_rxq;
6146        port->phy_node = phy_node;
6147        port->phy_interface = phy_mode;
6148
6149        res = platform_get_resource(pdev, IORESOURCE_MEM,
6150                                    priv_common_regs_num + id);
6151        port->base = devm_ioremap_resource(&pdev->dev, res);
6152        if (IS_ERR(port->base)) {
6153                err = PTR_ERR(port->base);
6154                goto err_free_irq;
6155        }
6156
6157        /* Alloc per-cpu stats */
6158        port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
6159        if (!port->stats) {
6160                err = -ENOMEM;
6161                goto err_free_irq;
6162        }
6163
6164        dt_mac_addr = of_get_mac_address(port_node);
6165        if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
6166                mac_from = "device tree";
6167                ether_addr_copy(dev->dev_addr, dt_mac_addr);
6168        } else {
6169                mvpp2_get_mac_address(port, hw_mac_addr);
6170                if (is_valid_ether_addr(hw_mac_addr)) {
6171                        mac_from = "hardware";
6172                        ether_addr_copy(dev->dev_addr, hw_mac_addr);
6173                } else {
6174                        mac_from = "random";
6175                        eth_hw_addr_random(dev);
6176                }
6177        }
6178
6179        port->tx_ring_size = MVPP2_MAX_TXD;
6180        port->rx_ring_size = MVPP2_MAX_RXD;
6181        port->dev = dev;
6182        SET_NETDEV_DEV(dev, &pdev->dev);
6183
6184        err = mvpp2_port_init(port);
6185        if (err < 0) {
6186                dev_err(&pdev->dev, "failed to init port %d\n", id);
6187                goto err_free_stats;
6188        }
6189        mvpp2_port_power_up(port);
6190
6191        port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
6192        if (!port->pcpu) {
6193                err = -ENOMEM;
6194                goto err_free_txq_pcpu;
6195        }
6196
6197        for_each_present_cpu(cpu) {
6198                port_pcpu = per_cpu_ptr(port->pcpu, cpu);
6199
6200                hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
6201                             HRTIMER_MODE_REL_PINNED);
6202                port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
6203                port_pcpu->timer_scheduled = false;
6204
6205                tasklet_init(&port_pcpu->tx_done_tasklet, mvpp2_tx_proc_cb,
6206                             (unsigned long)dev);
6207        }
6208
6209        netif_napi_add(dev, &port->napi, mvpp2_poll, NAPI_POLL_WEIGHT);
6210        features = NETIF_F_SG | NETIF_F_IP_CSUM;
6211        dev->features = features | NETIF_F_RXCSUM;
6212        dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
6213        dev->vlan_features |= features;
6214
6215        err = register_netdev(dev);
6216        if (err < 0) {
6217                dev_err(&pdev->dev, "failed to register netdev\n");
6218                goto err_free_port_pcpu;
6219        }
6220        netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
6221
6222        /* Increment the first Rx queue number to be used by the next port */
6223        *next_first_rxq += rxq_number;
6224        priv->port_list[id] = port;
6225        return 0;
6226
6227err_free_port_pcpu:
6228        free_percpu(port->pcpu);
6229err_free_txq_pcpu:
6230        for (i = 0; i < txq_number; i++)
6231                free_percpu(port->txqs[i]->pcpu);
6232err_free_stats:
6233        free_percpu(port->stats);
6234err_free_irq:
6235        irq_dispose_mapping(port->irq);
6236err_free_netdev:
6237        of_node_put(phy_node);
6238        free_netdev(dev);
6239        return err;
6240}
6241
6242/* Ports removal routine */
6243static void mvpp2_port_remove(struct mvpp2_port *port)
6244{
6245        int i;
6246
6247        unregister_netdev(port->dev);
6248        of_node_put(port->phy_node);
6249        free_percpu(port->pcpu);
6250        free_percpu(port->stats);
6251        for (i = 0; i < txq_number; i++)
6252                free_percpu(port->txqs[i]->pcpu);
6253        irq_dispose_mapping(port->irq);
6254        free_netdev(port->dev);
6255}
6256
6257/* Initialize decoding windows */
6258static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
6259                                    struct mvpp2 *priv)
6260{
6261        u32 win_enable;
6262        int i;
6263
6264        for (i = 0; i < 6; i++) {
6265                mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
6266                mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
6267
6268                if (i < 4)
6269                        mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
6270        }
6271
6272        win_enable = 0;
6273
6274        for (i = 0; i < dram->num_cs; i++) {
6275                const struct mbus_dram_window *cs = dram->cs + i;
6276
6277                mvpp2_write(priv, MVPP2_WIN_BASE(i),
6278                            (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
6279                            dram->mbus_dram_target_id);
6280
6281                mvpp2_write(priv, MVPP2_WIN_SIZE(i),
6282                            (cs->size - 1) & 0xffff0000);
6283
6284                win_enable |= (1 << i);
6285        }
6286
6287        mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
6288}
6289
6290/* Initialize Rx FIFO's */
6291static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
6292{
6293        int port;
6294
6295        for (port = 0; port < MVPP2_MAX_PORTS; port++) {
6296                mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
6297                            MVPP2_RX_FIFO_PORT_DATA_SIZE);
6298                mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
6299                            MVPP2_RX_FIFO_PORT_ATTR_SIZE);
6300        }
6301
6302        mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
6303                    MVPP2_RX_FIFO_PORT_MIN_PKT);
6304        mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
6305}
6306
6307/* Initialize network controller common part HW */
6308static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
6309{
6310        const struct mbus_dram_target_info *dram_target_info;
6311        int err, i;
6312        u32 val;
6313
6314        /* Checks for hardware constraints */
6315        if (rxq_number % 4 || (rxq_number > MVPP2_MAX_RXQ) ||
6316            (txq_number > MVPP2_MAX_TXQ)) {
6317                dev_err(&pdev->dev, "invalid queue size parameter\n");
6318                return -EINVAL;
6319        }
6320
6321        /* MBUS windows configuration */
6322        dram_target_info = mv_mbus_dram_info();
6323        if (dram_target_info)
6324                mvpp2_conf_mbus_windows(dram_target_info, priv);
6325
6326        /* Disable HW PHY polling */
6327        val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6328        val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
6329        writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6330
6331        /* Allocate and initialize aggregated TXQs */
6332        priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
6333                                       sizeof(struct mvpp2_tx_queue),
6334                                       GFP_KERNEL);
6335        if (!priv->aggr_txqs)
6336                return -ENOMEM;
6337
6338        for_each_present_cpu(i) {
6339                priv->aggr_txqs[i].id = i;
6340                priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
6341                err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i],
6342                                          MVPP2_AGGR_TXQ_SIZE, i, priv);
6343                if (err < 0)
6344                        return err;
6345        }
6346
6347        /* Rx Fifo Init */
6348        mvpp2_rx_fifo_init(priv);
6349
6350        /* Reset Rx queue group interrupt configuration */
6351        for (i = 0; i < MVPP2_MAX_PORTS; i++)
6352                mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(i), rxq_number);
6353
6354        writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
6355               priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
6356
6357        /* Allow cache snoop when transmiting packets */
6358        mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
6359
6360        /* Buffer Manager initialization */
6361        err = mvpp2_bm_init(pdev, priv);
6362        if (err < 0)
6363                return err;
6364
6365        /* Parser default initialization */
6366        err = mvpp2_prs_default_init(pdev, priv);
6367        if (err < 0)
6368                return err;
6369
6370        /* Classifier default initialization */
6371        mvpp2_cls_init(priv);
6372
6373        return 0;
6374}
6375
6376static int mvpp2_probe(struct platform_device *pdev)
6377{
6378        struct device_node *dn = pdev->dev.of_node;
6379        struct device_node *port_node;
6380        struct mvpp2 *priv;
6381        struct resource *res;
6382        int port_count, first_rxq;
6383        int err;
6384
6385        priv = devm_kzalloc(&pdev->dev, sizeof(struct mvpp2), GFP_KERNEL);
6386        if (!priv)
6387                return -ENOMEM;
6388
6389        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
6390        priv->base = devm_ioremap_resource(&pdev->dev, res);
6391        if (IS_ERR(priv->base))
6392                return PTR_ERR(priv->base);
6393
6394        res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
6395        priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
6396        if (IS_ERR(priv->lms_base))
6397                return PTR_ERR(priv->lms_base);
6398
6399        priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
6400        if (IS_ERR(priv->pp_clk))
6401                return PTR_ERR(priv->pp_clk);
6402        err = clk_prepare_enable(priv->pp_clk);
6403        if (err < 0)
6404                return err;
6405
6406        priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
6407        if (IS_ERR(priv->gop_clk)) {
6408                err = PTR_ERR(priv->gop_clk);
6409                goto err_pp_clk;
6410        }
6411        err = clk_prepare_enable(priv->gop_clk);
6412        if (err < 0)
6413                goto err_pp_clk;
6414
6415        /* Get system's tclk rate */
6416        priv->tclk = clk_get_rate(priv->pp_clk);
6417
6418        /* Initialize network controller */
6419        err = mvpp2_init(pdev, priv);
6420        if (err < 0) {
6421                dev_err(&pdev->dev, "failed to initialize controller\n");
6422                goto err_gop_clk;
6423        }
6424
6425        port_count = of_get_available_child_count(dn);
6426        if (port_count == 0) {
6427                dev_err(&pdev->dev, "no ports enabled\n");
6428                err = -ENODEV;
6429                goto err_gop_clk;
6430        }
6431
6432        priv->port_list = devm_kcalloc(&pdev->dev, port_count,
6433                                      sizeof(struct mvpp2_port *),
6434                                      GFP_KERNEL);
6435        if (!priv->port_list) {
6436                err = -ENOMEM;
6437                goto err_gop_clk;
6438        }
6439
6440        /* Initialize ports */
6441        first_rxq = 0;
6442        for_each_available_child_of_node(dn, port_node) {
6443                err = mvpp2_port_probe(pdev, port_node, priv, &first_rxq);
6444                if (err < 0)
6445                        goto err_gop_clk;
6446        }
6447
6448        platform_set_drvdata(pdev, priv);
6449        return 0;
6450
6451err_gop_clk:
6452        clk_disable_unprepare(priv->gop_clk);
6453err_pp_clk:
6454        clk_disable_unprepare(priv->pp_clk);
6455        return err;
6456}
6457
6458static int mvpp2_remove(struct platform_device *pdev)
6459{
6460        struct mvpp2 *priv = platform_get_drvdata(pdev);
6461        struct device_node *dn = pdev->dev.of_node;
6462        struct device_node *port_node;
6463        int i = 0;
6464
6465        for_each_available_child_of_node(dn, port_node) {
6466                if (priv->port_list[i])
6467                        mvpp2_port_remove(priv->port_list[i]);
6468                i++;
6469        }
6470
6471        for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
6472                struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
6473
6474                mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
6475        }
6476
6477        for_each_present_cpu(i) {
6478                struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
6479
6480                dma_free_coherent(&pdev->dev,
6481                                  MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
6482                                  aggr_txq->descs,
6483                                  aggr_txq->descs_phys);
6484        }
6485
6486        clk_disable_unprepare(priv->pp_clk);
6487        clk_disable_unprepare(priv->gop_clk);
6488
6489        return 0;
6490}
6491
6492static const struct of_device_id mvpp2_match[] = {
6493        { .compatible = "marvell,armada-375-pp2" },
6494        { }
6495};
6496MODULE_DEVICE_TABLE(of, mvpp2_match);
6497
6498static struct platform_driver mvpp2_driver = {
6499        .probe = mvpp2_probe,
6500        .remove = mvpp2_remove,
6501        .driver = {
6502                .name = MVPP2_DRIVER_NAME,
6503                .of_match_table = mvpp2_match,
6504        },
6505};
6506
6507module_platform_driver(mvpp2_driver);
6508
6509MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
6510MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
6511MODULE_LICENSE("GPL v2");
6512