]> git.sur5r.net Git - u-boot/blob - drivers/net/mvpp2.c
net: mvpp2: introduce PPv2.2 HW descriptors and adapt accessors
[u-boot] / drivers / net / mvpp2.c
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  * U-Boot version:
9  * Copyright (C) 2016 Stefan Roese <sr@denx.de>
10  *
11  * This file is licensed under the terms of the GNU General Public
12  * License version 2. This program is licensed "as is" without any
13  * warranty of any kind, whether express or implied.
14  */
15
16 #include <common.h>
17 #include <dm.h>
18 #include <dm/device-internal.h>
19 #include <dm/lists.h>
20 #include <net.h>
21 #include <netdev.h>
22 #include <config.h>
23 #include <malloc.h>
24 #include <asm/io.h>
25 #include <linux/errno.h>
26 #include <phy.h>
27 #include <miiphy.h>
28 #include <watchdog.h>
29 #include <asm/arch/cpu.h>
30 #include <asm/arch/soc.h>
31 #include <linux/compat.h>
32 #include <linux/mbus.h>
33
34 DECLARE_GLOBAL_DATA_PTR;
35
36 /* Some linux -> U-Boot compatibility stuff */
37 #define netdev_err(dev, fmt, args...)           \
38         printf(fmt, ##args)
39 #define netdev_warn(dev, fmt, args...)          \
40         printf(fmt, ##args)
41 #define netdev_info(dev, fmt, args...)          \
42         printf(fmt, ##args)
43 #define netdev_dbg(dev, fmt, args...)           \
44         printf(fmt, ##args)
45
46 #define ETH_ALEN        6               /* Octets in one ethernet addr  */
47
48 #define __verify_pcpu_ptr(ptr)                                          \
49 do {                                                                    \
50         const void __percpu *__vpp_verify = (typeof((ptr) + 0))NULL;    \
51         (void)__vpp_verify;                                             \
52 } while (0)
53
54 #define VERIFY_PERCPU_PTR(__p)                                          \
55 ({                                                                      \
56         __verify_pcpu_ptr(__p);                                         \
57         (typeof(*(__p)) __kernel __force *)(__p);                       \
58 })
59
60 #define per_cpu_ptr(ptr, cpu)   ({ (void)(cpu); VERIFY_PERCPU_PTR(ptr); })
61 #define smp_processor_id()      0
62 #define num_present_cpus()      1
63 #define for_each_present_cpu(cpu)                       \
64         for ((cpu) = 0; (cpu) < 1; (cpu)++)
65
66 #define NET_SKB_PAD     max(32, MVPP2_CPU_D_CACHE_LINE_SIZE)
67
68 #define CONFIG_NR_CPUS          1
69 #define ETH_HLEN                ETHER_HDR_SIZE  /* Total octets in header */
70
71 /* 2(HW hdr) 14(MAC hdr) 4(CRC) 32(extra for cache prefetch) */
72 #define WRAP                    (2 + ETH_HLEN + 4 + 32)
73 #define MTU                     1500
74 #define RX_BUFFER_SIZE          (ALIGN(MTU + WRAP, ARCH_DMA_MINALIGN))
75
76 #define MVPP2_SMI_TIMEOUT                       10000
77
78 /* RX Fifo Registers */
79 #define MVPP2_RX_DATA_FIFO_SIZE_REG(port)       (0x00 + 4 * (port))
80 #define MVPP2_RX_ATTR_FIFO_SIZE_REG(port)       (0x20 + 4 * (port))
81 #define MVPP2_RX_MIN_PKT_SIZE_REG               0x60
82 #define MVPP2_RX_FIFO_INIT_REG                  0x64
83
84 /* RX DMA Top Registers */
85 #define MVPP2_RX_CTRL_REG(port)                 (0x140 + 4 * (port))
86 #define     MVPP2_RX_LOW_LATENCY_PKT_SIZE(s)    (((s) & 0xfff) << 16)
87 #define     MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK   BIT(31)
88 #define MVPP2_POOL_BUF_SIZE_REG(pool)           (0x180 + 4 * (pool))
89 #define     MVPP2_POOL_BUF_SIZE_OFFSET          5
90 #define MVPP2_RXQ_CONFIG_REG(rxq)               (0x800 + 4 * (rxq))
91 #define     MVPP2_SNOOP_PKT_SIZE_MASK           0x1ff
92 #define     MVPP2_SNOOP_BUF_HDR_MASK            BIT(9)
93 #define     MVPP2_RXQ_POOL_SHORT_OFFS           20
94 #define     MVPP2_RXQ_POOL_SHORT_MASK           0x700000
95 #define     MVPP2_RXQ_POOL_LONG_OFFS            24
96 #define     MVPP2_RXQ_POOL_LONG_MASK            0x7000000
97 #define     MVPP2_RXQ_PACKET_OFFSET_OFFS        28
98 #define     MVPP2_RXQ_PACKET_OFFSET_MASK        0x70000000
99 #define     MVPP2_RXQ_DISABLE_MASK              BIT(31)
100
101 /* Parser Registers */
102 #define MVPP2_PRS_INIT_LOOKUP_REG               0x1000
103 #define     MVPP2_PRS_PORT_LU_MAX               0xf
104 #define     MVPP2_PRS_PORT_LU_MASK(port)        (0xff << ((port) * 4))
105 #define     MVPP2_PRS_PORT_LU_VAL(port, val)    ((val) << ((port) * 4))
106 #define MVPP2_PRS_INIT_OFFS_REG(port)           (0x1004 + ((port) & 4))
107 #define     MVPP2_PRS_INIT_OFF_MASK(port)       (0x3f << (((port) % 4) * 8))
108 #define     MVPP2_PRS_INIT_OFF_VAL(port, val)   ((val) << (((port) % 4) * 8))
109 #define MVPP2_PRS_MAX_LOOP_REG(port)            (0x100c + ((port) & 4))
110 #define     MVPP2_PRS_MAX_LOOP_MASK(port)       (0xff << (((port) % 4) * 8))
111 #define     MVPP2_PRS_MAX_LOOP_VAL(port, val)   ((val) << (((port) % 4) * 8))
112 #define MVPP2_PRS_TCAM_IDX_REG                  0x1100
113 #define MVPP2_PRS_TCAM_DATA_REG(idx)            (0x1104 + (idx) * 4)
114 #define     MVPP2_PRS_TCAM_INV_MASK             BIT(31)
115 #define MVPP2_PRS_SRAM_IDX_REG                  0x1200
116 #define MVPP2_PRS_SRAM_DATA_REG(idx)            (0x1204 + (idx) * 4)
117 #define MVPP2_PRS_TCAM_CTRL_REG                 0x1230
118 #define     MVPP2_PRS_TCAM_EN_MASK              BIT(0)
119
120 /* Classifier Registers */
121 #define MVPP2_CLS_MODE_REG                      0x1800
122 #define     MVPP2_CLS_MODE_ACTIVE_MASK          BIT(0)
123 #define MVPP2_CLS_PORT_WAY_REG                  0x1810
124 #define     MVPP2_CLS_PORT_WAY_MASK(port)       (1 << (port))
125 #define MVPP2_CLS_LKP_INDEX_REG                 0x1814
126 #define     MVPP2_CLS_LKP_INDEX_WAY_OFFS        6
127 #define MVPP2_CLS_LKP_TBL_REG                   0x1818
128 #define     MVPP2_CLS_LKP_TBL_RXQ_MASK          0xff
129 #define     MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK    BIT(25)
130 #define MVPP2_CLS_FLOW_INDEX_REG                0x1820
131 #define MVPP2_CLS_FLOW_TBL0_REG                 0x1824
132 #define MVPP2_CLS_FLOW_TBL1_REG                 0x1828
133 #define MVPP2_CLS_FLOW_TBL2_REG                 0x182c
134 #define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port)    (0x1980 + ((port) * 4))
135 #define     MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS     3
136 #define     MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK     0x7
137 #define MVPP2_CLS_SWFWD_P2HQ_REG(port)          (0x19b0 + ((port) * 4))
138 #define MVPP2_CLS_SWFWD_PCTRL_REG               0x19d0
139 #define     MVPP2_CLS_SWFWD_PCTRL_MASK(port)    (1 << (port))
140
141 /* Descriptor Manager Top Registers */
142 #define MVPP2_RXQ_NUM_REG                       0x2040
143 #define MVPP2_RXQ_DESC_ADDR_REG                 0x2044
144 #define MVPP2_RXQ_DESC_SIZE_REG                 0x2048
145 #define     MVPP2_RXQ_DESC_SIZE_MASK            0x3ff0
146 #define MVPP2_RXQ_STATUS_UPDATE_REG(rxq)        (0x3000 + 4 * (rxq))
147 #define     MVPP2_RXQ_NUM_PROCESSED_OFFSET      0
148 #define     MVPP2_RXQ_NUM_NEW_OFFSET            16
149 #define MVPP2_RXQ_STATUS_REG(rxq)               (0x3400 + 4 * (rxq))
150 #define     MVPP2_RXQ_OCCUPIED_MASK             0x3fff
151 #define     MVPP2_RXQ_NON_OCCUPIED_OFFSET       16
152 #define     MVPP2_RXQ_NON_OCCUPIED_MASK         0x3fff0000
153 #define MVPP2_RXQ_THRESH_REG                    0x204c
154 #define     MVPP2_OCCUPIED_THRESH_OFFSET        0
155 #define     MVPP2_OCCUPIED_THRESH_MASK          0x3fff
156 #define MVPP2_RXQ_INDEX_REG                     0x2050
157 #define MVPP2_TXQ_NUM_REG                       0x2080
158 #define MVPP2_TXQ_DESC_ADDR_REG                 0x2084
159 #define MVPP2_TXQ_DESC_SIZE_REG                 0x2088
160 #define     MVPP2_TXQ_DESC_SIZE_MASK            0x3ff0
161 #define MVPP2_AGGR_TXQ_UPDATE_REG               0x2090
162 #define MVPP2_TXQ_THRESH_REG                    0x2094
163 #define     MVPP2_TRANSMITTED_THRESH_OFFSET     16
164 #define     MVPP2_TRANSMITTED_THRESH_MASK       0x3fff0000
165 #define MVPP2_TXQ_INDEX_REG                     0x2098
166 #define MVPP2_TXQ_PREF_BUF_REG                  0x209c
167 #define     MVPP2_PREF_BUF_PTR(desc)            ((desc) & 0xfff)
168 #define     MVPP2_PREF_BUF_SIZE_4               (BIT(12) | BIT(13))
169 #define     MVPP2_PREF_BUF_SIZE_16              (BIT(12) | BIT(14))
170 #define     MVPP2_PREF_BUF_THRESH(val)          ((val) << 17)
171 #define     MVPP2_TXQ_DRAIN_EN_MASK             BIT(31)
172 #define MVPP2_TXQ_PENDING_REG                   0x20a0
173 #define     MVPP2_TXQ_PENDING_MASK              0x3fff
174 #define MVPP2_TXQ_INT_STATUS_REG                0x20a4
175 #define MVPP2_TXQ_SENT_REG(txq)                 (0x3c00 + 4 * (txq))
176 #define     MVPP2_TRANSMITTED_COUNT_OFFSET      16
177 #define     MVPP2_TRANSMITTED_COUNT_MASK        0x3fff0000
178 #define MVPP2_TXQ_RSVD_REQ_REG                  0x20b0
179 #define     MVPP2_TXQ_RSVD_REQ_Q_OFFSET         16
180 #define MVPP2_TXQ_RSVD_RSLT_REG                 0x20b4
181 #define     MVPP2_TXQ_RSVD_RSLT_MASK            0x3fff
182 #define MVPP2_TXQ_RSVD_CLR_REG                  0x20b8
183 #define     MVPP2_TXQ_RSVD_CLR_OFFSET           16
184 #define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu)       (0x2100 + 4 * (cpu))
185 #define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu)       (0x2140 + 4 * (cpu))
186 #define     MVPP2_AGGR_TXQ_DESC_SIZE_MASK       0x3ff0
187 #define MVPP2_AGGR_TXQ_STATUS_REG(cpu)          (0x2180 + 4 * (cpu))
188 #define     MVPP2_AGGR_TXQ_PENDING_MASK         0x3fff
189 #define MVPP2_AGGR_TXQ_INDEX_REG(cpu)           (0x21c0 + 4 * (cpu))
190
191 /* MBUS bridge registers */
192 #define MVPP2_WIN_BASE(w)                       (0x4000 + ((w) << 2))
193 #define MVPP2_WIN_SIZE(w)                       (0x4020 + ((w) << 2))
194 #define MVPP2_WIN_REMAP(w)                      (0x4040 + ((w) << 2))
195 #define MVPP2_BASE_ADDR_ENABLE                  0x4060
196
197 /* Interrupt Cause and Mask registers */
198 #define MVPP2_ISR_RX_THRESHOLD_REG(rxq)         (0x5200 + 4 * (rxq))
199 #define MVPP2_ISR_RXQ_GROUP_REG(rxq)            (0x5400 + 4 * (rxq))
200 #define MVPP2_ISR_ENABLE_REG(port)              (0x5420 + 4 * (port))
201 #define     MVPP2_ISR_ENABLE_INTERRUPT(mask)    ((mask) & 0xffff)
202 #define     MVPP2_ISR_DISABLE_INTERRUPT(mask)   (((mask) << 16) & 0xffff0000)
203 #define MVPP2_ISR_RX_TX_CAUSE_REG(port)         (0x5480 + 4 * (port))
204 #define     MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
205 #define     MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
206 #define     MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK    BIT(24)
207 #define     MVPP2_CAUSE_FCS_ERR_MASK            BIT(25)
208 #define     MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK   BIT(26)
209 #define     MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK   BIT(29)
210 #define     MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK   BIT(30)
211 #define     MVPP2_CAUSE_MISC_SUM_MASK           BIT(31)
212 #define MVPP2_ISR_RX_TX_MASK_REG(port)          (0x54a0 + 4 * (port))
213 #define MVPP2_ISR_PON_RX_TX_MASK_REG            0x54bc
214 #define     MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK     0xffff
215 #define     MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK     0x3fc00000
216 #define     MVPP2_PON_CAUSE_MISC_SUM_MASK               BIT(31)
217 #define MVPP2_ISR_MISC_CAUSE_REG                0x55b0
218
219 /* Buffer Manager registers */
220 #define MVPP2_BM_POOL_BASE_REG(pool)            (0x6000 + ((pool) * 4))
221 #define     MVPP2_BM_POOL_BASE_ADDR_MASK        0xfffff80
222 #define MVPP2_BM_POOL_SIZE_REG(pool)            (0x6040 + ((pool) * 4))
223 #define     MVPP2_BM_POOL_SIZE_MASK             0xfff0
224 #define MVPP2_BM_POOL_READ_PTR_REG(pool)        (0x6080 + ((pool) * 4))
225 #define     MVPP2_BM_POOL_GET_READ_PTR_MASK     0xfff0
226 #define MVPP2_BM_POOL_PTRS_NUM_REG(pool)        (0x60c0 + ((pool) * 4))
227 #define     MVPP2_BM_POOL_PTRS_NUM_MASK         0xfff0
228 #define MVPP2_BM_BPPI_READ_PTR_REG(pool)        (0x6100 + ((pool) * 4))
229 #define MVPP2_BM_BPPI_PTRS_NUM_REG(pool)        (0x6140 + ((pool) * 4))
230 #define     MVPP2_BM_BPPI_PTR_NUM_MASK          0x7ff
231 #define     MVPP2_BM_BPPI_PREFETCH_FULL_MASK    BIT(16)
232 #define MVPP2_BM_POOL_CTRL_REG(pool)            (0x6200 + ((pool) * 4))
233 #define     MVPP2_BM_START_MASK                 BIT(0)
234 #define     MVPP2_BM_STOP_MASK                  BIT(1)
235 #define     MVPP2_BM_STATE_MASK                 BIT(4)
236 #define     MVPP2_BM_LOW_THRESH_OFFS            8
237 #define     MVPP2_BM_LOW_THRESH_MASK            0x7f00
238 #define     MVPP2_BM_LOW_THRESH_VALUE(val)      ((val) << \
239                                                 MVPP2_BM_LOW_THRESH_OFFS)
240 #define     MVPP2_BM_HIGH_THRESH_OFFS           16
241 #define     MVPP2_BM_HIGH_THRESH_MASK           0x7f0000
242 #define     MVPP2_BM_HIGH_THRESH_VALUE(val)     ((val) << \
243                                                 MVPP2_BM_HIGH_THRESH_OFFS)
244 #define MVPP2_BM_INTR_CAUSE_REG(pool)           (0x6240 + ((pool) * 4))
245 #define     MVPP2_BM_RELEASED_DELAY_MASK        BIT(0)
246 #define     MVPP2_BM_ALLOC_FAILED_MASK          BIT(1)
247 #define     MVPP2_BM_BPPE_EMPTY_MASK            BIT(2)
248 #define     MVPP2_BM_BPPE_FULL_MASK             BIT(3)
249 #define     MVPP2_BM_AVAILABLE_BP_LOW_MASK      BIT(4)
250 #define MVPP2_BM_INTR_MASK_REG(pool)            (0x6280 + ((pool) * 4))
251 #define MVPP2_BM_PHY_ALLOC_REG(pool)            (0x6400 + ((pool) * 4))
252 #define     MVPP2_BM_PHY_ALLOC_GRNTD_MASK       BIT(0)
253 #define MVPP2_BM_VIRT_ALLOC_REG                 0x6440
254 #define MVPP2_BM_PHY_RLS_REG(pool)              (0x6480 + ((pool) * 4))
255 #define     MVPP2_BM_PHY_RLS_MC_BUFF_MASK       BIT(0)
256 #define     MVPP2_BM_PHY_RLS_PRIO_EN_MASK       BIT(1)
257 #define     MVPP2_BM_PHY_RLS_GRNTD_MASK         BIT(2)
258 #define MVPP2_BM_VIRT_RLS_REG                   0x64c0
259 #define MVPP2_BM_MC_RLS_REG                     0x64c4
260 #define     MVPP2_BM_MC_ID_MASK                 0xfff
261 #define     MVPP2_BM_FORCE_RELEASE_MASK         BIT(12)
262
263 /* TX Scheduler registers */
264 #define MVPP2_TXP_SCHED_PORT_INDEX_REG          0x8000
265 #define MVPP2_TXP_SCHED_Q_CMD_REG               0x8004
266 #define     MVPP2_TXP_SCHED_ENQ_MASK            0xff
267 #define     MVPP2_TXP_SCHED_DISQ_OFFSET         8
268 #define MVPP2_TXP_SCHED_CMD_1_REG               0x8010
269 #define MVPP2_TXP_SCHED_PERIOD_REG              0x8018
270 #define MVPP2_TXP_SCHED_MTU_REG                 0x801c
271 #define     MVPP2_TXP_MTU_MAX                   0x7FFFF
272 #define MVPP2_TXP_SCHED_REFILL_REG              0x8020
273 #define     MVPP2_TXP_REFILL_TOKENS_ALL_MASK    0x7ffff
274 #define     MVPP2_TXP_REFILL_PERIOD_ALL_MASK    0x3ff00000
275 #define     MVPP2_TXP_REFILL_PERIOD_MASK(v)     ((v) << 20)
276 #define MVPP2_TXP_SCHED_TOKEN_SIZE_REG          0x8024
277 #define     MVPP2_TXP_TOKEN_SIZE_MAX            0xffffffff
278 #define MVPP2_TXQ_SCHED_REFILL_REG(q)           (0x8040 + ((q) << 2))
279 #define     MVPP2_TXQ_REFILL_TOKENS_ALL_MASK    0x7ffff
280 #define     MVPP2_TXQ_REFILL_PERIOD_ALL_MASK    0x3ff00000
281 #define     MVPP2_TXQ_REFILL_PERIOD_MASK(v)     ((v) << 20)
282 #define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q)       (0x8060 + ((q) << 2))
283 #define     MVPP2_TXQ_TOKEN_SIZE_MAX            0x7fffffff
284 #define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q)       (0x8080 + ((q) << 2))
285 #define     MVPP2_TXQ_TOKEN_CNTR_MAX            0xffffffff
286
287 /* TX general registers */
288 #define MVPP2_TX_SNOOP_REG                      0x8800
289 #define MVPP2_TX_PORT_FLUSH_REG                 0x8810
290 #define     MVPP2_TX_PORT_FLUSH_MASK(port)      (1 << (port))
291
292 /* LMS registers */
293 #define MVPP2_SRC_ADDR_MIDDLE                   0x24
294 #define MVPP2_SRC_ADDR_HIGH                     0x28
295 #define MVPP2_PHY_AN_CFG0_REG                   0x34
296 #define     MVPP2_PHY_AN_STOP_SMI0_MASK         BIT(7)
297 #define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG      0x305c
298 #define     MVPP2_EXT_GLOBAL_CTRL_DEFAULT       0x27
299
300 /* Per-port registers */
301 #define MVPP2_GMAC_CTRL_0_REG                   0x0
302 #define      MVPP2_GMAC_PORT_EN_MASK            BIT(0)
303 #define      MVPP2_GMAC_MAX_RX_SIZE_OFFS        2
304 #define      MVPP2_GMAC_MAX_RX_SIZE_MASK        0x7ffc
305 #define      MVPP2_GMAC_MIB_CNTR_EN_MASK        BIT(15)
306 #define MVPP2_GMAC_CTRL_1_REG                   0x4
307 #define      MVPP2_GMAC_PERIODIC_XON_EN_MASK    BIT(1)
308 #define      MVPP2_GMAC_GMII_LB_EN_MASK         BIT(5)
309 #define      MVPP2_GMAC_PCS_LB_EN_BIT           6
310 #define      MVPP2_GMAC_PCS_LB_EN_MASK          BIT(6)
311 #define      MVPP2_GMAC_SA_LOW_OFFS             7
312 #define MVPP2_GMAC_CTRL_2_REG                   0x8
313 #define      MVPP2_GMAC_INBAND_AN_MASK          BIT(0)
314 #define      MVPP2_GMAC_PCS_ENABLE_MASK         BIT(3)
315 #define      MVPP2_GMAC_PORT_RGMII_MASK         BIT(4)
316 #define      MVPP2_GMAC_PORT_RESET_MASK         BIT(6)
317 #define MVPP2_GMAC_AUTONEG_CONFIG               0xc
318 #define      MVPP2_GMAC_FORCE_LINK_DOWN         BIT(0)
319 #define      MVPP2_GMAC_FORCE_LINK_PASS         BIT(1)
320 #define      MVPP2_GMAC_CONFIG_MII_SPEED        BIT(5)
321 #define      MVPP2_GMAC_CONFIG_GMII_SPEED       BIT(6)
322 #define      MVPP2_GMAC_AN_SPEED_EN             BIT(7)
323 #define      MVPP2_GMAC_FC_ADV_EN               BIT(9)
324 #define      MVPP2_GMAC_CONFIG_FULL_DUPLEX      BIT(12)
325 #define      MVPP2_GMAC_AN_DUPLEX_EN            BIT(13)
326 #define MVPP2_GMAC_PORT_FIFO_CFG_1_REG          0x1c
327 #define      MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS     6
328 #define      MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
329 #define      MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v)  (((v) << 6) & \
330                                         MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
331
332 #define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK      0xff
333
334 /* Descriptor ring Macros */
335 #define MVPP2_QUEUE_NEXT_DESC(q, index) \
336         (((index) < (q)->last_desc) ? ((index) + 1) : 0)
337
338 /* SMI: 0xc0054 -> offset 0x54 to lms_base */
339 #define MVPP2_SMI                               0x0054
340 #define     MVPP2_PHY_REG_MASK                  0x1f
341 /* SMI register fields */
342 #define     MVPP2_SMI_DATA_OFFS                 0       /* Data */
343 #define     MVPP2_SMI_DATA_MASK                 (0xffff << MVPP2_SMI_DATA_OFFS)
344 #define     MVPP2_SMI_DEV_ADDR_OFFS             16      /* PHY device address */
345 #define     MVPP2_SMI_REG_ADDR_OFFS             21      /* PHY device reg addr*/
346 #define     MVPP2_SMI_OPCODE_OFFS               26      /* Write/Read opcode */
347 #define     MVPP2_SMI_OPCODE_READ               (1 << MVPP2_SMI_OPCODE_OFFS)
348 #define     MVPP2_SMI_READ_VALID                (1 << 27)       /* Read Valid */
349 #define     MVPP2_SMI_BUSY                      (1 << 28)       /* Busy */
350
351 #define     MVPP2_PHY_ADDR_MASK                 0x1f
352 #define     MVPP2_PHY_REG_MASK                  0x1f
353
354 /* Various constants */
355
356 /* Coalescing */
357 #define MVPP2_TXDONE_COAL_PKTS_THRESH   15
358 #define MVPP2_TXDONE_HRTIMER_PERIOD_NS  1000000UL
359 #define MVPP2_RX_COAL_PKTS              32
360 #define MVPP2_RX_COAL_USEC              100
361
362 /* The two bytes Marvell header. Either contains a special value used
363  * by Marvell switches when a specific hardware mode is enabled (not
364  * supported by this driver) or is filled automatically by zeroes on
365  * the RX side. Those two bytes being at the front of the Ethernet
366  * header, they allow to have the IP header aligned on a 4 bytes
367  * boundary automatically: the hardware skips those two bytes on its
368  * own.
369  */
370 #define MVPP2_MH_SIZE                   2
371 #define MVPP2_ETH_TYPE_LEN              2
372 #define MVPP2_PPPOE_HDR_SIZE            8
373 #define MVPP2_VLAN_TAG_LEN              4
374
375 /* Lbtd 802.3 type */
376 #define MVPP2_IP_LBDT_TYPE              0xfffa
377
378 #define MVPP2_CPU_D_CACHE_LINE_SIZE     32
379 #define MVPP2_TX_CSUM_MAX_SIZE          9800
380
381 /* Timeout constants */
382 #define MVPP2_TX_DISABLE_TIMEOUT_MSEC   1000
383 #define MVPP2_TX_PENDING_TIMEOUT_MSEC   1000
384
385 #define MVPP2_TX_MTU_MAX                0x7ffff
386
387 /* Maximum number of T-CONTs of PON port */
388 #define MVPP2_MAX_TCONT                 16
389
390 /* Maximum number of supported ports */
391 #define MVPP2_MAX_PORTS                 4
392
393 /* Maximum number of TXQs used by single port */
394 #define MVPP2_MAX_TXQ                   8
395
396 /* Maximum number of RXQs used by single port */
397 #define MVPP2_MAX_RXQ                   8
398
399 /* Default number of TXQs in use */
400 #define MVPP2_DEFAULT_TXQ               1
401
402 /* Dfault number of RXQs in use */
403 #define MVPP2_DEFAULT_RXQ               1
404 #define CONFIG_MV_ETH_RXQ               8       /* increment by 8 */
405
406 /* Total number of RXQs available to all ports */
407 #define MVPP2_RXQ_TOTAL_NUM             (MVPP2_MAX_PORTS * MVPP2_MAX_RXQ)
408
409 /* Max number of Rx descriptors */
410 #define MVPP2_MAX_RXD                   16
411
412 /* Max number of Tx descriptors */
413 #define MVPP2_MAX_TXD                   16
414
415 /* Amount of Tx descriptors that can be reserved at once by CPU */
416 #define MVPP2_CPU_DESC_CHUNK            64
417
418 /* Max number of Tx descriptors in each aggregated queue */
419 #define MVPP2_AGGR_TXQ_SIZE             256
420
421 /* Descriptor aligned size */
422 #define MVPP2_DESC_ALIGNED_SIZE         32
423
424 /* Descriptor alignment mask */
425 #define MVPP2_TX_DESC_ALIGN             (MVPP2_DESC_ALIGNED_SIZE - 1)
426
427 /* RX FIFO constants */
428 #define MVPP2_RX_FIFO_PORT_DATA_SIZE    0x2000
429 #define MVPP2_RX_FIFO_PORT_ATTR_SIZE    0x80
430 #define MVPP2_RX_FIFO_PORT_MIN_PKT      0x80
431
432 /* RX buffer constants */
433 #define MVPP2_SKB_SHINFO_SIZE \
434         0
435
436 #define MVPP2_RX_PKT_SIZE(mtu) \
437         ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
438               ETH_HLEN + ETH_FCS_LEN, MVPP2_CPU_D_CACHE_LINE_SIZE)
439
440 #define MVPP2_RX_BUF_SIZE(pkt_size)     ((pkt_size) + NET_SKB_PAD)
441 #define MVPP2_RX_TOTAL_SIZE(buf_size)   ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
442 #define MVPP2_RX_MAX_PKT_SIZE(total_size) \
443         ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
444
445 #define MVPP2_BIT_TO_BYTE(bit)          ((bit) / 8)
446
447 /* IPv6 max L3 address size */
448 #define MVPP2_MAX_L3_ADDR_SIZE          16
449
450 /* Port flags */
451 #define MVPP2_F_LOOPBACK                BIT(0)
452
453 /* Marvell tag types */
454 enum mvpp2_tag_type {
455         MVPP2_TAG_TYPE_NONE = 0,
456         MVPP2_TAG_TYPE_MH   = 1,
457         MVPP2_TAG_TYPE_DSA  = 2,
458         MVPP2_TAG_TYPE_EDSA = 3,
459         MVPP2_TAG_TYPE_VLAN = 4,
460         MVPP2_TAG_TYPE_LAST = 5
461 };
462
463 /* Parser constants */
464 #define MVPP2_PRS_TCAM_SRAM_SIZE        256
465 #define MVPP2_PRS_TCAM_WORDS            6
466 #define MVPP2_PRS_SRAM_WORDS            4
467 #define MVPP2_PRS_FLOW_ID_SIZE          64
468 #define MVPP2_PRS_FLOW_ID_MASK          0x3f
469 #define MVPP2_PRS_TCAM_ENTRY_INVALID    1
470 #define MVPP2_PRS_TCAM_DSA_TAGGED_BIT   BIT(5)
471 #define MVPP2_PRS_IPV4_HEAD             0x40
472 #define MVPP2_PRS_IPV4_HEAD_MASK        0xf0
473 #define MVPP2_PRS_IPV4_MC               0xe0
474 #define MVPP2_PRS_IPV4_MC_MASK          0xf0
475 #define MVPP2_PRS_IPV4_BC_MASK          0xff
476 #define MVPP2_PRS_IPV4_IHL              0x5
477 #define MVPP2_PRS_IPV4_IHL_MASK         0xf
478 #define MVPP2_PRS_IPV6_MC               0xff
479 #define MVPP2_PRS_IPV6_MC_MASK          0xff
480 #define MVPP2_PRS_IPV6_HOP_MASK         0xff
481 #define MVPP2_PRS_TCAM_PROTO_MASK       0xff
482 #define MVPP2_PRS_TCAM_PROTO_MASK_L     0x3f
483 #define MVPP2_PRS_DBL_VLANS_MAX         100
484
485 /* Tcam structure:
486  * - lookup ID - 4 bits
487  * - port ID - 1 byte
488  * - additional information - 1 byte
489  * - header data - 8 bytes
490  * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
491  */
492 #define MVPP2_PRS_AI_BITS                       8
493 #define MVPP2_PRS_PORT_MASK                     0xff
494 #define MVPP2_PRS_LU_MASK                       0xf
495 #define MVPP2_PRS_TCAM_DATA_BYTE(offs)          \
496                                     (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
497 #define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)       \
498                                               (((offs) * 2) - ((offs) % 2)  + 2)
499 #define MVPP2_PRS_TCAM_AI_BYTE                  16
500 #define MVPP2_PRS_TCAM_PORT_BYTE                17
501 #define MVPP2_PRS_TCAM_LU_BYTE                  20
502 #define MVPP2_PRS_TCAM_EN_OFFS(offs)            ((offs) + 2)
503 #define MVPP2_PRS_TCAM_INV_WORD                 5
504 /* Tcam entries ID */
505 #define MVPP2_PE_DROP_ALL               0
506 #define MVPP2_PE_FIRST_FREE_TID         1
507 #define MVPP2_PE_LAST_FREE_TID          (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
508 #define MVPP2_PE_IP6_EXT_PROTO_UN       (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
509 #define MVPP2_PE_MAC_MC_IP6             (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
510 #define MVPP2_PE_IP6_ADDR_UN            (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
511 #define MVPP2_PE_IP4_ADDR_UN            (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
512 #define MVPP2_PE_LAST_DEFAULT_FLOW      (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
513 #define MVPP2_PE_FIRST_DEFAULT_FLOW     (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
514 #define MVPP2_PE_EDSA_TAGGED            (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
515 #define MVPP2_PE_EDSA_UNTAGGED          (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
516 #define MVPP2_PE_DSA_TAGGED             (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
517 #define MVPP2_PE_DSA_UNTAGGED           (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
518 #define MVPP2_PE_ETYPE_EDSA_TAGGED      (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
519 #define MVPP2_PE_ETYPE_EDSA_UNTAGGED    (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
520 #define MVPP2_PE_ETYPE_DSA_TAGGED       (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
521 #define MVPP2_PE_ETYPE_DSA_UNTAGGED     (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
522 #define MVPP2_PE_MH_DEFAULT             (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
523 #define MVPP2_PE_DSA_DEFAULT            (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
524 #define MVPP2_PE_IP6_PROTO_UN           (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
525 #define MVPP2_PE_IP4_PROTO_UN           (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
526 #define MVPP2_PE_ETH_TYPE_UN            (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
527 #define MVPP2_PE_VLAN_DBL               (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
528 #define MVPP2_PE_VLAN_NONE              (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
529 #define MVPP2_PE_MAC_MC_ALL             (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
530 #define MVPP2_PE_MAC_PROMISCUOUS        (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
531 #define MVPP2_PE_MAC_NON_PROMISCUOUS    (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
532
533 /* Sram structure
534  * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
535  */
536 #define MVPP2_PRS_SRAM_RI_OFFS                  0
537 #define MVPP2_PRS_SRAM_RI_WORD                  0
538 #define MVPP2_PRS_SRAM_RI_CTRL_OFFS             32
539 #define MVPP2_PRS_SRAM_RI_CTRL_WORD             1
540 #define MVPP2_PRS_SRAM_RI_CTRL_BITS             32
541 #define MVPP2_PRS_SRAM_SHIFT_OFFS               64
542 #define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT           72
543 #define MVPP2_PRS_SRAM_UDF_OFFS                 73
544 #define MVPP2_PRS_SRAM_UDF_BITS                 8
545 #define MVPP2_PRS_SRAM_UDF_MASK                 0xff
546 #define MVPP2_PRS_SRAM_UDF_SIGN_BIT             81
547 #define MVPP2_PRS_SRAM_UDF_TYPE_OFFS            82
548 #define MVPP2_PRS_SRAM_UDF_TYPE_MASK            0x7
549 #define MVPP2_PRS_SRAM_UDF_TYPE_L3              1
550 #define MVPP2_PRS_SRAM_UDF_TYPE_L4              4
551 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS        85
552 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK        0x3
553 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD         1
554 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD     2
555 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD     3
556 #define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS          87
557 #define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS          2
558 #define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK          0x3
559 #define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD           0
560 #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD       2
561 #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD       3
562 #define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS         89
563 #define MVPP2_PRS_SRAM_AI_OFFS                  90
564 #define MVPP2_PRS_SRAM_AI_CTRL_OFFS             98
565 #define MVPP2_PRS_SRAM_AI_CTRL_BITS             8
566 #define MVPP2_PRS_SRAM_AI_MASK                  0xff
567 #define MVPP2_PRS_SRAM_NEXT_LU_OFFS             106
568 #define MVPP2_PRS_SRAM_NEXT_LU_MASK             0xf
569 #define MVPP2_PRS_SRAM_LU_DONE_BIT              110
570 #define MVPP2_PRS_SRAM_LU_GEN_BIT               111
571
572 /* Sram result info bits assignment */
573 #define MVPP2_PRS_RI_MAC_ME_MASK                0x1
574 #define MVPP2_PRS_RI_DSA_MASK                   0x2
575 #define MVPP2_PRS_RI_VLAN_MASK                  (BIT(2) | BIT(3))
576 #define MVPP2_PRS_RI_VLAN_NONE                  0x0
577 #define MVPP2_PRS_RI_VLAN_SINGLE                BIT(2)
578 #define MVPP2_PRS_RI_VLAN_DOUBLE                BIT(3)
579 #define MVPP2_PRS_RI_VLAN_TRIPLE                (BIT(2) | BIT(3))
580 #define MVPP2_PRS_RI_CPU_CODE_MASK              0x70
581 #define MVPP2_PRS_RI_CPU_CODE_RX_SPEC           BIT(4)
582 #define MVPP2_PRS_RI_L2_CAST_MASK               (BIT(9) | BIT(10))
583 #define MVPP2_PRS_RI_L2_UCAST                   0x0
584 #define MVPP2_PRS_RI_L2_MCAST                   BIT(9)
585 #define MVPP2_PRS_RI_L2_BCAST                   BIT(10)
586 #define MVPP2_PRS_RI_PPPOE_MASK                 0x800
587 #define MVPP2_PRS_RI_L3_PROTO_MASK              (BIT(12) | BIT(13) | BIT(14))
588 #define MVPP2_PRS_RI_L3_UN                      0x0
589 #define MVPP2_PRS_RI_L3_IP4                     BIT(12)
590 #define MVPP2_PRS_RI_L3_IP4_OPT                 BIT(13)
591 #define MVPP2_PRS_RI_L3_IP4_OTHER               (BIT(12) | BIT(13))
592 #define MVPP2_PRS_RI_L3_IP6                     BIT(14)
593 #define MVPP2_PRS_RI_L3_IP6_EXT                 (BIT(12) | BIT(14))
594 #define MVPP2_PRS_RI_L3_ARP                     (BIT(13) | BIT(14))
595 #define MVPP2_PRS_RI_L3_ADDR_MASK               (BIT(15) | BIT(16))
596 #define MVPP2_PRS_RI_L3_UCAST                   0x0
597 #define MVPP2_PRS_RI_L3_MCAST                   BIT(15)
598 #define MVPP2_PRS_RI_L3_BCAST                   (BIT(15) | BIT(16))
599 #define MVPP2_PRS_RI_IP_FRAG_MASK               0x20000
600 #define MVPP2_PRS_RI_UDF3_MASK                  0x300000
601 #define MVPP2_PRS_RI_UDF3_RX_SPECIAL            BIT(21)
602 #define MVPP2_PRS_RI_L4_PROTO_MASK              0x1c00000
603 #define MVPP2_PRS_RI_L4_TCP                     BIT(22)
604 #define MVPP2_PRS_RI_L4_UDP                     BIT(23)
605 #define MVPP2_PRS_RI_L4_OTHER                   (BIT(22) | BIT(23))
606 #define MVPP2_PRS_RI_UDF7_MASK                  0x60000000
607 #define MVPP2_PRS_RI_UDF7_IP6_LITE              BIT(29)
608 #define MVPP2_PRS_RI_DROP_MASK                  0x80000000
609
610 /* Sram additional info bits assignment */
611 #define MVPP2_PRS_IPV4_DIP_AI_BIT               BIT(0)
612 #define MVPP2_PRS_IPV6_NO_EXT_AI_BIT            BIT(0)
613 #define MVPP2_PRS_IPV6_EXT_AI_BIT               BIT(1)
614 #define MVPP2_PRS_IPV6_EXT_AH_AI_BIT            BIT(2)
615 #define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT        BIT(3)
616 #define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT         BIT(4)
617 #define MVPP2_PRS_SINGLE_VLAN_AI                0
618 #define MVPP2_PRS_DBL_VLAN_AI_BIT               BIT(7)
619
620 /* DSA/EDSA type */
621 #define MVPP2_PRS_TAGGED                true
622 #define MVPP2_PRS_UNTAGGED              false
623 #define MVPP2_PRS_EDSA                  true
624 #define MVPP2_PRS_DSA                   false
625
626 /* MAC entries, shadow udf */
627 enum mvpp2_prs_udf {
628         MVPP2_PRS_UDF_MAC_DEF,
629         MVPP2_PRS_UDF_MAC_RANGE,
630         MVPP2_PRS_UDF_L2_DEF,
631         MVPP2_PRS_UDF_L2_DEF_COPY,
632         MVPP2_PRS_UDF_L2_USER,
633 };
634
635 /* Lookup ID */
636 enum mvpp2_prs_lookup {
637         MVPP2_PRS_LU_MH,
638         MVPP2_PRS_LU_MAC,
639         MVPP2_PRS_LU_DSA,
640         MVPP2_PRS_LU_VLAN,
641         MVPP2_PRS_LU_L2,
642         MVPP2_PRS_LU_PPPOE,
643         MVPP2_PRS_LU_IP4,
644         MVPP2_PRS_LU_IP6,
645         MVPP2_PRS_LU_FLOWS,
646         MVPP2_PRS_LU_LAST,
647 };
648
649 /* L3 cast enum */
650 enum mvpp2_prs_l3_cast {
651         MVPP2_PRS_L3_UNI_CAST,
652         MVPP2_PRS_L3_MULTI_CAST,
653         MVPP2_PRS_L3_BROAD_CAST
654 };
655
656 /* Classifier constants */
657 #define MVPP2_CLS_FLOWS_TBL_SIZE        512
658 #define MVPP2_CLS_FLOWS_TBL_DATA_WORDS  3
659 #define MVPP2_CLS_LKP_TBL_SIZE          64
660
661 /* BM constants */
662 #define MVPP2_BM_POOLS_NUM              1
663 #define MVPP2_BM_LONG_BUF_NUM           16
664 #define MVPP2_BM_SHORT_BUF_NUM          16
665 #define MVPP2_BM_POOL_SIZE_MAX          (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
666 #define MVPP2_BM_POOL_PTR_ALIGN         128
667 #define MVPP2_BM_SWF_LONG_POOL(port)    0
668
669 /* BM cookie (32 bits) definition */
670 #define MVPP2_BM_COOKIE_POOL_OFFS       8
671 #define MVPP2_BM_COOKIE_CPU_OFFS        24
672
673 /* BM short pool packet size
674  * These value assure that for SWF the total number
675  * of bytes allocated for each buffer will be 512
676  */
677 #define MVPP2_BM_SHORT_PKT_SIZE         MVPP2_RX_MAX_PKT_SIZE(512)
678
679 enum mvpp2_bm_type {
680         MVPP2_BM_FREE,
681         MVPP2_BM_SWF_LONG,
682         MVPP2_BM_SWF_SHORT
683 };
684
685 /* Definitions */
686
687 /* Shared Packet Processor resources */
688 struct mvpp2 {
689         /* Shared registers' base addresses */
690         void __iomem *base;
691         void __iomem *lms_base;
692
693         /* List of pointers to port structures */
694         struct mvpp2_port **port_list;
695
696         /* Aggregated TXQs */
697         struct mvpp2_tx_queue *aggr_txqs;
698
699         /* BM pools */
700         struct mvpp2_bm_pool *bm_pools;
701
702         /* PRS shadow table */
703         struct mvpp2_prs_shadow *prs_shadow;
704         /* PRS auxiliary table for double vlan entries control */
705         bool *prs_double_vlans;
706
707         /* Tclk value */
708         u32 tclk;
709
710         /* HW version */
711         enum { MVPP21, MVPP22 } hw_version;
712
713         struct mii_dev *bus;
714 };
715
716 struct mvpp2_pcpu_stats {
717         u64     rx_packets;
718         u64     rx_bytes;
719         u64     tx_packets;
720         u64     tx_bytes;
721 };
722
723 struct mvpp2_port {
724         u8 id;
725
726         int irq;
727
728         struct mvpp2 *priv;
729
730         /* Per-port registers' base address */
731         void __iomem *base;
732
733         struct mvpp2_rx_queue **rxqs;
734         struct mvpp2_tx_queue **txqs;
735
736         int pkt_size;
737
738         u32 pending_cause_rx;
739
740         /* Per-CPU port control */
741         struct mvpp2_port_pcpu __percpu *pcpu;
742
743         /* Flags */
744         unsigned long flags;
745
746         u16 tx_ring_size;
747         u16 rx_ring_size;
748         struct mvpp2_pcpu_stats __percpu *stats;
749
750         struct phy_device *phy_dev;
751         phy_interface_t phy_interface;
752         int phy_node;
753         int phyaddr;
754         int init;
755         unsigned int link;
756         unsigned int duplex;
757         unsigned int speed;
758
759         struct mvpp2_bm_pool *pool_long;
760         struct mvpp2_bm_pool *pool_short;
761
762         /* Index of first port's physical RXQ */
763         u8 first_rxq;
764
765         u8 dev_addr[ETH_ALEN];
766 };
767
768 /* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
769  * layout of the transmit and reception DMA descriptors, and their
770  * layout is therefore defined by the hardware design
771  */
772
773 #define MVPP2_TXD_L3_OFF_SHIFT          0
774 #define MVPP2_TXD_IP_HLEN_SHIFT         8
775 #define MVPP2_TXD_L4_CSUM_FRAG          BIT(13)
776 #define MVPP2_TXD_L4_CSUM_NOT           BIT(14)
777 #define MVPP2_TXD_IP_CSUM_DISABLE       BIT(15)
778 #define MVPP2_TXD_PADDING_DISABLE       BIT(23)
779 #define MVPP2_TXD_L4_UDP                BIT(24)
780 #define MVPP2_TXD_L3_IP6                BIT(26)
781 #define MVPP2_TXD_L_DESC                BIT(28)
782 #define MVPP2_TXD_F_DESC                BIT(29)
783
784 #define MVPP2_RXD_ERR_SUMMARY           BIT(15)
785 #define MVPP2_RXD_ERR_CODE_MASK         (BIT(13) | BIT(14))
786 #define MVPP2_RXD_ERR_CRC               0x0
787 #define MVPP2_RXD_ERR_OVERRUN           BIT(13)
788 #define MVPP2_RXD_ERR_RESOURCE          (BIT(13) | BIT(14))
789 #define MVPP2_RXD_BM_POOL_ID_OFFS       16
790 #define MVPP2_RXD_BM_POOL_ID_MASK       (BIT(16) | BIT(17) | BIT(18))
791 #define MVPP2_RXD_HWF_SYNC              BIT(21)
792 #define MVPP2_RXD_L4_CSUM_OK            BIT(22)
793 #define MVPP2_RXD_IP4_HEADER_ERR        BIT(24)
794 #define MVPP2_RXD_L4_TCP                BIT(25)
795 #define MVPP2_RXD_L4_UDP                BIT(26)
796 #define MVPP2_RXD_L3_IP4                BIT(28)
797 #define MVPP2_RXD_L3_IP6                BIT(30)
798 #define MVPP2_RXD_BUF_HDR               BIT(31)
799
800 /* HW TX descriptor for PPv2.1 */
801 struct mvpp21_tx_desc {
802         u32 command;            /* Options used by HW for packet transmitting.*/
803         u8  packet_offset;      /* the offset from the buffer beginning */
804         u8  phys_txq;           /* destination queue ID                 */
805         u16 data_size;          /* data size of transmitted packet in bytes */
806         u32 buf_dma_addr;       /* physical addr of transmitted buffer  */
807         u32 buf_cookie;         /* cookie for access to TX buffer in tx path */
808         u32 reserved1[3];       /* hw_cmd (for future use, BM, PON, PNC) */
809         u32 reserved2;          /* reserved (for future use)            */
810 };
811
812 /* HW RX descriptor for PPv2.1 */
813 struct mvpp21_rx_desc {
814         u32 status;             /* info about received packet           */
815         u16 reserved1;          /* parser_info (for future use, PnC)    */
816         u16 data_size;          /* size of received packet in bytes     */
817         u32 buf_dma_addr;       /* physical address of the buffer       */
818         u32 buf_cookie;         /* cookie for access to RX buffer in rx path */
819         u16 reserved2;          /* gem_port_id (for future use, PON)    */
820         u16 reserved3;          /* csum_l4 (for future use, PnC)        */
821         u8  reserved4;          /* bm_qset (for future use, BM)         */
822         u8  reserved5;
823         u16 reserved6;          /* classify_info (for future use, PnC)  */
824         u32 reserved7;          /* flow_id (for future use, PnC) */
825         u32 reserved8;
826 };
827
828 /* HW TX descriptor for PPv2.2 */
829 struct mvpp22_tx_desc {
830         u32 command;
831         u8  packet_offset;
832         u8  phys_txq;
833         u16 data_size;
834         u64 reserved1;
835         u64 buf_dma_addr_ptp;
836         u64 buf_cookie_misc;
837 };
838
839 /* HW RX descriptor for PPv2.2 */
840 struct mvpp22_rx_desc {
841         u32 status;
842         u16 reserved1;
843         u16 data_size;
844         u32 reserved2;
845         u32 reserved3;
846         u64 buf_dma_addr_key_hash;
847         u64 buf_cookie_misc;
848 };
849
850 /* Opaque type used by the driver to manipulate the HW TX and RX
851  * descriptors
852  */
853 struct mvpp2_tx_desc {
854         union {
855                 struct mvpp21_tx_desc pp21;
856                 struct mvpp22_tx_desc pp22;
857         };
858 };
859
860 struct mvpp2_rx_desc {
861         union {
862                 struct mvpp21_rx_desc pp21;
863                 struct mvpp22_rx_desc pp22;
864         };
865 };
866
867 /* Per-CPU Tx queue control */
868 struct mvpp2_txq_pcpu {
869         int cpu;
870
871         /* Number of Tx DMA descriptors in the descriptor ring */
872         int size;
873
874         /* Number of currently used Tx DMA descriptor in the
875          * descriptor ring
876          */
877         int count;
878
879         /* Number of Tx DMA descriptors reserved for each CPU */
880         int reserved_num;
881
882         /* Index of last TX DMA descriptor that was inserted */
883         int txq_put_index;
884
885         /* Index of the TX DMA descriptor to be cleaned up */
886         int txq_get_index;
887 };
888
889 struct mvpp2_tx_queue {
890         /* Physical number of this Tx queue */
891         u8 id;
892
893         /* Logical number of this Tx queue */
894         u8 log_id;
895
896         /* Number of Tx DMA descriptors in the descriptor ring */
897         int size;
898
899         /* Number of currently used Tx DMA descriptor in the descriptor ring */
900         int count;
901
902         /* Per-CPU control of physical Tx queues */
903         struct mvpp2_txq_pcpu __percpu *pcpu;
904
905         u32 done_pkts_coal;
906
907         /* Virtual address of thex Tx DMA descriptors array */
908         struct mvpp2_tx_desc *descs;
909
910         /* DMA address of the Tx DMA descriptors array */
911         dma_addr_t descs_dma;
912
913         /* Index of the last Tx DMA descriptor */
914         int last_desc;
915
916         /* Index of the next Tx DMA descriptor to process */
917         int next_desc_to_proc;
918 };
919
920 struct mvpp2_rx_queue {
921         /* RX queue number, in the range 0-31 for physical RXQs */
922         u8 id;
923
924         /* Num of rx descriptors in the rx descriptor ring */
925         int size;
926
927         u32 pkts_coal;
928         u32 time_coal;
929
930         /* Virtual address of the RX DMA descriptors array */
931         struct mvpp2_rx_desc *descs;
932
933         /* DMA address of the RX DMA descriptors array */
934         dma_addr_t descs_dma;
935
936         /* Index of the last RX DMA descriptor */
937         int last_desc;
938
939         /* Index of the next RX DMA descriptor to process */
940         int next_desc_to_proc;
941
942         /* ID of port to which physical RXQ is mapped */
943         int port;
944
945         /* Port's logic RXQ number to which physical RXQ is mapped */
946         int logic_rxq;
947 };
948
949 union mvpp2_prs_tcam_entry {
950         u32 word[MVPP2_PRS_TCAM_WORDS];
951         u8  byte[MVPP2_PRS_TCAM_WORDS * 4];
952 };
953
954 union mvpp2_prs_sram_entry {
955         u32 word[MVPP2_PRS_SRAM_WORDS];
956         u8  byte[MVPP2_PRS_SRAM_WORDS * 4];
957 };
958
959 struct mvpp2_prs_entry {
960         u32 index;
961         union mvpp2_prs_tcam_entry tcam;
962         union mvpp2_prs_sram_entry sram;
963 };
964
965 struct mvpp2_prs_shadow {
966         bool valid;
967         bool finish;
968
969         /* Lookup ID */
970         int lu;
971
972         /* User defined offset */
973         int udf;
974
975         /* Result info */
976         u32 ri;
977         u32 ri_mask;
978 };
979
980 struct mvpp2_cls_flow_entry {
981         u32 index;
982         u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
983 };
984
985 struct mvpp2_cls_lookup_entry {
986         u32 lkpid;
987         u32 way;
988         u32 data;
989 };
990
991 struct mvpp2_bm_pool {
992         /* Pool number in the range 0-7 */
993         int id;
994         enum mvpp2_bm_type type;
995
996         /* Buffer Pointers Pool External (BPPE) size */
997         int size;
998         /* Number of buffers for this pool */
999         int buf_num;
1000         /* Pool buffer size */
1001         int buf_size;
1002         /* Packet size */
1003         int pkt_size;
1004
1005         /* BPPE virtual base address */
1006         unsigned long *virt_addr;
1007         /* BPPE DMA base address */
1008         dma_addr_t dma_addr;
1009
1010         /* Ports using BM pool */
1011         u32 port_map;
1012
1013         /* Occupied buffers indicator */
1014         int in_use_thresh;
1015 };
1016
1017 /* Static declaractions */
1018
1019 /* Number of RXQs used by single port */
1020 static int rxq_number = MVPP2_DEFAULT_RXQ;
1021 /* Number of TXQs used by single port */
1022 static int txq_number = MVPP2_DEFAULT_TXQ;
1023
1024 #define MVPP2_DRIVER_NAME "mvpp2"
1025 #define MVPP2_DRIVER_VERSION "1.0"
1026
1027 /*
1028  * U-Boot internal data, mostly uncached buffers for descriptors and data
1029  */
1030 struct buffer_location {
1031         struct mvpp2_tx_desc *aggr_tx_descs;
1032         struct mvpp2_tx_desc *tx_descs;
1033         struct mvpp2_rx_desc *rx_descs;
1034         unsigned long *bm_pool[MVPP2_BM_POOLS_NUM];
1035         unsigned long *rx_buffer[MVPP2_BM_LONG_BUF_NUM];
1036         int first_rxq;
1037 };
1038
1039 /*
1040  * All 4 interfaces use the same global buffer, since only one interface
1041  * can be enabled at once
1042  */
1043 static struct buffer_location buffer_loc;
1044
1045 /*
1046  * Page table entries are set to 1MB, or multiples of 1MB
1047  * (not < 1MB). driver uses less bd's so use 1MB bdspace.
1048  */
1049 #define BD_SPACE        (1 << 20)
1050
1051 /* Utility/helper methods */
1052
1053 static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1054 {
1055         writel(data, priv->base + offset);
1056 }
1057
1058 static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1059 {
1060         return readl(priv->base + offset);
1061 }
1062
1063 static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1064                                       struct mvpp2_tx_desc *tx_desc,
1065                                       dma_addr_t dma_addr)
1066 {
1067         if (port->priv->hw_version == MVPP21) {
1068                 tx_desc->pp21.buf_dma_addr = dma_addr;
1069         } else {
1070                 u64 val = (u64)dma_addr;
1071
1072                 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1073                 tx_desc->pp22.buf_dma_addr_ptp |= val;
1074         }
1075 }
1076
1077 static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1078                                   struct mvpp2_tx_desc *tx_desc,
1079                                   size_t size)
1080 {
1081         if (port->priv->hw_version == MVPP21)
1082                 tx_desc->pp21.data_size = size;
1083         else
1084                 tx_desc->pp22.data_size = size;
1085 }
1086
1087 static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1088                                  struct mvpp2_tx_desc *tx_desc,
1089                                  unsigned int txq)
1090 {
1091         if (port->priv->hw_version == MVPP21)
1092                 tx_desc->pp21.phys_txq = txq;
1093         else
1094                 tx_desc->pp22.phys_txq = txq;
1095 }
1096
1097 static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1098                                  struct mvpp2_tx_desc *tx_desc,
1099                                  unsigned int command)
1100 {
1101         if (port->priv->hw_version == MVPP21)
1102                 tx_desc->pp21.command = command;
1103         else
1104                 tx_desc->pp22.command = command;
1105 }
1106
1107 static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
1108                                     struct mvpp2_tx_desc *tx_desc,
1109                                     unsigned int offset)
1110 {
1111         if (port->priv->hw_version == MVPP21)
1112                 tx_desc->pp21.packet_offset = offset;
1113         else
1114                 tx_desc->pp22.packet_offset = offset;
1115 }
1116
1117 static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1118                                             struct mvpp2_rx_desc *rx_desc)
1119 {
1120         if (port->priv->hw_version == MVPP21)
1121                 return rx_desc->pp21.buf_dma_addr;
1122         else
1123                 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
1124 }
1125
1126 static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1127                                              struct mvpp2_rx_desc *rx_desc)
1128 {
1129         if (port->priv->hw_version == MVPP21)
1130                 return rx_desc->pp21.buf_cookie;
1131         else
1132                 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
1133 }
1134
1135 static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1136                                     struct mvpp2_rx_desc *rx_desc)
1137 {
1138         if (port->priv->hw_version == MVPP21)
1139                 return rx_desc->pp21.data_size;
1140         else
1141                 return rx_desc->pp22.data_size;
1142 }
1143
1144 static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1145                                    struct mvpp2_rx_desc *rx_desc)
1146 {
1147         if (port->priv->hw_version == MVPP21)
1148                 return rx_desc->pp21.status;
1149         else
1150                 return rx_desc->pp22.status;
1151 }
1152
1153 static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1154 {
1155         txq_pcpu->txq_get_index++;
1156         if (txq_pcpu->txq_get_index == txq_pcpu->size)
1157                 txq_pcpu->txq_get_index = 0;
1158 }
1159
1160 /* Get number of physical egress port */
1161 static inline int mvpp2_egress_port(struct mvpp2_port *port)
1162 {
1163         return MVPP2_MAX_TCONT + port->id;
1164 }
1165
1166 /* Get number of physical TXQ */
1167 static inline int mvpp2_txq_phys(int port, int txq)
1168 {
1169         return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1170 }
1171
1172 /* Parser configuration routines */
1173
1174 /* Update parser tcam and sram hw entries */
1175 static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1176 {
1177         int i;
1178
1179         if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1180                 return -EINVAL;
1181
1182         /* Clear entry invalidation bit */
1183         pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1184
1185         /* Write tcam index - indirect access */
1186         mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1187         for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1188                 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1189
1190         /* Write sram index - indirect access */
1191         mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1192         for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1193                 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1194
1195         return 0;
1196 }
1197
1198 /* Read tcam entry from hw */
1199 static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1200 {
1201         int i;
1202
1203         if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1204                 return -EINVAL;
1205
1206         /* Write tcam index - indirect access */
1207         mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1208
1209         pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1210                               MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1211         if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1212                 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1213
1214         for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1215                 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1216
1217         /* Write sram index - indirect access */
1218         mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1219         for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1220                 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1221
1222         return 0;
1223 }
1224
1225 /* Invalidate tcam hw entry */
1226 static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1227 {
1228         /* Write index - indirect access */
1229         mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1230         mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1231                     MVPP2_PRS_TCAM_INV_MASK);
1232 }
1233
1234 /* Enable shadow table entry and set its lookup ID */
1235 static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1236 {
1237         priv->prs_shadow[index].valid = true;
1238         priv->prs_shadow[index].lu = lu;
1239 }
1240
1241 /* Update ri fields in shadow table entry */
1242 static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1243                                     unsigned int ri, unsigned int ri_mask)
1244 {
1245         priv->prs_shadow[index].ri_mask = ri_mask;
1246         priv->prs_shadow[index].ri = ri;
1247 }
1248
1249 /* Update lookup field in tcam sw entry */
1250 static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1251 {
1252         int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1253
1254         pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1255         pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1256 }
1257
1258 /* Update mask for single port in tcam sw entry */
1259 static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1260                                     unsigned int port, bool add)
1261 {
1262         int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1263
1264         if (add)
1265                 pe->tcam.byte[enable_off] &= ~(1 << port);
1266         else
1267                 pe->tcam.byte[enable_off] |= 1 << port;
1268 }
1269
1270 /* Update port map in tcam sw entry */
1271 static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1272                                         unsigned int ports)
1273 {
1274         unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1275         int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1276
1277         pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1278         pe->tcam.byte[enable_off] &= ~port_mask;
1279         pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1280 }
1281
1282 /* Obtain port map from tcam sw entry */
1283 static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1284 {
1285         int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1286
1287         return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1288 }
1289
1290 /* Set byte of data and its enable bits in tcam sw entry */
1291 static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1292                                          unsigned int offs, unsigned char byte,
1293                                          unsigned char enable)
1294 {
1295         pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1296         pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1297 }
1298
1299 /* Get byte of data and its enable bits from tcam sw entry */
1300 static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1301                                          unsigned int offs, unsigned char *byte,
1302                                          unsigned char *enable)
1303 {
1304         *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1305         *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1306 }
1307
1308 /* Set ethertype in tcam sw entry */
1309 static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1310                                   unsigned short ethertype)
1311 {
1312         mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1313         mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1314 }
1315
1316 /* Set bits in sram sw entry */
1317 static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1318                                     int val)
1319 {
1320         pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1321 }
1322
1323 /* Clear bits in sram sw entry */
1324 static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1325                                       int val)
1326 {
1327         pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1328 }
1329
1330 /* Update ri bits in sram sw entry */
1331 static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1332                                      unsigned int bits, unsigned int mask)
1333 {
1334         unsigned int i;
1335
1336         for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1337                 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1338
1339                 if (!(mask & BIT(i)))
1340                         continue;
1341
1342                 if (bits & BIT(i))
1343                         mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1344                 else
1345                         mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1346
1347                 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1348         }
1349 }
1350
1351 /* Update ai bits in sram sw entry */
1352 static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1353                                      unsigned int bits, unsigned int mask)
1354 {
1355         unsigned int i;
1356         int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1357
1358         for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1359
1360                 if (!(mask & BIT(i)))
1361                         continue;
1362
1363                 if (bits & BIT(i))
1364                         mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1365                 else
1366                         mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1367
1368                 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1369         }
1370 }
1371
1372 /* Read ai bits from sram sw entry */
1373 static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1374 {
1375         u8 bits;
1376         int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1377         int ai_en_off = ai_off + 1;
1378         int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1379
1380         bits = (pe->sram.byte[ai_off] >> ai_shift) |
1381                (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1382
1383         return bits;
1384 }
1385
1386 /* In sram sw entry set lookup ID field of the tcam key to be used in the next
1387  * lookup interation
1388  */
1389 static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1390                                        unsigned int lu)
1391 {
1392         int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1393
1394         mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1395                                   MVPP2_PRS_SRAM_NEXT_LU_MASK);
1396         mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1397 }
1398
1399 /* In the sram sw entry set sign and value of the next lookup offset
1400  * and the offset value generated to the classifier
1401  */
1402 static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1403                                      unsigned int op)
1404 {
1405         /* Set sign */
1406         if (shift < 0) {
1407                 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1408                 shift = 0 - shift;
1409         } else {
1410                 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1411         }
1412
1413         /* Set value */
1414         pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1415                                                            (unsigned char)shift;
1416
1417         /* Reset and set operation */
1418         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1419                                   MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1420         mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1421
1422         /* Set base offset as current */
1423         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1424 }
1425
1426 /* In the sram sw entry set sign and value of the user defined offset
1427  * generated to the classifier
1428  */
1429 static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1430                                       unsigned int type, int offset,
1431                                       unsigned int op)
1432 {
1433         /* Set sign */
1434         if (offset < 0) {
1435                 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1436                 offset = 0 - offset;
1437         } else {
1438                 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1439         }
1440
1441         /* Set value */
1442         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1443                                   MVPP2_PRS_SRAM_UDF_MASK);
1444         mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1445         pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1446                                         MVPP2_PRS_SRAM_UDF_BITS)] &=
1447               ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1448         pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1449                                         MVPP2_PRS_SRAM_UDF_BITS)] |=
1450                                 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1451
1452         /* Set offset type */
1453         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1454                                   MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1455         mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1456
1457         /* Set offset operation */
1458         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1459                                   MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1460         mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1461
1462         pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1463                                         MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1464                                              ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1465                                     (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1466
1467         pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1468                                         MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1469                              (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1470
1471         /* Set base offset as current */
1472         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1473 }
1474
1475 /* Find parser flow entry */
1476 static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1477 {
1478         struct mvpp2_prs_entry *pe;
1479         int tid;
1480
1481         pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1482         if (!pe)
1483                 return NULL;
1484         mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1485
1486         /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1487         for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1488                 u8 bits;
1489
1490                 if (!priv->prs_shadow[tid].valid ||
1491                     priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1492                         continue;
1493
1494                 pe->index = tid;
1495                 mvpp2_prs_hw_read(priv, pe);
1496                 bits = mvpp2_prs_sram_ai_get(pe);
1497
1498                 /* Sram store classification lookup ID in AI bits [5:0] */
1499                 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1500                         return pe;
1501         }
1502         kfree(pe);
1503
1504         return NULL;
1505 }
1506
1507 /* Return first free tcam index, seeking from start to end */
1508 static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1509                                      unsigned char end)
1510 {
1511         int tid;
1512
1513         if (start > end)
1514                 swap(start, end);
1515
1516         if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1517                 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1518
1519         for (tid = start; tid <= end; tid++) {
1520                 if (!priv->prs_shadow[tid].valid)
1521                         return tid;
1522         }
1523
1524         return -EINVAL;
1525 }
1526
1527 /* Enable/disable dropping all mac da's */
1528 static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1529 {
1530         struct mvpp2_prs_entry pe;
1531
1532         if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1533                 /* Entry exist - update port only */
1534                 pe.index = MVPP2_PE_DROP_ALL;
1535                 mvpp2_prs_hw_read(priv, &pe);
1536         } else {
1537                 /* Entry doesn't exist - create new */
1538                 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1539                 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1540                 pe.index = MVPP2_PE_DROP_ALL;
1541
1542                 /* Non-promiscuous mode for all ports - DROP unknown packets */
1543                 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1544                                          MVPP2_PRS_RI_DROP_MASK);
1545
1546                 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1547                 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1548
1549                 /* Update shadow table */
1550                 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1551
1552                 /* Mask all ports */
1553                 mvpp2_prs_tcam_port_map_set(&pe, 0);
1554         }
1555
1556         /* Update port mask */
1557         mvpp2_prs_tcam_port_set(&pe, port, add);
1558
1559         mvpp2_prs_hw_write(priv, &pe);
1560 }
1561
1562 /* Set port to promiscuous mode */
1563 static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1564 {
1565         struct mvpp2_prs_entry pe;
1566
1567         /* Promiscuous mode - Accept unknown packets */
1568
1569         if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1570                 /* Entry exist - update port only */
1571                 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1572                 mvpp2_prs_hw_read(priv, &pe);
1573         } else {
1574                 /* Entry doesn't exist - create new */
1575                 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1576                 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1577                 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1578
1579                 /* Continue - set next lookup */
1580                 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1581
1582                 /* Set result info bits */
1583                 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1584                                          MVPP2_PRS_RI_L2_CAST_MASK);
1585
1586                 /* Shift to ethertype */
1587                 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1588                                          MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1589
1590                 /* Mask all ports */
1591                 mvpp2_prs_tcam_port_map_set(&pe, 0);
1592
1593                 /* Update shadow table */
1594                 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1595         }
1596
1597         /* Update port mask */
1598         mvpp2_prs_tcam_port_set(&pe, port, add);
1599
1600         mvpp2_prs_hw_write(priv, &pe);
1601 }
1602
1603 /* Accept multicast */
1604 static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1605                                     bool add)
1606 {
1607         struct mvpp2_prs_entry pe;
1608         unsigned char da_mc;
1609
1610         /* Ethernet multicast address first byte is
1611          * 0x01 for IPv4 and 0x33 for IPv6
1612          */
1613         da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1614
1615         if (priv->prs_shadow[index].valid) {
1616                 /* Entry exist - update port only */
1617                 pe.index = index;
1618                 mvpp2_prs_hw_read(priv, &pe);
1619         } else {
1620                 /* Entry doesn't exist - create new */
1621                 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1622                 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1623                 pe.index = index;
1624
1625                 /* Continue - set next lookup */
1626                 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1627
1628                 /* Set result info bits */
1629                 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1630                                          MVPP2_PRS_RI_L2_CAST_MASK);
1631
1632                 /* Update tcam entry data first byte */
1633                 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1634
1635                 /* Shift to ethertype */
1636                 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1637                                          MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1638
1639                 /* Mask all ports */
1640                 mvpp2_prs_tcam_port_map_set(&pe, 0);
1641
1642                 /* Update shadow table */
1643                 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1644         }
1645
1646         /* Update port mask */
1647         mvpp2_prs_tcam_port_set(&pe, port, add);
1648
1649         mvpp2_prs_hw_write(priv, &pe);
1650 }
1651
1652 /* Parser per-port initialization */
1653 static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
1654                                    int lu_max, int offset)
1655 {
1656         u32 val;
1657
1658         /* Set lookup ID */
1659         val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
1660         val &= ~MVPP2_PRS_PORT_LU_MASK(port);
1661         val |=  MVPP2_PRS_PORT_LU_VAL(port, lu_first);
1662         mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
1663
1664         /* Set maximum number of loops for packet received from port */
1665         val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
1666         val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
1667         val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
1668         mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
1669
1670         /* Set initial offset for packet header extraction for the first
1671          * searching loop
1672          */
1673         val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
1674         val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
1675         val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
1676         mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
1677 }
1678
1679 /* Default flow entries initialization for all ports */
1680 static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
1681 {
1682         struct mvpp2_prs_entry pe;
1683         int port;
1684
1685         for (port = 0; port < MVPP2_MAX_PORTS; port++) {
1686                 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1687                 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1688                 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
1689
1690                 /* Mask all ports */
1691                 mvpp2_prs_tcam_port_map_set(&pe, 0);
1692
1693                 /* Set flow ID*/
1694                 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
1695                 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
1696
1697                 /* Update shadow table and hw entry */
1698                 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
1699                 mvpp2_prs_hw_write(priv, &pe);
1700         }
1701 }
1702
1703 /* Set default entry for Marvell Header field */
1704 static void mvpp2_prs_mh_init(struct mvpp2 *priv)
1705 {
1706         struct mvpp2_prs_entry pe;
1707
1708         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1709
1710         pe.index = MVPP2_PE_MH_DEFAULT;
1711         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
1712         mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
1713                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1714         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
1715
1716         /* Unmask all ports */
1717         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1718
1719         /* Update shadow table and hw entry */
1720         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
1721         mvpp2_prs_hw_write(priv, &pe);
1722 }
1723
1724 /* Set default entires (place holder) for promiscuous, non-promiscuous and
1725  * multicast MAC addresses
1726  */
1727 static void mvpp2_prs_mac_init(struct mvpp2 *priv)
1728 {
1729         struct mvpp2_prs_entry pe;
1730
1731         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1732
1733         /* Non-promiscuous mode for all ports - DROP unknown packets */
1734         pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
1735         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1736
1737         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1738                                  MVPP2_PRS_RI_DROP_MASK);
1739         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1740         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1741
1742         /* Unmask all ports */
1743         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1744
1745         /* Update shadow table and hw entry */
1746         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1747         mvpp2_prs_hw_write(priv, &pe);
1748
1749         /* place holders only - no ports */
1750         mvpp2_prs_mac_drop_all_set(priv, 0, false);
1751         mvpp2_prs_mac_promisc_set(priv, 0, false);
1752         mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
1753         mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
1754 }
1755
1756 /* Match basic ethertypes */
1757 static int mvpp2_prs_etype_init(struct mvpp2 *priv)
1758 {
1759         struct mvpp2_prs_entry pe;
1760         int tid;
1761
1762         /* Ethertype: PPPoE */
1763         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1764                                         MVPP2_PE_LAST_FREE_TID);
1765         if (tid < 0)
1766                 return tid;
1767
1768         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1769         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1770         pe.index = tid;
1771
1772         mvpp2_prs_match_etype(&pe, 0, PROT_PPP_SES);
1773
1774         mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
1775                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1776         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
1777         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
1778                                  MVPP2_PRS_RI_PPPOE_MASK);
1779
1780         /* Update shadow table and hw entry */
1781         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1782         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1783         priv->prs_shadow[pe.index].finish = false;
1784         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
1785                                 MVPP2_PRS_RI_PPPOE_MASK);
1786         mvpp2_prs_hw_write(priv, &pe);
1787
1788         /* Ethertype: ARP */
1789         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1790                                         MVPP2_PE_LAST_FREE_TID);
1791         if (tid < 0)
1792                 return tid;
1793
1794         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1795         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1796         pe.index = tid;
1797
1798         mvpp2_prs_match_etype(&pe, 0, PROT_ARP);
1799
1800         /* Generate flow in the next iteration*/
1801         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1802         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1803         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
1804                                  MVPP2_PRS_RI_L3_PROTO_MASK);
1805         /* Set L3 offset */
1806         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1807                                   MVPP2_ETH_TYPE_LEN,
1808                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1809
1810         /* Update shadow table and hw entry */
1811         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1812         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1813         priv->prs_shadow[pe.index].finish = true;
1814         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
1815                                 MVPP2_PRS_RI_L3_PROTO_MASK);
1816         mvpp2_prs_hw_write(priv, &pe);
1817
1818         /* Ethertype: LBTD */
1819         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1820                                         MVPP2_PE_LAST_FREE_TID);
1821         if (tid < 0)
1822                 return tid;
1823
1824         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1825         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1826         pe.index = tid;
1827
1828         mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
1829
1830         /* Generate flow in the next iteration*/
1831         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1832         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1833         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
1834                                  MVPP2_PRS_RI_UDF3_RX_SPECIAL,
1835                                  MVPP2_PRS_RI_CPU_CODE_MASK |
1836                                  MVPP2_PRS_RI_UDF3_MASK);
1837         /* Set L3 offset */
1838         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1839                                   MVPP2_ETH_TYPE_LEN,
1840                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1841
1842         /* Update shadow table and hw entry */
1843         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1844         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1845         priv->prs_shadow[pe.index].finish = true;
1846         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
1847                                 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
1848                                 MVPP2_PRS_RI_CPU_CODE_MASK |
1849                                 MVPP2_PRS_RI_UDF3_MASK);
1850         mvpp2_prs_hw_write(priv, &pe);
1851
1852         /* Ethertype: IPv4 without options */
1853         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1854                                         MVPP2_PE_LAST_FREE_TID);
1855         if (tid < 0)
1856                 return tid;
1857
1858         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1859         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1860         pe.index = tid;
1861
1862         mvpp2_prs_match_etype(&pe, 0, PROT_IP);
1863         mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
1864                                      MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
1865                                      MVPP2_PRS_IPV4_HEAD_MASK |
1866                                      MVPP2_PRS_IPV4_IHL_MASK);
1867
1868         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
1869         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
1870                                  MVPP2_PRS_RI_L3_PROTO_MASK);
1871         /* Skip eth_type + 4 bytes of IP header */
1872         mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
1873                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1874         /* Set L3 offset */
1875         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1876                                   MVPP2_ETH_TYPE_LEN,
1877                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1878
1879         /* Update shadow table and hw entry */
1880         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1881         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1882         priv->prs_shadow[pe.index].finish = false;
1883         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
1884                                 MVPP2_PRS_RI_L3_PROTO_MASK);
1885         mvpp2_prs_hw_write(priv, &pe);
1886
1887         /* Ethertype: IPv4 with options */
1888         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1889                                         MVPP2_PE_LAST_FREE_TID);
1890         if (tid < 0)
1891                 return tid;
1892
1893         pe.index = tid;
1894
1895         /* Clear tcam data before updating */
1896         pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
1897         pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
1898
1899         mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
1900                                      MVPP2_PRS_IPV4_HEAD,
1901                                      MVPP2_PRS_IPV4_HEAD_MASK);
1902
1903         /* Clear ri before updating */
1904         pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
1905         pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
1906         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
1907                                  MVPP2_PRS_RI_L3_PROTO_MASK);
1908
1909         /* Update shadow table and hw entry */
1910         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1911         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1912         priv->prs_shadow[pe.index].finish = false;
1913         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
1914                                 MVPP2_PRS_RI_L3_PROTO_MASK);
1915         mvpp2_prs_hw_write(priv, &pe);
1916
1917         /* Ethertype: IPv6 without options */
1918         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1919                                         MVPP2_PE_LAST_FREE_TID);
1920         if (tid < 0)
1921                 return tid;
1922
1923         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1924         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1925         pe.index = tid;
1926
1927         mvpp2_prs_match_etype(&pe, 0, PROT_IPV6);
1928
1929         /* Skip DIP of IPV6 header */
1930         mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
1931                                  MVPP2_MAX_L3_ADDR_SIZE,
1932                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1933         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
1934         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
1935                                  MVPP2_PRS_RI_L3_PROTO_MASK);
1936         /* Set L3 offset */
1937         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1938                                   MVPP2_ETH_TYPE_LEN,
1939                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1940
1941         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1942         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1943         priv->prs_shadow[pe.index].finish = false;
1944         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
1945                                 MVPP2_PRS_RI_L3_PROTO_MASK);
1946         mvpp2_prs_hw_write(priv, &pe);
1947
1948         /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
1949         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1950         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1951         pe.index = MVPP2_PE_ETH_TYPE_UN;
1952
1953         /* Unmask all ports */
1954         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1955
1956         /* Generate flow in the next iteration*/
1957         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1958         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1959         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
1960                                  MVPP2_PRS_RI_L3_PROTO_MASK);
1961         /* Set L3 offset even it's unknown L3 */
1962         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1963                                   MVPP2_ETH_TYPE_LEN,
1964                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1965
1966         /* Update shadow table and hw entry */
1967         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1968         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1969         priv->prs_shadow[pe.index].finish = true;
1970         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
1971                                 MVPP2_PRS_RI_L3_PROTO_MASK);
1972         mvpp2_prs_hw_write(priv, &pe);
1973
1974         return 0;
1975 }
1976
1977 /* Parser default initialization */
1978 static int mvpp2_prs_default_init(struct udevice *dev,
1979                                   struct mvpp2 *priv)
1980 {
1981         int err, index, i;
1982
1983         /* Enable tcam table */
1984         mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
1985
1986         /* Clear all tcam and sram entries */
1987         for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
1988                 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1989                 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1990                         mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
1991
1992                 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
1993                 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1994                         mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
1995         }
1996
1997         /* Invalidate all tcam entries */
1998         for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
1999                 mvpp2_prs_hw_inv(priv, index);
2000
2001         priv->prs_shadow = devm_kcalloc(dev, MVPP2_PRS_TCAM_SRAM_SIZE,
2002                                         sizeof(struct mvpp2_prs_shadow),
2003                                         GFP_KERNEL);
2004         if (!priv->prs_shadow)
2005                 return -ENOMEM;
2006
2007         /* Always start from lookup = 0 */
2008         for (index = 0; index < MVPP2_MAX_PORTS; index++)
2009                 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
2010                                        MVPP2_PRS_PORT_LU_MAX, 0);
2011
2012         mvpp2_prs_def_flow_init(priv);
2013
2014         mvpp2_prs_mh_init(priv);
2015
2016         mvpp2_prs_mac_init(priv);
2017
2018         err = mvpp2_prs_etype_init(priv);
2019         if (err)
2020                 return err;
2021
2022         return 0;
2023 }
2024
2025 /* Compare MAC DA with tcam entry data */
2026 static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
2027                                        const u8 *da, unsigned char *mask)
2028 {
2029         unsigned char tcam_byte, tcam_mask;
2030         int index;
2031
2032         for (index = 0; index < ETH_ALEN; index++) {
2033                 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
2034                 if (tcam_mask != mask[index])
2035                         return false;
2036
2037                 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
2038                         return false;
2039         }
2040
2041         return true;
2042 }
2043
2044 /* Find tcam entry with matched pair <MAC DA, port> */
2045 static struct mvpp2_prs_entry *
2046 mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
2047                             unsigned char *mask, int udf_type)
2048 {
2049         struct mvpp2_prs_entry *pe;
2050         int tid;
2051
2052         pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2053         if (!pe)
2054                 return NULL;
2055         mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
2056
2057         /* Go through the all entires with MVPP2_PRS_LU_MAC */
2058         for (tid = MVPP2_PE_FIRST_FREE_TID;
2059              tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2060                 unsigned int entry_pmap;
2061
2062                 if (!priv->prs_shadow[tid].valid ||
2063                     (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
2064                     (priv->prs_shadow[tid].udf != udf_type))
2065                         continue;
2066
2067                 pe->index = tid;
2068                 mvpp2_prs_hw_read(priv, pe);
2069                 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
2070
2071                 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
2072                     entry_pmap == pmap)
2073                         return pe;
2074         }
2075         kfree(pe);
2076
2077         return NULL;
2078 }
2079
2080 /* Update parser's mac da entry */
2081 static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
2082                                    const u8 *da, bool add)
2083 {
2084         struct mvpp2_prs_entry *pe;
2085         unsigned int pmap, len, ri;
2086         unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2087         int tid;
2088
2089         /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
2090         pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
2091                                          MVPP2_PRS_UDF_MAC_DEF);
2092
2093         /* No such entry */
2094         if (!pe) {
2095                 if (!add)
2096                         return 0;
2097
2098                 /* Create new TCAM entry */
2099                 /* Find first range mac entry*/
2100                 for (tid = MVPP2_PE_FIRST_FREE_TID;
2101                      tid <= MVPP2_PE_LAST_FREE_TID; tid++)
2102                         if (priv->prs_shadow[tid].valid &&
2103                             (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
2104                             (priv->prs_shadow[tid].udf ==
2105                                                        MVPP2_PRS_UDF_MAC_RANGE))
2106                                 break;
2107
2108                 /* Go through the all entries from first to last */
2109                 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2110                                                 tid - 1);
2111                 if (tid < 0)
2112                         return tid;
2113
2114                 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2115                 if (!pe)
2116                         return -1;
2117                 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
2118                 pe->index = tid;
2119
2120                 /* Mask all ports */
2121                 mvpp2_prs_tcam_port_map_set(pe, 0);
2122         }
2123
2124         /* Update port mask */
2125         mvpp2_prs_tcam_port_set(pe, port, add);
2126
2127         /* Invalidate the entry if no ports are left enabled */
2128         pmap = mvpp2_prs_tcam_port_map_get(pe);
2129         if (pmap == 0) {
2130                 if (add) {
2131                         kfree(pe);
2132                         return -1;
2133                 }
2134                 mvpp2_prs_hw_inv(priv, pe->index);
2135                 priv->prs_shadow[pe->index].valid = false;
2136                 kfree(pe);
2137                 return 0;
2138         }
2139
2140         /* Continue - set next lookup */
2141         mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
2142
2143         /* Set match on DA */
2144         len = ETH_ALEN;
2145         while (len--)
2146                 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
2147
2148         /* Set result info bits */
2149         ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
2150
2151         mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
2152                                  MVPP2_PRS_RI_MAC_ME_MASK);
2153         mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
2154                                 MVPP2_PRS_RI_MAC_ME_MASK);
2155
2156         /* Shift to ethertype */
2157         mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
2158                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2159
2160         /* Update shadow table and hw entry */
2161         priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
2162         mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
2163         mvpp2_prs_hw_write(priv, pe);
2164
2165         kfree(pe);
2166
2167         return 0;
2168 }
2169
2170 static int mvpp2_prs_update_mac_da(struct mvpp2_port *port, const u8 *da)
2171 {
2172         int err;
2173
2174         /* Remove old parser entry */
2175         err = mvpp2_prs_mac_da_accept(port->priv, port->id, port->dev_addr,
2176                                       false);
2177         if (err)
2178                 return err;
2179
2180         /* Add new parser entry */
2181         err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
2182         if (err)
2183                 return err;
2184
2185         /* Set addr in the device */
2186         memcpy(port->dev_addr, da, ETH_ALEN);
2187
2188         return 0;
2189 }
2190
2191 /* Set prs flow for the port */
2192 static int mvpp2_prs_def_flow(struct mvpp2_port *port)
2193 {
2194         struct mvpp2_prs_entry *pe;
2195         int tid;
2196
2197         pe = mvpp2_prs_flow_find(port->priv, port->id);
2198
2199         /* Such entry not exist */
2200         if (!pe) {
2201                 /* Go through the all entires from last to first */
2202                 tid = mvpp2_prs_tcam_first_free(port->priv,
2203                                                 MVPP2_PE_LAST_FREE_TID,
2204                                                MVPP2_PE_FIRST_FREE_TID);
2205                 if (tid < 0)
2206                         return tid;
2207
2208                 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2209                 if (!pe)
2210                         return -ENOMEM;
2211
2212                 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
2213                 pe->index = tid;
2214
2215                 /* Set flow ID*/
2216                 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
2217                 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2218
2219                 /* Update shadow table */
2220                 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
2221         }
2222
2223         mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
2224         mvpp2_prs_hw_write(port->priv, pe);
2225         kfree(pe);
2226
2227         return 0;
2228 }
2229
2230 /* Classifier configuration routines */
2231
2232 /* Update classification flow table registers */
2233 static void mvpp2_cls_flow_write(struct mvpp2 *priv,
2234                                  struct mvpp2_cls_flow_entry *fe)
2235 {
2236         mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
2237         mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG,  fe->data[0]);
2238         mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG,  fe->data[1]);
2239         mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG,  fe->data[2]);
2240 }
2241
2242 /* Update classification lookup table register */
2243 static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
2244                                    struct mvpp2_cls_lookup_entry *le)
2245 {
2246         u32 val;
2247
2248         val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
2249         mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
2250         mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
2251 }
2252
2253 /* Classifier default initialization */
2254 static void mvpp2_cls_init(struct mvpp2 *priv)
2255 {
2256         struct mvpp2_cls_lookup_entry le;
2257         struct mvpp2_cls_flow_entry fe;
2258         int index;
2259
2260         /* Enable classifier */
2261         mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
2262
2263         /* Clear classifier flow table */
2264         memset(&fe.data, 0, MVPP2_CLS_FLOWS_TBL_DATA_WORDS);
2265         for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
2266                 fe.index = index;
2267                 mvpp2_cls_flow_write(priv, &fe);
2268         }
2269
2270         /* Clear classifier lookup table */
2271         le.data = 0;
2272         for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
2273                 le.lkpid = index;
2274                 le.way = 0;
2275                 mvpp2_cls_lookup_write(priv, &le);
2276
2277                 le.way = 1;
2278                 mvpp2_cls_lookup_write(priv, &le);
2279         }
2280 }
2281
2282 static void mvpp2_cls_port_config(struct mvpp2_port *port)
2283 {
2284         struct mvpp2_cls_lookup_entry le;
2285         u32 val;
2286
2287         /* Set way for the port */
2288         val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
2289         val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
2290         mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
2291
2292         /* Pick the entry to be accessed in lookup ID decoding table
2293          * according to the way and lkpid.
2294          */
2295         le.lkpid = port->id;
2296         le.way = 0;
2297         le.data = 0;
2298
2299         /* Set initial CPU queue for receiving packets */
2300         le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
2301         le.data |= port->first_rxq;
2302
2303         /* Disable classification engines */
2304         le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
2305
2306         /* Update lookup ID table entry */
2307         mvpp2_cls_lookup_write(port->priv, &le);
2308 }
2309
2310 /* Set CPU queue number for oversize packets */
2311 static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
2312 {
2313         u32 val;
2314
2315         mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
2316                     port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
2317
2318         mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
2319                     (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
2320
2321         val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
2322         val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
2323         mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
2324 }
2325
2326 /* Buffer Manager configuration routines */
2327
2328 /* Create pool */
2329 static int mvpp2_bm_pool_create(struct udevice *dev,
2330                                 struct mvpp2 *priv,
2331                                 struct mvpp2_bm_pool *bm_pool, int size)
2332 {
2333         u32 val;
2334
2335         bm_pool->virt_addr = buffer_loc.bm_pool[bm_pool->id];
2336         bm_pool->dma_addr = (dma_addr_t)buffer_loc.bm_pool[bm_pool->id];
2337         if (!bm_pool->virt_addr)
2338                 return -ENOMEM;
2339
2340         if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
2341                         MVPP2_BM_POOL_PTR_ALIGN)) {
2342                 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
2343                         bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
2344                 return -ENOMEM;
2345         }
2346
2347         mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
2348                     bm_pool->dma_addr);
2349         mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
2350
2351         val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
2352         val |= MVPP2_BM_START_MASK;
2353         mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
2354
2355         bm_pool->type = MVPP2_BM_FREE;
2356         bm_pool->size = size;
2357         bm_pool->pkt_size = 0;
2358         bm_pool->buf_num = 0;
2359
2360         return 0;
2361 }
2362
2363 /* Set pool buffer size */
2364 static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
2365                                       struct mvpp2_bm_pool *bm_pool,
2366                                       int buf_size)
2367 {
2368         u32 val;
2369
2370         bm_pool->buf_size = buf_size;
2371
2372         val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
2373         mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
2374 }
2375
2376 /* Free all buffers from the pool */
2377 static void mvpp2_bm_bufs_free(struct udevice *dev, struct mvpp2 *priv,
2378                                struct mvpp2_bm_pool *bm_pool)
2379 {
2380         bm_pool->buf_num = 0;
2381 }
2382
2383 /* Cleanup pool */
2384 static int mvpp2_bm_pool_destroy(struct udevice *dev,
2385                                  struct mvpp2 *priv,
2386                                  struct mvpp2_bm_pool *bm_pool)
2387 {
2388         u32 val;
2389
2390         mvpp2_bm_bufs_free(dev, priv, bm_pool);
2391         if (bm_pool->buf_num) {
2392                 dev_err(dev, "cannot free all buffers in pool %d\n", bm_pool->id);
2393                 return 0;
2394         }
2395
2396         val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
2397         val |= MVPP2_BM_STOP_MASK;
2398         mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
2399
2400         return 0;
2401 }
2402
2403 static int mvpp2_bm_pools_init(struct udevice *dev,
2404                                struct mvpp2 *priv)
2405 {
2406         int i, err, size;
2407         struct mvpp2_bm_pool *bm_pool;
2408
2409         /* Create all pools with maximum size */
2410         size = MVPP2_BM_POOL_SIZE_MAX;
2411         for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
2412                 bm_pool = &priv->bm_pools[i];
2413                 bm_pool->id = i;
2414                 err = mvpp2_bm_pool_create(dev, priv, bm_pool, size);
2415                 if (err)
2416                         goto err_unroll_pools;
2417                 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
2418         }
2419         return 0;
2420
2421 err_unroll_pools:
2422         dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
2423         for (i = i - 1; i >= 0; i--)
2424                 mvpp2_bm_pool_destroy(dev, priv, &priv->bm_pools[i]);
2425         return err;
2426 }
2427
2428 static int mvpp2_bm_init(struct udevice *dev, struct mvpp2 *priv)
2429 {
2430         int i, err;
2431
2432         for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
2433                 /* Mask BM all interrupts */
2434                 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
2435                 /* Clear BM cause register */
2436                 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
2437         }
2438
2439         /* Allocate and initialize BM pools */
2440         priv->bm_pools = devm_kcalloc(dev, MVPP2_BM_POOLS_NUM,
2441                                      sizeof(struct mvpp2_bm_pool), GFP_KERNEL);
2442         if (!priv->bm_pools)
2443                 return -ENOMEM;
2444
2445         err = mvpp2_bm_pools_init(dev, priv);
2446         if (err < 0)
2447                 return err;
2448         return 0;
2449 }
2450
2451 /* Attach long pool to rxq */
2452 static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
2453                                     int lrxq, int long_pool)
2454 {
2455         u32 val;
2456         int prxq;
2457
2458         /* Get queue physical ID */
2459         prxq = port->rxqs[lrxq]->id;
2460
2461         val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
2462         val &= ~MVPP2_RXQ_POOL_LONG_MASK;
2463         val |= ((long_pool << MVPP2_RXQ_POOL_LONG_OFFS) &
2464                     MVPP2_RXQ_POOL_LONG_MASK);
2465
2466         mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
2467 }
2468
2469 /* Set pool number in a BM cookie */
2470 static inline u32 mvpp2_bm_cookie_pool_set(u32 cookie, int pool)
2471 {
2472         u32 bm;
2473
2474         bm = cookie & ~(0xFF << MVPP2_BM_COOKIE_POOL_OFFS);
2475         bm |= ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS);
2476
2477         return bm;
2478 }
2479
2480 /* Get pool number from a BM cookie */
2481 static inline int mvpp2_bm_cookie_pool_get(unsigned long cookie)
2482 {
2483         return (cookie >> MVPP2_BM_COOKIE_POOL_OFFS) & 0xFF;
2484 }
2485
2486 /* Release buffer to BM */
2487 static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
2488                                      dma_addr_t buf_dma_addr,
2489                                      unsigned long buf_phys_addr)
2490 {
2491         /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
2492          * returned in the "cookie" field of the RX
2493          * descriptor. Instead of storing the virtual address, we
2494          * store the physical address
2495          */
2496         mvpp2_write(port->priv, MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
2497         mvpp2_write(port->priv, MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
2498 }
2499
2500 /* Refill BM pool */
2501 static void mvpp2_pool_refill(struct mvpp2_port *port, u32 bm,
2502                               dma_addr_t dma_addr,
2503                               phys_addr_t phys_addr)
2504 {
2505         int pool = mvpp2_bm_cookie_pool_get(bm);
2506
2507         mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
2508 }
2509
2510 /* Allocate buffers for the pool */
2511 static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
2512                              struct mvpp2_bm_pool *bm_pool, int buf_num)
2513 {
2514         int i;
2515
2516         if (buf_num < 0 ||
2517             (buf_num + bm_pool->buf_num > bm_pool->size)) {
2518                 netdev_err(port->dev,
2519                            "cannot allocate %d buffers for pool %d\n",
2520                            buf_num, bm_pool->id);
2521                 return 0;
2522         }
2523
2524         for (i = 0; i < buf_num; i++) {
2525                 mvpp2_bm_pool_put(port, bm_pool->id,
2526                                   (dma_addr_t)buffer_loc.rx_buffer[i],
2527                                   (unsigned long)buffer_loc.rx_buffer[i]);
2528
2529         }
2530
2531         /* Update BM driver with number of buffers added to pool */
2532         bm_pool->buf_num += i;
2533         bm_pool->in_use_thresh = bm_pool->buf_num / 4;
2534
2535         return i;
2536 }
2537
2538 /* Notify the driver that BM pool is being used as specific type and return the
2539  * pool pointer on success
2540  */
2541 static struct mvpp2_bm_pool *
2542 mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
2543                   int pkt_size)
2544 {
2545         struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
2546         int num;
2547
2548         if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
2549                 netdev_err(port->dev, "mixing pool types is forbidden\n");
2550                 return NULL;
2551         }
2552
2553         if (new_pool->type == MVPP2_BM_FREE)
2554                 new_pool->type = type;
2555
2556         /* Allocate buffers in case BM pool is used as long pool, but packet
2557          * size doesn't match MTU or BM pool hasn't being used yet
2558          */
2559         if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
2560             (new_pool->pkt_size == 0)) {
2561                 int pkts_num;
2562
2563                 /* Set default buffer number or free all the buffers in case
2564                  * the pool is not empty
2565                  */
2566                 pkts_num = new_pool->buf_num;
2567                 if (pkts_num == 0)
2568                         pkts_num = type == MVPP2_BM_SWF_LONG ?
2569                                    MVPP2_BM_LONG_BUF_NUM :
2570                                    MVPP2_BM_SHORT_BUF_NUM;
2571                 else
2572                         mvpp2_bm_bufs_free(NULL,
2573                                            port->priv, new_pool);
2574
2575                 new_pool->pkt_size = pkt_size;
2576
2577                 /* Allocate buffers for this pool */
2578                 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
2579                 if (num != pkts_num) {
2580                         dev_err(dev, "pool %d: %d of %d allocated\n",
2581                                 new_pool->id, num, pkts_num);
2582                         return NULL;
2583                 }
2584         }
2585
2586         mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
2587                                   MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
2588
2589         return new_pool;
2590 }
2591
2592 /* Initialize pools for swf */
2593 static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
2594 {
2595         int rxq;
2596
2597         if (!port->pool_long) {
2598                 port->pool_long =
2599                        mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
2600                                          MVPP2_BM_SWF_LONG,
2601                                          port->pkt_size);
2602                 if (!port->pool_long)
2603                         return -ENOMEM;
2604
2605                 port->pool_long->port_map |= (1 << port->id);
2606
2607                 for (rxq = 0; rxq < rxq_number; rxq++)
2608                         mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
2609         }
2610
2611         return 0;
2612 }
2613
2614 /* Port configuration routines */
2615
2616 static void mvpp2_port_mii_set(struct mvpp2_port *port)
2617 {
2618         u32 val;
2619
2620         val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
2621
2622         switch (port->phy_interface) {
2623         case PHY_INTERFACE_MODE_SGMII:
2624                 val |= MVPP2_GMAC_INBAND_AN_MASK;
2625                 break;
2626         case PHY_INTERFACE_MODE_RGMII:
2627                 val |= MVPP2_GMAC_PORT_RGMII_MASK;
2628         default:
2629                 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
2630         }
2631
2632         writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
2633 }
2634
2635 static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
2636 {
2637         u32 val;
2638
2639         val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
2640         val |= MVPP2_GMAC_FC_ADV_EN;
2641         writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
2642 }
2643
2644 static void mvpp2_port_enable(struct mvpp2_port *port)
2645 {
2646         u32 val;
2647
2648         val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
2649         val |= MVPP2_GMAC_PORT_EN_MASK;
2650         val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
2651         writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
2652 }
2653
2654 static void mvpp2_port_disable(struct mvpp2_port *port)
2655 {
2656         u32 val;
2657
2658         val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
2659         val &= ~(MVPP2_GMAC_PORT_EN_MASK);
2660         writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
2661 }
2662
2663 /* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
2664 static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
2665 {
2666         u32 val;
2667
2668         val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
2669                     ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
2670         writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
2671 }
2672
2673 /* Configure loopback port */
2674 static void mvpp2_port_loopback_set(struct mvpp2_port *port)
2675 {
2676         u32 val;
2677
2678         val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
2679
2680         if (port->speed == 1000)
2681                 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
2682         else
2683                 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
2684
2685         if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
2686                 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
2687         else
2688                 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
2689
2690         writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
2691 }
2692
2693 static void mvpp2_port_reset(struct mvpp2_port *port)
2694 {
2695         u32 val;
2696
2697         val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
2698                     ~MVPP2_GMAC_PORT_RESET_MASK;
2699         writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
2700
2701         while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
2702                MVPP2_GMAC_PORT_RESET_MASK)
2703                 continue;
2704 }
2705
2706 /* Change maximum receive size of the port */
2707 static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
2708 {
2709         u32 val;
2710
2711         val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
2712         val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
2713         val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
2714                     MVPP2_GMAC_MAX_RX_SIZE_OFFS);
2715         writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
2716 }
2717
2718 /* Set defaults to the MVPP2 port */
2719 static void mvpp2_defaults_set(struct mvpp2_port *port)
2720 {
2721         int tx_port_num, val, queue, ptxq, lrxq;
2722
2723         /* Configure port to loopback if needed */
2724         if (port->flags & MVPP2_F_LOOPBACK)
2725                 mvpp2_port_loopback_set(port);
2726
2727         /* Update TX FIFO MIN Threshold */
2728         val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
2729         val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
2730         /* Min. TX threshold must be less than minimal packet length */
2731         val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
2732         writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
2733
2734         /* Disable Legacy WRR, Disable EJP, Release from reset */
2735         tx_port_num = mvpp2_egress_port(port);
2736         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
2737                     tx_port_num);
2738         mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
2739
2740         /* Close bandwidth for all queues */
2741         for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
2742                 ptxq = mvpp2_txq_phys(port->id, queue);
2743                 mvpp2_write(port->priv,
2744                             MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
2745         }
2746
2747         /* Set refill period to 1 usec, refill tokens
2748          * and bucket size to maximum
2749          */
2750         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG, 0xc8);
2751         val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
2752         val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
2753         val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
2754         val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
2755         mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
2756         val = MVPP2_TXP_TOKEN_SIZE_MAX;
2757         mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
2758
2759         /* Set MaximumLowLatencyPacketSize value to 256 */
2760         mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
2761                     MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
2762                     MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
2763
2764         /* Enable Rx cache snoop */
2765         for (lrxq = 0; lrxq < rxq_number; lrxq++) {
2766                 queue = port->rxqs[lrxq]->id;
2767                 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
2768                 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
2769                            MVPP2_SNOOP_BUF_HDR_MASK;
2770                 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
2771         }
2772 }
2773
2774 /* Enable/disable receiving packets */
2775 static void mvpp2_ingress_enable(struct mvpp2_port *port)
2776 {
2777         u32 val;
2778         int lrxq, queue;
2779
2780         for (lrxq = 0; lrxq < rxq_number; lrxq++) {
2781                 queue = port->rxqs[lrxq]->id;
2782                 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
2783                 val &= ~MVPP2_RXQ_DISABLE_MASK;
2784                 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
2785         }
2786 }
2787
2788 static void mvpp2_ingress_disable(struct mvpp2_port *port)
2789 {
2790         u32 val;
2791         int lrxq, queue;
2792
2793         for (lrxq = 0; lrxq < rxq_number; lrxq++) {
2794                 queue = port->rxqs[lrxq]->id;
2795                 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
2796                 val |= MVPP2_RXQ_DISABLE_MASK;
2797                 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
2798         }
2799 }
2800
2801 /* Enable transmit via physical egress queue
2802  * - HW starts take descriptors from DRAM
2803  */
2804 static void mvpp2_egress_enable(struct mvpp2_port *port)
2805 {
2806         u32 qmap;
2807         int queue;
2808         int tx_port_num = mvpp2_egress_port(port);
2809
2810         /* Enable all initialized TXs. */
2811         qmap = 0;
2812         for (queue = 0; queue < txq_number; queue++) {
2813                 struct mvpp2_tx_queue *txq = port->txqs[queue];
2814
2815                 if (txq->descs != NULL)
2816                         qmap |= (1 << queue);
2817         }
2818
2819         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
2820         mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
2821 }
2822
2823 /* Disable transmit via physical egress queue
2824  * - HW doesn't take descriptors from DRAM
2825  */
2826 static void mvpp2_egress_disable(struct mvpp2_port *port)
2827 {
2828         u32 reg_data;
2829         int delay;
2830         int tx_port_num = mvpp2_egress_port(port);
2831
2832         /* Issue stop command for active channels only */
2833         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
2834         reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
2835                     MVPP2_TXP_SCHED_ENQ_MASK;
2836         if (reg_data != 0)
2837                 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
2838                             (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
2839
2840         /* Wait for all Tx activity to terminate. */
2841         delay = 0;
2842         do {
2843                 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
2844                         netdev_warn(port->dev,
2845                                     "Tx stop timed out, status=0x%08x\n",
2846                                     reg_data);
2847                         break;
2848                 }
2849                 mdelay(1);
2850                 delay++;
2851
2852                 /* Check port TX Command register that all
2853                  * Tx queues are stopped
2854                  */
2855                 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
2856         } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
2857 }
2858
2859 /* Rx descriptors helper methods */
2860
2861 /* Get number of Rx descriptors occupied by received packets */
2862 static inline int
2863 mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
2864 {
2865         u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
2866
2867         return val & MVPP2_RXQ_OCCUPIED_MASK;
2868 }
2869
2870 /* Update Rx queue status with the number of occupied and available
2871  * Rx descriptor slots.
2872  */
2873 static inline void
2874 mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
2875                         int used_count, int free_count)
2876 {
2877         /* Decrement the number of used descriptors and increment count
2878          * increment the number of free descriptors.
2879          */
2880         u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
2881
2882         mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
2883 }
2884
2885 /* Get pointer to next RX descriptor to be processed by SW */
2886 static inline struct mvpp2_rx_desc *
2887 mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
2888 {
2889         int rx_desc = rxq->next_desc_to_proc;
2890
2891         rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
2892         prefetch(rxq->descs + rxq->next_desc_to_proc);
2893         return rxq->descs + rx_desc;
2894 }
2895
2896 /* Set rx queue offset */
2897 static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
2898                                  int prxq, int offset)
2899 {
2900         u32 val;
2901
2902         /* Convert offset from bytes to units of 32 bytes */
2903         offset = offset >> 5;
2904
2905         val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
2906         val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
2907
2908         /* Offset is in */
2909         val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
2910                     MVPP2_RXQ_PACKET_OFFSET_MASK);
2911
2912         mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
2913 }
2914
2915 /* Obtain BM cookie information from descriptor */
2916 static u32 mvpp2_bm_cookie_build(struct mvpp2_port *port,
2917                                  struct mvpp2_rx_desc *rx_desc)
2918 {
2919         int cpu = smp_processor_id();
2920         int pool;
2921
2922         pool = (mvpp2_rxdesc_status_get(port, rx_desc) &
2923                 MVPP2_RXD_BM_POOL_ID_MASK) >>
2924                 MVPP2_RXD_BM_POOL_ID_OFFS;
2925
2926         return ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS) |
2927                ((cpu & 0xFF) << MVPP2_BM_COOKIE_CPU_OFFS);
2928 }
2929
2930 /* Tx descriptors helper methods */
2931
2932 /* Get number of Tx descriptors waiting to be transmitted by HW */
2933 static int mvpp2_txq_pend_desc_num_get(struct mvpp2_port *port,
2934                                        struct mvpp2_tx_queue *txq)
2935 {
2936         u32 val;
2937
2938         mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
2939         val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
2940
2941         return val & MVPP2_TXQ_PENDING_MASK;
2942 }
2943
2944 /* Get pointer to next Tx descriptor to be processed (send) by HW */
2945 static struct mvpp2_tx_desc *
2946 mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
2947 {
2948         int tx_desc = txq->next_desc_to_proc;
2949
2950         txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
2951         return txq->descs + tx_desc;
2952 }
2953
2954 /* Update HW with number of aggregated Tx descriptors to be sent */
2955 static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
2956 {
2957         /* aggregated access - relevant TXQ number is written in TX desc */
2958         mvpp2_write(port->priv, MVPP2_AGGR_TXQ_UPDATE_REG, pending);
2959 }
2960
2961 /* Get number of sent descriptors and decrement counter.
2962  * The number of sent descriptors is returned.
2963  * Per-CPU access
2964  */
2965 static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
2966                                            struct mvpp2_tx_queue *txq)
2967 {
2968         u32 val;
2969
2970         /* Reading status reg resets transmitted descriptor counter */
2971         val = mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(txq->id));
2972
2973         return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
2974                 MVPP2_TRANSMITTED_COUNT_OFFSET;
2975 }
2976
2977 static void mvpp2_txq_sent_counter_clear(void *arg)
2978 {
2979         struct mvpp2_port *port = arg;
2980         int queue;
2981
2982         for (queue = 0; queue < txq_number; queue++) {
2983                 int id = port->txqs[queue]->id;
2984
2985                 mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(id));
2986         }
2987 }
2988
2989 /* Set max sizes for Tx queues */
2990 static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
2991 {
2992         u32     val, size, mtu;
2993         int     txq, tx_port_num;
2994
2995         mtu = port->pkt_size * 8;
2996         if (mtu > MVPP2_TXP_MTU_MAX)
2997                 mtu = MVPP2_TXP_MTU_MAX;
2998
2999         /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
3000         mtu = 3 * mtu;
3001
3002         /* Indirect access to registers */
3003         tx_port_num = mvpp2_egress_port(port);
3004         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
3005
3006         /* Set MTU */
3007         val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
3008         val &= ~MVPP2_TXP_MTU_MAX;
3009         val |= mtu;
3010         mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
3011
3012         /* TXP token size and all TXQs token size must be larger that MTU */
3013         val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
3014         size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
3015         if (size < mtu) {
3016                 size = mtu;
3017                 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
3018                 val |= size;
3019                 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
3020         }
3021
3022         for (txq = 0; txq < txq_number; txq++) {
3023                 val = mvpp2_read(port->priv,
3024                                  MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
3025                 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
3026
3027                 if (size < mtu) {
3028                         size = mtu;
3029                         val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
3030                         val |= size;
3031                         mvpp2_write(port->priv,
3032                                     MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
3033                                     val);
3034                 }
3035         }
3036 }
3037
3038 /* Free Tx queue skbuffs */
3039 static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
3040                                 struct mvpp2_tx_queue *txq,
3041                                 struct mvpp2_txq_pcpu *txq_pcpu, int num)
3042 {
3043         int i;
3044
3045         for (i = 0; i < num; i++)
3046                 mvpp2_txq_inc_get(txq_pcpu);
3047 }
3048
3049 static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
3050                                                         u32 cause)
3051 {
3052         int queue = fls(cause) - 1;
3053
3054         return port->rxqs[queue];
3055 }
3056
3057 static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
3058                                                         u32 cause)
3059 {
3060         int queue = fls(cause) - 1;
3061
3062         return port->txqs[queue];
3063 }
3064
3065 /* Rx/Tx queue initialization/cleanup methods */
3066
3067 /* Allocate and initialize descriptors for aggr TXQ */
3068 static int mvpp2_aggr_txq_init(struct udevice *dev,
3069                                struct mvpp2_tx_queue *aggr_txq,
3070                                int desc_num, int cpu,
3071                                struct mvpp2 *priv)
3072 {
3073         /* Allocate memory for TX descriptors */
3074         aggr_txq->descs = buffer_loc.aggr_tx_descs;
3075         aggr_txq->descs_dma = (dma_addr_t)buffer_loc.aggr_tx_descs;
3076         if (!aggr_txq->descs)
3077                 return -ENOMEM;
3078
3079         /* Make sure descriptor address is cache line size aligned  */
3080         BUG_ON(aggr_txq->descs !=
3081                PTR_ALIGN(aggr_txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
3082
3083         aggr_txq->last_desc = aggr_txq->size - 1;
3084
3085         /* Aggr TXQ no reset WA */
3086         aggr_txq->next_desc_to_proc = mvpp2_read(priv,
3087                                                  MVPP2_AGGR_TXQ_INDEX_REG(cpu));
3088
3089         /* Set Tx descriptors queue starting address */
3090         /* indirect access */
3091         mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu),
3092                     aggr_txq->descs_dma);
3093         mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
3094
3095         return 0;
3096 }
3097
3098 /* Create a specified Rx queue */
3099 static int mvpp2_rxq_init(struct mvpp2_port *port,
3100                           struct mvpp2_rx_queue *rxq)
3101
3102 {
3103         rxq->size = port->rx_ring_size;
3104
3105         /* Allocate memory for RX descriptors */
3106         rxq->descs = buffer_loc.rx_descs;
3107         rxq->descs_dma = (dma_addr_t)buffer_loc.rx_descs;
3108         if (!rxq->descs)
3109                 return -ENOMEM;
3110
3111         BUG_ON(rxq->descs !=
3112                PTR_ALIGN(rxq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
3113
3114         rxq->last_desc = rxq->size - 1;
3115
3116         /* Zero occupied and non-occupied counters - direct access */
3117         mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
3118
3119         /* Set Rx descriptors queue starting address - indirect access */
3120         mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
3121         mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, rxq->descs_dma);
3122         mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
3123         mvpp2_write(port->priv, MVPP2_RXQ_INDEX_REG, 0);
3124
3125         /* Set Offset */
3126         mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
3127
3128         /* Add number of descriptors ready for receiving packets */
3129         mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
3130
3131         return 0;
3132 }
3133
3134 /* Push packets received by the RXQ to BM pool */
3135 static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
3136                                 struct mvpp2_rx_queue *rxq)
3137 {
3138         int rx_received, i;
3139
3140         rx_received = mvpp2_rxq_received(port, rxq->id);
3141         if (!rx_received)
3142                 return;
3143
3144         for (i = 0; i < rx_received; i++) {
3145                 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
3146                 u32 bm = mvpp2_bm_cookie_build(port, rx_desc);
3147
3148                 mvpp2_pool_refill(port, bm,
3149                                   mvpp2_rxdesc_dma_addr_get(port, rx_desc),
3150                                   mvpp2_rxdesc_cookie_get(port, rx_desc));
3151         }
3152         mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
3153 }
3154
3155 /* Cleanup Rx queue */
3156 static void mvpp2_rxq_deinit(struct mvpp2_port *port,
3157                              struct mvpp2_rx_queue *rxq)
3158 {
3159         mvpp2_rxq_drop_pkts(port, rxq);
3160
3161         rxq->descs             = NULL;
3162         rxq->last_desc         = 0;
3163         rxq->next_desc_to_proc = 0;
3164         rxq->descs_dma         = 0;
3165
3166         /* Clear Rx descriptors queue starting address and size;
3167          * free descriptor number
3168          */
3169         mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
3170         mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
3171         mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, 0);
3172         mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, 0);
3173 }
3174
3175 /* Create and initialize a Tx queue */
3176 static int mvpp2_txq_init(struct mvpp2_port *port,
3177                           struct mvpp2_tx_queue *txq)
3178 {
3179         u32 val;
3180         int cpu, desc, desc_per_txq, tx_port_num;
3181         struct mvpp2_txq_pcpu *txq_pcpu;
3182
3183         txq->size = port->tx_ring_size;
3184
3185         /* Allocate memory for Tx descriptors */
3186         txq->descs = buffer_loc.tx_descs;
3187         txq->descs_dma = (dma_addr_t)buffer_loc.tx_descs;
3188         if (!txq->descs)
3189                 return -ENOMEM;
3190
3191         /* Make sure descriptor address is cache line size aligned  */
3192         BUG_ON(txq->descs !=
3193                PTR_ALIGN(txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
3194
3195         txq->last_desc = txq->size - 1;
3196
3197         /* Set Tx descriptors queue starting address - indirect access */
3198         mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
3199         mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, txq->descs_dma);
3200         mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, txq->size &
3201                                              MVPP2_TXQ_DESC_SIZE_MASK);
3202         mvpp2_write(port->priv, MVPP2_TXQ_INDEX_REG, 0);
3203         mvpp2_write(port->priv, MVPP2_TXQ_RSVD_CLR_REG,
3204                     txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
3205         val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
3206         val &= ~MVPP2_TXQ_PENDING_MASK;
3207         mvpp2_write(port->priv, MVPP2_TXQ_PENDING_REG, val);
3208
3209         /* Calculate base address in prefetch buffer. We reserve 16 descriptors
3210          * for each existing TXQ.
3211          * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
3212          * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
3213          */
3214         desc_per_txq = 16;
3215         desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
3216                (txq->log_id * desc_per_txq);
3217
3218         mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG,
3219                     MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
3220                     MVPP2_PREF_BUF_THRESH(desc_per_txq/2));
3221
3222         /* WRR / EJP configuration - indirect access */
3223         tx_port_num = mvpp2_egress_port(port);
3224         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
3225
3226         val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
3227         val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
3228         val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
3229         val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
3230         mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
3231
3232         val = MVPP2_TXQ_TOKEN_SIZE_MAX;
3233         mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
3234                     val);
3235
3236         for_each_present_cpu(cpu) {
3237                 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
3238                 txq_pcpu->size = txq->size;
3239         }
3240
3241         return 0;
3242 }
3243
3244 /* Free allocated TXQ resources */
3245 static void mvpp2_txq_deinit(struct mvpp2_port *port,
3246                              struct mvpp2_tx_queue *txq)
3247 {
3248         txq->descs             = NULL;
3249         txq->last_desc         = 0;
3250         txq->next_desc_to_proc = 0;
3251         txq->descs_dma         = 0;
3252
3253         /* Set minimum bandwidth for disabled TXQs */
3254         mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
3255
3256         /* Set Tx descriptors queue starting address and size */
3257         mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
3258         mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, 0);
3259         mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, 0);
3260 }
3261
3262 /* Cleanup Tx ports */
3263 static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
3264 {
3265         struct mvpp2_txq_pcpu *txq_pcpu;
3266         int delay, pending, cpu;
3267         u32 val;
3268
3269         mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
3270         val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
3271         val |= MVPP2_TXQ_DRAIN_EN_MASK;
3272         mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
3273
3274         /* The napi queue has been stopped so wait for all packets
3275          * to be transmitted.
3276          */
3277         delay = 0;
3278         do {
3279                 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
3280                         netdev_warn(port->dev,
3281                                     "port %d: cleaning queue %d timed out\n",
3282                                     port->id, txq->log_id);
3283                         break;
3284                 }
3285                 mdelay(1);
3286                 delay++;
3287
3288                 pending = mvpp2_txq_pend_desc_num_get(port, txq);
3289         } while (pending);
3290
3291         val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
3292         mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
3293
3294         for_each_present_cpu(cpu) {
3295                 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
3296
3297                 /* Release all packets */
3298                 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
3299
3300                 /* Reset queue */
3301                 txq_pcpu->count = 0;
3302                 txq_pcpu->txq_put_index = 0;
3303                 txq_pcpu->txq_get_index = 0;
3304         }
3305 }
3306
3307 /* Cleanup all Tx queues */
3308 static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
3309 {
3310         struct mvpp2_tx_queue *txq;
3311         int queue;
3312         u32 val;
3313
3314         val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
3315
3316         /* Reset Tx ports and delete Tx queues */
3317         val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
3318         mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
3319
3320         for (queue = 0; queue < txq_number; queue++) {
3321                 txq = port->txqs[queue];
3322                 mvpp2_txq_clean(port, txq);
3323                 mvpp2_txq_deinit(port, txq);
3324         }
3325
3326         mvpp2_txq_sent_counter_clear(port);
3327
3328         val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
3329         mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
3330 }
3331
3332 /* Cleanup all Rx queues */
3333 static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
3334 {
3335         int queue;
3336
3337         for (queue = 0; queue < rxq_number; queue++)
3338                 mvpp2_rxq_deinit(port, port->rxqs[queue]);
3339 }
3340
3341 /* Init all Rx queues for port */
3342 static int mvpp2_setup_rxqs(struct mvpp2_port *port)
3343 {
3344         int queue, err;
3345
3346         for (queue = 0; queue < rxq_number; queue++) {
3347                 err = mvpp2_rxq_init(port, port->rxqs[queue]);
3348                 if (err)
3349                         goto err_cleanup;
3350         }
3351         return 0;
3352
3353 err_cleanup:
3354         mvpp2_cleanup_rxqs(port);
3355         return err;
3356 }
3357
3358 /* Init all tx queues for port */
3359 static int mvpp2_setup_txqs(struct mvpp2_port *port)
3360 {
3361         struct mvpp2_tx_queue *txq;
3362         int queue, err;
3363
3364         for (queue = 0; queue < txq_number; queue++) {
3365                 txq = port->txqs[queue];
3366                 err = mvpp2_txq_init(port, txq);
3367                 if (err)
3368                         goto err_cleanup;
3369         }
3370
3371         mvpp2_txq_sent_counter_clear(port);
3372         return 0;
3373
3374 err_cleanup:
3375         mvpp2_cleanup_txqs(port);
3376         return err;
3377 }
3378
3379 /* Adjust link */
3380 static void mvpp2_link_event(struct mvpp2_port *port)
3381 {
3382         struct phy_device *phydev = port->phy_dev;
3383         int status_change = 0;
3384         u32 val;
3385
3386         if (phydev->link) {
3387                 if ((port->speed != phydev->speed) ||
3388                     (port->duplex != phydev->duplex)) {
3389                         u32 val;
3390
3391                         val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
3392                         val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
3393                                  MVPP2_GMAC_CONFIG_GMII_SPEED |
3394                                  MVPP2_GMAC_CONFIG_FULL_DUPLEX |
3395                                  MVPP2_GMAC_AN_SPEED_EN |
3396                                  MVPP2_GMAC_AN_DUPLEX_EN);
3397
3398                         if (phydev->duplex)
3399                                 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
3400
3401                         if (phydev->speed == SPEED_1000)
3402                                 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
3403                         else if (phydev->speed == SPEED_100)
3404                                 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
3405
3406                         writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
3407
3408                         port->duplex = phydev->duplex;
3409                         port->speed  = phydev->speed;
3410                 }
3411         }
3412
3413         if (phydev->link != port->link) {
3414                 if (!phydev->link) {
3415                         port->duplex = -1;
3416                         port->speed = 0;
3417                 }
3418
3419                 port->link = phydev->link;
3420                 status_change = 1;
3421         }
3422
3423         if (status_change) {
3424                 if (phydev->link) {
3425                         val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
3426                         val |= (MVPP2_GMAC_FORCE_LINK_PASS |
3427                                 MVPP2_GMAC_FORCE_LINK_DOWN);
3428                         writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
3429                         mvpp2_egress_enable(port);
3430                         mvpp2_ingress_enable(port);
3431                 } else {
3432                         mvpp2_ingress_disable(port);
3433                         mvpp2_egress_disable(port);
3434                 }
3435         }
3436 }
3437
3438 /* Main RX/TX processing routines */
3439
3440 /* Display more error info */
3441 static void mvpp2_rx_error(struct mvpp2_port *port,
3442                            struct mvpp2_rx_desc *rx_desc)
3443 {
3444         u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
3445         size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
3446
3447         switch (status & MVPP2_RXD_ERR_CODE_MASK) {
3448         case MVPP2_RXD_ERR_CRC:
3449                 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
3450                            status, sz);
3451                 break;
3452         case MVPP2_RXD_ERR_OVERRUN:
3453                 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
3454                            status, sz);
3455                 break;
3456         case MVPP2_RXD_ERR_RESOURCE:
3457                 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
3458                            status, sz);
3459                 break;
3460         }
3461 }
3462
3463 /* Reuse skb if possible, or allocate a new skb and add it to BM pool */
3464 static int mvpp2_rx_refill(struct mvpp2_port *port,
3465                            struct mvpp2_bm_pool *bm_pool,
3466                            u32 bm, dma_addr_t dma_addr)
3467 {
3468         mvpp2_pool_refill(port, bm, dma_addr, (unsigned long)dma_addr);
3469         return 0;
3470 }
3471
3472 /* Set hw internals when starting port */
3473 static void mvpp2_start_dev(struct mvpp2_port *port)
3474 {
3475         mvpp2_gmac_max_rx_size_set(port);
3476         mvpp2_txp_max_tx_size_set(port);
3477
3478         mvpp2_port_enable(port);
3479 }
3480
3481 /* Set hw internals when stopping port */
3482 static void mvpp2_stop_dev(struct mvpp2_port *port)
3483 {
3484         /* Stop new packets from arriving to RXQs */
3485         mvpp2_ingress_disable(port);
3486
3487         mvpp2_egress_disable(port);
3488         mvpp2_port_disable(port);
3489 }
3490
3491 static int mvpp2_phy_connect(struct udevice *dev, struct mvpp2_port *port)
3492 {
3493         struct phy_device *phy_dev;
3494
3495         if (!port->init || port->link == 0) {
3496                 phy_dev = phy_connect(port->priv->bus, port->phyaddr, dev,
3497                                       port->phy_interface);
3498                 port->phy_dev = phy_dev;
3499                 if (!phy_dev) {
3500                         netdev_err(port->dev, "cannot connect to phy\n");
3501                         return -ENODEV;
3502                 }
3503                 phy_dev->supported &= PHY_GBIT_FEATURES;
3504                 phy_dev->advertising = phy_dev->supported;
3505
3506                 port->phy_dev = phy_dev;
3507                 port->link    = 0;
3508                 port->duplex  = 0;
3509                 port->speed   = 0;
3510
3511                 phy_config(phy_dev);
3512                 phy_startup(phy_dev);
3513                 if (!phy_dev->link) {
3514                         printf("%s: No link\n", phy_dev->dev->name);
3515                         return -1;
3516                 }
3517
3518                 port->init = 1;
3519         } else {
3520                 mvpp2_egress_enable(port);
3521                 mvpp2_ingress_enable(port);
3522         }
3523
3524         return 0;
3525 }
3526
3527 static int mvpp2_open(struct udevice *dev, struct mvpp2_port *port)
3528 {
3529         unsigned char mac_bcast[ETH_ALEN] = {
3530                         0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3531         int err;
3532
3533         err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
3534         if (err) {
3535                 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
3536                 return err;
3537         }
3538         err = mvpp2_prs_mac_da_accept(port->priv, port->id,
3539                                       port->dev_addr, true);
3540         if (err) {
3541                 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
3542                 return err;
3543         }
3544         err = mvpp2_prs_def_flow(port);
3545         if (err) {
3546                 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
3547                 return err;
3548         }
3549
3550         /* Allocate the Rx/Tx queues */
3551         err = mvpp2_setup_rxqs(port);
3552         if (err) {
3553                 netdev_err(port->dev, "cannot allocate Rx queues\n");
3554                 return err;
3555         }
3556
3557         err = mvpp2_setup_txqs(port);
3558         if (err) {
3559                 netdev_err(port->dev, "cannot allocate Tx queues\n");
3560                 return err;
3561         }
3562
3563         err = mvpp2_phy_connect(dev, port);
3564         if (err < 0)
3565                 return err;
3566
3567         mvpp2_link_event(port);
3568
3569         mvpp2_start_dev(port);
3570
3571         return 0;
3572 }
3573
3574 /* No Device ops here in U-Boot */
3575
3576 /* Driver initialization */
3577
3578 static void mvpp2_port_power_up(struct mvpp2_port *port)
3579 {
3580         mvpp2_port_mii_set(port);
3581         mvpp2_port_periodic_xon_disable(port);
3582         mvpp2_port_fc_adv_enable(port);
3583         mvpp2_port_reset(port);
3584 }
3585
3586 /* Initialize port HW */
3587 static int mvpp2_port_init(struct udevice *dev, struct mvpp2_port *port)
3588 {
3589         struct mvpp2 *priv = port->priv;
3590         struct mvpp2_txq_pcpu *txq_pcpu;
3591         int queue, cpu, err;
3592
3593         if (port->first_rxq + rxq_number > MVPP2_RXQ_TOTAL_NUM)
3594                 return -EINVAL;
3595
3596         /* Disable port */
3597         mvpp2_egress_disable(port);
3598         mvpp2_port_disable(port);
3599
3600         port->txqs = devm_kcalloc(dev, txq_number, sizeof(*port->txqs),
3601                                   GFP_KERNEL);
3602         if (!port->txqs)
3603                 return -ENOMEM;
3604
3605         /* Associate physical Tx queues to this port and initialize.
3606          * The mapping is predefined.
3607          */
3608         for (queue = 0; queue < txq_number; queue++) {
3609                 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
3610                 struct mvpp2_tx_queue *txq;
3611
3612                 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
3613                 if (!txq)
3614                         return -ENOMEM;
3615
3616                 txq->pcpu = devm_kzalloc(dev, sizeof(struct mvpp2_txq_pcpu),
3617                                          GFP_KERNEL);
3618                 if (!txq->pcpu)
3619                         return -ENOMEM;
3620
3621                 txq->id = queue_phy_id;
3622                 txq->log_id = queue;
3623                 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
3624                 for_each_present_cpu(cpu) {
3625                         txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
3626                         txq_pcpu->cpu = cpu;
3627                 }
3628
3629                 port->txqs[queue] = txq;
3630         }
3631
3632         port->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*port->rxqs),
3633                                   GFP_KERNEL);
3634         if (!port->rxqs)
3635                 return -ENOMEM;
3636
3637         /* Allocate and initialize Rx queue for this port */
3638         for (queue = 0; queue < rxq_number; queue++) {
3639                 struct mvpp2_rx_queue *rxq;
3640
3641                 /* Map physical Rx queue to port's logical Rx queue */
3642                 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
3643                 if (!rxq)
3644                         return -ENOMEM;
3645                 /* Map this Rx queue to a physical queue */
3646                 rxq->id = port->first_rxq + queue;
3647                 rxq->port = port->id;
3648                 rxq->logic_rxq = queue;
3649
3650                 port->rxqs[queue] = rxq;
3651         }
3652
3653         /* Configure Rx queue group interrupt for this port */
3654         mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(port->id), CONFIG_MV_ETH_RXQ);
3655
3656         /* Create Rx descriptor rings */
3657         for (queue = 0; queue < rxq_number; queue++) {
3658                 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
3659
3660                 rxq->size = port->rx_ring_size;
3661                 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
3662                 rxq->time_coal = MVPP2_RX_COAL_USEC;
3663         }
3664
3665         mvpp2_ingress_disable(port);
3666
3667         /* Port default configuration */
3668         mvpp2_defaults_set(port);
3669
3670         /* Port's classifier configuration */
3671         mvpp2_cls_oversize_rxq_set(port);
3672         mvpp2_cls_port_config(port);
3673
3674         /* Provide an initial Rx packet size */
3675         port->pkt_size = MVPP2_RX_PKT_SIZE(PKTSIZE_ALIGN);
3676
3677         /* Initialize pools for swf */
3678         err = mvpp2_swf_bm_pool_init(port);
3679         if (err)
3680                 return err;
3681
3682         return 0;
3683 }
3684
3685 /* Ports initialization */
3686 static int mvpp2_port_probe(struct udevice *dev,
3687                             struct mvpp2_port *port,
3688                             int port_node,
3689                             struct mvpp2 *priv,
3690                             int *next_first_rxq)
3691 {
3692         int phy_node;
3693         u32 id;
3694         u32 phyaddr;
3695         const char *phy_mode_str;
3696         int phy_mode = -1;
3697         int priv_common_regs_num = 2;
3698         int err;
3699
3700         phy_node = fdtdec_lookup_phandle(gd->fdt_blob, port_node, "phy");
3701         if (phy_node < 0) {
3702                 dev_err(&pdev->dev, "missing phy\n");
3703                 return -ENODEV;
3704         }
3705
3706         phy_mode_str = fdt_getprop(gd->fdt_blob, port_node, "phy-mode", NULL);
3707         if (phy_mode_str)
3708                 phy_mode = phy_get_interface_by_name(phy_mode_str);
3709         if (phy_mode == -1) {
3710                 dev_err(&pdev->dev, "incorrect phy mode\n");
3711                 return -EINVAL;
3712         }
3713
3714         id = fdtdec_get_int(gd->fdt_blob, port_node, "port-id", -1);
3715         if (id == -1) {
3716                 dev_err(&pdev->dev, "missing port-id value\n");
3717                 return -EINVAL;
3718         }
3719
3720         phyaddr = fdtdec_get_int(gd->fdt_blob, phy_node, "reg", 0);
3721
3722         port->priv = priv;
3723         port->id = id;
3724         port->first_rxq = *next_first_rxq;
3725         port->phy_node = phy_node;
3726         port->phy_interface = phy_mode;
3727         port->phyaddr = phyaddr;
3728
3729         port->base = (void __iomem *)dev_get_addr_index(dev->parent,
3730                                                         priv_common_regs_num
3731                                                         + id);
3732         if (IS_ERR(port->base))
3733                 return PTR_ERR(port->base);
3734
3735         port->tx_ring_size = MVPP2_MAX_TXD;
3736         port->rx_ring_size = MVPP2_MAX_RXD;
3737
3738         err = mvpp2_port_init(dev, port);
3739         if (err < 0) {
3740                 dev_err(&pdev->dev, "failed to init port %d\n", id);
3741                 return err;
3742         }
3743         mvpp2_port_power_up(port);
3744
3745         /* Increment the first Rx queue number to be used by the next port */
3746         *next_first_rxq += CONFIG_MV_ETH_RXQ;
3747         priv->port_list[id] = port;
3748         return 0;
3749 }
3750
3751 /* Initialize decoding windows */
3752 static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
3753                                     struct mvpp2 *priv)
3754 {
3755         u32 win_enable;
3756         int i;
3757
3758         for (i = 0; i < 6; i++) {
3759                 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
3760                 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
3761
3762                 if (i < 4)
3763                         mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
3764         }
3765
3766         win_enable = 0;
3767
3768         for (i = 0; i < dram->num_cs; i++) {
3769                 const struct mbus_dram_window *cs = dram->cs + i;
3770
3771                 mvpp2_write(priv, MVPP2_WIN_BASE(i),
3772                             (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
3773                             dram->mbus_dram_target_id);
3774
3775                 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
3776                             (cs->size - 1) & 0xffff0000);
3777
3778                 win_enable |= (1 << i);
3779         }
3780
3781         mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
3782 }
3783
3784 /* Initialize Rx FIFO's */
3785 static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
3786 {
3787         int port;
3788
3789         for (port = 0; port < MVPP2_MAX_PORTS; port++) {
3790                 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
3791                             MVPP2_RX_FIFO_PORT_DATA_SIZE);
3792                 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
3793                             MVPP2_RX_FIFO_PORT_ATTR_SIZE);
3794         }
3795
3796         mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
3797                     MVPP2_RX_FIFO_PORT_MIN_PKT);
3798         mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
3799 }
3800
3801 /* Initialize network controller common part HW */
3802 static int mvpp2_init(struct udevice *dev, struct mvpp2 *priv)
3803 {
3804         const struct mbus_dram_target_info *dram_target_info;
3805         int err, i;
3806         u32 val;
3807
3808         /* Checks for hardware constraints (U-Boot uses only one rxq) */
3809         if ((rxq_number > MVPP2_MAX_RXQ) || (txq_number > MVPP2_MAX_TXQ)) {
3810                 dev_err(&pdev->dev, "invalid queue size parameter\n");
3811                 return -EINVAL;
3812         }
3813
3814         /* MBUS windows configuration */
3815         dram_target_info = mvebu_mbus_dram_info();
3816         if (dram_target_info)
3817                 mvpp2_conf_mbus_windows(dram_target_info, priv);
3818
3819         /* Disable HW PHY polling */
3820         val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
3821         val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
3822         writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
3823
3824         /* Allocate and initialize aggregated TXQs */
3825         priv->aggr_txqs = devm_kcalloc(dev, num_present_cpus(),
3826                                        sizeof(struct mvpp2_tx_queue),
3827                                        GFP_KERNEL);
3828         if (!priv->aggr_txqs)
3829                 return -ENOMEM;
3830
3831         for_each_present_cpu(i) {
3832                 priv->aggr_txqs[i].id = i;
3833                 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
3834                 err = mvpp2_aggr_txq_init(dev, &priv->aggr_txqs[i],
3835                                           MVPP2_AGGR_TXQ_SIZE, i, priv);
3836                 if (err < 0)
3837                         return err;
3838         }
3839
3840         /* Rx Fifo Init */
3841         mvpp2_rx_fifo_init(priv);
3842
3843         /* Reset Rx queue group interrupt configuration */
3844         for (i = 0; i < MVPP2_MAX_PORTS; i++)
3845                 mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(i),
3846                             CONFIG_MV_ETH_RXQ);
3847
3848         writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
3849                priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
3850
3851         /* Allow cache snoop when transmiting packets */
3852         mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
3853
3854         /* Buffer Manager initialization */
3855         err = mvpp2_bm_init(dev, priv);
3856         if (err < 0)
3857                 return err;
3858
3859         /* Parser default initialization */
3860         err = mvpp2_prs_default_init(dev, priv);
3861         if (err < 0)
3862                 return err;
3863
3864         /* Classifier default initialization */
3865         mvpp2_cls_init(priv);
3866
3867         return 0;
3868 }
3869
3870 /* SMI / MDIO functions */
3871
3872 static int smi_wait_ready(struct mvpp2 *priv)
3873 {
3874         u32 timeout = MVPP2_SMI_TIMEOUT;
3875         u32 smi_reg;
3876
3877         /* wait till the SMI is not busy */
3878         do {
3879                 /* read smi register */
3880                 smi_reg = readl(priv->lms_base + MVPP2_SMI);
3881                 if (timeout-- == 0) {
3882                         printf("Error: SMI busy timeout\n");
3883                         return -EFAULT;
3884                 }
3885         } while (smi_reg & MVPP2_SMI_BUSY);
3886
3887         return 0;
3888 }
3889
3890 /*
3891  * mpp2_mdio_read - miiphy_read callback function.
3892  *
3893  * Returns 16bit phy register value, or 0xffff on error
3894  */
3895 static int mpp2_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
3896 {
3897         struct mvpp2 *priv = bus->priv;
3898         u32 smi_reg;
3899         u32 timeout;
3900
3901         /* check parameters */
3902         if (addr > MVPP2_PHY_ADDR_MASK) {
3903                 printf("Error: Invalid PHY address %d\n", addr);
3904                 return -EFAULT;
3905         }
3906
3907         if (reg > MVPP2_PHY_REG_MASK) {
3908                 printf("Err: Invalid register offset %d\n", reg);
3909                 return -EFAULT;
3910         }
3911
3912         /* wait till the SMI is not busy */
3913         if (smi_wait_ready(priv) < 0)
3914                 return -EFAULT;
3915
3916         /* fill the phy address and regiser offset and read opcode */
3917         smi_reg = (addr << MVPP2_SMI_DEV_ADDR_OFFS)
3918                 | (reg << MVPP2_SMI_REG_ADDR_OFFS)
3919                 | MVPP2_SMI_OPCODE_READ;
3920
3921         /* write the smi register */
3922         writel(smi_reg, priv->lms_base + MVPP2_SMI);
3923
3924         /* wait till read value is ready */
3925         timeout = MVPP2_SMI_TIMEOUT;
3926
3927         do {
3928                 /* read smi register */
3929                 smi_reg = readl(priv->lms_base + MVPP2_SMI);
3930                 if (timeout-- == 0) {
3931                         printf("Err: SMI read ready timeout\n");
3932                         return -EFAULT;
3933                 }
3934         } while (!(smi_reg & MVPP2_SMI_READ_VALID));
3935
3936         /* Wait for the data to update in the SMI register */
3937         for (timeout = 0; timeout < MVPP2_SMI_TIMEOUT; timeout++)
3938                 ;
3939
3940         return readl(priv->lms_base + MVPP2_SMI) & MVPP2_SMI_DATA_MASK;
3941 }
3942
3943 /*
3944  * mpp2_mdio_write - miiphy_write callback function.
3945  *
3946  * Returns 0 if write succeed, -EINVAL on bad parameters
3947  * -ETIME on timeout
3948  */
3949 static int mpp2_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
3950                            u16 value)
3951 {
3952         struct mvpp2 *priv = bus->priv;
3953         u32 smi_reg;
3954
3955         /* check parameters */
3956         if (addr > MVPP2_PHY_ADDR_MASK) {
3957                 printf("Error: Invalid PHY address %d\n", addr);
3958                 return -EFAULT;
3959         }
3960
3961         if (reg > MVPP2_PHY_REG_MASK) {
3962                 printf("Err: Invalid register offset %d\n", reg);
3963                 return -EFAULT;
3964         }
3965
3966         /* wait till the SMI is not busy */
3967         if (smi_wait_ready(priv) < 0)
3968                 return -EFAULT;
3969
3970         /* fill the phy addr and reg offset and write opcode and data */
3971         smi_reg = value << MVPP2_SMI_DATA_OFFS;
3972         smi_reg |= (addr << MVPP2_SMI_DEV_ADDR_OFFS)
3973                 | (reg << MVPP2_SMI_REG_ADDR_OFFS);
3974         smi_reg &= ~MVPP2_SMI_OPCODE_READ;
3975
3976         /* write the smi register */
3977         writel(smi_reg, priv->lms_base + MVPP2_SMI);
3978
3979         return 0;
3980 }
3981
3982 static int mvpp2_recv(struct udevice *dev, int flags, uchar **packetp)
3983 {
3984         struct mvpp2_port *port = dev_get_priv(dev);
3985         struct mvpp2_rx_desc *rx_desc;
3986         struct mvpp2_bm_pool *bm_pool;
3987         dma_addr_t dma_addr;
3988         u32 bm, rx_status;
3989         int pool, rx_bytes, err;
3990         int rx_received;
3991         struct mvpp2_rx_queue *rxq;
3992         u32 cause_rx_tx, cause_rx, cause_misc;
3993         u8 *data;
3994
3995         cause_rx_tx = mvpp2_read(port->priv,
3996                                  MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
3997         cause_rx_tx &= ~MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
3998         cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
3999         if (!cause_rx_tx && !cause_misc)
4000                 return 0;
4001
4002         cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4003
4004         /* Process RX packets */
4005         cause_rx |= port->pending_cause_rx;
4006         rxq = mvpp2_get_rx_queue(port, cause_rx);
4007
4008         /* Get number of received packets and clamp the to-do */
4009         rx_received = mvpp2_rxq_received(port, rxq->id);
4010
4011         /* Return if no packets are received */
4012         if (!rx_received)
4013                 return 0;
4014
4015         rx_desc = mvpp2_rxq_next_desc_get(rxq);
4016         rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
4017         rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
4018         rx_bytes -= MVPP2_MH_SIZE;
4019         dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
4020
4021         bm = mvpp2_bm_cookie_build(port, rx_desc);
4022         pool = mvpp2_bm_cookie_pool_get(bm);
4023         bm_pool = &port->priv->bm_pools[pool];
4024
4025         /* In case of an error, release the requested buffer pointer
4026          * to the Buffer Manager. This request process is controlled
4027          * by the hardware, and the information about the buffer is
4028          * comprised by the RX descriptor.
4029          */
4030         if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
4031                 mvpp2_rx_error(port, rx_desc);
4032                 /* Return the buffer to the pool */
4033                 mvpp2_pool_refill(port, bm, dma_addr, dma_addr);
4034                 return 0;
4035         }
4036
4037         err = mvpp2_rx_refill(port, bm_pool, bm, dma_addr);
4038         if (err) {
4039                 netdev_err(port->dev, "failed to refill BM pools\n");
4040                 return 0;
4041         }
4042
4043         /* Update Rx queue management counters */
4044         mb();
4045         mvpp2_rxq_status_update(port, rxq->id, 1, 1);
4046
4047         /* give packet to stack - skip on first n bytes */
4048         data = (u8 *)dma_addr + 2 + 32;
4049
4050         if (rx_bytes <= 0)
4051                 return 0;
4052
4053         /*
4054          * No cache invalidation needed here, since the rx_buffer's are
4055          * located in a uncached memory region
4056          */
4057         *packetp = data;
4058
4059         return rx_bytes;
4060 }
4061
4062 /* Drain Txq */
4063 static void mvpp2_txq_drain(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
4064                             int enable)
4065 {
4066         u32 val;
4067
4068         mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
4069         val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
4070         if (enable)
4071                 val |= MVPP2_TXQ_DRAIN_EN_MASK;
4072         else
4073                 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
4074         mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
4075 }
4076
4077 static int mvpp2_send(struct udevice *dev, void *packet, int length)
4078 {
4079         struct mvpp2_port *port = dev_get_priv(dev);
4080         struct mvpp2_tx_queue *txq, *aggr_txq;
4081         struct mvpp2_tx_desc *tx_desc;
4082         int tx_done;
4083         int timeout;
4084
4085         txq = port->txqs[0];
4086         aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
4087
4088         /* Get a descriptor for the first part of the packet */
4089         tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
4090         mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
4091         mvpp2_txdesc_size_set(port, tx_desc, length);
4092         mvpp2_txdesc_offset_set(port, tx_desc,
4093                                 (dma_addr_t)packet & MVPP2_TX_DESC_ALIGN);
4094         mvpp2_txdesc_dma_addr_set(port, tx_desc,
4095                                   (dma_addr_t)packet & ~MVPP2_TX_DESC_ALIGN);
4096         /* First and Last descriptor */
4097         mvpp2_txdesc_cmd_set(port, tx_desc,
4098                              MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE
4099                              | MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC);
4100
4101         /* Flush tx data */
4102         flush_dcache_range((unsigned long)packet,
4103                            (unsigned long)packet + ALIGN(length, PKTALIGN));
4104
4105         /* Enable transmit */
4106         mb();
4107         mvpp2_aggr_txq_pend_desc_add(port, 1);
4108
4109         mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
4110
4111         timeout = 0;
4112         do {
4113                 if (timeout++ > 10000) {
4114                         printf("timeout: packet not sent from aggregated to phys TXQ\n");
4115                         return 0;
4116                 }
4117                 tx_done = mvpp2_txq_pend_desc_num_get(port, txq);
4118         } while (tx_done);
4119
4120         /* Enable TXQ drain */
4121         mvpp2_txq_drain(port, txq, 1);
4122
4123         timeout = 0;
4124         do {
4125                 if (timeout++ > 10000) {
4126                         printf("timeout: packet not sent\n");
4127                         return 0;
4128                 }
4129                 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
4130         } while (!tx_done);
4131
4132         /* Disable TXQ drain */
4133         mvpp2_txq_drain(port, txq, 0);
4134
4135         return 0;
4136 }
4137
4138 static int mvpp2_start(struct udevice *dev)
4139 {
4140         struct eth_pdata *pdata = dev_get_platdata(dev);
4141         struct mvpp2_port *port = dev_get_priv(dev);
4142
4143         /* Load current MAC address */
4144         memcpy(port->dev_addr, pdata->enetaddr, ETH_ALEN);
4145
4146         /* Reconfigure parser accept the original MAC address */
4147         mvpp2_prs_update_mac_da(port, port->dev_addr);
4148
4149         mvpp2_port_power_up(port);
4150
4151         mvpp2_open(dev, port);
4152
4153         return 0;
4154 }
4155
4156 static void mvpp2_stop(struct udevice *dev)
4157 {
4158         struct mvpp2_port *port = dev_get_priv(dev);
4159
4160         mvpp2_stop_dev(port);
4161         mvpp2_cleanup_rxqs(port);
4162         mvpp2_cleanup_txqs(port);
4163 }
4164
4165 static int mvpp2_probe(struct udevice *dev)
4166 {
4167         struct mvpp2_port *port = dev_get_priv(dev);
4168         struct mvpp2 *priv = dev_get_priv(dev->parent);
4169         int err;
4170
4171         /* Initialize network controller */
4172         err = mvpp2_init(dev, priv);
4173         if (err < 0) {
4174                 dev_err(&pdev->dev, "failed to initialize controller\n");
4175                 return err;
4176         }
4177
4178         return mvpp2_port_probe(dev, port, dev_of_offset(dev), priv,
4179                                 &buffer_loc.first_rxq);
4180 }
4181
4182 static const struct eth_ops mvpp2_ops = {
4183         .start          = mvpp2_start,
4184         .send           = mvpp2_send,
4185         .recv           = mvpp2_recv,
4186         .stop           = mvpp2_stop,
4187 };
4188
4189 static struct driver mvpp2_driver = {
4190         .name   = "mvpp2",
4191         .id     = UCLASS_ETH,
4192         .probe  = mvpp2_probe,
4193         .ops    = &mvpp2_ops,
4194         .priv_auto_alloc_size = sizeof(struct mvpp2_port),
4195         .platdata_auto_alloc_size = sizeof(struct eth_pdata),
4196 };
4197
4198 /*
4199  * Use a MISC device to bind the n instances (child nodes) of the
4200  * network base controller in UCLASS_ETH.
4201  */
4202 static int mvpp2_base_probe(struct udevice *dev)
4203 {
4204         struct mvpp2 *priv = dev_get_priv(dev);
4205         struct mii_dev *bus;
4206         void *bd_space;
4207         u32 size = 0;
4208         int i;
4209
4210         /* Save hw-version */
4211         priv->hw_version = dev_get_driver_data(dev);
4212
4213         /*
4214          * U-Boot special buffer handling:
4215          *
4216          * Allocate buffer area for descs and rx_buffers. This is only
4217          * done once for all interfaces. As only one interface can
4218          * be active. Make this area DMA-safe by disabling the D-cache
4219          */
4220
4221         /* Align buffer area for descs and rx_buffers to 1MiB */
4222         bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
4223         mmu_set_region_dcache_behaviour((unsigned long)bd_space,
4224                                         BD_SPACE, DCACHE_OFF);
4225
4226         buffer_loc.aggr_tx_descs = (struct mvpp2_tx_desc *)bd_space;
4227         size += MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE;
4228
4229         buffer_loc.tx_descs =
4230                 (struct mvpp2_tx_desc *)((unsigned long)bd_space + size);
4231         size += MVPP2_MAX_TXD * MVPP2_DESC_ALIGNED_SIZE;
4232
4233         buffer_loc.rx_descs =
4234                 (struct mvpp2_rx_desc *)((unsigned long)bd_space + size);
4235         size += MVPP2_MAX_RXD * MVPP2_DESC_ALIGNED_SIZE;
4236
4237         for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
4238                 buffer_loc.bm_pool[i] =
4239                         (unsigned long *)((unsigned long)bd_space + size);
4240                 size += MVPP2_BM_POOL_SIZE_MAX * sizeof(u32);
4241         }
4242
4243         for (i = 0; i < MVPP2_BM_LONG_BUF_NUM; i++) {
4244                 buffer_loc.rx_buffer[i] =
4245                         (unsigned long *)((unsigned long)bd_space + size);
4246                 size += RX_BUFFER_SIZE;
4247         }
4248
4249         /* Save base addresses for later use */
4250         priv->base = (void *)dev_get_addr_index(dev, 0);
4251         if (IS_ERR(priv->base))
4252                 return PTR_ERR(priv->base);
4253
4254         priv->lms_base = (void *)dev_get_addr_index(dev, 1);
4255         if (IS_ERR(priv->lms_base))
4256                 return PTR_ERR(priv->lms_base);
4257
4258         /* Finally create and register the MDIO bus driver */
4259         bus = mdio_alloc();
4260         if (!bus) {
4261                 printf("Failed to allocate MDIO bus\n");
4262                 return -ENOMEM;
4263         }
4264
4265         bus->read = mpp2_mdio_read;
4266         bus->write = mpp2_mdio_write;
4267         snprintf(bus->name, sizeof(bus->name), dev->name);
4268         bus->priv = (void *)priv;
4269         priv->bus = bus;
4270
4271         return mdio_register(bus);
4272 }
4273
4274 static int mvpp2_base_bind(struct udevice *parent)
4275 {
4276         const void *blob = gd->fdt_blob;
4277         int node = dev_of_offset(parent);
4278         struct uclass_driver *drv;
4279         struct udevice *dev;
4280         struct eth_pdata *plat;
4281         char *name;
4282         int subnode;
4283         u32 id;
4284
4285         /* Lookup eth driver */
4286         drv = lists_uclass_lookup(UCLASS_ETH);
4287         if (!drv) {
4288                 puts("Cannot find eth driver\n");
4289                 return -ENOENT;
4290         }
4291
4292         fdt_for_each_subnode(subnode, blob, node) {
4293                 /* Skip disabled ports */
4294                 if (!fdtdec_get_is_enabled(blob, subnode))
4295                         continue;
4296
4297                 plat = calloc(1, sizeof(*plat));
4298                 if (!plat)
4299                         return -ENOMEM;
4300
4301                 id = fdtdec_get_int(blob, subnode, "port-id", -1);
4302
4303                 name = calloc(1, 16);
4304                 sprintf(name, "mvpp2-%d", id);
4305
4306                 /* Create child device UCLASS_ETH and bind it */
4307                 device_bind(parent, &mvpp2_driver, name, plat, subnode, &dev);
4308                 dev_set_of_offset(dev, subnode);
4309         }
4310
4311         return 0;
4312 }
4313
4314 static const struct udevice_id mvpp2_ids[] = {
4315         {
4316                 .compatible = "marvell,armada-375-pp2",
4317                 .data = MVPP21,
4318         },
4319         { }
4320 };
4321
4322 U_BOOT_DRIVER(mvpp2_base) = {
4323         .name   = "mvpp2_base",
4324         .id     = UCLASS_MISC,
4325         .of_match = mvpp2_ids,
4326         .bind   = mvpp2_base_bind,
4327         .probe  = mvpp2_base_probe,
4328         .priv_auto_alloc_size = sizeof(struct mvpp2),
4329 };