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