blob: 84908aa883749d397d5e971d8117cee82d0cb9f3 [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 Petazzonieb1e93a2017-08-03 10:41:55 +0200188#define MVPP21_ISR_RXQ_GROUP_REG(port) (0x5400 + 4 * (port))
Thomas Petazzonia73fef12017-03-07 16:53:16 +0100189
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;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200771 unsigned int nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300772 struct mvpp2_tx_queue **txqs;
Thomas Petazzoni09f83972017-08-03 10:41:57 +0200773 unsigned int ntxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300774 struct net_device *dev;
775
776 int pkt_size;
777
778 u32 pending_cause_rx;
779 struct napi_struct napi;
780
Marcin Wojtasedc660f2015-08-06 19:00:30 +0200781 /* Per-CPU port control */
782 struct mvpp2_port_pcpu __percpu *pcpu;
783
Marcin Wojtas3f518502014-07-10 16:52:13 -0300784 /* Flags */
785 unsigned long flags;
786
787 u16 tx_ring_size;
788 u16 rx_ring_size;
789 struct mvpp2_pcpu_stats __percpu *stats;
790
Marcin Wojtas3f518502014-07-10 16:52:13 -0300791 phy_interface_t phy_interface;
792 struct device_node *phy_node;
793 unsigned int link;
794 unsigned int duplex;
795 unsigned int speed;
796
797 struct mvpp2_bm_pool *pool_long;
798 struct mvpp2_bm_pool *pool_short;
799
800 /* Index of first port's physical RXQ */
801 u8 first_rxq;
802};
803
804/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
805 * layout of the transmit and reception DMA descriptors, and their
806 * layout is therefore defined by the hardware design
807 */
808
809#define MVPP2_TXD_L3_OFF_SHIFT 0
810#define MVPP2_TXD_IP_HLEN_SHIFT 8
811#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
812#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
813#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
814#define MVPP2_TXD_PADDING_DISABLE BIT(23)
815#define MVPP2_TXD_L4_UDP BIT(24)
816#define MVPP2_TXD_L3_IP6 BIT(26)
817#define MVPP2_TXD_L_DESC BIT(28)
818#define MVPP2_TXD_F_DESC BIT(29)
819
820#define MVPP2_RXD_ERR_SUMMARY BIT(15)
821#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
822#define MVPP2_RXD_ERR_CRC 0x0
823#define MVPP2_RXD_ERR_OVERRUN BIT(13)
824#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
825#define MVPP2_RXD_BM_POOL_ID_OFFS 16
826#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
827#define MVPP2_RXD_HWF_SYNC BIT(21)
828#define MVPP2_RXD_L4_CSUM_OK BIT(22)
829#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
830#define MVPP2_RXD_L4_TCP BIT(25)
831#define MVPP2_RXD_L4_UDP BIT(26)
832#define MVPP2_RXD_L3_IP4 BIT(28)
833#define MVPP2_RXD_L3_IP6 BIT(30)
834#define MVPP2_RXD_BUF_HDR BIT(31)
835
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100836/* HW TX descriptor for PPv2.1 */
837struct mvpp21_tx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300838 u32 command; /* Options used by HW for packet transmitting.*/
839 u8 packet_offset; /* the offset from the buffer beginning */
840 u8 phys_txq; /* destination queue ID */
841 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100842 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300843 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
844 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
845 u32 reserved2; /* reserved (for future use) */
846};
847
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100848/* HW RX descriptor for PPv2.1 */
849struct mvpp21_rx_desc {
Marcin Wojtas3f518502014-07-10 16:52:13 -0300850 u32 status; /* info about received packet */
851 u16 reserved1; /* parser_info (for future use, PnC) */
852 u16 data_size; /* size of received packet in bytes */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100853 u32 buf_dma_addr; /* physical address of the buffer */
Marcin Wojtas3f518502014-07-10 16:52:13 -0300854 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
855 u16 reserved2; /* gem_port_id (for future use, PON) */
856 u16 reserved3; /* csum_l4 (for future use, PnC) */
857 u8 reserved4; /* bm_qset (for future use, BM) */
858 u8 reserved5;
859 u16 reserved6; /* classify_info (for future use, PnC) */
860 u32 reserved7; /* flow_id (for future use, PnC) */
861 u32 reserved8;
862};
863
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100864/* HW TX descriptor for PPv2.2 */
865struct mvpp22_tx_desc {
866 u32 command;
867 u8 packet_offset;
868 u8 phys_txq;
869 u16 data_size;
870 u64 reserved1;
871 u64 buf_dma_addr_ptp;
872 u64 buf_cookie_misc;
873};
874
875/* HW RX descriptor for PPv2.2 */
876struct mvpp22_rx_desc {
877 u32 status;
878 u16 reserved1;
879 u16 data_size;
880 u32 reserved2;
881 u32 reserved3;
882 u64 buf_dma_addr_key_hash;
883 u64 buf_cookie_misc;
884};
885
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100886/* Opaque type used by the driver to manipulate the HW TX and RX
887 * descriptors
888 */
889struct mvpp2_tx_desc {
890 union {
891 struct mvpp21_tx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100892 struct mvpp22_tx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100893 };
894};
895
896struct mvpp2_rx_desc {
897 union {
898 struct mvpp21_rx_desc pp21;
Thomas Petazzonie7c53592017-03-07 16:53:08 +0100899 struct mvpp22_rx_desc pp22;
Thomas Petazzoni054f6372017-03-07 16:53:07 +0100900 };
901};
902
Thomas Petazzoni83544912016-12-21 11:28:49 +0100903struct mvpp2_txq_pcpu_buf {
904 /* Transmitted SKB */
905 struct sk_buff *skb;
906
907 /* Physical address of transmitted buffer */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100908 dma_addr_t dma;
Thomas Petazzoni83544912016-12-21 11:28:49 +0100909
910 /* Size transmitted */
911 size_t size;
912};
913
Marcin Wojtas3f518502014-07-10 16:52:13 -0300914/* Per-CPU Tx queue control */
915struct mvpp2_txq_pcpu {
916 int cpu;
917
918 /* Number of Tx DMA descriptors in the descriptor ring */
919 int size;
920
921 /* Number of currently used Tx DMA descriptor in the
922 * descriptor ring
923 */
924 int count;
925
926 /* Number of Tx DMA descriptors reserved for each CPU */
927 int reserved_num;
928
Thomas Petazzoni83544912016-12-21 11:28:49 +0100929 /* Infos about transmitted buffers */
930 struct mvpp2_txq_pcpu_buf *buffs;
Marcin Wojtas71ce3912015-08-06 19:00:29 +0200931
Marcin Wojtas3f518502014-07-10 16:52:13 -0300932 /* Index of last TX DMA descriptor that was inserted */
933 int txq_put_index;
934
935 /* Index of the TX DMA descriptor to be cleaned up */
936 int txq_get_index;
937};
938
939struct mvpp2_tx_queue {
940 /* Physical number of this Tx queue */
941 u8 id;
942
943 /* Logical number of this Tx queue */
944 u8 log_id;
945
946 /* Number of Tx DMA descriptors in the descriptor ring */
947 int size;
948
949 /* Number of currently used Tx DMA descriptor in the descriptor ring */
950 int count;
951
952 /* Per-CPU control of physical Tx queues */
953 struct mvpp2_txq_pcpu __percpu *pcpu;
954
Marcin Wojtas3f518502014-07-10 16:52:13 -0300955 u32 done_pkts_coal;
956
957 /* Virtual address of thex Tx DMA descriptors array */
958 struct mvpp2_tx_desc *descs;
959
960 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100961 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300962
963 /* Index of the last Tx DMA descriptor */
964 int last_desc;
965
966 /* Index of the next Tx DMA descriptor to process */
967 int next_desc_to_proc;
968};
969
970struct mvpp2_rx_queue {
971 /* RX queue number, in the range 0-31 for physical RXQs */
972 u8 id;
973
974 /* Num of rx descriptors in the rx descriptor ring */
975 int size;
976
977 u32 pkts_coal;
978 u32 time_coal;
979
980 /* Virtual address of the RX DMA descriptors array */
981 struct mvpp2_rx_desc *descs;
982
983 /* DMA address of the RX DMA descriptors array */
Thomas Petazzoni20396132017-03-07 16:53:00 +0100984 dma_addr_t descs_dma;
Marcin Wojtas3f518502014-07-10 16:52:13 -0300985
986 /* Index of the last RX DMA descriptor */
987 int last_desc;
988
989 /* Index of the next RX DMA descriptor to process */
990 int next_desc_to_proc;
991
992 /* ID of port to which physical RXQ is mapped */
993 int port;
994
995 /* Port's logic RXQ number to which physical RXQ is mapped */
996 int logic_rxq;
997};
998
999union mvpp2_prs_tcam_entry {
1000 u32 word[MVPP2_PRS_TCAM_WORDS];
1001 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
1002};
1003
1004union mvpp2_prs_sram_entry {
1005 u32 word[MVPP2_PRS_SRAM_WORDS];
1006 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
1007};
1008
1009struct mvpp2_prs_entry {
1010 u32 index;
1011 union mvpp2_prs_tcam_entry tcam;
1012 union mvpp2_prs_sram_entry sram;
1013};
1014
1015struct mvpp2_prs_shadow {
1016 bool valid;
1017 bool finish;
1018
1019 /* Lookup ID */
1020 int lu;
1021
1022 /* User defined offset */
1023 int udf;
1024
1025 /* Result info */
1026 u32 ri;
1027 u32 ri_mask;
1028};
1029
1030struct mvpp2_cls_flow_entry {
1031 u32 index;
1032 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1033};
1034
1035struct mvpp2_cls_lookup_entry {
1036 u32 lkpid;
1037 u32 way;
1038 u32 data;
1039};
1040
1041struct mvpp2_bm_pool {
1042 /* Pool number in the range 0-7 */
1043 int id;
1044 enum mvpp2_bm_type type;
1045
1046 /* Buffer Pointers Pool External (BPPE) size */
1047 int size;
Thomas Petazzonid01524d2017-03-07 16:53:09 +01001048 /* BPPE size in bytes */
1049 int size_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001050 /* Number of buffers for this pool */
1051 int buf_num;
1052 /* Pool buffer size */
1053 int buf_size;
1054 /* Packet size */
1055 int pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01001056 int frag_size;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001057
1058 /* BPPE virtual base address */
1059 u32 *virt_addr;
Thomas Petazzoni20396132017-03-07 16:53:00 +01001060 /* BPPE DMA base address */
1061 dma_addr_t dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001062
1063 /* Ports using BM pool */
1064 u32 port_map;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001065};
1066
Marcin Wojtas3f518502014-07-10 16:52:13 -03001067#define MVPP2_DRIVER_NAME "mvpp2"
1068#define MVPP2_DRIVER_VERSION "1.0"
1069
1070/* Utility/helper methods */
1071
1072static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1073{
Thomas Petazzonia7868412017-03-07 16:53:13 +01001074 writel(data, priv->cpu_base[0] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001075}
1076
1077static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1078{
Thomas Petazzonia7868412017-03-07 16:53:13 +01001079 return readl(priv->cpu_base[0] + offset);
1080}
1081
1082/* These accessors should be used to access:
1083 *
1084 * - per-CPU registers, where each CPU has its own copy of the
1085 * register.
1086 *
1087 * MVPP2_BM_VIRT_ALLOC_REG
1088 * MVPP2_BM_ADDR_HIGH_ALLOC
1089 * MVPP22_BM_ADDR_HIGH_RLS_REG
1090 * MVPP2_BM_VIRT_RLS_REG
1091 * MVPP2_ISR_RX_TX_CAUSE_REG
1092 * MVPP2_ISR_RX_TX_MASK_REG
1093 * MVPP2_TXQ_NUM_REG
1094 * MVPP2_AGGR_TXQ_UPDATE_REG
1095 * MVPP2_TXQ_RSVD_REQ_REG
1096 * MVPP2_TXQ_RSVD_RSLT_REG
1097 * MVPP2_TXQ_SENT_REG
1098 * MVPP2_RXQ_NUM_REG
1099 *
1100 * - global registers that must be accessed through a specific CPU
1101 * window, because they are related to an access to a per-CPU
1102 * register
1103 *
1104 * MVPP2_BM_PHY_ALLOC_REG (related to MVPP2_BM_VIRT_ALLOC_REG)
1105 * MVPP2_BM_PHY_RLS_REG (related to MVPP2_BM_VIRT_RLS_REG)
1106 * MVPP2_RXQ_THRESH_REG (related to MVPP2_RXQ_NUM_REG)
1107 * MVPP2_RXQ_DESC_ADDR_REG (related to MVPP2_RXQ_NUM_REG)
1108 * MVPP2_RXQ_DESC_SIZE_REG (related to MVPP2_RXQ_NUM_REG)
1109 * MVPP2_RXQ_INDEX_REG (related to MVPP2_RXQ_NUM_REG)
1110 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1111 * MVPP2_TXQ_DESC_ADDR_REG (related to MVPP2_TXQ_NUM_REG)
1112 * MVPP2_TXQ_DESC_SIZE_REG (related to MVPP2_TXQ_NUM_REG)
1113 * MVPP2_TXQ_INDEX_REG (related to MVPP2_TXQ_NUM_REG)
1114 * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
1115 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1116 * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
1117 */
1118static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
1119 u32 offset, u32 data)
1120{
1121 writel(data, priv->cpu_base[cpu] + offset);
1122}
1123
1124static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
1125 u32 offset)
1126{
1127 return readl(priv->cpu_base[cpu] + offset);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001128}
1129
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001130static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1131 struct mvpp2_tx_desc *tx_desc)
1132{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001133 if (port->priv->hw_version == MVPP21)
1134 return tx_desc->pp21.buf_dma_addr;
1135 else
1136 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001137}
1138
1139static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1140 struct mvpp2_tx_desc *tx_desc,
1141 dma_addr_t dma_addr)
1142{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001143 if (port->priv->hw_version == MVPP21) {
1144 tx_desc->pp21.buf_dma_addr = dma_addr;
1145 } else {
1146 u64 val = (u64)dma_addr;
1147
1148 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1149 tx_desc->pp22.buf_dma_addr_ptp |= val;
1150 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001151}
1152
1153static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
1154 struct mvpp2_tx_desc *tx_desc)
1155{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001156 if (port->priv->hw_version == MVPP21)
1157 return tx_desc->pp21.data_size;
1158 else
1159 return tx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001160}
1161
1162static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1163 struct mvpp2_tx_desc *tx_desc,
1164 size_t size)
1165{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001166 if (port->priv->hw_version == MVPP21)
1167 tx_desc->pp21.data_size = size;
1168 else
1169 tx_desc->pp22.data_size = size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001170}
1171
1172static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1173 struct mvpp2_tx_desc *tx_desc,
1174 unsigned int txq)
1175{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001176 if (port->priv->hw_version == MVPP21)
1177 tx_desc->pp21.phys_txq = txq;
1178 else
1179 tx_desc->pp22.phys_txq = txq;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001180}
1181
1182static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1183 struct mvpp2_tx_desc *tx_desc,
1184 unsigned int command)
1185{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001186 if (port->priv->hw_version == MVPP21)
1187 tx_desc->pp21.command = command;
1188 else
1189 tx_desc->pp22.command = command;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001190}
1191
1192static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
1193 struct mvpp2_tx_desc *tx_desc,
1194 unsigned int offset)
1195{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001196 if (port->priv->hw_version == MVPP21)
1197 tx_desc->pp21.packet_offset = offset;
1198 else
1199 tx_desc->pp22.packet_offset = offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001200}
1201
1202static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1203 struct mvpp2_tx_desc *tx_desc)
1204{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001205 if (port->priv->hw_version == MVPP21)
1206 return tx_desc->pp21.packet_offset;
1207 else
1208 return tx_desc->pp22.packet_offset;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001209}
1210
1211static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1212 struct mvpp2_rx_desc *rx_desc)
1213{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001214 if (port->priv->hw_version == MVPP21)
1215 return rx_desc->pp21.buf_dma_addr;
1216 else
1217 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001218}
1219
1220static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1221 struct mvpp2_rx_desc *rx_desc)
1222{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001223 if (port->priv->hw_version == MVPP21)
1224 return rx_desc->pp21.buf_cookie;
1225 else
1226 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001227}
1228
1229static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1230 struct mvpp2_rx_desc *rx_desc)
1231{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001232 if (port->priv->hw_version == MVPP21)
1233 return rx_desc->pp21.data_size;
1234 else
1235 return rx_desc->pp22.data_size;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001236}
1237
1238static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1239 struct mvpp2_rx_desc *rx_desc)
1240{
Thomas Petazzonie7c53592017-03-07 16:53:08 +01001241 if (port->priv->hw_version == MVPP21)
1242 return rx_desc->pp21.status;
1243 else
1244 return rx_desc->pp22.status;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001245}
1246
Marcin Wojtas3f518502014-07-10 16:52:13 -03001247static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1248{
1249 txq_pcpu->txq_get_index++;
1250 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1251 txq_pcpu->txq_get_index = 0;
1252}
1253
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001254static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1255 struct mvpp2_txq_pcpu *txq_pcpu,
Marcin Wojtas71ce3912015-08-06 19:00:29 +02001256 struct sk_buff *skb,
1257 struct mvpp2_tx_desc *tx_desc)
Marcin Wojtas3f518502014-07-10 16:52:13 -03001258{
Thomas Petazzoni83544912016-12-21 11:28:49 +01001259 struct mvpp2_txq_pcpu_buf *tx_buf =
1260 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1261 tx_buf->skb = skb;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01001262 tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1263 tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1264 mvpp2_txdesc_offset_get(port, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03001265 txq_pcpu->txq_put_index++;
1266 if (txq_pcpu->txq_put_index == txq_pcpu->size)
1267 txq_pcpu->txq_put_index = 0;
1268}
1269
1270/* Get number of physical egress port */
1271static inline int mvpp2_egress_port(struct mvpp2_port *port)
1272{
1273 return MVPP2_MAX_TCONT + port->id;
1274}
1275
1276/* Get number of physical TXQ */
1277static inline int mvpp2_txq_phys(int port, int txq)
1278{
1279 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1280}
1281
1282/* Parser configuration routines */
1283
1284/* Update parser tcam and sram hw entries */
1285static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1286{
1287 int i;
1288
1289 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1290 return -EINVAL;
1291
1292 /* Clear entry invalidation bit */
1293 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1294
1295 /* Write tcam index - indirect access */
1296 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1297 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1298 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1299
1300 /* Write sram index - indirect access */
1301 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1302 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1303 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1304
1305 return 0;
1306}
1307
1308/* Read tcam entry from hw */
1309static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1310{
1311 int i;
1312
1313 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1314 return -EINVAL;
1315
1316 /* Write tcam index - indirect access */
1317 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1318
1319 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1320 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1321 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1322 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1323
1324 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1325 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1326
1327 /* Write sram index - indirect access */
1328 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1329 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1330 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1331
1332 return 0;
1333}
1334
1335/* Invalidate tcam hw entry */
1336static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1337{
1338 /* Write index - indirect access */
1339 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1340 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1341 MVPP2_PRS_TCAM_INV_MASK);
1342}
1343
1344/* Enable shadow table entry and set its lookup ID */
1345static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1346{
1347 priv->prs_shadow[index].valid = true;
1348 priv->prs_shadow[index].lu = lu;
1349}
1350
1351/* Update ri fields in shadow table entry */
1352static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1353 unsigned int ri, unsigned int ri_mask)
1354{
1355 priv->prs_shadow[index].ri_mask = ri_mask;
1356 priv->prs_shadow[index].ri = ri;
1357}
1358
1359/* Update lookup field in tcam sw entry */
1360static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1361{
1362 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1363
1364 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1365 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1366}
1367
1368/* Update mask for single port in tcam sw entry */
1369static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1370 unsigned int port, bool add)
1371{
1372 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1373
1374 if (add)
1375 pe->tcam.byte[enable_off] &= ~(1 << port);
1376 else
1377 pe->tcam.byte[enable_off] |= 1 << port;
1378}
1379
1380/* Update port map in tcam sw entry */
1381static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1382 unsigned int ports)
1383{
1384 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1385 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1386
1387 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1388 pe->tcam.byte[enable_off] &= ~port_mask;
1389 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1390}
1391
1392/* Obtain port map from tcam sw entry */
1393static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1394{
1395 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1396
1397 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1398}
1399
1400/* Set byte of data and its enable bits in tcam sw entry */
1401static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1402 unsigned int offs, unsigned char byte,
1403 unsigned char enable)
1404{
1405 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1406 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1407}
1408
1409/* Get byte of data and its enable bits from tcam sw entry */
1410static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1411 unsigned int offs, unsigned char *byte,
1412 unsigned char *enable)
1413{
1414 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1415 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1416}
1417
1418/* Compare tcam data bytes with a pattern */
1419static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1420 u16 data)
1421{
1422 int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1423 u16 tcam_data;
1424
1425 tcam_data = (8 << pe->tcam.byte[off + 1]) | pe->tcam.byte[off];
1426 if (tcam_data != data)
1427 return false;
1428 return true;
1429}
1430
1431/* Update ai bits in tcam sw entry */
1432static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1433 unsigned int bits, unsigned int enable)
1434{
1435 int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1436
1437 for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1438
1439 if (!(enable & BIT(i)))
1440 continue;
1441
1442 if (bits & BIT(i))
1443 pe->tcam.byte[ai_idx] |= 1 << i;
1444 else
1445 pe->tcam.byte[ai_idx] &= ~(1 << i);
1446 }
1447
1448 pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1449}
1450
1451/* Get ai bits from tcam sw entry */
1452static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1453{
1454 return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1455}
1456
1457/* Set ethertype in tcam sw entry */
1458static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1459 unsigned short ethertype)
1460{
1461 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1462 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1463}
1464
1465/* Set bits in sram sw entry */
1466static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1467 int val)
1468{
1469 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1470}
1471
1472/* Clear bits in sram sw entry */
1473static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1474 int val)
1475{
1476 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1477}
1478
1479/* Update ri bits in sram sw entry */
1480static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1481 unsigned int bits, unsigned int mask)
1482{
1483 unsigned int i;
1484
1485 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1486 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1487
1488 if (!(mask & BIT(i)))
1489 continue;
1490
1491 if (bits & BIT(i))
1492 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1493 else
1494 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1495
1496 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1497 }
1498}
1499
1500/* Obtain ri bits from sram sw entry */
1501static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1502{
1503 return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1504}
1505
1506/* Update ai bits in sram sw entry */
1507static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1508 unsigned int bits, unsigned int mask)
1509{
1510 unsigned int i;
1511 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1512
1513 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1514
1515 if (!(mask & BIT(i)))
1516 continue;
1517
1518 if (bits & BIT(i))
1519 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1520 else
1521 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1522
1523 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1524 }
1525}
1526
1527/* Read ai bits from sram sw entry */
1528static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1529{
1530 u8 bits;
1531 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1532 int ai_en_off = ai_off + 1;
1533 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1534
1535 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1536 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1537
1538 return bits;
1539}
1540
1541/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1542 * lookup interation
1543 */
1544static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1545 unsigned int lu)
1546{
1547 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1548
1549 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1550 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1551 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1552}
1553
1554/* In the sram sw entry set sign and value of the next lookup offset
1555 * and the offset value generated to the classifier
1556 */
1557static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1558 unsigned int op)
1559{
1560 /* Set sign */
1561 if (shift < 0) {
1562 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1563 shift = 0 - shift;
1564 } else {
1565 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1566 }
1567
1568 /* Set value */
1569 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1570 (unsigned char)shift;
1571
1572 /* Reset and set operation */
1573 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1574 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1575 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1576
1577 /* Set base offset as current */
1578 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1579}
1580
1581/* In the sram sw entry set sign and value of the user defined offset
1582 * generated to the classifier
1583 */
1584static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1585 unsigned int type, int offset,
1586 unsigned int op)
1587{
1588 /* Set sign */
1589 if (offset < 0) {
1590 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1591 offset = 0 - offset;
1592 } else {
1593 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1594 }
1595
1596 /* Set value */
1597 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1598 MVPP2_PRS_SRAM_UDF_MASK);
1599 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1600 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1601 MVPP2_PRS_SRAM_UDF_BITS)] &=
1602 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1603 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1604 MVPP2_PRS_SRAM_UDF_BITS)] |=
1605 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1606
1607 /* Set offset type */
1608 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1609 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1610 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1611
1612 /* Set offset operation */
1613 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1614 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1615 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1616
1617 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1618 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1619 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1620 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
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 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1625
1626 /* Set base offset as current */
1627 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1628}
1629
1630/* Find parser flow entry */
1631static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1632{
1633 struct mvpp2_prs_entry *pe;
1634 int tid;
1635
1636 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1637 if (!pe)
1638 return NULL;
1639 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1640
1641 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1642 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1643 u8 bits;
1644
1645 if (!priv->prs_shadow[tid].valid ||
1646 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1647 continue;
1648
1649 pe->index = tid;
1650 mvpp2_prs_hw_read(priv, pe);
1651 bits = mvpp2_prs_sram_ai_get(pe);
1652
1653 /* Sram store classification lookup ID in AI bits [5:0] */
1654 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1655 return pe;
1656 }
1657 kfree(pe);
1658
1659 return NULL;
1660}
1661
1662/* Return first free tcam index, seeking from start to end */
1663static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1664 unsigned char end)
1665{
1666 int tid;
1667
1668 if (start > end)
1669 swap(start, end);
1670
1671 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1672 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1673
1674 for (tid = start; tid <= end; tid++) {
1675 if (!priv->prs_shadow[tid].valid)
1676 return tid;
1677 }
1678
1679 return -EINVAL;
1680}
1681
1682/* Enable/disable dropping all mac da's */
1683static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1684{
1685 struct mvpp2_prs_entry pe;
1686
1687 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1688 /* Entry exist - update port only */
1689 pe.index = MVPP2_PE_DROP_ALL;
1690 mvpp2_prs_hw_read(priv, &pe);
1691 } else {
1692 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001693 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001694 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1695 pe.index = MVPP2_PE_DROP_ALL;
1696
1697 /* Non-promiscuous mode for all ports - DROP unknown packets */
1698 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1699 MVPP2_PRS_RI_DROP_MASK);
1700
1701 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1702 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1703
1704 /* Update shadow table */
1705 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1706
1707 /* Mask all ports */
1708 mvpp2_prs_tcam_port_map_set(&pe, 0);
1709 }
1710
1711 /* Update port mask */
1712 mvpp2_prs_tcam_port_set(&pe, port, add);
1713
1714 mvpp2_prs_hw_write(priv, &pe);
1715}
1716
1717/* Set port to promiscuous mode */
1718static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1719{
1720 struct mvpp2_prs_entry pe;
1721
Joe Perchesdbedd442015-03-06 20:49:12 -08001722 /* Promiscuous mode - Accept unknown packets */
Marcin Wojtas3f518502014-07-10 16:52:13 -03001723
1724 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1725 /* Entry exist - update port only */
1726 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1727 mvpp2_prs_hw_read(priv, &pe);
1728 } else {
1729 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001730 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001731 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1732 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1733
1734 /* Continue - set next lookup */
1735 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1736
1737 /* Set result info bits */
1738 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1739 MVPP2_PRS_RI_L2_CAST_MASK);
1740
1741 /* Shift to ethertype */
1742 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1743 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1744
1745 /* Mask all ports */
1746 mvpp2_prs_tcam_port_map_set(&pe, 0);
1747
1748 /* Update shadow table */
1749 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1750 }
1751
1752 /* Update port mask */
1753 mvpp2_prs_tcam_port_set(&pe, port, add);
1754
1755 mvpp2_prs_hw_write(priv, &pe);
1756}
1757
1758/* Accept multicast */
1759static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1760 bool add)
1761{
1762 struct mvpp2_prs_entry pe;
1763 unsigned char da_mc;
1764
1765 /* Ethernet multicast address first byte is
1766 * 0x01 for IPv4 and 0x33 for IPv6
1767 */
1768 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1769
1770 if (priv->prs_shadow[index].valid) {
1771 /* Entry exist - update port only */
1772 pe.index = index;
1773 mvpp2_prs_hw_read(priv, &pe);
1774 } else {
1775 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001776 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001777 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1778 pe.index = index;
1779
1780 /* Continue - set next lookup */
1781 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1782
1783 /* Set result info bits */
1784 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1785 MVPP2_PRS_RI_L2_CAST_MASK);
1786
1787 /* Update tcam entry data first byte */
1788 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1789
1790 /* Shift to ethertype */
1791 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1792 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1793
1794 /* Mask all ports */
1795 mvpp2_prs_tcam_port_map_set(&pe, 0);
1796
1797 /* Update shadow table */
1798 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1799 }
1800
1801 /* Update port mask */
1802 mvpp2_prs_tcam_port_set(&pe, port, add);
1803
1804 mvpp2_prs_hw_write(priv, &pe);
1805}
1806
1807/* Set entry for dsa packets */
1808static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
1809 bool tagged, bool extend)
1810{
1811 struct mvpp2_prs_entry pe;
1812 int tid, shift;
1813
1814 if (extend) {
1815 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
1816 shift = 8;
1817 } else {
1818 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
1819 shift = 4;
1820 }
1821
1822 if (priv->prs_shadow[tid].valid) {
1823 /* Entry exist - update port only */
1824 pe.index = tid;
1825 mvpp2_prs_hw_read(priv, &pe);
1826 } else {
1827 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001828 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001829 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1830 pe.index = tid;
1831
1832 /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
1833 mvpp2_prs_sram_shift_set(&pe, shift,
1834 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1835
1836 /* Update shadow table */
1837 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1838
1839 if (tagged) {
1840 /* Set tagged bit in DSA tag */
1841 mvpp2_prs_tcam_data_byte_set(&pe, 0,
1842 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1843 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1844 /* Clear all ai bits for next iteration */
1845 mvpp2_prs_sram_ai_update(&pe, 0,
1846 MVPP2_PRS_SRAM_AI_MASK);
1847 /* If packet is tagged continue check vlans */
1848 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1849 } else {
1850 /* Set result info bits to 'no vlans' */
1851 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1852 MVPP2_PRS_RI_VLAN_MASK);
1853 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1854 }
1855
1856 /* Mask all ports */
1857 mvpp2_prs_tcam_port_map_set(&pe, 0);
1858 }
1859
1860 /* Update port mask */
1861 mvpp2_prs_tcam_port_set(&pe, port, add);
1862
1863 mvpp2_prs_hw_write(priv, &pe);
1864}
1865
1866/* Set entry for dsa ethertype */
1867static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
1868 bool add, bool tagged, bool extend)
1869{
1870 struct mvpp2_prs_entry pe;
1871 int tid, shift, port_mask;
1872
1873 if (extend) {
1874 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
1875 MVPP2_PE_ETYPE_EDSA_UNTAGGED;
1876 port_mask = 0;
1877 shift = 8;
1878 } else {
1879 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
1880 MVPP2_PE_ETYPE_DSA_UNTAGGED;
1881 port_mask = MVPP2_PRS_PORT_MASK;
1882 shift = 4;
1883 }
1884
1885 if (priv->prs_shadow[tid].valid) {
1886 /* Entry exist - update port only */
1887 pe.index = tid;
1888 mvpp2_prs_hw_read(priv, &pe);
1889 } else {
1890 /* Entry doesn't exist - create new */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02001891 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03001892 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
1893 pe.index = tid;
1894
1895 /* Set ethertype */
1896 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
1897 mvpp2_prs_match_etype(&pe, 2, 0);
1898
1899 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
1900 MVPP2_PRS_RI_DSA_MASK);
1901 /* Shift ethertype + 2 byte reserved + tag*/
1902 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
1903 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1904
1905 /* Update shadow table */
1906 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
1907
1908 if (tagged) {
1909 /* Set tagged bit in DSA tag */
1910 mvpp2_prs_tcam_data_byte_set(&pe,
1911 MVPP2_ETH_TYPE_LEN + 2 + 3,
1912 MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
1913 MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
1914 /* Clear all ai bits for next iteration */
1915 mvpp2_prs_sram_ai_update(&pe, 0,
1916 MVPP2_PRS_SRAM_AI_MASK);
1917 /* If packet is tagged continue check vlans */
1918 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
1919 } else {
1920 /* Set result info bits to 'no vlans' */
1921 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
1922 MVPP2_PRS_RI_VLAN_MASK);
1923 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
1924 }
1925 /* Mask/unmask all ports, depending on dsa type */
1926 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
1927 }
1928
1929 /* Update port mask */
1930 mvpp2_prs_tcam_port_set(&pe, port, add);
1931
1932 mvpp2_prs_hw_write(priv, &pe);
1933}
1934
1935/* Search for existing single/triple vlan entry */
1936static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
1937 unsigned short tpid, int ai)
1938{
1939 struct mvpp2_prs_entry *pe;
1940 int tid;
1941
1942 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1943 if (!pe)
1944 return NULL;
1945 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
1946
1947 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
1948 for (tid = MVPP2_PE_FIRST_FREE_TID;
1949 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
1950 unsigned int ri_bits, ai_bits;
1951 bool match;
1952
1953 if (!priv->prs_shadow[tid].valid ||
1954 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
1955 continue;
1956
1957 pe->index = tid;
1958
1959 mvpp2_prs_hw_read(priv, pe);
1960 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
1961 if (!match)
1962 continue;
1963
1964 /* Get vlan type */
1965 ri_bits = mvpp2_prs_sram_ri_get(pe);
1966 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
1967
1968 /* Get current ai value from tcam */
1969 ai_bits = mvpp2_prs_tcam_ai_get(pe);
1970 /* Clear double vlan bit */
1971 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
1972
1973 if (ai != ai_bits)
1974 continue;
1975
1976 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
1977 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
1978 return pe;
1979 }
1980 kfree(pe);
1981
1982 return NULL;
1983}
1984
1985/* Add/update single/triple vlan entry */
1986static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
1987 unsigned int port_map)
1988{
1989 struct mvpp2_prs_entry *pe;
1990 int tid_aux, tid;
Sudip Mukherjee43737472014-11-01 16:59:34 +05301991 int ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03001992
1993 pe = mvpp2_prs_vlan_find(priv, tpid, ai);
1994
1995 if (!pe) {
1996 /* Create new tcam entry */
1997 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
1998 MVPP2_PE_FIRST_FREE_TID);
1999 if (tid < 0)
2000 return tid;
2001
2002 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2003 if (!pe)
2004 return -ENOMEM;
2005
2006 /* Get last double vlan tid */
2007 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
2008 tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
2009 unsigned int ri_bits;
2010
2011 if (!priv->prs_shadow[tid_aux].valid ||
2012 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2013 continue;
2014
2015 pe->index = tid_aux;
2016 mvpp2_prs_hw_read(priv, pe);
2017 ri_bits = mvpp2_prs_sram_ri_get(pe);
2018 if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
2019 MVPP2_PRS_RI_VLAN_DOUBLE)
2020 break;
2021 }
2022
Sudip Mukherjee43737472014-11-01 16:59:34 +05302023 if (tid <= tid_aux) {
2024 ret = -EINVAL;
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002025 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302026 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002027
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002028 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002029 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2030 pe->index = tid;
2031
2032 mvpp2_prs_match_etype(pe, 0, tpid);
2033
2034 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
2035 /* Shift 4 bytes - skip 1 vlan tag */
2036 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
2037 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2038 /* Clear all ai bits for next iteration */
2039 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2040
2041 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
2042 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
2043 MVPP2_PRS_RI_VLAN_MASK);
2044 } else {
2045 ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
2046 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
2047 MVPP2_PRS_RI_VLAN_MASK);
2048 }
2049 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
2050
2051 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2052 }
2053 /* Update ports' mask */
2054 mvpp2_prs_tcam_port_map_set(pe, port_map);
2055
2056 mvpp2_prs_hw_write(priv, pe);
Markus Elfringf9fd0e32017-04-17 13:50:35 +02002057free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002058 kfree(pe);
2059
Sudip Mukherjee43737472014-11-01 16:59:34 +05302060 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002061}
2062
2063/* Get first free double vlan ai number */
2064static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
2065{
2066 int i;
2067
2068 for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
2069 if (!priv->prs_double_vlans[i])
2070 return i;
2071 }
2072
2073 return -EINVAL;
2074}
2075
2076/* Search for existing double vlan entry */
2077static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
2078 unsigned short tpid1,
2079 unsigned short tpid2)
2080{
2081 struct mvpp2_prs_entry *pe;
2082 int tid;
2083
2084 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2085 if (!pe)
2086 return NULL;
2087 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2088
2089 /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2090 for (tid = MVPP2_PE_FIRST_FREE_TID;
2091 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2092 unsigned int ri_mask;
2093 bool match;
2094
2095 if (!priv->prs_shadow[tid].valid ||
2096 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2097 continue;
2098
2099 pe->index = tid;
2100 mvpp2_prs_hw_read(priv, pe);
2101
2102 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
2103 && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
2104
2105 if (!match)
2106 continue;
2107
2108 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
2109 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
2110 return pe;
2111 }
2112 kfree(pe);
2113
2114 return NULL;
2115}
2116
2117/* Add or update double vlan entry */
2118static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
2119 unsigned short tpid2,
2120 unsigned int port_map)
2121{
2122 struct mvpp2_prs_entry *pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302123 int tid_aux, tid, ai, ret = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002124
2125 pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2126
2127 if (!pe) {
2128 /* Create new tcam entry */
2129 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2130 MVPP2_PE_LAST_FREE_TID);
2131 if (tid < 0)
2132 return tid;
2133
2134 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2135 if (!pe)
2136 return -ENOMEM;
2137
2138 /* Set ai value for new double vlan entry */
2139 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302140 if (ai < 0) {
2141 ret = ai;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002142 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302143 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002144
2145 /* Get first single/triple vlan tid */
2146 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2147 tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2148 unsigned int ri_bits;
2149
2150 if (!priv->prs_shadow[tid_aux].valid ||
2151 priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2152 continue;
2153
2154 pe->index = tid_aux;
2155 mvpp2_prs_hw_read(priv, pe);
2156 ri_bits = mvpp2_prs_sram_ri_get(pe);
2157 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2158 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2159 ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2160 break;
2161 }
2162
Sudip Mukherjee43737472014-11-01 16:59:34 +05302163 if (tid >= tid_aux) {
2164 ret = -ERANGE;
Markus Elfringc9a7e122017-04-17 13:03:49 +02002165 goto free_pe;
Sudip Mukherjee43737472014-11-01 16:59:34 +05302166 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03002167
Markus Elfringbd6aaf52017-04-17 10:40:32 +02002168 memset(pe, 0, sizeof(*pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002169 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2170 pe->index = tid;
2171
2172 priv->prs_double_vlans[ai] = true;
2173
2174 mvpp2_prs_match_etype(pe, 0, tpid1);
2175 mvpp2_prs_match_etype(pe, 4, tpid2);
2176
2177 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
2178 /* Shift 8 bytes - skip 2 vlan tags */
2179 mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
2180 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2181 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2182 MVPP2_PRS_RI_VLAN_MASK);
2183 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2184 MVPP2_PRS_SRAM_AI_MASK);
2185
2186 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2187 }
2188
2189 /* Update ports' mask */
2190 mvpp2_prs_tcam_port_map_set(pe, port_map);
2191 mvpp2_prs_hw_write(priv, pe);
Markus Elfringc9a7e122017-04-17 13:03:49 +02002192free_pe:
Marcin Wojtas3f518502014-07-10 16:52:13 -03002193 kfree(pe);
Sudip Mukherjee43737472014-11-01 16:59:34 +05302194 return ret;
Marcin Wojtas3f518502014-07-10 16:52:13 -03002195}
2196
2197/* IPv4 header parsing for fragmentation and L4 offset */
2198static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2199 unsigned int ri, unsigned int ri_mask)
2200{
2201 struct mvpp2_prs_entry pe;
2202 int tid;
2203
2204 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2205 (proto != IPPROTO_IGMP))
2206 return -EINVAL;
2207
2208 /* Fragmented packet */
2209 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2210 MVPP2_PE_LAST_FREE_TID);
2211 if (tid < 0)
2212 return tid;
2213
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002214 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002215 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2216 pe.index = tid;
2217
2218 /* Set next lu to IPv4 */
2219 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2220 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2221 /* Set L4 offset */
2222 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2223 sizeof(struct iphdr) - 4,
2224 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2225 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2226 MVPP2_PRS_IPV4_DIP_AI_BIT);
2227 mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_MASK,
2228 ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2229
2230 mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2231 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2232 /* Unmask all ports */
2233 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2234
2235 /* Update shadow table and hw entry */
2236 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2237 mvpp2_prs_hw_write(priv, &pe);
2238
2239 /* Not fragmented packet */
2240 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2241 MVPP2_PE_LAST_FREE_TID);
2242 if (tid < 0)
2243 return tid;
2244
2245 pe.index = tid;
2246 /* Clear ri before updating */
2247 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2248 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2249 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2250
2251 mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, MVPP2_PRS_TCAM_PROTO_MASK_L);
2252 mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, MVPP2_PRS_TCAM_PROTO_MASK);
2253
2254 /* Update shadow table and hw entry */
2255 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2256 mvpp2_prs_hw_write(priv, &pe);
2257
2258 return 0;
2259}
2260
2261/* IPv4 L3 multicast or broadcast */
2262static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2263{
2264 struct mvpp2_prs_entry pe;
2265 int mask, tid;
2266
2267 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2268 MVPP2_PE_LAST_FREE_TID);
2269 if (tid < 0)
2270 return tid;
2271
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002272 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002273 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2274 pe.index = tid;
2275
2276 switch (l3_cast) {
2277 case MVPP2_PRS_L3_MULTI_CAST:
2278 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2279 MVPP2_PRS_IPV4_MC_MASK);
2280 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2281 MVPP2_PRS_RI_L3_ADDR_MASK);
2282 break;
2283 case MVPP2_PRS_L3_BROAD_CAST:
2284 mask = MVPP2_PRS_IPV4_BC_MASK;
2285 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2286 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2287 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2288 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2289 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2290 MVPP2_PRS_RI_L3_ADDR_MASK);
2291 break;
2292 default:
2293 return -EINVAL;
2294 }
2295
2296 /* Finished: go to flowid generation */
2297 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2298 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2299
2300 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2301 MVPP2_PRS_IPV4_DIP_AI_BIT);
2302 /* Unmask all ports */
2303 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2304
2305 /* Update shadow table and hw entry */
2306 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2307 mvpp2_prs_hw_write(priv, &pe);
2308
2309 return 0;
2310}
2311
2312/* Set entries for protocols over IPv6 */
2313static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2314 unsigned int ri, unsigned int ri_mask)
2315{
2316 struct mvpp2_prs_entry pe;
2317 int tid;
2318
2319 if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2320 (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2321 return -EINVAL;
2322
2323 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2324 MVPP2_PE_LAST_FREE_TID);
2325 if (tid < 0)
2326 return tid;
2327
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002328 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002329 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2330 pe.index = tid;
2331
2332 /* Finished: go to flowid generation */
2333 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2334 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2335 mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2336 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2337 sizeof(struct ipv6hdr) - 6,
2338 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2339
2340 mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2341 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2342 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2343 /* Unmask all ports */
2344 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2345
2346 /* Write HW */
2347 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2348 mvpp2_prs_hw_write(priv, &pe);
2349
2350 return 0;
2351}
2352
2353/* IPv6 L3 multicast entry */
2354static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2355{
2356 struct mvpp2_prs_entry pe;
2357 int tid;
2358
2359 if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2360 return -EINVAL;
2361
2362 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2363 MVPP2_PE_LAST_FREE_TID);
2364 if (tid < 0)
2365 return tid;
2366
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002367 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002368 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2369 pe.index = tid;
2370
2371 /* Finished: go to flowid generation */
2372 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2373 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2374 MVPP2_PRS_RI_L3_ADDR_MASK);
2375 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2376 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2377 /* Shift back to IPv6 NH */
2378 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2379
2380 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2381 MVPP2_PRS_IPV6_MC_MASK);
2382 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2383 /* Unmask all ports */
2384 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2385
2386 /* Update shadow table and hw entry */
2387 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2388 mvpp2_prs_hw_write(priv, &pe);
2389
2390 return 0;
2391}
2392
2393/* Parser per-port initialization */
2394static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2395 int lu_max, int offset)
2396{
2397 u32 val;
2398
2399 /* Set lookup ID */
2400 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2401 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2402 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2403 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2404
2405 /* Set maximum number of loops for packet received from port */
2406 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2407 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2408 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2409 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2410
2411 /* Set initial offset for packet header extraction for the first
2412 * searching loop
2413 */
2414 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2415 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2416 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2417 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2418}
2419
2420/* Default flow entries initialization for all ports */
2421static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2422{
2423 struct mvpp2_prs_entry pe;
2424 int port;
2425
2426 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002427 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002428 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2429 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2430
2431 /* Mask all ports */
2432 mvpp2_prs_tcam_port_map_set(&pe, 0);
2433
2434 /* Set flow ID*/
2435 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2436 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2437
2438 /* Update shadow table and hw entry */
2439 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2440 mvpp2_prs_hw_write(priv, &pe);
2441 }
2442}
2443
2444/* Set default entry for Marvell Header field */
2445static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2446{
2447 struct mvpp2_prs_entry pe;
2448
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002449 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002450
2451 pe.index = MVPP2_PE_MH_DEFAULT;
2452 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2453 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2454 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2455 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2456
2457 /* Unmask all ports */
2458 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2459
2460 /* Update shadow table and hw entry */
2461 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2462 mvpp2_prs_hw_write(priv, &pe);
2463}
2464
2465/* Set default entires (place holder) for promiscuous, non-promiscuous and
2466 * multicast MAC addresses
2467 */
2468static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2469{
2470 struct mvpp2_prs_entry pe;
2471
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002472 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002473
2474 /* Non-promiscuous mode for all ports - DROP unknown packets */
2475 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2476 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2477
2478 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2479 MVPP2_PRS_RI_DROP_MASK);
2480 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2481 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2482
2483 /* Unmask all ports */
2484 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2485
2486 /* Update shadow table and hw entry */
2487 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2488 mvpp2_prs_hw_write(priv, &pe);
2489
2490 /* place holders only - no ports */
2491 mvpp2_prs_mac_drop_all_set(priv, 0, false);
2492 mvpp2_prs_mac_promisc_set(priv, 0, false);
2493 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
2494 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
2495}
2496
2497/* Set default entries for various types of dsa packets */
2498static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2499{
2500 struct mvpp2_prs_entry pe;
2501
2502 /* None tagged EDSA entry - place holder */
2503 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2504 MVPP2_PRS_EDSA);
2505
2506 /* Tagged EDSA entry - place holder */
2507 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2508
2509 /* None tagged DSA entry - place holder */
2510 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2511 MVPP2_PRS_DSA);
2512
2513 /* Tagged DSA entry - place holder */
2514 mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2515
2516 /* None tagged EDSA ethertype entry - place holder*/
2517 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2518 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2519
2520 /* Tagged EDSA ethertype entry - place holder*/
2521 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2522 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2523
2524 /* None tagged DSA ethertype entry */
2525 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2526 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2527
2528 /* Tagged DSA ethertype entry */
2529 mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2530 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2531
2532 /* Set default entry, in case DSA or EDSA tag not found */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002533 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002534 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2535 pe.index = MVPP2_PE_DSA_DEFAULT;
2536 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2537
2538 /* Shift 0 bytes */
2539 mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2540 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2541
2542 /* Clear all sram ai bits for next iteration */
2543 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2544
2545 /* Unmask all ports */
2546 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2547
2548 mvpp2_prs_hw_write(priv, &pe);
2549}
2550
2551/* Match basic ethertypes */
2552static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2553{
2554 struct mvpp2_prs_entry pe;
2555 int tid;
2556
2557 /* Ethertype: PPPoE */
2558 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2559 MVPP2_PE_LAST_FREE_TID);
2560 if (tid < 0)
2561 return tid;
2562
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002563 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002564 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2565 pe.index = tid;
2566
2567 mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2568
2569 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2570 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2571 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2572 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2573 MVPP2_PRS_RI_PPPOE_MASK);
2574
2575 /* Update shadow table and hw entry */
2576 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2577 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2578 priv->prs_shadow[pe.index].finish = false;
2579 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2580 MVPP2_PRS_RI_PPPOE_MASK);
2581 mvpp2_prs_hw_write(priv, &pe);
2582
2583 /* Ethertype: ARP */
2584 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2585 MVPP2_PE_LAST_FREE_TID);
2586 if (tid < 0)
2587 return tid;
2588
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002589 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002590 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2591 pe.index = tid;
2592
2593 mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2594
2595 /* Generate flow in the next iteration*/
2596 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2597 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2598 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2599 MVPP2_PRS_RI_L3_PROTO_MASK);
2600 /* Set L3 offset */
2601 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2602 MVPP2_ETH_TYPE_LEN,
2603 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2604
2605 /* Update shadow table and hw entry */
2606 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2607 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2608 priv->prs_shadow[pe.index].finish = true;
2609 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2610 MVPP2_PRS_RI_L3_PROTO_MASK);
2611 mvpp2_prs_hw_write(priv, &pe);
2612
2613 /* Ethertype: LBTD */
2614 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2615 MVPP2_PE_LAST_FREE_TID);
2616 if (tid < 0)
2617 return tid;
2618
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002619 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002620 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2621 pe.index = tid;
2622
2623 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2624
2625 /* Generate flow in the next iteration*/
2626 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2627 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2628 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2629 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2630 MVPP2_PRS_RI_CPU_CODE_MASK |
2631 MVPP2_PRS_RI_UDF3_MASK);
2632 /* Set L3 offset */
2633 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2634 MVPP2_ETH_TYPE_LEN,
2635 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2636
2637 /* Update shadow table and hw entry */
2638 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2639 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2640 priv->prs_shadow[pe.index].finish = true;
2641 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2642 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2643 MVPP2_PRS_RI_CPU_CODE_MASK |
2644 MVPP2_PRS_RI_UDF3_MASK);
2645 mvpp2_prs_hw_write(priv, &pe);
2646
2647 /* Ethertype: IPv4 without options */
2648 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2649 MVPP2_PE_LAST_FREE_TID);
2650 if (tid < 0)
2651 return tid;
2652
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002653 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002654 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2655 pe.index = tid;
2656
2657 mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2658 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2659 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2660 MVPP2_PRS_IPV4_HEAD_MASK |
2661 MVPP2_PRS_IPV4_IHL_MASK);
2662
2663 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2664 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2665 MVPP2_PRS_RI_L3_PROTO_MASK);
2666 /* Skip eth_type + 4 bytes of IP header */
2667 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2668 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2669 /* Set L3 offset */
2670 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2671 MVPP2_ETH_TYPE_LEN,
2672 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2673
2674 /* Update shadow table and hw entry */
2675 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2676 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2677 priv->prs_shadow[pe.index].finish = false;
2678 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2679 MVPP2_PRS_RI_L3_PROTO_MASK);
2680 mvpp2_prs_hw_write(priv, &pe);
2681
2682 /* Ethertype: IPv4 with options */
2683 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2684 MVPP2_PE_LAST_FREE_TID);
2685 if (tid < 0)
2686 return tid;
2687
2688 pe.index = tid;
2689
2690 /* Clear tcam data before updating */
2691 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
2692 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
2693
2694 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2695 MVPP2_PRS_IPV4_HEAD,
2696 MVPP2_PRS_IPV4_HEAD_MASK);
2697
2698 /* Clear ri before updating */
2699 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2700 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2701 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2702 MVPP2_PRS_RI_L3_PROTO_MASK);
2703
2704 /* Update shadow table and hw entry */
2705 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2706 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2707 priv->prs_shadow[pe.index].finish = false;
2708 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
2709 MVPP2_PRS_RI_L3_PROTO_MASK);
2710 mvpp2_prs_hw_write(priv, &pe);
2711
2712 /* Ethertype: IPv6 without options */
2713 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2714 MVPP2_PE_LAST_FREE_TID);
2715 if (tid < 0)
2716 return tid;
2717
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002718 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002719 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2720 pe.index = tid;
2721
2722 mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
2723
2724 /* Skip DIP of IPV6 header */
2725 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
2726 MVPP2_MAX_L3_ADDR_SIZE,
2727 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2728 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2729 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2730 MVPP2_PRS_RI_L3_PROTO_MASK);
2731 /* Set L3 offset */
2732 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2733 MVPP2_ETH_TYPE_LEN,
2734 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2735
2736 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2737 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2738 priv->prs_shadow[pe.index].finish = false;
2739 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
2740 MVPP2_PRS_RI_L3_PROTO_MASK);
2741 mvpp2_prs_hw_write(priv, &pe);
2742
2743 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
2744 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2745 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2746 pe.index = MVPP2_PE_ETH_TYPE_UN;
2747
2748 /* Unmask all ports */
2749 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2750
2751 /* Generate flow in the next iteration*/
2752 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2753 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2754 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2755 MVPP2_PRS_RI_L3_PROTO_MASK);
2756 /* Set L3 offset even it's unknown L3 */
2757 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2758 MVPP2_ETH_TYPE_LEN,
2759 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2760
2761 /* Update shadow table and hw entry */
2762 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2763 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2764 priv->prs_shadow[pe.index].finish = true;
2765 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
2766 MVPP2_PRS_RI_L3_PROTO_MASK);
2767 mvpp2_prs_hw_write(priv, &pe);
2768
2769 return 0;
2770}
2771
2772/* Configure vlan entries and detect up to 2 successive VLAN tags.
2773 * Possible options:
2774 * 0x8100, 0x88A8
2775 * 0x8100, 0x8100
2776 * 0x8100
2777 * 0x88A8
2778 */
2779static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
2780{
2781 struct mvpp2_prs_entry pe;
2782 int err;
2783
2784 priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
2785 MVPP2_PRS_DBL_VLANS_MAX,
2786 GFP_KERNEL);
2787 if (!priv->prs_double_vlans)
2788 return -ENOMEM;
2789
2790 /* Double VLAN: 0x8100, 0x88A8 */
2791 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
2792 MVPP2_PRS_PORT_MASK);
2793 if (err)
2794 return err;
2795
2796 /* Double VLAN: 0x8100, 0x8100 */
2797 err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
2798 MVPP2_PRS_PORT_MASK);
2799 if (err)
2800 return err;
2801
2802 /* Single VLAN: 0x88a8 */
2803 err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
2804 MVPP2_PRS_PORT_MASK);
2805 if (err)
2806 return err;
2807
2808 /* Single VLAN: 0x8100 */
2809 err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
2810 MVPP2_PRS_PORT_MASK);
2811 if (err)
2812 return err;
2813
2814 /* Set default double vlan entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002815 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002816 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2817 pe.index = MVPP2_PE_VLAN_DBL;
2818
2819 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2820 /* Clear ai for next iterations */
2821 mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2822 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2823 MVPP2_PRS_RI_VLAN_MASK);
2824
2825 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
2826 MVPP2_PRS_DBL_VLAN_AI_BIT);
2827 /* Unmask all ports */
2828 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2829
2830 /* Update shadow table and hw entry */
2831 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2832 mvpp2_prs_hw_write(priv, &pe);
2833
2834 /* Set default vlan none entry */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002835 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002836 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2837 pe.index = MVPP2_PE_VLAN_NONE;
2838
2839 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2840 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2841 MVPP2_PRS_RI_VLAN_MASK);
2842
2843 /* Unmask all ports */
2844 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2845
2846 /* Update shadow table and hw entry */
2847 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
2848 mvpp2_prs_hw_write(priv, &pe);
2849
2850 return 0;
2851}
2852
2853/* Set entries for PPPoE ethertype */
2854static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
2855{
2856 struct mvpp2_prs_entry pe;
2857 int tid;
2858
2859 /* IPv4 over PPPoE with options */
2860 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2861 MVPP2_PE_LAST_FREE_TID);
2862 if (tid < 0)
2863 return tid;
2864
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002865 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002866 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2867 pe.index = tid;
2868
2869 mvpp2_prs_match_etype(&pe, 0, PPP_IP);
2870
2871 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2872 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2873 MVPP2_PRS_RI_L3_PROTO_MASK);
2874 /* Skip eth_type + 4 bytes of IP header */
2875 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2876 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2877 /* Set L3 offset */
2878 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2879 MVPP2_ETH_TYPE_LEN,
2880 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2881
2882 /* Update shadow table and hw entry */
2883 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2884 mvpp2_prs_hw_write(priv, &pe);
2885
2886 /* IPv4 over PPPoE without options */
2887 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2888 MVPP2_PE_LAST_FREE_TID);
2889 if (tid < 0)
2890 return tid;
2891
2892 pe.index = tid;
2893
2894 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2895 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2896 MVPP2_PRS_IPV4_HEAD_MASK |
2897 MVPP2_PRS_IPV4_IHL_MASK);
2898
2899 /* Clear ri before updating */
2900 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2901 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2902 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2903 MVPP2_PRS_RI_L3_PROTO_MASK);
2904
2905 /* Update shadow table and hw entry */
2906 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2907 mvpp2_prs_hw_write(priv, &pe);
2908
2909 /* IPv6 over PPPoE */
2910 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2911 MVPP2_PE_LAST_FREE_TID);
2912 if (tid < 0)
2913 return tid;
2914
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002915 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002916 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2917 pe.index = tid;
2918
2919 mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
2920
2921 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2922 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2923 MVPP2_PRS_RI_L3_PROTO_MASK);
2924 /* Skip eth_type + 4 bytes of IPv6 header */
2925 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2926 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2927 /* Set L3 offset */
2928 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2929 MVPP2_ETH_TYPE_LEN,
2930 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2931
2932 /* Update shadow table and hw entry */
2933 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2934 mvpp2_prs_hw_write(priv, &pe);
2935
2936 /* Non-IP over PPPoE */
2937 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2938 MVPP2_PE_LAST_FREE_TID);
2939 if (tid < 0)
2940 return tid;
2941
Markus Elfringc5b2ce22017-04-17 10:30:29 +02002942 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03002943 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2944 pe.index = tid;
2945
2946 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2947 MVPP2_PRS_RI_L3_PROTO_MASK);
2948
2949 /* Finished: go to flowid generation */
2950 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2951 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2952 /* Set L3 offset even if it's unknown L3 */
2953 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2954 MVPP2_ETH_TYPE_LEN,
2955 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2956
2957 /* Update shadow table and hw entry */
2958 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
2959 mvpp2_prs_hw_write(priv, &pe);
2960
2961 return 0;
2962}
2963
2964/* Initialize entries for IPv4 */
2965static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
2966{
2967 struct mvpp2_prs_entry pe;
2968 int err;
2969
2970 /* Set entries for TCP, UDP and IGMP over IPv4 */
2971 err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
2972 MVPP2_PRS_RI_L4_PROTO_MASK);
2973 if (err)
2974 return err;
2975
2976 err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
2977 MVPP2_PRS_RI_L4_PROTO_MASK);
2978 if (err)
2979 return err;
2980
2981 err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
2982 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2983 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2984 MVPP2_PRS_RI_CPU_CODE_MASK |
2985 MVPP2_PRS_RI_UDF3_MASK);
2986 if (err)
2987 return err;
2988
2989 /* IPv4 Broadcast */
2990 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
2991 if (err)
2992 return err;
2993
2994 /* IPv4 Multicast */
2995 err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
2996 if (err)
2997 return err;
2998
2999 /* Default IPv4 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003000 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003001 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3002 pe.index = MVPP2_PE_IP4_PROTO_UN;
3003
3004 /* Set next lu to IPv4 */
3005 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3006 mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3007 /* Set L4 offset */
3008 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3009 sizeof(struct iphdr) - 4,
3010 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3011 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3012 MVPP2_PRS_IPV4_DIP_AI_BIT);
3013 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3014 MVPP2_PRS_RI_L4_PROTO_MASK);
3015
3016 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
3017 /* Unmask all ports */
3018 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3019
3020 /* Update shadow table and hw entry */
3021 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3022 mvpp2_prs_hw_write(priv, &pe);
3023
3024 /* Default IPv4 entry for unicast address */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003025 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003026 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3027 pe.index = MVPP2_PE_IP4_ADDR_UN;
3028
3029 /* Finished: go to flowid generation */
3030 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3031 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3032 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3033 MVPP2_PRS_RI_L3_ADDR_MASK);
3034
3035 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3036 MVPP2_PRS_IPV4_DIP_AI_BIT);
3037 /* Unmask all ports */
3038 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3039
3040 /* Update shadow table and hw entry */
3041 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3042 mvpp2_prs_hw_write(priv, &pe);
3043
3044 return 0;
3045}
3046
3047/* Initialize entries for IPv6 */
3048static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
3049{
3050 struct mvpp2_prs_entry pe;
3051 int tid, err;
3052
3053 /* Set entries for TCP, UDP and ICMP over IPv6 */
3054 err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
3055 MVPP2_PRS_RI_L4_TCP,
3056 MVPP2_PRS_RI_L4_PROTO_MASK);
3057 if (err)
3058 return err;
3059
3060 err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
3061 MVPP2_PRS_RI_L4_UDP,
3062 MVPP2_PRS_RI_L4_PROTO_MASK);
3063 if (err)
3064 return err;
3065
3066 err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
3067 MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3068 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3069 MVPP2_PRS_RI_CPU_CODE_MASK |
3070 MVPP2_PRS_RI_UDF3_MASK);
3071 if (err)
3072 return err;
3073
3074 /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
3075 /* Result Info: UDF7=1, DS lite */
3076 err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
3077 MVPP2_PRS_RI_UDF7_IP6_LITE,
3078 MVPP2_PRS_RI_UDF7_MASK);
3079 if (err)
3080 return err;
3081
3082 /* IPv6 multicast */
3083 err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3084 if (err)
3085 return err;
3086
3087 /* Entry for checking hop limit */
3088 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3089 MVPP2_PE_LAST_FREE_TID);
3090 if (tid < 0)
3091 return tid;
3092
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003093 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003094 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3095 pe.index = tid;
3096
3097 /* Finished: go to flowid generation */
3098 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3099 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3100 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
3101 MVPP2_PRS_RI_DROP_MASK,
3102 MVPP2_PRS_RI_L3_PROTO_MASK |
3103 MVPP2_PRS_RI_DROP_MASK);
3104
3105 mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
3106 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3107 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3108
3109 /* Update shadow table and hw entry */
3110 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3111 mvpp2_prs_hw_write(priv, &pe);
3112
3113 /* Default IPv6 entry for unknown protocols */
Markus Elfringc5b2ce22017-04-17 10:30:29 +02003114 memset(&pe, 0, sizeof(pe));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003115 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3116 pe.index = MVPP2_PE_IP6_PROTO_UN;
3117
3118 /* Finished: go to flowid generation */
3119 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3120 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3121 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3122 MVPP2_PRS_RI_L4_PROTO_MASK);
3123 /* Set L4 offset relatively to our current place */
3124 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3125 sizeof(struct ipv6hdr) - 4,
3126 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3127
3128 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3129 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3130 /* Unmask all ports */
3131 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3132
3133 /* Update shadow table and hw entry */
3134 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3135 mvpp2_prs_hw_write(priv, &pe);
3136
3137 /* Default IPv6 entry for unknown ext protocols */
3138 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3139 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3140 pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3141
3142 /* Finished: go to flowid generation */
3143 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3144 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3145 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3146 MVPP2_PRS_RI_L4_PROTO_MASK);
3147
3148 mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3149 MVPP2_PRS_IPV6_EXT_AI_BIT);
3150 /* Unmask all ports */
3151 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3152
3153 /* Update shadow table and hw entry */
3154 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3155 mvpp2_prs_hw_write(priv, &pe);
3156
3157 /* Default IPv6 entry for unicast address */
3158 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3159 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3160 pe.index = MVPP2_PE_IP6_ADDR_UN;
3161
3162 /* Finished: go to IPv6 again */
3163 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3164 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3165 MVPP2_PRS_RI_L3_ADDR_MASK);
3166 mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3167 MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3168 /* Shift back to IPV6 NH */
3169 mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3170
3171 mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3172 /* Unmask all ports */
3173 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3174
3175 /* Update shadow table and hw entry */
3176 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3177 mvpp2_prs_hw_write(priv, &pe);
3178
3179 return 0;
3180}
3181
3182/* Parser default initialization */
3183static int mvpp2_prs_default_init(struct platform_device *pdev,
3184 struct mvpp2 *priv)
3185{
3186 int err, index, i;
3187
3188 /* Enable tcam table */
3189 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3190
3191 /* Clear all tcam and sram entries */
3192 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3193 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3194 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3195 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3196
3197 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3198 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3199 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3200 }
3201
3202 /* Invalidate all tcam entries */
3203 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3204 mvpp2_prs_hw_inv(priv, index);
3205
3206 priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
Markus Elfring37df25e2017-04-17 09:12:34 +02003207 sizeof(*priv->prs_shadow),
Marcin Wojtas3f518502014-07-10 16:52:13 -03003208 GFP_KERNEL);
3209 if (!priv->prs_shadow)
3210 return -ENOMEM;
3211
3212 /* Always start from lookup = 0 */
3213 for (index = 0; index < MVPP2_MAX_PORTS; index++)
3214 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3215 MVPP2_PRS_PORT_LU_MAX, 0);
3216
3217 mvpp2_prs_def_flow_init(priv);
3218
3219 mvpp2_prs_mh_init(priv);
3220
3221 mvpp2_prs_mac_init(priv);
3222
3223 mvpp2_prs_dsa_init(priv);
3224
3225 err = mvpp2_prs_etype_init(priv);
3226 if (err)
3227 return err;
3228
3229 err = mvpp2_prs_vlan_init(pdev, priv);
3230 if (err)
3231 return err;
3232
3233 err = mvpp2_prs_pppoe_init(priv);
3234 if (err)
3235 return err;
3236
3237 err = mvpp2_prs_ip6_init(priv);
3238 if (err)
3239 return err;
3240
3241 err = mvpp2_prs_ip4_init(priv);
3242 if (err)
3243 return err;
3244
3245 return 0;
3246}
3247
3248/* Compare MAC DA with tcam entry data */
3249static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3250 const u8 *da, unsigned char *mask)
3251{
3252 unsigned char tcam_byte, tcam_mask;
3253 int index;
3254
3255 for (index = 0; index < ETH_ALEN; index++) {
3256 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3257 if (tcam_mask != mask[index])
3258 return false;
3259
3260 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3261 return false;
3262 }
3263
3264 return true;
3265}
3266
3267/* Find tcam entry with matched pair <MAC DA, port> */
3268static struct mvpp2_prs_entry *
3269mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3270 unsigned char *mask, int udf_type)
3271{
3272 struct mvpp2_prs_entry *pe;
3273 int tid;
3274
3275 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3276 if (!pe)
3277 return NULL;
3278 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3279
3280 /* Go through the all entires with MVPP2_PRS_LU_MAC */
3281 for (tid = MVPP2_PE_FIRST_FREE_TID;
3282 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3283 unsigned int entry_pmap;
3284
3285 if (!priv->prs_shadow[tid].valid ||
3286 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3287 (priv->prs_shadow[tid].udf != udf_type))
3288 continue;
3289
3290 pe->index = tid;
3291 mvpp2_prs_hw_read(priv, pe);
3292 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3293
3294 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3295 entry_pmap == pmap)
3296 return pe;
3297 }
3298 kfree(pe);
3299
3300 return NULL;
3301}
3302
3303/* Update parser's mac da entry */
3304static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3305 const u8 *da, bool add)
3306{
3307 struct mvpp2_prs_entry *pe;
3308 unsigned int pmap, len, ri;
3309 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3310 int tid;
3311
3312 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3313 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3314 MVPP2_PRS_UDF_MAC_DEF);
3315
3316 /* No such entry */
3317 if (!pe) {
3318 if (!add)
3319 return 0;
3320
3321 /* Create new TCAM entry */
3322 /* Find first range mac entry*/
3323 for (tid = MVPP2_PE_FIRST_FREE_TID;
3324 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3325 if (priv->prs_shadow[tid].valid &&
3326 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3327 (priv->prs_shadow[tid].udf ==
3328 MVPP2_PRS_UDF_MAC_RANGE))
3329 break;
3330
3331 /* Go through the all entries from first to last */
3332 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3333 tid - 1);
3334 if (tid < 0)
3335 return tid;
3336
3337 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3338 if (!pe)
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303339 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003340 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3341 pe->index = tid;
3342
3343 /* Mask all ports */
3344 mvpp2_prs_tcam_port_map_set(pe, 0);
3345 }
3346
3347 /* Update port mask */
3348 mvpp2_prs_tcam_port_set(pe, port, add);
3349
3350 /* Invalidate the entry if no ports are left enabled */
3351 pmap = mvpp2_prs_tcam_port_map_get(pe);
3352 if (pmap == 0) {
3353 if (add) {
3354 kfree(pe);
Amitoj Kaur Chawlac2bb7bc2016-02-04 19:25:26 +05303355 return -EINVAL;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003356 }
3357 mvpp2_prs_hw_inv(priv, pe->index);
3358 priv->prs_shadow[pe->index].valid = false;
3359 kfree(pe);
3360 return 0;
3361 }
3362
3363 /* Continue - set next lookup */
3364 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3365
3366 /* Set match on DA */
3367 len = ETH_ALEN;
3368 while (len--)
3369 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3370
3371 /* Set result info bits */
3372 if (is_broadcast_ether_addr(da))
3373 ri = MVPP2_PRS_RI_L2_BCAST;
3374 else if (is_multicast_ether_addr(da))
3375 ri = MVPP2_PRS_RI_L2_MCAST;
3376 else
3377 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3378
3379 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3380 MVPP2_PRS_RI_MAC_ME_MASK);
3381 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3382 MVPP2_PRS_RI_MAC_ME_MASK);
3383
3384 /* Shift to ethertype */
3385 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3386 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3387
3388 /* Update shadow table and hw entry */
3389 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3390 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3391 mvpp2_prs_hw_write(priv, pe);
3392
3393 kfree(pe);
3394
3395 return 0;
3396}
3397
3398static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3399{
3400 struct mvpp2_port *port = netdev_priv(dev);
3401 int err;
3402
3403 /* Remove old parser entry */
3404 err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3405 false);
3406 if (err)
3407 return err;
3408
3409 /* Add new parser entry */
3410 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3411 if (err)
3412 return err;
3413
3414 /* Set addr in the device */
3415 ether_addr_copy(dev->dev_addr, da);
3416
3417 return 0;
3418}
3419
3420/* Delete all port's multicast simple (not range) entries */
3421static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3422{
3423 struct mvpp2_prs_entry pe;
3424 int index, tid;
3425
3426 for (tid = MVPP2_PE_FIRST_FREE_TID;
3427 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3428 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3429
3430 if (!priv->prs_shadow[tid].valid ||
3431 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3432 (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3433 continue;
3434
3435 /* Only simple mac entries */
3436 pe.index = tid;
3437 mvpp2_prs_hw_read(priv, &pe);
3438
3439 /* Read mac addr from entry */
3440 for (index = 0; index < ETH_ALEN; index++)
3441 mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3442 &da_mask[index]);
3443
3444 if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3445 /* Delete this entry */
3446 mvpp2_prs_mac_da_accept(priv, port, da, false);
3447 }
3448}
3449
3450static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3451{
3452 switch (type) {
3453 case MVPP2_TAG_TYPE_EDSA:
3454 /* Add port to EDSA entries */
3455 mvpp2_prs_dsa_tag_set(priv, port, true,
3456 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3457 mvpp2_prs_dsa_tag_set(priv, port, true,
3458 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3459 /* Remove port from DSA entries */
3460 mvpp2_prs_dsa_tag_set(priv, port, false,
3461 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3462 mvpp2_prs_dsa_tag_set(priv, port, false,
3463 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3464 break;
3465
3466 case MVPP2_TAG_TYPE_DSA:
3467 /* Add port to DSA entries */
3468 mvpp2_prs_dsa_tag_set(priv, port, true,
3469 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3470 mvpp2_prs_dsa_tag_set(priv, port, true,
3471 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3472 /* Remove port from EDSA entries */
3473 mvpp2_prs_dsa_tag_set(priv, port, false,
3474 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3475 mvpp2_prs_dsa_tag_set(priv, port, false,
3476 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3477 break;
3478
3479 case MVPP2_TAG_TYPE_MH:
3480 case MVPP2_TAG_TYPE_NONE:
3481 /* Remove port form EDSA and DSA entries */
3482 mvpp2_prs_dsa_tag_set(priv, port, false,
3483 MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3484 mvpp2_prs_dsa_tag_set(priv, port, false,
3485 MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3486 mvpp2_prs_dsa_tag_set(priv, port, false,
3487 MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3488 mvpp2_prs_dsa_tag_set(priv, port, false,
3489 MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3490 break;
3491
3492 default:
3493 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
3494 return -EINVAL;
3495 }
3496
3497 return 0;
3498}
3499
3500/* Set prs flow for the port */
3501static int mvpp2_prs_def_flow(struct mvpp2_port *port)
3502{
3503 struct mvpp2_prs_entry *pe;
3504 int tid;
3505
3506 pe = mvpp2_prs_flow_find(port->priv, port->id);
3507
3508 /* Such entry not exist */
3509 if (!pe) {
3510 /* Go through the all entires from last to first */
3511 tid = mvpp2_prs_tcam_first_free(port->priv,
3512 MVPP2_PE_LAST_FREE_TID,
3513 MVPP2_PE_FIRST_FREE_TID);
3514 if (tid < 0)
3515 return tid;
3516
3517 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3518 if (!pe)
3519 return -ENOMEM;
3520
3521 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
3522 pe->index = tid;
3523
3524 /* Set flow ID*/
3525 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
3526 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
3527
3528 /* Update shadow table */
3529 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
3530 }
3531
3532 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
3533 mvpp2_prs_hw_write(port->priv, pe);
3534 kfree(pe);
3535
3536 return 0;
3537}
3538
3539/* Classifier configuration routines */
3540
3541/* Update classification flow table registers */
3542static void mvpp2_cls_flow_write(struct mvpp2 *priv,
3543 struct mvpp2_cls_flow_entry *fe)
3544{
3545 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
3546 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
3547 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
3548 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
3549}
3550
3551/* Update classification lookup table register */
3552static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
3553 struct mvpp2_cls_lookup_entry *le)
3554{
3555 u32 val;
3556
3557 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
3558 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
3559 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
3560}
3561
3562/* Classifier default initialization */
3563static void mvpp2_cls_init(struct mvpp2 *priv)
3564{
3565 struct mvpp2_cls_lookup_entry le;
3566 struct mvpp2_cls_flow_entry fe;
3567 int index;
3568
3569 /* Enable classifier */
3570 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
3571
3572 /* Clear classifier flow table */
Arnd Bergmanne8f967c2016-11-24 17:28:12 +01003573 memset(&fe.data, 0, sizeof(fe.data));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003574 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
3575 fe.index = index;
3576 mvpp2_cls_flow_write(priv, &fe);
3577 }
3578
3579 /* Clear classifier lookup table */
3580 le.data = 0;
3581 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
3582 le.lkpid = index;
3583 le.way = 0;
3584 mvpp2_cls_lookup_write(priv, &le);
3585
3586 le.way = 1;
3587 mvpp2_cls_lookup_write(priv, &le);
3588 }
3589}
3590
3591static void mvpp2_cls_port_config(struct mvpp2_port *port)
3592{
3593 struct mvpp2_cls_lookup_entry le;
3594 u32 val;
3595
3596 /* Set way for the port */
3597 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
3598 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
3599 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
3600
3601 /* Pick the entry to be accessed in lookup ID decoding table
3602 * according to the way and lkpid.
3603 */
3604 le.lkpid = port->id;
3605 le.way = 0;
3606 le.data = 0;
3607
3608 /* Set initial CPU queue for receiving packets */
3609 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
3610 le.data |= port->first_rxq;
3611
3612 /* Disable classification engines */
3613 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
3614
3615 /* Update lookup ID table entry */
3616 mvpp2_cls_lookup_write(port->priv, &le);
3617}
3618
3619/* Set CPU queue number for oversize packets */
3620static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
3621{
3622 u32 val;
3623
3624 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
3625 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
3626
3627 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
3628 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
3629
3630 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
3631 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
3632 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
3633}
3634
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003635static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
3636{
3637 if (likely(pool->frag_size <= PAGE_SIZE))
3638 return netdev_alloc_frag(pool->frag_size);
3639 else
3640 return kmalloc(pool->frag_size, GFP_ATOMIC);
3641}
3642
3643static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
3644{
3645 if (likely(pool->frag_size <= PAGE_SIZE))
3646 skb_free_frag(data);
3647 else
3648 kfree(data);
3649}
3650
Marcin Wojtas3f518502014-07-10 16:52:13 -03003651/* Buffer Manager configuration routines */
3652
3653/* Create pool */
3654static int mvpp2_bm_pool_create(struct platform_device *pdev,
3655 struct mvpp2 *priv,
3656 struct mvpp2_bm_pool *bm_pool, int size)
3657{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003658 u32 val;
3659
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003660 /* Number of buffer pointers must be a multiple of 16, as per
3661 * hardware constraints
3662 */
3663 if (!IS_ALIGNED(size, 16))
3664 return -EINVAL;
3665
3666 /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
3667 * bytes per buffer pointer
3668 */
3669 if (priv->hw_version == MVPP21)
3670 bm_pool->size_bytes = 2 * sizeof(u32) * size;
3671 else
3672 bm_pool->size_bytes = 2 * sizeof(u64) * size;
3673
3674 bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003675 &bm_pool->dma_addr,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003676 GFP_KERNEL);
3677 if (!bm_pool->virt_addr)
3678 return -ENOMEM;
3679
Thomas Petazzonid3158802017-02-21 11:28:13 +01003680 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
3681 MVPP2_BM_POOL_PTR_ALIGN)) {
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003682 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
3683 bm_pool->virt_addr, bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003684 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
3685 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
3686 return -ENOMEM;
3687 }
3688
3689 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003690 lower_32_bits(bm_pool->dma_addr));
Marcin Wojtas3f518502014-07-10 16:52:13 -03003691 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
3692
3693 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3694 val |= MVPP2_BM_START_MASK;
3695 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3696
3697 bm_pool->type = MVPP2_BM_FREE;
3698 bm_pool->size = size;
3699 bm_pool->pkt_size = 0;
3700 bm_pool->buf_num = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003701
3702 return 0;
3703}
3704
3705/* Set pool buffer size */
3706static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
3707 struct mvpp2_bm_pool *bm_pool,
3708 int buf_size)
3709{
3710 u32 val;
3711
3712 bm_pool->buf_size = buf_size;
3713
3714 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
3715 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
3716}
3717
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003718static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
3719 struct mvpp2_bm_pool *bm_pool,
3720 dma_addr_t *dma_addr,
3721 phys_addr_t *phys_addr)
3722{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003723 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01003724
3725 *dma_addr = mvpp2_percpu_read(priv, cpu,
3726 MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
3727 *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003728
3729 if (priv->hw_version == MVPP22) {
3730 u32 val;
3731 u32 dma_addr_highbits, phys_addr_highbits;
3732
Thomas Petazzonia7868412017-03-07 16:53:13 +01003733 val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003734 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
3735 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
3736 MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
3737
3738 if (sizeof(dma_addr_t) == 8)
3739 *dma_addr |= (u64)dma_addr_highbits << 32;
3740
3741 if (sizeof(phys_addr_t) == 8)
3742 *phys_addr |= (u64)phys_addr_highbits << 32;
3743 }
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003744
3745 put_cpu();
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003746}
3747
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003748/* Free all buffers from the pool */
Marcin Wojtas4229d502015-12-03 15:20:50 +01003749static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
3750 struct mvpp2_bm_pool *bm_pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003751{
3752 int i;
3753
Ezequiel Garcia7861f122014-07-21 13:48:14 -03003754 for (i = 0; i < bm_pool->buf_num; i++) {
Thomas Petazzoni20396132017-03-07 16:53:00 +01003755 dma_addr_t buf_dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003756 phys_addr_t buf_phys_addr;
3757 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003758
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003759 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
3760 &buf_dma_addr, &buf_phys_addr);
Marcin Wojtas4229d502015-12-03 15:20:50 +01003761
Thomas Petazzoni20396132017-03-07 16:53:00 +01003762 dma_unmap_single(dev, buf_dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01003763 bm_pool->buf_size, DMA_FROM_DEVICE);
3764
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003765 data = (void *)phys_to_virt(buf_phys_addr);
3766 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003767 break;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003768
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003769 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003770 }
3771
3772 /* Update BM driver with number of buffers removed from pool */
3773 bm_pool->buf_num -= i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003774}
3775
3776/* Cleanup pool */
3777static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
3778 struct mvpp2 *priv,
3779 struct mvpp2_bm_pool *bm_pool)
3780{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003781 u32 val;
3782
Marcin Wojtas4229d502015-12-03 15:20:50 +01003783 mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03003784 if (bm_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03003785 WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
3786 return 0;
3787 }
3788
3789 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3790 val |= MVPP2_BM_STOP_MASK;
3791 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3792
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003793 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
Marcin Wojtas3f518502014-07-10 16:52:13 -03003794 bm_pool->virt_addr,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003795 bm_pool->dma_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003796 return 0;
3797}
3798
3799static int mvpp2_bm_pools_init(struct platform_device *pdev,
3800 struct mvpp2 *priv)
3801{
3802 int i, err, size;
3803 struct mvpp2_bm_pool *bm_pool;
3804
3805 /* Create all pools with maximum size */
3806 size = MVPP2_BM_POOL_SIZE_MAX;
3807 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3808 bm_pool = &priv->bm_pools[i];
3809 bm_pool->id = i;
3810 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
3811 if (err)
3812 goto err_unroll_pools;
3813 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
3814 }
3815 return 0;
3816
3817err_unroll_pools:
3818 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
3819 for (i = i - 1; i >= 0; i--)
3820 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
3821 return err;
3822}
3823
3824static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
3825{
3826 int i, err;
3827
3828 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
3829 /* Mask BM all interrupts */
3830 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
3831 /* Clear BM cause register */
3832 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
3833 }
3834
3835 /* Allocate and initialize BM pools */
3836 priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
Markus Elfring81f915e2017-04-17 09:06:33 +02003837 sizeof(*priv->bm_pools), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003838 if (!priv->bm_pools)
3839 return -ENOMEM;
3840
3841 err = mvpp2_bm_pools_init(pdev, priv);
3842 if (err < 0)
3843 return err;
3844 return 0;
3845}
3846
3847/* Attach long pool to rxq */
3848static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
3849 int lrxq, int long_pool)
3850{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003851 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003852 int prxq;
3853
3854 /* Get queue physical ID */
3855 prxq = port->rxqs[lrxq]->id;
3856
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003857 if (port->priv->hw_version == MVPP21)
3858 mask = MVPP21_RXQ_POOL_LONG_MASK;
3859 else
3860 mask = MVPP22_RXQ_POOL_LONG_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003861
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003862 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3863 val &= ~mask;
3864 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003865 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3866}
3867
3868/* Attach short pool to rxq */
3869static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
3870 int lrxq, int short_pool)
3871{
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003872 u32 val, mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003873 int prxq;
3874
3875 /* Get queue physical ID */
3876 prxq = port->rxqs[lrxq]->id;
3877
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003878 if (port->priv->hw_version == MVPP21)
3879 mask = MVPP21_RXQ_POOL_SHORT_MASK;
3880 else
3881 mask = MVPP22_RXQ_POOL_SHORT_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003882
Thomas Petazzoni5eac8922017-03-07 16:53:10 +01003883 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3884 val &= ~mask;
3885 val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003886 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3887}
3888
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003889static void *mvpp2_buf_alloc(struct mvpp2_port *port,
3890 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003891 dma_addr_t *buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003892 phys_addr_t *buf_phys_addr,
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003893 gfp_t gfp_mask)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003894{
Thomas Petazzoni20396132017-03-07 16:53:00 +01003895 dma_addr_t dma_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003896 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003897
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003898 data = mvpp2_frag_alloc(bm_pool);
3899 if (!data)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003900 return NULL;
3901
Thomas Petazzoni20396132017-03-07 16:53:00 +01003902 dma_addr = dma_map_single(port->dev->dev.parent, data,
3903 MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
3904 DMA_FROM_DEVICE);
3905 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003906 mvpp2_frag_free(bm_pool, data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003907 return NULL;
3908 }
Thomas Petazzoni20396132017-03-07 16:53:00 +01003909 *buf_dma_addr = dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003910 *buf_phys_addr = virt_to_phys(data);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003911
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003912 return data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003913}
3914
Marcin Wojtas3f518502014-07-10 16:52:13 -03003915/* Release buffer to BM */
3916static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzoni20396132017-03-07 16:53:00 +01003917 dma_addr_t buf_dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003918 phys_addr_t buf_phys_addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003919{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003920 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01003921
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003922 if (port->priv->hw_version == MVPP22) {
3923 u32 val = 0;
3924
3925 if (sizeof(dma_addr_t) == 8)
3926 val |= upper_32_bits(buf_dma_addr) &
3927 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
3928
3929 if (sizeof(phys_addr_t) == 8)
3930 val |= (upper_32_bits(buf_phys_addr)
3931 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
3932 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
3933
Thomas Petazzonia7868412017-03-07 16:53:13 +01003934 mvpp2_percpu_write(port->priv, cpu,
3935 MVPP22_BM_ADDR_HIGH_RLS_REG, val);
Thomas Petazzonid01524d2017-03-07 16:53:09 +01003936 }
3937
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003938 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
3939 * returned in the "cookie" field of the RX
3940 * descriptor. Instead of storing the virtual address, we
3941 * store the physical address
3942 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01003943 mvpp2_percpu_write(port->priv, cpu,
3944 MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
3945 mvpp2_percpu_write(port->priv, cpu,
3946 MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02003947
3948 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03003949}
3950
Marcin Wojtas3f518502014-07-10 16:52:13 -03003951/* Allocate buffers for the pool */
3952static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
3953 struct mvpp2_bm_pool *bm_pool, int buf_num)
3954{
Marcin Wojtas3f518502014-07-10 16:52:13 -03003955 int i, buf_size, total_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01003956 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003957 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003958 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003959
3960 buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
3961 total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
3962
3963 if (buf_num < 0 ||
3964 (buf_num + bm_pool->buf_num > bm_pool->size)) {
3965 netdev_err(port->dev,
3966 "cannot allocate %d buffers for pool %d\n",
3967 buf_num, bm_pool->id);
3968 return 0;
3969 }
3970
Marcin Wojtas3f518502014-07-10 16:52:13 -03003971 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003972 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
3973 &phys_addr, GFP_KERNEL);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01003974 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03003975 break;
3976
Thomas Petazzoni20396132017-03-07 16:53:00 +01003977 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01003978 phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03003979 }
3980
3981 /* Update BM driver with number of buffers added to pool */
3982 bm_pool->buf_num += i;
Marcin Wojtas3f518502014-07-10 16:52:13 -03003983
3984 netdev_dbg(port->dev,
3985 "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
3986 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
3987 bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
3988
3989 netdev_dbg(port->dev,
3990 "%s pool %d: %d of %d buffers added\n",
3991 bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
3992 bm_pool->id, i, buf_num);
3993 return i;
3994}
3995
3996/* Notify the driver that BM pool is being used as specific type and return the
3997 * pool pointer on success
3998 */
3999static struct mvpp2_bm_pool *
4000mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
4001 int pkt_size)
4002{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004003 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4004 int num;
4005
4006 if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
4007 netdev_err(port->dev, "mixing pool types is forbidden\n");
4008 return NULL;
4009 }
4010
Marcin Wojtas3f518502014-07-10 16:52:13 -03004011 if (new_pool->type == MVPP2_BM_FREE)
4012 new_pool->type = type;
4013
4014 /* Allocate buffers in case BM pool is used as long pool, but packet
4015 * size doesn't match MTU or BM pool hasn't being used yet
4016 */
4017 if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
4018 (new_pool->pkt_size == 0)) {
4019 int pkts_num;
4020
4021 /* Set default buffer number or free all the buffers in case
4022 * the pool is not empty
4023 */
4024 pkts_num = new_pool->buf_num;
4025 if (pkts_num == 0)
4026 pkts_num = type == MVPP2_BM_SWF_LONG ?
4027 MVPP2_BM_LONG_BUF_NUM :
4028 MVPP2_BM_SHORT_BUF_NUM;
4029 else
Marcin Wojtas4229d502015-12-03 15:20:50 +01004030 mvpp2_bm_bufs_free(port->dev->dev.parent,
4031 port->priv, new_pool);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004032
4033 new_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004034 new_pool->frag_size =
4035 SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4036 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004037
4038 /* Allocate buffers for this pool */
4039 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4040 if (num != pkts_num) {
4041 WARN(1, "pool %d: %d of %d allocated\n",
4042 new_pool->id, num, pkts_num);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004043 return NULL;
4044 }
4045 }
4046
4047 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4048 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4049
Marcin Wojtas3f518502014-07-10 16:52:13 -03004050 return new_pool;
4051}
4052
4053/* Initialize pools for swf */
4054static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4055{
Marcin Wojtas3f518502014-07-10 16:52:13 -03004056 int rxq;
4057
4058 if (!port->pool_long) {
4059 port->pool_long =
4060 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
4061 MVPP2_BM_SWF_LONG,
4062 port->pkt_size);
4063 if (!port->pool_long)
4064 return -ENOMEM;
4065
Marcin Wojtas3f518502014-07-10 16:52:13 -03004066 port->pool_long->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004067
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004068 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004069 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4070 }
4071
4072 if (!port->pool_short) {
4073 port->pool_short =
4074 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
4075 MVPP2_BM_SWF_SHORT,
4076 MVPP2_BM_SHORT_PKT_SIZE);
4077 if (!port->pool_short)
4078 return -ENOMEM;
4079
Marcin Wojtas3f518502014-07-10 16:52:13 -03004080 port->pool_short->port_map |= (1 << port->id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004081
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004082 for (rxq = 0; rxq < port->nrxqs; rxq++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004083 mvpp2_rxq_short_pool_set(port, rxq,
4084 port->pool_short->id);
4085 }
4086
4087 return 0;
4088}
4089
4090static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4091{
4092 struct mvpp2_port *port = netdev_priv(dev);
4093 struct mvpp2_bm_pool *port_pool = port->pool_long;
4094 int num, pkts_num = port_pool->buf_num;
4095 int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
4096
4097 /* Update BM pool with new buffer size */
Marcin Wojtas4229d502015-12-03 15:20:50 +01004098 mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
Ezequiel Garciad74c96c2014-07-21 13:48:13 -03004099 if (port_pool->buf_num) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004100 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
4101 return -EIO;
4102 }
4103
4104 port_pool->pkt_size = pkt_size;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01004105 port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4106 MVPP2_SKB_SHINFO_SIZE;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004107 num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4108 if (num != pkts_num) {
4109 WARN(1, "pool %d: %d of %d allocated\n",
4110 port_pool->id, num, pkts_num);
4111 return -EIO;
4112 }
4113
4114 mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
4115 MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
4116 dev->mtu = mtu;
4117 netdev_update_features(dev);
4118 return 0;
4119}
4120
4121static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4122{
4123 int cpu, cpu_mask = 0;
4124
4125 for_each_present_cpu(cpu)
4126 cpu_mask |= 1 << cpu;
4127 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4128 MVPP2_ISR_ENABLE_INTERRUPT(cpu_mask));
4129}
4130
4131static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4132{
4133 int cpu, cpu_mask = 0;
4134
4135 for_each_present_cpu(cpu)
4136 cpu_mask |= 1 << cpu;
4137 mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4138 MVPP2_ISR_DISABLE_INTERRUPT(cpu_mask));
4139}
4140
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004141/* Mask the current CPU's Rx/Tx interrupts
4142 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4143 * using smp_processor_id() is OK.
4144 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004145static void mvpp2_interrupts_mask(void *arg)
4146{
4147 struct mvpp2_port *port = arg;
4148
Thomas Petazzonia7868412017-03-07 16:53:13 +01004149 mvpp2_percpu_write(port->priv, smp_processor_id(),
4150 MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004151}
4152
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004153/* Unmask the current CPU's Rx/Tx interrupts.
4154 * Called by on_each_cpu(), guaranteed to run with migration disabled,
4155 * using smp_processor_id() is OK.
4156 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004157static void mvpp2_interrupts_unmask(void *arg)
4158{
4159 struct mvpp2_port *port = arg;
4160
Thomas Petazzonia7868412017-03-07 16:53:13 +01004161 mvpp2_percpu_write(port->priv, smp_processor_id(),
4162 MVPP2_ISR_RX_TX_MASK_REG(port->id),
4163 (MVPP2_CAUSE_MISC_SUM_MASK |
4164 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004165}
4166
4167/* Port configuration routines */
4168
Thomas Petazzoni26975822017-03-07 16:53:14 +01004169static void mvpp22_port_mii_set(struct mvpp2_port *port)
4170{
4171 u32 val;
4172
Thomas Petazzoni26975822017-03-07 16:53:14 +01004173 /* Only GOP port 0 has an XLG MAC */
4174 if (port->gop_id == 0) {
4175 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
4176 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
Antoine Ténart725757a2017-06-12 16:01:39 +02004177
4178 if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4179 port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4180 val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
4181 else
4182 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4183
Thomas Petazzoni26975822017-03-07 16:53:14 +01004184 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
4185 }
4186
4187 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4188 if (port->phy_interface == PHY_INTERFACE_MODE_RGMII)
4189 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4190 else
4191 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4192 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
4193 val |= MVPP22_CTRL4_SYNC_BYPASS;
4194 val |= MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4195 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4196}
4197
Marcin Wojtas3f518502014-07-10 16:52:13 -03004198static void mvpp2_port_mii_set(struct mvpp2_port *port)
4199{
Marcin Wojtas08a23752014-07-21 13:48:12 -03004200 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004201
Thomas Petazzoni26975822017-03-07 16:53:14 +01004202 if (port->priv->hw_version == MVPP22)
4203 mvpp22_port_mii_set(port);
4204
Marcin Wojtas08a23752014-07-21 13:48:12 -03004205 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004206
Marcin Wojtas08a23752014-07-21 13:48:12 -03004207 switch (port->phy_interface) {
4208 case PHY_INTERFACE_MODE_SGMII:
4209 val |= MVPP2_GMAC_INBAND_AN_MASK;
4210 break;
4211 case PHY_INTERFACE_MODE_RGMII:
4212 val |= MVPP2_GMAC_PORT_RGMII_MASK;
4213 default:
4214 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
4215 }
4216
4217 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4218}
4219
4220static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
4221{
4222 u32 val;
4223
4224 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4225 val |= MVPP2_GMAC_FC_ADV_EN;
4226 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004227}
4228
4229static void mvpp2_port_enable(struct mvpp2_port *port)
4230{
4231 u32 val;
4232
Antoine Ténart725757a2017-06-12 16:01:39 +02004233 /* Only GOP port 0 has an XLG MAC */
4234 if (port->gop_id == 0 &&
4235 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4236 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4237 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4238 val |= MVPP22_XLG_CTRL0_PORT_EN |
4239 MVPP22_XLG_CTRL0_MAC_RESET_DIS;
4240 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
4241 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4242 } else {
4243 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4244 val |= MVPP2_GMAC_PORT_EN_MASK;
4245 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
4246 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4247 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004248}
4249
4250static void mvpp2_port_disable(struct mvpp2_port *port)
4251{
4252 u32 val;
4253
Antoine Ténart725757a2017-06-12 16:01:39 +02004254 /* Only GOP port 0 has an XLG MAC */
4255 if (port->gop_id == 0 &&
4256 (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4257 port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4258 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4259 val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
4260 MVPP22_XLG_CTRL0_MAC_RESET_DIS);
4261 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4262 } else {
4263 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4264 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
4265 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4266 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004267}
4268
4269/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
4270static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
4271{
4272 u32 val;
4273
4274 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
4275 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
4276 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4277}
4278
4279/* Configure loopback port */
4280static void mvpp2_port_loopback_set(struct mvpp2_port *port)
4281{
4282 u32 val;
4283
4284 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4285
4286 if (port->speed == 1000)
4287 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
4288 else
4289 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
4290
4291 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4292 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
4293 else
4294 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
4295
4296 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4297}
4298
4299static void mvpp2_port_reset(struct mvpp2_port *port)
4300{
4301 u32 val;
4302
4303 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4304 ~MVPP2_GMAC_PORT_RESET_MASK;
4305 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4306
4307 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4308 MVPP2_GMAC_PORT_RESET_MASK)
4309 continue;
4310}
4311
4312/* Change maximum receive size of the port */
4313static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
4314{
4315 u32 val;
4316
4317 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4318 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
4319 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
4320 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
4321 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4322}
4323
4324/* Set defaults to the MVPP2 port */
4325static void mvpp2_defaults_set(struct mvpp2_port *port)
4326{
4327 int tx_port_num, val, queue, ptxq, lrxq;
4328
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004329 if (port->priv->hw_version == MVPP21) {
4330 /* Configure port to loopback if needed */
4331 if (port->flags & MVPP2_F_LOOPBACK)
4332 mvpp2_port_loopback_set(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004333
Thomas Petazzoni3d9017d2017-03-07 16:53:11 +01004334 /* Update TX FIFO MIN Threshold */
4335 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4336 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
4337 /* Min. TX threshold must be less than minimal packet length */
4338 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
4339 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4340 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03004341
4342 /* Disable Legacy WRR, Disable EJP, Release from reset */
4343 tx_port_num = mvpp2_egress_port(port);
4344 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
4345 tx_port_num);
4346 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
4347
4348 /* Close bandwidth for all queues */
4349 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
4350 ptxq = mvpp2_txq_phys(port->id, queue);
4351 mvpp2_write(port->priv,
4352 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
4353 }
4354
4355 /* Set refill period to 1 usec, refill tokens
4356 * and bucket size to maximum
4357 */
4358 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
4359 port->priv->tclk / USEC_PER_SEC);
4360 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
4361 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
4362 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
4363 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
4364 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
4365 val = MVPP2_TXP_TOKEN_SIZE_MAX;
4366 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4367
4368 /* Set MaximumLowLatencyPacketSize value to 256 */
4369 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
4370 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
4371 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
4372
4373 /* Enable Rx cache snoop */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004374 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004375 queue = port->rxqs[lrxq]->id;
4376 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4377 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
4378 MVPP2_SNOOP_BUF_HDR_MASK;
4379 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4380 }
4381
4382 /* At default, mask all interrupts to all present cpus */
4383 mvpp2_interrupts_disable(port);
4384}
4385
4386/* Enable/disable receiving packets */
4387static void mvpp2_ingress_enable(struct mvpp2_port *port)
4388{
4389 u32 val;
4390 int lrxq, queue;
4391
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004392 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004393 queue = port->rxqs[lrxq]->id;
4394 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
4395 val &= ~MVPP2_RXQ_DISABLE_MASK;
4396 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
4397 }
4398}
4399
4400static void mvpp2_ingress_disable(struct mvpp2_port *port)
4401{
4402 u32 val;
4403 int lrxq, queue;
4404
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004405 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004406 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
4413/* Enable transmit via physical egress queue
4414 * - HW starts take descriptors from DRAM
4415 */
4416static void mvpp2_egress_enable(struct mvpp2_port *port)
4417{
4418 u32 qmap;
4419 int queue;
4420 int tx_port_num = mvpp2_egress_port(port);
4421
4422 /* Enable all initialized TXs. */
4423 qmap = 0;
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004424 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004425 struct mvpp2_tx_queue *txq = port->txqs[queue];
4426
Markus Elfringdbbb2f02017-04-17 14:07:52 +02004427 if (txq->descs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004428 qmap |= (1 << queue);
4429 }
4430
4431 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4432 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
4433}
4434
4435/* Disable transmit via physical egress queue
4436 * - HW doesn't take descriptors from DRAM
4437 */
4438static void mvpp2_egress_disable(struct mvpp2_port *port)
4439{
4440 u32 reg_data;
4441 int delay;
4442 int tx_port_num = mvpp2_egress_port(port);
4443
4444 /* Issue stop command for active channels only */
4445 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4446 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
4447 MVPP2_TXP_SCHED_ENQ_MASK;
4448 if (reg_data != 0)
4449 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
4450 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
4451
4452 /* Wait for all Tx activity to terminate. */
4453 delay = 0;
4454 do {
4455 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
4456 netdev_warn(port->dev,
4457 "Tx stop timed out, status=0x%08x\n",
4458 reg_data);
4459 break;
4460 }
4461 mdelay(1);
4462 delay++;
4463
4464 /* Check port TX Command register that all
4465 * Tx queues are stopped
4466 */
4467 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
4468 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
4469}
4470
4471/* Rx descriptors helper methods */
4472
4473/* Get number of Rx descriptors occupied by received packets */
4474static inline int
4475mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
4476{
4477 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
4478
4479 return val & MVPP2_RXQ_OCCUPIED_MASK;
4480}
4481
4482/* Update Rx queue status with the number of occupied and available
4483 * Rx descriptor slots.
4484 */
4485static inline void
4486mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
4487 int used_count, int free_count)
4488{
4489 /* Decrement the number of used descriptors and increment count
4490 * increment the number of free descriptors.
4491 */
4492 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
4493
4494 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
4495}
4496
4497/* Get pointer to next RX descriptor to be processed by SW */
4498static inline struct mvpp2_rx_desc *
4499mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
4500{
4501 int rx_desc = rxq->next_desc_to_proc;
4502
4503 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
4504 prefetch(rxq->descs + rxq->next_desc_to_proc);
4505 return rxq->descs + rx_desc;
4506}
4507
4508/* Set rx queue offset */
4509static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
4510 int prxq, int offset)
4511{
4512 u32 val;
4513
4514 /* Convert offset from bytes to units of 32 bytes */
4515 offset = offset >> 5;
4516
4517 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4518 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
4519
4520 /* Offset is in */
4521 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
4522 MVPP2_RXQ_PACKET_OFFSET_MASK);
4523
4524 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4525}
4526
Marcin Wojtas3f518502014-07-10 16:52:13 -03004527/* Tx descriptors helper methods */
4528
Marcin Wojtas3f518502014-07-10 16:52:13 -03004529/* Get pointer to next Tx descriptor to be processed (send) by HW */
4530static struct mvpp2_tx_desc *
4531mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
4532{
4533 int tx_desc = txq->next_desc_to_proc;
4534
4535 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
4536 return txq->descs + tx_desc;
4537}
4538
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004539/* Update HW with number of aggregated Tx descriptors to be sent
4540 *
4541 * Called only from mvpp2_tx(), so migration is disabled, using
4542 * smp_processor_id() is OK.
4543 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004544static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
4545{
4546 /* aggregated access - relevant TXQ number is written in TX desc */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004547 mvpp2_percpu_write(port->priv, smp_processor_id(),
4548 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004549}
4550
4551
4552/* Check if there are enough free descriptors in aggregated txq.
4553 * If not, update the number of occupied descriptors and repeat the check.
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004554 *
4555 * Called only from mvpp2_tx(), so migration is disabled, using
4556 * smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03004557 */
4558static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
4559 struct mvpp2_tx_queue *aggr_txq, int num)
4560{
4561 if ((aggr_txq->count + num) > aggr_txq->size) {
4562 /* Update number of occupied aggregated Tx descriptors */
4563 int cpu = smp_processor_id();
4564 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
4565
4566 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
4567 }
4568
4569 if ((aggr_txq->count + num) > aggr_txq->size)
4570 return -ENOMEM;
4571
4572 return 0;
4573}
4574
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004575/* Reserved Tx descriptors allocation request
4576 *
4577 * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
4578 * only by mvpp2_tx(), so migration is disabled, using
4579 * smp_processor_id() is OK.
4580 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004581static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
4582 struct mvpp2_tx_queue *txq, int num)
4583{
4584 u32 val;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004585 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004586
4587 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004588 mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004589
Thomas Petazzonia7868412017-03-07 16:53:13 +01004590 val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004591
4592 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
4593}
4594
4595/* Check if there are enough reserved descriptors for transmission.
4596 * If not, request chunk of reserved descriptors and check again.
4597 */
4598static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
4599 struct mvpp2_tx_queue *txq,
4600 struct mvpp2_txq_pcpu *txq_pcpu,
4601 int num)
4602{
4603 int req, cpu, desc_count;
4604
4605 if (txq_pcpu->reserved_num >= num)
4606 return 0;
4607
4608 /* Not enough descriptors reserved! Update the reserved descriptor
4609 * count and check again.
4610 */
4611
4612 desc_count = 0;
4613 /* Compute total of used descriptors */
4614 for_each_present_cpu(cpu) {
4615 struct mvpp2_txq_pcpu *txq_pcpu_aux;
4616
4617 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
4618 desc_count += txq_pcpu_aux->count;
4619 desc_count += txq_pcpu_aux->reserved_num;
4620 }
4621
4622 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
4623 desc_count += req;
4624
4625 if (desc_count >
4626 (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
4627 return -ENOMEM;
4628
4629 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
4630
4631 /* OK, the descriptor cound has been updated: check again. */
4632 if (txq_pcpu->reserved_num < num)
4633 return -ENOMEM;
4634 return 0;
4635}
4636
4637/* Release the last allocated Tx descriptor. Useful to handle DMA
4638 * mapping failures in the Tx path.
4639 */
4640static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
4641{
4642 if (txq->next_desc_to_proc == 0)
4643 txq->next_desc_to_proc = txq->last_desc - 1;
4644 else
4645 txq->next_desc_to_proc--;
4646}
4647
4648/* Set Tx descriptors fields relevant for CSUM calculation */
4649static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
4650 int ip_hdr_len, int l4_proto)
4651{
4652 u32 command;
4653
4654 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
4655 * G_L4_chk, L4_type required only for checksum calculation
4656 */
4657 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
4658 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
4659 command |= MVPP2_TXD_IP_CSUM_DISABLE;
4660
4661 if (l3_proto == swab16(ETH_P_IP)) {
4662 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
4663 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
4664 } else {
4665 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
4666 }
4667
4668 if (l4_proto == IPPROTO_TCP) {
4669 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
4670 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
4671 } else if (l4_proto == IPPROTO_UDP) {
4672 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
4673 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
4674 } else {
4675 command |= MVPP2_TXD_L4_CSUM_NOT;
4676 }
4677
4678 return command;
4679}
4680
4681/* Get number of sent descriptors and decrement counter.
4682 * The number of sent descriptors is returned.
4683 * Per-CPU access
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004684 *
4685 * Called only from mvpp2_txq_done(), called from mvpp2_tx()
4686 * (migration disabled) and from the TX completion tasklet (migration
4687 * disabled) so using smp_processor_id() is OK.
Marcin Wojtas3f518502014-07-10 16:52:13 -03004688 */
4689static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
4690 struct mvpp2_tx_queue *txq)
4691{
4692 u32 val;
4693
4694 /* Reading status reg resets transmitted descriptor counter */
Thomas Petazzonia7868412017-03-07 16:53:13 +01004695 val = mvpp2_percpu_read(port->priv, smp_processor_id(),
4696 MVPP2_TXQ_SENT_REG(txq->id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004697
4698 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
4699 MVPP2_TRANSMITTED_COUNT_OFFSET;
4700}
4701
Thomas Petazzonie0af22d2017-06-22 14:23:18 +02004702/* Called through on_each_cpu(), so runs on all CPUs, with migration
4703 * disabled, therefore using smp_processor_id() is OK.
4704 */
Marcin Wojtas3f518502014-07-10 16:52:13 -03004705static void mvpp2_txq_sent_counter_clear(void *arg)
4706{
4707 struct mvpp2_port *port = arg;
4708 int queue;
4709
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004710 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004711 int id = port->txqs[queue]->id;
4712
Thomas Petazzonia7868412017-03-07 16:53:13 +01004713 mvpp2_percpu_read(port->priv, smp_processor_id(),
4714 MVPP2_TXQ_SENT_REG(id));
Marcin Wojtas3f518502014-07-10 16:52:13 -03004715 }
4716}
4717
4718/* Set max sizes for Tx queues */
4719static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
4720{
4721 u32 val, size, mtu;
4722 int txq, tx_port_num;
4723
4724 mtu = port->pkt_size * 8;
4725 if (mtu > MVPP2_TXP_MTU_MAX)
4726 mtu = MVPP2_TXP_MTU_MAX;
4727
4728 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
4729 mtu = 3 * mtu;
4730
4731 /* Indirect access to registers */
4732 tx_port_num = mvpp2_egress_port(port);
4733 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4734
4735 /* Set MTU */
4736 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
4737 val &= ~MVPP2_TXP_MTU_MAX;
4738 val |= mtu;
4739 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
4740
4741 /* TXP token size and all TXQs token size must be larger that MTU */
4742 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
4743 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
4744 if (size < mtu) {
4745 size = mtu;
4746 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
4747 val |= size;
4748 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
4749 }
4750
Thomas Petazzoni09f83972017-08-03 10:41:57 +02004751 for (txq = 0; txq < port->ntxqs; txq++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03004752 val = mvpp2_read(port->priv,
4753 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
4754 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
4755
4756 if (size < mtu) {
4757 size = mtu;
4758 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
4759 val |= size;
4760 mvpp2_write(port->priv,
4761 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
4762 val);
4763 }
4764 }
4765}
4766
4767/* Set the number of packets that will be received before Rx interrupt
4768 * will be generated by HW.
4769 */
4770static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004771 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004772{
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004773 int cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004774
Thomas Petazzonif8b0d5f2017-02-21 11:28:03 +01004775 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
4776 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004777
Thomas Petazzonia7868412017-03-07 16:53:13 +01004778 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
4779 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
4780 rxq->pkts_coal);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004781
4782 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004783}
4784
Thomas Petazzoniab426762017-02-21 11:28:04 +01004785static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
4786{
4787 u64 tmp = (u64)clk_hz * usec;
4788
4789 do_div(tmp, USEC_PER_SEC);
4790
4791 return tmp > U32_MAX ? U32_MAX : tmp;
4792}
4793
4794static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
4795{
4796 u64 tmp = (u64)cycles * USEC_PER_SEC;
4797
4798 do_div(tmp, clk_hz);
4799
4800 return tmp > U32_MAX ? U32_MAX : tmp;
4801}
4802
Marcin Wojtas3f518502014-07-10 16:52:13 -03004803/* Set the time delay in usec before Rx interrupt */
4804static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004805 struct mvpp2_rx_queue *rxq)
Marcin Wojtas3f518502014-07-10 16:52:13 -03004806{
Thomas Petazzoniab426762017-02-21 11:28:04 +01004807 unsigned long freq = port->priv->tclk;
4808 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004809
Thomas Petazzoniab426762017-02-21 11:28:04 +01004810 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
4811 rxq->time_coal =
4812 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
4813
4814 /* re-evaluate to get actual register value */
4815 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
4816 }
4817
Marcin Wojtas3f518502014-07-10 16:52:13 -03004818 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004819}
4820
Marcin Wojtas3f518502014-07-10 16:52:13 -03004821/* Free Tx queue skbuffs */
4822static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
4823 struct mvpp2_tx_queue *txq,
4824 struct mvpp2_txq_pcpu *txq_pcpu, int num)
4825{
4826 int i;
4827
4828 for (i = 0; i < num; i++) {
Thomas Petazzoni83544912016-12-21 11:28:49 +01004829 struct mvpp2_txq_pcpu_buf *tx_buf =
4830 txq_pcpu->buffs + txq_pcpu->txq_get_index;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004831
Thomas Petazzoni20396132017-03-07 16:53:00 +01004832 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
Thomas Petazzoni83544912016-12-21 11:28:49 +01004833 tx_buf->size, DMA_TO_DEVICE);
Thomas Petazzoni36fb7432017-02-21 11:28:05 +01004834 if (tx_buf->skb)
4835 dev_kfree_skb_any(tx_buf->skb);
4836
4837 mvpp2_txq_inc_get(txq_pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004838 }
4839}
4840
4841static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
4842 u32 cause)
4843{
4844 int queue = fls(cause) - 1;
4845
4846 return port->rxqs[queue];
4847}
4848
4849static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
4850 u32 cause)
4851{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02004852 int queue = fls(cause) - 1;
Marcin Wojtas3f518502014-07-10 16:52:13 -03004853
4854 return port->txqs[queue];
4855}
4856
4857/* Handle end of transmission */
4858static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
4859 struct mvpp2_txq_pcpu *txq_pcpu)
4860{
4861 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
4862 int tx_done;
4863
4864 if (txq_pcpu->cpu != smp_processor_id())
4865 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
4866
4867 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
4868 if (!tx_done)
4869 return;
4870 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
4871
4872 txq_pcpu->count -= tx_done;
4873
4874 if (netif_tx_queue_stopped(nq))
4875 if (txq_pcpu->size - txq_pcpu->count >= MAX_SKB_FRAGS + 1)
4876 netif_tx_wake_queue(nq);
4877}
4878
Marcin Wojtasedc660f2015-08-06 19:00:30 +02004879static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause)
4880{
4881 struct mvpp2_tx_queue *txq;
4882 struct mvpp2_txq_pcpu *txq_pcpu;
4883 unsigned int tx_todo = 0;
4884
4885 while (cause) {
4886 txq = mvpp2_get_tx_queue(port, cause);
4887 if (!txq)
4888 break;
4889
4890 txq_pcpu = this_cpu_ptr(txq->pcpu);
4891
4892 if (txq_pcpu->count) {
4893 mvpp2_txq_done(port, txq, txq_pcpu);
4894 tx_todo += txq_pcpu->count;
4895 }
4896
4897 cause &= ~(1 << txq->log_id);
4898 }
4899 return tx_todo;
4900}
4901
Marcin Wojtas3f518502014-07-10 16:52:13 -03004902/* Rx/Tx queue initialization/cleanup methods */
4903
4904/* Allocate and initialize descriptors for aggr TXQ */
4905static int mvpp2_aggr_txq_init(struct platform_device *pdev,
4906 struct mvpp2_tx_queue *aggr_txq,
4907 int desc_num, int cpu,
4908 struct mvpp2 *priv)
4909{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004910 u32 txq_dma;
4911
Marcin Wojtas3f518502014-07-10 16:52:13 -03004912 /* Allocate memory for TX descriptors */
4913 aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
4914 desc_num * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004915 &aggr_txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004916 if (!aggr_txq->descs)
4917 return -ENOMEM;
4918
Marcin Wojtas3f518502014-07-10 16:52:13 -03004919 aggr_txq->last_desc = aggr_txq->size - 1;
4920
4921 /* Aggr TXQ no reset WA */
4922 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
4923 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
4924
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004925 /* Set Tx descriptors queue starting address indirect
4926 * access
4927 */
4928 if (priv->hw_version == MVPP21)
4929 txq_dma = aggr_txq->descs_dma;
4930 else
4931 txq_dma = aggr_txq->descs_dma >>
4932 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
4933
4934 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004935 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
4936
4937 return 0;
4938}
4939
4940/* Create a specified Rx queue */
4941static int mvpp2_rxq_init(struct mvpp2_port *port,
4942 struct mvpp2_rx_queue *rxq)
4943
4944{
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004945 u32 rxq_dma;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004946 int cpu;
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004947
Marcin Wojtas3f518502014-07-10 16:52:13 -03004948 rxq->size = port->rx_ring_size;
4949
4950 /* Allocate memory for RX descriptors */
4951 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
4952 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01004953 &rxq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004954 if (!rxq->descs)
4955 return -ENOMEM;
4956
Marcin Wojtas3f518502014-07-10 16:52:13 -03004957 rxq->last_desc = rxq->size - 1;
4958
4959 /* Zero occupied and non-occupied counters - direct access */
4960 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
4961
4962 /* Set Rx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004963 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01004964 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzonib02f31f2017-03-07 16:53:12 +01004965 if (port->priv->hw_version == MVPP21)
4966 rxq_dma = rxq->descs_dma;
4967 else
4968 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
Thomas Petazzonia7868412017-03-07 16:53:13 +01004969 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
4970 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
4971 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02004972 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03004973
4974 /* Set Offset */
4975 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
4976
4977 /* Set coalescing pkts and time */
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01004978 mvpp2_rx_pkts_coal_set(port, rxq);
4979 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03004980
4981 /* Add number of descriptors ready for receiving packets */
4982 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
4983
4984 return 0;
4985}
4986
4987/* Push packets received by the RXQ to BM pool */
4988static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
4989 struct mvpp2_rx_queue *rxq)
4990{
4991 int rx_received, i;
4992
4993 rx_received = mvpp2_rxq_received(port, rxq->id);
4994 if (!rx_received)
4995 return;
4996
4997 for (i = 0; i < rx_received; i++) {
4998 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02004999 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5000 int pool;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005001
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005002 pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5003 MVPP2_RXD_BM_POOL_ID_OFFS;
5004
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005005 mvpp2_bm_pool_put(port, pool,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005006 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
5007 mvpp2_rxdesc_cookie_get(port, rx_desc));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005008 }
5009 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
5010}
5011
5012/* Cleanup Rx queue */
5013static void mvpp2_rxq_deinit(struct mvpp2_port *port,
5014 struct mvpp2_rx_queue *rxq)
5015{
Thomas Petazzonia7868412017-03-07 16:53:13 +01005016 int cpu;
5017
Marcin Wojtas3f518502014-07-10 16:52:13 -03005018 mvpp2_rxq_drop_pkts(port, rxq);
5019
5020 if (rxq->descs)
5021 dma_free_coherent(port->dev->dev.parent,
5022 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
5023 rxq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005024 rxq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005025
5026 rxq->descs = NULL;
5027 rxq->last_desc = 0;
5028 rxq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005029 rxq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005030
5031 /* Clear Rx descriptors queue starting address and size;
5032 * free descriptor number
5033 */
5034 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005035 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005036 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5037 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
5038 mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005039 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005040}
5041
5042/* Create and initialize a Tx queue */
5043static int mvpp2_txq_init(struct mvpp2_port *port,
5044 struct mvpp2_tx_queue *txq)
5045{
5046 u32 val;
5047 int cpu, desc, desc_per_txq, tx_port_num;
5048 struct mvpp2_txq_pcpu *txq_pcpu;
5049
5050 txq->size = port->tx_ring_size;
5051
5052 /* Allocate memory for Tx descriptors */
5053 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
5054 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005055 &txq->descs_dma, GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005056 if (!txq->descs)
5057 return -ENOMEM;
5058
Marcin Wojtas3f518502014-07-10 16:52:13 -03005059 txq->last_desc = txq->size - 1;
5060
5061 /* Set Tx descriptors queue starting address - indirect access */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005062 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005063 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5064 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
5065 txq->descs_dma);
5066 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
5067 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
5068 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
5069 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
5070 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
5071 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005072 val &= ~MVPP2_TXQ_PENDING_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005073 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005074
5075 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
5076 * for each existing TXQ.
5077 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
5078 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
5079 */
5080 desc_per_txq = 16;
5081 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
5082 (txq->log_id * desc_per_txq);
5083
Thomas Petazzonia7868412017-03-07 16:53:13 +01005084 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
5085 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
5086 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005087 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005088
5089 /* WRR / EJP configuration - indirect access */
5090 tx_port_num = mvpp2_egress_port(port);
5091 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5092
5093 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
5094 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
5095 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
5096 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
5097 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
5098
5099 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
5100 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
5101 val);
5102
5103 for_each_present_cpu(cpu) {
5104 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5105 txq_pcpu->size = txq->size;
Markus Elfring02c91ec2017-04-17 08:09:07 +02005106 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
5107 sizeof(*txq_pcpu->buffs),
5108 GFP_KERNEL);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005109 if (!txq_pcpu->buffs)
Markus Elfring20b1e162017-04-17 12:58:33 +02005110 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005111
5112 txq_pcpu->count = 0;
5113 txq_pcpu->reserved_num = 0;
5114 txq_pcpu->txq_put_index = 0;
5115 txq_pcpu->txq_get_index = 0;
5116 }
5117
5118 return 0;
Markus Elfring20b1e162017-04-17 12:58:33 +02005119cleanup:
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005120 for_each_present_cpu(cpu) {
5121 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005122 kfree(txq_pcpu->buffs);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005123 }
5124
5125 dma_free_coherent(port->dev->dev.parent,
5126 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005127 txq->descs, txq->descs_dma);
Marcin Wojtas71ce3912015-08-06 19:00:29 +02005128
5129 return -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005130}
5131
5132/* Free allocated TXQ resources */
5133static void mvpp2_txq_deinit(struct mvpp2_port *port,
5134 struct mvpp2_tx_queue *txq)
5135{
5136 struct mvpp2_txq_pcpu *txq_pcpu;
5137 int cpu;
5138
5139 for_each_present_cpu(cpu) {
5140 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
Thomas Petazzoni83544912016-12-21 11:28:49 +01005141 kfree(txq_pcpu->buffs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005142 }
5143
5144 if (txq->descs)
5145 dma_free_coherent(port->dev->dev.parent,
5146 txq->size * MVPP2_DESC_ALIGNED_SIZE,
Thomas Petazzoni20396132017-03-07 16:53:00 +01005147 txq->descs, txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005148
5149 txq->descs = NULL;
5150 txq->last_desc = 0;
5151 txq->next_desc_to_proc = 0;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005152 txq->descs_dma = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005153
5154 /* Set minimum bandwidth for disabled TXQs */
5155 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
5156
5157 /* Set Tx descriptors queue starting address and size */
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005158 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005159 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5160 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
5161 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005162 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005163}
5164
5165/* Cleanup Tx ports */
5166static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
5167{
5168 struct mvpp2_txq_pcpu *txq_pcpu;
5169 int delay, pending, cpu;
5170 u32 val;
5171
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005172 cpu = get_cpu();
Thomas Petazzonia7868412017-03-07 16:53:13 +01005173 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5174 val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005175 val |= MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005176 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005177
5178 /* The napi queue has been stopped so wait for all packets
5179 * to be transmitted.
5180 */
5181 delay = 0;
5182 do {
5183 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
5184 netdev_warn(port->dev,
5185 "port %d: cleaning queue %d timed out\n",
5186 port->id, txq->log_id);
5187 break;
5188 }
5189 mdelay(1);
5190 delay++;
5191
Thomas Petazzonia7868412017-03-07 16:53:13 +01005192 pending = mvpp2_percpu_read(port->priv, cpu,
5193 MVPP2_TXQ_PENDING_REG);
5194 pending &= MVPP2_TXQ_PENDING_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005195 } while (pending);
5196
5197 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
Thomas Petazzonia7868412017-03-07 16:53:13 +01005198 mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
Thomas Petazzonia704bb52017-06-10 23:18:22 +02005199 put_cpu();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005200
5201 for_each_present_cpu(cpu) {
5202 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5203
5204 /* Release all packets */
5205 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
5206
5207 /* Reset queue */
5208 txq_pcpu->count = 0;
5209 txq_pcpu->txq_put_index = 0;
5210 txq_pcpu->txq_get_index = 0;
5211 }
5212}
5213
5214/* Cleanup all Tx queues */
5215static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
5216{
5217 struct mvpp2_tx_queue *txq;
5218 int queue;
5219 u32 val;
5220
5221 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
5222
5223 /* Reset Tx ports and delete Tx queues */
5224 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
5225 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5226
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005227 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005228 txq = port->txqs[queue];
5229 mvpp2_txq_clean(port, txq);
5230 mvpp2_txq_deinit(port, txq);
5231 }
5232
5233 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5234
5235 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
5236 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5237}
5238
5239/* Cleanup all Rx queues */
5240static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
5241{
5242 int queue;
5243
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005244 for (queue = 0; queue < port->nrxqs; queue++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005245 mvpp2_rxq_deinit(port, port->rxqs[queue]);
5246}
5247
5248/* Init all Rx queues for port */
5249static int mvpp2_setup_rxqs(struct mvpp2_port *port)
5250{
5251 int queue, err;
5252
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005253 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005254 err = mvpp2_rxq_init(port, port->rxqs[queue]);
5255 if (err)
5256 goto err_cleanup;
5257 }
5258 return 0;
5259
5260err_cleanup:
5261 mvpp2_cleanup_rxqs(port);
5262 return err;
5263}
5264
5265/* Init all tx queues for port */
5266static int mvpp2_setup_txqs(struct mvpp2_port *port)
5267{
5268 struct mvpp2_tx_queue *txq;
5269 int queue, err;
5270
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005271 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005272 txq = port->txqs[queue];
5273 err = mvpp2_txq_init(port, txq);
5274 if (err)
5275 goto err_cleanup;
5276 }
5277
Marcin Wojtas3f518502014-07-10 16:52:13 -03005278 on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5279 return 0;
5280
5281err_cleanup:
5282 mvpp2_cleanup_txqs(port);
5283 return err;
5284}
5285
5286/* The callback for per-port interrupt */
5287static irqreturn_t mvpp2_isr(int irq, void *dev_id)
5288{
5289 struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
5290
5291 mvpp2_interrupts_disable(port);
5292
5293 napi_schedule(&port->napi);
5294
5295 return IRQ_HANDLED;
5296}
5297
5298/* Adjust link */
5299static void mvpp2_link_event(struct net_device *dev)
5300{
5301 struct mvpp2_port *port = netdev_priv(dev);
Philippe Reynes8e072692016-06-28 00:08:11 +02005302 struct phy_device *phydev = dev->phydev;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005303 int status_change = 0;
5304 u32 val;
5305
5306 if (phydev->link) {
5307 if ((port->speed != phydev->speed) ||
5308 (port->duplex != phydev->duplex)) {
5309 u32 val;
5310
5311 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5312 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
5313 MVPP2_GMAC_CONFIG_GMII_SPEED |
5314 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
5315 MVPP2_GMAC_AN_SPEED_EN |
5316 MVPP2_GMAC_AN_DUPLEX_EN);
5317
5318 if (phydev->duplex)
5319 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
5320
5321 if (phydev->speed == SPEED_1000)
5322 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
Thomas Petazzoni2add5112014-07-27 23:21:35 +02005323 else if (phydev->speed == SPEED_100)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005324 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
5325
5326 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5327
5328 port->duplex = phydev->duplex;
5329 port->speed = phydev->speed;
5330 }
5331 }
5332
5333 if (phydev->link != port->link) {
5334 if (!phydev->link) {
5335 port->duplex = -1;
5336 port->speed = 0;
5337 }
5338
5339 port->link = phydev->link;
5340 status_change = 1;
5341 }
5342
5343 if (status_change) {
5344 if (phydev->link) {
5345 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5346 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
5347 MVPP2_GMAC_FORCE_LINK_DOWN);
5348 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5349 mvpp2_egress_enable(port);
5350 mvpp2_ingress_enable(port);
5351 } else {
5352 mvpp2_ingress_disable(port);
5353 mvpp2_egress_disable(port);
5354 }
5355 phy_print_status(phydev);
5356 }
5357}
5358
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005359static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
5360{
5361 ktime_t interval;
5362
5363 if (!port_pcpu->timer_scheduled) {
5364 port_pcpu->timer_scheduled = true;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01005365 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005366 hrtimer_start(&port_pcpu->tx_done_timer, interval,
5367 HRTIMER_MODE_REL_PINNED);
5368 }
5369}
5370
5371static void mvpp2_tx_proc_cb(unsigned long data)
5372{
5373 struct net_device *dev = (struct net_device *)data;
5374 struct mvpp2_port *port = netdev_priv(dev);
5375 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5376 unsigned int tx_todo, cause;
5377
5378 if (!netif_running(dev))
5379 return;
5380 port_pcpu->timer_scheduled = false;
5381
5382 /* Process all the Tx queues */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02005383 cause = (1 << port->ntxqs) - 1;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005384 tx_todo = mvpp2_tx_done(port, cause);
5385
5386 /* Set the timer in case not all the packets were processed */
5387 if (tx_todo)
5388 mvpp2_timer_set(port_pcpu);
5389}
5390
5391static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
5392{
5393 struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
5394 struct mvpp2_port_pcpu,
5395 tx_done_timer);
5396
5397 tasklet_schedule(&port_pcpu->tx_done_tasklet);
5398
5399 return HRTIMER_NORESTART;
5400}
5401
Marcin Wojtas3f518502014-07-10 16:52:13 -03005402/* Main RX/TX processing routines */
5403
5404/* Display more error info */
5405static void mvpp2_rx_error(struct mvpp2_port *port,
5406 struct mvpp2_rx_desc *rx_desc)
5407{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005408 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5409 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005410
5411 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
5412 case MVPP2_RXD_ERR_CRC:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005413 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
5414 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005415 break;
5416 case MVPP2_RXD_ERR_OVERRUN:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005417 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
5418 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005419 break;
5420 case MVPP2_RXD_ERR_RESOURCE:
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005421 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
5422 status, sz);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005423 break;
5424 }
5425}
5426
5427/* Handle RX checksum offload */
5428static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
5429 struct sk_buff *skb)
5430{
5431 if (((status & MVPP2_RXD_L3_IP4) &&
5432 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
5433 (status & MVPP2_RXD_L3_IP6))
5434 if (((status & MVPP2_RXD_L4_UDP) ||
5435 (status & MVPP2_RXD_L4_TCP)) &&
5436 (status & MVPP2_RXD_L4_CSUM_OK)) {
5437 skb->csum = 0;
5438 skb->ip_summed = CHECKSUM_UNNECESSARY;
5439 return;
5440 }
5441
5442 skb->ip_summed = CHECKSUM_NONE;
5443}
5444
5445/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
5446static int mvpp2_rx_refill(struct mvpp2_port *port,
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005447 struct mvpp2_bm_pool *bm_pool, int pool)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005448{
Thomas Petazzoni20396132017-03-07 16:53:00 +01005449 dma_addr_t dma_addr;
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005450 phys_addr_t phys_addr;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005451 void *buf;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005452
Marcin Wojtas3f518502014-07-10 16:52:13 -03005453 /* No recycle or too many buffers are in use, so allocate a new skb */
Thomas Petazzoni4e4a1052017-03-07 16:53:04 +01005454 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
5455 GFP_ATOMIC);
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005456 if (!buf)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005457 return -ENOMEM;
5458
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005459 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Thomas Petazzoni7ef7e1d2017-02-21 11:28:07 +01005460
Marcin Wojtas3f518502014-07-10 16:52:13 -03005461 return 0;
5462}
5463
5464/* Handle tx checksum */
5465static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
5466{
5467 if (skb->ip_summed == CHECKSUM_PARTIAL) {
5468 int ip_hdr_len = 0;
5469 u8 l4_proto;
5470
5471 if (skb->protocol == htons(ETH_P_IP)) {
5472 struct iphdr *ip4h = ip_hdr(skb);
5473
5474 /* Calculate IPv4 checksum and L4 checksum */
5475 ip_hdr_len = ip4h->ihl;
5476 l4_proto = ip4h->protocol;
5477 } else if (skb->protocol == htons(ETH_P_IPV6)) {
5478 struct ipv6hdr *ip6h = ipv6_hdr(skb);
5479
5480 /* Read l4_protocol from one of IPv6 extra headers */
5481 if (skb_network_header_len(skb) > 0)
5482 ip_hdr_len = (skb_network_header_len(skb) >> 2);
5483 l4_proto = ip6h->nexthdr;
5484 } else {
5485 return MVPP2_TXD_L4_CSUM_NOT;
5486 }
5487
5488 return mvpp2_txq_desc_csum(skb_network_offset(skb),
5489 skb->protocol, ip_hdr_len, l4_proto);
5490 }
5491
5492 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
5493}
5494
Marcin Wojtas3f518502014-07-10 16:52:13 -03005495/* Main rx processing */
5496static int mvpp2_rx(struct mvpp2_port *port, int rx_todo,
5497 struct mvpp2_rx_queue *rxq)
5498{
5499 struct net_device *dev = port->dev;
Marcin Wojtasb5015852015-12-03 15:20:51 +01005500 int rx_received;
5501 int rx_done = 0;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005502 u32 rcvd_pkts = 0;
5503 u32 rcvd_bytes = 0;
5504
5505 /* Get number of received packets and clamp the to-do */
5506 rx_received = mvpp2_rxq_received(port, rxq->id);
5507 if (rx_todo > rx_received)
5508 rx_todo = rx_received;
5509
Marcin Wojtasb5015852015-12-03 15:20:51 +01005510 while (rx_done < rx_todo) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005511 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
5512 struct mvpp2_bm_pool *bm_pool;
5513 struct sk_buff *skb;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005514 unsigned int frag_size;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005515 dma_addr_t dma_addr;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005516 phys_addr_t phys_addr;
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005517 u32 rx_status;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005518 int pool, rx_bytes, err;
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005519 void *data;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005520
Marcin Wojtasb5015852015-12-03 15:20:51 +01005521 rx_done++;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005522 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
5523 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
5524 rx_bytes -= MVPP2_MH_SIZE;
5525 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
5526 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
5527 data = (void *)phys_to_virt(phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005528
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005529 pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5530 MVPP2_RXD_BM_POOL_ID_OFFS;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005531 bm_pool = &port->priv->bm_pools[pool];
Marcin Wojtas3f518502014-07-10 16:52:13 -03005532
5533 /* In case of an error, release the requested buffer pointer
5534 * to the Buffer Manager. This request process is controlled
5535 * by the hardware, and the information about the buffer is
5536 * comprised by the RX descriptor.
5537 */
5538 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
Markus Elfring8a524882017-04-17 10:52:02 +02005539err_drop_frame:
Marcin Wojtas3f518502014-07-10 16:52:13 -03005540 dev->stats.rx_errors++;
5541 mvpp2_rx_error(port, rx_desc);
Marcin Wojtasb5015852015-12-03 15:20:51 +01005542 /* Return the buffer to the pool */
Thomas Petazzoni7d7627b2017-06-22 14:23:20 +02005543 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005544 continue;
5545 }
5546
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005547 if (bm_pool->frag_size > PAGE_SIZE)
5548 frag_size = 0;
5549 else
5550 frag_size = bm_pool->frag_size;
5551
5552 skb = build_skb(data, frag_size);
5553 if (!skb) {
5554 netdev_warn(port->dev, "skb build failed\n");
5555 goto err_drop_frame;
5556 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03005557
Thomas Petazzoni56b8aae2017-06-10 23:18:21 +02005558 err = mvpp2_rx_refill(port, bm_pool, pool);
Marcin Wojtasb5015852015-12-03 15:20:51 +01005559 if (err) {
5560 netdev_err(port->dev, "failed to refill BM pools\n");
5561 goto err_drop_frame;
5562 }
5563
Thomas Petazzoni20396132017-03-07 16:53:00 +01005564 dma_unmap_single(dev->dev.parent, dma_addr,
Marcin Wojtas4229d502015-12-03 15:20:50 +01005565 bm_pool->buf_size, DMA_FROM_DEVICE);
5566
Marcin Wojtas3f518502014-07-10 16:52:13 -03005567 rcvd_pkts++;
5568 rcvd_bytes += rx_bytes;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005569
Thomas Petazzoni0e037282017-02-21 11:28:12 +01005570 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005571 skb_put(skb, rx_bytes);
5572 skb->protocol = eth_type_trans(skb, dev);
5573 mvpp2_rx_csum(port, rx_status, skb);
5574
5575 napi_gro_receive(&port->napi, skb);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005576 }
5577
5578 if (rcvd_pkts) {
5579 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
5580
5581 u64_stats_update_begin(&stats->syncp);
5582 stats->rx_packets += rcvd_pkts;
5583 stats->rx_bytes += rcvd_bytes;
5584 u64_stats_update_end(&stats->syncp);
5585 }
5586
5587 /* Update Rx queue management counters */
5588 wmb();
Marcin Wojtasb5015852015-12-03 15:20:51 +01005589 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005590
5591 return rx_todo;
5592}
5593
5594static inline void
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005595tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
Marcin Wojtas3f518502014-07-10 16:52:13 -03005596 struct mvpp2_tx_desc *desc)
5597{
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005598 dma_addr_t buf_dma_addr =
5599 mvpp2_txdesc_dma_addr_get(port, desc);
5600 size_t buf_sz =
5601 mvpp2_txdesc_size_get(port, desc);
5602 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
5603 buf_sz, DMA_TO_DEVICE);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005604 mvpp2_txq_desc_put(txq);
5605}
5606
5607/* Handle tx fragmentation processing */
5608static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
5609 struct mvpp2_tx_queue *aggr_txq,
5610 struct mvpp2_tx_queue *txq)
5611{
5612 struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
5613 struct mvpp2_tx_desc *tx_desc;
5614 int i;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005615 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005616
5617 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
5618 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5619 void *addr = page_address(frag->page.p) + frag->page_offset;
5620
5621 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005622 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
5623 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005624
Thomas Petazzoni20396132017-03-07 16:53:00 +01005625 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005626 frag->size,
5627 DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01005628 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005629 mvpp2_txq_desc_put(txq);
Markus Elfring32bae632017-04-17 11:36:34 +02005630 goto cleanup;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005631 }
5632
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005633 mvpp2_txdesc_offset_set(port, tx_desc,
5634 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
5635 mvpp2_txdesc_dma_addr_set(port, tx_desc,
5636 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005637
5638 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
5639 /* Last descriptor */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005640 mvpp2_txdesc_cmd_set(port, tx_desc,
5641 MVPP2_TXD_L_DESC);
5642 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005643 } else {
5644 /* Descriptor in the middle: Not First, Not Last */
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005645 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
5646 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005647 }
5648 }
5649
5650 return 0;
Markus Elfring32bae632017-04-17 11:36:34 +02005651cleanup:
Marcin Wojtas3f518502014-07-10 16:52:13 -03005652 /* Release all descriptors that were used to map fragments of
5653 * this packet, as well as the corresponding DMA mappings
5654 */
5655 for (i = i - 1; i >= 0; i--) {
5656 tx_desc = txq->descs + i;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005657 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005658 }
5659
5660 return -ENOMEM;
5661}
5662
5663/* Main tx processing */
5664static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
5665{
5666 struct mvpp2_port *port = netdev_priv(dev);
5667 struct mvpp2_tx_queue *txq, *aggr_txq;
5668 struct mvpp2_txq_pcpu *txq_pcpu;
5669 struct mvpp2_tx_desc *tx_desc;
Thomas Petazzoni20396132017-03-07 16:53:00 +01005670 dma_addr_t buf_dma_addr;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005671 int frags = 0;
5672 u16 txq_id;
5673 u32 tx_cmd;
5674
5675 txq_id = skb_get_queue_mapping(skb);
5676 txq = port->txqs[txq_id];
5677 txq_pcpu = this_cpu_ptr(txq->pcpu);
5678 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
5679
5680 frags = skb_shinfo(skb)->nr_frags + 1;
5681
5682 /* Check number of available descriptors */
5683 if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
5684 mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
5685 txq_pcpu, frags)) {
5686 frags = 0;
5687 goto out;
5688 }
5689
5690 /* Get a descriptor for the first part of the packet */
5691 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005692 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
5693 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
Marcin Wojtas3f518502014-07-10 16:52:13 -03005694
Thomas Petazzoni20396132017-03-07 16:53:00 +01005695 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005696 skb_headlen(skb), DMA_TO_DEVICE);
Thomas Petazzoni20396132017-03-07 16:53:00 +01005697 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03005698 mvpp2_txq_desc_put(txq);
5699 frags = 0;
5700 goto out;
5701 }
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005702
5703 mvpp2_txdesc_offset_set(port, tx_desc,
5704 buf_dma_addr & MVPP2_TX_DESC_ALIGN);
5705 mvpp2_txdesc_dma_addr_set(port, tx_desc,
5706 buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005707
5708 tx_cmd = mvpp2_skb_tx_csum(port, skb);
5709
5710 if (frags == 1) {
5711 /* First and Last descriptor */
5712 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005713 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
5714 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005715 } else {
5716 /* First but not Last */
5717 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005718 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
5719 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005720
5721 /* Continue with other skb fragments */
5722 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
Thomas Petazzoniac3dd2772017-03-07 16:53:05 +01005723 tx_desc_unmap_put(port, txq, tx_desc);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005724 frags = 0;
5725 goto out;
5726 }
5727 }
5728
5729 txq_pcpu->reserved_num -= frags;
5730 txq_pcpu->count += frags;
5731 aggr_txq->count += frags;
5732
5733 /* Enable transmit */
5734 wmb();
5735 mvpp2_aggr_txq_pend_desc_add(port, frags);
5736
5737 if (txq_pcpu->size - txq_pcpu->count < MAX_SKB_FRAGS + 1) {
5738 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
5739
5740 netif_tx_stop_queue(nq);
5741 }
5742out:
5743 if (frags > 0) {
5744 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
5745
5746 u64_stats_update_begin(&stats->syncp);
5747 stats->tx_packets++;
5748 stats->tx_bytes += skb->len;
5749 u64_stats_update_end(&stats->syncp);
5750 } else {
5751 dev->stats.tx_dropped++;
5752 dev_kfree_skb_any(skb);
5753 }
5754
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005755 /* Finalize TX processing */
5756 if (txq_pcpu->count >= txq->done_pkts_coal)
5757 mvpp2_txq_done(port, txq, txq_pcpu);
5758
5759 /* Set the timer in case not all frags were processed */
5760 if (txq_pcpu->count <= frags && txq_pcpu->count > 0) {
5761 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
5762
5763 mvpp2_timer_set(port_pcpu);
5764 }
5765
Marcin Wojtas3f518502014-07-10 16:52:13 -03005766 return NETDEV_TX_OK;
5767}
5768
5769static inline void mvpp2_cause_error(struct net_device *dev, int cause)
5770{
5771 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
5772 netdev_err(dev, "FCS error\n");
5773 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
5774 netdev_err(dev, "rx fifo overrun error\n");
5775 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
5776 netdev_err(dev, "tx fifo underrun error\n");
5777}
5778
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005779static int mvpp2_poll(struct napi_struct *napi, int budget)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005780{
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005781 u32 cause_rx_tx, cause_rx, cause_misc;
5782 int rx_done = 0;
5783 struct mvpp2_port *port = netdev_priv(napi->dev);
Thomas Petazzonia7868412017-03-07 16:53:13 +01005784 int cpu = smp_processor_id();
Marcin Wojtas3f518502014-07-10 16:52:13 -03005785
5786 /* Rx/Tx cause register
5787 *
5788 * Bits 0-15: each bit indicates received packets on the Rx queue
5789 * (bit 0 is for Rx queue 0).
5790 *
5791 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
5792 * (bit 16 is for Tx queue 0).
5793 *
5794 * Each CPU has its own Rx/Tx cause register
5795 */
Thomas Petazzonia7868412017-03-07 16:53:13 +01005796 cause_rx_tx = mvpp2_percpu_read(port->priv, cpu,
5797 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
Marcin Wojtasedc660f2015-08-06 19:00:30 +02005798 cause_rx_tx &= ~MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
Marcin Wojtas3f518502014-07-10 16:52:13 -03005799 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
5800
5801 if (cause_misc) {
5802 mvpp2_cause_error(port->dev, cause_misc);
5803
5804 /* Clear the cause register */
5805 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01005806 mvpp2_percpu_write(port->priv, cpu,
5807 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
5808 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005809 }
5810
Marcin Wojtas3f518502014-07-10 16:52:13 -03005811 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
5812
5813 /* Process RX packets */
5814 cause_rx |= port->pending_cause_rx;
5815 while (cause_rx && budget > 0) {
5816 int count;
5817 struct mvpp2_rx_queue *rxq;
5818
5819 rxq = mvpp2_get_rx_queue(port, cause_rx);
5820 if (!rxq)
5821 break;
5822
5823 count = mvpp2_rx(port, budget, rxq);
5824 rx_done += count;
5825 budget -= count;
5826 if (budget > 0) {
5827 /* Clear the bit associated to this Rx queue
5828 * so that next iteration will continue from
5829 * the next Rx queue.
5830 */
5831 cause_rx &= ~(1 << rxq->logic_rxq);
5832 }
5833 }
5834
5835 if (budget > 0) {
5836 cause_rx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08005837 napi_complete_done(napi, rx_done);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005838
5839 mvpp2_interrupts_enable(port);
5840 }
5841 port->pending_cause_rx = cause_rx;
5842 return rx_done;
5843}
5844
5845/* Set hw internals when starting port */
5846static void mvpp2_start_dev(struct mvpp2_port *port)
5847{
Philippe Reynes8e072692016-06-28 00:08:11 +02005848 struct net_device *ndev = port->dev;
5849
Marcin Wojtas3f518502014-07-10 16:52:13 -03005850 mvpp2_gmac_max_rx_size_set(port);
5851 mvpp2_txp_max_tx_size_set(port);
5852
5853 napi_enable(&port->napi);
5854
5855 /* Enable interrupts on all CPUs */
5856 mvpp2_interrupts_enable(port);
5857
5858 mvpp2_port_enable(port);
Philippe Reynes8e072692016-06-28 00:08:11 +02005859 phy_start(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005860 netif_tx_start_all_queues(port->dev);
5861}
5862
5863/* Set hw internals when stopping port */
5864static void mvpp2_stop_dev(struct mvpp2_port *port)
5865{
Philippe Reynes8e072692016-06-28 00:08:11 +02005866 struct net_device *ndev = port->dev;
5867
Marcin Wojtas3f518502014-07-10 16:52:13 -03005868 /* Stop new packets from arriving to RXQs */
5869 mvpp2_ingress_disable(port);
5870
5871 mdelay(10);
5872
5873 /* Disable interrupts on all CPUs */
5874 mvpp2_interrupts_disable(port);
5875
5876 napi_disable(&port->napi);
5877
5878 netif_carrier_off(port->dev);
5879 netif_tx_stop_all_queues(port->dev);
5880
5881 mvpp2_egress_disable(port);
5882 mvpp2_port_disable(port);
Philippe Reynes8e072692016-06-28 00:08:11 +02005883 phy_stop(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005884}
5885
Marcin Wojtas3f518502014-07-10 16:52:13 -03005886static int mvpp2_check_ringparam_valid(struct net_device *dev,
5887 struct ethtool_ringparam *ring)
5888{
5889 u16 new_rx_pending = ring->rx_pending;
5890 u16 new_tx_pending = ring->tx_pending;
5891
5892 if (ring->rx_pending == 0 || ring->tx_pending == 0)
5893 return -EINVAL;
5894
5895 if (ring->rx_pending > MVPP2_MAX_RXD)
5896 new_rx_pending = MVPP2_MAX_RXD;
5897 else if (!IS_ALIGNED(ring->rx_pending, 16))
5898 new_rx_pending = ALIGN(ring->rx_pending, 16);
5899
5900 if (ring->tx_pending > MVPP2_MAX_TXD)
5901 new_tx_pending = MVPP2_MAX_TXD;
5902 else if (!IS_ALIGNED(ring->tx_pending, 32))
5903 new_tx_pending = ALIGN(ring->tx_pending, 32);
5904
5905 if (ring->rx_pending != new_rx_pending) {
5906 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
5907 ring->rx_pending, new_rx_pending);
5908 ring->rx_pending = new_rx_pending;
5909 }
5910
5911 if (ring->tx_pending != new_tx_pending) {
5912 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
5913 ring->tx_pending, new_tx_pending);
5914 ring->tx_pending = new_tx_pending;
5915 }
5916
5917 return 0;
5918}
5919
Thomas Petazzoni26975822017-03-07 16:53:14 +01005920static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
Marcin Wojtas3f518502014-07-10 16:52:13 -03005921{
5922 u32 mac_addr_l, mac_addr_m, mac_addr_h;
5923
5924 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
5925 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
5926 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
5927 addr[0] = (mac_addr_h >> 24) & 0xFF;
5928 addr[1] = (mac_addr_h >> 16) & 0xFF;
5929 addr[2] = (mac_addr_h >> 8) & 0xFF;
5930 addr[3] = mac_addr_h & 0xFF;
5931 addr[4] = mac_addr_m & 0xFF;
5932 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
5933}
5934
5935static int mvpp2_phy_connect(struct mvpp2_port *port)
5936{
5937 struct phy_device *phy_dev;
5938
5939 phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
5940 port->phy_interface);
5941 if (!phy_dev) {
5942 netdev_err(port->dev, "cannot connect to phy\n");
5943 return -ENODEV;
5944 }
5945 phy_dev->supported &= PHY_GBIT_FEATURES;
5946 phy_dev->advertising = phy_dev->supported;
5947
Marcin Wojtas3f518502014-07-10 16:52:13 -03005948 port->link = 0;
5949 port->duplex = 0;
5950 port->speed = 0;
5951
5952 return 0;
5953}
5954
5955static void mvpp2_phy_disconnect(struct mvpp2_port *port)
5956{
Philippe Reynes8e072692016-06-28 00:08:11 +02005957 struct net_device *ndev = port->dev;
5958
5959 phy_disconnect(ndev->phydev);
Marcin Wojtas3f518502014-07-10 16:52:13 -03005960}
5961
5962static int mvpp2_open(struct net_device *dev)
5963{
5964 struct mvpp2_port *port = netdev_priv(dev);
5965 unsigned char mac_bcast[ETH_ALEN] = {
5966 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5967 int err;
5968
5969 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
5970 if (err) {
5971 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
5972 return err;
5973 }
5974 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
5975 dev->dev_addr, true);
5976 if (err) {
5977 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
5978 return err;
5979 }
5980 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
5981 if (err) {
5982 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
5983 return err;
5984 }
5985 err = mvpp2_prs_def_flow(port);
5986 if (err) {
5987 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
5988 return err;
5989 }
5990
5991 /* Allocate the Rx/Tx queues */
5992 err = mvpp2_setup_rxqs(port);
5993 if (err) {
5994 netdev_err(port->dev, "cannot allocate Rx queues\n");
5995 return err;
5996 }
5997
5998 err = mvpp2_setup_txqs(port);
5999 if (err) {
6000 netdev_err(port->dev, "cannot allocate Tx queues\n");
6001 goto err_cleanup_rxqs;
6002 }
6003
6004 err = request_irq(port->irq, mvpp2_isr, 0, dev->name, port);
6005 if (err) {
6006 netdev_err(port->dev, "cannot request IRQ %d\n", port->irq);
6007 goto err_cleanup_txqs;
6008 }
6009
6010 /* In default link is down */
6011 netif_carrier_off(port->dev);
6012
6013 err = mvpp2_phy_connect(port);
6014 if (err < 0)
6015 goto err_free_irq;
6016
6017 /* Unmask interrupts on all CPUs */
6018 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
6019
6020 mvpp2_start_dev(port);
6021
6022 return 0;
6023
6024err_free_irq:
6025 free_irq(port->irq, port);
6026err_cleanup_txqs:
6027 mvpp2_cleanup_txqs(port);
6028err_cleanup_rxqs:
6029 mvpp2_cleanup_rxqs(port);
6030 return err;
6031}
6032
6033static int mvpp2_stop(struct net_device *dev)
6034{
6035 struct mvpp2_port *port = netdev_priv(dev);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006036 struct mvpp2_port_pcpu *port_pcpu;
6037 int cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006038
6039 mvpp2_stop_dev(port);
6040 mvpp2_phy_disconnect(port);
6041
6042 /* Mask interrupts on all CPUs */
6043 on_each_cpu(mvpp2_interrupts_mask, port, 1);
6044
6045 free_irq(port->irq, port);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006046 for_each_present_cpu(cpu) {
6047 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
6048
6049 hrtimer_cancel(&port_pcpu->tx_done_timer);
6050 port_pcpu->timer_scheduled = false;
6051 tasklet_kill(&port_pcpu->tx_done_tasklet);
6052 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006053 mvpp2_cleanup_rxqs(port);
6054 mvpp2_cleanup_txqs(port);
6055
6056 return 0;
6057}
6058
6059static void mvpp2_set_rx_mode(struct net_device *dev)
6060{
6061 struct mvpp2_port *port = netdev_priv(dev);
6062 struct mvpp2 *priv = port->priv;
6063 struct netdev_hw_addr *ha;
6064 int id = port->id;
6065 bool allmulti = dev->flags & IFF_ALLMULTI;
6066
6067 mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
6068 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
6069 mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
6070
6071 /* Remove all port->id's mcast enries */
6072 mvpp2_prs_mcast_del_all(priv, id);
6073
6074 if (allmulti && !netdev_mc_empty(dev)) {
6075 netdev_for_each_mc_addr(ha, dev)
6076 mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
6077 }
6078}
6079
6080static int mvpp2_set_mac_address(struct net_device *dev, void *p)
6081{
6082 struct mvpp2_port *port = netdev_priv(dev);
6083 const struct sockaddr *addr = p;
6084 int err;
6085
6086 if (!is_valid_ether_addr(addr->sa_data)) {
6087 err = -EADDRNOTAVAIL;
Markus Elfringc1175542017-04-17 11:10:47 +02006088 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006089 }
6090
6091 if (!netif_running(dev)) {
6092 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6093 if (!err)
6094 return 0;
6095 /* Reconfigure parser to accept the original MAC address */
6096 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6097 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006098 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006099 }
6100
6101 mvpp2_stop_dev(port);
6102
6103 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
6104 if (!err)
6105 goto out_start;
6106
6107 /* Reconfigure parser accept the original MAC address */
6108 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
6109 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006110 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006111out_start:
6112 mvpp2_start_dev(port);
6113 mvpp2_egress_enable(port);
6114 mvpp2_ingress_enable(port);
6115 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02006116log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02006117 netdev_err(dev, "failed to change MAC address\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006118 return err;
6119}
6120
6121static int mvpp2_change_mtu(struct net_device *dev, int mtu)
6122{
6123 struct mvpp2_port *port = netdev_priv(dev);
6124 int err;
6125
Jarod Wilson57779872016-10-17 15:54:06 -04006126 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
6127 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
6128 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
6129 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006130 }
6131
6132 if (!netif_running(dev)) {
6133 err = mvpp2_bm_update_mtu(dev, mtu);
6134 if (!err) {
6135 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6136 return 0;
6137 }
6138
6139 /* Reconfigure BM to the original MTU */
6140 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6141 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006142 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006143 }
6144
6145 mvpp2_stop_dev(port);
6146
6147 err = mvpp2_bm_update_mtu(dev, mtu);
6148 if (!err) {
6149 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
6150 goto out_start;
6151 }
6152
6153 /* Reconfigure BM to the original MTU */
6154 err = mvpp2_bm_update_mtu(dev, dev->mtu);
6155 if (err)
Markus Elfringc1175542017-04-17 11:10:47 +02006156 goto log_error;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006157
6158out_start:
6159 mvpp2_start_dev(port);
6160 mvpp2_egress_enable(port);
6161 mvpp2_ingress_enable(port);
6162
6163 return 0;
Markus Elfringc1175542017-04-17 11:10:47 +02006164log_error:
Markus Elfringdfd42402017-04-17 11:20:41 +02006165 netdev_err(dev, "failed to change MTU\n");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006166 return err;
6167}
6168
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006169static void
Marcin Wojtas3f518502014-07-10 16:52:13 -03006170mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6171{
6172 struct mvpp2_port *port = netdev_priv(dev);
6173 unsigned int start;
6174 int cpu;
6175
6176 for_each_possible_cpu(cpu) {
6177 struct mvpp2_pcpu_stats *cpu_stats;
6178 u64 rx_packets;
6179 u64 rx_bytes;
6180 u64 tx_packets;
6181 u64 tx_bytes;
6182
6183 cpu_stats = per_cpu_ptr(port->stats, cpu);
6184 do {
6185 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
6186 rx_packets = cpu_stats->rx_packets;
6187 rx_bytes = cpu_stats->rx_bytes;
6188 tx_packets = cpu_stats->tx_packets;
6189 tx_bytes = cpu_stats->tx_bytes;
6190 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
6191
6192 stats->rx_packets += rx_packets;
6193 stats->rx_bytes += rx_bytes;
6194 stats->tx_packets += tx_packets;
6195 stats->tx_bytes += tx_bytes;
6196 }
6197
6198 stats->rx_errors = dev->stats.rx_errors;
6199 stats->rx_dropped = dev->stats.rx_dropped;
6200 stats->tx_dropped = dev->stats.tx_dropped;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006201}
6202
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006203static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6204{
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006205 int ret;
6206
Philippe Reynes8e072692016-06-28 00:08:11 +02006207 if (!dev->phydev)
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006208 return -ENOTSUPP;
6209
Philippe Reynes8e072692016-06-28 00:08:11 +02006210 ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006211 if (!ret)
6212 mvpp2_link_event(dev);
6213
6214 return ret;
6215}
6216
Marcin Wojtas3f518502014-07-10 16:52:13 -03006217/* Ethtool methods */
6218
Marcin Wojtas3f518502014-07-10 16:52:13 -03006219/* Set interrupt coalescing for ethtools */
6220static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
6221 struct ethtool_coalesce *c)
6222{
6223 struct mvpp2_port *port = netdev_priv(dev);
6224 int queue;
6225
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006226 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006227 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6228
6229 rxq->time_coal = c->rx_coalesce_usecs;
6230 rxq->pkts_coal = c->rx_max_coalesced_frames;
Thomas Petazzonid63f9e42017-02-21 11:28:02 +01006231 mvpp2_rx_pkts_coal_set(port, rxq);
6232 mvpp2_rx_time_coal_set(port, rxq);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006233 }
6234
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006235 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006236 struct mvpp2_tx_queue *txq = port->txqs[queue];
6237
6238 txq->done_pkts_coal = c->tx_max_coalesced_frames;
6239 }
6240
Marcin Wojtas3f518502014-07-10 16:52:13 -03006241 return 0;
6242}
6243
6244/* get coalescing for ethtools */
6245static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
6246 struct ethtool_coalesce *c)
6247{
6248 struct mvpp2_port *port = netdev_priv(dev);
6249
6250 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
6251 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
6252 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
6253 return 0;
6254}
6255
6256static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
6257 struct ethtool_drvinfo *drvinfo)
6258{
6259 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
6260 sizeof(drvinfo->driver));
6261 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
6262 sizeof(drvinfo->version));
6263 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
6264 sizeof(drvinfo->bus_info));
6265}
6266
6267static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
6268 struct ethtool_ringparam *ring)
6269{
6270 struct mvpp2_port *port = netdev_priv(dev);
6271
6272 ring->rx_max_pending = MVPP2_MAX_RXD;
6273 ring->tx_max_pending = MVPP2_MAX_TXD;
6274 ring->rx_pending = port->rx_ring_size;
6275 ring->tx_pending = port->tx_ring_size;
6276}
6277
6278static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
6279 struct ethtool_ringparam *ring)
6280{
6281 struct mvpp2_port *port = netdev_priv(dev);
6282 u16 prev_rx_ring_size = port->rx_ring_size;
6283 u16 prev_tx_ring_size = port->tx_ring_size;
6284 int err;
6285
6286 err = mvpp2_check_ringparam_valid(dev, ring);
6287 if (err)
6288 return err;
6289
6290 if (!netif_running(dev)) {
6291 port->rx_ring_size = ring->rx_pending;
6292 port->tx_ring_size = ring->tx_pending;
6293 return 0;
6294 }
6295
6296 /* The interface is running, so we have to force a
6297 * reallocation of the queues
6298 */
6299 mvpp2_stop_dev(port);
6300 mvpp2_cleanup_rxqs(port);
6301 mvpp2_cleanup_txqs(port);
6302
6303 port->rx_ring_size = ring->rx_pending;
6304 port->tx_ring_size = ring->tx_pending;
6305
6306 err = mvpp2_setup_rxqs(port);
6307 if (err) {
6308 /* Reallocate Rx queues with the original ring size */
6309 port->rx_ring_size = prev_rx_ring_size;
6310 ring->rx_pending = prev_rx_ring_size;
6311 err = mvpp2_setup_rxqs(port);
6312 if (err)
6313 goto err_out;
6314 }
6315 err = mvpp2_setup_txqs(port);
6316 if (err) {
6317 /* Reallocate Tx queues with the original ring size */
6318 port->tx_ring_size = prev_tx_ring_size;
6319 ring->tx_pending = prev_tx_ring_size;
6320 err = mvpp2_setup_txqs(port);
6321 if (err)
6322 goto err_clean_rxqs;
6323 }
6324
6325 mvpp2_start_dev(port);
6326 mvpp2_egress_enable(port);
6327 mvpp2_ingress_enable(port);
6328
6329 return 0;
6330
6331err_clean_rxqs:
6332 mvpp2_cleanup_rxqs(port);
6333err_out:
Markus Elfringdfd42402017-04-17 11:20:41 +02006334 netdev_err(dev, "failed to change ring parameters");
Marcin Wojtas3f518502014-07-10 16:52:13 -03006335 return err;
6336}
6337
6338/* Device ops */
6339
6340static const struct net_device_ops mvpp2_netdev_ops = {
6341 .ndo_open = mvpp2_open,
6342 .ndo_stop = mvpp2_stop,
6343 .ndo_start_xmit = mvpp2_tx,
6344 .ndo_set_rx_mode = mvpp2_set_rx_mode,
6345 .ndo_set_mac_address = mvpp2_set_mac_address,
6346 .ndo_change_mtu = mvpp2_change_mtu,
6347 .ndo_get_stats64 = mvpp2_get_stats64,
Thomas Petazzonibd695a52014-07-27 23:21:36 +02006348 .ndo_do_ioctl = mvpp2_ioctl,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006349};
6350
6351static const struct ethtool_ops mvpp2_eth_tool_ops = {
Florian Fainelli00606c42016-11-15 11:19:48 -08006352 .nway_reset = phy_ethtool_nway_reset,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006353 .get_link = ethtool_op_get_link,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006354 .set_coalesce = mvpp2_ethtool_set_coalesce,
6355 .get_coalesce = mvpp2_ethtool_get_coalesce,
6356 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
6357 .get_ringparam = mvpp2_ethtool_get_ringparam,
6358 .set_ringparam = mvpp2_ethtool_set_ringparam,
Philippe Reynesfb773e92016-06-28 00:08:12 +02006359 .get_link_ksettings = phy_ethtool_get_link_ksettings,
6360 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Marcin Wojtas3f518502014-07-10 16:52:13 -03006361};
6362
Marcin Wojtas3f518502014-07-10 16:52:13 -03006363/* Initialize port HW */
6364static int mvpp2_port_init(struct mvpp2_port *port)
6365{
6366 struct device *dev = port->dev->dev.parent;
6367 struct mvpp2 *priv = port->priv;
6368 struct mvpp2_txq_pcpu *txq_pcpu;
6369 int queue, cpu, err;
6370
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006371 /* Checks for hardware constraints */
6372 if (port->first_rxq + port->nrxqs >
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006373 MVPP2_MAX_PORTS * priv->max_port_rxqs)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006374 return -EINVAL;
6375
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006376 if (port->nrxqs % 4 || (port->nrxqs > priv->max_port_rxqs) ||
6377 (port->ntxqs > MVPP2_MAX_TXQ))
6378 return -EINVAL;
6379
Marcin Wojtas3f518502014-07-10 16:52:13 -03006380 /* Disable port */
6381 mvpp2_egress_disable(port);
6382 mvpp2_port_disable(port);
6383
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006384 port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03006385 GFP_KERNEL);
6386 if (!port->txqs)
6387 return -ENOMEM;
6388
6389 /* Associate physical Tx queues to this port and initialize.
6390 * The mapping is predefined.
6391 */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006392 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006393 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
6394 struct mvpp2_tx_queue *txq;
6395
6396 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
Christophe Jaillet177c8d12017-02-19 10:19:57 +01006397 if (!txq) {
6398 err = -ENOMEM;
6399 goto err_free_percpu;
6400 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006401
6402 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
6403 if (!txq->pcpu) {
6404 err = -ENOMEM;
6405 goto err_free_percpu;
6406 }
6407
6408 txq->id = queue_phy_id;
6409 txq->log_id = queue;
6410 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
6411 for_each_present_cpu(cpu) {
6412 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
6413 txq_pcpu->cpu = cpu;
6414 }
6415
6416 port->txqs[queue] = txq;
6417 }
6418
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006419 port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03006420 GFP_KERNEL);
6421 if (!port->rxqs) {
6422 err = -ENOMEM;
6423 goto err_free_percpu;
6424 }
6425
6426 /* Allocate and initialize Rx queue for this port */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006427 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006428 struct mvpp2_rx_queue *rxq;
6429
6430 /* Map physical Rx queue to port's logical Rx queue */
6431 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08006432 if (!rxq) {
6433 err = -ENOMEM;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006434 goto err_free_percpu;
Jisheng Zhangd82b0c22016-03-31 17:01:23 +08006435 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006436 /* Map this Rx queue to a physical queue */
6437 rxq->id = port->first_rxq + queue;
6438 rxq->port = port->id;
6439 rxq->logic_rxq = queue;
6440
6441 port->rxqs[queue] = rxq;
6442 }
6443
6444 /* Configure Rx queue group interrupt for this port */
Thomas Petazzonia73fef12017-03-07 16:53:16 +01006445 if (priv->hw_version == MVPP21) {
6446 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006447 port->nrxqs);
Thomas Petazzonia73fef12017-03-07 16:53:16 +01006448 } else {
6449 u32 val;
6450
6451 val = (port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET);
6452 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
6453
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006454 val = (port->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET);
Thomas Petazzonia73fef12017-03-07 16:53:16 +01006455 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
6456 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006457
6458 /* Create Rx descriptor rings */
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006459 for (queue = 0; queue < port->nrxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006460 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6461
6462 rxq->size = port->rx_ring_size;
6463 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
6464 rxq->time_coal = MVPP2_RX_COAL_USEC;
6465 }
6466
6467 mvpp2_ingress_disable(port);
6468
6469 /* Port default configuration */
6470 mvpp2_defaults_set(port);
6471
6472 /* Port's classifier configuration */
6473 mvpp2_cls_oversize_rxq_set(port);
6474 mvpp2_cls_port_config(port);
6475
6476 /* Provide an initial Rx packet size */
6477 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
6478
6479 /* Initialize pools for swf */
6480 err = mvpp2_swf_bm_pool_init(port);
6481 if (err)
6482 goto err_free_percpu;
6483
6484 return 0;
6485
6486err_free_percpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006487 for (queue = 0; queue < port->ntxqs; queue++) {
Marcin Wojtas3f518502014-07-10 16:52:13 -03006488 if (!port->txqs[queue])
6489 continue;
6490 free_percpu(port->txqs[queue]->pcpu);
6491 }
6492 return err;
6493}
6494
6495/* Ports initialization */
6496static int mvpp2_port_probe(struct platform_device *pdev,
6497 struct device_node *port_node,
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006498 struct mvpp2 *priv)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006499{
6500 struct device_node *phy_node;
6501 struct mvpp2_port *port;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006502 struct mvpp2_port_pcpu *port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006503 struct net_device *dev;
6504 struct resource *res;
6505 const char *dt_mac_addr;
6506 const char *mac_from;
6507 char hw_mac_addr[ETH_ALEN];
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006508 unsigned int ntxqs, nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006509 u32 id;
6510 int features;
6511 int phy_mode;
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006512 int err, i, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006513
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006514 ntxqs = MVPP2_MAX_TXQ;
6515 nrxqs = MVPP2_DEFAULT_RXQ;
6516
6517 dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006518 if (!dev)
6519 return -ENOMEM;
6520
6521 phy_node = of_parse_phandle(port_node, "phy", 0);
6522 if (!phy_node) {
6523 dev_err(&pdev->dev, "missing phy\n");
6524 err = -ENODEV;
6525 goto err_free_netdev;
6526 }
6527
6528 phy_mode = of_get_phy_mode(port_node);
6529 if (phy_mode < 0) {
6530 dev_err(&pdev->dev, "incorrect phy mode\n");
6531 err = phy_mode;
6532 goto err_free_netdev;
6533 }
6534
6535 if (of_property_read_u32(port_node, "port-id", &id)) {
6536 err = -EINVAL;
6537 dev_err(&pdev->dev, "missing port-id value\n");
6538 goto err_free_netdev;
6539 }
6540
6541 dev->tx_queue_len = MVPP2_MAX_TXD;
6542 dev->watchdog_timeo = 5 * HZ;
6543 dev->netdev_ops = &mvpp2_netdev_ops;
6544 dev->ethtool_ops = &mvpp2_eth_tool_ops;
6545
6546 port = netdev_priv(dev);
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006547 port->ntxqs = ntxqs;
6548 port->nrxqs = nrxqs;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006549
6550 port->irq = irq_of_parse_and_map(port_node, 0);
6551 if (port->irq <= 0) {
6552 err = -EINVAL;
6553 goto err_free_netdev;
6554 }
6555
6556 if (of_property_read_bool(port_node, "marvell,loopback"))
6557 port->flags |= MVPP2_F_LOOPBACK;
6558
6559 port->priv = priv;
6560 port->id = id;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006561 if (priv->hw_version == MVPP21)
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006562 port->first_rxq = port->id * port->nrxqs;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006563 else
6564 port->first_rxq = port->id * priv->max_port_rxqs;
6565
Marcin Wojtas3f518502014-07-10 16:52:13 -03006566 port->phy_node = phy_node;
6567 port->phy_interface = phy_mode;
6568
Thomas Petazzonia7868412017-03-07 16:53:13 +01006569 if (priv->hw_version == MVPP21) {
6570 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
6571 port->base = devm_ioremap_resource(&pdev->dev, res);
6572 if (IS_ERR(port->base)) {
6573 err = PTR_ERR(port->base);
6574 goto err_free_irq;
6575 }
6576 } else {
6577 if (of_property_read_u32(port_node, "gop-port-id",
6578 &port->gop_id)) {
6579 err = -EINVAL;
6580 dev_err(&pdev->dev, "missing gop-port-id value\n");
6581 goto err_free_irq;
6582 }
6583
6584 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006585 }
6586
6587 /* Alloc per-cpu stats */
6588 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
6589 if (!port->stats) {
6590 err = -ENOMEM;
6591 goto err_free_irq;
6592 }
6593
6594 dt_mac_addr = of_get_mac_address(port_node);
6595 if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
6596 mac_from = "device tree";
6597 ether_addr_copy(dev->dev_addr, dt_mac_addr);
6598 } else {
Thomas Petazzoni26975822017-03-07 16:53:14 +01006599 if (priv->hw_version == MVPP21)
6600 mvpp21_get_mac_address(port, hw_mac_addr);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006601 if (is_valid_ether_addr(hw_mac_addr)) {
6602 mac_from = "hardware";
6603 ether_addr_copy(dev->dev_addr, hw_mac_addr);
6604 } else {
6605 mac_from = "random";
6606 eth_hw_addr_random(dev);
6607 }
6608 }
6609
6610 port->tx_ring_size = MVPP2_MAX_TXD;
6611 port->rx_ring_size = MVPP2_MAX_RXD;
6612 port->dev = dev;
6613 SET_NETDEV_DEV(dev, &pdev->dev);
6614
6615 err = mvpp2_port_init(port);
6616 if (err < 0) {
6617 dev_err(&pdev->dev, "failed to init port %d\n", id);
6618 goto err_free_stats;
6619 }
Thomas Petazzoni26975822017-03-07 16:53:14 +01006620
6621 mvpp2_port_mii_set(port);
6622 mvpp2_port_periodic_xon_disable(port);
6623
6624 if (priv->hw_version == MVPP21)
6625 mvpp2_port_fc_adv_enable(port);
6626
6627 mvpp2_port_reset(port);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006628
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006629 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
6630 if (!port->pcpu) {
6631 err = -ENOMEM;
6632 goto err_free_txq_pcpu;
6633 }
6634
6635 for_each_present_cpu(cpu) {
6636 port_pcpu = per_cpu_ptr(port->pcpu, cpu);
6637
6638 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
6639 HRTIMER_MODE_REL_PINNED);
6640 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
6641 port_pcpu->timer_scheduled = false;
6642
6643 tasklet_init(&port_pcpu->tx_done_tasklet, mvpp2_tx_proc_cb,
6644 (unsigned long)dev);
6645 }
6646
Marcin Wojtas3f518502014-07-10 16:52:13 -03006647 netif_napi_add(dev, &port->napi, mvpp2_poll, NAPI_POLL_WEIGHT);
6648 features = NETIF_F_SG | NETIF_F_IP_CSUM;
6649 dev->features = features | NETIF_F_RXCSUM;
6650 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
6651 dev->vlan_features |= features;
6652
Jarod Wilson57779872016-10-17 15:54:06 -04006653 /* MTU range: 68 - 9676 */
6654 dev->min_mtu = ETH_MIN_MTU;
6655 /* 9676 == 9700 - 20 and rounding to 8 */
6656 dev->max_mtu = 9676;
6657
Marcin Wojtas3f518502014-07-10 16:52:13 -03006658 err = register_netdev(dev);
6659 if (err < 0) {
6660 dev_err(&pdev->dev, "failed to register netdev\n");
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006661 goto err_free_port_pcpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006662 }
6663 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
6664
Marcin Wojtas3f518502014-07-10 16:52:13 -03006665 priv->port_list[id] = port;
6666 return 0;
6667
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006668err_free_port_pcpu:
6669 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006670err_free_txq_pcpu:
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006671 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006672 free_percpu(port->txqs[i]->pcpu);
6673err_free_stats:
6674 free_percpu(port->stats);
6675err_free_irq:
6676 irq_dispose_mapping(port->irq);
6677err_free_netdev:
Peter Chenccb80392016-08-01 15:02:37 +08006678 of_node_put(phy_node);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006679 free_netdev(dev);
6680 return err;
6681}
6682
6683/* Ports removal routine */
6684static void mvpp2_port_remove(struct mvpp2_port *port)
6685{
6686 int i;
6687
6688 unregister_netdev(port->dev);
Peter Chenccb80392016-08-01 15:02:37 +08006689 of_node_put(port->phy_node);
Marcin Wojtasedc660f2015-08-06 19:00:30 +02006690 free_percpu(port->pcpu);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006691 free_percpu(port->stats);
Thomas Petazzoni09f83972017-08-03 10:41:57 +02006692 for (i = 0; i < port->ntxqs; i++)
Marcin Wojtas3f518502014-07-10 16:52:13 -03006693 free_percpu(port->txqs[i]->pcpu);
6694 irq_dispose_mapping(port->irq);
6695 free_netdev(port->dev);
6696}
6697
6698/* Initialize decoding windows */
6699static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
6700 struct mvpp2 *priv)
6701{
6702 u32 win_enable;
6703 int i;
6704
6705 for (i = 0; i < 6; i++) {
6706 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
6707 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
6708
6709 if (i < 4)
6710 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
6711 }
6712
6713 win_enable = 0;
6714
6715 for (i = 0; i < dram->num_cs; i++) {
6716 const struct mbus_dram_window *cs = dram->cs + i;
6717
6718 mvpp2_write(priv, MVPP2_WIN_BASE(i),
6719 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
6720 dram->mbus_dram_target_id);
6721
6722 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
6723 (cs->size - 1) & 0xffff0000);
6724
6725 win_enable |= (1 << i);
6726 }
6727
6728 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
6729}
6730
6731/* Initialize Rx FIFO's */
6732static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
6733{
6734 int port;
6735
6736 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
6737 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
6738 MVPP2_RX_FIFO_PORT_DATA_SIZE);
6739 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
6740 MVPP2_RX_FIFO_PORT_ATTR_SIZE);
6741 }
6742
6743 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
6744 MVPP2_RX_FIFO_PORT_MIN_PKT);
6745 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
6746}
6747
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01006748static void mvpp2_axi_init(struct mvpp2 *priv)
6749{
6750 u32 val, rdval, wrval;
6751
6752 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
6753
6754 /* AXI Bridge Configuration */
6755
6756 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
6757 << MVPP22_AXI_ATTR_CACHE_OFFS;
6758 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6759 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
6760
6761 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
6762 << MVPP22_AXI_ATTR_CACHE_OFFS;
6763 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6764 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
6765
6766 /* BM */
6767 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
6768 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
6769
6770 /* Descriptors */
6771 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
6772 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
6773 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
6774 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
6775
6776 /* Buffer Data */
6777 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
6778 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
6779
6780 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
6781 << MVPP22_AXI_CODE_CACHE_OFFS;
6782 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
6783 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6784 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
6785 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
6786
6787 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
6788 << MVPP22_AXI_CODE_CACHE_OFFS;
6789 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6790 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6791
6792 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
6793
6794 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
6795 << MVPP22_AXI_CODE_CACHE_OFFS;
6796 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6797 << MVPP22_AXI_CODE_DOMAIN_OFFS;
6798
6799 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
6800}
6801
Marcin Wojtas3f518502014-07-10 16:52:13 -03006802/* Initialize network controller common part HW */
6803static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
6804{
6805 const struct mbus_dram_target_info *dram_target_info;
6806 int err, i;
Marcin Wojtas08a23752014-07-21 13:48:12 -03006807 u32 val;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006808
Marcin Wojtas3f518502014-07-10 16:52:13 -03006809 /* MBUS windows configuration */
6810 dram_target_info = mv_mbus_dram_info();
6811 if (dram_target_info)
6812 mvpp2_conf_mbus_windows(dram_target_info, priv);
6813
Thomas Petazzoni6763ce32017-03-07 16:53:15 +01006814 if (priv->hw_version == MVPP22)
6815 mvpp2_axi_init(priv);
6816
Marcin Wojtas08a23752014-07-21 13:48:12 -03006817 /* Disable HW PHY polling */
Thomas Petazzoni26975822017-03-07 16:53:14 +01006818 if (priv->hw_version == MVPP21) {
6819 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6820 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
6821 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6822 } else {
6823 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
6824 val &= ~MVPP22_SMI_POLLING_EN;
6825 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
6826 }
Marcin Wojtas08a23752014-07-21 13:48:12 -03006827
Marcin Wojtas3f518502014-07-10 16:52:13 -03006828 /* Allocate and initialize aggregated TXQs */
6829 priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
Markus Elfringd7ce3ce2017-04-17 08:48:23 +02006830 sizeof(*priv->aggr_txqs),
Marcin Wojtas3f518502014-07-10 16:52:13 -03006831 GFP_KERNEL);
6832 if (!priv->aggr_txqs)
6833 return -ENOMEM;
6834
6835 for_each_present_cpu(i) {
6836 priv->aggr_txqs[i].id = i;
6837 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
6838 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i],
6839 MVPP2_AGGR_TXQ_SIZE, i, priv);
6840 if (err < 0)
6841 return err;
6842 }
6843
6844 /* Rx Fifo Init */
6845 mvpp2_rx_fifo_init(priv);
6846
Thomas Petazzoni26975822017-03-07 16:53:14 +01006847 if (priv->hw_version == MVPP21)
6848 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
6849 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006850
6851 /* Allow cache snoop when transmiting packets */
6852 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
6853
6854 /* Buffer Manager initialization */
6855 err = mvpp2_bm_init(pdev, priv);
6856 if (err < 0)
6857 return err;
6858
6859 /* Parser default initialization */
6860 err = mvpp2_prs_default_init(pdev, priv);
6861 if (err < 0)
6862 return err;
6863
6864 /* Classifier default initialization */
6865 mvpp2_cls_init(priv);
6866
6867 return 0;
6868}
6869
6870static int mvpp2_probe(struct platform_device *pdev)
6871{
6872 struct device_node *dn = pdev->dev.of_node;
6873 struct device_node *port_node;
6874 struct mvpp2 *priv;
6875 struct resource *res;
Thomas Petazzonia7868412017-03-07 16:53:13 +01006876 void __iomem *base;
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006877 int port_count, cpu;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006878 int err;
6879
Markus Elfring0b92e592017-04-17 08:38:32 +02006880 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006881 if (!priv)
6882 return -ENOMEM;
6883
Thomas Petazzonifaca9242017-03-07 16:53:06 +01006884 priv->hw_version =
6885 (unsigned long)of_device_get_match_data(&pdev->dev);
6886
Marcin Wojtas3f518502014-07-10 16:52:13 -03006887 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzonia7868412017-03-07 16:53:13 +01006888 base = devm_ioremap_resource(&pdev->dev, res);
6889 if (IS_ERR(base))
6890 return PTR_ERR(base);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006891
Thomas Petazzonia7868412017-03-07 16:53:13 +01006892 if (priv->hw_version == MVPP21) {
6893 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
6894 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
6895 if (IS_ERR(priv->lms_base))
6896 return PTR_ERR(priv->lms_base);
6897 } else {
6898 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
6899 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
6900 if (IS_ERR(priv->iface_base))
6901 return PTR_ERR(priv->iface_base);
6902 }
6903
6904 for_each_present_cpu(cpu) {
6905 u32 addr_space_sz;
6906
6907 addr_space_sz = (priv->hw_version == MVPP21 ?
6908 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
6909 priv->cpu_base[cpu] = base + cpu * addr_space_sz;
6910 }
Marcin Wojtas3f518502014-07-10 16:52:13 -03006911
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006912 if (priv->hw_version == MVPP21)
6913 priv->max_port_rxqs = 8;
6914 else
6915 priv->max_port_rxqs = 32;
6916
Marcin Wojtas3f518502014-07-10 16:52:13 -03006917 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
6918 if (IS_ERR(priv->pp_clk))
6919 return PTR_ERR(priv->pp_clk);
6920 err = clk_prepare_enable(priv->pp_clk);
6921 if (err < 0)
6922 return err;
6923
6924 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
6925 if (IS_ERR(priv->gop_clk)) {
6926 err = PTR_ERR(priv->gop_clk);
6927 goto err_pp_clk;
6928 }
6929 err = clk_prepare_enable(priv->gop_clk);
6930 if (err < 0)
6931 goto err_pp_clk;
6932
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006933 if (priv->hw_version == MVPP22) {
6934 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
6935 if (IS_ERR(priv->mg_clk)) {
6936 err = PTR_ERR(priv->mg_clk);
6937 goto err_gop_clk;
6938 }
6939
6940 err = clk_prepare_enable(priv->mg_clk);
6941 if (err < 0)
6942 goto err_gop_clk;
6943 }
6944
Marcin Wojtas3f518502014-07-10 16:52:13 -03006945 /* Get system's tclk rate */
6946 priv->tclk = clk_get_rate(priv->pp_clk);
6947
Thomas Petazzoni2067e0a2017-03-07 16:53:19 +01006948 if (priv->hw_version == MVPP22) {
6949 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
6950 if (err)
6951 goto err_mg_clk;
6952 /* Sadly, the BM pools all share the same register to
6953 * store the high 32 bits of their address. So they
6954 * must all have the same high 32 bits, which forces
6955 * us to restrict coherent memory to DMA_BIT_MASK(32).
6956 */
6957 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
6958 if (err)
6959 goto err_mg_clk;
6960 }
6961
Marcin Wojtas3f518502014-07-10 16:52:13 -03006962 /* Initialize network controller */
6963 err = mvpp2_init(pdev, priv);
6964 if (err < 0) {
6965 dev_err(&pdev->dev, "failed to initialize controller\n");
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006966 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006967 }
6968
6969 port_count = of_get_available_child_count(dn);
6970 if (port_count == 0) {
6971 dev_err(&pdev->dev, "no ports enabled\n");
Wei Yongjun575a1932014-07-20 22:02:43 +08006972 err = -ENODEV;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006973 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006974 }
6975
6976 priv->port_list = devm_kcalloc(&pdev->dev, port_count,
Markus Elfring0b92e592017-04-17 08:38:32 +02006977 sizeof(*priv->port_list),
6978 GFP_KERNEL);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006979 if (!priv->port_list) {
6980 err = -ENOMEM;
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006981 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006982 }
6983
6984 /* Initialize ports */
Marcin Wojtas3f518502014-07-10 16:52:13 -03006985 for_each_available_child_of_node(dn, port_node) {
Thomas Petazzoni59b9a312017-03-07 16:53:17 +01006986 err = mvpp2_port_probe(pdev, port_node, priv);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006987 if (err < 0)
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006988 goto err_mg_clk;
Marcin Wojtas3f518502014-07-10 16:52:13 -03006989 }
6990
6991 platform_set_drvdata(pdev, priv);
6992 return 0;
6993
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01006994err_mg_clk:
6995 if (priv->hw_version == MVPP22)
6996 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03006997err_gop_clk:
6998 clk_disable_unprepare(priv->gop_clk);
6999err_pp_clk:
7000 clk_disable_unprepare(priv->pp_clk);
7001 return err;
7002}
7003
7004static int mvpp2_remove(struct platform_device *pdev)
7005{
7006 struct mvpp2 *priv = platform_get_drvdata(pdev);
7007 struct device_node *dn = pdev->dev.of_node;
7008 struct device_node *port_node;
7009 int i = 0;
7010
7011 for_each_available_child_of_node(dn, port_node) {
7012 if (priv->port_list[i])
7013 mvpp2_port_remove(priv->port_list[i]);
7014 i++;
7015 }
7016
7017 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
7018 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
7019
7020 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
7021 }
7022
7023 for_each_present_cpu(i) {
7024 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
7025
7026 dma_free_coherent(&pdev->dev,
7027 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
7028 aggr_txq->descs,
Thomas Petazzoni20396132017-03-07 16:53:00 +01007029 aggr_txq->descs_dma);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007030 }
7031
Thomas Petazzonifceb55d2017-03-07 16:53:18 +01007032 clk_disable_unprepare(priv->mg_clk);
Marcin Wojtas3f518502014-07-10 16:52:13 -03007033 clk_disable_unprepare(priv->pp_clk);
7034 clk_disable_unprepare(priv->gop_clk);
7035
7036 return 0;
7037}
7038
7039static const struct of_device_id mvpp2_match[] = {
Thomas Petazzonifaca9242017-03-07 16:53:06 +01007040 {
7041 .compatible = "marvell,armada-375-pp2",
7042 .data = (void *)MVPP21,
7043 },
Thomas Petazzonifc5e1552017-03-07 16:53:20 +01007044 {
7045 .compatible = "marvell,armada-7k-pp22",
7046 .data = (void *)MVPP22,
7047 },
Marcin Wojtas3f518502014-07-10 16:52:13 -03007048 { }
7049};
7050MODULE_DEVICE_TABLE(of, mvpp2_match);
7051
7052static struct platform_driver mvpp2_driver = {
7053 .probe = mvpp2_probe,
7054 .remove = mvpp2_remove,
7055 .driver = {
7056 .name = MVPP2_DRIVER_NAME,
7057 .of_match_table = mvpp2_match,
7058 },
7059};
7060
7061module_platform_driver(mvpp2_driver);
7062
7063MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
7064MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
Ezequiel Garciac6340992014-07-14 10:34:47 -03007065MODULE_LICENSE("GPL v2");