blob: aad763c8b61909c3dce300156da3d75105ffc1b6 [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 Petazzonia704bb52017-06-10 23:18:22 +02003728 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01003729
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 }
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003749
3750 put_cpu();
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003751}
3752
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003753/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01003754static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
3755 struct mvpp2_bm_pool *bm_pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003756{
3757 int i;
3758
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003759 for (i = 0; i < bm_pool->buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01003760 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003761 phys_addr_t buf_phys_addr;
3762 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003763
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003764 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
3765 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01003766
Thomas Petazzoni20396132017-03-07 16:53:00 +01003767 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01003768 bm_pool->buf_size, DMA_FROM_DEVICE);
3769
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003770 data = (void *)phys_to_virt(buf_phys_addr);
3771 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003772 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003773
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003774 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003775 }
3776
3777 /* Update BM driver with number of buffers removed from pool */
3778 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003779}
3780
3781/* Cleanup pool */
3782static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
3783 struct mvpp2 *priv,
3784 struct mvpp2_bm_pool *bm_pool)
3785{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003786 u32 val;
3787
Marcin Wojtas4229d502015-12-03 15:20:50 +01003788 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03003789 if (bm_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003790 WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
3791 return 0;
3792 }
3793
3794 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3795 val |= MVPP2_BM_STOP_MASK;
3796 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3797
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003798 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003799 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003800 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003801 return 0;
3802}
3803
3804static int mvpp2_bm_pools_init(struct platform_device *pdev,
3805 struct mvpp2 *priv)
3806{
3807 int i, err, size;
3808 struct mvpp2_bm_pool *bm_pool;
3809
3810 /* Create all pools with maximum size */
3811 size = MVPP2_BM_POOL_SIZE_MAX;
3812 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3813 bm_pool = &priv->bm_pools[i];
3814 bm_pool->id = i;
3815 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
3816 if (err)
3817 goto err_unroll_pools;
3818 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
3819 }
3820 return 0;
3821
3822err_unroll_pools:
3823 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
3824 for (i = i - 1; i >= 0; i--)
3825 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
3826 return err;
3827}
3828
3829static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
3830{
3831 int i, err;
3832
3833 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3834 /* Mask BM all interrupts */
3835 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
3836 /* Clear BM cause register */
3837 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
3838 }
3839
3840 /* Allocate and initialize BM pools */
3841 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
Markus Elfring81f915e2017-04-17 09:06:33 +02003842 sizeof(*priv->bm_pools), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003843 if (!priv->bm_pools)
3844 return -ENOMEM;
3845
3846 err = mvpp2_bm_pools_init(pdev, priv);
3847 if (err < 0)
3848 return err;
3849 return 0;
3850}
3851
3852/* Attach long pool to rxq */
3853static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
3854 int lrxq, int long_pool)
3855{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003856 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003857 int prxq;
3858
3859 /* Get queue physical ID */
3860 prxq = port->rxqs[lrxq]->id;
3861
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003862 if (port->priv->hw_version == MVPP21)
3863 mask = MVPP21_RXQ_POOL_LONG_MASK;
3864 else
3865 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003866
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003867 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3868 val &= ~mask;
3869 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003870 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3871}
3872
3873/* Attach short pool to rxq */
3874static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
3875 int lrxq, int short_pool)
3876{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003877 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003878 int prxq;
3879
3880 /* Get queue physical ID */
3881 prxq = port->rxqs[lrxq]->id;
3882
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003883 if (port->priv->hw_version == MVPP21)
3884 mask = MVPP21_RXQ_POOL_SHORT_MASK;
3885 else
3886 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003887
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003888 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3889 val &= ~mask;
3890 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003891 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3892}
3893
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003894static void *mvpp2_buf_alloc(struct mvpp2_port *port,
3895 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003896 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003897 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003898 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003899{
Thomas Petazzoni20396132017-03-07 16:53:00 +01003900 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003901 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003902
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003903 data = mvpp2_frag_alloc(bm_pool);
3904 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003905 return NULL;
3906
Thomas Petazzoni20396132017-03-07 16:53:00 +01003907 dma_addr = dma_map_single(port->dev->dev.parent, data,
3908 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
3909 DMA_FROM_DEVICE);
3910 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003911 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003912 return NULL;
3913 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01003914 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003915 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003916
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003917 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003918}
3919
Marcin Wojtas3f518502014-07-10 16:52:13 -03003920/* Release buffer to BM */
3921static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003922 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003923 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003924{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003925 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01003926
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003927 if (port->priv->hw_version == MVPP22) {
3928 u32 val = 0;
3929
3930 if (sizeof(dma_addr_t) == 8)
3931 val |= upper_32_bits(buf_dma_addr) &
3932 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
3933
3934 if (sizeof(phys_addr_t) == 8)
3935 val |= (upper_32_bits(buf_phys_addr)
3936 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
3937 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
3938
Thomas Petazzonia7868412017-03-07 16:53:13 +01003939 mvpp2_percpu_write(port->priv, cpu,
3940 MVPP22_BM_ADDR_HIGH_RLS_REG, val);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003941 }
3942
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003943 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
3944 * returned in the "cookie" field of the RX
3945 * descriptor. Instead of storing the virtual address, we
3946 * store the physical address
3947 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01003948 mvpp2_percpu_write(port->priv, cpu,
3949 MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
3950 mvpp2_percpu_write(port->priv, cpu,
3951 MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003952
3953 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03003954}
3955
Marcin Wojtas3f518502014-07-10 16:52:13 -03003956/* Refill BM pool */
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02003957static void mvpp2_pool_refill(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003958 dma_addr_t dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003959 phys_addr_t phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003960{
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003961 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003962}
3963
3964/* Allocate buffers for the pool */
3965static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
3966 struct mvpp2_bm_pool *bm_pool, int buf_num)
3967{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003968 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01003969 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003970 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003971 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003972
3973 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
3974 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
3975
3976 if (buf_num < 0 ||
3977 (buf_num + bm_pool->buf_num > bm_pool->size)) {
3978 netdev_err(port->dev,
3979 "cannot allocate %d buffers for pool %d\n",
3980 buf_num, bm_pool->id);
3981 return 0;
3982 }
3983
Marcin Wojtas3f518502014-07-10 16:52:13 -03003984 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003985 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
3986 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003987 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003988 break;
3989
Thomas Petazzoni20396132017-03-07 16:53:00 +01003990 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003991 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003992 }
3993
3994 /* Update BM driver with number of buffers added to pool */
3995 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003996
3997 netdev_dbg(port->dev,
3998 "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
3999 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4000 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
4001
4002 netdev_dbg(port->dev,
4003 "%s pool %d: %d of %d buffers added\n",
4004 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4005 bm_pool->id, i, buf_num);
4006 return i;
4007}
4008
4009/* Notify the driver that BM pool is being used as specific type and return the
4010 * pool pointer on success
4011 */
4012static struct mvpp2_bm_pool *
4013mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
4014 int pkt_size)
4015{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004016 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4017 int num;
4018
4019 if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
4020 netdev_err(port->dev, "mixing pool types is forbidden\n");
4021 return NULL;
4022 }
4023
Marcin Wojtas3f518502014-07-10 16:52:13 -03004024 if (new_pool->type == MVPP2_BM_FREE)
4025 new_pool->type = type;
4026
4027 /* Allocate buffers in case BM pool is used as long pool, but packet
4028 * size doesn't match MTU or BM pool hasn't being used yet
4029 */
4030 if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
4031 (new_pool->pkt_size == 0)) {
4032 int pkts_num;
4033
4034 /* Set default buffer number or free all the buffers in case
4035 * the pool is not empty
4036 */
4037 pkts_num = new_pool->buf_num;
4038 if (pkts_num == 0)
4039 pkts_num = type == MVPP2_BM_SWF_LONG ?
4040 MVPP2_BM_LONG_BUF_NUM :
4041 MVPP2_BM_SHORT_BUF_NUM;
4042 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01004043 mvpp2_bm_bufs_free(port->dev->dev.parent,
4044 port->priv, new_pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004045
4046 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004047 new_pool->frag_size =
4048 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4049 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004050
4051 /* Allocate buffers for this pool */
4052 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4053 if (num != pkts_num) {
4054 WARN(1, "pool %d: %d of %d allocated\n",
4055 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004056 return NULL;
4057 }
4058 }
4059
4060 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4061 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4062
Marcin Wojtas3f518502014-07-10 16:52:13 -03004063 return new_pool;
4064}
4065
4066/* Initialize pools for swf */
4067static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4068{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004069 int rxq;
4070
4071 if (!port->pool_long) {
4072 port->pool_long =
4073 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
4074 MVPP2_BM_SWF_LONG,
4075 port->pkt_size);
4076 if (!port->pool_long)
4077 return -ENOMEM;
4078
Marcin Wojtas3f518502014-07-10 16:52:13 -03004079 port->pool_long->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004080
4081 for (rxq = 0; rxq < rxq_number; rxq++)
4082 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4083 }
4084
4085 if (!port->pool_short) {
4086 port->pool_short =
4087 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
4088 MVPP2_BM_SWF_SHORT,
4089 MVPP2_BM_SHORT_PKT_SIZE);
4090 if (!port->pool_short)
4091 return -ENOMEM;
4092
Marcin Wojtas3f518502014-07-10 16:52:13 -03004093 port->pool_short->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004094
4095 for (rxq = 0; rxq < rxq_number; rxq++)
4096 mvpp2_rxq_short_pool_set(port, rxq,
4097 port->pool_short->id);
4098 }
4099
4100 return 0;
4101}
4102
4103static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4104{
4105 struct mvpp2_port *port = netdev_priv(dev);
4106 struct mvpp2_bm_pool *port_pool = port->pool_long;
4107 int num, pkts_num = port_pool->buf_num;
4108 int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
4109
4110 /* Update BM pool with new buffer size */
Marcin Wojtas4229d502015-12-03 15:20:50 +01004111 mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03004112 if (port_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004113 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
4114 return -EIO;
4115 }
4116
4117 port_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004118 port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4119 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004120 num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4121 if (num != pkts_num) {
4122 WARN(1, "pool %d: %d of %d allocated\n",
4123 port_pool->id, num, pkts_num);
4124 return -EIO;
4125 }
4126
4127 mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
4128 MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
4129 dev->mtu = mtu;
4130 netdev_update_features(dev);
4131 return 0;
4132}
4133
4134static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4135{
4136 int cpu, cpu_mask = 0;
4137
4138 for_each_present_cpu(cpu)
4139 cpu_mask |= 1 << cpu;
4140 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4141 MVPP2_ISR_ENABLE_INTERRUPT(cpu_mask));
4142}
4143
4144static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4145{
4146 int cpu, cpu_mask = 0;
4147
4148 for_each_present_cpu(cpu)
4149 cpu_mask |= 1 << cpu;
4150 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4151 MVPP2_ISR_DISABLE_INTERRUPT(cpu_mask));
4152}
4153
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004154/* Mask the current CPU's Rx/Tx interrupts
4155 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4156 * using smp_processor_id() is OK.
4157 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004158static void mvpp2_interrupts_mask(void *arg)
4159{
4160 struct mvpp2_port *port = arg;
4161
Thomas Petazzonia7868412017-03-07 16:53:13 +01004162 mvpp2_percpu_write(port->priv, smp_processor_id(),
4163 MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004164}
4165
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004166/* Unmask the current CPU's Rx/Tx interrupts.
4167 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4168 * using smp_processor_id() is OK.
4169 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004170static void mvpp2_interrupts_unmask(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),
4176 (MVPP2_CAUSE_MISC_SUM_MASK |
4177 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004178}
4179
4180/* Port configuration routines */
4181
Thomas Petazzoni26975822017-03-07 16:53:14 +01004182static void mvpp22_port_mii_set(struct mvpp2_port *port)
4183{
4184 u32 val;
4185
Thomas Petazzoni26975822017-03-07 16:53:14 +01004186 /* Only GOP port 0 has an XLG MAC */
4187 if (port->gop_id == 0) {
4188 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
4189 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
Antoine Ténart725757a2017-06-12 16:01:39 +02004190
4191 if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4192 port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4193 val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
4194 else
4195 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4196
Thomas Petazzoni26975822017-03-07 16:53:14 +01004197 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
4198 }
4199
4200 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4201 if (port->phy_interface == PHY_INTERFACE_MODE_RGMII)
4202 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4203 else
4204 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4205 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
4206 val |= MVPP22_CTRL4_SYNC_BYPASS;
4207 val |= MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4208 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4209}
4210
Marcin Wojtas3f518502014-07-10 16:52:13 -03004211static void mvpp2_port_mii_set(struct mvpp2_port *port)
4212{
Marcin Wojtas08a23752014-07-21 13:48:12 -03004213 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004214
Thomas Petazzoni26975822017-03-07 16:53:14 +01004215 if (port->priv->hw_version == MVPP22)
4216 mvpp22_port_mii_set(port);
4217
Marcin Wojtas08a23752014-07-21 13:48:12 -03004218 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004219
Marcin Wojtas08a23752014-07-21 13:48:12 -03004220 switch (port->phy_interface) {
4221 case PHY_INTERFACE_MODE_SGMII:
4222 val |= MVPP2_GMAC_INBAND_AN_MASK;
4223 break;
4224 case PHY_INTERFACE_MODE_RGMII:
4225 val |= MVPP2_GMAC_PORT_RGMII_MASK;
4226 default:
4227 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
4228 }
4229
4230 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4231}
4232
4233static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
4234{
4235 u32 val;
4236
4237 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4238 val |= MVPP2_GMAC_FC_ADV_EN;
4239 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004240}
4241
4242static void mvpp2_port_enable(struct mvpp2_port *port)
4243{
4244 u32 val;
4245
Antoine Ténart725757a2017-06-12 16:01:39 +02004246 /* Only GOP port 0 has an XLG MAC */
4247 if (port->gop_id == 0 &&
4248 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4249 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4250 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4251 val |= MVPP22_XLG_CTRL0_PORT_EN |
4252 MVPP22_XLG_CTRL0_MAC_RESET_DIS;
4253 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
4254 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4255 } else {
4256 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4257 val |= MVPP2_GMAC_PORT_EN_MASK;
4258 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
4259 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4260 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004261}
4262
4263static void mvpp2_port_disable(struct mvpp2_port *port)
4264{
4265 u32 val;
4266
Antoine Ténart725757a2017-06-12 16:01:39 +02004267 /* Only GOP port 0 has an XLG MAC */
4268 if (port->gop_id == 0 &&
4269 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4270 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4271 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4272 val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
4273 MVPP22_XLG_CTRL0_MAC_RESET_DIS);
4274 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4275 } else {
4276 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4277 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
4278 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4279 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004280}
4281
4282/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
4283static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
4284{
4285 u32 val;
4286
4287 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
4288 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
4289 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4290}
4291
4292/* Configure loopback port */
4293static void mvpp2_port_loopback_set(struct mvpp2_port *port)
4294{
4295 u32 val;
4296
4297 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4298
4299 if (port->speed == 1000)
4300 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
4301 else
4302 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
4303
4304 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4305 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
4306 else
4307 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
4308
4309 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4310}
4311
4312static void mvpp2_port_reset(struct mvpp2_port *port)
4313{
4314 u32 val;
4315
4316 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4317 ~MVPP2_GMAC_PORT_RESET_MASK;
4318 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4319
4320 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4321 MVPP2_GMAC_PORT_RESET_MASK)
4322 continue;
4323}
4324
4325/* Change maximum receive size of the port */
4326static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
4327{
4328 u32 val;
4329
4330 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4331 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
4332 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
4333 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
4334 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4335}
4336
4337/* Set defaults to the MVPP2 port */
4338static void mvpp2_defaults_set(struct mvpp2_port *port)
4339{
4340 int tx_port_num, val, queue, ptxq, lrxq;
4341
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004342 if (port->priv->hw_version == MVPP21) {
4343 /* Configure port to loopback if needed */
4344 if (port->flags & MVPP2_F_LOOPBACK)
4345 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004346
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004347 /* Update TX FIFO MIN Threshold */
4348 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4349 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
4350 /* Min. TX threshold must be less than minimal packet length */
4351 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
4352 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4353 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004354
4355 /* Disable Legacy WRR, Disable EJP, Release from reset */
4356 tx_port_num = mvpp2_egress_port(port);
4357 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
4358 tx_port_num);
4359 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
4360
4361 /* Close bandwidth for all queues */
4362 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
4363 ptxq = mvpp2_txq_phys(port->id, queue);
4364 mvpp2_write(port->priv,
4365 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
4366 }
4367
4368 /* Set refill period to 1 usec, refill tokens
4369 * and bucket size to maximum
4370 */
4371 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
4372 port->priv->tclk / USEC_PER_SEC);
4373 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
4374 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
4375 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
4376 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
4377 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
4378 val = MVPP2_TXP_TOKEN_SIZE_MAX;
4379 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4380
4381 /* Set MaximumLowLatencyPacketSize value to 256 */
4382 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
4383 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
4384 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
4385
4386 /* Enable Rx cache snoop */
4387 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
4388 queue = port->rxqs[lrxq]->id;
4389 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4390 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
4391 MVPP2_SNOOP_BUF_HDR_MASK;
4392 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4393 }
4394
4395 /* At default, mask all interrupts to all present cpus */
4396 mvpp2_interrupts_disable(port);
4397}
4398
4399/* Enable/disable receiving packets */
4400static void mvpp2_ingress_enable(struct mvpp2_port *port)
4401{
4402 u32 val;
4403 int lrxq, queue;
4404
4405 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
4406 queue = port->rxqs[lrxq]->id;
4407 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4408 val &= ~MVPP2_RXQ_DISABLE_MASK;
4409 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4410 }
4411}
4412
4413static void mvpp2_ingress_disable(struct mvpp2_port *port)
4414{
4415 u32 val;
4416 int lrxq, queue;
4417
4418 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
4419 queue = port->rxqs[lrxq]->id;
4420 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4421 val |= MVPP2_RXQ_DISABLE_MASK;
4422 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4423 }
4424}
4425
4426/* Enable transmit via physical egress queue
4427 * - HW starts take descriptors from DRAM
4428 */
4429static void mvpp2_egress_enable(struct mvpp2_port *port)
4430{
4431 u32 qmap;
4432 int queue;
4433 int tx_port_num = mvpp2_egress_port(port);
4434
4435 /* Enable all initialized TXs. */
4436 qmap = 0;
4437 for (queue = 0; queue < txq_number; queue++) {
4438 struct mvpp2_tx_queue *txq = port->txqs[queue];
4439
Markus Elfringdbbb2f02017-04-17 14:07:52 +02004440 if (txq->descs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004441 qmap |= (1 << queue);
4442 }
4443
4444 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4445 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
4446}
4447
4448/* Disable transmit via physical egress queue
4449 * - HW doesn't take descriptors from DRAM
4450 */
4451static void mvpp2_egress_disable(struct mvpp2_port *port)
4452{
4453 u32 reg_data;
4454 int delay;
4455 int tx_port_num = mvpp2_egress_port(port);
4456
4457 /* Issue stop command for active channels only */
4458 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4459 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
4460 MVPP2_TXP_SCHED_ENQ_MASK;
4461 if (reg_data != 0)
4462 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
4463 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
4464
4465 /* Wait for all Tx activity to terminate. */
4466 delay = 0;
4467 do {
4468 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
4469 netdev_warn(port->dev,
4470 "Tx stop timed out, status=0x%08x\n",
4471 reg_data);
4472 break;
4473 }
4474 mdelay(1);
4475 delay++;
4476
4477 /* Check port TX Command register that all
4478 * Tx queues are stopped
4479 */
4480 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
4481 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
4482}
4483
4484/* Rx descriptors helper methods */
4485
4486/* Get number of Rx descriptors occupied by received packets */
4487static inline int
4488mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
4489{
4490 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
4491
4492 return val & MVPP2_RXQ_OCCUPIED_MASK;
4493}
4494
4495/* Update Rx queue status with the number of occupied and available
4496 * Rx descriptor slots.
4497 */
4498static inline void
4499mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
4500 int used_count, int free_count)
4501{
4502 /* Decrement the number of used descriptors and increment count
4503 * increment the number of free descriptors.
4504 */
4505 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
4506
4507 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
4508}
4509
4510/* Get pointer to next RX descriptor to be processed by SW */
4511static inline struct mvpp2_rx_desc *
4512mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
4513{
4514 int rx_desc = rxq->next_desc_to_proc;
4515
4516 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
4517 prefetch(rxq->descs + rxq->next_desc_to_proc);
4518 return rxq->descs + rx_desc;
4519}
4520
4521/* Set rx queue offset */
4522static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
4523 int prxq, int offset)
4524{
4525 u32 val;
4526
4527 /* Convert offset from bytes to units of 32 bytes */
4528 offset = offset >> 5;
4529
4530 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4531 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
4532
4533 /* Offset is in */
4534 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
4535 MVPP2_RXQ_PACKET_OFFSET_MASK);
4536
4537 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4538}
4539
Marcin Wojtas3f518502014-07-10 16:52:13 -03004540/* Tx descriptors helper methods */
4541
Marcin Wojtas3f518502014-07-10 16:52:13 -03004542/* Get pointer to next Tx descriptor to be processed (send) by HW */
4543static struct mvpp2_tx_desc *
4544mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
4545{
4546 int tx_desc = txq->next_desc_to_proc;
4547
4548 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
4549 return txq->descs + tx_desc;
4550}
4551
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004552/* Update HW with number of aggregated Tx descriptors to be sent
4553 *
4554 * Called only from mvpp2_tx(), so migration is disabled, using
4555 * smp_processor_id() is OK.
4556 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004557static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
4558{
4559 /* aggregated access - relevant TXQ number is written in TX desc */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004560 mvpp2_percpu_write(port->priv, smp_processor_id(),
4561 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004562}
4563
4564
4565/* Check if there are enough free descriptors in aggregated txq.
4566 * If not, update the number of occupied descriptors and repeat the check.
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004567 *
4568 * Called only from mvpp2_tx(), so migration is disabled, using
4569 * smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03004570 */
4571static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
4572 struct mvpp2_tx_queue *aggr_txq, int num)
4573{
4574 if ((aggr_txq->count + num) > aggr_txq->size) {
4575 /* Update number of occupied aggregated Tx descriptors */
4576 int cpu = smp_processor_id();
4577 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
4578
4579 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
4580 }
4581
4582 if ((aggr_txq->count + num) > aggr_txq->size)
4583 return -ENOMEM;
4584
4585 return 0;
4586}
4587
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004588/* Reserved Tx descriptors allocation request
4589 *
4590 * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
4591 * only by mvpp2_tx(), so migration is disabled, using
4592 * smp_processor_id() is OK.
4593 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004594static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
4595 struct mvpp2_tx_queue *txq, int num)
4596{
4597 u32 val;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004598 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004599
4600 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004601 mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004602
Thomas Petazzonia7868412017-03-07 16:53:13 +01004603 val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004604
4605 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
4606}
4607
4608/* Check if there are enough reserved descriptors for transmission.
4609 * If not, request chunk of reserved descriptors and check again.
4610 */
4611static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
4612 struct mvpp2_tx_queue *txq,
4613 struct mvpp2_txq_pcpu *txq_pcpu,
4614 int num)
4615{
4616 int req, cpu, desc_count;
4617
4618 if (txq_pcpu->reserved_num >= num)
4619 return 0;
4620
4621 /* Not enough descriptors reserved! Update the reserved descriptor
4622 * count and check again.
4623 */
4624
4625 desc_count = 0;
4626 /* Compute total of used descriptors */
4627 for_each_present_cpu(cpu) {
4628 struct mvpp2_txq_pcpu *txq_pcpu_aux;
4629
4630 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
4631 desc_count += txq_pcpu_aux->count;
4632 desc_count += txq_pcpu_aux->reserved_num;
4633 }
4634
4635 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
4636 desc_count += req;
4637
4638 if (desc_count >
4639 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
4640 return -ENOMEM;
4641
4642 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
4643
4644 /* OK, the descriptor cound has been updated: check again. */
4645 if (txq_pcpu->reserved_num < num)
4646 return -ENOMEM;
4647 return 0;
4648}
4649
4650/* Release the last allocated Tx descriptor. Useful to handle DMA
4651 * mapping failures in the Tx path.
4652 */
4653static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
4654{
4655 if (txq->next_desc_to_proc == 0)
4656 txq->next_desc_to_proc = txq->last_desc - 1;
4657 else
4658 txq->next_desc_to_proc--;
4659}
4660
4661/* Set Tx descriptors fields relevant for CSUM calculation */
4662static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
4663 int ip_hdr_len, int l4_proto)
4664{
4665 u32 command;
4666
4667 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
4668 * G_L4_chk, L4_type required only for checksum calculation
4669 */
4670 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
4671 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
4672 command |= MVPP2_TXD_IP_CSUM_DISABLE;
4673
4674 if (l3_proto == swab16(ETH_P_IP)) {
4675 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
4676 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
4677 } else {
4678 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
4679 }
4680
4681 if (l4_proto == IPPROTO_TCP) {
4682 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
4683 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
4684 } else if (l4_proto == IPPROTO_UDP) {
4685 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
4686 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
4687 } else {
4688 command |= MVPP2_TXD_L4_CSUM_NOT;
4689 }
4690
4691 return command;
4692}
4693
4694/* Get number of sent descriptors and decrement counter.
4695 * The number of sent descriptors is returned.
4696 * Per-CPU access
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004697 *
4698 * Called only from mvpp2_txq_done(), called from mvpp2_tx()
4699 * (migration disabled) and from the TX completion tasklet (migration
4700 * disabled) so using smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03004701 */
4702static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
4703 struct mvpp2_tx_queue *txq)
4704{
4705 u32 val;
4706
4707 /* Reading status reg resets transmitted descriptor counter */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004708 val = mvpp2_percpu_read(port->priv, smp_processor_id(),
4709 MVPP2_TXQ_SENT_REG(txq->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004710
4711 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
4712 MVPP2_TRANSMITTED_COUNT_OFFSET;
4713}
4714
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004715/* Called through on_each_cpu(), so runs on all CPUs, with migration
4716 * disabled, therefore using smp_processor_id() is OK.
4717 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004718static void mvpp2_txq_sent_counter_clear(void *arg)
4719{
4720 struct mvpp2_port *port = arg;
4721 int queue;
4722
4723 for (queue = 0; queue < txq_number; queue++) {
4724 int id = port->txqs[queue]->id;
4725
Thomas Petazzonia7868412017-03-07 16:53:13 +01004726 mvpp2_percpu_read(port->priv, smp_processor_id(),
4727 MVPP2_TXQ_SENT_REG(id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004728 }
4729}
4730
4731/* Set max sizes for Tx queues */
4732static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
4733{
4734 u32 val, size, mtu;
4735 int txq, tx_port_num;
4736
4737 mtu = port->pkt_size * 8;
4738 if (mtu > MVPP2_TXP_MTU_MAX)
4739 mtu = MVPP2_TXP_MTU_MAX;
4740
4741 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
4742 mtu = 3 * mtu;
4743
4744 /* Indirect access to registers */
4745 tx_port_num = mvpp2_egress_port(port);
4746 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4747
4748 /* Set MTU */
4749 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
4750 val &= ~MVPP2_TXP_MTU_MAX;
4751 val |= mtu;
4752 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
4753
4754 /* TXP token size and all TXQs token size must be larger that MTU */
4755 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
4756 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
4757 if (size < mtu) {
4758 size = mtu;
4759 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
4760 val |= size;
4761 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4762 }
4763
4764 for (txq = 0; txq < txq_number; txq++) {
4765 val = mvpp2_read(port->priv,
4766 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
4767 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
4768
4769 if (size < mtu) {
4770 size = mtu;
4771 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
4772 val |= size;
4773 mvpp2_write(port->priv,
4774 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
4775 val);
4776 }
4777 }
4778}
4779
4780/* Set the number of packets that will be received before Rx interrupt
4781 * will be generated by HW.
4782 */
4783static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004784 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004785{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004786 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004787
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01004788 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
4789 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004790
Thomas Petazzonia7868412017-03-07 16:53:13 +01004791 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
4792 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
4793 rxq->pkts_coal);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004794
4795 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004796}
4797
Thomas Petazzoniab426762017-02-21 11:28:04 +01004798static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
4799{
4800 u64 tmp = (u64)clk_hz * usec;
4801
4802 do_div(tmp, USEC_PER_SEC);
4803
4804 return tmp > U32_MAX ? U32_MAX : tmp;
4805}
4806
4807static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
4808{
4809 u64 tmp = (u64)cycles * USEC_PER_SEC;
4810
4811 do_div(tmp, clk_hz);
4812
4813 return tmp > U32_MAX ? U32_MAX : tmp;
4814}
4815
Marcin Wojtas3f518502014-07-10 16:52:13 -03004816/* Set the time delay in usec before Rx interrupt */
4817static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004818 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004819{
Thomas Petazzoniab426762017-02-21 11:28:04 +01004820 unsigned long freq = port->priv->tclk;
4821 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004822
Thomas Petazzoniab426762017-02-21 11:28:04 +01004823 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
4824 rxq->time_coal =
4825 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
4826
4827 /* re-evaluate to get actual register value */
4828 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
4829 }
4830
Marcin Wojtas3f518502014-07-10 16:52:13 -03004831 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004832}
4833
Marcin Wojtas3f518502014-07-10 16:52:13 -03004834/* Free Tx queue skbuffs */
4835static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
4836 struct mvpp2_tx_queue *txq,
4837 struct mvpp2_txq_pcpu *txq_pcpu, int num)
4838{
4839 int i;
4840
4841 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01004842 struct mvpp2_txq_pcpu_buf *tx_buf =
4843 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004844
Thomas Petazzoni20396132017-03-07 16:53:00 +01004845 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
Thomas Petazzoni83544912016-12-21 11:28:49 +01004846 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01004847 if (tx_buf->skb)
4848 dev_kfree_skb_any(tx_buf->skb);
4849
4850 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004851 }
4852}
4853
4854static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
4855 u32 cause)
4856{
4857 int queue = fls(cause) - 1;
4858
4859 return port->rxqs[queue];
4860}
4861
4862static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
4863 u32 cause)
4864{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02004865 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004866
4867 return port->txqs[queue];
4868}
4869
4870/* Handle end of transmission */
4871static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
4872 struct mvpp2_txq_pcpu *txq_pcpu)
4873{
4874 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
4875 int tx_done;
4876
4877 if (txq_pcpu->cpu != smp_processor_id())
4878 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
4879
4880 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
4881 if (!tx_done)
4882 return;
4883 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
4884
4885 txq_pcpu->count -= tx_done;
4886
4887 if (netif_tx_queue_stopped(nq))
4888 if (txq_pcpu->size - txq_pcpu->count >= MAX_SKB_FRAGS + 1)
4889 netif_tx_wake_queue(nq);
4890}
4891
Marcin Wojtasedc660f2015-08-06 19:00:30 +02004892static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause)
4893{
4894 struct mvpp2_tx_queue *txq;
4895 struct mvpp2_txq_pcpu *txq_pcpu;
4896 unsigned int tx_todo = 0;
4897
4898 while (cause) {
4899 txq = mvpp2_get_tx_queue(port, cause);
4900 if (!txq)
4901 break;
4902
4903 txq_pcpu = this_cpu_ptr(txq->pcpu);
4904
4905 if (txq_pcpu->count) {
4906 mvpp2_txq_done(port, txq, txq_pcpu);
4907 tx_todo += txq_pcpu->count;
4908 }
4909
4910 cause &= ~(1 << txq->log_id);
4911 }
4912 return tx_todo;
4913}
4914
Marcin Wojtas3f518502014-07-10 16:52:13 -03004915/* Rx/Tx queue initialization/cleanup methods */
4916
4917/* Allocate and initialize descriptors for aggr TXQ */
4918static int mvpp2_aggr_txq_init(struct platform_device *pdev,
4919 struct mvpp2_tx_queue *aggr_txq,
4920 int desc_num, int cpu,
4921 struct mvpp2 *priv)
4922{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004923 u32 txq_dma;
4924
Marcin Wojtas3f518502014-07-10 16:52:13 -03004925 /* Allocate memory for TX descriptors */
4926 aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
4927 desc_num * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004928 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004929 if (!aggr_txq->descs)
4930 return -ENOMEM;
4931
Marcin Wojtas3f518502014-07-10 16:52:13 -03004932 aggr_txq->last_desc = aggr_txq->size - 1;
4933
4934 /* Aggr TXQ no reset WA */
4935 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
4936 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
4937
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004938 /* Set Tx descriptors queue starting address indirect
4939 * access
4940 */
4941 if (priv->hw_version == MVPP21)
4942 txq_dma = aggr_txq->descs_dma;
4943 else
4944 txq_dma = aggr_txq->descs_dma >>
4945 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
4946
4947 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004948 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
4949
4950 return 0;
4951}
4952
4953/* Create a specified Rx queue */
4954static int mvpp2_rxq_init(struct mvpp2_port *port,
4955 struct mvpp2_rx_queue *rxq)
4956
4957{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004958 u32 rxq_dma;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004959 int cpu;
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004960
Marcin Wojtas3f518502014-07-10 16:52:13 -03004961 rxq->size = port->rx_ring_size;
4962
4963 /* Allocate memory for RX descriptors */
4964 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
4965 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004966 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004967 if (!rxq->descs)
4968 return -ENOMEM;
4969
Marcin Wojtas3f518502014-07-10 16:52:13 -03004970 rxq->last_desc = rxq->size - 1;
4971
4972 /* Zero occupied and non-occupied counters - direct access */
4973 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
4974
4975 /* Set Rx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004976 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004977 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004978 if (port->priv->hw_version == MVPP21)
4979 rxq_dma = rxq->descs_dma;
4980 else
4981 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004982 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
4983 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
4984 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004985 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004986
4987 /* Set Offset */
4988 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
4989
4990 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004991 mvpp2_rx_pkts_coal_set(port, rxq);
4992 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004993
4994 /* Add number of descriptors ready for receiving packets */
4995 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
4996
4997 return 0;
4998}
4999
5000/* Push packets received by the RXQ to BM pool */
5001static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
5002 struct mvpp2_rx_queue *rxq)
5003{
5004 int rx_received, i;
5005
5006 rx_received = mvpp2_rxq_received(port, rxq->id);
5007 if (!rx_received)
5008 return;
5009
5010 for (i = 0; i < rx_received; i++) {
5011 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005012 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5013 int pool;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005014
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005015 pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5016 MVPP2_RXD_BM_POOL_ID_OFFS;
5017
5018 mvpp2_pool_refill(port, pool,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005019 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 Petazzonia704bb52017-06-10 23:18:22 +02005048 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005049 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);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005052 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005053}
5054
5055/* Create and initialize a Tx queue */
5056static int mvpp2_txq_init(struct mvpp2_port *port,
5057 struct mvpp2_tx_queue *txq)
5058{
5059 u32 val;
5060 int cpu, desc, desc_per_txq, tx_port_num;
5061 struct mvpp2_txq_pcpu *txq_pcpu;
5062
5063 txq->size = port->tx_ring_size;
5064
5065 /* Allocate memory for Tx descriptors */
5066 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
5067 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005068 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005069 if (!txq->descs)
5070 return -ENOMEM;
5071
Marcin Wojtas3f518502014-07-10 16:52:13 -03005072 txq->last_desc = txq->size - 1;
5073
5074 /* Set Tx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005075 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005076 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5077 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
5078 txq->descs_dma);
5079 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
5080 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
5081 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
5082 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
5083 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
5084 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005085 val &= ~MVPP2_TXQ_PENDING_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005086 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005087
5088 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
5089 * for each existing TXQ.
5090 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
5091 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
5092 */
5093 desc_per_txq = 16;
5094 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
5095 (txq->log_id * desc_per_txq);
5096
Thomas Petazzonia7868412017-03-07 16:53:13 +01005097 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
5098 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
5099 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005100 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005101
5102 /* WRR / EJP configuration - indirect access */
5103 tx_port_num = mvpp2_egress_port(port);
5104 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5105
5106 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
5107 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
5108 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
5109 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
5110 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
5111
5112 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
5113 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
5114 val);
5115
5116 for_each_present_cpu(cpu) {
5117 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5118 txq_pcpu->size = txq->size;
Markus Elfring02c91ec2017-04-17 08:09:07 +02005119 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
5120 sizeof(*txq_pcpu->buffs),
5121 GFP_KERNEL);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005122 if (!txq_pcpu->buffs)
Markus Elfring20b1e162017-04-17 12:58:33 +02005123 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005124
5125 txq_pcpu->count = 0;
5126 txq_pcpu->reserved_num = 0;
5127 txq_pcpu->txq_put_index = 0;
5128 txq_pcpu->txq_get_index = 0;
5129 }
5130
5131 return 0;
Markus Elfring20b1e162017-04-17 12:58:33 +02005132cleanup:
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005133 for_each_present_cpu(cpu) {
5134 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005135 kfree(txq_pcpu->buffs);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005136 }
5137
5138 dma_free_coherent(port->dev->dev.parent,
5139 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005140 txq->descs, txq->descs_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005141
5142 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005143}
5144
5145/* Free allocated TXQ resources */
5146static void mvpp2_txq_deinit(struct mvpp2_port *port,
5147 struct mvpp2_tx_queue *txq)
5148{
5149 struct mvpp2_txq_pcpu *txq_pcpu;
5150 int cpu;
5151
5152 for_each_present_cpu(cpu) {
5153 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005154 kfree(txq_pcpu->buffs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005155 }
5156
5157 if (txq->descs)
5158 dma_free_coherent(port->dev->dev.parent,
5159 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005160 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005161
5162 txq->descs = NULL;
5163 txq->last_desc = 0;
5164 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005165 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005166
5167 /* Set minimum bandwidth for disabled TXQs */
5168 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
5169
5170 /* Set Tx descriptors queue starting address and size */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005171 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005172 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5173 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
5174 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005175 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005176}
5177
5178/* Cleanup Tx ports */
5179static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
5180{
5181 struct mvpp2_txq_pcpu *txq_pcpu;
5182 int delay, pending, cpu;
5183 u32 val;
5184
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005185 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005186 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5187 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005188 val |= MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005189 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005190
5191 /* The napi queue has been stopped so wait for all packets
5192 * to be transmitted.
5193 */
5194 delay = 0;
5195 do {
5196 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
5197 netdev_warn(port->dev,
5198 "port %d: cleaning queue %d timed out\n",
5199 port->id, txq->log_id);
5200 break;
5201 }
5202 mdelay(1);
5203 delay++;
5204
Thomas Petazzonia7868412017-03-07 16:53:13 +01005205 pending = mvpp2_percpu_read(port->priv, cpu,
5206 MVPP2_TXQ_PENDING_REG);
5207 pending &= MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005208 } while (pending);
5209
5210 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005211 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005212 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005213
5214 for_each_present_cpu(cpu) {
5215 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5216
5217 /* Release all packets */
5218 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
5219
5220 /* Reset queue */
5221 txq_pcpu->count = 0;
5222 txq_pcpu->txq_put_index = 0;
5223 txq_pcpu->txq_get_index = 0;
5224 }
5225}
5226
5227/* Cleanup all Tx queues */
5228static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
5229{
5230 struct mvpp2_tx_queue *txq;
5231 int queue;
5232 u32 val;
5233
5234 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
5235
5236 /* Reset Tx ports and delete Tx queues */
5237 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
5238 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5239
5240 for (queue = 0; queue < txq_number; queue++) {
5241 txq = port->txqs[queue];
5242 mvpp2_txq_clean(port, txq);
5243 mvpp2_txq_deinit(port, txq);
5244 }
5245
5246 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5247
5248 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
5249 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5250}
5251
5252/* Cleanup all Rx queues */
5253static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
5254{
5255 int queue;
5256
5257 for (queue = 0; queue < rxq_number; queue++)
5258 mvpp2_rxq_deinit(port, port->rxqs[queue]);
5259}
5260
5261/* Init all Rx queues for port */
5262static int mvpp2_setup_rxqs(struct mvpp2_port *port)
5263{
5264 int queue, err;
5265
5266 for (queue = 0; queue < rxq_number; queue++) {
5267 err = mvpp2_rxq_init(port, port->rxqs[queue]);
5268 if (err)
5269 goto err_cleanup;
5270 }
5271 return 0;
5272
5273err_cleanup:
5274 mvpp2_cleanup_rxqs(port);
5275 return err;
5276}
5277
5278/* Init all tx queues for port */
5279static int mvpp2_setup_txqs(struct mvpp2_port *port)
5280{
5281 struct mvpp2_tx_queue *txq;
5282 int queue, err;
5283
5284 for (queue = 0; queue < txq_number; queue++) {
5285 txq = port->txqs[queue];
5286 err = mvpp2_txq_init(port, txq);
5287 if (err)
5288 goto err_cleanup;
5289 }
5290
Marcin Wojtas3f518502014-07-10 16:52:13 -03005291 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5292 return 0;
5293
5294err_cleanup:
5295 mvpp2_cleanup_txqs(port);
5296 return err;
5297}
5298
5299/* The callback for per-port interrupt */
5300static irqreturn_t mvpp2_isr(int irq, void *dev_id)
5301{
5302 struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
5303
5304 mvpp2_interrupts_disable(port);
5305
5306 napi_schedule(&port->napi);
5307
5308 return IRQ_HANDLED;
5309}
5310
5311/* Adjust link */
5312static void mvpp2_link_event(struct net_device *dev)
5313{
5314 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02005315 struct phy_device *phydev = dev->phydev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005316 int status_change = 0;
5317 u32 val;
5318
5319 if (phydev->link) {
5320 if ((port->speed != phydev->speed) ||
5321 (port->duplex != phydev->duplex)) {
5322 u32 val;
5323
5324 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5325 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
5326 MVPP2_GMAC_CONFIG_GMII_SPEED |
5327 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
5328 MVPP2_GMAC_AN_SPEED_EN |
5329 MVPP2_GMAC_AN_DUPLEX_EN);
5330
5331 if (phydev->duplex)
5332 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
5333
5334 if (phydev->speed == SPEED_1000)
5335 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
Thomas Petazzoni2add5112014-07-27 23:21:35 +02005336 else if (phydev->speed == SPEED_100)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005337 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
5338
5339 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5340
5341 port->duplex = phydev->duplex;
5342 port->speed = phydev->speed;
5343 }
5344 }
5345
5346 if (phydev->link != port->link) {
5347 if (!phydev->link) {
5348 port->duplex = -1;
5349 port->speed = 0;
5350 }
5351
5352 port->link = phydev->link;
5353 status_change = 1;
5354 }
5355
5356 if (status_change) {
5357 if (phydev->link) {
5358 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5359 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
5360 MVPP2_GMAC_FORCE_LINK_DOWN);
5361 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5362 mvpp2_egress_enable(port);
5363 mvpp2_ingress_enable(port);
5364 } else {
5365 mvpp2_ingress_disable(port);
5366 mvpp2_egress_disable(port);
5367 }
5368 phy_print_status(phydev);
5369 }
5370}
5371
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005372static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
5373{
5374 ktime_t interval;
5375
5376 if (!port_pcpu->timer_scheduled) {
5377 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01005378 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005379 hrtimer_start(&port_pcpu->tx_done_timer, interval,
5380 HRTIMER_MODE_REL_PINNED);
5381 }
5382}
5383
5384static void mvpp2_tx_proc_cb(unsigned long data)
5385{
5386 struct net_device *dev = (struct net_device *)data;
5387 struct mvpp2_port *port = netdev_priv(dev);
5388 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5389 unsigned int tx_todo, cause;
5390
5391 if (!netif_running(dev))
5392 return;
5393 port_pcpu->timer_scheduled = false;
5394
5395 /* Process all the Tx queues */
5396 cause = (1 << txq_number) - 1;
5397 tx_todo = mvpp2_tx_done(port, cause);
5398
5399 /* Set the timer in case not all the packets were processed */
5400 if (tx_todo)
5401 mvpp2_timer_set(port_pcpu);
5402}
5403
5404static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
5405{
5406 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
5407 struct mvpp2_port_pcpu,
5408 tx_done_timer);
5409
5410 tasklet_schedule(&port_pcpu->tx_done_tasklet);
5411
5412 return HRTIMER_NORESTART;
5413}
5414
Marcin Wojtas3f518502014-07-10 16:52:13 -03005415/* Main RX/TX processing routines */
5416
5417/* Display more error info */
5418static void mvpp2_rx_error(struct mvpp2_port *port,
5419 struct mvpp2_rx_desc *rx_desc)
5420{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005421 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5422 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005423
5424 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
5425 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005426 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
5427 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005428 break;
5429 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005430 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
5431 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005432 break;
5433 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005434 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
5435 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005436 break;
5437 }
5438}
5439
5440/* Handle RX checksum offload */
5441static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
5442 struct sk_buff *skb)
5443{
5444 if (((status & MVPP2_RXD_L3_IP4) &&
5445 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
5446 (status & MVPP2_RXD_L3_IP6))
5447 if (((status & MVPP2_RXD_L4_UDP) ||
5448 (status & MVPP2_RXD_L4_TCP)) &&
5449 (status & MVPP2_RXD_L4_CSUM_OK)) {
5450 skb->csum = 0;
5451 skb->ip_summed = CHECKSUM_UNNECESSARY;
5452 return;
5453 }
5454
5455 skb->ip_summed = CHECKSUM_NONE;
5456}
5457
5458/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
5459static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005460 struct mvpp2_bm_pool *bm_pool, int pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005461{
Thomas Petazzoni20396132017-03-07 16:53:00 +01005462 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005463 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005464 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005465
Marcin Wojtas3f518502014-07-10 16:52:13 -03005466 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005467 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
5468 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005469 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005470 return -ENOMEM;
5471
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005472 mvpp2_pool_refill(port, pool, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01005473
Marcin Wojtas3f518502014-07-10 16:52:13 -03005474 return 0;
5475}
5476
5477/* Handle tx checksum */
5478static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
5479{
5480 if (skb->ip_summed == CHECKSUM_PARTIAL) {
5481 int ip_hdr_len = 0;
5482 u8 l4_proto;
5483
5484 if (skb->protocol == htons(ETH_P_IP)) {
5485 struct iphdr *ip4h = ip_hdr(skb);
5486
5487 /* Calculate IPv4 checksum and L4 checksum */
5488 ip_hdr_len = ip4h->ihl;
5489 l4_proto = ip4h->protocol;
5490 } else if (skb->protocol == htons(ETH_P_IPV6)) {
5491 struct ipv6hdr *ip6h = ipv6_hdr(skb);
5492
5493 /* Read l4_protocol from one of IPv6 extra headers */
5494 if (skb_network_header_len(skb) > 0)
5495 ip_hdr_len = (skb_network_header_len(skb) >> 2);
5496 l4_proto = ip6h->nexthdr;
5497 } else {
5498 return MVPP2_TXD_L4_CSUM_NOT;
5499 }
5500
5501 return mvpp2_txq_desc_csum(skb_network_offset(skb),
5502 skb->protocol, ip_hdr_len, l4_proto);
5503 }
5504
5505 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
5506}
5507
Marcin Wojtas3f518502014-07-10 16:52:13 -03005508/* Main rx processing */
5509static int mvpp2_rx(struct mvpp2_port *port, int rx_todo,
5510 struct mvpp2_rx_queue *rxq)
5511{
5512 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01005513 int rx_received;
5514 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005515 u32 rcvd_pkts = 0;
5516 u32 rcvd_bytes = 0;
5517
5518 /* Get number of received packets and clamp the to-do */
5519 rx_received = mvpp2_rxq_received(port, rxq->id);
5520 if (rx_todo > rx_received)
5521 rx_todo = rx_received;
5522
Marcin Wojtasb5015852015-12-03 15:20:51 +01005523 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005524 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
5525 struct mvpp2_bm_pool *bm_pool;
5526 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005527 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005528 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005529 phys_addr_t phys_addr;
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005530 u32 rx_status;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005531 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005532 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005533
Marcin Wojtasb5015852015-12-03 15:20:51 +01005534 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005535 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
5536 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
5537 rx_bytes -= MVPP2_MH_SIZE;
5538 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
5539 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
5540 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005541
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005542 pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5543 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005544 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03005545
5546 /* In case of an error, release the requested buffer pointer
5547 * to the Buffer Manager. This request process is controlled
5548 * by the hardware, and the information about the buffer is
5549 * comprised by the RX descriptor.
5550 */
5551 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Markus Elfring8a524882017-04-17 10:52:02 +02005552err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03005553 dev->stats.rx_errors++;
5554 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01005555 /* Return the buffer to the pool */
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005556 mvpp2_pool_refill(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005557 continue;
5558 }
5559
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005560 if (bm_pool->frag_size > PAGE_SIZE)
5561 frag_size = 0;
5562 else
5563 frag_size = bm_pool->frag_size;
5564
5565 skb = build_skb(data, frag_size);
5566 if (!skb) {
5567 netdev_warn(port->dev, "skb build failed\n");
5568 goto err_drop_frame;
5569 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005570
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005571 err = mvpp2_rx_refill(port, bm_pool, pool);
Marcin Wojtasb5015852015-12-03 15:20:51 +01005572 if (err) {
5573 netdev_err(port->dev, "failed to refill BM pools\n");
5574 goto err_drop_frame;
5575 }
5576
Thomas Petazzoni20396132017-03-07 16:53:00 +01005577 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01005578 bm_pool->buf_size, DMA_FROM_DEVICE);
5579
Marcin Wojtas3f518502014-07-10 16:52:13 -03005580 rcvd_pkts++;
5581 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005582
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005583 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005584 skb_put(skb, rx_bytes);
5585 skb->protocol = eth_type_trans(skb, dev);
5586 mvpp2_rx_csum(port, rx_status, skb);
5587
5588 napi_gro_receive(&port->napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005589 }
5590
5591 if (rcvd_pkts) {
5592 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
5593
5594 u64_stats_update_begin(&stats->syncp);
5595 stats->rx_packets += rcvd_pkts;
5596 stats->rx_bytes += rcvd_bytes;
5597 u64_stats_update_end(&stats->syncp);
5598 }
5599
5600 /* Update Rx queue management counters */
5601 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01005602 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005603
5604 return rx_todo;
5605}
5606
5607static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005608tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005609 struct mvpp2_tx_desc *desc)
5610{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005611 dma_addr_t buf_dma_addr =
5612 mvpp2_txdesc_dma_addr_get(port, desc);
5613 size_t buf_sz =
5614 mvpp2_txdesc_size_get(port, desc);
5615 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
5616 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005617 mvpp2_txq_desc_put(txq);
5618}
5619
5620/* Handle tx fragmentation processing */
5621static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
5622 struct mvpp2_tx_queue *aggr_txq,
5623 struct mvpp2_tx_queue *txq)
5624{
5625 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
5626 struct mvpp2_tx_desc *tx_desc;
5627 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005628 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005629
5630 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
5631 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5632 void *addr = page_address(frag->page.p) + frag->page_offset;
5633
5634 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005635 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
5636 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005637
Thomas Petazzoni20396132017-03-07 16:53:00 +01005638 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005639 frag->size,
5640 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01005641 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005642 mvpp2_txq_desc_put(txq);
Markus Elfring32bae632017-04-17 11:36:34 +02005643 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005644 }
5645
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005646 mvpp2_txdesc_offset_set(port, tx_desc,
5647 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
5648 mvpp2_txdesc_dma_addr_set(port, tx_desc,
5649 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005650
5651 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
5652 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005653 mvpp2_txdesc_cmd_set(port, tx_desc,
5654 MVPP2_TXD_L_DESC);
5655 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005656 } else {
5657 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005658 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
5659 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005660 }
5661 }
5662
5663 return 0;
Markus Elfring32bae632017-04-17 11:36:34 +02005664cleanup:
Marcin Wojtas3f518502014-07-10 16:52:13 -03005665 /* Release all descriptors that were used to map fragments of
5666 * this packet, as well as the corresponding DMA mappings
5667 */
5668 for (i = i - 1; i >= 0; i--) {
5669 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005670 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005671 }
5672
5673 return -ENOMEM;
5674}
5675
5676/* Main tx processing */
5677static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
5678{
5679 struct mvpp2_port *port = netdev_priv(dev);
5680 struct mvpp2_tx_queue *txq, *aggr_txq;
5681 struct mvpp2_txq_pcpu *txq_pcpu;
5682 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005683 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005684 int frags = 0;
5685 u16 txq_id;
5686 u32 tx_cmd;
5687
5688 txq_id = skb_get_queue_mapping(skb);
5689 txq = port->txqs[txq_id];
5690 txq_pcpu = this_cpu_ptr(txq->pcpu);
5691 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
5692
5693 frags = skb_shinfo(skb)->nr_frags + 1;
5694
5695 /* Check number of available descriptors */
5696 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
5697 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
5698 txq_pcpu, frags)) {
5699 frags = 0;
5700 goto out;
5701 }
5702
5703 /* Get a descriptor for the first part of the packet */
5704 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005705 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
5706 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005707
Thomas Petazzoni20396132017-03-07 16:53:00 +01005708 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005709 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01005710 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005711 mvpp2_txq_desc_put(txq);
5712 frags = 0;
5713 goto out;
5714 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005715
5716 mvpp2_txdesc_offset_set(port, tx_desc,
5717 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
5718 mvpp2_txdesc_dma_addr_set(port, tx_desc,
5719 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005720
5721 tx_cmd = mvpp2_skb_tx_csum(port, skb);
5722
5723 if (frags == 1) {
5724 /* First and Last descriptor */
5725 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005726 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
5727 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005728 } else {
5729 /* First but not Last */
5730 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005731 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
5732 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005733
5734 /* Continue with other skb fragments */
5735 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005736 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005737 frags = 0;
5738 goto out;
5739 }
5740 }
5741
5742 txq_pcpu->reserved_num -= frags;
5743 txq_pcpu->count += frags;
5744 aggr_txq->count += frags;
5745
5746 /* Enable transmit */
5747 wmb();
5748 mvpp2_aggr_txq_pend_desc_add(port, frags);
5749
5750 if (txq_pcpu->size - txq_pcpu->count < MAX_SKB_FRAGS + 1) {
5751 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
5752
5753 netif_tx_stop_queue(nq);
5754 }
5755out:
5756 if (frags > 0) {
5757 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
5758
5759 u64_stats_update_begin(&stats->syncp);
5760 stats->tx_packets++;
5761 stats->tx_bytes += skb->len;
5762 u64_stats_update_end(&stats->syncp);
5763 } else {
5764 dev->stats.tx_dropped++;
5765 dev_kfree_skb_any(skb);
5766 }
5767
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005768 /* Finalize TX processing */
5769 if (txq_pcpu->count >= txq->done_pkts_coal)
5770 mvpp2_txq_done(port, txq, txq_pcpu);
5771
5772 /* Set the timer in case not all frags were processed */
5773 if (txq_pcpu->count <= frags && txq_pcpu->count > 0) {
5774 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5775
5776 mvpp2_timer_set(port_pcpu);
5777 }
5778
Marcin Wojtas3f518502014-07-10 16:52:13 -03005779 return NETDEV_TX_OK;
5780}
5781
5782static inline void mvpp2_cause_error(struct net_device *dev, int cause)
5783{
5784 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
5785 netdev_err(dev, "FCS error\n");
5786 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
5787 netdev_err(dev, "rx fifo overrun error\n");
5788 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
5789 netdev_err(dev, "tx fifo underrun error\n");
5790}
5791
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005792static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005793{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005794 u32 cause_rx_tx, cause_rx, cause_misc;
5795 int rx_done = 0;
5796 struct mvpp2_port *port = netdev_priv(napi->dev);
Thomas Petazzonia7868412017-03-07 16:53:13 +01005797 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005798
5799 /* Rx/Tx cause register
5800 *
5801 * Bits 0-15: each bit indicates received packets on the Rx queue
5802 * (bit 0 is for Rx queue 0).
5803 *
5804 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
5805 * (bit 16 is for Tx queue 0).
5806 *
5807 * Each CPU has its own Rx/Tx cause register
5808 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005809 cause_rx_tx = mvpp2_percpu_read(port->priv, cpu,
5810 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005811 cause_rx_tx &= ~MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005812 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
5813
5814 if (cause_misc) {
5815 mvpp2_cause_error(port->dev, cause_misc);
5816
5817 /* Clear the cause register */
5818 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01005819 mvpp2_percpu_write(port->priv, cpu,
5820 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
5821 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005822 }
5823
Marcin Wojtas3f518502014-07-10 16:52:13 -03005824 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
5825
5826 /* Process RX packets */
5827 cause_rx |= port->pending_cause_rx;
5828 while (cause_rx && budget > 0) {
5829 int count;
5830 struct mvpp2_rx_queue *rxq;
5831
5832 rxq = mvpp2_get_rx_queue(port, cause_rx);
5833 if (!rxq)
5834 break;
5835
5836 count = mvpp2_rx(port, budget, rxq);
5837 rx_done += count;
5838 budget -= count;
5839 if (budget > 0) {
5840 /* Clear the bit associated to this Rx queue
5841 * so that next iteration will continue from
5842 * the next Rx queue.
5843 */
5844 cause_rx &= ~(1 << rxq->logic_rxq);
5845 }
5846 }
5847
5848 if (budget > 0) {
5849 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08005850 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005851
5852 mvpp2_interrupts_enable(port);
5853 }
5854 port->pending_cause_rx = cause_rx;
5855 return rx_done;
5856}
5857
5858/* Set hw internals when starting port */
5859static void mvpp2_start_dev(struct mvpp2_port *port)
5860{
Philippe Reynes8e072692016-06-28 00:08:11 +02005861 struct net_device *ndev = port->dev;
5862
Marcin Wojtas3f518502014-07-10 16:52:13 -03005863 mvpp2_gmac_max_rx_size_set(port);
5864 mvpp2_txp_max_tx_size_set(port);
5865
5866 napi_enable(&port->napi);
5867
5868 /* Enable interrupts on all CPUs */
5869 mvpp2_interrupts_enable(port);
5870
5871 mvpp2_port_enable(port);
Philippe Reynes8e072692016-06-28 00:08:11 +02005872 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005873 netif_tx_start_all_queues(port->dev);
5874}
5875
5876/* Set hw internals when stopping port */
5877static void mvpp2_stop_dev(struct mvpp2_port *port)
5878{
Philippe Reynes8e072692016-06-28 00:08:11 +02005879 struct net_device *ndev = port->dev;
5880
Marcin Wojtas3f518502014-07-10 16:52:13 -03005881 /* Stop new packets from arriving to RXQs */
5882 mvpp2_ingress_disable(port);
5883
5884 mdelay(10);
5885
5886 /* Disable interrupts on all CPUs */
5887 mvpp2_interrupts_disable(port);
5888
5889 napi_disable(&port->napi);
5890
5891 netif_carrier_off(port->dev);
5892 netif_tx_stop_all_queues(port->dev);
5893
5894 mvpp2_egress_disable(port);
5895 mvpp2_port_disable(port);
Philippe Reynes8e072692016-06-28 00:08:11 +02005896 phy_stop(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005897}
5898
Marcin Wojtas3f518502014-07-10 16:52:13 -03005899static int mvpp2_check_ringparam_valid(struct net_device *dev,
5900 struct ethtool_ringparam *ring)
5901{
5902 u16 new_rx_pending = ring->rx_pending;
5903 u16 new_tx_pending = ring->tx_pending;
5904
5905 if (ring->rx_pending == 0 || ring->tx_pending == 0)
5906 return -EINVAL;
5907
5908 if (ring->rx_pending > MVPP2_MAX_RXD)
5909 new_rx_pending = MVPP2_MAX_RXD;
5910 else if (!IS_ALIGNED(ring->rx_pending, 16))
5911 new_rx_pending = ALIGN(ring->rx_pending, 16);
5912
5913 if (ring->tx_pending > MVPP2_MAX_TXD)
5914 new_tx_pending = MVPP2_MAX_TXD;
5915 else if (!IS_ALIGNED(ring->tx_pending, 32))
5916 new_tx_pending = ALIGN(ring->tx_pending, 32);
5917
5918 if (ring->rx_pending != new_rx_pending) {
5919 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
5920 ring->rx_pending, new_rx_pending);
5921 ring->rx_pending = new_rx_pending;
5922 }
5923
5924 if (ring->tx_pending != new_tx_pending) {
5925 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
5926 ring->tx_pending, new_tx_pending);
5927 ring->tx_pending = new_tx_pending;
5928 }
5929
5930 return 0;
5931}
5932
Thomas Petazzoni26975822017-03-07 16:53:14 +01005933static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005934{
5935 u32 mac_addr_l, mac_addr_m, mac_addr_h;
5936
5937 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
5938 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
5939 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
5940 addr[0] = (mac_addr_h >> 24) & 0xFF;
5941 addr[1] = (mac_addr_h >> 16) & 0xFF;
5942 addr[2] = (mac_addr_h >> 8) & 0xFF;
5943 addr[3] = mac_addr_h & 0xFF;
5944 addr[4] = mac_addr_m & 0xFF;
5945 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
5946}
5947
5948static int mvpp2_phy_connect(struct mvpp2_port *port)
5949{
5950 struct phy_device *phy_dev;
5951
5952 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
5953 port->phy_interface);
5954 if (!phy_dev) {
5955 netdev_err(port->dev, "cannot connect to phy\n");
5956 return -ENODEV;
5957 }
5958 phy_dev->supported &= PHY_GBIT_FEATURES;
5959 phy_dev->advertising = phy_dev->supported;
5960
Marcin Wojtas3f518502014-07-10 16:52:13 -03005961 port->link = 0;
5962 port->duplex = 0;
5963 port->speed = 0;
5964
5965 return 0;
5966}
5967
5968static void mvpp2_phy_disconnect(struct mvpp2_port *port)
5969{
Philippe Reynes8e072692016-06-28 00:08:11 +02005970 struct net_device *ndev = port->dev;
5971
5972 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005973}
5974
5975static int mvpp2_open(struct net_device *dev)
5976{
5977 struct mvpp2_port *port = netdev_priv(dev);
5978 unsigned char mac_bcast[ETH_ALEN] = {
5979 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5980 int err;
5981
5982 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
5983 if (err) {
5984 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
5985 return err;
5986 }
5987 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
5988 dev->dev_addr, true);
5989 if (err) {
5990 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
5991 return err;
5992 }
5993 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
5994 if (err) {
5995 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
5996 return err;
5997 }
5998 err = mvpp2_prs_def_flow(port);
5999 if (err) {
6000 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
6001 return err;
6002 }
6003
6004 /* Allocate the Rx/Tx queues */
6005 err = mvpp2_setup_rxqs(port);
6006 if (err) {
6007 netdev_err(port->dev, "cannot allocate Rx queues\n");
6008 return err;
6009 }
6010
6011 err = mvpp2_setup_txqs(port);
6012 if (err) {
6013 netdev_err(port->dev, "cannot allocate Tx queues\n");
6014 goto err_cleanup_rxqs;
6015 }
6016
6017 err = request_irq(port->irq, mvpp2_isr, 0, dev->name, port);
6018 if (err) {
6019 netdev_err(port->dev, "cannot request IRQ %d\n", port->irq);
6020 goto err_cleanup_txqs;
6021 }
6022
6023 /* In default link is down */
6024 netif_carrier_off(port->dev);
6025
6026 err = mvpp2_phy_connect(port);
6027 if (err < 0)
6028 goto err_free_irq;
6029
6030 /* Unmask interrupts on all CPUs */
6031 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
6032
6033 mvpp2_start_dev(port);
6034
6035 return 0;
6036
6037err_free_irq:
6038 free_irq(port->irq, port);
6039err_cleanup_txqs:
6040 mvpp2_cleanup_txqs(port);
6041err_cleanup_rxqs:
6042 mvpp2_cleanup_rxqs(port);
6043 return err;
6044}
6045
6046static int mvpp2_stop(struct net_device *dev)
6047{
6048 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006049 struct mvpp2_port_pcpu *port_pcpu;
6050 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006051
6052 mvpp2_stop_dev(port);
6053 mvpp2_phy_disconnect(port);
6054
6055 /* Mask interrupts on all CPUs */
6056 on_each_cpu(mvpp2_interrupts_mask, port, 1);
6057
6058 free_irq(port->irq, port);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006059 for_each_present_cpu(cpu) {
6060 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
6061
6062 hrtimer_cancel(&port_pcpu->tx_done_timer);
6063 port_pcpu->timer_scheduled = false;
6064 tasklet_kill(&port_pcpu->tx_done_tasklet);
6065 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006066 mvpp2_cleanup_rxqs(port);
6067 mvpp2_cleanup_txqs(port);
6068
6069 return 0;
6070}
6071
6072static void mvpp2_set_rx_mode(struct net_device *dev)
6073{
6074 struct mvpp2_port *port = netdev_priv(dev);
6075 struct mvpp2 *priv = port->priv;
6076 struct netdev_hw_addr *ha;
6077 int id = port->id;
6078 bool allmulti = dev->flags & IFF_ALLMULTI;
6079
6080 mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
6081 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
6082 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
6083
6084 /* Remove all port->id's mcast enries */
6085 mvpp2_prs_mcast_del_all(priv, id);
6086
6087 if (allmulti && !netdev_mc_empty(dev)) {
6088 netdev_for_each_mc_addr(ha, dev)
6089 mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
6090 }
6091}
6092
6093static int mvpp2_set_mac_address(struct net_device *dev, void *p)
6094{
6095 struct mvpp2_port *port = netdev_priv(dev);
6096 const struct sockaddr *addr = p;
6097 int err;
6098
6099 if (!is_valid_ether_addr(addr->sa_data)) {
6100 err = -EADDRNOTAVAIL;
Markus Elfringc1175542017-04-17 11:10:47 +02006101 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006102 }
6103
6104 if (!netif_running(dev)) {
6105 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6106 if (!err)
6107 return 0;
6108 /* Reconfigure parser to accept the original MAC address */
6109 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6110 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006111 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006112 }
6113
6114 mvpp2_stop_dev(port);
6115
6116 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6117 if (!err)
6118 goto out_start;
6119
6120 /* Reconfigure parser accept the original MAC address */
6121 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6122 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006123 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006124out_start:
6125 mvpp2_start_dev(port);
6126 mvpp2_egress_enable(port);
6127 mvpp2_ingress_enable(port);
6128 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02006129log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02006130 netdev_err(dev, "failed to change MAC address\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006131 return err;
6132}
6133
6134static int mvpp2_change_mtu(struct net_device *dev, int mtu)
6135{
6136 struct mvpp2_port *port = netdev_priv(dev);
6137 int err;
6138
Jarod Wilson57779872016-10-17 15:54:06 -04006139 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
6140 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
6141 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
6142 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006143 }
6144
6145 if (!netif_running(dev)) {
6146 err = mvpp2_bm_update_mtu(dev, mtu);
6147 if (!err) {
6148 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6149 return 0;
6150 }
6151
6152 /* Reconfigure BM to the original MTU */
6153 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6154 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006155 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006156 }
6157
6158 mvpp2_stop_dev(port);
6159
6160 err = mvpp2_bm_update_mtu(dev, mtu);
6161 if (!err) {
6162 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6163 goto out_start;
6164 }
6165
6166 /* Reconfigure BM to the original MTU */
6167 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6168 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006169 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006170
6171out_start:
6172 mvpp2_start_dev(port);
6173 mvpp2_egress_enable(port);
6174 mvpp2_ingress_enable(port);
6175
6176 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02006177log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02006178 netdev_err(dev, "failed to change MTU\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006179 return err;
6180}
6181
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006182static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03006183mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6184{
6185 struct mvpp2_port *port = netdev_priv(dev);
6186 unsigned int start;
6187 int cpu;
6188
6189 for_each_possible_cpu(cpu) {
6190 struct mvpp2_pcpu_stats *cpu_stats;
6191 u64 rx_packets;
6192 u64 rx_bytes;
6193 u64 tx_packets;
6194 u64 tx_bytes;
6195
6196 cpu_stats = per_cpu_ptr(port->stats, cpu);
6197 do {
6198 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
6199 rx_packets = cpu_stats->rx_packets;
6200 rx_bytes = cpu_stats->rx_bytes;
6201 tx_packets = cpu_stats->tx_packets;
6202 tx_bytes = cpu_stats->tx_bytes;
6203 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
6204
6205 stats->rx_packets += rx_packets;
6206 stats->rx_bytes += rx_bytes;
6207 stats->tx_packets += tx_packets;
6208 stats->tx_bytes += tx_bytes;
6209 }
6210
6211 stats->rx_errors = dev->stats.rx_errors;
6212 stats->rx_dropped = dev->stats.rx_dropped;
6213 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006214}
6215
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006216static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6217{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006218 int ret;
6219
Philippe Reynes8e072692016-06-28 00:08:11 +02006220 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006221 return -ENOTSUPP;
6222
Philippe Reynes8e072692016-06-28 00:08:11 +02006223 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006224 if (!ret)
6225 mvpp2_link_event(dev);
6226
6227 return ret;
6228}
6229
Marcin Wojtas3f518502014-07-10 16:52:13 -03006230/* Ethtool methods */
6231
Marcin Wojtas3f518502014-07-10 16:52:13 -03006232/* Set interrupt coalescing for ethtools */
6233static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
6234 struct ethtool_coalesce *c)
6235{
6236 struct mvpp2_port *port = netdev_priv(dev);
6237 int queue;
6238
6239 for (queue = 0; queue < rxq_number; queue++) {
6240 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6241
6242 rxq->time_coal = c->rx_coalesce_usecs;
6243 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01006244 mvpp2_rx_pkts_coal_set(port, rxq);
6245 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006246 }
6247
6248 for (queue = 0; queue < txq_number; queue++) {
6249 struct mvpp2_tx_queue *txq = port->txqs[queue];
6250
6251 txq->done_pkts_coal = c->tx_max_coalesced_frames;
6252 }
6253
Marcin Wojtas3f518502014-07-10 16:52:13 -03006254 return 0;
6255}
6256
6257/* get coalescing for ethtools */
6258static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
6259 struct ethtool_coalesce *c)
6260{
6261 struct mvpp2_port *port = netdev_priv(dev);
6262
6263 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
6264 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
6265 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
6266 return 0;
6267}
6268
6269static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
6270 struct ethtool_drvinfo *drvinfo)
6271{
6272 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
6273 sizeof(drvinfo->driver));
6274 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
6275 sizeof(drvinfo->version));
6276 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
6277 sizeof(drvinfo->bus_info));
6278}
6279
6280static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
6281 struct ethtool_ringparam *ring)
6282{
6283 struct mvpp2_port *port = netdev_priv(dev);
6284
6285 ring->rx_max_pending = MVPP2_MAX_RXD;
6286 ring->tx_max_pending = MVPP2_MAX_TXD;
6287 ring->rx_pending = port->rx_ring_size;
6288 ring->tx_pending = port->tx_ring_size;
6289}
6290
6291static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
6292 struct ethtool_ringparam *ring)
6293{
6294 struct mvpp2_port *port = netdev_priv(dev);
6295 u16 prev_rx_ring_size = port->rx_ring_size;
6296 u16 prev_tx_ring_size = port->tx_ring_size;
6297 int err;
6298
6299 err = mvpp2_check_ringparam_valid(dev, ring);
6300 if (err)
6301 return err;
6302
6303 if (!netif_running(dev)) {
6304 port->rx_ring_size = ring->rx_pending;
6305 port->tx_ring_size = ring->tx_pending;
6306 return 0;
6307 }
6308
6309 /* The interface is running, so we have to force a
6310 * reallocation of the queues
6311 */
6312 mvpp2_stop_dev(port);
6313 mvpp2_cleanup_rxqs(port);
6314 mvpp2_cleanup_txqs(port);
6315
6316 port->rx_ring_size = ring->rx_pending;
6317 port->tx_ring_size = ring->tx_pending;
6318
6319 err = mvpp2_setup_rxqs(port);
6320 if (err) {
6321 /* Reallocate Rx queues with the original ring size */
6322 port->rx_ring_size = prev_rx_ring_size;
6323 ring->rx_pending = prev_rx_ring_size;
6324 err = mvpp2_setup_rxqs(port);
6325 if (err)
6326 goto err_out;
6327 }
6328 err = mvpp2_setup_txqs(port);
6329 if (err) {
6330 /* Reallocate Tx queues with the original ring size */
6331 port->tx_ring_size = prev_tx_ring_size;
6332 ring->tx_pending = prev_tx_ring_size;
6333 err = mvpp2_setup_txqs(port);
6334 if (err)
6335 goto err_clean_rxqs;
6336 }
6337
6338 mvpp2_start_dev(port);
6339 mvpp2_egress_enable(port);
6340 mvpp2_ingress_enable(port);
6341
6342 return 0;
6343
6344err_clean_rxqs:
6345 mvpp2_cleanup_rxqs(port);
6346err_out:
Markus Elfringdfd42402017-04-17 11:20:41 +02006347 netdev_err(dev, "failed to change ring parameters");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006348 return err;
6349}
6350
6351/* Device ops */
6352
6353static const struct net_device_ops mvpp2_netdev_ops = {
6354 .ndo_open = mvpp2_open,
6355 .ndo_stop = mvpp2_stop,
6356 .ndo_start_xmit = mvpp2_tx,
6357 .ndo_set_rx_mode = mvpp2_set_rx_mode,
6358 .ndo_set_mac_address = mvpp2_set_mac_address,
6359 .ndo_change_mtu = mvpp2_change_mtu,
6360 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006361 .ndo_do_ioctl = mvpp2_ioctl,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006362};
6363
6364static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08006365 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006366 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006367 .set_coalesce = mvpp2_ethtool_set_coalesce,
6368 .get_coalesce = mvpp2_ethtool_get_coalesce,
6369 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
6370 .get_ringparam = mvpp2_ethtool_get_ringparam,
6371 .set_ringparam = mvpp2_ethtool_set_ringparam,
Philippe Reynesfb773e92016-06-28 00:08:12 +02006372 .get_link_ksettings = phy_ethtool_get_link_ksettings,
6373 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006374};
6375
Marcin Wojtas3f518502014-07-10 16:52:13 -03006376/* Initialize port HW */
6377static int mvpp2_port_init(struct mvpp2_port *port)
6378{
6379 struct device *dev = port->dev->dev.parent;
6380 struct mvpp2 *priv = port->priv;
6381 struct mvpp2_txq_pcpu *txq_pcpu;
6382 int queue, cpu, err;
6383
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006384 if (port->first_rxq + rxq_number >
6385 MVPP2_MAX_PORTS * priv->max_port_rxqs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006386 return -EINVAL;
6387
6388 /* Disable port */
6389 mvpp2_egress_disable(port);
6390 mvpp2_port_disable(port);
6391
6392 port->txqs = devm_kcalloc(dev, txq_number, sizeof(*port->txqs),
6393 GFP_KERNEL);
6394 if (!port->txqs)
6395 return -ENOMEM;
6396
6397 /* Associate physical Tx queues to this port and initialize.
6398 * The mapping is predefined.
6399 */
6400 for (queue = 0; queue < txq_number; queue++) {
6401 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
6402 struct mvpp2_tx_queue *txq;
6403
6404 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01006405 if (!txq) {
6406 err = -ENOMEM;
6407 goto err_free_percpu;
6408 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006409
6410 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
6411 if (!txq->pcpu) {
6412 err = -ENOMEM;
6413 goto err_free_percpu;
6414 }
6415
6416 txq->id = queue_phy_id;
6417 txq->log_id = queue;
6418 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
6419 for_each_present_cpu(cpu) {
6420 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
6421 txq_pcpu->cpu = cpu;
6422 }
6423
6424 port->txqs[queue] = txq;
6425 }
6426
6427 port->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*port->rxqs),
6428 GFP_KERNEL);
6429 if (!port->rxqs) {
6430 err = -ENOMEM;
6431 goto err_free_percpu;
6432 }
6433
6434 /* Allocate and initialize Rx queue for this port */
6435 for (queue = 0; queue < rxq_number; queue++) {
6436 struct mvpp2_rx_queue *rxq;
6437
6438 /* Map physical Rx queue to port's logical Rx queue */
6439 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08006440 if (!rxq) {
6441 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006442 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08006443 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006444 /* Map this Rx queue to a physical queue */
6445 rxq->id = port->first_rxq + queue;
6446 rxq->port = port->id;
6447 rxq->logic_rxq = queue;
6448
6449 port->rxqs[queue] = rxq;
6450 }
6451
6452 /* Configure Rx queue group interrupt for this port */
Thomas Petazzonia73fef12017-03-07 16:53:16 +01006453 if (priv->hw_version == MVPP21) {
6454 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
6455 rxq_number);
6456 } else {
6457 u32 val;
6458
6459 val = (port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET);
6460 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
6461
6462 val = (rxq_number << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET);
6463 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
6464 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006465
6466 /* Create Rx descriptor rings */
6467 for (queue = 0; queue < rxq_number; queue++) {
6468 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6469
6470 rxq->size = port->rx_ring_size;
6471 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
6472 rxq->time_coal = MVPP2_RX_COAL_USEC;
6473 }
6474
6475 mvpp2_ingress_disable(port);
6476
6477 /* Port default configuration */
6478 mvpp2_defaults_set(port);
6479
6480 /* Port's classifier configuration */
6481 mvpp2_cls_oversize_rxq_set(port);
6482 mvpp2_cls_port_config(port);
6483
6484 /* Provide an initial Rx packet size */
6485 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
6486
6487 /* Initialize pools for swf */
6488 err = mvpp2_swf_bm_pool_init(port);
6489 if (err)
6490 goto err_free_percpu;
6491
6492 return 0;
6493
6494err_free_percpu:
6495 for (queue = 0; queue < txq_number; queue++) {
6496 if (!port->txqs[queue])
6497 continue;
6498 free_percpu(port->txqs[queue]->pcpu);
6499 }
6500 return err;
6501}
6502
6503/* Ports initialization */
6504static int mvpp2_port_probe(struct platform_device *pdev,
6505 struct device_node *port_node,
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006506 struct mvpp2 *priv)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006507{
6508 struct device_node *phy_node;
6509 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006510 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006511 struct net_device *dev;
6512 struct resource *res;
6513 const char *dt_mac_addr;
6514 const char *mac_from;
6515 char hw_mac_addr[ETH_ALEN];
6516 u32 id;
6517 int features;
6518 int phy_mode;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006519 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006520
Markus Elfring03bfffd2017-04-17 08:55:42 +02006521 dev = alloc_etherdev_mqs(sizeof(*port), txq_number, rxq_number);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006522 if (!dev)
6523 return -ENOMEM;
6524
6525 phy_node = of_parse_phandle(port_node, "phy", 0);
6526 if (!phy_node) {
6527 dev_err(&pdev->dev, "missing phy\n");
6528 err = -ENODEV;
6529 goto err_free_netdev;
6530 }
6531
6532 phy_mode = of_get_phy_mode(port_node);
6533 if (phy_mode < 0) {
6534 dev_err(&pdev->dev, "incorrect phy mode\n");
6535 err = phy_mode;
6536 goto err_free_netdev;
6537 }
6538
6539 if (of_property_read_u32(port_node, "port-id", &id)) {
6540 err = -EINVAL;
6541 dev_err(&pdev->dev, "missing port-id value\n");
6542 goto err_free_netdev;
6543 }
6544
6545 dev->tx_queue_len = MVPP2_MAX_TXD;
6546 dev->watchdog_timeo = 5 * HZ;
6547 dev->netdev_ops = &mvpp2_netdev_ops;
6548 dev->ethtool_ops = &mvpp2_eth_tool_ops;
6549
6550 port = netdev_priv(dev);
6551
6552 port->irq = irq_of_parse_and_map(port_node, 0);
6553 if (port->irq <= 0) {
6554 err = -EINVAL;
6555 goto err_free_netdev;
6556 }
6557
6558 if (of_property_read_bool(port_node, "marvell,loopback"))
6559 port->flags |= MVPP2_F_LOOPBACK;
6560
6561 port->priv = priv;
6562 port->id = id;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006563 if (priv->hw_version == MVPP21)
6564 port->first_rxq = port->id * rxq_number;
6565 else
6566 port->first_rxq = port->id * priv->max_port_rxqs;
6567
Marcin Wojtas3f518502014-07-10 16:52:13 -03006568 port->phy_node = phy_node;
6569 port->phy_interface = phy_mode;
6570
Thomas Petazzonia7868412017-03-07 16:53:13 +01006571 if (priv->hw_version == MVPP21) {
6572 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
6573 port->base = devm_ioremap_resource(&pdev->dev, res);
6574 if (IS_ERR(port->base)) {
6575 err = PTR_ERR(port->base);
6576 goto err_free_irq;
6577 }
6578 } else {
6579 if (of_property_read_u32(port_node, "gop-port-id",
6580 &port->gop_id)) {
6581 err = -EINVAL;
6582 dev_err(&pdev->dev, "missing gop-port-id value\n");
6583 goto err_free_irq;
6584 }
6585
6586 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006587 }
6588
6589 /* Alloc per-cpu stats */
6590 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
6591 if (!port->stats) {
6592 err = -ENOMEM;
6593 goto err_free_irq;
6594 }
6595
6596 dt_mac_addr = of_get_mac_address(port_node);
6597 if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
6598 mac_from = "device tree";
6599 ether_addr_copy(dev->dev_addr, dt_mac_addr);
6600 } else {
Thomas Petazzoni26975822017-03-07 16:53:14 +01006601 if (priv->hw_version == MVPP21)
6602 mvpp21_get_mac_address(port, hw_mac_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006603 if (is_valid_ether_addr(hw_mac_addr)) {
6604 mac_from = "hardware";
6605 ether_addr_copy(dev->dev_addr, hw_mac_addr);
6606 } else {
6607 mac_from = "random";
6608 eth_hw_addr_random(dev);
6609 }
6610 }
6611
6612 port->tx_ring_size = MVPP2_MAX_TXD;
6613 port->rx_ring_size = MVPP2_MAX_RXD;
6614 port->dev = dev;
6615 SET_NETDEV_DEV(dev, &pdev->dev);
6616
6617 err = mvpp2_port_init(port);
6618 if (err < 0) {
6619 dev_err(&pdev->dev, "failed to init port %d\n", id);
6620 goto err_free_stats;
6621 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01006622
6623 mvpp2_port_mii_set(port);
6624 mvpp2_port_periodic_xon_disable(port);
6625
6626 if (priv->hw_version == MVPP21)
6627 mvpp2_port_fc_adv_enable(port);
6628
6629 mvpp2_port_reset(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006630
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006631 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
6632 if (!port->pcpu) {
6633 err = -ENOMEM;
6634 goto err_free_txq_pcpu;
6635 }
6636
6637 for_each_present_cpu(cpu) {
6638 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
6639
6640 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
6641 HRTIMER_MODE_REL_PINNED);
6642 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
6643 port_pcpu->timer_scheduled = false;
6644
6645 tasklet_init(&port_pcpu->tx_done_tasklet, mvpp2_tx_proc_cb,
6646 (unsigned long)dev);
6647 }
6648
Marcin Wojtas3f518502014-07-10 16:52:13 -03006649 netif_napi_add(dev, &port->napi, mvpp2_poll, NAPI_POLL_WEIGHT);
6650 features = NETIF_F_SG | NETIF_F_IP_CSUM;
6651 dev->features = features | NETIF_F_RXCSUM;
6652 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
6653 dev->vlan_features |= features;
6654
Jarod Wilson57779872016-10-17 15:54:06 -04006655 /* MTU range: 68 - 9676 */
6656 dev->min_mtu = ETH_MIN_MTU;
6657 /* 9676 == 9700 - 20 and rounding to 8 */
6658 dev->max_mtu = 9676;
6659
Marcin Wojtas3f518502014-07-10 16:52:13 -03006660 err = register_netdev(dev);
6661 if (err < 0) {
6662 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006663 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006664 }
6665 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
6666
Marcin Wojtas3f518502014-07-10 16:52:13 -03006667 priv->port_list[id] = port;
6668 return 0;
6669
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006670err_free_port_pcpu:
6671 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006672err_free_txq_pcpu:
6673 for (i = 0; i < txq_number; i++)
6674 free_percpu(port->txqs[i]->pcpu);
6675err_free_stats:
6676 free_percpu(port->stats);
6677err_free_irq:
6678 irq_dispose_mapping(port->irq);
6679err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08006680 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006681 free_netdev(dev);
6682 return err;
6683}
6684
6685/* Ports removal routine */
6686static void mvpp2_port_remove(struct mvpp2_port *port)
6687{
6688 int i;
6689
6690 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08006691 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006692 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006693 free_percpu(port->stats);
6694 for (i = 0; i < txq_number; i++)
6695 free_percpu(port->txqs[i]->pcpu);
6696 irq_dispose_mapping(port->irq);
6697 free_netdev(port->dev);
6698}
6699
6700/* Initialize decoding windows */
6701static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
6702 struct mvpp2 *priv)
6703{
6704 u32 win_enable;
6705 int i;
6706
6707 for (i = 0; i < 6; i++) {
6708 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
6709 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
6710
6711 if (i < 4)
6712 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
6713 }
6714
6715 win_enable = 0;
6716
6717 for (i = 0; i < dram->num_cs; i++) {
6718 const struct mbus_dram_window *cs = dram->cs + i;
6719
6720 mvpp2_write(priv, MVPP2_WIN_BASE(i),
6721 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
6722 dram->mbus_dram_target_id);
6723
6724 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
6725 (cs->size - 1) & 0xffff0000);
6726
6727 win_enable |= (1 << i);
6728 }
6729
6730 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
6731}
6732
6733/* Initialize Rx FIFO's */
6734static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
6735{
6736 int port;
6737
6738 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
6739 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
6740 MVPP2_RX_FIFO_PORT_DATA_SIZE);
6741 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
6742 MVPP2_RX_FIFO_PORT_ATTR_SIZE);
6743 }
6744
6745 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
6746 MVPP2_RX_FIFO_PORT_MIN_PKT);
6747 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
6748}
6749
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01006750static void mvpp2_axi_init(struct mvpp2 *priv)
6751{
6752 u32 val, rdval, wrval;
6753
6754 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
6755
6756 /* AXI Bridge Configuration */
6757
6758 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
6759 << MVPP22_AXI_ATTR_CACHE_OFFS;
6760 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6761 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
6762
6763 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
6764 << MVPP22_AXI_ATTR_CACHE_OFFS;
6765 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6766 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
6767
6768 /* BM */
6769 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
6770 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
6771
6772 /* Descriptors */
6773 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
6774 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
6775 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
6776 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
6777
6778 /* Buffer Data */
6779 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
6780 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
6781
6782 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
6783 << MVPP22_AXI_CODE_CACHE_OFFS;
6784 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
6785 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6786 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
6787 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
6788
6789 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
6790 << MVPP22_AXI_CODE_CACHE_OFFS;
6791 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6792 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6793
6794 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
6795
6796 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
6797 << MVPP22_AXI_CODE_CACHE_OFFS;
6798 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6799 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6800
6801 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
6802}
6803
Marcin Wojtas3f518502014-07-10 16:52:13 -03006804/* Initialize network controller common part HW */
6805static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
6806{
6807 const struct mbus_dram_target_info *dram_target_info;
6808 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03006809 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006810
6811 /* Checks for hardware constraints */
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006812 if (rxq_number % 4 || (rxq_number > priv->max_port_rxqs) ||
Marcin Wojtas3f518502014-07-10 16:52:13 -03006813 (txq_number > MVPP2_MAX_TXQ)) {
6814 dev_err(&pdev->dev, "invalid queue size parameter\n");
6815 return -EINVAL;
6816 }
6817
6818 /* MBUS windows configuration */
6819 dram_target_info = mv_mbus_dram_info();
6820 if (dram_target_info)
6821 mvpp2_conf_mbus_windows(dram_target_info, priv);
6822
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01006823 if (priv->hw_version == MVPP22)
6824 mvpp2_axi_init(priv);
6825
Marcin Wojtas08a23752014-07-21 13:48:12 -03006826 /* Disable HW PHY polling */
Thomas Petazzoni26975822017-03-07 16:53:14 +01006827 if (priv->hw_version == MVPP21) {
6828 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6829 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
6830 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6831 } else {
6832 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
6833 val &= ~MVPP22_SMI_POLLING_EN;
6834 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
6835 }
Marcin Wojtas08a23752014-07-21 13:48:12 -03006836
Marcin Wojtas3f518502014-07-10 16:52:13 -03006837 /* Allocate and initialize aggregated TXQs */
6838 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
Markus Elfringd7ce3ce2017-04-17 08:48:23 +02006839 sizeof(*priv->aggr_txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03006840 GFP_KERNEL);
6841 if (!priv->aggr_txqs)
6842 return -ENOMEM;
6843
6844 for_each_present_cpu(i) {
6845 priv->aggr_txqs[i].id = i;
6846 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
6847 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i],
6848 MVPP2_AGGR_TXQ_SIZE, i, priv);
6849 if (err < 0)
6850 return err;
6851 }
6852
6853 /* Rx Fifo Init */
6854 mvpp2_rx_fifo_init(priv);
6855
6856 /* Reset Rx queue group interrupt configuration */
Thomas Petazzonia73fef12017-03-07 16:53:16 +01006857 for (i = 0; i < MVPP2_MAX_PORTS; i++) {
6858 if (priv->hw_version == MVPP21) {
6859 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(i),
6860 rxq_number);
6861 continue;
6862 } else {
6863 u32 val;
6864
6865 val = (i << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET);
6866 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
6867
6868 val = (rxq_number << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET);
6869 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
6870 }
6871 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006872
Thomas Petazzoni26975822017-03-07 16:53:14 +01006873 if (priv->hw_version == MVPP21)
6874 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
6875 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006876
6877 /* Allow cache snoop when transmiting packets */
6878 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
6879
6880 /* Buffer Manager initialization */
6881 err = mvpp2_bm_init(pdev, priv);
6882 if (err < 0)
6883 return err;
6884
6885 /* Parser default initialization */
6886 err = mvpp2_prs_default_init(pdev, priv);
6887 if (err < 0)
6888 return err;
6889
6890 /* Classifier default initialization */
6891 mvpp2_cls_init(priv);
6892
6893 return 0;
6894}
6895
6896static int mvpp2_probe(struct platform_device *pdev)
6897{
6898 struct device_node *dn = pdev->dev.of_node;
6899 struct device_node *port_node;
6900 struct mvpp2 *priv;
6901 struct resource *res;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006902 void __iomem *base;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006903 int port_count, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006904 int err;
6905
Markus Elfring0b92e592017-04-17 08:38:32 +02006906 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006907 if (!priv)
6908 return -ENOMEM;
6909
Thomas Petazzonifaca9242017-03-07 16:53:06 +01006910 priv->hw_version =
6911 (unsigned long)of_device_get_match_data(&pdev->dev);
6912
Marcin Wojtas3f518502014-07-10 16:52:13 -03006913 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01006914 base = devm_ioremap_resource(&pdev->dev, res);
6915 if (IS_ERR(base))
6916 return PTR_ERR(base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006917
Thomas Petazzonia7868412017-03-07 16:53:13 +01006918 if (priv->hw_version == MVPP21) {
6919 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
6920 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
6921 if (IS_ERR(priv->lms_base))
6922 return PTR_ERR(priv->lms_base);
6923 } else {
6924 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
6925 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
6926 if (IS_ERR(priv->iface_base))
6927 return PTR_ERR(priv->iface_base);
6928 }
6929
6930 for_each_present_cpu(cpu) {
6931 u32 addr_space_sz;
6932
6933 addr_space_sz = (priv->hw_version == MVPP21 ?
6934 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
6935 priv->cpu_base[cpu] = base + cpu * addr_space_sz;
6936 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006937
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006938 if (priv->hw_version == MVPP21)
6939 priv->max_port_rxqs = 8;
6940 else
6941 priv->max_port_rxqs = 32;
6942
Marcin Wojtas3f518502014-07-10 16:52:13 -03006943 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
6944 if (IS_ERR(priv->pp_clk))
6945 return PTR_ERR(priv->pp_clk);
6946 err = clk_prepare_enable(priv->pp_clk);
6947 if (err < 0)
6948 return err;
6949
6950 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
6951 if (IS_ERR(priv->gop_clk)) {
6952 err = PTR_ERR(priv->gop_clk);
6953 goto err_pp_clk;
6954 }
6955 err = clk_prepare_enable(priv->gop_clk);
6956 if (err < 0)
6957 goto err_pp_clk;
6958
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006959 if (priv->hw_version == MVPP22) {
6960 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
6961 if (IS_ERR(priv->mg_clk)) {
6962 err = PTR_ERR(priv->mg_clk);
6963 goto err_gop_clk;
6964 }
6965
6966 err = clk_prepare_enable(priv->mg_clk);
6967 if (err < 0)
6968 goto err_gop_clk;
6969 }
6970
Marcin Wojtas3f518502014-07-10 16:52:13 -03006971 /* Get system's tclk rate */
6972 priv->tclk = clk_get_rate(priv->pp_clk);
6973
Thomas Petazzoni2067e0a2017-03-07 16:53:19 +01006974 if (priv->hw_version == MVPP22) {
6975 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
6976 if (err)
6977 goto err_mg_clk;
6978 /* Sadly, the BM pools all share the same register to
6979 * store the high 32 bits of their address. So they
6980 * must all have the same high 32 bits, which forces
6981 * us to restrict coherent memory to DMA_BIT_MASK(32).
6982 */
6983 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
6984 if (err)
6985 goto err_mg_clk;
6986 }
6987
Marcin Wojtas3f518502014-07-10 16:52:13 -03006988 /* Initialize network controller */
6989 err = mvpp2_init(pdev, priv);
6990 if (err < 0) {
6991 dev_err(&pdev->dev, "failed to initialize controller\n");
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006992 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006993 }
6994
6995 port_count = of_get_available_child_count(dn);
6996 if (port_count == 0) {
6997 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08006998 err = -ENODEV;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006999 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007000 }
7001
7002 priv->port_list = devm_kcalloc(&pdev->dev, port_count,
Markus Elfring0b92e592017-04-17 08:38:32 +02007003 sizeof(*priv->port_list),
7004 GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007005 if (!priv->port_list) {
7006 err = -ENOMEM;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007007 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007008 }
7009
7010 /* Initialize ports */
Marcin Wojtas3f518502014-07-10 16:52:13 -03007011 for_each_available_child_of_node(dn, port_node) {
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01007012 err = mvpp2_port_probe(pdev, port_node, priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007013 if (err < 0)
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007014 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03007015 }
7016
7017 platform_set_drvdata(pdev, priv);
7018 return 0;
7019
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007020err_mg_clk:
7021 if (priv->hw_version == MVPP22)
7022 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007023err_gop_clk:
7024 clk_disable_unprepare(priv->gop_clk);
7025err_pp_clk:
7026 clk_disable_unprepare(priv->pp_clk);
7027 return err;
7028}
7029
7030static int mvpp2_remove(struct platform_device *pdev)
7031{
7032 struct mvpp2 *priv = platform_get_drvdata(pdev);
7033 struct device_node *dn = pdev->dev.of_node;
7034 struct device_node *port_node;
7035 int i = 0;
7036
7037 for_each_available_child_of_node(dn, port_node) {
7038 if (priv->port_list[i])
7039 mvpp2_port_remove(priv->port_list[i]);
7040 i++;
7041 }
7042
7043 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
7044 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
7045
7046 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
7047 }
7048
7049 for_each_present_cpu(i) {
7050 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
7051
7052 dma_free_coherent(&pdev->dev,
7053 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
7054 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01007055 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007056 }
7057
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007058 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007059 clk_disable_unprepare(priv->pp_clk);
7060 clk_disable_unprepare(priv->gop_clk);
7061
7062 return 0;
7063}
7064
7065static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01007066 {
7067 .compatible = "marvell,armada-375-pp2",
7068 .data = (void *)MVPP21,
7069 },
Thomas Petazzonifc5e1552017-03-07 16:53:20 +01007070 {
7071 .compatible = "marvell,armada-7k-pp22",
7072 .data = (void *)MVPP22,
7073 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03007074 { }
7075};
7076MODULE_DEVICE_TABLE(of, mvpp2_match);
7077
7078static struct platform_driver mvpp2_driver = {
7079 .probe = mvpp2_probe,
7080 .remove = mvpp2_remove,
7081 .driver = {
7082 .name = MVPP2_DRIVER_NAME,
7083 .of_match_table = mvpp2_match,
7084 },
7085};
7086
7087module_platform_driver(mvpp2_driver);
7088
7089MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
7090MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03007091MODULE_LICENSE("GPL v2");