blob: fe1458450e447ffd2e8aecf2df6bea440d1a1451 [file] [log] [blame]
Marcin Wojtas3f518502014-07-10 16:52:13 -03001/*
2 * Driver for Marvell PPv2 network controller for Armada 375 SoC.
3 *
4 * Copyright (C) 2014 Marvell
5 *
6 * Marcin Wojtas <mw@semihalf.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
13#include <linux/kernel.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/platform_device.h>
17#include <linux/skbuff.h>
18#include <linux/inetdevice.h>
19#include <linux/mbus.h>
20#include <linux/module.h>
21#include <linux/interrupt.h>
22#include <linux/cpumask.h>
23#include <linux/of.h>
24#include <linux/of_irq.h>
25#include <linux/of_mdio.h>
26#include <linux/of_net.h>
27#include <linux/of_address.h>
Thomas Petazzonifaca9242017-03-07 16:53:06 +010028#include <linux/of_device.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030029#include <linux/phy.h>
30#include <linux/clk.h>
Marcin Wojtasedc660f2015-08-06 19:00:30 +020031#include <linux/hrtimer.h>
32#include <linux/ktime.h>
Marcin Wojtas3f518502014-07-10 16:52:13 -030033#include <uapi/linux/ppp_defs.h>
34#include <net/ip.h>
35#include <net/ipv6.h>
36
37/* RX Fifo Registers */
38#define MVPP2_RX_DATA_FIFO_SIZE_REG(port) (0x00 + 4 * (port))
39#define MVPP2_RX_ATTR_FIFO_SIZE_REG(port) (0x20 + 4 * (port))
40#define MVPP2_RX_MIN_PKT_SIZE_REG 0x60
41#define MVPP2_RX_FIFO_INIT_REG 0x64
42
43/* RX DMA Top Registers */
44#define MVPP2_RX_CTRL_REG(port) (0x140 + 4 * (port))
45#define MVPP2_RX_LOW_LATENCY_PKT_SIZE(s) (((s) & 0xfff) << 16)
46#define MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK BIT(31)
47#define MVPP2_POOL_BUF_SIZE_REG(pool) (0x180 + 4 * (pool))
48#define MVPP2_POOL_BUF_SIZE_OFFSET 5
49#define MVPP2_RXQ_CONFIG_REG(rxq) (0x800 + 4 * (rxq))
50#define MVPP2_SNOOP_PKT_SIZE_MASK 0x1ff
51#define MVPP2_SNOOP_BUF_HDR_MASK BIT(9)
52#define MVPP2_RXQ_POOL_SHORT_OFFS 20
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010053#define MVPP21_RXQ_POOL_SHORT_MASK 0x700000
54#define MVPP22_RXQ_POOL_SHORT_MASK 0xf00000
Marcin Wojtas3f518502014-07-10 16:52:13 -030055#define MVPP2_RXQ_POOL_LONG_OFFS 24
Thomas Petazzoni5eac8922017-03-07 16:53:10 +010056#define MVPP21_RXQ_POOL_LONG_MASK 0x7000000
57#define MVPP22_RXQ_POOL_LONG_MASK 0xf000000
Marcin Wojtas3f518502014-07-10 16:52:13 -030058#define MVPP2_RXQ_PACKET_OFFSET_OFFS 28
59#define MVPP2_RXQ_PACKET_OFFSET_MASK 0x70000000
60#define MVPP2_RXQ_DISABLE_MASK BIT(31)
61
62/* Parser Registers */
63#define MVPP2_PRS_INIT_LOOKUP_REG 0x1000
64#define MVPP2_PRS_PORT_LU_MAX 0xf
65#define MVPP2_PRS_PORT_LU_MASK(port) (0xff << ((port) * 4))
66#define MVPP2_PRS_PORT_LU_VAL(port, val) ((val) << ((port) * 4))
67#define MVPP2_PRS_INIT_OFFS_REG(port) (0x1004 + ((port) & 4))
68#define MVPP2_PRS_INIT_OFF_MASK(port) (0x3f << (((port) % 4) * 8))
69#define MVPP2_PRS_INIT_OFF_VAL(port, val) ((val) << (((port) % 4) * 8))
70#define MVPP2_PRS_MAX_LOOP_REG(port) (0x100c + ((port) & 4))
71#define MVPP2_PRS_MAX_LOOP_MASK(port) (0xff << (((port) % 4) * 8))
72#define MVPP2_PRS_MAX_LOOP_VAL(port, val) ((val) << (((port) % 4) * 8))
73#define MVPP2_PRS_TCAM_IDX_REG 0x1100
74#define MVPP2_PRS_TCAM_DATA_REG(idx) (0x1104 + (idx) * 4)
75#define MVPP2_PRS_TCAM_INV_MASK BIT(31)
76#define MVPP2_PRS_SRAM_IDX_REG 0x1200
77#define MVPP2_PRS_SRAM_DATA_REG(idx) (0x1204 + (idx) * 4)
78#define MVPP2_PRS_TCAM_CTRL_REG 0x1230
79#define MVPP2_PRS_TCAM_EN_MASK BIT(0)
80
81/* Classifier Registers */
82#define MVPP2_CLS_MODE_REG 0x1800
83#define MVPP2_CLS_MODE_ACTIVE_MASK BIT(0)
84#define MVPP2_CLS_PORT_WAY_REG 0x1810
85#define MVPP2_CLS_PORT_WAY_MASK(port) (1 << (port))
86#define MVPP2_CLS_LKP_INDEX_REG 0x1814
87#define MVPP2_CLS_LKP_INDEX_WAY_OFFS 6
88#define MVPP2_CLS_LKP_TBL_REG 0x1818
89#define MVPP2_CLS_LKP_TBL_RXQ_MASK 0xff
90#define MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK BIT(25)
91#define MVPP2_CLS_FLOW_INDEX_REG 0x1820
92#define MVPP2_CLS_FLOW_TBL0_REG 0x1824
93#define MVPP2_CLS_FLOW_TBL1_REG 0x1828
94#define MVPP2_CLS_FLOW_TBL2_REG 0x182c
95#define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port) (0x1980 + ((port) * 4))
96#define MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS 3
97#define MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK 0x7
98#define MVPP2_CLS_SWFWD_P2HQ_REG(port) (0x19b0 + ((port) * 4))
99#define MVPP2_CLS_SWFWD_PCTRL_REG 0x19d0
100#define MVPP2_CLS_SWFWD_PCTRL_MASK(port) (1 << (port))
101
102/* Descriptor Manager Top Registers */
103#define MVPP2_RXQ_NUM_REG 0x2040
104#define MVPP2_RXQ_DESC_ADDR_REG 0x2044
Thomas Petazzonib02f31f2017-03-07 16:53:12 +0100105#define MVPP22_DESC_ADDR_OFFS 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300106#define MVPP2_RXQ_DESC_SIZE_REG 0x2048
107#define MVPP2_RXQ_DESC_SIZE_MASK 0x3ff0
108#define MVPP2_RXQ_STATUS_UPDATE_REG(rxq) (0x3000 + 4 * (rxq))
109#define MVPP2_RXQ_NUM_PROCESSED_OFFSET 0
110#define MVPP2_RXQ_NUM_NEW_OFFSET 16
111#define MVPP2_RXQ_STATUS_REG(rxq) (0x3400 + 4 * (rxq))
112#define MVPP2_RXQ_OCCUPIED_MASK 0x3fff
113#define MVPP2_RXQ_NON_OCCUPIED_OFFSET 16
114#define MVPP2_RXQ_NON_OCCUPIED_MASK 0x3fff0000
115#define MVPP2_RXQ_THRESH_REG 0x204c
116#define MVPP2_OCCUPIED_THRESH_OFFSET 0
117#define MVPP2_OCCUPIED_THRESH_MASK 0x3fff
118#define MVPP2_RXQ_INDEX_REG 0x2050
119#define MVPP2_TXQ_NUM_REG 0x2080
120#define MVPP2_TXQ_DESC_ADDR_REG 0x2084
121#define MVPP2_TXQ_DESC_SIZE_REG 0x2088
122#define MVPP2_TXQ_DESC_SIZE_MASK 0x3ff0
123#define MVPP2_AGGR_TXQ_UPDATE_REG 0x2090
Marcin Wojtas3f518502014-07-10 16:52:13 -0300124#define MVPP2_TXQ_INDEX_REG 0x2098
125#define MVPP2_TXQ_PREF_BUF_REG 0x209c
126#define MVPP2_PREF_BUF_PTR(desc) ((desc) & 0xfff)
127#define MVPP2_PREF_BUF_SIZE_4 (BIT(12) | BIT(13))
128#define MVPP2_PREF_BUF_SIZE_16 (BIT(12) | BIT(14))
129#define MVPP2_PREF_BUF_THRESH(val) ((val) << 17)
130#define MVPP2_TXQ_DRAIN_EN_MASK BIT(31)
131#define MVPP2_TXQ_PENDING_REG 0x20a0
132#define MVPP2_TXQ_PENDING_MASK 0x3fff
133#define MVPP2_TXQ_INT_STATUS_REG 0x20a4
134#define MVPP2_TXQ_SENT_REG(txq) (0x3c00 + 4 * (txq))
135#define MVPP2_TRANSMITTED_COUNT_OFFSET 16
136#define MVPP2_TRANSMITTED_COUNT_MASK 0x3fff0000
137#define MVPP2_TXQ_RSVD_REQ_REG 0x20b0
138#define MVPP2_TXQ_RSVD_REQ_Q_OFFSET 16
139#define MVPP2_TXQ_RSVD_RSLT_REG 0x20b4
140#define MVPP2_TXQ_RSVD_RSLT_MASK 0x3fff
141#define MVPP2_TXQ_RSVD_CLR_REG 0x20b8
142#define MVPP2_TXQ_RSVD_CLR_OFFSET 16
143#define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu) (0x2100 + 4 * (cpu))
Thomas Petazzonib02f31f2017-03-07 16:53:12 +0100144#define MVPP22_AGGR_TXQ_DESC_ADDR_OFFS 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300145#define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu) (0x2140 + 4 * (cpu))
146#define MVPP2_AGGR_TXQ_DESC_SIZE_MASK 0x3ff0
147#define MVPP2_AGGR_TXQ_STATUS_REG(cpu) (0x2180 + 4 * (cpu))
148#define MVPP2_AGGR_TXQ_PENDING_MASK 0x3fff
149#define MVPP2_AGGR_TXQ_INDEX_REG(cpu) (0x21c0 + 4 * (cpu))
150
151/* MBUS bridge registers */
152#define MVPP2_WIN_BASE(w) (0x4000 + ((w) << 2))
153#define MVPP2_WIN_SIZE(w) (0x4020 + ((w) << 2))
154#define MVPP2_WIN_REMAP(w) (0x4040 + ((w) << 2))
155#define MVPP2_BASE_ADDR_ENABLE 0x4060
156
Thomas Petazzoni6763ce32017-03-07 16:53:15 +0100157/* AXI Bridge Registers */
158#define MVPP22_AXI_BM_WR_ATTR_REG 0x4100
159#define MVPP22_AXI_BM_RD_ATTR_REG 0x4104
160#define MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG 0x4110
161#define MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG 0x4114
162#define MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG 0x4118
163#define MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG 0x411c
164#define MVPP22_AXI_RX_DATA_WR_ATTR_REG 0x4120
165#define MVPP22_AXI_TX_DATA_RD_ATTR_REG 0x4130
166#define MVPP22_AXI_RD_NORMAL_CODE_REG 0x4150
167#define MVPP22_AXI_RD_SNOOP_CODE_REG 0x4154
168#define MVPP22_AXI_WR_NORMAL_CODE_REG 0x4160
169#define MVPP22_AXI_WR_SNOOP_CODE_REG 0x4164
170
171/* Values for AXI Bridge registers */
172#define MVPP22_AXI_ATTR_CACHE_OFFS 0
173#define MVPP22_AXI_ATTR_DOMAIN_OFFS 12
174
175#define MVPP22_AXI_CODE_CACHE_OFFS 0
176#define MVPP22_AXI_CODE_DOMAIN_OFFS 4
177
178#define MVPP22_AXI_CODE_CACHE_NON_CACHE 0x3
179#define MVPP22_AXI_CODE_CACHE_WR_CACHE 0x7
180#define MVPP22_AXI_CODE_CACHE_RD_CACHE 0xb
181
182#define MVPP22_AXI_CODE_DOMAIN_OUTER_DOM 2
183#define MVPP22_AXI_CODE_DOMAIN_SYSTEM 3
184
Marcin Wojtas3f518502014-07-10 16:52:13 -0300185/* Interrupt Cause and Mask registers */
186#define MVPP2_ISR_RX_THRESHOLD_REG(rxq) (0x5200 + 4 * (rxq))
Thomas Petazzoniab426762017-02-21 11:28:04 +0100187#define MVPP2_MAX_ISR_RX_THRESHOLD 0xfffff0
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100188#define MVPP21_ISR_RXQ_GROUP_REG(rxq) (0x5400 + 4 * (rxq))
189
190#define MVPP22_ISR_RXQ_GROUP_INDEX_REG 0x5400
191#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
192#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
193#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET 7
194
195#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
196#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
197
198#define MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG 0x5404
199#define MVPP22_ISR_RXQ_SUB_GROUP_STARTQ_MASK 0x1f
200#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_MASK 0xf00
201#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET 8
202
Marcin Wojtas3f518502014-07-10 16:52:13 -0300203#define MVPP2_ISR_ENABLE_REG(port) (0x5420 + 4 * (port))
204#define MVPP2_ISR_ENABLE_INTERRUPT(mask) ((mask) & 0xffff)
205#define MVPP2_ISR_DISABLE_INTERRUPT(mask) (((mask) << 16) & 0xffff0000)
206#define MVPP2_ISR_RX_TX_CAUSE_REG(port) (0x5480 + 4 * (port))
207#define MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
208#define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
209#define MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK BIT(24)
210#define MVPP2_CAUSE_FCS_ERR_MASK BIT(25)
211#define MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK BIT(26)
212#define MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK BIT(29)
213#define MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK BIT(30)
214#define MVPP2_CAUSE_MISC_SUM_MASK BIT(31)
215#define MVPP2_ISR_RX_TX_MASK_REG(port) (0x54a0 + 4 * (port))
216#define MVPP2_ISR_PON_RX_TX_MASK_REG 0x54bc
217#define MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
218#define MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK 0x3fc00000
219#define MVPP2_PON_CAUSE_MISC_SUM_MASK BIT(31)
220#define MVPP2_ISR_MISC_CAUSE_REG 0x55b0
221
222/* Buffer Manager registers */
223#define MVPP2_BM_POOL_BASE_REG(pool) (0x6000 + ((pool) * 4))
224#define MVPP2_BM_POOL_BASE_ADDR_MASK 0xfffff80
225#define MVPP2_BM_POOL_SIZE_REG(pool) (0x6040 + ((pool) * 4))
226#define MVPP2_BM_POOL_SIZE_MASK 0xfff0
227#define MVPP2_BM_POOL_READ_PTR_REG(pool) (0x6080 + ((pool) * 4))
228#define MVPP2_BM_POOL_GET_READ_PTR_MASK 0xfff0
229#define MVPP2_BM_POOL_PTRS_NUM_REG(pool) (0x60c0 + ((pool) * 4))
230#define MVPP2_BM_POOL_PTRS_NUM_MASK 0xfff0
231#define MVPP2_BM_BPPI_READ_PTR_REG(pool) (0x6100 + ((pool) * 4))
232#define MVPP2_BM_BPPI_PTRS_NUM_REG(pool) (0x6140 + ((pool) * 4))
233#define MVPP2_BM_BPPI_PTR_NUM_MASK 0x7ff
234#define MVPP2_BM_BPPI_PREFETCH_FULL_MASK BIT(16)
235#define MVPP2_BM_POOL_CTRL_REG(pool) (0x6200 + ((pool) * 4))
236#define MVPP2_BM_START_MASK BIT(0)
237#define MVPP2_BM_STOP_MASK BIT(1)
238#define MVPP2_BM_STATE_MASK BIT(4)
239#define MVPP2_BM_LOW_THRESH_OFFS 8
240#define MVPP2_BM_LOW_THRESH_MASK 0x7f00
241#define MVPP2_BM_LOW_THRESH_VALUE(val) ((val) << \
242 MVPP2_BM_LOW_THRESH_OFFS)
243#define MVPP2_BM_HIGH_THRESH_OFFS 16
244#define MVPP2_BM_HIGH_THRESH_MASK 0x7f0000
245#define MVPP2_BM_HIGH_THRESH_VALUE(val) ((val) << \
246 MVPP2_BM_HIGH_THRESH_OFFS)
247#define MVPP2_BM_INTR_CAUSE_REG(pool) (0x6240 + ((pool) * 4))
248#define MVPP2_BM_RELEASED_DELAY_MASK BIT(0)
249#define MVPP2_BM_ALLOC_FAILED_MASK BIT(1)
250#define MVPP2_BM_BPPE_EMPTY_MASK BIT(2)
251#define MVPP2_BM_BPPE_FULL_MASK BIT(3)
252#define MVPP2_BM_AVAILABLE_BP_LOW_MASK BIT(4)
253#define MVPP2_BM_INTR_MASK_REG(pool) (0x6280 + ((pool) * 4))
254#define MVPP2_BM_PHY_ALLOC_REG(pool) (0x6400 + ((pool) * 4))
255#define MVPP2_BM_PHY_ALLOC_GRNTD_MASK BIT(0)
256#define MVPP2_BM_VIRT_ALLOC_REG 0x6440
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100257#define MVPP22_BM_ADDR_HIGH_ALLOC 0x6444
258#define MVPP22_BM_ADDR_HIGH_PHYS_MASK 0xff
259#define MVPP22_BM_ADDR_HIGH_VIRT_MASK 0xff00
260#define MVPP22_BM_ADDR_HIGH_VIRT_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300261#define MVPP2_BM_PHY_RLS_REG(pool) (0x6480 + ((pool) * 4))
262#define MVPP2_BM_PHY_RLS_MC_BUFF_MASK BIT(0)
263#define MVPP2_BM_PHY_RLS_PRIO_EN_MASK BIT(1)
264#define MVPP2_BM_PHY_RLS_GRNTD_MASK BIT(2)
265#define MVPP2_BM_VIRT_RLS_REG 0x64c0
Thomas Petazzonid01524d2017-03-07 16:53:09 +0100266#define MVPP22_BM_ADDR_HIGH_RLS_REG 0x64c4
267#define MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK 0xff
268#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK 0xff00
269#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT 8
Marcin Wojtas3f518502014-07-10 16:52:13 -0300270
271/* TX Scheduler registers */
272#define MVPP2_TXP_SCHED_PORT_INDEX_REG 0x8000
273#define MVPP2_TXP_SCHED_Q_CMD_REG 0x8004
274#define MVPP2_TXP_SCHED_ENQ_MASK 0xff
275#define MVPP2_TXP_SCHED_DISQ_OFFSET 8
276#define MVPP2_TXP_SCHED_CMD_1_REG 0x8010
277#define MVPP2_TXP_SCHED_PERIOD_REG 0x8018
278#define MVPP2_TXP_SCHED_MTU_REG 0x801c
279#define MVPP2_TXP_MTU_MAX 0x7FFFF
280#define MVPP2_TXP_SCHED_REFILL_REG 0x8020
281#define MVPP2_TXP_REFILL_TOKENS_ALL_MASK 0x7ffff
282#define MVPP2_TXP_REFILL_PERIOD_ALL_MASK 0x3ff00000
283#define MVPP2_TXP_REFILL_PERIOD_MASK(v) ((v) << 20)
284#define MVPP2_TXP_SCHED_TOKEN_SIZE_REG 0x8024
285#define MVPP2_TXP_TOKEN_SIZE_MAX 0xffffffff
286#define MVPP2_TXQ_SCHED_REFILL_REG(q) (0x8040 + ((q) << 2))
287#define MVPP2_TXQ_REFILL_TOKENS_ALL_MASK 0x7ffff
288#define MVPP2_TXQ_REFILL_PERIOD_ALL_MASK 0x3ff00000
289#define MVPP2_TXQ_REFILL_PERIOD_MASK(v) ((v) << 20)
290#define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q) (0x8060 + ((q) << 2))
291#define MVPP2_TXQ_TOKEN_SIZE_MAX 0x7fffffff
292#define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q) (0x8080 + ((q) << 2))
293#define MVPP2_TXQ_TOKEN_CNTR_MAX 0xffffffff
294
295/* TX general registers */
296#define MVPP2_TX_SNOOP_REG 0x8800
297#define MVPP2_TX_PORT_FLUSH_REG 0x8810
298#define MVPP2_TX_PORT_FLUSH_MASK(port) (1 << (port))
299
300/* LMS registers */
301#define MVPP2_SRC_ADDR_MIDDLE 0x24
302#define MVPP2_SRC_ADDR_HIGH 0x28
Marcin Wojtas08a23752014-07-21 13:48:12 -0300303#define MVPP2_PHY_AN_CFG0_REG 0x34
304#define MVPP2_PHY_AN_STOP_SMI0_MASK BIT(7)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300305#define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG 0x305c
Thomas Petazzoni31d76772017-02-21 11:28:10 +0100306#define MVPP2_EXT_GLOBAL_CTRL_DEFAULT 0x27
Marcin Wojtas3f518502014-07-10 16:52:13 -0300307
308/* Per-port registers */
309#define MVPP2_GMAC_CTRL_0_REG 0x0
310#define MVPP2_GMAC_PORT_EN_MASK BIT(0)
311#define MVPP2_GMAC_MAX_RX_SIZE_OFFS 2
312#define MVPP2_GMAC_MAX_RX_SIZE_MASK 0x7ffc
313#define MVPP2_GMAC_MIB_CNTR_EN_MASK BIT(15)
314#define MVPP2_GMAC_CTRL_1_REG 0x4
Marcin Wojtasb5c0a802014-07-21 13:48:11 -0300315#define MVPP2_GMAC_PERIODIC_XON_EN_MASK BIT(1)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300316#define MVPP2_GMAC_GMII_LB_EN_MASK BIT(5)
317#define MVPP2_GMAC_PCS_LB_EN_BIT 6
318#define MVPP2_GMAC_PCS_LB_EN_MASK BIT(6)
319#define MVPP2_GMAC_SA_LOW_OFFS 7
320#define MVPP2_GMAC_CTRL_2_REG 0x8
321#define MVPP2_GMAC_INBAND_AN_MASK BIT(0)
322#define MVPP2_GMAC_PCS_ENABLE_MASK BIT(3)
323#define MVPP2_GMAC_PORT_RGMII_MASK BIT(4)
324#define MVPP2_GMAC_PORT_RESET_MASK BIT(6)
325#define MVPP2_GMAC_AUTONEG_CONFIG 0xc
326#define MVPP2_GMAC_FORCE_LINK_DOWN BIT(0)
327#define MVPP2_GMAC_FORCE_LINK_PASS BIT(1)
328#define MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
329#define MVPP2_GMAC_CONFIG_GMII_SPEED BIT(6)
330#define MVPP2_GMAC_AN_SPEED_EN BIT(7)
Marcin Wojtas08a23752014-07-21 13:48:12 -0300331#define MVPP2_GMAC_FC_ADV_EN BIT(9)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300332#define MVPP2_GMAC_CONFIG_FULL_DUPLEX BIT(12)
333#define MVPP2_GMAC_AN_DUPLEX_EN BIT(13)
334#define MVPP2_GMAC_PORT_FIFO_CFG_1_REG 0x1c
335#define MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS 6
336#define MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
337#define MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v) (((v) << 6) & \
338 MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100339#define MVPP22_GMAC_CTRL_4_REG 0x90
340#define MVPP22_CTRL4_EXT_PIN_GMII_SEL BIT(0)
341#define MVPP22_CTRL4_DP_CLK_SEL BIT(5)
342#define MVPP22_CTRL4_SYNC_BYPASS BIT(6)
343#define MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE BIT(7)
344
345/* Per-port XGMAC registers. PPv2.2 only, only for GOP port 0,
346 * relative to port->base.
347 */
Antoine Ténart725757a2017-06-12 16:01:39 +0200348#define MVPP22_XLG_CTRL0_REG 0x100
349#define MVPP22_XLG_CTRL0_PORT_EN BIT(0)
350#define MVPP22_XLG_CTRL0_MAC_RESET_DIS BIT(1)
351#define MVPP22_XLG_CTRL0_MIB_CNT_DIS BIT(14)
352
Thomas Petazzoni26975822017-03-07 16:53:14 +0100353#define MVPP22_XLG_CTRL3_REG 0x11c
354#define MVPP22_XLG_CTRL3_MACMODESELECT_MASK (7 << 13)
355#define MVPP22_XLG_CTRL3_MACMODESELECT_GMAC (0 << 13)
Antoine Ténart725757a2017-06-12 16:01:39 +0200356#define MVPP22_XLG_CTRL3_MACMODESELECT_10G (1 << 13)
Thomas Petazzoni26975822017-03-07 16:53:14 +0100357
358/* SMI registers. PPv2.2 only, relative to priv->iface_base. */
359#define MVPP22_SMI_MISC_CFG_REG 0x1204
360#define MVPP22_SMI_POLLING_EN BIT(10)
Marcin Wojtas3f518502014-07-10 16:52:13 -0300361
Thomas Petazzonia7868412017-03-07 16:53:13 +0100362#define MVPP22_GMAC_BASE(port) (0x7000 + (port) * 0x1000 + 0xe00)
363
Marcin Wojtas3f518502014-07-10 16:52:13 -0300364#define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
365
366/* Descriptor ring Macros */
367#define MVPP2_QUEUE_NEXT_DESC(q, index) \
368 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
369
370/* Various constants */
371
372/* Coalescing */
373#define MVPP2_TXDONE_COAL_PKTS_THRESH 15
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200374#define MVPP2_TXDONE_HRTIMER_PERIOD_NS 1000000UL
Marcin Wojtas3f518502014-07-10 16:52:13 -0300375#define MVPP2_RX_COAL_PKTS 32
376#define MVPP2_RX_COAL_USEC 100
377
378/* The two bytes Marvell header. Either contains a special value used
379 * by Marvell switches when a specific hardware mode is enabled (not
380 * supported by this driver) or is filled automatically by zeroes on
381 * the RX side. Those two bytes being at the front of the Ethernet
382 * header, they allow to have the IP header aligned on a 4 bytes
383 * boundary automatically: the hardware skips those two bytes on its
384 * own.
385 */
386#define MVPP2_MH_SIZE 2
387#define MVPP2_ETH_TYPE_LEN 2
388#define MVPP2_PPPOE_HDR_SIZE 8
389#define MVPP2_VLAN_TAG_LEN 4
390
391/* Lbtd 802.3 type */
392#define MVPP2_IP_LBDT_TYPE 0xfffa
393
Marcin Wojtas3f518502014-07-10 16:52:13 -0300394#define MVPP2_TX_CSUM_MAX_SIZE 9800
395
396/* Timeout constants */
397#define MVPP2_TX_DISABLE_TIMEOUT_MSEC 1000
398#define MVPP2_TX_PENDING_TIMEOUT_MSEC 1000
399
400#define MVPP2_TX_MTU_MAX 0x7ffff
401
402/* Maximum number of T-CONTs of PON port */
403#define MVPP2_MAX_TCONT 16
404
405/* Maximum number of supported ports */
406#define MVPP2_MAX_PORTS 4
407
408/* Maximum number of TXQs used by single port */
409#define MVPP2_MAX_TXQ 8
410
Marcin Wojtas3f518502014-07-10 16:52:13 -0300411/* Dfault number of RXQs in use */
412#define MVPP2_DEFAULT_RXQ 4
413
Marcin Wojtas3f518502014-07-10 16:52:13 -0300414/* Max number of Rx descriptors */
415#define MVPP2_MAX_RXD 128
416
417/* Max number of Tx descriptors */
418#define MVPP2_MAX_TXD 1024
419
420/* Amount of Tx descriptors that can be reserved at once by CPU */
421#define MVPP2_CPU_DESC_CHUNK 64
422
423/* Max number of Tx descriptors in each aggregated queue */
424#define MVPP2_AGGR_TXQ_SIZE 256
425
426/* Descriptor aligned size */
427#define MVPP2_DESC_ALIGNED_SIZE 32
428
429/* Descriptor alignment mask */
430#define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
431
432/* RX FIFO constants */
433#define MVPP2_RX_FIFO_PORT_DATA_SIZE 0x2000
434#define MVPP2_RX_FIFO_PORT_ATTR_SIZE 0x80
435#define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
436
437/* RX buffer constants */
438#define MVPP2_SKB_SHINFO_SIZE \
439 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
440
441#define MVPP2_RX_PKT_SIZE(mtu) \
442 ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
Jisheng Zhang4a0a12d2016-04-01 17:11:05 +0800443 ETH_HLEN + ETH_FCS_LEN, cache_line_size())
Marcin Wojtas3f518502014-07-10 16:52:13 -0300444
445#define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
446#define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
447#define MVPP2_RX_MAX_PKT_SIZE(total_size) \
448 ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
449
450#define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
451
452/* IPv6 max L3 address size */
453#define MVPP2_MAX_L3_ADDR_SIZE 16
454
455/* Port flags */
456#define MVPP2_F_LOOPBACK BIT(0)
457
458/* Marvell tag types */
459enum mvpp2_tag_type {
460 MVPP2_TAG_TYPE_NONE = 0,
461 MVPP2_TAG_TYPE_MH = 1,
462 MVPP2_TAG_TYPE_DSA = 2,
463 MVPP2_TAG_TYPE_EDSA = 3,
464 MVPP2_TAG_TYPE_VLAN = 4,
465 MVPP2_TAG_TYPE_LAST = 5
466};
467
468/* Parser constants */
469#define MVPP2_PRS_TCAM_SRAM_SIZE 256
470#define MVPP2_PRS_TCAM_WORDS 6
471#define MVPP2_PRS_SRAM_WORDS 4
472#define MVPP2_PRS_FLOW_ID_SIZE 64
473#define MVPP2_PRS_FLOW_ID_MASK 0x3f
474#define MVPP2_PRS_TCAM_ENTRY_INVALID 1
475#define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
476#define MVPP2_PRS_IPV4_HEAD 0x40
477#define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
478#define MVPP2_PRS_IPV4_MC 0xe0
479#define MVPP2_PRS_IPV4_MC_MASK 0xf0
480#define MVPP2_PRS_IPV4_BC_MASK 0xff
481#define MVPP2_PRS_IPV4_IHL 0x5
482#define MVPP2_PRS_IPV4_IHL_MASK 0xf
483#define MVPP2_PRS_IPV6_MC 0xff
484#define MVPP2_PRS_IPV6_MC_MASK 0xff
485#define MVPP2_PRS_IPV6_HOP_MASK 0xff
486#define MVPP2_PRS_TCAM_PROTO_MASK 0xff
487#define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
488#define MVPP2_PRS_DBL_VLANS_MAX 100
489
490/* Tcam structure:
491 * - lookup ID - 4 bits
492 * - port ID - 1 byte
493 * - additional information - 1 byte
494 * - header data - 8 bytes
495 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
496 */
497#define MVPP2_PRS_AI_BITS 8
498#define MVPP2_PRS_PORT_MASK 0xff
499#define MVPP2_PRS_LU_MASK 0xf
500#define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
501 (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
502#define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
503 (((offs) * 2) - ((offs) % 2) + 2)
504#define MVPP2_PRS_TCAM_AI_BYTE 16
505#define MVPP2_PRS_TCAM_PORT_BYTE 17
506#define MVPP2_PRS_TCAM_LU_BYTE 20
507#define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
508#define MVPP2_PRS_TCAM_INV_WORD 5
509/* Tcam entries ID */
510#define MVPP2_PE_DROP_ALL 0
511#define MVPP2_PE_FIRST_FREE_TID 1
512#define MVPP2_PE_LAST_FREE_TID (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
513#define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
514#define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
515#define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
516#define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
517#define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
518#define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
519#define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
520#define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
521#define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
522#define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
523#define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
524#define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
525#define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
526#define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
527#define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
528#define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
529#define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
530#define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
531#define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
532#define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
533#define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
534#define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
535#define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
536#define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
537
538/* Sram structure
539 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
540 */
541#define MVPP2_PRS_SRAM_RI_OFFS 0
542#define MVPP2_PRS_SRAM_RI_WORD 0
543#define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
544#define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
545#define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
546#define MVPP2_PRS_SRAM_SHIFT_OFFS 64
547#define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
548#define MVPP2_PRS_SRAM_UDF_OFFS 73
549#define MVPP2_PRS_SRAM_UDF_BITS 8
550#define MVPP2_PRS_SRAM_UDF_MASK 0xff
551#define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
552#define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
553#define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
554#define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
555#define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
556#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
557#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
558#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
559#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
560#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
561#define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
562#define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
563#define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
564#define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
565#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
566#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
567#define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
568#define MVPP2_PRS_SRAM_AI_OFFS 90
569#define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
570#define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
571#define MVPP2_PRS_SRAM_AI_MASK 0xff
572#define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
573#define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
574#define MVPP2_PRS_SRAM_LU_DONE_BIT 110
575#define MVPP2_PRS_SRAM_LU_GEN_BIT 111
576
577/* Sram result info bits assignment */
578#define MVPP2_PRS_RI_MAC_ME_MASK 0x1
579#define MVPP2_PRS_RI_DSA_MASK 0x2
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100580#define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
581#define MVPP2_PRS_RI_VLAN_NONE 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300582#define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
583#define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
584#define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
585#define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
586#define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100587#define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
588#define MVPP2_PRS_RI_L2_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300589#define MVPP2_PRS_RI_L2_MCAST BIT(9)
590#define MVPP2_PRS_RI_L2_BCAST BIT(10)
591#define MVPP2_PRS_RI_PPPOE_MASK 0x800
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100592#define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
593#define MVPP2_PRS_RI_L3_UN 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300594#define MVPP2_PRS_RI_L3_IP4 BIT(12)
595#define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
596#define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
597#define MVPP2_PRS_RI_L3_IP6 BIT(14)
598#define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
599#define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
Thomas Petazzoni8138aff2017-02-21 11:28:11 +0100600#define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
601#define MVPP2_PRS_RI_L3_UCAST 0x0
Marcin Wojtas3f518502014-07-10 16:52:13 -0300602#define MVPP2_PRS_RI_L3_MCAST BIT(15)
603#define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
604#define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
605#define MVPP2_PRS_RI_UDF3_MASK 0x300000
606#define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
607#define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
608#define MVPP2_PRS_RI_L4_TCP BIT(22)
609#define MVPP2_PRS_RI_L4_UDP BIT(23)
610#define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
611#define MVPP2_PRS_RI_UDF7_MASK 0x60000000
612#define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
613#define MVPP2_PRS_RI_DROP_MASK 0x80000000
614
615/* Sram additional info bits assignment */
616#define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
617#define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
618#define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
619#define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
620#define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
621#define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
622#define MVPP2_PRS_SINGLE_VLAN_AI 0
623#define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
624
625/* DSA/EDSA type */
626#define MVPP2_PRS_TAGGED true
627#define MVPP2_PRS_UNTAGGED false
628#define MVPP2_PRS_EDSA true
629#define MVPP2_PRS_DSA false
630
631/* MAC entries, shadow udf */
632enum mvpp2_prs_udf {
633 MVPP2_PRS_UDF_MAC_DEF,
634 MVPP2_PRS_UDF_MAC_RANGE,
635 MVPP2_PRS_UDF_L2_DEF,
636 MVPP2_PRS_UDF_L2_DEF_COPY,
637 MVPP2_PRS_UDF_L2_USER,
638};
639
640/* Lookup ID */
641enum mvpp2_prs_lookup {
642 MVPP2_PRS_LU_MH,
643 MVPP2_PRS_LU_MAC,
644 MVPP2_PRS_LU_DSA,
645 MVPP2_PRS_LU_VLAN,
646 MVPP2_PRS_LU_L2,
647 MVPP2_PRS_LU_PPPOE,
648 MVPP2_PRS_LU_IP4,
649 MVPP2_PRS_LU_IP6,
650 MVPP2_PRS_LU_FLOWS,
651 MVPP2_PRS_LU_LAST,
652};
653
654/* L3 cast enum */
655enum mvpp2_prs_l3_cast {
656 MVPP2_PRS_L3_UNI_CAST,
657 MVPP2_PRS_L3_MULTI_CAST,
658 MVPP2_PRS_L3_BROAD_CAST
659};
660
661/* Classifier constants */
662#define MVPP2_CLS_FLOWS_TBL_SIZE 512
663#define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
664#define MVPP2_CLS_LKP_TBL_SIZE 64
665
666/* BM constants */
667#define MVPP2_BM_POOLS_NUM 8
668#define MVPP2_BM_LONG_BUF_NUM 1024
669#define MVPP2_BM_SHORT_BUF_NUM 2048
670#define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
671#define MVPP2_BM_POOL_PTR_ALIGN 128
672#define MVPP2_BM_SWF_LONG_POOL(port) ((port > 2) ? 2 : port)
673#define MVPP2_BM_SWF_SHORT_POOL 3
674
675/* BM cookie (32 bits) definition */
676#define MVPP2_BM_COOKIE_POOL_OFFS 8
677#define MVPP2_BM_COOKIE_CPU_OFFS 24
678
679/* BM short pool packet size
680 * These value assure that for SWF the total number
681 * of bytes allocated for each buffer will be 512
682 */
683#define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(512)
684
Thomas Petazzonia7868412017-03-07 16:53:13 +0100685#define MVPP21_ADDR_SPACE_SZ 0
686#define MVPP22_ADDR_SPACE_SZ SZ_64K
687
688#define MVPP2_MAX_CPUS 4
689
Marcin Wojtas3f518502014-07-10 16:52:13 -0300690enum 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 */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300701 void __iomem *lms_base;
Thomas Petazzonia7868412017-03-07 16:53:13 +0100702 void __iomem *iface_base;
703
704 /* On PPv2.2, each CPU can access the base register through a
705 * separate address space, each 64 KB apart from each
706 * other.
707 */
708 void __iomem *cpu_base[MVPP2_MAX_CPUS];
Marcin Wojtas3f518502014-07-10 16:52:13 -0300709
710 /* Common clocks */
711 struct clk *pp_clk;
712 struct clk *gop_clk;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +0100713 struct clk *mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300714
715 /* List of pointers to port structures */
716 struct mvpp2_port **port_list;
717
718 /* Aggregated TXQs */
719 struct mvpp2_tx_queue *aggr_txqs;
720
721 /* BM pools */
722 struct mvpp2_bm_pool *bm_pools;
723
724 /* PRS shadow table */
725 struct mvpp2_prs_shadow *prs_shadow;
726 /* PRS auxiliary table for double vlan entries control */
727 bool *prs_double_vlans;
728
729 /* Tclk value */
730 u32 tclk;
Thomas Petazzonifaca9242017-03-07 16:53:06 +0100731
732 /* HW version */
733 enum { MVPP21, MVPP22 } hw_version;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +0100734
735 /* Maximum number of RXQs per port */
736 unsigned int max_port_rxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300737};
738
739struct mvpp2_pcpu_stats {
740 struct u64_stats_sync syncp;
741 u64 rx_packets;
742 u64 rx_bytes;
743 u64 tx_packets;
744 u64 tx_bytes;
745};
746
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200747/* Per-CPU port control */
748struct mvpp2_port_pcpu {
749 struct hrtimer tx_done_timer;
750 bool timer_scheduled;
751 /* Tasklet for egress finalization */
752 struct tasklet_struct tx_done_tasklet;
753};
754
Marcin Wojtas3f518502014-07-10 16:52:13 -0300755struct mvpp2_port {
756 u8 id;
757
Thomas Petazzonia7868412017-03-07 16:53:13 +0100758 /* Index of the port from the "group of ports" complex point
759 * of view
760 */
761 int gop_id;
762
Marcin Wojtas3f518502014-07-10 16:52:13 -0300763 int irq;
764
765 struct mvpp2 *priv;
766
767 /* Per-port registers' base address */
768 void __iomem *base;
769
770 struct mvpp2_rx_queue **rxqs;
771 struct mvpp2_tx_queue **txqs;
772 struct net_device *dev;
773
774 int pkt_size;
775
776 u32 pending_cause_rx;
777 struct napi_struct napi;
778
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200779 /* Per-CPU port control */
780 struct mvpp2_port_pcpu __percpu *pcpu;
781
Marcin Wojtas3f518502014-07-10 16:52:13 -0300782 /* Flags */
783 unsigned long flags;
784
785 u16 tx_ring_size;
786 u16 rx_ring_size;
787 struct mvpp2_pcpu_stats __percpu *stats;
788
Marcin Wojtas3f518502014-07-10 16:52:13 -0300789 phy_interface_t phy_interface;
790 struct device_node *phy_node;
791 unsigned int link;
792 unsigned int duplex;
793 unsigned int speed;
794
795 struct mvpp2_bm_pool *pool_long;
796 struct mvpp2_bm_pool *pool_short;
797
798 /* Index of first port's physical RXQ */
799 u8 first_rxq;
800};
801
802/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
803 * layout of the transmit and reception DMA descriptors, and their
804 * layout is therefore defined by the hardware design
805 */
806
807#define MVPP2_TXD_L3_OFF_SHIFT 0
808#define MVPP2_TXD_IP_HLEN_SHIFT 8
809#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
810#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
811#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
812#define MVPP2_TXD_PADDING_DISABLE BIT(23)
813#define MVPP2_TXD_L4_UDP BIT(24)
814#define MVPP2_TXD_L3_IP6 BIT(26)
815#define MVPP2_TXD_L_DESC BIT(28)
816#define MVPP2_TXD_F_DESC BIT(29)
817
818#define MVPP2_RXD_ERR_SUMMARY BIT(15)
819#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
820#define MVPP2_RXD_ERR_CRC 0x0
821#define MVPP2_RXD_ERR_OVERRUN BIT(13)
822#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
823#define MVPP2_RXD_BM_POOL_ID_OFFS 16
824#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
825#define MVPP2_RXD_HWF_SYNC BIT(21)
826#define MVPP2_RXD_L4_CSUM_OK BIT(22)
827#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
828#define MVPP2_RXD_L4_TCP BIT(25)
829#define MVPP2_RXD_L4_UDP BIT(26)
830#define MVPP2_RXD_L3_IP4 BIT(28)
831#define MVPP2_RXD_L3_IP6 BIT(30)
832#define MVPP2_RXD_BUF_HDR BIT(31)
833
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100834/* HW TX descriptor for PPv2.1 */
835struct mvpp21_tx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300836 u32 command; /* Options used by HW for packet transmitting.*/
837 u8 packet_offset; /* the offset from the buffer beginning */
838 u8 phys_txq; /* destination queue ID */
839 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100840 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300841 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
842 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
843 u32 reserved2; /* reserved (for future use) */
844};
845
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100846/* HW RX descriptor for PPv2.1 */
847struct mvpp21_rx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300848 u32 status; /* info about received packet */
849 u16 reserved1; /* parser_info (for future use, PnC) */
850 u16 data_size; /* size of received packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100851 u32 buf_dma_addr; /* physical address of the buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300852 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
853 u16 reserved2; /* gem_port_id (for future use, PON) */
854 u16 reserved3; /* csum_l4 (for future use, PnC) */
855 u8 reserved4; /* bm_qset (for future use, BM) */
856 u8 reserved5;
857 u16 reserved6; /* classify_info (for future use, PnC) */
858 u32 reserved7; /* flow_id (for future use, PnC) */
859 u32 reserved8;
860};
861
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100862/* HW TX descriptor for PPv2.2 */
863struct mvpp22_tx_desc {
864 u32 command;
865 u8 packet_offset;
866 u8 phys_txq;
867 u16 data_size;
868 u64 reserved1;
869 u64 buf_dma_addr_ptp;
870 u64 buf_cookie_misc;
871};
872
873/* HW RX descriptor for PPv2.2 */
874struct mvpp22_rx_desc {
875 u32 status;
876 u16 reserved1;
877 u16 data_size;
878 u32 reserved2;
879 u32 reserved3;
880 u64 buf_dma_addr_key_hash;
881 u64 buf_cookie_misc;
882};
883
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100884/* Opaque type used by the driver to manipulate the HW TX and RX
885 * descriptors
886 */
887struct mvpp2_tx_desc {
888 union {
889 struct mvpp21_tx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100890 struct mvpp22_tx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100891 };
892};
893
894struct mvpp2_rx_desc {
895 union {
896 struct mvpp21_rx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100897 struct mvpp22_rx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100898 };
899};
900
Thomas Petazzoni83544912016-12-21 11:28:49 +0100901struct mvpp2_txq_pcpu_buf {
902 /* Transmitted SKB */
903 struct sk_buff *skb;
904
905 /* Physical address of transmitted buffer */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100906 dma_addr_t dma;
Thomas Petazzoni83544912016-12-21 11:28:49 +0100907
908 /* Size transmitted */
909 size_t size;
910};
911
Marcin Wojtas3f518502014-07-10 16:52:13 -0300912/* Per-CPU Tx queue control */
913struct mvpp2_txq_pcpu {
914 int cpu;
915
916 /* Number of Tx DMA descriptors in the descriptor ring */
917 int size;
918
919 /* Number of currently used Tx DMA descriptor in the
920 * descriptor ring
921 */
922 int count;
923
924 /* Number of Tx DMA descriptors reserved for each CPU */
925 int reserved_num;
926
Thomas Petazzoni83544912016-12-21 11:28:49 +0100927 /* Infos about transmitted buffers */
928 struct mvpp2_txq_pcpu_buf *buffs;
Marcin Wojtas71ce3912015-08-06 19:00:29 +0200929
Marcin Wojtas3f518502014-07-10 16:52:13 -0300930 /* Index of last TX DMA descriptor that was inserted */
931 int txq_put_index;
932
933 /* Index of the TX DMA descriptor to be cleaned up */
934 int txq_get_index;
935};
936
937struct mvpp2_tx_queue {
938 /* Physical number of this Tx queue */
939 u8 id;
940
941 /* Logical number of this Tx queue */
942 u8 log_id;
943
944 /* Number of Tx DMA descriptors in the descriptor ring */
945 int size;
946
947 /* Number of currently used Tx DMA descriptor in the descriptor ring */
948 int count;
949
950 /* Per-CPU control of physical Tx queues */
951 struct mvpp2_txq_pcpu __percpu *pcpu;
952
Marcin Wojtas3f518502014-07-10 16:52:13 -0300953 u32 done_pkts_coal;
954
955 /* Virtual address of thex Tx DMA descriptors array */
956 struct mvpp2_tx_desc *descs;
957
958 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100959 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300960
961 /* Index of the last Tx DMA descriptor */
962 int last_desc;
963
964 /* Index of the next Tx DMA descriptor to process */
965 int next_desc_to_proc;
966};
967
968struct mvpp2_rx_queue {
969 /* RX queue number, in the range 0-31 for physical RXQs */
970 u8 id;
971
972 /* Num of rx descriptors in the rx descriptor ring */
973 int size;
974
975 u32 pkts_coal;
976 u32 time_coal;
977
978 /* Virtual address of the RX DMA descriptors array */
979 struct mvpp2_rx_desc *descs;
980
981 /* DMA address of the RX DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100982 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300983
984 /* Index of the last RX DMA descriptor */
985 int last_desc;
986
987 /* Index of the next RX DMA descriptor to process */
988 int next_desc_to_proc;
989
990 /* ID of port to which physical RXQ is mapped */
991 int port;
992
993 /* Port's logic RXQ number to which physical RXQ is mapped */
994 int logic_rxq;
995};
996
997union mvpp2_prs_tcam_entry {
998 u32 word[MVPP2_PRS_TCAM_WORDS];
999 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
1000};
1001
1002union mvpp2_prs_sram_entry {
1003 u32 word[MVPP2_PRS_SRAM_WORDS];
1004 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
1005};
1006
1007struct mvpp2_prs_entry {
1008 u32 index;
1009 union mvpp2_prs_tcam_entry tcam;
1010 union mvpp2_prs_sram_entry sram;
1011};
1012
1013struct mvpp2_prs_shadow {
1014 bool valid;
1015 bool finish;
1016
1017 /* Lookup ID */
1018 int lu;
1019
1020 /* User defined offset */
1021 int udf;
1022
1023 /* Result info */
1024 u32 ri;
1025 u32 ri_mask;
1026};
1027
1028struct mvpp2_cls_flow_entry {
1029 u32 index;
1030 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1031};
1032
1033struct mvpp2_cls_lookup_entry {
1034 u32 lkpid;
1035 u32 way;
1036 u32 data;
1037};
1038
1039struct mvpp2_bm_pool {
1040 /* Pool number in the range 0-7 */
1041 int id;
1042 enum mvpp2_bm_type type;
1043
1044 /* Buffer Pointers Pool External (BPPE) size */
1045 int size;
Thomas Petazzonid01524d2017-03-07 16:53:09 +01001046 /* BPPE size in bytes */
1047 int size_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001048 /* Number of buffers for this pool */
1049 int buf_num;
1050 /* Pool buffer size */
1051 int buf_size;
1052 /* Packet size */
1053 int pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01001054 int frag_size;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001055
1056 /* BPPE virtual base address */
1057 u32 *virt_addr;
Thomas Petazzoni20396132017-03-07 16:53:00 +01001058 /* BPPE DMA base address */
1059 dma_addr_t dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001060
1061 /* Ports using BM pool */
1062 u32 port_map;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001063};
1064
Marcin Wojtas3f518502014-07-10 16:52:13 -03001065/* Static declaractions */
1066
1067/* Number of RXQs used by single port */
1068static int rxq_number = MVPP2_DEFAULT_RXQ;
1069/* Number of TXQs used by single port */
1070static int txq_number = MVPP2_MAX_TXQ;
1071
1072#define MVPP2_DRIVER_NAME "mvpp2"
1073#define MVPP2_DRIVER_VERSION "1.0"
1074
1075/* Utility/helper methods */
1076
1077static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1078{
Thomas Petazzonia7868412017-03-07 16:53:13 +01001079 writel(data, priv->cpu_base[0] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001080}
1081
1082static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1083{
Thomas Petazzonia7868412017-03-07 16:53:13 +01001084 return readl(priv->cpu_base[0] + offset);
1085}
1086
1087/* These accessors should be used to access:
1088 *
1089 * - per-CPU registers, where each CPU has its own copy of the
1090 * register.
1091 *
1092 * MVPP2_BM_VIRT_ALLOC_REG
1093 * MVPP2_BM_ADDR_HIGH_ALLOC
1094 * MVPP22_BM_ADDR_HIGH_RLS_REG
1095 * MVPP2_BM_VIRT_RLS_REG
1096 * MVPP2_ISR_RX_TX_CAUSE_REG
1097 * MVPP2_ISR_RX_TX_MASK_REG
1098 * MVPP2_TXQ_NUM_REG
1099 * MVPP2_AGGR_TXQ_UPDATE_REG
1100 * MVPP2_TXQ_RSVD_REQ_REG
1101 * MVPP2_TXQ_RSVD_RSLT_REG
1102 * MVPP2_TXQ_SENT_REG
1103 * MVPP2_RXQ_NUM_REG
1104 *
1105 * - global registers that must be accessed through a specific CPU
1106 * window, because they are related to an access to a per-CPU
1107 * register
1108 *
1109 * MVPP2_BM_PHY_ALLOC_REG (related to MVPP2_BM_VIRT_ALLOC_REG)
1110 * MVPP2_BM_PHY_RLS_REG (related to MVPP2_BM_VIRT_RLS_REG)
1111 * MVPP2_RXQ_THRESH_REG (related to MVPP2_RXQ_NUM_REG)
1112 * MVPP2_RXQ_DESC_ADDR_REG (related to MVPP2_RXQ_NUM_REG)
1113 * MVPP2_RXQ_DESC_SIZE_REG (related to MVPP2_RXQ_NUM_REG)
1114 * MVPP2_RXQ_INDEX_REG (related to MVPP2_RXQ_NUM_REG)
1115 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1116 * MVPP2_TXQ_DESC_ADDR_REG (related to MVPP2_TXQ_NUM_REG)
1117 * MVPP2_TXQ_DESC_SIZE_REG (related to MVPP2_TXQ_NUM_REG)
1118 * MVPP2_TXQ_INDEX_REG (related to MVPP2_TXQ_NUM_REG)
1119 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1120 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1121 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1122 */
1123static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
1124 u32 offset, u32 data)
1125{
1126 writel(data, priv->cpu_base[cpu] + offset);
1127}
1128
1129static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
1130 u32 offset)
1131{
1132 return readl(priv->cpu_base[cpu] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001133}
1134
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001135static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1136 struct mvpp2_tx_desc *tx_desc)
1137{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001138 if (port->priv->hw_version == MVPP21)
1139 return tx_desc->pp21.buf_dma_addr;
1140 else
1141 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001142}
1143
1144static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1145 struct mvpp2_tx_desc *tx_desc,
1146 dma_addr_t dma_addr)
1147{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001148 if (port->priv->hw_version == MVPP21) {
1149 tx_desc->pp21.buf_dma_addr = dma_addr;
1150 } else {
1151 u64 val = (u64)dma_addr;
1152
1153 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1154 tx_desc->pp22.buf_dma_addr_ptp |= val;
1155 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001156}
1157
1158static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
1159 struct mvpp2_tx_desc *tx_desc)
1160{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001161 if (port->priv->hw_version == MVPP21)
1162 return tx_desc->pp21.data_size;
1163 else
1164 return tx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001165}
1166
1167static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1168 struct mvpp2_tx_desc *tx_desc,
1169 size_t size)
1170{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001171 if (port->priv->hw_version == MVPP21)
1172 tx_desc->pp21.data_size = size;
1173 else
1174 tx_desc->pp22.data_size = size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001175}
1176
1177static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1178 struct mvpp2_tx_desc *tx_desc,
1179 unsigned int txq)
1180{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001181 if (port->priv->hw_version == MVPP21)
1182 tx_desc->pp21.phys_txq = txq;
1183 else
1184 tx_desc->pp22.phys_txq = txq;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001185}
1186
1187static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1188 struct mvpp2_tx_desc *tx_desc,
1189 unsigned int command)
1190{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001191 if (port->priv->hw_version == MVPP21)
1192 tx_desc->pp21.command = command;
1193 else
1194 tx_desc->pp22.command = command;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001195}
1196
1197static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
1198 struct mvpp2_tx_desc *tx_desc,
1199 unsigned int offset)
1200{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001201 if (port->priv->hw_version == MVPP21)
1202 tx_desc->pp21.packet_offset = offset;
1203 else
1204 tx_desc->pp22.packet_offset = offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001205}
1206
1207static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1208 struct mvpp2_tx_desc *tx_desc)
1209{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001210 if (port->priv->hw_version == MVPP21)
1211 return tx_desc->pp21.packet_offset;
1212 else
1213 return tx_desc->pp22.packet_offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001214}
1215
1216static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1217 struct mvpp2_rx_desc *rx_desc)
1218{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001219 if (port->priv->hw_version == MVPP21)
1220 return rx_desc->pp21.buf_dma_addr;
1221 else
1222 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001223}
1224
1225static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1226 struct mvpp2_rx_desc *rx_desc)
1227{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001228 if (port->priv->hw_version == MVPP21)
1229 return rx_desc->pp21.buf_cookie;
1230 else
1231 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001232}
1233
1234static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1235 struct mvpp2_rx_desc *rx_desc)
1236{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001237 if (port->priv->hw_version == MVPP21)
1238 return rx_desc->pp21.data_size;
1239 else
1240 return rx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001241}
1242
1243static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1244 struct mvpp2_rx_desc *rx_desc)
1245{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001246 if (port->priv->hw_version == MVPP21)
1247 return rx_desc->pp21.status;
1248 else
1249 return rx_desc->pp22.status;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001250}
1251
Marcin Wojtas3f518502014-07-10 16:52:13 -03001252static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1253{
1254 txq_pcpu->txq_get_index++;
1255 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1256 txq_pcpu->txq_get_index = 0;
1257}
1258
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001259static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1260 struct mvpp2_txq_pcpu *txq_pcpu,
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001261 struct sk_buff *skb,
1262 struct mvpp2_tx_desc *tx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03001263{
Thomas Petazzoni83544912016-12-21 11:28:49 +01001264 struct mvpp2_txq_pcpu_buf *tx_buf =
1265 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1266 tx_buf->skb = skb;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001267 tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1268 tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1269 mvpp2_txdesc_offset_get(port, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001270 txq_pcpu->txq_put_index++;
1271 if (txq_pcpu->txq_put_index == txq_pcpu->size)
1272 txq_pcpu->txq_put_index = 0;
1273}
1274
1275/* Get number of physical egress port */
1276static inline int mvpp2_egress_port(struct mvpp2_port *port)
1277{
1278 return MVPP2_MAX_TCONT + port->id;
1279}
1280
1281/* Get number of physical TXQ */
1282static inline int mvpp2_txq_phys(int port, int txq)
1283{
1284 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1285}
1286
1287/* Parser configuration routines */
1288
1289/* Update parser tcam and sram hw entries */
1290static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1291{
1292 int i;
1293
1294 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1295 return -EINVAL;
1296
1297 /* Clear entry invalidation bit */
1298 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1299
1300 /* Write tcam index - indirect access */
1301 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1302 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1303 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1304
1305 /* Write sram index - indirect access */
1306 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1307 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1308 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1309
1310 return 0;
1311}
1312
1313/* Read tcam entry from hw */
1314static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1315{
1316 int i;
1317
1318 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1319 return -EINVAL;
1320
1321 /* Write tcam index - indirect access */
1322 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1323
1324 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1325 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1326 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1327 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1328
1329 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1330 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1331
1332 /* Write sram index - indirect access */
1333 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1334 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1335 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1336
1337 return 0;
1338}
1339
1340/* Invalidate tcam hw entry */
1341static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1342{
1343 /* Write index - indirect access */
1344 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1345 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1346 MVPP2_PRS_TCAM_INV_MASK);
1347}
1348
1349/* Enable shadow table entry and set its lookup ID */
1350static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1351{
1352 priv->prs_shadow[index].valid = true;
1353 priv->prs_shadow[index].lu = lu;
1354}
1355
1356/* Update ri fields in shadow table entry */
1357static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1358 unsigned int ri, unsigned int ri_mask)
1359{
1360 priv->prs_shadow[index].ri_mask = ri_mask;
1361 priv->prs_shadow[index].ri = ri;
1362}
1363
1364/* Update lookup field in tcam sw entry */
1365static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1366{
1367 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1368
1369 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1370 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1371}
1372
1373/* Update mask for single port in tcam sw entry */
1374static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1375 unsigned int port, bool add)
1376{
1377 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1378
1379 if (add)
1380 pe->tcam.byte[enable_off] &= ~(1 << port);
1381 else
1382 pe->tcam.byte[enable_off] |= 1 << port;
1383}
1384
1385/* Update port map in tcam sw entry */
1386static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1387 unsigned int ports)
1388{
1389 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1390 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1391
1392 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1393 pe->tcam.byte[enable_off] &= ~port_mask;
1394 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1395}
1396
1397/* Obtain port map from tcam sw entry */
1398static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1399{
1400 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1401
1402 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1403}
1404
1405/* Set byte of data and its enable bits in tcam sw entry */
1406static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1407 unsigned int offs, unsigned char byte,
1408 unsigned char enable)
1409{
1410 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1411 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1412}
1413
1414/* Get byte of data and its enable bits from tcam sw entry */
1415static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1416 unsigned int offs, unsigned char *byte,
1417 unsigned char *enable)
1418{
1419 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1420 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1421}
1422
1423/* Compare tcam data bytes with a pattern */
1424static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1425 u16 data)
1426{
1427 int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1428 u16 tcam_data;
1429
1430 tcam_data = (8 << pe->tcam.byte[off + 1]) | pe->tcam.byte[off];
1431 if (tcam_data != data)
1432 return false;
1433 return true;
1434}
1435
1436/* Update ai bits in tcam sw entry */
1437static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1438 unsigned int bits, unsigned int enable)
1439{
1440 int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1441
1442 for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1443
1444 if (!(enable & BIT(i)))
1445 continue;
1446
1447 if (bits & BIT(i))
1448 pe->tcam.byte[ai_idx] |= 1 << i;
1449 else
1450 pe->tcam.byte[ai_idx] &= ~(1 << i);
1451 }
1452
1453 pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1454}
1455
1456/* Get ai bits from tcam sw entry */
1457static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1458{
1459 return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1460}
1461
1462/* Set ethertype in tcam sw entry */
1463static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1464 unsigned short ethertype)
1465{
1466 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1467 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1468}
1469
1470/* Set bits in sram sw entry */
1471static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1472 int val)
1473{
1474 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1475}
1476
1477/* Clear bits in sram sw entry */
1478static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1479 int val)
1480{
1481 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1482}
1483
1484/* Update ri bits in sram sw entry */
1485static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1486 unsigned int bits, unsigned int mask)
1487{
1488 unsigned int i;
1489
1490 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1491 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1492
1493 if (!(mask & BIT(i)))
1494 continue;
1495
1496 if (bits & BIT(i))
1497 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1498 else
1499 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1500
1501 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1502 }
1503}
1504
1505/* Obtain ri bits from sram sw entry */
1506static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1507{
1508 return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1509}
1510
1511/* Update ai bits in sram sw entry */
1512static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1513 unsigned int bits, unsigned int mask)
1514{
1515 unsigned int i;
1516 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1517
1518 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1519
1520 if (!(mask & BIT(i)))
1521 continue;
1522
1523 if (bits & BIT(i))
1524 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1525 else
1526 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1527
1528 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1529 }
1530}
1531
1532/* Read ai bits from sram sw entry */
1533static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1534{
1535 u8 bits;
1536 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1537 int ai_en_off = ai_off + 1;
1538 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1539
1540 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1541 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1542
1543 return bits;
1544}
1545
1546/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1547 * lookup interation
1548 */
1549static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1550 unsigned int lu)
1551{
1552 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1553
1554 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1555 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1556 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1557}
1558
1559/* In the sram sw entry set sign and value of the next lookup offset
1560 * and the offset value generated to the classifier
1561 */
1562static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1563 unsigned int op)
1564{
1565 /* Set sign */
1566 if (shift < 0) {
1567 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1568 shift = 0 - shift;
1569 } else {
1570 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1571 }
1572
1573 /* Set value */
1574 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1575 (unsigned char)shift;
1576
1577 /* Reset and set operation */
1578 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1579 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1580 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1581
1582 /* Set base offset as current */
1583 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1584}
1585
1586/* In the sram sw entry set sign and value of the user defined offset
1587 * generated to the classifier
1588 */
1589static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1590 unsigned int type, int offset,
1591 unsigned int op)
1592{
1593 /* Set sign */
1594 if (offset < 0) {
1595 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1596 offset = 0 - offset;
1597 } else {
1598 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1599 }
1600
1601 /* Set value */
1602 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1603 MVPP2_PRS_SRAM_UDF_MASK);
1604 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1605 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1606 MVPP2_PRS_SRAM_UDF_BITS)] &=
1607 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1608 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1609 MVPP2_PRS_SRAM_UDF_BITS)] |=
1610 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1611
1612 /* Set offset type */
1613 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1614 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1615 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1616
1617 /* Set offset operation */
1618 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1619 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1620 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1621
1622 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1623 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1624 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1625 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1626
1627 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1628 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1629 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1630
1631 /* Set base offset as current */
1632 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1633}
1634
1635/* Find parser flow entry */
1636static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1637{
1638 struct mvpp2_prs_entry *pe;
1639 int tid;
1640
1641 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1642 if (!pe)
1643 return NULL;
1644 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1645
1646 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1647 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1648 u8 bits;
1649
1650 if (!priv->prs_shadow[tid].valid ||
1651 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1652 continue;
1653
1654 pe->index = tid;
1655 mvpp2_prs_hw_read(priv, pe);
1656 bits = mvpp2_prs_sram_ai_get(pe);
1657
1658 /* Sram store classification lookup ID in AI bits [5:0] */
1659 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1660 return pe;
1661 }
1662 kfree(pe);
1663
1664 return NULL;
1665}
1666
1667/* Return first free tcam index, seeking from start to end */
1668static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1669 unsigned char end)
1670{
1671 int tid;
1672
1673 if (start > end)
1674 swap(start, end);
1675
1676 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1677 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1678
1679 for (tid = start; tid <= end; tid++) {
1680 if (!priv->prs_shadow[tid].valid)
1681 return tid;
1682 }
1683
1684 return -EINVAL;
1685}
1686
1687/* Enable/disable dropping all mac da's */
1688static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1689{
1690 struct mvpp2_prs_entry pe;
1691
1692 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1693 /* Entry exist - update port only */
1694 pe.index = MVPP2_PE_DROP_ALL;
1695 mvpp2_prs_hw_read(priv, &pe);
1696 } else {
1697 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001698 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001699 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1700 pe.index = MVPP2_PE_DROP_ALL;
1701
1702 /* Non-promiscuous mode for all ports - DROP unknown packets */
1703 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1704 MVPP2_PRS_RI_DROP_MASK);
1705
1706 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1707 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1708
1709 /* Update shadow table */
1710 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1711
1712 /* Mask all ports */
1713 mvpp2_prs_tcam_port_map_set(&pe, 0);
1714 }
1715
1716 /* Update port mask */
1717 mvpp2_prs_tcam_port_set(&pe, port, add);
1718
1719 mvpp2_prs_hw_write(priv, &pe);
1720}
1721
1722/* Set port to promiscuous mode */
1723static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1724{
1725 struct mvpp2_prs_entry pe;
1726
Joe Perchesdbedd442015-03-06 20:49:12 -08001727 /* Promiscuous mode - Accept unknown packets */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001728
1729 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1730 /* Entry exist - update port only */
1731 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1732 mvpp2_prs_hw_read(priv, &pe);
1733 } else {
1734 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001735 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001736 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1737 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1738
1739 /* Continue - set next lookup */
1740 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1741
1742 /* Set result info bits */
1743 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1744 MVPP2_PRS_RI_L2_CAST_MASK);
1745
1746 /* Shift to ethertype */
1747 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1748 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1749
1750 /* Mask all ports */
1751 mvpp2_prs_tcam_port_map_set(&pe, 0);
1752
1753 /* Update shadow table */
1754 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1755 }
1756
1757 /* Update port mask */
1758 mvpp2_prs_tcam_port_set(&pe, port, add);
1759
1760 mvpp2_prs_hw_write(priv, &pe);
1761}
1762
1763/* Accept multicast */
1764static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1765 bool add)
1766{
1767 struct mvpp2_prs_entry pe;
1768 unsigned char da_mc;
1769
1770 /* Ethernet multicast address first byte is
1771 * 0x01 for IPv4 and 0x33 for IPv6
1772 */
1773 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1774
1775 if (priv->prs_shadow[index].valid) {
1776 /* Entry exist - update port only */
1777 pe.index = index;
1778 mvpp2_prs_hw_read(priv, &pe);
1779 } else {
1780 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001781 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001782 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1783 pe.index = index;
1784
1785 /* Continue - set next lookup */
1786 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1787
1788 /* Set result info bits */
1789 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1790 MVPP2_PRS_RI_L2_CAST_MASK);
1791
1792 /* Update tcam entry data first byte */
1793 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1794
1795 /* Shift to ethertype */
1796 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1797 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1798
1799 /* Mask all ports */
1800 mvpp2_prs_tcam_port_map_set(&pe, 0);
1801
1802 /* Update shadow table */
1803 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1804 }
1805
1806 /* Update port mask */
1807 mvpp2_prs_tcam_port_set(&pe, port, add);
1808
1809 mvpp2_prs_hw_write(priv, &pe);
1810}
1811
1812/* Set entry for dsa packets */
1813static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
1814 bool tagged, bool extend)
1815{
1816 struct mvpp2_prs_entry pe;
1817 int tid, shift;
1818
1819 if (extend) {
1820 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
1821 shift = 8;
1822 } else {
1823 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
1824 shift = 4;
1825 }
1826
1827 if (priv->prs_shadow[tid].valid) {
1828 /* Entry exist - update port only */
1829 pe.index = tid;
1830 mvpp2_prs_hw_read(priv, &pe);
1831 } else {
1832 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001833 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001834 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1835 pe.index = tid;
1836
1837 /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
1838 mvpp2_prs_sram_shift_set(&pe, shift,
1839 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1840
1841 /* Update shadow table */
1842 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1843
1844 if (tagged) {
1845 /* Set tagged bit in DSA tag */
1846 mvpp2_prs_tcam_data_byte_set(&pe, 0,
1847 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1848 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1849 /* Clear all ai bits for next iteration */
1850 mvpp2_prs_sram_ai_update(&pe, 0,
1851 MVPP2_PRS_SRAM_AI_MASK);
1852 /* If packet is tagged continue check vlans */
1853 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1854 } else {
1855 /* Set result info bits to 'no vlans' */
1856 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1857 MVPP2_PRS_RI_VLAN_MASK);
1858 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1859 }
1860
1861 /* Mask all ports */
1862 mvpp2_prs_tcam_port_map_set(&pe, 0);
1863 }
1864
1865 /* Update port mask */
1866 mvpp2_prs_tcam_port_set(&pe, port, add);
1867
1868 mvpp2_prs_hw_write(priv, &pe);
1869}
1870
1871/* Set entry for dsa ethertype */
1872static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
1873 bool add, bool tagged, bool extend)
1874{
1875 struct mvpp2_prs_entry pe;
1876 int tid, shift, port_mask;
1877
1878 if (extend) {
1879 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
1880 MVPP2_PE_ETYPE_EDSA_UNTAGGED;
1881 port_mask = 0;
1882 shift = 8;
1883 } else {
1884 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
1885 MVPP2_PE_ETYPE_DSA_UNTAGGED;
1886 port_mask = MVPP2_PRS_PORT_MASK;
1887 shift = 4;
1888 }
1889
1890 if (priv->prs_shadow[tid].valid) {
1891 /* Entry exist - update port only */
1892 pe.index = tid;
1893 mvpp2_prs_hw_read(priv, &pe);
1894 } else {
1895 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001896 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001897 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1898 pe.index = tid;
1899
1900 /* Set ethertype */
1901 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
1902 mvpp2_prs_match_etype(&pe, 2, 0);
1903
1904 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
1905 MVPP2_PRS_RI_DSA_MASK);
1906 /* Shift ethertype + 2 byte reserved + tag*/
1907 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
1908 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1909
1910 /* Update shadow table */
1911 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1912
1913 if (tagged) {
1914 /* Set tagged bit in DSA tag */
1915 mvpp2_prs_tcam_data_byte_set(&pe,
1916 MVPP2_ETH_TYPE_LEN + 2 + 3,
1917 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1918 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1919 /* Clear all ai bits for next iteration */
1920 mvpp2_prs_sram_ai_update(&pe, 0,
1921 MVPP2_PRS_SRAM_AI_MASK);
1922 /* If packet is tagged continue check vlans */
1923 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1924 } else {
1925 /* Set result info bits to 'no vlans' */
1926 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1927 MVPP2_PRS_RI_VLAN_MASK);
1928 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1929 }
1930 /* Mask/unmask all ports, depending on dsa type */
1931 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
1932 }
1933
1934 /* Update port mask */
1935 mvpp2_prs_tcam_port_set(&pe, port, add);
1936
1937 mvpp2_prs_hw_write(priv, &pe);
1938}
1939
1940/* Search for existing single/triple vlan entry */
1941static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
1942 unsigned short tpid, int ai)
1943{
1944 struct mvpp2_prs_entry *pe;
1945 int tid;
1946
1947 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1948 if (!pe)
1949 return NULL;
1950 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
1951
1952 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
1953 for (tid = MVPP2_PE_FIRST_FREE_TID;
1954 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
1955 unsigned int ri_bits, ai_bits;
1956 bool match;
1957
1958 if (!priv->prs_shadow[tid].valid ||
1959 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
1960 continue;
1961
1962 pe->index = tid;
1963
1964 mvpp2_prs_hw_read(priv, pe);
1965 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
1966 if (!match)
1967 continue;
1968
1969 /* Get vlan type */
1970 ri_bits = mvpp2_prs_sram_ri_get(pe);
1971 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
1972
1973 /* Get current ai value from tcam */
1974 ai_bits = mvpp2_prs_tcam_ai_get(pe);
1975 /* Clear double vlan bit */
1976 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
1977
1978 if (ai != ai_bits)
1979 continue;
1980
1981 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
1982 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
1983 return pe;
1984 }
1985 kfree(pe);
1986
1987 return NULL;
1988}
1989
1990/* Add/update single/triple vlan entry */
1991static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
1992 unsigned int port_map)
1993{
1994 struct mvpp2_prs_entry *pe;
1995 int tid_aux, tid;
Sudip Mukherjee43737472014-11-01 16:59:34 +05301996 int ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001997
1998 pe = mvpp2_prs_vlan_find(priv, tpid, ai);
1999
2000 if (!pe) {
2001 /* Create new tcam entry */
2002 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
2003 MVPP2_PE_FIRST_FREE_TID);
2004 if (tid < 0)
2005 return tid;
2006
2007 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2008 if (!pe)
2009 return -ENOMEM;
2010
2011 /* Get last double vlan tid */
2012 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
2013 tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
2014 unsigned int ri_bits;
2015
2016 if (!priv->prs_shadow[tid_aux].valid ||
2017 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2018 continue;
2019
2020 pe->index = tid_aux;
2021 mvpp2_prs_hw_read(priv, pe);
2022 ri_bits = mvpp2_prs_sram_ri_get(pe);
2023 if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
2024 MVPP2_PRS_RI_VLAN_DOUBLE)
2025 break;
2026 }
2027
Sudip Mukherjee43737472014-11-01 16:59:34 +05302028 if (tid <= tid_aux) {
2029 ret = -EINVAL;
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002030 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302031 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002032
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002033 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002034 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2035 pe->index = tid;
2036
2037 mvpp2_prs_match_etype(pe, 0, tpid);
2038
2039 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
2040 /* Shift 4 bytes - skip 1 vlan tag */
2041 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
2042 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2043 /* Clear all ai bits for next iteration */
2044 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2045
2046 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
2047 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
2048 MVPP2_PRS_RI_VLAN_MASK);
2049 } else {
2050 ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
2051 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
2052 MVPP2_PRS_RI_VLAN_MASK);
2053 }
2054 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
2055
2056 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2057 }
2058 /* Update ports' mask */
2059 mvpp2_prs_tcam_port_map_set(pe, port_map);
2060
2061 mvpp2_prs_hw_write(priv, pe);
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002062free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002063 kfree(pe);
2064
Sudip Mukherjee43737472014-11-01 16:59:34 +05302065 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002066}
2067
2068/* Get first free double vlan ai number */
2069static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
2070{
2071 int i;
2072
2073 for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
2074 if (!priv->prs_double_vlans[i])
2075 return i;
2076 }
2077
2078 return -EINVAL;
2079}
2080
2081/* Search for existing double vlan entry */
2082static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
2083 unsigned short tpid1,
2084 unsigned short tpid2)
2085{
2086 struct mvpp2_prs_entry *pe;
2087 int tid;
2088
2089 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2090 if (!pe)
2091 return NULL;
2092 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2093
2094 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2095 for (tid = MVPP2_PE_FIRST_FREE_TID;
2096 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2097 unsigned int ri_mask;
2098 bool match;
2099
2100 if (!priv->prs_shadow[tid].valid ||
2101 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2102 continue;
2103
2104 pe->index = tid;
2105 mvpp2_prs_hw_read(priv, pe);
2106
2107 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
2108 && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
2109
2110 if (!match)
2111 continue;
2112
2113 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
2114 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
2115 return pe;
2116 }
2117 kfree(pe);
2118
2119 return NULL;
2120}
2121
2122/* Add or update double vlan entry */
2123static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
2124 unsigned short tpid2,
2125 unsigned int port_map)
2126{
2127 struct mvpp2_prs_entry *pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302128 int tid_aux, tid, ai, ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002129
2130 pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2131
2132 if (!pe) {
2133 /* Create new tcam entry */
2134 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2135 MVPP2_PE_LAST_FREE_TID);
2136 if (tid < 0)
2137 return tid;
2138
2139 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2140 if (!pe)
2141 return -ENOMEM;
2142
2143 /* Set ai value for new double vlan entry */
2144 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302145 if (ai < 0) {
2146 ret = ai;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002147 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302148 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002149
2150 /* Get first single/triple vlan tid */
2151 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2152 tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2153 unsigned int ri_bits;
2154
2155 if (!priv->prs_shadow[tid_aux].valid ||
2156 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2157 continue;
2158
2159 pe->index = tid_aux;
2160 mvpp2_prs_hw_read(priv, pe);
2161 ri_bits = mvpp2_prs_sram_ri_get(pe);
2162 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2163 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2164 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2165 break;
2166 }
2167
Sudip Mukherjee43737472014-11-01 16:59:34 +05302168 if (tid >= tid_aux) {
2169 ret = -ERANGE;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002170 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302171 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002172
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002173 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002174 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2175 pe->index = tid;
2176
2177 priv->prs_double_vlans[ai] = true;
2178
2179 mvpp2_prs_match_etype(pe, 0, tpid1);
2180 mvpp2_prs_match_etype(pe, 4, tpid2);
2181
2182 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
2183 /* Shift 8 bytes - skip 2 vlan tags */
2184 mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
2185 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2186 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2187 MVPP2_PRS_RI_VLAN_MASK);
2188 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2189 MVPP2_PRS_SRAM_AI_MASK);
2190
2191 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2192 }
2193
2194 /* Update ports' mask */
2195 mvpp2_prs_tcam_port_map_set(pe, port_map);
2196 mvpp2_prs_hw_write(priv, pe);
Markus Elfringc9a7e122017-04-17 13:03:49 +02002197free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002198 kfree(pe);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302199 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002200}
2201
2202/* IPv4 header parsing for fragmentation and L4 offset */
2203static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2204 unsigned int ri, unsigned int ri_mask)
2205{
2206 struct mvpp2_prs_entry pe;
2207 int tid;
2208
2209 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2210 (proto != IPPROTO_IGMP))
2211 return -EINVAL;
2212
2213 /* Fragmented packet */
2214 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2215 MVPP2_PE_LAST_FREE_TID);
2216 if (tid < 0)
2217 return tid;
2218
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002219 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002220 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2221 pe.index = tid;
2222
2223 /* Set next lu to IPv4 */
2224 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2225 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2226 /* Set L4 offset */
2227 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2228 sizeof(struct iphdr) - 4,
2229 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2230 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2231 MVPP2_PRS_IPV4_DIP_AI_BIT);
2232 mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_MASK,
2233 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2234
2235 mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2236 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2237 /* Unmask all ports */
2238 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2239
2240 /* Update shadow table and hw entry */
2241 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2242 mvpp2_prs_hw_write(priv, &pe);
2243
2244 /* Not fragmented packet */
2245 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2246 MVPP2_PE_LAST_FREE_TID);
2247 if (tid < 0)
2248 return tid;
2249
2250 pe.index = tid;
2251 /* Clear ri before updating */
2252 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2253 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2254 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2255
2256 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, MVPP2_PRS_TCAM_PROTO_MASK_L);
2257 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, MVPP2_PRS_TCAM_PROTO_MASK);
2258
2259 /* Update shadow table and hw entry */
2260 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2261 mvpp2_prs_hw_write(priv, &pe);
2262
2263 return 0;
2264}
2265
2266/* IPv4 L3 multicast or broadcast */
2267static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2268{
2269 struct mvpp2_prs_entry pe;
2270 int mask, tid;
2271
2272 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2273 MVPP2_PE_LAST_FREE_TID);
2274 if (tid < 0)
2275 return tid;
2276
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002277 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002278 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2279 pe.index = tid;
2280
2281 switch (l3_cast) {
2282 case MVPP2_PRS_L3_MULTI_CAST:
2283 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2284 MVPP2_PRS_IPV4_MC_MASK);
2285 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2286 MVPP2_PRS_RI_L3_ADDR_MASK);
2287 break;
2288 case MVPP2_PRS_L3_BROAD_CAST:
2289 mask = MVPP2_PRS_IPV4_BC_MASK;
2290 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2291 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2292 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2293 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2294 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2295 MVPP2_PRS_RI_L3_ADDR_MASK);
2296 break;
2297 default:
2298 return -EINVAL;
2299 }
2300
2301 /* Finished: go to flowid generation */
2302 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2303 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2304
2305 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2306 MVPP2_PRS_IPV4_DIP_AI_BIT);
2307 /* Unmask all ports */
2308 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2309
2310 /* Update shadow table and hw entry */
2311 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2312 mvpp2_prs_hw_write(priv, &pe);
2313
2314 return 0;
2315}
2316
2317/* Set entries for protocols over IPv6 */
2318static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2319 unsigned int ri, unsigned int ri_mask)
2320{
2321 struct mvpp2_prs_entry pe;
2322 int tid;
2323
2324 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2325 (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2326 return -EINVAL;
2327
2328 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2329 MVPP2_PE_LAST_FREE_TID);
2330 if (tid < 0)
2331 return tid;
2332
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002333 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002334 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2335 pe.index = tid;
2336
2337 /* Finished: go to flowid generation */
2338 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2339 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2340 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2341 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2342 sizeof(struct ipv6hdr) - 6,
2343 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2344
2345 mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2346 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2347 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2348 /* Unmask all ports */
2349 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2350
2351 /* Write HW */
2352 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2353 mvpp2_prs_hw_write(priv, &pe);
2354
2355 return 0;
2356}
2357
2358/* IPv6 L3 multicast entry */
2359static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2360{
2361 struct mvpp2_prs_entry pe;
2362 int tid;
2363
2364 if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2365 return -EINVAL;
2366
2367 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2368 MVPP2_PE_LAST_FREE_TID);
2369 if (tid < 0)
2370 return tid;
2371
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002372 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002373 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2374 pe.index = tid;
2375
2376 /* Finished: go to flowid generation */
2377 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2378 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2379 MVPP2_PRS_RI_L3_ADDR_MASK);
2380 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2381 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2382 /* Shift back to IPv6 NH */
2383 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2384
2385 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2386 MVPP2_PRS_IPV6_MC_MASK);
2387 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2388 /* Unmask all ports */
2389 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2390
2391 /* Update shadow table and hw entry */
2392 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2393 mvpp2_prs_hw_write(priv, &pe);
2394
2395 return 0;
2396}
2397
2398/* Parser per-port initialization */
2399static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2400 int lu_max, int offset)
2401{
2402 u32 val;
2403
2404 /* Set lookup ID */
2405 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2406 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2407 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2408 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2409
2410 /* Set maximum number of loops for packet received from port */
2411 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2412 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2413 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2414 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2415
2416 /* Set initial offset for packet header extraction for the first
2417 * searching loop
2418 */
2419 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2420 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2421 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2422 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2423}
2424
2425/* Default flow entries initialization for all ports */
2426static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2427{
2428 struct mvpp2_prs_entry pe;
2429 int port;
2430
2431 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002432 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002433 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2434 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2435
2436 /* Mask all ports */
2437 mvpp2_prs_tcam_port_map_set(&pe, 0);
2438
2439 /* Set flow ID*/
2440 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2441 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2442
2443 /* Update shadow table and hw entry */
2444 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2445 mvpp2_prs_hw_write(priv, &pe);
2446 }
2447}
2448
2449/* Set default entry for Marvell Header field */
2450static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2451{
2452 struct mvpp2_prs_entry pe;
2453
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002454 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002455
2456 pe.index = MVPP2_PE_MH_DEFAULT;
2457 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2458 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2459 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2460 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2461
2462 /* Unmask all ports */
2463 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2464
2465 /* Update shadow table and hw entry */
2466 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2467 mvpp2_prs_hw_write(priv, &pe);
2468}
2469
2470/* Set default entires (place holder) for promiscuous, non-promiscuous and
2471 * multicast MAC addresses
2472 */
2473static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2474{
2475 struct mvpp2_prs_entry pe;
2476
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002477 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002478
2479 /* Non-promiscuous mode for all ports - DROP unknown packets */
2480 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2481 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2482
2483 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2484 MVPP2_PRS_RI_DROP_MASK);
2485 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2486 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2487
2488 /* Unmask all ports */
2489 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2490
2491 /* Update shadow table and hw entry */
2492 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2493 mvpp2_prs_hw_write(priv, &pe);
2494
2495 /* place holders only - no ports */
2496 mvpp2_prs_mac_drop_all_set(priv, 0, false);
2497 mvpp2_prs_mac_promisc_set(priv, 0, false);
2498 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
2499 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
2500}
2501
2502/* Set default entries for various types of dsa packets */
2503static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2504{
2505 struct mvpp2_prs_entry pe;
2506
2507 /* None tagged EDSA entry - place holder */
2508 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2509 MVPP2_PRS_EDSA);
2510
2511 /* Tagged EDSA entry - place holder */
2512 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2513
2514 /* None tagged DSA entry - place holder */
2515 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2516 MVPP2_PRS_DSA);
2517
2518 /* Tagged DSA entry - place holder */
2519 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2520
2521 /* None tagged EDSA ethertype entry - place holder*/
2522 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2523 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2524
2525 /* Tagged EDSA ethertype entry - place holder*/
2526 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2527 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2528
2529 /* None tagged DSA ethertype entry */
2530 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2531 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2532
2533 /* Tagged DSA ethertype entry */
2534 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2535 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2536
2537 /* Set default entry, in case DSA or EDSA tag not found */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002538 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002539 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2540 pe.index = MVPP2_PE_DSA_DEFAULT;
2541 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2542
2543 /* Shift 0 bytes */
2544 mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2545 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2546
2547 /* Clear all sram ai bits for next iteration */
2548 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2549
2550 /* Unmask all ports */
2551 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2552
2553 mvpp2_prs_hw_write(priv, &pe);
2554}
2555
2556/* Match basic ethertypes */
2557static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2558{
2559 struct mvpp2_prs_entry pe;
2560 int tid;
2561
2562 /* Ethertype: PPPoE */
2563 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2564 MVPP2_PE_LAST_FREE_TID);
2565 if (tid < 0)
2566 return tid;
2567
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002568 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002569 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2570 pe.index = tid;
2571
2572 mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2573
2574 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2575 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2576 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2577 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2578 MVPP2_PRS_RI_PPPOE_MASK);
2579
2580 /* Update shadow table and hw entry */
2581 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2582 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2583 priv->prs_shadow[pe.index].finish = false;
2584 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2585 MVPP2_PRS_RI_PPPOE_MASK);
2586 mvpp2_prs_hw_write(priv, &pe);
2587
2588 /* Ethertype: ARP */
2589 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2590 MVPP2_PE_LAST_FREE_TID);
2591 if (tid < 0)
2592 return tid;
2593
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002594 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002595 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2596 pe.index = tid;
2597
2598 mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2599
2600 /* Generate flow in the next iteration*/
2601 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2602 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2603 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2604 MVPP2_PRS_RI_L3_PROTO_MASK);
2605 /* Set L3 offset */
2606 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2607 MVPP2_ETH_TYPE_LEN,
2608 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2609
2610 /* Update shadow table and hw entry */
2611 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2612 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2613 priv->prs_shadow[pe.index].finish = true;
2614 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2615 MVPP2_PRS_RI_L3_PROTO_MASK);
2616 mvpp2_prs_hw_write(priv, &pe);
2617
2618 /* Ethertype: LBTD */
2619 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2620 MVPP2_PE_LAST_FREE_TID);
2621 if (tid < 0)
2622 return tid;
2623
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002624 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002625 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2626 pe.index = tid;
2627
2628 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2629
2630 /* Generate flow in the next iteration*/
2631 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2632 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2633 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2634 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2635 MVPP2_PRS_RI_CPU_CODE_MASK |
2636 MVPP2_PRS_RI_UDF3_MASK);
2637 /* Set L3 offset */
2638 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2639 MVPP2_ETH_TYPE_LEN,
2640 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2641
2642 /* Update shadow table and hw entry */
2643 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2644 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2645 priv->prs_shadow[pe.index].finish = true;
2646 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2647 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2648 MVPP2_PRS_RI_CPU_CODE_MASK |
2649 MVPP2_PRS_RI_UDF3_MASK);
2650 mvpp2_prs_hw_write(priv, &pe);
2651
2652 /* Ethertype: IPv4 without options */
2653 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2654 MVPP2_PE_LAST_FREE_TID);
2655 if (tid < 0)
2656 return tid;
2657
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002658 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002659 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2660 pe.index = tid;
2661
2662 mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2663 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2664 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2665 MVPP2_PRS_IPV4_HEAD_MASK |
2666 MVPP2_PRS_IPV4_IHL_MASK);
2667
2668 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2669 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2670 MVPP2_PRS_RI_L3_PROTO_MASK);
2671 /* Skip eth_type + 4 bytes of IP header */
2672 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2673 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2674 /* Set L3 offset */
2675 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2676 MVPP2_ETH_TYPE_LEN,
2677 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2678
2679 /* Update shadow table and hw entry */
2680 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2681 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2682 priv->prs_shadow[pe.index].finish = false;
2683 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2684 MVPP2_PRS_RI_L3_PROTO_MASK);
2685 mvpp2_prs_hw_write(priv, &pe);
2686
2687 /* Ethertype: IPv4 with options */
2688 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2689 MVPP2_PE_LAST_FREE_TID);
2690 if (tid < 0)
2691 return tid;
2692
2693 pe.index = tid;
2694
2695 /* Clear tcam data before updating */
2696 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
2697 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
2698
2699 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2700 MVPP2_PRS_IPV4_HEAD,
2701 MVPP2_PRS_IPV4_HEAD_MASK);
2702
2703 /* Clear ri before updating */
2704 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2705 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2706 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2707 MVPP2_PRS_RI_L3_PROTO_MASK);
2708
2709 /* Update shadow table and hw entry */
2710 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2711 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2712 priv->prs_shadow[pe.index].finish = false;
2713 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
2714 MVPP2_PRS_RI_L3_PROTO_MASK);
2715 mvpp2_prs_hw_write(priv, &pe);
2716
2717 /* Ethertype: IPv6 without options */
2718 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2719 MVPP2_PE_LAST_FREE_TID);
2720 if (tid < 0)
2721 return tid;
2722
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002723 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002724 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2725 pe.index = tid;
2726
2727 mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
2728
2729 /* Skip DIP of IPV6 header */
2730 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
2731 MVPP2_MAX_L3_ADDR_SIZE,
2732 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2733 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2734 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2735 MVPP2_PRS_RI_L3_PROTO_MASK);
2736 /* Set L3 offset */
2737 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2738 MVPP2_ETH_TYPE_LEN,
2739 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2740
2741 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2742 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2743 priv->prs_shadow[pe.index].finish = false;
2744 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
2745 MVPP2_PRS_RI_L3_PROTO_MASK);
2746 mvpp2_prs_hw_write(priv, &pe);
2747
2748 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
2749 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2750 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2751 pe.index = MVPP2_PE_ETH_TYPE_UN;
2752
2753 /* Unmask all ports */
2754 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2755
2756 /* Generate flow in the next iteration*/
2757 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2758 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2759 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2760 MVPP2_PRS_RI_L3_PROTO_MASK);
2761 /* Set L3 offset even it's unknown L3 */
2762 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2763 MVPP2_ETH_TYPE_LEN,
2764 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2765
2766 /* Update shadow table and hw entry */
2767 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2768 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2769 priv->prs_shadow[pe.index].finish = true;
2770 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
2771 MVPP2_PRS_RI_L3_PROTO_MASK);
2772 mvpp2_prs_hw_write(priv, &pe);
2773
2774 return 0;
2775}
2776
2777/* Configure vlan entries and detect up to 2 successive VLAN tags.
2778 * Possible options:
2779 * 0x8100, 0x88A8
2780 * 0x8100, 0x8100
2781 * 0x8100
2782 * 0x88A8
2783 */
2784static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
2785{
2786 struct mvpp2_prs_entry pe;
2787 int err;
2788
2789 priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
2790 MVPP2_PRS_DBL_VLANS_MAX,
2791 GFP_KERNEL);
2792 if (!priv->prs_double_vlans)
2793 return -ENOMEM;
2794
2795 /* Double VLAN: 0x8100, 0x88A8 */
2796 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
2797 MVPP2_PRS_PORT_MASK);
2798 if (err)
2799 return err;
2800
2801 /* Double VLAN: 0x8100, 0x8100 */
2802 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
2803 MVPP2_PRS_PORT_MASK);
2804 if (err)
2805 return err;
2806
2807 /* Single VLAN: 0x88a8 */
2808 err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
2809 MVPP2_PRS_PORT_MASK);
2810 if (err)
2811 return err;
2812
2813 /* Single VLAN: 0x8100 */
2814 err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
2815 MVPP2_PRS_PORT_MASK);
2816 if (err)
2817 return err;
2818
2819 /* Set default double vlan entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002820 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002821 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2822 pe.index = MVPP2_PE_VLAN_DBL;
2823
2824 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2825 /* Clear ai for next iterations */
2826 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2827 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2828 MVPP2_PRS_RI_VLAN_MASK);
2829
2830 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
2831 MVPP2_PRS_DBL_VLAN_AI_BIT);
2832 /* Unmask all ports */
2833 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2834
2835 /* Update shadow table and hw entry */
2836 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2837 mvpp2_prs_hw_write(priv, &pe);
2838
2839 /* Set default vlan none entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002840 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002841 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2842 pe.index = MVPP2_PE_VLAN_NONE;
2843
2844 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2845 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2846 MVPP2_PRS_RI_VLAN_MASK);
2847
2848 /* Unmask all ports */
2849 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2850
2851 /* Update shadow table and hw entry */
2852 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2853 mvpp2_prs_hw_write(priv, &pe);
2854
2855 return 0;
2856}
2857
2858/* Set entries for PPPoE ethertype */
2859static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
2860{
2861 struct mvpp2_prs_entry pe;
2862 int tid;
2863
2864 /* IPv4 over PPPoE with options */
2865 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2866 MVPP2_PE_LAST_FREE_TID);
2867 if (tid < 0)
2868 return tid;
2869
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002870 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002871 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2872 pe.index = tid;
2873
2874 mvpp2_prs_match_etype(&pe, 0, PPP_IP);
2875
2876 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2877 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2878 MVPP2_PRS_RI_L3_PROTO_MASK);
2879 /* Skip eth_type + 4 bytes of IP header */
2880 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2881 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2882 /* Set L3 offset */
2883 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2884 MVPP2_ETH_TYPE_LEN,
2885 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2886
2887 /* Update shadow table and hw entry */
2888 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2889 mvpp2_prs_hw_write(priv, &pe);
2890
2891 /* IPv4 over PPPoE without options */
2892 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2893 MVPP2_PE_LAST_FREE_TID);
2894 if (tid < 0)
2895 return tid;
2896
2897 pe.index = tid;
2898
2899 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2900 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2901 MVPP2_PRS_IPV4_HEAD_MASK |
2902 MVPP2_PRS_IPV4_IHL_MASK);
2903
2904 /* Clear ri before updating */
2905 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2906 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2907 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2908 MVPP2_PRS_RI_L3_PROTO_MASK);
2909
2910 /* Update shadow table and hw entry */
2911 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2912 mvpp2_prs_hw_write(priv, &pe);
2913
2914 /* IPv6 over PPPoE */
2915 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2916 MVPP2_PE_LAST_FREE_TID);
2917 if (tid < 0)
2918 return tid;
2919
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002920 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002921 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2922 pe.index = tid;
2923
2924 mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
2925
2926 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2927 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2928 MVPP2_PRS_RI_L3_PROTO_MASK);
2929 /* Skip eth_type + 4 bytes of IPv6 header */
2930 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2931 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2932 /* Set L3 offset */
2933 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2934 MVPP2_ETH_TYPE_LEN,
2935 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2936
2937 /* Update shadow table and hw entry */
2938 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2939 mvpp2_prs_hw_write(priv, &pe);
2940
2941 /* Non-IP over PPPoE */
2942 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2943 MVPP2_PE_LAST_FREE_TID);
2944 if (tid < 0)
2945 return tid;
2946
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002947 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002948 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2949 pe.index = tid;
2950
2951 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2952 MVPP2_PRS_RI_L3_PROTO_MASK);
2953
2954 /* Finished: go to flowid generation */
2955 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2956 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2957 /* Set L3 offset even if it's unknown L3 */
2958 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2959 MVPP2_ETH_TYPE_LEN,
2960 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2961
2962 /* Update shadow table and hw entry */
2963 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2964 mvpp2_prs_hw_write(priv, &pe);
2965
2966 return 0;
2967}
2968
2969/* Initialize entries for IPv4 */
2970static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
2971{
2972 struct mvpp2_prs_entry pe;
2973 int err;
2974
2975 /* Set entries for TCP, UDP and IGMP over IPv4 */
2976 err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
2977 MVPP2_PRS_RI_L4_PROTO_MASK);
2978 if (err)
2979 return err;
2980
2981 err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
2982 MVPP2_PRS_RI_L4_PROTO_MASK);
2983 if (err)
2984 return err;
2985
2986 err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
2987 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2988 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2989 MVPP2_PRS_RI_CPU_CODE_MASK |
2990 MVPP2_PRS_RI_UDF3_MASK);
2991 if (err)
2992 return err;
2993
2994 /* IPv4 Broadcast */
2995 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
2996 if (err)
2997 return err;
2998
2999 /* IPv4 Multicast */
3000 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3001 if (err)
3002 return err;
3003
3004 /* Default IPv4 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003005 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003006 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3007 pe.index = MVPP2_PE_IP4_PROTO_UN;
3008
3009 /* Set next lu to IPv4 */
3010 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3011 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3012 /* Set L4 offset */
3013 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3014 sizeof(struct iphdr) - 4,
3015 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3016 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3017 MVPP2_PRS_IPV4_DIP_AI_BIT);
3018 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3019 MVPP2_PRS_RI_L4_PROTO_MASK);
3020
3021 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
3022 /* Unmask all ports */
3023 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3024
3025 /* Update shadow table and hw entry */
3026 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3027 mvpp2_prs_hw_write(priv, &pe);
3028
3029 /* Default IPv4 entry for unicast address */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003030 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003031 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3032 pe.index = MVPP2_PE_IP4_ADDR_UN;
3033
3034 /* Finished: go to flowid generation */
3035 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3036 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3037 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3038 MVPP2_PRS_RI_L3_ADDR_MASK);
3039
3040 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3041 MVPP2_PRS_IPV4_DIP_AI_BIT);
3042 /* Unmask all ports */
3043 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3044
3045 /* Update shadow table and hw entry */
3046 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3047 mvpp2_prs_hw_write(priv, &pe);
3048
3049 return 0;
3050}
3051
3052/* Initialize entries for IPv6 */
3053static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
3054{
3055 struct mvpp2_prs_entry pe;
3056 int tid, err;
3057
3058 /* Set entries for TCP, UDP and ICMP over IPv6 */
3059 err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
3060 MVPP2_PRS_RI_L4_TCP,
3061 MVPP2_PRS_RI_L4_PROTO_MASK);
3062 if (err)
3063 return err;
3064
3065 err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
3066 MVPP2_PRS_RI_L4_UDP,
3067 MVPP2_PRS_RI_L4_PROTO_MASK);
3068 if (err)
3069 return err;
3070
3071 err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
3072 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3073 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3074 MVPP2_PRS_RI_CPU_CODE_MASK |
3075 MVPP2_PRS_RI_UDF3_MASK);
3076 if (err)
3077 return err;
3078
3079 /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
3080 /* Result Info: UDF7=1, DS lite */
3081 err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
3082 MVPP2_PRS_RI_UDF7_IP6_LITE,
3083 MVPP2_PRS_RI_UDF7_MASK);
3084 if (err)
3085 return err;
3086
3087 /* IPv6 multicast */
3088 err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3089 if (err)
3090 return err;
3091
3092 /* Entry for checking hop limit */
3093 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3094 MVPP2_PE_LAST_FREE_TID);
3095 if (tid < 0)
3096 return tid;
3097
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003098 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003099 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3100 pe.index = tid;
3101
3102 /* Finished: go to flowid generation */
3103 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3104 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3105 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
3106 MVPP2_PRS_RI_DROP_MASK,
3107 MVPP2_PRS_RI_L3_PROTO_MASK |
3108 MVPP2_PRS_RI_DROP_MASK);
3109
3110 mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
3111 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3112 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3113
3114 /* Update shadow table and hw entry */
3115 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3116 mvpp2_prs_hw_write(priv, &pe);
3117
3118 /* Default IPv6 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003119 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003120 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3121 pe.index = MVPP2_PE_IP6_PROTO_UN;
3122
3123 /* Finished: go to flowid generation */
3124 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3125 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3126 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3127 MVPP2_PRS_RI_L4_PROTO_MASK);
3128 /* Set L4 offset relatively to our current place */
3129 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3130 sizeof(struct ipv6hdr) - 4,
3131 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3132
3133 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3134 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3135 /* Unmask all ports */
3136 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3137
3138 /* Update shadow table and hw entry */
3139 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3140 mvpp2_prs_hw_write(priv, &pe);
3141
3142 /* Default IPv6 entry for unknown ext protocols */
3143 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3144 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3145 pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3146
3147 /* Finished: go to flowid generation */
3148 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3149 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3150 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3151 MVPP2_PRS_RI_L4_PROTO_MASK);
3152
3153 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3154 MVPP2_PRS_IPV6_EXT_AI_BIT);
3155 /* Unmask all ports */
3156 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3157
3158 /* Update shadow table and hw entry */
3159 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3160 mvpp2_prs_hw_write(priv, &pe);
3161
3162 /* Default IPv6 entry for unicast address */
3163 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3164 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3165 pe.index = MVPP2_PE_IP6_ADDR_UN;
3166
3167 /* Finished: go to IPv6 again */
3168 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3169 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3170 MVPP2_PRS_RI_L3_ADDR_MASK);
3171 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3172 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3173 /* Shift back to IPV6 NH */
3174 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3175
3176 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3177 /* Unmask all ports */
3178 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3179
3180 /* Update shadow table and hw entry */
3181 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3182 mvpp2_prs_hw_write(priv, &pe);
3183
3184 return 0;
3185}
3186
3187/* Parser default initialization */
3188static int mvpp2_prs_default_init(struct platform_device *pdev,
3189 struct mvpp2 *priv)
3190{
3191 int err, index, i;
3192
3193 /* Enable tcam table */
3194 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3195
3196 /* Clear all tcam and sram entries */
3197 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3198 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3199 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3200 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3201
3202 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3203 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3204 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3205 }
3206
3207 /* Invalidate all tcam entries */
3208 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3209 mvpp2_prs_hw_inv(priv, index);
3210
3211 priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
Markus Elfring37df25e2017-04-17 09:12:34 +02003212 sizeof(*priv->prs_shadow),
Marcin Wojtas3f518502014-07-10 16:52:13 -03003213 GFP_KERNEL);
3214 if (!priv->prs_shadow)
3215 return -ENOMEM;
3216
3217 /* Always start from lookup = 0 */
3218 for (index = 0; index < MVPP2_MAX_PORTS; index++)
3219 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3220 MVPP2_PRS_PORT_LU_MAX, 0);
3221
3222 mvpp2_prs_def_flow_init(priv);
3223
3224 mvpp2_prs_mh_init(priv);
3225
3226 mvpp2_prs_mac_init(priv);
3227
3228 mvpp2_prs_dsa_init(priv);
3229
3230 err = mvpp2_prs_etype_init(priv);
3231 if (err)
3232 return err;
3233
3234 err = mvpp2_prs_vlan_init(pdev, priv);
3235 if (err)
3236 return err;
3237
3238 err = mvpp2_prs_pppoe_init(priv);
3239 if (err)
3240 return err;
3241
3242 err = mvpp2_prs_ip6_init(priv);
3243 if (err)
3244 return err;
3245
3246 err = mvpp2_prs_ip4_init(priv);
3247 if (err)
3248 return err;
3249
3250 return 0;
3251}
3252
3253/* Compare MAC DA with tcam entry data */
3254static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3255 const u8 *da, unsigned char *mask)
3256{
3257 unsigned char tcam_byte, tcam_mask;
3258 int index;
3259
3260 for (index = 0; index < ETH_ALEN; index++) {
3261 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3262 if (tcam_mask != mask[index])
3263 return false;
3264
3265 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3266 return false;
3267 }
3268
3269 return true;
3270}
3271
3272/* Find tcam entry with matched pair <MAC DA, port> */
3273static struct mvpp2_prs_entry *
3274mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3275 unsigned char *mask, int udf_type)
3276{
3277 struct mvpp2_prs_entry *pe;
3278 int tid;
3279
3280 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3281 if (!pe)
3282 return NULL;
3283 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3284
3285 /* Go through the all entires with MVPP2_PRS_LU_MAC */
3286 for (tid = MVPP2_PE_FIRST_FREE_TID;
3287 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3288 unsigned int entry_pmap;
3289
3290 if (!priv->prs_shadow[tid].valid ||
3291 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3292 (priv->prs_shadow[tid].udf != udf_type))
3293 continue;
3294
3295 pe->index = tid;
3296 mvpp2_prs_hw_read(priv, pe);
3297 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3298
3299 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3300 entry_pmap == pmap)
3301 return pe;
3302 }
3303 kfree(pe);
3304
3305 return NULL;
3306}
3307
3308/* Update parser's mac da entry */
3309static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3310 const u8 *da, bool add)
3311{
3312 struct mvpp2_prs_entry *pe;
3313 unsigned int pmap, len, ri;
3314 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3315 int tid;
3316
3317 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3318 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3319 MVPP2_PRS_UDF_MAC_DEF);
3320
3321 /* No such entry */
3322 if (!pe) {
3323 if (!add)
3324 return 0;
3325
3326 /* Create new TCAM entry */
3327 /* Find first range mac entry*/
3328 for (tid = MVPP2_PE_FIRST_FREE_TID;
3329 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3330 if (priv->prs_shadow[tid].valid &&
3331 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3332 (priv->prs_shadow[tid].udf ==
3333 MVPP2_PRS_UDF_MAC_RANGE))
3334 break;
3335
3336 /* Go through the all entries from first to last */
3337 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3338 tid - 1);
3339 if (tid < 0)
3340 return tid;
3341
3342 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3343 if (!pe)
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303344 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003345 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3346 pe->index = tid;
3347
3348 /* Mask all ports */
3349 mvpp2_prs_tcam_port_map_set(pe, 0);
3350 }
3351
3352 /* Update port mask */
3353 mvpp2_prs_tcam_port_set(pe, port, add);
3354
3355 /* Invalidate the entry if no ports are left enabled */
3356 pmap = mvpp2_prs_tcam_port_map_get(pe);
3357 if (pmap == 0) {
3358 if (add) {
3359 kfree(pe);
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303360 return -EINVAL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003361 }
3362 mvpp2_prs_hw_inv(priv, pe->index);
3363 priv->prs_shadow[pe->index].valid = false;
3364 kfree(pe);
3365 return 0;
3366 }
3367
3368 /* Continue - set next lookup */
3369 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3370
3371 /* Set match on DA */
3372 len = ETH_ALEN;
3373 while (len--)
3374 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3375
3376 /* Set result info bits */
3377 if (is_broadcast_ether_addr(da))
3378 ri = MVPP2_PRS_RI_L2_BCAST;
3379 else if (is_multicast_ether_addr(da))
3380 ri = MVPP2_PRS_RI_L2_MCAST;
3381 else
3382 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3383
3384 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3385 MVPP2_PRS_RI_MAC_ME_MASK);
3386 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3387 MVPP2_PRS_RI_MAC_ME_MASK);
3388
3389 /* Shift to ethertype */
3390 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3391 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3392
3393 /* Update shadow table and hw entry */
3394 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3395 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3396 mvpp2_prs_hw_write(priv, pe);
3397
3398 kfree(pe);
3399
3400 return 0;
3401}
3402
3403static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3404{
3405 struct mvpp2_port *port = netdev_priv(dev);
3406 int err;
3407
3408 /* Remove old parser entry */
3409 err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3410 false);
3411 if (err)
3412 return err;
3413
3414 /* Add new parser entry */
3415 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3416 if (err)
3417 return err;
3418
3419 /* Set addr in the device */
3420 ether_addr_copy(dev->dev_addr, da);
3421
3422 return 0;
3423}
3424
3425/* Delete all port's multicast simple (not range) entries */
3426static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3427{
3428 struct mvpp2_prs_entry pe;
3429 int index, tid;
3430
3431 for (tid = MVPP2_PE_FIRST_FREE_TID;
3432 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3433 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3434
3435 if (!priv->prs_shadow[tid].valid ||
3436 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3437 (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3438 continue;
3439
3440 /* Only simple mac entries */
3441 pe.index = tid;
3442 mvpp2_prs_hw_read(priv, &pe);
3443
3444 /* Read mac addr from entry */
3445 for (index = 0; index < ETH_ALEN; index++)
3446 mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3447 &da_mask[index]);
3448
3449 if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3450 /* Delete this entry */
3451 mvpp2_prs_mac_da_accept(priv, port, da, false);
3452 }
3453}
3454
3455static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3456{
3457 switch (type) {
3458 case MVPP2_TAG_TYPE_EDSA:
3459 /* Add port to EDSA entries */
3460 mvpp2_prs_dsa_tag_set(priv, port, true,
3461 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3462 mvpp2_prs_dsa_tag_set(priv, port, true,
3463 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3464 /* Remove port from DSA entries */
3465 mvpp2_prs_dsa_tag_set(priv, port, false,
3466 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3467 mvpp2_prs_dsa_tag_set(priv, port, false,
3468 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3469 break;
3470
3471 case MVPP2_TAG_TYPE_DSA:
3472 /* Add port to DSA entries */
3473 mvpp2_prs_dsa_tag_set(priv, port, true,
3474 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3475 mvpp2_prs_dsa_tag_set(priv, port, true,
3476 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3477 /* Remove port from EDSA entries */
3478 mvpp2_prs_dsa_tag_set(priv, port, false,
3479 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3480 mvpp2_prs_dsa_tag_set(priv, port, false,
3481 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3482 break;
3483
3484 case MVPP2_TAG_TYPE_MH:
3485 case MVPP2_TAG_TYPE_NONE:
3486 /* Remove port form EDSA and DSA entries */
3487 mvpp2_prs_dsa_tag_set(priv, port, false,
3488 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3489 mvpp2_prs_dsa_tag_set(priv, port, false,
3490 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3491 mvpp2_prs_dsa_tag_set(priv, port, false,
3492 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3493 mvpp2_prs_dsa_tag_set(priv, port, false,
3494 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3495 break;
3496
3497 default:
3498 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
3499 return -EINVAL;
3500 }
3501
3502 return 0;
3503}
3504
3505/* Set prs flow for the port */
3506static int mvpp2_prs_def_flow(struct mvpp2_port *port)
3507{
3508 struct mvpp2_prs_entry *pe;
3509 int tid;
3510
3511 pe = mvpp2_prs_flow_find(port->priv, port->id);
3512
3513 /* Such entry not exist */
3514 if (!pe) {
3515 /* Go through the all entires from last to first */
3516 tid = mvpp2_prs_tcam_first_free(port->priv,
3517 MVPP2_PE_LAST_FREE_TID,
3518 MVPP2_PE_FIRST_FREE_TID);
3519 if (tid < 0)
3520 return tid;
3521
3522 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3523 if (!pe)
3524 return -ENOMEM;
3525
3526 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
3527 pe->index = tid;
3528
3529 /* Set flow ID*/
3530 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
3531 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
3532
3533 /* Update shadow table */
3534 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
3535 }
3536
3537 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
3538 mvpp2_prs_hw_write(port->priv, pe);
3539 kfree(pe);
3540
3541 return 0;
3542}
3543
3544/* Classifier configuration routines */
3545
3546/* Update classification flow table registers */
3547static void mvpp2_cls_flow_write(struct mvpp2 *priv,
3548 struct mvpp2_cls_flow_entry *fe)
3549{
3550 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
3551 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
3552 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
3553 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
3554}
3555
3556/* Update classification lookup table register */
3557static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
3558 struct mvpp2_cls_lookup_entry *le)
3559{
3560 u32 val;
3561
3562 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
3563 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
3564 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
3565}
3566
3567/* Classifier default initialization */
3568static void mvpp2_cls_init(struct mvpp2 *priv)
3569{
3570 struct mvpp2_cls_lookup_entry le;
3571 struct mvpp2_cls_flow_entry fe;
3572 int index;
3573
3574 /* Enable classifier */
3575 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
3576
3577 /* Clear classifier flow table */
Arnd Bergmanne8f967c2016-11-24 17:28:12 +01003578 memset(&fe.data, 0, sizeof(fe.data));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003579 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
3580 fe.index = index;
3581 mvpp2_cls_flow_write(priv, &fe);
3582 }
3583
3584 /* Clear classifier lookup table */
3585 le.data = 0;
3586 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
3587 le.lkpid = index;
3588 le.way = 0;
3589 mvpp2_cls_lookup_write(priv, &le);
3590
3591 le.way = 1;
3592 mvpp2_cls_lookup_write(priv, &le);
3593 }
3594}
3595
3596static void mvpp2_cls_port_config(struct mvpp2_port *port)
3597{
3598 struct mvpp2_cls_lookup_entry le;
3599 u32 val;
3600
3601 /* Set way for the port */
3602 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
3603 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
3604 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
3605
3606 /* Pick the entry to be accessed in lookup ID decoding table
3607 * according to the way and lkpid.
3608 */
3609 le.lkpid = port->id;
3610 le.way = 0;
3611 le.data = 0;
3612
3613 /* Set initial CPU queue for receiving packets */
3614 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
3615 le.data |= port->first_rxq;
3616
3617 /* Disable classification engines */
3618 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
3619
3620 /* Update lookup ID table entry */
3621 mvpp2_cls_lookup_write(port->priv, &le);
3622}
3623
3624/* Set CPU queue number for oversize packets */
3625static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
3626{
3627 u32 val;
3628
3629 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
3630 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
3631
3632 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
3633 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
3634
3635 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
3636 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
3637 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
3638}
3639
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003640static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
3641{
3642 if (likely(pool->frag_size <= PAGE_SIZE))
3643 return netdev_alloc_frag(pool->frag_size);
3644 else
3645 return kmalloc(pool->frag_size, GFP_ATOMIC);
3646}
3647
3648static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
3649{
3650 if (likely(pool->frag_size <= PAGE_SIZE))
3651 skb_free_frag(data);
3652 else
3653 kfree(data);
3654}
3655
Marcin Wojtas3f518502014-07-10 16:52:13 -03003656/* Buffer Manager configuration routines */
3657
3658/* Create pool */
3659static int mvpp2_bm_pool_create(struct platform_device *pdev,
3660 struct mvpp2 *priv,
3661 struct mvpp2_bm_pool *bm_pool, int size)
3662{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003663 u32 val;
3664
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003665 /* Number of buffer pointers must be a multiple of 16, as per
3666 * hardware constraints
3667 */
3668 if (!IS_ALIGNED(size, 16))
3669 return -EINVAL;
3670
3671 /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
3672 * bytes per buffer pointer
3673 */
3674 if (priv->hw_version == MVPP21)
3675 bm_pool->size_bytes = 2 * sizeof(u32) * size;
3676 else
3677 bm_pool->size_bytes = 2 * sizeof(u64) * size;
3678
3679 bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003680 &bm_pool->dma_addr,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003681 GFP_KERNEL);
3682 if (!bm_pool->virt_addr)
3683 return -ENOMEM;
3684
Thomas Petazzonid3158802017-02-21 11:28:13 +01003685 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
3686 MVPP2_BM_POOL_PTR_ALIGN)) {
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003687 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
3688 bm_pool->virt_addr, bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003689 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
3690 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
3691 return -ENOMEM;
3692 }
3693
3694 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003695 lower_32_bits(bm_pool->dma_addr));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003696 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
3697
3698 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3699 val |= MVPP2_BM_START_MASK;
3700 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3701
3702 bm_pool->type = MVPP2_BM_FREE;
3703 bm_pool->size = size;
3704 bm_pool->pkt_size = 0;
3705 bm_pool->buf_num = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003706
3707 return 0;
3708}
3709
3710/* Set pool buffer size */
3711static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
3712 struct mvpp2_bm_pool *bm_pool,
3713 int buf_size)
3714{
3715 u32 val;
3716
3717 bm_pool->buf_size = buf_size;
3718
3719 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
3720 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
3721}
3722
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003723static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
3724 struct mvpp2_bm_pool *bm_pool,
3725 dma_addr_t *dma_addr,
3726 phys_addr_t *phys_addr)
3727{
Thomas Petazzonia7868412017-03-07 16:53:13 +01003728 int cpu = smp_processor_id();
3729
3730 *dma_addr = mvpp2_percpu_read(priv, cpu,
3731 MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
3732 *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003733
3734 if (priv->hw_version == MVPP22) {
3735 u32 val;
3736 u32 dma_addr_highbits, phys_addr_highbits;
3737
Thomas Petazzonia7868412017-03-07 16:53:13 +01003738 val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003739 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
3740 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
3741 MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
3742
3743 if (sizeof(dma_addr_t) == 8)
3744 *dma_addr |= (u64)dma_addr_highbits << 32;
3745
3746 if (sizeof(phys_addr_t) == 8)
3747 *phys_addr |= (u64)phys_addr_highbits << 32;
3748 }
3749}
3750
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003751/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01003752static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
3753 struct mvpp2_bm_pool *bm_pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003754{
3755 int i;
3756
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003757 for (i = 0; i < bm_pool->buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01003758 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003759 phys_addr_t buf_phys_addr;
3760 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003761
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003762 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
3763 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01003764
Thomas Petazzoni20396132017-03-07 16:53:00 +01003765 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01003766 bm_pool->buf_size, DMA_FROM_DEVICE);
3767
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003768 data = (void *)phys_to_virt(buf_phys_addr);
3769 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003770 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003771
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003772 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003773 }
3774
3775 /* Update BM driver with number of buffers removed from pool */
3776 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003777}
3778
3779/* Cleanup pool */
3780static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
3781 struct mvpp2 *priv,
3782 struct mvpp2_bm_pool *bm_pool)
3783{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003784 u32 val;
3785
Marcin Wojtas4229d502015-12-03 15:20:50 +01003786 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03003787 if (bm_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003788 WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
3789 return 0;
3790 }
3791
3792 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3793 val |= MVPP2_BM_STOP_MASK;
3794 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3795
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003796 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003797 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003798 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003799 return 0;
3800}
3801
3802static int mvpp2_bm_pools_init(struct platform_device *pdev,
3803 struct mvpp2 *priv)
3804{
3805 int i, err, size;
3806 struct mvpp2_bm_pool *bm_pool;
3807
3808 /* Create all pools with maximum size */
3809 size = MVPP2_BM_POOL_SIZE_MAX;
3810 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3811 bm_pool = &priv->bm_pools[i];
3812 bm_pool->id = i;
3813 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
3814 if (err)
3815 goto err_unroll_pools;
3816 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
3817 }
3818 return 0;
3819
3820err_unroll_pools:
3821 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
3822 for (i = i - 1; i >= 0; i--)
3823 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
3824 return err;
3825}
3826
3827static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
3828{
3829 int i, err;
3830
3831 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3832 /* Mask BM all interrupts */
3833 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
3834 /* Clear BM cause register */
3835 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
3836 }
3837
3838 /* Allocate and initialize BM pools */
3839 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
Markus Elfring81f915e2017-04-17 09:06:33 +02003840 sizeof(*priv->bm_pools), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003841 if (!priv->bm_pools)
3842 return -ENOMEM;
3843
3844 err = mvpp2_bm_pools_init(pdev, priv);
3845 if (err < 0)
3846 return err;
3847 return 0;
3848}
3849
3850/* Attach long pool to rxq */
3851static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
3852 int lrxq, int long_pool)
3853{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003854 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003855 int prxq;
3856
3857 /* Get queue physical ID */
3858 prxq = port->rxqs[lrxq]->id;
3859
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003860 if (port->priv->hw_version == MVPP21)
3861 mask = MVPP21_RXQ_POOL_LONG_MASK;
3862 else
3863 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003864
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003865 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3866 val &= ~mask;
3867 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003868 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3869}
3870
3871/* Attach short pool to rxq */
3872static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
3873 int lrxq, int short_pool)
3874{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003875 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003876 int prxq;
3877
3878 /* Get queue physical ID */
3879 prxq = port->rxqs[lrxq]->id;
3880
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003881 if (port->priv->hw_version == MVPP21)
3882 mask = MVPP21_RXQ_POOL_SHORT_MASK;
3883 else
3884 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003885
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003886 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3887 val &= ~mask;
3888 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003889 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3890}
3891
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003892static void *mvpp2_buf_alloc(struct mvpp2_port *port,
3893 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003894 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003895 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003896 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003897{
Thomas Petazzoni20396132017-03-07 16:53:00 +01003898 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003899 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003900
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003901 data = mvpp2_frag_alloc(bm_pool);
3902 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003903 return NULL;
3904
Thomas Petazzoni20396132017-03-07 16:53:00 +01003905 dma_addr = dma_map_single(port->dev->dev.parent, data,
3906 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
3907 DMA_FROM_DEVICE);
3908 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003909 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003910 return NULL;
3911 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01003912 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003913 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003914
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003915 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003916}
3917
3918/* Set pool number in a BM cookie */
3919static inline u32 mvpp2_bm_cookie_pool_set(u32 cookie, int pool)
3920{
3921 u32 bm;
3922
3923 bm = cookie & ~(0xFF << MVPP2_BM_COOKIE_POOL_OFFS);
3924 bm |= ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS);
3925
3926 return bm;
3927}
3928
3929/* Get pool number from a BM cookie */
Thomas Petazzonid3158802017-02-21 11:28:13 +01003930static inline int mvpp2_bm_cookie_pool_get(unsigned long cookie)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003931{
3932 return (cookie >> MVPP2_BM_COOKIE_POOL_OFFS) & 0xFF;
3933}
3934
3935/* Release buffer to BM */
3936static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003937 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003938 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003939{
Thomas Petazzonia7868412017-03-07 16:53:13 +01003940 int cpu = smp_processor_id();
3941
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003942 if (port->priv->hw_version == MVPP22) {
3943 u32 val = 0;
3944
3945 if (sizeof(dma_addr_t) == 8)
3946 val |= upper_32_bits(buf_dma_addr) &
3947 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
3948
3949 if (sizeof(phys_addr_t) == 8)
3950 val |= (upper_32_bits(buf_phys_addr)
3951 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
3952 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
3953
Thomas Petazzonia7868412017-03-07 16:53:13 +01003954 mvpp2_percpu_write(port->priv, cpu,
3955 MVPP22_BM_ADDR_HIGH_RLS_REG, val);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003956 }
3957
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003958 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
3959 * returned in the "cookie" field of the RX
3960 * descriptor. Instead of storing the virtual address, we
3961 * store the physical address
3962 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01003963 mvpp2_percpu_write(port->priv, cpu,
3964 MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
3965 mvpp2_percpu_write(port->priv, cpu,
3966 MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003967}
3968
Marcin Wojtas3f518502014-07-10 16:52:13 -03003969/* Refill BM pool */
3970static void mvpp2_pool_refill(struct mvpp2_port *port, u32 bm,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003971 dma_addr_t dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003972 phys_addr_t phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003973{
3974 int pool = mvpp2_bm_cookie_pool_get(bm);
3975
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003976 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003977}
3978
3979/* Allocate buffers for the pool */
3980static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
3981 struct mvpp2_bm_pool *bm_pool, int buf_num)
3982{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003983 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01003984 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003985 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003986 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003987
3988 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
3989 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
3990
3991 if (buf_num < 0 ||
3992 (buf_num + bm_pool->buf_num > bm_pool->size)) {
3993 netdev_err(port->dev,
3994 "cannot allocate %d buffers for pool %d\n",
3995 buf_num, bm_pool->id);
3996 return 0;
3997 }
3998
Marcin Wojtas3f518502014-07-10 16:52:13 -03003999 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004000 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
4001 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004002 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004003 break;
4004
Thomas Petazzoni20396132017-03-07 16:53:00 +01004005 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01004006 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004007 }
4008
4009 /* Update BM driver with number of buffers added to pool */
4010 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004011
4012 netdev_dbg(port->dev,
4013 "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
4014 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4015 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
4016
4017 netdev_dbg(port->dev,
4018 "%s pool %d: %d of %d buffers added\n",
4019 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4020 bm_pool->id, i, buf_num);
4021 return i;
4022}
4023
4024/* Notify the driver that BM pool is being used as specific type and return the
4025 * pool pointer on success
4026 */
4027static struct mvpp2_bm_pool *
4028mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
4029 int pkt_size)
4030{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004031 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4032 int num;
4033
4034 if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
4035 netdev_err(port->dev, "mixing pool types is forbidden\n");
4036 return NULL;
4037 }
4038
Marcin Wojtas3f518502014-07-10 16:52:13 -03004039 if (new_pool->type == MVPP2_BM_FREE)
4040 new_pool->type = type;
4041
4042 /* Allocate buffers in case BM pool is used as long pool, but packet
4043 * size doesn't match MTU or BM pool hasn't being used yet
4044 */
4045 if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
4046 (new_pool->pkt_size == 0)) {
4047 int pkts_num;
4048
4049 /* Set default buffer number or free all the buffers in case
4050 * the pool is not empty
4051 */
4052 pkts_num = new_pool->buf_num;
4053 if (pkts_num == 0)
4054 pkts_num = type == MVPP2_BM_SWF_LONG ?
4055 MVPP2_BM_LONG_BUF_NUM :
4056 MVPP2_BM_SHORT_BUF_NUM;
4057 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01004058 mvpp2_bm_bufs_free(port->dev->dev.parent,
4059 port->priv, new_pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004060
4061 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004062 new_pool->frag_size =
4063 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4064 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004065
4066 /* Allocate buffers for this pool */
4067 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4068 if (num != pkts_num) {
4069 WARN(1, "pool %d: %d of %d allocated\n",
4070 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004071 return NULL;
4072 }
4073 }
4074
4075 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4076 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4077
Marcin Wojtas3f518502014-07-10 16:52:13 -03004078 return new_pool;
4079}
4080
4081/* Initialize pools for swf */
4082static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4083{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004084 int rxq;
4085
4086 if (!port->pool_long) {
4087 port->pool_long =
4088 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
4089 MVPP2_BM_SWF_LONG,
4090 port->pkt_size);
4091 if (!port->pool_long)
4092 return -ENOMEM;
4093
Marcin Wojtas3f518502014-07-10 16:52:13 -03004094 port->pool_long->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004095
4096 for (rxq = 0; rxq < rxq_number; rxq++)
4097 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4098 }
4099
4100 if (!port->pool_short) {
4101 port->pool_short =
4102 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
4103 MVPP2_BM_SWF_SHORT,
4104 MVPP2_BM_SHORT_PKT_SIZE);
4105 if (!port->pool_short)
4106 return -ENOMEM;
4107
Marcin Wojtas3f518502014-07-10 16:52:13 -03004108 port->pool_short->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004109
4110 for (rxq = 0; rxq < rxq_number; rxq++)
4111 mvpp2_rxq_short_pool_set(port, rxq,
4112 port->pool_short->id);
4113 }
4114
4115 return 0;
4116}
4117
4118static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4119{
4120 struct mvpp2_port *port = netdev_priv(dev);
4121 struct mvpp2_bm_pool *port_pool = port->pool_long;
4122 int num, pkts_num = port_pool->buf_num;
4123 int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
4124
4125 /* Update BM pool with new buffer size */
Marcin Wojtas4229d502015-12-03 15:20:50 +01004126 mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03004127 if (port_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004128 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
4129 return -EIO;
4130 }
4131
4132 port_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004133 port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4134 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004135 num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4136 if (num != pkts_num) {
4137 WARN(1, "pool %d: %d of %d allocated\n",
4138 port_pool->id, num, pkts_num);
4139 return -EIO;
4140 }
4141
4142 mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
4143 MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
4144 dev->mtu = mtu;
4145 netdev_update_features(dev);
4146 return 0;
4147}
4148
4149static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4150{
4151 int cpu, cpu_mask = 0;
4152
4153 for_each_present_cpu(cpu)
4154 cpu_mask |= 1 << cpu;
4155 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4156 MVPP2_ISR_ENABLE_INTERRUPT(cpu_mask));
4157}
4158
4159static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4160{
4161 int cpu, cpu_mask = 0;
4162
4163 for_each_present_cpu(cpu)
4164 cpu_mask |= 1 << cpu;
4165 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4166 MVPP2_ISR_DISABLE_INTERRUPT(cpu_mask));
4167}
4168
4169/* Mask the current CPU's Rx/Tx interrupts */
4170static void mvpp2_interrupts_mask(void *arg)
4171{
4172 struct mvpp2_port *port = arg;
4173
Thomas Petazzonia7868412017-03-07 16:53:13 +01004174 mvpp2_percpu_write(port->priv, smp_processor_id(),
4175 MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004176}
4177
4178/* Unmask the current CPU's Rx/Tx interrupts */
4179static void mvpp2_interrupts_unmask(void *arg)
4180{
4181 struct mvpp2_port *port = arg;
4182
Thomas Petazzonia7868412017-03-07 16:53:13 +01004183 mvpp2_percpu_write(port->priv, smp_processor_id(),
4184 MVPP2_ISR_RX_TX_MASK_REG(port->id),
4185 (MVPP2_CAUSE_MISC_SUM_MASK |
4186 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004187}
4188
4189/* Port configuration routines */
4190
Thomas Petazzoni26975822017-03-07 16:53:14 +01004191static void mvpp22_port_mii_set(struct mvpp2_port *port)
4192{
4193 u32 val;
4194
4195 return;
4196
4197 /* Only GOP port 0 has an XLG MAC */
4198 if (port->gop_id == 0) {
4199 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
4200 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
Antoine Ténart725757a2017-06-12 16:01:39 +02004201
4202 if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4203 port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4204 val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
4205 else
4206 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4207
Thomas Petazzoni26975822017-03-07 16:53:14 +01004208 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
4209 }
4210
4211 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4212 if (port->phy_interface == PHY_INTERFACE_MODE_RGMII)
4213 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4214 else
4215 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4216 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
4217 val |= MVPP22_CTRL4_SYNC_BYPASS;
4218 val |= MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4219 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4220}
4221
Marcin Wojtas3f518502014-07-10 16:52:13 -03004222static void mvpp2_port_mii_set(struct mvpp2_port *port)
4223{
Marcin Wojtas08a23752014-07-21 13:48:12 -03004224 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004225
Thomas Petazzoni26975822017-03-07 16:53:14 +01004226 if (port->priv->hw_version == MVPP22)
4227 mvpp22_port_mii_set(port);
4228
Marcin Wojtas08a23752014-07-21 13:48:12 -03004229 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004230
Marcin Wojtas08a23752014-07-21 13:48:12 -03004231 switch (port->phy_interface) {
4232 case PHY_INTERFACE_MODE_SGMII:
4233 val |= MVPP2_GMAC_INBAND_AN_MASK;
4234 break;
4235 case PHY_INTERFACE_MODE_RGMII:
4236 val |= MVPP2_GMAC_PORT_RGMII_MASK;
4237 default:
4238 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
4239 }
4240
4241 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4242}
4243
4244static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
4245{
4246 u32 val;
4247
4248 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4249 val |= MVPP2_GMAC_FC_ADV_EN;
4250 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004251}
4252
4253static void mvpp2_port_enable(struct mvpp2_port *port)
4254{
4255 u32 val;
4256
Antoine Ténart725757a2017-06-12 16:01:39 +02004257 /* Only GOP port 0 has an XLG MAC */
4258 if (port->gop_id == 0 &&
4259 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4260 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4261 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4262 val |= MVPP22_XLG_CTRL0_PORT_EN |
4263 MVPP22_XLG_CTRL0_MAC_RESET_DIS;
4264 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
4265 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4266 } else {
4267 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4268 val |= MVPP2_GMAC_PORT_EN_MASK;
4269 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
4270 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4271 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004272}
4273
4274static void mvpp2_port_disable(struct mvpp2_port *port)
4275{
4276 u32 val;
4277
Antoine Ténart725757a2017-06-12 16:01:39 +02004278 /* Only GOP port 0 has an XLG MAC */
4279 if (port->gop_id == 0 &&
4280 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4281 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4282 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4283 val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
4284 MVPP22_XLG_CTRL0_MAC_RESET_DIS);
4285 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4286 } else {
4287 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4288 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
4289 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4290 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004291}
4292
4293/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
4294static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
4295{
4296 u32 val;
4297
4298 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
4299 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
4300 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4301}
4302
4303/* Configure loopback port */
4304static void mvpp2_port_loopback_set(struct mvpp2_port *port)
4305{
4306 u32 val;
4307
4308 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4309
4310 if (port->speed == 1000)
4311 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
4312 else
4313 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
4314
4315 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4316 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
4317 else
4318 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
4319
4320 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4321}
4322
4323static void mvpp2_port_reset(struct mvpp2_port *port)
4324{
4325 u32 val;
4326
4327 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4328 ~MVPP2_GMAC_PORT_RESET_MASK;
4329 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4330
4331 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4332 MVPP2_GMAC_PORT_RESET_MASK)
4333 continue;
4334}
4335
4336/* Change maximum receive size of the port */
4337static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
4338{
4339 u32 val;
4340
4341 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4342 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
4343 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
4344 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
4345 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4346}
4347
4348/* Set defaults to the MVPP2 port */
4349static void mvpp2_defaults_set(struct mvpp2_port *port)
4350{
4351 int tx_port_num, val, queue, ptxq, lrxq;
4352
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004353 if (port->priv->hw_version == MVPP21) {
4354 /* Configure port to loopback if needed */
4355 if (port->flags & MVPP2_F_LOOPBACK)
4356 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004357
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004358 /* Update TX FIFO MIN Threshold */
4359 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4360 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
4361 /* Min. TX threshold must be less than minimal packet length */
4362 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
4363 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4364 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004365
4366 /* Disable Legacy WRR, Disable EJP, Release from reset */
4367 tx_port_num = mvpp2_egress_port(port);
4368 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
4369 tx_port_num);
4370 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
4371
4372 /* Close bandwidth for all queues */
4373 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
4374 ptxq = mvpp2_txq_phys(port->id, queue);
4375 mvpp2_write(port->priv,
4376 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
4377 }
4378
4379 /* Set refill period to 1 usec, refill tokens
4380 * and bucket size to maximum
4381 */
4382 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
4383 port->priv->tclk / USEC_PER_SEC);
4384 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
4385 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
4386 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
4387 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
4388 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
4389 val = MVPP2_TXP_TOKEN_SIZE_MAX;
4390 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4391
4392 /* Set MaximumLowLatencyPacketSize value to 256 */
4393 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
4394 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
4395 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
4396
4397 /* Enable Rx cache snoop */
4398 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
4399 queue = port->rxqs[lrxq]->id;
4400 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4401 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
4402 MVPP2_SNOOP_BUF_HDR_MASK;
4403 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4404 }
4405
4406 /* At default, mask all interrupts to all present cpus */
4407 mvpp2_interrupts_disable(port);
4408}
4409
4410/* Enable/disable receiving packets */
4411static void mvpp2_ingress_enable(struct mvpp2_port *port)
4412{
4413 u32 val;
4414 int lrxq, queue;
4415
4416 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
4417 queue = port->rxqs[lrxq]->id;
4418 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4419 val &= ~MVPP2_RXQ_DISABLE_MASK;
4420 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4421 }
4422}
4423
4424static void mvpp2_ingress_disable(struct mvpp2_port *port)
4425{
4426 u32 val;
4427 int lrxq, queue;
4428
4429 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
4430 queue = port->rxqs[lrxq]->id;
4431 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4432 val |= MVPP2_RXQ_DISABLE_MASK;
4433 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4434 }
4435}
4436
4437/* Enable transmit via physical egress queue
4438 * - HW starts take descriptors from DRAM
4439 */
4440static void mvpp2_egress_enable(struct mvpp2_port *port)
4441{
4442 u32 qmap;
4443 int queue;
4444 int tx_port_num = mvpp2_egress_port(port);
4445
4446 /* Enable all initialized TXs. */
4447 qmap = 0;
4448 for (queue = 0; queue < txq_number; queue++) {
4449 struct mvpp2_tx_queue *txq = port->txqs[queue];
4450
Markus Elfringdbbb2f02017-04-17 14:07:52 +02004451 if (txq->descs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004452 qmap |= (1 << queue);
4453 }
4454
4455 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4456 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
4457}
4458
4459/* Disable transmit via physical egress queue
4460 * - HW doesn't take descriptors from DRAM
4461 */
4462static void mvpp2_egress_disable(struct mvpp2_port *port)
4463{
4464 u32 reg_data;
4465 int delay;
4466 int tx_port_num = mvpp2_egress_port(port);
4467
4468 /* Issue stop command for active channels only */
4469 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4470 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
4471 MVPP2_TXP_SCHED_ENQ_MASK;
4472 if (reg_data != 0)
4473 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
4474 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
4475
4476 /* Wait for all Tx activity to terminate. */
4477 delay = 0;
4478 do {
4479 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
4480 netdev_warn(port->dev,
4481 "Tx stop timed out, status=0x%08x\n",
4482 reg_data);
4483 break;
4484 }
4485 mdelay(1);
4486 delay++;
4487
4488 /* Check port TX Command register that all
4489 * Tx queues are stopped
4490 */
4491 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
4492 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
4493}
4494
4495/* Rx descriptors helper methods */
4496
4497/* Get number of Rx descriptors occupied by received packets */
4498static inline int
4499mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
4500{
4501 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
4502
4503 return val & MVPP2_RXQ_OCCUPIED_MASK;
4504}
4505
4506/* Update Rx queue status with the number of occupied and available
4507 * Rx descriptor slots.
4508 */
4509static inline void
4510mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
4511 int used_count, int free_count)
4512{
4513 /* Decrement the number of used descriptors and increment count
4514 * increment the number of free descriptors.
4515 */
4516 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
4517
4518 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
4519}
4520
4521/* Get pointer to next RX descriptor to be processed by SW */
4522static inline struct mvpp2_rx_desc *
4523mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
4524{
4525 int rx_desc = rxq->next_desc_to_proc;
4526
4527 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
4528 prefetch(rxq->descs + rxq->next_desc_to_proc);
4529 return rxq->descs + rx_desc;
4530}
4531
4532/* Set rx queue offset */
4533static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
4534 int prxq, int offset)
4535{
4536 u32 val;
4537
4538 /* Convert offset from bytes to units of 32 bytes */
4539 offset = offset >> 5;
4540
4541 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4542 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
4543
4544 /* Offset is in */
4545 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
4546 MVPP2_RXQ_PACKET_OFFSET_MASK);
4547
4548 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4549}
4550
4551/* Obtain BM cookie information from descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01004552static u32 mvpp2_bm_cookie_build(struct mvpp2_port *port,
4553 struct mvpp2_rx_desc *rx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004554{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004555 int cpu = smp_processor_id();
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01004556 int pool;
4557
4558 pool = (mvpp2_rxdesc_status_get(port, rx_desc) &
4559 MVPP2_RXD_BM_POOL_ID_MASK) >>
4560 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004561
4562 return ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS) |
4563 ((cpu & 0xFF) << MVPP2_BM_COOKIE_CPU_OFFS);
4564}
4565
4566/* Tx descriptors helper methods */
4567
Marcin Wojtas3f518502014-07-10 16:52:13 -03004568/* Get pointer to next Tx descriptor to be processed (send) by HW */
4569static struct mvpp2_tx_desc *
4570mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
4571{
4572 int tx_desc = txq->next_desc_to_proc;
4573
4574 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
4575 return txq->descs + tx_desc;
4576}
4577
4578/* Update HW with number of aggregated Tx descriptors to be sent */
4579static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
4580{
4581 /* aggregated access - relevant TXQ number is written in TX desc */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004582 mvpp2_percpu_write(port->priv, smp_processor_id(),
4583 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004584}
4585
4586
4587/* Check if there are enough free descriptors in aggregated txq.
4588 * If not, update the number of occupied descriptors and repeat the check.
4589 */
4590static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
4591 struct mvpp2_tx_queue *aggr_txq, int num)
4592{
4593 if ((aggr_txq->count + num) > aggr_txq->size) {
4594 /* Update number of occupied aggregated Tx descriptors */
4595 int cpu = smp_processor_id();
4596 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
4597
4598 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
4599 }
4600
4601 if ((aggr_txq->count + num) > aggr_txq->size)
4602 return -ENOMEM;
4603
4604 return 0;
4605}
4606
4607/* Reserved Tx descriptors allocation request */
4608static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
4609 struct mvpp2_tx_queue *txq, int num)
4610{
4611 u32 val;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004612 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004613
4614 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004615 mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004616
Thomas Petazzonia7868412017-03-07 16:53:13 +01004617 val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004618
4619 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
4620}
4621
4622/* Check if there are enough reserved descriptors for transmission.
4623 * If not, request chunk of reserved descriptors and check again.
4624 */
4625static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
4626 struct mvpp2_tx_queue *txq,
4627 struct mvpp2_txq_pcpu *txq_pcpu,
4628 int num)
4629{
4630 int req, cpu, desc_count;
4631
4632 if (txq_pcpu->reserved_num >= num)
4633 return 0;
4634
4635 /* Not enough descriptors reserved! Update the reserved descriptor
4636 * count and check again.
4637 */
4638
4639 desc_count = 0;
4640 /* Compute total of used descriptors */
4641 for_each_present_cpu(cpu) {
4642 struct mvpp2_txq_pcpu *txq_pcpu_aux;
4643
4644 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
4645 desc_count += txq_pcpu_aux->count;
4646 desc_count += txq_pcpu_aux->reserved_num;
4647 }
4648
4649 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
4650 desc_count += req;
4651
4652 if (desc_count >
4653 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
4654 return -ENOMEM;
4655
4656 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
4657
4658 /* OK, the descriptor cound has been updated: check again. */
4659 if (txq_pcpu->reserved_num < num)
4660 return -ENOMEM;
4661 return 0;
4662}
4663
4664/* Release the last allocated Tx descriptor. Useful to handle DMA
4665 * mapping failures in the Tx path.
4666 */
4667static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
4668{
4669 if (txq->next_desc_to_proc == 0)
4670 txq->next_desc_to_proc = txq->last_desc - 1;
4671 else
4672 txq->next_desc_to_proc--;
4673}
4674
4675/* Set Tx descriptors fields relevant for CSUM calculation */
4676static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
4677 int ip_hdr_len, int l4_proto)
4678{
4679 u32 command;
4680
4681 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
4682 * G_L4_chk, L4_type required only for checksum calculation
4683 */
4684 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
4685 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
4686 command |= MVPP2_TXD_IP_CSUM_DISABLE;
4687
4688 if (l3_proto == swab16(ETH_P_IP)) {
4689 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
4690 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
4691 } else {
4692 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
4693 }
4694
4695 if (l4_proto == IPPROTO_TCP) {
4696 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
4697 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
4698 } else if (l4_proto == IPPROTO_UDP) {
4699 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
4700 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
4701 } else {
4702 command |= MVPP2_TXD_L4_CSUM_NOT;
4703 }
4704
4705 return command;
4706}
4707
4708/* Get number of sent descriptors and decrement counter.
4709 * The number of sent descriptors is returned.
4710 * Per-CPU access
4711 */
4712static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
4713 struct mvpp2_tx_queue *txq)
4714{
4715 u32 val;
4716
4717 /* Reading status reg resets transmitted descriptor counter */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004718 val = mvpp2_percpu_read(port->priv, smp_processor_id(),
4719 MVPP2_TXQ_SENT_REG(txq->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004720
4721 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
4722 MVPP2_TRANSMITTED_COUNT_OFFSET;
4723}
4724
4725static void mvpp2_txq_sent_counter_clear(void *arg)
4726{
4727 struct mvpp2_port *port = arg;
4728 int queue;
4729
4730 for (queue = 0; queue < txq_number; queue++) {
4731 int id = port->txqs[queue]->id;
4732
Thomas Petazzonia7868412017-03-07 16:53:13 +01004733 mvpp2_percpu_read(port->priv, smp_processor_id(),
4734 MVPP2_TXQ_SENT_REG(id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004735 }
4736}
4737
4738/* Set max sizes for Tx queues */
4739static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
4740{
4741 u32 val, size, mtu;
4742 int txq, tx_port_num;
4743
4744 mtu = port->pkt_size * 8;
4745 if (mtu > MVPP2_TXP_MTU_MAX)
4746 mtu = MVPP2_TXP_MTU_MAX;
4747
4748 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
4749 mtu = 3 * mtu;
4750
4751 /* Indirect access to registers */
4752 tx_port_num = mvpp2_egress_port(port);
4753 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4754
4755 /* Set MTU */
4756 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
4757 val &= ~MVPP2_TXP_MTU_MAX;
4758 val |= mtu;
4759 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
4760
4761 /* TXP token size and all TXQs token size must be larger that MTU */
4762 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
4763 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
4764 if (size < mtu) {
4765 size = mtu;
4766 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
4767 val |= size;
4768 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4769 }
4770
4771 for (txq = 0; txq < txq_number; txq++) {
4772 val = mvpp2_read(port->priv,
4773 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
4774 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
4775
4776 if (size < mtu) {
4777 size = mtu;
4778 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
4779 val |= size;
4780 mvpp2_write(port->priv,
4781 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
4782 val);
4783 }
4784 }
4785}
4786
4787/* Set the number of packets that will be received before Rx interrupt
4788 * will be generated by HW.
4789 */
4790static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004791 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004792{
Thomas Petazzonia7868412017-03-07 16:53:13 +01004793 int cpu = smp_processor_id();
4794
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01004795 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
4796 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004797
Thomas Petazzonia7868412017-03-07 16:53:13 +01004798 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
4799 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
4800 rxq->pkts_coal);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004801}
4802
Thomas Petazzoniab426762017-02-21 11:28:04 +01004803static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
4804{
4805 u64 tmp = (u64)clk_hz * usec;
4806
4807 do_div(tmp, USEC_PER_SEC);
4808
4809 return tmp > U32_MAX ? U32_MAX : tmp;
4810}
4811
4812static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
4813{
4814 u64 tmp = (u64)cycles * USEC_PER_SEC;
4815
4816 do_div(tmp, clk_hz);
4817
4818 return tmp > U32_MAX ? U32_MAX : tmp;
4819}
4820
Marcin Wojtas3f518502014-07-10 16:52:13 -03004821/* Set the time delay in usec before Rx interrupt */
4822static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004823 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004824{
Thomas Petazzoniab426762017-02-21 11:28:04 +01004825 unsigned long freq = port->priv->tclk;
4826 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004827
Thomas Petazzoniab426762017-02-21 11:28:04 +01004828 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
4829 rxq->time_coal =
4830 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
4831
4832 /* re-evaluate to get actual register value */
4833 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
4834 }
4835
Marcin Wojtas3f518502014-07-10 16:52:13 -03004836 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004837}
4838
Marcin Wojtas3f518502014-07-10 16:52:13 -03004839/* Free Tx queue skbuffs */
4840static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
4841 struct mvpp2_tx_queue *txq,
4842 struct mvpp2_txq_pcpu *txq_pcpu, int num)
4843{
4844 int i;
4845
4846 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01004847 struct mvpp2_txq_pcpu_buf *tx_buf =
4848 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004849
Thomas Petazzoni20396132017-03-07 16:53:00 +01004850 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
Thomas Petazzoni83544912016-12-21 11:28:49 +01004851 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01004852 if (tx_buf->skb)
4853 dev_kfree_skb_any(tx_buf->skb);
4854
4855 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004856 }
4857}
4858
4859static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
4860 u32 cause)
4861{
4862 int queue = fls(cause) - 1;
4863
4864 return port->rxqs[queue];
4865}
4866
4867static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
4868 u32 cause)
4869{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02004870 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004871
4872 return port->txqs[queue];
4873}
4874
4875/* Handle end of transmission */
4876static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
4877 struct mvpp2_txq_pcpu *txq_pcpu)
4878{
4879 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
4880 int tx_done;
4881
4882 if (txq_pcpu->cpu != smp_processor_id())
4883 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
4884
4885 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
4886 if (!tx_done)
4887 return;
4888 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
4889
4890 txq_pcpu->count -= tx_done;
4891
4892 if (netif_tx_queue_stopped(nq))
4893 if (txq_pcpu->size - txq_pcpu->count >= MAX_SKB_FRAGS + 1)
4894 netif_tx_wake_queue(nq);
4895}
4896
Marcin Wojtasedc660f2015-08-06 19:00:30 +02004897static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause)
4898{
4899 struct mvpp2_tx_queue *txq;
4900 struct mvpp2_txq_pcpu *txq_pcpu;
4901 unsigned int tx_todo = 0;
4902
4903 while (cause) {
4904 txq = mvpp2_get_tx_queue(port, cause);
4905 if (!txq)
4906 break;
4907
4908 txq_pcpu = this_cpu_ptr(txq->pcpu);
4909
4910 if (txq_pcpu->count) {
4911 mvpp2_txq_done(port, txq, txq_pcpu);
4912 tx_todo += txq_pcpu->count;
4913 }
4914
4915 cause &= ~(1 << txq->log_id);
4916 }
4917 return tx_todo;
4918}
4919
Marcin Wojtas3f518502014-07-10 16:52:13 -03004920/* Rx/Tx queue initialization/cleanup methods */
4921
4922/* Allocate and initialize descriptors for aggr TXQ */
4923static int mvpp2_aggr_txq_init(struct platform_device *pdev,
4924 struct mvpp2_tx_queue *aggr_txq,
4925 int desc_num, int cpu,
4926 struct mvpp2 *priv)
4927{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004928 u32 txq_dma;
4929
Marcin Wojtas3f518502014-07-10 16:52:13 -03004930 /* Allocate memory for TX descriptors */
4931 aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
4932 desc_num * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004933 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004934 if (!aggr_txq->descs)
4935 return -ENOMEM;
4936
Marcin Wojtas3f518502014-07-10 16:52:13 -03004937 aggr_txq->last_desc = aggr_txq->size - 1;
4938
4939 /* Aggr TXQ no reset WA */
4940 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
4941 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
4942
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004943 /* Set Tx descriptors queue starting address indirect
4944 * access
4945 */
4946 if (priv->hw_version == MVPP21)
4947 txq_dma = aggr_txq->descs_dma;
4948 else
4949 txq_dma = aggr_txq->descs_dma >>
4950 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
4951
4952 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004953 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
4954
4955 return 0;
4956}
4957
4958/* Create a specified Rx queue */
4959static int mvpp2_rxq_init(struct mvpp2_port *port,
4960 struct mvpp2_rx_queue *rxq)
4961
4962{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004963 u32 rxq_dma;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004964 int cpu;
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004965
Marcin Wojtas3f518502014-07-10 16:52:13 -03004966 rxq->size = port->rx_ring_size;
4967
4968 /* Allocate memory for RX descriptors */
4969 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
4970 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004971 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004972 if (!rxq->descs)
4973 return -ENOMEM;
4974
Marcin Wojtas3f518502014-07-10 16:52:13 -03004975 rxq->last_desc = rxq->size - 1;
4976
4977 /* Zero occupied and non-occupied counters - direct access */
4978 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
4979
4980 /* Set Rx descriptors queue starting address - indirect access */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004981 cpu = smp_processor_id();
4982 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004983 if (port->priv->hw_version == MVPP21)
4984 rxq_dma = rxq->descs_dma;
4985 else
4986 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004987 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
4988 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
4989 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004990
4991 /* Set Offset */
4992 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
4993
4994 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004995 mvpp2_rx_pkts_coal_set(port, rxq);
4996 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004997
4998 /* Add number of descriptors ready for receiving packets */
4999 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
5000
5001 return 0;
5002}
5003
5004/* Push packets received by the RXQ to BM pool */
5005static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
5006 struct mvpp2_rx_queue *rxq)
5007{
5008 int rx_received, i;
5009
5010 rx_received = mvpp2_rxq_received(port, rxq->id);
5011 if (!rx_received)
5012 return;
5013
5014 for (i = 0; i < rx_received; i++) {
5015 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005016 u32 bm = mvpp2_bm_cookie_build(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005017
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005018 mvpp2_pool_refill(port, bm,
5019 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
5020 mvpp2_rxdesc_cookie_get(port, rx_desc));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005021 }
5022 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
5023}
5024
5025/* Cleanup Rx queue */
5026static void mvpp2_rxq_deinit(struct mvpp2_port *port,
5027 struct mvpp2_rx_queue *rxq)
5028{
Thomas Petazzonia7868412017-03-07 16:53:13 +01005029 int cpu;
5030
Marcin Wojtas3f518502014-07-10 16:52:13 -03005031 mvpp2_rxq_drop_pkts(port, rxq);
5032
5033 if (rxq->descs)
5034 dma_free_coherent(port->dev->dev.parent,
5035 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
5036 rxq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005037 rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005038
5039 rxq->descs = NULL;
5040 rxq->last_desc = 0;
5041 rxq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005042 rxq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005043
5044 /* Clear Rx descriptors queue starting address and size;
5045 * free descriptor number
5046 */
5047 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01005048 cpu = smp_processor_id();
5049 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5050 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
5051 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005052}
5053
5054/* Create and initialize a Tx queue */
5055static int mvpp2_txq_init(struct mvpp2_port *port,
5056 struct mvpp2_tx_queue *txq)
5057{
5058 u32 val;
5059 int cpu, desc, desc_per_txq, tx_port_num;
5060 struct mvpp2_txq_pcpu *txq_pcpu;
5061
5062 txq->size = port->tx_ring_size;
5063
5064 /* Allocate memory for Tx descriptors */
5065 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
5066 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005067 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005068 if (!txq->descs)
5069 return -ENOMEM;
5070
Marcin Wojtas3f518502014-07-10 16:52:13 -03005071 txq->last_desc = txq->size - 1;
5072
5073 /* Set Tx descriptors queue starting address - indirect access */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005074 cpu = smp_processor_id();
5075 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5076 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
5077 txq->descs_dma);
5078 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
5079 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
5080 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
5081 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
5082 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
5083 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005084 val &= ~MVPP2_TXQ_PENDING_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005085 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005086
5087 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
5088 * for each existing TXQ.
5089 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
5090 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
5091 */
5092 desc_per_txq = 16;
5093 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
5094 (txq->log_id * desc_per_txq);
5095
Thomas Petazzonia7868412017-03-07 16:53:13 +01005096 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
5097 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
5098 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005099
5100 /* WRR / EJP configuration - indirect access */
5101 tx_port_num = mvpp2_egress_port(port);
5102 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5103
5104 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
5105 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
5106 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
5107 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
5108 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
5109
5110 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
5111 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
5112 val);
5113
5114 for_each_present_cpu(cpu) {
5115 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5116 txq_pcpu->size = txq->size;
Markus Elfring02c91ec2017-04-17 08:09:07 +02005117 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
5118 sizeof(*txq_pcpu->buffs),
5119 GFP_KERNEL);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005120 if (!txq_pcpu->buffs)
Markus Elfring20b1e162017-04-17 12:58:33 +02005121 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005122
5123 txq_pcpu->count = 0;
5124 txq_pcpu->reserved_num = 0;
5125 txq_pcpu->txq_put_index = 0;
5126 txq_pcpu->txq_get_index = 0;
5127 }
5128
5129 return 0;
Markus Elfring20b1e162017-04-17 12:58:33 +02005130cleanup:
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005131 for_each_present_cpu(cpu) {
5132 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005133 kfree(txq_pcpu->buffs);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005134 }
5135
5136 dma_free_coherent(port->dev->dev.parent,
5137 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005138 txq->descs, txq->descs_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005139
5140 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005141}
5142
5143/* Free allocated TXQ resources */
5144static void mvpp2_txq_deinit(struct mvpp2_port *port,
5145 struct mvpp2_tx_queue *txq)
5146{
5147 struct mvpp2_txq_pcpu *txq_pcpu;
5148 int cpu;
5149
5150 for_each_present_cpu(cpu) {
5151 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005152 kfree(txq_pcpu->buffs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005153 }
5154
5155 if (txq->descs)
5156 dma_free_coherent(port->dev->dev.parent,
5157 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005158 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005159
5160 txq->descs = NULL;
5161 txq->last_desc = 0;
5162 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005163 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005164
5165 /* Set minimum bandwidth for disabled TXQs */
5166 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
5167
5168 /* Set Tx descriptors queue starting address and size */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005169 cpu = smp_processor_id();
5170 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5171 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
5172 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005173}
5174
5175/* Cleanup Tx ports */
5176static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
5177{
5178 struct mvpp2_txq_pcpu *txq_pcpu;
5179 int delay, pending, cpu;
5180 u32 val;
5181
Thomas Petazzonia7868412017-03-07 16:53:13 +01005182 cpu = smp_processor_id();
5183 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5184 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005185 val |= MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005186 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005187
5188 /* The napi queue has been stopped so wait for all packets
5189 * to be transmitted.
5190 */
5191 delay = 0;
5192 do {
5193 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
5194 netdev_warn(port->dev,
5195 "port %d: cleaning queue %d timed out\n",
5196 port->id, txq->log_id);
5197 break;
5198 }
5199 mdelay(1);
5200 delay++;
5201
Thomas Petazzonia7868412017-03-07 16:53:13 +01005202 pending = mvpp2_percpu_read(port->priv, cpu,
5203 MVPP2_TXQ_PENDING_REG);
5204 pending &= MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005205 } while (pending);
5206
5207 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005208 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005209
5210 for_each_present_cpu(cpu) {
5211 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5212
5213 /* Release all packets */
5214 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
5215
5216 /* Reset queue */
5217 txq_pcpu->count = 0;
5218 txq_pcpu->txq_put_index = 0;
5219 txq_pcpu->txq_get_index = 0;
5220 }
5221}
5222
5223/* Cleanup all Tx queues */
5224static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
5225{
5226 struct mvpp2_tx_queue *txq;
5227 int queue;
5228 u32 val;
5229
5230 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
5231
5232 /* Reset Tx ports and delete Tx queues */
5233 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
5234 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5235
5236 for (queue = 0; queue < txq_number; queue++) {
5237 txq = port->txqs[queue];
5238 mvpp2_txq_clean(port, txq);
5239 mvpp2_txq_deinit(port, txq);
5240 }
5241
5242 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5243
5244 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
5245 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5246}
5247
5248/* Cleanup all Rx queues */
5249static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
5250{
5251 int queue;
5252
5253 for (queue = 0; queue < rxq_number; queue++)
5254 mvpp2_rxq_deinit(port, port->rxqs[queue]);
5255}
5256
5257/* Init all Rx queues for port */
5258static int mvpp2_setup_rxqs(struct mvpp2_port *port)
5259{
5260 int queue, err;
5261
5262 for (queue = 0; queue < rxq_number; queue++) {
5263 err = mvpp2_rxq_init(port, port->rxqs[queue]);
5264 if (err)
5265 goto err_cleanup;
5266 }
5267 return 0;
5268
5269err_cleanup:
5270 mvpp2_cleanup_rxqs(port);
5271 return err;
5272}
5273
5274/* Init all tx queues for port */
5275static int mvpp2_setup_txqs(struct mvpp2_port *port)
5276{
5277 struct mvpp2_tx_queue *txq;
5278 int queue, err;
5279
5280 for (queue = 0; queue < txq_number; queue++) {
5281 txq = port->txqs[queue];
5282 err = mvpp2_txq_init(port, txq);
5283 if (err)
5284 goto err_cleanup;
5285 }
5286
Marcin Wojtas3f518502014-07-10 16:52:13 -03005287 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5288 return 0;
5289
5290err_cleanup:
5291 mvpp2_cleanup_txqs(port);
5292 return err;
5293}
5294
5295/* The callback for per-port interrupt */
5296static irqreturn_t mvpp2_isr(int irq, void *dev_id)
5297{
5298 struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
5299
5300 mvpp2_interrupts_disable(port);
5301
5302 napi_schedule(&port->napi);
5303
5304 return IRQ_HANDLED;
5305}
5306
5307/* Adjust link */
5308static void mvpp2_link_event(struct net_device *dev)
5309{
5310 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02005311 struct phy_device *phydev = dev->phydev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005312 int status_change = 0;
5313 u32 val;
5314
5315 if (phydev->link) {
5316 if ((port->speed != phydev->speed) ||
5317 (port->duplex != phydev->duplex)) {
5318 u32 val;
5319
5320 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5321 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
5322 MVPP2_GMAC_CONFIG_GMII_SPEED |
5323 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
5324 MVPP2_GMAC_AN_SPEED_EN |
5325 MVPP2_GMAC_AN_DUPLEX_EN);
5326
5327 if (phydev->duplex)
5328 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
5329
5330 if (phydev->speed == SPEED_1000)
5331 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
Thomas Petazzoni2add5112014-07-27 23:21:35 +02005332 else if (phydev->speed == SPEED_100)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005333 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
5334
5335 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5336
5337 port->duplex = phydev->duplex;
5338 port->speed = phydev->speed;
5339 }
5340 }
5341
5342 if (phydev->link != port->link) {
5343 if (!phydev->link) {
5344 port->duplex = -1;
5345 port->speed = 0;
5346 }
5347
5348 port->link = phydev->link;
5349 status_change = 1;
5350 }
5351
5352 if (status_change) {
5353 if (phydev->link) {
5354 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5355 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
5356 MVPP2_GMAC_FORCE_LINK_DOWN);
5357 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5358 mvpp2_egress_enable(port);
5359 mvpp2_ingress_enable(port);
5360 } else {
5361 mvpp2_ingress_disable(port);
5362 mvpp2_egress_disable(port);
5363 }
5364 phy_print_status(phydev);
5365 }
5366}
5367
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005368static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
5369{
5370 ktime_t interval;
5371
5372 if (!port_pcpu->timer_scheduled) {
5373 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01005374 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005375 hrtimer_start(&port_pcpu->tx_done_timer, interval,
5376 HRTIMER_MODE_REL_PINNED);
5377 }
5378}
5379
5380static void mvpp2_tx_proc_cb(unsigned long data)
5381{
5382 struct net_device *dev = (struct net_device *)data;
5383 struct mvpp2_port *port = netdev_priv(dev);
5384 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5385 unsigned int tx_todo, cause;
5386
5387 if (!netif_running(dev))
5388 return;
5389 port_pcpu->timer_scheduled = false;
5390
5391 /* Process all the Tx queues */
5392 cause = (1 << txq_number) - 1;
5393 tx_todo = mvpp2_tx_done(port, cause);
5394
5395 /* Set the timer in case not all the packets were processed */
5396 if (tx_todo)
5397 mvpp2_timer_set(port_pcpu);
5398}
5399
5400static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
5401{
5402 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
5403 struct mvpp2_port_pcpu,
5404 tx_done_timer);
5405
5406 tasklet_schedule(&port_pcpu->tx_done_tasklet);
5407
5408 return HRTIMER_NORESTART;
5409}
5410
Marcin Wojtas3f518502014-07-10 16:52:13 -03005411/* Main RX/TX processing routines */
5412
5413/* Display more error info */
5414static void mvpp2_rx_error(struct mvpp2_port *port,
5415 struct mvpp2_rx_desc *rx_desc)
5416{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005417 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5418 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005419
5420 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
5421 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005422 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
5423 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005424 break;
5425 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005426 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
5427 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005428 break;
5429 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005430 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
5431 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005432 break;
5433 }
5434}
5435
5436/* Handle RX checksum offload */
5437static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
5438 struct sk_buff *skb)
5439{
5440 if (((status & MVPP2_RXD_L3_IP4) &&
5441 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
5442 (status & MVPP2_RXD_L3_IP6))
5443 if (((status & MVPP2_RXD_L4_UDP) ||
5444 (status & MVPP2_RXD_L4_TCP)) &&
5445 (status & MVPP2_RXD_L4_CSUM_OK)) {
5446 skb->csum = 0;
5447 skb->ip_summed = CHECKSUM_UNNECESSARY;
5448 return;
5449 }
5450
5451 skb->ip_summed = CHECKSUM_NONE;
5452}
5453
5454/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
5455static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01005456 struct mvpp2_bm_pool *bm_pool, u32 bm)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005457{
Thomas Petazzoni20396132017-03-07 16:53:00 +01005458 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005459 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005460 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005461
Marcin Wojtas3f518502014-07-10 16:52:13 -03005462 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005463 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
5464 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005465 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005466 return -ENOMEM;
5467
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005468 mvpp2_pool_refill(port, bm, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01005469
Marcin Wojtas3f518502014-07-10 16:52:13 -03005470 return 0;
5471}
5472
5473/* Handle tx checksum */
5474static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
5475{
5476 if (skb->ip_summed == CHECKSUM_PARTIAL) {
5477 int ip_hdr_len = 0;
5478 u8 l4_proto;
5479
5480 if (skb->protocol == htons(ETH_P_IP)) {
5481 struct iphdr *ip4h = ip_hdr(skb);
5482
5483 /* Calculate IPv4 checksum and L4 checksum */
5484 ip_hdr_len = ip4h->ihl;
5485 l4_proto = ip4h->protocol;
5486 } else if (skb->protocol == htons(ETH_P_IPV6)) {
5487 struct ipv6hdr *ip6h = ipv6_hdr(skb);
5488
5489 /* Read l4_protocol from one of IPv6 extra headers */
5490 if (skb_network_header_len(skb) > 0)
5491 ip_hdr_len = (skb_network_header_len(skb) >> 2);
5492 l4_proto = ip6h->nexthdr;
5493 } else {
5494 return MVPP2_TXD_L4_CSUM_NOT;
5495 }
5496
5497 return mvpp2_txq_desc_csum(skb_network_offset(skb),
5498 skb->protocol, ip_hdr_len, l4_proto);
5499 }
5500
5501 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
5502}
5503
Marcin Wojtas3f518502014-07-10 16:52:13 -03005504/* Main rx processing */
5505static int mvpp2_rx(struct mvpp2_port *port, int rx_todo,
5506 struct mvpp2_rx_queue *rxq)
5507{
5508 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01005509 int rx_received;
5510 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005511 u32 rcvd_pkts = 0;
5512 u32 rcvd_bytes = 0;
5513
5514 /* Get number of received packets and clamp the to-do */
5515 rx_received = mvpp2_rxq_received(port, rxq->id);
5516 if (rx_todo > rx_received)
5517 rx_todo = rx_received;
5518
Marcin Wojtasb5015852015-12-03 15:20:51 +01005519 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005520 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
5521 struct mvpp2_bm_pool *bm_pool;
5522 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005523 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005524 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005525 phys_addr_t phys_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005526 u32 bm, rx_status;
5527 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005528 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005529
Marcin Wojtasb5015852015-12-03 15:20:51 +01005530 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005531 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
5532 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
5533 rx_bytes -= MVPP2_MH_SIZE;
5534 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
5535 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
5536 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005537
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005538 bm = mvpp2_bm_cookie_build(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005539 pool = mvpp2_bm_cookie_pool_get(bm);
5540 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03005541
5542 /* In case of an error, release the requested buffer pointer
5543 * to the Buffer Manager. This request process is controlled
5544 * by the hardware, and the information about the buffer is
5545 * comprised by the RX descriptor.
5546 */
5547 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Markus Elfring8a524882017-04-17 10:52:02 +02005548err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03005549 dev->stats.rx_errors++;
5550 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01005551 /* Return the buffer to the pool */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005552 mvpp2_pool_refill(port, bm, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005553 continue;
5554 }
5555
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005556 if (bm_pool->frag_size > PAGE_SIZE)
5557 frag_size = 0;
5558 else
5559 frag_size = bm_pool->frag_size;
5560
5561 skb = build_skb(data, frag_size);
5562 if (!skb) {
5563 netdev_warn(port->dev, "skb build failed\n");
5564 goto err_drop_frame;
5565 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005566
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01005567 err = mvpp2_rx_refill(port, bm_pool, bm);
Marcin Wojtasb5015852015-12-03 15:20:51 +01005568 if (err) {
5569 netdev_err(port->dev, "failed to refill BM pools\n");
5570 goto err_drop_frame;
5571 }
5572
Thomas Petazzoni20396132017-03-07 16:53:00 +01005573 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01005574 bm_pool->buf_size, DMA_FROM_DEVICE);
5575
Marcin Wojtas3f518502014-07-10 16:52:13 -03005576 rcvd_pkts++;
5577 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005578
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005579 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005580 skb_put(skb, rx_bytes);
5581 skb->protocol = eth_type_trans(skb, dev);
5582 mvpp2_rx_csum(port, rx_status, skb);
5583
5584 napi_gro_receive(&port->napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005585 }
5586
5587 if (rcvd_pkts) {
5588 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
5589
5590 u64_stats_update_begin(&stats->syncp);
5591 stats->rx_packets += rcvd_pkts;
5592 stats->rx_bytes += rcvd_bytes;
5593 u64_stats_update_end(&stats->syncp);
5594 }
5595
5596 /* Update Rx queue management counters */
5597 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01005598 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005599
5600 return rx_todo;
5601}
5602
5603static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005604tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005605 struct mvpp2_tx_desc *desc)
5606{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005607 dma_addr_t buf_dma_addr =
5608 mvpp2_txdesc_dma_addr_get(port, desc);
5609 size_t buf_sz =
5610 mvpp2_txdesc_size_get(port, desc);
5611 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
5612 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005613 mvpp2_txq_desc_put(txq);
5614}
5615
5616/* Handle tx fragmentation processing */
5617static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
5618 struct mvpp2_tx_queue *aggr_txq,
5619 struct mvpp2_tx_queue *txq)
5620{
5621 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
5622 struct mvpp2_tx_desc *tx_desc;
5623 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005624 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005625
5626 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
5627 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5628 void *addr = page_address(frag->page.p) + frag->page_offset;
5629
5630 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005631 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
5632 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005633
Thomas Petazzoni20396132017-03-07 16:53:00 +01005634 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005635 frag->size,
5636 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01005637 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005638 mvpp2_txq_desc_put(txq);
Markus Elfring32bae632017-04-17 11:36:34 +02005639 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005640 }
5641
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005642 mvpp2_txdesc_offset_set(port, tx_desc,
5643 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
5644 mvpp2_txdesc_dma_addr_set(port, tx_desc,
5645 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005646
5647 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
5648 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005649 mvpp2_txdesc_cmd_set(port, tx_desc,
5650 MVPP2_TXD_L_DESC);
5651 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005652 } else {
5653 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005654 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
5655 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005656 }
5657 }
5658
5659 return 0;
Markus Elfring32bae632017-04-17 11:36:34 +02005660cleanup:
Marcin Wojtas3f518502014-07-10 16:52:13 -03005661 /* Release all descriptors that were used to map fragments of
5662 * this packet, as well as the corresponding DMA mappings
5663 */
5664 for (i = i - 1; i >= 0; i--) {
5665 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005666 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005667 }
5668
5669 return -ENOMEM;
5670}
5671
5672/* Main tx processing */
5673static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
5674{
5675 struct mvpp2_port *port = netdev_priv(dev);
5676 struct mvpp2_tx_queue *txq, *aggr_txq;
5677 struct mvpp2_txq_pcpu *txq_pcpu;
5678 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005679 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005680 int frags = 0;
5681 u16 txq_id;
5682 u32 tx_cmd;
5683
5684 txq_id = skb_get_queue_mapping(skb);
5685 txq = port->txqs[txq_id];
5686 txq_pcpu = this_cpu_ptr(txq->pcpu);
5687 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
5688
5689 frags = skb_shinfo(skb)->nr_frags + 1;
5690
5691 /* Check number of available descriptors */
5692 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
5693 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
5694 txq_pcpu, frags)) {
5695 frags = 0;
5696 goto out;
5697 }
5698
5699 /* Get a descriptor for the first part of the packet */
5700 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005701 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
5702 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005703
Thomas Petazzoni20396132017-03-07 16:53:00 +01005704 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005705 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01005706 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005707 mvpp2_txq_desc_put(txq);
5708 frags = 0;
5709 goto out;
5710 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005711
5712 mvpp2_txdesc_offset_set(port, tx_desc,
5713 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
5714 mvpp2_txdesc_dma_addr_set(port, tx_desc,
5715 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005716
5717 tx_cmd = mvpp2_skb_tx_csum(port, skb);
5718
5719 if (frags == 1) {
5720 /* First and Last descriptor */
5721 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005722 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
5723 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005724 } else {
5725 /* First but not Last */
5726 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005727 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
5728 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005729
5730 /* Continue with other skb fragments */
5731 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005732 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005733 frags = 0;
5734 goto out;
5735 }
5736 }
5737
5738 txq_pcpu->reserved_num -= frags;
5739 txq_pcpu->count += frags;
5740 aggr_txq->count += frags;
5741
5742 /* Enable transmit */
5743 wmb();
5744 mvpp2_aggr_txq_pend_desc_add(port, frags);
5745
5746 if (txq_pcpu->size - txq_pcpu->count < MAX_SKB_FRAGS + 1) {
5747 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
5748
5749 netif_tx_stop_queue(nq);
5750 }
5751out:
5752 if (frags > 0) {
5753 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
5754
5755 u64_stats_update_begin(&stats->syncp);
5756 stats->tx_packets++;
5757 stats->tx_bytes += skb->len;
5758 u64_stats_update_end(&stats->syncp);
5759 } else {
5760 dev->stats.tx_dropped++;
5761 dev_kfree_skb_any(skb);
5762 }
5763
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005764 /* Finalize TX processing */
5765 if (txq_pcpu->count >= txq->done_pkts_coal)
5766 mvpp2_txq_done(port, txq, txq_pcpu);
5767
5768 /* Set the timer in case not all frags were processed */
5769 if (txq_pcpu->count <= frags && txq_pcpu->count > 0) {
5770 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5771
5772 mvpp2_timer_set(port_pcpu);
5773 }
5774
Marcin Wojtas3f518502014-07-10 16:52:13 -03005775 return NETDEV_TX_OK;
5776}
5777
5778static inline void mvpp2_cause_error(struct net_device *dev, int cause)
5779{
5780 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
5781 netdev_err(dev, "FCS error\n");
5782 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
5783 netdev_err(dev, "rx fifo overrun error\n");
5784 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
5785 netdev_err(dev, "tx fifo underrun error\n");
5786}
5787
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005788static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005789{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005790 u32 cause_rx_tx, cause_rx, cause_misc;
5791 int rx_done = 0;
5792 struct mvpp2_port *port = netdev_priv(napi->dev);
Thomas Petazzonia7868412017-03-07 16:53:13 +01005793 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005794
5795 /* Rx/Tx cause register
5796 *
5797 * Bits 0-15: each bit indicates received packets on the Rx queue
5798 * (bit 0 is for Rx queue 0).
5799 *
5800 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
5801 * (bit 16 is for Tx queue 0).
5802 *
5803 * Each CPU has its own Rx/Tx cause register
5804 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005805 cause_rx_tx = mvpp2_percpu_read(port->priv, cpu,
5806 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005807 cause_rx_tx &= ~MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005808 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
5809
5810 if (cause_misc) {
5811 mvpp2_cause_error(port->dev, cause_misc);
5812
5813 /* Clear the cause register */
5814 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01005815 mvpp2_percpu_write(port->priv, cpu,
5816 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
5817 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005818 }
5819
Marcin Wojtas3f518502014-07-10 16:52:13 -03005820 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
5821
5822 /* Process RX packets */
5823 cause_rx |= port->pending_cause_rx;
5824 while (cause_rx && budget > 0) {
5825 int count;
5826 struct mvpp2_rx_queue *rxq;
5827
5828 rxq = mvpp2_get_rx_queue(port, cause_rx);
5829 if (!rxq)
5830 break;
5831
5832 count = mvpp2_rx(port, budget, rxq);
5833 rx_done += count;
5834 budget -= count;
5835 if (budget > 0) {
5836 /* Clear the bit associated to this Rx queue
5837 * so that next iteration will continue from
5838 * the next Rx queue.
5839 */
5840 cause_rx &= ~(1 << rxq->logic_rxq);
5841 }
5842 }
5843
5844 if (budget > 0) {
5845 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08005846 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005847
5848 mvpp2_interrupts_enable(port);
5849 }
5850 port->pending_cause_rx = cause_rx;
5851 return rx_done;
5852}
5853
5854/* Set hw internals when starting port */
5855static void mvpp2_start_dev(struct mvpp2_port *port)
5856{
Philippe Reynes8e072692016-06-28 00:08:11 +02005857 struct net_device *ndev = port->dev;
5858
Marcin Wojtas3f518502014-07-10 16:52:13 -03005859 mvpp2_gmac_max_rx_size_set(port);
5860 mvpp2_txp_max_tx_size_set(port);
5861
5862 napi_enable(&port->napi);
5863
5864 /* Enable interrupts on all CPUs */
5865 mvpp2_interrupts_enable(port);
5866
5867 mvpp2_port_enable(port);
Philippe Reynes8e072692016-06-28 00:08:11 +02005868 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005869 netif_tx_start_all_queues(port->dev);
5870}
5871
5872/* Set hw internals when stopping port */
5873static void mvpp2_stop_dev(struct mvpp2_port *port)
5874{
Philippe Reynes8e072692016-06-28 00:08:11 +02005875 struct net_device *ndev = port->dev;
5876
Marcin Wojtas3f518502014-07-10 16:52:13 -03005877 /* Stop new packets from arriving to RXQs */
5878 mvpp2_ingress_disable(port);
5879
5880 mdelay(10);
5881
5882 /* Disable interrupts on all CPUs */
5883 mvpp2_interrupts_disable(port);
5884
5885 napi_disable(&port->napi);
5886
5887 netif_carrier_off(port->dev);
5888 netif_tx_stop_all_queues(port->dev);
5889
5890 mvpp2_egress_disable(port);
5891 mvpp2_port_disable(port);
Philippe Reynes8e072692016-06-28 00:08:11 +02005892 phy_stop(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005893}
5894
Marcin Wojtas3f518502014-07-10 16:52:13 -03005895static int mvpp2_check_ringparam_valid(struct net_device *dev,
5896 struct ethtool_ringparam *ring)
5897{
5898 u16 new_rx_pending = ring->rx_pending;
5899 u16 new_tx_pending = ring->tx_pending;
5900
5901 if (ring->rx_pending == 0 || ring->tx_pending == 0)
5902 return -EINVAL;
5903
5904 if (ring->rx_pending > MVPP2_MAX_RXD)
5905 new_rx_pending = MVPP2_MAX_RXD;
5906 else if (!IS_ALIGNED(ring->rx_pending, 16))
5907 new_rx_pending = ALIGN(ring->rx_pending, 16);
5908
5909 if (ring->tx_pending > MVPP2_MAX_TXD)
5910 new_tx_pending = MVPP2_MAX_TXD;
5911 else if (!IS_ALIGNED(ring->tx_pending, 32))
5912 new_tx_pending = ALIGN(ring->tx_pending, 32);
5913
5914 if (ring->rx_pending != new_rx_pending) {
5915 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
5916 ring->rx_pending, new_rx_pending);
5917 ring->rx_pending = new_rx_pending;
5918 }
5919
5920 if (ring->tx_pending != new_tx_pending) {
5921 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
5922 ring->tx_pending, new_tx_pending);
5923 ring->tx_pending = new_tx_pending;
5924 }
5925
5926 return 0;
5927}
5928
Thomas Petazzoni26975822017-03-07 16:53:14 +01005929static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005930{
5931 u32 mac_addr_l, mac_addr_m, mac_addr_h;
5932
5933 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
5934 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
5935 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
5936 addr[0] = (mac_addr_h >> 24) & 0xFF;
5937 addr[1] = (mac_addr_h >> 16) & 0xFF;
5938 addr[2] = (mac_addr_h >> 8) & 0xFF;
5939 addr[3] = mac_addr_h & 0xFF;
5940 addr[4] = mac_addr_m & 0xFF;
5941 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
5942}
5943
5944static int mvpp2_phy_connect(struct mvpp2_port *port)
5945{
5946 struct phy_device *phy_dev;
5947
5948 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
5949 port->phy_interface);
5950 if (!phy_dev) {
5951 netdev_err(port->dev, "cannot connect to phy\n");
5952 return -ENODEV;
5953 }
5954 phy_dev->supported &= PHY_GBIT_FEATURES;
5955 phy_dev->advertising = phy_dev->supported;
5956
Marcin Wojtas3f518502014-07-10 16:52:13 -03005957 port->link = 0;
5958 port->duplex = 0;
5959 port->speed = 0;
5960
5961 return 0;
5962}
5963
5964static void mvpp2_phy_disconnect(struct mvpp2_port *port)
5965{
Philippe Reynes8e072692016-06-28 00:08:11 +02005966 struct net_device *ndev = port->dev;
5967
5968 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005969}
5970
5971static int mvpp2_open(struct net_device *dev)
5972{
5973 struct mvpp2_port *port = netdev_priv(dev);
5974 unsigned char mac_bcast[ETH_ALEN] = {
5975 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5976 int err;
5977
5978 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
5979 if (err) {
5980 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
5981 return err;
5982 }
5983 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
5984 dev->dev_addr, true);
5985 if (err) {
5986 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
5987 return err;
5988 }
5989 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
5990 if (err) {
5991 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
5992 return err;
5993 }
5994 err = mvpp2_prs_def_flow(port);
5995 if (err) {
5996 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
5997 return err;
5998 }
5999
6000 /* Allocate the Rx/Tx queues */
6001 err = mvpp2_setup_rxqs(port);
6002 if (err) {
6003 netdev_err(port->dev, "cannot allocate Rx queues\n");
6004 return err;
6005 }
6006
6007 err = mvpp2_setup_txqs(port);
6008 if (err) {
6009 netdev_err(port->dev, "cannot allocate Tx queues\n");
6010 goto err_cleanup_rxqs;
6011 }
6012
6013 err = request_irq(port->irq, mvpp2_isr, 0, dev->name, port);
6014 if (err) {
6015 netdev_err(port->dev, "cannot request IRQ %d\n", port->irq);
6016 goto err_cleanup_txqs;
6017 }
6018
6019 /* In default link is down */
6020 netif_carrier_off(port->dev);
6021
6022 err = mvpp2_phy_connect(port);
6023 if (err < 0)
6024 goto err_free_irq;
6025
6026 /* Unmask interrupts on all CPUs */
6027 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
6028
6029 mvpp2_start_dev(port);
6030
6031 return 0;
6032
6033err_free_irq:
6034 free_irq(port->irq, port);
6035err_cleanup_txqs:
6036 mvpp2_cleanup_txqs(port);
6037err_cleanup_rxqs:
6038 mvpp2_cleanup_rxqs(port);
6039 return err;
6040}
6041
6042static int mvpp2_stop(struct net_device *dev)
6043{
6044 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006045 struct mvpp2_port_pcpu *port_pcpu;
6046 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006047
6048 mvpp2_stop_dev(port);
6049 mvpp2_phy_disconnect(port);
6050
6051 /* Mask interrupts on all CPUs */
6052 on_each_cpu(mvpp2_interrupts_mask, port, 1);
6053
6054 free_irq(port->irq, port);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006055 for_each_present_cpu(cpu) {
6056 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
6057
6058 hrtimer_cancel(&port_pcpu->tx_done_timer);
6059 port_pcpu->timer_scheduled = false;
6060 tasklet_kill(&port_pcpu->tx_done_tasklet);
6061 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006062 mvpp2_cleanup_rxqs(port);
6063 mvpp2_cleanup_txqs(port);
6064
6065 return 0;
6066}
6067
6068static void mvpp2_set_rx_mode(struct net_device *dev)
6069{
6070 struct mvpp2_port *port = netdev_priv(dev);
6071 struct mvpp2 *priv = port->priv;
6072 struct netdev_hw_addr *ha;
6073 int id = port->id;
6074 bool allmulti = dev->flags & IFF_ALLMULTI;
6075
6076 mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
6077 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
6078 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
6079
6080 /* Remove all port->id's mcast enries */
6081 mvpp2_prs_mcast_del_all(priv, id);
6082
6083 if (allmulti && !netdev_mc_empty(dev)) {
6084 netdev_for_each_mc_addr(ha, dev)
6085 mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
6086 }
6087}
6088
6089static int mvpp2_set_mac_address(struct net_device *dev, void *p)
6090{
6091 struct mvpp2_port *port = netdev_priv(dev);
6092 const struct sockaddr *addr = p;
6093 int err;
6094
6095 if (!is_valid_ether_addr(addr->sa_data)) {
6096 err = -EADDRNOTAVAIL;
Markus Elfringc1175542017-04-17 11:10:47 +02006097 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006098 }
6099
6100 if (!netif_running(dev)) {
6101 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6102 if (!err)
6103 return 0;
6104 /* Reconfigure parser to accept the original MAC address */
6105 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6106 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006107 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006108 }
6109
6110 mvpp2_stop_dev(port);
6111
6112 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6113 if (!err)
6114 goto out_start;
6115
6116 /* Reconfigure parser accept the original MAC address */
6117 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6118 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006119 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006120out_start:
6121 mvpp2_start_dev(port);
6122 mvpp2_egress_enable(port);
6123 mvpp2_ingress_enable(port);
6124 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02006125log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02006126 netdev_err(dev, "failed to change MAC address\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006127 return err;
6128}
6129
6130static int mvpp2_change_mtu(struct net_device *dev, int mtu)
6131{
6132 struct mvpp2_port *port = netdev_priv(dev);
6133 int err;
6134
Jarod Wilson57779872016-10-17 15:54:06 -04006135 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
6136 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
6137 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
6138 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006139 }
6140
6141 if (!netif_running(dev)) {
6142 err = mvpp2_bm_update_mtu(dev, mtu);
6143 if (!err) {
6144 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6145 return 0;
6146 }
6147
6148 /* Reconfigure BM to the original MTU */
6149 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6150 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006151 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006152 }
6153
6154 mvpp2_stop_dev(port);
6155
6156 err = mvpp2_bm_update_mtu(dev, mtu);
6157 if (!err) {
6158 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6159 goto out_start;
6160 }
6161
6162 /* Reconfigure BM to the original MTU */
6163 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6164 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006165 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006166
6167out_start:
6168 mvpp2_start_dev(port);
6169 mvpp2_egress_enable(port);
6170 mvpp2_ingress_enable(port);
6171
6172 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02006173log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02006174 netdev_err(dev, "failed to change MTU\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006175 return err;
6176}
6177
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006178static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03006179mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6180{
6181 struct mvpp2_port *port = netdev_priv(dev);
6182 unsigned int start;
6183 int cpu;
6184
6185 for_each_possible_cpu(cpu) {
6186 struct mvpp2_pcpu_stats *cpu_stats;
6187 u64 rx_packets;
6188 u64 rx_bytes;
6189 u64 tx_packets;
6190 u64 tx_bytes;
6191
6192 cpu_stats = per_cpu_ptr(port->stats, cpu);
6193 do {
6194 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
6195 rx_packets = cpu_stats->rx_packets;
6196 rx_bytes = cpu_stats->rx_bytes;
6197 tx_packets = cpu_stats->tx_packets;
6198 tx_bytes = cpu_stats->tx_bytes;
6199 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
6200
6201 stats->rx_packets += rx_packets;
6202 stats->rx_bytes += rx_bytes;
6203 stats->tx_packets += tx_packets;
6204 stats->tx_bytes += tx_bytes;
6205 }
6206
6207 stats->rx_errors = dev->stats.rx_errors;
6208 stats->rx_dropped = dev->stats.rx_dropped;
6209 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006210}
6211
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006212static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6213{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006214 int ret;
6215
Philippe Reynes8e072692016-06-28 00:08:11 +02006216 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006217 return -ENOTSUPP;
6218
Philippe Reynes8e072692016-06-28 00:08:11 +02006219 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006220 if (!ret)
6221 mvpp2_link_event(dev);
6222
6223 return ret;
6224}
6225
Marcin Wojtas3f518502014-07-10 16:52:13 -03006226/* Ethtool methods */
6227
Marcin Wojtas3f518502014-07-10 16:52:13 -03006228/* Set interrupt coalescing for ethtools */
6229static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
6230 struct ethtool_coalesce *c)
6231{
6232 struct mvpp2_port *port = netdev_priv(dev);
6233 int queue;
6234
6235 for (queue = 0; queue < rxq_number; queue++) {
6236 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6237
6238 rxq->time_coal = c->rx_coalesce_usecs;
6239 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01006240 mvpp2_rx_pkts_coal_set(port, rxq);
6241 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006242 }
6243
6244 for (queue = 0; queue < txq_number; queue++) {
6245 struct mvpp2_tx_queue *txq = port->txqs[queue];
6246
6247 txq->done_pkts_coal = c->tx_max_coalesced_frames;
6248 }
6249
Marcin Wojtas3f518502014-07-10 16:52:13 -03006250 return 0;
6251}
6252
6253/* get coalescing for ethtools */
6254static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
6255 struct ethtool_coalesce *c)
6256{
6257 struct mvpp2_port *port = netdev_priv(dev);
6258
6259 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
6260 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
6261 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
6262 return 0;
6263}
6264
6265static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
6266 struct ethtool_drvinfo *drvinfo)
6267{
6268 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
6269 sizeof(drvinfo->driver));
6270 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
6271 sizeof(drvinfo->version));
6272 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
6273 sizeof(drvinfo->bus_info));
6274}
6275
6276static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
6277 struct ethtool_ringparam *ring)
6278{
6279 struct mvpp2_port *port = netdev_priv(dev);
6280
6281 ring->rx_max_pending = MVPP2_MAX_RXD;
6282 ring->tx_max_pending = MVPP2_MAX_TXD;
6283 ring->rx_pending = port->rx_ring_size;
6284 ring->tx_pending = port->tx_ring_size;
6285}
6286
6287static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
6288 struct ethtool_ringparam *ring)
6289{
6290 struct mvpp2_port *port = netdev_priv(dev);
6291 u16 prev_rx_ring_size = port->rx_ring_size;
6292 u16 prev_tx_ring_size = port->tx_ring_size;
6293 int err;
6294
6295 err = mvpp2_check_ringparam_valid(dev, ring);
6296 if (err)
6297 return err;
6298
6299 if (!netif_running(dev)) {
6300 port->rx_ring_size = ring->rx_pending;
6301 port->tx_ring_size = ring->tx_pending;
6302 return 0;
6303 }
6304
6305 /* The interface is running, so we have to force a
6306 * reallocation of the queues
6307 */
6308 mvpp2_stop_dev(port);
6309 mvpp2_cleanup_rxqs(port);
6310 mvpp2_cleanup_txqs(port);
6311
6312 port->rx_ring_size = ring->rx_pending;
6313 port->tx_ring_size = ring->tx_pending;
6314
6315 err = mvpp2_setup_rxqs(port);
6316 if (err) {
6317 /* Reallocate Rx queues with the original ring size */
6318 port->rx_ring_size = prev_rx_ring_size;
6319 ring->rx_pending = prev_rx_ring_size;
6320 err = mvpp2_setup_rxqs(port);
6321 if (err)
6322 goto err_out;
6323 }
6324 err = mvpp2_setup_txqs(port);
6325 if (err) {
6326 /* Reallocate Tx queues with the original ring size */
6327 port->tx_ring_size = prev_tx_ring_size;
6328 ring->tx_pending = prev_tx_ring_size;
6329 err = mvpp2_setup_txqs(port);
6330 if (err)
6331 goto err_clean_rxqs;
6332 }
6333
6334 mvpp2_start_dev(port);
6335 mvpp2_egress_enable(port);
6336 mvpp2_ingress_enable(port);
6337
6338 return 0;
6339
6340err_clean_rxqs:
6341 mvpp2_cleanup_rxqs(port);
6342err_out:
Markus Elfringdfd42402017-04-17 11:20:41 +02006343 netdev_err(dev, "failed to change ring parameters");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006344 return err;
6345}
6346
6347/* Device ops */
6348
6349static const struct net_device_ops mvpp2_netdev_ops = {
6350 .ndo_open = mvpp2_open,
6351 .ndo_stop = mvpp2_stop,
6352 .ndo_start_xmit = mvpp2_tx,
6353 .ndo_set_rx_mode = mvpp2_set_rx_mode,
6354 .ndo_set_mac_address = mvpp2_set_mac_address,
6355 .ndo_change_mtu = mvpp2_change_mtu,
6356 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006357 .ndo_do_ioctl = mvpp2_ioctl,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006358};
6359
6360static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08006361 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006362 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006363 .set_coalesce = mvpp2_ethtool_set_coalesce,
6364 .get_coalesce = mvpp2_ethtool_get_coalesce,
6365 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
6366 .get_ringparam = mvpp2_ethtool_get_ringparam,
6367 .set_ringparam = mvpp2_ethtool_set_ringparam,
Philippe Reynesfb773e92016-06-28 00:08:12 +02006368 .get_link_ksettings = phy_ethtool_get_link_ksettings,
6369 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006370};
6371
Marcin Wojtas3f518502014-07-10 16:52:13 -03006372/* Initialize port HW */
6373static int mvpp2_port_init(struct mvpp2_port *port)
6374{
6375 struct device *dev = port->dev->dev.parent;
6376 struct mvpp2 *priv = port->priv;
6377 struct mvpp2_txq_pcpu *txq_pcpu;
6378 int queue, cpu, err;
6379
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006380 if (port->first_rxq + rxq_number >
6381 MVPP2_MAX_PORTS * priv->max_port_rxqs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006382 return -EINVAL;
6383
6384 /* Disable port */
6385 mvpp2_egress_disable(port);
6386 mvpp2_port_disable(port);
6387
6388 port->txqs = devm_kcalloc(dev, txq_number, sizeof(*port->txqs),
6389 GFP_KERNEL);
6390 if (!port->txqs)
6391 return -ENOMEM;
6392
6393 /* Associate physical Tx queues to this port and initialize.
6394 * The mapping is predefined.
6395 */
6396 for (queue = 0; queue < txq_number; queue++) {
6397 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
6398 struct mvpp2_tx_queue *txq;
6399
6400 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01006401 if (!txq) {
6402 err = -ENOMEM;
6403 goto err_free_percpu;
6404 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006405
6406 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
6407 if (!txq->pcpu) {
6408 err = -ENOMEM;
6409 goto err_free_percpu;
6410 }
6411
6412 txq->id = queue_phy_id;
6413 txq->log_id = queue;
6414 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
6415 for_each_present_cpu(cpu) {
6416 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
6417 txq_pcpu->cpu = cpu;
6418 }
6419
6420 port->txqs[queue] = txq;
6421 }
6422
6423 port->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*port->rxqs),
6424 GFP_KERNEL);
6425 if (!port->rxqs) {
6426 err = -ENOMEM;
6427 goto err_free_percpu;
6428 }
6429
6430 /* Allocate and initialize Rx queue for this port */
6431 for (queue = 0; queue < rxq_number; queue++) {
6432 struct mvpp2_rx_queue *rxq;
6433
6434 /* Map physical Rx queue to port's logical Rx queue */
6435 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08006436 if (!rxq) {
6437 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006438 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08006439 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006440 /* Map this Rx queue to a physical queue */
6441 rxq->id = port->first_rxq + queue;
6442 rxq->port = port->id;
6443 rxq->logic_rxq = queue;
6444
6445 port->rxqs[queue] = rxq;
6446 }
6447
6448 /* Configure Rx queue group interrupt for this port */
Thomas Petazzonia73fef12017-03-07 16:53:16 +01006449 if (priv->hw_version == MVPP21) {
6450 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
6451 rxq_number);
6452 } else {
6453 u32 val;
6454
6455 val = (port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET);
6456 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
6457
6458 val = (rxq_number << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET);
6459 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
6460 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006461
6462 /* Create Rx descriptor rings */
6463 for (queue = 0; queue < rxq_number; queue++) {
6464 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6465
6466 rxq->size = port->rx_ring_size;
6467 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
6468 rxq->time_coal = MVPP2_RX_COAL_USEC;
6469 }
6470
6471 mvpp2_ingress_disable(port);
6472
6473 /* Port default configuration */
6474 mvpp2_defaults_set(port);
6475
6476 /* Port's classifier configuration */
6477 mvpp2_cls_oversize_rxq_set(port);
6478 mvpp2_cls_port_config(port);
6479
6480 /* Provide an initial Rx packet size */
6481 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
6482
6483 /* Initialize pools for swf */
6484 err = mvpp2_swf_bm_pool_init(port);
6485 if (err)
6486 goto err_free_percpu;
6487
6488 return 0;
6489
6490err_free_percpu:
6491 for (queue = 0; queue < txq_number; queue++) {
6492 if (!port->txqs[queue])
6493 continue;
6494 free_percpu(port->txqs[queue]->pcpu);
6495 }
6496 return err;
6497}
6498
6499/* Ports initialization */
6500static int mvpp2_port_probe(struct platform_device *pdev,
6501 struct device_node *port_node,
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006502 struct mvpp2 *priv)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006503{
6504 struct device_node *phy_node;
6505 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006506 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006507 struct net_device *dev;
6508 struct resource *res;
6509 const char *dt_mac_addr;
6510 const char *mac_from;
6511 char hw_mac_addr[ETH_ALEN];
6512 u32 id;
6513 int features;
6514 int phy_mode;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006515 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006516
Markus Elfring03bfffd2017-04-17 08:55:42 +02006517 dev = alloc_etherdev_mqs(sizeof(*port), txq_number, rxq_number);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006518 if (!dev)
6519 return -ENOMEM;
6520
6521 phy_node = of_parse_phandle(port_node, "phy", 0);
6522 if (!phy_node) {
6523 dev_err(&pdev->dev, "missing phy\n");
6524 err = -ENODEV;
6525 goto err_free_netdev;
6526 }
6527
6528 phy_mode = of_get_phy_mode(port_node);
6529 if (phy_mode < 0) {
6530 dev_err(&pdev->dev, "incorrect phy mode\n");
6531 err = phy_mode;
6532 goto err_free_netdev;
6533 }
6534
6535 if (of_property_read_u32(port_node, "port-id", &id)) {
6536 err = -EINVAL;
6537 dev_err(&pdev->dev, "missing port-id value\n");
6538 goto err_free_netdev;
6539 }
6540
6541 dev->tx_queue_len = MVPP2_MAX_TXD;
6542 dev->watchdog_timeo = 5 * HZ;
6543 dev->netdev_ops = &mvpp2_netdev_ops;
6544 dev->ethtool_ops = &mvpp2_eth_tool_ops;
6545
6546 port = netdev_priv(dev);
6547
6548 port->irq = irq_of_parse_and_map(port_node, 0);
6549 if (port->irq <= 0) {
6550 err = -EINVAL;
6551 goto err_free_netdev;
6552 }
6553
6554 if (of_property_read_bool(port_node, "marvell,loopback"))
6555 port->flags |= MVPP2_F_LOOPBACK;
6556
6557 port->priv = priv;
6558 port->id = id;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006559 if (priv->hw_version == MVPP21)
6560 port->first_rxq = port->id * rxq_number;
6561 else
6562 port->first_rxq = port->id * priv->max_port_rxqs;
6563
Marcin Wojtas3f518502014-07-10 16:52:13 -03006564 port->phy_node = phy_node;
6565 port->phy_interface = phy_mode;
6566
Thomas Petazzonia7868412017-03-07 16:53:13 +01006567 if (priv->hw_version == MVPP21) {
6568 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
6569 port->base = devm_ioremap_resource(&pdev->dev, res);
6570 if (IS_ERR(port->base)) {
6571 err = PTR_ERR(port->base);
6572 goto err_free_irq;
6573 }
6574 } else {
6575 if (of_property_read_u32(port_node, "gop-port-id",
6576 &port->gop_id)) {
6577 err = -EINVAL;
6578 dev_err(&pdev->dev, "missing gop-port-id value\n");
6579 goto err_free_irq;
6580 }
6581
6582 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006583 }
6584
6585 /* Alloc per-cpu stats */
6586 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
6587 if (!port->stats) {
6588 err = -ENOMEM;
6589 goto err_free_irq;
6590 }
6591
6592 dt_mac_addr = of_get_mac_address(port_node);
6593 if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
6594 mac_from = "device tree";
6595 ether_addr_copy(dev->dev_addr, dt_mac_addr);
6596 } else {
Thomas Petazzoni26975822017-03-07 16:53:14 +01006597 if (priv->hw_version == MVPP21)
6598 mvpp21_get_mac_address(port, hw_mac_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006599 if (is_valid_ether_addr(hw_mac_addr)) {
6600 mac_from = "hardware";
6601 ether_addr_copy(dev->dev_addr, hw_mac_addr);
6602 } else {
6603 mac_from = "random";
6604 eth_hw_addr_random(dev);
6605 }
6606 }
6607
6608 port->tx_ring_size = MVPP2_MAX_TXD;
6609 port->rx_ring_size = MVPP2_MAX_RXD;
6610 port->dev = dev;
6611 SET_NETDEV_DEV(dev, &pdev->dev);
6612
6613 err = mvpp2_port_init(port);
6614 if (err < 0) {
6615 dev_err(&pdev->dev, "failed to init port %d\n", id);
6616 goto err_free_stats;
6617 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01006618
6619 mvpp2_port_mii_set(port);
6620 mvpp2_port_periodic_xon_disable(port);
6621
6622 if (priv->hw_version == MVPP21)
6623 mvpp2_port_fc_adv_enable(port);
6624
6625 mvpp2_port_reset(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006626
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006627 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
6628 if (!port->pcpu) {
6629 err = -ENOMEM;
6630 goto err_free_txq_pcpu;
6631 }
6632
6633 for_each_present_cpu(cpu) {
6634 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
6635
6636 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
6637 HRTIMER_MODE_REL_PINNED);
6638 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
6639 port_pcpu->timer_scheduled = false;
6640
6641 tasklet_init(&port_pcpu->tx_done_tasklet, mvpp2_tx_proc_cb,
6642 (unsigned long)dev);
6643 }
6644
Marcin Wojtas3f518502014-07-10 16:52:13 -03006645 netif_napi_add(dev, &port->napi, mvpp2_poll, NAPI_POLL_WEIGHT);
6646 features = NETIF_F_SG | NETIF_F_IP_CSUM;
6647 dev->features = features | NETIF_F_RXCSUM;
6648 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
6649 dev->vlan_features |= features;
6650
Jarod Wilson57779872016-10-17 15:54:06 -04006651 /* MTU range: 68 - 9676 */
6652 dev->min_mtu = ETH_MIN_MTU;
6653 /* 9676 == 9700 - 20 and rounding to 8 */
6654 dev->max_mtu = 9676;
6655
Marcin Wojtas3f518502014-07-10 16:52:13 -03006656 err = register_netdev(dev);
6657 if (err < 0) {
6658 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006659 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006660 }
6661 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
6662
Marcin Wojtas3f518502014-07-10 16:52:13 -03006663 priv->port_list[id] = port;
6664 return 0;
6665
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006666err_free_port_pcpu:
6667 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006668err_free_txq_pcpu:
6669 for (i = 0; i < txq_number; i++)
6670 free_percpu(port->txqs[i]->pcpu);
6671err_free_stats:
6672 free_percpu(port->stats);
6673err_free_irq:
6674 irq_dispose_mapping(port->irq);
6675err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08006676 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006677 free_netdev(dev);
6678 return err;
6679}
6680
6681/* Ports removal routine */
6682static void mvpp2_port_remove(struct mvpp2_port *port)
6683{
6684 int i;
6685
6686 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08006687 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006688 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006689 free_percpu(port->stats);
6690 for (i = 0; i < txq_number; i++)
6691 free_percpu(port->txqs[i]->pcpu);
6692 irq_dispose_mapping(port->irq);
6693 free_netdev(port->dev);
6694}
6695
6696/* Initialize decoding windows */
6697static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
6698 struct mvpp2 *priv)
6699{
6700 u32 win_enable;
6701 int i;
6702
6703 for (i = 0; i < 6; i++) {
6704 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
6705 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
6706
6707 if (i < 4)
6708 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
6709 }
6710
6711 win_enable = 0;
6712
6713 for (i = 0; i < dram->num_cs; i++) {
6714 const struct mbus_dram_window *cs = dram->cs + i;
6715
6716 mvpp2_write(priv, MVPP2_WIN_BASE(i),
6717 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
6718 dram->mbus_dram_target_id);
6719
6720 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
6721 (cs->size - 1) & 0xffff0000);
6722
6723 win_enable |= (1 << i);
6724 }
6725
6726 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
6727}
6728
6729/* Initialize Rx FIFO's */
6730static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
6731{
6732 int port;
6733
6734 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
6735 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
6736 MVPP2_RX_FIFO_PORT_DATA_SIZE);
6737 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
6738 MVPP2_RX_FIFO_PORT_ATTR_SIZE);
6739 }
6740
6741 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
6742 MVPP2_RX_FIFO_PORT_MIN_PKT);
6743 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
6744}
6745
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01006746static void mvpp2_axi_init(struct mvpp2 *priv)
6747{
6748 u32 val, rdval, wrval;
6749
6750 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
6751
6752 /* AXI Bridge Configuration */
6753
6754 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
6755 << MVPP22_AXI_ATTR_CACHE_OFFS;
6756 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6757 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
6758
6759 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
6760 << MVPP22_AXI_ATTR_CACHE_OFFS;
6761 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6762 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
6763
6764 /* BM */
6765 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
6766 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
6767
6768 /* Descriptors */
6769 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
6770 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
6771 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
6772 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
6773
6774 /* Buffer Data */
6775 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
6776 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
6777
6778 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
6779 << MVPP22_AXI_CODE_CACHE_OFFS;
6780 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
6781 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6782 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
6783 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
6784
6785 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
6786 << MVPP22_AXI_CODE_CACHE_OFFS;
6787 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6788 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6789
6790 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
6791
6792 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
6793 << MVPP22_AXI_CODE_CACHE_OFFS;
6794 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6795 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6796
6797 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
6798}
6799
Marcin Wojtas3f518502014-07-10 16:52:13 -03006800/* Initialize network controller common part HW */
6801static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
6802{
6803 const struct mbus_dram_target_info *dram_target_info;
6804 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03006805 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006806
6807 /* Checks for hardware constraints */
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006808 if (rxq_number % 4 || (rxq_number > priv->max_port_rxqs) ||
Marcin Wojtas3f518502014-07-10 16:52:13 -03006809 (txq_number > MVPP2_MAX_TXQ)) {
6810 dev_err(&pdev->dev, "invalid queue size parameter\n");
6811 return -EINVAL;
6812 }
6813
6814 /* MBUS windows configuration */
6815 dram_target_info = mv_mbus_dram_info();
6816 if (dram_target_info)
6817 mvpp2_conf_mbus_windows(dram_target_info, priv);
6818
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01006819 if (priv->hw_version == MVPP22)
6820 mvpp2_axi_init(priv);
6821
Marcin Wojtas08a23752014-07-21 13:48:12 -03006822 /* Disable HW PHY polling */
Thomas Petazzoni26975822017-03-07 16:53:14 +01006823 if (priv->hw_version == MVPP21) {
6824 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6825 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
6826 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6827 } else {
6828 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
6829 val &= ~MVPP22_SMI_POLLING_EN;
6830 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
6831 }
Marcin Wojtas08a23752014-07-21 13:48:12 -03006832
Marcin Wojtas3f518502014-07-10 16:52:13 -03006833 /* Allocate and initialize aggregated TXQs */
6834 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
Markus Elfringd7ce3ce2017-04-17 08:48:23 +02006835 sizeof(*priv->aggr_txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03006836 GFP_KERNEL);
6837 if (!priv->aggr_txqs)
6838 return -ENOMEM;
6839
6840 for_each_present_cpu(i) {
6841 priv->aggr_txqs[i].id = i;
6842 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
6843 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i],
6844 MVPP2_AGGR_TXQ_SIZE, i, priv);
6845 if (err < 0)
6846 return err;
6847 }
6848
6849 /* Rx Fifo Init */
6850 mvpp2_rx_fifo_init(priv);
6851
6852 /* Reset Rx queue group interrupt configuration */
Thomas Petazzonia73fef12017-03-07 16:53:16 +01006853 for (i = 0; i < MVPP2_MAX_PORTS; i++) {
6854 if (priv->hw_version == MVPP21) {
6855 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(i),
6856 rxq_number);
6857 continue;
6858 } else {
6859 u32 val;
6860
6861 val = (i << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET);
6862 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
6863
6864 val = (rxq_number << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET);
6865 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
6866 }
6867 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006868
Thomas Petazzoni26975822017-03-07 16:53:14 +01006869 if (priv->hw_version == MVPP21)
6870 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
6871 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006872
6873 /* Allow cache snoop when transmiting packets */
6874 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
6875
6876 /* Buffer Manager initialization */
6877 err = mvpp2_bm_init(pdev, priv);
6878 if (err < 0)
6879 return err;
6880
6881 /* Parser default initialization */
6882 err = mvpp2_prs_default_init(pdev, priv);
6883 if (err < 0)
6884 return err;
6885
6886 /* Classifier default initialization */
6887 mvpp2_cls_init(priv);
6888
6889 return 0;
6890}
6891
6892static int mvpp2_probe(struct platform_device *pdev)
6893{
6894 struct device_node *dn = pdev->dev.of_node;
6895 struct device_node *port_node;
6896 struct mvpp2 *priv;
6897 struct resource *res;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006898 void __iomem *base;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006899 int port_count, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006900 int err;
6901
Markus Elfring0b92e592017-04-17 08:38:32 +02006902 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006903 if (!priv)
6904 return -ENOMEM;
6905
Thomas Petazzonifaca9242017-03-07 16:53:06 +01006906 priv->hw_version =
6907 (unsigned long)of_device_get_match_data(&pdev->dev);
6908
Marcin Wojtas3f518502014-07-10 16:52:13 -03006909 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01006910 base = devm_ioremap_resource(&pdev->dev, res);
6911 if (IS_ERR(base))
6912 return PTR_ERR(base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006913
Thomas Petazzonia7868412017-03-07 16:53:13 +01006914 if (priv->hw_version == MVPP21) {
6915 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
6916 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
6917 if (IS_ERR(priv->lms_base))
6918 return PTR_ERR(priv->lms_base);
6919 } else {
6920 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
6921 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
6922 if (IS_ERR(priv->iface_base))
6923 return PTR_ERR(priv->iface_base);
6924 }
6925
6926 for_each_present_cpu(cpu) {
6927 u32 addr_space_sz;
6928
6929 addr_space_sz = (priv->hw_version == MVPP21 ?
6930 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
6931 priv->cpu_base[cpu] = base + cpu * addr_space_sz;
6932 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006933
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006934 if (priv->hw_version == MVPP21)
6935 priv->max_port_rxqs = 8;
6936 else
6937 priv->max_port_rxqs = 32;
6938
Marcin Wojtas3f518502014-07-10 16:52:13 -03006939 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
6940 if (IS_ERR(priv->pp_clk))
6941 return PTR_ERR(priv->pp_clk);
6942 err = clk_prepare_enable(priv->pp_clk);
6943 if (err < 0)
6944 return err;
6945
6946 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
6947 if (IS_ERR(priv->gop_clk)) {
6948 err = PTR_ERR(priv->gop_clk);
6949 goto err_pp_clk;
6950 }
6951 err = clk_prepare_enable(priv->gop_clk);
6952 if (err < 0)
6953 goto err_pp_clk;
6954
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006955 if (priv->hw_version == MVPP22) {
6956 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
6957 if (IS_ERR(priv->mg_clk)) {
6958 err = PTR_ERR(priv->mg_clk);
6959 goto err_gop_clk;
6960 }
6961
6962 err = clk_prepare_enable(priv->mg_clk);
6963 if (err < 0)
6964 goto err_gop_clk;
6965 }
6966
Marcin Wojtas3f518502014-07-10 16:52:13 -03006967 /* Get system's tclk rate */
6968 priv->tclk = clk_get_rate(priv->pp_clk);
6969
Thomas Petazzoni2067e0a2017-03-07 16:53:19 +01006970 if (priv->hw_version == MVPP22) {
6971 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
6972 if (err)
6973 goto err_mg_clk;
6974 /* Sadly, the BM pools all share the same register to
6975 * store the high 32 bits of their address. So they
6976 * must all have the same high 32 bits, which forces
6977 * us to restrict coherent memory to DMA_BIT_MASK(32).
6978 */
6979 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
6980 if (err)
6981 goto err_mg_clk;
6982 }
6983
Marcin Wojtas3f518502014-07-10 16:52:13 -03006984 /* Initialize network controller */
6985 err = mvpp2_init(pdev, priv);
6986 if (err < 0) {
6987 dev_err(&pdev->dev, "failed to initialize controller\n");
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006988 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006989 }
6990
6991 port_count = of_get_available_child_count(dn);
6992 if (port_count == 0) {
6993 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08006994 err = -ENODEV;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006995 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006996 }
6997
6998 priv->port_list = devm_kcalloc(&pdev->dev, port_count,
Markus Elfring0b92e592017-04-17 08:38:32 +02006999 sizeof(*priv->port_list),
7000 GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007001 if (!priv->port_list) {
7002 err = -ENOMEM;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007003 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007004 }
7005
7006 /* Initialize ports */
Marcin Wojtas3f518502014-07-10 16:52:13 -03007007 for_each_available_child_of_node(dn, port_node) {
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007008 err = mvpp2_port_probe(pdev, port_node, priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007009 if (err < 0)
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007010 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007011 }
7012
7013 platform_set_drvdata(pdev, priv);
7014 return 0;
7015
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007016err_mg_clk:
7017 if (priv->hw_version == MVPP22)
7018 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007019err_gop_clk:
7020 clk_disable_unprepare(priv->gop_clk);
7021err_pp_clk:
7022 clk_disable_unprepare(priv->pp_clk);
7023 return err;
7024}
7025
7026static int mvpp2_remove(struct platform_device *pdev)
7027{
7028 struct mvpp2 *priv = platform_get_drvdata(pdev);
7029 struct device_node *dn = pdev->dev.of_node;
7030 struct device_node *port_node;
7031 int i = 0;
7032
7033 for_each_available_child_of_node(dn, port_node) {
7034 if (priv->port_list[i])
7035 mvpp2_port_remove(priv->port_list[i]);
7036 i++;
7037 }
7038
7039 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
7040 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
7041
7042 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
7043 }
7044
7045 for_each_present_cpu(i) {
7046 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
7047
7048 dma_free_coherent(&pdev->dev,
7049 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
7050 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01007051 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007052 }
7053
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007054 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007055 clk_disable_unprepare(priv->pp_clk);
7056 clk_disable_unprepare(priv->gop_clk);
7057
7058 return 0;
7059}
7060
7061static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01007062 {
7063 .compatible = "marvell,armada-375-pp2",
7064 .data = (void *)MVPP21,
7065 },
Thomas Petazzonifc5e1552017-03-07 16:53:20 +01007066 {
7067 .compatible = "marvell,armada-7k-pp22",
7068 .data = (void *)MVPP22,
7069 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03007070 { }
7071};
7072MODULE_DEVICE_TABLE(of, mvpp2_match);
7073
7074static struct platform_driver mvpp2_driver = {
7075 .probe = mvpp2_probe,
7076 .remove = mvpp2_remove,
7077 .driver = {
7078 .name = MVPP2_DRIVER_NAME,
7079 .of_match_table = mvpp2_match,
7080 },
7081};
7082
7083module_platform_driver(mvpp2_driver);
7084
7085MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
7086MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03007087MODULE_LICENSE("GPL v2");